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.
384 Note that options.c restricts max_identifier_length to not more
385 than GFC_MAX_SYMBOL_LEN. */
388 gfc_match_name (char *buffer)
393 old_loc = gfc_current_locus;
394 gfc_gobble_whitespace ();
396 c = gfc_next_char ();
397 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
399 if (gfc_error_flag_test() == 0)
400 gfc_error ("Invalid character in name at %C");
401 gfc_current_locus = old_loc;
411 if (i > gfc_option.max_identifier_length)
413 gfc_error ("Name at %C is too long");
417 old_loc = gfc_current_locus;
418 c = gfc_next_char ();
420 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
423 gfc_current_locus = old_loc;
429 /* Match a symbol on the input. Modifies the pointer to the symbol
430 pointer if successful. */
433 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
435 char buffer[GFC_MAX_SYMBOL_LEN + 1];
438 m = gfc_match_name (buffer);
443 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
444 ? MATCH_ERROR : MATCH_YES;
446 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
454 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
459 m = gfc_match_sym_tree (&st, host_assoc);
464 *matched_symbol = st->n.sym;
466 *matched_symbol = NULL;
469 *matched_symbol = NULL;
474 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
475 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
479 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
483 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
485 if (op == INTRINSIC_NONE)
493 /* Match a loop control phrase:
495 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
497 If the final integer expression is not present, a constant unity
498 expression is returned. We don't return MATCH_ERROR until after
499 the equals sign is seen. */
502 gfc_match_iterator (gfc_iterator *iter, int init_flag)
504 char name[GFC_MAX_SYMBOL_LEN + 1];
505 gfc_expr *var, *e1, *e2, *e3;
509 /* Match the start of an iterator without affecting the symbol table. */
511 start = gfc_current_locus;
512 m = gfc_match (" %n =", name);
513 gfc_current_locus = start;
518 m = gfc_match_variable (&var, 0);
522 gfc_match_char ('=');
526 if (var->ref != NULL)
528 gfc_error ("Loop variable at %C cannot be a sub-component");
532 if (var->symtree->n.sym->attr.intent == INTENT_IN)
534 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
535 var->symtree->n.sym->name);
539 var->symtree->n.sym->attr.implied_index = 1;
541 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
544 if (m == MATCH_ERROR)
547 if (gfc_match_char (',') != MATCH_YES)
550 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
553 if (m == MATCH_ERROR)
556 if (gfc_match_char (',') != MATCH_YES)
558 e3 = gfc_int_expr (1);
562 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
563 if (m == MATCH_ERROR)
567 gfc_error ("Expected a step value in iterator at %C");
579 gfc_error ("Syntax error in iterator at %C");
590 /* Tries to match the next non-whitespace character on the input.
591 This subroutine does not return MATCH_ERROR. */
594 gfc_match_char (char c)
598 where = gfc_current_locus;
599 gfc_gobble_whitespace ();
601 if (gfc_next_char () == c)
604 gfc_current_locus = where;
609 /* General purpose matching subroutine. The target string is a
610 scanf-like format string in which spaces correspond to arbitrary
611 whitespace (including no whitespace), characters correspond to
612 themselves. The %-codes are:
614 %% Literal percent sign
615 %e Expression, pointer to a pointer is set
616 %s Symbol, pointer to the symbol is set
617 %n Name, character buffer is set to name
618 %t Matches end of statement.
619 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
620 %l Matches a statement label
621 %v Matches a variable expression (an lvalue)
622 % Matches a required space (in free form) and optional spaces. */
625 gfc_match (const char *target, ...)
627 gfc_st_label **label;
636 old_loc = gfc_current_locus;
637 va_start (argp, target);
647 gfc_gobble_whitespace ();
658 vp = va_arg (argp, void **);
659 n = gfc_match_expr ((gfc_expr **) vp);
670 vp = va_arg (argp, void **);
671 n = gfc_match_variable ((gfc_expr **) vp, 0);
682 vp = va_arg (argp, void **);
683 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
694 np = va_arg (argp, char *);
695 n = gfc_match_name (np);
706 label = va_arg (argp, gfc_st_label **);
707 n = gfc_match_st_label (label);
718 ip = va_arg (argp, int *);
719 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
730 if (gfc_match_eos () != MATCH_YES)
738 if (gfc_match_space () == MATCH_YES)
744 break; /* Fall through to character matcher. */
747 gfc_internal_error ("gfc_match(): Bad match code %c", c);
751 if (c == gfc_next_char ())
761 /* Clean up after a failed match. */
762 gfc_current_locus = old_loc;
763 va_start (argp, target);
766 for (; matches > 0; matches--)
776 /* Matches that don't have to be undone */
781 (void) va_arg (argp, void **);
786 vp = va_arg (argp, void **);
800 /*********************** Statement level matching **********************/
802 /* Matches the start of a program unit, which is the program keyword
803 followed by an obligatory symbol. */
806 gfc_match_program (void)
811 m = gfc_match ("% %s%t", &sym);
815 gfc_error ("Invalid form of PROGRAM statement at %C");
819 if (m == MATCH_ERROR)
822 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
831 /* Match a simple assignment statement. */
834 gfc_match_assignment (void)
836 gfc_expr *lvalue, *rvalue;
840 old_loc = gfc_current_locus;
843 m = gfc_match (" %v =", &lvalue);
846 gfc_current_locus = old_loc;
847 gfc_free_expr (lvalue);
851 if (lvalue->symtree->n.sym->attr.protected
852 && lvalue->symtree->n.sym->attr.use_assoc)
854 gfc_current_locus = old_loc;
855 gfc_free_expr (lvalue);
856 gfc_error ("Setting value of PROTECTED variable at %C");
861 m = gfc_match (" %e%t", &rvalue);
864 gfc_current_locus = old_loc;
865 gfc_free_expr (lvalue);
866 gfc_free_expr (rvalue);
870 gfc_set_sym_referenced (lvalue->symtree->n.sym);
872 new_st.op = EXEC_ASSIGN;
873 new_st.expr = lvalue;
874 new_st.expr2 = rvalue;
876 gfc_check_do_variable (lvalue->symtree);
882 /* Match a pointer assignment statement. */
885 gfc_match_pointer_assignment (void)
887 gfc_expr *lvalue, *rvalue;
891 old_loc = gfc_current_locus;
893 lvalue = rvalue = NULL;
895 m = gfc_match (" %v =>", &lvalue);
902 m = gfc_match (" %e%t", &rvalue);
906 if (lvalue->symtree->n.sym->attr.protected
907 && lvalue->symtree->n.sym->attr.use_assoc)
909 gfc_error ("Assigning to a PROTECTED pointer at %C");
914 new_st.op = EXEC_POINTER_ASSIGN;
915 new_st.expr = lvalue;
916 new_st.expr2 = rvalue;
921 gfc_current_locus = old_loc;
922 gfc_free_expr (lvalue);
923 gfc_free_expr (rvalue);
928 /* We try to match an easy arithmetic IF statement. This only happens
929 when just after having encountered a simple IF statement. This code
930 is really duplicate with parts of the gfc_match_if code, but this is
934 match_arithmetic_if (void)
936 gfc_st_label *l1, *l2, *l3;
940 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
944 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
945 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
946 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
948 gfc_free_expr (expr);
952 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF statement "
956 new_st.op = EXEC_ARITHMETIC_IF;
966 /* The IF statement is a bit of a pain. First of all, there are three
967 forms of it, the simple IF, the IF that starts a block and the
970 There is a problem with the simple IF and that is the fact that we
971 only have a single level of undo information on symbols. What this
972 means is for a simple IF, we must re-match the whole IF statement
973 multiple times in order to guarantee that the symbol table ends up
974 in the proper state. */
976 static match match_simple_forall (void);
977 static match match_simple_where (void);
980 gfc_match_if (gfc_statement *if_type)
983 gfc_st_label *l1, *l2, *l3;
988 n = gfc_match_label ();
989 if (n == MATCH_ERROR)
992 old_loc = gfc_current_locus;
994 m = gfc_match (" if ( %e", &expr);
998 if (gfc_match_char (')') != MATCH_YES)
1000 gfc_error ("Syntax error in IF-expression at %C");
1001 gfc_free_expr (expr);
1005 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1011 gfc_error ("Block label not appropriate for arithmetic IF "
1013 gfc_free_expr (expr);
1017 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1018 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1019 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1021 gfc_free_expr (expr);
1025 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF "
1026 "statement at %C") == FAILURE)
1029 new_st.op = EXEC_ARITHMETIC_IF;
1035 *if_type = ST_ARITHMETIC_IF;
1039 if (gfc_match (" then%t") == MATCH_YES)
1041 new_st.op = EXEC_IF;
1043 *if_type = ST_IF_BLOCK;
1049 gfc_error ("Block label is not appropriate IF statement at %C");
1050 gfc_free_expr (expr);
1054 /* At this point the only thing left is a simple IF statement. At
1055 this point, n has to be MATCH_NO, so we don't have to worry about
1056 re-matching a block label. From what we've got so far, try
1057 matching an assignment. */
1059 *if_type = ST_SIMPLE_IF;
1061 m = gfc_match_assignment ();
1065 gfc_free_expr (expr);
1066 gfc_undo_symbols ();
1067 gfc_current_locus = old_loc;
1069 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1070 assignment was found. For MATCH_NO, continue to call the various
1072 if (m == MATCH_ERROR)
1075 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1077 m = gfc_match_pointer_assignment ();
1081 gfc_free_expr (expr);
1082 gfc_undo_symbols ();
1083 gfc_current_locus = old_loc;
1085 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1087 /* Look at the next keyword to see which matcher to call. Matching
1088 the keyword doesn't affect the symbol table, so we don't have to
1089 restore between tries. */
1091 #define match(string, subr, statement) \
1092 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1096 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1097 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1098 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1099 match ("call", gfc_match_call, ST_CALL)
1100 match ("close", gfc_match_close, ST_CLOSE)
1101 match ("continue", gfc_match_continue, ST_CONTINUE)
1102 match ("cycle", gfc_match_cycle, ST_CYCLE)
1103 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1104 match ("end file", gfc_match_endfile, ST_END_FILE)
1105 match ("exit", gfc_match_exit, ST_EXIT)
1106 match ("flush", gfc_match_flush, ST_FLUSH)
1107 match ("forall", match_simple_forall, ST_FORALL)
1108 match ("go to", gfc_match_goto, ST_GOTO)
1109 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1110 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1111 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1112 match ("open", gfc_match_open, ST_OPEN)
1113 match ("pause", gfc_match_pause, ST_NONE)
1114 match ("print", gfc_match_print, ST_WRITE)
1115 match ("read", gfc_match_read, ST_READ)
1116 match ("return", gfc_match_return, ST_RETURN)
1117 match ("rewind", gfc_match_rewind, ST_REWIND)
1118 match ("stop", gfc_match_stop, ST_STOP)
1119 match ("where", match_simple_where, ST_WHERE)
1120 match ("write", gfc_match_write, ST_WRITE)
1122 /* The gfc_match_assignment() above may have returned a MATCH_NO
1123 where the assignment was to a named constant. Check that
1124 special case here. */
1125 m = gfc_match_assignment ();
1128 gfc_error ("Cannot assign to a named constant at %C");
1129 gfc_free_expr (expr);
1130 gfc_undo_symbols ();
1131 gfc_current_locus = old_loc;
1135 /* All else has failed, so give up. See if any of the matchers has
1136 stored an error message of some sort. */
1137 if (gfc_error_check () == 0)
1138 gfc_error ("Unclassifiable statement in IF-clause at %C");
1140 gfc_free_expr (expr);
1145 gfc_error ("Syntax error in IF-clause at %C");
1148 gfc_free_expr (expr);
1152 /* At this point, we've matched the single IF and the action clause
1153 is in new_st. Rearrange things so that the IF statement appears
1156 p = gfc_get_code ();
1157 p->next = gfc_get_code ();
1159 p->next->loc = gfc_current_locus;
1164 gfc_clear_new_st ();
1166 new_st.op = EXEC_IF;
1175 /* Match an ELSE statement. */
1178 gfc_match_else (void)
1180 char name[GFC_MAX_SYMBOL_LEN + 1];
1182 if (gfc_match_eos () == MATCH_YES)
1185 if (gfc_match_name (name) != MATCH_YES
1186 || gfc_current_block () == NULL
1187 || gfc_match_eos () != MATCH_YES)
1189 gfc_error ("Unexpected junk after ELSE statement at %C");
1193 if (strcmp (name, gfc_current_block ()->name) != 0)
1195 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1196 name, gfc_current_block ()->name);
1204 /* Match an ELSE IF statement. */
1207 gfc_match_elseif (void)
1209 char name[GFC_MAX_SYMBOL_LEN + 1];
1213 m = gfc_match (" ( %e ) then", &expr);
1217 if (gfc_match_eos () == MATCH_YES)
1220 if (gfc_match_name (name) != MATCH_YES
1221 || gfc_current_block () == NULL
1222 || gfc_match_eos () != MATCH_YES)
1224 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1228 if (strcmp (name, gfc_current_block ()->name) != 0)
1230 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1231 name, gfc_current_block ()->name);
1236 new_st.op = EXEC_IF;
1241 gfc_free_expr (expr);
1246 /* Free a gfc_iterator structure. */
1249 gfc_free_iterator (gfc_iterator *iter, int flag)
1255 gfc_free_expr (iter->var);
1256 gfc_free_expr (iter->start);
1257 gfc_free_expr (iter->end);
1258 gfc_free_expr (iter->step);
1265 /* Match a DO statement. */
1270 gfc_iterator iter, *ip;
1272 gfc_st_label *label;
1275 old_loc = gfc_current_locus;
1278 iter.var = iter.start = iter.end = iter.step = NULL;
1280 m = gfc_match_label ();
1281 if (m == MATCH_ERROR)
1284 if (gfc_match (" do") != MATCH_YES)
1287 m = gfc_match_st_label (&label);
1288 if (m == MATCH_ERROR)
1291 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1293 if (gfc_match_eos () == MATCH_YES)
1295 iter.end = gfc_logical_expr (1, NULL);
1296 new_st.op = EXEC_DO_WHILE;
1300 /* Match an optional comma, if no comma is found, a space is obligatory. */
1301 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1304 /* See if we have a DO WHILE. */
1305 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1307 new_st.op = EXEC_DO_WHILE;
1311 /* The abortive DO WHILE may have done something to the symbol
1312 table, so we start over. */
1313 gfc_undo_symbols ();
1314 gfc_current_locus = old_loc;
1316 gfc_match_label (); /* This won't error. */
1317 gfc_match (" do "); /* This will work. */
1319 gfc_match_st_label (&label); /* Can't error out. */
1320 gfc_match_char (','); /* Optional comma. */
1322 m = gfc_match_iterator (&iter, 0);
1325 if (m == MATCH_ERROR)
1328 gfc_check_do_variable (iter.var->symtree);
1330 if (gfc_match_eos () != MATCH_YES)
1332 gfc_syntax_error (ST_DO);
1336 new_st.op = EXEC_DO;
1340 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1343 new_st.label = label;
1345 if (new_st.op == EXEC_DO_WHILE)
1346 new_st.expr = iter.end;
1349 new_st.ext.iterator = ip = gfc_get_iterator ();
1356 gfc_free_iterator (&iter, 0);
1362 /* Match an EXIT or CYCLE statement. */
1365 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1367 gfc_state_data *p, *o;
1371 if (gfc_match_eos () == MATCH_YES)
1375 m = gfc_match ("% %s%t", &sym);
1376 if (m == MATCH_ERROR)
1380 gfc_syntax_error (st);
1384 if (sym->attr.flavor != FL_LABEL)
1386 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1387 sym->name, gfc_ascii_statement (st));
1392 /* Find the loop mentioned specified by the label (or lack of a label). */
1393 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1394 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1396 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1402 gfc_error ("%s statement at %C is not within a loop",
1403 gfc_ascii_statement (st));
1405 gfc_error ("%s statement at %C is not within loop '%s'",
1406 gfc_ascii_statement (st), sym->name);
1413 gfc_error ("%s statement at %C leaving OpenMP structured block",
1414 gfc_ascii_statement (st));
1417 else if (st == ST_EXIT
1418 && p->previous != NULL
1419 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1420 && (p->previous->head->op == EXEC_OMP_DO
1421 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1423 gcc_assert (p->previous->head->next != NULL);
1424 gcc_assert (p->previous->head->next->op == EXEC_DO
1425 || p->previous->head->next->op == EXEC_DO_WHILE);
1426 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1430 /* Save the first statement in the loop - needed by the backend. */
1431 new_st.ext.whichloop = p->head;
1439 /* Match the EXIT statement. */
1442 gfc_match_exit (void)
1444 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1448 /* Match the CYCLE statement. */
1451 gfc_match_cycle (void)
1453 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1457 /* Match a number or character constant after a STOP or PAUSE statement. */
1460 gfc_match_stopcode (gfc_statement st)
1470 if (gfc_match_eos () != MATCH_YES)
1472 m = gfc_match_small_literal_int (&stop_code, &cnt);
1473 if (m == MATCH_ERROR)
1476 if (m == MATCH_YES && cnt > 5)
1478 gfc_error ("Too many digits in STOP code at %C");
1484 /* Try a character constant. */
1485 m = gfc_match_expr (&e);
1486 if (m == MATCH_ERROR)
1490 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1494 if (gfc_match_eos () != MATCH_YES)
1498 if (gfc_pure (NULL))
1500 gfc_error ("%s statement not allowed in PURE procedure at %C",
1501 gfc_ascii_statement (st));
1505 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1507 new_st.ext.stop_code = stop_code;
1512 gfc_syntax_error (st);
1521 /* Match the (deprecated) PAUSE statement. */
1524 gfc_match_pause (void)
1528 m = gfc_match_stopcode (ST_PAUSE);
1531 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: PAUSE statement at %C")
1539 /* Match the STOP statement. */
1542 gfc_match_stop (void)
1544 return gfc_match_stopcode (ST_STOP);
1548 /* Match a CONTINUE statement. */
1551 gfc_match_continue (void)
1553 if (gfc_match_eos () != MATCH_YES)
1555 gfc_syntax_error (ST_CONTINUE);
1559 new_st.op = EXEC_CONTINUE;
1564 /* Match the (deprecated) ASSIGN statement. */
1567 gfc_match_assign (void)
1570 gfc_st_label *label;
1572 if (gfc_match (" %l", &label) == MATCH_YES)
1574 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1576 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1578 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: ASSIGN "
1583 expr->symtree->n.sym->attr.assign = 1;
1585 new_st.op = EXEC_LABEL_ASSIGN;
1586 new_st.label = label;
1595 /* Match the GO TO statement. As a computed GOTO statement is
1596 matched, it is transformed into an equivalent SELECT block. No
1597 tree is necessary, and the resulting jumps-to-jumps are
1598 specifically optimized away by the back end. */
1601 gfc_match_goto (void)
1603 gfc_code *head, *tail;
1606 gfc_st_label *label;
1610 if (gfc_match (" %l%t", &label) == MATCH_YES)
1612 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1615 new_st.op = EXEC_GOTO;
1616 new_st.label = label;
1620 /* The assigned GO TO statement. */
1622 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1624 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: Assigned GOTO "
1629 new_st.op = EXEC_GOTO;
1632 if (gfc_match_eos () == MATCH_YES)
1635 /* Match label list. */
1636 gfc_match_char (',');
1637 if (gfc_match_char ('(') != MATCH_YES)
1639 gfc_syntax_error (ST_GOTO);
1646 m = gfc_match_st_label (&label);
1650 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1654 head = tail = gfc_get_code ();
1657 tail->block = gfc_get_code ();
1661 tail->label = label;
1662 tail->op = EXEC_GOTO;
1664 while (gfc_match_char (',') == MATCH_YES);
1666 if (gfc_match (")%t") != MATCH_YES)
1671 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1674 new_st.block = head;
1679 /* Last chance is a computed GO TO statement. */
1680 if (gfc_match_char ('(') != MATCH_YES)
1682 gfc_syntax_error (ST_GOTO);
1691 m = gfc_match_st_label (&label);
1695 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1699 head = tail = gfc_get_code ();
1702 tail->block = gfc_get_code ();
1706 cp = gfc_get_case ();
1707 cp->low = cp->high = gfc_int_expr (i++);
1709 tail->op = EXEC_SELECT;
1710 tail->ext.case_list = cp;
1712 tail->next = gfc_get_code ();
1713 tail->next->op = EXEC_GOTO;
1714 tail->next->label = label;
1716 while (gfc_match_char (',') == MATCH_YES);
1718 if (gfc_match_char (')') != MATCH_YES)
1723 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1727 /* Get the rest of the statement. */
1728 gfc_match_char (',');
1730 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1733 /* At this point, a computed GOTO has been fully matched and an
1734 equivalent SELECT statement constructed. */
1736 new_st.op = EXEC_SELECT;
1739 /* Hack: For a "real" SELECT, the expression is in expr. We put
1740 it in expr2 so we can distinguish then and produce the correct
1742 new_st.expr2 = expr;
1743 new_st.block = head;
1747 gfc_syntax_error (ST_GOTO);
1749 gfc_free_statements (head);
1754 /* Frees a list of gfc_alloc structures. */
1757 gfc_free_alloc_list (gfc_alloc *p)
1764 gfc_free_expr (p->expr);
1770 /* Match an ALLOCATE statement. */
1773 gfc_match_allocate (void)
1775 gfc_alloc *head, *tail;
1782 if (gfc_match_char ('(') != MATCH_YES)
1788 head = tail = gfc_get_alloc ();
1791 tail->next = gfc_get_alloc ();
1795 m = gfc_match_variable (&tail->expr, 0);
1798 if (m == MATCH_ERROR)
1801 if (gfc_check_do_variable (tail->expr->symtree))
1805 && gfc_impure_variable (tail->expr->symtree->n.sym))
1807 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1812 if (tail->expr->ts.type == BT_DERIVED)
1813 tail->expr->ts.derived = gfc_use_derived (tail->expr->ts.derived);
1815 if (gfc_match_char (',') != MATCH_YES)
1818 m = gfc_match (" stat = %v", &stat);
1819 if (m == MATCH_ERROR)
1827 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1829 gfc_error ("STAT variable '%s' of ALLOCATE statement at %C cannot "
1830 "be INTENT(IN)", stat->symtree->n.sym->name);
1834 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1836 gfc_error ("Illegal STAT variable in ALLOCATE statement at %C "
1837 "for a PURE procedure");
1841 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1843 gfc_error ("STAT expression at %C must be a variable");
1847 gfc_check_do_variable(stat->symtree);
1850 if (gfc_match (" )%t") != MATCH_YES)
1853 new_st.op = EXEC_ALLOCATE;
1855 new_st.ext.alloc_list = head;
1860 gfc_syntax_error (ST_ALLOCATE);
1863 gfc_free_expr (stat);
1864 gfc_free_alloc_list (head);
1869 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1870 a set of pointer assignments to intrinsic NULL(). */
1873 gfc_match_nullify (void)
1881 if (gfc_match_char ('(') != MATCH_YES)
1886 m = gfc_match_variable (&p, 0);
1887 if (m == MATCH_ERROR)
1892 if (gfc_check_do_variable (p->symtree))
1895 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1897 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
1901 /* build ' => NULL() '. */
1902 e = gfc_get_expr ();
1903 e->where = gfc_current_locus;
1904 e->expr_type = EXPR_NULL;
1905 e->ts.type = BT_UNKNOWN;
1907 /* Chain to list. */
1912 tail->next = gfc_get_code ();
1916 tail->op = EXEC_POINTER_ASSIGN;
1920 if (gfc_match (" )%t") == MATCH_YES)
1922 if (gfc_match_char (',') != MATCH_YES)
1929 gfc_syntax_error (ST_NULLIFY);
1932 gfc_free_statements (new_st.next);
1937 /* Match a DEALLOCATE statement. */
1940 gfc_match_deallocate (void)
1942 gfc_alloc *head, *tail;
1949 if (gfc_match_char ('(') != MATCH_YES)
1955 head = tail = gfc_get_alloc ();
1958 tail->next = gfc_get_alloc ();
1962 m = gfc_match_variable (&tail->expr, 0);
1963 if (m == MATCH_ERROR)
1968 if (gfc_check_do_variable (tail->expr->symtree))
1972 && gfc_impure_variable (tail->expr->symtree->n.sym))
1974 gfc_error ("Illegal deallocate-expression in DEALLOCATE at %C "
1975 "for a PURE procedure");
1979 if (gfc_match_char (',') != MATCH_YES)
1982 m = gfc_match (" stat = %v", &stat);
1983 if (m == MATCH_ERROR)
1991 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1993 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1994 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
1998 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
2000 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
2001 "for a PURE procedure");
2005 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
2007 gfc_error ("STAT expression at %C must be a variable");
2011 gfc_check_do_variable(stat->symtree);
2014 if (gfc_match (" )%t") != MATCH_YES)
2017 new_st.op = EXEC_DEALLOCATE;
2019 new_st.ext.alloc_list = head;
2024 gfc_syntax_error (ST_DEALLOCATE);
2027 gfc_free_expr (stat);
2028 gfc_free_alloc_list (head);
2033 /* Match a RETURN statement. */
2036 gfc_match_return (void)
2040 gfc_compile_state s;
2044 if (gfc_match_eos () == MATCH_YES)
2047 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2049 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2054 if (gfc_current_form == FORM_FREE)
2056 /* The following are valid, so we can't require a blank after the
2060 c = gfc_peek_char ();
2061 if (ISALPHA (c) || ISDIGIT (c))
2065 m = gfc_match (" %e%t", &e);
2068 if (m == MATCH_ERROR)
2071 gfc_syntax_error (ST_RETURN);
2078 gfc_enclosing_unit (&s);
2079 if (s == COMP_PROGRAM
2080 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2081 "main program at %C") == FAILURE)
2084 new_st.op = EXEC_RETURN;
2091 /* Match a CALL statement. The tricky part here are possible
2092 alternate return specifiers. We handle these by having all
2093 "subroutines" actually return an integer via a register that gives
2094 the return number. If the call specifies alternate returns, we
2095 generate code for a SELECT statement whose case clauses contain
2096 GOTOs to the various labels. */
2099 gfc_match_call (void)
2101 char name[GFC_MAX_SYMBOL_LEN + 1];
2102 gfc_actual_arglist *a, *arglist;
2112 m = gfc_match ("% %n", name);
2118 if (gfc_get_ha_sym_tree (name, &st))
2122 gfc_set_sym_referenced (sym);
2124 if (!sym->attr.generic
2125 && !sym->attr.subroutine
2126 && gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2129 if (gfc_match_eos () != MATCH_YES)
2131 m = gfc_match_actual_arglist (1, &arglist);
2134 if (m == MATCH_ERROR)
2137 if (gfc_match_eos () != MATCH_YES)
2141 /* If any alternate return labels were found, construct a SELECT
2142 statement that will jump to the right place. */
2145 for (a = arglist; a; a = a->next)
2146 if (a->expr == NULL)
2151 gfc_symtree *select_st;
2152 gfc_symbol *select_sym;
2153 char name[GFC_MAX_SYMBOL_LEN + 1];
2155 new_st.next = c = gfc_get_code ();
2156 c->op = EXEC_SELECT;
2157 sprintf (name, "_result_%s", sym->name);
2158 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
2160 select_sym = select_st->n.sym;
2161 select_sym->ts.type = BT_INTEGER;
2162 select_sym->ts.kind = gfc_default_integer_kind;
2163 gfc_set_sym_referenced (select_sym);
2164 c->expr = gfc_get_expr ();
2165 c->expr->expr_type = EXPR_VARIABLE;
2166 c->expr->symtree = select_st;
2167 c->expr->ts = select_sym->ts;
2168 c->expr->where = gfc_current_locus;
2171 for (a = arglist; a; a = a->next)
2173 if (a->expr != NULL)
2176 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2181 c->block = gfc_get_code ();
2183 c->op = EXEC_SELECT;
2185 new_case = gfc_get_case ();
2186 new_case->high = new_case->low = gfc_int_expr (i);
2187 c->ext.case_list = new_case;
2189 c->next = gfc_get_code ();
2190 c->next->op = EXEC_GOTO;
2191 c->next->label = a->label;
2195 new_st.op = EXEC_CALL;
2196 new_st.symtree = st;
2197 new_st.ext.actual = arglist;
2202 gfc_syntax_error (ST_CALL);
2205 gfc_free_actual_arglist (arglist);
2210 /* Given a name, return a pointer to the common head structure,
2211 creating it if it does not exist. If FROM_MODULE is nonzero, we
2212 mangle the name so that it doesn't interfere with commons defined
2213 in the using namespace.
2214 TODO: Add to global symbol tree. */
2217 gfc_get_common (const char *name, int from_module)
2220 static int serial = 0;
2221 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
2225 /* A use associated common block is only needed to correctly layout
2226 the variables it contains. */
2227 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2228 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2232 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2235 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2238 if (st->n.common == NULL)
2240 st->n.common = gfc_get_common_head ();
2241 st->n.common->where = gfc_current_locus;
2242 strcpy (st->n.common->name, name);
2245 return st->n.common;
2249 /* Match a common block name. */
2252 match_common_name (char *name)
2256 if (gfc_match_char ('/') == MATCH_NO)
2262 if (gfc_match_char ('/') == MATCH_YES)
2268 m = gfc_match_name (name);
2270 if (m == MATCH_ERROR)
2272 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2275 gfc_error ("Syntax error in common block name at %C");
2280 /* Match a COMMON statement. */
2283 gfc_match_common (void)
2285 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2286 char name[GFC_MAX_SYMBOL_LEN + 1];
2293 old_blank_common = gfc_current_ns->blank_common.head;
2294 if (old_blank_common)
2296 while (old_blank_common->common_next)
2297 old_blank_common = old_blank_common->common_next;
2304 m = match_common_name (name);
2305 if (m == MATCH_ERROR)
2308 gsym = gfc_get_gsymbol (name);
2309 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2311 gfc_error ("Symbol '%s' at %C is already an external symbol that "
2312 "is not COMMON", name);
2316 if (gsym->type == GSYM_UNKNOWN)
2318 gsym->type = GSYM_COMMON;
2319 gsym->where = gfc_current_locus;
2325 if (name[0] == '\0')
2327 if (gfc_current_ns->is_block_data)
2329 gfc_warning ("BLOCK DATA unit cannot contain blank COMMON "
2332 t = &gfc_current_ns->blank_common;
2333 if (t->head == NULL)
2334 t->where = gfc_current_locus;
2338 t = gfc_get_common (name, 0);
2347 while (tail->common_next)
2348 tail = tail->common_next;
2351 /* Grab the list of symbols. */
2354 m = gfc_match_symbol (&sym, 0);
2355 if (m == MATCH_ERROR)
2360 if (sym->attr.in_common)
2362 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2367 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2370 if (sym->value != NULL
2371 && (name[0] == '\0' || !sym->attr.data))
2373 if (name[0] == '\0')
2374 gfc_error ("Previously initialized symbol '%s' in "
2375 "blank COMMON block at %C", sym->name);
2377 gfc_error ("Previously initialized symbol '%s' in "
2378 "COMMON block '%s' at %C", sym->name, name);
2382 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2385 /* Derived type names must have the SEQUENCE attribute. */
2386 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2388 gfc_error ("Derived type variable in COMMON at %C does not "
2389 "have the SEQUENCE attribute");
2394 tail->common_next = sym;
2400 /* Deal with an optional array specification after the
2402 m = gfc_match_array_spec (&as);
2403 if (m == MATCH_ERROR)
2408 if (as->type != AS_EXPLICIT)
2410 gfc_error ("Array specification for symbol '%s' in COMMON "
2411 "at %C must be explicit", sym->name);
2415 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2418 if (sym->attr.pointer)
2420 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
2421 "POINTER array", sym->name);
2430 sym->common_head = t;
2432 /* Check to see if the symbol is already in an equivalence group.
2433 If it is, set the other members as being in common. */
2434 if (sym->attr.in_equivalence)
2436 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2438 for (e2 = e1; e2; e2 = e2->eq)
2439 if (e2->expr->symtree->n.sym == sym)
2446 for (e2 = e1; e2; e2 = e2->eq)
2448 other = e2->expr->symtree->n.sym;
2449 if (other->common_head
2450 && other->common_head != sym->common_head)
2452 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2453 "%C is being indirectly equivalenced to "
2454 "another COMMON block '%s'",
2455 sym->name, sym->common_head->name,
2456 other->common_head->name);
2459 other->attr.in_common = 1;
2460 other->common_head = t;
2466 gfc_gobble_whitespace ();
2467 if (gfc_match_eos () == MATCH_YES)
2469 if (gfc_peek_char () == '/')
2471 if (gfc_match_char (',') != MATCH_YES)
2473 gfc_gobble_whitespace ();
2474 if (gfc_peek_char () == '/')
2483 gfc_syntax_error (ST_COMMON);
2486 if (old_blank_common)
2487 old_blank_common->common_next = NULL;
2489 gfc_current_ns->blank_common.head = NULL;
2490 gfc_free_array_spec (as);
2495 /* Match a BLOCK DATA program unit. */
2498 gfc_match_block_data (void)
2500 char name[GFC_MAX_SYMBOL_LEN + 1];
2504 if (gfc_match_eos () == MATCH_YES)
2506 gfc_new_block = NULL;
2510 m = gfc_match ("% %n%t", name);
2514 if (gfc_get_symbol (name, NULL, &sym))
2517 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2520 gfc_new_block = sym;
2526 /* Free a namelist structure. */
2529 gfc_free_namelist (gfc_namelist *name)
2533 for (; name; name = n)
2541 /* Match a NAMELIST statement. */
2544 gfc_match_namelist (void)
2546 gfc_symbol *group_name, *sym;
2550 m = gfc_match (" / %s /", &group_name);
2553 if (m == MATCH_ERROR)
2558 if (group_name->ts.type != BT_UNKNOWN)
2560 gfc_error ("Namelist group name '%s' at %C already has a basic "
2561 "type of %s", group_name->name,
2562 gfc_typename (&group_name->ts));
2566 if (group_name->attr.flavor == FL_NAMELIST
2567 && group_name->attr.use_assoc
2568 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2569 "at %C already is USE associated and can"
2570 "not be respecified.", group_name->name)
2574 if (group_name->attr.flavor != FL_NAMELIST
2575 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2576 group_name->name, NULL) == FAILURE)
2581 m = gfc_match_symbol (&sym, 1);
2584 if (m == MATCH_ERROR)
2587 if (sym->attr.in_namelist == 0
2588 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2591 /* Use gfc_error_check here, rather than goto error, so that
2592 these are the only errors for the next two lines. */
2593 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2595 gfc_error ("Assumed size array '%s' in namelist '%s' at "
2596 "%C is not allowed", sym->name, group_name->name);
2600 if (sym->ts.type == BT_CHARACTER && sym->ts.cl->length == NULL)
2602 gfc_error ("Assumed character length '%s' in namelist '%s' at "
2603 "%C is not allowed", sym->name, group_name->name);
2607 if (sym->as && sym->as->type == AS_ASSUMED_SHAPE
2608 && gfc_notify_std (GFC_STD_GNU, "Assumed shape array '%s' in "
2609 "namelist '%s' at %C is an extension.",
2610 sym->name, group_name->name) == FAILURE)
2613 nl = gfc_get_namelist ();
2617 if (group_name->namelist == NULL)
2618 group_name->namelist = group_name->namelist_tail = nl;
2621 group_name->namelist_tail->next = nl;
2622 group_name->namelist_tail = nl;
2625 if (gfc_match_eos () == MATCH_YES)
2628 m = gfc_match_char (',');
2630 if (gfc_match_char ('/') == MATCH_YES)
2632 m2 = gfc_match (" %s /", &group_name);
2633 if (m2 == MATCH_YES)
2635 if (m2 == MATCH_ERROR)
2649 gfc_syntax_error (ST_NAMELIST);
2656 /* Match a MODULE statement. */
2659 gfc_match_module (void)
2663 m = gfc_match (" %s%t", &gfc_new_block);
2667 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2668 gfc_new_block->name, NULL) == FAILURE)
2675 /* Free equivalence sets and lists. Recursively is the easiest way to
2679 gfc_free_equiv (gfc_equiv *eq)
2684 gfc_free_equiv (eq->eq);
2685 gfc_free_equiv (eq->next);
2686 gfc_free_expr (eq->expr);
2691 /* Match an EQUIVALENCE statement. */
2694 gfc_match_equivalence (void)
2696 gfc_equiv *eq, *set, *tail;
2700 gfc_common_head *common_head = NULL;
2708 eq = gfc_get_equiv ();
2712 eq->next = gfc_current_ns->equiv;
2713 gfc_current_ns->equiv = eq;
2715 if (gfc_match_char ('(') != MATCH_YES)
2719 common_flag = FALSE;
2724 m = gfc_match_equiv_variable (&set->expr);
2725 if (m == MATCH_ERROR)
2730 /* count the number of objects. */
2733 if (gfc_match_char ('%') == MATCH_YES)
2735 gfc_error ("Derived type component %C is not a "
2736 "permitted EQUIVALENCE member");
2740 for (ref = set->expr->ref; ref; ref = ref->next)
2741 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2743 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
2744 "be an array section");
2748 sym = set->expr->symtree->n.sym;
2750 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
2753 if (sym->attr.in_common)
2756 common_head = sym->common_head;
2759 if (gfc_match_char (')') == MATCH_YES)
2762 if (gfc_match_char (',') != MATCH_YES)
2765 set->eq = gfc_get_equiv ();
2771 gfc_error ("EQUIVALENCE at %C requires two or more objects");
2775 /* If one of the members of an equivalence is in common, then
2776 mark them all as being in common. Before doing this, check
2777 that members of the equivalence group are not in different
2780 for (set = eq; set; set = set->eq)
2782 sym = set->expr->symtree->n.sym;
2783 if (sym->common_head && sym->common_head != common_head)
2785 gfc_error ("Attempt to indirectly overlap COMMON "
2786 "blocks %s and %s by EQUIVALENCE at %C",
2787 sym->common_head->name, common_head->name);
2790 sym->attr.in_common = 1;
2791 sym->common_head = common_head;
2794 if (gfc_match_eos () == MATCH_YES)
2796 if (gfc_match_char (',') != MATCH_YES)
2803 gfc_syntax_error (ST_EQUIVALENCE);
2809 gfc_free_equiv (gfc_current_ns->equiv);
2810 gfc_current_ns->equiv = eq;
2816 /* Check that a statement function is not recursive. This is done by looking
2817 for the statement function symbol(sym) by looking recursively through its
2818 expression(e). If a reference to sym is found, true is returned.
2819 12.5.4 requires that any variable of function that is implicitly typed
2820 shall have that type confirmed by any subsequent type declaration. The
2821 implicit typing is conveniently done here. */
2824 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
2826 gfc_actual_arglist *arg;
2833 switch (e->expr_type)
2836 for (arg = e->value.function.actual; arg; arg = arg->next)
2838 if (sym->name == arg->name || recursive_stmt_fcn (arg->expr, sym))
2842 if (e->symtree == NULL)
2845 /* Check the name before testing for nested recursion! */
2846 if (sym->name == e->symtree->n.sym->name)
2849 /* Catch recursion via other statement functions. */
2850 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
2851 && e->symtree->n.sym->value
2852 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
2855 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
2856 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
2861 if (e->symtree && sym->name == e->symtree->n.sym->name)
2864 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
2865 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
2869 if (recursive_stmt_fcn (e->value.op.op1, sym)
2870 || recursive_stmt_fcn (e->value.op.op2, sym))
2878 /* Component references do not need to be checked. */
2881 for (ref = e->ref; ref; ref = ref->next)
2886 for (i = 0; i < ref->u.ar.dimen; i++)
2888 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
2889 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
2890 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
2896 if (recursive_stmt_fcn (ref->u.ss.start, sym)
2897 || recursive_stmt_fcn (ref->u.ss.end, sym))
2911 /* Match a statement function declaration. It is so easy to match
2912 non-statement function statements with a MATCH_ERROR as opposed to
2913 MATCH_NO that we suppress error message in most cases. */
2916 gfc_match_st_function (void)
2918 gfc_error_buf old_error;
2923 m = gfc_match_symbol (&sym, 0);
2927 gfc_push_error (&old_error);
2929 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
2930 sym->name, NULL) == FAILURE)
2933 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2936 m = gfc_match (" = %e%t", &expr);
2940 gfc_free_error (&old_error);
2941 if (m == MATCH_ERROR)
2944 if (recursive_stmt_fcn (expr, sym))
2946 gfc_error ("Statement function at %L is recursive", &expr->where);
2955 gfc_pop_error (&old_error);
2960 /***************** SELECT CASE subroutines ******************/
2962 /* Free a single case structure. */
2965 free_case (gfc_case *p)
2967 if (p->low == p->high)
2969 gfc_free_expr (p->low);
2970 gfc_free_expr (p->high);
2975 /* Free a list of case structures. */
2978 gfc_free_case_list (gfc_case *p)
2990 /* Match a single case selector. */
2993 match_case_selector (gfc_case **cp)
2998 c = gfc_get_case ();
2999 c->where = gfc_current_locus;
3001 if (gfc_match_char (':') == MATCH_YES)
3003 m = gfc_match_init_expr (&c->high);
3006 if (m == MATCH_ERROR)
3011 m = gfc_match_init_expr (&c->low);
3012 if (m == MATCH_ERROR)
3017 /* If we're not looking at a ':' now, make a range out of a single
3018 target. Else get the upper bound for the case range. */
3019 if (gfc_match_char (':') != MATCH_YES)
3023 m = gfc_match_init_expr (&c->high);
3024 if (m == MATCH_ERROR)
3026 /* MATCH_NO is fine. It's OK if nothing is there! */
3034 gfc_error ("Expected initialization expression in CASE at %C");
3042 /* Match the end of a case statement. */
3045 match_case_eos (void)
3047 char name[GFC_MAX_SYMBOL_LEN + 1];
3050 if (gfc_match_eos () == MATCH_YES)
3053 /* If the case construct doesn't have a case-construct-name, we
3054 should have matched the EOS. */
3055 if (!gfc_current_block ())
3057 gfc_error ("Expected the name of the SELECT CASE construct at %C");
3061 gfc_gobble_whitespace ();
3063 m = gfc_match_name (name);
3067 if (strcmp (name, gfc_current_block ()->name) != 0)
3069 gfc_error ("Expected case name of '%s' at %C",
3070 gfc_current_block ()->name);
3074 return gfc_match_eos ();
3078 /* Match a SELECT statement. */
3081 gfc_match_select (void)
3086 m = gfc_match_label ();
3087 if (m == MATCH_ERROR)
3090 m = gfc_match (" select case ( %e )%t", &expr);
3094 new_st.op = EXEC_SELECT;
3101 /* Match a CASE statement. */
3104 gfc_match_case (void)
3106 gfc_case *c, *head, *tail;
3111 if (gfc_current_state () != COMP_SELECT)
3113 gfc_error ("Unexpected CASE statement at %C");
3117 if (gfc_match ("% default") == MATCH_YES)
3119 m = match_case_eos ();
3122 if (m == MATCH_ERROR)
3125 new_st.op = EXEC_SELECT;
3126 c = gfc_get_case ();
3127 c->where = gfc_current_locus;
3128 new_st.ext.case_list = c;
3132 if (gfc_match_char ('(') != MATCH_YES)
3137 if (match_case_selector (&c) == MATCH_ERROR)
3147 if (gfc_match_char (')') == MATCH_YES)
3149 if (gfc_match_char (',') != MATCH_YES)
3153 m = match_case_eos ();
3156 if (m == MATCH_ERROR)
3159 new_st.op = EXEC_SELECT;
3160 new_st.ext.case_list = head;
3165 gfc_error ("Syntax error in CASE-specification at %C");
3168 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3172 /********************* WHERE subroutines ********************/
3174 /* Match the rest of a simple WHERE statement that follows an IF statement.
3178 match_simple_where (void)
3184 m = gfc_match (" ( %e )", &expr);
3188 m = gfc_match_assignment ();
3191 if (m == MATCH_ERROR)
3194 if (gfc_match_eos () != MATCH_YES)
3197 c = gfc_get_code ();
3201 c->next = gfc_get_code ();
3204 gfc_clear_new_st ();
3206 new_st.op = EXEC_WHERE;
3212 gfc_syntax_error (ST_WHERE);
3215 gfc_free_expr (expr);
3220 /* Match a WHERE statement. */
3223 gfc_match_where (gfc_statement *st)
3229 m0 = gfc_match_label ();
3230 if (m0 == MATCH_ERROR)
3233 m = gfc_match (" where ( %e )", &expr);
3237 if (gfc_match_eos () == MATCH_YES)
3239 *st = ST_WHERE_BLOCK;
3240 new_st.op = EXEC_WHERE;
3245 m = gfc_match_assignment ();
3247 gfc_syntax_error (ST_WHERE);
3251 gfc_free_expr (expr);
3255 /* We've got a simple WHERE statement. */
3257 c = gfc_get_code ();
3261 c->next = gfc_get_code ();
3264 gfc_clear_new_st ();
3266 new_st.op = EXEC_WHERE;
3273 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3274 new_st if successful. */
3277 gfc_match_elsewhere (void)
3279 char name[GFC_MAX_SYMBOL_LEN + 1];
3283 if (gfc_current_state () != COMP_WHERE)
3285 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3291 if (gfc_match_char ('(') == MATCH_YES)
3293 m = gfc_match_expr (&expr);
3296 if (m == MATCH_ERROR)
3299 if (gfc_match_char (')') != MATCH_YES)
3303 if (gfc_match_eos () != MATCH_YES)
3305 /* Only makes sense if we have a where-construct-name. */
3306 if (!gfc_current_block ())
3311 /* Better be a name at this point. */
3312 m = gfc_match_name (name);
3315 if (m == MATCH_ERROR)
3318 if (gfc_match_eos () != MATCH_YES)
3321 if (strcmp (name, gfc_current_block ()->name) != 0)
3323 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3324 name, gfc_current_block ()->name);
3329 new_st.op = EXEC_WHERE;
3334 gfc_syntax_error (ST_ELSEWHERE);
3337 gfc_free_expr (expr);
3342 /******************** FORALL subroutines ********************/
3344 /* Free a list of FORALL iterators. */
3347 gfc_free_forall_iterator (gfc_forall_iterator *iter)
3349 gfc_forall_iterator *next;
3354 gfc_free_expr (iter->var);
3355 gfc_free_expr (iter->start);
3356 gfc_free_expr (iter->end);
3357 gfc_free_expr (iter->stride);
3364 /* Match an iterator as part of a FORALL statement. The format is:
3366 <var> = <start>:<end>[:<stride>]
3368 On MATCH_NO, the caller tests for the possibility that there is a
3369 scalar mask expression. */
3372 match_forall_iterator (gfc_forall_iterator **result)
3374 gfc_forall_iterator *iter;
3378 where = gfc_current_locus;
3379 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3381 m = gfc_match_expr (&iter->var);
3385 if (gfc_match_char ('=') != MATCH_YES
3386 || iter->var->expr_type != EXPR_VARIABLE)
3392 m = gfc_match_expr (&iter->start);
3396 if (gfc_match_char (':') != MATCH_YES)
3399 m = gfc_match_expr (&iter->end);
3402 if (m == MATCH_ERROR)
3405 if (gfc_match_char (':') == MATCH_NO)
3406 iter->stride = gfc_int_expr (1);
3409 m = gfc_match_expr (&iter->stride);
3412 if (m == MATCH_ERROR)
3416 /* Mark the iteration variable's symbol as used as a FORALL index. */
3417 iter->var->symtree->n.sym->forall_index = true;
3423 gfc_error ("Syntax error in FORALL iterator at %C");
3428 gfc_current_locus = where;
3429 gfc_free_forall_iterator (iter);
3434 /* Match the header of a FORALL statement. */
3437 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
3439 gfc_forall_iterator *head, *tail, *new;
3443 gfc_gobble_whitespace ();
3448 if (gfc_match_char ('(') != MATCH_YES)
3451 m = match_forall_iterator (&new);
3452 if (m == MATCH_ERROR)
3461 if (gfc_match_char (',') != MATCH_YES)
3464 m = match_forall_iterator (&new);
3465 if (m == MATCH_ERROR)
3475 /* Have to have a mask expression. */
3477 m = gfc_match_expr (&msk);
3480 if (m == MATCH_ERROR)
3486 if (gfc_match_char (')') == MATCH_NO)
3494 gfc_syntax_error (ST_FORALL);
3497 gfc_free_expr (msk);
3498 gfc_free_forall_iterator (head);
3503 /* Match the rest of a simple FORALL statement that follows an
3507 match_simple_forall (void)
3509 gfc_forall_iterator *head;
3518 m = match_forall_header (&head, &mask);
3525 m = gfc_match_assignment ();
3527 if (m == MATCH_ERROR)
3531 m = gfc_match_pointer_assignment ();
3532 if (m == MATCH_ERROR)
3538 c = gfc_get_code ();
3540 c->loc = gfc_current_locus;
3542 if (gfc_match_eos () != MATCH_YES)
3545 gfc_clear_new_st ();
3546 new_st.op = EXEC_FORALL;
3548 new_st.ext.forall_iterator = head;
3549 new_st.block = gfc_get_code ();
3551 new_st.block->op = EXEC_FORALL;
3552 new_st.block->next = c;
3557 gfc_syntax_error (ST_FORALL);
3560 gfc_free_forall_iterator (head);
3561 gfc_free_expr (mask);
3567 /* Match a FORALL statement. */
3570 gfc_match_forall (gfc_statement *st)
3572 gfc_forall_iterator *head;
3581 m0 = gfc_match_label ();
3582 if (m0 == MATCH_ERROR)
3585 m = gfc_match (" forall");
3589 m = match_forall_header (&head, &mask);
3590 if (m == MATCH_ERROR)
3595 if (gfc_match_eos () == MATCH_YES)
3597 *st = ST_FORALL_BLOCK;
3598 new_st.op = EXEC_FORALL;
3600 new_st.ext.forall_iterator = head;
3604 m = gfc_match_assignment ();
3605 if (m == MATCH_ERROR)
3609 m = gfc_match_pointer_assignment ();
3610 if (m == MATCH_ERROR)
3616 c = gfc_get_code ();
3618 c->loc = gfc_current_locus;
3620 gfc_clear_new_st ();
3621 new_st.op = EXEC_FORALL;
3623 new_st.ext.forall_iterator = head;
3624 new_st.block = gfc_get_code ();
3625 new_st.block->op = EXEC_FORALL;
3626 new_st.block->next = c;
3632 gfc_syntax_error (ST_FORALL);
3635 gfc_free_forall_iterator (head);
3636 gfc_free_expr (mask);
3637 gfc_free_statements (c);