OSDN Git Service

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