2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / fortran / match.c
blob523e9b2a7f5ed4d340f379d5423a8563830dcf66
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2015 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 "flags.h"
26 #include "gfortran.h"
27 #include "match.h"
28 #include "parse.h"
29 #include "alias.h"
30 #include "tree.h"
31 #include "stringpool.h"
33 int gfc_matching_ptr_assignment = 0;
34 int gfc_matching_procptr_assignment = 0;
35 bool gfc_matching_prefix = false;
37 /* Stack of SELECT TYPE statements. */
38 gfc_select_type_stack *select_type_stack = NULL;
40 /* For debugging and diagnostic purposes. Return the textual representation
41 of the intrinsic operator OP. */
42 const char *
43 gfc_op2string (gfc_intrinsic_op op)
45 switch (op)
47 case INTRINSIC_UPLUS:
48 case INTRINSIC_PLUS:
49 return "+";
51 case INTRINSIC_UMINUS:
52 case INTRINSIC_MINUS:
53 return "-";
55 case INTRINSIC_POWER:
56 return "**";
57 case INTRINSIC_CONCAT:
58 return "//";
59 case INTRINSIC_TIMES:
60 return "*";
61 case INTRINSIC_DIVIDE:
62 return "/";
64 case INTRINSIC_AND:
65 return ".and.";
66 case INTRINSIC_OR:
67 return ".or.";
68 case INTRINSIC_EQV:
69 return ".eqv.";
70 case INTRINSIC_NEQV:
71 return ".neqv.";
73 case INTRINSIC_EQ_OS:
74 return ".eq.";
75 case INTRINSIC_EQ:
76 return "==";
77 case INTRINSIC_NE_OS:
78 return ".ne.";
79 case INTRINSIC_NE:
80 return "/=";
81 case INTRINSIC_GE_OS:
82 return ".ge.";
83 case INTRINSIC_GE:
84 return ">=";
85 case INTRINSIC_LE_OS:
86 return ".le.";
87 case INTRINSIC_LE:
88 return "<=";
89 case INTRINSIC_LT_OS:
90 return ".lt.";
91 case INTRINSIC_LT:
92 return "<";
93 case INTRINSIC_GT_OS:
94 return ".gt.";
95 case INTRINSIC_GT:
96 return ">";
97 case INTRINSIC_NOT:
98 return ".not.";
100 case INTRINSIC_ASSIGN:
101 return "=";
103 case INTRINSIC_PARENTHESES:
104 return "parens";
106 case INTRINSIC_NONE:
107 return "none";
109 default:
110 break;
113 gfc_internal_error ("gfc_op2string(): Bad code");
114 /* Not reached. */
118 /******************** Generic matching subroutines ************************/
120 /* This function scans the current statement counting the opened and closed
121 parenthesis to make sure they are balanced. */
123 match
124 gfc_match_parens (void)
126 locus old_loc, where;
127 int count;
128 gfc_instring instring;
129 gfc_char_t c, quote;
131 old_loc = gfc_current_locus;
132 count = 0;
133 instring = NONSTRING;
134 quote = ' ';
136 for (;;)
138 c = gfc_next_char_literal (instring);
139 if (c == '\n')
140 break;
141 if (quote == ' ' && ((c == '\'') || (c == '"')))
143 quote = c;
144 instring = INSTRING_WARN;
145 continue;
147 if (quote != ' ' && c == quote)
149 quote = ' ';
150 instring = NONSTRING;
151 continue;
154 if (c == '(' && quote == ' ')
156 count++;
157 where = gfc_current_locus;
159 if (c == ')' && quote == ' ')
161 count--;
162 where = gfc_current_locus;
166 gfc_current_locus = old_loc;
168 if (count > 0)
170 gfc_error ("Missing %<)%> in statement at or before %L", &where);
171 return MATCH_ERROR;
173 if (count < 0)
175 gfc_error ("Missing %<(%> in statement at or before %L", &where);
176 return MATCH_ERROR;
179 return MATCH_YES;
183 /* See if the next character is a special character that has
184 escaped by a \ via the -fbackslash option. */
186 match
187 gfc_match_special_char (gfc_char_t *res)
189 int len, i;
190 gfc_char_t c, n;
191 match m;
193 m = MATCH_YES;
195 switch ((c = gfc_next_char_literal (INSTRING_WARN)))
197 case 'a':
198 *res = '\a';
199 break;
200 case 'b':
201 *res = '\b';
202 break;
203 case 't':
204 *res = '\t';
205 break;
206 case 'f':
207 *res = '\f';
208 break;
209 case 'n':
210 *res = '\n';
211 break;
212 case 'r':
213 *res = '\r';
214 break;
215 case 'v':
216 *res = '\v';
217 break;
218 case '\\':
219 *res = '\\';
220 break;
221 case '0':
222 *res = '\0';
223 break;
225 case 'x':
226 case 'u':
227 case 'U':
228 /* Hexadecimal form of wide characters. */
229 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
230 n = 0;
231 for (i = 0; i < len; i++)
233 char buf[2] = { '\0', '\0' };
235 c = gfc_next_char_literal (INSTRING_WARN);
236 if (!gfc_wide_fits_in_byte (c)
237 || !gfc_check_digit ((unsigned char) c, 16))
238 return MATCH_NO;
240 buf[0] = (unsigned char) c;
241 n = n << 4;
242 n += strtol (buf, NULL, 16);
244 *res = n;
245 break;
247 default:
248 /* Unknown backslash codes are simply not expanded. */
249 m = MATCH_NO;
250 break;
253 return m;
257 /* In free form, match at least one space. Always matches in fixed
258 form. */
260 match
261 gfc_match_space (void)
263 locus old_loc;
264 char c;
266 if (gfc_current_form == FORM_FIXED)
267 return MATCH_YES;
269 old_loc = gfc_current_locus;
271 c = gfc_next_ascii_char ();
272 if (!gfc_is_whitespace (c))
274 gfc_current_locus = old_loc;
275 return MATCH_NO;
278 gfc_gobble_whitespace ();
280 return MATCH_YES;
284 /* Match an end of statement. End of statement is optional
285 whitespace, followed by a ';' or '\n' or comment '!'. If a
286 semicolon is found, we continue to eat whitespace and semicolons. */
288 match
289 gfc_match_eos (void)
291 locus old_loc;
292 int flag;
293 char c;
295 flag = 0;
297 for (;;)
299 old_loc = gfc_current_locus;
300 gfc_gobble_whitespace ();
302 c = gfc_next_ascii_char ();
303 switch (c)
305 case '!':
308 c = gfc_next_ascii_char ();
310 while (c != '\n');
312 /* Fall through. */
314 case '\n':
315 return MATCH_YES;
317 case ';':
318 flag = 1;
319 continue;
322 break;
325 gfc_current_locus = old_loc;
326 return (flag) ? MATCH_YES : MATCH_NO;
330 /* Match a literal integer on the input, setting the value on
331 MATCH_YES. Literal ints occur in kind-parameters as well as
332 old-style character length specifications. If cnt is non-NULL it
333 will be set to the number of digits. */
335 match
336 gfc_match_small_literal_int (int *value, int *cnt)
338 locus old_loc;
339 char c;
340 int i, j;
342 old_loc = gfc_current_locus;
344 *value = -1;
345 gfc_gobble_whitespace ();
346 c = gfc_next_ascii_char ();
347 if (cnt)
348 *cnt = 0;
350 if (!ISDIGIT (c))
352 gfc_current_locus = old_loc;
353 return MATCH_NO;
356 i = c - '0';
357 j = 1;
359 for (;;)
361 old_loc = gfc_current_locus;
362 c = gfc_next_ascii_char ();
364 if (!ISDIGIT (c))
365 break;
367 i = 10 * i + c - '0';
368 j++;
370 if (i > 99999999)
372 gfc_error ("Integer too large at %C");
373 return MATCH_ERROR;
377 gfc_current_locus = old_loc;
379 *value = i;
380 if (cnt)
381 *cnt = j;
382 return MATCH_YES;
386 /* Match a small, constant integer expression, like in a kind
387 statement. On MATCH_YES, 'value' is set. */
389 match
390 gfc_match_small_int (int *value)
392 gfc_expr *expr;
393 const char *p;
394 match m;
395 int i;
397 m = gfc_match_expr (&expr);
398 if (m != MATCH_YES)
399 return m;
401 p = gfc_extract_int (expr, &i);
402 gfc_free_expr (expr);
404 if (p != NULL)
406 gfc_error (p);
407 m = MATCH_ERROR;
410 *value = i;
411 return m;
415 /* This function is the same as the gfc_match_small_int, except that
416 we're keeping the pointer to the expr. This function could just be
417 removed and the previously mentioned one modified, though all calls
418 to it would have to be modified then (and there were a number of
419 them). Return MATCH_ERROR if fail to extract the int; otherwise,
420 return the result of gfc_match_expr(). The expr (if any) that was
421 matched is returned in the parameter expr. */
423 match
424 gfc_match_small_int_expr (int *value, gfc_expr **expr)
426 const char *p;
427 match m;
428 int i;
430 m = gfc_match_expr (expr);
431 if (m != MATCH_YES)
432 return m;
434 p = gfc_extract_int (*expr, &i);
436 if (p != NULL)
438 gfc_error (p);
439 m = MATCH_ERROR;
442 *value = i;
443 return m;
447 /* Matches a statement label. Uses gfc_match_small_literal_int() to
448 do most of the work. */
450 match
451 gfc_match_st_label (gfc_st_label **label)
453 locus old_loc;
454 match m;
455 int i, cnt;
457 old_loc = gfc_current_locus;
459 m = gfc_match_small_literal_int (&i, &cnt);
460 if (m != MATCH_YES)
461 return m;
463 if (cnt > 5)
465 gfc_error ("Too many digits in statement label at %C");
466 goto cleanup;
469 if (i == 0)
471 gfc_error ("Statement label at %C is zero");
472 goto cleanup;
475 *label = gfc_get_st_label (i);
476 return MATCH_YES;
478 cleanup:
480 gfc_current_locus = old_loc;
481 return MATCH_ERROR;
485 /* Match and validate a label associated with a named IF, DO or SELECT
486 statement. If the symbol does not have the label attribute, we add
487 it. We also make sure the symbol does not refer to another
488 (active) block. A matched label is pointed to by gfc_new_block. */
490 match
491 gfc_match_label (void)
493 char name[GFC_MAX_SYMBOL_LEN + 1];
494 match m;
496 gfc_new_block = NULL;
498 m = gfc_match (" %n :", name);
499 if (m != MATCH_YES)
500 return m;
502 if (gfc_get_symbol (name, NULL, &gfc_new_block))
504 gfc_error ("Label name %qs at %C is ambiguous", name);
505 return MATCH_ERROR;
508 if (gfc_new_block->attr.flavor == FL_LABEL)
510 gfc_error ("Duplicate construct label %qs at %C", name);
511 return MATCH_ERROR;
514 if (!gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
515 gfc_new_block->name, NULL))
516 return MATCH_ERROR;
518 return MATCH_YES;
522 /* See if the current input looks like a name of some sort. Modifies
523 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
524 Note that options.c restricts max_identifier_length to not more
525 than GFC_MAX_SYMBOL_LEN. */
527 match
528 gfc_match_name (char *buffer)
530 locus old_loc;
531 int i;
532 char c;
534 old_loc = gfc_current_locus;
535 gfc_gobble_whitespace ();
537 c = gfc_next_ascii_char ();
538 if (!(ISALPHA (c) || (c == '_' && flag_allow_leading_underscore)))
540 /* Special cases for unary minus and plus, which allows for a sensible
541 error message for code of the form 'c = exp(-a*b) )' where an
542 extra ')' appears at the end of statement. */
543 if (!gfc_error_flag_test () && c != '(' && c != '-' && c != '+')
544 gfc_error ("Invalid character in name at %C");
545 gfc_current_locus = old_loc;
546 return MATCH_NO;
549 i = 0;
553 buffer[i++] = c;
555 if (i > gfc_option.max_identifier_length)
557 gfc_error ("Name at %C is too long");
558 return MATCH_ERROR;
561 old_loc = gfc_current_locus;
562 c = gfc_next_ascii_char ();
564 while (ISALNUM (c) || c == '_' || (flag_dollar_ok && c == '$'));
566 if (c == '$' && !flag_dollar_ok)
568 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
569 "allow it as an extension", &old_loc);
570 return MATCH_ERROR;
573 buffer[i] = '\0';
574 gfc_current_locus = old_loc;
576 return MATCH_YES;
580 /* Match a symbol on the input. Modifies the pointer to the symbol
581 pointer if successful. */
583 match
584 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
586 char buffer[GFC_MAX_SYMBOL_LEN + 1];
587 match m;
589 m = gfc_match_name (buffer);
590 if (m != MATCH_YES)
591 return m;
593 if (host_assoc)
594 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
595 ? MATCH_ERROR : MATCH_YES;
597 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
598 return MATCH_ERROR;
600 return MATCH_YES;
604 match
605 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
607 gfc_symtree *st;
608 match m;
610 m = gfc_match_sym_tree (&st, host_assoc);
612 if (m == MATCH_YES)
614 if (st)
615 *matched_symbol = st->n.sym;
616 else
617 *matched_symbol = NULL;
619 else
620 *matched_symbol = NULL;
621 return m;
625 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
626 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
627 in matchexp.c. */
629 match
630 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
632 locus orig_loc = gfc_current_locus;
633 char ch;
635 gfc_gobble_whitespace ();
636 ch = gfc_next_ascii_char ();
637 switch (ch)
639 case '+':
640 /* Matched "+". */
641 *result = INTRINSIC_PLUS;
642 return MATCH_YES;
644 case '-':
645 /* Matched "-". */
646 *result = INTRINSIC_MINUS;
647 return MATCH_YES;
649 case '=':
650 if (gfc_next_ascii_char () == '=')
652 /* Matched "==". */
653 *result = INTRINSIC_EQ;
654 return MATCH_YES;
656 break;
658 case '<':
659 if (gfc_peek_ascii_char () == '=')
661 /* Matched "<=". */
662 gfc_next_ascii_char ();
663 *result = INTRINSIC_LE;
664 return MATCH_YES;
666 /* Matched "<". */
667 *result = INTRINSIC_LT;
668 return MATCH_YES;
670 case '>':
671 if (gfc_peek_ascii_char () == '=')
673 /* Matched ">=". */
674 gfc_next_ascii_char ();
675 *result = INTRINSIC_GE;
676 return MATCH_YES;
678 /* Matched ">". */
679 *result = INTRINSIC_GT;
680 return MATCH_YES;
682 case '*':
683 if (gfc_peek_ascii_char () == '*')
685 /* Matched "**". */
686 gfc_next_ascii_char ();
687 *result = INTRINSIC_POWER;
688 return MATCH_YES;
690 /* Matched "*". */
691 *result = INTRINSIC_TIMES;
692 return MATCH_YES;
694 case '/':
695 ch = gfc_peek_ascii_char ();
696 if (ch == '=')
698 /* Matched "/=". */
699 gfc_next_ascii_char ();
700 *result = INTRINSIC_NE;
701 return MATCH_YES;
703 else if (ch == '/')
705 /* Matched "//". */
706 gfc_next_ascii_char ();
707 *result = INTRINSIC_CONCAT;
708 return MATCH_YES;
710 /* Matched "/". */
711 *result = INTRINSIC_DIVIDE;
712 return MATCH_YES;
714 case '.':
715 ch = gfc_next_ascii_char ();
716 switch (ch)
718 case 'a':
719 if (gfc_next_ascii_char () == 'n'
720 && gfc_next_ascii_char () == 'd'
721 && gfc_next_ascii_char () == '.')
723 /* Matched ".and.". */
724 *result = INTRINSIC_AND;
725 return MATCH_YES;
727 break;
729 case 'e':
730 if (gfc_next_ascii_char () == 'q')
732 ch = gfc_next_ascii_char ();
733 if (ch == '.')
735 /* Matched ".eq.". */
736 *result = INTRINSIC_EQ_OS;
737 return MATCH_YES;
739 else if (ch == 'v')
741 if (gfc_next_ascii_char () == '.')
743 /* Matched ".eqv.". */
744 *result = INTRINSIC_EQV;
745 return MATCH_YES;
749 break;
751 case 'g':
752 ch = gfc_next_ascii_char ();
753 if (ch == 'e')
755 if (gfc_next_ascii_char () == '.')
757 /* Matched ".ge.". */
758 *result = INTRINSIC_GE_OS;
759 return MATCH_YES;
762 else if (ch == 't')
764 if (gfc_next_ascii_char () == '.')
766 /* Matched ".gt.". */
767 *result = INTRINSIC_GT_OS;
768 return MATCH_YES;
771 break;
773 case 'l':
774 ch = gfc_next_ascii_char ();
775 if (ch == 'e')
777 if (gfc_next_ascii_char () == '.')
779 /* Matched ".le.". */
780 *result = INTRINSIC_LE_OS;
781 return MATCH_YES;
784 else if (ch == 't')
786 if (gfc_next_ascii_char () == '.')
788 /* Matched ".lt.". */
789 *result = INTRINSIC_LT_OS;
790 return MATCH_YES;
793 break;
795 case 'n':
796 ch = gfc_next_ascii_char ();
797 if (ch == 'e')
799 ch = gfc_next_ascii_char ();
800 if (ch == '.')
802 /* Matched ".ne.". */
803 *result = INTRINSIC_NE_OS;
804 return MATCH_YES;
806 else if (ch == 'q')
808 if (gfc_next_ascii_char () == 'v'
809 && gfc_next_ascii_char () == '.')
811 /* Matched ".neqv.". */
812 *result = INTRINSIC_NEQV;
813 return MATCH_YES;
817 else if (ch == 'o')
819 if (gfc_next_ascii_char () == 't'
820 && gfc_next_ascii_char () == '.')
822 /* Matched ".not.". */
823 *result = INTRINSIC_NOT;
824 return MATCH_YES;
827 break;
829 case 'o':
830 if (gfc_next_ascii_char () == 'r'
831 && gfc_next_ascii_char () == '.')
833 /* Matched ".or.". */
834 *result = INTRINSIC_OR;
835 return MATCH_YES;
837 break;
839 default:
840 break;
842 break;
844 default:
845 break;
848 gfc_current_locus = orig_loc;
849 return MATCH_NO;
853 /* Match a loop control phrase:
855 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
857 If the final integer expression is not present, a constant unity
858 expression is returned. We don't return MATCH_ERROR until after
859 the equals sign is seen. */
861 match
862 gfc_match_iterator (gfc_iterator *iter, int init_flag)
864 char name[GFC_MAX_SYMBOL_LEN + 1];
865 gfc_expr *var, *e1, *e2, *e3;
866 locus start;
867 match m;
869 e1 = e2 = e3 = NULL;
871 /* Match the start of an iterator without affecting the symbol table. */
873 start = gfc_current_locus;
874 m = gfc_match (" %n =", name);
875 gfc_current_locus = start;
877 if (m != MATCH_YES)
878 return MATCH_NO;
880 m = gfc_match_variable (&var, 0);
881 if (m != MATCH_YES)
882 return MATCH_NO;
884 /* F2008, C617 & C565. */
885 if (var->symtree->n.sym->attr.codimension)
887 gfc_error ("Loop variable at %C cannot be a coarray");
888 goto cleanup;
891 if (var->ref != NULL)
893 gfc_error ("Loop variable at %C cannot be a sub-component");
894 goto cleanup;
897 gfc_match_char ('=');
899 var->symtree->n.sym->attr.implied_index = 1;
901 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
902 if (m == MATCH_NO)
903 goto syntax;
904 if (m == MATCH_ERROR)
905 goto cleanup;
907 if (gfc_match_char (',') != MATCH_YES)
908 goto syntax;
910 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
911 if (m == MATCH_NO)
912 goto syntax;
913 if (m == MATCH_ERROR)
914 goto cleanup;
916 if (gfc_match_char (',') != MATCH_YES)
918 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
919 goto done;
922 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
923 if (m == MATCH_ERROR)
924 goto cleanup;
925 if (m == MATCH_NO)
927 gfc_error ("Expected a step value in iterator at %C");
928 goto cleanup;
931 done:
932 iter->var = var;
933 iter->start = e1;
934 iter->end = e2;
935 iter->step = e3;
936 return MATCH_YES;
938 syntax:
939 gfc_error ("Syntax error in iterator at %C");
941 cleanup:
942 gfc_free_expr (e1);
943 gfc_free_expr (e2);
944 gfc_free_expr (e3);
946 return MATCH_ERROR;
950 /* Tries to match the next non-whitespace character on the input.
951 This subroutine does not return MATCH_ERROR. */
953 match
954 gfc_match_char (char c)
956 locus where;
958 where = gfc_current_locus;
959 gfc_gobble_whitespace ();
961 if (gfc_next_ascii_char () == c)
962 return MATCH_YES;
964 gfc_current_locus = where;
965 return MATCH_NO;
969 /* General purpose matching subroutine. The target string is a
970 scanf-like format string in which spaces correspond to arbitrary
971 whitespace (including no whitespace), characters correspond to
972 themselves. The %-codes are:
974 %% Literal percent sign
975 %e Expression, pointer to a pointer is set
976 %s Symbol, pointer to the symbol is set
977 %n Name, character buffer is set to name
978 %t Matches end of statement.
979 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
980 %l Matches a statement label
981 %v Matches a variable expression (an lvalue)
982 % Matches a required space (in free form) and optional spaces. */
984 match
985 gfc_match (const char *target, ...)
987 gfc_st_label **label;
988 int matches, *ip;
989 locus old_loc;
990 va_list argp;
991 char c, *np;
992 match m, n;
993 void **vp;
994 const char *p;
996 old_loc = gfc_current_locus;
997 va_start (argp, target);
998 m = MATCH_NO;
999 matches = 0;
1000 p = target;
1002 loop:
1003 c = *p++;
1004 switch (c)
1006 case ' ':
1007 gfc_gobble_whitespace ();
1008 goto loop;
1009 case '\0':
1010 m = MATCH_YES;
1011 break;
1013 case '%':
1014 c = *p++;
1015 switch (c)
1017 case 'e':
1018 vp = va_arg (argp, void **);
1019 n = gfc_match_expr ((gfc_expr **) vp);
1020 if (n != MATCH_YES)
1022 m = n;
1023 goto not_yes;
1026 matches++;
1027 goto loop;
1029 case 'v':
1030 vp = va_arg (argp, void **);
1031 n = gfc_match_variable ((gfc_expr **) vp, 0);
1032 if (n != MATCH_YES)
1034 m = n;
1035 goto not_yes;
1038 matches++;
1039 goto loop;
1041 case 's':
1042 vp = va_arg (argp, void **);
1043 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1044 if (n != MATCH_YES)
1046 m = n;
1047 goto not_yes;
1050 matches++;
1051 goto loop;
1053 case 'n':
1054 np = va_arg (argp, char *);
1055 n = gfc_match_name (np);
1056 if (n != MATCH_YES)
1058 m = n;
1059 goto not_yes;
1062 matches++;
1063 goto loop;
1065 case 'l':
1066 label = va_arg (argp, gfc_st_label **);
1067 n = gfc_match_st_label (label);
1068 if (n != MATCH_YES)
1070 m = n;
1071 goto not_yes;
1074 matches++;
1075 goto loop;
1077 case 'o':
1078 ip = va_arg (argp, int *);
1079 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1080 if (n != MATCH_YES)
1082 m = n;
1083 goto not_yes;
1086 matches++;
1087 goto loop;
1089 case 't':
1090 if (gfc_match_eos () != MATCH_YES)
1092 m = MATCH_NO;
1093 goto not_yes;
1095 goto loop;
1097 case ' ':
1098 if (gfc_match_space () == MATCH_YES)
1099 goto loop;
1100 m = MATCH_NO;
1101 goto not_yes;
1103 case '%':
1104 break; /* Fall through to character matcher. */
1106 default:
1107 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1110 default:
1112 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1113 expect an upper case character here! */
1114 gcc_assert (TOLOWER (c) == c);
1116 if (c == gfc_next_ascii_char ())
1117 goto loop;
1118 break;
1121 not_yes:
1122 va_end (argp);
1124 if (m != MATCH_YES)
1126 /* Clean up after a failed match. */
1127 gfc_current_locus = old_loc;
1128 va_start (argp, target);
1130 p = target;
1131 for (; matches > 0; matches--)
1133 while (*p++ != '%');
1135 switch (*p++)
1137 case '%':
1138 matches++;
1139 break; /* Skip. */
1141 /* Matches that don't have to be undone */
1142 case 'o':
1143 case 'l':
1144 case 'n':
1145 case 's':
1146 (void) va_arg (argp, void **);
1147 break;
1149 case 'e':
1150 case 'v':
1151 vp = va_arg (argp, void **);
1152 gfc_free_expr ((struct gfc_expr *)*vp);
1153 *vp = NULL;
1154 break;
1158 va_end (argp);
1161 return m;
1165 /*********************** Statement level matching **********************/
1167 /* Matches the start of a program unit, which is the program keyword
1168 followed by an obligatory symbol. */
1170 match
1171 gfc_match_program (void)
1173 gfc_symbol *sym;
1174 match m;
1176 m = gfc_match ("% %s%t", &sym);
1178 if (m == MATCH_NO)
1180 gfc_error ("Invalid form of PROGRAM statement at %C");
1181 m = MATCH_ERROR;
1184 if (m == MATCH_ERROR)
1185 return m;
1187 if (!gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL))
1188 return MATCH_ERROR;
1190 gfc_new_block = sym;
1192 return MATCH_YES;
1196 /* Match a simple assignment statement. */
1198 match
1199 gfc_match_assignment (void)
1201 gfc_expr *lvalue, *rvalue;
1202 locus old_loc;
1203 match m;
1205 old_loc = gfc_current_locus;
1207 lvalue = NULL;
1208 m = gfc_match (" %v =", &lvalue);
1209 if (m != MATCH_YES)
1211 gfc_current_locus = old_loc;
1212 gfc_free_expr (lvalue);
1213 return MATCH_NO;
1216 rvalue = NULL;
1217 m = gfc_match (" %e%t", &rvalue);
1218 if (m != MATCH_YES)
1220 gfc_current_locus = old_loc;
1221 gfc_free_expr (lvalue);
1222 gfc_free_expr (rvalue);
1223 return m;
1226 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1228 new_st.op = EXEC_ASSIGN;
1229 new_st.expr1 = lvalue;
1230 new_st.expr2 = rvalue;
1232 gfc_check_do_variable (lvalue->symtree);
1234 return MATCH_YES;
1238 /* Match a pointer assignment statement. */
1240 match
1241 gfc_match_pointer_assignment (void)
1243 gfc_expr *lvalue, *rvalue;
1244 locus old_loc;
1245 match m;
1247 old_loc = gfc_current_locus;
1249 lvalue = rvalue = NULL;
1250 gfc_matching_ptr_assignment = 0;
1251 gfc_matching_procptr_assignment = 0;
1253 m = gfc_match (" %v =>", &lvalue);
1254 if (m != MATCH_YES)
1256 m = MATCH_NO;
1257 goto cleanup;
1260 if (lvalue->symtree->n.sym->attr.proc_pointer
1261 || gfc_is_proc_ptr_comp (lvalue))
1262 gfc_matching_procptr_assignment = 1;
1263 else
1264 gfc_matching_ptr_assignment = 1;
1266 m = gfc_match (" %e%t", &rvalue);
1267 gfc_matching_ptr_assignment = 0;
1268 gfc_matching_procptr_assignment = 0;
1269 if (m != MATCH_YES)
1270 goto cleanup;
1272 new_st.op = EXEC_POINTER_ASSIGN;
1273 new_st.expr1 = lvalue;
1274 new_st.expr2 = rvalue;
1276 return MATCH_YES;
1278 cleanup:
1279 gfc_current_locus = old_loc;
1280 gfc_free_expr (lvalue);
1281 gfc_free_expr (rvalue);
1282 return m;
1286 /* We try to match an easy arithmetic IF statement. This only happens
1287 when just after having encountered a simple IF statement. This code
1288 is really duplicate with parts of the gfc_match_if code, but this is
1289 *much* easier. */
1291 static match
1292 match_arithmetic_if (void)
1294 gfc_st_label *l1, *l2, *l3;
1295 gfc_expr *expr;
1296 match m;
1298 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1299 if (m != MATCH_YES)
1300 return m;
1302 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1303 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1304 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1306 gfc_free_expr (expr);
1307 return MATCH_ERROR;
1310 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
1311 return MATCH_ERROR;
1313 new_st.op = EXEC_ARITHMETIC_IF;
1314 new_st.expr1 = expr;
1315 new_st.label1 = l1;
1316 new_st.label2 = l2;
1317 new_st.label3 = l3;
1319 return MATCH_YES;
1323 /* The IF statement is a bit of a pain. First of all, there are three
1324 forms of it, the simple IF, the IF that starts a block and the
1325 arithmetic IF.
1327 There is a problem with the simple IF and that is the fact that we
1328 only have a single level of undo information on symbols. What this
1329 means is for a simple IF, we must re-match the whole IF statement
1330 multiple times in order to guarantee that the symbol table ends up
1331 in the proper state. */
1333 static match match_simple_forall (void);
1334 static match match_simple_where (void);
1336 match
1337 gfc_match_if (gfc_statement *if_type)
1339 gfc_expr *expr;
1340 gfc_st_label *l1, *l2, *l3;
1341 locus old_loc, old_loc2;
1342 gfc_code *p;
1343 match m, n;
1345 n = gfc_match_label ();
1346 if (n == MATCH_ERROR)
1347 return n;
1349 old_loc = gfc_current_locus;
1351 m = gfc_match (" if ( %e", &expr);
1352 if (m != MATCH_YES)
1353 return m;
1355 old_loc2 = gfc_current_locus;
1356 gfc_current_locus = old_loc;
1358 if (gfc_match_parens () == MATCH_ERROR)
1359 return MATCH_ERROR;
1361 gfc_current_locus = old_loc2;
1363 if (gfc_match_char (')') != MATCH_YES)
1365 gfc_error ("Syntax error in IF-expression at %C");
1366 gfc_free_expr (expr);
1367 return MATCH_ERROR;
1370 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1372 if (m == MATCH_YES)
1374 if (n == MATCH_YES)
1376 gfc_error ("Block label not appropriate for arithmetic IF "
1377 "statement at %C");
1378 gfc_free_expr (expr);
1379 return MATCH_ERROR;
1382 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1383 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1384 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1386 gfc_free_expr (expr);
1387 return MATCH_ERROR;
1390 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
1391 return MATCH_ERROR;
1393 new_st.op = EXEC_ARITHMETIC_IF;
1394 new_st.expr1 = expr;
1395 new_st.label1 = l1;
1396 new_st.label2 = l2;
1397 new_st.label3 = l3;
1399 *if_type = ST_ARITHMETIC_IF;
1400 return MATCH_YES;
1403 if (gfc_match (" then%t") == MATCH_YES)
1405 new_st.op = EXEC_IF;
1406 new_st.expr1 = expr;
1407 *if_type = ST_IF_BLOCK;
1408 return MATCH_YES;
1411 if (n == MATCH_YES)
1413 gfc_error ("Block label is not appropriate for IF statement at %C");
1414 gfc_free_expr (expr);
1415 return MATCH_ERROR;
1418 /* At this point the only thing left is a simple IF statement. At
1419 this point, n has to be MATCH_NO, so we don't have to worry about
1420 re-matching a block label. From what we've got so far, try
1421 matching an assignment. */
1423 *if_type = ST_SIMPLE_IF;
1425 m = gfc_match_assignment ();
1426 if (m == MATCH_YES)
1427 goto got_match;
1429 gfc_free_expr (expr);
1430 gfc_undo_symbols ();
1431 gfc_current_locus = old_loc;
1433 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1434 assignment was found. For MATCH_NO, continue to call the various
1435 matchers. */
1436 if (m == MATCH_ERROR)
1437 return MATCH_ERROR;
1439 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1441 m = gfc_match_pointer_assignment ();
1442 if (m == MATCH_YES)
1443 goto got_match;
1445 gfc_free_expr (expr);
1446 gfc_undo_symbols ();
1447 gfc_current_locus = old_loc;
1449 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1451 /* Look at the next keyword to see which matcher to call. Matching
1452 the keyword doesn't affect the symbol table, so we don't have to
1453 restore between tries. */
1455 #define match(string, subr, statement) \
1456 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1458 gfc_clear_error ();
1460 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1461 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1462 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1463 match ("call", gfc_match_call, ST_CALL)
1464 match ("close", gfc_match_close, ST_CLOSE)
1465 match ("continue", gfc_match_continue, ST_CONTINUE)
1466 match ("cycle", gfc_match_cycle, ST_CYCLE)
1467 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1468 match ("end file", gfc_match_endfile, ST_END_FILE)
1469 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1470 match ("exit", gfc_match_exit, ST_EXIT)
1471 match ("flush", gfc_match_flush, ST_FLUSH)
1472 match ("forall", match_simple_forall, ST_FORALL)
1473 match ("go to", gfc_match_goto, ST_GOTO)
1474 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1475 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1476 match ("lock", gfc_match_lock, ST_LOCK)
1477 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1478 match ("open", gfc_match_open, ST_OPEN)
1479 match ("pause", gfc_match_pause, ST_NONE)
1480 match ("print", gfc_match_print, ST_WRITE)
1481 match ("read", gfc_match_read, ST_READ)
1482 match ("return", gfc_match_return, ST_RETURN)
1483 match ("rewind", gfc_match_rewind, ST_REWIND)
1484 match ("stop", gfc_match_stop, ST_STOP)
1485 match ("wait", gfc_match_wait, ST_WAIT)
1486 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1487 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1488 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1489 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1490 match ("where", match_simple_where, ST_WHERE)
1491 match ("write", gfc_match_write, ST_WRITE)
1493 /* The gfc_match_assignment() above may have returned a MATCH_NO
1494 where the assignment was to a named constant. Check that
1495 special case here. */
1496 m = gfc_match_assignment ();
1497 if (m == MATCH_NO)
1499 gfc_error ("Cannot assign to a named constant at %C");
1500 gfc_free_expr (expr);
1501 gfc_undo_symbols ();
1502 gfc_current_locus = old_loc;
1503 return MATCH_ERROR;
1506 /* All else has failed, so give up. See if any of the matchers has
1507 stored an error message of some sort. */
1508 if (!gfc_error_check ())
1509 gfc_error ("Unclassifiable statement in IF-clause at %C");
1511 gfc_free_expr (expr);
1512 return MATCH_ERROR;
1514 got_match:
1515 if (m == MATCH_NO)
1516 gfc_error ("Syntax error in IF-clause at %C");
1517 if (m != MATCH_YES)
1519 gfc_free_expr (expr);
1520 return MATCH_ERROR;
1523 /* At this point, we've matched the single IF and the action clause
1524 is in new_st. Rearrange things so that the IF statement appears
1525 in new_st. */
1527 p = gfc_get_code (EXEC_IF);
1528 p->next = XCNEW (gfc_code);
1529 *p->next = new_st;
1530 p->next->loc = gfc_current_locus;
1532 p->expr1 = expr;
1534 gfc_clear_new_st ();
1536 new_st.op = EXEC_IF;
1537 new_st.block = p;
1539 return MATCH_YES;
1542 #undef match
1545 /* Match an ELSE statement. */
1547 match
1548 gfc_match_else (void)
1550 char name[GFC_MAX_SYMBOL_LEN + 1];
1552 if (gfc_match_eos () == MATCH_YES)
1553 return MATCH_YES;
1555 if (gfc_match_name (name) != MATCH_YES
1556 || gfc_current_block () == NULL
1557 || gfc_match_eos () != MATCH_YES)
1559 gfc_error ("Unexpected junk after ELSE statement at %C");
1560 return MATCH_ERROR;
1563 if (strcmp (name, gfc_current_block ()->name) != 0)
1565 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1566 name, gfc_current_block ()->name);
1567 return MATCH_ERROR;
1570 return MATCH_YES;
1574 /* Match an ELSE IF statement. */
1576 match
1577 gfc_match_elseif (void)
1579 char name[GFC_MAX_SYMBOL_LEN + 1];
1580 gfc_expr *expr;
1581 match m;
1583 m = gfc_match (" ( %e ) then", &expr);
1584 if (m != MATCH_YES)
1585 return m;
1587 if (gfc_match_eos () == MATCH_YES)
1588 goto done;
1590 if (gfc_match_name (name) != MATCH_YES
1591 || gfc_current_block () == NULL
1592 || gfc_match_eos () != MATCH_YES)
1594 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1595 goto cleanup;
1598 if (strcmp (name, gfc_current_block ()->name) != 0)
1600 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1601 name, gfc_current_block ()->name);
1602 goto cleanup;
1605 done:
1606 new_st.op = EXEC_IF;
1607 new_st.expr1 = expr;
1608 return MATCH_YES;
1610 cleanup:
1611 gfc_free_expr (expr);
1612 return MATCH_ERROR;
1616 /* Free a gfc_iterator structure. */
1618 void
1619 gfc_free_iterator (gfc_iterator *iter, int flag)
1622 if (iter == NULL)
1623 return;
1625 gfc_free_expr (iter->var);
1626 gfc_free_expr (iter->start);
1627 gfc_free_expr (iter->end);
1628 gfc_free_expr (iter->step);
1630 if (flag)
1631 free (iter);
1635 /* Match a CRITICAL statement. */
1636 match
1637 gfc_match_critical (void)
1639 gfc_st_label *label = NULL;
1641 if (gfc_match_label () == MATCH_ERROR)
1642 return MATCH_ERROR;
1644 if (gfc_match (" critical") != MATCH_YES)
1645 return MATCH_NO;
1647 if (gfc_match_st_label (&label) == MATCH_ERROR)
1648 return MATCH_ERROR;
1650 if (gfc_match_eos () != MATCH_YES)
1652 gfc_syntax_error (ST_CRITICAL);
1653 return MATCH_ERROR;
1656 if (gfc_pure (NULL))
1658 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1659 return MATCH_ERROR;
1662 if (gfc_find_state (COMP_DO_CONCURRENT))
1664 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1665 "block");
1666 return MATCH_ERROR;
1669 gfc_unset_implicit_pure (NULL);
1671 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
1672 return MATCH_ERROR;
1674 if (flag_coarray == GFC_FCOARRAY_NONE)
1676 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1677 "enable");
1678 return MATCH_ERROR;
1681 if (gfc_find_state (COMP_CRITICAL))
1683 gfc_error ("Nested CRITICAL block at %C");
1684 return MATCH_ERROR;
1687 new_st.op = EXEC_CRITICAL;
1689 if (label != NULL
1690 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
1691 return MATCH_ERROR;
1693 return MATCH_YES;
1697 /* Match a BLOCK statement. */
1699 match
1700 gfc_match_block (void)
1702 match m;
1704 if (gfc_match_label () == MATCH_ERROR)
1705 return MATCH_ERROR;
1707 if (gfc_match (" block") != MATCH_YES)
1708 return MATCH_NO;
1710 /* For this to be a correct BLOCK statement, the line must end now. */
1711 m = gfc_match_eos ();
1712 if (m == MATCH_ERROR)
1713 return MATCH_ERROR;
1714 if (m == MATCH_NO)
1715 return MATCH_NO;
1717 return MATCH_YES;
1721 /* Match an ASSOCIATE statement. */
1723 match
1724 gfc_match_associate (void)
1726 if (gfc_match_label () == MATCH_ERROR)
1727 return MATCH_ERROR;
1729 if (gfc_match (" associate") != MATCH_YES)
1730 return MATCH_NO;
1732 /* Match the association list. */
1733 if (gfc_match_char ('(') != MATCH_YES)
1735 gfc_error ("Expected association list at %C");
1736 return MATCH_ERROR;
1738 new_st.ext.block.assoc = NULL;
1739 while (true)
1741 gfc_association_list* newAssoc = gfc_get_association_list ();
1742 gfc_association_list* a;
1744 /* Match the next association. */
1745 if (gfc_match (" %n => %e", newAssoc->name, &newAssoc->target)
1746 != MATCH_YES)
1748 gfc_error ("Expected association at %C");
1749 goto assocListError;
1751 newAssoc->where = gfc_current_locus;
1753 /* Check that the current name is not yet in the list. */
1754 for (a = new_st.ext.block.assoc; a; a = a->next)
1755 if (!strcmp (a->name, newAssoc->name))
1757 gfc_error ("Duplicate name %qs in association at %C",
1758 newAssoc->name);
1759 goto assocListError;
1762 /* The target expression must not be coindexed. */
1763 if (gfc_is_coindexed (newAssoc->target))
1765 gfc_error ("Association target at %C must not be coindexed");
1766 goto assocListError;
1769 /* The `variable' field is left blank for now; because the target is not
1770 yet resolved, we can't use gfc_has_vector_subscript to determine it
1771 for now. This is set during resolution. */
1773 /* Put it into the list. */
1774 newAssoc->next = new_st.ext.block.assoc;
1775 new_st.ext.block.assoc = newAssoc;
1777 /* Try next one or end if closing parenthesis is found. */
1778 gfc_gobble_whitespace ();
1779 if (gfc_peek_char () == ')')
1780 break;
1781 if (gfc_match_char (',') != MATCH_YES)
1783 gfc_error ("Expected %<)%> or %<,%> at %C");
1784 return MATCH_ERROR;
1787 continue;
1789 assocListError:
1790 free (newAssoc);
1791 goto error;
1793 if (gfc_match_char (')') != MATCH_YES)
1795 /* This should never happen as we peek above. */
1796 gcc_unreachable ();
1799 if (gfc_match_eos () != MATCH_YES)
1801 gfc_error ("Junk after ASSOCIATE statement at %C");
1802 goto error;
1805 return MATCH_YES;
1807 error:
1808 gfc_free_association_list (new_st.ext.block.assoc);
1809 return MATCH_ERROR;
1813 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1814 an accessible derived type. */
1816 static match
1817 match_derived_type_spec (gfc_typespec *ts)
1819 char name[GFC_MAX_SYMBOL_LEN + 1];
1820 locus old_locus;
1821 gfc_symbol *derived;
1823 old_locus = gfc_current_locus;
1825 if (gfc_match ("%n", name) != MATCH_YES)
1827 gfc_current_locus = old_locus;
1828 return MATCH_NO;
1831 gfc_find_symbol (name, NULL, 1, &derived);
1833 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
1834 derived = gfc_find_dt_in_generic (derived);
1836 if (derived && derived->attr.flavor == FL_DERIVED)
1838 ts->type = BT_DERIVED;
1839 ts->u.derived = derived;
1840 return MATCH_YES;
1843 gfc_current_locus = old_locus;
1844 return MATCH_NO;
1848 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
1849 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
1850 It only includes the intrinsic types from the Fortran 2003 standard
1851 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
1852 the implicit_flag is not needed, so it was removed. Derived types are
1853 identified by their name alone. */
1855 match
1856 gfc_match_type_spec (gfc_typespec *ts)
1858 match m;
1859 locus old_locus;
1861 gfc_clear_ts (ts);
1862 gfc_gobble_whitespace ();
1863 old_locus = gfc_current_locus;
1865 if (match_derived_type_spec (ts) == MATCH_YES)
1867 /* Enforce F03:C401. */
1868 if (ts->u.derived->attr.abstract)
1870 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
1871 ts->u.derived->name, &old_locus);
1872 return MATCH_ERROR;
1874 return MATCH_YES;
1877 if (gfc_match ("integer") == MATCH_YES)
1879 ts->type = BT_INTEGER;
1880 ts->kind = gfc_default_integer_kind;
1881 goto kind_selector;
1884 if (gfc_match ("real") == MATCH_YES)
1886 ts->type = BT_REAL;
1887 ts->kind = gfc_default_real_kind;
1888 goto kind_selector;
1891 if (gfc_match ("double precision") == MATCH_YES)
1893 ts->type = BT_REAL;
1894 ts->kind = gfc_default_double_kind;
1895 return MATCH_YES;
1898 if (gfc_match ("complex") == MATCH_YES)
1900 ts->type = BT_COMPLEX;
1901 ts->kind = gfc_default_complex_kind;
1902 goto kind_selector;
1905 if (gfc_match ("character") == MATCH_YES)
1907 ts->type = BT_CHARACTER;
1909 m = gfc_match_char_spec (ts);
1911 if (m == MATCH_NO)
1912 m = MATCH_YES;
1914 return m;
1917 if (gfc_match ("logical") == MATCH_YES)
1919 ts->type = BT_LOGICAL;
1920 ts->kind = gfc_default_logical_kind;
1921 goto kind_selector;
1924 /* If a type is not matched, simply return MATCH_NO. */
1925 gfc_current_locus = old_locus;
1926 return MATCH_NO;
1928 kind_selector:
1930 gfc_gobble_whitespace ();
1931 if (gfc_peek_ascii_char () == '*')
1933 gfc_error ("Invalid type-spec at %C");
1934 return MATCH_ERROR;
1937 m = gfc_match_kind_spec (ts, false);
1939 if (m == MATCH_NO)
1940 m = MATCH_YES; /* No kind specifier found. */
1942 return m;
1946 /******************** FORALL subroutines ********************/
1948 /* Free a list of FORALL iterators. */
1950 void
1951 gfc_free_forall_iterator (gfc_forall_iterator *iter)
1953 gfc_forall_iterator *next;
1955 while (iter)
1957 next = iter->next;
1958 gfc_free_expr (iter->var);
1959 gfc_free_expr (iter->start);
1960 gfc_free_expr (iter->end);
1961 gfc_free_expr (iter->stride);
1962 free (iter);
1963 iter = next;
1968 /* Match an iterator as part of a FORALL statement. The format is:
1970 <var> = <start>:<end>[:<stride>]
1972 On MATCH_NO, the caller tests for the possibility that there is a
1973 scalar mask expression. */
1975 static match
1976 match_forall_iterator (gfc_forall_iterator **result)
1978 gfc_forall_iterator *iter;
1979 locus where;
1980 match m;
1982 where = gfc_current_locus;
1983 iter = XCNEW (gfc_forall_iterator);
1985 m = gfc_match_expr (&iter->var);
1986 if (m != MATCH_YES)
1987 goto cleanup;
1989 if (gfc_match_char ('=') != MATCH_YES
1990 || iter->var->expr_type != EXPR_VARIABLE)
1992 m = MATCH_NO;
1993 goto cleanup;
1996 m = gfc_match_expr (&iter->start);
1997 if (m != MATCH_YES)
1998 goto cleanup;
2000 if (gfc_match_char (':') != MATCH_YES)
2001 goto syntax;
2003 m = gfc_match_expr (&iter->end);
2004 if (m == MATCH_NO)
2005 goto syntax;
2006 if (m == MATCH_ERROR)
2007 goto cleanup;
2009 if (gfc_match_char (':') == MATCH_NO)
2010 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2011 else
2013 m = gfc_match_expr (&iter->stride);
2014 if (m == MATCH_NO)
2015 goto syntax;
2016 if (m == MATCH_ERROR)
2017 goto cleanup;
2020 /* Mark the iteration variable's symbol as used as a FORALL index. */
2021 iter->var->symtree->n.sym->forall_index = true;
2023 *result = iter;
2024 return MATCH_YES;
2026 syntax:
2027 gfc_error ("Syntax error in FORALL iterator at %C");
2028 m = MATCH_ERROR;
2030 cleanup:
2032 gfc_current_locus = where;
2033 gfc_free_forall_iterator (iter);
2034 return m;
2038 /* Match the header of a FORALL statement. */
2040 static match
2041 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2043 gfc_forall_iterator *head, *tail, *new_iter;
2044 gfc_expr *msk;
2045 match m;
2047 gfc_gobble_whitespace ();
2049 head = tail = NULL;
2050 msk = NULL;
2052 if (gfc_match_char ('(') != MATCH_YES)
2053 return MATCH_NO;
2055 m = match_forall_iterator (&new_iter);
2056 if (m == MATCH_ERROR)
2057 goto cleanup;
2058 if (m == MATCH_NO)
2059 goto syntax;
2061 head = tail = new_iter;
2063 for (;;)
2065 if (gfc_match_char (',') != MATCH_YES)
2066 break;
2068 m = match_forall_iterator (&new_iter);
2069 if (m == MATCH_ERROR)
2070 goto cleanup;
2072 if (m == MATCH_YES)
2074 tail->next = new_iter;
2075 tail = new_iter;
2076 continue;
2079 /* Have to have a mask expression. */
2081 m = gfc_match_expr (&msk);
2082 if (m == MATCH_NO)
2083 goto syntax;
2084 if (m == MATCH_ERROR)
2085 goto cleanup;
2087 break;
2090 if (gfc_match_char (')') == MATCH_NO)
2091 goto syntax;
2093 *phead = head;
2094 *mask = msk;
2095 return MATCH_YES;
2097 syntax:
2098 gfc_syntax_error (ST_FORALL);
2100 cleanup:
2101 gfc_free_expr (msk);
2102 gfc_free_forall_iterator (head);
2104 return MATCH_ERROR;
2107 /* Match the rest of a simple FORALL statement that follows an
2108 IF statement. */
2110 static match
2111 match_simple_forall (void)
2113 gfc_forall_iterator *head;
2114 gfc_expr *mask;
2115 gfc_code *c;
2116 match m;
2118 mask = NULL;
2119 head = NULL;
2120 c = NULL;
2122 m = match_forall_header (&head, &mask);
2124 if (m == MATCH_NO)
2125 goto syntax;
2126 if (m != MATCH_YES)
2127 goto cleanup;
2129 m = gfc_match_assignment ();
2131 if (m == MATCH_ERROR)
2132 goto cleanup;
2133 if (m == MATCH_NO)
2135 m = gfc_match_pointer_assignment ();
2136 if (m == MATCH_ERROR)
2137 goto cleanup;
2138 if (m == MATCH_NO)
2139 goto syntax;
2142 c = XCNEW (gfc_code);
2143 *c = new_st;
2144 c->loc = gfc_current_locus;
2146 if (gfc_match_eos () != MATCH_YES)
2147 goto syntax;
2149 gfc_clear_new_st ();
2150 new_st.op = EXEC_FORALL;
2151 new_st.expr1 = mask;
2152 new_st.ext.forall_iterator = head;
2153 new_st.block = gfc_get_code (EXEC_FORALL);
2154 new_st.block->next = c;
2156 return MATCH_YES;
2158 syntax:
2159 gfc_syntax_error (ST_FORALL);
2161 cleanup:
2162 gfc_free_forall_iterator (head);
2163 gfc_free_expr (mask);
2165 return MATCH_ERROR;
2169 /* Match a FORALL statement. */
2171 match
2172 gfc_match_forall (gfc_statement *st)
2174 gfc_forall_iterator *head;
2175 gfc_expr *mask;
2176 gfc_code *c;
2177 match m0, m;
2179 head = NULL;
2180 mask = NULL;
2181 c = NULL;
2183 m0 = gfc_match_label ();
2184 if (m0 == MATCH_ERROR)
2185 return MATCH_ERROR;
2187 m = gfc_match (" forall");
2188 if (m != MATCH_YES)
2189 return m;
2191 m = match_forall_header (&head, &mask);
2192 if (m == MATCH_ERROR)
2193 goto cleanup;
2194 if (m == MATCH_NO)
2195 goto syntax;
2197 if (gfc_match_eos () == MATCH_YES)
2199 *st = ST_FORALL_BLOCK;
2200 new_st.op = EXEC_FORALL;
2201 new_st.expr1 = mask;
2202 new_st.ext.forall_iterator = head;
2203 return MATCH_YES;
2206 m = gfc_match_assignment ();
2207 if (m == MATCH_ERROR)
2208 goto cleanup;
2209 if (m == MATCH_NO)
2211 m = gfc_match_pointer_assignment ();
2212 if (m == MATCH_ERROR)
2213 goto cleanup;
2214 if (m == MATCH_NO)
2215 goto syntax;
2218 c = XCNEW (gfc_code);
2219 *c = new_st;
2220 c->loc = gfc_current_locus;
2222 gfc_clear_new_st ();
2223 new_st.op = EXEC_FORALL;
2224 new_st.expr1 = mask;
2225 new_st.ext.forall_iterator = head;
2226 new_st.block = gfc_get_code (EXEC_FORALL);
2227 new_st.block->next = c;
2229 *st = ST_FORALL;
2230 return MATCH_YES;
2232 syntax:
2233 gfc_syntax_error (ST_FORALL);
2235 cleanup:
2236 gfc_free_forall_iterator (head);
2237 gfc_free_expr (mask);
2238 gfc_free_statements (c);
2239 return MATCH_NO;
2243 /* Match a DO statement. */
2245 match
2246 gfc_match_do (void)
2248 gfc_iterator iter, *ip;
2249 locus old_loc;
2250 gfc_st_label *label;
2251 match m;
2253 old_loc = gfc_current_locus;
2255 label = NULL;
2256 iter.var = iter.start = iter.end = iter.step = NULL;
2258 m = gfc_match_label ();
2259 if (m == MATCH_ERROR)
2260 return m;
2262 if (gfc_match (" do") != MATCH_YES)
2263 return MATCH_NO;
2265 m = gfc_match_st_label (&label);
2266 if (m == MATCH_ERROR)
2267 goto cleanup;
2269 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2271 if (gfc_match_eos () == MATCH_YES)
2273 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2274 new_st.op = EXEC_DO_WHILE;
2275 goto done;
2278 /* Match an optional comma, if no comma is found, a space is obligatory. */
2279 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2280 return MATCH_NO;
2282 /* Check for balanced parens. */
2284 if (gfc_match_parens () == MATCH_ERROR)
2285 return MATCH_ERROR;
2287 if (gfc_match (" concurrent") == MATCH_YES)
2289 gfc_forall_iterator *head;
2290 gfc_expr *mask;
2292 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
2293 return MATCH_ERROR;
2296 mask = NULL;
2297 head = NULL;
2298 m = match_forall_header (&head, &mask);
2300 if (m == MATCH_NO)
2301 return m;
2302 if (m == MATCH_ERROR)
2303 goto concurr_cleanup;
2305 if (gfc_match_eos () != MATCH_YES)
2306 goto concurr_cleanup;
2308 if (label != NULL
2309 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2310 goto concurr_cleanup;
2312 new_st.label1 = label;
2313 new_st.op = EXEC_DO_CONCURRENT;
2314 new_st.expr1 = mask;
2315 new_st.ext.forall_iterator = head;
2317 return MATCH_YES;
2319 concurr_cleanup:
2320 gfc_syntax_error (ST_DO);
2321 gfc_free_expr (mask);
2322 gfc_free_forall_iterator (head);
2323 return MATCH_ERROR;
2326 /* See if we have a DO WHILE. */
2327 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2329 new_st.op = EXEC_DO_WHILE;
2330 goto done;
2333 /* The abortive DO WHILE may have done something to the symbol
2334 table, so we start over. */
2335 gfc_undo_symbols ();
2336 gfc_current_locus = old_loc;
2338 gfc_match_label (); /* This won't error. */
2339 gfc_match (" do "); /* This will work. */
2341 gfc_match_st_label (&label); /* Can't error out. */
2342 gfc_match_char (','); /* Optional comma. */
2344 m = gfc_match_iterator (&iter, 0);
2345 if (m == MATCH_NO)
2346 return MATCH_NO;
2347 if (m == MATCH_ERROR)
2348 goto cleanup;
2350 iter.var->symtree->n.sym->attr.implied_index = 0;
2351 gfc_check_do_variable (iter.var->symtree);
2353 if (gfc_match_eos () != MATCH_YES)
2355 gfc_syntax_error (ST_DO);
2356 goto cleanup;
2359 new_st.op = EXEC_DO;
2361 done:
2362 if (label != NULL
2363 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2364 goto cleanup;
2366 new_st.label1 = label;
2368 if (new_st.op == EXEC_DO_WHILE)
2369 new_st.expr1 = iter.end;
2370 else
2372 new_st.ext.iterator = ip = gfc_get_iterator ();
2373 *ip = iter;
2376 return MATCH_YES;
2378 cleanup:
2379 gfc_free_iterator (&iter, 0);
2381 return MATCH_ERROR;
2385 /* Match an EXIT or CYCLE statement. */
2387 static match
2388 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2390 gfc_state_data *p, *o;
2391 gfc_symbol *sym;
2392 match m;
2393 int cnt;
2395 if (gfc_match_eos () == MATCH_YES)
2396 sym = NULL;
2397 else
2399 char name[GFC_MAX_SYMBOL_LEN + 1];
2400 gfc_symtree* stree;
2402 m = gfc_match ("% %n%t", name);
2403 if (m == MATCH_ERROR)
2404 return MATCH_ERROR;
2405 if (m == MATCH_NO)
2407 gfc_syntax_error (st);
2408 return MATCH_ERROR;
2411 /* Find the corresponding symbol. If there's a BLOCK statement
2412 between here and the label, it is not in gfc_current_ns but a parent
2413 namespace! */
2414 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2415 if (!stree)
2417 gfc_error ("Name %qs in %s statement at %C is unknown",
2418 name, gfc_ascii_statement (st));
2419 return MATCH_ERROR;
2422 sym = stree->n.sym;
2423 if (sym->attr.flavor != FL_LABEL)
2425 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2426 name, gfc_ascii_statement (st));
2427 return MATCH_ERROR;
2431 /* Find the loop specified by the label (or lack of a label). */
2432 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2433 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2434 o = p;
2435 else if (p->state == COMP_CRITICAL)
2437 gfc_error("%s statement at %C leaves CRITICAL construct",
2438 gfc_ascii_statement (st));
2439 return MATCH_ERROR;
2441 else if (p->state == COMP_DO_CONCURRENT
2442 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2444 /* F2008, C821 & C845. */
2445 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2446 gfc_ascii_statement (st));
2447 return MATCH_ERROR;
2449 else if ((sym && sym == p->sym)
2450 || (!sym && (p->state == COMP_DO
2451 || p->state == COMP_DO_CONCURRENT)))
2452 break;
2454 if (p == NULL)
2456 if (sym == NULL)
2457 gfc_error ("%s statement at %C is not within a construct",
2458 gfc_ascii_statement (st));
2459 else
2460 gfc_error ("%s statement at %C is not within construct %qs",
2461 gfc_ascii_statement (st), sym->name);
2463 return MATCH_ERROR;
2466 /* Special checks for EXIT from non-loop constructs. */
2467 switch (p->state)
2469 case COMP_DO:
2470 case COMP_DO_CONCURRENT:
2471 break;
2473 case COMP_CRITICAL:
2474 /* This is already handled above. */
2475 gcc_unreachable ();
2477 case COMP_ASSOCIATE:
2478 case COMP_BLOCK:
2479 case COMP_IF:
2480 case COMP_SELECT:
2481 case COMP_SELECT_TYPE:
2482 gcc_assert (sym);
2483 if (op == EXEC_CYCLE)
2485 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2486 " construct %qs", sym->name);
2487 return MATCH_ERROR;
2489 gcc_assert (op == EXEC_EXIT);
2490 if (!gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2491 " do-construct-name at %C"))
2492 return MATCH_ERROR;
2493 break;
2495 default:
2496 gfc_error ("%s statement at %C is not applicable to construct %qs",
2497 gfc_ascii_statement (st), sym->name);
2498 return MATCH_ERROR;
2501 if (o != NULL)
2503 gfc_error (is_oacc (p)
2504 ? "%s statement at %C leaving OpenACC structured block"
2505 : "%s statement at %C leaving OpenMP structured block",
2506 gfc_ascii_statement (st));
2507 return MATCH_ERROR;
2510 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2511 o = o->previous;
2512 if (cnt > 0
2513 && o != NULL
2514 && o->state == COMP_OMP_STRUCTURED_BLOCK
2515 && (o->head->op == EXEC_OACC_LOOP
2516 || o->head->op == EXEC_OACC_PARALLEL_LOOP))
2518 int collapse = 1;
2519 gcc_assert (o->head->next != NULL
2520 && (o->head->next->op == EXEC_DO
2521 || o->head->next->op == EXEC_DO_WHILE)
2522 && o->previous != NULL
2523 && o->previous->tail->op == o->head->op);
2524 if (o->previous->tail->ext.omp_clauses != NULL
2525 && o->previous->tail->ext.omp_clauses->collapse > 1)
2526 collapse = o->previous->tail->ext.omp_clauses->collapse;
2527 if (st == ST_EXIT && cnt <= collapse)
2529 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2530 return MATCH_ERROR;
2532 if (st == ST_CYCLE && cnt < collapse)
2534 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2535 " !$ACC LOOP loop");
2536 return MATCH_ERROR;
2539 if (cnt > 0
2540 && o != NULL
2541 && (o->state == COMP_OMP_STRUCTURED_BLOCK)
2542 && (o->head->op == EXEC_OMP_DO
2543 || o->head->op == EXEC_OMP_PARALLEL_DO
2544 || o->head->op == EXEC_OMP_SIMD
2545 || o->head->op == EXEC_OMP_DO_SIMD
2546 || o->head->op == EXEC_OMP_PARALLEL_DO_SIMD))
2548 int collapse = 1;
2549 gcc_assert (o->head->next != NULL
2550 && (o->head->next->op == EXEC_DO
2551 || o->head->next->op == EXEC_DO_WHILE)
2552 && o->previous != NULL
2553 && o->previous->tail->op == o->head->op);
2554 if (o->previous->tail->ext.omp_clauses != NULL
2555 && o->previous->tail->ext.omp_clauses->collapse > 1)
2556 collapse = o->previous->tail->ext.omp_clauses->collapse;
2557 if (st == ST_EXIT && cnt <= collapse)
2559 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2560 return MATCH_ERROR;
2562 if (st == ST_CYCLE && cnt < collapse)
2564 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2565 " !$OMP DO loop");
2566 return MATCH_ERROR;
2570 /* Save the first statement in the construct - needed by the backend. */
2571 new_st.ext.which_construct = p->construct;
2573 new_st.op = op;
2575 return MATCH_YES;
2579 /* Match the EXIT statement. */
2581 match
2582 gfc_match_exit (void)
2584 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2588 /* Match the CYCLE statement. */
2590 match
2591 gfc_match_cycle (void)
2593 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2597 /* Match a number or character constant after an (ERROR) STOP or PAUSE
2598 statement. */
2600 static match
2601 gfc_match_stopcode (gfc_statement st)
2603 gfc_expr *e;
2604 match m;
2606 e = NULL;
2608 if (gfc_match_eos () != MATCH_YES)
2610 m = gfc_match_init_expr (&e);
2611 if (m == MATCH_ERROR)
2612 goto cleanup;
2613 if (m == MATCH_NO)
2614 goto syntax;
2616 if (gfc_match_eos () != MATCH_YES)
2617 goto syntax;
2620 if (gfc_pure (NULL))
2622 if (st == ST_ERROR_STOP)
2624 if (!gfc_notify_std (GFC_STD_F2015, "%s statement at %C in PURE "
2625 "procedure", gfc_ascii_statement (st)))
2626 goto cleanup;
2628 else
2630 gfc_error ("%s statement not allowed in PURE procedure at %C",
2631 gfc_ascii_statement (st));
2632 goto cleanup;
2636 gfc_unset_implicit_pure (NULL);
2638 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
2640 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2641 goto cleanup;
2643 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
2645 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
2646 goto cleanup;
2649 if (e != NULL)
2651 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
2653 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2654 &e->where);
2655 goto cleanup;
2658 if (e->rank != 0)
2660 gfc_error ("STOP code at %L must be scalar",
2661 &e->where);
2662 goto cleanup;
2665 if (e->ts.type == BT_CHARACTER
2666 && e->ts.kind != gfc_default_character_kind)
2668 gfc_error ("STOP code at %L must be default character KIND=%d",
2669 &e->where, (int) gfc_default_character_kind);
2670 goto cleanup;
2673 if (e->ts.type == BT_INTEGER
2674 && e->ts.kind != gfc_default_integer_kind)
2676 gfc_error ("STOP code at %L must be default integer KIND=%d",
2677 &e->where, (int) gfc_default_integer_kind);
2678 goto cleanup;
2682 switch (st)
2684 case ST_STOP:
2685 new_st.op = EXEC_STOP;
2686 break;
2687 case ST_ERROR_STOP:
2688 new_st.op = EXEC_ERROR_STOP;
2689 break;
2690 case ST_PAUSE:
2691 new_st.op = EXEC_PAUSE;
2692 break;
2693 default:
2694 gcc_unreachable ();
2697 new_st.expr1 = e;
2698 new_st.ext.stop_code = -1;
2700 return MATCH_YES;
2702 syntax:
2703 gfc_syntax_error (st);
2705 cleanup:
2707 gfc_free_expr (e);
2708 return MATCH_ERROR;
2712 /* Match the (deprecated) PAUSE statement. */
2714 match
2715 gfc_match_pause (void)
2717 match m;
2719 m = gfc_match_stopcode (ST_PAUSE);
2720 if (m == MATCH_YES)
2722 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
2723 m = MATCH_ERROR;
2725 return m;
2729 /* Match the STOP statement. */
2731 match
2732 gfc_match_stop (void)
2734 return gfc_match_stopcode (ST_STOP);
2738 /* Match the ERROR STOP statement. */
2740 match
2741 gfc_match_error_stop (void)
2743 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
2744 return MATCH_ERROR;
2746 return gfc_match_stopcode (ST_ERROR_STOP);
2750 /* Match LOCK/UNLOCK statement. Syntax:
2751 LOCK ( lock-variable [ , lock-stat-list ] )
2752 UNLOCK ( lock-variable [ , sync-stat-list ] )
2753 where lock-stat is ACQUIRED_LOCK or sync-stat
2754 and sync-stat is STAT= or ERRMSG=. */
2756 static match
2757 lock_unlock_statement (gfc_statement st)
2759 match m;
2760 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
2761 bool saw_acq_lock, saw_stat, saw_errmsg;
2763 tmp = lockvar = acq_lock = stat = errmsg = NULL;
2764 saw_acq_lock = saw_stat = saw_errmsg = false;
2766 if (gfc_pure (NULL))
2768 gfc_error ("Image control statement %s at %C in PURE procedure",
2769 st == ST_LOCK ? "LOCK" : "UNLOCK");
2770 return MATCH_ERROR;
2773 gfc_unset_implicit_pure (NULL);
2775 if (flag_coarray == GFC_FCOARRAY_NONE)
2777 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2778 return MATCH_ERROR;
2781 if (gfc_find_state (COMP_CRITICAL))
2783 gfc_error ("Image control statement %s at %C in CRITICAL block",
2784 st == ST_LOCK ? "LOCK" : "UNLOCK");
2785 return MATCH_ERROR;
2788 if (gfc_find_state (COMP_DO_CONCURRENT))
2790 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
2791 st == ST_LOCK ? "LOCK" : "UNLOCK");
2792 return MATCH_ERROR;
2795 if (gfc_match_char ('(') != MATCH_YES)
2796 goto syntax;
2798 if (gfc_match ("%e", &lockvar) != MATCH_YES)
2799 goto syntax;
2800 m = gfc_match_char (',');
2801 if (m == MATCH_ERROR)
2802 goto syntax;
2803 if (m == MATCH_NO)
2805 m = gfc_match_char (')');
2806 if (m == MATCH_YES)
2807 goto done;
2808 goto syntax;
2811 for (;;)
2813 m = gfc_match (" stat = %v", &tmp);
2814 if (m == MATCH_ERROR)
2815 goto syntax;
2816 if (m == MATCH_YES)
2818 if (saw_stat)
2820 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
2821 goto cleanup;
2823 stat = tmp;
2824 saw_stat = true;
2826 m = gfc_match_char (',');
2827 if (m == MATCH_YES)
2828 continue;
2830 tmp = NULL;
2831 break;
2834 m = gfc_match (" errmsg = %v", &tmp);
2835 if (m == MATCH_ERROR)
2836 goto syntax;
2837 if (m == MATCH_YES)
2839 if (saw_errmsg)
2841 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
2842 goto cleanup;
2844 errmsg = tmp;
2845 saw_errmsg = true;
2847 m = gfc_match_char (',');
2848 if (m == MATCH_YES)
2849 continue;
2851 tmp = NULL;
2852 break;
2855 m = gfc_match (" acquired_lock = %v", &tmp);
2856 if (m == MATCH_ERROR || st == ST_UNLOCK)
2857 goto syntax;
2858 if (m == MATCH_YES)
2860 if (saw_acq_lock)
2862 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L ",
2863 &tmp->where);
2864 goto cleanup;
2866 acq_lock = tmp;
2867 saw_acq_lock = true;
2869 m = gfc_match_char (',');
2870 if (m == MATCH_YES)
2871 continue;
2873 tmp = NULL;
2874 break;
2877 break;
2880 if (m == MATCH_ERROR)
2881 goto syntax;
2883 if (gfc_match (" )%t") != MATCH_YES)
2884 goto syntax;
2886 done:
2887 switch (st)
2889 case ST_LOCK:
2890 new_st.op = EXEC_LOCK;
2891 break;
2892 case ST_UNLOCK:
2893 new_st.op = EXEC_UNLOCK;
2894 break;
2895 default:
2896 gcc_unreachable ();
2899 new_st.expr1 = lockvar;
2900 new_st.expr2 = stat;
2901 new_st.expr3 = errmsg;
2902 new_st.expr4 = acq_lock;
2904 return MATCH_YES;
2906 syntax:
2907 gfc_syntax_error (st);
2909 cleanup:
2910 if (acq_lock != tmp)
2911 gfc_free_expr (acq_lock);
2912 if (errmsg != tmp)
2913 gfc_free_expr (errmsg);
2914 if (stat != tmp)
2915 gfc_free_expr (stat);
2917 gfc_free_expr (tmp);
2918 gfc_free_expr (lockvar);
2920 return MATCH_ERROR;
2924 match
2925 gfc_match_lock (void)
2927 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
2928 return MATCH_ERROR;
2930 return lock_unlock_statement (ST_LOCK);
2934 match
2935 gfc_match_unlock (void)
2937 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
2938 return MATCH_ERROR;
2940 return lock_unlock_statement (ST_UNLOCK);
2944 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
2945 SYNC ALL [(sync-stat-list)]
2946 SYNC MEMORY [(sync-stat-list)]
2947 SYNC IMAGES (image-set [, sync-stat-list] )
2948 with sync-stat is int-expr or *. */
2950 static match
2951 sync_statement (gfc_statement st)
2953 match m;
2954 gfc_expr *tmp, *imageset, *stat, *errmsg;
2955 bool saw_stat, saw_errmsg;
2957 tmp = imageset = stat = errmsg = NULL;
2958 saw_stat = saw_errmsg = false;
2960 if (gfc_pure (NULL))
2962 gfc_error ("Image control statement SYNC at %C in PURE procedure");
2963 return MATCH_ERROR;
2966 gfc_unset_implicit_pure (NULL);
2968 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
2969 return MATCH_ERROR;
2971 if (flag_coarray == GFC_FCOARRAY_NONE)
2973 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
2974 "enable");
2975 return MATCH_ERROR;
2978 if (gfc_find_state (COMP_CRITICAL))
2980 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
2981 return MATCH_ERROR;
2984 if (gfc_find_state (COMP_DO_CONCURRENT))
2986 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
2987 return MATCH_ERROR;
2990 if (gfc_match_eos () == MATCH_YES)
2992 if (st == ST_SYNC_IMAGES)
2993 goto syntax;
2994 goto done;
2997 if (gfc_match_char ('(') != MATCH_YES)
2998 goto syntax;
3000 if (st == ST_SYNC_IMAGES)
3002 /* Denote '*' as imageset == NULL. */
3003 m = gfc_match_char ('*');
3004 if (m == MATCH_ERROR)
3005 goto syntax;
3006 if (m == MATCH_NO)
3008 if (gfc_match ("%e", &imageset) != MATCH_YES)
3009 goto syntax;
3011 m = gfc_match_char (',');
3012 if (m == MATCH_ERROR)
3013 goto syntax;
3014 if (m == MATCH_NO)
3016 m = gfc_match_char (')');
3017 if (m == MATCH_YES)
3018 goto done;
3019 goto syntax;
3023 for (;;)
3025 m = gfc_match (" stat = %v", &tmp);
3026 if (m == MATCH_ERROR)
3027 goto syntax;
3028 if (m == MATCH_YES)
3030 if (saw_stat)
3032 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3033 goto cleanup;
3035 stat = tmp;
3036 saw_stat = true;
3038 if (gfc_match_char (',') == MATCH_YES)
3039 continue;
3041 tmp = NULL;
3042 break;
3045 m = gfc_match (" errmsg = %v", &tmp);
3046 if (m == MATCH_ERROR)
3047 goto syntax;
3048 if (m == MATCH_YES)
3050 if (saw_errmsg)
3052 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3053 goto cleanup;
3055 errmsg = tmp;
3056 saw_errmsg = true;
3058 if (gfc_match_char (',') == MATCH_YES)
3059 continue;
3061 tmp = NULL;
3062 break;
3065 break;
3068 if (gfc_match (" )%t") != MATCH_YES)
3069 goto syntax;
3071 done:
3072 switch (st)
3074 case ST_SYNC_ALL:
3075 new_st.op = EXEC_SYNC_ALL;
3076 break;
3077 case ST_SYNC_IMAGES:
3078 new_st.op = EXEC_SYNC_IMAGES;
3079 break;
3080 case ST_SYNC_MEMORY:
3081 new_st.op = EXEC_SYNC_MEMORY;
3082 break;
3083 default:
3084 gcc_unreachable ();
3087 new_st.expr1 = imageset;
3088 new_st.expr2 = stat;
3089 new_st.expr3 = errmsg;
3091 return MATCH_YES;
3093 syntax:
3094 gfc_syntax_error (st);
3096 cleanup:
3097 if (stat != tmp)
3098 gfc_free_expr (stat);
3099 if (errmsg != tmp)
3100 gfc_free_expr (errmsg);
3102 gfc_free_expr (tmp);
3103 gfc_free_expr (imageset);
3105 return MATCH_ERROR;
3109 /* Match SYNC ALL statement. */
3111 match
3112 gfc_match_sync_all (void)
3114 return sync_statement (ST_SYNC_ALL);
3118 /* Match SYNC IMAGES statement. */
3120 match
3121 gfc_match_sync_images (void)
3123 return sync_statement (ST_SYNC_IMAGES);
3127 /* Match SYNC MEMORY statement. */
3129 match
3130 gfc_match_sync_memory (void)
3132 return sync_statement (ST_SYNC_MEMORY);
3136 /* Match a CONTINUE statement. */
3138 match
3139 gfc_match_continue (void)
3141 if (gfc_match_eos () != MATCH_YES)
3143 gfc_syntax_error (ST_CONTINUE);
3144 return MATCH_ERROR;
3147 new_st.op = EXEC_CONTINUE;
3148 return MATCH_YES;
3152 /* Match the (deprecated) ASSIGN statement. */
3154 match
3155 gfc_match_assign (void)
3157 gfc_expr *expr;
3158 gfc_st_label *label;
3160 if (gfc_match (" %l", &label) == MATCH_YES)
3162 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
3163 return MATCH_ERROR;
3164 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
3166 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
3167 return MATCH_ERROR;
3169 expr->symtree->n.sym->attr.assign = 1;
3171 new_st.op = EXEC_LABEL_ASSIGN;
3172 new_st.label1 = label;
3173 new_st.expr1 = expr;
3174 return MATCH_YES;
3177 return MATCH_NO;
3181 /* Match the GO TO statement. As a computed GOTO statement is
3182 matched, it is transformed into an equivalent SELECT block. No
3183 tree is necessary, and the resulting jumps-to-jumps are
3184 specifically optimized away by the back end. */
3186 match
3187 gfc_match_goto (void)
3189 gfc_code *head, *tail;
3190 gfc_expr *expr;
3191 gfc_case *cp;
3192 gfc_st_label *label;
3193 int i;
3194 match m;
3196 if (gfc_match (" %l%t", &label) == MATCH_YES)
3198 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3199 return MATCH_ERROR;
3201 new_st.op = EXEC_GOTO;
3202 new_st.label1 = label;
3203 return MATCH_YES;
3206 /* The assigned GO TO statement. */
3208 if (gfc_match_variable (&expr, 0) == MATCH_YES)
3210 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
3211 return MATCH_ERROR;
3213 new_st.op = EXEC_GOTO;
3214 new_st.expr1 = expr;
3216 if (gfc_match_eos () == MATCH_YES)
3217 return MATCH_YES;
3219 /* Match label list. */
3220 gfc_match_char (',');
3221 if (gfc_match_char ('(') != MATCH_YES)
3223 gfc_syntax_error (ST_GOTO);
3224 return MATCH_ERROR;
3226 head = tail = NULL;
3230 m = gfc_match_st_label (&label);
3231 if (m != MATCH_YES)
3232 goto syntax;
3234 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3235 goto cleanup;
3237 if (head == NULL)
3238 head = tail = gfc_get_code (EXEC_GOTO);
3239 else
3241 tail->block = gfc_get_code (EXEC_GOTO);
3242 tail = tail->block;
3245 tail->label1 = label;
3247 while (gfc_match_char (',') == MATCH_YES);
3249 if (gfc_match (")%t") != MATCH_YES)
3250 goto syntax;
3252 if (head == NULL)
3254 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3255 goto syntax;
3257 new_st.block = head;
3259 return MATCH_YES;
3262 /* Last chance is a computed GO TO statement. */
3263 if (gfc_match_char ('(') != MATCH_YES)
3265 gfc_syntax_error (ST_GOTO);
3266 return MATCH_ERROR;
3269 head = tail = NULL;
3270 i = 1;
3274 m = gfc_match_st_label (&label);
3275 if (m != MATCH_YES)
3276 goto syntax;
3278 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3279 goto cleanup;
3281 if (head == NULL)
3282 head = tail = gfc_get_code (EXEC_SELECT);
3283 else
3285 tail->block = gfc_get_code (EXEC_SELECT);
3286 tail = tail->block;
3289 cp = gfc_get_case ();
3290 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
3291 NULL, i++);
3293 tail->ext.block.case_list = cp;
3295 tail->next = gfc_get_code (EXEC_GOTO);
3296 tail->next->label1 = label;
3298 while (gfc_match_char (',') == MATCH_YES);
3300 if (gfc_match_char (')') != MATCH_YES)
3301 goto syntax;
3303 if (head == NULL)
3305 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3306 goto syntax;
3309 /* Get the rest of the statement. */
3310 gfc_match_char (',');
3312 if (gfc_match (" %e%t", &expr) != MATCH_YES)
3313 goto syntax;
3315 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
3316 return MATCH_ERROR;
3318 /* At this point, a computed GOTO has been fully matched and an
3319 equivalent SELECT statement constructed. */
3321 new_st.op = EXEC_SELECT;
3322 new_st.expr1 = NULL;
3324 /* Hack: For a "real" SELECT, the expression is in expr. We put
3325 it in expr2 so we can distinguish then and produce the correct
3326 diagnostics. */
3327 new_st.expr2 = expr;
3328 new_st.block = head;
3329 return MATCH_YES;
3331 syntax:
3332 gfc_syntax_error (ST_GOTO);
3333 cleanup:
3334 gfc_free_statements (head);
3335 return MATCH_ERROR;
3339 /* Frees a list of gfc_alloc structures. */
3341 void
3342 gfc_free_alloc_list (gfc_alloc *p)
3344 gfc_alloc *q;
3346 for (; p; p = q)
3348 q = p->next;
3349 gfc_free_expr (p->expr);
3350 free (p);
3355 /* Match an ALLOCATE statement. */
3357 match
3358 gfc_match_allocate (void)
3360 gfc_alloc *head, *tail;
3361 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
3362 gfc_typespec ts;
3363 gfc_symbol *sym;
3364 match m;
3365 locus old_locus, deferred_locus;
3366 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
3367 bool saw_unlimited = false;
3369 head = tail = NULL;
3370 stat = errmsg = source = mold = tmp = NULL;
3371 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
3373 if (gfc_match_char ('(') != MATCH_YES)
3374 goto syntax;
3376 /* Match an optional type-spec. */
3377 old_locus = gfc_current_locus;
3378 m = gfc_match_type_spec (&ts);
3379 if (m == MATCH_ERROR)
3380 goto cleanup;
3381 else if (m == MATCH_NO)
3383 char name[GFC_MAX_SYMBOL_LEN + 3];
3385 if (gfc_match ("%n :: ", name) == MATCH_YES)
3387 gfc_error ("Error in type-spec at %L", &old_locus);
3388 goto cleanup;
3391 ts.type = BT_UNKNOWN;
3393 else
3395 if (gfc_match (" :: ") == MATCH_YES)
3397 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
3398 &old_locus))
3399 goto cleanup;
3401 if (ts.deferred)
3403 gfc_error ("Type-spec at %L cannot contain a deferred "
3404 "type parameter", &old_locus);
3405 goto cleanup;
3408 if (ts.type == BT_CHARACTER)
3409 ts.u.cl->length_from_typespec = true;
3411 else
3413 ts.type = BT_UNKNOWN;
3414 gfc_current_locus = old_locus;
3418 for (;;)
3420 if (head == NULL)
3421 head = tail = gfc_get_alloc ();
3422 else
3424 tail->next = gfc_get_alloc ();
3425 tail = tail->next;
3428 m = gfc_match_variable (&tail->expr, 0);
3429 if (m == MATCH_NO)
3430 goto syntax;
3431 if (m == MATCH_ERROR)
3432 goto cleanup;
3434 if (gfc_check_do_variable (tail->expr->symtree))
3435 goto cleanup;
3437 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
3438 if (impure && gfc_pure (NULL))
3440 gfc_error ("Bad allocate-object at %C for a PURE procedure");
3441 goto cleanup;
3444 if (impure)
3445 gfc_unset_implicit_pure (NULL);
3447 if (tail->expr->ts.deferred)
3449 saw_deferred = true;
3450 deferred_locus = tail->expr->where;
3453 if (gfc_find_state (COMP_DO_CONCURRENT)
3454 || gfc_find_state (COMP_CRITICAL))
3456 gfc_ref *ref;
3457 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
3458 for (ref = tail->expr->ref; ref; ref = ref->next)
3459 if (ref->type == REF_COMPONENT)
3460 coarray = ref->u.c.component->attr.codimension;
3462 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
3464 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
3465 goto cleanup;
3467 if (coarray && gfc_find_state (COMP_CRITICAL))
3469 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
3470 goto cleanup;
3474 /* Check for F08:C628. */
3475 sym = tail->expr->symtree->n.sym;
3476 b1 = !(tail->expr->ref
3477 && (tail->expr->ref->type == REF_COMPONENT
3478 || tail->expr->ref->type == REF_ARRAY));
3479 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
3480 b2 = !(CLASS_DATA (sym)->attr.allocatable
3481 || CLASS_DATA (sym)->attr.class_pointer);
3482 else
3483 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
3484 || sym->attr.proc_pointer);
3485 b3 = sym && sym->ns && sym->ns->proc_name
3486 && (sym->ns->proc_name->attr.allocatable
3487 || sym->ns->proc_name->attr.pointer
3488 || sym->ns->proc_name->attr.proc_pointer);
3489 if (b1 && b2 && !b3)
3491 gfc_error ("Allocate-object at %L is neither a data pointer "
3492 "nor an allocatable variable", &tail->expr->where);
3493 goto cleanup;
3496 /* The ALLOCATE statement had an optional typespec. Check the
3497 constraints. */
3498 if (ts.type != BT_UNKNOWN)
3500 /* Enforce F03:C624. */
3501 if (!gfc_type_compatible (&tail->expr->ts, &ts))
3503 gfc_error ("Type of entity at %L is type incompatible with "
3504 "typespec", &tail->expr->where);
3505 goto cleanup;
3508 /* Enforce F03:C627. */
3509 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
3511 gfc_error ("Kind type parameter for entity at %L differs from "
3512 "the kind type parameter of the typespec",
3513 &tail->expr->where);
3514 goto cleanup;
3518 if (tail->expr->ts.type == BT_DERIVED)
3519 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
3521 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
3523 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
3525 gfc_error ("Shape specification for allocatable scalar at %C");
3526 goto cleanup;
3529 if (gfc_match_char (',') != MATCH_YES)
3530 break;
3532 alloc_opt_list:
3534 m = gfc_match (" stat = %v", &tmp);
3535 if (m == MATCH_ERROR)
3536 goto cleanup;
3537 if (m == MATCH_YES)
3539 /* Enforce C630. */
3540 if (saw_stat)
3542 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3543 goto cleanup;
3546 stat = tmp;
3547 tmp = NULL;
3548 saw_stat = true;
3550 if (gfc_check_do_variable (stat->symtree))
3551 goto cleanup;
3553 if (gfc_match_char (',') == MATCH_YES)
3554 goto alloc_opt_list;
3557 m = gfc_match (" errmsg = %v", &tmp);
3558 if (m == MATCH_ERROR)
3559 goto cleanup;
3560 if (m == MATCH_YES)
3562 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
3563 goto cleanup;
3565 /* Enforce C630. */
3566 if (saw_errmsg)
3568 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3569 goto cleanup;
3572 errmsg = tmp;
3573 tmp = NULL;
3574 saw_errmsg = true;
3576 if (gfc_match_char (',') == MATCH_YES)
3577 goto alloc_opt_list;
3580 m = gfc_match (" source = %e", &tmp);
3581 if (m == MATCH_ERROR)
3582 goto cleanup;
3583 if (m == MATCH_YES)
3585 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
3586 goto cleanup;
3588 /* Enforce C630. */
3589 if (saw_source)
3591 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
3592 goto cleanup;
3595 /* The next 2 conditionals check C631. */
3596 if (ts.type != BT_UNKNOWN)
3598 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
3599 &tmp->where, &old_locus);
3600 goto cleanup;
3603 if (head->next
3604 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
3605 " with more than a single allocate object",
3606 &tmp->where))
3607 goto cleanup;
3609 source = tmp;
3610 tmp = NULL;
3611 saw_source = true;
3613 if (gfc_match_char (',') == MATCH_YES)
3614 goto alloc_opt_list;
3617 m = gfc_match (" mold = %e", &tmp);
3618 if (m == MATCH_ERROR)
3619 goto cleanup;
3620 if (m == MATCH_YES)
3622 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
3623 goto cleanup;
3625 /* Check F08:C636. */
3626 if (saw_mold)
3628 gfc_error ("Redundant MOLD tag found at %L ", &tmp->where);
3629 goto cleanup;
3632 /* Check F08:C637. */
3633 if (ts.type != BT_UNKNOWN)
3635 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
3636 &tmp->where, &old_locus);
3637 goto cleanup;
3640 mold = tmp;
3641 tmp = NULL;
3642 saw_mold = true;
3643 mold->mold = 1;
3645 if (gfc_match_char (',') == MATCH_YES)
3646 goto alloc_opt_list;
3649 gfc_gobble_whitespace ();
3651 if (gfc_peek_char () == ')')
3652 break;
3655 if (gfc_match (" )%t") != MATCH_YES)
3656 goto syntax;
3658 /* Check F08:C637. */
3659 if (source && mold)
3661 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
3662 &mold->where, &source->where);
3663 goto cleanup;
3666 /* Check F03:C623, */
3667 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
3669 gfc_error ("Allocate-object at %L with a deferred type parameter "
3670 "requires either a type-spec or SOURCE tag or a MOLD tag",
3671 &deferred_locus);
3672 goto cleanup;
3675 /* Check F03:C625, */
3676 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
3678 for (tail = head; tail; tail = tail->next)
3680 if (UNLIMITED_POLY (tail->expr))
3681 gfc_error ("Unlimited polymorphic allocate-object at %L "
3682 "requires either a type-spec or SOURCE tag "
3683 "or a MOLD tag", &tail->expr->where);
3685 goto cleanup;
3688 new_st.op = EXEC_ALLOCATE;
3689 new_st.expr1 = stat;
3690 new_st.expr2 = errmsg;
3691 if (source)
3692 new_st.expr3 = source;
3693 else
3694 new_st.expr3 = mold;
3695 new_st.ext.alloc.list = head;
3696 new_st.ext.alloc.ts = ts;
3698 return MATCH_YES;
3700 syntax:
3701 gfc_syntax_error (ST_ALLOCATE);
3703 cleanup:
3704 gfc_free_expr (errmsg);
3705 gfc_free_expr (source);
3706 gfc_free_expr (stat);
3707 gfc_free_expr (mold);
3708 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
3709 gfc_free_alloc_list (head);
3710 return MATCH_ERROR;
3714 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
3715 a set of pointer assignments to intrinsic NULL(). */
3717 match
3718 gfc_match_nullify (void)
3720 gfc_code *tail;
3721 gfc_expr *e, *p;
3722 match m;
3724 tail = NULL;
3726 if (gfc_match_char ('(') != MATCH_YES)
3727 goto syntax;
3729 for (;;)
3731 m = gfc_match_variable (&p, 0);
3732 if (m == MATCH_ERROR)
3733 goto cleanup;
3734 if (m == MATCH_NO)
3735 goto syntax;
3737 if (gfc_check_do_variable (p->symtree))
3738 goto cleanup;
3740 /* F2008, C1242. */
3741 if (gfc_is_coindexed (p))
3743 gfc_error ("Pointer object at %C shall not be coindexed");
3744 goto cleanup;
3747 /* build ' => NULL() '. */
3748 e = gfc_get_null_expr (&gfc_current_locus);
3750 /* Chain to list. */
3751 if (tail == NULL)
3753 tail = &new_st;
3754 tail->op = EXEC_POINTER_ASSIGN;
3756 else
3758 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
3759 tail = tail->next;
3762 tail->expr1 = p;
3763 tail->expr2 = e;
3765 if (gfc_match (" )%t") == MATCH_YES)
3766 break;
3767 if (gfc_match_char (',') != MATCH_YES)
3768 goto syntax;
3771 return MATCH_YES;
3773 syntax:
3774 gfc_syntax_error (ST_NULLIFY);
3776 cleanup:
3777 gfc_free_statements (new_st.next);
3778 new_st.next = NULL;
3779 gfc_free_expr (new_st.expr1);
3780 new_st.expr1 = NULL;
3781 gfc_free_expr (new_st.expr2);
3782 new_st.expr2 = NULL;
3783 return MATCH_ERROR;
3787 /* Match a DEALLOCATE statement. */
3789 match
3790 gfc_match_deallocate (void)
3792 gfc_alloc *head, *tail;
3793 gfc_expr *stat, *errmsg, *tmp;
3794 gfc_symbol *sym;
3795 match m;
3796 bool saw_stat, saw_errmsg, b1, b2;
3798 head = tail = NULL;
3799 stat = errmsg = tmp = NULL;
3800 saw_stat = saw_errmsg = false;
3802 if (gfc_match_char ('(') != MATCH_YES)
3803 goto syntax;
3805 for (;;)
3807 if (head == NULL)
3808 head = tail = gfc_get_alloc ();
3809 else
3811 tail->next = gfc_get_alloc ();
3812 tail = tail->next;
3815 m = gfc_match_variable (&tail->expr, 0);
3816 if (m == MATCH_ERROR)
3817 goto cleanup;
3818 if (m == MATCH_NO)
3819 goto syntax;
3821 if (gfc_check_do_variable (tail->expr->symtree))
3822 goto cleanup;
3824 sym = tail->expr->symtree->n.sym;
3826 bool impure = gfc_impure_variable (sym);
3827 if (impure && gfc_pure (NULL))
3829 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
3830 goto cleanup;
3833 if (impure)
3834 gfc_unset_implicit_pure (NULL);
3836 if (gfc_is_coarray (tail->expr)
3837 && gfc_find_state (COMP_DO_CONCURRENT))
3839 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
3840 goto cleanup;
3843 if (gfc_is_coarray (tail->expr)
3844 && gfc_find_state (COMP_CRITICAL))
3846 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
3847 goto cleanup;
3850 /* FIXME: disable the checking on derived types. */
3851 b1 = !(tail->expr->ref
3852 && (tail->expr->ref->type == REF_COMPONENT
3853 || tail->expr->ref->type == REF_ARRAY));
3854 if (sym && sym->ts.type == BT_CLASS)
3855 b2 = !(CLASS_DATA (sym)->attr.allocatable
3856 || CLASS_DATA (sym)->attr.class_pointer);
3857 else
3858 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
3859 || sym->attr.proc_pointer);
3860 if (b1 && b2)
3862 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
3863 "nor an allocatable variable");
3864 goto cleanup;
3867 if (gfc_match_char (',') != MATCH_YES)
3868 break;
3870 dealloc_opt_list:
3872 m = gfc_match (" stat = %v", &tmp);
3873 if (m == MATCH_ERROR)
3874 goto cleanup;
3875 if (m == MATCH_YES)
3877 if (saw_stat)
3879 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3880 gfc_free_expr (tmp);
3881 goto cleanup;
3884 stat = tmp;
3885 saw_stat = true;
3887 if (gfc_check_do_variable (stat->symtree))
3888 goto cleanup;
3890 if (gfc_match_char (',') == MATCH_YES)
3891 goto dealloc_opt_list;
3894 m = gfc_match (" errmsg = %v", &tmp);
3895 if (m == MATCH_ERROR)
3896 goto cleanup;
3897 if (m == MATCH_YES)
3899 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
3900 goto cleanup;
3902 if (saw_errmsg)
3904 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3905 gfc_free_expr (tmp);
3906 goto cleanup;
3909 errmsg = tmp;
3910 saw_errmsg = true;
3912 if (gfc_match_char (',') == MATCH_YES)
3913 goto dealloc_opt_list;
3916 gfc_gobble_whitespace ();
3918 if (gfc_peek_char () == ')')
3919 break;
3922 if (gfc_match (" )%t") != MATCH_YES)
3923 goto syntax;
3925 new_st.op = EXEC_DEALLOCATE;
3926 new_st.expr1 = stat;
3927 new_st.expr2 = errmsg;
3928 new_st.ext.alloc.list = head;
3930 return MATCH_YES;
3932 syntax:
3933 gfc_syntax_error (ST_DEALLOCATE);
3935 cleanup:
3936 gfc_free_expr (errmsg);
3937 gfc_free_expr (stat);
3938 gfc_free_alloc_list (head);
3939 return MATCH_ERROR;
3943 /* Match a RETURN statement. */
3945 match
3946 gfc_match_return (void)
3948 gfc_expr *e;
3949 match m;
3950 gfc_compile_state s;
3952 e = NULL;
3954 if (gfc_find_state (COMP_CRITICAL))
3956 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
3957 return MATCH_ERROR;
3960 if (gfc_find_state (COMP_DO_CONCURRENT))
3962 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
3963 return MATCH_ERROR;
3966 if (gfc_match_eos () == MATCH_YES)
3967 goto done;
3969 if (!gfc_find_state (COMP_SUBROUTINE))
3971 gfc_error ("Alternate RETURN statement at %C is only allowed within "
3972 "a SUBROUTINE");
3973 goto cleanup;
3976 if (gfc_current_form == FORM_FREE)
3978 /* The following are valid, so we can't require a blank after the
3979 RETURN keyword:
3980 return+1
3981 return(1) */
3982 char c = gfc_peek_ascii_char ();
3983 if (ISALPHA (c) || ISDIGIT (c))
3984 return MATCH_NO;
3987 m = gfc_match (" %e%t", &e);
3988 if (m == MATCH_YES)
3989 goto done;
3990 if (m == MATCH_ERROR)
3991 goto cleanup;
3993 gfc_syntax_error (ST_RETURN);
3995 cleanup:
3996 gfc_free_expr (e);
3997 return MATCH_ERROR;
3999 done:
4000 gfc_enclosing_unit (&s);
4001 if (s == COMP_PROGRAM
4002 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4003 "main program at %C"))
4004 return MATCH_ERROR;
4006 new_st.op = EXEC_RETURN;
4007 new_st.expr1 = e;
4009 return MATCH_YES;
4013 /* Match the call of a type-bound procedure, if CALL%var has already been
4014 matched and var found to be a derived-type variable. */
4016 static match
4017 match_typebound_call (gfc_symtree* varst)
4019 gfc_expr* base;
4020 match m;
4022 base = gfc_get_expr ();
4023 base->expr_type = EXPR_VARIABLE;
4024 base->symtree = varst;
4025 base->where = gfc_current_locus;
4026 gfc_set_sym_referenced (varst->n.sym);
4028 m = gfc_match_varspec (base, 0, true, true);
4029 if (m == MATCH_NO)
4030 gfc_error ("Expected component reference at %C");
4031 if (m != MATCH_YES)
4033 gfc_free_expr (base);
4034 return MATCH_ERROR;
4037 if (gfc_match_eos () != MATCH_YES)
4039 gfc_error ("Junk after CALL at %C");
4040 gfc_free_expr (base);
4041 return MATCH_ERROR;
4044 if (base->expr_type == EXPR_COMPCALL)
4045 new_st.op = EXEC_COMPCALL;
4046 else if (base->expr_type == EXPR_PPC)
4047 new_st.op = EXEC_CALL_PPC;
4048 else
4050 gfc_error ("Expected type-bound procedure or procedure pointer component "
4051 "at %C");
4052 gfc_free_expr (base);
4053 return MATCH_ERROR;
4055 new_st.expr1 = base;
4057 return MATCH_YES;
4061 /* Match a CALL statement. The tricky part here are possible
4062 alternate return specifiers. We handle these by having all
4063 "subroutines" actually return an integer via a register that gives
4064 the return number. If the call specifies alternate returns, we
4065 generate code for a SELECT statement whose case clauses contain
4066 GOTOs to the various labels. */
4068 match
4069 gfc_match_call (void)
4071 char name[GFC_MAX_SYMBOL_LEN + 1];
4072 gfc_actual_arglist *a, *arglist;
4073 gfc_case *new_case;
4074 gfc_symbol *sym;
4075 gfc_symtree *st;
4076 gfc_code *c;
4077 match m;
4078 int i;
4080 arglist = NULL;
4082 m = gfc_match ("% %n", name);
4083 if (m == MATCH_NO)
4084 goto syntax;
4085 if (m != MATCH_YES)
4086 return m;
4088 if (gfc_get_ha_sym_tree (name, &st))
4089 return MATCH_ERROR;
4091 sym = st->n.sym;
4093 /* If this is a variable of derived-type, it probably starts a type-bound
4094 procedure call. */
4095 if ((sym->attr.flavor != FL_PROCEDURE
4096 || gfc_is_function_return_value (sym, gfc_current_ns))
4097 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
4098 return match_typebound_call (st);
4100 /* If it does not seem to be callable (include functions so that the
4101 right association is made. They are thrown out in resolution.)
4102 ... */
4103 if (!sym->attr.generic
4104 && !sym->attr.subroutine
4105 && !sym->attr.function)
4107 if (!(sym->attr.external && !sym->attr.referenced))
4109 /* ...create a symbol in this scope... */
4110 if (sym->ns != gfc_current_ns
4111 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
4112 return MATCH_ERROR;
4114 if (sym != st->n.sym)
4115 sym = st->n.sym;
4118 /* ...and then to try to make the symbol into a subroutine. */
4119 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
4120 return MATCH_ERROR;
4123 gfc_set_sym_referenced (sym);
4125 if (gfc_match_eos () != MATCH_YES)
4127 m = gfc_match_actual_arglist (1, &arglist);
4128 if (m == MATCH_NO)
4129 goto syntax;
4130 if (m == MATCH_ERROR)
4131 goto cleanup;
4133 if (gfc_match_eos () != MATCH_YES)
4134 goto syntax;
4137 /* If any alternate return labels were found, construct a SELECT
4138 statement that will jump to the right place. */
4140 i = 0;
4141 for (a = arglist; a; a = a->next)
4142 if (a->expr == NULL)
4144 i = 1;
4145 break;
4148 if (i)
4150 gfc_symtree *select_st;
4151 gfc_symbol *select_sym;
4152 char name[GFC_MAX_SYMBOL_LEN + 1];
4154 new_st.next = c = gfc_get_code (EXEC_SELECT);
4155 sprintf (name, "_result_%s", sym->name);
4156 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
4158 select_sym = select_st->n.sym;
4159 select_sym->ts.type = BT_INTEGER;
4160 select_sym->ts.kind = gfc_default_integer_kind;
4161 gfc_set_sym_referenced (select_sym);
4162 c->expr1 = gfc_get_expr ();
4163 c->expr1->expr_type = EXPR_VARIABLE;
4164 c->expr1->symtree = select_st;
4165 c->expr1->ts = select_sym->ts;
4166 c->expr1->where = gfc_current_locus;
4168 i = 0;
4169 for (a = arglist; a; a = a->next)
4171 if (a->expr != NULL)
4172 continue;
4174 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
4175 continue;
4177 i++;
4179 c->block = gfc_get_code (EXEC_SELECT);
4180 c = c->block;
4182 new_case = gfc_get_case ();
4183 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
4184 new_case->low = new_case->high;
4185 c->ext.block.case_list = new_case;
4187 c->next = gfc_get_code (EXEC_GOTO);
4188 c->next->label1 = a->label;
4192 new_st.op = EXEC_CALL;
4193 new_st.symtree = st;
4194 new_st.ext.actual = arglist;
4196 return MATCH_YES;
4198 syntax:
4199 gfc_syntax_error (ST_CALL);
4201 cleanup:
4202 gfc_free_actual_arglist (arglist);
4203 return MATCH_ERROR;
4207 /* Given a name, return a pointer to the common head structure,
4208 creating it if it does not exist. If FROM_MODULE is nonzero, we
4209 mangle the name so that it doesn't interfere with commons defined
4210 in the using namespace.
4211 TODO: Add to global symbol tree. */
4213 gfc_common_head *
4214 gfc_get_common (const char *name, int from_module)
4216 gfc_symtree *st;
4217 static int serial = 0;
4218 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
4220 if (from_module)
4222 /* A use associated common block is only needed to correctly layout
4223 the variables it contains. */
4224 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
4225 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
4227 else
4229 st = gfc_find_symtree (gfc_current_ns->common_root, name);
4231 if (st == NULL)
4232 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
4235 if (st->n.common == NULL)
4237 st->n.common = gfc_get_common_head ();
4238 st->n.common->where = gfc_current_locus;
4239 strcpy (st->n.common->name, name);
4242 return st->n.common;
4246 /* Match a common block name. */
4248 match match_common_name (char *name)
4250 match m;
4252 if (gfc_match_char ('/') == MATCH_NO)
4254 name[0] = '\0';
4255 return MATCH_YES;
4258 if (gfc_match_char ('/') == MATCH_YES)
4260 name[0] = '\0';
4261 return MATCH_YES;
4264 m = gfc_match_name (name);
4266 if (m == MATCH_ERROR)
4267 return MATCH_ERROR;
4268 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
4269 return MATCH_YES;
4271 gfc_error ("Syntax error in common block name at %C");
4272 return MATCH_ERROR;
4276 /* Match a COMMON statement. */
4278 match
4279 gfc_match_common (void)
4281 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
4282 char name[GFC_MAX_SYMBOL_LEN + 1];
4283 gfc_common_head *t;
4284 gfc_array_spec *as;
4285 gfc_equiv *e1, *e2;
4286 match m;
4288 old_blank_common = gfc_current_ns->blank_common.head;
4289 if (old_blank_common)
4291 while (old_blank_common->common_next)
4292 old_blank_common = old_blank_common->common_next;
4295 as = NULL;
4297 for (;;)
4299 m = match_common_name (name);
4300 if (m == MATCH_ERROR)
4301 goto cleanup;
4303 if (name[0] == '\0')
4305 t = &gfc_current_ns->blank_common;
4306 if (t->head == NULL)
4307 t->where = gfc_current_locus;
4309 else
4311 t = gfc_get_common (name, 0);
4313 head = &t->head;
4315 if (*head == NULL)
4316 tail = NULL;
4317 else
4319 tail = *head;
4320 while (tail->common_next)
4321 tail = tail->common_next;
4324 /* Grab the list of symbols. */
4325 for (;;)
4327 m = gfc_match_symbol (&sym, 0);
4328 if (m == MATCH_ERROR)
4329 goto cleanup;
4330 if (m == MATCH_NO)
4331 goto syntax;
4333 /* Store a ref to the common block for error checking. */
4334 sym->common_block = t;
4335 sym->common_block->refs++;
4337 /* See if we know the current common block is bind(c), and if
4338 so, then see if we can check if the symbol is (which it'll
4339 need to be). This can happen if the bind(c) attr stmt was
4340 applied to the common block, and the variable(s) already
4341 defined, before declaring the common block. */
4342 if (t->is_bind_c == 1)
4344 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
4346 /* If we find an error, just print it and continue,
4347 cause it's just semantic, and we can see if there
4348 are more errors. */
4349 gfc_error_now ("Variable %qs at %L in common block %qs "
4350 "at %C must be declared with a C "
4351 "interoperable kind since common block "
4352 "%qs is bind(c)",
4353 sym->name, &(sym->declared_at), t->name,
4354 t->name);
4357 if (sym->attr.is_bind_c == 1)
4358 gfc_error_now ("Variable %qs in common block %qs at %C can not "
4359 "be bind(c) since it is not global", sym->name,
4360 t->name);
4363 if (sym->attr.in_common)
4365 gfc_error ("Symbol %qs at %C is already in a COMMON block",
4366 sym->name);
4367 goto cleanup;
4370 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
4371 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
4373 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
4374 "%C can only be COMMON in BLOCK DATA",
4375 sym->name))
4376 goto cleanup;
4379 if (!gfc_add_in_common (&sym->attr, sym->name, NULL))
4380 goto cleanup;
4382 if (tail != NULL)
4383 tail->common_next = sym;
4384 else
4385 *head = sym;
4387 tail = sym;
4389 /* Deal with an optional array specification after the
4390 symbol name. */
4391 m = gfc_match_array_spec (&as, true, true);
4392 if (m == MATCH_ERROR)
4393 goto cleanup;
4395 if (m == MATCH_YES)
4397 if (as->type != AS_EXPLICIT)
4399 gfc_error ("Array specification for symbol %qs in COMMON "
4400 "at %C must be explicit", sym->name);
4401 goto cleanup;
4404 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
4405 goto cleanup;
4407 if (sym->attr.pointer)
4409 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
4410 "POINTER array", sym->name);
4411 goto cleanup;
4414 sym->as = as;
4415 as = NULL;
4419 sym->common_head = t;
4421 /* Check to see if the symbol is already in an equivalence group.
4422 If it is, set the other members as being in common. */
4423 if (sym->attr.in_equivalence)
4425 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
4427 for (e2 = e1; e2; e2 = e2->eq)
4428 if (e2->expr->symtree->n.sym == sym)
4429 goto equiv_found;
4431 continue;
4433 equiv_found:
4435 for (e2 = e1; e2; e2 = e2->eq)
4437 other = e2->expr->symtree->n.sym;
4438 if (other->common_head
4439 && other->common_head != sym->common_head)
4441 gfc_error ("Symbol %qs, in COMMON block %qs at "
4442 "%C is being indirectly equivalenced to "
4443 "another COMMON block %qs",
4444 sym->name, sym->common_head->name,
4445 other->common_head->name);
4446 goto cleanup;
4448 other->attr.in_common = 1;
4449 other->common_head = t;
4455 gfc_gobble_whitespace ();
4456 if (gfc_match_eos () == MATCH_YES)
4457 goto done;
4458 if (gfc_peek_ascii_char () == '/')
4459 break;
4460 if (gfc_match_char (',') != MATCH_YES)
4461 goto syntax;
4462 gfc_gobble_whitespace ();
4463 if (gfc_peek_ascii_char () == '/')
4464 break;
4468 done:
4469 return MATCH_YES;
4471 syntax:
4472 gfc_syntax_error (ST_COMMON);
4474 cleanup:
4475 gfc_free_array_spec (as);
4476 return MATCH_ERROR;
4480 /* Match a BLOCK DATA program unit. */
4482 match
4483 gfc_match_block_data (void)
4485 char name[GFC_MAX_SYMBOL_LEN + 1];
4486 gfc_symbol *sym;
4487 match m;
4489 if (gfc_match_eos () == MATCH_YES)
4491 gfc_new_block = NULL;
4492 return MATCH_YES;
4495 m = gfc_match ("% %n%t", name);
4496 if (m != MATCH_YES)
4497 return MATCH_ERROR;
4499 if (gfc_get_symbol (name, NULL, &sym))
4500 return MATCH_ERROR;
4502 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
4503 return MATCH_ERROR;
4505 gfc_new_block = sym;
4507 return MATCH_YES;
4511 /* Free a namelist structure. */
4513 void
4514 gfc_free_namelist (gfc_namelist *name)
4516 gfc_namelist *n;
4518 for (; name; name = n)
4520 n = name->next;
4521 free (name);
4526 /* Free an OpenMP namelist structure. */
4528 void
4529 gfc_free_omp_namelist (gfc_omp_namelist *name)
4531 gfc_omp_namelist *n;
4533 for (; name; name = n)
4535 gfc_free_expr (name->expr);
4536 if (name->udr)
4538 if (name->udr->combiner)
4539 gfc_free_statement (name->udr->combiner);
4540 if (name->udr->initializer)
4541 gfc_free_statement (name->udr->initializer);
4542 free (name->udr);
4544 n = name->next;
4545 free (name);
4550 /* Match a NAMELIST statement. */
4552 match
4553 gfc_match_namelist (void)
4555 gfc_symbol *group_name, *sym;
4556 gfc_namelist *nl;
4557 match m, m2;
4559 m = gfc_match (" / %s /", &group_name);
4560 if (m == MATCH_NO)
4561 goto syntax;
4562 if (m == MATCH_ERROR)
4563 goto error;
4565 for (;;)
4567 if (group_name->ts.type != BT_UNKNOWN)
4569 gfc_error ("Namelist group name %qs at %C already has a basic "
4570 "type of %s", group_name->name,
4571 gfc_typename (&group_name->ts));
4572 return MATCH_ERROR;
4575 if (group_name->attr.flavor == FL_NAMELIST
4576 && group_name->attr.use_assoc
4577 && !gfc_notify_std (GFC_STD_GNU, "Namelist group name %qs "
4578 "at %C already is USE associated and can"
4579 "not be respecified.", group_name->name))
4580 return MATCH_ERROR;
4582 if (group_name->attr.flavor != FL_NAMELIST
4583 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
4584 group_name->name, NULL))
4585 return MATCH_ERROR;
4587 for (;;)
4589 m = gfc_match_symbol (&sym, 1);
4590 if (m == MATCH_NO)
4591 goto syntax;
4592 if (m == MATCH_ERROR)
4593 goto error;
4595 if (sym->attr.in_namelist == 0
4596 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
4597 goto error;
4599 /* Use gfc_error_check here, rather than goto error, so that
4600 these are the only errors for the next two lines. */
4601 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
4603 gfc_error ("Assumed size array %qs in namelist %qs at "
4604 "%C is not allowed", sym->name, group_name->name);
4605 gfc_error_check ();
4608 nl = gfc_get_namelist ();
4609 nl->sym = sym;
4610 sym->refs++;
4612 if (group_name->namelist == NULL)
4613 group_name->namelist = group_name->namelist_tail = nl;
4614 else
4616 group_name->namelist_tail->next = nl;
4617 group_name->namelist_tail = nl;
4620 if (gfc_match_eos () == MATCH_YES)
4621 goto done;
4623 m = gfc_match_char (',');
4625 if (gfc_match_char ('/') == MATCH_YES)
4627 m2 = gfc_match (" %s /", &group_name);
4628 if (m2 == MATCH_YES)
4629 break;
4630 if (m2 == MATCH_ERROR)
4631 goto error;
4632 goto syntax;
4635 if (m != MATCH_YES)
4636 goto syntax;
4640 done:
4641 return MATCH_YES;
4643 syntax:
4644 gfc_syntax_error (ST_NAMELIST);
4646 error:
4647 return MATCH_ERROR;
4651 /* Match a MODULE statement. */
4653 match
4654 gfc_match_module (void)
4656 match m;
4658 m = gfc_match (" %s%t", &gfc_new_block);
4659 if (m != MATCH_YES)
4660 return m;
4662 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
4663 gfc_new_block->name, NULL))
4664 return MATCH_ERROR;
4666 return MATCH_YES;
4670 /* Free equivalence sets and lists. Recursively is the easiest way to
4671 do this. */
4673 void
4674 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
4676 if (eq == stop)
4677 return;
4679 gfc_free_equiv (eq->eq);
4680 gfc_free_equiv_until (eq->next, stop);
4681 gfc_free_expr (eq->expr);
4682 free (eq);
4686 void
4687 gfc_free_equiv (gfc_equiv *eq)
4689 gfc_free_equiv_until (eq, NULL);
4693 /* Match an EQUIVALENCE statement. */
4695 match
4696 gfc_match_equivalence (void)
4698 gfc_equiv *eq, *set, *tail;
4699 gfc_ref *ref;
4700 gfc_symbol *sym;
4701 match m;
4702 gfc_common_head *common_head = NULL;
4703 bool common_flag;
4704 int cnt;
4706 tail = NULL;
4708 for (;;)
4710 eq = gfc_get_equiv ();
4711 if (tail == NULL)
4712 tail = eq;
4714 eq->next = gfc_current_ns->equiv;
4715 gfc_current_ns->equiv = eq;
4717 if (gfc_match_char ('(') != MATCH_YES)
4718 goto syntax;
4720 set = eq;
4721 common_flag = FALSE;
4722 cnt = 0;
4724 for (;;)
4726 m = gfc_match_equiv_variable (&set->expr);
4727 if (m == MATCH_ERROR)
4728 goto cleanup;
4729 if (m == MATCH_NO)
4730 goto syntax;
4732 /* count the number of objects. */
4733 cnt++;
4735 if (gfc_match_char ('%') == MATCH_YES)
4737 gfc_error ("Derived type component %C is not a "
4738 "permitted EQUIVALENCE member");
4739 goto cleanup;
4742 for (ref = set->expr->ref; ref; ref = ref->next)
4743 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
4745 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
4746 "be an array section");
4747 goto cleanup;
4750 sym = set->expr->symtree->n.sym;
4752 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
4753 goto cleanup;
4755 if (sym->attr.in_common)
4757 common_flag = TRUE;
4758 common_head = sym->common_head;
4761 if (gfc_match_char (')') == MATCH_YES)
4762 break;
4764 if (gfc_match_char (',') != MATCH_YES)
4765 goto syntax;
4767 set->eq = gfc_get_equiv ();
4768 set = set->eq;
4771 if (cnt < 2)
4773 gfc_error ("EQUIVALENCE at %C requires two or more objects");
4774 goto cleanup;
4777 /* If one of the members of an equivalence is in common, then
4778 mark them all as being in common. Before doing this, check
4779 that members of the equivalence group are not in different
4780 common blocks. */
4781 if (common_flag)
4782 for (set = eq; set; set = set->eq)
4784 sym = set->expr->symtree->n.sym;
4785 if (sym->common_head && sym->common_head != common_head)
4787 gfc_error ("Attempt to indirectly overlap COMMON "
4788 "blocks %s and %s by EQUIVALENCE at %C",
4789 sym->common_head->name, common_head->name);
4790 goto cleanup;
4792 sym->attr.in_common = 1;
4793 sym->common_head = common_head;
4796 if (gfc_match_eos () == MATCH_YES)
4797 break;
4798 if (gfc_match_char (',') != MATCH_YES)
4800 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
4801 goto cleanup;
4805 return MATCH_YES;
4807 syntax:
4808 gfc_syntax_error (ST_EQUIVALENCE);
4810 cleanup:
4811 eq = tail->next;
4812 tail->next = NULL;
4814 gfc_free_equiv (gfc_current_ns->equiv);
4815 gfc_current_ns->equiv = eq;
4817 return MATCH_ERROR;
4821 /* Check that a statement function is not recursive. This is done by looking
4822 for the statement function symbol(sym) by looking recursively through its
4823 expression(e). If a reference to sym is found, true is returned.
4824 12.5.4 requires that any variable of function that is implicitly typed
4825 shall have that type confirmed by any subsequent type declaration. The
4826 implicit typing is conveniently done here. */
4827 static bool
4828 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
4830 static bool
4831 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
4834 if (e == NULL)
4835 return false;
4837 switch (e->expr_type)
4839 case EXPR_FUNCTION:
4840 if (e->symtree == NULL)
4841 return false;
4843 /* Check the name before testing for nested recursion! */
4844 if (sym->name == e->symtree->n.sym->name)
4845 return true;
4847 /* Catch recursion via other statement functions. */
4848 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
4849 && e->symtree->n.sym->value
4850 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
4851 return true;
4853 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
4854 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
4856 break;
4858 case EXPR_VARIABLE:
4859 if (e->symtree && sym->name == e->symtree->n.sym->name)
4860 return true;
4862 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
4863 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
4864 break;
4866 default:
4867 break;
4870 return false;
4874 static bool
4875 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
4877 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
4881 /* Match a statement function declaration. It is so easy to match
4882 non-statement function statements with a MATCH_ERROR as opposed to
4883 MATCH_NO that we suppress error message in most cases. */
4885 match
4886 gfc_match_st_function (void)
4888 gfc_error_buffer old_error;
4890 gfc_symbol *sym;
4891 gfc_expr *expr;
4892 match m;
4894 m = gfc_match_symbol (&sym, 0);
4895 if (m != MATCH_YES)
4896 return m;
4898 gfc_push_error (&old_error);
4900 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
4901 goto undo_error;
4903 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
4904 goto undo_error;
4906 m = gfc_match (" = %e%t", &expr);
4907 if (m == MATCH_NO)
4908 goto undo_error;
4910 gfc_free_error (&old_error);
4912 if (m == MATCH_ERROR)
4913 return m;
4915 if (recursive_stmt_fcn (expr, sym))
4917 gfc_error ("Statement function at %L is recursive", &expr->where);
4918 return MATCH_ERROR;
4921 sym->value = expr;
4923 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
4924 return MATCH_ERROR;
4926 return MATCH_YES;
4928 undo_error:
4929 gfc_pop_error (&old_error);
4930 return MATCH_NO;
4934 /***************** SELECT CASE subroutines ******************/
4936 /* Free a single case structure. */
4938 static void
4939 free_case (gfc_case *p)
4941 if (p->low == p->high)
4942 p->high = NULL;
4943 gfc_free_expr (p->low);
4944 gfc_free_expr (p->high);
4945 free (p);
4949 /* Free a list of case structures. */
4951 void
4952 gfc_free_case_list (gfc_case *p)
4954 gfc_case *q;
4956 for (; p; p = q)
4958 q = p->next;
4959 free_case (p);
4964 /* Match a single case selector. */
4966 static match
4967 match_case_selector (gfc_case **cp)
4969 gfc_case *c;
4970 match m;
4972 c = gfc_get_case ();
4973 c->where = gfc_current_locus;
4975 if (gfc_match_char (':') == MATCH_YES)
4977 m = gfc_match_init_expr (&c->high);
4978 if (m == MATCH_NO)
4979 goto need_expr;
4980 if (m == MATCH_ERROR)
4981 goto cleanup;
4983 else
4985 m = gfc_match_init_expr (&c->low);
4986 if (m == MATCH_ERROR)
4987 goto cleanup;
4988 if (m == MATCH_NO)
4989 goto need_expr;
4991 /* If we're not looking at a ':' now, make a range out of a single
4992 target. Else get the upper bound for the case range. */
4993 if (gfc_match_char (':') != MATCH_YES)
4994 c->high = c->low;
4995 else
4997 m = gfc_match_init_expr (&c->high);
4998 if (m == MATCH_ERROR)
4999 goto cleanup;
5000 /* MATCH_NO is fine. It's OK if nothing is there! */
5004 *cp = c;
5005 return MATCH_YES;
5007 need_expr:
5008 gfc_error ("Expected initialization expression in CASE at %C");
5010 cleanup:
5011 free_case (c);
5012 return MATCH_ERROR;
5016 /* Match the end of a case statement. */
5018 static match
5019 match_case_eos (void)
5021 char name[GFC_MAX_SYMBOL_LEN + 1];
5022 match m;
5024 if (gfc_match_eos () == MATCH_YES)
5025 return MATCH_YES;
5027 /* If the case construct doesn't have a case-construct-name, we
5028 should have matched the EOS. */
5029 if (!gfc_current_block ())
5030 return MATCH_NO;
5032 gfc_gobble_whitespace ();
5034 m = gfc_match_name (name);
5035 if (m != MATCH_YES)
5036 return m;
5038 if (strcmp (name, gfc_current_block ()->name) != 0)
5040 gfc_error ("Expected block name %qs of SELECT construct at %C",
5041 gfc_current_block ()->name);
5042 return MATCH_ERROR;
5045 return gfc_match_eos ();
5049 /* Match a SELECT statement. */
5051 match
5052 gfc_match_select (void)
5054 gfc_expr *expr;
5055 match m;
5057 m = gfc_match_label ();
5058 if (m == MATCH_ERROR)
5059 return m;
5061 m = gfc_match (" select case ( %e )%t", &expr);
5062 if (m != MATCH_YES)
5063 return m;
5065 new_st.op = EXEC_SELECT;
5066 new_st.expr1 = expr;
5068 return MATCH_YES;
5072 /* Transfer the selector typespec to the associate name. */
5074 static void
5075 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
5077 gfc_ref *ref;
5078 gfc_symbol *assoc_sym;
5080 assoc_sym = associate->symtree->n.sym;
5082 /* At this stage the expression rank and arrayspec dimensions have
5083 not been completely sorted out. We must get the expr2->rank
5084 right here, so that the correct class container is obtained. */
5085 ref = selector->ref;
5086 while (ref && ref->next)
5087 ref = ref->next;
5089 if (selector->ts.type == BT_CLASS && CLASS_DATA (selector)->as
5090 && ref && ref->type == REF_ARRAY)
5092 /* Ensure that the array reference type is set. We cannot use
5093 gfc_resolve_expr at this point, so the usable parts of
5094 resolve.c(resolve_array_ref) are employed to do it. */
5095 if (ref->u.ar.type == AR_UNKNOWN)
5097 ref->u.ar.type = AR_ELEMENT;
5098 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
5099 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5100 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
5101 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
5102 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
5104 ref->u.ar.type = AR_SECTION;
5105 break;
5109 if (ref->u.ar.type == AR_FULL)
5110 selector->rank = CLASS_DATA (selector)->as->rank;
5111 else if (ref->u.ar.type == AR_SECTION)
5112 selector->rank = ref->u.ar.dimen;
5113 else
5114 selector->rank = 0;
5117 if (selector->rank)
5119 assoc_sym->attr.dimension = 1;
5120 assoc_sym->as = gfc_get_array_spec ();
5121 assoc_sym->as->rank = selector->rank;
5122 assoc_sym->as->type = AS_DEFERRED;
5124 else
5125 assoc_sym->as = NULL;
5127 if (selector->ts.type == BT_CLASS)
5129 /* The correct class container has to be available. */
5130 assoc_sym->ts.type = BT_CLASS;
5131 assoc_sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
5132 assoc_sym->attr.pointer = 1;
5133 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
5138 /* Push the current selector onto the SELECT TYPE stack. */
5140 static void
5141 select_type_push (gfc_symbol *sel)
5143 gfc_select_type_stack *top = gfc_get_select_type_stack ();
5144 top->selector = sel;
5145 top->tmp = NULL;
5146 top->prev = select_type_stack;
5148 select_type_stack = top;
5152 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
5154 static gfc_symtree *
5155 select_intrinsic_set_tmp (gfc_typespec *ts)
5157 char name[GFC_MAX_SYMBOL_LEN];
5158 gfc_symtree *tmp;
5159 int charlen = 0;
5161 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
5162 return NULL;
5164 if (select_type_stack->selector->ts.type == BT_CLASS
5165 && !select_type_stack->selector->attr.class_ok)
5166 return NULL;
5168 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
5169 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
5170 charlen = mpz_get_si (ts->u.cl->length->value.integer);
5172 if (ts->type != BT_CHARACTER)
5173 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
5174 ts->kind);
5175 else
5176 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (ts->type),
5177 charlen, ts->kind);
5179 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5180 gfc_add_type (tmp->n.sym, ts, NULL);
5182 /* Copy across the array spec to the selector. */
5183 if (select_type_stack->selector->ts.type == BT_CLASS
5184 && (CLASS_DATA (select_type_stack->selector)->attr.dimension
5185 || CLASS_DATA (select_type_stack->selector)->attr.codimension))
5187 tmp->n.sym->attr.pointer = 1;
5188 tmp->n.sym->attr.dimension
5189 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5190 tmp->n.sym->attr.codimension
5191 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5192 tmp->n.sym->as
5193 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5196 gfc_set_sym_referenced (tmp->n.sym);
5197 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5198 tmp->n.sym->attr.select_type_temporary = 1;
5200 return tmp;
5204 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
5206 static void
5207 select_type_set_tmp (gfc_typespec *ts)
5209 char name[GFC_MAX_SYMBOL_LEN];
5210 gfc_symtree *tmp = NULL;
5212 if (!ts)
5214 select_type_stack->tmp = NULL;
5215 return;
5218 tmp = select_intrinsic_set_tmp (ts);
5220 if (tmp == NULL)
5222 if (!ts->u.derived)
5223 return;
5225 if (ts->type == BT_CLASS)
5226 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
5227 else
5228 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
5229 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5230 gfc_add_type (tmp->n.sym, ts, NULL);
5232 if (select_type_stack->selector->ts.type == BT_CLASS
5233 && select_type_stack->selector->attr.class_ok)
5235 tmp->n.sym->attr.pointer
5236 = CLASS_DATA (select_type_stack->selector)->attr.class_pointer;
5238 /* Copy across the array spec to the selector. */
5239 if (CLASS_DATA (select_type_stack->selector)->attr.dimension
5240 || CLASS_DATA (select_type_stack->selector)->attr.codimension)
5242 tmp->n.sym->attr.dimension
5243 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5244 tmp->n.sym->attr.codimension
5245 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5246 tmp->n.sym->as
5247 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5251 gfc_set_sym_referenced (tmp->n.sym);
5252 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5253 tmp->n.sym->attr.select_type_temporary = 1;
5255 if (ts->type == BT_CLASS)
5256 gfc_build_class_symbol (&tmp->n.sym->ts, &tmp->n.sym->attr,
5257 &tmp->n.sym->as);
5260 /* Add an association for it, so the rest of the parser knows it is
5261 an associate-name. The target will be set during resolution. */
5262 tmp->n.sym->assoc = gfc_get_association_list ();
5263 tmp->n.sym->assoc->dangling = 1;
5264 tmp->n.sym->assoc->st = tmp;
5266 select_type_stack->tmp = tmp;
5270 /* Match a SELECT TYPE statement. */
5272 match
5273 gfc_match_select_type (void)
5275 gfc_expr *expr1, *expr2 = NULL;
5276 match m;
5277 char name[GFC_MAX_SYMBOL_LEN];
5278 bool class_array;
5279 gfc_symbol *sym;
5281 m = gfc_match_label ();
5282 if (m == MATCH_ERROR)
5283 return m;
5285 m = gfc_match (" select type ( ");
5286 if (m != MATCH_YES)
5287 return m;
5289 m = gfc_match (" %n => %e", name, &expr2);
5290 if (m == MATCH_YES)
5292 expr1 = gfc_get_expr();
5293 expr1->expr_type = EXPR_VARIABLE;
5294 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
5296 m = MATCH_ERROR;
5297 goto cleanup;
5300 sym = expr1->symtree->n.sym;
5301 if (expr2->ts.type == BT_UNKNOWN)
5302 sym->attr.untyped = 1;
5303 else
5304 copy_ts_from_selector_to_associate (expr1, expr2);
5306 sym->attr.flavor = FL_VARIABLE;
5307 sym->attr.referenced = 1;
5308 sym->attr.class_ok = 1;
5310 else
5312 m = gfc_match (" %e ", &expr1);
5313 if (m != MATCH_YES)
5314 return m;
5317 m = gfc_match (" )%t");
5318 if (m != MATCH_YES)
5320 gfc_error ("parse error in SELECT TYPE statement at %C");
5321 goto cleanup;
5324 /* This ghastly expression seems to be needed to distinguish a CLASS
5325 array, which can have a reference, from other expressions that
5326 have references, such as derived type components, and are not
5327 allowed by the standard.
5328 TODO: see if it is sufficient to exclude component and substring
5329 references. */
5330 class_array = expr1->expr_type == EXPR_VARIABLE
5331 && expr1->ts.type == BT_CLASS
5332 && CLASS_DATA (expr1)
5333 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
5334 && (CLASS_DATA (expr1)->attr.dimension
5335 || CLASS_DATA (expr1)->attr.codimension)
5336 && expr1->ref
5337 && expr1->ref->type == REF_ARRAY
5338 && expr1->ref->next == NULL;
5340 /* Check for F03:C811. */
5341 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
5342 || (!class_array && expr1->ref != NULL)))
5344 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
5345 "use associate-name=>");
5346 m = MATCH_ERROR;
5347 goto cleanup;
5350 new_st.op = EXEC_SELECT_TYPE;
5351 new_st.expr1 = expr1;
5352 new_st.expr2 = expr2;
5353 new_st.ext.block.ns = gfc_current_ns;
5355 select_type_push (expr1->symtree->n.sym);
5357 return MATCH_YES;
5359 cleanup:
5360 gfc_free_expr (expr1);
5361 gfc_free_expr (expr2);
5362 return m;
5366 /* Match a CASE statement. */
5368 match
5369 gfc_match_case (void)
5371 gfc_case *c, *head, *tail;
5372 match m;
5374 head = tail = NULL;
5376 if (gfc_current_state () != COMP_SELECT)
5378 gfc_error ("Unexpected CASE statement at %C");
5379 return MATCH_ERROR;
5382 if (gfc_match ("% default") == MATCH_YES)
5384 m = match_case_eos ();
5385 if (m == MATCH_NO)
5386 goto syntax;
5387 if (m == MATCH_ERROR)
5388 goto cleanup;
5390 new_st.op = EXEC_SELECT;
5391 c = gfc_get_case ();
5392 c->where = gfc_current_locus;
5393 new_st.ext.block.case_list = c;
5394 return MATCH_YES;
5397 if (gfc_match_char ('(') != MATCH_YES)
5398 goto syntax;
5400 for (;;)
5402 if (match_case_selector (&c) == MATCH_ERROR)
5403 goto cleanup;
5405 if (head == NULL)
5406 head = c;
5407 else
5408 tail->next = c;
5410 tail = c;
5412 if (gfc_match_char (')') == MATCH_YES)
5413 break;
5414 if (gfc_match_char (',') != MATCH_YES)
5415 goto syntax;
5418 m = match_case_eos ();
5419 if (m == MATCH_NO)
5420 goto syntax;
5421 if (m == MATCH_ERROR)
5422 goto cleanup;
5424 new_st.op = EXEC_SELECT;
5425 new_st.ext.block.case_list = head;
5427 return MATCH_YES;
5429 syntax:
5430 gfc_error ("Syntax error in CASE specification at %C");
5432 cleanup:
5433 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
5434 return MATCH_ERROR;
5438 /* Match a TYPE IS statement. */
5440 match
5441 gfc_match_type_is (void)
5443 gfc_case *c = NULL;
5444 match m;
5446 if (gfc_current_state () != COMP_SELECT_TYPE)
5448 gfc_error ("Unexpected TYPE IS statement at %C");
5449 return MATCH_ERROR;
5452 if (gfc_match_char ('(') != MATCH_YES)
5453 goto syntax;
5455 c = gfc_get_case ();
5456 c->where = gfc_current_locus;
5458 m = gfc_match_type_spec (&c->ts);
5459 if (m == MATCH_NO)
5460 goto syntax;
5461 if (m == MATCH_ERROR)
5462 goto cleanup;
5464 if (gfc_match_char (')') != MATCH_YES)
5465 goto syntax;
5467 m = match_case_eos ();
5468 if (m == MATCH_NO)
5469 goto syntax;
5470 if (m == MATCH_ERROR)
5471 goto cleanup;
5473 new_st.op = EXEC_SELECT_TYPE;
5474 new_st.ext.block.case_list = c;
5476 if (c->ts.type == BT_DERIVED && c->ts.u.derived
5477 && (c->ts.u.derived->attr.sequence
5478 || c->ts.u.derived->attr.is_bind_c))
5480 gfc_error ("The type-spec shall not specify a sequence derived "
5481 "type or a type with the BIND attribute in SELECT "
5482 "TYPE at %C [F2003:C815]");
5483 return MATCH_ERROR;
5486 /* Create temporary variable. */
5487 select_type_set_tmp (&c->ts);
5489 return MATCH_YES;
5491 syntax:
5492 gfc_error ("Syntax error in TYPE IS specification at %C");
5494 cleanup:
5495 if (c != NULL)
5496 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
5497 return MATCH_ERROR;
5501 /* Match a CLASS IS or CLASS DEFAULT statement. */
5503 match
5504 gfc_match_class_is (void)
5506 gfc_case *c = NULL;
5507 match m;
5509 if (gfc_current_state () != COMP_SELECT_TYPE)
5510 return MATCH_NO;
5512 if (gfc_match ("% default") == MATCH_YES)
5514 m = match_case_eos ();
5515 if (m == MATCH_NO)
5516 goto syntax;
5517 if (m == MATCH_ERROR)
5518 goto cleanup;
5520 new_st.op = EXEC_SELECT_TYPE;
5521 c = gfc_get_case ();
5522 c->where = gfc_current_locus;
5523 c->ts.type = BT_UNKNOWN;
5524 new_st.ext.block.case_list = c;
5525 select_type_set_tmp (NULL);
5526 return MATCH_YES;
5529 m = gfc_match ("% is");
5530 if (m == MATCH_NO)
5531 goto syntax;
5532 if (m == MATCH_ERROR)
5533 goto cleanup;
5535 if (gfc_match_char ('(') != MATCH_YES)
5536 goto syntax;
5538 c = gfc_get_case ();
5539 c->where = gfc_current_locus;
5541 m = match_derived_type_spec (&c->ts);
5542 if (m == MATCH_NO)
5543 goto syntax;
5544 if (m == MATCH_ERROR)
5545 goto cleanup;
5547 if (c->ts.type == BT_DERIVED)
5548 c->ts.type = BT_CLASS;
5550 if (gfc_match_char (')') != MATCH_YES)
5551 goto syntax;
5553 m = match_case_eos ();
5554 if (m == MATCH_NO)
5555 goto syntax;
5556 if (m == MATCH_ERROR)
5557 goto cleanup;
5559 new_st.op = EXEC_SELECT_TYPE;
5560 new_st.ext.block.case_list = c;
5562 /* Create temporary variable. */
5563 select_type_set_tmp (&c->ts);
5565 return MATCH_YES;
5567 syntax:
5568 gfc_error ("Syntax error in CLASS IS specification at %C");
5570 cleanup:
5571 if (c != NULL)
5572 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
5573 return MATCH_ERROR;
5577 /********************* WHERE subroutines ********************/
5579 /* Match the rest of a simple WHERE statement that follows an IF statement.
5582 static match
5583 match_simple_where (void)
5585 gfc_expr *expr;
5586 gfc_code *c;
5587 match m;
5589 m = gfc_match (" ( %e )", &expr);
5590 if (m != MATCH_YES)
5591 return m;
5593 m = gfc_match_assignment ();
5594 if (m == MATCH_NO)
5595 goto syntax;
5596 if (m == MATCH_ERROR)
5597 goto cleanup;
5599 if (gfc_match_eos () != MATCH_YES)
5600 goto syntax;
5602 c = gfc_get_code (EXEC_WHERE);
5603 c->expr1 = expr;
5605 c->next = XCNEW (gfc_code);
5606 *c->next = new_st;
5607 gfc_clear_new_st ();
5609 new_st.op = EXEC_WHERE;
5610 new_st.block = c;
5612 return MATCH_YES;
5614 syntax:
5615 gfc_syntax_error (ST_WHERE);
5617 cleanup:
5618 gfc_free_expr (expr);
5619 return MATCH_ERROR;
5623 /* Match a WHERE statement. */
5625 match
5626 gfc_match_where (gfc_statement *st)
5628 gfc_expr *expr;
5629 match m0, m;
5630 gfc_code *c;
5632 m0 = gfc_match_label ();
5633 if (m0 == MATCH_ERROR)
5634 return m0;
5636 m = gfc_match (" where ( %e )", &expr);
5637 if (m != MATCH_YES)
5638 return m;
5640 if (gfc_match_eos () == MATCH_YES)
5642 *st = ST_WHERE_BLOCK;
5643 new_st.op = EXEC_WHERE;
5644 new_st.expr1 = expr;
5645 return MATCH_YES;
5648 m = gfc_match_assignment ();
5649 if (m == MATCH_NO)
5650 gfc_syntax_error (ST_WHERE);
5652 if (m != MATCH_YES)
5654 gfc_free_expr (expr);
5655 return MATCH_ERROR;
5658 /* We've got a simple WHERE statement. */
5659 *st = ST_WHERE;
5660 c = gfc_get_code (EXEC_WHERE);
5661 c->expr1 = expr;
5663 c->next = XCNEW (gfc_code);
5664 *c->next = new_st;
5665 gfc_clear_new_st ();
5667 new_st.op = EXEC_WHERE;
5668 new_st.block = c;
5670 return MATCH_YES;
5674 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
5675 new_st if successful. */
5677 match
5678 gfc_match_elsewhere (void)
5680 char name[GFC_MAX_SYMBOL_LEN + 1];
5681 gfc_expr *expr;
5682 match m;
5684 if (gfc_current_state () != COMP_WHERE)
5686 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
5687 return MATCH_ERROR;
5690 expr = NULL;
5692 if (gfc_match_char ('(') == MATCH_YES)
5694 m = gfc_match_expr (&expr);
5695 if (m == MATCH_NO)
5696 goto syntax;
5697 if (m == MATCH_ERROR)
5698 return MATCH_ERROR;
5700 if (gfc_match_char (')') != MATCH_YES)
5701 goto syntax;
5704 if (gfc_match_eos () != MATCH_YES)
5706 /* Only makes sense if we have a where-construct-name. */
5707 if (!gfc_current_block ())
5709 m = MATCH_ERROR;
5710 goto cleanup;
5712 /* Better be a name at this point. */
5713 m = gfc_match_name (name);
5714 if (m == MATCH_NO)
5715 goto syntax;
5716 if (m == MATCH_ERROR)
5717 goto cleanup;
5719 if (gfc_match_eos () != MATCH_YES)
5720 goto syntax;
5722 if (strcmp (name, gfc_current_block ()->name) != 0)
5724 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
5725 name, gfc_current_block ()->name);
5726 goto cleanup;
5730 new_st.op = EXEC_WHERE;
5731 new_st.expr1 = expr;
5732 return MATCH_YES;
5734 syntax:
5735 gfc_syntax_error (ST_ELSEWHERE);
5737 cleanup:
5738 gfc_free_expr (expr);
5739 return MATCH_ERROR;