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