1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010 Free Software Foundation, Inc.
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
29 int gfc_matching_procptr_assignment = 0;
30 bool gfc_matching_prefix = false;
32 /* Stack of SELECT TYPE statements. */
33 gfc_select_type_stack *select_type_stack = NULL;
35 /* For debugging and diagnostic purposes. Return the textual representation
36 of the intrinsic operator OP. */
38 gfc_op2string (gfc_intrinsic_op op)
46 case INTRINSIC_UMINUS:
52 case INTRINSIC_CONCAT:
56 case INTRINSIC_DIVIDE:
95 case INTRINSIC_ASSIGN:
98 case INTRINSIC_PARENTHESES:
105 gfc_internal_error ("gfc_op2string(): Bad code");
110 /******************** Generic matching subroutines ************************/
112 /* This function scans the current statement counting the opened and closed
113 parenthesis to make sure they are balanced. */
116 gfc_match_parens (void)
118 locus old_loc, where;
122 old_loc = gfc_current_locus;
129 c = gfc_next_char_literal (instring);
132 if (quote == ' ' && ((c == '\'') || (c == '"')))
138 if (quote != ' ' && c == quote)
145 if (c == '(' && quote == ' ')
148 where = gfc_current_locus;
150 if (c == ')' && quote == ' ')
153 where = gfc_current_locus;
157 gfc_current_locus = old_loc;
161 gfc_error ("Missing ')' in statement at or before %L", &where);
166 gfc_error ("Missing '(' in statement at or before %L", &where);
174 /* See if the next character is a special character that has
175 escaped by a \ via the -fbackslash option. */
178 gfc_match_special_char (gfc_char_t *res)
186 switch ((c = gfc_next_char_literal (1)))
219 /* Hexadecimal form of wide characters. */
220 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
222 for (i = 0; i < len; i++)
224 char buf[2] = { '\0', '\0' };
226 c = gfc_next_char_literal (1);
227 if (!gfc_wide_fits_in_byte (c)
228 || !gfc_check_digit ((unsigned char) c, 16))
231 buf[0] = (unsigned char) c;
233 n += strtol (buf, NULL, 16);
239 /* Unknown backslash codes are simply not expanded. */
248 /* In free form, match at least one space. Always matches in fixed
252 gfc_match_space (void)
257 if (gfc_current_form == FORM_FIXED)
260 old_loc = gfc_current_locus;
262 c = gfc_next_ascii_char ();
263 if (!gfc_is_whitespace (c))
265 gfc_current_locus = old_loc;
269 gfc_gobble_whitespace ();
275 /* Match an end of statement. End of statement is optional
276 whitespace, followed by a ';' or '\n' or comment '!'. If a
277 semicolon is found, we continue to eat whitespace and semicolons. */
290 old_loc = gfc_current_locus;
291 gfc_gobble_whitespace ();
293 c = gfc_next_ascii_char ();
299 c = gfc_next_ascii_char ();
316 gfc_current_locus = old_loc;
317 return (flag) ? MATCH_YES : MATCH_NO;
321 /* Match a literal integer on the input, setting the value on
322 MATCH_YES. Literal ints occur in kind-parameters as well as
323 old-style character length specifications. If cnt is non-NULL it
324 will be set to the number of digits. */
327 gfc_match_small_literal_int (int *value, int *cnt)
333 old_loc = gfc_current_locus;
336 gfc_gobble_whitespace ();
337 c = gfc_next_ascii_char ();
343 gfc_current_locus = old_loc;
352 old_loc = gfc_current_locus;
353 c = gfc_next_ascii_char ();
358 i = 10 * i + c - '0';
363 gfc_error ("Integer too large at %C");
368 gfc_current_locus = old_loc;
377 /* Match a small, constant integer expression, like in a kind
378 statement. On MATCH_YES, 'value' is set. */
381 gfc_match_small_int (int *value)
388 m = gfc_match_expr (&expr);
392 p = gfc_extract_int (expr, &i);
393 gfc_free_expr (expr);
406 /* This function is the same as the gfc_match_small_int, except that
407 we're keeping the pointer to the expr. This function could just be
408 removed and the previously mentioned one modified, though all calls
409 to it would have to be modified then (and there were a number of
410 them). Return MATCH_ERROR if fail to extract the int; otherwise,
411 return the result of gfc_match_expr(). The expr (if any) that was
412 matched is returned in the parameter expr. */
415 gfc_match_small_int_expr (int *value, gfc_expr **expr)
421 m = gfc_match_expr (expr);
425 p = gfc_extract_int (*expr, &i);
438 /* Matches a statement label. Uses gfc_match_small_literal_int() to
439 do most of the work. */
442 gfc_match_st_label (gfc_st_label **label)
448 old_loc = gfc_current_locus;
450 m = gfc_match_small_literal_int (&i, &cnt);
456 gfc_error ("Too many digits in statement label at %C");
462 gfc_error ("Statement label at %C is zero");
466 *label = gfc_get_st_label (i);
471 gfc_current_locus = old_loc;
476 /* Match and validate a label associated with a named IF, DO or SELECT
477 statement. If the symbol does not have the label attribute, we add
478 it. We also make sure the symbol does not refer to another
479 (active) block. A matched label is pointed to by gfc_new_block. */
482 gfc_match_label (void)
484 char name[GFC_MAX_SYMBOL_LEN + 1];
487 gfc_new_block = NULL;
489 m = gfc_match (" %n :", name);
493 if (gfc_get_symbol (name, NULL, &gfc_new_block))
495 gfc_error ("Label name '%s' at %C is ambiguous", name);
499 if (gfc_new_block->attr.flavor == FL_LABEL)
501 gfc_error ("Duplicate construct label '%s' at %C", name);
505 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
506 gfc_new_block->name, NULL) == FAILURE)
513 /* See if the current input looks like a name of some sort. Modifies
514 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
515 Note that options.c restricts max_identifier_length to not more
516 than GFC_MAX_SYMBOL_LEN. */
519 gfc_match_name (char *buffer)
525 old_loc = gfc_current_locus;
526 gfc_gobble_whitespace ();
528 c = gfc_next_ascii_char ();
529 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
531 if (gfc_error_flag_test() == 0 && c != '(')
532 gfc_error ("Invalid character in name at %C");
533 gfc_current_locus = old_loc;
543 if (i > gfc_option.max_identifier_length)
545 gfc_error ("Name at %C is too long");
549 old_loc = gfc_current_locus;
550 c = gfc_next_ascii_char ();
552 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
554 if (c == '$' && !gfc_option.flag_dollar_ok)
556 gfc_error ("Invalid character '$' at %C. Use -fdollar-ok to allow it "
562 gfc_current_locus = old_loc;
568 /* Match a valid name for C, which is almost the same as for Fortran,
569 except that you can start with an underscore, etc.. It could have
570 been done by modifying the gfc_match_name, but this way other
571 things C allows can be added, such as no limits on the length.
572 Right now, the length is limited to the same thing as Fortran..
573 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
574 input characters from being automatically lower cased, since C is
575 case sensitive. The parameter, buffer, is used to return the name
576 that is matched. Return MATCH_ERROR if the name is too long
577 (though this is a self-imposed limit), MATCH_NO if what we're
578 seeing isn't a name, and MATCH_YES if we successfully match a C
582 gfc_match_name_C (char *buffer)
588 old_loc = gfc_current_locus;
589 gfc_gobble_whitespace ();
591 /* Get the next char (first possible char of name) and see if
592 it's valid for C (either a letter or an underscore). */
593 c = gfc_next_char_literal (1);
595 /* If the user put nothing expect spaces between the quotes, it is valid
596 and simply means there is no name= specifier and the name is the fortran
597 symbol name, all lowercase. */
598 if (c == '"' || c == '\'')
601 gfc_current_locus = old_loc;
605 if (!ISALPHA (c) && c != '_')
607 gfc_error ("Invalid C name in NAME= specifier at %C");
611 /* Continue to read valid variable name characters. */
614 gcc_assert (gfc_wide_fits_in_byte (c));
616 buffer[i++] = (unsigned char) c;
618 /* C does not define a maximum length of variable names, to my
619 knowledge, but the compiler typically places a limit on them.
620 For now, i'll use the same as the fortran limit for simplicity,
621 but this may need to be changed to a dynamic buffer that can
622 be realloc'ed here if necessary, or more likely, a larger
624 if (i > gfc_option.max_identifier_length)
626 gfc_error ("Name at %C is too long");
630 old_loc = gfc_current_locus;
632 /* Get next char; param means we're in a string. */
633 c = gfc_next_char_literal (1);
634 } while (ISALNUM (c) || c == '_');
637 gfc_current_locus = old_loc;
639 /* See if we stopped because of whitespace. */
642 gfc_gobble_whitespace ();
643 c = gfc_peek_ascii_char ();
644 if (c != '"' && c != '\'')
646 gfc_error ("Embedded space in NAME= specifier at %C");
651 /* If we stopped because we had an invalid character for a C name, report
652 that to the user by returning MATCH_NO. */
653 if (c != '"' && c != '\'')
655 gfc_error ("Invalid C name in NAME= specifier at %C");
663 /* Match a symbol on the input. Modifies the pointer to the symbol
664 pointer if successful. */
667 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
669 char buffer[GFC_MAX_SYMBOL_LEN + 1];
672 m = gfc_match_name (buffer);
677 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
678 ? MATCH_ERROR : MATCH_YES;
680 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
688 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
693 m = gfc_match_sym_tree (&st, host_assoc);
698 *matched_symbol = st->n.sym;
700 *matched_symbol = NULL;
703 *matched_symbol = NULL;
708 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
709 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
713 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
715 locus orig_loc = gfc_current_locus;
718 gfc_gobble_whitespace ();
719 ch = gfc_next_ascii_char ();
724 *result = INTRINSIC_PLUS;
729 *result = INTRINSIC_MINUS;
733 if (gfc_next_ascii_char () == '=')
736 *result = INTRINSIC_EQ;
742 if (gfc_peek_ascii_char () == '=')
745 gfc_next_ascii_char ();
746 *result = INTRINSIC_LE;
750 *result = INTRINSIC_LT;
754 if (gfc_peek_ascii_char () == '=')
757 gfc_next_ascii_char ();
758 *result = INTRINSIC_GE;
762 *result = INTRINSIC_GT;
766 if (gfc_peek_ascii_char () == '*')
769 gfc_next_ascii_char ();
770 *result = INTRINSIC_POWER;
774 *result = INTRINSIC_TIMES;
778 ch = gfc_peek_ascii_char ();
782 gfc_next_ascii_char ();
783 *result = INTRINSIC_NE;
789 gfc_next_ascii_char ();
790 *result = INTRINSIC_CONCAT;
794 *result = INTRINSIC_DIVIDE;
798 ch = gfc_next_ascii_char ();
802 if (gfc_next_ascii_char () == 'n'
803 && gfc_next_ascii_char () == 'd'
804 && gfc_next_ascii_char () == '.')
806 /* Matched ".and.". */
807 *result = INTRINSIC_AND;
813 if (gfc_next_ascii_char () == 'q')
815 ch = gfc_next_ascii_char ();
818 /* Matched ".eq.". */
819 *result = INTRINSIC_EQ_OS;
824 if (gfc_next_ascii_char () == '.')
826 /* Matched ".eqv.". */
827 *result = INTRINSIC_EQV;
835 ch = gfc_next_ascii_char ();
838 if (gfc_next_ascii_char () == '.')
840 /* Matched ".ge.". */
841 *result = INTRINSIC_GE_OS;
847 if (gfc_next_ascii_char () == '.')
849 /* Matched ".gt.". */
850 *result = INTRINSIC_GT_OS;
857 ch = gfc_next_ascii_char ();
860 if (gfc_next_ascii_char () == '.')
862 /* Matched ".le.". */
863 *result = INTRINSIC_LE_OS;
869 if (gfc_next_ascii_char () == '.')
871 /* Matched ".lt.". */
872 *result = INTRINSIC_LT_OS;
879 ch = gfc_next_ascii_char ();
882 ch = gfc_next_ascii_char ();
885 /* Matched ".ne.". */
886 *result = INTRINSIC_NE_OS;
891 if (gfc_next_ascii_char () == 'v'
892 && gfc_next_ascii_char () == '.')
894 /* Matched ".neqv.". */
895 *result = INTRINSIC_NEQV;
902 if (gfc_next_ascii_char () == 't'
903 && gfc_next_ascii_char () == '.')
905 /* Matched ".not.". */
906 *result = INTRINSIC_NOT;
913 if (gfc_next_ascii_char () == 'r'
914 && gfc_next_ascii_char () == '.')
916 /* Matched ".or.". */
917 *result = INTRINSIC_OR;
931 gfc_current_locus = orig_loc;
936 /* Match a loop control phrase:
938 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
940 If the final integer expression is not present, a constant unity
941 expression is returned. We don't return MATCH_ERROR until after
942 the equals sign is seen. */
945 gfc_match_iterator (gfc_iterator *iter, int init_flag)
947 char name[GFC_MAX_SYMBOL_LEN + 1];
948 gfc_expr *var, *e1, *e2, *e3;
952 /* Match the start of an iterator without affecting the symbol table. */
954 start = gfc_current_locus;
955 m = gfc_match (" %n =", name);
956 gfc_current_locus = start;
961 m = gfc_match_variable (&var, 0);
965 gfc_match_char ('=');
969 if (var->ref != NULL)
971 gfc_error ("Loop variable at %C cannot be a sub-component");
975 if (var->symtree->n.sym->attr.intent == INTENT_IN)
977 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
978 var->symtree->n.sym->name);
982 var->symtree->n.sym->attr.implied_index = 1;
984 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
987 if (m == MATCH_ERROR)
990 if (gfc_match_char (',') != MATCH_YES)
993 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
996 if (m == MATCH_ERROR)
999 if (gfc_match_char (',') != MATCH_YES)
1001 e3 = gfc_int_expr (1);
1005 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1006 if (m == MATCH_ERROR)
1010 gfc_error ("Expected a step value in iterator at %C");
1022 gfc_error ("Syntax error in iterator at %C");
1033 /* Tries to match the next non-whitespace character on the input.
1034 This subroutine does not return MATCH_ERROR. */
1037 gfc_match_char (char c)
1041 where = gfc_current_locus;
1042 gfc_gobble_whitespace ();
1044 if (gfc_next_ascii_char () == c)
1047 gfc_current_locus = where;
1052 /* General purpose matching subroutine. The target string is a
1053 scanf-like format string in which spaces correspond to arbitrary
1054 whitespace (including no whitespace), characters correspond to
1055 themselves. The %-codes are:
1057 %% Literal percent sign
1058 %e Expression, pointer to a pointer is set
1059 %s Symbol, pointer to the symbol is set
1060 %n Name, character buffer is set to name
1061 %t Matches end of statement.
1062 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1063 %l Matches a statement label
1064 %v Matches a variable expression (an lvalue)
1065 % Matches a required space (in free form) and optional spaces. */
1068 gfc_match (const char *target, ...)
1070 gfc_st_label **label;
1079 old_loc = gfc_current_locus;
1080 va_start (argp, target);
1090 gfc_gobble_whitespace ();
1101 vp = va_arg (argp, void **);
1102 n = gfc_match_expr ((gfc_expr **) vp);
1113 vp = va_arg (argp, void **);
1114 n = gfc_match_variable ((gfc_expr **) vp, 0);
1125 vp = va_arg (argp, void **);
1126 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1137 np = va_arg (argp, char *);
1138 n = gfc_match_name (np);
1149 label = va_arg (argp, gfc_st_label **);
1150 n = gfc_match_st_label (label);
1161 ip = va_arg (argp, int *);
1162 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1173 if (gfc_match_eos () != MATCH_YES)
1181 if (gfc_match_space () == MATCH_YES)
1187 break; /* Fall through to character matcher. */
1190 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1195 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1196 expect an upper case character here! */
1197 gcc_assert (TOLOWER (c) == c);
1199 if (c == gfc_next_ascii_char ())
1209 /* Clean up after a failed match. */
1210 gfc_current_locus = old_loc;
1211 va_start (argp, target);
1214 for (; matches > 0; matches--)
1216 while (*p++ != '%');
1224 /* Matches that don't have to be undone */
1229 (void) va_arg (argp, void **);
1234 vp = va_arg (argp, void **);
1235 gfc_free_expr ((struct gfc_expr *)*vp);
1248 /*********************** Statement level matching **********************/
1250 /* Matches the start of a program unit, which is the program keyword
1251 followed by an obligatory symbol. */
1254 gfc_match_program (void)
1259 m = gfc_match ("% %s%t", &sym);
1263 gfc_error ("Invalid form of PROGRAM statement at %C");
1267 if (m == MATCH_ERROR)
1270 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1273 gfc_new_block = sym;
1279 /* Match a simple assignment statement. */
1282 gfc_match_assignment (void)
1284 gfc_expr *lvalue, *rvalue;
1288 old_loc = gfc_current_locus;
1291 m = gfc_match (" %v =", &lvalue);
1294 gfc_current_locus = old_loc;
1295 gfc_free_expr (lvalue);
1300 m = gfc_match (" %e%t", &rvalue);
1303 gfc_current_locus = old_loc;
1304 gfc_free_expr (lvalue);
1305 gfc_free_expr (rvalue);
1309 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1311 new_st.op = EXEC_ASSIGN;
1312 new_st.expr1 = lvalue;
1313 new_st.expr2 = rvalue;
1315 gfc_check_do_variable (lvalue->symtree);
1321 /* Match a pointer assignment statement. */
1324 gfc_match_pointer_assignment (void)
1326 gfc_expr *lvalue, *rvalue;
1330 old_loc = gfc_current_locus;
1332 lvalue = rvalue = NULL;
1333 gfc_matching_procptr_assignment = 0;
1335 m = gfc_match (" %v =>", &lvalue);
1342 if (lvalue->symtree->n.sym->attr.proc_pointer
1343 || gfc_is_proc_ptr_comp (lvalue, NULL))
1344 gfc_matching_procptr_assignment = 1;
1346 m = gfc_match (" %e%t", &rvalue);
1347 gfc_matching_procptr_assignment = 0;
1351 new_st.op = EXEC_POINTER_ASSIGN;
1352 new_st.expr1 = lvalue;
1353 new_st.expr2 = rvalue;
1358 gfc_current_locus = old_loc;
1359 gfc_free_expr (lvalue);
1360 gfc_free_expr (rvalue);
1365 /* We try to match an easy arithmetic IF statement. This only happens
1366 when just after having encountered a simple IF statement. This code
1367 is really duplicate with parts of the gfc_match_if code, but this is
1371 match_arithmetic_if (void)
1373 gfc_st_label *l1, *l2, *l3;
1377 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1381 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1382 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1383 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1385 gfc_free_expr (expr);
1389 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Arithmetic IF "
1390 "statement at %C") == FAILURE)
1393 new_st.op = EXEC_ARITHMETIC_IF;
1394 new_st.expr1 = expr;
1403 /* The IF statement is a bit of a pain. First of all, there are three
1404 forms of it, the simple IF, the IF that starts a block and the
1407 There is a problem with the simple IF and that is the fact that we
1408 only have a single level of undo information on symbols. What this
1409 means is for a simple IF, we must re-match the whole IF statement
1410 multiple times in order to guarantee that the symbol table ends up
1411 in the proper state. */
1413 static match match_simple_forall (void);
1414 static match match_simple_where (void);
1417 gfc_match_if (gfc_statement *if_type)
1420 gfc_st_label *l1, *l2, *l3;
1421 locus old_loc, old_loc2;
1425 n = gfc_match_label ();
1426 if (n == MATCH_ERROR)
1429 old_loc = gfc_current_locus;
1431 m = gfc_match (" if ( %e", &expr);
1435 old_loc2 = gfc_current_locus;
1436 gfc_current_locus = old_loc;
1438 if (gfc_match_parens () == MATCH_ERROR)
1441 gfc_current_locus = old_loc2;
1443 if (gfc_match_char (')') != MATCH_YES)
1445 gfc_error ("Syntax error in IF-expression at %C");
1446 gfc_free_expr (expr);
1450 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1456 gfc_error ("Block label not appropriate for arithmetic IF "
1458 gfc_free_expr (expr);
1462 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1463 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1464 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1466 gfc_free_expr (expr);
1470 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Arithmetic IF "
1471 "statement at %C") == FAILURE)
1474 new_st.op = EXEC_ARITHMETIC_IF;
1475 new_st.expr1 = expr;
1480 *if_type = ST_ARITHMETIC_IF;
1484 if (gfc_match (" then%t") == MATCH_YES)
1486 new_st.op = EXEC_IF;
1487 new_st.expr1 = expr;
1488 *if_type = ST_IF_BLOCK;
1494 gfc_error ("Block label is not appropriate for IF statement at %C");
1495 gfc_free_expr (expr);
1499 /* At this point the only thing left is a simple IF statement. At
1500 this point, n has to be MATCH_NO, so we don't have to worry about
1501 re-matching a block label. From what we've got so far, try
1502 matching an assignment. */
1504 *if_type = ST_SIMPLE_IF;
1506 m = gfc_match_assignment ();
1510 gfc_free_expr (expr);
1511 gfc_undo_symbols ();
1512 gfc_current_locus = old_loc;
1514 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1515 assignment was found. For MATCH_NO, continue to call the various
1517 if (m == MATCH_ERROR)
1520 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1522 m = gfc_match_pointer_assignment ();
1526 gfc_free_expr (expr);
1527 gfc_undo_symbols ();
1528 gfc_current_locus = old_loc;
1530 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1532 /* Look at the next keyword to see which matcher to call. Matching
1533 the keyword doesn't affect the symbol table, so we don't have to
1534 restore between tries. */
1536 #define match(string, subr, statement) \
1537 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1541 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1542 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1543 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1544 match ("call", gfc_match_call, ST_CALL)
1545 match ("close", gfc_match_close, ST_CLOSE)
1546 match ("continue", gfc_match_continue, ST_CONTINUE)
1547 match ("cycle", gfc_match_cycle, ST_CYCLE)
1548 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1549 match ("end file", gfc_match_endfile, ST_END_FILE)
1550 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1551 match ("exit", gfc_match_exit, ST_EXIT)
1552 match ("flush", gfc_match_flush, ST_FLUSH)
1553 match ("forall", match_simple_forall, ST_FORALL)
1554 match ("go to", gfc_match_goto, ST_GOTO)
1555 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1556 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1557 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1558 match ("open", gfc_match_open, ST_OPEN)
1559 match ("pause", gfc_match_pause, ST_NONE)
1560 match ("print", gfc_match_print, ST_WRITE)
1561 match ("read", gfc_match_read, ST_READ)
1562 match ("return", gfc_match_return, ST_RETURN)
1563 match ("rewind", gfc_match_rewind, ST_REWIND)
1564 match ("stop", gfc_match_stop, ST_STOP)
1565 match ("wait", gfc_match_wait, ST_WAIT)
1566 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1567 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1568 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1569 match ("where", match_simple_where, ST_WHERE)
1570 match ("write", gfc_match_write, ST_WRITE)
1572 /* The gfc_match_assignment() above may have returned a MATCH_NO
1573 where the assignment was to a named constant. Check that
1574 special case here. */
1575 m = gfc_match_assignment ();
1578 gfc_error ("Cannot assign to a named constant at %C");
1579 gfc_free_expr (expr);
1580 gfc_undo_symbols ();
1581 gfc_current_locus = old_loc;
1585 /* All else has failed, so give up. See if any of the matchers has
1586 stored an error message of some sort. */
1587 if (gfc_error_check () == 0)
1588 gfc_error ("Unclassifiable statement in IF-clause at %C");
1590 gfc_free_expr (expr);
1595 gfc_error ("Syntax error in IF-clause at %C");
1598 gfc_free_expr (expr);
1602 /* At this point, we've matched the single IF and the action clause
1603 is in new_st. Rearrange things so that the IF statement appears
1606 p = gfc_get_code ();
1607 p->next = gfc_get_code ();
1609 p->next->loc = gfc_current_locus;
1614 gfc_clear_new_st ();
1616 new_st.op = EXEC_IF;
1625 /* Match an ELSE statement. */
1628 gfc_match_else (void)
1630 char name[GFC_MAX_SYMBOL_LEN + 1];
1632 if (gfc_match_eos () == MATCH_YES)
1635 if (gfc_match_name (name) != MATCH_YES
1636 || gfc_current_block () == NULL
1637 || gfc_match_eos () != MATCH_YES)
1639 gfc_error ("Unexpected junk after ELSE statement at %C");
1643 if (strcmp (name, gfc_current_block ()->name) != 0)
1645 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1646 name, gfc_current_block ()->name);
1654 /* Match an ELSE IF statement. */
1657 gfc_match_elseif (void)
1659 char name[GFC_MAX_SYMBOL_LEN + 1];
1663 m = gfc_match (" ( %e ) then", &expr);
1667 if (gfc_match_eos () == MATCH_YES)
1670 if (gfc_match_name (name) != MATCH_YES
1671 || gfc_current_block () == NULL
1672 || gfc_match_eos () != MATCH_YES)
1674 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1678 if (strcmp (name, gfc_current_block ()->name) != 0)
1680 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1681 name, gfc_current_block ()->name);
1686 new_st.op = EXEC_IF;
1687 new_st.expr1 = expr;
1691 gfc_free_expr (expr);
1696 /* Free a gfc_iterator structure. */
1699 gfc_free_iterator (gfc_iterator *iter, int flag)
1705 gfc_free_expr (iter->var);
1706 gfc_free_expr (iter->start);
1707 gfc_free_expr (iter->end);
1708 gfc_free_expr (iter->step);
1715 /* Match a CRITICAL statement. */
1717 gfc_match_critical (void)
1719 gfc_st_label *label = NULL;
1721 if (gfc_match_label () == MATCH_ERROR)
1724 if (gfc_match (" critical") != MATCH_YES)
1727 if (gfc_match_st_label (&label) == MATCH_ERROR)
1730 if (gfc_match_eos () != MATCH_YES)
1732 gfc_syntax_error (ST_CRITICAL);
1736 if (gfc_pure (NULL))
1738 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1742 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CRITICAL statement at %C")
1746 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
1748 gfc_error ("Coarrays disabled at %C, use -fcoarray= to enable");
1752 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
1754 gfc_error ("Nested CRITICAL block at %C");
1758 new_st.op = EXEC_CRITICAL;
1761 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1768 /* Match a BLOCK statement. */
1771 gfc_match_block (void)
1775 if (gfc_match_label () == MATCH_ERROR)
1778 if (gfc_match (" block") != MATCH_YES)
1781 /* For this to be a correct BLOCK statement, the line must end now. */
1782 m = gfc_match_eos ();
1783 if (m == MATCH_ERROR)
1792 /* Match a DO statement. */
1797 gfc_iterator iter, *ip;
1799 gfc_st_label *label;
1802 old_loc = gfc_current_locus;
1805 iter.var = iter.start = iter.end = iter.step = NULL;
1807 m = gfc_match_label ();
1808 if (m == MATCH_ERROR)
1811 if (gfc_match (" do") != MATCH_YES)
1814 m = gfc_match_st_label (&label);
1815 if (m == MATCH_ERROR)
1818 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1820 if (gfc_match_eos () == MATCH_YES)
1822 iter.end = gfc_logical_expr (1, NULL);
1823 new_st.op = EXEC_DO_WHILE;
1827 /* Match an optional comma, if no comma is found, a space is obligatory. */
1828 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1831 /* Check for balanced parens. */
1833 if (gfc_match_parens () == MATCH_ERROR)
1836 /* See if we have a DO WHILE. */
1837 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1839 new_st.op = EXEC_DO_WHILE;
1843 /* The abortive DO WHILE may have done something to the symbol
1844 table, so we start over. */
1845 gfc_undo_symbols ();
1846 gfc_current_locus = old_loc;
1848 gfc_match_label (); /* This won't error. */
1849 gfc_match (" do "); /* This will work. */
1851 gfc_match_st_label (&label); /* Can't error out. */
1852 gfc_match_char (','); /* Optional comma. */
1854 m = gfc_match_iterator (&iter, 0);
1857 if (m == MATCH_ERROR)
1860 iter.var->symtree->n.sym->attr.implied_index = 0;
1861 gfc_check_do_variable (iter.var->symtree);
1863 if (gfc_match_eos () != MATCH_YES)
1865 gfc_syntax_error (ST_DO);
1869 new_st.op = EXEC_DO;
1873 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1876 new_st.label1 = label;
1878 if (new_st.op == EXEC_DO_WHILE)
1879 new_st.expr1 = iter.end;
1882 new_st.ext.iterator = ip = gfc_get_iterator ();
1889 gfc_free_iterator (&iter, 0);
1895 /* Match an EXIT or CYCLE statement. */
1898 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1900 gfc_state_data *p, *o;
1904 if (gfc_match_eos () == MATCH_YES)
1908 m = gfc_match ("% %s%t", &sym);
1909 if (m == MATCH_ERROR)
1913 gfc_syntax_error (st);
1917 if (sym->attr.flavor != FL_LABEL)
1919 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1920 sym->name, gfc_ascii_statement (st));
1925 /* Find the loop mentioned specified by the label (or lack of a label). */
1926 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1927 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1929 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1931 else if (p->state == COMP_CRITICAL)
1933 gfc_error("%s statement at %C leaves CRITICAL construct",
1934 gfc_ascii_statement (st));
1941 gfc_error ("%s statement at %C is not within a loop",
1942 gfc_ascii_statement (st));
1944 gfc_error ("%s statement at %C is not within loop '%s'",
1945 gfc_ascii_statement (st), sym->name);
1952 gfc_error ("%s statement at %C leaving OpenMP structured block",
1953 gfc_ascii_statement (st));
1956 else if (st == ST_EXIT
1957 && p->previous != NULL
1958 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1959 && (p->previous->head->op == EXEC_OMP_DO
1960 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1962 gcc_assert (p->previous->head->next != NULL);
1963 gcc_assert (p->previous->head->next->op == EXEC_DO
1964 || p->previous->head->next->op == EXEC_DO_WHILE);
1965 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1969 /* Save the first statement in the loop - needed by the backend. */
1970 new_st.ext.whichloop = p->head;
1978 /* Match the EXIT statement. */
1981 gfc_match_exit (void)
1983 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1987 /* Match the CYCLE statement. */
1990 gfc_match_cycle (void)
1992 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1996 /* Match a number or character constant after an (ALL) STOP or PAUSE statement. */
1999 gfc_match_stopcode (gfc_statement st)
2009 if (gfc_match_eos () != MATCH_YES)
2011 m = gfc_match_small_literal_int (&stop_code, &cnt);
2012 if (m == MATCH_ERROR)
2015 if (m == MATCH_YES && cnt > 5)
2017 gfc_error ("Too many digits in STOP code at %C");
2023 /* Try a character constant. */
2024 m = gfc_match_expr (&e);
2025 if (m == MATCH_ERROR)
2029 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
2033 if (gfc_match_eos () != MATCH_YES)
2037 if (gfc_pure (NULL))
2039 gfc_error ("%s statement not allowed in PURE procedure at %C",
2040 gfc_ascii_statement (st));
2044 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL) == SUCCESS)
2046 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2053 new_st.op = EXEC_STOP;
2056 new_st.op = EXEC_ERROR_STOP;
2059 new_st.op = EXEC_PAUSE;
2066 new_st.ext.stop_code = stop_code;
2071 gfc_syntax_error (st);
2080 /* Match the (deprecated) PAUSE statement. */
2083 gfc_match_pause (void)
2087 m = gfc_match_stopcode (ST_PAUSE);
2090 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
2099 /* Match the STOP statement. */
2102 gfc_match_stop (void)
2104 return gfc_match_stopcode (ST_STOP);
2108 /* Match the ERROR STOP statement. */
2111 gfc_match_error_stop (void)
2113 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: ERROR STOP statement at %C")
2117 return gfc_match_stopcode (ST_ERROR_STOP);
2121 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
2122 SYNC ALL [(sync-stat-list)]
2123 SYNC MEMORY [(sync-stat-list)]
2124 SYNC IMAGES (image-set [, sync-stat-list] )
2125 with sync-stat is int-expr or *. */
2128 sync_statement (gfc_statement st)
2131 gfc_expr *tmp, *imageset, *stat, *errmsg;
2132 bool saw_stat, saw_errmsg;
2134 tmp = imageset = stat = errmsg = NULL;
2135 saw_stat = saw_errmsg = false;
2137 if (gfc_pure (NULL))
2139 gfc_error ("Image control statement SYNC at %C in PURE procedure");
2143 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: SYNC statement at %C")
2147 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
2149 gfc_error ("Coarrays disabled at %C, use -fcoarray= to enable");
2153 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
2155 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
2159 if (gfc_match_eos () == MATCH_YES)
2161 if (st == ST_SYNC_IMAGES)
2166 if (gfc_match_char ('(') != MATCH_YES)
2169 if (st == ST_SYNC_IMAGES)
2171 /* Denote '*' as imageset == NULL. */
2172 m = gfc_match_char ('*');
2173 if (m == MATCH_ERROR)
2177 if (gfc_match ("%e", &imageset) != MATCH_YES)
2180 m = gfc_match_char (',');
2181 if (m == MATCH_ERROR)
2185 m = gfc_match_char (')');
2194 m = gfc_match (" stat = %v", &tmp);
2195 if (m == MATCH_ERROR)
2201 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
2207 if (gfc_match_char (',') == MATCH_YES)
2211 m = gfc_match (" errmsg = %v", &tmp);
2212 if (m == MATCH_ERROR)
2218 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
2224 if (gfc_match_char (',') == MATCH_YES)
2228 gfc_gobble_whitespace ();
2230 if (gfc_peek_char () == ')')
2236 if (gfc_match (" )%t") != MATCH_YES)
2243 new_st.op = EXEC_SYNC_ALL;
2245 case ST_SYNC_IMAGES:
2246 new_st.op = EXEC_SYNC_IMAGES;
2248 case ST_SYNC_MEMORY:
2249 new_st.op = EXEC_SYNC_MEMORY;
2255 new_st.expr1 = imageset;
2256 new_st.expr2 = stat;
2257 new_st.expr3 = errmsg;
2262 gfc_syntax_error (st);
2265 gfc_free_expr (tmp);
2266 gfc_free_expr (imageset);
2267 gfc_free_expr (stat);
2268 gfc_free_expr (errmsg);
2274 /* Match SYNC ALL statement. */
2277 gfc_match_sync_all (void)
2279 return sync_statement (ST_SYNC_ALL);
2283 /* Match SYNC IMAGES statement. */
2286 gfc_match_sync_images (void)
2288 return sync_statement (ST_SYNC_IMAGES);
2292 /* Match SYNC MEMORY statement. */
2295 gfc_match_sync_memory (void)
2297 return sync_statement (ST_SYNC_MEMORY);
2301 /* Match a CONTINUE statement. */
2304 gfc_match_continue (void)
2306 if (gfc_match_eos () != MATCH_YES)
2308 gfc_syntax_error (ST_CONTINUE);
2312 new_st.op = EXEC_CONTINUE;
2317 /* Match the (deprecated) ASSIGN statement. */
2320 gfc_match_assign (void)
2323 gfc_st_label *label;
2325 if (gfc_match (" %l", &label) == MATCH_YES)
2327 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
2329 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
2331 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
2336 expr->symtree->n.sym->attr.assign = 1;
2338 new_st.op = EXEC_LABEL_ASSIGN;
2339 new_st.label1 = label;
2340 new_st.expr1 = expr;
2348 /* Match the GO TO statement. As a computed GOTO statement is
2349 matched, it is transformed into an equivalent SELECT block. No
2350 tree is necessary, and the resulting jumps-to-jumps are
2351 specifically optimized away by the back end. */
2354 gfc_match_goto (void)
2356 gfc_code *head, *tail;
2359 gfc_st_label *label;
2363 if (gfc_match (" %l%t", &label) == MATCH_YES)
2365 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2368 new_st.op = EXEC_GOTO;
2369 new_st.label1 = label;
2373 /* The assigned GO TO statement. */
2375 if (gfc_match_variable (&expr, 0) == MATCH_YES)
2377 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
2382 new_st.op = EXEC_GOTO;
2383 new_st.expr1 = expr;
2385 if (gfc_match_eos () == MATCH_YES)
2388 /* Match label list. */
2389 gfc_match_char (',');
2390 if (gfc_match_char ('(') != MATCH_YES)
2392 gfc_syntax_error (ST_GOTO);
2399 m = gfc_match_st_label (&label);
2403 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2407 head = tail = gfc_get_code ();
2410 tail->block = gfc_get_code ();
2414 tail->label1 = label;
2415 tail->op = EXEC_GOTO;
2417 while (gfc_match_char (',') == MATCH_YES);
2419 if (gfc_match (")%t") != MATCH_YES)
2424 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2427 new_st.block = head;
2432 /* Last chance is a computed GO TO statement. */
2433 if (gfc_match_char ('(') != MATCH_YES)
2435 gfc_syntax_error (ST_GOTO);
2444 m = gfc_match_st_label (&label);
2448 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2452 head = tail = gfc_get_code ();
2455 tail->block = gfc_get_code ();
2459 cp = gfc_get_case ();
2460 cp->low = cp->high = gfc_int_expr (i++);
2462 tail->op = EXEC_SELECT;
2463 tail->ext.case_list = cp;
2465 tail->next = gfc_get_code ();
2466 tail->next->op = EXEC_GOTO;
2467 tail->next->label1 = label;
2469 while (gfc_match_char (',') == MATCH_YES);
2471 if (gfc_match_char (')') != MATCH_YES)
2476 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2480 /* Get the rest of the statement. */
2481 gfc_match_char (',');
2483 if (gfc_match (" %e%t", &expr) != MATCH_YES)
2486 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Computed GOTO "
2487 "at %C") == FAILURE)
2490 /* At this point, a computed GOTO has been fully matched and an
2491 equivalent SELECT statement constructed. */
2493 new_st.op = EXEC_SELECT;
2494 new_st.expr1 = NULL;
2496 /* Hack: For a "real" SELECT, the expression is in expr. We put
2497 it in expr2 so we can distinguish then and produce the correct
2499 new_st.expr2 = expr;
2500 new_st.block = head;
2504 gfc_syntax_error (ST_GOTO);
2506 gfc_free_statements (head);
2511 /* Frees a list of gfc_alloc structures. */
2514 gfc_free_alloc_list (gfc_alloc *p)
2521 gfc_free_expr (p->expr);
2527 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
2528 an accessible derived type. */
2531 match_derived_type_spec (gfc_typespec *ts)
2534 gfc_symbol *derived;
2536 old_locus = gfc_current_locus;
2538 if (gfc_match_symbol (&derived, 1) == MATCH_YES)
2540 if (derived->attr.flavor == FL_DERIVED)
2542 ts->type = BT_DERIVED;
2543 ts->u.derived = derived;
2548 /* Enforce F03:C476. */
2549 gfc_error ("'%s' at %L is not an accessible derived type",
2550 derived->name, &gfc_current_locus);
2555 gfc_current_locus = old_locus;
2560 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2561 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
2562 It only includes the intrinsic types from the Fortran 2003 standard
2563 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2564 the implicit_flag is not needed, so it was removed. Derived types are
2565 identified by their name alone. */
2568 match_type_spec (gfc_typespec *ts)
2574 old_locus = gfc_current_locus;
2576 if (gfc_match ("integer") == MATCH_YES)
2578 ts->type = BT_INTEGER;
2579 ts->kind = gfc_default_integer_kind;
2583 if (gfc_match ("real") == MATCH_YES)
2586 ts->kind = gfc_default_real_kind;
2590 if (gfc_match ("double precision") == MATCH_YES)
2593 ts->kind = gfc_default_double_kind;
2597 if (gfc_match ("complex") == MATCH_YES)
2599 ts->type = BT_COMPLEX;
2600 ts->kind = gfc_default_complex_kind;
2604 if (gfc_match ("character") == MATCH_YES)
2606 ts->type = BT_CHARACTER;
2610 if (gfc_match ("logical") == MATCH_YES)
2612 ts->type = BT_LOGICAL;
2613 ts->kind = gfc_default_logical_kind;
2617 m = match_derived_type_spec (ts);
2620 old_locus = gfc_current_locus;
2621 if (gfc_match (" :: ") != MATCH_YES)
2623 gfc_current_locus = old_locus;
2624 /* Enfore F03:C401. */
2625 if (ts->u.derived->attr.abstract)
2627 gfc_error ("Derived type '%s' at %L may not be ABSTRACT",
2628 ts->u.derived->name, &old_locus);
2633 else if (m == MATCH_ERROR && gfc_match (" :: ") == MATCH_YES)
2636 /* If a type is not matched, simply return MATCH_NO. */
2637 gfc_current_locus = old_locus;
2642 gfc_gobble_whitespace ();
2643 if (gfc_peek_ascii_char () == '*')
2645 gfc_error ("Invalid type-spec at %C");
2649 m = gfc_match_kind_spec (ts, false);
2652 m = MATCH_YES; /* No kind specifier found. */
2658 m = gfc_match_char_spec (ts);
2661 m = MATCH_YES; /* No kind specifier found. */
2667 /* Match an ALLOCATE statement. */
2670 gfc_match_allocate (void)
2672 gfc_alloc *head, *tail;
2673 gfc_expr *stat, *errmsg, *tmp, *source;
2678 bool saw_stat, saw_errmsg, saw_source, b1, b2, b3;
2681 stat = errmsg = source = tmp = NULL;
2682 saw_stat = saw_errmsg = saw_source = false;
2684 if (gfc_match_char ('(') != MATCH_YES)
2687 /* Match an optional type-spec. */
2688 old_locus = gfc_current_locus;
2689 m = match_type_spec (&ts);
2690 if (m == MATCH_ERROR)
2692 else if (m == MATCH_NO)
2693 ts.type = BT_UNKNOWN;
2696 if (gfc_match (" :: ") == MATCH_YES)
2698 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: typespec in "
2699 "ALLOCATE at %L", &old_locus) == FAILURE)
2704 ts.type = BT_UNKNOWN;
2705 gfc_current_locus = old_locus;
2712 head = tail = gfc_get_alloc ();
2715 tail->next = gfc_get_alloc ();
2719 m = gfc_match_variable (&tail->expr, 0);
2722 if (m == MATCH_ERROR)
2725 if (gfc_check_do_variable (tail->expr->symtree))
2728 if (gfc_pure (NULL) && gfc_impure_variable (tail->expr->symtree->n.sym))
2730 gfc_error ("Bad allocate-object at %C for a PURE procedure");
2734 /* The ALLOCATE statement had an optional typespec. Check the
2736 if (ts.type != BT_UNKNOWN)
2738 /* Enforce F03:C624. */
2739 if (!gfc_type_compatible (&tail->expr->ts, &ts))
2741 gfc_error ("Type of entity at %L is type incompatible with "
2742 "typespec", &tail->expr->where);
2746 /* Enforce F03:C627. */
2747 if (ts.kind != tail->expr->ts.kind)
2749 gfc_error ("Kind type parameter for entity at %L differs from "
2750 "the kind type parameter of the typespec",
2751 &tail->expr->where);
2756 if (tail->expr->ts.type == BT_DERIVED)
2757 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
2759 /* FIXME: disable the checking on derived types and arrays. */
2760 sym = tail->expr->symtree->n.sym;
2761 b1 = !(tail->expr->ref
2762 && (tail->expr->ref->type == REF_COMPONENT
2763 || tail->expr->ref->type == REF_ARRAY));
2764 if (sym && sym->ts.type == BT_CLASS)
2765 b2 = !(sym->ts.u.derived->components->attr.allocatable
2766 || sym->ts.u.derived->components->attr.pointer);
2768 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
2769 || sym->attr.proc_pointer);
2770 b3 = sym && sym->ns && sym->ns->proc_name
2771 && (sym->ns->proc_name->attr.allocatable
2772 || sym->ns->proc_name->attr.pointer
2773 || sym->ns->proc_name->attr.proc_pointer);
2774 if (b1 && b2 && !b3)
2776 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
2777 "or an allocatable variable");
2781 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
2783 gfc_error ("Shape specification for allocatable scalar at %C");
2787 if (gfc_match_char (',') != MATCH_YES)
2792 m = gfc_match (" stat = %v", &tmp);
2793 if (m == MATCH_ERROR)
2800 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
2807 if (gfc_check_do_variable (stat->symtree))
2810 if (gfc_match_char (',') == MATCH_YES)
2811 goto alloc_opt_list;
2814 m = gfc_match (" errmsg = %v", &tmp);
2815 if (m == MATCH_ERROR)
2819 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ERRMSG tag at %L",
2820 &tmp->where) == FAILURE)
2826 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
2833 if (gfc_match_char (',') == MATCH_YES)
2834 goto alloc_opt_list;
2837 m = gfc_match (" source = %e", &tmp);
2838 if (m == MATCH_ERROR)
2842 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SOURCE tag at %L",
2843 &tmp->where) == FAILURE)
2849 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
2853 /* The next 2 conditionals check C631. */
2854 if (ts.type != BT_UNKNOWN)
2856 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
2857 &tmp->where, &old_locus);
2863 gfc_error ("SOURCE tag at %L requires only a single entity in "
2864 "the allocation-list", &tmp->where);
2871 if (gfc_match_char (',') == MATCH_YES)
2872 goto alloc_opt_list;
2875 gfc_gobble_whitespace ();
2877 if (gfc_peek_char () == ')')
2882 if (gfc_match (" )%t") != MATCH_YES)
2885 new_st.op = EXEC_ALLOCATE;
2886 new_st.expr1 = stat;
2887 new_st.expr2 = errmsg;
2888 new_st.expr3 = source;
2889 new_st.ext.alloc.list = head;
2890 new_st.ext.alloc.ts = ts;
2895 gfc_syntax_error (ST_ALLOCATE);
2898 gfc_free_expr (errmsg);
2899 gfc_free_expr (source);
2900 gfc_free_expr (stat);
2901 gfc_free_expr (tmp);
2902 gfc_free_alloc_list (head);
2907 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
2908 a set of pointer assignments to intrinsic NULL(). */
2911 gfc_match_nullify (void)
2919 if (gfc_match_char ('(') != MATCH_YES)
2924 m = gfc_match_variable (&p, 0);
2925 if (m == MATCH_ERROR)
2930 if (gfc_check_do_variable (p->symtree))
2933 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
2935 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
2939 /* build ' => NULL() '. */
2940 e = gfc_get_expr ();
2941 e->where = gfc_current_locus;
2942 e->expr_type = EXPR_NULL;
2943 e->ts.type = BT_UNKNOWN;
2945 /* Chain to list. */
2950 tail->next = gfc_get_code ();
2954 tail->op = EXEC_POINTER_ASSIGN;
2958 if (gfc_match (" )%t") == MATCH_YES)
2960 if (gfc_match_char (',') != MATCH_YES)
2967 gfc_syntax_error (ST_NULLIFY);
2970 gfc_free_statements (new_st.next);
2972 gfc_free_expr (new_st.expr1);
2973 new_st.expr1 = NULL;
2974 gfc_free_expr (new_st.expr2);
2975 new_st.expr2 = NULL;
2980 /* Match a DEALLOCATE statement. */
2983 gfc_match_deallocate (void)
2985 gfc_alloc *head, *tail;
2986 gfc_expr *stat, *errmsg, *tmp;
2989 bool saw_stat, saw_errmsg, b1, b2;
2992 stat = errmsg = tmp = NULL;
2993 saw_stat = saw_errmsg = false;
2995 if (gfc_match_char ('(') != MATCH_YES)
3001 head = tail = gfc_get_alloc ();
3004 tail->next = gfc_get_alloc ();
3008 m = gfc_match_variable (&tail->expr, 0);
3009 if (m == MATCH_ERROR)
3014 if (gfc_check_do_variable (tail->expr->symtree))
3017 sym = tail->expr->symtree->n.sym;
3019 if (gfc_pure (NULL) && gfc_impure_variable (sym))
3021 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
3025 /* FIXME: disable the checking on derived types. */
3026 b1 = !(tail->expr->ref
3027 && (tail->expr->ref->type == REF_COMPONENT
3028 || tail->expr->ref->type == REF_ARRAY));
3029 if (sym && sym->ts.type == BT_CLASS)
3030 b2 = !(sym->ts.u.derived->components->attr.allocatable
3031 || sym->ts.u.derived->components->attr.pointer);
3033 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
3034 || sym->attr.proc_pointer);
3037 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
3038 "or an allocatable variable");
3042 if (gfc_match_char (',') != MATCH_YES)
3047 m = gfc_match (" stat = %v", &tmp);
3048 if (m == MATCH_ERROR)
3054 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3055 gfc_free_expr (tmp);
3062 if (gfc_check_do_variable (stat->symtree))
3065 if (gfc_match_char (',') == MATCH_YES)
3066 goto dealloc_opt_list;
3069 m = gfc_match (" errmsg = %v", &tmp);
3070 if (m == MATCH_ERROR)
3074 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ERRMSG at %L",
3075 &tmp->where) == FAILURE)
3080 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3081 gfc_free_expr (tmp);
3088 if (gfc_match_char (',') == MATCH_YES)
3089 goto dealloc_opt_list;
3092 gfc_gobble_whitespace ();
3094 if (gfc_peek_char () == ')')
3098 if (gfc_match (" )%t") != MATCH_YES)
3101 new_st.op = EXEC_DEALLOCATE;
3102 new_st.expr1 = stat;
3103 new_st.expr2 = errmsg;
3104 new_st.ext.alloc.list = head;
3109 gfc_syntax_error (ST_DEALLOCATE);
3112 gfc_free_expr (errmsg);
3113 gfc_free_expr (stat);
3114 gfc_free_alloc_list (head);
3119 /* Match a RETURN statement. */
3122 gfc_match_return (void)
3126 gfc_compile_state s;
3130 if (gfc_find_state (COMP_CRITICAL) == SUCCESS)
3132 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
3136 if (gfc_match_eos () == MATCH_YES)
3139 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
3141 gfc_error ("Alternate RETURN statement at %C is only allowed within "
3146 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Alternate RETURN "
3147 "at %C") == FAILURE)
3150 if (gfc_current_form == FORM_FREE)
3152 /* The following are valid, so we can't require a blank after the
3156 char c = gfc_peek_ascii_char ();
3157 if (ISALPHA (c) || ISDIGIT (c))
3161 m = gfc_match (" %e%t", &e);
3164 if (m == MATCH_ERROR)
3167 gfc_syntax_error (ST_RETURN);
3174 gfc_enclosing_unit (&s);
3175 if (s == COMP_PROGRAM
3176 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
3177 "main program at %C") == FAILURE)
3180 new_st.op = EXEC_RETURN;
3187 /* Match the call of a type-bound procedure, if CALL%var has already been
3188 matched and var found to be a derived-type variable. */
3191 match_typebound_call (gfc_symtree* varst)
3196 base = gfc_get_expr ();
3197 base->expr_type = EXPR_VARIABLE;
3198 base->symtree = varst;
3199 base->where = gfc_current_locus;
3200 gfc_set_sym_referenced (varst->n.sym);
3202 m = gfc_match_varspec (base, 0, true, true);
3204 gfc_error ("Expected component reference at %C");
3208 if (gfc_match_eos () != MATCH_YES)
3210 gfc_error ("Junk after CALL at %C");
3214 if (base->expr_type == EXPR_COMPCALL)
3215 new_st.op = EXEC_COMPCALL;
3216 else if (base->expr_type == EXPR_PPC)
3217 new_st.op = EXEC_CALL_PPC;
3220 gfc_error ("Expected type-bound procedure or procedure pointer component "
3224 new_st.expr1 = base;
3230 /* Match a CALL statement. The tricky part here are possible
3231 alternate return specifiers. We handle these by having all
3232 "subroutines" actually return an integer via a register that gives
3233 the return number. If the call specifies alternate returns, we
3234 generate code for a SELECT statement whose case clauses contain
3235 GOTOs to the various labels. */
3238 gfc_match_call (void)
3240 char name[GFC_MAX_SYMBOL_LEN + 1];
3241 gfc_actual_arglist *a, *arglist;
3251 m = gfc_match ("% %n", name);
3257 if (gfc_get_ha_sym_tree (name, &st))
3262 /* If this is a variable of derived-type, it probably starts a type-bound
3264 if ((sym->attr.flavor != FL_PROCEDURE
3265 || gfc_is_function_return_value (sym, gfc_current_ns))
3266 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
3267 return match_typebound_call (st);
3269 /* If it does not seem to be callable (include functions so that the
3270 right association is made. They are thrown out in resolution.)
3272 if (!sym->attr.generic
3273 && !sym->attr.subroutine
3274 && !sym->attr.function)
3276 if (!(sym->attr.external && !sym->attr.referenced))
3278 /* ...create a symbol in this scope... */
3279 if (sym->ns != gfc_current_ns
3280 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
3283 if (sym != st->n.sym)
3287 /* ...and then to try to make the symbol into a subroutine. */
3288 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
3292 gfc_set_sym_referenced (sym);
3294 if (gfc_match_eos () != MATCH_YES)
3296 m = gfc_match_actual_arglist (1, &arglist);
3299 if (m == MATCH_ERROR)
3302 if (gfc_match_eos () != MATCH_YES)
3306 /* If any alternate return labels were found, construct a SELECT
3307 statement that will jump to the right place. */
3310 for (a = arglist; a; a = a->next)
3311 if (a->expr == NULL)
3316 gfc_symtree *select_st;
3317 gfc_symbol *select_sym;
3318 char name[GFC_MAX_SYMBOL_LEN + 1];
3320 new_st.next = c = gfc_get_code ();
3321 c->op = EXEC_SELECT;
3322 sprintf (name, "_result_%s", sym->name);
3323 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
3325 select_sym = select_st->n.sym;
3326 select_sym->ts.type = BT_INTEGER;
3327 select_sym->ts.kind = gfc_default_integer_kind;
3328 gfc_set_sym_referenced (select_sym);
3329 c->expr1 = gfc_get_expr ();
3330 c->expr1->expr_type = EXPR_VARIABLE;
3331 c->expr1->symtree = select_st;
3332 c->expr1->ts = select_sym->ts;
3333 c->expr1->where = gfc_current_locus;
3336 for (a = arglist; a; a = a->next)
3338 if (a->expr != NULL)
3341 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
3346 c->block = gfc_get_code ();
3348 c->op = EXEC_SELECT;
3350 new_case = gfc_get_case ();
3351 new_case->high = new_case->low = gfc_int_expr (i);
3352 c->ext.case_list = new_case;
3354 c->next = gfc_get_code ();
3355 c->next->op = EXEC_GOTO;
3356 c->next->label1 = a->label;
3360 new_st.op = EXEC_CALL;
3361 new_st.symtree = st;
3362 new_st.ext.actual = arglist;
3367 gfc_syntax_error (ST_CALL);
3370 gfc_free_actual_arglist (arglist);
3375 /* Given a name, return a pointer to the common head structure,
3376 creating it if it does not exist. If FROM_MODULE is nonzero, we
3377 mangle the name so that it doesn't interfere with commons defined
3378 in the using namespace.
3379 TODO: Add to global symbol tree. */
3382 gfc_get_common (const char *name, int from_module)
3385 static int serial = 0;
3386 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
3390 /* A use associated common block is only needed to correctly layout
3391 the variables it contains. */
3392 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
3393 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
3397 st = gfc_find_symtree (gfc_current_ns->common_root, name);
3400 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
3403 if (st->n.common == NULL)
3405 st->n.common = gfc_get_common_head ();
3406 st->n.common->where = gfc_current_locus;
3407 strcpy (st->n.common->name, name);
3410 return st->n.common;
3414 /* Match a common block name. */
3416 match match_common_name (char *name)
3420 if (gfc_match_char ('/') == MATCH_NO)
3426 if (gfc_match_char ('/') == MATCH_YES)
3432 m = gfc_match_name (name);
3434 if (m == MATCH_ERROR)
3436 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
3439 gfc_error ("Syntax error in common block name at %C");
3444 /* Match a COMMON statement. */
3447 gfc_match_common (void)
3449 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
3450 char name[GFC_MAX_SYMBOL_LEN + 1];
3457 old_blank_common = gfc_current_ns->blank_common.head;
3458 if (old_blank_common)
3460 while (old_blank_common->common_next)
3461 old_blank_common = old_blank_common->common_next;
3468 m = match_common_name (name);
3469 if (m == MATCH_ERROR)
3472 gsym = gfc_get_gsymbol (name);
3473 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
3475 gfc_error ("Symbol '%s' at %C is already an external symbol that "
3476 "is not COMMON", name);
3480 if (gsym->type == GSYM_UNKNOWN)
3482 gsym->type = GSYM_COMMON;
3483 gsym->where = gfc_current_locus;
3489 if (name[0] == '\0')
3491 t = &gfc_current_ns->blank_common;
3492 if (t->head == NULL)
3493 t->where = gfc_current_locus;
3497 t = gfc_get_common (name, 0);
3506 while (tail->common_next)
3507 tail = tail->common_next;
3510 /* Grab the list of symbols. */
3513 m = gfc_match_symbol (&sym, 0);
3514 if (m == MATCH_ERROR)
3519 /* Store a ref to the common block for error checking. */
3520 sym->common_block = t;
3522 /* See if we know the current common block is bind(c), and if
3523 so, then see if we can check if the symbol is (which it'll
3524 need to be). This can happen if the bind(c) attr stmt was
3525 applied to the common block, and the variable(s) already
3526 defined, before declaring the common block. */
3527 if (t->is_bind_c == 1)
3529 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
3531 /* If we find an error, just print it and continue,
3532 cause it's just semantic, and we can see if there
3534 gfc_error_now ("Variable '%s' at %L in common block '%s' "
3535 "at %C must be declared with a C "
3536 "interoperable kind since common block "
3538 sym->name, &(sym->declared_at), t->name,
3542 if (sym->attr.is_bind_c == 1)
3543 gfc_error_now ("Variable '%s' in common block "
3544 "'%s' at %C can not be bind(c) since "
3545 "it is not global", sym->name, t->name);
3548 if (sym->attr.in_common)
3550 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
3555 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
3556 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
3558 if (gfc_notify_std (GFC_STD_GNU, "Initialized symbol '%s' at %C "
3559 "can only be COMMON in "
3560 "BLOCK DATA", sym->name)
3565 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
3569 tail->common_next = sym;
3575 /* Deal with an optional array specification after the
3577 m = gfc_match_array_spec (&as, true, true);
3578 if (m == MATCH_ERROR)
3583 if (as->type != AS_EXPLICIT)
3585 gfc_error ("Array specification for symbol '%s' in COMMON "
3586 "at %C must be explicit", sym->name);
3590 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
3593 if (sym->attr.pointer)
3595 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
3596 "POINTER array", sym->name);
3605 sym->common_head = t;
3607 /* Check to see if the symbol is already in an equivalence group.
3608 If it is, set the other members as being in common. */
3609 if (sym->attr.in_equivalence)
3611 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
3613 for (e2 = e1; e2; e2 = e2->eq)
3614 if (e2->expr->symtree->n.sym == sym)
3621 for (e2 = e1; e2; e2 = e2->eq)
3623 other = e2->expr->symtree->n.sym;
3624 if (other->common_head
3625 && other->common_head != sym->common_head)
3627 gfc_error ("Symbol '%s', in COMMON block '%s' at "
3628 "%C is being indirectly equivalenced to "
3629 "another COMMON block '%s'",
3630 sym->name, sym->common_head->name,
3631 other->common_head->name);
3634 other->attr.in_common = 1;
3635 other->common_head = t;
3641 gfc_gobble_whitespace ();
3642 if (gfc_match_eos () == MATCH_YES)
3644 if (gfc_peek_ascii_char () == '/')
3646 if (gfc_match_char (',') != MATCH_YES)
3648 gfc_gobble_whitespace ();
3649 if (gfc_peek_ascii_char () == '/')
3658 gfc_syntax_error (ST_COMMON);
3661 if (old_blank_common)
3662 old_blank_common->common_next = NULL;
3664 gfc_current_ns->blank_common.head = NULL;
3665 gfc_free_array_spec (as);
3670 /* Match a BLOCK DATA program unit. */
3673 gfc_match_block_data (void)
3675 char name[GFC_MAX_SYMBOL_LEN + 1];
3679 if (gfc_match_eos () == MATCH_YES)
3681 gfc_new_block = NULL;
3685 m = gfc_match ("% %n%t", name);
3689 if (gfc_get_symbol (name, NULL, &sym))
3692 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
3695 gfc_new_block = sym;
3701 /* Free a namelist structure. */