Fix build on sparc64-linux-gnu.
[official-gcc.git] / gcc / fortran / match.c
blobf22241da60ba2253b8ea0756819f0fa200192432
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2018 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 c = gfc_next_char_literal (instring);
266 if (c == '\n')
267 break;
268 if (quote == ' ' && ((c == '\'') || (c == '"')))
270 quote = c;
271 instring = INSTRING_WARN;
272 continue;
274 if (quote != ' ' && c == quote)
276 quote = ' ';
277 instring = NONSTRING;
278 continue;
281 if (c == '(' && quote == ' ')
283 count++;
284 where = gfc_current_locus;
286 if (c == ')' && quote == ' ')
288 count--;
289 where = gfc_current_locus;
293 gfc_current_locus = old_loc;
295 if (count > 0)
297 gfc_error ("Missing %<)%> in statement at or before %L", &where);
298 return MATCH_ERROR;
300 if (count < 0)
302 gfc_error ("Missing %<(%> in statement at or before %L", &where);
303 return MATCH_ERROR;
306 return MATCH_YES;
310 /* See if the next character is a special character that has
311 escaped by a \ via the -fbackslash option. */
313 match
314 gfc_match_special_char (gfc_char_t *res)
316 int len, i;
317 gfc_char_t c, n;
318 match m;
320 m = MATCH_YES;
322 switch ((c = gfc_next_char_literal (INSTRING_WARN)))
324 case 'a':
325 *res = '\a';
326 break;
327 case 'b':
328 *res = '\b';
329 break;
330 case 't':
331 *res = '\t';
332 break;
333 case 'f':
334 *res = '\f';
335 break;
336 case 'n':
337 *res = '\n';
338 break;
339 case 'r':
340 *res = '\r';
341 break;
342 case 'v':
343 *res = '\v';
344 break;
345 case '\\':
346 *res = '\\';
347 break;
348 case '0':
349 *res = '\0';
350 break;
352 case 'x':
353 case 'u':
354 case 'U':
355 /* Hexadecimal form of wide characters. */
356 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
357 n = 0;
358 for (i = 0; i < len; i++)
360 char buf[2] = { '\0', '\0' };
362 c = gfc_next_char_literal (INSTRING_WARN);
363 if (!gfc_wide_fits_in_byte (c)
364 || !gfc_check_digit ((unsigned char) c, 16))
365 return MATCH_NO;
367 buf[0] = (unsigned char) c;
368 n = n << 4;
369 n += strtol (buf, NULL, 16);
371 *res = n;
372 break;
374 default:
375 /* Unknown backslash codes are simply not expanded. */
376 m = MATCH_NO;
377 break;
380 return m;
384 /* In free form, match at least one space. Always matches in fixed
385 form. */
387 match
388 gfc_match_space (void)
390 locus old_loc;
391 char c;
393 if (gfc_current_form == FORM_FIXED)
394 return MATCH_YES;
396 old_loc = gfc_current_locus;
398 c = gfc_next_ascii_char ();
399 if (!gfc_is_whitespace (c))
401 gfc_current_locus = old_loc;
402 return MATCH_NO;
405 gfc_gobble_whitespace ();
407 return MATCH_YES;
411 /* Match an end of statement. End of statement is optional
412 whitespace, followed by a ';' or '\n' or comment '!'. If a
413 semicolon is found, we continue to eat whitespace and semicolons. */
415 match
416 gfc_match_eos (void)
418 locus old_loc;
419 int flag;
420 char c;
422 flag = 0;
424 for (;;)
426 old_loc = gfc_current_locus;
427 gfc_gobble_whitespace ();
429 c = gfc_next_ascii_char ();
430 switch (c)
432 case '!':
435 c = gfc_next_ascii_char ();
437 while (c != '\n');
439 /* Fall through. */
441 case '\n':
442 return MATCH_YES;
444 case ';':
445 flag = 1;
446 continue;
449 break;
452 gfc_current_locus = old_loc;
453 return (flag) ? MATCH_YES : MATCH_NO;
457 /* Match a literal integer on the input, setting the value on
458 MATCH_YES. Literal ints occur in kind-parameters as well as
459 old-style character length specifications. If cnt is non-NULL it
460 will be set to the number of digits. */
462 match
463 gfc_match_small_literal_int (int *value, int *cnt)
465 locus old_loc;
466 char c;
467 int i, j;
469 old_loc = gfc_current_locus;
471 *value = -1;
472 gfc_gobble_whitespace ();
473 c = gfc_next_ascii_char ();
474 if (cnt)
475 *cnt = 0;
477 if (!ISDIGIT (c))
479 gfc_current_locus = old_loc;
480 return MATCH_NO;
483 i = c - '0';
484 j = 1;
486 for (;;)
488 old_loc = gfc_current_locus;
489 c = gfc_next_ascii_char ();
491 if (!ISDIGIT (c))
492 break;
494 i = 10 * i + c - '0';
495 j++;
497 if (i > 99999999)
499 gfc_error ("Integer too large at %C");
500 return MATCH_ERROR;
504 gfc_current_locus = old_loc;
506 *value = i;
507 if (cnt)
508 *cnt = j;
509 return MATCH_YES;
513 /* Match a small, constant integer expression, like in a kind
514 statement. On MATCH_YES, 'value' is set. */
516 match
517 gfc_match_small_int (int *value)
519 gfc_expr *expr;
520 match m;
521 int i;
523 m = gfc_match_expr (&expr);
524 if (m != MATCH_YES)
525 return m;
527 if (gfc_extract_int (expr, &i, 1))
528 m = MATCH_ERROR;
529 gfc_free_expr (expr);
531 *value = i;
532 return m;
536 /* This function is the same as the gfc_match_small_int, except that
537 we're keeping the pointer to the expr. This function could just be
538 removed and the previously mentioned one modified, though all calls
539 to it would have to be modified then (and there were a number of
540 them). Return MATCH_ERROR if fail to extract the int; otherwise,
541 return the result of gfc_match_expr(). The expr (if any) that was
542 matched is returned in the parameter expr. */
544 match
545 gfc_match_small_int_expr (int *value, gfc_expr **expr)
547 match m;
548 int i;
550 m = gfc_match_expr (expr);
551 if (m != MATCH_YES)
552 return m;
554 if (gfc_extract_int (*expr, &i, 1))
555 m = MATCH_ERROR;
557 *value = i;
558 return m;
562 /* Matches a statement label. Uses gfc_match_small_literal_int() to
563 do most of the work. */
565 match
566 gfc_match_st_label (gfc_st_label **label)
568 locus old_loc;
569 match m;
570 int i, cnt;
572 old_loc = gfc_current_locus;
574 m = gfc_match_small_literal_int (&i, &cnt);
575 if (m != MATCH_YES)
576 return m;
578 if (cnt > 5)
580 gfc_error ("Too many digits in statement label at %C");
581 goto cleanup;
584 if (i == 0)
586 gfc_error ("Statement label at %C is zero");
587 goto cleanup;
590 *label = gfc_get_st_label (i);
591 return MATCH_YES;
593 cleanup:
595 gfc_current_locus = old_loc;
596 return MATCH_ERROR;
600 /* Match and validate a label associated with a named IF, DO or SELECT
601 statement. If the symbol does not have the label attribute, we add
602 it. We also make sure the symbol does not refer to another
603 (active) block. A matched label is pointed to by gfc_new_block. */
605 match
606 gfc_match_label (void)
608 char name[GFC_MAX_SYMBOL_LEN + 1];
609 match m;
611 gfc_new_block = NULL;
613 m = gfc_match (" %n :", name);
614 if (m != MATCH_YES)
615 return m;
617 if (gfc_get_symbol (name, NULL, &gfc_new_block))
619 gfc_error ("Label name %qs at %C is ambiguous", name);
620 return MATCH_ERROR;
623 if (gfc_new_block->attr.flavor == FL_LABEL)
625 gfc_error ("Duplicate construct label %qs at %C", name);
626 return MATCH_ERROR;
629 if (!gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
630 gfc_new_block->name, NULL))
631 return MATCH_ERROR;
633 return MATCH_YES;
637 /* See if the current input looks like a name of some sort. Modifies
638 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
639 Note that options.c restricts max_identifier_length to not more
640 than GFC_MAX_SYMBOL_LEN. */
642 match
643 gfc_match_name (char *buffer)
645 locus old_loc;
646 int i;
647 char c;
649 old_loc = gfc_current_locus;
650 gfc_gobble_whitespace ();
652 c = gfc_next_ascii_char ();
653 if (!(ISALPHA (c) || (c == '_' && flag_allow_leading_underscore)))
655 /* Special cases for unary minus and plus, which allows for a sensible
656 error message for code of the form 'c = exp(-a*b) )' where an
657 extra ')' appears at the end of statement. */
658 if (!gfc_error_flag_test () && c != '(' && c != '-' && c != '+')
659 gfc_error ("Invalid character in name at %C");
660 gfc_current_locus = old_loc;
661 return MATCH_NO;
664 i = 0;
668 buffer[i++] = c;
670 if (i > gfc_option.max_identifier_length)
672 gfc_error ("Name at %C is too long");
673 return MATCH_ERROR;
676 old_loc = gfc_current_locus;
677 c = gfc_next_ascii_char ();
679 while (ISALNUM (c) || c == '_' || (flag_dollar_ok && c == '$'));
681 if (c == '$' && !flag_dollar_ok)
683 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
684 "allow it as an extension", &old_loc);
685 return MATCH_ERROR;
688 buffer[i] = '\0';
689 gfc_current_locus = old_loc;
691 return MATCH_YES;
695 /* Match a symbol on the input. Modifies the pointer to the symbol
696 pointer if successful. */
698 match
699 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
701 char buffer[GFC_MAX_SYMBOL_LEN + 1];
702 match m;
704 m = gfc_match_name (buffer);
705 if (m != MATCH_YES)
706 return m;
708 if (host_assoc)
709 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
710 ? MATCH_ERROR : MATCH_YES;
712 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
713 return MATCH_ERROR;
715 return MATCH_YES;
719 match
720 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
722 gfc_symtree *st;
723 match m;
725 m = gfc_match_sym_tree (&st, host_assoc);
727 if (m == MATCH_YES)
729 if (st)
730 *matched_symbol = st->n.sym;
731 else
732 *matched_symbol = NULL;
734 else
735 *matched_symbol = NULL;
736 return m;
740 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
741 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
742 in matchexp.c. */
744 match
745 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
747 locus orig_loc = gfc_current_locus;
748 char ch;
750 gfc_gobble_whitespace ();
751 ch = gfc_next_ascii_char ();
752 switch (ch)
754 case '+':
755 /* Matched "+". */
756 *result = INTRINSIC_PLUS;
757 return MATCH_YES;
759 case '-':
760 /* Matched "-". */
761 *result = INTRINSIC_MINUS;
762 return MATCH_YES;
764 case '=':
765 if (gfc_next_ascii_char () == '=')
767 /* Matched "==". */
768 *result = INTRINSIC_EQ;
769 return MATCH_YES;
771 break;
773 case '<':
774 if (gfc_peek_ascii_char () == '=')
776 /* Matched "<=". */
777 gfc_next_ascii_char ();
778 *result = INTRINSIC_LE;
779 return MATCH_YES;
781 /* Matched "<". */
782 *result = INTRINSIC_LT;
783 return MATCH_YES;
785 case '>':
786 if (gfc_peek_ascii_char () == '=')
788 /* Matched ">=". */
789 gfc_next_ascii_char ();
790 *result = INTRINSIC_GE;
791 return MATCH_YES;
793 /* Matched ">". */
794 *result = INTRINSIC_GT;
795 return MATCH_YES;
797 case '*':
798 if (gfc_peek_ascii_char () == '*')
800 /* Matched "**". */
801 gfc_next_ascii_char ();
802 *result = INTRINSIC_POWER;
803 return MATCH_YES;
805 /* Matched "*". */
806 *result = INTRINSIC_TIMES;
807 return MATCH_YES;
809 case '/':
810 ch = gfc_peek_ascii_char ();
811 if (ch == '=')
813 /* Matched "/=". */
814 gfc_next_ascii_char ();
815 *result = INTRINSIC_NE;
816 return MATCH_YES;
818 else if (ch == '/')
820 /* Matched "//". */
821 gfc_next_ascii_char ();
822 *result = INTRINSIC_CONCAT;
823 return MATCH_YES;
825 /* Matched "/". */
826 *result = INTRINSIC_DIVIDE;
827 return MATCH_YES;
829 case '.':
830 ch = gfc_next_ascii_char ();
831 switch (ch)
833 case 'a':
834 if (gfc_next_ascii_char () == 'n'
835 && gfc_next_ascii_char () == 'd'
836 && gfc_next_ascii_char () == '.')
838 /* Matched ".and.". */
839 *result = INTRINSIC_AND;
840 return MATCH_YES;
842 break;
844 case 'e':
845 if (gfc_next_ascii_char () == 'q')
847 ch = gfc_next_ascii_char ();
848 if (ch == '.')
850 /* Matched ".eq.". */
851 *result = INTRINSIC_EQ_OS;
852 return MATCH_YES;
854 else if (ch == 'v')
856 if (gfc_next_ascii_char () == '.')
858 /* Matched ".eqv.". */
859 *result = INTRINSIC_EQV;
860 return MATCH_YES;
864 break;
866 case 'g':
867 ch = gfc_next_ascii_char ();
868 if (ch == 'e')
870 if (gfc_next_ascii_char () == '.')
872 /* Matched ".ge.". */
873 *result = INTRINSIC_GE_OS;
874 return MATCH_YES;
877 else if (ch == 't')
879 if (gfc_next_ascii_char () == '.')
881 /* Matched ".gt.". */
882 *result = INTRINSIC_GT_OS;
883 return MATCH_YES;
886 break;
888 case 'l':
889 ch = gfc_next_ascii_char ();
890 if (ch == 'e')
892 if (gfc_next_ascii_char () == '.')
894 /* Matched ".le.". */
895 *result = INTRINSIC_LE_OS;
896 return MATCH_YES;
899 else if (ch == 't')
901 if (gfc_next_ascii_char () == '.')
903 /* Matched ".lt.". */
904 *result = INTRINSIC_LT_OS;
905 return MATCH_YES;
908 break;
910 case 'n':
911 ch = gfc_next_ascii_char ();
912 if (ch == 'e')
914 ch = gfc_next_ascii_char ();
915 if (ch == '.')
917 /* Matched ".ne.". */
918 *result = INTRINSIC_NE_OS;
919 return MATCH_YES;
921 else if (ch == 'q')
923 if (gfc_next_ascii_char () == 'v'
924 && gfc_next_ascii_char () == '.')
926 /* Matched ".neqv.". */
927 *result = INTRINSIC_NEQV;
928 return MATCH_YES;
932 else if (ch == 'o')
934 if (gfc_next_ascii_char () == 't'
935 && gfc_next_ascii_char () == '.')
937 /* Matched ".not.". */
938 *result = INTRINSIC_NOT;
939 return MATCH_YES;
942 break;
944 case 'o':
945 if (gfc_next_ascii_char () == 'r'
946 && gfc_next_ascii_char () == '.')
948 /* Matched ".or.". */
949 *result = INTRINSIC_OR;
950 return MATCH_YES;
952 break;
954 case 'x':
955 if (gfc_next_ascii_char () == 'o'
956 && gfc_next_ascii_char () == 'r'
957 && gfc_next_ascii_char () == '.')
959 if (!gfc_notify_std (GFC_STD_LEGACY, ".XOR. operator at %C"))
960 return MATCH_ERROR;
961 /* Matched ".xor." - equivalent to ".neqv.". */
962 *result = INTRINSIC_NEQV;
963 return MATCH_YES;
965 break;
967 default:
968 break;
970 break;
972 default:
973 break;
976 gfc_current_locus = orig_loc;
977 return MATCH_NO;
981 /* Match a loop control phrase:
983 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
985 If the final integer expression is not present, a constant unity
986 expression is returned. We don't return MATCH_ERROR until after
987 the equals sign is seen. */
989 match
990 gfc_match_iterator (gfc_iterator *iter, int init_flag)
992 char name[GFC_MAX_SYMBOL_LEN + 1];
993 gfc_expr *var, *e1, *e2, *e3;
994 locus start;
995 match m;
997 e1 = e2 = e3 = NULL;
999 /* Match the start of an iterator without affecting the symbol table. */
1001 start = gfc_current_locus;
1002 m = gfc_match (" %n =", name);
1003 gfc_current_locus = start;
1005 if (m != MATCH_YES)
1006 return MATCH_NO;
1008 m = gfc_match_variable (&var, 0);
1009 if (m != MATCH_YES)
1010 return MATCH_NO;
1012 if (var->symtree->n.sym->attr.dimension)
1014 gfc_error ("Loop variable at %C cannot be an array");
1015 goto cleanup;
1018 /* F2008, C617 & C565. */
1019 if (var->symtree->n.sym->attr.codimension)
1021 gfc_error ("Loop variable at %C cannot be a coarray");
1022 goto cleanup;
1025 if (var->ref != NULL)
1027 gfc_error ("Loop variable at %C cannot be a sub-component");
1028 goto cleanup;
1031 gfc_match_char ('=');
1033 var->symtree->n.sym->attr.implied_index = 1;
1035 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
1036 if (m == MATCH_NO)
1037 goto syntax;
1038 if (m == MATCH_ERROR)
1039 goto cleanup;
1041 if (gfc_match_char (',') != MATCH_YES)
1042 goto syntax;
1044 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
1045 if (m == MATCH_NO)
1046 goto syntax;
1047 if (m == MATCH_ERROR)
1048 goto cleanup;
1050 if (gfc_match_char (',') != MATCH_YES)
1052 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1053 goto done;
1056 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1057 if (m == MATCH_ERROR)
1058 goto cleanup;
1059 if (m == MATCH_NO)
1061 gfc_error ("Expected a step value in iterator at %C");
1062 goto cleanup;
1065 done:
1066 iter->var = var;
1067 iter->start = e1;
1068 iter->end = e2;
1069 iter->step = e3;
1070 return MATCH_YES;
1072 syntax:
1073 gfc_error ("Syntax error in iterator at %C");
1075 cleanup:
1076 gfc_free_expr (e1);
1077 gfc_free_expr (e2);
1078 gfc_free_expr (e3);
1080 return MATCH_ERROR;
1084 /* Tries to match the next non-whitespace character on the input.
1085 This subroutine does not return MATCH_ERROR. */
1087 match
1088 gfc_match_char (char c)
1090 locus where;
1092 where = gfc_current_locus;
1093 gfc_gobble_whitespace ();
1095 if (gfc_next_ascii_char () == c)
1096 return MATCH_YES;
1098 gfc_current_locus = where;
1099 return MATCH_NO;
1103 /* General purpose matching subroutine. The target string is a
1104 scanf-like format string in which spaces correspond to arbitrary
1105 whitespace (including no whitespace), characters correspond to
1106 themselves. The %-codes are:
1108 %% Literal percent sign
1109 %e Expression, pointer to a pointer is set
1110 %s Symbol, pointer to the symbol is set
1111 %n Name, character buffer is set to name
1112 %t Matches end of statement.
1113 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1114 %l Matches a statement label
1115 %v Matches a variable expression (an lvalue)
1116 % Matches a required space (in free form) and optional spaces. */
1118 match
1119 gfc_match (const char *target, ...)
1121 gfc_st_label **label;
1122 int matches, *ip;
1123 locus old_loc;
1124 va_list argp;
1125 char c, *np;
1126 match m, n;
1127 void **vp;
1128 const char *p;
1130 old_loc = gfc_current_locus;
1131 va_start (argp, target);
1132 m = MATCH_NO;
1133 matches = 0;
1134 p = target;
1136 loop:
1137 c = *p++;
1138 switch (c)
1140 case ' ':
1141 gfc_gobble_whitespace ();
1142 goto loop;
1143 case '\0':
1144 m = MATCH_YES;
1145 break;
1147 case '%':
1148 c = *p++;
1149 switch (c)
1151 case 'e':
1152 vp = va_arg (argp, void **);
1153 n = gfc_match_expr ((gfc_expr **) vp);
1154 if (n != MATCH_YES)
1156 m = n;
1157 goto not_yes;
1160 matches++;
1161 goto loop;
1163 case 'v':
1164 vp = va_arg (argp, void **);
1165 n = gfc_match_variable ((gfc_expr **) vp, 0);
1166 if (n != MATCH_YES)
1168 m = n;
1169 goto not_yes;
1172 matches++;
1173 goto loop;
1175 case 's':
1176 vp = va_arg (argp, void **);
1177 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1178 if (n != MATCH_YES)
1180 m = n;
1181 goto not_yes;
1184 matches++;
1185 goto loop;
1187 case 'n':
1188 np = va_arg (argp, char *);
1189 n = gfc_match_name (np);
1190 if (n != MATCH_YES)
1192 m = n;
1193 goto not_yes;
1196 matches++;
1197 goto loop;
1199 case 'l':
1200 label = va_arg (argp, gfc_st_label **);
1201 n = gfc_match_st_label (label);
1202 if (n != MATCH_YES)
1204 m = n;
1205 goto not_yes;
1208 matches++;
1209 goto loop;
1211 case 'o':
1212 ip = va_arg (argp, int *);
1213 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1214 if (n != MATCH_YES)
1216 m = n;
1217 goto not_yes;
1220 matches++;
1221 goto loop;
1223 case 't':
1224 if (gfc_match_eos () != MATCH_YES)
1226 m = MATCH_NO;
1227 goto not_yes;
1229 goto loop;
1231 case ' ':
1232 if (gfc_match_space () == MATCH_YES)
1233 goto loop;
1234 m = MATCH_NO;
1235 goto not_yes;
1237 case '%':
1238 break; /* Fall through to character matcher. */
1240 default:
1241 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1243 /* FALLTHRU */
1245 default:
1247 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1248 expect an upper case character here! */
1249 gcc_assert (TOLOWER (c) == c);
1251 if (c == gfc_next_ascii_char ())
1252 goto loop;
1253 break;
1256 not_yes:
1257 va_end (argp);
1259 if (m != MATCH_YES)
1261 /* Clean up after a failed match. */
1262 gfc_current_locus = old_loc;
1263 va_start (argp, target);
1265 p = target;
1266 for (; matches > 0; matches--)
1268 while (*p++ != '%');
1270 switch (*p++)
1272 case '%':
1273 matches++;
1274 break; /* Skip. */
1276 /* Matches that don't have to be undone */
1277 case 'o':
1278 case 'l':
1279 case 'n':
1280 case 's':
1281 (void) va_arg (argp, void **);
1282 break;
1284 case 'e':
1285 case 'v':
1286 vp = va_arg (argp, void **);
1287 gfc_free_expr ((struct gfc_expr *)*vp);
1288 *vp = NULL;
1289 break;
1293 va_end (argp);
1296 return m;
1300 /*********************** Statement level matching **********************/
1302 /* Matches the start of a program unit, which is the program keyword
1303 followed by an obligatory symbol. */
1305 match
1306 gfc_match_program (void)
1308 gfc_symbol *sym;
1309 match m;
1311 m = gfc_match ("% %s%t", &sym);
1313 if (m == MATCH_NO)
1315 gfc_error ("Invalid form of PROGRAM statement at %C");
1316 m = MATCH_ERROR;
1319 if (m == MATCH_ERROR)
1320 return m;
1322 if (!gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL))
1323 return MATCH_ERROR;
1325 gfc_new_block = sym;
1327 return MATCH_YES;
1331 /* Match a simple assignment statement. */
1333 match
1334 gfc_match_assignment (void)
1336 gfc_expr *lvalue, *rvalue;
1337 locus old_loc;
1338 match m;
1340 old_loc = gfc_current_locus;
1342 lvalue = NULL;
1343 m = gfc_match (" %v =", &lvalue);
1344 if (m != MATCH_YES)
1346 gfc_current_locus = old_loc;
1347 gfc_free_expr (lvalue);
1348 return MATCH_NO;
1351 rvalue = NULL;
1352 m = gfc_match (" %e%t", &rvalue);
1354 if (lvalue->expr_type == EXPR_CONSTANT)
1356 /* This clobbers %len and %kind. */
1357 m = MATCH_ERROR;
1358 gfc_error ("Assignment to a constant expression at %C");
1361 if (m != MATCH_YES)
1363 gfc_current_locus = old_loc;
1364 gfc_free_expr (lvalue);
1365 gfc_free_expr (rvalue);
1366 return m;
1369 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1371 new_st.op = EXEC_ASSIGN;
1372 new_st.expr1 = lvalue;
1373 new_st.expr2 = rvalue;
1375 gfc_check_do_variable (lvalue->symtree);
1377 if (lvalue->ts.type == BT_CLASS)
1378 gfc_find_vtab (&rvalue->ts);
1380 return MATCH_YES;
1384 /* Match a pointer assignment statement. */
1386 match
1387 gfc_match_pointer_assignment (void)
1389 gfc_expr *lvalue, *rvalue;
1390 locus old_loc;
1391 match m;
1393 old_loc = gfc_current_locus;
1395 lvalue = rvalue = NULL;
1396 gfc_matching_ptr_assignment = 0;
1397 gfc_matching_procptr_assignment = 0;
1399 m = gfc_match (" %v =>", &lvalue);
1400 if (m != MATCH_YES)
1402 m = MATCH_NO;
1403 goto cleanup;
1406 if (lvalue->symtree->n.sym->attr.proc_pointer
1407 || gfc_is_proc_ptr_comp (lvalue))
1408 gfc_matching_procptr_assignment = 1;
1409 else
1410 gfc_matching_ptr_assignment = 1;
1412 m = gfc_match (" %e%t", &rvalue);
1413 gfc_matching_ptr_assignment = 0;
1414 gfc_matching_procptr_assignment = 0;
1415 if (m != MATCH_YES)
1416 goto cleanup;
1418 new_st.op = EXEC_POINTER_ASSIGN;
1419 new_st.expr1 = lvalue;
1420 new_st.expr2 = rvalue;
1422 return MATCH_YES;
1424 cleanup:
1425 gfc_current_locus = old_loc;
1426 gfc_free_expr (lvalue);
1427 gfc_free_expr (rvalue);
1428 return m;
1432 /* We try to match an easy arithmetic IF statement. This only happens
1433 when just after having encountered a simple IF statement. This code
1434 is really duplicate with parts of the gfc_match_if code, but this is
1435 *much* easier. */
1437 static match
1438 match_arithmetic_if (void)
1440 gfc_st_label *l1, *l2, *l3;
1441 gfc_expr *expr;
1442 match m;
1444 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1445 if (m != MATCH_YES)
1446 return m;
1448 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1449 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1450 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1452 gfc_free_expr (expr);
1453 return MATCH_ERROR;
1456 if (!gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
1457 "Arithmetic IF statement at %C"))
1458 return MATCH_ERROR;
1460 new_st.op = EXEC_ARITHMETIC_IF;
1461 new_st.expr1 = expr;
1462 new_st.label1 = l1;
1463 new_st.label2 = l2;
1464 new_st.label3 = l3;
1466 return MATCH_YES;
1470 /* The IF statement is a bit of a pain. First of all, there are three
1471 forms of it, the simple IF, the IF that starts a block and the
1472 arithmetic IF.
1474 There is a problem with the simple IF and that is the fact that we
1475 only have a single level of undo information on symbols. What this
1476 means is for a simple IF, we must re-match the whole IF statement
1477 multiple times in order to guarantee that the symbol table ends up
1478 in the proper state. */
1480 static match match_simple_forall (void);
1481 static match match_simple_where (void);
1483 match
1484 gfc_match_if (gfc_statement *if_type)
1486 gfc_expr *expr;
1487 gfc_st_label *l1, *l2, *l3;
1488 locus old_loc, old_loc2;
1489 gfc_code *p;
1490 match m, n;
1492 n = gfc_match_label ();
1493 if (n == MATCH_ERROR)
1494 return n;
1496 old_loc = gfc_current_locus;
1498 m = gfc_match (" if ( %e", &expr);
1499 if (m != MATCH_YES)
1500 return m;
1502 old_loc2 = gfc_current_locus;
1503 gfc_current_locus = old_loc;
1505 if (gfc_match_parens () == MATCH_ERROR)
1506 return MATCH_ERROR;
1508 gfc_current_locus = old_loc2;
1510 if (gfc_match_char (')') != MATCH_YES)
1512 gfc_error ("Syntax error in IF-expression at %C");
1513 gfc_free_expr (expr);
1514 return MATCH_ERROR;
1517 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1519 if (m == MATCH_YES)
1521 if (n == MATCH_YES)
1523 gfc_error ("Block label not appropriate for arithmetic IF "
1524 "statement at %C");
1525 gfc_free_expr (expr);
1526 return MATCH_ERROR;
1529 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1530 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1531 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1533 gfc_free_expr (expr);
1534 return MATCH_ERROR;
1537 if (!gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
1538 "Arithmetic IF statement at %C"))
1539 return MATCH_ERROR;
1541 new_st.op = EXEC_ARITHMETIC_IF;
1542 new_st.expr1 = expr;
1543 new_st.label1 = l1;
1544 new_st.label2 = l2;
1545 new_st.label3 = l3;
1547 *if_type = ST_ARITHMETIC_IF;
1548 return MATCH_YES;
1551 if (gfc_match (" then%t") == MATCH_YES)
1553 new_st.op = EXEC_IF;
1554 new_st.expr1 = expr;
1555 *if_type = ST_IF_BLOCK;
1556 return MATCH_YES;
1559 if (n == MATCH_YES)
1561 gfc_error ("Block label is not appropriate for IF statement at %C");
1562 gfc_free_expr (expr);
1563 return MATCH_ERROR;
1566 /* At this point the only thing left is a simple IF statement. At
1567 this point, n has to be MATCH_NO, so we don't have to worry about
1568 re-matching a block label. From what we've got so far, try
1569 matching an assignment. */
1571 *if_type = ST_SIMPLE_IF;
1573 m = gfc_match_assignment ();
1574 if (m == MATCH_YES)
1575 goto got_match;
1577 gfc_free_expr (expr);
1578 gfc_undo_symbols ();
1579 gfc_current_locus = old_loc;
1581 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1582 assignment was found. For MATCH_NO, continue to call the various
1583 matchers. */
1584 if (m == MATCH_ERROR)
1585 return MATCH_ERROR;
1587 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1589 m = gfc_match_pointer_assignment ();
1590 if (m == MATCH_YES)
1591 goto got_match;
1593 gfc_free_expr (expr);
1594 gfc_undo_symbols ();
1595 gfc_current_locus = old_loc;
1597 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1599 /* Look at the next keyword to see which matcher to call. Matching
1600 the keyword doesn't affect the symbol table, so we don't have to
1601 restore between tries. */
1603 #define match(string, subr, statement) \
1604 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1606 gfc_clear_error ();
1608 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1609 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1610 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1611 match ("call", gfc_match_call, ST_CALL)
1612 match ("change team", gfc_match_change_team, ST_CHANGE_TEAM)
1613 match ("close", gfc_match_close, ST_CLOSE)
1614 match ("continue", gfc_match_continue, ST_CONTINUE)
1615 match ("cycle", gfc_match_cycle, ST_CYCLE)
1616 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1617 match ("end file", gfc_match_endfile, ST_END_FILE)
1618 match ("end team", gfc_match_end_team, ST_END_TEAM)
1619 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1620 match ("event post", gfc_match_event_post, ST_EVENT_POST)
1621 match ("event wait", gfc_match_event_wait, ST_EVENT_WAIT)
1622 match ("exit", gfc_match_exit, ST_EXIT)
1623 match ("fail image", gfc_match_fail_image, ST_FAIL_IMAGE)
1624 match ("flush", gfc_match_flush, ST_FLUSH)
1625 match ("forall", match_simple_forall, ST_FORALL)
1626 match ("form team", gfc_match_form_team, ST_FORM_TEAM)
1627 match ("go to", gfc_match_goto, ST_GOTO)
1628 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1629 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1630 match ("lock", gfc_match_lock, ST_LOCK)
1631 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1632 match ("open", gfc_match_open, ST_OPEN)
1633 match ("pause", gfc_match_pause, ST_NONE)
1634 match ("print", gfc_match_print, ST_WRITE)
1635 match ("read", gfc_match_read, ST_READ)
1636 match ("return", gfc_match_return, ST_RETURN)
1637 match ("rewind", gfc_match_rewind, ST_REWIND)
1638 match ("stop", gfc_match_stop, ST_STOP)
1639 match ("wait", gfc_match_wait, ST_WAIT)
1640 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1641 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1642 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1643 match ("sync team", gfc_match_sync_team, ST_SYNC_TEAM)
1644 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1645 match ("where", match_simple_where, ST_WHERE)
1646 match ("write", gfc_match_write, ST_WRITE)
1648 if (flag_dec)
1649 match ("type", gfc_match_print, ST_WRITE)
1651 /* The gfc_match_assignment() above may have returned a MATCH_NO
1652 where the assignment was to a named constant. Check that
1653 special case here. */
1654 m = gfc_match_assignment ();
1655 if (m == MATCH_NO)
1657 gfc_error ("Cannot assign to a named constant at %C");
1658 gfc_free_expr (expr);
1659 gfc_undo_symbols ();
1660 gfc_current_locus = old_loc;
1661 return MATCH_ERROR;
1664 /* All else has failed, so give up. See if any of the matchers has
1665 stored an error message of some sort. */
1666 if (!gfc_error_check ())
1667 gfc_error ("Unclassifiable statement in IF-clause at %C");
1669 gfc_free_expr (expr);
1670 return MATCH_ERROR;
1672 got_match:
1673 if (m == MATCH_NO)
1674 gfc_error ("Syntax error in IF-clause at %C");
1675 if (m != MATCH_YES)
1677 gfc_free_expr (expr);
1678 return MATCH_ERROR;
1681 /* At this point, we've matched the single IF and the action clause
1682 is in new_st. Rearrange things so that the IF statement appears
1683 in new_st. */
1685 p = gfc_get_code (EXEC_IF);
1686 p->next = XCNEW (gfc_code);
1687 *p->next = new_st;
1688 p->next->loc = gfc_current_locus;
1690 p->expr1 = expr;
1692 gfc_clear_new_st ();
1694 new_st.op = EXEC_IF;
1695 new_st.block = p;
1697 return MATCH_YES;
1700 #undef match
1703 /* Match an ELSE statement. */
1705 match
1706 gfc_match_else (void)
1708 char name[GFC_MAX_SYMBOL_LEN + 1];
1710 if (gfc_match_eos () == MATCH_YES)
1711 return MATCH_YES;
1713 if (gfc_match_name (name) != MATCH_YES
1714 || gfc_current_block () == NULL
1715 || gfc_match_eos () != MATCH_YES)
1717 gfc_error ("Unexpected junk after ELSE statement at %C");
1718 return MATCH_ERROR;
1721 if (strcmp (name, gfc_current_block ()->name) != 0)
1723 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1724 name, gfc_current_block ()->name);
1725 return MATCH_ERROR;
1728 return MATCH_YES;
1732 /* Match an ELSE IF statement. */
1734 match
1735 gfc_match_elseif (void)
1737 char name[GFC_MAX_SYMBOL_LEN + 1];
1738 gfc_expr *expr;
1739 match m;
1741 m = gfc_match (" ( %e ) then", &expr);
1742 if (m != MATCH_YES)
1743 return m;
1745 if (gfc_match_eos () == MATCH_YES)
1746 goto done;
1748 if (gfc_match_name (name) != MATCH_YES
1749 || gfc_current_block () == NULL
1750 || gfc_match_eos () != MATCH_YES)
1752 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1753 goto cleanup;
1756 if (strcmp (name, gfc_current_block ()->name) != 0)
1758 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1759 name, gfc_current_block ()->name);
1760 goto cleanup;
1763 done:
1764 new_st.op = EXEC_IF;
1765 new_st.expr1 = expr;
1766 return MATCH_YES;
1768 cleanup:
1769 gfc_free_expr (expr);
1770 return MATCH_ERROR;
1774 /* Free a gfc_iterator structure. */
1776 void
1777 gfc_free_iterator (gfc_iterator *iter, int flag)
1780 if (iter == NULL)
1781 return;
1783 gfc_free_expr (iter->var);
1784 gfc_free_expr (iter->start);
1785 gfc_free_expr (iter->end);
1786 gfc_free_expr (iter->step);
1788 if (flag)
1789 free (iter);
1793 /* Match a CRITICAL statement. */
1794 match
1795 gfc_match_critical (void)
1797 gfc_st_label *label = NULL;
1799 if (gfc_match_label () == MATCH_ERROR)
1800 return MATCH_ERROR;
1802 if (gfc_match (" critical") != MATCH_YES)
1803 return MATCH_NO;
1805 if (gfc_match_st_label (&label) == MATCH_ERROR)
1806 return MATCH_ERROR;
1808 if (gfc_match_eos () != MATCH_YES)
1810 gfc_syntax_error (ST_CRITICAL);
1811 return MATCH_ERROR;
1814 if (gfc_pure (NULL))
1816 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1817 return MATCH_ERROR;
1820 if (gfc_find_state (COMP_DO_CONCURRENT))
1822 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1823 "block");
1824 return MATCH_ERROR;
1827 gfc_unset_implicit_pure (NULL);
1829 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
1830 return MATCH_ERROR;
1832 if (flag_coarray == GFC_FCOARRAY_NONE)
1834 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1835 "enable");
1836 return MATCH_ERROR;
1839 if (gfc_find_state (COMP_CRITICAL))
1841 gfc_error ("Nested CRITICAL block at %C");
1842 return MATCH_ERROR;
1845 new_st.op = EXEC_CRITICAL;
1847 if (label != NULL
1848 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
1849 return MATCH_ERROR;
1851 return MATCH_YES;
1855 /* Match a BLOCK statement. */
1857 match
1858 gfc_match_block (void)
1860 match m;
1862 if (gfc_match_label () == MATCH_ERROR)
1863 return MATCH_ERROR;
1865 if (gfc_match (" block") != MATCH_YES)
1866 return MATCH_NO;
1868 /* For this to be a correct BLOCK statement, the line must end now. */
1869 m = gfc_match_eos ();
1870 if (m == MATCH_ERROR)
1871 return MATCH_ERROR;
1872 if (m == MATCH_NO)
1873 return MATCH_NO;
1875 return MATCH_YES;
1879 /* Match an ASSOCIATE statement. */
1881 match
1882 gfc_match_associate (void)
1884 if (gfc_match_label () == MATCH_ERROR)
1885 return MATCH_ERROR;
1887 if (gfc_match (" associate") != MATCH_YES)
1888 return MATCH_NO;
1890 /* Match the association list. */
1891 if (gfc_match_char ('(') != MATCH_YES)
1893 gfc_error ("Expected association list at %C");
1894 return MATCH_ERROR;
1896 new_st.ext.block.assoc = NULL;
1897 while (true)
1899 gfc_association_list* newAssoc = gfc_get_association_list ();
1900 gfc_association_list* a;
1902 /* Match the next association. */
1903 if (gfc_match (" %n =>", newAssoc->name) != MATCH_YES)
1905 gfc_error ("Expected association at %C");
1906 goto assocListError;
1909 if (gfc_match (" %e", &newAssoc->target) != MATCH_YES)
1911 /* Have another go, allowing for procedure pointer selectors. */
1912 gfc_matching_procptr_assignment = 1;
1913 if (gfc_match (" %e", &newAssoc->target) != MATCH_YES)
1915 gfc_error ("Invalid association target at %C");
1916 goto assocListError;
1918 gfc_matching_procptr_assignment = 0;
1920 newAssoc->where = gfc_current_locus;
1922 /* Check that the current name is not yet in the list. */
1923 for (a = new_st.ext.block.assoc; a; a = a->next)
1924 if (!strcmp (a->name, newAssoc->name))
1926 gfc_error ("Duplicate name %qs in association at %C",
1927 newAssoc->name);
1928 goto assocListError;
1931 /* The target expression must not be coindexed. */
1932 if (gfc_is_coindexed (newAssoc->target))
1934 gfc_error ("Association target at %C must not be coindexed");
1935 goto assocListError;
1938 /* The `variable' field is left blank for now; because the target is not
1939 yet resolved, we can't use gfc_has_vector_subscript to determine it
1940 for now. This is set during resolution. */
1942 /* Put it into the list. */
1943 newAssoc->next = new_st.ext.block.assoc;
1944 new_st.ext.block.assoc = newAssoc;
1946 /* Try next one or end if closing parenthesis is found. */
1947 gfc_gobble_whitespace ();
1948 if (gfc_peek_char () == ')')
1949 break;
1950 if (gfc_match_char (',') != MATCH_YES)
1952 gfc_error ("Expected %<)%> or %<,%> at %C");
1953 return MATCH_ERROR;
1956 continue;
1958 assocListError:
1959 free (newAssoc);
1960 goto error;
1962 if (gfc_match_char (')') != MATCH_YES)
1964 /* This should never happen as we peek above. */
1965 gcc_unreachable ();
1968 if (gfc_match_eos () != MATCH_YES)
1970 gfc_error ("Junk after ASSOCIATE statement at %C");
1971 goto error;
1974 return MATCH_YES;
1976 error:
1977 gfc_free_association_list (new_st.ext.block.assoc);
1978 return MATCH_ERROR;
1982 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1983 an accessible derived type. */
1985 static match
1986 match_derived_type_spec (gfc_typespec *ts)
1988 char name[GFC_MAX_SYMBOL_LEN + 1];
1989 locus old_locus;
1990 gfc_symbol *derived, *der_type;
1991 match m = MATCH_YES;
1992 gfc_actual_arglist *decl_type_param_list = NULL;
1993 bool is_pdt_template = false;
1995 old_locus = gfc_current_locus;
1997 if (gfc_match ("%n", name) != MATCH_YES)
1999 gfc_current_locus = old_locus;
2000 return MATCH_NO;
2003 gfc_find_symbol (name, NULL, 1, &derived);
2005 /* Match the PDT spec list, if there. */
2006 if (derived && derived->attr.flavor == FL_PROCEDURE)
2008 gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &der_type);
2009 is_pdt_template = der_type
2010 && der_type->attr.flavor == FL_DERIVED
2011 && der_type->attr.pdt_template;
2014 if (is_pdt_template)
2015 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
2017 if (m == MATCH_ERROR)
2019 gfc_free_actual_arglist (decl_type_param_list);
2020 return m;
2023 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
2024 derived = gfc_find_dt_in_generic (derived);
2026 /* If this is a PDT, find the specific instance. */
2027 if (m == MATCH_YES && is_pdt_template)
2029 gfc_namespace *old_ns;
2031 old_ns = gfc_current_ns;
2032 while (gfc_current_ns && gfc_current_ns->parent)
2033 gfc_current_ns = gfc_current_ns->parent;
2035 if (type_param_spec_list)
2036 gfc_free_actual_arglist (type_param_spec_list);
2037 m = gfc_get_pdt_instance (decl_type_param_list, &der_type,
2038 &type_param_spec_list);
2039 gfc_free_actual_arglist (decl_type_param_list);
2041 if (m != MATCH_YES)
2042 return m;
2043 derived = der_type;
2044 gcc_assert (!derived->attr.pdt_template && derived->attr.pdt_type);
2045 gfc_set_sym_referenced (derived);
2047 gfc_current_ns = old_ns;
2050 if (derived && derived->attr.flavor == FL_DERIVED)
2052 ts->type = BT_DERIVED;
2053 ts->u.derived = derived;
2054 return MATCH_YES;
2057 gfc_current_locus = old_locus;
2058 return MATCH_NO;
2062 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2063 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
2064 It only includes the intrinsic types from the Fortran 2003 standard
2065 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2066 the implicit_flag is not needed, so it was removed. Derived types are
2067 identified by their name alone. */
2069 match
2070 gfc_match_type_spec (gfc_typespec *ts)
2072 match m;
2073 locus old_locus;
2074 char c, name[GFC_MAX_SYMBOL_LEN + 1];
2076 gfc_clear_ts (ts);
2077 gfc_gobble_whitespace ();
2078 old_locus = gfc_current_locus;
2080 /* If c isn't [a-z], then return immediately. */
2081 c = gfc_peek_ascii_char ();
2082 if (!ISALPHA(c))
2083 return MATCH_NO;
2085 type_param_spec_list = NULL;
2087 if (match_derived_type_spec (ts) == MATCH_YES)
2089 /* Enforce F03:C401. */
2090 if (ts->u.derived->attr.abstract)
2092 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2093 ts->u.derived->name, &old_locus);
2094 return MATCH_ERROR;
2096 return MATCH_YES;
2099 if (gfc_match ("integer") == MATCH_YES)
2101 ts->type = BT_INTEGER;
2102 ts->kind = gfc_default_integer_kind;
2103 goto kind_selector;
2106 if (gfc_match ("double precision") == MATCH_YES)
2108 ts->type = BT_REAL;
2109 ts->kind = gfc_default_double_kind;
2110 return MATCH_YES;
2113 if (gfc_match ("complex") == MATCH_YES)
2115 ts->type = BT_COMPLEX;
2116 ts->kind = gfc_default_complex_kind;
2117 goto kind_selector;
2120 if (gfc_match ("character") == MATCH_YES)
2122 ts->type = BT_CHARACTER;
2124 m = gfc_match_char_spec (ts);
2125 if (ts->u.cl && ts->u.cl->length)
2126 gfc_resolve_expr (ts->u.cl->length);
2128 if (m == MATCH_NO)
2129 m = MATCH_YES;
2131 return m;
2134 /* REAL is a real pain because it can be a type, intrinsic subprogram,
2135 or list item in a type-list of an OpenMP reduction clause. Need to
2136 differentiate REAL([KIND]=scalar-int-initialization-expr) from
2137 REAL(A,[KIND]) and REAL(KIND,A). Logically, when this code was
2138 written the use of LOGICAL as a type-spec or intrinsic subprogram
2139 was overlooked. */
2141 m = gfc_match (" %n", name);
2142 if (m == MATCH_YES
2143 && (strcmp (name, "real") == 0 || strcmp (name, "logical") == 0))
2145 char c;
2146 gfc_expr *e;
2147 locus where;
2149 if (*name == 'r')
2151 ts->type = BT_REAL;
2152 ts->kind = gfc_default_real_kind;
2154 else
2156 ts->type = BT_LOGICAL;
2157 ts->kind = gfc_default_logical_kind;
2160 gfc_gobble_whitespace ();
2162 /* Prevent REAL*4, etc. */
2163 c = gfc_peek_ascii_char ();
2164 if (c == '*')
2166 gfc_error ("Invalid type-spec at %C");
2167 return MATCH_ERROR;
2170 /* Found leading colon in REAL::, a trailing ')' in for example
2171 TYPE IS (REAL), or REAL, for an OpenMP list-item. */
2172 if (c == ':' || c == ')' || (flag_openmp && c == ','))
2173 return MATCH_YES;
2175 /* Found something other than the opening '(' in REAL(... */
2176 if (c != '(')
2177 return MATCH_NO;
2178 else
2179 gfc_next_char (); /* Burn the '('. */
2181 /* Look for the optional KIND=. */
2182 where = gfc_current_locus;
2183 m = gfc_match ("%n", name);
2184 if (m == MATCH_YES)
2186 gfc_gobble_whitespace ();
2187 c = gfc_next_char ();
2188 if (c == '=')
2190 if (strcmp(name, "a") == 0 || strcmp(name, "l") == 0)
2191 return MATCH_NO;
2192 else if (strcmp(name, "kind") == 0)
2193 goto found;
2194 else
2195 return MATCH_ERROR;
2197 else
2198 gfc_current_locus = where;
2200 else
2201 gfc_current_locus = where;
2203 found:
2205 m = gfc_match_init_expr (&e);
2206 if (m == MATCH_NO || m == MATCH_ERROR)
2207 return MATCH_NO;
2209 /* If a comma appears, it is an intrinsic subprogram. */
2210 gfc_gobble_whitespace ();
2211 c = gfc_peek_ascii_char ();
2212 if (c == ',')
2214 gfc_free_expr (e);
2215 return MATCH_NO;
2218 /* If ')' appears, we have REAL(initialization-expr), here check for
2219 a scalar integer initialization-expr and valid kind parameter. */
2220 if (c == ')')
2222 if (e->ts.type != BT_INTEGER || e->rank > 0)
2224 gfc_free_expr (e);
2225 return MATCH_NO;
2228 gfc_next_char (); /* Burn the ')'. */
2229 ts->kind = (int) mpz_get_si (e->value.integer);
2230 if (gfc_validate_kind (ts->type, ts->kind , true) == -1)
2232 gfc_error ("Invalid type-spec at %C");
2233 return MATCH_ERROR;
2236 gfc_free_expr (e);
2238 return MATCH_YES;
2242 /* If a type is not matched, simply return MATCH_NO. */
2243 gfc_current_locus = old_locus;
2244 return MATCH_NO;
2246 kind_selector:
2248 gfc_gobble_whitespace ();
2250 /* This prevents INTEGER*4, etc. */
2251 if (gfc_peek_ascii_char () == '*')
2253 gfc_error ("Invalid type-spec at %C");
2254 return MATCH_ERROR;
2257 m = gfc_match_kind_spec (ts, false);
2259 /* No kind specifier found. */
2260 if (m == MATCH_NO)
2261 m = MATCH_YES;
2263 return m;
2267 /******************** FORALL subroutines ********************/
2269 /* Free a list of FORALL iterators. */
2271 void
2272 gfc_free_forall_iterator (gfc_forall_iterator *iter)
2274 gfc_forall_iterator *next;
2276 while (iter)
2278 next = iter->next;
2279 gfc_free_expr (iter->var);
2280 gfc_free_expr (iter->start);
2281 gfc_free_expr (iter->end);
2282 gfc_free_expr (iter->stride);
2283 free (iter);
2284 iter = next;
2289 /* Match an iterator as part of a FORALL statement. The format is:
2291 <var> = <start>:<end>[:<stride>]
2293 On MATCH_NO, the caller tests for the possibility that there is a
2294 scalar mask expression. */
2296 static match
2297 match_forall_iterator (gfc_forall_iterator **result)
2299 gfc_forall_iterator *iter;
2300 locus where;
2301 match m;
2303 where = gfc_current_locus;
2304 iter = XCNEW (gfc_forall_iterator);
2306 m = gfc_match_expr (&iter->var);
2307 if (m != MATCH_YES)
2308 goto cleanup;
2310 if (gfc_match_char ('=') != MATCH_YES
2311 || iter->var->expr_type != EXPR_VARIABLE)
2313 m = MATCH_NO;
2314 goto cleanup;
2317 m = gfc_match_expr (&iter->start);
2318 if (m != MATCH_YES)
2319 goto cleanup;
2321 if (gfc_match_char (':') != MATCH_YES)
2322 goto syntax;
2324 m = gfc_match_expr (&iter->end);
2325 if (m == MATCH_NO)
2326 goto syntax;
2327 if (m == MATCH_ERROR)
2328 goto cleanup;
2330 if (gfc_match_char (':') == MATCH_NO)
2331 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2332 else
2334 m = gfc_match_expr (&iter->stride);
2335 if (m == MATCH_NO)
2336 goto syntax;
2337 if (m == MATCH_ERROR)
2338 goto cleanup;
2341 /* Mark the iteration variable's symbol as used as a FORALL index. */
2342 iter->var->symtree->n.sym->forall_index = true;
2344 *result = iter;
2345 return MATCH_YES;
2347 syntax:
2348 gfc_error ("Syntax error in FORALL iterator at %C");
2349 m = MATCH_ERROR;
2351 cleanup:
2353 gfc_current_locus = where;
2354 gfc_free_forall_iterator (iter);
2355 return m;
2359 /* Match the header of a FORALL statement. */
2361 static match
2362 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2364 gfc_forall_iterator *head, *tail, *new_iter;
2365 gfc_expr *msk;
2366 match m;
2368 gfc_gobble_whitespace ();
2370 head = tail = NULL;
2371 msk = NULL;
2373 if (gfc_match_char ('(') != MATCH_YES)
2374 return MATCH_NO;
2376 m = match_forall_iterator (&new_iter);
2377 if (m == MATCH_ERROR)
2378 goto cleanup;
2379 if (m == MATCH_NO)
2380 goto syntax;
2382 head = tail = new_iter;
2384 for (;;)
2386 if (gfc_match_char (',') != MATCH_YES)
2387 break;
2389 m = match_forall_iterator (&new_iter);
2390 if (m == MATCH_ERROR)
2391 goto cleanup;
2393 if (m == MATCH_YES)
2395 tail->next = new_iter;
2396 tail = new_iter;
2397 continue;
2400 /* Have to have a mask expression. */
2402 m = gfc_match_expr (&msk);
2403 if (m == MATCH_NO)
2404 goto syntax;
2405 if (m == MATCH_ERROR)
2406 goto cleanup;
2408 break;
2411 if (gfc_match_char (')') == MATCH_NO)
2412 goto syntax;
2414 *phead = head;
2415 *mask = msk;
2416 return MATCH_YES;
2418 syntax:
2419 gfc_syntax_error (ST_FORALL);
2421 cleanup:
2422 gfc_free_expr (msk);
2423 gfc_free_forall_iterator (head);
2425 return MATCH_ERROR;
2428 /* Match the rest of a simple FORALL statement that follows an
2429 IF statement. */
2431 static match
2432 match_simple_forall (void)
2434 gfc_forall_iterator *head;
2435 gfc_expr *mask;
2436 gfc_code *c;
2437 match m;
2439 mask = NULL;
2440 head = NULL;
2441 c = NULL;
2443 m = match_forall_header (&head, &mask);
2445 if (m == MATCH_NO)
2446 goto syntax;
2447 if (m != MATCH_YES)
2448 goto cleanup;
2450 m = gfc_match_assignment ();
2452 if (m == MATCH_ERROR)
2453 goto cleanup;
2454 if (m == MATCH_NO)
2456 m = gfc_match_pointer_assignment ();
2457 if (m == MATCH_ERROR)
2458 goto cleanup;
2459 if (m == MATCH_NO)
2460 goto syntax;
2463 c = XCNEW (gfc_code);
2464 *c = new_st;
2465 c->loc = gfc_current_locus;
2467 if (gfc_match_eos () != MATCH_YES)
2468 goto syntax;
2470 gfc_clear_new_st ();
2471 new_st.op = EXEC_FORALL;
2472 new_st.expr1 = mask;
2473 new_st.ext.forall_iterator = head;
2474 new_st.block = gfc_get_code (EXEC_FORALL);
2475 new_st.block->next = c;
2477 return MATCH_YES;
2479 syntax:
2480 gfc_syntax_error (ST_FORALL);
2482 cleanup:
2483 gfc_free_forall_iterator (head);
2484 gfc_free_expr (mask);
2486 return MATCH_ERROR;
2490 /* Match a FORALL statement. */
2492 match
2493 gfc_match_forall (gfc_statement *st)
2495 gfc_forall_iterator *head;
2496 gfc_expr *mask;
2497 gfc_code *c;
2498 match m0, m;
2500 head = NULL;
2501 mask = NULL;
2502 c = NULL;
2504 m0 = gfc_match_label ();
2505 if (m0 == MATCH_ERROR)
2506 return MATCH_ERROR;
2508 m = gfc_match (" forall");
2509 if (m != MATCH_YES)
2510 return m;
2512 m = match_forall_header (&head, &mask);
2513 if (m == MATCH_ERROR)
2514 goto cleanup;
2515 if (m == MATCH_NO)
2516 goto syntax;
2518 if (gfc_match_eos () == MATCH_YES)
2520 *st = ST_FORALL_BLOCK;
2521 new_st.op = EXEC_FORALL;
2522 new_st.expr1 = mask;
2523 new_st.ext.forall_iterator = head;
2524 return MATCH_YES;
2527 m = gfc_match_assignment ();
2528 if (m == MATCH_ERROR)
2529 goto cleanup;
2530 if (m == MATCH_NO)
2532 m = gfc_match_pointer_assignment ();
2533 if (m == MATCH_ERROR)
2534 goto cleanup;
2535 if (m == MATCH_NO)
2536 goto syntax;
2539 c = XCNEW (gfc_code);
2540 *c = new_st;
2541 c->loc = gfc_current_locus;
2543 gfc_clear_new_st ();
2544 new_st.op = EXEC_FORALL;
2545 new_st.expr1 = mask;
2546 new_st.ext.forall_iterator = head;
2547 new_st.block = gfc_get_code (EXEC_FORALL);
2548 new_st.block->next = c;
2550 *st = ST_FORALL;
2551 return MATCH_YES;
2553 syntax:
2554 gfc_syntax_error (ST_FORALL);
2556 cleanup:
2557 gfc_free_forall_iterator (head);
2558 gfc_free_expr (mask);
2559 gfc_free_statements (c);
2560 return MATCH_NO;
2564 /* Match a DO statement. */
2566 match
2567 gfc_match_do (void)
2569 gfc_iterator iter, *ip;
2570 locus old_loc;
2571 gfc_st_label *label;
2572 match m;
2574 old_loc = gfc_current_locus;
2576 memset (&iter, '\0', sizeof (gfc_iterator));
2577 label = NULL;
2579 m = gfc_match_label ();
2580 if (m == MATCH_ERROR)
2581 return m;
2583 if (gfc_match (" do") != MATCH_YES)
2584 return MATCH_NO;
2586 m = gfc_match_st_label (&label);
2587 if (m == MATCH_ERROR)
2588 goto cleanup;
2590 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2592 if (gfc_match_eos () == MATCH_YES)
2594 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2595 new_st.op = EXEC_DO_WHILE;
2596 goto done;
2599 /* Match an optional comma, if no comma is found, a space is obligatory. */
2600 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2601 return MATCH_NO;
2603 /* Check for balanced parens. */
2605 if (gfc_match_parens () == MATCH_ERROR)
2606 return MATCH_ERROR;
2608 if (gfc_match (" concurrent") == MATCH_YES)
2610 gfc_forall_iterator *head;
2611 gfc_expr *mask;
2613 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
2614 return MATCH_ERROR;
2617 mask = NULL;
2618 head = NULL;
2619 m = match_forall_header (&head, &mask);
2621 if (m == MATCH_NO)
2622 return m;
2623 if (m == MATCH_ERROR)
2624 goto concurr_cleanup;
2626 if (gfc_match_eos () != MATCH_YES)
2627 goto concurr_cleanup;
2629 if (label != NULL
2630 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2631 goto concurr_cleanup;
2633 new_st.label1 = label;
2634 new_st.op = EXEC_DO_CONCURRENT;
2635 new_st.expr1 = mask;
2636 new_st.ext.forall_iterator = head;
2638 return MATCH_YES;
2640 concurr_cleanup:
2641 gfc_syntax_error (ST_DO);
2642 gfc_free_expr (mask);
2643 gfc_free_forall_iterator (head);
2644 return MATCH_ERROR;
2647 /* See if we have a DO WHILE. */
2648 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2650 new_st.op = EXEC_DO_WHILE;
2651 goto done;
2654 /* The abortive DO WHILE may have done something to the symbol
2655 table, so we start over. */
2656 gfc_undo_symbols ();
2657 gfc_current_locus = old_loc;
2659 gfc_match_label (); /* This won't error. */
2660 gfc_match (" do "); /* This will work. */
2662 gfc_match_st_label (&label); /* Can't error out. */
2663 gfc_match_char (','); /* Optional comma. */
2665 m = gfc_match_iterator (&iter, 0);
2666 if (m == MATCH_NO)
2667 return MATCH_NO;
2668 if (m == MATCH_ERROR)
2669 goto cleanup;
2671 iter.var->symtree->n.sym->attr.implied_index = 0;
2672 gfc_check_do_variable (iter.var->symtree);
2674 if (gfc_match_eos () != MATCH_YES)
2676 gfc_syntax_error (ST_DO);
2677 goto cleanup;
2680 new_st.op = EXEC_DO;
2682 done:
2683 if (label != NULL
2684 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2685 goto cleanup;
2687 new_st.label1 = label;
2689 if (new_st.op == EXEC_DO_WHILE)
2690 new_st.expr1 = iter.end;
2691 else
2693 new_st.ext.iterator = ip = gfc_get_iterator ();
2694 *ip = iter;
2697 return MATCH_YES;
2699 cleanup:
2700 gfc_free_iterator (&iter, 0);
2702 return MATCH_ERROR;
2706 /* Match an EXIT or CYCLE statement. */
2708 static match
2709 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2711 gfc_state_data *p, *o;
2712 gfc_symbol *sym;
2713 match m;
2714 int cnt;
2716 if (gfc_match_eos () == MATCH_YES)
2717 sym = NULL;
2718 else
2720 char name[GFC_MAX_SYMBOL_LEN + 1];
2721 gfc_symtree* stree;
2723 m = gfc_match ("% %n%t", name);
2724 if (m == MATCH_ERROR)
2725 return MATCH_ERROR;
2726 if (m == MATCH_NO)
2728 gfc_syntax_error (st);
2729 return MATCH_ERROR;
2732 /* Find the corresponding symbol. If there's a BLOCK statement
2733 between here and the label, it is not in gfc_current_ns but a parent
2734 namespace! */
2735 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2736 if (!stree)
2738 gfc_error ("Name %qs in %s statement at %C is unknown",
2739 name, gfc_ascii_statement (st));
2740 return MATCH_ERROR;
2743 sym = stree->n.sym;
2744 if (sym->attr.flavor != FL_LABEL)
2746 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2747 name, gfc_ascii_statement (st));
2748 return MATCH_ERROR;
2752 /* Find the loop specified by the label (or lack of a label). */
2753 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2754 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2755 o = p;
2756 else if (p->state == COMP_CRITICAL)
2758 gfc_error("%s statement at %C leaves CRITICAL construct",
2759 gfc_ascii_statement (st));
2760 return MATCH_ERROR;
2762 else if (p->state == COMP_DO_CONCURRENT
2763 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2765 /* F2008, C821 & C845. */
2766 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2767 gfc_ascii_statement (st));
2768 return MATCH_ERROR;
2770 else if ((sym && sym == p->sym)
2771 || (!sym && (p->state == COMP_DO
2772 || p->state == COMP_DO_CONCURRENT)))
2773 break;
2775 if (p == NULL)
2777 if (sym == NULL)
2778 gfc_error ("%s statement at %C is not within a construct",
2779 gfc_ascii_statement (st));
2780 else
2781 gfc_error ("%s statement at %C is not within construct %qs",
2782 gfc_ascii_statement (st), sym->name);
2784 return MATCH_ERROR;
2787 /* Special checks for EXIT from non-loop constructs. */
2788 switch (p->state)
2790 case COMP_DO:
2791 case COMP_DO_CONCURRENT:
2792 break;
2794 case COMP_CRITICAL:
2795 /* This is already handled above. */
2796 gcc_unreachable ();
2798 case COMP_ASSOCIATE:
2799 case COMP_BLOCK:
2800 case COMP_IF:
2801 case COMP_SELECT:
2802 case COMP_SELECT_TYPE:
2803 gcc_assert (sym);
2804 if (op == EXEC_CYCLE)
2806 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2807 " construct %qs", sym->name);
2808 return MATCH_ERROR;
2810 gcc_assert (op == EXEC_EXIT);
2811 if (!gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2812 " do-construct-name at %C"))
2813 return MATCH_ERROR;
2814 break;
2816 default:
2817 gfc_error ("%s statement at %C is not applicable to construct %qs",
2818 gfc_ascii_statement (st), sym->name);
2819 return MATCH_ERROR;
2822 if (o != NULL)
2824 gfc_error (is_oacc (p)
2825 ? G_("%s statement at %C leaving OpenACC structured block")
2826 : G_("%s statement at %C leaving OpenMP structured block"),
2827 gfc_ascii_statement (st));
2828 return MATCH_ERROR;
2831 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2832 o = o->previous;
2833 if (cnt > 0
2834 && o != NULL
2835 && o->state == COMP_OMP_STRUCTURED_BLOCK
2836 && (o->head->op == EXEC_OACC_LOOP
2837 || o->head->op == EXEC_OACC_PARALLEL_LOOP))
2839 int collapse = 1;
2840 gcc_assert (o->head->next != NULL
2841 && (o->head->next->op == EXEC_DO
2842 || o->head->next->op == EXEC_DO_WHILE)
2843 && o->previous != NULL
2844 && o->previous->tail->op == o->head->op);
2845 if (o->previous->tail->ext.omp_clauses != NULL
2846 && o->previous->tail->ext.omp_clauses->collapse > 1)
2847 collapse = o->previous->tail->ext.omp_clauses->collapse;
2848 if (st == ST_EXIT && cnt <= collapse)
2850 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2851 return MATCH_ERROR;
2853 if (st == ST_CYCLE && cnt < collapse)
2855 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2856 " !$ACC LOOP loop");
2857 return MATCH_ERROR;
2860 if (cnt > 0
2861 && o != NULL
2862 && (o->state == COMP_OMP_STRUCTURED_BLOCK)
2863 && (o->head->op == EXEC_OMP_DO
2864 || o->head->op == EXEC_OMP_PARALLEL_DO
2865 || o->head->op == EXEC_OMP_SIMD
2866 || o->head->op == EXEC_OMP_DO_SIMD
2867 || o->head->op == EXEC_OMP_PARALLEL_DO_SIMD))
2869 int count = 1;
2870 gcc_assert (o->head->next != NULL
2871 && (o->head->next->op == EXEC_DO
2872 || o->head->next->op == EXEC_DO_WHILE)
2873 && o->previous != NULL
2874 && o->previous->tail->op == o->head->op);
2875 if (o->previous->tail->ext.omp_clauses != NULL)
2877 if (o->previous->tail->ext.omp_clauses->collapse > 1)
2878 count = o->previous->tail->ext.omp_clauses->collapse;
2879 if (o->previous->tail->ext.omp_clauses->orderedc)
2880 count = o->previous->tail->ext.omp_clauses->orderedc;
2882 if (st == ST_EXIT && cnt <= count)
2884 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2885 return MATCH_ERROR;
2887 if (st == ST_CYCLE && cnt < count)
2889 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2890 " !$OMP DO loop");
2891 return MATCH_ERROR;
2895 /* Save the first statement in the construct - needed by the backend. */
2896 new_st.ext.which_construct = p->construct;
2898 new_st.op = op;
2900 return MATCH_YES;
2904 /* Match the EXIT statement. */
2906 match
2907 gfc_match_exit (void)
2909 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2913 /* Match the CYCLE statement. */
2915 match
2916 gfc_match_cycle (void)
2918 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2922 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
2923 requirements for a stop-code differ in the standards.
2925 Fortran 95 has
2927 R840 stop-stmt is STOP [ stop-code ]
2928 R841 stop-code is scalar-char-constant
2929 or digit [ digit [ digit [ digit [ digit ] ] ] ]
2931 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
2932 Fortran 2008 has
2934 R855 stop-stmt is STOP [ stop-code ]
2935 R856 allstop-stmt is ALL STOP [ stop-code ]
2936 R857 stop-code is scalar-default-char-constant-expr
2937 or scalar-int-constant-expr
2939 For free-form source code, all standards contain a statement of the form:
2941 A blank shall be used to separate names, constants, or labels from
2942 adjacent keywords, names, constants, or labels.
2944 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
2946 STOP123
2948 is valid, but it is invalid Fortran 2008. */
2950 static match
2951 gfc_match_stopcode (gfc_statement st)
2953 gfc_expr *e = NULL;
2954 match m;
2955 bool f95, f03;
2957 /* Set f95 for -std=f95. */
2958 f95 = (gfc_option.allow_std == GFC_STD_OPT_F95);
2960 /* Set f03 for -std=f2003. */
2961 f03 = (gfc_option.allow_std == GFC_STD_OPT_F03);
2963 /* Look for a blank between STOP and the stop-code for F2008 or later. */
2964 if (gfc_current_form != FORM_FIXED && !(f95 || f03))
2966 char c = gfc_peek_ascii_char ();
2968 /* Look for end-of-statement. There is no stop-code. */
2969 if (c == '\n' || c == '!' || c == ';')
2970 goto done;
2972 if (c != ' ')
2974 gfc_error ("Blank required in %s statement near %C",
2975 gfc_ascii_statement (st));
2976 return MATCH_ERROR;
2980 if (gfc_match_eos () != MATCH_YES)
2982 int stopcode;
2983 locus old_locus;
2985 /* First look for the F95 or F2003 digit [...] construct. */
2986 old_locus = gfc_current_locus;
2987 m = gfc_match_small_int (&stopcode);
2988 if (m == MATCH_YES && (f95 || f03))
2990 if (stopcode < 0)
2992 gfc_error ("STOP code at %C cannot be negative");
2993 return MATCH_ERROR;
2996 if (stopcode > 99999)
2998 gfc_error ("STOP code at %C contains too many digits");
2999 return MATCH_ERROR;
3003 /* Reset the locus and now load gfc_expr. */
3004 gfc_current_locus = old_locus;
3005 m = gfc_match_expr (&e);
3006 if (m == MATCH_ERROR)
3007 goto cleanup;
3008 if (m == MATCH_NO)
3009 goto syntax;
3011 if (gfc_match_eos () != MATCH_YES)
3012 goto syntax;
3015 if (gfc_pure (NULL))
3017 if (st == ST_ERROR_STOP)
3019 if (!gfc_notify_std (GFC_STD_F2018, "%s statement at %C in PURE "
3020 "procedure", gfc_ascii_statement (st)))
3021 goto cleanup;
3023 else
3025 gfc_error ("%s statement not allowed in PURE procedure at %C",
3026 gfc_ascii_statement (st));
3027 goto cleanup;
3031 gfc_unset_implicit_pure (NULL);
3033 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
3035 gfc_error ("Image control statement STOP at %C in CRITICAL block");
3036 goto cleanup;
3038 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
3040 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
3041 goto cleanup;
3044 if (e != NULL)
3046 gfc_simplify_expr (e, 0);
3048 /* Test for F95 and F2003 style STOP stop-code. */
3049 if (e->expr_type != EXPR_CONSTANT && (f95 || f03))
3051 gfc_error ("STOP code at %L must be a scalar CHARACTER constant or "
3052 "digit[digit[digit[digit[digit]]]]", &e->where);
3053 goto cleanup;
3056 /* Use the machinery for an initialization expression to reduce the
3057 stop-code to a constant. */
3058 gfc_init_expr_flag = true;
3059 gfc_reduce_init_expr (e);
3060 gfc_init_expr_flag = false;
3062 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
3064 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
3065 &e->where);
3066 goto cleanup;
3069 if (e->rank != 0)
3071 gfc_error ("STOP code at %L must be scalar", &e->where);
3072 goto cleanup;
3075 if (e->ts.type == BT_CHARACTER
3076 && e->ts.kind != gfc_default_character_kind)
3078 gfc_error ("STOP code at %L must be default character KIND=%d",
3079 &e->where, (int) gfc_default_character_kind);
3080 goto cleanup;
3083 if (e->ts.type == BT_INTEGER && e->ts.kind != gfc_default_integer_kind)
3085 gfc_error ("STOP code at %L must be default integer KIND=%d",
3086 &e->where, (int) gfc_default_integer_kind);
3087 goto cleanup;
3091 done:
3093 switch (st)
3095 case ST_STOP:
3096 new_st.op = EXEC_STOP;
3097 break;
3098 case ST_ERROR_STOP:
3099 new_st.op = EXEC_ERROR_STOP;
3100 break;
3101 case ST_PAUSE:
3102 new_st.op = EXEC_PAUSE;
3103 break;
3104 default:
3105 gcc_unreachable ();
3108 new_st.expr1 = e;
3109 new_st.ext.stop_code = -1;
3111 return MATCH_YES;
3113 syntax:
3114 gfc_syntax_error (st);
3116 cleanup:
3118 gfc_free_expr (e);
3119 return MATCH_ERROR;
3123 /* Match the (deprecated) PAUSE statement. */
3125 match
3126 gfc_match_pause (void)
3128 match m;
3130 m = gfc_match_stopcode (ST_PAUSE);
3131 if (m == MATCH_YES)
3133 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
3134 m = MATCH_ERROR;
3136 return m;
3140 /* Match the STOP statement. */
3142 match
3143 gfc_match_stop (void)
3145 return gfc_match_stopcode (ST_STOP);
3149 /* Match the ERROR STOP statement. */
3151 match
3152 gfc_match_error_stop (void)
3154 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
3155 return MATCH_ERROR;
3157 return gfc_match_stopcode (ST_ERROR_STOP);
3160 /* Match EVENT POST/WAIT statement. Syntax:
3161 EVENT POST ( event-variable [, sync-stat-list] )
3162 EVENT WAIT ( event-variable [, wait-spec-list] )
3163 with
3164 wait-spec-list is sync-stat-list or until-spec
3165 until-spec is UNTIL_COUNT = scalar-int-expr
3166 sync-stat is STAT= or ERRMSG=. */
3168 static match
3169 event_statement (gfc_statement st)
3171 match m;
3172 gfc_expr *tmp, *eventvar, *until_count, *stat, *errmsg;
3173 bool saw_until_count, saw_stat, saw_errmsg;
3175 tmp = eventvar = until_count = stat = errmsg = NULL;
3176 saw_until_count = saw_stat = saw_errmsg = false;
3178 if (gfc_pure (NULL))
3180 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3181 st == ST_EVENT_POST ? "POST" : "WAIT");
3182 return MATCH_ERROR;
3185 gfc_unset_implicit_pure (NULL);
3187 if (flag_coarray == GFC_FCOARRAY_NONE)
3189 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3190 return MATCH_ERROR;
3193 if (gfc_find_state (COMP_CRITICAL))
3195 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3196 st == ST_EVENT_POST ? "POST" : "WAIT");
3197 return MATCH_ERROR;
3200 if (gfc_find_state (COMP_DO_CONCURRENT))
3202 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3203 "block", st == ST_EVENT_POST ? "POST" : "WAIT");
3204 return MATCH_ERROR;
3207 if (gfc_match_char ('(') != MATCH_YES)
3208 goto syntax;
3210 if (gfc_match ("%e", &eventvar) != MATCH_YES)
3211 goto syntax;
3212 m = gfc_match_char (',');
3213 if (m == MATCH_ERROR)
3214 goto syntax;
3215 if (m == MATCH_NO)
3217 m = gfc_match_char (')');
3218 if (m == MATCH_YES)
3219 goto done;
3220 goto syntax;
3223 for (;;)
3225 m = gfc_match (" stat = %v", &tmp);
3226 if (m == MATCH_ERROR)
3227 goto syntax;
3228 if (m == MATCH_YES)
3230 if (saw_stat)
3232 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3233 goto cleanup;
3235 stat = tmp;
3236 saw_stat = true;
3238 m = gfc_match_char (',');
3239 if (m == MATCH_YES)
3240 continue;
3242 tmp = NULL;
3243 break;
3246 m = gfc_match (" errmsg = %v", &tmp);
3247 if (m == MATCH_ERROR)
3248 goto syntax;
3249 if (m == MATCH_YES)
3251 if (saw_errmsg)
3253 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3254 goto cleanup;
3256 errmsg = tmp;
3257 saw_errmsg = true;
3259 m = gfc_match_char (',');
3260 if (m == MATCH_YES)
3261 continue;
3263 tmp = NULL;
3264 break;
3267 m = gfc_match (" until_count = %e", &tmp);
3268 if (m == MATCH_ERROR || st == ST_EVENT_POST)
3269 goto syntax;
3270 if (m == MATCH_YES)
3272 if (saw_until_count)
3274 gfc_error ("Redundant UNTIL_COUNT tag found at %L",
3275 &tmp->where);
3276 goto cleanup;
3278 until_count = tmp;
3279 saw_until_count = true;
3281 m = gfc_match_char (',');
3282 if (m == MATCH_YES)
3283 continue;
3285 tmp = NULL;
3286 break;
3289 break;
3292 if (m == MATCH_ERROR)
3293 goto syntax;
3295 if (gfc_match (" )%t") != MATCH_YES)
3296 goto syntax;
3298 done:
3299 switch (st)
3301 case ST_EVENT_POST:
3302 new_st.op = EXEC_EVENT_POST;
3303 break;
3304 case ST_EVENT_WAIT:
3305 new_st.op = EXEC_EVENT_WAIT;
3306 break;
3307 default:
3308 gcc_unreachable ();
3311 new_st.expr1 = eventvar;
3312 new_st.expr2 = stat;
3313 new_st.expr3 = errmsg;
3314 new_st.expr4 = until_count;
3316 return MATCH_YES;
3318 syntax:
3319 gfc_syntax_error (st);
3321 cleanup:
3322 if (until_count != tmp)
3323 gfc_free_expr (until_count);
3324 if (errmsg != tmp)
3325 gfc_free_expr (errmsg);
3326 if (stat != tmp)
3327 gfc_free_expr (stat);
3329 gfc_free_expr (tmp);
3330 gfc_free_expr (eventvar);
3332 return MATCH_ERROR;
3337 match
3338 gfc_match_event_post (void)
3340 if (!gfc_notify_std (GFC_STD_F2018, "EVENT POST statement at %C"))
3341 return MATCH_ERROR;
3343 return event_statement (ST_EVENT_POST);
3347 match
3348 gfc_match_event_wait (void)
3350 if (!gfc_notify_std (GFC_STD_F2018, "EVENT WAIT statement at %C"))
3351 return MATCH_ERROR;
3353 return event_statement (ST_EVENT_WAIT);
3357 /* Match a FAIL IMAGE statement. */
3359 match
3360 gfc_match_fail_image (void)
3362 if (!gfc_notify_std (GFC_STD_F2018, "FAIL IMAGE statement at %C"))
3363 return MATCH_ERROR;
3365 if (gfc_match_char ('(') == MATCH_YES)
3366 goto syntax;
3368 new_st.op = EXEC_FAIL_IMAGE;
3370 return MATCH_YES;
3372 syntax:
3373 gfc_syntax_error (ST_FAIL_IMAGE);
3375 return MATCH_ERROR;
3378 /* Match a FORM TEAM statement. */
3380 match
3381 gfc_match_form_team (void)
3383 match m;
3384 gfc_expr *teamid,*team;
3386 if (!gfc_notify_std (GFC_STD_F2018, "FORM TEAM statement at %C"))
3387 return MATCH_ERROR;
3389 if (gfc_match_char ('(') == MATCH_NO)
3390 goto syntax;
3392 new_st.op = EXEC_FORM_TEAM;
3394 if (gfc_match ("%e", &teamid) != MATCH_YES)
3395 goto syntax;
3396 m = gfc_match_char (',');
3397 if (m == MATCH_ERROR)
3398 goto syntax;
3399 if (gfc_match ("%e", &team) != MATCH_YES)
3400 goto syntax;
3402 m = gfc_match_char (')');
3403 if (m == MATCH_NO)
3404 goto syntax;
3406 new_st.expr1 = teamid;
3407 new_st.expr2 = team;
3409 return MATCH_YES;
3411 syntax:
3412 gfc_syntax_error (ST_FORM_TEAM);
3414 return MATCH_ERROR;
3417 /* Match a CHANGE TEAM statement. */
3419 match
3420 gfc_match_change_team (void)
3422 match m;
3423 gfc_expr *team;
3425 if (!gfc_notify_std (GFC_STD_F2018, "CHANGE TEAM statement at %C"))
3426 return MATCH_ERROR;
3428 if (gfc_match_char ('(') == MATCH_NO)
3429 goto syntax;
3431 new_st.op = EXEC_CHANGE_TEAM;
3433 if (gfc_match ("%e", &team) != MATCH_YES)
3434 goto syntax;
3436 m = gfc_match_char (')');
3437 if (m == MATCH_NO)
3438 goto syntax;
3440 new_st.expr1 = team;
3442 return MATCH_YES;
3444 syntax:
3445 gfc_syntax_error (ST_CHANGE_TEAM);
3447 return MATCH_ERROR;
3450 /* Match a END TEAM statement. */
3452 match
3453 gfc_match_end_team (void)
3455 if (!gfc_notify_std (GFC_STD_F2018, "END TEAM statement at %C"))
3456 return MATCH_ERROR;
3458 if (gfc_match_char ('(') == MATCH_YES)
3459 goto syntax;
3461 new_st.op = EXEC_END_TEAM;
3463 return MATCH_YES;
3465 syntax:
3466 gfc_syntax_error (ST_END_TEAM);
3468 return MATCH_ERROR;
3471 /* Match a SYNC TEAM statement. */
3473 match
3474 gfc_match_sync_team (void)
3476 match m;
3477 gfc_expr *team;
3479 if (!gfc_notify_std (GFC_STD_F2018, "SYNC TEAM statement at %C"))
3480 return MATCH_ERROR;
3482 if (gfc_match_char ('(') == MATCH_NO)
3483 goto syntax;
3485 new_st.op = EXEC_SYNC_TEAM;
3487 if (gfc_match ("%e", &team) != MATCH_YES)
3488 goto syntax;
3490 m = gfc_match_char (')');
3491 if (m == MATCH_NO)
3492 goto syntax;
3494 new_st.expr1 = team;
3496 return MATCH_YES;
3498 syntax:
3499 gfc_syntax_error (ST_SYNC_TEAM);
3501 return MATCH_ERROR;
3504 /* Match LOCK/UNLOCK statement. Syntax:
3505 LOCK ( lock-variable [ , lock-stat-list ] )
3506 UNLOCK ( lock-variable [ , sync-stat-list ] )
3507 where lock-stat is ACQUIRED_LOCK or sync-stat
3508 and sync-stat is STAT= or ERRMSG=. */
3510 static match
3511 lock_unlock_statement (gfc_statement st)
3513 match m;
3514 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
3515 bool saw_acq_lock, saw_stat, saw_errmsg;
3517 tmp = lockvar = acq_lock = stat = errmsg = NULL;
3518 saw_acq_lock = saw_stat = saw_errmsg = false;
3520 if (gfc_pure (NULL))
3522 gfc_error ("Image control statement %s at %C in PURE procedure",
3523 st == ST_LOCK ? "LOCK" : "UNLOCK");
3524 return MATCH_ERROR;
3527 gfc_unset_implicit_pure (NULL);
3529 if (flag_coarray == GFC_FCOARRAY_NONE)
3531 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3532 return MATCH_ERROR;
3535 if (gfc_find_state (COMP_CRITICAL))
3537 gfc_error ("Image control statement %s at %C in CRITICAL block",
3538 st == ST_LOCK ? "LOCK" : "UNLOCK");
3539 return MATCH_ERROR;
3542 if (gfc_find_state (COMP_DO_CONCURRENT))
3544 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3545 st == ST_LOCK ? "LOCK" : "UNLOCK");
3546 return MATCH_ERROR;
3549 if (gfc_match_char ('(') != MATCH_YES)
3550 goto syntax;
3552 if (gfc_match ("%e", &lockvar) != MATCH_YES)
3553 goto syntax;
3554 m = gfc_match_char (',');
3555 if (m == MATCH_ERROR)
3556 goto syntax;
3557 if (m == MATCH_NO)
3559 m = gfc_match_char (')');
3560 if (m == MATCH_YES)
3561 goto done;
3562 goto syntax;
3565 for (;;)
3567 m = gfc_match (" stat = %v", &tmp);
3568 if (m == MATCH_ERROR)
3569 goto syntax;
3570 if (m == MATCH_YES)
3572 if (saw_stat)
3574 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3575 goto cleanup;
3577 stat = tmp;
3578 saw_stat = true;
3580 m = gfc_match_char (',');
3581 if (m == MATCH_YES)
3582 continue;
3584 tmp = NULL;
3585 break;
3588 m = gfc_match (" errmsg = %v", &tmp);
3589 if (m == MATCH_ERROR)
3590 goto syntax;
3591 if (m == MATCH_YES)
3593 if (saw_errmsg)
3595 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3596 goto cleanup;
3598 errmsg = tmp;
3599 saw_errmsg = true;
3601 m = gfc_match_char (',');
3602 if (m == MATCH_YES)
3603 continue;
3605 tmp = NULL;
3606 break;
3609 m = gfc_match (" acquired_lock = %v", &tmp);
3610 if (m == MATCH_ERROR || st == ST_UNLOCK)
3611 goto syntax;
3612 if (m == MATCH_YES)
3614 if (saw_acq_lock)
3616 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L",
3617 &tmp->where);
3618 goto cleanup;
3620 acq_lock = tmp;
3621 saw_acq_lock = true;
3623 m = gfc_match_char (',');
3624 if (m == MATCH_YES)
3625 continue;
3627 tmp = NULL;
3628 break;
3631 break;
3634 if (m == MATCH_ERROR)
3635 goto syntax;
3637 if (gfc_match (" )%t") != MATCH_YES)
3638 goto syntax;
3640 done:
3641 switch (st)
3643 case ST_LOCK:
3644 new_st.op = EXEC_LOCK;
3645 break;
3646 case ST_UNLOCK:
3647 new_st.op = EXEC_UNLOCK;
3648 break;
3649 default:
3650 gcc_unreachable ();
3653 new_st.expr1 = lockvar;
3654 new_st.expr2 = stat;
3655 new_st.expr3 = errmsg;
3656 new_st.expr4 = acq_lock;
3658 return MATCH_YES;
3660 syntax:
3661 gfc_syntax_error (st);
3663 cleanup:
3664 if (acq_lock != tmp)
3665 gfc_free_expr (acq_lock);
3666 if (errmsg != tmp)
3667 gfc_free_expr (errmsg);
3668 if (stat != tmp)
3669 gfc_free_expr (stat);
3671 gfc_free_expr (tmp);
3672 gfc_free_expr (lockvar);
3674 return MATCH_ERROR;
3678 match
3679 gfc_match_lock (void)
3681 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
3682 return MATCH_ERROR;
3684 return lock_unlock_statement (ST_LOCK);
3688 match
3689 gfc_match_unlock (void)
3691 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
3692 return MATCH_ERROR;
3694 return lock_unlock_statement (ST_UNLOCK);
3698 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3699 SYNC ALL [(sync-stat-list)]
3700 SYNC MEMORY [(sync-stat-list)]
3701 SYNC IMAGES (image-set [, sync-stat-list] )
3702 with sync-stat is int-expr or *. */
3704 static match
3705 sync_statement (gfc_statement st)
3707 match m;
3708 gfc_expr *tmp, *imageset, *stat, *errmsg;
3709 bool saw_stat, saw_errmsg;
3711 tmp = imageset = stat = errmsg = NULL;
3712 saw_stat = saw_errmsg = false;
3714 if (gfc_pure (NULL))
3716 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3717 return MATCH_ERROR;
3720 gfc_unset_implicit_pure (NULL);
3722 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
3723 return MATCH_ERROR;
3725 if (flag_coarray == GFC_FCOARRAY_NONE)
3727 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3728 "enable");
3729 return MATCH_ERROR;
3732 if (gfc_find_state (COMP_CRITICAL))
3734 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3735 return MATCH_ERROR;
3738 if (gfc_find_state (COMP_DO_CONCURRENT))
3740 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3741 return MATCH_ERROR;
3744 if (gfc_match_eos () == MATCH_YES)
3746 if (st == ST_SYNC_IMAGES)
3747 goto syntax;
3748 goto done;
3751 if (gfc_match_char ('(') != MATCH_YES)
3752 goto syntax;
3754 if (st == ST_SYNC_IMAGES)
3756 /* Denote '*' as imageset == NULL. */
3757 m = gfc_match_char ('*');
3758 if (m == MATCH_ERROR)
3759 goto syntax;
3760 if (m == MATCH_NO)
3762 if (gfc_match ("%e", &imageset) != MATCH_YES)
3763 goto syntax;
3765 m = gfc_match_char (',');
3766 if (m == MATCH_ERROR)
3767 goto syntax;
3768 if (m == MATCH_NO)
3770 m = gfc_match_char (')');
3771 if (m == MATCH_YES)
3772 goto done;
3773 goto syntax;
3777 for (;;)
3779 m = gfc_match (" stat = %v", &tmp);
3780 if (m == MATCH_ERROR)
3781 goto syntax;
3782 if (m == MATCH_YES)
3784 if (saw_stat)
3786 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3787 goto cleanup;
3789 stat = tmp;
3790 saw_stat = true;
3792 if (gfc_match_char (',') == MATCH_YES)
3793 continue;
3795 tmp = NULL;
3796 break;
3799 m = gfc_match (" errmsg = %v", &tmp);
3800 if (m == MATCH_ERROR)
3801 goto syntax;
3802 if (m == MATCH_YES)
3804 if (saw_errmsg)
3806 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3807 goto cleanup;
3809 errmsg = tmp;
3810 saw_errmsg = true;
3812 if (gfc_match_char (',') == MATCH_YES)
3813 continue;
3815 tmp = NULL;
3816 break;
3819 break;
3822 if (gfc_match (" )%t") != MATCH_YES)
3823 goto syntax;
3825 done:
3826 switch (st)
3828 case ST_SYNC_ALL:
3829 new_st.op = EXEC_SYNC_ALL;
3830 break;
3831 case ST_SYNC_IMAGES:
3832 new_st.op = EXEC_SYNC_IMAGES;
3833 break;
3834 case ST_SYNC_MEMORY:
3835 new_st.op = EXEC_SYNC_MEMORY;
3836 break;
3837 default:
3838 gcc_unreachable ();
3841 new_st.expr1 = imageset;
3842 new_st.expr2 = stat;
3843 new_st.expr3 = errmsg;
3845 return MATCH_YES;
3847 syntax:
3848 gfc_syntax_error (st);
3850 cleanup:
3851 if (stat != tmp)
3852 gfc_free_expr (stat);
3853 if (errmsg != tmp)
3854 gfc_free_expr (errmsg);
3856 gfc_free_expr (tmp);
3857 gfc_free_expr (imageset);
3859 return MATCH_ERROR;
3863 /* Match SYNC ALL statement. */
3865 match
3866 gfc_match_sync_all (void)
3868 return sync_statement (ST_SYNC_ALL);
3872 /* Match SYNC IMAGES statement. */
3874 match
3875 gfc_match_sync_images (void)
3877 return sync_statement (ST_SYNC_IMAGES);
3881 /* Match SYNC MEMORY statement. */
3883 match
3884 gfc_match_sync_memory (void)
3886 return sync_statement (ST_SYNC_MEMORY);
3890 /* Match a CONTINUE statement. */
3892 match
3893 gfc_match_continue (void)
3895 if (gfc_match_eos () != MATCH_YES)
3897 gfc_syntax_error (ST_CONTINUE);
3898 return MATCH_ERROR;
3901 new_st.op = EXEC_CONTINUE;
3902 return MATCH_YES;
3906 /* Match the (deprecated) ASSIGN statement. */
3908 match
3909 gfc_match_assign (void)
3911 gfc_expr *expr;
3912 gfc_st_label *label;
3914 if (gfc_match (" %l", &label) == MATCH_YES)
3916 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
3917 return MATCH_ERROR;
3918 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
3920 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
3921 return MATCH_ERROR;
3923 expr->symtree->n.sym->attr.assign = 1;
3925 new_st.op = EXEC_LABEL_ASSIGN;
3926 new_st.label1 = label;
3927 new_st.expr1 = expr;
3928 return MATCH_YES;
3931 return MATCH_NO;
3935 /* Match the GO TO statement. As a computed GOTO statement is
3936 matched, it is transformed into an equivalent SELECT block. No
3937 tree is necessary, and the resulting jumps-to-jumps are
3938 specifically optimized away by the back end. */
3940 match
3941 gfc_match_goto (void)
3943 gfc_code *head, *tail;
3944 gfc_expr *expr;
3945 gfc_case *cp;
3946 gfc_st_label *label;
3947 int i;
3948 match m;
3950 if (gfc_match (" %l%t", &label) == MATCH_YES)
3952 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3953 return MATCH_ERROR;
3955 new_st.op = EXEC_GOTO;
3956 new_st.label1 = label;
3957 return MATCH_YES;
3960 /* The assigned GO TO statement. */
3962 if (gfc_match_variable (&expr, 0) == MATCH_YES)
3964 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
3965 return MATCH_ERROR;
3967 new_st.op = EXEC_GOTO;
3968 new_st.expr1 = expr;
3970 if (gfc_match_eos () == MATCH_YES)
3971 return MATCH_YES;
3973 /* Match label list. */
3974 gfc_match_char (',');
3975 if (gfc_match_char ('(') != MATCH_YES)
3977 gfc_syntax_error (ST_GOTO);
3978 return MATCH_ERROR;
3980 head = tail = NULL;
3984 m = gfc_match_st_label (&label);
3985 if (m != MATCH_YES)
3986 goto syntax;
3988 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3989 goto cleanup;
3991 if (head == NULL)
3992 head = tail = gfc_get_code (EXEC_GOTO);
3993 else
3995 tail->block = gfc_get_code (EXEC_GOTO);
3996 tail = tail->block;
3999 tail->label1 = label;
4001 while (gfc_match_char (',') == MATCH_YES);
4003 if (gfc_match (")%t") != MATCH_YES)
4004 goto syntax;
4006 if (head == NULL)
4008 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4009 goto syntax;
4011 new_st.block = head;
4013 return MATCH_YES;
4016 /* Last chance is a computed GO TO statement. */
4017 if (gfc_match_char ('(') != MATCH_YES)
4019 gfc_syntax_error (ST_GOTO);
4020 return MATCH_ERROR;
4023 head = tail = NULL;
4024 i = 1;
4028 m = gfc_match_st_label (&label);
4029 if (m != MATCH_YES)
4030 goto syntax;
4032 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4033 goto cleanup;
4035 if (head == NULL)
4036 head = tail = gfc_get_code (EXEC_SELECT);
4037 else
4039 tail->block = gfc_get_code (EXEC_SELECT);
4040 tail = tail->block;
4043 cp = gfc_get_case ();
4044 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
4045 NULL, i++);
4047 tail->ext.block.case_list = cp;
4049 tail->next = gfc_get_code (EXEC_GOTO);
4050 tail->next->label1 = label;
4052 while (gfc_match_char (',') == MATCH_YES);
4054 if (gfc_match_char (')') != MATCH_YES)
4055 goto syntax;
4057 if (head == NULL)
4059 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4060 goto syntax;
4063 /* Get the rest of the statement. */
4064 gfc_match_char (',');
4066 if (gfc_match (" %e%t", &expr) != MATCH_YES)
4067 goto syntax;
4069 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
4070 return MATCH_ERROR;
4072 /* At this point, a computed GOTO has been fully matched and an
4073 equivalent SELECT statement constructed. */
4075 new_st.op = EXEC_SELECT;
4076 new_st.expr1 = NULL;
4078 /* Hack: For a "real" SELECT, the expression is in expr. We put
4079 it in expr2 so we can distinguish then and produce the correct
4080 diagnostics. */
4081 new_st.expr2 = expr;
4082 new_st.block = head;
4083 return MATCH_YES;
4085 syntax:
4086 gfc_syntax_error (ST_GOTO);
4087 cleanup:
4088 gfc_free_statements (head);
4089 return MATCH_ERROR;
4093 /* Frees a list of gfc_alloc structures. */
4095 void
4096 gfc_free_alloc_list (gfc_alloc *p)
4098 gfc_alloc *q;
4100 for (; p; p = q)
4102 q = p->next;
4103 gfc_free_expr (p->expr);
4104 free (p);
4109 /* Match an ALLOCATE statement. */
4111 match
4112 gfc_match_allocate (void)
4114 gfc_alloc *head, *tail;
4115 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
4116 gfc_typespec ts;
4117 gfc_symbol *sym;
4118 match m;
4119 locus old_locus, deferred_locus, assumed_locus;
4120 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
4121 bool saw_unlimited = false, saw_assumed = false;
4123 head = tail = NULL;
4124 stat = errmsg = source = mold = tmp = NULL;
4125 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
4127 if (gfc_match_char ('(') != MATCH_YES)
4129 gfc_syntax_error (ST_ALLOCATE);
4130 return MATCH_ERROR;
4133 /* Match an optional type-spec. */
4134 old_locus = gfc_current_locus;
4135 m = gfc_match_type_spec (&ts);
4136 if (m == MATCH_ERROR)
4137 goto cleanup;
4138 else if (m == MATCH_NO)
4140 char name[GFC_MAX_SYMBOL_LEN + 3];
4142 if (gfc_match ("%n :: ", name) == MATCH_YES)
4144 gfc_error ("Error in type-spec at %L", &old_locus);
4145 goto cleanup;
4148 ts.type = BT_UNKNOWN;
4150 else
4152 /* Needed for the F2008:C631 check below. */
4153 assumed_locus = gfc_current_locus;
4155 if (gfc_match (" :: ") == MATCH_YES)
4157 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
4158 &old_locus))
4159 goto cleanup;
4161 if (ts.deferred)
4163 gfc_error ("Type-spec at %L cannot contain a deferred "
4164 "type parameter", &old_locus);
4165 goto cleanup;
4168 if (ts.type == BT_CHARACTER)
4170 if (!ts.u.cl->length)
4171 saw_assumed = true;
4172 else
4173 ts.u.cl->length_from_typespec = true;
4176 if (type_param_spec_list
4177 && gfc_spec_list_type (type_param_spec_list, NULL)
4178 == SPEC_DEFERRED)
4180 gfc_error ("The type parameter spec list in the type-spec at "
4181 "%L cannot contain DEFERRED parameters", &old_locus);
4182 goto cleanup;
4185 else
4187 ts.type = BT_UNKNOWN;
4188 gfc_current_locus = old_locus;
4192 for (;;)
4194 if (head == NULL)
4195 head = tail = gfc_get_alloc ();
4196 else
4198 tail->next = gfc_get_alloc ();
4199 tail = tail->next;
4202 m = gfc_match_variable (&tail->expr, 0);
4203 if (m == MATCH_NO)
4204 goto syntax;
4205 if (m == MATCH_ERROR)
4206 goto cleanup;
4208 if (gfc_check_do_variable (tail->expr->symtree))
4209 goto cleanup;
4211 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
4212 if (impure && gfc_pure (NULL))
4214 gfc_error ("Bad allocate-object at %C for a PURE procedure");
4215 goto cleanup;
4218 if (impure)
4219 gfc_unset_implicit_pure (NULL);
4221 /* F2008:C631 (R626) A type-param-value in a type-spec shall be an
4222 asterisk if and only if each allocate-object is a dummy argument
4223 for which the corresponding type parameter is assumed. */
4224 if (saw_assumed
4225 && (tail->expr->ts.deferred
4226 || (tail->expr->ts.u.cl && tail->expr->ts.u.cl->length)
4227 || tail->expr->symtree->n.sym->attr.dummy == 0))
4229 gfc_error ("Incompatible allocate-object at %C for CHARACTER "
4230 "type-spec at %L", &assumed_locus);
4231 goto cleanup;
4234 if (tail->expr->ts.deferred)
4236 saw_deferred = true;
4237 deferred_locus = tail->expr->where;
4240 if (gfc_find_state (COMP_DO_CONCURRENT)
4241 || gfc_find_state (COMP_CRITICAL))
4243 gfc_ref *ref;
4244 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
4245 for (ref = tail->expr->ref; ref; ref = ref->next)
4246 if (ref->type == REF_COMPONENT)
4247 coarray = ref->u.c.component->attr.codimension;
4249 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
4251 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
4252 goto cleanup;
4254 if (coarray && gfc_find_state (COMP_CRITICAL))
4256 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
4257 goto cleanup;
4261 /* Check for F08:C628. */
4262 sym = tail->expr->symtree->n.sym;
4263 b1 = !(tail->expr->ref
4264 && (tail->expr->ref->type == REF_COMPONENT
4265 || tail->expr->ref->type == REF_ARRAY));
4266 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
4267 b2 = !(CLASS_DATA (sym)->attr.allocatable
4268 || CLASS_DATA (sym)->attr.class_pointer);
4269 else
4270 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4271 || sym->attr.proc_pointer);
4272 b3 = sym && sym->ns && sym->ns->proc_name
4273 && (sym->ns->proc_name->attr.allocatable
4274 || sym->ns->proc_name->attr.pointer
4275 || sym->ns->proc_name->attr.proc_pointer);
4276 if (b1 && b2 && !b3)
4278 gfc_error ("Allocate-object at %L is neither a data pointer "
4279 "nor an allocatable variable", &tail->expr->where);
4280 goto cleanup;
4283 /* The ALLOCATE statement had an optional typespec. Check the
4284 constraints. */
4285 if (ts.type != BT_UNKNOWN)
4287 /* Enforce F03:C624. */
4288 if (!gfc_type_compatible (&tail->expr->ts, &ts))
4290 gfc_error ("Type of entity at %L is type incompatible with "
4291 "typespec", &tail->expr->where);
4292 goto cleanup;
4295 /* Enforce F03:C627. */
4296 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
4298 gfc_error ("Kind type parameter for entity at %L differs from "
4299 "the kind type parameter of the typespec",
4300 &tail->expr->where);
4301 goto cleanup;
4305 if (tail->expr->ts.type == BT_DERIVED)
4306 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
4308 if (type_param_spec_list)
4309 tail->expr->param_list = gfc_copy_actual_arglist (type_param_spec_list);
4311 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
4313 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
4315 gfc_error ("Shape specification for allocatable scalar at %C");
4316 goto cleanup;
4319 if (gfc_match_char (',') != MATCH_YES)
4320 break;
4322 alloc_opt_list:
4324 m = gfc_match (" stat = %v", &tmp);
4325 if (m == MATCH_ERROR)
4326 goto cleanup;
4327 if (m == MATCH_YES)
4329 /* Enforce C630. */
4330 if (saw_stat)
4332 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
4333 goto cleanup;
4336 stat = tmp;
4337 tmp = NULL;
4338 saw_stat = true;
4340 if (gfc_check_do_variable (stat->symtree))
4341 goto cleanup;
4343 if (gfc_match_char (',') == MATCH_YES)
4344 goto alloc_opt_list;
4347 m = gfc_match (" errmsg = %v", &tmp);
4348 if (m == MATCH_ERROR)
4349 goto cleanup;
4350 if (m == MATCH_YES)
4352 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
4353 goto cleanup;
4355 /* Enforce C630. */
4356 if (saw_errmsg)
4358 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
4359 goto cleanup;
4362 errmsg = tmp;
4363 tmp = NULL;
4364 saw_errmsg = true;
4366 if (gfc_match_char (',') == MATCH_YES)
4367 goto alloc_opt_list;
4370 m = gfc_match (" source = %e", &tmp);
4371 if (m == MATCH_ERROR)
4372 goto cleanup;
4373 if (m == MATCH_YES)
4375 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
4376 goto cleanup;
4378 /* Enforce C630. */
4379 if (saw_source)
4381 gfc_error ("Redundant SOURCE tag found at %L", &tmp->where);
4382 goto cleanup;
4385 /* The next 2 conditionals check C631. */
4386 if (ts.type != BT_UNKNOWN)
4388 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4389 &tmp->where, &old_locus);
4390 goto cleanup;
4393 if (head->next
4394 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
4395 " with more than a single allocate object",
4396 &tmp->where))
4397 goto cleanup;
4399 source = tmp;
4400 tmp = NULL;
4401 saw_source = true;
4403 if (gfc_match_char (',') == MATCH_YES)
4404 goto alloc_opt_list;
4407 m = gfc_match (" mold = %e", &tmp);
4408 if (m == MATCH_ERROR)
4409 goto cleanup;
4410 if (m == MATCH_YES)
4412 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
4413 goto cleanup;
4415 /* Check F08:C636. */
4416 if (saw_mold)
4418 gfc_error ("Redundant MOLD tag found at %L", &tmp->where);
4419 goto cleanup;
4422 /* Check F08:C637. */
4423 if (ts.type != BT_UNKNOWN)
4425 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4426 &tmp->where, &old_locus);
4427 goto cleanup;
4430 mold = tmp;
4431 tmp = NULL;
4432 saw_mold = true;
4433 mold->mold = 1;
4435 if (gfc_match_char (',') == MATCH_YES)
4436 goto alloc_opt_list;
4439 gfc_gobble_whitespace ();
4441 if (gfc_peek_char () == ')')
4442 break;
4445 if (gfc_match (" )%t") != MATCH_YES)
4446 goto syntax;
4448 /* Check F08:C637. */
4449 if (source && mold)
4451 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4452 &mold->where, &source->where);
4453 goto cleanup;
4456 /* Check F03:C623, */
4457 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
4459 gfc_error ("Allocate-object at %L with a deferred type parameter "
4460 "requires either a type-spec or SOURCE tag or a MOLD tag",
4461 &deferred_locus);
4462 goto cleanup;
4465 /* Check F03:C625, */
4466 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
4468 for (tail = head; tail; tail = tail->next)
4470 if (UNLIMITED_POLY (tail->expr))
4471 gfc_error ("Unlimited polymorphic allocate-object at %L "
4472 "requires either a type-spec or SOURCE tag "
4473 "or a MOLD tag", &tail->expr->where);
4475 goto cleanup;
4478 new_st.op = EXEC_ALLOCATE;
4479 new_st.expr1 = stat;
4480 new_st.expr2 = errmsg;
4481 if (source)
4482 new_st.expr3 = source;
4483 else
4484 new_st.expr3 = mold;
4485 new_st.ext.alloc.list = head;
4486 new_st.ext.alloc.ts = ts;
4488 if (type_param_spec_list)
4489 gfc_free_actual_arglist (type_param_spec_list);
4491 return MATCH_YES;
4493 syntax:
4494 gfc_syntax_error (ST_ALLOCATE);
4496 cleanup:
4497 gfc_free_expr (errmsg);
4498 gfc_free_expr (source);
4499 gfc_free_expr (stat);
4500 gfc_free_expr (mold);
4501 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
4502 gfc_free_alloc_list (head);
4503 if (type_param_spec_list)
4504 gfc_free_actual_arglist (type_param_spec_list);
4505 return MATCH_ERROR;
4509 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4510 a set of pointer assignments to intrinsic NULL(). */
4512 match
4513 gfc_match_nullify (void)
4515 gfc_code *tail;
4516 gfc_expr *e, *p;
4517 match m;
4519 tail = NULL;
4521 if (gfc_match_char ('(') != MATCH_YES)
4522 goto syntax;
4524 for (;;)
4526 m = gfc_match_variable (&p, 0);
4527 if (m == MATCH_ERROR)
4528 goto cleanup;
4529 if (m == MATCH_NO)
4530 goto syntax;
4532 if (gfc_check_do_variable (p->symtree))
4533 goto cleanup;
4535 /* F2008, C1242. */
4536 if (gfc_is_coindexed (p))
4538 gfc_error ("Pointer object at %C shall not be coindexed");
4539 goto cleanup;
4542 /* build ' => NULL() '. */
4543 e = gfc_get_null_expr (&gfc_current_locus);
4545 /* Chain to list. */
4546 if (tail == NULL)
4548 tail = &new_st;
4549 tail->op = EXEC_POINTER_ASSIGN;
4551 else
4553 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
4554 tail = tail->next;
4557 tail->expr1 = p;
4558 tail->expr2 = e;
4560 if (gfc_match (" )%t") == MATCH_YES)
4561 break;
4562 if (gfc_match_char (',') != MATCH_YES)
4563 goto syntax;
4566 return MATCH_YES;
4568 syntax:
4569 gfc_syntax_error (ST_NULLIFY);
4571 cleanup:
4572 gfc_free_statements (new_st.next);
4573 new_st.next = NULL;
4574 gfc_free_expr (new_st.expr1);
4575 new_st.expr1 = NULL;
4576 gfc_free_expr (new_st.expr2);
4577 new_st.expr2 = NULL;
4578 return MATCH_ERROR;
4582 /* Match a DEALLOCATE statement. */
4584 match
4585 gfc_match_deallocate (void)
4587 gfc_alloc *head, *tail;
4588 gfc_expr *stat, *errmsg, *tmp;
4589 gfc_symbol *sym;
4590 match m;
4591 bool saw_stat, saw_errmsg, b1, b2;
4593 head = tail = NULL;
4594 stat = errmsg = tmp = NULL;
4595 saw_stat = saw_errmsg = false;
4597 if (gfc_match_char ('(') != MATCH_YES)
4598 goto syntax;
4600 for (;;)
4602 if (head == NULL)
4603 head = tail = gfc_get_alloc ();
4604 else
4606 tail->next = gfc_get_alloc ();
4607 tail = tail->next;
4610 m = gfc_match_variable (&tail->expr, 0);
4611 if (m == MATCH_ERROR)
4612 goto cleanup;
4613 if (m == MATCH_NO)
4614 goto syntax;
4616 if (gfc_check_do_variable (tail->expr->symtree))
4617 goto cleanup;
4619 sym = tail->expr->symtree->n.sym;
4621 bool impure = gfc_impure_variable (sym);
4622 if (impure && gfc_pure (NULL))
4624 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4625 goto cleanup;
4628 if (impure)
4629 gfc_unset_implicit_pure (NULL);
4631 if (gfc_is_coarray (tail->expr)
4632 && gfc_find_state (COMP_DO_CONCURRENT))
4634 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4635 goto cleanup;
4638 if (gfc_is_coarray (tail->expr)
4639 && gfc_find_state (COMP_CRITICAL))
4641 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4642 goto cleanup;
4645 /* FIXME: disable the checking on derived types. */
4646 b1 = !(tail->expr->ref
4647 && (tail->expr->ref->type == REF_COMPONENT
4648 || tail->expr->ref->type == REF_ARRAY));
4649 if (sym && sym->ts.type == BT_CLASS)
4650 b2 = !(CLASS_DATA (sym) && (CLASS_DATA (sym)->attr.allocatable
4651 || CLASS_DATA (sym)->attr.class_pointer));
4652 else
4653 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4654 || sym->attr.proc_pointer);
4655 if (b1 && b2)
4657 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4658 "nor an allocatable variable");
4659 goto cleanup;
4662 if (gfc_match_char (',') != MATCH_YES)
4663 break;
4665 dealloc_opt_list:
4667 m = gfc_match (" stat = %v", &tmp);
4668 if (m == MATCH_ERROR)
4669 goto cleanup;
4670 if (m == MATCH_YES)
4672 if (saw_stat)
4674 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
4675 gfc_free_expr (tmp);
4676 goto cleanup;
4679 stat = tmp;
4680 saw_stat = true;
4682 if (gfc_check_do_variable (stat->symtree))
4683 goto cleanup;
4685 if (gfc_match_char (',') == MATCH_YES)
4686 goto dealloc_opt_list;
4689 m = gfc_match (" errmsg = %v", &tmp);
4690 if (m == MATCH_ERROR)
4691 goto cleanup;
4692 if (m == MATCH_YES)
4694 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
4695 goto cleanup;
4697 if (saw_errmsg)
4699 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
4700 gfc_free_expr (tmp);
4701 goto cleanup;
4704 errmsg = tmp;
4705 saw_errmsg = true;
4707 if (gfc_match_char (',') == MATCH_YES)
4708 goto dealloc_opt_list;
4711 gfc_gobble_whitespace ();
4713 if (gfc_peek_char () == ')')
4714 break;
4717 if (gfc_match (" )%t") != MATCH_YES)
4718 goto syntax;
4720 new_st.op = EXEC_DEALLOCATE;
4721 new_st.expr1 = stat;
4722 new_st.expr2 = errmsg;
4723 new_st.ext.alloc.list = head;
4725 return MATCH_YES;
4727 syntax:
4728 gfc_syntax_error (ST_DEALLOCATE);
4730 cleanup:
4731 gfc_free_expr (errmsg);
4732 gfc_free_expr (stat);
4733 gfc_free_alloc_list (head);
4734 return MATCH_ERROR;
4738 /* Match a RETURN statement. */
4740 match
4741 gfc_match_return (void)
4743 gfc_expr *e;
4744 match m;
4745 gfc_compile_state s;
4747 e = NULL;
4749 if (gfc_find_state (COMP_CRITICAL))
4751 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4752 return MATCH_ERROR;
4755 if (gfc_find_state (COMP_DO_CONCURRENT))
4757 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4758 return MATCH_ERROR;
4761 if (gfc_match_eos () == MATCH_YES)
4762 goto done;
4764 if (!gfc_find_state (COMP_SUBROUTINE))
4766 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4767 "a SUBROUTINE");
4768 goto cleanup;
4771 if (gfc_current_form == FORM_FREE)
4773 /* The following are valid, so we can't require a blank after the
4774 RETURN keyword:
4775 return+1
4776 return(1) */
4777 char c = gfc_peek_ascii_char ();
4778 if (ISALPHA (c) || ISDIGIT (c))
4779 return MATCH_NO;
4782 m = gfc_match (" %e%t", &e);
4783 if (m == MATCH_YES)
4784 goto done;
4785 if (m == MATCH_ERROR)
4786 goto cleanup;
4788 gfc_syntax_error (ST_RETURN);
4790 cleanup:
4791 gfc_free_expr (e);
4792 return MATCH_ERROR;
4794 done:
4795 gfc_enclosing_unit (&s);
4796 if (s == COMP_PROGRAM
4797 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4798 "main program at %C"))
4799 return MATCH_ERROR;
4801 new_st.op = EXEC_RETURN;
4802 new_st.expr1 = e;
4804 return MATCH_YES;
4808 /* Match the call of a type-bound procedure, if CALL%var has already been
4809 matched and var found to be a derived-type variable. */
4811 static match
4812 match_typebound_call (gfc_symtree* varst)
4814 gfc_expr* base;
4815 match m;
4817 base = gfc_get_expr ();
4818 base->expr_type = EXPR_VARIABLE;
4819 base->symtree = varst;
4820 base->where = gfc_current_locus;
4821 gfc_set_sym_referenced (varst->n.sym);
4823 m = gfc_match_varspec (base, 0, true, true);
4824 if (m == MATCH_NO)
4825 gfc_error ("Expected component reference at %C");
4826 if (m != MATCH_YES)
4828 gfc_free_expr (base);
4829 return MATCH_ERROR;
4832 if (gfc_match_eos () != MATCH_YES)
4834 gfc_error ("Junk after CALL at %C");
4835 gfc_free_expr (base);
4836 return MATCH_ERROR;
4839 if (base->expr_type == EXPR_COMPCALL)
4840 new_st.op = EXEC_COMPCALL;
4841 else if (base->expr_type == EXPR_PPC)
4842 new_st.op = EXEC_CALL_PPC;
4843 else
4845 gfc_error ("Expected type-bound procedure or procedure pointer component "
4846 "at %C");
4847 gfc_free_expr (base);
4848 return MATCH_ERROR;
4850 new_st.expr1 = base;
4852 return MATCH_YES;
4856 /* Match a CALL statement. The tricky part here are possible
4857 alternate return specifiers. We handle these by having all
4858 "subroutines" actually return an integer via a register that gives
4859 the return number. If the call specifies alternate returns, we
4860 generate code for a SELECT statement whose case clauses contain
4861 GOTOs to the various labels. */
4863 match
4864 gfc_match_call (void)
4866 char name[GFC_MAX_SYMBOL_LEN + 1];
4867 gfc_actual_arglist *a, *arglist;
4868 gfc_case *new_case;
4869 gfc_symbol *sym;
4870 gfc_symtree *st;
4871 gfc_code *c;
4872 match m;
4873 int i;
4875 arglist = NULL;
4877 m = gfc_match ("% %n", name);
4878 if (m == MATCH_NO)
4879 goto syntax;
4880 if (m != MATCH_YES)
4881 return m;
4883 if (gfc_get_ha_sym_tree (name, &st))
4884 return MATCH_ERROR;
4886 sym = st->n.sym;
4888 /* If this is a variable of derived-type, it probably starts a type-bound
4889 procedure call. */
4890 if ((sym->attr.flavor != FL_PROCEDURE
4891 || gfc_is_function_return_value (sym, gfc_current_ns))
4892 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
4893 return match_typebound_call (st);
4895 /* If it does not seem to be callable (include functions so that the
4896 right association is made. They are thrown out in resolution.)
4897 ... */
4898 if (!sym->attr.generic
4899 && !sym->attr.subroutine
4900 && !sym->attr.function)
4902 if (!(sym->attr.external && !sym->attr.referenced))
4904 /* ...create a symbol in this scope... */
4905 if (sym->ns != gfc_current_ns
4906 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
4907 return MATCH_ERROR;
4909 if (sym != st->n.sym)
4910 sym = st->n.sym;
4913 /* ...and then to try to make the symbol into a subroutine. */
4914 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
4915 return MATCH_ERROR;
4918 gfc_set_sym_referenced (sym);
4920 if (gfc_match_eos () != MATCH_YES)
4922 m = gfc_match_actual_arglist (1, &arglist);
4923 if (m == MATCH_NO)
4924 goto syntax;
4925 if (m == MATCH_ERROR)
4926 goto cleanup;
4928 if (gfc_match_eos () != MATCH_YES)
4929 goto syntax;
4932 /* If any alternate return labels were found, construct a SELECT
4933 statement that will jump to the right place. */
4935 i = 0;
4936 for (a = arglist; a; a = a->next)
4937 if (a->expr == NULL)
4939 i = 1;
4940 break;
4943 if (i)
4945 gfc_symtree *select_st;
4946 gfc_symbol *select_sym;
4947 char name[GFC_MAX_SYMBOL_LEN + 1];
4949 new_st.next = c = gfc_get_code (EXEC_SELECT);
4950 sprintf (name, "_result_%s", sym->name);
4951 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
4953 select_sym = select_st->n.sym;
4954 select_sym->ts.type = BT_INTEGER;
4955 select_sym->ts.kind = gfc_default_integer_kind;
4956 gfc_set_sym_referenced (select_sym);
4957 c->expr1 = gfc_get_expr ();
4958 c->expr1->expr_type = EXPR_VARIABLE;
4959 c->expr1->symtree = select_st;
4960 c->expr1->ts = select_sym->ts;
4961 c->expr1->where = gfc_current_locus;
4963 i = 0;
4964 for (a = arglist; a; a = a->next)
4966 if (a->expr != NULL)
4967 continue;
4969 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
4970 continue;
4972 i++;
4974 c->block = gfc_get_code (EXEC_SELECT);
4975 c = c->block;
4977 new_case = gfc_get_case ();
4978 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
4979 new_case->low = new_case->high;
4980 c->ext.block.case_list = new_case;
4982 c->next = gfc_get_code (EXEC_GOTO);
4983 c->next->label1 = a->label;
4987 new_st.op = EXEC_CALL;
4988 new_st.symtree = st;
4989 new_st.ext.actual = arglist;
4991 return MATCH_YES;
4993 syntax:
4994 gfc_syntax_error (ST_CALL);
4996 cleanup:
4997 gfc_free_actual_arglist (arglist);
4998 return MATCH_ERROR;
5002 /* Given a name, return a pointer to the common head structure,
5003 creating it if it does not exist. If FROM_MODULE is nonzero, we
5004 mangle the name so that it doesn't interfere with commons defined
5005 in the using namespace.
5006 TODO: Add to global symbol tree. */
5008 gfc_common_head *
5009 gfc_get_common (const char *name, int from_module)
5011 gfc_symtree *st;
5012 static int serial = 0;
5013 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
5015 if (from_module)
5017 /* A use associated common block is only needed to correctly layout
5018 the variables it contains. */
5019 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
5020 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
5022 else
5024 st = gfc_find_symtree (gfc_current_ns->common_root, name);
5026 if (st == NULL)
5027 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
5030 if (st->n.common == NULL)
5032 st->n.common = gfc_get_common_head ();
5033 st->n.common->where = gfc_current_locus;
5034 strcpy (st->n.common->name, name);
5037 return st->n.common;
5041 /* Match a common block name. */
5043 match match_common_name (char *name)
5045 match m;
5047 if (gfc_match_char ('/') == MATCH_NO)
5049 name[0] = '\0';
5050 return MATCH_YES;
5053 if (gfc_match_char ('/') == MATCH_YES)
5055 name[0] = '\0';
5056 return MATCH_YES;
5059 m = gfc_match_name (name);
5061 if (m == MATCH_ERROR)
5062 return MATCH_ERROR;
5063 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
5064 return MATCH_YES;
5066 gfc_error ("Syntax error in common block name at %C");
5067 return MATCH_ERROR;
5071 /* Match a COMMON statement. */
5073 match
5074 gfc_match_common (void)
5076 gfc_symbol *sym, **head, *tail, *other;
5077 char name[GFC_MAX_SYMBOL_LEN + 1];
5078 gfc_common_head *t;
5079 gfc_array_spec *as;
5080 gfc_equiv *e1, *e2;
5081 match m;
5083 as = NULL;
5085 for (;;)
5087 m = match_common_name (name);
5088 if (m == MATCH_ERROR)
5089 goto cleanup;
5091 if (name[0] == '\0')
5093 t = &gfc_current_ns->blank_common;
5094 if (t->head == NULL)
5095 t->where = gfc_current_locus;
5097 else
5099 t = gfc_get_common (name, 0);
5101 head = &t->head;
5103 if (*head == NULL)
5104 tail = NULL;
5105 else
5107 tail = *head;
5108 while (tail->common_next)
5109 tail = tail->common_next;
5112 /* Grab the list of symbols. */
5113 for (;;)
5115 m = gfc_match_symbol (&sym, 0);
5116 if (m == MATCH_ERROR)
5117 goto cleanup;
5118 if (m == MATCH_NO)
5119 goto syntax;
5121 /* See if we know the current common block is bind(c), and if
5122 so, then see if we can check if the symbol is (which it'll
5123 need to be). This can happen if the bind(c) attr stmt was
5124 applied to the common block, and the variable(s) already
5125 defined, before declaring the common block. */
5126 if (t->is_bind_c == 1)
5128 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
5130 /* If we find an error, just print it and continue,
5131 cause it's just semantic, and we can see if there
5132 are more errors. */
5133 gfc_error_now ("Variable %qs at %L in common block %qs "
5134 "at %C must be declared with a C "
5135 "interoperable kind since common block "
5136 "%qs is bind(c)",
5137 sym->name, &(sym->declared_at), t->name,
5138 t->name);
5141 if (sym->attr.is_bind_c == 1)
5142 gfc_error_now ("Variable %qs in common block %qs at %C can not "
5143 "be bind(c) since it is not global", sym->name,
5144 t->name);
5147 if (sym->attr.in_common)
5149 gfc_error ("Symbol %qs at %C is already in a COMMON block",
5150 sym->name);
5151 goto cleanup;
5154 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
5155 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
5157 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
5158 "%C can only be COMMON in BLOCK DATA",
5159 sym->name))
5160 goto cleanup;
5163 /* Deal with an optional array specification after the
5164 symbol name. */
5165 m = gfc_match_array_spec (&as, true, true);
5166 if (m == MATCH_ERROR)
5167 goto cleanup;
5169 if (m == MATCH_YES)
5171 if (as->type != AS_EXPLICIT)
5173 gfc_error ("Array specification for symbol %qs in COMMON "
5174 "at %C must be explicit", sym->name);
5175 goto cleanup;
5178 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
5179 goto cleanup;
5181 if (sym->attr.pointer)
5183 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5184 "POINTER array", sym->name);
5185 goto cleanup;
5188 sym->as = as;
5189 as = NULL;
5193 /* Add the in_common attribute, but ignore the reported errors
5194 if any, and continue matching. */
5195 gfc_add_in_common (&sym->attr, sym->name, NULL);
5197 sym->common_block = t;
5198 sym->common_block->refs++;
5200 if (tail != NULL)
5201 tail->common_next = sym;
5202 else
5203 *head = sym;
5205 tail = sym;
5207 sym->common_head = t;
5209 /* Check to see if the symbol is already in an equivalence group.
5210 If it is, set the other members as being in common. */
5211 if (sym->attr.in_equivalence)
5213 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
5215 for (e2 = e1; e2; e2 = e2->eq)
5216 if (e2->expr->symtree->n.sym == sym)
5217 goto equiv_found;
5219 continue;
5221 equiv_found:
5223 for (e2 = e1; e2; e2 = e2->eq)
5225 other = e2->expr->symtree->n.sym;
5226 if (other->common_head
5227 && other->common_head != sym->common_head)
5229 gfc_error ("Symbol %qs, in COMMON block %qs at "
5230 "%C is being indirectly equivalenced to "
5231 "another COMMON block %qs",
5232 sym->name, sym->common_head->name,
5233 other->common_head->name);
5234 goto cleanup;
5236 other->attr.in_common = 1;
5237 other->common_head = t;
5243 gfc_gobble_whitespace ();
5244 if (gfc_match_eos () == MATCH_YES)
5245 goto done;
5246 if (gfc_peek_ascii_char () == '/')
5247 break;
5248 if (gfc_match_char (',') != MATCH_YES)
5249 goto syntax;
5250 gfc_gobble_whitespace ();
5251 if (gfc_peek_ascii_char () == '/')
5252 break;
5256 done:
5257 return MATCH_YES;
5259 syntax:
5260 gfc_syntax_error (ST_COMMON);
5262 cleanup:
5263 gfc_free_array_spec (as);
5264 return MATCH_ERROR;
5268 /* Match a BLOCK DATA program unit. */
5270 match
5271 gfc_match_block_data (void)
5273 char name[GFC_MAX_SYMBOL_LEN + 1];
5274 gfc_symbol *sym;
5275 match m;
5277 if (!gfc_notify_std (GFC_STD_F2018_OBS, "BLOCK DATA construct at %L",
5278 &gfc_current_locus))
5279 return MATCH_ERROR;
5281 if (gfc_match_eos () == MATCH_YES)
5283 gfc_new_block = NULL;
5284 return MATCH_YES;
5287 m = gfc_match ("% %n%t", name);
5288 if (m != MATCH_YES)
5289 return MATCH_ERROR;
5291 if (gfc_get_symbol (name, NULL, &sym))
5292 return MATCH_ERROR;
5294 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
5295 return MATCH_ERROR;
5297 gfc_new_block = sym;
5299 return MATCH_YES;
5303 /* Free a namelist structure. */
5305 void
5306 gfc_free_namelist (gfc_namelist *name)
5308 gfc_namelist *n;
5310 for (; name; name = n)
5312 n = name->next;
5313 free (name);
5318 /* Free an OpenMP namelist structure. */
5320 void
5321 gfc_free_omp_namelist (gfc_omp_namelist *name)
5323 gfc_omp_namelist *n;
5325 for (; name; name = n)
5327 gfc_free_expr (name->expr);
5328 if (name->udr)
5330 if (name->udr->combiner)
5331 gfc_free_statement (name->udr->combiner);
5332 if (name->udr->initializer)
5333 gfc_free_statement (name->udr->initializer);
5334 free (name->udr);
5336 n = name->next;
5337 free (name);
5342 /* Match a NAMELIST statement. */
5344 match
5345 gfc_match_namelist (void)
5347 gfc_symbol *group_name, *sym;
5348 gfc_namelist *nl;
5349 match m, m2;
5351 m = gfc_match (" / %s /", &group_name);
5352 if (m == MATCH_NO)
5353 goto syntax;
5354 if (m == MATCH_ERROR)
5355 goto error;
5357 for (;;)
5359 if (group_name->ts.type != BT_UNKNOWN)
5361 gfc_error ("Namelist group name %qs at %C already has a basic "
5362 "type of %s", group_name->name,
5363 gfc_typename (&group_name->ts));
5364 return MATCH_ERROR;
5367 if (group_name->attr.flavor == FL_NAMELIST
5368 && group_name->attr.use_assoc
5369 && !gfc_notify_std (GFC_STD_GNU, "Namelist group name %qs "
5370 "at %C already is USE associated and can"
5371 "not be respecified.", group_name->name))
5372 return MATCH_ERROR;
5374 if (group_name->attr.flavor != FL_NAMELIST
5375 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
5376 group_name->name, NULL))
5377 return MATCH_ERROR;
5379 for (;;)
5381 m = gfc_match_symbol (&sym, 1);
5382 if (m == MATCH_NO)
5383 goto syntax;
5384 if (m == MATCH_ERROR)
5385 goto error;
5387 if (sym->attr.in_namelist == 0
5388 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
5389 goto error;
5391 /* Use gfc_error_check here, rather than goto error, so that
5392 these are the only errors for the next two lines. */
5393 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
5395 gfc_error ("Assumed size array %qs in namelist %qs at "
5396 "%C is not allowed", sym->name, group_name->name);
5397 gfc_error_check ();
5400 nl = gfc_get_namelist ();
5401 nl->sym = sym;
5402 sym->refs++;
5404 if (group_name->namelist == NULL)
5405 group_name->namelist = group_name->namelist_tail = nl;
5406 else
5408 group_name->namelist_tail->next = nl;
5409 group_name->namelist_tail = nl;
5412 if (gfc_match_eos () == MATCH_YES)
5413 goto done;
5415 m = gfc_match_char (',');
5417 if (gfc_match_char ('/') == MATCH_YES)
5419 m2 = gfc_match (" %s /", &group_name);
5420 if (m2 == MATCH_YES)
5421 break;
5422 if (m2 == MATCH_ERROR)
5423 goto error;
5424 goto syntax;
5427 if (m != MATCH_YES)
5428 goto syntax;
5432 done:
5433 return MATCH_YES;
5435 syntax:
5436 gfc_syntax_error (ST_NAMELIST);
5438 error:
5439 return MATCH_ERROR;
5443 /* Match a MODULE statement. */
5445 match
5446 gfc_match_module (void)
5448 match m;
5450 m = gfc_match (" %s%t", &gfc_new_block);
5451 if (m != MATCH_YES)
5452 return m;
5454 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
5455 gfc_new_block->name, NULL))
5456 return MATCH_ERROR;
5458 return MATCH_YES;
5462 /* Free equivalence sets and lists. Recursively is the easiest way to
5463 do this. */
5465 void
5466 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
5468 if (eq == stop)
5469 return;
5471 gfc_free_equiv (eq->eq);
5472 gfc_free_equiv_until (eq->next, stop);
5473 gfc_free_expr (eq->expr);
5474 free (eq);
5478 void
5479 gfc_free_equiv (gfc_equiv *eq)
5481 gfc_free_equiv_until (eq, NULL);
5485 /* Match an EQUIVALENCE statement. */
5487 match
5488 gfc_match_equivalence (void)
5490 gfc_equiv *eq, *set, *tail;
5491 gfc_ref *ref;
5492 gfc_symbol *sym;
5493 match m;
5494 gfc_common_head *common_head = NULL;
5495 bool common_flag;
5496 int cnt;
5498 tail = NULL;
5500 for (;;)
5502 eq = gfc_get_equiv ();
5503 if (tail == NULL)
5504 tail = eq;
5506 eq->next = gfc_current_ns->equiv;
5507 gfc_current_ns->equiv = eq;
5509 if (gfc_match_char ('(') != MATCH_YES)
5510 goto syntax;
5512 set = eq;
5513 common_flag = FALSE;
5514 cnt = 0;
5516 for (;;)
5518 m = gfc_match_equiv_variable (&set->expr);
5519 if (m == MATCH_ERROR)
5520 goto cleanup;
5521 if (m == MATCH_NO)
5522 goto syntax;
5524 /* count the number of objects. */
5525 cnt++;
5527 if (gfc_match_char ('%') == MATCH_YES)
5529 gfc_error ("Derived type component %C is not a "
5530 "permitted EQUIVALENCE member");
5531 goto cleanup;
5534 for (ref = set->expr->ref; ref; ref = ref->next)
5535 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
5537 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5538 "be an array section");
5539 goto cleanup;
5542 sym = set->expr->symtree->n.sym;
5544 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
5545 goto cleanup;
5547 if (sym->attr.in_common)
5549 common_flag = TRUE;
5550 common_head = sym->common_head;
5553 if (gfc_match_char (')') == MATCH_YES)
5554 break;
5556 if (gfc_match_char (',') != MATCH_YES)
5557 goto syntax;
5559 set->eq = gfc_get_equiv ();
5560 set = set->eq;
5563 if (cnt < 2)
5565 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5566 goto cleanup;
5569 /* If one of the members of an equivalence is in common, then
5570 mark them all as being in common. Before doing this, check
5571 that members of the equivalence group are not in different
5572 common blocks. */
5573 if (common_flag)
5574 for (set = eq; set; set = set->eq)
5576 sym = set->expr->symtree->n.sym;
5577 if (sym->common_head && sym->common_head != common_head)
5579 gfc_error ("Attempt to indirectly overlap COMMON "
5580 "blocks %s and %s by EQUIVALENCE at %C",
5581 sym->common_head->name, common_head->name);
5582 goto cleanup;
5584 sym->attr.in_common = 1;
5585 sym->common_head = common_head;
5588 if (gfc_match_eos () == MATCH_YES)
5589 break;
5590 if (gfc_match_char (',') != MATCH_YES)
5592 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5593 goto cleanup;
5597 if (!gfc_notify_std (GFC_STD_F2018_OBS, "EQUIVALENCE statement at %C"))
5598 return MATCH_ERROR;
5600 return MATCH_YES;
5602 syntax:
5603 gfc_syntax_error (ST_EQUIVALENCE);
5605 cleanup:
5606 eq = tail->next;
5607 tail->next = NULL;
5609 gfc_free_equiv (gfc_current_ns->equiv);
5610 gfc_current_ns->equiv = eq;
5612 return MATCH_ERROR;
5616 /* Check that a statement function is not recursive. This is done by looking
5617 for the statement function symbol(sym) by looking recursively through its
5618 expression(e). If a reference to sym is found, true is returned.
5619 12.5.4 requires that any variable of function that is implicitly typed
5620 shall have that type confirmed by any subsequent type declaration. The
5621 implicit typing is conveniently done here. */
5622 static bool
5623 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
5625 static bool
5626 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5629 if (e == NULL)
5630 return false;
5632 switch (e->expr_type)
5634 case EXPR_FUNCTION:
5635 if (e->symtree == NULL)
5636 return false;
5638 /* Check the name before testing for nested recursion! */
5639 if (sym->name == e->symtree->n.sym->name)
5640 return true;
5642 /* Catch recursion via other statement functions. */
5643 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
5644 && e->symtree->n.sym->value
5645 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
5646 return true;
5648 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5649 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5651 break;
5653 case EXPR_VARIABLE:
5654 if (e->symtree && sym->name == e->symtree->n.sym->name)
5655 return true;
5657 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5658 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5659 break;
5661 default:
5662 break;
5665 return false;
5669 static bool
5670 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
5672 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
5676 /* Match a statement function declaration. It is so easy to match
5677 non-statement function statements with a MATCH_ERROR as opposed to
5678 MATCH_NO that we suppress error message in most cases. */
5680 match
5681 gfc_match_st_function (void)
5683 gfc_error_buffer old_error;
5684 gfc_symbol *sym;
5685 gfc_expr *expr;
5686 match m;
5688 m = gfc_match_symbol (&sym, 0);
5689 if (m != MATCH_YES)
5690 return m;
5692 gfc_push_error (&old_error);
5694 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
5695 goto undo_error;
5697 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
5698 goto undo_error;
5700 m = gfc_match (" = %e%t", &expr);
5701 if (m == MATCH_NO)
5702 goto undo_error;
5704 gfc_free_error (&old_error);
5706 if (m == MATCH_ERROR)
5707 return m;
5709 if (recursive_stmt_fcn (expr, sym))
5711 gfc_error ("Statement function at %L is recursive", &expr->where);
5712 return MATCH_ERROR;
5715 sym->value = expr;
5717 if ((gfc_current_state () == COMP_FUNCTION
5718 || gfc_current_state () == COMP_SUBROUTINE)
5719 && gfc_state_stack->previous->state == COMP_INTERFACE)
5721 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
5722 &expr->where);
5723 return MATCH_ERROR;
5726 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
5727 return MATCH_ERROR;
5729 return MATCH_YES;
5731 undo_error:
5732 gfc_pop_error (&old_error);
5733 return MATCH_NO;
5737 /* Match an assignment to a pointer function (F2008). This could, in
5738 general be ambiguous with a statement function. In this implementation
5739 it remains so if it is the first statement after the specification
5740 block. */
5742 match
5743 gfc_match_ptr_fcn_assign (void)
5745 gfc_error_buffer old_error;
5746 locus old_loc;
5747 gfc_symbol *sym;
5748 gfc_expr *expr;
5749 match m;
5750 char name[GFC_MAX_SYMBOL_LEN + 1];
5752 old_loc = gfc_current_locus;
5753 m = gfc_match_name (name);
5754 if (m != MATCH_YES)
5755 return m;
5757 gfc_find_symbol (name, NULL, 1, &sym);
5758 if (sym && sym->attr.flavor != FL_PROCEDURE)
5759 return MATCH_NO;
5761 gfc_push_error (&old_error);
5763 if (sym && sym->attr.function)
5764 goto match_actual_arglist;
5766 gfc_current_locus = old_loc;
5767 m = gfc_match_symbol (&sym, 0);
5768 if (m != MATCH_YES)
5769 return m;
5771 if (!gfc_add_procedure (&sym->attr, PROC_UNKNOWN, sym->name, NULL))
5772 goto undo_error;
5774 match_actual_arglist:
5775 gfc_current_locus = old_loc;
5776 m = gfc_match (" %e", &expr);
5777 if (m != MATCH_YES)
5778 goto undo_error;
5780 new_st.op = EXEC_ASSIGN;
5781 new_st.expr1 = expr;
5782 expr = NULL;
5784 m = gfc_match (" = %e%t", &expr);
5785 if (m != MATCH_YES)
5786 goto undo_error;
5788 new_st.expr2 = expr;
5789 return MATCH_YES;
5791 undo_error:
5792 gfc_pop_error (&old_error);
5793 return MATCH_NO;
5797 /***************** SELECT CASE subroutines ******************/
5799 /* Free a single case structure. */
5801 static void
5802 free_case (gfc_case *p)
5804 if (p->low == p->high)
5805 p->high = NULL;
5806 gfc_free_expr (p->low);
5807 gfc_free_expr (p->high);
5808 free (p);
5812 /* Free a list of case structures. */
5814 void
5815 gfc_free_case_list (gfc_case *p)
5817 gfc_case *q;
5819 for (; p; p = q)
5821 q = p->next;
5822 free_case (p);
5827 /* Match a single case selector. Combining the requirements of F08:C830
5828 and F08:C832 (R838) means that the case-value must have either CHARACTER,
5829 INTEGER, or LOGICAL type. */
5831 static match
5832 match_case_selector (gfc_case **cp)
5834 gfc_case *c;
5835 match m;
5837 c = gfc_get_case ();
5838 c->where = gfc_current_locus;
5840 if (gfc_match_char (':') == MATCH_YES)
5842 m = gfc_match_init_expr (&c->high);
5843 if (m == MATCH_NO)
5844 goto need_expr;
5845 if (m == MATCH_ERROR)
5846 goto cleanup;
5848 if (c->high->ts.type != BT_LOGICAL && c->high->ts.type != BT_INTEGER
5849 && c->high->ts.type != BT_CHARACTER)
5851 gfc_error ("Expression in CASE selector at %L cannot be %s",
5852 &c->high->where, gfc_typename (&c->high->ts));
5853 goto cleanup;
5856 else
5858 m = gfc_match_init_expr (&c->low);
5859 if (m == MATCH_ERROR)
5860 goto cleanup;
5861 if (m == MATCH_NO)
5862 goto need_expr;
5864 if (c->low->ts.type != BT_LOGICAL && c->low->ts.type != BT_INTEGER
5865 && c->low->ts.type != BT_CHARACTER)
5867 gfc_error ("Expression in CASE selector at %L cannot be %s",
5868 &c->low->where, gfc_typename (&c->low->ts));
5869 goto cleanup;
5872 /* If we're not looking at a ':' now, make a range out of a single
5873 target. Else get the upper bound for the case range. */
5874 if (gfc_match_char (':') != MATCH_YES)
5875 c->high = c->low;
5876 else
5878 m = gfc_match_init_expr (&c->high);
5879 if (m == MATCH_ERROR)
5880 goto cleanup;
5881 /* MATCH_NO is fine. It's OK if nothing is there! */
5885 *cp = c;
5886 return MATCH_YES;
5888 need_expr:
5889 gfc_error ("Expected initialization expression in CASE at %C");
5891 cleanup:
5892 free_case (c);
5893 return MATCH_ERROR;
5897 /* Match the end of a case statement. */
5899 static match
5900 match_case_eos (void)
5902 char name[GFC_MAX_SYMBOL_LEN + 1];
5903 match m;
5905 if (gfc_match_eos () == MATCH_YES)
5906 return MATCH_YES;
5908 /* If the case construct doesn't have a case-construct-name, we
5909 should have matched the EOS. */
5910 if (!gfc_current_block ())
5911 return MATCH_NO;
5913 gfc_gobble_whitespace ();
5915 m = gfc_match_name (name);
5916 if (m != MATCH_YES)
5917 return m;
5919 if (strcmp (name, gfc_current_block ()->name) != 0)
5921 gfc_error ("Expected block name %qs of SELECT construct at %C",
5922 gfc_current_block ()->name);
5923 return MATCH_ERROR;
5926 return gfc_match_eos ();
5930 /* Match a SELECT statement. */
5932 match
5933 gfc_match_select (void)
5935 gfc_expr *expr;
5936 match m;
5938 m = gfc_match_label ();
5939 if (m == MATCH_ERROR)
5940 return m;
5942 m = gfc_match (" select case ( %e )%t", &expr);
5943 if (m != MATCH_YES)
5944 return m;
5946 new_st.op = EXEC_SELECT;
5947 new_st.expr1 = expr;
5949 return MATCH_YES;
5953 /* Transfer the selector typespec to the associate name. */
5955 static void
5956 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
5958 gfc_ref *ref;
5959 gfc_symbol *assoc_sym;
5960 int rank = 0;
5962 assoc_sym = associate->symtree->n.sym;
5964 /* At this stage the expression rank and arrayspec dimensions have
5965 not been completely sorted out. We must get the expr2->rank
5966 right here, so that the correct class container is obtained. */
5967 ref = selector->ref;
5968 while (ref && ref->next)
5969 ref = ref->next;
5971 if (selector->ts.type == BT_CLASS && CLASS_DATA (selector)->as
5972 && ref && ref->type == REF_ARRAY)
5974 /* Ensure that the array reference type is set. We cannot use
5975 gfc_resolve_expr at this point, so the usable parts of
5976 resolve.c(resolve_array_ref) are employed to do it. */
5977 if (ref->u.ar.type == AR_UNKNOWN)
5979 ref->u.ar.type = AR_ELEMENT;
5980 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
5981 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5982 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
5983 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
5984 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
5986 ref->u.ar.type = AR_SECTION;
5987 break;
5991 if (ref->u.ar.type == AR_FULL)
5992 selector->rank = CLASS_DATA (selector)->as->rank;
5993 else if (ref->u.ar.type == AR_SECTION)
5994 selector->rank = ref->u.ar.dimen;
5995 else
5996 selector->rank = 0;
5998 rank = selector->rank;
6001 if (rank)
6003 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
6004 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT
6005 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6006 && ref->u.ar.end[i] == NULL
6007 && ref->u.ar.stride[i] == NULL))
6008 rank--;
6010 if (rank)
6012 assoc_sym->attr.dimension = 1;
6013 assoc_sym->as = gfc_get_array_spec ();
6014 assoc_sym->as->rank = rank;
6015 assoc_sym->as->type = AS_DEFERRED;
6017 else
6018 assoc_sym->as = NULL;
6020 else
6021 assoc_sym->as = NULL;
6023 if (selector->ts.type == BT_CLASS)
6025 /* The correct class container has to be available. */
6026 assoc_sym->ts.type = BT_CLASS;
6027 assoc_sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
6028 assoc_sym->attr.pointer = 1;
6029 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
6034 /* Push the current selector onto the SELECT TYPE stack. */
6036 static void
6037 select_type_push (gfc_symbol *sel)
6039 gfc_select_type_stack *top = gfc_get_select_type_stack ();
6040 top->selector = sel;
6041 top->tmp = NULL;
6042 top->prev = select_type_stack;
6044 select_type_stack = top;
6048 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
6050 static gfc_symtree *
6051 select_intrinsic_set_tmp (gfc_typespec *ts)
6053 char name[GFC_MAX_SYMBOL_LEN];
6054 gfc_symtree *tmp;
6055 HOST_WIDE_INT charlen = 0;
6057 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
6058 return NULL;
6060 if (select_type_stack->selector->ts.type == BT_CLASS
6061 && !select_type_stack->selector->attr.class_ok)
6062 return NULL;
6064 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
6065 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6066 charlen = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
6068 if (ts->type != BT_CHARACTER)
6069 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
6070 ts->kind);
6071 else
6072 snprintf (name, sizeof (name), "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
6073 gfc_basic_typename (ts->type), charlen, ts->kind);
6075 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6076 gfc_add_type (tmp->n.sym, ts, NULL);
6078 /* Copy across the array spec to the selector. */
6079 if (select_type_stack->selector->ts.type == BT_CLASS
6080 && (CLASS_DATA (select_type_stack->selector)->attr.dimension
6081 || CLASS_DATA (select_type_stack->selector)->attr.codimension))
6083 tmp->n.sym->attr.pointer = 1;
6084 tmp->n.sym->attr.dimension
6085 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
6086 tmp->n.sym->attr.codimension
6087 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
6088 tmp->n.sym->as
6089 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
6092 gfc_set_sym_referenced (tmp->n.sym);
6093 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
6094 tmp->n.sym->attr.select_type_temporary = 1;
6096 return tmp;
6100 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
6102 static void
6103 select_type_set_tmp (gfc_typespec *ts)
6105 char name[GFC_MAX_SYMBOL_LEN];
6106 gfc_symtree *tmp = NULL;
6108 if (!ts)
6110 select_type_stack->tmp = NULL;
6111 return;
6114 tmp = select_intrinsic_set_tmp (ts);
6116 if (tmp == NULL)
6118 if (!ts->u.derived)
6119 return;
6121 if (ts->type == BT_CLASS)
6122 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
6123 else
6124 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
6125 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6126 gfc_add_type (tmp->n.sym, ts, NULL);
6128 if (select_type_stack->selector->ts.type == BT_CLASS
6129 && select_type_stack->selector->attr.class_ok)
6131 tmp->n.sym->attr.pointer
6132 = CLASS_DATA (select_type_stack->selector)->attr.class_pointer;
6134 /* Copy across the array spec to the selector. */
6135 if (CLASS_DATA (select_type_stack->selector)->attr.dimension
6136 || CLASS_DATA (select_type_stack->selector)->attr.codimension)
6138 tmp->n.sym->attr.dimension
6139 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
6140 tmp->n.sym->attr.codimension
6141 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
6142 tmp->n.sym->as
6143 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
6147 gfc_set_sym_referenced (tmp->n.sym);
6148 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
6149 tmp->n.sym->attr.select_type_temporary = 1;
6151 if (ts->type == BT_CLASS)
6152 gfc_build_class_symbol (&tmp->n.sym->ts, &tmp->n.sym->attr,
6153 &tmp->n.sym->as);
6156 /* Add an association for it, so the rest of the parser knows it is
6157 an associate-name. The target will be set during resolution. */
6158 tmp->n.sym->assoc = gfc_get_association_list ();
6159 tmp->n.sym->assoc->dangling = 1;
6160 tmp->n.sym->assoc->st = tmp;
6162 select_type_stack->tmp = tmp;
6166 /* Match a SELECT TYPE statement. */
6168 match
6169 gfc_match_select_type (void)
6171 gfc_expr *expr1, *expr2 = NULL;
6172 match m;
6173 char name[GFC_MAX_SYMBOL_LEN];
6174 bool class_array;
6175 gfc_symbol *sym;
6176 gfc_namespace *ns = gfc_current_ns;
6178 m = gfc_match_label ();
6179 if (m == MATCH_ERROR)
6180 return m;
6182 m = gfc_match (" select type ( ");
6183 if (m != MATCH_YES)
6184 return m;
6186 gfc_current_ns = gfc_build_block_ns (ns);
6187 m = gfc_match (" %n => %e", name, &expr2);
6188 if (m == MATCH_YES)
6190 expr1 = gfc_get_expr ();
6191 expr1->expr_type = EXPR_VARIABLE;
6192 expr1->where = expr2->where;
6193 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
6195 m = MATCH_ERROR;
6196 goto cleanup;
6199 sym = expr1->symtree->n.sym;
6200 if (expr2->ts.type == BT_UNKNOWN)
6201 sym->attr.untyped = 1;
6202 else
6203 copy_ts_from_selector_to_associate (expr1, expr2);
6205 sym->attr.flavor = FL_VARIABLE;
6206 sym->attr.referenced = 1;
6207 sym->attr.class_ok = 1;
6209 else
6211 m = gfc_match (" %e ", &expr1);
6212 if (m != MATCH_YES)
6214 std::swap (ns, gfc_current_ns);
6215 gfc_free_namespace (ns);
6216 return m;
6220 m = gfc_match (" )%t");
6221 if (m != MATCH_YES)
6223 gfc_error ("parse error in SELECT TYPE statement at %C");
6224 goto cleanup;
6227 /* This ghastly expression seems to be needed to distinguish a CLASS
6228 array, which can have a reference, from other expressions that
6229 have references, such as derived type components, and are not
6230 allowed by the standard.
6231 TODO: see if it is sufficient to exclude component and substring
6232 references. */
6233 class_array = (expr1->expr_type == EXPR_VARIABLE
6234 && expr1->ts.type == BT_CLASS
6235 && CLASS_DATA (expr1)
6236 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
6237 && (CLASS_DATA (expr1)->attr.dimension
6238 || CLASS_DATA (expr1)->attr.codimension)
6239 && expr1->ref
6240 && expr1->ref->type == REF_ARRAY
6241 && expr1->ref->u.ar.type == AR_FULL
6242 && expr1->ref->next == NULL);
6244 /* Check for F03:C811 (F08:C835). */
6245 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
6246 || (!class_array && expr1->ref != NULL)))
6248 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
6249 "use associate-name=>");
6250 m = MATCH_ERROR;
6251 goto cleanup;
6254 new_st.op = EXEC_SELECT_TYPE;
6255 new_st.expr1 = expr1;
6256 new_st.expr2 = expr2;
6257 new_st.ext.block.ns = gfc_current_ns;
6259 select_type_push (expr1->symtree->n.sym);
6260 gfc_current_ns = ns;
6262 return MATCH_YES;
6264 cleanup:
6265 gfc_free_expr (expr1);
6266 gfc_free_expr (expr2);
6267 gfc_undo_symbols ();
6268 std::swap (ns, gfc_current_ns);
6269 gfc_free_namespace (ns);
6270 return m;
6274 /* Match a CASE statement. */
6276 match
6277 gfc_match_case (void)
6279 gfc_case *c, *head, *tail;
6280 match m;
6282 head = tail = NULL;
6284 if (gfc_current_state () != COMP_SELECT)
6286 gfc_error ("Unexpected CASE statement at %C");
6287 return MATCH_ERROR;
6290 if (gfc_match ("% default") == MATCH_YES)
6292 m = match_case_eos ();
6293 if (m == MATCH_NO)
6294 goto syntax;
6295 if (m == MATCH_ERROR)
6296 goto cleanup;
6298 new_st.op = EXEC_SELECT;
6299 c = gfc_get_case ();
6300 c->where = gfc_current_locus;
6301 new_st.ext.block.case_list = c;
6302 return MATCH_YES;
6305 if (gfc_match_char ('(') != MATCH_YES)
6306 goto syntax;
6308 for (;;)
6310 if (match_case_selector (&c) == MATCH_ERROR)
6311 goto cleanup;
6313 if (head == NULL)
6314 head = c;
6315 else
6316 tail->next = c;
6318 tail = c;
6320 if (gfc_match_char (')') == MATCH_YES)
6321 break;
6322 if (gfc_match_char (',') != MATCH_YES)
6323 goto syntax;
6326 m = match_case_eos ();
6327 if (m == MATCH_NO)
6328 goto syntax;
6329 if (m == MATCH_ERROR)
6330 goto cleanup;
6332 new_st.op = EXEC_SELECT;
6333 new_st.ext.block.case_list = head;
6335 return MATCH_YES;
6337 syntax:
6338 gfc_error ("Syntax error in CASE specification at %C");
6340 cleanup:
6341 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
6342 return MATCH_ERROR;
6346 /* Match a TYPE IS statement. */
6348 match
6349 gfc_match_type_is (void)
6351 gfc_case *c = NULL;
6352 match m;
6354 if (gfc_current_state () != COMP_SELECT_TYPE)
6356 gfc_error ("Unexpected TYPE IS statement at %C");
6357 return MATCH_ERROR;
6360 if (gfc_match_char ('(') != MATCH_YES)
6361 goto syntax;
6363 c = gfc_get_case ();
6364 c->where = gfc_current_locus;
6366 m = gfc_match_type_spec (&c->ts);
6367 if (m == MATCH_NO)
6368 goto syntax;
6369 if (m == MATCH_ERROR)
6370 goto cleanup;
6372 if (gfc_match_char (')') != MATCH_YES)
6373 goto syntax;
6375 m = match_case_eos ();
6376 if (m == MATCH_NO)
6377 goto syntax;
6378 if (m == MATCH_ERROR)
6379 goto cleanup;
6381 new_st.op = EXEC_SELECT_TYPE;
6382 new_st.ext.block.case_list = c;
6384 if (c->ts.type == BT_DERIVED && c->ts.u.derived
6385 && (c->ts.u.derived->attr.sequence
6386 || c->ts.u.derived->attr.is_bind_c))
6388 gfc_error ("The type-spec shall not specify a sequence derived "
6389 "type or a type with the BIND attribute in SELECT "
6390 "TYPE at %C [F2003:C815]");
6391 return MATCH_ERROR;
6394 if (c->ts.type == BT_DERIVED
6395 && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
6396 && gfc_spec_list_type (type_param_spec_list, c->ts.u.derived)
6397 != SPEC_ASSUMED)
6399 gfc_error ("All the LEN type parameters in the TYPE IS statement "
6400 "at %C must be ASSUMED");
6401 return MATCH_ERROR;
6404 /* Create temporary variable. */
6405 select_type_set_tmp (&c->ts);
6407 return MATCH_YES;
6409 syntax:
6410 gfc_error ("Syntax error in TYPE IS specification at %C");
6412 cleanup:
6413 if (c != NULL)
6414 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6415 return MATCH_ERROR;
6419 /* Match a CLASS IS or CLASS DEFAULT statement. */
6421 match
6422 gfc_match_class_is (void)
6424 gfc_case *c = NULL;
6425 match m;
6427 if (gfc_current_state () != COMP_SELECT_TYPE)
6428 return MATCH_NO;
6430 if (gfc_match ("% default") == MATCH_YES)
6432 m = match_case_eos ();
6433 if (m == MATCH_NO)
6434 goto syntax;
6435 if (m == MATCH_ERROR)
6436 goto cleanup;
6438 new_st.op = EXEC_SELECT_TYPE;
6439 c = gfc_get_case ();
6440 c->where = gfc_current_locus;
6441 c->ts.type = BT_UNKNOWN;
6442 new_st.ext.block.case_list = c;
6443 select_type_set_tmp (NULL);
6444 return MATCH_YES;
6447 m = gfc_match ("% is");
6448 if (m == MATCH_NO)
6449 goto syntax;
6450 if (m == MATCH_ERROR)
6451 goto cleanup;
6453 if (gfc_match_char ('(') != MATCH_YES)
6454 goto syntax;
6456 c = gfc_get_case ();
6457 c->where = gfc_current_locus;
6459 m = match_derived_type_spec (&c->ts);
6460 if (m == MATCH_NO)
6461 goto syntax;
6462 if (m == MATCH_ERROR)
6463 goto cleanup;
6465 if (c->ts.type == BT_DERIVED)
6466 c->ts.type = BT_CLASS;
6468 if (gfc_match_char (')') != MATCH_YES)
6469 goto syntax;
6471 m = match_case_eos ();
6472 if (m == MATCH_NO)
6473 goto syntax;
6474 if (m == MATCH_ERROR)
6475 goto cleanup;
6477 new_st.op = EXEC_SELECT_TYPE;
6478 new_st.ext.block.case_list = c;
6480 /* Create temporary variable. */
6481 select_type_set_tmp (&c->ts);
6483 return MATCH_YES;
6485 syntax:
6486 gfc_error ("Syntax error in CLASS IS specification at %C");
6488 cleanup:
6489 if (c != NULL)
6490 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6491 return MATCH_ERROR;
6495 /********************* WHERE subroutines ********************/
6497 /* Match the rest of a simple WHERE statement that follows an IF statement.
6500 static match
6501 match_simple_where (void)
6503 gfc_expr *expr;
6504 gfc_code *c;
6505 match m;
6507 m = gfc_match (" ( %e )", &expr);
6508 if (m != MATCH_YES)
6509 return m;
6511 m = gfc_match_assignment ();
6512 if (m == MATCH_NO)
6513 goto syntax;
6514 if (m == MATCH_ERROR)
6515 goto cleanup;
6517 if (gfc_match_eos () != MATCH_YES)
6518 goto syntax;
6520 c = gfc_get_code (EXEC_WHERE);
6521 c->expr1 = expr;
6523 c->next = XCNEW (gfc_code);
6524 *c->next = new_st;
6525 c->next->loc = gfc_current_locus;
6526 gfc_clear_new_st ();
6528 new_st.op = EXEC_WHERE;
6529 new_st.block = c;
6531 return MATCH_YES;
6533 syntax:
6534 gfc_syntax_error (ST_WHERE);
6536 cleanup:
6537 gfc_free_expr (expr);
6538 return MATCH_ERROR;
6542 /* Match a WHERE statement. */
6544 match
6545 gfc_match_where (gfc_statement *st)
6547 gfc_expr *expr;
6548 match m0, m;
6549 gfc_code *c;
6551 m0 = gfc_match_label ();
6552 if (m0 == MATCH_ERROR)
6553 return m0;
6555 m = gfc_match (" where ( %e )", &expr);
6556 if (m != MATCH_YES)
6557 return m;
6559 if (gfc_match_eos () == MATCH_YES)
6561 *st = ST_WHERE_BLOCK;
6562 new_st.op = EXEC_WHERE;
6563 new_st.expr1 = expr;
6564 return MATCH_YES;
6567 m = gfc_match_assignment ();
6568 if (m == MATCH_NO)
6569 gfc_syntax_error (ST_WHERE);
6571 if (m != MATCH_YES)
6573 gfc_free_expr (expr);
6574 return MATCH_ERROR;
6577 /* We've got a simple WHERE statement. */
6578 *st = ST_WHERE;
6579 c = gfc_get_code (EXEC_WHERE);
6580 c->expr1 = expr;
6582 /* Put in the assignment. It will not be processed by add_statement, so we
6583 need to copy the location here. */
6585 c->next = XCNEW (gfc_code);
6586 *c->next = new_st;
6587 c->next->loc = gfc_current_locus;
6588 gfc_clear_new_st ();
6590 new_st.op = EXEC_WHERE;
6591 new_st.block = c;
6593 return MATCH_YES;
6597 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
6598 new_st if successful. */
6600 match
6601 gfc_match_elsewhere (void)
6603 char name[GFC_MAX_SYMBOL_LEN + 1];
6604 gfc_expr *expr;
6605 match m;
6607 if (gfc_current_state () != COMP_WHERE)
6609 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
6610 return MATCH_ERROR;
6613 expr = NULL;
6615 if (gfc_match_char ('(') == MATCH_YES)
6617 m = gfc_match_expr (&expr);
6618 if (m == MATCH_NO)
6619 goto syntax;
6620 if (m == MATCH_ERROR)
6621 return MATCH_ERROR;
6623 if (gfc_match_char (')') != MATCH_YES)
6624 goto syntax;
6627 if (gfc_match_eos () != MATCH_YES)
6629 /* Only makes sense if we have a where-construct-name. */
6630 if (!gfc_current_block ())
6632 m = MATCH_ERROR;
6633 goto cleanup;
6635 /* Better be a name at this point. */
6636 m = gfc_match_name (name);
6637 if (m == MATCH_NO)
6638 goto syntax;
6639 if (m == MATCH_ERROR)
6640 goto cleanup;
6642 if (gfc_match_eos () != MATCH_YES)
6643 goto syntax;
6645 if (strcmp (name, gfc_current_block ()->name) != 0)
6647 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
6648 name, gfc_current_block ()->name);
6649 goto cleanup;
6653 new_st.op = EXEC_WHERE;
6654 new_st.expr1 = expr;
6655 return MATCH_YES;
6657 syntax:
6658 gfc_syntax_error (ST_ELSEWHERE);
6660 cleanup:
6661 gfc_free_expr (expr);
6662 return MATCH_ERROR;