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, int allow_zero)
226 old_loc = gfc_current_locus;
228 m = gfc_match_small_literal_int (&i);
232 if (((i == 0) && allow_zero) || i <= 99999)
234 *label = gfc_get_st_label (i);
238 gfc_error ("Statement label at %C is out of range");
239 gfc_current_locus = old_loc;
244 /* Match and validate a label associated with a named IF, DO or SELECT
245 statement. If the symbol does not have the label attribute, we add
246 it. We also make sure the symbol does not refer to another
247 (active) block. A matched label is pointed to by gfc_new_block. */
250 gfc_match_label (void)
252 char name[GFC_MAX_SYMBOL_LEN + 1];
255 gfc_new_block = NULL;
257 m = gfc_match (" %n :", name);
261 if (gfc_get_symbol (name, NULL, &gfc_new_block))
263 gfc_error ("Label name '%s' at %C is ambiguous", name);
267 if (gfc_new_block->attr.flavor == FL_LABEL)
269 gfc_error ("Duplicate construct label '%s' at %C", name);
273 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
274 gfc_new_block->name, NULL) == FAILURE)
281 /* Try and match the input against an array of possibilities. If one
282 potential matching string is a substring of another, the longest
283 match takes precedence. Spaces in the target strings are optional
284 spaces that do not necessarily have to be found in the input
285 stream. In fixed mode, spaces never appear. If whitespace is
286 matched, it matches unlimited whitespace in the input. For this
287 reason, the 'mp' member of the mstring structure is used to track
288 the progress of each potential match.
290 If there is no match we return the tag associated with the
291 terminating NULL mstring structure and leave the locus pointer
292 where it started. If there is a match we return the tag member of
293 the matched mstring and leave the locus pointer after the matched
296 A '%' character is a mandatory space. */
299 gfc_match_strings (mstring * a)
301 mstring *p, *best_match;
302 int no_match, c, possibles;
307 for (p = a; p->string != NULL; p++)
316 match_loc = gfc_current_locus;
318 gfc_gobble_whitespace ();
320 while (possibles > 0)
322 c = gfc_next_char ();
324 /* Apply the next character to the current possibilities. */
325 for (p = a; p->string != NULL; p++)
332 /* Space matches 1+ whitespace(s). */
333 if ((gfc_current_form == FORM_FREE)
334 && gfc_is_whitespace (c))
352 match_loc = gfc_current_locus;
360 gfc_current_locus = match_loc;
362 return (best_match == NULL) ? no_match : best_match->tag;
366 /* See if the current input looks like a name of some sort. Modifies
367 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long. */
370 gfc_match_name (char *buffer)
375 old_loc = gfc_current_locus;
376 gfc_gobble_whitespace ();
378 c = gfc_next_char ();
381 gfc_current_locus = old_loc;
391 if (i > gfc_option.max_identifier_length)
393 gfc_error ("Name at %C is too long");
397 old_loc = gfc_current_locus;
398 c = gfc_next_char ();
402 || (gfc_option.flag_dollar_ok && c == '$'));
405 gfc_current_locus = old_loc;
411 /* Match a symbol on the input. Modifies the pointer to the symbol
412 pointer if successful. */
415 gfc_match_sym_tree (gfc_symtree ** matched_symbol, int host_assoc)
417 char buffer[GFC_MAX_SYMBOL_LEN + 1];
420 m = gfc_match_name (buffer);
425 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
426 ? MATCH_ERROR : MATCH_YES;
428 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
436 gfc_match_symbol (gfc_symbol ** matched_symbol, int host_assoc)
441 m = gfc_match_sym_tree (&st, host_assoc);
446 *matched_symbol = st->n.sym;
448 *matched_symbol = NULL;
451 *matched_symbol = NULL;
455 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
456 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
460 gfc_match_intrinsic_op (gfc_intrinsic_op * result)
464 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
466 if (op == INTRINSIC_NONE)
474 /* Match a loop control phrase:
476 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
478 If the final integer expression is not present, a constant unity
479 expression is returned. We don't return MATCH_ERROR until after
480 the equals sign is seen. */
483 gfc_match_iterator (gfc_iterator * iter, int init_flag)
485 char name[GFC_MAX_SYMBOL_LEN + 1];
486 gfc_expr *var, *e1, *e2, *e3;
490 /* Match the start of an iterator without affecting the symbol
493 start = gfc_current_locus;
494 m = gfc_match (" %n =", name);
495 gfc_current_locus = start;
500 m = gfc_match_variable (&var, 0);
504 gfc_match_char ('=');
508 if (var->ref != NULL)
510 gfc_error ("Loop variable at %C cannot be a sub-component");
514 if (var->symtree->n.sym->attr.intent == INTENT_IN)
516 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
517 var->symtree->n.sym->name);
521 if (var->symtree->n.sym->attr.pointer)
523 gfc_error ("Loop variable at %C cannot have the POINTER attribute");
527 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
530 if (m == MATCH_ERROR)
533 if (gfc_match_char (',') != MATCH_YES)
536 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
539 if (m == MATCH_ERROR)
542 if (gfc_match_char (',') != MATCH_YES)
544 e3 = gfc_int_expr (1);
548 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
549 if (m == MATCH_ERROR)
553 gfc_error ("Expected a step value in iterator at %C");
565 gfc_error ("Syntax error in iterator at %C");
576 /* Tries to match the next non-whitespace character on the input.
577 This subroutine does not return MATCH_ERROR. */
580 gfc_match_char (char c)
584 where = gfc_current_locus;
585 gfc_gobble_whitespace ();
587 if (gfc_next_char () == c)
590 gfc_current_locus = where;
595 /* General purpose matching subroutine. The target string is a
596 scanf-like format string in which spaces correspond to arbitrary
597 whitespace (including no whitespace), characters correspond to
598 themselves. The %-codes are:
600 %% Literal percent sign
601 %e Expression, pointer to a pointer is set
602 %s Symbol, pointer to the symbol is set
603 %n Name, character buffer is set to name
604 %t Matches end of statement.
605 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
606 %l Matches a statement label
607 %v Matches a variable expression (an lvalue)
608 % Matches a required space (in free form) and optional spaces. */
611 gfc_match (const char *target, ...)
613 gfc_st_label **label;
622 old_loc = gfc_current_locus;
623 va_start (argp, target);
633 gfc_gobble_whitespace ();
644 vp = va_arg (argp, void **);
645 n = gfc_match_expr ((gfc_expr **) vp);
656 vp = va_arg (argp, void **);
657 n = gfc_match_variable ((gfc_expr **) vp, 0);
668 vp = va_arg (argp, void **);
669 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
680 np = va_arg (argp, char *);
681 n = gfc_match_name (np);
692 label = va_arg (argp, gfc_st_label **);
693 n = gfc_match_st_label (label, 0);
704 ip = va_arg (argp, int *);
705 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
716 if (gfc_match_eos () != MATCH_YES)
724 if (gfc_match_space () == MATCH_YES)
730 break; /* Fall through to character matcher */
733 gfc_internal_error ("gfc_match(): Bad match code %c", c);
737 if (c == gfc_next_char ())
747 /* Clean up after a failed match. */
748 gfc_current_locus = old_loc;
749 va_start (argp, target);
752 for (; matches > 0; matches--)
762 /* Matches that don't have to be undone */
767 (void)va_arg (argp, void **);
772 vp = va_arg (argp, void **);
786 /*********************** Statement level matching **********************/
788 /* Matches the start of a program unit, which is the program keyword
789 followed by an obligatory symbol. */
792 gfc_match_program (void)
797 m = gfc_match ("% %s%t", &sym);
801 gfc_error ("Invalid form of PROGRAM statement at %C");
805 if (m == MATCH_ERROR)
808 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
817 /* Match a simple assignment statement. */
820 gfc_match_assignment (void)
822 gfc_expr *lvalue, *rvalue;
826 old_loc = gfc_current_locus;
828 lvalue = rvalue = NULL;
829 m = gfc_match (" %v =", &lvalue);
833 if (lvalue->symtree->n.sym->attr.flavor == FL_PARAMETER)
835 gfc_error ("Cannot assign to a PARAMETER variable at %C");
840 m = gfc_match (" %e%t", &rvalue);
844 gfc_set_sym_referenced (lvalue->symtree->n.sym);
846 new_st.op = EXEC_ASSIGN;
847 new_st.expr = lvalue;
848 new_st.expr2 = rvalue;
850 gfc_check_do_variable (lvalue->symtree);
855 gfc_current_locus = old_loc;
856 gfc_free_expr (lvalue);
857 gfc_free_expr (rvalue);
862 /* Match a pointer assignment statement. */
865 gfc_match_pointer_assignment (void)
867 gfc_expr *lvalue, *rvalue;
871 old_loc = gfc_current_locus;
873 lvalue = rvalue = NULL;
875 m = gfc_match (" %v =>", &lvalue);
882 m = gfc_match (" %e%t", &rvalue);
886 new_st.op = EXEC_POINTER_ASSIGN;
887 new_st.expr = lvalue;
888 new_st.expr2 = rvalue;
893 gfc_current_locus = old_loc;
894 gfc_free_expr (lvalue);
895 gfc_free_expr (rvalue);
900 /* We try to match an easy arithmetic IF statement. This only happens
901 when just after having encountered a simple IF statement. This code
902 is really duplicate with parts of the gfc_match_if code, but this is
905 match_arithmetic_if (void)
907 gfc_st_label *l1, *l2, *l3;
911 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
915 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
916 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
917 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
919 gfc_free_expr (expr);
923 if (gfc_notify_std (GFC_STD_F95_DEL,
924 "Obsolete: arithmetic IF statement at %C") == FAILURE)
927 new_st.op = EXEC_ARITHMETIC_IF;
937 /* The IF statement is a bit of a pain. First of all, there are three
938 forms of it, the simple IF, the IF that starts a block and the
941 There is a problem with the simple IF and that is the fact that we
942 only have a single level of undo information on symbols. What this
943 means is for a simple IF, we must re-match the whole IF statement
944 multiple times in order to guarantee that the symbol table ends up
945 in the proper state. */
947 static match match_simple_forall (void);
948 static match match_simple_where (void);
951 gfc_match_if (gfc_statement * if_type)
954 gfc_st_label *l1, *l2, *l3;
959 n = gfc_match_label ();
960 if (n == MATCH_ERROR)
963 old_loc = gfc_current_locus;
965 m = gfc_match (" if ( %e", &expr);
969 if (gfc_match_char (')') != MATCH_YES)
971 gfc_error ("Syntax error in IF-expression at %C");
972 gfc_free_expr (expr);
976 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
983 ("Block label not appropriate for arithmetic IF statement "
986 gfc_free_expr (expr);
990 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
991 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
992 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
995 gfc_free_expr (expr);
999 if (gfc_notify_std (GFC_STD_F95_DEL,
1000 "Obsolete: arithmetic IF statement at %C")
1004 new_st.op = EXEC_ARITHMETIC_IF;
1010 *if_type = ST_ARITHMETIC_IF;
1014 if (gfc_match (" then%t") == MATCH_YES)
1016 new_st.op = EXEC_IF;
1019 *if_type = ST_IF_BLOCK;
1025 gfc_error ("Block label is not appropriate IF statement at %C");
1027 gfc_free_expr (expr);
1031 /* At this point the only thing left is a simple IF statement. At
1032 this point, n has to be MATCH_NO, so we don't have to worry about
1033 re-matching a block label. From what we've got so far, try
1034 matching an assignment. */
1036 *if_type = ST_SIMPLE_IF;
1038 m = gfc_match_assignment ();
1042 gfc_free_expr (expr);
1043 gfc_undo_symbols ();
1044 gfc_current_locus = old_loc;
1046 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1048 m = gfc_match_pointer_assignment ();
1052 gfc_free_expr (expr);
1053 gfc_undo_symbols ();
1054 gfc_current_locus = old_loc;
1056 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1058 /* Look at the next keyword to see which matcher to call. Matching
1059 the keyword doesn't affect the symbol table, so we don't have to
1060 restore between tries. */
1062 #define match(string, subr, statement) \
1063 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1067 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1068 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1069 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1070 match ("call", gfc_match_call, ST_CALL)
1071 match ("close", gfc_match_close, ST_CLOSE)
1072 match ("continue", gfc_match_continue, ST_CONTINUE)
1073 match ("cycle", gfc_match_cycle, ST_CYCLE)
1074 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1075 match ("end file", gfc_match_endfile, ST_END_FILE)
1076 match ("exit", gfc_match_exit, ST_EXIT)
1077 match ("flush", gfc_match_flush, ST_FLUSH)
1078 match ("forall", match_simple_forall, ST_FORALL)
1079 match ("go to", gfc_match_goto, ST_GOTO)
1080 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1081 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1082 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1083 match ("open", gfc_match_open, ST_OPEN)
1084 match ("pause", gfc_match_pause, ST_NONE)
1085 match ("print", gfc_match_print, ST_WRITE)
1086 match ("read", gfc_match_read, ST_READ)
1087 match ("return", gfc_match_return, ST_RETURN)
1088 match ("rewind", gfc_match_rewind, ST_REWIND)
1089 match ("stop", gfc_match_stop, ST_STOP)
1090 match ("where", match_simple_where, ST_WHERE)
1091 match ("write", gfc_match_write, ST_WRITE)
1093 /* All else has failed, so give up. See if any of the matchers has
1094 stored an error message of some sort. */
1095 if (gfc_error_check () == 0)
1096 gfc_error ("Unclassifiable statement in IF-clause at %C");
1098 gfc_free_expr (expr);
1103 gfc_error ("Syntax error in IF-clause at %C");
1106 gfc_free_expr (expr);
1110 /* At this point, we've matched the single IF and the action clause
1111 is in new_st. Rearrange things so that the IF statement appears
1114 p = gfc_get_code ();
1115 p->next = gfc_get_code ();
1117 p->next->loc = gfc_current_locus;
1122 gfc_clear_new_st ();
1124 new_st.op = EXEC_IF;
1133 /* Match an ELSE statement. */
1136 gfc_match_else (void)
1138 char name[GFC_MAX_SYMBOL_LEN + 1];
1140 if (gfc_match_eos () == MATCH_YES)
1143 if (gfc_match_name (name) != MATCH_YES
1144 || gfc_current_block () == NULL
1145 || gfc_match_eos () != MATCH_YES)
1147 gfc_error ("Unexpected junk after ELSE statement at %C");
1151 if (strcmp (name, gfc_current_block ()->name) != 0)
1153 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1154 name, gfc_current_block ()->name);
1162 /* Match an ELSE IF statement. */
1165 gfc_match_elseif (void)
1167 char name[GFC_MAX_SYMBOL_LEN + 1];
1171 m = gfc_match (" ( %e ) then", &expr);
1175 if (gfc_match_eos () == MATCH_YES)
1178 if (gfc_match_name (name) != MATCH_YES
1179 || gfc_current_block () == NULL
1180 || gfc_match_eos () != MATCH_YES)
1182 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1186 if (strcmp (name, gfc_current_block ()->name) != 0)
1188 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1189 name, gfc_current_block ()->name);
1194 new_st.op = EXEC_IF;
1199 gfc_free_expr (expr);
1204 /* Free a gfc_iterator structure. */
1207 gfc_free_iterator (gfc_iterator * iter, int flag)
1213 gfc_free_expr (iter->var);
1214 gfc_free_expr (iter->start);
1215 gfc_free_expr (iter->end);
1216 gfc_free_expr (iter->step);
1223 /* Match a DO statement. */
1228 gfc_iterator iter, *ip;
1230 gfc_st_label *label;
1233 old_loc = gfc_current_locus;
1236 iter.var = iter.start = iter.end = iter.step = NULL;
1238 m = gfc_match_label ();
1239 if (m == MATCH_ERROR)
1242 if (gfc_match (" do") != MATCH_YES)
1245 m = gfc_match_st_label (&label, 0);
1246 if (m == MATCH_ERROR)
1249 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1251 if (gfc_match_eos () == MATCH_YES)
1253 iter.end = gfc_logical_expr (1, NULL);
1254 new_st.op = EXEC_DO_WHILE;
1258 /* match an optional comma, if no comma is found a space is obligatory. */
1259 if (gfc_match_char(',') != MATCH_YES
1260 && gfc_match ("% ") != MATCH_YES)
1263 /* See if we have a DO WHILE. */
1264 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1266 new_st.op = EXEC_DO_WHILE;
1270 /* The abortive DO WHILE may have done something to the symbol
1271 table, so we start over: */
1272 gfc_undo_symbols ();
1273 gfc_current_locus = old_loc;
1275 gfc_match_label (); /* This won't error */
1276 gfc_match (" do "); /* This will work */
1278 gfc_match_st_label (&label, 0); /* Can't error out */
1279 gfc_match_char (','); /* Optional comma */
1281 m = gfc_match_iterator (&iter, 0);
1284 if (m == MATCH_ERROR)
1287 gfc_check_do_variable (iter.var->symtree);
1289 if (gfc_match_eos () != MATCH_YES)
1291 gfc_syntax_error (ST_DO);
1295 new_st.op = EXEC_DO;
1299 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1302 new_st.label = label;
1304 if (new_st.op == EXEC_DO_WHILE)
1305 new_st.expr = iter.end;
1308 new_st.ext.iterator = ip = gfc_get_iterator ();
1315 gfc_free_iterator (&iter, 0);
1321 /* Match an EXIT or CYCLE statement. */
1324 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1330 if (gfc_match_eos () == MATCH_YES)
1334 m = gfc_match ("% %s%t", &sym);
1335 if (m == MATCH_ERROR)
1339 gfc_syntax_error (st);
1343 if (sym->attr.flavor != FL_LABEL)
1345 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1346 sym->name, gfc_ascii_statement (st));
1351 /* Find the loop mentioned specified by the label (or lack of a
1353 for (p = gfc_state_stack; p; p = p->previous)
1354 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1360 gfc_error ("%s statement at %C is not within a loop",
1361 gfc_ascii_statement (st));
1363 gfc_error ("%s statement at %C is not within loop '%s'",
1364 gfc_ascii_statement (st), sym->name);
1369 /* Save the first statement in the loop - needed by the backend. */
1370 new_st.ext.whichloop = p->head;
1373 /* new_st.sym = sym;*/
1379 /* Match the EXIT statement. */
1382 gfc_match_exit (void)
1385 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1389 /* Match the CYCLE statement. */
1392 gfc_match_cycle (void)
1395 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1399 /* Match a number or character constant after a STOP or PAUSE statement. */
1402 gfc_match_stopcode (gfc_statement st)
1411 if (gfc_match_eos () != MATCH_YES)
1413 m = gfc_match_small_literal_int (&stop_code);
1414 if (m == MATCH_ERROR)
1417 if (m == MATCH_YES && stop_code > 99999)
1419 gfc_error ("STOP code out of range at %C");
1425 /* Try a character constant. */
1426 m = gfc_match_expr (&e);
1427 if (m == MATCH_ERROR)
1431 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1435 if (gfc_match_eos () != MATCH_YES)
1439 if (gfc_pure (NULL))
1441 gfc_error ("%s statement not allowed in PURE procedure at %C",
1442 gfc_ascii_statement (st));
1446 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1448 new_st.ext.stop_code = stop_code;
1453 gfc_syntax_error (st);
1461 /* Match the (deprecated) PAUSE statement. */
1464 gfc_match_pause (void)
1468 m = gfc_match_stopcode (ST_PAUSE);
1471 if (gfc_notify_std (GFC_STD_F95_DEL,
1472 "Obsolete: PAUSE statement at %C")
1480 /* Match the STOP statement. */
1483 gfc_match_stop (void)
1485 return gfc_match_stopcode (ST_STOP);
1489 /* Match a CONTINUE statement. */
1492 gfc_match_continue (void)
1495 if (gfc_match_eos () != MATCH_YES)
1497 gfc_syntax_error (ST_CONTINUE);
1501 new_st.op = EXEC_CONTINUE;
1506 /* Match the (deprecated) ASSIGN statement. */
1509 gfc_match_assign (void)
1512 gfc_st_label *label;
1514 if (gfc_match (" %l", &label) == MATCH_YES)
1516 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1518 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1520 if (gfc_notify_std (GFC_STD_F95_DEL,
1521 "Obsolete: ASSIGN statement at %C")
1525 expr->symtree->n.sym->attr.assign = 1;
1527 new_st.op = EXEC_LABEL_ASSIGN;
1528 new_st.label = label;
1537 /* Match the GO TO statement. As a computed GOTO statement is
1538 matched, it is transformed into an equivalent SELECT block. No
1539 tree is necessary, and the resulting jumps-to-jumps are
1540 specifically optimized away by the back end. */
1543 gfc_match_goto (void)
1545 gfc_code *head, *tail;
1548 gfc_st_label *label;
1552 if (gfc_match (" %l%t", &label) == MATCH_YES)
1554 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1557 new_st.op = EXEC_GOTO;
1558 new_st.label = label;
1562 /* The assigned GO TO statement. */
1564 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1566 if (gfc_notify_std (GFC_STD_F95_DEL,
1567 "Obsolete: Assigned GOTO statement at %C")
1571 new_st.op = EXEC_GOTO;
1574 if (gfc_match_eos () == MATCH_YES)
1577 /* Match label list. */
1578 gfc_match_char (',');
1579 if (gfc_match_char ('(') != MATCH_YES)
1581 gfc_syntax_error (ST_GOTO);
1588 m = gfc_match_st_label (&label, 0);
1592 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1596 head = tail = gfc_get_code ();
1599 tail->block = gfc_get_code ();
1603 tail->label = label;
1604 tail->op = EXEC_GOTO;
1606 while (gfc_match_char (',') == MATCH_YES);
1608 if (gfc_match (")%t") != MATCH_YES)
1614 "Statement label list in GOTO at %C cannot be empty");
1617 new_st.block = head;
1622 /* Last chance is a computed GO TO statement. */
1623 if (gfc_match_char ('(') != MATCH_YES)
1625 gfc_syntax_error (ST_GOTO);
1634 m = gfc_match_st_label (&label, 0);
1638 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1642 head = tail = gfc_get_code ();
1645 tail->block = gfc_get_code ();
1649 cp = gfc_get_case ();
1650 cp->low = cp->high = gfc_int_expr (i++);
1652 tail->op = EXEC_SELECT;
1653 tail->ext.case_list = cp;
1655 tail->next = gfc_get_code ();
1656 tail->next->op = EXEC_GOTO;
1657 tail->next->label = label;
1659 while (gfc_match_char (',') == MATCH_YES);
1661 if (gfc_match_char (')') != MATCH_YES)
1666 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1670 /* Get the rest of the statement. */
1671 gfc_match_char (',');
1673 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1676 /* At this point, a computed GOTO has been fully matched and an
1677 equivalent SELECT statement constructed. */
1679 new_st.op = EXEC_SELECT;
1682 /* Hack: For a "real" SELECT, the expression is in expr. We put
1683 it in expr2 so we can distinguish then and produce the correct
1685 new_st.expr2 = expr;
1686 new_st.block = head;
1690 gfc_syntax_error (ST_GOTO);
1692 gfc_free_statements (head);
1697 /* Frees a list of gfc_alloc structures. */
1700 gfc_free_alloc_list (gfc_alloc * p)
1707 gfc_free_expr (p->expr);
1713 /* Match an ALLOCATE statement. */
1716 gfc_match_allocate (void)
1718 gfc_alloc *head, *tail;
1725 if (gfc_match_char ('(') != MATCH_YES)
1731 head = tail = gfc_get_alloc ();
1734 tail->next = gfc_get_alloc ();
1738 m = gfc_match_variable (&tail->expr, 0);
1741 if (m == MATCH_ERROR)
1744 if (gfc_check_do_variable (tail->expr->symtree))
1748 && gfc_impure_variable (tail->expr->symtree->n.sym))
1750 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1755 if (gfc_match_char (',') != MATCH_YES)
1758 m = gfc_match (" stat = %v", &stat);
1759 if (m == MATCH_ERROR)
1767 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1770 ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1771 "INTENT(IN)", stat->symtree->n.sym->name);
1775 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1778 ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1783 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1785 gfc_error("STAT expression at %C must be a variable");
1789 gfc_check_do_variable(stat->symtree);
1792 if (gfc_match (" )%t") != MATCH_YES)
1795 new_st.op = EXEC_ALLOCATE;
1797 new_st.ext.alloc_list = head;
1802 gfc_syntax_error (ST_ALLOCATE);
1805 gfc_free_expr (stat);
1806 gfc_free_alloc_list (head);
1811 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1812 a set of pointer assignments to intrinsic NULL(). */
1815 gfc_match_nullify (void)
1823 if (gfc_match_char ('(') != MATCH_YES)
1828 m = gfc_match_variable (&p, 0);
1829 if (m == MATCH_ERROR)
1834 if (gfc_check_do_variable(p->symtree))
1837 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1840 ("Illegal variable in NULLIFY at %C for a PURE procedure");
1844 /* build ' => NULL() ' */
1845 e = gfc_get_expr ();
1846 e->where = gfc_current_locus;
1847 e->expr_type = EXPR_NULL;
1848 e->ts.type = BT_UNKNOWN;
1855 tail->next = gfc_get_code ();
1859 tail->op = EXEC_POINTER_ASSIGN;
1863 if (gfc_match (" )%t") == MATCH_YES)
1865 if (gfc_match_char (',') != MATCH_YES)
1872 gfc_syntax_error (ST_NULLIFY);
1875 gfc_free_statements (tail);
1880 /* Match a DEALLOCATE statement. */
1883 gfc_match_deallocate (void)
1885 gfc_alloc *head, *tail;
1892 if (gfc_match_char ('(') != MATCH_YES)
1898 head = tail = gfc_get_alloc ();
1901 tail->next = gfc_get_alloc ();
1905 m = gfc_match_variable (&tail->expr, 0);
1906 if (m == MATCH_ERROR)
1911 if (gfc_check_do_variable (tail->expr->symtree))
1915 && gfc_impure_variable (tail->expr->symtree->n.sym))
1918 ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1923 if (gfc_match_char (',') != MATCH_YES)
1926 m = gfc_match (" stat = %v", &stat);
1927 if (m == MATCH_ERROR)
1935 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1937 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1938 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
1942 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
1944 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
1945 "for a PURE procedure");
1949 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1951 gfc_error("STAT expression at %C must be a variable");
1955 gfc_check_do_variable(stat->symtree);
1958 if (gfc_match (" )%t") != MATCH_YES)
1961 new_st.op = EXEC_DEALLOCATE;
1963 new_st.ext.alloc_list = head;
1968 gfc_syntax_error (ST_DEALLOCATE);
1971 gfc_free_expr (stat);
1972 gfc_free_alloc_list (head);
1977 /* Match a RETURN statement. */
1980 gfc_match_return (void)
1984 gfc_compile_state s;
1988 if (gfc_match_eos () == MATCH_YES)
1991 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
1993 gfc_error ("Alternate RETURN statement at %C is only allowed within "
1998 if (gfc_current_form == FORM_FREE)
2000 /* The following are valid, so we can't require a blank after the
2004 c = gfc_peek_char ();
2005 if (ISALPHA (c) || ISDIGIT (c))
2009 m = gfc_match (" %e%t", &e);
2012 if (m == MATCH_ERROR)
2015 gfc_syntax_error (ST_RETURN);
2022 gfc_enclosing_unit (&s);
2023 if (s == COMP_PROGRAM
2024 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2025 "main program at %C") == FAILURE)
2028 new_st.op = EXEC_RETURN;
2035 /* Match a CALL statement. The tricky part here are possible
2036 alternate return specifiers. We handle these by having all
2037 "subroutines" actually return an integer via a register that gives
2038 the return number. If the call specifies alternate returns, we
2039 generate code for a SELECT statement whose case clauses contain
2040 GOTOs to the various labels. */
2043 gfc_match_call (void)
2045 char name[GFC_MAX_SYMBOL_LEN + 1];
2046 gfc_actual_arglist *a, *arglist;
2056 m = gfc_match ("% %n", name);
2062 if (gfc_get_ha_sym_tree (name, &st))
2066 gfc_set_sym_referenced (sym);
2068 if (!sym->attr.generic
2069 && !sym->attr.subroutine
2070 && gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2073 if (gfc_match_eos () != MATCH_YES)
2075 m = gfc_match_actual_arglist (1, &arglist);
2078 if (m == MATCH_ERROR)
2081 if (gfc_match_eos () != MATCH_YES)
2085 /* If any alternate return labels were found, construct a SELECT
2086 statement that will jump to the right place. */
2089 for (a = arglist; a; a = a->next)
2090 if (a->expr == NULL)
2095 gfc_symtree *select_st;
2096 gfc_symbol *select_sym;
2097 char name[GFC_MAX_SYMBOL_LEN + 1];
2099 new_st.next = c = gfc_get_code ();
2100 c->op = EXEC_SELECT;
2101 sprintf (name, "_result_%s",sym->name);
2102 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail */
2104 select_sym = select_st->n.sym;
2105 select_sym->ts.type = BT_INTEGER;
2106 select_sym->ts.kind = gfc_default_integer_kind;
2107 gfc_set_sym_referenced (select_sym);
2108 c->expr = gfc_get_expr ();
2109 c->expr->expr_type = EXPR_VARIABLE;
2110 c->expr->symtree = select_st;
2111 c->expr->ts = select_sym->ts;
2112 c->expr->where = gfc_current_locus;
2115 for (a = arglist; a; a = a->next)
2117 if (a->expr != NULL)
2120 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2125 c->block = gfc_get_code ();
2127 c->op = EXEC_SELECT;
2129 new_case = gfc_get_case ();
2130 new_case->high = new_case->low = gfc_int_expr (i);
2131 c->ext.case_list = new_case;
2133 c->next = gfc_get_code ();
2134 c->next->op = EXEC_GOTO;
2135 c->next->label = a->label;
2139 new_st.op = EXEC_CALL;
2140 new_st.symtree = st;
2141 new_st.ext.actual = arglist;
2146 gfc_syntax_error (ST_CALL);
2149 gfc_free_actual_arglist (arglist);
2154 /* Given a name, return a pointer to the common head structure,
2155 creating it if it does not exist. If FROM_MODULE is nonzero, we
2156 mangle the name so that it doesn't interfere with commons defined
2157 in the using namespace.
2158 TODO: Add to global symbol tree. */
2161 gfc_get_common (const char *name, int from_module)
2164 static int serial = 0;
2165 char mangled_name[GFC_MAX_SYMBOL_LEN+1];
2169 /* A use associated common block is only needed to correctly layout
2170 the variables it contains. */
2171 snprintf(mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2172 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2176 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2179 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2182 if (st->n.common == NULL)
2184 st->n.common = gfc_get_common_head ();
2185 st->n.common->where = gfc_current_locus;
2186 strcpy (st->n.common->name, name);
2189 return st->n.common;
2193 /* Match a common block name. */
2196 match_common_name (char *name)
2200 if (gfc_match_char ('/') == MATCH_NO)
2206 if (gfc_match_char ('/') == MATCH_YES)
2212 m = gfc_match_name (name);
2214 if (m == MATCH_ERROR)
2216 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2219 gfc_error ("Syntax error in common block name at %C");
2224 /* Match a COMMON statement. */
2227 gfc_match_common (void)
2229 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2230 char name[GFC_MAX_SYMBOL_LEN+1];
2233 gfc_equiv * e1, * e2;
2236 old_blank_common = gfc_current_ns->blank_common.head;
2237 if (old_blank_common)
2239 while (old_blank_common->common_next)
2240 old_blank_common = old_blank_common->common_next;
2247 m = match_common_name (name);
2248 if (m == MATCH_ERROR)
2251 if (name[0] == '\0')
2253 t = &gfc_current_ns->blank_common;
2254 if (t->head == NULL)
2255 t->where = gfc_current_locus;
2260 t = gfc_get_common (name, 0);
2269 while (tail->common_next)
2270 tail = tail->common_next;
2273 /* Grab the list of symbols. */
2276 m = gfc_match_symbol (&sym, 0);
2277 if (m == MATCH_ERROR)
2282 if (sym->attr.in_common)
2284 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2289 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2292 if (sym->value != NULL
2293 && (name[0] == '\0' || !sym->attr.data))
2295 if (name[0] == '\0')
2296 gfc_error ("Previously initialized symbol '%s' in "
2297 "blank COMMON block at %C", sym->name);
2299 gfc_error ("Previously initialized symbol '%s' in "
2300 "COMMON block '%s' at %C", sym->name, name);
2304 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2307 /* Derived type names must have the SEQUENCE attribute. */
2308 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2311 ("Derived type variable in COMMON at %C does not have the "
2312 "SEQUENCE attribute");
2317 tail->common_next = sym;
2323 /* Deal with an optional array specification after the
2325 m = gfc_match_array_spec (&as);
2326 if (m == MATCH_ERROR)
2331 if (as->type != AS_EXPLICIT)
2334 ("Array specification for symbol '%s' in COMMON at %C "
2335 "must be explicit", sym->name);
2339 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2342 if (sym->attr.pointer)
2345 ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2355 sym->common_head = t;
2357 /* Check to see if the symbol is already in an equivalence group.
2358 If it is, set the other members as being in common. */
2359 if (sym->attr.in_equivalence)
2361 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2363 for (e2 = e1; e2; e2 = e2->eq)
2364 if (e2->expr->symtree->n.sym == sym)
2371 for (e2 = e1; e2; e2 = e2->eq)
2373 other = e2->expr->symtree->n.sym;
2374 if (other->common_head
2375 && other->common_head != sym->common_head)
2377 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2378 "%C is being indirectly equivalenced to "
2379 "another COMMON block '%s'",
2381 sym->common_head->name,
2382 other->common_head->name);
2385 other->attr.in_common = 1;
2386 other->common_head = t;
2392 gfc_gobble_whitespace ();
2393 if (gfc_match_eos () == MATCH_YES)
2395 if (gfc_peek_char () == '/')
2397 if (gfc_match_char (',') != MATCH_YES)
2399 gfc_gobble_whitespace ();
2400 if (gfc_peek_char () == '/')
2409 gfc_syntax_error (ST_COMMON);
2412 if (old_blank_common)
2413 old_blank_common->common_next = NULL;
2415 gfc_current_ns->blank_common.head = NULL;
2416 gfc_free_array_spec (as);
2421 /* Match a BLOCK DATA program unit. */
2424 gfc_match_block_data (void)
2426 char name[GFC_MAX_SYMBOL_LEN + 1];
2430 if (gfc_match_eos () == MATCH_YES)
2432 gfc_new_block = NULL;
2436 m = gfc_match ("% %n%t", name);
2440 if (gfc_get_symbol (name, NULL, &sym))
2443 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2446 gfc_new_block = sym;
2452 /* Free a namelist structure. */
2455 gfc_free_namelist (gfc_namelist * name)
2459 for (; name; name = n)
2467 /* Match a NAMELIST statement. */
2470 gfc_match_namelist (void)
2472 gfc_symbol *group_name, *sym;
2476 m = gfc_match (" / %s /", &group_name);
2479 if (m == MATCH_ERROR)
2484 if (group_name->ts.type != BT_UNKNOWN)
2487 ("Namelist group name '%s' at %C already has a basic type "
2488 "of %s", group_name->name, gfc_typename (&group_name->ts));
2492 if (group_name->attr.flavor != FL_NAMELIST
2493 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2494 group_name->name, NULL) == FAILURE)
2499 m = gfc_match_symbol (&sym, 1);
2502 if (m == MATCH_ERROR)
2505 if (sym->attr.in_namelist == 0
2506 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2509 nl = gfc_get_namelist ();
2512 if (group_name->namelist == NULL)
2513 group_name->namelist = group_name->namelist_tail = nl;
2516 group_name->namelist_tail->next = nl;
2517 group_name->namelist_tail = nl;
2520 if (gfc_match_eos () == MATCH_YES)
2523 m = gfc_match_char (',');
2525 if (gfc_match_char ('/') == MATCH_YES)
2527 m2 = gfc_match (" %s /", &group_name);
2528 if (m2 == MATCH_YES)
2530 if (m2 == MATCH_ERROR)
2544 gfc_syntax_error (ST_NAMELIST);
2551 /* Match a MODULE statement. */
2554 gfc_match_module (void)
2558 m = gfc_match (" %s%t", &gfc_new_block);
2562 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2563 gfc_new_block->name, NULL) == FAILURE)
2570 /* Free equivalence sets and lists. Recursively is the easiest way to
2574 gfc_free_equiv (gfc_equiv * eq)
2580 gfc_free_equiv (eq->eq);
2581 gfc_free_equiv (eq->next);
2583 gfc_free_expr (eq->expr);
2588 /* Match an EQUIVALENCE statement. */
2591 gfc_match_equivalence (void)
2593 gfc_equiv *eq, *set, *tail;
2597 gfc_common_head *common_head = NULL;
2604 eq = gfc_get_equiv ();
2608 eq->next = gfc_current_ns->equiv;
2609 gfc_current_ns->equiv = eq;
2611 if (gfc_match_char ('(') != MATCH_YES)
2615 common_flag = FALSE;
2619 m = gfc_match_equiv_variable (&set->expr);
2620 if (m == MATCH_ERROR)
2625 if (gfc_match_char ('%') == MATCH_YES)
2627 gfc_error ("Derived type component %C is not a "
2628 "permitted EQUIVALENCE member");
2632 for (ref = set->expr->ref; ref; ref = ref->next)
2633 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2636 ("Array reference in EQUIVALENCE at %C cannot be an "
2641 sym = set->expr->symtree->n.sym;
2643 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL)
2647 if (sym->attr.in_common)
2650 common_head = sym->common_head;
2653 if (gfc_match_char (')') == MATCH_YES)
2655 if (gfc_match_char (',') != MATCH_YES)
2658 set->eq = gfc_get_equiv ();
2662 /* If one of the members of an equivalence is in common, then
2663 mark them all as being in common. Before doing this, check
2664 that members of the equivalence group are not in different
2667 for (set = eq; set; set = set->eq)
2669 sym = set->expr->symtree->n.sym;
2670 if (sym->common_head && sym->common_head != common_head)
2672 gfc_error ("Attempt to indirectly overlap COMMON "
2673 "blocks %s and %s by EQUIVALENCE at %C",
2674 sym->common_head->name,
2678 sym->attr.in_common = 1;
2679 sym->common_head = common_head;
2682 if (gfc_match_eos () == MATCH_YES)
2684 if (gfc_match_char (',') != MATCH_YES)
2691 gfc_syntax_error (ST_EQUIVALENCE);
2697 gfc_free_equiv (gfc_current_ns->equiv);
2698 gfc_current_ns->equiv = eq;
2703 /* Check that a statement function is not recursive. This is done by looking
2704 for the statement function symbol(sym) by looking recursively through its
2705 expression(e). If a reference to sym is found, true is returned. */
2707 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
2709 gfc_actual_arglist *arg;
2716 switch (e->expr_type)
2719 for (arg = e->value.function.actual; arg; arg = arg->next)
2721 if (sym->name == arg->name
2722 || recursive_stmt_fcn (arg->expr, sym))
2726 if (e->symtree == NULL)
2729 /* Check the name before testing for nested recursion! */
2730 if (sym->name == e->symtree->n.sym->name)
2733 /* Catch recursion via other statement functions. */
2734 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
2735 && e->symtree->n.sym->value
2736 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
2742 if (e->symtree && sym->name == e->symtree->n.sym->name)
2747 if (recursive_stmt_fcn (e->value.op.op1, sym)
2748 || recursive_stmt_fcn (e->value.op.op2, sym))
2756 /* Component references do not need to be checked. */
2759 for (ref = e->ref; ref; ref = ref->next)
2764 for (i = 0; i < ref->u.ar.dimen; i++)
2766 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
2767 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
2768 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
2774 if (recursive_stmt_fcn (ref->u.ss.start, sym)
2775 || recursive_stmt_fcn (ref->u.ss.end, sym))
2789 /* Match a statement function declaration. It is so easy to match
2790 non-statement function statements with a MATCH_ERROR as opposed to
2791 MATCH_NO that we suppress error message in most cases. */
2794 gfc_match_st_function (void)
2796 gfc_error_buf old_error;
2801 m = gfc_match_symbol (&sym, 0);
2805 gfc_push_error (&old_error);
2807 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
2808 sym->name, NULL) == FAILURE)
2811 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2814 m = gfc_match (" = %e%t", &expr);
2818 gfc_free_error (&old_error);
2819 if (m == MATCH_ERROR)
2822 if (recursive_stmt_fcn (expr, sym))
2824 gfc_error ("Statement function at %L is recursive",
2834 gfc_pop_error (&old_error);
2839 /***************** SELECT CASE subroutines ******************/
2841 /* Free a single case structure. */
2844 free_case (gfc_case * p)
2846 if (p->low == p->high)
2848 gfc_free_expr (p->low);
2849 gfc_free_expr (p->high);
2854 /* Free a list of case structures. */
2857 gfc_free_case_list (gfc_case * p)
2869 /* Match a single case selector. */
2872 match_case_selector (gfc_case ** cp)
2877 c = gfc_get_case ();
2878 c->where = gfc_current_locus;
2880 if (gfc_match_char (':') == MATCH_YES)
2882 m = gfc_match_init_expr (&c->high);
2885 if (m == MATCH_ERROR)
2891 m = gfc_match_init_expr (&c->low);
2892 if (m == MATCH_ERROR)
2897 /* If we're not looking at a ':' now, make a range out of a single
2898 target. Else get the upper bound for the case range. */
2899 if (gfc_match_char (':') != MATCH_YES)
2903 m = gfc_match_init_expr (&c->high);
2904 if (m == MATCH_ERROR)
2906 /* MATCH_NO is fine. It's OK if nothing is there! */
2914 gfc_error ("Expected initialization expression in CASE at %C");
2922 /* Match the end of a case statement. */
2925 match_case_eos (void)
2927 char name[GFC_MAX_SYMBOL_LEN + 1];
2930 if (gfc_match_eos () == MATCH_YES)
2933 gfc_gobble_whitespace ();
2935 m = gfc_match_name (name);
2939 if (strcmp (name, gfc_current_block ()->name) != 0)
2941 gfc_error ("Expected case name of '%s' at %C",
2942 gfc_current_block ()->name);
2946 return gfc_match_eos ();
2950 /* Match a SELECT statement. */
2953 gfc_match_select (void)
2958 m = gfc_match_label ();
2959 if (m == MATCH_ERROR)
2962 m = gfc_match (" select case ( %e )%t", &expr);
2966 new_st.op = EXEC_SELECT;
2973 /* Match a CASE statement. */
2976 gfc_match_case (void)
2978 gfc_case *c, *head, *tail;
2983 if (gfc_current_state () != COMP_SELECT)
2985 gfc_error ("Unexpected CASE statement at %C");
2989 if (gfc_match ("% default") == MATCH_YES)
2991 m = match_case_eos ();
2994 if (m == MATCH_ERROR)
2997 new_st.op = EXEC_SELECT;
2998 c = gfc_get_case ();
2999 c->where = gfc_current_locus;
3000 new_st.ext.case_list = c;
3004 if (gfc_match_char ('(') != MATCH_YES)
3009 if (match_case_selector (&c) == MATCH_ERROR)
3019 if (gfc_match_char (')') == MATCH_YES)
3021 if (gfc_match_char (',') != MATCH_YES)
3025 m = match_case_eos ();
3028 if (m == MATCH_ERROR)
3031 new_st.op = EXEC_SELECT;
3032 new_st.ext.case_list = head;
3037 gfc_error ("Syntax error in CASE-specification at %C");
3040 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3044 /********************* WHERE subroutines ********************/
3046 /* Match the rest of a simple WHERE statement that follows an IF statement.
3050 match_simple_where (void)
3056 m = gfc_match (" ( %e )", &expr);
3060 m = gfc_match_assignment ();
3063 if (m == MATCH_ERROR)
3066 if (gfc_match_eos () != MATCH_YES)
3069 c = gfc_get_code ();
3073 c->next = gfc_get_code ();
3076 gfc_clear_new_st ();
3078 new_st.op = EXEC_WHERE;
3084 gfc_syntax_error (ST_WHERE);
3087 gfc_free_expr (expr);
3091 /* Match a WHERE statement. */
3094 gfc_match_where (gfc_statement * st)
3100 m0 = gfc_match_label ();
3101 if (m0 == MATCH_ERROR)
3104 m = gfc_match (" where ( %e )", &expr);
3108 if (gfc_match_eos () == MATCH_YES)
3110 *st = ST_WHERE_BLOCK;
3112 new_st.op = EXEC_WHERE;
3117 m = gfc_match_assignment ();
3119 gfc_syntax_error (ST_WHERE);
3123 gfc_free_expr (expr);
3127 /* We've got a simple WHERE statement. */
3129 c = gfc_get_code ();
3133 c->next = gfc_get_code ();
3136 gfc_clear_new_st ();
3138 new_st.op = EXEC_WHERE;
3145 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3146 new_st if successful. */
3149 gfc_match_elsewhere (void)
3151 char name[GFC_MAX_SYMBOL_LEN + 1];
3155 if (gfc_current_state () != COMP_WHERE)
3157 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3163 if (gfc_match_char ('(') == MATCH_YES)
3165 m = gfc_match_expr (&expr);
3168 if (m == MATCH_ERROR)
3171 if (gfc_match_char (')') != MATCH_YES)
3175 if (gfc_match_eos () != MATCH_YES)
3176 { /* Better be a name at this point */
3177 m = gfc_match_name (name);
3180 if (m == MATCH_ERROR)
3183 if (gfc_match_eos () != MATCH_YES)
3186 if (strcmp (name, gfc_current_block ()->name) != 0)
3188 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3189 name, gfc_current_block ()->name);
3194 new_st.op = EXEC_WHERE;
3199 gfc_syntax_error (ST_ELSEWHERE);
3202 gfc_free_expr (expr);
3207 /******************** FORALL subroutines ********************/
3209 /* Free a list of FORALL iterators. */
3212 gfc_free_forall_iterator (gfc_forall_iterator * iter)
3214 gfc_forall_iterator *next;
3220 gfc_free_expr (iter->var);
3221 gfc_free_expr (iter->start);
3222 gfc_free_expr (iter->end);
3223 gfc_free_expr (iter->stride);
3231 /* Match an iterator as part of a FORALL statement. The format is:
3233 <var> = <start>:<end>[:<stride>][, <scalar mask>] */
3236 match_forall_iterator (gfc_forall_iterator ** result)
3238 gfc_forall_iterator *iter;
3242 where = gfc_current_locus;
3243 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3245 m = gfc_match_variable (&iter->var, 0);
3249 if (gfc_match_char ('=') != MATCH_YES)
3255 m = gfc_match_expr (&iter->start);
3259 if (gfc_match_char (':') != MATCH_YES)
3262 m = gfc_match_expr (&iter->end);
3265 if (m == MATCH_ERROR)
3268 if (gfc_match_char (':') == MATCH_NO)
3269 iter->stride = gfc_int_expr (1);
3272 m = gfc_match_expr (&iter->stride);
3275 if (m == MATCH_ERROR)
3283 gfc_error ("Syntax error in FORALL iterator at %C");
3287 gfc_current_locus = where;
3288 gfc_free_forall_iterator (iter);
3293 /* Match the header of a FORALL statement. */
3296 match_forall_header (gfc_forall_iterator ** phead, gfc_expr ** mask)
3298 gfc_forall_iterator *head, *tail, *new;
3301 gfc_gobble_whitespace ();
3306 if (gfc_match_char ('(') != MATCH_YES)
3309 m = match_forall_iterator (&new);
3310 if (m == MATCH_ERROR)
3319 if (gfc_match_char (',') != MATCH_YES)
3322 m = match_forall_iterator (&new);
3323 if (m == MATCH_ERROR)
3332 /* Have to have a mask expression */
3334 m = gfc_match_expr (mask);
3337 if (m == MATCH_ERROR)
3343 if (gfc_match_char (')') == MATCH_NO)
3350 gfc_syntax_error (ST_FORALL);
3353 gfc_free_expr (*mask);
3354 gfc_free_forall_iterator (head);
3359 /* Match the rest of a simple FORALL statement that follows an IF statement.
3363 match_simple_forall (void)
3365 gfc_forall_iterator *head;
3374 m = match_forall_header (&head, &mask);
3381 m = gfc_match_assignment ();
3383 if (m == MATCH_ERROR)
3387 m = gfc_match_pointer_assignment ();
3388 if (m == MATCH_ERROR)
3394 c = gfc_get_code ();
3396 c->loc = gfc_current_locus;
3398 if (gfc_match_eos () != MATCH_YES)
3401 gfc_clear_new_st ();
3402 new_st.op = EXEC_FORALL;
3404 new_st.ext.forall_iterator = head;
3405 new_st.block = gfc_get_code ();
3407 new_st.block->op = EXEC_FORALL;
3408 new_st.block->next = c;
3413 gfc_syntax_error (ST_FORALL);
3416 gfc_free_forall_iterator (head);
3417 gfc_free_expr (mask);
3423 /* Match a FORALL statement. */
3426 gfc_match_forall (gfc_statement * st)
3428 gfc_forall_iterator *head;
3437 m0 = gfc_match_label ();
3438 if (m0 == MATCH_ERROR)
3441 m = gfc_match (" forall");
3445 m = match_forall_header (&head, &mask);
3446 if (m == MATCH_ERROR)
3451 if (gfc_match_eos () == MATCH_YES)
3453 *st = ST_FORALL_BLOCK;
3455 new_st.op = EXEC_FORALL;
3457 new_st.ext.forall_iterator = head;
3462 m = gfc_match_assignment ();
3463 if (m == MATCH_ERROR)
3467 m = gfc_match_pointer_assignment ();
3468 if (m == MATCH_ERROR)
3474 c = gfc_get_code ();
3477 if (gfc_match_eos () != MATCH_YES)
3480 gfc_clear_new_st ();
3481 new_st.op = EXEC_FORALL;
3483 new_st.ext.forall_iterator = head;
3484 new_st.block = gfc_get_code ();
3486 new_st.block->op = EXEC_FORALL;
3487 new_st.block->next = c;
3493 gfc_syntax_error (ST_FORALL);
3496 gfc_free_forall_iterator (head);
3497 gfc_free_expr (mask);
3498 gfc_free_statements (c);