1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
31 /* For matching and debugging purposes. Order matters here! The
32 unary operators /must/ precede the binary plus and minus, or
33 the expression parser breaks. */
35 mstring intrinsic_operators[] = {
36 minit ("+", INTRINSIC_UPLUS),
37 minit ("-", INTRINSIC_UMINUS),
38 minit ("+", INTRINSIC_PLUS),
39 minit ("-", INTRINSIC_MINUS),
40 minit ("**", INTRINSIC_POWER),
41 minit ("//", INTRINSIC_CONCAT),
42 minit ("*", INTRINSIC_TIMES),
43 minit ("/", INTRINSIC_DIVIDE),
44 minit (".and.", INTRINSIC_AND),
45 minit (".or.", INTRINSIC_OR),
46 minit (".eqv.", INTRINSIC_EQV),
47 minit (".neqv.", INTRINSIC_NEQV),
48 minit (".eq.", INTRINSIC_EQ),
49 minit ("==", INTRINSIC_EQ),
50 minit (".ne.", INTRINSIC_NE),
51 minit ("/=", INTRINSIC_NE),
52 minit (".ge.", INTRINSIC_GE),
53 minit (">=", INTRINSIC_GE),
54 minit (".le.", INTRINSIC_LE),
55 minit ("<=", INTRINSIC_LE),
56 minit (".lt.", INTRINSIC_LT),
57 minit ("<", INTRINSIC_LT),
58 minit (".gt.", INTRINSIC_GT),
59 minit (">", INTRINSIC_GT),
60 minit (".not.", INTRINSIC_NOT),
61 minit ("parens", INTRINSIC_PARENTHESES),
62 minit (NULL, INTRINSIC_NONE)
66 /******************** Generic matching subroutines ************************/
68 /* In free form, match at least one space. Always matches in fixed
72 gfc_match_space (void)
77 if (gfc_current_form == FORM_FIXED)
80 old_loc = gfc_current_locus;
83 if (!gfc_is_whitespace (c))
85 gfc_current_locus = old_loc;
89 gfc_gobble_whitespace ();
95 /* Match an end of statement. End of statement is optional
96 whitespace, followed by a ';' or '\n' or comment '!'. If a
97 semicolon is found, we continue to eat whitespace and semicolons. */
109 old_loc = gfc_current_locus;
110 gfc_gobble_whitespace ();
112 c = gfc_next_char ();
118 c = gfc_next_char ();
135 gfc_current_locus = old_loc;
136 return (flag) ? MATCH_YES : MATCH_NO;
140 /* Match a literal integer on the input, setting the value on
141 MATCH_YES. Literal ints occur in kind-parameters as well as
142 old-style character length specifications. If cnt is non-NULL it
143 will be set to the number of digits. */
146 gfc_match_small_literal_int (int *value, int *cnt)
152 old_loc = gfc_current_locus;
154 gfc_gobble_whitespace ();
155 c = gfc_next_char ();
161 gfc_current_locus = old_loc;
170 old_loc = gfc_current_locus;
171 c = gfc_next_char ();
176 i = 10 * i + c - '0';
181 gfc_error ("Integer too large at %C");
186 gfc_current_locus = old_loc;
195 /* Match a small, constant integer expression, like in a kind
196 statement. On MATCH_YES, 'value' is set. */
199 gfc_match_small_int (int *value)
206 m = gfc_match_expr (&expr);
210 p = gfc_extract_int (expr, &i);
211 gfc_free_expr (expr);
224 /* Matches a statement label. Uses gfc_match_small_literal_int() to
225 do most of the work. */
228 gfc_match_st_label (gfc_st_label ** label)
234 old_loc = gfc_current_locus;
236 m = gfc_match_small_literal_int (&i, &cnt);
242 gfc_error ("Too many digits in statement label at %C");
248 gfc_error ("Statement label at %C is zero");
252 *label = gfc_get_st_label (i);
257 gfc_current_locus = old_loc;
262 /* Match and validate a label associated with a named IF, DO or SELECT
263 statement. If the symbol does not have the label attribute, we add
264 it. We also make sure the symbol does not refer to another
265 (active) block. A matched label is pointed to by gfc_new_block. */
268 gfc_match_label (void)
270 char name[GFC_MAX_SYMBOL_LEN + 1];
273 gfc_new_block = NULL;
275 m = gfc_match (" %n :", name);
279 if (gfc_get_symbol (name, NULL, &gfc_new_block))
281 gfc_error ("Label name '%s' at %C is ambiguous", name);
285 if (gfc_new_block->attr.flavor == FL_LABEL)
287 gfc_error ("Duplicate construct label '%s' at %C", name);
291 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
292 gfc_new_block->name, NULL) == FAILURE)
299 /* Try and match the input against an array of possibilities. If one
300 potential matching string is a substring of another, the longest
301 match takes precedence. Spaces in the target strings are optional
302 spaces that do not necessarily have to be found in the input
303 stream. In fixed mode, spaces never appear. If whitespace is
304 matched, it matches unlimited whitespace in the input. For this
305 reason, the 'mp' member of the mstring structure is used to track
306 the progress of each potential match.
308 If there is no match we return the tag associated with the
309 terminating NULL mstring structure and leave the locus pointer
310 where it started. If there is a match we return the tag member of
311 the matched mstring and leave the locus pointer after the matched
314 A '%' character is a mandatory space. */
317 gfc_match_strings (mstring * a)
319 mstring *p, *best_match;
320 int no_match, c, possibles;
325 for (p = a; p->string != NULL; p++)
334 match_loc = gfc_current_locus;
336 gfc_gobble_whitespace ();
338 while (possibles > 0)
340 c = gfc_next_char ();
342 /* Apply the next character to the current possibilities. */
343 for (p = a; p->string != NULL; p++)
350 /* Space matches 1+ whitespace(s). */
351 if ((gfc_current_form == FORM_FREE)
352 && gfc_is_whitespace (c))
370 match_loc = gfc_current_locus;
378 gfc_current_locus = match_loc;
380 return (best_match == NULL) ? no_match : best_match->tag;
384 /* See if the current input looks like a name of some sort. Modifies
385 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long. */
388 gfc_match_name (char *buffer)
393 old_loc = gfc_current_locus;
394 gfc_gobble_whitespace ();
396 c = gfc_next_char ();
399 gfc_current_locus = old_loc;
409 if (i > gfc_option.max_identifier_length)
411 gfc_error ("Name at %C is too long");
415 old_loc = gfc_current_locus;
416 c = gfc_next_char ();
420 || (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;
473 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
474 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
478 gfc_match_intrinsic_op (gfc_intrinsic_op * result)
482 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
484 if (op == INTRINSIC_NONE)
492 /* Match a loop control phrase:
494 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
496 If the final integer expression is not present, a constant unity
497 expression is returned. We don't return MATCH_ERROR until after
498 the equals sign is seen. */
501 gfc_match_iterator (gfc_iterator * iter, int init_flag)
503 char name[GFC_MAX_SYMBOL_LEN + 1];
504 gfc_expr *var, *e1, *e2, *e3;
508 /* Match the start of an iterator without affecting the symbol
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 if (var->symtree->n.sym->attr.pointer)
541 gfc_error ("Loop variable at %C cannot have the POINTER attribute");
545 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
548 if (m == MATCH_ERROR)
551 if (gfc_match_char (',') != MATCH_YES)
554 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
557 if (m == MATCH_ERROR)
560 if (gfc_match_char (',') != MATCH_YES)
562 e3 = gfc_int_expr (1);
566 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
567 if (m == MATCH_ERROR)
571 gfc_error ("Expected a step value in iterator at %C");
583 gfc_error ("Syntax error in iterator at %C");
594 /* Tries to match the next non-whitespace character on the input.
595 This subroutine does not return MATCH_ERROR. */
598 gfc_match_char (char c)
602 where = gfc_current_locus;
603 gfc_gobble_whitespace ();
605 if (gfc_next_char () == c)
608 gfc_current_locus = where;
613 /* General purpose matching subroutine. The target string is a
614 scanf-like format string in which spaces correspond to arbitrary
615 whitespace (including no whitespace), characters correspond to
616 themselves. The %-codes are:
618 %% Literal percent sign
619 %e Expression, pointer to a pointer is set
620 %s Symbol, pointer to the symbol is set
621 %n Name, character buffer is set to name
622 %t Matches end of statement.
623 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
624 %l Matches a statement label
625 %v Matches a variable expression (an lvalue)
626 % Matches a required space (in free form) and optional spaces. */
629 gfc_match (const char *target, ...)
631 gfc_st_label **label;
640 old_loc = gfc_current_locus;
641 va_start (argp, target);
651 gfc_gobble_whitespace ();
662 vp = va_arg (argp, void **);
663 n = gfc_match_expr ((gfc_expr **) vp);
674 vp = va_arg (argp, void **);
675 n = gfc_match_variable ((gfc_expr **) vp, 0);
686 vp = va_arg (argp, void **);
687 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
698 np = va_arg (argp, char *);
699 n = gfc_match_name (np);
710 label = va_arg (argp, gfc_st_label **);
711 n = gfc_match_st_label (label);
722 ip = va_arg (argp, int *);
723 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
734 if (gfc_match_eos () != MATCH_YES)
742 if (gfc_match_space () == MATCH_YES)
748 break; /* Fall through to character matcher */
751 gfc_internal_error ("gfc_match(): Bad match code %c", c);
755 if (c == gfc_next_char ())
765 /* Clean up after a failed match. */
766 gfc_current_locus = old_loc;
767 va_start (argp, target);
770 for (; matches > 0; matches--)
780 /* Matches that don't have to be undone */
785 (void)va_arg (argp, void **);
790 vp = va_arg (argp, void **);
804 /*********************** Statement level matching **********************/
806 /* Matches the start of a program unit, which is the program keyword
807 followed by an obligatory symbol. */
810 gfc_match_program (void)
815 m = gfc_match ("% %s%t", &sym);
819 gfc_error ("Invalid form of PROGRAM statement at %C");
823 if (m == MATCH_ERROR)
826 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
835 /* Match a simple assignment statement. */
838 gfc_match_assignment (void)
840 gfc_expr *lvalue, *rvalue;
844 old_loc = gfc_current_locus;
846 lvalue = rvalue = NULL;
847 m = gfc_match (" %v =", &lvalue);
851 if (lvalue->symtree->n.sym->attr.flavor == FL_PARAMETER)
853 gfc_error ("Cannot assign to a PARAMETER variable at %C");
858 m = gfc_match (" %e%t", &rvalue);
862 gfc_set_sym_referenced (lvalue->symtree->n.sym);
864 new_st.op = EXEC_ASSIGN;
865 new_st.expr = lvalue;
866 new_st.expr2 = rvalue;
868 gfc_check_do_variable (lvalue->symtree);
873 gfc_current_locus = old_loc;
874 gfc_free_expr (lvalue);
875 gfc_free_expr (rvalue);
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 new_st.op = EXEC_POINTER_ASSIGN;
905 new_st.expr = lvalue;
906 new_st.expr2 = rvalue;
911 gfc_current_locus = old_loc;
912 gfc_free_expr (lvalue);
913 gfc_free_expr (rvalue);
918 /* We try to match an easy arithmetic IF statement. This only happens
919 when just after having encountered a simple IF statement. This code
920 is really duplicate with parts of the gfc_match_if code, but this is
923 match_arithmetic_if (void)
925 gfc_st_label *l1, *l2, *l3;
929 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
933 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
934 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
935 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
937 gfc_free_expr (expr);
941 if (gfc_notify_std (GFC_STD_F95_DEL,
942 "Obsolete: arithmetic IF statement at %C") == FAILURE)
945 new_st.op = EXEC_ARITHMETIC_IF;
955 /* The IF statement is a bit of a pain. First of all, there are three
956 forms of it, the simple IF, the IF that starts a block and the
959 There is a problem with the simple IF and that is the fact that we
960 only have a single level of undo information on symbols. What this
961 means is for a simple IF, we must re-match the whole IF statement
962 multiple times in order to guarantee that the symbol table ends up
963 in the proper state. */
965 static match match_simple_forall (void);
966 static match match_simple_where (void);
969 gfc_match_if (gfc_statement * if_type)
972 gfc_st_label *l1, *l2, *l3;
977 n = gfc_match_label ();
978 if (n == MATCH_ERROR)
981 old_loc = gfc_current_locus;
983 m = gfc_match (" if ( %e", &expr);
987 if (gfc_match_char (')') != MATCH_YES)
989 gfc_error ("Syntax error in IF-expression at %C");
990 gfc_free_expr (expr);
994 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1001 ("Block label not appropriate for arithmetic IF statement "
1004 gfc_free_expr (expr);
1008 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1009 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1010 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1013 gfc_free_expr (expr);
1017 if (gfc_notify_std (GFC_STD_F95_DEL,
1018 "Obsolete: arithmetic IF statement at %C")
1022 new_st.op = EXEC_ARITHMETIC_IF;
1028 *if_type = ST_ARITHMETIC_IF;
1032 if (gfc_match (" then%t") == MATCH_YES)
1034 new_st.op = EXEC_IF;
1037 *if_type = ST_IF_BLOCK;
1043 gfc_error ("Block label is not appropriate IF statement at %C");
1045 gfc_free_expr (expr);
1049 /* At this point the only thing left is a simple IF statement. At
1050 this point, n has to be MATCH_NO, so we don't have to worry about
1051 re-matching a block label. From what we've got so far, try
1052 matching an assignment. */
1054 *if_type = ST_SIMPLE_IF;
1056 m = gfc_match_assignment ();
1060 gfc_free_expr (expr);
1061 gfc_undo_symbols ();
1062 gfc_current_locus = old_loc;
1064 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1066 m = gfc_match_pointer_assignment ();
1070 gfc_free_expr (expr);
1071 gfc_undo_symbols ();
1072 gfc_current_locus = old_loc;
1074 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1076 /* Look at the next keyword to see which matcher to call. Matching
1077 the keyword doesn't affect the symbol table, so we don't have to
1078 restore between tries. */
1080 #define match(string, subr, statement) \
1081 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1085 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1086 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1087 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1088 match ("call", gfc_match_call, ST_CALL)
1089 match ("close", gfc_match_close, ST_CLOSE)
1090 match ("continue", gfc_match_continue, ST_CONTINUE)
1091 match ("cycle", gfc_match_cycle, ST_CYCLE)
1092 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1093 match ("end file", gfc_match_endfile, ST_END_FILE)
1094 match ("exit", gfc_match_exit, ST_EXIT)
1095 match ("flush", gfc_match_flush, ST_FLUSH)
1096 match ("forall", match_simple_forall, ST_FORALL)
1097 match ("go to", gfc_match_goto, ST_GOTO)
1098 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1099 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1100 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1101 match ("open", gfc_match_open, ST_OPEN)
1102 match ("pause", gfc_match_pause, ST_NONE)
1103 match ("print", gfc_match_print, ST_WRITE)
1104 match ("read", gfc_match_read, ST_READ)
1105 match ("return", gfc_match_return, ST_RETURN)
1106 match ("rewind", gfc_match_rewind, ST_REWIND)
1107 match ("stop", gfc_match_stop, ST_STOP)
1108 match ("where", match_simple_where, ST_WHERE)
1109 match ("write", gfc_match_write, ST_WRITE)
1111 /* All else has failed, so give up. See if any of the matchers has
1112 stored an error message of some sort. */
1113 if (gfc_error_check () == 0)
1114 gfc_error ("Unclassifiable statement in IF-clause at %C");
1116 gfc_free_expr (expr);
1121 gfc_error ("Syntax error in IF-clause at %C");
1124 gfc_free_expr (expr);
1128 /* At this point, we've matched the single IF and the action clause
1129 is in new_st. Rearrange things so that the IF statement appears
1132 p = gfc_get_code ();
1133 p->next = gfc_get_code ();
1135 p->next->loc = gfc_current_locus;
1140 gfc_clear_new_st ();
1142 new_st.op = EXEC_IF;
1151 /* Match an ELSE statement. */
1154 gfc_match_else (void)
1156 char name[GFC_MAX_SYMBOL_LEN + 1];
1158 if (gfc_match_eos () == MATCH_YES)
1161 if (gfc_match_name (name) != MATCH_YES
1162 || gfc_current_block () == NULL
1163 || gfc_match_eos () != MATCH_YES)
1165 gfc_error ("Unexpected junk after ELSE statement at %C");
1169 if (strcmp (name, gfc_current_block ()->name) != 0)
1171 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1172 name, gfc_current_block ()->name);
1180 /* Match an ELSE IF statement. */
1183 gfc_match_elseif (void)
1185 char name[GFC_MAX_SYMBOL_LEN + 1];
1189 m = gfc_match (" ( %e ) then", &expr);
1193 if (gfc_match_eos () == MATCH_YES)
1196 if (gfc_match_name (name) != MATCH_YES
1197 || gfc_current_block () == NULL
1198 || gfc_match_eos () != MATCH_YES)
1200 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1204 if (strcmp (name, gfc_current_block ()->name) != 0)
1206 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1207 name, gfc_current_block ()->name);
1212 new_st.op = EXEC_IF;
1217 gfc_free_expr (expr);
1222 /* Free a gfc_iterator structure. */
1225 gfc_free_iterator (gfc_iterator * iter, int flag)
1231 gfc_free_expr (iter->var);
1232 gfc_free_expr (iter->start);
1233 gfc_free_expr (iter->end);
1234 gfc_free_expr (iter->step);
1241 /* Match a DO statement. */
1246 gfc_iterator iter, *ip;
1248 gfc_st_label *label;
1251 old_loc = gfc_current_locus;
1254 iter.var = iter.start = iter.end = iter.step = NULL;
1256 m = gfc_match_label ();
1257 if (m == MATCH_ERROR)
1260 if (gfc_match (" do") != MATCH_YES)
1263 m = gfc_match_st_label (&label);
1264 if (m == MATCH_ERROR)
1267 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1269 if (gfc_match_eos () == MATCH_YES)
1271 iter.end = gfc_logical_expr (1, NULL);
1272 new_st.op = EXEC_DO_WHILE;
1276 /* match an optional comma, if no comma is found a space is obligatory. */
1277 if (gfc_match_char(',') != MATCH_YES
1278 && gfc_match ("% ") != MATCH_YES)
1281 /* See if we have a DO WHILE. */
1282 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1284 new_st.op = EXEC_DO_WHILE;
1288 /* The abortive DO WHILE may have done something to the symbol
1289 table, so we start over: */
1290 gfc_undo_symbols ();
1291 gfc_current_locus = old_loc;
1293 gfc_match_label (); /* This won't error */
1294 gfc_match (" do "); /* This will work */
1296 gfc_match_st_label (&label); /* Can't error out */
1297 gfc_match_char (','); /* Optional comma */
1299 m = gfc_match_iterator (&iter, 0);
1302 if (m == MATCH_ERROR)
1305 gfc_check_do_variable (iter.var->symtree);
1307 if (gfc_match_eos () != MATCH_YES)
1309 gfc_syntax_error (ST_DO);
1313 new_st.op = EXEC_DO;
1317 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1320 new_st.label = label;
1322 if (new_st.op == EXEC_DO_WHILE)
1323 new_st.expr = iter.end;
1326 new_st.ext.iterator = ip = gfc_get_iterator ();
1333 gfc_free_iterator (&iter, 0);
1339 /* Match an EXIT or CYCLE statement. */
1342 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1344 gfc_state_data *p, *o;
1348 if (gfc_match_eos () == MATCH_YES)
1352 m = gfc_match ("% %s%t", &sym);
1353 if (m == MATCH_ERROR)
1357 gfc_syntax_error (st);
1361 if (sym->attr.flavor != FL_LABEL)
1363 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1364 sym->name, gfc_ascii_statement (st));
1369 /* Find the loop mentioned specified by the label (or lack of a
1371 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1372 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1374 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1380 gfc_error ("%s statement at %C is not within a loop",
1381 gfc_ascii_statement (st));
1383 gfc_error ("%s statement at %C is not within loop '%s'",
1384 gfc_ascii_statement (st), sym->name);
1391 gfc_error ("%s statement at %C leaving OpenMP structured block",
1392 gfc_ascii_statement (st));
1395 else if (st == ST_EXIT
1396 && p->previous != NULL
1397 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1398 && (p->previous->head->op == EXEC_OMP_DO
1399 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1401 gcc_assert (p->previous->head->next != NULL);
1402 gcc_assert (p->previous->head->next->op == EXEC_DO
1403 || p->previous->head->next->op == EXEC_DO_WHILE);
1404 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1408 /* Save the first statement in the loop - needed by the backend. */
1409 new_st.ext.whichloop = p->head;
1412 /* new_st.sym = sym;*/
1418 /* Match the EXIT statement. */
1421 gfc_match_exit (void)
1424 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1428 /* Match the CYCLE statement. */
1431 gfc_match_cycle (void)
1434 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1438 /* Match a number or character constant after a STOP or PAUSE statement. */
1441 gfc_match_stopcode (gfc_statement st)
1451 if (gfc_match_eos () != MATCH_YES)
1453 m = gfc_match_small_literal_int (&stop_code, &cnt);
1454 if (m == MATCH_ERROR)
1457 if (m == MATCH_YES && cnt > 5)
1459 gfc_error ("Too many digits in STOP code at %C");
1465 /* Try a character constant. */
1466 m = gfc_match_expr (&e);
1467 if (m == MATCH_ERROR)
1471 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1475 if (gfc_match_eos () != MATCH_YES)
1479 if (gfc_pure (NULL))
1481 gfc_error ("%s statement not allowed in PURE procedure at %C",
1482 gfc_ascii_statement (st));
1486 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1488 new_st.ext.stop_code = stop_code;
1493 gfc_syntax_error (st);
1501 /* Match the (deprecated) PAUSE statement. */
1504 gfc_match_pause (void)
1508 m = gfc_match_stopcode (ST_PAUSE);
1511 if (gfc_notify_std (GFC_STD_F95_DEL,
1512 "Obsolete: PAUSE statement at %C")
1520 /* Match the STOP statement. */
1523 gfc_match_stop (void)
1525 return gfc_match_stopcode (ST_STOP);
1529 /* Match a CONTINUE statement. */
1532 gfc_match_continue (void)
1535 if (gfc_match_eos () != MATCH_YES)
1537 gfc_syntax_error (ST_CONTINUE);
1541 new_st.op = EXEC_CONTINUE;
1546 /* Match the (deprecated) ASSIGN statement. */
1549 gfc_match_assign (void)
1552 gfc_st_label *label;
1554 if (gfc_match (" %l", &label) == MATCH_YES)
1556 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1558 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1560 if (gfc_notify_std (GFC_STD_F95_DEL,
1561 "Obsolete: ASSIGN statement at %C")
1565 expr->symtree->n.sym->attr.assign = 1;
1567 new_st.op = EXEC_LABEL_ASSIGN;
1568 new_st.label = label;
1577 /* Match the GO TO statement. As a computed GOTO statement is
1578 matched, it is transformed into an equivalent SELECT block. No
1579 tree is necessary, and the resulting jumps-to-jumps are
1580 specifically optimized away by the back end. */
1583 gfc_match_goto (void)
1585 gfc_code *head, *tail;
1588 gfc_st_label *label;
1592 if (gfc_match (" %l%t", &label) == MATCH_YES)
1594 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1597 new_st.op = EXEC_GOTO;
1598 new_st.label = label;
1602 /* The assigned GO TO statement. */
1604 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1606 if (gfc_notify_std (GFC_STD_F95_DEL,
1607 "Obsolete: Assigned GOTO statement at %C")
1611 new_st.op = EXEC_GOTO;
1614 if (gfc_match_eos () == MATCH_YES)
1617 /* Match label list. */
1618 gfc_match_char (',');
1619 if (gfc_match_char ('(') != MATCH_YES)
1621 gfc_syntax_error (ST_GOTO);
1628 m = gfc_match_st_label (&label);
1632 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1636 head = tail = gfc_get_code ();
1639 tail->block = gfc_get_code ();
1643 tail->label = label;
1644 tail->op = EXEC_GOTO;
1646 while (gfc_match_char (',') == MATCH_YES);
1648 if (gfc_match (")%t") != MATCH_YES)
1654 "Statement label list in GOTO at %C cannot be empty");
1657 new_st.block = head;
1662 /* Last chance is a computed GO TO statement. */
1663 if (gfc_match_char ('(') != MATCH_YES)
1665 gfc_syntax_error (ST_GOTO);
1674 m = gfc_match_st_label (&label);
1678 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1682 head = tail = gfc_get_code ();
1685 tail->block = gfc_get_code ();
1689 cp = gfc_get_case ();
1690 cp->low = cp->high = gfc_int_expr (i++);
1692 tail->op = EXEC_SELECT;
1693 tail->ext.case_list = cp;
1695 tail->next = gfc_get_code ();
1696 tail->next->op = EXEC_GOTO;
1697 tail->next->label = label;
1699 while (gfc_match_char (',') == MATCH_YES);
1701 if (gfc_match_char (')') != MATCH_YES)
1706 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1710 /* Get the rest of the statement. */
1711 gfc_match_char (',');
1713 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1716 /* At this point, a computed GOTO has been fully matched and an
1717 equivalent SELECT statement constructed. */
1719 new_st.op = EXEC_SELECT;
1722 /* Hack: For a "real" SELECT, the expression is in expr. We put
1723 it in expr2 so we can distinguish then and produce the correct
1725 new_st.expr2 = expr;
1726 new_st.block = head;
1730 gfc_syntax_error (ST_GOTO);
1732 gfc_free_statements (head);
1737 /* Frees a list of gfc_alloc structures. */
1740 gfc_free_alloc_list (gfc_alloc * p)
1747 gfc_free_expr (p->expr);
1753 /* Match an ALLOCATE statement. */
1756 gfc_match_allocate (void)
1758 gfc_alloc *head, *tail;
1765 if (gfc_match_char ('(') != MATCH_YES)
1771 head = tail = gfc_get_alloc ();
1774 tail->next = gfc_get_alloc ();
1778 m = gfc_match_variable (&tail->expr, 0);
1781 if (m == MATCH_ERROR)
1784 if (gfc_check_do_variable (tail->expr->symtree))
1788 && gfc_impure_variable (tail->expr->symtree->n.sym))
1790 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1795 if (gfc_match_char (',') != MATCH_YES)
1798 m = gfc_match (" stat = %v", &stat);
1799 if (m == MATCH_ERROR)
1807 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1810 ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1811 "INTENT(IN)", stat->symtree->n.sym->name);
1815 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1818 ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1823 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1825 gfc_error("STAT expression at %C must be a variable");
1829 gfc_check_do_variable(stat->symtree);
1832 if (gfc_match (" )%t") != MATCH_YES)
1835 new_st.op = EXEC_ALLOCATE;
1837 new_st.ext.alloc_list = head;
1842 gfc_syntax_error (ST_ALLOCATE);
1845 gfc_free_expr (stat);
1846 gfc_free_alloc_list (head);
1851 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1852 a set of pointer assignments to intrinsic NULL(). */
1855 gfc_match_nullify (void)
1863 if (gfc_match_char ('(') != MATCH_YES)
1868 m = gfc_match_variable (&p, 0);
1869 if (m == MATCH_ERROR)
1874 if (gfc_check_do_variable(p->symtree))
1877 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1880 ("Illegal variable in NULLIFY at %C for a PURE procedure");
1884 /* build ' => NULL() ' */
1885 e = gfc_get_expr ();
1886 e->where = gfc_current_locus;
1887 e->expr_type = EXPR_NULL;
1888 e->ts.type = BT_UNKNOWN;
1895 tail->next = gfc_get_code ();
1899 tail->op = EXEC_POINTER_ASSIGN;
1903 if (gfc_match (" )%t") == MATCH_YES)
1905 if (gfc_match_char (',') != MATCH_YES)
1912 gfc_syntax_error (ST_NULLIFY);
1915 gfc_free_statements (new_st.next);
1920 /* Match a DEALLOCATE statement. */
1923 gfc_match_deallocate (void)
1925 gfc_alloc *head, *tail;
1932 if (gfc_match_char ('(') != MATCH_YES)
1938 head = tail = gfc_get_alloc ();
1941 tail->next = gfc_get_alloc ();
1945 m = gfc_match_variable (&tail->expr, 0);
1946 if (m == MATCH_ERROR)
1951 if (gfc_check_do_variable (tail->expr->symtree))
1955 && gfc_impure_variable (tail->expr->symtree->n.sym))
1958 ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1963 if (gfc_match_char (',') != MATCH_YES)
1966 m = gfc_match (" stat = %v", &stat);
1967 if (m == MATCH_ERROR)
1975 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1977 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1978 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
1982 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
1984 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
1985 "for a PURE procedure");
1989 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1991 gfc_error("STAT expression at %C must be a variable");
1995 gfc_check_do_variable(stat->symtree);
1998 if (gfc_match (" )%t") != MATCH_YES)
2001 new_st.op = EXEC_DEALLOCATE;
2003 new_st.ext.alloc_list = head;
2008 gfc_syntax_error (ST_DEALLOCATE);
2011 gfc_free_expr (stat);
2012 gfc_free_alloc_list (head);
2017 /* Match a RETURN statement. */
2020 gfc_match_return (void)
2024 gfc_compile_state s;
2028 if (gfc_match_eos () == MATCH_YES)
2031 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2033 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2038 if (gfc_current_form == FORM_FREE)
2040 /* The following are valid, so we can't require a blank after the
2044 c = gfc_peek_char ();
2045 if (ISALPHA (c) || ISDIGIT (c))
2049 m = gfc_match (" %e%t", &e);
2052 if (m == MATCH_ERROR)
2055 gfc_syntax_error (ST_RETURN);
2062 gfc_enclosing_unit (&s);
2063 if (s == COMP_PROGRAM
2064 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2065 "main program at %C") == FAILURE)
2068 new_st.op = EXEC_RETURN;
2075 /* Match a CALL statement. The tricky part here are possible
2076 alternate return specifiers. We handle these by having all
2077 "subroutines" actually return an integer via a register that gives
2078 the return number. If the call specifies alternate returns, we
2079 generate code for a SELECT statement whose case clauses contain
2080 GOTOs to the various labels. */
2083 gfc_match_call (void)
2085 char name[GFC_MAX_SYMBOL_LEN + 1];
2086 gfc_actual_arglist *a, *arglist;
2096 m = gfc_match ("% %n", name);
2102 if (gfc_get_ha_sym_tree (name, &st))
2106 gfc_set_sym_referenced (sym);
2108 if (!sym->attr.generic
2109 && !sym->attr.subroutine
2110 && gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2113 if (gfc_match_eos () != MATCH_YES)
2115 m = gfc_match_actual_arglist (1, &arglist);
2118 if (m == MATCH_ERROR)
2121 if (gfc_match_eos () != MATCH_YES)
2125 /* If any alternate return labels were found, construct a SELECT
2126 statement that will jump to the right place. */
2129 for (a = arglist; a; a = a->next)
2130 if (a->expr == NULL)
2135 gfc_symtree *select_st;
2136 gfc_symbol *select_sym;
2137 char name[GFC_MAX_SYMBOL_LEN + 1];
2139 new_st.next = c = gfc_get_code ();
2140 c->op = EXEC_SELECT;
2141 sprintf (name, "_result_%s",sym->name);
2142 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail */
2144 select_sym = select_st->n.sym;
2145 select_sym->ts.type = BT_INTEGER;
2146 select_sym->ts.kind = gfc_default_integer_kind;
2147 gfc_set_sym_referenced (select_sym);
2148 c->expr = gfc_get_expr ();
2149 c->expr->expr_type = EXPR_VARIABLE;
2150 c->expr->symtree = select_st;
2151 c->expr->ts = select_sym->ts;
2152 c->expr->where = gfc_current_locus;
2155 for (a = arglist; a; a = a->next)
2157 if (a->expr != NULL)
2160 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2165 c->block = gfc_get_code ();
2167 c->op = EXEC_SELECT;
2169 new_case = gfc_get_case ();
2170 new_case->high = new_case->low = gfc_int_expr (i);
2171 c->ext.case_list = new_case;
2173 c->next = gfc_get_code ();
2174 c->next->op = EXEC_GOTO;
2175 c->next->label = a->label;
2179 new_st.op = EXEC_CALL;
2180 new_st.symtree = st;
2181 new_st.ext.actual = arglist;
2186 gfc_syntax_error (ST_CALL);
2189 gfc_free_actual_arglist (arglist);
2194 /* Given a name, return a pointer to the common head structure,
2195 creating it if it does not exist. If FROM_MODULE is nonzero, we
2196 mangle the name so that it doesn't interfere with commons defined
2197 in the using namespace.
2198 TODO: Add to global symbol tree. */
2201 gfc_get_common (const char *name, int from_module)
2204 static int serial = 0;
2205 char mangled_name[GFC_MAX_SYMBOL_LEN+1];
2209 /* A use associated common block is only needed to correctly layout
2210 the variables it contains. */
2211 snprintf(mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2212 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2216 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2219 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2222 if (st->n.common == NULL)
2224 st->n.common = gfc_get_common_head ();
2225 st->n.common->where = gfc_current_locus;
2226 strcpy (st->n.common->name, name);
2229 return st->n.common;
2233 /* Match a common block name. */
2236 match_common_name (char *name)
2240 if (gfc_match_char ('/') == MATCH_NO)
2246 if (gfc_match_char ('/') == MATCH_YES)
2252 m = gfc_match_name (name);
2254 if (m == MATCH_ERROR)
2256 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2259 gfc_error ("Syntax error in common block name at %C");
2264 /* Match a COMMON statement. */
2267 gfc_match_common (void)
2269 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2270 char name[GFC_MAX_SYMBOL_LEN+1];
2273 gfc_equiv * e1, * e2;
2277 old_blank_common = gfc_current_ns->blank_common.head;
2278 if (old_blank_common)
2280 while (old_blank_common->common_next)
2281 old_blank_common = old_blank_common->common_next;
2288 m = match_common_name (name);
2289 if (m == MATCH_ERROR)
2292 gsym = gfc_get_gsymbol (name);
2293 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2295 gfc_error ("Symbol '%s' at %C is already an external symbol that is not COMMON",
2300 if (gsym->type == GSYM_UNKNOWN)
2302 gsym->type = GSYM_COMMON;
2303 gsym->where = gfc_current_locus;
2309 if (name[0] == '\0')
2311 t = &gfc_current_ns->blank_common;
2312 if (t->head == NULL)
2313 t->where = gfc_current_locus;
2318 t = gfc_get_common (name, 0);
2327 while (tail->common_next)
2328 tail = tail->common_next;
2331 /* Grab the list of symbols. */
2334 m = gfc_match_symbol (&sym, 0);
2335 if (m == MATCH_ERROR)
2340 if (sym->attr.in_common)
2342 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2347 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2350 if (sym->value != NULL
2351 && (name[0] == '\0' || !sym->attr.data))
2353 if (name[0] == '\0')
2354 gfc_error ("Previously initialized symbol '%s' in "
2355 "blank COMMON block at %C", sym->name);
2357 gfc_error ("Previously initialized symbol '%s' in "
2358 "COMMON block '%s' at %C", sym->name, name);
2362 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2365 /* Derived type names must have the SEQUENCE attribute. */
2366 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2369 ("Derived type variable in COMMON at %C does not have the "
2370 "SEQUENCE attribute");
2375 tail->common_next = sym;
2381 /* Deal with an optional array specification after the
2383 m = gfc_match_array_spec (&as);
2384 if (m == MATCH_ERROR)
2389 if (as->type != AS_EXPLICIT)
2392 ("Array specification for symbol '%s' in COMMON at %C "
2393 "must be explicit", sym->name);
2397 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2400 if (sym->attr.pointer)
2403 ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2413 sym->common_head = t;
2415 /* Check to see if the symbol is already in an equivalence group.
2416 If it is, set the other members as being in common. */
2417 if (sym->attr.in_equivalence)
2419 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2421 for (e2 = e1; e2; e2 = e2->eq)
2422 if (e2->expr->symtree->n.sym == sym)
2429 for (e2 = e1; e2; e2 = e2->eq)
2431 other = e2->expr->symtree->n.sym;
2432 if (other->common_head
2433 && other->common_head != sym->common_head)
2435 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2436 "%C is being indirectly equivalenced to "
2437 "another COMMON block '%s'",
2439 sym->common_head->name,
2440 other->common_head->name);
2443 other->attr.in_common = 1;
2444 other->common_head = t;
2450 gfc_gobble_whitespace ();
2451 if (gfc_match_eos () == MATCH_YES)
2453 if (gfc_peek_char () == '/')
2455 if (gfc_match_char (',') != MATCH_YES)
2457 gfc_gobble_whitespace ();
2458 if (gfc_peek_char () == '/')
2467 gfc_syntax_error (ST_COMMON);
2470 if (old_blank_common)
2471 old_blank_common->common_next = NULL;
2473 gfc_current_ns->blank_common.head = NULL;
2474 gfc_free_array_spec (as);
2479 /* Match a BLOCK DATA program unit. */
2482 gfc_match_block_data (void)
2484 char name[GFC_MAX_SYMBOL_LEN + 1];
2488 if (gfc_match_eos () == MATCH_YES)
2490 gfc_new_block = NULL;
2494 m = gfc_match ("% %n%t", name);
2498 if (gfc_get_symbol (name, NULL, &sym))
2501 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2504 gfc_new_block = sym;
2510 /* Free a namelist structure. */
2513 gfc_free_namelist (gfc_namelist * name)
2517 for (; name; name = n)
2525 /* Match a NAMELIST statement. */
2528 gfc_match_namelist (void)
2530 gfc_symbol *group_name, *sym;
2534 m = gfc_match (" / %s /", &group_name);
2537 if (m == MATCH_ERROR)
2542 if (group_name->ts.type != BT_UNKNOWN)
2545 ("Namelist group name '%s' at %C already has a basic type "
2546 "of %s", group_name->name, gfc_typename (&group_name->ts));
2550 if (group_name->attr.flavor == FL_NAMELIST
2551 && group_name->attr.use_assoc
2552 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2553 "at %C already is USE associated and can"
2554 "not be respecified.", group_name->name)
2558 if (group_name->attr.flavor != FL_NAMELIST
2559 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2560 group_name->name, NULL) == FAILURE)
2565 m = gfc_match_symbol (&sym, 1);
2568 if (m == MATCH_ERROR)
2571 if (sym->attr.in_namelist == 0
2572 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2575 /* Use gfc_error_check here, rather than goto error, so that this
2576 these are the only errors for the next two lines. */
2577 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2579 gfc_error ("Assumed size array '%s' in namelist '%s'at "
2580 "%C is not allowed.", sym->name, group_name->name);
2584 if (sym->as && sym->as->type == AS_ASSUMED_SHAPE
2585 && gfc_notify_std (GFC_STD_GNU, "Assumed shape array '%s' in "
2586 "namelist '%s' at %C is an extension.",
2587 sym->name, group_name->name) == FAILURE)
2590 nl = gfc_get_namelist ();
2593 if (group_name->namelist == NULL)
2594 group_name->namelist = group_name->namelist_tail = nl;
2597 group_name->namelist_tail->next = nl;
2598 group_name->namelist_tail = nl;
2601 if (gfc_match_eos () == MATCH_YES)
2604 m = gfc_match_char (',');
2606 if (gfc_match_char ('/') == MATCH_YES)
2608 m2 = gfc_match (" %s /", &group_name);
2609 if (m2 == MATCH_YES)
2611 if (m2 == MATCH_ERROR)
2625 gfc_syntax_error (ST_NAMELIST);
2632 /* Match a MODULE statement. */
2635 gfc_match_module (void)
2639 m = gfc_match (" %s%t", &gfc_new_block);
2643 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2644 gfc_new_block->name, NULL) == FAILURE)
2651 /* Free equivalence sets and lists. Recursively is the easiest way to
2655 gfc_free_equiv (gfc_equiv * eq)
2661 gfc_free_equiv (eq->eq);
2662 gfc_free_equiv (eq->next);
2664 gfc_free_expr (eq->expr);
2669 /* Match an EQUIVALENCE statement. */
2672 gfc_match_equivalence (void)
2674 gfc_equiv *eq, *set, *tail;
2678 gfc_common_head *common_head = NULL;
2686 eq = gfc_get_equiv ();
2690 eq->next = gfc_current_ns->equiv;
2691 gfc_current_ns->equiv = eq;
2693 if (gfc_match_char ('(') != MATCH_YES)
2697 common_flag = FALSE;
2702 m = gfc_match_equiv_variable (&set->expr);
2703 if (m == MATCH_ERROR)
2708 /* count the number of objects. */
2711 if (gfc_match_char ('%') == MATCH_YES)
2713 gfc_error ("Derived type component %C is not a "
2714 "permitted EQUIVALENCE member");
2718 for (ref = set->expr->ref; ref; ref = ref->next)
2719 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2722 ("Array reference in EQUIVALENCE at %C cannot be an "
2727 sym = set->expr->symtree->n.sym;
2729 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL)
2733 if (sym->attr.in_common)
2736 common_head = sym->common_head;
2739 if (gfc_match_char (')') == MATCH_YES)
2742 if (gfc_match_char (',') != MATCH_YES)
2745 set->eq = gfc_get_equiv ();
2751 gfc_error ("EQUIVALENCE at %C requires two or more objects");
2755 /* If one of the members of an equivalence is in common, then
2756 mark them all as being in common. Before doing this, check
2757 that members of the equivalence group are not in different
2760 for (set = eq; set; set = set->eq)
2762 sym = set->expr->symtree->n.sym;
2763 if (sym->common_head && sym->common_head != common_head)
2765 gfc_error ("Attempt to indirectly overlap COMMON "
2766 "blocks %s and %s by EQUIVALENCE at %C",
2767 sym->common_head->name,
2771 sym->attr.in_common = 1;
2772 sym->common_head = common_head;
2775 if (gfc_match_eos () == MATCH_YES)
2777 if (gfc_match_char (',') != MATCH_YES)
2784 gfc_syntax_error (ST_EQUIVALENCE);
2790 gfc_free_equiv (gfc_current_ns->equiv);
2791 gfc_current_ns->equiv = eq;
2796 /* Check that a statement function is not recursive. This is done by looking
2797 for the statement function symbol(sym) by looking recursively through its
2798 expression(e). If a reference to sym is found, true is returned. */
2800 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
2802 gfc_actual_arglist *arg;
2809 switch (e->expr_type)
2812 for (arg = e->value.function.actual; arg; arg = arg->next)
2814 if (sym->name == arg->name
2815 || recursive_stmt_fcn (arg->expr, sym))
2819 if (e->symtree == NULL)
2822 /* Check the name before testing for nested recursion! */
2823 if (sym->name == e->symtree->n.sym->name)
2826 /* Catch recursion via other statement functions. */
2827 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
2828 && e->symtree->n.sym->value
2829 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
2835 if (e->symtree && sym->name == e->symtree->n.sym->name)
2840 if (recursive_stmt_fcn (e->value.op.op1, sym)
2841 || recursive_stmt_fcn (e->value.op.op2, sym))
2849 /* Component references do not need to be checked. */
2852 for (ref = e->ref; ref; ref = ref->next)
2857 for (i = 0; i < ref->u.ar.dimen; i++)
2859 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
2860 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
2861 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
2867 if (recursive_stmt_fcn (ref->u.ss.start, sym)
2868 || recursive_stmt_fcn (ref->u.ss.end, sym))
2882 /* Match a statement function declaration. It is so easy to match
2883 non-statement function statements with a MATCH_ERROR as opposed to
2884 MATCH_NO that we suppress error message in most cases. */
2887 gfc_match_st_function (void)
2889 gfc_error_buf old_error;
2894 m = gfc_match_symbol (&sym, 0);
2898 gfc_push_error (&old_error);
2900 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
2901 sym->name, NULL) == FAILURE)
2904 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2907 m = gfc_match (" = %e%t", &expr);
2911 gfc_free_error (&old_error);
2912 if (m == MATCH_ERROR)
2915 if (recursive_stmt_fcn (expr, sym))
2917 gfc_error ("Statement function at %L is recursive",
2927 gfc_pop_error (&old_error);
2932 /***************** SELECT CASE subroutines ******************/
2934 /* Free a single case structure. */
2937 free_case (gfc_case * p)
2939 if (p->low == p->high)
2941 gfc_free_expr (p->low);
2942 gfc_free_expr (p->high);
2947 /* Free a list of case structures. */
2950 gfc_free_case_list (gfc_case * p)
2962 /* Match a single case selector. */
2965 match_case_selector (gfc_case ** cp)
2970 c = gfc_get_case ();
2971 c->where = gfc_current_locus;
2973 if (gfc_match_char (':') == MATCH_YES)
2975 m = gfc_match_init_expr (&c->high);
2978 if (m == MATCH_ERROR)
2984 m = gfc_match_init_expr (&c->low);
2985 if (m == MATCH_ERROR)
2990 /* If we're not looking at a ':' now, make a range out of a single
2991 target. Else get the upper bound for the case range. */
2992 if (gfc_match_char (':') != MATCH_YES)
2996 m = gfc_match_init_expr (&c->high);
2997 if (m == MATCH_ERROR)
2999 /* MATCH_NO is fine. It's OK if nothing is there! */
3007 gfc_error ("Expected initialization expression in CASE at %C");
3015 /* Match the end of a case statement. */
3018 match_case_eos (void)
3020 char name[GFC_MAX_SYMBOL_LEN + 1];
3023 if (gfc_match_eos () == MATCH_YES)
3026 gfc_gobble_whitespace ();
3028 m = gfc_match_name (name);
3032 if (strcmp (name, gfc_current_block ()->name) != 0)
3034 gfc_error ("Expected case name of '%s' at %C",
3035 gfc_current_block ()->name);
3039 return gfc_match_eos ();
3043 /* Match a SELECT statement. */
3046 gfc_match_select (void)
3051 m = gfc_match_label ();
3052 if (m == MATCH_ERROR)
3055 m = gfc_match (" select case ( %e )%t", &expr);
3059 new_st.op = EXEC_SELECT;
3066 /* Match a CASE statement. */
3069 gfc_match_case (void)
3071 gfc_case *c, *head, *tail;
3076 if (gfc_current_state () != COMP_SELECT)
3078 gfc_error ("Unexpected CASE statement at %C");
3082 if (gfc_match ("% default") == MATCH_YES)
3084 m = match_case_eos ();
3087 if (m == MATCH_ERROR)
3090 new_st.op = EXEC_SELECT;
3091 c = gfc_get_case ();
3092 c->where = gfc_current_locus;
3093 new_st.ext.case_list = c;
3097 if (gfc_match_char ('(') != MATCH_YES)
3102 if (match_case_selector (&c) == MATCH_ERROR)
3112 if (gfc_match_char (')') == MATCH_YES)
3114 if (gfc_match_char (',') != MATCH_YES)
3118 m = match_case_eos ();
3121 if (m == MATCH_ERROR)
3124 new_st.op = EXEC_SELECT;
3125 new_st.ext.case_list = head;
3130 gfc_error ("Syntax error in CASE-specification at %C");
3133 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3137 /********************* WHERE subroutines ********************/
3139 /* Match the rest of a simple WHERE statement that follows an IF statement.
3143 match_simple_where (void)
3149 m = gfc_match (" ( %e )", &expr);
3153 m = gfc_match_assignment ();
3156 if (m == MATCH_ERROR)
3159 if (gfc_match_eos () != MATCH_YES)
3162 c = gfc_get_code ();
3166 c->next = gfc_get_code ();
3169 gfc_clear_new_st ();
3171 new_st.op = EXEC_WHERE;
3177 gfc_syntax_error (ST_WHERE);
3180 gfc_free_expr (expr);
3184 /* Match a WHERE statement. */
3187 gfc_match_where (gfc_statement * st)
3193 m0 = gfc_match_label ();
3194 if (m0 == MATCH_ERROR)
3197 m = gfc_match (" where ( %e )", &expr);
3201 if (gfc_match_eos () == MATCH_YES)
3203 *st = ST_WHERE_BLOCK;
3205 new_st.op = EXEC_WHERE;
3210 m = gfc_match_assignment ();
3212 gfc_syntax_error (ST_WHERE);
3216 gfc_free_expr (expr);
3220 /* We've got a simple WHERE statement. */
3222 c = gfc_get_code ();
3226 c->next = gfc_get_code ();
3229 gfc_clear_new_st ();
3231 new_st.op = EXEC_WHERE;
3238 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3239 new_st if successful. */
3242 gfc_match_elsewhere (void)
3244 char name[GFC_MAX_SYMBOL_LEN + 1];
3248 if (gfc_current_state () != COMP_WHERE)
3250 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3256 if (gfc_match_char ('(') == MATCH_YES)
3258 m = gfc_match_expr (&expr);
3261 if (m == MATCH_ERROR)
3264 if (gfc_match_char (')') != MATCH_YES)
3268 if (gfc_match_eos () != MATCH_YES)
3269 { /* Better be a name at this point */
3270 m = gfc_match_name (name);
3273 if (m == MATCH_ERROR)
3276 if (gfc_match_eos () != MATCH_YES)
3279 if (strcmp (name, gfc_current_block ()->name) != 0)
3281 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3282 name, gfc_current_block ()->name);
3287 new_st.op = EXEC_WHERE;
3292 gfc_syntax_error (ST_ELSEWHERE);
3295 gfc_free_expr (expr);
3300 /******************** FORALL subroutines ********************/
3302 /* Free a list of FORALL iterators. */
3305 gfc_free_forall_iterator (gfc_forall_iterator * iter)
3307 gfc_forall_iterator *next;
3313 gfc_free_expr (iter->var);
3314 gfc_free_expr (iter->start);
3315 gfc_free_expr (iter->end);
3316 gfc_free_expr (iter->stride);
3324 /* Match an iterator as part of a FORALL statement. The format is:
3326 <var> = <start>:<end>[:<stride>][, <scalar mask>] */
3329 match_forall_iterator (gfc_forall_iterator ** result)
3331 gfc_forall_iterator *iter;
3335 where = gfc_current_locus;
3336 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3338 m = gfc_match_variable (&iter->var, 0);
3342 if (gfc_match_char ('=') != MATCH_YES)
3348 m = gfc_match_expr (&iter->start);
3352 if (gfc_match_char (':') != MATCH_YES)
3355 m = gfc_match_expr (&iter->end);
3358 if (m == MATCH_ERROR)
3361 if (gfc_match_char (':') == MATCH_NO)
3362 iter->stride = gfc_int_expr (1);
3365 m = gfc_match_expr (&iter->stride);
3368 if (m == MATCH_ERROR)
3376 gfc_error ("Syntax error in FORALL iterator at %C");
3380 gfc_current_locus = where;
3381 gfc_free_forall_iterator (iter);
3386 /* Match the header of a FORALL statement. */
3389 match_forall_header (gfc_forall_iterator ** phead, gfc_expr ** mask)
3391 gfc_forall_iterator *head, *tail, *new;
3395 gfc_gobble_whitespace ();
3400 if (gfc_match_char ('(') != MATCH_YES)
3403 m = match_forall_iterator (&new);
3404 if (m == MATCH_ERROR)
3413 if (gfc_match_char (',') != MATCH_YES)
3416 m = match_forall_iterator (&new);
3417 if (m == MATCH_ERROR)
3427 /* Have to have a mask expression */
3429 m = gfc_match_expr (&msk);
3432 if (m == MATCH_ERROR)
3438 if (gfc_match_char (')') == MATCH_NO)
3446 gfc_syntax_error (ST_FORALL);
3449 gfc_free_expr (msk);
3450 gfc_free_forall_iterator (head);
3455 /* Match the rest of a simple FORALL statement that follows an IF statement.
3459 match_simple_forall (void)
3461 gfc_forall_iterator *head;
3470 m = match_forall_header (&head, &mask);
3477 m = gfc_match_assignment ();
3479 if (m == MATCH_ERROR)
3483 m = gfc_match_pointer_assignment ();
3484 if (m == MATCH_ERROR)
3490 c = gfc_get_code ();
3492 c->loc = gfc_current_locus;
3494 if (gfc_match_eos () != MATCH_YES)
3497 gfc_clear_new_st ();
3498 new_st.op = EXEC_FORALL;
3500 new_st.ext.forall_iterator = head;
3501 new_st.block = gfc_get_code ();
3503 new_st.block->op = EXEC_FORALL;
3504 new_st.block->next = c;
3509 gfc_syntax_error (ST_FORALL);
3512 gfc_free_forall_iterator (head);
3513 gfc_free_expr (mask);
3519 /* Match a FORALL statement. */
3522 gfc_match_forall (gfc_statement * st)
3524 gfc_forall_iterator *head;
3533 m0 = gfc_match_label ();
3534 if (m0 == MATCH_ERROR)
3537 m = gfc_match (" forall");
3541 m = match_forall_header (&head, &mask);
3542 if (m == MATCH_ERROR)
3547 if (gfc_match_eos () == MATCH_YES)
3549 *st = ST_FORALL_BLOCK;
3551 new_st.op = EXEC_FORALL;
3553 new_st.ext.forall_iterator = head;
3558 m = gfc_match_assignment ();
3559 if (m == MATCH_ERROR)
3563 m = gfc_match_pointer_assignment ();
3564 if (m == MATCH_ERROR)
3570 c = gfc_get_code ();
3573 if (gfc_match_eos () != MATCH_YES)
3576 gfc_clear_new_st ();
3577 new_st.op = EXEC_FORALL;
3579 new_st.ext.forall_iterator = head;
3580 new_st.block = gfc_get_code ();
3582 new_st.block->op = EXEC_FORALL;
3583 new_st.block->next = c;
3589 gfc_syntax_error (ST_FORALL);
3592 gfc_free_forall_iterator (head);
3593 gfc_free_expr (mask);
3594 gfc_free_statements (c);