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 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
542 if (m == MATCH_ERROR)
545 if (gfc_match_char (',') != MATCH_YES)
548 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
551 if (m == MATCH_ERROR)
554 if (gfc_match_char (',') != MATCH_YES)
556 e3 = gfc_int_expr (1);
560 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
561 if (m == MATCH_ERROR)
565 gfc_error ("Expected a step value in iterator at %C");
577 gfc_error ("Syntax error in iterator at %C");
588 /* Tries to match the next non-whitespace character on the input.
589 This subroutine does not return MATCH_ERROR. */
592 gfc_match_char (char c)
596 where = gfc_current_locus;
597 gfc_gobble_whitespace ();
599 if (gfc_next_char () == c)
602 gfc_current_locus = where;
607 /* General purpose matching subroutine. The target string is a
608 scanf-like format string in which spaces correspond to arbitrary
609 whitespace (including no whitespace), characters correspond to
610 themselves. The %-codes are:
612 %% Literal percent sign
613 %e Expression, pointer to a pointer is set
614 %s Symbol, pointer to the symbol is set
615 %n Name, character buffer is set to name
616 %t Matches end of statement.
617 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
618 %l Matches a statement label
619 %v Matches a variable expression (an lvalue)
620 % Matches a required space (in free form) and optional spaces. */
623 gfc_match (const char *target, ...)
625 gfc_st_label **label;
634 old_loc = gfc_current_locus;
635 va_start (argp, target);
645 gfc_gobble_whitespace ();
656 vp = va_arg (argp, void **);
657 n = gfc_match_expr ((gfc_expr **) vp);
668 vp = va_arg (argp, void **);
669 n = gfc_match_variable ((gfc_expr **) vp, 0);
680 vp = va_arg (argp, void **);
681 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
692 np = va_arg (argp, char *);
693 n = gfc_match_name (np);
704 label = va_arg (argp, gfc_st_label **);
705 n = gfc_match_st_label (label);
716 ip = va_arg (argp, int *);
717 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
728 if (gfc_match_eos () != MATCH_YES)
736 if (gfc_match_space () == MATCH_YES)
742 break; /* Fall through to character matcher */
745 gfc_internal_error ("gfc_match(): Bad match code %c", c);
749 if (c == gfc_next_char ())
759 /* Clean up after a failed match. */
760 gfc_current_locus = old_loc;
761 va_start (argp, target);
764 for (; matches > 0; matches--)
774 /* Matches that don't have to be undone */
779 (void) va_arg (argp, void **);
784 vp = va_arg (argp, void **);
798 /*********************** Statement level matching **********************/
800 /* Matches the start of a program unit, which is the program keyword
801 followed by an obligatory symbol. */
804 gfc_match_program (void)
809 m = gfc_match ("% %s%t", &sym);
813 gfc_error ("Invalid form of PROGRAM statement at %C");
817 if (m == MATCH_ERROR)
820 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
829 /* Match a simple assignment statement. */
832 gfc_match_assignment (void)
834 gfc_expr *lvalue, *rvalue;
838 old_loc = gfc_current_locus;
841 m = gfc_match (" %v =", &lvalue);
844 gfc_current_locus = old_loc;
845 gfc_free_expr (lvalue);
849 if (lvalue->symtree->n.sym->attr.protected
850 && lvalue->symtree->n.sym->attr.use_assoc)
852 gfc_current_locus = old_loc;
853 gfc_free_expr (lvalue);
854 gfc_error ("Setting value of PROTECTED variable at %C");
859 m = gfc_match (" %e%t", &rvalue);
862 gfc_current_locus = old_loc;
863 gfc_free_expr (lvalue);
864 gfc_free_expr (rvalue);
868 gfc_set_sym_referenced (lvalue->symtree->n.sym);
870 new_st.op = EXEC_ASSIGN;
871 new_st.expr = lvalue;
872 new_st.expr2 = rvalue;
874 gfc_check_do_variable (lvalue->symtree);
880 /* Match a pointer assignment statement. */
883 gfc_match_pointer_assignment (void)
885 gfc_expr *lvalue, *rvalue;
889 old_loc = gfc_current_locus;
891 lvalue = rvalue = NULL;
893 m = gfc_match (" %v =>", &lvalue);
900 m = gfc_match (" %e%t", &rvalue);
904 if (lvalue->symtree->n.sym->attr.protected
905 && lvalue->symtree->n.sym->attr.use_assoc)
907 gfc_error ("Assigning to a PROTECTED pointer at %C");
913 new_st.op = EXEC_POINTER_ASSIGN;
914 new_st.expr = lvalue;
915 new_st.expr2 = rvalue;
920 gfc_current_locus = old_loc;
921 gfc_free_expr (lvalue);
922 gfc_free_expr (rvalue);
927 /* We try to match an easy arithmetic IF statement. This only happens
928 when just after having encountered a simple IF statement. This code
929 is really duplicate with parts of the gfc_match_if code, but this is
933 match_arithmetic_if (void)
935 gfc_st_label *l1, *l2, *l3;
939 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
943 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
944 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
945 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
947 gfc_free_expr (expr);
951 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF statement "
955 new_st.op = EXEC_ARITHMETIC_IF;
965 /* The IF statement is a bit of a pain. First of all, there are three
966 forms of it, the simple IF, the IF that starts a block and the
969 There is a problem with the simple IF and that is the fact that we
970 only have a single level of undo information on symbols. What this
971 means is for a simple IF, we must re-match the whole IF statement
972 multiple times in order to guarantee that the symbol table ends up
973 in the proper state. */
975 static match match_simple_forall (void);
976 static match match_simple_where (void);
979 gfc_match_if (gfc_statement *if_type)
982 gfc_st_label *l1, *l2, *l3;
987 n = gfc_match_label ();
988 if (n == MATCH_ERROR)
991 old_loc = gfc_current_locus;
993 m = gfc_match (" if ( %e", &expr);
997 if (gfc_match_char (')') != MATCH_YES)
999 gfc_error ("Syntax error in IF-expression at %C");
1000 gfc_free_expr (expr);
1004 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1010 gfc_error ("Block label not appropriate for arithmetic IF "
1012 gfc_free_expr (expr);
1016 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1017 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1018 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1020 gfc_free_expr (expr);
1024 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF "
1025 "statement at %C") == FAILURE)
1028 new_st.op = EXEC_ARITHMETIC_IF;
1034 *if_type = ST_ARITHMETIC_IF;
1038 if (gfc_match (" then%t") == MATCH_YES)
1040 new_st.op = EXEC_IF;
1042 *if_type = ST_IF_BLOCK;
1048 gfc_error ("Block label is not appropriate IF statement at %C");
1049 gfc_free_expr (expr);
1053 /* At this point the only thing left is a simple IF statement. At
1054 this point, n has to be MATCH_NO, so we don't have to worry about
1055 re-matching a block label. From what we've got so far, try
1056 matching an assignment. */
1058 *if_type = ST_SIMPLE_IF;
1060 m = gfc_match_assignment ();
1064 gfc_free_expr (expr);
1065 gfc_undo_symbols ();
1066 gfc_current_locus = old_loc;
1068 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1069 assignment was found. For MATCH_NO, continue to call the various
1071 if (m == MATCH_ERROR)
1074 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1076 m = gfc_match_pointer_assignment ();
1080 gfc_free_expr (expr);
1081 gfc_undo_symbols ();
1082 gfc_current_locus = old_loc;
1084 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1086 /* Look at the next keyword to see which matcher to call. Matching
1087 the keyword doesn't affect the symbol table, so we don't have to
1088 restore between tries. */
1090 #define match(string, subr, statement) \
1091 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1095 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1096 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1097 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1098 match ("call", gfc_match_call, ST_CALL)
1099 match ("close", gfc_match_close, ST_CLOSE)
1100 match ("continue", gfc_match_continue, ST_CONTINUE)
1101 match ("cycle", gfc_match_cycle, ST_CYCLE)
1102 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1103 match ("end file", gfc_match_endfile, ST_END_FILE)
1104 match ("exit", gfc_match_exit, ST_EXIT)
1105 match ("flush", gfc_match_flush, ST_FLUSH)
1106 match ("forall", match_simple_forall, ST_FORALL)
1107 match ("go to", gfc_match_goto, ST_GOTO)
1108 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1109 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1110 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1111 match ("open", gfc_match_open, ST_OPEN)
1112 match ("pause", gfc_match_pause, ST_NONE)
1113 match ("print", gfc_match_print, ST_WRITE)
1114 match ("read", gfc_match_read, ST_READ)
1115 match ("return", gfc_match_return, ST_RETURN)
1116 match ("rewind", gfc_match_rewind, ST_REWIND)
1117 match ("stop", gfc_match_stop, ST_STOP)
1118 match ("where", match_simple_where, ST_WHERE)
1119 match ("write", gfc_match_write, ST_WRITE)
1121 /* The gfc_match_assignment() above may have returned a MATCH_NO
1122 where the assignment was to a named constant. Check that
1123 special case here. */
1124 m = gfc_match_assignment ();
1127 gfc_error ("Cannot assign to a named constant at %C");
1128 gfc_free_expr (expr);
1129 gfc_undo_symbols ();
1130 gfc_current_locus = old_loc;
1134 /* All else has failed, so give up. See if any of the matchers has
1135 stored an error message of some sort. */
1136 if (gfc_error_check () == 0)
1137 gfc_error ("Unclassifiable statement in IF-clause at %C");
1139 gfc_free_expr (expr);
1144 gfc_error ("Syntax error in IF-clause at %C");
1147 gfc_free_expr (expr);
1151 /* At this point, we've matched the single IF and the action clause
1152 is in new_st. Rearrange things so that the IF statement appears
1155 p = gfc_get_code ();
1156 p->next = gfc_get_code ();
1158 p->next->loc = gfc_current_locus;
1163 gfc_clear_new_st ();
1165 new_st.op = EXEC_IF;
1174 /* Match an ELSE statement. */
1177 gfc_match_else (void)
1179 char name[GFC_MAX_SYMBOL_LEN + 1];
1181 if (gfc_match_eos () == MATCH_YES)
1184 if (gfc_match_name (name) != MATCH_YES
1185 || gfc_current_block () == NULL
1186 || gfc_match_eos () != MATCH_YES)
1188 gfc_error ("Unexpected junk after ELSE statement at %C");
1192 if (strcmp (name, gfc_current_block ()->name) != 0)
1194 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1195 name, gfc_current_block ()->name);
1203 /* Match an ELSE IF statement. */
1206 gfc_match_elseif (void)
1208 char name[GFC_MAX_SYMBOL_LEN + 1];
1212 m = gfc_match (" ( %e ) then", &expr);
1216 if (gfc_match_eos () == MATCH_YES)
1219 if (gfc_match_name (name) != MATCH_YES
1220 || gfc_current_block () == NULL
1221 || gfc_match_eos () != MATCH_YES)
1223 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1227 if (strcmp (name, gfc_current_block ()->name) != 0)
1229 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1230 name, gfc_current_block ()->name);
1235 new_st.op = EXEC_IF;
1240 gfc_free_expr (expr);
1245 /* Free a gfc_iterator structure. */
1248 gfc_free_iterator (gfc_iterator *iter, int flag)
1253 gfc_free_expr (iter->var);
1254 gfc_free_expr (iter->start);
1255 gfc_free_expr (iter->end);
1256 gfc_free_expr (iter->step);
1263 /* Match a DO statement. */
1268 gfc_iterator iter, *ip;
1270 gfc_st_label *label;
1273 old_loc = gfc_current_locus;
1276 iter.var = iter.start = iter.end = iter.step = NULL;
1278 m = gfc_match_label ();
1279 if (m == MATCH_ERROR)
1282 if (gfc_match (" do") != MATCH_YES)
1285 m = gfc_match_st_label (&label);
1286 if (m == MATCH_ERROR)
1289 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1291 if (gfc_match_eos () == MATCH_YES)
1293 iter.end = gfc_logical_expr (1, NULL);
1294 new_st.op = EXEC_DO_WHILE;
1298 /* match an optional comma, if no comma is found a space is obligatory. */
1299 if (gfc_match_char(',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1302 /* See if we have a DO WHILE. */
1303 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1305 new_st.op = EXEC_DO_WHILE;
1309 /* The abortive DO WHILE may have done something to the symbol
1310 table, so we start over: */
1311 gfc_undo_symbols ();
1312 gfc_current_locus = old_loc;
1314 gfc_match_label (); /* This won't error */
1315 gfc_match (" do "); /* This will work */
1317 gfc_match_st_label (&label); /* Can't error out */
1318 gfc_match_char (','); /* Optional comma */
1320 m = gfc_match_iterator (&iter, 0);
1323 if (m == MATCH_ERROR)
1326 gfc_check_do_variable (iter.var->symtree);
1328 if (gfc_match_eos () != MATCH_YES)
1330 gfc_syntax_error (ST_DO);
1334 new_st.op = EXEC_DO;
1338 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1341 new_st.label = label;
1343 if (new_st.op == EXEC_DO_WHILE)
1344 new_st.expr = iter.end;
1347 new_st.ext.iterator = ip = gfc_get_iterator ();
1354 gfc_free_iterator (&iter, 0);
1360 /* Match an EXIT or CYCLE statement. */
1363 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1365 gfc_state_data *p, *o;
1369 if (gfc_match_eos () == MATCH_YES)
1373 m = gfc_match ("% %s%t", &sym);
1374 if (m == MATCH_ERROR)
1378 gfc_syntax_error (st);
1382 if (sym->attr.flavor != FL_LABEL)
1384 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1385 sym->name, gfc_ascii_statement (st));
1390 /* Find the loop mentioned specified by the label (or lack of a
1392 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1393 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1395 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1401 gfc_error ("%s statement at %C is not within a loop",
1402 gfc_ascii_statement (st));
1404 gfc_error ("%s statement at %C is not within loop '%s'",
1405 gfc_ascii_statement (st), sym->name);
1412 gfc_error ("%s statement at %C leaving OpenMP structured block",
1413 gfc_ascii_statement (st));
1416 else if (st == ST_EXIT
1417 && p->previous != NULL
1418 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1419 && (p->previous->head->op == EXEC_OMP_DO
1420 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1422 gcc_assert (p->previous->head->next != NULL);
1423 gcc_assert (p->previous->head->next->op == EXEC_DO
1424 || p->previous->head->next->op == EXEC_DO_WHILE);
1425 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1429 /* Save the first statement in the loop - needed by the backend. */
1430 new_st.ext.whichloop = p->head;
1433 /* new_st.sym = sym;*/
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);
1520 /* Match the (deprecated) PAUSE statement. */
1523 gfc_match_pause (void)
1527 m = gfc_match_stopcode (ST_PAUSE);
1530 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: PAUSE statement at %C")
1538 /* Match the STOP statement. */
1541 gfc_match_stop (void)
1543 return gfc_match_stopcode (ST_STOP);
1547 /* Match a CONTINUE statement. */
1550 gfc_match_continue (void)
1552 if (gfc_match_eos () != MATCH_YES)
1554 gfc_syntax_error (ST_CONTINUE);
1558 new_st.op = EXEC_CONTINUE;
1563 /* Match the (deprecated) ASSIGN statement. */
1566 gfc_match_assign (void)
1569 gfc_st_label *label;
1571 if (gfc_match (" %l", &label) == MATCH_YES)
1573 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1575 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1577 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: ASSIGN "
1582 expr->symtree->n.sym->attr.assign = 1;
1584 new_st.op = EXEC_LABEL_ASSIGN;
1585 new_st.label = label;
1594 /* Match the GO TO statement. As a computed GOTO statement is
1595 matched, it is transformed into an equivalent SELECT block. No
1596 tree is necessary, and the resulting jumps-to-jumps are
1597 specifically optimized away by the back end. */
1600 gfc_match_goto (void)
1602 gfc_code *head, *tail;
1605 gfc_st_label *label;
1609 if (gfc_match (" %l%t", &label) == MATCH_YES)
1611 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1614 new_st.op = EXEC_GOTO;
1615 new_st.label = label;
1619 /* The assigned GO TO statement. */
1621 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1623 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: Assigned GOTO "
1628 new_st.op = EXEC_GOTO;
1631 if (gfc_match_eos () == MATCH_YES)
1634 /* Match label list. */
1635 gfc_match_char (',');
1636 if (gfc_match_char ('(') != MATCH_YES)
1638 gfc_syntax_error (ST_GOTO);
1645 m = gfc_match_st_label (&label);
1649 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1653 head = tail = gfc_get_code ();
1656 tail->block = gfc_get_code ();
1660 tail->label = label;
1661 tail->op = EXEC_GOTO;
1663 while (gfc_match_char (',') == MATCH_YES);
1665 if (gfc_match (")%t") != MATCH_YES)
1670 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1673 new_st.block = head;
1678 /* Last chance is a computed GO TO statement. */
1679 if (gfc_match_char ('(') != MATCH_YES)
1681 gfc_syntax_error (ST_GOTO);
1690 m = gfc_match_st_label (&label);
1694 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1698 head = tail = gfc_get_code ();
1701 tail->block = gfc_get_code ();
1705 cp = gfc_get_case ();
1706 cp->low = cp->high = gfc_int_expr (i++);
1708 tail->op = EXEC_SELECT;
1709 tail->ext.case_list = cp;
1711 tail->next = gfc_get_code ();
1712 tail->next->op = EXEC_GOTO;
1713 tail->next->label = label;
1715 while (gfc_match_char (',') == MATCH_YES);
1717 if (gfc_match_char (')') != MATCH_YES)
1722 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1726 /* Get the rest of the statement. */
1727 gfc_match_char (',');
1729 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1732 /* At this point, a computed GOTO has been fully matched and an
1733 equivalent SELECT statement constructed. */
1735 new_st.op = EXEC_SELECT;
1738 /* Hack: For a "real" SELECT, the expression is in expr. We put
1739 it in expr2 so we can distinguish then and produce the correct
1741 new_st.expr2 = expr;
1742 new_st.block = head;
1746 gfc_syntax_error (ST_GOTO);
1748 gfc_free_statements (head);
1753 /* Frees a list of gfc_alloc structures. */
1756 gfc_free_alloc_list (gfc_alloc *p)
1763 gfc_free_expr (p->expr);
1769 /* Match an ALLOCATE statement. */
1772 gfc_match_allocate (void)
1774 gfc_alloc *head, *tail;
1781 if (gfc_match_char ('(') != MATCH_YES)
1787 head = tail = gfc_get_alloc ();
1790 tail->next = gfc_get_alloc ();
1794 m = gfc_match_variable (&tail->expr, 0);
1797 if (m == MATCH_ERROR)
1800 if (gfc_check_do_variable (tail->expr->symtree))
1804 && gfc_impure_variable (tail->expr->symtree->n.sym))
1806 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1811 if (tail->expr->ts.type == BT_DERIVED)
1812 tail->expr->ts.derived = gfc_use_derived (tail->expr->ts.derived);
1814 if (gfc_match_char (',') != MATCH_YES)
1817 m = gfc_match (" stat = %v", &stat);
1818 if (m == MATCH_ERROR)
1826 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1828 gfc_error ("STAT variable '%s' of ALLOCATE statement at %C cannot "
1829 "be INTENT(IN)", stat->symtree->n.sym->name);
1833 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1835 gfc_error ("Illegal STAT variable in ALLOCATE statement at %C "
1836 "for a PURE procedure");
1840 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1842 gfc_error ("STAT expression at %C must be a variable");
1846 gfc_check_do_variable(stat->symtree);
1849 if (gfc_match (" )%t") != MATCH_YES)
1852 new_st.op = EXEC_ALLOCATE;
1854 new_st.ext.alloc_list = head;
1859 gfc_syntax_error (ST_ALLOCATE);
1862 gfc_free_expr (stat);
1863 gfc_free_alloc_list (head);
1868 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1869 a set of pointer assignments to intrinsic NULL(). */
1872 gfc_match_nullify (void)
1880 if (gfc_match_char ('(') != MATCH_YES)
1885 m = gfc_match_variable (&p, 0);
1886 if (m == MATCH_ERROR)
1891 if (gfc_check_do_variable(p->symtree))
1894 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1896 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
1900 /* build ' => NULL() ' */
1901 e = gfc_get_expr ();
1902 e->where = gfc_current_locus;
1903 e->expr_type = EXPR_NULL;
1904 e->ts.type = BT_UNKNOWN;
1911 tail->next = gfc_get_code ();
1915 tail->op = EXEC_POINTER_ASSIGN;
1919 if (gfc_match (" )%t") == MATCH_YES)
1921 if (gfc_match_char (',') != MATCH_YES)
1928 gfc_syntax_error (ST_NULLIFY);
1931 gfc_free_statements (new_st.next);
1936 /* Match a DEALLOCATE statement. */
1939 gfc_match_deallocate (void)
1941 gfc_alloc *head, *tail;
1948 if (gfc_match_char ('(') != MATCH_YES)
1954 head = tail = gfc_get_alloc ();
1957 tail->next = gfc_get_alloc ();
1961 m = gfc_match_variable (&tail->expr, 0);
1962 if (m == MATCH_ERROR)
1967 if (gfc_check_do_variable (tail->expr->symtree))
1971 && gfc_impure_variable (tail->expr->symtree->n.sym))
1973 gfc_error ("Illegal deallocate-expression in DEALLOCATE at %C "
1974 "for a PURE procedure");
1978 if (gfc_match_char (',') != MATCH_YES)
1981 m = gfc_match (" stat = %v", &stat);
1982 if (m == MATCH_ERROR)
1990 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1992 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1993 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
1997 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
1999 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
2000 "for a PURE procedure");
2004 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
2006 gfc_error ("STAT expression at %C must be a variable");
2010 gfc_check_do_variable(stat->symtree);
2013 if (gfc_match (" )%t") != MATCH_YES)
2016 new_st.op = EXEC_DEALLOCATE;
2018 new_st.ext.alloc_list = head;
2023 gfc_syntax_error (ST_DEALLOCATE);
2026 gfc_free_expr (stat);
2027 gfc_free_alloc_list (head);
2032 /* Match a RETURN statement. */
2035 gfc_match_return (void)
2039 gfc_compile_state s;
2043 if (gfc_match_eos () == MATCH_YES)
2046 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2048 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2053 if (gfc_current_form == FORM_FREE)
2055 /* The following are valid, so we can't require a blank after the
2059 c = gfc_peek_char ();
2060 if (ISALPHA (c) || ISDIGIT (c))
2064 m = gfc_match (" %e%t", &e);
2067 if (m == MATCH_ERROR)
2070 gfc_syntax_error (ST_RETURN);
2077 gfc_enclosing_unit (&s);
2078 if (s == COMP_PROGRAM
2079 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2080 "main program at %C") == FAILURE)
2083 new_st.op = EXEC_RETURN;
2090 /* Match a CALL statement. The tricky part here are possible
2091 alternate return specifiers. We handle these by having all
2092 "subroutines" actually return an integer via a register that gives
2093 the return number. If the call specifies alternate returns, we
2094 generate code for a SELECT statement whose case clauses contain
2095 GOTOs to the various labels. */
2098 gfc_match_call (void)
2100 char name[GFC_MAX_SYMBOL_LEN + 1];
2101 gfc_actual_arglist *a, *arglist;
2111 m = gfc_match ("% %n", name);
2117 if (gfc_get_ha_sym_tree (name, &st))
2121 gfc_set_sym_referenced (sym);
2123 if (!sym->attr.generic
2124 && !sym->attr.subroutine
2125 && gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2128 if (gfc_match_eos () != MATCH_YES)
2130 m = gfc_match_actual_arglist (1, &arglist);
2133 if (m == MATCH_ERROR)
2136 if (gfc_match_eos () != MATCH_YES)
2140 /* If any alternate return labels were found, construct a SELECT
2141 statement that will jump to the right place. */
2144 for (a = arglist; a; a = a->next)
2145 if (a->expr == NULL)
2150 gfc_symtree *select_st;
2151 gfc_symbol *select_sym;
2152 char name[GFC_MAX_SYMBOL_LEN + 1];
2154 new_st.next = c = gfc_get_code ();
2155 c->op = EXEC_SELECT;
2156 sprintf (name, "_result_%s", sym->name);
2157 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail */
2159 select_sym = select_st->n.sym;
2160 select_sym->ts.type = BT_INTEGER;
2161 select_sym->ts.kind = gfc_default_integer_kind;
2162 gfc_set_sym_referenced (select_sym);
2163 c->expr = gfc_get_expr ();
2164 c->expr->expr_type = EXPR_VARIABLE;
2165 c->expr->symtree = select_st;
2166 c->expr->ts = select_sym->ts;
2167 c->expr->where = gfc_current_locus;
2170 for (a = arglist; a; a = a->next)
2172 if (a->expr != NULL)
2175 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2180 c->block = gfc_get_code ();
2182 c->op = EXEC_SELECT;
2184 new_case = gfc_get_case ();
2185 new_case->high = new_case->low = gfc_int_expr (i);
2186 c->ext.case_list = new_case;
2188 c->next = gfc_get_code ();
2189 c->next->op = EXEC_GOTO;
2190 c->next->label = a->label;
2194 new_st.op = EXEC_CALL;
2195 new_st.symtree = st;
2196 new_st.ext.actual = arglist;
2201 gfc_syntax_error (ST_CALL);
2204 gfc_free_actual_arglist (arglist);
2209 /* Given a name, return a pointer to the common head structure,
2210 creating it if it does not exist. If FROM_MODULE is nonzero, we
2211 mangle the name so that it doesn't interfere with commons defined
2212 in the using namespace.
2213 TODO: Add to global symbol tree. */
2216 gfc_get_common (const char *name, int from_module)
2219 static int serial = 0;
2220 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
2224 /* A use associated common block is only needed to correctly layout
2225 the variables it contains. */
2226 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2227 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2231 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2234 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2237 if (st->n.common == NULL)
2239 st->n.common = gfc_get_common_head ();
2240 st->n.common->where = gfc_current_locus;
2241 strcpy (st->n.common->name, name);
2244 return st->n.common;
2248 /* Match a common block name. */
2251 match_common_name (char *name)
2255 if (gfc_match_char ('/') == MATCH_NO)
2261 if (gfc_match_char ('/') == MATCH_YES)
2267 m = gfc_match_name (name);
2269 if (m == MATCH_ERROR)
2271 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2274 gfc_error ("Syntax error in common block name at %C");
2279 /* Match a COMMON statement. */
2282 gfc_match_common (void)
2284 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2285 char name[GFC_MAX_SYMBOL_LEN + 1];
2292 old_blank_common = gfc_current_ns->blank_common.head;
2293 if (old_blank_common)
2295 while (old_blank_common->common_next)
2296 old_blank_common = old_blank_common->common_next;
2303 m = match_common_name (name);
2304 if (m == MATCH_ERROR)
2307 gsym = gfc_get_gsymbol (name);
2308 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2310 gfc_error ("Symbol '%s' at %C is already an external symbol that "
2311 "is not COMMON", name);
2315 if (gsym->type == GSYM_UNKNOWN)
2317 gsym->type = GSYM_COMMON;
2318 gsym->where = gfc_current_locus;
2324 if (name[0] == '\0')
2326 if (gfc_current_ns->is_block_data)
2328 gfc_warning ("BLOCK DATA unit cannot contain blank COMMON "
2331 t = &gfc_current_ns->blank_common;
2332 if (t->head == NULL)
2333 t->where = gfc_current_locus;
2337 t = gfc_get_common (name, 0);
2346 while (tail->common_next)
2347 tail = tail->common_next;
2350 /* Grab the list of symbols. */
2353 m = gfc_match_symbol (&sym, 0);
2354 if (m == MATCH_ERROR)
2359 if (sym->attr.in_common)
2361 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2366 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2369 if (sym->value != NULL
2370 && (name[0] == '\0' || !sym->attr.data))
2372 if (name[0] == '\0')
2373 gfc_error ("Previously initialized symbol '%s' in "
2374 "blank COMMON block at %C", sym->name);
2376 gfc_error ("Previously initialized symbol '%s' in "
2377 "COMMON block '%s' at %C", sym->name, name);
2381 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2384 /* Derived type names must have the SEQUENCE attribute. */
2385 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2387 gfc_error ("Derived type variable in COMMON at %C does not "
2388 "have the SEQUENCE attribute");
2393 tail->common_next = sym;
2399 /* Deal with an optional array specification after the
2401 m = gfc_match_array_spec (&as);
2402 if (m == MATCH_ERROR)
2407 if (as->type != AS_EXPLICIT)
2409 gfc_error ("Array specification for symbol '%s' in COMMON "
2410 "at %C must be explicit", sym->name);
2414 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2417 if (sym->attr.pointer)
2419 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
2420 "POINTER array", sym->name);
2429 sym->common_head = t;
2431 /* Check to see if the symbol is already in an equivalence group.
2432 If it is, set the other members as being in common. */
2433 if (sym->attr.in_equivalence)
2435 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2437 for (e2 = e1; e2; e2 = e2->eq)
2438 if (e2->expr->symtree->n.sym == sym)
2445 for (e2 = e1; e2; e2 = e2->eq)
2447 other = e2->expr->symtree->n.sym;
2448 if (other->common_head
2449 && other->common_head != sym->common_head)
2451 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2452 "%C is being indirectly equivalenced to "
2453 "another COMMON block '%s'",
2454 sym->name, sym->common_head->name,
2455 other->common_head->name);
2458 other->attr.in_common = 1;
2459 other->common_head = t;
2465 gfc_gobble_whitespace ();
2466 if (gfc_match_eos () == MATCH_YES)
2468 if (gfc_peek_char () == '/')
2470 if (gfc_match_char (',') != MATCH_YES)
2472 gfc_gobble_whitespace ();
2473 if (gfc_peek_char () == '/')
2482 gfc_syntax_error (ST_COMMON);
2485 if (old_blank_common)
2486 old_blank_common->common_next = NULL;
2488 gfc_current_ns->blank_common.head = NULL;
2489 gfc_free_array_spec (as);
2494 /* Match a BLOCK DATA program unit. */
2497 gfc_match_block_data (void)
2499 char name[GFC_MAX_SYMBOL_LEN + 1];
2503 if (gfc_match_eos () == MATCH_YES)
2505 gfc_new_block = NULL;
2509 m = gfc_match ("% %n%t", name);
2513 if (gfc_get_symbol (name, NULL, &sym))
2516 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2519 gfc_new_block = sym;
2525 /* Free a namelist structure. */
2528 gfc_free_namelist (gfc_namelist *name)
2532 for (; name; name = n)
2540 /* Match a NAMELIST statement. */
2543 gfc_match_namelist (void)
2545 gfc_symbol *group_name, *sym;
2549 m = gfc_match (" / %s /", &group_name);
2552 if (m == MATCH_ERROR)
2557 if (group_name->ts.type != BT_UNKNOWN)
2559 gfc_error ("Namelist group name '%s' at %C already has a basic "
2560 "type of %s", group_name->name,
2561 gfc_typename (&group_name->ts));
2565 if (group_name->attr.flavor == FL_NAMELIST
2566 && group_name->attr.use_assoc
2567 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2568 "at %C already is USE associated and can"
2569 "not be respecified.", group_name->name)
2573 if (group_name->attr.flavor != FL_NAMELIST
2574 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2575 group_name->name, NULL) == FAILURE)
2580 m = gfc_match_symbol (&sym, 1);
2583 if (m == MATCH_ERROR)
2586 if (sym->attr.in_namelist == 0
2587 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2590 /* Use gfc_error_check here, rather than goto error, so that
2591 these are the only errors for the next two lines. */
2592 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2594 gfc_error ("Assumed size array '%s' in namelist '%s' at "
2595 "%C is not allowed", sym->name, group_name->name);
2599 if (sym->ts.type == BT_CHARACTER && sym->ts.cl->length == NULL)
2601 gfc_error ("Assumed character length '%s' in namelist '%s' at "
2602 "%C is not allowed", sym->name, group_name->name);
2606 if (sym->as && sym->as->type == AS_ASSUMED_SHAPE
2607 && gfc_notify_std (GFC_STD_GNU, "Assumed shape array '%s' in "
2608 "namelist '%s' at %C is an extension.",
2609 sym->name, group_name->name) == FAILURE)
2612 nl = gfc_get_namelist ();
2616 if (group_name->namelist == NULL)
2617 group_name->namelist = group_name->namelist_tail = nl;
2620 group_name->namelist_tail->next = nl;
2621 group_name->namelist_tail = nl;
2624 if (gfc_match_eos () == MATCH_YES)
2627 m = gfc_match_char (',');
2629 if (gfc_match_char ('/') == MATCH_YES)
2631 m2 = gfc_match (" %s /", &group_name);
2632 if (m2 == MATCH_YES)
2634 if (m2 == MATCH_ERROR)
2648 gfc_syntax_error (ST_NAMELIST);
2655 /* Match a MODULE statement. */
2658 gfc_match_module (void)
2662 m = gfc_match (" %s%t", &gfc_new_block);
2666 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2667 gfc_new_block->name, NULL) == FAILURE)
2674 /* Free equivalence sets and lists. Recursively is the easiest way to
2678 gfc_free_equiv (gfc_equiv *eq)
2683 gfc_free_equiv (eq->eq);
2684 gfc_free_equiv (eq->next);
2685 gfc_free_expr (eq->expr);
2690 /* Match an EQUIVALENCE statement. */
2693 gfc_match_equivalence (void)
2695 gfc_equiv *eq, *set, *tail;
2699 gfc_common_head *common_head = NULL;
2707 eq = gfc_get_equiv ();
2711 eq->next = gfc_current_ns->equiv;
2712 gfc_current_ns->equiv = eq;
2714 if (gfc_match_char ('(') != MATCH_YES)
2718 common_flag = FALSE;
2723 m = gfc_match_equiv_variable (&set->expr);
2724 if (m == MATCH_ERROR)
2729 /* count the number of objects. */
2732 if (gfc_match_char ('%') == MATCH_YES)
2734 gfc_error ("Derived type component %C is not a "
2735 "permitted EQUIVALENCE member");
2739 for (ref = set->expr->ref; ref; ref = ref->next)
2740 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2742 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
2743 "be an array section");
2747 sym = set->expr->symtree->n.sym;
2749 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
2752 if (sym->attr.in_common)
2755 common_head = sym->common_head;
2758 if (gfc_match_char (')') == MATCH_YES)
2761 if (gfc_match_char (',') != MATCH_YES)
2764 set->eq = gfc_get_equiv ();
2770 gfc_error ("EQUIVALENCE at %C requires two or more objects");
2774 /* If one of the members of an equivalence is in common, then
2775 mark them all as being in common. Before doing this, check
2776 that members of the equivalence group are not in different
2779 for (set = eq; set; set = set->eq)
2781 sym = set->expr->symtree->n.sym;
2782 if (sym->common_head && sym->common_head != common_head)
2784 gfc_error ("Attempt to indirectly overlap COMMON "
2785 "blocks %s and %s by EQUIVALENCE at %C",
2786 sym->common_head->name, common_head->name);
2789 sym->attr.in_common = 1;
2790 sym->common_head = common_head;
2793 if (gfc_match_eos () == MATCH_YES)
2795 if (gfc_match_char (',') != MATCH_YES)
2802 gfc_syntax_error (ST_EQUIVALENCE);
2808 gfc_free_equiv (gfc_current_ns->equiv);
2809 gfc_current_ns->equiv = eq;
2815 /* Check that a statement function is not recursive. This is done by looking
2816 for the statement function symbol(sym) by looking recursively through its
2817 expression(e). If a reference to sym is found, true is returned.
2818 12.5.4 requires that any variable of function that is implicitly typed
2819 shall have that type confirmed by any subsequent type declaration. The
2820 implicit typing is conveniently done here. */
2823 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
2825 gfc_actual_arglist *arg;
2832 switch (e->expr_type)
2835 for (arg = e->value.function.actual; arg; arg = arg->next)
2837 if (sym->name == arg->name || recursive_stmt_fcn (arg->expr, sym))
2841 if (e->symtree == NULL)
2844 /* Check the name before testing for nested recursion! */
2845 if (sym->name == e->symtree->n.sym->name)
2848 /* Catch recursion via other statement functions. */
2849 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
2850 && e->symtree->n.sym->value
2851 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
2854 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
2855 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
2860 if (e->symtree && sym->name == e->symtree->n.sym->name)
2863 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
2864 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
2868 if (recursive_stmt_fcn (e->value.op.op1, sym)
2869 || recursive_stmt_fcn (e->value.op.op2, sym))
2877 /* Component references do not need to be checked. */
2880 for (ref = e->ref; ref; ref = ref->next)
2885 for (i = 0; i < ref->u.ar.dimen; i++)
2887 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
2888 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
2889 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
2895 if (recursive_stmt_fcn (ref->u.ss.start, sym)
2896 || recursive_stmt_fcn (ref->u.ss.end, sym))
2910 /* Match a statement function declaration. It is so easy to match
2911 non-statement function statements with a MATCH_ERROR as opposed to
2912 MATCH_NO that we suppress error message in most cases. */
2915 gfc_match_st_function (void)
2917 gfc_error_buf old_error;
2922 m = gfc_match_symbol (&sym, 0);
2926 gfc_push_error (&old_error);
2928 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
2929 sym->name, NULL) == FAILURE)
2932 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2935 m = gfc_match (" = %e%t", &expr);
2939 gfc_free_error (&old_error);
2940 if (m == MATCH_ERROR)
2943 if (recursive_stmt_fcn (expr, sym))
2945 gfc_error ("Statement function at %L is recursive", &expr->where);
2954 gfc_pop_error (&old_error);
2959 /***************** SELECT CASE subroutines ******************/
2961 /* Free a single case structure. */
2964 free_case (gfc_case *p)
2966 if (p->low == p->high)
2968 gfc_free_expr (p->low);
2969 gfc_free_expr (p->high);
2974 /* Free a list of case structures. */
2977 gfc_free_case_list (gfc_case *p)
2989 /* Match a single case selector. */
2992 match_case_selector (gfc_case **cp)
2997 c = gfc_get_case ();
2998 c->where = gfc_current_locus;
3000 if (gfc_match_char (':') == MATCH_YES)
3002 m = gfc_match_init_expr (&c->high);
3005 if (m == MATCH_ERROR)
3010 m = gfc_match_init_expr (&c->low);
3011 if (m == MATCH_ERROR)
3016 /* If we're not looking at a ':' now, make a range out of a single
3017 target. Else get the upper bound for the case range. */
3018 if (gfc_match_char (':') != MATCH_YES)
3022 m = gfc_match_init_expr (&c->high);
3023 if (m == MATCH_ERROR)
3025 /* MATCH_NO is fine. It's OK if nothing is there! */
3033 gfc_error ("Expected initialization expression in CASE at %C");
3041 /* Match the end of a case statement. */
3044 match_case_eos (void)
3046 char name[GFC_MAX_SYMBOL_LEN + 1];
3049 if (gfc_match_eos () == MATCH_YES)
3052 /* If the case construct doesn't have a case-construct-name, we
3053 should have matched the EOS. */
3054 if (!gfc_current_block ())
3056 gfc_error ("Expected the name of the SELECT CASE construct at %C");
3060 gfc_gobble_whitespace ();
3062 m = gfc_match_name (name);
3066 if (strcmp (name, gfc_current_block ()->name) != 0)
3068 gfc_error ("Expected case name of '%s' at %C",
3069 gfc_current_block ()->name);
3073 return gfc_match_eos ();
3077 /* Match a SELECT statement. */
3080 gfc_match_select (void)
3085 m = gfc_match_label ();
3086 if (m == MATCH_ERROR)
3089 m = gfc_match (" select case ( %e )%t", &expr);
3093 new_st.op = EXEC_SELECT;
3100 /* Match a CASE statement. */
3103 gfc_match_case (void)
3105 gfc_case *c, *head, *tail;
3110 if (gfc_current_state () != COMP_SELECT)
3112 gfc_error ("Unexpected CASE statement at %C");
3116 if (gfc_match ("% default") == MATCH_YES)
3118 m = match_case_eos ();
3121 if (m == MATCH_ERROR)
3124 new_st.op = EXEC_SELECT;
3125 c = gfc_get_case ();
3126 c->where = gfc_current_locus;
3127 new_st.ext.case_list = c;
3131 if (gfc_match_char ('(') != MATCH_YES)
3136 if (match_case_selector (&c) == MATCH_ERROR)
3146 if (gfc_match_char (')') == MATCH_YES)
3148 if (gfc_match_char (',') != MATCH_YES)
3152 m = match_case_eos ();
3155 if (m == MATCH_ERROR)
3158 new_st.op = EXEC_SELECT;
3159 new_st.ext.case_list = head;
3164 gfc_error ("Syntax error in CASE-specification at %C");
3167 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3171 /********************* WHERE subroutines ********************/
3173 /* Match the rest of a simple WHERE statement that follows an IF statement.
3177 match_simple_where (void)
3183 m = gfc_match (" ( %e )", &expr);
3187 m = gfc_match_assignment ();
3190 if (m == MATCH_ERROR)
3193 if (gfc_match_eos () != MATCH_YES)
3196 c = gfc_get_code ();
3200 c->next = gfc_get_code ();
3203 gfc_clear_new_st ();
3205 new_st.op = EXEC_WHERE;
3211 gfc_syntax_error (ST_WHERE);
3214 gfc_free_expr (expr);
3218 /* Match a WHERE statement. */
3221 gfc_match_where (gfc_statement *st)
3227 m0 = gfc_match_label ();
3228 if (m0 == MATCH_ERROR)
3231 m = gfc_match (" where ( %e )", &expr);
3235 if (gfc_match_eos () == MATCH_YES)
3237 *st = ST_WHERE_BLOCK;
3238 new_st.op = EXEC_WHERE;
3243 m = gfc_match_assignment ();
3245 gfc_syntax_error (ST_WHERE);
3249 gfc_free_expr (expr);
3253 /* We've got a simple WHERE statement. */
3255 c = gfc_get_code ();
3259 c->next = gfc_get_code ();
3262 gfc_clear_new_st ();
3264 new_st.op = EXEC_WHERE;
3271 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3272 new_st if successful. */
3275 gfc_match_elsewhere (void)
3277 char name[GFC_MAX_SYMBOL_LEN + 1];
3281 if (gfc_current_state () != COMP_WHERE)
3283 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3289 if (gfc_match_char ('(') == MATCH_YES)
3291 m = gfc_match_expr (&expr);
3294 if (m == MATCH_ERROR)
3297 if (gfc_match_char (')') != MATCH_YES)
3301 if (gfc_match_eos () != MATCH_YES)
3303 /* Only makes sense if we have a where-construct-name. */
3304 if (!gfc_current_block ())
3309 /* Better be a name at this point */
3310 m = gfc_match_name (name);
3313 if (m == MATCH_ERROR)
3316 if (gfc_match_eos () != MATCH_YES)
3319 if (strcmp (name, gfc_current_block ()->name) != 0)
3321 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3322 name, gfc_current_block ()->name);
3327 new_st.op = EXEC_WHERE;
3332 gfc_syntax_error (ST_ELSEWHERE);
3335 gfc_free_expr (expr);
3340 /******************** FORALL subroutines ********************/
3342 /* Free a list of FORALL iterators. */
3345 gfc_free_forall_iterator (gfc_forall_iterator *iter)
3347 gfc_forall_iterator *next;
3352 gfc_free_expr (iter->var);
3353 gfc_free_expr (iter->start);
3354 gfc_free_expr (iter->end);
3355 gfc_free_expr (iter->stride);
3362 /* Match an iterator as part of a FORALL statement. The format is:
3364 <var> = <start>:<end>[:<stride>]
3366 On MATCH_NO, the caller tests for the possibility that there is a
3367 scalar mask expression. */
3370 match_forall_iterator (gfc_forall_iterator **result)
3372 gfc_forall_iterator *iter;
3376 where = gfc_current_locus;
3377 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3379 m = gfc_match_expr (&iter->var);
3383 if (gfc_match_char ('=') != MATCH_YES
3384 || iter->var->expr_type != EXPR_VARIABLE)
3390 m = gfc_match_expr (&iter->start);
3394 if (gfc_match_char (':') != MATCH_YES)
3397 m = gfc_match_expr (&iter->end);
3400 if (m == MATCH_ERROR)
3403 if (gfc_match_char (':') == MATCH_NO)
3404 iter->stride = gfc_int_expr (1);
3407 m = gfc_match_expr (&iter->stride);
3410 if (m == MATCH_ERROR)
3414 /* Mark the iteration variable's symbol as used as a FORALL index. */
3415 iter->var->symtree->n.sym->forall_index = true;
3421 gfc_error ("Syntax error in FORALL iterator at %C");
3426 gfc_current_locus = where;
3427 gfc_free_forall_iterator (iter);
3432 /* Match the header of a FORALL statement. */
3435 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
3437 gfc_forall_iterator *head, *tail, *new;
3441 gfc_gobble_whitespace ();
3446 if (gfc_match_char ('(') != MATCH_YES)
3449 m = match_forall_iterator (&new);
3450 if (m == MATCH_ERROR)
3459 if (gfc_match_char (',') != MATCH_YES)
3462 m = match_forall_iterator (&new);
3463 if (m == MATCH_ERROR)
3473 /* Have to have a mask expression */
3475 m = gfc_match_expr (&msk);
3478 if (m == MATCH_ERROR)
3484 if (gfc_match_char (')') == MATCH_NO)
3492 gfc_syntax_error (ST_FORALL);
3495 gfc_free_expr (msk);
3496 gfc_free_forall_iterator (head);
3501 /* Match the rest of a simple FORALL statement that follows an
3505 match_simple_forall (void)
3507 gfc_forall_iterator *head;
3516 m = match_forall_header (&head, &mask);
3523 m = gfc_match_assignment ();
3525 if (m == MATCH_ERROR)
3529 m = gfc_match_pointer_assignment ();
3530 if (m == MATCH_ERROR)
3536 c = gfc_get_code ();
3538 c->loc = gfc_current_locus;
3540 if (gfc_match_eos () != MATCH_YES)
3543 gfc_clear_new_st ();
3544 new_st.op = EXEC_FORALL;
3546 new_st.ext.forall_iterator = head;
3547 new_st.block = gfc_get_code ();
3549 new_st.block->op = EXEC_FORALL;
3550 new_st.block->next = c;
3555 gfc_syntax_error (ST_FORALL);
3558 gfc_free_forall_iterator (head);
3559 gfc_free_expr (mask);
3565 /* Match a FORALL statement. */
3568 gfc_match_forall (gfc_statement *st)
3570 gfc_forall_iterator *head;
3579 m0 = gfc_match_label ();
3580 if (m0 == MATCH_ERROR)
3583 m = gfc_match (" forall");
3587 m = match_forall_header (&head, &mask);
3588 if (m == MATCH_ERROR)
3593 if (gfc_match_eos () == MATCH_YES)
3595 *st = ST_FORALL_BLOCK;
3596 new_st.op = EXEC_FORALL;
3598 new_st.ext.forall_iterator = head;
3602 m = gfc_match_assignment ();
3603 if (m == MATCH_ERROR)
3607 m = gfc_match_pointer_assignment ();
3608 if (m == MATCH_ERROR)
3614 c = gfc_get_code ();
3616 c->loc = gfc_current_locus;
3618 gfc_clear_new_st ();
3619 new_st.op = EXEC_FORALL;
3621 new_st.ext.forall_iterator = head;
3622 new_st.block = gfc_get_code ();
3623 new_st.block->op = EXEC_FORALL;
3624 new_st.block->next = c;
3630 gfc_syntax_error (ST_FORALL);
3633 gfc_free_forall_iterator (head);
3634 gfc_free_expr (mask);
3635 gfc_free_statements (c);