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)
150 old_loc = gfc_current_locus;
152 gfc_gobble_whitespace ();
153 c = gfc_next_char ();
157 gfc_current_locus = old_loc;
165 old_loc = gfc_current_locus;
166 c = gfc_next_char ();
171 i = 10 * i + c - '0';
175 gfc_error ("Integer too large at %C");
180 gfc_current_locus = old_loc;
187 /* Match a small, constant integer expression, like in a kind
188 statement. On MATCH_YES, 'value' is set. */
191 gfc_match_small_int (int *value)
198 m = gfc_match_expr (&expr);
202 p = gfc_extract_int (expr, &i);
203 gfc_free_expr (expr);
216 /* Matches a statement label. Uses gfc_match_small_literal_int() to
217 do most of the work. */
220 gfc_match_st_label (gfc_st_label ** label)
226 old_loc = gfc_current_locus;
228 m = gfc_match_small_literal_int (&i);
232 if (i > 0 && i <= 99999)
234 *label = gfc_get_st_label (i);
239 gfc_error ("Statement label at %C is zero");
241 gfc_error ("Statement label at %C is out of range");
242 gfc_current_locus = old_loc;
247 /* Match and validate a label associated with a named IF, DO or SELECT
248 statement. If the symbol does not have the label attribute, we add
249 it. We also make sure the symbol does not refer to another
250 (active) block. A matched label is pointed to by gfc_new_block. */
253 gfc_match_label (void)
255 char name[GFC_MAX_SYMBOL_LEN + 1];
258 gfc_new_block = NULL;
260 m = gfc_match (" %n :", name);
264 if (gfc_get_symbol (name, NULL, &gfc_new_block))
266 gfc_error ("Label name '%s' at %C is ambiguous", name);
270 if (gfc_new_block->attr.flavor == FL_LABEL)
272 gfc_error ("Duplicate construct label '%s' at %C", name);
276 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
277 gfc_new_block->name, NULL) == FAILURE)
284 /* Try and match the input against an array of possibilities. If one
285 potential matching string is a substring of another, the longest
286 match takes precedence. Spaces in the target strings are optional
287 spaces that do not necessarily have to be found in the input
288 stream. In fixed mode, spaces never appear. If whitespace is
289 matched, it matches unlimited whitespace in the input. For this
290 reason, the 'mp' member of the mstring structure is used to track
291 the progress of each potential match.
293 If there is no match we return the tag associated with the
294 terminating NULL mstring structure and leave the locus pointer
295 where it started. If there is a match we return the tag member of
296 the matched mstring and leave the locus pointer after the matched
299 A '%' character is a mandatory space. */
302 gfc_match_strings (mstring * a)
304 mstring *p, *best_match;
305 int no_match, c, possibles;
310 for (p = a; p->string != NULL; p++)
319 match_loc = gfc_current_locus;
321 gfc_gobble_whitespace ();
323 while (possibles > 0)
325 c = gfc_next_char ();
327 /* Apply the next character to the current possibilities. */
328 for (p = a; p->string != NULL; p++)
335 /* Space matches 1+ whitespace(s). */
336 if ((gfc_current_form == FORM_FREE)
337 && gfc_is_whitespace (c))
355 match_loc = gfc_current_locus;
363 gfc_current_locus = match_loc;
365 return (best_match == NULL) ? no_match : best_match->tag;
369 /* See if the current input looks like a name of some sort. Modifies
370 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long. */
373 gfc_match_name (char *buffer)
378 old_loc = gfc_current_locus;
379 gfc_gobble_whitespace ();
381 c = gfc_next_char ();
384 gfc_current_locus = old_loc;
394 if (i > gfc_option.max_identifier_length)
396 gfc_error ("Name at %C is too long");
400 old_loc = gfc_current_locus;
401 c = gfc_next_char ();
405 || (gfc_option.flag_dollar_ok && c == '$'));
408 gfc_current_locus = old_loc;
414 /* Match a symbol on the input. Modifies the pointer to the symbol
415 pointer if successful. */
418 gfc_match_sym_tree (gfc_symtree ** matched_symbol, int host_assoc)
420 char buffer[GFC_MAX_SYMBOL_LEN + 1];
423 m = gfc_match_name (buffer);
428 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
429 ? MATCH_ERROR : MATCH_YES;
431 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
439 gfc_match_symbol (gfc_symbol ** matched_symbol, int host_assoc)
444 m = gfc_match_sym_tree (&st, host_assoc);
449 *matched_symbol = st->n.sym;
451 *matched_symbol = NULL;
454 *matched_symbol = NULL;
458 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
459 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
463 gfc_match_intrinsic_op (gfc_intrinsic_op * result)
467 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
469 if (op == INTRINSIC_NONE)
477 /* Match a loop control phrase:
479 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
481 If the final integer expression is not present, a constant unity
482 expression is returned. We don't return MATCH_ERROR until after
483 the equals sign is seen. */
486 gfc_match_iterator (gfc_iterator * iter, int init_flag)
488 char name[GFC_MAX_SYMBOL_LEN + 1];
489 gfc_expr *var, *e1, *e2, *e3;
493 /* Match the start of an iterator without affecting the symbol
496 start = gfc_current_locus;
497 m = gfc_match (" %n =", name);
498 gfc_current_locus = start;
503 m = gfc_match_variable (&var, 0);
507 gfc_match_char ('=');
511 if (var->ref != NULL)
513 gfc_error ("Loop variable at %C cannot be a sub-component");
517 if (var->symtree->n.sym->attr.intent == INTENT_IN)
519 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
520 var->symtree->n.sym->name);
524 if (var->symtree->n.sym->attr.pointer)
526 gfc_error ("Loop variable at %C cannot have the POINTER attribute");
530 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
533 if (m == MATCH_ERROR)
536 if (gfc_match_char (',') != MATCH_YES)
539 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
542 if (m == MATCH_ERROR)
545 if (gfc_match_char (',') != MATCH_YES)
547 e3 = gfc_int_expr (1);
551 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
552 if (m == MATCH_ERROR)
556 gfc_error ("Expected a step value in iterator at %C");
568 gfc_error ("Syntax error in iterator at %C");
579 /* Tries to match the next non-whitespace character on the input.
580 This subroutine does not return MATCH_ERROR. */
583 gfc_match_char (char c)
587 where = gfc_current_locus;
588 gfc_gobble_whitespace ();
590 if (gfc_next_char () == c)
593 gfc_current_locus = where;
598 /* General purpose matching subroutine. The target string is a
599 scanf-like format string in which spaces correspond to arbitrary
600 whitespace (including no whitespace), characters correspond to
601 themselves. The %-codes are:
603 %% Literal percent sign
604 %e Expression, pointer to a pointer is set
605 %s Symbol, pointer to the symbol is set
606 %n Name, character buffer is set to name
607 %t Matches end of statement.
608 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
609 %l Matches a statement label
610 %v Matches a variable expression (an lvalue)
611 % Matches a required space (in free form) and optional spaces. */
614 gfc_match (const char *target, ...)
616 gfc_st_label **label;
625 old_loc = gfc_current_locus;
626 va_start (argp, target);
636 gfc_gobble_whitespace ();
647 vp = va_arg (argp, void **);
648 n = gfc_match_expr ((gfc_expr **) vp);
659 vp = va_arg (argp, void **);
660 n = gfc_match_variable ((gfc_expr **) vp, 0);
671 vp = va_arg (argp, void **);
672 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
683 np = va_arg (argp, char *);
684 n = gfc_match_name (np);
695 label = va_arg (argp, gfc_st_label **);
696 n = gfc_match_st_label (label);
707 ip = va_arg (argp, int *);
708 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
719 if (gfc_match_eos () != MATCH_YES)
727 if (gfc_match_space () == MATCH_YES)
733 break; /* Fall through to character matcher */
736 gfc_internal_error ("gfc_match(): Bad match code %c", c);
740 if (c == gfc_next_char ())
750 /* Clean up after a failed match. */
751 gfc_current_locus = old_loc;
752 va_start (argp, target);
755 for (; matches > 0; matches--)
765 /* Matches that don't have to be undone */
770 (void)va_arg (argp, void **);
775 vp = va_arg (argp, void **);
789 /*********************** Statement level matching **********************/
791 /* Matches the start of a program unit, which is the program keyword
792 followed by an obligatory symbol. */
795 gfc_match_program (void)
800 m = gfc_match ("% %s%t", &sym);
804 gfc_error ("Invalid form of PROGRAM statement at %C");
808 if (m == MATCH_ERROR)
811 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
820 /* Match a simple assignment statement. */
823 gfc_match_assignment (void)
825 gfc_expr *lvalue, *rvalue;
829 old_loc = gfc_current_locus;
831 lvalue = rvalue = NULL;
832 m = gfc_match (" %v =", &lvalue);
836 if (lvalue->symtree->n.sym->attr.flavor == FL_PARAMETER)
838 gfc_error ("Cannot assign to a PARAMETER variable at %C");
843 m = gfc_match (" %e%t", &rvalue);
847 gfc_set_sym_referenced (lvalue->symtree->n.sym);
849 new_st.op = EXEC_ASSIGN;
850 new_st.expr = lvalue;
851 new_st.expr2 = rvalue;
853 gfc_check_do_variable (lvalue->symtree);
858 gfc_current_locus = old_loc;
859 gfc_free_expr (lvalue);
860 gfc_free_expr (rvalue);
865 /* Match a pointer assignment statement. */
868 gfc_match_pointer_assignment (void)
870 gfc_expr *lvalue, *rvalue;
874 old_loc = gfc_current_locus;
876 lvalue = rvalue = NULL;
878 m = gfc_match (" %v =>", &lvalue);
885 m = gfc_match (" %e%t", &rvalue);
889 new_st.op = EXEC_POINTER_ASSIGN;
890 new_st.expr = lvalue;
891 new_st.expr2 = rvalue;
896 gfc_current_locus = old_loc;
897 gfc_free_expr (lvalue);
898 gfc_free_expr (rvalue);
903 /* We try to match an easy arithmetic IF statement. This only happens
904 when just after having encountered a simple IF statement. This code
905 is really duplicate with parts of the gfc_match_if code, but this is
908 match_arithmetic_if (void)
910 gfc_st_label *l1, *l2, *l3;
914 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
918 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
919 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
920 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
922 gfc_free_expr (expr);
926 if (gfc_notify_std (GFC_STD_F95_DEL,
927 "Obsolete: arithmetic IF statement at %C") == FAILURE)
930 new_st.op = EXEC_ARITHMETIC_IF;
940 /* The IF statement is a bit of a pain. First of all, there are three
941 forms of it, the simple IF, the IF that starts a block and the
944 There is a problem with the simple IF and that is the fact that we
945 only have a single level of undo information on symbols. What this
946 means is for a simple IF, we must re-match the whole IF statement
947 multiple times in order to guarantee that the symbol table ends up
948 in the proper state. */
950 static match match_simple_forall (void);
951 static match match_simple_where (void);
954 gfc_match_if (gfc_statement * if_type)
957 gfc_st_label *l1, *l2, *l3;
962 n = gfc_match_label ();
963 if (n == MATCH_ERROR)
966 old_loc = gfc_current_locus;
968 m = gfc_match (" if ( %e", &expr);
972 if (gfc_match_char (')') != MATCH_YES)
974 gfc_error ("Syntax error in IF-expression at %C");
975 gfc_free_expr (expr);
979 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
986 ("Block label not appropriate for arithmetic IF statement "
989 gfc_free_expr (expr);
993 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
994 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
995 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
998 gfc_free_expr (expr);
1002 if (gfc_notify_std (GFC_STD_F95_DEL,
1003 "Obsolete: arithmetic IF statement at %C")
1007 new_st.op = EXEC_ARITHMETIC_IF;
1013 *if_type = ST_ARITHMETIC_IF;
1017 if (gfc_match (" then%t") == MATCH_YES)
1019 new_st.op = EXEC_IF;
1022 *if_type = ST_IF_BLOCK;
1028 gfc_error ("Block label is not appropriate IF statement at %C");
1030 gfc_free_expr (expr);
1034 /* At this point the only thing left is a simple IF statement. At
1035 this point, n has to be MATCH_NO, so we don't have to worry about
1036 re-matching a block label. From what we've got so far, try
1037 matching an assignment. */
1039 *if_type = ST_SIMPLE_IF;
1041 m = gfc_match_assignment ();
1045 gfc_free_expr (expr);
1046 gfc_undo_symbols ();
1047 gfc_current_locus = old_loc;
1049 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1051 m = gfc_match_pointer_assignment ();
1055 gfc_free_expr (expr);
1056 gfc_undo_symbols ();
1057 gfc_current_locus = old_loc;
1059 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1061 /* Look at the next keyword to see which matcher to call. Matching
1062 the keyword doesn't affect the symbol table, so we don't have to
1063 restore between tries. */
1065 #define match(string, subr, statement) \
1066 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1070 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1071 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1072 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1073 match ("call", gfc_match_call, ST_CALL)
1074 match ("close", gfc_match_close, ST_CLOSE)
1075 match ("continue", gfc_match_continue, ST_CONTINUE)
1076 match ("cycle", gfc_match_cycle, ST_CYCLE)
1077 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1078 match ("end file", gfc_match_endfile, ST_END_FILE)
1079 match ("exit", gfc_match_exit, ST_EXIT)
1080 match ("flush", gfc_match_flush, ST_FLUSH)
1081 match ("forall", match_simple_forall, ST_FORALL)
1082 match ("go to", gfc_match_goto, ST_GOTO)
1083 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1084 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1085 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1086 match ("open", gfc_match_open, ST_OPEN)
1087 match ("pause", gfc_match_pause, ST_NONE)
1088 match ("print", gfc_match_print, ST_WRITE)
1089 match ("read", gfc_match_read, ST_READ)
1090 match ("return", gfc_match_return, ST_RETURN)
1091 match ("rewind", gfc_match_rewind, ST_REWIND)
1092 match ("stop", gfc_match_stop, ST_STOP)
1093 match ("where", match_simple_where, ST_WHERE)
1094 match ("write", gfc_match_write, ST_WRITE)
1096 /* All else has failed, so give up. See if any of the matchers has
1097 stored an error message of some sort. */
1098 if (gfc_error_check () == 0)
1099 gfc_error ("Unclassifiable statement in IF-clause at %C");
1101 gfc_free_expr (expr);
1106 gfc_error ("Syntax error in IF-clause at %C");
1109 gfc_free_expr (expr);
1113 /* At this point, we've matched the single IF and the action clause
1114 is in new_st. Rearrange things so that the IF statement appears
1117 p = gfc_get_code ();
1118 p->next = gfc_get_code ();
1120 p->next->loc = gfc_current_locus;
1125 gfc_clear_new_st ();
1127 new_st.op = EXEC_IF;
1136 /* Match an ELSE statement. */
1139 gfc_match_else (void)
1141 char name[GFC_MAX_SYMBOL_LEN + 1];
1143 if (gfc_match_eos () == MATCH_YES)
1146 if (gfc_match_name (name) != MATCH_YES
1147 || gfc_current_block () == NULL
1148 || gfc_match_eos () != MATCH_YES)
1150 gfc_error ("Unexpected junk after ELSE statement at %C");
1154 if (strcmp (name, gfc_current_block ()->name) != 0)
1156 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1157 name, gfc_current_block ()->name);
1165 /* Match an ELSE IF statement. */
1168 gfc_match_elseif (void)
1170 char name[GFC_MAX_SYMBOL_LEN + 1];
1174 m = gfc_match (" ( %e ) then", &expr);
1178 if (gfc_match_eos () == MATCH_YES)
1181 if (gfc_match_name (name) != MATCH_YES
1182 || gfc_current_block () == NULL
1183 || gfc_match_eos () != MATCH_YES)
1185 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1189 if (strcmp (name, gfc_current_block ()->name) != 0)
1191 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1192 name, gfc_current_block ()->name);
1197 new_st.op = EXEC_IF;
1202 gfc_free_expr (expr);
1207 /* Free a gfc_iterator structure. */
1210 gfc_free_iterator (gfc_iterator * iter, int flag)
1216 gfc_free_expr (iter->var);
1217 gfc_free_expr (iter->start);
1218 gfc_free_expr (iter->end);
1219 gfc_free_expr (iter->step);
1226 /* Match a DO statement. */
1231 gfc_iterator iter, *ip;
1233 gfc_st_label *label;
1236 old_loc = gfc_current_locus;
1239 iter.var = iter.start = iter.end = iter.step = NULL;
1241 m = gfc_match_label ();
1242 if (m == MATCH_ERROR)
1245 if (gfc_match (" do") != MATCH_YES)
1248 m = gfc_match_st_label (&label);
1249 if (m == MATCH_ERROR)
1252 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1254 if (gfc_match_eos () == MATCH_YES)
1256 iter.end = gfc_logical_expr (1, NULL);
1257 new_st.op = EXEC_DO_WHILE;
1261 /* match an optional comma, if no comma is found a space is obligatory. */
1262 if (gfc_match_char(',') != MATCH_YES
1263 && gfc_match ("% ") != MATCH_YES)
1266 /* See if we have a DO WHILE. */
1267 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1269 new_st.op = EXEC_DO_WHILE;
1273 /* The abortive DO WHILE may have done something to the symbol
1274 table, so we start over: */
1275 gfc_undo_symbols ();
1276 gfc_current_locus = old_loc;
1278 gfc_match_label (); /* This won't error */
1279 gfc_match (" do "); /* This will work */
1281 gfc_match_st_label (&label); /* Can't error out */
1282 gfc_match_char (','); /* Optional comma */
1284 m = gfc_match_iterator (&iter, 0);
1287 if (m == MATCH_ERROR)
1290 gfc_check_do_variable (iter.var->symtree);
1292 if (gfc_match_eos () != MATCH_YES)
1294 gfc_syntax_error (ST_DO);
1298 new_st.op = EXEC_DO;
1302 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1305 new_st.label = label;
1307 if (new_st.op == EXEC_DO_WHILE)
1308 new_st.expr = iter.end;
1311 new_st.ext.iterator = ip = gfc_get_iterator ();
1318 gfc_free_iterator (&iter, 0);
1324 /* Match an EXIT or CYCLE statement. */
1327 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1333 if (gfc_match_eos () == MATCH_YES)
1337 m = gfc_match ("% %s%t", &sym);
1338 if (m == MATCH_ERROR)
1342 gfc_syntax_error (st);
1346 if (sym->attr.flavor != FL_LABEL)
1348 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1349 sym->name, gfc_ascii_statement (st));
1354 /* Find the loop mentioned specified by the label (or lack of a
1356 for (p = gfc_state_stack; p; p = p->previous)
1357 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1363 gfc_error ("%s statement at %C is not within a loop",
1364 gfc_ascii_statement (st));
1366 gfc_error ("%s statement at %C is not within loop '%s'",
1367 gfc_ascii_statement (st), sym->name);
1372 /* Save the first statement in the loop - needed by the backend. */
1373 new_st.ext.whichloop = p->head;
1376 /* new_st.sym = sym;*/
1382 /* Match the EXIT statement. */
1385 gfc_match_exit (void)
1388 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1392 /* Match the CYCLE statement. */
1395 gfc_match_cycle (void)
1398 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1402 /* Match a number or character constant after a STOP or PAUSE statement. */
1405 gfc_match_stopcode (gfc_statement st)
1414 if (gfc_match_eos () != MATCH_YES)
1416 m = gfc_match_small_literal_int (&stop_code);
1417 if (m == MATCH_ERROR)
1420 if (m == MATCH_YES && stop_code > 99999)
1422 gfc_error ("STOP code out of range at %C");
1428 /* Try a character constant. */
1429 m = gfc_match_expr (&e);
1430 if (m == MATCH_ERROR)
1434 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1438 if (gfc_match_eos () != MATCH_YES)
1442 if (gfc_pure (NULL))
1444 gfc_error ("%s statement not allowed in PURE procedure at %C",
1445 gfc_ascii_statement (st));
1449 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1451 new_st.ext.stop_code = stop_code;
1456 gfc_syntax_error (st);
1464 /* Match the (deprecated) PAUSE statement. */
1467 gfc_match_pause (void)
1471 m = gfc_match_stopcode (ST_PAUSE);
1474 if (gfc_notify_std (GFC_STD_F95_DEL,
1475 "Obsolete: PAUSE statement at %C")
1483 /* Match the STOP statement. */
1486 gfc_match_stop (void)
1488 return gfc_match_stopcode (ST_STOP);
1492 /* Match a CONTINUE statement. */
1495 gfc_match_continue (void)
1498 if (gfc_match_eos () != MATCH_YES)
1500 gfc_syntax_error (ST_CONTINUE);
1504 new_st.op = EXEC_CONTINUE;
1509 /* Match the (deprecated) ASSIGN statement. */
1512 gfc_match_assign (void)
1515 gfc_st_label *label;
1517 if (gfc_match (" %l", &label) == MATCH_YES)
1519 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1521 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1523 if (gfc_notify_std (GFC_STD_F95_DEL,
1524 "Obsolete: ASSIGN statement at %C")
1528 expr->symtree->n.sym->attr.assign = 1;
1530 new_st.op = EXEC_LABEL_ASSIGN;
1531 new_st.label = label;
1540 /* Match the GO TO statement. As a computed GOTO statement is
1541 matched, it is transformed into an equivalent SELECT block. No
1542 tree is necessary, and the resulting jumps-to-jumps are
1543 specifically optimized away by the back end. */
1546 gfc_match_goto (void)
1548 gfc_code *head, *tail;
1551 gfc_st_label *label;
1555 if (gfc_match (" %l%t", &label) == MATCH_YES)
1557 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1560 new_st.op = EXEC_GOTO;
1561 new_st.label = label;
1565 /* The assigned GO TO statement. */
1567 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1569 if (gfc_notify_std (GFC_STD_F95_DEL,
1570 "Obsolete: Assigned GOTO statement at %C")
1574 new_st.op = EXEC_GOTO;
1577 if (gfc_match_eos () == MATCH_YES)
1580 /* Match label list. */
1581 gfc_match_char (',');
1582 if (gfc_match_char ('(') != MATCH_YES)
1584 gfc_syntax_error (ST_GOTO);
1591 m = gfc_match_st_label (&label);
1595 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1599 head = tail = gfc_get_code ();
1602 tail->block = gfc_get_code ();
1606 tail->label = label;
1607 tail->op = EXEC_GOTO;
1609 while (gfc_match_char (',') == MATCH_YES);
1611 if (gfc_match (")%t") != MATCH_YES)
1617 "Statement label list in GOTO at %C cannot be empty");
1620 new_st.block = head;
1625 /* Last chance is a computed GO TO statement. */
1626 if (gfc_match_char ('(') != MATCH_YES)
1628 gfc_syntax_error (ST_GOTO);
1637 m = gfc_match_st_label (&label);
1641 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1645 head = tail = gfc_get_code ();
1648 tail->block = gfc_get_code ();
1652 cp = gfc_get_case ();
1653 cp->low = cp->high = gfc_int_expr (i++);
1655 tail->op = EXEC_SELECT;
1656 tail->ext.case_list = cp;
1658 tail->next = gfc_get_code ();
1659 tail->next->op = EXEC_GOTO;
1660 tail->next->label = label;
1662 while (gfc_match_char (',') == MATCH_YES);
1664 if (gfc_match_char (')') != MATCH_YES)
1669 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1673 /* Get the rest of the statement. */
1674 gfc_match_char (',');
1676 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1679 /* At this point, a computed GOTO has been fully matched and an
1680 equivalent SELECT statement constructed. */
1682 new_st.op = EXEC_SELECT;
1685 /* Hack: For a "real" SELECT, the expression is in expr. We put
1686 it in expr2 so we can distinguish then and produce the correct
1688 new_st.expr2 = expr;
1689 new_st.block = head;
1693 gfc_syntax_error (ST_GOTO);
1695 gfc_free_statements (head);
1700 /* Frees a list of gfc_alloc structures. */
1703 gfc_free_alloc_list (gfc_alloc * p)
1710 gfc_free_expr (p->expr);
1716 /* Match an ALLOCATE statement. */
1719 gfc_match_allocate (void)
1721 gfc_alloc *head, *tail;
1728 if (gfc_match_char ('(') != MATCH_YES)
1734 head = tail = gfc_get_alloc ();
1737 tail->next = gfc_get_alloc ();
1741 m = gfc_match_variable (&tail->expr, 0);
1744 if (m == MATCH_ERROR)
1747 if (gfc_check_do_variable (tail->expr->symtree))
1751 && gfc_impure_variable (tail->expr->symtree->n.sym))
1753 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1758 if (gfc_match_char (',') != MATCH_YES)
1761 m = gfc_match (" stat = %v", &stat);
1762 if (m == MATCH_ERROR)
1770 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1773 ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1774 "INTENT(IN)", stat->symtree->n.sym->name);
1778 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1781 ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1786 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1788 gfc_error("STAT expression at %C must be a variable");
1792 gfc_check_do_variable(stat->symtree);
1795 if (gfc_match (" )%t") != MATCH_YES)
1798 new_st.op = EXEC_ALLOCATE;
1800 new_st.ext.alloc_list = head;
1805 gfc_syntax_error (ST_ALLOCATE);
1808 gfc_free_expr (stat);
1809 gfc_free_alloc_list (head);
1814 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1815 a set of pointer assignments to intrinsic NULL(). */
1818 gfc_match_nullify (void)
1826 if (gfc_match_char ('(') != MATCH_YES)
1831 m = gfc_match_variable (&p, 0);
1832 if (m == MATCH_ERROR)
1837 if (gfc_check_do_variable(p->symtree))
1840 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1843 ("Illegal variable in NULLIFY at %C for a PURE procedure");
1847 /* build ' => NULL() ' */
1848 e = gfc_get_expr ();
1849 e->where = gfc_current_locus;
1850 e->expr_type = EXPR_NULL;
1851 e->ts.type = BT_UNKNOWN;
1858 tail->next = gfc_get_code ();
1862 tail->op = EXEC_POINTER_ASSIGN;
1866 if (gfc_match (" )%t") == MATCH_YES)
1868 if (gfc_match_char (',') != MATCH_YES)
1875 gfc_syntax_error (ST_NULLIFY);
1878 gfc_free_statements (tail);
1883 /* Match a DEALLOCATE statement. */
1886 gfc_match_deallocate (void)
1888 gfc_alloc *head, *tail;
1895 if (gfc_match_char ('(') != MATCH_YES)
1901 head = tail = gfc_get_alloc ();
1904 tail->next = gfc_get_alloc ();
1908 m = gfc_match_variable (&tail->expr, 0);
1909 if (m == MATCH_ERROR)
1914 if (gfc_check_do_variable (tail->expr->symtree))
1918 && gfc_impure_variable (tail->expr->symtree->n.sym))
1921 ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1926 if (gfc_match_char (',') != MATCH_YES)
1929 m = gfc_match (" stat = %v", &stat);
1930 if (m == MATCH_ERROR)
1938 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1940 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1941 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
1945 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
1947 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
1948 "for a PURE procedure");
1952 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1954 gfc_error("STAT expression at %C must be a variable");
1958 gfc_check_do_variable(stat->symtree);
1961 if (gfc_match (" )%t") != MATCH_YES)
1964 new_st.op = EXEC_DEALLOCATE;
1966 new_st.ext.alloc_list = head;
1971 gfc_syntax_error (ST_DEALLOCATE);
1974 gfc_free_expr (stat);
1975 gfc_free_alloc_list (head);
1980 /* Match a RETURN statement. */
1983 gfc_match_return (void)
1987 gfc_compile_state s;
1991 if (gfc_match_eos () == MATCH_YES)
1994 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
1996 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2001 if (gfc_current_form == FORM_FREE)
2003 /* The following are valid, so we can't require a blank after the
2007 c = gfc_peek_char ();
2008 if (ISALPHA (c) || ISDIGIT (c))
2012 m = gfc_match (" %e%t", &e);
2015 if (m == MATCH_ERROR)
2018 gfc_syntax_error (ST_RETURN);
2025 gfc_enclosing_unit (&s);
2026 if (s == COMP_PROGRAM
2027 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2028 "main program at %C") == FAILURE)
2031 new_st.op = EXEC_RETURN;
2038 /* Match a CALL statement. The tricky part here are possible
2039 alternate return specifiers. We handle these by having all
2040 "subroutines" actually return an integer via a register that gives
2041 the return number. If the call specifies alternate returns, we
2042 generate code for a SELECT statement whose case clauses contain
2043 GOTOs to the various labels. */
2046 gfc_match_call (void)
2048 char name[GFC_MAX_SYMBOL_LEN + 1];
2049 gfc_actual_arglist *a, *arglist;
2059 m = gfc_match ("% %n", name);
2065 if (gfc_get_ha_sym_tree (name, &st))
2069 gfc_set_sym_referenced (sym);
2071 if (!sym->attr.generic
2072 && !sym->attr.subroutine
2073 && gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2076 if (gfc_match_eos () != MATCH_YES)
2078 m = gfc_match_actual_arglist (1, &arglist);
2081 if (m == MATCH_ERROR)
2084 if (gfc_match_eos () != MATCH_YES)
2088 /* If any alternate return labels were found, construct a SELECT
2089 statement that will jump to the right place. */
2092 for (a = arglist; a; a = a->next)
2093 if (a->expr == NULL)
2098 gfc_symtree *select_st;
2099 gfc_symbol *select_sym;
2100 char name[GFC_MAX_SYMBOL_LEN + 1];
2102 new_st.next = c = gfc_get_code ();
2103 c->op = EXEC_SELECT;
2104 sprintf (name, "_result_%s",sym->name);
2105 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail */
2107 select_sym = select_st->n.sym;
2108 select_sym->ts.type = BT_INTEGER;
2109 select_sym->ts.kind = gfc_default_integer_kind;
2110 gfc_set_sym_referenced (select_sym);
2111 c->expr = gfc_get_expr ();
2112 c->expr->expr_type = EXPR_VARIABLE;
2113 c->expr->symtree = select_st;
2114 c->expr->ts = select_sym->ts;
2115 c->expr->where = gfc_current_locus;
2118 for (a = arglist; a; a = a->next)
2120 if (a->expr != NULL)
2123 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2128 c->block = gfc_get_code ();
2130 c->op = EXEC_SELECT;
2132 new_case = gfc_get_case ();
2133 new_case->high = new_case->low = gfc_int_expr (i);
2134 c->ext.case_list = new_case;
2136 c->next = gfc_get_code ();
2137 c->next->op = EXEC_GOTO;
2138 c->next->label = a->label;
2142 new_st.op = EXEC_CALL;
2143 new_st.symtree = st;
2144 new_st.ext.actual = arglist;
2149 gfc_syntax_error (ST_CALL);
2152 gfc_free_actual_arglist (arglist);
2157 /* Given a name, return a pointer to the common head structure,
2158 creating it if it does not exist. If FROM_MODULE is nonzero, we
2159 mangle the name so that it doesn't interfere with commons defined
2160 in the using namespace.
2161 TODO: Add to global symbol tree. */
2164 gfc_get_common (const char *name, int from_module)
2167 static int serial = 0;
2168 char mangled_name[GFC_MAX_SYMBOL_LEN+1];
2172 /* A use associated common block is only needed to correctly layout
2173 the variables it contains. */
2174 snprintf(mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2175 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2179 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2182 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2185 if (st->n.common == NULL)
2187 st->n.common = gfc_get_common_head ();
2188 st->n.common->where = gfc_current_locus;
2189 strcpy (st->n.common->name, name);
2192 return st->n.common;
2196 /* Match a common block name. */
2199 match_common_name (char *name)
2203 if (gfc_match_char ('/') == MATCH_NO)
2209 if (gfc_match_char ('/') == MATCH_YES)
2215 m = gfc_match_name (name);
2217 if (m == MATCH_ERROR)
2219 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2222 gfc_error ("Syntax error in common block name at %C");
2227 /* Match a COMMON statement. */
2230 gfc_match_common (void)
2232 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2233 char name[GFC_MAX_SYMBOL_LEN+1];
2236 gfc_equiv * e1, * e2;
2239 old_blank_common = gfc_current_ns->blank_common.head;
2240 if (old_blank_common)
2242 while (old_blank_common->common_next)
2243 old_blank_common = old_blank_common->common_next;
2250 m = match_common_name (name);
2251 if (m == MATCH_ERROR)
2254 if (name[0] == '\0')
2256 t = &gfc_current_ns->blank_common;
2257 if (t->head == NULL)
2258 t->where = gfc_current_locus;
2263 t = gfc_get_common (name, 0);
2272 while (tail->common_next)
2273 tail = tail->common_next;
2276 /* Grab the list of symbols. */
2279 m = gfc_match_symbol (&sym, 0);
2280 if (m == MATCH_ERROR)
2285 if (sym->attr.in_common)
2287 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2292 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2295 if (sym->value != NULL
2296 && (name[0] == '\0' || !sym->attr.data))
2298 if (name[0] == '\0')
2299 gfc_error ("Previously initialized symbol '%s' in "
2300 "blank COMMON block at %C", sym->name);
2302 gfc_error ("Previously initialized symbol '%s' in "
2303 "COMMON block '%s' at %C", sym->name, name);
2307 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2310 /* Derived type names must have the SEQUENCE attribute. */
2311 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2314 ("Derived type variable in COMMON at %C does not have the "
2315 "SEQUENCE attribute");
2320 tail->common_next = sym;
2326 /* Deal with an optional array specification after the
2328 m = gfc_match_array_spec (&as);
2329 if (m == MATCH_ERROR)
2334 if (as->type != AS_EXPLICIT)
2337 ("Array specification for symbol '%s' in COMMON at %C "
2338 "must be explicit", sym->name);
2342 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2345 if (sym->attr.pointer)
2348 ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2358 sym->common_head = t;
2360 /* Check to see if the symbol is already in an equivalence group.
2361 If it is, set the other members as being in common. */
2362 if (sym->attr.in_equivalence)
2364 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2366 for (e2 = e1; e2; e2 = e2->eq)
2367 if (e2->expr->symtree->n.sym == sym)
2374 for (e2 = e1; e2; e2 = e2->eq)
2376 other = e2->expr->symtree->n.sym;
2377 if (other->common_head
2378 && other->common_head != sym->common_head)
2380 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2381 "%C is being indirectly equivalenced to "
2382 "another COMMON block '%s'",
2384 sym->common_head->name,
2385 other->common_head->name);
2388 other->attr.in_common = 1;
2389 other->common_head = t;
2395 gfc_gobble_whitespace ();
2396 if (gfc_match_eos () == MATCH_YES)
2398 if (gfc_peek_char () == '/')
2400 if (gfc_match_char (',') != MATCH_YES)
2402 gfc_gobble_whitespace ();
2403 if (gfc_peek_char () == '/')
2412 gfc_syntax_error (ST_COMMON);
2415 if (old_blank_common)
2416 old_blank_common->common_next = NULL;
2418 gfc_current_ns->blank_common.head = NULL;
2419 gfc_free_array_spec (as);
2424 /* Match a BLOCK DATA program unit. */
2427 gfc_match_block_data (void)
2429 char name[GFC_MAX_SYMBOL_LEN + 1];
2433 if (gfc_match_eos () == MATCH_YES)
2435 gfc_new_block = NULL;
2439 m = gfc_match ("% %n%t", name);
2443 if (gfc_get_symbol (name, NULL, &sym))
2446 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2449 gfc_new_block = sym;
2455 /* Free a namelist structure. */
2458 gfc_free_namelist (gfc_namelist * name)
2462 for (; name; name = n)
2470 /* Match a NAMELIST statement. */
2473 gfc_match_namelist (void)
2475 gfc_symbol *group_name, *sym;
2479 m = gfc_match (" / %s /", &group_name);
2482 if (m == MATCH_ERROR)
2487 if (group_name->ts.type != BT_UNKNOWN)
2490 ("Namelist group name '%s' at %C already has a basic type "
2491 "of %s", group_name->name, gfc_typename (&group_name->ts));
2495 if (group_name->attr.flavor != FL_NAMELIST
2496 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2497 group_name->name, NULL) == FAILURE)
2502 m = gfc_match_symbol (&sym, 1);
2505 if (m == MATCH_ERROR)
2508 if (sym->attr.in_namelist == 0
2509 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2512 nl = gfc_get_namelist ();
2515 if (group_name->namelist == NULL)
2516 group_name->namelist = group_name->namelist_tail = nl;
2519 group_name->namelist_tail->next = nl;
2520 group_name->namelist_tail = nl;
2523 if (gfc_match_eos () == MATCH_YES)
2526 m = gfc_match_char (',');
2528 if (gfc_match_char ('/') == MATCH_YES)
2530 m2 = gfc_match (" %s /", &group_name);
2531 if (m2 == MATCH_YES)
2533 if (m2 == MATCH_ERROR)
2547 gfc_syntax_error (ST_NAMELIST);
2554 /* Match a MODULE statement. */
2557 gfc_match_module (void)
2561 m = gfc_match (" %s%t", &gfc_new_block);
2565 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2566 gfc_new_block->name, NULL) == FAILURE)
2573 /* Free equivalence sets and lists. Recursively is the easiest way to
2577 gfc_free_equiv (gfc_equiv * eq)
2583 gfc_free_equiv (eq->eq);
2584 gfc_free_equiv (eq->next);
2586 gfc_free_expr (eq->expr);
2591 /* Match an EQUIVALENCE statement. */
2594 gfc_match_equivalence (void)
2596 gfc_equiv *eq, *set, *tail;
2600 gfc_common_head *common_head = NULL;
2608 eq = gfc_get_equiv ();
2612 eq->next = gfc_current_ns->equiv;
2613 gfc_current_ns->equiv = eq;
2615 if (gfc_match_char ('(') != MATCH_YES)
2619 common_flag = FALSE;
2624 m = gfc_match_equiv_variable (&set->expr);
2625 if (m == MATCH_ERROR)
2630 /* count the number of objects. */
2633 if (gfc_match_char ('%') == MATCH_YES)
2635 gfc_error ("Derived type component %C is not a "
2636 "permitted EQUIVALENCE member");
2640 for (ref = set->expr->ref; ref; ref = ref->next)
2641 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2644 ("Array reference in EQUIVALENCE at %C cannot be an "
2649 sym = set->expr->symtree->n.sym;
2651 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL)
2655 if (sym->attr.in_common)
2658 common_head = sym->common_head;
2661 if (gfc_match_char (')') == MATCH_YES)
2664 if (gfc_match_char (',') != MATCH_YES)
2667 set->eq = gfc_get_equiv ();
2673 gfc_error ("EQUIVALENCE at %C requires two or more objects");
2677 /* If one of the members of an equivalence is in common, then
2678 mark them all as being in common. Before doing this, check
2679 that members of the equivalence group are not in different
2682 for (set = eq; set; set = set->eq)
2684 sym = set->expr->symtree->n.sym;
2685 if (sym->common_head && sym->common_head != common_head)
2687 gfc_error ("Attempt to indirectly overlap COMMON "
2688 "blocks %s and %s by EQUIVALENCE at %C",
2689 sym->common_head->name,
2693 sym->attr.in_common = 1;
2694 sym->common_head = common_head;
2697 if (gfc_match_eos () == MATCH_YES)
2699 if (gfc_match_char (',') != MATCH_YES)
2706 gfc_syntax_error (ST_EQUIVALENCE);
2712 gfc_free_equiv (gfc_current_ns->equiv);
2713 gfc_current_ns->equiv = eq;
2718 /* Check that a statement function is not recursive. This is done by looking
2719 for the statement function symbol(sym) by looking recursively through its
2720 expression(e). If a reference to sym is found, true is returned. */
2722 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
2724 gfc_actual_arglist *arg;
2731 switch (e->expr_type)
2734 for (arg = e->value.function.actual; arg; arg = arg->next)
2736 if (sym->name == arg->name
2737 || recursive_stmt_fcn (arg->expr, sym))
2741 if (e->symtree == NULL)
2744 /* Check the name before testing for nested recursion! */
2745 if (sym->name == e->symtree->n.sym->name)
2748 /* Catch recursion via other statement functions. */
2749 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
2750 && e->symtree->n.sym->value
2751 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
2757 if (e->symtree && sym->name == e->symtree->n.sym->name)
2762 if (recursive_stmt_fcn (e->value.op.op1, sym)
2763 || recursive_stmt_fcn (e->value.op.op2, sym))
2771 /* Component references do not need to be checked. */
2774 for (ref = e->ref; ref; ref = ref->next)
2779 for (i = 0; i < ref->u.ar.dimen; i++)
2781 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
2782 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
2783 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
2789 if (recursive_stmt_fcn (ref->u.ss.start, sym)
2790 || recursive_stmt_fcn (ref->u.ss.end, sym))
2804 /* Match a statement function declaration. It is so easy to match
2805 non-statement function statements with a MATCH_ERROR as opposed to
2806 MATCH_NO that we suppress error message in most cases. */
2809 gfc_match_st_function (void)
2811 gfc_error_buf old_error;
2816 m = gfc_match_symbol (&sym, 0);
2820 gfc_push_error (&old_error);
2822 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
2823 sym->name, NULL) == FAILURE)
2826 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2829 m = gfc_match (" = %e%t", &expr);
2833 gfc_free_error (&old_error);
2834 if (m == MATCH_ERROR)
2837 if (recursive_stmt_fcn (expr, sym))
2839 gfc_error ("Statement function at %L is recursive",
2849 gfc_pop_error (&old_error);
2854 /***************** SELECT CASE subroutines ******************/
2856 /* Free a single case structure. */
2859 free_case (gfc_case * p)
2861 if (p->low == p->high)
2863 gfc_free_expr (p->low);
2864 gfc_free_expr (p->high);
2869 /* Free a list of case structures. */
2872 gfc_free_case_list (gfc_case * p)
2884 /* Match a single case selector. */
2887 match_case_selector (gfc_case ** cp)
2892 c = gfc_get_case ();
2893 c->where = gfc_current_locus;
2895 if (gfc_match_char (':') == MATCH_YES)
2897 m = gfc_match_init_expr (&c->high);
2900 if (m == MATCH_ERROR)
2906 m = gfc_match_init_expr (&c->low);
2907 if (m == MATCH_ERROR)
2912 /* If we're not looking at a ':' now, make a range out of a single
2913 target. Else get the upper bound for the case range. */
2914 if (gfc_match_char (':') != MATCH_YES)
2918 m = gfc_match_init_expr (&c->high);
2919 if (m == MATCH_ERROR)
2921 /* MATCH_NO is fine. It's OK if nothing is there! */
2929 gfc_error ("Expected initialization expression in CASE at %C");
2937 /* Match the end of a case statement. */
2940 match_case_eos (void)
2942 char name[GFC_MAX_SYMBOL_LEN + 1];
2945 if (gfc_match_eos () == MATCH_YES)
2948 gfc_gobble_whitespace ();
2950 m = gfc_match_name (name);
2954 if (strcmp (name, gfc_current_block ()->name) != 0)
2956 gfc_error ("Expected case name of '%s' at %C",
2957 gfc_current_block ()->name);
2961 return gfc_match_eos ();
2965 /* Match a SELECT statement. */
2968 gfc_match_select (void)
2973 m = gfc_match_label ();
2974 if (m == MATCH_ERROR)
2977 m = gfc_match (" select case ( %e )%t", &expr);
2981 new_st.op = EXEC_SELECT;
2988 /* Match a CASE statement. */
2991 gfc_match_case (void)
2993 gfc_case *c, *head, *tail;
2998 if (gfc_current_state () != COMP_SELECT)
3000 gfc_error ("Unexpected CASE statement at %C");
3004 if (gfc_match ("% default") == MATCH_YES)
3006 m = match_case_eos ();
3009 if (m == MATCH_ERROR)
3012 new_st.op = EXEC_SELECT;
3013 c = gfc_get_case ();
3014 c->where = gfc_current_locus;
3015 new_st.ext.case_list = c;
3019 if (gfc_match_char ('(') != MATCH_YES)
3024 if (match_case_selector (&c) == MATCH_ERROR)
3034 if (gfc_match_char (')') == MATCH_YES)
3036 if (gfc_match_char (',') != MATCH_YES)
3040 m = match_case_eos ();
3043 if (m == MATCH_ERROR)
3046 new_st.op = EXEC_SELECT;
3047 new_st.ext.case_list = head;
3052 gfc_error ("Syntax error in CASE-specification at %C");
3055 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3059 /********************* WHERE subroutines ********************/
3061 /* Match the rest of a simple WHERE statement that follows an IF statement.
3065 match_simple_where (void)
3071 m = gfc_match (" ( %e )", &expr);
3075 m = gfc_match_assignment ();
3078 if (m == MATCH_ERROR)
3081 if (gfc_match_eos () != MATCH_YES)
3084 c = gfc_get_code ();
3088 c->next = gfc_get_code ();
3091 gfc_clear_new_st ();
3093 new_st.op = EXEC_WHERE;
3099 gfc_syntax_error (ST_WHERE);
3102 gfc_free_expr (expr);
3106 /* Match a WHERE statement. */
3109 gfc_match_where (gfc_statement * st)
3115 m0 = gfc_match_label ();
3116 if (m0 == MATCH_ERROR)
3119 m = gfc_match (" where ( %e )", &expr);
3123 if (gfc_match_eos () == MATCH_YES)
3125 *st = ST_WHERE_BLOCK;
3127 new_st.op = EXEC_WHERE;
3132 m = gfc_match_assignment ();
3134 gfc_syntax_error (ST_WHERE);
3138 gfc_free_expr (expr);
3142 /* We've got a simple WHERE statement. */
3144 c = gfc_get_code ();
3148 c->next = gfc_get_code ();
3151 gfc_clear_new_st ();
3153 new_st.op = EXEC_WHERE;
3160 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3161 new_st if successful. */
3164 gfc_match_elsewhere (void)
3166 char name[GFC_MAX_SYMBOL_LEN + 1];
3170 if (gfc_current_state () != COMP_WHERE)
3172 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3178 if (gfc_match_char ('(') == MATCH_YES)
3180 m = gfc_match_expr (&expr);
3183 if (m == MATCH_ERROR)
3186 if (gfc_match_char (')') != MATCH_YES)
3190 if (gfc_match_eos () != MATCH_YES)
3191 { /* Better be a name at this point */
3192 m = gfc_match_name (name);
3195 if (m == MATCH_ERROR)
3198 if (gfc_match_eos () != MATCH_YES)
3201 if (strcmp (name, gfc_current_block ()->name) != 0)
3203 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3204 name, gfc_current_block ()->name);
3209 new_st.op = EXEC_WHERE;
3214 gfc_syntax_error (ST_ELSEWHERE);
3217 gfc_free_expr (expr);
3222 /******************** FORALL subroutines ********************/
3224 /* Free a list of FORALL iterators. */
3227 gfc_free_forall_iterator (gfc_forall_iterator * iter)
3229 gfc_forall_iterator *next;
3235 gfc_free_expr (iter->var);
3236 gfc_free_expr (iter->start);
3237 gfc_free_expr (iter->end);
3238 gfc_free_expr (iter->stride);
3246 /* Match an iterator as part of a FORALL statement. The format is:
3248 <var> = <start>:<end>[:<stride>][, <scalar mask>] */
3251 match_forall_iterator (gfc_forall_iterator ** result)
3253 gfc_forall_iterator *iter;
3257 where = gfc_current_locus;
3258 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3260 m = gfc_match_variable (&iter->var, 0);
3264 if (gfc_match_char ('=') != MATCH_YES)
3270 m = gfc_match_expr (&iter->start);
3274 if (gfc_match_char (':') != MATCH_YES)
3277 m = gfc_match_expr (&iter->end);
3280 if (m == MATCH_ERROR)
3283 if (gfc_match_char (':') == MATCH_NO)
3284 iter->stride = gfc_int_expr (1);
3287 m = gfc_match_expr (&iter->stride);
3290 if (m == MATCH_ERROR)
3298 gfc_error ("Syntax error in FORALL iterator at %C");
3302 gfc_current_locus = where;
3303 gfc_free_forall_iterator (iter);
3308 /* Match the header of a FORALL statement. */
3311 match_forall_header (gfc_forall_iterator ** phead, gfc_expr ** mask)
3313 gfc_forall_iterator *head, *tail, *new;
3316 gfc_gobble_whitespace ();
3321 if (gfc_match_char ('(') != MATCH_YES)
3324 m = match_forall_iterator (&new);
3325 if (m == MATCH_ERROR)
3334 if (gfc_match_char (',') != MATCH_YES)
3337 m = match_forall_iterator (&new);
3338 if (m == MATCH_ERROR)
3347 /* Have to have a mask expression */
3349 m = gfc_match_expr (mask);
3352 if (m == MATCH_ERROR)
3358 if (gfc_match_char (')') == MATCH_NO)
3365 gfc_syntax_error (ST_FORALL);
3368 gfc_free_expr (*mask);
3369 gfc_free_forall_iterator (head);
3374 /* Match the rest of a simple FORALL statement that follows an IF statement.
3378 match_simple_forall (void)
3380 gfc_forall_iterator *head;
3389 m = match_forall_header (&head, &mask);
3396 m = gfc_match_assignment ();
3398 if (m == MATCH_ERROR)
3402 m = gfc_match_pointer_assignment ();
3403 if (m == MATCH_ERROR)
3409 c = gfc_get_code ();
3411 c->loc = gfc_current_locus;
3413 if (gfc_match_eos () != MATCH_YES)
3416 gfc_clear_new_st ();
3417 new_st.op = EXEC_FORALL;
3419 new_st.ext.forall_iterator = head;
3420 new_st.block = gfc_get_code ();
3422 new_st.block->op = EXEC_FORALL;
3423 new_st.block->next = c;
3428 gfc_syntax_error (ST_FORALL);
3431 gfc_free_forall_iterator (head);
3432 gfc_free_expr (mask);
3438 /* Match a FORALL statement. */
3441 gfc_match_forall (gfc_statement * st)
3443 gfc_forall_iterator *head;
3452 m0 = gfc_match_label ();
3453 if (m0 == MATCH_ERROR)
3456 m = gfc_match (" forall");
3460 m = match_forall_header (&head, &mask);
3461 if (m == MATCH_ERROR)
3466 if (gfc_match_eos () == MATCH_YES)
3468 *st = ST_FORALL_BLOCK;
3470 new_st.op = EXEC_FORALL;
3472 new_st.ext.forall_iterator = head;
3477 m = gfc_match_assignment ();
3478 if (m == MATCH_ERROR)
3482 m = gfc_match_pointer_assignment ();
3483 if (m == MATCH_ERROR)
3489 c = gfc_get_code ();
3492 if (gfc_match_eos () != MATCH_YES)
3495 gfc_clear_new_st ();
3496 new_st.op = EXEC_FORALL;
3498 new_st.ext.forall_iterator = head;
3499 new_st.block = gfc_get_code ();
3501 new_st.block->op = EXEC_FORALL;
3502 new_st.block->next = c;
3508 gfc_syntax_error (ST_FORALL);
3511 gfc_free_forall_iterator (head);
3512 gfc_free_expr (mask);
3513 gfc_free_statements (c);