Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / gcc / fortran / match.c
blob501a0918937bb008f3ec316cb764298fcca770b6
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
3 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "flags.h"
27 #include "gfortran.h"
28 #include "match.h"
29 #include "parse.h"
31 /* For matching and debugging purposes. Order matters here! The
32 unary operators /must/ precede the binary plus and minus, or
33 the expression parser breaks. */
35 mstring intrinsic_operators[] = {
36 minit ("+", INTRINSIC_UPLUS),
37 minit ("-", INTRINSIC_UMINUS),
38 minit ("+", INTRINSIC_PLUS),
39 minit ("-", INTRINSIC_MINUS),
40 minit ("**", INTRINSIC_POWER),
41 minit ("//", INTRINSIC_CONCAT),
42 minit ("*", INTRINSIC_TIMES),
43 minit ("/", INTRINSIC_DIVIDE),
44 minit (".and.", INTRINSIC_AND),
45 minit (".or.", INTRINSIC_OR),
46 minit (".eqv.", INTRINSIC_EQV),
47 minit (".neqv.", INTRINSIC_NEQV),
48 minit (".eq.", INTRINSIC_EQ),
49 minit ("==", INTRINSIC_EQ),
50 minit (".ne.", INTRINSIC_NE),
51 minit ("/=", INTRINSIC_NE),
52 minit (".ge.", INTRINSIC_GE),
53 minit (">=", INTRINSIC_GE),
54 minit (".le.", INTRINSIC_LE),
55 minit ("<=", INTRINSIC_LE),
56 minit (".lt.", INTRINSIC_LT),
57 minit ("<", INTRINSIC_LT),
58 minit (".gt.", INTRINSIC_GT),
59 minit (">", INTRINSIC_GT),
60 minit (".not.", INTRINSIC_NOT),
61 minit (NULL, INTRINSIC_NONE)
65 /******************** Generic matching subroutines ************************/
67 /* In free form, match at least one space. Always matches in fixed
68 form. */
70 match
71 gfc_match_space (void)
73 locus old_loc;
74 int c;
76 if (gfc_current_form == FORM_FIXED)
77 return MATCH_YES;
79 old_loc = gfc_current_locus;
81 c = gfc_next_char ();
82 if (!gfc_is_whitespace (c))
84 gfc_current_locus = old_loc;
85 return MATCH_NO;
88 gfc_gobble_whitespace ();
90 return MATCH_YES;
94 /* Match an end of statement. End of statement is optional
95 whitespace, followed by a ';' or '\n' or comment '!'. If a
96 semicolon is found, we continue to eat whitespace and semicolons. */
98 match
99 gfc_match_eos (void)
101 locus old_loc;
102 int flag, c;
104 flag = 0;
106 for (;;)
108 old_loc = gfc_current_locus;
109 gfc_gobble_whitespace ();
111 c = gfc_next_char ();
112 switch (c)
114 case '!':
117 c = gfc_next_char ();
119 while (c != '\n');
121 /* Fall through */
123 case '\n':
124 return MATCH_YES;
126 case ';':
127 flag = 1;
128 continue;
131 break;
134 gfc_current_locus = old_loc;
135 return (flag) ? MATCH_YES : MATCH_NO;
139 /* Match a literal integer on the input, setting the value on
140 MATCH_YES. Literal ints occur in kind-parameters as well as
141 old-style character length specifications. */
143 match
144 gfc_match_small_literal_int (int *value, int *cnt)
146 locus old_loc;
147 char c;
148 int i, j;
150 old_loc = gfc_current_locus;
152 gfc_gobble_whitespace ();
153 c = gfc_next_char ();
155 if (!ISDIGIT (c))
157 gfc_current_locus = old_loc;
158 return MATCH_NO;
161 i = c - '0';
162 j = 1;
164 for (;;)
166 old_loc = gfc_current_locus;
167 c = gfc_next_char ();
169 if (!ISDIGIT (c))
170 break;
172 i = 10 * i + c - '0';
173 j++;
175 if (i > 99999999)
177 gfc_error ("Integer too large at %C");
178 return MATCH_ERROR;
182 gfc_current_locus = old_loc;
184 *value = i;
185 *cnt = j;
186 return MATCH_YES;
190 /* Match a small, constant integer expression, like in a kind
191 statement. On MATCH_YES, 'value' is set. */
193 match
194 gfc_match_small_int (int *value)
196 gfc_expr *expr;
197 const char *p;
198 match m;
199 int i;
201 m = gfc_match_expr (&expr);
202 if (m != MATCH_YES)
203 return m;
205 p = gfc_extract_int (expr, &i);
206 gfc_free_expr (expr);
208 if (p != NULL)
210 gfc_error (p);
211 m = MATCH_ERROR;
214 *value = i;
215 return m;
219 /* Matches a statement label. Uses gfc_match_small_literal_int() to
220 do most of the work. */
222 match
223 gfc_match_st_label (gfc_st_label ** label)
225 locus old_loc;
226 match m;
227 int i, cnt;
229 old_loc = gfc_current_locus;
231 m = gfc_match_small_literal_int (&i, &cnt);
232 if (m != MATCH_YES)
233 return m;
235 if (cnt > 5)
237 gfc_error ("Too many digits in statement label at %C");
238 goto cleanup;
241 if (i == 0)
243 gfc_error ("Statement label at %C is zero");
244 goto cleanup;
247 *label = gfc_get_st_label (i);
248 return MATCH_YES;
250 cleanup:
252 gfc_current_locus = old_loc;
253 return MATCH_ERROR;
257 /* Match and validate a label associated with a named IF, DO or SELECT
258 statement. If the symbol does not have the label attribute, we add
259 it. We also make sure the symbol does not refer to another
260 (active) block. A matched label is pointed to by gfc_new_block. */
262 match
263 gfc_match_label (void)
265 char name[GFC_MAX_SYMBOL_LEN + 1];
266 match m;
268 gfc_new_block = NULL;
270 m = gfc_match (" %n :", name);
271 if (m != MATCH_YES)
272 return m;
274 if (gfc_get_symbol (name, NULL, &gfc_new_block))
276 gfc_error ("Label name '%s' at %C is ambiguous", name);
277 return MATCH_ERROR;
280 if (gfc_new_block->attr.flavor == FL_LABEL)
282 gfc_error ("Duplicate construct label '%s' at %C", name);
283 return MATCH_ERROR;
286 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
287 gfc_new_block->name, NULL) == FAILURE)
288 return MATCH_ERROR;
290 return MATCH_YES;
294 /* Try and match the input against an array of possibilities. If one
295 potential matching string is a substring of another, the longest
296 match takes precedence. Spaces in the target strings are optional
297 spaces that do not necessarily have to be found in the input
298 stream. In fixed mode, spaces never appear. If whitespace is
299 matched, it matches unlimited whitespace in the input. For this
300 reason, the 'mp' member of the mstring structure is used to track
301 the progress of each potential match.
303 If there is no match we return the tag associated with the
304 terminating NULL mstring structure and leave the locus pointer
305 where it started. If there is a match we return the tag member of
306 the matched mstring and leave the locus pointer after the matched
307 character.
309 A '%' character is a mandatory space. */
312 gfc_match_strings (mstring * a)
314 mstring *p, *best_match;
315 int no_match, c, possibles;
316 locus match_loc;
318 possibles = 0;
320 for (p = a; p->string != NULL; p++)
322 p->mp = p->string;
323 possibles++;
326 no_match = p->tag;
328 best_match = NULL;
329 match_loc = gfc_current_locus;
331 gfc_gobble_whitespace ();
333 while (possibles > 0)
335 c = gfc_next_char ();
337 /* Apply the next character to the current possibilities. */
338 for (p = a; p->string != NULL; p++)
340 if (p->mp == NULL)
341 continue;
343 if (*p->mp == ' ')
345 /* Space matches 1+ whitespace(s). */
346 if ((gfc_current_form == FORM_FREE)
347 && gfc_is_whitespace (c))
348 continue;
350 p->mp++;
353 if (*p->mp != c)
355 /* Match failed. */
356 p->mp = NULL;
357 possibles--;
358 continue;
361 p->mp++;
362 if (*p->mp == '\0')
364 /* Found a match. */
365 match_loc = gfc_current_locus;
366 best_match = p;
367 possibles--;
368 p->mp = NULL;
373 gfc_current_locus = match_loc;
375 return (best_match == NULL) ? no_match : best_match->tag;
379 /* See if the current input looks like a name of some sort. Modifies
380 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long. */
382 match
383 gfc_match_name (char *buffer)
385 locus old_loc;
386 int i, c;
388 old_loc = gfc_current_locus;
389 gfc_gobble_whitespace ();
391 c = gfc_next_char ();
392 if (!ISALPHA (c))
394 gfc_current_locus = old_loc;
395 return MATCH_NO;
398 i = 0;
402 buffer[i++] = c;
404 if (i > gfc_option.max_identifier_length)
406 gfc_error ("Name at %C is too long");
407 return MATCH_ERROR;
410 old_loc = gfc_current_locus;
411 c = gfc_next_char ();
413 while (ISALNUM (c)
414 || c == '_'
415 || (gfc_option.flag_dollar_ok && c == '$'));
417 buffer[i] = '\0';
418 gfc_current_locus = old_loc;
420 return MATCH_YES;
424 /* Match a symbol on the input. Modifies the pointer to the symbol
425 pointer if successful. */
427 match
428 gfc_match_sym_tree (gfc_symtree ** matched_symbol, int host_assoc)
430 char buffer[GFC_MAX_SYMBOL_LEN + 1];
431 match m;
433 m = gfc_match_name (buffer);
434 if (m != MATCH_YES)
435 return m;
437 if (host_assoc)
438 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
439 ? MATCH_ERROR : MATCH_YES;
441 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
442 return MATCH_ERROR;
444 return MATCH_YES;
448 match
449 gfc_match_symbol (gfc_symbol ** matched_symbol, int host_assoc)
451 gfc_symtree *st;
452 match m;
454 m = gfc_match_sym_tree (&st, host_assoc);
456 if (m == MATCH_YES)
458 if (st)
459 *matched_symbol = st->n.sym;
460 else
461 *matched_symbol = NULL;
463 else
464 *matched_symbol = NULL;
465 return m;
468 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
469 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
470 in matchexp.c. */
472 match
473 gfc_match_intrinsic_op (gfc_intrinsic_op * result)
475 gfc_intrinsic_op op;
477 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
479 if (op == INTRINSIC_NONE)
480 return MATCH_NO;
482 *result = op;
483 return MATCH_YES;
487 /* Match a loop control phrase:
489 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
491 If the final integer expression is not present, a constant unity
492 expression is returned. We don't return MATCH_ERROR until after
493 the equals sign is seen. */
495 match
496 gfc_match_iterator (gfc_iterator * iter, int init_flag)
498 char name[GFC_MAX_SYMBOL_LEN + 1];
499 gfc_expr *var, *e1, *e2, *e3;
500 locus start;
501 match m;
503 /* Match the start of an iterator without affecting the symbol
504 table. */
506 start = gfc_current_locus;
507 m = gfc_match (" %n =", name);
508 gfc_current_locus = start;
510 if (m != MATCH_YES)
511 return MATCH_NO;
513 m = gfc_match_variable (&var, 0);
514 if (m != MATCH_YES)
515 return MATCH_NO;
517 gfc_match_char ('=');
519 e1 = e2 = e3 = NULL;
521 if (var->ref != NULL)
523 gfc_error ("Loop variable at %C cannot be a sub-component");
524 goto cleanup;
527 if (var->symtree->n.sym->attr.intent == INTENT_IN)
529 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
530 var->symtree->n.sym->name);
531 goto cleanup;
534 if (var->symtree->n.sym->attr.pointer)
536 gfc_error ("Loop variable at %C cannot have the POINTER attribute");
537 goto cleanup;
540 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
541 if (m == MATCH_NO)
542 goto syntax;
543 if (m == MATCH_ERROR)
544 goto cleanup;
546 if (gfc_match_char (',') != MATCH_YES)
547 goto syntax;
549 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
550 if (m == MATCH_NO)
551 goto syntax;
552 if (m == MATCH_ERROR)
553 goto cleanup;
555 if (gfc_match_char (',') != MATCH_YES)
557 e3 = gfc_int_expr (1);
558 goto done;
561 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
562 if (m == MATCH_ERROR)
563 goto cleanup;
564 if (m == MATCH_NO)
566 gfc_error ("Expected a step value in iterator at %C");
567 goto cleanup;
570 done:
571 iter->var = var;
572 iter->start = e1;
573 iter->end = e2;
574 iter->step = e3;
575 return MATCH_YES;
577 syntax:
578 gfc_error ("Syntax error in iterator at %C");
580 cleanup:
581 gfc_free_expr (e1);
582 gfc_free_expr (e2);
583 gfc_free_expr (e3);
585 return MATCH_ERROR;
589 /* Tries to match the next non-whitespace character on the input.
590 This subroutine does not return MATCH_ERROR. */
592 match
593 gfc_match_char (char c)
595 locus where;
597 where = gfc_current_locus;
598 gfc_gobble_whitespace ();
600 if (gfc_next_char () == c)
601 return MATCH_YES;
603 gfc_current_locus = where;
604 return MATCH_NO;
608 /* General purpose matching subroutine. The target string is a
609 scanf-like format string in which spaces correspond to arbitrary
610 whitespace (including no whitespace), characters correspond to
611 themselves. The %-codes are:
613 %% Literal percent sign
614 %e Expression, pointer to a pointer is set
615 %s Symbol, pointer to the symbol is set
616 %n Name, character buffer is set to name
617 %t Matches end of statement.
618 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
619 %l Matches a statement label
620 %v Matches a variable expression (an lvalue)
621 % Matches a required space (in free form) and optional spaces. */
623 match
624 gfc_match (const char *target, ...)
626 gfc_st_label **label;
627 int matches, *ip;
628 locus old_loc;
629 va_list argp;
630 char c, *np;
631 match m, n;
632 void **vp;
633 const char *p;
635 old_loc = gfc_current_locus;
636 va_start (argp, target);
637 m = MATCH_NO;
638 matches = 0;
639 p = target;
641 loop:
642 c = *p++;
643 switch (c)
645 case ' ':
646 gfc_gobble_whitespace ();
647 goto loop;
648 case '\0':
649 m = MATCH_YES;
650 break;
652 case '%':
653 c = *p++;
654 switch (c)
656 case 'e':
657 vp = va_arg (argp, void **);
658 n = gfc_match_expr ((gfc_expr **) vp);
659 if (n != MATCH_YES)
661 m = n;
662 goto not_yes;
665 matches++;
666 goto loop;
668 case 'v':
669 vp = va_arg (argp, void **);
670 n = gfc_match_variable ((gfc_expr **) vp, 0);
671 if (n != MATCH_YES)
673 m = n;
674 goto not_yes;
677 matches++;
678 goto loop;
680 case 's':
681 vp = va_arg (argp, void **);
682 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
683 if (n != MATCH_YES)
685 m = n;
686 goto not_yes;
689 matches++;
690 goto loop;
692 case 'n':
693 np = va_arg (argp, char *);
694 n = gfc_match_name (np);
695 if (n != MATCH_YES)
697 m = n;
698 goto not_yes;
701 matches++;
702 goto loop;
704 case 'l':
705 label = va_arg (argp, gfc_st_label **);
706 n = gfc_match_st_label (label);
707 if (n != MATCH_YES)
709 m = n;
710 goto not_yes;
713 matches++;
714 goto loop;
716 case 'o':
717 ip = va_arg (argp, int *);
718 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
719 if (n != MATCH_YES)
721 m = n;
722 goto not_yes;
725 matches++;
726 goto loop;
728 case 't':
729 if (gfc_match_eos () != MATCH_YES)
731 m = MATCH_NO;
732 goto not_yes;
734 goto loop;
736 case ' ':
737 if (gfc_match_space () == MATCH_YES)
738 goto loop;
739 m = MATCH_NO;
740 goto not_yes;
742 case '%':
743 break; /* Fall through to character matcher */
745 default:
746 gfc_internal_error ("gfc_match(): Bad match code %c", c);
749 default:
750 if (c == gfc_next_char ())
751 goto loop;
752 break;
755 not_yes:
756 va_end (argp);
758 if (m != MATCH_YES)
760 /* Clean up after a failed match. */
761 gfc_current_locus = old_loc;
762 va_start (argp, target);
764 p = target;
765 for (; matches > 0; matches--)
767 while (*p++ != '%');
769 switch (*p++)
771 case '%':
772 matches++;
773 break; /* Skip */
775 /* Matches that don't have to be undone */
776 case 'o':
777 case 'l':
778 case 'n':
779 case 's':
780 (void)va_arg (argp, void **);
781 break;
783 case 'e':
784 case 'v':
785 vp = va_arg (argp, void **);
786 gfc_free_expr (*vp);
787 *vp = NULL;
788 break;
792 va_end (argp);
795 return m;
799 /*********************** Statement level matching **********************/
801 /* Matches the start of a program unit, which is the program keyword
802 followed by an obligatory symbol. */
804 match
805 gfc_match_program (void)
807 gfc_symbol *sym;
808 match m;
810 m = gfc_match ("% %s%t", &sym);
812 if (m == MATCH_NO)
814 gfc_error ("Invalid form of PROGRAM statement at %C");
815 m = MATCH_ERROR;
818 if (m == MATCH_ERROR)
819 return m;
821 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
822 return MATCH_ERROR;
824 gfc_new_block = sym;
826 return MATCH_YES;
830 /* Match a simple assignment statement. */
832 match
833 gfc_match_assignment (void)
835 gfc_expr *lvalue, *rvalue;
836 locus old_loc;
837 match m;
839 old_loc = gfc_current_locus;
841 lvalue = rvalue = NULL;
842 m = gfc_match (" %v =", &lvalue);
843 if (m != MATCH_YES)
844 goto cleanup;
846 if (lvalue->symtree->n.sym->attr.flavor == FL_PARAMETER)
848 gfc_error ("Cannot assign to a PARAMETER variable at %C");
849 m = MATCH_ERROR;
850 goto cleanup;
853 m = gfc_match (" %e%t", &rvalue);
854 if (m != MATCH_YES)
855 goto cleanup;
857 gfc_set_sym_referenced (lvalue->symtree->n.sym);
859 new_st.op = EXEC_ASSIGN;
860 new_st.expr = lvalue;
861 new_st.expr2 = rvalue;
863 gfc_check_do_variable (lvalue->symtree);
865 return MATCH_YES;
867 cleanup:
868 gfc_current_locus = old_loc;
869 gfc_free_expr (lvalue);
870 gfc_free_expr (rvalue);
871 return m;
875 /* Match a pointer assignment statement. */
877 match
878 gfc_match_pointer_assignment (void)
880 gfc_expr *lvalue, *rvalue;
881 locus old_loc;
882 match m;
884 old_loc = gfc_current_locus;
886 lvalue = rvalue = NULL;
888 m = gfc_match (" %v =>", &lvalue);
889 if (m != MATCH_YES)
891 m = MATCH_NO;
892 goto cleanup;
895 m = gfc_match (" %e%t", &rvalue);
896 if (m != MATCH_YES)
897 goto cleanup;
899 new_st.op = EXEC_POINTER_ASSIGN;
900 new_st.expr = lvalue;
901 new_st.expr2 = rvalue;
903 return MATCH_YES;
905 cleanup:
906 gfc_current_locus = old_loc;
907 gfc_free_expr (lvalue);
908 gfc_free_expr (rvalue);
909 return m;
913 /* We try to match an easy arithmetic IF statement. This only happens
914 when just after having encountered a simple IF statement. This code
915 is really duplicate with parts of the gfc_match_if code, but this is
916 *much* easier. */
917 static match
918 match_arithmetic_if (void)
920 gfc_st_label *l1, *l2, *l3;
921 gfc_expr *expr;
922 match m;
924 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
925 if (m != MATCH_YES)
926 return m;
928 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
929 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
930 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
932 gfc_free_expr (expr);
933 return MATCH_ERROR;
936 if (gfc_notify_std (GFC_STD_F95_DEL,
937 "Obsolete: arithmetic IF statement at %C") == FAILURE)
938 return MATCH_ERROR;
940 new_st.op = EXEC_ARITHMETIC_IF;
941 new_st.expr = expr;
942 new_st.label = l1;
943 new_st.label2 = l2;
944 new_st.label3 = l3;
946 return MATCH_YES;
950 /* The IF statement is a bit of a pain. First of all, there are three
951 forms of it, the simple IF, the IF that starts a block and the
952 arithmetic IF.
954 There is a problem with the simple IF and that is the fact that we
955 only have a single level of undo information on symbols. What this
956 means is for a simple IF, we must re-match the whole IF statement
957 multiple times in order to guarantee that the symbol table ends up
958 in the proper state. */
960 static match match_simple_forall (void);
961 static match match_simple_where (void);
963 match
964 gfc_match_if (gfc_statement * if_type)
966 gfc_expr *expr;
967 gfc_st_label *l1, *l2, *l3;
968 locus old_loc;
969 gfc_code *p;
970 match m, n;
972 n = gfc_match_label ();
973 if (n == MATCH_ERROR)
974 return n;
976 old_loc = gfc_current_locus;
978 m = gfc_match (" if ( %e", &expr);
979 if (m != MATCH_YES)
980 return m;
982 if (gfc_match_char (')') != MATCH_YES)
984 gfc_error ("Syntax error in IF-expression at %C");
985 gfc_free_expr (expr);
986 return MATCH_ERROR;
989 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
991 if (m == MATCH_YES)
993 if (n == MATCH_YES)
995 gfc_error
996 ("Block label not appropriate for arithmetic IF statement "
997 "at %C");
999 gfc_free_expr (expr);
1000 return MATCH_ERROR;
1003 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1004 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1005 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1008 gfc_free_expr (expr);
1009 return MATCH_ERROR;
1012 if (gfc_notify_std (GFC_STD_F95_DEL,
1013 "Obsolete: arithmetic IF statement at %C")
1014 == FAILURE)
1015 return MATCH_ERROR;
1017 new_st.op = EXEC_ARITHMETIC_IF;
1018 new_st.expr = expr;
1019 new_st.label = l1;
1020 new_st.label2 = l2;
1021 new_st.label3 = l3;
1023 *if_type = ST_ARITHMETIC_IF;
1024 return MATCH_YES;
1027 if (gfc_match (" then%t") == MATCH_YES)
1029 new_st.op = EXEC_IF;
1030 new_st.expr = expr;
1032 *if_type = ST_IF_BLOCK;
1033 return MATCH_YES;
1036 if (n == MATCH_YES)
1038 gfc_error ("Block label is not appropriate IF statement at %C");
1040 gfc_free_expr (expr);
1041 return MATCH_ERROR;
1044 /* At this point the only thing left is a simple IF statement. At
1045 this point, n has to be MATCH_NO, so we don't have to worry about
1046 re-matching a block label. From what we've got so far, try
1047 matching an assignment. */
1049 *if_type = ST_SIMPLE_IF;
1051 m = gfc_match_assignment ();
1052 if (m == MATCH_YES)
1053 goto got_match;
1055 gfc_free_expr (expr);
1056 gfc_undo_symbols ();
1057 gfc_current_locus = old_loc;
1059 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1061 m = gfc_match_pointer_assignment ();
1062 if (m == MATCH_YES)
1063 goto got_match;
1065 gfc_free_expr (expr);
1066 gfc_undo_symbols ();
1067 gfc_current_locus = old_loc;
1069 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1071 /* Look at the next keyword to see which matcher to call. Matching
1072 the keyword doesn't affect the symbol table, so we don't have to
1073 restore between tries. */
1075 #define match(string, subr, statement) \
1076 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1078 gfc_clear_error ();
1080 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1081 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1082 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1083 match ("call", gfc_match_call, ST_CALL)
1084 match ("close", gfc_match_close, ST_CLOSE)
1085 match ("continue", gfc_match_continue, ST_CONTINUE)
1086 match ("cycle", gfc_match_cycle, ST_CYCLE)
1087 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1088 match ("end file", gfc_match_endfile, ST_END_FILE)
1089 match ("exit", gfc_match_exit, ST_EXIT)
1090 match ("flush", gfc_match_flush, ST_FLUSH)
1091 match ("forall", match_simple_forall, ST_FORALL)
1092 match ("go to", gfc_match_goto, ST_GOTO)
1093 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1094 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1095 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1096 match ("open", gfc_match_open, ST_OPEN)
1097 match ("pause", gfc_match_pause, ST_NONE)
1098 match ("print", gfc_match_print, ST_WRITE)
1099 match ("read", gfc_match_read, ST_READ)
1100 match ("return", gfc_match_return, ST_RETURN)
1101 match ("rewind", gfc_match_rewind, ST_REWIND)
1102 match ("stop", gfc_match_stop, ST_STOP)
1103 match ("where", match_simple_where, ST_WHERE)
1104 match ("write", gfc_match_write, ST_WRITE)
1106 /* All else has failed, so give up. See if any of the matchers has
1107 stored an error message of some sort. */
1108 if (gfc_error_check () == 0)
1109 gfc_error ("Unclassifiable statement in IF-clause at %C");
1111 gfc_free_expr (expr);
1112 return MATCH_ERROR;
1114 got_match:
1115 if (m == MATCH_NO)
1116 gfc_error ("Syntax error in IF-clause at %C");
1117 if (m != MATCH_YES)
1119 gfc_free_expr (expr);
1120 return MATCH_ERROR;
1123 /* At this point, we've matched the single IF and the action clause
1124 is in new_st. Rearrange things so that the IF statement appears
1125 in new_st. */
1127 p = gfc_get_code ();
1128 p->next = gfc_get_code ();
1129 *p->next = new_st;
1130 p->next->loc = gfc_current_locus;
1132 p->expr = expr;
1133 p->op = EXEC_IF;
1135 gfc_clear_new_st ();
1137 new_st.op = EXEC_IF;
1138 new_st.block = p;
1140 return MATCH_YES;
1143 #undef match
1146 /* Match an ELSE statement. */
1148 match
1149 gfc_match_else (void)
1151 char name[GFC_MAX_SYMBOL_LEN + 1];
1153 if (gfc_match_eos () == MATCH_YES)
1154 return MATCH_YES;
1156 if (gfc_match_name (name) != MATCH_YES
1157 || gfc_current_block () == NULL
1158 || gfc_match_eos () != MATCH_YES)
1160 gfc_error ("Unexpected junk after ELSE statement at %C");
1161 return MATCH_ERROR;
1164 if (strcmp (name, gfc_current_block ()->name) != 0)
1166 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1167 name, gfc_current_block ()->name);
1168 return MATCH_ERROR;
1171 return MATCH_YES;
1175 /* Match an ELSE IF statement. */
1177 match
1178 gfc_match_elseif (void)
1180 char name[GFC_MAX_SYMBOL_LEN + 1];
1181 gfc_expr *expr;
1182 match m;
1184 m = gfc_match (" ( %e ) then", &expr);
1185 if (m != MATCH_YES)
1186 return m;
1188 if (gfc_match_eos () == MATCH_YES)
1189 goto done;
1191 if (gfc_match_name (name) != MATCH_YES
1192 || gfc_current_block () == NULL
1193 || gfc_match_eos () != MATCH_YES)
1195 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1196 goto cleanup;
1199 if (strcmp (name, gfc_current_block ()->name) != 0)
1201 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1202 name, gfc_current_block ()->name);
1203 goto cleanup;
1206 done:
1207 new_st.op = EXEC_IF;
1208 new_st.expr = expr;
1209 return MATCH_YES;
1211 cleanup:
1212 gfc_free_expr (expr);
1213 return MATCH_ERROR;
1217 /* Free a gfc_iterator structure. */
1219 void
1220 gfc_free_iterator (gfc_iterator * iter, int flag)
1223 if (iter == NULL)
1224 return;
1226 gfc_free_expr (iter->var);
1227 gfc_free_expr (iter->start);
1228 gfc_free_expr (iter->end);
1229 gfc_free_expr (iter->step);
1231 if (flag)
1232 gfc_free (iter);
1236 /* Match a DO statement. */
1238 match
1239 gfc_match_do (void)
1241 gfc_iterator iter, *ip;
1242 locus old_loc;
1243 gfc_st_label *label;
1244 match m;
1246 old_loc = gfc_current_locus;
1248 label = NULL;
1249 iter.var = iter.start = iter.end = iter.step = NULL;
1251 m = gfc_match_label ();
1252 if (m == MATCH_ERROR)
1253 return m;
1255 if (gfc_match (" do") != MATCH_YES)
1256 return MATCH_NO;
1258 m = gfc_match_st_label (&label);
1259 if (m == MATCH_ERROR)
1260 goto cleanup;
1262 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1264 if (gfc_match_eos () == MATCH_YES)
1266 iter.end = gfc_logical_expr (1, NULL);
1267 new_st.op = EXEC_DO_WHILE;
1268 goto done;
1271 /* match an optional comma, if no comma is found a space is obligatory. */
1272 if (gfc_match_char(',') != MATCH_YES
1273 && gfc_match ("% ") != MATCH_YES)
1274 return MATCH_NO;
1276 /* See if we have a DO WHILE. */
1277 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1279 new_st.op = EXEC_DO_WHILE;
1280 goto done;
1283 /* The abortive DO WHILE may have done something to the symbol
1284 table, so we start over: */
1285 gfc_undo_symbols ();
1286 gfc_current_locus = old_loc;
1288 gfc_match_label (); /* This won't error */
1289 gfc_match (" do "); /* This will work */
1291 gfc_match_st_label (&label); /* Can't error out */
1292 gfc_match_char (','); /* Optional comma */
1294 m = gfc_match_iterator (&iter, 0);
1295 if (m == MATCH_NO)
1296 return MATCH_NO;
1297 if (m == MATCH_ERROR)
1298 goto cleanup;
1300 gfc_check_do_variable (iter.var->symtree);
1302 if (gfc_match_eos () != MATCH_YES)
1304 gfc_syntax_error (ST_DO);
1305 goto cleanup;
1308 new_st.op = EXEC_DO;
1310 done:
1311 if (label != NULL
1312 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1313 goto cleanup;
1315 new_st.label = label;
1317 if (new_st.op == EXEC_DO_WHILE)
1318 new_st.expr = iter.end;
1319 else
1321 new_st.ext.iterator = ip = gfc_get_iterator ();
1322 *ip = iter;
1325 return MATCH_YES;
1327 cleanup:
1328 gfc_free_iterator (&iter, 0);
1330 return MATCH_ERROR;
1334 /* Match an EXIT or CYCLE statement. */
1336 static match
1337 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1339 gfc_state_data *p;
1340 gfc_symbol *sym;
1341 match m;
1343 if (gfc_match_eos () == MATCH_YES)
1344 sym = NULL;
1345 else
1347 m = gfc_match ("% %s%t", &sym);
1348 if (m == MATCH_ERROR)
1349 return MATCH_ERROR;
1350 if (m == MATCH_NO)
1352 gfc_syntax_error (st);
1353 return MATCH_ERROR;
1356 if (sym->attr.flavor != FL_LABEL)
1358 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1359 sym->name, gfc_ascii_statement (st));
1360 return MATCH_ERROR;
1364 /* Find the loop mentioned specified by the label (or lack of a
1365 label). */
1366 for (p = gfc_state_stack; p; p = p->previous)
1367 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1368 break;
1370 if (p == NULL)
1372 if (sym == NULL)
1373 gfc_error ("%s statement at %C is not within a loop",
1374 gfc_ascii_statement (st));
1375 else
1376 gfc_error ("%s statement at %C is not within loop '%s'",
1377 gfc_ascii_statement (st), sym->name);
1379 return MATCH_ERROR;
1382 /* Save the first statement in the loop - needed by the backend. */
1383 new_st.ext.whichloop = p->head;
1385 new_st.op = op;
1386 /* new_st.sym = sym;*/
1388 return MATCH_YES;
1392 /* Match the EXIT statement. */
1394 match
1395 gfc_match_exit (void)
1398 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1402 /* Match the CYCLE statement. */
1404 match
1405 gfc_match_cycle (void)
1408 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1412 /* Match a number or character constant after a STOP or PAUSE statement. */
1414 static match
1415 gfc_match_stopcode (gfc_statement st)
1417 int stop_code;
1418 gfc_expr *e;
1419 match m;
1420 int cnt;
1422 stop_code = -1;
1423 e = NULL;
1425 if (gfc_match_eos () != MATCH_YES)
1427 m = gfc_match_small_literal_int (&stop_code, &cnt);
1428 if (m == MATCH_ERROR)
1429 goto cleanup;
1431 if (m == MATCH_YES && cnt > 5)
1433 gfc_error ("Too many digits in STOP code at %C");
1434 goto cleanup;
1437 if (m == MATCH_NO)
1439 /* Try a character constant. */
1440 m = gfc_match_expr (&e);
1441 if (m == MATCH_ERROR)
1442 goto cleanup;
1443 if (m == MATCH_NO)
1444 goto syntax;
1445 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1446 goto syntax;
1449 if (gfc_match_eos () != MATCH_YES)
1450 goto syntax;
1453 if (gfc_pure (NULL))
1455 gfc_error ("%s statement not allowed in PURE procedure at %C",
1456 gfc_ascii_statement (st));
1457 goto cleanup;
1460 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1461 new_st.expr = e;
1462 new_st.ext.stop_code = stop_code;
1464 return MATCH_YES;
1466 syntax:
1467 gfc_syntax_error (st);
1469 cleanup:
1471 gfc_free_expr (e);
1472 return MATCH_ERROR;
1475 /* Match the (deprecated) PAUSE statement. */
1477 match
1478 gfc_match_pause (void)
1480 match m;
1482 m = gfc_match_stopcode (ST_PAUSE);
1483 if (m == MATCH_YES)
1485 if (gfc_notify_std (GFC_STD_F95_DEL,
1486 "Obsolete: PAUSE statement at %C")
1487 == FAILURE)
1488 m = MATCH_ERROR;
1490 return m;
1494 /* Match the STOP statement. */
1496 match
1497 gfc_match_stop (void)
1499 return gfc_match_stopcode (ST_STOP);
1503 /* Match a CONTINUE statement. */
1505 match
1506 gfc_match_continue (void)
1509 if (gfc_match_eos () != MATCH_YES)
1511 gfc_syntax_error (ST_CONTINUE);
1512 return MATCH_ERROR;
1515 new_st.op = EXEC_CONTINUE;
1516 return MATCH_YES;
1520 /* Match the (deprecated) ASSIGN statement. */
1522 match
1523 gfc_match_assign (void)
1525 gfc_expr *expr;
1526 gfc_st_label *label;
1528 if (gfc_match (" %l", &label) == MATCH_YES)
1530 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1531 return MATCH_ERROR;
1532 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1534 if (gfc_notify_std (GFC_STD_F95_DEL,
1535 "Obsolete: ASSIGN statement at %C")
1536 == FAILURE)
1537 return MATCH_ERROR;
1539 expr->symtree->n.sym->attr.assign = 1;
1541 new_st.op = EXEC_LABEL_ASSIGN;
1542 new_st.label = label;
1543 new_st.expr = expr;
1544 return MATCH_YES;
1547 return MATCH_NO;
1551 /* Match the GO TO statement. As a computed GOTO statement is
1552 matched, it is transformed into an equivalent SELECT block. No
1553 tree is necessary, and the resulting jumps-to-jumps are
1554 specifically optimized away by the back end. */
1556 match
1557 gfc_match_goto (void)
1559 gfc_code *head, *tail;
1560 gfc_expr *expr;
1561 gfc_case *cp;
1562 gfc_st_label *label;
1563 int i;
1564 match m;
1566 if (gfc_match (" %l%t", &label) == MATCH_YES)
1568 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1569 return MATCH_ERROR;
1571 new_st.op = EXEC_GOTO;
1572 new_st.label = label;
1573 return MATCH_YES;
1576 /* The assigned GO TO statement. */
1578 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1580 if (gfc_notify_std (GFC_STD_F95_DEL,
1581 "Obsolete: Assigned GOTO statement at %C")
1582 == FAILURE)
1583 return MATCH_ERROR;
1585 new_st.op = EXEC_GOTO;
1586 new_st.expr = expr;
1588 if (gfc_match_eos () == MATCH_YES)
1589 return MATCH_YES;
1591 /* Match label list. */
1592 gfc_match_char (',');
1593 if (gfc_match_char ('(') != MATCH_YES)
1595 gfc_syntax_error (ST_GOTO);
1596 return MATCH_ERROR;
1598 head = tail = NULL;
1602 m = gfc_match_st_label (&label);
1603 if (m != MATCH_YES)
1604 goto syntax;
1606 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1607 goto cleanup;
1609 if (head == NULL)
1610 head = tail = gfc_get_code ();
1611 else
1613 tail->block = gfc_get_code ();
1614 tail = tail->block;
1617 tail->label = label;
1618 tail->op = EXEC_GOTO;
1620 while (gfc_match_char (',') == MATCH_YES);
1622 if (gfc_match (")%t") != MATCH_YES)
1623 goto syntax;
1625 if (head == NULL)
1627 gfc_error (
1628 "Statement label list in GOTO at %C cannot be empty");
1629 goto syntax;
1631 new_st.block = head;
1633 return MATCH_YES;
1636 /* Last chance is a computed GO TO statement. */
1637 if (gfc_match_char ('(') != MATCH_YES)
1639 gfc_syntax_error (ST_GOTO);
1640 return MATCH_ERROR;
1643 head = tail = NULL;
1644 i = 1;
1648 m = gfc_match_st_label (&label);
1649 if (m != MATCH_YES)
1650 goto syntax;
1652 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1653 goto cleanup;
1655 if (head == NULL)
1656 head = tail = gfc_get_code ();
1657 else
1659 tail->block = gfc_get_code ();
1660 tail = tail->block;
1663 cp = gfc_get_case ();
1664 cp->low = cp->high = gfc_int_expr (i++);
1666 tail->op = EXEC_SELECT;
1667 tail->ext.case_list = cp;
1669 tail->next = gfc_get_code ();
1670 tail->next->op = EXEC_GOTO;
1671 tail->next->label = label;
1673 while (gfc_match_char (',') == MATCH_YES);
1675 if (gfc_match_char (')') != MATCH_YES)
1676 goto syntax;
1678 if (head == NULL)
1680 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1681 goto syntax;
1684 /* Get the rest of the statement. */
1685 gfc_match_char (',');
1687 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1688 goto syntax;
1690 /* At this point, a computed GOTO has been fully matched and an
1691 equivalent SELECT statement constructed. */
1693 new_st.op = EXEC_SELECT;
1694 new_st.expr = NULL;
1696 /* Hack: For a "real" SELECT, the expression is in expr. We put
1697 it in expr2 so we can distinguish then and produce the correct
1698 diagnostics. */
1699 new_st.expr2 = expr;
1700 new_st.block = head;
1701 return MATCH_YES;
1703 syntax:
1704 gfc_syntax_error (ST_GOTO);
1705 cleanup:
1706 gfc_free_statements (head);
1707 return MATCH_ERROR;
1711 /* Frees a list of gfc_alloc structures. */
1713 void
1714 gfc_free_alloc_list (gfc_alloc * p)
1716 gfc_alloc *q;
1718 for (; p; p = q)
1720 q = p->next;
1721 gfc_free_expr (p->expr);
1722 gfc_free (p);
1727 /* Match an ALLOCATE statement. */
1729 match
1730 gfc_match_allocate (void)
1732 gfc_alloc *head, *tail;
1733 gfc_expr *stat;
1734 match m;
1736 head = tail = NULL;
1737 stat = NULL;
1739 if (gfc_match_char ('(') != MATCH_YES)
1740 goto syntax;
1742 for (;;)
1744 if (head == NULL)
1745 head = tail = gfc_get_alloc ();
1746 else
1748 tail->next = gfc_get_alloc ();
1749 tail = tail->next;
1752 m = gfc_match_variable (&tail->expr, 0);
1753 if (m == MATCH_NO)
1754 goto syntax;
1755 if (m == MATCH_ERROR)
1756 goto cleanup;
1758 if (gfc_check_do_variable (tail->expr->symtree))
1759 goto cleanup;
1761 if (gfc_pure (NULL)
1762 && gfc_impure_variable (tail->expr->symtree->n.sym))
1764 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1765 "PURE procedure");
1766 goto cleanup;
1769 if (gfc_match_char (',') != MATCH_YES)
1770 break;
1772 m = gfc_match (" stat = %v", &stat);
1773 if (m == MATCH_ERROR)
1774 goto cleanup;
1775 if (m == MATCH_YES)
1776 break;
1779 if (stat != NULL)
1781 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1783 gfc_error
1784 ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1785 "INTENT(IN)", stat->symtree->n.sym->name);
1786 goto cleanup;
1789 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1791 gfc_error
1792 ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1793 "procedure");
1794 goto cleanup;
1797 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1799 gfc_error("STAT expression at %C must be a variable");
1800 goto cleanup;
1803 gfc_check_do_variable(stat->symtree);
1806 if (gfc_match (" )%t") != MATCH_YES)
1807 goto syntax;
1809 new_st.op = EXEC_ALLOCATE;
1810 new_st.expr = stat;
1811 new_st.ext.alloc_list = head;
1813 return MATCH_YES;
1815 syntax:
1816 gfc_syntax_error (ST_ALLOCATE);
1818 cleanup:
1819 gfc_free_expr (stat);
1820 gfc_free_alloc_list (head);
1821 return MATCH_ERROR;
1825 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1826 a set of pointer assignments to intrinsic NULL(). */
1828 match
1829 gfc_match_nullify (void)
1831 gfc_code *tail;
1832 gfc_expr *e, *p;
1833 match m;
1835 tail = NULL;
1837 if (gfc_match_char ('(') != MATCH_YES)
1838 goto syntax;
1840 for (;;)
1842 m = gfc_match_variable (&p, 0);
1843 if (m == MATCH_ERROR)
1844 goto cleanup;
1845 if (m == MATCH_NO)
1846 goto syntax;
1848 if (gfc_check_do_variable(p->symtree))
1849 goto cleanup;
1851 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1853 gfc_error
1854 ("Illegal variable in NULLIFY at %C for a PURE procedure");
1855 goto cleanup;
1858 /* build ' => NULL() ' */
1859 e = gfc_get_expr ();
1860 e->where = gfc_current_locus;
1861 e->expr_type = EXPR_NULL;
1862 e->ts.type = BT_UNKNOWN;
1864 /* Chain to list */
1865 if (tail == NULL)
1866 tail = &new_st;
1867 else
1869 tail->next = gfc_get_code ();
1870 tail = tail->next;
1873 tail->op = EXEC_POINTER_ASSIGN;
1874 tail->expr = p;
1875 tail->expr2 = e;
1877 if (gfc_match (" )%t") == MATCH_YES)
1878 break;
1879 if (gfc_match_char (',') != MATCH_YES)
1880 goto syntax;
1883 return MATCH_YES;
1885 syntax:
1886 gfc_syntax_error (ST_NULLIFY);
1888 cleanup:
1889 gfc_free_statements (tail);
1890 return MATCH_ERROR;
1894 /* Match a DEALLOCATE statement. */
1896 match
1897 gfc_match_deallocate (void)
1899 gfc_alloc *head, *tail;
1900 gfc_expr *stat;
1901 match m;
1903 head = tail = NULL;
1904 stat = NULL;
1906 if (gfc_match_char ('(') != MATCH_YES)
1907 goto syntax;
1909 for (;;)
1911 if (head == NULL)
1912 head = tail = gfc_get_alloc ();
1913 else
1915 tail->next = gfc_get_alloc ();
1916 tail = tail->next;
1919 m = gfc_match_variable (&tail->expr, 0);
1920 if (m == MATCH_ERROR)
1921 goto cleanup;
1922 if (m == MATCH_NO)
1923 goto syntax;
1925 if (gfc_check_do_variable (tail->expr->symtree))
1926 goto cleanup;
1928 if (gfc_pure (NULL)
1929 && gfc_impure_variable (tail->expr->symtree->n.sym))
1931 gfc_error
1932 ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1933 "procedure");
1934 goto cleanup;
1937 if (gfc_match_char (',') != MATCH_YES)
1938 break;
1940 m = gfc_match (" stat = %v", &stat);
1941 if (m == MATCH_ERROR)
1942 goto cleanup;
1943 if (m == MATCH_YES)
1944 break;
1947 if (stat != NULL)
1949 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1951 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1952 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
1953 goto cleanup;
1956 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
1958 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
1959 "for a PURE procedure");
1960 goto cleanup;
1963 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1965 gfc_error("STAT expression at %C must be a variable");
1966 goto cleanup;
1969 gfc_check_do_variable(stat->symtree);
1972 if (gfc_match (" )%t") != MATCH_YES)
1973 goto syntax;
1975 new_st.op = EXEC_DEALLOCATE;
1976 new_st.expr = stat;
1977 new_st.ext.alloc_list = head;
1979 return MATCH_YES;
1981 syntax:
1982 gfc_syntax_error (ST_DEALLOCATE);
1984 cleanup:
1985 gfc_free_expr (stat);
1986 gfc_free_alloc_list (head);
1987 return MATCH_ERROR;
1991 /* Match a RETURN statement. */
1993 match
1994 gfc_match_return (void)
1996 gfc_expr *e;
1997 match m;
1998 gfc_compile_state s;
1999 int c;
2001 e = NULL;
2002 if (gfc_match_eos () == MATCH_YES)
2003 goto done;
2005 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2007 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2008 "a SUBROUTINE");
2009 goto cleanup;
2012 if (gfc_current_form == FORM_FREE)
2014 /* The following are valid, so we can't require a blank after the
2015 RETURN keyword:
2016 return+1
2017 return(1) */
2018 c = gfc_peek_char ();
2019 if (ISALPHA (c) || ISDIGIT (c))
2020 return MATCH_NO;
2023 m = gfc_match (" %e%t", &e);
2024 if (m == MATCH_YES)
2025 goto done;
2026 if (m == MATCH_ERROR)
2027 goto cleanup;
2029 gfc_syntax_error (ST_RETURN);
2031 cleanup:
2032 gfc_free_expr (e);
2033 return MATCH_ERROR;
2035 done:
2036 gfc_enclosing_unit (&s);
2037 if (s == COMP_PROGRAM
2038 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2039 "main program at %C") == FAILURE)
2040 return MATCH_ERROR;
2042 new_st.op = EXEC_RETURN;
2043 new_st.expr = e;
2045 return MATCH_YES;
2049 /* Match a CALL statement. The tricky part here are possible
2050 alternate return specifiers. We handle these by having all
2051 "subroutines" actually return an integer via a register that gives
2052 the return number. If the call specifies alternate returns, we
2053 generate code for a SELECT statement whose case clauses contain
2054 GOTOs to the various labels. */
2056 match
2057 gfc_match_call (void)
2059 char name[GFC_MAX_SYMBOL_LEN + 1];
2060 gfc_actual_arglist *a, *arglist;
2061 gfc_case *new_case;
2062 gfc_symbol *sym;
2063 gfc_symtree *st;
2064 gfc_code *c;
2065 match m;
2066 int i;
2068 arglist = NULL;
2070 m = gfc_match ("% %n", name);
2071 if (m == MATCH_NO)
2072 goto syntax;
2073 if (m != MATCH_YES)
2074 return m;
2076 if (gfc_get_ha_sym_tree (name, &st))
2077 return MATCH_ERROR;
2079 sym = st->n.sym;
2080 gfc_set_sym_referenced (sym);
2082 if (!sym->attr.generic
2083 && !sym->attr.subroutine
2084 && gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2085 return MATCH_ERROR;
2087 if (gfc_match_eos () != MATCH_YES)
2089 m = gfc_match_actual_arglist (1, &arglist);
2090 if (m == MATCH_NO)
2091 goto syntax;
2092 if (m == MATCH_ERROR)
2093 goto cleanup;
2095 if (gfc_match_eos () != MATCH_YES)
2096 goto syntax;
2099 /* If any alternate return labels were found, construct a SELECT
2100 statement that will jump to the right place. */
2102 i = 0;
2103 for (a = arglist; a; a = a->next)
2104 if (a->expr == NULL)
2105 i = 1;
2107 if (i)
2109 gfc_symtree *select_st;
2110 gfc_symbol *select_sym;
2111 char name[GFC_MAX_SYMBOL_LEN + 1];
2113 new_st.next = c = gfc_get_code ();
2114 c->op = EXEC_SELECT;
2115 sprintf (name, "_result_%s",sym->name);
2116 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail */
2118 select_sym = select_st->n.sym;
2119 select_sym->ts.type = BT_INTEGER;
2120 select_sym->ts.kind = gfc_default_integer_kind;
2121 gfc_set_sym_referenced (select_sym);
2122 c->expr = gfc_get_expr ();
2123 c->expr->expr_type = EXPR_VARIABLE;
2124 c->expr->symtree = select_st;
2125 c->expr->ts = select_sym->ts;
2126 c->expr->where = gfc_current_locus;
2128 i = 0;
2129 for (a = arglist; a; a = a->next)
2131 if (a->expr != NULL)
2132 continue;
2134 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2135 continue;
2137 i++;
2139 c->block = gfc_get_code ();
2140 c = c->block;
2141 c->op = EXEC_SELECT;
2143 new_case = gfc_get_case ();
2144 new_case->high = new_case->low = gfc_int_expr (i);
2145 c->ext.case_list = new_case;
2147 c->next = gfc_get_code ();
2148 c->next->op = EXEC_GOTO;
2149 c->next->label = a->label;
2153 new_st.op = EXEC_CALL;
2154 new_st.symtree = st;
2155 new_st.ext.actual = arglist;
2157 return MATCH_YES;
2159 syntax:
2160 gfc_syntax_error (ST_CALL);
2162 cleanup:
2163 gfc_free_actual_arglist (arglist);
2164 return MATCH_ERROR;
2168 /* Given a name, return a pointer to the common head structure,
2169 creating it if it does not exist. If FROM_MODULE is nonzero, we
2170 mangle the name so that it doesn't interfere with commons defined
2171 in the using namespace.
2172 TODO: Add to global symbol tree. */
2174 gfc_common_head *
2175 gfc_get_common (const char *name, int from_module)
2177 gfc_symtree *st;
2178 static int serial = 0;
2179 char mangled_name[GFC_MAX_SYMBOL_LEN+1];
2181 if (from_module)
2183 /* A use associated common block is only needed to correctly layout
2184 the variables it contains. */
2185 snprintf(mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2186 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2188 else
2190 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2192 if (st == NULL)
2193 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2196 if (st->n.common == NULL)
2198 st->n.common = gfc_get_common_head ();
2199 st->n.common->where = gfc_current_locus;
2200 strcpy (st->n.common->name, name);
2203 return st->n.common;
2207 /* Match a common block name. */
2209 static match
2210 match_common_name (char *name)
2212 match m;
2214 if (gfc_match_char ('/') == MATCH_NO)
2216 name[0] = '\0';
2217 return MATCH_YES;
2220 if (gfc_match_char ('/') == MATCH_YES)
2222 name[0] = '\0';
2223 return MATCH_YES;
2226 m = gfc_match_name (name);
2228 if (m == MATCH_ERROR)
2229 return MATCH_ERROR;
2230 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2231 return MATCH_YES;
2233 gfc_error ("Syntax error in common block name at %C");
2234 return MATCH_ERROR;
2238 /* Match a COMMON statement. */
2240 match
2241 gfc_match_common (void)
2243 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2244 char name[GFC_MAX_SYMBOL_LEN+1];
2245 gfc_common_head *t;
2246 gfc_array_spec *as;
2247 gfc_equiv * e1, * e2;
2248 match m;
2250 old_blank_common = gfc_current_ns->blank_common.head;
2251 if (old_blank_common)
2253 while (old_blank_common->common_next)
2254 old_blank_common = old_blank_common->common_next;
2257 as = NULL;
2259 for (;;)
2261 m = match_common_name (name);
2262 if (m == MATCH_ERROR)
2263 goto cleanup;
2265 if (name[0] == '\0')
2267 t = &gfc_current_ns->blank_common;
2268 if (t->head == NULL)
2269 t->where = gfc_current_locus;
2270 head = &t->head;
2272 else
2274 t = gfc_get_common (name, 0);
2275 head = &t->head;
2278 if (*head == NULL)
2279 tail = NULL;
2280 else
2282 tail = *head;
2283 while (tail->common_next)
2284 tail = tail->common_next;
2287 /* Grab the list of symbols. */
2288 for (;;)
2290 m = gfc_match_symbol (&sym, 0);
2291 if (m == MATCH_ERROR)
2292 goto cleanup;
2293 if (m == MATCH_NO)
2294 goto syntax;
2296 if (sym->attr.in_common)
2298 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2299 sym->name);
2300 goto cleanup;
2303 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2304 goto cleanup;
2306 if (sym->value != NULL
2307 && (name[0] == '\0' || !sym->attr.data))
2309 if (name[0] == '\0')
2310 gfc_error ("Previously initialized symbol '%s' in "
2311 "blank COMMON block at %C", sym->name);
2312 else
2313 gfc_error ("Previously initialized symbol '%s' in "
2314 "COMMON block '%s' at %C", sym->name, name);
2315 goto cleanup;
2318 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2319 goto cleanup;
2321 /* Derived type names must have the SEQUENCE attribute. */
2322 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2324 gfc_error
2325 ("Derived type variable in COMMON at %C does not have the "
2326 "SEQUENCE attribute");
2327 goto cleanup;
2330 if (tail != NULL)
2331 tail->common_next = sym;
2332 else
2333 *head = sym;
2335 tail = sym;
2337 /* Deal with an optional array specification after the
2338 symbol name. */
2339 m = gfc_match_array_spec (&as);
2340 if (m == MATCH_ERROR)
2341 goto cleanup;
2343 if (m == MATCH_YES)
2345 if (as->type != AS_EXPLICIT)
2347 gfc_error
2348 ("Array specification for symbol '%s' in COMMON at %C "
2349 "must be explicit", sym->name);
2350 goto cleanup;
2353 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2354 goto cleanup;
2356 if (sym->attr.pointer)
2358 gfc_error
2359 ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2360 sym->name);
2361 goto cleanup;
2364 sym->as = as;
2365 as = NULL;
2369 sym->common_head = t;
2371 /* Check to see if the symbol is already in an equivalence group.
2372 If it is, set the other members as being in common. */
2373 if (sym->attr.in_equivalence)
2375 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2377 for (e2 = e1; e2; e2 = e2->eq)
2378 if (e2->expr->symtree->n.sym == sym)
2379 goto equiv_found;
2381 continue;
2383 equiv_found:
2385 for (e2 = e1; e2; e2 = e2->eq)
2387 other = e2->expr->symtree->n.sym;
2388 if (other->common_head
2389 && other->common_head != sym->common_head)
2391 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2392 "%C is being indirectly equivalenced to "
2393 "another COMMON block '%s'",
2394 sym->name,
2395 sym->common_head->name,
2396 other->common_head->name);
2397 goto cleanup;
2399 other->attr.in_common = 1;
2400 other->common_head = t;
2406 gfc_gobble_whitespace ();
2407 if (gfc_match_eos () == MATCH_YES)
2408 goto done;
2409 if (gfc_peek_char () == '/')
2410 break;
2411 if (gfc_match_char (',') != MATCH_YES)
2412 goto syntax;
2413 gfc_gobble_whitespace ();
2414 if (gfc_peek_char () == '/')
2415 break;
2419 done:
2420 return MATCH_YES;
2422 syntax:
2423 gfc_syntax_error (ST_COMMON);
2425 cleanup:
2426 if (old_blank_common)
2427 old_blank_common->common_next = NULL;
2428 else
2429 gfc_current_ns->blank_common.head = NULL;
2430 gfc_free_array_spec (as);
2431 return MATCH_ERROR;
2435 /* Match a BLOCK DATA program unit. */
2437 match
2438 gfc_match_block_data (void)
2440 char name[GFC_MAX_SYMBOL_LEN + 1];
2441 gfc_symbol *sym;
2442 match m;
2444 if (gfc_match_eos () == MATCH_YES)
2446 gfc_new_block = NULL;
2447 return MATCH_YES;
2450 m = gfc_match ("% %n%t", name);
2451 if (m != MATCH_YES)
2452 return MATCH_ERROR;
2454 if (gfc_get_symbol (name, NULL, &sym))
2455 return MATCH_ERROR;
2457 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2458 return MATCH_ERROR;
2460 gfc_new_block = sym;
2462 return MATCH_YES;
2466 /* Free a namelist structure. */
2468 void
2469 gfc_free_namelist (gfc_namelist * name)
2471 gfc_namelist *n;
2473 for (; name; name = n)
2475 n = name->next;
2476 gfc_free (name);
2481 /* Match a NAMELIST statement. */
2483 match
2484 gfc_match_namelist (void)
2486 gfc_symbol *group_name, *sym;
2487 gfc_namelist *nl;
2488 match m, m2;
2490 m = gfc_match (" / %s /", &group_name);
2491 if (m == MATCH_NO)
2492 goto syntax;
2493 if (m == MATCH_ERROR)
2494 goto error;
2496 for (;;)
2498 if (group_name->ts.type != BT_UNKNOWN)
2500 gfc_error
2501 ("Namelist group name '%s' at %C already has a basic type "
2502 "of %s", group_name->name, gfc_typename (&group_name->ts));
2503 return MATCH_ERROR;
2506 if (group_name->attr.flavor != FL_NAMELIST
2507 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2508 group_name->name, NULL) == FAILURE)
2509 return MATCH_ERROR;
2511 for (;;)
2513 m = gfc_match_symbol (&sym, 1);
2514 if (m == MATCH_NO)
2515 goto syntax;
2516 if (m == MATCH_ERROR)
2517 goto error;
2519 if (sym->attr.in_namelist == 0
2520 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2521 goto error;
2523 nl = gfc_get_namelist ();
2524 nl->sym = sym;
2526 if (group_name->namelist == NULL)
2527 group_name->namelist = group_name->namelist_tail = nl;
2528 else
2530 group_name->namelist_tail->next = nl;
2531 group_name->namelist_tail = nl;
2534 if (gfc_match_eos () == MATCH_YES)
2535 goto done;
2537 m = gfc_match_char (',');
2539 if (gfc_match_char ('/') == MATCH_YES)
2541 m2 = gfc_match (" %s /", &group_name);
2542 if (m2 == MATCH_YES)
2543 break;
2544 if (m2 == MATCH_ERROR)
2545 goto error;
2546 goto syntax;
2549 if (m != MATCH_YES)
2550 goto syntax;
2554 done:
2555 return MATCH_YES;
2557 syntax:
2558 gfc_syntax_error (ST_NAMELIST);
2560 error:
2561 return MATCH_ERROR;
2565 /* Match a MODULE statement. */
2567 match
2568 gfc_match_module (void)
2570 match m;
2572 m = gfc_match (" %s%t", &gfc_new_block);
2573 if (m != MATCH_YES)
2574 return m;
2576 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2577 gfc_new_block->name, NULL) == FAILURE)
2578 return MATCH_ERROR;
2580 return MATCH_YES;
2584 /* Free equivalence sets and lists. Recursively is the easiest way to
2585 do this. */
2587 void
2588 gfc_free_equiv (gfc_equiv * eq)
2591 if (eq == NULL)
2592 return;
2594 gfc_free_equiv (eq->eq);
2595 gfc_free_equiv (eq->next);
2597 gfc_free_expr (eq->expr);
2598 gfc_free (eq);
2602 /* Match an EQUIVALENCE statement. */
2604 match
2605 gfc_match_equivalence (void)
2607 gfc_equiv *eq, *set, *tail;
2608 gfc_ref *ref;
2609 gfc_symbol *sym;
2610 match m;
2611 gfc_common_head *common_head = NULL;
2612 bool common_flag;
2613 int cnt;
2615 tail = NULL;
2617 for (;;)
2619 eq = gfc_get_equiv ();
2620 if (tail == NULL)
2621 tail = eq;
2623 eq->next = gfc_current_ns->equiv;
2624 gfc_current_ns->equiv = eq;
2626 if (gfc_match_char ('(') != MATCH_YES)
2627 goto syntax;
2629 set = eq;
2630 common_flag = FALSE;
2631 cnt = 0;
2633 for (;;)
2635 m = gfc_match_equiv_variable (&set->expr);
2636 if (m == MATCH_ERROR)
2637 goto cleanup;
2638 if (m == MATCH_NO)
2639 goto syntax;
2641 /* count the number of objects. */
2642 cnt++;
2644 if (gfc_match_char ('%') == MATCH_YES)
2646 gfc_error ("Derived type component %C is not a "
2647 "permitted EQUIVALENCE member");
2648 goto cleanup;
2651 for (ref = set->expr->ref; ref; ref = ref->next)
2652 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2654 gfc_error
2655 ("Array reference in EQUIVALENCE at %C cannot be an "
2656 "array section");
2657 goto cleanup;
2660 sym = set->expr->symtree->n.sym;
2662 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL)
2663 == FAILURE)
2664 goto cleanup;
2666 if (sym->attr.in_common)
2668 common_flag = TRUE;
2669 common_head = sym->common_head;
2672 if (gfc_match_char (')') == MATCH_YES)
2673 break;
2675 if (gfc_match_char (',') != MATCH_YES)
2676 goto syntax;
2678 set->eq = gfc_get_equiv ();
2679 set = set->eq;
2682 if (cnt < 2)
2684 gfc_error ("EQUIVALENCE at %C requires two or more objects");
2685 goto cleanup;
2688 /* If one of the members of an equivalence is in common, then
2689 mark them all as being in common. Before doing this, check
2690 that members of the equivalence group are not in different
2691 common blocks. */
2692 if (common_flag)
2693 for (set = eq; set; set = set->eq)
2695 sym = set->expr->symtree->n.sym;
2696 if (sym->common_head && sym->common_head != common_head)
2698 gfc_error ("Attempt to indirectly overlap COMMON "
2699 "blocks %s and %s by EQUIVALENCE at %C",
2700 sym->common_head->name,
2701 common_head->name);
2702 goto cleanup;
2704 sym->attr.in_common = 1;
2705 sym->common_head = common_head;
2708 if (gfc_match_eos () == MATCH_YES)
2709 break;
2710 if (gfc_match_char (',') != MATCH_YES)
2711 goto syntax;
2714 return MATCH_YES;
2716 syntax:
2717 gfc_syntax_error (ST_EQUIVALENCE);
2719 cleanup:
2720 eq = tail->next;
2721 tail->next = NULL;
2723 gfc_free_equiv (gfc_current_ns->equiv);
2724 gfc_current_ns->equiv = eq;
2726 return MATCH_ERROR;
2729 /* Check that a statement function is not recursive. This is done by looking
2730 for the statement function symbol(sym) by looking recursively through its
2731 expression(e). If a reference to sym is found, true is returned. */
2732 static bool
2733 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
2735 gfc_actual_arglist *arg;
2736 gfc_ref *ref;
2737 int i;
2739 if (e == NULL)
2740 return false;
2742 switch (e->expr_type)
2744 case EXPR_FUNCTION:
2745 for (arg = e->value.function.actual; arg; arg = arg->next)
2747 if (sym->name == arg->name
2748 || recursive_stmt_fcn (arg->expr, sym))
2749 return true;
2752 if (e->symtree == NULL)
2753 return false;
2755 /* Check the name before testing for nested recursion! */
2756 if (sym->name == e->symtree->n.sym->name)
2757 return true;
2759 /* Catch recursion via other statement functions. */
2760 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
2761 && e->symtree->n.sym->value
2762 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
2763 return true;
2765 break;
2767 case EXPR_VARIABLE:
2768 if (e->symtree && sym->name == e->symtree->n.sym->name)
2769 return true;
2770 break;
2772 case EXPR_OP:
2773 if (recursive_stmt_fcn (e->value.op.op1, sym)
2774 || recursive_stmt_fcn (e->value.op.op2, sym))
2775 return true;
2776 break;
2778 default:
2779 break;
2782 /* Component references do not need to be checked. */
2783 if (e->ref)
2785 for (ref = e->ref; ref; ref = ref->next)
2787 switch (ref->type)
2789 case REF_ARRAY:
2790 for (i = 0; i < ref->u.ar.dimen; i++)
2792 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
2793 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
2794 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
2795 return true;
2797 break;
2799 case REF_SUBSTRING:
2800 if (recursive_stmt_fcn (ref->u.ss.start, sym)
2801 || recursive_stmt_fcn (ref->u.ss.end, sym))
2802 return true;
2804 break;
2806 default:
2807 break;
2811 return false;
2815 /* Match a statement function declaration. It is so easy to match
2816 non-statement function statements with a MATCH_ERROR as opposed to
2817 MATCH_NO that we suppress error message in most cases. */
2819 match
2820 gfc_match_st_function (void)
2822 gfc_error_buf old_error;
2823 gfc_symbol *sym;
2824 gfc_expr *expr;
2825 match m;
2827 m = gfc_match_symbol (&sym, 0);
2828 if (m != MATCH_YES)
2829 return m;
2831 gfc_push_error (&old_error);
2833 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
2834 sym->name, NULL) == FAILURE)
2835 goto undo_error;
2837 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2838 goto undo_error;
2840 m = gfc_match (" = %e%t", &expr);
2841 if (m == MATCH_NO)
2842 goto undo_error;
2844 gfc_free_error (&old_error);
2845 if (m == MATCH_ERROR)
2846 return m;
2848 if (recursive_stmt_fcn (expr, sym))
2850 gfc_error ("Statement function at %L is recursive",
2851 &expr->where);
2852 return MATCH_ERROR;
2855 sym->value = expr;
2857 return MATCH_YES;
2859 undo_error:
2860 gfc_pop_error (&old_error);
2861 return MATCH_NO;
2865 /***************** SELECT CASE subroutines ******************/
2867 /* Free a single case structure. */
2869 static void
2870 free_case (gfc_case * p)
2872 if (p->low == p->high)
2873 p->high = NULL;
2874 gfc_free_expr (p->low);
2875 gfc_free_expr (p->high);
2876 gfc_free (p);
2880 /* Free a list of case structures. */
2882 void
2883 gfc_free_case_list (gfc_case * p)
2885 gfc_case *q;
2887 for (; p; p = q)
2889 q = p->next;
2890 free_case (p);
2895 /* Match a single case selector. */
2897 static match
2898 match_case_selector (gfc_case ** cp)
2900 gfc_case *c;
2901 match m;
2903 c = gfc_get_case ();
2904 c->where = gfc_current_locus;
2906 if (gfc_match_char (':') == MATCH_YES)
2908 m = gfc_match_init_expr (&c->high);
2909 if (m == MATCH_NO)
2910 goto need_expr;
2911 if (m == MATCH_ERROR)
2912 goto cleanup;
2915 else
2917 m = gfc_match_init_expr (&c->low);
2918 if (m == MATCH_ERROR)
2919 goto cleanup;
2920 if (m == MATCH_NO)
2921 goto need_expr;
2923 /* If we're not looking at a ':' now, make a range out of a single
2924 target. Else get the upper bound for the case range. */
2925 if (gfc_match_char (':') != MATCH_YES)
2926 c->high = c->low;
2927 else
2929 m = gfc_match_init_expr (&c->high);
2930 if (m == MATCH_ERROR)
2931 goto cleanup;
2932 /* MATCH_NO is fine. It's OK if nothing is there! */
2936 *cp = c;
2937 return MATCH_YES;
2939 need_expr:
2940 gfc_error ("Expected initialization expression in CASE at %C");
2942 cleanup:
2943 free_case (c);
2944 return MATCH_ERROR;
2948 /* Match the end of a case statement. */
2950 static match
2951 match_case_eos (void)
2953 char name[GFC_MAX_SYMBOL_LEN + 1];
2954 match m;
2956 if (gfc_match_eos () == MATCH_YES)
2957 return MATCH_YES;
2959 gfc_gobble_whitespace ();
2961 m = gfc_match_name (name);
2962 if (m != MATCH_YES)
2963 return m;
2965 if (strcmp (name, gfc_current_block ()->name) != 0)
2967 gfc_error ("Expected case name of '%s' at %C",
2968 gfc_current_block ()->name);
2969 return MATCH_ERROR;
2972 return gfc_match_eos ();
2976 /* Match a SELECT statement. */
2978 match
2979 gfc_match_select (void)
2981 gfc_expr *expr;
2982 match m;
2984 m = gfc_match_label ();
2985 if (m == MATCH_ERROR)
2986 return m;
2988 m = gfc_match (" select case ( %e )%t", &expr);
2989 if (m != MATCH_YES)
2990 return m;
2992 new_st.op = EXEC_SELECT;
2993 new_st.expr = expr;
2995 return MATCH_YES;
2999 /* Match a CASE statement. */
3001 match
3002 gfc_match_case (void)
3004 gfc_case *c, *head, *tail;
3005 match m;
3007 head = tail = NULL;
3009 if (gfc_current_state () != COMP_SELECT)
3011 gfc_error ("Unexpected CASE statement at %C");
3012 return MATCH_ERROR;
3015 if (gfc_match ("% default") == MATCH_YES)
3017 m = match_case_eos ();
3018 if (m == MATCH_NO)
3019 goto syntax;
3020 if (m == MATCH_ERROR)
3021 goto cleanup;
3023 new_st.op = EXEC_SELECT;
3024 c = gfc_get_case ();
3025 c->where = gfc_current_locus;
3026 new_st.ext.case_list = c;
3027 return MATCH_YES;
3030 if (gfc_match_char ('(') != MATCH_YES)
3031 goto syntax;
3033 for (;;)
3035 if (match_case_selector (&c) == MATCH_ERROR)
3036 goto cleanup;
3038 if (head == NULL)
3039 head = c;
3040 else
3041 tail->next = c;
3043 tail = c;
3045 if (gfc_match_char (')') == MATCH_YES)
3046 break;
3047 if (gfc_match_char (',') != MATCH_YES)
3048 goto syntax;
3051 m = match_case_eos ();
3052 if (m == MATCH_NO)
3053 goto syntax;
3054 if (m == MATCH_ERROR)
3055 goto cleanup;
3057 new_st.op = EXEC_SELECT;
3058 new_st.ext.case_list = head;
3060 return MATCH_YES;
3062 syntax:
3063 gfc_error ("Syntax error in CASE-specification at %C");
3065 cleanup:
3066 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3067 return MATCH_ERROR;
3070 /********************* WHERE subroutines ********************/
3072 /* Match the rest of a simple WHERE statement that follows an IF statement.
3075 static match
3076 match_simple_where (void)
3078 gfc_expr *expr;
3079 gfc_code *c;
3080 match m;
3082 m = gfc_match (" ( %e )", &expr);
3083 if (m != MATCH_YES)
3084 return m;
3086 m = gfc_match_assignment ();
3087 if (m == MATCH_NO)
3088 goto syntax;
3089 if (m == MATCH_ERROR)
3090 goto cleanup;
3092 if (gfc_match_eos () != MATCH_YES)
3093 goto syntax;
3095 c = gfc_get_code ();
3097 c->op = EXEC_WHERE;
3098 c->expr = expr;
3099 c->next = gfc_get_code ();
3101 *c->next = new_st;
3102 gfc_clear_new_st ();
3104 new_st.op = EXEC_WHERE;
3105 new_st.block = c;
3107 return MATCH_YES;
3109 syntax:
3110 gfc_syntax_error (ST_WHERE);
3112 cleanup:
3113 gfc_free_expr (expr);
3114 return MATCH_ERROR;
3117 /* Match a WHERE statement. */
3119 match
3120 gfc_match_where (gfc_statement * st)
3122 gfc_expr *expr;
3123 match m0, m;
3124 gfc_code *c;
3126 m0 = gfc_match_label ();
3127 if (m0 == MATCH_ERROR)
3128 return m0;
3130 m = gfc_match (" where ( %e )", &expr);
3131 if (m != MATCH_YES)
3132 return m;
3134 if (gfc_match_eos () == MATCH_YES)
3136 *st = ST_WHERE_BLOCK;
3138 new_st.op = EXEC_WHERE;
3139 new_st.expr = expr;
3140 return MATCH_YES;
3143 m = gfc_match_assignment ();
3144 if (m == MATCH_NO)
3145 gfc_syntax_error (ST_WHERE);
3147 if (m != MATCH_YES)
3149 gfc_free_expr (expr);
3150 return MATCH_ERROR;
3153 /* We've got a simple WHERE statement. */
3154 *st = ST_WHERE;
3155 c = gfc_get_code ();
3157 c->op = EXEC_WHERE;
3158 c->expr = expr;
3159 c->next = gfc_get_code ();
3161 *c->next = new_st;
3162 gfc_clear_new_st ();
3164 new_st.op = EXEC_WHERE;
3165 new_st.block = c;
3167 return MATCH_YES;
3171 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3172 new_st if successful. */
3174 match
3175 gfc_match_elsewhere (void)
3177 char name[GFC_MAX_SYMBOL_LEN + 1];
3178 gfc_expr *expr;
3179 match m;
3181 if (gfc_current_state () != COMP_WHERE)
3183 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3184 return MATCH_ERROR;
3187 expr = NULL;
3189 if (gfc_match_char ('(') == MATCH_YES)
3191 m = gfc_match_expr (&expr);
3192 if (m == MATCH_NO)
3193 goto syntax;
3194 if (m == MATCH_ERROR)
3195 return MATCH_ERROR;
3197 if (gfc_match_char (')') != MATCH_YES)
3198 goto syntax;
3201 if (gfc_match_eos () != MATCH_YES)
3202 { /* Better be a name at this point */
3203 m = gfc_match_name (name);
3204 if (m == MATCH_NO)
3205 goto syntax;
3206 if (m == MATCH_ERROR)
3207 goto cleanup;
3209 if (gfc_match_eos () != MATCH_YES)
3210 goto syntax;
3212 if (strcmp (name, gfc_current_block ()->name) != 0)
3214 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3215 name, gfc_current_block ()->name);
3216 goto cleanup;
3220 new_st.op = EXEC_WHERE;
3221 new_st.expr = expr;
3222 return MATCH_YES;
3224 syntax:
3225 gfc_syntax_error (ST_ELSEWHERE);
3227 cleanup:
3228 gfc_free_expr (expr);
3229 return MATCH_ERROR;
3233 /******************** FORALL subroutines ********************/
3235 /* Free a list of FORALL iterators. */
3237 void
3238 gfc_free_forall_iterator (gfc_forall_iterator * iter)
3240 gfc_forall_iterator *next;
3242 while (iter)
3244 next = iter->next;
3246 gfc_free_expr (iter->var);
3247 gfc_free_expr (iter->start);
3248 gfc_free_expr (iter->end);
3249 gfc_free_expr (iter->stride);
3251 gfc_free (iter);
3252 iter = next;
3257 /* Match an iterator as part of a FORALL statement. The format is:
3259 <var> = <start>:<end>[:<stride>][, <scalar mask>] */
3261 static match
3262 match_forall_iterator (gfc_forall_iterator ** result)
3264 gfc_forall_iterator *iter;
3265 locus where;
3266 match m;
3268 where = gfc_current_locus;
3269 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3271 m = gfc_match_variable (&iter->var, 0);
3272 if (m != MATCH_YES)
3273 goto cleanup;
3275 if (gfc_match_char ('=') != MATCH_YES)
3277 m = MATCH_NO;
3278 goto cleanup;
3281 m = gfc_match_expr (&iter->start);
3282 if (m != MATCH_YES)
3283 goto cleanup;
3285 if (gfc_match_char (':') != MATCH_YES)
3286 goto syntax;
3288 m = gfc_match_expr (&iter->end);
3289 if (m == MATCH_NO)
3290 goto syntax;
3291 if (m == MATCH_ERROR)
3292 goto cleanup;
3294 if (gfc_match_char (':') == MATCH_NO)
3295 iter->stride = gfc_int_expr (1);
3296 else
3298 m = gfc_match_expr (&iter->stride);
3299 if (m == MATCH_NO)
3300 goto syntax;
3301 if (m == MATCH_ERROR)
3302 goto cleanup;
3305 *result = iter;
3306 return MATCH_YES;
3308 syntax:
3309 gfc_error ("Syntax error in FORALL iterator at %C");
3310 m = MATCH_ERROR;
3312 cleanup:
3313 gfc_current_locus = where;
3314 gfc_free_forall_iterator (iter);
3315 return m;
3319 /* Match the header of a FORALL statement. */
3321 static match
3322 match_forall_header (gfc_forall_iterator ** phead, gfc_expr ** mask)
3324 gfc_forall_iterator *head, *tail, *new;
3325 match m;
3327 gfc_gobble_whitespace ();
3329 head = tail = NULL;
3330 *mask = NULL;
3332 if (gfc_match_char ('(') != MATCH_YES)
3333 return MATCH_NO;
3335 m = match_forall_iterator (&new);
3336 if (m == MATCH_ERROR)
3337 goto cleanup;
3338 if (m == MATCH_NO)
3339 goto syntax;
3341 head = tail = new;
3343 for (;;)
3345 if (gfc_match_char (',') != MATCH_YES)
3346 break;
3348 m = match_forall_iterator (&new);
3349 if (m == MATCH_ERROR)
3350 goto cleanup;
3351 if (m == MATCH_YES)
3353 tail->next = new;
3354 tail = new;
3355 continue;
3358 /* Have to have a mask expression */
3360 m = gfc_match_expr (mask);
3361 if (m == MATCH_NO)
3362 goto syntax;
3363 if (m == MATCH_ERROR)
3364 goto cleanup;
3366 break;
3369 if (gfc_match_char (')') == MATCH_NO)
3370 goto syntax;
3372 *phead = head;
3373 return MATCH_YES;
3375 syntax:
3376 gfc_syntax_error (ST_FORALL);
3378 cleanup:
3379 gfc_free_expr (*mask);
3380 gfc_free_forall_iterator (head);
3382 return MATCH_ERROR;
3385 /* Match the rest of a simple FORALL statement that follows an IF statement.
3388 static match
3389 match_simple_forall (void)
3391 gfc_forall_iterator *head;
3392 gfc_expr *mask;
3393 gfc_code *c;
3394 match m;
3396 mask = NULL;
3397 head = NULL;
3398 c = NULL;
3400 m = match_forall_header (&head, &mask);
3402 if (m == MATCH_NO)
3403 goto syntax;
3404 if (m != MATCH_YES)
3405 goto cleanup;
3407 m = gfc_match_assignment ();
3409 if (m == MATCH_ERROR)
3410 goto cleanup;
3411 if (m == MATCH_NO)
3413 m = gfc_match_pointer_assignment ();
3414 if (m == MATCH_ERROR)
3415 goto cleanup;
3416 if (m == MATCH_NO)
3417 goto syntax;
3420 c = gfc_get_code ();
3421 *c = new_st;
3422 c->loc = gfc_current_locus;
3424 if (gfc_match_eos () != MATCH_YES)
3425 goto syntax;
3427 gfc_clear_new_st ();
3428 new_st.op = EXEC_FORALL;
3429 new_st.expr = mask;
3430 new_st.ext.forall_iterator = head;
3431 new_st.block = gfc_get_code ();
3433 new_st.block->op = EXEC_FORALL;
3434 new_st.block->next = c;
3436 return MATCH_YES;
3438 syntax:
3439 gfc_syntax_error (ST_FORALL);
3441 cleanup:
3442 gfc_free_forall_iterator (head);
3443 gfc_free_expr (mask);
3445 return MATCH_ERROR;
3449 /* Match a FORALL statement. */
3451 match
3452 gfc_match_forall (gfc_statement * st)
3454 gfc_forall_iterator *head;
3455 gfc_expr *mask;
3456 gfc_code *c;
3457 match m0, m;
3459 head = NULL;
3460 mask = NULL;
3461 c = NULL;
3463 m0 = gfc_match_label ();
3464 if (m0 == MATCH_ERROR)
3465 return MATCH_ERROR;
3467 m = gfc_match (" forall");
3468 if (m != MATCH_YES)
3469 return m;
3471 m = match_forall_header (&head, &mask);
3472 if (m == MATCH_ERROR)
3473 goto cleanup;
3474 if (m == MATCH_NO)
3475 goto syntax;
3477 if (gfc_match_eos () == MATCH_YES)
3479 *st = ST_FORALL_BLOCK;
3481 new_st.op = EXEC_FORALL;
3482 new_st.expr = mask;
3483 new_st.ext.forall_iterator = head;
3485 return MATCH_YES;
3488 m = gfc_match_assignment ();
3489 if (m == MATCH_ERROR)
3490 goto cleanup;
3491 if (m == MATCH_NO)
3493 m = gfc_match_pointer_assignment ();
3494 if (m == MATCH_ERROR)
3495 goto cleanup;
3496 if (m == MATCH_NO)
3497 goto syntax;
3500 c = gfc_get_code ();
3501 *c = new_st;
3503 if (gfc_match_eos () != MATCH_YES)
3504 goto syntax;
3506 gfc_clear_new_st ();
3507 new_st.op = EXEC_FORALL;
3508 new_st.expr = mask;
3509 new_st.ext.forall_iterator = head;
3510 new_st.block = gfc_get_code ();
3512 new_st.block->op = EXEC_FORALL;
3513 new_st.block->next = c;
3515 *st = ST_FORALL;
3516 return MATCH_YES;
3518 syntax:
3519 gfc_syntax_error (ST_FORALL);
3521 cleanup:
3522 gfc_free_forall_iterator (head);
3523 gfc_free_expr (mask);
3524 gfc_free_statements (c);
3525 return MATCH_NO;