1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
31 int gfc_matching_ptr_assignment = 0;
32 int gfc_matching_procptr_assignment = 0;
33 bool gfc_matching_prefix = false;
35 /* Stack of SELECT TYPE statements. */
36 gfc_select_type_stack *select_type_stack = NULL;
38 /* For debugging and diagnostic purposes. Return the textual representation
39 of the intrinsic operator OP. */
41 gfc_op2string (gfc_intrinsic_op op)
49 case INTRINSIC_UMINUS:
55 case INTRINSIC_CONCAT:
59 case INTRINSIC_DIVIDE:
98 case INTRINSIC_ASSIGN:
101 case INTRINSIC_PARENTHESES:
108 gfc_internal_error ("gfc_op2string(): Bad code");
113 /******************** Generic matching subroutines ************************/
115 /* This function scans the current statement counting the opened and closed
116 parenthesis to make sure they are balanced. */
119 gfc_match_parens (void)
121 locus old_loc, where;
123 gfc_instring instring;
126 old_loc = gfc_current_locus;
128 instring = NONSTRING;
133 c = gfc_next_char_literal (instring);
136 if (quote == ' ' && ((c == '\'') || (c == '"')))
139 instring = INSTRING_WARN;
142 if (quote != ' ' && c == quote)
145 instring = NONSTRING;
149 if (c == '(' && quote == ' ')
152 where = gfc_current_locus;
154 if (c == ')' && quote == ' ')
157 where = gfc_current_locus;
161 gfc_current_locus = old_loc;
165 gfc_error ("Missing ')' in statement at or before %L", &where);
170 gfc_error ("Missing '(' in statement at or before %L", &where);
178 /* See if the next character is a special character that has
179 escaped by a \ via the -fbackslash option. */
182 gfc_match_special_char (gfc_char_t *res)
190 switch ((c = gfc_next_char_literal (INSTRING_WARN)))
223 /* Hexadecimal form of wide characters. */
224 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
226 for (i = 0; i < len; i++)
228 char buf[2] = { '\0', '\0' };
230 c = gfc_next_char_literal (INSTRING_WARN);
231 if (!gfc_wide_fits_in_byte (c)
232 || !gfc_check_digit ((unsigned char) c, 16))
235 buf[0] = (unsigned char) c;
237 n += strtol (buf, NULL, 16);
243 /* Unknown backslash codes are simply not expanded. */
252 /* In free form, match at least one space. Always matches in fixed
256 gfc_match_space (void)
261 if (gfc_current_form == FORM_FIXED)
264 old_loc = gfc_current_locus;
266 c = gfc_next_ascii_char ();
267 if (!gfc_is_whitespace (c))
269 gfc_current_locus = old_loc;
273 gfc_gobble_whitespace ();
279 /* Match an end of statement. End of statement is optional
280 whitespace, followed by a ';' or '\n' or comment '!'. If a
281 semicolon is found, we continue to eat whitespace and semicolons. */
294 old_loc = gfc_current_locus;
295 gfc_gobble_whitespace ();
297 c = gfc_next_ascii_char ();
303 c = gfc_next_ascii_char ();
320 gfc_current_locus = old_loc;
321 return (flag) ? MATCH_YES : MATCH_NO;
325 /* Match a literal integer on the input, setting the value on
326 MATCH_YES. Literal ints occur in kind-parameters as well as
327 old-style character length specifications. If cnt is non-NULL it
328 will be set to the number of digits. */
331 gfc_match_small_literal_int (int *value, int *cnt)
337 old_loc = gfc_current_locus;
340 gfc_gobble_whitespace ();
341 c = gfc_next_ascii_char ();
347 gfc_current_locus = old_loc;
356 old_loc = gfc_current_locus;
357 c = gfc_next_ascii_char ();
362 i = 10 * i + c - '0';
367 gfc_error ("Integer too large at %C");
372 gfc_current_locus = old_loc;
381 /* Match a small, constant integer expression, like in a kind
382 statement. On MATCH_YES, 'value' is set. */
385 gfc_match_small_int (int *value)
392 m = gfc_match_expr (&expr);
396 p = gfc_extract_int (expr, &i);
397 gfc_free_expr (expr);
410 /* This function is the same as the gfc_match_small_int, except that
411 we're keeping the pointer to the expr. This function could just be
412 removed and the previously mentioned one modified, though all calls
413 to it would have to be modified then (and there were a number of
414 them). Return MATCH_ERROR if fail to extract the int; otherwise,
415 return the result of gfc_match_expr(). The expr (if any) that was
416 matched is returned in the parameter expr. */
419 gfc_match_small_int_expr (int *value, gfc_expr **expr)
425 m = gfc_match_expr (expr);
429 p = gfc_extract_int (*expr, &i);
442 /* Matches a statement label. Uses gfc_match_small_literal_int() to
443 do most of the work. */
446 gfc_match_st_label (gfc_st_label **label)
452 old_loc = gfc_current_locus;
454 m = gfc_match_small_literal_int (&i, &cnt);
460 gfc_error ("Too many digits in statement label at %C");
466 gfc_error ("Statement label at %C is zero");
470 *label = gfc_get_st_label (i);
475 gfc_current_locus = old_loc;
480 /* Match and validate a label associated with a named IF, DO or SELECT
481 statement. If the symbol does not have the label attribute, we add
482 it. We also make sure the symbol does not refer to another
483 (active) block. A matched label is pointed to by gfc_new_block. */
486 gfc_match_label (void)
488 char name[GFC_MAX_SYMBOL_LEN + 1];
491 gfc_new_block = NULL;
493 m = gfc_match (" %n :", name);
497 if (gfc_get_symbol (name, NULL, &gfc_new_block))
499 gfc_error ("Label name '%s' at %C is ambiguous", name);
503 if (gfc_new_block->attr.flavor == FL_LABEL)
505 gfc_error ("Duplicate construct label '%s' at %C", name);
509 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
510 gfc_new_block->name, NULL) == FAILURE)
517 /* See if the current input looks like a name of some sort. Modifies
518 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
519 Note that options.c restricts max_identifier_length to not more
520 than GFC_MAX_SYMBOL_LEN. */
523 gfc_match_name (char *buffer)
529 old_loc = gfc_current_locus;
530 gfc_gobble_whitespace ();
532 c = gfc_next_ascii_char ();
533 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
535 if (gfc_error_flag_test() == 0 && c != '(')
536 gfc_error ("Invalid character in name at %C");
537 gfc_current_locus = old_loc;
547 if (i > gfc_option.max_identifier_length)
549 gfc_error ("Name at %C is too long");
553 old_loc = gfc_current_locus;
554 c = gfc_next_ascii_char ();
556 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
558 if (c == '$' && !gfc_option.flag_dollar_ok)
560 gfc_error ("Invalid character '$' at %C. Use -fdollar-ok to allow it "
566 gfc_current_locus = old_loc;
572 /* Match a valid name for C, which is almost the same as for Fortran,
573 except that you can start with an underscore, etc.. It could have
574 been done by modifying the gfc_match_name, but this way other
575 things C allows can be done, such as no limits on the length.
576 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
577 input characters from being automatically lower cased, since C is
578 case sensitive. The parameter, buffer, is used to return the name
579 that is matched. Return MATCH_ERROR if the name is not a valid C
580 name, MATCH_NO if what we're seeing isn't a name, and MATCH_YES if
581 we successfully match a C name. */
584 gfc_match_name_C (const char **buffer)
592 old_loc = gfc_current_locus;
593 gfc_gobble_whitespace ();
595 /* Get the next char (first possible char of name) and see if
596 it's valid for C (either a letter or an underscore). */
597 c = gfc_next_char_literal (INSTRING_WARN);
599 /* If the user put nothing expect spaces between the quotes, it is valid
600 and simply means there is no name= specifier and the name is the fortran
601 symbol name, all lowercase. */
602 if (c == '"' || c == '\'')
604 gfc_current_locus = old_loc;
608 if (!ISALPHA (c) && c != '_')
610 gfc_error ("Invalid C name in NAME= specifier at %C");
614 buf = XNEWVEC (char, cursz);
615 /* Continue to read valid variable name characters. */
618 gcc_assert (gfc_wide_fits_in_byte (c));
620 buf[i++] = (unsigned char) c;
625 buf = XRESIZEVEC (char, buf, cursz);
628 old_loc = gfc_current_locus;
630 /* Get next char; param means we're in a string. */
631 c = gfc_next_char_literal (INSTRING_WARN);
632 } while (ISALNUM (c) || c == '_');
634 /* The binding label will be needed later anyway, so just insert it
635 into the symbol table. */
637 *buffer = IDENTIFIER_POINTER (get_identifier (buf));
639 gfc_current_locus = old_loc;
641 /* See if we stopped because of whitespace. */
644 gfc_gobble_whitespace ();
645 c = gfc_peek_ascii_char ();
646 if (c != '"' && c != '\'')
648 gfc_error ("Embedded space in NAME= specifier at %C");
653 /* If we stopped because we had an invalid character for a C name, report
654 that to the user by returning MATCH_NO. */
655 if (c != '"' && c != '\'')
657 gfc_error ("Invalid C name in NAME= specifier at %C");
665 /* Match a symbol on the input. Modifies the pointer to the symbol
666 pointer if successful. */
669 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
671 char buffer[GFC_MAX_SYMBOL_LEN + 1];
674 m = gfc_match_name (buffer);
679 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
680 ? MATCH_ERROR : MATCH_YES;
682 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
690 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
695 m = gfc_match_sym_tree (&st, host_assoc);
700 *matched_symbol = st->n.sym;
702 *matched_symbol = NULL;
705 *matched_symbol = NULL;
710 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
711 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
715 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
717 locus orig_loc = gfc_current_locus;
720 gfc_gobble_whitespace ();
721 ch = gfc_next_ascii_char ();
726 *result = INTRINSIC_PLUS;
731 *result = INTRINSIC_MINUS;
735 if (gfc_next_ascii_char () == '=')
738 *result = INTRINSIC_EQ;
744 if (gfc_peek_ascii_char () == '=')
747 gfc_next_ascii_char ();
748 *result = INTRINSIC_LE;
752 *result = INTRINSIC_LT;
756 if (gfc_peek_ascii_char () == '=')
759 gfc_next_ascii_char ();
760 *result = INTRINSIC_GE;
764 *result = INTRINSIC_GT;
768 if (gfc_peek_ascii_char () == '*')
771 gfc_next_ascii_char ();
772 *result = INTRINSIC_POWER;
776 *result = INTRINSIC_TIMES;
780 ch = gfc_peek_ascii_char ();
784 gfc_next_ascii_char ();
785 *result = INTRINSIC_NE;
791 gfc_next_ascii_char ();
792 *result = INTRINSIC_CONCAT;
796 *result = INTRINSIC_DIVIDE;
800 ch = gfc_next_ascii_char ();
804 if (gfc_next_ascii_char () == 'n'
805 && gfc_next_ascii_char () == 'd'
806 && gfc_next_ascii_char () == '.')
808 /* Matched ".and.". */
809 *result = INTRINSIC_AND;
815 if (gfc_next_ascii_char () == 'q')
817 ch = gfc_next_ascii_char ();
820 /* Matched ".eq.". */
821 *result = INTRINSIC_EQ_OS;
826 if (gfc_next_ascii_char () == '.')
828 /* Matched ".eqv.". */
829 *result = INTRINSIC_EQV;
837 ch = gfc_next_ascii_char ();
840 if (gfc_next_ascii_char () == '.')
842 /* Matched ".ge.". */
843 *result = INTRINSIC_GE_OS;
849 if (gfc_next_ascii_char () == '.')
851 /* Matched ".gt.". */
852 *result = INTRINSIC_GT_OS;
859 ch = gfc_next_ascii_char ();
862 if (gfc_next_ascii_char () == '.')
864 /* Matched ".le.". */
865 *result = INTRINSIC_LE_OS;
871 if (gfc_next_ascii_char () == '.')
873 /* Matched ".lt.". */
874 *result = INTRINSIC_LT_OS;
881 ch = gfc_next_ascii_char ();
884 ch = gfc_next_ascii_char ();
887 /* Matched ".ne.". */
888 *result = INTRINSIC_NE_OS;
893 if (gfc_next_ascii_char () == 'v'
894 && gfc_next_ascii_char () == '.')
896 /* Matched ".neqv.". */
897 *result = INTRINSIC_NEQV;
904 if (gfc_next_ascii_char () == 't'
905 && gfc_next_ascii_char () == '.')
907 /* Matched ".not.". */
908 *result = INTRINSIC_NOT;
915 if (gfc_next_ascii_char () == 'r'
916 && gfc_next_ascii_char () == '.')
918 /* Matched ".or.". */
919 *result = INTRINSIC_OR;
933 gfc_current_locus = orig_loc;
938 /* Match a loop control phrase:
940 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
942 If the final integer expression is not present, a constant unity
943 expression is returned. We don't return MATCH_ERROR until after
944 the equals sign is seen. */
947 gfc_match_iterator (gfc_iterator *iter, int init_flag)
949 char name[GFC_MAX_SYMBOL_LEN + 1];
950 gfc_expr *var, *e1, *e2, *e3;
956 /* Match the start of an iterator without affecting the symbol table. */
958 start = gfc_current_locus;
959 m = gfc_match (" %n =", name);
960 gfc_current_locus = start;
965 m = gfc_match_variable (&var, 0);
969 /* F2008, C617 & C565. */
970 if (var->symtree->n.sym->attr.codimension)
972 gfc_error ("Loop variable at %C cannot be a coarray");
976 if (var->ref != NULL)
978 gfc_error ("Loop variable at %C cannot be a sub-component");
982 gfc_match_char ('=');
984 var->symtree->n.sym->attr.implied_index = 1;
986 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
989 if (m == MATCH_ERROR)
992 if (gfc_match_char (',') != MATCH_YES)
995 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
998 if (m == MATCH_ERROR)
1001 if (gfc_match_char (',') != MATCH_YES)
1003 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1007 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1008 if (m == MATCH_ERROR)
1012 gfc_error ("Expected a step value in iterator at %C");
1024 gfc_error ("Syntax error in iterator at %C");
1035 /* Tries to match the next non-whitespace character on the input.
1036 This subroutine does not return MATCH_ERROR. */
1039 gfc_match_char (char c)
1043 where = gfc_current_locus;
1044 gfc_gobble_whitespace ();
1046 if (gfc_next_ascii_char () == c)
1049 gfc_current_locus = where;
1054 /* General purpose matching subroutine. The target string is a
1055 scanf-like format string in which spaces correspond to arbitrary
1056 whitespace (including no whitespace), characters correspond to
1057 themselves. The %-codes are:
1059 %% Literal percent sign
1060 %e Expression, pointer to a pointer is set
1061 %s Symbol, pointer to the symbol is set
1062 %n Name, character buffer is set to name
1063 %t Matches end of statement.
1064 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1065 %l Matches a statement label
1066 %v Matches a variable expression (an lvalue)
1067 % Matches a required space (in free form) and optional spaces. */
1070 gfc_match (const char *target, ...)
1072 gfc_st_label **label;
1081 old_loc = gfc_current_locus;
1082 va_start (argp, target);
1092 gfc_gobble_whitespace ();
1103 vp = va_arg (argp, void **);
1104 n = gfc_match_expr ((gfc_expr **) vp);
1115 vp = va_arg (argp, void **);
1116 n = gfc_match_variable ((gfc_expr **) vp, 0);
1127 vp = va_arg (argp, void **);
1128 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1139 np = va_arg (argp, char *);
1140 n = gfc_match_name (np);
1151 label = va_arg (argp, gfc_st_label **);
1152 n = gfc_match_st_label (label);
1163 ip = va_arg (argp, int *);
1164 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1175 if (gfc_match_eos () != MATCH_YES)
1183 if (gfc_match_space () == MATCH_YES)
1189 break; /* Fall through to character matcher. */
1192 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1197 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1198 expect an upper case character here! */
1199 gcc_assert (TOLOWER (c) == c);
1201 if (c == gfc_next_ascii_char ())
1211 /* Clean up after a failed match. */
1212 gfc_current_locus = old_loc;
1213 va_start (argp, target);
1216 for (; matches > 0; matches--)
1218 while (*p++ != '%');
1226 /* Matches that don't have to be undone */
1231 (void) va_arg (argp, void **);
1236 vp = va_arg (argp, void **);
1237 gfc_free_expr ((struct gfc_expr *)*vp);
1250 /*********************** Statement level matching **********************/
1252 /* Matches the start of a program unit, which is the program keyword
1253 followed by an obligatory symbol. */
1256 gfc_match_program (void)
1261 m = gfc_match ("% %s%t", &sym);
1265 gfc_error ("Invalid form of PROGRAM statement at %C");
1269 if (m == MATCH_ERROR)
1272 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1275 gfc_new_block = sym;
1281 /* Match a simple assignment statement. */
1284 gfc_match_assignment (void)
1286 gfc_expr *lvalue, *rvalue;
1290 old_loc = gfc_current_locus;
1293 m = gfc_match (" %v =", &lvalue);
1296 gfc_current_locus = old_loc;
1297 gfc_free_expr (lvalue);
1302 m = gfc_match (" %e%t", &rvalue);
1305 gfc_current_locus = old_loc;
1306 gfc_free_expr (lvalue);
1307 gfc_free_expr (rvalue);
1311 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1313 new_st.op = EXEC_ASSIGN;
1314 new_st.expr1 = lvalue;
1315 new_st.expr2 = rvalue;
1317 gfc_check_do_variable (lvalue->symtree);
1323 /* Match a pointer assignment statement. */
1326 gfc_match_pointer_assignment (void)
1328 gfc_expr *lvalue, *rvalue;
1332 old_loc = gfc_current_locus;
1334 lvalue = rvalue = NULL;
1335 gfc_matching_ptr_assignment = 0;
1336 gfc_matching_procptr_assignment = 0;
1338 m = gfc_match (" %v =>", &lvalue);
1345 if (lvalue->symtree->n.sym->attr.proc_pointer
1346 || gfc_is_proc_ptr_comp (lvalue, NULL))
1347 gfc_matching_procptr_assignment = 1;
1349 gfc_matching_ptr_assignment = 1;
1351 m = gfc_match (" %e%t", &rvalue);
1352 gfc_matching_ptr_assignment = 0;
1353 gfc_matching_procptr_assignment = 0;
1357 new_st.op = EXEC_POINTER_ASSIGN;
1358 new_st.expr1 = lvalue;
1359 new_st.expr2 = rvalue;
1364 gfc_current_locus = old_loc;
1365 gfc_free_expr (lvalue);
1366 gfc_free_expr (rvalue);
1371 /* We try to match an easy arithmetic IF statement. This only happens
1372 when just after having encountered a simple IF statement. This code
1373 is really duplicate with parts of the gfc_match_if code, but this is
1377 match_arithmetic_if (void)
1379 gfc_st_label *l1, *l2, *l3;
1383 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1387 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1388 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1389 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1391 gfc_free_expr (expr);
1395 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Arithmetic IF "
1396 "statement at %C") == FAILURE)
1399 new_st.op = EXEC_ARITHMETIC_IF;
1400 new_st.expr1 = expr;
1409 /* The IF statement is a bit of a pain. First of all, there are three
1410 forms of it, the simple IF, the IF that starts a block and the
1413 There is a problem with the simple IF and that is the fact that we
1414 only have a single level of undo information on symbols. What this
1415 means is for a simple IF, we must re-match the whole IF statement
1416 multiple times in order to guarantee that the symbol table ends up
1417 in the proper state. */
1419 static match match_simple_forall (void);
1420 static match match_simple_where (void);
1423 gfc_match_if (gfc_statement *if_type)
1426 gfc_st_label *l1, *l2, *l3;
1427 locus old_loc, old_loc2;
1431 n = gfc_match_label ();
1432 if (n == MATCH_ERROR)
1435 old_loc = gfc_current_locus;
1437 m = gfc_match (" if ( %e", &expr);
1441 old_loc2 = gfc_current_locus;
1442 gfc_current_locus = old_loc;
1444 if (gfc_match_parens () == MATCH_ERROR)
1447 gfc_current_locus = old_loc2;
1449 if (gfc_match_char (')') != MATCH_YES)
1451 gfc_error ("Syntax error in IF-expression at %C");
1452 gfc_free_expr (expr);
1456 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1462 gfc_error ("Block label not appropriate for arithmetic IF "
1464 gfc_free_expr (expr);
1468 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1469 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1470 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1472 gfc_free_expr (expr);
1476 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Arithmetic IF "
1477 "statement at %C") == FAILURE)
1480 new_st.op = EXEC_ARITHMETIC_IF;
1481 new_st.expr1 = expr;
1486 *if_type = ST_ARITHMETIC_IF;
1490 if (gfc_match (" then%t") == MATCH_YES)
1492 new_st.op = EXEC_IF;
1493 new_st.expr1 = expr;
1494 *if_type = ST_IF_BLOCK;
1500 gfc_error ("Block label is not appropriate for IF statement at %C");
1501 gfc_free_expr (expr);
1505 /* At this point the only thing left is a simple IF statement. At
1506 this point, n has to be MATCH_NO, so we don't have to worry about
1507 re-matching a block label. From what we've got so far, try
1508 matching an assignment. */
1510 *if_type = ST_SIMPLE_IF;
1512 m = gfc_match_assignment ();
1516 gfc_free_expr (expr);
1517 gfc_undo_symbols ();
1518 gfc_current_locus = old_loc;
1520 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1521 assignment was found. For MATCH_NO, continue to call the various
1523 if (m == MATCH_ERROR)
1526 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1528 m = gfc_match_pointer_assignment ();
1532 gfc_free_expr (expr);
1533 gfc_undo_symbols ();
1534 gfc_current_locus = old_loc;
1536 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1538 /* Look at the next keyword to see which matcher to call. Matching
1539 the keyword doesn't affect the symbol table, so we don't have to
1540 restore between tries. */
1542 #define match(string, subr, statement) \
1543 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1547 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1548 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1549 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1550 match ("call", gfc_match_call, ST_CALL)
1551 match ("close", gfc_match_close, ST_CLOSE)
1552 match ("continue", gfc_match_continue, ST_CONTINUE)
1553 match ("cycle", gfc_match_cycle, ST_CYCLE)
1554 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1555 match ("end file", gfc_match_endfile, ST_END_FILE)
1556 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1557 match ("exit", gfc_match_exit, ST_EXIT)
1558 match ("flush", gfc_match_flush, ST_FLUSH)
1559 match ("forall", match_simple_forall, ST_FORALL)
1560 match ("go to", gfc_match_goto, ST_GOTO)
1561 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1562 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1563 match ("lock", gfc_match_lock, ST_LOCK)
1564 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1565 match ("open", gfc_match_open, ST_OPEN)
1566 match ("pause", gfc_match_pause, ST_NONE)
1567 match ("print", gfc_match_print, ST_WRITE)
1568 match ("read", gfc_match_read, ST_READ)
1569 match ("return", gfc_match_return, ST_RETURN)
1570 match ("rewind", gfc_match_rewind, ST_REWIND)
1571 match ("stop", gfc_match_stop, ST_STOP)
1572 match ("wait", gfc_match_wait, ST_WAIT)
1573 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1574 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1575 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1576 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1577 match ("where", match_simple_where, ST_WHERE)
1578 match ("write", gfc_match_write, ST_WRITE)
1580 /* The gfc_match_assignment() above may have returned a MATCH_NO
1581 where the assignment was to a named constant. Check that
1582 special case here. */
1583 m = gfc_match_assignment ();
1586 gfc_error ("Cannot assign to a named constant at %C");
1587 gfc_free_expr (expr);
1588 gfc_undo_symbols ();
1589 gfc_current_locus = old_loc;
1593 /* All else has failed, so give up. See if any of the matchers has
1594 stored an error message of some sort. */
1595 if (gfc_error_check () == 0)
1596 gfc_error ("Unclassifiable statement in IF-clause at %C");
1598 gfc_free_expr (expr);
1603 gfc_error ("Syntax error in IF-clause at %C");
1606 gfc_free_expr (expr);
1610 /* At this point, we've matched the single IF and the action clause
1611 is in new_st. Rearrange things so that the IF statement appears
1614 p = gfc_get_code ();
1615 p->next = gfc_get_code ();
1617 p->next->loc = gfc_current_locus;
1622 gfc_clear_new_st ();
1624 new_st.op = EXEC_IF;
1633 /* Match an ELSE statement. */
1636 gfc_match_else (void)
1638 char name[GFC_MAX_SYMBOL_LEN + 1];
1640 if (gfc_match_eos () == MATCH_YES)
1643 if (gfc_match_name (name) != MATCH_YES
1644 || gfc_current_block () == NULL
1645 || gfc_match_eos () != MATCH_YES)
1647 gfc_error ("Unexpected junk after ELSE statement at %C");
1651 if (strcmp (name, gfc_current_block ()->name) != 0)
1653 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1654 name, gfc_current_block ()->name);
1662 /* Match an ELSE IF statement. */
1665 gfc_match_elseif (void)
1667 char name[GFC_MAX_SYMBOL_LEN + 1];
1671 m = gfc_match (" ( %e ) then", &expr);
1675 if (gfc_match_eos () == MATCH_YES)
1678 if (gfc_match_name (name) != MATCH_YES
1679 || gfc_current_block () == NULL
1680 || gfc_match_eos () != MATCH_YES)
1682 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1686 if (strcmp (name, gfc_current_block ()->name) != 0)
1688 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1689 name, gfc_current_block ()->name);
1694 new_st.op = EXEC_IF;
1695 new_st.expr1 = expr;
1699 gfc_free_expr (expr);
1704 /* Free a gfc_iterator structure. */
1707 gfc_free_iterator (gfc_iterator *iter, int flag)
1713 gfc_free_expr (iter->var);
1714 gfc_free_expr (iter->start);
1715 gfc_free_expr (iter->end);
1716 gfc_free_expr (iter->step);
1723 /* Match a CRITICAL statement. */
1725 gfc_match_critical (void)
1727 gfc_st_label *label = NULL;
1729 if (gfc_match_label () == MATCH_ERROR)
1732 if (gfc_match (" critical") != MATCH_YES)
1735 if (gfc_match_st_label (&label) == MATCH_ERROR)
1738 if (gfc_match_eos () != MATCH_YES)
1740 gfc_syntax_error (ST_CRITICAL);
1744 if (gfc_pure (NULL))
1746 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1750 if (gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS)
1752 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1757 gfc_unset_implicit_pure (NULL);
1759 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CRITICAL statement at %C")
1763 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
1765 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
1769 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
1771 gfc_error ("Nested CRITICAL block at %C");
1775 new_st.op = EXEC_CRITICAL;
1778 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1785 /* Match a BLOCK statement. */
1788 gfc_match_block (void)
1792 if (gfc_match_label () == MATCH_ERROR)
1795 if (gfc_match (" block") != MATCH_YES)
1798 /* For this to be a correct BLOCK statement, the line must end now. */
1799 m = gfc_match_eos ();
1800 if (m == MATCH_ERROR)
1809 /* Match an ASSOCIATE statement. */
1812 gfc_match_associate (void)
1814 if (gfc_match_label () == MATCH_ERROR)
1817 if (gfc_match (" associate") != MATCH_YES)
1820 /* Match the association list. */
1821 if (gfc_match_char ('(') != MATCH_YES)
1823 gfc_error ("Expected association list at %C");
1826 new_st.ext.block.assoc = NULL;
1829 gfc_association_list* newAssoc = gfc_get_association_list ();
1830 gfc_association_list* a;
1832 /* Match the next association. */
1833 if (gfc_match (" %n => %e", newAssoc->name, &newAssoc->target)
1836 gfc_error ("Expected association at %C");
1837 goto assocListError;
1839 newAssoc->where = gfc_current_locus;
1841 /* Check that the current name is not yet in the list. */
1842 for (a = new_st.ext.block.assoc; a; a = a->next)
1843 if (!strcmp (a->name, newAssoc->name))
1845 gfc_error ("Duplicate name '%s' in association at %C",
1847 goto assocListError;
1850 /* The target expression must not be coindexed. */
1851 if (gfc_is_coindexed (newAssoc->target))
1853 gfc_error ("Association target at %C must not be coindexed");
1854 goto assocListError;
1857 /* The `variable' field is left blank for now; because the target is not
1858 yet resolved, we can't use gfc_has_vector_subscript to determine it
1859 for now. This is set during resolution. */
1861 /* Put it into the list. */
1862 newAssoc->next = new_st.ext.block.assoc;
1863 new_st.ext.block.assoc = newAssoc;
1865 /* Try next one or end if closing parenthesis is found. */
1866 gfc_gobble_whitespace ();
1867 if (gfc_peek_char () == ')')
1869 if (gfc_match_char (',') != MATCH_YES)
1871 gfc_error ("Expected ')' or ',' at %C");
1881 if (gfc_match_char (')') != MATCH_YES)
1883 /* This should never happen as we peek above. */
1887 if (gfc_match_eos () != MATCH_YES)
1889 gfc_error ("Junk after ASSOCIATE statement at %C");
1896 gfc_free_association_list (new_st.ext.block.assoc);
1901 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1902 an accessible derived type. */
1905 match_derived_type_spec (gfc_typespec *ts)
1907 char name[GFC_MAX_SYMBOL_LEN + 1];
1909 gfc_symbol *derived;
1911 old_locus = gfc_current_locus;
1913 if (gfc_match ("%n", name) != MATCH_YES)
1915 gfc_current_locus = old_locus;
1919 gfc_find_symbol (name, NULL, 1, &derived);
1921 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
1922 derived = gfc_find_dt_in_generic (derived);
1924 if (derived && derived->attr.flavor == FL_DERIVED)
1926 ts->type = BT_DERIVED;
1927 ts->u.derived = derived;
1931 gfc_current_locus = old_locus;
1936 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
1937 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
1938 It only includes the intrinsic types from the Fortran 2003 standard
1939 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
1940 the implicit_flag is not needed, so it was removed. Derived types are
1941 identified by their name alone. */
1944 match_type_spec (gfc_typespec *ts)
1950 gfc_gobble_whitespace ();
1951 old_locus = gfc_current_locus;
1953 if (match_derived_type_spec (ts) == MATCH_YES)
1955 /* Enforce F03:C401. */
1956 if (ts->u.derived->attr.abstract)
1958 gfc_error ("Derived type '%s' at %L may not be ABSTRACT",
1959 ts->u.derived->name, &old_locus);
1965 if (gfc_match ("integer") == MATCH_YES)
1967 ts->type = BT_INTEGER;
1968 ts->kind = gfc_default_integer_kind;
1972 if (gfc_match ("real") == MATCH_YES)
1975 ts->kind = gfc_default_real_kind;
1979 if (gfc_match ("double precision") == MATCH_YES)
1982 ts->kind = gfc_default_double_kind;
1986 if (gfc_match ("complex") == MATCH_YES)
1988 ts->type = BT_COMPLEX;
1989 ts->kind = gfc_default_complex_kind;
1993 if (gfc_match ("character") == MATCH_YES)
1995 ts->type = BT_CHARACTER;
1997 m = gfc_match_char_spec (ts);
2005 if (gfc_match ("logical") == MATCH_YES)
2007 ts->type = BT_LOGICAL;
2008 ts->kind = gfc_default_logical_kind;
2012 /* If a type is not matched, simply return MATCH_NO. */
2013 gfc_current_locus = old_locus;
2018 gfc_gobble_whitespace ();
2019 if (gfc_peek_ascii_char () == '*')
2021 gfc_error ("Invalid type-spec at %C");
2025 m = gfc_match_kind_spec (ts, false);
2028 m = MATCH_YES; /* No kind specifier found. */
2034 /******************** FORALL subroutines ********************/
2036 /* Free a list of FORALL iterators. */
2039 gfc_free_forall_iterator (gfc_forall_iterator *iter)
2041 gfc_forall_iterator *next;
2046 gfc_free_expr (iter->var);
2047 gfc_free_expr (iter->start);
2048 gfc_free_expr (iter->end);
2049 gfc_free_expr (iter->stride);
2056 /* Match an iterator as part of a FORALL statement. The format is:
2058 <var> = <start>:<end>[:<stride>]
2060 On MATCH_NO, the caller tests for the possibility that there is a
2061 scalar mask expression. */
2064 match_forall_iterator (gfc_forall_iterator **result)
2066 gfc_forall_iterator *iter;
2070 where = gfc_current_locus;
2071 iter = XCNEW (gfc_forall_iterator);
2073 m = gfc_match_expr (&iter->var);
2077 if (gfc_match_char ('=') != MATCH_YES
2078 || iter->var->expr_type != EXPR_VARIABLE)
2084 m = gfc_match_expr (&iter->start);
2088 if (gfc_match_char (':') != MATCH_YES)
2091 m = gfc_match_expr (&iter->end);
2094 if (m == MATCH_ERROR)
2097 if (gfc_match_char (':') == MATCH_NO)
2098 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2101 m = gfc_match_expr (&iter->stride);
2104 if (m == MATCH_ERROR)
2108 /* Mark the iteration variable's symbol as used as a FORALL index. */
2109 iter->var->symtree->n.sym->forall_index = true;
2115 gfc_error ("Syntax error in FORALL iterator at %C");
2120 gfc_current_locus = where;
2121 gfc_free_forall_iterator (iter);
2126 /* Match the header of a FORALL statement. */
2129 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2131 gfc_forall_iterator *head, *tail, *new_iter;
2135 gfc_gobble_whitespace ();
2140 if (gfc_match_char ('(') != MATCH_YES)
2143 m = match_forall_iterator (&new_iter);
2144 if (m == MATCH_ERROR)
2149 head = tail = new_iter;
2153 if (gfc_match_char (',') != MATCH_YES)
2156 m = match_forall_iterator (&new_iter);
2157 if (m == MATCH_ERROR)
2162 tail->next = new_iter;
2167 /* Have to have a mask expression. */
2169 m = gfc_match_expr (&msk);
2172 if (m == MATCH_ERROR)
2178 if (gfc_match_char (')') == MATCH_NO)
2186 gfc_syntax_error (ST_FORALL);
2189 gfc_free_expr (msk);
2190 gfc_free_forall_iterator (head);
2195 /* Match the rest of a simple FORALL statement that follows an
2199 match_simple_forall (void)
2201 gfc_forall_iterator *head;
2210 m = match_forall_header (&head, &mask);
2217 m = gfc_match_assignment ();
2219 if (m == MATCH_ERROR)
2223 m = gfc_match_pointer_assignment ();
2224 if (m == MATCH_ERROR)
2230 c = gfc_get_code ();
2232 c->loc = gfc_current_locus;
2234 if (gfc_match_eos () != MATCH_YES)
2237 gfc_clear_new_st ();
2238 new_st.op = EXEC_FORALL;
2239 new_st.expr1 = mask;
2240 new_st.ext.forall_iterator = head;
2241 new_st.block = gfc_get_code ();
2243 new_st.block->op = EXEC_FORALL;
2244 new_st.block->next = c;
2249 gfc_syntax_error (ST_FORALL);
2252 gfc_free_forall_iterator (head);
2253 gfc_free_expr (mask);
2259 /* Match a FORALL statement. */
2262 gfc_match_forall (gfc_statement *st)
2264 gfc_forall_iterator *head;
2273 m0 = gfc_match_label ();
2274 if (m0 == MATCH_ERROR)
2277 m = gfc_match (" forall");
2281 m = match_forall_header (&head, &mask);
2282 if (m == MATCH_ERROR)
2287 if (gfc_match_eos () == MATCH_YES)
2289 *st = ST_FORALL_BLOCK;
2290 new_st.op = EXEC_FORALL;
2291 new_st.expr1 = mask;
2292 new_st.ext.forall_iterator = head;
2296 m = gfc_match_assignment ();
2297 if (m == MATCH_ERROR)
2301 m = gfc_match_pointer_assignment ();
2302 if (m == MATCH_ERROR)
2308 c = gfc_get_code ();
2310 c->loc = gfc_current_locus;
2312 gfc_clear_new_st ();
2313 new_st.op = EXEC_FORALL;
2314 new_st.expr1 = mask;
2315 new_st.ext.forall_iterator = head;
2316 new_st.block = gfc_get_code ();
2317 new_st.block->op = EXEC_FORALL;
2318 new_st.block->next = c;
2324 gfc_syntax_error (ST_FORALL);
2327 gfc_free_forall_iterator (head);
2328 gfc_free_expr (mask);
2329 gfc_free_statements (c);
2334 /* Match a DO statement. */
2339 gfc_iterator iter, *ip;
2341 gfc_st_label *label;
2344 old_loc = gfc_current_locus;
2347 iter.var = iter.start = iter.end = iter.step = NULL;
2349 m = gfc_match_label ();
2350 if (m == MATCH_ERROR)
2353 if (gfc_match (" do") != MATCH_YES)
2356 m = gfc_match_st_label (&label);
2357 if (m == MATCH_ERROR)
2360 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2362 if (gfc_match_eos () == MATCH_YES)
2364 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2365 new_st.op = EXEC_DO_WHILE;
2369 /* Match an optional comma, if no comma is found, a space is obligatory. */
2370 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2373 /* Check for balanced parens. */
2375 if (gfc_match_parens () == MATCH_ERROR)
2378 if (gfc_match (" concurrent") == MATCH_YES)
2380 gfc_forall_iterator *head;
2383 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: DO CONCURRENT "
2384 "construct at %C") == FAILURE)
2390 m = match_forall_header (&head, &mask);
2394 if (m == MATCH_ERROR)
2395 goto concurr_cleanup;
2397 if (gfc_match_eos () != MATCH_YES)
2398 goto concurr_cleanup;
2401 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2402 goto concurr_cleanup;
2404 new_st.label1 = label;
2405 new_st.op = EXEC_DO_CONCURRENT;
2406 new_st.expr1 = mask;
2407 new_st.ext.forall_iterator = head;
2412 gfc_syntax_error (ST_DO);
2413 gfc_free_expr (mask);
2414 gfc_free_forall_iterator (head);
2418 /* See if we have a DO WHILE. */
2419 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2421 new_st.op = EXEC_DO_WHILE;
2425 /* The abortive DO WHILE may have done something to the symbol
2426 table, so we start over. */
2427 gfc_undo_symbols ();
2428 gfc_current_locus = old_loc;
2430 gfc_match_label (); /* This won't error. */
2431 gfc_match (" do "); /* This will work. */
2433 gfc_match_st_label (&label); /* Can't error out. */
2434 gfc_match_char (','); /* Optional comma. */
2436 m = gfc_match_iterator (&iter, 0);
2439 if (m == MATCH_ERROR)
2442 iter.var->symtree->n.sym->attr.implied_index = 0;
2443 gfc_check_do_variable (iter.var->symtree);
2445 if (gfc_match_eos () != MATCH_YES)
2447 gfc_syntax_error (ST_DO);
2451 new_st.op = EXEC_DO;
2455 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2458 new_st.label1 = label;
2460 if (new_st.op == EXEC_DO_WHILE)
2461 new_st.expr1 = iter.end;
2464 new_st.ext.iterator = ip = gfc_get_iterator ();
2471 gfc_free_iterator (&iter, 0);
2477 /* Match an EXIT or CYCLE statement. */
2480 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2482 gfc_state_data *p, *o;
2487 if (gfc_match_eos () == MATCH_YES)
2491 char name[GFC_MAX_SYMBOL_LEN + 1];
2494 m = gfc_match ("% %n%t", name);
2495 if (m == MATCH_ERROR)
2499 gfc_syntax_error (st);
2503 /* Find the corresponding symbol. If there's a BLOCK statement
2504 between here and the label, it is not in gfc_current_ns but a parent
2506 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2509 gfc_error ("Name '%s' in %s statement at %C is unknown",
2510 name, gfc_ascii_statement (st));
2515 if (sym->attr.flavor != FL_LABEL)
2517 gfc_error ("Name '%s' in %s statement at %C is not a construct name",
2518 name, gfc_ascii_statement (st));
2523 /* Find the loop specified by the label (or lack of a label). */
2524 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2525 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2527 else if (p->state == COMP_CRITICAL)
2529 gfc_error("%s statement at %C leaves CRITICAL construct",
2530 gfc_ascii_statement (st));
2533 else if (p->state == COMP_DO_CONCURRENT
2534 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2536 /* F2008, C821 & C845. */
2537 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2538 gfc_ascii_statement (st));
2541 else if ((sym && sym == p->sym)
2542 || (!sym && (p->state == COMP_DO
2543 || p->state == COMP_DO_CONCURRENT)))
2549 gfc_error ("%s statement at %C is not within a construct",
2550 gfc_ascii_statement (st));
2552 gfc_error ("%s statement at %C is not within construct '%s'",
2553 gfc_ascii_statement (st), sym->name);
2558 /* Special checks for EXIT from non-loop constructs. */
2562 case COMP_DO_CONCURRENT:
2566 /* This is already handled above. */
2569 case COMP_ASSOCIATE:
2573 case COMP_SELECT_TYPE:
2575 if (op == EXEC_CYCLE)
2577 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2578 " construct '%s'", sym->name);
2581 gcc_assert (op == EXEC_EXIT);
2582 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: EXIT statement with no"
2583 " do-construct-name at %C") == FAILURE)
2588 gfc_error ("%s statement at %C is not applicable to construct '%s'",
2589 gfc_ascii_statement (st), sym->name);
2595 gfc_error ("%s statement at %C leaving OpenMP structured block",
2596 gfc_ascii_statement (st));
2600 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2604 && o->state == COMP_OMP_STRUCTURED_BLOCK
2605 && (o->head->op == EXEC_OMP_DO
2606 || o->head->op == EXEC_OMP_PARALLEL_DO))
2609 gcc_assert (o->head->next != NULL
2610 && (o->head->next->op == EXEC_DO
2611 || o->head->next->op == EXEC_DO_WHILE)
2612 && o->previous != NULL
2613 && o->previous->tail->op == o->head->op);
2614 if (o->previous->tail->ext.omp_clauses != NULL
2615 && o->previous->tail->ext.omp_clauses->collapse > 1)
2616 collapse = o->previous->tail->ext.omp_clauses->collapse;
2617 if (st == ST_EXIT && cnt <= collapse)
2619 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2622 if (st == ST_CYCLE && cnt < collapse)
2624 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2630 /* Save the first statement in the construct - needed by the backend. */
2631 new_st.ext.which_construct = p->construct;
2639 /* Match the EXIT statement. */
2642 gfc_match_exit (void)
2644 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2648 /* Match the CYCLE statement. */
2651 gfc_match_cycle (void)
2653 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2657 /* Match a number or character constant after an (ALL) STOP or PAUSE statement. */
2660 gfc_match_stopcode (gfc_statement st)
2667 if (gfc_match_eos () != MATCH_YES)
2669 m = gfc_match_init_expr (&e);
2670 if (m == MATCH_ERROR)
2675 if (gfc_match_eos () != MATCH_YES)
2679 if (gfc_pure (NULL))
2681 gfc_error ("%s statement not allowed in PURE procedure at %C",
2682 gfc_ascii_statement (st));
2686 gfc_unset_implicit_pure (NULL);
2688 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL) == SUCCESS)
2690 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2693 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS)
2695 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
2701 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
2703 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2710 gfc_error ("STOP code at %L must be scalar",
2715 if (e->ts.type == BT_CHARACTER
2716 && e->ts.kind != gfc_default_character_kind)
2718 gfc_error ("STOP code at %L must be default character KIND=%d",
2719 &e->where, (int) gfc_default_character_kind);
2723 if (e->ts.type == BT_INTEGER
2724 && e->ts.kind != gfc_default_integer_kind)
2726 gfc_error ("STOP code at %L must be default integer KIND=%d",
2727 &e->where, (int) gfc_default_integer_kind);
2735 new_st.op = EXEC_STOP;
2738 new_st.op = EXEC_ERROR_STOP;
2741 new_st.op = EXEC_PAUSE;
2748 new_st.ext.stop_code = -1;
2753 gfc_syntax_error (st);
2762 /* Match the (deprecated) PAUSE statement. */
2765 gfc_match_pause (void)
2769 m = gfc_match_stopcode (ST_PAUSE);
2772 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
2781 /* Match the STOP statement. */
2784 gfc_match_stop (void)
2786 return gfc_match_stopcode (ST_STOP);
2790 /* Match the ERROR STOP statement. */
2793 gfc_match_error_stop (void)
2795 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: ERROR STOP statement at %C")
2799 return gfc_match_stopcode (ST_ERROR_STOP);
2803 /* Match LOCK/UNLOCK statement. Syntax:
2804 LOCK ( lock-variable [ , lock-stat-list ] )
2805 UNLOCK ( lock-variable [ , sync-stat-list ] )
2806 where lock-stat is ACQUIRED_LOCK or sync-stat
2807 and sync-stat is STAT= or ERRMSG=. */
2810 lock_unlock_statement (gfc_statement st)
2813 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
2814 bool saw_acq_lock, saw_stat, saw_errmsg;
2816 tmp = lockvar = acq_lock = stat = errmsg = NULL;
2817 saw_acq_lock = saw_stat = saw_errmsg = false;
2819 if (gfc_pure (NULL))
2821 gfc_error ("Image control statement %s at %C in PURE procedure",
2822 st == ST_LOCK ? "LOCK" : "UNLOCK");
2826 gfc_unset_implicit_pure (NULL);
2828 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
2830 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
2834 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
2836 gfc_error ("Image control statement %s at %C in CRITICAL block",
2837 st == ST_LOCK ? "LOCK" : "UNLOCK");
2841 if (gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS)
2843 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
2844 st == ST_LOCK ? "LOCK" : "UNLOCK");
2848 if (gfc_match_char ('(') != MATCH_YES)
2851 if (gfc_match ("%e", &lockvar) != MATCH_YES)
2853 m = gfc_match_char (',');
2854 if (m == MATCH_ERROR)
2858 m = gfc_match_char (')');
2866 m = gfc_match (" stat = %v", &tmp);
2867 if (m == MATCH_ERROR)
2873 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
2879 m = gfc_match_char (',');
2887 m = gfc_match (" errmsg = %v", &tmp);
2888 if (m == MATCH_ERROR)
2894 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
2900 m = gfc_match_char (',');
2908 m = gfc_match (" acquired_lock = %v", &tmp);
2909 if (m == MATCH_ERROR || st == ST_UNLOCK)
2915 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L ",
2920 saw_acq_lock = true;
2922 m = gfc_match_char (',');
2933 if (m == MATCH_ERROR)
2936 if (gfc_match (" )%t") != MATCH_YES)
2943 new_st.op = EXEC_LOCK;
2946 new_st.op = EXEC_UNLOCK;
2952 new_st.expr1 = lockvar;
2953 new_st.expr2 = stat;
2954 new_st.expr3 = errmsg;
2955 new_st.expr4 = acq_lock;
2960 gfc_syntax_error (st);
2963 gfc_free_expr (tmp);
2964 gfc_free_expr (lockvar);
2965 gfc_free_expr (acq_lock);
2966 gfc_free_expr (stat);
2967 gfc_free_expr (errmsg);
2974 gfc_match_lock (void)
2976 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: LOCK statement at %C")
2980 return lock_unlock_statement (ST_LOCK);
2985 gfc_match_unlock (void)
2987 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: UNLOCK statement at %C")
2991 return lock_unlock_statement (ST_UNLOCK);
2995 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
2996 SYNC ALL [(sync-stat-list)]
2997 SYNC MEMORY [(sync-stat-list)]
2998 SYNC IMAGES (image-set [, sync-stat-list] )
2999 with sync-stat is int-expr or *. */
3002 sync_statement (gfc_statement st)
3005 gfc_expr *tmp, *imageset, *stat, *errmsg;
3006 bool saw_stat, saw_errmsg;
3008 tmp = imageset = stat = errmsg = NULL;
3009 saw_stat = saw_errmsg = false;
3011 if (gfc_pure (NULL))
3013 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3017 gfc_unset_implicit_pure (NULL);
3019 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: SYNC statement at %C")
3023 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
3025 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
3029 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
3031 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3035 if (gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS)
3037 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3041 if (gfc_match_eos () == MATCH_YES)
3043 if (st == ST_SYNC_IMAGES)
3048 if (gfc_match_char ('(') != MATCH_YES)
3051 if (st == ST_SYNC_IMAGES)
3053 /* Denote '*' as imageset == NULL. */
3054 m = gfc_match_char ('*');
3055 if (m == MATCH_ERROR)
3059 if (gfc_match ("%e", &imageset) != MATCH_YES)
3062 m = gfc_match_char (',');
3063 if (m == MATCH_ERROR)
3067 m = gfc_match_char (')');
3076 m = gfc_match (" stat = %v", &tmp);
3077 if (m == MATCH_ERROR)
3083 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3089 if (gfc_match_char (',') == MATCH_YES)
3096 m = gfc_match (" errmsg = %v", &tmp);
3097 if (m == MATCH_ERROR)
3103 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3109 if (gfc_match_char (',') == MATCH_YES)
3119 if (m == MATCH_ERROR)
3122 if (gfc_match (" )%t") != MATCH_YES)
3129 new_st.op = EXEC_SYNC_ALL;
3131 case ST_SYNC_IMAGES:
3132 new_st.op = EXEC_SYNC_IMAGES;
3134 case ST_SYNC_MEMORY:
3135 new_st.op = EXEC_SYNC_MEMORY;
3141 new_st.expr1 = imageset;
3142 new_st.expr2 = stat;
3143 new_st.expr3 = errmsg;
3148 gfc_syntax_error (st);
3151 gfc_free_expr (tmp);
3152 gfc_free_expr (imageset);
3153 gfc_free_expr (stat);
3154 gfc_free_expr (errmsg);
3160 /* Match SYNC ALL statement. */
3163 gfc_match_sync_all (void)
3165 return sync_statement (ST_SYNC_ALL);
3169 /* Match SYNC IMAGES statement. */
3172 gfc_match_sync_images (void)
3174 return sync_statement (ST_SYNC_IMAGES);
3178 /* Match SYNC MEMORY statement. */
3181 gfc_match_sync_memory (void)
3183 return sync_statement (ST_SYNC_MEMORY);
3187 /* Match a CONTINUE statement. */
3190 gfc_match_continue (void)
3192 if (gfc_match_eos () != MATCH_YES)
3194 gfc_syntax_error (ST_CONTINUE);
3198 new_st.op = EXEC_CONTINUE;
3203 /* Match the (deprecated) ASSIGN statement. */
3206 gfc_match_assign (void)
3209 gfc_st_label *label;
3211 if (gfc_match (" %l", &label) == MATCH_YES)
3213 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
3215 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
3217 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
3222 expr->symtree->n.sym->attr.assign = 1;
3224 new_st.op = EXEC_LABEL_ASSIGN;
3225 new_st.label1 = label;
3226 new_st.expr1 = expr;
3234 /* Match the GO TO statement. As a computed GOTO statement is
3235 matched, it is transformed into an equivalent SELECT block. No
3236 tree is necessary, and the resulting jumps-to-jumps are
3237 specifically optimized away by the back end. */
3240 gfc_match_goto (void)
3242 gfc_code *head, *tail;
3245 gfc_st_label *label;
3249 if (gfc_match (" %l%t", &label) == MATCH_YES)
3251 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
3254 new_st.op = EXEC_GOTO;
3255 new_st.label1 = label;
3259 /* The assigned GO TO statement. */
3261 if (gfc_match_variable (&expr, 0) == MATCH_YES)
3263 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
3268 new_st.op = EXEC_GOTO;
3269 new_st.expr1 = expr;
3271 if (gfc_match_eos () == MATCH_YES)
3274 /* Match label list. */
3275 gfc_match_char (',');
3276 if (gfc_match_char ('(') != MATCH_YES)
3278 gfc_syntax_error (ST_GOTO);
3285 m = gfc_match_st_label (&label);
3289 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
3293 head = tail = gfc_get_code ();
3296 tail->block = gfc_get_code ();
3300 tail->label1 = label;
3301 tail->op = EXEC_GOTO;
3303 while (gfc_match_char (',') == MATCH_YES);
3305 if (gfc_match (")%t") != MATCH_YES)
3310 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3313 new_st.block = head;
3318 /* Last chance is a computed GO TO statement. */
3319 if (gfc_match_char ('(') != MATCH_YES)
3321 gfc_syntax_error (ST_GOTO);
3330 m = gfc_match_st_label (&label);
3334 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
3338 head = tail = gfc_get_code ();
3341 tail->block = gfc_get_code ();
3345 cp = gfc_get_case ();
3346 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
3349 tail->op = EXEC_SELECT;
3350 tail->ext.block.case_list = cp;
3352 tail->next = gfc_get_code ();
3353 tail->next->op = EXEC_GOTO;
3354 tail->next->label1 = label;
3356 while (gfc_match_char (',') == MATCH_YES);
3358 if (gfc_match_char (')') != MATCH_YES)
3363 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3367 /* Get the rest of the statement. */
3368 gfc_match_char (',');
3370 if (gfc_match (" %e%t", &expr) != MATCH_YES)
3373 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Computed GOTO "
3374 "at %C") == FAILURE)
3377 /* At this point, a computed GOTO has been fully matched and an
3378 equivalent SELECT statement constructed. */
3380 new_st.op = EXEC_SELECT;
3381 new_st.expr1 = NULL;
3383 /* Hack: For a "real" SELECT, the expression is in expr. We put
3384 it in expr2 so we can distinguish then and produce the correct
3386 new_st.expr2 = expr;
3387 new_st.block = head;
3391 gfc_syntax_error (ST_GOTO);
3393 gfc_free_statements (head);
3398 /* Frees a list of gfc_alloc structures. */
3401 gfc_free_alloc_list (gfc_alloc *p)
3408 gfc_free_expr (p->expr);
3414 /* Match an ALLOCATE statement. */
3417 gfc_match_allocate (void)
3419 gfc_alloc *head, *tail;
3420 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
3424 locus old_locus, deferred_locus;
3425 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
3428 stat = errmsg = source = mold = tmp = NULL;
3429 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
3431 if (gfc_match_char ('(') != MATCH_YES)
3434 /* Match an optional type-spec. */
3435 old_locus = gfc_current_locus;
3436 m = match_type_spec (&ts);
3437 if (m == MATCH_ERROR)
3439 else if (m == MATCH_NO)
3441 char name[GFC_MAX_SYMBOL_LEN + 3];
3443 if (gfc_match ("%n :: ", name) == MATCH_YES)
3445 gfc_error ("Error in type-spec at %L", &old_locus);
3449 ts.type = BT_UNKNOWN;
3453 if (gfc_match (" :: ") == MATCH_YES)
3455 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: typespec in "
3456 "ALLOCATE at %L", &old_locus) == FAILURE)
3461 gfc_error ("Type-spec at %L cannot contain a deferred "
3462 "type parameter", &old_locus);
3468 ts.type = BT_UNKNOWN;
3469 gfc_current_locus = old_locus;
3476 head = tail = gfc_get_alloc ();
3479 tail->next = gfc_get_alloc ();
3483 m = gfc_match_variable (&tail->expr, 0);
3486 if (m == MATCH_ERROR)
3489 if (gfc_check_do_variable (tail->expr->symtree))
3492 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
3493 if (impure && gfc_pure (NULL))
3495 gfc_error ("Bad allocate-object at %C for a PURE procedure");
3500 gfc_unset_implicit_pure (NULL);
3502 if (tail->expr->ts.deferred)
3504 saw_deferred = true;
3505 deferred_locus = tail->expr->where;
3508 if (gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS
3509 || gfc_find_state (COMP_CRITICAL) == SUCCESS)
3512 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
3513 for (ref = tail->expr->ref; ref; ref = ref->next)
3514 if (ref->type == REF_COMPONENT)
3515 coarray = ref->u.c.component->attr.codimension;
3517 if (coarray && gfc_find_state (COMP_DO_CONCURRENT) == SUCCESS)
3519 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
3522 if (coarray && gfc_find_state (COMP_CRITICAL) == SUCCESS)
3524 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
3529 /* The ALLOCATE statement had an optional typespec. Check the
3531 if (ts.type != BT_UNKNOWN)
3533 /* Enforce F03:C624. */
3534 if (!gfc_type_compatible (&tail->expr->ts, &ts))
3536 gfc_error ("Type of entity at %L is type incompatible with "
3537 "typespec", &tail->expr->where);
3541 /* Enforce F03:C627. */
3542 if (ts.kind != tail->expr->ts.kind)
3544 gfc_error ("Kind type parameter for entity at %L differs from "
3545 "the kind type parameter of the typespec",
3546 &tail->expr->where);
3551 if (tail->expr->ts.type == BT_DERIVED)
3552 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
3554 /* FIXME: disable the checking on derived types and arrays. */
3555 sym = tail->expr->symtree->n.sym;
3556 b1 = !(tail->expr->ref
3557 && (tail->expr->ref->type == REF_COMPONENT
3558 || tail->expr->ref->type == REF_ARRAY));
3559 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
3560 b2 = !(CLASS_DATA (sym)->attr.allocatable
3561 || CLASS_DATA (sym)->attr.class_pointer);
3563 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
3564 || sym->attr.proc_pointer);
3565 b3 = sym && sym->ns && sym->ns->proc_name
3566 && (sym->ns->proc_name->attr.allocatable
3567 || sym->ns->proc_name->attr.pointer
3568 || sym->ns->proc_name->attr.proc_pointer);
3569 if (b1 && b2 && !b3)
3571 gfc_error ("Allocate-object at %L is not a nonprocedure pointer "
3572 "or an allocatable variable", &tail->expr->where);
3576 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
3578 gfc_error ("Shape specification for allocatable scalar at %C");
3582 if (gfc_match_char (',') != MATCH_YES)
3587 m = gfc_match (" stat = %v", &tmp);
3588 if (m == MATCH_ERROR)
3595 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3603 if (gfc_check_do_variable (stat->symtree))
3606 if (gfc_match_char (',') == MATCH_YES)
3607 goto alloc_opt_list;
3610 m = gfc_match (" errmsg = %v", &tmp);
3611 if (m == MATCH_ERROR)
3615 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ERRMSG tag at %L",
3616 &tmp->where) == FAILURE)
3622 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3630 if (gfc_match_char (',') == MATCH_YES)
3631 goto alloc_opt_list;
3634 m = gfc_match (" source = %e", &tmp);
3635 if (m == MATCH_ERROR)
3639 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SOURCE tag at %L",
3640 &tmp->where) == FAILURE)
3646 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
3650 /* The next 2 conditionals check C631. */
3651 if (ts.type != BT_UNKNOWN)
3653 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
3654 &tmp->where, &old_locus);
3659 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: SOURCE tag at %L"
3660 " with more than a single allocate object",
3661 &tmp->where) == FAILURE)
3668 if (gfc_match_char (',') == MATCH_YES)
3669 goto alloc_opt_list;
3672 m = gfc_match (" mold = %e", &tmp);
3673 if (m == MATCH_ERROR)
3677 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: MOLD tag at %L",
3678 &tmp->where) == FAILURE)
3681 /* Check F08:C636. */
3684 gfc_error ("Redundant MOLD tag found at %L ", &tmp->where);
3688 /* Check F08:C637. */
3689 if (ts.type != BT_UNKNOWN)
3691 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
3692 &tmp->where, &old_locus);
3701 if (gfc_match_char (',') == MATCH_YES)
3702 goto alloc_opt_list;
3705 gfc_gobble_whitespace ();
3707 if (gfc_peek_char () == ')')
3711 if (gfc_match (" )%t") != MATCH_YES)
3714 /* Check F08:C637. */
3717 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
3718 &mold->where, &source->where);
3722 /* Check F03:C623, */
3723 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
3725 gfc_error ("Allocate-object at %L with a deferred type parameter "
3726 "requires either a type-spec or SOURCE tag or a MOLD tag",