2010-10-20 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / match.c
blob836c95cc2df008b72c34ffa531acfb6cdef3a1f1
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010
4 2010 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "flags.h"
26 #include "gfortran.h"
27 #include "match.h"
28 #include "parse.h"
30 int gfc_matching_procptr_assignment = 0;
31 bool gfc_matching_prefix = false;
33 /* Stack of SELECT TYPE statements. */
34 gfc_select_type_stack *select_type_stack = NULL;
36 /* For debugging and diagnostic purposes. Return the textual representation
37 of the intrinsic operator OP. */
38 const char *
39 gfc_op2string (gfc_intrinsic_op op)
41 switch (op)
43 case INTRINSIC_UPLUS:
44 case INTRINSIC_PLUS:
45 return "+";
47 case INTRINSIC_UMINUS:
48 case INTRINSIC_MINUS:
49 return "-";
51 case INTRINSIC_POWER:
52 return "**";
53 case INTRINSIC_CONCAT:
54 return "//";
55 case INTRINSIC_TIMES:
56 return "*";
57 case INTRINSIC_DIVIDE:
58 return "/";
60 case INTRINSIC_AND:
61 return ".and.";
62 case INTRINSIC_OR:
63 return ".or.";
64 case INTRINSIC_EQV:
65 return ".eqv.";
66 case INTRINSIC_NEQV:
67 return ".neqv.";
69 case INTRINSIC_EQ_OS:
70 return ".eq.";
71 case INTRINSIC_EQ:
72 return "==";
73 case INTRINSIC_NE_OS:
74 return ".ne.";
75 case INTRINSIC_NE:
76 return "/=";
77 case INTRINSIC_GE_OS:
78 return ".ge.";
79 case INTRINSIC_GE:
80 return ">=";
81 case INTRINSIC_LE_OS:
82 return ".le.";
83 case INTRINSIC_LE:
84 return "<=";
85 case INTRINSIC_LT_OS:
86 return ".lt.";
87 case INTRINSIC_LT:
88 return "<";
89 case INTRINSIC_GT_OS:
90 return ".gt.";
91 case INTRINSIC_GT:
92 return ">";
93 case INTRINSIC_NOT:
94 return ".not.";
96 case INTRINSIC_ASSIGN:
97 return "=";
99 case INTRINSIC_PARENTHESES:
100 return "parens";
102 default:
103 break;
106 gfc_internal_error ("gfc_op2string(): Bad code");
107 /* Not reached. */
111 /******************** Generic matching subroutines ************************/
113 /* This function scans the current statement counting the opened and closed
114 parenthesis to make sure they are balanced. */
116 match
117 gfc_match_parens (void)
119 locus old_loc, where;
120 int count, instring;
121 gfc_char_t c, quote;
123 old_loc = gfc_current_locus;
124 count = 0;
125 instring = 0;
126 quote = ' ';
128 for (;;)
130 c = gfc_next_char_literal (instring);
131 if (c == '\n')
132 break;
133 if (quote == ' ' && ((c == '\'') || (c == '"')))
135 quote = c;
136 instring = 1;
137 continue;
139 if (quote != ' ' && c == quote)
141 quote = ' ';
142 instring = 0;
143 continue;
146 if (c == '(' && quote == ' ')
148 count++;
149 where = gfc_current_locus;
151 if (c == ')' && quote == ' ')
153 count--;
154 where = gfc_current_locus;
158 gfc_current_locus = old_loc;
160 if (count > 0)
162 gfc_error ("Missing ')' in statement at or before %L", &where);
163 return MATCH_ERROR;
165 if (count < 0)
167 gfc_error ("Missing '(' in statement at or before %L", &where);
168 return MATCH_ERROR;
171 return MATCH_YES;
175 /* See if the next character is a special character that has
176 escaped by a \ via the -fbackslash option. */
178 match
179 gfc_match_special_char (gfc_char_t *res)
181 int len, i;
182 gfc_char_t c, n;
183 match m;
185 m = MATCH_YES;
187 switch ((c = gfc_next_char_literal (1)))
189 case 'a':
190 *res = '\a';
191 break;
192 case 'b':
193 *res = '\b';
194 break;
195 case 't':
196 *res = '\t';
197 break;
198 case 'f':
199 *res = '\f';
200 break;
201 case 'n':
202 *res = '\n';
203 break;
204 case 'r':
205 *res = '\r';
206 break;
207 case 'v':
208 *res = '\v';
209 break;
210 case '\\':
211 *res = '\\';
212 break;
213 case '0':
214 *res = '\0';
215 break;
217 case 'x':
218 case 'u':
219 case 'U':
220 /* Hexadecimal form of wide characters. */
221 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
222 n = 0;
223 for (i = 0; i < len; i++)
225 char buf[2] = { '\0', '\0' };
227 c = gfc_next_char_literal (1);
228 if (!gfc_wide_fits_in_byte (c)
229 || !gfc_check_digit ((unsigned char) c, 16))
230 return MATCH_NO;
232 buf[0] = (unsigned char) c;
233 n = n << 4;
234 n += strtol (buf, NULL, 16);
236 *res = n;
237 break;
239 default:
240 /* Unknown backslash codes are simply not expanded. */
241 m = MATCH_NO;
242 break;
245 return m;
249 /* In free form, match at least one space. Always matches in fixed
250 form. */
252 match
253 gfc_match_space (void)
255 locus old_loc;
256 char c;
258 if (gfc_current_form == FORM_FIXED)
259 return MATCH_YES;
261 old_loc = gfc_current_locus;
263 c = gfc_next_ascii_char ();
264 if (!gfc_is_whitespace (c))
266 gfc_current_locus = old_loc;
267 return MATCH_NO;
270 gfc_gobble_whitespace ();
272 return MATCH_YES;
276 /* Match an end of statement. End of statement is optional
277 whitespace, followed by a ';' or '\n' or comment '!'. If a
278 semicolon is found, we continue to eat whitespace and semicolons. */
280 match
281 gfc_match_eos (void)
283 locus old_loc;
284 int flag;
285 char c;
287 flag = 0;
289 for (;;)
291 old_loc = gfc_current_locus;
292 gfc_gobble_whitespace ();
294 c = gfc_next_ascii_char ();
295 switch (c)
297 case '!':
300 c = gfc_next_ascii_char ();
302 while (c != '\n');
304 /* Fall through. */
306 case '\n':
307 return MATCH_YES;
309 case ';':
310 flag = 1;
311 continue;
314 break;
317 gfc_current_locus = old_loc;
318 return (flag) ? MATCH_YES : MATCH_NO;
322 /* Match a literal integer on the input, setting the value on
323 MATCH_YES. Literal ints occur in kind-parameters as well as
324 old-style character length specifications. If cnt is non-NULL it
325 will be set to the number of digits. */
327 match
328 gfc_match_small_literal_int (int *value, int *cnt)
330 locus old_loc;
331 char c;
332 int i, j;
334 old_loc = gfc_current_locus;
336 *value = -1;
337 gfc_gobble_whitespace ();
338 c = gfc_next_ascii_char ();
339 if (cnt)
340 *cnt = 0;
342 if (!ISDIGIT (c))
344 gfc_current_locus = old_loc;
345 return MATCH_NO;
348 i = c - '0';
349 j = 1;
351 for (;;)
353 old_loc = gfc_current_locus;
354 c = gfc_next_ascii_char ();
356 if (!ISDIGIT (c))
357 break;
359 i = 10 * i + c - '0';
360 j++;
362 if (i > 99999999)
364 gfc_error ("Integer too large at %C");
365 return MATCH_ERROR;
369 gfc_current_locus = old_loc;
371 *value = i;
372 if (cnt)
373 *cnt = j;
374 return MATCH_YES;
378 /* Match a small, constant integer expression, like in a kind
379 statement. On MATCH_YES, 'value' is set. */
381 match
382 gfc_match_small_int (int *value)
384 gfc_expr *expr;
385 const char *p;
386 match m;
387 int i;
389 m = gfc_match_expr (&expr);
390 if (m != MATCH_YES)
391 return m;
393 p = gfc_extract_int (expr, &i);
394 gfc_free_expr (expr);
396 if (p != NULL)
398 gfc_error (p);
399 m = MATCH_ERROR;
402 *value = i;
403 return m;
407 /* This function is the same as the gfc_match_small_int, except that
408 we're keeping the pointer to the expr. This function could just be
409 removed and the previously mentioned one modified, though all calls
410 to it would have to be modified then (and there were a number of
411 them). Return MATCH_ERROR if fail to extract the int; otherwise,
412 return the result of gfc_match_expr(). The expr (if any) that was
413 matched is returned in the parameter expr. */
415 match
416 gfc_match_small_int_expr (int *value, gfc_expr **expr)
418 const char *p;
419 match m;
420 int i;
422 m = gfc_match_expr (expr);
423 if (m != MATCH_YES)
424 return m;
426 p = gfc_extract_int (*expr, &i);
428 if (p != NULL)
430 gfc_error (p);
431 m = MATCH_ERROR;
434 *value = i;
435 return m;
439 /* Matches a statement label. Uses gfc_match_small_literal_int() to
440 do most of the work. */
442 match
443 gfc_match_st_label (gfc_st_label **label)
445 locus old_loc;
446 match m;
447 int i, cnt;
449 old_loc = gfc_current_locus;
451 m = gfc_match_small_literal_int (&i, &cnt);
452 if (m != MATCH_YES)
453 return m;
455 if (cnt > 5)
457 gfc_error ("Too many digits in statement label at %C");
458 goto cleanup;
461 if (i == 0)
463 gfc_error ("Statement label at %C is zero");
464 goto cleanup;
467 *label = gfc_get_st_label (i);
468 return MATCH_YES;
470 cleanup:
472 gfc_current_locus = old_loc;
473 return MATCH_ERROR;
477 /* Match and validate a label associated with a named IF, DO or SELECT
478 statement. If the symbol does not have the label attribute, we add
479 it. We also make sure the symbol does not refer to another
480 (active) block. A matched label is pointed to by gfc_new_block. */
482 match
483 gfc_match_label (void)
485 char name[GFC_MAX_SYMBOL_LEN + 1];
486 match m;
488 gfc_new_block = NULL;
490 m = gfc_match (" %n :", name);
491 if (m != MATCH_YES)
492 return m;
494 if (gfc_get_symbol (name, NULL, &gfc_new_block))
496 gfc_error ("Label name '%s' at %C is ambiguous", name);
497 return MATCH_ERROR;
500 if (gfc_new_block->attr.flavor == FL_LABEL)
502 gfc_error ("Duplicate construct label '%s' at %C", name);
503 return MATCH_ERROR;
506 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
507 gfc_new_block->name, NULL) == FAILURE)
508 return MATCH_ERROR;
510 return MATCH_YES;
514 /* See if the current input looks like a name of some sort. Modifies
515 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
516 Note that options.c restricts max_identifier_length to not more
517 than GFC_MAX_SYMBOL_LEN. */
519 match
520 gfc_match_name (char *buffer)
522 locus old_loc;
523 int i;
524 char c;
526 old_loc = gfc_current_locus;
527 gfc_gobble_whitespace ();
529 c = gfc_next_ascii_char ();
530 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
532 if (gfc_error_flag_test() == 0 && c != '(')
533 gfc_error ("Invalid character in name at %C");
534 gfc_current_locus = old_loc;
535 return MATCH_NO;
538 i = 0;
542 buffer[i++] = c;
544 if (i > gfc_option.max_identifier_length)
546 gfc_error ("Name at %C is too long");
547 return MATCH_ERROR;
550 old_loc = gfc_current_locus;
551 c = gfc_next_ascii_char ();
553 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
555 if (c == '$' && !gfc_option.flag_dollar_ok)
557 gfc_error ("Invalid character '$' at %C. Use -fdollar-ok to allow it "
558 "as an extension");
559 return MATCH_ERROR;
562 buffer[i] = '\0';
563 gfc_current_locus = old_loc;
565 return MATCH_YES;
569 /* Match a valid name for C, which is almost the same as for Fortran,
570 except that you can start with an underscore, etc.. It could have
571 been done by modifying the gfc_match_name, but this way other
572 things C allows can be added, such as no limits on the length.
573 Right now, the length is limited to the same thing as Fortran..
574 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
575 input characters from being automatically lower cased, since C is
576 case sensitive. The parameter, buffer, is used to return the name
577 that is matched. Return MATCH_ERROR if the name is too long
578 (though this is a self-imposed limit), MATCH_NO if what we're
579 seeing isn't a name, and MATCH_YES if we successfully match a C
580 name. */
582 match
583 gfc_match_name_C (char *buffer)
585 locus old_loc;
586 int i = 0;
587 gfc_char_t c;
589 old_loc = gfc_current_locus;
590 gfc_gobble_whitespace ();
592 /* Get the next char (first possible char of name) and see if
593 it's valid for C (either a letter or an underscore). */
594 c = gfc_next_char_literal (1);
596 /* If the user put nothing expect spaces between the quotes, it is valid
597 and simply means there is no name= specifier and the name is the fortran
598 symbol name, all lowercase. */
599 if (c == '"' || c == '\'')
601 buffer[0] = '\0';
602 gfc_current_locus = old_loc;
603 return MATCH_YES;
606 if (!ISALPHA (c) && c != '_')
608 gfc_error ("Invalid C name in NAME= specifier at %C");
609 return MATCH_ERROR;
612 /* Continue to read valid variable name characters. */
615 gcc_assert (gfc_wide_fits_in_byte (c));
617 buffer[i++] = (unsigned char) c;
619 /* C does not define a maximum length of variable names, to my
620 knowledge, but the compiler typically places a limit on them.
621 For now, i'll use the same as the fortran limit for simplicity,
622 but this may need to be changed to a dynamic buffer that can
623 be realloc'ed here if necessary, or more likely, a larger
624 upper-bound set. */
625 if (i > gfc_option.max_identifier_length)
627 gfc_error ("Name at %C is too long");
628 return MATCH_ERROR;
631 old_loc = gfc_current_locus;
633 /* Get next char; param means we're in a string. */
634 c = gfc_next_char_literal (1);
635 } while (ISALNUM (c) || c == '_');
637 buffer[i] = '\0';
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_procptr_assignment = 0;
1336 m = gfc_match (" %v =>", &lvalue);
1337 if (m != MATCH_YES)
1339 m = MATCH_NO;
1340 goto cleanup;
1343 if (lvalue->symtree->n.sym->attr.proc_pointer
1344 || gfc_is_proc_ptr_comp (lvalue, NULL))
1345 gfc_matching_procptr_assignment = 1;
1347 m = gfc_match (" %e%t", &rvalue);
1348 gfc_matching_procptr_assignment = 0;
1349 if (m != MATCH_YES)
1350 goto cleanup;
1352 new_st.op = EXEC_POINTER_ASSIGN;
1353 new_st.expr1 = lvalue;
1354 new_st.expr2 = rvalue;
1356 return MATCH_YES;
1358 cleanup:
1359 gfc_current_locus = old_loc;
1360 gfc_free_expr (lvalue);
1361 gfc_free_expr (rvalue);
1362 return m;
1366 /* We try to match an easy arithmetic IF statement. This only happens
1367 when just after having encountered a simple IF statement. This code
1368 is really duplicate with parts of the gfc_match_if code, but this is
1369 *much* easier. */
1371 static match
1372 match_arithmetic_if (void)
1374 gfc_st_label *l1, *l2, *l3;
1375 gfc_expr *expr;
1376 match m;
1378 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1379 if (m != MATCH_YES)
1380 return m;
1382 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1383 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1384 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1386 gfc_free_expr (expr);
1387 return MATCH_ERROR;
1390 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Arithmetic IF "
1391 "statement at %C") == FAILURE)
1392 return MATCH_ERROR;
1394 new_st.op = EXEC_ARITHMETIC_IF;
1395 new_st.expr1 = expr;
1396 new_st.label1 = l1;
1397 new_st.label2 = l2;
1398 new_st.label3 = l3;
1400 return MATCH_YES;
1404 /* The IF statement is a bit of a pain. First of all, there are three
1405 forms of it, the simple IF, the IF that starts a block and the
1406 arithmetic IF.
1408 There is a problem with the simple IF and that is the fact that we
1409 only have a single level of undo information on symbols. What this
1410 means is for a simple IF, we must re-match the whole IF statement
1411 multiple times in order to guarantee that the symbol table ends up
1412 in the proper state. */
1414 static match match_simple_forall (void);
1415 static match match_simple_where (void);
1417 match
1418 gfc_match_if (gfc_statement *if_type)
1420 gfc_expr *expr;
1421 gfc_st_label *l1, *l2, *l3;
1422 locus old_loc, old_loc2;
1423 gfc_code *p;
1424 match m, n;
1426 n = gfc_match_label ();
1427 if (n == MATCH_ERROR)
1428 return n;
1430 old_loc = gfc_current_locus;
1432 m = gfc_match (" if ( %e", &expr);
1433 if (m != MATCH_YES)
1434 return m;
1436 old_loc2 = gfc_current_locus;
1437 gfc_current_locus = old_loc;
1439 if (gfc_match_parens () == MATCH_ERROR)
1440 return MATCH_ERROR;
1442 gfc_current_locus = old_loc2;
1444 if (gfc_match_char (')') != MATCH_YES)
1446 gfc_error ("Syntax error in IF-expression at %C");
1447 gfc_free_expr (expr);
1448 return MATCH_ERROR;
1451 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1453 if (m == MATCH_YES)
1455 if (n == MATCH_YES)
1457 gfc_error ("Block label not appropriate for arithmetic IF "
1458 "statement at %C");
1459 gfc_free_expr (expr);
1460 return MATCH_ERROR;
1463 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1464 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1465 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1467 gfc_free_expr (expr);
1468 return MATCH_ERROR;
1471 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Arithmetic IF "
1472 "statement at %C") == FAILURE)
1473 return MATCH_ERROR;
1475 new_st.op = EXEC_ARITHMETIC_IF;
1476 new_st.expr1 = expr;
1477 new_st.label1 = l1;
1478 new_st.label2 = l2;
1479 new_st.label3 = l3;
1481 *if_type = ST_ARITHMETIC_IF;
1482 return MATCH_YES;
1485 if (gfc_match (" then%t") == MATCH_YES)
1487 new_st.op = EXEC_IF;
1488 new_st.expr1 = expr;
1489 *if_type = ST_IF_BLOCK;
1490 return MATCH_YES;
1493 if (n == MATCH_YES)
1495 gfc_error ("Block label is not appropriate for IF statement at %C");
1496 gfc_free_expr (expr);
1497 return MATCH_ERROR;
1500 /* At this point the only thing left is a simple IF statement. At
1501 this point, n has to be MATCH_NO, so we don't have to worry about
1502 re-matching a block label. From what we've got so far, try
1503 matching an assignment. */
1505 *if_type = ST_SIMPLE_IF;
1507 m = gfc_match_assignment ();
1508 if (m == MATCH_YES)
1509 goto got_match;
1511 gfc_free_expr (expr);
1512 gfc_undo_symbols ();
1513 gfc_current_locus = old_loc;
1515 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1516 assignment was found. For MATCH_NO, continue to call the various
1517 matchers. */
1518 if (m == MATCH_ERROR)
1519 return MATCH_ERROR;
1521 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1523 m = gfc_match_pointer_assignment ();
1524 if (m == MATCH_YES)
1525 goto got_match;
1527 gfc_free_expr (expr);
1528 gfc_undo_symbols ();
1529 gfc_current_locus = old_loc;
1531 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1533 /* Look at the next keyword to see which matcher to call. Matching
1534 the keyword doesn't affect the symbol table, so we don't have to
1535 restore between tries. */
1537 #define match(string, subr, statement) \
1538 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1540 gfc_clear_error ();
1542 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1543 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1544 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1545 match ("call", gfc_match_call, ST_CALL)
1546 match ("close", gfc_match_close, ST_CLOSE)
1547 match ("continue", gfc_match_continue, ST_CONTINUE)
1548 match ("cycle", gfc_match_cycle, ST_CYCLE)
1549 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1550 match ("end file", gfc_match_endfile, ST_END_FILE)
1551 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1552 match ("exit", gfc_match_exit, ST_EXIT)
1553 match ("flush", gfc_match_flush, ST_FLUSH)
1554 match ("forall", match_simple_forall, ST_FORALL)
1555 match ("go to", gfc_match_goto, ST_GOTO)
1556 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1557 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1558 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1559 match ("open", gfc_match_open, ST_OPEN)
1560 match ("pause", gfc_match_pause, ST_NONE)
1561 match ("print", gfc_match_print, ST_WRITE)
1562 match ("read", gfc_match_read, ST_READ)
1563 match ("return", gfc_match_return, ST_RETURN)
1564 match ("rewind", gfc_match_rewind, ST_REWIND)
1565 match ("stop", gfc_match_stop, ST_STOP)
1566 match ("wait", gfc_match_wait, ST_WAIT)
1567 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1568 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1569 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1570 match ("where", match_simple_where, ST_WHERE)
1571 match ("write", gfc_match_write, ST_WRITE)
1573 /* The gfc_match_assignment() above may have returned a MATCH_NO
1574 where the assignment was to a named constant. Check that
1575 special case here. */
1576 m = gfc_match_assignment ();
1577 if (m == MATCH_NO)
1579 gfc_error ("Cannot assign to a named constant at %C");
1580 gfc_free_expr (expr);
1581 gfc_undo_symbols ();
1582 gfc_current_locus = old_loc;
1583 return MATCH_ERROR;
1586 /* All else has failed, so give up. See if any of the matchers has
1587 stored an error message of some sort. */
1588 if (gfc_error_check () == 0)
1589 gfc_error ("Unclassifiable statement in IF-clause at %C");
1591 gfc_free_expr (expr);
1592 return MATCH_ERROR;
1594 got_match:
1595 if (m == MATCH_NO)
1596 gfc_error ("Syntax error in IF-clause at %C");
1597 if (m != MATCH_YES)
1599 gfc_free_expr (expr);
1600 return MATCH_ERROR;
1603 /* At this point, we've matched the single IF and the action clause
1604 is in new_st. Rearrange things so that the IF statement appears
1605 in new_st. */
1607 p = gfc_get_code ();
1608 p->next = gfc_get_code ();
1609 *p->next = new_st;
1610 p->next->loc = gfc_current_locus;
1612 p->expr1 = expr;
1613 p->op = EXEC_IF;
1615 gfc_clear_new_st ();
1617 new_st.op = EXEC_IF;
1618 new_st.block = p;
1620 return MATCH_YES;
1623 #undef match
1626 /* Match an ELSE statement. */
1628 match
1629 gfc_match_else (void)
1631 char name[GFC_MAX_SYMBOL_LEN + 1];
1633 if (gfc_match_eos () == MATCH_YES)
1634 return MATCH_YES;
1636 if (gfc_match_name (name) != MATCH_YES
1637 || gfc_current_block () == NULL
1638 || gfc_match_eos () != MATCH_YES)
1640 gfc_error ("Unexpected junk after ELSE statement at %C");
1641 return MATCH_ERROR;
1644 if (strcmp (name, gfc_current_block ()->name) != 0)
1646 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1647 name, gfc_current_block ()->name);
1648 return MATCH_ERROR;
1651 return MATCH_YES;
1655 /* Match an ELSE IF statement. */
1657 match
1658 gfc_match_elseif (void)
1660 char name[GFC_MAX_SYMBOL_LEN + 1];
1661 gfc_expr *expr;
1662 match m;
1664 m = gfc_match (" ( %e ) then", &expr);
1665 if (m != MATCH_YES)
1666 return m;
1668 if (gfc_match_eos () == MATCH_YES)
1669 goto done;
1671 if (gfc_match_name (name) != MATCH_YES
1672 || gfc_current_block () == NULL
1673 || gfc_match_eos () != MATCH_YES)
1675 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1676 goto cleanup;
1679 if (strcmp (name, gfc_current_block ()->name) != 0)
1681 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1682 name, gfc_current_block ()->name);
1683 goto cleanup;
1686 done:
1687 new_st.op = EXEC_IF;
1688 new_st.expr1 = expr;
1689 return MATCH_YES;
1691 cleanup:
1692 gfc_free_expr (expr);
1693 return MATCH_ERROR;
1697 /* Free a gfc_iterator structure. */
1699 void
1700 gfc_free_iterator (gfc_iterator *iter, int flag)
1703 if (iter == NULL)
1704 return;
1706 gfc_free_expr (iter->var);
1707 gfc_free_expr (iter->start);
1708 gfc_free_expr (iter->end);
1709 gfc_free_expr (iter->step);
1711 if (flag)
1712 gfc_free (iter);
1716 /* Match a CRITICAL statement. */
1717 match
1718 gfc_match_critical (void)
1720 gfc_st_label *label = NULL;
1722 if (gfc_match_label () == MATCH_ERROR)
1723 return MATCH_ERROR;
1725 if (gfc_match (" critical") != MATCH_YES)
1726 return MATCH_NO;
1728 if (gfc_match_st_label (&label) == MATCH_ERROR)
1729 return MATCH_ERROR;
1731 if (gfc_match_eos () != MATCH_YES)
1733 gfc_syntax_error (ST_CRITICAL);
1734 return MATCH_ERROR;
1737 if (gfc_pure (NULL))
1739 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1740 return MATCH_ERROR;
1743 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CRITICAL statement at %C")
1744 == FAILURE)
1745 return MATCH_ERROR;
1747 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
1749 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
1750 return MATCH_ERROR;
1753 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
1755 gfc_error ("Nested CRITICAL block at %C");
1756 return MATCH_ERROR;
1759 new_st.op = EXEC_CRITICAL;
1761 if (label != NULL
1762 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1763 return MATCH_ERROR;
1765 return MATCH_YES;
1769 /* Match a BLOCK statement. */
1771 match
1772 gfc_match_block (void)
1774 match m;
1776 if (gfc_match_label () == MATCH_ERROR)
1777 return MATCH_ERROR;
1779 if (gfc_match (" block") != MATCH_YES)
1780 return MATCH_NO;
1782 /* For this to be a correct BLOCK statement, the line must end now. */
1783 m = gfc_match_eos ();
1784 if (m == MATCH_ERROR)
1785 return MATCH_ERROR;
1786 if (m == MATCH_NO)
1787 return MATCH_NO;
1789 return MATCH_YES;
1793 /* Match an ASSOCIATE statement. */
1795 match
1796 gfc_match_associate (void)
1798 if (gfc_match_label () == MATCH_ERROR)
1799 return MATCH_ERROR;
1801 if (gfc_match (" associate") != MATCH_YES)
1802 return MATCH_NO;
1804 /* Match the association list. */
1805 if (gfc_match_char ('(') != MATCH_YES)
1807 gfc_error ("Expected association list at %C");
1808 return MATCH_ERROR;
1810 new_st.ext.block.assoc = NULL;
1811 while (true)
1813 gfc_association_list* newAssoc = gfc_get_association_list ();
1814 gfc_association_list* a;
1816 /* Match the next association. */
1817 if (gfc_match (" %n => %e", newAssoc->name, &newAssoc->target)
1818 != MATCH_YES)
1820 gfc_error ("Expected association at %C");
1821 goto assocListError;
1823 newAssoc->where = gfc_current_locus;
1825 /* Check that the current name is not yet in the list. */
1826 for (a = new_st.ext.block.assoc; a; a = a->next)
1827 if (!strcmp (a->name, newAssoc->name))
1829 gfc_error ("Duplicate name '%s' in association at %C",
1830 newAssoc->name);
1831 goto assocListError;
1834 /* The target expression must not be coindexed. */
1835 if (gfc_is_coindexed (newAssoc->target))
1837 gfc_error ("Association target at %C must not be coindexed");
1838 goto assocListError;
1841 /* The `variable' field is left blank for now; because the target is not
1842 yet resolved, we can't use gfc_has_vector_subscript to determine it
1843 for now. This is set during resolution. */
1845 /* Put it into the list. */
1846 newAssoc->next = new_st.ext.block.assoc;
1847 new_st.ext.block.assoc = newAssoc;
1849 /* Try next one or end if closing parenthesis is found. */
1850 gfc_gobble_whitespace ();
1851 if (gfc_peek_char () == ')')
1852 break;
1853 if (gfc_match_char (',') != MATCH_YES)
1855 gfc_error ("Expected ')' or ',' at %C");
1856 return MATCH_ERROR;
1859 continue;
1861 assocListError:
1862 gfc_free (newAssoc);
1863 goto error;
1865 if (gfc_match_char (')') != MATCH_YES)
1867 /* This should never happen as we peek above. */
1868 gcc_unreachable ();
1871 if (gfc_match_eos () != MATCH_YES)
1873 gfc_error ("Junk after ASSOCIATE statement at %C");
1874 goto error;
1877 return MATCH_YES;
1879 error:
1880 gfc_free_association_list (new_st.ext.block.assoc);
1881 return MATCH_ERROR;
1885 /* Match a DO statement. */
1887 match
1888 gfc_match_do (void)
1890 gfc_iterator iter, *ip;
1891 locus old_loc;
1892 gfc_st_label *label;
1893 match m;
1895 old_loc = gfc_current_locus;
1897 label = NULL;
1898 iter.var = iter.start = iter.end = iter.step = NULL;
1900 m = gfc_match_label ();
1901 if (m == MATCH_ERROR)
1902 return m;
1904 if (gfc_match (" do") != MATCH_YES)
1905 return MATCH_NO;
1907 m = gfc_match_st_label (&label);
1908 if (m == MATCH_ERROR)
1909 goto cleanup;
1911 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1913 if (gfc_match_eos () == MATCH_YES)
1915 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
1916 new_st.op = EXEC_DO_WHILE;
1917 goto done;
1920 /* Match an optional comma, if no comma is found, a space is obligatory. */
1921 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1922 return MATCH_NO;
1924 /* Check for balanced parens. */
1926 if (gfc_match_parens () == MATCH_ERROR)
1927 return MATCH_ERROR;
1929 /* See if we have a DO WHILE. */
1930 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1932 new_st.op = EXEC_DO_WHILE;
1933 goto done;
1936 /* The abortive DO WHILE may have done something to the symbol
1937 table, so we start over. */
1938 gfc_undo_symbols ();
1939 gfc_current_locus = old_loc;
1941 gfc_match_label (); /* This won't error. */
1942 gfc_match (" do "); /* This will work. */
1944 gfc_match_st_label (&label); /* Can't error out. */
1945 gfc_match_char (','); /* Optional comma. */
1947 m = gfc_match_iterator (&iter, 0);
1948 if (m == MATCH_NO)
1949 return MATCH_NO;
1950 if (m == MATCH_ERROR)
1951 goto cleanup;
1953 iter.var->symtree->n.sym->attr.implied_index = 0;
1954 gfc_check_do_variable (iter.var->symtree);
1956 if (gfc_match_eos () != MATCH_YES)
1958 gfc_syntax_error (ST_DO);
1959 goto cleanup;
1962 new_st.op = EXEC_DO;
1964 done:
1965 if (label != NULL
1966 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1967 goto cleanup;
1969 new_st.label1 = label;
1971 if (new_st.op == EXEC_DO_WHILE)
1972 new_st.expr1 = iter.end;
1973 else
1975 new_st.ext.iterator = ip = gfc_get_iterator ();
1976 *ip = iter;
1979 return MATCH_YES;
1981 cleanup:
1982 gfc_free_iterator (&iter, 0);
1984 return MATCH_ERROR;
1988 /* Match an EXIT or CYCLE statement. */
1990 static match
1991 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1993 gfc_state_data *p, *o;
1994 gfc_symbol *sym;
1995 match m;
1996 int cnt;
1998 if (gfc_match_eos () == MATCH_YES)
1999 sym = NULL;
2000 else
2002 char name[GFC_MAX_SYMBOL_LEN + 1];
2003 gfc_symtree* stree;
2005 m = gfc_match ("% %n%t", name);
2006 if (m == MATCH_ERROR)
2007 return MATCH_ERROR;
2008 if (m == MATCH_NO)
2010 gfc_syntax_error (st);
2011 return MATCH_ERROR;
2014 /* Find the corresponding symbol. If there's a BLOCK statement
2015 between here and the label, it is not in gfc_current_ns but a parent
2016 namespace! */
2017 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2018 if (!stree)
2020 gfc_error ("Name '%s' in %s statement at %C is unknown",
2021 name, gfc_ascii_statement (st));
2022 return MATCH_ERROR;
2025 sym = stree->n.sym;
2026 if (sym->attr.flavor != FL_LABEL)
2028 gfc_error ("Name '%s' in %s statement at %C is not a construct name",
2029 name, gfc_ascii_statement (st));
2030 return MATCH_ERROR;
2034 /* Find the loop specified by the label (or lack of a label). */
2035 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2036 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2037 o = p;
2038 else if (p->state == COMP_CRITICAL)
2040 gfc_error("%s statement at %C leaves CRITICAL construct",
2041 gfc_ascii_statement (st));
2042 return MATCH_ERROR;
2044 else if ((sym && sym == p->sym) || (!sym && p->state == COMP_DO))
2045 break;
2047 if (p == NULL)
2049 if (sym == NULL)
2050 gfc_error ("%s statement at %C is not within a construct",
2051 gfc_ascii_statement (st));
2052 else
2053 gfc_error ("%s statement at %C is not within construct '%s'",
2054 gfc_ascii_statement (st), sym->name);
2056 return MATCH_ERROR;
2059 /* Special checks for EXIT from non-loop constructs. */
2060 switch (p->state)
2062 case COMP_DO:
2063 break;
2065 case COMP_CRITICAL:
2066 /* This is already handled above. */
2067 gcc_unreachable ();
2069 case COMP_ASSOCIATE:
2070 case COMP_BLOCK:
2071 case COMP_IF:
2072 case COMP_SELECT:
2073 case COMP_SELECT_TYPE:
2074 gcc_assert (sym);
2075 if (op == EXEC_CYCLE)
2077 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2078 " construct '%s'", sym->name);
2079 return MATCH_ERROR;
2081 gcc_assert (op == EXEC_EXIT);
2082 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: EXIT statement with no"
2083 " do-construct-name at %C") == FAILURE)
2084 return MATCH_ERROR;
2085 break;
2087 default:
2088 gfc_error ("%s statement at %C is not applicable to construct '%s'",
2089 gfc_ascii_statement (st), sym->name);
2090 return MATCH_ERROR;
2093 if (o != NULL)
2095 gfc_error ("%s statement at %C leaving OpenMP structured block",
2096 gfc_ascii_statement (st));
2097 return MATCH_ERROR;
2100 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2101 o = o->previous;
2102 if (cnt > 0
2103 && o != NULL
2104 && o->state == COMP_OMP_STRUCTURED_BLOCK
2105 && (o->head->op == EXEC_OMP_DO
2106 || o->head->op == EXEC_OMP_PARALLEL_DO))
2108 int collapse = 1;
2109 gcc_assert (o->head->next != NULL
2110 && (o->head->next->op == EXEC_DO
2111 || o->head->next->op == EXEC_DO_WHILE)
2112 && o->previous != NULL
2113 && o->previous->tail->op == o->head->op);
2114 if (o->previous->tail->ext.omp_clauses != NULL
2115 && o->previous->tail->ext.omp_clauses->collapse > 1)
2116 collapse = o->previous->tail->ext.omp_clauses->collapse;
2117 if (st == ST_EXIT && cnt <= collapse)
2119 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2120 return MATCH_ERROR;
2122 if (st == ST_CYCLE && cnt < collapse)
2124 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2125 " !$OMP DO loop");
2126 return MATCH_ERROR;
2130 /* Save the first statement in the construct - needed by the backend. */
2131 new_st.ext.which_construct = p->construct;
2133 new_st.op = op;
2135 return MATCH_YES;
2139 /* Match the EXIT statement. */
2141 match
2142 gfc_match_exit (void)
2144 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2148 /* Match the CYCLE statement. */
2150 match
2151 gfc_match_cycle (void)
2153 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2157 /* Match a number or character constant after an (ALL) STOP or PAUSE statement. */
2159 static match
2160 gfc_match_stopcode (gfc_statement st)
2162 gfc_expr *e;
2163 match m;
2165 e = NULL;
2167 if (gfc_match_eos () != MATCH_YES)
2169 m = gfc_match_init_expr (&e);
2170 if (m == MATCH_ERROR)
2171 goto cleanup;
2172 if (m == MATCH_NO)
2173 goto syntax;
2175 if (gfc_match_eos () != MATCH_YES)
2176 goto syntax;
2179 if (gfc_pure (NULL))
2181 gfc_error ("%s statement not allowed in PURE procedure at %C",
2182 gfc_ascii_statement (st));
2183 goto cleanup;
2186 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL) == SUCCESS)
2188 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2189 goto cleanup;
2192 if (e != NULL)
2194 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
2196 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2197 &e->where);
2198 goto cleanup;
2201 if (e->rank != 0)
2203 gfc_error ("STOP code at %L must be scalar",
2204 &e->where);
2205 goto cleanup;
2208 if (e->ts.type == BT_CHARACTER
2209 && e->ts.kind != gfc_default_character_kind)
2211 gfc_error ("STOP code at %L must be default character KIND=%d",
2212 &e->where, (int) gfc_default_character_kind);
2213 goto cleanup;
2216 if (e->ts.type == BT_INTEGER
2217 && e->ts.kind != gfc_default_integer_kind)
2219 gfc_error ("STOP code at %L must be default integer KIND=%d",
2220 &e->where, (int) gfc_default_integer_kind);
2221 goto cleanup;
2225 switch (st)
2227 case ST_STOP:
2228 new_st.op = EXEC_STOP;
2229 break;
2230 case ST_ERROR_STOP:
2231 new_st.op = EXEC_ERROR_STOP;
2232 break;
2233 case ST_PAUSE:
2234 new_st.op = EXEC_PAUSE;
2235 break;
2236 default:
2237 gcc_unreachable ();
2240 new_st.expr1 = e;
2241 new_st.ext.stop_code = -1;
2243 return MATCH_YES;
2245 syntax:
2246 gfc_syntax_error (st);
2248 cleanup:
2250 gfc_free_expr (e);
2251 return MATCH_ERROR;
2255 /* Match the (deprecated) PAUSE statement. */
2257 match
2258 gfc_match_pause (void)
2260 match m;
2262 m = gfc_match_stopcode (ST_PAUSE);
2263 if (m == MATCH_YES)
2265 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
2266 " at %C")
2267 == FAILURE)
2268 m = MATCH_ERROR;
2270 return m;
2274 /* Match the STOP statement. */
2276 match
2277 gfc_match_stop (void)
2279 return gfc_match_stopcode (ST_STOP);
2283 /* Match the ERROR STOP statement. */
2285 match
2286 gfc_match_error_stop (void)
2288 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: ERROR STOP statement at %C")
2289 == FAILURE)
2290 return MATCH_ERROR;
2292 return gfc_match_stopcode (ST_ERROR_STOP);
2296 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
2297 SYNC ALL [(sync-stat-list)]
2298 SYNC MEMORY [(sync-stat-list)]
2299 SYNC IMAGES (image-set [, sync-stat-list] )
2300 with sync-stat is int-expr or *. */
2302 static match
2303 sync_statement (gfc_statement st)
2305 match m;
2306 gfc_expr *tmp, *imageset, *stat, *errmsg;
2307 bool saw_stat, saw_errmsg;
2309 tmp = imageset = stat = errmsg = NULL;
2310 saw_stat = saw_errmsg = false;
2312 if (gfc_pure (NULL))
2314 gfc_error ("Image control statement SYNC at %C in PURE procedure");
2315 return MATCH_ERROR;
2318 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: SYNC statement at %C")
2319 == FAILURE)
2320 return MATCH_ERROR;
2322 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
2324 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
2325 return MATCH_ERROR;
2328 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
2330 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
2331 return MATCH_ERROR;
2334 if (gfc_match_eos () == MATCH_YES)
2336 if (st == ST_SYNC_IMAGES)
2337 goto syntax;
2338 goto done;
2341 if (gfc_match_char ('(') != MATCH_YES)
2342 goto syntax;
2344 if (st == ST_SYNC_IMAGES)
2346 /* Denote '*' as imageset == NULL. */
2347 m = gfc_match_char ('*');
2348 if (m == MATCH_ERROR)
2349 goto syntax;
2350 if (m == MATCH_NO)
2352 if (gfc_match ("%e", &imageset) != MATCH_YES)
2353 goto syntax;
2355 m = gfc_match_char (',');
2356 if (m == MATCH_ERROR)
2357 goto syntax;
2358 if (m == MATCH_NO)
2360 m = gfc_match_char (')');
2361 if (m == MATCH_YES)
2362 goto done;
2363 goto syntax;
2367 for (;;)
2369 m = gfc_match (" stat = %v", &tmp);
2370 if (m == MATCH_ERROR)
2371 goto syntax;
2372 if (m == MATCH_YES)
2374 if (saw_stat)
2376 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
2377 goto cleanup;
2379 stat = tmp;
2380 saw_stat = true;
2382 if (gfc_match_char (',') == MATCH_YES)
2383 continue;
2386 m = gfc_match (" errmsg = %v", &tmp);
2387 if (m == MATCH_ERROR)
2388 goto syntax;
2389 if (m == MATCH_YES)
2391 if (saw_errmsg)
2393 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
2394 goto cleanup;
2396 errmsg = tmp;
2397 saw_errmsg = true;
2399 if (gfc_match_char (',') == MATCH_YES)
2400 continue;
2403 gfc_gobble_whitespace ();
2405 if (gfc_peek_char () == ')')
2406 break;
2408 goto syntax;
2411 if (gfc_match (" )%t") != MATCH_YES)
2412 goto syntax;
2414 done:
2415 switch (st)
2417 case ST_SYNC_ALL:
2418 new_st.op = EXEC_SYNC_ALL;
2419 break;
2420 case ST_SYNC_IMAGES:
2421 new_st.op = EXEC_SYNC_IMAGES;
2422 break;
2423 case ST_SYNC_MEMORY:
2424 new_st.op = EXEC_SYNC_MEMORY;
2425 break;
2426 default:
2427 gcc_unreachable ();
2430 new_st.expr1 = imageset;
2431 new_st.expr2 = stat;
2432 new_st.expr3 = errmsg;
2434 return MATCH_YES;
2436 syntax:
2437 gfc_syntax_error (st);
2439 cleanup:
2440 gfc_free_expr (tmp);
2441 gfc_free_expr (imageset);
2442 gfc_free_expr (stat);
2443 gfc_free_expr (errmsg);
2445 return MATCH_ERROR;
2449 /* Match SYNC ALL statement. */
2451 match
2452 gfc_match_sync_all (void)
2454 return sync_statement (ST_SYNC_ALL);
2458 /* Match SYNC IMAGES statement. */
2460 match
2461 gfc_match_sync_images (void)
2463 return sync_statement (ST_SYNC_IMAGES);
2467 /* Match SYNC MEMORY statement. */
2469 match
2470 gfc_match_sync_memory (void)
2472 return sync_statement (ST_SYNC_MEMORY);
2476 /* Match a CONTINUE statement. */
2478 match
2479 gfc_match_continue (void)
2481 if (gfc_match_eos () != MATCH_YES)
2483 gfc_syntax_error (ST_CONTINUE);
2484 return MATCH_ERROR;
2487 new_st.op = EXEC_CONTINUE;
2488 return MATCH_YES;
2492 /* Match the (deprecated) ASSIGN statement. */
2494 match
2495 gfc_match_assign (void)
2497 gfc_expr *expr;
2498 gfc_st_label *label;
2500 if (gfc_match (" %l", &label) == MATCH_YES)
2502 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
2503 return MATCH_ERROR;
2504 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
2506 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
2507 "statement at %C")
2508 == FAILURE)
2509 return MATCH_ERROR;
2511 expr->symtree->n.sym->attr.assign = 1;
2513 new_st.op = EXEC_LABEL_ASSIGN;
2514 new_st.label1 = label;
2515 new_st.expr1 = expr;
2516 return MATCH_YES;
2519 return MATCH_NO;
2523 /* Match the GO TO statement. As a computed GOTO statement is
2524 matched, it is transformed into an equivalent SELECT block. No
2525 tree is necessary, and the resulting jumps-to-jumps are
2526 specifically optimized away by the back end. */
2528 match
2529 gfc_match_goto (void)
2531 gfc_code *head, *tail;
2532 gfc_expr *expr;
2533 gfc_case *cp;
2534 gfc_st_label *label;
2535 int i;
2536 match m;
2538 if (gfc_match (" %l%t", &label) == MATCH_YES)
2540 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2541 return MATCH_ERROR;
2543 new_st.op = EXEC_GOTO;
2544 new_st.label1 = label;
2545 return MATCH_YES;
2548 /* The assigned GO TO statement. */
2550 if (gfc_match_variable (&expr, 0) == MATCH_YES)
2552 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
2553 "statement at %C")
2554 == FAILURE)
2555 return MATCH_ERROR;
2557 new_st.op = EXEC_GOTO;
2558 new_st.expr1 = expr;
2560 if (gfc_match_eos () == MATCH_YES)
2561 return MATCH_YES;
2563 /* Match label list. */
2564 gfc_match_char (',');
2565 if (gfc_match_char ('(') != MATCH_YES)
2567 gfc_syntax_error (ST_GOTO);
2568 return MATCH_ERROR;
2570 head = tail = NULL;
2574 m = gfc_match_st_label (&label);
2575 if (m != MATCH_YES)
2576 goto syntax;
2578 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2579 goto cleanup;
2581 if (head == NULL)
2582 head = tail = gfc_get_code ();
2583 else
2585 tail->block = gfc_get_code ();
2586 tail = tail->block;
2589 tail->label1 = label;
2590 tail->op = EXEC_GOTO;
2592 while (gfc_match_char (',') == MATCH_YES);
2594 if (gfc_match (")%t") != MATCH_YES)
2595 goto syntax;
2597 if (head == NULL)
2599 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2600 goto syntax;
2602 new_st.block = head;
2604 return MATCH_YES;
2607 /* Last chance is a computed GO TO statement. */
2608 if (gfc_match_char ('(') != MATCH_YES)
2610 gfc_syntax_error (ST_GOTO);
2611 return MATCH_ERROR;
2614 head = tail = NULL;
2615 i = 1;
2619 m = gfc_match_st_label (&label);
2620 if (m != MATCH_YES)
2621 goto syntax;
2623 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2624 goto cleanup;
2626 if (head == NULL)
2627 head = tail = gfc_get_code ();
2628 else
2630 tail->block = gfc_get_code ();
2631 tail = tail->block;
2634 cp = gfc_get_case ();
2635 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
2636 NULL, i++);
2638 tail->op = EXEC_SELECT;
2639 tail->ext.case_list = cp;
2641 tail->next = gfc_get_code ();
2642 tail->next->op = EXEC_GOTO;
2643 tail->next->label1 = label;
2645 while (gfc_match_char (',') == MATCH_YES);
2647 if (gfc_match_char (')') != MATCH_YES)
2648 goto syntax;
2650 if (head == NULL)
2652 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2653 goto syntax;
2656 /* Get the rest of the statement. */
2657 gfc_match_char (',');
2659 if (gfc_match (" %e%t", &expr) != MATCH_YES)
2660 goto syntax;
2662 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Computed GOTO "
2663 "at %C") == FAILURE)
2664 return MATCH_ERROR;
2666 /* At this point, a computed GOTO has been fully matched and an
2667 equivalent SELECT statement constructed. */
2669 new_st.op = EXEC_SELECT;
2670 new_st.expr1 = NULL;
2672 /* Hack: For a "real" SELECT, the expression is in expr. We put
2673 it in expr2 so we can distinguish then and produce the correct
2674 diagnostics. */
2675 new_st.expr2 = expr;
2676 new_st.block = head;
2677 return MATCH_YES;
2679 syntax:
2680 gfc_syntax_error (ST_GOTO);
2681 cleanup:
2682 gfc_free_statements (head);
2683 return MATCH_ERROR;
2687 /* Frees a list of gfc_alloc structures. */
2689 void
2690 gfc_free_alloc_list (gfc_alloc *p)
2692 gfc_alloc *q;
2694 for (; p; p = q)
2696 q = p->next;
2697 gfc_free_expr (p->expr);
2698 gfc_free (p);
2703 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
2704 an accessible derived type. */
2706 static match
2707 match_derived_type_spec (gfc_typespec *ts)
2709 locus old_locus;
2710 gfc_symbol *derived;
2712 old_locus = gfc_current_locus;
2714 if (gfc_match_symbol (&derived, 1) == MATCH_YES)
2716 if (derived->attr.flavor == FL_DERIVED)
2718 ts->type = BT_DERIVED;
2719 ts->u.derived = derived;
2720 return MATCH_YES;
2722 else
2724 /* Enforce F03:C476. */
2725 gfc_error ("'%s' at %L is not an accessible derived type",
2726 derived->name, &gfc_current_locus);
2727 return MATCH_ERROR;
2731 gfc_current_locus = old_locus;
2732 return MATCH_NO;
2736 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2737 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
2738 It only includes the intrinsic types from the Fortran 2003 standard
2739 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2740 the implicit_flag is not needed, so it was removed. Derived types are
2741 identified by their name alone. */
2743 static match
2744 match_type_spec (gfc_typespec *ts)
2746 match m;
2747 locus old_locus;
2749 gfc_clear_ts (ts);
2750 gfc_gobble_whitespace();
2751 old_locus = gfc_current_locus;
2753 m = match_derived_type_spec (ts);
2754 if (m == MATCH_YES)
2756 old_locus = gfc_current_locus;
2757 if (gfc_match (" :: ") != MATCH_YES)
2758 return MATCH_ERROR;
2759 gfc_current_locus = old_locus;
2760 /* Enfore F03:C401. */
2761 if (ts->u.derived->attr.abstract)
2763 gfc_error ("Derived type '%s' at %L may not be ABSTRACT",
2764 ts->u.derived->name, &old_locus);
2765 return MATCH_ERROR;
2767 return MATCH_YES;
2769 else if (m == MATCH_ERROR && gfc_match (" :: ") == MATCH_YES)
2770 return MATCH_ERROR;
2772 gfc_current_locus = old_locus;
2774 if (gfc_match ("integer") == MATCH_YES)
2776 ts->type = BT_INTEGER;
2777 ts->kind = gfc_default_integer_kind;
2778 goto kind_selector;
2781 if (gfc_match ("real") == MATCH_YES)
2783 ts->type = BT_REAL;
2784 ts->kind = gfc_default_real_kind;
2785 goto kind_selector;
2788 if (gfc_match ("double precision") == MATCH_YES)
2790 ts->type = BT_REAL;
2791 ts->kind = gfc_default_double_kind;
2792 return MATCH_YES;
2795 if (gfc_match ("complex") == MATCH_YES)
2797 ts->type = BT_COMPLEX;
2798 ts->kind = gfc_default_complex_kind;
2799 goto kind_selector;
2802 if (gfc_match ("character") == MATCH_YES)
2804 ts->type = BT_CHARACTER;
2805 goto char_selector;
2808 if (gfc_match ("logical") == MATCH_YES)
2810 ts->type = BT_LOGICAL;
2811 ts->kind = gfc_default_logical_kind;
2812 goto kind_selector;
2815 /* If a type is not matched, simply return MATCH_NO. */
2816 gfc_current_locus = old_locus;
2817 return MATCH_NO;
2819 kind_selector:
2821 gfc_gobble_whitespace ();
2822 if (gfc_peek_ascii_char () == '*')
2824 gfc_error ("Invalid type-spec at %C");
2825 return MATCH_ERROR;
2828 m = gfc_match_kind_spec (ts, false);
2830 if (m == MATCH_NO)
2831 m = MATCH_YES; /* No kind specifier found. */
2833 return m;
2835 char_selector:
2837 m = gfc_match_char_spec (ts);
2839 if (m == MATCH_NO)
2840 m = MATCH_YES; /* No kind specifier found. */
2842 return m;
2846 /* Match an ALLOCATE statement. */
2848 match
2849 gfc_match_allocate (void)
2851 gfc_alloc *head, *tail;
2852 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
2853 gfc_typespec ts;
2854 gfc_symbol *sym;
2855 match m;
2856 locus old_locus;
2857 bool saw_stat, saw_errmsg, saw_source, saw_mold, b1, b2, b3;
2859 head = tail = NULL;
2860 stat = errmsg = source = mold = tmp = NULL;
2861 saw_stat = saw_errmsg = saw_source = saw_mold = false;
2863 if (gfc_match_char ('(') != MATCH_YES)
2864 goto syntax;
2866 /* Match an optional type-spec. */
2867 old_locus = gfc_current_locus;
2868 m = match_type_spec (&ts);
2869 if (m == MATCH_ERROR)
2870 goto cleanup;
2871 else if (m == MATCH_NO)
2872 ts.type = BT_UNKNOWN;
2873 else
2875 if (gfc_match (" :: ") == MATCH_YES)
2877 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: typespec in "
2878 "ALLOCATE at %L", &old_locus) == FAILURE)
2879 goto cleanup;
2881 else
2883 ts.type = BT_UNKNOWN;
2884 gfc_current_locus = old_locus;
2888 for (;;)
2890 if (head == NULL)
2891 head = tail = gfc_get_alloc ();
2892 else
2894 tail->next = gfc_get_alloc ();
2895 tail = tail->next;
2898 m = gfc_match_variable (&tail->expr, 0);
2899 if (m == MATCH_NO)
2900 goto syntax;
2901 if (m == MATCH_ERROR)
2902 goto cleanup;
2904 if (gfc_check_do_variable (tail->expr->symtree))
2905 goto cleanup;
2907 if (gfc_pure (NULL) && gfc_impure_variable (tail->expr->symtree->n.sym))
2909 gfc_error ("Bad allocate-object at %C for a PURE procedure");
2910 goto cleanup;
2913 /* The ALLOCATE statement had an optional typespec. Check the
2914 constraints. */
2915 if (ts.type != BT_UNKNOWN)
2917 /* Enforce F03:C624. */
2918 if (!gfc_type_compatible (&tail->expr->ts, &ts))
2920 gfc_error ("Type of entity at %L is type incompatible with "
2921 "typespec", &tail->expr->where);
2922 goto cleanup;
2925 /* Enforce F03:C627. */
2926 if (ts.kind != tail->expr->ts.kind)
2928 gfc_error ("Kind type parameter for entity at %L differs from "
2929 "the kind type parameter of the typespec",
2930 &tail->expr->where);
2931 goto cleanup;
2935 if (tail->expr->ts.type == BT_DERIVED)
2936 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
2938 /* FIXME: disable the checking on derived types and arrays. */
2939 sym = tail->expr->symtree->n.sym;
2940 b1 = !(tail->expr->ref
2941 && (tail->expr->ref->type == REF_COMPONENT
2942 || tail->expr->ref->type == REF_ARRAY));
2943 if (sym && sym->ts.type == BT_CLASS)
2944 b2 = !(CLASS_DATA (sym)->attr.allocatable
2945 || CLASS_DATA (sym)->attr.class_pointer);
2946 else
2947 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
2948 || sym->attr.proc_pointer);
2949 b3 = sym && sym->ns && sym->ns->proc_name
2950 && (sym->ns->proc_name->attr.allocatable
2951 || sym->ns->proc_name->attr.pointer
2952 || sym->ns->proc_name->attr.proc_pointer);
2953 if (b1 && b2 && !b3)
2955 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
2956 "or an allocatable variable");
2957 goto cleanup;
2960 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
2962 gfc_error ("Shape specification for allocatable scalar at %C");
2963 goto cleanup;
2966 if (gfc_match_char (',') != MATCH_YES)
2967 break;
2969 alloc_opt_list:
2971 m = gfc_match (" stat = %v", &tmp);
2972 if (m == MATCH_ERROR)
2973 goto cleanup;
2974 if (m == MATCH_YES)
2976 /* Enforce C630. */
2977 if (saw_stat)
2979 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
2980 goto cleanup;
2983 stat = tmp;
2984 tmp = NULL;
2985 saw_stat = true;
2987 if (gfc_check_do_variable (stat->symtree))
2988 goto cleanup;
2990 if (gfc_match_char (',') == MATCH_YES)
2991 goto alloc_opt_list;
2994 m = gfc_match (" errmsg = %v", &tmp);
2995 if (m == MATCH_ERROR)
2996 goto cleanup;
2997 if (m == MATCH_YES)
2999 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ERRMSG tag at %L",
3000 &tmp->where) == FAILURE)
3001 goto cleanup;
3003 /* Enforce C630. */
3004 if (saw_errmsg)
3006 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3007 goto cleanup;
3010 errmsg = tmp;
3011 tmp = NULL;
3012 saw_errmsg = true;
3014 if (gfc_match_char (',') == MATCH_YES)
3015 goto alloc_opt_list;
3018 m = gfc_match (" source = %e", &tmp);
3019 if (m == MATCH_ERROR)
3020 goto cleanup;
3021 if (m == MATCH_YES)
3023 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SOURCE tag at %L",
3024 &tmp->where) == FAILURE)
3025 goto cleanup;
3027 /* Enforce C630. */
3028 if (saw_source)
3030 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
3031 goto cleanup;
3034 /* The next 2 conditionals check C631. */
3035 if (ts.type != BT_UNKNOWN)
3037 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
3038 &tmp->where, &old_locus);
3039 goto cleanup;
3042 if (head->next)
3044 gfc_error ("SOURCE tag at %L requires only a single entity in "
3045 "the allocation-list", &tmp->where);
3046 goto cleanup;
3049 source = tmp;
3050 tmp = NULL;
3051 saw_source = true;
3053 if (gfc_match_char (',') == MATCH_YES)
3054 goto alloc_opt_list;
3057 m = gfc_match (" mold = %e", &tmp);
3058 if (m == MATCH_ERROR)
3059 goto cleanup;
3060 if (m == MATCH_YES)
3062 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: MOLD tag at %L",
3063 &tmp->where) == FAILURE)
3064 goto cleanup;
3066 /* Check F08:C636. */
3067 if (saw_mold)
3069 gfc_error ("Redundant MOLD tag found at %L ", &tmp->where);
3070 goto cleanup;
3073 /* Check F08:C637. */
3074 if (ts.type != BT_UNKNOWN)
3076 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
3077 &tmp->where, &old_locus);
3078 goto cleanup;
3081 mold = tmp;
3082 tmp = NULL;
3083 saw_mold = true;
3084 mold->mold = 1;
3086 if (gfc_match_char (',') == MATCH_YES)
3087 goto alloc_opt_list;
3090 gfc_gobble_whitespace ();
3092 if (gfc_peek_char () == ')')
3093 break;
3097 if (gfc_match (" )%t") != MATCH_YES)
3098 goto syntax;
3100 /* Check F08:C637. */
3101 if (source && mold)
3103 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
3104 &mold->where, &source->where);
3105 goto cleanup;
3108 new_st.op = EXEC_ALLOCATE;
3109 new_st.expr1 = stat;
3110 new_st.expr2 = errmsg;
3111 if (source)
3112 new_st.expr3 = source;
3113 else
3114 new_st.expr3 = mold;
3115 new_st.ext.alloc.list = head;
3116 new_st.ext.alloc.ts = ts;
3118 return MATCH_YES;
3120 syntax:
3121 gfc_syntax_error (ST_ALLOCATE);
3123 cleanup:
3124 gfc_free_expr (errmsg);
3125 gfc_free_expr (source);
3126 gfc_free_expr (stat);
3127 gfc_free_expr (mold);
3128 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
3129 gfc_free_alloc_list (head);
3130 return MATCH_ERROR;
3134 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
3135 a set of pointer assignments to intrinsic NULL(). */
3137 match
3138 gfc_match_nullify (void)
3140 gfc_code *tail;
3141 gfc_expr *e, *p;
3142 match m;
3144 tail = NULL;
3146 if (gfc_match_char ('(') != MATCH_YES)
3147 goto syntax;
3149 for (;;)
3151 m = gfc_match_variable (&p, 0);
3152 if (m == MATCH_ERROR)
3153 goto cleanup;
3154 if (m == MATCH_NO)
3155 goto syntax;
3157 if (gfc_check_do_variable (p->symtree))
3158 goto cleanup;
3160 /* build ' => NULL() '. */
3161 e = gfc_get_null_expr (&gfc_current_locus);
3163 /* Chain to list. */
3164 if (tail == NULL)
3165 tail = &new_st;
3166 else
3168 tail->next = gfc_get_code ();
3169 tail = tail->next;
3172 tail->op = EXEC_POINTER_ASSIGN;
3173 tail->expr1 = p;
3174 tail->expr2 = e;
3176 if (gfc_match (" )%t") == MATCH_YES)
3177 break;
3178 if (gfc_match_char (',') != MATCH_YES)
3179 goto syntax;
3182 return MATCH_YES;
3184 syntax:
3185 gfc_syntax_error (ST_NULLIFY);
3187 cleanup:
3188 gfc_free_statements (new_st.next);
3189 new_st.next = NULL;
3190 gfc_free_expr (new_st.expr1);
3191 new_st.expr1 = NULL;
3192 gfc_free_expr (new_st.expr2);
3193 new_st.expr2 = NULL;
3194 return MATCH_ERROR;
3198 /* Match a DEALLOCATE statement. */
3200 match
3201 gfc_match_deallocate (void)
3203 gfc_alloc *head, *tail;
3204 gfc_expr *stat, *errmsg, *tmp;
3205 gfc_symbol *sym;
3206 match m;
3207 bool saw_stat, saw_errmsg, b1, b2;
3209 head = tail = NULL;
3210 stat = errmsg = tmp = NULL;
3211 saw_stat = saw_errmsg = false;
3213 if (gfc_match_char ('(') != MATCH_YES)
3214 goto syntax;
3216 for (;;)
3218 if (head == NULL)
3219 head = tail = gfc_get_alloc ();
3220 else
3222 tail->next = gfc_get_alloc ();
3223 tail = tail->next;
3226 m = gfc_match_variable (&tail->expr, 0);
3227 if (m == MATCH_ERROR)
3228 goto cleanup;
3229 if (m == MATCH_NO)
3230 goto syntax;
3232 if (gfc_check_do_variable (tail->expr->symtree))
3233 goto cleanup;
3235 sym = tail->expr->symtree->n.sym;
3237 if (gfc_pure (NULL) && gfc_impure_variable (sym))
3239 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
3240 goto cleanup;
3243 /* FIXME: disable the checking on derived types. */
3244 b1 = !(tail->expr->ref
3245 && (tail->expr->ref->type == REF_COMPONENT
3246 || tail->expr->ref->type == REF_ARRAY));
3247 if (sym && sym->ts.type == BT_CLASS)
3248 b2 = !(CLASS_DATA (sym)->attr.allocatable
3249 || CLASS_DATA (sym)->attr.class_pointer);
3250 else
3251 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
3252 || sym->attr.proc_pointer);
3253 if (b1 && b2)
3255 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
3256 "or an allocatable variable");
3257 goto cleanup;
3260 if (gfc_match_char (',') != MATCH_YES)
3261 break;
3263 dealloc_opt_list:
3265 m = gfc_match (" stat = %v", &tmp);
3266 if (m == MATCH_ERROR)
3267 goto cleanup;
3268 if (m == MATCH_YES)
3270 if (saw_stat)
3272 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3273 gfc_free_expr (tmp);
3274 goto cleanup;
3277 stat = tmp;
3278 saw_stat = true;
3280 if (gfc_check_do_variable (stat->symtree))
3281 goto cleanup;
3283 if (gfc_match_char (',') == MATCH_YES)
3284 goto dealloc_opt_list;
3287 m = gfc_match (" errmsg = %v", &tmp);
3288 if (m == MATCH_ERROR)
3289 goto cleanup;
3290 if (m == MATCH_YES)
3292 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ERRMSG at %L",
3293 &tmp->where) == FAILURE)
3294 goto cleanup;
3296 if (saw_errmsg)
3298 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3299 gfc_free_expr (tmp);
3300 goto cleanup;
3303 errmsg = tmp;
3304 saw_errmsg = true;
3306 if (gfc_match_char (',') == MATCH_YES)
3307 goto dealloc_opt_list;
3310 gfc_gobble_whitespace ();
3312 if (gfc_peek_char () == ')')
3313 break;
3316 if (gfc_match (" )%t") != MATCH_YES)
3317 goto syntax;
3319 new_st.op = EXEC_DEALLOCATE;
3320 new_st.expr1 = stat;
3321 new_st.expr2 = errmsg;
3322 new_st.ext.alloc.list = head;
3324 return MATCH_YES;
3326 syntax:
3327 gfc_syntax_error (ST_DEALLOCATE);
3329 cleanup:
3330 gfc_free_expr (errmsg);
3331 gfc_free_expr (stat);
3332 gfc_free_alloc_list (head);
3333 return MATCH_ERROR;
3337 /* Match a RETURN statement. */
3339 match
3340 gfc_match_return (void)
3342 gfc_expr *e;
3343 match m;
3344 gfc_compile_state s;
3346 e = NULL;
3348 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
3350 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
3351 return MATCH_ERROR;
3354 if (gfc_match_eos () == MATCH_YES)
3355 goto done;
3357 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
3359 gfc_error ("Alternate RETURN statement at %C is only allowed within "
3360 "a SUBROUTINE");
3361 goto cleanup;
3364 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Alternate RETURN "
3365 "at %C") == FAILURE)
3366 return MATCH_ERROR;
3368 if (gfc_current_form == FORM_FREE)
3370 /* The following are valid, so we can't require a blank after the
3371 RETURN keyword:
3372 return+1
3373 return(1) */
3374 char c = gfc_peek_ascii_char ();
3375 if (ISALPHA (c) || ISDIGIT (c))
3376 return MATCH_NO;
3379 m = gfc_match (" %e%t", &e);
3380 if (m == MATCH_YES)
3381 goto done;
3382 if (m == MATCH_ERROR)
3383 goto cleanup;
3385 gfc_syntax_error (ST_RETURN);
3387 cleanup:
3388 gfc_free_expr (e);
3389 return MATCH_ERROR;
3391 done:
3392 gfc_enclosing_unit (&s);
3393 if (s == COMP_PROGRAM
3394 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
3395 "main program at %C") == FAILURE)
3396 return MATCH_ERROR;
3398 new_st.op = EXEC_RETURN;
3399 new_st.expr1 = e;
3401 return MATCH_YES;
3405 /* Match the call of a type-bound procedure, if CALL%var has already been
3406 matched and var found to be a derived-type variable. */
3408 static match
3409 match_typebound_call (gfc_symtree* varst)
3411 gfc_expr* base;
3412 match m;
3414 base = gfc_get_expr ();
3415 base->expr_type = EXPR_VARIABLE;
3416 base->symtree = varst;
3417 base->where = gfc_current_locus;
3418 gfc_set_sym_referenced (varst->n.sym);
3420 m = gfc_match_varspec (base, 0, true, true);
3421 if (m == MATCH_NO)
3422 gfc_error ("Expected component reference at %C");
3423 if (m != MATCH_YES)
3424 return MATCH_ERROR;
3426 if (gfc_match_eos () != MATCH_YES)
3428 gfc_error ("Junk after CALL at %C");
3429 return MATCH_ERROR;
3432 if (base->expr_type == EXPR_COMPCALL)
3433 new_st.op = EXEC_COMPCALL;
3434 else if (base->expr_type == EXPR_PPC)
3435 new_st.op = EXEC_CALL_PPC;
3436 else
3438 gfc_error ("Expected type-bound procedure or procedure pointer component "
3439 "at %C");
3440 return MATCH_ERROR;
3442 new_st.expr1 = base;
3444 return MATCH_YES;
3448 /* Match a CALL statement. The tricky part here are possible
3449 alternate return specifiers. We handle these by having all
3450 "subroutines" actually return an integer via a register that gives
3451 the return number. If the call specifies alternate returns, we
3452 generate code for a SELECT statement whose case clauses contain
3453 GOTOs to the various labels. */
3455 match
3456 gfc_match_call (void)
3458 char name[GFC_MAX_SYMBOL_LEN + 1];
3459 gfc_actual_arglist *a, *arglist;
3460 gfc_case *new_case;
3461 gfc_symbol *sym;
3462 gfc_symtree *st;
3463 gfc_code *c;
3464 match m;
3465 int i;
3467 arglist = NULL;
3469 m = gfc_match ("% %n", name);
3470 if (m == MATCH_NO)
3471 goto syntax;
3472 if (m != MATCH_YES)
3473 return m;
3475 if (gfc_get_ha_sym_tree (name, &st))
3476 return MATCH_ERROR;
3478 sym = st->n.sym;
3480 /* If this is a variable of derived-type, it probably starts a type-bound
3481 procedure call. */
3482 if ((sym->attr.flavor != FL_PROCEDURE
3483 || gfc_is_function_return_value (sym, gfc_current_ns))
3484 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
3485 return match_typebound_call (st);
3487 /* If it does not seem to be callable (include functions so that the
3488 right association is made. They are thrown out in resolution.)
3489 ... */
3490 if (!sym->attr.generic
3491 && !sym->attr.subroutine
3492 && !sym->attr.function)
3494 if (!(sym->attr.external && !sym->attr.referenced))
3496 /* ...create a symbol in this scope... */
3497 if (sym->ns != gfc_current_ns
3498 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
3499 return MATCH_ERROR;
3501 if (sym != st->n.sym)
3502 sym = st->n.sym;
3505 /* ...and then to try to make the symbol into a subroutine. */
3506 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
3507 return MATCH_ERROR;
3510 gfc_set_sym_referenced (sym);
3512 if (gfc_match_eos () != MATCH_YES)
3514 m = gfc_match_actual_arglist (1, &arglist);
3515 if (m == MATCH_NO)
3516 goto syntax;
3517 if (m == MATCH_ERROR)
3518 goto cleanup;
3520 if (gfc_match_eos () != MATCH_YES)
3521 goto syntax;
3524 /* If any alternate return labels were found, construct a SELECT
3525 statement that will jump to the right place. */
3527 i = 0;
3528 for (a = arglist; a; a = a->next)
3529 if (a->expr == NULL)
3530 i = 1;
3532 if (i)
3534 gfc_symtree *select_st;
3535 gfc_symbol *select_sym;
3536 char name[GFC_MAX_SYMBOL_LEN + 1];
3538 new_st.next = c = gfc_get_code ();
3539 c->op = EXEC_SELECT;
3540 sprintf (name, "_result_%s", sym->name);
3541 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
3543 select_sym = select_st->n.sym;
3544 select_sym->ts.type = BT_INTEGER;
3545 select_sym->ts.kind = gfc_default_integer_kind;
3546 gfc_set_sym_referenced (select_sym);
3547 c->expr1 = gfc_get_expr ();
3548 c->expr1->expr_type = EXPR_VARIABLE;
3549 c->expr1->symtree = select_st;
3550 c->expr1->ts = select_sym->ts;
3551 c->expr1->where = gfc_current_locus;
3553 i = 0;
3554 for (a = arglist; a; a = a->next)
3556 if (a->expr != NULL)
3557 continue;
3559 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
3560 continue;
3562 i++;
3564 c->block = gfc_get_code ();
3565 c = c->block;
3566 c->op = EXEC_SELECT;
3568 new_case = gfc_get_case ();
3569 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
3570 new_case->low = new_case->high;
3571 c->ext.case_list = new_case;
3573 c->next = gfc_get_code ();
3574 c->next->op = EXEC_GOTO;
3575 c->next->label1 = a->label;
3579 new_st.op = EXEC_CALL;
3580 new_st.symtree = st;
3581 new_st.ext.actual = arglist;
3583 return MATCH_YES;
3585 syntax:
3586 gfc_syntax_error (ST_CALL);
3588 cleanup:
3589 gfc_free_actual_arglist (arglist);
3590 return MATCH_ERROR;
3594 /* Given a name, return a pointer to the common head structure,
3595 creating it if it does not exist. If FROM_MODULE is nonzero, we
3596 mangle the name so that it doesn't interfere with commons defined
3597 in the using namespace.
3598 TODO: Add to global symbol tree. */
3600 gfc_common_head *
3601 gfc_get_common (const char *name, int from_module)
3603 gfc_symtree *st;
3604 static int serial = 0;
3605 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
3607 if (from_module)
3609 /* A use associated common block is only needed to correctly layout
3610 the variables it contains. */
3611 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
3612 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
3614 else
3616 st = gfc_find_symtree (gfc_current_ns->common_root, name);
3618 if (st == NULL)
3619 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
3622 if (st->n.common == NULL)
3624 st->n.common = gfc_get_common_head ();
3625 st->n.common->where = gfc_current_locus;
3626 strcpy (st->n.common->name, name);
3629 return st->n.common;
3633 /* Match a common block name. */
3635 match match_common_name (char *name)
3637 match m;
3639 if (gfc_match_char ('/') == MATCH_NO)
3641 name[0] = '\0';
3642 return MATCH_YES;
3645 if (gfc_match_char ('/') == MATCH_YES)
3647 name[0] = '\0';
3648 return MATCH_YES;
3651 m = gfc_match_name (name);
3653 if (m == MATCH_ERROR)
3654 return MATCH_ERROR;
3655 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
3656 return MATCH_YES;
3658 gfc_error ("Syntax error in common block name at %C");
3659 return MATCH_ERROR;
3663 /* Match a COMMON statement. */
3665 match
3666 gfc_match_common (void)
3668 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
3669 char name[GFC_MAX_SYMBOL_LEN + 1];
3670 gfc_common_head *t;
3671 gfc_array_spec *as;
3672 gfc_equiv *e1, *e2;
3673 match m;
3674 gfc_gsymbol *gsym;
3676 old_blank_common = gfc_current_ns->blank_common.head;
3677 if (old_blank_common)
3679 while (old_blank_common->common_next)
3680 old_blank_common = old_blank_common->common_next;
3683 as = NULL;
3685 for (;;)
3687 m = match_common_name (name);
3688 if (m == MATCH_ERROR)
3689 goto cleanup;
3691 gsym = gfc_get_gsymbol (name);
3692 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
3694 gfc_error ("Symbol '%s' at %C is already an external symbol that "
3695 "is not COMMON", name);
3696 goto cleanup;
3699 if (gsym->type == GSYM_UNKNOWN)
3701 gsym->type = GSYM_COMMON;
3702 gsym->where = gfc_current_locus;
3703 gsym->defined = 1;
3706 gsym->used = 1;
3708 if (name[0] == '\0')
3710 t = &gfc_current_ns->blank_common;
3711 if (t->head == NULL)
3712 t->where = gfc_current_locus;
3714 else
3716 t = gfc_get_common (name, 0);
3718 head = &t->head;
3720 if (*head == NULL)
3721 tail = NULL;
3722 else
3724 tail = *head;
3725 while (tail->common_next)
3726 tail = tail->common_next;
3729 /* Grab the list of symbols. */
3730 for (;;)
3732 m = gfc_match_symbol (&sym, 0);
3733 if (m == MATCH_ERROR)
3734 goto cleanup;
3735 if (m == MATCH_NO)
3736 goto syntax;
3738 /* Store a ref to the common block for error checking. */
3739 sym->common_block = t;
3741 /* See if we know the current common block is bind(c), and if
3742 so, then see if we can check if the symbol is (which it'll
3743 need to be). This can happen if the bind(c) attr stmt was
3744 applied to the common block, and the variable(s) already
3745 defined, before declaring the common block. */
3746 if (t->is_bind_c == 1)
3748 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
3750 /* If we find an error, just print it and continue,
3751 cause it's just semantic, and we can see if there
3752 are more errors. */
3753 gfc_error_now ("Variable '%s' at %L in common block '%s' "
3754 "at %C must be declared with a C "
3755 "interoperable kind since common block "
3756 "'%s' is bind(c)",
3757 sym->name, &(sym->declared_at), t->name,
3758 t->name);
3761 if (sym->attr.is_bind_c == 1)
3762 gfc_error_now ("Variable '%s' in common block "
3763 "'%s' at %C can not be bind(c) since "
3764 "it is not global", sym->name, t->name);
3767 if (sym->attr.in_common)
3769 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
3770 sym->name);
3771 goto cleanup;
3774 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
3775 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
3777 if (gfc_notify_std (GFC_STD_GNU, "Initialized symbol '%s' at %C "
3778 "can only be COMMON in "
3779 "BLOCK DATA", sym->name)
3780 == FAILURE)
3781 goto cleanup;
3784 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
3785 goto cleanup;
3787 if (tail != NULL)
3788 tail->common_next = sym;
3789 else
3790 *head = sym;
3792 tail = sym;
3794 /* Deal with an optional array specification after the
3795 symbol name. */
3796 m = gfc_match_array_spec (&as, true, true);
3797 if (m == MATCH_ERROR)
3798 goto cleanup;
3800 if (m == MATCH_YES)
3802 if (as->type != AS_EXPLICIT)
3804 gfc_error ("Array specification for symbol '%s' in COMMON "
3805 "at %C must be explicit", sym->name);
3806 goto cleanup;
3809 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
3810 goto cleanup;
3812 if (sym->attr.pointer)
3814 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
3815 "POINTER array", sym->name);
3816 goto cleanup;
3819 sym->as = as;
3820 as = NULL;
3824 sym->common_head = t;
3826 /* Check to see if the symbol is already in an equivalence group.
3827 If it is, set the other members as being in common. */
3828 if (sym->attr.in_equivalence)
3830 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
3832 for (e2 = e1; e2; e2 = e2->eq)
3833 if (e2->expr->symtree->n.sym == sym)
3834 goto equiv_found;
3836 continue;
3838 equiv_found:
3840 for (e2 = e1; e2; e2 = e2->eq)
3842 other = e2->expr->symtree->n.sym;
3843 if (other->common_head
3844 && other->common_head != sym->common_head)
3846 gfc_error ("Symbol '%s', in COMMON block '%s' at "
3847 "%C is being indirectly equivalenced to "
3848 "another COMMON block '%s'",
3849 sym->name, sym->common_head->name,
3850 other->common_head->name);
3851 goto cleanup;
3853 other->attr.in_common = 1;
3854 other->common_head = t;
3860 gfc_gobble_whitespace ();
3861 if (gfc_match_eos () == MATCH_YES)
3862 goto done;
3863 if (gfc_peek_ascii_char () == '/')
3864 break;
3865 if (gfc_match_char (',') != MATCH_YES)
3866 goto syntax;
3867 gfc_gobble_whitespace ();
3868 if (gfc_peek_ascii_char () == '/')
3869 break;
3873 done:
3874 return MATCH_YES;
3876 syntax:
3877 gfc_syntax_error (ST_COMMON);
3879 cleanup:
3880 if (old_blank_common)
3881 old_blank_common->common_next = NULL;
3882 else
3883 gfc_current_ns->blank_common.head = NULL;
3884 gfc_free_array_spec (as);
3885 return MATCH_ERROR;
3889 /* Match a BLOCK DATA program unit. */
3891 match
3892 gfc_match_block_data (void)
3894 char name[GFC_MAX_SYMBOL_LEN + 1];
3895 gfc_symbol *sym;
3896 match m;
3898 if (gfc_match_eos () == MATCH_YES)
3900 gfc_new_block = NULL;
3901 return MATCH_YES;
3904 m = gfc_match ("% %n%t", name);
3905 if (m != MATCH_YES)
3906 return MATCH_ERROR;
3908 if (gfc_get_symbol (name, NULL, &sym))
3909 return MATCH_ERROR;
3911 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
3912 return MATCH_ERROR;
3914 gfc_new_block = sym;
3916 return MATCH_YES;
3920 /* Free a namelist structure. */
3922 void
3923 gfc_free_namelist (gfc_namelist *name)
3925 gfc_namelist *n;
3927 for (; name; name = n)
3929 n = name->next;
3930 gfc_free (name);
3935 /* Match a NAMELIST statement. */
3937 match
3938 gfc_match_namelist (void)
3940 gfc_symbol *group_name, *sym;
3941 gfc_namelist *nl;
3942 match m, m2;
3944 m = gfc_match (" / %s /", &group_name);
3945 if (m == MATCH_NO)
3946 goto syntax;
3947 if (m == MATCH_ERROR)
3948 goto error;
3950 for (;;)
3952 if (group_name->ts.type != BT_UNKNOWN)
3954 gfc_error ("Namelist group name '%s' at %C already has a basic "
3955 "type of %s", group_name->name,
3956 gfc_typename (&group_name->ts));
3957 return MATCH_ERROR;
3960 if (group_name->attr.flavor == FL_NAMELIST
3961 && group_name->attr.use_assoc
3962 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
3963 "at %C already is USE associated and can"
3964 "not be respecified.", group_name->name)
3965 == FAILURE)
3966 return MATCH_ERROR;
3968 if (group_name->attr.flavor != FL_NAMELIST
3969 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
3970 group_name->name, NULL) == FAILURE)
3971 return MATCH_ERROR;
3973 for (;;)
3975 m = gfc_match_symbol (&sym, 1);
3976 if (m == MATCH_NO)
3977 goto syntax;
3978 if (m == MATCH_ERROR)
3979 goto error;
3981 if (sym->attr.in_namelist == 0
3982 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
3983 goto error;
3985 /* Use gfc_error_check here, rather than goto error, so that
3986 these are the only errors for the next two lines. */
3987 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
3989 gfc_error ("Assumed size array '%s' in namelist '%s' at "
3990 "%C is not allowed", sym->name, group_name->name);
3991 gfc_error_check ();
3994 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl->length == NULL)
3996 gfc_error ("Assumed character length '%s' in namelist '%s' at "
3997 "%C is not allowed", sym->name, group_name->name);
3998 gfc_error_check ();
4001 nl = gfc_get_namelist ();
4002 nl->sym = sym;
4003 sym->refs++;
4005 if (group_name->namelist == NULL)
4006 group_name->namelist = group_name->namelist_tail = nl;
4007 else
4009 group_name->namelist_tail->next = nl;
4010 group_name->namelist_tail = nl;
4013 if (gfc_match_eos () == MATCH_YES)
4014 goto done;
4016 m = gfc_match_char (',');
4018 if (gfc_match_char ('/') == MATCH_YES)
4020 m2 = gfc_match (" %s /", &group_name);
4021 if (m2 == MATCH_YES)
4022 break;
4023 if (m2 == MATCH_ERROR)
4024 goto error;
4025 goto syntax;
4028 if (m != MATCH_YES)
4029 goto syntax;
4033 done:
4034 return MATCH_YES;
4036 syntax:
4037 gfc_syntax_error (ST_NAMELIST);
4039 error:
4040 return MATCH_ERROR;
4044 /* Match a MODULE statement. */
4046 match
4047 gfc_match_module (void)
4049 match m;
4051 m = gfc_match (" %s%t", &gfc_new_block);
4052 if (m != MATCH_YES)
4053 return m;
4055 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
4056 gfc_new_block->name, NULL) == FAILURE)
4057 return MATCH_ERROR;
4059 return MATCH_YES;
4063 /* Free equivalence sets and lists. Recursively is the easiest way to
4064 do this. */
4066 void
4067 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
4069 if (eq == stop)
4070 return;
4072 gfc_free_equiv (eq->eq);
4073 gfc_free_equiv_until (eq->next, stop);
4074 gfc_free_expr (eq->expr);
4075 gfc_free (eq);
4079 void
4080 gfc_free_equiv (gfc_equiv *eq)
4082 gfc_free_equiv_until (eq, NULL);
4086 /* Match an EQUIVALENCE statement. */
4088 match
4089 gfc_match_equivalence (void)
4091 gfc_equiv *eq, *set, *tail;
4092 gfc_ref *ref;
4093 gfc_symbol *sym;
4094 match m;
4095 gfc_common_head *common_head = NULL;
4096 bool common_flag;
4097 int cnt;
4099 tail = NULL;
4101 for (;;)
4103 eq = gfc_get_equiv ();
4104 if (tail == NULL)
4105 tail = eq;
4107 eq->next = gfc_current_ns->equiv;
4108 gfc_current_ns->equiv = eq;
4110 if (gfc_match_char ('(') != MATCH_YES)
4111 goto syntax;
4113 set = eq;
4114 common_flag = FALSE;
4115 cnt = 0;
4117 for (;;)
4119 m = gfc_match_equiv_variable (&set->expr);
4120 if (m == MATCH_ERROR)
4121 goto cleanup;
4122 if (m == MATCH_NO)
4123 goto syntax;
4125 /* count the number of objects. */
4126 cnt++;
4128 if (gfc_match_char ('%') == MATCH_YES)
4130 gfc_error ("Derived type component %C is not a "
4131 "permitted EQUIVALENCE member");
4132 goto cleanup;
4135 for (ref = set->expr->ref; ref; ref = ref->next)
4136 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
4138 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
4139 "be an array section");
4140 goto cleanup;
4143 sym = set->expr->symtree->n.sym;
4145 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
4146 goto cleanup;
4148 if (sym->attr.in_common)
4150 common_flag = TRUE;
4151 common_head = sym->common_head;
4154 if (gfc_match_char (')') == MATCH_YES)
4155 break;
4157 if (gfc_match_char (',') != MATCH_YES)
4158 goto syntax;
4160 set->eq = gfc_get_equiv ();
4161 set = set->eq;
4164 if (cnt < 2)
4166 gfc_error ("EQUIVALENCE at %C requires two or more objects");
4167 goto cleanup;
4170 /* If one of the members of an equivalence is in common, then
4171 mark them all as being in common. Before doing this, check
4172 that members of the equivalence group are not in different
4173 common blocks. */
4174 if (common_flag)
4175 for (set = eq; set; set = set->eq)
4177 sym = set->expr->symtree->n.sym;
4178 if (sym->common_head && sym->common_head != common_head)
4180 gfc_error ("Attempt to indirectly overlap COMMON "
4181 "blocks %s and %s by EQUIVALENCE at %C",
4182 sym->common_head->name, common_head->name);
4183 goto cleanup;
4185 sym->attr.in_common = 1;
4186 sym->common_head = common_head;
4189 if (gfc_match_eos () == MATCH_YES)
4190 break;
4191 if (gfc_match_char (',') != MATCH_YES)
4193 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
4194 goto cleanup;
4198 return MATCH_YES;
4200 syntax:
4201 gfc_syntax_error (ST_EQUIVALENCE);
4203 cleanup:
4204 eq = tail->next;
4205 tail->next = NULL;
4207 gfc_free_equiv (gfc_current_ns->equiv);
4208 gfc_current_ns->equiv = eq;
4210 return MATCH_ERROR;
4214 /* Check that a statement function is not recursive. This is done by looking
4215 for the statement function symbol(sym) by looking recursively through its
4216 expression(e). If a reference to sym is found, true is returned.
4217 12.5.4 requires that any variable of function that is implicitly typed
4218 shall have that type confirmed by any subsequent type declaration. The
4219 implicit typing is conveniently done here. */
4220 static bool
4221 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
4223 static bool
4224 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
4227 if (e == NULL)
4228 return false;
4230 switch (e->expr_type)
4232 case EXPR_FUNCTION:
4233 if (e->symtree == NULL)
4234 return false;
4236 /* Check the name before testing for nested recursion! */
4237 if (sym->name == e->symtree->n.sym->name)
4238 return true;
4240 /* Catch recursion via other statement functions. */
4241 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
4242 && e->symtree->n.sym->value
4243 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
4244 return true;
4246 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
4247 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
4249 break;
4251 case EXPR_VARIABLE:
4252 if (e->symtree && sym->name == e->symtree->n.sym->name)
4253 return true;
4255 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
4256 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
4257 break;
4259 default:
4260 break;
4263 return false;
4267 static bool
4268 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
4270 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
4274 /* Match a statement function declaration. It is so easy to match
4275 non-statement function statements with a MATCH_ERROR as opposed to
4276 MATCH_NO that we suppress error message in most cases. */
4278 match
4279 gfc_match_st_function (void)
4281 gfc_error_buf old_error;
4282 gfc_symbol *sym;
4283 gfc_expr *expr;
4284 match m;
4286 m = gfc_match_symbol (&sym, 0);
4287 if (m != MATCH_YES)
4288 return m;
4290 gfc_push_error (&old_error);
4292 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
4293 sym->name, NULL) == FAILURE)
4294 goto undo_error;
4296 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
4297 goto undo_error;
4299 m = gfc_match (" = %e%t", &expr);
4300 if (m == MATCH_NO)
4301 goto undo_error;
4303 gfc_free_error (&old_error);
4304 if (m == MATCH_ERROR)
4305 return m;
4307 if (recursive_stmt_fcn (expr, sym))
4309 gfc_error ("Statement function at %L is recursive", &expr->where);
4310 return MATCH_ERROR;
4313 sym->value = expr;
4315 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
4316 "Statement function at %C") == FAILURE)
4317 return MATCH_ERROR;
4319 return MATCH_YES;
4321 undo_error:
4322 gfc_pop_error (&old_error);
4323 return MATCH_NO;
4327 /***************** SELECT CASE subroutines ******************/
4329 /* Free a single case structure. */
4331 static void
4332 free_case (gfc_case *p)
4334 if (p->low == p->high)
4335 p->high = NULL;
4336 gfc_free_expr (p->low);
4337 gfc_free_expr (p->high);
4338 gfc_free (p);
4342 /* Free a list of case structures. */
4344 void
4345 gfc_free_case_list (gfc_case *p)
4347 gfc_case *q;
4349 for (; p; p = q)
4351 q = p->next;
4352 free_case (p);
4357 /* Match a single case selector. */
4359 static match
4360 match_case_selector (gfc_case **cp)
4362 gfc_case *c;
4363 match m;
4365 c = gfc_get_case ();
4366 c->where = gfc_current_locus;
4368 if (gfc_match_char (':') == MATCH_YES)
4370 m = gfc_match_init_expr (&c->high);
4371 if (m == MATCH_NO)
4372 goto need_expr;
4373 if (m == MATCH_ERROR)
4374 goto cleanup;
4376 else
4378 m = gfc_match_init_expr (&c->low);
4379 if (m == MATCH_ERROR)
4380 goto cleanup;
4381 if (m == MATCH_NO)
4382 goto need_expr;
4384 /* If we're not looking at a ':' now, make a range out of a single
4385 target. Else get the upper bound for the case range. */
4386 if (gfc_match_char (':') != MATCH_YES)
4387 c->high = c->low;
4388 else
4390 m = gfc_match_init_expr (&c->high);
4391 if (m == MATCH_ERROR)
4392 goto cleanup;
4393 /* MATCH_NO is fine. It's OK if nothing is there! */
4397 *cp = c;
4398 return MATCH_YES;
4400 need_expr:
4401 gfc_error ("Expected initialization expression in CASE at %C");
4403 cleanup:
4404 free_case (c);
4405 return MATCH_ERROR;
4409 /* Match the end of a case statement. */
4411 static match
4412 match_case_eos (void)
4414 char name[GFC_MAX_SYMBOL_LEN + 1];
4415 match m;
4417 if (gfc_match_eos () == MATCH_YES)
4418 return MATCH_YES;
4420 /* If the case construct doesn't have a case-construct-name, we
4421 should have matched the EOS. */
4422 if (!gfc_current_block ())
4423 return MATCH_NO;
4425 gfc_gobble_whitespace ();
4427 m = gfc_match_name (name);
4428 if (m != MATCH_YES)
4429 return m;
4431 if (strcmp (name, gfc_current_block ()->name) != 0)
4433 gfc_error ("Expected block name '%s' of SELECT construct at %C",
4434 gfc_current_block ()->name);
4435 return MATCH_ERROR;
4438 return gfc_match_eos ();
4442 /* Match a SELECT statement. */
4444 match
4445 gfc_match_select (void)
4447 gfc_expr *expr;
4448 match m;
4450 m = gfc_match_label ();
4451 if (m == MATCH_ERROR)
4452 return m;
4454 m = gfc_match (" select case ( %e )%t", &expr);
4455 if (m != MATCH_YES)
4456 return m;
4458 new_st.op = EXEC_SELECT;
4459 new_st.expr1 = expr;
4461 return MATCH_YES;
4465 /* Push the current selector onto the SELECT TYPE stack. */
4467 static void
4468 select_type_push (gfc_symbol *sel)
4470 gfc_select_type_stack *top = gfc_get_select_type_stack ();
4471 top->selector = sel;
4472 top->tmp = NULL;
4473 top->prev = select_type_stack;
4475 select_type_stack = top;
4479 /* Set the temporary for the current SELECT TYPE selector. */
4481 static void
4482 select_type_set_tmp (gfc_typespec *ts)
4484 char name[GFC_MAX_SYMBOL_LEN];
4485 gfc_symtree *tmp;
4487 if (!ts)
4489 select_type_stack->tmp = NULL;
4490 return;
4493 if (!gfc_type_is_extensible (ts->u.derived))
4494 return;
4496 if (ts->type == BT_CLASS)
4497 sprintf (name, "tmp$class$%s", ts->u.derived->name);
4498 else
4499 sprintf (name, "tmp$type$%s", ts->u.derived->name);
4500 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
4501 gfc_add_type (tmp->n.sym, ts, NULL);
4502 gfc_set_sym_referenced (tmp->n.sym);
4503 gfc_add_pointer (&tmp->n.sym->attr, NULL);
4504 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
4505 if (ts->type == BT_CLASS)
4507 gfc_build_class_symbol (&tmp->n.sym->ts, &tmp->n.sym->attr,
4508 &tmp->n.sym->as, false);
4509 tmp->n.sym->attr.class_ok = 1;
4511 tmp->n.sym->attr.select_type_temporary = 1;
4513 /* Add an association for it, so the rest of the parser knows it is
4514 an associate-name. The target will be set during resolution. */
4515 tmp->n.sym->assoc = gfc_get_association_list ();
4516 tmp->n.sym->assoc->dangling = 1;
4517 tmp->n.sym->assoc->st = tmp;
4519 select_type_stack->tmp = tmp;
4523 /* Match a SELECT TYPE statement. */
4525 match
4526 gfc_match_select_type (void)
4528 gfc_expr *expr1, *expr2 = NULL;
4529 match m;
4530 char name[GFC_MAX_SYMBOL_LEN];
4532 m = gfc_match_label ();
4533 if (m == MATCH_ERROR)
4534 return m;
4536 m = gfc_match (" select type ( ");
4537 if (m != MATCH_YES)
4538 return m;
4540 gfc_current_ns = gfc_build_block_ns (gfc_current_ns);
4542 m = gfc_match (" %n => %e", name, &expr2);
4543 if (m == MATCH_YES)
4545 expr1 = gfc_get_expr();
4546 expr1->expr_type = EXPR_VARIABLE;
4547 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
4549 m = MATCH_ERROR;
4550 goto cleanup;
4552 if (expr2->ts.type == BT_UNKNOWN)
4553 expr1->symtree->n.sym->attr.untyped = 1;
4554 else
4555 expr1->symtree->n.sym->ts = expr2->ts;
4556 expr1->symtree->n.sym->attr.flavor = FL_VARIABLE;
4557 expr1->symtree->n.sym->attr.referenced = 1;
4558 expr1->symtree->n.sym->attr.class_ok = 1;
4560 else
4562 m = gfc_match (" %e ", &expr1);
4563 if (m != MATCH_YES)
4564 goto cleanup;
4567 m = gfc_match (" )%t");
4568 if (m != MATCH_YES)
4569 goto cleanup;
4571 /* Check for F03:C811. */
4572 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE || expr1->ref != NULL))
4574 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
4575 "use associate-name=>");
4576 m = MATCH_ERROR;
4577 goto cleanup;
4580 new_st.op = EXEC_SELECT_TYPE;
4581 new_st.expr1 = expr1;
4582 new_st.expr2 = expr2;
4583 new_st.ext.block.ns = gfc_current_ns;
4585 select_type_push (expr1->symtree->n.sym);
4587 return MATCH_YES;
4589 cleanup:
4590 gfc_current_ns = gfc_current_ns->parent;
4591 return m;
4595 /* Match a CASE statement. */
4597 match
4598 gfc_match_case (void)
4600 gfc_case *c, *head, *tail;
4601 match m;
4603 head = tail = NULL;
4605 if (gfc_current_state () != COMP_SELECT)
4607 gfc_error ("Unexpected CASE statement at %C");
4608 return MATCH_ERROR;
4611 if (gfc_match ("% default") == MATCH_YES)
4613 m = match_case_eos ();
4614 if (m == MATCH_NO)
4615 goto syntax;
4616 if (m == MATCH_ERROR)
4617 goto cleanup;
4619 new_st.op = EXEC_SELECT;
4620 c = gfc_get_case ();
4621 c->where = gfc_current_locus;
4622 new_st.ext.case_list = c;
4623 return MATCH_YES;
4626 if (gfc_match_char ('(') != MATCH_YES)
4627 goto syntax;
4629 for (;;)
4631 if (match_case_selector (&c) == MATCH_ERROR)
4632 goto cleanup;
4634 if (head == NULL)
4635 head = c;
4636 else
4637 tail->next = c;
4639 tail = c;
4641 if (gfc_match_char (')') == MATCH_YES)
4642 break;
4643 if (gfc_match_char (',') != MATCH_YES)
4644 goto syntax;
4647 m = match_case_eos ();
4648 if (m == MATCH_NO)
4649 goto syntax;
4650 if (m == MATCH_ERROR)
4651 goto cleanup;
4653 new_st.op = EXEC_SELECT;
4654 new_st.ext.case_list = head;
4656 return MATCH_YES;
4658 syntax:
4659 gfc_error ("Syntax error in CASE specification at %C");
4661 cleanup:
4662 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
4663 return MATCH_ERROR;
4667 /* Match a TYPE IS statement. */
4669 match
4670 gfc_match_type_is (void)
4672 gfc_case *c = NULL;
4673 match m;
4675 if (gfc_current_state () != COMP_SELECT_TYPE)
4677 gfc_error ("Unexpected TYPE IS statement at %C");
4678 return MATCH_ERROR;
4681 if (gfc_match_char ('(') != MATCH_YES)
4682 goto syntax;
4684 c = gfc_get_case ();
4685 c->where = gfc_current_locus;
4687 /* TODO: Once unlimited polymorphism is implemented, we will need to call
4688 match_type_spec here. */
4689 if (match_derived_type_spec (&c->ts) == MATCH_ERROR)
4690 goto cleanup;
4692 if (gfc_match_char (')') != MATCH_YES)
4693 goto syntax;
4695 m = match_case_eos ();
4696 if (m == MATCH_NO)
4697 goto syntax;
4698 if (m == MATCH_ERROR)
4699 goto cleanup;
4701 new_st.op = EXEC_SELECT_TYPE;
4702 new_st.ext.case_list = c;
4704 /* Create temporary variable. */
4705 select_type_set_tmp (&c->ts);
4707 return MATCH_YES;
4709 syntax:
4710 gfc_error ("Syntax error in TYPE IS specification at %C");
4712 cleanup:
4713 if (c != NULL)
4714 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
4715 return MATCH_ERROR;
4719 /* Match a CLASS IS or CLASS DEFAULT statement. */
4721 match
4722 gfc_match_class_is (void)
4724 gfc_case *c = NULL;
4725 match m;
4727 if (gfc_current_state () != COMP_SELECT_TYPE)
4728 return MATCH_NO;
4730 if (gfc_match ("% default") == MATCH_YES)
4732 m = match_case_eos ();
4733 if (m == MATCH_NO)
4734 goto syntax;
4735 if (m == MATCH_ERROR)
4736 goto cleanup;
4738 new_st.op = EXEC_SELECT_TYPE;
4739 c = gfc_get_case ();
4740 c->where = gfc_current_locus;
4741 c->ts.type = BT_UNKNOWN;
4742 new_st.ext.case_list = c;
4743 select_type_set_tmp (NULL);
4744 return MATCH_YES;
4747 m = gfc_match ("% is");
4748 if (m == MATCH_NO)
4749 goto syntax;
4750 if (m == MATCH_ERROR)
4751 goto cleanup;
4753 if (gfc_match_char ('(') != MATCH_YES)
4754 goto syntax;
4756 c = gfc_get_case ();
4757 c->where = gfc_current_locus;
4759 if (match_derived_type_spec (&c->ts) == MATCH_ERROR)
4760 goto cleanup;
4762 if (c->ts.type == BT_DERIVED)
4763 c->ts.type = BT_CLASS;
4765 if (gfc_match_char (')') != MATCH_YES)
4766 goto syntax;
4768 m = match_case_eos ();
4769 if (m == MATCH_NO)
4770 goto syntax;
4771 if (m == MATCH_ERROR)
4772 goto cleanup;
4774 new_st.op = EXEC_SELECT_TYPE;
4775 new_st.ext.case_list = c;
4777 /* Create temporary variable. */
4778 select_type_set_tmp (&c->ts);
4780 return MATCH_YES;
4782 syntax:
4783 gfc_error ("Syntax error in CLASS IS specification at %C");
4785 cleanup:
4786 if (c != NULL)
4787 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
4788 return MATCH_ERROR;
4792 /********************* WHERE subroutines ********************/
4794 /* Match the rest of a simple WHERE statement that follows an IF statement.
4797 static match
4798 match_simple_where (void)
4800 gfc_expr *expr;
4801 gfc_code *c;
4802 match m;
4804 m = gfc_match (" ( %e )", &expr);
4805 if (m != MATCH_YES)
4806 return m;
4808 m = gfc_match_assignment ();
4809 if (m == MATCH_NO)
4810 goto syntax;
4811 if (m == MATCH_ERROR)
4812 goto cleanup;
4814 if (gfc_match_eos () != MATCH_YES)
4815 goto syntax;
4817 c = gfc_get_code ();
4819 c->op = EXEC_WHERE;
4820 c->expr1 = expr;
4821 c->next = gfc_get_code ();
4823 *c->next = new_st;
4824 gfc_clear_new_st ();
4826 new_st.op = EXEC_WHERE;
4827 new_st.block = c;
4829 return MATCH_YES;
4831 syntax:
4832 gfc_syntax_error (ST_WHERE);
4834 cleanup:
4835 gfc_free_expr (expr);
4836 return MATCH_ERROR;
4840 /* Match a WHERE statement. */
4842 match
4843 gfc_match_where (gfc_statement *st)
4845 gfc_expr *expr;
4846 match m0, m;
4847 gfc_code *c;
4849 m0 = gfc_match_label ();
4850 if (m0 == MATCH_ERROR)
4851 return m0;
4853 m = gfc_match (" where ( %e )", &expr);
4854 if (m != MATCH_YES)
4855 return m;
4857 if (gfc_match_eos () == MATCH_YES)
4859 *st = ST_WHERE_BLOCK;
4860 new_st.op = EXEC_WHERE;
4861 new_st.expr1 = expr;
4862 return MATCH_YES;
4865 m = gfc_match_assignment ();
4866 if (m == MATCH_NO)
4867 gfc_syntax_error (ST_WHERE);
4869 if (m != MATCH_YES)
4871 gfc_free_expr (expr);
4872 return MATCH_ERROR;
4875 /* We've got a simple WHERE statement. */
4876 *st = ST_WHERE;
4877 c = gfc_get_code ();
4879 c->op = EXEC_WHERE;
4880 c->expr1 = expr;
4881 c->next = gfc_get_code ();
4883 *c->next = new_st;
4884 gfc_clear_new_st ();
4886 new_st.op = EXEC_WHERE;
4887 new_st.block = c;
4889 return MATCH_YES;
4893 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
4894 new_st if successful. */
4896 match
4897 gfc_match_elsewhere (void)
4899 char name[GFC_MAX_SYMBOL_LEN + 1];
4900 gfc_expr *expr;
4901 match m;
4903 if (gfc_current_state () != COMP_WHERE)
4905 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
4906 return MATCH_ERROR;
4909 expr = NULL;
4911 if (gfc_match_char ('(') == MATCH_YES)
4913 m = gfc_match_expr (&expr);
4914 if (m == MATCH_NO)
4915 goto syntax;
4916 if (m == MATCH_ERROR)
4917 return MATCH_ERROR;
4919 if (gfc_match_char (')') != MATCH_YES)
4920 goto syntax;
4923 if (gfc_match_eos () != MATCH_YES)
4925 /* Only makes sense if we have a where-construct-name. */
4926 if (!gfc_current_block ())
4928 m = MATCH_ERROR;
4929 goto cleanup;
4931 /* Better be a name at this point. */
4932 m = gfc_match_name (name);
4933 if (m == MATCH_NO)
4934 goto syntax;
4935 if (m == MATCH_ERROR)
4936 goto cleanup;
4938 if (gfc_match_eos () != MATCH_YES)
4939 goto syntax;
4941 if (strcmp (name, gfc_current_block ()->name) != 0)
4943 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
4944 name, gfc_current_block ()->name);
4945 goto cleanup;
4949 new_st.op = EXEC_WHERE;
4950 new_st.expr1 = expr;
4951 return MATCH_YES;
4953 syntax:
4954 gfc_syntax_error (ST_ELSEWHERE);
4956 cleanup:
4957 gfc_free_expr (expr);
4958 return MATCH_ERROR;
4962 /******************** FORALL subroutines ********************/
4964 /* Free a list of FORALL iterators. */
4966 void
4967 gfc_free_forall_iterator (gfc_forall_iterator *iter)
4969 gfc_forall_iterator *next;
4971 while (iter)
4973 next = iter->next;
4974 gfc_free_expr (iter->var);
4975 gfc_free_expr (iter->start);
4976 gfc_free_expr (iter->end);
4977 gfc_free_expr (iter->stride);
4978 gfc_free (iter);
4979 iter = next;
4984 /* Match an iterator as part of a FORALL statement. The format is:
4986 <var> = <start>:<end>[:<stride>]
4988 On MATCH_NO, the caller tests for the possibility that there is a
4989 scalar mask expression. */
4991 static match
4992 match_forall_iterator (gfc_forall_iterator **result)
4994 gfc_forall_iterator *iter;
4995 locus where;
4996 match m;
4998 where = gfc_current_locus;
4999 iter = XCNEW (gfc_forall_iterator);
5001 m = gfc_match_expr (&iter->var);
5002 if (m != MATCH_YES)
5003 goto cleanup;
5005 if (gfc_match_char ('=') != MATCH_YES
5006 || iter->var->expr_type != EXPR_VARIABLE)
5008 m = MATCH_NO;
5009 goto cleanup;
5012 m = gfc_match_expr (&iter->start);
5013 if (m != MATCH_YES)
5014 goto cleanup;
5016 if (gfc_match_char (':') != MATCH_YES)
5017 goto syntax;
5019 m = gfc_match_expr (&iter->end);
5020 if (m == MATCH_NO)
5021 goto syntax;
5022 if (m == MATCH_ERROR)
5023 goto cleanup;
5025 if (gfc_match_char (':') == MATCH_NO)
5026 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5027 else
5029 m = gfc_match_expr (&iter->stride);
5030 if (m == MATCH_NO)
5031 goto syntax;
5032 if (m == MATCH_ERROR)
5033 goto cleanup;
5036 /* Mark the iteration variable's symbol as used as a FORALL index. */
5037 iter->var->symtree->n.sym->forall_index = true;
5039 *result = iter;
5040 return MATCH_YES;
5042 syntax:
5043 gfc_error ("Syntax error in FORALL iterator at %C");
5044 m = MATCH_ERROR;
5046 cleanup:
5048 gfc_current_locus = where;
5049 gfc_free_forall_iterator (iter);
5050 return m;
5054 /* Match the header of a FORALL statement. */
5056 static match
5057 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
5059 gfc_forall_iterator *head, *tail, *new_iter;
5060 gfc_expr *msk;
5061 match m;
5063 gfc_gobble_whitespace ();
5065 head = tail = NULL;
5066 msk = NULL;
5068 if (gfc_match_char ('(') != MATCH_YES)
5069 return MATCH_NO;
5071 m = match_forall_iterator (&new_iter);
5072 if (m == MATCH_ERROR)
5073 goto cleanup;
5074 if (m == MATCH_NO)
5075 goto syntax;
5077 head = tail = new_iter;
5079 for (;;)
5081 if (gfc_match_char (',') != MATCH_YES)
5082 break;
5084 m = match_forall_iterator (&new_iter);
5085 if (m == MATCH_ERROR)
5086 goto cleanup;
5088 if (m == MATCH_YES)
5090 tail->next = new_iter;
5091 tail = new_iter;
5092 continue;
5095 /* Have to have a mask expression. */
5097 m = gfc_match_expr (&msk);
5098 if (m == MATCH_NO)
5099 goto syntax;
5100 if (m == MATCH_ERROR)
5101 goto cleanup;
5103 break;
5106 if (gfc_match_char (')') == MATCH_NO)
5107 goto syntax;
5109 *phead = head;
5110 *mask = msk;
5111 return MATCH_YES;
5113 syntax:
5114 gfc_syntax_error (ST_FORALL);
5116 cleanup:
5117 gfc_free_expr (msk);
5118 gfc_free_forall_iterator (head);
5120 return MATCH_ERROR;
5123 /* Match the rest of a simple FORALL statement that follows an
5124 IF statement. */
5126 static match
5127 match_simple_forall (void)
5129 gfc_forall_iterator *head;
5130 gfc_expr *mask;
5131 gfc_code *c;
5132 match m;
5134 mask = NULL;
5135 head = NULL;
5136 c = NULL;
5138 m = match_forall_header (&head, &mask);
5140 if (m == MATCH_NO)
5141 goto syntax;
5142 if (m != MATCH_YES)
5143 goto cleanup;
5145 m = gfc_match_assignment ();
5147 if (m == MATCH_ERROR)
5148 goto cleanup;
5149 if (m == MATCH_NO)
5151 m = gfc_match_pointer_assignment ();
5152 if (m == MATCH_ERROR)
5153 goto cleanup;
5154 if (m == MATCH_NO)
5155 goto syntax;
5158 c = gfc_get_code ();
5159 *c = new_st;
5160 c->loc = gfc_current_locus;
5162 if (gfc_match_eos () != MATCH_YES)
5163 goto syntax;
5165 gfc_clear_new_st ();
5166 new_st.op = EXEC_FORALL;
5167 new_st.expr1 = mask;
5168 new_st.ext.forall_iterator = head;
5169 new_st.block = gfc_get_code ();
5171 new_st.block->op = EXEC_FORALL;
5172 new_st.block->next = c;
5174 return MATCH_YES;
5176 syntax:
5177 gfc_syntax_error (ST_FORALL);
5179 cleanup:
5180 gfc_free_forall_iterator (head);
5181 gfc_free_expr (mask);
5183 return MATCH_ERROR;
5187 /* Match a FORALL statement. */
5189 match
5190 gfc_match_forall (gfc_statement *st)
5192 gfc_forall_iterator *head;
5193 gfc_expr *mask;
5194 gfc_code *c;
5195 match m0, m;
5197 head = NULL;
5198 mask = NULL;
5199 c = NULL;
5201 m0 = gfc_match_label ();
5202 if (m0 == MATCH_ERROR)
5203 return MATCH_ERROR;
5205 m = gfc_match (" forall");
5206 if (m != MATCH_YES)
5207 return m;
5209 m = match_forall_header (&head, &mask);
5210 if (m == MATCH_ERROR)
5211 goto cleanup;
5212 if (m == MATCH_NO)
5213 goto syntax;
5215 if (gfc_match_eos () == MATCH_YES)
5217 *st = ST_FORALL_BLOCK;
5218 new_st.op = EXEC_FORALL;
5219 new_st.expr1 = mask;
5220 new_st.ext.forall_iterator = head;
5221 return MATCH_YES;
5224 m = gfc_match_assignment ();
5225 if (m == MATCH_ERROR)
5226 goto cleanup;
5227 if (m == MATCH_NO)
5229 m = gfc_match_pointer_assignment ();
5230 if (m == MATCH_ERROR)
5231 goto cleanup;
5232 if (m == MATCH_NO)
5233 goto syntax;
5236 c = gfc_get_code ();
5237 *c = new_st;
5238 c->loc = gfc_current_locus;
5240 gfc_clear_new_st ();
5241 new_st.op = EXEC_FORALL;
5242 new_st.expr1 = mask;
5243 new_st.ext.forall_iterator = head;
5244 new_st.block = gfc_get_code ();
5245 new_st.block->op = EXEC_FORALL;
5246 new_st.block->next = c;
5248 *st = ST_FORALL;
5249 return MATCH_YES;
5251 syntax:
5252 gfc_syntax_error (ST_FORALL);
5254 cleanup:
5255 gfc_free_forall_iterator (head);
5256 gfc_free_expr (mask);
5257 gfc_free_statements (c);
5258 return MATCH_NO;