1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
31 /* For matching and debugging purposes. Order matters here! The
32 unary operators /must/ precede the binary plus and minus, or
33 the expression parser breaks. */
35 mstring intrinsic_operators[] = {
36 minit ("+", INTRINSIC_UPLUS),
37 minit ("-", INTRINSIC_UMINUS),
38 minit ("+", INTRINSIC_PLUS),
39 minit ("-", INTRINSIC_MINUS),
40 minit ("**", INTRINSIC_POWER),
41 minit ("//", INTRINSIC_CONCAT),
42 minit ("*", INTRINSIC_TIMES),
43 minit ("/", INTRINSIC_DIVIDE),
44 minit (".and.", INTRINSIC_AND),
45 minit (".or.", INTRINSIC_OR),
46 minit (".eqv.", INTRINSIC_EQV),
47 minit (".neqv.", INTRINSIC_NEQV),
48 minit (".eq.", INTRINSIC_EQ),
49 minit ("==", INTRINSIC_EQ),
50 minit (".ne.", INTRINSIC_NE),
51 minit ("/=", INTRINSIC_NE),
52 minit (".ge.", INTRINSIC_GE),
53 minit (">=", INTRINSIC_GE),
54 minit (".le.", INTRINSIC_LE),
55 minit ("<=", INTRINSIC_LE),
56 minit (".lt.", INTRINSIC_LT),
57 minit ("<", INTRINSIC_LT),
58 minit (".gt.", INTRINSIC_GT),
59 minit (">", INTRINSIC_GT),
60 minit (".not.", INTRINSIC_NOT),
61 minit (NULL, INTRINSIC_NONE)
65 /******************** Generic matching subroutines ************************/
67 /* In free form, match at least one space. Always matches in fixed
71 gfc_match_space (void)
76 if (gfc_current_form == FORM_FIXED)
79 old_loc = gfc_current_locus;
82 if (!gfc_is_whitespace (c))
84 gfc_current_locus = old_loc;
88 gfc_gobble_whitespace ();
94 /* Match an end of statement. End of statement is optional
95 whitespace, followed by a ';' or '\n' or comment '!'. If a
96 semicolon is found, we continue to eat whitespace and semicolons. */
108 old_loc = gfc_current_locus;
109 gfc_gobble_whitespace ();
111 c = gfc_next_char ();
117 c = gfc_next_char ();
134 gfc_current_locus = old_loc;
135 return (flag) ? MATCH_YES : MATCH_NO;
139 /* Match a literal integer on the input, setting the value on
140 MATCH_YES. Literal ints occur in kind-parameters as well as
141 old-style character length specifications. */
144 gfc_match_small_literal_int (int *value)
150 old_loc = gfc_current_locus;
152 gfc_gobble_whitespace ();
153 c = gfc_next_char ();
157 gfc_current_locus = old_loc;
165 old_loc = gfc_current_locus;
166 c = gfc_next_char ();
171 i = 10 * i + c - '0';
175 gfc_error ("Integer too large at %C");
180 gfc_current_locus = old_loc;
187 /* Match a small, constant integer expression, like in a kind
188 statement. On MATCH_YES, 'value' is set. */
191 gfc_match_small_int (int *value)
198 m = gfc_match_expr (&expr);
202 p = gfc_extract_int (expr, &i);
203 gfc_free_expr (expr);
216 /* Matches a statement label. Uses gfc_match_small_literal_int() to
217 do most of the work. */
220 gfc_match_st_label (gfc_st_label ** label, int allow_zero)
226 old_loc = gfc_current_locus;
228 m = gfc_match_small_literal_int (&i);
232 if (((i == 0) && allow_zero) || i <= 99999)
234 *label = gfc_get_st_label (i);
238 gfc_error ("Statement label at %C is out of range");
239 gfc_current_locus = old_loc;
244 /* Match and validate a label associated with a named IF, DO or SELECT
245 statement. If the symbol does not have the label attribute, we add
246 it. We also make sure the symbol does not refer to another
247 (active) block. A matched label is pointed to by gfc_new_block. */
250 gfc_match_label (void)
252 char name[GFC_MAX_SYMBOL_LEN + 1];
256 gfc_new_block = NULL;
258 m = gfc_match (" %n :", name);
262 if (gfc_get_symbol (name, NULL, &gfc_new_block))
264 gfc_error ("Label name '%s' at %C is ambiguous", name);
268 if (gfc_new_block->attr.flavor != FL_LABEL
269 && gfc_add_flavor (&gfc_new_block->attr, FL_LABEL, NULL) == FAILURE)
272 for (p = gfc_state_stack; p; p = p->previous)
273 if (p->sym == gfc_new_block)
275 gfc_error ("Label %s at %C already in use by a parent block",
276 gfc_new_block->name);
284 /* Try and match the input against an array of possibilities. If one
285 potential matching string is a substring of another, the longest
286 match takes precedence. Spaces in the target strings are optional
287 spaces that do not necessarily have to be found in the input
288 stream. In fixed mode, spaces never appear. If whitespace is
289 matched, it matches unlimited whitespace in the input. For this
290 reason, the 'mp' member of the mstring structure is used to track
291 the progress of each potential match.
293 If there is no match we return the tag associated with the
294 terminating NULL mstring structure and leave the locus pointer
295 where it started. If there is a match we return the tag member of
296 the matched mstring and leave the locus pointer after the matched
299 A '%' character is a mandatory space. */
302 gfc_match_strings (mstring * a)
304 mstring *p, *best_match;
305 int no_match, c, possibles;
310 for (p = a; p->string != NULL; p++)
319 match_loc = gfc_current_locus;
321 gfc_gobble_whitespace ();
323 while (possibles > 0)
325 c = gfc_next_char ();
327 /* Apply the next character to the current possibilities. */
328 for (p = a; p->string != NULL; p++)
335 /* Space matches 1+ whitespace(s). */
336 if ((gfc_current_form == FORM_FREE)
337 && gfc_is_whitespace (c))
355 match_loc = gfc_current_locus;
363 gfc_current_locus = match_loc;
365 return (best_match == NULL) ? no_match : best_match->tag;
369 /* See if the current input looks like a name of some sort. Modifies
370 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long. */
373 gfc_match_name (char *buffer)
378 old_loc = gfc_current_locus;
379 gfc_gobble_whitespace ();
381 c = gfc_next_char ();
384 gfc_current_locus = old_loc;
394 if (i > gfc_option.max_identifier_length)
396 gfc_error ("Name at %C is too long");
400 old_loc = gfc_current_locus;
401 c = gfc_next_char ();
405 || (gfc_option.flag_dollar_ok && c == '$'));
408 gfc_current_locus = old_loc;
414 /* Match a symbol on the input. Modifies the pointer to the symbol
415 pointer if successful. */
418 gfc_match_sym_tree (gfc_symtree ** matched_symbol, int host_assoc)
420 char buffer[GFC_MAX_SYMBOL_LEN + 1];
423 m = gfc_match_name (buffer);
428 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
429 ? MATCH_ERROR : MATCH_YES;
431 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
439 gfc_match_symbol (gfc_symbol ** matched_symbol, int host_assoc)
444 m = gfc_match_sym_tree (&st, host_assoc);
449 *matched_symbol = st->n.sym;
451 *matched_symbol = NULL;
456 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
457 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
461 gfc_match_intrinsic_op (gfc_intrinsic_op * result)
465 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
467 if (op == INTRINSIC_NONE)
475 /* Match a loop control phrase:
477 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
479 If the final integer expression is not present, a constant unity
480 expression is returned. We don't return MATCH_ERROR until after
481 the equals sign is seen. */
484 gfc_match_iterator (gfc_iterator * iter, int init_flag)
486 char name[GFC_MAX_SYMBOL_LEN + 1];
487 gfc_expr *var, *e1, *e2, *e3;
491 /* Match the start of an iterator without affecting the symbol
494 start = gfc_current_locus;
495 m = gfc_match (" %n =", name);
496 gfc_current_locus = start;
501 m = gfc_match_variable (&var, 0);
505 gfc_match_char ('=');
509 if (var->ref != NULL)
511 gfc_error ("Loop variable at %C cannot be a sub-component");
515 if (var->symtree->n.sym->attr.intent == INTENT_IN)
517 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
518 var->symtree->n.sym->name);
522 if (var->symtree->n.sym->attr.pointer)
524 gfc_error ("Loop variable at %C cannot have the POINTER attribute");
528 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
531 if (m == MATCH_ERROR)
534 if (gfc_match_char (',') != MATCH_YES)
537 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
540 if (m == MATCH_ERROR)
543 if (gfc_match_char (',') != MATCH_YES)
545 e3 = gfc_int_expr (1);
549 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
550 if (m == MATCH_ERROR)
554 gfc_error ("Expected a step value in iterator at %C");
566 gfc_error ("Syntax error in iterator at %C");
577 /* Tries to match the next non-whitespace character on the input.
578 This subroutine does not return MATCH_ERROR. */
581 gfc_match_char (char c)
585 where = gfc_current_locus;
586 gfc_gobble_whitespace ();
588 if (gfc_next_char () == c)
591 gfc_current_locus = where;
596 /* General purpose matching subroutine. The target string is a
597 scanf-like format string in which spaces correspond to arbitrary
598 whitespace (including no whitespace), characters correspond to
599 themselves. The %-codes are:
601 %% Literal percent sign
602 %e Expression, pointer to a pointer is set
603 %s Symbol, pointer to the symbol is set
604 %n Name, character buffer is set to name
605 %t Matches end of statement.
606 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
607 %l Matches a statement label
608 %v Matches a variable expression (an lvalue)
609 % Matches a required space (in free form) and optional spaces. */
612 gfc_match (const char *target, ...)
614 gfc_st_label **label;
623 old_loc = gfc_current_locus;
624 va_start (argp, target);
634 gfc_gobble_whitespace ();
645 vp = va_arg (argp, void **);
646 n = gfc_match_expr ((gfc_expr **) vp);
657 vp = va_arg (argp, void **);
658 n = gfc_match_variable ((gfc_expr **) vp, 0);
669 vp = va_arg (argp, void **);
670 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
681 np = va_arg (argp, char *);
682 n = gfc_match_name (np);
693 label = va_arg (argp, gfc_st_label **);
694 n = gfc_match_st_label (label, 0);
705 ip = va_arg (argp, int *);
706 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
717 if (gfc_match_eos () != MATCH_YES)
725 if (gfc_match_space () == MATCH_YES)
731 break; /* Fall through to character matcher */
734 gfc_internal_error ("gfc_match(): Bad match code %c", c);
738 if (c == gfc_next_char ())
748 /* Clean up after a failed match. */
749 gfc_current_locus = old_loc;
750 va_start (argp, target);
753 for (; matches > 0; matches--)
763 /* Matches that don't have to be undone */
768 (void)va_arg (argp, void **);
773 vp = va_arg (argp, void **);
787 /*********************** Statement level matching **********************/
789 /* Matches the start of a program unit, which is the program keyword
790 followed by an obligatory symbol. */
793 gfc_match_program (void)
798 m = gfc_match ("% %s%t", &sym);
802 gfc_error ("Invalid form of PROGRAM statement at %C");
806 if (m == MATCH_ERROR)
809 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, NULL) == FAILURE)
818 /* Match a simple assignment statement. */
821 gfc_match_assignment (void)
823 gfc_expr *lvalue, *rvalue;
827 old_loc = gfc_current_locus;
829 lvalue = rvalue = NULL;
830 m = gfc_match (" %v =", &lvalue);
834 if (lvalue->symtree->n.sym->attr.flavor == FL_PARAMETER)
836 gfc_error ("Cannot assign to a PARAMETER variable at %C");
841 m = gfc_match (" %e%t", &rvalue);
845 gfc_set_sym_referenced (lvalue->symtree->n.sym);
847 new_st.op = EXEC_ASSIGN;
848 new_st.expr = lvalue;
849 new_st.expr2 = rvalue;
851 gfc_check_do_variable (lvalue->symtree);
856 gfc_current_locus = old_loc;
857 gfc_free_expr (lvalue);
858 gfc_free_expr (rvalue);
863 /* Match a pointer assignment statement. */
866 gfc_match_pointer_assignment (void)
868 gfc_expr *lvalue, *rvalue;
872 old_loc = gfc_current_locus;
874 lvalue = rvalue = NULL;
876 m = gfc_match (" %v =>", &lvalue);
883 m = gfc_match (" %e%t", &rvalue);
887 new_st.op = EXEC_POINTER_ASSIGN;
888 new_st.expr = lvalue;
889 new_st.expr2 = rvalue;
894 gfc_current_locus = old_loc;
895 gfc_free_expr (lvalue);
896 gfc_free_expr (rvalue);
901 /* The IF statement is a bit of a pain. First of all, there are three
902 forms of it, the simple IF, the IF that starts a block and the
905 There is a problem with the simple IF and that is the fact that we
906 only have a single level of undo information on symbols. What this
907 means is for a simple IF, we must re-match the whole IF statement
908 multiple times in order to guarantee that the symbol table ends up
909 in the proper state. */
911 static match match_simple_forall (void);
912 static match match_simple_where (void);
915 gfc_match_if (gfc_statement * if_type)
918 gfc_st_label *l1, *l2, *l3;
923 n = gfc_match_label ();
924 if (n == MATCH_ERROR)
927 old_loc = gfc_current_locus;
929 m = gfc_match (" if ( %e", &expr);
933 if (gfc_match_char (')') != MATCH_YES)
935 gfc_error ("Syntax error in IF-expression at %C");
936 gfc_free_expr (expr);
940 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
947 ("Block label not appropriate for arithmetic IF statement "
950 gfc_free_expr (expr);
954 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
955 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
956 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
959 gfc_free_expr (expr);
963 new_st.op = EXEC_ARITHMETIC_IF;
969 *if_type = ST_ARITHMETIC_IF;
973 if (gfc_match (" then%t") == MATCH_YES)
978 *if_type = ST_IF_BLOCK;
984 gfc_error ("Block label is not appropriate IF statement at %C");
986 gfc_free_expr (expr);
990 /* At this point the only thing left is a simple IF statement. At
991 this point, n has to be MATCH_NO, so we don't have to worry about
992 re-matching a block label. From what we've got so far, try
993 matching an assignment. */
995 *if_type = ST_SIMPLE_IF;
997 m = gfc_match_assignment ();
1001 gfc_free_expr (expr);
1002 gfc_undo_symbols ();
1003 gfc_current_locus = old_loc;
1005 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1007 m = gfc_match_pointer_assignment ();
1011 gfc_free_expr (expr);
1012 gfc_undo_symbols ();
1013 gfc_current_locus = old_loc;
1015 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1017 /* Look at the next keyword to see which matcher to call. Matching
1018 the keyword doesn't affect the symbol table, so we don't have to
1019 restore between tries. */
1021 #define match(string, subr, statement) \
1022 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1026 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1027 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1028 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1029 match ("call", gfc_match_call, ST_CALL)
1030 match ("close", gfc_match_close, ST_CLOSE)
1031 match ("continue", gfc_match_continue, ST_CONTINUE)
1032 match ("cycle", gfc_match_cycle, ST_CYCLE)
1033 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1034 match ("end file", gfc_match_endfile, ST_END_FILE)
1035 match ("exit", gfc_match_exit, ST_EXIT)
1036 match ("forall", match_simple_forall, ST_FORALL)
1037 match ("go to", gfc_match_goto, ST_GOTO)
1038 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1039 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1040 match ("open", gfc_match_open, ST_OPEN)
1041 match ("pause", gfc_match_pause, ST_NONE)
1042 match ("print", gfc_match_print, ST_WRITE)
1043 match ("read", gfc_match_read, ST_READ)
1044 match ("return", gfc_match_return, ST_RETURN)
1045 match ("rewind", gfc_match_rewind, ST_REWIND)
1046 match ("stop", gfc_match_stop, ST_STOP)
1047 match ("where", match_simple_where, ST_WHERE)
1048 match ("write", gfc_match_write, ST_WRITE)
1050 /* All else has failed, so give up. See if any of the matchers has
1051 stored an error message of some sort. */
1052 if (gfc_error_check () == 0)
1053 gfc_error ("Unclassifiable statement in IF-clause at %C");
1055 gfc_free_expr (expr);
1060 gfc_error ("Syntax error in IF-clause at %C");
1063 gfc_free_expr (expr);
1067 /* At this point, we've matched the single IF and the action clause
1068 is in new_st. Rearrange things so that the IF statement appears
1071 p = gfc_get_code ();
1072 p->next = gfc_get_code ();
1074 p->next->loc = gfc_current_locus;
1079 gfc_clear_new_st ();
1081 new_st.op = EXEC_IF;
1090 /* Match an ELSE statement. */
1093 gfc_match_else (void)
1095 char name[GFC_MAX_SYMBOL_LEN + 1];
1097 if (gfc_match_eos () == MATCH_YES)
1100 if (gfc_match_name (name) != MATCH_YES
1101 || gfc_current_block () == NULL
1102 || gfc_match_eos () != MATCH_YES)
1104 gfc_error ("Unexpected junk after ELSE statement at %C");
1108 if (strcmp (name, gfc_current_block ()->name) != 0)
1110 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1111 name, gfc_current_block ()->name);
1119 /* Match an ELSE IF statement. */
1122 gfc_match_elseif (void)
1124 char name[GFC_MAX_SYMBOL_LEN + 1];
1128 m = gfc_match (" ( %e ) then", &expr);
1132 if (gfc_match_eos () == MATCH_YES)
1135 if (gfc_match_name (name) != MATCH_YES
1136 || gfc_current_block () == NULL
1137 || gfc_match_eos () != MATCH_YES)
1139 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1143 if (strcmp (name, gfc_current_block ()->name) != 0)
1145 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1146 name, gfc_current_block ()->name);
1151 new_st.op = EXEC_IF;
1156 gfc_free_expr (expr);
1161 /* Free a gfc_iterator structure. */
1164 gfc_free_iterator (gfc_iterator * iter, int flag)
1170 gfc_free_expr (iter->var);
1171 gfc_free_expr (iter->start);
1172 gfc_free_expr (iter->end);
1173 gfc_free_expr (iter->step);
1180 /* Match a DO statement. */
1185 gfc_iterator iter, *ip;
1187 gfc_st_label *label;
1190 old_loc = gfc_current_locus;
1193 iter.var = iter.start = iter.end = iter.step = NULL;
1195 m = gfc_match_label ();
1196 if (m == MATCH_ERROR)
1199 if (gfc_match (" do") != MATCH_YES)
1202 m = gfc_match_st_label (&label, 0);
1203 if (m == MATCH_ERROR)
1206 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1208 if (gfc_match_eos () == MATCH_YES)
1210 iter.end = gfc_logical_expr (1, NULL);
1211 new_st.op = EXEC_DO_WHILE;
1215 /* match an optional comma, if no comma is found a space is obligatory. */
1216 if (gfc_match_char(',') != MATCH_YES
1217 && gfc_match ("% ") != MATCH_YES)
1220 /* See if we have a DO WHILE. */
1221 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1223 new_st.op = EXEC_DO_WHILE;
1227 /* The abortive DO WHILE may have done something to the symbol
1228 table, so we start over: */
1229 gfc_undo_symbols ();
1230 gfc_current_locus = old_loc;
1232 gfc_match_label (); /* This won't error */
1233 gfc_match (" do "); /* This will work */
1235 gfc_match_st_label (&label, 0); /* Can't error out */
1236 gfc_match_char (','); /* Optional comma */
1238 m = gfc_match_iterator (&iter, 0);
1241 if (m == MATCH_ERROR)
1244 gfc_check_do_variable (iter.var->symtree);
1246 if (gfc_match_eos () != MATCH_YES)
1248 gfc_syntax_error (ST_DO);
1252 new_st.op = EXEC_DO;
1256 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1259 new_st.label = label;
1261 if (new_st.op == EXEC_DO_WHILE)
1262 new_st.expr = iter.end;
1265 new_st.ext.iterator = ip = gfc_get_iterator ();
1272 gfc_free_iterator (&iter, 0);
1278 /* Match an EXIT or CYCLE statement. */
1281 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1287 if (gfc_match_eos () == MATCH_YES)
1291 m = gfc_match ("% %s%t", &sym);
1292 if (m == MATCH_ERROR)
1296 gfc_syntax_error (st);
1300 if (sym->attr.flavor != FL_LABEL)
1302 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1303 sym->name, gfc_ascii_statement (st));
1308 /* Find the loop mentioned specified by the label (or lack of a
1310 for (p = gfc_state_stack; p; p = p->previous)
1311 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1317 gfc_error ("%s statement at %C is not within a loop",
1318 gfc_ascii_statement (st));
1320 gfc_error ("%s statement at %C is not within loop '%s'",
1321 gfc_ascii_statement (st), sym->name);
1326 /* Save the first statement in the loop - needed by the backend. */
1327 new_st.ext.whichloop = p->head;
1330 /* new_st.sym = sym;*/
1336 /* Match the EXIT statement. */
1339 gfc_match_exit (void)
1342 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1346 /* Match the CYCLE statement. */
1349 gfc_match_cycle (void)
1352 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1356 /* Match a number or character constant after a STOP or PAUSE statement. */
1359 gfc_match_stopcode (gfc_statement st)
1368 if (gfc_match_eos () != MATCH_YES)
1370 m = gfc_match_small_literal_int (&stop_code);
1371 if (m == MATCH_ERROR)
1374 if (m == MATCH_YES && stop_code > 99999)
1376 gfc_error ("STOP code out of range at %C");
1382 /* Try a character constant. */
1383 m = gfc_match_expr (&e);
1384 if (m == MATCH_ERROR)
1388 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1392 if (gfc_match_eos () != MATCH_YES)
1396 if (gfc_pure (NULL))
1398 gfc_error ("%s statement not allowed in PURE procedure at %C",
1399 gfc_ascii_statement (st));
1403 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1405 new_st.ext.stop_code = stop_code;
1410 gfc_syntax_error (st);
1418 /* Match the (deprecated) PAUSE statement. */
1421 gfc_match_pause (void)
1425 m = gfc_match_stopcode (ST_PAUSE);
1428 if (gfc_notify_std (GFC_STD_F95_DEL,
1429 "Obsolete: PAUSE statement at %C")
1437 /* Match the STOP statement. */
1440 gfc_match_stop (void)
1442 return gfc_match_stopcode (ST_STOP);
1446 /* Match a CONTINUE statement. */
1449 gfc_match_continue (void)
1452 if (gfc_match_eos () != MATCH_YES)
1454 gfc_syntax_error (ST_CONTINUE);
1458 new_st.op = EXEC_CONTINUE;
1463 /* Match the (deprecated) ASSIGN statement. */
1466 gfc_match_assign (void)
1469 gfc_st_label *label;
1471 if (gfc_match (" %l", &label) == MATCH_YES)
1473 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1475 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1477 if (gfc_notify_std (GFC_STD_F95_DEL,
1478 "Obsolete: ASSIGN statement at %C")
1482 expr->symtree->n.sym->attr.assign = 1;
1484 new_st.op = EXEC_LABEL_ASSIGN;
1485 new_st.label = label;
1494 /* Match the GO TO statement. As a computed GOTO statement is
1495 matched, it is transformed into an equivalent SELECT block. No
1496 tree is necessary, and the resulting jumps-to-jumps are
1497 specifically optimized away by the back end. */
1500 gfc_match_goto (void)
1502 gfc_code *head, *tail;
1505 gfc_st_label *label;
1509 if (gfc_match (" %l%t", &label) == MATCH_YES)
1511 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1514 new_st.op = EXEC_GOTO;
1515 new_st.label = label;
1519 /* The assigned GO TO statement. */
1521 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1523 if (gfc_notify_std (GFC_STD_F95_DEL,
1524 "Obsolete: Assigned GOTO statement at %C")
1528 expr->symtree->n.sym->attr.assign = 1;
1529 new_st.op = EXEC_GOTO;
1532 if (gfc_match_eos () == MATCH_YES)
1535 /* Match label list. */
1536 gfc_match_char (',');
1537 if (gfc_match_char ('(') != MATCH_YES)
1539 gfc_syntax_error (ST_GOTO);
1546 m = gfc_match_st_label (&label, 0);
1550 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1554 head = tail = gfc_get_code ();
1557 tail->block = gfc_get_code ();
1561 tail->label = label;
1562 tail->op = EXEC_GOTO;
1564 while (gfc_match_char (',') == MATCH_YES);
1566 if (gfc_match (")%t") != MATCH_YES)
1572 "Statement label list in GOTO at %C cannot be empty");
1575 new_st.block = head;
1580 /* Last chance is a computed GO TO statement. */
1581 if (gfc_match_char ('(') != MATCH_YES)
1583 gfc_syntax_error (ST_GOTO);
1592 m = gfc_match_st_label (&label, 0);
1596 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1600 head = tail = gfc_get_code ();
1603 tail->block = gfc_get_code ();
1607 cp = gfc_get_case ();
1608 cp->low = cp->high = gfc_int_expr (i++);
1610 tail->op = EXEC_SELECT;
1611 tail->ext.case_list = cp;
1613 tail->next = gfc_get_code ();
1614 tail->next->op = EXEC_GOTO;
1615 tail->next->label = label;
1617 while (gfc_match_char (',') == MATCH_YES);
1619 if (gfc_match_char (')') != MATCH_YES)
1624 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1628 /* Get the rest of the statement. */
1629 gfc_match_char (',');
1631 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1634 /* At this point, a computed GOTO has been fully matched and an
1635 equivalent SELECT statement constructed. */
1637 new_st.op = EXEC_SELECT;
1640 /* Hack: For a "real" SELECT, the expression is in expr. We put
1641 it in expr2 so we can distinguish then and produce the correct
1643 new_st.expr2 = expr;
1644 new_st.block = head;
1648 gfc_syntax_error (ST_GOTO);
1650 gfc_free_statements (head);
1655 /* Frees a list of gfc_alloc structures. */
1658 gfc_free_alloc_list (gfc_alloc * p)
1665 gfc_free_expr (p->expr);
1671 /* Match an ALLOCATE statement. */
1674 gfc_match_allocate (void)
1676 gfc_alloc *head, *tail;
1683 if (gfc_match_char ('(') != MATCH_YES)
1689 head = tail = gfc_get_alloc ();
1692 tail->next = gfc_get_alloc ();
1696 m = gfc_match_variable (&tail->expr, 0);
1699 if (m == MATCH_ERROR)
1702 if (gfc_check_do_variable (tail->expr->symtree))
1706 && gfc_impure_variable (tail->expr->symtree->n.sym))
1708 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1713 if (gfc_match_char (',') != MATCH_YES)
1716 m = gfc_match (" stat = %v", &stat);
1717 if (m == MATCH_ERROR)
1725 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1728 ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1729 "INTENT(IN)", stat->symtree->n.sym->name);
1733 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1736 ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1741 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1743 gfc_error("STAT expression at %C must be a variable");
1747 gfc_check_do_variable(stat->symtree);
1750 if (gfc_match (" )%t") != MATCH_YES)
1753 new_st.op = EXEC_ALLOCATE;
1755 new_st.ext.alloc_list = head;
1760 gfc_syntax_error (ST_ALLOCATE);
1763 gfc_free_expr (stat);
1764 gfc_free_alloc_list (head);
1769 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1770 a set of pointer assignments to intrinsic NULL(). */
1773 gfc_match_nullify (void)
1781 if (gfc_match_char ('(') != MATCH_YES)
1786 m = gfc_match_variable (&p, 0);
1787 if (m == MATCH_ERROR)
1792 if (gfc_check_do_variable(p->symtree))
1795 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1798 ("Illegal variable in NULLIFY at %C for a PURE procedure");
1802 /* build ' => NULL() ' */
1803 e = gfc_get_expr ();
1804 e->where = gfc_current_locus;
1805 e->expr_type = EXPR_NULL;
1806 e->ts.type = BT_UNKNOWN;
1813 tail->next = gfc_get_code ();
1817 tail->op = EXEC_POINTER_ASSIGN;
1821 if (gfc_match (" )%t") == MATCH_YES)
1823 if (gfc_match_char (',') != MATCH_YES)
1830 gfc_syntax_error (ST_NULLIFY);
1833 gfc_free_statements (tail);
1838 /* Match a DEALLOCATE statement. */
1841 gfc_match_deallocate (void)
1843 gfc_alloc *head, *tail;
1850 if (gfc_match_char ('(') != MATCH_YES)
1856 head = tail = gfc_get_alloc ();
1859 tail->next = gfc_get_alloc ();
1863 m = gfc_match_variable (&tail->expr, 0);
1864 if (m == MATCH_ERROR)
1869 if (gfc_check_do_variable (tail->expr->symtree))
1873 && gfc_impure_variable (tail->expr->symtree->n.sym))
1876 ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1881 if (gfc_match_char (',') != MATCH_YES)
1884 m = gfc_match (" stat = %v", &stat);
1885 if (m == MATCH_ERROR)
1893 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1895 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1896 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
1900 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
1902 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
1903 "for a PURE procedure");
1907 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1909 gfc_error("STAT expression at %C must be a variable");
1913 gfc_check_do_variable(stat->symtree);
1916 if (gfc_match (" )%t") != MATCH_YES)
1919 new_st.op = EXEC_DEALLOCATE;
1921 new_st.ext.alloc_list = head;
1926 gfc_syntax_error (ST_DEALLOCATE);
1929 gfc_free_expr (stat);
1930 gfc_free_alloc_list (head);
1935 /* Match a RETURN statement. */
1938 gfc_match_return (void)
1942 gfc_compile_state s;
1944 gfc_enclosing_unit (&s);
1945 if (s == COMP_PROGRAM
1946 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
1947 "main program at %C") == FAILURE)
1951 if (gfc_match_eos () == MATCH_YES)
1954 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
1956 gfc_error ("Alternate RETURN statement at %C is only allowed within "
1961 m = gfc_match ("% %e%t", &e);
1964 if (m == MATCH_ERROR)
1967 gfc_syntax_error (ST_RETURN);
1974 new_st.op = EXEC_RETURN;
1981 /* Match a CALL statement. The tricky part here are possible
1982 alternate return specifiers. We handle these by having all
1983 "subroutines" actually return an integer via a register that gives
1984 the return number. If the call specifies alternate returns, we
1985 generate code for a SELECT statement whose case clauses contain
1986 GOTOs to the various labels. */
1989 gfc_match_call (void)
1991 char name[GFC_MAX_SYMBOL_LEN + 1];
1992 gfc_actual_arglist *a, *arglist;
2002 m = gfc_match ("% %n", name);
2008 if (gfc_get_ha_sym_tree (name, &st))
2012 gfc_set_sym_referenced (sym);
2014 if (!sym->attr.generic
2015 && !sym->attr.subroutine
2016 && gfc_add_subroutine (&sym->attr, NULL) == FAILURE)
2019 if (gfc_match_eos () != MATCH_YES)
2021 m = gfc_match_actual_arglist (1, &arglist);
2024 if (m == MATCH_ERROR)
2027 if (gfc_match_eos () != MATCH_YES)
2031 /* If any alternate return labels were found, construct a SELECT
2032 statement that will jump to the right place. */
2035 for (a = arglist; a; a = a->next)
2036 if (a->expr == NULL)
2041 gfc_symtree *select_st;
2042 gfc_symbol *select_sym;
2043 char name[GFC_MAX_SYMBOL_LEN + 1];
2045 new_st.next = c = gfc_get_code ();
2046 c->op = EXEC_SELECT;
2047 sprintf (name, "_result_%s",sym->name);
2048 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail */
2050 select_sym = select_st->n.sym;
2051 select_sym->ts.type = BT_INTEGER;
2052 select_sym->ts.kind = gfc_default_integer_kind;
2053 gfc_set_sym_referenced (select_sym);
2054 c->expr = gfc_get_expr ();
2055 c->expr->expr_type = EXPR_VARIABLE;
2056 c->expr->symtree = select_st;
2057 c->expr->ts = select_sym->ts;
2058 c->expr->where = gfc_current_locus;
2061 for (a = arglist; a; a = a->next)
2063 if (a->expr != NULL)
2066 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2071 c->block = gfc_get_code ();
2073 c->op = EXEC_SELECT;
2075 new_case = gfc_get_case ();
2076 new_case->high = new_case->low = gfc_int_expr (i);
2077 c->ext.case_list = new_case;
2079 c->next = gfc_get_code ();
2080 c->next->op = EXEC_GOTO;
2081 c->next->label = a->label;
2085 new_st.op = EXEC_CALL;
2086 new_st.symtree = st;
2087 new_st.ext.actual = arglist;
2092 gfc_syntax_error (ST_CALL);
2095 gfc_free_actual_arglist (arglist);
2100 /* Given a name, return a pointer to the common head structure,
2101 creating it if it does not exist. If FROM_MODULE is nonzero, we
2102 mangle the name so that it doesn't interfere with commons defined
2103 in the using namespace.
2104 TODO: Add to global symbol tree. */
2107 gfc_get_common (const char *name, int from_module)
2110 static int serial = 0;
2111 char mangled_name[GFC_MAX_SYMBOL_LEN+1];
2115 /* A use associated common block is only needed to correctly layout
2116 the variables it contains. */
2117 snprintf(mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2118 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2122 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2125 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2128 if (st->n.common == NULL)
2130 st->n.common = gfc_get_common_head ();
2131 st->n.common->where = gfc_current_locus;
2132 strcpy (st->n.common->name, name);
2135 return st->n.common;
2139 /* Match a common block name. */
2142 match_common_name (char *name)
2146 if (gfc_match_char ('/') == MATCH_NO)
2152 if (gfc_match_char ('/') == MATCH_YES)
2158 m = gfc_match_name (name);
2160 if (m == MATCH_ERROR)
2162 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2165 gfc_error ("Syntax error in common block name at %C");
2170 /* Match a COMMON statement. */
2173 gfc_match_common (void)
2175 gfc_symbol *sym, **head, *tail, *old_blank_common;
2176 char name[GFC_MAX_SYMBOL_LEN+1];
2181 old_blank_common = gfc_current_ns->blank_common.head;
2182 if (old_blank_common)
2184 while (old_blank_common->common_next)
2185 old_blank_common = old_blank_common->common_next;
2190 if (gfc_match_eos () == MATCH_YES)
2195 m = match_common_name (name);
2196 if (m == MATCH_ERROR)
2199 if (name[0] == '\0')
2201 t = &gfc_current_ns->blank_common;
2202 if (t->head == NULL)
2203 t->where = gfc_current_locus;
2208 t = gfc_get_common (name, 0);
2217 while (tail->common_next)
2218 tail = tail->common_next;
2221 /* Grab the list of symbols. */
2222 if (gfc_match_eos () == MATCH_YES)
2227 m = gfc_match_symbol (&sym, 0);
2228 if (m == MATCH_ERROR)
2233 if (sym->attr.in_common)
2235 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2240 if (gfc_add_in_common (&sym->attr, NULL) == FAILURE)
2243 if (sym->value != NULL
2244 && (name[0] == '\0' || !sym->attr.data))
2246 if (name[0] == '\0')
2247 gfc_error ("Previously initialized symbol '%s' in "
2248 "blank COMMON block at %C", sym->name);
2250 gfc_error ("Previously initialized symbol '%s' in "
2251 "COMMON block '%s' at %C", sym->name, name);
2255 if (gfc_add_in_common (&sym->attr, NULL) == FAILURE)
2258 /* Derived type names must have the SEQUENCE attribute. */
2259 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2262 ("Derived type variable in COMMON at %C does not have the "
2263 "SEQUENCE attribute");
2268 tail->common_next = sym;
2274 /* Deal with an optional array specification after the
2276 m = gfc_match_array_spec (&as);
2277 if (m == MATCH_ERROR)
2282 if (as->type != AS_EXPLICIT)
2285 ("Array specification for symbol '%s' in COMMON at %C "
2286 "must be explicit", sym->name);
2290 if (gfc_add_dimension (&sym->attr, NULL) == FAILURE)
2293 if (sym->attr.pointer)
2296 ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2305 gfc_gobble_whitespace ();
2306 if (gfc_match_eos () == MATCH_YES)
2308 if (gfc_peek_char () == '/')
2310 if (gfc_match_char (',') != MATCH_YES)
2312 gfc_gobble_whitespace ();
2313 if (gfc_peek_char () == '/')
2322 gfc_syntax_error (ST_COMMON);
2325 if (old_blank_common)
2326 old_blank_common->common_next = NULL;
2328 gfc_current_ns->blank_common.head = NULL;
2329 gfc_free_array_spec (as);
2334 /* Match a BLOCK DATA program unit. */
2337 gfc_match_block_data (void)
2339 char name[GFC_MAX_SYMBOL_LEN + 1];
2343 if (gfc_match_eos () == MATCH_YES)
2345 gfc_new_block = NULL;
2349 m = gfc_match ("% %n%t", name);
2353 if (gfc_get_symbol (name, NULL, &sym))
2356 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, NULL) == FAILURE)
2359 gfc_new_block = sym;
2365 /* Free a namelist structure. */
2368 gfc_free_namelist (gfc_namelist * name)
2372 for (; name; name = n)
2380 /* Match a NAMELIST statement. */
2383 gfc_match_namelist (void)
2385 gfc_symbol *group_name, *sym;
2389 m = gfc_match (" / %s /", &group_name);
2392 if (m == MATCH_ERROR)
2397 if (group_name->ts.type != BT_UNKNOWN)
2400 ("Namelist group name '%s' at %C already has a basic type "
2401 "of %s", group_name->name, gfc_typename (&group_name->ts));
2405 if (group_name->attr.flavor != FL_NAMELIST
2406 && gfc_add_flavor (&group_name->attr, FL_NAMELIST, NULL) == FAILURE)
2411 m = gfc_match_symbol (&sym, 1);
2414 if (m == MATCH_ERROR)
2417 if (sym->attr.in_namelist == 0
2418 && gfc_add_in_namelist (&sym->attr, NULL) == FAILURE)
2421 /* TODO: worry about PRIVATE members of a PUBLIC namelist
2424 nl = gfc_get_namelist ();
2427 if (group_name->namelist == NULL)
2428 group_name->namelist = group_name->namelist_tail = nl;
2431 group_name->namelist_tail->next = nl;
2432 group_name->namelist_tail = nl;
2435 if (gfc_match_eos () == MATCH_YES)
2438 m = gfc_match_char (',');
2440 if (gfc_match_char ('/') == MATCH_YES)
2442 m2 = gfc_match (" %s /", &group_name);
2443 if (m2 == MATCH_YES)
2445 if (m2 == MATCH_ERROR)
2459 gfc_syntax_error (ST_NAMELIST);
2466 /* Match a MODULE statement. */
2469 gfc_match_module (void)
2473 m = gfc_match (" %s%t", &gfc_new_block);
2477 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE, NULL) == FAILURE)
2484 /* Free equivalence sets and lists. Recursively is the easiest way to
2488 gfc_free_equiv (gfc_equiv * eq)
2494 gfc_free_equiv (eq->eq);
2495 gfc_free_equiv (eq->next);
2497 gfc_free_expr (eq->expr);
2502 /* Match an EQUIVALENCE statement. */
2505 gfc_match_equivalence (void)
2507 gfc_equiv *eq, *set, *tail;
2515 eq = gfc_get_equiv ();
2519 eq->next = gfc_current_ns->equiv;
2520 gfc_current_ns->equiv = eq;
2522 if (gfc_match_char ('(') != MATCH_YES)
2529 m = gfc_match_variable (&set->expr, 1);
2530 if (m == MATCH_ERROR)
2535 for (ref = set->expr->ref; ref; ref = ref->next)
2536 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2539 ("Array reference in EQUIVALENCE at %C cannot be an "
2544 if (gfc_match_char (')') == MATCH_YES)
2546 if (gfc_match_char (',') != MATCH_YES)
2549 set->eq = gfc_get_equiv ();
2553 if (gfc_match_eos () == MATCH_YES)
2555 if (gfc_match_char (',') != MATCH_YES)
2562 gfc_syntax_error (ST_EQUIVALENCE);
2568 gfc_free_equiv (gfc_current_ns->equiv);
2569 gfc_current_ns->equiv = eq;
2575 /* Match a statement function declaration. It is so easy to match
2576 non-statement function statements with a MATCH_ERROR as opposed to
2577 MATCH_NO that we suppress error message in most cases. */
2580 gfc_match_st_function (void)
2582 gfc_error_buf old_error;
2587 m = gfc_match_symbol (&sym, 0);
2591 gfc_push_error (&old_error);
2593 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, NULL) == FAILURE)
2596 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2599 m = gfc_match (" = %e%t", &expr);
2602 if (m == MATCH_ERROR)
2610 gfc_pop_error (&old_error);
2615 /***************** SELECT CASE subroutines ******************/
2617 /* Free a single case structure. */
2620 free_case (gfc_case * p)
2622 if (p->low == p->high)
2624 gfc_free_expr (p->low);
2625 gfc_free_expr (p->high);
2630 /* Free a list of case structures. */
2633 gfc_free_case_list (gfc_case * p)
2645 /* Match a single case selector. */
2648 match_case_selector (gfc_case ** cp)
2653 c = gfc_get_case ();
2654 c->where = gfc_current_locus;
2656 if (gfc_match_char (':') == MATCH_YES)
2658 m = gfc_match_init_expr (&c->high);
2661 if (m == MATCH_ERROR)
2667 m = gfc_match_init_expr (&c->low);
2668 if (m == MATCH_ERROR)
2673 /* If we're not looking at a ':' now, make a range out of a single
2674 target. Else get the upper bound for the case range. */
2675 if (gfc_match_char (':') != MATCH_YES)
2679 m = gfc_match_init_expr (&c->high);
2680 if (m == MATCH_ERROR)
2682 /* MATCH_NO is fine. It's OK if nothing is there! */
2690 gfc_error ("Expected initialization expression in CASE at %C");
2698 /* Match the end of a case statement. */
2701 match_case_eos (void)
2703 char name[GFC_MAX_SYMBOL_LEN + 1];
2706 if (gfc_match_eos () == MATCH_YES)
2709 gfc_gobble_whitespace ();
2711 m = gfc_match_name (name);
2715 if (strcmp (name, gfc_current_block ()->name) != 0)
2717 gfc_error ("Expected case name of '%s' at %C",
2718 gfc_current_block ()->name);
2722 return gfc_match_eos ();
2726 /* Match a SELECT statement. */
2729 gfc_match_select (void)
2734 m = gfc_match_label ();
2735 if (m == MATCH_ERROR)
2738 m = gfc_match (" select case ( %e )%t", &expr);
2742 new_st.op = EXEC_SELECT;
2749 /* Match a CASE statement. */
2752 gfc_match_case (void)
2754 gfc_case *c, *head, *tail;
2759 if (gfc_current_state () != COMP_SELECT)
2761 gfc_error ("Unexpected CASE statement at %C");
2765 if (gfc_match ("% default") == MATCH_YES)
2767 m = match_case_eos ();
2770 if (m == MATCH_ERROR)
2773 new_st.op = EXEC_SELECT;
2774 c = gfc_get_case ();
2775 c->where = gfc_current_locus;
2776 new_st.ext.case_list = c;
2780 if (gfc_match_char ('(') != MATCH_YES)
2785 if (match_case_selector (&c) == MATCH_ERROR)
2795 if (gfc_match_char (')') == MATCH_YES)
2797 if (gfc_match_char (',') != MATCH_YES)
2801 m = match_case_eos ();
2804 if (m == MATCH_ERROR)
2807 new_st.op = EXEC_SELECT;
2808 new_st.ext.case_list = head;
2813 gfc_error ("Syntax error in CASE-specification at %C");
2816 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
2820 /********************* WHERE subroutines ********************/
2822 /* Match the rest of a simple WHERE statement that follows an IF statement.
2826 match_simple_where (void)
2832 m = gfc_match (" ( %e )", &expr);
2836 m = gfc_match_assignment ();
2839 if (m == MATCH_ERROR)
2842 if (gfc_match_eos () != MATCH_YES)
2845 c = gfc_get_code ();
2849 c->next = gfc_get_code ();
2852 gfc_clear_new_st ();
2854 new_st.op = EXEC_WHERE;
2860 gfc_syntax_error (ST_WHERE);
2863 gfc_free_expr (expr);
2867 /* Match a WHERE statement. */
2870 gfc_match_where (gfc_statement * st)
2876 m0 = gfc_match_label ();
2877 if (m0 == MATCH_ERROR)
2880 m = gfc_match (" where ( %e )", &expr);
2884 if (gfc_match_eos () == MATCH_YES)
2886 *st = ST_WHERE_BLOCK;
2888 new_st.op = EXEC_WHERE;
2893 m = gfc_match_assignment ();
2895 gfc_syntax_error (ST_WHERE);
2899 gfc_free_expr (expr);
2903 /* We've got a simple WHERE statement. */
2905 c = gfc_get_code ();
2909 c->next = gfc_get_code ();
2912 gfc_clear_new_st ();
2914 new_st.op = EXEC_WHERE;
2921 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
2922 new_st if successful. */
2925 gfc_match_elsewhere (void)
2927 char name[GFC_MAX_SYMBOL_LEN + 1];
2931 if (gfc_current_state () != COMP_WHERE)
2933 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
2939 if (gfc_match_char ('(') == MATCH_YES)
2941 m = gfc_match_expr (&expr);
2944 if (m == MATCH_ERROR)
2947 if (gfc_match_char (')') != MATCH_YES)
2951 if (gfc_match_eos () != MATCH_YES)
2952 { /* Better be a name at this point */
2953 m = gfc_match_name (name);
2956 if (m == MATCH_ERROR)
2959 if (gfc_match_eos () != MATCH_YES)
2962 if (strcmp (name, gfc_current_block ()->name) != 0)
2964 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
2965 name, gfc_current_block ()->name);
2970 new_st.op = EXEC_WHERE;
2975 gfc_syntax_error (ST_ELSEWHERE);
2978 gfc_free_expr (expr);
2983 /******************** FORALL subroutines ********************/
2985 /* Free a list of FORALL iterators. */
2988 gfc_free_forall_iterator (gfc_forall_iterator * iter)
2990 gfc_forall_iterator *next;
2996 gfc_free_expr (iter->var);
2997 gfc_free_expr (iter->start);
2998 gfc_free_expr (iter->end);
2999 gfc_free_expr (iter->stride);
3007 /* Match an iterator as part of a FORALL statement. The format is:
3009 <var> = <start>:<end>[:<stride>][, <scalar mask>] */
3012 match_forall_iterator (gfc_forall_iterator ** result)
3014 gfc_forall_iterator *iter;
3018 where = gfc_current_locus;
3019 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3021 m = gfc_match_variable (&iter->var, 0);
3025 if (gfc_match_char ('=') != MATCH_YES)
3031 m = gfc_match_expr (&iter->start);
3034 if (m == MATCH_ERROR)
3037 if (gfc_match_char (':') != MATCH_YES)
3040 m = gfc_match_expr (&iter->end);
3043 if (m == MATCH_ERROR)
3046 if (gfc_match_char (':') == MATCH_NO)
3047 iter->stride = gfc_int_expr (1);
3050 m = gfc_match_expr (&iter->stride);
3053 if (m == MATCH_ERROR)
3061 gfc_error ("Syntax error in FORALL iterator at %C");
3065 gfc_current_locus = where;
3066 gfc_free_forall_iterator (iter);
3071 /* Match the header of a FORALL statement. */
3074 match_forall_header (gfc_forall_iterator ** phead, gfc_expr ** mask)
3076 gfc_forall_iterator *head, *tail, *new;
3079 gfc_gobble_whitespace ();
3084 if (gfc_match_char ('(') != MATCH_YES)
3087 m = match_forall_iterator (&new);
3088 if (m == MATCH_ERROR)
3097 if (gfc_match_char (',') != MATCH_YES)
3100 m = match_forall_iterator (&new);
3101 if (m == MATCH_ERROR)
3110 /* Have to have a mask expression */
3112 m = gfc_match_expr (mask);
3115 if (m == MATCH_ERROR)
3121 if (gfc_match_char (')') == MATCH_NO)
3128 gfc_syntax_error (ST_FORALL);
3131 gfc_free_expr (*mask);
3132 gfc_free_forall_iterator (head);
3137 /* Match the rest of a simple FORALL statement that follows an IF statement.
3141 match_simple_forall (void)
3143 gfc_forall_iterator *head;
3152 m = match_forall_header (&head, &mask);
3159 m = gfc_match_assignment ();
3161 if (m == MATCH_ERROR)
3165 m = gfc_match_pointer_assignment ();
3166 if (m == MATCH_ERROR)
3172 c = gfc_get_code ();
3174 c->loc = gfc_current_locus;
3176 if (gfc_match_eos () != MATCH_YES)
3179 gfc_clear_new_st ();
3180 new_st.op = EXEC_FORALL;
3182 new_st.ext.forall_iterator = head;
3183 new_st.block = gfc_get_code ();
3185 new_st.block->op = EXEC_FORALL;
3186 new_st.block->next = c;
3191 gfc_syntax_error (ST_FORALL);
3194 gfc_free_forall_iterator (head);
3195 gfc_free_expr (mask);
3201 /* Match a FORALL statement. */
3204 gfc_match_forall (gfc_statement * st)
3206 gfc_forall_iterator *head;
3215 m0 = gfc_match_label ();
3216 if (m0 == MATCH_ERROR)
3219 m = gfc_match (" forall");
3223 m = match_forall_header (&head, &mask);
3224 if (m == MATCH_ERROR)
3229 if (gfc_match_eos () == MATCH_YES)
3231 *st = ST_FORALL_BLOCK;
3233 new_st.op = EXEC_FORALL;
3235 new_st.ext.forall_iterator = head;
3240 m = gfc_match_assignment ();
3241 if (m == MATCH_ERROR)
3245 m = gfc_match_pointer_assignment ();
3246 if (m == MATCH_ERROR)
3252 c = gfc_get_code ();
3255 if (gfc_match_eos () != MATCH_YES)
3258 gfc_clear_new_st ();
3259 new_st.op = EXEC_FORALL;
3261 new_st.ext.forall_iterator = head;
3262 new_st.block = gfc_get_code ();
3264 new_st.block->op = EXEC_FORALL;
3265 new_st.block->next = c;
3271 gfc_syntax_error (ST_FORALL);
3274 gfc_free_forall_iterator (head);
3275 gfc_free_expr (mask);
3276 gfc_free_statements (c);