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 if (gfc_implicit_pure (NULL
))
1758 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
1760 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: CRITICAL statement at %C")
1764 if (gfc_option
.coarray
== GFC_FCOARRAY_NONE
)
1766 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
1770 if (gfc_find_state (COMP_CRITICAL
) == SUCCESS
)
1772 gfc_error ("Nested CRITICAL block at %C");
1776 new_st
.op
= EXEC_CRITICAL
;
1779 && gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
1786 /* Match a BLOCK statement. */
1789 gfc_match_block (void)
1793 if (gfc_match_label () == MATCH_ERROR
)
1796 if (gfc_match (" block") != MATCH_YES
)
1799 /* For this to be a correct BLOCK statement, the line must end now. */
1800 m
= gfc_match_eos ();
1801 if (m
== MATCH_ERROR
)
1810 /* Match an ASSOCIATE statement. */
1813 gfc_match_associate (void)
1815 if (gfc_match_label () == MATCH_ERROR
)
1818 if (gfc_match (" associate") != MATCH_YES
)
1821 /* Match the association list. */
1822 if (gfc_match_char ('(') != MATCH_YES
)
1824 gfc_error ("Expected association list at %C");
1827 new_st
.ext
.block
.assoc
= NULL
;
1830 gfc_association_list
* newAssoc
= gfc_get_association_list ();
1831 gfc_association_list
* a
;
1833 /* Match the next association. */
1834 if (gfc_match (" %n => %e", newAssoc
->name
, &newAssoc
->target
)
1837 gfc_error ("Expected association at %C");
1838 goto assocListError
;
1840 newAssoc
->where
= gfc_current_locus
;
1842 /* Check that the current name is not yet in the list. */
1843 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
1844 if (!strcmp (a
->name
, newAssoc
->name
))
1846 gfc_error ("Duplicate name '%s' in association at %C",
1848 goto assocListError
;
1851 /* The target expression must not be coindexed. */
1852 if (gfc_is_coindexed (newAssoc
->target
))
1854 gfc_error ("Association target at %C must not be coindexed");
1855 goto assocListError
;
1858 /* The `variable' field is left blank for now; because the target is not
1859 yet resolved, we can't use gfc_has_vector_subscript to determine it
1860 for now. This is set during resolution. */
1862 /* Put it into the list. */
1863 newAssoc
->next
= new_st
.ext
.block
.assoc
;
1864 new_st
.ext
.block
.assoc
= newAssoc
;
1866 /* Try next one or end if closing parenthesis is found. */
1867 gfc_gobble_whitespace ();
1868 if (gfc_peek_char () == ')')
1870 if (gfc_match_char (',') != MATCH_YES
)
1872 gfc_error ("Expected ')' or ',' at %C");
1882 if (gfc_match_char (')') != MATCH_YES
)
1884 /* This should never happen as we peek above. */
1888 if (gfc_match_eos () != MATCH_YES
)
1890 gfc_error ("Junk after ASSOCIATE statement at %C");
1897 gfc_free_association_list (new_st
.ext
.block
.assoc
);
1902 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1903 an accessible derived type. */
1906 match_derived_type_spec (gfc_typespec
*ts
)
1908 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1910 gfc_symbol
*derived
;
1912 old_locus
= gfc_current_locus
;
1914 if (gfc_match ("%n", name
) != MATCH_YES
)
1916 gfc_current_locus
= old_locus
;
1920 gfc_find_symbol (name
, NULL
, 1, &derived
);
1922 if (derived
&& derived
->attr
.flavor
== FL_PROCEDURE
&& derived
->attr
.generic
)
1923 derived
= gfc_find_dt_in_generic (derived
);
1925 if (derived
&& derived
->attr
.flavor
== FL_DERIVED
)
1927 ts
->type
= BT_DERIVED
;
1928 ts
->u
.derived
= derived
;
1932 gfc_current_locus
= old_locus
;
1937 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
1938 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
1939 It only includes the intrinsic types from the Fortran 2003 standard
1940 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
1941 the implicit_flag is not needed, so it was removed. Derived types are
1942 identified by their name alone. */
1945 match_type_spec (gfc_typespec
*ts
)
1951 gfc_gobble_whitespace ();
1952 old_locus
= gfc_current_locus
;
1954 if (match_derived_type_spec (ts
) == MATCH_YES
)
1956 /* Enforce F03:C401. */
1957 if (ts
->u
.derived
->attr
.abstract
)
1959 gfc_error ("Derived type '%s' at %L may not be ABSTRACT",
1960 ts
->u
.derived
->name
, &old_locus
);
1966 if (gfc_match ("integer") == MATCH_YES
)
1968 ts
->type
= BT_INTEGER
;
1969 ts
->kind
= gfc_default_integer_kind
;
1973 if (gfc_match ("real") == MATCH_YES
)
1976 ts
->kind
= gfc_default_real_kind
;
1980 if (gfc_match ("double precision") == MATCH_YES
)
1983 ts
->kind
= gfc_default_double_kind
;
1987 if (gfc_match ("complex") == MATCH_YES
)
1989 ts
->type
= BT_COMPLEX
;
1990 ts
->kind
= gfc_default_complex_kind
;
1994 if (gfc_match ("character") == MATCH_YES
)
1996 ts
->type
= BT_CHARACTER
;
1998 m
= gfc_match_char_spec (ts
);
2006 if (gfc_match ("logical") == MATCH_YES
)
2008 ts
->type
= BT_LOGICAL
;
2009 ts
->kind
= gfc_default_logical_kind
;
2013 /* If a type is not matched, simply return MATCH_NO. */
2014 gfc_current_locus
= old_locus
;
2019 gfc_gobble_whitespace ();
2020 if (gfc_peek_ascii_char () == '*')
2022 gfc_error ("Invalid type-spec at %C");
2026 m
= gfc_match_kind_spec (ts
, false);
2029 m
= MATCH_YES
; /* No kind specifier found. */
2035 /******************** FORALL subroutines ********************/
2037 /* Free a list of FORALL iterators. */
2040 gfc_free_forall_iterator (gfc_forall_iterator
*iter
)
2042 gfc_forall_iterator
*next
;
2047 gfc_free_expr (iter
->var
);
2048 gfc_free_expr (iter
->start
);
2049 gfc_free_expr (iter
->end
);
2050 gfc_free_expr (iter
->stride
);
2057 /* Match an iterator as part of a FORALL statement. The format is:
2059 <var> = <start>:<end>[:<stride>]
2061 On MATCH_NO, the caller tests for the possibility that there is a
2062 scalar mask expression. */
2065 match_forall_iterator (gfc_forall_iterator
**result
)
2067 gfc_forall_iterator
*iter
;
2071 where
= gfc_current_locus
;
2072 iter
= XCNEW (gfc_forall_iterator
);
2074 m
= gfc_match_expr (&iter
->var
);
2078 if (gfc_match_char ('=') != MATCH_YES
2079 || iter
->var
->expr_type
!= EXPR_VARIABLE
)
2085 m
= gfc_match_expr (&iter
->start
);
2089 if (gfc_match_char (':') != MATCH_YES
)
2092 m
= gfc_match_expr (&iter
->end
);
2095 if (m
== MATCH_ERROR
)
2098 if (gfc_match_char (':') == MATCH_NO
)
2099 iter
->stride
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2102 m
= gfc_match_expr (&iter
->stride
);
2105 if (m
== MATCH_ERROR
)
2109 /* Mark the iteration variable's symbol as used as a FORALL index. */
2110 iter
->var
->symtree
->n
.sym
->forall_index
= true;
2116 gfc_error ("Syntax error in FORALL iterator at %C");
2121 gfc_current_locus
= where
;
2122 gfc_free_forall_iterator (iter
);
2127 /* Match the header of a FORALL statement. */
2130 match_forall_header (gfc_forall_iterator
**phead
, gfc_expr
**mask
)
2132 gfc_forall_iterator
*head
, *tail
, *new_iter
;
2136 gfc_gobble_whitespace ();
2141 if (gfc_match_char ('(') != MATCH_YES
)
2144 m
= match_forall_iterator (&new_iter
);
2145 if (m
== MATCH_ERROR
)
2150 head
= tail
= new_iter
;
2154 if (gfc_match_char (',') != MATCH_YES
)
2157 m
= match_forall_iterator (&new_iter
);
2158 if (m
== MATCH_ERROR
)
2163 tail
->next
= new_iter
;
2168 /* Have to have a mask expression. */
2170 m
= gfc_match_expr (&msk
);
2173 if (m
== MATCH_ERROR
)
2179 if (gfc_match_char (')') == MATCH_NO
)
2187 gfc_syntax_error (ST_FORALL
);
2190 gfc_free_expr (msk
);
2191 gfc_free_forall_iterator (head
);
2196 /* Match the rest of a simple FORALL statement that follows an
2200 match_simple_forall (void)
2202 gfc_forall_iterator
*head
;
2211 m
= match_forall_header (&head
, &mask
);
2218 m
= gfc_match_assignment ();
2220 if (m
== MATCH_ERROR
)
2224 m
= gfc_match_pointer_assignment ();
2225 if (m
== MATCH_ERROR
)
2231 c
= gfc_get_code ();
2233 c
->loc
= gfc_current_locus
;
2235 if (gfc_match_eos () != MATCH_YES
)
2238 gfc_clear_new_st ();
2239 new_st
.op
= EXEC_FORALL
;
2240 new_st
.expr1
= mask
;
2241 new_st
.ext
.forall_iterator
= head
;
2242 new_st
.block
= gfc_get_code ();
2244 new_st
.block
->op
= EXEC_FORALL
;
2245 new_st
.block
->next
= c
;
2250 gfc_syntax_error (ST_FORALL
);
2253 gfc_free_forall_iterator (head
);
2254 gfc_free_expr (mask
);
2260 /* Match a FORALL statement. */
2263 gfc_match_forall (gfc_statement
*st
)
2265 gfc_forall_iterator
*head
;
2274 m0
= gfc_match_label ();
2275 if (m0
== MATCH_ERROR
)
2278 m
= gfc_match (" forall");
2282 m
= match_forall_header (&head
, &mask
);
2283 if (m
== MATCH_ERROR
)
2288 if (gfc_match_eos () == MATCH_YES
)
2290 *st
= ST_FORALL_BLOCK
;
2291 new_st
.op
= EXEC_FORALL
;
2292 new_st
.expr1
= mask
;
2293 new_st
.ext
.forall_iterator
= head
;
2297 m
= gfc_match_assignment ();
2298 if (m
== MATCH_ERROR
)
2302 m
= gfc_match_pointer_assignment ();
2303 if (m
== MATCH_ERROR
)
2309 c
= gfc_get_code ();
2311 c
->loc
= gfc_current_locus
;
2313 gfc_clear_new_st ();
2314 new_st
.op
= EXEC_FORALL
;
2315 new_st
.expr1
= mask
;
2316 new_st
.ext
.forall_iterator
= head
;
2317 new_st
.block
= gfc_get_code ();
2318 new_st
.block
->op
= EXEC_FORALL
;
2319 new_st
.block
->next
= c
;
2325 gfc_syntax_error (ST_FORALL
);
2328 gfc_free_forall_iterator (head
);
2329 gfc_free_expr (mask
);
2330 gfc_free_statements (c
);
2335 /* Match a DO statement. */
2340 gfc_iterator iter
, *ip
;
2342 gfc_st_label
*label
;
2345 old_loc
= gfc_current_locus
;
2348 iter
.var
= iter
.start
= iter
.end
= iter
.step
= NULL
;
2350 m
= gfc_match_label ();
2351 if (m
== MATCH_ERROR
)
2354 if (gfc_match (" do") != MATCH_YES
)
2357 m
= gfc_match_st_label (&label
);
2358 if (m
== MATCH_ERROR
)
2361 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2363 if (gfc_match_eos () == MATCH_YES
)
2365 iter
.end
= gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, true);
2366 new_st
.op
= EXEC_DO_WHILE
;
2370 /* Match an optional comma, if no comma is found, a space is obligatory. */
2371 if (gfc_match_char (',') != MATCH_YES
&& gfc_match ("% ") != MATCH_YES
)
2374 /* Check for balanced parens. */
2376 if (gfc_match_parens () == MATCH_ERROR
)
2379 if (gfc_match (" concurrent") == MATCH_YES
)
2381 gfc_forall_iterator
*head
;
2384 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: DO CONCURRENT "
2385 "construct at %C") == FAILURE
)
2391 m
= match_forall_header (&head
, &mask
);
2395 if (m
== MATCH_ERROR
)
2396 goto concurr_cleanup
;
2398 if (gfc_match_eos () != MATCH_YES
)
2399 goto concurr_cleanup
;
2402 && gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
2403 goto concurr_cleanup
;
2405 new_st
.label1
= label
;
2406 new_st
.op
= EXEC_DO_CONCURRENT
;
2407 new_st
.expr1
= mask
;
2408 new_st
.ext
.forall_iterator
= head
;
2413 gfc_syntax_error (ST_DO
);
2414 gfc_free_expr (mask
);
2415 gfc_free_forall_iterator (head
);
2419 /* See if we have a DO WHILE. */
2420 if (gfc_match (" while ( %e )%t", &iter
.end
) == MATCH_YES
)
2422 new_st
.op
= EXEC_DO_WHILE
;
2426 /* The abortive DO WHILE may have done something to the symbol
2427 table, so we start over. */
2428 gfc_undo_symbols ();
2429 gfc_current_locus
= old_loc
;
2431 gfc_match_label (); /* This won't error. */
2432 gfc_match (" do "); /* This will work. */
2434 gfc_match_st_label (&label
); /* Can't error out. */
2435 gfc_match_char (','); /* Optional comma. */
2437 m
= gfc_match_iterator (&iter
, 0);
2440 if (m
== MATCH_ERROR
)
2443 iter
.var
->symtree
->n
.sym
->attr
.implied_index
= 0;
2444 gfc_check_do_variable (iter
.var
->symtree
);
2446 if (gfc_match_eos () != MATCH_YES
)
2448 gfc_syntax_error (ST_DO
);
2452 new_st
.op
= EXEC_DO
;
2456 && gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
2459 new_st
.label1
= label
;
2461 if (new_st
.op
== EXEC_DO_WHILE
)
2462 new_st
.expr1
= iter
.end
;
2465 new_st
.ext
.iterator
= ip
= gfc_get_iterator ();
2472 gfc_free_iterator (&iter
, 0);
2478 /* Match an EXIT or CYCLE statement. */
2481 match_exit_cycle (gfc_statement st
, gfc_exec_op op
)
2483 gfc_state_data
*p
, *o
;
2488 if (gfc_match_eos () == MATCH_YES
)
2492 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2495 m
= gfc_match ("% %n%t", name
);
2496 if (m
== MATCH_ERROR
)
2500 gfc_syntax_error (st
);
2504 /* Find the corresponding symbol. If there's a BLOCK statement
2505 between here and the label, it is not in gfc_current_ns but a parent
2507 stree
= gfc_find_symtree_in_proc (name
, gfc_current_ns
);
2510 gfc_error ("Name '%s' in %s statement at %C is unknown",
2511 name
, gfc_ascii_statement (st
));
2516 if (sym
->attr
.flavor
!= FL_LABEL
)
2518 gfc_error ("Name '%s' in %s statement at %C is not a construct name",
2519 name
, gfc_ascii_statement (st
));
2524 /* Find the loop specified by the label (or lack of a label). */
2525 for (o
= NULL
, p
= gfc_state_stack
; p
; p
= p
->previous
)
2526 if (o
== NULL
&& p
->state
== COMP_OMP_STRUCTURED_BLOCK
)
2528 else if (p
->state
== COMP_CRITICAL
)
2530 gfc_error("%s statement at %C leaves CRITICAL construct",
2531 gfc_ascii_statement (st
));
2534 else if (p
->state
== COMP_DO_CONCURRENT
2535 && (op
== EXEC_EXIT
|| (sym
&& sym
!= p
->sym
)))
2537 /* F2008, C821 & C845. */
2538 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2539 gfc_ascii_statement (st
));
2542 else if ((sym
&& sym
== p
->sym
)
2543 || (!sym
&& (p
->state
== COMP_DO
2544 || p
->state
== COMP_DO_CONCURRENT
)))
2550 gfc_error ("%s statement at %C is not within a construct",
2551 gfc_ascii_statement (st
));
2553 gfc_error ("%s statement at %C is not within construct '%s'",
2554 gfc_ascii_statement (st
), sym
->name
);
2559 /* Special checks for EXIT from non-loop constructs. */
2563 case COMP_DO_CONCURRENT
:
2567 /* This is already handled above. */
2570 case COMP_ASSOCIATE
:
2574 case COMP_SELECT_TYPE
:
2576 if (op
== EXEC_CYCLE
)
2578 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2579 " construct '%s'", sym
->name
);
2582 gcc_assert (op
== EXEC_EXIT
);
2583 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: EXIT statement with no"
2584 " do-construct-name at %C") == FAILURE
)
2589 gfc_error ("%s statement at %C is not applicable to construct '%s'",
2590 gfc_ascii_statement (st
), sym
->name
);
2596 gfc_error ("%s statement at %C leaving OpenMP structured block",
2597 gfc_ascii_statement (st
));
2601 for (o
= p
, cnt
= 0; o
->state
== COMP_DO
&& o
->previous
!= NULL
; cnt
++)
2605 && o
->state
== COMP_OMP_STRUCTURED_BLOCK
2606 && (o
->head
->op
== EXEC_OMP_DO
2607 || o
->head
->op
== EXEC_OMP_PARALLEL_DO
))
2610 gcc_assert (o
->head
->next
!= NULL
2611 && (o
->head
->next
->op
== EXEC_DO
2612 || o
->head
->next
->op
== EXEC_DO_WHILE
)
2613 && o
->previous
!= NULL
2614 && o
->previous
->tail
->op
== o
->head
->op
);
2615 if (o
->previous
->tail
->ext
.omp_clauses
!= NULL
2616 && o
->previous
->tail
->ext
.omp_clauses
->collapse
> 1)
2617 collapse
= o
->previous
->tail
->ext
.omp_clauses
->collapse
;
2618 if (st
== ST_EXIT
&& cnt
<= collapse
)
2620 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2623 if (st
== ST_CYCLE
&& cnt
< collapse
)
2625 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2631 /* Save the first statement in the construct - needed by the backend. */
2632 new_st
.ext
.which_construct
= p
->construct
;
2640 /* Match the EXIT statement. */
2643 gfc_match_exit (void)
2645 return match_exit_cycle (ST_EXIT
, EXEC_EXIT
);
2649 /* Match the CYCLE statement. */
2652 gfc_match_cycle (void)
2654 return match_exit_cycle (ST_CYCLE
, EXEC_CYCLE
);
2658 /* Match a number or character constant after an (ALL) STOP or PAUSE statement. */
2661 gfc_match_stopcode (gfc_statement st
)
2668 if (gfc_match_eos () != MATCH_YES
)
2670 m
= gfc_match_init_expr (&e
);
2671 if (m
== MATCH_ERROR
)
2676 if (gfc_match_eos () != MATCH_YES
)
2680 if (gfc_pure (NULL
))
2682 gfc_error ("%s statement not allowed in PURE procedure at %C",
2683 gfc_ascii_statement (st
));
2687 if (gfc_implicit_pure (NULL
))
2688 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
2690 if (st
== ST_STOP
&& gfc_find_state (COMP_CRITICAL
) == SUCCESS
)
2692 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2695 if (st
== ST_STOP
&& gfc_find_state (COMP_DO_CONCURRENT
) == SUCCESS
)
2697 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
2703 if (!(e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_INTEGER
))
2705 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2712 gfc_error ("STOP code at %L must be scalar",
2717 if (e
->ts
.type
== BT_CHARACTER
2718 && e
->ts
.kind
!= gfc_default_character_kind
)
2720 gfc_error ("STOP code at %L must be default character KIND=%d",
2721 &e
->where
, (int) gfc_default_character_kind
);
2725 if (e
->ts
.type
== BT_INTEGER
2726 && e
->ts
.kind
!= gfc_default_integer_kind
)
2728 gfc_error ("STOP code at %L must be default integer KIND=%d",
2729 &e
->where
, (int) gfc_default_integer_kind
);
2737 new_st
.op
= EXEC_STOP
;
2740 new_st
.op
= EXEC_ERROR_STOP
;
2743 new_st
.op
= EXEC_PAUSE
;
2750 new_st
.ext
.stop_code
= -1;
2755 gfc_syntax_error (st
);
2764 /* Match the (deprecated) PAUSE statement. */
2767 gfc_match_pause (void)
2771 m
= gfc_match_stopcode (ST_PAUSE
);
2774 if (gfc_notify_std (GFC_STD_F95_DEL
, "Deleted feature: PAUSE statement"
2783 /* Match the STOP statement. */
2786 gfc_match_stop (void)
2788 return gfc_match_stopcode (ST_STOP
);
2792 /* Match the ERROR STOP statement. */
2795 gfc_match_error_stop (void)
2797 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: ERROR STOP statement at %C")
2801 return gfc_match_stopcode (ST_ERROR_STOP
);
2805 /* Match LOCK/UNLOCK statement. Syntax:
2806 LOCK ( lock-variable [ , lock-stat-list ] )
2807 UNLOCK ( lock-variable [ , sync-stat-list ] )
2808 where lock-stat is ACQUIRED_LOCK or sync-stat
2809 and sync-stat is STAT= or ERRMSG=. */
2812 lock_unlock_statement (gfc_statement st
)
2815 gfc_expr
*tmp
, *lockvar
, *acq_lock
, *stat
, *errmsg
;
2816 bool saw_acq_lock
, saw_stat
, saw_errmsg
;
2818 tmp
= lockvar
= acq_lock
= stat
= errmsg
= NULL
;
2819 saw_acq_lock
= saw_stat
= saw_errmsg
= false;
2821 if (gfc_pure (NULL
))
2823 gfc_error ("Image control statement %s at %C in PURE procedure",
2824 st
== ST_LOCK
? "LOCK" : "UNLOCK");
2828 if (gfc_implicit_pure (NULL
))
2829 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
2831 if (gfc_option
.coarray
== GFC_FCOARRAY_NONE
)
2833 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
2837 if (gfc_find_state (COMP_CRITICAL
) == SUCCESS
)
2839 gfc_error ("Image control statement %s at %C in CRITICAL block",
2840 st
== ST_LOCK
? "LOCK" : "UNLOCK");
2844 if (gfc_find_state (COMP_DO_CONCURRENT
) == SUCCESS
)
2846 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
2847 st
== ST_LOCK
? "LOCK" : "UNLOCK");
2851 if (gfc_match_char ('(') != MATCH_YES
)
2854 if (gfc_match ("%e", &lockvar
) != MATCH_YES
)
2856 m
= gfc_match_char (',');
2857 if (m
== MATCH_ERROR
)
2861 m
= gfc_match_char (')');
2869 m
= gfc_match (" stat = %v", &tmp
);
2870 if (m
== MATCH_ERROR
)
2876 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
2882 m
= gfc_match_char (',');
2890 m
= gfc_match (" errmsg = %v", &tmp
);
2891 if (m
== MATCH_ERROR
)
2897 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
2903 m
= gfc_match_char (',');
2911 m
= gfc_match (" acquired_lock = %v", &tmp
);
2912 if (m
== MATCH_ERROR
|| st
== ST_UNLOCK
)
2918 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L ",
2923 saw_acq_lock
= true;
2925 m
= gfc_match_char (',');
2936 if (m
== MATCH_ERROR
)
2939 if (gfc_match (" )%t") != MATCH_YES
)
2946 new_st
.op
= EXEC_LOCK
;
2949 new_st
.op
= EXEC_UNLOCK
;
2955 new_st
.expr1
= lockvar
;
2956 new_st
.expr2
= stat
;
2957 new_st
.expr3
= errmsg
;
2958 new_st
.expr4
= acq_lock
;
2963 gfc_syntax_error (st
);
2966 gfc_free_expr (tmp
);
2967 gfc_free_expr (lockvar
);
2968 gfc_free_expr (acq_lock
);
2969 gfc_free_expr (stat
);
2970 gfc_free_expr (errmsg
);
2977 gfc_match_lock (void)
2979 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: LOCK statement at %C")
2983 return lock_unlock_statement (ST_LOCK
);
2988 gfc_match_unlock (void)
2990 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: UNLOCK statement at %C")
2994 return lock_unlock_statement (ST_UNLOCK
);
2998 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
2999 SYNC ALL [(sync-stat-list)]
3000 SYNC MEMORY [(sync-stat-list)]
3001 SYNC IMAGES (image-set [, sync-stat-list] )
3002 with sync-stat is int-expr or *. */
3005 sync_statement (gfc_statement st
)
3008 gfc_expr
*tmp
, *imageset
, *stat
, *errmsg
;
3009 bool saw_stat
, saw_errmsg
;
3011 tmp
= imageset
= stat
= errmsg
= NULL
;
3012 saw_stat
= saw_errmsg
= false;
3014 if (gfc_pure (NULL
))
3016 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3020 if (gfc_implicit_pure (NULL
))
3021 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
3023 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: SYNC statement at %C")
3027 if (gfc_option
.coarray
== GFC_FCOARRAY_NONE
)
3029 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
3033 if (gfc_find_state (COMP_CRITICAL
) == SUCCESS
)
3035 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3039 if (gfc_find_state (COMP_DO_CONCURRENT
) == SUCCESS
)
3041 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3045 if (gfc_match_eos () == MATCH_YES
)
3047 if (st
== ST_SYNC_IMAGES
)
3052 if (gfc_match_char ('(') != MATCH_YES
)
3055 if (st
== ST_SYNC_IMAGES
)
3057 /* Denote '*' as imageset == NULL. */
3058 m
= gfc_match_char ('*');
3059 if (m
== MATCH_ERROR
)
3063 if (gfc_match ("%e", &imageset
) != MATCH_YES
)
3066 m
= gfc_match_char (',');
3067 if (m
== MATCH_ERROR
)
3071 m
= gfc_match_char (')');
3080 m
= gfc_match (" stat = %v", &tmp
);
3081 if (m
== MATCH_ERROR
)
3087 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
3093 if (gfc_match_char (',') == MATCH_YES
)
3100 m
= gfc_match (" errmsg = %v", &tmp
);
3101 if (m
== MATCH_ERROR
)
3107 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
3113 if (gfc_match_char (',') == MATCH_YES
)
3123 if (m
== MATCH_ERROR
)
3126 if (gfc_match (" )%t") != MATCH_YES
)
3133 new_st
.op
= EXEC_SYNC_ALL
;
3135 case ST_SYNC_IMAGES
:
3136 new_st
.op
= EXEC_SYNC_IMAGES
;
3138 case ST_SYNC_MEMORY
:
3139 new_st
.op
= EXEC_SYNC_MEMORY
;
3145 new_st
.expr1
= imageset
;
3146 new_st
.expr2
= stat
;
3147 new_st
.expr3
= errmsg
;
3152 gfc_syntax_error (st
);
3155 gfc_free_expr (tmp
);
3156 gfc_free_expr (imageset
);
3157 gfc_free_expr (stat
);
3158 gfc_free_expr (errmsg
);
3164 /* Match SYNC ALL statement. */
3167 gfc_match_sync_all (void)
3169 return sync_statement (ST_SYNC_ALL
);
3173 /* Match SYNC IMAGES statement. */
3176 gfc_match_sync_images (void)
3178 return sync_statement (ST_SYNC_IMAGES
);
3182 /* Match SYNC MEMORY statement. */
3185 gfc_match_sync_memory (void)
3187 return sync_statement (ST_SYNC_MEMORY
);
3191 /* Match a CONTINUE statement. */
3194 gfc_match_continue (void)
3196 if (gfc_match_eos () != MATCH_YES
)
3198 gfc_syntax_error (ST_CONTINUE
);
3202 new_st
.op
= EXEC_CONTINUE
;
3207 /* Match the (deprecated) ASSIGN statement. */
3210 gfc_match_assign (void)
3213 gfc_st_label
*label
;
3215 if (gfc_match (" %l", &label
) == MATCH_YES
)
3217 if (gfc_reference_st_label (label
, ST_LABEL_UNKNOWN
) == FAILURE
)
3219 if (gfc_match (" to %v%t", &expr
) == MATCH_YES
)
3221 if (gfc_notify_std (GFC_STD_F95_DEL
, "Deleted feature: ASSIGN "
3226 expr
->symtree
->n
.sym
->attr
.assign
= 1;
3228 new_st
.op
= EXEC_LABEL_ASSIGN
;
3229 new_st
.label1
= label
;
3230 new_st
.expr1
= expr
;
3238 /* Match the GO TO statement. As a computed GOTO statement is
3239 matched, it is transformed into an equivalent SELECT block. No
3240 tree is necessary, and the resulting jumps-to-jumps are
3241 specifically optimized away by the back end. */
3244 gfc_match_goto (void)
3246 gfc_code
*head
, *tail
;
3249 gfc_st_label
*label
;
3253 if (gfc_match (" %l%t", &label
) == MATCH_YES
)
3255 if (gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
3258 new_st
.op
= EXEC_GOTO
;
3259 new_st
.label1
= label
;
3263 /* The assigned GO TO statement. */
3265 if (gfc_match_variable (&expr
, 0) == MATCH_YES
)
3267 if (gfc_notify_std (GFC_STD_F95_DEL
, "Deleted feature: Assigned GOTO "
3272 new_st
.op
= EXEC_GOTO
;
3273 new_st
.expr1
= expr
;
3275 if (gfc_match_eos () == MATCH_YES
)
3278 /* Match label list. */
3279 gfc_match_char (',');
3280 if (gfc_match_char ('(') != MATCH_YES
)
3282 gfc_syntax_error (ST_GOTO
);
3289 m
= gfc_match_st_label (&label
);
3293 if (gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
3297 head
= tail
= gfc_get_code ();
3300 tail
->block
= gfc_get_code ();
3304 tail
->label1
= label
;
3305 tail
->op
= EXEC_GOTO
;
3307 while (gfc_match_char (',') == MATCH_YES
);
3309 if (gfc_match (")%t") != MATCH_YES
)
3314 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3317 new_st
.block
= head
;
3322 /* Last chance is a computed GO TO statement. */
3323 if (gfc_match_char ('(') != MATCH_YES
)
3325 gfc_syntax_error (ST_GOTO
);
3334 m
= gfc_match_st_label (&label
);
3338 if (gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
3342 head
= tail
= gfc_get_code ();
3345 tail
->block
= gfc_get_code ();
3349 cp
= gfc_get_case ();
3350 cp
->low
= cp
->high
= gfc_get_int_expr (gfc_default_integer_kind
,
3353 tail
->op
= EXEC_SELECT
;
3354 tail
->ext
.block
.case_list
= cp
;
3356 tail
->next
= gfc_get_code ();
3357 tail
->next
->op
= EXEC_GOTO
;
3358 tail
->next
->label1
= label
;
3360 while (gfc_match_char (',') == MATCH_YES
);
3362 if (gfc_match_char (')') != MATCH_YES
)
3367 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3371 /* Get the rest of the statement. */
3372 gfc_match_char (',');
3374 if (gfc_match (" %e%t", &expr
) != MATCH_YES
)
3377 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: Computed GOTO "
3378 "at %C") == FAILURE
)
3381 /* At this point, a computed GOTO has been fully matched and an
3382 equivalent SELECT statement constructed. */
3384 new_st
.op
= EXEC_SELECT
;
3385 new_st
.expr1
= NULL
;
3387 /* Hack: For a "real" SELECT, the expression is in expr. We put
3388 it in expr2 so we can distinguish then and produce the correct
3390 new_st
.expr2
= expr
;
3391 new_st
.block
= head
;
3395 gfc_syntax_error (ST_GOTO
);
3397 gfc_free_statements (head
);
3402 /* Frees a list of gfc_alloc structures. */
3405 gfc_free_alloc_list (gfc_alloc
*p
)
3412 gfc_free_expr (p
->expr
);
3418 /* Match an ALLOCATE statement. */
3421 gfc_match_allocate (void)
3423 gfc_alloc
*head
, *tail
;
3424 gfc_expr
*stat
, *errmsg
, *tmp
, *source
, *mold
;
3428 locus old_locus
, deferred_locus
;
3429 bool saw_stat
, saw_errmsg
, saw_source
, saw_mold
, saw_deferred
, b1
, b2
, b3
;
3432 stat
= errmsg
= source
= mold
= tmp
= NULL
;
3433 saw_stat
= saw_errmsg
= saw_source
= saw_mold
= saw_deferred
= false;
3435 if (gfc_match_char ('(') != MATCH_YES
)
3438 /* Match an optional type-spec. */
3439 old_locus
= gfc_current_locus
;
3440 m
= match_type_spec (&ts
);
3441 if (m
== MATCH_ERROR
)
3443 else if (m
== MATCH_NO
)
3445 char name
[GFC_MAX_SYMBOL_LEN
+ 3];
3447 if (gfc_match ("%n :: ", name
) == MATCH_YES
)
3449 gfc_error ("Error in type-spec at %L", &old_locus
);
3453 ts
.type
= BT_UNKNOWN
;
3457 if (gfc_match (" :: ") == MATCH_YES
)
3459 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: typespec in "
3460 "ALLOCATE at %L", &old_locus
) == FAILURE
)
3465 gfc_error ("Type-spec at %L cannot contain a deferred "
3466 "type parameter", &old_locus
);
3472 ts
.type
= BT_UNKNOWN
;
3473 gfc_current_locus
= old_locus
;
3480 head
= tail
= gfc_get_alloc ();
3483 tail
->next
= gfc_get_alloc ();
3487 m
= gfc_match_variable (&tail
->expr
, 0);
3490 if (m
== MATCH_ERROR
)
3493 if (gfc_check_do_variable (tail
->expr
->symtree
))
3496 if (gfc_pure (NULL
) && gfc_impure_variable (tail
->expr
->symtree
->n
.sym
))
3498 gfc_error ("Bad allocate-object at %C for a PURE procedure");
3502 if (gfc_implicit_pure (NULL
)
3503 && gfc_impure_variable (tail
->expr
->symtree
->n
.sym
))
3504 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
3506 if (tail
->expr
->ts
.deferred
)
3508 saw_deferred
= true;
3509 deferred_locus
= tail
->expr
->where
;
3512 if (gfc_find_state (COMP_DO_CONCURRENT
) == SUCCESS
3513 || gfc_find_state (COMP_CRITICAL
) == SUCCESS
)
3516 bool coarray
= tail
->expr
->symtree
->n
.sym
->attr
.codimension
;
3517 for (ref
= tail
->expr
->ref
; ref
; ref
= ref
->next
)
3518 if (ref
->type
== REF_COMPONENT
)
3519 coarray
= ref
->u
.c
.component
->attr
.codimension
;
3521 if (coarray
&& gfc_find_state (COMP_DO_CONCURRENT
) == SUCCESS
)
3523 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
3526 if (coarray
&& gfc_find_state (COMP_CRITICAL
) == SUCCESS
)
3528 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
3533 /* The ALLOCATE statement had an optional typespec. Check the
3535 if (ts
.type
!= BT_UNKNOWN
)
3537 /* Enforce F03:C624. */
3538 if (!gfc_type_compatible (&tail
->expr
->ts
, &ts
))
3540 gfc_error ("Type of entity at %L is type incompatible with "
3541 "typespec", &tail
->expr
->where
);
3545 /* Enforce F03:C627. */
3546 if (ts
.kind
!= tail
->expr
->ts
.kind
)
3548 gfc_error ("Kind type parameter for entity at %L differs from "
3549 "the kind type parameter of the typespec",
3550 &tail
->expr
->where
);
3555 if (tail
->expr
->ts
.type
== BT_DERIVED
)
3556 tail
->expr
->ts
.u
.derived
= gfc_use_derived (tail
->expr
->ts
.u
.derived
);
3558 /* FIXME: disable the checking on derived types and arrays. */
3559 sym
= tail
->expr
->symtree
->n
.sym
;
3560 b1
= !(tail
->expr
->ref
3561 && (tail
->expr
->ref
->type
== REF_COMPONENT
3562 || tail
->expr
->ref
->type
== REF_ARRAY
));
3563 if (sym
&& sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
3564 b2
= !(CLASS_DATA (sym
)->attr
.allocatable
3565 || CLASS_DATA (sym
)->attr
.class_pointer
);
3567 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
3568 || sym
->attr
.proc_pointer
);
3569 b3
= sym
&& sym
->ns
&& sym
->ns
->proc_name
3570 && (sym
->ns
->proc_name
->attr
.allocatable
3571 || sym
->ns
->proc_name
->attr
.pointer
3572 || sym
->ns
->proc_name
->attr
.proc_pointer
);
3573 if (b1
&& b2
&& !b3
)
3575 gfc_error ("Allocate-object at %L is not a nonprocedure pointer "
3576 "or an allocatable variable", &tail
->expr
->where
);
3580 if (gfc_peek_ascii_char () == '(' && !sym
->attr
.dimension
)
3582 gfc_error ("Shape specification for allocatable scalar at %C");
3586 if (gfc_match_char (',') != MATCH_YES
)
3591 m
= gfc_match (" stat = %v", &tmp
);
3592 if (m
== MATCH_ERROR
)
3599 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
3607 if (gfc_check_do_variable (stat
->symtree
))
3610 if (gfc_match_char (',') == MATCH_YES
)
3611 goto alloc_opt_list
;
3614 m
= gfc_match (" errmsg = %v", &tmp
);
3615 if (m
== MATCH_ERROR
)
3619 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ERRMSG tag at %L",
3620 &tmp
->where
) == FAILURE
)
3626 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
3634 if (gfc_match_char (',') == MATCH_YES
)
3635 goto alloc_opt_list
;
3638 m
= gfc_match (" source = %e", &tmp
);
3639 if (m
== MATCH_ERROR
)
3643 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: SOURCE tag at %L",
3644 &tmp
->where
) == FAILURE
)
3650 gfc_error ("Redundant SOURCE tag found at %L ", &tmp
->where
);
3654 /* The next 2 conditionals check C631. */
3655 if (ts
.type
!= BT_UNKNOWN
)
3657 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
3658 &tmp
->where
, &old_locus
);
3663 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: SOURCE tag at %L"
3664 " with more than a single allocate object",
3665 &tmp
->where
) == FAILURE
)
3672 if (gfc_match_char (',') == MATCH_YES
)
3673 goto alloc_opt_list
;
3676 m
= gfc_match (" mold = %e", &tmp
);
3677 if (m
== MATCH_ERROR
)
3681 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: MOLD tag at %L",
3682 &tmp
->where
) == FAILURE
)
3685 /* Check F08:C636. */
3688 gfc_error ("Redundant MOLD tag found at %L ", &tmp
->where
);
3692 /* Check F08:C637. */
3693 if (ts
.type
!= BT_UNKNOWN
)
3695 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
3696 &tmp
->where
, &old_locus
);
3705 if (gfc_match_char (',') == MATCH_YES
)
3706 goto alloc_opt_list
;
3709 gfc_gobble_whitespace ();
3711 if (gfc_peek_char () == ')')
3715 if (gfc_match (" )%t") != MATCH_YES
)
3718 /* Check F08:C637. */
3721 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
3722 &mold
->where
, &source
->where
);
3726 /* Check F03:C623, */
3727 if (saw_deferred
&& ts
.type
== BT_UNKNOWN
&& !source
&& !mold
)
3729 gfc_error ("Allocate-object at %L with a deferred type parameter "
3730 "requires either a type-spec or SOURCE tag or a MOLD tag",
3735 new_st
.op
= EXEC_ALLOCATE
;
3736 new_st
.expr1
= stat
;
3737 new_st
.expr2
= errmsg
;
3739 new_st
.expr3
= source
;
3741 new_st
.expr3
= mold
;
3742 new_st
.ext
.alloc
.list
= head
;
3743 new_st
.ext
.alloc
.ts
= ts
;
3748 gfc_syntax_error (ST_ALLOCATE
);
3751 gfc_free_expr (errmsg
);
3752 gfc_free_expr (source
);
3753 gfc_free_expr (stat
);
3754 gfc_free_expr (mold
);
3755 if (tmp
&& tmp
->expr_type
) gfc_free_expr (tmp
);
3756 gfc_free_alloc_list (head
);
3761 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
3762 a set of pointer assignments to intrinsic NULL(). */
3765 gfc_match_nullify (void)
3773 if (gfc_match_char ('(') != MATCH_YES
)
3778 m
= gfc_match_variable (&p
, 0);
3779 if (m
== MATCH_ERROR
)
3784 if (gfc_check_do_variable (p
->symtree
))
3788 if (gfc_is_coindexed (p
))
3790 gfc_error ("Pointer object at %C shall not be coindexed");
3794 /* build ' => NULL() '. */
3795 e
= gfc_get_null_expr (&gfc_current_locus
);
3797 /* Chain to list. */
3802 tail
->next
= gfc_get_code ();
3806 tail
->op
= EXEC_POINTER_ASSIGN
;
3810 if (gfc_match (" )%t") == MATCH_YES
)
3812 if (gfc_match_char (',') != MATCH_YES
)
3819 gfc_syntax_error (ST_NULLIFY
);
3822 gfc_free_statements (new_st
.next
);
3824 gfc_free_expr (new_st
.expr1
);
3825 new_st
.expr1
= NULL
;
3826 gfc_free_expr (new_st
.expr2
);
3827 new_st
.expr2
= NULL
;
3832 /* Match a DEALLOCATE statement. */
3835 gfc_match_deallocate (void)
3837 gfc_alloc
*head
, *tail
;
3838 gfc_expr
*stat
, *errmsg
, *tmp
;
3841 bool saw_stat
, saw_errmsg
, b1
, b2
;
3844 stat
= errmsg
= tmp
= NULL
;
3845 saw_stat
= saw_errmsg
= false;
3847 if (gfc_match_char ('(') != MATCH_YES
)
3853 head
= tail
= gfc_get_alloc ();
3856 tail
->next
= gfc_get_alloc ();
3860 m
= gfc_match_variable (&tail
->expr
, 0);
3861 if (m
== MATCH_ERROR
)
3866 if (gfc_check_do_variable (tail
->expr
->symtree
))
3869 sym
= tail
->expr
->symtree
->n
.sym
;
3871 if (gfc_pure (NULL
) && gfc_impure_variable (sym
))
3873 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
3877 if (gfc_implicit_pure (NULL
) && gfc_impure_variable (sym
))
3878 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
3880 if (gfc_is_coarray (tail
->expr
)
3881 && gfc_find_state (COMP_DO_CONCURRENT
) == SUCCESS
)
3883 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
3887 if (gfc_is_coarray (tail
->expr
)
3888 && gfc_find_state (COMP_CRITICAL
) == SUCCESS
)
3890 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
3894 /* FIXME: disable the checking on derived types. */
3895 b1
= !(tail
->expr
->ref
3896 && (tail
->expr
->ref
->type
== REF_COMPONENT
3897 || tail
->expr
->ref
->type
== REF_ARRAY
));
3898 if (sym
&& sym
->ts
.type
== BT_CLASS
)
3899 b2
= !(CLASS_DATA (sym
)->attr
.allocatable
3900 || CLASS_DATA (sym
)->attr
.class_pointer
);
3902 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
3903 || sym
->attr
.proc_pointer
);
3906 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
3907 "or an allocatable variable");
3911 if (gfc_match_char (',') != MATCH_YES
)
3916 m
= gfc_match (" stat = %v", &tmp
);
3917 if (m
== MATCH_ERROR
)
3923 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
3924 gfc_free_expr (tmp
);
3931 if (gfc_check_do_variable (stat
->symtree
))
3934 if (gfc_match_char (',') == MATCH_YES
)
3935 goto dealloc_opt_list
;
3938 m
= gfc_match (" errmsg = %v", &tmp
);
3939 if (m
== MATCH_ERROR
)
3943 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ERRMSG at %L",
3944 &tmp
->where
) == FAILURE
)
3949 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
3950 gfc_free_expr (tmp
);
3957 if (gfc_match_char (',') == MATCH_YES
)
3958 goto dealloc_opt_list
;
3961 gfc_gobble_whitespace ();
3963 if (gfc_peek_char () == ')')
3967 if (gfc_match (" )%t") != MATCH_YES
)
3970 new_st
.op
= EXEC_DEALLOCATE
;
3971 new_st
.expr1
= stat
;
3972 new_st
.expr2
= errmsg
;
3973 new_st
.ext
.alloc
.list
= head
;
3978 gfc_syntax_error (ST_DEALLOCATE
);
3981 gfc_free_expr (errmsg
);
3982 gfc_free_expr (stat
);
3983 gfc_free_alloc_list (head
);
3988 /* Match a RETURN statement. */
3991 gfc_match_return (void)
3995 gfc_compile_state s
;
3999 if (gfc_find_state (COMP_CRITICAL
) == SUCCESS
)
4001 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4005 if (gfc_find_state (COMP_DO_CONCURRENT
) == SUCCESS
)
4007 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4011 if (gfc_match_eos () == MATCH_YES
)
4014 if (gfc_find_state (COMP_SUBROUTINE
) == FAILURE
)
4016 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4021 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: Alternate RETURN "
4022 "at %C") == FAILURE
)
4025 if (gfc_current_form
== FORM_FREE
)
4027 /* The following are valid, so we can't require a blank after the
4031 char c
= gfc_peek_ascii_char ();
4032 if (ISALPHA (c
) || ISDIGIT (c
))
4036 m
= gfc_match (" %e%t", &e
);
4039 if (m
== MATCH_ERROR
)
4042 gfc_syntax_error (ST_RETURN
);
4049 gfc_enclosing_unit (&s
);
4050 if (s
== COMP_PROGRAM
4051 && gfc_notify_std (GFC_STD_GNU
, "Extension: RETURN statement in "
4052 "main program at %C") == FAILURE
)
4055 new_st
.op
= EXEC_RETURN
;
4062 /* Match the call of a type-bound procedure, if CALL%var has already been
4063 matched and var found to be a derived-type variable. */
4066 match_typebound_call (gfc_symtree
* varst
)
4071 base
= gfc_get_expr ();
4072 base
->expr_type
= EXPR_VARIABLE
;
4073 base
->symtree
= varst
;
4074 base
->where
= gfc_current_locus
;
4075 gfc_set_sym_referenced (varst
->n
.sym
);
4077 m
= gfc_match_varspec (base
, 0, true, true);
4079 gfc_error ("Expected component reference at %C");
4083 if (gfc_match_eos () != MATCH_YES
)
4085 gfc_error ("Junk after CALL at %C");
4089 if (base
->expr_type
== EXPR_COMPCALL
)
4090 new_st
.op
= EXEC_COMPCALL
;
4091 else if (base
->expr_type
== EXPR_PPC
)
4092 new_st
.op
= EXEC_CALL_PPC
;
4095 gfc_error ("Expected type-bound procedure or procedure pointer component "
4099 new_st
.expr1
= base
;
4105 /* Match a CALL statement. The tricky part here are possible
4106 alternate return specifiers. We handle these by having all
4107 "subroutines" actually return an integer via a register that gives
4108 the return number. If the call specifies alternate returns, we
4109 generate code for a SELECT statement whose case clauses contain
4110 GOTOs to the various labels. */
4113 gfc_match_call (void)
4115 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4116 gfc_actual_arglist
*a
, *arglist
;
4126 m
= gfc_match ("% %n", name
);
4132 if (gfc_get_ha_sym_tree (name
, &st
))
4137 /* If this is a variable of derived-type, it probably starts a type-bound
4139 if ((sym
->attr
.flavor
!= FL_PROCEDURE
4140 || gfc_is_function_return_value (sym
, gfc_current_ns
))
4141 && (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
))
4142 return match_typebound_call (st
);
4144 /* If it does not seem to be callable (include functions so that the
4145 right association is made. They are thrown out in resolution.)
4147 if (!sym
->attr
.generic
4148 && !sym
->attr
.subroutine
4149 && !sym
->attr
.function
)
4151 if (!(sym
->attr
.external
&& !sym
->attr
.referenced
))
4153 /* ...create a symbol in this scope... */
4154 if (sym
->ns
!= gfc_current_ns
4155 && gfc_get_sym_tree (name
, NULL
, &st
, false) == 1)
4158 if (sym
!= st
->n
.sym
)
4162 /* ...and then to try to make the symbol into a subroutine. */
4163 if (gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4167 gfc_set_sym_referenced (sym
);
4169 if (gfc_match_eos () != MATCH_YES
)
4171 m
= gfc_match_actual_arglist (1, &arglist
);
4174 if (m
== MATCH_ERROR
)
4177 if (gfc_match_eos () != MATCH_YES
)
4181 /* If any alternate return labels were found, construct a SELECT
4182 statement that will jump to the right place. */
4185 for (a
= arglist
; a
; a
= a
->next
)
4186 if (a
->expr
== NULL
)
4191 gfc_symtree
*select_st
;
4192 gfc_symbol
*select_sym
;
4193 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4195 new_st
.next
= c
= gfc_get_code ();
4196 c
->op
= EXEC_SELECT
;
4197 sprintf (name
, "_result_%s", sym
->name
);
4198 gfc_get_ha_sym_tree (name
, &select_st
); /* Can't fail. */
4200 select_sym
= select_st
->n
.sym
;
4201 select_sym
->ts
.type
= BT_INTEGER
;
4202 select_sym
->ts
.kind
= gfc_default_integer_kind
;
4203 gfc_set_sym_referenced (select_sym
);
4204 c
->expr1
= gfc_get_expr ();
4205 c
->expr1
->expr_type
= EXPR_VARIABLE
;
4206 c
->expr1
->symtree
= select_st
;
4207 c
->expr1
->ts
= select_sym
->ts
;
4208 c
->expr1
->where
= gfc_current_locus
;
4211 for (a
= arglist
; a
; a
= a
->next
)
4213 if (a
->expr
!= NULL
)
4216 if (gfc_reference_st_label (a
->label
, ST_LABEL_TARGET
) == FAILURE
)
4221 c
->block
= gfc_get_code ();
4223 c
->op
= EXEC_SELECT
;
4225 new_case
= gfc_get_case ();
4226 new_case
->high
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, i
);
4227 new_case
->low
= new_case
->high
;
4228 c
->ext
.block
.case_list
= new_case
;
4230 c
->next
= gfc_get_code ();
4231 c
->next
->op
= EXEC_GOTO
;
4232 c
->next
->label1
= a
->label
;
4236 new_st
.op
= EXEC_CALL
;
4237 new_st
.symtree
= st
;
4238 new_st
.ext
.actual
= arglist
;
4243 gfc_syntax_error (ST_CALL
);
4246 gfc_free_actual_arglist (arglist
);
4251 /* Given a name, return a pointer to the common head structure,
4252 creating it if it does not exist. If FROM_MODULE is nonzero, we
4253 mangle the name so that it doesn't interfere with commons defined
4254 in the using namespace.
4255 TODO: Add to global symbol tree. */
4258 gfc_get_common (const char *name
, int from_module
)
4261 static int serial
= 0;
4262 char mangled_name
[GFC_MAX_SYMBOL_LEN
+ 1];
4266 /* A use associated common block is only needed to correctly layout
4267 the variables it contains. */
4268 snprintf (mangled_name
, GFC_MAX_SYMBOL_LEN
, "_%d_%s", serial
++, name
);
4269 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, mangled_name
);
4273 st
= gfc_find_symtree (gfc_current_ns
->common_root
, name
);
4276 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, name
);
4279 if (st
->n
.common
== NULL
)
4281 st
->n
.common
= gfc_get_common_head ();
4282 st
->n
.common
->where
= gfc_current_locus
;
4283 strcpy (st
->n
.common
->name
, name
);
4286 return st
->n
.common
;
4290 /* Match a common block name. */
4292 match
match_common_name (char *name
)
4296 if (gfc_match_char ('/') == MATCH_NO
)
4302 if (gfc_match_char ('/') == MATCH_YES
)
4308 m
= gfc_match_name (name
);
4310 if (m
== MATCH_ERROR
)
4312 if (m
== MATCH_YES
&& gfc_match_char ('/') == MATCH_YES
)
4315 gfc_error ("Syntax error in common block name at %C");
4320 /* Match a COMMON statement. */
4323 gfc_match_common (void)
4325 gfc_symbol
*sym
, **head
, *tail
, *other
, *old_blank_common
;
4326 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4333 old_blank_common
= gfc_current_ns
->blank_common
.head
;
4334 if (old_blank_common
)
4336 while (old_blank_common
->common_next
)
4337 old_blank_common
= old_blank_common
->common_next
;
4344 m
= match_common_name (name
);
4345 if (m
== MATCH_ERROR
)
4348 gsym
= gfc_get_gsymbol (name
);
4349 if (gsym
->type
!= GSYM_UNKNOWN
&& gsym
->type
!= GSYM_COMMON
)
4351 gfc_error ("Symbol '%s' at %C is already an external symbol that "
4352 "is not COMMON", name
);
4356 if (gsym
->type
== GSYM_UNKNOWN
)
4358 gsym
->type
= GSYM_COMMON
;
4359 gsym
->where
= gfc_current_locus
;
4365 if (name
[0] == '\0')
4367 t
= &gfc_current_ns
->blank_common
;
4368 if (t
->head
== NULL
)
4369 t
->where
= gfc_current_locus
;
4373 t
= gfc_get_common (name
, 0);
4382 while (tail
->common_next
)
4383 tail
= tail
->common_next
;
4386 /* Grab the list of symbols. */
4389 m
= gfc_match_symbol (&sym
, 0);
4390 if (m
== MATCH_ERROR
)
4395 /* Store a ref to the common block for error checking. */
4396 sym
->common_block
= t
;
4398 /* See if we know the current common block is bind(c), and if
4399 so, then see if we can check if the symbol is (which it'll
4400 need to be). This can happen if the bind(c) attr stmt was
4401 applied to the common block, and the variable(s) already
4402 defined, before declaring the common block. */
4403 if (t
->is_bind_c
== 1)
4405 if (sym
->ts
.type
!= BT_UNKNOWN
&& sym
->ts
.is_c_interop
!= 1)
4407 /* If we find an error, just print it and continue,
4408 cause it's just semantic, and we can see if there
4410 gfc_error_now ("Variable '%s' at %L in common block '%s' "
4411 "at %C must be declared with a C "
4412 "interoperable kind since common block "
4414 sym
->name
, &(sym
->declared_at
), t
->name
,
4418 if (sym
->attr
.is_bind_c
== 1)
4419 gfc_error_now ("Variable '%s' in common block "
4420 "'%s' at %C can not be bind(c) since "
4421 "it is not global", sym
->name
, t
->name
);
4424 if (sym
->attr
.in_common
)
4426 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
4431 if (((sym
->value
!= NULL
&& sym
->value
->expr_type
!= EXPR_NULL
)
4432 || sym
->attr
.data
) && gfc_current_state () != COMP_BLOCK_DATA
)
4434 if (gfc_notify_std (GFC_STD_GNU
, "Initialized symbol '%s' at %C "
4435 "can only be COMMON in "
4436 "BLOCK DATA", sym
->name
)
4441 if (gfc_add_in_common (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4445 tail
->common_next
= sym
;
4451 /* Deal with an optional array specification after the
4453 m
= gfc_match_array_spec (&as
, true, true);
4454 if (m
== MATCH_ERROR
)
4459 if (as
->type
!= AS_EXPLICIT
)
4461 gfc_error ("Array specification for symbol '%s' in COMMON "
4462 "at %C must be explicit", sym
->name
);
4466 if (gfc_add_dimension (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4469 if (sym
->attr
.pointer
)
4471 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
4472 "POINTER array", sym
->name
);
4481 sym
->common_head
= t
;
4483 /* Check to see if the symbol is already in an equivalence group.
4484 If it is, set the other members as being in common. */
4485 if (sym
->attr
.in_equivalence
)
4487 for (e1
= gfc_current_ns
->equiv
; e1
; e1
= e1
->next
)
4489 for (e2
= e1
; e2
; e2
= e2
->eq
)
4490 if (e2
->expr
->symtree
->n
.sym
== sym
)
4497 for (e2
= e1
; e2
; e2
= e2
->eq
)
4499 other
= e2
->expr
->symtree
->n
.sym
;
4500 if (other
->common_head
4501 && other
->common_head
!= sym
->common_head
)
4503 gfc_error ("Symbol '%s', in COMMON block '%s' at "
4504 "%C is being indirectly equivalenced to "
4505 "another COMMON block '%s'",
4506 sym
->name
, sym
->common_head
->name
,
4507 other
->common_head
->name
);
4510 other
->attr
.in_common
= 1;
4511 other
->common_head
= t
;
4517 gfc_gobble_whitespace ();
4518 if (gfc_match_eos () == MATCH_YES
)
4520 if (gfc_peek_ascii_char () == '/')
4522 if (gfc_match_char (',') != MATCH_YES
)
4524 gfc_gobble_whitespace ();
4525 if (gfc_peek_ascii_char () == '/')
4534 gfc_syntax_error (ST_COMMON
);
4537 if (old_blank_common
)
4538 old_blank_common
->common_next
= NULL
;
4540 gfc_current_ns
->blank_common
.head
= NULL
;
4541 gfc_free_array_spec (as
);
4546 /* Match a BLOCK DATA program unit. */
4549 gfc_match_block_data (void)
4551 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4555 if (gfc_match_eos () == MATCH_YES
)
4557 gfc_new_block
= NULL
;
4561 m
= gfc_match ("% %n%t", name
);
4565 if (gfc_get_symbol (name
, NULL
, &sym
))
4568 if (gfc_add_flavor (&sym
->attr
, FL_BLOCK_DATA
, sym
->name
, NULL
) == FAILURE
)
4571 gfc_new_block
= sym
;
4577 /* Free a namelist structure. */
4580 gfc_free_namelist (gfc_namelist
*name
)
4584 for (; name
; name
= n
)
4592 /* Match a NAMELIST statement. */
4595 gfc_match_namelist (void)
4597 gfc_symbol
*group_name
, *sym
;
4601 m
= gfc_match (" / %s /", &group_name
);
4604 if (m
== MATCH_ERROR
)
4609 if (group_name
->ts
.type
!= BT_UNKNOWN
)
4611 gfc_error ("Namelist group name '%s' at %C already has a basic "
4612 "type of %s", group_name
->name
,
4613 gfc_typename (&group_name
->ts
));
4617 if (group_name
->attr
.flavor
== FL_NAMELIST
4618 && group_name
->attr
.use_assoc
4619 && gfc_notify_std (GFC_STD_GNU
, "Namelist group name '%s' "
4620 "at %C already is USE associated and can"
4621 "not be respecified.", group_name
->name
)
4625 if (group_name
->attr
.flavor
!= FL_NAMELIST
4626 && gfc_add_flavor (&group_name
->attr
, FL_NAMELIST
,
4627 group_name
->name
, NULL
) == FAILURE
)
4632 m
= gfc_match_symbol (&sym
, 1);
4635 if (m
== MATCH_ERROR
)
4638 if (sym
->attr
.in_namelist
== 0
4639 && gfc_add_in_namelist (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4642 /* Use gfc_error_check here, rather than goto error, so that
4643 these are the only errors for the next two lines. */
4644 if (sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
)
4646 gfc_error ("Assumed size array '%s' in namelist '%s' at "
4647 "%C is not allowed", sym
->name
, group_name
->name
);
4651 nl
= gfc_get_namelist ();
4655 if (group_name
->namelist
== NULL
)
4656 group_name
->namelist
= group_name
->namelist_tail
= nl
;
4659 group_name
->namelist_tail
->next
= nl
;
4660 group_name
->namelist_tail
= nl
;
4663 if (gfc_match_eos () == MATCH_YES
)
4666 m
= gfc_match_char (',');
4668 if (gfc_match_char ('/') == MATCH_YES
)
4670 m2
= gfc_match (" %s /", &group_name
);
4671 if (m2
== MATCH_YES
)
4673 if (m2
== MATCH_ERROR
)
4687 gfc_syntax_error (ST_NAMELIST
);
4694 /* Match a MODULE statement. */
4697 gfc_match_module (void)
4701 m
= gfc_match (" %s%t", &gfc_new_block
);
4705 if (gfc_add_flavor (&gfc_new_block
->attr
, FL_MODULE
,
4706 gfc_new_block
->name
, NULL
) == FAILURE
)
4713 /* Free equivalence sets and lists. Recursively is the easiest way to
4717 gfc_free_equiv_until (gfc_equiv
*eq
, gfc_equiv
*stop
)
4722 gfc_free_equiv (eq
->eq
);
4723 gfc_free_equiv_until (eq
->next
, stop
);
4724 gfc_free_expr (eq
->expr
);
4730 gfc_free_equiv (gfc_equiv
*eq
)
4732 gfc_free_equiv_until (eq
, NULL
);
4736 /* Match an EQUIVALENCE statement. */
4739 gfc_match_equivalence (void)
4741 gfc_equiv
*eq
, *set
, *tail
;
4745 gfc_common_head
*common_head
= NULL
;
4753 eq
= gfc_get_equiv ();
4757 eq
->next
= gfc_current_ns
->equiv
;
4758 gfc_current_ns
->equiv
= eq
;
4760 if (gfc_match_char ('(') != MATCH_YES
)
4764 common_flag
= FALSE
;
4769 m
= gfc_match_equiv_variable (&set
->expr
);
4770 if (m
== MATCH_ERROR
)
4775 /* count the number of objects. */
4778 if (gfc_match_char ('%') == MATCH_YES
)
4780 gfc_error ("Derived type component %C is not a "
4781 "permitted EQUIVALENCE member");
4785 for (ref
= set
->expr
->ref
; ref
; ref
= ref
->next
)
4786 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
== AR_SECTION
)
4788 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
4789 "be an array section");
4793 sym
= set
->expr
->symtree
->n
.sym
;
4795 if (gfc_add_in_equivalence (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4798 if (sym
->attr
.in_common
)
4801 common_head
= sym
->common_head
;
4804 if (gfc_match_char (')') == MATCH_YES
)
4807 if (gfc_match_char (',') != MATCH_YES
)
4810 set
->eq
= gfc_get_equiv ();
4816 gfc_error ("EQUIVALENCE at %C requires two or more objects");
4820 /* If one of the members of an equivalence is in common, then
4821 mark them all as being in common. Before doing this, check
4822 that members of the equivalence group are not in different
4825 for (set
= eq
; set
; set
= set
->eq
)
4827 sym
= set
->expr
->symtree
->n
.sym
;
4828 if (sym
->common_head
&& sym
->common_head
!= common_head
)
4830 gfc_error ("Attempt to indirectly overlap COMMON "
4831 "blocks %s and %s by EQUIVALENCE at %C",
4832 sym
->common_head
->name
, common_head
->name
);
4835 sym
->attr
.in_common
= 1;
4836 sym
->common_head
= common_head
;
4839 if (gfc_match_eos () == MATCH_YES
)
4841 if (gfc_match_char (',') != MATCH_YES
)
4843 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
4851 gfc_syntax_error (ST_EQUIVALENCE
);
4857 gfc_free_equiv (gfc_current_ns
->equiv
);
4858 gfc_current_ns
->equiv
= eq
;
4864 /* Check that a statement function is not recursive. This is done by looking
4865 for the statement function symbol(sym) by looking recursively through its
4866 expression(e). If a reference to sym is found, true is returned.
4867 12.5.4 requires that any variable of function that is implicitly typed
4868 shall have that type confirmed by any subsequent type declaration. The
4869 implicit typing is conveniently done here. */
4871 recursive_stmt_fcn (gfc_expr
*, gfc_symbol
*);
4874 check_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
4880 switch (e
->expr_type
)
4883 if (e
->symtree
== NULL
)
4886 /* Check the name before testing for nested recursion! */
4887 if (sym
->name
== e
->symtree
->n
.sym
->name
)
4890 /* Catch recursion via other statement functions. */
4891 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
4892 && e
->symtree
->n
.sym
->value
4893 && recursive_stmt_fcn (e
->symtree
->n
.sym
->value
, sym
))
4896 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
4897 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
4902 if (e
->symtree
&& sym
->name
== e
->symtree
->n
.sym
->name
)
4905 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
4906 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
4918 recursive_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
)
4920 return gfc_traverse_expr (e
, sym
, check_stmt_fcn
, 0);
4924 /* Match a statement function declaration. It is so easy to match
4925 non-statement function statements with a MATCH_ERROR as opposed to
4926 MATCH_NO that we suppress error message in most cases. */
4929 gfc_match_st_function (void)
4931 gfc_error_buf old_error
;
4936 m
= gfc_match_symbol (&sym
, 0);
4940 gfc_push_error (&old_error
);
4942 if (gfc_add_procedure (&sym
->attr
, PROC_ST_FUNCTION
,
4943 sym
->name
, NULL
) == FAILURE
)
4946 if (gfc_match_formal_arglist (sym
, 1, 0) != MATCH_YES
)
4949 m
= gfc_match (" = %e%t", &expr
);
4953 gfc_free_error (&old_error
);
4954 if (m
== MATCH_ERROR
)
4957 if (recursive_stmt_fcn (expr
, sym
))
4959 gfc_error ("Statement function at %L is recursive", &expr
->where
);
4965 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: "
4966 "Statement function at %C") == FAILURE
)
4972 gfc_pop_error (&old_error
);
4977 /***************** SELECT CASE subroutines ******************/
4979 /* Free a single case structure. */
4982 free_case (gfc_case
*p
)
4984 if (p
->low
== p
->high
)
4986 gfc_free_expr (p
->low
);
4987 gfc_free_expr (p
->high
);
4992 /* Free a list of case structures. */
4995 gfc_free_case_list (gfc_case
*p
)
5007 /* Match a single case selector. */
5010 match_case_selector (gfc_case
**cp
)
5015 c
= gfc_get_case ();
5016 c
->where
= gfc_current_locus
;
5018 if (gfc_match_char (':') == MATCH_YES
)
5020 m
= gfc_match_init_expr (&c
->high
);
5023 if (m
== MATCH_ERROR
)
5028 m
= gfc_match_init_expr (&c
->low
);
5029 if (m
== MATCH_ERROR
)
5034 /* If we're not looking at a ':' now, make a range out of a single
5035 target. Else get the upper bound for the case range. */
5036 if (gfc_match_char (':') != MATCH_YES
)
5040 m
= gfc_match_init_expr (&c
->high
);
5041 if (m
== MATCH_ERROR
)
5043 /* MATCH_NO is fine. It's OK if nothing is there! */
5051 gfc_error ("Expected initialization expression in CASE at %C");
5059 /* Match the end of a case statement. */
5062 match_case_eos (void)
5064 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5067 if (gfc_match_eos () == MATCH_YES
)
5070 /* If the case construct doesn't have a case-construct-name, we
5071 should have matched the EOS. */
5072 if (!gfc_current_block ())
5075 gfc_gobble_whitespace ();
5077 m
= gfc_match_name (name
);
5081 if (strcmp (name
, gfc_current_block ()->name
) != 0)
5083 gfc_error ("Expected block name '%s' of SELECT construct at %C",
5084 gfc_current_block ()->name
);
5088 return gfc_match_eos ();
5092 /* Match a SELECT statement. */
5095 gfc_match_select (void)
5100 m
= gfc_match_label ();
5101 if (m
== MATCH_ERROR
)
5104 m
= gfc_match (" select case ( %e )%t", &expr
);
5108 new_st
.op
= EXEC_SELECT
;
5109 new_st
.expr1
= expr
;
5115 /* Push the current selector onto the SELECT TYPE stack. */
5118 select_type_push (gfc_symbol
*sel
)
5120 gfc_select_type_stack
*top
= gfc_get_select_type_stack ();
5121 top
->selector
= sel
;
5123 top
->prev
= select_type_stack
;
5125 select_type_stack
= top
;
5129 /* Set the temporary for the current SELECT TYPE selector. */
5132 select_type_set_tmp (gfc_typespec
*ts
)
5134 char name
[GFC_MAX_SYMBOL_LEN
];
5139 select_type_stack
->tmp
= NULL
;
5143 if (!gfc_type_is_extensible (ts
->u
.derived
))
5146 if (ts
->type
== BT_CLASS
)
5147 sprintf (name
, "__tmp_class_%s", ts
->u
.derived
->name
);
5149 sprintf (name
, "__tmp_type_%s", ts
->u
.derived
->name
);
5150 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
5151 gfc_add_type (tmp
->n
.sym
, ts
, NULL
);
5153 /* Copy across the array spec to the selector, taking care as to
5154 whether or not it is a class object or not. */
5155 if (select_type_stack
->selector
->ts
.type
== BT_CLASS
5156 && select_type_stack
->selector
->attr
.class_ok
5157 && (CLASS_DATA (select_type_stack
->selector
)->attr
.dimension
5158 || CLASS_DATA (select_type_stack
->selector
)->attr
.codimension
))
5160 if (ts
->type
== BT_CLASS
)
5162 CLASS_DATA (tmp
->n
.sym
)->attr
.dimension
5163 = CLASS_DATA (select_type_stack
->selector
)->attr
.dimension
;
5164 CLASS_DATA (tmp
->n
.sym
)->attr
.codimension
5165 = CLASS_DATA (select_type_stack
->selector
)->attr
.codimension
;
5166 CLASS_DATA (tmp
->n
.sym
)->as
= gfc_get_array_spec ();
5167 CLASS_DATA (tmp
->n
.sym
)->as
5168 = CLASS_DATA (select_type_stack
->selector
)->as
;
5172 tmp
->n
.sym
->attr
.dimension
5173 = CLASS_DATA (select_type_stack
->selector
)->attr
.dimension
;
5174 tmp
->n
.sym
->attr
.codimension
5175 = CLASS_DATA (select_type_stack
->selector
)->attr
.codimension
;
5176 tmp
->n
.sym
->as
= gfc_get_array_spec ();
5177 tmp
->n
.sym
->as
= CLASS_DATA (select_type_stack
->selector
)->as
;
5181 gfc_set_sym_referenced (tmp
->n
.sym
);
5182 gfc_add_flavor (&tmp
->n
.sym
->attr
, FL_VARIABLE
, name
, NULL
);
5183 tmp
->n
.sym
->attr
.select_type_temporary
= 1;
5184 if (ts
->type
== BT_CLASS
)
5185 gfc_build_class_symbol (&tmp
->n
.sym
->ts
, &tmp
->n
.sym
->attr
,
5186 &tmp
->n
.sym
->as
, false);
5188 /* Add an association for it, so the rest of the parser knows it is
5189 an associate-name. The target will be set during resolution. */
5190 tmp
->n
.sym
->assoc
= gfc_get_association_list ();
5191 tmp
->n
.sym
->assoc
->dangling
= 1;
5192 tmp
->n
.sym
->assoc
->st
= tmp
;
5194 select_type_stack
->tmp
= tmp
;
5198 /* Match a SELECT TYPE statement. */
5201 gfc_match_select_type (void)
5203 gfc_expr
*expr1
, *expr2
= NULL
;
5205 char name
[GFC_MAX_SYMBOL_LEN
];
5208 m
= gfc_match_label ();
5209 if (m
== MATCH_ERROR
)
5212 m
= gfc_match (" select type ( ");
5216 gfc_current_ns
= gfc_build_block_ns (gfc_current_ns
);
5218 m
= gfc_match (" %n => %e", name
, &expr2
);
5221 expr1
= gfc_get_expr();
5222 expr1
->expr_type
= EXPR_VARIABLE
;
5223 if (gfc_get_sym_tree (name
, NULL
, &expr1
->symtree
, false))
5228 if (expr2
->ts
.type
== BT_UNKNOWN
)
5229 expr1
->symtree
->n
.sym
->attr
.untyped
= 1;
5231 expr1
->symtree
->n
.sym
->ts
= expr2
->ts
;
5232 expr1
->symtree
->n
.sym
->attr
.flavor
= FL_VARIABLE
;
5233 expr1
->symtree
->n
.sym
->attr
.referenced
= 1;
5234 expr1
->symtree
->n
.sym
->attr
.class_ok
= 1;
5238 m
= gfc_match (" %e ", &expr1
);
5243 m
= gfc_match (" )%t");
5247 /* This ghastly expression seems to be needed to distinguish a CLASS
5248 array, which can have a reference, from other expressions that
5249 have references, such as derived type components, and are not
5250 allowed by the standard.
5251 TODO; see is it is sufficent to exclude component and substring
5253 class_array
= expr1
->expr_type
== EXPR_VARIABLE
5254 && expr1
->ts
.type
!= BT_UNKNOWN
5255 && CLASS_DATA (expr1
)
5256 && (strcmp (CLASS_DATA (expr1
)->name
, "_data") == 0)
5257 && (CLASS_DATA (expr1
)->attr
.dimension
5258 || CLASS_DATA (expr1
)->attr
.codimension
)
5260 && expr1
->ref
->type
== REF_ARRAY
5261 && expr1
->ref
->next
== NULL
;
5263 /* Check for F03:C811. */
5264 if (!expr2
&& (expr1
->expr_type
!= EXPR_VARIABLE
5265 || (!class_array
&& expr1
->ref
!= NULL
)))
5267 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
5268 "use associate-name=>");
5273 new_st
.op
= EXEC_SELECT_TYPE
;
5274 new_st
.expr1
= expr1
;
5275 new_st
.expr2
= expr2
;
5276 new_st
.ext
.block
.ns
= gfc_current_ns
;
5278 select_type_push (expr1
->symtree
->n
.sym
);
5283 gfc_current_ns
= gfc_current_ns
->parent
;
5288 /* Match a CASE statement. */
5291 gfc_match_case (void)
5293 gfc_case
*c
, *head
, *tail
;
5298 if (gfc_current_state () != COMP_SELECT
)
5300 gfc_error ("Unexpected CASE statement at %C");
5304 if (gfc_match ("% default") == MATCH_YES
)
5306 m
= match_case_eos ();
5309 if (m
== MATCH_ERROR
)
5312 new_st
.op
= EXEC_SELECT
;
5313 c
= gfc_get_case ();
5314 c
->where
= gfc_current_locus
;
5315 new_st
.ext
.block
.case_list
= c
;
5319 if (gfc_match_char ('(') != MATCH_YES
)
5324 if (match_case_selector (&c
) == MATCH_ERROR
)
5334 if (gfc_match_char (')') == MATCH_YES
)
5336 if (gfc_match_char (',') != MATCH_YES
)
5340 m
= match_case_eos ();
5343 if (m
== MATCH_ERROR
)
5346 new_st
.op
= EXEC_SELECT
;
5347 new_st
.ext
.block
.case_list
= head
;
5352 gfc_error ("Syntax error in CASE specification at %C");
5355 gfc_free_case_list (head
); /* new_st is cleaned up in parse.c. */
5360 /* Match a TYPE IS statement. */
5363 gfc_match_type_is (void)
5368 if (gfc_current_state () != COMP_SELECT_TYPE
)
5370 gfc_error ("Unexpected TYPE IS statement at %C");
5374 if (gfc_match_char ('(') != MATCH_YES
)
5377 c
= gfc_get_case ();
5378 c
->where
= gfc_current_locus
;
5380 /* TODO: Once unlimited polymorphism is implemented, we will need to call
5381 match_type_spec here. */
5382 if (match_derived_type_spec (&c
->ts
) == MATCH_ERROR
)
5385 if (gfc_match_char (')') != MATCH_YES
)
5388 m
= match_case_eos ();
5391 if (m
== MATCH_ERROR
)
5394 new_st
.op
= EXEC_SELECT_TYPE
;
5395 new_st
.ext
.block
.case_list
= c
;
5397 /* Create temporary variable. */
5398 select_type_set_tmp (&c
->ts
);
5403 gfc_error ("Syntax error in TYPE IS specification at %C");
5407 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
5412 /* Match a CLASS IS or CLASS DEFAULT statement. */
5415 gfc_match_class_is (void)
5420 if (gfc_current_state () != COMP_SELECT_TYPE
)
5423 if (gfc_match ("% default") == MATCH_YES
)
5425 m
= match_case_eos ();
5428 if (m
== MATCH_ERROR
)
5431 new_st
.op
= EXEC_SELECT_TYPE
;
5432 c
= gfc_get_case ();
5433 c
->where
= gfc_current_locus
;
5434 c
->ts
.type
= BT_UNKNOWN
;
5435 new_st
.ext
.block
.case_list
= c
;
5436 select_type_set_tmp (NULL
);
5440 m
= gfc_match ("% is");
5443 if (m
== MATCH_ERROR
)
5446 if (gfc_match_char ('(') != MATCH_YES
)
5449 c
= gfc_get_case ();
5450 c
->where
= gfc_current_locus
;
5452 if (match_derived_type_spec (&c
->ts
) == MATCH_ERROR
)
5455 if (c
->ts
.type
== BT_DERIVED
)
5456 c
->ts
.type
= BT_CLASS
;
5458 if (gfc_match_char (')') != MATCH_YES
)
5461 m
= match_case_eos ();
5464 if (m
== MATCH_ERROR
)
5467 new_st
.op
= EXEC_SELECT_TYPE
;
5468 new_st
.ext
.block
.case_list
= c
;
5470 /* Create temporary variable. */
5471 select_type_set_tmp (&c
->ts
);
5476 gfc_error ("Syntax error in CLASS IS specification at %C");
5480 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
5485 /********************* WHERE subroutines ********************/
5487 /* Match the rest of a simple WHERE statement that follows an IF statement.
5491 match_simple_where (void)
5497 m
= gfc_match (" ( %e )", &expr
);
5501 m
= gfc_match_assignment ();
5504 if (m
== MATCH_ERROR
)
5507 if (gfc_match_eos () != MATCH_YES
)
5510 c
= gfc_get_code ();
5514 c
->next
= gfc_get_code ();
5517 gfc_clear_new_st ();
5519 new_st
.op
= EXEC_WHERE
;
5525 gfc_syntax_error (ST_WHERE
);
5528 gfc_free_expr (expr
);
5533 /* Match a WHERE statement. */
5536 gfc_match_where (gfc_statement
*st
)
5542 m0
= gfc_match_label ();
5543 if (m0
== MATCH_ERROR
)
5546 m
= gfc_match (" where ( %e )", &expr
);
5550 if (gfc_match_eos () == MATCH_YES
)
5552 *st
= ST_WHERE_BLOCK
;
5553 new_st
.op
= EXEC_WHERE
;
5554 new_st
.expr1
= expr
;
5558 m
= gfc_match_assignment ();
5560 gfc_syntax_error (ST_WHERE
);
5564 gfc_free_expr (expr
);
5568 /* We've got a simple WHERE statement. */
5570 c
= gfc_get_code ();
5574 c
->next
= gfc_get_code ();
5577 gfc_clear_new_st ();
5579 new_st
.op
= EXEC_WHERE
;
5586 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
5587 new_st if successful. */
5590 gfc_match_elsewhere (void)
5592 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5596 if (gfc_current_state () != COMP_WHERE
)
5598 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
5604 if (gfc_match_char ('(') == MATCH_YES
)
5606 m
= gfc_match_expr (&expr
);
5609 if (m
== MATCH_ERROR
)
5612 if (gfc_match_char (')') != MATCH_YES
)
5616 if (gfc_match_eos () != MATCH_YES
)
5618 /* Only makes sense if we have a where-construct-name. */
5619 if (!gfc_current_block ())
5624 /* Better be a name at this point. */
5625 m
= gfc_match_name (name
);
5628 if (m
== MATCH_ERROR
)
5631 if (gfc_match_eos () != MATCH_YES
)
5634 if (strcmp (name
, gfc_current_block ()->name
) != 0)
5636 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
5637 name
, gfc_current_block ()->name
);
5642 new_st
.op
= EXEC_WHERE
;
5643 new_st
.expr1
= expr
;
5647 gfc_syntax_error (ST_ELSEWHERE
);
5650 gfc_free_expr (expr
);