* Merge with edge-vector-mergepoint-20040918.
[official-gcc.git] / gcc / fortran / match.c
blobb468c8e4be6cee6c6fd46bd52a11a040b9b86d42
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "flags.h"
28 #include <stdarg.h>
29 #include <string.h>
31 #include "gfortran.h"
32 #include "match.h"
33 #include "parse.h"
35 /* For matching and debugging purposes. Order matters here! The
36 unary operators /must/ precede the binary plus and minus, or
37 the expression parser breaks. */
39 mstring intrinsic_operators[] = {
40 minit ("+", INTRINSIC_UPLUS),
41 minit ("-", INTRINSIC_UMINUS),
42 minit ("+", INTRINSIC_PLUS),
43 minit ("-", INTRINSIC_MINUS),
44 minit ("**", INTRINSIC_POWER),
45 minit ("//", INTRINSIC_CONCAT),
46 minit ("*", INTRINSIC_TIMES),
47 minit ("/", INTRINSIC_DIVIDE),
48 minit (".and.", INTRINSIC_AND),
49 minit (".or.", INTRINSIC_OR),
50 minit (".eqv.", INTRINSIC_EQV),
51 minit (".neqv.", INTRINSIC_NEQV),
52 minit (".eq.", INTRINSIC_EQ),
53 minit ("==", INTRINSIC_EQ),
54 minit (".ne.", INTRINSIC_NE),
55 minit ("/=", INTRINSIC_NE),
56 minit (".ge.", INTRINSIC_GE),
57 minit (">=", INTRINSIC_GE),
58 minit (".le.", INTRINSIC_LE),
59 minit ("<=", INTRINSIC_LE),
60 minit (".lt.", INTRINSIC_LT),
61 minit ("<", INTRINSIC_LT),
62 minit (".gt.", INTRINSIC_GT),
63 minit (">", INTRINSIC_GT),
64 minit (".not.", INTRINSIC_NOT),
65 minit (NULL, INTRINSIC_NONE)
69 /******************** Generic matching subroutines ************************/
71 /* In free form, match at least one space. Always matches in fixed
72 form. */
74 match
75 gfc_match_space (void)
77 locus old_loc;
78 int c;
80 if (gfc_current_form == FORM_FIXED)
81 return MATCH_YES;
83 old_loc = gfc_current_locus;
85 c = gfc_next_char ();
86 if (!gfc_is_whitespace (c))
88 gfc_current_locus = old_loc;
89 return MATCH_NO;
92 gfc_gobble_whitespace ();
94 return MATCH_YES;
98 /* Match an end of statement. End of statement is optional
99 whitespace, followed by a ';' or '\n' or comment '!'. If a
100 semicolon is found, we continue to eat whitespace and semicolons. */
102 match
103 gfc_match_eos (void)
105 locus old_loc;
106 int flag, c;
108 flag = 0;
110 for (;;)
112 old_loc = gfc_current_locus;
113 gfc_gobble_whitespace ();
115 c = gfc_next_char ();
116 switch (c)
118 case '!':
121 c = gfc_next_char ();
123 while (c != '\n');
125 /* Fall through */
127 case '\n':
128 return MATCH_YES;
130 case ';':
131 flag = 1;
132 continue;
135 break;
138 gfc_current_locus = old_loc;
139 return (flag) ? MATCH_YES : MATCH_NO;
143 /* Match a literal integer on the input, setting the value on
144 MATCH_YES. Literal ints occur in kind-parameters as well as
145 old-style character length specifications. */
147 match
148 gfc_match_small_literal_int (int *value)
150 locus old_loc;
151 char c;
152 int i;
154 old_loc = gfc_current_locus;
156 gfc_gobble_whitespace ();
157 c = gfc_next_char ();
159 if (!ISDIGIT (c))
161 gfc_current_locus = old_loc;
162 return MATCH_NO;
165 i = c - '0';
167 for (;;)
169 old_loc = gfc_current_locus;
170 c = gfc_next_char ();
172 if (!ISDIGIT (c))
173 break;
175 i = 10 * i + c - '0';
177 if (i > 99999999)
179 gfc_error ("Integer too large at %C");
180 return MATCH_ERROR;
184 gfc_current_locus = old_loc;
186 *value = i;
187 return MATCH_YES;
191 /* Match a small, constant integer expression, like in a kind
192 statement. On MATCH_YES, 'value' is set. */
194 match
195 gfc_match_small_int (int *value)
197 gfc_expr *expr;
198 const char *p;
199 match m;
200 int i;
202 m = gfc_match_expr (&expr);
203 if (m != MATCH_YES)
204 return m;
206 p = gfc_extract_int (expr, &i);
207 gfc_free_expr (expr);
209 if (p != NULL)
211 gfc_error (p);
212 m = MATCH_ERROR;
215 *value = i;
216 return m;
220 /* Matches a statement label. Uses gfc_match_small_literal_int() to
221 do most of the work. */
223 match
224 gfc_match_st_label (gfc_st_label ** label, int allow_zero)
226 locus old_loc;
227 match m;
228 int i;
230 old_loc = gfc_current_locus;
232 m = gfc_match_small_literal_int (&i);
233 if (m != MATCH_YES)
234 return m;
236 if (((i == 0) && allow_zero) || i <= 99999)
238 *label = gfc_get_st_label (i);
239 return MATCH_YES;
242 gfc_error ("Statement label at %C is out of range");
243 gfc_current_locus = old_loc;
244 return MATCH_ERROR;
248 /* Match and validate a label associated with a named IF, DO or SELECT
249 statement. If the symbol does not have the label attribute, we add
250 it. We also make sure the symbol does not refer to another
251 (active) block. A matched label is pointed to by gfc_new_block. */
253 match
254 gfc_match_label (void)
256 char name[GFC_MAX_SYMBOL_LEN + 1];
257 gfc_state_data *p;
258 match m;
260 gfc_new_block = NULL;
262 m = gfc_match (" %n :", name);
263 if (m != MATCH_YES)
264 return m;
266 if (gfc_get_symbol (name, NULL, &gfc_new_block))
268 gfc_error ("Label name '%s' at %C is ambiguous", name);
269 return MATCH_ERROR;
272 if (gfc_new_block->attr.flavor != FL_LABEL
273 && gfc_add_flavor (&gfc_new_block->attr, FL_LABEL, NULL) == FAILURE)
274 return MATCH_ERROR;
276 for (p = gfc_state_stack; p; p = p->previous)
277 if (p->sym == gfc_new_block)
279 gfc_error ("Label %s at %C already in use by a parent block",
280 gfc_new_block->name);
281 return MATCH_ERROR;
284 return MATCH_YES;
288 /* Try and match the input against an array of possibilities. If one
289 potential matching string is a substring of another, the longest
290 match takes precedence. Spaces in the target strings are optional
291 spaces that do not necessarily have to be found in the input
292 stream. In fixed mode, spaces never appear. If whitespace is
293 matched, it matches unlimited whitespace in the input. For this
294 reason, the 'mp' member of the mstring structure is used to track
295 the progress of each potential match.
297 If there is no match we return the tag associated with the
298 terminating NULL mstring structure and leave the locus pointer
299 where it started. If there is a match we return the tag member of
300 the matched mstring and leave the locus pointer after the matched
301 character.
303 A '%' character is a mandatory space. */
306 gfc_match_strings (mstring * a)
308 mstring *p, *best_match;
309 int no_match, c, possibles;
310 locus match_loc;
312 possibles = 0;
314 for (p = a; p->string != NULL; p++)
316 p->mp = p->string;
317 possibles++;
320 no_match = p->tag;
322 best_match = NULL;
323 match_loc = gfc_current_locus;
325 gfc_gobble_whitespace ();
327 while (possibles > 0)
329 c = gfc_next_char ();
331 /* Apply the next character to the current possibilities. */
332 for (p = a; p->string != NULL; p++)
334 if (p->mp == NULL)
335 continue;
337 if (*p->mp == ' ')
339 /* Space matches 1+ whitespace(s). */
340 if ((gfc_current_form == FORM_FREE)
341 && gfc_is_whitespace (c))
342 continue;
344 p->mp++;
347 if (*p->mp != c)
349 /* Match failed. */
350 p->mp = NULL;
351 possibles--;
352 continue;
355 p->mp++;
356 if (*p->mp == '\0')
358 /* Found a match. */
359 match_loc = gfc_current_locus;
360 best_match = p;
361 possibles--;
362 p->mp = NULL;
367 gfc_current_locus = match_loc;
369 return (best_match == NULL) ? no_match : best_match->tag;
373 /* See if the current input looks like a name of some sort. Modifies
374 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long. */
376 match
377 gfc_match_name (char *buffer)
379 locus old_loc;
380 int i, c;
382 old_loc = gfc_current_locus;
383 gfc_gobble_whitespace ();
385 c = gfc_next_char ();
386 if (!ISALPHA (c))
388 gfc_current_locus = old_loc;
389 return MATCH_NO;
392 i = 0;
396 buffer[i++] = c;
398 if (i > gfc_option.max_identifier_length)
400 gfc_error ("Name at %C is too long");
401 return MATCH_ERROR;
404 old_loc = gfc_current_locus;
405 c = gfc_next_char ();
407 while (ISALNUM (c)
408 || c == '_'
409 || (gfc_option.flag_dollar_ok && c == '$'));
411 buffer[i] = '\0';
412 gfc_current_locus = old_loc;
414 return MATCH_YES;
418 /* Match a symbol on the input. Modifies the pointer to the symbol
419 pointer if successful. */
421 match
422 gfc_match_sym_tree (gfc_symtree ** matched_symbol, int host_assoc)
424 char buffer[GFC_MAX_SYMBOL_LEN + 1];
425 match m;
427 m = gfc_match_name (buffer);
428 if (m != MATCH_YES)
429 return m;
431 if (host_assoc)
432 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
433 ? MATCH_ERROR : MATCH_YES;
435 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
436 return MATCH_ERROR;
438 return MATCH_YES;
442 match
443 gfc_match_symbol (gfc_symbol ** matched_symbol, int host_assoc)
445 gfc_symtree *st;
446 match m;
448 m = gfc_match_sym_tree (&st, host_assoc);
450 if (m == MATCH_YES)
452 if (st)
453 *matched_symbol = st->n.sym;
454 else
455 *matched_symbol = NULL;
457 return m;
460 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
461 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
462 in matchexp.c. */
464 match
465 gfc_match_intrinsic_op (gfc_intrinsic_op * result)
467 gfc_intrinsic_op op;
469 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
471 if (op == INTRINSIC_NONE)
472 return MATCH_NO;
474 *result = op;
475 return MATCH_YES;
479 /* Match a loop control phrase:
481 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
483 If the final integer expression is not present, a constant unity
484 expression is returned. We don't return MATCH_ERROR until after
485 the equals sign is seen. */
487 match
488 gfc_match_iterator (gfc_iterator * iter, int init_flag)
490 char name[GFC_MAX_SYMBOL_LEN + 1];
491 gfc_expr *var, *e1, *e2, *e3;
492 locus start;
493 match m;
495 /* Match the start of an iterator without affecting the symbol
496 table. */
498 start = gfc_current_locus;
499 m = gfc_match (" %n =", name);
500 gfc_current_locus = start;
502 if (m != MATCH_YES)
503 return MATCH_NO;
505 m = gfc_match_variable (&var, 0);
506 if (m != MATCH_YES)
507 return MATCH_NO;
509 gfc_match_char ('=');
511 e1 = e2 = e3 = NULL;
513 if (var->ref != NULL)
515 gfc_error ("Loop variable at %C cannot be a sub-component");
516 goto cleanup;
519 if (var->symtree->n.sym->attr.intent == INTENT_IN)
521 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
522 var->symtree->n.sym->name);
523 goto cleanup;
526 if (var->symtree->n.sym->attr.pointer)
528 gfc_error ("Loop variable at %C cannot have the POINTER attribute");
529 goto cleanup;
532 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
533 if (m == MATCH_NO)
534 goto syntax;
535 if (m == MATCH_ERROR)
536 goto cleanup;
538 if (gfc_match_char (',') != MATCH_YES)
539 goto syntax;
541 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
542 if (m == MATCH_NO)
543 goto syntax;
544 if (m == MATCH_ERROR)
545 goto cleanup;
547 if (gfc_match_char (',') != MATCH_YES)
549 e3 = gfc_int_expr (1);
550 goto done;
553 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
554 if (m == MATCH_ERROR)
555 goto cleanup;
556 if (m == MATCH_NO)
558 gfc_error ("Expected a step value in iterator at %C");
559 goto cleanup;
562 done:
563 iter->var = var;
564 iter->start = e1;
565 iter->end = e2;
566 iter->step = e3;
567 return MATCH_YES;
569 syntax:
570 gfc_error ("Syntax error in iterator at %C");
572 cleanup:
573 gfc_free_expr (e1);
574 gfc_free_expr (e2);
575 gfc_free_expr (e3);
577 return MATCH_ERROR;
581 /* Tries to match the next non-whitespace character on the input.
582 This subroutine does not return MATCH_ERROR. */
584 match
585 gfc_match_char (char c)
587 locus where;
589 where = gfc_current_locus;
590 gfc_gobble_whitespace ();
592 if (gfc_next_char () == c)
593 return MATCH_YES;
595 gfc_current_locus = where;
596 return MATCH_NO;
600 /* General purpose matching subroutine. The target string is a
601 scanf-like format string in which spaces correspond to arbitrary
602 whitespace (including no whitespace), characters correspond to
603 themselves. The %-codes are:
605 %% Literal percent sign
606 %e Expression, pointer to a pointer is set
607 %s Symbol, pointer to the symbol is set
608 %n Name, character buffer is set to name
609 %t Matches end of statement.
610 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
611 %l Matches a statement label
612 %v Matches a variable expression (an lvalue)
613 % Matches a required space (in free form) and optional spaces. */
615 match
616 gfc_match (const char *target, ...)
618 gfc_st_label **label;
619 int matches, *ip;
620 locus old_loc;
621 va_list argp;
622 char c, *np;
623 match m, n;
624 void **vp;
625 const char *p;
627 old_loc = gfc_current_locus;
628 va_start (argp, target);
629 m = MATCH_NO;
630 matches = 0;
631 p = target;
633 loop:
634 c = *p++;
635 switch (c)
637 case ' ':
638 gfc_gobble_whitespace ();
639 goto loop;
640 case '\0':
641 m = MATCH_YES;
642 break;
644 case '%':
645 c = *p++;
646 switch (c)
648 case 'e':
649 vp = va_arg (argp, void **);
650 n = gfc_match_expr ((gfc_expr **) vp);
651 if (n != MATCH_YES)
653 m = n;
654 goto not_yes;
657 matches++;
658 goto loop;
660 case 'v':
661 vp = va_arg (argp, void **);
662 n = gfc_match_variable ((gfc_expr **) vp, 0);
663 if (n != MATCH_YES)
665 m = n;
666 goto not_yes;
669 matches++;
670 goto loop;
672 case 's':
673 vp = va_arg (argp, void **);
674 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
675 if (n != MATCH_YES)
677 m = n;
678 goto not_yes;
681 matches++;
682 goto loop;
684 case 'n':
685 np = va_arg (argp, char *);
686 n = gfc_match_name (np);
687 if (n != MATCH_YES)
689 m = n;
690 goto not_yes;
693 matches++;
694 goto loop;
696 case 'l':
697 label = va_arg (argp, gfc_st_label **);
698 n = gfc_match_st_label (label, 0);
699 if (n != MATCH_YES)
701 m = n;
702 goto not_yes;
705 matches++;
706 goto loop;
708 case 'o':
709 ip = va_arg (argp, int *);
710 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
711 if (n != MATCH_YES)
713 m = n;
714 goto not_yes;
717 matches++;
718 goto loop;
720 case 't':
721 if (gfc_match_eos () != MATCH_YES)
723 m = MATCH_NO;
724 goto not_yes;
726 goto loop;
728 case ' ':
729 if (gfc_match_space () == MATCH_YES)
730 goto loop;
731 m = MATCH_NO;
732 goto not_yes;
734 case '%':
735 break; /* Fall through to character matcher */
737 default:
738 gfc_internal_error ("gfc_match(): Bad match code %c", c);
741 default:
742 if (c == gfc_next_char ())
743 goto loop;
744 break;
747 not_yes:
748 va_end (argp);
750 if (m != MATCH_YES)
752 /* Clean up after a failed match. */
753 gfc_current_locus = old_loc;
754 va_start (argp, target);
756 p = target;
757 for (; matches > 0; matches--)
759 while (*p++ != '%');
761 switch (*p++)
763 case '%':
764 matches++;
765 break; /* Skip */
767 /* Matches that don't have to be undone */
768 case 'o':
769 case 'l':
770 case 'n':
771 case 's':
772 (void)va_arg (argp, void **);
773 break;
775 case 'e':
776 case 'v':
777 vp = va_arg (argp, void **);
778 gfc_free_expr (*vp);
779 *vp = NULL;
780 break;
784 va_end (argp);
787 return m;
791 /*********************** Statement level matching **********************/
793 /* Matches the start of a program unit, which is the program keyword
794 followed by an obligatory symbol. */
796 match
797 gfc_match_program (void)
799 gfc_symbol *sym;
800 match m;
802 m = gfc_match ("% %s%t", &sym);
804 if (m == MATCH_NO)
806 gfc_error ("Invalid form of PROGRAM statement at %C");
807 m = MATCH_ERROR;
810 if (m == MATCH_ERROR)
811 return m;
813 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, NULL) == FAILURE)
814 return MATCH_ERROR;
816 gfc_new_block = sym;
818 return MATCH_YES;
822 /* Match a simple assignment statement. */
824 match
825 gfc_match_assignment (void)
827 gfc_expr *lvalue, *rvalue;
828 locus old_loc;
829 match m;
831 old_loc = gfc_current_locus;
833 lvalue = rvalue = NULL;
834 m = gfc_match (" %v =", &lvalue);
835 if (m != MATCH_YES)
836 goto cleanup;
838 if (lvalue->symtree->n.sym->attr.flavor == FL_PARAMETER)
840 gfc_error ("Cannot assign to a PARAMETER variable at %C");
841 m = MATCH_ERROR;
842 goto cleanup;
845 m = gfc_match (" %e%t", &rvalue);
846 if (m != MATCH_YES)
847 goto cleanup;
849 gfc_set_sym_referenced (lvalue->symtree->n.sym);
851 new_st.op = EXEC_ASSIGN;
852 new_st.expr = lvalue;
853 new_st.expr2 = rvalue;
855 gfc_check_do_variable (lvalue->symtree);
857 return MATCH_YES;
859 cleanup:
860 gfc_current_locus = old_loc;
861 gfc_free_expr (lvalue);
862 gfc_free_expr (rvalue);
863 return m;
867 /* Match a pointer assignment statement. */
869 match
870 gfc_match_pointer_assignment (void)
872 gfc_expr *lvalue, *rvalue;
873 locus old_loc;
874 match m;
876 old_loc = gfc_current_locus;
878 lvalue = rvalue = NULL;
880 m = gfc_match (" %v =>", &lvalue);
881 if (m != MATCH_YES)
883 m = MATCH_NO;
884 goto cleanup;
887 m = gfc_match (" %e%t", &rvalue);
888 if (m != MATCH_YES)
889 goto cleanup;
891 new_st.op = EXEC_POINTER_ASSIGN;
892 new_st.expr = lvalue;
893 new_st.expr2 = rvalue;
895 return MATCH_YES;
897 cleanup:
898 gfc_current_locus = old_loc;
899 gfc_free_expr (lvalue);
900 gfc_free_expr (rvalue);
901 return m;
905 /* The IF statement is a bit of a pain. First of all, there are three
906 forms of it, the simple IF, the IF that starts a block and the
907 arithmetic IF.
909 There is a problem with the simple IF and that is the fact that we
910 only have a single level of undo information on symbols. What this
911 means is for a simple IF, we must re-match the whole IF statement
912 multiple times in order to guarantee that the symbol table ends up
913 in the proper state. */
915 static match match_simple_forall (void);
916 static match match_simple_where (void);
918 match
919 gfc_match_if (gfc_statement * if_type)
921 gfc_expr *expr;
922 gfc_st_label *l1, *l2, *l3;
923 locus old_loc;
924 gfc_code *p;
925 match m, n;
927 n = gfc_match_label ();
928 if (n == MATCH_ERROR)
929 return n;
931 old_loc = gfc_current_locus;
933 m = gfc_match (" if ( %e", &expr);
934 if (m != MATCH_YES)
935 return m;
937 if (gfc_match_char (')') != MATCH_YES)
939 gfc_error ("Syntax error in IF-expression at %C");
940 gfc_free_expr (expr);
941 return MATCH_ERROR;
944 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
946 if (m == MATCH_YES)
948 if (n == MATCH_YES)
950 gfc_error
951 ("Block label not appropriate for arithmetic IF statement "
952 "at %C");
954 gfc_free_expr (expr);
955 return MATCH_ERROR;
958 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
959 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
960 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
963 gfc_free_expr (expr);
964 return MATCH_ERROR;
967 new_st.op = EXEC_ARITHMETIC_IF;
968 new_st.expr = expr;
969 new_st.label = l1;
970 new_st.label2 = l2;
971 new_st.label3 = l3;
973 *if_type = ST_ARITHMETIC_IF;
974 return MATCH_YES;
977 if (gfc_match (" then %t") == MATCH_YES)
979 new_st.op = EXEC_IF;
980 new_st.expr = expr;
982 *if_type = ST_IF_BLOCK;
983 return MATCH_YES;
986 if (n == MATCH_YES)
988 gfc_error ("Block label is not appropriate IF statement at %C");
990 gfc_free_expr (expr);
991 return MATCH_ERROR;
994 /* At this point the only thing left is a simple IF statement. At
995 this point, n has to be MATCH_NO, so we don't have to worry about
996 re-matching a block label. From what we've got so far, try
997 matching an assignment. */
999 *if_type = ST_SIMPLE_IF;
1001 m = gfc_match_assignment ();
1002 if (m == MATCH_YES)
1003 goto got_match;
1005 gfc_free_expr (expr);
1006 gfc_undo_symbols ();
1007 gfc_current_locus = old_loc;
1009 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1011 m = gfc_match_pointer_assignment ();
1012 if (m == MATCH_YES)
1013 goto got_match;
1015 gfc_free_expr (expr);
1016 gfc_undo_symbols ();
1017 gfc_current_locus = old_loc;
1019 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1021 /* Look at the next keyword to see which matcher to call. Matching
1022 the keyword doesn't affect the symbol table, so we don't have to
1023 restore between tries. */
1025 #define match(string, subr, statement) \
1026 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1028 gfc_clear_error ();
1030 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1031 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1032 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1033 match ("call", gfc_match_call, ST_CALL)
1034 match ("close", gfc_match_close, ST_CLOSE)
1035 match ("continue", gfc_match_continue, ST_CONTINUE)
1036 match ("cycle", gfc_match_cycle, ST_CYCLE)
1037 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1038 match ("end file", gfc_match_endfile, ST_END_FILE)
1039 match ("exit", gfc_match_exit, ST_EXIT)
1040 match ("forall", match_simple_forall, ST_FORALL)
1041 match ("go to", gfc_match_goto, ST_GOTO)
1042 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1043 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1044 match ("open", gfc_match_open, ST_OPEN)
1045 match ("pause", gfc_match_pause, ST_NONE)
1046 match ("print", gfc_match_print, ST_WRITE)
1047 match ("read", gfc_match_read, ST_READ)
1048 match ("return", gfc_match_return, ST_RETURN)
1049 match ("rewind", gfc_match_rewind, ST_REWIND)
1050 match ("stop", gfc_match_stop, ST_STOP)
1051 match ("where", match_simple_where, ST_WHERE)
1052 match ("write", gfc_match_write, ST_WRITE)
1054 /* All else has failed, so give up. See if any of the matchers has
1055 stored an error message of some sort. */
1056 if (gfc_error_check () == 0)
1057 gfc_error ("Unclassifiable statement in IF-clause at %C");
1059 gfc_free_expr (expr);
1060 return MATCH_ERROR;
1062 got_match:
1063 if (m == MATCH_NO)
1064 gfc_error ("Syntax error in IF-clause at %C");
1065 if (m != MATCH_YES)
1067 gfc_free_expr (expr);
1068 return MATCH_ERROR;
1071 /* At this point, we've matched the single IF and the action clause
1072 is in new_st. Rearrange things so that the IF statement appears
1073 in new_st. */
1075 p = gfc_get_code ();
1076 p->next = gfc_get_code ();
1077 *p->next = new_st;
1078 p->next->loc = gfc_current_locus;
1080 p->expr = expr;
1081 p->op = EXEC_IF;
1083 gfc_clear_new_st ();
1085 new_st.op = EXEC_IF;
1086 new_st.block = p;
1088 return MATCH_YES;
1091 #undef match
1094 /* Match an ELSE statement. */
1096 match
1097 gfc_match_else (void)
1099 char name[GFC_MAX_SYMBOL_LEN + 1];
1101 if (gfc_match_eos () == MATCH_YES)
1102 return MATCH_YES;
1104 if (gfc_match_name (name) != MATCH_YES
1105 || gfc_current_block () == NULL
1106 || gfc_match_eos () != MATCH_YES)
1108 gfc_error ("Unexpected junk after ELSE statement at %C");
1109 return MATCH_ERROR;
1112 if (strcmp (name, gfc_current_block ()->name) != 0)
1114 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1115 name, gfc_current_block ()->name);
1116 return MATCH_ERROR;
1119 return MATCH_YES;
1123 /* Match an ELSE IF statement. */
1125 match
1126 gfc_match_elseif (void)
1128 char name[GFC_MAX_SYMBOL_LEN + 1];
1129 gfc_expr *expr;
1130 match m;
1132 m = gfc_match (" ( %e ) then", &expr);
1133 if (m != MATCH_YES)
1134 return m;
1136 if (gfc_match_eos () == MATCH_YES)
1137 goto done;
1139 if (gfc_match_name (name) != MATCH_YES
1140 || gfc_current_block () == NULL
1141 || gfc_match_eos () != MATCH_YES)
1143 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1144 goto cleanup;
1147 if (strcmp (name, gfc_current_block ()->name) != 0)
1149 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1150 name, gfc_current_block ()->name);
1151 goto cleanup;
1154 done:
1155 new_st.op = EXEC_IF;
1156 new_st.expr = expr;
1157 return MATCH_YES;
1159 cleanup:
1160 gfc_free_expr (expr);
1161 return MATCH_ERROR;
1165 /* Free a gfc_iterator structure. */
1167 void
1168 gfc_free_iterator (gfc_iterator * iter, int flag)
1171 if (iter == NULL)
1172 return;
1174 gfc_free_expr (iter->var);
1175 gfc_free_expr (iter->start);
1176 gfc_free_expr (iter->end);
1177 gfc_free_expr (iter->step);
1179 if (flag)
1180 gfc_free (iter);
1184 /* Match a DO statement. */
1186 match
1187 gfc_match_do (void)
1189 gfc_iterator iter, *ip;
1190 locus old_loc;
1191 gfc_st_label *label;
1192 match m;
1194 old_loc = gfc_current_locus;
1196 label = NULL;
1197 iter.var = iter.start = iter.end = iter.step = NULL;
1199 m = gfc_match_label ();
1200 if (m == MATCH_ERROR)
1201 return m;
1203 if (gfc_match (" do") != MATCH_YES)
1204 return MATCH_NO;
1206 m = gfc_match_st_label (&label, 0);
1207 if (m == MATCH_ERROR)
1208 goto cleanup;
1210 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1212 if (gfc_match_eos () == MATCH_YES)
1214 iter.end = gfc_logical_expr (1, NULL);
1215 new_st.op = EXEC_DO_WHILE;
1216 goto done;
1219 /* match an optional comma, if no comma is found a space is obligatory. */
1220 if (gfc_match_char(',') != MATCH_YES
1221 && gfc_match ("% ") != MATCH_YES)
1222 return MATCH_NO;
1224 /* See if we have a DO WHILE. */
1225 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1227 new_st.op = EXEC_DO_WHILE;
1228 goto done;
1231 /* The abortive DO WHILE may have done something to the symbol
1232 table, so we start over: */
1233 gfc_undo_symbols ();
1234 gfc_current_locus = old_loc;
1236 gfc_match_label (); /* This won't error */
1237 gfc_match (" do "); /* This will work */
1239 gfc_match_st_label (&label, 0); /* Can't error out */
1240 gfc_match_char (','); /* Optional comma */
1242 m = gfc_match_iterator (&iter, 0);
1243 if (m == MATCH_NO)
1244 return MATCH_NO;
1245 if (m == MATCH_ERROR)
1246 goto cleanup;
1248 gfc_check_do_variable (iter.var->symtree);
1250 if (gfc_match_eos () != MATCH_YES)
1252 gfc_syntax_error (ST_DO);
1253 goto cleanup;
1256 new_st.op = EXEC_DO;
1258 done:
1259 if (label != NULL
1260 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1261 goto cleanup;
1263 new_st.label = label;
1265 if (new_st.op == EXEC_DO_WHILE)
1266 new_st.expr = iter.end;
1267 else
1269 new_st.ext.iterator = ip = gfc_get_iterator ();
1270 *ip = iter;
1273 return MATCH_YES;
1275 cleanup:
1276 gfc_free_iterator (&iter, 0);
1278 return MATCH_ERROR;
1282 /* Match an EXIT or CYCLE statement. */
1284 static match
1285 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1287 gfc_state_data *p;
1288 gfc_symbol *sym;
1289 match m;
1291 if (gfc_match_eos () == MATCH_YES)
1292 sym = NULL;
1293 else
1295 m = gfc_match ("% %s%t", &sym);
1296 if (m == MATCH_ERROR)
1297 return MATCH_ERROR;
1298 if (m == MATCH_NO)
1300 gfc_syntax_error (st);
1301 return MATCH_ERROR;
1304 if (sym->attr.flavor != FL_LABEL)
1306 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1307 sym->name, gfc_ascii_statement (st));
1308 return MATCH_ERROR;
1312 /* Find the loop mentioned specified by the label (or lack of a
1313 label). */
1314 for (p = gfc_state_stack; p; p = p->previous)
1315 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1316 break;
1318 if (p == NULL)
1320 if (sym == NULL)
1321 gfc_error ("%s statement at %C is not within a loop",
1322 gfc_ascii_statement (st));
1323 else
1324 gfc_error ("%s statement at %C is not within loop '%s'",
1325 gfc_ascii_statement (st), sym->name);
1327 return MATCH_ERROR;
1330 /* Save the first statement in the loop - needed by the backend. */
1331 new_st.ext.whichloop = p->head;
1333 new_st.op = op;
1334 /* new_st.sym = sym;*/
1336 return MATCH_YES;
1340 /* Match the EXIT statement. */
1342 match
1343 gfc_match_exit (void)
1346 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1350 /* Match the CYCLE statement. */
1352 match
1353 gfc_match_cycle (void)
1356 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1360 /* Match a number or character constant after a STOP or PAUSE statement. */
1362 static match
1363 gfc_match_stopcode (gfc_statement st)
1365 int stop_code;
1366 gfc_expr *e;
1367 match m;
1369 stop_code = 0;
1370 e = NULL;
1372 if (gfc_match_eos () != MATCH_YES)
1374 m = gfc_match_small_literal_int (&stop_code);
1375 if (m == MATCH_ERROR)
1376 goto cleanup;
1378 if (m == MATCH_YES && stop_code > 99999)
1380 gfc_error ("STOP code out of range at %C");
1381 goto cleanup;
1384 if (m == MATCH_NO)
1386 /* Try a character constant. */
1387 m = gfc_match_expr (&e);
1388 if (m == MATCH_ERROR)
1389 goto cleanup;
1390 if (m == MATCH_NO)
1391 goto syntax;
1392 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1393 goto syntax;
1396 if (gfc_match_eos () != MATCH_YES)
1397 goto syntax;
1400 if (gfc_pure (NULL))
1402 gfc_error ("%s statement not allowed in PURE procedure at %C",
1403 gfc_ascii_statement (st));
1404 goto cleanup;
1407 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1408 new_st.expr = e;
1409 new_st.ext.stop_code = stop_code;
1411 return MATCH_YES;
1413 syntax:
1414 gfc_syntax_error (st);
1416 cleanup:
1418 gfc_free_expr (e);
1419 return MATCH_ERROR;
1422 /* Match the (deprecated) PAUSE statement. */
1424 match
1425 gfc_match_pause (void)
1427 match m;
1429 m = gfc_match_stopcode (ST_PAUSE);
1430 if (m == MATCH_YES)
1432 if (gfc_notify_std (GFC_STD_F95_DEL,
1433 "Obsolete: PAUSE statement at %C")
1434 == FAILURE)
1435 m = MATCH_ERROR;
1437 return m;
1441 /* Match the STOP statement. */
1443 match
1444 gfc_match_stop (void)
1446 return gfc_match_stopcode (ST_STOP);
1450 /* Match a CONTINUE statement. */
1452 match
1453 gfc_match_continue (void)
1456 if (gfc_match_eos () != MATCH_YES)
1458 gfc_syntax_error (ST_CONTINUE);
1459 return MATCH_ERROR;
1462 new_st.op = EXEC_CONTINUE;
1463 return MATCH_YES;
1467 /* Match the (deprecated) ASSIGN statement. */
1469 match
1470 gfc_match_assign (void)
1472 gfc_expr *expr;
1473 gfc_st_label *label;
1475 if (gfc_match (" %l", &label) == MATCH_YES)
1477 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1478 return MATCH_ERROR;
1479 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1481 if (gfc_notify_std (GFC_STD_F95_DEL,
1482 "Obsolete: ASSIGN statement at %C")
1483 == FAILURE)
1484 return MATCH_ERROR;
1486 expr->symtree->n.sym->attr.assign = 1;
1488 new_st.op = EXEC_LABEL_ASSIGN;
1489 new_st.label = label;
1490 new_st.expr = expr;
1491 return MATCH_YES;
1494 return MATCH_NO;
1498 /* Match the GO TO statement. As a computed GOTO statement is
1499 matched, it is transformed into an equivalent SELECT block. No
1500 tree is necessary, and the resulting jumps-to-jumps are
1501 specifically optimized away by the back end. */
1503 match
1504 gfc_match_goto (void)
1506 gfc_code *head, *tail;
1507 gfc_expr *expr;
1508 gfc_case *cp;
1509 gfc_st_label *label;
1510 int i;
1511 match m;
1513 if (gfc_match (" %l%t", &label) == MATCH_YES)
1515 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1516 return MATCH_ERROR;
1518 new_st.op = EXEC_GOTO;
1519 new_st.label = label;
1520 return MATCH_YES;
1523 /* The assigned GO TO statement. */
1525 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1527 if (gfc_notify_std (GFC_STD_F95_DEL,
1528 "Obsolete: Assigned GOTO statement at %C")
1529 == FAILURE)
1530 return MATCH_ERROR;
1532 expr->symtree->n.sym->attr.assign = 1;
1533 new_st.op = EXEC_GOTO;
1534 new_st.expr = expr;
1536 if (gfc_match_eos () == MATCH_YES)
1537 return MATCH_YES;
1539 /* Match label list. */
1540 gfc_match_char (',');
1541 if (gfc_match_char ('(') != MATCH_YES)
1543 gfc_syntax_error (ST_GOTO);
1544 return MATCH_ERROR;
1546 head = tail = NULL;
1550 m = gfc_match_st_label (&label, 0);
1551 if (m != MATCH_YES)
1552 goto syntax;
1554 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1555 goto cleanup;
1557 if (head == NULL)
1558 head = tail = gfc_get_code ();
1559 else
1561 tail->block = gfc_get_code ();
1562 tail = tail->block;
1565 tail->label = label;
1566 tail->op = EXEC_GOTO;
1568 while (gfc_match_char (',') == MATCH_YES);
1570 if (gfc_match (")%t") != MATCH_YES)
1571 goto syntax;
1573 if (head == NULL)
1575 gfc_error (
1576 "Statement label list in GOTO at %C cannot be empty");
1577 goto syntax;
1579 new_st.block = head;
1581 return MATCH_YES;
1584 /* Last chance is a computed GO TO statement. */
1585 if (gfc_match_char ('(') != MATCH_YES)
1587 gfc_syntax_error (ST_GOTO);
1588 return MATCH_ERROR;
1591 head = tail = NULL;
1592 i = 1;
1596 m = gfc_match_st_label (&label, 0);
1597 if (m != MATCH_YES)
1598 goto syntax;
1600 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1601 goto cleanup;
1603 if (head == NULL)
1604 head = tail = gfc_get_code ();
1605 else
1607 tail->block = gfc_get_code ();
1608 tail = tail->block;
1611 cp = gfc_get_case ();
1612 cp->low = cp->high = gfc_int_expr (i++);
1614 tail->op = EXEC_SELECT;
1615 tail->ext.case_list = cp;
1617 tail->next = gfc_get_code ();
1618 tail->next->op = EXEC_GOTO;
1619 tail->next->label = label;
1621 while (gfc_match_char (',') == MATCH_YES);
1623 if (gfc_match_char (')') != MATCH_YES)
1624 goto syntax;
1626 if (head == NULL)
1628 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1629 goto syntax;
1632 /* Get the rest of the statement. */
1633 gfc_match_char (',');
1635 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1636 goto syntax;
1638 /* At this point, a computed GOTO has been fully matched and an
1639 equivalent SELECT statement constructed. */
1641 new_st.op = EXEC_SELECT;
1642 new_st.expr = NULL;
1644 /* Hack: For a "real" SELECT, the expression is in expr. We put
1645 it in expr2 so we can distinguish then and produce the correct
1646 diagnostics. */
1647 new_st.expr2 = expr;
1648 new_st.block = head;
1649 return MATCH_YES;
1651 syntax:
1652 gfc_syntax_error (ST_GOTO);
1653 cleanup:
1654 gfc_free_statements (head);
1655 return MATCH_ERROR;
1659 /* Frees a list of gfc_alloc structures. */
1661 void
1662 gfc_free_alloc_list (gfc_alloc * p)
1664 gfc_alloc *q;
1666 for (; p; p = q)
1668 q = p->next;
1669 gfc_free_expr (p->expr);
1670 gfc_free (p);
1675 /* Match an ALLOCATE statement. */
1677 match
1678 gfc_match_allocate (void)
1680 gfc_alloc *head, *tail;
1681 gfc_expr *stat;
1682 match m;
1684 head = tail = NULL;
1685 stat = NULL;
1687 if (gfc_match_char ('(') != MATCH_YES)
1688 goto syntax;
1690 for (;;)
1692 if (head == NULL)
1693 head = tail = gfc_get_alloc ();
1694 else
1696 tail->next = gfc_get_alloc ();
1697 tail = tail->next;
1700 m = gfc_match_variable (&tail->expr, 0);
1701 if (m == MATCH_NO)
1702 goto syntax;
1703 if (m == MATCH_ERROR)
1704 goto cleanup;
1706 if (gfc_check_do_variable (tail->expr->symtree))
1707 goto cleanup;
1709 if (gfc_pure (NULL)
1710 && gfc_impure_variable (tail->expr->symtree->n.sym))
1712 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1713 "PURE procedure");
1714 goto cleanup;
1717 if (gfc_match_char (',') != MATCH_YES)
1718 break;
1720 m = gfc_match (" stat = %v", &stat);
1721 if (m == MATCH_ERROR)
1722 goto cleanup;
1723 if (m == MATCH_YES)
1724 break;
1727 if (stat != NULL)
1729 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1731 gfc_error
1732 ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1733 "INTENT(IN)", stat->symtree->n.sym->name);
1734 goto cleanup;
1737 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1739 gfc_error
1740 ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1741 "procedure");
1742 goto cleanup;
1745 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1747 gfc_error("STAT expression at %C must be a variable");
1748 goto cleanup;
1751 gfc_check_do_variable(stat->symtree);
1754 if (gfc_match (" )%t") != MATCH_YES)
1755 goto syntax;
1757 new_st.op = EXEC_ALLOCATE;
1758 new_st.expr = stat;
1759 new_st.ext.alloc_list = head;
1761 return MATCH_YES;
1763 syntax:
1764 gfc_syntax_error (ST_ALLOCATE);
1766 cleanup:
1767 gfc_free_expr (stat);
1768 gfc_free_alloc_list (head);
1769 return MATCH_ERROR;
1773 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1774 a set of pointer assignments to intrinsic NULL(). */
1776 match
1777 gfc_match_nullify (void)
1779 gfc_code *tail;
1780 gfc_expr *e, *p;
1781 match m;
1783 tail = NULL;
1785 if (gfc_match_char ('(') != MATCH_YES)
1786 goto syntax;
1788 for (;;)
1790 m = gfc_match_variable (&p, 0);
1791 if (m == MATCH_ERROR)
1792 goto cleanup;
1793 if (m == MATCH_NO)
1794 goto syntax;
1796 if (gfc_check_do_variable(p->symtree))
1797 goto cleanup;
1799 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1801 gfc_error
1802 ("Illegal variable in NULLIFY at %C for a PURE procedure");
1803 goto cleanup;
1806 /* build ' => NULL() ' */
1807 e = gfc_get_expr ();
1808 e->where = gfc_current_locus;
1809 e->expr_type = EXPR_NULL;
1810 e->ts.type = BT_UNKNOWN;
1812 /* Chain to list */
1813 if (tail == NULL)
1814 tail = &new_st;
1815 else
1817 tail->next = gfc_get_code ();
1818 tail = tail->next;
1821 tail->op = EXEC_POINTER_ASSIGN;
1822 tail->expr = p;
1823 tail->expr2 = e;
1825 if (gfc_match_char (')') == MATCH_YES)
1826 break;
1827 if (gfc_match_char (',') != MATCH_YES)
1828 goto syntax;
1831 return MATCH_YES;
1833 syntax:
1834 gfc_syntax_error (ST_NULLIFY);
1836 cleanup:
1837 gfc_free_statements (tail);
1838 return MATCH_ERROR;
1842 /* Match a DEALLOCATE statement. */
1844 match
1845 gfc_match_deallocate (void)
1847 gfc_alloc *head, *tail;
1848 gfc_expr *stat;
1849 match m;
1851 head = tail = NULL;
1852 stat = NULL;
1854 if (gfc_match_char ('(') != MATCH_YES)
1855 goto syntax;
1857 for (;;)
1859 if (head == NULL)
1860 head = tail = gfc_get_alloc ();
1861 else
1863 tail->next = gfc_get_alloc ();
1864 tail = tail->next;
1867 m = gfc_match_variable (&tail->expr, 0);
1868 if (m == MATCH_ERROR)
1869 goto cleanup;
1870 if (m == MATCH_NO)
1871 goto syntax;
1873 if (gfc_check_do_variable (tail->expr->symtree))
1874 goto cleanup;
1876 if (gfc_pure (NULL)
1877 && gfc_impure_variable (tail->expr->symtree->n.sym))
1879 gfc_error
1880 ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1881 "procedure");
1882 goto cleanup;
1885 if (gfc_match_char (',') != MATCH_YES)
1886 break;
1888 m = gfc_match (" stat = %v", &stat);
1889 if (m == MATCH_ERROR)
1890 goto cleanup;
1891 if (m == MATCH_YES)
1892 break;
1895 if (stat != NULL)
1897 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1899 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1900 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
1901 goto cleanup;
1904 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
1906 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
1907 "for a PURE procedure");
1908 goto cleanup;
1911 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1913 gfc_error("STAT expression at %C must be a variable");
1914 goto cleanup;
1917 gfc_check_do_variable(stat->symtree);
1920 if (gfc_match (" )%t") != MATCH_YES)
1921 goto syntax;
1923 new_st.op = EXEC_DEALLOCATE;
1924 new_st.expr = stat;
1925 new_st.ext.alloc_list = head;
1927 return MATCH_YES;
1929 syntax:
1930 gfc_syntax_error (ST_DEALLOCATE);
1932 cleanup:
1933 gfc_free_expr (stat);
1934 gfc_free_alloc_list (head);
1935 return MATCH_ERROR;
1939 /* Match a RETURN statement. */
1941 match
1942 gfc_match_return (void)
1944 gfc_expr *e;
1945 match m;
1946 gfc_compile_state s;
1948 gfc_enclosing_unit (&s);
1949 if (s == COMP_PROGRAM
1950 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
1951 "main program at %C") == FAILURE)
1952 return MATCH_ERROR;
1954 e = NULL;
1955 if (gfc_match_eos () == MATCH_YES)
1956 goto done;
1958 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
1960 gfc_error ("Alternate RETURN statement at %C is only allowed within "
1961 "a SUBROUTINE");
1962 goto cleanup;
1965 m = gfc_match ("% %e%t", &e);
1966 if (m == MATCH_YES)
1967 goto done;
1968 if (m == MATCH_ERROR)
1969 goto cleanup;
1971 gfc_syntax_error (ST_RETURN);
1973 cleanup:
1974 gfc_free_expr (e);
1975 return MATCH_ERROR;
1977 done:
1978 new_st.op = EXEC_RETURN;
1979 new_st.expr = e;
1981 return MATCH_YES;
1985 /* Match a CALL statement. The tricky part here are possible
1986 alternate return specifiers. We handle these by having all
1987 "subroutines" actually return an integer via a register that gives
1988 the return number. If the call specifies alternate returns, we
1989 generate code for a SELECT statement whose case clauses contain
1990 GOTOs to the various labels. */
1992 match
1993 gfc_match_call (void)
1995 char name[GFC_MAX_SYMBOL_LEN + 1];
1996 gfc_actual_arglist *a, *arglist;
1997 gfc_case *new_case;
1998 gfc_symbol *sym;
1999 gfc_symtree *st;
2000 gfc_code *c;
2001 match m;
2002 int i;
2004 arglist = NULL;
2006 m = gfc_match ("% %n", name);
2007 if (m == MATCH_NO)
2008 goto syntax;
2009 if (m != MATCH_YES)
2010 return m;
2012 if (gfc_get_ha_sym_tree (name, &st))
2013 return MATCH_ERROR;
2015 sym = st->n.sym;
2016 gfc_set_sym_referenced (sym);
2018 if (!sym->attr.generic
2019 && !sym->attr.subroutine
2020 && gfc_add_subroutine (&sym->attr, NULL) == FAILURE)
2021 return MATCH_ERROR;
2023 if (gfc_match_eos () != MATCH_YES)
2025 m = gfc_match_actual_arglist (1, &arglist);
2026 if (m == MATCH_NO)
2027 goto syntax;
2028 if (m == MATCH_ERROR)
2029 goto cleanup;
2031 if (gfc_match_eos () != MATCH_YES)
2032 goto syntax;
2035 /* If any alternate return labels were found, construct a SELECT
2036 statement that will jump to the right place. */
2038 i = 0;
2039 for (a = arglist; a; a = a->next)
2040 if (a->expr == NULL)
2041 i = 1;
2043 if (i)
2045 gfc_symtree *select_st;
2046 gfc_symbol *select_sym;
2047 char name[GFC_MAX_SYMBOL_LEN + 1];
2049 new_st.next = c = gfc_get_code ();
2050 c->op = EXEC_SELECT;
2051 sprintf (name, "_result_%s",sym->name);
2052 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail */
2054 select_sym = select_st->n.sym;
2055 select_sym->ts.type = BT_INTEGER;
2056 select_sym->ts.kind = gfc_default_integer_kind;
2057 gfc_set_sym_referenced (select_sym);
2058 c->expr = gfc_get_expr ();
2059 c->expr->expr_type = EXPR_VARIABLE;
2060 c->expr->symtree = select_st;
2061 c->expr->ts = select_sym->ts;
2062 c->expr->where = gfc_current_locus;
2064 i = 0;
2065 for (a = arglist; a; a = a->next)
2067 if (a->expr != NULL)
2068 continue;
2070 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2071 continue;
2073 i++;
2075 c->block = gfc_get_code ();
2076 c = c->block;
2077 c->op = EXEC_SELECT;
2079 new_case = gfc_get_case ();
2080 new_case->high = new_case->low = gfc_int_expr (i);
2081 c->ext.case_list = new_case;
2083 c->next = gfc_get_code ();
2084 c->next->op = EXEC_GOTO;
2085 c->next->label = a->label;
2089 new_st.op = EXEC_CALL;
2090 new_st.symtree = st;
2091 new_st.ext.actual = arglist;
2093 return MATCH_YES;
2095 syntax:
2096 gfc_syntax_error (ST_CALL);
2098 cleanup:
2099 gfc_free_actual_arglist (arglist);
2100 return MATCH_ERROR;
2104 /* Given a name, return a pointer to the common head structure,
2105 creating it if it does not exist. If FROM_MODULE is nonzero, we
2106 mangle the name so that it doesn't interfere with commons defined
2107 in the using namespace.
2108 TODO: Add to global symbol tree. */
2110 gfc_common_head *
2111 gfc_get_common (const char *name, int from_module)
2113 gfc_symtree *st;
2114 static int serial = 0;
2115 char mangled_name[GFC_MAX_SYMBOL_LEN+1];
2117 if (from_module)
2119 /* A use associated common block is only needed to correctly layout
2120 the variables it contains. */
2121 snprintf(mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2122 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2124 else
2126 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2128 if (st == NULL)
2129 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2132 if (st->n.common == NULL)
2134 st->n.common = gfc_get_common_head ();
2135 st->n.common->where = gfc_current_locus;
2136 strcpy (st->n.common->name, name);
2139 return st->n.common;
2143 /* Match a common block name. */
2145 static match
2146 match_common_name (char *name)
2148 match m;
2150 if (gfc_match_char ('/') == MATCH_NO)
2152 name[0] = '\0';
2153 return MATCH_YES;
2156 if (gfc_match_char ('/') == MATCH_YES)
2158 name[0] = '\0';
2159 return MATCH_YES;
2162 m = gfc_match_name (name);
2164 if (m == MATCH_ERROR)
2165 return MATCH_ERROR;
2166 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2167 return MATCH_YES;
2169 gfc_error ("Syntax error in common block name at %C");
2170 return MATCH_ERROR;
2174 /* Match a COMMON statement. */
2176 match
2177 gfc_match_common (void)
2179 gfc_symbol *sym, **head, *tail, *old_blank_common;
2180 char name[GFC_MAX_SYMBOL_LEN+1];
2181 gfc_common_head *t;
2182 gfc_array_spec *as;
2183 match m;
2185 old_blank_common = gfc_current_ns->blank_common.head;
2186 if (old_blank_common)
2188 while (old_blank_common->common_next)
2189 old_blank_common = old_blank_common->common_next;
2192 as = NULL;
2194 if (gfc_match_eos () == MATCH_YES)
2195 goto syntax;
2197 for (;;)
2199 m = match_common_name (name);
2200 if (m == MATCH_ERROR)
2201 goto cleanup;
2203 if (name[0] == '\0')
2205 t = &gfc_current_ns->blank_common;
2206 if (t->head == NULL)
2207 t->where = gfc_current_locus;
2208 head = &t->head;
2210 else
2212 t = gfc_get_common (name, 0);
2213 head = &t->head;
2216 if (*head == NULL)
2217 tail = NULL;
2218 else
2220 tail = *head;
2221 while (tail->common_next)
2222 tail = tail->common_next;
2225 /* Grab the list of symbols. */
2226 if (gfc_match_eos () == MATCH_YES)
2227 goto done;
2229 for (;;)
2231 m = gfc_match_symbol (&sym, 0);
2232 if (m == MATCH_ERROR)
2233 goto cleanup;
2234 if (m == MATCH_NO)
2235 goto syntax;
2237 if (sym->attr.in_common)
2239 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2240 sym->name);
2241 goto cleanup;
2244 if (gfc_add_in_common (&sym->attr, NULL) == FAILURE)
2245 goto cleanup;
2247 if (sym->value != NULL
2248 && (name[0] == '\0' || !sym->attr.data))
2250 if (name[0] == '\0')
2251 gfc_error ("Previously initialized symbol '%s' in "
2252 "blank COMMON block at %C", sym->name);
2253 else
2254 gfc_error ("Previously initialized symbol '%s' in "
2255 "COMMON block '%s' at %C", sym->name, name);
2256 goto cleanup;
2259 if (gfc_add_in_common (&sym->attr, NULL) == FAILURE)
2260 goto cleanup;
2262 /* Derived type names must have the SEQUENCE attribute. */
2263 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2265 gfc_error
2266 ("Derived type variable in COMMON at %C does not have the "
2267 "SEQUENCE attribute");
2268 goto cleanup;
2271 if (tail != NULL)
2272 tail->common_next = sym;
2273 else
2274 *head = sym;
2276 tail = sym;
2278 /* Deal with an optional array specification after the
2279 symbol name. */
2280 m = gfc_match_array_spec (&as);
2281 if (m == MATCH_ERROR)
2282 goto cleanup;
2284 if (m == MATCH_YES)
2286 if (as->type != AS_EXPLICIT)
2288 gfc_error
2289 ("Array specification for symbol '%s' in COMMON at %C "
2290 "must be explicit", sym->name);
2291 goto cleanup;
2294 if (gfc_add_dimension (&sym->attr, NULL) == FAILURE)
2295 goto cleanup;
2297 if (sym->attr.pointer)
2299 gfc_error
2300 ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2301 sym->name);
2302 goto cleanup;
2305 sym->as = as;
2306 as = NULL;
2309 if (gfc_match_eos () == MATCH_YES)
2310 goto done;
2311 if (gfc_peek_char () == '/')
2312 break;
2313 if (gfc_match_char (',') != MATCH_YES)
2314 goto syntax;
2315 if (gfc_peek_char () == '/')
2316 break;
2320 done:
2321 return MATCH_YES;
2323 syntax:
2324 gfc_syntax_error (ST_COMMON);
2326 cleanup:
2327 if (old_blank_common)
2328 old_blank_common->common_next = NULL;
2329 else
2330 gfc_current_ns->blank_common.head = NULL;
2331 gfc_free_array_spec (as);
2332 return MATCH_ERROR;
2336 /* Match a BLOCK DATA program unit. */
2338 match
2339 gfc_match_block_data (void)
2341 char name[GFC_MAX_SYMBOL_LEN + 1];
2342 gfc_symbol *sym;
2343 match m;
2345 if (gfc_match_eos () == MATCH_YES)
2347 gfc_new_block = NULL;
2348 return MATCH_YES;
2351 m = gfc_match ("% %n%t", name);
2352 if (m != MATCH_YES)
2353 return MATCH_ERROR;
2355 if (gfc_get_symbol (name, NULL, &sym))
2356 return MATCH_ERROR;
2358 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, NULL) == FAILURE)
2359 return MATCH_ERROR;
2361 gfc_new_block = sym;
2363 return MATCH_YES;
2367 /* Free a namelist structure. */
2369 void
2370 gfc_free_namelist (gfc_namelist * name)
2372 gfc_namelist *n;
2374 for (; name; name = n)
2376 n = name->next;
2377 gfc_free (name);
2382 /* Match a NAMELIST statement. */
2384 match
2385 gfc_match_namelist (void)
2387 gfc_symbol *group_name, *sym;
2388 gfc_namelist *nl;
2389 match m, m2;
2391 m = gfc_match (" / %s /", &group_name);
2392 if (m == MATCH_NO)
2393 goto syntax;
2394 if (m == MATCH_ERROR)
2395 goto error;
2397 for (;;)
2399 if (group_name->ts.type != BT_UNKNOWN)
2401 gfc_error
2402 ("Namelist group name '%s' at %C already has a basic type "
2403 "of %s", group_name->name, gfc_typename (&group_name->ts));
2404 return MATCH_ERROR;
2407 if (group_name->attr.flavor != FL_NAMELIST
2408 && gfc_add_flavor (&group_name->attr, FL_NAMELIST, NULL) == FAILURE)
2409 return MATCH_ERROR;
2411 for (;;)
2413 m = gfc_match_symbol (&sym, 1);
2414 if (m == MATCH_NO)
2415 goto syntax;
2416 if (m == MATCH_ERROR)
2417 goto error;
2419 if (sym->attr.in_namelist == 0
2420 && gfc_add_in_namelist (&sym->attr, NULL) == FAILURE)
2421 goto error;
2423 /* TODO: worry about PRIVATE members of a PUBLIC namelist
2424 group. */
2426 nl = gfc_get_namelist ();
2427 nl->sym = sym;
2429 if (group_name->namelist == NULL)
2430 group_name->namelist = group_name->namelist_tail = nl;
2431 else
2433 group_name->namelist_tail->next = nl;
2434 group_name->namelist_tail = nl;
2437 if (gfc_match_eos () == MATCH_YES)
2438 goto done;
2440 m = gfc_match_char (',');
2442 if (gfc_match_char ('/') == MATCH_YES)
2444 m2 = gfc_match (" %s /", &group_name);
2445 if (m2 == MATCH_YES)
2446 break;
2447 if (m2 == MATCH_ERROR)
2448 goto error;
2449 goto syntax;
2452 if (m != MATCH_YES)
2453 goto syntax;
2457 done:
2458 return MATCH_YES;
2460 syntax:
2461 gfc_syntax_error (ST_NAMELIST);
2463 error:
2464 return MATCH_ERROR;
2468 /* Match a MODULE statement. */
2470 match
2471 gfc_match_module (void)
2473 match m;
2475 m = gfc_match (" %s%t", &gfc_new_block);
2476 if (m != MATCH_YES)
2477 return m;
2479 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE, NULL) == FAILURE)
2480 return MATCH_ERROR;
2482 return MATCH_YES;
2486 /* Free equivalence sets and lists. Recursively is the easiest way to
2487 do this. */
2489 void
2490 gfc_free_equiv (gfc_equiv * eq)
2493 if (eq == NULL)
2494 return;
2496 gfc_free_equiv (eq->eq);
2497 gfc_free_equiv (eq->next);
2499 gfc_free_expr (eq->expr);
2500 gfc_free (eq);
2504 /* Match an EQUIVALENCE statement. */
2506 match
2507 gfc_match_equivalence (void)
2509 gfc_equiv *eq, *set, *tail;
2510 gfc_ref *ref;
2511 match m;
2513 tail = NULL;
2515 for (;;)
2517 eq = gfc_get_equiv ();
2518 if (tail == NULL)
2519 tail = eq;
2521 eq->next = gfc_current_ns->equiv;
2522 gfc_current_ns->equiv = eq;
2524 if (gfc_match_char ('(') != MATCH_YES)
2525 goto syntax;
2527 set = eq;
2529 for (;;)
2531 m = gfc_match_variable (&set->expr, 1);
2532 if (m == MATCH_ERROR)
2533 goto cleanup;
2534 if (m == MATCH_NO)
2535 goto syntax;
2537 for (ref = set->expr->ref; ref; ref = ref->next)
2538 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2540 gfc_error
2541 ("Array reference in EQUIVALENCE at %C cannot be an "
2542 "array section");
2543 goto cleanup;
2546 if (gfc_match_char (')') == MATCH_YES)
2547 break;
2548 if (gfc_match_char (',') != MATCH_YES)
2549 goto syntax;
2551 set->eq = gfc_get_equiv ();
2552 set = set->eq;
2555 if (gfc_match_eos () == MATCH_YES)
2556 break;
2557 if (gfc_match_char (',') != MATCH_YES)
2558 goto syntax;
2561 return MATCH_YES;
2563 syntax:
2564 gfc_syntax_error (ST_EQUIVALENCE);
2566 cleanup:
2567 eq = tail->next;
2568 tail->next = NULL;
2570 gfc_free_equiv (gfc_current_ns->equiv);
2571 gfc_current_ns->equiv = eq;
2573 return MATCH_ERROR;
2577 /* Match a statement function declaration. It is so easy to match
2578 non-statement function statements with a MATCH_ERROR as opposed to
2579 MATCH_NO that we suppress error message in most cases. */
2581 match
2582 gfc_match_st_function (void)
2584 gfc_error_buf old_error;
2585 gfc_symbol *sym;
2586 gfc_expr *expr;
2587 match m;
2589 m = gfc_match_symbol (&sym, 0);
2590 if (m != MATCH_YES)
2591 return m;
2593 gfc_push_error (&old_error);
2595 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, NULL) == FAILURE)
2596 goto undo_error;
2598 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2599 goto undo_error;
2601 m = gfc_match (" = %e%t", &expr);
2602 if (m == MATCH_NO)
2603 goto undo_error;
2604 if (m == MATCH_ERROR)
2605 return m;
2607 sym->value = expr;
2609 return MATCH_YES;
2611 undo_error:
2612 gfc_pop_error (&old_error);
2613 return MATCH_NO;
2617 /***************** SELECT CASE subroutines ******************/
2619 /* Free a single case structure. */
2621 static void
2622 free_case (gfc_case * p)
2624 if (p->low == p->high)
2625 p->high = NULL;
2626 gfc_free_expr (p->low);
2627 gfc_free_expr (p->high);
2628 gfc_free (p);
2632 /* Free a list of case structures. */
2634 void
2635 gfc_free_case_list (gfc_case * p)
2637 gfc_case *q;
2639 for (; p; p = q)
2641 q = p->next;
2642 free_case (p);
2647 /* Match a single case selector. */
2649 static match
2650 match_case_selector (gfc_case ** cp)
2652 gfc_case *c;
2653 match m;
2655 c = gfc_get_case ();
2656 c->where = gfc_current_locus;
2658 if (gfc_match_char (':') == MATCH_YES)
2660 m = gfc_match_init_expr (&c->high);
2661 if (m == MATCH_NO)
2662 goto need_expr;
2663 if (m == MATCH_ERROR)
2664 goto cleanup;
2667 else
2669 m = gfc_match_init_expr (&c->low);
2670 if (m == MATCH_ERROR)
2671 goto cleanup;
2672 if (m == MATCH_NO)
2673 goto need_expr;
2675 /* If we're not looking at a ':' now, make a range out of a single
2676 target. Else get the upper bound for the case range. */
2677 if (gfc_match_char (':') != MATCH_YES)
2678 c->high = c->low;
2679 else
2681 m = gfc_match_init_expr (&c->high);
2682 if (m == MATCH_ERROR)
2683 goto cleanup;
2684 /* MATCH_NO is fine. It's OK if nothing is there! */
2688 *cp = c;
2689 return MATCH_YES;
2691 need_expr:
2692 gfc_error ("Expected initialization expression in CASE at %C");
2694 cleanup:
2695 free_case (c);
2696 return MATCH_ERROR;
2700 /* Match the end of a case statement. */
2702 static match
2703 match_case_eos (void)
2705 char name[GFC_MAX_SYMBOL_LEN + 1];
2706 match m;
2708 if (gfc_match_eos () == MATCH_YES)
2709 return MATCH_YES;
2711 gfc_gobble_whitespace ();
2713 m = gfc_match_name (name);
2714 if (m != MATCH_YES)
2715 return m;
2717 if (strcmp (name, gfc_current_block ()->name) != 0)
2719 gfc_error ("Expected case name of '%s' at %C",
2720 gfc_current_block ()->name);
2721 return MATCH_ERROR;
2724 return gfc_match_eos ();
2728 /* Match a SELECT statement. */
2730 match
2731 gfc_match_select (void)
2733 gfc_expr *expr;
2734 match m;
2736 m = gfc_match_label ();
2737 if (m == MATCH_ERROR)
2738 return m;
2740 m = gfc_match (" select case ( %e )%t", &expr);
2741 if (m != MATCH_YES)
2742 return m;
2744 new_st.op = EXEC_SELECT;
2745 new_st.expr = expr;
2747 return MATCH_YES;
2751 /* Match a CASE statement. */
2753 match
2754 gfc_match_case (void)
2756 gfc_case *c, *head, *tail;
2757 match m;
2759 head = tail = NULL;
2761 if (gfc_current_state () != COMP_SELECT)
2763 gfc_error ("Unexpected CASE statement at %C");
2764 return MATCH_ERROR;
2767 if (gfc_match ("% default") == MATCH_YES)
2769 m = match_case_eos ();
2770 if (m == MATCH_NO)
2771 goto syntax;
2772 if (m == MATCH_ERROR)
2773 goto cleanup;
2775 new_st.op = EXEC_SELECT;
2776 c = gfc_get_case ();
2777 c->where = gfc_current_locus;
2778 new_st.ext.case_list = c;
2779 return MATCH_YES;
2782 if (gfc_match_char ('(') != MATCH_YES)
2783 goto syntax;
2785 for (;;)
2787 if (match_case_selector (&c) == MATCH_ERROR)
2788 goto cleanup;
2790 if (head == NULL)
2791 head = c;
2792 else
2793 tail->next = c;
2795 tail = c;
2797 if (gfc_match_char (')') == MATCH_YES)
2798 break;
2799 if (gfc_match_char (',') != MATCH_YES)
2800 goto syntax;
2803 m = match_case_eos ();
2804 if (m == MATCH_NO)
2805 goto syntax;
2806 if (m == MATCH_ERROR)
2807 goto cleanup;
2809 new_st.op = EXEC_SELECT;
2810 new_st.ext.case_list = head;
2812 return MATCH_YES;
2814 syntax:
2815 gfc_error ("Syntax error in CASE-specification at %C");
2817 cleanup:
2818 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
2819 return MATCH_ERROR;
2822 /********************* WHERE subroutines ********************/
2824 /* Match the rest of a simple WHERE statement that follows an IF statement.
2827 static match
2828 match_simple_where (void)
2830 gfc_expr *expr;
2831 gfc_code *c;
2832 match m;
2834 m = gfc_match (" ( %e )", &expr);
2835 if (m != MATCH_YES)
2836 return m;
2838 m = gfc_match_assignment ();
2839 if (m == MATCH_NO)
2840 goto syntax;
2841 if (m == MATCH_ERROR)
2842 goto cleanup;
2844 if (gfc_match_eos () != MATCH_YES)
2845 goto syntax;
2847 c = gfc_get_code ();
2849 c->op = EXEC_WHERE;
2850 c->expr = expr;
2851 c->next = gfc_get_code ();
2853 *c->next = new_st;
2854 gfc_clear_new_st ();
2856 new_st.op = EXEC_WHERE;
2857 new_st.block = c;
2859 return MATCH_YES;
2861 syntax:
2862 gfc_syntax_error (ST_WHERE);
2864 cleanup:
2865 gfc_free_expr (expr);
2866 return MATCH_ERROR;
2869 /* Match a WHERE statement. */
2871 match
2872 gfc_match_where (gfc_statement * st)
2874 gfc_expr *expr;
2875 match m0, m;
2876 gfc_code *c;
2878 m0 = gfc_match_label ();
2879 if (m0 == MATCH_ERROR)
2880 return m0;
2882 m = gfc_match (" where ( %e )", &expr);
2883 if (m != MATCH_YES)
2884 return m;
2886 if (gfc_match_eos () == MATCH_YES)
2888 *st = ST_WHERE_BLOCK;
2890 new_st.op = EXEC_WHERE;
2891 new_st.expr = expr;
2892 return MATCH_YES;
2895 m = gfc_match_assignment ();
2896 if (m == MATCH_NO)
2897 gfc_syntax_error (ST_WHERE);
2899 if (m != MATCH_YES)
2901 gfc_free_expr (expr);
2902 return MATCH_ERROR;
2905 /* We've got a simple WHERE statement. */
2906 *st = ST_WHERE;
2907 c = gfc_get_code ();
2909 c->op = EXEC_WHERE;
2910 c->expr = expr;
2911 c->next = gfc_get_code ();
2913 *c->next = new_st;
2914 gfc_clear_new_st ();
2916 new_st.op = EXEC_WHERE;
2917 new_st.block = c;
2919 return MATCH_YES;
2923 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
2924 new_st if successful. */
2926 match
2927 gfc_match_elsewhere (void)
2929 char name[GFC_MAX_SYMBOL_LEN + 1];
2930 gfc_expr *expr;
2931 match m;
2933 if (gfc_current_state () != COMP_WHERE)
2935 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
2936 return MATCH_ERROR;
2939 expr = NULL;
2941 if (gfc_match_char ('(') == MATCH_YES)
2943 m = gfc_match_expr (&expr);
2944 if (m == MATCH_NO)
2945 goto syntax;
2946 if (m == MATCH_ERROR)
2947 return MATCH_ERROR;
2949 if (gfc_match_char (')') != MATCH_YES)
2950 goto syntax;
2953 if (gfc_match_eos () != MATCH_YES)
2954 { /* Better be a name at this point */
2955 m = gfc_match_name (name);
2956 if (m == MATCH_NO)
2957 goto syntax;
2958 if (m == MATCH_ERROR)
2959 goto cleanup;
2961 if (gfc_match_eos () != MATCH_YES)
2962 goto syntax;
2964 if (strcmp (name, gfc_current_block ()->name) != 0)
2966 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
2967 name, gfc_current_block ()->name);
2968 goto cleanup;
2972 new_st.op = EXEC_WHERE;
2973 new_st.expr = expr;
2974 return MATCH_YES;
2976 syntax:
2977 gfc_syntax_error (ST_ELSEWHERE);
2979 cleanup:
2980 gfc_free_expr (expr);
2981 return MATCH_ERROR;
2985 /******************** FORALL subroutines ********************/
2987 /* Free a list of FORALL iterators. */
2989 void
2990 gfc_free_forall_iterator (gfc_forall_iterator * iter)
2992 gfc_forall_iterator *next;
2994 while (iter)
2996 next = iter->next;
2998 gfc_free_expr (iter->var);
2999 gfc_free_expr (iter->start);
3000 gfc_free_expr (iter->end);
3001 gfc_free_expr (iter->stride);
3003 gfc_free (iter);
3004 iter = next;
3009 /* Match an iterator as part of a FORALL statement. The format is:
3011 <var> = <start>:<end>[:<stride>][, <scalar mask>] */
3013 static match
3014 match_forall_iterator (gfc_forall_iterator ** result)
3016 gfc_forall_iterator *iter;
3017 locus where;
3018 match m;
3020 where = gfc_current_locus;
3021 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3023 m = gfc_match_variable (&iter->var, 0);
3024 if (m != MATCH_YES)
3025 goto cleanup;
3027 if (gfc_match_char ('=') != MATCH_YES)
3029 m = MATCH_NO;
3030 goto cleanup;
3033 m = gfc_match_expr (&iter->start);
3034 if (m == MATCH_NO)
3035 goto syntax;
3036 if (m == MATCH_ERROR)
3037 goto cleanup;
3039 if (gfc_match_char (':') != MATCH_YES)
3040 goto syntax;
3042 m = gfc_match_expr (&iter->end);
3043 if (m == MATCH_NO)
3044 goto syntax;
3045 if (m == MATCH_ERROR)
3046 goto cleanup;
3048 if (gfc_match_char (':') == MATCH_NO)
3049 iter->stride = gfc_int_expr (1);
3050 else
3052 m = gfc_match_expr (&iter->stride);
3053 if (m == MATCH_NO)
3054 goto syntax;
3055 if (m == MATCH_ERROR)
3056 goto cleanup;
3059 *result = iter;
3060 return MATCH_YES;
3062 syntax:
3063 gfc_error ("Syntax error in FORALL iterator at %C");
3064 m = MATCH_ERROR;
3066 cleanup:
3067 gfc_current_locus = where;
3068 gfc_free_forall_iterator (iter);
3069 return m;
3073 /* Match the header of a FORALL statement. */
3075 static match
3076 match_forall_header (gfc_forall_iterator ** phead, gfc_expr ** mask)
3078 gfc_forall_iterator *head, *tail, *new;
3079 match m;
3081 gfc_gobble_whitespace ();
3083 head = tail = NULL;
3084 *mask = NULL;
3086 if (gfc_match_char ('(') != MATCH_YES)
3087 return MATCH_NO;
3089 m = match_forall_iterator (&new);
3090 if (m == MATCH_ERROR)
3091 goto cleanup;
3092 if (m == MATCH_NO)
3093 goto syntax;
3095 head = tail = new;
3097 for (;;)
3099 if (gfc_match_char (',') != MATCH_YES)
3100 break;
3102 m = match_forall_iterator (&new);
3103 if (m == MATCH_ERROR)
3104 goto cleanup;
3105 if (m == MATCH_YES)
3107 tail->next = new;
3108 tail = new;
3109 continue;
3112 /* Have to have a mask expression */
3114 m = gfc_match_expr (mask);
3115 if (m == MATCH_NO)
3116 goto syntax;
3117 if (m == MATCH_ERROR)
3118 goto cleanup;
3120 break;
3123 if (gfc_match_char (')') == MATCH_NO)
3124 goto syntax;
3126 *phead = head;
3127 return MATCH_YES;
3129 syntax:
3130 gfc_syntax_error (ST_FORALL);
3132 cleanup:
3133 gfc_free_expr (*mask);
3134 gfc_free_forall_iterator (head);
3136 return MATCH_ERROR;
3139 /* Match the rest of a simple FORALL statement that follows an IF statement.
3142 static match
3143 match_simple_forall (void)
3145 gfc_forall_iterator *head;
3146 gfc_expr *mask;
3147 gfc_code *c;
3148 match m;
3150 mask = NULL;
3151 head = NULL;
3152 c = NULL;
3154 m = match_forall_header (&head, &mask);
3156 if (m == MATCH_NO)
3157 goto syntax;
3158 if (m != MATCH_YES)
3159 goto cleanup;
3161 m = gfc_match_assignment ();
3163 if (m == MATCH_ERROR)
3164 goto cleanup;
3165 if (m == MATCH_NO)
3167 m = gfc_match_pointer_assignment ();
3168 if (m == MATCH_ERROR)
3169 goto cleanup;
3170 if (m == MATCH_NO)
3171 goto syntax;
3174 c = gfc_get_code ();
3175 *c = new_st;
3176 c->loc = gfc_current_locus;
3178 if (gfc_match_eos () != MATCH_YES)
3179 goto syntax;
3181 gfc_clear_new_st ();
3182 new_st.op = EXEC_FORALL;
3183 new_st.expr = mask;
3184 new_st.ext.forall_iterator = head;
3185 new_st.block = gfc_get_code ();
3187 new_st.block->op = EXEC_FORALL;
3188 new_st.block->next = c;
3190 return MATCH_YES;
3192 syntax:
3193 gfc_syntax_error (ST_FORALL);
3195 cleanup:
3196 gfc_free_forall_iterator (head);
3197 gfc_free_expr (mask);
3199 return MATCH_ERROR;
3203 /* Match a FORALL statement. */
3205 match
3206 gfc_match_forall (gfc_statement * st)
3208 gfc_forall_iterator *head;
3209 gfc_expr *mask;
3210 gfc_code *c;
3211 match m0, m;
3213 head = NULL;
3214 mask = NULL;
3215 c = NULL;
3217 m0 = gfc_match_label ();
3218 if (m0 == MATCH_ERROR)
3219 return MATCH_ERROR;
3221 m = gfc_match (" forall");
3222 if (m != MATCH_YES)
3223 return m;
3225 m = match_forall_header (&head, &mask);
3226 if (m == MATCH_ERROR)
3227 goto cleanup;
3228 if (m == MATCH_NO)
3229 goto syntax;
3231 if (gfc_match_eos () == MATCH_YES)
3233 *st = ST_FORALL_BLOCK;
3235 new_st.op = EXEC_FORALL;
3236 new_st.expr = mask;
3237 new_st.ext.forall_iterator = head;
3239 return MATCH_YES;
3242 m = gfc_match_assignment ();
3243 if (m == MATCH_ERROR)
3244 goto cleanup;
3245 if (m == MATCH_NO)
3247 m = gfc_match_pointer_assignment ();
3248 if (m == MATCH_ERROR)
3249 goto cleanup;
3250 if (m == MATCH_NO)
3251 goto syntax;
3254 c = gfc_get_code ();
3255 *c = new_st;
3257 if (gfc_match_eos () != MATCH_YES)
3258 goto syntax;
3260 gfc_clear_new_st ();
3261 new_st.op = EXEC_FORALL;
3262 new_st.expr = mask;
3263 new_st.ext.forall_iterator = head;
3264 new_st.block = gfc_get_code ();
3266 new_st.block->op = EXEC_FORALL;
3267 new_st.block->next = c;
3269 *st = ST_FORALL;
3270 return MATCH_YES;
3272 syntax:
3273 gfc_syntax_error (ST_FORALL);
3275 cleanup:
3276 gfc_free_forall_iterator (head);
3277 gfc_free_expr (mask);
3278 gfc_free_statements (c);
3279 return MATCH_NO;