Daily bump.
[official-gcc.git] / gcc / fortran / match.c
blob1ab0e0fad9a6392edb5aad7756753b7c654e8630
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);
1353 if (m != MATCH_YES)
1355 gfc_current_locus = old_loc;
1356 gfc_free_expr (lvalue);
1357 gfc_free_expr (rvalue);
1358 return m;
1361 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1363 new_st.op = EXEC_ASSIGN;
1364 new_st.expr1 = lvalue;
1365 new_st.expr2 = rvalue;
1367 gfc_check_do_variable (lvalue->symtree);
1369 return MATCH_YES;
1373 /* Match a pointer assignment statement. */
1375 match
1376 gfc_match_pointer_assignment (void)
1378 gfc_expr *lvalue, *rvalue;
1379 locus old_loc;
1380 match m;
1382 old_loc = gfc_current_locus;
1384 lvalue = rvalue = NULL;
1385 gfc_matching_ptr_assignment = 0;
1386 gfc_matching_procptr_assignment = 0;
1388 m = gfc_match (" %v =>", &lvalue);
1389 if (m != MATCH_YES)
1391 m = MATCH_NO;
1392 goto cleanup;
1395 if (lvalue->symtree->n.sym->attr.proc_pointer
1396 || gfc_is_proc_ptr_comp (lvalue))
1397 gfc_matching_procptr_assignment = 1;
1398 else
1399 gfc_matching_ptr_assignment = 1;
1401 m = gfc_match (" %e%t", &rvalue);
1402 gfc_matching_ptr_assignment = 0;
1403 gfc_matching_procptr_assignment = 0;
1404 if (m != MATCH_YES)
1405 goto cleanup;
1407 new_st.op = EXEC_POINTER_ASSIGN;
1408 new_st.expr1 = lvalue;
1409 new_st.expr2 = rvalue;
1411 return MATCH_YES;
1413 cleanup:
1414 gfc_current_locus = old_loc;
1415 gfc_free_expr (lvalue);
1416 gfc_free_expr (rvalue);
1417 return m;
1421 /* We try to match an easy arithmetic IF statement. This only happens
1422 when just after having encountered a simple IF statement. This code
1423 is really duplicate with parts of the gfc_match_if code, but this is
1424 *much* easier. */
1426 static match
1427 match_arithmetic_if (void)
1429 gfc_st_label *l1, *l2, *l3;
1430 gfc_expr *expr;
1431 match m;
1433 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1434 if (m != MATCH_YES)
1435 return m;
1437 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1438 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1439 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1441 gfc_free_expr (expr);
1442 return MATCH_ERROR;
1445 if (!gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
1446 "Arithmetic IF statement at %C"))
1447 return MATCH_ERROR;
1449 new_st.op = EXEC_ARITHMETIC_IF;
1450 new_st.expr1 = expr;
1451 new_st.label1 = l1;
1452 new_st.label2 = l2;
1453 new_st.label3 = l3;
1455 return MATCH_YES;
1459 /* The IF statement is a bit of a pain. First of all, there are three
1460 forms of it, the simple IF, the IF that starts a block and the
1461 arithmetic IF.
1463 There is a problem with the simple IF and that is the fact that we
1464 only have a single level of undo information on symbols. What this
1465 means is for a simple IF, we must re-match the whole IF statement
1466 multiple times in order to guarantee that the symbol table ends up
1467 in the proper state. */
1469 static match match_simple_forall (void);
1470 static match match_simple_where (void);
1472 match
1473 gfc_match_if (gfc_statement *if_type)
1475 gfc_expr *expr;
1476 gfc_st_label *l1, *l2, *l3;
1477 locus old_loc, old_loc2;
1478 gfc_code *p;
1479 match m, n;
1481 n = gfc_match_label ();
1482 if (n == MATCH_ERROR)
1483 return n;
1485 old_loc = gfc_current_locus;
1487 m = gfc_match (" if ( %e", &expr);
1488 if (m != MATCH_YES)
1489 return m;
1491 old_loc2 = gfc_current_locus;
1492 gfc_current_locus = old_loc;
1494 if (gfc_match_parens () == MATCH_ERROR)
1495 return MATCH_ERROR;
1497 gfc_current_locus = old_loc2;
1499 if (gfc_match_char (')') != MATCH_YES)
1501 gfc_error ("Syntax error in IF-expression at %C");
1502 gfc_free_expr (expr);
1503 return MATCH_ERROR;
1506 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1508 if (m == MATCH_YES)
1510 if (n == MATCH_YES)
1512 gfc_error ("Block label not appropriate for arithmetic IF "
1513 "statement at %C");
1514 gfc_free_expr (expr);
1515 return MATCH_ERROR;
1518 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1519 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1520 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1522 gfc_free_expr (expr);
1523 return MATCH_ERROR;
1526 if (!gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
1527 "Arithmetic IF statement at %C"))
1528 return MATCH_ERROR;
1530 new_st.op = EXEC_ARITHMETIC_IF;
1531 new_st.expr1 = expr;
1532 new_st.label1 = l1;
1533 new_st.label2 = l2;
1534 new_st.label3 = l3;
1536 *if_type = ST_ARITHMETIC_IF;
1537 return MATCH_YES;
1540 if (gfc_match (" then%t") == MATCH_YES)
1542 new_st.op = EXEC_IF;
1543 new_st.expr1 = expr;
1544 *if_type = ST_IF_BLOCK;
1545 return MATCH_YES;
1548 if (n == MATCH_YES)
1550 gfc_error ("Block label is not appropriate for IF statement at %C");
1551 gfc_free_expr (expr);
1552 return MATCH_ERROR;
1555 /* At this point the only thing left is a simple IF statement. At
1556 this point, n has to be MATCH_NO, so we don't have to worry about
1557 re-matching a block label. From what we've got so far, try
1558 matching an assignment. */
1560 *if_type = ST_SIMPLE_IF;
1562 m = gfc_match_assignment ();
1563 if (m == MATCH_YES)
1564 goto got_match;
1566 gfc_free_expr (expr);
1567 gfc_undo_symbols ();
1568 gfc_current_locus = old_loc;
1570 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1571 assignment was found. For MATCH_NO, continue to call the various
1572 matchers. */
1573 if (m == MATCH_ERROR)
1574 return MATCH_ERROR;
1576 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1578 m = gfc_match_pointer_assignment ();
1579 if (m == MATCH_YES)
1580 goto got_match;
1582 gfc_free_expr (expr);
1583 gfc_undo_symbols ();
1584 gfc_current_locus = old_loc;
1586 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1588 /* Look at the next keyword to see which matcher to call. Matching
1589 the keyword doesn't affect the symbol table, so we don't have to
1590 restore between tries. */
1592 #define match(string, subr, statement) \
1593 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1595 gfc_clear_error ();
1597 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1598 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1599 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1600 match ("call", gfc_match_call, ST_CALL)
1601 match ("change team", gfc_match_change_team, ST_CHANGE_TEAM)
1602 match ("close", gfc_match_close, ST_CLOSE)
1603 match ("continue", gfc_match_continue, ST_CONTINUE)
1604 match ("cycle", gfc_match_cycle, ST_CYCLE)
1605 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1606 match ("end file", gfc_match_endfile, ST_END_FILE)
1607 match ("end team", gfc_match_end_team, ST_END_TEAM)
1608 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1609 match ("event post", gfc_match_event_post, ST_EVENT_POST)
1610 match ("event wait", gfc_match_event_wait, ST_EVENT_WAIT)
1611 match ("exit", gfc_match_exit, ST_EXIT)
1612 match ("fail image", gfc_match_fail_image, ST_FAIL_IMAGE)
1613 match ("flush", gfc_match_flush, ST_FLUSH)
1614 match ("forall", match_simple_forall, ST_FORALL)
1615 match ("form team", gfc_match_form_team, ST_FORM_TEAM)
1616 match ("go to", gfc_match_goto, ST_GOTO)
1617 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1618 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1619 match ("lock", gfc_match_lock, ST_LOCK)
1620 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1621 match ("open", gfc_match_open, ST_OPEN)
1622 match ("pause", gfc_match_pause, ST_NONE)
1623 match ("print", gfc_match_print, ST_WRITE)
1624 match ("read", gfc_match_read, ST_READ)
1625 match ("return", gfc_match_return, ST_RETURN)
1626 match ("rewind", gfc_match_rewind, ST_REWIND)
1627 match ("stop", gfc_match_stop, ST_STOP)
1628 match ("wait", gfc_match_wait, ST_WAIT)
1629 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1630 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1631 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1632 match ("sync team", gfc_match_sync_team, ST_SYNC_TEAM)
1633 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1634 match ("where", match_simple_where, ST_WHERE)
1635 match ("write", gfc_match_write, ST_WRITE)
1637 if (flag_dec)
1638 match ("type", gfc_match_print, ST_WRITE)
1640 /* The gfc_match_assignment() above may have returned a MATCH_NO
1641 where the assignment was to a named constant. Check that
1642 special case here. */
1643 m = gfc_match_assignment ();
1644 if (m == MATCH_NO)
1646 gfc_error ("Cannot assign to a named constant at %C");
1647 gfc_free_expr (expr);
1648 gfc_undo_symbols ();
1649 gfc_current_locus = old_loc;
1650 return MATCH_ERROR;
1653 /* All else has failed, so give up. See if any of the matchers has
1654 stored an error message of some sort. */
1655 if (!gfc_error_check ())
1656 gfc_error ("Unclassifiable statement in IF-clause at %C");
1658 gfc_free_expr (expr);
1659 return MATCH_ERROR;
1661 got_match:
1662 if (m == MATCH_NO)
1663 gfc_error ("Syntax error in IF-clause at %C");
1664 if (m != MATCH_YES)
1666 gfc_free_expr (expr);
1667 return MATCH_ERROR;
1670 /* At this point, we've matched the single IF and the action clause
1671 is in new_st. Rearrange things so that the IF statement appears
1672 in new_st. */
1674 p = gfc_get_code (EXEC_IF);
1675 p->next = XCNEW (gfc_code);
1676 *p->next = new_st;
1677 p->next->loc = gfc_current_locus;
1679 p->expr1 = expr;
1681 gfc_clear_new_st ();
1683 new_st.op = EXEC_IF;
1684 new_st.block = p;
1686 return MATCH_YES;
1689 #undef match
1692 /* Match an ELSE statement. */
1694 match
1695 gfc_match_else (void)
1697 char name[GFC_MAX_SYMBOL_LEN + 1];
1699 if (gfc_match_eos () == MATCH_YES)
1700 return MATCH_YES;
1702 if (gfc_match_name (name) != MATCH_YES
1703 || gfc_current_block () == NULL
1704 || gfc_match_eos () != MATCH_YES)
1706 gfc_error ("Unexpected junk after ELSE statement at %C");
1707 return MATCH_ERROR;
1710 if (strcmp (name, gfc_current_block ()->name) != 0)
1712 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1713 name, gfc_current_block ()->name);
1714 return MATCH_ERROR;
1717 return MATCH_YES;
1721 /* Match an ELSE IF statement. */
1723 match
1724 gfc_match_elseif (void)
1726 char name[GFC_MAX_SYMBOL_LEN + 1];
1727 gfc_expr *expr;
1728 match m;
1730 m = gfc_match (" ( %e ) then", &expr);
1731 if (m != MATCH_YES)
1732 return m;
1734 if (gfc_match_eos () == MATCH_YES)
1735 goto done;
1737 if (gfc_match_name (name) != MATCH_YES
1738 || gfc_current_block () == NULL
1739 || gfc_match_eos () != MATCH_YES)
1741 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1742 goto cleanup;
1745 if (strcmp (name, gfc_current_block ()->name) != 0)
1747 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1748 name, gfc_current_block ()->name);
1749 goto cleanup;
1752 done:
1753 new_st.op = EXEC_IF;
1754 new_st.expr1 = expr;
1755 return MATCH_YES;
1757 cleanup:
1758 gfc_free_expr (expr);
1759 return MATCH_ERROR;
1763 /* Free a gfc_iterator structure. */
1765 void
1766 gfc_free_iterator (gfc_iterator *iter, int flag)
1769 if (iter == NULL)
1770 return;
1772 gfc_free_expr (iter->var);
1773 gfc_free_expr (iter->start);
1774 gfc_free_expr (iter->end);
1775 gfc_free_expr (iter->step);
1777 if (flag)
1778 free (iter);
1782 /* Match a CRITICAL statement. */
1783 match
1784 gfc_match_critical (void)
1786 gfc_st_label *label = NULL;
1788 if (gfc_match_label () == MATCH_ERROR)
1789 return MATCH_ERROR;
1791 if (gfc_match (" critical") != MATCH_YES)
1792 return MATCH_NO;
1794 if (gfc_match_st_label (&label) == MATCH_ERROR)
1795 return MATCH_ERROR;
1797 if (gfc_match_eos () != MATCH_YES)
1799 gfc_syntax_error (ST_CRITICAL);
1800 return MATCH_ERROR;
1803 if (gfc_pure (NULL))
1805 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1806 return MATCH_ERROR;
1809 if (gfc_find_state (COMP_DO_CONCURRENT))
1811 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1812 "block");
1813 return MATCH_ERROR;
1816 gfc_unset_implicit_pure (NULL);
1818 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
1819 return MATCH_ERROR;
1821 if (flag_coarray == GFC_FCOARRAY_NONE)
1823 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1824 "enable");
1825 return MATCH_ERROR;
1828 if (gfc_find_state (COMP_CRITICAL))
1830 gfc_error ("Nested CRITICAL block at %C");
1831 return MATCH_ERROR;
1834 new_st.op = EXEC_CRITICAL;
1836 if (label != NULL
1837 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
1838 return MATCH_ERROR;
1840 return MATCH_YES;
1844 /* Match a BLOCK statement. */
1846 match
1847 gfc_match_block (void)
1849 match m;
1851 if (gfc_match_label () == MATCH_ERROR)
1852 return MATCH_ERROR;
1854 if (gfc_match (" block") != MATCH_YES)
1855 return MATCH_NO;
1857 /* For this to be a correct BLOCK statement, the line must end now. */
1858 m = gfc_match_eos ();
1859 if (m == MATCH_ERROR)
1860 return MATCH_ERROR;
1861 if (m == MATCH_NO)
1862 return MATCH_NO;
1864 return MATCH_YES;
1868 /* Match an ASSOCIATE statement. */
1870 match
1871 gfc_match_associate (void)
1873 if (gfc_match_label () == MATCH_ERROR)
1874 return MATCH_ERROR;
1876 if (gfc_match (" associate") != MATCH_YES)
1877 return MATCH_NO;
1879 /* Match the association list. */
1880 if (gfc_match_char ('(') != MATCH_YES)
1882 gfc_error ("Expected association list at %C");
1883 return MATCH_ERROR;
1885 new_st.ext.block.assoc = NULL;
1886 while (true)
1888 gfc_association_list* newAssoc = gfc_get_association_list ();
1889 gfc_association_list* a;
1891 /* Match the next association. */
1892 if (gfc_match (" %n => %e", newAssoc->name, &newAssoc->target)
1893 != MATCH_YES)
1895 /* Have another go, allowing for procedure pointer selectors. */
1896 gfc_matching_procptr_assignment = 1;
1897 if (gfc_match (" %n => %e", newAssoc->name, &newAssoc->target)
1898 != MATCH_YES)
1900 gfc_error ("Expected association at %C");
1901 goto assocListError;
1903 gfc_matching_procptr_assignment = 0;
1905 newAssoc->where = gfc_current_locus;
1907 /* Check that the current name is not yet in the list. */
1908 for (a = new_st.ext.block.assoc; a; a = a->next)
1909 if (!strcmp (a->name, newAssoc->name))
1911 gfc_error ("Duplicate name %qs in association at %C",
1912 newAssoc->name);
1913 goto assocListError;
1916 /* The target expression must not be coindexed. */
1917 if (gfc_is_coindexed (newAssoc->target))
1919 gfc_error ("Association target at %C must not be coindexed");
1920 goto assocListError;
1923 /* The `variable' field is left blank for now; because the target is not
1924 yet resolved, we can't use gfc_has_vector_subscript to determine it
1925 for now. This is set during resolution. */
1927 /* Put it into the list. */
1928 newAssoc->next = new_st.ext.block.assoc;
1929 new_st.ext.block.assoc = newAssoc;
1931 /* Try next one or end if closing parenthesis is found. */
1932 gfc_gobble_whitespace ();
1933 if (gfc_peek_char () == ')')
1934 break;
1935 if (gfc_match_char (',') != MATCH_YES)
1937 gfc_error ("Expected %<)%> or %<,%> at %C");
1938 return MATCH_ERROR;
1941 continue;
1943 assocListError:
1944 free (newAssoc);
1945 goto error;
1947 if (gfc_match_char (')') != MATCH_YES)
1949 /* This should never happen as we peek above. */
1950 gcc_unreachable ();
1953 if (gfc_match_eos () != MATCH_YES)
1955 gfc_error ("Junk after ASSOCIATE statement at %C");
1956 goto error;
1959 return MATCH_YES;
1961 error:
1962 gfc_free_association_list (new_st.ext.block.assoc);
1963 return MATCH_ERROR;
1967 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1968 an accessible derived type. */
1970 static match
1971 match_derived_type_spec (gfc_typespec *ts)
1973 char name[GFC_MAX_SYMBOL_LEN + 1];
1974 locus old_locus;
1975 gfc_symbol *derived, *der_type;
1976 match m = MATCH_YES;
1977 gfc_actual_arglist *decl_type_param_list = NULL;
1978 bool is_pdt_template = false;
1980 old_locus = gfc_current_locus;
1982 if (gfc_match ("%n", name) != MATCH_YES)
1984 gfc_current_locus = old_locus;
1985 return MATCH_NO;
1988 gfc_find_symbol (name, NULL, 1, &derived);
1990 /* Match the PDT spec list, if there. */
1991 if (derived && derived->attr.flavor == FL_PROCEDURE)
1993 gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &der_type);
1994 is_pdt_template = der_type
1995 && der_type->attr.flavor == FL_DERIVED
1996 && der_type->attr.pdt_template;
1999 if (is_pdt_template)
2000 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
2002 if (m == MATCH_ERROR)
2004 gfc_free_actual_arglist (decl_type_param_list);
2005 return m;
2008 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
2009 derived = gfc_find_dt_in_generic (derived);
2011 /* If this is a PDT, find the specific instance. */
2012 if (m == MATCH_YES && is_pdt_template)
2014 gfc_namespace *old_ns;
2016 old_ns = gfc_current_ns;
2017 while (gfc_current_ns && gfc_current_ns->parent)
2018 gfc_current_ns = gfc_current_ns->parent;
2020 if (type_param_spec_list)
2021 gfc_free_actual_arglist (type_param_spec_list);
2022 m = gfc_get_pdt_instance (decl_type_param_list, &der_type,
2023 &type_param_spec_list);
2024 gfc_free_actual_arglist (decl_type_param_list);
2026 if (m != MATCH_YES)
2027 return m;
2028 derived = der_type;
2029 gcc_assert (!derived->attr.pdt_template && derived->attr.pdt_type);
2030 gfc_set_sym_referenced (derived);
2032 gfc_current_ns = old_ns;
2035 if (derived && derived->attr.flavor == FL_DERIVED)
2037 ts->type = BT_DERIVED;
2038 ts->u.derived = derived;
2039 return MATCH_YES;
2042 gfc_current_locus = old_locus;
2043 return MATCH_NO;
2047 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2048 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
2049 It only includes the intrinsic types from the Fortran 2003 standard
2050 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2051 the implicit_flag is not needed, so it was removed. Derived types are
2052 identified by their name alone. */
2054 match
2055 gfc_match_type_spec (gfc_typespec *ts)
2057 match m;
2058 locus old_locus;
2059 char c, name[GFC_MAX_SYMBOL_LEN + 1];
2061 gfc_clear_ts (ts);
2062 gfc_gobble_whitespace ();
2063 old_locus = gfc_current_locus;
2065 /* If c isn't [a-z], then return immediately. */
2066 c = gfc_peek_ascii_char ();
2067 if (!ISALPHA(c))
2068 return MATCH_NO;
2070 type_param_spec_list = NULL;
2072 if (match_derived_type_spec (ts) == MATCH_YES)
2074 /* Enforce F03:C401. */
2075 if (ts->u.derived->attr.abstract)
2077 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2078 ts->u.derived->name, &old_locus);
2079 return MATCH_ERROR;
2081 return MATCH_YES;
2084 if (gfc_match ("integer") == MATCH_YES)
2086 ts->type = BT_INTEGER;
2087 ts->kind = gfc_default_integer_kind;
2088 goto kind_selector;
2091 if (gfc_match ("double precision") == MATCH_YES)
2093 ts->type = BT_REAL;
2094 ts->kind = gfc_default_double_kind;
2095 return MATCH_YES;
2098 if (gfc_match ("complex") == MATCH_YES)
2100 ts->type = BT_COMPLEX;
2101 ts->kind = gfc_default_complex_kind;
2102 goto kind_selector;
2105 if (gfc_match ("character") == MATCH_YES)
2107 ts->type = BT_CHARACTER;
2109 m = gfc_match_char_spec (ts);
2110 if (ts->u.cl && ts->u.cl->length)
2111 gfc_resolve_expr (ts->u.cl->length);
2113 if (m == MATCH_NO)
2114 m = MATCH_YES;
2116 return m;
2119 /* REAL is a real pain because it can be a type, intrinsic subprogram,
2120 or list item in a type-list of an OpenMP reduction clause. Need to
2121 differentiate REAL([KIND]=scalar-int-initialization-expr) from
2122 REAL(A,[KIND]) and REAL(KIND,A). Logically, when this code was
2123 written the use of LOGICAL as a type-spec or intrinsic subprogram
2124 was overlooked. */
2126 m = gfc_match (" %n", name);
2127 if (m == MATCH_YES
2128 && (strcmp (name, "real") == 0 || strcmp (name, "logical") == 0))
2130 char c;
2131 gfc_expr *e;
2132 locus where;
2134 if (*name == 'r')
2136 ts->type = BT_REAL;
2137 ts->kind = gfc_default_real_kind;
2139 else
2141 ts->type = BT_LOGICAL;
2142 ts->kind = gfc_default_logical_kind;
2145 gfc_gobble_whitespace ();
2147 /* Prevent REAL*4, etc. */
2148 c = gfc_peek_ascii_char ();
2149 if (c == '*')
2151 gfc_error ("Invalid type-spec at %C");
2152 return MATCH_ERROR;
2155 /* Found leading colon in REAL::, a trailing ')' in for example
2156 TYPE IS (REAL), or REAL, for an OpenMP list-item. */
2157 if (c == ':' || c == ')' || (flag_openmp && c == ','))
2158 return MATCH_YES;
2160 /* Found something other than the opening '(' in REAL(... */
2161 if (c != '(')
2162 return MATCH_NO;
2163 else
2164 gfc_next_char (); /* Burn the '('. */
2166 /* Look for the optional KIND=. */
2167 where = gfc_current_locus;
2168 m = gfc_match ("%n", name);
2169 if (m == MATCH_YES)
2171 gfc_gobble_whitespace ();
2172 c = gfc_next_char ();
2173 if (c == '=')
2175 if (strcmp(name, "a") == 0 || strcmp(name, "l") == 0)
2176 return MATCH_NO;
2177 else if (strcmp(name, "kind") == 0)
2178 goto found;
2179 else
2180 return MATCH_ERROR;
2182 else
2183 gfc_current_locus = where;
2185 else
2186 gfc_current_locus = where;
2188 found:
2190 m = gfc_match_init_expr (&e);
2191 if (m == MATCH_NO || m == MATCH_ERROR)
2192 return MATCH_NO;
2194 /* If a comma appears, it is an intrinsic subprogram. */
2195 gfc_gobble_whitespace ();
2196 c = gfc_peek_ascii_char ();
2197 if (c == ',')
2199 gfc_free_expr (e);
2200 return MATCH_NO;
2203 /* If ')' appears, we have REAL(initialization-expr), here check for
2204 a scalar integer initialization-expr and valid kind parameter. */
2205 if (c == ')')
2207 if (e->ts.type != BT_INTEGER || e->rank > 0)
2209 gfc_free_expr (e);
2210 return MATCH_NO;
2213 gfc_next_char (); /* Burn the ')'. */
2214 ts->kind = (int) mpz_get_si (e->value.integer);
2215 if (gfc_validate_kind (ts->type, ts->kind , true) == -1)
2217 gfc_error ("Invalid type-spec at %C");
2218 return MATCH_ERROR;
2221 gfc_free_expr (e);
2223 return MATCH_YES;
2227 /* If a type is not matched, simply return MATCH_NO. */
2228 gfc_current_locus = old_locus;
2229 return MATCH_NO;
2231 kind_selector:
2233 gfc_gobble_whitespace ();
2235 /* This prevents INTEGER*4, etc. */
2236 if (gfc_peek_ascii_char () == '*')
2238 gfc_error ("Invalid type-spec at %C");
2239 return MATCH_ERROR;
2242 m = gfc_match_kind_spec (ts, false);
2244 /* No kind specifier found. */
2245 if (m == MATCH_NO)
2246 m = MATCH_YES;
2248 return m;
2252 /******************** FORALL subroutines ********************/
2254 /* Free a list of FORALL iterators. */
2256 void
2257 gfc_free_forall_iterator (gfc_forall_iterator *iter)
2259 gfc_forall_iterator *next;
2261 while (iter)
2263 next = iter->next;
2264 gfc_free_expr (iter->var);
2265 gfc_free_expr (iter->start);
2266 gfc_free_expr (iter->end);
2267 gfc_free_expr (iter->stride);
2268 free (iter);
2269 iter = next;
2274 /* Match an iterator as part of a FORALL statement. The format is:
2276 <var> = <start>:<end>[:<stride>]
2278 On MATCH_NO, the caller tests for the possibility that there is a
2279 scalar mask expression. */
2281 static match
2282 match_forall_iterator (gfc_forall_iterator **result)
2284 gfc_forall_iterator *iter;
2285 locus where;
2286 match m;
2288 where = gfc_current_locus;
2289 iter = XCNEW (gfc_forall_iterator);
2291 m = gfc_match_expr (&iter->var);
2292 if (m != MATCH_YES)
2293 goto cleanup;
2295 if (gfc_match_char ('=') != MATCH_YES
2296 || iter->var->expr_type != EXPR_VARIABLE)
2298 m = MATCH_NO;
2299 goto cleanup;
2302 m = gfc_match_expr (&iter->start);
2303 if (m != MATCH_YES)
2304 goto cleanup;
2306 if (gfc_match_char (':') != MATCH_YES)
2307 goto syntax;
2309 m = gfc_match_expr (&iter->end);
2310 if (m == MATCH_NO)
2311 goto syntax;
2312 if (m == MATCH_ERROR)
2313 goto cleanup;
2315 if (gfc_match_char (':') == MATCH_NO)
2316 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2317 else
2319 m = gfc_match_expr (&iter->stride);
2320 if (m == MATCH_NO)
2321 goto syntax;
2322 if (m == MATCH_ERROR)
2323 goto cleanup;
2326 /* Mark the iteration variable's symbol as used as a FORALL index. */
2327 iter->var->symtree->n.sym->forall_index = true;
2329 *result = iter;
2330 return MATCH_YES;
2332 syntax:
2333 gfc_error ("Syntax error in FORALL iterator at %C");
2334 m = MATCH_ERROR;
2336 cleanup:
2338 gfc_current_locus = where;
2339 gfc_free_forall_iterator (iter);
2340 return m;
2344 /* Match the header of a FORALL statement. */
2346 static match
2347 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2349 gfc_forall_iterator *head, *tail, *new_iter;
2350 gfc_expr *msk;
2351 match m;
2353 gfc_gobble_whitespace ();
2355 head = tail = NULL;
2356 msk = NULL;
2358 if (gfc_match_char ('(') != MATCH_YES)
2359 return MATCH_NO;
2361 m = match_forall_iterator (&new_iter);
2362 if (m == MATCH_ERROR)
2363 goto cleanup;
2364 if (m == MATCH_NO)
2365 goto syntax;
2367 head = tail = new_iter;
2369 for (;;)
2371 if (gfc_match_char (',') != MATCH_YES)
2372 break;
2374 m = match_forall_iterator (&new_iter);
2375 if (m == MATCH_ERROR)
2376 goto cleanup;
2378 if (m == MATCH_YES)
2380 tail->next = new_iter;
2381 tail = new_iter;
2382 continue;
2385 /* Have to have a mask expression. */
2387 m = gfc_match_expr (&msk);
2388 if (m == MATCH_NO)
2389 goto syntax;
2390 if (m == MATCH_ERROR)
2391 goto cleanup;
2393 break;
2396 if (gfc_match_char (')') == MATCH_NO)
2397 goto syntax;
2399 *phead = head;
2400 *mask = msk;
2401 return MATCH_YES;
2403 syntax:
2404 gfc_syntax_error (ST_FORALL);
2406 cleanup:
2407 gfc_free_expr (msk);
2408 gfc_free_forall_iterator (head);
2410 return MATCH_ERROR;
2413 /* Match the rest of a simple FORALL statement that follows an
2414 IF statement. */
2416 static match
2417 match_simple_forall (void)
2419 gfc_forall_iterator *head;
2420 gfc_expr *mask;
2421 gfc_code *c;
2422 match m;
2424 mask = NULL;
2425 head = NULL;
2426 c = NULL;
2428 m = match_forall_header (&head, &mask);
2430 if (m == MATCH_NO)
2431 goto syntax;
2432 if (m != MATCH_YES)
2433 goto cleanup;
2435 m = gfc_match_assignment ();
2437 if (m == MATCH_ERROR)
2438 goto cleanup;
2439 if (m == MATCH_NO)
2441 m = gfc_match_pointer_assignment ();
2442 if (m == MATCH_ERROR)
2443 goto cleanup;
2444 if (m == MATCH_NO)
2445 goto syntax;
2448 c = XCNEW (gfc_code);
2449 *c = new_st;
2450 c->loc = gfc_current_locus;
2452 if (gfc_match_eos () != MATCH_YES)
2453 goto syntax;
2455 gfc_clear_new_st ();
2456 new_st.op = EXEC_FORALL;
2457 new_st.expr1 = mask;
2458 new_st.ext.forall_iterator = head;
2459 new_st.block = gfc_get_code (EXEC_FORALL);
2460 new_st.block->next = c;
2462 return MATCH_YES;
2464 syntax:
2465 gfc_syntax_error (ST_FORALL);
2467 cleanup:
2468 gfc_free_forall_iterator (head);
2469 gfc_free_expr (mask);
2471 return MATCH_ERROR;
2475 /* Match a FORALL statement. */
2477 match
2478 gfc_match_forall (gfc_statement *st)
2480 gfc_forall_iterator *head;
2481 gfc_expr *mask;
2482 gfc_code *c;
2483 match m0, m;
2485 head = NULL;
2486 mask = NULL;
2487 c = NULL;
2489 m0 = gfc_match_label ();
2490 if (m0 == MATCH_ERROR)
2491 return MATCH_ERROR;
2493 m = gfc_match (" forall");
2494 if (m != MATCH_YES)
2495 return m;
2497 m = match_forall_header (&head, &mask);
2498 if (m == MATCH_ERROR)
2499 goto cleanup;
2500 if (m == MATCH_NO)
2501 goto syntax;
2503 if (gfc_match_eos () == MATCH_YES)
2505 *st = ST_FORALL_BLOCK;
2506 new_st.op = EXEC_FORALL;
2507 new_st.expr1 = mask;
2508 new_st.ext.forall_iterator = head;
2509 return MATCH_YES;
2512 m = gfc_match_assignment ();
2513 if (m == MATCH_ERROR)
2514 goto cleanup;
2515 if (m == MATCH_NO)
2517 m = gfc_match_pointer_assignment ();
2518 if (m == MATCH_ERROR)
2519 goto cleanup;
2520 if (m == MATCH_NO)
2521 goto syntax;
2524 c = XCNEW (gfc_code);
2525 *c = new_st;
2526 c->loc = gfc_current_locus;
2528 gfc_clear_new_st ();
2529 new_st.op = EXEC_FORALL;
2530 new_st.expr1 = mask;
2531 new_st.ext.forall_iterator = head;
2532 new_st.block = gfc_get_code (EXEC_FORALL);
2533 new_st.block->next = c;
2535 *st = ST_FORALL;
2536 return MATCH_YES;
2538 syntax:
2539 gfc_syntax_error (ST_FORALL);
2541 cleanup:
2542 gfc_free_forall_iterator (head);
2543 gfc_free_expr (mask);
2544 gfc_free_statements (c);
2545 return MATCH_NO;
2549 /* Match a DO statement. */
2551 match
2552 gfc_match_do (void)
2554 gfc_iterator iter, *ip;
2555 locus old_loc;
2556 gfc_st_label *label;
2557 match m;
2559 old_loc = gfc_current_locus;
2561 memset (&iter, '\0', sizeof (gfc_iterator));
2562 label = NULL;
2564 m = gfc_match_label ();
2565 if (m == MATCH_ERROR)
2566 return m;
2568 if (gfc_match (" do") != MATCH_YES)
2569 return MATCH_NO;
2571 m = gfc_match_st_label (&label);
2572 if (m == MATCH_ERROR)
2573 goto cleanup;
2575 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2577 if (gfc_match_eos () == MATCH_YES)
2579 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2580 new_st.op = EXEC_DO_WHILE;
2581 goto done;
2584 /* Match an optional comma, if no comma is found, a space is obligatory. */
2585 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2586 return MATCH_NO;
2588 /* Check for balanced parens. */
2590 if (gfc_match_parens () == MATCH_ERROR)
2591 return MATCH_ERROR;
2593 if (gfc_match (" concurrent") == MATCH_YES)
2595 gfc_forall_iterator *head;
2596 gfc_expr *mask;
2598 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
2599 return MATCH_ERROR;
2602 mask = NULL;
2603 head = NULL;
2604 m = match_forall_header (&head, &mask);
2606 if (m == MATCH_NO)
2607 return m;
2608 if (m == MATCH_ERROR)
2609 goto concurr_cleanup;
2611 if (gfc_match_eos () != MATCH_YES)
2612 goto concurr_cleanup;
2614 if (label != NULL
2615 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2616 goto concurr_cleanup;
2618 new_st.label1 = label;
2619 new_st.op = EXEC_DO_CONCURRENT;
2620 new_st.expr1 = mask;
2621 new_st.ext.forall_iterator = head;
2623 return MATCH_YES;
2625 concurr_cleanup:
2626 gfc_syntax_error (ST_DO);
2627 gfc_free_expr (mask);
2628 gfc_free_forall_iterator (head);
2629 return MATCH_ERROR;
2632 /* See if we have a DO WHILE. */
2633 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2635 new_st.op = EXEC_DO_WHILE;
2636 goto done;
2639 /* The abortive DO WHILE may have done something to the symbol
2640 table, so we start over. */
2641 gfc_undo_symbols ();
2642 gfc_current_locus = old_loc;
2644 gfc_match_label (); /* This won't error. */
2645 gfc_match (" do "); /* This will work. */
2647 gfc_match_st_label (&label); /* Can't error out. */
2648 gfc_match_char (','); /* Optional comma. */
2650 m = gfc_match_iterator (&iter, 0);
2651 if (m == MATCH_NO)
2652 return MATCH_NO;
2653 if (m == MATCH_ERROR)
2654 goto cleanup;
2656 iter.var->symtree->n.sym->attr.implied_index = 0;
2657 gfc_check_do_variable (iter.var->symtree);
2659 if (gfc_match_eos () != MATCH_YES)
2661 gfc_syntax_error (ST_DO);
2662 goto cleanup;
2665 new_st.op = EXEC_DO;
2667 done:
2668 if (label != NULL
2669 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2670 goto cleanup;
2672 new_st.label1 = label;
2674 if (new_st.op == EXEC_DO_WHILE)
2675 new_st.expr1 = iter.end;
2676 else
2678 new_st.ext.iterator = ip = gfc_get_iterator ();
2679 *ip = iter;
2682 return MATCH_YES;
2684 cleanup:
2685 gfc_free_iterator (&iter, 0);
2687 return MATCH_ERROR;
2691 /* Match an EXIT or CYCLE statement. */
2693 static match
2694 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2696 gfc_state_data *p, *o;
2697 gfc_symbol *sym;
2698 match m;
2699 int cnt;
2701 if (gfc_match_eos () == MATCH_YES)
2702 sym = NULL;
2703 else
2705 char name[GFC_MAX_SYMBOL_LEN + 1];
2706 gfc_symtree* stree;
2708 m = gfc_match ("% %n%t", name);
2709 if (m == MATCH_ERROR)
2710 return MATCH_ERROR;
2711 if (m == MATCH_NO)
2713 gfc_syntax_error (st);
2714 return MATCH_ERROR;
2717 /* Find the corresponding symbol. If there's a BLOCK statement
2718 between here and the label, it is not in gfc_current_ns but a parent
2719 namespace! */
2720 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2721 if (!stree)
2723 gfc_error ("Name %qs in %s statement at %C is unknown",
2724 name, gfc_ascii_statement (st));
2725 return MATCH_ERROR;
2728 sym = stree->n.sym;
2729 if (sym->attr.flavor != FL_LABEL)
2731 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2732 name, gfc_ascii_statement (st));
2733 return MATCH_ERROR;
2737 /* Find the loop specified by the label (or lack of a label). */
2738 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2739 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2740 o = p;
2741 else if (p->state == COMP_CRITICAL)
2743 gfc_error("%s statement at %C leaves CRITICAL construct",
2744 gfc_ascii_statement (st));
2745 return MATCH_ERROR;
2747 else if (p->state == COMP_DO_CONCURRENT
2748 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2750 /* F2008, C821 & C845. */
2751 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2752 gfc_ascii_statement (st));
2753 return MATCH_ERROR;
2755 else if ((sym && sym == p->sym)
2756 || (!sym && (p->state == COMP_DO
2757 || p->state == COMP_DO_CONCURRENT)))
2758 break;
2760 if (p == NULL)
2762 if (sym == NULL)
2763 gfc_error ("%s statement at %C is not within a construct",
2764 gfc_ascii_statement (st));
2765 else
2766 gfc_error ("%s statement at %C is not within construct %qs",
2767 gfc_ascii_statement (st), sym->name);
2769 return MATCH_ERROR;
2772 /* Special checks for EXIT from non-loop constructs. */
2773 switch (p->state)
2775 case COMP_DO:
2776 case COMP_DO_CONCURRENT:
2777 break;
2779 case COMP_CRITICAL:
2780 /* This is already handled above. */
2781 gcc_unreachable ();
2783 case COMP_ASSOCIATE:
2784 case COMP_BLOCK:
2785 case COMP_IF:
2786 case COMP_SELECT:
2787 case COMP_SELECT_TYPE:
2788 gcc_assert (sym);
2789 if (op == EXEC_CYCLE)
2791 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2792 " construct %qs", sym->name);
2793 return MATCH_ERROR;
2795 gcc_assert (op == EXEC_EXIT);
2796 if (!gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2797 " do-construct-name at %C"))
2798 return MATCH_ERROR;
2799 break;
2801 default:
2802 gfc_error ("%s statement at %C is not applicable to construct %qs",
2803 gfc_ascii_statement (st), sym->name);
2804 return MATCH_ERROR;
2807 if (o != NULL)
2809 gfc_error (is_oacc (p)
2810 ? G_("%s statement at %C leaving OpenACC structured block")
2811 : G_("%s statement at %C leaving OpenMP structured block"),
2812 gfc_ascii_statement (st));
2813 return MATCH_ERROR;
2816 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2817 o = o->previous;
2818 if (cnt > 0
2819 && o != NULL
2820 && o->state == COMP_OMP_STRUCTURED_BLOCK
2821 && (o->head->op == EXEC_OACC_LOOP
2822 || o->head->op == EXEC_OACC_PARALLEL_LOOP))
2824 int collapse = 1;
2825 gcc_assert (o->head->next != NULL
2826 && (o->head->next->op == EXEC_DO
2827 || o->head->next->op == EXEC_DO_WHILE)
2828 && o->previous != NULL
2829 && o->previous->tail->op == o->head->op);
2830 if (o->previous->tail->ext.omp_clauses != NULL
2831 && o->previous->tail->ext.omp_clauses->collapse > 1)
2832 collapse = o->previous->tail->ext.omp_clauses->collapse;
2833 if (st == ST_EXIT && cnt <= collapse)
2835 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2836 return MATCH_ERROR;
2838 if (st == ST_CYCLE && cnt < collapse)
2840 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2841 " !$ACC LOOP loop");
2842 return MATCH_ERROR;
2845 if (cnt > 0
2846 && o != NULL
2847 && (o->state == COMP_OMP_STRUCTURED_BLOCK)
2848 && (o->head->op == EXEC_OMP_DO
2849 || o->head->op == EXEC_OMP_PARALLEL_DO
2850 || o->head->op == EXEC_OMP_SIMD
2851 || o->head->op == EXEC_OMP_DO_SIMD
2852 || o->head->op == EXEC_OMP_PARALLEL_DO_SIMD))
2854 int count = 1;
2855 gcc_assert (o->head->next != NULL
2856 && (o->head->next->op == EXEC_DO
2857 || o->head->next->op == EXEC_DO_WHILE)
2858 && o->previous != NULL
2859 && o->previous->tail->op == o->head->op);
2860 if (o->previous->tail->ext.omp_clauses != NULL)
2862 if (o->previous->tail->ext.omp_clauses->collapse > 1)
2863 count = o->previous->tail->ext.omp_clauses->collapse;
2864 if (o->previous->tail->ext.omp_clauses->orderedc)
2865 count = o->previous->tail->ext.omp_clauses->orderedc;
2867 if (st == ST_EXIT && cnt <= count)
2869 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2870 return MATCH_ERROR;
2872 if (st == ST_CYCLE && cnt < count)
2874 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2875 " !$OMP DO loop");
2876 return MATCH_ERROR;
2880 /* Save the first statement in the construct - needed by the backend. */
2881 new_st.ext.which_construct = p->construct;
2883 new_st.op = op;
2885 return MATCH_YES;
2889 /* Match the EXIT statement. */
2891 match
2892 gfc_match_exit (void)
2894 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2898 /* Match the CYCLE statement. */
2900 match
2901 gfc_match_cycle (void)
2903 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2907 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
2908 requirements for a stop-code differ in the standards.
2910 Fortran 95 has
2912 R840 stop-stmt is STOP [ stop-code ]
2913 R841 stop-code is scalar-char-constant
2914 or digit [ digit [ digit [ digit [ digit ] ] ] ]
2916 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
2917 Fortran 2008 has
2919 R855 stop-stmt is STOP [ stop-code ]
2920 R856 allstop-stmt is ALL STOP [ stop-code ]
2921 R857 stop-code is scalar-default-char-constant-expr
2922 or scalar-int-constant-expr
2924 For free-form source code, all standards contain a statement of the form:
2926 A blank shall be used to separate names, constants, or labels from
2927 adjacent keywords, names, constants, or labels.
2929 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
2931 STOP123
2933 is valid, but it is invalid Fortran 2008. */
2935 static match
2936 gfc_match_stopcode (gfc_statement st)
2938 gfc_expr *e = NULL;
2939 match m;
2940 bool f95, f03;
2942 /* Set f95 for -std=f95. */
2943 f95 = (gfc_option.allow_std == GFC_STD_OPT_F95);
2945 /* Set f03 for -std=f2003. */
2946 f03 = (gfc_option.allow_std == GFC_STD_OPT_F03);
2948 /* Look for a blank between STOP and the stop-code for F2008 or later. */
2949 if (gfc_current_form != FORM_FIXED && !(f95 || f03))
2951 char c = gfc_peek_ascii_char ();
2953 /* Look for end-of-statement. There is no stop-code. */
2954 if (c == '\n' || c == '!' || c == ';')
2955 goto done;
2957 if (c != ' ')
2959 gfc_error ("Blank required in %s statement near %C",
2960 gfc_ascii_statement (st));
2961 return MATCH_ERROR;
2965 if (gfc_match_eos () != MATCH_YES)
2967 int stopcode;
2968 locus old_locus;
2970 /* First look for the F95 or F2003 digit [...] construct. */
2971 old_locus = gfc_current_locus;
2972 m = gfc_match_small_int (&stopcode);
2973 if (m == MATCH_YES && (f95 || f03))
2975 if (stopcode < 0)
2977 gfc_error ("STOP code at %C cannot be negative");
2978 return MATCH_ERROR;
2981 if (stopcode > 99999)
2983 gfc_error ("STOP code at %C contains too many digits");
2984 return MATCH_ERROR;
2988 /* Reset the locus and now load gfc_expr. */
2989 gfc_current_locus = old_locus;
2990 m = gfc_match_expr (&e);
2991 if (m == MATCH_ERROR)
2992 goto cleanup;
2993 if (m == MATCH_NO)
2994 goto syntax;
2996 if (gfc_match_eos () != MATCH_YES)
2997 goto syntax;
3000 if (gfc_pure (NULL))
3002 if (st == ST_ERROR_STOP)
3004 if (!gfc_notify_std (GFC_STD_F2018, "%s statement at %C in PURE "
3005 "procedure", gfc_ascii_statement (st)))
3006 goto cleanup;
3008 else
3010 gfc_error ("%s statement not allowed in PURE procedure at %C",
3011 gfc_ascii_statement (st));
3012 goto cleanup;
3016 gfc_unset_implicit_pure (NULL);
3018 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
3020 gfc_error ("Image control statement STOP at %C in CRITICAL block");
3021 goto cleanup;
3023 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
3025 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
3026 goto cleanup;
3029 if (e != NULL)
3031 gfc_simplify_expr (e, 0);
3033 /* Test for F95 and F2003 style STOP stop-code. */
3034 if (e->expr_type != EXPR_CONSTANT && (f95 || f03))
3036 gfc_error ("STOP code at %L must be a scalar CHARACTER constant or "
3037 "digit[digit[digit[digit[digit]]]]", &e->where);
3038 goto cleanup;
3041 /* Use the machinery for an initialization expression to reduce the
3042 stop-code to a constant. */
3043 gfc_init_expr_flag = true;
3044 gfc_reduce_init_expr (e);
3045 gfc_init_expr_flag = false;
3047 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
3049 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
3050 &e->where);
3051 goto cleanup;
3054 if (e->rank != 0)
3056 gfc_error ("STOP code at %L must be scalar", &e->where);
3057 goto cleanup;
3060 if (e->ts.type == BT_CHARACTER
3061 && e->ts.kind != gfc_default_character_kind)
3063 gfc_error ("STOP code at %L must be default character KIND=%d",
3064 &e->where, (int) gfc_default_character_kind);
3065 goto cleanup;
3068 if (e->ts.type == BT_INTEGER && e->ts.kind != gfc_default_integer_kind)
3070 gfc_error ("STOP code at %L must be default integer KIND=%d",
3071 &e->where, (int) gfc_default_integer_kind);
3072 goto cleanup;
3076 done:
3078 switch (st)
3080 case ST_STOP:
3081 new_st.op = EXEC_STOP;
3082 break;
3083 case ST_ERROR_STOP:
3084 new_st.op = EXEC_ERROR_STOP;
3085 break;
3086 case ST_PAUSE:
3087 new_st.op = EXEC_PAUSE;
3088 break;
3089 default:
3090 gcc_unreachable ();
3093 new_st.expr1 = e;
3094 new_st.ext.stop_code = -1;
3096 return MATCH_YES;
3098 syntax:
3099 gfc_syntax_error (st);
3101 cleanup:
3103 gfc_free_expr (e);
3104 return MATCH_ERROR;
3108 /* Match the (deprecated) PAUSE statement. */
3110 match
3111 gfc_match_pause (void)
3113 match m;
3115 m = gfc_match_stopcode (ST_PAUSE);
3116 if (m == MATCH_YES)
3118 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
3119 m = MATCH_ERROR;
3121 return m;
3125 /* Match the STOP statement. */
3127 match
3128 gfc_match_stop (void)
3130 return gfc_match_stopcode (ST_STOP);
3134 /* Match the ERROR STOP statement. */
3136 match
3137 gfc_match_error_stop (void)
3139 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
3140 return MATCH_ERROR;
3142 return gfc_match_stopcode (ST_ERROR_STOP);
3145 /* Match EVENT POST/WAIT statement. Syntax:
3146 EVENT POST ( event-variable [, sync-stat-list] )
3147 EVENT WAIT ( event-variable [, wait-spec-list] )
3148 with
3149 wait-spec-list is sync-stat-list or until-spec
3150 until-spec is UNTIL_COUNT = scalar-int-expr
3151 sync-stat is STAT= or ERRMSG=. */
3153 static match
3154 event_statement (gfc_statement st)
3156 match m;
3157 gfc_expr *tmp, *eventvar, *until_count, *stat, *errmsg;
3158 bool saw_until_count, saw_stat, saw_errmsg;
3160 tmp = eventvar = until_count = stat = errmsg = NULL;
3161 saw_until_count = saw_stat = saw_errmsg = false;
3163 if (gfc_pure (NULL))
3165 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3166 st == ST_EVENT_POST ? "POST" : "WAIT");
3167 return MATCH_ERROR;
3170 gfc_unset_implicit_pure (NULL);
3172 if (flag_coarray == GFC_FCOARRAY_NONE)
3174 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3175 return MATCH_ERROR;
3178 if (gfc_find_state (COMP_CRITICAL))
3180 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3181 st == ST_EVENT_POST ? "POST" : "WAIT");
3182 return MATCH_ERROR;
3185 if (gfc_find_state (COMP_DO_CONCURRENT))
3187 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3188 "block", st == ST_EVENT_POST ? "POST" : "WAIT");
3189 return MATCH_ERROR;
3192 if (gfc_match_char ('(') != MATCH_YES)
3193 goto syntax;
3195 if (gfc_match ("%e", &eventvar) != MATCH_YES)
3196 goto syntax;
3197 m = gfc_match_char (',');
3198 if (m == MATCH_ERROR)
3199 goto syntax;
3200 if (m == MATCH_NO)
3202 m = gfc_match_char (')');
3203 if (m == MATCH_YES)
3204 goto done;
3205 goto syntax;
3208 for (;;)
3210 m = gfc_match (" stat = %v", &tmp);
3211 if (m == MATCH_ERROR)
3212 goto syntax;
3213 if (m == MATCH_YES)
3215 if (saw_stat)
3217 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3218 goto cleanup;
3220 stat = tmp;
3221 saw_stat = true;
3223 m = gfc_match_char (',');
3224 if (m == MATCH_YES)
3225 continue;
3227 tmp = NULL;
3228 break;
3231 m = gfc_match (" errmsg = %v", &tmp);
3232 if (m == MATCH_ERROR)
3233 goto syntax;
3234 if (m == MATCH_YES)
3236 if (saw_errmsg)
3238 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3239 goto cleanup;
3241 errmsg = tmp;
3242 saw_errmsg = true;
3244 m = gfc_match_char (',');
3245 if (m == MATCH_YES)
3246 continue;
3248 tmp = NULL;
3249 break;
3252 m = gfc_match (" until_count = %e", &tmp);
3253 if (m == MATCH_ERROR || st == ST_EVENT_POST)
3254 goto syntax;
3255 if (m == MATCH_YES)
3257 if (saw_until_count)
3259 gfc_error ("Redundant UNTIL_COUNT tag found at %L",
3260 &tmp->where);
3261 goto cleanup;
3263 until_count = tmp;
3264 saw_until_count = true;
3266 m = gfc_match_char (',');
3267 if (m == MATCH_YES)
3268 continue;
3270 tmp = NULL;
3271 break;
3274 break;
3277 if (m == MATCH_ERROR)
3278 goto syntax;
3280 if (gfc_match (" )%t") != MATCH_YES)
3281 goto syntax;
3283 done:
3284 switch (st)
3286 case ST_EVENT_POST:
3287 new_st.op = EXEC_EVENT_POST;
3288 break;
3289 case ST_EVENT_WAIT:
3290 new_st.op = EXEC_EVENT_WAIT;
3291 break;
3292 default:
3293 gcc_unreachable ();
3296 new_st.expr1 = eventvar;
3297 new_st.expr2 = stat;
3298 new_st.expr3 = errmsg;
3299 new_st.expr4 = until_count;
3301 return MATCH_YES;
3303 syntax:
3304 gfc_syntax_error (st);
3306 cleanup:
3307 if (until_count != tmp)
3308 gfc_free_expr (until_count);
3309 if (errmsg != tmp)
3310 gfc_free_expr (errmsg);
3311 if (stat != tmp)
3312 gfc_free_expr (stat);
3314 gfc_free_expr (tmp);
3315 gfc_free_expr (eventvar);
3317 return MATCH_ERROR;
3322 match
3323 gfc_match_event_post (void)
3325 if (!gfc_notify_std (GFC_STD_F2018, "EVENT POST statement at %C"))
3326 return MATCH_ERROR;
3328 return event_statement (ST_EVENT_POST);
3332 match
3333 gfc_match_event_wait (void)
3335 if (!gfc_notify_std (GFC_STD_F2018, "EVENT WAIT statement at %C"))
3336 return MATCH_ERROR;
3338 return event_statement (ST_EVENT_WAIT);
3342 /* Match a FAIL IMAGE statement. */
3344 match
3345 gfc_match_fail_image (void)
3347 if (!gfc_notify_std (GFC_STD_F2018, "FAIL IMAGE statement at %C"))
3348 return MATCH_ERROR;
3350 if (gfc_match_char ('(') == MATCH_YES)
3351 goto syntax;
3353 new_st.op = EXEC_FAIL_IMAGE;
3355 return MATCH_YES;
3357 syntax:
3358 gfc_syntax_error (ST_FAIL_IMAGE);
3360 return MATCH_ERROR;
3363 /* Match a FORM TEAM statement. */
3365 match
3366 gfc_match_form_team (void)
3368 match m;
3369 gfc_expr *teamid,*team;
3371 if (!gfc_notify_std (GFC_STD_F2018, "FORM TEAM statement at %C"))
3372 return MATCH_ERROR;
3374 if (gfc_match_char ('(') == MATCH_NO)
3375 goto syntax;
3377 new_st.op = EXEC_FORM_TEAM;
3379 if (gfc_match ("%e", &teamid) != MATCH_YES)
3380 goto syntax;
3381 m = gfc_match_char (',');
3382 if (m == MATCH_ERROR)
3383 goto syntax;
3384 if (gfc_match ("%e", &team) != MATCH_YES)
3385 goto syntax;
3387 m = gfc_match_char (')');
3388 if (m == MATCH_NO)
3389 goto syntax;
3391 new_st.expr1 = teamid;
3392 new_st.expr2 = team;
3394 return MATCH_YES;
3396 syntax:
3397 gfc_syntax_error (ST_FORM_TEAM);
3399 return MATCH_ERROR;
3402 /* Match a CHANGE TEAM statement. */
3404 match
3405 gfc_match_change_team (void)
3407 match m;
3408 gfc_expr *team;
3410 if (!gfc_notify_std (GFC_STD_F2018, "CHANGE TEAM statement at %C"))
3411 return MATCH_ERROR;
3413 if (gfc_match_char ('(') == MATCH_NO)
3414 goto syntax;
3416 new_st.op = EXEC_CHANGE_TEAM;
3418 if (gfc_match ("%e", &team) != MATCH_YES)
3419 goto syntax;
3421 m = gfc_match_char (')');
3422 if (m == MATCH_NO)
3423 goto syntax;
3425 new_st.expr1 = team;
3427 return MATCH_YES;
3429 syntax:
3430 gfc_syntax_error (ST_CHANGE_TEAM);
3432 return MATCH_ERROR;
3435 /* Match a END TEAM statement. */
3437 match
3438 gfc_match_end_team (void)
3440 if (!gfc_notify_std (GFC_STD_F2018, "END TEAM statement at %C"))
3441 return MATCH_ERROR;
3443 if (gfc_match_char ('(') == MATCH_YES)
3444 goto syntax;
3446 new_st.op = EXEC_END_TEAM;
3448 return MATCH_YES;
3450 syntax:
3451 gfc_syntax_error (ST_END_TEAM);
3453 return MATCH_ERROR;
3456 /* Match a SYNC TEAM statement. */
3458 match
3459 gfc_match_sync_team (void)
3461 match m;
3462 gfc_expr *team;
3464 if (!gfc_notify_std (GFC_STD_F2018, "SYNC TEAM statement at %C"))
3465 return MATCH_ERROR;
3467 if (gfc_match_char ('(') == MATCH_NO)
3468 goto syntax;
3470 new_st.op = EXEC_SYNC_TEAM;
3472 if (gfc_match ("%e", &team) != MATCH_YES)
3473 goto syntax;
3475 m = gfc_match_char (')');
3476 if (m == MATCH_NO)
3477 goto syntax;
3479 new_st.expr1 = team;
3481 return MATCH_YES;
3483 syntax:
3484 gfc_syntax_error (ST_SYNC_TEAM);
3486 return MATCH_ERROR;
3489 /* Match LOCK/UNLOCK statement. Syntax:
3490 LOCK ( lock-variable [ , lock-stat-list ] )
3491 UNLOCK ( lock-variable [ , sync-stat-list ] )
3492 where lock-stat is ACQUIRED_LOCK or sync-stat
3493 and sync-stat is STAT= or ERRMSG=. */
3495 static match
3496 lock_unlock_statement (gfc_statement st)
3498 match m;
3499 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
3500 bool saw_acq_lock, saw_stat, saw_errmsg;
3502 tmp = lockvar = acq_lock = stat = errmsg = NULL;
3503 saw_acq_lock = saw_stat = saw_errmsg = false;
3505 if (gfc_pure (NULL))
3507 gfc_error ("Image control statement %s at %C in PURE procedure",
3508 st == ST_LOCK ? "LOCK" : "UNLOCK");
3509 return MATCH_ERROR;
3512 gfc_unset_implicit_pure (NULL);
3514 if (flag_coarray == GFC_FCOARRAY_NONE)
3516 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3517 return MATCH_ERROR;
3520 if (gfc_find_state (COMP_CRITICAL))
3522 gfc_error ("Image control statement %s at %C in CRITICAL block",
3523 st == ST_LOCK ? "LOCK" : "UNLOCK");
3524 return MATCH_ERROR;
3527 if (gfc_find_state (COMP_DO_CONCURRENT))
3529 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3530 st == ST_LOCK ? "LOCK" : "UNLOCK");
3531 return MATCH_ERROR;
3534 if (gfc_match_char ('(') != MATCH_YES)
3535 goto syntax;
3537 if (gfc_match ("%e", &lockvar) != MATCH_YES)
3538 goto syntax;
3539 m = gfc_match_char (',');
3540 if (m == MATCH_ERROR)
3541 goto syntax;
3542 if (m == MATCH_NO)
3544 m = gfc_match_char (')');
3545 if (m == MATCH_YES)
3546 goto done;
3547 goto syntax;
3550 for (;;)
3552 m = gfc_match (" stat = %v", &tmp);
3553 if (m == MATCH_ERROR)
3554 goto syntax;
3555 if (m == MATCH_YES)
3557 if (saw_stat)
3559 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3560 goto cleanup;
3562 stat = tmp;
3563 saw_stat = true;
3565 m = gfc_match_char (',');
3566 if (m == MATCH_YES)
3567 continue;
3569 tmp = NULL;
3570 break;
3573 m = gfc_match (" errmsg = %v", &tmp);
3574 if (m == MATCH_ERROR)
3575 goto syntax;
3576 if (m == MATCH_YES)
3578 if (saw_errmsg)
3580 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3581 goto cleanup;
3583 errmsg = tmp;
3584 saw_errmsg = true;
3586 m = gfc_match_char (',');
3587 if (m == MATCH_YES)
3588 continue;
3590 tmp = NULL;
3591 break;
3594 m = gfc_match (" acquired_lock = %v", &tmp);
3595 if (m == MATCH_ERROR || st == ST_UNLOCK)
3596 goto syntax;
3597 if (m == MATCH_YES)
3599 if (saw_acq_lock)
3601 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L",
3602 &tmp->where);
3603 goto cleanup;
3605 acq_lock = tmp;
3606 saw_acq_lock = true;
3608 m = gfc_match_char (',');
3609 if (m == MATCH_YES)
3610 continue;
3612 tmp = NULL;
3613 break;
3616 break;
3619 if (m == MATCH_ERROR)
3620 goto syntax;
3622 if (gfc_match (" )%t") != MATCH_YES)
3623 goto syntax;
3625 done:
3626 switch (st)
3628 case ST_LOCK:
3629 new_st.op = EXEC_LOCK;
3630 break;
3631 case ST_UNLOCK:
3632 new_st.op = EXEC_UNLOCK;
3633 break;
3634 default:
3635 gcc_unreachable ();
3638 new_st.expr1 = lockvar;
3639 new_st.expr2 = stat;
3640 new_st.expr3 = errmsg;
3641 new_st.expr4 = acq_lock;
3643 return MATCH_YES;
3645 syntax:
3646 gfc_syntax_error (st);
3648 cleanup:
3649 if (acq_lock != tmp)
3650 gfc_free_expr (acq_lock);
3651 if (errmsg != tmp)
3652 gfc_free_expr (errmsg);
3653 if (stat != tmp)
3654 gfc_free_expr (stat);
3656 gfc_free_expr (tmp);
3657 gfc_free_expr (lockvar);
3659 return MATCH_ERROR;
3663 match
3664 gfc_match_lock (void)
3666 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
3667 return MATCH_ERROR;
3669 return lock_unlock_statement (ST_LOCK);
3673 match
3674 gfc_match_unlock (void)
3676 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
3677 return MATCH_ERROR;
3679 return lock_unlock_statement (ST_UNLOCK);
3683 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3684 SYNC ALL [(sync-stat-list)]
3685 SYNC MEMORY [(sync-stat-list)]
3686 SYNC IMAGES (image-set [, sync-stat-list] )
3687 with sync-stat is int-expr or *. */
3689 static match
3690 sync_statement (gfc_statement st)
3692 match m;
3693 gfc_expr *tmp, *imageset, *stat, *errmsg;
3694 bool saw_stat, saw_errmsg;
3696 tmp = imageset = stat = errmsg = NULL;
3697 saw_stat = saw_errmsg = false;
3699 if (gfc_pure (NULL))
3701 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3702 return MATCH_ERROR;
3705 gfc_unset_implicit_pure (NULL);
3707 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
3708 return MATCH_ERROR;
3710 if (flag_coarray == GFC_FCOARRAY_NONE)
3712 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3713 "enable");
3714 return MATCH_ERROR;
3717 if (gfc_find_state (COMP_CRITICAL))
3719 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3720 return MATCH_ERROR;
3723 if (gfc_find_state (COMP_DO_CONCURRENT))
3725 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3726 return MATCH_ERROR;
3729 if (gfc_match_eos () == MATCH_YES)
3731 if (st == ST_SYNC_IMAGES)
3732 goto syntax;
3733 goto done;
3736 if (gfc_match_char ('(') != MATCH_YES)
3737 goto syntax;
3739 if (st == ST_SYNC_IMAGES)
3741 /* Denote '*' as imageset == NULL. */
3742 m = gfc_match_char ('*');
3743 if (m == MATCH_ERROR)
3744 goto syntax;
3745 if (m == MATCH_NO)
3747 if (gfc_match ("%e", &imageset) != MATCH_YES)
3748 goto syntax;
3750 m = gfc_match_char (',');
3751 if (m == MATCH_ERROR)
3752 goto syntax;
3753 if (m == MATCH_NO)
3755 m = gfc_match_char (')');
3756 if (m == MATCH_YES)
3757 goto done;
3758 goto syntax;
3762 for (;;)
3764 m = gfc_match (" stat = %v", &tmp);
3765 if (m == MATCH_ERROR)
3766 goto syntax;
3767 if (m == MATCH_YES)
3769 if (saw_stat)
3771 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3772 goto cleanup;
3774 stat = tmp;
3775 saw_stat = true;
3777 if (gfc_match_char (',') == MATCH_YES)
3778 continue;
3780 tmp = NULL;
3781 break;
3784 m = gfc_match (" errmsg = %v", &tmp);
3785 if (m == MATCH_ERROR)
3786 goto syntax;
3787 if (m == MATCH_YES)
3789 if (saw_errmsg)
3791 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3792 goto cleanup;
3794 errmsg = tmp;
3795 saw_errmsg = true;
3797 if (gfc_match_char (',') == MATCH_YES)
3798 continue;
3800 tmp = NULL;
3801 break;
3804 break;
3807 if (gfc_match (" )%t") != MATCH_YES)
3808 goto syntax;
3810 done:
3811 switch (st)
3813 case ST_SYNC_ALL:
3814 new_st.op = EXEC_SYNC_ALL;
3815 break;
3816 case ST_SYNC_IMAGES:
3817 new_st.op = EXEC_SYNC_IMAGES;
3818 break;
3819 case ST_SYNC_MEMORY:
3820 new_st.op = EXEC_SYNC_MEMORY;
3821 break;
3822 default:
3823 gcc_unreachable ();
3826 new_st.expr1 = imageset;
3827 new_st.expr2 = stat;
3828 new_st.expr3 = errmsg;
3830 return MATCH_YES;
3832 syntax:
3833 gfc_syntax_error (st);
3835 cleanup:
3836 if (stat != tmp)
3837 gfc_free_expr (stat);
3838 if (errmsg != tmp)
3839 gfc_free_expr (errmsg);
3841 gfc_free_expr (tmp);
3842 gfc_free_expr (imageset);
3844 return MATCH_ERROR;
3848 /* Match SYNC ALL statement. */
3850 match
3851 gfc_match_sync_all (void)
3853 return sync_statement (ST_SYNC_ALL);
3857 /* Match SYNC IMAGES statement. */
3859 match
3860 gfc_match_sync_images (void)
3862 return sync_statement (ST_SYNC_IMAGES);
3866 /* Match SYNC MEMORY statement. */
3868 match
3869 gfc_match_sync_memory (void)
3871 return sync_statement (ST_SYNC_MEMORY);
3875 /* Match a CONTINUE statement. */
3877 match
3878 gfc_match_continue (void)
3880 if (gfc_match_eos () != MATCH_YES)
3882 gfc_syntax_error (ST_CONTINUE);
3883 return MATCH_ERROR;
3886 new_st.op = EXEC_CONTINUE;
3887 return MATCH_YES;
3891 /* Match the (deprecated) ASSIGN statement. */
3893 match
3894 gfc_match_assign (void)
3896 gfc_expr *expr;
3897 gfc_st_label *label;
3899 if (gfc_match (" %l", &label) == MATCH_YES)
3901 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
3902 return MATCH_ERROR;
3903 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
3905 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
3906 return MATCH_ERROR;
3908 expr->symtree->n.sym->attr.assign = 1;
3910 new_st.op = EXEC_LABEL_ASSIGN;
3911 new_st.label1 = label;
3912 new_st.expr1 = expr;
3913 return MATCH_YES;
3916 return MATCH_NO;
3920 /* Match the GO TO statement. As a computed GOTO statement is
3921 matched, it is transformed into an equivalent SELECT block. No
3922 tree is necessary, and the resulting jumps-to-jumps are
3923 specifically optimized away by the back end. */
3925 match
3926 gfc_match_goto (void)
3928 gfc_code *head, *tail;
3929 gfc_expr *expr;
3930 gfc_case *cp;
3931 gfc_st_label *label;
3932 int i;
3933 match m;
3935 if (gfc_match (" %l%t", &label) == MATCH_YES)
3937 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3938 return MATCH_ERROR;
3940 new_st.op = EXEC_GOTO;
3941 new_st.label1 = label;
3942 return MATCH_YES;
3945 /* The assigned GO TO statement. */
3947 if (gfc_match_variable (&expr, 0) == MATCH_YES)
3949 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
3950 return MATCH_ERROR;
3952 new_st.op = EXEC_GOTO;
3953 new_st.expr1 = expr;
3955 if (gfc_match_eos () == MATCH_YES)
3956 return MATCH_YES;
3958 /* Match label list. */
3959 gfc_match_char (',');
3960 if (gfc_match_char ('(') != MATCH_YES)
3962 gfc_syntax_error (ST_GOTO);
3963 return MATCH_ERROR;
3965 head = tail = NULL;
3969 m = gfc_match_st_label (&label);
3970 if (m != MATCH_YES)
3971 goto syntax;
3973 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3974 goto cleanup;
3976 if (head == NULL)
3977 head = tail = gfc_get_code (EXEC_GOTO);
3978 else
3980 tail->block = gfc_get_code (EXEC_GOTO);
3981 tail = tail->block;
3984 tail->label1 = label;
3986 while (gfc_match_char (',') == MATCH_YES);
3988 if (gfc_match (")%t") != MATCH_YES)
3989 goto syntax;
3991 if (head == NULL)
3993 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3994 goto syntax;
3996 new_st.block = head;
3998 return MATCH_YES;
4001 /* Last chance is a computed GO TO statement. */
4002 if (gfc_match_char ('(') != MATCH_YES)
4004 gfc_syntax_error (ST_GOTO);
4005 return MATCH_ERROR;
4008 head = tail = NULL;
4009 i = 1;
4013 m = gfc_match_st_label (&label);
4014 if (m != MATCH_YES)
4015 goto syntax;
4017 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4018 goto cleanup;
4020 if (head == NULL)
4021 head = tail = gfc_get_code (EXEC_SELECT);
4022 else
4024 tail->block = gfc_get_code (EXEC_SELECT);
4025 tail = tail->block;
4028 cp = gfc_get_case ();
4029 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
4030 NULL, i++);
4032 tail->ext.block.case_list = cp;
4034 tail->next = gfc_get_code (EXEC_GOTO);
4035 tail->next->label1 = label;
4037 while (gfc_match_char (',') == MATCH_YES);
4039 if (gfc_match_char (')') != MATCH_YES)
4040 goto syntax;
4042 if (head == NULL)
4044 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4045 goto syntax;
4048 /* Get the rest of the statement. */
4049 gfc_match_char (',');
4051 if (gfc_match (" %e%t", &expr) != MATCH_YES)
4052 goto syntax;
4054 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
4055 return MATCH_ERROR;
4057 /* At this point, a computed GOTO has been fully matched and an
4058 equivalent SELECT statement constructed. */
4060 new_st.op = EXEC_SELECT;
4061 new_st.expr1 = NULL;
4063 /* Hack: For a "real" SELECT, the expression is in expr. We put
4064 it in expr2 so we can distinguish then and produce the correct
4065 diagnostics. */
4066 new_st.expr2 = expr;
4067 new_st.block = head;
4068 return MATCH_YES;
4070 syntax:
4071 gfc_syntax_error (ST_GOTO);
4072 cleanup:
4073 gfc_free_statements (head);
4074 return MATCH_ERROR;
4078 /* Frees a list of gfc_alloc structures. */
4080 void
4081 gfc_free_alloc_list (gfc_alloc *p)
4083 gfc_alloc *q;
4085 for (; p; p = q)
4087 q = p->next;
4088 gfc_free_expr (p->expr);
4089 free (p);
4094 /* Match an ALLOCATE statement. */
4096 match
4097 gfc_match_allocate (void)
4099 gfc_alloc *head, *tail;
4100 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
4101 gfc_typespec ts;
4102 gfc_symbol *sym;
4103 match m;
4104 locus old_locus, deferred_locus, assumed_locus;
4105 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
4106 bool saw_unlimited = false, saw_assumed = false;
4108 head = tail = NULL;
4109 stat = errmsg = source = mold = tmp = NULL;
4110 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
4112 if (gfc_match_char ('(') != MATCH_YES)
4114 gfc_syntax_error (ST_ALLOCATE);
4115 return MATCH_ERROR;
4118 /* Match an optional type-spec. */
4119 old_locus = gfc_current_locus;
4120 m = gfc_match_type_spec (&ts);
4121 if (m == MATCH_ERROR)
4122 goto cleanup;
4123 else if (m == MATCH_NO)
4125 char name[GFC_MAX_SYMBOL_LEN + 3];
4127 if (gfc_match ("%n :: ", name) == MATCH_YES)
4129 gfc_error ("Error in type-spec at %L", &old_locus);
4130 goto cleanup;
4133 ts.type = BT_UNKNOWN;
4135 else
4137 /* Needed for the F2008:C631 check below. */
4138 assumed_locus = gfc_current_locus;
4140 if (gfc_match (" :: ") == MATCH_YES)
4142 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
4143 &old_locus))
4144 goto cleanup;
4146 if (ts.deferred)
4148 gfc_error ("Type-spec at %L cannot contain a deferred "
4149 "type parameter", &old_locus);
4150 goto cleanup;
4153 if (ts.type == BT_CHARACTER)
4155 if (!ts.u.cl->length)
4156 saw_assumed = true;
4157 else
4158 ts.u.cl->length_from_typespec = true;
4161 if (type_param_spec_list
4162 && gfc_spec_list_type (type_param_spec_list, NULL)
4163 == SPEC_DEFERRED)
4165 gfc_error ("The type parameter spec list in the type-spec at "
4166 "%L cannot contain DEFERRED parameters", &old_locus);
4167 goto cleanup;
4170 else
4172 ts.type = BT_UNKNOWN;
4173 gfc_current_locus = old_locus;
4177 for (;;)
4179 if (head == NULL)
4180 head = tail = gfc_get_alloc ();
4181 else
4183 tail->next = gfc_get_alloc ();
4184 tail = tail->next;
4187 m = gfc_match_variable (&tail->expr, 0);
4188 if (m == MATCH_NO)
4189 goto syntax;
4190 if (m == MATCH_ERROR)
4191 goto cleanup;
4193 if (gfc_check_do_variable (tail->expr->symtree))
4194 goto cleanup;
4196 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
4197 if (impure && gfc_pure (NULL))
4199 gfc_error ("Bad allocate-object at %C for a PURE procedure");
4200 goto cleanup;
4203 if (impure)
4204 gfc_unset_implicit_pure (NULL);
4206 /* F2008:C631 (R626) A type-param-value in a type-spec shall be an
4207 asterisk if and only if each allocate-object is a dummy argument
4208 for which the corresponding type parameter is assumed. */
4209 if (saw_assumed
4210 && (tail->expr->ts.deferred
4211 || (tail->expr->ts.u.cl && tail->expr->ts.u.cl->length)
4212 || tail->expr->symtree->n.sym->attr.dummy == 0))
4214 gfc_error ("Incompatible allocate-object at %C for CHARACTER "
4215 "type-spec at %L", &assumed_locus);
4216 goto cleanup;
4219 if (tail->expr->ts.deferred)
4221 saw_deferred = true;
4222 deferred_locus = tail->expr->where;
4225 if (gfc_find_state (COMP_DO_CONCURRENT)
4226 || gfc_find_state (COMP_CRITICAL))
4228 gfc_ref *ref;
4229 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
4230 for (ref = tail->expr->ref; ref; ref = ref->next)
4231 if (ref->type == REF_COMPONENT)
4232 coarray = ref->u.c.component->attr.codimension;
4234 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
4236 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
4237 goto cleanup;
4239 if (coarray && gfc_find_state (COMP_CRITICAL))
4241 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
4242 goto cleanup;
4246 /* Check for F08:C628. */
4247 sym = tail->expr->symtree->n.sym;
4248 b1 = !(tail->expr->ref
4249 && (tail->expr->ref->type == REF_COMPONENT
4250 || tail->expr->ref->type == REF_ARRAY));
4251 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
4252 b2 = !(CLASS_DATA (sym)->attr.allocatable
4253 || CLASS_DATA (sym)->attr.class_pointer);
4254 else
4255 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4256 || sym->attr.proc_pointer);
4257 b3 = sym && sym->ns && sym->ns->proc_name
4258 && (sym->ns->proc_name->attr.allocatable
4259 || sym->ns->proc_name->attr.pointer
4260 || sym->ns->proc_name->attr.proc_pointer);
4261 if (b1 && b2 && !b3)
4263 gfc_error ("Allocate-object at %L is neither a data pointer "
4264 "nor an allocatable variable", &tail->expr->where);
4265 goto cleanup;
4268 /* The ALLOCATE statement had an optional typespec. Check the
4269 constraints. */
4270 if (ts.type != BT_UNKNOWN)
4272 /* Enforce F03:C624. */
4273 if (!gfc_type_compatible (&tail->expr->ts, &ts))
4275 gfc_error ("Type of entity at %L is type incompatible with "
4276 "typespec", &tail->expr->where);
4277 goto cleanup;
4280 /* Enforce F03:C627. */
4281 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
4283 gfc_error ("Kind type parameter for entity at %L differs from "
4284 "the kind type parameter of the typespec",
4285 &tail->expr->where);
4286 goto cleanup;
4290 if (tail->expr->ts.type == BT_DERIVED)
4291 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
4293 if (type_param_spec_list)
4294 tail->expr->param_list = gfc_copy_actual_arglist (type_param_spec_list);
4296 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
4298 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
4300 gfc_error ("Shape specification for allocatable scalar at %C");
4301 goto cleanup;
4304 if (gfc_match_char (',') != MATCH_YES)
4305 break;
4307 alloc_opt_list:
4309 m = gfc_match (" stat = %v", &tmp);
4310 if (m == MATCH_ERROR)
4311 goto cleanup;
4312 if (m == MATCH_YES)
4314 /* Enforce C630. */
4315 if (saw_stat)
4317 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
4318 goto cleanup;
4321 stat = tmp;
4322 tmp = NULL;
4323 saw_stat = true;
4325 if (gfc_check_do_variable (stat->symtree))
4326 goto cleanup;
4328 if (gfc_match_char (',') == MATCH_YES)
4329 goto alloc_opt_list;
4332 m = gfc_match (" errmsg = %v", &tmp);
4333 if (m == MATCH_ERROR)
4334 goto cleanup;
4335 if (m == MATCH_YES)
4337 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
4338 goto cleanup;
4340 /* Enforce C630. */
4341 if (saw_errmsg)
4343 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
4344 goto cleanup;
4347 errmsg = tmp;
4348 tmp = NULL;
4349 saw_errmsg = true;
4351 if (gfc_match_char (',') == MATCH_YES)
4352 goto alloc_opt_list;
4355 m = gfc_match (" source = %e", &tmp);
4356 if (m == MATCH_ERROR)
4357 goto cleanup;
4358 if (m == MATCH_YES)
4360 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
4361 goto cleanup;
4363 /* Enforce C630. */
4364 if (saw_source)
4366 gfc_error ("Redundant SOURCE tag found at %L", &tmp->where);
4367 goto cleanup;
4370 /* The next 2 conditionals check C631. */
4371 if (ts.type != BT_UNKNOWN)
4373 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4374 &tmp->where, &old_locus);
4375 goto cleanup;
4378 if (head->next
4379 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
4380 " with more than a single allocate object",
4381 &tmp->where))
4382 goto cleanup;
4384 source = tmp;
4385 tmp = NULL;
4386 saw_source = true;
4388 if (gfc_match_char (',') == MATCH_YES)
4389 goto alloc_opt_list;
4392 m = gfc_match (" mold = %e", &tmp);
4393 if (m == MATCH_ERROR)
4394 goto cleanup;
4395 if (m == MATCH_YES)
4397 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
4398 goto cleanup;
4400 /* Check F08:C636. */
4401 if (saw_mold)
4403 gfc_error ("Redundant MOLD tag found at %L", &tmp->where);
4404 goto cleanup;
4407 /* Check F08:C637. */
4408 if (ts.type != BT_UNKNOWN)
4410 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4411 &tmp->where, &old_locus);
4412 goto cleanup;
4415 mold = tmp;
4416 tmp = NULL;
4417 saw_mold = true;
4418 mold->mold = 1;
4420 if (gfc_match_char (',') == MATCH_YES)
4421 goto alloc_opt_list;
4424 gfc_gobble_whitespace ();
4426 if (gfc_peek_char () == ')')
4427 break;
4430 if (gfc_match (" )%t") != MATCH_YES)
4431 goto syntax;
4433 /* Check F08:C637. */
4434 if (source && mold)
4436 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4437 &mold->where, &source->where);
4438 goto cleanup;
4441 /* Check F03:C623, */
4442 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
4444 gfc_error ("Allocate-object at %L with a deferred type parameter "
4445 "requires either a type-spec or SOURCE tag or a MOLD tag",
4446 &deferred_locus);
4447 goto cleanup;
4450 /* Check F03:C625, */
4451 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
4453 for (tail = head; tail; tail = tail->next)
4455 if (UNLIMITED_POLY (tail->expr))
4456 gfc_error ("Unlimited polymorphic allocate-object at %L "
4457 "requires either a type-spec or SOURCE tag "
4458 "or a MOLD tag", &tail->expr->where);
4460 goto cleanup;
4463 new_st.op = EXEC_ALLOCATE;
4464 new_st.expr1 = stat;
4465 new_st.expr2 = errmsg;
4466 if (source)
4467 new_st.expr3 = source;
4468 else
4469 new_st.expr3 = mold;
4470 new_st.ext.alloc.list = head;
4471 new_st.ext.alloc.ts = ts;
4473 if (type_param_spec_list)
4474 gfc_free_actual_arglist (type_param_spec_list);
4476 return MATCH_YES;
4478 syntax:
4479 gfc_syntax_error (ST_ALLOCATE);
4481 cleanup:
4482 gfc_free_expr (errmsg);
4483 gfc_free_expr (source);
4484 gfc_free_expr (stat);
4485 gfc_free_expr (mold);
4486 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
4487 gfc_free_alloc_list (head);
4488 if (type_param_spec_list)
4489 gfc_free_actual_arglist (type_param_spec_list);
4490 return MATCH_ERROR;
4494 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4495 a set of pointer assignments to intrinsic NULL(). */
4497 match
4498 gfc_match_nullify (void)
4500 gfc_code *tail;
4501 gfc_expr *e, *p;
4502 match m;
4504 tail = NULL;
4506 if (gfc_match_char ('(') != MATCH_YES)
4507 goto syntax;
4509 for (;;)
4511 m = gfc_match_variable (&p, 0);
4512 if (m == MATCH_ERROR)
4513 goto cleanup;
4514 if (m == MATCH_NO)
4515 goto syntax;
4517 if (gfc_check_do_variable (p->symtree))
4518 goto cleanup;
4520 /* F2008, C1242. */
4521 if (gfc_is_coindexed (p))
4523 gfc_error ("Pointer object at %C shall not be coindexed");
4524 goto cleanup;
4527 /* build ' => NULL() '. */
4528 e = gfc_get_null_expr (&gfc_current_locus);
4530 /* Chain to list. */
4531 if (tail == NULL)
4533 tail = &new_st;
4534 tail->op = EXEC_POINTER_ASSIGN;
4536 else
4538 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
4539 tail = tail->next;
4542 tail->expr1 = p;
4543 tail->expr2 = e;
4545 if (gfc_match (" )%t") == MATCH_YES)
4546 break;
4547 if (gfc_match_char (',') != MATCH_YES)
4548 goto syntax;
4551 return MATCH_YES;
4553 syntax:
4554 gfc_syntax_error (ST_NULLIFY);
4556 cleanup:
4557 gfc_free_statements (new_st.next);
4558 new_st.next = NULL;
4559 gfc_free_expr (new_st.expr1);
4560 new_st.expr1 = NULL;
4561 gfc_free_expr (new_st.expr2);
4562 new_st.expr2 = NULL;
4563 return MATCH_ERROR;
4567 /* Match a DEALLOCATE statement. */
4569 match
4570 gfc_match_deallocate (void)
4572 gfc_alloc *head, *tail;
4573 gfc_expr *stat, *errmsg, *tmp;
4574 gfc_symbol *sym;
4575 match m;
4576 bool saw_stat, saw_errmsg, b1, b2;
4578 head = tail = NULL;
4579 stat = errmsg = tmp = NULL;
4580 saw_stat = saw_errmsg = false;
4582 if (gfc_match_char ('(') != MATCH_YES)
4583 goto syntax;
4585 for (;;)
4587 if (head == NULL)
4588 head = tail = gfc_get_alloc ();
4589 else
4591 tail->next = gfc_get_alloc ();
4592 tail = tail->next;
4595 m = gfc_match_variable (&tail->expr, 0);
4596 if (m == MATCH_ERROR)
4597 goto cleanup;
4598 if (m == MATCH_NO)
4599 goto syntax;
4601 if (gfc_check_do_variable (tail->expr->symtree))
4602 goto cleanup;
4604 sym = tail->expr->symtree->n.sym;
4606 bool impure = gfc_impure_variable (sym);
4607 if (impure && gfc_pure (NULL))
4609 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4610 goto cleanup;
4613 if (impure)
4614 gfc_unset_implicit_pure (NULL);
4616 if (gfc_is_coarray (tail->expr)
4617 && gfc_find_state (COMP_DO_CONCURRENT))
4619 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4620 goto cleanup;
4623 if (gfc_is_coarray (tail->expr)
4624 && gfc_find_state (COMP_CRITICAL))
4626 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4627 goto cleanup;
4630 /* FIXME: disable the checking on derived types. */
4631 b1 = !(tail->expr->ref
4632 && (tail->expr->ref->type == REF_COMPONENT
4633 || tail->expr->ref->type == REF_ARRAY));
4634 if (sym && sym->ts.type == BT_CLASS)
4635 b2 = !(CLASS_DATA (sym) && (CLASS_DATA (sym)->attr.allocatable
4636 || CLASS_DATA (sym)->attr.class_pointer));
4637 else
4638 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4639 || sym->attr.proc_pointer);
4640 if (b1 && b2)
4642 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4643 "nor an allocatable variable");
4644 goto cleanup;
4647 if (gfc_match_char (',') != MATCH_YES)
4648 break;
4650 dealloc_opt_list:
4652 m = gfc_match (" stat = %v", &tmp);
4653 if (m == MATCH_ERROR)
4654 goto cleanup;
4655 if (m == MATCH_YES)
4657 if (saw_stat)
4659 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
4660 gfc_free_expr (tmp);
4661 goto cleanup;
4664 stat = tmp;
4665 saw_stat = true;
4667 if (gfc_check_do_variable (stat->symtree))
4668 goto cleanup;
4670 if (gfc_match_char (',') == MATCH_YES)
4671 goto dealloc_opt_list;
4674 m = gfc_match (" errmsg = %v", &tmp);
4675 if (m == MATCH_ERROR)
4676 goto cleanup;
4677 if (m == MATCH_YES)
4679 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
4680 goto cleanup;
4682 if (saw_errmsg)
4684 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
4685 gfc_free_expr (tmp);
4686 goto cleanup;
4689 errmsg = tmp;
4690 saw_errmsg = true;
4692 if (gfc_match_char (',') == MATCH_YES)
4693 goto dealloc_opt_list;
4696 gfc_gobble_whitespace ();
4698 if (gfc_peek_char () == ')')
4699 break;
4702 if (gfc_match (" )%t") != MATCH_YES)
4703 goto syntax;
4705 new_st.op = EXEC_DEALLOCATE;
4706 new_st.expr1 = stat;
4707 new_st.expr2 = errmsg;
4708 new_st.ext.alloc.list = head;
4710 return MATCH_YES;
4712 syntax:
4713 gfc_syntax_error (ST_DEALLOCATE);
4715 cleanup:
4716 gfc_free_expr (errmsg);
4717 gfc_free_expr (stat);
4718 gfc_free_alloc_list (head);
4719 return MATCH_ERROR;
4723 /* Match a RETURN statement. */
4725 match
4726 gfc_match_return (void)
4728 gfc_expr *e;
4729 match m;
4730 gfc_compile_state s;
4732 e = NULL;
4734 if (gfc_find_state (COMP_CRITICAL))
4736 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4737 return MATCH_ERROR;
4740 if (gfc_find_state (COMP_DO_CONCURRENT))
4742 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4743 return MATCH_ERROR;
4746 if (gfc_match_eos () == MATCH_YES)
4747 goto done;
4749 if (!gfc_find_state (COMP_SUBROUTINE))
4751 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4752 "a SUBROUTINE");
4753 goto cleanup;
4756 if (gfc_current_form == FORM_FREE)
4758 /* The following are valid, so we can't require a blank after the
4759 RETURN keyword:
4760 return+1
4761 return(1) */
4762 char c = gfc_peek_ascii_char ();
4763 if (ISALPHA (c) || ISDIGIT (c))
4764 return MATCH_NO;
4767 m = gfc_match (" %e%t", &e);
4768 if (m == MATCH_YES)
4769 goto done;
4770 if (m == MATCH_ERROR)
4771 goto cleanup;
4773 gfc_syntax_error (ST_RETURN);
4775 cleanup:
4776 gfc_free_expr (e);
4777 return MATCH_ERROR;
4779 done:
4780 gfc_enclosing_unit (&s);
4781 if (s == COMP_PROGRAM
4782 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4783 "main program at %C"))
4784 return MATCH_ERROR;
4786 new_st.op = EXEC_RETURN;
4787 new_st.expr1 = e;
4789 return MATCH_YES;
4793 /* Match the call of a type-bound procedure, if CALL%var has already been
4794 matched and var found to be a derived-type variable. */
4796 static match
4797 match_typebound_call (gfc_symtree* varst)
4799 gfc_expr* base;
4800 match m;
4802 base = gfc_get_expr ();
4803 base->expr_type = EXPR_VARIABLE;
4804 base->symtree = varst;
4805 base->where = gfc_current_locus;
4806 gfc_set_sym_referenced (varst->n.sym);
4808 m = gfc_match_varspec (base, 0, true, true);
4809 if (m == MATCH_NO)
4810 gfc_error ("Expected component reference at %C");
4811 if (m != MATCH_YES)
4813 gfc_free_expr (base);
4814 return MATCH_ERROR;
4817 if (gfc_match_eos () != MATCH_YES)
4819 gfc_error ("Junk after CALL at %C");
4820 gfc_free_expr (base);
4821 return MATCH_ERROR;
4824 if (base->expr_type == EXPR_COMPCALL)
4825 new_st.op = EXEC_COMPCALL;
4826 else if (base->expr_type == EXPR_PPC)
4827 new_st.op = EXEC_CALL_PPC;
4828 else
4830 gfc_error ("Expected type-bound procedure or procedure pointer component "
4831 "at %C");
4832 gfc_free_expr (base);
4833 return MATCH_ERROR;
4835 new_st.expr1 = base;
4837 return MATCH_YES;
4841 /* Match a CALL statement. The tricky part here are possible
4842 alternate return specifiers. We handle these by having all
4843 "subroutines" actually return an integer via a register that gives
4844 the return number. If the call specifies alternate returns, we
4845 generate code for a SELECT statement whose case clauses contain
4846 GOTOs to the various labels. */
4848 match
4849 gfc_match_call (void)
4851 char name[GFC_MAX_SYMBOL_LEN + 1];
4852 gfc_actual_arglist *a, *arglist;
4853 gfc_case *new_case;
4854 gfc_symbol *sym;
4855 gfc_symtree *st;
4856 gfc_code *c;
4857 match m;
4858 int i;
4860 arglist = NULL;
4862 m = gfc_match ("% %n", name);
4863 if (m == MATCH_NO)
4864 goto syntax;
4865 if (m != MATCH_YES)
4866 return m;
4868 if (gfc_get_ha_sym_tree (name, &st))
4869 return MATCH_ERROR;
4871 sym = st->n.sym;
4873 /* If this is a variable of derived-type, it probably starts a type-bound
4874 procedure call. */
4875 if ((sym->attr.flavor != FL_PROCEDURE
4876 || gfc_is_function_return_value (sym, gfc_current_ns))
4877 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
4878 return match_typebound_call (st);
4880 /* If it does not seem to be callable (include functions so that the
4881 right association is made. They are thrown out in resolution.)
4882 ... */
4883 if (!sym->attr.generic
4884 && !sym->attr.subroutine
4885 && !sym->attr.function)
4887 if (!(sym->attr.external && !sym->attr.referenced))
4889 /* ...create a symbol in this scope... */
4890 if (sym->ns != gfc_current_ns
4891 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
4892 return MATCH_ERROR;
4894 if (sym != st->n.sym)
4895 sym = st->n.sym;
4898 /* ...and then to try to make the symbol into a subroutine. */
4899 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
4900 return MATCH_ERROR;
4903 gfc_set_sym_referenced (sym);
4905 if (gfc_match_eos () != MATCH_YES)
4907 m = gfc_match_actual_arglist (1, &arglist);
4908 if (m == MATCH_NO)
4909 goto syntax;
4910 if (m == MATCH_ERROR)
4911 goto cleanup;
4913 if (gfc_match_eos () != MATCH_YES)
4914 goto syntax;
4917 /* If any alternate return labels were found, construct a SELECT
4918 statement that will jump to the right place. */
4920 i = 0;
4921 for (a = arglist; a; a = a->next)
4922 if (a->expr == NULL)
4924 i = 1;
4925 break;
4928 if (i)
4930 gfc_symtree *select_st;
4931 gfc_symbol *select_sym;
4932 char name[GFC_MAX_SYMBOL_LEN + 1];
4934 new_st.next = c = gfc_get_code (EXEC_SELECT);
4935 sprintf (name, "_result_%s", sym->name);
4936 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
4938 select_sym = select_st->n.sym;
4939 select_sym->ts.type = BT_INTEGER;
4940 select_sym->ts.kind = gfc_default_integer_kind;
4941 gfc_set_sym_referenced (select_sym);
4942 c->expr1 = gfc_get_expr ();
4943 c->expr1->expr_type = EXPR_VARIABLE;
4944 c->expr1->symtree = select_st;
4945 c->expr1->ts = select_sym->ts;
4946 c->expr1->where = gfc_current_locus;
4948 i = 0;
4949 for (a = arglist; a; a = a->next)
4951 if (a->expr != NULL)
4952 continue;
4954 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
4955 continue;
4957 i++;
4959 c->block = gfc_get_code (EXEC_SELECT);
4960 c = c->block;
4962 new_case = gfc_get_case ();
4963 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
4964 new_case->low = new_case->high;
4965 c->ext.block.case_list = new_case;
4967 c->next = gfc_get_code (EXEC_GOTO);
4968 c->next->label1 = a->label;
4972 new_st.op = EXEC_CALL;
4973 new_st.symtree = st;
4974 new_st.ext.actual = arglist;
4976 return MATCH_YES;
4978 syntax:
4979 gfc_syntax_error (ST_CALL);
4981 cleanup:
4982 gfc_free_actual_arglist (arglist);
4983 return MATCH_ERROR;
4987 /* Given a name, return a pointer to the common head structure,
4988 creating it if it does not exist. If FROM_MODULE is nonzero, we
4989 mangle the name so that it doesn't interfere with commons defined
4990 in the using namespace.
4991 TODO: Add to global symbol tree. */
4993 gfc_common_head *
4994 gfc_get_common (const char *name, int from_module)
4996 gfc_symtree *st;
4997 static int serial = 0;
4998 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
5000 if (from_module)
5002 /* A use associated common block is only needed to correctly layout
5003 the variables it contains. */
5004 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
5005 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
5007 else
5009 st = gfc_find_symtree (gfc_current_ns->common_root, name);
5011 if (st == NULL)
5012 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
5015 if (st->n.common == NULL)
5017 st->n.common = gfc_get_common_head ();
5018 st->n.common->where = gfc_current_locus;
5019 strcpy (st->n.common->name, name);
5022 return st->n.common;
5026 /* Match a common block name. */
5028 match match_common_name (char *name)
5030 match m;
5032 if (gfc_match_char ('/') == MATCH_NO)
5034 name[0] = '\0';
5035 return MATCH_YES;
5038 if (gfc_match_char ('/') == MATCH_YES)
5040 name[0] = '\0';
5041 return MATCH_YES;
5044 m = gfc_match_name (name);
5046 if (m == MATCH_ERROR)
5047 return MATCH_ERROR;
5048 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
5049 return MATCH_YES;
5051 gfc_error ("Syntax error in common block name at %C");
5052 return MATCH_ERROR;
5056 /* Match a COMMON statement. */
5058 match
5059 gfc_match_common (void)
5061 gfc_symbol *sym, **head, *tail, *other;
5062 char name[GFC_MAX_SYMBOL_LEN + 1];
5063 gfc_common_head *t;
5064 gfc_array_spec *as;
5065 gfc_equiv *e1, *e2;
5066 match m;
5068 as = NULL;
5070 for (;;)
5072 m = match_common_name (name);
5073 if (m == MATCH_ERROR)
5074 goto cleanup;
5076 if (name[0] == '\0')
5078 t = &gfc_current_ns->blank_common;
5079 if (t->head == NULL)
5080 t->where = gfc_current_locus;
5082 else
5084 t = gfc_get_common (name, 0);
5086 head = &t->head;
5088 if (*head == NULL)
5089 tail = NULL;
5090 else
5092 tail = *head;
5093 while (tail->common_next)
5094 tail = tail->common_next;
5097 /* Grab the list of symbols. */
5098 for (;;)
5100 m = gfc_match_symbol (&sym, 0);
5101 if (m == MATCH_ERROR)
5102 goto cleanup;
5103 if (m == MATCH_NO)
5104 goto syntax;
5106 /* See if we know the current common block is bind(c), and if
5107 so, then see if we can check if the symbol is (which it'll
5108 need to be). This can happen if the bind(c) attr stmt was
5109 applied to the common block, and the variable(s) already
5110 defined, before declaring the common block. */
5111 if (t->is_bind_c == 1)
5113 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
5115 /* If we find an error, just print it and continue,
5116 cause it's just semantic, and we can see if there
5117 are more errors. */
5118 gfc_error_now ("Variable %qs at %L in common block %qs "
5119 "at %C must be declared with a C "
5120 "interoperable kind since common block "
5121 "%qs is bind(c)",
5122 sym->name, &(sym->declared_at), t->name,
5123 t->name);
5126 if (sym->attr.is_bind_c == 1)
5127 gfc_error_now ("Variable %qs in common block %qs at %C can not "
5128 "be bind(c) since it is not global", sym->name,
5129 t->name);
5132 if (sym->attr.in_common)
5134 gfc_error ("Symbol %qs at %C is already in a COMMON block",
5135 sym->name);
5136 goto cleanup;
5139 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
5140 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
5142 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
5143 "%C can only be COMMON in BLOCK DATA",
5144 sym->name))
5145 goto cleanup;
5148 /* Deal with an optional array specification after the
5149 symbol name. */
5150 m = gfc_match_array_spec (&as, true, true);
5151 if (m == MATCH_ERROR)
5152 goto cleanup;
5154 if (m == MATCH_YES)
5156 if (as->type != AS_EXPLICIT)
5158 gfc_error ("Array specification for symbol %qs in COMMON "
5159 "at %C must be explicit", sym->name);
5160 goto cleanup;
5163 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
5164 goto cleanup;
5166 if (sym->attr.pointer)
5168 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5169 "POINTER array", sym->name);
5170 goto cleanup;
5173 sym->as = as;
5174 as = NULL;
5178 /* Add the in_common attribute, but ignore the reported errors
5179 if any, and continue matching. */
5180 gfc_add_in_common (&sym->attr, sym->name, NULL);
5182 sym->common_block = t;
5183 sym->common_block->refs++;
5185 if (tail != NULL)
5186 tail->common_next = sym;
5187 else
5188 *head = sym;
5190 tail = sym;
5192 sym->common_head = t;
5194 /* Check to see if the symbol is already in an equivalence group.
5195 If it is, set the other members as being in common. */
5196 if (sym->attr.in_equivalence)
5198 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
5200 for (e2 = e1; e2; e2 = e2->eq)
5201 if (e2->expr->symtree->n.sym == sym)
5202 goto equiv_found;
5204 continue;
5206 equiv_found:
5208 for (e2 = e1; e2; e2 = e2->eq)
5210 other = e2->expr->symtree->n.sym;
5211 if (other->common_head
5212 && other->common_head != sym->common_head)
5214 gfc_error ("Symbol %qs, in COMMON block %qs at "
5215 "%C is being indirectly equivalenced to "
5216 "another COMMON block %qs",
5217 sym->name, sym->common_head->name,
5218 other->common_head->name);
5219 goto cleanup;
5221 other->attr.in_common = 1;
5222 other->common_head = t;
5228 gfc_gobble_whitespace ();
5229 if (gfc_match_eos () == MATCH_YES)
5230 goto done;
5231 if (gfc_peek_ascii_char () == '/')
5232 break;
5233 if (gfc_match_char (',') != MATCH_YES)
5234 goto syntax;
5235 gfc_gobble_whitespace ();
5236 if (gfc_peek_ascii_char () == '/')
5237 break;
5241 done:
5242 return MATCH_YES;
5244 syntax:
5245 gfc_syntax_error (ST_COMMON);
5247 cleanup:
5248 gfc_free_array_spec (as);
5249 return MATCH_ERROR;
5253 /* Match a BLOCK DATA program unit. */
5255 match
5256 gfc_match_block_data (void)
5258 char name[GFC_MAX_SYMBOL_LEN + 1];
5259 gfc_symbol *sym;
5260 match m;
5262 if (!gfc_notify_std (GFC_STD_F2018_OBS, "BLOCK DATA construct at %L",
5263 &gfc_current_locus))
5264 return MATCH_ERROR;
5266 if (gfc_match_eos () == MATCH_YES)
5268 gfc_new_block = NULL;
5269 return MATCH_YES;
5272 m = gfc_match ("% %n%t", name);
5273 if (m != MATCH_YES)
5274 return MATCH_ERROR;
5276 if (gfc_get_symbol (name, NULL, &sym))
5277 return MATCH_ERROR;
5279 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
5280 return MATCH_ERROR;
5282 gfc_new_block = sym;
5284 return MATCH_YES;
5288 /* Free a namelist structure. */
5290 void
5291 gfc_free_namelist (gfc_namelist *name)
5293 gfc_namelist *n;
5295 for (; name; name = n)
5297 n = name->next;
5298 free (name);
5303 /* Free an OpenMP namelist structure. */
5305 void
5306 gfc_free_omp_namelist (gfc_omp_namelist *name)
5308 gfc_omp_namelist *n;
5310 for (; name; name = n)
5312 gfc_free_expr (name->expr);
5313 if (name->udr)
5315 if (name->udr->combiner)
5316 gfc_free_statement (name->udr->combiner);
5317 if (name->udr->initializer)
5318 gfc_free_statement (name->udr->initializer);
5319 free (name->udr);
5321 n = name->next;
5322 free (name);
5327 /* Match a NAMELIST statement. */
5329 match
5330 gfc_match_namelist (void)
5332 gfc_symbol *group_name, *sym;
5333 gfc_namelist *nl;
5334 match m, m2;
5336 m = gfc_match (" / %s /", &group_name);
5337 if (m == MATCH_NO)
5338 goto syntax;
5339 if (m == MATCH_ERROR)
5340 goto error;
5342 for (;;)
5344 if (group_name->ts.type != BT_UNKNOWN)
5346 gfc_error ("Namelist group name %qs at %C already has a basic "
5347 "type of %s", group_name->name,
5348 gfc_typename (&group_name->ts));
5349 return MATCH_ERROR;
5352 if (group_name->attr.flavor == FL_NAMELIST
5353 && group_name->attr.use_assoc
5354 && !gfc_notify_std (GFC_STD_GNU, "Namelist group name %qs "
5355 "at %C already is USE associated and can"
5356 "not be respecified.", group_name->name))
5357 return MATCH_ERROR;
5359 if (group_name->attr.flavor != FL_NAMELIST
5360 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
5361 group_name->name, NULL))
5362 return MATCH_ERROR;
5364 for (;;)
5366 m = gfc_match_symbol (&sym, 1);
5367 if (m == MATCH_NO)
5368 goto syntax;
5369 if (m == MATCH_ERROR)
5370 goto error;
5372 if (sym->attr.in_namelist == 0
5373 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
5374 goto error;
5376 /* Use gfc_error_check here, rather than goto error, so that
5377 these are the only errors for the next two lines. */
5378 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
5380 gfc_error ("Assumed size array %qs in namelist %qs at "
5381 "%C is not allowed", sym->name, group_name->name);
5382 gfc_error_check ();
5385 nl = gfc_get_namelist ();
5386 nl->sym = sym;
5387 sym->refs++;
5389 if (group_name->namelist == NULL)
5390 group_name->namelist = group_name->namelist_tail = nl;
5391 else
5393 group_name->namelist_tail->next = nl;
5394 group_name->namelist_tail = nl;
5397 if (gfc_match_eos () == MATCH_YES)
5398 goto done;
5400 m = gfc_match_char (',');
5402 if (gfc_match_char ('/') == MATCH_YES)
5404 m2 = gfc_match (" %s /", &group_name);
5405 if (m2 == MATCH_YES)
5406 break;
5407 if (m2 == MATCH_ERROR)
5408 goto error;
5409 goto syntax;
5412 if (m != MATCH_YES)
5413 goto syntax;
5417 done:
5418 return MATCH_YES;
5420 syntax:
5421 gfc_syntax_error (ST_NAMELIST);
5423 error:
5424 return MATCH_ERROR;
5428 /* Match a MODULE statement. */
5430 match
5431 gfc_match_module (void)
5433 match m;
5435 m = gfc_match (" %s%t", &gfc_new_block);
5436 if (m != MATCH_YES)
5437 return m;
5439 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
5440 gfc_new_block->name, NULL))
5441 return MATCH_ERROR;
5443 return MATCH_YES;
5447 /* Free equivalence sets and lists. Recursively is the easiest way to
5448 do this. */
5450 void
5451 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
5453 if (eq == stop)
5454 return;
5456 gfc_free_equiv (eq->eq);
5457 gfc_free_equiv_until (eq->next, stop);
5458 gfc_free_expr (eq->expr);
5459 free (eq);
5463 void
5464 gfc_free_equiv (gfc_equiv *eq)
5466 gfc_free_equiv_until (eq, NULL);
5470 /* Match an EQUIVALENCE statement. */
5472 match
5473 gfc_match_equivalence (void)
5475 gfc_equiv *eq, *set, *tail;
5476 gfc_ref *ref;
5477 gfc_symbol *sym;
5478 match m;
5479 gfc_common_head *common_head = NULL;
5480 bool common_flag;
5481 int cnt;
5483 tail = NULL;
5485 for (;;)
5487 eq = gfc_get_equiv ();
5488 if (tail == NULL)
5489 tail = eq;
5491 eq->next = gfc_current_ns->equiv;
5492 gfc_current_ns->equiv = eq;
5494 if (gfc_match_char ('(') != MATCH_YES)
5495 goto syntax;
5497 set = eq;
5498 common_flag = FALSE;
5499 cnt = 0;
5501 for (;;)
5503 m = gfc_match_equiv_variable (&set->expr);
5504 if (m == MATCH_ERROR)
5505 goto cleanup;
5506 if (m == MATCH_NO)
5507 goto syntax;
5509 /* count the number of objects. */
5510 cnt++;
5512 if (gfc_match_char ('%') == MATCH_YES)
5514 gfc_error ("Derived type component %C is not a "
5515 "permitted EQUIVALENCE member");
5516 goto cleanup;
5519 for (ref = set->expr->ref; ref; ref = ref->next)
5520 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
5522 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5523 "be an array section");
5524 goto cleanup;
5527 sym = set->expr->symtree->n.sym;
5529 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
5530 goto cleanup;
5532 if (sym->attr.in_common)
5534 common_flag = TRUE;
5535 common_head = sym->common_head;
5538 if (gfc_match_char (')') == MATCH_YES)
5539 break;
5541 if (gfc_match_char (',') != MATCH_YES)
5542 goto syntax;
5544 set->eq = gfc_get_equiv ();
5545 set = set->eq;
5548 if (cnt < 2)
5550 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5551 goto cleanup;
5554 /* If one of the members of an equivalence is in common, then
5555 mark them all as being in common. Before doing this, check
5556 that members of the equivalence group are not in different
5557 common blocks. */
5558 if (common_flag)
5559 for (set = eq; set; set = set->eq)
5561 sym = set->expr->symtree->n.sym;
5562 if (sym->common_head && sym->common_head != common_head)
5564 gfc_error ("Attempt to indirectly overlap COMMON "
5565 "blocks %s and %s by EQUIVALENCE at %C",
5566 sym->common_head->name, common_head->name);
5567 goto cleanup;
5569 sym->attr.in_common = 1;
5570 sym->common_head = common_head;
5573 if (gfc_match_eos () == MATCH_YES)
5574 break;
5575 if (gfc_match_char (',') != MATCH_YES)
5577 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5578 goto cleanup;
5582 if (!gfc_notify_std (GFC_STD_F2018_OBS, "EQUIVALENCE statement at %C"))
5583 return MATCH_ERROR;
5585 return MATCH_YES;
5587 syntax:
5588 gfc_syntax_error (ST_EQUIVALENCE);
5590 cleanup:
5591 eq = tail->next;
5592 tail->next = NULL;
5594 gfc_free_equiv (gfc_current_ns->equiv);
5595 gfc_current_ns->equiv = eq;
5597 return MATCH_ERROR;
5601 /* Check that a statement function is not recursive. This is done by looking
5602 for the statement function symbol(sym) by looking recursively through its
5603 expression(e). If a reference to sym is found, true is returned.
5604 12.5.4 requires that any variable of function that is implicitly typed
5605 shall have that type confirmed by any subsequent type declaration. The
5606 implicit typing is conveniently done here. */
5607 static bool
5608 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
5610 static bool
5611 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5614 if (e == NULL)
5615 return false;
5617 switch (e->expr_type)
5619 case EXPR_FUNCTION:
5620 if (e->symtree == NULL)
5621 return false;
5623 /* Check the name before testing for nested recursion! */
5624 if (sym->name == e->symtree->n.sym->name)
5625 return true;
5627 /* Catch recursion via other statement functions. */
5628 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
5629 && e->symtree->n.sym->value
5630 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
5631 return true;
5633 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5634 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5636 break;
5638 case EXPR_VARIABLE:
5639 if (e->symtree && sym->name == e->symtree->n.sym->name)
5640 return true;
5642 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5643 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5644 break;
5646 default:
5647 break;
5650 return false;
5654 static bool
5655 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
5657 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
5661 /* Match a statement function declaration. It is so easy to match
5662 non-statement function statements with a MATCH_ERROR as opposed to
5663 MATCH_NO that we suppress error message in most cases. */
5665 match
5666 gfc_match_st_function (void)
5668 gfc_error_buffer old_error;
5669 gfc_symbol *sym;
5670 gfc_expr *expr;
5671 match m;
5673 m = gfc_match_symbol (&sym, 0);
5674 if (m != MATCH_YES)
5675 return m;
5677 gfc_push_error (&old_error);
5679 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
5680 goto undo_error;
5682 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
5683 goto undo_error;
5685 m = gfc_match (" = %e%t", &expr);
5686 if (m == MATCH_NO)
5687 goto undo_error;
5689 gfc_free_error (&old_error);
5691 if (m == MATCH_ERROR)
5692 return m;
5694 if (recursive_stmt_fcn (expr, sym))
5696 gfc_error ("Statement function at %L is recursive", &expr->where);
5697 return MATCH_ERROR;
5700 sym->value = expr;
5702 if ((gfc_current_state () == COMP_FUNCTION
5703 || gfc_current_state () == COMP_SUBROUTINE)
5704 && gfc_state_stack->previous->state == COMP_INTERFACE)
5706 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
5707 &expr->where);
5708 return MATCH_ERROR;
5711 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
5712 return MATCH_ERROR;
5714 return MATCH_YES;
5716 undo_error:
5717 gfc_pop_error (&old_error);
5718 return MATCH_NO;
5722 /* Match an assignment to a pointer function (F2008). This could, in
5723 general be ambiguous with a statement function. In this implementation
5724 it remains so if it is the first statement after the specification
5725 block. */
5727 match
5728 gfc_match_ptr_fcn_assign (void)
5730 gfc_error_buffer old_error;
5731 locus old_loc;
5732 gfc_symbol *sym;
5733 gfc_expr *expr;
5734 match m;
5735 char name[GFC_MAX_SYMBOL_LEN + 1];
5737 old_loc = gfc_current_locus;
5738 m = gfc_match_name (name);
5739 if (m != MATCH_YES)
5740 return m;
5742 gfc_find_symbol (name, NULL, 1, &sym);
5743 if (sym && sym->attr.flavor != FL_PROCEDURE)
5744 return MATCH_NO;
5746 gfc_push_error (&old_error);
5748 if (sym && sym->attr.function)
5749 goto match_actual_arglist;
5751 gfc_current_locus = old_loc;
5752 m = gfc_match_symbol (&sym, 0);
5753 if (m != MATCH_YES)
5754 return m;
5756 if (!gfc_add_procedure (&sym->attr, PROC_UNKNOWN, sym->name, NULL))
5757 goto undo_error;
5759 match_actual_arglist:
5760 gfc_current_locus = old_loc;
5761 m = gfc_match (" %e", &expr);
5762 if (m != MATCH_YES)
5763 goto undo_error;
5765 new_st.op = EXEC_ASSIGN;
5766 new_st.expr1 = expr;
5767 expr = NULL;
5769 m = gfc_match (" = %e%t", &expr);
5770 if (m != MATCH_YES)
5771 goto undo_error;
5773 new_st.expr2 = expr;
5774 return MATCH_YES;
5776 undo_error:
5777 gfc_pop_error (&old_error);
5778 return MATCH_NO;
5782 /***************** SELECT CASE subroutines ******************/
5784 /* Free a single case structure. */
5786 static void
5787 free_case (gfc_case *p)
5789 if (p->low == p->high)
5790 p->high = NULL;
5791 gfc_free_expr (p->low);
5792 gfc_free_expr (p->high);
5793 free (p);
5797 /* Free a list of case structures. */
5799 void
5800 gfc_free_case_list (gfc_case *p)
5802 gfc_case *q;
5804 for (; p; p = q)
5806 q = p->next;
5807 free_case (p);
5812 /* Match a single case selector. Combining the requirements of F08:C830
5813 and F08:C832 (R838) means that the case-value must have either CHARACTER,
5814 INTEGER, or LOGICAL type. */
5816 static match
5817 match_case_selector (gfc_case **cp)
5819 gfc_case *c;
5820 match m;
5822 c = gfc_get_case ();
5823 c->where = gfc_current_locus;
5825 if (gfc_match_char (':') == MATCH_YES)
5827 m = gfc_match_init_expr (&c->high);
5828 if (m == MATCH_NO)
5829 goto need_expr;
5830 if (m == MATCH_ERROR)
5831 goto cleanup;
5833 if (c->high->ts.type != BT_LOGICAL && c->high->ts.type != BT_INTEGER
5834 && c->high->ts.type != BT_CHARACTER)
5836 gfc_error ("Expression in CASE selector at %L cannot be %s",
5837 &c->high->where, gfc_typename (&c->high->ts));
5838 goto cleanup;
5841 else
5843 m = gfc_match_init_expr (&c->low);
5844 if (m == MATCH_ERROR)
5845 goto cleanup;
5846 if (m == MATCH_NO)
5847 goto need_expr;
5849 if (c->low->ts.type != BT_LOGICAL && c->low->ts.type != BT_INTEGER
5850 && c->low->ts.type != BT_CHARACTER)
5852 gfc_error ("Expression in CASE selector at %L cannot be %s",
5853 &c->low->where, gfc_typename (&c->low->ts));
5854 goto cleanup;
5857 /* If we're not looking at a ':' now, make a range out of a single
5858 target. Else get the upper bound for the case range. */
5859 if (gfc_match_char (':') != MATCH_YES)
5860 c->high = c->low;
5861 else
5863 m = gfc_match_init_expr (&c->high);
5864 if (m == MATCH_ERROR)
5865 goto cleanup;
5866 /* MATCH_NO is fine. It's OK if nothing is there! */
5870 *cp = c;
5871 return MATCH_YES;
5873 need_expr:
5874 gfc_error ("Expected initialization expression in CASE at %C");
5876 cleanup:
5877 free_case (c);
5878 return MATCH_ERROR;
5882 /* Match the end of a case statement. */
5884 static match
5885 match_case_eos (void)
5887 char name[GFC_MAX_SYMBOL_LEN + 1];
5888 match m;
5890 if (gfc_match_eos () == MATCH_YES)
5891 return MATCH_YES;
5893 /* If the case construct doesn't have a case-construct-name, we
5894 should have matched the EOS. */
5895 if (!gfc_current_block ())
5896 return MATCH_NO;
5898 gfc_gobble_whitespace ();
5900 m = gfc_match_name (name);
5901 if (m != MATCH_YES)
5902 return m;
5904 if (strcmp (name, gfc_current_block ()->name) != 0)
5906 gfc_error ("Expected block name %qs of SELECT construct at %C",
5907 gfc_current_block ()->name);
5908 return MATCH_ERROR;
5911 return gfc_match_eos ();
5915 /* Match a SELECT statement. */
5917 match
5918 gfc_match_select (void)
5920 gfc_expr *expr;
5921 match m;
5923 m = gfc_match_label ();
5924 if (m == MATCH_ERROR)
5925 return m;
5927 m = gfc_match (" select case ( %e )%t", &expr);
5928 if (m != MATCH_YES)
5929 return m;
5931 new_st.op = EXEC_SELECT;
5932 new_st.expr1 = expr;
5934 return MATCH_YES;
5938 /* Transfer the selector typespec to the associate name. */
5940 static void
5941 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
5943 gfc_ref *ref;
5944 gfc_symbol *assoc_sym;
5945 int rank = 0;
5947 assoc_sym = associate->symtree->n.sym;
5949 /* At this stage the expression rank and arrayspec dimensions have
5950 not been completely sorted out. We must get the expr2->rank
5951 right here, so that the correct class container is obtained. */
5952 ref = selector->ref;
5953 while (ref && ref->next)
5954 ref = ref->next;
5956 if (selector->ts.type == BT_CLASS && CLASS_DATA (selector)->as
5957 && ref && ref->type == REF_ARRAY)
5959 /* Ensure that the array reference type is set. We cannot use
5960 gfc_resolve_expr at this point, so the usable parts of
5961 resolve.c(resolve_array_ref) are employed to do it. */
5962 if (ref->u.ar.type == AR_UNKNOWN)
5964 ref->u.ar.type = AR_ELEMENT;
5965 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
5966 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5967 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
5968 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
5969 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
5971 ref->u.ar.type = AR_SECTION;
5972 break;
5976 if (ref->u.ar.type == AR_FULL)
5977 selector->rank = CLASS_DATA (selector)->as->rank;
5978 else if (ref->u.ar.type == AR_SECTION)
5979 selector->rank = ref->u.ar.dimen;
5980 else
5981 selector->rank = 0;
5983 rank = selector->rank;
5986 if (rank)
5988 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
5989 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT
5990 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
5991 && ref->u.ar.end[i] == NULL
5992 && ref->u.ar.stride[i] == NULL))
5993 rank--;
5995 if (rank)
5997 assoc_sym->attr.dimension = 1;
5998 assoc_sym->as = gfc_get_array_spec ();
5999 assoc_sym->as->rank = rank;
6000 assoc_sym->as->type = AS_DEFERRED;
6002 else
6003 assoc_sym->as = NULL;
6005 else
6006 assoc_sym->as = NULL;
6008 if (selector->ts.type == BT_CLASS)
6010 /* The correct class container has to be available. */
6011 assoc_sym->ts.type = BT_CLASS;
6012 assoc_sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
6013 assoc_sym->attr.pointer = 1;
6014 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
6019 /* Push the current selector onto the SELECT TYPE stack. */
6021 static void
6022 select_type_push (gfc_symbol *sel)
6024 gfc_select_type_stack *top = gfc_get_select_type_stack ();
6025 top->selector = sel;
6026 top->tmp = NULL;
6027 top->prev = select_type_stack;
6029 select_type_stack = top;
6033 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
6035 static gfc_symtree *
6036 select_intrinsic_set_tmp (gfc_typespec *ts)
6038 char name[GFC_MAX_SYMBOL_LEN];
6039 gfc_symtree *tmp;
6040 HOST_WIDE_INT charlen = 0;
6042 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
6043 return NULL;
6045 if (select_type_stack->selector->ts.type == BT_CLASS
6046 && !select_type_stack->selector->attr.class_ok)
6047 return NULL;
6049 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
6050 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6051 charlen = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
6053 if (ts->type != BT_CHARACTER)
6054 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
6055 ts->kind);
6056 else
6057 snprintf (name, sizeof (name), "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
6058 gfc_basic_typename (ts->type), charlen, ts->kind);
6060 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6061 gfc_add_type (tmp->n.sym, ts, NULL);
6063 /* Copy across the array spec to the selector. */
6064 if (select_type_stack->selector->ts.type == BT_CLASS
6065 && (CLASS_DATA (select_type_stack->selector)->attr.dimension
6066 || CLASS_DATA (select_type_stack->selector)->attr.codimension))
6068 tmp->n.sym->attr.pointer = 1;
6069 tmp->n.sym->attr.dimension
6070 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
6071 tmp->n.sym->attr.codimension
6072 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
6073 tmp->n.sym->as
6074 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
6077 gfc_set_sym_referenced (tmp->n.sym);
6078 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
6079 tmp->n.sym->attr.select_type_temporary = 1;
6081 return tmp;
6085 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
6087 static void
6088 select_type_set_tmp (gfc_typespec *ts)
6090 char name[GFC_MAX_SYMBOL_LEN];
6091 gfc_symtree *tmp = NULL;
6093 if (!ts)
6095 select_type_stack->tmp = NULL;
6096 return;
6099 tmp = select_intrinsic_set_tmp (ts);
6101 if (tmp == NULL)
6103 if (!ts->u.derived)
6104 return;
6106 if (ts->type == BT_CLASS)
6107 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
6108 else
6109 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
6110 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6111 gfc_add_type (tmp->n.sym, ts, NULL);
6113 if (select_type_stack->selector->ts.type == BT_CLASS
6114 && select_type_stack->selector->attr.class_ok)
6116 tmp->n.sym->attr.pointer
6117 = CLASS_DATA (select_type_stack->selector)->attr.class_pointer;
6119 /* Copy across the array spec to the selector. */
6120 if (CLASS_DATA (select_type_stack->selector)->attr.dimension
6121 || CLASS_DATA (select_type_stack->selector)->attr.codimension)
6123 tmp->n.sym->attr.dimension
6124 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
6125 tmp->n.sym->attr.codimension
6126 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
6127 tmp->n.sym->as
6128 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
6132 gfc_set_sym_referenced (tmp->n.sym);
6133 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
6134 tmp->n.sym->attr.select_type_temporary = 1;
6136 if (ts->type == BT_CLASS)
6137 gfc_build_class_symbol (&tmp->n.sym->ts, &tmp->n.sym->attr,
6138 &tmp->n.sym->as);
6141 /* Add an association for it, so the rest of the parser knows it is
6142 an associate-name. The target will be set during resolution. */
6143 tmp->n.sym->assoc = gfc_get_association_list ();
6144 tmp->n.sym->assoc->dangling = 1;
6145 tmp->n.sym->assoc->st = tmp;
6147 select_type_stack->tmp = tmp;
6151 /* Match a SELECT TYPE statement. */
6153 match
6154 gfc_match_select_type (void)
6156 gfc_expr *expr1, *expr2 = NULL;
6157 match m;
6158 char name[GFC_MAX_SYMBOL_LEN];
6159 bool class_array;
6160 gfc_symbol *sym;
6161 gfc_namespace *ns = gfc_current_ns;
6163 m = gfc_match_label ();
6164 if (m == MATCH_ERROR)
6165 return m;
6167 m = gfc_match (" select type ( ");
6168 if (m != MATCH_YES)
6169 return m;
6171 gfc_current_ns = gfc_build_block_ns (ns);
6172 m = gfc_match (" %n => %e", name, &expr2);
6173 if (m == MATCH_YES)
6175 expr1 = gfc_get_expr ();
6176 expr1->expr_type = EXPR_VARIABLE;
6177 expr1->where = expr2->where;
6178 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
6180 m = MATCH_ERROR;
6181 goto cleanup;
6184 sym = expr1->symtree->n.sym;
6185 if (expr2->ts.type == BT_UNKNOWN)
6186 sym->attr.untyped = 1;
6187 else
6188 copy_ts_from_selector_to_associate (expr1, expr2);
6190 sym->attr.flavor = FL_VARIABLE;
6191 sym->attr.referenced = 1;
6192 sym->attr.class_ok = 1;
6194 else
6196 m = gfc_match (" %e ", &expr1);
6197 if (m != MATCH_YES)
6199 std::swap (ns, gfc_current_ns);
6200 gfc_free_namespace (ns);
6201 return m;
6205 m = gfc_match (" )%t");
6206 if (m != MATCH_YES)
6208 gfc_error ("parse error in SELECT TYPE statement at %C");
6209 goto cleanup;
6212 /* This ghastly expression seems to be needed to distinguish a CLASS
6213 array, which can have a reference, from other expressions that
6214 have references, such as derived type components, and are not
6215 allowed by the standard.
6216 TODO: see if it is sufficient to exclude component and substring
6217 references. */
6218 class_array = (expr1->expr_type == EXPR_VARIABLE
6219 && expr1->ts.type == BT_CLASS
6220 && CLASS_DATA (expr1)
6221 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
6222 && (CLASS_DATA (expr1)->attr.dimension
6223 || CLASS_DATA (expr1)->attr.codimension)
6224 && expr1->ref
6225 && expr1->ref->type == REF_ARRAY
6226 && expr1->ref->u.ar.type == AR_FULL
6227 && expr1->ref->next == NULL);
6229 /* Check for F03:C811 (F08:C835). */
6230 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
6231 || (!class_array && expr1->ref != NULL)))
6233 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
6234 "use associate-name=>");
6235 m = MATCH_ERROR;
6236 goto cleanup;
6239 new_st.op = EXEC_SELECT_TYPE;
6240 new_st.expr1 = expr1;
6241 new_st.expr2 = expr2;
6242 new_st.ext.block.ns = gfc_current_ns;
6244 select_type_push (expr1->symtree->n.sym);
6245 gfc_current_ns = ns;
6247 return MATCH_YES;
6249 cleanup:
6250 gfc_free_expr (expr1);
6251 gfc_free_expr (expr2);
6252 gfc_undo_symbols ();
6253 std::swap (ns, gfc_current_ns);
6254 gfc_free_namespace (ns);
6255 return m;
6259 /* Match a CASE statement. */
6261 match
6262 gfc_match_case (void)
6264 gfc_case *c, *head, *tail;
6265 match m;
6267 head = tail = NULL;
6269 if (gfc_current_state () != COMP_SELECT)
6271 gfc_error ("Unexpected CASE statement at %C");
6272 return MATCH_ERROR;
6275 if (gfc_match ("% default") == MATCH_YES)
6277 m = match_case_eos ();
6278 if (m == MATCH_NO)
6279 goto syntax;
6280 if (m == MATCH_ERROR)
6281 goto cleanup;
6283 new_st.op = EXEC_SELECT;
6284 c = gfc_get_case ();
6285 c->where = gfc_current_locus;
6286 new_st.ext.block.case_list = c;
6287 return MATCH_YES;
6290 if (gfc_match_char ('(') != MATCH_YES)
6291 goto syntax;
6293 for (;;)
6295 if (match_case_selector (&c) == MATCH_ERROR)
6296 goto cleanup;
6298 if (head == NULL)
6299 head = c;
6300 else
6301 tail->next = c;
6303 tail = c;
6305 if (gfc_match_char (')') == MATCH_YES)
6306 break;
6307 if (gfc_match_char (',') != MATCH_YES)
6308 goto syntax;
6311 m = match_case_eos ();
6312 if (m == MATCH_NO)
6313 goto syntax;
6314 if (m == MATCH_ERROR)
6315 goto cleanup;
6317 new_st.op = EXEC_SELECT;
6318 new_st.ext.block.case_list = head;
6320 return MATCH_YES;
6322 syntax:
6323 gfc_error ("Syntax error in CASE specification at %C");
6325 cleanup:
6326 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
6327 return MATCH_ERROR;
6331 /* Match a TYPE IS statement. */
6333 match
6334 gfc_match_type_is (void)
6336 gfc_case *c = NULL;
6337 match m;
6339 if (gfc_current_state () != COMP_SELECT_TYPE)
6341 gfc_error ("Unexpected TYPE IS statement at %C");
6342 return MATCH_ERROR;
6345 if (gfc_match_char ('(') != MATCH_YES)
6346 goto syntax;
6348 c = gfc_get_case ();
6349 c->where = gfc_current_locus;
6351 m = gfc_match_type_spec (&c->ts);
6352 if (m == MATCH_NO)
6353 goto syntax;
6354 if (m == MATCH_ERROR)
6355 goto cleanup;
6357 if (gfc_match_char (')') != MATCH_YES)
6358 goto syntax;
6360 m = match_case_eos ();
6361 if (m == MATCH_NO)
6362 goto syntax;
6363 if (m == MATCH_ERROR)
6364 goto cleanup;
6366 new_st.op = EXEC_SELECT_TYPE;
6367 new_st.ext.block.case_list = c;
6369 if (c->ts.type == BT_DERIVED && c->ts.u.derived
6370 && (c->ts.u.derived->attr.sequence
6371 || c->ts.u.derived->attr.is_bind_c))
6373 gfc_error ("The type-spec shall not specify a sequence derived "
6374 "type or a type with the BIND attribute in SELECT "
6375 "TYPE at %C [F2003:C815]");
6376 return MATCH_ERROR;
6379 if (c->ts.type == BT_DERIVED
6380 && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
6381 && gfc_spec_list_type (type_param_spec_list, c->ts.u.derived)
6382 != SPEC_ASSUMED)
6384 gfc_error ("All the LEN type parameters in the TYPE IS statement "
6385 "at %C must be ASSUMED");
6386 return MATCH_ERROR;
6389 /* Create temporary variable. */
6390 select_type_set_tmp (&c->ts);
6392 return MATCH_YES;
6394 syntax:
6395 gfc_error ("Syntax error in TYPE IS specification at %C");
6397 cleanup:
6398 if (c != NULL)
6399 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6400 return MATCH_ERROR;
6404 /* Match a CLASS IS or CLASS DEFAULT statement. */
6406 match
6407 gfc_match_class_is (void)
6409 gfc_case *c = NULL;
6410 match m;
6412 if (gfc_current_state () != COMP_SELECT_TYPE)
6413 return MATCH_NO;
6415 if (gfc_match ("% default") == MATCH_YES)
6417 m = match_case_eos ();
6418 if (m == MATCH_NO)
6419 goto syntax;
6420 if (m == MATCH_ERROR)
6421 goto cleanup;
6423 new_st.op = EXEC_SELECT_TYPE;
6424 c = gfc_get_case ();
6425 c->where = gfc_current_locus;
6426 c->ts.type = BT_UNKNOWN;
6427 new_st.ext.block.case_list = c;
6428 select_type_set_tmp (NULL);
6429 return MATCH_YES;
6432 m = gfc_match ("% is");
6433 if (m == MATCH_NO)
6434 goto syntax;
6435 if (m == MATCH_ERROR)
6436 goto cleanup;
6438 if (gfc_match_char ('(') != MATCH_YES)
6439 goto syntax;
6441 c = gfc_get_case ();
6442 c->where = gfc_current_locus;
6444 m = match_derived_type_spec (&c->ts);
6445 if (m == MATCH_NO)
6446 goto syntax;
6447 if (m == MATCH_ERROR)
6448 goto cleanup;
6450 if (c->ts.type == BT_DERIVED)
6451 c->ts.type = BT_CLASS;
6453 if (gfc_match_char (')') != MATCH_YES)
6454 goto syntax;
6456 m = match_case_eos ();
6457 if (m == MATCH_NO)
6458 goto syntax;
6459 if (m == MATCH_ERROR)
6460 goto cleanup;
6462 new_st.op = EXEC_SELECT_TYPE;
6463 new_st.ext.block.case_list = c;
6465 /* Create temporary variable. */
6466 select_type_set_tmp (&c->ts);
6468 return MATCH_YES;
6470 syntax:
6471 gfc_error ("Syntax error in CLASS IS specification at %C");
6473 cleanup:
6474 if (c != NULL)
6475 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6476 return MATCH_ERROR;
6480 /********************* WHERE subroutines ********************/
6482 /* Match the rest of a simple WHERE statement that follows an IF statement.
6485 static match
6486 match_simple_where (void)
6488 gfc_expr *expr;
6489 gfc_code *c;
6490 match m;
6492 m = gfc_match (" ( %e )", &expr);
6493 if (m != MATCH_YES)
6494 return m;
6496 m = gfc_match_assignment ();
6497 if (m == MATCH_NO)
6498 goto syntax;
6499 if (m == MATCH_ERROR)
6500 goto cleanup;
6502 if (gfc_match_eos () != MATCH_YES)
6503 goto syntax;
6505 c = gfc_get_code (EXEC_WHERE);
6506 c->expr1 = expr;
6508 c->next = XCNEW (gfc_code);
6509 *c->next = new_st;
6510 c->next->loc = gfc_current_locus;
6511 gfc_clear_new_st ();
6513 new_st.op = EXEC_WHERE;
6514 new_st.block = c;
6516 return MATCH_YES;
6518 syntax:
6519 gfc_syntax_error (ST_WHERE);
6521 cleanup:
6522 gfc_free_expr (expr);
6523 return MATCH_ERROR;
6527 /* Match a WHERE statement. */
6529 match
6530 gfc_match_where (gfc_statement *st)
6532 gfc_expr *expr;
6533 match m0, m;
6534 gfc_code *c;
6536 m0 = gfc_match_label ();
6537 if (m0 == MATCH_ERROR)
6538 return m0;
6540 m = gfc_match (" where ( %e )", &expr);
6541 if (m != MATCH_YES)
6542 return m;
6544 if (gfc_match_eos () == MATCH_YES)
6546 *st = ST_WHERE_BLOCK;
6547 new_st.op = EXEC_WHERE;
6548 new_st.expr1 = expr;
6549 return MATCH_YES;
6552 m = gfc_match_assignment ();
6553 if (m == MATCH_NO)
6554 gfc_syntax_error (ST_WHERE);
6556 if (m != MATCH_YES)
6558 gfc_free_expr (expr);
6559 return MATCH_ERROR;
6562 /* We've got a simple WHERE statement. */
6563 *st = ST_WHERE;
6564 c = gfc_get_code (EXEC_WHERE);
6565 c->expr1 = expr;
6567 /* Put in the assignment. It will not be processed by add_statement, so we
6568 need to copy the location here. */
6570 c->next = XCNEW (gfc_code);
6571 *c->next = new_st;
6572 c->next->loc = gfc_current_locus;
6573 gfc_clear_new_st ();
6575 new_st.op = EXEC_WHERE;
6576 new_st.block = c;
6578 return MATCH_YES;
6582 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
6583 new_st if successful. */
6585 match
6586 gfc_match_elsewhere (void)
6588 char name[GFC_MAX_SYMBOL_LEN + 1];
6589 gfc_expr *expr;
6590 match m;
6592 if (gfc_current_state () != COMP_WHERE)
6594 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
6595 return MATCH_ERROR;
6598 expr = NULL;
6600 if (gfc_match_char ('(') == MATCH_YES)
6602 m = gfc_match_expr (&expr);
6603 if (m == MATCH_NO)
6604 goto syntax;
6605 if (m == MATCH_ERROR)
6606 return MATCH_ERROR;
6608 if (gfc_match_char (')') != MATCH_YES)
6609 goto syntax;
6612 if (gfc_match_eos () != MATCH_YES)
6614 /* Only makes sense if we have a where-construct-name. */
6615 if (!gfc_current_block ())
6617 m = MATCH_ERROR;
6618 goto cleanup;
6620 /* Better be a name at this point. */
6621 m = gfc_match_name (name);
6622 if (m == MATCH_NO)
6623 goto syntax;
6624 if (m == MATCH_ERROR)
6625 goto cleanup;
6627 if (gfc_match_eos () != MATCH_YES)
6628 goto syntax;
6630 if (strcmp (name, gfc_current_block ()->name) != 0)
6632 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
6633 name, gfc_current_block ()->name);
6634 goto cleanup;
6638 new_st.op = EXEC_WHERE;
6639 new_st.expr1 = expr;
6640 return MATCH_YES;
6642 syntax:
6643 gfc_syntax_error (ST_ELSEWHERE);
6645 cleanup:
6646 gfc_free_expr (expr);
6647 return MATCH_ERROR;