1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 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
30 /* For matching and debugging purposes. Order matters here! The
31 unary operators /must/ precede the binary plus and minus, or
32 the expression parser breaks. */
34 mstring intrinsic_operators[] = {
35 minit ("+", INTRINSIC_UPLUS),
36 minit ("-", INTRINSIC_UMINUS),
37 minit ("+", INTRINSIC_PLUS),
38 minit ("-", INTRINSIC_MINUS),
39 minit ("**", INTRINSIC_POWER),
40 minit ("//", INTRINSIC_CONCAT),
41 minit ("*", INTRINSIC_TIMES),
42 minit ("/", INTRINSIC_DIVIDE),
43 minit (".and.", INTRINSIC_AND),
44 minit (".or.", INTRINSIC_OR),
45 minit (".eqv.", INTRINSIC_EQV),
46 minit (".neqv.", INTRINSIC_NEQV),
47 minit (".eq.", INTRINSIC_EQ),
48 minit ("==", INTRINSIC_EQ),
49 minit (".ne.", INTRINSIC_NE),
50 minit ("/=", INTRINSIC_NE),
51 minit (".ge.", INTRINSIC_GE),
52 minit (">=", INTRINSIC_GE),
53 minit (".le.", INTRINSIC_LE),
54 minit ("<=", INTRINSIC_LE),
55 minit (".lt.", INTRINSIC_LT),
56 minit ("<", INTRINSIC_LT),
57 minit (".gt.", INTRINSIC_GT),
58 minit (">", INTRINSIC_GT),
59 minit (".not.", INTRINSIC_NOT),
60 minit ("parens", INTRINSIC_PARENTHESES),
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. If cnt is non-NULL it
142 will be set to the number of digits. */
145 gfc_match_small_literal_int (int *value, int *cnt)
151 old_loc = gfc_current_locus;
153 gfc_gobble_whitespace ();
154 c = gfc_next_char ();
160 gfc_current_locus = old_loc;
169 old_loc = gfc_current_locus;
170 c = gfc_next_char ();
175 i = 10 * i + c - '0';
180 gfc_error ("Integer too large at %C");
185 gfc_current_locus = old_loc;
194 /* Match a small, constant integer expression, like in a kind
195 statement. On MATCH_YES, 'value' is set. */
198 gfc_match_small_int (int *value)
205 m = gfc_match_expr (&expr);
209 p = gfc_extract_int (expr, &i);
210 gfc_free_expr (expr);
223 /* Matches a statement label. Uses gfc_match_small_literal_int() to
224 do most of the work. */
227 gfc_match_st_label (gfc_st_label **label)
233 old_loc = gfc_current_locus;
235 m = gfc_match_small_literal_int (&i, &cnt);
241 gfc_error ("Too many digits in statement label at %C");
247 gfc_error ("Statement label at %C is zero");
251 *label = gfc_get_st_label (i);
256 gfc_current_locus = old_loc;
261 /* Match and validate a label associated with a named IF, DO or SELECT
262 statement. If the symbol does not have the label attribute, we add
263 it. We also make sure the symbol does not refer to another
264 (active) block. A matched label is pointed to by gfc_new_block. */
267 gfc_match_label (void)
269 char name[GFC_MAX_SYMBOL_LEN + 1];
272 gfc_new_block = NULL;
274 m = gfc_match (" %n :", name);
278 if (gfc_get_symbol (name, NULL, &gfc_new_block))
280 gfc_error ("Label name '%s' at %C is ambiguous", name);
284 if (gfc_new_block->attr.flavor == FL_LABEL)
286 gfc_error ("Duplicate construct label '%s' at %C", name);
290 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
291 gfc_new_block->name, NULL) == FAILURE)
298 /* Try and match the input against an array of possibilities. If one
299 potential matching string is a substring of another, the longest
300 match takes precedence. Spaces in the target strings are optional
301 spaces that do not necessarily have to be found in the input
302 stream. In fixed mode, spaces never appear. If whitespace is
303 matched, it matches unlimited whitespace in the input. For this
304 reason, the 'mp' member of the mstring structure is used to track
305 the progress of each potential match.
307 If there is no match we return the tag associated with the
308 terminating NULL mstring structure and leave the locus pointer
309 where it started. If there is a match we return the tag member of
310 the matched mstring and leave the locus pointer after the matched
313 A '%' character is a mandatory space. */
316 gfc_match_strings (mstring *a)
318 mstring *p, *best_match;
319 int no_match, c, possibles;
324 for (p = a; p->string != NULL; p++)
333 match_loc = gfc_current_locus;
335 gfc_gobble_whitespace ();
337 while (possibles > 0)
339 c = gfc_next_char ();
341 /* Apply the next character to the current possibilities. */
342 for (p = a; p->string != NULL; p++)
349 /* Space matches 1+ whitespace(s). */
350 if ((gfc_current_form == FORM_FREE) && gfc_is_whitespace (c))
368 match_loc = gfc_current_locus;
376 gfc_current_locus = match_loc;
378 return (best_match == NULL) ? no_match : best_match->tag;
382 /* See if the current input looks like a name of some sort. Modifies
383 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long. */
386 gfc_match_name (char *buffer)
391 old_loc = gfc_current_locus;
392 gfc_gobble_whitespace ();
394 c = gfc_next_char ();
395 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
397 if (gfc_error_flag_test() == 0)
398 gfc_error ("Invalid character in name at %C");
399 gfc_current_locus = old_loc;
409 if (i > gfc_option.max_identifier_length)
411 gfc_error ("Name at %C is too long");
415 old_loc = gfc_current_locus;
416 c = gfc_next_char ();
418 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
421 gfc_current_locus = old_loc;
427 /* Match a symbol on the input. Modifies the pointer to the symbol
428 pointer if successful. */
431 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
433 char buffer[GFC_MAX_SYMBOL_LEN + 1];
436 m = gfc_match_name (buffer);
441 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
442 ? MATCH_ERROR : MATCH_YES;
444 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
452 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
457 m = gfc_match_sym_tree (&st, host_assoc);
462 *matched_symbol = st->n.sym;
464 *matched_symbol = NULL;
467 *matched_symbol = NULL;
472 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
473 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
477 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
481 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
483 if (op == INTRINSIC_NONE)
491 /* Match a loop control phrase:
493 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
495 If the final integer expression is not present, a constant unity
496 expression is returned. We don't return MATCH_ERROR until after
497 the equals sign is seen. */
500 gfc_match_iterator (gfc_iterator *iter, int init_flag)
502 char name[GFC_MAX_SYMBOL_LEN + 1];
503 gfc_expr *var, *e1, *e2, *e3;
507 /* Match the start of an iterator without affecting the symbol table. */
509 start = gfc_current_locus;
510 m = gfc_match (" %n =", name);
511 gfc_current_locus = start;
516 m = gfc_match_variable (&var, 0);
520 gfc_match_char ('=');
524 if (var->ref != NULL)
526 gfc_error ("Loop variable at %C cannot be a sub-component");
530 if (var->symtree->n.sym->attr.intent == INTENT_IN)
532 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
533 var->symtree->n.sym->name);
537 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
540 if (m == MATCH_ERROR)
543 if (gfc_match_char (',') != MATCH_YES)
546 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
549 if (m == MATCH_ERROR)
552 if (gfc_match_char (',') != MATCH_YES)
554 e3 = gfc_int_expr (1);
558 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
559 if (m == MATCH_ERROR)
563 gfc_error ("Expected a step value in iterator at %C");
575 gfc_error ("Syntax error in iterator at %C");
586 /* Tries to match the next non-whitespace character on the input.
587 This subroutine does not return MATCH_ERROR. */
590 gfc_match_char (char c)
594 where = gfc_current_locus;
595 gfc_gobble_whitespace ();
597 if (gfc_next_char () == c)
600 gfc_current_locus = where;
605 /* General purpose matching subroutine. The target string is a
606 scanf-like format string in which spaces correspond to arbitrary
607 whitespace (including no whitespace), characters correspond to
608 themselves. The %-codes are:
610 %% Literal percent sign
611 %e Expression, pointer to a pointer is set
612 %s Symbol, pointer to the symbol is set
613 %n Name, character buffer is set to name
614 %t Matches end of statement.
615 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
616 %l Matches a statement label
617 %v Matches a variable expression (an lvalue)
618 % Matches a required space (in free form) and optional spaces. */
621 gfc_match (const char *target, ...)
623 gfc_st_label **label;
632 old_loc = gfc_current_locus;
633 va_start (argp, target);
643 gfc_gobble_whitespace ();
654 vp = va_arg (argp, void **);
655 n = gfc_match_expr ((gfc_expr **) vp);
666 vp = va_arg (argp, void **);
667 n = gfc_match_variable ((gfc_expr **) vp, 0);
678 vp = va_arg (argp, void **);
679 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
690 np = va_arg (argp, char *);
691 n = gfc_match_name (np);
702 label = va_arg (argp, gfc_st_label **);
703 n = gfc_match_st_label (label);
714 ip = va_arg (argp, int *);
715 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
726 if (gfc_match_eos () != MATCH_YES)
734 if (gfc_match_space () == MATCH_YES)
740 break; /* Fall through to character matcher */
743 gfc_internal_error ("gfc_match(): Bad match code %c", c);
747 if (c == gfc_next_char ())
757 /* Clean up after a failed match. */
758 gfc_current_locus = old_loc;
759 va_start (argp, target);
762 for (; matches > 0; matches--)
772 /* Matches that don't have to be undone */
777 (void) va_arg (argp, void **);
782 vp = va_arg (argp, void **);
796 /*********************** Statement level matching **********************/
798 /* Matches the start of a program unit, which is the program keyword
799 followed by an obligatory symbol. */
802 gfc_match_program (void)
807 m = gfc_match ("% %s%t", &sym);
811 gfc_error ("Invalid form of PROGRAM statement at %C");
815 if (m == MATCH_ERROR)
818 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
827 /* Match a simple assignment statement. */
830 gfc_match_assignment (void)
832 gfc_expr *lvalue, *rvalue;
836 old_loc = gfc_current_locus;
839 m = gfc_match (" %v =", &lvalue);
842 gfc_current_locus = old_loc;
843 gfc_free_expr (lvalue);
847 if (lvalue->symtree->n.sym->attr.protected
848 && lvalue->symtree->n.sym->attr.use_assoc)
850 gfc_current_locus = old_loc;
851 gfc_free_expr (lvalue);
852 gfc_error ("Setting value of PROTECTED variable at %C");
857 m = gfc_match (" %e%t", &rvalue);
860 gfc_current_locus = old_loc;
861 gfc_free_expr (lvalue);
862 gfc_free_expr (rvalue);
866 gfc_set_sym_referenced (lvalue->symtree->n.sym);
868 new_st.op = EXEC_ASSIGN;
869 new_st.expr = lvalue;
870 new_st.expr2 = rvalue;
872 gfc_check_do_variable (lvalue->symtree);
878 /* Match a pointer assignment statement. */
881 gfc_match_pointer_assignment (void)
883 gfc_expr *lvalue, *rvalue;
887 old_loc = gfc_current_locus;
889 lvalue = rvalue = NULL;
891 m = gfc_match (" %v =>", &lvalue);
898 m = gfc_match (" %e%t", &rvalue);
902 if (lvalue->symtree->n.sym->attr.protected
903 && lvalue->symtree->n.sym->attr.use_assoc)
905 gfc_error ("Assigning to a PROTECTED pointer at %C");
911 new_st.op = EXEC_POINTER_ASSIGN;
912 new_st.expr = lvalue;
913 new_st.expr2 = rvalue;
918 gfc_current_locus = old_loc;
919 gfc_free_expr (lvalue);
920 gfc_free_expr (rvalue);
925 /* We try to match an easy arithmetic IF statement. This only happens
926 when just after having encountered a simple IF statement. This code
927 is really duplicate with parts of the gfc_match_if code, but this is
931 match_arithmetic_if (void)
933 gfc_st_label *l1, *l2, *l3;
937 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
941 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
942 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
943 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
945 gfc_free_expr (expr);
949 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF statement "
953 new_st.op = EXEC_ARITHMETIC_IF;
963 /* The IF statement is a bit of a pain. First of all, there are three
964 forms of it, the simple IF, the IF that starts a block and the
967 There is a problem with the simple IF and that is the fact that we
968 only have a single level of undo information on symbols. What this
969 means is for a simple IF, we must re-match the whole IF statement
970 multiple times in order to guarantee that the symbol table ends up
971 in the proper state. */
973 static match match_simple_forall (void);
974 static match match_simple_where (void);
977 gfc_match_if (gfc_statement *if_type)
980 gfc_st_label *l1, *l2, *l3;
985 n = gfc_match_label ();
986 if (n == MATCH_ERROR)
989 old_loc = gfc_current_locus;
991 m = gfc_match (" if ( %e", &expr);
995 if (gfc_match_char (')') != MATCH_YES)
997 gfc_error ("Syntax error in IF-expression at %C");
998 gfc_free_expr (expr);
1002 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1008 gfc_error ("Block label not appropriate for arithmetic IF "
1010 gfc_free_expr (expr);
1014 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1015 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1016 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1018 gfc_free_expr (expr);
1022 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF "
1023 "statement at %C") == FAILURE)
1026 new_st.op = EXEC_ARITHMETIC_IF;
1032 *if_type = ST_ARITHMETIC_IF;
1036 if (gfc_match (" then%t") == MATCH_YES)
1038 new_st.op = EXEC_IF;
1040 *if_type = ST_IF_BLOCK;
1046 gfc_error ("Block label is not appropriate IF statement at %C");
1047 gfc_free_expr (expr);
1051 /* At this point the only thing left is a simple IF statement. At
1052 this point, n has to be MATCH_NO, so we don't have to worry about
1053 re-matching a block label. From what we've got so far, try
1054 matching an assignment. */
1056 *if_type = ST_SIMPLE_IF;
1058 m = gfc_match_assignment ();
1062 gfc_free_expr (expr);
1063 gfc_undo_symbols ();
1064 gfc_current_locus = old_loc;
1066 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1067 assignment was found. For MATCH_NO, continue to call the various
1069 if (m == MATCH_ERROR)
1072 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1074 m = gfc_match_pointer_assignment ();
1078 gfc_free_expr (expr);
1079 gfc_undo_symbols ();
1080 gfc_current_locus = old_loc;
1082 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1084 /* Look at the next keyword to see which matcher to call. Matching
1085 the keyword doesn't affect the symbol table, so we don't have to
1086 restore between tries. */
1088 #define match(string, subr, statement) \
1089 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1093 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1094 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1095 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1096 match ("call", gfc_match_call, ST_CALL)
1097 match ("close", gfc_match_close, ST_CLOSE)
1098 match ("continue", gfc_match_continue, ST_CONTINUE)
1099 match ("cycle", gfc_match_cycle, ST_CYCLE)
1100 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1101 match ("end file", gfc_match_endfile, ST_END_FILE)
1102 match ("exit", gfc_match_exit, ST_EXIT)
1103 match ("flush", gfc_match_flush, ST_FLUSH)
1104 match ("forall", match_simple_forall, ST_FORALL)
1105 match ("go to", gfc_match_goto, ST_GOTO)
1106 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1107 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1108 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1109 match ("open", gfc_match_open, ST_OPEN)
1110 match ("pause", gfc_match_pause, ST_NONE)
1111 match ("print", gfc_match_print, ST_WRITE)
1112 match ("read", gfc_match_read, ST_READ)
1113 match ("return", gfc_match_return, ST_RETURN)
1114 match ("rewind", gfc_match_rewind, ST_REWIND)
1115 match ("stop", gfc_match_stop, ST_STOP)
1116 match ("where", match_simple_where, ST_WHERE)
1117 match ("write", gfc_match_write, ST_WRITE)
1119 /* The gfc_match_assignment() above may have returned a MATCH_NO
1120 where the assignment was to a named constant. Check that
1121 special case here. */
1122 m = gfc_match_assignment ();
1125 gfc_error ("Cannot assign to a named constant at %C");
1126 gfc_free_expr (expr);
1127 gfc_undo_symbols ();
1128 gfc_current_locus = old_loc;
1132 /* All else has failed, so give up. See if any of the matchers has
1133 stored an error message of some sort. */
1134 if (gfc_error_check () == 0)
1135 gfc_error ("Unclassifiable statement in IF-clause at %C");
1137 gfc_free_expr (expr);
1142 gfc_error ("Syntax error in IF-clause at %C");
1145 gfc_free_expr (expr);
1149 /* At this point, we've matched the single IF and the action clause
1150 is in new_st. Rearrange things so that the IF statement appears
1153 p = gfc_get_code ();
1154 p->next = gfc_get_code ();
1156 p->next->loc = gfc_current_locus;
1161 gfc_clear_new_st ();
1163 new_st.op = EXEC_IF;
1172 /* Match an ELSE statement. */
1175 gfc_match_else (void)
1177 char name[GFC_MAX_SYMBOL_LEN + 1];
1179 if (gfc_match_eos () == MATCH_YES)
1182 if (gfc_match_name (name) != MATCH_YES
1183 || gfc_current_block () == NULL
1184 || gfc_match_eos () != MATCH_YES)
1186 gfc_error ("Unexpected junk after ELSE statement at %C");
1190 if (strcmp (name, gfc_current_block ()->name) != 0)
1192 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1193 name, gfc_current_block ()->name);
1201 /* Match an ELSE IF statement. */
1204 gfc_match_elseif (void)
1206 char name[GFC_MAX_SYMBOL_LEN + 1];
1210 m = gfc_match (" ( %e ) then", &expr);
1214 if (gfc_match_eos () == MATCH_YES)
1217 if (gfc_match_name (name) != MATCH_YES
1218 || gfc_current_block () == NULL
1219 || gfc_match_eos () != MATCH_YES)
1221 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1225 if (strcmp (name, gfc_current_block ()->name) != 0)
1227 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1228 name, gfc_current_block ()->name);
1233 new_st.op = EXEC_IF;
1238 gfc_free_expr (expr);
1243 /* Free a gfc_iterator structure. */
1246 gfc_free_iterator (gfc_iterator *iter, int flag)
1251 gfc_free_expr (iter->var);
1252 gfc_free_expr (iter->start);
1253 gfc_free_expr (iter->end);
1254 gfc_free_expr (iter->step);
1261 /* Match a DO statement. */
1266 gfc_iterator iter, *ip;
1268 gfc_st_label *label;
1271 old_loc = gfc_current_locus;
1274 iter.var = iter.start = iter.end = iter.step = NULL;
1276 m = gfc_match_label ();
1277 if (m == MATCH_ERROR)
1280 if (gfc_match (" do") != MATCH_YES)
1283 m = gfc_match_st_label (&label);
1284 if (m == MATCH_ERROR)
1287 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1289 if (gfc_match_eos () == MATCH_YES)
1291 iter.end = gfc_logical_expr (1, NULL);
1292 new_st.op = EXEC_DO_WHILE;
1296 /* match an optional comma, if no comma is found a space is obligatory. */
1297 if (gfc_match_char(',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1300 /* See if we have a DO WHILE. */
1301 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1303 new_st.op = EXEC_DO_WHILE;
1307 /* The abortive DO WHILE may have done something to the symbol
1308 table, so we start over: */
1309 gfc_undo_symbols ();
1310 gfc_current_locus = old_loc;
1312 gfc_match_label (); /* This won't error */
1313 gfc_match (" do "); /* This will work */
1315 gfc_match_st_label (&label); /* Can't error out */
1316 gfc_match_char (','); /* Optional comma */
1318 m = gfc_match_iterator (&iter, 0);
1321 if (m == MATCH_ERROR)
1324 gfc_check_do_variable (iter.var->symtree);
1326 if (gfc_match_eos () != MATCH_YES)
1328 gfc_syntax_error (ST_DO);
1332 new_st.op = EXEC_DO;
1336 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1339 new_st.label = label;
1341 if (new_st.op == EXEC_DO_WHILE)
1342 new_st.expr = iter.end;
1345 new_st.ext.iterator = ip = gfc_get_iterator ();
1352 gfc_free_iterator (&iter, 0);
1358 /* Match an EXIT or CYCLE statement. */
1361 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1363 gfc_state_data *p, *o;
1367 if (gfc_match_eos () == MATCH_YES)
1371 m = gfc_match ("% %s%t", &sym);
1372 if (m == MATCH_ERROR)
1376 gfc_syntax_error (st);
1380 if (sym->attr.flavor != FL_LABEL)
1382 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1383 sym->name, gfc_ascii_statement (st));
1388 /* Find the loop mentioned specified by the label (or lack of a
1390 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1391 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1393 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1399 gfc_error ("%s statement at %C is not within a loop",
1400 gfc_ascii_statement (st));
1402 gfc_error ("%s statement at %C is not within loop '%s'",
1403 gfc_ascii_statement (st), sym->name);
1410 gfc_error ("%s statement at %C leaving OpenMP structured block",
1411 gfc_ascii_statement (st));
1414 else if (st == ST_EXIT
1415 && p->previous != NULL
1416 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1417 && (p->previous->head->op == EXEC_OMP_DO
1418 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1420 gcc_assert (p->previous->head->next != NULL);
1421 gcc_assert (p->previous->head->next->op == EXEC_DO
1422 || p->previous->head->next->op == EXEC_DO_WHILE);
1423 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1427 /* Save the first statement in the loop - needed by the backend. */
1428 new_st.ext.whichloop = p->head;
1431 /* new_st.sym = sym;*/
1437 /* Match the EXIT statement. */
1440 gfc_match_exit (void)
1442 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1446 /* Match the CYCLE statement. */
1449 gfc_match_cycle (void)
1451 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1455 /* Match a number or character constant after a STOP or PAUSE statement. */
1458 gfc_match_stopcode (gfc_statement st)
1468 if (gfc_match_eos () != MATCH_YES)
1470 m = gfc_match_small_literal_int (&stop_code, &cnt);
1471 if (m == MATCH_ERROR)
1474 if (m == MATCH_YES && cnt > 5)
1476 gfc_error ("Too many digits in STOP code at %C");
1482 /* Try a character constant. */
1483 m = gfc_match_expr (&e);
1484 if (m == MATCH_ERROR)
1488 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1492 if (gfc_match_eos () != MATCH_YES)
1496 if (gfc_pure (NULL))
1498 gfc_error ("%s statement not allowed in PURE procedure at %C",
1499 gfc_ascii_statement (st));
1503 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1505 new_st.ext.stop_code = stop_code;
1510 gfc_syntax_error (st);
1518 /* Match the (deprecated) PAUSE statement. */
1521 gfc_match_pause (void)
1525 m = gfc_match_stopcode (ST_PAUSE);
1528 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: PAUSE statement at %C")
1536 /* Match the STOP statement. */
1539 gfc_match_stop (void)
1541 return gfc_match_stopcode (ST_STOP);
1545 /* Match a CONTINUE statement. */
1548 gfc_match_continue (void)
1550 if (gfc_match_eos () != MATCH_YES)
1552 gfc_syntax_error (ST_CONTINUE);
1556 new_st.op = EXEC_CONTINUE;
1561 /* Match the (deprecated) ASSIGN statement. */
1564 gfc_match_assign (void)
1567 gfc_st_label *label;
1569 if (gfc_match (" %l", &label) == MATCH_YES)
1571 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1573 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1575 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: ASSIGN "
1580 expr->symtree->n.sym->attr.assign = 1;
1582 new_st.op = EXEC_LABEL_ASSIGN;
1583 new_st.label = label;
1592 /* Match the GO TO statement. As a computed GOTO statement is
1593 matched, it is transformed into an equivalent SELECT block. No
1594 tree is necessary, and the resulting jumps-to-jumps are
1595 specifically optimized away by the back end. */
1598 gfc_match_goto (void)
1600 gfc_code *head, *tail;
1603 gfc_st_label *label;
1607 if (gfc_match (" %l%t", &label) == MATCH_YES)
1609 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1612 new_st.op = EXEC_GOTO;
1613 new_st.label = label;
1617 /* The assigned GO TO statement. */
1619 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1621 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: Assigned GOTO "
1626 new_st.op = EXEC_GOTO;
1629 if (gfc_match_eos () == MATCH_YES)
1632 /* Match label list. */
1633 gfc_match_char (',');
1634 if (gfc_match_char ('(') != MATCH_YES)
1636 gfc_syntax_error (ST_GOTO);
1643 m = gfc_match_st_label (&label);
1647 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1651 head = tail = gfc_get_code ();
1654 tail->block = gfc_get_code ();
1658 tail->label = label;
1659 tail->op = EXEC_GOTO;
1661 while (gfc_match_char (',') == MATCH_YES);
1663 if (gfc_match (")%t") != MATCH_YES)
1668 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1671 new_st.block = head;
1676 /* Last chance is a computed GO TO statement. */
1677 if (gfc_match_char ('(') != MATCH_YES)
1679 gfc_syntax_error (ST_GOTO);
1688 m = gfc_match_st_label (&label);
1692 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1696 head = tail = gfc_get_code ();
1699 tail->block = gfc_get_code ();
1703 cp = gfc_get_case ();
1704 cp->low = cp->high = gfc_int_expr (i++);
1706 tail->op = EXEC_SELECT;
1707 tail->ext.case_list = cp;
1709 tail->next = gfc_get_code ();
1710 tail->next->op = EXEC_GOTO;
1711 tail->next->label = label;
1713 while (gfc_match_char (',') == MATCH_YES);
1715 if (gfc_match_char (')') != MATCH_YES)
1720 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1724 /* Get the rest of the statement. */
1725 gfc_match_char (',');
1727 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1730 /* At this point, a computed GOTO has been fully matched and an
1731 equivalent SELECT statement constructed. */
1733 new_st.op = EXEC_SELECT;
1736 /* Hack: For a "real" SELECT, the expression is in expr. We put
1737 it in expr2 so we can distinguish then and produce the correct
1739 new_st.expr2 = expr;
1740 new_st.block = head;
1744 gfc_syntax_error (ST_GOTO);
1746 gfc_free_statements (head);
1751 /* Frees a list of gfc_alloc structures. */
1754 gfc_free_alloc_list (gfc_alloc *p)
1761 gfc_free_expr (p->expr);
1767 /* Match an ALLOCATE statement. */
1770 gfc_match_allocate (void)
1772 gfc_alloc *head, *tail;
1779 if (gfc_match_char ('(') != MATCH_YES)
1785 head = tail = gfc_get_alloc ();
1788 tail->next = gfc_get_alloc ();
1792 m = gfc_match_variable (&tail->expr, 0);
1795 if (m == MATCH_ERROR)
1798 if (gfc_check_do_variable (tail->expr->symtree))
1802 && gfc_impure_variable (tail->expr->symtree->n.sym))
1804 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1809 if (tail->expr->ts.type == BT_DERIVED)
1810 tail->expr->ts.derived = gfc_use_derived (tail->expr->ts.derived);
1812 if (gfc_match_char (',') != MATCH_YES)
1815 m = gfc_match (" stat = %v", &stat);
1816 if (m == MATCH_ERROR)
1824 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1826 gfc_error ("STAT variable '%s' of ALLOCATE statement at %C cannot "
1827 "be INTENT(IN)", stat->symtree->n.sym->name);
1831 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1833 gfc_error ("Illegal STAT variable in ALLOCATE statement at %C "
1834 "for a PURE procedure");
1838 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1840 gfc_error ("STAT expression at %C must be a variable");
1844 gfc_check_do_variable(stat->symtree);
1847 if (gfc_match (" )%t") != MATCH_YES)
1850 new_st.op = EXEC_ALLOCATE;
1852 new_st.ext.alloc_list = head;
1857 gfc_syntax_error (ST_ALLOCATE);
1860 gfc_free_expr (stat);
1861 gfc_free_alloc_list (head);
1866 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1867 a set of pointer assignments to intrinsic NULL(). */
1870 gfc_match_nullify (void)
1878 if (gfc_match_char ('(') != MATCH_YES)
1883 m = gfc_match_variable (&p, 0);
1884 if (m == MATCH_ERROR)
1889 if (gfc_check_do_variable(p->symtree))
1892 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1894 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
1898 /* build ' => NULL() ' */
1899 e = gfc_get_expr ();
1900 e->where = gfc_current_locus;
1901 e->expr_type = EXPR_NULL;
1902 e->ts.type = BT_UNKNOWN;
1909 tail->next = gfc_get_code ();
1913 tail->op = EXEC_POINTER_ASSIGN;
1917 if (gfc_match (" )%t") == MATCH_YES)
1919 if (gfc_match_char (',') != MATCH_YES)
1926 gfc_syntax_error (ST_NULLIFY);
1929 gfc_free_statements (new_st.next);
1934 /* Match a DEALLOCATE statement. */
1937 gfc_match_deallocate (void)
1939 gfc_alloc *head, *tail;
1946 if (gfc_match_char ('(') != MATCH_YES)
1952 head = tail = gfc_get_alloc ();
1955 tail->next = gfc_get_alloc ();
1959 m = gfc_match_variable (&tail->expr, 0);
1960 if (m == MATCH_ERROR)
1965 if (gfc_check_do_variable (tail->expr->symtree))
1969 && gfc_impure_variable (tail->expr->symtree->n.sym))
1971 gfc_error ("Illegal deallocate-expression in DEALLOCATE at %C "
1972 "for a PURE procedure");
1976 if (gfc_match_char (',') != MATCH_YES)
1979 m = gfc_match (" stat = %v", &stat);
1980 if (m == MATCH_ERROR)
1988 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1990 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1991 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
1995 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
1997 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
1998 "for a PURE procedure");
2002 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
2004 gfc_error ("STAT expression at %C must be a variable");
2008 gfc_check_do_variable(stat->symtree);
2011 if (gfc_match (" )%t") != MATCH_YES)
2014 new_st.op = EXEC_DEALLOCATE;
2016 new_st.ext.alloc_list = head;
2021 gfc_syntax_error (ST_DEALLOCATE);
2024 gfc_free_expr (stat);
2025 gfc_free_alloc_list (head);
2030 /* Match a RETURN statement. */
2033 gfc_match_return (void)
2037 gfc_compile_state s;
2041 if (gfc_match_eos () == MATCH_YES)
2044 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2046 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2051 if (gfc_current_form == FORM_FREE)
2053 /* The following are valid, so we can't require a blank after the
2057 c = gfc_peek_char ();
2058 if (ISALPHA (c) || ISDIGIT (c))
2062 m = gfc_match (" %e%t", &e);
2065 if (m == MATCH_ERROR)
2068 gfc_syntax_error (ST_RETURN);
2075 gfc_enclosing_unit (&s);
2076 if (s == COMP_PROGRAM
2077 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2078 "main program at %C") == FAILURE)
2081 new_st.op = EXEC_RETURN;
2088 /* Match a CALL statement. The tricky part here are possible
2089 alternate return specifiers. We handle these by having all
2090 "subroutines" actually return an integer via a register that gives
2091 the return number. If the call specifies alternate returns, we
2092 generate code for a SELECT statement whose case clauses contain
2093 GOTOs to the various labels. */
2096 gfc_match_call (void)
2098 char name[GFC_MAX_SYMBOL_LEN + 1];
2099 gfc_actual_arglist *a, *arglist;
2109 m = gfc_match ("% %n", name);
2115 if (gfc_get_ha_sym_tree (name, &st))
2119 gfc_set_sym_referenced (sym);
2121 if (!sym->attr.generic
2122 && !sym->attr.subroutine
2123 && gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2126 if (gfc_match_eos () != MATCH_YES)
2128 m = gfc_match_actual_arglist (1, &arglist);
2131 if (m == MATCH_ERROR)
2134 if (gfc_match_eos () != MATCH_YES)
2138 /* If any alternate return labels were found, construct a SELECT
2139 statement that will jump to the right place. */
2142 for (a = arglist; a; a = a->next)
2143 if (a->expr == NULL)
2148 gfc_symtree *select_st;
2149 gfc_symbol *select_sym;
2150 char name[GFC_MAX_SYMBOL_LEN + 1];
2152 new_st.next = c = gfc_get_code ();
2153 c->op = EXEC_SELECT;
2154 sprintf (name, "_result_%s", sym->name);
2155 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail */
2157 select_sym = select_st->n.sym;
2158 select_sym->ts.type = BT_INTEGER;
2159 select_sym->ts.kind = gfc_default_integer_kind;
2160 gfc_set_sym_referenced (select_sym);
2161 c->expr = gfc_get_expr ();
2162 c->expr->expr_type = EXPR_VARIABLE;
2163 c->expr->symtree = select_st;
2164 c->expr->ts = select_sym->ts;
2165 c->expr->where = gfc_current_locus;
2168 for (a = arglist; a; a = a->next)
2170 if (a->expr != NULL)
2173 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2178 c->block = gfc_get_code ();
2180 c->op = EXEC_SELECT;
2182 new_case = gfc_get_case ();
2183 new_case->high = new_case->low = gfc_int_expr (i);
2184 c->ext.case_list = new_case;
2186 c->next = gfc_get_code ();
2187 c->next->op = EXEC_GOTO;
2188 c->next->label = a->label;
2192 new_st.op = EXEC_CALL;
2193 new_st.symtree = st;
2194 new_st.ext.actual = arglist;
2199 gfc_syntax_error (ST_CALL);
2202 gfc_free_actual_arglist (arglist);
2207 /* Given a name, return a pointer to the common head structure,
2208 creating it if it does not exist. If FROM_MODULE is nonzero, we
2209 mangle the name so that it doesn't interfere with commons defined
2210 in the using namespace.
2211 TODO: Add to global symbol tree. */
2214 gfc_get_common (const char *name, int from_module)
2217 static int serial = 0;
2218 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
2222 /* A use associated common block is only needed to correctly layout
2223 the variables it contains. */
2224 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2225 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2229 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2232 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2235 if (st->n.common == NULL)
2237 st->n.common = gfc_get_common_head ();
2238 st->n.common->where = gfc_current_locus;
2239 strcpy (st->n.common->name, name);
2242 return st->n.common;
2246 /* Match a common block name. */
2249 match_common_name (char *name)
2253 if (gfc_match_char ('/') == MATCH_NO)
2259 if (gfc_match_char ('/') == MATCH_YES)
2265 m = gfc_match_name (name);
2267 if (m == MATCH_ERROR)
2269 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2272 gfc_error ("Syntax error in common block name at %C");
2277 /* Match a COMMON statement. */
2280 gfc_match_common (void)
2282 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2283 char name[GFC_MAX_SYMBOL_LEN + 1];
2290 old_blank_common = gfc_current_ns->blank_common.head;
2291 if (old_blank_common)
2293 while (old_blank_common->common_next)
2294 old_blank_common = old_blank_common->common_next;
2301 m = match_common_name (name);
2302 if (m == MATCH_ERROR)
2305 gsym = gfc_get_gsymbol (name);
2306 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2308 gfc_error ("Symbol '%s' at %C is already an external symbol that "
2309 "is not COMMON", name);
2313 if (gsym->type == GSYM_UNKNOWN)
2315 gsym->type = GSYM_COMMON;
2316 gsym->where = gfc_current_locus;
2322 if (name[0] == '\0')
2324 if (gfc_current_ns->is_block_data)
2326 gfc_warning ("BLOCK DATA unit cannot contain blank COMMON "
2329 t = &gfc_current_ns->blank_common;
2330 if (t->head == NULL)
2331 t->where = gfc_current_locus;
2335 t = gfc_get_common (name, 0);
2344 while (tail->common_next)
2345 tail = tail->common_next;
2348 /* Grab the list of symbols. */
2351 m = gfc_match_symbol (&sym, 0);
2352 if (m == MATCH_ERROR)
2357 if (sym->attr.in_common)
2359 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2364 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2367 if (sym->value != NULL
2368 && (name[0] == '\0' || !sym->attr.data))
2370 if (name[0] == '\0')
2371 gfc_error ("Previously initialized symbol '%s' in "
2372 "blank COMMON block at %C", sym->name);
2374 gfc_error ("Previously initialized symbol '%s' in "
2375 "COMMON block '%s' at %C", sym->name, name);
2379 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2382 /* Derived type names must have the SEQUENCE attribute. */
2383 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2385 gfc_error ("Derived type variable in COMMON at %C does not "
2386 "have the SEQUENCE attribute");
2391 tail->common_next = sym;
2397 /* Deal with an optional array specification after the
2399 m = gfc_match_array_spec (&as);
2400 if (m == MATCH_ERROR)
2405 if (as->type != AS_EXPLICIT)
2407 gfc_error ("Array specification for symbol '%s' in COMMON "
2408 "at %C must be explicit", sym->name);
2412 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2415 if (sym->attr.pointer)
2417 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
2418 "POINTER array", sym->name);
2427 sym->common_head = t;
2429 /* Check to see if the symbol is already in an equivalence group.
2430 If it is, set the other members as being in common. */
2431 if (sym->attr.in_equivalence)
2433 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2435 for (e2 = e1; e2; e2 = e2->eq)
2436 if (e2->expr->symtree->n.sym == sym)
2443 for (e2 = e1; e2; e2 = e2->eq)
2445 other = e2->expr->symtree->n.sym;
2446 if (other->common_head
2447 && other->common_head != sym->common_head)
2449 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2450 "%C is being indirectly equivalenced to "
2451 "another COMMON block '%s'",
2452 sym->name, sym->common_head->name,
2453 other->common_head->name);
2456 other->attr.in_common = 1;
2457 other->common_head = t;
2463 gfc_gobble_whitespace ();
2464 if (gfc_match_eos () == MATCH_YES)
2466 if (gfc_peek_char () == '/')
2468 if (gfc_match_char (',') != MATCH_YES)
2470 gfc_gobble_whitespace ();
2471 if (gfc_peek_char () == '/')
2480 gfc_syntax_error (ST_COMMON);
2483 if (old_blank_common)
2484 old_blank_common->common_next = NULL;
2486 gfc_current_ns->blank_common.head = NULL;
2487 gfc_free_array_spec (as);
2492 /* Match a BLOCK DATA program unit. */
2495 gfc_match_block_data (void)
2497 char name[GFC_MAX_SYMBOL_LEN + 1];
2501 if (gfc_match_eos () == MATCH_YES)
2503 gfc_new_block = NULL;
2507 m = gfc_match ("% %n%t", name);
2511 if (gfc_get_symbol (name, NULL, &sym))
2514 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2517 gfc_new_block = sym;
2523 /* Free a namelist structure. */
2526 gfc_free_namelist (gfc_namelist *name)
2530 for (; name; name = n)
2538 /* Match a NAMELIST statement. */
2541 gfc_match_namelist (void)
2543 gfc_symbol *group_name, *sym;
2547 m = gfc_match (" / %s /", &group_name);
2550 if (m == MATCH_ERROR)
2555 if (group_name->ts.type != BT_UNKNOWN)
2557 gfc_error ("Namelist group name '%s' at %C already has a basic "
2558 "type of %s", group_name->name,
2559 gfc_typename (&group_name->ts));
2563 if (group_name->attr.flavor == FL_NAMELIST
2564 && group_name->attr.use_assoc
2565 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2566 "at %C already is USE associated and can"
2567 "not be respecified.", group_name->name)
2571 if (group_name->attr.flavor != FL_NAMELIST
2572 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2573 group_name->name, NULL) == FAILURE)
2578 m = gfc_match_symbol (&sym, 1);
2581 if (m == MATCH_ERROR)
2584 if (sym->attr.in_namelist == 0
2585 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2588 /* Use gfc_error_check here, rather than goto error, so that
2589 these are the only errors for the next two lines. */
2590 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2592 gfc_error ("Assumed size array '%s' in namelist '%s' at "
2593 "%C is not allowed", sym->name, group_name->name);
2597 if (sym->ts.type == BT_CHARACTER && sym->ts.cl->length == NULL)
2599 gfc_error ("Assumed character length '%s' in namelist '%s' at "
2600 "%C is not allowed", sym->name, group_name->name);
2604 if (sym->as && sym->as->type == AS_ASSUMED_SHAPE
2605 && gfc_notify_std (GFC_STD_GNU, "Assumed shape array '%s' in "
2606 "namelist '%s' at %C is an extension.",
2607 sym->name, group_name->name) == FAILURE)
2610 nl = gfc_get_namelist ();
2614 if (group_name->namelist == NULL)
2615 group_name->namelist = group_name->namelist_tail = nl;
2618 group_name->namelist_tail->next = nl;
2619 group_name->namelist_tail = nl;
2622 if (gfc_match_eos () == MATCH_YES)
2625 m = gfc_match_char (',');
2627 if (gfc_match_char ('/') == MATCH_YES)
2629 m2 = gfc_match (" %s /", &group_name);
2630 if (m2 == MATCH_YES)
2632 if (m2 == MATCH_ERROR)
2646 gfc_syntax_error (ST_NAMELIST);
2653 /* Match a MODULE statement. */
2656 gfc_match_module (void)
2660 m = gfc_match (" %s%t", &gfc_new_block);
2664 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2665 gfc_new_block->name, NULL) == FAILURE)
2672 /* Free equivalence sets and lists. Recursively is the easiest way to
2676 gfc_free_equiv (gfc_equiv *eq)
2681 gfc_free_equiv (eq->eq);
2682 gfc_free_equiv (eq->next);
2683 gfc_free_expr (eq->expr);
2688 /* Match an EQUIVALENCE statement. */
2691 gfc_match_equivalence (void)
2693 gfc_equiv *eq, *set, *tail;
2697 gfc_common_head *common_head = NULL;
2705 eq = gfc_get_equiv ();
2709 eq->next = gfc_current_ns->equiv;
2710 gfc_current_ns->equiv = eq;
2712 if (gfc_match_char ('(') != MATCH_YES)
2716 common_flag = FALSE;
2721 m = gfc_match_equiv_variable (&set->expr);
2722 if (m == MATCH_ERROR)
2727 /* count the number of objects. */
2730 if (gfc_match_char ('%') == MATCH_YES)
2732 gfc_error ("Derived type component %C is not a "
2733 "permitted EQUIVALENCE member");
2737 for (ref = set->expr->ref; ref; ref = ref->next)
2738 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2740 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
2741 "be an array section");
2745 sym = set->expr->symtree->n.sym;
2747 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
2750 if (sym->attr.in_common)
2753 common_head = sym->common_head;
2756 if (gfc_match_char (')') == MATCH_YES)
2759 if (gfc_match_char (',') != MATCH_YES)
2762 set->eq = gfc_get_equiv ();
2768 gfc_error ("EQUIVALENCE at %C requires two or more objects");
2772 /* If one of the members of an equivalence is in common, then
2773 mark them all as being in common. Before doing this, check
2774 that members of the equivalence group are not in different
2777 for (set = eq; set; set = set->eq)
2779 sym = set->expr->symtree->n.sym;
2780 if (sym->common_head && sym->common_head != common_head)
2782 gfc_error ("Attempt to indirectly overlap COMMON "
2783 "blocks %s and %s by EQUIVALENCE at %C",
2784 sym->common_head->name, common_head->name);
2787 sym->attr.in_common = 1;
2788 sym->common_head = common_head;
2791 if (gfc_match_eos () == MATCH_YES)
2793 if (gfc_match_char (',') != MATCH_YES)
2800 gfc_syntax_error (ST_EQUIVALENCE);
2806 gfc_free_equiv (gfc_current_ns->equiv);
2807 gfc_current_ns->equiv = eq;
2813 /* Check that a statement function is not recursive. This is done by looking
2814 for the statement function symbol(sym) by looking recursively through its
2815 expression(e). If a reference to sym is found, true is returned.
2816 12.5.4 requires that any variable of function that is implicitly typed
2817 shall have that type confirmed by any subsequent type declaration. The
2818 implicit typing is conveniently done here. */
2821 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
2823 gfc_actual_arglist *arg;
2830 switch (e->expr_type)
2833 for (arg = e->value.function.actual; arg; arg = arg->next)
2835 if (sym->name == arg->name || recursive_stmt_fcn (arg->expr, sym))
2839 if (e->symtree == NULL)
2842 /* Check the name before testing for nested recursion! */
2843 if (sym->name == e->symtree->n.sym->name)
2846 /* Catch recursion via other statement functions. */
2847 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
2848 && e->symtree->n.sym->value
2849 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
2852 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
2853 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
2858 if (e->symtree && sym->name == e->symtree->n.sym->name)
2861 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
2862 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
2866 if (recursive_stmt_fcn (e->value.op.op1, sym)
2867 || recursive_stmt_fcn (e->value.op.op2, sym))
2875 /* Component references do not need to be checked. */
2878 for (ref = e->ref; ref; ref = ref->next)
2883 for (i = 0; i < ref->u.ar.dimen; i++)
2885 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
2886 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
2887 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
2893 if (recursive_stmt_fcn (ref->u.ss.start, sym)
2894 || recursive_stmt_fcn (ref->u.ss.end, sym))
2908 /* Match a statement function declaration. It is so easy to match
2909 non-statement function statements with a MATCH_ERROR as opposed to
2910 MATCH_NO that we suppress error message in most cases. */
2913 gfc_match_st_function (void)
2915 gfc_error_buf old_error;
2920 m = gfc_match_symbol (&sym, 0);
2924 gfc_push_error (&old_error);
2926 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
2927 sym->name, NULL) == FAILURE)
2930 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2933 m = gfc_match (" = %e%t", &expr);
2937 gfc_free_error (&old_error);
2938 if (m == MATCH_ERROR)
2941 if (recursive_stmt_fcn (expr, sym))
2943 gfc_error ("Statement function at %L is recursive", &expr->where);
2952 gfc_pop_error (&old_error);
2957 /***************** SELECT CASE subroutines ******************/
2959 /* Free a single case structure. */
2962 free_case (gfc_case *p)
2964 if (p->low == p->high)
2966 gfc_free_expr (p->low);
2967 gfc_free_expr (p->high);
2972 /* Free a list of case structures. */
2975 gfc_free_case_list (gfc_case *p)
2987 /* Match a single case selector. */
2990 match_case_selector (gfc_case **cp)
2995 c = gfc_get_case ();
2996 c->where = gfc_current_locus;
2998 if (gfc_match_char (':') == MATCH_YES)
3000 m = gfc_match_init_expr (&c->high);
3003 if (m == MATCH_ERROR)
3008 m = gfc_match_init_expr (&c->low);
3009 if (m == MATCH_ERROR)
3014 /* If we're not looking at a ':' now, make a range out of a single
3015 target. Else get the upper bound for the case range. */
3016 if (gfc_match_char (':') != MATCH_YES)
3020 m = gfc_match_init_expr (&c->high);
3021 if (m == MATCH_ERROR)
3023 /* MATCH_NO is fine. It's OK if nothing is there! */
3031 gfc_error ("Expected initialization expression in CASE at %C");
3039 /* Match the end of a case statement. */
3042 match_case_eos (void)
3044 char name[GFC_MAX_SYMBOL_LEN + 1];
3047 if (gfc_match_eos () == MATCH_YES)
3050 /* If the case construct doesn't have a case-construct-name, we
3051 should have matched the EOS. */
3052 if (!gfc_current_block ())
3054 gfc_error ("Expected the name of the select case construct at %C");
3058 gfc_gobble_whitespace ();
3060 m = gfc_match_name (name);
3064 if (strcmp (name, gfc_current_block ()->name) != 0)
3066 gfc_error ("Expected case name of '%s' at %C",
3067 gfc_current_block ()->name);
3071 return gfc_match_eos ();
3075 /* Match a SELECT statement. */
3078 gfc_match_select (void)
3083 m = gfc_match_label ();
3084 if (m == MATCH_ERROR)
3087 m = gfc_match (" select case ( %e )%t", &expr);
3091 new_st.op = EXEC_SELECT;
3098 /* Match a CASE statement. */
3101 gfc_match_case (void)
3103 gfc_case *c, *head, *tail;
3108 if (gfc_current_state () != COMP_SELECT)
3110 gfc_error ("Unexpected CASE statement at %C");
3114 if (gfc_match ("% default") == MATCH_YES)
3116 m = match_case_eos ();
3119 if (m == MATCH_ERROR)
3122 new_st.op = EXEC_SELECT;
3123 c = gfc_get_case ();
3124 c->where = gfc_current_locus;
3125 new_st.ext.case_list = c;
3129 if (gfc_match_char ('(') != MATCH_YES)
3134 if (match_case_selector (&c) == MATCH_ERROR)
3144 if (gfc_match_char (')') == MATCH_YES)
3146 if (gfc_match_char (',') != MATCH_YES)
3150 m = match_case_eos ();
3153 if (m == MATCH_ERROR)
3156 new_st.op = EXEC_SELECT;
3157 new_st.ext.case_list = head;
3162 gfc_error ("Syntax error in CASE-specification at %C");
3165 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3169 /********************* WHERE subroutines ********************/
3171 /* Match the rest of a simple WHERE statement that follows an IF statement.
3175 match_simple_where (void)
3181 m = gfc_match (" ( %e )", &expr);
3185 m = gfc_match_assignment ();
3188 if (m == MATCH_ERROR)
3191 if (gfc_match_eos () != MATCH_YES)
3194 c = gfc_get_code ();
3198 c->next = gfc_get_code ();
3201 gfc_clear_new_st ();
3203 new_st.op = EXEC_WHERE;
3209 gfc_syntax_error (ST_WHERE);
3212 gfc_free_expr (expr);
3216 /* Match a WHERE statement. */
3219 gfc_match_where (gfc_statement *st)
3225 m0 = gfc_match_label ();
3226 if (m0 == MATCH_ERROR)
3229 m = gfc_match (" where ( %e )", &expr);
3233 if (gfc_match_eos () == MATCH_YES)
3235 *st = ST_WHERE_BLOCK;
3236 new_st.op = EXEC_WHERE;
3241 m = gfc_match_assignment ();
3243 gfc_syntax_error (ST_WHERE);
3247 gfc_free_expr (expr);
3251 /* We've got a simple WHERE statement. */
3253 c = gfc_get_code ();
3257 c->next = gfc_get_code ();
3260 gfc_clear_new_st ();
3262 new_st.op = EXEC_WHERE;
3269 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3270 new_st if successful. */
3273 gfc_match_elsewhere (void)
3275 char name[GFC_MAX_SYMBOL_LEN + 1];
3279 if (gfc_current_state () != COMP_WHERE)
3281 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3287 if (gfc_match_char ('(') == MATCH_YES)
3289 m = gfc_match_expr (&expr);
3292 if (m == MATCH_ERROR)
3295 if (gfc_match_char (')') != MATCH_YES)
3299 if (gfc_match_eos () != MATCH_YES)
3300 { /* Better be a name at this point */
3301 m = gfc_match_name (name);
3304 if (m == MATCH_ERROR)
3307 if (gfc_match_eos () != MATCH_YES)
3310 if (strcmp (name, gfc_current_block ()->name) != 0)
3312 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3313 name, gfc_current_block ()->name);
3318 new_st.op = EXEC_WHERE;
3323 gfc_syntax_error (ST_ELSEWHERE);
3326 gfc_free_expr (expr);
3331 /******************** FORALL subroutines ********************/
3333 /* Free a list of FORALL iterators. */
3336 gfc_free_forall_iterator (gfc_forall_iterator *iter)
3338 gfc_forall_iterator *next;
3343 gfc_free_expr (iter->var);
3344 gfc_free_expr (iter->start);
3345 gfc_free_expr (iter->end);
3346 gfc_free_expr (iter->stride);
3353 /* Match an iterator as part of a FORALL statement. The format is:
3355 <var> = <start>:<end>[:<stride>]
3357 On MATCH_NO, the caller tests for the possibility that there is a
3358 scalar mask expression. */
3361 match_forall_iterator (gfc_forall_iterator **result)
3363 gfc_forall_iterator *iter;
3367 where = gfc_current_locus;
3368 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3370 m = gfc_match_expr (&iter->var);
3374 if (gfc_match_char ('=') != MATCH_YES
3375 || iter->var->expr_type != EXPR_VARIABLE)
3381 m = gfc_match_expr (&iter->start);
3385 if (gfc_match_char (':') != MATCH_YES)
3388 m = gfc_match_expr (&iter->end);
3391 if (m == MATCH_ERROR)
3394 if (gfc_match_char (':') == MATCH_NO)
3395 iter->stride = gfc_int_expr (1);
3398 m = gfc_match_expr (&iter->stride);
3401 if (m == MATCH_ERROR)
3405 /* Mark the iteration variable's symbol as used as a FORALL index. */
3406 iter->var->symtree->n.sym->forall_index = true;
3412 gfc_error ("Syntax error in FORALL iterator at %C");
3417 gfc_current_locus = where;
3418 gfc_free_forall_iterator (iter);
3423 /* Match the header of a FORALL statement. */
3426 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
3428 gfc_forall_iterator *head, *tail, *new;
3432 gfc_gobble_whitespace ();
3437 if (gfc_match_char ('(') != MATCH_YES)
3440 m = match_forall_iterator (&new);
3441 if (m == MATCH_ERROR)
3450 if (gfc_match_char (',') != MATCH_YES)
3453 m = match_forall_iterator (&new);
3454 if (m == MATCH_ERROR)
3464 /* Have to have a mask expression */
3466 m = gfc_match_expr (&msk);
3469 if (m == MATCH_ERROR)
3475 if (gfc_match_char (')') == MATCH_NO)
3483 gfc_syntax_error (ST_FORALL);
3486 gfc_free_expr (msk);
3487 gfc_free_forall_iterator (head);
3492 /* Match the rest of a simple FORALL statement that follows an
3496 match_simple_forall (void)
3498 gfc_forall_iterator *head;
3507 m = match_forall_header (&head, &mask);
3514 m = gfc_match_assignment ();
3516 if (m == MATCH_ERROR)
3520 m = gfc_match_pointer_assignment ();
3521 if (m == MATCH_ERROR)
3527 c = gfc_get_code ();
3529 c->loc = gfc_current_locus;
3531 if (gfc_match_eos () != MATCH_YES)
3534 gfc_clear_new_st ();
3535 new_st.op = EXEC_FORALL;
3537 new_st.ext.forall_iterator = head;
3538 new_st.block = gfc_get_code ();
3540 new_st.block->op = EXEC_FORALL;
3541 new_st.block->next = c;
3546 gfc_syntax_error (ST_FORALL);
3549 gfc_free_forall_iterator (head);
3550 gfc_free_expr (mask);
3556 /* Match a FORALL statement. */
3559 gfc_match_forall (gfc_statement *st)
3561 gfc_forall_iterator *head;
3570 m0 = gfc_match_label ();
3571 if (m0 == MATCH_ERROR)
3574 m = gfc_match (" forall");
3578 m = match_forall_header (&head, &mask);
3579 if (m == MATCH_ERROR)
3584 if (gfc_match_eos () == MATCH_YES)
3586 *st = ST_FORALL_BLOCK;
3587 new_st.op = EXEC_FORALL;
3589 new_st.ext.forall_iterator = head;
3593 m = gfc_match_assignment ();
3594 if (m == MATCH_ERROR)
3598 m = gfc_match_pointer_assignment ();
3599 if (m == MATCH_ERROR)
3605 c = gfc_get_code ();
3607 c->loc = gfc_current_locus;
3609 gfc_clear_new_st ();
3610 new_st.op = EXEC_FORALL;
3612 new_st.ext.forall_iterator = head;
3613 new_st.block = gfc_get_code ();
3614 new_st.block->op = EXEC_FORALL;
3615 new_st.block->next = c;
3621 gfc_syntax_error (ST_FORALL);
3624 gfc_free_forall_iterator (head);
3625 gfc_free_expr (mask);
3626 gfc_free_statements (c);