1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
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 2, or (at your option) any later
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
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
31 /* For matching and debugging purposes. Order matters here! The
32 unary operators /must/ precede the binary plus and minus, or
33 the expression parser breaks. */
35 mstring intrinsic_operators[] = {
36 minit ("+", INTRINSIC_UPLUS),
37 minit ("-", INTRINSIC_UMINUS),
38 minit ("+", INTRINSIC_PLUS),
39 minit ("-", INTRINSIC_MINUS),
40 minit ("**", INTRINSIC_POWER),
41 minit ("//", INTRINSIC_CONCAT),
42 minit ("*", INTRINSIC_TIMES),
43 minit ("/", INTRINSIC_DIVIDE),
44 minit (".and.", INTRINSIC_AND),
45 minit (".or.", INTRINSIC_OR),
46 minit (".eqv.", INTRINSIC_EQV),
47 minit (".neqv.", INTRINSIC_NEQV),
48 minit (".eq.", INTRINSIC_EQ),
49 minit ("==", INTRINSIC_EQ),
50 minit (".ne.", INTRINSIC_NE),
51 minit ("/=", INTRINSIC_NE),
52 minit (".ge.", INTRINSIC_GE),
53 minit (">=", INTRINSIC_GE),
54 minit (".le.", INTRINSIC_LE),
55 minit ("<=", INTRINSIC_LE),
56 minit (".lt.", INTRINSIC_LT),
57 minit ("<", INTRINSIC_LT),
58 minit (".gt.", INTRINSIC_GT),
59 minit (">", INTRINSIC_GT),
60 minit (".not.", INTRINSIC_NOT),
61 minit (NULL, INTRINSIC_NONE)
65 /******************** Generic matching subroutines ************************/
67 /* In free form, match at least one space. Always matches in fixed
71 gfc_match_space (void)
76 if (gfc_current_form == FORM_FIXED)
79 old_loc = gfc_current_locus;
82 if (!gfc_is_whitespace (c))
84 gfc_current_locus = old_loc;
88 gfc_gobble_whitespace ();
94 /* Match an end of statement. End of statement is optional
95 whitespace, followed by a ';' or '\n' or comment '!'. If a
96 semicolon is found, we continue to eat whitespace and semicolons. */
108 old_loc = gfc_current_locus;
109 gfc_gobble_whitespace ();
111 c = gfc_next_char ();
117 c = gfc_next_char ();
134 gfc_current_locus = old_loc;
135 return (flag) ? MATCH_YES : MATCH_NO;
139 /* Match a literal integer on the input, setting the value on
140 MATCH_YES. Literal ints occur in kind-parameters as well as
141 old-style character length specifications. */
144 gfc_match_small_literal_int (int *value, int *cnt)
150 old_loc = gfc_current_locus;
152 gfc_gobble_whitespace ();
153 c = gfc_next_char ();
158 gfc_current_locus = old_loc;
167 old_loc = gfc_current_locus;
168 c = gfc_next_char ();
173 i = 10 * i + c - '0';
178 gfc_error ("Integer too large at %C");
183 gfc_current_locus = old_loc;
191 /* Match a small, constant integer expression, like in a kind
192 statement. On MATCH_YES, 'value' is set. */
195 gfc_match_small_int (int *value)
202 m = gfc_match_expr (&expr);
206 p = gfc_extract_int (expr, &i);
207 gfc_free_expr (expr);
220 /* Matches a statement label. Uses gfc_match_small_literal_int() to
221 do most of the work. */
224 gfc_match_st_label (gfc_st_label ** label)
230 old_loc = gfc_current_locus;
232 m = gfc_match_small_literal_int (&i, &cnt);
238 gfc_error ("Too many digits in statement label at %C");
244 gfc_error ("Statement label at %C is zero");
248 *label = gfc_get_st_label (i);
253 gfc_current_locus = old_loc;
258 /* Match and validate a label associated with a named IF, DO or SELECT
259 statement. If the symbol does not have the label attribute, we add
260 it. We also make sure the symbol does not refer to another
261 (active) block. A matched label is pointed to by gfc_new_block. */
264 gfc_match_label (void)
266 char name[GFC_MAX_SYMBOL_LEN + 1];
269 gfc_new_block = NULL;
271 m = gfc_match (" %n :", name);
275 if (gfc_get_symbol (name, NULL, &gfc_new_block))
277 gfc_error ("Label name '%s' at %C is ambiguous", name);
281 if (gfc_new_block->attr.flavor == FL_LABEL)
283 gfc_error ("Duplicate construct label '%s' at %C", name);
287 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
288 gfc_new_block->name, NULL) == FAILURE)
295 /* Try and match the input against an array of possibilities. If one
296 potential matching string is a substring of another, the longest
297 match takes precedence. Spaces in the target strings are optional
298 spaces that do not necessarily have to be found in the input
299 stream. In fixed mode, spaces never appear. If whitespace is
300 matched, it matches unlimited whitespace in the input. For this
301 reason, the 'mp' member of the mstring structure is used to track
302 the progress of each potential match.
304 If there is no match we return the tag associated with the
305 terminating NULL mstring structure and leave the locus pointer
306 where it started. If there is a match we return the tag member of
307 the matched mstring and leave the locus pointer after the matched
310 A '%' character is a mandatory space. */
313 gfc_match_strings (mstring * a)
315 mstring *p, *best_match;
316 int no_match, c, possibles;
321 for (p = a; p->string != NULL; p++)
330 match_loc = gfc_current_locus;
332 gfc_gobble_whitespace ();
334 while (possibles > 0)
336 c = gfc_next_char ();
338 /* Apply the next character to the current possibilities. */
339 for (p = a; p->string != NULL; p++)
346 /* Space matches 1+ whitespace(s). */
347 if ((gfc_current_form == FORM_FREE)
348 && gfc_is_whitespace (c))
366 match_loc = gfc_current_locus;
374 gfc_current_locus = match_loc;
376 return (best_match == NULL) ? no_match : best_match->tag;
380 /* See if the current input looks like a name of some sort. Modifies
381 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long. */
384 gfc_match_name (char *buffer)
389 old_loc = gfc_current_locus;
390 gfc_gobble_whitespace ();
392 c = gfc_next_char ();
395 gfc_current_locus = old_loc;
405 if (i > gfc_option.max_identifier_length)
407 gfc_error ("Name at %C is too long");
411 old_loc = gfc_current_locus;
412 c = gfc_next_char ();
416 || (gfc_option.flag_dollar_ok && c == '$'));
419 gfc_current_locus = old_loc;
425 /* Match a symbol on the input. Modifies the pointer to the symbol
426 pointer if successful. */
429 gfc_match_sym_tree (gfc_symtree ** matched_symbol, int host_assoc)
431 char buffer[GFC_MAX_SYMBOL_LEN + 1];
434 m = gfc_match_name (buffer);
439 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
440 ? MATCH_ERROR : MATCH_YES;
442 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
450 gfc_match_symbol (gfc_symbol ** matched_symbol, int host_assoc)
455 m = gfc_match_sym_tree (&st, host_assoc);
460 *matched_symbol = st->n.sym;
462 *matched_symbol = NULL;
465 *matched_symbol = NULL;
469 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
470 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
474 gfc_match_intrinsic_op (gfc_intrinsic_op * result)
478 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
480 if (op == INTRINSIC_NONE)
488 /* Match a loop control phrase:
490 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
492 If the final integer expression is not present, a constant unity
493 expression is returned. We don't return MATCH_ERROR until after
494 the equals sign is seen. */
497 gfc_match_iterator (gfc_iterator * iter, int init_flag)
499 char name[GFC_MAX_SYMBOL_LEN + 1];
500 gfc_expr *var, *e1, *e2, *e3;
504 /* Match the start of an iterator without affecting the symbol
507 start = gfc_current_locus;
508 m = gfc_match (" %n =", name);
509 gfc_current_locus = start;
514 m = gfc_match_variable (&var, 0);
518 gfc_match_char ('=');
522 if (var->ref != NULL)
524 gfc_error ("Loop variable at %C cannot be a sub-component");
528 if (var->symtree->n.sym->attr.intent == INTENT_IN)
530 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
531 var->symtree->n.sym->name);
535 if (var->symtree->n.sym->attr.pointer)
537 gfc_error ("Loop variable at %C cannot have the POINTER attribute");
541 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
544 if (m == MATCH_ERROR)
547 if (gfc_match_char (',') != MATCH_YES)
550 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
553 if (m == MATCH_ERROR)
556 if (gfc_match_char (',') != MATCH_YES)
558 e3 = gfc_int_expr (1);
562 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
563 if (m == MATCH_ERROR)
567 gfc_error ("Expected a step value in iterator at %C");
579 gfc_error ("Syntax error in iterator at %C");
590 /* Tries to match the next non-whitespace character on the input.
591 This subroutine does not return MATCH_ERROR. */
594 gfc_match_char (char c)
598 where = gfc_current_locus;
599 gfc_gobble_whitespace ();
601 if (gfc_next_char () == c)
604 gfc_current_locus = where;
609 /* General purpose matching subroutine. The target string is a
610 scanf-like format string in which spaces correspond to arbitrary
611 whitespace (including no whitespace), characters correspond to
612 themselves. The %-codes are:
614 %% Literal percent sign
615 %e Expression, pointer to a pointer is set
616 %s Symbol, pointer to the symbol is set
617 %n Name, character buffer is set to name
618 %t Matches end of statement.
619 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
620 %l Matches a statement label
621 %v Matches a variable expression (an lvalue)
622 % Matches a required space (in free form) and optional spaces. */
625 gfc_match (const char *target, ...)
627 gfc_st_label **label;
636 old_loc = gfc_current_locus;
637 va_start (argp, target);
647 gfc_gobble_whitespace ();
658 vp = va_arg (argp, void **);
659 n = gfc_match_expr ((gfc_expr **) vp);
670 vp = va_arg (argp, void **);
671 n = gfc_match_variable ((gfc_expr **) vp, 0);
682 vp = va_arg (argp, void **);
683 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
694 np = va_arg (argp, char *);
695 n = gfc_match_name (np);
706 label = va_arg (argp, gfc_st_label **);
707 n = gfc_match_st_label (label);
718 ip = va_arg (argp, int *);
719 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
730 if (gfc_match_eos () != MATCH_YES)
738 if (gfc_match_space () == MATCH_YES)
744 break; /* Fall through to character matcher */
747 gfc_internal_error ("gfc_match(): Bad match code %c", c);
751 if (c == gfc_next_char ())
761 /* Clean up after a failed match. */
762 gfc_current_locus = old_loc;
763 va_start (argp, target);
766 for (; matches > 0; matches--)
776 /* Matches that don't have to be undone */
781 (void)va_arg (argp, void **);
786 vp = va_arg (argp, void **);
800 /*********************** Statement level matching **********************/
802 /* Matches the start of a program unit, which is the program keyword
803 followed by an obligatory symbol. */
806 gfc_match_program (void)
811 m = gfc_match ("% %s%t", &sym);
815 gfc_error ("Invalid form of PROGRAM statement at %C");
819 if (m == MATCH_ERROR)
822 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
831 /* Match a simple assignment statement. */
834 gfc_match_assignment (void)
836 gfc_expr *lvalue, *rvalue;
840 old_loc = gfc_current_locus;
842 lvalue = rvalue = NULL;
843 m = gfc_match (" %v =", &lvalue);
847 if (lvalue->symtree->n.sym->attr.flavor == FL_PARAMETER)
849 gfc_error ("Cannot assign to a PARAMETER variable at %C");
854 m = gfc_match (" %e%t", &rvalue);
858 gfc_set_sym_referenced (lvalue->symtree->n.sym);
860 new_st.op = EXEC_ASSIGN;
861 new_st.expr = lvalue;
862 new_st.expr2 = rvalue;
864 gfc_check_do_variable (lvalue->symtree);
869 gfc_current_locus = old_loc;
870 gfc_free_expr (lvalue);
871 gfc_free_expr (rvalue);
876 /* Match a pointer assignment statement. */
879 gfc_match_pointer_assignment (void)
881 gfc_expr *lvalue, *rvalue;
885 old_loc = gfc_current_locus;
887 lvalue = rvalue = NULL;
889 m = gfc_match (" %v =>", &lvalue);
896 m = gfc_match (" %e%t", &rvalue);
900 new_st.op = EXEC_POINTER_ASSIGN;
901 new_st.expr = lvalue;
902 new_st.expr2 = rvalue;
907 gfc_current_locus = old_loc;
908 gfc_free_expr (lvalue);
909 gfc_free_expr (rvalue);
914 /* We try to match an easy arithmetic IF statement. This only happens
915 when just after having encountered a simple IF statement. This code
916 is really duplicate with parts of the gfc_match_if code, but this is
919 match_arithmetic_if (void)
921 gfc_st_label *l1, *l2, *l3;
925 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
929 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
930 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
931 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
933 gfc_free_expr (expr);
937 if (gfc_notify_std (GFC_STD_F95_DEL,
938 "Obsolete: arithmetic IF statement at %C") == FAILURE)
941 new_st.op = EXEC_ARITHMETIC_IF;
951 /* The IF statement is a bit of a pain. First of all, there are three
952 forms of it, the simple IF, the IF that starts a block and the
955 There is a problem with the simple IF and that is the fact that we
956 only have a single level of undo information on symbols. What this
957 means is for a simple IF, we must re-match the whole IF statement
958 multiple times in order to guarantee that the symbol table ends up
959 in the proper state. */
961 static match match_simple_forall (void);
962 static match match_simple_where (void);
965 gfc_match_if (gfc_statement * if_type)
968 gfc_st_label *l1, *l2, *l3;
973 n = gfc_match_label ();
974 if (n == MATCH_ERROR)
977 old_loc = gfc_current_locus;
979 m = gfc_match (" if ( %e", &expr);
983 if (gfc_match_char (')') != MATCH_YES)
985 gfc_error ("Syntax error in IF-expression at %C");
986 gfc_free_expr (expr);
990 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
997 ("Block label not appropriate for arithmetic IF statement "
1000 gfc_free_expr (expr);
1004 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1005 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1006 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1009 gfc_free_expr (expr);
1013 if (gfc_notify_std (GFC_STD_F95_DEL,
1014 "Obsolete: arithmetic IF statement at %C")
1018 new_st.op = EXEC_ARITHMETIC_IF;
1024 *if_type = ST_ARITHMETIC_IF;
1028 if (gfc_match (" then%t") == MATCH_YES)
1030 new_st.op = EXEC_IF;
1033 *if_type = ST_IF_BLOCK;
1039 gfc_error ("Block label is not appropriate IF statement at %C");
1041 gfc_free_expr (expr);
1045 /* At this point the only thing left is a simple IF statement. At
1046 this point, n has to be MATCH_NO, so we don't have to worry about
1047 re-matching a block label. From what we've got so far, try
1048 matching an assignment. */
1050 *if_type = ST_SIMPLE_IF;
1052 m = gfc_match_assignment ();
1056 gfc_free_expr (expr);
1057 gfc_undo_symbols ();
1058 gfc_current_locus = old_loc;
1060 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1062 m = gfc_match_pointer_assignment ();
1066 gfc_free_expr (expr);
1067 gfc_undo_symbols ();
1068 gfc_current_locus = old_loc;
1070 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1072 /* Look at the next keyword to see which matcher to call. Matching
1073 the keyword doesn't affect the symbol table, so we don't have to
1074 restore between tries. */
1076 #define match(string, subr, statement) \
1077 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1081 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1082 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1083 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1084 match ("call", gfc_match_call, ST_CALL)
1085 match ("close", gfc_match_close, ST_CLOSE)
1086 match ("continue", gfc_match_continue, ST_CONTINUE)
1087 match ("cycle", gfc_match_cycle, ST_CYCLE)
1088 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1089 match ("end file", gfc_match_endfile, ST_END_FILE)
1090 match ("exit", gfc_match_exit, ST_EXIT)
1091 match ("flush", gfc_match_flush, ST_FLUSH)
1092 match ("forall", match_simple_forall, ST_FORALL)
1093 match ("go to", gfc_match_goto, ST_GOTO)
1094 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1095 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1096 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1097 match ("open", gfc_match_open, ST_OPEN)
1098 match ("pause", gfc_match_pause, ST_NONE)
1099 match ("print", gfc_match_print, ST_WRITE)
1100 match ("read", gfc_match_read, ST_READ)
1101 match ("return", gfc_match_return, ST_RETURN)
1102 match ("rewind", gfc_match_rewind, ST_REWIND)
1103 match ("stop", gfc_match_stop, ST_STOP)
1104 match ("where", match_simple_where, ST_WHERE)
1105 match ("write", gfc_match_write, ST_WRITE)
1107 /* All else has failed, so give up. See if any of the matchers has
1108 stored an error message of some sort. */
1109 if (gfc_error_check () == 0)
1110 gfc_error ("Unclassifiable statement in IF-clause at %C");
1112 gfc_free_expr (expr);
1117 gfc_error ("Syntax error in IF-clause at %C");
1120 gfc_free_expr (expr);
1124 /* At this point, we've matched the single IF and the action clause
1125 is in new_st. Rearrange things so that the IF statement appears
1128 p = gfc_get_code ();
1129 p->next = gfc_get_code ();
1131 p->next->loc = gfc_current_locus;
1136 gfc_clear_new_st ();
1138 new_st.op = EXEC_IF;
1147 /* Match an ELSE statement. */
1150 gfc_match_else (void)
1152 char name[GFC_MAX_SYMBOL_LEN + 1];
1154 if (gfc_match_eos () == MATCH_YES)
1157 if (gfc_match_name (name) != MATCH_YES
1158 || gfc_current_block () == NULL
1159 || gfc_match_eos () != MATCH_YES)
1161 gfc_error ("Unexpected junk after ELSE statement at %C");
1165 if (strcmp (name, gfc_current_block ()->name) != 0)
1167 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1168 name, gfc_current_block ()->name);
1176 /* Match an ELSE IF statement. */
1179 gfc_match_elseif (void)
1181 char name[GFC_MAX_SYMBOL_LEN + 1];
1185 m = gfc_match (" ( %e ) then", &expr);
1189 if (gfc_match_eos () == MATCH_YES)
1192 if (gfc_match_name (name) != MATCH_YES
1193 || gfc_current_block () == NULL
1194 || gfc_match_eos () != MATCH_YES)
1196 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1200 if (strcmp (name, gfc_current_block ()->name) != 0)
1202 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1203 name, gfc_current_block ()->name);
1208 new_st.op = EXEC_IF;
1213 gfc_free_expr (expr);
1218 /* Free a gfc_iterator structure. */
1221 gfc_free_iterator (gfc_iterator * iter, int flag)
1227 gfc_free_expr (iter->var);
1228 gfc_free_expr (iter->start);
1229 gfc_free_expr (iter->end);
1230 gfc_free_expr (iter->step);
1237 /* Match a DO statement. */
1242 gfc_iterator iter, *ip;
1244 gfc_st_label *label;
1247 old_loc = gfc_current_locus;
1250 iter.var = iter.start = iter.end = iter.step = NULL;
1252 m = gfc_match_label ();
1253 if (m == MATCH_ERROR)
1256 if (gfc_match (" do") != MATCH_YES)
1259 m = gfc_match_st_label (&label);
1260 if (m == MATCH_ERROR)
1263 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1265 if (gfc_match_eos () == MATCH_YES)
1267 iter.end = gfc_logical_expr (1, NULL);
1268 new_st.op = EXEC_DO_WHILE;
1272 /* match an optional comma, if no comma is found a space is obligatory. */
1273 if (gfc_match_char(',') != MATCH_YES
1274 && gfc_match ("% ") != MATCH_YES)
1277 /* See if we have a DO WHILE. */
1278 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1280 new_st.op = EXEC_DO_WHILE;
1284 /* The abortive DO WHILE may have done something to the symbol
1285 table, so we start over: */
1286 gfc_undo_symbols ();
1287 gfc_current_locus = old_loc;
1289 gfc_match_label (); /* This won't error */
1290 gfc_match (" do "); /* This will work */
1292 gfc_match_st_label (&label); /* Can't error out */
1293 gfc_match_char (','); /* Optional comma */
1295 m = gfc_match_iterator (&iter, 0);
1298 if (m == MATCH_ERROR)
1301 gfc_check_do_variable (iter.var->symtree);
1303 if (gfc_match_eos () != MATCH_YES)
1305 gfc_syntax_error (ST_DO);
1309 new_st.op = EXEC_DO;
1313 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1316 new_st.label = label;
1318 if (new_st.op == EXEC_DO_WHILE)
1319 new_st.expr = iter.end;
1322 new_st.ext.iterator = ip = gfc_get_iterator ();
1329 gfc_free_iterator (&iter, 0);
1335 /* Match an EXIT or CYCLE statement. */
1338 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1344 if (gfc_match_eos () == MATCH_YES)
1348 m = gfc_match ("% %s%t", &sym);
1349 if (m == MATCH_ERROR)
1353 gfc_syntax_error (st);
1357 if (sym->attr.flavor != FL_LABEL)
1359 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1360 sym->name, gfc_ascii_statement (st));
1365 /* Find the loop mentioned specified by the label (or lack of a
1367 for (p = gfc_state_stack; p; p = p->previous)
1368 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1374 gfc_error ("%s statement at %C is not within a loop",
1375 gfc_ascii_statement (st));
1377 gfc_error ("%s statement at %C is not within loop '%s'",
1378 gfc_ascii_statement (st), sym->name);
1383 /* Save the first statement in the loop - needed by the backend. */
1384 new_st.ext.whichloop = p->head;
1387 /* new_st.sym = sym;*/
1393 /* Match the EXIT statement. */
1396 gfc_match_exit (void)
1399 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1403 /* Match the CYCLE statement. */
1406 gfc_match_cycle (void)
1409 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1413 /* Match a number or character constant after a STOP or PAUSE statement. */
1416 gfc_match_stopcode (gfc_statement st)
1426 if (gfc_match_eos () != MATCH_YES)
1428 m = gfc_match_small_literal_int (&stop_code, &cnt);
1429 if (m == MATCH_ERROR)
1432 if (m == MATCH_YES && cnt > 5)
1434 gfc_error ("Too many digits in STOP code at %C");
1440 /* Try a character constant. */
1441 m = gfc_match_expr (&e);
1442 if (m == MATCH_ERROR)
1446 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1450 if (gfc_match_eos () != MATCH_YES)
1454 if (gfc_pure (NULL))
1456 gfc_error ("%s statement not allowed in PURE procedure at %C",
1457 gfc_ascii_statement (st));
1461 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1463 new_st.ext.stop_code = stop_code;
1468 gfc_syntax_error (st);
1476 /* Match the (deprecated) PAUSE statement. */
1479 gfc_match_pause (void)
1483 m = gfc_match_stopcode (ST_PAUSE);
1486 if (gfc_notify_std (GFC_STD_F95_DEL,
1487 "Obsolete: PAUSE statement at %C")
1495 /* Match the STOP statement. */
1498 gfc_match_stop (void)
1500 return gfc_match_stopcode (ST_STOP);
1504 /* Match a CONTINUE statement. */
1507 gfc_match_continue (void)
1510 if (gfc_match_eos () != MATCH_YES)
1512 gfc_syntax_error (ST_CONTINUE);
1516 new_st.op = EXEC_CONTINUE;
1521 /* Match the (deprecated) ASSIGN statement. */
1524 gfc_match_assign (void)
1527 gfc_st_label *label;
1529 if (gfc_match (" %l", &label) == MATCH_YES)
1531 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1533 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1535 if (gfc_notify_std (GFC_STD_F95_DEL,
1536 "Obsolete: ASSIGN statement at %C")
1540 expr->symtree->n.sym->attr.assign = 1;
1542 new_st.op = EXEC_LABEL_ASSIGN;
1543 new_st.label = label;
1552 /* Match the GO TO statement. As a computed GOTO statement is
1553 matched, it is transformed into an equivalent SELECT block. No
1554 tree is necessary, and the resulting jumps-to-jumps are
1555 specifically optimized away by the back end. */
1558 gfc_match_goto (void)
1560 gfc_code *head, *tail;
1563 gfc_st_label *label;
1567 if (gfc_match (" %l%t", &label) == MATCH_YES)
1569 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1572 new_st.op = EXEC_GOTO;
1573 new_st.label = label;
1577 /* The assigned GO TO statement. */
1579 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1581 if (gfc_notify_std (GFC_STD_F95_DEL,
1582 "Obsolete: Assigned GOTO statement at %C")
1586 new_st.op = EXEC_GOTO;
1589 if (gfc_match_eos () == MATCH_YES)
1592 /* Match label list. */
1593 gfc_match_char (',');
1594 if (gfc_match_char ('(') != MATCH_YES)
1596 gfc_syntax_error (ST_GOTO);
1603 m = gfc_match_st_label (&label);
1607 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1611 head = tail = gfc_get_code ();
1614 tail->block = gfc_get_code ();
1618 tail->label = label;
1619 tail->op = EXEC_GOTO;
1621 while (gfc_match_char (',') == MATCH_YES);
1623 if (gfc_match (")%t") != MATCH_YES)
1629 "Statement label list in GOTO at %C cannot be empty");
1632 new_st.block = head;
1637 /* Last chance is a computed GO TO statement. */
1638 if (gfc_match_char ('(') != MATCH_YES)
1640 gfc_syntax_error (ST_GOTO);
1649 m = gfc_match_st_label (&label);
1653 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1657 head = tail = gfc_get_code ();
1660 tail->block = gfc_get_code ();
1664 cp = gfc_get_case ();
1665 cp->low = cp->high = gfc_int_expr (i++);
1667 tail->op = EXEC_SELECT;
1668 tail->ext.case_list = cp;
1670 tail->next = gfc_get_code ();
1671 tail->next->op = EXEC_GOTO;
1672 tail->next->label = label;
1674 while (gfc_match_char (',') == MATCH_YES);
1676 if (gfc_match_char (')') != MATCH_YES)
1681 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1685 /* Get the rest of the statement. */
1686 gfc_match_char (',');
1688 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1691 /* At this point, a computed GOTO has been fully matched and an
1692 equivalent SELECT statement constructed. */
1694 new_st.op = EXEC_SELECT;
1697 /* Hack: For a "real" SELECT, the expression is in expr. We put
1698 it in expr2 so we can distinguish then and produce the correct
1700 new_st.expr2 = expr;
1701 new_st.block = head;
1705 gfc_syntax_error (ST_GOTO);
1707 gfc_free_statements (head);
1712 /* Frees a list of gfc_alloc structures. */
1715 gfc_free_alloc_list (gfc_alloc * p)
1722 gfc_free_expr (p->expr);
1728 /* Match an ALLOCATE statement. */
1731 gfc_match_allocate (void)
1733 gfc_alloc *head, *tail;
1740 if (gfc_match_char ('(') != MATCH_YES)
1746 head = tail = gfc_get_alloc ();
1749 tail->next = gfc_get_alloc ();
1753 m = gfc_match_variable (&tail->expr, 0);
1756 if (m == MATCH_ERROR)
1759 if (gfc_check_do_variable (tail->expr->symtree))
1763 && gfc_impure_variable (tail->expr->symtree->n.sym))
1765 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1770 if (gfc_match_char (',') != MATCH_YES)
1773 m = gfc_match (" stat = %v", &stat);
1774 if (m == MATCH_ERROR)
1782 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1785 ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1786 "INTENT(IN)", stat->symtree->n.sym->name);
1790 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1793 ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1798 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1800 gfc_error("STAT expression at %C must be a variable");
1804 gfc_check_do_variable(stat->symtree);
1807 if (gfc_match (" )%t") != MATCH_YES)
1810 new_st.op = EXEC_ALLOCATE;
1812 new_st.ext.alloc_list = head;
1817 gfc_syntax_error (ST_ALLOCATE);
1820 gfc_free_expr (stat);
1821 gfc_free_alloc_list (head);
1826 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1827 a set of pointer assignments to intrinsic NULL(). */
1830 gfc_match_nullify (void)
1838 if (gfc_match_char ('(') != MATCH_YES)
1843 m = gfc_match_variable (&p, 0);
1844 if (m == MATCH_ERROR)
1849 if (gfc_check_do_variable(p->symtree))
1852 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1855 ("Illegal variable in NULLIFY at %C for a PURE procedure");
1859 /* build ' => NULL() ' */
1860 e = gfc_get_expr ();
1861 e->where = gfc_current_locus;
1862 e->expr_type = EXPR_NULL;
1863 e->ts.type = BT_UNKNOWN;
1870 tail->next = gfc_get_code ();
1874 tail->op = EXEC_POINTER_ASSIGN;
1878 if (gfc_match (" )%t") == MATCH_YES)
1880 if (gfc_match_char (',') != MATCH_YES)
1887 gfc_syntax_error (ST_NULLIFY);
1890 gfc_free_statements (tail);
1895 /* Match a DEALLOCATE statement. */
1898 gfc_match_deallocate (void)
1900 gfc_alloc *head, *tail;
1907 if (gfc_match_char ('(') != MATCH_YES)
1913 head = tail = gfc_get_alloc ();
1916 tail->next = gfc_get_alloc ();
1920 m = gfc_match_variable (&tail->expr, 0);
1921 if (m == MATCH_ERROR)
1926 if (gfc_check_do_variable (tail->expr->symtree))
1930 && gfc_impure_variable (tail->expr->symtree->n.sym))
1933 ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1938 if (gfc_match_char (',') != MATCH_YES)
1941 m = gfc_match (" stat = %v", &stat);
1942 if (m == MATCH_ERROR)
1950 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1952 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1953 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
1957 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
1959 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
1960 "for a PURE procedure");
1964 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1966 gfc_error("STAT expression at %C must be a variable");
1970 gfc_check_do_variable(stat->symtree);
1973 if (gfc_match (" )%t") != MATCH_YES)
1976 new_st.op = EXEC_DEALLOCATE;
1978 new_st.ext.alloc_list = head;
1983 gfc_syntax_error (ST_DEALLOCATE);
1986 gfc_free_expr (stat);
1987 gfc_free_alloc_list (head);
1992 /* Match a RETURN statement. */
1995 gfc_match_return (void)
1999 gfc_compile_state s;
2003 if (gfc_match_eos () == MATCH_YES)
2006 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2008 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2013 if (gfc_current_form == FORM_FREE)
2015 /* The following are valid, so we can't require a blank after the
2019 c = gfc_peek_char ();
2020 if (ISALPHA (c) || ISDIGIT (c))
2024 m = gfc_match (" %e%t", &e);
2027 if (m == MATCH_ERROR)
2030 gfc_syntax_error (ST_RETURN);
2037 gfc_enclosing_unit (&s);
2038 if (s == COMP_PROGRAM
2039 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2040 "main program at %C") == FAILURE)
2043 new_st.op = EXEC_RETURN;
2050 /* Match a CALL statement. The tricky part here are possible
2051 alternate return specifiers. We handle these by having all
2052 "subroutines" actually return an integer via a register that gives
2053 the return number. If the call specifies alternate returns, we
2054 generate code for a SELECT statement whose case clauses contain
2055 GOTOs to the various labels. */
2058 gfc_match_call (void)
2060 char name[GFC_MAX_SYMBOL_LEN + 1];
2061 gfc_actual_arglist *a, *arglist;
2071 m = gfc_match ("% %n", name);
2077 if (gfc_get_ha_sym_tree (name, &st))
2081 gfc_set_sym_referenced (sym);
2083 if (!sym->attr.generic
2084 && !sym->attr.subroutine
2085 && gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2088 if (gfc_match_eos () != MATCH_YES)
2090 m = gfc_match_actual_arglist (1, &arglist);
2093 if (m == MATCH_ERROR)
2096 if (gfc_match_eos () != MATCH_YES)
2100 /* If any alternate return labels were found, construct a SELECT
2101 statement that will jump to the right place. */
2104 for (a = arglist; a; a = a->next)
2105 if (a->expr == NULL)
2110 gfc_symtree *select_st;
2111 gfc_symbol *select_sym;
2112 char name[GFC_MAX_SYMBOL_LEN + 1];
2114 new_st.next = c = gfc_get_code ();
2115 c->op = EXEC_SELECT;
2116 sprintf (name, "_result_%s",sym->name);
2117 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail */
2119 select_sym = select_st->n.sym;
2120 select_sym->ts.type = BT_INTEGER;
2121 select_sym->ts.kind = gfc_default_integer_kind;
2122 gfc_set_sym_referenced (select_sym);
2123 c->expr = gfc_get_expr ();
2124 c->expr->expr_type = EXPR_VARIABLE;
2125 c->expr->symtree = select_st;
2126 c->expr->ts = select_sym->ts;
2127 c->expr->where = gfc_current_locus;
2130 for (a = arglist; a; a = a->next)
2132 if (a->expr != NULL)
2135 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2140 c->block = gfc_get_code ();
2142 c->op = EXEC_SELECT;
2144 new_case = gfc_get_case ();
2145 new_case->high = new_case->low = gfc_int_expr (i);
2146 c->ext.case_list = new_case;
2148 c->next = gfc_get_code ();
2149 c->next->op = EXEC_GOTO;
2150 c->next->label = a->label;
2154 new_st.op = EXEC_CALL;
2155 new_st.symtree = st;
2156 new_st.ext.actual = arglist;
2161 gfc_syntax_error (ST_CALL);
2164 gfc_free_actual_arglist (arglist);
2169 /* Given a name, return a pointer to the common head structure,
2170 creating it if it does not exist. If FROM_MODULE is nonzero, we
2171 mangle the name so that it doesn't interfere with commons defined
2172 in the using namespace.
2173 TODO: Add to global symbol tree. */
2176 gfc_get_common (const char *name, int from_module)
2179 static int serial = 0;
2180 char mangled_name[GFC_MAX_SYMBOL_LEN+1];
2184 /* A use associated common block is only needed to correctly layout
2185 the variables it contains. */
2186 snprintf(mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2187 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2191 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2194 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2197 if (st->n.common == NULL)
2199 st->n.common = gfc_get_common_head ();
2200 st->n.common->where = gfc_current_locus;
2201 strcpy (st->n.common->name, name);
2204 return st->n.common;
2208 /* Match a common block name. */
2211 match_common_name (char *name)
2215 if (gfc_match_char ('/') == MATCH_NO)
2221 if (gfc_match_char ('/') == MATCH_YES)
2227 m = gfc_match_name (name);
2229 if (m == MATCH_ERROR)
2231 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2234 gfc_error ("Syntax error in common block name at %C");
2239 /* Match a COMMON statement. */
2242 gfc_match_common (void)
2244 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2245 char name[GFC_MAX_SYMBOL_LEN+1];
2248 gfc_equiv * e1, * e2;
2251 old_blank_common = gfc_current_ns->blank_common.head;
2252 if (old_blank_common)
2254 while (old_blank_common->common_next)
2255 old_blank_common = old_blank_common->common_next;
2262 m = match_common_name (name);
2263 if (m == MATCH_ERROR)
2266 if (name[0] == '\0')
2268 t = &gfc_current_ns->blank_common;
2269 if (t->head == NULL)
2270 t->where = gfc_current_locus;
2275 t = gfc_get_common (name, 0);
2284 while (tail->common_next)
2285 tail = tail->common_next;
2288 /* Grab the list of symbols. */
2291 m = gfc_match_symbol (&sym, 0);
2292 if (m == MATCH_ERROR)
2297 if (sym->attr.in_common)
2299 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2304 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2307 if (sym->value != NULL
2308 && (name[0] == '\0' || !sym->attr.data))
2310 if (name[0] == '\0')
2311 gfc_error ("Previously initialized symbol '%s' in "
2312 "blank COMMON block at %C", sym->name);
2314 gfc_error ("Previously initialized symbol '%s' in "
2315 "COMMON block '%s' at %C", sym->name, name);
2319 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2322 /* Derived type names must have the SEQUENCE attribute. */
2323 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2326 ("Derived type variable in COMMON at %C does not have the "
2327 "SEQUENCE attribute");
2332 tail->common_next = sym;
2338 /* Deal with an optional array specification after the
2340 m = gfc_match_array_spec (&as);
2341 if (m == MATCH_ERROR)
2346 if (as->type != AS_EXPLICIT)
2349 ("Array specification for symbol '%s' in COMMON at %C "
2350 "must be explicit", sym->name);
2354 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2357 if (sym->attr.pointer)
2360 ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2370 sym->common_head = t;
2372 /* Check to see if the symbol is already in an equivalence group.
2373 If it is, set the other members as being in common. */
2374 if (sym->attr.in_equivalence)
2376 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2378 for (e2 = e1; e2; e2 = e2->eq)
2379 if (e2->expr->symtree->n.sym == sym)
2386 for (e2 = e1; e2; e2 = e2->eq)
2388 other = e2->expr->symtree->n.sym;
2389 if (other->common_head
2390 && other->common_head != sym->common_head)
2392 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2393 "%C is being indirectly equivalenced to "
2394 "another COMMON block '%s'",
2396 sym->common_head->name,
2397 other->common_head->name);
2400 other->attr.in_common = 1;
2401 other->common_head = t;
2407 gfc_gobble_whitespace ();
2408 if (gfc_match_eos () == MATCH_YES)
2410 if (gfc_peek_char () == '/')
2412 if (gfc_match_char (',') != MATCH_YES)
2414 gfc_gobble_whitespace ();
2415 if (gfc_peek_char () == '/')
2424 gfc_syntax_error (ST_COMMON);
2427 if (old_blank_common)
2428 old_blank_common->common_next = NULL;
2430 gfc_current_ns->blank_common.head = NULL;
2431 gfc_free_array_spec (as);
2436 /* Match a BLOCK DATA program unit. */
2439 gfc_match_block_data (void)
2441 char name[GFC_MAX_SYMBOL_LEN + 1];
2445 if (gfc_match_eos () == MATCH_YES)
2447 gfc_new_block = NULL;
2451 m = gfc_match ("% %n%t", name);
2455 if (gfc_get_symbol (name, NULL, &sym))
2458 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2461 gfc_new_block = sym;
2467 /* Free a namelist structure. */
2470 gfc_free_namelist (gfc_namelist * name)
2474 for (; name; name = n)
2482 /* Match a NAMELIST statement. */
2485 gfc_match_namelist (void)
2487 gfc_symbol *group_name, *sym;
2491 m = gfc_match (" / %s /", &group_name);
2494 if (m == MATCH_ERROR)
2499 if (group_name->ts.type != BT_UNKNOWN)
2502 ("Namelist group name '%s' at %C already has a basic type "
2503 "of %s", group_name->name, gfc_typename (&group_name->ts));
2507 if (group_name->attr.flavor == FL_NAMELIST
2508 && group_name->attr.use_assoc
2509 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2510 "at %C already is USE associated and can"
2511 "not be respecified.", group_name->name)
2515 if (group_name->attr.flavor != FL_NAMELIST
2516 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2517 group_name->name, NULL) == FAILURE)
2522 m = gfc_match_symbol (&sym, 1);
2525 if (m == MATCH_ERROR)
2528 if (sym->attr.in_namelist == 0
2529 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2532 /* Use gfc_error_check here, rather than goto error, so that this
2533 these are the only errors for the next two lines. */
2534 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2536 gfc_error ("Assumed size array '%s' in namelist '%s'at "
2537 "%C is not allowed.", sym->name, group_name->name);
2541 if (sym->as && sym->as->type == AS_ASSUMED_SHAPE
2542 && gfc_notify_std (GFC_STD_GNU, "Assumed shape array '%s' in "
2543 "namelist '%s' at %C is an extension.",
2544 sym->name, group_name->name) == FAILURE)
2547 nl = gfc_get_namelist ();
2550 if (group_name->namelist == NULL)
2551 group_name->namelist = group_name->namelist_tail = nl;
2554 group_name->namelist_tail->next = nl;
2555 group_name->namelist_tail = nl;
2558 if (gfc_match_eos () == MATCH_YES)
2561 m = gfc_match_char (',');
2563 if (gfc_match_char ('/') == MATCH_YES)
2565 m2 = gfc_match (" %s /", &group_name);
2566 if (m2 == MATCH_YES)
2568 if (m2 == MATCH_ERROR)
2582 gfc_syntax_error (ST_NAMELIST);
2589 /* Match a MODULE statement. */
2592 gfc_match_module (void)
2596 m = gfc_match (" %s%t", &gfc_new_block);
2600 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2601 gfc_new_block->name, NULL) == FAILURE)
2608 /* Free equivalence sets and lists. Recursively is the easiest way to
2612 gfc_free_equiv (gfc_equiv * eq)
2618 gfc_free_equiv (eq->eq);
2619 gfc_free_equiv (eq->next);
2621 gfc_free_expr (eq->expr);
2626 /* Match an EQUIVALENCE statement. */
2629 gfc_match_equivalence (void)
2631 gfc_equiv *eq, *set, *tail;
2635 gfc_common_head *common_head = NULL;
2643 eq = gfc_get_equiv ();
2647 eq->next = gfc_current_ns->equiv;
2648 gfc_current_ns->equiv = eq;
2650 if (gfc_match_char ('(') != MATCH_YES)
2654 common_flag = FALSE;
2659 m = gfc_match_equiv_variable (&set->expr);
2660 if (m == MATCH_ERROR)
2665 /* count the number of objects. */
2668 if (gfc_match_char ('%') == MATCH_YES)
2670 gfc_error ("Derived type component %C is not a "
2671 "permitted EQUIVALENCE member");
2675 for (ref = set->expr->ref; ref; ref = ref->next)
2676 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2679 ("Array reference in EQUIVALENCE at %C cannot be an "
2684 sym = set->expr->symtree->n.sym;
2686 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL)
2690 if (sym->attr.in_common)
2693 common_head = sym->common_head;
2696 if (gfc_match_char (')') == MATCH_YES)
2699 if (gfc_match_char (',') != MATCH_YES)
2702 set->eq = gfc_get_equiv ();
2708 gfc_error ("EQUIVALENCE at %C requires two or more objects");
2712 /* If one of the members of an equivalence is in common, then
2713 mark them all as being in common. Before doing this, check
2714 that members of the equivalence group are not in different
2717 for (set = eq; set; set = set->eq)
2719 sym = set->expr->symtree->n.sym;
2720 if (sym->common_head && sym->common_head != common_head)
2722 gfc_error ("Attempt to indirectly overlap COMMON "
2723 "blocks %s and %s by EQUIVALENCE at %C",
2724 sym->common_head->name,
2728 sym->attr.in_common = 1;
2729 sym->common_head = common_head;
2732 if (gfc_match_eos () == MATCH_YES)
2734 if (gfc_match_char (',') != MATCH_YES)
2741 gfc_syntax_error (ST_EQUIVALENCE);
2747 gfc_free_equiv (gfc_current_ns->equiv);
2748 gfc_current_ns->equiv = eq;
2753 /* Check that a statement function is not recursive. This is done by looking
2754 for the statement function symbol(sym) by looking recursively through its
2755 expression(e). If a reference to sym is found, true is returned. */
2757 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
2759 gfc_actual_arglist *arg;
2766 switch (e->expr_type)
2769 for (arg = e->value.function.actual; arg; arg = arg->next)
2771 if (sym->name == arg->name
2772 || recursive_stmt_fcn (arg->expr, sym))
2776 if (e->symtree == NULL)
2779 /* Check the name before testing for nested recursion! */
2780 if (sym->name == e->symtree->n.sym->name)
2783 /* Catch recursion via other statement functions. */
2784 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
2785 && e->symtree->n.sym->value
2786 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
2792 if (e->symtree && sym->name == e->symtree->n.sym->name)
2797 if (recursive_stmt_fcn (e->value.op.op1, sym)
2798 || recursive_stmt_fcn (e->value.op.op2, sym))
2806 /* Component references do not need to be checked. */
2809 for (ref = e->ref; ref; ref = ref->next)
2814 for (i = 0; i < ref->u.ar.dimen; i++)
2816 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
2817 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
2818 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
2824 if (recursive_stmt_fcn (ref->u.ss.start, sym)
2825 || recursive_stmt_fcn (ref->u.ss.end, sym))
2839 /* Match a statement function declaration. It is so easy to match
2840 non-statement function statements with a MATCH_ERROR as opposed to
2841 MATCH_NO that we suppress error message in most cases. */
2844 gfc_match_st_function (void)
2846 gfc_error_buf old_error;
2851 m = gfc_match_symbol (&sym, 0);
2855 gfc_push_error (&old_error);
2857 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
2858 sym->name, NULL) == FAILURE)
2861 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2864 m = gfc_match (" = %e%t", &expr);
2868 gfc_free_error (&old_error);
2869 if (m == MATCH_ERROR)
2872 if (recursive_stmt_fcn (expr, sym))
2874 gfc_error ("Statement function at %L is recursive",
2884 gfc_pop_error (&old_error);
2889 /***************** SELECT CASE subroutines ******************/
2891 /* Free a single case structure. */
2894 free_case (gfc_case * p)
2896 if (p->low == p->high)
2898 gfc_free_expr (p->low);
2899 gfc_free_expr (p->high);
2904 /* Free a list of case structures. */
2907 gfc_free_case_list (gfc_case * p)
2919 /* Match a single case selector. */
2922 match_case_selector (gfc_case ** cp)
2927 c = gfc_get_case ();
2928 c->where = gfc_current_locus;
2930 if (gfc_match_char (':') == MATCH_YES)
2932 m = gfc_match_init_expr (&c->high);
2935 if (m == MATCH_ERROR)
2941 m = gfc_match_init_expr (&c->low);
2942 if (m == MATCH_ERROR)
2947 /* If we're not looking at a ':' now, make a range out of a single
2948 target. Else get the upper bound for the case range. */
2949 if (gfc_match_char (':') != MATCH_YES)
2953 m = gfc_match_init_expr (&c->high);
2954 if (m == MATCH_ERROR)
2956 /* MATCH_NO is fine. It's OK if nothing is there! */
2964 gfc_error ("Expected initialization expression in CASE at %C");
2972 /* Match the end of a case statement. */
2975 match_case_eos (void)
2977 char name[GFC_MAX_SYMBOL_LEN + 1];
2980 if (gfc_match_eos () == MATCH_YES)
2983 gfc_gobble_whitespace ();
2985 m = gfc_match_name (name);
2989 if (strcmp (name, gfc_current_block ()->name) != 0)
2991 gfc_error ("Expected case name of '%s' at %C",
2992 gfc_current_block ()->name);
2996 return gfc_match_eos ();
3000 /* Match a SELECT statement. */
3003 gfc_match_select (void)
3008 m = gfc_match_label ();
3009 if (m == MATCH_ERROR)
3012 m = gfc_match (" select case ( %e )%t", &expr);
3016 new_st.op = EXEC_SELECT;
3023 /* Match a CASE statement. */
3026 gfc_match_case (void)
3028 gfc_case *c, *head, *tail;
3033 if (gfc_current_state () != COMP_SELECT)
3035 gfc_error ("Unexpected CASE statement at %C");
3039 if (gfc_match ("% default") == MATCH_YES)
3041 m = match_case_eos ();
3044 if (m == MATCH_ERROR)
3047 new_st.op = EXEC_SELECT;
3048 c = gfc_get_case ();
3049 c->where = gfc_current_locus;
3050 new_st.ext.case_list = c;
3054 if (gfc_match_char ('(') != MATCH_YES)
3059 if (match_case_selector (&c) == MATCH_ERROR)
3069 if (gfc_match_char (')') == MATCH_YES)
3071 if (gfc_match_char (',') != MATCH_YES)
3075 m = match_case_eos ();
3078 if (m == MATCH_ERROR)
3081 new_st.op = EXEC_SELECT;
3082 new_st.ext.case_list = head;
3087 gfc_error ("Syntax error in CASE-specification at %C");
3090 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3094 /********************* WHERE subroutines ********************/
3096 /* Match the rest of a simple WHERE statement that follows an IF statement.
3100 match_simple_where (void)
3106 m = gfc_match (" ( %e )", &expr);
3110 m = gfc_match_assignment ();
3113 if (m == MATCH_ERROR)
3116 if (gfc_match_eos () != MATCH_YES)
3119 c = gfc_get_code ();
3123 c->next = gfc_get_code ();
3126 gfc_clear_new_st ();
3128 new_st.op = EXEC_WHERE;
3134 gfc_syntax_error (ST_WHERE);
3137 gfc_free_expr (expr);
3141 /* Match a WHERE statement. */
3144 gfc_match_where (gfc_statement * st)
3150 m0 = gfc_match_label ();
3151 if (m0 == MATCH_ERROR)
3154 m = gfc_match (" where ( %e )", &expr);
3158 if (gfc_match_eos () == MATCH_YES)
3160 *st = ST_WHERE_BLOCK;
3162 new_st.op = EXEC_WHERE;
3167 m = gfc_match_assignment ();
3169 gfc_syntax_error (ST_WHERE);
3173 gfc_free_expr (expr);
3177 /* We've got a simple WHERE statement. */
3179 c = gfc_get_code ();
3183 c->next = gfc_get_code ();
3186 gfc_clear_new_st ();
3188 new_st.op = EXEC_WHERE;
3195 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3196 new_st if successful. */
3199 gfc_match_elsewhere (void)
3201 char name[GFC_MAX_SYMBOL_LEN + 1];
3205 if (gfc_current_state () != COMP_WHERE)
3207 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3213 if (gfc_match_char ('(') == MATCH_YES)
3215 m = gfc_match_expr (&expr);
3218 if (m == MATCH_ERROR)
3221 if (gfc_match_char (')') != MATCH_YES)
3225 if (gfc_match_eos () != MATCH_YES)
3226 { /* Better be a name at this point */
3227 m = gfc_match_name (name);
3230 if (m == MATCH_ERROR)
3233 if (gfc_match_eos () != MATCH_YES)
3236 if (strcmp (name, gfc_current_block ()->name) != 0)
3238 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3239 name, gfc_current_block ()->name);
3244 new_st.op = EXEC_WHERE;
3249 gfc_syntax_error (ST_ELSEWHERE);
3252 gfc_free_expr (expr);
3257 /******************** FORALL subroutines ********************/
3259 /* Free a list of FORALL iterators. */
3262 gfc_free_forall_iterator (gfc_forall_iterator * iter)
3264 gfc_forall_iterator *next;
3270 gfc_free_expr (iter->var);
3271 gfc_free_expr (iter->start);
3272 gfc_free_expr (iter->end);
3273 gfc_free_expr (iter->stride);
3281 /* Match an iterator as part of a FORALL statement. The format is:
3283 <var> = <start>:<end>[:<stride>][, <scalar mask>] */
3286 match_forall_iterator (gfc_forall_iterator ** result)
3288 gfc_forall_iterator *iter;
3292 where = gfc_current_locus;
3293 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3295 m = gfc_match_variable (&iter->var, 0);
3299 if (gfc_match_char ('=') != MATCH_YES)
3305 m = gfc_match_expr (&iter->start);
3309 if (gfc_match_char (':') != MATCH_YES)
3312 m = gfc_match_expr (&iter->end);
3315 if (m == MATCH_ERROR)
3318 if (gfc_match_char (':') == MATCH_NO)
3319 iter->stride = gfc_int_expr (1);
3322 m = gfc_match_expr (&iter->stride);
3325 if (m == MATCH_ERROR)
3333 gfc_error ("Syntax error in FORALL iterator at %C");
3337 gfc_current_locus = where;
3338 gfc_free_forall_iterator (iter);
3343 /* Match the header of a FORALL statement. */
3346 match_forall_header (gfc_forall_iterator ** phead, gfc_expr ** mask)
3348 gfc_forall_iterator *head, *tail, *new;
3351 gfc_gobble_whitespace ();
3356 if (gfc_match_char ('(') != MATCH_YES)
3359 m = match_forall_iterator (&new);
3360 if (m == MATCH_ERROR)
3369 if (gfc_match_char (',') != MATCH_YES)
3372 m = match_forall_iterator (&new);
3373 if (m == MATCH_ERROR)
3382 /* Have to have a mask expression */
3384 m = gfc_match_expr (mask);
3387 if (m == MATCH_ERROR)
3393 if (gfc_match_char (')') == MATCH_NO)
3400 gfc_syntax_error (ST_FORALL);
3403 gfc_free_expr (*mask);
3404 gfc_free_forall_iterator (head);
3409 /* Match the rest of a simple FORALL statement that follows an IF statement.
3413 match_simple_forall (void)
3415 gfc_forall_iterator *head;
3424 m = match_forall_header (&head, &mask);
3431 m = gfc_match_assignment ();
3433 if (m == MATCH_ERROR)
3437 m = gfc_match_pointer_assignment ();
3438 if (m == MATCH_ERROR)
3444 c = gfc_get_code ();
3446 c->loc = gfc_current_locus;
3448 if (gfc_match_eos () != MATCH_YES)
3451 gfc_clear_new_st ();
3452 new_st.op = EXEC_FORALL;
3454 new_st.ext.forall_iterator = head;
3455 new_st.block = gfc_get_code ();
3457 new_st.block->op = EXEC_FORALL;
3458 new_st.block->next = c;
3463 gfc_syntax_error (ST_FORALL);
3466 gfc_free_forall_iterator (head);
3467 gfc_free_expr (mask);
3473 /* Match a FORALL statement. */
3476 gfc_match_forall (gfc_statement * st)
3478 gfc_forall_iterator *head;
3487 m0 = gfc_match_label ();
3488 if (m0 == MATCH_ERROR)
3491 m = gfc_match (" forall");
3495 m = match_forall_header (&head, &mask);
3496 if (m == MATCH_ERROR)
3501 if (gfc_match_eos () == MATCH_YES)
3503 *st = ST_FORALL_BLOCK;
3505 new_st.op = EXEC_FORALL;
3507 new_st.ext.forall_iterator = head;
3512 m = gfc_match_assignment ();
3513 if (m == MATCH_ERROR)
3517 m = gfc_match_pointer_assignment ();
3518 if (m == MATCH_ERROR)
3524 c = gfc_get_code ();
3527 if (gfc_match_eos () != MATCH_YES)
3530 gfc_clear_new_st ();
3531 new_st.op = EXEC_FORALL;
3533 new_st.ext.forall_iterator = head;
3534 new_st.block = gfc_get_code ();
3536 new_st.block->op = EXEC_FORALL;
3537 new_st.block->next = c;
3543 gfc_syntax_error (ST_FORALL);
3546 gfc_free_forall_iterator (head);
3547 gfc_free_expr (mask);
3548 gfc_free_statements (c);