OSDN Git Service

2006-06-25 Paul Thomas <pault@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / match.c
1 /* Matching subroutines in all sizes, shapes and colors.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
3    Foundation, Inc.
4    Contributed by Andy Vaught
5
6 This file is part of GCC.
7
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.
12
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.
17
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.  */
22
23
24 #include "config.h"
25 #include "system.h"
26 #include "flags.h"
27 #include "gfortran.h"
28 #include "match.h"
29 #include "parse.h"
30
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.  */
34
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)
63 };
64
65
66 /******************** Generic matching subroutines ************************/
67
68 /* In free form, match at least one space.  Always matches in fixed
69    form.  */
70
71 match
72 gfc_match_space (void)
73 {
74   locus old_loc;
75   int c;
76
77   if (gfc_current_form == FORM_FIXED)
78     return MATCH_YES;
79
80   old_loc = gfc_current_locus;
81
82   c = gfc_next_char ();
83   if (!gfc_is_whitespace (c))
84     {
85       gfc_current_locus = old_loc;
86       return MATCH_NO;
87     }
88
89   gfc_gobble_whitespace ();
90
91   return MATCH_YES;
92 }
93
94
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.  */
98
99 match
100 gfc_match_eos (void)
101 {
102   locus old_loc;
103   int flag, c;
104
105   flag = 0;
106
107   for (;;)
108     {
109       old_loc = gfc_current_locus;
110       gfc_gobble_whitespace ();
111
112       c = gfc_next_char ();
113       switch (c)
114         {
115         case '!':
116           do
117             {
118               c = gfc_next_char ();
119             }
120           while (c != '\n');
121
122           /* Fall through */
123
124         case '\n':
125           return MATCH_YES;
126
127         case ';':
128           flag = 1;
129           continue;
130         }
131
132       break;
133     }
134
135   gfc_current_locus = old_loc;
136   return (flag) ? MATCH_YES : MATCH_NO;
137 }
138
139
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.  */
144
145 match
146 gfc_match_small_literal_int (int *value, int *cnt)
147 {
148   locus old_loc;
149   char c;
150   int i, j;
151
152   old_loc = gfc_current_locus;
153
154   gfc_gobble_whitespace ();
155   c = gfc_next_char ();
156   if (cnt)
157     *cnt = 0;
158
159   if (!ISDIGIT (c))
160     {
161       gfc_current_locus = old_loc;
162       return MATCH_NO;
163     }
164
165   i = c - '0';
166   j = 1;
167
168   for (;;)
169     {
170       old_loc = gfc_current_locus;
171       c = gfc_next_char ();
172
173       if (!ISDIGIT (c))
174         break;
175
176       i = 10 * i + c - '0';
177       j++;
178
179       if (i > 99999999)
180         {
181           gfc_error ("Integer too large at %C");
182           return MATCH_ERROR;
183         }
184     }
185
186   gfc_current_locus = old_loc;
187
188   *value = i;
189   if (cnt)
190     *cnt = j;
191   return MATCH_YES;
192 }
193
194
195 /* Match a small, constant integer expression, like in a kind
196    statement.  On MATCH_YES, 'value' is set.  */
197
198 match
199 gfc_match_small_int (int *value)
200 {
201   gfc_expr *expr;
202   const char *p;
203   match m;
204   int i;
205
206   m = gfc_match_expr (&expr);
207   if (m != MATCH_YES)
208     return m;
209
210   p = gfc_extract_int (expr, &i);
211   gfc_free_expr (expr);
212
213   if (p != NULL)
214     {
215       gfc_error (p);
216       m = MATCH_ERROR;
217     }
218
219   *value = i;
220   return m;
221 }
222
223
224 /* Matches a statement label.  Uses gfc_match_small_literal_int() to
225    do most of the work.  */
226
227 match
228 gfc_match_st_label (gfc_st_label ** label)
229 {
230   locus old_loc;
231   match m;
232   int i, cnt;
233
234   old_loc = gfc_current_locus;
235
236   m = gfc_match_small_literal_int (&i, &cnt);
237   if (m != MATCH_YES)
238     return m;
239
240   if (cnt > 5)
241     {
242       gfc_error ("Too many digits in statement label at %C");
243       goto cleanup;
244     }
245
246   if (i == 0)
247     {
248       gfc_error ("Statement label at %C is zero");
249       goto cleanup;
250     }
251
252   *label = gfc_get_st_label (i);
253   return MATCH_YES;
254
255 cleanup:
256
257   gfc_current_locus = old_loc;
258   return MATCH_ERROR;
259 }
260
261
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.  */
266
267 match
268 gfc_match_label (void)
269 {
270   char name[GFC_MAX_SYMBOL_LEN + 1];
271   match m;
272
273   gfc_new_block = NULL;
274
275   m = gfc_match (" %n :", name);
276   if (m != MATCH_YES)
277     return m;
278
279   if (gfc_get_symbol (name, NULL, &gfc_new_block))
280     {
281       gfc_error ("Label name '%s' at %C is ambiguous", name);
282       return MATCH_ERROR;
283     }
284
285   if (gfc_new_block->attr.flavor == FL_LABEL)
286     {
287       gfc_error ("Duplicate construct label '%s' at %C", name);
288       return MATCH_ERROR;
289     }
290
291   if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
292                       gfc_new_block->name, NULL) == FAILURE)
293     return MATCH_ERROR;
294
295   return MATCH_YES;
296 }
297
298
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.
307
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.
313
314    A '%' character is a mandatory space.  */
315
316 int
317 gfc_match_strings (mstring * a)
318 {
319   mstring *p, *best_match;
320   int no_match, c, possibles;
321   locus match_loc;
322
323   possibles = 0;
324
325   for (p = a; p->string != NULL; p++)
326     {
327       p->mp = p->string;
328       possibles++;
329     }
330
331   no_match = p->tag;
332
333   best_match = NULL;
334   match_loc = gfc_current_locus;
335
336   gfc_gobble_whitespace ();
337
338   while (possibles > 0)
339     {
340       c = gfc_next_char ();
341
342       /* Apply the next character to the current possibilities.  */
343       for (p = a; p->string != NULL; p++)
344         {
345           if (p->mp == NULL)
346             continue;
347
348           if (*p->mp == ' ')
349             {
350               /* Space matches 1+ whitespace(s).  */
351               if ((gfc_current_form == FORM_FREE)
352                   && gfc_is_whitespace (c))
353                 continue;
354
355               p->mp++;
356             }
357
358           if (*p->mp != c)
359             {
360               /* Match failed.  */
361               p->mp = NULL;
362               possibles--;
363               continue;
364             }
365
366           p->mp++;
367           if (*p->mp == '\0')
368             {
369               /* Found a match.  */
370               match_loc = gfc_current_locus;
371               best_match = p;
372               possibles--;
373               p->mp = NULL;
374             }
375         }
376     }
377
378   gfc_current_locus = match_loc;
379
380   return (best_match == NULL) ? no_match : best_match->tag;
381 }
382
383
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.  */
386
387 match
388 gfc_match_name (char *buffer)
389 {
390   locus old_loc;
391   int i, c;
392
393   old_loc = gfc_current_locus;
394   gfc_gobble_whitespace ();
395
396   c = gfc_next_char ();
397   if (!ISALPHA (c))
398     {
399       gfc_current_locus = old_loc;
400       return MATCH_NO;
401     }
402
403   i = 0;
404
405   do
406     {
407       buffer[i++] = c;
408
409       if (i > gfc_option.max_identifier_length)
410         {
411           gfc_error ("Name at %C is too long");
412           return MATCH_ERROR;
413         }
414
415       old_loc = gfc_current_locus;
416       c = gfc_next_char ();
417     }
418   while (ISALNUM (c)
419          || c == '_'
420          || (gfc_option.flag_dollar_ok && c == '$'));
421
422   buffer[i] = '\0';
423   gfc_current_locus = old_loc;
424
425   return MATCH_YES;
426 }
427
428
429 /* Match a symbol on the input.  Modifies the pointer to the symbol
430    pointer if successful.  */
431
432 match
433 gfc_match_sym_tree (gfc_symtree ** matched_symbol, int host_assoc)
434 {
435   char buffer[GFC_MAX_SYMBOL_LEN + 1];
436   match m;
437
438   m = gfc_match_name (buffer);
439   if (m != MATCH_YES)
440     return m;
441
442   if (host_assoc)
443     return (gfc_get_ha_sym_tree (buffer, matched_symbol))
444       ? MATCH_ERROR : MATCH_YES;
445
446   if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
447     return MATCH_ERROR;
448
449   return MATCH_YES;
450 }
451
452
453 match
454 gfc_match_symbol (gfc_symbol ** matched_symbol, int host_assoc)
455 {
456   gfc_symtree *st;
457   match m;
458
459   m = gfc_match_sym_tree (&st, host_assoc);
460
461   if (m == MATCH_YES)
462     {
463       if (st)
464         *matched_symbol = st->n.sym;
465       else
466         *matched_symbol = NULL;
467     }
468   else
469     *matched_symbol = NULL;
470   return m;
471 }
472
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.  */
476
477 match
478 gfc_match_intrinsic_op (gfc_intrinsic_op * result)
479 {
480   gfc_intrinsic_op op;
481
482   op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
483
484   if (op == INTRINSIC_NONE)
485     return MATCH_NO;
486
487   *result = op;
488   return MATCH_YES;
489 }
490
491
492 /* Match a loop control phrase:
493
494     <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
495
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.  */
499
500 match
501 gfc_match_iterator (gfc_iterator * iter, int init_flag)
502 {
503   char name[GFC_MAX_SYMBOL_LEN + 1];
504   gfc_expr *var, *e1, *e2, *e3;
505   locus start;
506   match m;
507
508   /* Match the start of an iterator without affecting the symbol
509      table.  */
510
511   start = gfc_current_locus;
512   m = gfc_match (" %n =", name);
513   gfc_current_locus = start;
514
515   if (m != MATCH_YES)
516     return MATCH_NO;
517
518   m = gfc_match_variable (&var, 0);
519   if (m != MATCH_YES)
520     return MATCH_NO;
521
522   gfc_match_char ('=');
523
524   e1 = e2 = e3 = NULL;
525
526   if (var->ref != NULL)
527     {
528       gfc_error ("Loop variable at %C cannot be a sub-component");
529       goto cleanup;
530     }
531
532   if (var->symtree->n.sym->attr.intent == INTENT_IN)
533     {
534       gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
535                  var->symtree->n.sym->name);
536       goto cleanup;
537     }
538
539   if (var->symtree->n.sym->attr.pointer)
540     {
541       gfc_error ("Loop variable at %C cannot have the POINTER attribute");
542       goto cleanup;
543     }
544
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;
550
551   if (gfc_match_char (',') != MATCH_YES)
552     goto syntax;
553
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;
559
560   if (gfc_match_char (',') != MATCH_YES)
561     {
562       e3 = gfc_int_expr (1);
563       goto done;
564     }
565
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)
570     {
571       gfc_error ("Expected a step value in iterator at %C");
572       goto cleanup;
573     }
574
575 done:
576   iter->var = var;
577   iter->start = e1;
578   iter->end = e2;
579   iter->step = e3;
580   return MATCH_YES;
581
582 syntax:
583   gfc_error ("Syntax error in iterator at %C");
584
585 cleanup:
586   gfc_free_expr (e1);
587   gfc_free_expr (e2);
588   gfc_free_expr (e3);
589
590   return MATCH_ERROR;
591 }
592
593
594 /* Tries to match the next non-whitespace character on the input.
595    This subroutine does not return MATCH_ERROR.  */
596
597 match
598 gfc_match_char (char c)
599 {
600   locus where;
601
602   where = gfc_current_locus;
603   gfc_gobble_whitespace ();
604
605   if (gfc_next_char () == c)
606     return MATCH_YES;
607
608   gfc_current_locus = where;
609   return MATCH_NO;
610 }
611
612
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:
617
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.  */
627
628 match
629 gfc_match (const char *target, ...)
630 {
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;
639
640   old_loc = gfc_current_locus;
641   va_start (argp, target);
642   m = MATCH_NO;
643   matches = 0;
644   p = target;
645
646 loop:
647   c = *p++;
648   switch (c)
649     {
650     case ' ':
651       gfc_gobble_whitespace ();
652       goto loop;
653     case '\0':
654       m = MATCH_YES;
655       break;
656
657     case '%':
658       c = *p++;
659       switch (c)
660         {
661         case 'e':
662           vp = va_arg (argp, void **);
663           n = gfc_match_expr ((gfc_expr **) vp);
664           if (n != MATCH_YES)
665             {
666               m = n;
667               goto not_yes;
668             }
669
670           matches++;
671           goto loop;
672
673         case 'v':
674           vp = va_arg (argp, void **);
675           n = gfc_match_variable ((gfc_expr **) vp, 0);
676           if (n != MATCH_YES)
677             {
678               m = n;
679               goto not_yes;
680             }
681
682           matches++;
683           goto loop;
684
685         case 's':
686           vp = va_arg (argp, void **);
687           n = gfc_match_symbol ((gfc_symbol **) vp, 0);
688           if (n != MATCH_YES)
689             {
690               m = n;
691               goto not_yes;
692             }
693
694           matches++;
695           goto loop;
696
697         case 'n':
698           np = va_arg (argp, char *);
699           n = gfc_match_name (np);
700           if (n != MATCH_YES)
701             {
702               m = n;
703               goto not_yes;
704             }
705
706           matches++;
707           goto loop;
708
709         case 'l':
710           label = va_arg (argp, gfc_st_label **);
711           n = gfc_match_st_label (label);
712           if (n != MATCH_YES)
713             {
714               m = n;
715               goto not_yes;
716             }
717
718           matches++;
719           goto loop;
720
721         case 'o':
722           ip = va_arg (argp, int *);
723           n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
724           if (n != MATCH_YES)
725             {
726               m = n;
727               goto not_yes;
728             }
729
730           matches++;
731           goto loop;
732
733         case 't':
734           if (gfc_match_eos () != MATCH_YES)
735             {
736               m = MATCH_NO;
737               goto not_yes;
738             }
739           goto loop;
740
741         case ' ':
742           if (gfc_match_space () == MATCH_YES)
743             goto loop;
744           m = MATCH_NO;
745           goto not_yes;
746
747         case '%':
748           break;        /* Fall through to character matcher */
749
750         default:
751           gfc_internal_error ("gfc_match(): Bad match code %c", c);
752         }
753
754     default:
755       if (c == gfc_next_char ())
756         goto loop;
757       break;
758     }
759
760 not_yes:
761   va_end (argp);
762
763   if (m != MATCH_YES)
764     {
765       /* Clean up after a failed match.  */
766       gfc_current_locus = old_loc;
767       va_start (argp, target);
768
769       p = target;
770       for (; matches > 0; matches--)
771         {
772           while (*p++ != '%');
773
774           switch (*p++)
775             {
776             case '%':
777               matches++;
778               break;            /* Skip */
779
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;
787
788             case 'e':
789             case 'v':
790               vp = va_arg (argp, void **);
791               gfc_free_expr (*vp);
792               *vp = NULL;
793               break;
794             }
795         }
796
797       va_end (argp);
798     }
799
800   return m;
801 }
802
803
804 /*********************** Statement level matching **********************/
805
806 /* Matches the start of a program unit, which is the program keyword
807    followed by an obligatory symbol.  */
808
809 match
810 gfc_match_program (void)
811 {
812   gfc_symbol *sym;
813   match m;
814
815   m = gfc_match ("% %s%t", &sym);
816
817   if (m == MATCH_NO)
818     {
819       gfc_error ("Invalid form of PROGRAM statement at %C");
820       m = MATCH_ERROR;
821     }
822
823   if (m == MATCH_ERROR)
824     return m;
825
826   if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
827     return MATCH_ERROR;
828
829   gfc_new_block = sym;
830
831   return MATCH_YES;
832 }
833
834
835 /* Match a simple assignment statement.  */
836
837 match
838 gfc_match_assignment (void)
839 {
840   gfc_expr *lvalue, *rvalue;
841   locus old_loc;
842   match m;
843
844   old_loc = gfc_current_locus;
845
846   lvalue = rvalue = NULL;
847   m = gfc_match (" %v =", &lvalue);
848   if (m != MATCH_YES)
849     goto cleanup;
850
851   if (lvalue->symtree->n.sym->attr.flavor == FL_PARAMETER)
852     {
853       gfc_error ("Cannot assign to a PARAMETER variable at %C");
854       m = MATCH_ERROR;
855       goto cleanup;
856     }
857
858   m = gfc_match (" %e%t", &rvalue);
859   if (m != MATCH_YES)
860     goto cleanup;
861
862   gfc_set_sym_referenced (lvalue->symtree->n.sym);
863
864   new_st.op = EXEC_ASSIGN;
865   new_st.expr = lvalue;
866   new_st.expr2 = rvalue;
867
868   gfc_check_do_variable (lvalue->symtree);
869
870   return MATCH_YES;
871
872 cleanup:
873   gfc_current_locus = old_loc;
874   gfc_free_expr (lvalue);
875   gfc_free_expr (rvalue);
876   return m;
877 }
878
879
880 /* Match a pointer assignment statement.  */
881
882 match
883 gfc_match_pointer_assignment (void)
884 {
885   gfc_expr *lvalue, *rvalue;
886   locus old_loc;
887   match m;
888
889   old_loc = gfc_current_locus;
890
891   lvalue = rvalue = NULL;
892
893   m = gfc_match (" %v =>", &lvalue);
894   if (m != MATCH_YES)
895     {
896       m = MATCH_NO;
897       goto cleanup;
898     }
899
900   m = gfc_match (" %e%t", &rvalue);
901   if (m != MATCH_YES)
902     goto cleanup;
903
904   new_st.op = EXEC_POINTER_ASSIGN;
905   new_st.expr = lvalue;
906   new_st.expr2 = rvalue;
907
908   return MATCH_YES;
909
910 cleanup:
911   gfc_current_locus = old_loc;
912   gfc_free_expr (lvalue);
913   gfc_free_expr (rvalue);
914   return m;
915 }
916
917
918 /* We try to match an easy arithmetic IF statement. This only happens
919    when just after having encountered a simple IF statement. This code
920    is really duplicate with parts of the gfc_match_if code, but this is
921    *much* easier.  */
922 static match
923 match_arithmetic_if (void)
924 {
925   gfc_st_label *l1, *l2, *l3;
926   gfc_expr *expr;
927   match m;
928
929   m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
930   if (m != MATCH_YES)
931     return m;
932
933   if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
934       || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
935       || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
936     {
937       gfc_free_expr (expr);
938       return MATCH_ERROR;
939     }
940
941   if (gfc_notify_std (GFC_STD_F95_DEL,
942                       "Obsolete: arithmetic IF statement at %C") == FAILURE)
943     return MATCH_ERROR;
944
945   new_st.op = EXEC_ARITHMETIC_IF;
946   new_st.expr = expr;
947   new_st.label = l1;
948   new_st.label2 = l2;
949   new_st.label3 = l3;
950
951   return MATCH_YES;
952 }
953
954
955 /* The IF statement is a bit of a pain.  First of all, there are three
956    forms of it, the simple IF, the IF that starts a block and the
957    arithmetic IF.
958
959    There is a problem with the simple IF and that is the fact that we
960    only have a single level of undo information on symbols.  What this
961    means is for a simple IF, we must re-match the whole IF statement
962    multiple times in order to guarantee that the symbol table ends up
963    in the proper state.  */
964
965 static match match_simple_forall (void);
966 static match match_simple_where (void);
967
968 match
969 gfc_match_if (gfc_statement * if_type)
970 {
971   gfc_expr *expr;
972   gfc_st_label *l1, *l2, *l3;
973   locus old_loc;
974   gfc_code *p;
975   match m, n;
976
977   n = gfc_match_label ();
978   if (n == MATCH_ERROR)
979     return n;
980
981   old_loc = gfc_current_locus;
982
983   m = gfc_match (" if ( %e", &expr);
984   if (m != MATCH_YES)
985     return m;
986
987   if (gfc_match_char (')') != MATCH_YES)
988     {
989       gfc_error ("Syntax error in IF-expression at %C");
990       gfc_free_expr (expr);
991       return MATCH_ERROR;
992     }
993
994   m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
995
996   if (m == MATCH_YES)
997     {
998       if (n == MATCH_YES)
999         {
1000           gfc_error
1001             ("Block label not appropriate for arithmetic IF statement "
1002              "at %C");
1003
1004           gfc_free_expr (expr);
1005           return MATCH_ERROR;
1006         }
1007
1008       if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1009           || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1010           || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1011         {
1012
1013           gfc_free_expr (expr);
1014           return MATCH_ERROR;
1015         }
1016       
1017       if (gfc_notify_std (GFC_STD_F95_DEL,
1018                           "Obsolete: arithmetic IF statement at %C")
1019           == FAILURE)
1020         return MATCH_ERROR;
1021
1022       new_st.op = EXEC_ARITHMETIC_IF;
1023       new_st.expr = expr;
1024       new_st.label = l1;
1025       new_st.label2 = l2;
1026       new_st.label3 = l3;
1027
1028       *if_type = ST_ARITHMETIC_IF;
1029       return MATCH_YES;
1030     }
1031
1032   if (gfc_match (" then%t") == MATCH_YES)
1033     {
1034       new_st.op = EXEC_IF;
1035       new_st.expr = expr;
1036
1037       *if_type = ST_IF_BLOCK;
1038       return MATCH_YES;
1039     }
1040
1041   if (n == MATCH_YES)
1042     {
1043       gfc_error ("Block label is not appropriate IF statement at %C");
1044
1045       gfc_free_expr (expr);
1046       return MATCH_ERROR;
1047     }
1048
1049   /* At this point the only thing left is a simple IF statement.  At
1050      this point, n has to be MATCH_NO, so we don't have to worry about
1051      re-matching a block label.  From what we've got so far, try
1052      matching an assignment.  */
1053
1054   *if_type = ST_SIMPLE_IF;
1055
1056   m = gfc_match_assignment ();
1057   if (m == MATCH_YES)
1058     goto got_match;
1059
1060   gfc_free_expr (expr);
1061   gfc_undo_symbols ();
1062   gfc_current_locus = old_loc;
1063
1064   /* m can be MATCH_NO or MATCH_ERROR, here.  For MATCH_NO, continue to
1065      call the various matchers.  For MATCH_ERROR, a mangled assignment
1066      was found.  */
1067   if (m == MATCH_ERROR)
1068     return MATCH_ERROR;
1069
1070   gfc_match (" if ( %e ) ", &expr);     /* Guaranteed to match */
1071
1072   m = gfc_match_pointer_assignment ();
1073   if (m == MATCH_YES)
1074     goto got_match;
1075
1076   gfc_free_expr (expr);
1077   gfc_undo_symbols ();
1078   gfc_current_locus = old_loc;
1079
1080   gfc_match (" if ( %e ) ", &expr);     /* Guaranteed to match */
1081
1082   /* Look at the next keyword to see which matcher to call.  Matching
1083      the keyword doesn't affect the symbol table, so we don't have to
1084      restore between tries.  */
1085
1086 #define match(string, subr, statement) \
1087   if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1088
1089   gfc_clear_error ();
1090
1091   match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1092     match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1093     match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1094     match ("call", gfc_match_call, ST_CALL)
1095     match ("close", gfc_match_close, ST_CLOSE)
1096     match ("continue", gfc_match_continue, ST_CONTINUE)
1097     match ("cycle", gfc_match_cycle, ST_CYCLE)
1098     match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1099     match ("end file", gfc_match_endfile, ST_END_FILE)
1100     match ("exit", gfc_match_exit, ST_EXIT)
1101     match ("flush", gfc_match_flush, ST_FLUSH)
1102     match ("forall", match_simple_forall, ST_FORALL)
1103     match ("go to", gfc_match_goto, ST_GOTO)
1104     match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1105     match ("inquire", gfc_match_inquire, ST_INQUIRE)
1106     match ("nullify", gfc_match_nullify, ST_NULLIFY)
1107     match ("open", gfc_match_open, ST_OPEN)
1108     match ("pause", gfc_match_pause, ST_NONE)
1109     match ("print", gfc_match_print, ST_WRITE)
1110     match ("read", gfc_match_read, ST_READ)
1111     match ("return", gfc_match_return, ST_RETURN)
1112     match ("rewind", gfc_match_rewind, ST_REWIND)
1113     match ("stop", gfc_match_stop, ST_STOP)
1114     match ("where", match_simple_where, ST_WHERE)
1115     match ("write", gfc_match_write, ST_WRITE)
1116
1117   /* All else has failed, so give up.  See if any of the matchers has
1118      stored an error message of some sort.  */
1119     if (gfc_error_check () == 0)
1120     gfc_error ("Unclassifiable statement in IF-clause at %C");
1121
1122   gfc_free_expr (expr);
1123   return MATCH_ERROR;
1124
1125 got_match:
1126   if (m == MATCH_NO)
1127     gfc_error ("Syntax error in IF-clause at %C");
1128   if (m != MATCH_YES)
1129     {
1130       gfc_free_expr (expr);
1131       return MATCH_ERROR;
1132     }
1133
1134   /* At this point, we've matched the single IF and the action clause
1135      is in new_st.  Rearrange things so that the IF statement appears
1136      in new_st.  */
1137
1138   p = gfc_get_code ();
1139   p->next = gfc_get_code ();
1140   *p->next = new_st;
1141   p->next->loc = gfc_current_locus;
1142
1143   p->expr = expr;
1144   p->op = EXEC_IF;
1145
1146   gfc_clear_new_st ();
1147
1148   new_st.op = EXEC_IF;
1149   new_st.block = p;
1150
1151   return MATCH_YES;
1152 }
1153
1154 #undef match
1155
1156
1157 /* Match an ELSE statement.  */
1158
1159 match
1160 gfc_match_else (void)
1161 {
1162   char name[GFC_MAX_SYMBOL_LEN + 1];
1163
1164   if (gfc_match_eos () == MATCH_YES)
1165     return MATCH_YES;
1166
1167   if (gfc_match_name (name) != MATCH_YES
1168       || gfc_current_block () == NULL
1169       || gfc_match_eos () != MATCH_YES)
1170     {
1171       gfc_error ("Unexpected junk after ELSE statement at %C");
1172       return MATCH_ERROR;
1173     }
1174
1175   if (strcmp (name, gfc_current_block ()->name) != 0)
1176     {
1177       gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1178                  name, gfc_current_block ()->name);
1179       return MATCH_ERROR;
1180     }
1181
1182   return MATCH_YES;
1183 }
1184
1185
1186 /* Match an ELSE IF statement.  */
1187
1188 match
1189 gfc_match_elseif (void)
1190 {
1191   char name[GFC_MAX_SYMBOL_LEN + 1];
1192   gfc_expr *expr;
1193   match m;
1194
1195   m = gfc_match (" ( %e ) then", &expr);
1196   if (m != MATCH_YES)
1197     return m;
1198
1199   if (gfc_match_eos () == MATCH_YES)
1200     goto done;
1201
1202   if (gfc_match_name (name) != MATCH_YES
1203       || gfc_current_block () == NULL
1204       || gfc_match_eos () != MATCH_YES)
1205     {
1206       gfc_error ("Unexpected junk after ELSE IF statement at %C");
1207       goto cleanup;
1208     }
1209
1210   if (strcmp (name, gfc_current_block ()->name) != 0)
1211     {
1212       gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1213                  name, gfc_current_block ()->name);
1214       goto cleanup;
1215     }
1216
1217 done:
1218   new_st.op = EXEC_IF;
1219   new_st.expr = expr;
1220   return MATCH_YES;
1221
1222 cleanup:
1223   gfc_free_expr (expr);
1224   return MATCH_ERROR;
1225 }
1226
1227
1228 /* Free a gfc_iterator structure.  */
1229
1230 void
1231 gfc_free_iterator (gfc_iterator * iter, int flag)
1232 {
1233
1234   if (iter == NULL)
1235     return;
1236
1237   gfc_free_expr (iter->var);
1238   gfc_free_expr (iter->start);
1239   gfc_free_expr (iter->end);
1240   gfc_free_expr (iter->step);
1241
1242   if (flag)
1243     gfc_free (iter);
1244 }
1245
1246
1247 /* Match a DO statement.  */
1248
1249 match
1250 gfc_match_do (void)
1251 {
1252   gfc_iterator iter, *ip;
1253   locus old_loc;
1254   gfc_st_label *label;
1255   match m;
1256
1257   old_loc = gfc_current_locus;
1258
1259   label = NULL;
1260   iter.var = iter.start = iter.end = iter.step = NULL;
1261
1262   m = gfc_match_label ();
1263   if (m == MATCH_ERROR)
1264     return m;
1265
1266   if (gfc_match (" do") != MATCH_YES)
1267     return MATCH_NO;
1268
1269   m = gfc_match_st_label (&label);
1270   if (m == MATCH_ERROR)
1271     goto cleanup;
1272
1273 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1274
1275   if (gfc_match_eos () == MATCH_YES)
1276     {
1277       iter.end = gfc_logical_expr (1, NULL);
1278       new_st.op = EXEC_DO_WHILE;
1279       goto done;
1280     }
1281
1282   /* match an optional comma, if no comma is found a space is obligatory.  */
1283   if (gfc_match_char(',') != MATCH_YES
1284       && gfc_match ("% ") != MATCH_YES)
1285     return MATCH_NO;
1286
1287   /* See if we have a DO WHILE.  */
1288   if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1289     {
1290       new_st.op = EXEC_DO_WHILE;
1291       goto done;
1292     }
1293
1294   /* The abortive DO WHILE may have done something to the symbol
1295      table, so we start over: */
1296   gfc_undo_symbols ();
1297   gfc_current_locus = old_loc;
1298
1299   gfc_match_label ();           /* This won't error */
1300   gfc_match (" do ");           /* This will work */
1301
1302   gfc_match_st_label (&label);  /* Can't error out */
1303   gfc_match_char (',');         /* Optional comma */
1304
1305   m = gfc_match_iterator (&iter, 0);
1306   if (m == MATCH_NO)
1307     return MATCH_NO;
1308   if (m == MATCH_ERROR)
1309     goto cleanup;
1310
1311   gfc_check_do_variable (iter.var->symtree);
1312
1313   if (gfc_match_eos () != MATCH_YES)
1314     {
1315       gfc_syntax_error (ST_DO);
1316       goto cleanup;
1317     }
1318
1319   new_st.op = EXEC_DO;
1320
1321 done:
1322   if (label != NULL
1323       && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1324     goto cleanup;
1325
1326   new_st.label = label;
1327
1328   if (new_st.op == EXEC_DO_WHILE)
1329     new_st.expr = iter.end;
1330   else
1331     {
1332       new_st.ext.iterator = ip = gfc_get_iterator ();
1333       *ip = iter;
1334     }
1335
1336   return MATCH_YES;
1337
1338 cleanup:
1339   gfc_free_iterator (&iter, 0);
1340
1341   return MATCH_ERROR;
1342 }
1343
1344
1345 /* Match an EXIT or CYCLE statement.  */
1346
1347 static match
1348 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1349 {
1350   gfc_state_data *p, *o;
1351   gfc_symbol *sym;
1352   match m;
1353
1354   if (gfc_match_eos () == MATCH_YES)
1355     sym = NULL;
1356   else
1357     {
1358       m = gfc_match ("% %s%t", &sym);
1359       if (m == MATCH_ERROR)
1360         return MATCH_ERROR;
1361       if (m == MATCH_NO)
1362         {
1363           gfc_syntax_error (st);
1364           return MATCH_ERROR;
1365         }
1366
1367       if (sym->attr.flavor != FL_LABEL)
1368         {
1369           gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1370                      sym->name, gfc_ascii_statement (st));
1371           return MATCH_ERROR;
1372         }
1373     }
1374
1375   /* Find the loop mentioned specified by the label (or lack of a
1376      label).  */
1377   for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1378     if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1379       break;
1380     else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1381       o = p;
1382
1383   if (p == NULL)
1384     {
1385       if (sym == NULL)
1386         gfc_error ("%s statement at %C is not within a loop",
1387                    gfc_ascii_statement (st));
1388       else
1389         gfc_error ("%s statement at %C is not within loop '%s'",
1390                    gfc_ascii_statement (st), sym->name);
1391
1392       return MATCH_ERROR;
1393     }
1394
1395   if (o != NULL)
1396     {
1397       gfc_error ("%s statement at %C leaving OpenMP structured block",
1398                  gfc_ascii_statement (st));
1399       return MATCH_ERROR;
1400     }
1401   else if (st == ST_EXIT
1402            && p->previous != NULL
1403            && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1404            && (p->previous->head->op == EXEC_OMP_DO
1405                || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1406     {
1407       gcc_assert (p->previous->head->next != NULL);
1408       gcc_assert (p->previous->head->next->op == EXEC_DO
1409                   || p->previous->head->next->op == EXEC_DO_WHILE);
1410       gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1411       return MATCH_ERROR;
1412     }
1413
1414   /* Save the first statement in the loop - needed by the backend.  */
1415   new_st.ext.whichloop = p->head;
1416
1417   new_st.op = op;
1418 /*  new_st.sym = sym;*/
1419
1420   return MATCH_YES;
1421 }
1422
1423
1424 /* Match the EXIT statement.  */
1425
1426 match
1427 gfc_match_exit (void)
1428 {
1429
1430   return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1431 }
1432
1433
1434 /* Match the CYCLE statement.  */
1435
1436 match
1437 gfc_match_cycle (void)
1438 {
1439
1440   return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1441 }
1442
1443
1444 /* Match a number or character constant after a STOP or PAUSE statement.  */
1445
1446 static match
1447 gfc_match_stopcode (gfc_statement st)
1448 {
1449   int stop_code;
1450   gfc_expr *e;
1451   match m;
1452   int cnt;
1453
1454   stop_code = -1;
1455   e = NULL;
1456
1457   if (gfc_match_eos () != MATCH_YES)
1458     {
1459       m = gfc_match_small_literal_int (&stop_code, &cnt);
1460       if (m == MATCH_ERROR)
1461         goto cleanup;
1462
1463       if (m == MATCH_YES && cnt > 5)
1464         {
1465           gfc_error ("Too many digits in STOP code at %C");
1466           goto cleanup;
1467         }
1468
1469       if (m == MATCH_NO)
1470         {
1471           /* Try a character constant.  */
1472           m = gfc_match_expr (&e);
1473           if (m == MATCH_ERROR)
1474             goto cleanup;
1475           if (m == MATCH_NO)
1476             goto syntax;
1477           if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1478             goto syntax;
1479         }
1480
1481       if (gfc_match_eos () != MATCH_YES)
1482         goto syntax;
1483     }
1484
1485   if (gfc_pure (NULL))
1486     {
1487       gfc_error ("%s statement not allowed in PURE procedure at %C",
1488                  gfc_ascii_statement (st));
1489       goto cleanup;
1490     }
1491
1492   new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1493   new_st.expr = e;
1494   new_st.ext.stop_code = stop_code;
1495
1496   return MATCH_YES;
1497
1498 syntax:
1499   gfc_syntax_error (st);
1500
1501 cleanup:
1502
1503   gfc_free_expr (e);
1504   return MATCH_ERROR;
1505 }
1506
1507 /* Match the (deprecated) PAUSE statement.  */
1508
1509 match
1510 gfc_match_pause (void)
1511 {
1512   match m;
1513
1514   m = gfc_match_stopcode (ST_PAUSE);
1515   if (m == MATCH_YES)
1516     {
1517       if (gfc_notify_std (GFC_STD_F95_DEL,
1518             "Obsolete: PAUSE statement at %C")
1519           == FAILURE)
1520         m = MATCH_ERROR;
1521     }
1522   return m;
1523 }
1524
1525
1526 /* Match the STOP statement.  */
1527
1528 match
1529 gfc_match_stop (void)
1530 {
1531   return gfc_match_stopcode (ST_STOP);
1532 }
1533
1534
1535 /* Match a CONTINUE statement.  */
1536
1537 match
1538 gfc_match_continue (void)
1539 {
1540
1541   if (gfc_match_eos () != MATCH_YES)
1542     {
1543       gfc_syntax_error (ST_CONTINUE);
1544       return MATCH_ERROR;
1545     }
1546
1547   new_st.op = EXEC_CONTINUE;
1548   return MATCH_YES;
1549 }
1550
1551
1552 /* Match the (deprecated) ASSIGN statement.  */
1553
1554 match
1555 gfc_match_assign (void)
1556 {
1557   gfc_expr *expr;
1558   gfc_st_label *label;
1559
1560   if (gfc_match (" %l", &label) == MATCH_YES)
1561     {
1562       if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1563         return MATCH_ERROR;
1564       if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1565         {
1566           if (gfc_notify_std (GFC_STD_F95_DEL,
1567                 "Obsolete: ASSIGN statement at %C")
1568               == FAILURE)
1569             return MATCH_ERROR;
1570
1571           expr->symtree->n.sym->attr.assign = 1;
1572
1573           new_st.op = EXEC_LABEL_ASSIGN;
1574           new_st.label = label;
1575           new_st.expr = expr;
1576           return MATCH_YES;
1577         }
1578     }
1579   return MATCH_NO;
1580 }
1581
1582
1583 /* Match the GO TO statement.  As a computed GOTO statement is
1584    matched, it is transformed into an equivalent SELECT block.  No
1585    tree is necessary, and the resulting jumps-to-jumps are
1586    specifically optimized away by the back end.  */
1587
1588 match
1589 gfc_match_goto (void)
1590 {
1591   gfc_code *head, *tail;
1592   gfc_expr *expr;
1593   gfc_case *cp;
1594   gfc_st_label *label;
1595   int i;
1596   match m;
1597
1598   if (gfc_match (" %l%t", &label) == MATCH_YES)
1599     {
1600       if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1601         return MATCH_ERROR;
1602
1603       new_st.op = EXEC_GOTO;
1604       new_st.label = label;
1605       return MATCH_YES;
1606     }
1607
1608   /* The assigned GO TO statement.  */ 
1609
1610   if (gfc_match_variable (&expr, 0) == MATCH_YES)
1611     {
1612       if (gfc_notify_std (GFC_STD_F95_DEL,
1613                           "Obsolete: Assigned GOTO statement at %C")
1614           == FAILURE)
1615         return MATCH_ERROR;
1616
1617       new_st.op = EXEC_GOTO;
1618       new_st.expr = expr;
1619
1620       if (gfc_match_eos () == MATCH_YES)
1621         return MATCH_YES;
1622
1623       /* Match label list.  */
1624       gfc_match_char (',');
1625       if (gfc_match_char ('(') != MATCH_YES)
1626         {
1627           gfc_syntax_error (ST_GOTO);
1628           return MATCH_ERROR;
1629         }
1630       head = tail = NULL;
1631
1632       do
1633         {
1634           m = gfc_match_st_label (&label);
1635           if (m != MATCH_YES)
1636             goto syntax;
1637
1638           if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1639             goto cleanup;
1640
1641           if (head == NULL)
1642             head = tail = gfc_get_code ();
1643           else
1644             {
1645               tail->block = gfc_get_code ();
1646               tail = tail->block;
1647             }
1648
1649           tail->label = label;
1650           tail->op = EXEC_GOTO;
1651         }
1652       while (gfc_match_char (',') == MATCH_YES);
1653
1654       if (gfc_match (")%t") != MATCH_YES)
1655         goto syntax;
1656
1657       if (head == NULL)
1658         {
1659            gfc_error (
1660                "Statement label list in GOTO at %C cannot be empty");
1661            goto syntax;
1662         }
1663       new_st.block = head;
1664
1665       return MATCH_YES;
1666     }
1667
1668   /* Last chance is a computed GO TO statement.  */
1669   if (gfc_match_char ('(') != MATCH_YES)
1670     {
1671       gfc_syntax_error (ST_GOTO);
1672       return MATCH_ERROR;
1673     }
1674
1675   head = tail = NULL;
1676   i = 1;
1677
1678   do
1679     {
1680       m = gfc_match_st_label (&label);
1681       if (m != MATCH_YES)
1682         goto syntax;
1683
1684       if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1685         goto cleanup;
1686
1687       if (head == NULL)
1688         head = tail = gfc_get_code ();
1689       else
1690         {
1691           tail->block = gfc_get_code ();
1692           tail = tail->block;
1693         }
1694
1695       cp = gfc_get_case ();
1696       cp->low = cp->high = gfc_int_expr (i++);
1697
1698       tail->op = EXEC_SELECT;
1699       tail->ext.case_list = cp;
1700
1701       tail->next = gfc_get_code ();
1702       tail->next->op = EXEC_GOTO;
1703       tail->next->label = label;
1704     }
1705   while (gfc_match_char (',') == MATCH_YES);
1706
1707   if (gfc_match_char (')') != MATCH_YES)
1708     goto syntax;
1709
1710   if (head == NULL)
1711     {
1712       gfc_error ("Statement label list in GOTO at %C cannot be empty");
1713       goto syntax;
1714     }
1715
1716   /* Get the rest of the statement.  */
1717   gfc_match_char (',');
1718
1719   if (gfc_match (" %e%t", &expr) != MATCH_YES)
1720     goto syntax;
1721
1722   /* At this point, a computed GOTO has been fully matched and an
1723      equivalent SELECT statement constructed.  */
1724
1725   new_st.op = EXEC_SELECT;
1726   new_st.expr = NULL;
1727
1728   /* Hack: For a "real" SELECT, the expression is in expr. We put
1729      it in expr2 so we can distinguish then and produce the correct
1730      diagnostics.  */
1731   new_st.expr2 = expr;
1732   new_st.block = head;
1733   return MATCH_YES;
1734
1735 syntax:
1736   gfc_syntax_error (ST_GOTO);
1737 cleanup:
1738   gfc_free_statements (head);
1739   return MATCH_ERROR;
1740 }
1741
1742
1743 /* Frees a list of gfc_alloc structures.  */
1744
1745 void
1746 gfc_free_alloc_list (gfc_alloc * p)
1747 {
1748   gfc_alloc *q;
1749
1750   for (; p; p = q)
1751     {
1752       q = p->next;
1753       gfc_free_expr (p->expr);
1754       gfc_free (p);
1755     }
1756 }
1757
1758
1759 /* Match an ALLOCATE statement.  */
1760
1761 match
1762 gfc_match_allocate (void)
1763 {
1764   gfc_alloc *head, *tail;
1765   gfc_expr *stat;
1766   match m;
1767
1768   head = tail = NULL;
1769   stat = NULL;
1770
1771   if (gfc_match_char ('(') != MATCH_YES)
1772     goto syntax;
1773
1774   for (;;)
1775     {
1776       if (head == NULL)
1777         head = tail = gfc_get_alloc ();
1778       else
1779         {
1780           tail->next = gfc_get_alloc ();
1781           tail = tail->next;
1782         }
1783
1784       m = gfc_match_variable (&tail->expr, 0);
1785       if (m == MATCH_NO)
1786         goto syntax;
1787       if (m == MATCH_ERROR)
1788         goto cleanup;
1789
1790       if (gfc_check_do_variable (tail->expr->symtree))
1791         goto cleanup;
1792
1793       if (gfc_pure (NULL)
1794           && gfc_impure_variable (tail->expr->symtree->n.sym))
1795         {
1796           gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1797                      "PURE procedure");
1798           goto cleanup;
1799         }
1800
1801       if (gfc_match_char (',') != MATCH_YES)
1802         break;
1803
1804       m = gfc_match (" stat = %v", &stat);
1805       if (m == MATCH_ERROR)
1806         goto cleanup;
1807       if (m == MATCH_YES)
1808         break;
1809     }
1810
1811   if (stat != NULL)
1812     {
1813       if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1814         {
1815           gfc_error
1816             ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1817              "INTENT(IN)", stat->symtree->n.sym->name);
1818           goto cleanup;
1819         }
1820
1821       if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1822         {
1823           gfc_error
1824             ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1825              "procedure");
1826           goto cleanup;
1827         }
1828
1829       if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1830         {
1831           gfc_error("STAT expression at %C must be a variable");
1832           goto cleanup;
1833         }
1834
1835       gfc_check_do_variable(stat->symtree);
1836     }
1837
1838   if (gfc_match (" )%t") != MATCH_YES)
1839     goto syntax;
1840
1841   new_st.op = EXEC_ALLOCATE;
1842   new_st.expr = stat;
1843   new_st.ext.alloc_list = head;
1844
1845   return MATCH_YES;
1846
1847 syntax:
1848   gfc_syntax_error (ST_ALLOCATE);
1849
1850 cleanup:
1851   gfc_free_expr (stat);
1852   gfc_free_alloc_list (head);
1853   return MATCH_ERROR;
1854 }
1855
1856
1857 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1858    a set of pointer assignments to intrinsic NULL().  */
1859
1860 match
1861 gfc_match_nullify (void)
1862 {
1863   gfc_code *tail;
1864   gfc_expr *e, *p;
1865   match m;
1866
1867   tail = NULL;
1868
1869   if (gfc_match_char ('(') != MATCH_YES)
1870     goto syntax;
1871
1872   for (;;)
1873     {
1874       m = gfc_match_variable (&p, 0);
1875       if (m == MATCH_ERROR)
1876         goto cleanup;
1877       if (m == MATCH_NO)
1878         goto syntax;
1879
1880       if (gfc_check_do_variable(p->symtree))
1881         goto cleanup;
1882
1883       if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1884         {
1885           gfc_error
1886             ("Illegal variable in NULLIFY at %C for a PURE procedure");
1887           goto cleanup;
1888         }
1889
1890       /* build ' => NULL() ' */
1891       e = gfc_get_expr ();
1892       e->where = gfc_current_locus;
1893       e->expr_type = EXPR_NULL;
1894       e->ts.type = BT_UNKNOWN;
1895
1896       /* Chain to list */
1897       if (tail == NULL)
1898         tail = &new_st;
1899       else
1900         {
1901           tail->next = gfc_get_code ();
1902           tail = tail->next;
1903         }
1904
1905       tail->op = EXEC_POINTER_ASSIGN;
1906       tail->expr = p;
1907       tail->expr2 = e;
1908
1909       if (gfc_match (" )%t") == MATCH_YES)
1910         break;
1911       if (gfc_match_char (',') != MATCH_YES)
1912         goto syntax;
1913     }
1914
1915   return MATCH_YES;
1916
1917 syntax:
1918   gfc_syntax_error (ST_NULLIFY);
1919
1920 cleanup:
1921   gfc_free_statements (new_st.next);
1922   return MATCH_ERROR;
1923 }
1924
1925
1926 /* Match a DEALLOCATE statement.  */
1927
1928 match
1929 gfc_match_deallocate (void)
1930 {
1931   gfc_alloc *head, *tail;
1932   gfc_expr *stat;
1933   match m;
1934
1935   head = tail = NULL;
1936   stat = NULL;
1937
1938   if (gfc_match_char ('(') != MATCH_YES)
1939     goto syntax;
1940
1941   for (;;)
1942     {
1943       if (head == NULL)
1944         head = tail = gfc_get_alloc ();
1945       else
1946         {
1947           tail->next = gfc_get_alloc ();
1948           tail = tail->next;
1949         }
1950
1951       m = gfc_match_variable (&tail->expr, 0);
1952       if (m == MATCH_ERROR)
1953         goto cleanup;
1954       if (m == MATCH_NO)
1955         goto syntax;
1956
1957       if (gfc_check_do_variable (tail->expr->symtree))
1958         goto cleanup;
1959
1960       if (gfc_pure (NULL)
1961           && gfc_impure_variable (tail->expr->symtree->n.sym))
1962         {
1963           gfc_error
1964             ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1965              "procedure");
1966           goto cleanup;
1967         }
1968
1969       if (gfc_match_char (',') != MATCH_YES)
1970         break;
1971
1972       m = gfc_match (" stat = %v", &stat);
1973       if (m == MATCH_ERROR)
1974         goto cleanup;
1975       if (m == MATCH_YES)
1976         break;
1977     }
1978
1979   if (stat != NULL)
1980     {
1981       if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1982         {
1983           gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1984                      "cannot be INTENT(IN)", stat->symtree->n.sym->name);
1985           goto cleanup;
1986         }
1987
1988       if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
1989         {
1990           gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
1991                      "for a PURE procedure");
1992           goto cleanup;
1993         }
1994
1995       if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1996         {
1997           gfc_error("STAT expression at %C must be a variable");
1998           goto cleanup;
1999         }
2000
2001       gfc_check_do_variable(stat->symtree);
2002     }
2003
2004   if (gfc_match (" )%t") != MATCH_YES)
2005     goto syntax;
2006
2007   new_st.op = EXEC_DEALLOCATE;
2008   new_st.expr = stat;
2009   new_st.ext.alloc_list = head;
2010
2011   return MATCH_YES;
2012
2013 syntax:
2014   gfc_syntax_error (ST_DEALLOCATE);
2015
2016 cleanup:
2017   gfc_free_expr (stat);
2018   gfc_free_alloc_list (head);
2019   return MATCH_ERROR;
2020 }
2021
2022
2023 /* Match a RETURN statement.  */
2024
2025 match
2026 gfc_match_return (void)
2027 {
2028   gfc_expr *e;
2029   match m;
2030   gfc_compile_state s;
2031   int c;
2032
2033   e = NULL;
2034   if (gfc_match_eos () == MATCH_YES)
2035     goto done;
2036
2037   if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2038     {
2039       gfc_error ("Alternate RETURN statement at %C is only allowed within "
2040                  "a SUBROUTINE");
2041       goto cleanup;
2042     }
2043
2044   if (gfc_current_form == FORM_FREE)
2045     {
2046       /* The following are valid, so we can't require a blank after the
2047         RETURN keyword:
2048           return+1
2049           return(1)  */
2050       c = gfc_peek_char ();
2051       if (ISALPHA (c) || ISDIGIT (c))
2052        return MATCH_NO;
2053     }
2054
2055   m = gfc_match (" %e%t", &e);
2056   if (m == MATCH_YES)
2057     goto done;
2058   if (m == MATCH_ERROR)
2059     goto cleanup;
2060
2061   gfc_syntax_error (ST_RETURN);
2062
2063 cleanup:
2064   gfc_free_expr (e);
2065   return MATCH_ERROR;
2066
2067 done:
2068   gfc_enclosing_unit (&s);
2069   if (s == COMP_PROGRAM
2070       && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2071                         "main program at %C") == FAILURE)
2072       return MATCH_ERROR;
2073
2074   new_st.op = EXEC_RETURN;
2075   new_st.expr = e;
2076
2077   return MATCH_YES;
2078 }
2079
2080
2081 /* Match a CALL statement.  The tricky part here are possible
2082    alternate return specifiers.  We handle these by having all
2083    "subroutines" actually return an integer via a register that gives
2084    the return number.  If the call specifies alternate returns, we
2085    generate code for a SELECT statement whose case clauses contain
2086    GOTOs to the various labels.  */
2087
2088 match
2089 gfc_match_call (void)
2090 {
2091   char name[GFC_MAX_SYMBOL_LEN + 1];
2092   gfc_actual_arglist *a, *arglist;
2093   gfc_case *new_case;
2094   gfc_symbol *sym;
2095   gfc_symtree *st;
2096   gfc_code *c;
2097   match m;
2098   int i;
2099
2100   arglist = NULL;
2101
2102   m = gfc_match ("% %n", name);
2103   if (m == MATCH_NO)
2104     goto syntax;
2105   if (m != MATCH_YES)
2106     return m;
2107
2108   if (gfc_get_ha_sym_tree (name, &st))
2109     return MATCH_ERROR;
2110
2111   sym = st->n.sym;
2112   gfc_set_sym_referenced (sym);
2113
2114   if (!sym->attr.generic
2115       && !sym->attr.subroutine
2116       && gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2117     return MATCH_ERROR;
2118
2119   if (gfc_match_eos () != MATCH_YES)
2120     {
2121       m = gfc_match_actual_arglist (1, &arglist);
2122       if (m == MATCH_NO)
2123         goto syntax;
2124       if (m == MATCH_ERROR)
2125         goto cleanup;
2126
2127       if (gfc_match_eos () != MATCH_YES)
2128         goto syntax;
2129     }
2130
2131   /* If any alternate return labels were found, construct a SELECT
2132      statement that will jump to the right place.  */
2133
2134   i = 0;
2135   for (a = arglist; a; a = a->next)
2136     if (a->expr == NULL)
2137         i = 1;
2138
2139   if (i)
2140     {
2141       gfc_symtree *select_st;
2142       gfc_symbol *select_sym;
2143       char name[GFC_MAX_SYMBOL_LEN + 1];
2144
2145       new_st.next = c = gfc_get_code ();
2146       c->op = EXEC_SELECT;
2147       sprintf (name, "_result_%s",sym->name);
2148       gfc_get_ha_sym_tree (name, &select_st);  /* Can't fail */
2149
2150       select_sym = select_st->n.sym;
2151       select_sym->ts.type = BT_INTEGER;
2152       select_sym->ts.kind = gfc_default_integer_kind;
2153       gfc_set_sym_referenced (select_sym);
2154       c->expr = gfc_get_expr ();
2155       c->expr->expr_type = EXPR_VARIABLE;
2156       c->expr->symtree = select_st;
2157       c->expr->ts = select_sym->ts;
2158       c->expr->where = gfc_current_locus;
2159
2160       i = 0;
2161       for (a = arglist; a; a = a->next)
2162         {
2163           if (a->expr != NULL)
2164             continue;
2165
2166           if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2167             continue;
2168
2169           i++;
2170
2171           c->block = gfc_get_code ();
2172           c = c->block;
2173           c->op = EXEC_SELECT;
2174
2175           new_case = gfc_get_case ();
2176           new_case->high = new_case->low = gfc_int_expr (i);
2177           c->ext.case_list = new_case;
2178
2179           c->next = gfc_get_code ();
2180           c->next->op = EXEC_GOTO;
2181           c->next->label = a->label;
2182         }
2183     }
2184
2185   new_st.op = EXEC_CALL;
2186   new_st.symtree = st;
2187   new_st.ext.actual = arglist;
2188
2189   return MATCH_YES;
2190
2191 syntax:
2192   gfc_syntax_error (ST_CALL);
2193
2194 cleanup:
2195   gfc_free_actual_arglist (arglist);
2196   return MATCH_ERROR;
2197 }
2198
2199
2200 /* Given a name, return a pointer to the common head structure,
2201    creating it if it does not exist. If FROM_MODULE is nonzero, we
2202    mangle the name so that it doesn't interfere with commons defined 
2203    in the using namespace.
2204    TODO: Add to global symbol tree.  */
2205
2206 gfc_common_head *
2207 gfc_get_common (const char *name, int from_module)
2208 {
2209   gfc_symtree *st;
2210   static int serial = 0;
2211   char mangled_name[GFC_MAX_SYMBOL_LEN+1];
2212
2213   if (from_module)
2214     {
2215       /* A use associated common block is only needed to correctly layout
2216          the variables it contains.  */
2217       snprintf(mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2218       st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2219     }
2220   else
2221     {
2222       st = gfc_find_symtree (gfc_current_ns->common_root, name);
2223
2224       if (st == NULL)
2225         st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2226     }
2227
2228   if (st->n.common == NULL)
2229     {
2230       st->n.common = gfc_get_common_head ();
2231       st->n.common->where = gfc_current_locus;
2232       strcpy (st->n.common->name, name);
2233     }
2234
2235   return st->n.common;
2236 }
2237
2238
2239 /* Match a common block name.  */
2240
2241 static match
2242 match_common_name (char *name)
2243 {
2244   match m;
2245
2246   if (gfc_match_char ('/') == MATCH_NO)
2247     {
2248       name[0] = '\0';
2249       return MATCH_YES;
2250     }
2251
2252   if (gfc_match_char ('/') == MATCH_YES)
2253     {
2254       name[0] = '\0';
2255       return MATCH_YES;
2256     }
2257
2258   m = gfc_match_name (name);
2259
2260   if (m == MATCH_ERROR)
2261     return MATCH_ERROR;
2262   if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2263     return MATCH_YES;
2264
2265   gfc_error ("Syntax error in common block name at %C");
2266   return MATCH_ERROR;
2267 }
2268
2269
2270 /* Match a COMMON statement.  */
2271
2272 match
2273 gfc_match_common (void)
2274 {
2275   gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2276   char name[GFC_MAX_SYMBOL_LEN+1];
2277   gfc_common_head *t;
2278   gfc_array_spec *as;
2279   gfc_equiv * e1, * e2;
2280   match m;
2281   gfc_gsymbol *gsym;
2282
2283   old_blank_common = gfc_current_ns->blank_common.head;
2284   if (old_blank_common)
2285     {
2286       while (old_blank_common->common_next)
2287         old_blank_common = old_blank_common->common_next;
2288     }
2289
2290   as = NULL;
2291
2292   for (;;)
2293     {
2294       m = match_common_name (name);
2295       if (m == MATCH_ERROR)
2296         goto cleanup;
2297
2298       gsym = gfc_get_gsymbol (name);
2299       if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2300         {
2301           gfc_error ("Symbol '%s' at %C is already an external symbol that is not COMMON",
2302                      name);
2303           goto cleanup;
2304         }
2305
2306       if (gsym->type == GSYM_UNKNOWN)
2307         {
2308           gsym->type = GSYM_COMMON;
2309           gsym->where = gfc_current_locus;
2310           gsym->defined = 1;
2311         }
2312
2313       gsym->used = 1;
2314
2315       if (name[0] == '\0')
2316         {
2317           t = &gfc_current_ns->blank_common;
2318           if (t->head == NULL)
2319             t->where = gfc_current_locus;
2320           head = &t->head;
2321         }
2322       else
2323         {
2324           t = gfc_get_common (name, 0);
2325           head = &t->head;
2326         }
2327
2328       if (*head == NULL)
2329         tail = NULL;
2330       else
2331         {
2332           tail = *head;
2333           while (tail->common_next)
2334             tail = tail->common_next;
2335         }
2336
2337       /* Grab the list of symbols.  */
2338       for (;;)
2339         {
2340           m = gfc_match_symbol (&sym, 0);
2341           if (m == MATCH_ERROR)
2342             goto cleanup;
2343           if (m == MATCH_NO)
2344             goto syntax;
2345
2346           if (sym->attr.in_common)
2347             {
2348               gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2349                          sym->name);
2350               goto cleanup;
2351             }
2352
2353           if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE) 
2354             goto cleanup;
2355
2356           if (sym->value != NULL
2357               && (name[0] == '\0' || !sym->attr.data))
2358             {
2359               if (name[0] == '\0')
2360                 gfc_error ("Previously initialized symbol '%s' in "
2361                            "blank COMMON block at %C", sym->name);
2362               else
2363                 gfc_error ("Previously initialized symbol '%s' in "
2364                            "COMMON block '%s' at %C", sym->name, name);
2365               goto cleanup;
2366             }
2367
2368           if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2369             goto cleanup;
2370
2371           /* Derived type names must have the SEQUENCE attribute.  */
2372           if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2373             {
2374               gfc_error
2375                 ("Derived type variable in COMMON at %C does not have the "
2376                  "SEQUENCE attribute");
2377               goto cleanup;
2378             }
2379
2380           if (tail != NULL)
2381             tail->common_next = sym;
2382           else
2383             *head = sym;
2384
2385           tail = sym;
2386
2387           /* Deal with an optional array specification after the
2388              symbol name.  */
2389           m = gfc_match_array_spec (&as);
2390           if (m == MATCH_ERROR)
2391             goto cleanup;
2392
2393           if (m == MATCH_YES)
2394             {
2395               if (as->type != AS_EXPLICIT)
2396                 {
2397                   gfc_error
2398                     ("Array specification for symbol '%s' in COMMON at %C "
2399                      "must be explicit", sym->name);
2400                   goto cleanup;
2401                 }
2402
2403               if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2404                 goto cleanup;
2405
2406               if (sym->attr.pointer)
2407                 {
2408                   gfc_error
2409                     ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2410                      sym->name);
2411                   goto cleanup;
2412                 }
2413
2414               sym->as = as;
2415               as = NULL;
2416
2417             }
2418
2419           sym->common_head = t;
2420
2421           /* Check to see if the symbol is already in an equivalence group.
2422              If it is, set the other members as being in common.  */
2423           if (sym->attr.in_equivalence)
2424             {
2425               for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2426                 {
2427                   for (e2 = e1; e2; e2 = e2->eq)
2428                     if (e2->expr->symtree->n.sym == sym)
2429                       goto equiv_found;
2430
2431                   continue;
2432
2433           equiv_found:
2434
2435                   for (e2 = e1; e2; e2 = e2->eq)
2436                     {
2437                       other = e2->expr->symtree->n.sym;
2438                       if (other->common_head
2439                             && other->common_head != sym->common_head)
2440                         {
2441                           gfc_error ("Symbol '%s', in COMMON block '%s' at "
2442                                      "%C is being indirectly equivalenced to "
2443                                      "another COMMON block '%s'",
2444                                      sym->name,
2445                                      sym->common_head->name,
2446                                      other->common_head->name);
2447                             goto cleanup;
2448                         }
2449                       other->attr.in_common = 1;
2450                       other->common_head = t;
2451                     }
2452                 }
2453             }
2454
2455
2456           gfc_gobble_whitespace ();
2457           if (gfc_match_eos () == MATCH_YES)
2458             goto done;
2459           if (gfc_peek_char () == '/')
2460             break;
2461           if (gfc_match_char (',') != MATCH_YES)
2462             goto syntax;
2463           gfc_gobble_whitespace ();
2464           if (gfc_peek_char () == '/')
2465             break;
2466         }
2467     }
2468
2469 done:
2470   return MATCH_YES;
2471
2472 syntax:
2473   gfc_syntax_error (ST_COMMON);
2474
2475 cleanup:
2476   if (old_blank_common)
2477     old_blank_common->common_next = NULL;
2478   else
2479     gfc_current_ns->blank_common.head = NULL;
2480   gfc_free_array_spec (as);
2481   return MATCH_ERROR;
2482 }
2483
2484
2485 /* Match a BLOCK DATA program unit.  */
2486
2487 match
2488 gfc_match_block_data (void)
2489 {
2490   char name[GFC_MAX_SYMBOL_LEN + 1];
2491   gfc_symbol *sym;
2492   match m;
2493
2494   if (gfc_match_eos () == MATCH_YES)
2495     {
2496       gfc_new_block = NULL;
2497       return MATCH_YES;
2498     }
2499
2500   m = gfc_match ("% %n%t", name);
2501   if (m != MATCH_YES)
2502     return MATCH_ERROR;
2503
2504   if (gfc_get_symbol (name, NULL, &sym))
2505     return MATCH_ERROR;
2506
2507   if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2508     return MATCH_ERROR;
2509
2510   gfc_new_block = sym;
2511
2512   return MATCH_YES;
2513 }
2514
2515
2516 /* Free a namelist structure.  */
2517
2518 void
2519 gfc_free_namelist (gfc_namelist * name)
2520 {
2521   gfc_namelist *n;
2522
2523   for (; name; name = n)
2524     {
2525       n = name->next;
2526       gfc_free (name);
2527     }
2528 }
2529
2530
2531 /* Match a NAMELIST statement.  */
2532
2533 match
2534 gfc_match_namelist (void)
2535 {
2536   gfc_symbol *group_name, *sym;
2537   gfc_namelist *nl;
2538   match m, m2;
2539
2540   m = gfc_match (" / %s /", &group_name);
2541   if (m == MATCH_NO)
2542     goto syntax;
2543   if (m == MATCH_ERROR)
2544     goto error;
2545
2546   for (;;)
2547     {
2548       if (group_name->ts.type != BT_UNKNOWN)
2549         {
2550           gfc_error
2551             ("Namelist group name '%s' at %C already has a basic type "
2552              "of %s", group_name->name, gfc_typename (&group_name->ts));
2553           return MATCH_ERROR;
2554         }
2555
2556       if (group_name->attr.flavor == FL_NAMELIST
2557             && group_name->attr.use_assoc
2558             && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2559                                "at %C already is USE associated and can"
2560                                "not be respecified.", group_name->name)
2561                  == FAILURE)
2562         return MATCH_ERROR;
2563
2564       if (group_name->attr.flavor != FL_NAMELIST
2565           && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2566                              group_name->name, NULL) == FAILURE)
2567         return MATCH_ERROR;
2568
2569       for (;;)
2570         {
2571           m = gfc_match_symbol (&sym, 1);
2572           if (m == MATCH_NO)
2573             goto syntax;
2574           if (m == MATCH_ERROR)
2575             goto error;
2576
2577           if (sym->attr.in_namelist == 0
2578               && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2579             goto error;
2580
2581           /* Use gfc_error_check here, rather than goto error, so that this
2582              these are the only errors for the next two lines.  */
2583           if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2584             {
2585               gfc_error ("Assumed size array '%s' in namelist '%s'at "
2586                          "%C is not allowed.", sym->name, group_name->name);
2587               gfc_error_check ();
2588             }
2589
2590           if (sym->as && sym->as->type == AS_ASSUMED_SHAPE
2591                 && gfc_notify_std (GFC_STD_GNU, "Assumed shape array '%s' in "
2592                                    "namelist '%s' at %C is an extension.",
2593                                    sym->name, group_name->name) == FAILURE)
2594             gfc_error_check ();
2595
2596           nl = gfc_get_namelist ();
2597           nl->sym = sym;
2598           sym->refs++;
2599
2600           if (group_name->namelist == NULL)
2601             group_name->namelist = group_name->namelist_tail = nl;
2602           else
2603             {
2604               group_name->namelist_tail->next = nl;
2605               group_name->namelist_tail = nl;
2606             }
2607
2608           if (gfc_match_eos () == MATCH_YES)
2609             goto done;
2610
2611           m = gfc_match_char (',');
2612
2613           if (gfc_match_char ('/') == MATCH_YES)
2614             {
2615               m2 = gfc_match (" %s /", &group_name);
2616               if (m2 == MATCH_YES)
2617                 break;
2618               if (m2 == MATCH_ERROR)
2619                 goto error;
2620               goto syntax;
2621             }
2622
2623           if (m != MATCH_YES)
2624             goto syntax;
2625         }
2626     }
2627
2628 done:
2629   return MATCH_YES;
2630
2631 syntax:
2632   gfc_syntax_error (ST_NAMELIST);
2633
2634 error:
2635   return MATCH_ERROR;
2636 }
2637
2638
2639 /* Match a MODULE statement.  */
2640
2641 match
2642 gfc_match_module (void)
2643 {
2644   match m;
2645
2646   m = gfc_match (" %s%t", &gfc_new_block);
2647   if (m != MATCH_YES)
2648     return m;
2649
2650   if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2651                       gfc_new_block->name, NULL) == FAILURE)
2652     return MATCH_ERROR;
2653
2654   return MATCH_YES;
2655 }
2656
2657
2658 /* Free equivalence sets and lists.  Recursively is the easiest way to
2659    do this.  */
2660
2661 void
2662 gfc_free_equiv (gfc_equiv * eq)
2663 {
2664
2665   if (eq == NULL)
2666     return;
2667
2668   gfc_free_equiv (eq->eq);
2669   gfc_free_equiv (eq->next);
2670
2671   gfc_free_expr (eq->expr);
2672   gfc_free (eq);
2673 }
2674
2675
2676 /* Match an EQUIVALENCE statement.  */
2677
2678 match
2679 gfc_match_equivalence (void)
2680 {
2681   gfc_equiv *eq, *set, *tail;
2682   gfc_ref *ref;
2683   gfc_symbol *sym;
2684   match m;
2685   gfc_common_head *common_head = NULL;
2686   bool common_flag;
2687   int cnt;
2688
2689   tail = NULL;
2690
2691   for (;;)
2692     {
2693       eq = gfc_get_equiv ();
2694       if (tail == NULL)
2695         tail = eq;
2696
2697       eq->next = gfc_current_ns->equiv;
2698       gfc_current_ns->equiv = eq;
2699
2700       if (gfc_match_char ('(') != MATCH_YES)
2701         goto syntax;
2702
2703       set = eq;
2704       common_flag = FALSE;
2705       cnt = 0;
2706
2707       for (;;)
2708         {
2709           m = gfc_match_equiv_variable (&set->expr);
2710           if (m == MATCH_ERROR)
2711             goto cleanup;
2712           if (m == MATCH_NO)
2713             goto syntax;
2714
2715           /*  count the number of objects.  */
2716           cnt++;
2717
2718           if (gfc_match_char ('%') == MATCH_YES)
2719             {
2720               gfc_error ("Derived type component %C is not a "
2721                          "permitted EQUIVALENCE member");
2722               goto cleanup;
2723             }
2724
2725           for (ref = set->expr->ref; ref; ref = ref->next)
2726             if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2727               {
2728                 gfc_error
2729                   ("Array reference in EQUIVALENCE at %C cannot be an "
2730                    "array section");
2731                 goto cleanup;
2732               }
2733
2734           sym = set->expr->symtree->n.sym;
2735
2736           if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL)
2737                 == FAILURE)
2738             goto cleanup;
2739
2740           if (sym->attr.in_common)
2741             {
2742               common_flag = TRUE;
2743               common_head = sym->common_head;
2744             }
2745
2746           if (gfc_match_char (')') == MATCH_YES)
2747             break;
2748
2749           if (gfc_match_char (',') != MATCH_YES)
2750             goto syntax;
2751
2752           set->eq = gfc_get_equiv ();
2753           set = set->eq;
2754         }
2755
2756       if (cnt < 2)
2757         {
2758           gfc_error ("EQUIVALENCE at %C requires two or more objects");
2759           goto cleanup;
2760         }
2761
2762       /* If one of the members of an equivalence is in common, then
2763          mark them all as being in common.  Before doing this, check
2764          that members of the equivalence group are not in different
2765          common blocks. */
2766       if (common_flag)
2767         for (set = eq; set; set = set->eq)
2768           {
2769             sym = set->expr->symtree->n.sym;
2770             if (sym->common_head && sym->common_head != common_head)
2771               {
2772                 gfc_error ("Attempt to indirectly overlap COMMON "
2773                            "blocks %s and %s by EQUIVALENCE at %C",
2774                            sym->common_head->name,
2775                            common_head->name);
2776                 goto cleanup;
2777               }
2778             sym->attr.in_common = 1;
2779             sym->common_head = common_head;
2780           }
2781
2782       if (gfc_match_eos () == MATCH_YES)
2783         break;
2784       if (gfc_match_char (',') != MATCH_YES)
2785         goto syntax;
2786     }
2787
2788   return MATCH_YES;
2789
2790 syntax:
2791   gfc_syntax_error (ST_EQUIVALENCE);
2792
2793 cleanup:
2794   eq = tail->next;
2795   tail->next = NULL;
2796
2797   gfc_free_equiv (gfc_current_ns->equiv);
2798   gfc_current_ns->equiv = eq;
2799
2800   return MATCH_ERROR;
2801 }
2802
2803 /* Check that a statement function is not recursive. This is done by looking
2804    for the statement function symbol(sym) by looking recursively through its
2805    expression(e).  If a reference to sym is found, true is returned.  
2806    12.5.4 requires that any variable of function that is implicitly typed
2807    shall have that type confirmed by any subsequent type declaration.  The
2808    implicit typing is conveniently done here.  */
2809
2810 static bool
2811 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
2812 {
2813   gfc_actual_arglist *arg;
2814   gfc_ref *ref;
2815   int i;
2816
2817   if (e == NULL)
2818     return false;
2819
2820   switch (e->expr_type)
2821     {
2822     case EXPR_FUNCTION:
2823       for (arg = e->value.function.actual; arg; arg = arg->next)
2824         {
2825           if (sym->name == arg->name
2826                 || recursive_stmt_fcn (arg->expr, sym))
2827             return true;
2828         }
2829
2830       if (e->symtree == NULL)
2831         return false;
2832
2833       /* Check the name before testing for nested recursion!  */
2834       if (sym->name == e->symtree->n.sym->name)
2835         return true;
2836
2837       /* Catch recursion via other statement functions.  */
2838       if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
2839             && e->symtree->n.sym->value
2840             && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
2841         return true;
2842
2843       if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
2844         gfc_set_default_type (e->symtree->n.sym, 0, NULL);
2845
2846       break;
2847
2848     case EXPR_VARIABLE:
2849       if (e->symtree && sym->name == e->symtree->n.sym->name)
2850         return true;
2851
2852       if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
2853         gfc_set_default_type (e->symtree->n.sym, 0, NULL);
2854       break;
2855
2856     case EXPR_OP:
2857       if (recursive_stmt_fcn (e->value.op.op1, sym)
2858             || recursive_stmt_fcn (e->value.op.op2, sym))
2859         return true;
2860       break;
2861
2862     default:
2863       break;
2864     }
2865
2866   /* Component references do not need to be checked.  */
2867   if (e->ref)
2868     {
2869       for (ref = e->ref; ref; ref = ref->next)
2870         {
2871           switch (ref->type)
2872             {
2873             case REF_ARRAY:
2874               for (i = 0; i < ref->u.ar.dimen; i++)
2875                 {
2876                   if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
2877                         || recursive_stmt_fcn (ref->u.ar.end[i], sym)
2878                         || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
2879                     return true;
2880                 }
2881               break;
2882
2883             case REF_SUBSTRING:
2884               if (recursive_stmt_fcn (ref->u.ss.start, sym)
2885                     || recursive_stmt_fcn (ref->u.ss.end, sym))
2886                 return true;
2887
2888               break;
2889
2890             default:
2891               break;
2892             }
2893         }
2894     }
2895   return false;
2896 }
2897
2898
2899 /* Match a statement function declaration.  It is so easy to match
2900    non-statement function statements with a MATCH_ERROR as opposed to
2901    MATCH_NO that we suppress error message in most cases.  */
2902
2903 match
2904 gfc_match_st_function (void)
2905 {
2906   gfc_error_buf old_error;
2907   gfc_symbol *sym;
2908   gfc_expr *expr;
2909   match m;
2910
2911   m = gfc_match_symbol (&sym, 0);
2912   if (m != MATCH_YES)
2913     return m;
2914
2915   gfc_push_error (&old_error);
2916
2917   if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
2918                          sym->name, NULL) == FAILURE)
2919     goto undo_error;
2920
2921   if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2922     goto undo_error;
2923
2924   m = gfc_match (" = %e%t", &expr);
2925   if (m == MATCH_NO)
2926     goto undo_error;
2927
2928   gfc_free_error (&old_error);
2929   if (m == MATCH_ERROR)
2930     return m;
2931
2932   if (recursive_stmt_fcn (expr, sym))
2933     {
2934       gfc_error ("Statement function at %L is recursive",
2935                  &expr->where);
2936       return MATCH_ERROR;
2937     }
2938
2939   sym->value = expr;
2940
2941   return MATCH_YES;
2942
2943 undo_error:
2944   gfc_pop_error (&old_error);
2945   return MATCH_NO;
2946 }
2947
2948
2949 /***************** SELECT CASE subroutines ******************/
2950
2951 /* Free a single case structure.  */
2952
2953 static void
2954 free_case (gfc_case * p)
2955 {
2956   if (p->low == p->high)
2957     p->high = NULL;
2958   gfc_free_expr (p->low);
2959   gfc_free_expr (p->high);
2960   gfc_free (p);
2961 }
2962
2963
2964 /* Free a list of case structures.  */
2965
2966 void
2967 gfc_free_case_list (gfc_case * p)
2968 {
2969   gfc_case *q;
2970
2971   for (; p; p = q)
2972     {
2973       q = p->next;
2974       free_case (p);
2975     }
2976 }
2977
2978
2979 /* Match a single case selector.  */
2980
2981 static match
2982 match_case_selector (gfc_case ** cp)
2983 {
2984   gfc_case *c;
2985   match m;
2986
2987   c = gfc_get_case ();
2988   c->where = gfc_current_locus;
2989
2990   if (gfc_match_char (':') == MATCH_YES)
2991     {
2992       m = gfc_match_init_expr (&c->high);
2993       if (m == MATCH_NO)
2994         goto need_expr;
2995       if (m == MATCH_ERROR)
2996         goto cleanup;
2997     }
2998
2999   else
3000     {
3001       m = gfc_match_init_expr (&c->low);
3002       if (m == MATCH_ERROR)
3003         goto cleanup;
3004       if (m == MATCH_NO)
3005         goto need_expr;
3006
3007       /* If we're not looking at a ':' now, make a range out of a single
3008          target.  Else get the upper bound for the case range.  */
3009       if (gfc_match_char (':') != MATCH_YES)
3010         c->high = c->low;
3011       else
3012         {
3013           m = gfc_match_init_expr (&c->high);
3014           if (m == MATCH_ERROR)
3015             goto cleanup;
3016           /* MATCH_NO is fine.  It's OK if nothing is there!  */
3017         }
3018     }
3019
3020   *cp = c;
3021   return MATCH_YES;
3022
3023 need_expr:
3024   gfc_error ("Expected initialization expression in CASE at %C");
3025
3026 cleanup:
3027   free_case (c);
3028   return MATCH_ERROR;
3029 }
3030
3031
3032 /* Match the end of a case statement.  */
3033
3034 static match
3035 match_case_eos (void)
3036 {
3037   char name[GFC_MAX_SYMBOL_LEN + 1];
3038   match m;
3039
3040   if (gfc_match_eos () == MATCH_YES)
3041     return MATCH_YES;
3042
3043   /* If the case construct doesn't have a case-construct-name, we
3044      should have matched the EOS.  */
3045   if (!gfc_current_block ())
3046     return MATCH_ERROR;
3047
3048   gfc_gobble_whitespace ();
3049
3050   m = gfc_match_name (name);
3051   if (m != MATCH_YES)
3052     return m;
3053
3054   if (strcmp (name, gfc_current_block ()->name) != 0)
3055     {
3056       gfc_error ("Expected case name of '%s' at %C",
3057                  gfc_current_block ()->name);
3058       return MATCH_ERROR;
3059     }
3060
3061   return gfc_match_eos ();
3062 }
3063
3064
3065 /* Match a SELECT statement.  */
3066
3067 match
3068 gfc_match_select (void)
3069 {
3070   gfc_expr *expr;
3071   match m;
3072
3073   m = gfc_match_label ();
3074   if (m == MATCH_ERROR)
3075     return m;
3076
3077   m = gfc_match (" select case ( %e )%t", &expr);
3078   if (m != MATCH_YES)
3079     return m;
3080
3081   new_st.op = EXEC_SELECT;
3082   new_st.expr = expr;
3083
3084   return MATCH_YES;
3085 }
3086
3087
3088 /* Match a CASE statement.  */
3089
3090 match
3091 gfc_match_case (void)
3092 {
3093   gfc_case *c, *head, *tail;
3094   match m;
3095
3096   head = tail = NULL;
3097
3098   if (gfc_current_state () != COMP_SELECT)
3099     {
3100       gfc_error ("Unexpected CASE statement at %C");
3101       return MATCH_ERROR;
3102     }
3103
3104   if (gfc_match ("% default") == MATCH_YES)
3105     {
3106       m = match_case_eos ();
3107       if (m == MATCH_NO)
3108         goto syntax;
3109       if (m == MATCH_ERROR)
3110         goto cleanup;
3111
3112       new_st.op = EXEC_SELECT;
3113       c = gfc_get_case ();
3114       c->where = gfc_current_locus;
3115       new_st.ext.case_list = c;
3116       return MATCH_YES;
3117     }
3118
3119   if (gfc_match_char ('(') != MATCH_YES)
3120     goto syntax;
3121
3122   for (;;)
3123     {
3124       if (match_case_selector (&c) == MATCH_ERROR)
3125         goto cleanup;
3126
3127       if (head == NULL)
3128         head = c;
3129       else
3130         tail->next = c;
3131
3132       tail = c;
3133
3134       if (gfc_match_char (')') == MATCH_YES)
3135         break;
3136       if (gfc_match_char (',') != MATCH_YES)
3137         goto syntax;
3138     }
3139
3140   m = match_case_eos ();
3141   if (m == MATCH_NO)
3142     goto syntax;
3143   if (m == MATCH_ERROR)
3144     goto cleanup;
3145
3146   new_st.op = EXEC_SELECT;
3147   new_st.ext.case_list = head;
3148
3149   return MATCH_YES;
3150
3151 syntax:
3152   gfc_error ("Syntax error in CASE-specification at %C");
3153
3154 cleanup:
3155   gfc_free_case_list (head);  /* new_st is cleaned up in parse.c.  */
3156   return MATCH_ERROR;
3157 }
3158
3159 /********************* WHERE subroutines ********************/
3160
3161 /* Match the rest of a simple WHERE statement that follows an IF statement.  
3162  */
3163
3164 static match
3165 match_simple_where (void)
3166 {
3167   gfc_expr *expr;
3168   gfc_code *c;
3169   match m;
3170
3171   m = gfc_match (" ( %e )", &expr);
3172   if (m != MATCH_YES)
3173     return m;
3174
3175   m = gfc_match_assignment ();
3176   if (m == MATCH_NO)
3177     goto syntax;
3178   if (m == MATCH_ERROR)
3179     goto cleanup;
3180
3181   if (gfc_match_eos () != MATCH_YES)
3182     goto syntax;
3183
3184   c = gfc_get_code ();
3185
3186   c->op = EXEC_WHERE;
3187   c->expr = expr;
3188   c->next = gfc_get_code ();
3189
3190   *c->next = new_st;
3191   gfc_clear_new_st ();
3192
3193   new_st.op = EXEC_WHERE;
3194   new_st.block = c;
3195
3196   return MATCH_YES;
3197
3198 syntax:
3199   gfc_syntax_error (ST_WHERE);
3200
3201 cleanup:
3202   gfc_free_expr (expr);
3203   return MATCH_ERROR;
3204 }
3205
3206 /* Match a WHERE statement.  */
3207
3208 match
3209 gfc_match_where (gfc_statement * st)
3210 {
3211   gfc_expr *expr;
3212   match m0, m;
3213   gfc_code *c;
3214
3215   m0 = gfc_match_label ();
3216   if (m0 == MATCH_ERROR)
3217     return m0;
3218
3219   m = gfc_match (" where ( %e )", &expr);
3220   if (m != MATCH_YES)
3221     return m;
3222
3223   if (gfc_match_eos () == MATCH_YES)
3224     {
3225       *st = ST_WHERE_BLOCK;
3226
3227       new_st.op = EXEC_WHERE;
3228       new_st.expr = expr;
3229       return MATCH_YES;
3230     }
3231
3232   m = gfc_match_assignment ();
3233   if (m == MATCH_NO)
3234     gfc_syntax_error (ST_WHERE);
3235
3236   if (m != MATCH_YES)
3237     {
3238       gfc_free_expr (expr);
3239       return MATCH_ERROR;
3240     }
3241
3242   /* We've got a simple WHERE statement.  */
3243   *st = ST_WHERE;
3244   c = gfc_get_code ();
3245
3246   c->op = EXEC_WHERE;
3247   c->expr = expr;
3248   c->next = gfc_get_code ();
3249
3250   *c->next = new_st;
3251   gfc_clear_new_st ();
3252
3253   new_st.op = EXEC_WHERE;
3254   new_st.block = c;
3255
3256   return MATCH_YES;
3257 }
3258
3259
3260 /* Match an ELSEWHERE statement.  We leave behind a WHERE node in
3261    new_st if successful.  */
3262
3263 match
3264 gfc_match_elsewhere (void)
3265 {
3266   char name[GFC_MAX_SYMBOL_LEN + 1];
3267   gfc_expr *expr;
3268   match m;
3269
3270   if (gfc_current_state () != COMP_WHERE)
3271     {
3272       gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3273       return MATCH_ERROR;
3274     }
3275
3276   expr = NULL;
3277
3278   if (gfc_match_char ('(') == MATCH_YES)
3279     {
3280       m = gfc_match_expr (&expr);
3281       if (m == MATCH_NO)
3282         goto syntax;
3283       if (m == MATCH_ERROR)
3284         return MATCH_ERROR;
3285
3286       if (gfc_match_char (')') != MATCH_YES)
3287         goto syntax;
3288     }
3289
3290   if (gfc_match_eos () != MATCH_YES)
3291     {                           /* Better be a name at this point */
3292       m = gfc_match_name (name);
3293       if (m == MATCH_NO)
3294         goto syntax;
3295       if (m == MATCH_ERROR)
3296         goto cleanup;
3297
3298       if (gfc_match_eos () != MATCH_YES)
3299         goto syntax;
3300
3301       if (strcmp (name, gfc_current_block ()->name) != 0)
3302         {
3303           gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3304                      name, gfc_current_block ()->name);
3305           goto cleanup;
3306         }
3307     }
3308
3309   new_st.op = EXEC_WHERE;
3310   new_st.expr = expr;
3311   return MATCH_YES;
3312
3313 syntax:
3314   gfc_syntax_error (ST_ELSEWHERE);
3315
3316 cleanup:
3317   gfc_free_expr (expr);
3318   return MATCH_ERROR;
3319 }
3320
3321
3322 /******************** FORALL subroutines ********************/
3323
3324 /* Free a list of FORALL iterators.  */
3325
3326 void
3327 gfc_free_forall_iterator (gfc_forall_iterator * iter)
3328 {
3329   gfc_forall_iterator *next;
3330
3331   while (iter)
3332     {
3333       next = iter->next;
3334
3335       gfc_free_expr (iter->var);
3336       gfc_free_expr (iter->start);
3337       gfc_free_expr (iter->end);
3338       gfc_free_expr (iter->stride);
3339
3340       gfc_free (iter);
3341       iter = next;
3342     }
3343 }
3344
3345
3346 /* Match an iterator as part of a FORALL statement.  The format is:
3347
3348      <var> = <start>:<end>[:<stride>][, <scalar mask>]  */
3349
3350 static match
3351 match_forall_iterator (gfc_forall_iterator ** result)
3352 {
3353   gfc_forall_iterator *iter;
3354   locus where;
3355   match m;
3356
3357   where = gfc_current_locus;
3358   iter = gfc_getmem (sizeof (gfc_forall_iterator));
3359
3360   m = gfc_match_variable (&iter->var, 0);
3361   if (m != MATCH_YES)
3362     goto cleanup;
3363
3364   if (gfc_match_char ('=') != MATCH_YES)
3365     {
3366       m = MATCH_NO;
3367       goto cleanup;
3368     }
3369
3370   m = gfc_match_expr (&iter->start);
3371   if (m != MATCH_YES)
3372     goto cleanup;
3373
3374   if (gfc_match_char (':') != MATCH_YES)
3375     goto syntax;
3376
3377   m = gfc_match_expr (&iter->end);
3378   if (m == MATCH_NO)
3379     goto syntax;
3380   if (m == MATCH_ERROR)
3381     goto cleanup;
3382
3383   if (gfc_match_char (':') == MATCH_NO)
3384     iter->stride = gfc_int_expr (1);
3385   else
3386     {
3387       m = gfc_match_expr (&iter->stride);
3388       if (m == MATCH_NO)
3389         goto syntax;
3390       if (m == MATCH_ERROR)
3391         goto cleanup;
3392     }
3393
3394   /* Mark the iteration variable's symbol as used as a FORALL index.  */
3395   iter->var->symtree->n.sym->forall_index = true;
3396
3397   *result = iter;
3398   return MATCH_YES;
3399
3400 syntax:
3401   gfc_error ("Syntax error in FORALL iterator at %C");
3402   m = MATCH_ERROR;
3403
3404 cleanup:
3405   /* Make sure that potential internal function references in the
3406      mask do not get messed up.  */
3407   if (iter->var
3408         && iter->var->expr_type == EXPR_VARIABLE
3409         && iter->var->symtree->n.sym->refs == 1)
3410     iter->var->symtree->n.sym->attr.flavor = FL_UNKNOWN;
3411
3412   gfc_current_locus = where;
3413   gfc_free_forall_iterator (iter);
3414   return m;
3415 }
3416
3417
3418 /* Match the header of a FORALL statement.  */
3419
3420 static match
3421 match_forall_header (gfc_forall_iterator ** phead, gfc_expr ** mask)
3422 {
3423   gfc_forall_iterator *head, *tail, *new;
3424   gfc_expr *msk;
3425   match m;
3426
3427   gfc_gobble_whitespace ();
3428
3429   head = tail = NULL;
3430   msk = NULL;
3431
3432   if (gfc_match_char ('(') != MATCH_YES)
3433     return MATCH_NO;
3434
3435   m = match_forall_iterator (&new);
3436   if (m == MATCH_ERROR)
3437     goto cleanup;
3438   if (m == MATCH_NO)
3439     goto syntax;
3440
3441   head = tail = new;
3442
3443   for (;;)
3444     {
3445       if (gfc_match_char (',') != MATCH_YES)
3446         break;
3447
3448       m = match_forall_iterator (&new);
3449       if (m == MATCH_ERROR)
3450         goto cleanup;
3451
3452       if (m == MATCH_YES)
3453         {
3454           tail->next = new;
3455           tail = new;
3456           continue;
3457         }
3458
3459       /* Have to have a mask expression */
3460
3461       m = gfc_match_expr (&msk);
3462       if (m == MATCH_NO)
3463         goto syntax;
3464       if (m == MATCH_ERROR)
3465         goto cleanup;
3466
3467       break;
3468     }
3469
3470   if (gfc_match_char (')') == MATCH_NO)
3471     goto syntax;
3472
3473   *phead = head;
3474   *mask = msk;
3475   return MATCH_YES;
3476
3477 syntax:
3478   gfc_syntax_error (ST_FORALL);
3479
3480 cleanup:
3481   gfc_free_expr (msk);
3482   gfc_free_forall_iterator (head);
3483
3484   return MATCH_ERROR;
3485 }
3486
3487 /* Match the rest of a simple FORALL statement that follows an IF statement. 
3488  */
3489
3490 static match
3491 match_simple_forall (void)
3492 {
3493   gfc_forall_iterator *head;
3494   gfc_expr *mask;
3495   gfc_code *c;
3496   match m;
3497
3498   mask = NULL;
3499   head = NULL;
3500   c = NULL;
3501
3502   m = match_forall_header (&head, &mask);
3503
3504   if (m == MATCH_NO)
3505     goto syntax;
3506   if (m != MATCH_YES)
3507     goto cleanup;
3508
3509   m = gfc_match_assignment ();
3510
3511   if (m == MATCH_ERROR)
3512     goto cleanup;
3513   if (m == MATCH_NO)
3514     {
3515       m = gfc_match_pointer_assignment ();
3516       if (m == MATCH_ERROR)
3517         goto cleanup;
3518       if (m == MATCH_NO)
3519         goto syntax;
3520     }
3521
3522   c = gfc_get_code ();
3523   *c = new_st;
3524   c->loc = gfc_current_locus;
3525
3526   if (gfc_match_eos () != MATCH_YES)
3527     goto syntax;
3528
3529   gfc_clear_new_st ();
3530   new_st.op = EXEC_FORALL;
3531   new_st.expr = mask;
3532   new_st.ext.forall_iterator = head;
3533   new_st.block = gfc_get_code ();
3534
3535   new_st.block->op = EXEC_FORALL;
3536   new_st.block->next = c;
3537
3538   return MATCH_YES;
3539
3540 syntax:
3541   gfc_syntax_error (ST_FORALL);
3542
3543 cleanup:
3544   gfc_free_forall_iterator (head);
3545   gfc_free_expr (mask);
3546
3547   return MATCH_ERROR;
3548 }
3549
3550
3551 /* Match a FORALL statement.  */
3552
3553 match
3554 gfc_match_forall (gfc_statement * st)
3555 {
3556   gfc_forall_iterator *head;
3557   gfc_expr *mask;
3558   gfc_code *c;
3559   match m0, m;
3560
3561   head = NULL;
3562   mask = NULL;
3563   c = NULL;
3564
3565   m0 = gfc_match_label ();
3566   if (m0 == MATCH_ERROR)
3567     return MATCH_ERROR;
3568
3569   m = gfc_match (" forall");
3570   if (m != MATCH_YES)
3571     return m;
3572
3573   m = match_forall_header (&head, &mask);
3574   if (m == MATCH_ERROR)
3575     goto cleanup;
3576   if (m == MATCH_NO)
3577     goto syntax;
3578
3579   if (gfc_match_eos () == MATCH_YES)
3580     {
3581       *st = ST_FORALL_BLOCK;
3582
3583       new_st.op = EXEC_FORALL;
3584       new_st.expr = mask;
3585       new_st.ext.forall_iterator = head;
3586
3587       return MATCH_YES;
3588     }
3589
3590   m = gfc_match_assignment ();
3591   if (m == MATCH_ERROR)
3592     goto cleanup;
3593   if (m == MATCH_NO)
3594     {
3595       m = gfc_match_pointer_assignment ();
3596       if (m == MATCH_ERROR)
3597         goto cleanup;
3598       if (m == MATCH_NO)
3599         goto syntax;
3600     }
3601
3602   c = gfc_get_code ();
3603   *c = new_st;
3604   c->loc = gfc_current_locus;
3605
3606   gfc_clear_new_st ();
3607   new_st.op = EXEC_FORALL;
3608   new_st.expr = mask;
3609   new_st.ext.forall_iterator = head;
3610   new_st.block = gfc_get_code ();
3611
3612   new_st.block->op = EXEC_FORALL;
3613   new_st.block->next = c;
3614
3615   *st = ST_FORALL;
3616   return MATCH_YES;
3617
3618 syntax:
3619   gfc_syntax_error (ST_FORALL);
3620
3621 cleanup:
3622   gfc_free_forall_iterator (head);
3623   gfc_free_expr (mask);
3624   gfc_free_statements (c);
3625   return MATCH_NO;
3626 }