2008-09-25 Tobias Burnus <burnus@net-b.de>
[official-gcc/alias-decl.git] / gcc / fortran / match.c
blobc8fd30d754f568816b11dd2b11b15296ae645121
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "parse.h"
29 int gfc_matching_procptr_assignment = 0;
30 bool gfc_matching_prefix = false;
32 /* For debugging and diagnostic purposes. Return the textual representation
33 of the intrinsic operator OP. */
34 const char *
35 gfc_op2string (gfc_intrinsic_op op)
37 switch (op)
39 case INTRINSIC_UPLUS:
40 case INTRINSIC_PLUS:
41 return "+";
43 case INTRINSIC_UMINUS:
44 case INTRINSIC_MINUS:
45 return "-";
47 case INTRINSIC_POWER:
48 return "**";
49 case INTRINSIC_CONCAT:
50 return "//";
51 case INTRINSIC_TIMES:
52 return "*";
53 case INTRINSIC_DIVIDE:
54 return "/";
56 case INTRINSIC_AND:
57 return ".and.";
58 case INTRINSIC_OR:
59 return ".or.";
60 case INTRINSIC_EQV:
61 return ".eqv.";
62 case INTRINSIC_NEQV:
63 return ".neqv.";
65 case INTRINSIC_EQ_OS:
66 return ".eq.";
67 case INTRINSIC_EQ:
68 return "==";
69 case INTRINSIC_NE_OS:
70 return ".ne.";
71 case INTRINSIC_NE:
72 return "/=";
73 case INTRINSIC_GE_OS:
74 return ".ge.";
75 case INTRINSIC_GE:
76 return ">=";
77 case INTRINSIC_LE_OS:
78 return ".le.";
79 case INTRINSIC_LE:
80 return "<=";
81 case INTRINSIC_LT_OS:
82 return ".lt.";
83 case INTRINSIC_LT:
84 return "<";
85 case INTRINSIC_GT_OS:
86 return ".gt.";
87 case INTRINSIC_GT:
88 return ">";
89 case INTRINSIC_NOT:
90 return ".not.";
92 case INTRINSIC_ASSIGN:
93 return "=";
95 case INTRINSIC_PARENTHESES:
96 return "parens";
98 default:
99 break;
102 gfc_internal_error ("gfc_op2string(): Bad code");
103 /* Not reached. */
107 /******************** Generic matching subroutines ************************/
109 /* This function scans the current statement counting the opened and closed
110 parenthesis to make sure they are balanced. */
112 match
113 gfc_match_parens (void)
115 locus old_loc, where;
116 int count, instring;
117 gfc_char_t c, quote;
119 old_loc = gfc_current_locus;
120 count = 0;
121 instring = 0;
122 quote = ' ';
124 for (;;)
126 c = gfc_next_char_literal (instring);
127 if (c == '\n')
128 break;
129 if (quote == ' ' && ((c == '\'') || (c == '"')))
131 quote = c;
132 instring = 1;
133 continue;
135 if (quote != ' ' && c == quote)
137 quote = ' ';
138 instring = 0;
139 continue;
142 if (c == '(' && quote == ' ')
144 count++;
145 where = gfc_current_locus;
147 if (c == ')' && quote == ' ')
149 count--;
150 where = gfc_current_locus;
154 gfc_current_locus = old_loc;
156 if (count > 0)
158 gfc_error ("Missing ')' in statement at or before %L", &where);
159 return MATCH_ERROR;
161 if (count < 0)
163 gfc_error ("Missing '(' in statement at or before %L", &where);
164 return MATCH_ERROR;
167 return MATCH_YES;
171 /* See if the next character is a special character that has
172 escaped by a \ via the -fbackslash option. */
174 match
175 gfc_match_special_char (gfc_char_t *res)
177 int len, i;
178 gfc_char_t c, n;
179 match m;
181 m = MATCH_YES;
183 switch ((c = gfc_next_char_literal (1)))
185 case 'a':
186 *res = '\a';
187 break;
188 case 'b':
189 *res = '\b';
190 break;
191 case 't':
192 *res = '\t';
193 break;
194 case 'f':
195 *res = '\f';
196 break;
197 case 'n':
198 *res = '\n';
199 break;
200 case 'r':
201 *res = '\r';
202 break;
203 case 'v':
204 *res = '\v';
205 break;
206 case '\\':
207 *res = '\\';
208 break;
209 case '0':
210 *res = '\0';
211 break;
213 case 'x':
214 case 'u':
215 case 'U':
216 /* Hexadecimal form of wide characters. */
217 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
218 n = 0;
219 for (i = 0; i < len; i++)
221 char buf[2] = { '\0', '\0' };
223 c = gfc_next_char_literal (1);
224 if (!gfc_wide_fits_in_byte (c)
225 || !gfc_check_digit ((unsigned char) c, 16))
226 return MATCH_NO;
228 buf[0] = (unsigned char) c;
229 n = n << 4;
230 n += strtol (buf, NULL, 16);
232 *res = n;
233 break;
235 default:
236 /* Unknown backslash codes are simply not expanded. */
237 m = MATCH_NO;
238 break;
241 return m;
245 /* In free form, match at least one space. Always matches in fixed
246 form. */
248 match
249 gfc_match_space (void)
251 locus old_loc;
252 char c;
254 if (gfc_current_form == FORM_FIXED)
255 return MATCH_YES;
257 old_loc = gfc_current_locus;
259 c = gfc_next_ascii_char ();
260 if (!gfc_is_whitespace (c))
262 gfc_current_locus = old_loc;
263 return MATCH_NO;
266 gfc_gobble_whitespace ();
268 return MATCH_YES;
272 /* Match an end of statement. End of statement is optional
273 whitespace, followed by a ';' or '\n' or comment '!'. If a
274 semicolon is found, we continue to eat whitespace and semicolons. */
276 match
277 gfc_match_eos (void)
279 locus old_loc;
280 int flag;
281 char c;
283 flag = 0;
285 for (;;)
287 old_loc = gfc_current_locus;
288 gfc_gobble_whitespace ();
290 c = gfc_next_ascii_char ();
291 switch (c)
293 case '!':
296 c = gfc_next_ascii_char ();
298 while (c != '\n');
300 /* Fall through. */
302 case '\n':
303 return MATCH_YES;
305 case ';':
306 flag = 1;
307 continue;
310 break;
313 gfc_current_locus = old_loc;
314 return (flag) ? MATCH_YES : MATCH_NO;
318 /* Match a literal integer on the input, setting the value on
319 MATCH_YES. Literal ints occur in kind-parameters as well as
320 old-style character length specifications. If cnt is non-NULL it
321 will be set to the number of digits. */
323 match
324 gfc_match_small_literal_int (int *value, int *cnt)
326 locus old_loc;
327 char c;
328 int i, j;
330 old_loc = gfc_current_locus;
332 *value = -1;
333 gfc_gobble_whitespace ();
334 c = gfc_next_ascii_char ();
335 if (cnt)
336 *cnt = 0;
338 if (!ISDIGIT (c))
340 gfc_current_locus = old_loc;
341 return MATCH_NO;
344 i = c - '0';
345 j = 1;
347 for (;;)
349 old_loc = gfc_current_locus;
350 c = gfc_next_ascii_char ();
352 if (!ISDIGIT (c))
353 break;
355 i = 10 * i + c - '0';
356 j++;
358 if (i > 99999999)
360 gfc_error ("Integer too large at %C");
361 return MATCH_ERROR;
365 gfc_current_locus = old_loc;
367 *value = i;
368 if (cnt)
369 *cnt = j;
370 return MATCH_YES;
374 /* Match a small, constant integer expression, like in a kind
375 statement. On MATCH_YES, 'value' is set. */
377 match
378 gfc_match_small_int (int *value)
380 gfc_expr *expr;
381 const char *p;
382 match m;
383 int i;
385 m = gfc_match_expr (&expr);
386 if (m != MATCH_YES)
387 return m;
389 p = gfc_extract_int (expr, &i);
390 gfc_free_expr (expr);
392 if (p != NULL)
394 gfc_error (p);
395 m = MATCH_ERROR;
398 *value = i;
399 return m;
403 /* This function is the same as the gfc_match_small_int, except that
404 we're keeping the pointer to the expr. This function could just be
405 removed and the previously mentioned one modified, though all calls
406 to it would have to be modified then (and there were a number of
407 them). Return MATCH_ERROR if fail to extract the int; otherwise,
408 return the result of gfc_match_expr(). The expr (if any) that was
409 matched is returned in the parameter expr. */
411 match
412 gfc_match_small_int_expr (int *value, gfc_expr **expr)
414 const char *p;
415 match m;
416 int i;
418 m = gfc_match_expr (expr);
419 if (m != MATCH_YES)
420 return m;
422 p = gfc_extract_int (*expr, &i);
424 if (p != NULL)
426 gfc_error (p);
427 m = MATCH_ERROR;
430 *value = i;
431 return m;
435 /* Matches a statement label. Uses gfc_match_small_literal_int() to
436 do most of the work. */
438 match
439 gfc_match_st_label (gfc_st_label **label)
441 locus old_loc;
442 match m;
443 int i, cnt;
445 old_loc = gfc_current_locus;
447 m = gfc_match_small_literal_int (&i, &cnt);
448 if (m != MATCH_YES)
449 return m;
451 if (cnt > 5)
453 gfc_error ("Too many digits in statement label at %C");
454 goto cleanup;
457 if (i == 0)
459 gfc_error ("Statement label at %C is zero");
460 goto cleanup;
463 *label = gfc_get_st_label (i);
464 return MATCH_YES;
466 cleanup:
468 gfc_current_locus = old_loc;
469 return MATCH_ERROR;
473 /* Match and validate a label associated with a named IF, DO or SELECT
474 statement. If the symbol does not have the label attribute, we add
475 it. We also make sure the symbol does not refer to another
476 (active) block. A matched label is pointed to by gfc_new_block. */
478 match
479 gfc_match_label (void)
481 char name[GFC_MAX_SYMBOL_LEN + 1];
482 match m;
484 gfc_new_block = NULL;
486 m = gfc_match (" %n :", name);
487 if (m != MATCH_YES)
488 return m;
490 if (gfc_get_symbol (name, NULL, &gfc_new_block))
492 gfc_error ("Label name '%s' at %C is ambiguous", name);
493 return MATCH_ERROR;
496 if (gfc_new_block->attr.flavor == FL_LABEL)
498 gfc_error ("Duplicate construct label '%s' at %C", name);
499 return MATCH_ERROR;
502 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
503 gfc_new_block->name, NULL) == FAILURE)
504 return MATCH_ERROR;
506 return MATCH_YES;
510 /* See if the current input looks like a name of some sort. Modifies
511 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
512 Note that options.c restricts max_identifier_length to not more
513 than GFC_MAX_SYMBOL_LEN. */
515 match
516 gfc_match_name (char *buffer)
518 locus old_loc;
519 int i;
520 char c;
522 old_loc = gfc_current_locus;
523 gfc_gobble_whitespace ();
525 c = gfc_next_ascii_char ();
526 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
528 if (gfc_error_flag_test() == 0 && c != '(')
529 gfc_error ("Invalid character in name at %C");
530 gfc_current_locus = old_loc;
531 return MATCH_NO;
534 i = 0;
538 buffer[i++] = c;
540 if (i > gfc_option.max_identifier_length)
542 gfc_error ("Name at %C is too long");
543 return MATCH_ERROR;
546 old_loc = gfc_current_locus;
547 c = gfc_next_ascii_char ();
549 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
551 if (c == '$' && !gfc_option.flag_dollar_ok)
553 gfc_error ("Invalid character '$' at %C. Use -fdollar-ok to allow it "
554 "as an extension");
555 return MATCH_ERROR;
558 buffer[i] = '\0';
559 gfc_current_locus = old_loc;
561 return MATCH_YES;
565 /* Match a valid name for C, which is almost the same as for Fortran,
566 except that you can start with an underscore, etc.. It could have
567 been done by modifying the gfc_match_name, but this way other
568 things C allows can be added, such as no limits on the length.
569 Right now, the length is limited to the same thing as Fortran..
570 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
571 input characters from being automatically lower cased, since C is
572 case sensitive. The parameter, buffer, is used to return the name
573 that is matched. Return MATCH_ERROR if the name is too long
574 (though this is a self-imposed limit), MATCH_NO if what we're
575 seeing isn't a name, and MATCH_YES if we successfully match a C
576 name. */
578 match
579 gfc_match_name_C (char *buffer)
581 locus old_loc;
582 int i = 0;
583 gfc_char_t c;
585 old_loc = gfc_current_locus;
586 gfc_gobble_whitespace ();
588 /* Get the next char (first possible char of name) and see if
589 it's valid for C (either a letter or an underscore). */
590 c = gfc_next_char_literal (1);
592 /* If the user put nothing expect spaces between the quotes, it is valid
593 and simply means there is no name= specifier and the name is the fortran
594 symbol name, all lowercase. */
595 if (c == '"' || c == '\'')
597 buffer[0] = '\0';
598 gfc_current_locus = old_loc;
599 return MATCH_YES;
602 if (!ISALPHA (c) && c != '_')
604 gfc_error ("Invalid C name in NAME= specifier at %C");
605 return MATCH_ERROR;
608 /* Continue to read valid variable name characters. */
611 gcc_assert (gfc_wide_fits_in_byte (c));
613 buffer[i++] = (unsigned char) c;
615 /* C does not define a maximum length of variable names, to my
616 knowledge, but the compiler typically places a limit on them.
617 For now, i'll use the same as the fortran limit for simplicity,
618 but this may need to be changed to a dynamic buffer that can
619 be realloc'ed here if necessary, or more likely, a larger
620 upper-bound set. */
621 if (i > gfc_option.max_identifier_length)
623 gfc_error ("Name at %C is too long");
624 return MATCH_ERROR;
627 old_loc = gfc_current_locus;
629 /* Get next char; param means we're in a string. */
630 c = gfc_next_char_literal (1);
631 } while (ISALNUM (c) || c == '_');
633 buffer[i] = '\0';
634 gfc_current_locus = old_loc;
636 /* See if we stopped because of whitespace. */
637 if (c == ' ')
639 gfc_gobble_whitespace ();
640 c = gfc_peek_ascii_char ();
641 if (c != '"' && c != '\'')
643 gfc_error ("Embedded space in NAME= specifier at %C");
644 return MATCH_ERROR;
648 /* If we stopped because we had an invalid character for a C name, report
649 that to the user by returning MATCH_NO. */
650 if (c != '"' && c != '\'')
652 gfc_error ("Invalid C name in NAME= specifier at %C");
653 return MATCH_ERROR;
656 return MATCH_YES;
660 /* Match a symbol on the input. Modifies the pointer to the symbol
661 pointer if successful. */
663 match
664 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
666 char buffer[GFC_MAX_SYMBOL_LEN + 1];
667 match m;
669 m = gfc_match_name (buffer);
670 if (m != MATCH_YES)
671 return m;
673 if (host_assoc)
674 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
675 ? MATCH_ERROR : MATCH_YES;
677 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
678 return MATCH_ERROR;
680 return MATCH_YES;
684 match
685 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
687 gfc_symtree *st;
688 match m;
690 m = gfc_match_sym_tree (&st, host_assoc);
692 if (m == MATCH_YES)
694 if (st)
695 *matched_symbol = st->n.sym;
696 else
697 *matched_symbol = NULL;
699 else
700 *matched_symbol = NULL;
701 return m;
705 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
706 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
707 in matchexp.c. */
709 match
710 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
712 locus orig_loc = gfc_current_locus;
713 char ch;
715 gfc_gobble_whitespace ();
716 ch = gfc_next_ascii_char ();
717 switch (ch)
719 case '+':
720 /* Matched "+". */
721 *result = INTRINSIC_PLUS;
722 return MATCH_YES;
724 case '-':
725 /* Matched "-". */
726 *result = INTRINSIC_MINUS;
727 return MATCH_YES;
729 case '=':
730 if (gfc_next_ascii_char () == '=')
732 /* Matched "==". */
733 *result = INTRINSIC_EQ;
734 return MATCH_YES;
736 break;
738 case '<':
739 if (gfc_peek_ascii_char () == '=')
741 /* Matched "<=". */
742 gfc_next_ascii_char ();
743 *result = INTRINSIC_LE;
744 return MATCH_YES;
746 /* Matched "<". */
747 *result = INTRINSIC_LT;
748 return MATCH_YES;
750 case '>':
751 if (gfc_peek_ascii_char () == '=')
753 /* Matched ">=". */
754 gfc_next_ascii_char ();
755 *result = INTRINSIC_GE;
756 return MATCH_YES;
758 /* Matched ">". */
759 *result = INTRINSIC_GT;
760 return MATCH_YES;
762 case '*':
763 if (gfc_peek_ascii_char () == '*')
765 /* Matched "**". */
766 gfc_next_ascii_char ();
767 *result = INTRINSIC_POWER;
768 return MATCH_YES;
770 /* Matched "*". */
771 *result = INTRINSIC_TIMES;
772 return MATCH_YES;
774 case '/':
775 ch = gfc_peek_ascii_char ();
776 if (ch == '=')
778 /* Matched "/=". */
779 gfc_next_ascii_char ();
780 *result = INTRINSIC_NE;
781 return MATCH_YES;
783 else if (ch == '/')
785 /* Matched "//". */
786 gfc_next_ascii_char ();
787 *result = INTRINSIC_CONCAT;
788 return MATCH_YES;
790 /* Matched "/". */
791 *result = INTRINSIC_DIVIDE;
792 return MATCH_YES;
794 case '.':
795 ch = gfc_next_ascii_char ();
796 switch (ch)
798 case 'a':
799 if (gfc_next_ascii_char () == 'n'
800 && gfc_next_ascii_char () == 'd'
801 && gfc_next_ascii_char () == '.')
803 /* Matched ".and.". */
804 *result = INTRINSIC_AND;
805 return MATCH_YES;
807 break;
809 case 'e':
810 if (gfc_next_ascii_char () == 'q')
812 ch = gfc_next_ascii_char ();
813 if (ch == '.')
815 /* Matched ".eq.". */
816 *result = INTRINSIC_EQ_OS;
817 return MATCH_YES;
819 else if (ch == 'v')
821 if (gfc_next_ascii_char () == '.')
823 /* Matched ".eqv.". */
824 *result = INTRINSIC_EQV;
825 return MATCH_YES;
829 break;
831 case 'g':
832 ch = gfc_next_ascii_char ();
833 if (ch == 'e')
835 if (gfc_next_ascii_char () == '.')
837 /* Matched ".ge.". */
838 *result = INTRINSIC_GE_OS;
839 return MATCH_YES;
842 else if (ch == 't')
844 if (gfc_next_ascii_char () == '.')
846 /* Matched ".gt.". */
847 *result = INTRINSIC_GT_OS;
848 return MATCH_YES;
851 break;
853 case 'l':
854 ch = gfc_next_ascii_char ();
855 if (ch == 'e')
857 if (gfc_next_ascii_char () == '.')
859 /* Matched ".le.". */
860 *result = INTRINSIC_LE_OS;
861 return MATCH_YES;
864 else if (ch == 't')
866 if (gfc_next_ascii_char () == '.')
868 /* Matched ".lt.". */
869 *result = INTRINSIC_LT_OS;
870 return MATCH_YES;
873 break;
875 case 'n':
876 ch = gfc_next_ascii_char ();
877 if (ch == 'e')
879 ch = gfc_next_ascii_char ();
880 if (ch == '.')
882 /* Matched ".ne.". */
883 *result = INTRINSIC_NE_OS;
884 return MATCH_YES;
886 else if (ch == 'q')
888 if (gfc_next_ascii_char () == 'v'
889 && gfc_next_ascii_char () == '.')
891 /* Matched ".neqv.". */
892 *result = INTRINSIC_NEQV;
893 return MATCH_YES;
897 else if (ch == 'o')
899 if (gfc_next_ascii_char () == 't'
900 && gfc_next_ascii_char () == '.')
902 /* Matched ".not.". */
903 *result = INTRINSIC_NOT;
904 return MATCH_YES;
907 break;
909 case 'o':
910 if (gfc_next_ascii_char () == 'r'
911 && gfc_next_ascii_char () == '.')
913 /* Matched ".or.". */
914 *result = INTRINSIC_OR;
915 return MATCH_YES;
917 break;
919 default:
920 break;
922 break;
924 default:
925 break;
928 gfc_current_locus = orig_loc;
929 return MATCH_NO;
933 /* Match a loop control phrase:
935 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
937 If the final integer expression is not present, a constant unity
938 expression is returned. We don't return MATCH_ERROR until after
939 the equals sign is seen. */
941 match
942 gfc_match_iterator (gfc_iterator *iter, int init_flag)
944 char name[GFC_MAX_SYMBOL_LEN + 1];
945 gfc_expr *var, *e1, *e2, *e3;
946 locus start;
947 match m;
949 /* Match the start of an iterator without affecting the symbol table. */
951 start = gfc_current_locus;
952 m = gfc_match (" %n =", name);
953 gfc_current_locus = start;
955 if (m != MATCH_YES)
956 return MATCH_NO;
958 m = gfc_match_variable (&var, 0);
959 if (m != MATCH_YES)
960 return MATCH_NO;
962 gfc_match_char ('=');
964 e1 = e2 = e3 = NULL;
966 if (var->ref != NULL)
968 gfc_error ("Loop variable at %C cannot be a sub-component");
969 goto cleanup;
972 if (var->symtree->n.sym->attr.intent == INTENT_IN)
974 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
975 var->symtree->n.sym->name);
976 goto cleanup;
979 var->symtree->n.sym->attr.implied_index = 1;
981 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
982 if (m == MATCH_NO)
983 goto syntax;
984 if (m == MATCH_ERROR)
985 goto cleanup;
987 if (gfc_match_char (',') != MATCH_YES)
988 goto syntax;
990 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
991 if (m == MATCH_NO)
992 goto syntax;
993 if (m == MATCH_ERROR)
994 goto cleanup;
996 if (gfc_match_char (',') != MATCH_YES)
998 e3 = gfc_int_expr (1);
999 goto done;
1002 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1003 if (m == MATCH_ERROR)
1004 goto cleanup;
1005 if (m == MATCH_NO)
1007 gfc_error ("Expected a step value in iterator at %C");
1008 goto cleanup;
1011 done:
1012 iter->var = var;
1013 iter->start = e1;
1014 iter->end = e2;
1015 iter->step = e3;
1016 return MATCH_YES;
1018 syntax:
1019 gfc_error ("Syntax error in iterator at %C");
1021 cleanup:
1022 gfc_free_expr (e1);
1023 gfc_free_expr (e2);
1024 gfc_free_expr (e3);
1026 return MATCH_ERROR;
1030 /* Tries to match the next non-whitespace character on the input.
1031 This subroutine does not return MATCH_ERROR. */
1033 match
1034 gfc_match_char (char c)
1036 locus where;
1038 where = gfc_current_locus;
1039 gfc_gobble_whitespace ();
1041 if (gfc_next_ascii_char () == c)
1042 return MATCH_YES;
1044 gfc_current_locus = where;
1045 return MATCH_NO;
1049 /* General purpose matching subroutine. The target string is a
1050 scanf-like format string in which spaces correspond to arbitrary
1051 whitespace (including no whitespace), characters correspond to
1052 themselves. The %-codes are:
1054 %% Literal percent sign
1055 %e Expression, pointer to a pointer is set
1056 %s Symbol, pointer to the symbol is set
1057 %n Name, character buffer is set to name
1058 %t Matches end of statement.
1059 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1060 %l Matches a statement label
1061 %v Matches a variable expression (an lvalue)
1062 % Matches a required space (in free form) and optional spaces. */
1064 match
1065 gfc_match (const char *target, ...)
1067 gfc_st_label **label;
1068 int matches, *ip;
1069 locus old_loc;
1070 va_list argp;
1071 char c, *np;
1072 match m, n;
1073 void **vp;
1074 const char *p;
1076 old_loc = gfc_current_locus;
1077 va_start (argp, target);
1078 m = MATCH_NO;
1079 matches = 0;
1080 p = target;
1082 loop:
1083 c = *p++;
1084 switch (c)
1086 case ' ':
1087 gfc_gobble_whitespace ();
1088 goto loop;
1089 case '\0':
1090 m = MATCH_YES;
1091 break;
1093 case '%':
1094 c = *p++;
1095 switch (c)
1097 case 'e':
1098 vp = va_arg (argp, void **);
1099 n = gfc_match_expr ((gfc_expr **) vp);
1100 if (n != MATCH_YES)
1102 m = n;
1103 goto not_yes;
1106 matches++;
1107 goto loop;
1109 case 'v':
1110 vp = va_arg (argp, void **);
1111 n = gfc_match_variable ((gfc_expr **) vp, 0);
1112 if (n != MATCH_YES)
1114 m = n;
1115 goto not_yes;
1118 matches++;
1119 goto loop;
1121 case 's':
1122 vp = va_arg (argp, void **);
1123 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1124 if (n != MATCH_YES)
1126 m = n;
1127 goto not_yes;
1130 matches++;
1131 goto loop;
1133 case 'n':
1134 np = va_arg (argp, char *);
1135 n = gfc_match_name (np);
1136 if (n != MATCH_YES)
1138 m = n;
1139 goto not_yes;
1142 matches++;
1143 goto loop;
1145 case 'l':
1146 label = va_arg (argp, gfc_st_label **);
1147 n = gfc_match_st_label (label);
1148 if (n != MATCH_YES)
1150 m = n;
1151 goto not_yes;
1154 matches++;
1155 goto loop;
1157 case 'o':
1158 ip = va_arg (argp, int *);
1159 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1160 if (n != MATCH_YES)
1162 m = n;
1163 goto not_yes;
1166 matches++;
1167 goto loop;
1169 case 't':
1170 if (gfc_match_eos () != MATCH_YES)
1172 m = MATCH_NO;
1173 goto not_yes;
1175 goto loop;
1177 case ' ':
1178 if (gfc_match_space () == MATCH_YES)
1179 goto loop;
1180 m = MATCH_NO;
1181 goto not_yes;
1183 case '%':
1184 break; /* Fall through to character matcher. */
1186 default:
1187 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1190 default:
1192 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1193 expect an upper case character here! */
1194 gcc_assert (TOLOWER (c) == c);
1196 if (c == gfc_next_ascii_char ())
1197 goto loop;
1198 break;
1201 not_yes:
1202 va_end (argp);
1204 if (m != MATCH_YES)
1206 /* Clean up after a failed match. */
1207 gfc_current_locus = old_loc;
1208 va_start (argp, target);
1210 p = target;
1211 for (; matches > 0; matches--)
1213 while (*p++ != '%');
1215 switch (*p++)
1217 case '%':
1218 matches++;
1219 break; /* Skip. */
1221 /* Matches that don't have to be undone */
1222 case 'o':
1223 case 'l':
1224 case 'n':
1225 case 's':
1226 (void) va_arg (argp, void **);
1227 break;
1229 case 'e':
1230 case 'v':
1231 vp = va_arg (argp, void **);
1232 gfc_free_expr ((struct gfc_expr *)*vp);
1233 *vp = NULL;
1234 break;
1238 va_end (argp);
1241 return m;
1245 /*********************** Statement level matching **********************/
1247 /* Matches the start of a program unit, which is the program keyword
1248 followed by an obligatory symbol. */
1250 match
1251 gfc_match_program (void)
1253 gfc_symbol *sym;
1254 match m;
1256 m = gfc_match ("% %s%t", &sym);
1258 if (m == MATCH_NO)
1260 gfc_error ("Invalid form of PROGRAM statement at %C");
1261 m = MATCH_ERROR;
1264 if (m == MATCH_ERROR)
1265 return m;
1267 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1268 return MATCH_ERROR;
1270 gfc_new_block = sym;
1272 return MATCH_YES;
1276 /* Match a simple assignment statement. */
1278 match
1279 gfc_match_assignment (void)
1281 gfc_expr *lvalue, *rvalue;
1282 locus old_loc;
1283 match m;
1285 old_loc = gfc_current_locus;
1287 lvalue = NULL;
1288 m = gfc_match (" %v =", &lvalue);
1289 if (m != MATCH_YES)
1291 gfc_current_locus = old_loc;
1292 gfc_free_expr (lvalue);
1293 return MATCH_NO;
1296 rvalue = NULL;
1297 m = gfc_match (" %e%t", &rvalue);
1298 if (m != MATCH_YES)
1300 gfc_current_locus = old_loc;
1301 gfc_free_expr (lvalue);
1302 gfc_free_expr (rvalue);
1303 return m;
1306 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1308 new_st.op = EXEC_ASSIGN;
1309 new_st.expr = lvalue;
1310 new_st.expr2 = rvalue;
1312 gfc_check_do_variable (lvalue->symtree);
1314 return MATCH_YES;
1318 /* Match a pointer assignment statement. */
1320 match
1321 gfc_match_pointer_assignment (void)
1323 gfc_expr *lvalue, *rvalue;
1324 locus old_loc;
1325 match m;
1327 old_loc = gfc_current_locus;
1329 lvalue = rvalue = NULL;
1330 gfc_matching_procptr_assignment = 0;
1332 m = gfc_match (" %v =>", &lvalue);
1333 if (m != MATCH_YES)
1335 m = MATCH_NO;
1336 goto cleanup;
1339 if (lvalue->symtree->n.sym->attr.proc_pointer)
1340 gfc_matching_procptr_assignment = 1;
1342 m = gfc_match (" %e%t", &rvalue);
1343 gfc_matching_procptr_assignment = 0;
1344 if (m != MATCH_YES)
1345 goto cleanup;
1347 new_st.op = EXEC_POINTER_ASSIGN;
1348 new_st.expr = lvalue;
1349 new_st.expr2 = rvalue;
1351 return MATCH_YES;
1353 cleanup:
1354 gfc_current_locus = old_loc;
1355 gfc_free_expr (lvalue);
1356 gfc_free_expr (rvalue);
1357 return m;
1361 /* We try to match an easy arithmetic IF statement. This only happens
1362 when just after having encountered a simple IF statement. This code
1363 is really duplicate with parts of the gfc_match_if code, but this is
1364 *much* easier. */
1366 static match
1367 match_arithmetic_if (void)
1369 gfc_st_label *l1, *l2, *l3;
1370 gfc_expr *expr;
1371 match m;
1373 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1374 if (m != MATCH_YES)
1375 return m;
1377 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1378 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1379 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1381 gfc_free_expr (expr);
1382 return MATCH_ERROR;
1385 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF statement "
1386 "at %C") == FAILURE)
1387 return MATCH_ERROR;
1389 new_st.op = EXEC_ARITHMETIC_IF;
1390 new_st.expr = expr;
1391 new_st.label = l1;
1392 new_st.label2 = l2;
1393 new_st.label3 = l3;
1395 return MATCH_YES;
1399 /* The IF statement is a bit of a pain. First of all, there are three
1400 forms of it, the simple IF, the IF that starts a block and the
1401 arithmetic IF.
1403 There is a problem with the simple IF and that is the fact that we
1404 only have a single level of undo information on symbols. What this
1405 means is for a simple IF, we must re-match the whole IF statement
1406 multiple times in order to guarantee that the symbol table ends up
1407 in the proper state. */
1409 static match match_simple_forall (void);
1410 static match match_simple_where (void);
1412 match
1413 gfc_match_if (gfc_statement *if_type)
1415 gfc_expr *expr;
1416 gfc_st_label *l1, *l2, *l3;
1417 locus old_loc, old_loc2;
1418 gfc_code *p;
1419 match m, n;
1421 n = gfc_match_label ();
1422 if (n == MATCH_ERROR)
1423 return n;
1425 old_loc = gfc_current_locus;
1427 m = gfc_match (" if ( %e", &expr);
1428 if (m != MATCH_YES)
1429 return m;
1431 old_loc2 = gfc_current_locus;
1432 gfc_current_locus = old_loc;
1434 if (gfc_match_parens () == MATCH_ERROR)
1435 return MATCH_ERROR;
1437 gfc_current_locus = old_loc2;
1439 if (gfc_match_char (')') != MATCH_YES)
1441 gfc_error ("Syntax error in IF-expression at %C");
1442 gfc_free_expr (expr);
1443 return MATCH_ERROR;
1446 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1448 if (m == MATCH_YES)
1450 if (n == MATCH_YES)
1452 gfc_error ("Block label not appropriate for arithmetic IF "
1453 "statement at %C");
1454 gfc_free_expr (expr);
1455 return MATCH_ERROR;
1458 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1459 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1460 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1462 gfc_free_expr (expr);
1463 return MATCH_ERROR;
1466 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF "
1467 "statement at %C") == FAILURE)
1468 return MATCH_ERROR;
1470 new_st.op = EXEC_ARITHMETIC_IF;
1471 new_st.expr = expr;
1472 new_st.label = l1;
1473 new_st.label2 = l2;
1474 new_st.label3 = l3;
1476 *if_type = ST_ARITHMETIC_IF;
1477 return MATCH_YES;
1480 if (gfc_match (" then%t") == MATCH_YES)
1482 new_st.op = EXEC_IF;
1483 new_st.expr = expr;
1484 *if_type = ST_IF_BLOCK;
1485 return MATCH_YES;
1488 if (n == MATCH_YES)
1490 gfc_error ("Block label is not appropriate for IF statement at %C");
1491 gfc_free_expr (expr);
1492 return MATCH_ERROR;
1495 /* At this point the only thing left is a simple IF statement. At
1496 this point, n has to be MATCH_NO, so we don't have to worry about
1497 re-matching a block label. From what we've got so far, try
1498 matching an assignment. */
1500 *if_type = ST_SIMPLE_IF;
1502 m = gfc_match_assignment ();
1503 if (m == MATCH_YES)
1504 goto got_match;
1506 gfc_free_expr (expr);
1507 gfc_undo_symbols ();
1508 gfc_current_locus = old_loc;
1510 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1511 assignment was found. For MATCH_NO, continue to call the various
1512 matchers. */
1513 if (m == MATCH_ERROR)
1514 return MATCH_ERROR;
1516 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1518 m = gfc_match_pointer_assignment ();
1519 if (m == MATCH_YES)
1520 goto got_match;
1522 gfc_free_expr (expr);
1523 gfc_undo_symbols ();
1524 gfc_current_locus = old_loc;
1526 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1528 /* Look at the next keyword to see which matcher to call. Matching
1529 the keyword doesn't affect the symbol table, so we don't have to
1530 restore between tries. */
1532 #define match(string, subr, statement) \
1533 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1535 gfc_clear_error ();
1537 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1538 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1539 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1540 match ("call", gfc_match_call, ST_CALL)
1541 match ("close", gfc_match_close, ST_CLOSE)
1542 match ("continue", gfc_match_continue, ST_CONTINUE)
1543 match ("cycle", gfc_match_cycle, ST_CYCLE)
1544 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1545 match ("end file", gfc_match_endfile, ST_END_FILE)
1546 match ("exit", gfc_match_exit, ST_EXIT)
1547 match ("flush", gfc_match_flush, ST_FLUSH)
1548 match ("forall", match_simple_forall, ST_FORALL)
1549 match ("go to", gfc_match_goto, ST_GOTO)
1550 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1551 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1552 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1553 match ("open", gfc_match_open, ST_OPEN)
1554 match ("pause", gfc_match_pause, ST_NONE)
1555 match ("print", gfc_match_print, ST_WRITE)
1556 match ("read", gfc_match_read, ST_READ)
1557 match ("return", gfc_match_return, ST_RETURN)
1558 match ("rewind", gfc_match_rewind, ST_REWIND)
1559 match ("stop", gfc_match_stop, ST_STOP)
1560 match ("wait", gfc_match_wait, ST_WAIT)
1561 match ("where", match_simple_where, ST_WHERE)
1562 match ("write", gfc_match_write, ST_WRITE)
1564 /* The gfc_match_assignment() above may have returned a MATCH_NO
1565 where the assignment was to a named constant. Check that
1566 special case here. */
1567 m = gfc_match_assignment ();
1568 if (m == MATCH_NO)
1570 gfc_error ("Cannot assign to a named constant at %C");
1571 gfc_free_expr (expr);
1572 gfc_undo_symbols ();
1573 gfc_current_locus = old_loc;
1574 return MATCH_ERROR;
1577 /* All else has failed, so give up. See if any of the matchers has
1578 stored an error message of some sort. */
1579 if (gfc_error_check () == 0)
1580 gfc_error ("Unclassifiable statement in IF-clause at %C");
1582 gfc_free_expr (expr);
1583 return MATCH_ERROR;
1585 got_match:
1586 if (m == MATCH_NO)
1587 gfc_error ("Syntax error in IF-clause at %C");
1588 if (m != MATCH_YES)
1590 gfc_free_expr (expr);
1591 return MATCH_ERROR;
1594 /* At this point, we've matched the single IF and the action clause
1595 is in new_st. Rearrange things so that the IF statement appears
1596 in new_st. */
1598 p = gfc_get_code ();
1599 p->next = gfc_get_code ();
1600 *p->next = new_st;
1601 p->next->loc = gfc_current_locus;
1603 p->expr = expr;
1604 p->op = EXEC_IF;
1606 gfc_clear_new_st ();
1608 new_st.op = EXEC_IF;
1609 new_st.block = p;
1611 return MATCH_YES;
1614 #undef match
1617 /* Match an ELSE statement. */
1619 match
1620 gfc_match_else (void)
1622 char name[GFC_MAX_SYMBOL_LEN + 1];
1624 if (gfc_match_eos () == MATCH_YES)
1625 return MATCH_YES;
1627 if (gfc_match_name (name) != MATCH_YES
1628 || gfc_current_block () == NULL
1629 || gfc_match_eos () != MATCH_YES)
1631 gfc_error ("Unexpected junk after ELSE statement at %C");
1632 return MATCH_ERROR;
1635 if (strcmp (name, gfc_current_block ()->name) != 0)
1637 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1638 name, gfc_current_block ()->name);
1639 return MATCH_ERROR;
1642 return MATCH_YES;
1646 /* Match an ELSE IF statement. */
1648 match
1649 gfc_match_elseif (void)
1651 char name[GFC_MAX_SYMBOL_LEN + 1];
1652 gfc_expr *expr;
1653 match m;
1655 m = gfc_match (" ( %e ) then", &expr);
1656 if (m != MATCH_YES)
1657 return m;
1659 if (gfc_match_eos () == MATCH_YES)
1660 goto done;
1662 if (gfc_match_name (name) != MATCH_YES
1663 || gfc_current_block () == NULL
1664 || gfc_match_eos () != MATCH_YES)
1666 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1667 goto cleanup;
1670 if (strcmp (name, gfc_current_block ()->name) != 0)
1672 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1673 name, gfc_current_block ()->name);
1674 goto cleanup;
1677 done:
1678 new_st.op = EXEC_IF;
1679 new_st.expr = expr;
1680 return MATCH_YES;
1682 cleanup:
1683 gfc_free_expr (expr);
1684 return MATCH_ERROR;
1688 /* Free a gfc_iterator structure. */
1690 void
1691 gfc_free_iterator (gfc_iterator *iter, int flag)
1694 if (iter == NULL)
1695 return;
1697 gfc_free_expr (iter->var);
1698 gfc_free_expr (iter->start);
1699 gfc_free_expr (iter->end);
1700 gfc_free_expr (iter->step);
1702 if (flag)
1703 gfc_free (iter);
1707 /* Match a DO statement. */
1709 match
1710 gfc_match_do (void)
1712 gfc_iterator iter, *ip;
1713 locus old_loc;
1714 gfc_st_label *label;
1715 match m;
1717 old_loc = gfc_current_locus;
1719 label = NULL;
1720 iter.var = iter.start = iter.end = iter.step = NULL;
1722 m = gfc_match_label ();
1723 if (m == MATCH_ERROR)
1724 return m;
1726 if (gfc_match (" do") != MATCH_YES)
1727 return MATCH_NO;
1729 m = gfc_match_st_label (&label);
1730 if (m == MATCH_ERROR)
1731 goto cleanup;
1733 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1735 if (gfc_match_eos () == MATCH_YES)
1737 iter.end = gfc_logical_expr (1, NULL);
1738 new_st.op = EXEC_DO_WHILE;
1739 goto done;
1742 /* Match an optional comma, if no comma is found, a space is obligatory. */
1743 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1744 return MATCH_NO;
1746 /* Check for balanced parens. */
1748 if (gfc_match_parens () == MATCH_ERROR)
1749 return MATCH_ERROR;
1751 /* See if we have a DO WHILE. */
1752 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1754 new_st.op = EXEC_DO_WHILE;
1755 goto done;
1758 /* The abortive DO WHILE may have done something to the symbol
1759 table, so we start over. */
1760 gfc_undo_symbols ();
1761 gfc_current_locus = old_loc;
1763 gfc_match_label (); /* This won't error. */
1764 gfc_match (" do "); /* This will work. */
1766 gfc_match_st_label (&label); /* Can't error out. */
1767 gfc_match_char (','); /* Optional comma. */
1769 m = gfc_match_iterator (&iter, 0);
1770 if (m == MATCH_NO)
1771 return MATCH_NO;
1772 if (m == MATCH_ERROR)
1773 goto cleanup;
1775 iter.var->symtree->n.sym->attr.implied_index = 0;
1776 gfc_check_do_variable (iter.var->symtree);
1778 if (gfc_match_eos () != MATCH_YES)
1780 gfc_syntax_error (ST_DO);
1781 goto cleanup;
1784 new_st.op = EXEC_DO;
1786 done:
1787 if (label != NULL
1788 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1789 goto cleanup;
1791 new_st.label = label;
1793 if (new_st.op == EXEC_DO_WHILE)
1794 new_st.expr = iter.end;
1795 else
1797 new_st.ext.iterator = ip = gfc_get_iterator ();
1798 *ip = iter;
1801 return MATCH_YES;
1803 cleanup:
1804 gfc_free_iterator (&iter, 0);
1806 return MATCH_ERROR;
1810 /* Match an EXIT or CYCLE statement. */
1812 static match
1813 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1815 gfc_state_data *p, *o;
1816 gfc_symbol *sym;
1817 match m;
1819 if (gfc_match_eos () == MATCH_YES)
1820 sym = NULL;
1821 else
1823 m = gfc_match ("% %s%t", &sym);
1824 if (m == MATCH_ERROR)
1825 return MATCH_ERROR;
1826 if (m == MATCH_NO)
1828 gfc_syntax_error (st);
1829 return MATCH_ERROR;
1832 if (sym->attr.flavor != FL_LABEL)
1834 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1835 sym->name, gfc_ascii_statement (st));
1836 return MATCH_ERROR;
1840 /* Find the loop mentioned specified by the label (or lack of a label). */
1841 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1842 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1843 break;
1844 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1845 o = p;
1847 if (p == NULL)
1849 if (sym == NULL)
1850 gfc_error ("%s statement at %C is not within a loop",
1851 gfc_ascii_statement (st));
1852 else
1853 gfc_error ("%s statement at %C is not within loop '%s'",
1854 gfc_ascii_statement (st), sym->name);
1856 return MATCH_ERROR;
1859 if (o != NULL)
1861 gfc_error ("%s statement at %C leaving OpenMP structured block",
1862 gfc_ascii_statement (st));
1863 return MATCH_ERROR;
1865 else if (st == ST_EXIT
1866 && p->previous != NULL
1867 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1868 && (p->previous->head->op == EXEC_OMP_DO
1869 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1871 gcc_assert (p->previous->head->next != NULL);
1872 gcc_assert (p->previous->head->next->op == EXEC_DO
1873 || p->previous->head->next->op == EXEC_DO_WHILE);
1874 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1875 return MATCH_ERROR;
1878 /* Save the first statement in the loop - needed by the backend. */
1879 new_st.ext.whichloop = p->head;
1881 new_st.op = op;
1883 return MATCH_YES;
1887 /* Match the EXIT statement. */
1889 match
1890 gfc_match_exit (void)
1892 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1896 /* Match the CYCLE statement. */
1898 match
1899 gfc_match_cycle (void)
1901 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1905 /* Match a number or character constant after a STOP or PAUSE statement. */
1907 static match
1908 gfc_match_stopcode (gfc_statement st)
1910 int stop_code;
1911 gfc_expr *e;
1912 match m;
1913 int cnt;
1915 stop_code = -1;
1916 e = NULL;
1918 if (gfc_match_eos () != MATCH_YES)
1920 m = gfc_match_small_literal_int (&stop_code, &cnt);
1921 if (m == MATCH_ERROR)
1922 goto cleanup;
1924 if (m == MATCH_YES && cnt > 5)
1926 gfc_error ("Too many digits in STOP code at %C");
1927 goto cleanup;
1930 if (m == MATCH_NO)
1932 /* Try a character constant. */
1933 m = gfc_match_expr (&e);
1934 if (m == MATCH_ERROR)
1935 goto cleanup;
1936 if (m == MATCH_NO)
1937 goto syntax;
1938 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1939 goto syntax;
1942 if (gfc_match_eos () != MATCH_YES)
1943 goto syntax;
1946 if (gfc_pure (NULL))
1948 gfc_error ("%s statement not allowed in PURE procedure at %C",
1949 gfc_ascii_statement (st));
1950 goto cleanup;
1953 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1954 new_st.expr = e;
1955 new_st.ext.stop_code = stop_code;
1957 return MATCH_YES;
1959 syntax:
1960 gfc_syntax_error (st);
1962 cleanup:
1964 gfc_free_expr (e);
1965 return MATCH_ERROR;
1969 /* Match the (deprecated) PAUSE statement. */
1971 match
1972 gfc_match_pause (void)
1974 match m;
1976 m = gfc_match_stopcode (ST_PAUSE);
1977 if (m == MATCH_YES)
1979 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
1980 " at %C")
1981 == FAILURE)
1982 m = MATCH_ERROR;
1984 return m;
1988 /* Match the STOP statement. */
1990 match
1991 gfc_match_stop (void)
1993 return gfc_match_stopcode (ST_STOP);
1997 /* Match a CONTINUE statement. */
1999 match
2000 gfc_match_continue (void)
2002 if (gfc_match_eos () != MATCH_YES)
2004 gfc_syntax_error (ST_CONTINUE);
2005 return MATCH_ERROR;
2008 new_st.op = EXEC_CONTINUE;
2009 return MATCH_YES;
2013 /* Match the (deprecated) ASSIGN statement. */
2015 match
2016 gfc_match_assign (void)
2018 gfc_expr *expr;
2019 gfc_st_label *label;
2021 if (gfc_match (" %l", &label) == MATCH_YES)
2023 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
2024 return MATCH_ERROR;
2025 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
2027 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
2028 "statement at %C")
2029 == FAILURE)
2030 return MATCH_ERROR;
2032 expr->symtree->n.sym->attr.assign = 1;
2034 new_st.op = EXEC_LABEL_ASSIGN;
2035 new_st.label = label;
2036 new_st.expr = expr;
2037 return MATCH_YES;
2040 return MATCH_NO;
2044 /* Match the GO TO statement. As a computed GOTO statement is
2045 matched, it is transformed into an equivalent SELECT block. No
2046 tree is necessary, and the resulting jumps-to-jumps are
2047 specifically optimized away by the back end. */
2049 match
2050 gfc_match_goto (void)
2052 gfc_code *head, *tail;
2053 gfc_expr *expr;
2054 gfc_case *cp;
2055 gfc_st_label *label;
2056 int i;
2057 match m;
2059 if (gfc_match (" %l%t", &label) == MATCH_YES)
2061 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2062 return MATCH_ERROR;
2064 new_st.op = EXEC_GOTO;
2065 new_st.label = label;
2066 return MATCH_YES;
2069 /* The assigned GO TO statement. */
2071 if (gfc_match_variable (&expr, 0) == MATCH_YES)
2073 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
2074 "statement at %C")
2075 == FAILURE)
2076 return MATCH_ERROR;
2078 new_st.op = EXEC_GOTO;
2079 new_st.expr = expr;
2081 if (gfc_match_eos () == MATCH_YES)
2082 return MATCH_YES;
2084 /* Match label list. */
2085 gfc_match_char (',');
2086 if (gfc_match_char ('(') != MATCH_YES)
2088 gfc_syntax_error (ST_GOTO);
2089 return MATCH_ERROR;
2091 head = tail = NULL;
2095 m = gfc_match_st_label (&label);
2096 if (m != MATCH_YES)
2097 goto syntax;
2099 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2100 goto cleanup;
2102 if (head == NULL)
2103 head = tail = gfc_get_code ();
2104 else
2106 tail->block = gfc_get_code ();
2107 tail = tail->block;
2110 tail->label = label;
2111 tail->op = EXEC_GOTO;
2113 while (gfc_match_char (',') == MATCH_YES);
2115 if (gfc_match (")%t") != MATCH_YES)
2116 goto syntax;
2118 if (head == NULL)
2120 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2121 goto syntax;
2123 new_st.block = head;
2125 return MATCH_YES;
2128 /* Last chance is a computed GO TO statement. */
2129 if (gfc_match_char ('(') != MATCH_YES)
2131 gfc_syntax_error (ST_GOTO);
2132 return MATCH_ERROR;
2135 head = tail = NULL;
2136 i = 1;
2140 m = gfc_match_st_label (&label);
2141 if (m != MATCH_YES)
2142 goto syntax;
2144 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2145 goto cleanup;
2147 if (head == NULL)
2148 head = tail = gfc_get_code ();
2149 else
2151 tail->block = gfc_get_code ();
2152 tail = tail->block;
2155 cp = gfc_get_case ();
2156 cp->low = cp->high = gfc_int_expr (i++);
2158 tail->op = EXEC_SELECT;
2159 tail->ext.case_list = cp;
2161 tail->next = gfc_get_code ();
2162 tail->next->op = EXEC_GOTO;
2163 tail->next->label = label;
2165 while (gfc_match_char (',') == MATCH_YES);
2167 if (gfc_match_char (')') != MATCH_YES)
2168 goto syntax;
2170 if (head == NULL)
2172 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2173 goto syntax;
2176 /* Get the rest of the statement. */
2177 gfc_match_char (',');
2179 if (gfc_match (" %e%t", &expr) != MATCH_YES)
2180 goto syntax;
2182 /* At this point, a computed GOTO has been fully matched and an
2183 equivalent SELECT statement constructed. */
2185 new_st.op = EXEC_SELECT;
2186 new_st.expr = NULL;
2188 /* Hack: For a "real" SELECT, the expression is in expr. We put
2189 it in expr2 so we can distinguish then and produce the correct
2190 diagnostics. */
2191 new_st.expr2 = expr;
2192 new_st.block = head;
2193 return MATCH_YES;
2195 syntax:
2196 gfc_syntax_error (ST_GOTO);
2197 cleanup:
2198 gfc_free_statements (head);
2199 return MATCH_ERROR;
2203 /* Frees a list of gfc_alloc structures. */
2205 void
2206 gfc_free_alloc_list (gfc_alloc *p)
2208 gfc_alloc *q;
2210 for (; p; p = q)
2212 q = p->next;
2213 gfc_free_expr (p->expr);
2214 gfc_free (p);
2219 /* Match an ALLOCATE statement. */
2221 match
2222 gfc_match_allocate (void)
2224 gfc_alloc *head, *tail;
2225 gfc_expr *stat;
2226 match m;
2228 head = tail = NULL;
2229 stat = NULL;
2231 if (gfc_match_char ('(') != MATCH_YES)
2232 goto syntax;
2234 for (;;)
2236 if (head == NULL)
2237 head = tail = gfc_get_alloc ();
2238 else
2240 tail->next = gfc_get_alloc ();
2241 tail = tail->next;
2244 m = gfc_match_variable (&tail->expr, 0);
2245 if (m == MATCH_NO)
2246 goto syntax;
2247 if (m == MATCH_ERROR)
2248 goto cleanup;
2250 if (gfc_check_do_variable (tail->expr->symtree))
2251 goto cleanup;
2253 if (gfc_pure (NULL)
2254 && gfc_impure_variable (tail->expr->symtree->n.sym))
2256 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
2257 "PURE procedure");
2258 goto cleanup;
2261 if (tail->expr->ts.type == BT_DERIVED)
2262 tail->expr->ts.derived = gfc_use_derived (tail->expr->ts.derived);
2264 if (gfc_match_char (',') != MATCH_YES)
2265 break;
2267 m = gfc_match (" stat = %v", &stat);
2268 if (m == MATCH_ERROR)
2269 goto cleanup;
2270 if (m == MATCH_YES)
2271 break;
2274 if (stat != NULL)
2275 gfc_check_do_variable(stat->symtree);
2277 if (gfc_match (" )%t") != MATCH_YES)
2278 goto syntax;
2280 new_st.op = EXEC_ALLOCATE;
2281 new_st.expr = stat;
2282 new_st.ext.alloc_list = head;
2284 return MATCH_YES;
2286 syntax:
2287 gfc_syntax_error (ST_ALLOCATE);
2289 cleanup:
2290 gfc_free_expr (stat);
2291 gfc_free_alloc_list (head);
2292 return MATCH_ERROR;
2296 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
2297 a set of pointer assignments to intrinsic NULL(). */
2299 match
2300 gfc_match_nullify (void)
2302 gfc_code *tail;
2303 gfc_expr *e, *p;
2304 match m;
2306 tail = NULL;
2308 if (gfc_match_char ('(') != MATCH_YES)
2309 goto syntax;
2311 for (;;)
2313 m = gfc_match_variable (&p, 0);
2314 if (m == MATCH_ERROR)
2315 goto cleanup;
2316 if (m == MATCH_NO)
2317 goto syntax;
2319 if (gfc_check_do_variable (p->symtree))
2320 goto cleanup;
2322 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
2324 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
2325 goto cleanup;
2328 /* build ' => NULL() '. */
2329 e = gfc_get_expr ();
2330 e->where = gfc_current_locus;
2331 e->expr_type = EXPR_NULL;
2332 e->ts.type = BT_UNKNOWN;
2334 /* Chain to list. */
2335 if (tail == NULL)
2336 tail = &new_st;
2337 else
2339 tail->next = gfc_get_code ();
2340 tail = tail->next;
2343 tail->op = EXEC_POINTER_ASSIGN;
2344 tail->expr = p;
2345 tail->expr2 = e;
2347 if (gfc_match (" )%t") == MATCH_YES)
2348 break;
2349 if (gfc_match_char (',') != MATCH_YES)
2350 goto syntax;
2353 return MATCH_YES;
2355 syntax:
2356 gfc_syntax_error (ST_NULLIFY);
2358 cleanup:
2359 gfc_free_statements (new_st.next);
2360 return MATCH_ERROR;
2364 /* Match a DEALLOCATE statement. */
2366 match
2367 gfc_match_deallocate (void)
2369 gfc_alloc *head, *tail;
2370 gfc_expr *stat;
2371 match m;
2373 head = tail = NULL;
2374 stat = NULL;
2376 if (gfc_match_char ('(') != MATCH_YES)
2377 goto syntax;
2379 for (;;)
2381 if (head == NULL)
2382 head = tail = gfc_get_alloc ();
2383 else
2385 tail->next = gfc_get_alloc ();
2386 tail = tail->next;
2389 m = gfc_match_variable (&tail->expr, 0);
2390 if (m == MATCH_ERROR)
2391 goto cleanup;
2392 if (m == MATCH_NO)
2393 goto syntax;
2395 if (gfc_check_do_variable (tail->expr->symtree))
2396 goto cleanup;
2398 if (gfc_pure (NULL)
2399 && gfc_impure_variable (tail->expr->symtree->n.sym))
2401 gfc_error ("Illegal deallocate-expression in DEALLOCATE at %C "
2402 "for a PURE procedure");
2403 goto cleanup;
2406 if (gfc_match_char (',') != MATCH_YES)
2407 break;
2409 m = gfc_match (" stat = %v", &stat);
2410 if (m == MATCH_ERROR)
2411 goto cleanup;
2412 if (m == MATCH_YES)
2413 break;
2416 if (stat != NULL)
2417 gfc_check_do_variable(stat->symtree);
2419 if (gfc_match (" )%t") != MATCH_YES)
2420 goto syntax;
2422 new_st.op = EXEC_DEALLOCATE;
2423 new_st.expr = stat;
2424 new_st.ext.alloc_list = head;
2426 return MATCH_YES;
2428 syntax:
2429 gfc_syntax_error (ST_DEALLOCATE);
2431 cleanup:
2432 gfc_free_expr (stat);
2433 gfc_free_alloc_list (head);
2434 return MATCH_ERROR;
2438 /* Match a RETURN statement. */
2440 match
2441 gfc_match_return (void)
2443 gfc_expr *e;
2444 match m;
2445 gfc_compile_state s;
2447 e = NULL;
2448 if (gfc_match_eos () == MATCH_YES)
2449 goto done;
2451 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2453 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2454 "a SUBROUTINE");
2455 goto cleanup;
2458 if (gfc_current_form == FORM_FREE)
2460 /* The following are valid, so we can't require a blank after the
2461 RETURN keyword:
2462 return+1
2463 return(1) */
2464 char c = gfc_peek_ascii_char ();
2465 if (ISALPHA (c) || ISDIGIT (c))
2466 return MATCH_NO;
2469 m = gfc_match (" %e%t", &e);
2470 if (m == MATCH_YES)
2471 goto done;
2472 if (m == MATCH_ERROR)
2473 goto cleanup;
2475 gfc_syntax_error (ST_RETURN);
2477 cleanup:
2478 gfc_free_expr (e);
2479 return MATCH_ERROR;
2481 done:
2482 gfc_enclosing_unit (&s);
2483 if (s == COMP_PROGRAM
2484 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2485 "main program at %C") == FAILURE)
2486 return MATCH_ERROR;
2488 new_st.op = EXEC_RETURN;
2489 new_st.expr = e;
2491 return MATCH_YES;
2495 /* Match the call of a type-bound procedure, if CALL%var has already been
2496 matched and var found to be a derived-type variable. */
2498 static match
2499 match_typebound_call (gfc_symtree* varst)
2501 gfc_symbol* var;
2502 gfc_expr* base;
2503 match m;
2505 var = varst->n.sym;
2507 base = gfc_get_expr ();
2508 base->expr_type = EXPR_VARIABLE;
2509 base->symtree = varst;
2510 base->where = gfc_current_locus;
2511 gfc_set_sym_referenced (varst->n.sym);
2513 m = gfc_match_varspec (base, 0, true);
2514 if (m == MATCH_NO)
2515 gfc_error ("Expected component reference at %C");
2516 if (m != MATCH_YES)
2517 return MATCH_ERROR;
2519 if (gfc_match_eos () != MATCH_YES)
2521 gfc_error ("Junk after CALL at %C");
2522 return MATCH_ERROR;
2525 if (base->expr_type != EXPR_COMPCALL)
2527 gfc_error ("Expected type-bound procedure reference at %C");
2528 return MATCH_ERROR;
2531 new_st.op = EXEC_COMPCALL;
2532 new_st.expr = base;
2534 return MATCH_YES;
2538 /* Match a CALL statement. The tricky part here are possible
2539 alternate return specifiers. We handle these by having all
2540 "subroutines" actually return an integer via a register that gives
2541 the return number. If the call specifies alternate returns, we
2542 generate code for a SELECT statement whose case clauses contain
2543 GOTOs to the various labels. */
2545 match
2546 gfc_match_call (void)
2548 char name[GFC_MAX_SYMBOL_LEN + 1];
2549 gfc_actual_arglist *a, *arglist;
2550 gfc_case *new_case;
2551 gfc_symbol *sym;
2552 gfc_symtree *st;
2553 gfc_code *c;
2554 match m;
2555 int i;
2557 arglist = NULL;
2559 m = gfc_match ("% %n", name);
2560 if (m == MATCH_NO)
2561 goto syntax;
2562 if (m != MATCH_YES)
2563 return m;
2565 if (gfc_get_ha_sym_tree (name, &st))
2566 return MATCH_ERROR;
2568 sym = st->n.sym;
2570 /* If this is a variable of derived-type, it probably starts a type-bound
2571 procedure call. */
2572 if (sym->attr.flavor != FL_PROCEDURE && sym->ts.type == BT_DERIVED)
2573 return match_typebound_call (st);
2575 /* If it does not seem to be callable (include functions so that the
2576 right association is made. They are thrown out in resolution.)
2577 ... */
2578 if (!sym->attr.generic
2579 && !sym->attr.subroutine
2580 && !sym->attr.function)
2582 if (!(sym->attr.external && !sym->attr.referenced))
2584 /* ...create a symbol in this scope... */
2585 if (sym->ns != gfc_current_ns
2586 && gfc_get_sym_tree (name, NULL, &st) == 1)
2587 return MATCH_ERROR;
2589 if (sym != st->n.sym)
2590 sym = st->n.sym;
2593 /* ...and then to try to make the symbol into a subroutine. */
2594 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2595 return MATCH_ERROR;
2598 gfc_set_sym_referenced (sym);
2600 if (gfc_match_eos () != MATCH_YES)
2602 m = gfc_match_actual_arglist (1, &arglist);
2603 if (m == MATCH_NO)
2604 goto syntax;
2605 if (m == MATCH_ERROR)
2606 goto cleanup;
2608 if (gfc_match_eos () != MATCH_YES)
2609 goto syntax;
2612 /* If any alternate return labels were found, construct a SELECT
2613 statement that will jump to the right place. */
2615 i = 0;
2616 for (a = arglist; a; a = a->next)
2617 if (a->expr == NULL)
2618 i = 1;
2620 if (i)
2622 gfc_symtree *select_st;
2623 gfc_symbol *select_sym;
2624 char name[GFC_MAX_SYMBOL_LEN + 1];
2626 new_st.next = c = gfc_get_code ();
2627 c->op = EXEC_SELECT;
2628 sprintf (name, "_result_%s", sym->name);
2629 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
2631 select_sym = select_st->n.sym;
2632 select_sym->ts.type = BT_INTEGER;
2633 select_sym->ts.kind = gfc_default_integer_kind;
2634 gfc_set_sym_referenced (select_sym);
2635 c->expr = gfc_get_expr ();
2636 c->expr->expr_type = EXPR_VARIABLE;
2637 c->expr->symtree = select_st;
2638 c->expr->ts = select_sym->ts;
2639 c->expr->where = gfc_current_locus;
2641 i = 0;
2642 for (a = arglist; a; a = a->next)
2644 if (a->expr != NULL)
2645 continue;
2647 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2648 continue;
2650 i++;
2652 c->block = gfc_get_code ();
2653 c = c->block;
2654 c->op = EXEC_SELECT;
2656 new_case = gfc_get_case ();
2657 new_case->high = new_case->low = gfc_int_expr (i);
2658 c->ext.case_list = new_case;
2660 c->next = gfc_get_code ();
2661 c->next->op = EXEC_GOTO;
2662 c->next->label = a->label;
2666 new_st.op = EXEC_CALL;
2667 new_st.symtree = st;
2668 new_st.ext.actual = arglist;
2670 return MATCH_YES;
2672 syntax:
2673 gfc_syntax_error (ST_CALL);
2675 cleanup:
2676 gfc_free_actual_arglist (arglist);
2677 return MATCH_ERROR;
2681 /* Given a name, return a pointer to the common head structure,
2682 creating it if it does not exist. If FROM_MODULE is nonzero, we
2683 mangle the name so that it doesn't interfere with commons defined
2684 in the using namespace.
2685 TODO: Add to global symbol tree. */
2687 gfc_common_head *
2688 gfc_get_common (const char *name, int from_module)
2690 gfc_symtree *st;
2691 static int serial = 0;
2692 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
2694 if (from_module)
2696 /* A use associated common block is only needed to correctly layout
2697 the variables it contains. */
2698 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2699 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2701 else
2703 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2705 if (st == NULL)
2706 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2709 if (st->n.common == NULL)
2711 st->n.common = gfc_get_common_head ();
2712 st->n.common->where = gfc_current_locus;
2713 strcpy (st->n.common->name, name);
2716 return st->n.common;
2720 /* Match a common block name. */
2722 match match_common_name (char *name)
2724 match m;
2726 if (gfc_match_char ('/') == MATCH_NO)
2728 name[0] = '\0';
2729 return MATCH_YES;
2732 if (gfc_match_char ('/') == MATCH_YES)
2734 name[0] = '\0';
2735 return MATCH_YES;
2738 m = gfc_match_name (name);
2740 if (m == MATCH_ERROR)
2741 return MATCH_ERROR;
2742 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2743 return MATCH_YES;
2745 gfc_error ("Syntax error in common block name at %C");
2746 return MATCH_ERROR;
2750 /* Match a COMMON statement. */
2752 match
2753 gfc_match_common (void)
2755 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2756 char name[GFC_MAX_SYMBOL_LEN + 1];
2757 gfc_common_head *t;
2758 gfc_array_spec *as;
2759 gfc_equiv *e1, *e2;
2760 match m;
2761 gfc_gsymbol *gsym;
2763 old_blank_common = gfc_current_ns->blank_common.head;
2764 if (old_blank_common)
2766 while (old_blank_common->common_next)
2767 old_blank_common = old_blank_common->common_next;
2770 as = NULL;
2772 for (;;)
2774 m = match_common_name (name);
2775 if (m == MATCH_ERROR)
2776 goto cleanup;
2778 gsym = gfc_get_gsymbol (name);
2779 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2781 gfc_error ("Symbol '%s' at %C is already an external symbol that "
2782 "is not COMMON", name);
2783 goto cleanup;
2786 if (gsym->type == GSYM_UNKNOWN)
2788 gsym->type = GSYM_COMMON;
2789 gsym->where = gfc_current_locus;
2790 gsym->defined = 1;
2793 gsym->used = 1;
2795 if (name[0] == '\0')
2797 t = &gfc_current_ns->blank_common;
2798 if (t->head == NULL)
2799 t->where = gfc_current_locus;
2801 else
2803 t = gfc_get_common (name, 0);
2805 head = &t->head;
2807 if (*head == NULL)
2808 tail = NULL;
2809 else
2811 tail = *head;
2812 while (tail->common_next)
2813 tail = tail->common_next;
2816 /* Grab the list of symbols. */
2817 for (;;)
2819 m = gfc_match_symbol (&sym, 0);
2820 if (m == MATCH_ERROR)
2821 goto cleanup;
2822 if (m == MATCH_NO)
2823 goto syntax;
2825 /* Store a ref to the common block for error checking. */
2826 sym->common_block = t;
2828 /* See if we know the current common block is bind(c), and if
2829 so, then see if we can check if the symbol is (which it'll
2830 need to be). This can happen if the bind(c) attr stmt was
2831 applied to the common block, and the variable(s) already
2832 defined, before declaring the common block. */
2833 if (t->is_bind_c == 1)
2835 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
2837 /* If we find an error, just print it and continue,
2838 cause it's just semantic, and we can see if there
2839 are more errors. */
2840 gfc_error_now ("Variable '%s' at %L in common block '%s' "
2841 "at %C must be declared with a C "
2842 "interoperable kind since common block "
2843 "'%s' is bind(c)",
2844 sym->name, &(sym->declared_at), t->name,
2845 t->name);
2848 if (sym->attr.is_bind_c == 1)
2849 gfc_error_now ("Variable '%s' in common block "
2850 "'%s' at %C can not be bind(c) since "
2851 "it is not global", sym->name, t->name);
2854 if (sym->attr.in_common)
2856 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2857 sym->name);
2858 goto cleanup;
2861 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
2862 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
2864 if (gfc_notify_std (GFC_STD_GNU, "Initialized symbol '%s' at %C "
2865 "can only be COMMON in "
2866 "BLOCK DATA", sym->name)
2867 == FAILURE)
2868 goto cleanup;
2871 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2872 goto cleanup;
2874 if (tail != NULL)
2875 tail->common_next = sym;
2876 else
2877 *head = sym;
2879 tail = sym;
2881 /* Deal with an optional array specification after the
2882 symbol name. */
2883 m = gfc_match_array_spec (&as);
2884 if (m == MATCH_ERROR)
2885 goto cleanup;
2887 if (m == MATCH_YES)
2889 if (as->type != AS_EXPLICIT)
2891 gfc_error ("Array specification for symbol '%s' in COMMON "
2892 "at %C must be explicit", sym->name);
2893 goto cleanup;
2896 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2897 goto cleanup;
2899 if (sym->attr.pointer)
2901 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
2902 "POINTER array", sym->name);
2903 goto cleanup;
2906 sym->as = as;
2907 as = NULL;
2911 sym->common_head = t;
2913 /* Check to see if the symbol is already in an equivalence group.
2914 If it is, set the other members as being in common. */
2915 if (sym->attr.in_equivalence)
2917 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2919 for (e2 = e1; e2; e2 = e2->eq)
2920 if (e2->expr->symtree->n.sym == sym)
2921 goto equiv_found;
2923 continue;
2925 equiv_found:
2927 for (e2 = e1; e2; e2 = e2->eq)
2929 other = e2->expr->symtree->n.sym;
2930 if (other->common_head
2931 && other->common_head != sym->common_head)
2933 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2934 "%C is being indirectly equivalenced to "
2935 "another COMMON block '%s'",
2936 sym->name, sym->common_head->name,
2937 other->common_head->name);
2938 goto cleanup;
2940 other->attr.in_common = 1;
2941 other->common_head = t;
2947 gfc_gobble_whitespace ();
2948 if (gfc_match_eos () == MATCH_YES)
2949 goto done;
2950 if (gfc_peek_ascii_char () == '/')
2951 break;
2952 if (gfc_match_char (',') != MATCH_YES)
2953 goto syntax;
2954 gfc_gobble_whitespace ();
2955 if (gfc_peek_ascii_char () == '/')
2956 break;
2960 done:
2961 return MATCH_YES;
2963 syntax:
2964 gfc_syntax_error (ST_COMMON);
2966 cleanup:
2967 if (old_blank_common)
2968 old_blank_common->common_next = NULL;
2969 else
2970 gfc_current_ns->blank_common.head = NULL;
2971 gfc_free_array_spec (as);
2972 return MATCH_ERROR;
2976 /* Match a BLOCK DATA program unit. */
2978 match
2979 gfc_match_block_data (void)
2981 char name[GFC_MAX_SYMBOL_LEN + 1];
2982 gfc_symbol *sym;
2983 match m;
2985 if (gfc_match_eos () == MATCH_YES)
2987 gfc_new_block = NULL;
2988 return MATCH_YES;
2991 m = gfc_match ("% %n%t", name);
2992 if (m != MATCH_YES)
2993 return MATCH_ERROR;
2995 if (gfc_get_symbol (name, NULL, &sym))
2996 return MATCH_ERROR;
2998 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2999 return MATCH_ERROR;
3001 gfc_new_block = sym;
3003 return MATCH_YES;
3007 /* Free a namelist structure. */
3009 void
3010 gfc_free_namelist (gfc_namelist *name)
3012 gfc_namelist *n;
3014 for (; name; name = n)
3016 n = name->next;
3017 gfc_free (name);
3022 /* Match a NAMELIST statement. */
3024 match
3025 gfc_match_namelist (void)
3027 gfc_symbol *group_name, *sym;
3028 gfc_namelist *nl;
3029 match m, m2;
3031 m = gfc_match (" / %s /", &group_name);
3032 if (m == MATCH_NO)
3033 goto syntax;
3034 if (m == MATCH_ERROR)
3035 goto error;
3037 for (;;)
3039 if (group_name->ts.type != BT_UNKNOWN)
3041 gfc_error ("Namelist group name '%s' at %C already has a basic "
3042 "type of %s", group_name->name,
3043 gfc_typename (&group_name->ts));
3044 return MATCH_ERROR;
3047 if (group_name->attr.flavor == FL_NAMELIST
3048 && group_name->attr.use_assoc
3049 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
3050 "at %C already is USE associated and can"
3051 "not be respecified.", group_name->name)
3052 == FAILURE)
3053 return MATCH_ERROR;
3055 if (group_name->attr.flavor != FL_NAMELIST
3056 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
3057 group_name->name, NULL) == FAILURE)
3058 return MATCH_ERROR;
3060 for (;;)
3062 m = gfc_match_symbol (&sym, 1);
3063 if (m == MATCH_NO)
3064 goto syntax;
3065 if (m == MATCH_ERROR)
3066 goto error;
3068 if (sym->attr.in_namelist == 0
3069 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
3070 goto error;
3072 /* Use gfc_error_check here, rather than goto error, so that
3073 these are the only errors for the next two lines. */
3074 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
3076 gfc_error ("Assumed size array '%s' in namelist '%s' at "
3077 "%C is not allowed", sym->name, group_name->name);
3078 gfc_error_check ();
3081 if (sym->ts.type == BT_CHARACTER && sym->ts.cl->length == NULL)
3083 gfc_error ("Assumed character length '%s' in namelist '%s' at "
3084 "%C is not allowed", sym->name, group_name->name);
3085 gfc_error_check ();
3088 nl = gfc_get_namelist ();
3089 nl->sym = sym;
3090 sym->refs++;
3092 if (group_name->namelist == NULL)
3093 group_name->namelist = group_name->namelist_tail = nl;
3094 else
3096 group_name->namelist_tail->next = nl;
3097 group_name->namelist_tail = nl;
3100 if (gfc_match_eos () == MATCH_YES)
3101 goto done;
3103 m = gfc_match_char (',');
3105 if (gfc_match_char ('/') == MATCH_YES)
3107 m2 = gfc_match (" %s /", &group_name);
3108 if (m2 == MATCH_YES)
3109 break;
3110 if (m2 == MATCH_ERROR)
3111 goto error;
3112 goto syntax;
3115 if (m != MATCH_YES)
3116 goto syntax;
3120 done:
3121 return MATCH_YES;
3123 syntax:
3124 gfc_syntax_error (ST_NAMELIST);
3126 error:
3127 return MATCH_ERROR;
3131 /* Match a MODULE statement. */
3133 match
3134 gfc_match_module (void)
3136 match m;
3138 m = gfc_match (" %s%t", &gfc_new_block);
3139 if (m != MATCH_YES)
3140 return m;
3142 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
3143 gfc_new_block->name, NULL) == FAILURE)
3144 return MATCH_ERROR;
3146 return MATCH_YES;
3150 /* Free equivalence sets and lists. Recursively is the easiest way to
3151 do this. */
3153 void
3154 gfc_free_equiv (gfc_equiv *eq)
3156 if (eq == NULL)
3157 return;
3159 gfc_free_equiv (eq->eq);
3160 gfc_free_equiv (eq->next);
3161 gfc_free_expr (eq->expr);
3162 gfc_free (eq);
3166 /* Match an EQUIVALENCE statement. */
3168 match
3169 gfc_match_equivalence (void)
3171 gfc_equiv *eq, *set, *tail;
3172 gfc_ref *ref;
3173 gfc_symbol *sym;
3174 match m;
3175 gfc_common_head *common_head = NULL;
3176 bool common_flag;
3177 int cnt;
3179 tail = NULL;
3181 for (;;)
3183 eq = gfc_get_equiv ();
3184 if (tail == NULL)
3185 tail = eq;
3187 eq->next = gfc_current_ns->equiv;
3188 gfc_current_ns->equiv = eq;
3190 if (gfc_match_char ('(') != MATCH_YES)
3191 goto syntax;
3193 set = eq;
3194 common_flag = FALSE;
3195 cnt = 0;
3197 for (;;)
3199 m = gfc_match_equiv_variable (&set->expr);
3200 if (m == MATCH_ERROR)
3201 goto cleanup;
3202 if (m == MATCH_NO)
3203 goto syntax;
3205 /* count the number of objects. */
3206 cnt++;
3208 if (gfc_match_char ('%') == MATCH_YES)
3210 gfc_error ("Derived type component %C is not a "
3211 "permitted EQUIVALENCE member");
3212 goto cleanup;
3215 for (ref = set->expr->ref; ref; ref = ref->next)
3216 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3218 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
3219 "be an array section");
3220 goto cleanup;
3223 sym = set->expr->symtree->n.sym;
3225 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
3226 goto cleanup;
3228 if (sym->attr.in_common)
3230 common_flag = TRUE;
3231 common_head = sym->common_head;
3234 if (gfc_match_char (')') == MATCH_YES)
3235 break;
3237 if (gfc_match_char (',') != MATCH_YES)
3238 goto syntax;
3240 set->eq = gfc_get_equiv ();
3241 set = set->eq;
3244 if (cnt < 2)
3246 gfc_error ("EQUIVALENCE at %C requires two or more objects");
3247 goto cleanup;
3250 /* If one of the members of an equivalence is in common, then
3251 mark them all as being in common. Before doing this, check
3252 that members of the equivalence group are not in different
3253 common blocks. */
3254 if (common_flag)
3255 for (set = eq; set; set = set->eq)
3257 sym = set->expr->symtree->n.sym;
3258 if (sym->common_head && sym->common_head != common_head)
3260 gfc_error ("Attempt to indirectly overlap COMMON "
3261 "blocks %s and %s by EQUIVALENCE at %C",
3262 sym->common_head->name, common_head->name);
3263 goto cleanup;
3265 sym->attr.in_common = 1;
3266 sym->common_head = common_head;
3269 if (gfc_match_eos () == MATCH_YES)
3270 break;
3271 if (gfc_match_char (',') != MATCH_YES)
3272 goto syntax;
3275 return MATCH_YES;
3277 syntax:
3278 gfc_syntax_error (ST_EQUIVALENCE);
3280 cleanup:
3281 eq = tail->next;
3282 tail->next = NULL;
3284 gfc_free_equiv (gfc_current_ns->equiv);
3285 gfc_current_ns->equiv = eq;
3287 return MATCH_ERROR;
3291 /* Check that a statement function is not recursive. This is done by looking
3292 for the statement function symbol(sym) by looking recursively through its
3293 expression(e). If a reference to sym is found, true is returned.
3294 12.5.4 requires that any variable of function that is implicitly typed
3295 shall have that type confirmed by any subsequent type declaration. The
3296 implicit typing is conveniently done here. */
3297 static bool
3298 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
3300 static bool
3301 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
3304 if (e == NULL)
3305 return false;
3307 switch (e->expr_type)
3309 case EXPR_FUNCTION:
3310 if (e->symtree == NULL)
3311 return false;
3313 /* Check the name before testing for nested recursion! */
3314 if (sym->name == e->symtree->n.sym->name)
3315 return true;
3317 /* Catch recursion via other statement functions. */
3318 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
3319 && e->symtree->n.sym->value
3320 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
3321 return true;
3323 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3324 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3326 break;
3328 case EXPR_VARIABLE:
3329 if (e->symtree && sym->name == e->symtree->n.sym->name)
3330 return true;
3332 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3333 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3334 break;
3336 default:
3337 break;
3340 return false;
3344 static bool
3345 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
3347 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
3351 /* Match a statement function declaration. It is so easy to match
3352 non-statement function statements with a MATCH_ERROR as opposed to
3353 MATCH_NO that we suppress error message in most cases. */
3355 match
3356 gfc_match_st_function (void)
3358 gfc_error_buf old_error;
3359 gfc_symbol *sym;
3360 gfc_expr *expr;
3361 match m;
3363 m = gfc_match_symbol (&sym, 0);
3364 if (m != MATCH_YES)
3365 return m;
3367 gfc_push_error (&old_error);
3369 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
3370 sym->name, NULL) == FAILURE)
3371 goto undo_error;
3373 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
3374 goto undo_error;
3376 m = gfc_match (" = %e%t", &expr);
3377 if (m == MATCH_NO)
3378 goto undo_error;
3380 gfc_free_error (&old_error);
3381 if (m == MATCH_ERROR)
3382 return m;
3384 if (recursive_stmt_fcn (expr, sym))
3386 gfc_error ("Statement function at %L is recursive", &expr->where);
3387 return MATCH_ERROR;
3390 sym->value = expr;
3392 return MATCH_YES;
3394 undo_error:
3395 gfc_pop_error (&old_error);
3396 return MATCH_NO;
3400 /***************** SELECT CASE subroutines ******************/
3402 /* Free a single case structure. */
3404 static void
3405 free_case (gfc_case *p)
3407 if (p->low == p->high)
3408 p->high = NULL;
3409 gfc_free_expr (p->low);
3410 gfc_free_expr (p->high);
3411 gfc_free (p);
3415 /* Free a list of case structures. */
3417 void
3418 gfc_free_case_list (gfc_case *p)
3420 gfc_case *q;
3422 for (; p; p = q)
3424 q = p->next;
3425 free_case (p);
3430 /* Match a single case selector. */
3432 static match
3433 match_case_selector (gfc_case **cp)
3435 gfc_case *c;
3436 match m;
3438 c = gfc_get_case ();
3439 c->where = gfc_current_locus;
3441 if (gfc_match_char (':') == MATCH_YES)
3443 m = gfc_match_init_expr (&c->high);
3444 if (m == MATCH_NO)
3445 goto need_expr;
3446 if (m == MATCH_ERROR)
3447 goto cleanup;
3449 else
3451 m = gfc_match_init_expr (&c->low);
3452 if (m == MATCH_ERROR)
3453 goto cleanup;
3454 if (m == MATCH_NO)
3455 goto need_expr;
3457 /* If we're not looking at a ':' now, make a range out of a single
3458 target. Else get the upper bound for the case range. */
3459 if (gfc_match_char (':') != MATCH_YES)
3460 c->high = c->low;
3461 else
3463 m = gfc_match_init_expr (&c->high);
3464 if (m == MATCH_ERROR)
3465 goto cleanup;
3466 /* MATCH_NO is fine. It's OK if nothing is there! */
3470 *cp = c;
3471 return MATCH_YES;
3473 need_expr:
3474 gfc_error ("Expected initialization expression in CASE at %C");
3476 cleanup:
3477 free_case (c);
3478 return MATCH_ERROR;
3482 /* Match the end of a case statement. */
3484 static match
3485 match_case_eos (void)
3487 char name[GFC_MAX_SYMBOL_LEN + 1];
3488 match m;
3490 if (gfc_match_eos () == MATCH_YES)
3491 return MATCH_YES;
3493 /* If the case construct doesn't have a case-construct-name, we
3494 should have matched the EOS. */
3495 if (!gfc_current_block ())
3497 gfc_error ("Expected the name of the SELECT CASE construct at %C");
3498 return MATCH_ERROR;
3501 gfc_gobble_whitespace ();
3503 m = gfc_match_name (name);
3504 if (m != MATCH_YES)
3505 return m;
3507 if (strcmp (name, gfc_current_block ()->name) != 0)
3509 gfc_error ("Expected case name of '%s' at %C",
3510 gfc_current_block ()->name);
3511 return MATCH_ERROR;
3514 return gfc_match_eos ();
3518 /* Match a SELECT statement. */
3520 match
3521 gfc_match_select (void)
3523 gfc_expr *expr;
3524 match m;
3526 m = gfc_match_label ();
3527 if (m == MATCH_ERROR)
3528 return m;
3530 m = gfc_match (" select case ( %e )%t", &expr);
3531 if (m != MATCH_YES)
3532 return m;
3534 new_st.op = EXEC_SELECT;
3535 new_st.expr = expr;
3537 return MATCH_YES;
3541 /* Match a CASE statement. */
3543 match
3544 gfc_match_case (void)
3546 gfc_case *c, *head, *tail;
3547 match m;
3549 head = tail = NULL;
3551 if (gfc_current_state () != COMP_SELECT)
3553 gfc_error ("Unexpected CASE statement at %C");
3554 return MATCH_ERROR;
3557 if (gfc_match ("% default") == MATCH_YES)
3559 m = match_case_eos ();
3560 if (m == MATCH_NO)
3561 goto syntax;
3562 if (m == MATCH_ERROR)
3563 goto cleanup;
3565 new_st.op = EXEC_SELECT;
3566 c = gfc_get_case ();
3567 c->where = gfc_current_locus;
3568 new_st.ext.case_list = c;
3569 return MATCH_YES;
3572 if (gfc_match_char ('(') != MATCH_YES)
3573 goto syntax;
3575 for (;;)
3577 if (match_case_selector (&c) == MATCH_ERROR)
3578 goto cleanup;
3580 if (head == NULL)
3581 head = c;
3582 else
3583 tail->next = c;
3585 tail = c;
3587 if (gfc_match_char (')') == MATCH_YES)
3588 break;
3589 if (gfc_match_char (',') != MATCH_YES)
3590 goto syntax;
3593 m = match_case_eos ();
3594 if (m == MATCH_NO)
3595 goto syntax;
3596 if (m == MATCH_ERROR)
3597 goto cleanup;
3599 new_st.op = EXEC_SELECT;
3600 new_st.ext.case_list = head;
3602 return MATCH_YES;
3604 syntax:
3605 gfc_error ("Syntax error in CASE-specification at %C");
3607 cleanup:
3608 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3609 return MATCH_ERROR;
3612 /********************* WHERE subroutines ********************/
3614 /* Match the rest of a simple WHERE statement that follows an IF statement.
3617 static match
3618 match_simple_where (void)
3620 gfc_expr *expr;
3621 gfc_code *c;
3622 match m;
3624 m = gfc_match (" ( %e )", &expr);
3625 if (m != MATCH_YES)
3626 return m;
3628 m = gfc_match_assignment ();
3629 if (m == MATCH_NO)
3630 goto syntax;
3631 if (m == MATCH_ERROR)
3632 goto cleanup;
3634 if (gfc_match_eos () != MATCH_YES)
3635 goto syntax;
3637 c = gfc_get_code ();
3639 c->op = EXEC_WHERE;
3640 c->expr = expr;
3641 c->next = gfc_get_code ();
3643 *c->next = new_st;
3644 gfc_clear_new_st ();
3646 new_st.op = EXEC_WHERE;
3647 new_st.block = c;
3649 return MATCH_YES;
3651 syntax:
3652 gfc_syntax_error (ST_WHERE);
3654 cleanup:
3655 gfc_free_expr (expr);
3656 return MATCH_ERROR;
3660 /* Match a WHERE statement. */
3662 match
3663 gfc_match_where (gfc_statement *st)
3665 gfc_expr *expr;
3666 match m0, m;
3667 gfc_code *c;
3669 m0 = gfc_match_label ();
3670 if (m0 == MATCH_ERROR)
3671 return m0;
3673 m = gfc_match (" where ( %e )", &expr);
3674 if (m != MATCH_YES)
3675 return m;
3677 if (gfc_match_eos () == MATCH_YES)
3679 *st = ST_WHERE_BLOCK;
3680 new_st.op = EXEC_WHERE;
3681 new_st.expr = expr;
3682 return MATCH_YES;
3685 m = gfc_match_assignment ();
3686 if (m == MATCH_NO)
3687 gfc_syntax_error (ST_WHERE);
3689 if (m != MATCH_YES)
3691 gfc_free_expr (expr);
3692 return MATCH_ERROR;
3695 /* We've got a simple WHERE statement. */
3696 *st = ST_WHERE;
3697 c = gfc_get_code ();
3699 c->op = EXEC_WHERE;
3700 c->expr = expr;
3701 c->next = gfc_get_code ();
3703 *c->next = new_st;
3704 gfc_clear_new_st ();
3706 new_st.op = EXEC_WHERE;
3707 new_st.block = c;
3709 return MATCH_YES;
3713 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3714 new_st if successful. */
3716 match
3717 gfc_match_elsewhere (void)
3719 char name[GFC_MAX_SYMBOL_LEN + 1];
3720 gfc_expr *expr;
3721 match m;
3723 if (gfc_current_state () != COMP_WHERE)
3725 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3726 return MATCH_ERROR;
3729 expr = NULL;
3731 if (gfc_match_char ('(') == MATCH_YES)
3733 m = gfc_match_expr (&expr);
3734 if (m == MATCH_NO)
3735 goto syntax;
3736 if (m == MATCH_ERROR)
3737 return MATCH_ERROR;
3739 if (gfc_match_char (')') != MATCH_YES)
3740 goto syntax;
3743 if (gfc_match_eos () != MATCH_YES)
3745 /* Only makes sense if we have a where-construct-name. */
3746 if (!gfc_current_block ())
3748 m = MATCH_ERROR;
3749 goto cleanup;
3751 /* Better be a name at this point. */
3752 m = gfc_match_name (name);
3753 if (m == MATCH_NO)
3754 goto syntax;
3755 if (m == MATCH_ERROR)
3756 goto cleanup;
3758 if (gfc_match_eos () != MATCH_YES)
3759 goto syntax;
3761 if (strcmp (name, gfc_current_block ()->name) != 0)
3763 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3764 name, gfc_current_block ()->name);
3765 goto cleanup;
3769 new_st.op = EXEC_WHERE;
3770 new_st.expr = expr;
3771 return MATCH_YES;
3773 syntax:
3774 gfc_syntax_error (ST_ELSEWHERE);
3776 cleanup:
3777 gfc_free_expr (expr);
3778 return MATCH_ERROR;
3782 /******************** FORALL subroutines ********************/
3784 /* Free a list of FORALL iterators. */
3786 void
3787 gfc_free_forall_iterator (gfc_forall_iterator *iter)
3789 gfc_forall_iterator *next;
3791 while (iter)
3793 next = iter->next;
3794 gfc_free_expr (iter->var);
3795 gfc_free_expr (iter->start);
3796 gfc_free_expr (iter->end);
3797 gfc_free_expr (iter->stride);
3798 gfc_free (iter);
3799 iter = next;
3804 /* Match an iterator as part of a FORALL statement. The format is:
3806 <var> = <start>:<end>[:<stride>]
3808 On MATCH_NO, the caller tests for the possibility that there is a
3809 scalar mask expression. */
3811 static match
3812 match_forall_iterator (gfc_forall_iterator **result)
3814 gfc_forall_iterator *iter;
3815 locus where;
3816 match m;
3818 where = gfc_current_locus;
3819 iter = XCNEW (gfc_forall_iterator);
3821 m = gfc_match_expr (&iter->var);
3822 if (m != MATCH_YES)
3823 goto cleanup;
3825 if (gfc_match_char ('=') != MATCH_YES
3826 || iter->var->expr_type != EXPR_VARIABLE)
3828 m = MATCH_NO;
3829 goto cleanup;
3832 m = gfc_match_expr (&iter->start);
3833 if (m != MATCH_YES)
3834 goto cleanup;
3836 if (gfc_match_char (':') != MATCH_YES)
3837 goto syntax;
3839 m = gfc_match_expr (&iter->end);
3840 if (m == MATCH_NO)
3841 goto syntax;
3842 if (m == MATCH_ERROR)
3843 goto cleanup;
3845 if (gfc_match_char (':') == MATCH_NO)
3846 iter->stride = gfc_int_expr (1);
3847 else
3849 m = gfc_match_expr (&iter->stride);
3850 if (m == MATCH_NO)
3851 goto syntax;
3852 if (m == MATCH_ERROR)
3853 goto cleanup;
3856 /* Mark the iteration variable's symbol as used as a FORALL index. */
3857 iter->var->symtree->n.sym->forall_index = true;
3859 *result = iter;
3860 return MATCH_YES;
3862 syntax:
3863 gfc_error ("Syntax error in FORALL iterator at %C");
3864 m = MATCH_ERROR;
3866 cleanup:
3868 gfc_current_locus = where;
3869 gfc_free_forall_iterator (iter);
3870 return m;
3874 /* Match the header of a FORALL statement. */
3876 static match
3877 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
3879 gfc_forall_iterator *head, *tail, *new_iter;
3880 gfc_expr *msk;
3881 match m;
3883 gfc_gobble_whitespace ();
3885 head = tail = NULL;
3886 msk = NULL;
3888 if (gfc_match_char ('(') != MATCH_YES)
3889 return MATCH_NO;
3891 m = match_forall_iterator (&new_iter);
3892 if (m == MATCH_ERROR)
3893 goto cleanup;
3894 if (m == MATCH_NO)
3895 goto syntax;
3897 head = tail = new_iter;
3899 for (;;)
3901 if (gfc_match_char (',') != MATCH_YES)
3902 break;
3904 m = match_forall_iterator (&new_iter);
3905 if (m == MATCH_ERROR)
3906 goto cleanup;
3908 if (m == MATCH_YES)
3910 tail->next = new_iter;
3911 tail = new_iter;
3912 continue;
3915 /* Have to have a mask expression. */
3917 m = gfc_match_expr (&msk);
3918 if (m == MATCH_NO)
3919 goto syntax;
3920 if (m == MATCH_ERROR)
3921 goto cleanup;
3923 break;
3926 if (gfc_match_char (')') == MATCH_NO)
3927 goto syntax;
3929 *phead = head;
3930 *mask = msk;
3931 return MATCH_YES;
3933 syntax:
3934 gfc_syntax_error (ST_FORALL);
3936 cleanup:
3937 gfc_free_expr (msk);
3938 gfc_free_forall_iterator (head);
3940 return MATCH_ERROR;
3943 /* Match the rest of a simple FORALL statement that follows an
3944 IF statement. */
3946 static match
3947 match_simple_forall (void)
3949 gfc_forall_iterator *head;
3950 gfc_expr *mask;
3951 gfc_code *c;
3952 match m;
3954 mask = NULL;
3955 head = NULL;
3956 c = NULL;
3958 m = match_forall_header (&head, &mask);
3960 if (m == MATCH_NO)
3961 goto syntax;
3962 if (m != MATCH_YES)
3963 goto cleanup;
3965 m = gfc_match_assignment ();
3967 if (m == MATCH_ERROR)
3968 goto cleanup;
3969 if (m == MATCH_NO)
3971 m = gfc_match_pointer_assignment ();
3972 if (m == MATCH_ERROR)
3973 goto cleanup;
3974 if (m == MATCH_NO)
3975 goto syntax;
3978 c = gfc_get_code ();
3979 *c = new_st;
3980 c->loc = gfc_current_locus;
3982 if (gfc_match_eos () != MATCH_YES)
3983 goto syntax;
3985 gfc_clear_new_st ();
3986 new_st.op = EXEC_FORALL;
3987 new_st.expr = mask;
3988 new_st.ext.forall_iterator = head;
3989 new_st.block = gfc_get_code ();
3991 new_st.block->op = EXEC_FORALL;
3992 new_st.block->next = c;
3994 return MATCH_YES;
3996 syntax:
3997 gfc_syntax_error (ST_FORALL);
3999 cleanup:
4000 gfc_free_forall_iterator (head);
4001 gfc_free_expr (mask);
4003 return MATCH_ERROR;
4007 /* Match a FORALL statement. */
4009 match
4010 gfc_match_forall (gfc_statement *st)
4012 gfc_forall_iterator *head;
4013 gfc_expr *mask;
4014 gfc_code *c;
4015 match m0, m;
4017 head = NULL;
4018 mask = NULL;
4019 c = NULL;
4021 m0 = gfc_match_label ();
4022 if (m0 == MATCH_ERROR)
4023 return MATCH_ERROR;
4025 m = gfc_match (" forall");
4026 if (m != MATCH_YES)
4027 return m;
4029 m = match_forall_header (&head, &mask);
4030 if (m == MATCH_ERROR)
4031 goto cleanup;
4032 if (m == MATCH_NO)
4033 goto syntax;
4035 if (gfc_match_eos () == MATCH_YES)
4037 *st = ST_FORALL_BLOCK;
4038 new_st.op = EXEC_FORALL;
4039 new_st.expr = mask;
4040 new_st.ext.forall_iterator = head;
4041 return MATCH_YES;
4044 m = gfc_match_assignment ();
4045 if (m == MATCH_ERROR)
4046 goto cleanup;
4047 if (m == MATCH_NO)
4049 m = gfc_match_pointer_assignment ();
4050 if (m == MATCH_ERROR)
4051 goto cleanup;
4052 if (m == MATCH_NO)
4053 goto syntax;
4056 c = gfc_get_code ();
4057 *c = new_st;
4058 c->loc = gfc_current_locus;
4060 gfc_clear_new_st ();
4061 new_st.op = EXEC_FORALL;
4062 new_st.expr = mask;
4063 new_st.ext.forall_iterator = head;
4064 new_st.block = gfc_get_code ();
4065 new_st.block->op = EXEC_FORALL;
4066 new_st.block->next = c;
4068 *st = ST_FORALL;
4069 return MATCH_YES;
4071 syntax:
4072 gfc_syntax_error (ST_FORALL);
4074 cleanup:
4075 gfc_free_forall_iterator (head);
4076 gfc_free_expr (mask);
4077 gfc_free_statements (c);
4078 return MATCH_NO;