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