PR c++/14032
[official-gcc.git] / gcc / fortran / match.c
blob83b887372cb56b2cac935c3cd34d391e1bae9dfc
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
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"
30 /* For debugging and diagnostic purposes. Return the textual representation
31 of the intrinsic operator OP. */
32 const char *
33 gfc_op2string (gfc_intrinsic_op op)
35 switch (op)
37 case INTRINSIC_UPLUS:
38 case INTRINSIC_PLUS:
39 return "+";
41 case INTRINSIC_UMINUS:
42 case INTRINSIC_MINUS:
43 return "-";
45 case INTRINSIC_POWER:
46 return "**";
47 case INTRINSIC_CONCAT:
48 return "//";
49 case INTRINSIC_TIMES:
50 return "*";
51 case INTRINSIC_DIVIDE:
52 return "/";
54 case INTRINSIC_AND:
55 return ".and.";
56 case INTRINSIC_OR:
57 return ".or.";
58 case INTRINSIC_EQV:
59 return ".eqv.";
60 case INTRINSIC_NEQV:
61 return ".neqv.";
63 case INTRINSIC_EQ_OS:
64 return ".eq.";
65 case INTRINSIC_EQ:
66 return "==";
67 case INTRINSIC_NE_OS:
68 return ".ne.";
69 case INTRINSIC_NE:
70 return "/=";
71 case INTRINSIC_GE_OS:
72 return ".ge.";
73 case INTRINSIC_GE:
74 return ">=";
75 case INTRINSIC_LE_OS:
76 return ".le.";
77 case INTRINSIC_LE:
78 return "<=";
79 case INTRINSIC_LT_OS:
80 return ".lt.";
81 case INTRINSIC_LT:
82 return "<";
83 case INTRINSIC_GT_OS:
84 return ".gt.";
85 case INTRINSIC_GT:
86 return ">";
87 case INTRINSIC_NOT:
88 return ".not.";
90 case INTRINSIC_ASSIGN:
91 return "=";
93 case INTRINSIC_PARENTHESES:
94 return "parens";
96 default:
97 break;
100 gfc_internal_error ("gfc_op2string(): Bad code");
101 /* Not reached. */
105 /******************** Generic matching subroutines ************************/
107 /* See if the next character is a special character that has
108 escaped by a \ via the -fbackslash option. */
110 match
111 gfc_match_special_char (int *c)
114 match m;
116 m = MATCH_YES;
118 switch (gfc_next_char_literal (1))
120 case 'a':
121 *c = '\a';
122 break;
123 case 'b':
124 *c = '\b';
125 break;
126 case 't':
127 *c = '\t';
128 break;
129 case 'f':
130 *c = '\f';
131 break;
132 case 'n':
133 *c = '\n';
134 break;
135 case 'r':
136 *c = '\r';
137 break;
138 case 'v':
139 *c = '\v';
140 break;
141 case '\\':
142 *c = '\\';
143 break;
144 case '0':
145 *c = '\0';
146 break;
147 default:
148 /* Unknown backslash codes are simply not expanded. */
149 m = MATCH_NO;
150 break;
153 return m;
157 /* In free form, match at least one space. Always matches in fixed
158 form. */
160 match
161 gfc_match_space (void)
163 locus old_loc;
164 int c;
166 if (gfc_current_form == FORM_FIXED)
167 return MATCH_YES;
169 old_loc = gfc_current_locus;
171 c = gfc_next_char ();
172 if (!gfc_is_whitespace (c))
174 gfc_current_locus = old_loc;
175 return MATCH_NO;
178 gfc_gobble_whitespace ();
180 return MATCH_YES;
184 /* Match an end of statement. End of statement is optional
185 whitespace, followed by a ';' or '\n' or comment '!'. If a
186 semicolon is found, we continue to eat whitespace and semicolons. */
188 match
189 gfc_match_eos (void)
191 locus old_loc;
192 int flag, c;
194 flag = 0;
196 for (;;)
198 old_loc = gfc_current_locus;
199 gfc_gobble_whitespace ();
201 c = gfc_next_char ();
202 switch (c)
204 case '!':
207 c = gfc_next_char ();
209 while (c != '\n');
211 /* Fall through. */
213 case '\n':
214 return MATCH_YES;
216 case ';':
217 flag = 1;
218 continue;
221 break;
224 gfc_current_locus = old_loc;
225 return (flag) ? MATCH_YES : MATCH_NO;
229 /* Match a literal integer on the input, setting the value on
230 MATCH_YES. Literal ints occur in kind-parameters as well as
231 old-style character length specifications. If cnt is non-NULL it
232 will be set to the number of digits. */
234 match
235 gfc_match_small_literal_int (int *value, int *cnt)
237 locus old_loc;
238 char c;
239 int i, j;
241 old_loc = gfc_current_locus;
243 gfc_gobble_whitespace ();
244 c = gfc_next_char ();
245 if (cnt)
246 *cnt = 0;
248 if (!ISDIGIT (c))
250 gfc_current_locus = old_loc;
251 return MATCH_NO;
254 i = c - '0';
255 j = 1;
257 for (;;)
259 old_loc = gfc_current_locus;
260 c = gfc_next_char ();
262 if (!ISDIGIT (c))
263 break;
265 i = 10 * i + c - '0';
266 j++;
268 if (i > 99999999)
270 gfc_error ("Integer too large at %C");
271 return MATCH_ERROR;
275 gfc_current_locus = old_loc;
277 *value = i;
278 if (cnt)
279 *cnt = j;
280 return MATCH_YES;
284 /* Match a small, constant integer expression, like in a kind
285 statement. On MATCH_YES, 'value' is set. */
287 match
288 gfc_match_small_int (int *value)
290 gfc_expr *expr;
291 const char *p;
292 match m;
293 int i;
295 m = gfc_match_expr (&expr);
296 if (m != MATCH_YES)
297 return m;
299 p = gfc_extract_int (expr, &i);
300 gfc_free_expr (expr);
302 if (p != NULL)
304 gfc_error (p);
305 m = MATCH_ERROR;
308 *value = i;
309 return m;
313 /* This function is the same as the gfc_match_small_int, except that
314 we're keeping the pointer to the expr. This function could just be
315 removed and the previously mentioned one modified, though all calls
316 to it would have to be modified then (and there were a number of
317 them). Return MATCH_ERROR if fail to extract the int; otherwise,
318 return the result of gfc_match_expr(). The expr (if any) that was
319 matched is returned in the parameter expr. */
321 match
322 gfc_match_small_int_expr (int *value, gfc_expr **expr)
324 const char *p;
325 match m;
326 int i;
328 m = gfc_match_expr (expr);
329 if (m != MATCH_YES)
330 return m;
332 p = gfc_extract_int (*expr, &i);
334 if (p != NULL)
336 gfc_error (p);
337 m = MATCH_ERROR;
340 *value = i;
341 return m;
345 /* Matches a statement label. Uses gfc_match_small_literal_int() to
346 do most of the work. */
348 match
349 gfc_match_st_label (gfc_st_label **label)
351 locus old_loc;
352 match m;
353 int i, cnt;
355 old_loc = gfc_current_locus;
357 m = gfc_match_small_literal_int (&i, &cnt);
358 if (m != MATCH_YES)
359 return m;
361 if (cnt > 5)
363 gfc_error ("Too many digits in statement label at %C");
364 goto cleanup;
367 if (i == 0)
369 gfc_error ("Statement label at %C is zero");
370 goto cleanup;
373 *label = gfc_get_st_label (i);
374 return MATCH_YES;
376 cleanup:
378 gfc_current_locus = old_loc;
379 return MATCH_ERROR;
383 /* Match and validate a label associated with a named IF, DO or SELECT
384 statement. If the symbol does not have the label attribute, we add
385 it. We also make sure the symbol does not refer to another
386 (active) block. A matched label is pointed to by gfc_new_block. */
388 match
389 gfc_match_label (void)
391 char name[GFC_MAX_SYMBOL_LEN + 1];
392 match m;
394 gfc_new_block = NULL;
396 m = gfc_match (" %n :", name);
397 if (m != MATCH_YES)
398 return m;
400 if (gfc_get_symbol (name, NULL, &gfc_new_block))
402 gfc_error ("Label name '%s' at %C is ambiguous", name);
403 return MATCH_ERROR;
406 if (gfc_new_block->attr.flavor == FL_LABEL)
408 gfc_error ("Duplicate construct label '%s' at %C", name);
409 return MATCH_ERROR;
412 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
413 gfc_new_block->name, NULL) == FAILURE)
414 return MATCH_ERROR;
416 return MATCH_YES;
420 /* See if the current input looks like a name of some sort. Modifies
421 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
422 Note that options.c restricts max_identifier_length to not more
423 than GFC_MAX_SYMBOL_LEN. */
425 match
426 gfc_match_name (char *buffer)
428 locus old_loc;
429 int i, c;
431 old_loc = gfc_current_locus;
432 gfc_gobble_whitespace ();
434 c = gfc_next_char ();
435 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
437 if (gfc_error_flag_test() == 0)
438 gfc_error ("Invalid character in name at %C");
439 gfc_current_locus = old_loc;
440 return MATCH_NO;
443 i = 0;
447 buffer[i++] = c;
449 if (i > gfc_option.max_identifier_length)
451 gfc_error ("Name at %C is too long");
452 return MATCH_ERROR;
455 old_loc = gfc_current_locus;
456 c = gfc_next_char ();
458 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
460 buffer[i] = '\0';
461 gfc_current_locus = old_loc;
463 return MATCH_YES;
467 /* Match a valid name for C, which is almost the same as for Fortran,
468 except that you can start with an underscore, etc.. It could have
469 been done by modifying the gfc_match_name, but this way other
470 things C allows can be added, such as no limits on the length.
471 Right now, the length is limited to the same thing as Fortran..
472 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
473 input characters from being automatically lower cased, since C is
474 case sensitive. The parameter, buffer, is used to return the name
475 that is matched. Return MATCH_ERROR if the name is too long
476 (though this is a self-imposed limit), MATCH_NO if what we're
477 seeing isn't a name, and MATCH_YES if we successfully match a C
478 name. */
480 match
481 gfc_match_name_C (char *buffer)
483 locus old_loc;
484 int i = 0;
485 int c;
487 old_loc = gfc_current_locus;
488 gfc_gobble_whitespace ();
490 /* Get the next char (first possible char of name) and see if
491 it's valid for C (either a letter or an underscore). */
492 c = gfc_next_char_literal (1);
494 /* If the user put nothing expect spaces between the quotes, it is valid
495 and simply means there is no name= specifier and the name is the fortran
496 symbol name, all lowercase. */
497 if (c == '"' || c == '\'')
499 buffer[0] = '\0';
500 gfc_current_locus = old_loc;
501 return MATCH_YES;
504 if (!ISALPHA (c) && c != '_')
506 gfc_error ("Invalid C name in NAME= specifier at %C");
507 return MATCH_ERROR;
510 /* Continue to read valid variable name characters. */
513 buffer[i++] = c;
515 /* C does not define a maximum length of variable names, to my
516 knowledge, but the compiler typically places a limit on them.
517 For now, i'll use the same as the fortran limit for simplicity,
518 but this may need to be changed to a dynamic buffer that can
519 be realloc'ed here if necessary, or more likely, a larger
520 upper-bound set. */
521 if (i > gfc_option.max_identifier_length)
523 gfc_error ("Name at %C is too long");
524 return MATCH_ERROR;
527 old_loc = gfc_current_locus;
529 /* Get next char; param means we're in a string. */
530 c = gfc_next_char_literal (1);
531 } while (ISALNUM (c) || c == '_');
533 buffer[i] = '\0';
534 gfc_current_locus = old_loc;
536 /* See if we stopped because of whitespace. */
537 if (c == ' ')
539 gfc_gobble_whitespace ();
540 c = gfc_peek_char ();
541 if (c != '"' && c != '\'')
543 gfc_error ("Embedded space in NAME= specifier at %C");
544 return MATCH_ERROR;
548 /* If we stopped because we had an invalid character for a C name, report
549 that to the user by returning MATCH_NO. */
550 if (c != '"' && c != '\'')
552 gfc_error ("Invalid C name in NAME= specifier at %C");
553 return MATCH_ERROR;
556 return MATCH_YES;
560 /* Match a symbol on the input. Modifies the pointer to the symbol
561 pointer if successful. */
563 match
564 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
566 char buffer[GFC_MAX_SYMBOL_LEN + 1];
567 match m;
569 m = gfc_match_name (buffer);
570 if (m != MATCH_YES)
571 return m;
573 if (host_assoc)
574 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
575 ? MATCH_ERROR : MATCH_YES;
577 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
578 return MATCH_ERROR;
580 return MATCH_YES;
584 match
585 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
587 gfc_symtree *st;
588 match m;
590 m = gfc_match_sym_tree (&st, host_assoc);
592 if (m == MATCH_YES)
594 if (st)
595 *matched_symbol = st->n.sym;
596 else
597 *matched_symbol = NULL;
599 else
600 *matched_symbol = NULL;
601 return m;
605 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
606 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
607 in matchexp.c. */
609 match
610 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
612 locus orig_loc = gfc_current_locus;
613 int ch;
615 gfc_gobble_whitespace ();
616 ch = gfc_next_char ();
617 switch (ch)
619 case '+':
620 /* Matched "+". */
621 *result = INTRINSIC_PLUS;
622 return MATCH_YES;
624 case '-':
625 /* Matched "-". */
626 *result = INTRINSIC_MINUS;
627 return MATCH_YES;
629 case '=':
630 if (gfc_next_char () == '=')
632 /* Matched "==". */
633 *result = INTRINSIC_EQ;
634 return MATCH_YES;
636 break;
638 case '<':
639 if (gfc_peek_char () == '=')
641 /* Matched "<=". */
642 gfc_next_char ();
643 *result = INTRINSIC_LE;
644 return MATCH_YES;
646 /* Matched "<". */
647 *result = INTRINSIC_LT;
648 return MATCH_YES;
650 case '>':
651 if (gfc_peek_char () == '=')
653 /* Matched ">=". */
654 gfc_next_char ();
655 *result = INTRINSIC_GE;
656 return MATCH_YES;
658 /* Matched ">". */
659 *result = INTRINSIC_GT;
660 return MATCH_YES;
662 case '*':
663 if (gfc_peek_char () == '*')
665 /* Matched "**". */
666 gfc_next_char ();
667 *result = INTRINSIC_POWER;
668 return MATCH_YES;
670 /* Matched "*". */
671 *result = INTRINSIC_TIMES;
672 return MATCH_YES;
674 case '/':
675 ch = gfc_peek_char ();
676 if (ch == '=')
678 /* Matched "/=". */
679 gfc_next_char ();
680 *result = INTRINSIC_NE;
681 return MATCH_YES;
683 else if (ch == '/')
685 /* Matched "//". */
686 gfc_next_char ();
687 *result = INTRINSIC_CONCAT;
688 return MATCH_YES;
690 /* Matched "/". */
691 *result = INTRINSIC_DIVIDE;
692 return MATCH_YES;
694 case '.':
695 ch = gfc_next_char ();
696 switch (ch)
698 case 'a':
699 if (gfc_next_char () == 'n'
700 && gfc_next_char () == 'd'
701 && gfc_next_char () == '.')
703 /* Matched ".and.". */
704 *result = INTRINSIC_AND;
705 return MATCH_YES;
707 break;
709 case 'e':
710 if (gfc_next_char () == 'q')
712 ch = gfc_next_char ();
713 if (ch == '.')
715 /* Matched ".eq.". */
716 *result = INTRINSIC_EQ_OS;
717 return MATCH_YES;
719 else if (ch == 'v')
721 if (gfc_next_char () == '.')
723 /* Matched ".eqv.". */
724 *result = INTRINSIC_EQV;
725 return MATCH_YES;
729 break;
731 case 'g':
732 ch = gfc_next_char ();
733 if (ch == 'e')
735 if (gfc_next_char () == '.')
737 /* Matched ".ge.". */
738 *result = INTRINSIC_GE_OS;
739 return MATCH_YES;
742 else if (ch == 't')
744 if (gfc_next_char () == '.')
746 /* Matched ".gt.". */
747 *result = INTRINSIC_GT_OS;
748 return MATCH_YES;
751 break;
753 case 'l':
754 ch = gfc_next_char ();
755 if (ch == 'e')
757 if (gfc_next_char () == '.')
759 /* Matched ".le.". */
760 *result = INTRINSIC_LE_OS;
761 return MATCH_YES;
764 else if (ch == 't')
766 if (gfc_next_char () == '.')
768 /* Matched ".lt.". */
769 *result = INTRINSIC_LT_OS;
770 return MATCH_YES;
773 break;
775 case 'n':
776 ch = gfc_next_char ();
777 if (ch == 'e')
779 ch = gfc_next_char ();
780 if (ch == '.')
782 /* Matched ".ne.". */
783 *result = INTRINSIC_NE_OS;
784 return MATCH_YES;
786 else if (ch == 'q')
788 if (gfc_next_char () == 'v'
789 && gfc_next_char () == '.')
791 /* Matched ".neqv.". */
792 *result = INTRINSIC_NEQV;
793 return MATCH_YES;
797 else if (ch == 'o')
799 if (gfc_next_char () == 't'
800 && gfc_next_char () == '.')
802 /* Matched ".not.". */
803 *result = INTRINSIC_NOT;
804 return MATCH_YES;
807 break;
809 case 'o':
810 if (gfc_next_char () == 'r'
811 && gfc_next_char () == '.')
813 /* Matched ".or.". */
814 *result = INTRINSIC_OR;
815 return MATCH_YES;
817 break;
819 default:
820 break;
822 break;
824 default:
825 break;
828 gfc_current_locus = orig_loc;
829 return MATCH_NO;
833 /* Match a loop control phrase:
835 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
837 If the final integer expression is not present, a constant unity
838 expression is returned. We don't return MATCH_ERROR until after
839 the equals sign is seen. */
841 match
842 gfc_match_iterator (gfc_iterator *iter, int init_flag)
844 char name[GFC_MAX_SYMBOL_LEN + 1];
845 gfc_expr *var, *e1, *e2, *e3;
846 locus start;
847 match m;
849 /* Match the start of an iterator without affecting the symbol table. */
851 start = gfc_current_locus;
852 m = gfc_match (" %n =", name);
853 gfc_current_locus = start;
855 if (m != MATCH_YES)
856 return MATCH_NO;
858 m = gfc_match_variable (&var, 0);
859 if (m != MATCH_YES)
860 return MATCH_NO;
862 gfc_match_char ('=');
864 e1 = e2 = e3 = NULL;
866 if (var->ref != NULL)
868 gfc_error ("Loop variable at %C cannot be a sub-component");
869 goto cleanup;
872 if (var->symtree->n.sym->attr.intent == INTENT_IN)
874 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
875 var->symtree->n.sym->name);
876 goto cleanup;
879 var->symtree->n.sym->attr.implied_index = 1;
881 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
882 if (m == MATCH_NO)
883 goto syntax;
884 if (m == MATCH_ERROR)
885 goto cleanup;
887 if (gfc_match_char (',') != MATCH_YES)
888 goto syntax;
890 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
891 if (m == MATCH_NO)
892 goto syntax;
893 if (m == MATCH_ERROR)
894 goto cleanup;
896 if (gfc_match_char (',') != MATCH_YES)
898 e3 = gfc_int_expr (1);
899 goto done;
902 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
903 if (m == MATCH_ERROR)
904 goto cleanup;
905 if (m == MATCH_NO)
907 gfc_error ("Expected a step value in iterator at %C");
908 goto cleanup;
911 done:
912 iter->var = var;
913 iter->start = e1;
914 iter->end = e2;
915 iter->step = e3;
916 return MATCH_YES;
918 syntax:
919 gfc_error ("Syntax error in iterator at %C");
921 cleanup:
922 gfc_free_expr (e1);
923 gfc_free_expr (e2);
924 gfc_free_expr (e3);
926 return MATCH_ERROR;
930 /* Tries to match the next non-whitespace character on the input.
931 This subroutine does not return MATCH_ERROR. */
933 match
934 gfc_match_char (char c)
936 locus where;
938 where = gfc_current_locus;
939 gfc_gobble_whitespace ();
941 if (gfc_next_char () == c)
942 return MATCH_YES;
944 gfc_current_locus = where;
945 return MATCH_NO;
949 /* General purpose matching subroutine. The target string is a
950 scanf-like format string in which spaces correspond to arbitrary
951 whitespace (including no whitespace), characters correspond to
952 themselves. The %-codes are:
954 %% Literal percent sign
955 %e Expression, pointer to a pointer is set
956 %s Symbol, pointer to the symbol is set
957 %n Name, character buffer is set to name
958 %t Matches end of statement.
959 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
960 %l Matches a statement label
961 %v Matches a variable expression (an lvalue)
962 % Matches a required space (in free form) and optional spaces. */
964 match
965 gfc_match (const char *target, ...)
967 gfc_st_label **label;
968 int matches, *ip;
969 locus old_loc;
970 va_list argp;
971 char c, *np;
972 match m, n;
973 void **vp;
974 const char *p;
976 old_loc = gfc_current_locus;
977 va_start (argp, target);
978 m = MATCH_NO;
979 matches = 0;
980 p = target;
982 loop:
983 c = *p++;
984 switch (c)
986 case ' ':
987 gfc_gobble_whitespace ();
988 goto loop;
989 case '\0':
990 m = MATCH_YES;
991 break;
993 case '%':
994 c = *p++;
995 switch (c)
997 case 'e':
998 vp = va_arg (argp, void **);
999 n = gfc_match_expr ((gfc_expr **) vp);
1000 if (n != MATCH_YES)
1002 m = n;
1003 goto not_yes;
1006 matches++;
1007 goto loop;
1009 case 'v':
1010 vp = va_arg (argp, void **);
1011 n = gfc_match_variable ((gfc_expr **) vp, 0);
1012 if (n != MATCH_YES)
1014 m = n;
1015 goto not_yes;
1018 matches++;
1019 goto loop;
1021 case 's':
1022 vp = va_arg (argp, void **);
1023 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1024 if (n != MATCH_YES)
1026 m = n;
1027 goto not_yes;
1030 matches++;
1031 goto loop;
1033 case 'n':
1034 np = va_arg (argp, char *);
1035 n = gfc_match_name (np);
1036 if (n != MATCH_YES)
1038 m = n;
1039 goto not_yes;
1042 matches++;
1043 goto loop;
1045 case 'l':
1046 label = va_arg (argp, gfc_st_label **);
1047 n = gfc_match_st_label (label);
1048 if (n != MATCH_YES)
1050 m = n;
1051 goto not_yes;
1054 matches++;
1055 goto loop;
1057 case 'o':
1058 ip = va_arg (argp, int *);
1059 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1060 if (n != MATCH_YES)
1062 m = n;
1063 goto not_yes;
1066 matches++;
1067 goto loop;
1069 case 't':
1070 if (gfc_match_eos () != MATCH_YES)
1072 m = MATCH_NO;
1073 goto not_yes;
1075 goto loop;
1077 case ' ':
1078 if (gfc_match_space () == MATCH_YES)
1079 goto loop;
1080 m = MATCH_NO;
1081 goto not_yes;
1083 case '%':
1084 break; /* Fall through to character matcher. */
1086 default:
1087 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1090 default:
1091 if (c == gfc_next_char ())
1092 goto loop;
1093 break;
1096 not_yes:
1097 va_end (argp);
1099 if (m != MATCH_YES)
1101 /* Clean up after a failed match. */
1102 gfc_current_locus = old_loc;
1103 va_start (argp, target);
1105 p = target;
1106 for (; matches > 0; matches--)
1108 while (*p++ != '%');
1110 switch (*p++)
1112 case '%':
1113 matches++;
1114 break; /* Skip. */
1116 /* Matches that don't have to be undone */
1117 case 'o':
1118 case 'l':
1119 case 'n':
1120 case 's':
1121 (void) va_arg (argp, void **);
1122 break;
1124 case 'e':
1125 case 'v':
1126 vp = va_arg (argp, void **);
1127 gfc_free_expr (*vp);
1128 *vp = NULL;
1129 break;
1133 va_end (argp);
1136 return m;
1140 /*********************** Statement level matching **********************/
1142 /* Matches the start of a program unit, which is the program keyword
1143 followed by an obligatory symbol. */
1145 match
1146 gfc_match_program (void)
1148 gfc_symbol *sym;
1149 match m;
1151 m = gfc_match ("% %s%t", &sym);
1153 if (m == MATCH_NO)
1155 gfc_error ("Invalid form of PROGRAM statement at %C");
1156 m = MATCH_ERROR;
1159 if (m == MATCH_ERROR)
1160 return m;
1162 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1163 return MATCH_ERROR;
1165 gfc_new_block = sym;
1167 return MATCH_YES;
1171 /* Match a simple assignment statement. */
1173 match
1174 gfc_match_assignment (void)
1176 gfc_expr *lvalue, *rvalue;
1177 locus old_loc;
1178 match m;
1180 old_loc = gfc_current_locus;
1182 lvalue = NULL;
1183 m = gfc_match (" %v =", &lvalue);
1184 if (m != MATCH_YES)
1186 gfc_current_locus = old_loc;
1187 gfc_free_expr (lvalue);
1188 return MATCH_NO;
1191 if (lvalue->symtree->n.sym->attr.protected
1192 && lvalue->symtree->n.sym->attr.use_assoc)
1194 gfc_current_locus = old_loc;
1195 gfc_free_expr (lvalue);
1196 gfc_error ("Setting value of PROTECTED variable at %C");
1197 return MATCH_ERROR;
1200 rvalue = NULL;
1201 m = gfc_match (" %e%t", &rvalue);
1202 if (m != MATCH_YES)
1204 gfc_current_locus = old_loc;
1205 gfc_free_expr (lvalue);
1206 gfc_free_expr (rvalue);
1207 return m;
1210 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1212 new_st.op = EXEC_ASSIGN;
1213 new_st.expr = lvalue;
1214 new_st.expr2 = rvalue;
1216 gfc_check_do_variable (lvalue->symtree);
1218 return MATCH_YES;
1222 /* Match a pointer assignment statement. */
1224 match
1225 gfc_match_pointer_assignment (void)
1227 gfc_expr *lvalue, *rvalue;
1228 locus old_loc;
1229 match m;
1231 old_loc = gfc_current_locus;
1233 lvalue = rvalue = NULL;
1235 m = gfc_match (" %v =>", &lvalue);
1236 if (m != MATCH_YES)
1238 m = MATCH_NO;
1239 goto cleanup;
1242 m = gfc_match (" %e%t", &rvalue);
1243 if (m != MATCH_YES)
1244 goto cleanup;
1246 if (lvalue->symtree->n.sym->attr.protected
1247 && lvalue->symtree->n.sym->attr.use_assoc)
1249 gfc_error ("Assigning to a PROTECTED pointer at %C");
1250 m = MATCH_ERROR;
1251 goto cleanup;
1254 new_st.op = EXEC_POINTER_ASSIGN;
1255 new_st.expr = lvalue;
1256 new_st.expr2 = rvalue;
1258 return MATCH_YES;
1260 cleanup:
1261 gfc_current_locus = old_loc;
1262 gfc_free_expr (lvalue);
1263 gfc_free_expr (rvalue);
1264 return m;
1268 /* We try to match an easy arithmetic IF statement. This only happens
1269 when just after having encountered a simple IF statement. This code
1270 is really duplicate with parts of the gfc_match_if code, but this is
1271 *much* easier. */
1273 static match
1274 match_arithmetic_if (void)
1276 gfc_st_label *l1, *l2, *l3;
1277 gfc_expr *expr;
1278 match m;
1280 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1281 if (m != MATCH_YES)
1282 return m;
1284 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1285 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1286 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1288 gfc_free_expr (expr);
1289 return MATCH_ERROR;
1292 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF statement "
1293 "at %C") == FAILURE)
1294 return MATCH_ERROR;
1296 new_st.op = EXEC_ARITHMETIC_IF;
1297 new_st.expr = expr;
1298 new_st.label = l1;
1299 new_st.label2 = l2;
1300 new_st.label3 = l3;
1302 return MATCH_YES;
1306 /* The IF statement is a bit of a pain. First of all, there are three
1307 forms of it, the simple IF, the IF that starts a block and the
1308 arithmetic IF.
1310 There is a problem with the simple IF and that is the fact that we
1311 only have a single level of undo information on symbols. What this
1312 means is for a simple IF, we must re-match the whole IF statement
1313 multiple times in order to guarantee that the symbol table ends up
1314 in the proper state. */
1316 static match match_simple_forall (void);
1317 static match match_simple_where (void);
1319 match
1320 gfc_match_if (gfc_statement *if_type)
1322 gfc_expr *expr;
1323 gfc_st_label *l1, *l2, *l3;
1324 locus old_loc;
1325 gfc_code *p;
1326 match m, n;
1328 n = gfc_match_label ();
1329 if (n == MATCH_ERROR)
1330 return n;
1332 old_loc = gfc_current_locus;
1334 m = gfc_match (" if ( %e", &expr);
1335 if (m != MATCH_YES)
1336 return m;
1338 if (gfc_match_char (')') != MATCH_YES)
1340 gfc_error ("Syntax error in IF-expression at %C");
1341 gfc_free_expr (expr);
1342 return MATCH_ERROR;
1345 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1347 if (m == MATCH_YES)
1349 if (n == MATCH_YES)
1351 gfc_error ("Block label not appropriate for arithmetic IF "
1352 "statement at %C");
1353 gfc_free_expr (expr);
1354 return MATCH_ERROR;
1357 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1358 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1359 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1361 gfc_free_expr (expr);
1362 return MATCH_ERROR;
1365 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF "
1366 "statement at %C") == FAILURE)
1367 return MATCH_ERROR;
1369 new_st.op = EXEC_ARITHMETIC_IF;
1370 new_st.expr = expr;
1371 new_st.label = l1;
1372 new_st.label2 = l2;
1373 new_st.label3 = l3;
1375 *if_type = ST_ARITHMETIC_IF;
1376 return MATCH_YES;
1379 if (gfc_match (" then%t") == MATCH_YES)
1381 new_st.op = EXEC_IF;
1382 new_st.expr = expr;
1383 *if_type = ST_IF_BLOCK;
1384 return MATCH_YES;
1387 if (n == MATCH_YES)
1389 gfc_error ("Block label is not appropriate IF statement at %C");
1390 gfc_free_expr (expr);
1391 return MATCH_ERROR;
1394 /* At this point the only thing left is a simple IF statement. At
1395 this point, n has to be MATCH_NO, so we don't have to worry about
1396 re-matching a block label. From what we've got so far, try
1397 matching an assignment. */
1399 *if_type = ST_SIMPLE_IF;
1401 m = gfc_match_assignment ();
1402 if (m == MATCH_YES)
1403 goto got_match;
1405 gfc_free_expr (expr);
1406 gfc_undo_symbols ();
1407 gfc_current_locus = old_loc;
1409 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1410 assignment was found. For MATCH_NO, continue to call the various
1411 matchers. */
1412 if (m == MATCH_ERROR)
1413 return MATCH_ERROR;
1415 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1417 m = gfc_match_pointer_assignment ();
1418 if (m == MATCH_YES)
1419 goto got_match;
1421 gfc_free_expr (expr);
1422 gfc_undo_symbols ();
1423 gfc_current_locus = old_loc;
1425 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1427 /* Look at the next keyword to see which matcher to call. Matching
1428 the keyword doesn't affect the symbol table, so we don't have to
1429 restore between tries. */
1431 #define match(string, subr, statement) \
1432 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1434 gfc_clear_error ();
1436 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1437 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1438 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1439 match ("call", gfc_match_call, ST_CALL)
1440 match ("close", gfc_match_close, ST_CLOSE)
1441 match ("continue", gfc_match_continue, ST_CONTINUE)
1442 match ("cycle", gfc_match_cycle, ST_CYCLE)
1443 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1444 match ("end file", gfc_match_endfile, ST_END_FILE)
1445 match ("exit", gfc_match_exit, ST_EXIT)
1446 match ("flush", gfc_match_flush, ST_FLUSH)
1447 match ("forall", match_simple_forall, ST_FORALL)
1448 match ("go to", gfc_match_goto, ST_GOTO)
1449 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1450 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1451 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1452 match ("open", gfc_match_open, ST_OPEN)
1453 match ("pause", gfc_match_pause, ST_NONE)
1454 match ("print", gfc_match_print, ST_WRITE)
1455 match ("read", gfc_match_read, ST_READ)
1456 match ("return", gfc_match_return, ST_RETURN)
1457 match ("rewind", gfc_match_rewind, ST_REWIND)
1458 match ("stop", gfc_match_stop, ST_STOP)
1459 match ("where", match_simple_where, ST_WHERE)
1460 match ("write", gfc_match_write, ST_WRITE)
1462 /* The gfc_match_assignment() above may have returned a MATCH_NO
1463 where the assignment was to a named constant. Check that
1464 special case here. */
1465 m = gfc_match_assignment ();
1466 if (m == MATCH_NO)
1468 gfc_error ("Cannot assign to a named constant at %C");
1469 gfc_free_expr (expr);
1470 gfc_undo_symbols ();
1471 gfc_current_locus = old_loc;
1472 return MATCH_ERROR;
1475 /* All else has failed, so give up. See if any of the matchers has
1476 stored an error message of some sort. */
1477 if (gfc_error_check () == 0)
1478 gfc_error ("Unclassifiable statement in IF-clause at %C");
1480 gfc_free_expr (expr);
1481 return MATCH_ERROR;
1483 got_match:
1484 if (m == MATCH_NO)
1485 gfc_error ("Syntax error in IF-clause at %C");
1486 if (m != MATCH_YES)
1488 gfc_free_expr (expr);
1489 return MATCH_ERROR;
1492 /* At this point, we've matched the single IF and the action clause
1493 is in new_st. Rearrange things so that the IF statement appears
1494 in new_st. */
1496 p = gfc_get_code ();
1497 p->next = gfc_get_code ();
1498 *p->next = new_st;
1499 p->next->loc = gfc_current_locus;
1501 p->expr = expr;
1502 p->op = EXEC_IF;
1504 gfc_clear_new_st ();
1506 new_st.op = EXEC_IF;
1507 new_st.block = p;
1509 return MATCH_YES;
1512 #undef match
1515 /* Match an ELSE statement. */
1517 match
1518 gfc_match_else (void)
1520 char name[GFC_MAX_SYMBOL_LEN + 1];
1522 if (gfc_match_eos () == MATCH_YES)
1523 return MATCH_YES;
1525 if (gfc_match_name (name) != MATCH_YES
1526 || gfc_current_block () == NULL
1527 || gfc_match_eos () != MATCH_YES)
1529 gfc_error ("Unexpected junk after ELSE statement at %C");
1530 return MATCH_ERROR;
1533 if (strcmp (name, gfc_current_block ()->name) != 0)
1535 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1536 name, gfc_current_block ()->name);
1537 return MATCH_ERROR;
1540 return MATCH_YES;
1544 /* Match an ELSE IF statement. */
1546 match
1547 gfc_match_elseif (void)
1549 char name[GFC_MAX_SYMBOL_LEN + 1];
1550 gfc_expr *expr;
1551 match m;
1553 m = gfc_match (" ( %e ) then", &expr);
1554 if (m != MATCH_YES)
1555 return m;
1557 if (gfc_match_eos () == MATCH_YES)
1558 goto done;
1560 if (gfc_match_name (name) != MATCH_YES
1561 || gfc_current_block () == NULL
1562 || gfc_match_eos () != MATCH_YES)
1564 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1565 goto cleanup;
1568 if (strcmp (name, gfc_current_block ()->name) != 0)
1570 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1571 name, gfc_current_block ()->name);
1572 goto cleanup;
1575 done:
1576 new_st.op = EXEC_IF;
1577 new_st.expr = expr;
1578 return MATCH_YES;
1580 cleanup:
1581 gfc_free_expr (expr);
1582 return MATCH_ERROR;
1586 /* Free a gfc_iterator structure. */
1588 void
1589 gfc_free_iterator (gfc_iterator *iter, int flag)
1592 if (iter == NULL)
1593 return;
1595 gfc_free_expr (iter->var);
1596 gfc_free_expr (iter->start);
1597 gfc_free_expr (iter->end);
1598 gfc_free_expr (iter->step);
1600 if (flag)
1601 gfc_free (iter);
1605 /* Match a DO statement. */
1607 match
1608 gfc_match_do (void)
1610 gfc_iterator iter, *ip;
1611 locus old_loc;
1612 gfc_st_label *label;
1613 match m;
1615 old_loc = gfc_current_locus;
1617 label = NULL;
1618 iter.var = iter.start = iter.end = iter.step = NULL;
1620 m = gfc_match_label ();
1621 if (m == MATCH_ERROR)
1622 return m;
1624 if (gfc_match (" do") != MATCH_YES)
1625 return MATCH_NO;
1627 m = gfc_match_st_label (&label);
1628 if (m == MATCH_ERROR)
1629 goto cleanup;
1631 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1633 if (gfc_match_eos () == MATCH_YES)
1635 iter.end = gfc_logical_expr (1, NULL);
1636 new_st.op = EXEC_DO_WHILE;
1637 goto done;
1640 /* Match an optional comma, if no comma is found, a space is obligatory. */
1641 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1642 return MATCH_NO;
1644 /* See if we have a DO WHILE. */
1645 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1647 new_st.op = EXEC_DO_WHILE;
1648 goto done;
1651 /* The abortive DO WHILE may have done something to the symbol
1652 table, so we start over. */
1653 gfc_undo_symbols ();
1654 gfc_current_locus = old_loc;
1656 gfc_match_label (); /* This won't error. */
1657 gfc_match (" do "); /* This will work. */
1659 gfc_match_st_label (&label); /* Can't error out. */
1660 gfc_match_char (','); /* Optional comma. */
1662 m = gfc_match_iterator (&iter, 0);
1663 if (m == MATCH_NO)
1664 return MATCH_NO;
1665 if (m == MATCH_ERROR)
1666 goto cleanup;
1668 iter.var->symtree->n.sym->attr.implied_index = 0;
1669 gfc_check_do_variable (iter.var->symtree);
1671 if (gfc_match_eos () != MATCH_YES)
1673 gfc_syntax_error (ST_DO);
1674 goto cleanup;
1677 new_st.op = EXEC_DO;
1679 done:
1680 if (label != NULL
1681 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1682 goto cleanup;
1684 new_st.label = label;
1686 if (new_st.op == EXEC_DO_WHILE)
1687 new_st.expr = iter.end;
1688 else
1690 new_st.ext.iterator = ip = gfc_get_iterator ();
1691 *ip = iter;
1694 return MATCH_YES;
1696 cleanup:
1697 gfc_free_iterator (&iter, 0);
1699 return MATCH_ERROR;
1703 /* Match an EXIT or CYCLE statement. */
1705 static match
1706 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1708 gfc_state_data *p, *o;
1709 gfc_symbol *sym;
1710 match m;
1712 if (gfc_match_eos () == MATCH_YES)
1713 sym = NULL;
1714 else
1716 m = gfc_match ("% %s%t", &sym);
1717 if (m == MATCH_ERROR)
1718 return MATCH_ERROR;
1719 if (m == MATCH_NO)
1721 gfc_syntax_error (st);
1722 return MATCH_ERROR;
1725 if (sym->attr.flavor != FL_LABEL)
1727 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1728 sym->name, gfc_ascii_statement (st));
1729 return MATCH_ERROR;
1733 /* Find the loop mentioned specified by the label (or lack of a label). */
1734 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1735 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1736 break;
1737 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1738 o = p;
1740 if (p == NULL)
1742 if (sym == NULL)
1743 gfc_error ("%s statement at %C is not within a loop",
1744 gfc_ascii_statement (st));
1745 else
1746 gfc_error ("%s statement at %C is not within loop '%s'",
1747 gfc_ascii_statement (st), sym->name);
1749 return MATCH_ERROR;
1752 if (o != NULL)
1754 gfc_error ("%s statement at %C leaving OpenMP structured block",
1755 gfc_ascii_statement (st));
1756 return MATCH_ERROR;
1758 else if (st == ST_EXIT
1759 && p->previous != NULL
1760 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1761 && (p->previous->head->op == EXEC_OMP_DO
1762 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1764 gcc_assert (p->previous->head->next != NULL);
1765 gcc_assert (p->previous->head->next->op == EXEC_DO
1766 || p->previous->head->next->op == EXEC_DO_WHILE);
1767 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1768 return MATCH_ERROR;
1771 /* Save the first statement in the loop - needed by the backend. */
1772 new_st.ext.whichloop = p->head;
1774 new_st.op = op;
1776 return MATCH_YES;
1780 /* Match the EXIT statement. */
1782 match
1783 gfc_match_exit (void)
1785 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1789 /* Match the CYCLE statement. */
1791 match
1792 gfc_match_cycle (void)
1794 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1798 /* Match a number or character constant after a STOP or PAUSE statement. */
1800 static match
1801 gfc_match_stopcode (gfc_statement st)
1803 int stop_code;
1804 gfc_expr *e;
1805 match m;
1806 int cnt;
1808 stop_code = -1;
1809 e = NULL;
1811 if (gfc_match_eos () != MATCH_YES)
1813 m = gfc_match_small_literal_int (&stop_code, &cnt);
1814 if (m == MATCH_ERROR)
1815 goto cleanup;
1817 if (m == MATCH_YES && cnt > 5)
1819 gfc_error ("Too many digits in STOP code at %C");
1820 goto cleanup;
1823 if (m == MATCH_NO)
1825 /* Try a character constant. */
1826 m = gfc_match_expr (&e);
1827 if (m == MATCH_ERROR)
1828 goto cleanup;
1829 if (m == MATCH_NO)
1830 goto syntax;
1831 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1832 goto syntax;
1835 if (gfc_match_eos () != MATCH_YES)
1836 goto syntax;
1839 if (gfc_pure (NULL))
1841 gfc_error ("%s statement not allowed in PURE procedure at %C",
1842 gfc_ascii_statement (st));
1843 goto cleanup;
1846 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1847 new_st.expr = e;
1848 new_st.ext.stop_code = stop_code;
1850 return MATCH_YES;
1852 syntax:
1853 gfc_syntax_error (st);
1855 cleanup:
1857 gfc_free_expr (e);
1858 return MATCH_ERROR;
1862 /* Match the (deprecated) PAUSE statement. */
1864 match
1865 gfc_match_pause (void)
1867 match m;
1869 m = gfc_match_stopcode (ST_PAUSE);
1870 if (m == MATCH_YES)
1872 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
1873 " at %C")
1874 == FAILURE)
1875 m = MATCH_ERROR;
1877 return m;
1881 /* Match the STOP statement. */
1883 match
1884 gfc_match_stop (void)
1886 return gfc_match_stopcode (ST_STOP);
1890 /* Match a CONTINUE statement. */
1892 match
1893 gfc_match_continue (void)
1895 if (gfc_match_eos () != MATCH_YES)
1897 gfc_syntax_error (ST_CONTINUE);
1898 return MATCH_ERROR;
1901 new_st.op = EXEC_CONTINUE;
1902 return MATCH_YES;
1906 /* Match the (deprecated) ASSIGN statement. */
1908 match
1909 gfc_match_assign (void)
1911 gfc_expr *expr;
1912 gfc_st_label *label;
1914 if (gfc_match (" %l", &label) == MATCH_YES)
1916 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1917 return MATCH_ERROR;
1918 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1920 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
1921 "statement at %C")
1922 == FAILURE)
1923 return MATCH_ERROR;
1925 expr->symtree->n.sym->attr.assign = 1;
1927 new_st.op = EXEC_LABEL_ASSIGN;
1928 new_st.label = label;
1929 new_st.expr = expr;
1930 return MATCH_YES;
1933 return MATCH_NO;
1937 /* Match the GO TO statement. As a computed GOTO statement is
1938 matched, it is transformed into an equivalent SELECT block. No
1939 tree is necessary, and the resulting jumps-to-jumps are
1940 specifically optimized away by the back end. */
1942 match
1943 gfc_match_goto (void)
1945 gfc_code *head, *tail;
1946 gfc_expr *expr;
1947 gfc_case *cp;
1948 gfc_st_label *label;
1949 int i;
1950 match m;
1952 if (gfc_match (" %l%t", &label) == MATCH_YES)
1954 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1955 return MATCH_ERROR;
1957 new_st.op = EXEC_GOTO;
1958 new_st.label = label;
1959 return MATCH_YES;
1962 /* The assigned GO TO statement. */
1964 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1966 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
1967 "statement at %C")
1968 == FAILURE)
1969 return MATCH_ERROR;
1971 new_st.op = EXEC_GOTO;
1972 new_st.expr = expr;
1974 if (gfc_match_eos () == MATCH_YES)
1975 return MATCH_YES;
1977 /* Match label list. */
1978 gfc_match_char (',');
1979 if (gfc_match_char ('(') != MATCH_YES)
1981 gfc_syntax_error (ST_GOTO);
1982 return MATCH_ERROR;
1984 head = tail = NULL;
1988 m = gfc_match_st_label (&label);
1989 if (m != MATCH_YES)
1990 goto syntax;
1992 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1993 goto cleanup;
1995 if (head == NULL)
1996 head = tail = gfc_get_code ();
1997 else
1999 tail->block = gfc_get_code ();
2000 tail = tail->block;
2003 tail->label = label;
2004 tail->op = EXEC_GOTO;
2006 while (gfc_match_char (',') == MATCH_YES);
2008 if (gfc_match (")%t") != MATCH_YES)
2009 goto syntax;
2011 if (head == NULL)
2013 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2014 goto syntax;
2016 new_st.block = head;
2018 return MATCH_YES;
2021 /* Last chance is a computed GO TO statement. */
2022 if (gfc_match_char ('(') != MATCH_YES)
2024 gfc_syntax_error (ST_GOTO);
2025 return MATCH_ERROR;
2028 head = tail = NULL;
2029 i = 1;
2033 m = gfc_match_st_label (&label);
2034 if (m != MATCH_YES)
2035 goto syntax;
2037 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2038 goto cleanup;
2040 if (head == NULL)
2041 head = tail = gfc_get_code ();
2042 else
2044 tail->block = gfc_get_code ();
2045 tail = tail->block;
2048 cp = gfc_get_case ();
2049 cp->low = cp->high = gfc_int_expr (i++);
2051 tail->op = EXEC_SELECT;
2052 tail->ext.case_list = cp;
2054 tail->next = gfc_get_code ();
2055 tail->next->op = EXEC_GOTO;
2056 tail->next->label = label;
2058 while (gfc_match_char (',') == MATCH_YES);
2060 if (gfc_match_char (')') != MATCH_YES)
2061 goto syntax;
2063 if (head == NULL)
2065 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2066 goto syntax;
2069 /* Get the rest of the statement. */
2070 gfc_match_char (',');
2072 if (gfc_match (" %e%t", &expr) != MATCH_YES)
2073 goto syntax;
2075 /* At this point, a computed GOTO has been fully matched and an
2076 equivalent SELECT statement constructed. */
2078 new_st.op = EXEC_SELECT;
2079 new_st.expr = NULL;
2081 /* Hack: For a "real" SELECT, the expression is in expr. We put
2082 it in expr2 so we can distinguish then and produce the correct
2083 diagnostics. */
2084 new_st.expr2 = expr;
2085 new_st.block = head;
2086 return MATCH_YES;
2088 syntax:
2089 gfc_syntax_error (ST_GOTO);
2090 cleanup:
2091 gfc_free_statements (head);
2092 return MATCH_ERROR;
2096 /* Frees a list of gfc_alloc structures. */
2098 void
2099 gfc_free_alloc_list (gfc_alloc *p)
2101 gfc_alloc *q;
2103 for (; p; p = q)
2105 q = p->next;
2106 gfc_free_expr (p->expr);
2107 gfc_free (p);
2112 /* Match an ALLOCATE statement. */
2114 match
2115 gfc_match_allocate (void)
2117 gfc_alloc *head, *tail;
2118 gfc_expr *stat;
2119 match m;
2121 head = tail = NULL;
2122 stat = NULL;
2124 if (gfc_match_char ('(') != MATCH_YES)
2125 goto syntax;
2127 for (;;)
2129 if (head == NULL)
2130 head = tail = gfc_get_alloc ();
2131 else
2133 tail->next = gfc_get_alloc ();
2134 tail = tail->next;
2137 m = gfc_match_variable (&tail->expr, 0);
2138 if (m == MATCH_NO)
2139 goto syntax;
2140 if (m == MATCH_ERROR)
2141 goto cleanup;
2143 if (gfc_check_do_variable (tail->expr->symtree))
2144 goto cleanup;
2146 if (gfc_pure (NULL)
2147 && gfc_impure_variable (tail->expr->symtree->n.sym))
2149 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
2150 "PURE procedure");
2151 goto cleanup;
2154 if (tail->expr->ts.type == BT_DERIVED)
2155 tail->expr->ts.derived = gfc_use_derived (tail->expr->ts.derived);
2157 if (gfc_match_char (',') != MATCH_YES)
2158 break;
2160 m = gfc_match (" stat = %v", &stat);
2161 if (m == MATCH_ERROR)
2162 goto cleanup;
2163 if (m == MATCH_YES)
2164 break;
2167 if (stat != NULL)
2169 bool is_variable;
2171 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
2173 gfc_error ("STAT variable '%s' of ALLOCATE statement at %C cannot "
2174 "be INTENT(IN)", stat->symtree->n.sym->name);
2175 goto cleanup;
2178 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
2180 gfc_error ("Illegal STAT variable in ALLOCATE statement at %C "
2181 "for a PURE procedure");
2182 goto cleanup;
2185 is_variable = false;
2186 if (stat->symtree->n.sym->attr.flavor == FL_VARIABLE)
2187 is_variable = true;
2188 else if (stat->symtree->n.sym->attr.function
2189 && stat->symtree->n.sym->result == stat->symtree->n.sym
2190 && (gfc_current_ns->proc_name == stat->symtree->n.sym
2191 || (gfc_current_ns->parent
2192 && gfc_current_ns->parent->proc_name
2193 == stat->symtree->n.sym)))
2194 is_variable = true;
2195 else if (gfc_current_ns->entries
2196 && stat->symtree->n.sym->result == stat->symtree->n.sym)
2198 gfc_entry_list *el;
2199 for (el = gfc_current_ns->entries; el; el = el->next)
2200 if (el->sym == stat->symtree->n.sym)
2202 is_variable = true;
2205 else if (gfc_current_ns->parent && gfc_current_ns->parent->entries
2206 && stat->symtree->n.sym->result == stat->symtree->n.sym)
2208 gfc_entry_list *el;
2209 for (el = gfc_current_ns->parent->entries; el; el = el->next)
2210 if (el->sym == stat->symtree->n.sym)
2212 is_variable = true;
2216 if (!is_variable)
2218 gfc_error ("STAT expression at %C must be a variable");
2219 goto cleanup;
2222 gfc_check_do_variable(stat->symtree);
2225 if (gfc_match (" )%t") != MATCH_YES)
2226 goto syntax;
2228 new_st.op = EXEC_ALLOCATE;
2229 new_st.expr = stat;
2230 new_st.ext.alloc_list = head;
2232 return MATCH_YES;
2234 syntax:
2235 gfc_syntax_error (ST_ALLOCATE);
2237 cleanup:
2238 gfc_free_expr (stat);
2239 gfc_free_alloc_list (head);
2240 return MATCH_ERROR;
2244 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
2245 a set of pointer assignments to intrinsic NULL(). */
2247 match
2248 gfc_match_nullify (void)
2250 gfc_code *tail;
2251 gfc_expr *e, *p;
2252 match m;
2254 tail = NULL;
2256 if (gfc_match_char ('(') != MATCH_YES)
2257 goto syntax;
2259 for (;;)
2261 m = gfc_match_variable (&p, 0);
2262 if (m == MATCH_ERROR)
2263 goto cleanup;
2264 if (m == MATCH_NO)
2265 goto syntax;
2267 if (gfc_check_do_variable (p->symtree))
2268 goto cleanup;
2270 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
2272 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
2273 goto cleanup;
2276 /* build ' => NULL() '. */
2277 e = gfc_get_expr ();
2278 e->where = gfc_current_locus;
2279 e->expr_type = EXPR_NULL;
2280 e->ts.type = BT_UNKNOWN;
2282 /* Chain to list. */
2283 if (tail == NULL)
2284 tail = &new_st;
2285 else
2287 tail->next = gfc_get_code ();
2288 tail = tail->next;
2291 tail->op = EXEC_POINTER_ASSIGN;
2292 tail->expr = p;
2293 tail->expr2 = e;
2295 if (gfc_match (" )%t") == MATCH_YES)
2296 break;
2297 if (gfc_match_char (',') != MATCH_YES)
2298 goto syntax;
2301 return MATCH_YES;
2303 syntax:
2304 gfc_syntax_error (ST_NULLIFY);
2306 cleanup:
2307 gfc_free_statements (new_st.next);
2308 return MATCH_ERROR;
2312 /* Match a DEALLOCATE statement. */
2314 match
2315 gfc_match_deallocate (void)
2317 gfc_alloc *head, *tail;
2318 gfc_expr *stat;
2319 match m;
2321 head = tail = NULL;
2322 stat = NULL;
2324 if (gfc_match_char ('(') != MATCH_YES)
2325 goto syntax;
2327 for (;;)
2329 if (head == NULL)
2330 head = tail = gfc_get_alloc ();
2331 else
2333 tail->next = gfc_get_alloc ();
2334 tail = tail->next;
2337 m = gfc_match_variable (&tail->expr, 0);
2338 if (m == MATCH_ERROR)
2339 goto cleanup;
2340 if (m == MATCH_NO)
2341 goto syntax;
2343 if (gfc_check_do_variable (tail->expr->symtree))
2344 goto cleanup;
2346 if (gfc_pure (NULL)
2347 && gfc_impure_variable (tail->expr->symtree->n.sym))
2349 gfc_error ("Illegal deallocate-expression in DEALLOCATE at %C "
2350 "for a PURE procedure");
2351 goto cleanup;
2354 if (gfc_match_char (',') != MATCH_YES)
2355 break;
2357 m = gfc_match (" stat = %v", &stat);
2358 if (m == MATCH_ERROR)
2359 goto cleanup;
2360 if (m == MATCH_YES)
2361 break;
2364 if (stat != NULL)
2366 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
2368 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
2369 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
2370 goto cleanup;
2373 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
2375 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
2376 "for a PURE procedure");
2377 goto cleanup;
2380 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
2382 gfc_error ("STAT expression at %C must be a variable");
2383 goto cleanup;
2386 gfc_check_do_variable(stat->symtree);
2389 if (gfc_match (" )%t") != MATCH_YES)
2390 goto syntax;
2392 new_st.op = EXEC_DEALLOCATE;
2393 new_st.expr = stat;
2394 new_st.ext.alloc_list = head;
2396 return MATCH_YES;
2398 syntax:
2399 gfc_syntax_error (ST_DEALLOCATE);
2401 cleanup:
2402 gfc_free_expr (stat);
2403 gfc_free_alloc_list (head);
2404 return MATCH_ERROR;
2408 /* Match a RETURN statement. */
2410 match
2411 gfc_match_return (void)
2413 gfc_expr *e;
2414 match m;
2415 gfc_compile_state s;
2416 int c;
2418 e = NULL;
2419 if (gfc_match_eos () == MATCH_YES)
2420 goto done;
2422 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2424 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2425 "a SUBROUTINE");
2426 goto cleanup;
2429 if (gfc_current_form == FORM_FREE)
2431 /* The following are valid, so we can't require a blank after the
2432 RETURN keyword:
2433 return+1
2434 return(1) */
2435 c = gfc_peek_char ();
2436 if (ISALPHA (c) || ISDIGIT (c))
2437 return MATCH_NO;
2440 m = gfc_match (" %e%t", &e);
2441 if (m == MATCH_YES)
2442 goto done;
2443 if (m == MATCH_ERROR)
2444 goto cleanup;
2446 gfc_syntax_error (ST_RETURN);
2448 cleanup:
2449 gfc_free_expr (e);
2450 return MATCH_ERROR;
2452 done:
2453 gfc_enclosing_unit (&s);
2454 if (s == COMP_PROGRAM
2455 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2456 "main program at %C") == FAILURE)
2457 return MATCH_ERROR;
2459 new_st.op = EXEC_RETURN;
2460 new_st.expr = e;
2462 return MATCH_YES;
2466 /* Match a CALL statement. The tricky part here are possible
2467 alternate return specifiers. We handle these by having all
2468 "subroutines" actually return an integer via a register that gives
2469 the return number. If the call specifies alternate returns, we
2470 generate code for a SELECT statement whose case clauses contain
2471 GOTOs to the various labels. */
2473 match
2474 gfc_match_call (void)
2476 char name[GFC_MAX_SYMBOL_LEN + 1];
2477 gfc_actual_arglist *a, *arglist;
2478 gfc_case *new_case;
2479 gfc_symbol *sym;
2480 gfc_symtree *st;
2481 gfc_code *c;
2482 match m;
2483 int i;
2485 arglist = NULL;
2487 m = gfc_match ("% %n", name);
2488 if (m == MATCH_NO)
2489 goto syntax;
2490 if (m != MATCH_YES)
2491 return m;
2493 if (gfc_get_ha_sym_tree (name, &st))
2494 return MATCH_ERROR;
2496 sym = st->n.sym;
2498 /* If it does not seem to be callable... */
2499 if (!sym->attr.generic
2500 && !sym->attr.subroutine)
2502 if (!(sym->attr.external && !sym->attr.referenced))
2504 /* ...create a symbol in this scope... */
2505 if (sym->ns != gfc_current_ns
2506 && gfc_get_sym_tree (name, NULL, &st) == 1)
2507 return MATCH_ERROR;
2509 if (sym != st->n.sym)
2510 sym = st->n.sym;
2513 /* ...and then to try to make the symbol into a subroutine. */
2514 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2515 return MATCH_ERROR;
2518 gfc_set_sym_referenced (sym);
2520 if (gfc_match_eos () != MATCH_YES)
2522 m = gfc_match_actual_arglist (1, &arglist);
2523 if (m == MATCH_NO)
2524 goto syntax;
2525 if (m == MATCH_ERROR)
2526 goto cleanup;
2528 if (gfc_match_eos () != MATCH_YES)
2529 goto syntax;
2532 /* If any alternate return labels were found, construct a SELECT
2533 statement that will jump to the right place. */
2535 i = 0;
2536 for (a = arglist; a; a = a->next)
2537 if (a->expr == NULL)
2538 i = 1;
2540 if (i)
2542 gfc_symtree *select_st;
2543 gfc_symbol *select_sym;
2544 char name[GFC_MAX_SYMBOL_LEN + 1];
2546 new_st.next = c = gfc_get_code ();
2547 c->op = EXEC_SELECT;
2548 sprintf (name, "_result_%s", sym->name);
2549 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
2551 select_sym = select_st->n.sym;
2552 select_sym->ts.type = BT_INTEGER;
2553 select_sym->ts.kind = gfc_default_integer_kind;
2554 gfc_set_sym_referenced (select_sym);
2555 c->expr = gfc_get_expr ();
2556 c->expr->expr_type = EXPR_VARIABLE;
2557 c->expr->symtree = select_st;
2558 c->expr->ts = select_sym->ts;
2559 c->expr->where = gfc_current_locus;
2561 i = 0;
2562 for (a = arglist; a; a = a->next)
2564 if (a->expr != NULL)
2565 continue;
2567 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2568 continue;
2570 i++;
2572 c->block = gfc_get_code ();
2573 c = c->block;
2574 c->op = EXEC_SELECT;
2576 new_case = gfc_get_case ();
2577 new_case->high = new_case->low = gfc_int_expr (i);
2578 c->ext.case_list = new_case;
2580 c->next = gfc_get_code ();
2581 c->next->op = EXEC_GOTO;
2582 c->next->label = a->label;
2586 new_st.op = EXEC_CALL;
2587 new_st.symtree = st;
2588 new_st.ext.actual = arglist;
2590 return MATCH_YES;
2592 syntax:
2593 gfc_syntax_error (ST_CALL);
2595 cleanup:
2596 gfc_free_actual_arglist (arglist);
2597 return MATCH_ERROR;
2601 /* Given a name, return a pointer to the common head structure,
2602 creating it if it does not exist. If FROM_MODULE is nonzero, we
2603 mangle the name so that it doesn't interfere with commons defined
2604 in the using namespace.
2605 TODO: Add to global symbol tree. */
2607 gfc_common_head *
2608 gfc_get_common (const char *name, int from_module)
2610 gfc_symtree *st;
2611 static int serial = 0;
2612 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
2614 if (from_module)
2616 /* A use associated common block is only needed to correctly layout
2617 the variables it contains. */
2618 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2619 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2621 else
2623 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2625 if (st == NULL)
2626 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2629 if (st->n.common == NULL)
2631 st->n.common = gfc_get_common_head ();
2632 st->n.common->where = gfc_current_locus;
2633 strcpy (st->n.common->name, name);
2636 return st->n.common;
2640 /* Match a common block name. */
2642 match match_common_name (char *name)
2644 match m;
2646 if (gfc_match_char ('/') == MATCH_NO)
2648 name[0] = '\0';
2649 return MATCH_YES;
2652 if (gfc_match_char ('/') == MATCH_YES)
2654 name[0] = '\0';
2655 return MATCH_YES;
2658 m = gfc_match_name (name);
2660 if (m == MATCH_ERROR)
2661 return MATCH_ERROR;
2662 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2663 return MATCH_YES;
2665 gfc_error ("Syntax error in common block name at %C");
2666 return MATCH_ERROR;
2670 /* Match a COMMON statement. */
2672 match
2673 gfc_match_common (void)
2675 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2676 char name[GFC_MAX_SYMBOL_LEN + 1];
2677 gfc_common_head *t;
2678 gfc_array_spec *as;
2679 gfc_equiv *e1, *e2;
2680 match m;
2681 gfc_gsymbol *gsym;
2683 old_blank_common = gfc_current_ns->blank_common.head;
2684 if (old_blank_common)
2686 while (old_blank_common->common_next)
2687 old_blank_common = old_blank_common->common_next;
2690 as = NULL;
2692 for (;;)
2694 m = match_common_name (name);
2695 if (m == MATCH_ERROR)
2696 goto cleanup;
2698 gsym = gfc_get_gsymbol (name);
2699 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2701 gfc_error ("Symbol '%s' at %C is already an external symbol that "
2702 "is not COMMON", name);
2703 goto cleanup;
2706 if (gsym->type == GSYM_UNKNOWN)
2708 gsym->type = GSYM_COMMON;
2709 gsym->where = gfc_current_locus;
2710 gsym->defined = 1;
2713 gsym->used = 1;
2715 if (name[0] == '\0')
2717 if (gfc_current_ns->is_block_data)
2719 gfc_warning ("BLOCK DATA unit cannot contain blank COMMON "
2720 "at %C");
2722 t = &gfc_current_ns->blank_common;
2723 if (t->head == NULL)
2724 t->where = gfc_current_locus;
2726 else
2728 t = gfc_get_common (name, 0);
2730 head = &t->head;
2732 if (*head == NULL)
2733 tail = NULL;
2734 else
2736 tail = *head;
2737 while (tail->common_next)
2738 tail = tail->common_next;
2741 /* Grab the list of symbols. */
2742 for (;;)
2744 m = gfc_match_symbol (&sym, 0);
2745 if (m == MATCH_ERROR)
2746 goto cleanup;
2747 if (m == MATCH_NO)
2748 goto syntax;
2750 /* Store a ref to the common block for error checking. */
2751 sym->common_block = t;
2753 /* See if we know the current common block is bind(c), and if
2754 so, then see if we can check if the symbol is (which it'll
2755 need to be). This can happen if the bind(c) attr stmt was
2756 applied to the common block, and the variable(s) already
2757 defined, before declaring the common block. */
2758 if (t->is_bind_c == 1)
2760 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
2762 /* If we find an error, just print it and continue,
2763 cause it's just semantic, and we can see if there
2764 are more errors. */
2765 gfc_error_now ("Variable '%s' at %L in common block '%s' "
2766 "at %C must be declared with a C "
2767 "interoperable kind since common block "
2768 "'%s' is bind(c)",
2769 sym->name, &(sym->declared_at), t->name,
2770 t->name);
2773 if (sym->attr.is_bind_c == 1)
2774 gfc_error_now ("Variable '%s' in common block "
2775 "'%s' at %C can not be bind(c) since "
2776 "it is not global", sym->name, t->name);
2779 if (sym->attr.in_common)
2781 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2782 sym->name);
2783 goto cleanup;
2786 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2787 goto cleanup;
2789 if (sym->value != NULL && sym->value->expr_type != EXPR_NULL
2790 && (name[0] == '\0' || !sym->attr.data))
2792 if (name[0] == '\0')
2793 gfc_error ("Previously initialized symbol '%s' in "
2794 "blank COMMON block at %C", sym->name);
2795 else
2796 gfc_error ("Previously initialized symbol '%s' in "
2797 "COMMON block '%s' at %C", sym->name, name);
2798 goto cleanup;
2801 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2802 goto cleanup;
2804 if (tail != NULL)
2805 tail->common_next = sym;
2806 else
2807 *head = sym;
2809 tail = sym;
2811 /* Deal with an optional array specification after the
2812 symbol name. */
2813 m = gfc_match_array_spec (&as);
2814 if (m == MATCH_ERROR)
2815 goto cleanup;
2817 if (m == MATCH_YES)
2819 if (as->type != AS_EXPLICIT)
2821 gfc_error ("Array specification for symbol '%s' in COMMON "
2822 "at %C must be explicit", sym->name);
2823 goto cleanup;
2826 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2827 goto cleanup;
2829 if (sym->attr.pointer)
2831 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
2832 "POINTER array", sym->name);
2833 goto cleanup;
2836 sym->as = as;
2837 as = NULL;
2841 sym->common_head = t;
2843 /* Check to see if the symbol is already in an equivalence group.
2844 If it is, set the other members as being in common. */
2845 if (sym->attr.in_equivalence)
2847 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2849 for (e2 = e1; e2; e2 = e2->eq)
2850 if (e2->expr->symtree->n.sym == sym)
2851 goto equiv_found;
2853 continue;
2855 equiv_found:
2857 for (e2 = e1; e2; e2 = e2->eq)
2859 other = e2->expr->symtree->n.sym;
2860 if (other->common_head
2861 && other->common_head != sym->common_head)
2863 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2864 "%C is being indirectly equivalenced to "
2865 "another COMMON block '%s'",
2866 sym->name, sym->common_head->name,
2867 other->common_head->name);
2868 goto cleanup;
2870 other->attr.in_common = 1;
2871 other->common_head = t;
2877 gfc_gobble_whitespace ();
2878 if (gfc_match_eos () == MATCH_YES)
2879 goto done;
2880 if (gfc_peek_char () == '/')
2881 break;
2882 if (gfc_match_char (',') != MATCH_YES)
2883 goto syntax;
2884 gfc_gobble_whitespace ();
2885 if (gfc_peek_char () == '/')
2886 break;
2890 done:
2891 return MATCH_YES;
2893 syntax:
2894 gfc_syntax_error (ST_COMMON);
2896 cleanup:
2897 if (old_blank_common)
2898 old_blank_common->common_next = NULL;
2899 else
2900 gfc_current_ns->blank_common.head = NULL;
2901 gfc_free_array_spec (as);
2902 return MATCH_ERROR;
2906 /* Match a BLOCK DATA program unit. */
2908 match
2909 gfc_match_block_data (void)
2911 char name[GFC_MAX_SYMBOL_LEN + 1];
2912 gfc_symbol *sym;
2913 match m;
2915 if (gfc_match_eos () == MATCH_YES)
2917 gfc_new_block = NULL;
2918 return MATCH_YES;
2921 m = gfc_match ("% %n%t", name);
2922 if (m != MATCH_YES)
2923 return MATCH_ERROR;
2925 if (gfc_get_symbol (name, NULL, &sym))
2926 return MATCH_ERROR;
2928 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2929 return MATCH_ERROR;
2931 gfc_new_block = sym;
2933 return MATCH_YES;
2937 /* Free a namelist structure. */
2939 void
2940 gfc_free_namelist (gfc_namelist *name)
2942 gfc_namelist *n;
2944 for (; name; name = n)
2946 n = name->next;
2947 gfc_free (name);
2952 /* Match a NAMELIST statement. */
2954 match
2955 gfc_match_namelist (void)
2957 gfc_symbol *group_name, *sym;
2958 gfc_namelist *nl;
2959 match m, m2;
2961 m = gfc_match (" / %s /", &group_name);
2962 if (m == MATCH_NO)
2963 goto syntax;
2964 if (m == MATCH_ERROR)
2965 goto error;
2967 for (;;)
2969 if (group_name->ts.type != BT_UNKNOWN)
2971 gfc_error ("Namelist group name '%s' at %C already has a basic "
2972 "type of %s", group_name->name,
2973 gfc_typename (&group_name->ts));
2974 return MATCH_ERROR;
2977 if (group_name->attr.flavor == FL_NAMELIST
2978 && group_name->attr.use_assoc
2979 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2980 "at %C already is USE associated and can"
2981 "not be respecified.", group_name->name)
2982 == FAILURE)
2983 return MATCH_ERROR;
2985 if (group_name->attr.flavor != FL_NAMELIST
2986 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2987 group_name->name, NULL) == FAILURE)
2988 return MATCH_ERROR;
2990 for (;;)
2992 m = gfc_match_symbol (&sym, 1);
2993 if (m == MATCH_NO)
2994 goto syntax;
2995 if (m == MATCH_ERROR)
2996 goto error;
2998 if (sym->attr.in_namelist == 0
2999 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
3000 goto error;
3002 /* Use gfc_error_check here, rather than goto error, so that
3003 these are the only errors for the next two lines. */
3004 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
3006 gfc_error ("Assumed size array '%s' in namelist '%s' at "
3007 "%C is not allowed", sym->name, group_name->name);
3008 gfc_error_check ();
3011 if (sym->ts.type == BT_CHARACTER && sym->ts.cl->length == NULL)
3013 gfc_error ("Assumed character length '%s' in namelist '%s' at "
3014 "%C is not allowed", sym->name, group_name->name);
3015 gfc_error_check ();
3018 nl = gfc_get_namelist ();
3019 nl->sym = sym;
3020 sym->refs++;
3022 if (group_name->namelist == NULL)
3023 group_name->namelist = group_name->namelist_tail = nl;
3024 else
3026 group_name->namelist_tail->next = nl;
3027 group_name->namelist_tail = nl;
3030 if (gfc_match_eos () == MATCH_YES)
3031 goto done;
3033 m = gfc_match_char (',');
3035 if (gfc_match_char ('/') == MATCH_YES)
3037 m2 = gfc_match (" %s /", &group_name);
3038 if (m2 == MATCH_YES)
3039 break;
3040 if (m2 == MATCH_ERROR)
3041 goto error;
3042 goto syntax;
3045 if (m != MATCH_YES)
3046 goto syntax;
3050 done:
3051 return MATCH_YES;
3053 syntax:
3054 gfc_syntax_error (ST_NAMELIST);
3056 error:
3057 return MATCH_ERROR;
3061 /* Match a MODULE statement. */
3063 match
3064 gfc_match_module (void)
3066 match m;
3068 m = gfc_match (" %s%t", &gfc_new_block);
3069 if (m != MATCH_YES)
3070 return m;
3072 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
3073 gfc_new_block->name, NULL) == FAILURE)
3074 return MATCH_ERROR;
3076 return MATCH_YES;
3080 /* Free equivalence sets and lists. Recursively is the easiest way to
3081 do this. */
3083 void
3084 gfc_free_equiv (gfc_equiv *eq)
3086 if (eq == NULL)
3087 return;
3089 gfc_free_equiv (eq->eq);
3090 gfc_free_equiv (eq->next);
3091 gfc_free_expr (eq->expr);
3092 gfc_free (eq);
3096 /* Match an EQUIVALENCE statement. */
3098 match
3099 gfc_match_equivalence (void)
3101 gfc_equiv *eq, *set, *tail;
3102 gfc_ref *ref;
3103 gfc_symbol *sym;
3104 match m;
3105 gfc_common_head *common_head = NULL;
3106 bool common_flag;
3107 int cnt;
3109 tail = NULL;
3111 for (;;)
3113 eq = gfc_get_equiv ();
3114 if (tail == NULL)
3115 tail = eq;
3117 eq->next = gfc_current_ns->equiv;
3118 gfc_current_ns->equiv = eq;
3120 if (gfc_match_char ('(') != MATCH_YES)
3121 goto syntax;
3123 set = eq;
3124 common_flag = FALSE;
3125 cnt = 0;
3127 for (;;)
3129 m = gfc_match_equiv_variable (&set->expr);
3130 if (m == MATCH_ERROR)
3131 goto cleanup;
3132 if (m == MATCH_NO)
3133 goto syntax;
3135 /* count the number of objects. */
3136 cnt++;
3138 if (gfc_match_char ('%') == MATCH_YES)
3140 gfc_error ("Derived type component %C is not a "
3141 "permitted EQUIVALENCE member");
3142 goto cleanup;
3145 for (ref = set->expr->ref; ref; ref = ref->next)
3146 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3148 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
3149 "be an array section");
3150 goto cleanup;
3153 sym = set->expr->symtree->n.sym;
3155 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
3156 goto cleanup;
3158 if (sym->attr.in_common)
3160 common_flag = TRUE;
3161 common_head = sym->common_head;
3164 if (gfc_match_char (')') == MATCH_YES)
3165 break;
3167 if (gfc_match_char (',') != MATCH_YES)
3168 goto syntax;
3170 set->eq = gfc_get_equiv ();
3171 set = set->eq;
3174 if (cnt < 2)
3176 gfc_error ("EQUIVALENCE at %C requires two or more objects");
3177 goto cleanup;
3180 /* If one of the members of an equivalence is in common, then
3181 mark them all as being in common. Before doing this, check
3182 that members of the equivalence group are not in different
3183 common blocks. */
3184 if (common_flag)
3185 for (set = eq; set; set = set->eq)
3187 sym = set->expr->symtree->n.sym;
3188 if (sym->common_head && sym->common_head != common_head)
3190 gfc_error ("Attempt to indirectly overlap COMMON "
3191 "blocks %s and %s by EQUIVALENCE at %C",
3192 sym->common_head->name, common_head->name);
3193 goto cleanup;
3195 sym->attr.in_common = 1;
3196 sym->common_head = common_head;
3199 if (gfc_match_eos () == MATCH_YES)
3200 break;
3201 if (gfc_match_char (',') != MATCH_YES)
3202 goto syntax;
3205 return MATCH_YES;
3207 syntax:
3208 gfc_syntax_error (ST_EQUIVALENCE);
3210 cleanup:
3211 eq = tail->next;
3212 tail->next = NULL;
3214 gfc_free_equiv (gfc_current_ns->equiv);
3215 gfc_current_ns->equiv = eq;
3217 return MATCH_ERROR;
3221 /* Check that a statement function is not recursive. This is done by looking
3222 for the statement function symbol(sym) by looking recursively through its
3223 expression(e). If a reference to sym is found, true is returned.
3224 12.5.4 requires that any variable of function that is implicitly typed
3225 shall have that type confirmed by any subsequent type declaration. The
3226 implicit typing is conveniently done here. */
3228 static bool
3229 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
3231 gfc_actual_arglist *arg;
3232 gfc_ref *ref;
3233 int i;
3235 if (e == NULL)
3236 return false;
3238 switch (e->expr_type)
3240 case EXPR_FUNCTION:
3241 for (arg = e->value.function.actual; arg; arg = arg->next)
3243 if (sym->name == arg->name || recursive_stmt_fcn (arg->expr, sym))
3244 return true;
3247 if (e->symtree == NULL)
3248 return false;
3250 /* Check the name before testing for nested recursion! */
3251 if (sym->name == e->symtree->n.sym->name)
3252 return true;
3254 /* Catch recursion via other statement functions. */
3255 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
3256 && e->symtree->n.sym->value
3257 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
3258 return true;
3260 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3261 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3263 break;
3265 case EXPR_VARIABLE:
3266 if (e->symtree && sym->name == e->symtree->n.sym->name)
3267 return true;
3269 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3270 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3271 break;
3273 case EXPR_OP:
3274 if (recursive_stmt_fcn (e->value.op.op1, sym)
3275 || recursive_stmt_fcn (e->value.op.op2, sym))
3276 return true;
3277 break;
3279 default:
3280 break;
3283 /* Component references do not need to be checked. */
3284 if (e->ref)
3286 for (ref = e->ref; ref; ref = ref->next)
3288 switch (ref->type)
3290 case REF_ARRAY:
3291 for (i = 0; i < ref->u.ar.dimen; i++)
3293 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
3294 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
3295 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
3296 return true;
3298 break;
3300 case REF_SUBSTRING:
3301 if (recursive_stmt_fcn (ref->u.ss.start, sym)
3302 || recursive_stmt_fcn (ref->u.ss.end, sym))
3303 return true;
3305 break;
3307 default:
3308 break;
3312 return false;
3316 /* Match a statement function declaration. It is so easy to match
3317 non-statement function statements with a MATCH_ERROR as opposed to
3318 MATCH_NO that we suppress error message in most cases. */
3320 match
3321 gfc_match_st_function (void)
3323 gfc_error_buf old_error;
3324 gfc_symbol *sym;
3325 gfc_expr *expr;
3326 match m;
3328 m = gfc_match_symbol (&sym, 0);
3329 if (m != MATCH_YES)
3330 return m;
3332 gfc_push_error (&old_error);
3334 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
3335 sym->name, NULL) == FAILURE)
3336 goto undo_error;
3338 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
3339 goto undo_error;
3341 m = gfc_match (" = %e%t", &expr);
3342 if (m == MATCH_NO)
3343 goto undo_error;
3345 gfc_free_error (&old_error);
3346 if (m == MATCH_ERROR)
3347 return m;
3349 if (recursive_stmt_fcn (expr, sym))
3351 gfc_error ("Statement function at %L is recursive", &expr->where);
3352 return MATCH_ERROR;
3355 sym->value = expr;
3357 return MATCH_YES;
3359 undo_error:
3360 gfc_pop_error (&old_error);
3361 return MATCH_NO;
3365 /***************** SELECT CASE subroutines ******************/
3367 /* Free a single case structure. */
3369 static void
3370 free_case (gfc_case *p)
3372 if (p->low == p->high)
3373 p->high = NULL;
3374 gfc_free_expr (p->low);
3375 gfc_free_expr (p->high);
3376 gfc_free (p);
3380 /* Free a list of case structures. */
3382 void
3383 gfc_free_case_list (gfc_case *p)
3385 gfc_case *q;
3387 for (; p; p = q)
3389 q = p->next;
3390 free_case (p);
3395 /* Match a single case selector. */
3397 static match
3398 match_case_selector (gfc_case **cp)
3400 gfc_case *c;
3401 match m;
3403 c = gfc_get_case ();
3404 c->where = gfc_current_locus;
3406 if (gfc_match_char (':') == MATCH_YES)
3408 m = gfc_match_init_expr (&c->high);
3409 if (m == MATCH_NO)
3410 goto need_expr;
3411 if (m == MATCH_ERROR)
3412 goto cleanup;
3414 else
3416 m = gfc_match_init_expr (&c->low);
3417 if (m == MATCH_ERROR)
3418 goto cleanup;
3419 if (m == MATCH_NO)
3420 goto need_expr;
3422 /* If we're not looking at a ':' now, make a range out of a single
3423 target. Else get the upper bound for the case range. */
3424 if (gfc_match_char (':') != MATCH_YES)
3425 c->high = c->low;
3426 else
3428 m = gfc_match_init_expr (&c->high);
3429 if (m == MATCH_ERROR)
3430 goto cleanup;
3431 /* MATCH_NO is fine. It's OK if nothing is there! */
3435 *cp = c;
3436 return MATCH_YES;
3438 need_expr:
3439 gfc_error ("Expected initialization expression in CASE at %C");
3441 cleanup:
3442 free_case (c);
3443 return MATCH_ERROR;
3447 /* Match the end of a case statement. */
3449 static match
3450 match_case_eos (void)
3452 char name[GFC_MAX_SYMBOL_LEN + 1];
3453 match m;
3455 if (gfc_match_eos () == MATCH_YES)
3456 return MATCH_YES;
3458 /* If the case construct doesn't have a case-construct-name, we
3459 should have matched the EOS. */
3460 if (!gfc_current_block ())
3462 gfc_error ("Expected the name of the SELECT CASE construct at %C");
3463 return MATCH_ERROR;
3466 gfc_gobble_whitespace ();
3468 m = gfc_match_name (name);
3469 if (m != MATCH_YES)
3470 return m;
3472 if (strcmp (name, gfc_current_block ()->name) != 0)
3474 gfc_error ("Expected case name of '%s' at %C",
3475 gfc_current_block ()->name);
3476 return MATCH_ERROR;
3479 return gfc_match_eos ();
3483 /* Match a SELECT statement. */
3485 match
3486 gfc_match_select (void)
3488 gfc_expr *expr;
3489 match m;
3491 m = gfc_match_label ();
3492 if (m == MATCH_ERROR)
3493 return m;
3495 m = gfc_match (" select case ( %e )%t", &expr);
3496 if (m != MATCH_YES)
3497 return m;
3499 new_st.op = EXEC_SELECT;
3500 new_st.expr = expr;
3502 return MATCH_YES;
3506 /* Match a CASE statement. */
3508 match
3509 gfc_match_case (void)
3511 gfc_case *c, *head, *tail;
3512 match m;
3514 head = tail = NULL;
3516 if (gfc_current_state () != COMP_SELECT)
3518 gfc_error ("Unexpected CASE statement at %C");
3519 return MATCH_ERROR;
3522 if (gfc_match ("% default") == MATCH_YES)
3524 m = match_case_eos ();
3525 if (m == MATCH_NO)
3526 goto syntax;
3527 if (m == MATCH_ERROR)
3528 goto cleanup;
3530 new_st.op = EXEC_SELECT;
3531 c = gfc_get_case ();
3532 c->where = gfc_current_locus;
3533 new_st.ext.case_list = c;
3534 return MATCH_YES;
3537 if (gfc_match_char ('(') != MATCH_YES)
3538 goto syntax;
3540 for (;;)
3542 if (match_case_selector (&c) == MATCH_ERROR)
3543 goto cleanup;
3545 if (head == NULL)
3546 head = c;
3547 else
3548 tail->next = c;
3550 tail = c;
3552 if (gfc_match_char (')') == MATCH_YES)
3553 break;
3554 if (gfc_match_char (',') != MATCH_YES)
3555 goto syntax;
3558 m = match_case_eos ();
3559 if (m == MATCH_NO)
3560 goto syntax;
3561 if (m == MATCH_ERROR)
3562 goto cleanup;
3564 new_st.op = EXEC_SELECT;
3565 new_st.ext.case_list = head;
3567 return MATCH_YES;
3569 syntax:
3570 gfc_error ("Syntax error in CASE-specification at %C");
3572 cleanup:
3573 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3574 return MATCH_ERROR;
3577 /********************* WHERE subroutines ********************/
3579 /* Match the rest of a simple WHERE statement that follows an IF statement.
3582 static match
3583 match_simple_where (void)
3585 gfc_expr *expr;
3586 gfc_code *c;
3587 match m;
3589 m = gfc_match (" ( %e )", &expr);
3590 if (m != MATCH_YES)
3591 return m;
3593 m = gfc_match_assignment ();
3594 if (m == MATCH_NO)
3595 goto syntax;
3596 if (m == MATCH_ERROR)
3597 goto cleanup;
3599 if (gfc_match_eos () != MATCH_YES)
3600 goto syntax;
3602 c = gfc_get_code ();
3604 c->op = EXEC_WHERE;
3605 c->expr = expr;
3606 c->next = gfc_get_code ();
3608 *c->next = new_st;
3609 gfc_clear_new_st ();
3611 new_st.op = EXEC_WHERE;
3612 new_st.block = c;
3614 return MATCH_YES;
3616 syntax:
3617 gfc_syntax_error (ST_WHERE);
3619 cleanup:
3620 gfc_free_expr (expr);
3621 return MATCH_ERROR;
3625 /* Match a WHERE statement. */
3627 match
3628 gfc_match_where (gfc_statement *st)
3630 gfc_expr *expr;
3631 match m0, m;
3632 gfc_code *c;
3634 m0 = gfc_match_label ();
3635 if (m0 == MATCH_ERROR)
3636 return m0;
3638 m = gfc_match (" where ( %e )", &expr);
3639 if (m != MATCH_YES)
3640 return m;
3642 if (gfc_match_eos () == MATCH_YES)
3644 *st = ST_WHERE_BLOCK;
3645 new_st.op = EXEC_WHERE;
3646 new_st.expr = expr;
3647 return MATCH_YES;
3650 m = gfc_match_assignment ();
3651 if (m == MATCH_NO)
3652 gfc_syntax_error (ST_WHERE);
3654 if (m != MATCH_YES)
3656 gfc_free_expr (expr);
3657 return MATCH_ERROR;
3660 /* We've got a simple WHERE statement. */
3661 *st = ST_WHERE;
3662 c = gfc_get_code ();
3664 c->op = EXEC_WHERE;
3665 c->expr = expr;
3666 c->next = gfc_get_code ();
3668 *c->next = new_st;
3669 gfc_clear_new_st ();
3671 new_st.op = EXEC_WHERE;
3672 new_st.block = c;
3674 return MATCH_YES;
3678 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3679 new_st if successful. */
3681 match
3682 gfc_match_elsewhere (void)
3684 char name[GFC_MAX_SYMBOL_LEN + 1];
3685 gfc_expr *expr;
3686 match m;
3688 if (gfc_current_state () != COMP_WHERE)
3690 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3691 return MATCH_ERROR;
3694 expr = NULL;
3696 if (gfc_match_char ('(') == MATCH_YES)
3698 m = gfc_match_expr (&expr);
3699 if (m == MATCH_NO)
3700 goto syntax;
3701 if (m == MATCH_ERROR)
3702 return MATCH_ERROR;
3704 if (gfc_match_char (')') != MATCH_YES)
3705 goto syntax;
3708 if (gfc_match_eos () != MATCH_YES)
3710 /* Only makes sense if we have a where-construct-name. */
3711 if (!gfc_current_block ())
3713 m = MATCH_ERROR;
3714 goto cleanup;
3716 /* Better be a name at this point. */
3717 m = gfc_match_name (name);
3718 if (m == MATCH_NO)
3719 goto syntax;
3720 if (m == MATCH_ERROR)
3721 goto cleanup;
3723 if (gfc_match_eos () != MATCH_YES)
3724 goto syntax;
3726 if (strcmp (name, gfc_current_block ()->name) != 0)
3728 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3729 name, gfc_current_block ()->name);
3730 goto cleanup;
3734 new_st.op = EXEC_WHERE;
3735 new_st.expr = expr;
3736 return MATCH_YES;
3738 syntax:
3739 gfc_syntax_error (ST_ELSEWHERE);
3741 cleanup:
3742 gfc_free_expr (expr);
3743 return MATCH_ERROR;
3747 /******************** FORALL subroutines ********************/
3749 /* Free a list of FORALL iterators. */
3751 void
3752 gfc_free_forall_iterator (gfc_forall_iterator *iter)
3754 gfc_forall_iterator *next;
3756 while (iter)
3758 next = iter->next;
3759 gfc_free_expr (iter->var);
3760 gfc_free_expr (iter->start);
3761 gfc_free_expr (iter->end);
3762 gfc_free_expr (iter->stride);
3763 gfc_free (iter);
3764 iter = next;
3769 /* Match an iterator as part of a FORALL statement. The format is:
3771 <var> = <start>:<end>[:<stride>]
3773 On MATCH_NO, the caller tests for the possibility that there is a
3774 scalar mask expression. */
3776 static match
3777 match_forall_iterator (gfc_forall_iterator **result)
3779 gfc_forall_iterator *iter;
3780 locus where;
3781 match m;
3783 where = gfc_current_locus;
3784 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3786 m = gfc_match_expr (&iter->var);
3787 if (m != MATCH_YES)
3788 goto cleanup;
3790 if (gfc_match_char ('=') != MATCH_YES
3791 || iter->var->expr_type != EXPR_VARIABLE)
3793 m = MATCH_NO;
3794 goto cleanup;
3797 m = gfc_match_expr (&iter->start);
3798 if (m != MATCH_YES)
3799 goto cleanup;
3801 if (gfc_match_char (':') != MATCH_YES)
3802 goto syntax;
3804 m = gfc_match_expr (&iter->end);
3805 if (m == MATCH_NO)
3806 goto syntax;
3807 if (m == MATCH_ERROR)
3808 goto cleanup;
3810 if (gfc_match_char (':') == MATCH_NO)
3811 iter->stride = gfc_int_expr (1);
3812 else
3814 m = gfc_match_expr (&iter->stride);
3815 if (m == MATCH_NO)
3816 goto syntax;
3817 if (m == MATCH_ERROR)
3818 goto cleanup;
3821 /* Mark the iteration variable's symbol as used as a FORALL index. */
3822 iter->var->symtree->n.sym->forall_index = true;
3824 *result = iter;
3825 return MATCH_YES;
3827 syntax:
3828 gfc_error ("Syntax error in FORALL iterator at %C");
3829 m = MATCH_ERROR;
3831 cleanup:
3833 gfc_current_locus = where;
3834 gfc_free_forall_iterator (iter);
3835 return m;
3839 /* Match the header of a FORALL statement. */
3841 static match
3842 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
3844 gfc_forall_iterator *head, *tail, *new;
3845 gfc_expr *msk;
3846 match m;
3848 gfc_gobble_whitespace ();
3850 head = tail = NULL;
3851 msk = NULL;
3853 if (gfc_match_char ('(') != MATCH_YES)
3854 return MATCH_NO;
3856 m = match_forall_iterator (&new);
3857 if (m == MATCH_ERROR)
3858 goto cleanup;
3859 if (m == MATCH_NO)
3860 goto syntax;
3862 head = tail = new;
3864 for (;;)
3866 if (gfc_match_char (',') != MATCH_YES)
3867 break;
3869 m = match_forall_iterator (&new);
3870 if (m == MATCH_ERROR)
3871 goto cleanup;
3873 if (m == MATCH_YES)
3875 tail->next = new;
3876 tail = new;
3877 continue;
3880 /* Have to have a mask expression. */
3882 m = gfc_match_expr (&msk);
3883 if (m == MATCH_NO)
3884 goto syntax;
3885 if (m == MATCH_ERROR)
3886 goto cleanup;
3888 break;
3891 if (gfc_match_char (')') == MATCH_NO)
3892 goto syntax;
3894 *phead = head;
3895 *mask = msk;
3896 return MATCH_YES;
3898 syntax:
3899 gfc_syntax_error (ST_FORALL);
3901 cleanup:
3902 gfc_free_expr (msk);
3903 gfc_free_forall_iterator (head);
3905 return MATCH_ERROR;
3908 /* Match the rest of a simple FORALL statement that follows an
3909 IF statement. */
3911 static match
3912 match_simple_forall (void)
3914 gfc_forall_iterator *head;
3915 gfc_expr *mask;
3916 gfc_code *c;
3917 match m;
3919 mask = NULL;
3920 head = NULL;
3921 c = NULL;
3923 m = match_forall_header (&head, &mask);
3925 if (m == MATCH_NO)
3926 goto syntax;
3927 if (m != MATCH_YES)
3928 goto cleanup;
3930 m = gfc_match_assignment ();
3932 if (m == MATCH_ERROR)
3933 goto cleanup;
3934 if (m == MATCH_NO)
3936 m = gfc_match_pointer_assignment ();
3937 if (m == MATCH_ERROR)
3938 goto cleanup;
3939 if (m == MATCH_NO)
3940 goto syntax;
3943 c = gfc_get_code ();
3944 *c = new_st;
3945 c->loc = gfc_current_locus;
3947 if (gfc_match_eos () != MATCH_YES)
3948 goto syntax;
3950 gfc_clear_new_st ();
3951 new_st.op = EXEC_FORALL;
3952 new_st.expr = mask;
3953 new_st.ext.forall_iterator = head;
3954 new_st.block = gfc_get_code ();
3956 new_st.block->op = EXEC_FORALL;
3957 new_st.block->next = c;
3959 return MATCH_YES;
3961 syntax:
3962 gfc_syntax_error (ST_FORALL);
3964 cleanup:
3965 gfc_free_forall_iterator (head);
3966 gfc_free_expr (mask);
3968 return MATCH_ERROR;
3972 /* Match a FORALL statement. */
3974 match
3975 gfc_match_forall (gfc_statement *st)
3977 gfc_forall_iterator *head;
3978 gfc_expr *mask;
3979 gfc_code *c;
3980 match m0, m;
3982 head = NULL;
3983 mask = NULL;
3984 c = NULL;
3986 m0 = gfc_match_label ();
3987 if (m0 == MATCH_ERROR)
3988 return MATCH_ERROR;
3990 m = gfc_match (" forall");
3991 if (m != MATCH_YES)
3992 return m;
3994 m = match_forall_header (&head, &mask);
3995 if (m == MATCH_ERROR)
3996 goto cleanup;
3997 if (m == MATCH_NO)
3998 goto syntax;
4000 if (gfc_match_eos () == MATCH_YES)
4002 *st = ST_FORALL_BLOCK;
4003 new_st.op = EXEC_FORALL;
4004 new_st.expr = mask;
4005 new_st.ext.forall_iterator = head;
4006 return MATCH_YES;
4009 m = gfc_match_assignment ();
4010 if (m == MATCH_ERROR)
4011 goto cleanup;
4012 if (m == MATCH_NO)
4014 m = gfc_match_pointer_assignment ();
4015 if (m == MATCH_ERROR)
4016 goto cleanup;
4017 if (m == MATCH_NO)
4018 goto syntax;
4021 c = gfc_get_code ();
4022 *c = new_st;
4023 c->loc = gfc_current_locus;
4025 gfc_clear_new_st ();
4026 new_st.op = EXEC_FORALL;
4027 new_st.expr = mask;
4028 new_st.ext.forall_iterator = head;
4029 new_st.block = gfc_get_code ();
4030 new_st.block->op = EXEC_FORALL;
4031 new_st.block->next = c;
4033 *st = ST_FORALL;
4034 return MATCH_YES;
4036 syntax:
4037 gfc_syntax_error (ST_FORALL);
4039 cleanup:
4040 gfc_free_forall_iterator (head);
4041 gfc_free_expr (mask);
4042 gfc_free_statements (c);
4043 return MATCH_NO;