2015-05-18 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / match.c
blob3e12483cc0fcabd0db02105c1aeec8b40f8c311c
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 "flags.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "parse.h"
28 #include "hash-set.h"
29 #include "machmode.h"
30 #include "vec.h"
31 #include "double-int.h"
32 #include "input.h"
33 #include "alias.h"
34 #include "symtab.h"
35 #include "wide-int.h"
36 #include "inchash.h"
37 #include "tree.h"
38 #include "stringpool.h"
40 int gfc_matching_ptr_assignment = 0;
41 int gfc_matching_procptr_assignment = 0;
42 bool gfc_matching_prefix = false;
44 /* Stack of SELECT TYPE statements. */
45 gfc_select_type_stack *select_type_stack = NULL;
47 /* For debugging and diagnostic purposes. Return the textual representation
48 of the intrinsic operator OP. */
49 const char *
50 gfc_op2string (gfc_intrinsic_op op)
52 switch (op)
54 case INTRINSIC_UPLUS:
55 case INTRINSIC_PLUS:
56 return "+";
58 case INTRINSIC_UMINUS:
59 case INTRINSIC_MINUS:
60 return "-";
62 case INTRINSIC_POWER:
63 return "**";
64 case INTRINSIC_CONCAT:
65 return "//";
66 case INTRINSIC_TIMES:
67 return "*";
68 case INTRINSIC_DIVIDE:
69 return "/";
71 case INTRINSIC_AND:
72 return ".and.";
73 case INTRINSIC_OR:
74 return ".or.";
75 case INTRINSIC_EQV:
76 return ".eqv.";
77 case INTRINSIC_NEQV:
78 return ".neqv.";
80 case INTRINSIC_EQ_OS:
81 return ".eq.";
82 case INTRINSIC_EQ:
83 return "==";
84 case INTRINSIC_NE_OS:
85 return ".ne.";
86 case INTRINSIC_NE:
87 return "/=";
88 case INTRINSIC_GE_OS:
89 return ".ge.";
90 case INTRINSIC_GE:
91 return ">=";
92 case INTRINSIC_LE_OS:
93 return ".le.";
94 case INTRINSIC_LE:
95 return "<=";
96 case INTRINSIC_LT_OS:
97 return ".lt.";
98 case INTRINSIC_LT:
99 return "<";
100 case INTRINSIC_GT_OS:
101 return ".gt.";
102 case INTRINSIC_GT:
103 return ">";
104 case INTRINSIC_NOT:
105 return ".not.";
107 case INTRINSIC_ASSIGN:
108 return "=";
110 case INTRINSIC_PARENTHESES:
111 return "parens";
113 case INTRINSIC_NONE:
114 return "none";
116 default:
117 break;
120 gfc_internal_error ("gfc_op2string(): Bad code");
121 /* Not reached. */
125 /******************** Generic matching subroutines ************************/
127 /* This function scans the current statement counting the opened and closed
128 parenthesis to make sure they are balanced. */
130 match
131 gfc_match_parens (void)
133 locus old_loc, where;
134 int count;
135 gfc_instring instring;
136 gfc_char_t c, quote;
138 old_loc = gfc_current_locus;
139 count = 0;
140 instring = NONSTRING;
141 quote = ' ';
143 for (;;)
145 c = gfc_next_char_literal (instring);
146 if (c == '\n')
147 break;
148 if (quote == ' ' && ((c == '\'') || (c == '"')))
150 quote = c;
151 instring = INSTRING_WARN;
152 continue;
154 if (quote != ' ' && c == quote)
156 quote = ' ';
157 instring = NONSTRING;
158 continue;
161 if (c == '(' && quote == ' ')
163 count++;
164 where = gfc_current_locus;
166 if (c == ')' && quote == ' ')
168 count--;
169 where = gfc_current_locus;
173 gfc_current_locus = old_loc;
175 if (count > 0)
177 gfc_error ("Missing %<)%> in statement at or before %L", &where);
178 return MATCH_ERROR;
180 if (count < 0)
182 gfc_error ("Missing %<(%> in statement at or before %L", &where);
183 return MATCH_ERROR;
186 return MATCH_YES;
190 /* See if the next character is a special character that has
191 escaped by a \ via the -fbackslash option. */
193 match
194 gfc_match_special_char (gfc_char_t *res)
196 int len, i;
197 gfc_char_t c, n;
198 match m;
200 m = MATCH_YES;
202 switch ((c = gfc_next_char_literal (INSTRING_WARN)))
204 case 'a':
205 *res = '\a';
206 break;
207 case 'b':
208 *res = '\b';
209 break;
210 case 't':
211 *res = '\t';
212 break;
213 case 'f':
214 *res = '\f';
215 break;
216 case 'n':
217 *res = '\n';
218 break;
219 case 'r':
220 *res = '\r';
221 break;
222 case 'v':
223 *res = '\v';
224 break;
225 case '\\':
226 *res = '\\';
227 break;
228 case '0':
229 *res = '\0';
230 break;
232 case 'x':
233 case 'u':
234 case 'U':
235 /* Hexadecimal form of wide characters. */
236 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
237 n = 0;
238 for (i = 0; i < len; i++)
240 char buf[2] = { '\0', '\0' };
242 c = gfc_next_char_literal (INSTRING_WARN);
243 if (!gfc_wide_fits_in_byte (c)
244 || !gfc_check_digit ((unsigned char) c, 16))
245 return MATCH_NO;
247 buf[0] = (unsigned char) c;
248 n = n << 4;
249 n += strtol (buf, NULL, 16);
251 *res = n;
252 break;
254 default:
255 /* Unknown backslash codes are simply not expanded. */
256 m = MATCH_NO;
257 break;
260 return m;
264 /* In free form, match at least one space. Always matches in fixed
265 form. */
267 match
268 gfc_match_space (void)
270 locus old_loc;
271 char c;
273 if (gfc_current_form == FORM_FIXED)
274 return MATCH_YES;
276 old_loc = gfc_current_locus;
278 c = gfc_next_ascii_char ();
279 if (!gfc_is_whitespace (c))
281 gfc_current_locus = old_loc;
282 return MATCH_NO;
285 gfc_gobble_whitespace ();
287 return MATCH_YES;
291 /* Match an end of statement. End of statement is optional
292 whitespace, followed by a ';' or '\n' or comment '!'. If a
293 semicolon is found, we continue to eat whitespace and semicolons. */
295 match
296 gfc_match_eos (void)
298 locus old_loc;
299 int flag;
300 char c;
302 flag = 0;
304 for (;;)
306 old_loc = gfc_current_locus;
307 gfc_gobble_whitespace ();
309 c = gfc_next_ascii_char ();
310 switch (c)
312 case '!':
315 c = gfc_next_ascii_char ();
317 while (c != '\n');
319 /* Fall through. */
321 case '\n':
322 return MATCH_YES;
324 case ';':
325 flag = 1;
326 continue;
329 break;
332 gfc_current_locus = old_loc;
333 return (flag) ? MATCH_YES : MATCH_NO;
337 /* Match a literal integer on the input, setting the value on
338 MATCH_YES. Literal ints occur in kind-parameters as well as
339 old-style character length specifications. If cnt is non-NULL it
340 will be set to the number of digits. */
342 match
343 gfc_match_small_literal_int (int *value, int *cnt)
345 locus old_loc;
346 char c;
347 int i, j;
349 old_loc = gfc_current_locus;
351 *value = -1;
352 gfc_gobble_whitespace ();
353 c = gfc_next_ascii_char ();
354 if (cnt)
355 *cnt = 0;
357 if (!ISDIGIT (c))
359 gfc_current_locus = old_loc;
360 return MATCH_NO;
363 i = c - '0';
364 j = 1;
366 for (;;)
368 old_loc = gfc_current_locus;
369 c = gfc_next_ascii_char ();
371 if (!ISDIGIT (c))
372 break;
374 i = 10 * i + c - '0';
375 j++;
377 if (i > 99999999)
379 gfc_error ("Integer too large at %C");
380 return MATCH_ERROR;
384 gfc_current_locus = old_loc;
386 *value = i;
387 if (cnt)
388 *cnt = j;
389 return MATCH_YES;
393 /* Match a small, constant integer expression, like in a kind
394 statement. On MATCH_YES, 'value' is set. */
396 match
397 gfc_match_small_int (int *value)
399 gfc_expr *expr;
400 const char *p;
401 match m;
402 int i;
404 m = gfc_match_expr (&expr);
405 if (m != MATCH_YES)
406 return m;
408 p = gfc_extract_int (expr, &i);
409 gfc_free_expr (expr);
411 if (p != NULL)
413 gfc_error (p);
414 m = MATCH_ERROR;
417 *value = i;
418 return m;
422 /* This function is the same as the gfc_match_small_int, except that
423 we're keeping the pointer to the expr. This function could just be
424 removed and the previously mentioned one modified, though all calls
425 to it would have to be modified then (and there were a number of
426 them). Return MATCH_ERROR if fail to extract the int; otherwise,
427 return the result of gfc_match_expr(). The expr (if any) that was
428 matched is returned in the parameter expr. */
430 match
431 gfc_match_small_int_expr (int *value, gfc_expr **expr)
433 const char *p;
434 match m;
435 int i;
437 m = gfc_match_expr (expr);
438 if (m != MATCH_YES)
439 return m;
441 p = gfc_extract_int (*expr, &i);
443 if (p != NULL)
445 gfc_error (p);
446 m = MATCH_ERROR;
449 *value = i;
450 return m;
454 /* Matches a statement label. Uses gfc_match_small_literal_int() to
455 do most of the work. */
457 match
458 gfc_match_st_label (gfc_st_label **label)
460 locus old_loc;
461 match m;
462 int i, cnt;
464 old_loc = gfc_current_locus;
466 m = gfc_match_small_literal_int (&i, &cnt);
467 if (m != MATCH_YES)
468 return m;
470 if (cnt > 5)
472 gfc_error ("Too many digits in statement label at %C");
473 goto cleanup;
476 if (i == 0)
478 gfc_error ("Statement label at %C is zero");
479 goto cleanup;
482 *label = gfc_get_st_label (i);
483 return MATCH_YES;
485 cleanup:
487 gfc_current_locus = old_loc;
488 return MATCH_ERROR;
492 /* Match and validate a label associated with a named IF, DO or SELECT
493 statement. If the symbol does not have the label attribute, we add
494 it. We also make sure the symbol does not refer to another
495 (active) block. A matched label is pointed to by gfc_new_block. */
497 match
498 gfc_match_label (void)
500 char name[GFC_MAX_SYMBOL_LEN + 1];
501 match m;
503 gfc_new_block = NULL;
505 m = gfc_match (" %n :", name);
506 if (m != MATCH_YES)
507 return m;
509 if (gfc_get_symbol (name, NULL, &gfc_new_block))
511 gfc_error ("Label name %qs at %C is ambiguous", name);
512 return MATCH_ERROR;
515 if (gfc_new_block->attr.flavor == FL_LABEL)
517 gfc_error ("Duplicate construct label %qs at %C", name);
518 return MATCH_ERROR;
521 if (!gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
522 gfc_new_block->name, NULL))
523 return MATCH_ERROR;
525 return MATCH_YES;
529 /* See if the current input looks like a name of some sort. Modifies
530 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
531 Note that options.c restricts max_identifier_length to not more
532 than GFC_MAX_SYMBOL_LEN. */
534 match
535 gfc_match_name (char *buffer)
537 locus old_loc;
538 int i;
539 char c;
541 old_loc = gfc_current_locus;
542 gfc_gobble_whitespace ();
544 c = gfc_next_ascii_char ();
545 if (!(ISALPHA (c) || (c == '_' && flag_allow_leading_underscore)))
547 if (!gfc_error_flag_test () && c != '(')
548 gfc_error ("Invalid character in name at %C");
549 gfc_current_locus = old_loc;
550 return MATCH_NO;
553 i = 0;
557 buffer[i++] = c;
559 if (i > gfc_option.max_identifier_length)
561 gfc_error ("Name at %C is too long");
562 return MATCH_ERROR;
565 old_loc = gfc_current_locus;
566 c = gfc_next_ascii_char ();
568 while (ISALNUM (c) || c == '_' || (flag_dollar_ok && c == '$'));
570 if (c == '$' && !flag_dollar_ok)
572 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
573 "allow it as an extension", &old_loc);
574 return MATCH_ERROR;
577 buffer[i] = '\0';
578 gfc_current_locus = old_loc;
580 return MATCH_YES;
584 /* Match a symbol on the input. Modifies the pointer to the symbol
585 pointer if successful. */
587 match
588 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
590 char buffer[GFC_MAX_SYMBOL_LEN + 1];
591 match m;
593 m = gfc_match_name (buffer);
594 if (m != MATCH_YES)
595 return m;
597 if (host_assoc)
598 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
599 ? MATCH_ERROR : MATCH_YES;
601 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
602 return MATCH_ERROR;
604 return MATCH_YES;
608 match
609 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
611 gfc_symtree *st;
612 match m;
614 m = gfc_match_sym_tree (&st, host_assoc);
616 if (m == MATCH_YES)
618 if (st)
619 *matched_symbol = st->n.sym;
620 else
621 *matched_symbol = NULL;
623 else
624 *matched_symbol = NULL;
625 return m;
629 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
630 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
631 in matchexp.c. */
633 match
634 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
636 locus orig_loc = gfc_current_locus;
637 char ch;
639 gfc_gobble_whitespace ();
640 ch = gfc_next_ascii_char ();
641 switch (ch)
643 case '+':
644 /* Matched "+". */
645 *result = INTRINSIC_PLUS;
646 return MATCH_YES;
648 case '-':
649 /* Matched "-". */
650 *result = INTRINSIC_MINUS;
651 return MATCH_YES;
653 case '=':
654 if (gfc_next_ascii_char () == '=')
656 /* Matched "==". */
657 *result = INTRINSIC_EQ;
658 return MATCH_YES;
660 break;
662 case '<':
663 if (gfc_peek_ascii_char () == '=')
665 /* Matched "<=". */
666 gfc_next_ascii_char ();
667 *result = INTRINSIC_LE;
668 return MATCH_YES;
670 /* Matched "<". */
671 *result = INTRINSIC_LT;
672 return MATCH_YES;
674 case '>':
675 if (gfc_peek_ascii_char () == '=')
677 /* Matched ">=". */
678 gfc_next_ascii_char ();
679 *result = INTRINSIC_GE;
680 return MATCH_YES;
682 /* Matched ">". */
683 *result = INTRINSIC_GT;
684 return MATCH_YES;
686 case '*':
687 if (gfc_peek_ascii_char () == '*')
689 /* Matched "**". */
690 gfc_next_ascii_char ();
691 *result = INTRINSIC_POWER;
692 return MATCH_YES;
694 /* Matched "*". */
695 *result = INTRINSIC_TIMES;
696 return MATCH_YES;
698 case '/':
699 ch = gfc_peek_ascii_char ();
700 if (ch == '=')
702 /* Matched "/=". */
703 gfc_next_ascii_char ();
704 *result = INTRINSIC_NE;
705 return MATCH_YES;
707 else if (ch == '/')
709 /* Matched "//". */
710 gfc_next_ascii_char ();
711 *result = INTRINSIC_CONCAT;
712 return MATCH_YES;
714 /* Matched "/". */
715 *result = INTRINSIC_DIVIDE;
716 return MATCH_YES;
718 case '.':
719 ch = gfc_next_ascii_char ();
720 switch (ch)
722 case 'a':
723 if (gfc_next_ascii_char () == 'n'
724 && gfc_next_ascii_char () == 'd'
725 && gfc_next_ascii_char () == '.')
727 /* Matched ".and.". */
728 *result = INTRINSIC_AND;
729 return MATCH_YES;
731 break;
733 case 'e':
734 if (gfc_next_ascii_char () == 'q')
736 ch = gfc_next_ascii_char ();
737 if (ch == '.')
739 /* Matched ".eq.". */
740 *result = INTRINSIC_EQ_OS;
741 return MATCH_YES;
743 else if (ch == 'v')
745 if (gfc_next_ascii_char () == '.')
747 /* Matched ".eqv.". */
748 *result = INTRINSIC_EQV;
749 return MATCH_YES;
753 break;
755 case 'g':
756 ch = gfc_next_ascii_char ();
757 if (ch == 'e')
759 if (gfc_next_ascii_char () == '.')
761 /* Matched ".ge.". */
762 *result = INTRINSIC_GE_OS;
763 return MATCH_YES;
766 else if (ch == 't')
768 if (gfc_next_ascii_char () == '.')
770 /* Matched ".gt.". */
771 *result = INTRINSIC_GT_OS;
772 return MATCH_YES;
775 break;
777 case 'l':
778 ch = gfc_next_ascii_char ();
779 if (ch == 'e')
781 if (gfc_next_ascii_char () == '.')
783 /* Matched ".le.". */
784 *result = INTRINSIC_LE_OS;
785 return MATCH_YES;
788 else if (ch == 't')
790 if (gfc_next_ascii_char () == '.')
792 /* Matched ".lt.". */
793 *result = INTRINSIC_LT_OS;
794 return MATCH_YES;
797 break;
799 case 'n':
800 ch = gfc_next_ascii_char ();
801 if (ch == 'e')
803 ch = gfc_next_ascii_char ();
804 if (ch == '.')
806 /* Matched ".ne.". */
807 *result = INTRINSIC_NE_OS;
808 return MATCH_YES;
810 else if (ch == 'q')
812 if (gfc_next_ascii_char () == 'v'
813 && gfc_next_ascii_char () == '.')
815 /* Matched ".neqv.". */
816 *result = INTRINSIC_NEQV;
817 return MATCH_YES;
821 else if (ch == 'o')
823 if (gfc_next_ascii_char () == 't'
824 && gfc_next_ascii_char () == '.')
826 /* Matched ".not.". */
827 *result = INTRINSIC_NOT;
828 return MATCH_YES;
831 break;
833 case 'o':
834 if (gfc_next_ascii_char () == 'r'
835 && gfc_next_ascii_char () == '.')
837 /* Matched ".or.". */
838 *result = INTRINSIC_OR;
839 return MATCH_YES;
841 break;
843 default:
844 break;
846 break;
848 default:
849 break;
852 gfc_current_locus = orig_loc;
853 return MATCH_NO;
857 /* Match a loop control phrase:
859 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
861 If the final integer expression is not present, a constant unity
862 expression is returned. We don't return MATCH_ERROR until after
863 the equals sign is seen. */
865 match
866 gfc_match_iterator (gfc_iterator *iter, int init_flag)
868 char name[GFC_MAX_SYMBOL_LEN + 1];
869 gfc_expr *var, *e1, *e2, *e3;
870 locus start;
871 match m;
873 e1 = e2 = e3 = NULL;
875 /* Match the start of an iterator without affecting the symbol table. */
877 start = gfc_current_locus;
878 m = gfc_match (" %n =", name);
879 gfc_current_locus = start;
881 if (m != MATCH_YES)
882 return MATCH_NO;
884 m = gfc_match_variable (&var, 0);
885 if (m != MATCH_YES)
886 return MATCH_NO;
888 /* F2008, C617 & C565. */
889 if (var->symtree->n.sym->attr.codimension)
891 gfc_error ("Loop variable at %C cannot be a coarray");
892 goto cleanup;
895 if (var->ref != NULL)
897 gfc_error ("Loop variable at %C cannot be a sub-component");
898 goto cleanup;
901 gfc_match_char ('=');
903 var->symtree->n.sym->attr.implied_index = 1;
905 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
906 if (m == MATCH_NO)
907 goto syntax;
908 if (m == MATCH_ERROR)
909 goto cleanup;
911 if (gfc_match_char (',') != MATCH_YES)
912 goto syntax;
914 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
915 if (m == MATCH_NO)
916 goto syntax;
917 if (m == MATCH_ERROR)
918 goto cleanup;
920 if (gfc_match_char (',') != MATCH_YES)
922 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
923 goto done;
926 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
927 if (m == MATCH_ERROR)
928 goto cleanup;
929 if (m == MATCH_NO)
931 gfc_error ("Expected a step value in iterator at %C");
932 goto cleanup;
935 done:
936 iter->var = var;
937 iter->start = e1;
938 iter->end = e2;
939 iter->step = e3;
940 return MATCH_YES;
942 syntax:
943 gfc_error ("Syntax error in iterator at %C");
945 cleanup:
946 gfc_free_expr (e1);
947 gfc_free_expr (e2);
948 gfc_free_expr (e3);
950 return MATCH_ERROR;
954 /* Tries to match the next non-whitespace character on the input.
955 This subroutine does not return MATCH_ERROR. */
957 match
958 gfc_match_char (char c)
960 locus where;
962 where = gfc_current_locus;
963 gfc_gobble_whitespace ();
965 if (gfc_next_ascii_char () == c)
966 return MATCH_YES;
968 gfc_current_locus = where;
969 return MATCH_NO;
973 /* General purpose matching subroutine. The target string is a
974 scanf-like format string in which spaces correspond to arbitrary
975 whitespace (including no whitespace), characters correspond to
976 themselves. The %-codes are:
978 %% Literal percent sign
979 %e Expression, pointer to a pointer is set
980 %s Symbol, pointer to the symbol is set
981 %n Name, character buffer is set to name
982 %t Matches end of statement.
983 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
984 %l Matches a statement label
985 %v Matches a variable expression (an lvalue)
986 % Matches a required space (in free form) and optional spaces. */
988 match
989 gfc_match (const char *target, ...)
991 gfc_st_label **label;
992 int matches, *ip;
993 locus old_loc;
994 va_list argp;
995 char c, *np;
996 match m, n;
997 void **vp;
998 const char *p;
1000 old_loc = gfc_current_locus;
1001 va_start (argp, target);
1002 m = MATCH_NO;
1003 matches = 0;
1004 p = target;
1006 loop:
1007 c = *p++;
1008 switch (c)
1010 case ' ':
1011 gfc_gobble_whitespace ();
1012 goto loop;
1013 case '\0':
1014 m = MATCH_YES;
1015 break;
1017 case '%':
1018 c = *p++;
1019 switch (c)
1021 case 'e':
1022 vp = va_arg (argp, void **);
1023 n = gfc_match_expr ((gfc_expr **) vp);
1024 if (n != MATCH_YES)
1026 m = n;
1027 goto not_yes;
1030 matches++;
1031 goto loop;
1033 case 'v':
1034 vp = va_arg (argp, void **);
1035 n = gfc_match_variable ((gfc_expr **) vp, 0);
1036 if (n != MATCH_YES)
1038 m = n;
1039 goto not_yes;
1042 matches++;
1043 goto loop;
1045 case 's':
1046 vp = va_arg (argp, void **);
1047 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1048 if (n != MATCH_YES)
1050 m = n;
1051 goto not_yes;
1054 matches++;
1055 goto loop;
1057 case 'n':
1058 np = va_arg (argp, char *);
1059 n = gfc_match_name (np);
1060 if (n != MATCH_YES)
1062 m = n;
1063 goto not_yes;
1066 matches++;
1067 goto loop;
1069 case 'l':
1070 label = va_arg (argp, gfc_st_label **);
1071 n = gfc_match_st_label (label);
1072 if (n != MATCH_YES)
1074 m = n;
1075 goto not_yes;
1078 matches++;
1079 goto loop;
1081 case 'o':
1082 ip = va_arg (argp, int *);
1083 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1084 if (n != MATCH_YES)
1086 m = n;
1087 goto not_yes;
1090 matches++;
1091 goto loop;
1093 case 't':
1094 if (gfc_match_eos () != MATCH_YES)
1096 m = MATCH_NO;
1097 goto not_yes;
1099 goto loop;
1101 case ' ':
1102 if (gfc_match_space () == MATCH_YES)
1103 goto loop;
1104 m = MATCH_NO;
1105 goto not_yes;
1107 case '%':
1108 break; /* Fall through to character matcher. */
1110 default:
1111 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1114 default:
1116 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1117 expect an upper case character here! */
1118 gcc_assert (TOLOWER (c) == c);
1120 if (c == gfc_next_ascii_char ())
1121 goto loop;
1122 break;
1125 not_yes:
1126 va_end (argp);
1128 if (m != MATCH_YES)
1130 /* Clean up after a failed match. */
1131 gfc_current_locus = old_loc;
1132 va_start (argp, target);
1134 p = target;
1135 for (; matches > 0; matches--)
1137 while (*p++ != '%');
1139 switch (*p++)
1141 case '%':
1142 matches++;
1143 break; /* Skip. */
1145 /* Matches that don't have to be undone */
1146 case 'o':
1147 case 'l':
1148 case 'n':
1149 case 's':
1150 (void) va_arg (argp, void **);
1151 break;
1153 case 'e':
1154 case 'v':
1155 vp = va_arg (argp, void **);
1156 gfc_free_expr ((struct gfc_expr *)*vp);
1157 *vp = NULL;
1158 break;
1162 va_end (argp);
1165 return m;
1169 /*********************** Statement level matching **********************/
1171 /* Matches the start of a program unit, which is the program keyword
1172 followed by an obligatory symbol. */
1174 match
1175 gfc_match_program (void)
1177 gfc_symbol *sym;
1178 match m;
1180 m = gfc_match ("% %s%t", &sym);
1182 if (m == MATCH_NO)
1184 gfc_error ("Invalid form of PROGRAM statement at %C");
1185 m = MATCH_ERROR;
1188 if (m == MATCH_ERROR)
1189 return m;
1191 if (!gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL))
1192 return MATCH_ERROR;
1194 gfc_new_block = sym;
1196 return MATCH_YES;
1200 /* Match a simple assignment statement. */
1202 match
1203 gfc_match_assignment (void)
1205 gfc_expr *lvalue, *rvalue;
1206 locus old_loc;
1207 match m;
1209 old_loc = gfc_current_locus;
1211 lvalue = NULL;
1212 m = gfc_match (" %v =", &lvalue);
1213 if (m != MATCH_YES)
1215 gfc_current_locus = old_loc;
1216 gfc_free_expr (lvalue);
1217 return MATCH_NO;
1220 rvalue = NULL;
1221 m = gfc_match (" %e%t", &rvalue);
1222 if (m != MATCH_YES)
1224 gfc_current_locus = old_loc;
1225 gfc_free_expr (lvalue);
1226 gfc_free_expr (rvalue);
1227 return m;
1230 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1232 new_st.op = EXEC_ASSIGN;
1233 new_st.expr1 = lvalue;
1234 new_st.expr2 = rvalue;
1236 gfc_check_do_variable (lvalue->symtree);
1238 return MATCH_YES;
1242 /* Match a pointer assignment statement. */
1244 match
1245 gfc_match_pointer_assignment (void)
1247 gfc_expr *lvalue, *rvalue;
1248 locus old_loc;
1249 match m;
1251 old_loc = gfc_current_locus;
1253 lvalue = rvalue = NULL;
1254 gfc_matching_ptr_assignment = 0;
1255 gfc_matching_procptr_assignment = 0;
1257 m = gfc_match (" %v =>", &lvalue);
1258 if (m != MATCH_YES)
1260 m = MATCH_NO;
1261 goto cleanup;
1264 if (lvalue->symtree->n.sym->attr.proc_pointer
1265 || gfc_is_proc_ptr_comp (lvalue))
1266 gfc_matching_procptr_assignment = 1;
1267 else
1268 gfc_matching_ptr_assignment = 1;
1270 m = gfc_match (" %e%t", &rvalue);
1271 gfc_matching_ptr_assignment = 0;
1272 gfc_matching_procptr_assignment = 0;
1273 if (m != MATCH_YES)
1274 goto cleanup;
1276 new_st.op = EXEC_POINTER_ASSIGN;
1277 new_st.expr1 = lvalue;
1278 new_st.expr2 = rvalue;
1280 return MATCH_YES;
1282 cleanup:
1283 gfc_current_locus = old_loc;
1284 gfc_free_expr (lvalue);
1285 gfc_free_expr (rvalue);
1286 return m;
1290 /* We try to match an easy arithmetic IF statement. This only happens
1291 when just after having encountered a simple IF statement. This code
1292 is really duplicate with parts of the gfc_match_if code, but this is
1293 *much* easier. */
1295 static match
1296 match_arithmetic_if (void)
1298 gfc_st_label *l1, *l2, *l3;
1299 gfc_expr *expr;
1300 match m;
1302 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1303 if (m != MATCH_YES)
1304 return m;
1306 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1307 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1308 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1310 gfc_free_expr (expr);
1311 return MATCH_ERROR;
1314 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
1315 return MATCH_ERROR;
1317 new_st.op = EXEC_ARITHMETIC_IF;
1318 new_st.expr1 = expr;
1319 new_st.label1 = l1;
1320 new_st.label2 = l2;
1321 new_st.label3 = l3;
1323 return MATCH_YES;
1327 /* The IF statement is a bit of a pain. First of all, there are three
1328 forms of it, the simple IF, the IF that starts a block and the
1329 arithmetic IF.
1331 There is a problem with the simple IF and that is the fact that we
1332 only have a single level of undo information on symbols. What this
1333 means is for a simple IF, we must re-match the whole IF statement
1334 multiple times in order to guarantee that the symbol table ends up
1335 in the proper state. */
1337 static match match_simple_forall (void);
1338 static match match_simple_where (void);
1340 match
1341 gfc_match_if (gfc_statement *if_type)
1343 gfc_expr *expr;
1344 gfc_st_label *l1, *l2, *l3;
1345 locus old_loc, old_loc2;
1346 gfc_code *p;
1347 match m, n;
1349 n = gfc_match_label ();
1350 if (n == MATCH_ERROR)
1351 return n;
1353 old_loc = gfc_current_locus;
1355 m = gfc_match (" if ( %e", &expr);
1356 if (m != MATCH_YES)
1357 return m;
1359 old_loc2 = gfc_current_locus;
1360 gfc_current_locus = old_loc;
1362 if (gfc_match_parens () == MATCH_ERROR)
1363 return MATCH_ERROR;
1365 gfc_current_locus = old_loc2;
1367 if (gfc_match_char (')') != MATCH_YES)
1369 gfc_error ("Syntax error in IF-expression at %C");
1370 gfc_free_expr (expr);
1371 return MATCH_ERROR;
1374 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1376 if (m == MATCH_YES)
1378 if (n == MATCH_YES)
1380 gfc_error ("Block label not appropriate for arithmetic IF "
1381 "statement at %C");
1382 gfc_free_expr (expr);
1383 return MATCH_ERROR;
1386 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1387 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1388 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1390 gfc_free_expr (expr);
1391 return MATCH_ERROR;
1394 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
1395 return MATCH_ERROR;
1397 new_st.op = EXEC_ARITHMETIC_IF;
1398 new_st.expr1 = expr;
1399 new_st.label1 = l1;
1400 new_st.label2 = l2;
1401 new_st.label3 = l3;
1403 *if_type = ST_ARITHMETIC_IF;
1404 return MATCH_YES;
1407 if (gfc_match (" then%t") == MATCH_YES)
1409 new_st.op = EXEC_IF;
1410 new_st.expr1 = expr;
1411 *if_type = ST_IF_BLOCK;
1412 return MATCH_YES;
1415 if (n == MATCH_YES)
1417 gfc_error ("Block label is not appropriate for IF statement at %C");
1418 gfc_free_expr (expr);
1419 return MATCH_ERROR;
1422 /* At this point the only thing left is a simple IF statement. At
1423 this point, n has to be MATCH_NO, so we don't have to worry about
1424 re-matching a block label. From what we've got so far, try
1425 matching an assignment. */
1427 *if_type = ST_SIMPLE_IF;
1429 m = gfc_match_assignment ();
1430 if (m == MATCH_YES)
1431 goto got_match;
1433 gfc_free_expr (expr);
1434 gfc_undo_symbols ();
1435 gfc_current_locus = old_loc;
1437 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1438 assignment was found. For MATCH_NO, continue to call the various
1439 matchers. */
1440 if (m == MATCH_ERROR)
1441 return MATCH_ERROR;
1443 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1445 m = gfc_match_pointer_assignment ();
1446 if (m == MATCH_YES)
1447 goto got_match;
1449 gfc_free_expr (expr);
1450 gfc_undo_symbols ();
1451 gfc_current_locus = old_loc;
1453 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1455 /* Look at the next keyword to see which matcher to call. Matching
1456 the keyword doesn't affect the symbol table, so we don't have to
1457 restore between tries. */
1459 #define match(string, subr, statement) \
1460 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1462 gfc_clear_error ();
1464 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1465 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1466 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1467 match ("call", gfc_match_call, ST_CALL)
1468 match ("close", gfc_match_close, ST_CLOSE)
1469 match ("continue", gfc_match_continue, ST_CONTINUE)
1470 match ("cycle", gfc_match_cycle, ST_CYCLE)
1471 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1472 match ("end file", gfc_match_endfile, ST_END_FILE)
1473 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1474 match ("exit", gfc_match_exit, ST_EXIT)
1475 match ("flush", gfc_match_flush, ST_FLUSH)
1476 match ("forall", match_simple_forall, ST_FORALL)
1477 match ("go to", gfc_match_goto, ST_GOTO)
1478 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1479 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1480 match ("lock", gfc_match_lock, ST_LOCK)
1481 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1482 match ("open", gfc_match_open, ST_OPEN)
1483 match ("pause", gfc_match_pause, ST_NONE)
1484 match ("print", gfc_match_print, ST_WRITE)
1485 match ("read", gfc_match_read, ST_READ)
1486 match ("return", gfc_match_return, ST_RETURN)
1487 match ("rewind", gfc_match_rewind, ST_REWIND)
1488 match ("stop", gfc_match_stop, ST_STOP)
1489 match ("wait", gfc_match_wait, ST_WAIT)
1490 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1491 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1492 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1493 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1494 match ("where", match_simple_where, ST_WHERE)
1495 match ("write", gfc_match_write, ST_WRITE)
1497 /* The gfc_match_assignment() above may have returned a MATCH_NO
1498 where the assignment was to a named constant. Check that
1499 special case here. */
1500 m = gfc_match_assignment ();
1501 if (m == MATCH_NO)
1503 gfc_error ("Cannot assign to a named constant at %C");
1504 gfc_free_expr (expr);
1505 gfc_undo_symbols ();
1506 gfc_current_locus = old_loc;
1507 return MATCH_ERROR;
1510 /* All else has failed, so give up. See if any of the matchers has
1511 stored an error message of some sort. */
1512 if (!gfc_error_check ())
1513 gfc_error ("Unclassifiable statement in IF-clause at %C");
1515 gfc_free_expr (expr);
1516 return MATCH_ERROR;
1518 got_match:
1519 if (m == MATCH_NO)
1520 gfc_error ("Syntax error in IF-clause at %C");
1521 if (m != MATCH_YES)
1523 gfc_free_expr (expr);
1524 return MATCH_ERROR;
1527 /* At this point, we've matched the single IF and the action clause
1528 is in new_st. Rearrange things so that the IF statement appears
1529 in new_st. */
1531 p = gfc_get_code (EXEC_IF);
1532 p->next = XCNEW (gfc_code);
1533 *p->next = new_st;
1534 p->next->loc = gfc_current_locus;
1536 p->expr1 = expr;
1538 gfc_clear_new_st ();
1540 new_st.op = EXEC_IF;
1541 new_st.block = p;
1543 return MATCH_YES;
1546 #undef match
1549 /* Match an ELSE statement. */
1551 match
1552 gfc_match_else (void)
1554 char name[GFC_MAX_SYMBOL_LEN + 1];
1556 if (gfc_match_eos () == MATCH_YES)
1557 return MATCH_YES;
1559 if (gfc_match_name (name) != MATCH_YES
1560 || gfc_current_block () == NULL
1561 || gfc_match_eos () != MATCH_YES)
1563 gfc_error ("Unexpected junk after ELSE statement at %C");
1564 return MATCH_ERROR;
1567 if (strcmp (name, gfc_current_block ()->name) != 0)
1569 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1570 name, gfc_current_block ()->name);
1571 return MATCH_ERROR;
1574 return MATCH_YES;
1578 /* Match an ELSE IF statement. */
1580 match
1581 gfc_match_elseif (void)
1583 char name[GFC_MAX_SYMBOL_LEN + 1];
1584 gfc_expr *expr;
1585 match m;
1587 m = gfc_match (" ( %e ) then", &expr);
1588 if (m != MATCH_YES)
1589 return m;
1591 if (gfc_match_eos () == MATCH_YES)
1592 goto done;
1594 if (gfc_match_name (name) != MATCH_YES
1595 || gfc_current_block () == NULL
1596 || gfc_match_eos () != MATCH_YES)
1598 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1599 goto cleanup;
1602 if (strcmp (name, gfc_current_block ()->name) != 0)
1604 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1605 name, gfc_current_block ()->name);
1606 goto cleanup;
1609 done:
1610 new_st.op = EXEC_IF;
1611 new_st.expr1 = expr;
1612 return MATCH_YES;
1614 cleanup:
1615 gfc_free_expr (expr);
1616 return MATCH_ERROR;
1620 /* Free a gfc_iterator structure. */
1622 void
1623 gfc_free_iterator (gfc_iterator *iter, int flag)
1626 if (iter == NULL)
1627 return;
1629 gfc_free_expr (iter->var);
1630 gfc_free_expr (iter->start);
1631 gfc_free_expr (iter->end);
1632 gfc_free_expr (iter->step);
1634 if (flag)
1635 free (iter);
1639 /* Match a CRITICAL statement. */
1640 match
1641 gfc_match_critical (void)
1643 gfc_st_label *label = NULL;
1645 if (gfc_match_label () == MATCH_ERROR)
1646 return MATCH_ERROR;
1648 if (gfc_match (" critical") != MATCH_YES)
1649 return MATCH_NO;
1651 if (gfc_match_st_label (&label) == MATCH_ERROR)
1652 return MATCH_ERROR;
1654 if (gfc_match_eos () != MATCH_YES)
1656 gfc_syntax_error (ST_CRITICAL);
1657 return MATCH_ERROR;
1660 if (gfc_pure (NULL))
1662 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1663 return MATCH_ERROR;
1666 if (gfc_find_state (COMP_DO_CONCURRENT))
1668 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1669 "block");
1670 return MATCH_ERROR;
1673 gfc_unset_implicit_pure (NULL);
1675 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
1676 return MATCH_ERROR;
1678 if (flag_coarray == GFC_FCOARRAY_NONE)
1680 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1681 "enable");
1682 return MATCH_ERROR;
1685 if (gfc_find_state (COMP_CRITICAL))
1687 gfc_error ("Nested CRITICAL block at %C");
1688 return MATCH_ERROR;
1691 new_st.op = EXEC_CRITICAL;
1693 if (label != NULL
1694 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
1695 return MATCH_ERROR;
1697 return MATCH_YES;
1701 /* Match a BLOCK statement. */
1703 match
1704 gfc_match_block (void)
1706 match m;
1708 if (gfc_match_label () == MATCH_ERROR)
1709 return MATCH_ERROR;
1711 if (gfc_match (" block") != MATCH_YES)
1712 return MATCH_NO;
1714 /* For this to be a correct BLOCK statement, the line must end now. */
1715 m = gfc_match_eos ();
1716 if (m == MATCH_ERROR)
1717 return MATCH_ERROR;
1718 if (m == MATCH_NO)
1719 return MATCH_NO;
1721 return MATCH_YES;
1725 /* Match an ASSOCIATE statement. */
1727 match
1728 gfc_match_associate (void)
1730 if (gfc_match_label () == MATCH_ERROR)
1731 return MATCH_ERROR;
1733 if (gfc_match (" associate") != MATCH_YES)
1734 return MATCH_NO;
1736 /* Match the association list. */
1737 if (gfc_match_char ('(') != MATCH_YES)
1739 gfc_error ("Expected association list at %C");
1740 return MATCH_ERROR;
1742 new_st.ext.block.assoc = NULL;
1743 while (true)
1745 gfc_association_list* newAssoc = gfc_get_association_list ();
1746 gfc_association_list* a;
1748 /* Match the next association. */
1749 if (gfc_match (" %n => %e", newAssoc->name, &newAssoc->target)
1750 != MATCH_YES)
1752 gfc_error ("Expected association at %C");
1753 goto assocListError;
1755 newAssoc->where = gfc_current_locus;
1757 /* Check that the current name is not yet in the list. */
1758 for (a = new_st.ext.block.assoc; a; a = a->next)
1759 if (!strcmp (a->name, newAssoc->name))
1761 gfc_error ("Duplicate name %qs in association at %C",
1762 newAssoc->name);
1763 goto assocListError;
1766 /* The target expression must not be coindexed. */
1767 if (gfc_is_coindexed (newAssoc->target))
1769 gfc_error ("Association target at %C must not be coindexed");
1770 goto assocListError;
1773 /* The `variable' field is left blank for now; because the target is not
1774 yet resolved, we can't use gfc_has_vector_subscript to determine it
1775 for now. This is set during resolution. */
1777 /* Put it into the list. */
1778 newAssoc->next = new_st.ext.block.assoc;
1779 new_st.ext.block.assoc = newAssoc;
1781 /* Try next one or end if closing parenthesis is found. */
1782 gfc_gobble_whitespace ();
1783 if (gfc_peek_char () == ')')
1784 break;
1785 if (gfc_match_char (',') != MATCH_YES)
1787 gfc_error ("Expected %<)%> or %<,%> at %C");
1788 return MATCH_ERROR;
1791 continue;
1793 assocListError:
1794 free (newAssoc);
1795 goto error;
1797 if (gfc_match_char (')') != MATCH_YES)
1799 /* This should never happen as we peek above. */
1800 gcc_unreachable ();
1803 if (gfc_match_eos () != MATCH_YES)
1805 gfc_error ("Junk after ASSOCIATE statement at %C");
1806 goto error;
1809 return MATCH_YES;
1811 error:
1812 gfc_free_association_list (new_st.ext.block.assoc);
1813 return MATCH_ERROR;
1817 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1818 an accessible derived type. */
1820 static match
1821 match_derived_type_spec (gfc_typespec *ts)
1823 char name[GFC_MAX_SYMBOL_LEN + 1];
1824 locus old_locus;
1825 gfc_symbol *derived;
1827 old_locus = gfc_current_locus;
1829 if (gfc_match ("%n", name) != MATCH_YES)
1831 gfc_current_locus = old_locus;
1832 return MATCH_NO;
1835 gfc_find_symbol (name, NULL, 1, &derived);
1837 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
1838 derived = gfc_find_dt_in_generic (derived);
1840 if (derived && derived->attr.flavor == FL_DERIVED)
1842 ts->type = BT_DERIVED;
1843 ts->u.derived = derived;
1844 return MATCH_YES;
1847 gfc_current_locus = old_locus;
1848 return MATCH_NO;
1852 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
1853 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
1854 It only includes the intrinsic types from the Fortran 2003 standard
1855 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
1856 the implicit_flag is not needed, so it was removed. Derived types are
1857 identified by their name alone. */
1859 match
1860 gfc_match_type_spec (gfc_typespec *ts)
1862 match m;
1863 locus old_locus;
1865 gfc_clear_ts (ts);
1866 gfc_gobble_whitespace ();
1867 old_locus = gfc_current_locus;
1869 if (match_derived_type_spec (ts) == MATCH_YES)
1871 /* Enforce F03:C401. */
1872 if (ts->u.derived->attr.abstract)
1874 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
1875 ts->u.derived->name, &old_locus);
1876 return MATCH_ERROR;
1878 return MATCH_YES;
1881 if (gfc_match ("integer") == MATCH_YES)
1883 ts->type = BT_INTEGER;
1884 ts->kind = gfc_default_integer_kind;
1885 goto kind_selector;
1888 if (gfc_match ("real") == MATCH_YES)
1890 ts->type = BT_REAL;
1891 ts->kind = gfc_default_real_kind;
1892 goto kind_selector;
1895 if (gfc_match ("double precision") == MATCH_YES)
1897 ts->type = BT_REAL;
1898 ts->kind = gfc_default_double_kind;
1899 return MATCH_YES;
1902 if (gfc_match ("complex") == MATCH_YES)
1904 ts->type = BT_COMPLEX;
1905 ts->kind = gfc_default_complex_kind;
1906 goto kind_selector;
1909 if (gfc_match ("character") == MATCH_YES)
1911 ts->type = BT_CHARACTER;
1913 m = gfc_match_char_spec (ts);
1915 if (m == MATCH_NO)
1916 m = MATCH_YES;
1918 return m;
1921 if (gfc_match ("logical") == MATCH_YES)
1923 ts->type = BT_LOGICAL;
1924 ts->kind = gfc_default_logical_kind;
1925 goto kind_selector;
1928 /* If a type is not matched, simply return MATCH_NO. */
1929 gfc_current_locus = old_locus;
1930 return MATCH_NO;
1932 kind_selector:
1934 gfc_gobble_whitespace ();
1935 if (gfc_peek_ascii_char () == '*')
1937 gfc_error ("Invalid type-spec at %C");
1938 return MATCH_ERROR;
1941 m = gfc_match_kind_spec (ts, false);
1943 if (m == MATCH_NO)
1944 m = MATCH_YES; /* No kind specifier found. */
1946 return m;
1950 /******************** FORALL subroutines ********************/
1952 /* Free a list of FORALL iterators. */
1954 void
1955 gfc_free_forall_iterator (gfc_forall_iterator *iter)
1957 gfc_forall_iterator *next;
1959 while (iter)
1961 next = iter->next;
1962 gfc_free_expr (iter->var);
1963 gfc_free_expr (iter->start);
1964 gfc_free_expr (iter->end);
1965 gfc_free_expr (iter->stride);
1966 free (iter);
1967 iter = next;
1972 /* Match an iterator as part of a FORALL statement. The format is:
1974 <var> = <start>:<end>[:<stride>]
1976 On MATCH_NO, the caller tests for the possibility that there is a
1977 scalar mask expression. */
1979 static match
1980 match_forall_iterator (gfc_forall_iterator **result)
1982 gfc_forall_iterator *iter;
1983 locus where;
1984 match m;
1986 where = gfc_current_locus;
1987 iter = XCNEW (gfc_forall_iterator);
1989 m = gfc_match_expr (&iter->var);
1990 if (m != MATCH_YES)
1991 goto cleanup;
1993 if (gfc_match_char ('=') != MATCH_YES
1994 || iter->var->expr_type != EXPR_VARIABLE)
1996 m = MATCH_NO;
1997 goto cleanup;
2000 m = gfc_match_expr (&iter->start);
2001 if (m != MATCH_YES)
2002 goto cleanup;
2004 if (gfc_match_char (':') != MATCH_YES)
2005 goto syntax;
2007 m = gfc_match_expr (&iter->end);
2008 if (m == MATCH_NO)
2009 goto syntax;
2010 if (m == MATCH_ERROR)
2011 goto cleanup;
2013 if (gfc_match_char (':') == MATCH_NO)
2014 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2015 else
2017 m = gfc_match_expr (&iter->stride);
2018 if (m == MATCH_NO)
2019 goto syntax;
2020 if (m == MATCH_ERROR)
2021 goto cleanup;
2024 /* Mark the iteration variable's symbol as used as a FORALL index. */
2025 iter->var->symtree->n.sym->forall_index = true;
2027 *result = iter;
2028 return MATCH_YES;
2030 syntax:
2031 gfc_error ("Syntax error in FORALL iterator at %C");
2032 m = MATCH_ERROR;
2034 cleanup:
2036 gfc_current_locus = where;
2037 gfc_free_forall_iterator (iter);
2038 return m;
2042 /* Match the header of a FORALL statement. */
2044 static match
2045 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2047 gfc_forall_iterator *head, *tail, *new_iter;
2048 gfc_expr *msk;
2049 match m;
2051 gfc_gobble_whitespace ();
2053 head = tail = NULL;
2054 msk = NULL;
2056 if (gfc_match_char ('(') != MATCH_YES)
2057 return MATCH_NO;
2059 m = match_forall_iterator (&new_iter);
2060 if (m == MATCH_ERROR)
2061 goto cleanup;
2062 if (m == MATCH_NO)
2063 goto syntax;
2065 head = tail = new_iter;
2067 for (;;)
2069 if (gfc_match_char (',') != MATCH_YES)
2070 break;
2072 m = match_forall_iterator (&new_iter);
2073 if (m == MATCH_ERROR)
2074 goto cleanup;
2076 if (m == MATCH_YES)
2078 tail->next = new_iter;
2079 tail = new_iter;
2080 continue;
2083 /* Have to have a mask expression. */
2085 m = gfc_match_expr (&msk);
2086 if (m == MATCH_NO)
2087 goto syntax;
2088 if (m == MATCH_ERROR)
2089 goto cleanup;
2091 break;
2094 if (gfc_match_char (')') == MATCH_NO)
2095 goto syntax;
2097 *phead = head;
2098 *mask = msk;
2099 return MATCH_YES;
2101 syntax:
2102 gfc_syntax_error (ST_FORALL);
2104 cleanup:
2105 gfc_free_expr (msk);
2106 gfc_free_forall_iterator (head);
2108 return MATCH_ERROR;
2111 /* Match the rest of a simple FORALL statement that follows an
2112 IF statement. */
2114 static match
2115 match_simple_forall (void)
2117 gfc_forall_iterator *head;
2118 gfc_expr *mask;
2119 gfc_code *c;
2120 match m;
2122 mask = NULL;
2123 head = NULL;
2124 c = NULL;
2126 m = match_forall_header (&head, &mask);
2128 if (m == MATCH_NO)
2129 goto syntax;
2130 if (m != MATCH_YES)
2131 goto cleanup;
2133 m = gfc_match_assignment ();
2135 if (m == MATCH_ERROR)
2136 goto cleanup;
2137 if (m == MATCH_NO)
2139 m = gfc_match_pointer_assignment ();
2140 if (m == MATCH_ERROR)
2141 goto cleanup;
2142 if (m == MATCH_NO)
2143 goto syntax;
2146 c = XCNEW (gfc_code);
2147 *c = new_st;
2148 c->loc = gfc_current_locus;
2150 if (gfc_match_eos () != MATCH_YES)
2151 goto syntax;
2153 gfc_clear_new_st ();
2154 new_st.op = EXEC_FORALL;
2155 new_st.expr1 = mask;
2156 new_st.ext.forall_iterator = head;
2157 new_st.block = gfc_get_code (EXEC_FORALL);
2158 new_st.block->next = c;
2160 return MATCH_YES;
2162 syntax:
2163 gfc_syntax_error (ST_FORALL);
2165 cleanup:
2166 gfc_free_forall_iterator (head);
2167 gfc_free_expr (mask);
2169 return MATCH_ERROR;
2173 /* Match a FORALL statement. */
2175 match
2176 gfc_match_forall (gfc_statement *st)
2178 gfc_forall_iterator *head;
2179 gfc_expr *mask;
2180 gfc_code *c;
2181 match m0, m;
2183 head = NULL;
2184 mask = NULL;
2185 c = NULL;
2187 m0 = gfc_match_label ();
2188 if (m0 == MATCH_ERROR)
2189 return MATCH_ERROR;
2191 m = gfc_match (" forall");
2192 if (m != MATCH_YES)
2193 return m;
2195 m = match_forall_header (&head, &mask);
2196 if (m == MATCH_ERROR)
2197 goto cleanup;
2198 if (m == MATCH_NO)
2199 goto syntax;
2201 if (gfc_match_eos () == MATCH_YES)
2203 *st = ST_FORALL_BLOCK;
2204 new_st.op = EXEC_FORALL;
2205 new_st.expr1 = mask;
2206 new_st.ext.forall_iterator = head;
2207 return MATCH_YES;
2210 m = gfc_match_assignment ();
2211 if (m == MATCH_ERROR)
2212 goto cleanup;
2213 if (m == MATCH_NO)
2215 m = gfc_match_pointer_assignment ();
2216 if (m == MATCH_ERROR)
2217 goto cleanup;
2218 if (m == MATCH_NO)
2219 goto syntax;
2222 c = XCNEW (gfc_code);
2223 *c = new_st;
2224 c->loc = gfc_current_locus;
2226 gfc_clear_new_st ();
2227 new_st.op = EXEC_FORALL;
2228 new_st.expr1 = mask;
2229 new_st.ext.forall_iterator = head;
2230 new_st.block = gfc_get_code (EXEC_FORALL);
2231 new_st.block->next = c;
2233 *st = ST_FORALL;
2234 return MATCH_YES;
2236 syntax:
2237 gfc_syntax_error (ST_FORALL);
2239 cleanup:
2240 gfc_free_forall_iterator (head);
2241 gfc_free_expr (mask);
2242 gfc_free_statements (c);
2243 return MATCH_NO;
2247 /* Match a DO statement. */
2249 match
2250 gfc_match_do (void)
2252 gfc_iterator iter, *ip;
2253 locus old_loc;
2254 gfc_st_label *label;
2255 match m;
2257 old_loc = gfc_current_locus;
2259 label = NULL;
2260 iter.var = iter.start = iter.end = iter.step = NULL;
2262 m = gfc_match_label ();
2263 if (m == MATCH_ERROR)
2264 return m;
2266 if (gfc_match (" do") != MATCH_YES)
2267 return MATCH_NO;
2269 m = gfc_match_st_label (&label);
2270 if (m == MATCH_ERROR)
2271 goto cleanup;
2273 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2275 if (gfc_match_eos () == MATCH_YES)
2277 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2278 new_st.op = EXEC_DO_WHILE;
2279 goto done;
2282 /* Match an optional comma, if no comma is found, a space is obligatory. */
2283 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2284 return MATCH_NO;
2286 /* Check for balanced parens. */
2288 if (gfc_match_parens () == MATCH_ERROR)
2289 return MATCH_ERROR;
2291 if (gfc_match (" concurrent") == MATCH_YES)
2293 gfc_forall_iterator *head;
2294 gfc_expr *mask;
2296 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
2297 return MATCH_ERROR;
2300 mask = NULL;
2301 head = NULL;
2302 m = match_forall_header (&head, &mask);
2304 if (m == MATCH_NO)
2305 return m;
2306 if (m == MATCH_ERROR)
2307 goto concurr_cleanup;
2309 if (gfc_match_eos () != MATCH_YES)
2310 goto concurr_cleanup;
2312 if (label != NULL
2313 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2314 goto concurr_cleanup;
2316 new_st.label1 = label;
2317 new_st.op = EXEC_DO_CONCURRENT;
2318 new_st.expr1 = mask;
2319 new_st.ext.forall_iterator = head;
2321 return MATCH_YES;
2323 concurr_cleanup:
2324 gfc_syntax_error (ST_DO);
2325 gfc_free_expr (mask);
2326 gfc_free_forall_iterator (head);
2327 return MATCH_ERROR;
2330 /* See if we have a DO WHILE. */
2331 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2333 new_st.op = EXEC_DO_WHILE;
2334 goto done;
2337 /* The abortive DO WHILE may have done something to the symbol
2338 table, so we start over. */
2339 gfc_undo_symbols ();
2340 gfc_current_locus = old_loc;
2342 gfc_match_label (); /* This won't error. */
2343 gfc_match (" do "); /* This will work. */
2345 gfc_match_st_label (&label); /* Can't error out. */
2346 gfc_match_char (','); /* Optional comma. */
2348 m = gfc_match_iterator (&iter, 0);
2349 if (m == MATCH_NO)
2350 return MATCH_NO;
2351 if (m == MATCH_ERROR)
2352 goto cleanup;
2354 iter.var->symtree->n.sym->attr.implied_index = 0;
2355 gfc_check_do_variable (iter.var->symtree);
2357 if (gfc_match_eos () != MATCH_YES)
2359 gfc_syntax_error (ST_DO);
2360 goto cleanup;
2363 new_st.op = EXEC_DO;
2365 done:
2366 if (label != NULL
2367 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2368 goto cleanup;
2370 new_st.label1 = label;
2372 if (new_st.op == EXEC_DO_WHILE)
2373 new_st.expr1 = iter.end;
2374 else
2376 new_st.ext.iterator = ip = gfc_get_iterator ();
2377 *ip = iter;
2380 return MATCH_YES;
2382 cleanup:
2383 gfc_free_iterator (&iter, 0);
2385 return MATCH_ERROR;
2389 /* Match an EXIT or CYCLE statement. */
2391 static match
2392 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2394 gfc_state_data *p, *o;
2395 gfc_symbol *sym;
2396 match m;
2397 int cnt;
2399 if (gfc_match_eos () == MATCH_YES)
2400 sym = NULL;
2401 else
2403 char name[GFC_MAX_SYMBOL_LEN + 1];
2404 gfc_symtree* stree;
2406 m = gfc_match ("% %n%t", name);
2407 if (m == MATCH_ERROR)
2408 return MATCH_ERROR;
2409 if (m == MATCH_NO)
2411 gfc_syntax_error (st);
2412 return MATCH_ERROR;
2415 /* Find the corresponding symbol. If there's a BLOCK statement
2416 between here and the label, it is not in gfc_current_ns but a parent
2417 namespace! */
2418 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2419 if (!stree)
2421 gfc_error ("Name %qs in %s statement at %C is unknown",
2422 name, gfc_ascii_statement (st));
2423 return MATCH_ERROR;
2426 sym = stree->n.sym;
2427 if (sym->attr.flavor != FL_LABEL)
2429 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2430 name, gfc_ascii_statement (st));
2431 return MATCH_ERROR;
2435 /* Find the loop specified by the label (or lack of a label). */
2436 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2437 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2438 o = p;
2439 else if (p->state == COMP_CRITICAL)
2441 gfc_error("%s statement at %C leaves CRITICAL construct",
2442 gfc_ascii_statement (st));
2443 return MATCH_ERROR;
2445 else if (p->state == COMP_DO_CONCURRENT
2446 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2448 /* F2008, C821 & C845. */
2449 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2450 gfc_ascii_statement (st));
2451 return MATCH_ERROR;
2453 else if ((sym && sym == p->sym)
2454 || (!sym && (p->state == COMP_DO
2455 || p->state == COMP_DO_CONCURRENT)))
2456 break;
2458 if (p == NULL)
2460 if (sym == NULL)
2461 gfc_error ("%s statement at %C is not within a construct",
2462 gfc_ascii_statement (st));
2463 else
2464 gfc_error ("%s statement at %C is not within construct %qs",
2465 gfc_ascii_statement (st), sym->name);
2467 return MATCH_ERROR;
2470 /* Special checks for EXIT from non-loop constructs. */
2471 switch (p->state)
2473 case COMP_DO:
2474 case COMP_DO_CONCURRENT:
2475 break;
2477 case COMP_CRITICAL:
2478 /* This is already handled above. */
2479 gcc_unreachable ();
2481 case COMP_ASSOCIATE:
2482 case COMP_BLOCK:
2483 case COMP_IF:
2484 case COMP_SELECT:
2485 case COMP_SELECT_TYPE:
2486 gcc_assert (sym);
2487 if (op == EXEC_CYCLE)
2489 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2490 " construct %qs", sym->name);
2491 return MATCH_ERROR;
2493 gcc_assert (op == EXEC_EXIT);
2494 if (!gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2495 " do-construct-name at %C"))
2496 return MATCH_ERROR;
2497 break;
2499 default:
2500 gfc_error ("%s statement at %C is not applicable to construct %qs",
2501 gfc_ascii_statement (st), sym->name);
2502 return MATCH_ERROR;
2505 if (o != NULL)
2507 gfc_error (is_oacc (p)
2508 ? "%s statement at %C leaving OpenACC structured block"
2509 : "%s statement at %C leaving OpenMP structured block",
2510 gfc_ascii_statement (st));
2511 return MATCH_ERROR;
2514 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2515 o = o->previous;
2516 if (cnt > 0
2517 && o != NULL
2518 && o->state == COMP_OMP_STRUCTURED_BLOCK
2519 && (o->head->op == EXEC_OACC_LOOP
2520 || o->head->op == EXEC_OACC_PARALLEL_LOOP))
2522 int collapse = 1;
2523 gcc_assert (o->head->next != NULL
2524 && (o->head->next->op == EXEC_DO
2525 || o->head->next->op == EXEC_DO_WHILE)
2526 && o->previous != NULL
2527 && o->previous->tail->op == o->head->op);
2528 if (o->previous->tail->ext.omp_clauses != NULL
2529 && o->previous->tail->ext.omp_clauses->collapse > 1)
2530 collapse = o->previous->tail->ext.omp_clauses->collapse;
2531 if (st == ST_EXIT && cnt <= collapse)
2533 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2534 return MATCH_ERROR;
2536 if (st == ST_CYCLE && cnt < collapse)
2538 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2539 " !$ACC LOOP loop");
2540 return MATCH_ERROR;
2543 if (cnt > 0
2544 && o != NULL
2545 && (o->state == COMP_OMP_STRUCTURED_BLOCK)
2546 && (o->head->op == EXEC_OMP_DO
2547 || o->head->op == EXEC_OMP_PARALLEL_DO
2548 || o->head->op == EXEC_OMP_SIMD
2549 || o->head->op == EXEC_OMP_DO_SIMD
2550 || o->head->op == EXEC_OMP_PARALLEL_DO_SIMD))
2552 int collapse = 1;
2553 gcc_assert (o->head->next != NULL
2554 && (o->head->next->op == EXEC_DO
2555 || o->head->next->op == EXEC_DO_WHILE)
2556 && o->previous != NULL
2557 && o->previous->tail->op == o->head->op);
2558 if (o->previous->tail->ext.omp_clauses != NULL
2559 && o->previous->tail->ext.omp_clauses->collapse > 1)
2560 collapse = o->previous->tail->ext.omp_clauses->collapse;
2561 if (st == ST_EXIT && cnt <= collapse)
2563 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2564 return MATCH_ERROR;
2566 if (st == ST_CYCLE && cnt < collapse)
2568 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2569 " !$OMP DO loop");
2570 return MATCH_ERROR;
2574 /* Save the first statement in the construct - needed by the backend. */
2575 new_st.ext.which_construct = p->construct;
2577 new_st.op = op;
2579 return MATCH_YES;
2583 /* Match the EXIT statement. */
2585 match
2586 gfc_match_exit (void)
2588 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2592 /* Match the CYCLE statement. */
2594 match
2595 gfc_match_cycle (void)
2597 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2601 /* Match a number or character constant after an (ERROR) STOP or PAUSE
2602 statement. */
2604 static match
2605 gfc_match_stopcode (gfc_statement st)
2607 gfc_expr *e;
2608 match m;
2610 e = NULL;
2612 if (gfc_match_eos () != MATCH_YES)
2614 m = gfc_match_init_expr (&e);
2615 if (m == MATCH_ERROR)
2616 goto cleanup;
2617 if (m == MATCH_NO)
2618 goto syntax;
2620 if (gfc_match_eos () != MATCH_YES)
2621 goto syntax;
2624 if (gfc_pure (NULL))
2626 if (st == ST_ERROR_STOP)
2628 if (!gfc_notify_std (GFC_STD_F2015, "%s statement at %C in PURE "
2629 "procedure", gfc_ascii_statement (st)))
2630 goto cleanup;
2632 else
2634 gfc_error ("%s statement not allowed in PURE procedure at %C",
2635 gfc_ascii_statement (st));
2636 goto cleanup;
2640 gfc_unset_implicit_pure (NULL);
2642 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
2644 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2645 goto cleanup;
2647 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
2649 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
2650 goto cleanup;
2653 if (e != NULL)
2655 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
2657 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2658 &e->where);
2659 goto cleanup;
2662 if (e->rank != 0)
2664 gfc_error ("STOP code at %L must be scalar",
2665 &e->where);
2666 goto cleanup;
2669 if (e->ts.type == BT_CHARACTER
2670 && e->ts.kind != gfc_default_character_kind)
2672 gfc_error ("STOP code at %L must be default character KIND=%d",
2673 &e->where, (int) gfc_default_character_kind);
2674 goto cleanup;
2677 if (e->ts.type == BT_INTEGER
2678 && e->ts.kind != gfc_default_integer_kind)
2680 gfc_error ("STOP code at %L must be default integer KIND=%d",
2681 &e->where, (int) gfc_default_integer_kind);
2682 goto cleanup;
2686 switch (st)
2688 case ST_STOP:
2689 new_st.op = EXEC_STOP;
2690 break;
2691 case ST_ERROR_STOP:
2692 new_st.op = EXEC_ERROR_STOP;
2693 break;
2694 case ST_PAUSE:
2695 new_st.op = EXEC_PAUSE;
2696 break;
2697 default:
2698 gcc_unreachable ();
2701 new_st.expr1 = e;
2702 new_st.ext.stop_code = -1;
2704 return MATCH_YES;
2706 syntax:
2707 gfc_syntax_error (st);
2709 cleanup:
2711 gfc_free_expr (e);
2712 return MATCH_ERROR;
2716 /* Match the (deprecated) PAUSE statement. */
2718 match
2719 gfc_match_pause (void)
2721 match m;
2723 m = gfc_match_stopcode (ST_PAUSE);
2724 if (m == MATCH_YES)
2726 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
2727 m = MATCH_ERROR;
2729 return m;
2733 /* Match the STOP statement. */
2735 match
2736 gfc_match_stop (void)
2738 return gfc_match_stopcode (ST_STOP);
2742 /* Match the ERROR STOP statement. */
2744 match
2745 gfc_match_error_stop (void)
2747 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
2748 return MATCH_ERROR;
2750 return gfc_match_stopcode (ST_ERROR_STOP);
2754 /* Match LOCK/UNLOCK statement. Syntax:
2755 LOCK ( lock-variable [ , lock-stat-list ] )
2756 UNLOCK ( lock-variable [ , sync-stat-list ] )
2757 where lock-stat is ACQUIRED_LOCK or sync-stat
2758 and sync-stat is STAT= or ERRMSG=. */
2760 static match
2761 lock_unlock_statement (gfc_statement st)
2763 match m;
2764 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
2765 bool saw_acq_lock, saw_stat, saw_errmsg;
2767 tmp = lockvar = acq_lock = stat = errmsg = NULL;
2768 saw_acq_lock = saw_stat = saw_errmsg = false;
2770 if (gfc_pure (NULL))
2772 gfc_error ("Image control statement %s at %C in PURE procedure",
2773 st == ST_LOCK ? "LOCK" : "UNLOCK");
2774 return MATCH_ERROR;
2777 gfc_unset_implicit_pure (NULL);
2779 if (flag_coarray == GFC_FCOARRAY_NONE)
2781 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2782 return MATCH_ERROR;
2785 if (gfc_find_state (COMP_CRITICAL))
2787 gfc_error ("Image control statement %s at %C in CRITICAL block",
2788 st == ST_LOCK ? "LOCK" : "UNLOCK");
2789 return MATCH_ERROR;
2792 if (gfc_find_state (COMP_DO_CONCURRENT))
2794 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
2795 st == ST_LOCK ? "LOCK" : "UNLOCK");
2796 return MATCH_ERROR;
2799 if (gfc_match_char ('(') != MATCH_YES)
2800 goto syntax;
2802 if (gfc_match ("%e", &lockvar) != MATCH_YES)
2803 goto syntax;
2804 m = gfc_match_char (',');
2805 if (m == MATCH_ERROR)
2806 goto syntax;
2807 if (m == MATCH_NO)
2809 m = gfc_match_char (')');
2810 if (m == MATCH_YES)
2811 goto done;
2812 goto syntax;
2815 for (;;)
2817 m = gfc_match (" stat = %v", &tmp);
2818 if (m == MATCH_ERROR)
2819 goto syntax;
2820 if (m == MATCH_YES)
2822 if (saw_stat)
2824 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
2825 goto cleanup;
2827 stat = tmp;
2828 saw_stat = true;
2830 m = gfc_match_char (',');
2831 if (m == MATCH_YES)
2832 continue;
2834 tmp = NULL;
2835 break;
2838 m = gfc_match (" errmsg = %v", &tmp);
2839 if (m == MATCH_ERROR)
2840 goto syntax;
2841 if (m == MATCH_YES)
2843 if (saw_errmsg)
2845 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
2846 goto cleanup;
2848 errmsg = tmp;
2849 saw_errmsg = true;
2851 m = gfc_match_char (',');
2852 if (m == MATCH_YES)
2853 continue;
2855 tmp = NULL;
2856 break;
2859 m = gfc_match (" acquired_lock = %v", &tmp);
2860 if (m == MATCH_ERROR || st == ST_UNLOCK)
2861 goto syntax;
2862 if (m == MATCH_YES)
2864 if (saw_acq_lock)
2866 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L ",
2867 &tmp->where);
2868 goto cleanup;
2870 acq_lock = tmp;
2871 saw_acq_lock = true;
2873 m = gfc_match_char (',');
2874 if (m == MATCH_YES)
2875 continue;
2877 tmp = NULL;
2878 break;
2881 break;
2884 if (m == MATCH_ERROR)
2885 goto syntax;
2887 if (gfc_match (" )%t") != MATCH_YES)
2888 goto syntax;
2890 done:
2891 switch (st)
2893 case ST_LOCK:
2894 new_st.op = EXEC_LOCK;
2895 break;
2896 case ST_UNLOCK:
2897 new_st.op = EXEC_UNLOCK;
2898 break;
2899 default:
2900 gcc_unreachable ();
2903 new_st.expr1 = lockvar;
2904 new_st.expr2 = stat;
2905 new_st.expr3 = errmsg;
2906 new_st.expr4 = acq_lock;
2908 return MATCH_YES;
2910 syntax:
2911 gfc_syntax_error (st);
2913 cleanup:
2914 if (acq_lock != tmp)
2915 gfc_free_expr (acq_lock);
2916 if (errmsg != tmp)
2917 gfc_free_expr (errmsg);
2918 if (stat != tmp)
2919 gfc_free_expr (stat);
2921 gfc_free_expr (tmp);
2922 gfc_free_expr (lockvar);
2924 return MATCH_ERROR;
2928 match
2929 gfc_match_lock (void)
2931 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
2932 return MATCH_ERROR;
2934 return lock_unlock_statement (ST_LOCK);
2938 match
2939 gfc_match_unlock (void)
2941 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
2942 return MATCH_ERROR;
2944 return lock_unlock_statement (ST_UNLOCK);
2948 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
2949 SYNC ALL [(sync-stat-list)]
2950 SYNC MEMORY [(sync-stat-list)]
2951 SYNC IMAGES (image-set [, sync-stat-list] )
2952 with sync-stat is int-expr or *. */
2954 static match
2955 sync_statement (gfc_statement st)
2957 match m;
2958 gfc_expr *tmp, *imageset, *stat, *errmsg;
2959 bool saw_stat, saw_errmsg;
2961 tmp = imageset = stat = errmsg = NULL;
2962 saw_stat = saw_errmsg = false;
2964 if (gfc_pure (NULL))
2966 gfc_error ("Image control statement SYNC at %C in PURE procedure");
2967 return MATCH_ERROR;
2970 gfc_unset_implicit_pure (NULL);
2972 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
2973 return MATCH_ERROR;
2975 if (flag_coarray == GFC_FCOARRAY_NONE)
2977 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
2978 "enable");
2979 return MATCH_ERROR;
2982 if (gfc_find_state (COMP_CRITICAL))
2984 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
2985 return MATCH_ERROR;
2988 if (gfc_find_state (COMP_DO_CONCURRENT))
2990 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
2991 return MATCH_ERROR;
2994 if (gfc_match_eos () == MATCH_YES)
2996 if (st == ST_SYNC_IMAGES)
2997 goto syntax;
2998 goto done;
3001 if (gfc_match_char ('(') != MATCH_YES)
3002 goto syntax;
3004 if (st == ST_SYNC_IMAGES)
3006 /* Denote '*' as imageset == NULL. */
3007 m = gfc_match_char ('*');
3008 if (m == MATCH_ERROR)
3009 goto syntax;
3010 if (m == MATCH_NO)
3012 if (gfc_match ("%e", &imageset) != MATCH_YES)
3013 goto syntax;
3015 m = gfc_match_char (',');
3016 if (m == MATCH_ERROR)
3017 goto syntax;
3018 if (m == MATCH_NO)
3020 m = gfc_match_char (')');
3021 if (m == MATCH_YES)
3022 goto done;
3023 goto syntax;
3027 for (;;)
3029 m = gfc_match (" stat = %v", &tmp);
3030 if (m == MATCH_ERROR)
3031 goto syntax;
3032 if (m == MATCH_YES)
3034 if (saw_stat)
3036 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3037 goto cleanup;
3039 stat = tmp;
3040 saw_stat = true;
3042 if (gfc_match_char (',') == MATCH_YES)
3043 continue;
3045 tmp = NULL;
3046 break;
3049 m = gfc_match (" errmsg = %v", &tmp);
3050 if (m == MATCH_ERROR)
3051 goto syntax;
3052 if (m == MATCH_YES)
3054 if (saw_errmsg)
3056 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3057 goto cleanup;
3059 errmsg = tmp;
3060 saw_errmsg = true;
3062 if (gfc_match_char (',') == MATCH_YES)
3063 continue;
3065 tmp = NULL;
3066 break;
3069 break;
3072 if (gfc_match (" )%t") != MATCH_YES)
3073 goto syntax;
3075 done:
3076 switch (st)
3078 case ST_SYNC_ALL:
3079 new_st.op = EXEC_SYNC_ALL;
3080 break;
3081 case ST_SYNC_IMAGES:
3082 new_st.op = EXEC_SYNC_IMAGES;
3083 break;
3084 case ST_SYNC_MEMORY:
3085 new_st.op = EXEC_SYNC_MEMORY;
3086 break;
3087 default:
3088 gcc_unreachable ();
3091 new_st.expr1 = imageset;
3092 new_st.expr2 = stat;
3093 new_st.expr3 = errmsg;
3095 return MATCH_YES;
3097 syntax:
3098 gfc_syntax_error (st);
3100 cleanup:
3101 if (stat != tmp)
3102 gfc_free_expr (stat);
3103 if (errmsg != tmp)
3104 gfc_free_expr (errmsg);
3106 gfc_free_expr (tmp);
3107 gfc_free_expr (imageset);
3109 return MATCH_ERROR;
3113 /* Match SYNC ALL statement. */
3115 match
3116 gfc_match_sync_all (void)
3118 return sync_statement (ST_SYNC_ALL);
3122 /* Match SYNC IMAGES statement. */
3124 match
3125 gfc_match_sync_images (void)
3127 return sync_statement (ST_SYNC_IMAGES);
3131 /* Match SYNC MEMORY statement. */
3133 match
3134 gfc_match_sync_memory (void)
3136 return sync_statement (ST_SYNC_MEMORY);
3140 /* Match a CONTINUE statement. */
3142 match
3143 gfc_match_continue (void)
3145 if (gfc_match_eos () != MATCH_YES)
3147 gfc_syntax_error (ST_CONTINUE);
3148 return MATCH_ERROR;
3151 new_st.op = EXEC_CONTINUE;
3152 return MATCH_YES;
3156 /* Match the (deprecated) ASSIGN statement. */
3158 match
3159 gfc_match_assign (void)
3161 gfc_expr *expr;
3162 gfc_st_label *label;
3164 if (gfc_match (" %l", &label) == MATCH_YES)
3166 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
3167 return MATCH_ERROR;
3168 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
3170 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
3171 return MATCH_ERROR;
3173 expr->symtree->n.sym->attr.assign = 1;
3175 new_st.op = EXEC_LABEL_ASSIGN;
3176 new_st.label1 = label;
3177 new_st.expr1 = expr;
3178 return MATCH_YES;
3181 return MATCH_NO;
3185 /* Match the GO TO statement. As a computed GOTO statement is
3186 matched, it is transformed into an equivalent SELECT block. No
3187 tree is necessary, and the resulting jumps-to-jumps are
3188 specifically optimized away by the back end. */
3190 match
3191 gfc_match_goto (void)
3193 gfc_code *head, *tail;
3194 gfc_expr *expr;
3195 gfc_case *cp;
3196 gfc_st_label *label;
3197 int i;
3198 match m;
3200 if (gfc_match (" %l%t", &label) == MATCH_YES)
3202 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3203 return MATCH_ERROR;
3205 new_st.op = EXEC_GOTO;
3206 new_st.label1 = label;
3207 return MATCH_YES;
3210 /* The assigned GO TO statement. */
3212 if (gfc_match_variable (&expr, 0) == MATCH_YES)
3214 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
3215 return MATCH_ERROR;
3217 new_st.op = EXEC_GOTO;
3218 new_st.expr1 = expr;
3220 if (gfc_match_eos () == MATCH_YES)
3221 return MATCH_YES;
3223 /* Match label list. */
3224 gfc_match_char (',');
3225 if (gfc_match_char ('(') != MATCH_YES)
3227 gfc_syntax_error (ST_GOTO);
3228 return MATCH_ERROR;
3230 head = tail = NULL;
3234 m = gfc_match_st_label (&label);
3235 if (m != MATCH_YES)
3236 goto syntax;
3238 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3239 goto cleanup;
3241 if (head == NULL)
3242 head = tail = gfc_get_code (EXEC_GOTO);
3243 else
3245 tail->block = gfc_get_code (EXEC_GOTO);
3246 tail = tail->block;
3249 tail->label1 = label;
3251 while (gfc_match_char (',') == MATCH_YES);
3253 if (gfc_match (")%t") != MATCH_YES)
3254 goto syntax;
3256 if (head == NULL)
3258 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3259 goto syntax;
3261 new_st.block = head;
3263 return MATCH_YES;
3266 /* Last chance is a computed GO TO statement. */
3267 if (gfc_match_char ('(') != MATCH_YES)
3269 gfc_syntax_error (ST_GOTO);
3270 return MATCH_ERROR;
3273 head = tail = NULL;
3274 i = 1;
3278 m = gfc_match_st_label (&label);
3279 if (m != MATCH_YES)
3280 goto syntax;
3282 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3283 goto cleanup;
3285 if (head == NULL)
3286 head = tail = gfc_get_code (EXEC_SELECT);
3287 else
3289 tail->block = gfc_get_code (EXEC_SELECT);
3290 tail = tail->block;
3293 cp = gfc_get_case ();
3294 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
3295 NULL, i++);
3297 tail->ext.block.case_list = cp;
3299 tail->next = gfc_get_code (EXEC_GOTO);
3300 tail->next->label1 = label;
3302 while (gfc_match_char (',') == MATCH_YES);
3304 if (gfc_match_char (')') != MATCH_YES)
3305 goto syntax;
3307 if (head == NULL)
3309 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3310 goto syntax;
3313 /* Get the rest of the statement. */
3314 gfc_match_char (',');
3316 if (gfc_match (" %e%t", &expr) != MATCH_YES)
3317 goto syntax;
3319 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
3320 return MATCH_ERROR;
3322 /* At this point, a computed GOTO has been fully matched and an
3323 equivalent SELECT statement constructed. */
3325 new_st.op = EXEC_SELECT;
3326 new_st.expr1 = NULL;
3328 /* Hack: For a "real" SELECT, the expression is in expr. We put
3329 it in expr2 so we can distinguish then and produce the correct
3330 diagnostics. */
3331 new_st.expr2 = expr;
3332 new_st.block = head;
3333 return MATCH_YES;
3335 syntax:
3336 gfc_syntax_error (ST_GOTO);
3337 cleanup:
3338 gfc_free_statements (head);
3339 return MATCH_ERROR;
3343 /* Frees a list of gfc_alloc structures. */
3345 void
3346 gfc_free_alloc_list (gfc_alloc *p)
3348 gfc_alloc *q;
3350 for (; p; p = q)
3352 q = p->next;
3353 gfc_free_expr (p->expr);
3354 free (p);
3359 /* Match an ALLOCATE statement. */
3361 match
3362 gfc_match_allocate (void)
3364 gfc_alloc *head, *tail;
3365 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
3366 gfc_typespec ts;
3367 gfc_symbol *sym;
3368 match m;
3369 locus old_locus, deferred_locus;
3370 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
3371 bool saw_unlimited = false;
3373 head = tail = NULL;
3374 stat = errmsg = source = mold = tmp = NULL;
3375 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
3377 if (gfc_match_char ('(') != MATCH_YES)
3378 goto syntax;
3380 /* Match an optional type-spec. */
3381 old_locus = gfc_current_locus;
3382 m = gfc_match_type_spec (&ts);
3383 if (m == MATCH_ERROR)
3384 goto cleanup;
3385 else if (m == MATCH_NO)
3387 char name[GFC_MAX_SYMBOL_LEN + 3];
3389 if (gfc_match ("%n :: ", name) == MATCH_YES)
3391 gfc_error ("Error in type-spec at %L", &old_locus);
3392 goto cleanup;
3395 ts.type = BT_UNKNOWN;
3397 else
3399 if (gfc_match (" :: ") == MATCH_YES)
3401 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
3402 &old_locus))
3403 goto cleanup;
3405 if (ts.deferred)
3407 gfc_error ("Type-spec at %L cannot contain a deferred "
3408 "type parameter", &old_locus);
3409 goto cleanup;
3412 if (ts.type == BT_CHARACTER)
3413 ts.u.cl->length_from_typespec = true;
3415 else
3417 ts.type = BT_UNKNOWN;
3418 gfc_current_locus = old_locus;
3422 for (;;)
3424 if (head == NULL)
3425 head = tail = gfc_get_alloc ();
3426 else
3428 tail->next = gfc_get_alloc ();
3429 tail = tail->next;
3432 m = gfc_match_variable (&tail->expr, 0);
3433 if (m == MATCH_NO)
3434 goto syntax;
3435 if (m == MATCH_ERROR)
3436 goto cleanup;
3438 if (gfc_check_do_variable (tail->expr->symtree))
3439 goto cleanup;
3441 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
3442 if (impure && gfc_pure (NULL))
3444 gfc_error ("Bad allocate-object at %C for a PURE procedure");
3445 goto cleanup;
3448 if (impure)
3449 gfc_unset_implicit_pure (NULL);
3451 if (tail->expr->ts.deferred)
3453 saw_deferred = true;
3454 deferred_locus = tail->expr->where;
3457 if (gfc_find_state (COMP_DO_CONCURRENT)
3458 || gfc_find_state (COMP_CRITICAL))
3460 gfc_ref *ref;
3461 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
3462 for (ref = tail->expr->ref; ref; ref = ref->next)
3463 if (ref->type == REF_COMPONENT)
3464 coarray = ref->u.c.component->attr.codimension;
3466 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
3468 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
3469 goto cleanup;
3471 if (coarray && gfc_find_state (COMP_CRITICAL))
3473 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
3474 goto cleanup;
3478 /* Check for F08:C628. */
3479 sym = tail->expr->symtree->n.sym;
3480 b1 = !(tail->expr->ref
3481 && (tail->expr->ref->type == REF_COMPONENT
3482 || tail->expr->ref->type == REF_ARRAY));
3483 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
3484 b2 = !(CLASS_DATA (sym)->attr.allocatable
3485 || CLASS_DATA (sym)->attr.class_pointer);
3486 else
3487 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
3488 || sym->attr.proc_pointer);
3489 b3 = sym && sym->ns && sym->ns->proc_name
3490 && (sym->ns->proc_name->attr.allocatable
3491 || sym->ns->proc_name->attr.pointer
3492 || sym->ns->proc_name->attr.proc_pointer);
3493 if (b1 && b2 && !b3)
3495 gfc_error ("Allocate-object at %L is neither a data pointer "
3496 "nor an allocatable variable", &tail->expr->where);
3497 goto cleanup;
3500 /* The ALLOCATE statement had an optional typespec. Check the
3501 constraints. */
3502 if (ts.type != BT_UNKNOWN)
3504 /* Enforce F03:C624. */
3505 if (!gfc_type_compatible (&tail->expr->ts, &ts))
3507 gfc_error ("Type of entity at %L is type incompatible with "
3508 "typespec", &tail->expr->where);
3509 goto cleanup;
3512 /* Enforce F03:C627. */
3513 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
3515 gfc_error ("Kind type parameter for entity at %L differs from "
3516 "the kind type parameter of the typespec",
3517 &tail->expr->where);
3518 goto cleanup;
3522 if (tail->expr->ts.type == BT_DERIVED)
3523 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
3525 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
3527 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
3529 gfc_error ("Shape specification for allocatable scalar at %C");
3530 goto cleanup;
3533 if (gfc_match_char (',') != MATCH_YES)
3534 break;
3536 alloc_opt_list:
3538 m = gfc_match (" stat = %v", &tmp);
3539 if (m == MATCH_ERROR)
3540 goto cleanup;
3541 if (m == MATCH_YES)
3543 /* Enforce C630. */
3544 if (saw_stat)
3546 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3547 goto cleanup;
3550 stat = tmp;
3551 tmp = NULL;
3552 saw_stat = true;
3554 if (gfc_check_do_variable (stat->symtree))
3555 goto cleanup;
3557 if (gfc_match_char (',') == MATCH_YES)
3558 goto alloc_opt_list;
3561 m = gfc_match (" errmsg = %v", &tmp);
3562 if (m == MATCH_ERROR)
3563 goto cleanup;
3564 if (m == MATCH_YES)
3566 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
3567 goto cleanup;
3569 /* Enforce C630. */
3570 if (saw_errmsg)
3572 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3573 goto cleanup;
3576 errmsg = tmp;
3577 tmp = NULL;
3578 saw_errmsg = true;
3580 if (gfc_match_char (',') == MATCH_YES)
3581 goto alloc_opt_list;
3584 m = gfc_match (" source = %e", &tmp);
3585 if (m == MATCH_ERROR)
3586 goto cleanup;
3587 if (m == MATCH_YES)
3589 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
3590 goto cleanup;
3592 /* Enforce C630. */
3593 if (saw_source)
3595 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
3596 goto cleanup;
3599 /* The next 2 conditionals check C631. */
3600 if (ts.type != BT_UNKNOWN)
3602 gfc_error_1 ("SOURCE tag at %L conflicts with the typespec at %L",
3603 &tmp->where, &old_locus);
3604 goto cleanup;
3607 if (head->next
3608 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
3609 " with more than a single allocate object",
3610 &tmp->where))
3611 goto cleanup;
3613 source = tmp;
3614 tmp = NULL;
3615 saw_source = true;
3617 if (gfc_match_char (',') == MATCH_YES)
3618 goto alloc_opt_list;
3621 m = gfc_match (" mold = %e", &tmp);
3622 if (m == MATCH_ERROR)
3623 goto cleanup;
3624 if (m == MATCH_YES)
3626 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
3627 goto cleanup;
3629 /* Check F08:C636. */
3630 if (saw_mold)
3632 gfc_error ("Redundant MOLD tag found at %L ", &tmp->where);
3633 goto cleanup;
3636 /* Check F08:C637. */
3637 if (ts.type != BT_UNKNOWN)
3639 gfc_error_1 ("MOLD tag at %L conflicts with the typespec at %L",
3640 &tmp->where, &old_locus);
3641 goto cleanup;
3644 mold = tmp;
3645 tmp = NULL;
3646 saw_mold = true;
3647 mold->mold = 1;
3649 if (gfc_match_char (',') == MATCH_YES)
3650 goto alloc_opt_list;
3653 gfc_gobble_whitespace ();
3655 if (gfc_peek_char () == ')')
3656 break;
3659 if (gfc_match (" )%t") != MATCH_YES)
3660 goto syntax;
3662 /* Check F08:C637. */
3663 if (source && mold)
3665 gfc_error_1 ("MOLD tag at %L conflicts with SOURCE tag at %L",
3666 &mold->where, &source->where);
3667 goto cleanup;
3670 /* Check F03:C623, */
3671 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
3673 gfc_error ("Allocate-object at %L with a deferred type parameter "
3674 "requires either a type-spec or SOURCE tag or a MOLD tag",
3675 &deferred_locus);
3676 goto cleanup;
3679 /* Check F03:C625, */
3680 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
3682 for (tail = head; tail; tail = tail->next)
3684 if (UNLIMITED_POLY (tail->expr))
3685 gfc_error ("Unlimited polymorphic allocate-object at %L "
3686 "requires either a type-spec or SOURCE tag "
3687 "or a MOLD tag", &tail->expr->where);
3689 goto cleanup;
3692 new_st.op = EXEC_ALLOCATE;
3693 new_st.expr1 = stat;
3694 new_st.expr2 = errmsg;
3695 if (source)
3696 new_st.expr3 = source;
3697 else
3698 new_st.expr3 = mold;
3699 new_st.ext.alloc.list = head;
3700 new_st.ext.alloc.ts = ts;
3702 return MATCH_YES;
3704 syntax:
3705 gfc_syntax_error (ST_ALLOCATE);
3707 cleanup:
3708 gfc_free_expr (errmsg);
3709 gfc_free_expr (source);
3710 gfc_free_expr (stat);
3711 gfc_free_expr (mold);
3712 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
3713 gfc_free_alloc_list (head);
3714 return MATCH_ERROR;
3718 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
3719 a set of pointer assignments to intrinsic NULL(). */
3721 match
3722 gfc_match_nullify (void)
3724 gfc_code *tail;
3725 gfc_expr *e, *p;
3726 match m;
3728 tail = NULL;
3730 if (gfc_match_char ('(') != MATCH_YES)
3731 goto syntax;
3733 for (;;)
3735 m = gfc_match_variable (&p, 0);
3736 if (m == MATCH_ERROR)
3737 goto cleanup;
3738 if (m == MATCH_NO)
3739 goto syntax;
3741 if (gfc_check_do_variable (p->symtree))
3742 goto cleanup;
3744 /* F2008, C1242. */
3745 if (gfc_is_coindexed (p))
3747 gfc_error ("Pointer object at %C shall not be coindexed");
3748 goto cleanup;
3751 /* build ' => NULL() '. */
3752 e = gfc_get_null_expr (&gfc_current_locus);
3754 /* Chain to list. */
3755 if (tail == NULL)
3757 tail = &new_st;
3758 tail->op = EXEC_POINTER_ASSIGN;
3760 else
3762 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
3763 tail = tail->next;
3766 tail->expr1 = p;
3767 tail->expr2 = e;
3769 if (gfc_match (" )%t") == MATCH_YES)
3770 break;
3771 if (gfc_match_char (',') != MATCH_YES)
3772 goto syntax;
3775 return MATCH_YES;
3777 syntax:
3778 gfc_syntax_error (ST_NULLIFY);
3780 cleanup:
3781 gfc_free_statements (new_st.next);
3782 new_st.next = NULL;
3783 gfc_free_expr (new_st.expr1);
3784 new_st.expr1 = NULL;
3785 gfc_free_expr (new_st.expr2);
3786 new_st.expr2 = NULL;
3787 return MATCH_ERROR;
3791 /* Match a DEALLOCATE statement. */
3793 match
3794 gfc_match_deallocate (void)
3796 gfc_alloc *head, *tail;
3797 gfc_expr *stat, *errmsg, *tmp;
3798 gfc_symbol *sym;
3799 match m;
3800 bool saw_stat, saw_errmsg, b1, b2;
3802 head = tail = NULL;
3803 stat = errmsg = tmp = NULL;
3804 saw_stat = saw_errmsg = false;
3806 if (gfc_match_char ('(') != MATCH_YES)
3807 goto syntax;
3809 for (;;)
3811 if (head == NULL)
3812 head = tail = gfc_get_alloc ();
3813 else
3815 tail->next = gfc_get_alloc ();
3816 tail = tail->next;
3819 m = gfc_match_variable (&tail->expr, 0);
3820 if (m == MATCH_ERROR)
3821 goto cleanup;
3822 if (m == MATCH_NO)
3823 goto syntax;
3825 if (gfc_check_do_variable (tail->expr->symtree))
3826 goto cleanup;
3828 sym = tail->expr->symtree->n.sym;
3830 bool impure = gfc_impure_variable (sym);
3831 if (impure && gfc_pure (NULL))
3833 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
3834 goto cleanup;
3837 if (impure)
3838 gfc_unset_implicit_pure (NULL);
3840 if (gfc_is_coarray (tail->expr)
3841 && gfc_find_state (COMP_DO_CONCURRENT))
3843 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
3844 goto cleanup;
3847 if (gfc_is_coarray (tail->expr)
3848 && gfc_find_state (COMP_CRITICAL))
3850 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
3851 goto cleanup;
3854 /* FIXME: disable the checking on derived types. */
3855 b1 = !(tail->expr->ref
3856 && (tail->expr->ref->type == REF_COMPONENT
3857 || tail->expr->ref->type == REF_ARRAY));
3858 if (sym && sym->ts.type == BT_CLASS)
3859 b2 = !(CLASS_DATA (sym)->attr.allocatable
3860 || CLASS_DATA (sym)->attr.class_pointer);
3861 else
3862 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
3863 || sym->attr.proc_pointer);
3864 if (b1 && b2)
3866 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
3867 "nor an allocatable variable");
3868 goto cleanup;
3871 if (gfc_match_char (',') != MATCH_YES)
3872 break;
3874 dealloc_opt_list:
3876 m = gfc_match (" stat = %v", &tmp);
3877 if (m == MATCH_ERROR)
3878 goto cleanup;
3879 if (m == MATCH_YES)
3881 if (saw_stat)
3883 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3884 gfc_free_expr (tmp);
3885 goto cleanup;
3888 stat = tmp;
3889 saw_stat = true;
3891 if (gfc_check_do_variable (stat->symtree))
3892 goto cleanup;
3894 if (gfc_match_char (',') == MATCH_YES)
3895 goto dealloc_opt_list;
3898 m = gfc_match (" errmsg = %v", &tmp);
3899 if (m == MATCH_ERROR)
3900 goto cleanup;
3901 if (m == MATCH_YES)
3903 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
3904 goto cleanup;
3906 if (saw_errmsg)
3908 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3909 gfc_free_expr (tmp);
3910 goto cleanup;
3913 errmsg = tmp;
3914 saw_errmsg = true;
3916 if (gfc_match_char (',') == MATCH_YES)
3917 goto dealloc_opt_list;
3920 gfc_gobble_whitespace ();
3922 if (gfc_peek_char () == ')')
3923 break;
3926 if (gfc_match (" )%t") != MATCH_YES)
3927 goto syntax;
3929 new_st.op = EXEC_DEALLOCATE;
3930 new_st.expr1 = stat;
3931 new_st.expr2 = errmsg;
3932 new_st.ext.alloc.list = head;
3934 return MATCH_YES;
3936 syntax:
3937 gfc_syntax_error (ST_DEALLOCATE);
3939 cleanup:
3940 gfc_free_expr (errmsg);
3941 gfc_free_expr (stat);
3942 gfc_free_alloc_list (head);
3943 return MATCH_ERROR;
3947 /* Match a RETURN statement. */
3949 match
3950 gfc_match_return (void)
3952 gfc_expr *e;
3953 match m;
3954 gfc_compile_state s;
3956 e = NULL;
3958 if (gfc_find_state (COMP_CRITICAL))
3960 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
3961 return MATCH_ERROR;
3964 if (gfc_find_state (COMP_DO_CONCURRENT))
3966 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
3967 return MATCH_ERROR;
3970 if (gfc_match_eos () == MATCH_YES)
3971 goto done;
3973 if (!gfc_find_state (COMP_SUBROUTINE))
3975 gfc_error ("Alternate RETURN statement at %C is only allowed within "
3976 "a SUBROUTINE");
3977 goto cleanup;
3980 if (gfc_current_form == FORM_FREE)
3982 /* The following are valid, so we can't require a blank after the
3983 RETURN keyword:
3984 return+1
3985 return(1) */
3986 char c = gfc_peek_ascii_char ();
3987 if (ISALPHA (c) || ISDIGIT (c))
3988 return MATCH_NO;
3991 m = gfc_match (" %e%t", &e);
3992 if (m == MATCH_YES)
3993 goto done;
3994 if (m == MATCH_ERROR)
3995 goto cleanup;
3997 gfc_syntax_error (ST_RETURN);
3999 cleanup:
4000 gfc_free_expr (e);
4001 return MATCH_ERROR;
4003 done:
4004 gfc_enclosing_unit (&s);
4005 if (s == COMP_PROGRAM
4006 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4007 "main program at %C"))
4008 return MATCH_ERROR;
4010 new_st.op = EXEC_RETURN;
4011 new_st.expr1 = e;
4013 return MATCH_YES;
4017 /* Match the call of a type-bound procedure, if CALL%var has already been
4018 matched and var found to be a derived-type variable. */
4020 static match
4021 match_typebound_call (gfc_symtree* varst)
4023 gfc_expr* base;
4024 match m;
4026 base = gfc_get_expr ();
4027 base->expr_type = EXPR_VARIABLE;
4028 base->symtree = varst;
4029 base->where = gfc_current_locus;
4030 gfc_set_sym_referenced (varst->n.sym);
4032 m = gfc_match_varspec (base, 0, true, true);
4033 if (m == MATCH_NO)
4034 gfc_error ("Expected component reference at %C");
4035 if (m != MATCH_YES)
4037 gfc_free_expr (base);
4038 return MATCH_ERROR;
4041 if (gfc_match_eos () != MATCH_YES)
4043 gfc_error ("Junk after CALL at %C");
4044 gfc_free_expr (base);
4045 return MATCH_ERROR;
4048 if (base->expr_type == EXPR_COMPCALL)
4049 new_st.op = EXEC_COMPCALL;
4050 else if (base->expr_type == EXPR_PPC)
4051 new_st.op = EXEC_CALL_PPC;
4052 else
4054 gfc_error ("Expected type-bound procedure or procedure pointer component "
4055 "at %C");
4056 gfc_free_expr (base);
4057 return MATCH_ERROR;
4059 new_st.expr1 = base;
4061 return MATCH_YES;
4065 /* Match a CALL statement. The tricky part here are possible
4066 alternate return specifiers. We handle these by having all
4067 "subroutines" actually return an integer via a register that gives
4068 the return number. If the call specifies alternate returns, we
4069 generate code for a SELECT statement whose case clauses contain
4070 GOTOs to the various labels. */
4072 match
4073 gfc_match_call (void)
4075 char name[GFC_MAX_SYMBOL_LEN + 1];
4076 gfc_actual_arglist *a, *arglist;
4077 gfc_case *new_case;
4078 gfc_symbol *sym;
4079 gfc_symtree *st;
4080 gfc_code *c;
4081 match m;
4082 int i;
4084 arglist = NULL;
4086 m = gfc_match ("% %n", name);
4087 if (m == MATCH_NO)
4088 goto syntax;
4089 if (m != MATCH_YES)
4090 return m;
4092 if (gfc_get_ha_sym_tree (name, &st))
4093 return MATCH_ERROR;
4095 sym = st->n.sym;
4097 /* If this is a variable of derived-type, it probably starts a type-bound
4098 procedure call. */
4099 if ((sym->attr.flavor != FL_PROCEDURE
4100 || gfc_is_function_return_value (sym, gfc_current_ns))
4101 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
4102 return match_typebound_call (st);
4104 /* If it does not seem to be callable (include functions so that the
4105 right association is made. They are thrown out in resolution.)
4106 ... */
4107 if (!sym->attr.generic
4108 && !sym->attr.subroutine
4109 && !sym->attr.function)
4111 if (!(sym->attr.external && !sym->attr.referenced))
4113 /* ...create a symbol in this scope... */
4114 if (sym->ns != gfc_current_ns
4115 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
4116 return MATCH_ERROR;
4118 if (sym != st->n.sym)
4119 sym = st->n.sym;
4122 /* ...and then to try to make the symbol into a subroutine. */
4123 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
4124 return MATCH_ERROR;
4127 gfc_set_sym_referenced (sym);
4129 if (gfc_match_eos () != MATCH_YES)
4131 m = gfc_match_actual_arglist (1, &arglist);
4132 if (m == MATCH_NO)
4133 goto syntax;
4134 if (m == MATCH_ERROR)
4135 goto cleanup;
4137 if (gfc_match_eos () != MATCH_YES)
4138 goto syntax;
4141 /* If any alternate return labels were found, construct a SELECT
4142 statement that will jump to the right place. */
4144 i = 0;
4145 for (a = arglist; a; a = a->next)
4146 if (a->expr == NULL)
4148 i = 1;
4149 break;
4152 if (i)
4154 gfc_symtree *select_st;
4155 gfc_symbol *select_sym;
4156 char name[GFC_MAX_SYMBOL_LEN + 1];
4158 new_st.next = c = gfc_get_code (EXEC_SELECT);
4159 sprintf (name, "_result_%s", sym->name);
4160 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
4162 select_sym = select_st->n.sym;
4163 select_sym->ts.type = BT_INTEGER;
4164 select_sym->ts.kind = gfc_default_integer_kind;
4165 gfc_set_sym_referenced (select_sym);
4166 c->expr1 = gfc_get_expr ();
4167 c->expr1->expr_type = EXPR_VARIABLE;
4168 c->expr1->symtree = select_st;
4169 c->expr1->ts = select_sym->ts;
4170 c->expr1->where = gfc_current_locus;
4172 i = 0;
4173 for (a = arglist; a; a = a->next)
4175 if (a->expr != NULL)
4176 continue;
4178 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
4179 continue;
4181 i++;
4183 c->block = gfc_get_code (EXEC_SELECT);
4184 c = c->block;
4186 new_case = gfc_get_case ();
4187 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
4188 new_case->low = new_case->high;
4189 c->ext.block.case_list = new_case;
4191 c->next = gfc_get_code (EXEC_GOTO);
4192 c->next->label1 = a->label;
4196 new_st.op = EXEC_CALL;
4197 new_st.symtree = st;
4198 new_st.ext.actual = arglist;
4200 return MATCH_YES;
4202 syntax:
4203 gfc_syntax_error (ST_CALL);
4205 cleanup:
4206 gfc_free_actual_arglist (arglist);
4207 return MATCH_ERROR;
4211 /* Given a name, return a pointer to the common head structure,
4212 creating it if it does not exist. If FROM_MODULE is nonzero, we
4213 mangle the name so that it doesn't interfere with commons defined
4214 in the using namespace.
4215 TODO: Add to global symbol tree. */
4217 gfc_common_head *
4218 gfc_get_common (const char *name, int from_module)
4220 gfc_symtree *st;
4221 static int serial = 0;
4222 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
4224 if (from_module)
4226 /* A use associated common block is only needed to correctly layout
4227 the variables it contains. */
4228 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
4229 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
4231 else
4233 st = gfc_find_symtree (gfc_current_ns->common_root, name);
4235 if (st == NULL)
4236 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
4239 if (st->n.common == NULL)
4241 st->n.common = gfc_get_common_head ();
4242 st->n.common->where = gfc_current_locus;
4243 strcpy (st->n.common->name, name);
4246 return st->n.common;
4250 /* Match a common block name. */
4252 match match_common_name (char *name)
4254 match m;
4256 if (gfc_match_char ('/') == MATCH_NO)
4258 name[0] = '\0';
4259 return MATCH_YES;
4262 if (gfc_match_char ('/') == MATCH_YES)
4264 name[0] = '\0';
4265 return MATCH_YES;
4268 m = gfc_match_name (name);
4270 if (m == MATCH_ERROR)
4271 return MATCH_ERROR;
4272 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
4273 return MATCH_YES;
4275 gfc_error ("Syntax error in common block name at %C");
4276 return MATCH_ERROR;
4280 /* Match a COMMON statement. */
4282 match
4283 gfc_match_common (void)
4285 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
4286 char name[GFC_MAX_SYMBOL_LEN + 1];
4287 gfc_common_head *t;
4288 gfc_array_spec *as;
4289 gfc_equiv *e1, *e2;
4290 match m;
4292 old_blank_common = gfc_current_ns->blank_common.head;
4293 if (old_blank_common)
4295 while (old_blank_common->common_next)
4296 old_blank_common = old_blank_common->common_next;
4299 as = NULL;
4301 for (;;)
4303 m = match_common_name (name);
4304 if (m == MATCH_ERROR)
4305 goto cleanup;
4307 if (name[0] == '\0')
4309 t = &gfc_current_ns->blank_common;
4310 if (t->head == NULL)
4311 t->where = gfc_current_locus;
4313 else
4315 t = gfc_get_common (name, 0);
4317 head = &t->head;
4319 if (*head == NULL)
4320 tail = NULL;
4321 else
4323 tail = *head;
4324 while (tail->common_next)
4325 tail = tail->common_next;
4328 /* Grab the list of symbols. */
4329 for (;;)
4331 m = gfc_match_symbol (&sym, 0);
4332 if (m == MATCH_ERROR)
4333 goto cleanup;
4334 if (m == MATCH_NO)
4335 goto syntax;
4337 /* Store a ref to the common block for error checking. */
4338 sym->common_block = t;
4339 sym->common_block->refs++;
4341 /* See if we know the current common block is bind(c), and if
4342 so, then see if we can check if the symbol is (which it'll
4343 need to be). This can happen if the bind(c) attr stmt was
4344 applied to the common block, and the variable(s) already
4345 defined, before declaring the common block. */
4346 if (t->is_bind_c == 1)
4348 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
4350 /* If we find an error, just print it and continue,
4351 cause it's just semantic, and we can see if there
4352 are more errors. */
4353 gfc_error_now_1 ("Variable '%s' at %L in common block '%s' "
4354 "at %C must be declared with a C "
4355 "interoperable kind since common block "
4356 "'%s' is bind(c)",
4357 sym->name, &(sym->declared_at), t->name,
4358 t->name);
4361 if (sym->attr.is_bind_c == 1)
4362 gfc_error_now ("Variable %qs in common block %qs at %C can not "
4363 "be bind(c) since it is not global", sym->name,
4364 t->name);
4367 if (sym->attr.in_common)
4369 gfc_error ("Symbol %qs at %C is already in a COMMON block",
4370 sym->name);
4371 goto cleanup;
4374 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
4375 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
4377 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
4378 "%C can only be COMMON in BLOCK DATA",
4379 sym->name))
4380 goto cleanup;
4383 if (!gfc_add_in_common (&sym->attr, sym->name, NULL))
4384 goto cleanup;
4386 if (tail != NULL)
4387 tail->common_next = sym;
4388 else
4389 *head = sym;
4391 tail = sym;
4393 /* Deal with an optional array specification after the
4394 symbol name. */
4395 m = gfc_match_array_spec (&as, true, true);
4396 if (m == MATCH_ERROR)
4397 goto cleanup;
4399 if (m == MATCH_YES)
4401 if (as->type != AS_EXPLICIT)
4403 gfc_error ("Array specification for symbol %qs in COMMON "
4404 "at %C must be explicit", sym->name);
4405 goto cleanup;
4408 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
4409 goto cleanup;
4411 if (sym->attr.pointer)
4413 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
4414 "POINTER array", sym->name);
4415 goto cleanup;
4418 sym->as = as;
4419 as = NULL;
4423 sym->common_head = t;
4425 /* Check to see if the symbol is already in an equivalence group.
4426 If it is, set the other members as being in common. */
4427 if (sym->attr.in_equivalence)
4429 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
4431 for (e2 = e1; e2; e2 = e2->eq)
4432 if (e2->expr->symtree->n.sym == sym)
4433 goto equiv_found;
4435 continue;
4437 equiv_found:
4439 for (e2 = e1; e2; e2 = e2->eq)
4441 other = e2->expr->symtree->n.sym;
4442 if (other->common_head
4443 && other->common_head != sym->common_head)
4445 gfc_error ("Symbol %qs, in COMMON block %qs at "
4446 "%C is being indirectly equivalenced to "
4447 "another COMMON block %qs",
4448 sym->name, sym->common_head->name,
4449 other->common_head->name);
4450 goto cleanup;
4452 other->attr.in_common = 1;
4453 other->common_head = t;
4459 gfc_gobble_whitespace ();
4460 if (gfc_match_eos () == MATCH_YES)
4461 goto done;
4462 if (gfc_peek_ascii_char () == '/')
4463 break;
4464 if (gfc_match_char (',') != MATCH_YES)
4465 goto syntax;
4466 gfc_gobble_whitespace ();
4467 if (gfc_peek_ascii_char () == '/')
4468 break;
4472 done:
4473 return MATCH_YES;
4475 syntax:
4476 gfc_syntax_error (ST_COMMON);
4478 cleanup:
4479 gfc_free_array_spec (as);
4480 return MATCH_ERROR;
4484 /* Match a BLOCK DATA program unit. */
4486 match
4487 gfc_match_block_data (void)
4489 char name[GFC_MAX_SYMBOL_LEN + 1];
4490 gfc_symbol *sym;
4491 match m;
4493 if (gfc_match_eos () == MATCH_YES)
4495 gfc_new_block = NULL;
4496 return MATCH_YES;
4499 m = gfc_match ("% %n%t", name);
4500 if (m != MATCH_YES)
4501 return MATCH_ERROR;
4503 if (gfc_get_symbol (name, NULL, &sym))
4504 return MATCH_ERROR;
4506 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
4507 return MATCH_ERROR;
4509 gfc_new_block = sym;
4511 return MATCH_YES;
4515 /* Free a namelist structure. */
4517 void
4518 gfc_free_namelist (gfc_namelist *name)
4520 gfc_namelist *n;
4522 for (; name; name = n)
4524 n = name->next;
4525 free (name);
4530 /* Free an OpenMP namelist structure. */
4532 void
4533 gfc_free_omp_namelist (gfc_omp_namelist *name)
4535 gfc_omp_namelist *n;
4537 for (; name; name = n)
4539 gfc_free_expr (name->expr);
4540 if (name->udr)
4542 if (name->udr->combiner)
4543 gfc_free_statement (name->udr->combiner);
4544 if (name->udr->initializer)
4545 gfc_free_statement (name->udr->initializer);
4546 free (name->udr);
4548 n = name->next;
4549 free (name);
4554 /* Match a NAMELIST statement. */
4556 match
4557 gfc_match_namelist (void)
4559 gfc_symbol *group_name, *sym;
4560 gfc_namelist *nl;
4561 match m, m2;
4563 m = gfc_match (" / %s /", &group_name);
4564 if (m == MATCH_NO)
4565 goto syntax;
4566 if (m == MATCH_ERROR)
4567 goto error;
4569 for (;;)
4571 if (group_name->ts.type != BT_UNKNOWN)
4573 gfc_error ("Namelist group name %qs at %C already has a basic "
4574 "type of %s", group_name->name,
4575 gfc_typename (&group_name->ts));
4576 return MATCH_ERROR;
4579 if (group_name->attr.flavor == FL_NAMELIST
4580 && group_name->attr.use_assoc
4581 && !gfc_notify_std (GFC_STD_GNU, "Namelist group name %qs "
4582 "at %C already is USE associated and can"
4583 "not be respecified.", group_name->name))
4584 return MATCH_ERROR;
4586 if (group_name->attr.flavor != FL_NAMELIST
4587 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
4588 group_name->name, NULL))
4589 return MATCH_ERROR;
4591 for (;;)
4593 m = gfc_match_symbol (&sym, 1);
4594 if (m == MATCH_NO)
4595 goto syntax;
4596 if (m == MATCH_ERROR)
4597 goto error;
4599 if (sym->attr.in_namelist == 0
4600 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
4601 goto error;
4603 /* Use gfc_error_check here, rather than goto error, so that
4604 these are the only errors for the next two lines. */
4605 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
4607 gfc_error ("Assumed size array %qs in namelist %qs at "
4608 "%C is not allowed", sym->name, group_name->name);
4609 gfc_error_check ();
4612 nl = gfc_get_namelist ();
4613 nl->sym = sym;
4614 sym->refs++;
4616 if (group_name->namelist == NULL)
4617 group_name->namelist = group_name->namelist_tail = nl;
4618 else
4620 group_name->namelist_tail->next = nl;
4621 group_name->namelist_tail = nl;
4624 if (gfc_match_eos () == MATCH_YES)
4625 goto done;
4627 m = gfc_match_char (',');
4629 if (gfc_match_char ('/') == MATCH_YES)
4631 m2 = gfc_match (" %s /", &group_name);
4632 if (m2 == MATCH_YES)
4633 break;
4634 if (m2 == MATCH_ERROR)
4635 goto error;
4636 goto syntax;
4639 if (m != MATCH_YES)
4640 goto syntax;
4644 done:
4645 return MATCH_YES;
4647 syntax:
4648 gfc_syntax_error (ST_NAMELIST);
4650 error:
4651 return MATCH_ERROR;
4655 /* Match a MODULE statement. */
4657 match
4658 gfc_match_module (void)
4660 match m;
4662 m = gfc_match (" %s%t", &gfc_new_block);
4663 if (m != MATCH_YES)
4664 return m;
4666 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
4667 gfc_new_block->name, NULL))
4668 return MATCH_ERROR;
4670 return MATCH_YES;
4674 /* Free equivalence sets and lists. Recursively is the easiest way to
4675 do this. */
4677 void
4678 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
4680 if (eq == stop)
4681 return;
4683 gfc_free_equiv (eq->eq);
4684 gfc_free_equiv_until (eq->next, stop);
4685 gfc_free_expr (eq->expr);
4686 free (eq);
4690 void
4691 gfc_free_equiv (gfc_equiv *eq)
4693 gfc_free_equiv_until (eq, NULL);
4697 /* Match an EQUIVALENCE statement. */
4699 match
4700 gfc_match_equivalence (void)
4702 gfc_equiv *eq, *set, *tail;
4703 gfc_ref *ref;
4704 gfc_symbol *sym;
4705 match m;
4706 gfc_common_head *common_head = NULL;
4707 bool common_flag;
4708 int cnt;
4710 tail = NULL;
4712 for (;;)
4714 eq = gfc_get_equiv ();
4715 if (tail == NULL)
4716 tail = eq;
4718 eq->next = gfc_current_ns->equiv;
4719 gfc_current_ns->equiv = eq;
4721 if (gfc_match_char ('(') != MATCH_YES)
4722 goto syntax;
4724 set = eq;
4725 common_flag = FALSE;
4726 cnt = 0;
4728 for (;;)
4730 m = gfc_match_equiv_variable (&set->expr);
4731 if (m == MATCH_ERROR)
4732 goto cleanup;
4733 if (m == MATCH_NO)
4734 goto syntax;
4736 /* count the number of objects. */
4737 cnt++;
4739 if (gfc_match_char ('%') == MATCH_YES)
4741 gfc_error ("Derived type component %C is not a "
4742 "permitted EQUIVALENCE member");
4743 goto cleanup;
4746 for (ref = set->expr->ref; ref; ref = ref->next)
4747 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
4749 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
4750 "be an array section");
4751 goto cleanup;
4754 sym = set->expr->symtree->n.sym;
4756 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
4757 goto cleanup;
4759 if (sym->attr.in_common)
4761 common_flag = TRUE;
4762 common_head = sym->common_head;
4765 if (gfc_match_char (')') == MATCH_YES)
4766 break;
4768 if (gfc_match_char (',') != MATCH_YES)
4769 goto syntax;
4771 set->eq = gfc_get_equiv ();
4772 set = set->eq;
4775 if (cnt < 2)
4777 gfc_error ("EQUIVALENCE at %C requires two or more objects");
4778 goto cleanup;
4781 /* If one of the members of an equivalence is in common, then
4782 mark them all as being in common. Before doing this, check
4783 that members of the equivalence group are not in different
4784 common blocks. */
4785 if (common_flag)
4786 for (set = eq; set; set = set->eq)
4788 sym = set->expr->symtree->n.sym;
4789 if (sym->common_head && sym->common_head != common_head)
4791 gfc_error ("Attempt to indirectly overlap COMMON "
4792 "blocks %s and %s by EQUIVALENCE at %C",
4793 sym->common_head->name, common_head->name);
4794 goto cleanup;
4796 sym->attr.in_common = 1;
4797 sym->common_head = common_head;
4800 if (gfc_match_eos () == MATCH_YES)
4801 break;
4802 if (gfc_match_char (',') != MATCH_YES)
4804 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
4805 goto cleanup;
4809 return MATCH_YES;
4811 syntax:
4812 gfc_syntax_error (ST_EQUIVALENCE);
4814 cleanup:
4815 eq = tail->next;
4816 tail->next = NULL;
4818 gfc_free_equiv (gfc_current_ns->equiv);
4819 gfc_current_ns->equiv = eq;
4821 return MATCH_ERROR;
4825 /* Check that a statement function is not recursive. This is done by looking
4826 for the statement function symbol(sym) by looking recursively through its
4827 expression(e). If a reference to sym is found, true is returned.
4828 12.5.4 requires that any variable of function that is implicitly typed
4829 shall have that type confirmed by any subsequent type declaration. The
4830 implicit typing is conveniently done here. */
4831 static bool
4832 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
4834 static bool
4835 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
4838 if (e == NULL)
4839 return false;
4841 switch (e->expr_type)
4843 case EXPR_FUNCTION:
4844 if (e->symtree == NULL)
4845 return false;
4847 /* Check the name before testing for nested recursion! */
4848 if (sym->name == e->symtree->n.sym->name)
4849 return true;
4851 /* Catch recursion via other statement functions. */
4852 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
4853 && e->symtree->n.sym->value
4854 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
4855 return true;
4857 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
4858 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
4860 break;
4862 case EXPR_VARIABLE:
4863 if (e->symtree && sym->name == e->symtree->n.sym->name)
4864 return true;
4866 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
4867 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
4868 break;
4870 default:
4871 break;
4874 return false;
4878 static bool
4879 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
4881 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
4885 /* Match a statement function declaration. It is so easy to match
4886 non-statement function statements with a MATCH_ERROR as opposed to
4887 MATCH_NO that we suppress error message in most cases. */
4889 match
4890 gfc_match_st_function (void)
4892 gfc_error_buf old_error_1;
4893 output_buffer old_error;
4895 gfc_symbol *sym;
4896 gfc_expr *expr;
4897 match m;
4899 m = gfc_match_symbol (&sym, 0);
4900 if (m != MATCH_YES)
4901 return m;
4903 gfc_push_error (&old_error, &old_error_1);
4905 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
4906 goto undo_error;
4908 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
4909 goto undo_error;
4911 m = gfc_match (" = %e%t", &expr);
4912 if (m == MATCH_NO)
4913 goto undo_error;
4915 gfc_free_error (&old_error, &old_error_1);
4917 if (m == MATCH_ERROR)
4918 return m;
4920 if (recursive_stmt_fcn (expr, sym))
4922 gfc_error ("Statement function at %L is recursive", &expr->where);
4923 return MATCH_ERROR;
4926 sym->value = expr;
4928 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
4929 return MATCH_ERROR;
4931 return MATCH_YES;
4933 undo_error:
4934 gfc_pop_error (&old_error, &old_error_1);
4935 return MATCH_NO;
4939 /***************** SELECT CASE subroutines ******************/
4941 /* Free a single case structure. */
4943 static void
4944 free_case (gfc_case *p)
4946 if (p->low == p->high)
4947 p->high = NULL;
4948 gfc_free_expr (p->low);
4949 gfc_free_expr (p->high);
4950 free (p);
4954 /* Free a list of case structures. */
4956 void
4957 gfc_free_case_list (gfc_case *p)
4959 gfc_case *q;
4961 for (; p; p = q)
4963 q = p->next;
4964 free_case (p);
4969 /* Match a single case selector. */
4971 static match
4972 match_case_selector (gfc_case **cp)
4974 gfc_case *c;
4975 match m;
4977 c = gfc_get_case ();
4978 c->where = gfc_current_locus;
4980 if (gfc_match_char (':') == MATCH_YES)
4982 m = gfc_match_init_expr (&c->high);
4983 if (m == MATCH_NO)
4984 goto need_expr;
4985 if (m == MATCH_ERROR)
4986 goto cleanup;
4988 else
4990 m = gfc_match_init_expr (&c->low);
4991 if (m == MATCH_ERROR)
4992 goto cleanup;
4993 if (m == MATCH_NO)
4994 goto need_expr;
4996 /* If we're not looking at a ':' now, make a range out of a single
4997 target. Else get the upper bound for the case range. */
4998 if (gfc_match_char (':') != MATCH_YES)
4999 c->high = c->low;
5000 else
5002 m = gfc_match_init_expr (&c->high);
5003 if (m == MATCH_ERROR)
5004 goto cleanup;
5005 /* MATCH_NO is fine. It's OK if nothing is there! */
5009 *cp = c;
5010 return MATCH_YES;
5012 need_expr:
5013 gfc_error ("Expected initialization expression in CASE at %C");
5015 cleanup:
5016 free_case (c);
5017 return MATCH_ERROR;
5021 /* Match the end of a case statement. */
5023 static match
5024 match_case_eos (void)
5026 char name[GFC_MAX_SYMBOL_LEN + 1];
5027 match m;
5029 if (gfc_match_eos () == MATCH_YES)
5030 return MATCH_YES;
5032 /* If the case construct doesn't have a case-construct-name, we
5033 should have matched the EOS. */
5034 if (!gfc_current_block ())
5035 return MATCH_NO;
5037 gfc_gobble_whitespace ();
5039 m = gfc_match_name (name);
5040 if (m != MATCH_YES)
5041 return m;
5043 if (strcmp (name, gfc_current_block ()->name) != 0)
5045 gfc_error ("Expected block name %qs of SELECT construct at %C",
5046 gfc_current_block ()->name);
5047 return MATCH_ERROR;
5050 return gfc_match_eos ();
5054 /* Match a SELECT statement. */
5056 match
5057 gfc_match_select (void)
5059 gfc_expr *expr;
5060 match m;
5062 m = gfc_match_label ();
5063 if (m == MATCH_ERROR)
5064 return m;
5066 m = gfc_match (" select case ( %e )%t", &expr);
5067 if (m != MATCH_YES)
5068 return m;
5070 new_st.op = EXEC_SELECT;
5071 new_st.expr1 = expr;
5073 return MATCH_YES;
5077 /* Transfer the selector typespec to the associate name. */
5079 static void
5080 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
5082 gfc_ref *ref;
5083 gfc_symbol *assoc_sym;
5085 assoc_sym = associate->symtree->n.sym;
5087 /* At this stage the expression rank and arrayspec dimensions have
5088 not been completely sorted out. We must get the expr2->rank
5089 right here, so that the correct class container is obtained. */
5090 ref = selector->ref;
5091 while (ref && ref->next)
5092 ref = ref->next;
5094 if (selector->ts.type == BT_CLASS && CLASS_DATA (selector)->as
5095 && ref && ref->type == REF_ARRAY)
5097 /* Ensure that the array reference type is set. We cannot use
5098 gfc_resolve_expr at this point, so the usable parts of
5099 resolve.c(resolve_array_ref) are employed to do it. */
5100 if (ref->u.ar.type == AR_UNKNOWN)
5102 ref->u.ar.type = AR_ELEMENT;
5103 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
5104 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5105 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
5106 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
5107 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
5109 ref->u.ar.type = AR_SECTION;
5110 break;
5114 if (ref->u.ar.type == AR_FULL)
5115 selector->rank = CLASS_DATA (selector)->as->rank;
5116 else if (ref->u.ar.type == AR_SECTION)
5117 selector->rank = ref->u.ar.dimen;
5118 else
5119 selector->rank = 0;
5122 if (selector->rank)
5124 assoc_sym->attr.dimension = 1;
5125 assoc_sym->as = gfc_get_array_spec ();
5126 assoc_sym->as->rank = selector->rank;
5127 assoc_sym->as->type = AS_DEFERRED;
5129 else
5130 assoc_sym->as = NULL;
5132 if (selector->ts.type == BT_CLASS)
5134 /* The correct class container has to be available. */
5135 assoc_sym->ts.type = BT_CLASS;
5136 assoc_sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
5137 assoc_sym->attr.pointer = 1;
5138 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
5143 /* Push the current selector onto the SELECT TYPE stack. */
5145 static void
5146 select_type_push (gfc_symbol *sel)
5148 gfc_select_type_stack *top = gfc_get_select_type_stack ();
5149 top->selector = sel;
5150 top->tmp = NULL;
5151 top->prev = select_type_stack;
5153 select_type_stack = top;
5157 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
5159 static gfc_symtree *
5160 select_intrinsic_set_tmp (gfc_typespec *ts)
5162 char name[GFC_MAX_SYMBOL_LEN];
5163 gfc_symtree *tmp;
5164 int charlen = 0;
5166 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
5167 return NULL;
5169 if (select_type_stack->selector->ts.type == BT_CLASS
5170 && !select_type_stack->selector->attr.class_ok)
5171 return NULL;
5173 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
5174 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
5175 charlen = mpz_get_si (ts->u.cl->length->value.integer);
5177 if (ts->type != BT_CHARACTER)
5178 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
5179 ts->kind);
5180 else
5181 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (ts->type),
5182 charlen, ts->kind);
5184 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5185 gfc_add_type (tmp->n.sym, ts, NULL);
5187 /* Copy across the array spec to the selector. */
5188 if (select_type_stack->selector->ts.type == BT_CLASS
5189 && (CLASS_DATA (select_type_stack->selector)->attr.dimension
5190 || CLASS_DATA (select_type_stack->selector)->attr.codimension))
5192 tmp->n.sym->attr.pointer = 1;
5193 tmp->n.sym->attr.dimension
5194 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5195 tmp->n.sym->attr.codimension
5196 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5197 tmp->n.sym->as
5198 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5201 gfc_set_sym_referenced (tmp->n.sym);
5202 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5203 tmp->n.sym->attr.select_type_temporary = 1;
5205 return tmp;
5209 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
5211 static void
5212 select_type_set_tmp (gfc_typespec *ts)
5214 char name[GFC_MAX_SYMBOL_LEN];
5215 gfc_symtree *tmp = NULL;
5217 if (!ts)
5219 select_type_stack->tmp = NULL;
5220 return;
5223 tmp = select_intrinsic_set_tmp (ts);
5225 if (tmp == NULL)
5227 if (!ts->u.derived)
5228 return;
5230 if (ts->type == BT_CLASS)
5231 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
5232 else
5233 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
5234 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5235 gfc_add_type (tmp->n.sym, ts, NULL);
5237 if (select_type_stack->selector->ts.type == BT_CLASS
5238 && select_type_stack->selector->attr.class_ok)
5240 tmp->n.sym->attr.pointer
5241 = CLASS_DATA (select_type_stack->selector)->attr.class_pointer;
5243 /* Copy across the array spec to the selector. */
5244 if (CLASS_DATA (select_type_stack->selector)->attr.dimension
5245 || CLASS_DATA (select_type_stack->selector)->attr.codimension)
5247 tmp->n.sym->attr.dimension
5248 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5249 tmp->n.sym->attr.codimension
5250 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5251 tmp->n.sym->as
5252 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5256 gfc_set_sym_referenced (tmp->n.sym);
5257 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5258 tmp->n.sym->attr.select_type_temporary = 1;
5260 if (ts->type == BT_CLASS)
5261 gfc_build_class_symbol (&tmp->n.sym->ts, &tmp->n.sym->attr,
5262 &tmp->n.sym->as);
5265 /* Add an association for it, so the rest of the parser knows it is
5266 an associate-name. The target will be set during resolution. */
5267 tmp->n.sym->assoc = gfc_get_association_list ();
5268 tmp->n.sym->assoc->dangling = 1;
5269 tmp->n.sym->assoc->st = tmp;
5271 select_type_stack->tmp = tmp;
5275 /* Match a SELECT TYPE statement. */
5277 match
5278 gfc_match_select_type (void)
5280 gfc_expr *expr1, *expr2 = NULL;
5281 match m;
5282 char name[GFC_MAX_SYMBOL_LEN];
5283 bool class_array;
5284 gfc_symbol *sym;
5286 m = gfc_match_label ();
5287 if (m == MATCH_ERROR)
5288 return m;
5290 m = gfc_match (" select type ( ");
5291 if (m != MATCH_YES)
5292 return m;
5294 m = gfc_match (" %n => %e", name, &expr2);
5295 if (m == MATCH_YES)
5297 expr1 = gfc_get_expr();
5298 expr1->expr_type = EXPR_VARIABLE;
5299 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
5301 m = MATCH_ERROR;
5302 goto cleanup;
5305 sym = expr1->symtree->n.sym;
5306 if (expr2->ts.type == BT_UNKNOWN)
5307 sym->attr.untyped = 1;
5308 else
5309 copy_ts_from_selector_to_associate (expr1, expr2);
5311 sym->attr.flavor = FL_VARIABLE;
5312 sym->attr.referenced = 1;
5313 sym->attr.class_ok = 1;
5315 else
5317 m = gfc_match (" %e ", &expr1);
5318 if (m != MATCH_YES)
5319 return m;
5322 m = gfc_match (" )%t");
5323 if (m != MATCH_YES)
5325 gfc_error ("parse error in SELECT TYPE statement at %C");
5326 goto cleanup;
5329 /* This ghastly expression seems to be needed to distinguish a CLASS
5330 array, which can have a reference, from other expressions that
5331 have references, such as derived type components, and are not
5332 allowed by the standard.
5333 TODO: see if it is sufficient to exclude component and substring
5334 references. */
5335 class_array = expr1->expr_type == EXPR_VARIABLE
5336 && expr1->ts.type == BT_CLASS
5337 && CLASS_DATA (expr1)
5338 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
5339 && (CLASS_DATA (expr1)->attr.dimension
5340 || CLASS_DATA (expr1)->attr.codimension)
5341 && expr1->ref
5342 && expr1->ref->type == REF_ARRAY
5343 && expr1->ref->next == NULL;
5345 /* Check for F03:C811. */
5346 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
5347 || (!class_array && expr1->ref != NULL)))
5349 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
5350 "use associate-name=>");
5351 m = MATCH_ERROR;
5352 goto cleanup;
5355 new_st.op = EXEC_SELECT_TYPE;
5356 new_st.expr1 = expr1;
5357 new_st.expr2 = expr2;
5358 new_st.ext.block.ns = gfc_current_ns;
5360 select_type_push (expr1->symtree->n.sym);
5362 return MATCH_YES;
5364 cleanup:
5365 gfc_free_expr (expr1);
5366 gfc_free_expr (expr2);
5367 return m;
5371 /* Match a CASE statement. */
5373 match
5374 gfc_match_case (void)
5376 gfc_case *c, *head, *tail;
5377 match m;
5379 head = tail = NULL;
5381 if (gfc_current_state () != COMP_SELECT)
5383 gfc_error ("Unexpected CASE statement at %C");
5384 return MATCH_ERROR;
5387 if (gfc_match ("% default") == MATCH_YES)
5389 m = match_case_eos ();
5390 if (m == MATCH_NO)
5391 goto syntax;
5392 if (m == MATCH_ERROR)
5393 goto cleanup;
5395 new_st.op = EXEC_SELECT;
5396 c = gfc_get_case ();
5397 c->where = gfc_current_locus;
5398 new_st.ext.block.case_list = c;
5399 return MATCH_YES;
5402 if (gfc_match_char ('(') != MATCH_YES)
5403 goto syntax;
5405 for (;;)
5407 if (match_case_selector (&c) == MATCH_ERROR)
5408 goto cleanup;
5410 if (head == NULL)
5411 head = c;
5412 else
5413 tail->next = c;
5415 tail = c;
5417 if (gfc_match_char (')') == MATCH_YES)
5418 break;
5419 if (gfc_match_char (',') != MATCH_YES)
5420 goto syntax;
5423 m = match_case_eos ();
5424 if (m == MATCH_NO)
5425 goto syntax;
5426 if (m == MATCH_ERROR)
5427 goto cleanup;
5429 new_st.op = EXEC_SELECT;
5430 new_st.ext.block.case_list = head;
5432 return MATCH_YES;
5434 syntax:
5435 gfc_error ("Syntax error in CASE specification at %C");
5437 cleanup:
5438 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
5439 return MATCH_ERROR;
5443 /* Match a TYPE IS statement. */
5445 match
5446 gfc_match_type_is (void)
5448 gfc_case *c = NULL;
5449 match m;
5451 if (gfc_current_state () != COMP_SELECT_TYPE)
5453 gfc_error ("Unexpected TYPE IS statement at %C");
5454 return MATCH_ERROR;
5457 if (gfc_match_char ('(') != MATCH_YES)
5458 goto syntax;
5460 c = gfc_get_case ();
5461 c->where = gfc_current_locus;
5463 if (gfc_match_type_spec (&c->ts) == MATCH_ERROR)
5464 goto cleanup;
5466 if (gfc_match_char (')') != MATCH_YES)
5467 goto syntax;
5469 m = match_case_eos ();
5470 if (m == MATCH_NO)
5471 goto syntax;
5472 if (m == MATCH_ERROR)
5473 goto cleanup;
5475 new_st.op = EXEC_SELECT_TYPE;
5476 new_st.ext.block.case_list = c;
5478 if (c->ts.type == BT_DERIVED && c->ts.u.derived
5479 && (c->ts.u.derived->attr.sequence
5480 || c->ts.u.derived->attr.is_bind_c))
5482 gfc_error ("The type-spec shall not specify a sequence derived "
5483 "type or a type with the BIND attribute in SELECT "
5484 "TYPE at %C [F2003:C815]");
5485 return MATCH_ERROR;
5488 /* Create temporary variable. */
5489 select_type_set_tmp (&c->ts);
5491 return MATCH_YES;
5493 syntax:
5494 gfc_error ("Syntax error in TYPE IS specification at %C");
5496 cleanup:
5497 if (c != NULL)
5498 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
5499 return MATCH_ERROR;
5503 /* Match a CLASS IS or CLASS DEFAULT statement. */
5505 match
5506 gfc_match_class_is (void)
5508 gfc_case *c = NULL;
5509 match m;
5511 if (gfc_current_state () != COMP_SELECT_TYPE)
5512 return MATCH_NO;
5514 if (gfc_match ("% default") == MATCH_YES)
5516 m = match_case_eos ();
5517 if (m == MATCH_NO)
5518 goto syntax;
5519 if (m == MATCH_ERROR)
5520 goto cleanup;
5522 new_st.op = EXEC_SELECT_TYPE;
5523 c = gfc_get_case ();
5524 c->where = gfc_current_locus;
5525 c->ts.type = BT_UNKNOWN;
5526 new_st.ext.block.case_list = c;
5527 select_type_set_tmp (NULL);
5528 return MATCH_YES;
5531 m = gfc_match ("% is");
5532 if (m == MATCH_NO)
5533 goto syntax;
5534 if (m == MATCH_ERROR)
5535 goto cleanup;
5537 if (gfc_match_char ('(') != MATCH_YES)
5538 goto syntax;
5540 c = gfc_get_case ();
5541 c->where = gfc_current_locus;
5543 if (match_derived_type_spec (&c->ts) == MATCH_ERROR)
5544 goto cleanup;
5546 if (c->ts.type == BT_DERIVED)
5547 c->ts.type = BT_CLASS;
5549 if (gfc_match_char (')') != MATCH_YES)
5550 goto syntax;
5552 m = match_case_eos ();
5553 if (m == MATCH_NO)
5554 goto syntax;
5555 if (m == MATCH_ERROR)
5556 goto cleanup;
5558 new_st.op = EXEC_SELECT_TYPE;
5559 new_st.ext.block.case_list = c;
5561 /* Create temporary variable. */
5562 select_type_set_tmp (&c->ts);
5564 return MATCH_YES;
5566 syntax:
5567 gfc_error ("Syntax error in CLASS IS specification at %C");
5569 cleanup:
5570 if (c != NULL)
5571 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
5572 return MATCH_ERROR;
5576 /********************* WHERE subroutines ********************/
5578 /* Match the rest of a simple WHERE statement that follows an IF statement.
5581 static match
5582 match_simple_where (void)
5584 gfc_expr *expr;
5585 gfc_code *c;
5586 match m;
5588 m = gfc_match (" ( %e )", &expr);
5589 if (m != MATCH_YES)
5590 return m;
5592 m = gfc_match_assignment ();
5593 if (m == MATCH_NO)
5594 goto syntax;
5595 if (m == MATCH_ERROR)
5596 goto cleanup;
5598 if (gfc_match_eos () != MATCH_YES)
5599 goto syntax;
5601 c = gfc_get_code (EXEC_WHERE);
5602 c->expr1 = expr;
5604 c->next = XCNEW (gfc_code);
5605 *c->next = new_st;
5606 gfc_clear_new_st ();
5608 new_st.op = EXEC_WHERE;
5609 new_st.block = c;
5611 return MATCH_YES;
5613 syntax:
5614 gfc_syntax_error (ST_WHERE);
5616 cleanup:
5617 gfc_free_expr (expr);
5618 return MATCH_ERROR;
5622 /* Match a WHERE statement. */
5624 match
5625 gfc_match_where (gfc_statement *st)
5627 gfc_expr *expr;
5628 match m0, m;
5629 gfc_code *c;
5631 m0 = gfc_match_label ();
5632 if (m0 == MATCH_ERROR)
5633 return m0;
5635 m = gfc_match (" where ( %e )", &expr);
5636 if (m != MATCH_YES)
5637 return m;
5639 if (gfc_match_eos () == MATCH_YES)
5641 *st = ST_WHERE_BLOCK;
5642 new_st.op = EXEC_WHERE;
5643 new_st.expr1 = expr;
5644 return MATCH_YES;
5647 m = gfc_match_assignment ();
5648 if (m == MATCH_NO)
5649 gfc_syntax_error (ST_WHERE);
5651 if (m != MATCH_YES)
5653 gfc_free_expr (expr);
5654 return MATCH_ERROR;
5657 /* We've got a simple WHERE statement. */
5658 *st = ST_WHERE;
5659 c = gfc_get_code (EXEC_WHERE);
5660 c->expr1 = expr;
5662 c->next = XCNEW (gfc_code);
5663 *c->next = new_st;
5664 gfc_clear_new_st ();
5666 new_st.op = EXEC_WHERE;
5667 new_st.block = c;
5669 return MATCH_YES;
5673 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
5674 new_st if successful. */
5676 match
5677 gfc_match_elsewhere (void)
5679 char name[GFC_MAX_SYMBOL_LEN + 1];
5680 gfc_expr *expr;
5681 match m;
5683 if (gfc_current_state () != COMP_WHERE)
5685 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
5686 return MATCH_ERROR;
5689 expr = NULL;
5691 if (gfc_match_char ('(') == MATCH_YES)
5693 m = gfc_match_expr (&expr);
5694 if (m == MATCH_NO)
5695 goto syntax;
5696 if (m == MATCH_ERROR)
5697 return MATCH_ERROR;
5699 if (gfc_match_char (')') != MATCH_YES)
5700 goto syntax;
5703 if (gfc_match_eos () != MATCH_YES)
5705 /* Only makes sense if we have a where-construct-name. */
5706 if (!gfc_current_block ())
5708 m = MATCH_ERROR;
5709 goto cleanup;
5711 /* Better be a name at this point. */
5712 m = gfc_match_name (name);
5713 if (m == MATCH_NO)
5714 goto syntax;
5715 if (m == MATCH_ERROR)
5716 goto cleanup;
5718 if (gfc_match_eos () != MATCH_YES)
5719 goto syntax;
5721 if (strcmp (name, gfc_current_block ()->name) != 0)
5723 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
5724 name, gfc_current_block ()->name);
5725 goto cleanup;
5729 new_st.op = EXEC_WHERE;
5730 new_st.expr1 = expr;
5731 return MATCH_YES;
5733 syntax:
5734 gfc_syntax_error (ST_ELSEWHERE);
5736 cleanup:
5737 gfc_free_expr (expr);
5738 return MATCH_ERROR;