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_form == FORM_FIXED)
83 old_loc = gfc_current_locus;
86 if (!gfc_is_whitespace (c))
88 gfc_current_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_current_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_current_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_current_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_current_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_form == FORM_FREE)
341 && gfc_is_whitespace (c))
359 match_loc = gfc_current_locus;
367 gfc_current_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_current_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_current_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_current_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_current_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_current_locus = old_loc;
754 va_start (argp, target);
757 for (; matches > 0; matches--)
767 /* Matches that don't have to be undone */
772 (void)va_arg (argp, void **);
777 vp = va_arg (argp, void **);
791 /*********************** Statement level matching **********************/
793 /* Matches the start of a program unit, which is the program keyword
794 followed by an obligatory symbol. */
797 gfc_match_program (void)
802 m = gfc_match ("% %s%t", &sym);
806 gfc_error ("Invalid form of PROGRAM statement at %C");
810 if (m == MATCH_ERROR)
813 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, NULL) == FAILURE)
822 /* Match a simple assignment statement. */
825 gfc_match_assignment (void)
827 gfc_expr *lvalue, *rvalue;
831 old_loc = gfc_current_locus;
833 lvalue = rvalue = NULL;
834 m = gfc_match (" %v =", &lvalue);
838 if (lvalue->symtree->n.sym->attr.flavor == FL_PARAMETER)
840 gfc_error ("Cannot assign to a PARAMETER variable at %C");
845 m = gfc_match (" %e%t", &rvalue);
849 gfc_set_sym_referenced (lvalue->symtree->n.sym);
851 new_st.op = EXEC_ASSIGN;
852 new_st.expr = lvalue;
853 new_st.expr2 = rvalue;
855 gfc_check_do_variable (lvalue->symtree);
860 gfc_current_locus = old_loc;
861 gfc_free_expr (lvalue);
862 gfc_free_expr (rvalue);
867 /* Match a pointer assignment statement. */
870 gfc_match_pointer_assignment (void)
872 gfc_expr *lvalue, *rvalue;
876 old_loc = gfc_current_locus;
878 lvalue = rvalue = NULL;
880 m = gfc_match (" %v =>", &lvalue);
887 m = gfc_match (" %e%t", &rvalue);
891 new_st.op = EXEC_POINTER_ASSIGN;
892 new_st.expr = lvalue;
893 new_st.expr2 = rvalue;
898 gfc_current_locus = old_loc;
899 gfc_free_expr (lvalue);
900 gfc_free_expr (rvalue);
905 /* The IF statement is a bit of a pain. First of all, there are three
906 forms of it, the simple IF, the IF that starts a block and the
909 There is a problem with the simple IF and that is the fact that we
910 only have a single level of undo information on symbols. What this
911 means is for a simple IF, we must re-match the whole IF statement
912 multiple times in order to guarantee that the symbol table ends up
913 in the proper state. */
916 gfc_match_if (gfc_statement * if_type)
919 gfc_st_label *l1, *l2, *l3;
924 n = gfc_match_label ();
925 if (n == MATCH_ERROR)
928 old_loc = gfc_current_locus;
930 m = gfc_match (" if ( %e", &expr);
934 if (gfc_match_char (')') != MATCH_YES)
936 gfc_error ("Syntax error in IF-expression at %C");
937 gfc_free_expr (expr);
941 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
948 ("Block label not appropriate for arithmetic IF statement "
951 gfc_free_expr (expr);
955 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
956 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
957 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
960 gfc_free_expr (expr);
964 new_st.op = EXEC_ARITHMETIC_IF;
970 *if_type = ST_ARITHMETIC_IF;
974 if (gfc_match (" then %t") == MATCH_YES)
979 *if_type = ST_IF_BLOCK;
985 gfc_error ("Block label is not appropriate IF statement at %C");
987 gfc_free_expr (expr);
991 /* At this point the only thing left is a simple IF statement. At
992 this point, n has to be MATCH_NO, so we don't have to worry about
993 re-matching a block label. From what we've got so far, try
994 matching an assignment. */
996 *if_type = ST_SIMPLE_IF;
998 m = gfc_match_assignment ();
1002 gfc_free_expr (expr);
1003 gfc_undo_symbols ();
1004 gfc_current_locus = old_loc;
1006 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1008 m = gfc_match_pointer_assignment ();
1012 gfc_free_expr (expr);
1013 gfc_undo_symbols ();
1014 gfc_current_locus = old_loc;
1016 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match */
1018 /* Look at the next keyword to see which matcher to call. Matching
1019 the keyword doesn't affect the symbol table, so we don't have to
1020 restore between tries. */
1022 #define match(string, subr, statement) \
1023 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1027 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
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 ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
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 ("pause", gfc_match_stop, ST_PAUSE)
1047 match ("stop", gfc_match_stop, ST_STOP)
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_char (')') == 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 non-zero, 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 if (gfc_match_eos () == MATCH_YES)
2307 if (gfc_peek_char () == '/')
2309 if (gfc_match_char (',') != MATCH_YES)
2311 if (gfc_peek_char () == '/')
2320 gfc_syntax_error (ST_COMMON);
2323 if (old_blank_common)
2324 old_blank_common->common_next = NULL;
2326 gfc_current_ns->blank_common.head = NULL;
2327 gfc_free_array_spec (as);
2332 /* Match a BLOCK DATA program unit. */
2335 gfc_match_block_data (void)
2337 char name[GFC_MAX_SYMBOL_LEN + 1];
2341 if (gfc_match_eos () == MATCH_YES)
2343 gfc_new_block = NULL;
2347 m = gfc_match ("% %n%t", name);
2351 if (gfc_get_symbol (name, NULL, &sym))
2354 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, NULL) == FAILURE)
2357 gfc_new_block = sym;
2363 /* Free a namelist structure. */
2366 gfc_free_namelist (gfc_namelist * name)
2370 for (; name; name = n)
2378 /* Match a NAMELIST statement. */
2381 gfc_match_namelist (void)
2383 gfc_symbol *group_name, *sym;
2387 m = gfc_match (" / %s /", &group_name);
2390 if (m == MATCH_ERROR)
2395 if (group_name->ts.type != BT_UNKNOWN)
2398 ("Namelist group name '%s' at %C already has a basic type "
2399 "of %s", group_name->name, gfc_typename (&group_name->ts));
2403 if (group_name->attr.flavor != FL_NAMELIST
2404 && gfc_add_flavor (&group_name->attr, FL_NAMELIST, NULL) == FAILURE)
2409 m = gfc_match_symbol (&sym, 1);
2412 if (m == MATCH_ERROR)
2415 if (sym->attr.in_namelist == 0
2416 && gfc_add_in_namelist (&sym->attr, NULL) == FAILURE)
2419 /* TODO: worry about PRIVATE members of a PUBLIC namelist
2422 nl = gfc_get_namelist ();
2425 if (group_name->namelist == NULL)
2426 group_name->namelist = group_name->namelist_tail = nl;
2429 group_name->namelist_tail->next = nl;
2430 group_name->namelist_tail = nl;
2433 if (gfc_match_eos () == MATCH_YES)
2436 m = gfc_match_char (',');
2438 if (gfc_match_char ('/') == MATCH_YES)
2440 m2 = gfc_match (" %s /", &group_name);
2441 if (m2 == MATCH_YES)
2443 if (m2 == MATCH_ERROR)
2457 gfc_syntax_error (ST_NAMELIST);
2464 /* Match a MODULE statement. */
2467 gfc_match_module (void)
2471 m = gfc_match (" %s%t", &gfc_new_block);
2475 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE, NULL) == FAILURE)
2482 /* Free equivalence sets and lists. Recursively is the easiest way to
2486 gfc_free_equiv (gfc_equiv * eq)
2492 gfc_free_equiv (eq->eq);
2493 gfc_free_equiv (eq->next);
2495 gfc_free_expr (eq->expr);
2500 /* Match an EQUIVALENCE statement. */
2503 gfc_match_equivalence (void)
2505 gfc_equiv *eq, *set, *tail;
2513 eq = gfc_get_equiv ();
2517 eq->next = gfc_current_ns->equiv;
2518 gfc_current_ns->equiv = eq;
2520 if (gfc_match_char ('(') != MATCH_YES)
2527 m = gfc_match_variable (&set->expr, 1);
2528 if (m == MATCH_ERROR)
2533 for (ref = set->expr->ref; ref; ref = ref->next)
2534 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2537 ("Array reference in EQUIVALENCE at %C cannot be an "
2542 if (gfc_match_char (')') == MATCH_YES)
2544 if (gfc_match_char (',') != MATCH_YES)
2547 set->eq = gfc_get_equiv ();
2551 if (gfc_match_eos () == MATCH_YES)
2553 if (gfc_match_char (',') != MATCH_YES)
2560 gfc_syntax_error (ST_EQUIVALENCE);
2566 gfc_free_equiv (gfc_current_ns->equiv);
2567 gfc_current_ns->equiv = eq;
2573 /* Match a statement function declaration. It is so easy to match
2574 non-statement function statements with a MATCH_ERROR as opposed to
2575 MATCH_NO that we suppress error message in most cases. */
2578 gfc_match_st_function (void)
2580 gfc_error_buf old_error;
2585 m = gfc_match_symbol (&sym, 0);
2589 gfc_push_error (&old_error);
2591 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, NULL) == FAILURE)
2594 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2597 m = gfc_match (" = %e%t", &expr);
2600 if (m == MATCH_ERROR)
2608 gfc_pop_error (&old_error);
2613 /********************* DATA statement subroutines *********************/
2615 /* Free a gfc_data_variable structure and everything beneath it. */
2618 free_variable (gfc_data_variable * p)
2620 gfc_data_variable *q;
2625 gfc_free_expr (p->expr);
2626 gfc_free_iterator (&p->iter, 0);
2627 free_variable (p->list);
2634 /* Free a gfc_data_value structure and everything beneath it. */
2637 free_value (gfc_data_value * p)
2644 gfc_free_expr (p->expr);
2650 /* Free a list of gfc_data structures. */
2653 gfc_free_data (gfc_data * p)
2661 free_variable (p->var);
2662 free_value (p->value);
2669 static match var_element (gfc_data_variable *);
2671 /* Match a list of variables terminated by an iterator and a right
2675 var_list (gfc_data_variable * parent)
2677 gfc_data_variable *tail, var;
2680 m = var_element (&var);
2681 if (m == MATCH_ERROR)
2686 tail = gfc_get_data_variable ();
2689 parent->list = tail;
2693 if (gfc_match_char (',') != MATCH_YES)
2696 m = gfc_match_iterator (&parent->iter, 1);
2699 if (m == MATCH_ERROR)
2702 m = var_element (&var);
2703 if (m == MATCH_ERROR)
2708 tail->next = gfc_get_data_variable ();
2714 if (gfc_match_char (')') != MATCH_YES)
2719 gfc_syntax_error (ST_DATA);
2724 /* Match a single element in a data variable list, which can be a
2725 variable-iterator list. */
2728 var_element (gfc_data_variable * new)
2733 memset (new, '\0', sizeof (gfc_data_variable));
2735 if (gfc_match_char ('(') == MATCH_YES)
2736 return var_list (new);
2738 m = gfc_match_variable (&new->expr, 0);
2742 sym = new->expr->symtree->n.sym;
2744 if(sym->value != NULL)
2746 gfc_error ("Variable '%s' at %C already has an initialization",
2751 #if 0 // TODO: Find out where to move this message
2752 if (sym->attr.in_common)
2753 /* See if sym is in the blank common block. */
2754 for (t = &sym->ns->blank_common; t; t = t->common_next)
2757 gfc_error ("DATA statement at %C may not initialize variable "
2758 "'%s' from blank COMMON", sym->name);
2763 if (gfc_add_data (&sym->attr, &new->expr->where) == FAILURE)
2770 /* Match the top-level list of data variables. */
2773 top_var_list (gfc_data * d)
2775 gfc_data_variable var, *tail, *new;
2782 m = var_element (&var);
2785 if (m == MATCH_ERROR)
2788 new = gfc_get_data_variable ();
2798 if (gfc_match_char ('/') == MATCH_YES)
2800 if (gfc_match_char (',') != MATCH_YES)
2807 gfc_syntax_error (ST_DATA);
2813 match_data_constant (gfc_expr ** result)
2815 char name[GFC_MAX_SYMBOL_LEN + 1];
2820 m = gfc_match_literal_constant (&expr, 1);
2827 if (m == MATCH_ERROR)
2830 m = gfc_match_null (result);
2834 m = gfc_match_name (name);
2838 if (gfc_find_symbol (name, NULL, 1, &sym))
2842 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
2844 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
2848 else if (sym->attr.flavor == FL_DERIVED)
2849 return gfc_match_structure_constructor (sym, result);
2851 *result = gfc_copy_expr (sym->value);
2856 /* Match a list of values in a DATA statement. The leading '/' has
2857 already been seen at this point. */
2860 top_val_list (gfc_data * data)
2862 gfc_data_value *new, *tail;
2871 m = match_data_constant (&expr);
2874 if (m == MATCH_ERROR)
2877 new = gfc_get_data_value ();
2886 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
2893 msg = gfc_extract_int (expr, &tail->repeat);
2894 gfc_free_expr (expr);
2901 m = match_data_constant (&tail->expr);
2904 if (m == MATCH_ERROR)
2908 if (gfc_match_char ('/') == MATCH_YES)
2910 if (gfc_match_char (',') == MATCH_NO)
2917 gfc_syntax_error (ST_DATA);
2922 /* Match a DATA statement. */
2925 gfc_match_data (void)
2932 new = gfc_get_data ();
2933 new->where = gfc_current_locus;
2935 m = top_var_list (new);
2939 m = top_val_list (new);
2943 new->next = gfc_current_ns->data;
2944 gfc_current_ns->data = new;
2946 if (gfc_match_eos () == MATCH_YES)
2949 gfc_match_char (','); /* Optional comma */
2952 if (gfc_pure (NULL))
2954 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
2961 gfc_free_data (new);
2966 /***************** SELECT CASE subroutines ******************/
2968 /* Free a single case structure. */
2971 free_case (gfc_case * p)
2973 if (p->low == p->high)
2975 gfc_free_expr (p->low);
2976 gfc_free_expr (p->high);
2981 /* Free a list of case structures. */
2984 gfc_free_case_list (gfc_case * p)
2996 /* Match a single case selector. */
2999 match_case_selector (gfc_case ** cp)
3004 c = gfc_get_case ();
3005 c->where = gfc_current_locus;
3007 if (gfc_match_char (':') == MATCH_YES)
3009 m = gfc_match_init_expr (&c->high);
3012 if (m == MATCH_ERROR)
3018 m = gfc_match_init_expr (&c->low);
3019 if (m == MATCH_ERROR)
3024 /* If we're not looking at a ':' now, make a range out of a single
3025 target. Else get the upper bound for the case range. */
3026 if (gfc_match_char (':') != MATCH_YES)
3030 m = gfc_match_init_expr (&c->high);
3031 if (m == MATCH_ERROR)
3033 /* MATCH_NO is fine. It's OK if nothing is there! */
3041 gfc_error ("Expected initialization expression in CASE at %C");
3049 /* Match the end of a case statement. */
3052 match_case_eos (void)
3054 char name[GFC_MAX_SYMBOL_LEN + 1];
3057 if (gfc_match_eos () == MATCH_YES)
3060 gfc_gobble_whitespace ();
3062 m = gfc_match_name (name);
3066 if (strcmp (name, gfc_current_block ()->name) != 0)
3068 gfc_error ("Expected case name of '%s' at %C",
3069 gfc_current_block ()->name);
3073 return gfc_match_eos ();
3077 /* Match a SELECT statement. */
3080 gfc_match_select (void)
3085 m = gfc_match_label ();
3086 if (m == MATCH_ERROR)
3089 m = gfc_match (" select case ( %e )%t", &expr);
3093 new_st.op = EXEC_SELECT;
3100 /* Match a CASE statement. */
3103 gfc_match_case (void)
3105 gfc_case *c, *head, *tail;
3110 if (gfc_current_state () != COMP_SELECT)
3112 gfc_error ("Unexpected CASE statement at %C");
3116 if (gfc_match ("% default") == MATCH_YES)
3118 m = match_case_eos ();
3121 if (m == MATCH_ERROR)
3124 new_st.op = EXEC_SELECT;
3125 c = gfc_get_case ();
3126 c->where = gfc_current_locus;
3127 new_st.ext.case_list = c;
3131 if (gfc_match_char ('(') != MATCH_YES)
3136 if (match_case_selector (&c) == MATCH_ERROR)
3146 if (gfc_match_char (')') == MATCH_YES)
3148 if (gfc_match_char (',') != MATCH_YES)
3152 m = match_case_eos ();
3155 if (m == MATCH_ERROR)
3158 new_st.op = EXEC_SELECT;
3159 new_st.ext.case_list = head;
3164 gfc_error ("Syntax error in CASE-specification at %C");
3167 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3171 /********************* WHERE subroutines ********************/
3173 /* Match a WHERE statement. */
3176 gfc_match_where (gfc_statement * st)
3182 m0 = gfc_match_label ();
3183 if (m0 == MATCH_ERROR)
3186 m = gfc_match (" where ( %e )", &expr);
3190 if (gfc_match_eos () == MATCH_YES)
3192 *st = ST_WHERE_BLOCK;
3194 new_st.op = EXEC_WHERE;
3199 m = gfc_match_assignment ();
3201 gfc_syntax_error (ST_WHERE);
3205 gfc_free_expr (expr);
3209 /* We've got a simple WHERE statement. */
3211 c = gfc_get_code ();
3215 c->next = gfc_get_code ();
3218 gfc_clear_new_st ();
3220 new_st.op = EXEC_WHERE;
3227 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3228 new_st if successful. */
3231 gfc_match_elsewhere (void)
3233 char name[GFC_MAX_SYMBOL_LEN + 1];
3237 if (gfc_current_state () != COMP_WHERE)
3239 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3245 if (gfc_match_char ('(') == MATCH_YES)
3247 m = gfc_match_expr (&expr);
3250 if (m == MATCH_ERROR)
3253 if (gfc_match_char (')') != MATCH_YES)
3257 if (gfc_match_eos () != MATCH_YES)
3258 { /* Better be a name at this point */
3259 m = gfc_match_name (name);
3262 if (m == MATCH_ERROR)
3265 if (gfc_match_eos () != MATCH_YES)
3268 if (strcmp (name, gfc_current_block ()->name) != 0)
3270 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3271 name, gfc_current_block ()->name);
3276 new_st.op = EXEC_WHERE;
3281 gfc_syntax_error (ST_ELSEWHERE);
3284 gfc_free_expr (expr);
3289 /******************** FORALL subroutines ********************/
3291 /* Free a list of FORALL iterators. */
3294 gfc_free_forall_iterator (gfc_forall_iterator * iter)
3296 gfc_forall_iterator *next;
3302 gfc_free_expr (iter->var);
3303 gfc_free_expr (iter->start);
3304 gfc_free_expr (iter->end);
3305 gfc_free_expr (iter->stride);
3313 /* Match an iterator as part of a FORALL statement. The format is:
3315 <var> = <start>:<end>[:<stride>][, <scalar mask>] */
3318 match_forall_iterator (gfc_forall_iterator ** result)
3320 gfc_forall_iterator *iter;
3324 where = gfc_current_locus;
3325 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3327 m = gfc_match_variable (&iter->var, 0);
3331 if (gfc_match_char ('=') != MATCH_YES)
3337 m = gfc_match_expr (&iter->start);
3340 if (m == MATCH_ERROR)
3343 if (gfc_match_char (':') != MATCH_YES)
3346 m = gfc_match_expr (&iter->end);
3349 if (m == MATCH_ERROR)
3352 if (gfc_match_char (':') == MATCH_NO)
3353 iter->stride = gfc_int_expr (1);
3356 m = gfc_match_expr (&iter->stride);
3359 if (m == MATCH_ERROR)
3367 gfc_error ("Syntax error in FORALL iterator at %C");
3371 gfc_current_locus = where;
3372 gfc_free_forall_iterator (iter);
3377 /* Match a FORALL statement. */
3380 gfc_match_forall (gfc_statement * st)
3382 gfc_forall_iterator *head, *tail, *new;
3391 m0 = gfc_match_label ();
3392 if (m0 == MATCH_ERROR)
3395 m = gfc_match (" forall (");
3399 m = match_forall_iterator (&new);
3400 if (m == MATCH_ERROR)
3409 if (gfc_match_char (',') != MATCH_YES)
3412 m = match_forall_iterator (&new);
3413 if (m == MATCH_ERROR)
3422 /* Have to have a mask expression. */
3423 m = gfc_match_expr (&mask);
3426 if (m == MATCH_ERROR)
3432 if (gfc_match_char (')') == MATCH_NO)
3435 if (gfc_match_eos () == MATCH_YES)
3437 *st = ST_FORALL_BLOCK;
3439 new_st.op = EXEC_FORALL;
3441 new_st.ext.forall_iterator = head;
3446 m = gfc_match_assignment ();
3447 if (m == MATCH_ERROR)
3451 m = gfc_match_pointer_assignment ();
3452 if (m == MATCH_ERROR)
3458 c = gfc_get_code ();
3461 if (gfc_match_eos () != MATCH_YES)
3464 gfc_clear_new_st ();
3465 new_st.op = EXEC_FORALL;
3467 new_st.ext.forall_iterator = head;
3468 new_st.block = gfc_get_code ();
3470 new_st.block->op = EXEC_FORALL;
3471 new_st.block->next = c;
3477 gfc_syntax_error (ST_FORALL);
3480 gfc_free_forall_iterator (head);
3481 gfc_free_expr (mask);
3482 gfc_free_statements (c);