2009-08-22 Steven K. kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / match.c
blob9ba3e09b85f30526ea2a6bd5e21ee2c15eca2dd3
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
6 This file is part of GCC.
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.
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.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "parse.h"
29 int gfc_matching_procptr_assignment = 0;
30 bool gfc_matching_prefix = false;
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)
37 switch (op)
39 case INTRINSIC_UPLUS:
40 case INTRINSIC_PLUS:
41 return "+";
43 case INTRINSIC_UMINUS:
44 case INTRINSIC_MINUS:
45 return "-";
47 case INTRINSIC_POWER:
48 return "**";
49 case INTRINSIC_CONCAT:
50 return "//";
51 case INTRINSIC_TIMES:
52 return "*";
53 case INTRINSIC_DIVIDE:
54 return "/";
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.";
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.";
92 case INTRINSIC_ASSIGN:
93 return "=";
95 case INTRINSIC_PARENTHESES:
96 return "parens";
98 default:
99 break;
102 gfc_internal_error ("gfc_op2string(): Bad code");
103 /* Not reached. */
107 /******************** Generic matching subroutines ************************/
109 /* This function scans the current statement counting the opened and closed
110 parenthesis to make sure they are balanced. */
112 match
113 gfc_match_parens (void)
115 locus old_loc, where;
116 int count, instring;
117 gfc_char_t c, quote;
119 old_loc = gfc_current_locus;
120 count = 0;
121 instring = 0;
122 quote = ' ';
124 for (;;)
126 c = gfc_next_char_literal (instring);
127 if (c == '\n')
128 break;
129 if (quote == ' ' && ((c == '\'') || (c == '"')))
131 quote = c;
132 instring = 1;
133 continue;
135 if (quote != ' ' && c == quote)
137 quote = ' ';
138 instring = 0;
139 continue;
142 if (c == '(' && quote == ' ')
144 count++;
145 where = gfc_current_locus;
147 if (c == ')' && quote == ' ')
149 count--;
150 where = gfc_current_locus;
154 gfc_current_locus = old_loc;
156 if (count > 0)
158 gfc_error ("Missing ')' in statement at or before %L", &where);
159 return MATCH_ERROR;
161 if (count < 0)
163 gfc_error ("Missing '(' in statement at or before %L", &where);
164 return MATCH_ERROR;
167 return MATCH_YES;
171 /* See if the next character is a special character that has
172 escaped by a \ via the -fbackslash option. */
174 match
175 gfc_match_special_char (gfc_char_t *res)
177 int len, i;
178 gfc_char_t c, n;
179 match m;
181 m = MATCH_YES;
183 switch ((c = gfc_next_char_literal (1)))
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;
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++)
221 char buf[2] = { '\0', '\0' };
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;
228 buf[0] = (unsigned char) c;
229 n = n << 4;
230 n += strtol (buf, NULL, 16);
232 *res = n;
233 break;
235 default:
236 /* Unknown backslash codes are simply not expanded. */
237 m = MATCH_NO;
238 break;
241 return m;
245 /* In free form, match at least one space. Always matches in fixed
246 form. */
248 match
249 gfc_match_space (void)
251 locus old_loc;
252 char c;
254 if (gfc_current_form == FORM_FIXED)
255 return MATCH_YES;
257 old_loc = gfc_current_locus;
259 c = gfc_next_ascii_char ();
260 if (!gfc_is_whitespace (c))
262 gfc_current_locus = old_loc;
263 return MATCH_NO;
266 gfc_gobble_whitespace ();
268 return MATCH_YES;
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. */
276 match
277 gfc_match_eos (void)
279 locus old_loc;
280 int flag;
281 char c;
283 flag = 0;
285 for (;;)
287 old_loc = gfc_current_locus;
288 gfc_gobble_whitespace ();
290 c = gfc_next_ascii_char ();
291 switch (c)
293 case '!':
296 c = gfc_next_ascii_char ();
298 while (c != '\n');
300 /* Fall through. */
302 case '\n':
303 return MATCH_YES;
305 case ';':
306 flag = 1;
307 continue;
310 break;
313 gfc_current_locus = old_loc;
314 return (flag) ? MATCH_YES : MATCH_NO;
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. */
323 match
324 gfc_match_small_literal_int (int *value, int *cnt)
326 locus old_loc;
327 char c;
328 int i, j;
330 old_loc = gfc_current_locus;
332 *value = -1;
333 gfc_gobble_whitespace ();
334 c = gfc_next_ascii_char ();
335 if (cnt)
336 *cnt = 0;
338 if (!ISDIGIT (c))
340 gfc_current_locus = old_loc;
341 return MATCH_NO;
344 i = c - '0';
345 j = 1;
347 for (;;)
349 old_loc = gfc_current_locus;
350 c = gfc_next_ascii_char ();
352 if (!ISDIGIT (c))
353 break;
355 i = 10 * i + c - '0';
356 j++;
358 if (i > 99999999)
360 gfc_error ("Integer too large at %C");
361 return MATCH_ERROR;
365 gfc_current_locus = old_loc;
367 *value = i;
368 if (cnt)
369 *cnt = j;
370 return MATCH_YES;
374 /* Match a small, constant integer expression, like in a kind
375 statement. On MATCH_YES, 'value' is set. */
377 match
378 gfc_match_small_int (int *value)
380 gfc_expr *expr;
381 const char *p;
382 match m;
383 int i;
385 m = gfc_match_expr (&expr);
386 if (m != MATCH_YES)
387 return m;
389 p = gfc_extract_int (expr, &i);
390 gfc_free_expr (expr);
392 if (p != NULL)
394 gfc_error (p);
395 m = MATCH_ERROR;
398 *value = i;
399 return m;
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. */
411 match
412 gfc_match_small_int_expr (int *value, gfc_expr **expr)
414 const char *p;
415 match m;
416 int i;
418 m = gfc_match_expr (expr);
419 if (m != MATCH_YES)
420 return m;
422 p = gfc_extract_int (*expr, &i);
424 if (p != NULL)
426 gfc_error (p);
427 m = MATCH_ERROR;
430 *value = i;
431 return m;
435 /* Matches a statement label. Uses gfc_match_small_literal_int() to
436 do most of the work. */
438 match
439 gfc_match_st_label (gfc_st_label **label)
441 locus old_loc;
442 match m;
443 int i, cnt;
445 old_loc = gfc_current_locus;
447 m = gfc_match_small_literal_int (&i, &cnt);
448 if (m != MATCH_YES)
449 return m;
451 if (cnt > 5)
453 gfc_error ("Too many digits in statement label at %C");
454 goto cleanup;
457 if (i == 0)
459 gfc_error ("Statement label at %C is zero");
460 goto cleanup;
463 *label = gfc_get_st_label (i);
464 return MATCH_YES;
466 cleanup:
468 gfc_current_locus = old_loc;
469 return MATCH_ERROR;
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. */
478 match
479 gfc_match_label (void)
481 char name[GFC_MAX_SYMBOL_LEN + 1];
482 match m;
484 gfc_new_block = NULL;
486 m = gfc_match (" %n :", name);
487 if (m != MATCH_YES)
488 return m;
490 if (gfc_get_symbol (name, NULL, &gfc_new_block))
492 gfc_error ("Label name '%s' at %C is ambiguous", name);
493 return MATCH_ERROR;
496 if (gfc_new_block->attr.flavor == FL_LABEL)
498 gfc_error ("Duplicate construct label '%s' at %C", name);
499 return MATCH_ERROR;
502 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
503 gfc_new_block->name, NULL) == FAILURE)
504 return MATCH_ERROR;
506 return MATCH_YES;
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. */
515 match
516 gfc_match_name (char *buffer)
518 locus old_loc;
519 int i;
520 char c;
522 old_loc = gfc_current_locus;
523 gfc_gobble_whitespace ();
525 c = gfc_next_ascii_char ();
526 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
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;
534 i = 0;
538 buffer[i++] = c;
540 if (i > gfc_option.max_identifier_length)
542 gfc_error ("Name at %C is too long");
543 return MATCH_ERROR;
546 old_loc = gfc_current_locus;
547 c = gfc_next_ascii_char ();
549 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
551 if (c == '$' && !gfc_option.flag_dollar_ok)
553 gfc_error ("Invalid character '$' at %C. Use -fdollar-ok to allow it "
554 "as an extension");
555 return MATCH_ERROR;
558 buffer[i] = '\0';
559 gfc_current_locus = old_loc;
561 return MATCH_YES;
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. */
578 match
579 gfc_match_name_C (char *buffer)
581 locus old_loc;
582 int i = 0;
583 gfc_char_t c;
585 old_loc = gfc_current_locus;
586 gfc_gobble_whitespace ();
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);
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 == '\'')
597 buffer[0] = '\0';
598 gfc_current_locus = old_loc;
599 return MATCH_YES;
602 if (!ISALPHA (c) && c != '_')
604 gfc_error ("Invalid C name in NAME= specifier at %C");
605 return MATCH_ERROR;
608 /* Continue to read valid variable name characters. */
611 gcc_assert (gfc_wide_fits_in_byte (c));
613 buffer[i++] = (unsigned char) c;
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)
623 gfc_error ("Name at %C is too long");
624 return MATCH_ERROR;
627 old_loc = gfc_current_locus;
629 /* Get next char; param means we're in a string. */
630 c = gfc_next_char_literal (1);
631 } while (ISALNUM (c) || c == '_');
633 buffer[i] = '\0';
634 gfc_current_locus = old_loc;
636 /* See if we stopped because of whitespace. */
637 if (c == ' ')
639 gfc_gobble_whitespace ();
640 c = gfc_peek_ascii_char ();
641 if (c != '"' && c != '\'')
643 gfc_error ("Embedded space in NAME= specifier at %C");
644 return MATCH_ERROR;
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 != '\'')
652 gfc_error ("Invalid C name in NAME= specifier at %C");
653 return MATCH_ERROR;
656 return MATCH_YES;
660 /* Match a symbol on the input. Modifies the pointer to the symbol
661 pointer if successful. */
663 match
664 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
666 char buffer[GFC_MAX_SYMBOL_LEN + 1];
667 match m;
669 m = gfc_match_name (buffer);
670 if (m != MATCH_YES)
671 return m;
673 if (host_assoc)
674 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
675 ? MATCH_ERROR : MATCH_YES;
677 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
678 return MATCH_ERROR;
680 return MATCH_YES;
684 match
685 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
687 gfc_symtree *st;
688 match m;
690 m = gfc_match_sym_tree (&st, host_assoc);
692 if (m == MATCH_YES)
694 if (st)
695 *matched_symbol = st->n.sym;
696 else
697 *matched_symbol = NULL;
699 else
700 *matched_symbol = NULL;
701 return m;
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. */
709 match
710 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
712 locus orig_loc = gfc_current_locus;
713 char ch;
715 gfc_gobble_whitespace ();
716 ch = gfc_next_ascii_char ();
717 switch (ch)
719 case '+':
720 /* Matched "+". */
721 *result = INTRINSIC_PLUS;
722 return MATCH_YES;
724 case '-':
725 /* Matched "-". */
726 *result = INTRINSIC_MINUS;
727 return MATCH_YES;
729 case '=':
730 if (gfc_next_ascii_char () == '=')
732 /* Matched "==". */
733 *result = INTRINSIC_EQ;
734 return MATCH_YES;
736 break;
738 case '<':
739 if (gfc_peek_ascii_char () == '=')
741 /* Matched "<=". */
742 gfc_next_ascii_char ();
743 *result = INTRINSIC_LE;
744 return MATCH_YES;
746 /* Matched "<". */
747 *result = INTRINSIC_LT;
748 return MATCH_YES;
750 case '>':
751 if (gfc_peek_ascii_char () == '=')
753 /* Matched ">=". */
754 gfc_next_ascii_char ();
755 *result = INTRINSIC_GE;
756 return MATCH_YES;
758 /* Matched ">". */
759 *result = INTRINSIC_GT;
760 return MATCH_YES;
762 case '*':
763 if (gfc_peek_ascii_char () == '*')
765 /* Matched "**". */
766 gfc_next_ascii_char ();
767 *result = INTRINSIC_POWER;
768 return MATCH_YES;
770 /* Matched "*". */
771 *result = INTRINSIC_TIMES;
772 return MATCH_YES;
774 case '/':
775 ch = gfc_peek_ascii_char ();
776 if (ch == '=')
778 /* Matched "/=". */
779 gfc_next_ascii_char ();
780 *result = INTRINSIC_NE;
781 return MATCH_YES;
783 else if (ch == '/')
785 /* Matched "//". */
786 gfc_next_ascii_char ();
787 *result = INTRINSIC_CONCAT;
788 return MATCH_YES;
790 /* Matched "/". */
791 *result = INTRINSIC_DIVIDE;
792 return MATCH_YES;
794 case '.':
795 ch = gfc_next_ascii_char ();
796 switch (ch)
798 case 'a':
799 if (gfc_next_ascii_char () == 'n'
800 && gfc_next_ascii_char () == 'd'
801 && gfc_next_ascii_char () == '.')
803 /* Matched ".and.". */
804 *result = INTRINSIC_AND;
805 return MATCH_YES;
807 break;
809 case 'e':
810 if (gfc_next_ascii_char () == 'q')
812 ch = gfc_next_ascii_char ();
813 if (ch == '.')
815 /* Matched ".eq.". */
816 *result = INTRINSIC_EQ_OS;
817 return MATCH_YES;
819 else if (ch == 'v')
821 if (gfc_next_ascii_char () == '.')
823 /* Matched ".eqv.". */
824 *result = INTRINSIC_EQV;
825 return MATCH_YES;
829 break;
831 case 'g':
832 ch = gfc_next_ascii_char ();
833 if (ch == 'e')
835 if (gfc_next_ascii_char () == '.')
837 /* Matched ".ge.". */
838 *result = INTRINSIC_GE_OS;
839 return MATCH_YES;
842 else if (ch == 't')
844 if (gfc_next_ascii_char () == '.')
846 /* Matched ".gt.". */
847 *result = INTRINSIC_GT_OS;
848 return MATCH_YES;
851 break;
853 case 'l':
854 ch = gfc_next_ascii_char ();
855 if (ch == 'e')
857 if (gfc_next_ascii_char () == '.')
859 /* Matched ".le.". */
860 *result = INTRINSIC_LE_OS;
861 return MATCH_YES;
864 else if (ch == 't')
866 if (gfc_next_ascii_char () == '.')
868 /* Matched ".lt.". */
869 *result = INTRINSIC_LT_OS;
870 return MATCH_YES;
873 break;
875 case 'n':
876 ch = gfc_next_ascii_char ();
877 if (ch == 'e')
879 ch = gfc_next_ascii_char ();
880 if (ch == '.')
882 /* Matched ".ne.". */
883 *result = INTRINSIC_NE_OS;
884 return MATCH_YES;
886 else if (ch == 'q')
888 if (gfc_next_ascii_char () == 'v'
889 && gfc_next_ascii_char () == '.')
891 /* Matched ".neqv.". */
892 *result = INTRINSIC_NEQV;
893 return MATCH_YES;
897 else if (ch == 'o')
899 if (gfc_next_ascii_char () == 't'
900 && gfc_next_ascii_char () == '.')
902 /* Matched ".not.". */
903 *result = INTRINSIC_NOT;
904 return MATCH_YES;
907 break;
909 case 'o':
910 if (gfc_next_ascii_char () == 'r'
911 && gfc_next_ascii_char () == '.')
913 /* Matched ".or.". */
914 *result = INTRINSIC_OR;
915 return MATCH_YES;
917 break;
919 default:
920 break;
922 break;
924 default:
925 break;
928 gfc_current_locus = orig_loc;
929 return MATCH_NO;
933 /* Match a loop control phrase:
935 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
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. */
941 match
942 gfc_match_iterator (gfc_iterator *iter, int init_flag)
944 char name[GFC_MAX_SYMBOL_LEN + 1];
945 gfc_expr *var, *e1, *e2, *e3;
946 locus start;
947 match m;
949 /* Match the start of an iterator without affecting the symbol table. */
951 start = gfc_current_locus;
952 m = gfc_match (" %n =", name);
953 gfc_current_locus = start;
955 if (m != MATCH_YES)
956 return MATCH_NO;
958 m = gfc_match_variable (&var, 0);
959 if (m != MATCH_YES)
960 return MATCH_NO;
962 gfc_match_char ('=');
964 e1 = e2 = e3 = NULL;
966 if (var->ref != NULL)
968 gfc_error ("Loop variable at %C cannot be a sub-component");
969 goto cleanup;
972 if (var->symtree->n.sym->attr.intent == INTENT_IN)
974 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
975 var->symtree->n.sym->name);
976 goto cleanup;
979 var->symtree->n.sym->attr.implied_index = 1;
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;
987 if (gfc_match_char (',') != MATCH_YES)
988 goto syntax;
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;
996 if (gfc_match_char (',') != MATCH_YES)
998 e3 = gfc_int_expr (1);
999 goto done;
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)
1007 gfc_error ("Expected a step value in iterator at %C");
1008 goto cleanup;
1011 done:
1012 iter->var = var;
1013 iter->start = e1;
1014 iter->end = e2;
1015 iter->step = e3;
1016 return MATCH_YES;
1018 syntax:
1019 gfc_error ("Syntax error in iterator at %C");
1021 cleanup:
1022 gfc_free_expr (e1);
1023 gfc_free_expr (e2);
1024 gfc_free_expr (e3);
1026 return MATCH_ERROR;
1030 /* Tries to match the next non-whitespace character on the input.
1031 This subroutine does not return MATCH_ERROR. */
1033 match
1034 gfc_match_char (char c)
1036 locus where;
1038 where = gfc_current_locus;
1039 gfc_gobble_whitespace ();
1041 if (gfc_next_ascii_char () == c)
1042 return MATCH_YES;
1044 gfc_current_locus = where;
1045 return MATCH_NO;
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:
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. */
1064 match
1065 gfc_match (const char *target, ...)
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;
1076 old_loc = gfc_current_locus;
1077 va_start (argp, target);
1078 m = MATCH_NO;
1079 matches = 0;
1080 p = target;
1082 loop:
1083 c = *p++;
1084 switch (c)
1086 case ' ':
1087 gfc_gobble_whitespace ();
1088 goto loop;
1089 case '\0':
1090 m = MATCH_YES;
1091 break;
1093 case '%':
1094 c = *p++;
1095 switch (c)
1097 case 'e':
1098 vp = va_arg (argp, void **);
1099 n = gfc_match_expr ((gfc_expr **) vp);
1100 if (n != MATCH_YES)
1102 m = n;
1103 goto not_yes;
1106 matches++;
1107 goto loop;
1109 case 'v':
1110 vp = va_arg (argp, void **);
1111 n = gfc_match_variable ((gfc_expr **) vp, 0);
1112 if (n != MATCH_YES)
1114 m = n;
1115 goto not_yes;
1118 matches++;
1119 goto loop;
1121 case 's':
1122 vp = va_arg (argp, void **);
1123 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1124 if (n != MATCH_YES)
1126 m = n;
1127 goto not_yes;
1130 matches++;
1131 goto loop;
1133 case 'n':
1134 np = va_arg (argp, char *);
1135 n = gfc_match_name (np);
1136 if (n != MATCH_YES)
1138 m = n;
1139 goto not_yes;
1142 matches++;
1143 goto loop;
1145 case 'l':
1146 label = va_arg (argp, gfc_st_label **);
1147 n = gfc_match_st_label (label);
1148 if (n != MATCH_YES)
1150 m = n;
1151 goto not_yes;
1154 matches++;
1155 goto loop;
1157 case 'o':
1158 ip = va_arg (argp, int *);
1159 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1160 if (n != MATCH_YES)
1162 m = n;
1163 goto not_yes;
1166 matches++;
1167 goto loop;
1169 case 't':
1170 if (gfc_match_eos () != MATCH_YES)
1172 m = MATCH_NO;
1173 goto not_yes;
1175 goto loop;
1177 case ' ':
1178 if (gfc_match_space () == MATCH_YES)
1179 goto loop;
1180 m = MATCH_NO;
1181 goto not_yes;
1183 case '%':
1184 break; /* Fall through to character matcher. */
1186 default:
1187 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1190 default:
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);
1196 if (c == gfc_next_ascii_char ())
1197 goto loop;
1198 break;
1201 not_yes:
1202 va_end (argp);
1204 if (m != MATCH_YES)
1206 /* Clean up after a failed match. */
1207 gfc_current_locus = old_loc;
1208 va_start (argp, target);
1210 p = target;
1211 for (; matches > 0; matches--)
1213 while (*p++ != '%');
1215 switch (*p++)
1217 case '%':
1218 matches++;
1219 break; /* Skip. */
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;
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;
1238 va_end (argp);
1241 return m;
1245 /*********************** Statement level matching **********************/
1247 /* Matches the start of a program unit, which is the program keyword
1248 followed by an obligatory symbol. */
1250 match
1251 gfc_match_program (void)
1253 gfc_symbol *sym;
1254 match m;
1256 m = gfc_match ("% %s%t", &sym);
1258 if (m == MATCH_NO)
1260 gfc_error ("Invalid form of PROGRAM statement at %C");
1261 m = MATCH_ERROR;
1264 if (m == MATCH_ERROR)
1265 return m;
1267 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1268 return MATCH_ERROR;
1270 gfc_new_block = sym;
1272 return MATCH_YES;
1276 /* Match a simple assignment statement. */
1278 match
1279 gfc_match_assignment (void)
1281 gfc_expr *lvalue, *rvalue;
1282 locus old_loc;
1283 match m;
1285 old_loc = gfc_current_locus;
1287 lvalue = NULL;
1288 m = gfc_match (" %v =", &lvalue);
1289 if (m != MATCH_YES)
1291 gfc_current_locus = old_loc;
1292 gfc_free_expr (lvalue);
1293 return MATCH_NO;
1296 rvalue = NULL;
1297 m = gfc_match (" %e%t", &rvalue);
1298 if (m != MATCH_YES)
1300 gfc_current_locus = old_loc;
1301 gfc_free_expr (lvalue);
1302 gfc_free_expr (rvalue);
1303 return m;
1306 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1308 new_st.op = EXEC_ASSIGN;
1309 new_st.expr1 = lvalue;
1310 new_st.expr2 = rvalue;
1312 gfc_check_do_variable (lvalue->symtree);
1314 return MATCH_YES;
1318 /* Match a pointer assignment statement. */
1320 match
1321 gfc_match_pointer_assignment (void)
1323 gfc_expr *lvalue, *rvalue;
1324 locus old_loc;
1325 match m;
1327 old_loc = gfc_current_locus;
1329 lvalue = rvalue = NULL;
1330 gfc_matching_procptr_assignment = 0;
1332 m = gfc_match (" %v =>", &lvalue);
1333 if (m != MATCH_YES)
1335 m = MATCH_NO;
1336 goto cleanup;
1339 if (lvalue->symtree->n.sym->attr.proc_pointer
1340 || gfc_is_proc_ptr_comp (lvalue, NULL))
1341 gfc_matching_procptr_assignment = 1;
1343 m = gfc_match (" %e%t", &rvalue);
1344 gfc_matching_procptr_assignment = 0;
1345 if (m != MATCH_YES)
1346 goto cleanup;
1348 new_st.op = EXEC_POINTER_ASSIGN;
1349 new_st.expr1 = lvalue;
1350 new_st.expr2 = rvalue;
1352 return MATCH_YES;
1354 cleanup:
1355 gfc_current_locus = old_loc;
1356 gfc_free_expr (lvalue);
1357 gfc_free_expr (rvalue);
1358 return m;
1362 /* We try to match an easy arithmetic IF statement. This only happens
1363 when just after having encountered a simple IF statement. This code
1364 is really duplicate with parts of the gfc_match_if code, but this is
1365 *much* easier. */
1367 static match
1368 match_arithmetic_if (void)
1370 gfc_st_label *l1, *l2, *l3;
1371 gfc_expr *expr;
1372 match m;
1374 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1375 if (m != MATCH_YES)
1376 return m;
1378 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1379 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1380 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1382 gfc_free_expr (expr);
1383 return MATCH_ERROR;
1386 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Arithmetic IF "
1387 "statement at %C") == FAILURE)
1388 return MATCH_ERROR;
1390 new_st.op = EXEC_ARITHMETIC_IF;
1391 new_st.expr1 = expr;
1392 new_st.label1 = l1;
1393 new_st.label2 = l2;
1394 new_st.label3 = l3;
1396 return MATCH_YES;
1400 /* The IF statement is a bit of a pain. First of all, there are three
1401 forms of it, the simple IF, the IF that starts a block and the
1402 arithmetic IF.
1404 There is a problem with the simple IF and that is the fact that we
1405 only have a single level of undo information on symbols. What this
1406 means is for a simple IF, we must re-match the whole IF statement
1407 multiple times in order to guarantee that the symbol table ends up
1408 in the proper state. */
1410 static match match_simple_forall (void);
1411 static match match_simple_where (void);
1413 match
1414 gfc_match_if (gfc_statement *if_type)
1416 gfc_expr *expr;
1417 gfc_st_label *l1, *l2, *l3;
1418 locus old_loc, old_loc2;
1419 gfc_code *p;
1420 match m, n;
1422 n = gfc_match_label ();
1423 if (n == MATCH_ERROR)
1424 return n;
1426 old_loc = gfc_current_locus;
1428 m = gfc_match (" if ( %e", &expr);
1429 if (m != MATCH_YES)
1430 return m;
1432 old_loc2 = gfc_current_locus;
1433 gfc_current_locus = old_loc;
1435 if (gfc_match_parens () == MATCH_ERROR)
1436 return MATCH_ERROR;
1438 gfc_current_locus = old_loc2;
1440 if (gfc_match_char (')') != MATCH_YES)
1442 gfc_error ("Syntax error in IF-expression at %C");
1443 gfc_free_expr (expr);
1444 return MATCH_ERROR;
1447 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1449 if (m == MATCH_YES)
1451 if (n == MATCH_YES)
1453 gfc_error ("Block label not appropriate for arithmetic IF "
1454 "statement at %C");
1455 gfc_free_expr (expr);
1456 return MATCH_ERROR;
1459 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1460 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1461 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1463 gfc_free_expr (expr);
1464 return MATCH_ERROR;
1467 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Arithmetic IF "
1468 "statement at %C") == FAILURE)
1469 return MATCH_ERROR;
1471 new_st.op = EXEC_ARITHMETIC_IF;
1472 new_st.expr1 = expr;
1473 new_st.label1 = l1;
1474 new_st.label2 = l2;
1475 new_st.label3 = l3;
1477 *if_type = ST_ARITHMETIC_IF;
1478 return MATCH_YES;
1481 if (gfc_match (" then%t") == MATCH_YES)
1483 new_st.op = EXEC_IF;
1484 new_st.expr1 = expr;
1485 *if_type = ST_IF_BLOCK;
1486 return MATCH_YES;
1489 if (n == MATCH_YES)
1491 gfc_error ("Block label is not appropriate for IF statement at %C");
1492 gfc_free_expr (expr);
1493 return MATCH_ERROR;
1496 /* At this point the only thing left is a simple IF statement. At
1497 this point, n has to be MATCH_NO, so we don't have to worry about
1498 re-matching a block label. From what we've got so far, try
1499 matching an assignment. */
1501 *if_type = ST_SIMPLE_IF;
1503 m = gfc_match_assignment ();
1504 if (m == MATCH_YES)
1505 goto got_match;
1507 gfc_free_expr (expr);
1508 gfc_undo_symbols ();
1509 gfc_current_locus = old_loc;
1511 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1512 assignment was found. For MATCH_NO, continue to call the various
1513 matchers. */
1514 if (m == MATCH_ERROR)
1515 return MATCH_ERROR;
1517 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1519 m = gfc_match_pointer_assignment ();
1520 if (m == MATCH_YES)
1521 goto got_match;
1523 gfc_free_expr (expr);
1524 gfc_undo_symbols ();
1525 gfc_current_locus = old_loc;
1527 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1529 /* Look at the next keyword to see which matcher to call. Matching
1530 the keyword doesn't affect the symbol table, so we don't have to
1531 restore between tries. */
1533 #define match(string, subr, statement) \
1534 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1536 gfc_clear_error ();
1538 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1539 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1540 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1541 match ("call", gfc_match_call, ST_CALL)
1542 match ("close", gfc_match_close, ST_CLOSE)
1543 match ("continue", gfc_match_continue, ST_CONTINUE)
1544 match ("cycle", gfc_match_cycle, ST_CYCLE)
1545 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1546 match ("end file", gfc_match_endfile, ST_END_FILE)
1547 match ("exit", gfc_match_exit, ST_EXIT)
1548 match ("flush", gfc_match_flush, ST_FLUSH)
1549 match ("forall", match_simple_forall, ST_FORALL)
1550 match ("go to", gfc_match_goto, ST_GOTO)
1551 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1552 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1553 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1554 match ("open", gfc_match_open, ST_OPEN)
1555 match ("pause", gfc_match_pause, ST_NONE)
1556 match ("print", gfc_match_print, ST_WRITE)
1557 match ("read", gfc_match_read, ST_READ)
1558 match ("return", gfc_match_return, ST_RETURN)
1559 match ("rewind", gfc_match_rewind, ST_REWIND)
1560 match ("stop", gfc_match_stop, ST_STOP)
1561 match ("wait", gfc_match_wait, ST_WAIT)
1562 match ("where", match_simple_where, ST_WHERE)
1563 match ("write", gfc_match_write, ST_WRITE)
1565 /* The gfc_match_assignment() above may have returned a MATCH_NO
1566 where the assignment was to a named constant. Check that
1567 special case here. */
1568 m = gfc_match_assignment ();
1569 if (m == MATCH_NO)
1571 gfc_error ("Cannot assign to a named constant at %C");
1572 gfc_free_expr (expr);
1573 gfc_undo_symbols ();
1574 gfc_current_locus = old_loc;
1575 return MATCH_ERROR;
1578 /* All else has failed, so give up. See if any of the matchers has
1579 stored an error message of some sort. */
1580 if (gfc_error_check () == 0)
1581 gfc_error ("Unclassifiable statement in IF-clause at %C");
1583 gfc_free_expr (expr);
1584 return MATCH_ERROR;
1586 got_match:
1587 if (m == MATCH_NO)
1588 gfc_error ("Syntax error in IF-clause at %C");
1589 if (m != MATCH_YES)
1591 gfc_free_expr (expr);
1592 return MATCH_ERROR;
1595 /* At this point, we've matched the single IF and the action clause
1596 is in new_st. Rearrange things so that the IF statement appears
1597 in new_st. */
1599 p = gfc_get_code ();
1600 p->next = gfc_get_code ();
1601 *p->next = new_st;
1602 p->next->loc = gfc_current_locus;
1604 p->expr1 = expr;
1605 p->op = EXEC_IF;
1607 gfc_clear_new_st ();
1609 new_st.op = EXEC_IF;
1610 new_st.block = p;
1612 return MATCH_YES;
1615 #undef match
1618 /* Match an ELSE statement. */
1620 match
1621 gfc_match_else (void)
1623 char name[GFC_MAX_SYMBOL_LEN + 1];
1625 if (gfc_match_eos () == MATCH_YES)
1626 return MATCH_YES;
1628 if (gfc_match_name (name) != MATCH_YES
1629 || gfc_current_block () == NULL
1630 || gfc_match_eos () != MATCH_YES)
1632 gfc_error ("Unexpected junk after ELSE statement at %C");
1633 return MATCH_ERROR;
1636 if (strcmp (name, gfc_current_block ()->name) != 0)
1638 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1639 name, gfc_current_block ()->name);
1640 return MATCH_ERROR;
1643 return MATCH_YES;
1647 /* Match an ELSE IF statement. */
1649 match
1650 gfc_match_elseif (void)
1652 char name[GFC_MAX_SYMBOL_LEN + 1];
1653 gfc_expr *expr;
1654 match m;
1656 m = gfc_match (" ( %e ) then", &expr);
1657 if (m != MATCH_YES)
1658 return m;
1660 if (gfc_match_eos () == MATCH_YES)
1661 goto done;
1663 if (gfc_match_name (name) != MATCH_YES
1664 || gfc_current_block () == NULL
1665 || gfc_match_eos () != MATCH_YES)
1667 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1668 goto cleanup;
1671 if (strcmp (name, gfc_current_block ()->name) != 0)
1673 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1674 name, gfc_current_block ()->name);
1675 goto cleanup;
1678 done:
1679 new_st.op = EXEC_IF;
1680 new_st.expr1 = expr;
1681 return MATCH_YES;
1683 cleanup:
1684 gfc_free_expr (expr);
1685 return MATCH_ERROR;
1689 /* Free a gfc_iterator structure. */
1691 void
1692 gfc_free_iterator (gfc_iterator *iter, int flag)
1695 if (iter == NULL)
1696 return;
1698 gfc_free_expr (iter->var);
1699 gfc_free_expr (iter->start);
1700 gfc_free_expr (iter->end);
1701 gfc_free_expr (iter->step);
1703 if (flag)
1704 gfc_free (iter);
1708 /* Match a DO statement. */
1710 match
1711 gfc_match_do (void)
1713 gfc_iterator iter, *ip;
1714 locus old_loc;
1715 gfc_st_label *label;
1716 match m;
1718 old_loc = gfc_current_locus;
1720 label = NULL;
1721 iter.var = iter.start = iter.end = iter.step = NULL;
1723 m = gfc_match_label ();
1724 if (m == MATCH_ERROR)
1725 return m;
1727 if (gfc_match (" do") != MATCH_YES)
1728 return MATCH_NO;
1730 m = gfc_match_st_label (&label);
1731 if (m == MATCH_ERROR)
1732 goto cleanup;
1734 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1736 if (gfc_match_eos () == MATCH_YES)
1738 iter.end = gfc_logical_expr (1, NULL);
1739 new_st.op = EXEC_DO_WHILE;
1740 goto done;
1743 /* Match an optional comma, if no comma is found, a space is obligatory. */
1744 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1745 return MATCH_NO;
1747 /* Check for balanced parens. */
1749 if (gfc_match_parens () == MATCH_ERROR)
1750 return MATCH_ERROR;
1752 /* See if we have a DO WHILE. */
1753 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1755 new_st.op = EXEC_DO_WHILE;
1756 goto done;
1759 /* The abortive DO WHILE may have done something to the symbol
1760 table, so we start over. */
1761 gfc_undo_symbols ();
1762 gfc_current_locus = old_loc;
1764 gfc_match_label (); /* This won't error. */
1765 gfc_match (" do "); /* This will work. */
1767 gfc_match_st_label (&label); /* Can't error out. */
1768 gfc_match_char (','); /* Optional comma. */
1770 m = gfc_match_iterator (&iter, 0);
1771 if (m == MATCH_NO)
1772 return MATCH_NO;
1773 if (m == MATCH_ERROR)
1774 goto cleanup;
1776 iter.var->symtree->n.sym->attr.implied_index = 0;
1777 gfc_check_do_variable (iter.var->symtree);
1779 if (gfc_match_eos () != MATCH_YES)
1781 gfc_syntax_error (ST_DO);
1782 goto cleanup;
1785 new_st.op = EXEC_DO;
1787 done:
1788 if (label != NULL
1789 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1790 goto cleanup;
1792 new_st.label1 = label;
1794 if (new_st.op == EXEC_DO_WHILE)
1795 new_st.expr1 = iter.end;
1796 else
1798 new_st.ext.iterator = ip = gfc_get_iterator ();
1799 *ip = iter;
1802 return MATCH_YES;
1804 cleanup:
1805 gfc_free_iterator (&iter, 0);
1807 return MATCH_ERROR;
1811 /* Match an EXIT or CYCLE statement. */
1813 static match
1814 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1816 gfc_state_data *p, *o;
1817 gfc_symbol *sym;
1818 match m;
1820 if (gfc_match_eos () == MATCH_YES)
1821 sym = NULL;
1822 else
1824 m = gfc_match ("% %s%t", &sym);
1825 if (m == MATCH_ERROR)
1826 return MATCH_ERROR;
1827 if (m == MATCH_NO)
1829 gfc_syntax_error (st);
1830 return MATCH_ERROR;
1833 if (sym->attr.flavor != FL_LABEL)
1835 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1836 sym->name, gfc_ascii_statement (st));
1837 return MATCH_ERROR;
1841 /* Find the loop mentioned specified by the label (or lack of a label). */
1842 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1843 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1844 break;
1845 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1846 o = p;
1848 if (p == NULL)
1850 if (sym == NULL)
1851 gfc_error ("%s statement at %C is not within a loop",
1852 gfc_ascii_statement (st));
1853 else
1854 gfc_error ("%s statement at %C is not within loop '%s'",
1855 gfc_ascii_statement (st), sym->name);
1857 return MATCH_ERROR;
1860 if (o != NULL)
1862 gfc_error ("%s statement at %C leaving OpenMP structured block",
1863 gfc_ascii_statement (st));
1864 return MATCH_ERROR;
1866 else if (st == ST_EXIT
1867 && p->previous != NULL
1868 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1869 && (p->previous->head->op == EXEC_OMP_DO
1870 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1872 gcc_assert (p->previous->head->next != NULL);
1873 gcc_assert (p->previous->head->next->op == EXEC_DO
1874 || p->previous->head->next->op == EXEC_DO_WHILE);
1875 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1876 return MATCH_ERROR;
1879 /* Save the first statement in the loop - needed by the backend. */
1880 new_st.ext.whichloop = p->head;
1882 new_st.op = op;
1884 return MATCH_YES;
1888 /* Match the EXIT statement. */
1890 match
1891 gfc_match_exit (void)
1893 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1897 /* Match the CYCLE statement. */
1899 match
1900 gfc_match_cycle (void)
1902 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1906 /* Match a number or character constant after a STOP or PAUSE statement. */
1908 static match
1909 gfc_match_stopcode (gfc_statement st)
1911 int stop_code;
1912 gfc_expr *e;
1913 match m;
1914 int cnt;
1916 stop_code = -1;
1917 e = NULL;
1919 if (gfc_match_eos () != MATCH_YES)
1921 m = gfc_match_small_literal_int (&stop_code, &cnt);
1922 if (m == MATCH_ERROR)
1923 goto cleanup;
1925 if (m == MATCH_YES && cnt > 5)
1927 gfc_error ("Too many digits in STOP code at %C");
1928 goto cleanup;
1931 if (m == MATCH_NO)
1933 /* Try a character constant. */
1934 m = gfc_match_expr (&e);
1935 if (m == MATCH_ERROR)
1936 goto cleanup;
1937 if (m == MATCH_NO)
1938 goto syntax;
1939 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1940 goto syntax;
1943 if (gfc_match_eos () != MATCH_YES)
1944 goto syntax;
1947 if (gfc_pure (NULL))
1949 gfc_error ("%s statement not allowed in PURE procedure at %C",
1950 gfc_ascii_statement (st));
1951 goto cleanup;
1954 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1955 new_st.expr1 = e;
1956 new_st.ext.stop_code = stop_code;
1958 return MATCH_YES;
1960 syntax:
1961 gfc_syntax_error (st);
1963 cleanup:
1965 gfc_free_expr (e);
1966 return MATCH_ERROR;
1970 /* Match the (deprecated) PAUSE statement. */
1972 match
1973 gfc_match_pause (void)
1975 match m;
1977 m = gfc_match_stopcode (ST_PAUSE);
1978 if (m == MATCH_YES)
1980 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
1981 " at %C")
1982 == FAILURE)
1983 m = MATCH_ERROR;
1985 return m;
1989 /* Match the STOP statement. */
1991 match
1992 gfc_match_stop (void)
1994 return gfc_match_stopcode (ST_STOP);
1998 /* Match a CONTINUE statement. */
2000 match
2001 gfc_match_continue (void)
2003 if (gfc_match_eos () != MATCH_YES)
2005 gfc_syntax_error (ST_CONTINUE);
2006 return MATCH_ERROR;
2009 new_st.op = EXEC_CONTINUE;
2010 return MATCH_YES;
2014 /* Match the (deprecated) ASSIGN statement. */
2016 match
2017 gfc_match_assign (void)
2019 gfc_expr *expr;
2020 gfc_st_label *label;
2022 if (gfc_match (" %l", &label) == MATCH_YES)
2024 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
2025 return MATCH_ERROR;
2026 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
2028 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
2029 "statement at %C")
2030 == FAILURE)
2031 return MATCH_ERROR;
2033 expr->symtree->n.sym->attr.assign = 1;
2035 new_st.op = EXEC_LABEL_ASSIGN;
2036 new_st.label1 = label;
2037 new_st.expr1 = expr;
2038 return MATCH_YES;
2041 return MATCH_NO;
2045 /* Match the GO TO statement. As a computed GOTO statement is
2046 matched, it is transformed into an equivalent SELECT block. No
2047 tree is necessary, and the resulting jumps-to-jumps are
2048 specifically optimized away by the back end. */
2050 match
2051 gfc_match_goto (void)
2053 gfc_code *head, *tail;
2054 gfc_expr *expr;
2055 gfc_case *cp;
2056 gfc_st_label *label;
2057 int i;
2058 match m;
2060 if (gfc_match (" %l%t", &label) == MATCH_YES)
2062 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2063 return MATCH_ERROR;
2065 new_st.op = EXEC_GOTO;
2066 new_st.label1 = label;
2067 return MATCH_YES;
2070 /* The assigned GO TO statement. */
2072 if (gfc_match_variable (&expr, 0) == MATCH_YES)
2074 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
2075 "statement at %C")
2076 == FAILURE)
2077 return MATCH_ERROR;
2079 new_st.op = EXEC_GOTO;
2080 new_st.expr1 = expr;
2082 if (gfc_match_eos () == MATCH_YES)
2083 return MATCH_YES;
2085 /* Match label list. */
2086 gfc_match_char (',');
2087 if (gfc_match_char ('(') != MATCH_YES)
2089 gfc_syntax_error (ST_GOTO);
2090 return MATCH_ERROR;
2092 head = tail = NULL;
2096 m = gfc_match_st_label (&label);
2097 if (m != MATCH_YES)
2098 goto syntax;
2100 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2101 goto cleanup;
2103 if (head == NULL)
2104 head = tail = gfc_get_code ();
2105 else
2107 tail->block = gfc_get_code ();
2108 tail = tail->block;
2111 tail->label1 = label;
2112 tail->op = EXEC_GOTO;
2114 while (gfc_match_char (',') == MATCH_YES);
2116 if (gfc_match (")%t") != MATCH_YES)
2117 goto syntax;
2119 if (head == NULL)
2121 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2122 goto syntax;
2124 new_st.block = head;
2126 return MATCH_YES;
2129 /* Last chance is a computed GO TO statement. */
2130 if (gfc_match_char ('(') != MATCH_YES)
2132 gfc_syntax_error (ST_GOTO);
2133 return MATCH_ERROR;
2136 head = tail = NULL;
2137 i = 1;
2141 m = gfc_match_st_label (&label);
2142 if (m != MATCH_YES)
2143 goto syntax;
2145 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2146 goto cleanup;
2148 if (head == NULL)
2149 head = tail = gfc_get_code ();
2150 else
2152 tail->block = gfc_get_code ();
2153 tail = tail->block;
2156 cp = gfc_get_case ();
2157 cp->low = cp->high = gfc_int_expr (i++);
2159 tail->op = EXEC_SELECT;
2160 tail->ext.case_list = cp;
2162 tail->next = gfc_get_code ();
2163 tail->next->op = EXEC_GOTO;
2164 tail->next->label1 = label;
2166 while (gfc_match_char (',') == MATCH_YES);
2168 if (gfc_match_char (')') != MATCH_YES)
2169 goto syntax;
2171 if (head == NULL)
2173 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2174 goto syntax;
2177 /* Get the rest of the statement. */
2178 gfc_match_char (',');
2180 if (gfc_match (" %e%t", &expr) != MATCH_YES)
2181 goto syntax;
2183 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Computed GOTO "
2184 "at %C") == FAILURE)
2185 return MATCH_ERROR;
2187 /* At this point, a computed GOTO has been fully matched and an
2188 equivalent SELECT statement constructed. */
2190 new_st.op = EXEC_SELECT;
2191 new_st.expr1 = NULL;
2193 /* Hack: For a "real" SELECT, the expression is in expr. We put
2194 it in expr2 so we can distinguish then and produce the correct
2195 diagnostics. */
2196 new_st.expr2 = expr;
2197 new_st.block = head;
2198 return MATCH_YES;
2200 syntax:
2201 gfc_syntax_error (ST_GOTO);
2202 cleanup:
2203 gfc_free_statements (head);
2204 return MATCH_ERROR;
2208 /* Frees a list of gfc_alloc structures. */
2210 void
2211 gfc_free_alloc_list (gfc_alloc *p)
2213 gfc_alloc *q;
2215 for (; p; p = q)
2217 q = p->next;
2218 gfc_free_expr (p->expr);
2219 gfc_free (p);
2224 /* Match a Fortran 2003 intrinsic-type-spec. This is a stripped
2225 down version of gfc_match_type_spec() from decl.c. It only includes
2226 the intrinsic types from the Fortran 2003 standard. Thus, neither
2227 BYTE nor forms like REAL*4 are allowed. Additionally, the implicit_flag
2228 is not needed, so it was removed. The handling of derived types has
2229 been removed and no notion of the gfc_matching_function state
2230 is needed. In short, this functions matches only standard conforming
2231 intrinsic-type-spec (R403). */
2233 static match
2234 match_intrinsic_typespec (gfc_typespec *ts)
2236 match m;
2238 gfc_clear_ts (ts);
2240 if (gfc_match ("integer") == MATCH_YES)
2242 ts->type = BT_INTEGER;
2243 ts->kind = gfc_default_integer_kind;
2244 goto kind_selector;
2247 if (gfc_match ("real") == MATCH_YES)
2249 ts->type = BT_REAL;
2250 ts->kind = gfc_default_real_kind;
2251 goto kind_selector;
2254 if (gfc_match ("double precision") == MATCH_YES)
2256 ts->type = BT_REAL;
2257 ts->kind = gfc_default_double_kind;
2258 return MATCH_YES;
2261 if (gfc_match ("complex") == MATCH_YES)
2263 ts->type = BT_COMPLEX;
2264 ts->kind = gfc_default_complex_kind;
2265 goto kind_selector;
2268 if (gfc_match ("character") == MATCH_YES)
2270 ts->type = BT_CHARACTER;
2271 goto char_selector;
2274 if (gfc_match ("logical") == MATCH_YES)
2276 ts->type = BT_LOGICAL;
2277 ts->kind = gfc_default_logical_kind;
2278 goto kind_selector;
2281 /* If an intrinsic type is not matched, simply return MATCH_NO. */
2282 return MATCH_NO;
2284 kind_selector:
2286 gfc_gobble_whitespace ();
2287 if (gfc_peek_ascii_char () == '*')
2289 gfc_error ("Invalid type-spec at %C");
2290 return MATCH_ERROR;
2293 m = gfc_match_kind_spec (ts, false);
2295 if (m == MATCH_NO)
2296 m = MATCH_YES; /* No kind specifier found. */
2298 return m;
2300 char_selector:
2302 m = gfc_match_char_spec (ts);
2304 if (m == MATCH_NO)
2305 m = MATCH_YES; /* No kind specifier found. */
2307 return m;
2311 /* Used in gfc_match_allocate to check that a allocation-object and
2312 a source-expr are conformable. This does not catch all possible
2313 cases; in particular a runtime checking is needed. */
2315 static gfc_try
2316 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
2318 /* First compare rank. */
2319 if (e2->ref && e1->rank != e2->ref->u.ar.as->rank)
2321 gfc_error ("Source-expr at %L must be scalar or have the "
2322 "same rank as the allocate-object at %L",
2323 &e1->where, &e2->where);
2324 return FAILURE;
2327 if (e1->shape)
2329 int i;
2330 mpz_t s;
2332 mpz_init (s);
2334 for (i = 0; i < e1->rank; i++)
2336 if (e2->ref->u.ar.end[i])
2338 mpz_set (s, e2->ref->u.ar.end[i]->value.integer);
2339 mpz_sub (s, s, e2->ref->u.ar.start[i]->value.integer);
2340 mpz_add_ui (s, s, 1);
2342 else
2344 mpz_set (s, e2->ref->u.ar.start[i]->value.integer);
2347 if (mpz_cmp (e1->shape[i], s) != 0)
2349 gfc_error ("Source-expr at %L and allocate-object at %L must "
2350 "have the same shape", &e1->where, &e2->where);
2351 mpz_clear (s);
2352 return FAILURE;
2356 mpz_clear (s);
2359 return SUCCESS;
2363 /* Match an ALLOCATE statement. */
2365 match
2366 gfc_match_allocate (void)
2368 gfc_alloc *head, *tail;
2369 gfc_expr *stat, *errmsg, *tmp, *source;
2370 gfc_typespec ts;
2371 match m;
2372 locus old_locus;
2373 bool saw_stat, saw_errmsg, saw_source, b1, b2, b3;
2375 head = tail = NULL;
2376 stat = errmsg = source = tmp = NULL;
2377 saw_stat = saw_errmsg = saw_source = false;
2379 if (gfc_match_char ('(') != MATCH_YES)
2380 goto syntax;
2382 /* Match an optional intrinsic-type-spec. */
2383 old_locus = gfc_current_locus;
2384 m = match_intrinsic_typespec (&ts);
2385 if (m == MATCH_ERROR)
2386 goto cleanup;
2387 else if (m == MATCH_NO)
2388 ts.type = BT_UNKNOWN;
2389 else
2391 if (gfc_match (" :: ") == MATCH_YES)
2393 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: typespec in "
2394 "ALLOCATE at %L", &old_locus) == FAILURE)
2395 goto cleanup;
2397 else
2399 ts.type = BT_UNKNOWN;
2400 gfc_current_locus = old_locus;
2404 for (;;)
2406 if (head == NULL)
2407 head = tail = gfc_get_alloc ();
2408 else
2410 tail->next = gfc_get_alloc ();
2411 tail = tail->next;
2414 m = gfc_match_variable (&tail->expr, 0);
2415 if (m == MATCH_NO)
2416 goto syntax;
2417 if (m == MATCH_ERROR)
2418 goto cleanup;
2420 if (gfc_check_do_variable (tail->expr->symtree))
2421 goto cleanup;
2423 if (gfc_pure (NULL) && gfc_impure_variable (tail->expr->symtree->n.sym))
2425 gfc_error ("Bad allocate-object at %C for a PURE procedure");
2426 goto cleanup;
2429 /* The ALLOCATE statement had an optional typespec. Check the
2430 constraints. */
2431 if (ts.type != BT_UNKNOWN)
2433 /* Enforce C626. */
2434 if (ts.type != tail->expr->ts.type)
2436 gfc_error ("Type of entity at %L is type incompatible with "
2437 "typespec", &tail->expr->where);
2438 goto cleanup;
2441 /* Enforce C627. */
2442 if (ts.kind != tail->expr->ts.kind)
2444 gfc_error ("Kind type parameter for entity at %L differs from "
2445 "the kind type parameter of the typespec",
2446 &tail->expr->where);
2447 goto cleanup;
2451 if (tail->expr->ts.type == BT_DERIVED)
2452 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
2454 /* FIXME: disable the checking on derived types and arrays. */
2455 b1 = !(tail->expr->ref
2456 && (tail->expr->ref->type == REF_COMPONENT
2457 || tail->expr->ref->type == REF_ARRAY));
2458 b2 = tail->expr->symtree->n.sym
2459 && !(tail->expr->symtree->n.sym->attr.allocatable
2460 || tail->expr->symtree->n.sym->attr.pointer
2461 || tail->expr->symtree->n.sym->attr.proc_pointer);
2462 b3 = tail->expr->symtree->n.sym
2463 && tail->expr->symtree->n.sym->ns
2464 && tail->expr->symtree->n.sym->ns->proc_name
2465 && (tail->expr->symtree->n.sym->ns->proc_name->attr.allocatable
2466 || tail->expr->symtree->n.sym->ns->proc_name->attr.pointer
2467 || tail->expr->symtree->n.sym->ns->proc_name->attr.proc_pointer);
2468 if (b1 && b2 && !b3)
2470 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
2471 "or an allocatable variable");
2472 goto cleanup;
2475 if (gfc_match_char (',') != MATCH_YES)
2476 break;
2478 alloc_opt_list:
2480 m = gfc_match (" stat = %v", &tmp);
2481 if (m == MATCH_ERROR)
2482 goto cleanup;
2483 if (m == MATCH_YES)
2485 /* Enforce C630. */
2486 if (saw_stat)
2488 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
2489 goto cleanup;
2492 stat = tmp;
2493 saw_stat = true;
2495 if (gfc_check_do_variable (stat->symtree))
2496 goto cleanup;
2498 if (gfc_match_char (',') == MATCH_YES)
2499 goto alloc_opt_list;
2502 m = gfc_match (" errmsg = %v", &tmp);
2503 if (m == MATCH_ERROR)
2504 goto cleanup;
2505 if (m == MATCH_YES)
2507 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ERRMSG tag at %L",
2508 &tmp->where) == FAILURE)
2509 goto cleanup;
2511 /* Enforce C630. */
2512 if (saw_errmsg)
2514 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
2515 goto cleanup;
2518 errmsg = tmp;
2519 saw_errmsg = true;
2521 if (gfc_match_char (',') == MATCH_YES)
2522 goto alloc_opt_list;
2525 m = gfc_match (" source = %e", &tmp);
2526 if (m == MATCH_ERROR)
2527 goto cleanup;
2528 if (m == MATCH_YES)
2530 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SOURCE tag at %L",
2531 &tmp->where) == FAILURE)
2532 goto cleanup;
2534 /* Enforce C630. */
2535 if (saw_source)
2537 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
2538 goto cleanup;
2541 /* The next 3 conditionals check C631. */
2542 if (ts.type != BT_UNKNOWN)
2544 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
2545 &tmp->where, &old_locus);
2546 goto cleanup;
2549 if (head->next)
2551 gfc_error ("SOURCE tag at %L requires only a single entity in "
2552 "the allocation-list", &tmp->where);
2553 goto cleanup;
2556 gfc_resolve_expr (tmp);
2558 if (head->expr->ts.type != tmp->ts.type)
2560 gfc_error ("Type of entity at %L is type incompatible with "
2561 "source-expr at %L", &head->expr->where, &tmp->where);
2562 goto cleanup;
2565 /* Check C633. */
2566 if (tmp->ts.kind != head->expr->ts.kind)
2568 gfc_error ("The allocate-object at %L and the source-expr at %L "
2569 "shall have the same kind type parameter",
2570 &head->expr->where, &tmp->where);
2571 goto cleanup;
2574 /* Check C632 and restriction following Note 6.18. */
2575 if (tmp->rank > 0 && conformable_arrays (tmp, head->expr) == FAILURE)
2576 goto cleanup;
2578 source = tmp;
2579 saw_source = true;
2581 if (gfc_match_char (',') == MATCH_YES)
2582 goto alloc_opt_list;
2585 gfc_gobble_whitespace ();
2587 if (gfc_peek_char () == ')')
2588 break;
2592 if (gfc_match (" )%t") != MATCH_YES)
2593 goto syntax;
2595 new_st.op = EXEC_ALLOCATE;
2596 new_st.expr1 = stat;
2597 new_st.expr2 = errmsg;
2598 new_st.expr3 = source;
2599 new_st.ext.alloc_list = head;
2601 return MATCH_YES;
2603 syntax:
2604 gfc_syntax_error (ST_ALLOCATE);
2606 cleanup:
2607 gfc_free_expr (errmsg);
2608 gfc_free_expr (source);
2609 gfc_free_expr (stat);
2610 gfc_free_expr (tmp);
2611 gfc_free_alloc_list (head);
2612 return MATCH_ERROR;
2616 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
2617 a set of pointer assignments to intrinsic NULL(). */
2619 match
2620 gfc_match_nullify (void)
2622 gfc_code *tail;
2623 gfc_expr *e, *p;
2624 match m;
2626 tail = NULL;
2628 if (gfc_match_char ('(') != MATCH_YES)
2629 goto syntax;
2631 for (;;)
2633 m = gfc_match_variable (&p, 0);
2634 if (m == MATCH_ERROR)
2635 goto cleanup;
2636 if (m == MATCH_NO)
2637 goto syntax;
2639 if (gfc_check_do_variable (p->symtree))
2640 goto cleanup;
2642 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
2644 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
2645 goto cleanup;
2648 /* build ' => NULL() '. */
2649 e = gfc_get_expr ();
2650 e->where = gfc_current_locus;
2651 e->expr_type = EXPR_NULL;
2652 e->ts.type = BT_UNKNOWN;
2654 /* Chain to list. */
2655 if (tail == NULL)
2656 tail = &new_st;
2657 else
2659 tail->next = gfc_get_code ();
2660 tail = tail->next;
2663 tail->op = EXEC_POINTER_ASSIGN;
2664 tail->expr1 = p;
2665 tail->expr2 = e;
2667 if (gfc_match (" )%t") == MATCH_YES)
2668 break;
2669 if (gfc_match_char (',') != MATCH_YES)
2670 goto syntax;
2673 return MATCH_YES;
2675 syntax:
2676 gfc_syntax_error (ST_NULLIFY);
2678 cleanup:
2679 gfc_free_statements (new_st.next);
2680 new_st.next = NULL;
2681 gfc_free_expr (new_st.expr1);
2682 new_st.expr1 = NULL;
2683 gfc_free_expr (new_st.expr2);
2684 new_st.expr2 = NULL;
2685 return MATCH_ERROR;
2689 /* Match a DEALLOCATE statement. */
2691 match
2692 gfc_match_deallocate (void)
2694 gfc_alloc *head, *tail;
2695 gfc_expr *stat, *errmsg, *tmp;
2696 match m;
2697 bool saw_stat, saw_errmsg;
2699 head = tail = NULL;
2700 stat = errmsg = tmp = NULL;
2701 saw_stat = saw_errmsg = false;
2703 if (gfc_match_char ('(') != MATCH_YES)
2704 goto syntax;
2706 for (;;)
2708 if (head == NULL)
2709 head = tail = gfc_get_alloc ();
2710 else
2712 tail->next = gfc_get_alloc ();
2713 tail = tail->next;
2716 m = gfc_match_variable (&tail->expr, 0);
2717 if (m == MATCH_ERROR)
2718 goto cleanup;
2719 if (m == MATCH_NO)
2720 goto syntax;
2722 if (gfc_check_do_variable (tail->expr->symtree))
2723 goto cleanup;
2725 if (gfc_pure (NULL) && gfc_impure_variable (tail->expr->symtree->n.sym))
2727 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
2728 goto cleanup;
2731 /* FIXME: disable the checking on derived types. */
2732 if (!(tail->expr->ref
2733 && (tail->expr->ref->type == REF_COMPONENT
2734 || tail->expr->ref->type == REF_ARRAY))
2735 && tail->expr->symtree->n.sym
2736 && !(tail->expr->symtree->n.sym->attr.allocatable
2737 || tail->expr->symtree->n.sym->attr.pointer
2738 || tail->expr->symtree->n.sym->attr.proc_pointer))
2740 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
2741 "or an allocatable variable");
2742 goto cleanup;
2745 if (gfc_match_char (',') != MATCH_YES)
2746 break;
2748 dealloc_opt_list:
2750 m = gfc_match (" stat = %v", &tmp);
2751 if (m == MATCH_ERROR)
2752 goto cleanup;
2753 if (m == MATCH_YES)
2755 if (saw_stat)
2757 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
2758 gfc_free_expr (tmp);
2759 goto cleanup;
2762 stat = tmp;
2763 saw_stat = true;
2765 if (gfc_check_do_variable (stat->symtree))
2766 goto cleanup;
2768 if (gfc_match_char (',') == MATCH_YES)
2769 goto dealloc_opt_list;
2772 m = gfc_match (" errmsg = %v", &tmp);
2773 if (m == MATCH_ERROR)
2774 goto cleanup;
2775 if (m == MATCH_YES)
2777 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ERRMSG at %L",
2778 &tmp->where) == FAILURE)
2779 goto cleanup;
2781 if (saw_errmsg)
2783 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
2784 gfc_free_expr (tmp);
2785 goto cleanup;
2788 errmsg = tmp;
2789 saw_errmsg = true;
2791 if (gfc_match_char (',') == MATCH_YES)
2792 goto dealloc_opt_list;
2795 gfc_gobble_whitespace ();
2797 if (gfc_peek_char () == ')')
2798 break;
2801 if (gfc_match (" )%t") != MATCH_YES)
2802 goto syntax;
2804 new_st.op = EXEC_DEALLOCATE;
2805 new_st.expr1 = stat;
2806 new_st.expr2 = errmsg;
2807 new_st.ext.alloc_list = head;
2809 return MATCH_YES;
2811 syntax:
2812 gfc_syntax_error (ST_DEALLOCATE);
2814 cleanup:
2815 gfc_free_expr (errmsg);
2816 gfc_free_expr (stat);
2817 gfc_free_alloc_list (head);
2818 return MATCH_ERROR;
2822 /* Match a RETURN statement. */
2824 match
2825 gfc_match_return (void)
2827 gfc_expr *e;
2828 match m;
2829 gfc_compile_state s;
2831 e = NULL;
2832 if (gfc_match_eos () == MATCH_YES)
2833 goto done;
2835 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2837 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2838 "a SUBROUTINE");
2839 goto cleanup;
2842 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Alternate RETURN "
2843 "at %C") == FAILURE)
2844 return MATCH_ERROR;
2846 if (gfc_current_form == FORM_FREE)
2848 /* The following are valid, so we can't require a blank after the
2849 RETURN keyword:
2850 return+1
2851 return(1) */
2852 char c = gfc_peek_ascii_char ();
2853 if (ISALPHA (c) || ISDIGIT (c))
2854 return MATCH_NO;
2857 m = gfc_match (" %e%t", &e);
2858 if (m == MATCH_YES)
2859 goto done;
2860 if (m == MATCH_ERROR)
2861 goto cleanup;
2863 gfc_syntax_error (ST_RETURN);
2865 cleanup:
2866 gfc_free_expr (e);
2867 return MATCH_ERROR;
2869 done:
2870 gfc_enclosing_unit (&s);
2871 if (s == COMP_PROGRAM
2872 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2873 "main program at %C") == FAILURE)
2874 return MATCH_ERROR;
2876 new_st.op = EXEC_RETURN;
2877 new_st.expr1 = e;
2879 return MATCH_YES;
2883 /* Match the call of a type-bound procedure, if CALL%var has already been
2884 matched and var found to be a derived-type variable. */
2886 static match
2887 match_typebound_call (gfc_symtree* varst)
2889 gfc_symbol* var;
2890 gfc_expr* base;
2891 match m;
2893 var = varst->n.sym;
2895 base = gfc_get_expr ();
2896 base->expr_type = EXPR_VARIABLE;
2897 base->symtree = varst;
2898 base->where = gfc_current_locus;
2899 gfc_set_sym_referenced (varst->n.sym);
2901 m = gfc_match_varspec (base, 0, true, true);
2902 if (m == MATCH_NO)
2903 gfc_error ("Expected component reference at %C");
2904 if (m != MATCH_YES)
2905 return MATCH_ERROR;
2907 if (gfc_match_eos () != MATCH_YES)
2909 gfc_error ("Junk after CALL at %C");
2910 return MATCH_ERROR;
2913 if (base->expr_type == EXPR_COMPCALL)
2914 new_st.op = EXEC_COMPCALL;
2915 else if (base->expr_type == EXPR_PPC)
2916 new_st.op = EXEC_CALL_PPC;
2917 else
2919 gfc_error ("Expected type-bound procedure or procedure pointer component "
2920 "at %C");
2921 return MATCH_ERROR;
2923 new_st.expr1 = base;
2925 return MATCH_YES;
2929 /* Match a CALL statement. The tricky part here are possible
2930 alternate return specifiers. We handle these by having all
2931 "subroutines" actually return an integer via a register that gives
2932 the return number. If the call specifies alternate returns, we
2933 generate code for a SELECT statement whose case clauses contain
2934 GOTOs to the various labels. */
2936 match
2937 gfc_match_call (void)
2939 char name[GFC_MAX_SYMBOL_LEN + 1];
2940 gfc_actual_arglist *a, *arglist;
2941 gfc_case *new_case;
2942 gfc_symbol *sym;
2943 gfc_symtree *st;
2944 gfc_code *c;
2945 match m;
2946 int i;
2948 arglist = NULL;
2950 m = gfc_match ("% %n", name);
2951 if (m == MATCH_NO)
2952 goto syntax;
2953 if (m != MATCH_YES)
2954 return m;
2956 if (gfc_get_ha_sym_tree (name, &st))
2957 return MATCH_ERROR;
2959 sym = st->n.sym;
2961 /* If this is a variable of derived-type, it probably starts a type-bound
2962 procedure call. */
2963 if (sym->attr.flavor != FL_PROCEDURE && sym->ts.type == BT_DERIVED)
2964 return match_typebound_call (st);
2966 /* If it does not seem to be callable (include functions so that the
2967 right association is made. They are thrown out in resolution.)
2968 ... */
2969 if (!sym->attr.generic
2970 && !sym->attr.subroutine
2971 && !sym->attr.function)
2973 if (!(sym->attr.external && !sym->attr.referenced))
2975 /* ...create a symbol in this scope... */
2976 if (sym->ns != gfc_current_ns
2977 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
2978 return MATCH_ERROR;
2980 if (sym != st->n.sym)
2981 sym = st->n.sym;
2984 /* ...and then to try to make the symbol into a subroutine. */
2985 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2986 return MATCH_ERROR;
2989 gfc_set_sym_referenced (sym);
2991 if (gfc_match_eos () != MATCH_YES)
2993 m = gfc_match_actual_arglist (1, &arglist);
2994 if (m == MATCH_NO)
2995 goto syntax;
2996 if (m == MATCH_ERROR)
2997 goto cleanup;
2999 if (gfc_match_eos () != MATCH_YES)
3000 goto syntax;
3003 /* If any alternate return labels were found, construct a SELECT
3004 statement that will jump to the right place. */
3006 i = 0;
3007 for (a = arglist; a; a = a->next)
3008 if (a->expr == NULL)
3009 i = 1;
3011 if (i)
3013 gfc_symtree *select_st;
3014 gfc_symbol *select_sym;
3015 char name[GFC_MAX_SYMBOL_LEN + 1];
3017 new_st.next = c = gfc_get_code ();
3018 c->op = EXEC_SELECT;
3019 sprintf (name, "_result_%s", sym->name);
3020 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
3022 select_sym = select_st->n.sym;
3023 select_sym->ts.type = BT_INTEGER;
3024 select_sym->ts.kind = gfc_default_integer_kind;
3025 gfc_set_sym_referenced (select_sym);
3026 c->expr1 = gfc_get_expr ();
3027 c->expr1->expr_type = EXPR_VARIABLE;
3028 c->expr1->symtree = select_st;
3029 c->expr1->ts = select_sym->ts;
3030 c->expr1->where = gfc_current_locus;
3032 i = 0;
3033 for (a = arglist; a; a = a->next)
3035 if (a->expr != NULL)
3036 continue;
3038 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
3039 continue;
3041 i++;
3043 c->block = gfc_get_code ();
3044 c = c->block;
3045 c->op = EXEC_SELECT;
3047 new_case = gfc_get_case ();
3048 new_case->high = new_case->low = gfc_int_expr (i);
3049 c->ext.case_list = new_case;
3051 c->next = gfc_get_code ();
3052 c->next->op = EXEC_GOTO;
3053 c->next->label1 = a->label;
3057 new_st.op = EXEC_CALL;
3058 new_st.symtree = st;
3059 new_st.ext.actual = arglist;
3061 return MATCH_YES;
3063 syntax:
3064 gfc_syntax_error (ST_CALL);
3066 cleanup:
3067 gfc_free_actual_arglist (arglist);
3068 return MATCH_ERROR;
3072 /* Given a name, return a pointer to the common head structure,
3073 creating it if it does not exist. If FROM_MODULE is nonzero, we
3074 mangle the name so that it doesn't interfere with commons defined
3075 in the using namespace.
3076 TODO: Add to global symbol tree. */
3078 gfc_common_head *
3079 gfc_get_common (const char *name, int from_module)
3081 gfc_symtree *st;
3082 static int serial = 0;
3083 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
3085 if (from_module)
3087 /* A use associated common block is only needed to correctly layout
3088 the variables it contains. */
3089 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
3090 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
3092 else
3094 st = gfc_find_symtree (gfc_current_ns->common_root, name);
3096 if (st == NULL)
3097 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
3100 if (st->n.common == NULL)
3102 st->n.common = gfc_get_common_head ();
3103 st->n.common->where = gfc_current_locus;
3104 strcpy (st->n.common->name, name);
3107 return st->n.common;
3111 /* Match a common block name. */
3113 match match_common_name (char *name)
3115 match m;
3117 if (gfc_match_char ('/') == MATCH_NO)
3119 name[0] = '\0';
3120 return MATCH_YES;
3123 if (gfc_match_char ('/') == MATCH_YES)
3125 name[0] = '\0';
3126 return MATCH_YES;
3129 m = gfc_match_name (name);
3131 if (m == MATCH_ERROR)
3132 return MATCH_ERROR;
3133 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
3134 return MATCH_YES;
3136 gfc_error ("Syntax error in common block name at %C");
3137 return MATCH_ERROR;
3141 /* Match a COMMON statement. */
3143 match
3144 gfc_match_common (void)
3146 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
3147 char name[GFC_MAX_SYMBOL_LEN + 1];
3148 gfc_common_head *t;
3149 gfc_array_spec *as;
3150 gfc_equiv *e1, *e2;
3151 match m;
3152 gfc_gsymbol *gsym;
3154 old_blank_common = gfc_current_ns->blank_common.head;
3155 if (old_blank_common)
3157 while (old_blank_common->common_next)
3158 old_blank_common = old_blank_common->common_next;
3161 as = NULL;
3163 for (;;)
3165 m = match_common_name (name);
3166 if (m == MATCH_ERROR)
3167 goto cleanup;
3169 gsym = gfc_get_gsymbol (name);
3170 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
3172 gfc_error ("Symbol '%s' at %C is already an external symbol that "
3173 "is not COMMON", name);
3174 goto cleanup;
3177 if (gsym->type == GSYM_UNKNOWN)
3179 gsym->type = GSYM_COMMON;
3180 gsym->where = gfc_current_locus;
3181 gsym->defined = 1;
3184 gsym->used = 1;
3186 if (name[0] == '\0')
3188 t = &gfc_current_ns->blank_common;
3189 if (t->head == NULL)
3190 t->where = gfc_current_locus;
3192 else
3194 t = gfc_get_common (name, 0);
3196 head = &t->head;
3198 if (*head == NULL)
3199 tail = NULL;
3200 else
3202 tail = *head;
3203 while (tail->common_next)
3204 tail = tail->common_next;
3207 /* Grab the list of symbols. */
3208 for (;;)
3210 m = gfc_match_symbol (&sym, 0);
3211 if (m == MATCH_ERROR)
3212 goto cleanup;
3213 if (m == MATCH_NO)
3214 goto syntax;
3216 /* Store a ref to the common block for error checking. */
3217 sym->common_block = t;
3219 /* See if we know the current common block is bind(c), and if
3220 so, then see if we can check if the symbol is (which it'll
3221 need to be). This can happen if the bind(c) attr stmt was
3222 applied to the common block, and the variable(s) already
3223 defined, before declaring the common block. */
3224 if (t->is_bind_c == 1)
3226 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
3228 /* If we find an error, just print it and continue,
3229 cause it's just semantic, and we can see if there
3230 are more errors. */
3231 gfc_error_now ("Variable '%s' at %L in common block '%s' "
3232 "at %C must be declared with a C "
3233 "interoperable kind since common block "
3234 "'%s' is bind(c)",
3235 sym->name, &(sym->declared_at), t->name,
3236 t->name);
3239 if (sym->attr.is_bind_c == 1)
3240 gfc_error_now ("Variable '%s' in common block "
3241 "'%s' at %C can not be bind(c) since "
3242 "it is not global", sym->name, t->name);
3245 if (sym->attr.in_common)
3247 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
3248 sym->name);
3249 goto cleanup;
3252 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
3253 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
3255 if (gfc_notify_std (GFC_STD_GNU, "Initialized symbol '%s' at %C "
3256 "can only be COMMON in "
3257 "BLOCK DATA", sym->name)
3258 == FAILURE)
3259 goto cleanup;
3262 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
3263 goto cleanup;
3265 if (tail != NULL)
3266 tail->common_next = sym;
3267 else
3268 *head = sym;
3270 tail = sym;
3272 /* Deal with an optional array specification after the
3273 symbol name. */
3274 m = gfc_match_array_spec (&as);
3275 if (m == MATCH_ERROR)
3276 goto cleanup;
3278 if (m == MATCH_YES)
3280 if (as->type != AS_EXPLICIT)
3282 gfc_error ("Array specification for symbol '%s' in COMMON "
3283 "at %C must be explicit", sym->name);
3284 goto cleanup;
3287 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
3288 goto cleanup;
3290 if (sym->attr.pointer)
3292 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
3293 "POINTER array", sym->name);
3294 goto cleanup;
3297 sym->as = as;
3298 as = NULL;
3302 sym->common_head = t;
3304 /* Check to see if the symbol is already in an equivalence group.
3305 If it is, set the other members as being in common. */
3306 if (sym->attr.in_equivalence)
3308 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
3310 for (e2 = e1; e2; e2 = e2->eq)
3311 if (e2->expr->symtree->n.sym == sym)
3312 goto equiv_found;
3314 continue;
3316 equiv_found:
3318 for (e2 = e1; e2; e2 = e2->eq)
3320 other = e2->expr->symtree->n.sym;
3321 if (other->common_head
3322 && other->common_head != sym->common_head)
3324 gfc_error ("Symbol '%s', in COMMON block '%s' at "
3325 "%C is being indirectly equivalenced to "
3326 "another COMMON block '%s'",
3327 sym->name, sym->common_head->name,
3328 other->common_head->name);
3329 goto cleanup;
3331 other->attr.in_common = 1;
3332 other->common_head = t;
3338 gfc_gobble_whitespace ();
3339 if (gfc_match_eos () == MATCH_YES)
3340 goto done;
3341 if (gfc_peek_ascii_char () == '/')
3342 break;
3343 if (gfc_match_char (',') != MATCH_YES)
3344 goto syntax;
3345 gfc_gobble_whitespace ();
3346 if (gfc_peek_ascii_char () == '/')
3347 break;
3351 done:
3352 return MATCH_YES;
3354 syntax:
3355 gfc_syntax_error (ST_COMMON);
3357 cleanup:
3358 if (old_blank_common)
3359 old_blank_common->common_next = NULL;
3360 else
3361 gfc_current_ns->blank_common.head = NULL;
3362 gfc_free_array_spec (as);
3363 return MATCH_ERROR;
3367 /* Match a BLOCK DATA program unit. */
3369 match
3370 gfc_match_block_data (void)
3372 char name[GFC_MAX_SYMBOL_LEN + 1];
3373 gfc_symbol *sym;
3374 match m;
3376 if (gfc_match_eos () == MATCH_YES)
3378 gfc_new_block = NULL;
3379 return MATCH_YES;
3382 m = gfc_match ("% %n%t", name);
3383 if (m != MATCH_YES)
3384 return MATCH_ERROR;
3386 if (gfc_get_symbol (name, NULL, &sym))
3387 return MATCH_ERROR;
3389 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
3390 return MATCH_ERROR;
3392 gfc_new_block = sym;
3394 return MATCH_YES;
3398 /* Free a namelist structure. */
3400 void
3401 gfc_free_namelist (gfc_namelist *name)
3403 gfc_namelist *n;
3405 for (; name; name = n)
3407 n = name->next;
3408 gfc_free (name);
3413 /* Match a NAMELIST statement. */
3415 match
3416 gfc_match_namelist (void)
3418 gfc_symbol *group_name, *sym;
3419 gfc_namelist *nl;
3420 match m, m2;
3422 m = gfc_match (" / %s /", &group_name);
3423 if (m == MATCH_NO)
3424 goto syntax;
3425 if (m == MATCH_ERROR)
3426 goto error;
3428 for (;;)
3430 if (group_name->ts.type != BT_UNKNOWN)
3432 gfc_error ("Namelist group name '%s' at %C already has a basic "
3433 "type of %s", group_name->name,
3434 gfc_typename (&group_name->ts));
3435 return MATCH_ERROR;
3438 if (group_name->attr.flavor == FL_NAMELIST
3439 && group_name->attr.use_assoc
3440 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
3441 "at %C already is USE associated and can"
3442 "not be respecified.", group_name->name)
3443 == FAILURE)
3444 return MATCH_ERROR;
3446 if (group_name->attr.flavor != FL_NAMELIST
3447 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
3448 group_name->name, NULL) == FAILURE)
3449 return MATCH_ERROR;
3451 for (;;)
3453 m = gfc_match_symbol (&sym, 1);
3454 if (m == MATCH_NO)
3455 goto syntax;
3456 if (m == MATCH_ERROR)
3457 goto error;
3459 if (sym->attr.in_namelist == 0
3460 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
3461 goto error;
3463 /* Use gfc_error_check here, rather than goto error, so that
3464 these are the only errors for the next two lines. */
3465 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
3467 gfc_error ("Assumed size array '%s' in namelist '%s' at "
3468 "%C is not allowed", sym->name, group_name->name);
3469 gfc_error_check ();
3472 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl->length == NULL)
3474 gfc_error ("Assumed character length '%s' in namelist '%s' at "
3475 "%C is not allowed", sym->name, group_name->name);
3476 gfc_error_check ();
3479 nl = gfc_get_namelist ();
3480 nl->sym = sym;
3481 sym->refs++;
3483 if (group_name->namelist == NULL)
3484 group_name->namelist = group_name->namelist_tail = nl;
3485 else
3487 group_name->namelist_tail->next = nl;
3488 group_name->namelist_tail = nl;
3491 if (gfc_match_eos () == MATCH_YES)
3492 goto done;
3494 m = gfc_match_char (',');
3496 if (gfc_match_char ('/') == MATCH_YES)
3498 m2 = gfc_match (" %s /", &group_name);
3499 if (m2 == MATCH_YES)
3500 break;
3501 if (m2 == MATCH_ERROR)
3502 goto error;
3503 goto syntax;
3506 if (m != MATCH_YES)
3507 goto syntax;
3511 done:
3512 return MATCH_YES;
3514 syntax:
3515 gfc_syntax_error (ST_NAMELIST);
3517 error:
3518 return MATCH_ERROR;
3522 /* Match a MODULE statement. */
3524 match
3525 gfc_match_module (void)
3527 match m;
3529 m = gfc_match (" %s%t", &gfc_new_block);
3530 if (m != MATCH_YES)
3531 return m;
3533 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
3534 gfc_new_block->name, NULL) == FAILURE)
3535 return MATCH_ERROR;
3537 return MATCH_YES;
3541 /* Free equivalence sets and lists. Recursively is the easiest way to
3542 do this. */
3544 void
3545 gfc_free_equiv (gfc_equiv *eq)
3547 if (eq == NULL)
3548 return;
3550 gfc_free_equiv (eq->eq);
3551 gfc_free_equiv (eq->next);
3552 gfc_free_expr (eq->expr);
3553 gfc_free (eq);
3557 /* Match an EQUIVALENCE statement. */
3559 match
3560 gfc_match_equivalence (void)
3562 gfc_equiv *eq, *set, *tail;
3563 gfc_ref *ref;
3564 gfc_symbol *sym;
3565 match m;
3566 gfc_common_head *common_head = NULL;
3567 bool common_flag;
3568 int cnt;
3570 tail = NULL;
3572 for (;;)
3574 eq = gfc_get_equiv ();
3575 if (tail == NULL)
3576 tail = eq;
3578 eq->next = gfc_current_ns->equiv;
3579 gfc_current_ns->equiv = eq;
3581 if (gfc_match_char ('(') != MATCH_YES)
3582 goto syntax;
3584 set = eq;
3585 common_flag = FALSE;
3586 cnt = 0;
3588 for (;;)
3590 m = gfc_match_equiv_variable (&set->expr);
3591 if (m == MATCH_ERROR)
3592 goto cleanup;
3593 if (m == MATCH_NO)
3594 goto syntax;
3596 /* count the number of objects. */
3597 cnt++;
3599 if (gfc_match_char ('%') == MATCH_YES)
3601 gfc_error ("Derived type component %C is not a "
3602 "permitted EQUIVALENCE member");
3603 goto cleanup;
3606 for (ref = set->expr->ref; ref; ref = ref->next)
3607 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3609 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
3610 "be an array section");
3611 goto cleanup;
3614 sym = set->expr->symtree->n.sym;
3616 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
3617 goto cleanup;
3619 if (sym->attr.in_common)
3621 common_flag = TRUE;
3622 common_head = sym->common_head;
3625 if (gfc_match_char (')') == MATCH_YES)
3626 break;
3628 if (gfc_match_char (',') != MATCH_YES)
3629 goto syntax;
3631 set->eq = gfc_get_equiv ();
3632 set = set->eq;
3635 if (cnt < 2)
3637 gfc_error ("EQUIVALENCE at %C requires two or more objects");
3638 goto cleanup;
3641 /* If one of the members of an equivalence is in common, then
3642 mark them all as being in common. Before doing this, check
3643 that members of the equivalence group are not in different
3644 common blocks. */
3645 if (common_flag)
3646 for (set = eq; set; set = set->eq)
3648 sym = set->expr->symtree->n.sym;
3649 if (sym->common_head && sym->common_head != common_head)
3651 gfc_error ("Attempt to indirectly overlap COMMON "
3652 "blocks %s and %s by EQUIVALENCE at %C",
3653 sym->common_head->name, common_head->name);
3654 goto cleanup;
3656 sym->attr.in_common = 1;
3657 sym->common_head = common_head;
3660 if (gfc_match_eos () == MATCH_YES)
3661 break;
3662 if (gfc_match_char (',') != MATCH_YES)
3663 goto syntax;
3666 return MATCH_YES;
3668 syntax:
3669 gfc_syntax_error (ST_EQUIVALENCE);
3671 cleanup:
3672 eq = tail->next;
3673 tail->next = NULL;
3675 gfc_free_equiv (gfc_current_ns->equiv);
3676 gfc_current_ns->equiv = eq;
3678 return MATCH_ERROR;
3682 /* Check that a statement function is not recursive. This is done by looking
3683 for the statement function symbol(sym) by looking recursively through its
3684 expression(e). If a reference to sym is found, true is returned.
3685 12.5.4 requires that any variable of function that is implicitly typed
3686 shall have that type confirmed by any subsequent type declaration. The
3687 implicit typing is conveniently done here. */
3688 static bool
3689 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
3691 static bool
3692 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
3695 if (e == NULL)
3696 return false;
3698 switch (e->expr_type)
3700 case EXPR_FUNCTION:
3701 if (e->symtree == NULL)
3702 return false;
3704 /* Check the name before testing for nested recursion! */
3705 if (sym->name == e->symtree->n.sym->name)
3706 return true;
3708 /* Catch recursion via other statement functions. */
3709 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
3710 && e->symtree->n.sym->value
3711 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
3712 return true;
3714 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3715 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3717 break;
3719 case EXPR_VARIABLE:
3720 if (e->symtree && sym->name == e->symtree->n.sym->name)
3721 return true;
3723 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3724 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3725 break;
3727 default:
3728 break;
3731 return false;
3735 static bool
3736 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
3738 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
3742 /* Match a statement function declaration. It is so easy to match
3743 non-statement function statements with a MATCH_ERROR as opposed to
3744 MATCH_NO that we suppress error message in most cases. */
3746 match
3747 gfc_match_st_function (void)
3749 gfc_error_buf old_error;
3750 gfc_symbol *sym;
3751 gfc_expr *expr;
3752 match m;
3754 m = gfc_match_symbol (&sym, 0);
3755 if (m != MATCH_YES)
3756 return m;
3758 gfc_push_error (&old_error);
3760 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
3761 sym->name, NULL) == FAILURE)
3762 goto undo_error;
3764 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
3765 goto undo_error;
3767 m = gfc_match (" = %e%t", &expr);
3768 if (m == MATCH_NO)
3769 goto undo_error;
3771 gfc_free_error (&old_error);
3772 if (m == MATCH_ERROR)
3773 return m;
3775 if (recursive_stmt_fcn (expr, sym))
3777 gfc_error ("Statement function at %L is recursive", &expr->where);
3778 return MATCH_ERROR;
3781 sym->value = expr;
3783 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
3784 "Statement function at %C") == FAILURE)
3785 return MATCH_ERROR;
3787 return MATCH_YES;
3789 undo_error:
3790 gfc_pop_error (&old_error);
3791 return MATCH_NO;
3795 /***************** SELECT CASE subroutines ******************/
3797 /* Free a single case structure. */
3799 static void
3800 free_case (gfc_case *p)
3802 if (p->low == p->high)
3803 p->high = NULL;
3804 gfc_free_expr (p->low);
3805 gfc_free_expr (p->high);
3806 gfc_free (p);
3810 /* Free a list of case structures. */
3812 void
3813 gfc_free_case_list (gfc_case *p)
3815 gfc_case *q;
3817 for (; p; p = q)
3819 q = p->next;
3820 free_case (p);
3825 /* Match a single case selector. */
3827 static match
3828 match_case_selector (gfc_case **cp)
3830 gfc_case *c;
3831 match m;
3833 c = gfc_get_case ();
3834 c->where = gfc_current_locus;
3836 if (gfc_match_char (':') == MATCH_YES)
3838 m = gfc_match_init_expr (&c->high);
3839 if (m == MATCH_NO)
3840 goto need_expr;
3841 if (m == MATCH_ERROR)
3842 goto cleanup;
3844 else
3846 m = gfc_match_init_expr (&c->low);
3847 if (m == MATCH_ERROR)
3848 goto cleanup;
3849 if (m == MATCH_NO)
3850 goto need_expr;
3852 /* If we're not looking at a ':' now, make a range out of a single
3853 target. Else get the upper bound for the case range. */
3854 if (gfc_match_char (':') != MATCH_YES)
3855 c->high = c->low;
3856 else
3858 m = gfc_match_init_expr (&c->high);
3859 if (m == MATCH_ERROR)
3860 goto cleanup;
3861 /* MATCH_NO is fine. It's OK if nothing is there! */
3865 *cp = c;
3866 return MATCH_YES;
3868 need_expr:
3869 gfc_error ("Expected initialization expression in CASE at %C");
3871 cleanup:
3872 free_case (c);
3873 return MATCH_ERROR;
3877 /* Match the end of a case statement. */
3879 static match
3880 match_case_eos (void)
3882 char name[GFC_MAX_SYMBOL_LEN + 1];
3883 match m;
3885 if (gfc_match_eos () == MATCH_YES)
3886 return MATCH_YES;
3888 /* If the case construct doesn't have a case-construct-name, we
3889 should have matched the EOS. */
3890 if (!gfc_current_block ())
3892 gfc_error ("Expected the name of the SELECT CASE construct at %C");
3893 return MATCH_ERROR;
3896 gfc_gobble_whitespace ();
3898 m = gfc_match_name (name);
3899 if (m != MATCH_YES)
3900 return m;
3902 if (strcmp (name, gfc_current_block ()->name) != 0)
3904 gfc_error ("Expected case name of '%s' at %C",
3905 gfc_current_block ()->name);
3906 return MATCH_ERROR;
3909 return gfc_match_eos ();
3913 /* Match a SELECT statement. */
3915 match
3916 gfc_match_select (void)
3918 gfc_expr *expr;
3919 match m;
3921 m = gfc_match_label ();
3922 if (m == MATCH_ERROR)
3923 return m;
3925 m = gfc_match (" select case ( %e )%t", &expr);
3926 if (m != MATCH_YES)
3927 return m;
3929 new_st.op = EXEC_SELECT;
3930 new_st.expr1 = expr;
3932 return MATCH_YES;
3936 /* Match a CASE statement. */
3938 match
3939 gfc_match_case (void)
3941 gfc_case *c, *head, *tail;
3942 match m;
3944 head = tail = NULL;
3946 if (gfc_current_state () != COMP_SELECT)
3948 gfc_error ("Unexpected CASE statement at %C");
3949 return MATCH_ERROR;
3952 if (gfc_match ("% default") == MATCH_YES)
3954 m = match_case_eos ();
3955 if (m == MATCH_NO)
3956 goto syntax;
3957 if (m == MATCH_ERROR)
3958 goto cleanup;
3960 new_st.op = EXEC_SELECT;
3961 c = gfc_get_case ();
3962 c->where = gfc_current_locus;
3963 new_st.ext.case_list = c;
3964 return MATCH_YES;
3967 if (gfc_match_char ('(') != MATCH_YES)
3968 goto syntax;
3970 for (;;)
3972 if (match_case_selector (&c) == MATCH_ERROR)
3973 goto cleanup;
3975 if (head == NULL)
3976 head = c;
3977 else
3978 tail->next = c;
3980 tail = c;
3982 if (gfc_match_char (')') == MATCH_YES)
3983 break;
3984 if (gfc_match_char (',') != MATCH_YES)
3985 goto syntax;
3988 m = match_case_eos ();
3989 if (m == MATCH_NO)
3990 goto syntax;
3991 if (m == MATCH_ERROR)
3992 goto cleanup;
3994 new_st.op = EXEC_SELECT;
3995 new_st.ext.case_list = head;
3997 return MATCH_YES;
3999 syntax:
4000 gfc_error ("Syntax error in CASE-specification at %C");
4002 cleanup:
4003 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
4004 return MATCH_ERROR;
4007 /********************* WHERE subroutines ********************/
4009 /* Match the rest of a simple WHERE statement that follows an IF statement.
4012 static match
4013 match_simple_where (void)
4015 gfc_expr *expr;
4016 gfc_code *c;
4017 match m;
4019 m = gfc_match (" ( %e )", &expr);
4020 if (m != MATCH_YES)
4021 return m;
4023 m = gfc_match_assignment ();
4024 if (m == MATCH_NO)
4025 goto syntax;
4026 if (m == MATCH_ERROR)
4027 goto cleanup;
4029 if (gfc_match_eos () != MATCH_YES)
4030 goto syntax;
4032 c = gfc_get_code ();
4034 c->op = EXEC_WHERE;
4035 c->expr1 = expr;
4036 c->next = gfc_get_code ();
4038 *c->next = new_st;
4039 gfc_clear_new_st ();
4041 new_st.op = EXEC_WHERE;
4042 new_st.block = c;
4044 return MATCH_YES;
4046 syntax:
4047 gfc_syntax_error (ST_WHERE);
4049 cleanup:
4050 gfc_free_expr (expr);
4051 return MATCH_ERROR;
4055 /* Match a WHERE statement. */
4057 match
4058 gfc_match_where (gfc_statement *st)
4060 gfc_expr *expr;
4061 match m0, m;
4062 gfc_code *c;
4064 m0 = gfc_match_label ();
4065 if (m0 == MATCH_ERROR)
4066 return m0;
4068 m = gfc_match (" where ( %e )", &expr);
4069 if (m != MATCH_YES)
4070 return m;
4072 if (gfc_match_eos () == MATCH_YES)
4074 *st = ST_WHERE_BLOCK;
4075 new_st.op = EXEC_WHERE;
4076 new_st.expr1 = expr;
4077 return MATCH_YES;
4080 m = gfc_match_assignment ();
4081 if (m == MATCH_NO)
4082 gfc_syntax_error (ST_WHERE);
4084 if (m != MATCH_YES)
4086 gfc_free_expr (expr);
4087 return MATCH_ERROR;
4090 /* We've got a simple WHERE statement. */
4091 *st = ST_WHERE;
4092 c = gfc_get_code ();
4094 c->op = EXEC_WHERE;
4095 c->expr1 = expr;
4096 c->next = gfc_get_code ();
4098 *c->next = new_st;
4099 gfc_clear_new_st ();
4101 new_st.op = EXEC_WHERE;
4102 new_st.block = c;
4104 return MATCH_YES;
4108 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
4109 new_st if successful. */
4111 match
4112 gfc_match_elsewhere (void)
4114 char name[GFC_MAX_SYMBOL_LEN + 1];
4115 gfc_expr *expr;
4116 match m;
4118 if (gfc_current_state () != COMP_WHERE)
4120 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
4121 return MATCH_ERROR;
4124 expr = NULL;
4126 if (gfc_match_char ('(') == MATCH_YES)
4128 m = gfc_match_expr (&expr);
4129 if (m == MATCH_NO)
4130 goto syntax;
4131 if (m == MATCH_ERROR)
4132 return MATCH_ERROR;
4134 if (gfc_match_char (')') != MATCH_YES)
4135 goto syntax;
4138 if (gfc_match_eos () != MATCH_YES)
4140 /* Only makes sense if we have a where-construct-name. */
4141 if (!gfc_current_block ())
4143 m = MATCH_ERROR;
4144 goto cleanup;
4146 /* Better be a name at this point. */
4147 m = gfc_match_name (name);
4148 if (m == MATCH_NO)
4149 goto syntax;
4150 if (m == MATCH_ERROR)
4151 goto cleanup;
4153 if (gfc_match_eos () != MATCH_YES)
4154 goto syntax;
4156 if (strcmp (name, gfc_current_block ()->name) != 0)
4158 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
4159 name, gfc_current_block ()->name);
4160 goto cleanup;
4164 new_st.op = EXEC_WHERE;
4165 new_st.expr1 = expr;
4166 return MATCH_YES;
4168 syntax:
4169 gfc_syntax_error (ST_ELSEWHERE);
4171 cleanup:
4172 gfc_free_expr (expr);
4173 return MATCH_ERROR;
4177 /******************** FORALL subroutines ********************/
4179 /* Free a list of FORALL iterators. */
4181 void
4182 gfc_free_forall_iterator (gfc_forall_iterator *iter)
4184 gfc_forall_iterator *next;
4186 while (iter)
4188 next = iter->next;
4189 gfc_free_expr (iter->var);
4190 gfc_free_expr (iter->start);
4191 gfc_free_expr (iter->end);
4192 gfc_free_expr (iter->stride);
4193 gfc_free (iter);
4194 iter = next;
4199 /* Match an iterator as part of a FORALL statement. The format is:
4201 <var> = <start>:<end>[:<stride>]
4203 On MATCH_NO, the caller tests for the possibility that there is a
4204 scalar mask expression. */
4206 static match
4207 match_forall_iterator (gfc_forall_iterator **result)
4209 gfc_forall_iterator *iter;
4210 locus where;
4211 match m;
4213 where = gfc_current_locus;
4214 iter = XCNEW (gfc_forall_iterator);
4216 m = gfc_match_expr (&iter->var);
4217 if (m != MATCH_YES)
4218 goto cleanup;
4220 if (gfc_match_char ('=') != MATCH_YES
4221 || iter->var->expr_type != EXPR_VARIABLE)
4223 m = MATCH_NO;
4224 goto cleanup;
4227 m = gfc_match_expr (&iter->start);
4228 if (m != MATCH_YES)
4229 goto cleanup;
4231 if (gfc_match_char (':') != MATCH_YES)
4232 goto syntax;
4234 m = gfc_match_expr (&iter->end);
4235 if (m == MATCH_NO)
4236 goto syntax;
4237 if (m == MATCH_ERROR)
4238 goto cleanup;
4240 if (gfc_match_char (':') == MATCH_NO)
4241 iter->stride = gfc_int_expr (1);
4242 else
4244 m = gfc_match_expr (&iter->stride);
4245 if (m == MATCH_NO)
4246 goto syntax;
4247 if (m == MATCH_ERROR)
4248 goto cleanup;
4251 /* Mark the iteration variable's symbol as used as a FORALL index. */
4252 iter->var->symtree->n.sym->forall_index = true;
4254 *result = iter;
4255 return MATCH_YES;
4257 syntax:
4258 gfc_error ("Syntax error in FORALL iterator at %C");
4259 m = MATCH_ERROR;
4261 cleanup:
4263 gfc_current_locus = where;
4264 gfc_free_forall_iterator (iter);
4265 return m;
4269 /* Match the header of a FORALL statement. */
4271 static match
4272 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
4274 gfc_forall_iterator *head, *tail, *new_iter;
4275 gfc_expr *msk;
4276 match m;
4278 gfc_gobble_whitespace ();
4280 head = tail = NULL;
4281 msk = NULL;
4283 if (gfc_match_char ('(') != MATCH_YES)
4284 return MATCH_NO;
4286 m = match_forall_iterator (&new_iter);
4287 if (m == MATCH_ERROR)
4288 goto cleanup;
4289 if (m == MATCH_NO)
4290 goto syntax;
4292 head = tail = new_iter;
4294 for (;;)
4296 if (gfc_match_char (',') != MATCH_YES)
4297 break;
4299 m = match_forall_iterator (&new_iter);
4300 if (m == MATCH_ERROR)
4301 goto cleanup;
4303 if (m == MATCH_YES)
4305 tail->next = new_iter;
4306 tail = new_iter;
4307 continue;
4310 /* Have to have a mask expression. */
4312 m = gfc_match_expr (&msk);
4313 if (m == MATCH_NO)
4314 goto syntax;
4315 if (m == MATCH_ERROR)
4316 goto cleanup;
4318 break;
4321 if (gfc_match_char (')') == MATCH_NO)
4322 goto syntax;
4324 *phead = head;
4325 *mask = msk;
4326 return MATCH_YES;
4328 syntax:
4329 gfc_syntax_error (ST_FORALL);
4331 cleanup:
4332 gfc_free_expr (msk);
4333 gfc_free_forall_iterator (head);
4335 return MATCH_ERROR;
4338 /* Match the rest of a simple FORALL statement that follows an
4339 IF statement. */
4341 static match
4342 match_simple_forall (void)
4344 gfc_forall_iterator *head;
4345 gfc_expr *mask;
4346 gfc_code *c;
4347 match m;
4349 mask = NULL;
4350 head = NULL;
4351 c = NULL;
4353 m = match_forall_header (&head, &mask);
4355 if (m == MATCH_NO)
4356 goto syntax;
4357 if (m != MATCH_YES)
4358 goto cleanup;
4360 m = gfc_match_assignment ();
4362 if (m == MATCH_ERROR)
4363 goto cleanup;
4364 if (m == MATCH_NO)
4366 m = gfc_match_pointer_assignment ();
4367 if (m == MATCH_ERROR)
4368 goto cleanup;
4369 if (m == MATCH_NO)
4370 goto syntax;
4373 c = gfc_get_code ();
4374 *c = new_st;
4375 c->loc = gfc_current_locus;
4377 if (gfc_match_eos () != MATCH_YES)
4378 goto syntax;
4380 gfc_clear_new_st ();
4381 new_st.op = EXEC_FORALL;
4382 new_st.expr1 = mask;
4383 new_st.ext.forall_iterator = head;
4384 new_st.block = gfc_get_code ();
4386 new_st.block->op = EXEC_FORALL;
4387 new_st.block->next = c;
4389 return MATCH_YES;
4391 syntax:
4392 gfc_syntax_error (ST_FORALL);
4394 cleanup:
4395 gfc_free_forall_iterator (head);
4396 gfc_free_expr (mask);
4398 return MATCH_ERROR;
4402 /* Match a FORALL statement. */
4404 match
4405 gfc_match_forall (gfc_statement *st)
4407 gfc_forall_iterator *head;
4408 gfc_expr *mask;
4409 gfc_code *c;
4410 match m0, m;
4412 head = NULL;
4413 mask = NULL;
4414 c = NULL;
4416 m0 = gfc_match_label ();
4417 if (m0 == MATCH_ERROR)
4418 return MATCH_ERROR;
4420 m = gfc_match (" forall");
4421 if (m != MATCH_YES)
4422 return m;
4424 m = match_forall_header (&head, &mask);
4425 if (m == MATCH_ERROR)
4426 goto cleanup;
4427 if (m == MATCH_NO)
4428 goto syntax;
4430 if (gfc_match_eos () == MATCH_YES)
4432 *st = ST_FORALL_BLOCK;
4433 new_st.op = EXEC_FORALL;
4434 new_st.expr1 = mask;
4435 new_st.ext.forall_iterator = head;
4436 return MATCH_YES;
4439 m = gfc_match_assignment ();
4440 if (m == MATCH_ERROR)
4441 goto cleanup;
4442 if (m == MATCH_NO)
4444 m = gfc_match_pointer_assignment ();
4445 if (m == MATCH_ERROR)
4446 goto cleanup;
4447 if (m == MATCH_NO)
4448 goto syntax;
4451 c = gfc_get_code ();
4452 *c = new_st;
4453 c->loc = gfc_current_locus;
4455 gfc_clear_new_st ();
4456 new_st.op = EXEC_FORALL;
4457 new_st.expr1 = mask;
4458 new_st.ext.forall_iterator = head;
4459 new_st.block = gfc_get_code ();
4460 new_st.block->op = EXEC_FORALL;
4461 new_st.block->next = c;
4463 *st = ST_FORALL;
4464 return MATCH_YES;
4466 syntax:
4467 gfc_syntax_error (ST_FORALL);
4469 cleanup:
4470 gfc_free_forall_iterator (head);
4471 gfc_free_expr (mask);
4472 gfc_free_statements (c);
4473 return MATCH_NO;