2013-02-21 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / match.c
blobd75cf1cc17d12e12c610c36eeb2392acb686c7b7
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2013 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 "tree.h"
30 int gfc_matching_ptr_assignment = 0;
31 int gfc_matching_procptr_assignment = 0;
32 bool gfc_matching_prefix = false;
34 /* Stack of SELECT TYPE statements. */
35 gfc_select_type_stack *select_type_stack = NULL;
37 /* For debugging and diagnostic purposes. Return the textual representation
38 of the intrinsic operator OP. */
39 const char *
40 gfc_op2string (gfc_intrinsic_op op)
42 switch (op)
44 case INTRINSIC_UPLUS:
45 case INTRINSIC_PLUS:
46 return "+";
48 case INTRINSIC_UMINUS:
49 case INTRINSIC_MINUS:
50 return "-";
52 case INTRINSIC_POWER:
53 return "**";
54 case INTRINSIC_CONCAT:
55 return "//";
56 case INTRINSIC_TIMES:
57 return "*";
58 case INTRINSIC_DIVIDE:
59 return "/";
61 case INTRINSIC_AND:
62 return ".and.";
63 case INTRINSIC_OR:
64 return ".or.";
65 case INTRINSIC_EQV:
66 return ".eqv.";
67 case INTRINSIC_NEQV:
68 return ".neqv.";
70 case INTRINSIC_EQ_OS:
71 return ".eq.";
72 case INTRINSIC_EQ:
73 return "==";
74 case INTRINSIC_NE_OS:
75 return ".ne.";
76 case INTRINSIC_NE:
77 return "/=";
78 case INTRINSIC_GE_OS:
79 return ".ge.";
80 case INTRINSIC_GE:
81 return ">=";
82 case INTRINSIC_LE_OS:
83 return ".le.";
84 case INTRINSIC_LE:
85 return "<=";
86 case INTRINSIC_LT_OS:
87 return ".lt.";
88 case INTRINSIC_LT:
89 return "<";
90 case INTRINSIC_GT_OS:
91 return ".gt.";
92 case INTRINSIC_GT:
93 return ">";
94 case INTRINSIC_NOT:
95 return ".not.";
97 case INTRINSIC_ASSIGN:
98 return "=";
100 case INTRINSIC_PARENTHESES:
101 return "parens";
103 default:
104 break;
107 gfc_internal_error ("gfc_op2string(): Bad code");
108 /* Not reached. */
112 /******************** Generic matching subroutines ************************/
114 /* This function scans the current statement counting the opened and closed
115 parenthesis to make sure they are balanced. */
117 match
118 gfc_match_parens (void)
120 locus old_loc, where;
121 int count;
122 gfc_instring instring;
123 gfc_char_t c, quote;
125 old_loc = gfc_current_locus;
126 count = 0;
127 instring = NONSTRING;
128 quote = ' ';
130 for (;;)
132 c = gfc_next_char_literal (instring);
133 if (c == '\n')
134 break;
135 if (quote == ' ' && ((c == '\'') || (c == '"')))
137 quote = c;
138 instring = INSTRING_WARN;
139 continue;
141 if (quote != ' ' && c == quote)
143 quote = ' ';
144 instring = NONSTRING;
145 continue;
148 if (c == '(' && quote == ' ')
150 count++;
151 where = gfc_current_locus;
153 if (c == ')' && quote == ' ')
155 count--;
156 where = gfc_current_locus;
160 gfc_current_locus = old_loc;
162 if (count > 0)
164 gfc_error ("Missing ')' in statement at or before %L", &where);
165 return MATCH_ERROR;
167 if (count < 0)
169 gfc_error ("Missing '(' in statement at or before %L", &where);
170 return MATCH_ERROR;
173 return MATCH_YES;
177 /* See if the next character is a special character that has
178 escaped by a \ via the -fbackslash option. */
180 match
181 gfc_match_special_char (gfc_char_t *res)
183 int len, i;
184 gfc_char_t c, n;
185 match m;
187 m = MATCH_YES;
189 switch ((c = gfc_next_char_literal (INSTRING_WARN)))
191 case 'a':
192 *res = '\a';
193 break;
194 case 'b':
195 *res = '\b';
196 break;
197 case 't':
198 *res = '\t';
199 break;
200 case 'f':
201 *res = '\f';
202 break;
203 case 'n':
204 *res = '\n';
205 break;
206 case 'r':
207 *res = '\r';
208 break;
209 case 'v':
210 *res = '\v';
211 break;
212 case '\\':
213 *res = '\\';
214 break;
215 case '0':
216 *res = '\0';
217 break;
219 case 'x':
220 case 'u':
221 case 'U':
222 /* Hexadecimal form of wide characters. */
223 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
224 n = 0;
225 for (i = 0; i < len; i++)
227 char buf[2] = { '\0', '\0' };
229 c = gfc_next_char_literal (INSTRING_WARN);
230 if (!gfc_wide_fits_in_byte (c)
231 || !gfc_check_digit ((unsigned char) c, 16))
232 return MATCH_NO;
234 buf[0] = (unsigned char) c;
235 n = n << 4;
236 n += strtol (buf, NULL, 16);
238 *res = n;
239 break;
241 default:
242 /* Unknown backslash codes are simply not expanded. */
243 m = MATCH_NO;
244 break;
247 return m;
251 /* In free form, match at least one space. Always matches in fixed
252 form. */
254 match
255 gfc_match_space (void)
257 locus old_loc;
258 char c;
260 if (gfc_current_form == FORM_FIXED)
261 return MATCH_YES;
263 old_loc = gfc_current_locus;
265 c = gfc_next_ascii_char ();
266 if (!gfc_is_whitespace (c))
268 gfc_current_locus = old_loc;
269 return MATCH_NO;
272 gfc_gobble_whitespace ();
274 return MATCH_YES;
278 /* Match an end of statement. End of statement is optional
279 whitespace, followed by a ';' or '\n' or comment '!'. If a
280 semicolon is found, we continue to eat whitespace and semicolons. */
282 match
283 gfc_match_eos (void)
285 locus old_loc;
286 int flag;
287 char c;
289 flag = 0;
291 for (;;)
293 old_loc = gfc_current_locus;
294 gfc_gobble_whitespace ();
296 c = gfc_next_ascii_char ();
297 switch (c)
299 case '!':
302 c = gfc_next_ascii_char ();
304 while (c != '\n');
306 /* Fall through. */
308 case '\n':
309 return MATCH_YES;
311 case ';':
312 flag = 1;
313 continue;
316 break;
319 gfc_current_locus = old_loc;
320 return (flag) ? MATCH_YES : MATCH_NO;
324 /* Match a literal integer on the input, setting the value on
325 MATCH_YES. Literal ints occur in kind-parameters as well as
326 old-style character length specifications. If cnt is non-NULL it
327 will be set to the number of digits. */
329 match
330 gfc_match_small_literal_int (int *value, int *cnt)
332 locus old_loc;
333 char c;
334 int i, j;
336 old_loc = gfc_current_locus;
338 *value = -1;
339 gfc_gobble_whitespace ();
340 c = gfc_next_ascii_char ();
341 if (cnt)
342 *cnt = 0;
344 if (!ISDIGIT (c))
346 gfc_current_locus = old_loc;
347 return MATCH_NO;
350 i = c - '0';
351 j = 1;
353 for (;;)
355 old_loc = gfc_current_locus;
356 c = gfc_next_ascii_char ();
358 if (!ISDIGIT (c))
359 break;
361 i = 10 * i + c - '0';
362 j++;
364 if (i > 99999999)
366 gfc_error ("Integer too large at %C");
367 return MATCH_ERROR;
371 gfc_current_locus = old_loc;
373 *value = i;
374 if (cnt)
375 *cnt = j;
376 return MATCH_YES;
380 /* Match a small, constant integer expression, like in a kind
381 statement. On MATCH_YES, 'value' is set. */
383 match
384 gfc_match_small_int (int *value)
386 gfc_expr *expr;
387 const char *p;
388 match m;
389 int i;
391 m = gfc_match_expr (&expr);
392 if (m != MATCH_YES)
393 return m;
395 p = gfc_extract_int (expr, &i);
396 gfc_free_expr (expr);
398 if (p != NULL)
400 gfc_error (p);
401 m = MATCH_ERROR;
404 *value = i;
405 return m;
409 /* This function is the same as the gfc_match_small_int, except that
410 we're keeping the pointer to the expr. This function could just be
411 removed and the previously mentioned one modified, though all calls
412 to it would have to be modified then (and there were a number of
413 them). Return MATCH_ERROR if fail to extract the int; otherwise,
414 return the result of gfc_match_expr(). The expr (if any) that was
415 matched is returned in the parameter expr. */
417 match
418 gfc_match_small_int_expr (int *value, gfc_expr **expr)
420 const char *p;
421 match m;
422 int i;
424 m = gfc_match_expr (expr);
425 if (m != MATCH_YES)
426 return m;
428 p = gfc_extract_int (*expr, &i);
430 if (p != NULL)
432 gfc_error (p);
433 m = MATCH_ERROR;
436 *value = i;
437 return m;
441 /* Matches a statement label. Uses gfc_match_small_literal_int() to
442 do most of the work. */
444 match
445 gfc_match_st_label (gfc_st_label **label)
447 locus old_loc;
448 match m;
449 int i, cnt;
451 old_loc = gfc_current_locus;
453 m = gfc_match_small_literal_int (&i, &cnt);
454 if (m != MATCH_YES)
455 return m;
457 if (cnt > 5)
459 gfc_error ("Too many digits in statement label at %C");
460 goto cleanup;
463 if (i == 0)
465 gfc_error ("Statement label at %C is zero");
466 goto cleanup;
469 *label = gfc_get_st_label (i);
470 return MATCH_YES;
472 cleanup:
474 gfc_current_locus = old_loc;
475 return MATCH_ERROR;
479 /* Match and validate a label associated with a named IF, DO or SELECT
480 statement. If the symbol does not have the label attribute, we add
481 it. We also make sure the symbol does not refer to another
482 (active) block. A matched label is pointed to by gfc_new_block. */
484 match
485 gfc_match_label (void)
487 char name[GFC_MAX_SYMBOL_LEN + 1];
488 match m;
490 gfc_new_block = NULL;
492 m = gfc_match (" %n :", name);
493 if (m != MATCH_YES)
494 return m;
496 if (gfc_get_symbol (name, NULL, &gfc_new_block))
498 gfc_error ("Label name '%s' at %C is ambiguous", name);
499 return MATCH_ERROR;
502 if (gfc_new_block->attr.flavor == FL_LABEL)
504 gfc_error ("Duplicate construct label '%s' at %C", name);
505 return MATCH_ERROR;
508 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
509 gfc_new_block->name, NULL) == FAILURE)
510 return MATCH_ERROR;
512 return MATCH_YES;
516 /* See if the current input looks like a name of some sort. Modifies
517 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
518 Note that options.c restricts max_identifier_length to not more
519 than GFC_MAX_SYMBOL_LEN. */
521 match
522 gfc_match_name (char *buffer)
524 locus old_loc;
525 int i;
526 char c;
528 old_loc = gfc_current_locus;
529 gfc_gobble_whitespace ();
531 c = gfc_next_ascii_char ();
532 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
534 if (gfc_error_flag_test() == 0 && c != '(')
535 gfc_error ("Invalid character in name at %C");
536 gfc_current_locus = old_loc;
537 return MATCH_NO;
540 i = 0;
544 buffer[i++] = c;
546 if (i > gfc_option.max_identifier_length)
548 gfc_error ("Name at %C is too long");
549 return MATCH_ERROR;
552 old_loc = gfc_current_locus;
553 c = gfc_next_ascii_char ();
555 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
557 if (c == '$' && !gfc_option.flag_dollar_ok)
559 gfc_error ("Invalid character '$' at %C. Use -fdollar-ok to allow it "
560 "as an extension");
561 return MATCH_ERROR;
564 buffer[i] = '\0';
565 gfc_current_locus = old_loc;
567 return MATCH_YES;
571 /* Match a valid name for C, which is almost the same as for Fortran,
572 except that you can start with an underscore, etc.. It could have
573 been done by modifying the gfc_match_name, but this way other
574 things C allows can be done, such as no limits on the length.
575 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
576 input characters from being automatically lower cased, since C is
577 case sensitive. The parameter, buffer, is used to return the name
578 that is matched. Return MATCH_ERROR if the name is not a valid C
579 name, MATCH_NO if what we're seeing isn't a name, and MATCH_YES if
580 we successfully match a C name. */
582 match
583 gfc_match_name_C (const char **buffer)
585 locus old_loc;
586 size_t i = 0;
587 gfc_char_t c;
588 char* buf;
589 size_t cursz = 16;
591 old_loc = gfc_current_locus;
592 gfc_gobble_whitespace ();
594 /* Get the next char (first possible char of name) and see if
595 it's valid for C (either a letter or an underscore). */
596 c = gfc_next_char_literal (INSTRING_WARN);
598 /* If the user put nothing expect spaces between the quotes, it is valid
599 and simply means there is no name= specifier and the name is the Fortran
600 symbol name, all lowercase. */
601 if (c == '"' || c == '\'')
603 gfc_current_locus = old_loc;
604 return MATCH_YES;
607 if (!ISALPHA (c) && c != '_')
609 gfc_error ("Invalid C name in NAME= specifier at %C");
610 return MATCH_ERROR;
613 buf = XNEWVEC (char, cursz);
614 /* Continue to read valid variable name characters. */
617 gcc_assert (gfc_wide_fits_in_byte (c));
619 buf[i++] = (unsigned char) c;
621 if (i >= cursz)
623 cursz *= 2;
624 buf = XRESIZEVEC (char, buf, cursz);
627 old_loc = gfc_current_locus;
629 /* Get next char; param means we're in a string. */
630 c = gfc_next_char_literal (INSTRING_WARN);
631 } while (ISALNUM (c) || c == '_');
633 /* The binding label will be needed later anyway, so just insert it
634 into the symbol table. */
635 buf[i] = '\0';
636 *buffer = IDENTIFIER_POINTER (get_identifier (buf));
637 XDELETEVEC (buf);
638 gfc_current_locus = old_loc;
640 /* See if we stopped because of whitespace. */
641 if (c == ' ')
643 gfc_gobble_whitespace ();
644 c = gfc_peek_ascii_char ();
645 if (c != '"' && c != '\'')
647 gfc_error ("Embedded space in NAME= specifier at %C");
648 return MATCH_ERROR;
652 /* If we stopped because we had an invalid character for a C name, report
653 that to the user by returning MATCH_NO. */
654 if (c != '"' && c != '\'')
656 gfc_error ("Invalid C name in NAME= specifier at %C");
657 return MATCH_ERROR;
660 return MATCH_YES;
664 /* Match a symbol on the input. Modifies the pointer to the symbol
665 pointer if successful. */
667 match
668 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
670 char buffer[GFC_MAX_SYMBOL_LEN + 1];
671 match m;
673 m = gfc_match_name (buffer);
674 if (m != MATCH_YES)
675 return m;
677 if (host_assoc)
678 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
679 ? MATCH_ERROR : MATCH_YES;
681 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
682 return MATCH_ERROR;
684 return MATCH_YES;
688 match
689 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
691 gfc_symtree *st;
692 match m;
694 m = gfc_match_sym_tree (&st, host_assoc);
696 if (m == MATCH_YES)
698 if (st)
699 *matched_symbol = st->n.sym;
700 else
701 *matched_symbol = NULL;
703 else
704 *matched_symbol = NULL;
705 return m;
709 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
710 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
711 in matchexp.c. */
713 match
714 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
716 locus orig_loc = gfc_current_locus;
717 char ch;
719 gfc_gobble_whitespace ();
720 ch = gfc_next_ascii_char ();
721 switch (ch)
723 case '+':
724 /* Matched "+". */
725 *result = INTRINSIC_PLUS;
726 return MATCH_YES;
728 case '-':
729 /* Matched "-". */
730 *result = INTRINSIC_MINUS;
731 return MATCH_YES;
733 case '=':
734 if (gfc_next_ascii_char () == '=')
736 /* Matched "==". */
737 *result = INTRINSIC_EQ;
738 return MATCH_YES;
740 break;
742 case '<':
743 if (gfc_peek_ascii_char () == '=')
745 /* Matched "<=". */
746 gfc_next_ascii_char ();
747 *result = INTRINSIC_LE;
748 return MATCH_YES;
750 /* Matched "<". */
751 *result = INTRINSIC_LT;
752 return MATCH_YES;
754 case '>':
755 if (gfc_peek_ascii_char () == '=')
757 /* Matched ">=". */
758 gfc_next_ascii_char ();
759 *result = INTRINSIC_GE;
760 return MATCH_YES;
762 /* Matched ">". */
763 *result = INTRINSIC_GT;
764 return MATCH_YES;
766 case '*':
767 if (gfc_peek_ascii_char () == '*')
769 /* Matched "**". */
770 gfc_next_ascii_char ();
771 *result = INTRINSIC_POWER;
772 return MATCH_YES;
774 /* Matched "*". */
775 *result = INTRINSIC_TIMES;
776 return MATCH_YES;
778 case '/':
779 ch = gfc_peek_ascii_char ();
780 if (ch == '=')
782 /* Matched "/=". */
783 gfc_next_ascii_char ();
784 *result = INTRINSIC_NE;
785 return MATCH_YES;
787 else if (ch == '/')
789 /* Matched "//". */
790 gfc_next_ascii_char ();
791 *result = INTRINSIC_CONCAT;
792 return MATCH_YES;
794 /* Matched "/". */
795 *result = INTRINSIC_DIVIDE;
796 return MATCH_YES;
798 case '.':
799 ch = gfc_next_ascii_char ();
800 switch (ch)
802 case 'a':
803 if (gfc_next_ascii_char () == 'n'
804 && gfc_next_ascii_char () == 'd'
805 && gfc_next_ascii_char () == '.')
807 /* Matched ".and.". */
808 *result = INTRINSIC_AND;
809 return MATCH_YES;
811 break;
813 case 'e':
814 if (gfc_next_ascii_char () == 'q')
816 ch = gfc_next_ascii_char ();
817 if (ch == '.')
819 /* Matched ".eq.". */
820 *result = INTRINSIC_EQ_OS;
821 return MATCH_YES;
823 else if (ch == 'v')
825 if (gfc_next_ascii_char () == '.')
827 /* Matched ".eqv.". */
828 *result = INTRINSIC_EQV;
829 return MATCH_YES;
833 break;
835 case 'g':
836 ch = gfc_next_ascii_char ();
837 if (ch == 'e')
839 if (gfc_next_ascii_char () == '.')
841 /* Matched ".ge.". */
842 *result = INTRINSIC_GE_OS;
843 return MATCH_YES;
846 else if (ch == 't')
848 if (gfc_next_ascii_char () == '.')
850 /* Matched ".gt.". */
851 *result = INTRINSIC_GT_OS;
852 return MATCH_YES;
855 break;
857 case 'l':
858 ch = gfc_next_ascii_char ();
859 if (ch == 'e')
861 if (gfc_next_ascii_char () == '.')
863 /* Matched ".le.". */
864 *result = INTRINSIC_LE_OS;
865 return MATCH_YES;
868 else if (ch == 't')
870 if (gfc_next_ascii_char () == '.')
872 /* Matched ".lt.". */
873 *result = INTRINSIC_LT_OS;
874 return MATCH_YES;
877 break;
879 case 'n':
880 ch = gfc_next_ascii_char ();
881 if (ch == 'e')
883 ch = gfc_next_ascii_char ();
884 if (ch == '.')
886 /* Matched ".ne.". */
887 *result = INTRINSIC_NE_OS;
888 return MATCH_YES;
890 else if (ch == 'q')
892 if (gfc_next_ascii_char () == 'v'
893 && gfc_next_ascii_char () == '.')
895 /* Matched ".neqv.". */
896 *result = INTRINSIC_NEQV;
897 return MATCH_YES;
901 else if (ch == 'o')
903 if (gfc_next_ascii_char () == 't'
904 && gfc_next_ascii_char () == '.')
906 /* Matched ".not.". */
907 *result = INTRINSIC_NOT;
908 return MATCH_YES;
911 break;
913 case 'o':
914 if (gfc_next_ascii_char () == 'r'
915 && gfc_next_ascii_char () == '.')
917 /* Matched ".or.". */
918 *result = INTRINSIC_OR;
919 return MATCH_YES;
921 break;
923 default:
924 break;
926 break;
928 default:
929 break;
932 gfc_current_locus = orig_loc;
933 return MATCH_NO;
937 /* Match a loop control phrase:
939 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
941 If the final integer expression is not present, a constant unity
942 expression is returned. We don't return MATCH_ERROR until after
943 the equals sign is seen. */
945 match
946 gfc_match_iterator (gfc_iterator *iter, int init_flag)
948 char name[GFC_MAX_SYMBOL_LEN + 1];
949 gfc_expr *var, *e1, *e2, *e3;
950 locus start;
951 match m;
953 e1 = e2 = e3 = NULL;
955 /* Match the start of an iterator without affecting the symbol table. */
957 start = gfc_current_locus;
958 m = gfc_match (" %n =", name);
959 gfc_current_locus = start;
961 if (m != MATCH_YES)
962 return MATCH_NO;
964 m = gfc_match_variable (&var, 0);
965 if (m != MATCH_YES)
966 return MATCH_NO;
968 /* F2008, C617 & C565. */
969 if (var->symtree->n.sym->attr.codimension)
971 gfc_error ("Loop variable at %C cannot be a coarray");
972 goto cleanup;
975 if (var->ref != NULL)
977 gfc_error ("Loop variable at %C cannot be a sub-component");
978 goto cleanup;
981 gfc_match_char ('=');
983 var->symtree->n.sym->attr.implied_index = 1;
985 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
986 if (m == MATCH_NO)
987 goto syntax;
988 if (m == MATCH_ERROR)
989 goto cleanup;
991 if (gfc_match_char (',') != MATCH_YES)
992 goto syntax;
994 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
995 if (m == MATCH_NO)
996 goto syntax;
997 if (m == MATCH_ERROR)
998 goto cleanup;
1000 if (gfc_match_char (',') != MATCH_YES)
1002 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1003 goto done;
1006 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1007 if (m == MATCH_ERROR)
1008 goto cleanup;
1009 if (m == MATCH_NO)
1011 gfc_error ("Expected a step value in iterator at %C");
1012 goto cleanup;
1015 done:
1016 iter->var = var;
1017 iter->start = e1;
1018 iter->end = e2;
1019 iter->step = e3;
1020 return MATCH_YES;
1022 syntax:
1023 gfc_error ("Syntax error in iterator at %C");
1025 cleanup:
1026 gfc_free_expr (e1);
1027 gfc_free_expr (e2);
1028 gfc_free_expr (e3);
1030 return MATCH_ERROR;
1034 /* Tries to match the next non-whitespace character on the input.
1035 This subroutine does not return MATCH_ERROR. */
1037 match
1038 gfc_match_char (char c)
1040 locus where;
1042 where = gfc_current_locus;
1043 gfc_gobble_whitespace ();
1045 if (gfc_next_ascii_char () == c)
1046 return MATCH_YES;
1048 gfc_current_locus = where;
1049 return MATCH_NO;
1053 /* General purpose matching subroutine. The target string is a
1054 scanf-like format string in which spaces correspond to arbitrary
1055 whitespace (including no whitespace), characters correspond to
1056 themselves. The %-codes are:
1058 %% Literal percent sign
1059 %e Expression, pointer to a pointer is set
1060 %s Symbol, pointer to the symbol is set
1061 %n Name, character buffer is set to name
1062 %t Matches end of statement.
1063 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1064 %l Matches a statement label
1065 %v Matches a variable expression (an lvalue)
1066 % Matches a required space (in free form) and optional spaces. */
1068 match
1069 gfc_match (const char *target, ...)
1071 gfc_st_label **label;
1072 int matches, *ip;
1073 locus old_loc;
1074 va_list argp;
1075 char c, *np;
1076 match m, n;
1077 void **vp;
1078 const char *p;
1080 old_loc = gfc_current_locus;
1081 va_start (argp, target);
1082 m = MATCH_NO;
1083 matches = 0;
1084 p = target;
1086 loop:
1087 c = *p++;
1088 switch (c)
1090 case ' ':
1091 gfc_gobble_whitespace ();
1092 goto loop;
1093 case '\0':
1094 m = MATCH_YES;
1095 break;
1097 case '%':
1098 c = *p++;
1099 switch (c)
1101 case 'e':
1102 vp = va_arg (argp, void **);
1103 n = gfc_match_expr ((gfc_expr **) vp);
1104 if (n != MATCH_YES)
1106 m = n;
1107 goto not_yes;
1110 matches++;
1111 goto loop;
1113 case 'v':
1114 vp = va_arg (argp, void **);
1115 n = gfc_match_variable ((gfc_expr **) vp, 0);
1116 if (n != MATCH_YES)
1118 m = n;
1119 goto not_yes;
1122 matches++;
1123 goto loop;
1125 case 's':
1126 vp = va_arg (argp, void **);
1127 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1128 if (n != MATCH_YES)
1130 m = n;
1131 goto not_yes;
1134 matches++;
1135 goto loop;
1137 case 'n':
1138 np = va_arg (argp, char *);
1139 n = gfc_match_name (np);
1140 if (n != MATCH_YES)
1142 m = n;
1143 goto not_yes;
1146 matches++;
1147 goto loop;
1149 case 'l':
1150 label = va_arg (argp, gfc_st_label **);
1151 n = gfc_match_st_label (label);
1152 if (n != MATCH_YES)
1154 m = n;
1155 goto not_yes;
1158 matches++;
1159 goto loop;
1161 case 'o':
1162 ip = va_arg (argp, int *);
1163 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1164 if (n != MATCH_YES)
1166 m = n;
1167 goto not_yes;
1170 matches++;
1171 goto loop;
1173 case 't':
1174 if (gfc_match_eos () != MATCH_YES)
1176 m = MATCH_NO;
1177 goto not_yes;
1179 goto loop;
1181 case ' ':
1182 if (gfc_match_space () == MATCH_YES)
1183 goto loop;
1184 m = MATCH_NO;
1185 goto not_yes;
1187 case '%':
1188 break; /* Fall through to character matcher. */
1190 default:
1191 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1194 default:
1196 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1197 expect an upper case character here! */
1198 gcc_assert (TOLOWER (c) == c);
1200 if (c == gfc_next_ascii_char ())
1201 goto loop;
1202 break;
1205 not_yes:
1206 va_end (argp);
1208 if (m != MATCH_YES)
1210 /* Clean up after a failed match. */
1211 gfc_current_locus = old_loc;
1212 va_start (argp, target);
1214 p = target;
1215 for (; matches > 0; matches--)
1217 while (*p++ != '%');
1219 switch (*p++)
1221 case '%':
1222 matches++;
1223 break; /* Skip. */
1225 /* Matches that don't have to be undone */
1226 case 'o':
1227 case 'l':
1228 case 'n':
1229 case 's':
1230 (void) va_arg (argp, void **);
1231 break;
1233 case 'e':
1234 case 'v':
1235 vp = va_arg (argp, void **);
1236 gfc_free_expr ((struct gfc_expr *)*vp);
1237 *vp = NULL;
1238 break;
1242 va_end (argp);
1245 return m;
1249 /*********************** Statement level matching **********************/
1251 /* Matches the start of a program unit, which is the program keyword
1252 followed by an obligatory symbol. */
1254 match
1255 gfc_match_program (void)
1257 gfc_symbol *sym;
1258 match m;
1260 m = gfc_match ("% %s%t", &sym);
1262 if (m == MATCH_NO)
1264 gfc_error ("Invalid form of PROGRAM statement at %C");
1265 m = MATCH_ERROR;
1268 if (m == MATCH_ERROR)
1269 return m;
1271 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1272 return MATCH_ERROR;
1274 gfc_new_block = sym;
1276 return MATCH_YES;
1280 /* Match a simple assignment statement. */
1282 match
1283 gfc_match_assignment (void)
1285 gfc_expr *lvalue, *rvalue;
1286 locus old_loc;
1287 match m;
1289 old_loc = gfc_current_locus;
1291 lvalue = NULL;
1292 m = gfc_match (" %v =", &lvalue);
1293 if (m != MATCH_YES)
1295 gfc_current_locus = old_loc;
1296 gfc_free_expr (lvalue);
1297 return MATCH_NO;
1300 rvalue = NULL;
1301 m = gfc_match (" %e%t", &rvalue);
1302 if (m != MATCH_YES)
1304 gfc_current_locus = old_loc;
1305 gfc_free_expr (lvalue);
1306 gfc_free_expr (rvalue);
1307 return m;
1310 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1312 new_st.op = EXEC_ASSIGN;
1313 new_st.expr1 = lvalue;
1314 new_st.expr2 = rvalue;
1316 gfc_check_do_variable (lvalue->symtree);
1318 return MATCH_YES;
1322 /* Match a pointer assignment statement. */
1324 match
1325 gfc_match_pointer_assignment (void)
1327 gfc_expr *lvalue, *rvalue;
1328 locus old_loc;
1329 match m;
1331 old_loc = gfc_current_locus;
1333 lvalue = rvalue = NULL;
1334 gfc_matching_ptr_assignment = 0;
1335 gfc_matching_procptr_assignment = 0;
1337 m = gfc_match (" %v =>", &lvalue);
1338 if (m != MATCH_YES)
1340 m = MATCH_NO;
1341 goto cleanup;
1344 if (lvalue->symtree->n.sym->attr.proc_pointer
1345 || gfc_is_proc_ptr_comp (lvalue))
1346 gfc_matching_procptr_assignment = 1;
1347 else
1348 gfc_matching_ptr_assignment = 1;
1350 m = gfc_match (" %e%t", &rvalue);
1351 gfc_matching_ptr_assignment = 0;
1352 gfc_matching_procptr_assignment = 0;
1353 if (m != MATCH_YES)
1354 goto cleanup;
1356 new_st.op = EXEC_POINTER_ASSIGN;
1357 new_st.expr1 = lvalue;
1358 new_st.expr2 = rvalue;
1360 return MATCH_YES;
1362 cleanup:
1363 gfc_current_locus = old_loc;
1364 gfc_free_expr (lvalue);
1365 gfc_free_expr (rvalue);
1366 return m;
1370 /* We try to match an easy arithmetic IF statement. This only happens
1371 when just after having encountered a simple IF statement. This code
1372 is really duplicate with parts of the gfc_match_if code, but this is
1373 *much* easier. */
1375 static match
1376 match_arithmetic_if (void)
1378 gfc_st_label *l1, *l2, *l3;
1379 gfc_expr *expr;
1380 match m;
1382 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1383 if (m != MATCH_YES)
1384 return m;
1386 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1387 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1388 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1390 gfc_free_expr (expr);
1391 return MATCH_ERROR;
1394 if (gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF "
1395 "statement at %C") == FAILURE)
1396 return MATCH_ERROR;
1398 new_st.op = EXEC_ARITHMETIC_IF;
1399 new_st.expr1 = expr;
1400 new_st.label1 = l1;
1401 new_st.label2 = l2;
1402 new_st.label3 = l3;
1404 return MATCH_YES;
1408 /* The IF statement is a bit of a pain. First of all, there are three
1409 forms of it, the simple IF, the IF that starts a block and the
1410 arithmetic IF.
1412 There is a problem with the simple IF and that is the fact that we
1413 only have a single level of undo information on symbols. What this
1414 means is for a simple IF, we must re-match the whole IF statement
1415 multiple times in order to guarantee that the symbol table ends up
1416 in the proper state. */
1418 static match match_simple_forall (void);
1419 static match match_simple_where (void);
1421 match
1422 gfc_match_if (gfc_statement *if_type)
1424 gfc_expr *expr;
1425 gfc_st_label *l1, *l2, *l3;
1426 locus old_loc, old_loc2;
1427 gfc_code *p;
1428 match m, n;
1430 n = gfc_match_label ();
1431 if (n == MATCH_ERROR)
1432 return n;
1434 old_loc = gfc_current_locus;
1436 m = gfc_match (" if ( %e", &expr);
1437 if (m != MATCH_YES)
1438 return m;
1440 old_loc2 = gfc_current_locus;
1441 gfc_current_locus = old_loc;
1443 if (gfc_match_parens () == MATCH_ERROR)
1444 return MATCH_ERROR;
1446 gfc_current_locus = old_loc2;
1448 if (gfc_match_char (')') != MATCH_YES)
1450 gfc_error ("Syntax error in IF-expression at %C");
1451 gfc_free_expr (expr);
1452 return MATCH_ERROR;
1455 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1457 if (m == MATCH_YES)
1459 if (n == MATCH_YES)
1461 gfc_error ("Block label not appropriate for arithmetic IF "
1462 "statement at %C");
1463 gfc_free_expr (expr);
1464 return MATCH_ERROR;
1467 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1468 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1469 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1471 gfc_free_expr (expr);
1472 return MATCH_ERROR;
1475 if (gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF "
1476 "statement at %C") == FAILURE)
1477 return MATCH_ERROR;
1479 new_st.op = EXEC_ARITHMETIC_IF;
1480 new_st.expr1 = expr;
1481 new_st.label1 = l1;
1482 new_st.label2 = l2;
1483 new_st.label3 = l3;
1485 *if_type = ST_ARITHMETIC_IF;
1486 return MATCH_YES;
1489 if (gfc_match (" then%t") == MATCH_YES)
1491 new_st.op = EXEC_IF;
1492 new_st.expr1 = expr;
1493 *if_type = ST_IF_BLOCK;
1494 return MATCH_YES;
1497 if (n == MATCH_YES)
1499 gfc_error ("Block label is not appropriate for IF statement at %C");
1500 gfc_free_expr (expr);
1501 return MATCH_ERROR;
1504 /* At this point the only thing left is a simple IF statement. At
1505 this point, n has to be MATCH_NO, so we don't have to worry about
1506 re-matching a block label. From what we've got so far, try
1507 matching an assignment. */
1509 *if_type = ST_SIMPLE_IF;
1511 m = gfc_match_assignment ();
1512 if (m == MATCH_YES)
1513 goto got_match;
1515 gfc_free_expr (expr);
1516 gfc_undo_symbols ();
1517 gfc_current_locus = old_loc;
1519 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1520 assignment was found. For MATCH_NO, continue to call the various
1521 matchers. */
1522 if (m == MATCH_ERROR)
1523 return MATCH_ERROR;
1525 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1527 m = gfc_match_pointer_assignment ();
1528 if (m == MATCH_YES)
1529 goto got_match;
1531 gfc_free_expr (expr);
1532 gfc_undo_symbols ();
1533 gfc_current_locus = old_loc;
1535 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1537 /* Look at the next keyword to see which matcher to call. Matching
1538 the keyword doesn't affect the symbol table, so we don't have to
1539 restore between tries. */
1541 #define match(string, subr, statement) \
1542 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1544 gfc_clear_error ();
1546 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1547 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1548 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1549 match ("call", gfc_match_call, ST_CALL)
1550 match ("close", gfc_match_close, ST_CLOSE)
1551 match ("continue", gfc_match_continue, ST_CONTINUE)
1552 match ("cycle", gfc_match_cycle, ST_CYCLE)
1553 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1554 match ("end file", gfc_match_endfile, ST_END_FILE)
1555 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1556 match ("exit", gfc_match_exit, ST_EXIT)
1557 match ("flush", gfc_match_flush, ST_FLUSH)
1558 match ("forall", match_simple_forall, ST_FORALL)
1559 match ("go to", gfc_match_goto, ST_GOTO)
1560 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1561 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1562 match ("lock", gfc_match_lock, ST_LOCK)
1563 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1564 match ("open", gfc_match_open, ST_OPEN)
1565 match ("pause", gfc_match_pause, ST_NONE)
1566 match ("print", gfc_match_print, ST_WRITE)
1567 match ("read", gfc_match_read, ST_READ)
1568 match ("return", gfc_match_return, ST_RETURN)
1569 match ("rewind", gfc_match_rewind, ST_REWIND)
1570 match ("stop", gfc_match_stop, ST_STOP)
1571 match ("wait", gfc_match_wait, ST_WAIT)
1572 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1573 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1574 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1575 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1576 match ("where", match_simple_where, ST_WHERE)
1577 match ("write", gfc_match_write, ST_WRITE)
1579 /* The gfc_match_assignment() above may have returned a MATCH_NO
1580 where the assignment was to a named constant. Check that
1581 special case here. */
1582 m = gfc_match_assignment ();
1583 if (m == MATCH_NO)
1585 gfc_error ("Cannot assign to a named constant at %C");
1586 gfc_free_expr (expr);
1587 gfc_undo_symbols ();
1588 gfc_current_locus = old_loc;
1589 return MATCH_ERROR;
1592 /* All else has failed, so give up. See if any of the matchers has
1593 stored an error message of some sort. */
1594 if (gfc_error_check () == 0)
1595 gfc_error ("Unclassifiable statement in IF-clause at %C");
1597 gfc_free_expr (expr);
1598 return MATCH_ERROR;
1600 got_match:
1601 if (m == MATCH_NO)
1602 gfc_error ("Syntax error in IF-clause at %C");
1603 if (m != MATCH_YES)
1605 gfc_free_expr (expr);
1606 return MATCH_ERROR;
1609 /* At this point, we've matched the single IF and the action clause
1610 is in new_st. Rearrange things so that the IF statement appears
1611 in new_st. */
1613 p = gfc_get_code ();
1614 p->next = gfc_get_code ();
1615 *p->next = new_st;
1616 p->next->loc = gfc_current_locus;
1618 p->expr1 = expr;
1619 p->op = EXEC_IF;
1621 gfc_clear_new_st ();
1623 new_st.op = EXEC_IF;
1624 new_st.block = p;
1626 return MATCH_YES;
1629 #undef match
1632 /* Match an ELSE statement. */
1634 match
1635 gfc_match_else (void)
1637 char name[GFC_MAX_SYMBOL_LEN + 1];
1639 if (gfc_match_eos () == MATCH_YES)
1640 return MATCH_YES;
1642 if (gfc_match_name (name) != MATCH_YES
1643 || gfc_current_block () == NULL
1644 || gfc_match_eos () != MATCH_YES)
1646 gfc_error ("Unexpected junk after ELSE statement at %C");
1647 return MATCH_ERROR;
1650 if (strcmp (name, gfc_current_block ()->name) != 0)
1652 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1653 name, gfc_current_block ()->name);
1654 return MATCH_ERROR;
1657 return MATCH_YES;
1661 /* Match an ELSE IF statement. */
1663 match
1664 gfc_match_elseif (void)
1666 char name[GFC_MAX_SYMBOL_LEN + 1];
1667 gfc_expr *expr;
1668 match m;
1670 m = gfc_match (" ( %e ) then", &expr);
1671 if (m != MATCH_YES)
1672 return m;
1674 if (gfc_match_eos () == MATCH_YES)
1675 goto done;
1677 if (gfc_match_name (name) != MATCH_YES
1678 || gfc_current_block () == NULL
1679 || gfc_match_eos () != MATCH_YES)
1681 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1682 goto cleanup;
1685 if (strcmp (name, gfc_current_block ()->name) != 0)
1687 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1688 name, gfc_current_block ()->name);
1689 goto cleanup;
1692 done:
1693 new_st.op = EXEC_IF;
1694 new_st.expr1 = expr;
1695 return MATCH_YES;
1697 cleanup:
1698 gfc_free_expr (expr);
1699 return MATCH_ERROR;
1703 /* Free a gfc_iterator structure. */
1705 void
1706 gfc_free_iterator (gfc_iterator *iter, int flag)
1709 if (iter == NULL)
1710 return;
1712 gfc_free_expr (iter->var);
1713 gfc_free_expr (iter->start);
1714 gfc_free_expr (iter->end);
1715 gfc_free_expr (iter->step);
1717 if (flag)
1718 free (iter);
1722 /* Match a CRITICAL statement. */
1723 match
1724 gfc_match_critical (void)
1726 gfc_st_label *label = NULL;
1728 if (gfc_match_label () == MATCH_ERROR)
1729 return MATCH_ERROR;
1731 if (gfc_match (" critical") != MATCH_YES)
1732 return MATCH_NO;
1734 if (gfc_match_st_label (&label) == MATCH_ERROR)
1735 return MATCH_ERROR;
1737 if (gfc_match_eos () != MATCH_YES)
1739 gfc_syntax_error (ST_CRITICAL);
1740 return MATCH_ERROR;
1743 if (gfc_pure (NULL))
1745 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1746 return MATCH_ERROR;
1749 if (gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS)
1751 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1752 "block");
1753 return MATCH_ERROR;
1756 if (gfc_implicit_pure (NULL))
1757 gfc_current_ns->proc_name->attr.implicit_pure = 0;
1759 if (gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C")
1760 == FAILURE)
1761 return MATCH_ERROR;
1763 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
1765 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
1766 return MATCH_ERROR;
1769 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
1771 gfc_error ("Nested CRITICAL block at %C");
1772 return MATCH_ERROR;
1775 new_st.op = EXEC_CRITICAL;
1777 if (label != NULL
1778 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1779 return MATCH_ERROR;
1781 return MATCH_YES;
1785 /* Match a BLOCK statement. */
1787 match
1788 gfc_match_block (void)
1790 match m;
1792 if (gfc_match_label () == MATCH_ERROR)
1793 return MATCH_ERROR;
1795 if (gfc_match (" block") != MATCH_YES)
1796 return MATCH_NO;
1798 /* For this to be a correct BLOCK statement, the line must end now. */
1799 m = gfc_match_eos ();
1800 if (m == MATCH_ERROR)
1801 return MATCH_ERROR;
1802 if (m == MATCH_NO)
1803 return MATCH_NO;
1805 return MATCH_YES;
1809 /* Match an ASSOCIATE statement. */
1811 match
1812 gfc_match_associate (void)
1814 if (gfc_match_label () == MATCH_ERROR)
1815 return MATCH_ERROR;
1817 if (gfc_match (" associate") != MATCH_YES)
1818 return MATCH_NO;
1820 /* Match the association list. */
1821 if (gfc_match_char ('(') != MATCH_YES)
1823 gfc_error ("Expected association list at %C");
1824 return MATCH_ERROR;
1826 new_st.ext.block.assoc = NULL;
1827 while (true)
1829 gfc_association_list* newAssoc = gfc_get_association_list ();
1830 gfc_association_list* a;
1832 /* Match the next association. */
1833 if (gfc_match (" %n => %e", newAssoc->name, &newAssoc->target)
1834 != MATCH_YES)
1836 gfc_error ("Expected association at %C");
1837 goto assocListError;
1839 newAssoc->where = gfc_current_locus;
1841 /* Check that the current name is not yet in the list. */
1842 for (a = new_st.ext.block.assoc; a; a = a->next)
1843 if (!strcmp (a->name, newAssoc->name))
1845 gfc_error ("Duplicate name '%s' in association at %C",
1846 newAssoc->name);
1847 goto assocListError;
1850 /* The target expression must not be coindexed. */
1851 if (gfc_is_coindexed (newAssoc->target))
1853 gfc_error ("Association target at %C must not be coindexed");
1854 goto assocListError;
1857 /* The `variable' field is left blank for now; because the target is not
1858 yet resolved, we can't use gfc_has_vector_subscript to determine it
1859 for now. This is set during resolution. */
1861 /* Put it into the list. */
1862 newAssoc->next = new_st.ext.block.assoc;
1863 new_st.ext.block.assoc = newAssoc;
1865 /* Try next one or end if closing parenthesis is found. */
1866 gfc_gobble_whitespace ();
1867 if (gfc_peek_char () == ')')
1868 break;
1869 if (gfc_match_char (',') != MATCH_YES)
1871 gfc_error ("Expected ')' or ',' at %C");
1872 return MATCH_ERROR;
1875 continue;
1877 assocListError:
1878 free (newAssoc);
1879 goto error;
1881 if (gfc_match_char (')') != MATCH_YES)
1883 /* This should never happen as we peek above. */
1884 gcc_unreachable ();
1887 if (gfc_match_eos () != MATCH_YES)
1889 gfc_error ("Junk after ASSOCIATE statement at %C");
1890 goto error;
1893 return MATCH_YES;
1895 error:
1896 gfc_free_association_list (new_st.ext.block.assoc);
1897 return MATCH_ERROR;
1901 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1902 an accessible derived type. */
1904 static match
1905 match_derived_type_spec (gfc_typespec *ts)
1907 char name[GFC_MAX_SYMBOL_LEN + 1];
1908 locus old_locus;
1909 gfc_symbol *derived;
1911 old_locus = gfc_current_locus;
1913 if (gfc_match ("%n", name) != MATCH_YES)
1915 gfc_current_locus = old_locus;
1916 return MATCH_NO;
1919 gfc_find_symbol (name, NULL, 1, &derived);
1921 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
1922 derived = gfc_find_dt_in_generic (derived);
1924 if (derived && derived->attr.flavor == FL_DERIVED)
1926 ts->type = BT_DERIVED;
1927 ts->u.derived = derived;
1928 return MATCH_YES;
1931 gfc_current_locus = old_locus;
1932 return MATCH_NO;
1936 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
1937 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
1938 It only includes the intrinsic types from the Fortran 2003 standard
1939 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
1940 the implicit_flag is not needed, so it was removed. Derived types are
1941 identified by their name alone. */
1943 static match
1944 match_type_spec (gfc_typespec *ts)
1946 match m;
1947 locus old_locus;
1949 gfc_clear_ts (ts);
1950 gfc_gobble_whitespace ();
1951 old_locus = gfc_current_locus;
1953 if (match_derived_type_spec (ts) == MATCH_YES)
1955 /* Enforce F03:C401. */
1956 if (ts->u.derived->attr.abstract)
1958 gfc_error ("Derived type '%s' at %L may not be ABSTRACT",
1959 ts->u.derived->name, &old_locus);
1960 return MATCH_ERROR;
1962 return MATCH_YES;
1965 if (gfc_match ("integer") == MATCH_YES)
1967 ts->type = BT_INTEGER;
1968 ts->kind = gfc_default_integer_kind;
1969 goto kind_selector;
1972 if (gfc_match ("real") == MATCH_YES)
1974 ts->type = BT_REAL;
1975 ts->kind = gfc_default_real_kind;
1976 goto kind_selector;
1979 if (gfc_match ("double precision") == MATCH_YES)
1981 ts->type = BT_REAL;
1982 ts->kind = gfc_default_double_kind;
1983 return MATCH_YES;
1986 if (gfc_match ("complex") == MATCH_YES)
1988 ts->type = BT_COMPLEX;
1989 ts->kind = gfc_default_complex_kind;
1990 goto kind_selector;
1993 if (gfc_match ("character") == MATCH_YES)
1995 ts->type = BT_CHARACTER;
1997 m = gfc_match_char_spec (ts);
1999 if (m == MATCH_NO)
2000 m = MATCH_YES;
2002 return m;
2005 if (gfc_match ("logical") == MATCH_YES)
2007 ts->type = BT_LOGICAL;
2008 ts->kind = gfc_default_logical_kind;
2009 goto kind_selector;
2012 /* If a type is not matched, simply return MATCH_NO. */
2013 gfc_current_locus = old_locus;
2014 return MATCH_NO;
2016 kind_selector:
2018 gfc_gobble_whitespace ();
2019 if (gfc_peek_ascii_char () == '*')
2021 gfc_error ("Invalid type-spec at %C");
2022 return MATCH_ERROR;
2025 m = gfc_match_kind_spec (ts, false);
2027 if (m == MATCH_NO)
2028 m = MATCH_YES; /* No kind specifier found. */
2030 return m;
2034 /******************** FORALL subroutines ********************/
2036 /* Free a list of FORALL iterators. */
2038 void
2039 gfc_free_forall_iterator (gfc_forall_iterator *iter)
2041 gfc_forall_iterator *next;
2043 while (iter)
2045 next = iter->next;
2046 gfc_free_expr (iter->var);
2047 gfc_free_expr (iter->start);
2048 gfc_free_expr (iter->end);
2049 gfc_free_expr (iter->stride);
2050 free (iter);
2051 iter = next;
2056 /* Match an iterator as part of a FORALL statement. The format is:
2058 <var> = <start>:<end>[:<stride>]
2060 On MATCH_NO, the caller tests for the possibility that there is a
2061 scalar mask expression. */
2063 static match
2064 match_forall_iterator (gfc_forall_iterator **result)
2066 gfc_forall_iterator *iter;
2067 locus where;
2068 match m;
2070 where = gfc_current_locus;
2071 iter = XCNEW (gfc_forall_iterator);
2073 m = gfc_match_expr (&iter->var);
2074 if (m != MATCH_YES)
2075 goto cleanup;
2077 if (gfc_match_char ('=') != MATCH_YES
2078 || iter->var->expr_type != EXPR_VARIABLE)
2080 m = MATCH_NO;
2081 goto cleanup;
2084 m = gfc_match_expr (&iter->start);
2085 if (m != MATCH_YES)
2086 goto cleanup;
2088 if (gfc_match_char (':') != MATCH_YES)
2089 goto syntax;
2091 m = gfc_match_expr (&iter->end);
2092 if (m == MATCH_NO)
2093 goto syntax;
2094 if (m == MATCH_ERROR)
2095 goto cleanup;
2097 if (gfc_match_char (':') == MATCH_NO)
2098 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2099 else
2101 m = gfc_match_expr (&iter->stride);
2102 if (m == MATCH_NO)
2103 goto syntax;
2104 if (m == MATCH_ERROR)
2105 goto cleanup;
2108 /* Mark the iteration variable's symbol as used as a FORALL index. */
2109 iter->var->symtree->n.sym->forall_index = true;
2111 *result = iter;
2112 return MATCH_YES;
2114 syntax:
2115 gfc_error ("Syntax error in FORALL iterator at %C");
2116 m = MATCH_ERROR;
2118 cleanup:
2120 gfc_current_locus = where;
2121 gfc_free_forall_iterator (iter);
2122 return m;
2126 /* Match the header of a FORALL statement. */
2128 static match
2129 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2131 gfc_forall_iterator *head, *tail, *new_iter;
2132 gfc_expr *msk;
2133 match m;
2135 gfc_gobble_whitespace ();
2137 head = tail = NULL;
2138 msk = NULL;
2140 if (gfc_match_char ('(') != MATCH_YES)
2141 return MATCH_NO;
2143 m = match_forall_iterator (&new_iter);
2144 if (m == MATCH_ERROR)
2145 goto cleanup;
2146 if (m == MATCH_NO)
2147 goto syntax;
2149 head = tail = new_iter;
2151 for (;;)
2153 if (gfc_match_char (',') != MATCH_YES)
2154 break;
2156 m = match_forall_iterator (&new_iter);
2157 if (m == MATCH_ERROR)
2158 goto cleanup;
2160 if (m == MATCH_YES)
2162 tail->next = new_iter;
2163 tail = new_iter;
2164 continue;
2167 /* Have to have a mask expression. */
2169 m = gfc_match_expr (&msk);
2170 if (m == MATCH_NO)
2171 goto syntax;
2172 if (m == MATCH_ERROR)
2173 goto cleanup;
2175 break;
2178 if (gfc_match_char (')') == MATCH_NO)
2179 goto syntax;
2181 *phead = head;
2182 *mask = msk;
2183 return MATCH_YES;
2185 syntax:
2186 gfc_syntax_error (ST_FORALL);
2188 cleanup:
2189 gfc_free_expr (msk);
2190 gfc_free_forall_iterator (head);
2192 return MATCH_ERROR;
2195 /* Match the rest of a simple FORALL statement that follows an
2196 IF statement. */
2198 static match
2199 match_simple_forall (void)
2201 gfc_forall_iterator *head;
2202 gfc_expr *mask;
2203 gfc_code *c;
2204 match m;
2206 mask = NULL;
2207 head = NULL;
2208 c = NULL;
2210 m = match_forall_header (&head, &mask);
2212 if (m == MATCH_NO)
2213 goto syntax;
2214 if (m != MATCH_YES)
2215 goto cleanup;
2217 m = gfc_match_assignment ();
2219 if (m == MATCH_ERROR)
2220 goto cleanup;
2221 if (m == MATCH_NO)
2223 m = gfc_match_pointer_assignment ();
2224 if (m == MATCH_ERROR)
2225 goto cleanup;
2226 if (m == MATCH_NO)
2227 goto syntax;
2230 c = gfc_get_code ();
2231 *c = new_st;
2232 c->loc = gfc_current_locus;
2234 if (gfc_match_eos () != MATCH_YES)
2235 goto syntax;
2237 gfc_clear_new_st ();
2238 new_st.op = EXEC_FORALL;
2239 new_st.expr1 = mask;
2240 new_st.ext.forall_iterator = head;
2241 new_st.block = gfc_get_code ();
2243 new_st.block->op = EXEC_FORALL;
2244 new_st.block->next = c;
2246 return MATCH_YES;
2248 syntax:
2249 gfc_syntax_error (ST_FORALL);
2251 cleanup:
2252 gfc_free_forall_iterator (head);
2253 gfc_free_expr (mask);
2255 return MATCH_ERROR;
2259 /* Match a FORALL statement. */
2261 match
2262 gfc_match_forall (gfc_statement *st)
2264 gfc_forall_iterator *head;
2265 gfc_expr *mask;
2266 gfc_code *c;
2267 match m0, m;
2269 head = NULL;
2270 mask = NULL;
2271 c = NULL;
2273 m0 = gfc_match_label ();
2274 if (m0 == MATCH_ERROR)
2275 return MATCH_ERROR;
2277 m = gfc_match (" forall");
2278 if (m != MATCH_YES)
2279 return m;
2281 m = match_forall_header (&head, &mask);
2282 if (m == MATCH_ERROR)
2283 goto cleanup;
2284 if (m == MATCH_NO)
2285 goto syntax;
2287 if (gfc_match_eos () == MATCH_YES)
2289 *st = ST_FORALL_BLOCK;
2290 new_st.op = EXEC_FORALL;
2291 new_st.expr1 = mask;
2292 new_st.ext.forall_iterator = head;
2293 return MATCH_YES;
2296 m = gfc_match_assignment ();
2297 if (m == MATCH_ERROR)
2298 goto cleanup;
2299 if (m == MATCH_NO)
2301 m = gfc_match_pointer_assignment ();
2302 if (m == MATCH_ERROR)
2303 goto cleanup;
2304 if (m == MATCH_NO)
2305 goto syntax;
2308 c = gfc_get_code ();
2309 *c = new_st;
2310 c->loc = gfc_current_locus;
2312 gfc_clear_new_st ();
2313 new_st.op = EXEC_FORALL;
2314 new_st.expr1 = mask;
2315 new_st.ext.forall_iterator = head;
2316 new_st.block = gfc_get_code ();
2317 new_st.block->op = EXEC_FORALL;
2318 new_st.block->next = c;
2320 *st = ST_FORALL;
2321 return MATCH_YES;
2323 syntax:
2324 gfc_syntax_error (ST_FORALL);
2326 cleanup:
2327 gfc_free_forall_iterator (head);
2328 gfc_free_expr (mask);
2329 gfc_free_statements (c);
2330 return MATCH_NO;
2334 /* Match a DO statement. */
2336 match
2337 gfc_match_do (void)
2339 gfc_iterator iter, *ip;
2340 locus old_loc;
2341 gfc_st_label *label;
2342 match m;
2344 old_loc = gfc_current_locus;
2346 label = NULL;
2347 iter.var = iter.start = iter.end = iter.step = NULL;
2349 m = gfc_match_label ();
2350 if (m == MATCH_ERROR)
2351 return m;
2353 if (gfc_match (" do") != MATCH_YES)
2354 return MATCH_NO;
2356 m = gfc_match_st_label (&label);
2357 if (m == MATCH_ERROR)
2358 goto cleanup;
2360 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2362 if (gfc_match_eos () == MATCH_YES)
2364 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2365 new_st.op = EXEC_DO_WHILE;
2366 goto done;
2369 /* Match an optional comma, if no comma is found, a space is obligatory. */
2370 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2371 return MATCH_NO;
2373 /* Check for balanced parens. */
2375 if (gfc_match_parens () == MATCH_ERROR)
2376 return MATCH_ERROR;
2378 if (gfc_match (" concurrent") == MATCH_YES)
2380 gfc_forall_iterator *head;
2381 gfc_expr *mask;
2383 if (gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT "
2384 "construct at %C") == FAILURE)
2385 return MATCH_ERROR;
2388 mask = NULL;
2389 head = NULL;
2390 m = match_forall_header (&head, &mask);
2392 if (m == MATCH_NO)
2393 return m;
2394 if (m == MATCH_ERROR)
2395 goto concurr_cleanup;
2397 if (gfc_match_eos () != MATCH_YES)
2398 goto concurr_cleanup;
2400 if (label != NULL
2401 && gfc_reference_st_label (label, ST_LABEL_DO_TARGET) == FAILURE)
2402 goto concurr_cleanup;
2404 new_st.label1 = label;
2405 new_st.op = EXEC_DO_CONCURRENT;
2406 new_st.expr1 = mask;
2407 new_st.ext.forall_iterator = head;
2409 return MATCH_YES;
2411 concurr_cleanup:
2412 gfc_syntax_error (ST_DO);
2413 gfc_free_expr (mask);
2414 gfc_free_forall_iterator (head);
2415 return MATCH_ERROR;
2418 /* See if we have a DO WHILE. */
2419 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2421 new_st.op = EXEC_DO_WHILE;
2422 goto done;
2425 /* The abortive DO WHILE may have done something to the symbol
2426 table, so we start over. */
2427 gfc_undo_symbols ();
2428 gfc_current_locus = old_loc;
2430 gfc_match_label (); /* This won't error. */
2431 gfc_match (" do "); /* This will work. */
2433 gfc_match_st_label (&label); /* Can't error out. */
2434 gfc_match_char (','); /* Optional comma. */
2436 m = gfc_match_iterator (&iter, 0);
2437 if (m == MATCH_NO)
2438 return MATCH_NO;
2439 if (m == MATCH_ERROR)
2440 goto cleanup;
2442 iter.var->symtree->n.sym->attr.implied_index = 0;
2443 gfc_check_do_variable (iter.var->symtree);
2445 if (gfc_match_eos () != MATCH_YES)
2447 gfc_syntax_error (ST_DO);
2448 goto cleanup;
2451 new_st.op = EXEC_DO;
2453 done:
2454 if (label != NULL
2455 && gfc_reference_st_label (label, ST_LABEL_DO_TARGET) == FAILURE)
2456 goto cleanup;
2458 new_st.label1 = label;
2460 if (new_st.op == EXEC_DO_WHILE)
2461 new_st.expr1 = iter.end;
2462 else
2464 new_st.ext.iterator = ip = gfc_get_iterator ();
2465 *ip = iter;
2468 return MATCH_YES;
2470 cleanup:
2471 gfc_free_iterator (&iter, 0);
2473 return MATCH_ERROR;
2477 /* Match an EXIT or CYCLE statement. */
2479 static match
2480 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2482 gfc_state_data *p, *o;
2483 gfc_symbol *sym;
2484 match m;
2485 int cnt;
2487 if (gfc_match_eos () == MATCH_YES)
2488 sym = NULL;
2489 else
2491 char name[GFC_MAX_SYMBOL_LEN + 1];
2492 gfc_symtree* stree;
2494 m = gfc_match ("% %n%t", name);
2495 if (m == MATCH_ERROR)
2496 return MATCH_ERROR;
2497 if (m == MATCH_NO)
2499 gfc_syntax_error (st);
2500 return MATCH_ERROR;
2503 /* Find the corresponding symbol. If there's a BLOCK statement
2504 between here and the label, it is not in gfc_current_ns but a parent
2505 namespace! */
2506 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2507 if (!stree)
2509 gfc_error ("Name '%s' in %s statement at %C is unknown",
2510 name, gfc_ascii_statement (st));
2511 return MATCH_ERROR;
2514 sym = stree->n.sym;
2515 if (sym->attr.flavor != FL_LABEL)
2517 gfc_error ("Name '%s' in %s statement at %C is not a construct name",
2518 name, gfc_ascii_statement (st));
2519 return MATCH_ERROR;
2523 /* Find the loop specified by the label (or lack of a label). */
2524 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2525 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2526 o = p;
2527 else if (p->state == COMP_CRITICAL)
2529 gfc_error("%s statement at %C leaves CRITICAL construct",
2530 gfc_ascii_statement (st));
2531 return MATCH_ERROR;
2533 else if (p->state == COMP_DO_CONCURRENT
2534 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2536 /* F2008, C821 & C845. */
2537 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2538 gfc_ascii_statement (st));
2539 return MATCH_ERROR;
2541 else if ((sym && sym == p->sym)
2542 || (!sym && (p->state == COMP_DO
2543 || p->state == COMP_DO_CONCURRENT)))
2544 break;
2546 if (p == NULL)
2548 if (sym == NULL)
2549 gfc_error ("%s statement at %C is not within a construct",
2550 gfc_ascii_statement (st));
2551 else
2552 gfc_error ("%s statement at %C is not within construct '%s'",
2553 gfc_ascii_statement (st), sym->name);
2555 return MATCH_ERROR;
2558 /* Special checks for EXIT from non-loop constructs. */
2559 switch (p->state)
2561 case COMP_DO:
2562 case COMP_DO_CONCURRENT:
2563 break;
2565 case COMP_CRITICAL:
2566 /* This is already handled above. */
2567 gcc_unreachable ();
2569 case COMP_ASSOCIATE:
2570 case COMP_BLOCK:
2571 case COMP_IF:
2572 case COMP_SELECT:
2573 case COMP_SELECT_TYPE:
2574 gcc_assert (sym);
2575 if (op == EXEC_CYCLE)
2577 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2578 " construct '%s'", sym->name);
2579 return MATCH_ERROR;
2581 gcc_assert (op == EXEC_EXIT);
2582 if (gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2583 " do-construct-name at %C") == FAILURE)
2584 return MATCH_ERROR;
2585 break;
2587 default:
2588 gfc_error ("%s statement at %C is not applicable to construct '%s'",
2589 gfc_ascii_statement (st), sym->name);
2590 return MATCH_ERROR;
2593 if (o != NULL)
2595 gfc_error ("%s statement at %C leaving OpenMP structured block",
2596 gfc_ascii_statement (st));
2597 return MATCH_ERROR;
2600 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2601 o = o->previous;
2602 if (cnt > 0
2603 && o != NULL
2604 && o->state == COMP_OMP_STRUCTURED_BLOCK
2605 && (o->head->op == EXEC_OMP_DO
2606 || o->head->op == EXEC_OMP_PARALLEL_DO))
2608 int collapse = 1;
2609 gcc_assert (o->head->next != NULL
2610 && (o->head->next->op == EXEC_DO
2611 || o->head->next->op == EXEC_DO_WHILE)
2612 && o->previous != NULL
2613 && o->previous->tail->op == o->head->op);
2614 if (o->previous->tail->ext.omp_clauses != NULL
2615 && o->previous->tail->ext.omp_clauses->collapse > 1)
2616 collapse = o->previous->tail->ext.omp_clauses->collapse;
2617 if (st == ST_EXIT && cnt <= collapse)
2619 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2620 return MATCH_ERROR;
2622 if (st == ST_CYCLE && cnt < collapse)
2624 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2625 " !$OMP DO loop");
2626 return MATCH_ERROR;
2630 /* Save the first statement in the construct - needed by the backend. */
2631 new_st.ext.which_construct = p->construct;
2633 new_st.op = op;
2635 return MATCH_YES;
2639 /* Match the EXIT statement. */
2641 match
2642 gfc_match_exit (void)
2644 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2648 /* Match the CYCLE statement. */
2650 match
2651 gfc_match_cycle (void)
2653 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2657 /* Match a number or character constant after an (ALL) STOP or PAUSE statement. */
2659 static match
2660 gfc_match_stopcode (gfc_statement st)
2662 gfc_expr *e;
2663 match m;
2665 e = NULL;
2667 if (gfc_match_eos () != MATCH_YES)
2669 m = gfc_match_init_expr (&e);
2670 if (m == MATCH_ERROR)
2671 goto cleanup;
2672 if (m == MATCH_NO)
2673 goto syntax;
2675 if (gfc_match_eos () != MATCH_YES)
2676 goto syntax;
2679 if (gfc_pure (NULL))
2681 gfc_error ("%s statement not allowed in PURE procedure at %C",
2682 gfc_ascii_statement (st));
2683 goto cleanup;
2686 if (gfc_implicit_pure (NULL))
2687 gfc_current_ns->proc_name->attr.implicit_pure = 0;
2689 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL) == SUCCESS)
2691 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2692 goto cleanup;
2694 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS)
2696 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
2697 goto cleanup;
2700 if (e != NULL)
2702 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
2704 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2705 &e->where);
2706 goto cleanup;
2709 if (e->rank != 0)
2711 gfc_error ("STOP code at %L must be scalar",
2712 &e->where);
2713 goto cleanup;
2716 if (e->ts.type == BT_CHARACTER
2717 && e->ts.kind != gfc_default_character_kind)
2719 gfc_error ("STOP code at %L must be default character KIND=%d",
2720 &e->where, (int) gfc_default_character_kind);
2721 goto cleanup;
2724 if (e->ts.type == BT_INTEGER
2725 && e->ts.kind != gfc_default_integer_kind)
2727 gfc_error ("STOP code at %L must be default integer KIND=%d",
2728 &e->where, (int) gfc_default_integer_kind);
2729 goto cleanup;
2733 switch (st)
2735 case ST_STOP:
2736 new_st.op = EXEC_STOP;
2737 break;
2738 case ST_ERROR_STOP:
2739 new_st.op = EXEC_ERROR_STOP;
2740 break;
2741 case ST_PAUSE:
2742 new_st.op = EXEC_PAUSE;
2743 break;
2744 default:
2745 gcc_unreachable ();
2748 new_st.expr1 = e;
2749 new_st.ext.stop_code = -1;
2751 return MATCH_YES;
2753 syntax:
2754 gfc_syntax_error (st);
2756 cleanup:
2758 gfc_free_expr (e);
2759 return MATCH_ERROR;
2763 /* Match the (deprecated) PAUSE statement. */
2765 match
2766 gfc_match_pause (void)
2768 match m;
2770 m = gfc_match_stopcode (ST_PAUSE);
2771 if (m == MATCH_YES)
2773 if (gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement"
2774 " at %C")
2775 == FAILURE)
2776 m = MATCH_ERROR;
2778 return m;
2782 /* Match the STOP statement. */
2784 match
2785 gfc_match_stop (void)
2787 return gfc_match_stopcode (ST_STOP);
2791 /* Match the ERROR STOP statement. */
2793 match
2794 gfc_match_error_stop (void)
2796 if (gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C")
2797 == FAILURE)
2798 return MATCH_ERROR;
2800 return gfc_match_stopcode (ST_ERROR_STOP);
2804 /* Match LOCK/UNLOCK statement. Syntax:
2805 LOCK ( lock-variable [ , lock-stat-list ] )
2806 UNLOCK ( lock-variable [ , sync-stat-list ] )
2807 where lock-stat is ACQUIRED_LOCK or sync-stat
2808 and sync-stat is STAT= or ERRMSG=. */
2810 static match
2811 lock_unlock_statement (gfc_statement st)
2813 match m;
2814 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
2815 bool saw_acq_lock, saw_stat, saw_errmsg;
2817 tmp = lockvar = acq_lock = stat = errmsg = NULL;
2818 saw_acq_lock = saw_stat = saw_errmsg = false;
2820 if (gfc_pure (NULL))
2822 gfc_error ("Image control statement %s at %C in PURE procedure",
2823 st == ST_LOCK ? "LOCK" : "UNLOCK");
2824 return MATCH_ERROR;
2827 if (gfc_implicit_pure (NULL))
2828 gfc_current_ns->proc_name->attr.implicit_pure = 0;
2830 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
2832 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
2833 return MATCH_ERROR;
2836 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
2838 gfc_error ("Image control statement %s at %C in CRITICAL block",
2839 st == ST_LOCK ? "LOCK" : "UNLOCK");
2840 return MATCH_ERROR;
2843 if (gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS)
2845 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
2846 st == ST_LOCK ? "LOCK" : "UNLOCK");
2847 return MATCH_ERROR;
2850 if (gfc_match_char ('(') != MATCH_YES)
2851 goto syntax;
2853 if (gfc_match ("%e", &lockvar) != MATCH_YES)
2854 goto syntax;
2855 m = gfc_match_char (',');
2856 if (m == MATCH_ERROR)
2857 goto syntax;
2858 if (m == MATCH_NO)
2860 m = gfc_match_char (')');
2861 if (m == MATCH_YES)
2862 goto done;
2863 goto syntax;
2866 for (;;)
2868 m = gfc_match (" stat = %v", &tmp);
2869 if (m == MATCH_ERROR)
2870 goto syntax;
2871 if (m == MATCH_YES)
2873 if (saw_stat)
2875 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
2876 goto cleanup;
2878 stat = tmp;
2879 saw_stat = true;
2881 m = gfc_match_char (',');
2882 if (m == MATCH_YES)
2883 continue;
2885 tmp = NULL;
2886 break;
2889 m = gfc_match (" errmsg = %v", &tmp);
2890 if (m == MATCH_ERROR)
2891 goto syntax;
2892 if (m == MATCH_YES)
2894 if (saw_errmsg)
2896 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
2897 goto cleanup;
2899 errmsg = tmp;
2900 saw_errmsg = true;
2902 m = gfc_match_char (',');
2903 if (m == MATCH_YES)
2904 continue;
2906 tmp = NULL;
2907 break;
2910 m = gfc_match (" acquired_lock = %v", &tmp);
2911 if (m == MATCH_ERROR || st == ST_UNLOCK)
2912 goto syntax;
2913 if (m == MATCH_YES)
2915 if (saw_acq_lock)
2917 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L ",
2918 &tmp->where);
2919 goto cleanup;
2921 acq_lock = tmp;
2922 saw_acq_lock = true;
2924 m = gfc_match_char (',');
2925 if (m == MATCH_YES)
2926 continue;
2928 tmp = NULL;
2929 break;
2932 break;
2935 if (m == MATCH_ERROR)
2936 goto syntax;
2938 if (gfc_match (" )%t") != MATCH_YES)
2939 goto syntax;
2941 done:
2942 switch (st)
2944 case ST_LOCK:
2945 new_st.op = EXEC_LOCK;
2946 break;
2947 case ST_UNLOCK:
2948 new_st.op = EXEC_UNLOCK;
2949 break;
2950 default:
2951 gcc_unreachable ();
2954 new_st.expr1 = lockvar;
2955 new_st.expr2 = stat;
2956 new_st.expr3 = errmsg;
2957 new_st.expr4 = acq_lock;
2959 return MATCH_YES;
2961 syntax:
2962 gfc_syntax_error (st);
2964 cleanup:
2965 if (acq_lock != tmp)
2966 gfc_free_expr (acq_lock);
2967 if (errmsg != tmp)
2968 gfc_free_expr (errmsg);
2969 if (stat != tmp)
2970 gfc_free_expr (stat);
2972 gfc_free_expr (tmp);
2973 gfc_free_expr (lockvar);
2975 return MATCH_ERROR;
2979 match
2980 gfc_match_lock (void)
2982 if (gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C")
2983 == FAILURE)
2984 return MATCH_ERROR;
2986 return lock_unlock_statement (ST_LOCK);
2990 match
2991 gfc_match_unlock (void)
2993 if (gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C")
2994 == FAILURE)
2995 return MATCH_ERROR;
2997 return lock_unlock_statement (ST_UNLOCK);
3001 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3002 SYNC ALL [(sync-stat-list)]
3003 SYNC MEMORY [(sync-stat-list)]
3004 SYNC IMAGES (image-set [, sync-stat-list] )
3005 with sync-stat is int-expr or *. */
3007 static match
3008 sync_statement (gfc_statement st)
3010 match m;
3011 gfc_expr *tmp, *imageset, *stat, *errmsg;
3012 bool saw_stat, saw_errmsg;
3014 tmp = imageset = stat = errmsg = NULL;
3015 saw_stat = saw_errmsg = false;
3017 if (gfc_pure (NULL))
3019 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3020 return MATCH_ERROR;
3023 if (gfc_implicit_pure (NULL))
3024 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3026 if (gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C")
3027 == FAILURE)
3028 return MATCH_ERROR;
3030 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
3032 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
3033 return MATCH_ERROR;
3036 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
3038 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3039 return MATCH_ERROR;
3042 if (gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS)
3044 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3045 return MATCH_ERROR;
3048 if (gfc_match_eos () == MATCH_YES)
3050 if (st == ST_SYNC_IMAGES)
3051 goto syntax;
3052 goto done;
3055 if (gfc_match_char ('(') != MATCH_YES)
3056 goto syntax;
3058 if (st == ST_SYNC_IMAGES)
3060 /* Denote '*' as imageset == NULL. */
3061 m = gfc_match_char ('*');
3062 if (m == MATCH_ERROR)
3063 goto syntax;
3064 if (m == MATCH_NO)
3066 if (gfc_match ("%e", &imageset) != MATCH_YES)
3067 goto syntax;
3069 m = gfc_match_char (',');
3070 if (m == MATCH_ERROR)
3071 goto syntax;
3072 if (m == MATCH_NO)
3074 m = gfc_match_char (')');
3075 if (m == MATCH_YES)
3076 goto done;
3077 goto syntax;
3081 for (;;)
3083 m = gfc_match (" stat = %v", &tmp);
3084 if (m == MATCH_ERROR)
3085 goto syntax;
3086 if (m == MATCH_YES)
3088 if (saw_stat)
3090 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3091 goto cleanup;
3093 stat = tmp;
3094 saw_stat = true;
3096 if (gfc_match_char (',') == MATCH_YES)
3097 continue;
3099 tmp = NULL;
3100 break;
3103 m = gfc_match (" errmsg = %v", &tmp);
3104 if (m == MATCH_ERROR)
3105 goto syntax;
3106 if (m == MATCH_YES)
3108 if (saw_errmsg)
3110 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3111 goto cleanup;
3113 errmsg = tmp;
3114 saw_errmsg = true;
3116 if (gfc_match_char (',') == MATCH_YES)
3117 continue;
3119 tmp = NULL;
3120 break;
3123 break;
3126 if (gfc_match (" )%t") != MATCH_YES)
3127 goto syntax;
3129 done:
3130 switch (st)
3132 case ST_SYNC_ALL:
3133 new_st.op = EXEC_SYNC_ALL;
3134 break;
3135 case ST_SYNC_IMAGES:
3136 new_st.op = EXEC_SYNC_IMAGES;
3137 break;
3138 case ST_SYNC_MEMORY:
3139 new_st.op = EXEC_SYNC_MEMORY;
3140 break;
3141 default:
3142 gcc_unreachable ();
3145 new_st.expr1 = imageset;
3146 new_st.expr2 = stat;
3147 new_st.expr3 = errmsg;
3149 return MATCH_YES;
3151 syntax:
3152 gfc_syntax_error (st);
3154 cleanup:
3155 if (stat != tmp)
3156 gfc_free_expr (stat);
3157 if (errmsg != tmp)
3158 gfc_free_expr (errmsg);
3160 gfc_free_expr (tmp);
3161 gfc_free_expr (imageset);
3163 return MATCH_ERROR;
3167 /* Match SYNC ALL statement. */
3169 match
3170 gfc_match_sync_all (void)
3172 return sync_statement (ST_SYNC_ALL);
3176 /* Match SYNC IMAGES statement. */
3178 match
3179 gfc_match_sync_images (void)
3181 return sync_statement (ST_SYNC_IMAGES);
3185 /* Match SYNC MEMORY statement. */
3187 match
3188 gfc_match_sync_memory (void)
3190 return sync_statement (ST_SYNC_MEMORY);
3194 /* Match a CONTINUE statement. */
3196 match
3197 gfc_match_continue (void)
3199 if (gfc_match_eos () != MATCH_YES)
3201 gfc_syntax_error (ST_CONTINUE);
3202 return MATCH_ERROR;
3205 new_st.op = EXEC_CONTINUE;
3206 return MATCH_YES;
3210 /* Match the (deprecated) ASSIGN statement. */
3212 match
3213 gfc_match_assign (void)
3215 gfc_expr *expr;
3216 gfc_st_label *label;
3218 if (gfc_match (" %l", &label) == MATCH_YES)
3220 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
3221 return MATCH_ERROR;
3222 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
3224 if (gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN "
3225 "statement at %C")
3226 == FAILURE)
3227 return MATCH_ERROR;
3229 expr->symtree->n.sym->attr.assign = 1;
3231 new_st.op = EXEC_LABEL_ASSIGN;
3232 new_st.label1 = label;
3233 new_st.expr1 = expr;
3234 return MATCH_YES;
3237 return MATCH_NO;
3241 /* Match the GO TO statement. As a computed GOTO statement is
3242 matched, it is transformed into an equivalent SELECT block. No
3243 tree is necessary, and the resulting jumps-to-jumps are
3244 specifically optimized away by the back end. */
3246 match
3247 gfc_match_goto (void)
3249 gfc_code *head, *tail;
3250 gfc_expr *expr;
3251 gfc_case *cp;
3252 gfc_st_label *label;
3253 int i;
3254 match m;
3256 if (gfc_match (" %l%t", &label) == MATCH_YES)
3258 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
3259 return MATCH_ERROR;
3261 new_st.op = EXEC_GOTO;
3262 new_st.label1 = label;
3263 return MATCH_YES;
3266 /* The assigned GO TO statement. */
3268 if (gfc_match_variable (&expr, 0) == MATCH_YES)
3270 if (gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO "
3271 "statement at %C")
3272 == FAILURE)
3273 return MATCH_ERROR;
3275 new_st.op = EXEC_GOTO;
3276 new_st.expr1 = expr;
3278 if (gfc_match_eos () == MATCH_YES)
3279 return MATCH_YES;
3281 /* Match label list. */
3282 gfc_match_char (',');
3283 if (gfc_match_char ('(') != MATCH_YES)
3285 gfc_syntax_error (ST_GOTO);
3286 return MATCH_ERROR;
3288 head = tail = NULL;
3292 m = gfc_match_st_label (&label);
3293 if (m != MATCH_YES)
3294 goto syntax;
3296 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
3297 goto cleanup;
3299 if (head == NULL)
3300 head = tail = gfc_get_code ();
3301 else
3303 tail->block = gfc_get_code ();
3304 tail = tail->block;
3307 tail->label1 = label;
3308 tail->op = EXEC_GOTO;
3310 while (gfc_match_char (',') == MATCH_YES);
3312 if (gfc_match (")%t") != MATCH_YES)
3313 goto syntax;
3315 if (head == NULL)
3317 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3318 goto syntax;
3320 new_st.block = head;
3322 return MATCH_YES;
3325 /* Last chance is a computed GO TO statement. */
3326 if (gfc_match_char ('(') != MATCH_YES)
3328 gfc_syntax_error (ST_GOTO);
3329 return MATCH_ERROR;
3332 head = tail = NULL;
3333 i = 1;
3337 m = gfc_match_st_label (&label);
3338 if (m != MATCH_YES)
3339 goto syntax;
3341 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
3342 goto cleanup;
3344 if (head == NULL)
3345 head = tail = gfc_get_code ();
3346 else
3348 tail->block = gfc_get_code ();
3349 tail = tail->block;
3352 cp = gfc_get_case ();
3353 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
3354 NULL, i++);
3356 tail->op = EXEC_SELECT;
3357 tail->ext.block.case_list = cp;
3359 tail->next = gfc_get_code ();
3360 tail->next->op = EXEC_GOTO;
3361 tail->next->label1 = label;
3363 while (gfc_match_char (',') == MATCH_YES);
3365 if (gfc_match_char (')') != MATCH_YES)
3366 goto syntax;
3368 if (head == NULL)
3370 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3371 goto syntax;
3374 /* Get the rest of the statement. */
3375 gfc_match_char (',');
3377 if (gfc_match (" %e%t", &expr) != MATCH_YES)
3378 goto syntax;
3380 if (gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO "
3381 "at %C") == FAILURE)
3382 return MATCH_ERROR;
3384 /* At this point, a computed GOTO has been fully matched and an
3385 equivalent SELECT statement constructed. */
3387 new_st.op = EXEC_SELECT;
3388 new_st.expr1 = NULL;
3390 /* Hack: For a "real" SELECT, the expression is in expr. We put
3391 it in expr2 so we can distinguish then and produce the correct
3392 diagnostics. */
3393 new_st.expr2 = expr;
3394 new_st.block = head;
3395 return MATCH_YES;
3397 syntax:
3398 gfc_syntax_error (ST_GOTO);
3399 cleanup:
3400 gfc_free_statements (head);
3401 return MATCH_ERROR;
3405 /* Frees a list of gfc_alloc structures. */
3407 void
3408 gfc_free_alloc_list (gfc_alloc *p)
3410 gfc_alloc *q;
3412 for (; p; p = q)
3414 q = p->next;
3415 gfc_free_expr (p->expr);
3416 free (p);
3421 /* Match an ALLOCATE statement. */
3423 match
3424 gfc_match_allocate (void)
3426 gfc_alloc *head, *tail;
3427 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
3428 gfc_typespec ts;
3429 gfc_symbol *sym;
3430 match m;
3431 locus old_locus, deferred_locus;
3432 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
3433 bool saw_unlimited = false;
3435 head = tail = NULL;
3436 stat = errmsg = source = mold = tmp = NULL;
3437 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
3439 if (gfc_match_char ('(') != MATCH_YES)
3440 goto syntax;
3442 /* Match an optional type-spec. */
3443 old_locus = gfc_current_locus;
3444 m = match_type_spec (&ts);
3445 if (m == MATCH_ERROR)
3446 goto cleanup;
3447 else if (m == MATCH_NO)
3449 char name[GFC_MAX_SYMBOL_LEN + 3];
3451 if (gfc_match ("%n :: ", name) == MATCH_YES)
3453 gfc_error ("Error in type-spec at %L", &old_locus);
3454 goto cleanup;
3457 ts.type = BT_UNKNOWN;
3459 else
3461 if (gfc_match (" :: ") == MATCH_YES)
3463 if (gfc_notify_std (GFC_STD_F2003, "typespec in "
3464 "ALLOCATE at %L", &old_locus) == FAILURE)
3465 goto cleanup;
3467 if (ts.deferred)
3469 gfc_error ("Type-spec at %L cannot contain a deferred "
3470 "type parameter", &old_locus);
3471 goto cleanup;
3474 if (ts.type == BT_CHARACTER)
3475 ts.u.cl->length_from_typespec = true;
3477 else
3479 ts.type = BT_UNKNOWN;
3480 gfc_current_locus = old_locus;
3484 for (;;)
3486 if (head == NULL)
3487 head = tail = gfc_get_alloc ();
3488 else
3490 tail->next = gfc_get_alloc ();
3491 tail = tail->next;
3494 m = gfc_match_variable (&tail->expr, 0);
3495 if (m == MATCH_NO)
3496 goto syntax;
3497 if (m == MATCH_ERROR)
3498 goto cleanup;
3500 if (gfc_check_do_variable (tail->expr->symtree))
3501 goto cleanup;
3503 if (gfc_pure (NULL) && gfc_impure_variable (tail->expr->symtree->n.sym))
3505 gfc_error ("Bad allocate-object at %C for a PURE procedure");
3506 goto cleanup;
3509 if (gfc_implicit_pure (NULL)
3510 && gfc_impure_variable (tail->expr->symtree->n.sym))
3511 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3513 if (tail->expr->ts.deferred)
3515 saw_deferred = true;
3516 deferred_locus = tail->expr->where;
3519 if (gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS
3520 || gfc_find_state (COMP_CRITICAL) == SUCCESS)
3522 gfc_ref *ref;
3523 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
3524 for (ref = tail->expr->ref; ref; ref = ref->next)
3525 if (ref->type == REF_COMPONENT)
3526 coarray = ref->u.c.component->attr.codimension;
3528 if (coarray && gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS)
3530 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
3531 goto cleanup;
3533 if (coarray && gfc_find_state (COMP_CRITICAL) == SUCCESS)
3535 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
3536 goto cleanup;
3540 /* Check for F08:C628. */
3541 sym = tail->expr->symtree->n.sym;
3542 b1 = !(tail->expr->ref
3543 && (tail->expr->ref->type == REF_COMPONENT
3544 || tail->expr->ref->type == REF_ARRAY));
3545 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
3546 b2 = !(CLASS_DATA (sym)->attr.allocatable
3547 || CLASS_DATA (sym)->attr.class_pointer);
3548 else
3549 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
3550 || sym->attr.proc_pointer);
3551 b3 = sym && sym->ns && sym->ns->proc_name
3552 && (sym->ns->proc_name->attr.allocatable
3553 || sym->ns->proc_name->attr.pointer
3554 || sym->ns->proc_name->attr.proc_pointer);
3555 if (b1 && b2 && !b3)
3557 gfc_error ("Allocate-object at %L is neither a data pointer "
3558 "nor an allocatable variable", &tail->expr->where);
3559 goto cleanup;
3562 /* The ALLOCATE statement had an optional typespec. Check the
3563 constraints. */
3564 if (ts.type != BT_UNKNOWN)
3566 /* Enforce F03:C624. */
3567 if (!gfc_type_compatible (&tail->expr->ts, &ts))
3569 gfc_error ("Type of entity at %L is type incompatible with "
3570 "typespec", &tail->expr->where);
3571 goto cleanup;
3574 /* Enforce F03:C627. */
3575 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
3577 gfc_error ("Kind type parameter for entity at %L differs from "
3578 "the kind type parameter of the typespec",
3579 &tail->expr->where);
3580 goto cleanup;
3584 if (tail->expr->ts.type == BT_DERIVED)
3585 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
3587 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
3589 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
3591 gfc_error ("Shape specification for allocatable scalar at %C");
3592 goto cleanup;
3595 if (gfc_match_char (',') != MATCH_YES)
3596 break;
3598 alloc_opt_list:
3600 m = gfc_match (" stat = %v", &tmp);
3601 if (m == MATCH_ERROR)
3602 goto cleanup;
3603 if (m == MATCH_YES)
3605 /* Enforce C630. */
3606 if (saw_stat)
3608 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3609 goto cleanup;
3612 stat = tmp;
3613 tmp = NULL;
3614 saw_stat = true;
3616 if (gfc_check_do_variable (stat->symtree))
3617 goto cleanup;
3619 if (gfc_match_char (',') == MATCH_YES)
3620 goto alloc_opt_list;
3623 m = gfc_match (" errmsg = %v", &tmp);
3624 if (m == MATCH_ERROR)
3625 goto cleanup;
3626 if (m == MATCH_YES)
3628 if (gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L",
3629 &tmp->where) == FAILURE)
3630 goto cleanup;
3632 /* Enforce C630. */
3633 if (saw_errmsg)
3635 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3636 goto cleanup;
3639 errmsg = tmp;
3640 tmp = NULL;
3641 saw_errmsg = true;
3643 if (gfc_match_char (',') == MATCH_YES)
3644 goto alloc_opt_list;
3647 m = gfc_match (" source = %e", &tmp);
3648 if (m == MATCH_ERROR)
3649 goto cleanup;
3650 if (m == MATCH_YES)
3652 if (gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L",
3653 &tmp->where) == FAILURE)
3654 goto cleanup;
3656 /* Enforce C630. */
3657 if (saw_source)
3659 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
3660 goto cleanup;
3663 /* The next 2 conditionals check C631. */
3664 if (ts.type != BT_UNKNOWN)
3666 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
3667 &tmp->where, &old_locus);
3668 goto cleanup;
3671 if (head->next
3672 && gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
3673 " with more than a single allocate object",
3674 &tmp->where) == FAILURE)
3675 goto cleanup;
3677 source = tmp;
3678 tmp = NULL;
3679 saw_source = true;
3681 if (gfc_match_char (',') == MATCH_YES)
3682 goto alloc_opt_list;
3685 m = gfc_match (" mold = %e", &tmp);
3686 if (m == MATCH_ERROR)
3687 goto cleanup;
3688 if (m == MATCH_YES)
3690 if (gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L",
3691 &tmp->where) == FAILURE)
3692 goto cleanup;
3694 /* Check F08:C636. */
3695 if (saw_mold)
3697 gfc_error ("Redundant MOLD tag found at %L ", &tmp->where);
3698 goto cleanup;
3701 /* Check F08:C637. */
3702 if (ts.type != BT_UNKNOWN)
3704 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
3705 &tmp->where, &old_locus);
3706 goto cleanup;
3709 mold = tmp;
3710 tmp = NULL;
3711 saw_mold = true;
3712 mold->mold = 1;
3714 if (gfc_match_char (',') == MATCH_YES)
3715 goto alloc_opt_list;
3718 gfc_gobble_whitespace ();
3720 if (gfc_peek_char () == ')')
3721 break;
3724 if (gfc_match (" )%t") != MATCH_YES)
3725 goto syntax;
3727 /* Check F08:C637. */
3728 if (source && mold)
3730 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
3731 &mold->where, &source->where);
3732 goto cleanup;
3735 /* Check F03:C623, */
3736 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
3738 gfc_error ("Allocate-object at %L with a deferred type parameter "
3739 "requires either a type-spec or SOURCE tag or a MOLD tag",
3740 &deferred_locus);
3741 goto cleanup;
3744 /* Check F03:C625, */
3745 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
3747 for (tail = head; tail; tail = tail->next)
3749 if (UNLIMITED_POLY (tail->expr))
3750 gfc_error ("Unlimited polymorphic allocate-object at %L "
3751 "requires either a type-spec or SOURCE tag "
3752 "or a MOLD tag", &tail->expr->where);
3754 goto cleanup;
3757 new_st.op = EXEC_ALLOCATE;
3758 new_st.expr1 = stat;
3759 new_st.expr2 = errmsg;
3760 if (source)
3761 new_st.expr3 = source;
3762 else
3763 new_st.expr3 = mold;
3764 new_st.ext.alloc.list = head;
3765 new_st.ext.alloc.ts = ts;
3767 return MATCH_YES;
3769 syntax:
3770 gfc_syntax_error (ST_ALLOCATE);
3772 cleanup:
3773 gfc_free_expr (errmsg);
3774 gfc_free_expr (source);
3775 gfc_free_expr (stat);
3776 gfc_free_expr (mold);
3777 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
3778 gfc_free_alloc_list (head);
3779 return MATCH_ERROR;
3783 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
3784 a set of pointer assignments to intrinsic NULL(). */
3786 match
3787 gfc_match_nullify (void)
3789 gfc_code *tail;
3790 gfc_expr *e, *p;
3791 match m;
3793 tail = NULL;
3795 if (gfc_match_char ('(') != MATCH_YES)
3796 goto syntax;
3798 for (;;)
3800 m = gfc_match_variable (&p, 0);
3801 if (m == MATCH_ERROR)
3802 goto cleanup;
3803 if (m == MATCH_NO)
3804 goto syntax;
3806 if (gfc_check_do_variable (p->symtree))
3807 goto cleanup;
3809 /* F2008, C1242. */
3810 if (gfc_is_coindexed (p))
3812 gfc_error ("Pointer object at %C shall not be coindexed");
3813 goto cleanup;
3816 /* build ' => NULL() '. */
3817 e = gfc_get_null_expr (&gfc_current_locus);
3819 /* Chain to list. */
3820 if (tail == NULL)
3821 tail = &new_st;
3822 else
3824 tail->next = gfc_get_code ();
3825 tail = tail->next;
3828 tail->op = EXEC_POINTER_ASSIGN;
3829 tail->expr1 = p;
3830 tail->expr2 = e;
3832 if (gfc_match (" )%t") == MATCH_YES)
3833 break;
3834 if (gfc_match_char (',') != MATCH_YES)
3835 goto syntax;
3838 return MATCH_YES;
3840 syntax:
3841 gfc_syntax_error (ST_NULLIFY);
3843 cleanup:
3844 gfc_free_statements (new_st.next);
3845 new_st.next = NULL;
3846 gfc_free_expr (new_st.expr1);
3847 new_st.expr1 = NULL;
3848 gfc_free_expr (new_st.expr2);
3849 new_st.expr2 = NULL;
3850 return MATCH_ERROR;
3854 /* Match a DEALLOCATE statement. */
3856 match
3857 gfc_match_deallocate (void)
3859 gfc_alloc *head, *tail;
3860 gfc_expr *stat, *errmsg, *tmp;
3861 gfc_symbol *sym;
3862 match m;
3863 bool saw_stat, saw_errmsg, b1, b2;
3865 head = tail = NULL;
3866 stat = errmsg = tmp = NULL;
3867 saw_stat = saw_errmsg = false;
3869 if (gfc_match_char ('(') != MATCH_YES)
3870 goto syntax;
3872 for (;;)
3874 if (head == NULL)
3875 head = tail = gfc_get_alloc ();
3876 else
3878 tail->next = gfc_get_alloc ();
3879 tail = tail->next;
3882 m = gfc_match_variable (&tail->expr, 0);
3883 if (m == MATCH_ERROR)
3884 goto cleanup;
3885 if (m == MATCH_NO)
3886 goto syntax;
3888 if (gfc_check_do_variable (tail->expr->symtree))
3889 goto cleanup;
3891 sym = tail->expr->symtree->n.sym;
3893 if (gfc_pure (NULL) && gfc_impure_variable (sym))
3895 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
3896 goto cleanup;
3899 if (gfc_implicit_pure (NULL) && gfc_impure_variable (sym))
3900 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3902 if (gfc_is_coarray (tail->expr)
3903 && gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS)
3905 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
3906 goto cleanup;
3909 if (gfc_is_coarray (tail->expr)
3910 && gfc_find_state (COMP_CRITICAL) == SUCCESS)
3912 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
3913 goto cleanup;
3916 /* FIXME: disable the checking on derived types. */
3917 b1 = !(tail->expr->ref
3918 && (tail->expr->ref->type == REF_COMPONENT
3919 || tail->expr->ref->type == REF_ARRAY));
3920 if (sym && sym->ts.type == BT_CLASS)
3921 b2 = !(CLASS_DATA (sym)->attr.allocatable
3922 || CLASS_DATA (sym)->attr.class_pointer);
3923 else
3924 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
3925 || sym->attr.proc_pointer);
3926 if (b1 && b2)
3928 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
3929 "nor an allocatable variable");
3930 goto cleanup;
3933 if (gfc_match_char (',') != MATCH_YES)
3934 break;
3936 dealloc_opt_list:
3938 m = gfc_match (" stat = %v", &tmp);
3939 if (m == MATCH_ERROR)
3940 goto cleanup;
3941 if (m == MATCH_YES)
3943 if (saw_stat)
3945 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3946 gfc_free_expr (tmp);
3947 goto cleanup;
3950 stat = tmp;
3951 saw_stat = true;
3953 if (gfc_check_do_variable (stat->symtree))
3954 goto cleanup;
3956 if (gfc_match_char (',') == MATCH_YES)
3957 goto dealloc_opt_list;
3960 m = gfc_match (" errmsg = %v", &tmp);
3961 if (m == MATCH_ERROR)
3962 goto cleanup;
3963 if (m == MATCH_YES)
3965 if (gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L",
3966 &tmp->where) == FAILURE)
3967 goto cleanup;
3969 if (saw_errmsg)
3971 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3972 gfc_free_expr (tmp);
3973 goto cleanup;
3976 errmsg = tmp;
3977 saw_errmsg = true;
3979 if (gfc_match_char (',') == MATCH_YES)
3980 goto dealloc_opt_list;
3983 gfc_gobble_whitespace ();
3985 if (gfc_peek_char () == ')')
3986 break;
3989 if (gfc_match (" )%t") != MATCH_YES)
3990 goto syntax;
3992 new_st.op = EXEC_DEALLOCATE;
3993 new_st.expr1 = stat;
3994 new_st.expr2 = errmsg;
3995 new_st.ext.alloc.list = head;
3997 return MATCH_YES;
3999 syntax:
4000 gfc_syntax_error (ST_DEALLOCATE);
4002 cleanup:
4003 gfc_free_expr (errmsg);
4004 gfc_free_expr (stat);
4005 gfc_free_alloc_list (head);
4006 return MATCH_ERROR;
4010 /* Match a RETURN statement. */
4012 match
4013 gfc_match_return (void)
4015 gfc_expr *e;
4016 match m;
4017 gfc_compile_state s;
4019 e = NULL;
4021 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
4023 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4024 return MATCH_ERROR;
4027 if (gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS)
4029 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4030 return MATCH_ERROR;
4033 if (gfc_match_eos () == MATCH_YES)
4034 goto done;
4036 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
4038 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4039 "a SUBROUTINE");
4040 goto cleanup;
4043 if (gfc_notify_std (GFC_STD_F95_OBS, "Alternate RETURN "
4044 "at %C") == FAILURE)
4045 return MATCH_ERROR;
4047 if (gfc_current_form == FORM_FREE)
4049 /* The following are valid, so we can't require a blank after the
4050 RETURN keyword:
4051 return+1
4052 return(1) */
4053 char c = gfc_peek_ascii_char ();
4054 if (ISALPHA (c) || ISDIGIT (c))
4055 return MATCH_NO;
4058 m = gfc_match (" %e%t", &e);
4059 if (m == MATCH_YES)
4060 goto done;
4061 if (m == MATCH_ERROR)
4062 goto cleanup;
4064 gfc_syntax_error (ST_RETURN);
4066 cleanup:
4067 gfc_free_expr (e);
4068 return MATCH_ERROR;
4070 done:
4071 gfc_enclosing_unit (&s);
4072 if (s == COMP_PROGRAM
4073 && gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4074 "main program at %C") == FAILURE)
4075 return MATCH_ERROR;
4077 new_st.op = EXEC_RETURN;
4078 new_st.expr1 = e;
4080 return MATCH_YES;
4084 /* Match the call of a type-bound procedure, if CALL%var has already been
4085 matched and var found to be a derived-type variable. */
4087 static match
4088 match_typebound_call (gfc_symtree* varst)
4090 gfc_expr* base;
4091 match m;
4093 base = gfc_get_expr ();
4094 base->expr_type = EXPR_VARIABLE;
4095 base->symtree = varst;
4096 base->where = gfc_current_locus;
4097 gfc_set_sym_referenced (varst->n.sym);
4099 m = gfc_match_varspec (base, 0, true, true);
4100 if (m == MATCH_NO)
4101 gfc_error ("Expected component reference at %C");
4102 if (m != MATCH_YES)
4103 return MATCH_ERROR;
4105 if (gfc_match_eos () != MATCH_YES)
4107 gfc_error ("Junk after CALL at %C");
4108 return MATCH_ERROR;
4111 if (base->expr_type == EXPR_COMPCALL)
4112 new_st.op = EXEC_COMPCALL;
4113 else if (base->expr_type == EXPR_PPC)
4114 new_st.op = EXEC_CALL_PPC;
4115 else
4117 gfc_error ("Expected type-bound procedure or procedure pointer component "
4118 "at %C");
4119 return MATCH_ERROR;
4121 new_st.expr1 = base;
4123 return MATCH_YES;
4127 /* Match a CALL statement. The tricky part here are possible
4128 alternate return specifiers. We handle these by having all
4129 "subroutines" actually return an integer via a register that gives
4130 the return number. If the call specifies alternate returns, we
4131 generate code for a SELECT statement whose case clauses contain
4132 GOTOs to the various labels. */
4134 match
4135 gfc_match_call (void)
4137 char name[GFC_MAX_SYMBOL_LEN + 1];
4138 gfc_actual_arglist *a, *arglist;
4139 gfc_case *new_case;
4140 gfc_symbol *sym;
4141 gfc_symtree *st;
4142 gfc_code *c;
4143 match m;
4144 int i;
4146 arglist = NULL;
4148 m = gfc_match ("% %n", name);
4149 if (m == MATCH_NO)
4150 goto syntax;
4151 if (m != MATCH_YES)
4152 return m;
4154 if (gfc_get_ha_sym_tree (name, &st))
4155 return MATCH_ERROR;
4157 sym = st->n.sym;
4159 /* If this is a variable of derived-type, it probably starts a type-bound
4160 procedure call. */
4161 if ((sym->attr.flavor != FL_PROCEDURE
4162 || gfc_is_function_return_value (sym, gfc_current_ns))
4163 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
4164 return match_typebound_call (st);
4166 /* If it does not seem to be callable (include functions so that the
4167 right association is made. They are thrown out in resolution.)
4168 ... */
4169 if (!sym->attr.generic
4170 && !sym->attr.subroutine
4171 && !sym->attr.function)
4173 if (!(sym->attr.external && !sym->attr.referenced))
4175 /* ...create a symbol in this scope... */
4176 if (sym->ns != gfc_current_ns
4177 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
4178 return MATCH_ERROR;
4180 if (sym != st->n.sym)
4181 sym = st->n.sym;
4184 /* ...and then to try to make the symbol into a subroutine. */
4185 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4186 return MATCH_ERROR;
4189 gfc_set_sym_referenced (sym);
4191 if (gfc_match_eos () != MATCH_YES)
4193 m = gfc_match_actual_arglist (1, &arglist);
4194 if (m == MATCH_NO)
4195 goto syntax;
4196 if (m == MATCH_ERROR)
4197 goto cleanup;
4199 if (gfc_match_eos () != MATCH_YES)
4200 goto syntax;
4203 /* If any alternate return labels were found, construct a SELECT
4204 statement that will jump to the right place. */
4206 i = 0;
4207 for (a = arglist; a; a = a->next)
4208 if (a->expr == NULL)
4209 i = 1;
4211 if (i)
4213 gfc_symtree *select_st;
4214 gfc_symbol *select_sym;
4215 char name[GFC_MAX_SYMBOL_LEN + 1];
4217 new_st.next = c = gfc_get_code ();
4218 c->op = EXEC_SELECT;
4219 sprintf (name, "_result_%s", sym->name);
4220 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
4222 select_sym = select_st->n.sym;
4223 select_sym->ts.type = BT_INTEGER;
4224 select_sym->ts.kind = gfc_default_integer_kind;
4225 gfc_set_sym_referenced (select_sym);
4226 c->expr1 = gfc_get_expr ();
4227 c->expr1->expr_type = EXPR_VARIABLE;
4228 c->expr1->symtree = select_st;
4229 c->expr1->ts = select_sym->ts;
4230 c->expr1->where = gfc_current_locus;
4232 i = 0;
4233 for (a = arglist; a; a = a->next)
4235 if (a->expr != NULL)
4236 continue;
4238 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
4239 continue;
4241 i++;
4243 c->block = gfc_get_code ();
4244 c = c->block;
4245 c->op = EXEC_SELECT;
4247 new_case = gfc_get_case ();
4248 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
4249 new_case->low = new_case->high;
4250 c->ext.block.case_list = new_case;
4252 c->next = gfc_get_code ();
4253 c->next->op = EXEC_GOTO;
4254 c->next->label1 = a->label;
4258 new_st.op = EXEC_CALL;
4259 new_st.symtree = st;
4260 new_st.ext.actual = arglist;
4262 return MATCH_YES;
4264 syntax:
4265 gfc_syntax_error (ST_CALL);
4267 cleanup:
4268 gfc_free_actual_arglist (arglist);
4269 return MATCH_ERROR;
4273 /* Given a name, return a pointer to the common head structure,
4274 creating it if it does not exist. If FROM_MODULE is nonzero, we
4275 mangle the name so that it doesn't interfere with commons defined
4276 in the using namespace.
4277 TODO: Add to global symbol tree. */
4279 gfc_common_head *
4280 gfc_get_common (const char *name, int from_module)
4282 gfc_symtree *st;
4283 static int serial = 0;
4284 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
4286 if (from_module)
4288 /* A use associated common block is only needed to correctly layout
4289 the variables it contains. */
4290 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
4291 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
4293 else
4295 st = gfc_find_symtree (gfc_current_ns->common_root, name);
4297 if (st == NULL)
4298 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
4301 if (st->n.common == NULL)
4303 st->n.common = gfc_get_common_head ();
4304 st->n.common->where = gfc_current_locus;
4305 strcpy (st->n.common->name, name);
4308 return st->n.common;
4312 /* Match a common block name. */
4314 match match_common_name (char *name)
4316 match m;
4318 if (gfc_match_char ('/') == MATCH_NO)
4320 name[0] = '\0';
4321 return MATCH_YES;
4324 if (gfc_match_char ('/') == MATCH_YES)
4326 name[0] = '\0';
4327 return MATCH_YES;
4330 m = gfc_match_name (name);
4332 if (m == MATCH_ERROR)
4333 return MATCH_ERROR;
4334 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
4335 return MATCH_YES;
4337 gfc_error ("Syntax error in common block name at %C");
4338 return MATCH_ERROR;
4342 /* Match a COMMON statement. */
4344 match
4345 gfc_match_common (void)
4347 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
4348 char name[GFC_MAX_SYMBOL_LEN + 1];
4349 gfc_common_head *t;
4350 gfc_array_spec *as;
4351 gfc_equiv *e1, *e2;
4352 match m;
4353 gfc_gsymbol *gsym;
4355 old_blank_common = gfc_current_ns->blank_common.head;
4356 if (old_blank_common)
4358 while (old_blank_common->common_next)
4359 old_blank_common = old_blank_common->common_next;
4362 as = NULL;
4364 for (;;)
4366 m = match_common_name (name);
4367 if (m == MATCH_ERROR)
4368 goto cleanup;
4370 gsym = gfc_get_gsymbol (name);
4371 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
4373 gfc_error ("Symbol '%s' at %C is already an external symbol that "
4374 "is not COMMON", name);
4375 goto cleanup;
4378 if (gsym->type == GSYM_UNKNOWN)
4380 gsym->type = GSYM_COMMON;
4381 gsym->where = gfc_current_locus;
4382 gsym->defined = 1;
4385 gsym->used = 1;
4387 if (name[0] == '\0')
4389 t = &gfc_current_ns->blank_common;
4390 if (t->head == NULL)
4391 t->where = gfc_current_locus;
4393 else
4395 t = gfc_get_common (name, 0);
4397 head = &t->head;
4399 if (*head == NULL)
4400 tail = NULL;
4401 else
4403 tail = *head;
4404 while (tail->common_next)
4405 tail = tail->common_next;
4408 /* Grab the list of symbols. */
4409 for (;;)
4411 m = gfc_match_symbol (&sym, 0);
4412 if (m == MATCH_ERROR)
4413 goto cleanup;
4414 if (m == MATCH_NO)
4415 goto syntax;
4417 /* Store a ref to the common block for error checking. */
4418 sym->common_block = t;
4419 sym->common_block->refs++;
4421 /* See if we know the current common block is bind(c), and if
4422 so, then see if we can check if the symbol is (which it'll
4423 need to be). This can happen if the bind(c) attr stmt was
4424 applied to the common block, and the variable(s) already
4425 defined, before declaring the common block. */
4426 if (t->is_bind_c == 1)
4428 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
4430 /* If we find an error, just print it and continue,
4431 cause it's just semantic, and we can see if there
4432 are more errors. */
4433 gfc_error_now ("Variable '%s' at %L in common block '%s' "
4434 "at %C must be declared with a C "
4435 "interoperable kind since common block "
4436 "'%s' is bind(c)",
4437 sym->name, &(sym->declared_at), t->name,
4438 t->name);
4441 if (sym->attr.is_bind_c == 1)
4442 gfc_error_now ("Variable '%s' in common block "
4443 "'%s' at %C can not be bind(c) since "
4444 "it is not global", sym->name, t->name);
4447 if (sym->attr.in_common)
4449 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
4450 sym->name);
4451 goto cleanup;
4454 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
4455 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
4457 if (gfc_notify_std (GFC_STD_GNU, "Initialized symbol '%s' at %C "
4458 "can only be COMMON in "
4459 "BLOCK DATA", sym->name)
4460 == FAILURE)
4461 goto cleanup;
4464 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
4465 goto cleanup;
4467 if (tail != NULL)
4468 tail->common_next = sym;
4469 else
4470 *head = sym;
4472 tail = sym;
4474 /* Deal with an optional array specification after the
4475 symbol name. */
4476 m = gfc_match_array_spec (&as, true, true);
4477 if (m == MATCH_ERROR)
4478 goto cleanup;
4480 if (m == MATCH_YES)
4482 if (as->type != AS_EXPLICIT)
4484 gfc_error ("Array specification for symbol '%s' in COMMON "
4485 "at %C must be explicit", sym->name);
4486 goto cleanup;
4489 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
4490 goto cleanup;
4492 if (sym->attr.pointer)
4494 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
4495 "POINTER array", sym->name);
4496 goto cleanup;
4499 sym->as = as;
4500 as = NULL;
4504 sym->common_head = t;
4506 /* Check to see if the symbol is already in an equivalence group.
4507 If it is, set the other members as being in common. */
4508 if (sym->attr.in_equivalence)
4510 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
4512 for (e2 = e1; e2; e2 = e2->eq)
4513 if (e2->expr->symtree->n.sym == sym)
4514 goto equiv_found;
4516 continue;
4518 equiv_found:
4520 for (e2 = e1; e2; e2 = e2->eq)
4522 other = e2->expr->symtree->n.sym;
4523 if (other->common_head
4524 && other->common_head != sym->common_head)
4526 gfc_error ("Symbol '%s', in COMMON block '%s' at "
4527 "%C is being indirectly equivalenced to "
4528 "another COMMON block '%s'",
4529 sym->name, sym->common_head->name,
4530 other->common_head->name);
4531 goto cleanup;
4533 other->attr.in_common = 1;
4534 other->common_head = t;
4540 gfc_gobble_whitespace ();
4541 if (gfc_match_eos () == MATCH_YES)
4542 goto done;
4543 if (gfc_peek_ascii_char () == '/')
4544 break;
4545 if (gfc_match_char (',') != MATCH_YES)
4546 goto syntax;
4547 gfc_gobble_whitespace ();
4548 if (gfc_peek_ascii_char () == '/')
4549 break;
4553 done:
4554 return MATCH_YES;
4556 syntax:
4557 gfc_syntax_error (ST_COMMON);
4559 cleanup:
4560 if (old_blank_common)
4561 old_blank_common->common_next = NULL;
4562 else
4563 gfc_current_ns->blank_common.head = NULL;
4564 gfc_free_array_spec (as);
4565 return MATCH_ERROR;
4569 /* Match a BLOCK DATA program unit. */
4571 match
4572 gfc_match_block_data (void)
4574 char name[GFC_MAX_SYMBOL_LEN + 1];
4575 gfc_symbol *sym;
4576 match m;
4578 if (gfc_match_eos () == MATCH_YES)
4580 gfc_new_block = NULL;
4581 return MATCH_YES;
4584 m = gfc_match ("% %n%t", name);
4585 if (m != MATCH_YES)
4586 return MATCH_ERROR;
4588 if (gfc_get_symbol (name, NULL, &sym))
4589 return MATCH_ERROR;
4591 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
4592 return MATCH_ERROR;
4594 gfc_new_block = sym;
4596 return MATCH_YES;
4600 /* Free a namelist structure. */
4602 void
4603 gfc_free_namelist (gfc_namelist *name)
4605 gfc_namelist *n;
4607 for (; name; name = n)
4609 n = name->next;
4610 free (name);
4615 /* Match a NAMELIST statement. */
4617 match
4618 gfc_match_namelist (void)
4620 gfc_symbol *group_name, *sym;
4621 gfc_namelist *nl;
4622 match m, m2;
4624 m = gfc_match (" / %s /", &group_name);
4625 if (m == MATCH_NO)
4626 goto syntax;
4627 if (m == MATCH_ERROR)
4628 goto error;
4630 for (;;)
4632 if (group_name->ts.type != BT_UNKNOWN)
4634 gfc_error ("Namelist group name '%s' at %C already has a basic "
4635 "type of %s", group_name->name,
4636 gfc_typename (&group_name->ts));
4637 return MATCH_ERROR;
4640 if (group_name->attr.flavor == FL_NAMELIST
4641 && group_name->attr.use_assoc
4642 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
4643 "at %C already is USE associated and can"
4644 "not be respecified.", group_name->name)
4645 == FAILURE)
4646 return MATCH_ERROR;
4648 if (group_name->attr.flavor != FL_NAMELIST
4649 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
4650 group_name->name, NULL) == FAILURE)
4651 return MATCH_ERROR;
4653 for (;;)
4655 m = gfc_match_symbol (&sym, 1);
4656 if (m == MATCH_NO)
4657 goto syntax;
4658 if (m == MATCH_ERROR)
4659 goto error;
4661 if (sym->attr.in_namelist == 0
4662 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
4663 goto error;
4665 /* Use gfc_error_check here, rather than goto error, so that
4666 these are the only errors for the next two lines. */
4667 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
4669 gfc_error ("Assumed size array '%s' in namelist '%s' at "
4670 "%C is not allowed", sym->name, group_name->name);
4671 gfc_error_check ();
4674 nl = gfc_get_namelist ();
4675 nl->sym = sym;
4676 sym->refs++;
4678 if (group_name->namelist == NULL)
4679 group_name->namelist = group_name->namelist_tail = nl;
4680 else
4682 group_name->namelist_tail->next = nl;
4683 group_name->namelist_tail = nl;
4686 if (gfc_match_eos () == MATCH_YES)
4687 goto done;
4689 m = gfc_match_char (',');
4691 if (gfc_match_char ('/') == MATCH_YES)
4693 m2 = gfc_match (" %s /", &group_name);
4694 if (m2 == MATCH_YES)
4695 break;
4696 if (m2 == MATCH_ERROR)
4697 goto error;
4698 goto syntax;
4701 if (m != MATCH_YES)
4702 goto syntax;
4706 done:
4707 return MATCH_YES;
4709 syntax:
4710 gfc_syntax_error (ST_NAMELIST);
4712 error:
4713 return MATCH_ERROR;
4717 /* Match a MODULE statement. */
4719 match
4720 gfc_match_module (void)
4722 match m;
4724 m = gfc_match (" %s%t", &gfc_new_block);
4725 if (m != MATCH_YES)
4726 return m;
4728 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
4729 gfc_new_block->name, NULL) == FAILURE)
4730 return MATCH_ERROR;
4732 return MATCH_YES;
4736 /* Free equivalence sets and lists. Recursively is the easiest way to
4737 do this. */
4739 void
4740 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
4742 if (eq == stop)
4743 return;
4745 gfc_free_equiv (eq->eq);
4746 gfc_free_equiv_until (eq->next, stop);
4747 gfc_free_expr (eq->expr);
4748 free (eq);
4752 void
4753 gfc_free_equiv (gfc_equiv *eq)
4755 gfc_free_equiv_until (eq, NULL);
4759 /* Match an EQUIVALENCE statement. */
4761 match
4762 gfc_match_equivalence (void)
4764 gfc_equiv *eq, *set, *tail;
4765 gfc_ref *ref;
4766 gfc_symbol *sym;
4767 match m;
4768 gfc_common_head *common_head = NULL;
4769 bool common_flag;
4770 int cnt;
4772 tail = NULL;
4774 for (;;)
4776 eq = gfc_get_equiv ();
4777 if (tail == NULL)
4778 tail = eq;
4780 eq->next = gfc_current_ns->equiv;
4781 gfc_current_ns->equiv = eq;
4783 if (gfc_match_char ('(') != MATCH_YES)
4784 goto syntax;
4786 set = eq;
4787 common_flag = FALSE;
4788 cnt = 0;
4790 for (;;)
4792 m = gfc_match_equiv_variable (&set->expr);
4793 if (m == MATCH_ERROR)
4794 goto cleanup;
4795 if (m == MATCH_NO)
4796 goto syntax;
4798 /* count the number of objects. */
4799 cnt++;
4801 if (gfc_match_char ('%') == MATCH_YES)
4803 gfc_error ("Derived type component %C is not a "
4804 "permitted EQUIVALENCE member");
4805 goto cleanup;
4808 for (ref = set->expr->ref; ref; ref = ref->next)
4809 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
4811 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
4812 "be an array section");
4813 goto cleanup;
4816 sym = set->expr->symtree->n.sym;
4818 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
4819 goto cleanup;
4821 if (sym->attr.in_common)
4823 common_flag = TRUE;
4824 common_head = sym->common_head;
4827 if (gfc_match_char (')') == MATCH_YES)
4828 break;
4830 if (gfc_match_char (',') != MATCH_YES)
4831 goto syntax;
4833 set->eq = gfc_get_equiv ();
4834 set = set->eq;
4837 if (cnt < 2)
4839 gfc_error ("EQUIVALENCE at %C requires two or more objects");
4840 goto cleanup;
4843 /* If one of the members of an equivalence is in common, then
4844 mark them all as being in common. Before doing this, check
4845 that members of the equivalence group are not in different
4846 common blocks. */
4847 if (common_flag)
4848 for (set = eq; set; set = set->eq)
4850 sym = set->expr->symtree->n.sym;
4851 if (sym->common_head && sym->common_head != common_head)
4853 gfc_error ("Attempt to indirectly overlap COMMON "
4854 "blocks %s and %s by EQUIVALENCE at %C",
4855 sym->common_head->name, common_head->name);
4856 goto cleanup;
4858 sym->attr.in_common = 1;
4859 sym->common_head = common_head;
4862 if (gfc_match_eos () == MATCH_YES)
4863 break;
4864 if (gfc_match_char (',') != MATCH_YES)
4866 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
4867 goto cleanup;
4871 return MATCH_YES;
4873 syntax:
4874 gfc_syntax_error (ST_EQUIVALENCE);
4876 cleanup:
4877 eq = tail->next;
4878 tail->next = NULL;
4880 gfc_free_equiv (gfc_current_ns->equiv);
4881 gfc_current_ns->equiv = eq;
4883 return MATCH_ERROR;
4887 /* Check that a statement function is not recursive. This is done by looking
4888 for the statement function symbol(sym) by looking recursively through its
4889 expression(e). If a reference to sym is found, true is returned.
4890 12.5.4 requires that any variable of function that is implicitly typed
4891 shall have that type confirmed by any subsequent type declaration. The
4892 implicit typing is conveniently done here. */
4893 static bool
4894 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
4896 static bool
4897 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
4900 if (e == NULL)
4901 return false;
4903 switch (e->expr_type)
4905 case EXPR_FUNCTION:
4906 if (e->symtree == NULL)
4907 return false;
4909 /* Check the name before testing for nested recursion! */
4910 if (sym->name == e->symtree->n.sym->name)
4911 return true;
4913 /* Catch recursion via other statement functions. */
4914 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
4915 && e->symtree->n.sym->value
4916 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
4917 return true;
4919 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
4920 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
4922 break;
4924 case EXPR_VARIABLE:
4925 if (e->symtree && sym->name == e->symtree->n.sym->name)
4926 return true;
4928 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
4929 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
4930 break;
4932 default:
4933 break;
4936 return false;
4940 static bool
4941 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
4943 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
4947 /* Match a statement function declaration. It is so easy to match
4948 non-statement function statements with a MATCH_ERROR as opposed to
4949 MATCH_NO that we suppress error message in most cases. */
4951 match
4952 gfc_match_st_function (void)
4954 gfc_error_buf old_error;
4955 gfc_symbol *sym;
4956 gfc_expr *expr;
4957 match m;
4959 m = gfc_match_symbol (&sym, 0);
4960 if (m != MATCH_YES)
4961 return m;
4963 gfc_push_error (&old_error);
4965 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
4966 sym->name, NULL) == FAILURE)
4967 goto undo_error;
4969 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
4970 goto undo_error;
4972 m = gfc_match (" = %e%t", &expr);
4973 if (m == MATCH_NO)
4974 goto undo_error;
4976 gfc_free_error (&old_error);
4977 if (m == MATCH_ERROR)
4978 return m;
4980 if (recursive_stmt_fcn (expr, sym))
4982 gfc_error ("Statement function at %L is recursive", &expr->where);
4983 return MATCH_ERROR;
4986 sym->value = expr;
4988 if (gfc_notify_std (GFC_STD_F95_OBS,
4989 "Statement function at %C") == FAILURE)
4990 return MATCH_ERROR;
4992 return MATCH_YES;
4994 undo_error:
4995 gfc_pop_error (&old_error);
4996 return MATCH_NO;
5000 /***************** SELECT CASE subroutines ******************/
5002 /* Free a single case structure. */
5004 static void
5005 free_case (gfc_case *p)
5007 if (p->low == p->high)
5008 p->high = NULL;
5009 gfc_free_expr (p->low);
5010 gfc_free_expr (p->high);
5011 free (p);
5015 /* Free a list of case structures. */
5017 void
5018 gfc_free_case_list (gfc_case *p)
5020 gfc_case *q;
5022 for (; p; p = q)
5024 q = p->next;
5025 free_case (p);
5030 /* Match a single case selector. */
5032 static match
5033 match_case_selector (gfc_case **cp)
5035 gfc_case *c;
5036 match m;
5038 c = gfc_get_case ();
5039 c->where = gfc_current_locus;
5041 if (gfc_match_char (':') == MATCH_YES)
5043 m = gfc_match_init_expr (&c->high);
5044 if (m == MATCH_NO)
5045 goto need_expr;
5046 if (m == MATCH_ERROR)
5047 goto cleanup;
5049 else
5051 m = gfc_match_init_expr (&c->low);
5052 if (m == MATCH_ERROR)
5053 goto cleanup;
5054 if (m == MATCH_NO)
5055 goto need_expr;
5057 /* If we're not looking at a ':' now, make a range out of a single
5058 target. Else get the upper bound for the case range. */
5059 if (gfc_match_char (':') != MATCH_YES)
5060 c->high = c->low;
5061 else
5063 m = gfc_match_init_expr (&c->high);
5064 if (m == MATCH_ERROR)
5065 goto cleanup;
5066 /* MATCH_NO is fine. It's OK if nothing is there! */
5070 *cp = c;
5071 return MATCH_YES;
5073 need_expr:
5074 gfc_error ("Expected initialization expression in CASE at %C");
5076 cleanup:
5077 free_case (c);
5078 return MATCH_ERROR;
5082 /* Match the end of a case statement. */
5084 static match
5085 match_case_eos (void)
5087 char name[GFC_MAX_SYMBOL_LEN + 1];
5088 match m;
5090 if (gfc_match_eos () == MATCH_YES)
5091 return MATCH_YES;
5093 /* If the case construct doesn't have a case-construct-name, we
5094 should have matched the EOS. */
5095 if (!gfc_current_block ())
5096 return MATCH_NO;
5098 gfc_gobble_whitespace ();
5100 m = gfc_match_name (name);
5101 if (m != MATCH_YES)
5102 return m;
5104 if (strcmp (name, gfc_current_block ()->name) != 0)
5106 gfc_error ("Expected block name '%s' of SELECT construct at %C",
5107 gfc_current_block ()->name);
5108 return MATCH_ERROR;
5111 return gfc_match_eos ();
5115 /* Match a SELECT statement. */
5117 match
5118 gfc_match_select (void)
5120 gfc_expr *expr;
5121 match m;
5123 m = gfc_match_label ();
5124 if (m == MATCH_ERROR)
5125 return m;
5127 m = gfc_match (" select case ( %e )%t", &expr);
5128 if (m != MATCH_YES)
5129 return m;
5131 new_st.op = EXEC_SELECT;
5132 new_st.expr1 = expr;
5134 return MATCH_YES;
5138 /* Transfer the selector typespec to the associate name. */
5140 static void
5141 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
5143 gfc_ref *ref;
5144 gfc_symbol *assoc_sym;
5145 int i;
5147 assoc_sym = associate->symtree->n.sym;
5149 /* At this stage the expression rank and arrayspec dimensions have
5150 not been completely sorted out. We must get the expr2->rank
5151 right here, so that the correct class container is obtained. */
5152 ref = selector->ref;
5153 while (ref && ref->next)
5154 ref = ref->next;
5156 if (selector->ts.type == BT_CLASS
5157 && CLASS_DATA (selector)->as
5158 && ref && ref->type == REF_ARRAY)
5160 /* Ensure that the array reference type is set. We cannot use
5161 gfc_resolve_expr at this point, so the usable parts of
5162 resolve.c(resolve_array_ref) are employed to do it. */
5163 if (ref->u.ar.type == AR_UNKNOWN)
5165 ref->u.ar.type = AR_ELEMENT;
5166 for (i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
5167 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5168 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
5169 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
5170 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
5172 ref->u.ar.type = AR_SECTION;
5173 break;
5177 if (ref->u.ar.type == AR_FULL)
5178 selector->rank = CLASS_DATA (selector)->as->rank;
5179 else if (ref->u.ar.type == AR_SECTION)
5180 selector->rank = ref->u.ar.dimen;
5181 else
5182 selector->rank = 0;
5185 if (selector->ts.type != BT_CLASS)
5187 /* The correct class container has to be available. */
5188 if (selector->rank)
5190 assoc_sym->attr.dimension = 1;
5191 assoc_sym->as = gfc_get_array_spec ();
5192 assoc_sym->as->rank = selector->rank;
5193 assoc_sym->as->type = AS_DEFERRED;
5195 else
5196 assoc_sym->as = NULL;
5198 assoc_sym->ts.type = BT_CLASS;
5199 assoc_sym->ts.u.derived = selector->ts.u.derived;
5200 assoc_sym->attr.pointer = 1;
5201 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr,
5202 &assoc_sym->as, false);
5204 else
5206 /* The correct class container has to be available. */
5207 if (selector->rank)
5209 assoc_sym->attr.dimension = 1;
5210 assoc_sym->as = gfc_get_array_spec ();
5211 assoc_sym->as->rank = selector->rank;
5212 assoc_sym->as->type = AS_DEFERRED;
5214 else
5215 assoc_sym->as = NULL;
5216 assoc_sym->ts.type = BT_CLASS;
5217 assoc_sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
5218 assoc_sym->attr.pointer = 1;
5219 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr,
5220 &assoc_sym->as, false);
5225 /* Push the current selector onto the SELECT TYPE stack. */
5227 static void
5228 select_type_push (gfc_symbol *sel)
5230 gfc_select_type_stack *top = gfc_get_select_type_stack ();
5231 top->selector = sel;
5232 top->tmp = NULL;
5233 top->prev = select_type_stack;
5235 select_type_stack = top;
5239 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
5241 static gfc_symtree *
5242 select_intrinsic_set_tmp (gfc_typespec *ts)
5244 char name[GFC_MAX_SYMBOL_LEN];
5245 gfc_symtree *tmp;
5246 int charlen = 0;
5248 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
5249 return NULL;
5251 if (select_type_stack->selector->ts.type == BT_CLASS
5252 && !select_type_stack->selector->attr.class_ok)
5253 return NULL;
5255 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
5256 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
5257 charlen = mpz_get_si (ts->u.cl->length->value.integer);
5259 if (ts->type != BT_CHARACTER)
5260 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
5261 ts->kind);
5262 else
5263 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (ts->type),
5264 charlen, ts->kind);
5266 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5267 gfc_add_type (tmp->n.sym, ts, NULL);
5269 /* Copy across the array spec to the selector. */
5270 if (select_type_stack->selector->ts.type == BT_CLASS
5271 && (CLASS_DATA (select_type_stack->selector)->attr.dimension
5272 || CLASS_DATA (select_type_stack->selector)->attr.codimension))
5274 tmp->n.sym->attr.pointer = 1;
5275 tmp->n.sym->attr.dimension
5276 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5277 tmp->n.sym->attr.codimension
5278 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5279 tmp->n.sym->as
5280 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5283 gfc_set_sym_referenced (tmp->n.sym);
5284 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5285 tmp->n.sym->attr.select_type_temporary = 1;
5287 return tmp;
5291 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
5293 static void
5294 select_type_set_tmp (gfc_typespec *ts)
5296 char name[GFC_MAX_SYMBOL_LEN];
5297 gfc_symtree *tmp = NULL;
5299 if (!ts)
5301 select_type_stack->tmp = NULL;
5302 return;
5305 tmp = select_intrinsic_set_tmp (ts);
5307 if (tmp == NULL)
5309 if (!ts->u.derived)
5310 return;
5312 if (ts->type == BT_CLASS)
5313 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
5314 else
5315 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
5316 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5317 gfc_add_type (tmp->n.sym, ts, NULL);
5319 if (select_type_stack->selector->ts.type == BT_CLASS
5320 && select_type_stack->selector->attr.class_ok)
5322 tmp->n.sym->attr.pointer
5323 = CLASS_DATA (select_type_stack->selector)->attr.class_pointer;
5325 /* Copy across the array spec to the selector. */
5326 if (CLASS_DATA (select_type_stack->selector)->attr.dimension
5327 || CLASS_DATA (select_type_stack->selector)->attr.codimension)
5329 tmp->n.sym->attr.dimension
5330 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5331 tmp->n.sym->attr.codimension
5332 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5333 tmp->n.sym->as
5334 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5338 gfc_set_sym_referenced (tmp->n.sym);
5339 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5340 tmp->n.sym->attr.select_type_temporary = 1;
5342 if (ts->type == BT_CLASS)
5343 gfc_build_class_symbol (&tmp->n.sym->ts, &tmp->n.sym->attr,
5344 &tmp->n.sym->as, false);
5347 /* Add an association for it, so the rest of the parser knows it is
5348 an associate-name. The target will be set during resolution. */
5349 tmp->n.sym->assoc = gfc_get_association_list ();
5350 tmp->n.sym->assoc->dangling = 1;
5351 tmp->n.sym->assoc->st = tmp;
5353 select_type_stack->tmp = tmp;
5357 /* Match a SELECT TYPE statement. */
5359 match
5360 gfc_match_select_type (void)
5362 gfc_expr *expr1, *expr2 = NULL;
5363 match m;
5364 char name[GFC_MAX_SYMBOL_LEN];
5365 bool class_array;
5366 gfc_symbol *sym;
5367 gfc_namespace *parent_ns;
5369 m = gfc_match_label ();
5370 if (m == MATCH_ERROR)
5371 return m;
5373 m = gfc_match (" select type ( ");
5374 if (m != MATCH_YES)
5375 return m;
5377 gfc_current_ns = gfc_build_block_ns (gfc_current_ns);
5379 m = gfc_match (" %n => %e", name, &expr2);
5380 if (m == MATCH_YES)
5382 expr1 = gfc_get_expr();
5383 expr1->expr_type = EXPR_VARIABLE;
5384 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
5386 m = MATCH_ERROR;
5387 goto cleanup;
5390 sym = expr1->symtree->n.sym;
5391 if (expr2->ts.type == BT_UNKNOWN)
5392 sym->attr.untyped = 1;
5393 else
5394 copy_ts_from_selector_to_associate (expr1, expr2);
5396 sym->attr.flavor = FL_VARIABLE;
5397 sym->attr.referenced = 1;
5398 sym->attr.class_ok = 1;
5400 else
5402 m = gfc_match (" %e ", &expr1);
5403 if (m != MATCH_YES)
5404 goto cleanup;
5407 m = gfc_match (" )%t");
5408 if (m != MATCH_YES)
5409 goto cleanup;
5411 /* This ghastly expression seems to be needed to distinguish a CLASS
5412 array, which can have a reference, from other expressions that
5413 have references, such as derived type components, and are not
5414 allowed by the standard.
5415 TODO: see if it is sufficient to exclude component and substring
5416 references. */
5417 class_array = expr1->expr_type == EXPR_VARIABLE
5418 && expr1->ts.type == BT_CLASS
5419 && CLASS_DATA (expr1)
5420 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
5421 && (CLASS_DATA (expr1)->attr.dimension
5422 || CLASS_DATA (expr1)->attr.codimension)
5423 && expr1->ref
5424 && expr1->ref->type == REF_ARRAY
5425 && expr1->ref->next == NULL;
5427 /* Check for F03:C811. */
5428 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
5429 || (!class_array && expr1->ref != NULL)))
5431 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
5432 "use associate-name=>");
5433 m = MATCH_ERROR;
5434 goto cleanup;
5437 new_st.op = EXEC_SELECT_TYPE;
5438 new_st.expr1 = expr1;
5439 new_st.expr2 = expr2;
5440 new_st.ext.block.ns = gfc_current_ns;
5442 select_type_push (expr1->symtree->n.sym);
5444 return MATCH_YES;
5446 cleanup:
5447 parent_ns = gfc_current_ns->parent;
5448 gfc_free_namespace (gfc_current_ns);
5449 gfc_current_ns = parent_ns;
5450 return m;
5454 /* Match a CASE statement. */
5456 match
5457 gfc_match_case (void)
5459 gfc_case *c, *head, *tail;
5460 match m;
5462 head = tail = NULL;
5464 if (gfc_current_state () != COMP_SELECT)
5466 gfc_error ("Unexpected CASE statement at %C");
5467 return MATCH_ERROR;
5470 if (gfc_match ("% default") == MATCH_YES)
5472 m = match_case_eos ();
5473 if (m == MATCH_NO)
5474 goto syntax;
5475 if (m == MATCH_ERROR)
5476 goto cleanup;
5478 new_st.op = EXEC_SELECT;
5479 c = gfc_get_case ();
5480 c->where = gfc_current_locus;
5481 new_st.ext.block.case_list = c;
5482 return MATCH_YES;
5485 if (gfc_match_char ('(') != MATCH_YES)
5486 goto syntax;
5488 for (;;)
5490 if (match_case_selector (&c) == MATCH_ERROR)
5491 goto cleanup;
5493 if (head == NULL)
5494 head = c;
5495 else
5496 tail->next = c;
5498 tail = c;
5500 if (gfc_match_char (')') == MATCH_YES)
5501 break;
5502 if (gfc_match_char (',') != MATCH_YES)
5503 goto syntax;
5506 m = match_case_eos ();
5507 if (m == MATCH_NO)
5508 goto syntax;
5509 if (m == MATCH_ERROR)
5510 goto cleanup;
5512 new_st.op = EXEC_SELECT;
5513 new_st.ext.block.case_list = head;
5515 return MATCH_YES;
5517 syntax:
5518 gfc_error ("Syntax error in CASE specification at %C");
5520 cleanup:
5521 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
5522 return MATCH_ERROR;
5526 /* Match a TYPE IS statement. */
5528 match
5529 gfc_match_type_is (void)
5531 gfc_case *c = NULL;
5532 match m;
5534 if (gfc_current_state () != COMP_SELECT_TYPE)
5536 gfc_error ("Unexpected TYPE IS statement at %C");
5537 return MATCH_ERROR;
5540 if (gfc_match_char ('(') != MATCH_YES)
5541 goto syntax;
5543 c = gfc_get_case ();
5544 c->where = gfc_current_locus;
5546 if (match_type_spec (&c->ts) == MATCH_ERROR)
5547 goto cleanup;
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 if (c->ts.type == BT_DERIVED && c->ts.u.derived
5562 && (c->ts.u.derived->attr.sequence
5563 || c->ts.u.derived->attr.is_bind_c))
5565 gfc_error ("The type-spec shall not specify a sequence derived "
5566 "type or a type with the BIND attribute in SELECT "
5567 "TYPE at %C [F2003:C815]");
5568 return MATCH_ERROR;
5571 /* Create temporary variable. */
5572 select_type_set_tmp (&c->ts);
5574 return MATCH_YES;
5576 syntax:
5577 gfc_error ("Syntax error in TYPE IS specification at %C");
5579 cleanup:
5580 if (c != NULL)
5581 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
5582 return MATCH_ERROR;
5586 /* Match a CLASS IS or CLASS DEFAULT statement. */
5588 match
5589 gfc_match_class_is (void)
5591 gfc_case *c = NULL;
5592 match m;
5594 if (gfc_current_state () != COMP_SELECT_TYPE)
5595 return MATCH_NO;
5597 if (gfc_match ("% default") == MATCH_YES)
5599 m = match_case_eos ();
5600 if (m == MATCH_NO)
5601 goto syntax;
5602 if (m == MATCH_ERROR)
5603 goto cleanup;
5605 new_st.op = EXEC_SELECT_TYPE;
5606 c = gfc_get_case ();
5607 c->where = gfc_current_locus;
5608 c->ts.type = BT_UNKNOWN;
5609 new_st.ext.block.case_list = c;
5610 select_type_set_tmp (NULL);
5611 return MATCH_YES;
5614 m = gfc_match ("% is");
5615 if (m == MATCH_NO)
5616 goto syntax;
5617 if (m == MATCH_ERROR)
5618 goto cleanup;
5620 if (gfc_match_char ('(') != MATCH_YES)
5621 goto syntax;
5623 c = gfc_get_case ();
5624 c->where = gfc_current_locus;
5626 if (match_derived_type_spec (&c->ts) == MATCH_ERROR)
5627 goto cleanup;
5629 if (c->ts.type == BT_DERIVED)
5630 c->ts.type = BT_CLASS;
5632 if (gfc_match_char (')') != MATCH_YES)
5633 goto syntax;
5635 m = match_case_eos ();
5636 if (m == MATCH_NO)
5637 goto syntax;
5638 if (m == MATCH_ERROR)
5639 goto cleanup;
5641 new_st.op = EXEC_SELECT_TYPE;
5642 new_st.ext.block.case_list = c;
5644 /* Create temporary variable. */
5645 select_type_set_tmp (&c->ts);
5647 return MATCH_YES;
5649 syntax:
5650 gfc_error ("Syntax error in CLASS IS specification at %C");
5652 cleanup:
5653 if (c != NULL)
5654 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
5655 return MATCH_ERROR;
5659 /********************* WHERE subroutines ********************/
5661 /* Match the rest of a simple WHERE statement that follows an IF statement.
5664 static match
5665 match_simple_where (void)
5667 gfc_expr *expr;
5668 gfc_code *c;
5669 match m;
5671 m = gfc_match (" ( %e )", &expr);
5672 if (m != MATCH_YES)
5673 return m;
5675 m = gfc_match_assignment ();
5676 if (m == MATCH_NO)
5677 goto syntax;
5678 if (m == MATCH_ERROR)
5679 goto cleanup;
5681 if (gfc_match_eos () != MATCH_YES)
5682 goto syntax;
5684 c = gfc_get_code ();
5686 c->op = EXEC_WHERE;
5687 c->expr1 = expr;
5688 c->next = gfc_get_code ();
5690 *c->next = new_st;
5691 gfc_clear_new_st ();
5693 new_st.op = EXEC_WHERE;
5694 new_st.block = c;
5696 return MATCH_YES;
5698 syntax:
5699 gfc_syntax_error (ST_WHERE);
5701 cleanup:
5702 gfc_free_expr (expr);
5703 return MATCH_ERROR;
5707 /* Match a WHERE statement. */
5709 match
5710 gfc_match_where (gfc_statement *st)
5712 gfc_expr *expr;
5713 match m0, m;
5714 gfc_code *c;
5716 m0 = gfc_match_label ();
5717 if (m0 == MATCH_ERROR)
5718 return m0;
5720 m = gfc_match (" where ( %e )", &expr);
5721 if (m != MATCH_YES)
5722 return m;
5724 if (gfc_match_eos () == MATCH_YES)
5726 *st = ST_WHERE_BLOCK;
5727 new_st.op = EXEC_WHERE;
5728 new_st.expr1 = expr;
5729 return MATCH_YES;
5732 m = gfc_match_assignment ();
5733 if (m == MATCH_NO)
5734 gfc_syntax_error (ST_WHERE);
5736 if (m != MATCH_YES)
5738 gfc_free_expr (expr);
5739 return MATCH_ERROR;
5742 /* We've got a simple WHERE statement. */
5743 *st = ST_WHERE;
5744 c = gfc_get_code ();
5746 c->op = EXEC_WHERE;
5747 c->expr1 = expr;
5748 c->next = gfc_get_code ();
5750 *c->next = new_st;
5751 gfc_clear_new_st ();
5753 new_st.op = EXEC_WHERE;
5754 new_st.block = c;
5756 return MATCH_YES;
5760 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
5761 new_st if successful. */
5763 match
5764 gfc_match_elsewhere (void)
5766 char name[GFC_MAX_SYMBOL_LEN + 1];
5767 gfc_expr *expr;
5768 match m;
5770 if (gfc_current_state () != COMP_WHERE)
5772 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
5773 return MATCH_ERROR;
5776 expr = NULL;
5778 if (gfc_match_char ('(') == MATCH_YES)
5780 m = gfc_match_expr (&expr);
5781 if (m == MATCH_NO)
5782 goto syntax;
5783 if (m == MATCH_ERROR)
5784 return MATCH_ERROR;
5786 if (gfc_match_char (')') != MATCH_YES)
5787 goto syntax;
5790 if (gfc_match_eos () != MATCH_YES)
5792 /* Only makes sense if we have a where-construct-name. */
5793 if (!gfc_current_block ())
5795 m = MATCH_ERROR;
5796 goto cleanup;
5798 /* Better be a name at this point. */
5799 m = gfc_match_name (name);
5800 if (m == MATCH_NO)
5801 goto syntax;
5802 if (m == MATCH_ERROR)
5803 goto cleanup;
5805 if (gfc_match_eos () != MATCH_YES)
5806 goto syntax;
5808 if (strcmp (name, gfc_current_block ()->name) != 0)
5810 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
5811 name, gfc_current_block ()->name);
5812 goto cleanup;
5816 new_st.op = EXEC_WHERE;
5817 new_st.expr1 = expr;
5818 return MATCH_YES;
5820 syntax:
5821 gfc_syntax_error (ST_ELSEWHERE);
5823 cleanup:
5824 gfc_free_expr (expr);
5825 return MATCH_ERROR;