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