Daily bump.
[official-gcc.git] / gcc / fortran / match.c
blob0dc2c7295b15aabe80f02e8dc6161c844074931d
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 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 ("parens", INTRINSIC_PARENTHESES),
62 minit (NULL, INTRINSIC_NONE)
66 /******************** Generic matching subroutines ************************/
68 /* In free form, match at least one space. Always matches in fixed
69 form. */
71 match
72 gfc_match_space (void)
74 locus old_loc;
75 int c;
77 if (gfc_current_form == FORM_FIXED)
78 return MATCH_YES;
80 old_loc = gfc_current_locus;
82 c = gfc_next_char ();
83 if (!gfc_is_whitespace (c))
85 gfc_current_locus = old_loc;
86 return MATCH_NO;
89 gfc_gobble_whitespace ();
91 return MATCH_YES;
95 /* Match an end of statement. End of statement is optional
96 whitespace, followed by a ';' or '\n' or comment '!'. If a
97 semicolon is found, we continue to eat whitespace and semicolons. */
99 match
100 gfc_match_eos (void)
102 locus old_loc;
103 int flag, c;
105 flag = 0;
107 for (;;)
109 old_loc = gfc_current_locus;
110 gfc_gobble_whitespace ();
112 c = gfc_next_char ();
113 switch (c)
115 case '!':
118 c = gfc_next_char ();
120 while (c != '\n');
122 /* Fall through */
124 case '\n':
125 return MATCH_YES;
127 case ';':
128 flag = 1;
129 continue;
132 break;
135 gfc_current_locus = old_loc;
136 return (flag) ? MATCH_YES : MATCH_NO;
140 /* Match a literal integer on the input, setting the value on
141 MATCH_YES. Literal ints occur in kind-parameters as well as
142 old-style character length specifications. If cnt is non-NULL it
143 will be set to the number of digits. */
145 match
146 gfc_match_small_literal_int (int *value, int *cnt)
148 locus old_loc;
149 char c;
150 int i, j;
152 old_loc = gfc_current_locus;
154 gfc_gobble_whitespace ();
155 c = gfc_next_char ();
156 if (cnt)
157 *cnt = 0;
159 if (!ISDIGIT (c))
161 gfc_current_locus = old_loc;
162 return MATCH_NO;
165 i = c - '0';
166 j = 1;
168 for (;;)
170 old_loc = gfc_current_locus;
171 c = gfc_next_char ();
173 if (!ISDIGIT (c))
174 break;
176 i = 10 * i + c - '0';
177 j++;
179 if (i > 99999999)
181 gfc_error ("Integer too large at %C");
182 return MATCH_ERROR;
186 gfc_current_locus = old_loc;
188 *value = i;
189 if (cnt)
190 *cnt = j;
191 return MATCH_YES;
195 /* Match a small, constant integer expression, like in a kind
196 statement. On MATCH_YES, 'value' is set. */
198 match
199 gfc_match_small_int (int *value)
201 gfc_expr *expr;
202 const char *p;
203 match m;
204 int i;
206 m = gfc_match_expr (&expr);
207 if (m != MATCH_YES)
208 return m;
210 p = gfc_extract_int (expr, &i);
211 gfc_free_expr (expr);
213 if (p != NULL)
215 gfc_error (p);
216 m = MATCH_ERROR;
219 *value = i;
220 return m;
224 /* Matches a statement label. Uses gfc_match_small_literal_int() to
225 do most of the work. */
227 match
228 gfc_match_st_label (gfc_st_label ** label)
230 locus old_loc;
231 match m;
232 int i, cnt;
234 old_loc = gfc_current_locus;
236 m = gfc_match_small_literal_int (&i, &cnt);
237 if (m != MATCH_YES)
238 return m;
240 if (cnt > 5)
242 gfc_error ("Too many digits in statement label at %C");
243 goto cleanup;
246 if (i == 0)
248 gfc_error ("Statement label at %C is zero");
249 goto cleanup;
252 *label = gfc_get_st_label (i);
253 return MATCH_YES;
255 cleanup:
257 gfc_current_locus = old_loc;
258 return MATCH_ERROR;
262 /* Match and validate a label associated with a named IF, DO or SELECT
263 statement. If the symbol does not have the label attribute, we add
264 it. We also make sure the symbol does not refer to another
265 (active) block. A matched label is pointed to by gfc_new_block. */
267 match
268 gfc_match_label (void)
270 char name[GFC_MAX_SYMBOL_LEN + 1];
271 match m;
273 gfc_new_block = NULL;
275 m = gfc_match (" %n :", name);
276 if (m != MATCH_YES)
277 return m;
279 if (gfc_get_symbol (name, NULL, &gfc_new_block))
281 gfc_error ("Label name '%s' at %C is ambiguous", name);
282 return MATCH_ERROR;
285 if (gfc_new_block->attr.flavor == FL_LABEL)
287 gfc_error ("Duplicate construct label '%s' at %C", name);
288 return MATCH_ERROR;
291 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
292 gfc_new_block->name, NULL) == FAILURE)
293 return MATCH_ERROR;
295 return MATCH_YES;
299 /* Try and match the input against an array of possibilities. If one
300 potential matching string is a substring of another, the longest
301 match takes precedence. Spaces in the target strings are optional
302 spaces that do not necessarily have to be found in the input
303 stream. In fixed mode, spaces never appear. If whitespace is
304 matched, it matches unlimited whitespace in the input. For this
305 reason, the 'mp' member of the mstring structure is used to track
306 the progress of each potential match.
308 If there is no match we return the tag associated with the
309 terminating NULL mstring structure and leave the locus pointer
310 where it started. If there is a match we return the tag member of
311 the matched mstring and leave the locus pointer after the matched
312 character.
314 A '%' character is a mandatory space. */
317 gfc_match_strings (mstring * a)
319 mstring *p, *best_match;
320 int no_match, c, possibles;
321 locus match_loc;
323 possibles = 0;
325 for (p = a; p->string != NULL; p++)
327 p->mp = p->string;
328 possibles++;
331 no_match = p->tag;
333 best_match = NULL;
334 match_loc = gfc_current_locus;
336 gfc_gobble_whitespace ();
338 while (possibles > 0)
340 c = gfc_next_char ();
342 /* Apply the next character to the current possibilities. */
343 for (p = a; p->string != NULL; p++)
345 if (p->mp == NULL)
346 continue;
348 if (*p->mp == ' ')
350 /* Space matches 1+ whitespace(s). */
351 if ((gfc_current_form == FORM_FREE)
352 && gfc_is_whitespace (c))
353 continue;
355 p->mp++;
358 if (*p->mp != c)
360 /* Match failed. */
361 p->mp = NULL;
362 possibles--;
363 continue;
366 p->mp++;
367 if (*p->mp == '\0')
369 /* Found a match. */
370 match_loc = gfc_current_locus;
371 best_match = p;
372 possibles--;
373 p->mp = NULL;
378 gfc_current_locus = match_loc;
380 return (best_match == NULL) ? no_match : best_match->tag;
384 /* See if the current input looks like a name of some sort. Modifies
385 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long. */
387 match
388 gfc_match_name (char *buffer)
390 locus old_loc;
391 int i, c;
393 old_loc = gfc_current_locus;
394 gfc_gobble_whitespace ();
396 c = gfc_next_char ();
397 if (!ISALPHA (c))
399 gfc_current_locus = old_loc;
400 return MATCH_NO;
403 i = 0;
407 buffer[i++] = c;
409 if (i > gfc_option.max_identifier_length)
411 gfc_error ("Name at %C is too long");
412 return MATCH_ERROR;
415 old_loc = gfc_current_locus;
416 c = gfc_next_char ();
418 while (ISALNUM (c)
419 || c == '_'
420 || (gfc_option.flag_dollar_ok && c == '$'));
422 buffer[i] = '\0';
423 gfc_current_locus = old_loc;
425 return MATCH_YES;
429 /* Match a symbol on the input. Modifies the pointer to the symbol
430 pointer if successful. */
432 match
433 gfc_match_sym_tree (gfc_symtree ** matched_symbol, int host_assoc)
435 char buffer[GFC_MAX_SYMBOL_LEN + 1];
436 match m;
438 m = gfc_match_name (buffer);
439 if (m != MATCH_YES)
440 return m;
442 if (host_assoc)
443 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
444 ? MATCH_ERROR : MATCH_YES;
446 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
447 return MATCH_ERROR;
449 return MATCH_YES;
453 match
454 gfc_match_symbol (gfc_symbol ** matched_symbol, int host_assoc)
456 gfc_symtree *st;
457 match m;
459 m = gfc_match_sym_tree (&st, host_assoc);
461 if (m == MATCH_YES)
463 if (st)
464 *matched_symbol = st->n.sym;
465 else
466 *matched_symbol = NULL;
468 else
469 *matched_symbol = NULL;
470 return m;
473 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
474 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
475 in matchexp.c. */
477 match
478 gfc_match_intrinsic_op (gfc_intrinsic_op * result)
480 gfc_intrinsic_op op;
482 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
484 if (op == INTRINSIC_NONE)
485 return MATCH_NO;
487 *result = op;
488 return MATCH_YES;
492 /* Match a loop control phrase:
494 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
496 If the final integer expression is not present, a constant unity
497 expression is returned. We don't return MATCH_ERROR until after
498 the equals sign is seen. */
500 match
501 gfc_match_iterator (gfc_iterator * iter, int init_flag)
503 char name[GFC_MAX_SYMBOL_LEN + 1];
504 gfc_expr *var, *e1, *e2, *e3;
505 locus start;
506 match m;
508 /* Match the start of an iterator without affecting the symbol
509 table. */
511 start = gfc_current_locus;
512 m = gfc_match (" %n =", name);
513 gfc_current_locus = start;
515 if (m != MATCH_YES)
516 return MATCH_NO;
518 m = gfc_match_variable (&var, 0);
519 if (m != MATCH_YES)
520 return MATCH_NO;
522 gfc_match_char ('=');
524 e1 = e2 = e3 = NULL;
526 if (var->ref != NULL)
528 gfc_error ("Loop variable at %C cannot be a sub-component");
529 goto cleanup;
532 if (var->symtree->n.sym->attr.intent == INTENT_IN)
534 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
535 var->symtree->n.sym->name);
536 goto cleanup;
539 if (var->symtree->n.sym->attr.pointer)
541 gfc_error ("Loop variable at %C cannot have the POINTER attribute");
542 goto cleanup;
545 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
546 if (m == MATCH_NO)
547 goto syntax;
548 if (m == MATCH_ERROR)
549 goto cleanup;
551 if (gfc_match_char (',') != MATCH_YES)
552 goto syntax;
554 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
555 if (m == MATCH_NO)
556 goto syntax;
557 if (m == MATCH_ERROR)
558 goto cleanup;
560 if (gfc_match_char (',') != MATCH_YES)
562 e3 = gfc_int_expr (1);
563 goto done;
566 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
567 if (m == MATCH_ERROR)
568 goto cleanup;
569 if (m == MATCH_NO)
571 gfc_error ("Expected a step value in iterator at %C");
572 goto cleanup;
575 done:
576 iter->var = var;
577 iter->start = e1;
578 iter->end = e2;
579 iter->step = e3;
580 return MATCH_YES;
582 syntax:
583 gfc_error ("Syntax error in iterator at %C");
585 cleanup:
586 gfc_free_expr (e1);
587 gfc_free_expr (e2);
588 gfc_free_expr (e3);
590 return MATCH_ERROR;
594 /* Tries to match the next non-whitespace character on the input.
595 This subroutine does not return MATCH_ERROR. */
597 match
598 gfc_match_char (char c)
600 locus where;
602 where = gfc_current_locus;
603 gfc_gobble_whitespace ();
605 if (gfc_next_char () == c)
606 return MATCH_YES;
608 gfc_current_locus = where;
609 return MATCH_NO;
613 /* General purpose matching subroutine. The target string is a
614 scanf-like format string in which spaces correspond to arbitrary
615 whitespace (including no whitespace), characters correspond to
616 themselves. The %-codes are:
618 %% Literal percent sign
619 %e Expression, pointer to a pointer is set
620 %s Symbol, pointer to the symbol is set
621 %n Name, character buffer is set to name
622 %t Matches end of statement.
623 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
624 %l Matches a statement label
625 %v Matches a variable expression (an lvalue)
626 % Matches a required space (in free form) and optional spaces. */
628 match
629 gfc_match (const char *target, ...)
631 gfc_st_label **label;
632 int matches, *ip;
633 locus old_loc;
634 va_list argp;
635 char c, *np;
636 match m, n;
637 void **vp;
638 const char *p;
640 old_loc = gfc_current_locus;
641 va_start (argp, target);
642 m = MATCH_NO;
643 matches = 0;
644 p = target;
646 loop:
647 c = *p++;
648 switch (c)
650 case ' ':
651 gfc_gobble_whitespace ();
652 goto loop;
653 case '\0':
654 m = MATCH_YES;
655 break;
657 case '%':
658 c = *p++;
659 switch (c)
661 case 'e':
662 vp = va_arg (argp, void **);
663 n = gfc_match_expr ((gfc_expr **) vp);
664 if (n != MATCH_YES)
666 m = n;
667 goto not_yes;
670 matches++;
671 goto loop;
673 case 'v':
674 vp = va_arg (argp, void **);
675 n = gfc_match_variable ((gfc_expr **) vp, 0);
676 if (n != MATCH_YES)
678 m = n;
679 goto not_yes;
682 matches++;
683 goto loop;
685 case 's':
686 vp = va_arg (argp, void **);
687 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
688 if (n != MATCH_YES)
690 m = n;
691 goto not_yes;
694 matches++;
695 goto loop;
697 case 'n':
698 np = va_arg (argp, char *);
699 n = gfc_match_name (np);
700 if (n != MATCH_YES)
702 m = n;
703 goto not_yes;
706 matches++;
707 goto loop;
709 case 'l':
710 label = va_arg (argp, gfc_st_label **);
711 n = gfc_match_st_label (label);
712 if (n != MATCH_YES)
714 m = n;
715 goto not_yes;
718 matches++;
719 goto loop;
721 case 'o':
722 ip = va_arg (argp, int *);
723 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
724 if (n != MATCH_YES)
726 m = n;
727 goto not_yes;
730 matches++;
731 goto loop;
733 case 't':
734 if (gfc_match_eos () != MATCH_YES)
736 m = MATCH_NO;
737 goto not_yes;
739 goto loop;
741 case ' ':
742 if (gfc_match_space () == MATCH_YES)
743 goto loop;
744 m = MATCH_NO;
745 goto not_yes;
747 case '%':
748 break; /* Fall through to character matcher */
750 default:
751 gfc_internal_error ("gfc_match(): Bad match code %c", c);
754 default:
755 if (c == gfc_next_char ())
756 goto loop;
757 break;
760 not_yes:
761 va_end (argp);
763 if (m != MATCH_YES)
765 /* Clean up after a failed match. */
766 gfc_current_locus = old_loc;
767 va_start (argp, target);
769 p = target;
770 for (; matches > 0; matches--)
772 while (*p++ != '%');
774 switch (*p++)
776 case '%':
777 matches++;
778 break; /* Skip */
780 /* Matches that don't have to be undone */
781 case 'o':
782 case 'l':
783 case 'n':
784 case 's':
785 (void)va_arg (argp, void **);
786 break;
788 case 'e':
789 case 'v':
790 vp = va_arg (argp, void **);
791 gfc_free_expr (*vp);
792 *vp = NULL;
793 break;
797 va_end (argp);
800 return m;
804 /*********************** Statement level matching **********************/
806 /* Matches the start of a program unit, which is the program keyword
807 followed by an obligatory symbol. */
809 match
810 gfc_match_program (void)
812 gfc_symbol *sym;
813 match m;
815 m = gfc_match ("% %s%t", &sym);
817 if (m == MATCH_NO)
819 gfc_error ("Invalid form of PROGRAM statement at %C");
820 m = MATCH_ERROR;
823 if (m == MATCH_ERROR)
824 return m;
826 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
827 return MATCH_ERROR;
829 gfc_new_block = sym;
831 return MATCH_YES;
835 /* Match a simple assignment statement. */
837 match
838 gfc_match_assignment (void)
840 gfc_expr *lvalue, *rvalue;
841 locus old_loc;
842 match m;
844 old_loc = gfc_current_locus;
846 lvalue = NULL;
847 m = gfc_match (" %v =", &lvalue);
848 if (m != MATCH_YES)
850 gfc_current_locus = old_loc;
851 gfc_free_expr (lvalue);
852 return MATCH_NO;
855 if (lvalue->symtree->n.sym->attr.protected
856 && lvalue->symtree->n.sym->attr.use_assoc)
858 gfc_current_locus = old_loc;
859 gfc_free_expr (lvalue);
860 gfc_error ("Setting value of PROTECTED variable at %C");
861 return MATCH_ERROR;
864 rvalue = NULL;
865 m = gfc_match (" %e%t", &rvalue);
866 if (m != MATCH_YES)
868 gfc_current_locus = old_loc;
869 gfc_free_expr (lvalue);
870 gfc_free_expr (rvalue);
871 return m;
874 gfc_set_sym_referenced (lvalue->symtree->n.sym);
876 new_st.op = EXEC_ASSIGN;
877 new_st.expr = lvalue;
878 new_st.expr2 = rvalue;
880 gfc_check_do_variable (lvalue->symtree);
882 return MATCH_YES;
886 /* Match a pointer assignment statement. */
888 match
889 gfc_match_pointer_assignment (void)
891 gfc_expr *lvalue, *rvalue;
892 locus old_loc;
893 match m;
895 old_loc = gfc_current_locus;
897 lvalue = rvalue = NULL;
899 m = gfc_match (" %v =>", &lvalue);
900 if (m != MATCH_YES)
902 m = MATCH_NO;
903 goto cleanup;
906 m = gfc_match (" %e%t", &rvalue);
907 if (m != MATCH_YES)
908 goto cleanup;
910 if (lvalue->symtree->n.sym->attr.protected
911 && lvalue->symtree->n.sym->attr.use_assoc)
913 gfc_error ("Assigning to a PROTECTED pointer at %C");
914 m = MATCH_ERROR;
915 goto cleanup;
919 new_st.op = EXEC_POINTER_ASSIGN;
920 new_st.expr = lvalue;
921 new_st.expr2 = rvalue;
923 return MATCH_YES;
925 cleanup:
926 gfc_current_locus = old_loc;
927 gfc_free_expr (lvalue);
928 gfc_free_expr (rvalue);
929 return m;
933 /* We try to match an easy arithmetic IF statement. This only happens
934 when just after having encountered a simple IF statement. This code
935 is really duplicate with parts of the gfc_match_if code, but this is
936 *much* easier. */
937 static match
938 match_arithmetic_if (void)
940 gfc_st_label *l1, *l2, *l3;
941 gfc_expr *expr;
942 match m;
944 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
945 if (m != MATCH_YES)
946 return m;
948 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
949 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
950 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
952 gfc_free_expr (expr);
953 return MATCH_ERROR;
956 if (gfc_notify_std (GFC_STD_F95_DEL,
957 "Obsolete: arithmetic IF statement at %C") == FAILURE)
958 return MATCH_ERROR;
960 new_st.op = EXEC_ARITHMETIC_IF;
961 new_st.expr = expr;
962 new_st.label = l1;
963 new_st.label2 = l2;
964 new_st.label3 = l3;
966 return MATCH_YES;
970 /* The IF statement is a bit of a pain. First of all, there are three
971 forms of it, the simple IF, the IF that starts a block and the
972 arithmetic IF.
974 There is a problem with the simple IF and that is the fact that we
975 only have a single level of undo information on symbols. What this
976 means is for a simple IF, we must re-match the whole IF statement
977 multiple times in order to guarantee that the symbol table ends up
978 in the proper state. */
980 static match match_simple_forall (void);
981 static match match_simple_where (void);
983 match
984 gfc_match_if (gfc_statement * if_type)
986 gfc_expr *expr;
987 gfc_st_label *l1, *l2, *l3;
988 locus old_loc;
989 gfc_code *p;
990 match m, n;
992 n = gfc_match_label ();
993 if (n == MATCH_ERROR)
994 return n;
996 old_loc = gfc_current_locus;
998 m = gfc_match (" if ( %e", &expr);
999 if (m != MATCH_YES)
1000 return m;
1002 if (gfc_match_char (')') != MATCH_YES)
1004 gfc_error ("Syntax error in IF-expression at %C");
1005 gfc_free_expr (expr);
1006 return MATCH_ERROR;
1009 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1011 if (m == MATCH_YES)
1013 if (n == MATCH_YES)
1015 gfc_error
1016 ("Block label not appropriate for arithmetic IF statement "
1017 "at %C");
1019 gfc_free_expr (expr);
1020 return MATCH_ERROR;
1023 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1024 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1025 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1028 gfc_free_expr (expr);
1029 return MATCH_ERROR;
1032 if (gfc_notify_std (GFC_STD_F95_DEL,
1033 "Obsolete: arithmetic IF statement at %C")
1034 == FAILURE)
1035 return MATCH_ERROR;
1037 new_st.op = EXEC_ARITHMETIC_IF;
1038 new_st.expr = expr;
1039 new_st.label = l1;
1040 new_st.label2 = l2;
1041 new_st.label3 = l3;
1043 *if_type = ST_ARITHMETIC_IF;
1044 return MATCH_YES;
1047 if (gfc_match (" then%t") == MATCH_YES)
1049 new_st.op = EXEC_IF;
1050 new_st.expr = expr;
1052 *if_type = ST_IF_BLOCK;
1053 return MATCH_YES;
1056 if (n == MATCH_YES)
1058 gfc_error ("Block label is not appropriate IF statement at %C");
1060 gfc_free_expr (expr);
1061 return MATCH_ERROR;
1064 /* At this point the only thing left is a simple IF statement. At
1065 this point, n has to be MATCH_NO, so we don't have to worry about
1066 re-matching a block label. From what we've got so far, try
1067 matching an assignment. */
1069 *if_type = ST_SIMPLE_IF;
1071 m = gfc_match_assignment ();
1072 if (m == MATCH_YES)
1073 goto got_match;
1075 gfc_free_expr (expr);
1076 gfc_undo_symbols ();
1077 gfc_current_locus = old_loc;
1079 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1080 assignment was found. For MATCH_NO, continue to call the various
1081 matchers. */
1082 if (m == MATCH_ERROR)
1083 return MATCH_ERROR;
1085 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1087 m = gfc_match_pointer_assignment ();
1088 if (m == MATCH_YES)
1089 goto got_match;
1091 gfc_free_expr (expr);
1092 gfc_undo_symbols ();
1093 gfc_current_locus = old_loc;
1095 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1097 /* Look at the next keyword to see which matcher to call. Matching
1098 the keyword doesn't affect the symbol table, so we don't have to
1099 restore between tries. */
1101 #define match(string, subr, statement) \
1102 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1104 gfc_clear_error ();
1106 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1107 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1108 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1109 match ("call", gfc_match_call, ST_CALL)
1110 match ("close", gfc_match_close, ST_CLOSE)
1111 match ("continue", gfc_match_continue, ST_CONTINUE)
1112 match ("cycle", gfc_match_cycle, ST_CYCLE)
1113 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1114 match ("end file", gfc_match_endfile, ST_END_FILE)
1115 match ("exit", gfc_match_exit, ST_EXIT)
1116 match ("flush", gfc_match_flush, ST_FLUSH)
1117 match ("forall", match_simple_forall, ST_FORALL)
1118 match ("go to", gfc_match_goto, ST_GOTO)
1119 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1120 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1121 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1122 match ("open", gfc_match_open, ST_OPEN)
1123 match ("pause", gfc_match_pause, ST_NONE)
1124 match ("print", gfc_match_print, ST_WRITE)
1125 match ("read", gfc_match_read, ST_READ)
1126 match ("return", gfc_match_return, ST_RETURN)
1127 match ("rewind", gfc_match_rewind, ST_REWIND)
1128 match ("stop", gfc_match_stop, ST_STOP)
1129 match ("where", match_simple_where, ST_WHERE)
1130 match ("write", gfc_match_write, ST_WRITE)
1132 /* The gfc_match_assignment() above may have returned a MATCH_NO
1133 where the assignment was to a named constant. Check that
1134 special case here. */
1135 m = gfc_match_assignment ();
1136 if (m == MATCH_NO)
1138 gfc_error ("Cannot assign to a named constant at %C");
1139 gfc_free_expr (expr);
1140 gfc_undo_symbols ();
1141 gfc_current_locus = old_loc;
1142 return MATCH_ERROR;
1145 /* All else has failed, so give up. See if any of the matchers has
1146 stored an error message of some sort. */
1147 if (gfc_error_check () == 0)
1148 gfc_error ("Unclassifiable statement in IF-clause at %C");
1150 gfc_free_expr (expr);
1151 return MATCH_ERROR;
1153 got_match:
1154 if (m == MATCH_NO)
1155 gfc_error ("Syntax error in IF-clause at %C");
1156 if (m != MATCH_YES)
1158 gfc_free_expr (expr);
1159 return MATCH_ERROR;
1162 /* At this point, we've matched the single IF and the action clause
1163 is in new_st. Rearrange things so that the IF statement appears
1164 in new_st. */
1166 p = gfc_get_code ();
1167 p->next = gfc_get_code ();
1168 *p->next = new_st;
1169 p->next->loc = gfc_current_locus;
1171 p->expr = expr;
1172 p->op = EXEC_IF;
1174 gfc_clear_new_st ();
1176 new_st.op = EXEC_IF;
1177 new_st.block = p;
1179 return MATCH_YES;
1182 #undef match
1185 /* Match an ELSE statement. */
1187 match
1188 gfc_match_else (void)
1190 char name[GFC_MAX_SYMBOL_LEN + 1];
1192 if (gfc_match_eos () == MATCH_YES)
1193 return MATCH_YES;
1195 if (gfc_match_name (name) != MATCH_YES
1196 || gfc_current_block () == NULL
1197 || gfc_match_eos () != MATCH_YES)
1199 gfc_error ("Unexpected junk after ELSE statement at %C");
1200 return MATCH_ERROR;
1203 if (strcmp (name, gfc_current_block ()->name) != 0)
1205 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1206 name, gfc_current_block ()->name);
1207 return MATCH_ERROR;
1210 return MATCH_YES;
1214 /* Match an ELSE IF statement. */
1216 match
1217 gfc_match_elseif (void)
1219 char name[GFC_MAX_SYMBOL_LEN + 1];
1220 gfc_expr *expr;
1221 match m;
1223 m = gfc_match (" ( %e ) then", &expr);
1224 if (m != MATCH_YES)
1225 return m;
1227 if (gfc_match_eos () == MATCH_YES)
1228 goto done;
1230 if (gfc_match_name (name) != MATCH_YES
1231 || gfc_current_block () == NULL
1232 || gfc_match_eos () != MATCH_YES)
1234 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1235 goto cleanup;
1238 if (strcmp (name, gfc_current_block ()->name) != 0)
1240 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1241 name, gfc_current_block ()->name);
1242 goto cleanup;
1245 done:
1246 new_st.op = EXEC_IF;
1247 new_st.expr = expr;
1248 return MATCH_YES;
1250 cleanup:
1251 gfc_free_expr (expr);
1252 return MATCH_ERROR;
1256 /* Free a gfc_iterator structure. */
1258 void
1259 gfc_free_iterator (gfc_iterator * iter, int flag)
1262 if (iter == NULL)
1263 return;
1265 gfc_free_expr (iter->var);
1266 gfc_free_expr (iter->start);
1267 gfc_free_expr (iter->end);
1268 gfc_free_expr (iter->step);
1270 if (flag)
1271 gfc_free (iter);
1275 /* Match a DO statement. */
1277 match
1278 gfc_match_do (void)
1280 gfc_iterator iter, *ip;
1281 locus old_loc;
1282 gfc_st_label *label;
1283 match m;
1285 old_loc = gfc_current_locus;
1287 label = NULL;
1288 iter.var = iter.start = iter.end = iter.step = NULL;
1290 m = gfc_match_label ();
1291 if (m == MATCH_ERROR)
1292 return m;
1294 if (gfc_match (" do") != MATCH_YES)
1295 return MATCH_NO;
1297 m = gfc_match_st_label (&label);
1298 if (m == MATCH_ERROR)
1299 goto cleanup;
1301 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1303 if (gfc_match_eos () == MATCH_YES)
1305 iter.end = gfc_logical_expr (1, NULL);
1306 new_st.op = EXEC_DO_WHILE;
1307 goto done;
1310 /* match an optional comma, if no comma is found a space is obligatory. */
1311 if (gfc_match_char(',') != MATCH_YES
1312 && gfc_match ("% ") != MATCH_YES)
1313 return MATCH_NO;
1315 /* See if we have a DO WHILE. */
1316 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1318 new_st.op = EXEC_DO_WHILE;
1319 goto done;
1322 /* The abortive DO WHILE may have done something to the symbol
1323 table, so we start over: */
1324 gfc_undo_symbols ();
1325 gfc_current_locus = old_loc;
1327 gfc_match_label (); /* This won't error */
1328 gfc_match (" do "); /* This will work */
1330 gfc_match_st_label (&label); /* Can't error out */
1331 gfc_match_char (','); /* Optional comma */
1333 m = gfc_match_iterator (&iter, 0);
1334 if (m == MATCH_NO)
1335 return MATCH_NO;
1336 if (m == MATCH_ERROR)
1337 goto cleanup;
1339 gfc_check_do_variable (iter.var->symtree);
1341 if (gfc_match_eos () != MATCH_YES)
1343 gfc_syntax_error (ST_DO);
1344 goto cleanup;
1347 new_st.op = EXEC_DO;
1349 done:
1350 if (label != NULL
1351 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1352 goto cleanup;
1354 new_st.label = label;
1356 if (new_st.op == EXEC_DO_WHILE)
1357 new_st.expr = iter.end;
1358 else
1360 new_st.ext.iterator = ip = gfc_get_iterator ();
1361 *ip = iter;
1364 return MATCH_YES;
1366 cleanup:
1367 gfc_free_iterator (&iter, 0);
1369 return MATCH_ERROR;
1373 /* Match an EXIT or CYCLE statement. */
1375 static match
1376 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1378 gfc_state_data *p, *o;
1379 gfc_symbol *sym;
1380 match m;
1382 if (gfc_match_eos () == MATCH_YES)
1383 sym = NULL;
1384 else
1386 m = gfc_match ("% %s%t", &sym);
1387 if (m == MATCH_ERROR)
1388 return MATCH_ERROR;
1389 if (m == MATCH_NO)
1391 gfc_syntax_error (st);
1392 return MATCH_ERROR;
1395 if (sym->attr.flavor != FL_LABEL)
1397 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1398 sym->name, gfc_ascii_statement (st));
1399 return MATCH_ERROR;
1403 /* Find the loop mentioned specified by the label (or lack of a
1404 label). */
1405 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1406 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1407 break;
1408 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1409 o = p;
1411 if (p == NULL)
1413 if (sym == NULL)
1414 gfc_error ("%s statement at %C is not within a loop",
1415 gfc_ascii_statement (st));
1416 else
1417 gfc_error ("%s statement at %C is not within loop '%s'",
1418 gfc_ascii_statement (st), sym->name);
1420 return MATCH_ERROR;
1423 if (o != NULL)
1425 gfc_error ("%s statement at %C leaving OpenMP structured block",
1426 gfc_ascii_statement (st));
1427 return MATCH_ERROR;
1429 else if (st == ST_EXIT
1430 && p->previous != NULL
1431 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1432 && (p->previous->head->op == EXEC_OMP_DO
1433 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1435 gcc_assert (p->previous->head->next != NULL);
1436 gcc_assert (p->previous->head->next->op == EXEC_DO
1437 || p->previous->head->next->op == EXEC_DO_WHILE);
1438 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1439 return MATCH_ERROR;
1442 /* Save the first statement in the loop - needed by the backend. */
1443 new_st.ext.whichloop = p->head;
1445 new_st.op = op;
1446 /* new_st.sym = sym;*/
1448 return MATCH_YES;
1452 /* Match the EXIT statement. */
1454 match
1455 gfc_match_exit (void)
1458 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1462 /* Match the CYCLE statement. */
1464 match
1465 gfc_match_cycle (void)
1468 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1472 /* Match a number or character constant after a STOP or PAUSE statement. */
1474 static match
1475 gfc_match_stopcode (gfc_statement st)
1477 int stop_code;
1478 gfc_expr *e;
1479 match m;
1480 int cnt;
1482 stop_code = -1;
1483 e = NULL;
1485 if (gfc_match_eos () != MATCH_YES)
1487 m = gfc_match_small_literal_int (&stop_code, &cnt);
1488 if (m == MATCH_ERROR)
1489 goto cleanup;
1491 if (m == MATCH_YES && cnt > 5)
1493 gfc_error ("Too many digits in STOP code at %C");
1494 goto cleanup;
1497 if (m == MATCH_NO)
1499 /* Try a character constant. */
1500 m = gfc_match_expr (&e);
1501 if (m == MATCH_ERROR)
1502 goto cleanup;
1503 if (m == MATCH_NO)
1504 goto syntax;
1505 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1506 goto syntax;
1509 if (gfc_match_eos () != MATCH_YES)
1510 goto syntax;
1513 if (gfc_pure (NULL))
1515 gfc_error ("%s statement not allowed in PURE procedure at %C",
1516 gfc_ascii_statement (st));
1517 goto cleanup;
1520 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1521 new_st.expr = e;
1522 new_st.ext.stop_code = stop_code;
1524 return MATCH_YES;
1526 syntax:
1527 gfc_syntax_error (st);
1529 cleanup:
1531 gfc_free_expr (e);
1532 return MATCH_ERROR;
1535 /* Match the (deprecated) PAUSE statement. */
1537 match
1538 gfc_match_pause (void)
1540 match m;
1542 m = gfc_match_stopcode (ST_PAUSE);
1543 if (m == MATCH_YES)
1545 if (gfc_notify_std (GFC_STD_F95_DEL,
1546 "Obsolete: PAUSE statement at %C")
1547 == FAILURE)
1548 m = MATCH_ERROR;
1550 return m;
1554 /* Match the STOP statement. */
1556 match
1557 gfc_match_stop (void)
1559 return gfc_match_stopcode (ST_STOP);
1563 /* Match a CONTINUE statement. */
1565 match
1566 gfc_match_continue (void)
1569 if (gfc_match_eos () != MATCH_YES)
1571 gfc_syntax_error (ST_CONTINUE);
1572 return MATCH_ERROR;
1575 new_st.op = EXEC_CONTINUE;
1576 return MATCH_YES;
1580 /* Match the (deprecated) ASSIGN statement. */
1582 match
1583 gfc_match_assign (void)
1585 gfc_expr *expr;
1586 gfc_st_label *label;
1588 if (gfc_match (" %l", &label) == MATCH_YES)
1590 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1591 return MATCH_ERROR;
1592 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1594 if (gfc_notify_std (GFC_STD_F95_DEL,
1595 "Obsolete: ASSIGN statement at %C")
1596 == FAILURE)
1597 return MATCH_ERROR;
1599 expr->symtree->n.sym->attr.assign = 1;
1601 new_st.op = EXEC_LABEL_ASSIGN;
1602 new_st.label = label;
1603 new_st.expr = expr;
1604 return MATCH_YES;
1607 return MATCH_NO;
1611 /* Match the GO TO statement. As a computed GOTO statement is
1612 matched, it is transformed into an equivalent SELECT block. No
1613 tree is necessary, and the resulting jumps-to-jumps are
1614 specifically optimized away by the back end. */
1616 match
1617 gfc_match_goto (void)
1619 gfc_code *head, *tail;
1620 gfc_expr *expr;
1621 gfc_case *cp;
1622 gfc_st_label *label;
1623 int i;
1624 match m;
1626 if (gfc_match (" %l%t", &label) == MATCH_YES)
1628 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1629 return MATCH_ERROR;
1631 new_st.op = EXEC_GOTO;
1632 new_st.label = label;
1633 return MATCH_YES;
1636 /* The assigned GO TO statement. */
1638 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1640 if (gfc_notify_std (GFC_STD_F95_DEL,
1641 "Obsolete: Assigned GOTO statement at %C")
1642 == FAILURE)
1643 return MATCH_ERROR;
1645 new_st.op = EXEC_GOTO;
1646 new_st.expr = expr;
1648 if (gfc_match_eos () == MATCH_YES)
1649 return MATCH_YES;
1651 /* Match label list. */
1652 gfc_match_char (',');
1653 if (gfc_match_char ('(') != MATCH_YES)
1655 gfc_syntax_error (ST_GOTO);
1656 return MATCH_ERROR;
1658 head = tail = NULL;
1662 m = gfc_match_st_label (&label);
1663 if (m != MATCH_YES)
1664 goto syntax;
1666 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1667 goto cleanup;
1669 if (head == NULL)
1670 head = tail = gfc_get_code ();
1671 else
1673 tail->block = gfc_get_code ();
1674 tail = tail->block;
1677 tail->label = label;
1678 tail->op = EXEC_GOTO;
1680 while (gfc_match_char (',') == MATCH_YES);
1682 if (gfc_match (")%t") != MATCH_YES)
1683 goto syntax;
1685 if (head == NULL)
1687 gfc_error (
1688 "Statement label list in GOTO at %C cannot be empty");
1689 goto syntax;
1691 new_st.block = head;
1693 return MATCH_YES;
1696 /* Last chance is a computed GO TO statement. */
1697 if (gfc_match_char ('(') != MATCH_YES)
1699 gfc_syntax_error (ST_GOTO);
1700 return MATCH_ERROR;
1703 head = tail = NULL;
1704 i = 1;
1708 m = gfc_match_st_label (&label);
1709 if (m != MATCH_YES)
1710 goto syntax;
1712 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1713 goto cleanup;
1715 if (head == NULL)
1716 head = tail = gfc_get_code ();
1717 else
1719 tail->block = gfc_get_code ();
1720 tail = tail->block;
1723 cp = gfc_get_case ();
1724 cp->low = cp->high = gfc_int_expr (i++);
1726 tail->op = EXEC_SELECT;
1727 tail->ext.case_list = cp;
1729 tail->next = gfc_get_code ();
1730 tail->next->op = EXEC_GOTO;
1731 tail->next->label = label;
1733 while (gfc_match_char (',') == MATCH_YES);
1735 if (gfc_match_char (')') != MATCH_YES)
1736 goto syntax;
1738 if (head == NULL)
1740 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1741 goto syntax;
1744 /* Get the rest of the statement. */
1745 gfc_match_char (',');
1747 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1748 goto syntax;
1750 /* At this point, a computed GOTO has been fully matched and an
1751 equivalent SELECT statement constructed. */
1753 new_st.op = EXEC_SELECT;
1754 new_st.expr = NULL;
1756 /* Hack: For a "real" SELECT, the expression is in expr. We put
1757 it in expr2 so we can distinguish then and produce the correct
1758 diagnostics. */
1759 new_st.expr2 = expr;
1760 new_st.block = head;
1761 return MATCH_YES;
1763 syntax:
1764 gfc_syntax_error (ST_GOTO);
1765 cleanup:
1766 gfc_free_statements (head);
1767 return MATCH_ERROR;
1771 /* Frees a list of gfc_alloc structures. */
1773 void
1774 gfc_free_alloc_list (gfc_alloc * p)
1776 gfc_alloc *q;
1778 for (; p; p = q)
1780 q = p->next;
1781 gfc_free_expr (p->expr);
1782 gfc_free (p);
1787 /* Match an ALLOCATE statement. */
1789 match
1790 gfc_match_allocate (void)
1792 gfc_alloc *head, *tail;
1793 gfc_expr *stat;
1794 match m;
1796 head = tail = NULL;
1797 stat = NULL;
1799 if (gfc_match_char ('(') != MATCH_YES)
1800 goto syntax;
1802 for (;;)
1804 if (head == NULL)
1805 head = tail = gfc_get_alloc ();
1806 else
1808 tail->next = gfc_get_alloc ();
1809 tail = tail->next;
1812 m = gfc_match_variable (&tail->expr, 0);
1813 if (m == MATCH_NO)
1814 goto syntax;
1815 if (m == MATCH_ERROR)
1816 goto cleanup;
1818 if (gfc_check_do_variable (tail->expr->symtree))
1819 goto cleanup;
1821 if (gfc_pure (NULL)
1822 && gfc_impure_variable (tail->expr->symtree->n.sym))
1824 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1825 "PURE procedure");
1826 goto cleanup;
1829 if (tail->expr->ts.type == BT_DERIVED)
1830 tail->expr->ts.derived = gfc_use_derived (tail->expr->ts.derived);
1832 if (gfc_match_char (',') != MATCH_YES)
1833 break;
1835 m = gfc_match (" stat = %v", &stat);
1836 if (m == MATCH_ERROR)
1837 goto cleanup;
1838 if (m == MATCH_YES)
1839 break;
1842 if (stat != NULL)
1844 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1846 gfc_error
1847 ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1848 "INTENT(IN)", stat->symtree->n.sym->name);
1849 goto cleanup;
1852 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1854 gfc_error
1855 ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1856 "procedure");
1857 goto cleanup;
1860 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1862 gfc_error("STAT expression at %C must be a variable");
1863 goto cleanup;
1866 gfc_check_do_variable(stat->symtree);
1869 if (gfc_match (" )%t") != MATCH_YES)
1870 goto syntax;
1872 new_st.op = EXEC_ALLOCATE;
1873 new_st.expr = stat;
1874 new_st.ext.alloc_list = head;
1876 return MATCH_YES;
1878 syntax:
1879 gfc_syntax_error (ST_ALLOCATE);
1881 cleanup:
1882 gfc_free_expr (stat);
1883 gfc_free_alloc_list (head);
1884 return MATCH_ERROR;
1888 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1889 a set of pointer assignments to intrinsic NULL(). */
1891 match
1892 gfc_match_nullify (void)
1894 gfc_code *tail;
1895 gfc_expr *e, *p;
1896 match m;
1898 tail = NULL;
1900 if (gfc_match_char ('(') != MATCH_YES)
1901 goto syntax;
1903 for (;;)
1905 m = gfc_match_variable (&p, 0);
1906 if (m == MATCH_ERROR)
1907 goto cleanup;
1908 if (m == MATCH_NO)
1909 goto syntax;
1911 if (gfc_check_do_variable(p->symtree))
1912 goto cleanup;
1914 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1916 gfc_error
1917 ("Illegal variable in NULLIFY at %C for a PURE procedure");
1918 goto cleanup;
1921 /* build ' => NULL() ' */
1922 e = gfc_get_expr ();
1923 e->where = gfc_current_locus;
1924 e->expr_type = EXPR_NULL;
1925 e->ts.type = BT_UNKNOWN;
1927 /* Chain to list */
1928 if (tail == NULL)
1929 tail = &new_st;
1930 else
1932 tail->next = gfc_get_code ();
1933 tail = tail->next;
1936 tail->op = EXEC_POINTER_ASSIGN;
1937 tail->expr = p;
1938 tail->expr2 = e;
1940 if (gfc_match (" )%t") == MATCH_YES)
1941 break;
1942 if (gfc_match_char (',') != MATCH_YES)
1943 goto syntax;
1946 return MATCH_YES;
1948 syntax:
1949 gfc_syntax_error (ST_NULLIFY);
1951 cleanup:
1952 gfc_free_statements (new_st.next);
1953 return MATCH_ERROR;
1957 /* Match a DEALLOCATE statement. */
1959 match
1960 gfc_match_deallocate (void)
1962 gfc_alloc *head, *tail;
1963 gfc_expr *stat;
1964 match m;
1966 head = tail = NULL;
1967 stat = NULL;
1969 if (gfc_match_char ('(') != MATCH_YES)
1970 goto syntax;
1972 for (;;)
1974 if (head == NULL)
1975 head = tail = gfc_get_alloc ();
1976 else
1978 tail->next = gfc_get_alloc ();
1979 tail = tail->next;
1982 m = gfc_match_variable (&tail->expr, 0);
1983 if (m == MATCH_ERROR)
1984 goto cleanup;
1985 if (m == MATCH_NO)
1986 goto syntax;
1988 if (gfc_check_do_variable (tail->expr->symtree))
1989 goto cleanup;
1991 if (gfc_pure (NULL)
1992 && gfc_impure_variable (tail->expr->symtree->n.sym))
1994 gfc_error
1995 ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1996 "procedure");
1997 goto cleanup;
2000 if (gfc_match_char (',') != MATCH_YES)
2001 break;
2003 m = gfc_match (" stat = %v", &stat);
2004 if (m == MATCH_ERROR)
2005 goto cleanup;
2006 if (m == MATCH_YES)
2007 break;
2010 if (stat != NULL)
2012 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
2014 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
2015 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
2016 goto cleanup;
2019 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
2021 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
2022 "for a PURE procedure");
2023 goto cleanup;
2026 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
2028 gfc_error("STAT expression at %C must be a variable");
2029 goto cleanup;
2032 gfc_check_do_variable(stat->symtree);
2035 if (gfc_match (" )%t") != MATCH_YES)
2036 goto syntax;
2038 new_st.op = EXEC_DEALLOCATE;
2039 new_st.expr = stat;
2040 new_st.ext.alloc_list = head;
2042 return MATCH_YES;
2044 syntax:
2045 gfc_syntax_error (ST_DEALLOCATE);
2047 cleanup:
2048 gfc_free_expr (stat);
2049 gfc_free_alloc_list (head);
2050 return MATCH_ERROR;
2054 /* Match a RETURN statement. */
2056 match
2057 gfc_match_return (void)
2059 gfc_expr *e;
2060 match m;
2061 gfc_compile_state s;
2062 int c;
2064 e = NULL;
2065 if (gfc_match_eos () == MATCH_YES)
2066 goto done;
2068 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2070 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2071 "a SUBROUTINE");
2072 goto cleanup;
2075 if (gfc_current_form == FORM_FREE)
2077 /* The following are valid, so we can't require a blank after the
2078 RETURN keyword:
2079 return+1
2080 return(1) */
2081 c = gfc_peek_char ();
2082 if (ISALPHA (c) || ISDIGIT (c))
2083 return MATCH_NO;
2086 m = gfc_match (" %e%t", &e);
2087 if (m == MATCH_YES)
2088 goto done;
2089 if (m == MATCH_ERROR)
2090 goto cleanup;
2092 gfc_syntax_error (ST_RETURN);
2094 cleanup:
2095 gfc_free_expr (e);
2096 return MATCH_ERROR;
2098 done:
2099 gfc_enclosing_unit (&s);
2100 if (s == COMP_PROGRAM
2101 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2102 "main program at %C") == FAILURE)
2103 return MATCH_ERROR;
2105 new_st.op = EXEC_RETURN;
2106 new_st.expr = e;
2108 return MATCH_YES;
2112 /* Match a CALL statement. The tricky part here are possible
2113 alternate return specifiers. We handle these by having all
2114 "subroutines" actually return an integer via a register that gives
2115 the return number. If the call specifies alternate returns, we
2116 generate code for a SELECT statement whose case clauses contain
2117 GOTOs to the various labels. */
2119 match
2120 gfc_match_call (void)
2122 char name[GFC_MAX_SYMBOL_LEN + 1];
2123 gfc_actual_arglist *a, *arglist;
2124 gfc_case *new_case;
2125 gfc_symbol *sym;
2126 gfc_symtree *st;
2127 gfc_code *c;
2128 match m;
2129 int i;
2131 arglist = NULL;
2133 m = gfc_match ("% %n", name);
2134 if (m == MATCH_NO)
2135 goto syntax;
2136 if (m != MATCH_YES)
2137 return m;
2139 if (gfc_get_ha_sym_tree (name, &st))
2140 return MATCH_ERROR;
2142 sym = st->n.sym;
2143 gfc_set_sym_referenced (sym);
2145 if (!sym->attr.generic
2146 && !sym->attr.subroutine
2147 && gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2148 return MATCH_ERROR;
2150 if (gfc_match_eos () != MATCH_YES)
2152 m = gfc_match_actual_arglist (1, &arglist);
2153 if (m == MATCH_NO)
2154 goto syntax;
2155 if (m == MATCH_ERROR)
2156 goto cleanup;
2158 if (gfc_match_eos () != MATCH_YES)
2159 goto syntax;
2162 /* If any alternate return labels were found, construct a SELECT
2163 statement that will jump to the right place. */
2165 i = 0;
2166 for (a = arglist; a; a = a->next)
2167 if (a->expr == NULL)
2168 i = 1;
2170 if (i)
2172 gfc_symtree *select_st;
2173 gfc_symbol *select_sym;
2174 char name[GFC_MAX_SYMBOL_LEN + 1];
2176 new_st.next = c = gfc_get_code ();
2177 c->op = EXEC_SELECT;
2178 sprintf (name, "_result_%s",sym->name);
2179 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail */
2181 select_sym = select_st->n.sym;
2182 select_sym->ts.type = BT_INTEGER;
2183 select_sym->ts.kind = gfc_default_integer_kind;
2184 gfc_set_sym_referenced (select_sym);
2185 c->expr = gfc_get_expr ();
2186 c->expr->expr_type = EXPR_VARIABLE;
2187 c->expr->symtree = select_st;
2188 c->expr->ts = select_sym->ts;
2189 c->expr->where = gfc_current_locus;
2191 i = 0;
2192 for (a = arglist; a; a = a->next)
2194 if (a->expr != NULL)
2195 continue;
2197 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2198 continue;
2200 i++;
2202 c->block = gfc_get_code ();
2203 c = c->block;
2204 c->op = EXEC_SELECT;
2206 new_case = gfc_get_case ();
2207 new_case->high = new_case->low = gfc_int_expr (i);
2208 c->ext.case_list = new_case;
2210 c->next = gfc_get_code ();
2211 c->next->op = EXEC_GOTO;
2212 c->next->label = a->label;
2216 new_st.op = EXEC_CALL;
2217 new_st.symtree = st;
2218 new_st.ext.actual = arglist;
2220 return MATCH_YES;
2222 syntax:
2223 gfc_syntax_error (ST_CALL);
2225 cleanup:
2226 gfc_free_actual_arglist (arglist);
2227 return MATCH_ERROR;
2231 /* Given a name, return a pointer to the common head structure,
2232 creating it if it does not exist. If FROM_MODULE is nonzero, we
2233 mangle the name so that it doesn't interfere with commons defined
2234 in the using namespace.
2235 TODO: Add to global symbol tree. */
2237 gfc_common_head *
2238 gfc_get_common (const char *name, int from_module)
2240 gfc_symtree *st;
2241 static int serial = 0;
2242 char mangled_name[GFC_MAX_SYMBOL_LEN+1];
2244 if (from_module)
2246 /* A use associated common block is only needed to correctly layout
2247 the variables it contains. */
2248 snprintf(mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2249 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2251 else
2253 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2255 if (st == NULL)
2256 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2259 if (st->n.common == NULL)
2261 st->n.common = gfc_get_common_head ();
2262 st->n.common->where = gfc_current_locus;
2263 strcpy (st->n.common->name, name);
2266 return st->n.common;
2270 /* Match a common block name. */
2272 static match
2273 match_common_name (char *name)
2275 match m;
2277 if (gfc_match_char ('/') == MATCH_NO)
2279 name[0] = '\0';
2280 return MATCH_YES;
2283 if (gfc_match_char ('/') == MATCH_YES)
2285 name[0] = '\0';
2286 return MATCH_YES;
2289 m = gfc_match_name (name);
2291 if (m == MATCH_ERROR)
2292 return MATCH_ERROR;
2293 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2294 return MATCH_YES;
2296 gfc_error ("Syntax error in common block name at %C");
2297 return MATCH_ERROR;
2301 /* Match a COMMON statement. */
2303 match
2304 gfc_match_common (void)
2306 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2307 char name[GFC_MAX_SYMBOL_LEN+1];
2308 gfc_common_head *t;
2309 gfc_array_spec *as;
2310 gfc_equiv * e1, * e2;
2311 match m;
2312 gfc_gsymbol *gsym;
2314 old_blank_common = gfc_current_ns->blank_common.head;
2315 if (old_blank_common)
2317 while (old_blank_common->common_next)
2318 old_blank_common = old_blank_common->common_next;
2321 as = NULL;
2323 for (;;)
2325 m = match_common_name (name);
2326 if (m == MATCH_ERROR)
2327 goto cleanup;
2329 gsym = gfc_get_gsymbol (name);
2330 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2332 gfc_error ("Symbol '%s' at %C is already an external symbol that is not COMMON",
2333 name);
2334 goto cleanup;
2337 if (gsym->type == GSYM_UNKNOWN)
2339 gsym->type = GSYM_COMMON;
2340 gsym->where = gfc_current_locus;
2341 gsym->defined = 1;
2344 gsym->used = 1;
2346 if (name[0] == '\0')
2348 if (gfc_current_ns->is_block_data)
2350 gfc_warning ("BLOCK DATA unit cannot contain blank COMMON at %C");
2352 t = &gfc_current_ns->blank_common;
2353 if (t->head == NULL)
2354 t->where = gfc_current_locus;
2356 else
2358 t = gfc_get_common (name, 0);
2360 head = &t->head;
2362 if (*head == NULL)
2363 tail = NULL;
2364 else
2366 tail = *head;
2367 while (tail->common_next)
2368 tail = tail->common_next;
2371 /* Grab the list of symbols. */
2372 for (;;)
2374 m = gfc_match_symbol (&sym, 0);
2375 if (m == MATCH_ERROR)
2376 goto cleanup;
2377 if (m == MATCH_NO)
2378 goto syntax;
2380 if (sym->attr.in_common)
2382 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2383 sym->name);
2384 goto cleanup;
2387 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2388 goto cleanup;
2390 if (sym->value != NULL
2391 && (name[0] == '\0' || !sym->attr.data))
2393 if (name[0] == '\0')
2394 gfc_error ("Previously initialized symbol '%s' in "
2395 "blank COMMON block at %C", sym->name);
2396 else
2397 gfc_error ("Previously initialized symbol '%s' in "
2398 "COMMON block '%s' at %C", sym->name, name);
2399 goto cleanup;
2402 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2403 goto cleanup;
2405 /* Derived type names must have the SEQUENCE attribute. */
2406 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2408 gfc_error
2409 ("Derived type variable in COMMON at %C does not have the "
2410 "SEQUENCE attribute");
2411 goto cleanup;
2414 if (tail != NULL)
2415 tail->common_next = sym;
2416 else
2417 *head = sym;
2419 tail = sym;
2421 /* Deal with an optional array specification after the
2422 symbol name. */
2423 m = gfc_match_array_spec (&as);
2424 if (m == MATCH_ERROR)
2425 goto cleanup;
2427 if (m == MATCH_YES)
2429 if (as->type != AS_EXPLICIT)
2431 gfc_error
2432 ("Array specification for symbol '%s' in COMMON at %C "
2433 "must be explicit", sym->name);
2434 goto cleanup;
2437 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2438 goto cleanup;
2440 if (sym->attr.pointer)
2442 gfc_error
2443 ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2444 sym->name);
2445 goto cleanup;
2448 sym->as = as;
2449 as = NULL;
2453 sym->common_head = t;
2455 /* Check to see if the symbol is already in an equivalence group.
2456 If it is, set the other members as being in common. */
2457 if (sym->attr.in_equivalence)
2459 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2461 for (e2 = e1; e2; e2 = e2->eq)
2462 if (e2->expr->symtree->n.sym == sym)
2463 goto equiv_found;
2465 continue;
2467 equiv_found:
2469 for (e2 = e1; e2; e2 = e2->eq)
2471 other = e2->expr->symtree->n.sym;
2472 if (other->common_head
2473 && other->common_head != sym->common_head)
2475 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2476 "%C is being indirectly equivalenced to "
2477 "another COMMON block '%s'",
2478 sym->name,
2479 sym->common_head->name,
2480 other->common_head->name);
2481 goto cleanup;
2483 other->attr.in_common = 1;
2484 other->common_head = t;
2490 gfc_gobble_whitespace ();
2491 if (gfc_match_eos () == MATCH_YES)
2492 goto done;
2493 if (gfc_peek_char () == '/')
2494 break;
2495 if (gfc_match_char (',') != MATCH_YES)
2496 goto syntax;
2497 gfc_gobble_whitespace ();
2498 if (gfc_peek_char () == '/')
2499 break;
2503 done:
2504 return MATCH_YES;
2506 syntax:
2507 gfc_syntax_error (ST_COMMON);
2509 cleanup:
2510 if (old_blank_common)
2511 old_blank_common->common_next = NULL;
2512 else
2513 gfc_current_ns->blank_common.head = NULL;
2514 gfc_free_array_spec (as);
2515 return MATCH_ERROR;
2519 /* Match a BLOCK DATA program unit. */
2521 match
2522 gfc_match_block_data (void)
2524 char name[GFC_MAX_SYMBOL_LEN + 1];
2525 gfc_symbol *sym;
2526 match m;
2528 if (gfc_match_eos () == MATCH_YES)
2530 gfc_new_block = NULL;
2531 return MATCH_YES;
2534 m = gfc_match ("% %n%t", name);
2535 if (m != MATCH_YES)
2536 return MATCH_ERROR;
2538 if (gfc_get_symbol (name, NULL, &sym))
2539 return MATCH_ERROR;
2541 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2542 return MATCH_ERROR;
2544 gfc_new_block = sym;
2546 return MATCH_YES;
2550 /* Free a namelist structure. */
2552 void
2553 gfc_free_namelist (gfc_namelist * name)
2555 gfc_namelist *n;
2557 for (; name; name = n)
2559 n = name->next;
2560 gfc_free (name);
2565 /* Match a NAMELIST statement. */
2567 match
2568 gfc_match_namelist (void)
2570 gfc_symbol *group_name, *sym;
2571 gfc_namelist *nl;
2572 match m, m2;
2574 m = gfc_match (" / %s /", &group_name);
2575 if (m == MATCH_NO)
2576 goto syntax;
2577 if (m == MATCH_ERROR)
2578 goto error;
2580 for (;;)
2582 if (group_name->ts.type != BT_UNKNOWN)
2584 gfc_error
2585 ("Namelist group name '%s' at %C already has a basic type "
2586 "of %s", group_name->name, gfc_typename (&group_name->ts));
2587 return MATCH_ERROR;
2590 if (group_name->attr.flavor == FL_NAMELIST
2591 && group_name->attr.use_assoc
2592 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2593 "at %C already is USE associated and can"
2594 "not be respecified.", group_name->name)
2595 == FAILURE)
2596 return MATCH_ERROR;
2598 if (group_name->attr.flavor != FL_NAMELIST
2599 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2600 group_name->name, NULL) == FAILURE)
2601 return MATCH_ERROR;
2603 for (;;)
2605 m = gfc_match_symbol (&sym, 1);
2606 if (m == MATCH_NO)
2607 goto syntax;
2608 if (m == MATCH_ERROR)
2609 goto error;
2611 if (sym->attr.in_namelist == 0
2612 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2613 goto error;
2615 /* Use gfc_error_check here, rather than goto error, so that this
2616 these are the only errors for the next two lines. */
2617 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2619 gfc_error ("Assumed size array '%s' in namelist '%s' at "
2620 "%C is not allowed", sym->name, group_name->name);
2621 gfc_error_check ();
2624 if (sym->as && sym->as->type == AS_ASSUMED_SHAPE
2625 && gfc_notify_std (GFC_STD_GNU, "Assumed shape array '%s' in "
2626 "namelist '%s' at %C is an extension.",
2627 sym->name, group_name->name) == FAILURE)
2628 gfc_error_check ();
2630 nl = gfc_get_namelist ();
2631 nl->sym = sym;
2632 sym->refs++;
2634 if (group_name->namelist == NULL)
2635 group_name->namelist = group_name->namelist_tail = nl;
2636 else
2638 group_name->namelist_tail->next = nl;
2639 group_name->namelist_tail = nl;
2642 if (gfc_match_eos () == MATCH_YES)
2643 goto done;
2645 m = gfc_match_char (',');
2647 if (gfc_match_char ('/') == MATCH_YES)
2649 m2 = gfc_match (" %s /", &group_name);
2650 if (m2 == MATCH_YES)
2651 break;
2652 if (m2 == MATCH_ERROR)
2653 goto error;
2654 goto syntax;
2657 if (m != MATCH_YES)
2658 goto syntax;
2662 done:
2663 return MATCH_YES;
2665 syntax:
2666 gfc_syntax_error (ST_NAMELIST);
2668 error:
2669 return MATCH_ERROR;
2673 /* Match a MODULE statement. */
2675 match
2676 gfc_match_module (void)
2678 match m;
2680 m = gfc_match (" %s%t", &gfc_new_block);
2681 if (m != MATCH_YES)
2682 return m;
2684 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2685 gfc_new_block->name, NULL) == FAILURE)
2686 return MATCH_ERROR;
2688 return MATCH_YES;
2692 /* Free equivalence sets and lists. Recursively is the easiest way to
2693 do this. */
2695 void
2696 gfc_free_equiv (gfc_equiv * eq)
2699 if (eq == NULL)
2700 return;
2702 gfc_free_equiv (eq->eq);
2703 gfc_free_equiv (eq->next);
2705 gfc_free_expr (eq->expr);
2706 gfc_free (eq);
2710 /* Match an EQUIVALENCE statement. */
2712 match
2713 gfc_match_equivalence (void)
2715 gfc_equiv *eq, *set, *tail;
2716 gfc_ref *ref;
2717 gfc_symbol *sym;
2718 match m;
2719 gfc_common_head *common_head = NULL;
2720 bool common_flag;
2721 int cnt;
2723 tail = NULL;
2725 for (;;)
2727 eq = gfc_get_equiv ();
2728 if (tail == NULL)
2729 tail = eq;
2731 eq->next = gfc_current_ns->equiv;
2732 gfc_current_ns->equiv = eq;
2734 if (gfc_match_char ('(') != MATCH_YES)
2735 goto syntax;
2737 set = eq;
2738 common_flag = FALSE;
2739 cnt = 0;
2741 for (;;)
2743 m = gfc_match_equiv_variable (&set->expr);
2744 if (m == MATCH_ERROR)
2745 goto cleanup;
2746 if (m == MATCH_NO)
2747 goto syntax;
2749 /* count the number of objects. */
2750 cnt++;
2752 if (gfc_match_char ('%') == MATCH_YES)
2754 gfc_error ("Derived type component %C is not a "
2755 "permitted EQUIVALENCE member");
2756 goto cleanup;
2759 for (ref = set->expr->ref; ref; ref = ref->next)
2760 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2762 gfc_error
2763 ("Array reference in EQUIVALENCE at %C cannot be an "
2764 "array section");
2765 goto cleanup;
2768 sym = set->expr->symtree->n.sym;
2770 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL)
2771 == FAILURE)
2772 goto cleanup;
2774 if (sym->attr.in_common)
2776 common_flag = TRUE;
2777 common_head = sym->common_head;
2780 if (gfc_match_char (')') == MATCH_YES)
2781 break;
2783 if (gfc_match_char (',') != MATCH_YES)
2784 goto syntax;
2786 set->eq = gfc_get_equiv ();
2787 set = set->eq;
2790 if (cnt < 2)
2792 gfc_error ("EQUIVALENCE at %C requires two or more objects");
2793 goto cleanup;
2796 /* If one of the members of an equivalence is in common, then
2797 mark them all as being in common. Before doing this, check
2798 that members of the equivalence group are not in different
2799 common blocks. */
2800 if (common_flag)
2801 for (set = eq; set; set = set->eq)
2803 sym = set->expr->symtree->n.sym;
2804 if (sym->common_head && sym->common_head != common_head)
2806 gfc_error ("Attempt to indirectly overlap COMMON "
2807 "blocks %s and %s by EQUIVALENCE at %C",
2808 sym->common_head->name,
2809 common_head->name);
2810 goto cleanup;
2812 sym->attr.in_common = 1;
2813 sym->common_head = common_head;
2816 if (gfc_match_eos () == MATCH_YES)
2817 break;
2818 if (gfc_match_char (',') != MATCH_YES)
2819 goto syntax;
2822 return MATCH_YES;
2824 syntax:
2825 gfc_syntax_error (ST_EQUIVALENCE);
2827 cleanup:
2828 eq = tail->next;
2829 tail->next = NULL;
2831 gfc_free_equiv (gfc_current_ns->equiv);
2832 gfc_current_ns->equiv = eq;
2834 return MATCH_ERROR;
2837 /* Check that a statement function is not recursive. This is done by looking
2838 for the statement function symbol(sym) by looking recursively through its
2839 expression(e). If a reference to sym is found, true is returned.
2840 12.5.4 requires that any variable of function that is implicitly typed
2841 shall have that type confirmed by any subsequent type declaration. The
2842 implicit typing is conveniently done here. */
2844 static bool
2845 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
2847 gfc_actual_arglist *arg;
2848 gfc_ref *ref;
2849 int i;
2851 if (e == NULL)
2852 return false;
2854 switch (e->expr_type)
2856 case EXPR_FUNCTION:
2857 for (arg = e->value.function.actual; arg; arg = arg->next)
2859 if (sym->name == arg->name
2860 || recursive_stmt_fcn (arg->expr, sym))
2861 return true;
2864 if (e->symtree == NULL)
2865 return false;
2867 /* Check the name before testing for nested recursion! */
2868 if (sym->name == e->symtree->n.sym->name)
2869 return true;
2871 /* Catch recursion via other statement functions. */
2872 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
2873 && e->symtree->n.sym->value
2874 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
2875 return true;
2877 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
2878 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
2880 break;
2882 case EXPR_VARIABLE:
2883 if (e->symtree && sym->name == e->symtree->n.sym->name)
2884 return true;
2886 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
2887 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
2888 break;
2890 case EXPR_OP:
2891 if (recursive_stmt_fcn (e->value.op.op1, sym)
2892 || recursive_stmt_fcn (e->value.op.op2, sym))
2893 return true;
2894 break;
2896 default:
2897 break;
2900 /* Component references do not need to be checked. */
2901 if (e->ref)
2903 for (ref = e->ref; ref; ref = ref->next)
2905 switch (ref->type)
2907 case REF_ARRAY:
2908 for (i = 0; i < ref->u.ar.dimen; i++)
2910 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
2911 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
2912 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
2913 return true;
2915 break;
2917 case REF_SUBSTRING:
2918 if (recursive_stmt_fcn (ref->u.ss.start, sym)
2919 || recursive_stmt_fcn (ref->u.ss.end, sym))
2920 return true;
2922 break;
2924 default:
2925 break;
2929 return false;
2933 /* Match a statement function declaration. It is so easy to match
2934 non-statement function statements with a MATCH_ERROR as opposed to
2935 MATCH_NO that we suppress error message in most cases. */
2937 match
2938 gfc_match_st_function (void)
2940 gfc_error_buf old_error;
2941 gfc_symbol *sym;
2942 gfc_expr *expr;
2943 match m;
2945 m = gfc_match_symbol (&sym, 0);
2946 if (m != MATCH_YES)
2947 return m;
2949 gfc_push_error (&old_error);
2951 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
2952 sym->name, NULL) == FAILURE)
2953 goto undo_error;
2955 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2956 goto undo_error;
2958 m = gfc_match (" = %e%t", &expr);
2959 if (m == MATCH_NO)
2960 goto undo_error;
2962 gfc_free_error (&old_error);
2963 if (m == MATCH_ERROR)
2964 return m;
2966 if (recursive_stmt_fcn (expr, sym))
2968 gfc_error ("Statement function at %L is recursive",
2969 &expr->where);
2970 return MATCH_ERROR;
2973 sym->value = expr;
2975 return MATCH_YES;
2977 undo_error:
2978 gfc_pop_error (&old_error);
2979 return MATCH_NO;
2983 /***************** SELECT CASE subroutines ******************/
2985 /* Free a single case structure. */
2987 static void
2988 free_case (gfc_case * p)
2990 if (p->low == p->high)
2991 p->high = NULL;
2992 gfc_free_expr (p->low);
2993 gfc_free_expr (p->high);
2994 gfc_free (p);
2998 /* Free a list of case structures. */
3000 void
3001 gfc_free_case_list (gfc_case * p)
3003 gfc_case *q;
3005 for (; p; p = q)
3007 q = p->next;
3008 free_case (p);
3013 /* Match a single case selector. */
3015 static match
3016 match_case_selector (gfc_case ** cp)
3018 gfc_case *c;
3019 match m;
3021 c = gfc_get_case ();
3022 c->where = gfc_current_locus;
3024 if (gfc_match_char (':') == MATCH_YES)
3026 m = gfc_match_init_expr (&c->high);
3027 if (m == MATCH_NO)
3028 goto need_expr;
3029 if (m == MATCH_ERROR)
3030 goto cleanup;
3033 else
3035 m = gfc_match_init_expr (&c->low);
3036 if (m == MATCH_ERROR)
3037 goto cleanup;
3038 if (m == MATCH_NO)
3039 goto need_expr;
3041 /* If we're not looking at a ':' now, make a range out of a single
3042 target. Else get the upper bound for the case range. */
3043 if (gfc_match_char (':') != MATCH_YES)
3044 c->high = c->low;
3045 else
3047 m = gfc_match_init_expr (&c->high);
3048 if (m == MATCH_ERROR)
3049 goto cleanup;
3050 /* MATCH_NO is fine. It's OK if nothing is there! */
3054 *cp = c;
3055 return MATCH_YES;
3057 need_expr:
3058 gfc_error ("Expected initialization expression in CASE at %C");
3060 cleanup:
3061 free_case (c);
3062 return MATCH_ERROR;
3066 /* Match the end of a case statement. */
3068 static match
3069 match_case_eos (void)
3071 char name[GFC_MAX_SYMBOL_LEN + 1];
3072 match m;
3074 if (gfc_match_eos () == MATCH_YES)
3075 return MATCH_YES;
3077 /* If the case construct doesn't have a case-construct-name, we
3078 should have matched the EOS. */
3079 if (!gfc_current_block ())
3081 gfc_error ("Expected the name of the select case construct at %C");
3082 return MATCH_ERROR;
3085 gfc_gobble_whitespace ();
3087 m = gfc_match_name (name);
3088 if (m != MATCH_YES)
3089 return m;
3091 if (strcmp (name, gfc_current_block ()->name) != 0)
3093 gfc_error ("Expected case name of '%s' at %C",
3094 gfc_current_block ()->name);
3095 return MATCH_ERROR;
3098 return gfc_match_eos ();
3102 /* Match a SELECT statement. */
3104 match
3105 gfc_match_select (void)
3107 gfc_expr *expr;
3108 match m;
3110 m = gfc_match_label ();
3111 if (m == MATCH_ERROR)
3112 return m;
3114 m = gfc_match (" select case ( %e )%t", &expr);
3115 if (m != MATCH_YES)
3116 return m;
3118 new_st.op = EXEC_SELECT;
3119 new_st.expr = expr;
3121 return MATCH_YES;
3125 /* Match a CASE statement. */
3127 match
3128 gfc_match_case (void)
3130 gfc_case *c, *head, *tail;
3131 match m;
3133 head = tail = NULL;
3135 if (gfc_current_state () != COMP_SELECT)
3137 gfc_error ("Unexpected CASE statement at %C");
3138 return MATCH_ERROR;
3141 if (gfc_match ("% default") == MATCH_YES)
3143 m = match_case_eos ();
3144 if (m == MATCH_NO)
3145 goto syntax;
3146 if (m == MATCH_ERROR)
3147 goto cleanup;
3149 new_st.op = EXEC_SELECT;
3150 c = gfc_get_case ();
3151 c->where = gfc_current_locus;
3152 new_st.ext.case_list = c;
3153 return MATCH_YES;
3156 if (gfc_match_char ('(') != MATCH_YES)
3157 goto syntax;
3159 for (;;)
3161 if (match_case_selector (&c) == MATCH_ERROR)
3162 goto cleanup;
3164 if (head == NULL)
3165 head = c;
3166 else
3167 tail->next = c;
3169 tail = c;
3171 if (gfc_match_char (')') == MATCH_YES)
3172 break;
3173 if (gfc_match_char (',') != MATCH_YES)
3174 goto syntax;
3177 m = match_case_eos ();
3178 if (m == MATCH_NO)
3179 goto syntax;
3180 if (m == MATCH_ERROR)
3181 goto cleanup;
3183 new_st.op = EXEC_SELECT;
3184 new_st.ext.case_list = head;
3186 return MATCH_YES;
3188 syntax:
3189 gfc_error ("Syntax error in CASE-specification at %C");
3191 cleanup:
3192 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3193 return MATCH_ERROR;
3196 /********************* WHERE subroutines ********************/
3198 /* Match the rest of a simple WHERE statement that follows an IF statement.
3201 static match
3202 match_simple_where (void)
3204 gfc_expr *expr;
3205 gfc_code *c;
3206 match m;
3208 m = gfc_match (" ( %e )", &expr);
3209 if (m != MATCH_YES)
3210 return m;
3212 m = gfc_match_assignment ();
3213 if (m == MATCH_NO)
3214 goto syntax;
3215 if (m == MATCH_ERROR)
3216 goto cleanup;
3218 if (gfc_match_eos () != MATCH_YES)
3219 goto syntax;
3221 c = gfc_get_code ();
3223 c->op = EXEC_WHERE;
3224 c->expr = expr;
3225 c->next = gfc_get_code ();
3227 *c->next = new_st;
3228 gfc_clear_new_st ();
3230 new_st.op = EXEC_WHERE;
3231 new_st.block = c;
3233 return MATCH_YES;
3235 syntax:
3236 gfc_syntax_error (ST_WHERE);
3238 cleanup:
3239 gfc_free_expr (expr);
3240 return MATCH_ERROR;
3243 /* Match a WHERE statement. */
3245 match
3246 gfc_match_where (gfc_statement * st)
3248 gfc_expr *expr;
3249 match m0, m;
3250 gfc_code *c;
3252 m0 = gfc_match_label ();
3253 if (m0 == MATCH_ERROR)
3254 return m0;
3256 m = gfc_match (" where ( %e )", &expr);
3257 if (m != MATCH_YES)
3258 return m;
3260 if (gfc_match_eos () == MATCH_YES)
3262 *st = ST_WHERE_BLOCK;
3264 new_st.op = EXEC_WHERE;
3265 new_st.expr = expr;
3266 return MATCH_YES;
3269 m = gfc_match_assignment ();
3270 if (m == MATCH_NO)
3271 gfc_syntax_error (ST_WHERE);
3273 if (m != MATCH_YES)
3275 gfc_free_expr (expr);
3276 return MATCH_ERROR;
3279 /* We've got a simple WHERE statement. */
3280 *st = ST_WHERE;
3281 c = gfc_get_code ();
3283 c->op = EXEC_WHERE;
3284 c->expr = expr;
3285 c->next = gfc_get_code ();
3287 *c->next = new_st;
3288 gfc_clear_new_st ();
3290 new_st.op = EXEC_WHERE;
3291 new_st.block = c;
3293 return MATCH_YES;
3297 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3298 new_st if successful. */
3300 match
3301 gfc_match_elsewhere (void)
3303 char name[GFC_MAX_SYMBOL_LEN + 1];
3304 gfc_expr *expr;
3305 match m;
3307 if (gfc_current_state () != COMP_WHERE)
3309 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3310 return MATCH_ERROR;
3313 expr = NULL;
3315 if (gfc_match_char ('(') == MATCH_YES)
3317 m = gfc_match_expr (&expr);
3318 if (m == MATCH_NO)
3319 goto syntax;
3320 if (m == MATCH_ERROR)
3321 return MATCH_ERROR;
3323 if (gfc_match_char (')') != MATCH_YES)
3324 goto syntax;
3327 if (gfc_match_eos () != MATCH_YES)
3328 { /* Better be a name at this point */
3329 m = gfc_match_name (name);
3330 if (m == MATCH_NO)
3331 goto syntax;
3332 if (m == MATCH_ERROR)
3333 goto cleanup;
3335 if (gfc_match_eos () != MATCH_YES)
3336 goto syntax;
3338 if (strcmp (name, gfc_current_block ()->name) != 0)
3340 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3341 name, gfc_current_block ()->name);
3342 goto cleanup;
3346 new_st.op = EXEC_WHERE;
3347 new_st.expr = expr;
3348 return MATCH_YES;
3350 syntax:
3351 gfc_syntax_error (ST_ELSEWHERE);
3353 cleanup:
3354 gfc_free_expr (expr);
3355 return MATCH_ERROR;
3359 /******************** FORALL subroutines ********************/
3361 /* Free a list of FORALL iterators. */
3363 void
3364 gfc_free_forall_iterator (gfc_forall_iterator * iter)
3366 gfc_forall_iterator *next;
3368 while (iter)
3370 next = iter->next;
3372 gfc_free_expr (iter->var);
3373 gfc_free_expr (iter->start);
3374 gfc_free_expr (iter->end);
3375 gfc_free_expr (iter->stride);
3377 gfc_free (iter);
3378 iter = next;
3383 /* Match an iterator as part of a FORALL statement. The format is:
3385 <var> = <start>:<end>[:<stride>][, <scalar mask>] */
3387 static match
3388 match_forall_iterator (gfc_forall_iterator ** result)
3390 gfc_forall_iterator *iter;
3391 locus where;
3392 match m;
3394 where = gfc_current_locus;
3395 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3397 m = gfc_match_variable (&iter->var, 0);
3398 if (m != MATCH_YES)
3399 goto cleanup;
3401 if (gfc_match_char ('=') != MATCH_YES)
3403 m = MATCH_NO;
3404 goto cleanup;
3407 m = gfc_match_expr (&iter->start);
3408 if (m != MATCH_YES)
3409 goto cleanup;
3411 if (gfc_match_char (':') != MATCH_YES)
3412 goto syntax;
3414 m = gfc_match_expr (&iter->end);
3415 if (m == MATCH_NO)
3416 goto syntax;
3417 if (m == MATCH_ERROR)
3418 goto cleanup;
3420 if (gfc_match_char (':') == MATCH_NO)
3421 iter->stride = gfc_int_expr (1);
3422 else
3424 m = gfc_match_expr (&iter->stride);
3425 if (m == MATCH_NO)
3426 goto syntax;
3427 if (m == MATCH_ERROR)
3428 goto cleanup;
3431 /* Mark the iteration variable's symbol as used as a FORALL index. */
3432 iter->var->symtree->n.sym->forall_index = true;
3434 *result = iter;
3435 return MATCH_YES;
3437 syntax:
3438 gfc_error ("Syntax error in FORALL iterator at %C");
3439 m = MATCH_ERROR;
3441 cleanup:
3442 /* Make sure that potential internal function references in the
3443 mask do not get messed up. */
3444 if (iter->var
3445 && iter->var->expr_type == EXPR_VARIABLE
3446 && iter->var->symtree->n.sym->refs == 1)
3447 iter->var->symtree->n.sym->attr.flavor = FL_UNKNOWN;
3449 gfc_current_locus = where;
3450 gfc_free_forall_iterator (iter);
3451 return m;
3455 /* Match the header of a FORALL statement. */
3457 static match
3458 match_forall_header (gfc_forall_iterator ** phead, gfc_expr ** mask)
3460 gfc_forall_iterator *head, *tail, *new;
3461 gfc_expr *msk;
3462 match m;
3464 gfc_gobble_whitespace ();
3466 head = tail = NULL;
3467 msk = NULL;
3469 if (gfc_match_char ('(') != MATCH_YES)
3470 return MATCH_NO;
3472 m = match_forall_iterator (&new);
3473 if (m == MATCH_ERROR)
3474 goto cleanup;
3475 if (m == MATCH_NO)
3476 goto syntax;
3478 head = tail = new;
3480 for (;;)
3482 if (gfc_match_char (',') != MATCH_YES)
3483 break;
3485 m = match_forall_iterator (&new);
3486 if (m == MATCH_ERROR)
3487 goto cleanup;
3489 if (m == MATCH_YES)
3491 tail->next = new;
3492 tail = new;
3493 continue;
3496 /* Have to have a mask expression */
3498 m = gfc_match_expr (&msk);
3499 if (m == MATCH_NO)
3500 goto syntax;
3501 if (m == MATCH_ERROR)
3502 goto cleanup;
3504 break;
3507 if (gfc_match_char (')') == MATCH_NO)
3508 goto syntax;
3510 *phead = head;
3511 *mask = msk;
3512 return MATCH_YES;
3514 syntax:
3515 gfc_syntax_error (ST_FORALL);
3517 cleanup:
3518 gfc_free_expr (msk);
3519 gfc_free_forall_iterator (head);
3521 return MATCH_ERROR;
3524 /* Match the rest of a simple FORALL statement that follows an IF statement.
3527 static match
3528 match_simple_forall (void)
3530 gfc_forall_iterator *head;
3531 gfc_expr *mask;
3532 gfc_code *c;
3533 match m;
3535 mask = NULL;
3536 head = NULL;
3537 c = NULL;
3539 m = match_forall_header (&head, &mask);
3541 if (m == MATCH_NO)
3542 goto syntax;
3543 if (m != MATCH_YES)
3544 goto cleanup;
3546 m = gfc_match_assignment ();
3548 if (m == MATCH_ERROR)
3549 goto cleanup;
3550 if (m == MATCH_NO)
3552 m = gfc_match_pointer_assignment ();
3553 if (m == MATCH_ERROR)
3554 goto cleanup;
3555 if (m == MATCH_NO)
3556 goto syntax;
3559 c = gfc_get_code ();
3560 *c = new_st;
3561 c->loc = gfc_current_locus;
3563 if (gfc_match_eos () != MATCH_YES)
3564 goto syntax;
3566 gfc_clear_new_st ();
3567 new_st.op = EXEC_FORALL;
3568 new_st.expr = mask;
3569 new_st.ext.forall_iterator = head;
3570 new_st.block = gfc_get_code ();
3572 new_st.block->op = EXEC_FORALL;
3573 new_st.block->next = c;
3575 return MATCH_YES;
3577 syntax:
3578 gfc_syntax_error (ST_FORALL);
3580 cleanup:
3581 gfc_free_forall_iterator (head);
3582 gfc_free_expr (mask);
3584 return MATCH_ERROR;
3588 /* Match a FORALL statement. */
3590 match
3591 gfc_match_forall (gfc_statement * st)
3593 gfc_forall_iterator *head;
3594 gfc_expr *mask;
3595 gfc_code *c;
3596 match m0, m;
3598 head = NULL;
3599 mask = NULL;
3600 c = NULL;
3602 m0 = gfc_match_label ();
3603 if (m0 == MATCH_ERROR)
3604 return MATCH_ERROR;
3606 m = gfc_match (" forall");
3607 if (m != MATCH_YES)
3608 return m;
3610 m = match_forall_header (&head, &mask);
3611 if (m == MATCH_ERROR)
3612 goto cleanup;
3613 if (m == MATCH_NO)
3614 goto syntax;
3616 if (gfc_match_eos () == MATCH_YES)
3618 *st = ST_FORALL_BLOCK;
3620 new_st.op = EXEC_FORALL;
3621 new_st.expr = mask;
3622 new_st.ext.forall_iterator = head;
3624 return MATCH_YES;
3627 m = gfc_match_assignment ();
3628 if (m == MATCH_ERROR)
3629 goto cleanup;
3630 if (m == MATCH_NO)
3632 m = gfc_match_pointer_assignment ();
3633 if (m == MATCH_ERROR)
3634 goto cleanup;
3635 if (m == MATCH_NO)
3636 goto syntax;
3639 c = gfc_get_code ();
3640 *c = new_st;
3641 c->loc = gfc_current_locus;
3643 gfc_clear_new_st ();
3644 new_st.op = EXEC_FORALL;
3645 new_st.expr = mask;
3646 new_st.ext.forall_iterator = head;
3647 new_st.block = gfc_get_code ();
3649 new_st.block->op = EXEC_FORALL;
3650 new_st.block->next = c;
3652 *st = ST_FORALL;
3653 return MATCH_YES;
3655 syntax:
3656 gfc_syntax_error (ST_FORALL);
3658 cleanup:
3659 gfc_free_forall_iterator (head);
3660 gfc_free_expr (mask);
3661 gfc_free_statements (c);
3662 return MATCH_NO;