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