1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 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
35 /* For matching and debugging purposes. Order matters here! The
36 unary operators /must/ precede the binary plus and minus, or
37 the expression parser breaks. */
39 mstring intrinsic_operators[] = {
40 minit ("+", INTRINSIC_UPLUS),
41 minit ("-", INTRINSIC_UMINUS),
42 minit ("+", INTRINSIC_PLUS),
43 minit ("-", INTRINSIC_MINUS),
44 minit ("**", INTRINSIC_POWER),
45 minit ("//", INTRINSIC_CONCAT),
46 minit ("*", INTRINSIC_TIMES),
47 minit ("/", INTRINSIC_DIVIDE),
48 minit (".and.", INTRINSIC_AND),
49 minit (".or.", INTRINSIC_OR),
50 minit (".eqv.", INTRINSIC_EQV),
51 minit (".neqv.", INTRINSIC_NEQV),
52 minit (".eq.", INTRINSIC_EQ),
53 minit ("==", INTRINSIC_EQ),
54 minit (".ne.", INTRINSIC_NE),
55 minit ("/=", INTRINSIC_NE),
56 minit (".ge.", INTRINSIC_GE),
57 minit (">=", INTRINSIC_GE),
58 minit (".le.", INTRINSIC_LE),
59 minit ("<=", INTRINSIC_LE),
60 minit (".lt.", INTRINSIC_LT),
61 minit ("<", INTRINSIC_LT),
62 minit (".gt.", INTRINSIC_GT),
63 minit (">", INTRINSIC_GT),
64 minit (".not.", INTRINSIC_NOT),
65 minit (NULL, INTRINSIC_NONE)
69 /******************** Generic matching subroutines ************************/
71 /* In free form, match at least one space. Always matches in fixed
75 gfc_match_space (void)
80 if (gfc_current_file->form == FORM_FIXED)
83 old_loc = *gfc_current_locus ();
86 if (!gfc_is_whitespace (c))
88 gfc_set_locus (&old_loc);
92 gfc_gobble_whitespace ();
98 /* Match an end of statement. End of statement is optional
99 whitespace, followed by a ';' or '\n' or comment '!'. If a
100 semicolon is found, we continue to eat whitespace and semicolons. */
112 old_loc = *gfc_current_locus ();
113 gfc_gobble_whitespace ();
115 c = gfc_next_char ();
121 c = gfc_next_char ();
138 gfc_set_locus (&old_loc);
139 return (flag) ? MATCH_YES : MATCH_NO;
143 /* Match a literal integer on the input, setting the value on
144 MATCH_YES. Literal ints occur in kind-parameters as well as
145 old-style character length specifications. */
148 gfc_match_small_literal_int (int *value)
154 old_loc = *gfc_current_locus ();
156 gfc_gobble_whitespace ();
157 c = gfc_next_char ();
161 gfc_set_locus (&old_loc);
169 old_loc = *gfc_current_locus ();
170 c = gfc_next_char ();
175 i = 10 * i + c - '0';
179 gfc_error ("Integer too large at %C");
184 gfc_set_locus (&old_loc);
191 /* Match a small, constant integer expression, like in a kind
192 statement. On MATCH_YES, 'value' is set. */
195 gfc_match_small_int (int *value)
202 m = gfc_match_expr (&expr);
206 p = gfc_extract_int (expr, &i);
207 gfc_free_expr (expr);
220 /* Matches a statement label. Uses gfc_match_small_literal_int() to
221 do most of the work. */
224 gfc_match_st_label (gfc_st_label ** label, int allow_zero)
230 old_loc = *gfc_current_locus ();
232 m = gfc_match_small_literal_int (&i);
236 if (((i == 0) && allow_zero) || i <= 99999)
238 *label = gfc_get_st_label (i);
242 gfc_error ("Statement label at %C is out of range");
243 gfc_set_locus (&old_loc);
248 /* Match and validate a label associated with a named IF, DO or SELECT
249 statement. If the symbol does not have the label attribute, we add
250 it. We also make sure the symbol does not refer to another
251 (active) block. A matched label is pointed to by gfc_new_block. */
254 gfc_match_label (void)
256 char name[GFC_MAX_SYMBOL_LEN + 1];
260 gfc_new_block = NULL;
262 m = gfc_match (" %n :", name);
266 if (gfc_get_symbol (name, NULL, &gfc_new_block))
268 gfc_error ("Label name '%s' at %C is ambiguous", name);
272 if (gfc_new_block->attr.flavor != FL_LABEL
273 && gfc_add_flavor (&gfc_new_block->attr, FL_LABEL, NULL) == FAILURE)
276 for (p = gfc_state_stack; p; p = p->previous)
277 if (p->sym == gfc_new_block)
279 gfc_error ("Label %s at %C already in use by a parent block",
280 gfc_new_block->name);
288 /* Try and match the input against an array of possibilities. If one
289 potential matching string is a substring of another, the longest
290 match takes precedence. Spaces in the target strings are optional
291 spaces that do not necessarily have to be found in the input
292 stream. In fixed mode, spaces never appear. If whitespace is
293 matched, it matches unlimited whitespace in the input. For this
294 reason, the 'mp' member of the mstring structure is used to track
295 the progress of each potential match.
297 If there is no match we return the tag associated with the
298 terminating NULL mstring structure and leave the locus pointer
299 where it started. If there is a match we return the tag member of
300 the matched mstring and leave the locus pointer after the matched
303 A '%' character is a mandatory space. */
306 gfc_match_strings (mstring * a)
308 mstring *p, *best_match;
309 int no_match, c, possibles;
314 for (p = a; p->string != NULL; p++)
323 match_loc = *gfc_current_locus ();
325 gfc_gobble_whitespace ();
327 while (possibles > 0)
329 c = gfc_next_char ();
331 /* Apply the next character to the current possibilities. */
332 for (p = a; p->string != NULL; p++)
339 /* Space matches 1+ whitespace(s). */
340 if ((gfc_current_file->form == FORM_FREE)
341 && gfc_is_whitespace (c))
359 match_loc = *gfc_current_locus ();
367 gfc_set_locus (&match_loc);
369 return (best_match == NULL) ? no_match : best_match->tag;
373 /* See if the current input looks like a name of some sort. Modifies
374 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long. */
377 gfc_match_name (char *buffer)
382 old_loc = *gfc_current_locus ();
383 gfc_gobble_whitespace ();
385 c = gfc_next_char ();
388 gfc_set_locus (&old_loc);
398 if (i > gfc_option.max_identifier_length)
400 gfc_error ("Name at %C is too long");
404 old_loc = *gfc_current_locus ();
405 c = gfc_next_char ();
409 || (gfc_option.flag_dollar_ok && c == '$'));
412 gfc_set_locus (&old_loc);
418 /* Match a symbol on the input. Modifies the pointer to the symbol
419 pointer if successful. */
422 gfc_match_sym_tree (gfc_symtree ** matched_symbol, int host_assoc)
424 char buffer[GFC_MAX_SYMBOL_LEN + 1];
427 m = gfc_match_name (buffer);
432 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
433 ? MATCH_ERROR : MATCH_YES;
435 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
443 gfc_match_symbol (gfc_symbol ** matched_symbol, int host_assoc)
448 m = gfc_match_sym_tree (&st, host_assoc);
453 *matched_symbol = st->n.sym;
455 *matched_symbol = NULL;
460 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
461 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
465 gfc_match_intrinsic_op (gfc_intrinsic_op * result)
469 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
471 if (op == INTRINSIC_NONE)
479 /* Match a loop control phrase:
481 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
483 If the final integer expression is not present, a constant unity
484 expression is returned. We don't return MATCH_ERROR until after
485 the equals sign is seen. */
488 gfc_match_iterator (gfc_iterator * iter, int init_flag)
490 char name[GFC_MAX_SYMBOL_LEN + 1];
491 gfc_expr *var, *e1, *e2, *e3;
495 /* Match the start of an iterator without affecting the symbol
498 start = *gfc_current_locus ();
499 m = gfc_match (" %n =", name);
500 gfc_set_locus (&start);
505 m = gfc_match_variable (&var, 0);
509 gfc_match_char ('=');
513 if (var->ref != NULL)
515 gfc_error ("Loop variable at %C cannot be a sub-component");
519 if (var->symtree->n.sym->attr.intent == INTENT_IN)
521 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
522 var->symtree->n.sym->name);
526 if (var->symtree->n.sym->attr.pointer)
528 gfc_error ("Loop variable at %C cannot have the POINTER attribute");
532 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
535 if (m == MATCH_ERROR)
538 if (gfc_match_char (',') != MATCH_YES)
541 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
544 if (m == MATCH_ERROR)
547 if (gfc_match_char (',') != MATCH_YES)
549 e3 = gfc_int_expr (1);
553 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
554 if (m == MATCH_ERROR)
558 gfc_error ("Expected a step value in iterator at %C");
570 gfc_error ("Syntax error in iterator at %C");
581 /* Tries to match the next non-whitespace character on the input.
582 This subroutine does not return MATCH_ERROR. */
585 gfc_match_char (char c)
589 where = *gfc_current_locus ();
590 gfc_gobble_whitespace ();
592 if (gfc_next_char () == c)
595 gfc_set_locus (&where);
600 /* General purpose matching subroutine. The target string is a
601 scanf-like format string in which spaces correspond to arbitrary
602 whitespace (including no whitespace), characters correspond to
603 themselves. The %-codes are:
605 %% Literal percent sign
606 %e Expression, pointer to a pointer is set
607 %s Symbol, pointer to the symbol is set
608 %n Name, character buffer is set to name
609 %t Matches end of statement.
610 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
611 %l Matches a statement label
612 %v Matches a variable expression (an lvalue)
613 % Matches a required space (in free form) and optional spaces. */
616 gfc_match (const char *target, ...)
618 gfc_st_label **label;
627 old_loc = *gfc_current_locus ();
628 va_start (argp, target);
638 gfc_gobble_whitespace ();
649 vp = va_arg (argp, void **);
650 n = gfc_match_expr ((gfc_expr **) vp);
661 vp = va_arg (argp, void **);
662 n = gfc_match_variable ((gfc_expr **) vp, 0);
673 vp = va_arg (argp, void **);
674 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
685 np = va_arg (argp, char *);
686 n = gfc_match_name (np);
697 label = va_arg (argp, gfc_st_label **);
698 n = gfc_match_st_label (label, 0);
709 ip = va_arg (argp, int *);
710 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
721 if (gfc_match_eos () != MATCH_YES)
729 if (gfc_match_space () == MATCH_YES)
735 break; /* Fall through to character matcher */
738 gfc_internal_error ("gfc_match(): Bad match code %c", c);
742 if (c == gfc_next_char ())
752 /* Clean up after a failed match. */
753 gfc_set_locus (&old_loc);
754 va_start (argp, target);
757 for (; matches > 0; matches--)
774 /* Matches that don't have to be undone */
779 (void)va_arg (argp, void **);
786 vp = va_arg (argp, void **);
800 /*********************** Statement level matching **********************/
802 /* Matches the start of a program unit, which is the program keyword
803 followed by an optional symbol. */
806 gfc_match_program (void)
811 m = gfc_match_eos ();
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, 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 m = gfc_match (" %e%t", &rvalue);
855 gfc_set_sym_referenced (lvalue->symtree->n.sym);
857 new_st.op = EXEC_ASSIGN;
858 new_st.expr = lvalue;
859 new_st.expr2 = rvalue;
864 gfc_set_locus (&old_loc);
865 gfc_free_expr (lvalue);
866 gfc_free_expr (rvalue);
871 /* Match a pointer assignment statement. */
874 gfc_match_pointer_assignment (void)
876 gfc_expr *lvalue, *rvalue;
880 old_loc = *gfc_current_locus ();
882 lvalue = rvalue = NULL;
884 m = gfc_match (" %v =>", &lvalue);
891 m = gfc_match (" %e%t", &rvalue);
895 new_st.op = EXEC_POINTER_ASSIGN;
896 new_st.expr = lvalue;
897 new_st.expr2 = rvalue;
902 gfc_set_locus (&old_loc);
903 gfc_free_expr (lvalue);
904 gfc_free_expr (rvalue);
909 /* The IF statement is a bit of a pain. First of all, there are three
910 forms of it, the simple IF, the IF that starts a block and the
913 There is a problem with the simple IF and that is the fact that we
914 only have a single level of undo information on symbols. What this
915 means is for a simple IF, we must re-match the whole IF statement
916 multiple times in order to guarantee that the symbol table ends up
917 in the proper state. */
920 gfc_match_if (gfc_statement * if_type)
923 gfc_st_label *l1, *l2, *l3;
928 n = gfc_match_label ();
929 if (n == MATCH_ERROR)
932 old_loc = *gfc_current_locus ();
934 m = gfc_match (" if ( %e", &expr);
938 if (gfc_match_char (')') != MATCH_YES)
940 gfc_error ("Syntax error in IF-expression at %C");
941 gfc_free_expr (expr);
945 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
952 ("Block label not appropriate for arithmetic IF statement "
955 gfc_free_expr (expr);
959 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
960 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
961 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
964 gfc_free_expr (expr);
968 new_st.op = EXEC_ARITHMETIC_IF;
974 *if_type = ST_ARITHMETIC_IF;
978 if (gfc_match (" then %t") == MATCH_YES)
983 *if_type = ST_IF_BLOCK;
989 gfc_error ("Block label is not appropriate IF statement at %C");
991 gfc_free_expr (expr);
995 /* At this point the only thing left is a simple IF statement. At
996 this point, n has to be MATCH_NO, so we don't have to worry about
997 re-matching a block label. From what we've got so far, try
998 matching an assignment. */
1000 *if_type = ST_SIMPLE_IF;
1002 m = gfc_match_assignment ();
1006 gfc_free_expr (expr);
1007 gfc_undo_symbols ();
1008 gfc_set_locus (&old_loc);
1010 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1012 m = gfc_match_pointer_assignment ();
1016 gfc_free_expr (expr);
1017 gfc_undo_symbols ();
1018 gfc_set_locus (&old_loc);
1020 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1022 /* Look at the next keyword to see which matcher to call. Matching
1023 the keyword doesn't affect the symbol table, so we don't have to
1024 restore between tries. */
1026 #define match(string, subr, statement) \
1027 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1031 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1032 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1033 match ("call", gfc_match_call, ST_CALL)
1034 match ("close", gfc_match_close, ST_CLOSE)
1035 match ("continue", gfc_match_continue, ST_CONTINUE)
1036 match ("cycle", gfc_match_cycle, ST_CYCLE)
1037 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1038 match ("end file", gfc_match_endfile, ST_END_FILE)
1039 match ("exit", gfc_match_exit, ST_EXIT)
1040 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1041 match ("go to", gfc_match_goto, ST_GOTO)
1042 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1043 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1044 match ("open", gfc_match_open, ST_OPEN)
1045 match ("pause", gfc_match_pause, ST_NONE)
1046 match ("print", gfc_match_print, ST_WRITE)
1047 match ("read", gfc_match_read, ST_READ)
1048 match ("return", gfc_match_return, ST_RETURN)
1049 match ("rewind", gfc_match_rewind, ST_REWIND)
1050 match ("pause", gfc_match_stop, ST_PAUSE)
1051 match ("stop", gfc_match_stop, ST_STOP)
1052 match ("write", gfc_match_write, ST_WRITE)
1054 /* All else has failed, so give up. See if any of the matchers has
1055 stored an error message of some sort. */
1056 if (gfc_error_check () == 0)
1057 gfc_error ("Unclassifiable statement in IF-clause at %C");
1059 gfc_free_expr (expr);
1064 gfc_error ("Syntax error in IF-clause at %C");
1067 gfc_free_expr (expr);
1071 /* At this point, we've matched the single IF and the action clause
1072 is in new_st. Rearrange things so that the IF statement appears
1075 p = gfc_get_code ();
1076 p->next = gfc_get_code ();
1078 p->next->loc = *gfc_current_locus ();
1083 gfc_clear_new_st ();
1085 new_st.op = EXEC_IF;
1094 /* Match an ELSE statement. */
1097 gfc_match_else (void)
1099 char name[GFC_MAX_SYMBOL_LEN + 1];
1101 if (gfc_match_eos () == MATCH_YES)
1104 if (gfc_match_name (name) != MATCH_YES
1105 || gfc_current_block () == NULL
1106 || gfc_match_eos () != MATCH_YES)
1108 gfc_error ("Unexpected junk after ELSE statement at %C");
1112 if (strcmp (name, gfc_current_block ()->name) != 0)
1114 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1115 name, gfc_current_block ()->name);
1123 /* Match an ELSE IF statement. */
1126 gfc_match_elseif (void)
1128 char name[GFC_MAX_SYMBOL_LEN + 1];
1132 m = gfc_match (" ( %e ) then", &expr);
1136 if (gfc_match_eos () == MATCH_YES)
1139 if (gfc_match_name (name) != MATCH_YES
1140 || gfc_current_block () == NULL
1141 || gfc_match_eos () != MATCH_YES)
1143 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1147 if (strcmp (name, gfc_current_block ()->name) != 0)
1149 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1150 name, gfc_current_block ()->name);
1155 new_st.op = EXEC_IF;
1160 gfc_free_expr (expr);
1165 /* Free a gfc_iterator structure. */
1168 gfc_free_iterator (gfc_iterator * iter, int flag)
1174 gfc_free_expr (iter->var);
1175 gfc_free_expr (iter->start);
1176 gfc_free_expr (iter->end);
1177 gfc_free_expr (iter->step);
1184 /* Match a DO statement. */
1189 gfc_iterator iter, *ip;
1191 gfc_st_label *label;
1194 old_loc = *gfc_current_locus ();
1197 iter.var = iter.start = iter.end = iter.step = NULL;
1199 m = gfc_match_label ();
1200 if (m == MATCH_ERROR)
1203 if (gfc_match (" do") != MATCH_YES)
1206 m = gfc_match_st_label (&label, 0);
1207 if (m == MATCH_ERROR)
1210 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1212 if (gfc_match_eos () == MATCH_YES)
1214 iter.end = gfc_logical_expr (1, NULL);
1215 new_st.op = EXEC_DO_WHILE;
1219 /* match an optional comma, if no comma is found a space is obligatory. */
1220 if (gfc_match_char(',') != MATCH_YES
1221 && gfc_match ("% ") != MATCH_YES)
1224 /* See if we have a DO WHILE. */
1225 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1227 new_st.op = EXEC_DO_WHILE;
1231 /* The abortive DO WHILE may have done something to the symbol
1232 table, so we start over: */
1233 gfc_undo_symbols ();
1234 gfc_set_locus (&old_loc);
1236 gfc_match_label (); /* This won't error */
1237 gfc_match (" do "); /* This will work */
1239 gfc_match_st_label (&label, 0); /* Can't error out */
1240 gfc_match_char (','); /* Optional comma */
1242 m = gfc_match_iterator (&iter, 0);
1245 if (m == MATCH_ERROR)
1248 if (gfc_match_eos () != MATCH_YES)
1250 gfc_syntax_error (ST_DO);
1254 new_st.op = EXEC_DO;
1258 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1261 new_st.label = label;
1263 if (new_st.op == EXEC_DO_WHILE)
1264 new_st.expr = iter.end;
1267 new_st.ext.iterator = ip = gfc_get_iterator ();
1274 gfc_free_iterator (&iter, 0);
1280 /* Match an EXIT or CYCLE statement. */
1283 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1289 if (gfc_match_eos () == MATCH_YES)
1293 m = gfc_match ("% %s%t", &sym);
1294 if (m == MATCH_ERROR)
1298 gfc_syntax_error (st);
1302 if (sym->attr.flavor != FL_LABEL)
1304 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1305 sym->name, gfc_ascii_statement (st));
1310 /* Find the loop mentioned specified by the label (or lack of a
1312 for (p = gfc_state_stack; p; p = p->previous)
1313 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1319 gfc_error ("%s statement at %C is not within a loop",
1320 gfc_ascii_statement (st));
1322 gfc_error ("%s statement at %C is not within loop '%s'",
1323 gfc_ascii_statement (st), sym->name);
1328 /* Save the first statement in the loop - needed by the backend. */
1329 new_st.ext.whichloop = p->head;
1332 /* new_st.sym = sym;*/
1338 /* Match the EXIT statement. */
1341 gfc_match_exit (void)
1344 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1348 /* Match the CYCLE statement. */
1351 gfc_match_cycle (void)
1354 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1358 /* Match a number or character constant after a STOP or PAUSE statement. */
1361 gfc_match_stopcode (gfc_statement st)
1370 if (gfc_match_eos () != MATCH_YES)
1372 m = gfc_match_small_literal_int (&stop_code);
1373 if (m == MATCH_ERROR)
1376 if (m == MATCH_YES && stop_code > 99999)
1378 gfc_error ("STOP code out of range at %C");
1384 /* Try a character constant. */
1385 m = gfc_match_expr (&e);
1386 if (m == MATCH_ERROR)
1390 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1394 if (gfc_match_eos () != MATCH_YES)
1398 if (gfc_pure (NULL))
1400 gfc_error ("%s statement not allowed in PURE procedure at %C",
1401 gfc_ascii_statement (st));
1405 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1407 new_st.ext.stop_code = stop_code;
1412 gfc_syntax_error (st);
1420 /* Match the (deprecated) PAUSE statement. */
1423 gfc_match_pause (void)
1427 m = gfc_match_stopcode (ST_PAUSE);
1430 if (gfc_notify_std (GFC_STD_F95_DEL,
1431 "Obsolete: PAUSE statement at %C")
1439 /* Match the STOP statement. */
1442 gfc_match_stop (void)
1444 return gfc_match_stopcode (ST_STOP);
1448 /* Match a CONTINUE statement. */
1451 gfc_match_continue (void)
1454 if (gfc_match_eos () != MATCH_YES)
1456 gfc_syntax_error (ST_CONTINUE);
1460 new_st.op = EXEC_CONTINUE;
1465 /* Match the (deprecated) ASSIGN statement. */
1468 gfc_match_assign (void)
1471 gfc_st_label *label;
1473 if (gfc_match (" %l", &label) == MATCH_YES)
1475 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1477 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1479 if (gfc_notify_std (GFC_STD_F95_DEL,
1480 "Obsolete: ASSIGN statement at %C")
1484 expr->symtree->n.sym->attr.assign = 1;
1486 new_st.op = EXEC_LABEL_ASSIGN;
1487 new_st.label = label;
1496 /* Match the GO TO statement. As a computed GOTO statement is
1497 matched, it is transformed into an equivalent SELECT block. No
1498 tree is necessary, and the resulting jumps-to-jumps are
1499 specifically optimized away by the back end. */
1502 gfc_match_goto (void)
1504 gfc_code *head, *tail;
1507 gfc_st_label *label;
1511 if (gfc_match (" %l%t", &label) == MATCH_YES)
1513 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1516 new_st.op = EXEC_GOTO;
1517 new_st.label = label;
1521 /* The assigned GO TO statement. */
1523 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1525 if (gfc_notify_std (GFC_STD_F95_DEL,
1526 "Obsolete: Assigned GOTO statement at %C")
1530 expr->symtree->n.sym->attr.assign = 1;
1531 new_st.op = EXEC_GOTO;
1534 if (gfc_match_eos () == MATCH_YES)
1537 /* Match label list. */
1538 gfc_match_char (',');
1539 if (gfc_match_char ('(') != MATCH_YES)
1541 gfc_syntax_error (ST_GOTO);
1548 m = gfc_match_st_label (&label, 0);
1552 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1556 head = tail = gfc_get_code ();
1559 tail->block = gfc_get_code ();
1563 tail->label = label;
1564 tail->op = EXEC_GOTO;
1566 while (gfc_match_char (',') == MATCH_YES);
1568 if (gfc_match (")%t") != MATCH_YES)
1574 "Statement label list in GOTO at %C cannot be empty");
1577 new_st.block = head;
1582 /* Last chance is a computed GO TO statement. */
1583 if (gfc_match_char ('(') != MATCH_YES)
1585 gfc_syntax_error (ST_GOTO);
1594 m = gfc_match_st_label (&label, 0);
1598 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1602 head = tail = gfc_get_code ();
1605 tail->block = gfc_get_code ();
1609 cp = gfc_get_case ();
1610 cp->low = cp->high = gfc_int_expr (i++);
1612 tail->op = EXEC_SELECT;
1613 tail->ext.case_list = cp;
1615 tail->next = gfc_get_code ();
1616 tail->next->op = EXEC_GOTO;
1617 tail->next->label = label;
1619 while (gfc_match_char (',') == MATCH_YES);
1621 if (gfc_match_char (')') != MATCH_YES)
1626 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1630 /* Get the rest of the statement. */
1631 gfc_match_char (',');
1633 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1636 /* At this point, a computed GOTO has been fully matched and an
1637 equivalent SELECT statement constructed. */
1639 new_st.op = EXEC_SELECT;
1642 /* Hack: For a "real" SELECT, the expression is in expr. We put
1643 it in expr2 so we can distinguish then and produce the correct
1645 new_st.expr2 = expr;
1646 new_st.block = head;
1650 gfc_syntax_error (ST_GOTO);
1652 gfc_free_statements (head);
1657 /* Frees a list of gfc_alloc structures. */
1660 gfc_free_alloc_list (gfc_alloc * p)
1667 gfc_free_expr (p->expr);
1673 /* Match an ALLOCATE statement. */
1676 gfc_match_allocate (void)
1678 gfc_alloc *head, *tail;
1685 if (gfc_match_char ('(') != MATCH_YES)
1691 head = tail = gfc_get_alloc ();
1694 tail->next = gfc_get_alloc ();
1698 m = gfc_match_variable (&tail->expr, 0);
1701 if (m == MATCH_ERROR)
1705 && gfc_impure_variable (tail->expr->symtree->n.sym))
1707 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1712 if (gfc_match_char (',') != MATCH_YES)
1715 m = gfc_match (" stat = %v", &stat);
1716 if (m == MATCH_ERROR)
1724 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1727 ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1728 "INTENT(IN)", stat->symtree->n.sym->name);
1732 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1735 ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1741 if (gfc_match (" )%t") != MATCH_YES)
1744 new_st.op = EXEC_ALLOCATE;
1746 new_st.ext.alloc_list = head;
1751 gfc_syntax_error (ST_ALLOCATE);
1754 gfc_free_expr (stat);
1755 gfc_free_alloc_list (head);
1760 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1761 a set of pointer assignments to intrinsic NULL(). */
1764 gfc_match_nullify (void)
1772 if (gfc_match_char ('(') != MATCH_YES)
1777 m = gfc_match_variable (&p, 0);
1778 if (m == MATCH_ERROR)
1783 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1786 ("Illegal variable in NULLIFY at %C for a PURE procedure");
1790 /* build ' => NULL() ' */
1791 e = gfc_get_expr ();
1792 e->where = *gfc_current_locus ();
1793 e->expr_type = EXPR_NULL;
1794 e->ts.type = BT_UNKNOWN;
1801 tail->next = gfc_get_code ();
1805 tail->op = EXEC_POINTER_ASSIGN;
1809 if (gfc_match_char (')') == MATCH_YES)
1811 if (gfc_match_char (',') != MATCH_YES)
1818 gfc_syntax_error (ST_NULLIFY);
1821 gfc_free_statements (tail);
1826 /* Match a DEALLOCATE statement. */
1829 gfc_match_deallocate (void)
1831 gfc_alloc *head, *tail;
1838 if (gfc_match_char ('(') != MATCH_YES)
1844 head = tail = gfc_get_alloc ();
1847 tail->next = gfc_get_alloc ();
1851 m = gfc_match_variable (&tail->expr, 0);
1852 if (m == MATCH_ERROR)
1858 && gfc_impure_variable (tail->expr->symtree->n.sym))
1861 ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1866 if (gfc_match_char (',') != MATCH_YES)
1869 m = gfc_match (" stat = %v", &stat);
1870 if (m == MATCH_ERROR)
1876 if (stat != NULL && stat->symtree->n.sym->attr.intent == INTENT_IN)
1878 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C cannot be "
1879 "INTENT(IN)", stat->symtree->n.sym->name);
1883 if (gfc_match (" )%t") != MATCH_YES)
1886 new_st.op = EXEC_DEALLOCATE;
1888 new_st.ext.alloc_list = head;
1893 gfc_syntax_error (ST_DEALLOCATE);
1896 gfc_free_expr (stat);
1897 gfc_free_alloc_list (head);
1902 /* Match a RETURN statement. */
1905 gfc_match_return (void)
1911 if (gfc_match_eos () == MATCH_YES)
1914 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
1916 gfc_error ("Alternate RETURN statement at %C is only allowed within "
1921 m = gfc_match ("% %e%t", &e);
1924 if (m == MATCH_ERROR)
1927 gfc_syntax_error (ST_RETURN);
1934 new_st.op = EXEC_RETURN;
1941 /* Match a CALL statement. The tricky part here are possible
1942 alternate return specifiers. We handle these by having all
1943 "subroutines" actually return an integer via a register that gives
1944 the return number. If the call specifies alternate returns, we
1945 generate code for a SELECT statement whose case clauses contain
1946 GOTOs to the various labels. */
1949 gfc_match_call (void)
1951 char name[GFC_MAX_SYMBOL_LEN + 1];
1952 gfc_actual_arglist *a, *arglist;
1962 m = gfc_match ("% %n", name);
1968 if (gfc_get_ha_sym_tree (name, &st))
1972 gfc_set_sym_referenced (sym);
1974 if (!sym->attr.generic
1975 && !sym->attr.subroutine
1976 && gfc_add_subroutine (&sym->attr, NULL) == FAILURE)
1979 if (gfc_match_eos () != MATCH_YES)
1981 m = gfc_match_actual_arglist (1, &arglist);
1984 if (m == MATCH_ERROR)
1987 if (gfc_match_eos () != MATCH_YES)
1991 /* If any alternate return labels were found, construct a SELECT
1992 statement that will jump to the right place. */
1995 for (a = arglist; a; a = a->next)
1996 if (a->expr == NULL)
2001 gfc_symtree *select_st;
2002 gfc_symbol *select_sym;
2003 char name[GFC_MAX_SYMBOL_LEN + 1];
2005 new_st.next = c = gfc_get_code ();
2006 c->op = EXEC_SELECT;
2007 sprintf (name, "_result_%s",sym->name);
2008 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail */
2010 select_sym = select_st->n.sym;
2011 select_sym->ts.type = BT_INTEGER;
2012 select_sym->ts.kind = gfc_default_integer_kind ();
2013 gfc_set_sym_referenced (select_sym);
2014 c->expr = gfc_get_expr ();
2015 c->expr->expr_type = EXPR_VARIABLE;
2016 c->expr->symtree = select_st;
2017 c->expr->ts = select_sym->ts;
2018 c->expr->where = *gfc_current_locus ();
2021 for (a = arglist; a; a = a->next)
2023 if (a->expr != NULL)
2026 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2031 c->block = gfc_get_code ();
2033 c->op = EXEC_SELECT;
2035 new_case = gfc_get_case ();
2036 new_case->high = new_case->low = gfc_int_expr (i);
2037 c->ext.case_list = new_case;
2039 c->next = gfc_get_code ();
2040 c->next->op = EXEC_GOTO;
2041 c->next->label = a->label;
2045 new_st.op = EXEC_CALL;
2046 new_st.symtree = st;
2047 new_st.ext.actual = arglist;
2052 gfc_syntax_error (ST_CALL);
2055 gfc_free_actual_arglist (arglist);
2060 /* Match an IMPLICIT NONE statement. Actually, this statement is
2061 already matched in parse.c, or we would not end up here in the
2062 first place. So the only thing we need to check, is if there is
2063 trailing garbage. If not, the match is successful. */
2066 gfc_match_implicit_none (void)
2069 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2073 /* Match the letter range(s) of an IMPLICIT statement. */
2076 match_implicit_range (gfc_typespec * ts)
2078 int c, c1, c2, inner;
2081 cur_loc = *gfc_current_locus ();
2083 gfc_gobble_whitespace ();
2084 c = gfc_next_char ();
2087 gfc_error ("Missing character range in IMPLICIT at %C");
2094 gfc_gobble_whitespace ();
2095 c1 = gfc_next_char ();
2099 gfc_gobble_whitespace ();
2100 c = gfc_next_char ();
2105 inner = 0; /* Fall through */
2112 gfc_gobble_whitespace ();
2113 c2 = gfc_next_char ();
2117 gfc_gobble_whitespace ();
2118 c = gfc_next_char ();
2120 if ((c != ',') && (c != ')'))
2133 gfc_error ("Letters must be in alphabetic order in "
2134 "IMPLICIT statement at %C");
2138 /* See if we can add the newly matched range to the pending
2139 implicits from this IMPLICIT statement. We do not check for
2140 conflicts with whatever earlier IMPLICIT statements may have
2141 set. This is done when we've successfully finished matching
2143 if (gfc_add_new_implicit_range (c1, c2, ts) != SUCCESS)
2150 gfc_syntax_error (ST_IMPLICIT);
2152 gfc_set_locus (&cur_loc);
2157 /* Match an IMPLICIT statement, storing the types for
2158 gfc_set_implicit() if the statement is accepted by the parser.
2159 There is a strange looking, but legal syntactic construction
2160 possible. It looks like:
2162 IMPLICIT INTEGER (a-b) (c-d)
2164 This is legal if "a-b" is a constant expression that happens to
2165 equal one of the legal kinds for integers. The real problem
2166 happens with an implicit specification that looks like:
2168 IMPLICIT INTEGER (a-b)
2170 In this case, a typespec matcher that is "greedy" (as most of the
2171 matchers are) gobbles the character range as a kindspec, leaving
2172 nothing left. We therefore have to go a bit more slowly in the
2173 matching process by inhibiting the kindspec checking during
2174 typespec matching and checking for a kind later. */
2177 gfc_match_implicit (void)
2184 /* We don't allow empty implicit statements. */
2185 if (gfc_match_eos () == MATCH_YES)
2187 gfc_error ("Empty IMPLICIT statement at %C");
2191 /* First cleanup. */
2192 gfc_clear_new_implicit ();
2196 /* A basic type is mandatory here. */
2197 m = gfc_match_type_spec (&ts, 0);
2198 if (m == MATCH_ERROR)
2203 cur_loc = *gfc_current_locus ();
2204 m = match_implicit_range (&ts);
2208 /* Looks like we have the <TYPE> (<RANGE>). */
2209 gfc_gobble_whitespace ();
2210 c = gfc_next_char ();
2211 if ((c == '\n') || (c == ','))
2214 gfc_set_locus (&cur_loc);
2217 /* Last chance -- check <TYPE> (<KIND>) (<RANGE>). */
2218 m = gfc_match_kind_spec (&ts);
2219 if (m == MATCH_ERROR)
2223 m = gfc_match_old_kind_spec (&ts);
2224 if (m == MATCH_ERROR)
2230 m = match_implicit_range (&ts);
2231 if (m == MATCH_ERROR)
2236 gfc_gobble_whitespace ();
2237 c = gfc_next_char ();
2238 if ((c != '\n') && (c != ','))
2244 /* All we need to now is try to merge the new implicit types back
2245 into the existing types. This will fail if another implicit
2246 type is already defined for a letter. */
2247 return (gfc_merge_new_implicit () == SUCCESS) ?
2248 MATCH_YES : MATCH_ERROR;
2251 gfc_syntax_error (ST_IMPLICIT);
2258 /* Match a common block name. */
2261 match_common_name (gfc_symbol ** sym)
2265 if (gfc_match_char ('/') == MATCH_NO)
2268 if (gfc_match_char ('/') == MATCH_YES)
2274 m = gfc_match_symbol (sym, 0);
2276 if (m == MATCH_ERROR)
2278 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2281 gfc_error ("Syntax error in common block name at %C");
2286 /* Match a COMMON statement. */
2289 gfc_match_common (void)
2291 gfc_symbol *sym, *common_name, **head, *tail, *old_blank_common;
2295 old_blank_common = gfc_current_ns->blank_common;
2296 if (old_blank_common)
2298 while (old_blank_common->common_next)
2299 old_blank_common = old_blank_common->common_next;
2305 if (gfc_match_eos () == MATCH_YES)
2310 m = match_common_name (&common_name);
2311 if (m == MATCH_ERROR)
2314 if (common_name == NULL)
2315 head = &gfc_current_ns->blank_common;
2318 head = &common_name->common_head;
2320 if (!common_name->attr.common
2321 && gfc_add_common (&common_name->attr, NULL) == FAILURE)
2330 while (tail->common_next)
2331 tail = tail->common_next;
2334 /* Grab the list of symbols. */
2337 m = gfc_match_symbol (&sym, 0);
2338 if (m == MATCH_ERROR)
2343 if (sym->attr.in_common)
2345 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2350 if (gfc_add_in_common (&sym->attr, NULL) == FAILURE)
2353 /* Derived type names must have the SEQUENCE attribute. */
2354 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2357 ("Derived type variable in COMMON at %C does not have the "
2358 "SEQUENCE attribute");
2363 tail->common_next = sym;
2369 /* Deal with an optional array specification after the
2371 m = gfc_match_array_spec (&as);
2372 if (m == MATCH_ERROR)
2377 if (as->type != AS_EXPLICIT)
2380 ("Array specification for symbol '%s' in COMMON at %C "
2381 "must be explicit", sym->name);
2385 if (gfc_add_dimension (&sym->attr, NULL) == FAILURE)
2388 if (sym->attr.pointer)
2391 ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2400 if (gfc_match_eos () == MATCH_YES)
2402 if (gfc_peek_char () == '/')
2404 if (gfc_match_char (',') != MATCH_YES)
2406 if (gfc_peek_char () == '/')
2415 gfc_syntax_error (ST_COMMON);
2418 if (old_blank_common)
2419 old_blank_common->common_next = NULL;
2421 gfc_current_ns->blank_common = NULL;
2422 gfc_free_array_spec (as);
2427 /* Match a BLOCK DATA program unit. */
2430 gfc_match_block_data (void)
2432 char name[GFC_MAX_SYMBOL_LEN + 1];
2436 if (gfc_match_eos () == MATCH_YES)
2438 gfc_new_block = NULL;
2442 m = gfc_match (" %n%t", name);
2446 if (gfc_get_symbol (name, NULL, &sym))
2449 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, NULL) == FAILURE)
2452 gfc_new_block = sym;
2458 /* Free a namelist structure. */
2461 gfc_free_namelist (gfc_namelist * name)
2465 for (; name; name = n)
2473 /* Match a NAMELIST statement. */
2476 gfc_match_namelist (void)
2478 gfc_symbol *group_name, *sym;
2482 m = gfc_match (" / %s /", &group_name);
2485 if (m == MATCH_ERROR)
2490 if (group_name->ts.type != BT_UNKNOWN)
2493 ("Namelist group name '%s' at %C already has a basic type "
2494 "of %s", group_name->name, gfc_typename (&group_name->ts));
2498 if (group_name->attr.flavor != FL_NAMELIST
2499 && gfc_add_flavor (&group_name->attr, FL_NAMELIST, NULL) == FAILURE)
2504 m = gfc_match_symbol (&sym, 1);
2507 if (m == MATCH_ERROR)
2510 if (sym->attr.in_namelist == 0
2511 && gfc_add_in_namelist (&sym->attr, NULL) == FAILURE)
2514 /* TODO: worry about PRIVATE members of a PUBLIC namelist
2517 nl = gfc_get_namelist ();
2520 if (group_name->namelist == NULL)
2521 group_name->namelist = group_name->namelist_tail = nl;
2524 group_name->namelist_tail->next = nl;
2525 group_name->namelist_tail = nl;
2528 if (gfc_match_eos () == MATCH_YES)
2531 m = gfc_match_char (',');
2533 if (gfc_match_char ('/') == MATCH_YES)
2535 m2 = gfc_match (" %s /", &group_name);
2536 if (m2 == MATCH_YES)
2538 if (m2 == MATCH_ERROR)
2552 gfc_syntax_error (ST_NAMELIST);
2559 /* Match a MODULE statement. */
2562 gfc_match_module (void)
2566 m = gfc_match (" %s%t", &gfc_new_block);
2570 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE, NULL) == FAILURE)
2577 /* Free equivalence sets and lists. Recursively is the easiest way to
2581 gfc_free_equiv (gfc_equiv * eq)
2587 gfc_free_equiv (eq->eq);
2588 gfc_free_equiv (eq->next);
2590 gfc_free_expr (eq->expr);
2595 /* Match an EQUIVALENCE statement. */
2598 gfc_match_equivalence (void)
2600 gfc_equiv *eq, *set, *tail;
2608 eq = gfc_get_equiv ();
2612 eq->next = gfc_current_ns->equiv;
2613 gfc_current_ns->equiv = eq;
2615 if (gfc_match_char ('(') != MATCH_YES)
2622 m = gfc_match_variable (&set->expr, 1);
2623 if (m == MATCH_ERROR)
2628 for (ref = set->expr->ref; ref; ref = ref->next)
2629 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2632 ("Array reference in EQUIVALENCE at %C cannot be an "
2637 if (gfc_match_char (')') == MATCH_YES)
2639 if (gfc_match_char (',') != MATCH_YES)
2642 set->eq = gfc_get_equiv ();
2646 if (gfc_match_eos () == MATCH_YES)
2648 if (gfc_match_char (',') != MATCH_YES)
2655 gfc_syntax_error (ST_EQUIVALENCE);
2661 gfc_free_equiv (gfc_current_ns->equiv);
2662 gfc_current_ns->equiv = eq;
2668 /* Match a statement function declaration. It is so easy to match
2669 non-statement function statements with a MATCH_ERROR as opposed to
2670 MATCH_NO that we suppress error message in most cases. */
2673 gfc_match_st_function (void)
2675 gfc_error_buf old_error;
2680 m = gfc_match_symbol (&sym, 0);
2684 gfc_push_error (&old_error);
2686 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, NULL) == FAILURE)
2689 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2692 m = gfc_match (" = %e%t", &expr);
2695 if (m == MATCH_ERROR)
2703 gfc_pop_error (&old_error);
2708 /********************* DATA statement subroutines *********************/
2710 /* Free a gfc_data_variable structure and everything beneath it. */
2713 free_variable (gfc_data_variable * p)
2715 gfc_data_variable *q;
2720 gfc_free_expr (p->expr);
2721 gfc_free_iterator (&p->iter, 0);
2722 free_variable (p->list);
2729 /* Free a gfc_data_value structure and everything beneath it. */
2732 free_value (gfc_data_value * p)
2739 gfc_free_expr (p->expr);
2745 /* Free a list of gfc_data structures. */
2748 gfc_free_data (gfc_data * p)
2756 free_variable (p->var);
2757 free_value (p->value);
2764 static match var_element (gfc_data_variable *);
2766 /* Match a list of variables terminated by an iterator and a right
2770 var_list (gfc_data_variable * parent)
2772 gfc_data_variable *tail, var;
2775 m = var_element (&var);
2776 if (m == MATCH_ERROR)
2781 tail = gfc_get_data_variable ();
2784 parent->list = tail;
2788 if (gfc_match_char (',') != MATCH_YES)
2791 m = gfc_match_iterator (&parent->iter, 1);
2794 if (m == MATCH_ERROR)
2797 m = var_element (&var);
2798 if (m == MATCH_ERROR)
2803 tail->next = gfc_get_data_variable ();
2809 if (gfc_match_char (')') != MATCH_YES)
2814 gfc_syntax_error (ST_DATA);
2819 /* Match a single element in a data variable list, which can be a
2820 variable-iterator list. */
2823 var_element (gfc_data_variable * new)
2827 memset (new, '\0', sizeof (gfc_data_variable));
2829 if (gfc_match_char ('(') == MATCH_YES)
2830 return var_list (new);
2832 m = gfc_match_variable (&new->expr, 0);
2836 if (new->expr->symtree->n.sym->value != NULL)
2838 gfc_error ("Variable '%s' at %C already has an initialization",
2839 new->expr->symtree->n.sym->name);
2843 new->expr->symtree->n.sym->attr.data = 1;
2848 /* Match the top-level list of data variables. */
2851 top_var_list (gfc_data * d)
2853 gfc_data_variable var, *tail, *new;
2860 m = var_element (&var);
2863 if (m == MATCH_ERROR)
2866 new = gfc_get_data_variable ();
2876 if (gfc_match_char ('/') == MATCH_YES)
2878 if (gfc_match_char (',') != MATCH_YES)
2885 gfc_syntax_error (ST_DATA);
2891 match_data_constant (gfc_expr ** result)
2893 char name[GFC_MAX_SYMBOL_LEN + 1];
2898 m = gfc_match_literal_constant (&expr, 1);
2905 if (m == MATCH_ERROR)
2908 m = gfc_match_null (result);
2912 m = gfc_match_name (name);
2916 if (gfc_find_symbol (name, NULL, 1, &sym))
2919 if (sym == NULL || sym->attr.flavor != FL_PARAMETER)
2921 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
2926 *result = gfc_copy_expr (sym->value);
2931 /* Match a list of values in a DATA statement. The leading '/' has
2932 already been seen at this point. */
2935 top_val_list (gfc_data * data)
2937 gfc_data_value *new, *tail;
2946 m = match_data_constant (&expr);
2949 if (m == MATCH_ERROR)
2952 new = gfc_get_data_value ();
2961 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
2968 msg = gfc_extract_int (expr, &tail->repeat);
2969 gfc_free_expr (expr);
2976 m = match_data_constant (&tail->expr);
2979 if (m == MATCH_ERROR)
2983 if (gfc_match_char ('/') == MATCH_YES)
2985 if (gfc_match_char (',') == MATCH_NO)
2992 gfc_syntax_error (ST_DATA);
2997 /* Match a DATA statement. */
3000 gfc_match_data (void)
3007 new = gfc_get_data ();
3008 new->where = *gfc_current_locus ();
3010 m = top_var_list (new);
3014 m = top_val_list (new);
3018 new->next = gfc_current_ns->data;
3019 gfc_current_ns->data = new;
3021 if (gfc_match_eos () == MATCH_YES)
3024 gfc_match_char (','); /* Optional comma */
3027 if (gfc_pure (NULL))
3029 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
3036 gfc_free_data (new);
3041 /***************** SELECT CASE subroutines ******************/
3043 /* Free a single case structure. */
3046 free_case (gfc_case * p)
3048 if (p->low == p->high)
3050 gfc_free_expr (p->low);
3051 gfc_free_expr (p->high);
3056 /* Free a list of case structures. */
3059 gfc_free_case_list (gfc_case * p)
3071 /* Match a single case selector. */
3074 match_case_selector (gfc_case ** cp)
3079 c = gfc_get_case ();
3080 c->where = *gfc_current_locus ();
3082 if (gfc_match_char (':') == MATCH_YES)
3084 m = gfc_match_expr (&c->high);
3087 if (m == MATCH_ERROR)
3093 m = gfc_match_expr (&c->low);
3094 if (m == MATCH_ERROR)
3099 /* If we're not looking at a ':' now, make a range out of a single
3100 target. Else get the upper bound for the case range. */
3101 if (gfc_match_char (':') != MATCH_YES)
3105 m = gfc_match_expr (&c->high);
3106 if (m == MATCH_ERROR)
3108 /* MATCH_NO is fine. It's OK if nothing is there! */
3116 gfc_error ("Expected expression in CASE at %C");
3124 /* Match the end of a case statement. */
3127 match_case_eos (void)
3129 char name[GFC_MAX_SYMBOL_LEN + 1];
3132 if (gfc_match_eos () == MATCH_YES)
3135 gfc_gobble_whitespace ();
3137 m = gfc_match_name (name);
3141 if (strcmp (name, gfc_current_block ()->name) != 0)
3143 gfc_error ("Expected case name of '%s' at %C",
3144 gfc_current_block ()->name);
3148 return gfc_match_eos ();
3152 /* Match a SELECT statement. */
3155 gfc_match_select (void)
3160 m = gfc_match_label ();
3161 if (m == MATCH_ERROR)
3164 m = gfc_match (" select case ( %e )%t", &expr);
3168 new_st.op = EXEC_SELECT;
3175 /* Match a CASE statement. */
3178 gfc_match_case (void)
3180 gfc_case *c, *head, *tail;
3185 if (gfc_current_state () != COMP_SELECT)
3187 gfc_error ("Unexpected CASE statement at %C");
3191 if (gfc_match ("% default") == MATCH_YES)
3193 m = match_case_eos ();
3196 if (m == MATCH_ERROR)
3199 new_st.op = EXEC_SELECT;
3200 c = gfc_get_case ();
3201 c->where = *gfc_current_locus ();
3202 new_st.ext.case_list = c;
3206 if (gfc_match_char ('(') != MATCH_YES)
3211 if (match_case_selector (&c) == MATCH_ERROR)
3221 if (gfc_match_char (')') == MATCH_YES)
3223 if (gfc_match_char (',') != MATCH_YES)
3227 m = match_case_eos ();
3230 if (m == MATCH_ERROR)
3233 new_st.op = EXEC_SELECT;
3234 new_st.ext.case_list = head;
3239 gfc_error ("Syntax error in CASE-specification at %C");
3242 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3246 /********************* WHERE subroutines ********************/
3248 /* Match a WHERE statement. */
3251 gfc_match_where (gfc_statement * st)
3257 m0 = gfc_match_label ();
3258 if (m0 == MATCH_ERROR)
3261 m = gfc_match (" where ( %e )", &expr);
3265 if (gfc_match_eos () == MATCH_YES)
3267 *st = ST_WHERE_BLOCK;
3269 new_st.op = EXEC_WHERE;
3274 m = gfc_match_assignment ();
3276 gfc_syntax_error (ST_WHERE);
3280 gfc_free_expr (expr);
3284 /* We've got a simple WHERE statement. */
3286 c = gfc_get_code ();
3290 c->next = gfc_get_code ();
3293 gfc_clear_new_st ();
3295 new_st.op = EXEC_WHERE;
3302 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3303 new_st if successful. */
3306 gfc_match_elsewhere (void)
3308 char name[GFC_MAX_SYMBOL_LEN + 1];
3312 if (gfc_current_state () != COMP_WHERE)
3314 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3320 if (gfc_match_char ('(') == MATCH_YES)
3322 m = gfc_match_expr (&expr);
3325 if (m == MATCH_ERROR)
3328 if (gfc_match_char (')') != MATCH_YES)
3332 if (gfc_match_eos () != MATCH_YES)
3333 { /* Better be a name at this point */
3334 m = gfc_match_name (name);
3337 if (m == MATCH_ERROR)
3340 if (gfc_match_eos () != MATCH_YES)
3343 if (strcmp (name, gfc_current_block ()->name) != 0)
3345 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3346 name, gfc_current_block ()->name);
3351 new_st.op = EXEC_WHERE;
3356 gfc_syntax_error (ST_ELSEWHERE);
3359 gfc_free_expr (expr);
3364 /******************** FORALL subroutines ********************/
3366 /* Free a list of FORALL iterators. */
3369 gfc_free_forall_iterator (gfc_forall_iterator * iter)
3371 gfc_forall_iterator *next;
3377 gfc_free_expr (iter->var);
3378 gfc_free_expr (iter->start);
3379 gfc_free_expr (iter->end);
3380 gfc_free_expr (iter->stride);
3388 /* Match an iterator as part of a FORALL statement. The format is:
3390 <var> = <start>:<end>[:<stride>][, <scalar mask>] */
3393 match_forall_iterator (gfc_forall_iterator ** result)
3395 gfc_forall_iterator *iter;
3399 where = *gfc_current_locus ();
3400 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3402 m = gfc_match_variable (&iter->var, 0);
3406 if (gfc_match_char ('=') != MATCH_YES)
3412 m = gfc_match_expr (&iter->start);
3415 if (m == MATCH_ERROR)
3418 if (gfc_match_char (':') != MATCH_YES)
3421 m = gfc_match_expr (&iter->end);
3424 if (m == MATCH_ERROR)
3427 if (gfc_match_char (':') == MATCH_NO)
3428 iter->stride = gfc_int_expr (1);
3431 m = gfc_match_expr (&iter->stride);
3434 if (m == MATCH_ERROR)
3442 gfc_error ("Syntax error in FORALL iterator at %C");
3446 gfc_set_locus (&where);
3447 gfc_free_forall_iterator (iter);
3452 /* Match a FORALL statement. */
3455 gfc_match_forall (gfc_statement * st)
3457 gfc_forall_iterator *head, *tail, *new;
3466 m0 = gfc_match_label ();
3467 if (m0 == MATCH_ERROR)
3470 m = gfc_match (" forall (");
3474 m = match_forall_iterator (&new);
3475 if (m == MATCH_ERROR)
3484 if (gfc_match_char (',') != MATCH_YES)
3487 m = match_forall_iterator (&new);
3488 if (m == MATCH_ERROR)
3497 /* Have to have a mask expression. */
3498 m = gfc_match_expr (&mask);
3501 if (m == MATCH_ERROR)
3507 if (gfc_match_char (')') == MATCH_NO)
3510 if (gfc_match_eos () == MATCH_YES)
3512 *st = ST_FORALL_BLOCK;
3514 new_st.op = EXEC_FORALL;
3516 new_st.ext.forall_iterator = head;
3521 m = gfc_match_assignment ();
3522 if (m == MATCH_ERROR)
3526 m = gfc_match_pointer_assignment ();
3527 if (m == MATCH_ERROR)
3533 c = gfc_get_code ();
3536 if (gfc_match_eos () != MATCH_YES)
3539 gfc_clear_new_st ();
3540 new_st.op = EXEC_FORALL;
3542 new_st.ext.forall_iterator = head;
3543 new_st.block = gfc_get_code ();
3545 new_st.block->op = EXEC_FORALL;
3546 new_st.block->next = c;
3552 gfc_syntax_error (ST_FORALL);
3555 gfc_free_forall_iterator (head);
3556 gfc_free_expr (mask);
3557 gfc_free_statements (c);