libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / fortran / match.cc
blob0cd78a57a2f7ab84e9af16f1b8f04b2dc2929eeb
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "parse.h"
29 int gfc_matching_ptr_assignment = 0;
30 int gfc_matching_procptr_assignment = 0;
31 bool gfc_matching_prefix = false;
33 /* Stack of SELECT TYPE statements. */
34 gfc_select_type_stack *select_type_stack = NULL;
36 /* List of type parameter expressions. */
37 gfc_actual_arglist *type_param_spec_list;
39 /* For debugging and diagnostic purposes. Return the textual representation
40 of the intrinsic operator OP. */
41 const char *
42 gfc_op2string (gfc_intrinsic_op op)
44 switch (op)
46 case INTRINSIC_UPLUS:
47 case INTRINSIC_PLUS:
48 return "+";
50 case INTRINSIC_UMINUS:
51 case INTRINSIC_MINUS:
52 return "-";
54 case INTRINSIC_POWER:
55 return "**";
56 case INTRINSIC_CONCAT:
57 return "//";
58 case INTRINSIC_TIMES:
59 return "*";
60 case INTRINSIC_DIVIDE:
61 return "/";
63 case INTRINSIC_AND:
64 return ".and.";
65 case INTRINSIC_OR:
66 return ".or.";
67 case INTRINSIC_EQV:
68 return ".eqv.";
69 case INTRINSIC_NEQV:
70 return ".neqv.";
72 case INTRINSIC_EQ_OS:
73 return ".eq.";
74 case INTRINSIC_EQ:
75 return "==";
76 case INTRINSIC_NE_OS:
77 return ".ne.";
78 case INTRINSIC_NE:
79 return "/=";
80 case INTRINSIC_GE_OS:
81 return ".ge.";
82 case INTRINSIC_GE:
83 return ">=";
84 case INTRINSIC_LE_OS:
85 return ".le.";
86 case INTRINSIC_LE:
87 return "<=";
88 case INTRINSIC_LT_OS:
89 return ".lt.";
90 case INTRINSIC_LT:
91 return "<";
92 case INTRINSIC_GT_OS:
93 return ".gt.";
94 case INTRINSIC_GT:
95 return ">";
96 case INTRINSIC_NOT:
97 return ".not.";
99 case INTRINSIC_ASSIGN:
100 return "=";
102 case INTRINSIC_PARENTHESES:
103 return "parens";
105 case INTRINSIC_NONE:
106 return "none";
108 /* DTIO */
109 case INTRINSIC_FORMATTED:
110 return "formatted";
111 case INTRINSIC_UNFORMATTED:
112 return "unformatted";
114 default:
115 break;
118 gfc_internal_error ("gfc_op2string(): Bad code");
119 /* Not reached. */
123 /******************** Generic matching subroutines ************************/
125 /* Matches a member separator. With standard FORTRAN this is '%', but with
126 DEC structures we must carefully match dot ('.').
127 Because operators are spelled ".op.", a dotted string such as "x.y.z..."
128 can be either a component reference chain or a combination of binary
129 operations.
130 There is no real way to win because the string may be grammatically
131 ambiguous. The following rules help avoid ambiguities - they match
132 some behavior of other (older) compilers. If the rules here are changed
133 the test cases should be updated. If the user has problems with these rules
134 they probably deserve the consequences. Consider "x.y.z":
135 (1) If any user defined operator ".y." exists, this is always y(x,z)
136 (even if ".y." is the wrong type and/or x has a member y).
137 (2) Otherwise if x has a member y, and y is itself a derived type,
138 this is (x->y)->z, even if an intrinsic operator exists which
139 can handle (x,z).
140 (3) If x has no member y or (x->y) is not a derived type but ".y."
141 is an intrinsic operator (such as ".eq."), this is y(x,z).
142 (4) Lastly if there is no operator ".y." and x has no member "y", it is an
143 error.
144 It is worth noting that the logic here does not support mixed use of member
145 accessors within a single string. That is, even if x has component y and y
146 has component z, the following are all syntax errors:
147 "x%y.z" "x.y%z" "(x.y).z" "(x%y)%z"
150 match
151 gfc_match_member_sep(gfc_symbol *sym)
153 char name[GFC_MAX_SYMBOL_LEN + 1];
154 locus dot_loc, start_loc;
155 gfc_intrinsic_op iop;
156 match m;
157 gfc_symbol *tsym;
158 gfc_component *c = NULL;
160 /* What a relief: '%' is an unambiguous member separator. */
161 if (gfc_match_char ('%') == MATCH_YES)
162 return MATCH_YES;
164 /* Beware ye who enter here. */
165 if (!flag_dec_structure || !sym)
166 return MATCH_NO;
168 tsym = NULL;
170 /* We may be given either a derived type variable or the derived type
171 declaration itself (which actually contains the components);
172 we need the latter to search for components. */
173 if (gfc_fl_struct (sym->attr.flavor))
174 tsym = sym;
175 else if (gfc_bt_struct (sym->ts.type))
176 tsym = sym->ts.u.derived;
178 iop = INTRINSIC_NONE;
179 name[0] = '\0';
180 m = MATCH_NO;
182 /* If we have to reject come back here later. */
183 start_loc = gfc_current_locus;
185 /* Look for a component access next. */
186 if (gfc_match_char ('.') != MATCH_YES)
187 return MATCH_NO;
189 /* If we accept, come back here. */
190 dot_loc = gfc_current_locus;
192 /* Try to match a symbol name following the dot. */
193 if (gfc_match_name (name) != MATCH_YES)
195 gfc_error ("Expected structure component or operator name "
196 "after %<.%> at %C");
197 goto error;
200 /* If no dot follows we have "x.y" which should be a component access. */
201 if (gfc_match_char ('.') != MATCH_YES)
202 goto yes;
204 /* Now we have a string "x.y.z" which could be a nested member access
205 (x->y)->z or a binary operation y on x and z. */
207 /* First use any user-defined operators ".y." */
208 if (gfc_find_uop (name, sym->ns) != NULL)
209 goto no;
211 /* Match accesses to existing derived-type components for
212 derived-type vars: "x.y.z" = (x->y)->z */
213 c = gfc_find_component(tsym, name, false, true, NULL);
214 if (c && (gfc_bt_struct (c->ts.type) || c->ts.type == BT_CLASS))
215 goto yes;
217 /* If y is not a component or has no members, try intrinsic operators. */
218 gfc_current_locus = start_loc;
219 if (gfc_match_intrinsic_op (&iop) != MATCH_YES)
221 /* If ".y." is not an intrinsic operator but y was a valid non-
222 structure component, match and leave the trailing dot to be
223 dealt with later. */
224 if (c)
225 goto yes;
227 gfc_error ("%qs is neither a defined operator nor a "
228 "structure component in dotted string at %C", name);
229 goto error;
232 /* .y. is an intrinsic operator, overriding any possible member access. */
233 goto no;
235 /* Return keeping the current locus consistent with the match result. */
236 error:
237 m = MATCH_ERROR;
239 gfc_current_locus = start_loc;
240 return m;
241 yes:
242 gfc_current_locus = dot_loc;
243 return MATCH_YES;
247 /* This function scans the current statement counting the opened and closed
248 parenthesis to make sure they are balanced. */
250 match
251 gfc_match_parens (void)
253 locus old_loc, where;
254 int count;
255 gfc_instring instring;
256 gfc_char_t c, quote;
258 old_loc = gfc_current_locus;
259 count = 0;
260 instring = NONSTRING;
261 quote = ' ';
263 for (;;)
265 if (count > 0)
266 where = gfc_current_locus;
267 c = gfc_next_char_literal (instring);
268 if (c == '\n')
269 break;
270 if (quote == ' ' && ((c == '\'') || (c == '"')))
272 quote = c;
273 instring = INSTRING_WARN;
274 continue;
276 if (quote != ' ' && c == quote)
278 quote = ' ';
279 instring = NONSTRING;
280 continue;
283 if (c == '(' && quote == ' ')
285 count++;
287 if (c == ')' && quote == ' ')
289 count--;
290 where = gfc_current_locus;
294 gfc_current_locus = old_loc;
296 if (count != 0)
298 gfc_error ("Missing %qs in statement at or before %L",
299 count > 0? ")":"(", &where);
300 return MATCH_ERROR;
303 return MATCH_YES;
307 /* See if the next character is a special character that has
308 escaped by a \ via the -fbackslash option. */
310 match
311 gfc_match_special_char (gfc_char_t *res)
313 int len, i;
314 gfc_char_t c, n;
315 match m;
317 m = MATCH_YES;
319 switch ((c = gfc_next_char_literal (INSTRING_WARN)))
321 case 'a':
322 *res = '\a';
323 break;
324 case 'b':
325 *res = '\b';
326 break;
327 case 't':
328 *res = '\t';
329 break;
330 case 'f':
331 *res = '\f';
332 break;
333 case 'n':
334 *res = '\n';
335 break;
336 case 'r':
337 *res = '\r';
338 break;
339 case 'v':
340 *res = '\v';
341 break;
342 case '\\':
343 *res = '\\';
344 break;
345 case '0':
346 *res = '\0';
347 break;
349 case 'x':
350 case 'u':
351 case 'U':
352 /* Hexadecimal form of wide characters. */
353 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
354 n = 0;
355 for (i = 0; i < len; i++)
357 char buf[2] = { '\0', '\0' };
359 c = gfc_next_char_literal (INSTRING_WARN);
360 if (!gfc_wide_fits_in_byte (c)
361 || !gfc_check_digit ((unsigned char) c, 16))
362 return MATCH_NO;
364 buf[0] = (unsigned char) c;
365 n = n << 4;
366 n += strtol (buf, NULL, 16);
368 *res = n;
369 break;
371 default:
372 /* Unknown backslash codes are simply not expanded. */
373 m = MATCH_NO;
374 break;
377 return m;
381 /* In free form, match at least one space. Always matches in fixed
382 form. */
384 match
385 gfc_match_space (void)
387 locus old_loc;
388 char c;
390 if (gfc_current_form == FORM_FIXED)
391 return MATCH_YES;
393 old_loc = gfc_current_locus;
395 c = gfc_next_ascii_char ();
396 if (!gfc_is_whitespace (c))
398 gfc_current_locus = old_loc;
399 return MATCH_NO;
402 gfc_gobble_whitespace ();
404 return MATCH_YES;
408 /* Match an end of statement. End of statement is optional
409 whitespace, followed by a ';' or '\n' or comment '!'. If a
410 semicolon is found, we continue to eat whitespace and semicolons. */
412 match
413 gfc_match_eos (void)
415 locus old_loc;
416 int flag;
417 char c;
419 flag = 0;
421 for (;;)
423 old_loc = gfc_current_locus;
424 gfc_gobble_whitespace ();
426 c = gfc_next_ascii_char ();
427 switch (c)
429 case '!':
432 c = gfc_next_ascii_char ();
434 while (c != '\n');
436 /* Fall through. */
438 case '\n':
439 return MATCH_YES;
441 case ';':
442 flag = 1;
443 continue;
446 break;
449 gfc_current_locus = old_loc;
450 return (flag) ? MATCH_YES : MATCH_NO;
454 /* Match a literal integer on the input, setting the value on
455 MATCH_YES. Literal ints occur in kind-parameters as well as
456 old-style character length specifications. If cnt is non-NULL it
457 will be set to the number of digits.
458 When gobble_ws is false, do not skip over leading blanks. */
460 match
461 gfc_match_small_literal_int (int *value, int *cnt, bool gobble_ws)
463 locus old_loc;
464 char c;
465 int i, j;
467 old_loc = gfc_current_locus;
469 *value = -1;
470 if (gobble_ws)
471 gfc_gobble_whitespace ();
472 c = gfc_next_ascii_char ();
473 if (cnt)
474 *cnt = 0;
476 if (!ISDIGIT (c))
478 gfc_current_locus = old_loc;
479 return MATCH_NO;
482 i = c - '0';
483 j = 1;
485 for (;;)
487 old_loc = gfc_current_locus;
488 c = gfc_next_ascii_char ();
490 if (!ISDIGIT (c))
491 break;
493 i = 10 * i + c - '0';
494 j++;
496 if (i > 99999999)
498 gfc_error ("Integer too large at %C");
499 return MATCH_ERROR;
503 gfc_current_locus = old_loc;
505 *value = i;
506 if (cnt)
507 *cnt = j;
508 return MATCH_YES;
512 /* Match a small, constant integer expression, like in a kind
513 statement. On MATCH_YES, 'value' is set. */
515 match
516 gfc_match_small_int (int *value)
518 gfc_expr *expr;
519 match m;
520 int i;
522 m = gfc_match_expr (&expr);
523 if (m != MATCH_YES)
524 return m;
526 if (gfc_extract_int (expr, &i, 1))
527 m = MATCH_ERROR;
528 gfc_free_expr (expr);
530 *value = i;
531 return m;
535 /* Matches a statement label. Uses gfc_match_small_literal_int() to
536 do most of the work. */
538 match
539 gfc_match_st_label (gfc_st_label **label)
541 locus old_loc;
542 match m;
543 int i, cnt;
545 old_loc = gfc_current_locus;
547 m = gfc_match_small_literal_int (&i, &cnt);
548 if (m != MATCH_YES)
549 return m;
551 if (cnt > 5)
553 gfc_error ("Too many digits in statement label at %C");
554 goto cleanup;
557 if (i == 0)
559 gfc_error ("Statement label at %C is zero");
560 goto cleanup;
563 *label = gfc_get_st_label (i);
564 return MATCH_YES;
566 cleanup:
568 gfc_current_locus = old_loc;
569 return MATCH_ERROR;
573 /* Match and validate a label associated with a named IF, DO or SELECT
574 statement. If the symbol does not have the label attribute, we add
575 it. We also make sure the symbol does not refer to another
576 (active) block. A matched label is pointed to by gfc_new_block. */
578 static match
579 gfc_match_label (void)
581 char name[GFC_MAX_SYMBOL_LEN + 1];
582 match m;
584 gfc_new_block = NULL;
586 m = gfc_match (" %n :", name);
587 if (m != MATCH_YES)
588 return m;
590 if (gfc_get_symbol (name, NULL, &gfc_new_block))
592 gfc_error ("Label name %qs at %C is ambiguous", name);
593 return MATCH_ERROR;
596 if (gfc_new_block->attr.flavor == FL_LABEL)
598 gfc_error ("Duplicate construct label %qs at %C", name);
599 return MATCH_ERROR;
602 if (!gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
603 gfc_new_block->name, NULL))
604 return MATCH_ERROR;
606 return MATCH_YES;
610 /* See if the current input looks like a name of some sort. Modifies
611 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
612 Note that options.cc restricts max_identifier_length to not more
613 than GFC_MAX_SYMBOL_LEN.
614 When gobble_ws is false, do not skip over leading blanks. */
616 match
617 gfc_match_name (char *buffer, bool gobble_ws)
619 locus old_loc;
620 int i;
621 char c;
623 old_loc = gfc_current_locus;
624 if (gobble_ws)
625 gfc_gobble_whitespace ();
627 c = gfc_next_ascii_char ();
628 if (!(ISALPHA (c) || (c == '_' && flag_allow_leading_underscore)))
630 /* Special cases for unary minus and plus, which allows for a sensible
631 error message for code of the form 'c = exp(-a*b) )' where an
632 extra ')' appears at the end of statement. */
633 if (!gfc_error_flag_test () && c != '(' && c != '-' && c != '+')
634 gfc_error ("Invalid character in name at %C");
635 gfc_current_locus = old_loc;
636 return MATCH_NO;
639 i = 0;
643 buffer[i++] = c;
645 if (i > gfc_option.max_identifier_length)
647 gfc_error ("Name at %C is too long");
648 return MATCH_ERROR;
651 old_loc = gfc_current_locus;
652 c = gfc_next_ascii_char ();
654 while (ISALNUM (c) || c == '_' || (flag_dollar_ok && c == '$'));
656 if (c == '$' && !flag_dollar_ok)
658 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
659 "allow it as an extension", &old_loc);
660 return MATCH_ERROR;
663 buffer[i] = '\0';
664 gfc_current_locus = old_loc;
666 return MATCH_YES;
670 /* Match a symbol on the input. Modifies the pointer to the symbol
671 pointer if successful. */
673 match
674 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
676 char buffer[GFC_MAX_SYMBOL_LEN + 1];
677 match m;
679 m = gfc_match_name (buffer);
680 if (m != MATCH_YES)
681 return m;
683 if (host_assoc)
684 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
685 ? MATCH_ERROR : MATCH_YES;
687 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
688 return MATCH_ERROR;
690 return MATCH_YES;
694 match
695 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
697 gfc_symtree *st;
698 match m;
700 m = gfc_match_sym_tree (&st, host_assoc);
702 if (m == MATCH_YES)
704 if (st)
705 *matched_symbol = st->n.sym;
706 else
707 *matched_symbol = NULL;
709 else
710 *matched_symbol = NULL;
711 return m;
715 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
716 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
717 in matchexp.cc. */
719 match
720 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
722 locus orig_loc = gfc_current_locus;
723 char ch;
725 gfc_gobble_whitespace ();
726 ch = gfc_next_ascii_char ();
727 switch (ch)
729 case '+':
730 /* Matched "+". */
731 *result = INTRINSIC_PLUS;
732 return MATCH_YES;
734 case '-':
735 /* Matched "-". */
736 *result = INTRINSIC_MINUS;
737 return MATCH_YES;
739 case '=':
740 if (gfc_next_ascii_char () == '=')
742 /* Matched "==". */
743 *result = INTRINSIC_EQ;
744 return MATCH_YES;
746 break;
748 case '<':
749 if (gfc_peek_ascii_char () == '=')
751 /* Matched "<=". */
752 gfc_next_ascii_char ();
753 *result = INTRINSIC_LE;
754 return MATCH_YES;
756 /* Matched "<". */
757 *result = INTRINSIC_LT;
758 return MATCH_YES;
760 case '>':
761 if (gfc_peek_ascii_char () == '=')
763 /* Matched ">=". */
764 gfc_next_ascii_char ();
765 *result = INTRINSIC_GE;
766 return MATCH_YES;
768 /* Matched ">". */
769 *result = INTRINSIC_GT;
770 return MATCH_YES;
772 case '*':
773 if (gfc_peek_ascii_char () == '*')
775 /* Matched "**". */
776 gfc_next_ascii_char ();
777 *result = INTRINSIC_POWER;
778 return MATCH_YES;
780 /* Matched "*". */
781 *result = INTRINSIC_TIMES;
782 return MATCH_YES;
784 case '/':
785 ch = gfc_peek_ascii_char ();
786 if (ch == '=')
788 /* Matched "/=". */
789 gfc_next_ascii_char ();
790 *result = INTRINSIC_NE;
791 return MATCH_YES;
793 else if (ch == '/')
795 /* Matched "//". */
796 gfc_next_ascii_char ();
797 *result = INTRINSIC_CONCAT;
798 return MATCH_YES;
800 /* Matched "/". */
801 *result = INTRINSIC_DIVIDE;
802 return MATCH_YES;
804 case '.':
805 ch = gfc_next_ascii_char ();
806 switch (ch)
808 case 'a':
809 if (gfc_next_ascii_char () == 'n'
810 && gfc_next_ascii_char () == 'd'
811 && gfc_next_ascii_char () == '.')
813 /* Matched ".and.". */
814 *result = INTRINSIC_AND;
815 return MATCH_YES;
817 break;
819 case 'e':
820 if (gfc_next_ascii_char () == 'q')
822 ch = gfc_next_ascii_char ();
823 if (ch == '.')
825 /* Matched ".eq.". */
826 *result = INTRINSIC_EQ_OS;
827 return MATCH_YES;
829 else if (ch == 'v')
831 if (gfc_next_ascii_char () == '.')
833 /* Matched ".eqv.". */
834 *result = INTRINSIC_EQV;
835 return MATCH_YES;
839 break;
841 case 'g':
842 ch = gfc_next_ascii_char ();
843 if (ch == 'e')
845 if (gfc_next_ascii_char () == '.')
847 /* Matched ".ge.". */
848 *result = INTRINSIC_GE_OS;
849 return MATCH_YES;
852 else if (ch == 't')
854 if (gfc_next_ascii_char () == '.')
856 /* Matched ".gt.". */
857 *result = INTRINSIC_GT_OS;
858 return MATCH_YES;
861 break;
863 case 'l':
864 ch = gfc_next_ascii_char ();
865 if (ch == 'e')
867 if (gfc_next_ascii_char () == '.')
869 /* Matched ".le.". */
870 *result = INTRINSIC_LE_OS;
871 return MATCH_YES;
874 else if (ch == 't')
876 if (gfc_next_ascii_char () == '.')
878 /* Matched ".lt.". */
879 *result = INTRINSIC_LT_OS;
880 return MATCH_YES;
883 break;
885 case 'n':
886 ch = gfc_next_ascii_char ();
887 if (ch == 'e')
889 ch = gfc_next_ascii_char ();
890 if (ch == '.')
892 /* Matched ".ne.". */
893 *result = INTRINSIC_NE_OS;
894 return MATCH_YES;
896 else if (ch == 'q')
898 if (gfc_next_ascii_char () == 'v'
899 && gfc_next_ascii_char () == '.')
901 /* Matched ".neqv.". */
902 *result = INTRINSIC_NEQV;
903 return MATCH_YES;
907 else if (ch == 'o')
909 if (gfc_next_ascii_char () == 't'
910 && gfc_next_ascii_char () == '.')
912 /* Matched ".not.". */
913 *result = INTRINSIC_NOT;
914 return MATCH_YES;
917 break;
919 case 'o':
920 if (gfc_next_ascii_char () == 'r'
921 && gfc_next_ascii_char () == '.')
923 /* Matched ".or.". */
924 *result = INTRINSIC_OR;
925 return MATCH_YES;
927 break;
929 case 'x':
930 if (gfc_next_ascii_char () == 'o'
931 && gfc_next_ascii_char () == 'r'
932 && gfc_next_ascii_char () == '.')
934 if (!gfc_notify_std (GFC_STD_LEGACY, ".XOR. operator at %C"))
935 return MATCH_ERROR;
936 /* Matched ".xor." - equivalent to ".neqv.". */
937 *result = INTRINSIC_NEQV;
938 return MATCH_YES;
940 break;
942 default:
943 break;
945 break;
947 default:
948 break;
951 gfc_current_locus = orig_loc;
952 return MATCH_NO;
956 /* Match a loop control phrase:
958 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
960 If the final integer expression is not present, a constant unity
961 expression is returned. We don't return MATCH_ERROR until after
962 the equals sign is seen. */
964 match
965 gfc_match_iterator (gfc_iterator *iter, int init_flag)
967 char name[GFC_MAX_SYMBOL_LEN + 1];
968 gfc_expr *var, *e1, *e2, *e3;
969 locus start;
970 match m;
972 e1 = e2 = e3 = NULL;
974 /* Match the start of an iterator without affecting the symbol table. */
976 start = gfc_current_locus;
977 m = gfc_match (" %n =", name);
978 gfc_current_locus = start;
980 if (m != MATCH_YES)
981 return MATCH_NO;
983 m = gfc_match_variable (&var, 0);
984 if (m != MATCH_YES)
985 return MATCH_NO;
987 if (var->symtree->n.sym->attr.dimension)
989 gfc_error ("Loop variable at %C cannot be an array");
990 goto cleanup;
993 /* F2008, C617 & C565. */
994 if (var->symtree->n.sym->attr.codimension)
996 gfc_error ("Loop variable at %C cannot be a coarray");
997 goto cleanup;
1000 if (var->ref != NULL)
1002 gfc_error ("Loop variable at %C cannot be a sub-component");
1003 goto cleanup;
1006 gfc_match_char ('=');
1008 var->symtree->n.sym->attr.implied_index = 1;
1010 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
1011 if (m == MATCH_NO)
1012 goto syntax;
1013 if (m == MATCH_ERROR)
1014 goto cleanup;
1016 if (gfc_match_char (',') != MATCH_YES)
1017 goto syntax;
1019 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
1020 if (m == MATCH_NO)
1021 goto syntax;
1022 if (m == MATCH_ERROR)
1023 goto cleanup;
1025 if (gfc_match_char (',') != MATCH_YES)
1027 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1028 goto done;
1031 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1032 if (m == MATCH_ERROR)
1033 goto cleanup;
1034 if (m == MATCH_NO)
1036 gfc_error ("Expected a step value in iterator at %C");
1037 goto cleanup;
1040 done:
1041 iter->var = var;
1042 iter->start = e1;
1043 iter->end = e2;
1044 iter->step = e3;
1045 return MATCH_YES;
1047 syntax:
1048 gfc_error ("Syntax error in iterator at %C");
1050 cleanup:
1051 gfc_free_expr (e1);
1052 gfc_free_expr (e2);
1053 gfc_free_expr (e3);
1055 return MATCH_ERROR;
1059 /* Tries to match the next non-whitespace character on the input.
1060 This subroutine does not return MATCH_ERROR.
1061 When gobble_ws is false, do not skip over leading blanks. */
1063 match
1064 gfc_match_char (char c, bool gobble_ws)
1066 locus where;
1068 where = gfc_current_locus;
1069 if (gobble_ws)
1070 gfc_gobble_whitespace ();
1072 if (gfc_next_ascii_char () == c)
1073 return MATCH_YES;
1075 gfc_current_locus = where;
1076 return MATCH_NO;
1080 /* General purpose matching subroutine. The target string is a
1081 scanf-like format string in which spaces correspond to arbitrary
1082 whitespace (including no whitespace), characters correspond to
1083 themselves. The %-codes are:
1085 %% Literal percent sign
1086 %e Expression, pointer to a pointer is set
1087 %s Symbol, pointer to the symbol is set (host_assoc = 0)
1088 %S Symbol, pointer to the symbol is set (host_assoc = 1)
1089 %n Name, character buffer is set to name
1090 %t Matches end of statement.
1091 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1092 %l Matches a statement label
1093 %v Matches a variable expression (an lvalue, except function references
1094 having a data pointer result)
1095 % Matches a required space (in free form) and optional spaces. */
1097 match
1098 gfc_match (const char *target, ...)
1100 gfc_st_label **label;
1101 int matches, *ip;
1102 locus old_loc;
1103 va_list argp;
1104 char c, *np;
1105 match m, n;
1106 void **vp;
1107 const char *p;
1109 old_loc = gfc_current_locus;
1110 va_start (argp, target);
1111 m = MATCH_NO;
1112 matches = 0;
1113 p = target;
1115 loop:
1116 c = *p++;
1117 switch (c)
1119 case ' ':
1120 gfc_gobble_whitespace ();
1121 goto loop;
1122 case '\0':
1123 m = MATCH_YES;
1124 break;
1126 case '%':
1127 c = *p++;
1128 switch (c)
1130 case 'e':
1131 vp = va_arg (argp, void **);
1132 n = gfc_match_expr ((gfc_expr **) vp);
1133 if (n != MATCH_YES)
1135 m = n;
1136 goto not_yes;
1139 matches++;
1140 goto loop;
1142 case 'v':
1143 vp = va_arg (argp, void **);
1144 n = gfc_match_variable ((gfc_expr **) vp, 0);
1145 if (n != MATCH_YES)
1147 m = n;
1148 goto not_yes;
1151 matches++;
1152 goto loop;
1154 case 's':
1155 case 'S':
1156 vp = va_arg (argp, void **);
1157 n = gfc_match_symbol ((gfc_symbol **) vp, c == 'S');
1158 if (n != MATCH_YES)
1160 m = n;
1161 goto not_yes;
1164 matches++;
1165 goto loop;
1167 case 'n':
1168 np = va_arg (argp, char *);
1169 n = gfc_match_name (np);
1170 if (n != MATCH_YES)
1172 m = n;
1173 goto not_yes;
1176 matches++;
1177 goto loop;
1179 case 'l':
1180 label = va_arg (argp, gfc_st_label **);
1181 n = gfc_match_st_label (label);
1182 if (n != MATCH_YES)
1184 m = n;
1185 goto not_yes;
1188 matches++;
1189 goto loop;
1191 case 'o':
1192 ip = va_arg (argp, int *);
1193 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1194 if (n != MATCH_YES)
1196 m = n;
1197 goto not_yes;
1200 matches++;
1201 goto loop;
1203 case 't':
1204 if (gfc_match_eos () != MATCH_YES)
1206 m = MATCH_NO;
1207 goto not_yes;
1209 goto loop;
1211 case ' ':
1212 if (gfc_match_space () == MATCH_YES)
1213 goto loop;
1214 m = MATCH_NO;
1215 goto not_yes;
1217 case '%':
1218 break; /* Fall through to character matcher. */
1220 default:
1221 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1223 /* FALLTHRU */
1225 default:
1227 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1228 expect an upper case character here! */
1229 gcc_assert (TOLOWER (c) == c);
1231 if (c == gfc_next_ascii_char ())
1232 goto loop;
1233 break;
1236 not_yes:
1237 va_end (argp);
1239 if (m != MATCH_YES)
1241 /* Clean up after a failed match. */
1242 gfc_current_locus = old_loc;
1243 va_start (argp, target);
1245 p = target;
1246 for (; matches > 0; matches--)
1248 while (*p++ != '%');
1250 switch (*p++)
1252 case '%':
1253 matches++;
1254 break; /* Skip. */
1256 /* Matches that don't have to be undone */
1257 case 'o':
1258 case 'l':
1259 case 'n':
1260 case 's':
1261 (void) va_arg (argp, void **);
1262 break;
1264 case 'e':
1265 case 'v':
1266 vp = va_arg (argp, void **);
1267 gfc_free_expr ((struct gfc_expr *)*vp);
1268 *vp = NULL;
1269 break;
1273 va_end (argp);
1276 return m;
1280 /*********************** Statement level matching **********************/
1282 /* Matches the start of a program unit, which is the program keyword
1283 followed by an obligatory symbol. */
1285 match
1286 gfc_match_program (void)
1288 gfc_symbol *sym;
1289 match m;
1291 m = gfc_match ("% %s%t", &sym);
1293 if (m == MATCH_NO)
1295 gfc_error ("Invalid form of PROGRAM statement at %C");
1296 m = MATCH_ERROR;
1299 if (m == MATCH_ERROR)
1300 return m;
1302 if (!gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL))
1303 return MATCH_ERROR;
1305 gfc_new_block = sym;
1307 return MATCH_YES;
1311 /* Match a simple assignment statement. */
1313 match
1314 gfc_match_assignment (void)
1316 gfc_expr *lvalue, *rvalue;
1317 locus old_loc;
1318 match m;
1320 old_loc = gfc_current_locus;
1322 lvalue = NULL;
1323 m = gfc_match (" %v =", &lvalue);
1324 if (m != MATCH_YES)
1326 gfc_current_locus = old_loc;
1327 gfc_free_expr (lvalue);
1328 return MATCH_NO;
1331 rvalue = NULL;
1332 m = gfc_match (" %e%t", &rvalue);
1334 if (m == MATCH_YES
1335 && rvalue->ts.type == BT_BOZ
1336 && lvalue->ts.type == BT_CLASS)
1338 m = MATCH_ERROR;
1339 gfc_error ("BOZ literal constant at %L is neither a DATA statement "
1340 "value nor an actual argument of INT/REAL/DBLE/CMPLX "
1341 "intrinsic subprogram", &rvalue->where);
1344 if (lvalue->expr_type == EXPR_CONSTANT)
1346 /* This clobbers %len and %kind. */
1347 m = MATCH_ERROR;
1348 gfc_error ("Assignment to a constant expression at %C");
1351 if (m != MATCH_YES)
1353 gfc_current_locus = old_loc;
1354 gfc_free_expr (lvalue);
1355 gfc_free_expr (rvalue);
1356 return m;
1359 if (!lvalue->symtree)
1361 gfc_free_expr (lvalue);
1362 gfc_free_expr (rvalue);
1363 return MATCH_ERROR;
1367 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1369 new_st.op = EXEC_ASSIGN;
1370 new_st.expr1 = lvalue;
1371 new_st.expr2 = rvalue;
1373 gfc_check_do_variable (lvalue->symtree);
1375 return MATCH_YES;
1379 /* Match a pointer assignment statement. */
1381 match
1382 gfc_match_pointer_assignment (void)
1384 gfc_expr *lvalue, *rvalue;
1385 locus old_loc;
1386 match m;
1388 old_loc = gfc_current_locus;
1390 lvalue = rvalue = NULL;
1391 gfc_matching_ptr_assignment = 0;
1392 gfc_matching_procptr_assignment = 0;
1394 m = gfc_match (" %v =>", &lvalue);
1395 if (m != MATCH_YES || !lvalue->symtree)
1397 m = MATCH_NO;
1398 goto cleanup;
1401 if (lvalue->symtree->n.sym->attr.proc_pointer
1402 || gfc_is_proc_ptr_comp (lvalue))
1403 gfc_matching_procptr_assignment = 1;
1404 else
1405 gfc_matching_ptr_assignment = 1;
1407 m = gfc_match (" %e%t", &rvalue);
1408 gfc_matching_ptr_assignment = 0;
1409 gfc_matching_procptr_assignment = 0;
1410 if (m != MATCH_YES)
1411 goto cleanup;
1413 new_st.op = EXEC_POINTER_ASSIGN;
1414 new_st.expr1 = lvalue;
1415 new_st.expr2 = rvalue;
1417 return MATCH_YES;
1419 cleanup:
1420 gfc_current_locus = old_loc;
1421 gfc_free_expr (lvalue);
1422 gfc_free_expr (rvalue);
1423 return m;
1427 /* We try to match an easy arithmetic IF statement. This only happens
1428 when just after having encountered a simple IF statement. This code
1429 is really duplicate with parts of the gfc_match_if code, but this is
1430 *much* easier. */
1432 static match
1433 match_arithmetic_if (void)
1435 gfc_st_label *l1, *l2, *l3;
1436 gfc_expr *expr;
1437 match m;
1439 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1440 if (m != MATCH_YES)
1441 return m;
1443 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1444 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1445 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1447 gfc_free_expr (expr);
1448 return MATCH_ERROR;
1451 if (!gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
1452 "Arithmetic IF statement at %C"))
1453 return MATCH_ERROR;
1455 new_st.op = EXEC_ARITHMETIC_IF;
1456 new_st.expr1 = expr;
1457 new_st.label1 = l1;
1458 new_st.label2 = l2;
1459 new_st.label3 = l3;
1461 return MATCH_YES;
1465 /* The IF statement is a bit of a pain. First of all, there are three
1466 forms of it, the simple IF, the IF that starts a block and the
1467 arithmetic IF.
1469 There is a problem with the simple IF and that is the fact that we
1470 only have a single level of undo information on symbols. What this
1471 means is for a simple IF, we must re-match the whole IF statement
1472 multiple times in order to guarantee that the symbol table ends up
1473 in the proper state. */
1475 static match match_simple_forall (void);
1476 static match match_simple_where (void);
1478 match
1479 gfc_match_if (gfc_statement *if_type)
1481 gfc_expr *expr;
1482 gfc_st_label *l1, *l2, *l3;
1483 locus old_loc, old_loc2;
1484 gfc_code *p;
1485 match m, n;
1487 n = gfc_match_label ();
1488 if (n == MATCH_ERROR)
1489 return n;
1491 old_loc = gfc_current_locus;
1493 m = gfc_match (" if ", &expr);
1494 if (m != MATCH_YES)
1495 return m;
1497 if (gfc_match_char ('(') != MATCH_YES)
1499 gfc_error ("Missing %<(%> in IF-expression at %C");
1500 return MATCH_ERROR;
1503 m = gfc_match ("%e", &expr);
1504 if (m != MATCH_YES)
1505 return m;
1507 old_loc2 = gfc_current_locus;
1508 gfc_current_locus = old_loc;
1510 if (gfc_match_parens () == MATCH_ERROR)
1511 return MATCH_ERROR;
1513 gfc_current_locus = old_loc2;
1515 if (gfc_match_char (')') != MATCH_YES)
1517 gfc_error ("Syntax error in IF-expression at %C");
1518 gfc_free_expr (expr);
1519 return MATCH_ERROR;
1522 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1524 if (m == MATCH_YES)
1526 if (n == MATCH_YES)
1528 gfc_error ("Block label not appropriate for arithmetic IF "
1529 "statement at %C");
1530 gfc_free_expr (expr);
1531 return MATCH_ERROR;
1534 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1535 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1536 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1538 gfc_free_expr (expr);
1539 return MATCH_ERROR;
1542 if (!gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
1543 "Arithmetic IF statement at %C"))
1544 return MATCH_ERROR;
1546 new_st.op = EXEC_ARITHMETIC_IF;
1547 new_st.expr1 = expr;
1548 new_st.label1 = l1;
1549 new_st.label2 = l2;
1550 new_st.label3 = l3;
1552 *if_type = ST_ARITHMETIC_IF;
1553 return MATCH_YES;
1556 if (gfc_match (" then%t") == MATCH_YES)
1558 new_st.op = EXEC_IF;
1559 new_st.expr1 = expr;
1560 *if_type = ST_IF_BLOCK;
1561 return MATCH_YES;
1564 if (n == MATCH_YES)
1566 gfc_error ("Block label is not appropriate for IF statement at %C");
1567 gfc_free_expr (expr);
1568 return MATCH_ERROR;
1571 /* At this point the only thing left is a simple IF statement. At
1572 this point, n has to be MATCH_NO, so we don't have to worry about
1573 re-matching a block label. From what we've got so far, try
1574 matching an assignment. */
1576 *if_type = ST_SIMPLE_IF;
1578 m = gfc_match_assignment ();
1579 if (m == MATCH_YES)
1580 goto got_match;
1582 gfc_free_expr (expr);
1583 gfc_undo_symbols ();
1584 gfc_current_locus = old_loc;
1586 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1587 assignment was found. For MATCH_NO, continue to call the various
1588 matchers. */
1589 if (m == MATCH_ERROR)
1590 return MATCH_ERROR;
1592 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1594 m = gfc_match_pointer_assignment ();
1595 if (m == MATCH_YES)
1596 goto got_match;
1598 gfc_free_expr (expr);
1599 gfc_undo_symbols ();
1600 gfc_current_locus = old_loc;
1602 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1604 /* Look at the next keyword to see which matcher to call. Matching
1605 the keyword doesn't affect the symbol table, so we don't have to
1606 restore between tries. */
1608 #define match(string, subr, statement) \
1609 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1611 gfc_clear_error ();
1613 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1614 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1615 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1616 match ("call", gfc_match_call, ST_CALL)
1617 match ("change% team", gfc_match_change_team, ST_CHANGE_TEAM)
1618 match ("close", gfc_match_close, ST_CLOSE)
1619 match ("continue", gfc_match_continue, ST_CONTINUE)
1620 match ("cycle", gfc_match_cycle, ST_CYCLE)
1621 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1622 match ("end file", gfc_match_endfile, ST_END_FILE)
1623 match ("end team", gfc_match_end_team, ST_END_TEAM)
1624 match ("error% stop", gfc_match_error_stop, ST_ERROR_STOP)
1625 match ("event% post", gfc_match_event_post, ST_EVENT_POST)
1626 match ("event% wait", gfc_match_event_wait, ST_EVENT_WAIT)
1627 match ("exit", gfc_match_exit, ST_EXIT)
1628 match ("fail% image", gfc_match_fail_image, ST_FAIL_IMAGE)
1629 match ("flush", gfc_match_flush, ST_FLUSH)
1630 match ("forall", match_simple_forall, ST_FORALL)
1631 match ("form% team", gfc_match_form_team, ST_FORM_TEAM)
1632 match ("go to", gfc_match_goto, ST_GOTO)
1633 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1634 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1635 match ("lock", gfc_match_lock, ST_LOCK)
1636 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1637 match ("open", gfc_match_open, ST_OPEN)
1638 match ("pause", gfc_match_pause, ST_NONE)
1639 match ("print", gfc_match_print, ST_WRITE)
1640 match ("read", gfc_match_read, ST_READ)
1641 match ("return", gfc_match_return, ST_RETURN)
1642 match ("rewind", gfc_match_rewind, ST_REWIND)
1643 match ("stop", gfc_match_stop, ST_STOP)
1644 match ("wait", gfc_match_wait, ST_WAIT)
1645 match ("sync% all", gfc_match_sync_all, ST_SYNC_CALL);
1646 match ("sync% images", gfc_match_sync_images, ST_SYNC_IMAGES);
1647 match ("sync% memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1648 match ("sync% team", gfc_match_sync_team, ST_SYNC_TEAM)
1649 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1650 match ("where", match_simple_where, ST_WHERE)
1651 match ("write", gfc_match_write, ST_WRITE)
1653 if (flag_dec)
1654 match ("type", gfc_match_print, ST_WRITE)
1656 /* All else has failed, so give up. See if any of the matchers has
1657 stored an error message of some sort. */
1658 if (!gfc_error_check ())
1659 gfc_error ("Syntax error in IF-clause after %C");
1661 gfc_free_expr (expr);
1662 return MATCH_ERROR;
1664 got_match:
1665 if (m == MATCH_NO)
1666 gfc_error ("Syntax error in IF-clause after %C");
1667 if (m != MATCH_YES)
1669 gfc_free_expr (expr);
1670 return MATCH_ERROR;
1673 /* At this point, we've matched the single IF and the action clause
1674 is in new_st. Rearrange things so that the IF statement appears
1675 in new_st. */
1677 p = gfc_get_code (EXEC_IF);
1678 p->next = XCNEW (gfc_code);
1679 *p->next = new_st;
1680 p->next->loc = gfc_current_locus;
1682 p->expr1 = expr;
1684 gfc_clear_new_st ();
1686 new_st.op = EXEC_IF;
1687 new_st.block = p;
1689 return MATCH_YES;
1692 #undef match
1695 /* Match an ELSE statement. */
1697 match
1698 gfc_match_else (void)
1700 char name[GFC_MAX_SYMBOL_LEN + 1];
1702 if (gfc_match_eos () == MATCH_YES)
1703 return MATCH_YES;
1705 if (gfc_match_name (name) != MATCH_YES
1706 || gfc_current_block () == NULL
1707 || gfc_match_eos () != MATCH_YES)
1709 gfc_error ("Invalid character(s) in ELSE statement after %C");
1710 return MATCH_ERROR;
1713 if (strcmp (name, gfc_current_block ()->name) != 0)
1715 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1716 name, gfc_current_block ()->name);
1717 return MATCH_ERROR;
1720 return MATCH_YES;
1724 /* Match an ELSE IF statement. */
1726 match
1727 gfc_match_elseif (void)
1729 char name[GFC_MAX_SYMBOL_LEN + 1];
1730 gfc_expr *expr, *then;
1731 locus where;
1732 match m;
1734 if (gfc_match_char ('(') != MATCH_YES)
1736 gfc_error ("Missing %<(%> in ELSE IF expression at %C");
1737 return MATCH_ERROR;
1740 m = gfc_match (" %e ", &expr);
1741 if (m != MATCH_YES)
1742 return m;
1744 if (gfc_match_char (')') != MATCH_YES)
1746 gfc_error ("Missing %<)%> in ELSE IF expression at %C");
1747 goto cleanup;
1750 m = gfc_match (" then ", &then);
1752 where = gfc_current_locus;
1754 if (m == MATCH_YES && (gfc_match_eos () == MATCH_YES
1755 || (gfc_current_block ()
1756 && gfc_match_name (name) == MATCH_YES)))
1757 goto done;
1759 if (gfc_match_eos () == MATCH_YES)
1761 gfc_error ("Missing THEN in ELSE IF statement after %L", &where);
1762 goto cleanup;
1765 if (gfc_match_name (name) != MATCH_YES
1766 || gfc_current_block () == NULL
1767 || gfc_match_eos () != MATCH_YES)
1769 gfc_error ("Syntax error in ELSE IF statement after %L", &where);
1770 goto cleanup;
1773 if (strcmp (name, gfc_current_block ()->name) != 0)
1775 gfc_error ("Label %qs after %L doesn't match IF label %qs",
1776 name, &where, gfc_current_block ()->name);
1777 goto cleanup;
1780 if (m != MATCH_YES)
1781 return m;
1783 done:
1784 new_st.op = EXEC_IF;
1785 new_st.expr1 = expr;
1786 return MATCH_YES;
1788 cleanup:
1789 gfc_free_expr (expr);
1790 return MATCH_ERROR;
1794 /* Free a gfc_iterator structure. */
1796 void
1797 gfc_free_iterator (gfc_iterator *iter, int flag)
1800 if (iter == NULL)
1801 return;
1803 gfc_free_expr (iter->var);
1804 gfc_free_expr (iter->start);
1805 gfc_free_expr (iter->end);
1806 gfc_free_expr (iter->step);
1808 if (flag)
1809 free (iter);
1813 /* Match a CRITICAL statement. */
1814 match
1815 gfc_match_critical (void)
1817 gfc_st_label *label = NULL;
1819 if (gfc_match_label () == MATCH_ERROR)
1820 return MATCH_ERROR;
1822 if (gfc_match (" critical") != MATCH_YES)
1823 return MATCH_NO;
1825 if (gfc_match_st_label (&label) == MATCH_ERROR)
1826 return MATCH_ERROR;
1828 if (gfc_match_eos () != MATCH_YES)
1830 gfc_syntax_error (ST_CRITICAL);
1831 return MATCH_ERROR;
1834 if (gfc_pure (NULL))
1836 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1837 return MATCH_ERROR;
1840 if (gfc_find_state (COMP_DO_CONCURRENT))
1842 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1843 "block");
1844 return MATCH_ERROR;
1847 gfc_unset_implicit_pure (NULL);
1849 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
1850 return MATCH_ERROR;
1852 if (flag_coarray == GFC_FCOARRAY_NONE)
1854 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1855 "enable");
1856 return MATCH_ERROR;
1859 if (gfc_find_state (COMP_CRITICAL))
1861 gfc_error ("Nested CRITICAL block at %C");
1862 return MATCH_ERROR;
1865 new_st.op = EXEC_CRITICAL;
1867 if (label != NULL
1868 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
1869 return MATCH_ERROR;
1871 return MATCH_YES;
1875 /* Match a BLOCK statement. */
1877 match
1878 gfc_match_block (void)
1880 match m;
1882 if (gfc_match_label () == MATCH_ERROR)
1883 return MATCH_ERROR;
1885 if (gfc_match (" block") != MATCH_YES)
1886 return MATCH_NO;
1888 /* For this to be a correct BLOCK statement, the line must end now. */
1889 m = gfc_match_eos ();
1890 if (m == MATCH_ERROR)
1891 return MATCH_ERROR;
1892 if (m == MATCH_NO)
1893 return MATCH_NO;
1895 return MATCH_YES;
1899 /* Match an ASSOCIATE statement. */
1901 match
1902 gfc_match_associate (void)
1904 if (gfc_match_label () == MATCH_ERROR)
1905 return MATCH_ERROR;
1907 if (gfc_match (" associate") != MATCH_YES)
1908 return MATCH_NO;
1910 /* Match the association list. */
1911 if (gfc_match_char ('(') != MATCH_YES)
1913 gfc_error ("Expected association list at %C");
1914 return MATCH_ERROR;
1916 new_st.ext.block.assoc = NULL;
1917 while (true)
1919 gfc_association_list* newAssoc = gfc_get_association_list ();
1920 gfc_association_list* a;
1922 /* Match the next association. */
1923 if (gfc_match (" %n =>", newAssoc->name) != MATCH_YES)
1925 gfc_error ("Expected association at %C");
1926 goto assocListError;
1929 if (gfc_match (" %e", &newAssoc->target) != MATCH_YES)
1931 /* Have another go, allowing for procedure pointer selectors. */
1932 gfc_matching_procptr_assignment = 1;
1933 if (gfc_match (" %e", &newAssoc->target) != MATCH_YES)
1935 gfc_matching_procptr_assignment = 0;
1936 gfc_error ("Invalid association target at %C");
1937 goto assocListError;
1939 gfc_matching_procptr_assignment = 0;
1941 newAssoc->where = gfc_current_locus;
1943 /* Check that the current name is not yet in the list. */
1944 for (a = new_st.ext.block.assoc; a; a = a->next)
1945 if (!strcmp (a->name, newAssoc->name))
1947 gfc_error ("Duplicate name %qs in association at %C",
1948 newAssoc->name);
1949 goto assocListError;
1952 /* The target expression must not be coindexed. */
1953 if (gfc_is_coindexed (newAssoc->target))
1955 gfc_error ("Association target at %C must not be coindexed");
1956 goto assocListError;
1959 /* The target expression cannot be a BOZ literal constant. */
1960 if (newAssoc->target->ts.type == BT_BOZ)
1962 gfc_error ("Association target at %L cannot be a BOZ literal "
1963 "constant", &newAssoc->target->where);
1964 goto assocListError;
1967 /* The `variable' field is left blank for now; because the target is not
1968 yet resolved, we can't use gfc_has_vector_subscript to determine it
1969 for now. This is set during resolution. */
1971 /* Put it into the list. */
1972 newAssoc->next = new_st.ext.block.assoc;
1973 new_st.ext.block.assoc = newAssoc;
1975 /* Try next one or end if closing parenthesis is found. */
1976 gfc_gobble_whitespace ();
1977 if (gfc_peek_char () == ')')
1978 break;
1979 if (gfc_match_char (',') != MATCH_YES)
1981 gfc_error ("Expected %<)%> or %<,%> at %C");
1982 return MATCH_ERROR;
1985 continue;
1987 assocListError:
1988 free (newAssoc);
1989 goto error;
1991 if (gfc_match_char (')') != MATCH_YES)
1993 /* This should never happen as we peek above. */
1994 gcc_unreachable ();
1997 if (gfc_match_eos () != MATCH_YES)
1999 gfc_error ("Junk after ASSOCIATE statement at %C");
2000 goto error;
2003 return MATCH_YES;
2005 error:
2006 gfc_free_association_list (new_st.ext.block.assoc);
2007 return MATCH_ERROR;
2011 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
2012 an accessible derived type. */
2014 static match
2015 match_derived_type_spec (gfc_typespec *ts)
2017 char name[GFC_MAX_SYMBOL_LEN + 1];
2018 locus old_locus;
2019 gfc_symbol *derived, *der_type;
2020 match m = MATCH_YES;
2021 gfc_actual_arglist *decl_type_param_list = NULL;
2022 bool is_pdt_template = false;
2024 old_locus = gfc_current_locus;
2026 if (gfc_match ("%n", name) != MATCH_YES)
2028 gfc_current_locus = old_locus;
2029 return MATCH_NO;
2032 gfc_find_symbol (name, NULL, 1, &derived);
2034 /* Match the PDT spec list, if there. */
2035 if (derived && derived->attr.flavor == FL_PROCEDURE)
2037 gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &der_type);
2038 is_pdt_template = der_type
2039 && der_type->attr.flavor == FL_DERIVED
2040 && der_type->attr.pdt_template;
2043 if (is_pdt_template)
2044 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
2046 if (m == MATCH_ERROR)
2048 gfc_free_actual_arglist (decl_type_param_list);
2049 return m;
2052 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
2053 derived = gfc_find_dt_in_generic (derived);
2055 /* If this is a PDT, find the specific instance. */
2056 if (m == MATCH_YES && is_pdt_template)
2058 gfc_namespace *old_ns;
2060 old_ns = gfc_current_ns;
2061 while (gfc_current_ns && gfc_current_ns->parent)
2062 gfc_current_ns = gfc_current_ns->parent;
2064 if (type_param_spec_list)
2065 gfc_free_actual_arglist (type_param_spec_list);
2066 m = gfc_get_pdt_instance (decl_type_param_list, &der_type,
2067 &type_param_spec_list);
2068 gfc_free_actual_arglist (decl_type_param_list);
2070 if (m != MATCH_YES)
2071 return m;
2072 derived = der_type;
2073 gcc_assert (!derived->attr.pdt_template && derived->attr.pdt_type);
2074 gfc_set_sym_referenced (derived);
2076 gfc_current_ns = old_ns;
2079 if (derived && derived->attr.flavor == FL_DERIVED)
2081 ts->type = BT_DERIVED;
2082 ts->u.derived = derived;
2083 return MATCH_YES;
2086 gfc_current_locus = old_locus;
2087 return MATCH_NO;
2091 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2092 gfc_match_decl_type_spec() from decl.cc, with the following exceptions:
2093 It only includes the intrinsic types from the Fortran 2003 standard
2094 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2095 the implicit_flag is not needed, so it was removed. Derived types are
2096 identified by their name alone. */
2098 match
2099 gfc_match_type_spec (gfc_typespec *ts)
2101 match m;
2102 locus old_locus;
2103 char c, name[GFC_MAX_SYMBOL_LEN + 1];
2105 gfc_clear_ts (ts);
2106 gfc_gobble_whitespace ();
2107 old_locus = gfc_current_locus;
2109 /* If c isn't [a-z], then return immediately. */
2110 c = gfc_peek_ascii_char ();
2111 if (!ISALPHA(c))
2112 return MATCH_NO;
2114 type_param_spec_list = NULL;
2116 if (match_derived_type_spec (ts) == MATCH_YES)
2118 /* Enforce F03:C401. */
2119 if (ts->u.derived->attr.abstract)
2121 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2122 ts->u.derived->name, &old_locus);
2123 return MATCH_ERROR;
2125 return MATCH_YES;
2128 if (gfc_match ("integer") == MATCH_YES)
2130 ts->type = BT_INTEGER;
2131 ts->kind = gfc_default_integer_kind;
2132 goto kind_selector;
2135 if (flag_unsigned && gfc_match ("unsigned") == MATCH_YES)
2137 ts->type = BT_UNSIGNED;
2138 ts->kind = gfc_default_integer_kind;
2139 goto kind_selector;
2142 if (gfc_match ("double precision") == MATCH_YES)
2144 ts->type = BT_REAL;
2145 ts->kind = gfc_default_double_kind;
2146 return MATCH_YES;
2149 if (gfc_match ("complex") == MATCH_YES)
2151 ts->type = BT_COMPLEX;
2152 ts->kind = gfc_default_complex_kind;
2153 goto kind_selector;
2156 if (gfc_match ("character") == MATCH_YES)
2158 ts->type = BT_CHARACTER;
2160 m = gfc_match_char_spec (ts);
2162 if (m == MATCH_NO)
2163 m = MATCH_YES;
2165 return m;
2168 /* REAL is a real pain because it can be a type, intrinsic subprogram,
2169 or list item in a type-list of an OpenMP reduction clause. Need to
2170 differentiate REAL([KIND]=scalar-int-initialization-expr) from
2171 REAL(A,[KIND]) and REAL(KIND,A). Logically, when this code was
2172 written the use of LOGICAL as a type-spec or intrinsic subprogram
2173 was overlooked. */
2175 m = gfc_match (" %n", name);
2176 if (m == MATCH_YES
2177 && (strcmp (name, "real") == 0 || strcmp (name, "logical") == 0))
2179 char c;
2180 gfc_expr *e;
2181 locus where;
2183 if (*name == 'r')
2185 ts->type = BT_REAL;
2186 ts->kind = gfc_default_real_kind;
2188 else
2190 ts->type = BT_LOGICAL;
2191 ts->kind = gfc_default_logical_kind;
2194 gfc_gobble_whitespace ();
2196 /* Prevent REAL*4, etc. */
2197 c = gfc_peek_ascii_char ();
2198 if (c == '*')
2200 gfc_error ("Invalid type-spec at %C");
2201 return MATCH_ERROR;
2204 /* Found leading colon in REAL::, a trailing ')' in for example
2205 TYPE IS (REAL), or REAL, for an OpenMP list-item. */
2206 if (c == ':' || c == ')' || (flag_openmp && c == ','))
2207 return MATCH_YES;
2209 /* Found something other than the opening '(' in REAL(... */
2210 if (c != '(')
2211 return MATCH_NO;
2212 else
2213 gfc_next_char (); /* Burn the '('. */
2215 /* Look for the optional KIND=. */
2216 where = gfc_current_locus;
2217 m = gfc_match ("%n", name);
2218 if (m == MATCH_YES)
2220 gfc_gobble_whitespace ();
2221 c = gfc_next_char ();
2222 if (c == '=')
2224 if (strcmp(name, "a") == 0 || strcmp(name, "l") == 0)
2225 return MATCH_NO;
2226 else if (strcmp(name, "kind") == 0)
2227 goto found;
2228 else
2229 return MATCH_ERROR;
2231 else
2232 gfc_current_locus = where;
2234 else
2235 gfc_current_locus = where;
2237 found:
2239 m = gfc_match_expr (&e);
2240 if (m == MATCH_NO || m == MATCH_ERROR)
2241 return m;
2243 /* If a comma appears, it is an intrinsic subprogram. */
2244 gfc_gobble_whitespace ();
2245 c = gfc_peek_ascii_char ();
2246 if (c == ',')
2248 gfc_free_expr (e);
2249 return MATCH_NO;
2252 /* If ')' appears, we have REAL(initialization-expr), here check for
2253 a scalar integer initialization-expr and valid kind parameter. */
2254 if (c == ')')
2256 bool ok = true;
2257 if (e->expr_type != EXPR_CONSTANT && e->expr_type != EXPR_VARIABLE)
2258 ok = gfc_reduce_init_expr (e);
2259 if (!ok || e->ts.type != BT_INTEGER || e->rank > 0)
2261 gfc_free_expr (e);
2262 return MATCH_NO;
2265 if (e->expr_type != EXPR_CONSTANT)
2266 goto ohno;
2268 gfc_next_char (); /* Burn the ')'. */
2269 ts->kind = (int) mpz_get_si (e->value.integer);
2270 if (gfc_validate_kind (ts->type, ts->kind , true) == -1)
2272 gfc_error ("Invalid type-spec at %C");
2273 return MATCH_ERROR;
2276 gfc_free_expr (e);
2278 return MATCH_YES;
2282 ohno:
2284 /* If a type is not matched, simply return MATCH_NO. */
2285 gfc_current_locus = old_locus;
2286 return MATCH_NO;
2288 kind_selector:
2290 gfc_gobble_whitespace ();
2292 /* This prevents INTEGER*4, etc. */
2293 if (gfc_peek_ascii_char () == '*')
2295 gfc_error ("Invalid type-spec at %C");
2296 return MATCH_ERROR;
2299 m = gfc_match_kind_spec (ts, false);
2301 /* No kind specifier found. */
2302 if (m == MATCH_NO)
2303 m = MATCH_YES;
2305 return m;
2309 /******************** FORALL subroutines ********************/
2311 /* Free a list of FORALL iterators. */
2313 void
2314 gfc_free_forall_iterator (gfc_forall_iterator *iter)
2316 gfc_forall_iterator *next;
2318 while (iter)
2320 next = iter->next;
2321 gfc_free_expr (iter->var);
2322 gfc_free_expr (iter->start);
2323 gfc_free_expr (iter->end);
2324 gfc_free_expr (iter->stride);
2325 free (iter);
2326 iter = next;
2331 /* Match an iterator as part of a FORALL statement. The format is:
2333 <var> = <start>:<end>[:<stride>]
2335 On MATCH_NO, the caller tests for the possibility that there is a
2336 scalar mask expression. */
2338 static match
2339 match_forall_iterator (gfc_forall_iterator **result)
2341 gfc_forall_iterator *iter;
2342 locus where;
2343 match m;
2345 where = gfc_current_locus;
2346 iter = XCNEW (gfc_forall_iterator);
2348 m = gfc_match_expr (&iter->var);
2349 if (m != MATCH_YES)
2350 goto cleanup;
2352 if (gfc_match_char ('=') != MATCH_YES
2353 || iter->var->expr_type != EXPR_VARIABLE)
2355 m = MATCH_NO;
2356 goto cleanup;
2359 m = gfc_match_expr (&iter->start);
2360 if (m != MATCH_YES)
2361 goto cleanup;
2363 if (gfc_match_char (':') != MATCH_YES)
2364 goto syntax;
2366 m = gfc_match_expr (&iter->end);
2367 if (m == MATCH_NO)
2368 goto syntax;
2369 if (m == MATCH_ERROR)
2370 goto cleanup;
2372 if (gfc_match_char (':') == MATCH_NO)
2373 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2374 else
2376 m = gfc_match_expr (&iter->stride);
2377 if (m == MATCH_NO)
2378 goto syntax;
2379 if (m == MATCH_ERROR)
2380 goto cleanup;
2383 /* Mark the iteration variable's symbol as used as a FORALL index. */
2384 iter->var->symtree->n.sym->forall_index = true;
2386 *result = iter;
2387 return MATCH_YES;
2389 syntax:
2390 gfc_error ("Syntax error in FORALL iterator at %C");
2391 m = MATCH_ERROR;
2393 cleanup:
2395 gfc_current_locus = where;
2396 gfc_free_forall_iterator (iter);
2397 return m;
2401 /* Match the header of a FORALL statement. */
2403 static match
2404 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2406 gfc_forall_iterator *head, *tail, *new_iter;
2407 gfc_expr *msk;
2408 match m;
2410 gfc_gobble_whitespace ();
2412 head = tail = NULL;
2413 msk = NULL;
2415 if (gfc_match_char ('(') != MATCH_YES)
2416 return MATCH_NO;
2418 m = match_forall_iterator (&new_iter);
2419 if (m == MATCH_ERROR)
2420 goto cleanup;
2421 if (m == MATCH_NO)
2422 goto syntax;
2424 head = tail = new_iter;
2426 for (;;)
2428 if (gfc_match_char (',') != MATCH_YES)
2429 break;
2431 m = match_forall_iterator (&new_iter);
2432 if (m == MATCH_ERROR)
2433 goto cleanup;
2435 if (m == MATCH_YES)
2437 tail->next = new_iter;
2438 tail = new_iter;
2439 continue;
2442 /* Have to have a mask expression. */
2444 m = gfc_match_expr (&msk);
2445 if (m == MATCH_NO)
2446 goto syntax;
2447 if (m == MATCH_ERROR)
2448 goto cleanup;
2450 break;
2453 if (gfc_match_char (')') == MATCH_NO)
2454 goto syntax;
2456 *phead = head;
2457 *mask = msk;
2458 return MATCH_YES;
2460 syntax:
2461 gfc_syntax_error (ST_FORALL);
2463 cleanup:
2464 gfc_free_expr (msk);
2465 gfc_free_forall_iterator (head);
2467 return MATCH_ERROR;
2470 /* Match the rest of a simple FORALL statement that follows an
2471 IF statement. */
2473 static match
2474 match_simple_forall (void)
2476 gfc_forall_iterator *head;
2477 gfc_expr *mask;
2478 gfc_code *c;
2479 match m;
2481 mask = NULL;
2482 head = NULL;
2483 c = NULL;
2485 m = match_forall_header (&head, &mask);
2487 if (m == MATCH_NO)
2488 goto syntax;
2489 if (m != MATCH_YES)
2490 goto cleanup;
2492 m = gfc_match_assignment ();
2494 if (m == MATCH_ERROR)
2495 goto cleanup;
2496 if (m == MATCH_NO)
2498 m = gfc_match_pointer_assignment ();
2499 if (m == MATCH_ERROR)
2500 goto cleanup;
2501 if (m == MATCH_NO)
2502 goto syntax;
2505 c = XCNEW (gfc_code);
2506 *c = new_st;
2507 c->loc = gfc_current_locus;
2509 if (gfc_match_eos () != MATCH_YES)
2510 goto syntax;
2512 gfc_clear_new_st ();
2513 new_st.op = EXEC_FORALL;
2514 new_st.expr1 = mask;
2515 new_st.ext.forall_iterator = head;
2516 new_st.block = gfc_get_code (EXEC_FORALL);
2517 new_st.block->next = c;
2519 return MATCH_YES;
2521 syntax:
2522 gfc_syntax_error (ST_FORALL);
2524 cleanup:
2525 gfc_free_forall_iterator (head);
2526 gfc_free_expr (mask);
2528 return MATCH_ERROR;
2532 /* Match a FORALL statement. */
2534 match
2535 gfc_match_forall (gfc_statement *st)
2537 gfc_forall_iterator *head;
2538 gfc_expr *mask;
2539 gfc_code *c;
2540 match m0, m;
2542 head = NULL;
2543 mask = NULL;
2544 c = NULL;
2546 m0 = gfc_match_label ();
2547 if (m0 == MATCH_ERROR)
2548 return MATCH_ERROR;
2550 m = gfc_match (" forall");
2551 if (m != MATCH_YES)
2552 return m;
2554 m = match_forall_header (&head, &mask);
2555 if (m == MATCH_ERROR)
2556 goto cleanup;
2557 if (m == MATCH_NO)
2558 goto syntax;
2560 if (gfc_match_eos () == MATCH_YES)
2562 *st = ST_FORALL_BLOCK;
2563 new_st.op = EXEC_FORALL;
2564 new_st.expr1 = mask;
2565 new_st.ext.forall_iterator = head;
2566 return MATCH_YES;
2569 m = gfc_match_assignment ();
2570 if (m == MATCH_ERROR)
2571 goto cleanup;
2572 if (m == MATCH_NO)
2574 m = gfc_match_pointer_assignment ();
2575 if (m == MATCH_ERROR)
2576 goto cleanup;
2577 if (m == MATCH_NO)
2578 goto syntax;
2581 c = XCNEW (gfc_code);
2582 *c = new_st;
2583 c->loc = gfc_current_locus;
2585 gfc_clear_new_st ();
2586 new_st.op = EXEC_FORALL;
2587 new_st.expr1 = mask;
2588 new_st.ext.forall_iterator = head;
2589 new_st.block = gfc_get_code (EXEC_FORALL);
2590 new_st.block->next = c;
2592 *st = ST_FORALL;
2593 return MATCH_YES;
2595 syntax:
2596 gfc_syntax_error (ST_FORALL);
2598 cleanup:
2599 gfc_free_forall_iterator (head);
2600 gfc_free_expr (mask);
2601 gfc_free_statements (c);
2602 return MATCH_NO;
2606 /* Match a DO statement. */
2608 match
2609 gfc_match_do (void)
2611 gfc_iterator iter, *ip;
2612 locus old_loc;
2613 gfc_st_label *label;
2614 match m;
2616 old_loc = gfc_current_locus;
2618 memset (&iter, '\0', sizeof (gfc_iterator));
2619 label = NULL;
2621 m = gfc_match_label ();
2622 if (m == MATCH_ERROR)
2623 return m;
2625 if (gfc_match (" do") != MATCH_YES)
2626 return MATCH_NO;
2628 m = gfc_match_st_label (&label);
2629 if (m == MATCH_ERROR)
2630 goto cleanup;
2632 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2634 if (gfc_match_eos () == MATCH_YES)
2636 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2637 new_st.op = EXEC_DO_WHILE;
2638 goto done;
2641 /* Match an optional comma, if no comma is found, a space is obligatory. */
2642 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2643 return MATCH_NO;
2645 /* Check for balanced parens. */
2647 if (gfc_match_parens () == MATCH_ERROR)
2648 return MATCH_ERROR;
2650 if (gfc_match (" concurrent") == MATCH_YES)
2652 gfc_forall_iterator *head;
2653 gfc_expr *mask;
2655 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
2656 return MATCH_ERROR;
2659 mask = NULL;
2660 head = NULL;
2661 m = match_forall_header (&head, &mask);
2663 if (m == MATCH_NO)
2664 return m;
2665 if (m == MATCH_ERROR)
2666 goto concurr_cleanup;
2668 if (gfc_match_eos () != MATCH_YES)
2669 goto concurr_cleanup;
2671 if (label != NULL
2672 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2673 goto concurr_cleanup;
2675 new_st.label1 = label;
2676 new_st.op = EXEC_DO_CONCURRENT;
2677 new_st.expr1 = mask;
2678 new_st.ext.forall_iterator = head;
2680 return MATCH_YES;
2682 concurr_cleanup:
2683 gfc_syntax_error (ST_DO);
2684 gfc_free_expr (mask);
2685 gfc_free_forall_iterator (head);
2686 return MATCH_ERROR;
2689 /* See if we have a DO WHILE. */
2690 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2692 new_st.op = EXEC_DO_WHILE;
2693 goto done;
2696 /* The abortive DO WHILE may have done something to the symbol
2697 table, so we start over. */
2698 gfc_undo_symbols ();
2699 gfc_current_locus = old_loc;
2701 gfc_match_label (); /* This won't error. */
2702 gfc_match (" do "); /* This will work. */
2704 gfc_match_st_label (&label); /* Can't error out. */
2705 gfc_match_char (','); /* Optional comma. */
2707 m = gfc_match_iterator (&iter, 0);
2708 if (m == MATCH_NO)
2709 return MATCH_NO;
2710 if (m == MATCH_ERROR)
2711 goto cleanup;
2713 iter.var->symtree->n.sym->attr.implied_index = 0;
2714 gfc_check_do_variable (iter.var->symtree);
2716 if (gfc_match_eos () != MATCH_YES)
2718 gfc_syntax_error (ST_DO);
2719 goto cleanup;
2722 new_st.op = EXEC_DO;
2724 done:
2725 if (label != NULL
2726 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2727 goto cleanup;
2729 new_st.label1 = label;
2731 if (new_st.op == EXEC_DO_WHILE)
2732 new_st.expr1 = iter.end;
2733 else
2735 new_st.ext.iterator = ip = gfc_get_iterator ();
2736 *ip = iter;
2739 return MATCH_YES;
2741 cleanup:
2742 gfc_free_iterator (&iter, 0);
2744 return MATCH_ERROR;
2748 /* Match an EXIT or CYCLE statement. */
2750 static match
2751 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2753 gfc_state_data *p, *o;
2754 gfc_symbol *sym;
2755 match m;
2756 int cnt;
2758 if (gfc_match_eos () == MATCH_YES)
2759 sym = NULL;
2760 else
2762 char name[GFC_MAX_SYMBOL_LEN + 1];
2763 gfc_symtree* stree;
2765 m = gfc_match ("% %n%t", name);
2766 if (m == MATCH_ERROR)
2767 return MATCH_ERROR;
2768 if (m == MATCH_NO)
2770 gfc_syntax_error (st);
2771 return MATCH_ERROR;
2774 /* Find the corresponding symbol. If there's a BLOCK statement
2775 between here and the label, it is not in gfc_current_ns but a parent
2776 namespace! */
2777 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2778 if (!stree)
2780 gfc_error ("Name %qs in %s statement at %C is unknown",
2781 name, gfc_ascii_statement (st));
2782 return MATCH_ERROR;
2785 sym = stree->n.sym;
2786 if (sym->attr.flavor != FL_LABEL)
2788 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2789 name, gfc_ascii_statement (st));
2790 return MATCH_ERROR;
2794 /* Find the loop specified by the label (or lack of a label). */
2795 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2796 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2797 o = p;
2798 else if (p->state == COMP_CRITICAL)
2800 gfc_error("%s statement at %C leaves CRITICAL construct",
2801 gfc_ascii_statement (st));
2802 return MATCH_ERROR;
2804 else if (p->state == COMP_DO_CONCURRENT
2805 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2807 /* F2008, C821 & C845. */
2808 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2809 gfc_ascii_statement (st));
2810 return MATCH_ERROR;
2812 else if ((sym && sym == p->sym)
2813 || (!sym && (p->state == COMP_DO
2814 || p->state == COMP_DO_CONCURRENT)))
2815 break;
2817 if (p == NULL)
2819 if (sym == NULL)
2820 gfc_error ("%s statement at %C is not within a construct",
2821 gfc_ascii_statement (st));
2822 else
2823 gfc_error ("%s statement at %C is not within construct %qs",
2824 gfc_ascii_statement (st), sym->name);
2826 return MATCH_ERROR;
2829 /* Special checks for EXIT from non-loop constructs. */
2830 switch (p->state)
2832 case COMP_DO:
2833 case COMP_DO_CONCURRENT:
2834 break;
2836 case COMP_CRITICAL:
2837 /* This is already handled above. */
2838 gcc_unreachable ();
2840 case COMP_ASSOCIATE:
2841 case COMP_BLOCK:
2842 case COMP_IF:
2843 case COMP_SELECT:
2844 case COMP_SELECT_TYPE:
2845 case COMP_SELECT_RANK:
2846 gcc_assert (sym);
2847 if (op == EXEC_CYCLE)
2849 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2850 " construct %qs", sym->name);
2851 return MATCH_ERROR;
2853 gcc_assert (op == EXEC_EXIT);
2854 if (!gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2855 " do-construct-name at %C"))
2856 return MATCH_ERROR;
2857 break;
2859 default:
2860 gfc_error ("%s statement at %C is not applicable to construct %qs",
2861 gfc_ascii_statement (st), sym->name);
2862 return MATCH_ERROR;
2865 if (o != NULL)
2867 gfc_error (is_oacc (p)
2868 ? G_("%s statement at %C leaving OpenACC structured block")
2869 : G_("%s statement at %C leaving OpenMP structured block"),
2870 gfc_ascii_statement (st));
2871 return MATCH_ERROR;
2874 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2875 o = o->previous;
2877 int count = 1;
2878 if (cnt > 0
2879 && o != NULL
2880 && o->state == COMP_OMP_STRUCTURED_BLOCK)
2881 switch (o->head->op)
2883 case EXEC_OACC_LOOP:
2884 case EXEC_OACC_KERNELS_LOOP:
2885 case EXEC_OACC_PARALLEL_LOOP:
2886 case EXEC_OACC_SERIAL_LOOP:
2887 gcc_assert (o->head->next != NULL
2888 && (o->head->next->op == EXEC_DO
2889 || o->head->next->op == EXEC_DO_WHILE)
2890 && o->previous != NULL
2891 && o->previous->tail->op == o->head->op);
2892 if (o->previous->tail->ext.omp_clauses != NULL)
2894 /* Both collapsed and tiled loops are lowered the same way, but are
2895 not compatible. In gfc_trans_omp_do, the tile is prioritized. */
2896 if (o->previous->tail->ext.omp_clauses->tile_list)
2898 count = 0;
2899 gfc_expr_list *el
2900 = o->previous->tail->ext.omp_clauses->tile_list;
2901 for ( ; el; el = el->next)
2902 ++count;
2904 else if (o->previous->tail->ext.omp_clauses->collapse > 1)
2905 count = o->previous->tail->ext.omp_clauses->collapse;
2907 if (st == ST_EXIT && cnt <= count)
2909 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2910 return MATCH_ERROR;
2912 if (st == ST_CYCLE && cnt < count)
2914 gfc_error (o->previous->tail->ext.omp_clauses->tile_list
2915 ? G_("CYCLE statement at %C to non-innermost tiled "
2916 "!$ACC LOOP loop")
2917 : G_("CYCLE statement at %C to non-innermost collapsed "
2918 "!$ACC LOOP loop"));
2919 return MATCH_ERROR;
2921 break;
2922 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2923 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
2924 case EXEC_OMP_TARGET_SIMD:
2925 case EXEC_OMP_TASKLOOP_SIMD:
2926 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
2927 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
2928 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
2929 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
2930 case EXEC_OMP_PARALLEL_DO_SIMD:
2931 case EXEC_OMP_DISTRIBUTE_SIMD:
2932 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
2933 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
2934 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2935 case EXEC_OMP_LOOP:
2936 case EXEC_OMP_PARALLEL_LOOP:
2937 case EXEC_OMP_TEAMS_LOOP:
2938 case EXEC_OMP_TARGET_PARALLEL_LOOP:
2939 case EXEC_OMP_TARGET_TEAMS_LOOP:
2940 case EXEC_OMP_DO:
2941 case EXEC_OMP_PARALLEL_DO:
2942 case EXEC_OMP_SIMD:
2943 case EXEC_OMP_DO_SIMD:
2944 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
2945 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2946 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2947 case EXEC_OMP_TARGET_PARALLEL_DO:
2948 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2950 gcc_assert (o->head->next != NULL
2951 && (o->head->next->op == EXEC_DO
2952 || o->head->next->op == EXEC_DO_WHILE)
2953 && o->previous != NULL
2954 && o->previous->tail->op == o->head->op);
2955 if (o->previous->tail->ext.omp_clauses != NULL)
2957 if (o->previous->tail->ext.omp_clauses->collapse > 1)
2958 count = o->previous->tail->ext.omp_clauses->collapse;
2959 if (o->previous->tail->ext.omp_clauses->orderedc)
2960 count = o->previous->tail->ext.omp_clauses->orderedc;
2962 if (st == ST_EXIT && cnt <= count)
2964 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2965 return MATCH_ERROR;
2967 if (st == ST_CYCLE && cnt < count)
2969 gfc_error ("CYCLE statement at %C to non-innermost collapsed "
2970 "!$OMP DO loop");
2971 return MATCH_ERROR;
2973 break;
2974 default:
2975 break;
2978 /* Save the first statement in the construct - needed by the backend. */
2979 new_st.ext.which_construct = p->construct;
2981 new_st.op = op;
2983 return MATCH_YES;
2987 /* Match the EXIT statement. */
2989 match
2990 gfc_match_exit (void)
2992 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2996 /* Match the CYCLE statement. */
2998 match
2999 gfc_match_cycle (void)
3001 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
3005 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
3006 requirements for a stop-code differ in the standards.
3008 Fortran 95 has
3010 R840 stop-stmt is STOP [ stop-code ]
3011 R841 stop-code is scalar-char-constant
3012 or digit [ digit [ digit [ digit [ digit ] ] ] ]
3014 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
3015 Fortran 2008 has
3017 R855 stop-stmt is STOP [ stop-code ]
3018 R856 allstop-stmt is ALL STOP [ stop-code ]
3019 R857 stop-code is scalar-default-char-constant-expr
3020 or scalar-int-constant-expr
3021 Fortran 2018 has
3023 R1160 stop-stmt is STOP [ stop-code ] [ , QUIET = scalar-logical-expr]
3024 R1161 error-stop-stmt is
3025 ERROR STOP [ stop-code ] [ , QUIET = scalar-logical-expr]
3026 R1162 stop-code is scalar-default-char-expr
3027 or scalar-int-expr
3029 For free-form source code, all standards contain a statement of the form:
3031 A blank shall be used to separate names, constants, or labels from
3032 adjacent keywords, names, constants, or labels.
3034 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
3036 STOP123
3038 is valid, but it is invalid Fortran 2008. */
3040 static match
3041 gfc_match_stopcode (gfc_statement st)
3043 gfc_expr *e = NULL;
3044 gfc_expr *quiet = NULL;
3045 match m;
3046 bool f95, f03, f08;
3047 char c;
3049 /* Set f95 for -std=f95. */
3050 f95 = (gfc_option.allow_std == GFC_STD_OPT_F95);
3052 /* Set f03 for -std=f2003. */
3053 f03 = (gfc_option.allow_std == GFC_STD_OPT_F03);
3055 /* Set f08 for -std=f2008. */
3056 f08 = (gfc_option.allow_std == GFC_STD_OPT_F08);
3058 /* Plain STOP statement? */
3059 if (gfc_match_eos () == MATCH_YES)
3060 goto checks;
3062 /* Look for a blank between STOP and the stop-code for F2008 or later.
3063 But allow for F2018's ,QUIET= specifier. */
3064 c = gfc_peek_ascii_char ();
3066 if (gfc_current_form != FORM_FIXED && !(f95 || f03) && c != ',')
3068 /* Look for end-of-statement. There is no stop-code. */
3069 if (c == '\n' || c == '!' || c == ';')
3070 goto done;
3072 if (c != ' ')
3074 gfc_error ("Blank required in %s statement near %C",
3075 gfc_ascii_statement (st));
3076 return MATCH_ERROR;
3080 if (c == ' ')
3082 gfc_gobble_whitespace ();
3083 c = gfc_peek_ascii_char ();
3085 if (c != ',')
3087 int stopcode;
3088 locus old_locus;
3090 /* First look for the F95 or F2003 digit [...] construct. */
3091 old_locus = gfc_current_locus;
3092 m = gfc_match_small_int (&stopcode);
3093 if (m == MATCH_YES && (f95 || f03))
3095 if (stopcode < 0)
3097 gfc_error ("STOP code at %C cannot be negative");
3098 return MATCH_ERROR;
3101 if (stopcode > 99999)
3103 gfc_error ("STOP code at %C contains too many digits");
3104 return MATCH_ERROR;
3108 /* Reset the locus and now load gfc_expr. */
3109 gfc_current_locus = old_locus;
3110 m = gfc_match_expr (&e);
3111 if (m == MATCH_ERROR)
3112 goto cleanup;
3113 if (m == MATCH_NO)
3114 goto syntax;
3117 if (gfc_match (" , quiet = %e", &quiet) == MATCH_YES)
3119 if (!gfc_notify_std (GFC_STD_F2018, "QUIET= specifier for %s at %L",
3120 gfc_ascii_statement (st), &quiet->where))
3121 goto cleanup;
3124 if (gfc_match_eos () != MATCH_YES)
3125 goto syntax;
3127 checks:
3129 if (gfc_pure (NULL))
3131 if (st == ST_ERROR_STOP)
3133 if (!gfc_notify_std (GFC_STD_F2018, "%s statement at %C in PURE "
3134 "procedure", gfc_ascii_statement (st)))
3135 goto cleanup;
3137 else
3139 gfc_error ("%s statement not allowed in PURE procedure at %C",
3140 gfc_ascii_statement (st));
3141 goto cleanup;
3145 gfc_unset_implicit_pure (NULL);
3147 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
3149 gfc_error ("Image control statement STOP at %C in CRITICAL block");
3150 goto cleanup;
3152 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
3154 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
3155 goto cleanup;
3158 if (e != NULL)
3160 if (!gfc_simplify_expr (e, 0))
3161 goto cleanup;
3163 /* Test for F95 and F2003 style STOP stop-code. */
3164 if (e->expr_type != EXPR_CONSTANT && (f95 || f03))
3166 gfc_error ("STOP code at %L must be a scalar CHARACTER constant "
3167 "or digit[digit[digit[digit[digit]]]]", &e->where);
3168 goto cleanup;
3171 /* Use the machinery for an initialization expression to reduce the
3172 stop-code to a constant. */
3173 gfc_reduce_init_expr (e);
3175 /* Test for F2008 style STOP stop-code. */
3176 if (e->expr_type != EXPR_CONSTANT && f08)
3178 gfc_error ("STOP code at %L must be a scalar default CHARACTER or "
3179 "INTEGER constant expression", &e->where);
3180 goto cleanup;
3183 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
3185 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
3186 &e->where);
3187 goto cleanup;
3190 if (e->rank != 0)
3192 gfc_error ("STOP code at %L must be scalar", &e->where);
3193 goto cleanup;
3196 if (e->ts.type == BT_CHARACTER
3197 && e->ts.kind != gfc_default_character_kind)
3199 gfc_error ("STOP code at %L must be default character KIND=%d",
3200 &e->where, (int) gfc_default_character_kind);
3201 goto cleanup;
3204 if (e->ts.type == BT_INTEGER && e->ts.kind != gfc_default_integer_kind
3205 && !gfc_notify_std (GFC_STD_F2018,
3206 "STOP code at %L must be default integer KIND=%d",
3207 &e->where, (int) gfc_default_integer_kind))
3208 goto cleanup;
3211 if (quiet != NULL)
3213 if (!gfc_simplify_expr (quiet, 0))
3214 goto cleanup;
3216 if (quiet->rank != 0)
3218 gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
3219 &quiet->where);
3220 goto cleanup;
3224 done:
3226 switch (st)
3228 case ST_STOP:
3229 new_st.op = EXEC_STOP;
3230 break;
3231 case ST_ERROR_STOP:
3232 new_st.op = EXEC_ERROR_STOP;
3233 break;
3234 case ST_PAUSE:
3235 new_st.op = EXEC_PAUSE;
3236 break;
3237 default:
3238 gcc_unreachable ();
3241 new_st.expr1 = e;
3242 new_st.expr2 = quiet;
3243 new_st.ext.stop_code = -1;
3245 return MATCH_YES;
3247 syntax:
3248 gfc_syntax_error (st);
3250 cleanup:
3252 gfc_free_expr (e);
3253 gfc_free_expr (quiet);
3254 return MATCH_ERROR;
3258 /* Match the (deprecated) PAUSE statement. */
3260 match
3261 gfc_match_pause (void)
3263 match m;
3265 m = gfc_match_stopcode (ST_PAUSE);
3266 if (m == MATCH_YES)
3268 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
3269 m = MATCH_ERROR;
3271 return m;
3275 /* Match the STOP statement. */
3277 match
3278 gfc_match_stop (void)
3280 return gfc_match_stopcode (ST_STOP);
3284 /* Match the ERROR STOP statement. */
3286 match
3287 gfc_match_error_stop (void)
3289 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
3290 return MATCH_ERROR;
3292 return gfc_match_stopcode (ST_ERROR_STOP);
3295 /* Match EVENT POST/WAIT statement. Syntax:
3296 EVENT POST ( event-variable [, sync-stat-list] )
3297 EVENT WAIT ( event-variable [, wait-spec-list] )
3298 with
3299 wait-spec-list is sync-stat-list or until-spec
3300 until-spec is UNTIL_COUNT = scalar-int-expr
3301 sync-stat is STAT= or ERRMSG=. */
3303 static match
3304 event_statement (gfc_statement st)
3306 match m;
3307 gfc_expr *tmp, *eventvar, *until_count, *stat, *errmsg;
3308 bool saw_until_count, saw_stat, saw_errmsg;
3310 tmp = eventvar = until_count = stat = errmsg = NULL;
3311 saw_until_count = saw_stat = saw_errmsg = false;
3313 if (gfc_pure (NULL))
3315 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3316 st == ST_EVENT_POST ? "POST" : "WAIT");
3317 return MATCH_ERROR;
3320 gfc_unset_implicit_pure (NULL);
3322 if (flag_coarray == GFC_FCOARRAY_NONE)
3324 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3325 return MATCH_ERROR;
3328 if (gfc_find_state (COMP_CRITICAL))
3330 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3331 st == ST_EVENT_POST ? "POST" : "WAIT");
3332 return MATCH_ERROR;
3335 if (gfc_find_state (COMP_DO_CONCURRENT))
3337 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3338 "block", st == ST_EVENT_POST ? "POST" : "WAIT");
3339 return MATCH_ERROR;
3342 if (gfc_match_char ('(') != MATCH_YES)
3343 goto syntax;
3345 if (gfc_match ("%e", &eventvar) != MATCH_YES)
3346 goto syntax;
3347 m = gfc_match_char (',');
3348 if (m == MATCH_ERROR)
3349 goto syntax;
3350 if (m == MATCH_NO)
3352 m = gfc_match_char (')');
3353 if (m == MATCH_YES)
3354 goto done;
3355 goto syntax;
3358 for (;;)
3360 m = gfc_match (" stat = %v", &tmp);
3361 if (m == MATCH_ERROR)
3362 goto syntax;
3363 if (m == MATCH_YES)
3365 if (saw_stat)
3367 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3368 goto cleanup;
3370 stat = tmp;
3371 saw_stat = true;
3373 m = gfc_match_char (',');
3374 if (m == MATCH_YES)
3375 continue;
3377 tmp = NULL;
3378 break;
3381 m = gfc_match (" errmsg = %v", &tmp);
3382 if (m == MATCH_ERROR)
3383 goto syntax;
3384 if (m == MATCH_YES)
3386 if (saw_errmsg)
3388 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3389 goto cleanup;
3391 errmsg = tmp;
3392 saw_errmsg = true;
3394 m = gfc_match_char (',');
3395 if (m == MATCH_YES)
3396 continue;
3398 tmp = NULL;
3399 break;
3402 m = gfc_match (" until_count = %e", &tmp);
3403 if (m == MATCH_ERROR || st == ST_EVENT_POST)
3404 goto syntax;
3405 if (m == MATCH_YES)
3407 if (saw_until_count)
3409 gfc_error ("Redundant UNTIL_COUNT tag found at %L",
3410 &tmp->where);
3411 goto cleanup;
3413 until_count = tmp;
3414 saw_until_count = true;
3416 m = gfc_match_char (',');
3417 if (m == MATCH_YES)
3418 continue;
3420 tmp = NULL;
3421 break;
3424 break;
3427 if (m == MATCH_ERROR)
3428 goto syntax;
3430 if (gfc_match (" )%t") != MATCH_YES)
3431 goto syntax;
3433 done:
3434 switch (st)
3436 case ST_EVENT_POST:
3437 new_st.op = EXEC_EVENT_POST;
3438 break;
3439 case ST_EVENT_WAIT:
3440 new_st.op = EXEC_EVENT_WAIT;
3441 break;
3442 default:
3443 gcc_unreachable ();
3446 new_st.expr1 = eventvar;
3447 new_st.expr2 = stat;
3448 new_st.expr3 = errmsg;
3449 new_st.expr4 = until_count;
3451 return MATCH_YES;
3453 syntax:
3454 gfc_syntax_error (st);
3456 cleanup:
3457 if (until_count != tmp)
3458 gfc_free_expr (until_count);
3459 if (errmsg != tmp)
3460 gfc_free_expr (errmsg);
3461 if (stat != tmp)
3462 gfc_free_expr (stat);
3464 gfc_free_expr (tmp);
3465 gfc_free_expr (eventvar);
3467 return MATCH_ERROR;
3472 match
3473 gfc_match_event_post (void)
3475 if (!gfc_notify_std (GFC_STD_F2018, "EVENT POST statement at %C"))
3476 return MATCH_ERROR;
3478 return event_statement (ST_EVENT_POST);
3482 match
3483 gfc_match_event_wait (void)
3485 if (!gfc_notify_std (GFC_STD_F2018, "EVENT WAIT statement at %C"))
3486 return MATCH_ERROR;
3488 return event_statement (ST_EVENT_WAIT);
3492 /* Match a FAIL IMAGE statement. */
3494 match
3495 gfc_match_fail_image (void)
3497 if (!gfc_notify_std (GFC_STD_F2018, "FAIL IMAGE statement at %C"))
3498 return MATCH_ERROR;
3500 if (gfc_match_char ('(') == MATCH_YES)
3501 goto syntax;
3503 new_st.op = EXEC_FAIL_IMAGE;
3505 return MATCH_YES;
3507 syntax:
3508 gfc_syntax_error (ST_FAIL_IMAGE);
3510 return MATCH_ERROR;
3513 /* Match a FORM TEAM statement. */
3515 match
3516 gfc_match_form_team (void)
3518 match m;
3519 gfc_expr *teamid,*team;
3521 if (!gfc_notify_std (GFC_STD_F2018, "FORM TEAM statement at %C"))
3522 return MATCH_ERROR;
3524 if (gfc_match_char ('(') == MATCH_NO)
3525 goto syntax;
3527 new_st.op = EXEC_FORM_TEAM;
3529 if (gfc_match ("%e", &teamid) != MATCH_YES)
3530 goto syntax;
3531 m = gfc_match_char (',');
3532 if (m == MATCH_ERROR)
3533 goto syntax;
3534 if (gfc_match ("%e", &team) != MATCH_YES)
3535 goto syntax;
3537 m = gfc_match_char (')');
3538 if (m == MATCH_NO)
3539 goto syntax;
3541 new_st.expr1 = teamid;
3542 new_st.expr2 = team;
3544 return MATCH_YES;
3546 syntax:
3547 gfc_syntax_error (ST_FORM_TEAM);
3549 return MATCH_ERROR;
3552 /* Match a CHANGE TEAM statement. */
3554 match
3555 gfc_match_change_team (void)
3557 match m;
3558 gfc_expr *team;
3560 if (!gfc_notify_std (GFC_STD_F2018, "CHANGE TEAM statement at %C"))
3561 return MATCH_ERROR;
3563 if (gfc_match_char ('(') == MATCH_NO)
3564 goto syntax;
3566 new_st.op = EXEC_CHANGE_TEAM;
3568 if (gfc_match ("%e", &team) != MATCH_YES)
3569 goto syntax;
3571 m = gfc_match_char (')');
3572 if (m == MATCH_NO)
3573 goto syntax;
3575 new_st.expr1 = team;
3577 return MATCH_YES;
3579 syntax:
3580 gfc_syntax_error (ST_CHANGE_TEAM);
3582 return MATCH_ERROR;
3585 /* Match a END TEAM statement. */
3587 match
3588 gfc_match_end_team (void)
3590 if (!gfc_notify_std (GFC_STD_F2018, "END TEAM statement at %C"))
3591 return MATCH_ERROR;
3593 if (gfc_match_char ('(') == MATCH_YES)
3594 goto syntax;
3596 new_st.op = EXEC_END_TEAM;
3598 return MATCH_YES;
3600 syntax:
3601 gfc_syntax_error (ST_END_TEAM);
3603 return MATCH_ERROR;
3606 /* Match a SYNC TEAM statement. */
3608 match
3609 gfc_match_sync_team (void)
3611 match m;
3612 gfc_expr *team;
3614 if (!gfc_notify_std (GFC_STD_F2018, "SYNC TEAM statement at %C"))
3615 return MATCH_ERROR;
3617 if (gfc_match_char ('(') == MATCH_NO)
3618 goto syntax;
3620 new_st.op = EXEC_SYNC_TEAM;
3622 if (gfc_match ("%e", &team) != MATCH_YES)
3623 goto syntax;
3625 m = gfc_match_char (')');
3626 if (m == MATCH_NO)
3627 goto syntax;
3629 new_st.expr1 = team;
3631 return MATCH_YES;
3633 syntax:
3634 gfc_syntax_error (ST_SYNC_TEAM);
3636 return MATCH_ERROR;
3639 /* Match LOCK/UNLOCK statement. Syntax:
3640 LOCK ( lock-variable [ , lock-stat-list ] )
3641 UNLOCK ( lock-variable [ , sync-stat-list ] )
3642 where lock-stat is ACQUIRED_LOCK or sync-stat
3643 and sync-stat is STAT= or ERRMSG=. */
3645 static match
3646 lock_unlock_statement (gfc_statement st)
3648 match m;
3649 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
3650 bool saw_acq_lock, saw_stat, saw_errmsg;
3652 tmp = lockvar = acq_lock = stat = errmsg = NULL;
3653 saw_acq_lock = saw_stat = saw_errmsg = false;
3655 if (gfc_pure (NULL))
3657 gfc_error ("Image control statement %s at %C in PURE procedure",
3658 st == ST_LOCK ? "LOCK" : "UNLOCK");
3659 return MATCH_ERROR;
3662 gfc_unset_implicit_pure (NULL);
3664 if (flag_coarray == GFC_FCOARRAY_NONE)
3666 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3667 return MATCH_ERROR;
3670 if (gfc_find_state (COMP_CRITICAL))
3672 gfc_error ("Image control statement %s at %C in CRITICAL block",
3673 st == ST_LOCK ? "LOCK" : "UNLOCK");
3674 return MATCH_ERROR;
3677 if (gfc_find_state (COMP_DO_CONCURRENT))
3679 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3680 st == ST_LOCK ? "LOCK" : "UNLOCK");
3681 return MATCH_ERROR;
3684 if (gfc_match_char ('(') != MATCH_YES)
3685 goto syntax;
3687 if (gfc_match ("%e", &lockvar) != MATCH_YES)
3688 goto syntax;
3689 m = gfc_match_char (',');
3690 if (m == MATCH_ERROR)
3691 goto syntax;
3692 if (m == MATCH_NO)
3694 m = gfc_match_char (')');
3695 if (m == MATCH_YES)
3696 goto done;
3697 goto syntax;
3700 for (;;)
3702 m = gfc_match (" stat = %v", &tmp);
3703 if (m == MATCH_ERROR)
3704 goto syntax;
3705 if (m == MATCH_YES)
3707 if (saw_stat)
3709 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3710 goto cleanup;
3712 stat = tmp;
3713 saw_stat = true;
3715 m = gfc_match_char (',');
3716 if (m == MATCH_YES)
3717 continue;
3719 tmp = NULL;
3720 break;
3723 m = gfc_match (" errmsg = %v", &tmp);
3724 if (m == MATCH_ERROR)
3725 goto syntax;
3726 if (m == MATCH_YES)
3728 if (saw_errmsg)
3730 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3731 goto cleanup;
3733 errmsg = tmp;
3734 saw_errmsg = true;
3736 m = gfc_match_char (',');
3737 if (m == MATCH_YES)
3738 continue;
3740 tmp = NULL;
3741 break;
3744 m = gfc_match (" acquired_lock = %v", &tmp);
3745 if (m == MATCH_ERROR || st == ST_UNLOCK)
3746 goto syntax;
3747 if (m == MATCH_YES)
3749 if (saw_acq_lock)
3751 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L",
3752 &tmp->where);
3753 goto cleanup;
3755 acq_lock = tmp;
3756 saw_acq_lock = true;
3758 m = gfc_match_char (',');
3759 if (m == MATCH_YES)
3760 continue;
3762 tmp = NULL;
3763 break;
3766 break;
3769 if (m == MATCH_ERROR)
3770 goto syntax;
3772 if (gfc_match (" )%t") != MATCH_YES)
3773 goto syntax;
3775 done:
3776 switch (st)
3778 case ST_LOCK:
3779 new_st.op = EXEC_LOCK;
3780 break;
3781 case ST_UNLOCK:
3782 new_st.op = EXEC_UNLOCK;
3783 break;
3784 default:
3785 gcc_unreachable ();
3788 new_st.expr1 = lockvar;
3789 new_st.expr2 = stat;
3790 new_st.expr3 = errmsg;
3791 new_st.expr4 = acq_lock;
3793 return MATCH_YES;
3795 syntax:
3796 gfc_syntax_error (st);
3798 cleanup:
3799 if (acq_lock != tmp)
3800 gfc_free_expr (acq_lock);
3801 if (errmsg != tmp)
3802 gfc_free_expr (errmsg);
3803 if (stat != tmp)
3804 gfc_free_expr (stat);
3806 gfc_free_expr (tmp);
3807 gfc_free_expr (lockvar);
3809 return MATCH_ERROR;
3813 match
3814 gfc_match_lock (void)
3816 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
3817 return MATCH_ERROR;
3819 return lock_unlock_statement (ST_LOCK);
3823 match
3824 gfc_match_unlock (void)
3826 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
3827 return MATCH_ERROR;
3829 return lock_unlock_statement (ST_UNLOCK);
3833 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3834 SYNC ALL [(sync-stat-list)]
3835 SYNC MEMORY [(sync-stat-list)]
3836 SYNC IMAGES (image-set [, sync-stat-list] )
3837 with sync-stat is int-expr or *. */
3839 static match
3840 sync_statement (gfc_statement st)
3842 match m;
3843 gfc_expr *tmp, *imageset, *stat, *errmsg;
3844 bool saw_stat, saw_errmsg;
3846 tmp = imageset = stat = errmsg = NULL;
3847 saw_stat = saw_errmsg = false;
3849 if (gfc_pure (NULL))
3851 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3852 return MATCH_ERROR;
3855 gfc_unset_implicit_pure (NULL);
3857 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
3858 return MATCH_ERROR;
3860 if (flag_coarray == GFC_FCOARRAY_NONE)
3862 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3863 "enable");
3864 return MATCH_ERROR;
3867 if (gfc_find_state (COMP_CRITICAL))
3869 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3870 return MATCH_ERROR;
3873 if (gfc_find_state (COMP_DO_CONCURRENT))
3875 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3876 return MATCH_ERROR;
3879 if (gfc_match_eos () == MATCH_YES)
3881 if (st == ST_SYNC_IMAGES)
3882 goto syntax;
3883 goto done;
3886 if (gfc_match_char ('(') != MATCH_YES)
3887 goto syntax;
3889 if (st == ST_SYNC_IMAGES)
3891 /* Denote '*' as imageset == NULL. */
3892 m = gfc_match_char ('*');
3893 if (m == MATCH_ERROR)
3894 goto syntax;
3895 if (m == MATCH_NO)
3897 if (gfc_match ("%e", &imageset) != MATCH_YES)
3898 goto syntax;
3900 m = gfc_match_char (',');
3901 if (m == MATCH_ERROR)
3902 goto syntax;
3903 if (m == MATCH_NO)
3905 m = gfc_match_char (')');
3906 if (m == MATCH_YES)
3907 goto done;
3908 goto syntax;
3912 for (;;)
3914 m = gfc_match (" stat = %e", &tmp);
3915 if (m == MATCH_ERROR)
3916 goto syntax;
3917 if (m == MATCH_YES)
3919 if (saw_stat)
3921 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3922 goto cleanup;
3924 stat = tmp;
3925 saw_stat = true;
3927 if (gfc_match_char (',') == MATCH_YES)
3928 continue;
3930 tmp = NULL;
3931 break;
3934 m = gfc_match (" errmsg = %e", &tmp);
3935 if (m == MATCH_ERROR)
3936 goto syntax;
3937 if (m == MATCH_YES)
3939 if (saw_errmsg)
3941 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3942 goto cleanup;
3944 errmsg = tmp;
3945 saw_errmsg = true;
3947 if (gfc_match_char (',') == MATCH_YES)
3948 continue;
3950 tmp = NULL;
3951 break;
3954 break;
3957 if (gfc_match (" )%t") != MATCH_YES)
3958 goto syntax;
3960 done:
3961 switch (st)
3963 case ST_SYNC_ALL:
3964 new_st.op = EXEC_SYNC_ALL;
3965 break;
3966 case ST_SYNC_IMAGES:
3967 new_st.op = EXEC_SYNC_IMAGES;
3968 break;
3969 case ST_SYNC_MEMORY:
3970 new_st.op = EXEC_SYNC_MEMORY;
3971 break;
3972 default:
3973 gcc_unreachable ();
3976 new_st.expr1 = imageset;
3977 new_st.expr2 = stat;
3978 new_st.expr3 = errmsg;
3980 return MATCH_YES;
3982 syntax:
3983 gfc_syntax_error (st);
3985 cleanup:
3986 if (stat != tmp)
3987 gfc_free_expr (stat);
3988 if (errmsg != tmp)
3989 gfc_free_expr (errmsg);
3991 gfc_free_expr (tmp);
3992 gfc_free_expr (imageset);
3994 return MATCH_ERROR;
3998 /* Match SYNC ALL statement. */
4000 match
4001 gfc_match_sync_all (void)
4003 return sync_statement (ST_SYNC_ALL);
4007 /* Match SYNC IMAGES statement. */
4009 match
4010 gfc_match_sync_images (void)
4012 return sync_statement (ST_SYNC_IMAGES);
4016 /* Match SYNC MEMORY statement. */
4018 match
4019 gfc_match_sync_memory (void)
4021 return sync_statement (ST_SYNC_MEMORY);
4025 /* Match a CONTINUE statement. */
4027 match
4028 gfc_match_continue (void)
4030 if (gfc_match_eos () != MATCH_YES)
4032 gfc_syntax_error (ST_CONTINUE);
4033 return MATCH_ERROR;
4036 new_st.op = EXEC_CONTINUE;
4037 return MATCH_YES;
4041 /* Match the (deprecated) ASSIGN statement. */
4043 match
4044 gfc_match_assign (void)
4046 gfc_expr *expr;
4047 gfc_st_label *label;
4049 if (gfc_match (" %l", &label) == MATCH_YES)
4051 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
4052 return MATCH_ERROR;
4053 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
4055 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
4056 return MATCH_ERROR;
4058 expr->symtree->n.sym->attr.assign = 1;
4060 new_st.op = EXEC_LABEL_ASSIGN;
4061 new_st.label1 = label;
4062 new_st.expr1 = expr;
4063 return MATCH_YES;
4066 return MATCH_NO;
4070 /* Match the GO TO statement. As a computed GOTO statement is
4071 matched, it is transformed into an equivalent SELECT block. No
4072 tree is necessary, and the resulting jumps-to-jumps are
4073 specifically optimized away by the back end. */
4075 match
4076 gfc_match_goto (void)
4078 gfc_code *head, *tail;
4079 gfc_expr *expr;
4080 gfc_case *cp;
4081 gfc_st_label *label;
4082 int i;
4083 match m;
4085 if (gfc_match (" %l%t", &label) == MATCH_YES)
4087 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4088 return MATCH_ERROR;
4090 new_st.op = EXEC_GOTO;
4091 new_st.label1 = label;
4092 return MATCH_YES;
4095 /* The assigned GO TO statement. */
4097 if (gfc_match_variable (&expr, 0) == MATCH_YES)
4099 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
4100 return MATCH_ERROR;
4102 new_st.op = EXEC_GOTO;
4103 new_st.expr1 = expr;
4105 if (gfc_match_eos () == MATCH_YES)
4106 return MATCH_YES;
4108 /* Match label list. */
4109 gfc_match_char (',');
4110 if (gfc_match_char ('(') != MATCH_YES)
4112 gfc_syntax_error (ST_GOTO);
4113 return MATCH_ERROR;
4115 head = tail = NULL;
4119 m = gfc_match_st_label (&label);
4120 if (m != MATCH_YES)
4121 goto syntax;
4123 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4124 goto cleanup;
4126 if (head == NULL)
4127 head = tail = gfc_get_code (EXEC_GOTO);
4128 else
4130 tail->block = gfc_get_code (EXEC_GOTO);
4131 tail = tail->block;
4134 tail->label1 = label;
4136 while (gfc_match_char (',') == MATCH_YES);
4138 if (gfc_match (" )%t") != MATCH_YES)
4139 goto syntax;
4141 if (head == NULL)
4143 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4144 goto syntax;
4146 new_st.block = head;
4148 return MATCH_YES;
4151 /* Last chance is a computed GO TO statement. */
4152 if (gfc_match_char ('(') != MATCH_YES)
4154 gfc_syntax_error (ST_GOTO);
4155 return MATCH_ERROR;
4158 head = tail = NULL;
4159 i = 1;
4163 m = gfc_match_st_label (&label);
4164 if (m != MATCH_YES)
4165 goto syntax;
4167 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4168 goto cleanup;
4170 if (head == NULL)
4171 head = tail = gfc_get_code (EXEC_SELECT);
4172 else
4174 tail->block = gfc_get_code (EXEC_SELECT);
4175 tail = tail->block;
4178 cp = gfc_get_case ();
4179 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
4180 NULL, i++);
4182 tail->ext.block.case_list = cp;
4184 tail->next = gfc_get_code (EXEC_GOTO);
4185 tail->next->label1 = label;
4187 while (gfc_match_char (',') == MATCH_YES);
4189 if (gfc_match_char (')') != MATCH_YES)
4190 goto syntax;
4192 if (head == NULL)
4194 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4195 goto syntax;
4198 /* Get the rest of the statement. */
4199 gfc_match_char (',');
4201 if (gfc_match (" %e%t", &expr) != MATCH_YES)
4202 goto syntax;
4204 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
4205 return MATCH_ERROR;
4207 /* At this point, a computed GOTO has been fully matched and an
4208 equivalent SELECT statement constructed. */
4210 new_st.op = EXEC_SELECT;
4211 new_st.expr1 = NULL;
4213 /* Hack: For a "real" SELECT, the expression is in expr. We put
4214 it in expr2 so we can distinguish then and produce the correct
4215 diagnostics. */
4216 new_st.expr2 = expr;
4217 new_st.block = head;
4218 return MATCH_YES;
4220 syntax:
4221 gfc_syntax_error (ST_GOTO);
4222 cleanup:
4223 gfc_free_statements (head);
4224 return MATCH_ERROR;
4228 /* Frees a list of gfc_alloc structures. */
4230 void
4231 gfc_free_alloc_list (gfc_alloc *p)
4233 gfc_alloc *q;
4235 for (; p; p = q)
4237 q = p->next;
4238 gfc_free_expr (p->expr);
4239 free (p);
4244 /* Match an ALLOCATE statement. */
4246 match
4247 gfc_match_allocate (void)
4249 gfc_alloc *head, *tail;
4250 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
4251 gfc_typespec ts;
4252 gfc_symbol *sym;
4253 match m;
4254 locus old_locus, deferred_locus, assumed_locus;
4255 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
4256 bool saw_unlimited = false, saw_assumed = false;
4258 head = tail = NULL;
4259 stat = errmsg = source = mold = tmp = NULL;
4260 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
4262 if (gfc_match_char ('(') != MATCH_YES)
4264 gfc_syntax_error (ST_ALLOCATE);
4265 return MATCH_ERROR;
4268 /* Match an optional type-spec. */
4269 old_locus = gfc_current_locus;
4270 m = gfc_match_type_spec (&ts);
4271 if (m == MATCH_ERROR)
4272 goto cleanup;
4273 else if (m == MATCH_NO)
4275 char name[GFC_MAX_SYMBOL_LEN + 3];
4277 if (gfc_match ("%n :: ", name) == MATCH_YES)
4279 gfc_error ("Error in type-spec at %L", &old_locus);
4280 goto cleanup;
4283 ts.type = BT_UNKNOWN;
4285 else
4287 /* Needed for the F2008:C631 check below. */
4288 assumed_locus = gfc_current_locus;
4290 if (gfc_match (" :: ") == MATCH_YES)
4292 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
4293 &old_locus))
4294 goto cleanup;
4296 if (ts.deferred)
4298 gfc_error ("Type-spec at %L cannot contain a deferred "
4299 "type parameter", &old_locus);
4300 goto cleanup;
4303 if (ts.type == BT_CHARACTER)
4305 if (!ts.u.cl->length)
4306 saw_assumed = true;
4307 else
4308 ts.u.cl->length_from_typespec = true;
4311 if (type_param_spec_list
4312 && gfc_spec_list_type (type_param_spec_list, NULL)
4313 == SPEC_DEFERRED)
4315 gfc_error ("The type parameter spec list in the type-spec at "
4316 "%L cannot contain DEFERRED parameters", &old_locus);
4317 goto cleanup;
4320 else
4322 ts.type = BT_UNKNOWN;
4323 gfc_current_locus = old_locus;
4327 for (;;)
4329 if (head == NULL)
4330 head = tail = gfc_get_alloc ();
4331 else
4333 tail->next = gfc_get_alloc ();
4334 tail = tail->next;
4337 m = gfc_match_variable (&tail->expr, 0);
4338 if (m == MATCH_NO)
4339 goto syntax;
4340 if (m == MATCH_ERROR)
4341 goto cleanup;
4343 if (tail->expr->expr_type == EXPR_CONSTANT)
4345 gfc_error ("Unexpected constant at %C");
4346 goto cleanup;
4349 if (gfc_check_do_variable (tail->expr->symtree))
4350 goto cleanup;
4352 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
4353 if (impure && gfc_pure (NULL))
4355 gfc_error ("Bad allocate-object at %C for a PURE procedure");
4356 goto cleanup;
4359 if (impure)
4360 gfc_unset_implicit_pure (NULL);
4362 /* F2008:C631 (R626) A type-param-value in a type-spec shall be an
4363 asterisk if and only if each allocate-object is a dummy argument
4364 for which the corresponding type parameter is assumed. */
4365 if (saw_assumed
4366 && (tail->expr->ts.deferred
4367 || (tail->expr->ts.u.cl && tail->expr->ts.u.cl->length)
4368 || tail->expr->symtree->n.sym->attr.dummy == 0))
4370 gfc_error ("Incompatible allocate-object at %C for CHARACTER "
4371 "type-spec at %L", &assumed_locus);
4372 goto cleanup;
4375 if (tail->expr->ts.deferred)
4377 saw_deferred = true;
4378 deferred_locus = tail->expr->where;
4381 if (gfc_find_state (COMP_DO_CONCURRENT)
4382 || gfc_find_state (COMP_CRITICAL))
4384 gfc_ref *ref;
4385 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
4386 for (ref = tail->expr->ref; ref; ref = ref->next)
4387 if (ref->type == REF_COMPONENT)
4388 coarray = ref->u.c.component->attr.codimension;
4390 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
4392 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
4393 goto cleanup;
4395 if (coarray && gfc_find_state (COMP_CRITICAL))
4397 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
4398 goto cleanup;
4402 /* Check for F08:C628. */
4403 sym = tail->expr->symtree->n.sym;
4404 b1 = !(tail->expr->ref
4405 && (tail->expr->ref->type == REF_COMPONENT
4406 || tail->expr->ref->type == REF_ARRAY));
4407 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
4408 b2 = !(CLASS_DATA (sym)->attr.allocatable
4409 || CLASS_DATA (sym)->attr.class_pointer);
4410 else
4411 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4412 || sym->attr.proc_pointer);
4413 b3 = sym && sym->ns && sym->ns->proc_name
4414 && (sym->ns->proc_name->attr.allocatable
4415 || sym->ns->proc_name->attr.pointer
4416 || sym->ns->proc_name->attr.proc_pointer);
4417 if (b1 && b2 && !b3)
4419 gfc_error ("Allocate-object at %L is neither a data pointer "
4420 "nor an allocatable variable", &tail->expr->where);
4421 goto cleanup;
4424 /* The ALLOCATE statement had an optional typespec. Check the
4425 constraints. */
4426 if (ts.type != BT_UNKNOWN)
4428 /* Enforce F03:C624. */
4429 if (!gfc_type_compatible (&tail->expr->ts, &ts))
4431 gfc_error ("Type of entity at %L is type incompatible with "
4432 "typespec", &tail->expr->where);
4433 goto cleanup;
4436 /* Enforce F03:C627. */
4437 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
4439 gfc_error ("Kind type parameter for entity at %L differs from "
4440 "the kind type parameter of the typespec",
4441 &tail->expr->where);
4442 goto cleanup;
4446 if (tail->expr->ts.type == BT_DERIVED)
4447 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
4449 if (type_param_spec_list)
4450 tail->expr->param_list = gfc_copy_actual_arglist (type_param_spec_list);
4452 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
4454 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
4456 gfc_error ("Shape specification for allocatable scalar at %C");
4457 goto cleanup;
4460 if (gfc_match_char (',') != MATCH_YES)
4461 break;
4463 alloc_opt_list:
4465 m = gfc_match (" stat = %e", &tmp);
4466 if (m == MATCH_ERROR)
4467 goto cleanup;
4468 if (m == MATCH_YES)
4470 /* Enforce C630. */
4471 if (saw_stat)
4473 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
4474 goto cleanup;
4477 stat = tmp;
4478 tmp = NULL;
4479 saw_stat = true;
4481 if (stat->expr_type == EXPR_CONSTANT)
4483 gfc_error ("STAT tag at %L cannot be a constant", &stat->where);
4484 goto cleanup;
4487 if (gfc_check_do_variable (stat->symtree))
4488 goto cleanup;
4490 if (gfc_match_char (',') == MATCH_YES)
4491 goto alloc_opt_list;
4494 m = gfc_match (" errmsg = %e", &tmp);
4495 if (m == MATCH_ERROR)
4496 goto cleanup;
4497 if (m == MATCH_YES)
4499 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
4500 goto cleanup;
4502 /* Enforce C630. */
4503 if (saw_errmsg)
4505 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
4506 goto cleanup;
4509 errmsg = tmp;
4510 tmp = NULL;
4511 saw_errmsg = true;
4513 if (gfc_match_char (',') == MATCH_YES)
4514 goto alloc_opt_list;
4517 m = gfc_match (" source = %e", &tmp);
4518 if (m == MATCH_ERROR)
4519 goto cleanup;
4520 if (m == MATCH_YES)
4522 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
4523 goto cleanup;
4525 /* Enforce C630. */
4526 if (saw_source)
4528 gfc_error ("Redundant SOURCE tag found at %L", &tmp->where);
4529 goto cleanup;
4532 /* The next 2 conditionals check C631. */
4533 if (ts.type != BT_UNKNOWN)
4535 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4536 &tmp->where, &old_locus);
4537 goto cleanup;
4540 if (head->next
4541 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
4542 " with more than a single allocate object",
4543 &tmp->where))
4544 goto cleanup;
4546 source = tmp;
4547 tmp = NULL;
4548 saw_source = true;
4550 if (gfc_match_char (',') == MATCH_YES)
4551 goto alloc_opt_list;
4554 m = gfc_match (" mold = %e", &tmp);
4555 if (m == MATCH_ERROR)
4556 goto cleanup;
4557 if (m == MATCH_YES)
4559 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
4560 goto cleanup;
4562 /* Check F08:C636. */
4563 if (saw_mold)
4565 gfc_error ("Redundant MOLD tag found at %L", &tmp->where);
4566 goto cleanup;
4569 /* Check F08:C637. */
4570 if (ts.type != BT_UNKNOWN)
4572 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4573 &tmp->where, &old_locus);
4574 goto cleanup;
4577 mold = tmp;
4578 tmp = NULL;
4579 saw_mold = true;
4580 mold->mold = 1;
4582 if (gfc_match_char (',') == MATCH_YES)
4583 goto alloc_opt_list;
4586 gfc_gobble_whitespace ();
4588 if (gfc_peek_char () == ')')
4589 break;
4592 if (gfc_match (" )%t") != MATCH_YES)
4593 goto syntax;
4595 /* Check F08:C637. */
4596 if (source && mold)
4598 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4599 &mold->where, &source->where);
4600 goto cleanup;
4603 /* Check F03:C623, */
4604 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
4606 gfc_error ("Allocate-object at %L with a deferred type parameter "
4607 "requires either a type-spec or SOURCE tag or a MOLD tag",
4608 &deferred_locus);
4609 goto cleanup;
4612 /* Check F03:C625, */
4613 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
4615 for (tail = head; tail; tail = tail->next)
4617 if (UNLIMITED_POLY (tail->expr))
4618 gfc_error ("Unlimited polymorphic allocate-object at %L "
4619 "requires either a type-spec or SOURCE tag "
4620 "or a MOLD tag", &tail->expr->where);
4622 goto cleanup;
4625 new_st.op = EXEC_ALLOCATE;
4626 new_st.expr1 = stat;
4627 new_st.expr2 = errmsg;
4628 if (source)
4629 new_st.expr3 = source;
4630 else
4631 new_st.expr3 = mold;
4632 new_st.ext.alloc.list = head;
4633 new_st.ext.alloc.ts = ts;
4635 if (type_param_spec_list)
4636 gfc_free_actual_arglist (type_param_spec_list);
4638 return MATCH_YES;
4640 syntax:
4641 gfc_syntax_error (ST_ALLOCATE);
4643 cleanup:
4644 gfc_free_expr (errmsg);
4645 gfc_free_expr (source);
4646 gfc_free_expr (stat);
4647 gfc_free_expr (mold);
4648 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
4649 gfc_free_alloc_list (head);
4650 if (type_param_spec_list)
4651 gfc_free_actual_arglist (type_param_spec_list);
4652 return MATCH_ERROR;
4656 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4657 a set of pointer assignments to intrinsic NULL(). */
4659 match
4660 gfc_match_nullify (void)
4662 gfc_code *tail;
4663 gfc_expr *e, *p;
4664 match m;
4666 tail = NULL;
4668 if (gfc_match_char ('(') != MATCH_YES)
4669 goto syntax;
4671 for (;;)
4673 m = gfc_match_variable (&p, 0);
4674 if (m == MATCH_ERROR)
4675 goto cleanup;
4676 if (m == MATCH_NO)
4677 goto syntax;
4679 if (gfc_check_do_variable (p->symtree))
4680 goto cleanup;
4682 /* F2008, C1242. */
4683 if (gfc_is_coindexed (p))
4685 gfc_error ("Pointer object at %C shall not be coindexed");
4686 goto cleanup;
4689 /* Check for valid array pointer object. Bounds remapping is not
4690 allowed with NULLIFY. */
4691 if (p->ref)
4693 gfc_ref *remap = p->ref;
4694 for (; remap; remap = remap->next)
4695 if (!remap->next && remap->type == REF_ARRAY
4696 && remap->u.ar.type != AR_FULL)
4697 break;
4698 if (remap)
4700 gfc_error ("NULLIFY does not allow bounds remapping for "
4701 "pointer object at %C");
4702 goto cleanup;
4706 /* build ' => NULL() '. */
4707 e = gfc_get_null_expr (&gfc_current_locus);
4709 /* Chain to list. */
4710 if (tail == NULL)
4712 tail = &new_st;
4713 tail->op = EXEC_POINTER_ASSIGN;
4715 else
4717 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
4718 tail = tail->next;
4721 tail->expr1 = p;
4722 tail->expr2 = e;
4724 if (gfc_match (" )%t") == MATCH_YES)
4725 break;
4726 if (gfc_match_char (',') != MATCH_YES)
4727 goto syntax;
4730 return MATCH_YES;
4732 syntax:
4733 gfc_syntax_error (ST_NULLIFY);
4735 cleanup:
4736 gfc_free_statements (new_st.next);
4737 new_st.next = NULL;
4738 gfc_free_expr (new_st.expr1);
4739 new_st.expr1 = NULL;
4740 gfc_free_expr (new_st.expr2);
4741 new_st.expr2 = NULL;
4742 return MATCH_ERROR;
4746 /* Match a DEALLOCATE statement. */
4748 match
4749 gfc_match_deallocate (void)
4751 gfc_alloc *head, *tail;
4752 gfc_expr *stat, *errmsg, *tmp;
4753 gfc_symbol *sym;
4754 match m;
4755 bool saw_stat, saw_errmsg, b1, b2;
4757 head = tail = NULL;
4758 stat = errmsg = tmp = NULL;
4759 saw_stat = saw_errmsg = false;
4761 if (gfc_match_char ('(') != MATCH_YES)
4762 goto syntax;
4764 for (;;)
4766 if (head == NULL)
4767 head = tail = gfc_get_alloc ();
4768 else
4770 tail->next = gfc_get_alloc ();
4771 tail = tail->next;
4774 m = gfc_match_variable (&tail->expr, 0);
4775 if (m == MATCH_ERROR)
4776 goto cleanup;
4777 if (m == MATCH_NO)
4778 goto syntax;
4780 if (tail->expr->expr_type == EXPR_CONSTANT)
4782 gfc_error ("Unexpected constant at %C");
4783 goto cleanup;
4786 if (gfc_check_do_variable (tail->expr->symtree))
4787 goto cleanup;
4789 sym = tail->expr->symtree->n.sym;
4791 bool impure = gfc_impure_variable (sym);
4792 if (impure && gfc_pure (NULL))
4794 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4795 goto cleanup;
4798 if (impure)
4799 gfc_unset_implicit_pure (NULL);
4801 if (gfc_is_coarray (tail->expr)
4802 && gfc_find_state (COMP_DO_CONCURRENT))
4804 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4805 goto cleanup;
4808 if (gfc_is_coarray (tail->expr)
4809 && gfc_find_state (COMP_CRITICAL))
4811 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4812 goto cleanup;
4815 /* FIXME: disable the checking on derived types. */
4816 b1 = !(tail->expr->ref
4817 && (tail->expr->ref->type == REF_COMPONENT
4818 || tail->expr->ref->type == REF_ARRAY));
4819 if (sym && sym->ts.type == BT_CLASS)
4820 b2 = !(CLASS_DATA (sym) && (CLASS_DATA (sym)->attr.allocatable
4821 || CLASS_DATA (sym)->attr.class_pointer));
4822 else
4823 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4824 || sym->attr.proc_pointer);
4825 if (b1 && b2)
4827 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4828 "nor an allocatable variable");
4829 goto cleanup;
4832 if (gfc_match_char (',') != MATCH_YES)
4833 break;
4835 dealloc_opt_list:
4837 m = gfc_match (" stat = %e", &tmp);
4838 if (m == MATCH_ERROR)
4839 goto cleanup;
4840 if (m == MATCH_YES)
4842 if (saw_stat)
4844 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
4845 gfc_free_expr (tmp);
4846 goto cleanup;
4849 stat = tmp;
4850 saw_stat = true;
4852 if (gfc_check_do_variable (stat->symtree))
4853 goto cleanup;
4855 if (gfc_match_char (',') == MATCH_YES)
4856 goto dealloc_opt_list;
4859 m = gfc_match (" errmsg = %e", &tmp);
4860 if (m == MATCH_ERROR)
4861 goto cleanup;
4862 if (m == MATCH_YES)
4864 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
4865 goto cleanup;
4867 if (saw_errmsg)
4869 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
4870 gfc_free_expr (tmp);
4871 goto cleanup;
4874 errmsg = tmp;
4875 saw_errmsg = true;
4877 if (gfc_match_char (',') == MATCH_YES)
4878 goto dealloc_opt_list;
4881 gfc_gobble_whitespace ();
4883 if (gfc_peek_char () == ')')
4884 break;
4887 if (gfc_match (" )%t") != MATCH_YES)
4888 goto syntax;
4890 new_st.op = EXEC_DEALLOCATE;
4891 new_st.expr1 = stat;
4892 new_st.expr2 = errmsg;
4893 new_st.ext.alloc.list = head;
4895 return MATCH_YES;
4897 syntax:
4898 gfc_syntax_error (ST_DEALLOCATE);
4900 cleanup:
4901 gfc_free_expr (errmsg);
4902 gfc_free_expr (stat);
4903 gfc_free_alloc_list (head);
4904 return MATCH_ERROR;
4908 /* Match a RETURN statement. */
4910 match
4911 gfc_match_return (void)
4913 gfc_expr *e;
4914 match m;
4915 gfc_compile_state s;
4917 e = NULL;
4919 if (gfc_find_state (COMP_CRITICAL))
4921 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4922 return MATCH_ERROR;
4925 if (gfc_find_state (COMP_DO_CONCURRENT))
4927 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4928 return MATCH_ERROR;
4931 if (gfc_match_eos () == MATCH_YES)
4932 goto done;
4934 if (!gfc_find_state (COMP_SUBROUTINE))
4936 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4937 "a SUBROUTINE");
4938 goto cleanup;
4941 if (gfc_current_form == FORM_FREE)
4943 /* The following are valid, so we can't require a blank after the
4944 RETURN keyword:
4945 return+1
4946 return(1) */
4947 char c = gfc_peek_ascii_char ();
4948 if (ISALPHA (c) || ISDIGIT (c))
4949 return MATCH_NO;
4952 m = gfc_match (" %e%t", &e);
4953 if (m == MATCH_YES)
4954 goto done;
4955 if (m == MATCH_ERROR)
4956 goto cleanup;
4958 gfc_syntax_error (ST_RETURN);
4960 cleanup:
4961 gfc_free_expr (e);
4962 return MATCH_ERROR;
4964 done:
4965 gfc_enclosing_unit (&s);
4966 if (s == COMP_PROGRAM
4967 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4968 "main program at %C"))
4969 return MATCH_ERROR;
4971 new_st.op = EXEC_RETURN;
4972 new_st.expr1 = e;
4974 return MATCH_YES;
4978 /* Match the call of a type-bound procedure, if CALL%var has already been
4979 matched and var found to be a derived-type variable. */
4981 static match
4982 match_typebound_call (gfc_symtree* varst)
4984 gfc_expr* base;
4985 match m;
4987 base = gfc_get_expr ();
4988 base->expr_type = EXPR_VARIABLE;
4989 base->symtree = varst;
4990 base->where = gfc_current_locus;
4991 gfc_set_sym_referenced (varst->n.sym);
4993 m = gfc_match_varspec (base, 0, true, true);
4994 if (m == MATCH_NO)
4995 gfc_error ("Expected component reference at %C");
4996 if (m != MATCH_YES)
4998 gfc_free_expr (base);
4999 return MATCH_ERROR;
5002 if (gfc_match_eos () != MATCH_YES)
5004 gfc_error ("Junk after CALL at %C");
5005 gfc_free_expr (base);
5006 return MATCH_ERROR;
5009 if (base->expr_type == EXPR_COMPCALL)
5010 new_st.op = EXEC_COMPCALL;
5011 else if (base->expr_type == EXPR_PPC)
5012 new_st.op = EXEC_CALL_PPC;
5013 else
5015 gfc_error ("Expected type-bound procedure or procedure pointer component "
5016 "at %C");
5017 gfc_free_expr (base);
5018 return MATCH_ERROR;
5020 new_st.expr1 = base;
5022 return MATCH_YES;
5026 /* Match a CALL statement. The tricky part here are possible
5027 alternate return specifiers. We handle these by having all
5028 "subroutines" actually return an integer via a register that gives
5029 the return number. If the call specifies alternate returns, we
5030 generate code for a SELECT statement whose case clauses contain
5031 GOTOs to the various labels. */
5033 match
5034 gfc_match_call (void)
5036 char name[GFC_MAX_SYMBOL_LEN + 1];
5037 gfc_actual_arglist *a, *arglist;
5038 gfc_case *new_case;
5039 gfc_symbol *sym;
5040 gfc_symtree *st;
5041 gfc_code *c;
5042 match m;
5043 int i;
5045 arglist = NULL;
5047 m = gfc_match ("% %n", name);
5048 if (m == MATCH_NO)
5049 goto syntax;
5050 if (m != MATCH_YES)
5051 return m;
5053 if (gfc_get_ha_sym_tree (name, &st))
5054 return MATCH_ERROR;
5056 sym = st->n.sym;
5058 /* If this is a variable of derived-type, it probably starts a type-bound
5059 procedure call. Associate variable targets have to be resolved for the
5060 target type. */
5061 if (((sym->attr.flavor != FL_PROCEDURE
5062 || gfc_is_function_return_value (sym, gfc_current_ns))
5063 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
5065 (sym->assoc && sym->assoc->target
5066 && gfc_resolve_expr (sym->assoc->target)
5067 && (sym->assoc->target->ts.type == BT_DERIVED
5068 || sym->assoc->target->ts.type == BT_CLASS)))
5069 return match_typebound_call (st);
5071 /* If it does not seem to be callable (include functions so that the
5072 right association is made. They are thrown out in resolution.)
5073 ... */
5074 if (!sym->attr.generic
5075 && !sym->attr.proc_pointer
5076 && !sym->attr.subroutine
5077 && !sym->attr.function)
5079 if (!(sym->attr.external && !sym->attr.referenced))
5081 /* ...create a symbol in this scope... */
5082 if (sym->ns != gfc_current_ns
5083 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
5084 return MATCH_ERROR;
5086 if (sym != st->n.sym)
5087 sym = st->n.sym;
5090 /* ...and then to try to make the symbol into a subroutine. */
5091 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
5092 return MATCH_ERROR;
5095 gfc_set_sym_referenced (sym);
5097 if (gfc_match_eos () != MATCH_YES)
5099 m = gfc_match_actual_arglist (1, &arglist);
5100 if (m == MATCH_NO)
5101 goto syntax;
5102 if (m == MATCH_ERROR)
5103 goto cleanup;
5105 if (gfc_match_eos () != MATCH_YES)
5106 goto syntax;
5109 /* Walk the argument list looking for invalid BOZ. */
5110 for (a = arglist; a; a = a->next)
5111 if (a->expr && a->expr->ts.type == BT_BOZ)
5113 gfc_error ("A BOZ literal constant at %L cannot appear as an actual "
5114 "argument in a subroutine reference", &a->expr->where);
5115 goto cleanup;
5119 /* If any alternate return labels were found, construct a SELECT
5120 statement that will jump to the right place. */
5122 i = 0;
5123 for (a = arglist; a; a = a->next)
5124 if (a->expr == NULL)
5126 i = 1;
5127 break;
5130 if (i)
5132 gfc_symtree *select_st;
5133 gfc_symbol *select_sym;
5134 char name[GFC_MAX_SYMBOL_LEN + 1];
5136 new_st.next = c = gfc_get_code (EXEC_SELECT);
5137 sprintf (name, "_result_%s", sym->name);
5138 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
5140 select_sym = select_st->n.sym;
5141 select_sym->ts.type = BT_INTEGER;
5142 select_sym->ts.kind = gfc_default_integer_kind;
5143 gfc_set_sym_referenced (select_sym);
5144 c->expr1 = gfc_get_expr ();
5145 c->expr1->expr_type = EXPR_VARIABLE;
5146 c->expr1->symtree = select_st;
5147 c->expr1->ts = select_sym->ts;
5148 c->expr1->where = gfc_current_locus;
5150 i = 0;
5151 for (a = arglist; a; a = a->next)
5153 if (a->expr != NULL)
5154 continue;
5156 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
5157 continue;
5159 i++;
5161 c->block = gfc_get_code (EXEC_SELECT);
5162 c = c->block;
5164 new_case = gfc_get_case ();
5165 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
5166 new_case->low = new_case->high;
5167 c->ext.block.case_list = new_case;
5169 c->next = gfc_get_code (EXEC_GOTO);
5170 c->next->label1 = a->label;
5174 new_st.op = EXEC_CALL;
5175 new_st.symtree = st;
5176 new_st.ext.actual = arglist;
5178 return MATCH_YES;
5180 syntax:
5181 gfc_syntax_error (ST_CALL);
5183 cleanup:
5184 gfc_free_actual_arglist (arglist);
5185 return MATCH_ERROR;
5189 /* Given a name, return a pointer to the common head structure,
5190 creating it if it does not exist. If FROM_MODULE is nonzero, we
5191 mangle the name so that it doesn't interfere with commons defined
5192 in the using namespace.
5193 TODO: Add to global symbol tree. */
5195 gfc_common_head *
5196 gfc_get_common (const char *name, int from_module)
5198 gfc_symtree *st;
5199 static int serial = 0;
5200 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
5202 if (from_module)
5204 /* A use associated common block is only needed to correctly layout
5205 the variables it contains. */
5206 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
5207 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
5209 else
5211 st = gfc_find_symtree (gfc_current_ns->common_root, name);
5213 if (st == NULL)
5214 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
5217 if (st->n.common == NULL)
5219 st->n.common = gfc_get_common_head ();
5220 st->n.common->where = gfc_current_locus;
5221 strcpy (st->n.common->name, name);
5224 return st->n.common;
5228 /* Match a common block name. */
5230 match
5231 gfc_match_common_name (char *name)
5233 match m;
5235 if (gfc_match_char ('/') == MATCH_NO)
5237 name[0] = '\0';
5238 return MATCH_YES;
5241 if (gfc_match_char ('/') == MATCH_YES)
5243 name[0] = '\0';
5244 return MATCH_YES;
5247 m = gfc_match_name (name);
5249 if (m == MATCH_ERROR)
5250 return MATCH_ERROR;
5251 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
5252 return MATCH_YES;
5254 gfc_error ("Syntax error in common block name at %C");
5255 return MATCH_ERROR;
5259 /* Match a COMMON statement. */
5261 match
5262 gfc_match_common (void)
5264 gfc_symbol *sym, **head, *tail, *other;
5265 char name[GFC_MAX_SYMBOL_LEN + 1];
5266 gfc_common_head *t;
5267 gfc_array_spec *as;
5268 gfc_equiv *e1, *e2;
5269 match m;
5270 char c;
5272 /* COMMON has been matched. In free form source code, the next character
5273 needs to be whitespace or '/'. Check that here. Fixed form source
5274 code needs to be checked below. */
5275 c = gfc_peek_ascii_char ();
5276 if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '/')
5277 return MATCH_NO;
5279 as = NULL;
5281 for (;;)
5283 m = gfc_match_common_name (name);
5284 if (m == MATCH_ERROR)
5285 goto cleanup;
5287 if (name[0] == '\0')
5289 t = &gfc_current_ns->blank_common;
5290 if (t->head == NULL)
5291 t->where = gfc_current_locus;
5293 else
5295 t = gfc_get_common (name, 0);
5297 head = &t->head;
5299 if (*head == NULL)
5300 tail = NULL;
5301 else
5303 tail = *head;
5304 while (tail->common_next)
5305 tail = tail->common_next;
5308 /* Grab the list of symbols. */
5309 for (;;)
5311 m = gfc_match_symbol (&sym, 0);
5312 if (m == MATCH_ERROR)
5313 goto cleanup;
5314 if (m == MATCH_NO)
5315 goto syntax;
5317 /* See if we know the current common block is bind(c), and if
5318 so, then see if we can check if the symbol is (which it'll
5319 need to be). This can happen if the bind(c) attr stmt was
5320 applied to the common block, and the variable(s) already
5321 defined, before declaring the common block. */
5322 if (t->is_bind_c == 1)
5324 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
5326 /* If we find an error, just print it and continue,
5327 cause it's just semantic, and we can see if there
5328 are more errors. */
5329 gfc_error_now ("Variable %qs at %L in common block %qs "
5330 "at %C must be declared with a C "
5331 "interoperable kind since common block "
5332 "%qs is bind(c)",
5333 sym->name, &(sym->declared_at), t->name,
5334 t->name);
5337 if (sym->attr.is_bind_c == 1)
5338 gfc_error_now ("Variable %qs in common block %qs at %C cannot "
5339 "be bind(c) since it is not global", sym->name,
5340 t->name);
5343 if (sym->attr.in_common)
5345 gfc_error ("Symbol %qs at %C is already in a COMMON block",
5346 sym->name);
5347 goto cleanup;
5350 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
5351 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
5353 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
5354 "%C can only be COMMON in BLOCK DATA",
5355 sym->name))
5356 goto cleanup;
5359 /* F2018:R874: common-block-object is variable-name [ (array-spec) ]
5360 F2018:C8121: A variable-name shall not be a name made accessible
5361 by use association. */
5362 if (sym->attr.use_assoc)
5364 gfc_error ("Symbol %qs at %C is USE associated from module %qs "
5365 "and cannot occur in COMMON", sym->name, sym->module);
5366 goto cleanup;
5369 /* Deal with an optional array specification after the
5370 symbol name. */
5371 m = gfc_match_array_spec (&as, true, true);
5372 if (m == MATCH_ERROR)
5373 goto cleanup;
5375 if (m == MATCH_YES)
5377 if (as->type != AS_EXPLICIT)
5379 gfc_error ("Array specification for symbol %qs in COMMON "
5380 "at %C must be explicit", sym->name);
5381 goto cleanup;
5384 if (as->corank)
5386 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5387 "coarray", sym->name);
5388 goto cleanup;
5391 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
5392 goto cleanup;
5394 if (sym->attr.pointer)
5396 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5397 "POINTER array", sym->name);
5398 goto cleanup;
5401 sym->as = as;
5402 as = NULL;
5406 /* Add the in_common attribute, but ignore the reported errors
5407 if any, and continue matching. */
5408 gfc_add_in_common (&sym->attr, sym->name, NULL);
5410 sym->common_block = t;
5411 sym->common_block->refs++;
5413 if (tail != NULL)
5414 tail->common_next = sym;
5415 else
5416 *head = sym;
5418 tail = sym;
5420 sym->common_head = t;
5422 /* Check to see if the symbol is already in an equivalence group.
5423 If it is, set the other members as being in common. */
5424 if (sym->attr.in_equivalence)
5426 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
5428 for (e2 = e1; e2; e2 = e2->eq)
5429 if (e2->expr->symtree->n.sym == sym)
5430 goto equiv_found;
5432 continue;
5434 equiv_found:
5436 for (e2 = e1; e2; e2 = e2->eq)
5438 other = e2->expr->symtree->n.sym;
5439 if (other->common_head
5440 && other->common_head != sym->common_head)
5442 gfc_error ("Symbol %qs, in COMMON block %qs at "
5443 "%C is being indirectly equivalenced to "
5444 "another COMMON block %qs",
5445 sym->name, sym->common_head->name,
5446 other->common_head->name);
5447 goto cleanup;
5449 other->attr.in_common = 1;
5450 other->common_head = t;
5456 gfc_gobble_whitespace ();
5457 if (gfc_match_eos () == MATCH_YES)
5458 goto done;
5459 c = gfc_peek_ascii_char ();
5460 if (c == '/')
5461 break;
5462 if (c != ',')
5464 /* In Fixed form source code, gfortran can end up here for an
5465 expression of the form COMMONI = RHS. This may not be an
5466 error, so return MATCH_NO. */
5467 if (gfc_current_form == FORM_FIXED && c == '=')
5469 gfc_free_array_spec (as);
5470 return MATCH_NO;
5472 goto syntax;
5474 else
5475 gfc_match_char (',');
5477 gfc_gobble_whitespace ();
5478 if (gfc_peek_ascii_char () == '/')
5479 break;
5483 done:
5484 return MATCH_YES;
5486 syntax:
5487 gfc_syntax_error (ST_COMMON);
5489 cleanup:
5490 gfc_free_array_spec (as);
5491 return MATCH_ERROR;
5495 /* Match a BLOCK DATA program unit. */
5497 match
5498 gfc_match_block_data (void)
5500 char name[GFC_MAX_SYMBOL_LEN + 1];
5501 gfc_symbol *sym;
5502 match m;
5504 if (!gfc_notify_std (GFC_STD_F2018_OBS, "BLOCK DATA construct at %L",
5505 &gfc_current_locus))
5506 return MATCH_ERROR;
5508 if (gfc_match_eos () == MATCH_YES)
5510 gfc_new_block = NULL;
5511 return MATCH_YES;
5514 m = gfc_match ("% %n%t", name);
5515 if (m != MATCH_YES)
5516 return MATCH_ERROR;
5518 if (gfc_get_symbol (name, NULL, &sym))
5519 return MATCH_ERROR;
5521 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
5522 return MATCH_ERROR;
5524 gfc_new_block = sym;
5526 return MATCH_YES;
5530 /* Free a namelist structure. */
5532 void
5533 gfc_free_namelist (gfc_namelist *name)
5535 gfc_namelist *n;
5537 for (; name; name = n)
5539 n = name->next;
5540 free (name);
5545 /* Free an OpenMP namelist structure. */
5547 void
5548 gfc_free_omp_namelist (gfc_omp_namelist *name, bool free_ns,
5549 bool free_align_allocator,
5550 bool free_mem_traits_space, bool free_init)
5552 gfc_omp_namelist *n;
5553 gfc_expr *last_allocator = NULL;
5554 char *last_init_attr = NULL;
5556 for (; name; name = n)
5558 gfc_free_expr (name->expr);
5559 if (free_align_allocator)
5560 gfc_free_expr (name->u.align);
5561 else if (free_mem_traits_space)
5562 { } /* name->u.memspace_sym: shall not call gfc_free_symbol here. */
5564 if (free_ns)
5565 gfc_free_namespace (name->u2.ns);
5566 else if (free_align_allocator)
5568 if (last_allocator != name->u2.allocator)
5570 last_allocator = name->u2.allocator;
5571 gfc_free_expr (name->u2.allocator);
5574 else if (free_mem_traits_space)
5575 { } /* name->u2.traits_sym: shall not call gfc_free_symbol here. */
5576 else if (free_init)
5578 if (name->u.init.attr != last_init_attr)
5580 last_init_attr = name->u.init.attr;
5581 free (name->u.init.attr);
5582 free (name->u2.init_interop_fr);
5585 else if (name->u2.udr)
5587 if (name->u2.udr->combiner)
5588 gfc_free_statement (name->u2.udr->combiner);
5589 if (name->u2.udr->initializer)
5590 gfc_free_statement (name->u2.udr->initializer);
5591 free (name->u2.udr);
5593 n = name->next;
5594 free (name);
5599 /* Match a NAMELIST statement. */
5601 match
5602 gfc_match_namelist (void)
5604 gfc_symbol *group_name, *sym;
5605 gfc_namelist *nl;
5606 match m, m2;
5608 m = gfc_match (" / %s /", &group_name);
5609 if (m == MATCH_NO)
5610 goto syntax;
5611 if (m == MATCH_ERROR)
5612 goto error;
5614 for (;;)
5616 if (group_name->ts.type != BT_UNKNOWN)
5618 gfc_error ("Namelist group name %qs at %C already has a basic "
5619 "type of %s", group_name->name,
5620 gfc_typename (&group_name->ts));
5621 return MATCH_ERROR;
5624 /* A use associated name shall not be used as a namelist group name
5625 (e.g. F2003:C581). It is only supported as a legacy extension. */
5626 if (group_name->attr.flavor == FL_NAMELIST
5627 && group_name->attr.use_assoc
5628 && !gfc_notify_std (GFC_STD_LEGACY, "Namelist group name %qs "
5629 "at %C already is USE associated and can"
5630 "not be respecified.", group_name->name))
5631 return MATCH_ERROR;
5633 if (group_name->attr.flavor != FL_NAMELIST
5634 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
5635 group_name->name, NULL))
5636 return MATCH_ERROR;
5638 for (;;)
5640 m = gfc_match_symbol (&sym, 1);
5641 if (m == MATCH_NO)
5642 goto syntax;
5643 if (m == MATCH_ERROR)
5644 goto error;
5646 if (sym->ts.type == BT_UNKNOWN)
5648 if (gfc_current_ns->seen_implicit_none)
5650 /* It is required that members of a namelist be declared
5651 before the namelist. We check this by checking if the
5652 symbol has a defined type for IMPLICIT NONE. */
5653 gfc_error ("Symbol %qs in namelist %qs at %C must be "
5654 "declared before the namelist is declared.",
5655 sym->name, group_name->name);
5656 gfc_error_check ();
5658 else
5660 /* Before the symbol is given an implicit type, check to
5661 see if the symbol is already available in the namespace,
5662 possibly through host association. Importantly, the
5663 symbol may be a user defined type. */
5665 gfc_symbol *tmp;
5667 gfc_find_symbol (sym->name, NULL, 1, &tmp);
5668 if (tmp && tmp->attr.generic
5669 && (tmp = gfc_find_dt_in_generic (tmp)))
5671 if (tmp->attr.flavor == FL_DERIVED)
5673 gfc_error ("Derived type %qs at %L conflicts with "
5674 "namelist object %qs at %C",
5675 tmp->name, &tmp->declared_at, sym->name);
5676 goto error;
5680 /* Set type of the symbol to its implicit default type. It is
5681 not allowed to set it later to any other type. */
5682 gfc_set_default_type (sym, 0, gfc_current_ns);
5685 if (sym->attr.in_namelist == 0
5686 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
5687 goto error;
5689 /* Use gfc_error_check here, rather than goto error, so that
5690 these are the only errors for the next two lines. */
5691 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
5693 gfc_error ("Assumed size array %qs in namelist %qs at "
5694 "%C is not allowed", sym->name, group_name->name);
5695 gfc_error_check ();
5698 nl = gfc_get_namelist ();
5699 nl->sym = sym;
5700 sym->refs++;
5702 if (group_name->namelist == NULL)
5703 group_name->namelist = group_name->namelist_tail = nl;
5704 else
5706 group_name->namelist_tail->next = nl;
5707 group_name->namelist_tail = nl;
5710 if (gfc_match_eos () == MATCH_YES)
5711 goto done;
5713 m = gfc_match_char (',');
5715 if (gfc_match_char ('/') == MATCH_YES)
5717 m2 = gfc_match (" %s /", &group_name);
5718 if (m2 == MATCH_YES)
5719 break;
5720 if (m2 == MATCH_ERROR)
5721 goto error;
5722 goto syntax;
5725 if (m != MATCH_YES)
5726 goto syntax;
5730 done:
5731 return MATCH_YES;
5733 syntax:
5734 gfc_syntax_error (ST_NAMELIST);
5736 error:
5737 return MATCH_ERROR;
5741 /* Match a MODULE statement. */
5743 match
5744 gfc_match_module (void)
5746 match m;
5748 m = gfc_match (" %s%t", &gfc_new_block);
5749 if (m != MATCH_YES)
5750 return m;
5752 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
5753 gfc_new_block->name, NULL))
5754 return MATCH_ERROR;
5756 return MATCH_YES;
5760 /* Free equivalence sets and lists. Recursively is the easiest way to
5761 do this. */
5763 void
5764 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
5766 if (eq == stop)
5767 return;
5769 gfc_free_equiv (eq->eq);
5770 gfc_free_equiv_until (eq->next, stop);
5771 gfc_free_expr (eq->expr);
5772 free (eq);
5776 void
5777 gfc_free_equiv (gfc_equiv *eq)
5779 gfc_free_equiv_until (eq, NULL);
5783 /* Match an EQUIVALENCE statement. */
5785 match
5786 gfc_match_equivalence (void)
5788 gfc_equiv *eq, *set, *tail;
5789 gfc_ref *ref;
5790 gfc_symbol *sym;
5791 match m;
5792 gfc_common_head *common_head = NULL;
5793 bool common_flag;
5794 int cnt;
5795 char c;
5797 /* EQUIVALENCE has been matched. After gobbling any possible whitespace,
5798 the next character needs to be '('. Check that here, and return
5799 MATCH_NO for a variable of the form equivalence. */
5800 gfc_gobble_whitespace ();
5801 c = gfc_peek_ascii_char ();
5802 if (c != '(')
5803 return MATCH_NO;
5805 tail = NULL;
5807 for (;;)
5809 eq = gfc_get_equiv ();
5810 if (tail == NULL)
5811 tail = eq;
5813 eq->next = gfc_current_ns->equiv;
5814 gfc_current_ns->equiv = eq;
5816 if (gfc_match_char ('(') != MATCH_YES)
5817 goto syntax;
5819 set = eq;
5820 common_flag = false;
5821 cnt = 0;
5823 for (;;)
5825 m = gfc_match_equiv_variable (&set->expr);
5826 if (m == MATCH_ERROR)
5827 goto cleanup;
5828 if (m == MATCH_NO)
5829 goto syntax;
5831 /* count the number of objects. */
5832 cnt++;
5834 if (gfc_match_char ('%') == MATCH_YES)
5836 gfc_error ("Derived type component %C is not a "
5837 "permitted EQUIVALENCE member");
5838 goto cleanup;
5841 for (ref = set->expr->ref; ref; ref = ref->next)
5842 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
5844 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5845 "be an array section");
5846 goto cleanup;
5849 sym = set->expr->symtree->n.sym;
5851 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
5852 goto cleanup;
5853 if (sym->ts.type == BT_CLASS
5854 && CLASS_DATA (sym)
5855 && !gfc_add_in_equivalence (&CLASS_DATA (sym)->attr,
5856 sym->name, NULL))
5857 goto cleanup;
5859 if (sym->attr.in_common)
5861 common_flag = true;
5862 common_head = sym->common_head;
5865 if (gfc_match_char (')') == MATCH_YES)
5866 break;
5868 if (gfc_match_char (',') != MATCH_YES)
5869 goto syntax;
5871 set->eq = gfc_get_equiv ();
5872 set = set->eq;
5875 if (cnt < 2)
5877 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5878 goto cleanup;
5881 /* If one of the members of an equivalence is in common, then
5882 mark them all as being in common. Before doing this, check
5883 that members of the equivalence group are not in different
5884 common blocks. */
5885 if (common_flag)
5886 for (set = eq; set; set = set->eq)
5888 sym = set->expr->symtree->n.sym;
5889 if (sym->common_head && sym->common_head != common_head)
5891 gfc_error ("Attempt to indirectly overlap COMMON "
5892 "blocks %s and %s by EQUIVALENCE at %C",
5893 sym->common_head->name, common_head->name);
5894 goto cleanup;
5896 sym->attr.in_common = 1;
5897 sym->common_head = common_head;
5900 if (gfc_match_eos () == MATCH_YES)
5901 break;
5902 if (gfc_match_char (',') != MATCH_YES)
5904 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5905 goto cleanup;
5909 if (!gfc_notify_std (GFC_STD_F2018_OBS, "EQUIVALENCE statement at %C"))
5910 return MATCH_ERROR;
5912 return MATCH_YES;
5914 syntax:
5915 gfc_syntax_error (ST_EQUIVALENCE);
5917 cleanup:
5918 eq = tail->next;
5919 tail->next = NULL;
5921 gfc_free_equiv (gfc_current_ns->equiv);
5922 gfc_current_ns->equiv = eq;
5924 return MATCH_ERROR;
5928 /* Check that a statement function is not recursive. This is done by looking
5929 for the statement function symbol(sym) by looking recursively through its
5930 expression(e). If a reference to sym is found, true is returned.
5931 12.5.4 requires that any variable of function that is implicitly typed
5932 shall have that type confirmed by any subsequent type declaration. The
5933 implicit typing is conveniently done here. */
5934 static bool
5935 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
5937 static bool
5938 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5941 if (e == NULL)
5942 return false;
5944 switch (e->expr_type)
5946 case EXPR_FUNCTION:
5947 if (e->symtree == NULL)
5948 return false;
5950 /* Check the name before testing for nested recursion! */
5951 if (sym->name == e->symtree->n.sym->name)
5952 return true;
5954 /* Catch recursion via other statement functions. */
5955 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
5956 && e->symtree->n.sym->value
5957 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
5958 return true;
5960 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5961 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5963 break;
5965 case EXPR_VARIABLE:
5966 if (e->symtree && sym->name == e->symtree->n.sym->name)
5967 return true;
5969 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5970 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5971 break;
5973 default:
5974 break;
5977 return false;
5981 static bool
5982 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
5984 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
5988 /* Check for invalid uses of statement function dummy arguments in body. */
5990 static bool
5991 chk_stmt_fcn_body (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5993 gfc_formal_arglist *formal;
5995 if (e == NULL || e->symtree == NULL || e->expr_type != EXPR_FUNCTION)
5996 return false;
5998 for (formal = sym->formal; formal; formal = formal->next)
6000 if (formal->sym == e->symtree->n.sym)
6002 gfc_error ("Invalid use of statement function argument at %L",
6003 &e->where);
6004 return true;
6008 return false;
6012 /* Match a statement function declaration. It is so easy to match
6013 non-statement function statements with a MATCH_ERROR as opposed to
6014 MATCH_NO that we suppress error message in most cases. */
6016 match
6017 gfc_match_st_function (void)
6019 gfc_error_buffer old_error;
6020 gfc_symbol *sym;
6021 gfc_expr *expr;
6022 match m;
6023 char name[GFC_MAX_SYMBOL_LEN + 1];
6024 locus old_locus;
6025 bool fcn;
6026 gfc_formal_arglist *ptr;
6028 /* Read the possible statement function name, and then check to see if
6029 a symbol is already present in the namespace. Record if it is a
6030 function and whether it has been referenced. */
6031 fcn = false;
6032 ptr = NULL;
6033 old_locus = gfc_current_locus;
6034 m = gfc_match_name (name);
6035 if (m == MATCH_YES)
6037 gfc_find_symbol (name, NULL, 1, &sym);
6038 if (sym && sym->attr.function && !sym->attr.referenced)
6040 fcn = true;
6041 ptr = sym->formal;
6045 gfc_current_locus = old_locus;
6046 m = gfc_match_symbol (&sym, 0);
6047 if (m != MATCH_YES)
6048 return m;
6050 gfc_push_error (&old_error);
6052 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
6053 goto undo_error;
6055 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
6056 goto undo_error;
6058 m = gfc_match (" = %e%t", &expr);
6059 if (m == MATCH_NO)
6060 goto undo_error;
6062 gfc_free_error (&old_error);
6064 if (m == MATCH_ERROR)
6065 return m;
6067 if (recursive_stmt_fcn (expr, sym))
6069 gfc_error ("Statement function at %L is recursive", &expr->where);
6070 return MATCH_ERROR;
6073 if (fcn && ptr != sym->formal)
6075 gfc_error ("Statement function %qs at %L conflicts with function name",
6076 sym->name, &expr->where);
6077 return MATCH_ERROR;
6080 if (gfc_traverse_expr (expr, sym, chk_stmt_fcn_body, 0))
6081 return MATCH_ERROR;
6083 sym->value = expr;
6085 if ((gfc_current_state () == COMP_FUNCTION
6086 || gfc_current_state () == COMP_SUBROUTINE)
6087 && gfc_state_stack->previous->state == COMP_INTERFACE)
6089 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
6090 &expr->where);
6091 return MATCH_ERROR;
6094 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
6095 return MATCH_ERROR;
6097 return MATCH_YES;
6099 undo_error:
6100 gfc_pop_error (&old_error);
6101 return MATCH_NO;
6105 /* Match an assignment to a pointer function (F2008). This could, in
6106 general be ambiguous with a statement function. In this implementation
6107 it remains so if it is the first statement after the specification
6108 block. */
6110 match
6111 gfc_match_ptr_fcn_assign (void)
6113 gfc_error_buffer old_error;
6114 locus old_loc;
6115 gfc_symbol *sym;
6116 gfc_expr *expr;
6117 match m;
6118 char name[GFC_MAX_SYMBOL_LEN + 1];
6120 old_loc = gfc_current_locus;
6121 m = gfc_match_name (name);
6122 if (m != MATCH_YES)
6123 return m;
6125 gfc_find_symbol (name, NULL, 1, &sym);
6126 if (sym && sym->attr.flavor != FL_PROCEDURE)
6127 return MATCH_NO;
6129 gfc_push_error (&old_error);
6131 if (sym && sym->attr.function)
6132 goto match_actual_arglist;
6134 gfc_current_locus = old_loc;
6135 m = gfc_match_symbol (&sym, 0);
6136 if (m != MATCH_YES)
6137 return m;
6139 if (!gfc_add_procedure (&sym->attr, PROC_UNKNOWN, sym->name, NULL))
6140 goto undo_error;
6142 match_actual_arglist:
6143 gfc_current_locus = old_loc;
6144 m = gfc_match (" %e", &expr);
6145 if (m != MATCH_YES)
6146 goto undo_error;
6148 new_st.op = EXEC_ASSIGN;
6149 new_st.expr1 = expr;
6150 expr = NULL;
6152 m = gfc_match (" = %e%t", &expr);
6153 if (m != MATCH_YES)
6154 goto undo_error;
6156 new_st.expr2 = expr;
6157 return MATCH_YES;
6159 undo_error:
6160 gfc_pop_error (&old_error);
6161 return MATCH_NO;
6165 /***************** SELECT CASE subroutines ******************/
6167 /* Free a single case structure. */
6169 static void
6170 free_case (gfc_case *p)
6172 if (p->low == p->high)
6173 p->high = NULL;
6174 gfc_free_expr (p->low);
6175 gfc_free_expr (p->high);
6176 free (p);
6180 /* Free a list of case structures. */
6182 void
6183 gfc_free_case_list (gfc_case *p)
6185 gfc_case *q;
6187 for (; p; p = q)
6189 q = p->next;
6190 free_case (p);
6195 /* Match a single case selector. Combining the requirements of F08:C830
6196 and F08:C832 (R838) means that the case-value must have either CHARACTER,
6197 INTEGER, or LOGICAL type. */
6199 static match
6200 match_case_selector (gfc_case **cp)
6202 gfc_case *c;
6203 match m;
6205 c = gfc_get_case ();
6206 c->where = gfc_current_locus;
6208 if (gfc_match_char (':') == MATCH_YES)
6210 m = gfc_match_init_expr (&c->high);
6211 if (m == MATCH_NO)
6212 goto need_expr;
6213 if (m == MATCH_ERROR)
6214 goto cleanup;
6216 if (c->high->ts.type != BT_LOGICAL && c->high->ts.type != BT_INTEGER
6217 && c->high->ts.type != BT_CHARACTER
6218 && (!flag_unsigned
6219 || (flag_unsigned && c->high->ts.type != BT_UNSIGNED)))
6221 gfc_error ("Expression in CASE selector at %L cannot be %s",
6222 &c->high->where, gfc_typename (&c->high->ts));
6223 goto cleanup;
6226 else
6228 m = gfc_match_init_expr (&c->low);
6229 if (m == MATCH_ERROR)
6230 goto cleanup;
6231 if (m == MATCH_NO)
6232 goto need_expr;
6234 if (c->low->ts.type != BT_LOGICAL && c->low->ts.type != BT_INTEGER
6235 && c->low->ts.type != BT_CHARACTER
6236 && (!flag_unsigned
6237 || (flag_unsigned && c->low->ts.type != BT_UNSIGNED)))
6239 gfc_error ("Expression in CASE selector at %L cannot be %s",
6240 &c->low->where, gfc_typename (&c->low->ts));
6241 goto cleanup;
6244 /* If we're not looking at a ':' now, make a range out of a single
6245 target. Else get the upper bound for the case range. */
6246 if (gfc_match_char (':') != MATCH_YES)
6247 c->high = c->low;
6248 else
6250 m = gfc_match_init_expr (&c->high);
6251 if (m == MATCH_ERROR)
6252 goto cleanup;
6253 if (m == MATCH_YES
6254 && c->high->ts.type != BT_LOGICAL
6255 && c->high->ts.type != BT_INTEGER
6256 && c->high->ts.type != BT_CHARACTER
6257 && (!flag_unsigned
6258 || (flag_unsigned && c->high->ts.type != BT_UNSIGNED)))
6260 gfc_error ("Expression in CASE selector at %L cannot be %s",
6261 &c->high->where, gfc_typename (c->high));
6262 goto cleanup;
6264 /* MATCH_NO is fine. It's OK if nothing is there! */
6268 if (c->low && c->low->rank != 0)
6270 gfc_error ("Expression in CASE selector at %L must be scalar",
6271 &c->low->where);
6272 goto cleanup;
6274 if (c->high && c->high->rank != 0)
6276 gfc_error ("Expression in CASE selector at %L must be scalar",
6277 &c->high->where);
6278 goto cleanup;
6281 *cp = c;
6282 return MATCH_YES;
6284 need_expr:
6285 gfc_error ("Expected initialization expression in CASE at %C");
6287 cleanup:
6288 free_case (c);
6289 return MATCH_ERROR;
6293 /* Match the end of a case statement. */
6295 static match
6296 match_case_eos (void)
6298 char name[GFC_MAX_SYMBOL_LEN + 1];
6299 match m;
6301 if (gfc_match_eos () == MATCH_YES)
6302 return MATCH_YES;
6304 /* If the case construct doesn't have a case-construct-name, we
6305 should have matched the EOS. */
6306 if (!gfc_current_block ())
6307 return MATCH_NO;
6309 gfc_gobble_whitespace ();
6311 m = gfc_match_name (name);
6312 if (m != MATCH_YES)
6313 return m;
6315 if (strcmp (name, gfc_current_block ()->name) != 0)
6317 gfc_error ("Expected block name %qs of SELECT construct at %C",
6318 gfc_current_block ()->name);
6319 return MATCH_ERROR;
6322 return gfc_match_eos ();
6326 /* Match a SELECT statement. */
6328 match
6329 gfc_match_select (void)
6331 gfc_expr *expr;
6332 match m;
6334 m = gfc_match_label ();
6335 if (m == MATCH_ERROR)
6336 return m;
6338 m = gfc_match (" select case ( %e )%t", &expr);
6339 if (m != MATCH_YES)
6340 return m;
6342 new_st.op = EXEC_SELECT;
6343 new_st.expr1 = expr;
6345 return MATCH_YES;
6349 /* Transfer the selector typespec to the associate name. */
6351 static void
6352 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector,
6353 bool select_type = false)
6355 gfc_ref *ref;
6356 gfc_symbol *assoc_sym;
6357 int rank = 0, corank = 0;
6359 assoc_sym = associate->symtree->n.sym;
6361 /* At this stage the expression rank and arrayspec dimensions have
6362 not been completely sorted out. We must get the expr2->rank
6363 right here, so that the correct class container is obtained. */
6364 ref = selector->ref;
6365 while (ref && ref->next)
6366 ref = ref->next;
6368 if (selector->ts.type == BT_CLASS
6369 && CLASS_DATA (selector)
6370 && CLASS_DATA (selector)->as
6371 && CLASS_DATA (selector)->as->type == AS_ASSUMED_RANK)
6373 assoc_sym->attr.dimension = 1;
6374 assoc_sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6375 corank = assoc_sym->as->corank;
6376 goto build_class_sym;
6378 else if (selector->ts.type == BT_CLASS
6379 && CLASS_DATA (selector)
6380 && CLASS_DATA (selector)->as
6381 && ((ref && ref->type == REF_ARRAY)
6382 || selector->expr_type == EXPR_OP))
6384 /* Ensure that the array reference type is set. We cannot use
6385 gfc_resolve_expr at this point, so the usable parts of
6386 resolve.cc(resolve_array_ref) are employed to do it. */
6387 if (ref && ref->u.ar.type == AR_UNKNOWN)
6389 ref->u.ar.type = AR_ELEMENT;
6390 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
6391 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
6392 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
6393 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6394 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
6396 ref->u.ar.type = AR_SECTION;
6397 break;
6401 if (!ref || ref->u.ar.type == AR_FULL)
6403 selector->rank = CLASS_DATA (selector)->as->rank;
6404 selector->corank = CLASS_DATA (selector)->as->corank;
6406 else if (ref->u.ar.type == AR_SECTION)
6408 selector->rank = ref->u.ar.dimen;
6409 selector->corank = ref->u.ar.codimen;
6411 else
6412 selector->rank = 0;
6414 rank = selector->rank;
6415 corank = selector->corank;
6418 if (rank)
6420 if (ref)
6422 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
6423 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT
6424 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6425 && ref->u.ar.end[i] == NULL
6426 && ref->u.ar.stride[i] == NULL))
6427 rank--;
6430 if (rank)
6432 assoc_sym->attr.dimension = 1;
6433 assoc_sym->as = gfc_get_array_spec ();
6434 assoc_sym->as->rank = rank;
6435 assoc_sym->as->type = AS_DEFERRED;
6439 if (corank != 0 && rank == 0)
6441 if (!assoc_sym->as)
6442 assoc_sym->as = gfc_get_array_spec ();
6443 assoc_sym->as->corank = corank;
6444 assoc_sym->attr.codimension = 1;
6446 else if (corank == 0 && rank == 0 && assoc_sym->as)
6448 free (assoc_sym->as);
6449 assoc_sym->as = NULL;
6451 build_class_sym:
6452 /* Deal with the very specific case of a SELECT_TYPE selector being an
6453 associate_name whose type has been identified by component references.
6454 It must be assumed that it will be identified as a CLASS expression,
6455 so convert it now. */
6456 if (select_type
6457 && IS_INFERRED_TYPE (selector)
6458 && selector->ts.type == BT_DERIVED)
6460 gfc_find_derived_vtab (selector->ts.u.derived);
6461 /* The correct class container has to be available. */
6462 assoc_sym->ts.u.derived = selector->ts.u.derived;
6463 assoc_sym->ts.type = BT_CLASS;
6464 assoc_sym->attr.pointer = 1;
6465 if (!selector->ts.u.derived->attr.is_class)
6466 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
6467 associate->ts = assoc_sym->ts;
6469 else if (selector->ts.type == BT_CLASS)
6471 /* The correct class container has to be available. */
6472 assoc_sym->ts.type = BT_CLASS;
6473 assoc_sym->ts.u.derived = CLASS_DATA (selector)
6474 ? CLASS_DATA (selector)->ts.u.derived
6475 : selector->ts.u.derived;
6476 assoc_sym->attr.pointer = 1;
6477 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
6482 /* Build the associate name */
6483 static int
6484 build_associate_name (const char *name, gfc_expr **e1, gfc_expr **e2)
6486 gfc_expr *expr1 = *e1;
6487 gfc_expr *expr2 = *e2;
6488 gfc_symbol *sym;
6490 /* For the case where the associate name is already an associate name. */
6491 if (!expr2)
6492 expr2 = expr1;
6493 expr1 = gfc_get_expr ();
6494 expr1->expr_type = EXPR_VARIABLE;
6495 expr1->where = expr2->where;
6496 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
6497 return 1;
6499 sym = expr1->symtree->n.sym;
6500 if (expr2->ts.type == BT_UNKNOWN)
6501 sym->attr.untyped = 1;
6502 else
6503 copy_ts_from_selector_to_associate (expr1, expr2, true);
6505 sym->attr.flavor = FL_VARIABLE;
6506 sym->attr.referenced = 1;
6507 sym->attr.class_ok = 1;
6509 *e1 = expr1;
6510 *e2 = expr2;
6511 return 0;
6515 /* Push the current selector onto the SELECT TYPE stack. */
6517 static void
6518 select_type_push (gfc_symbol *sel)
6520 gfc_select_type_stack *top = gfc_get_select_type_stack ();
6521 top->selector = sel;
6522 top->tmp = NULL;
6523 top->prev = select_type_stack;
6525 select_type_stack = top;
6529 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
6531 static gfc_symtree *
6532 select_intrinsic_set_tmp (gfc_typespec *ts)
6534 char name[GFC_MAX_SYMBOL_LEN];
6535 gfc_symtree *tmp;
6536 HOST_WIDE_INT charlen = 0;
6537 gfc_symbol *selector = select_type_stack->selector;
6538 gfc_symbol *sym;
6540 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
6541 return NULL;
6543 if (selector->ts.type == BT_CLASS && !selector->attr.class_ok)
6544 return NULL;
6546 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6547 the values correspond to SELECT rank cases. */
6548 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
6549 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6550 charlen = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
6552 if (ts->type != BT_CHARACTER)
6553 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
6554 ts->kind);
6555 else
6556 snprintf (name, sizeof (name),
6557 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
6558 gfc_basic_typename (ts->type), charlen, ts->kind);
6560 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6561 sym = tmp->n.sym;
6562 gfc_add_type (sym, ts, NULL);
6564 /* Copy across the array spec to the selector. */
6565 if (selector->ts.type == BT_CLASS
6566 && (CLASS_DATA (selector)->attr.dimension
6567 || CLASS_DATA (selector)->attr.codimension))
6569 sym->attr.pointer = 1;
6570 sym->attr.dimension = CLASS_DATA (selector)->attr.dimension;
6571 sym->attr.codimension = CLASS_DATA (selector)->attr.codimension;
6572 sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6575 gfc_set_sym_referenced (sym);
6576 gfc_add_flavor (&sym->attr, FL_VARIABLE, name, NULL);
6577 sym->attr.select_type_temporary = 1;
6579 return tmp;
6583 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
6585 static void
6586 select_type_set_tmp (gfc_typespec *ts)
6588 char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
6589 gfc_symtree *tmp = NULL;
6590 gfc_symbol *selector = select_type_stack->selector;
6591 gfc_symbol *sym;
6592 gfc_expr *expr2;
6594 if (!ts)
6596 select_type_stack->tmp = NULL;
6597 return;
6600 tmp = select_intrinsic_set_tmp (ts);
6602 if (tmp == NULL)
6604 if (!ts->u.derived)
6605 return;
6607 if (ts->type == BT_CLASS)
6608 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
6609 else
6610 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
6612 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6613 sym = tmp->n.sym;
6614 gfc_add_type (sym, ts, NULL);
6616 /* If the SELECT TYPE selector is a function we might be able to obtain
6617 a typespec from the result. Since the function might not have been
6618 parsed yet we have to check that there is indeed a result symbol. */
6619 if (selector->ts.type == BT_UNKNOWN
6620 && gfc_state_stack->construct
6622 && (expr2 = gfc_state_stack->construct->expr2)
6623 && expr2->expr_type == EXPR_FUNCTION
6624 && expr2->symtree
6625 && expr2->symtree->n.sym && expr2->symtree->n.sym->result)
6626 selector->ts = expr2->symtree->n.sym->result->ts;
6628 if (selector->ts.type == BT_CLASS
6629 && selector->attr.class_ok
6630 && selector->ts.u.derived && CLASS_DATA (selector))
6632 sym->attr.pointer
6633 = CLASS_DATA (selector)->attr.class_pointer;
6635 /* Copy across the array spec to the selector. */
6636 if (CLASS_DATA (selector)->attr.dimension
6637 || CLASS_DATA (selector)->attr.codimension)
6639 sym->attr.dimension
6640 = CLASS_DATA (selector)->attr.dimension;
6641 sym->attr.codimension
6642 = CLASS_DATA (selector)->attr.codimension;
6643 if (CLASS_DATA (selector)->as->type != AS_EXPLICIT)
6644 sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6645 else
6647 sym->as = gfc_get_array_spec();
6648 sym->as->rank = CLASS_DATA (selector)->as->rank;
6649 sym->as->type = AS_DEFERRED;
6654 gfc_set_sym_referenced (sym);
6655 gfc_add_flavor (&sym->attr, FL_VARIABLE, name, NULL);
6656 sym->attr.select_type_temporary = 1;
6658 if (ts->type == BT_CLASS)
6659 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
6661 else
6662 sym = tmp->n.sym;
6665 /* Add an association for it, so the rest of the parser knows it is
6666 an associate-name. The target will be set during resolution. */
6667 sym->assoc = gfc_get_association_list ();
6668 sym->assoc->dangling = 1;
6669 sym->assoc->st = tmp;
6671 select_type_stack->tmp = tmp;
6675 /* Match a SELECT TYPE statement. */
6677 match
6678 gfc_match_select_type (void)
6680 gfc_expr *expr1, *expr2 = NULL;
6681 match m;
6682 char name[GFC_MAX_SYMBOL_LEN + 1];
6683 bool class_array;
6684 gfc_namespace *ns = gfc_current_ns;
6686 m = gfc_match_label ();
6687 if (m == MATCH_ERROR)
6688 return m;
6690 m = gfc_match (" select type ( ");
6691 if (m != MATCH_YES)
6692 return m;
6694 if (gfc_current_state() == COMP_MODULE
6695 || gfc_current_state() == COMP_SUBMODULE)
6697 gfc_error ("SELECT TYPE at %C cannot appear in this scope");
6698 return MATCH_ERROR;
6701 gfc_current_ns = gfc_build_block_ns (ns);
6702 m = gfc_match (" %n => %e", name, &expr2);
6703 if (m == MATCH_YES)
6705 if (build_associate_name (name, &expr1, &expr2))
6707 m = MATCH_ERROR;
6708 goto cleanup;
6711 else
6713 m = gfc_match (" %e ", &expr1);
6714 if (m != MATCH_YES)
6716 std::swap (ns, gfc_current_ns);
6717 gfc_free_namespace (ns);
6718 return m;
6722 m = gfc_match (" )%t");
6723 if (m != MATCH_YES)
6725 gfc_error ("parse error in SELECT TYPE statement at %C");
6726 goto cleanup;
6729 /* This ghastly expression seems to be needed to distinguish a CLASS
6730 array, which can have a reference, from other expressions that
6731 have references, such as derived type components, and are not
6732 allowed by the standard.
6733 TODO: see if it is sufficient to exclude component and substring
6734 references. */
6735 class_array = (expr1->expr_type == EXPR_VARIABLE
6736 && expr1->ts.type == BT_CLASS
6737 && CLASS_DATA (expr1)
6738 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
6739 && (CLASS_DATA (expr1)->attr.dimension
6740 || CLASS_DATA (expr1)->attr.codimension)
6741 && expr1->ref
6742 && expr1->ref->type == REF_ARRAY
6743 && expr1->ref->u.ar.type == AR_FULL
6744 && expr1->ref->next == NULL);
6746 /* Check for F03:C811 (F08:C835). */
6747 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
6748 || (!class_array && expr1->ref != NULL)))
6750 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
6751 "use associate-name=>");
6752 m = MATCH_ERROR;
6753 goto cleanup;
6756 /* Prevent an existing associate name from reuse here by pushing expr1 to
6757 expr2 and building a new associate name. */
6758 if (!expr2 && expr1->symtree->n.sym->assoc
6759 && !expr1->symtree->n.sym->attr.select_type_temporary
6760 && !expr1->symtree->n.sym->attr.select_rank_temporary
6761 && build_associate_name (expr1->symtree->n.sym->name, &expr1, &expr2))
6763 m = MATCH_ERROR;
6764 goto cleanup;
6767 /* Select type namespaces are not filled until resolution. Therefore, the
6768 namespace must be marked as having an inferred type associate name if
6769 either expr1 is an inferred type variable or expr2 is. In the latter
6770 case, as well as the symbol being marked as inferred type, it might be
6771 that it has not been detected to be so. In this case the target has
6772 unknown type. Once the namespace is marked, the fixups in resolution can
6773 be triggered. */
6774 if (!expr2
6775 && expr1->symtree->n.sym->assoc
6776 && expr1->symtree->n.sym->assoc->inferred_type)
6777 gfc_current_ns->assoc_name_inferred = 1;
6778 else if (expr2 && expr2->expr_type == EXPR_VARIABLE
6779 && expr2->symtree->n.sym->assoc)
6781 if (expr2->symtree->n.sym->assoc->inferred_type)
6782 gfc_current_ns->assoc_name_inferred = 1;
6783 else if (expr2->symtree->n.sym->assoc->target
6784 && expr2->symtree->n.sym->assoc->target->ts.type == BT_UNKNOWN)
6785 gfc_current_ns->assoc_name_inferred = 1;
6788 new_st.op = EXEC_SELECT_TYPE;
6789 new_st.expr1 = expr1;
6790 new_st.expr2 = expr2;
6791 new_st.ext.block.ns = gfc_current_ns;
6793 select_type_push (expr1->symtree->n.sym);
6794 gfc_current_ns = ns;
6796 return MATCH_YES;
6798 cleanup:
6799 gfc_free_expr (expr1);
6800 gfc_free_expr (expr2);
6801 gfc_undo_symbols ();
6802 std::swap (ns, gfc_current_ns);
6803 gfc_free_namespace (ns);
6804 return m;
6808 /* Set the temporary for the current intrinsic SELECT RANK selector. */
6810 static void
6811 select_rank_set_tmp (gfc_typespec *ts, int *case_value)
6813 char name[2 * GFC_MAX_SYMBOL_LEN];
6814 char tname[GFC_MAX_SYMBOL_LEN + 7];
6815 gfc_symtree *tmp;
6816 gfc_symbol *selector = select_type_stack->selector;
6817 gfc_symbol *sym;
6818 gfc_symtree *st;
6819 HOST_WIDE_INT charlen = 0;
6821 if (case_value == NULL)
6822 return;
6824 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
6825 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6826 charlen = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
6828 if (ts->type == BT_CLASS)
6829 sprintf (tname, "class_%s", ts->u.derived->name);
6830 else if (ts->type == BT_DERIVED)
6831 sprintf (tname, "type_%s", ts->u.derived->name);
6832 else if (ts->type != BT_CHARACTER)
6833 sprintf (tname, "%s_%d", gfc_basic_typename (ts->type), ts->kind);
6834 else
6835 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
6836 gfc_basic_typename (ts->type), charlen, ts->kind);
6838 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6839 the values correspond to SELECT rank cases. */
6840 if (*case_value >=0)
6841 sprintf (name, "__tmp_%s_rank_%d", tname, *case_value);
6842 else
6843 sprintf (name, "__tmp_%s_rank_m%d", tname, -*case_value);
6845 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
6846 if (st)
6847 return;
6849 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6850 sym = tmp->n.sym;
6851 gfc_add_type (sym, ts, NULL);
6853 /* Copy across the array spec to the selector. */
6854 if (selector->ts.type == BT_CLASS)
6856 sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
6857 sym->attr.pointer = CLASS_DATA (selector)->attr.pointer;
6858 sym->attr.allocatable = CLASS_DATA (selector)->attr.allocatable;
6859 sym->attr.target = CLASS_DATA (selector)->attr.target;
6860 sym->attr.class_ok = 0;
6861 if (case_value && *case_value != 0)
6863 sym->attr.dimension = 1;
6864 sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6865 if (*case_value > 0)
6867 sym->as->type = AS_DEFERRED;
6868 sym->as->rank = *case_value;
6870 else if (*case_value == -1)
6872 sym->as->type = AS_ASSUMED_SIZE;
6873 sym->as->rank = 1;
6877 else
6879 sym->attr.pointer = selector->attr.pointer;
6880 sym->attr.allocatable = selector->attr.allocatable;
6881 sym->attr.target = selector->attr.target;
6882 if (case_value && *case_value != 0)
6884 sym->attr.dimension = 1;
6885 sym->as = gfc_copy_array_spec (selector->as);
6886 if (*case_value > 0)
6888 sym->as->type = AS_DEFERRED;
6889 sym->as->rank = *case_value;
6891 else if (*case_value == -1)
6893 sym->as->type = AS_ASSUMED_SIZE;
6894 sym->as->rank = 1;
6899 gfc_set_sym_referenced (sym);
6900 gfc_add_flavor (&sym->attr, FL_VARIABLE, name, NULL);
6901 sym->attr.select_type_temporary = 1;
6902 if (case_value)
6903 sym->attr.select_rank_temporary = 1;
6905 if (ts->type == BT_CLASS)
6906 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
6908 /* Add an association for it, so the rest of the parser knows it is
6909 an associate-name. The target will be set during resolution. */
6910 sym->assoc = gfc_get_association_list ();
6911 sym->assoc->dangling = 1;
6912 sym->assoc->st = tmp;
6914 select_type_stack->tmp = tmp;
6918 /* Match a SELECT RANK statement. */
6920 match
6921 gfc_match_select_rank (void)
6923 gfc_expr *expr1, *expr2 = NULL;
6924 match m;
6925 char name[GFC_MAX_SYMBOL_LEN + 1];
6926 gfc_symbol *sym, *sym2;
6927 gfc_namespace *ns = gfc_current_ns;
6928 gfc_array_spec *as = NULL;
6930 m = gfc_match_label ();
6931 if (m == MATCH_ERROR)
6932 return m;
6934 m = gfc_match (" select% rank ( ");
6935 if (m != MATCH_YES)
6936 return m;
6938 if (!gfc_notify_std (GFC_STD_F2018, "SELECT RANK statement at %C"))
6939 return MATCH_NO;
6941 gfc_current_ns = gfc_build_block_ns (ns);
6942 m = gfc_match (" %n => %e", name, &expr2);
6944 if (m == MATCH_YES)
6946 /* If expr2 corresponds to an implicitly typed variable, then the
6947 actual type of the variable may not have been set. Set it here. */
6948 if (!gfc_current_ns->seen_implicit_none
6949 && expr2->expr_type == EXPR_VARIABLE
6950 && expr2->ts.type == BT_UNKNOWN
6951 && expr2->symtree && expr2->symtree->n.sym)
6953 gfc_set_default_type (expr2->symtree->n.sym, 0, gfc_current_ns);
6954 expr2->ts.type = expr2->symtree->n.sym->ts.type;
6957 expr1 = gfc_get_expr ();
6958 expr1->expr_type = EXPR_VARIABLE;
6959 expr1->where = expr2->where;
6960 expr1->ref = gfc_copy_ref (expr2->ref);
6961 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
6963 m = MATCH_ERROR;
6964 goto cleanup;
6967 sym = expr1->symtree->n.sym;
6969 if (expr2->symtree)
6971 sym2 = expr2->symtree->n.sym;
6972 as = (sym2->ts.type == BT_CLASS
6973 && CLASS_DATA (sym2)) ? CLASS_DATA (sym2)->as : sym2->as;
6976 if (expr2->expr_type != EXPR_VARIABLE
6977 || !(as && as->type == AS_ASSUMED_RANK))
6979 gfc_error ("The SELECT RANK selector at %C must be an assumed "
6980 "rank variable");
6981 m = MATCH_ERROR;
6982 goto cleanup;
6985 if (expr2->ts.type == BT_CLASS && CLASS_DATA (sym2))
6987 copy_ts_from_selector_to_associate (expr1, expr2);
6989 sym->attr.flavor = FL_VARIABLE;
6990 sym->attr.referenced = 1;
6991 sym->attr.class_ok = 1;
6992 CLASS_DATA (sym)->attr.allocatable = CLASS_DATA (sym2)->attr.allocatable;
6993 CLASS_DATA (sym)->attr.pointer = CLASS_DATA (sym2)->attr.pointer;
6994 CLASS_DATA (sym)->attr.target = CLASS_DATA (sym2)->attr.target;
6995 sym->attr.pointer = 1;
6997 else
6999 sym->ts = sym2->ts;
7000 sym->as = gfc_copy_array_spec (sym2->as);
7001 sym->attr.dimension = 1;
7003 sym->attr.flavor = FL_VARIABLE;
7004 sym->attr.referenced = 1;
7005 sym->attr.class_ok = sym2->attr.class_ok;
7006 sym->attr.allocatable = sym2->attr.allocatable;
7007 sym->attr.pointer = sym2->attr.pointer;
7008 sym->attr.target = sym2->attr.target;
7011 else
7013 m = gfc_match (" %e ", &expr1);
7015 if (m != MATCH_YES)
7017 gfc_undo_symbols ();
7018 std::swap (ns, gfc_current_ns);
7019 gfc_free_namespace (ns);
7020 return m;
7023 if (expr1->symtree)
7025 sym = expr1->symtree->n.sym;
7026 as = (sym->ts.type == BT_CLASS
7027 && CLASS_DATA (sym)) ? CLASS_DATA (sym)->as : sym->as;
7030 if (expr1->expr_type != EXPR_VARIABLE
7031 || !(as && as->type == AS_ASSUMED_RANK))
7033 gfc_error("The SELECT RANK selector at %C must be an assumed "
7034 "rank variable");
7035 m = MATCH_ERROR;
7036 goto cleanup;
7040 m = gfc_match (" )%t");
7041 if (m != MATCH_YES)
7043 gfc_error ("parse error in SELECT RANK statement at %C");
7044 goto cleanup;
7047 new_st.op = EXEC_SELECT_RANK;
7048 new_st.expr1 = expr1;
7049 new_st.expr2 = expr2;
7050 new_st.ext.block.ns = gfc_current_ns;
7052 select_type_push (expr1->symtree->n.sym);
7053 gfc_current_ns = ns;
7055 return MATCH_YES;
7057 cleanup:
7058 gfc_free_expr (expr1);
7059 gfc_free_expr (expr2);
7060 gfc_undo_symbols ();
7061 std::swap (ns, gfc_current_ns);
7062 gfc_free_namespace (ns);
7063 return m;
7067 /* Match a CASE statement. */
7069 match
7070 gfc_match_case (void)
7072 gfc_case *c, *head, *tail;
7073 match m;
7075 head = tail = NULL;
7077 if (gfc_current_state () != COMP_SELECT)
7079 gfc_error ("Unexpected CASE statement at %C");
7080 return MATCH_ERROR;
7083 if (gfc_match ("% default") == MATCH_YES)
7085 m = match_case_eos ();
7086 if (m == MATCH_NO)
7087 goto syntax;
7088 if (m == MATCH_ERROR)
7089 goto cleanup;
7091 new_st.op = EXEC_SELECT;
7092 c = gfc_get_case ();
7093 c->where = gfc_current_locus;
7094 new_st.ext.block.case_list = c;
7095 return MATCH_YES;
7098 if (gfc_match_char ('(') != MATCH_YES)
7099 goto syntax;
7101 for (;;)
7103 if (match_case_selector (&c) == MATCH_ERROR)
7104 goto cleanup;
7106 if (head == NULL)
7107 head = c;
7108 else
7109 tail->next = c;
7111 tail = c;
7113 if (gfc_match_char (')') == MATCH_YES)
7114 break;
7115 if (gfc_match_char (',') != MATCH_YES)
7116 goto syntax;
7119 m = match_case_eos ();
7120 if (m == MATCH_NO)
7121 goto syntax;
7122 if (m == MATCH_ERROR)
7123 goto cleanup;
7125 new_st.op = EXEC_SELECT;
7126 new_st.ext.block.case_list = head;
7128 return MATCH_YES;
7130 syntax:
7131 gfc_error ("Syntax error in CASE specification at %C");
7133 cleanup:
7134 gfc_free_case_list (head); /* new_st is cleaned up in parse.cc. */
7135 return MATCH_ERROR;
7139 /* Match a TYPE IS statement. */
7141 match
7142 gfc_match_type_is (void)
7144 gfc_case *c = NULL;
7145 match m;
7147 if (gfc_current_state () != COMP_SELECT_TYPE)
7149 gfc_error ("Unexpected TYPE IS statement at %C");
7150 return MATCH_ERROR;
7153 if (gfc_match_char ('(') != MATCH_YES)
7154 goto syntax;
7156 c = gfc_get_case ();
7157 c->where = gfc_current_locus;
7159 m = gfc_match_type_spec (&c->ts);
7160 if (m == MATCH_NO)
7161 goto syntax;
7162 if (m == MATCH_ERROR)
7163 goto cleanup;
7165 if (gfc_match_char (')') != MATCH_YES)
7166 goto syntax;
7168 m = match_case_eos ();
7169 if (m == MATCH_NO)
7170 goto syntax;
7171 if (m == MATCH_ERROR)
7172 goto cleanup;
7174 new_st.op = EXEC_SELECT_TYPE;
7175 new_st.ext.block.case_list = c;
7177 if (c->ts.type == BT_DERIVED && c->ts.u.derived
7178 && (c->ts.u.derived->attr.sequence
7179 || c->ts.u.derived->attr.is_bind_c))
7181 gfc_error ("The type-spec shall not specify a sequence derived "
7182 "type or a type with the BIND attribute in SELECT "
7183 "TYPE at %C [F2003:C815]");
7184 return MATCH_ERROR;
7187 if (c->ts.type == BT_DERIVED
7188 && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
7189 && gfc_spec_list_type (type_param_spec_list, c->ts.u.derived)
7190 != SPEC_ASSUMED)
7192 gfc_error ("All the LEN type parameters in the TYPE IS statement "
7193 "at %C must be ASSUMED");
7194 return MATCH_ERROR;
7197 /* Create temporary variable. */
7198 select_type_set_tmp (&c->ts);
7200 return MATCH_YES;
7202 syntax:
7203 gfc_error ("Syntax error in TYPE IS specification at %C");
7205 cleanup:
7206 if (c != NULL)
7207 gfc_free_case_list (c); /* new_st is cleaned up in parse.cc. */
7208 return MATCH_ERROR;
7212 /* Match a CLASS IS or CLASS DEFAULT statement. */
7214 match
7215 gfc_match_class_is (void)
7217 gfc_case *c = NULL;
7218 match m;
7220 if (gfc_current_state () != COMP_SELECT_TYPE)
7221 return MATCH_NO;
7223 if (gfc_match ("% default") == MATCH_YES)
7225 m = match_case_eos ();
7226 if (m == MATCH_NO)
7227 goto syntax;
7228 if (m == MATCH_ERROR)
7229 goto cleanup;
7231 new_st.op = EXEC_SELECT_TYPE;
7232 c = gfc_get_case ();
7233 c->where = gfc_current_locus;
7234 c->ts.type = BT_UNKNOWN;
7235 new_st.ext.block.case_list = c;
7236 select_type_set_tmp (NULL);
7237 return MATCH_YES;
7240 m = gfc_match ("% is");
7241 if (m == MATCH_NO)
7242 goto syntax;
7243 if (m == MATCH_ERROR)
7244 goto cleanup;
7246 if (gfc_match_char ('(') != MATCH_YES)
7247 goto syntax;
7249 c = gfc_get_case ();
7250 c->where = gfc_current_locus;
7252 m = match_derived_type_spec (&c->ts);
7253 if (m == MATCH_NO)
7254 goto syntax;
7255 if (m == MATCH_ERROR)
7256 goto cleanup;
7258 if (c->ts.type == BT_DERIVED)
7259 c->ts.type = BT_CLASS;
7261 if (gfc_match_char (')') != MATCH_YES)
7262 goto syntax;
7264 m = match_case_eos ();
7265 if (m == MATCH_NO)
7266 goto syntax;
7267 if (m == MATCH_ERROR)
7268 goto cleanup;
7270 new_st.op = EXEC_SELECT_TYPE;
7271 new_st.ext.block.case_list = c;
7273 /* Create temporary variable. */
7274 select_type_set_tmp (&c->ts);
7276 return MATCH_YES;
7278 syntax:
7279 gfc_error ("Syntax error in CLASS IS specification at %C");
7281 cleanup:
7282 if (c != NULL)
7283 gfc_free_case_list (c); /* new_st is cleaned up in parse.cc. */
7284 return MATCH_ERROR;
7288 /* Match a RANK statement. */
7290 match
7291 gfc_match_rank_is (void)
7293 gfc_case *c = NULL;
7294 match m;
7295 int case_value;
7297 if (gfc_current_state () != COMP_SELECT_RANK)
7299 gfc_error ("Unexpected RANK statement at %C");
7300 return MATCH_ERROR;
7303 if (gfc_match ("% default") == MATCH_YES)
7305 m = match_case_eos ();
7306 if (m == MATCH_NO)
7307 goto syntax;
7308 if (m == MATCH_ERROR)
7309 goto cleanup;
7311 new_st.op = EXEC_SELECT_RANK;
7312 c = gfc_get_case ();
7313 c->ts.type = BT_UNKNOWN;
7314 c->where = gfc_current_locus;
7315 new_st.ext.block.case_list = c;
7316 select_type_stack->tmp = NULL;
7317 return MATCH_YES;
7320 if (gfc_match_char ('(') != MATCH_YES)
7321 goto syntax;
7323 c = gfc_get_case ();
7324 c->where = gfc_current_locus;
7325 c->ts = select_type_stack->selector->ts;
7327 m = gfc_match_expr (&c->low);
7328 if (m == MATCH_NO)
7330 if (gfc_match_char ('*') == MATCH_YES)
7331 c->low = gfc_get_int_expr (gfc_default_integer_kind,
7332 NULL, -1);
7333 else
7334 goto syntax;
7336 case_value = -1;
7338 else if (m == MATCH_YES)
7340 /* F2018: R1150 */
7341 if (c->low->expr_type != EXPR_CONSTANT
7342 || c->low->ts.type != BT_INTEGER
7343 || c->low->rank)
7345 gfc_error ("The SELECT RANK CASE expression at %C must be a "
7346 "scalar, integer constant");
7347 goto cleanup;
7350 case_value = (int) mpz_get_si (c->low->value.integer);
7351 /* F2018: C1151 */
7352 if ((case_value < 0) || (case_value > GFC_MAX_DIMENSIONS))
7354 gfc_error ("The value of the SELECT RANK CASE expression at "
7355 "%C must not be less than zero or greater than %d",
7356 GFC_MAX_DIMENSIONS);
7357 goto cleanup;
7360 else
7361 goto cleanup;
7363 if (gfc_match_char (')') != MATCH_YES)
7364 goto syntax;
7366 m = match_case_eos ();
7367 if (m == MATCH_NO)
7368 goto syntax;
7369 if (m == MATCH_ERROR)
7370 goto cleanup;
7372 new_st.op = EXEC_SELECT_RANK;
7373 new_st.ext.block.case_list = c;
7375 /* Create temporary variable. Recycle the select type code. */
7376 select_rank_set_tmp (&c->ts, &case_value);
7378 return MATCH_YES;
7380 syntax:
7381 gfc_error ("Syntax error in RANK specification at %C");
7383 cleanup:
7384 if (c != NULL)
7385 gfc_free_case_list (c); /* new_st is cleaned up in parse.cc. */
7386 return MATCH_ERROR;
7389 /********************* WHERE subroutines ********************/
7391 /* Match the rest of a simple WHERE statement that follows an IF statement.
7394 static match
7395 match_simple_where (void)
7397 gfc_expr *expr;
7398 gfc_code *c;
7399 match m;
7401 m = gfc_match (" ( %e )", &expr);
7402 if (m != MATCH_YES)
7403 return m;
7405 m = gfc_match_assignment ();
7406 if (m == MATCH_NO)
7407 goto syntax;
7408 if (m == MATCH_ERROR)
7409 goto cleanup;
7411 if (gfc_match_eos () != MATCH_YES)
7412 goto syntax;
7414 c = gfc_get_code (EXEC_WHERE);
7415 c->expr1 = expr;
7417 c->next = XCNEW (gfc_code);
7418 *c->next = new_st;
7419 c->next->loc = gfc_current_locus;
7420 gfc_clear_new_st ();
7422 new_st.op = EXEC_WHERE;
7423 new_st.block = c;
7425 return MATCH_YES;
7427 syntax:
7428 gfc_syntax_error (ST_WHERE);
7430 cleanup:
7431 gfc_free_expr (expr);
7432 return MATCH_ERROR;
7436 /* Match a WHERE statement. */
7438 match
7439 gfc_match_where (gfc_statement *st)
7441 gfc_expr *expr;
7442 match m0, m;
7443 gfc_code *c;
7445 m0 = gfc_match_label ();
7446 if (m0 == MATCH_ERROR)
7447 return m0;
7449 m = gfc_match (" where ( %e )", &expr);
7450 if (m != MATCH_YES)
7451 return m;
7453 if (gfc_match_eos () == MATCH_YES)
7455 *st = ST_WHERE_BLOCK;
7456 new_st.op = EXEC_WHERE;
7457 new_st.expr1 = expr;
7458 return MATCH_YES;
7461 m = gfc_match_assignment ();
7462 if (m == MATCH_NO)
7463 gfc_syntax_error (ST_WHERE);
7465 if (m != MATCH_YES)
7467 gfc_free_expr (expr);
7468 return MATCH_ERROR;
7471 /* We've got a simple WHERE statement. */
7472 *st = ST_WHERE;
7473 c = gfc_get_code (EXEC_WHERE);
7474 c->expr1 = expr;
7476 /* Put in the assignment. It will not be processed by add_statement, so we
7477 need to copy the location here. */
7479 c->next = XCNEW (gfc_code);
7480 *c->next = new_st;
7481 c->next->loc = gfc_current_locus;
7482 gfc_clear_new_st ();
7484 new_st.op = EXEC_WHERE;
7485 new_st.block = c;
7487 return MATCH_YES;
7491 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
7492 new_st if successful. */
7494 match
7495 gfc_match_elsewhere (void)
7497 char name[GFC_MAX_SYMBOL_LEN + 1];
7498 gfc_expr *expr;
7499 match m;
7501 if (gfc_current_state () != COMP_WHERE)
7503 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
7504 return MATCH_ERROR;
7507 expr = NULL;
7509 if (gfc_match_char ('(') == MATCH_YES)
7511 m = gfc_match_expr (&expr);
7512 if (m == MATCH_NO)
7513 goto syntax;
7514 if (m == MATCH_ERROR)
7515 return MATCH_ERROR;
7517 if (gfc_match_char (')') != MATCH_YES)
7518 goto syntax;
7521 if (gfc_match_eos () != MATCH_YES)
7523 /* Only makes sense if we have a where-construct-name. */
7524 if (!gfc_current_block ())
7526 m = MATCH_ERROR;
7527 goto cleanup;
7529 /* Better be a name at this point. */
7530 m = gfc_match_name (name);
7531 if (m == MATCH_NO)
7532 goto syntax;
7533 if (m == MATCH_ERROR)
7534 goto cleanup;
7536 if (gfc_match_eos () != MATCH_YES)
7537 goto syntax;
7539 if (strcmp (name, gfc_current_block ()->name) != 0)
7541 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
7542 name, gfc_current_block ()->name);
7543 goto cleanup;
7547 new_st.op = EXEC_WHERE;
7548 new_st.expr1 = expr;
7549 return MATCH_YES;
7551 syntax:
7552 gfc_syntax_error (ST_ELSEWHERE);
7554 cleanup:
7555 gfc_free_expr (expr);
7556 return MATCH_ERROR;