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