1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
31 #include "double-int.h"
38 #include "stringpool.h"
40 int gfc_matching_ptr_assignment
= 0;
41 int gfc_matching_procptr_assignment
= 0;
42 bool gfc_matching_prefix
= false;
44 /* Stack of SELECT TYPE statements. */
45 gfc_select_type_stack
*select_type_stack
= NULL
;
47 /* For debugging and diagnostic purposes. Return the textual representation
48 of the intrinsic operator OP. */
50 gfc_op2string (gfc_intrinsic_op op
)
58 case INTRINSIC_UMINUS
:
64 case INTRINSIC_CONCAT
:
68 case INTRINSIC_DIVIDE
:
100 case INTRINSIC_GT_OS
:
107 case INTRINSIC_ASSIGN
:
110 case INTRINSIC_PARENTHESES
:
120 gfc_internal_error ("gfc_op2string(): Bad code");
125 /******************** Generic matching subroutines ************************/
127 /* This function scans the current statement counting the opened and closed
128 parenthesis to make sure they are balanced. */
131 gfc_match_parens (void)
133 locus old_loc
, where
;
135 gfc_instring instring
;
138 old_loc
= gfc_current_locus
;
140 instring
= NONSTRING
;
145 c
= gfc_next_char_literal (instring
);
148 if (quote
== ' ' && ((c
== '\'') || (c
== '"')))
151 instring
= INSTRING_WARN
;
154 if (quote
!= ' ' && c
== quote
)
157 instring
= NONSTRING
;
161 if (c
== '(' && quote
== ' ')
164 where
= gfc_current_locus
;
166 if (c
== ')' && quote
== ' ')
169 where
= gfc_current_locus
;
173 gfc_current_locus
= old_loc
;
177 gfc_error ("Missing %<)%> in statement at or before %L", &where
);
182 gfc_error ("Missing %<(%> in statement at or before %L", &where
);
190 /* See if the next character is a special character that has
191 escaped by a \ via the -fbackslash option. */
194 gfc_match_special_char (gfc_char_t
*res
)
202 switch ((c
= gfc_next_char_literal (INSTRING_WARN
)))
235 /* Hexadecimal form of wide characters. */
236 len
= (c
== 'x' ? 2 : (c
== 'u' ? 4 : 8));
238 for (i
= 0; i
< len
; i
++)
240 char buf
[2] = { '\0', '\0' };
242 c
= gfc_next_char_literal (INSTRING_WARN
);
243 if (!gfc_wide_fits_in_byte (c
)
244 || !gfc_check_digit ((unsigned char) c
, 16))
247 buf
[0] = (unsigned char) c
;
249 n
+= strtol (buf
, NULL
, 16);
255 /* Unknown backslash codes are simply not expanded. */
264 /* In free form, match at least one space. Always matches in fixed
268 gfc_match_space (void)
273 if (gfc_current_form
== FORM_FIXED
)
276 old_loc
= gfc_current_locus
;
278 c
= gfc_next_ascii_char ();
279 if (!gfc_is_whitespace (c
))
281 gfc_current_locus
= old_loc
;
285 gfc_gobble_whitespace ();
291 /* Match an end of statement. End of statement is optional
292 whitespace, followed by a ';' or '\n' or comment '!'. If a
293 semicolon is found, we continue to eat whitespace and semicolons. */
306 old_loc
= gfc_current_locus
;
307 gfc_gobble_whitespace ();
309 c
= gfc_next_ascii_char ();
315 c
= gfc_next_ascii_char ();
332 gfc_current_locus
= old_loc
;
333 return (flag
) ? MATCH_YES
: MATCH_NO
;
337 /* Match a literal integer on the input, setting the value on
338 MATCH_YES. Literal ints occur in kind-parameters as well as
339 old-style character length specifications. If cnt is non-NULL it
340 will be set to the number of digits. */
343 gfc_match_small_literal_int (int *value
, int *cnt
)
349 old_loc
= gfc_current_locus
;
352 gfc_gobble_whitespace ();
353 c
= gfc_next_ascii_char ();
359 gfc_current_locus
= old_loc
;
368 old_loc
= gfc_current_locus
;
369 c
= gfc_next_ascii_char ();
374 i
= 10 * i
+ c
- '0';
379 gfc_error ("Integer too large at %C");
384 gfc_current_locus
= old_loc
;
393 /* Match a small, constant integer expression, like in a kind
394 statement. On MATCH_YES, 'value' is set. */
397 gfc_match_small_int (int *value
)
404 m
= gfc_match_expr (&expr
);
408 p
= gfc_extract_int (expr
, &i
);
409 gfc_free_expr (expr
);
422 /* This function is the same as the gfc_match_small_int, except that
423 we're keeping the pointer to the expr. This function could just be
424 removed and the previously mentioned one modified, though all calls
425 to it would have to be modified then (and there were a number of
426 them). Return MATCH_ERROR if fail to extract the int; otherwise,
427 return the result of gfc_match_expr(). The expr (if any) that was
428 matched is returned in the parameter expr. */
431 gfc_match_small_int_expr (int *value
, gfc_expr
**expr
)
437 m
= gfc_match_expr (expr
);
441 p
= gfc_extract_int (*expr
, &i
);
454 /* Matches a statement label. Uses gfc_match_small_literal_int() to
455 do most of the work. */
458 gfc_match_st_label (gfc_st_label
**label
)
464 old_loc
= gfc_current_locus
;
466 m
= gfc_match_small_literal_int (&i
, &cnt
);
472 gfc_error ("Too many digits in statement label at %C");
478 gfc_error ("Statement label at %C is zero");
482 *label
= gfc_get_st_label (i
);
487 gfc_current_locus
= old_loc
;
492 /* Match and validate a label associated with a named IF, DO or SELECT
493 statement. If the symbol does not have the label attribute, we add
494 it. We also make sure the symbol does not refer to another
495 (active) block. A matched label is pointed to by gfc_new_block. */
498 gfc_match_label (void)
500 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
503 gfc_new_block
= NULL
;
505 m
= gfc_match (" %n :", name
);
509 if (gfc_get_symbol (name
, NULL
, &gfc_new_block
))
511 gfc_error ("Label name %qs at %C is ambiguous", name
);
515 if (gfc_new_block
->attr
.flavor
== FL_LABEL
)
517 gfc_error ("Duplicate construct label %qs at %C", name
);
521 if (!gfc_add_flavor (&gfc_new_block
->attr
, FL_LABEL
,
522 gfc_new_block
->name
, NULL
))
529 /* See if the current input looks like a name of some sort. Modifies
530 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
531 Note that options.c restricts max_identifier_length to not more
532 than GFC_MAX_SYMBOL_LEN. */
535 gfc_match_name (char *buffer
)
541 old_loc
= gfc_current_locus
;
542 gfc_gobble_whitespace ();
544 c
= gfc_next_ascii_char ();
545 if (!(ISALPHA (c
) || (c
== '_' && flag_allow_leading_underscore
)))
547 if (!gfc_error_flag_test () && c
!= '(')
548 gfc_error ("Invalid character in name at %C");
549 gfc_current_locus
= old_loc
;
559 if (i
> gfc_option
.max_identifier_length
)
561 gfc_error ("Name at %C is too long");
565 old_loc
= gfc_current_locus
;
566 c
= gfc_next_ascii_char ();
568 while (ISALNUM (c
) || c
== '_' || (flag_dollar_ok
&& c
== '$'));
570 if (c
== '$' && !flag_dollar_ok
)
572 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
573 "allow it as an extension", &old_loc
);
578 gfc_current_locus
= old_loc
;
584 /* Match a symbol on the input. Modifies the pointer to the symbol
585 pointer if successful. */
588 gfc_match_sym_tree (gfc_symtree
**matched_symbol
, int host_assoc
)
590 char buffer
[GFC_MAX_SYMBOL_LEN
+ 1];
593 m
= gfc_match_name (buffer
);
598 return (gfc_get_ha_sym_tree (buffer
, matched_symbol
))
599 ? MATCH_ERROR
: MATCH_YES
;
601 if (gfc_get_sym_tree (buffer
, NULL
, matched_symbol
, false))
609 gfc_match_symbol (gfc_symbol
**matched_symbol
, int host_assoc
)
614 m
= gfc_match_sym_tree (&st
, host_assoc
);
619 *matched_symbol
= st
->n
.sym
;
621 *matched_symbol
= NULL
;
624 *matched_symbol
= NULL
;
629 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
630 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
634 gfc_match_intrinsic_op (gfc_intrinsic_op
*result
)
636 locus orig_loc
= gfc_current_locus
;
639 gfc_gobble_whitespace ();
640 ch
= gfc_next_ascii_char ();
645 *result
= INTRINSIC_PLUS
;
650 *result
= INTRINSIC_MINUS
;
654 if (gfc_next_ascii_char () == '=')
657 *result
= INTRINSIC_EQ
;
663 if (gfc_peek_ascii_char () == '=')
666 gfc_next_ascii_char ();
667 *result
= INTRINSIC_LE
;
671 *result
= INTRINSIC_LT
;
675 if (gfc_peek_ascii_char () == '=')
678 gfc_next_ascii_char ();
679 *result
= INTRINSIC_GE
;
683 *result
= INTRINSIC_GT
;
687 if (gfc_peek_ascii_char () == '*')
690 gfc_next_ascii_char ();
691 *result
= INTRINSIC_POWER
;
695 *result
= INTRINSIC_TIMES
;
699 ch
= gfc_peek_ascii_char ();
703 gfc_next_ascii_char ();
704 *result
= INTRINSIC_NE
;
710 gfc_next_ascii_char ();
711 *result
= INTRINSIC_CONCAT
;
715 *result
= INTRINSIC_DIVIDE
;
719 ch
= gfc_next_ascii_char ();
723 if (gfc_next_ascii_char () == 'n'
724 && gfc_next_ascii_char () == 'd'
725 && gfc_next_ascii_char () == '.')
727 /* Matched ".and.". */
728 *result
= INTRINSIC_AND
;
734 if (gfc_next_ascii_char () == 'q')
736 ch
= gfc_next_ascii_char ();
739 /* Matched ".eq.". */
740 *result
= INTRINSIC_EQ_OS
;
745 if (gfc_next_ascii_char () == '.')
747 /* Matched ".eqv.". */
748 *result
= INTRINSIC_EQV
;
756 ch
= gfc_next_ascii_char ();
759 if (gfc_next_ascii_char () == '.')
761 /* Matched ".ge.". */
762 *result
= INTRINSIC_GE_OS
;
768 if (gfc_next_ascii_char () == '.')
770 /* Matched ".gt.". */
771 *result
= INTRINSIC_GT_OS
;
778 ch
= gfc_next_ascii_char ();
781 if (gfc_next_ascii_char () == '.')
783 /* Matched ".le.". */
784 *result
= INTRINSIC_LE_OS
;
790 if (gfc_next_ascii_char () == '.')
792 /* Matched ".lt.". */
793 *result
= INTRINSIC_LT_OS
;
800 ch
= gfc_next_ascii_char ();
803 ch
= gfc_next_ascii_char ();
806 /* Matched ".ne.". */
807 *result
= INTRINSIC_NE_OS
;
812 if (gfc_next_ascii_char () == 'v'
813 && gfc_next_ascii_char () == '.')
815 /* Matched ".neqv.". */
816 *result
= INTRINSIC_NEQV
;
823 if (gfc_next_ascii_char () == 't'
824 && gfc_next_ascii_char () == '.')
826 /* Matched ".not.". */
827 *result
= INTRINSIC_NOT
;
834 if (gfc_next_ascii_char () == 'r'
835 && gfc_next_ascii_char () == '.')
837 /* Matched ".or.". */
838 *result
= INTRINSIC_OR
;
852 gfc_current_locus
= orig_loc
;
857 /* Match a loop control phrase:
859 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
861 If the final integer expression is not present, a constant unity
862 expression is returned. We don't return MATCH_ERROR until after
863 the equals sign is seen. */
866 gfc_match_iterator (gfc_iterator
*iter
, int init_flag
)
868 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
869 gfc_expr
*var
, *e1
, *e2
, *e3
;
875 /* Match the start of an iterator without affecting the symbol table. */
877 start
= gfc_current_locus
;
878 m
= gfc_match (" %n =", name
);
879 gfc_current_locus
= start
;
884 m
= gfc_match_variable (&var
, 0);
888 /* F2008, C617 & C565. */
889 if (var
->symtree
->n
.sym
->attr
.codimension
)
891 gfc_error ("Loop variable at %C cannot be a coarray");
895 if (var
->ref
!= NULL
)
897 gfc_error ("Loop variable at %C cannot be a sub-component");
901 gfc_match_char ('=');
903 var
->symtree
->n
.sym
->attr
.implied_index
= 1;
905 m
= init_flag
? gfc_match_init_expr (&e1
) : gfc_match_expr (&e1
);
908 if (m
== MATCH_ERROR
)
911 if (gfc_match_char (',') != MATCH_YES
)
914 m
= init_flag
? gfc_match_init_expr (&e2
) : gfc_match_expr (&e2
);
917 if (m
== MATCH_ERROR
)
920 if (gfc_match_char (',') != MATCH_YES
)
922 e3
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
926 m
= init_flag
? gfc_match_init_expr (&e3
) : gfc_match_expr (&e3
);
927 if (m
== MATCH_ERROR
)
931 gfc_error ("Expected a step value in iterator at %C");
943 gfc_error ("Syntax error in iterator at %C");
954 /* Tries to match the next non-whitespace character on the input.
955 This subroutine does not return MATCH_ERROR. */
958 gfc_match_char (char c
)
962 where
= gfc_current_locus
;
963 gfc_gobble_whitespace ();
965 if (gfc_next_ascii_char () == c
)
968 gfc_current_locus
= where
;
973 /* General purpose matching subroutine. The target string is a
974 scanf-like format string in which spaces correspond to arbitrary
975 whitespace (including no whitespace), characters correspond to
976 themselves. The %-codes are:
978 %% Literal percent sign
979 %e Expression, pointer to a pointer is set
980 %s Symbol, pointer to the symbol is set
981 %n Name, character buffer is set to name
982 %t Matches end of statement.
983 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
984 %l Matches a statement label
985 %v Matches a variable expression (an lvalue)
986 % Matches a required space (in free form) and optional spaces. */
989 gfc_match (const char *target
, ...)
991 gfc_st_label
**label
;
1000 old_loc
= gfc_current_locus
;
1001 va_start (argp
, target
);
1011 gfc_gobble_whitespace ();
1022 vp
= va_arg (argp
, void **);
1023 n
= gfc_match_expr ((gfc_expr
**) vp
);
1034 vp
= va_arg (argp
, void **);
1035 n
= gfc_match_variable ((gfc_expr
**) vp
, 0);
1046 vp
= va_arg (argp
, void **);
1047 n
= gfc_match_symbol ((gfc_symbol
**) vp
, 0);
1058 np
= va_arg (argp
, char *);
1059 n
= gfc_match_name (np
);
1070 label
= va_arg (argp
, gfc_st_label
**);
1071 n
= gfc_match_st_label (label
);
1082 ip
= va_arg (argp
, int *);
1083 n
= gfc_match_intrinsic_op ((gfc_intrinsic_op
*) ip
);
1094 if (gfc_match_eos () != MATCH_YES
)
1102 if (gfc_match_space () == MATCH_YES
)
1108 break; /* Fall through to character matcher. */
1111 gfc_internal_error ("gfc_match(): Bad match code %c", c
);
1116 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1117 expect an upper case character here! */
1118 gcc_assert (TOLOWER (c
) == c
);
1120 if (c
== gfc_next_ascii_char ())
1130 /* Clean up after a failed match. */
1131 gfc_current_locus
= old_loc
;
1132 va_start (argp
, target
);
1135 for (; matches
> 0; matches
--)
1137 while (*p
++ != '%');
1145 /* Matches that don't have to be undone */
1150 (void) va_arg (argp
, void **);
1155 vp
= va_arg (argp
, void **);
1156 gfc_free_expr ((struct gfc_expr
*)*vp
);
1169 /*********************** Statement level matching **********************/
1171 /* Matches the start of a program unit, which is the program keyword
1172 followed by an obligatory symbol. */
1175 gfc_match_program (void)
1180 m
= gfc_match ("% %s%t", &sym
);
1184 gfc_error ("Invalid form of PROGRAM statement at %C");
1188 if (m
== MATCH_ERROR
)
1191 if (!gfc_add_flavor (&sym
->attr
, FL_PROGRAM
, sym
->name
, NULL
))
1194 gfc_new_block
= sym
;
1200 /* Match a simple assignment statement. */
1203 gfc_match_assignment (void)
1205 gfc_expr
*lvalue
, *rvalue
;
1209 old_loc
= gfc_current_locus
;
1212 m
= gfc_match (" %v =", &lvalue
);
1215 gfc_current_locus
= old_loc
;
1216 gfc_free_expr (lvalue
);
1221 m
= gfc_match (" %e%t", &rvalue
);
1224 gfc_current_locus
= old_loc
;
1225 gfc_free_expr (lvalue
);
1226 gfc_free_expr (rvalue
);
1230 gfc_set_sym_referenced (lvalue
->symtree
->n
.sym
);
1232 new_st
.op
= EXEC_ASSIGN
;
1233 new_st
.expr1
= lvalue
;
1234 new_st
.expr2
= rvalue
;
1236 gfc_check_do_variable (lvalue
->symtree
);
1242 /* Match a pointer assignment statement. */
1245 gfc_match_pointer_assignment (void)
1247 gfc_expr
*lvalue
, *rvalue
;
1251 old_loc
= gfc_current_locus
;
1253 lvalue
= rvalue
= NULL
;
1254 gfc_matching_ptr_assignment
= 0;
1255 gfc_matching_procptr_assignment
= 0;
1257 m
= gfc_match (" %v =>", &lvalue
);
1264 if (lvalue
->symtree
->n
.sym
->attr
.proc_pointer
1265 || gfc_is_proc_ptr_comp (lvalue
))
1266 gfc_matching_procptr_assignment
= 1;
1268 gfc_matching_ptr_assignment
= 1;
1270 m
= gfc_match (" %e%t", &rvalue
);
1271 gfc_matching_ptr_assignment
= 0;
1272 gfc_matching_procptr_assignment
= 0;
1276 new_st
.op
= EXEC_POINTER_ASSIGN
;
1277 new_st
.expr1
= lvalue
;
1278 new_st
.expr2
= rvalue
;
1283 gfc_current_locus
= old_loc
;
1284 gfc_free_expr (lvalue
);
1285 gfc_free_expr (rvalue
);
1290 /* We try to match an easy arithmetic IF statement. This only happens
1291 when just after having encountered a simple IF statement. This code
1292 is really duplicate with parts of the gfc_match_if code, but this is
1296 match_arithmetic_if (void)
1298 gfc_st_label
*l1
, *l2
, *l3
;
1302 m
= gfc_match (" ( %e ) %l , %l , %l%t", &expr
, &l1
, &l2
, &l3
);
1306 if (!gfc_reference_st_label (l1
, ST_LABEL_TARGET
)
1307 || !gfc_reference_st_label (l2
, ST_LABEL_TARGET
)
1308 || !gfc_reference_st_label (l3
, ST_LABEL_TARGET
))
1310 gfc_free_expr (expr
);
1314 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Arithmetic IF statement at %C"))
1317 new_st
.op
= EXEC_ARITHMETIC_IF
;
1318 new_st
.expr1
= expr
;
1327 /* The IF statement is a bit of a pain. First of all, there are three
1328 forms of it, the simple IF, the IF that starts a block and the
1331 There is a problem with the simple IF and that is the fact that we
1332 only have a single level of undo information on symbols. What this
1333 means is for a simple IF, we must re-match the whole IF statement
1334 multiple times in order to guarantee that the symbol table ends up
1335 in the proper state. */
1337 static match
match_simple_forall (void);
1338 static match
match_simple_where (void);
1341 gfc_match_if (gfc_statement
*if_type
)
1344 gfc_st_label
*l1
, *l2
, *l3
;
1345 locus old_loc
, old_loc2
;
1349 n
= gfc_match_label ();
1350 if (n
== MATCH_ERROR
)
1353 old_loc
= gfc_current_locus
;
1355 m
= gfc_match (" if ( %e", &expr
);
1359 old_loc2
= gfc_current_locus
;
1360 gfc_current_locus
= old_loc
;
1362 if (gfc_match_parens () == MATCH_ERROR
)
1365 gfc_current_locus
= old_loc2
;
1367 if (gfc_match_char (')') != MATCH_YES
)
1369 gfc_error ("Syntax error in IF-expression at %C");
1370 gfc_free_expr (expr
);
1374 m
= gfc_match (" %l , %l , %l%t", &l1
, &l2
, &l3
);
1380 gfc_error ("Block label not appropriate for arithmetic IF "
1382 gfc_free_expr (expr
);
1386 if (!gfc_reference_st_label (l1
, ST_LABEL_TARGET
)
1387 || !gfc_reference_st_label (l2
, ST_LABEL_TARGET
)
1388 || !gfc_reference_st_label (l3
, ST_LABEL_TARGET
))
1390 gfc_free_expr (expr
);
1394 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Arithmetic IF statement at %C"))
1397 new_st
.op
= EXEC_ARITHMETIC_IF
;
1398 new_st
.expr1
= expr
;
1403 *if_type
= ST_ARITHMETIC_IF
;
1407 if (gfc_match (" then%t") == MATCH_YES
)
1409 new_st
.op
= EXEC_IF
;
1410 new_st
.expr1
= expr
;
1411 *if_type
= ST_IF_BLOCK
;
1417 gfc_error ("Block label is not appropriate for IF statement at %C");
1418 gfc_free_expr (expr
);
1422 /* At this point the only thing left is a simple IF statement. At
1423 this point, n has to be MATCH_NO, so we don't have to worry about
1424 re-matching a block label. From what we've got so far, try
1425 matching an assignment. */
1427 *if_type
= ST_SIMPLE_IF
;
1429 m
= gfc_match_assignment ();
1433 gfc_free_expr (expr
);
1434 gfc_undo_symbols ();
1435 gfc_current_locus
= old_loc
;
1437 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1438 assignment was found. For MATCH_NO, continue to call the various
1440 if (m
== MATCH_ERROR
)
1443 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match. */
1445 m
= gfc_match_pointer_assignment ();
1449 gfc_free_expr (expr
);
1450 gfc_undo_symbols ();
1451 gfc_current_locus
= old_loc
;
1453 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match. */
1455 /* Look at the next keyword to see which matcher to call. Matching
1456 the keyword doesn't affect the symbol table, so we don't have to
1457 restore between tries. */
1459 #define match(string, subr, statement) \
1460 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1464 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
)
1465 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
)
1466 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
)
1467 match ("call", gfc_match_call
, ST_CALL
)
1468 match ("close", gfc_match_close
, ST_CLOSE
)
1469 match ("continue", gfc_match_continue
, ST_CONTINUE
)
1470 match ("cycle", gfc_match_cycle
, ST_CYCLE
)
1471 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
)
1472 match ("end file", gfc_match_endfile
, ST_END_FILE
)
1473 match ("error stop", gfc_match_error_stop
, ST_ERROR_STOP
)
1474 match ("exit", gfc_match_exit
, ST_EXIT
)
1475 match ("flush", gfc_match_flush
, ST_FLUSH
)
1476 match ("forall", match_simple_forall
, ST_FORALL
)
1477 match ("go to", gfc_match_goto
, ST_GOTO
)
1478 match ("if", match_arithmetic_if
, ST_ARITHMETIC_IF
)
1479 match ("inquire", gfc_match_inquire
, ST_INQUIRE
)
1480 match ("lock", gfc_match_lock
, ST_LOCK
)
1481 match ("nullify", gfc_match_nullify
, ST_NULLIFY
)
1482 match ("open", gfc_match_open
, ST_OPEN
)
1483 match ("pause", gfc_match_pause
, ST_NONE
)
1484 match ("print", gfc_match_print
, ST_WRITE
)
1485 match ("read", gfc_match_read
, ST_READ
)
1486 match ("return", gfc_match_return
, ST_RETURN
)
1487 match ("rewind", gfc_match_rewind
, ST_REWIND
)
1488 match ("stop", gfc_match_stop
, ST_STOP
)
1489 match ("wait", gfc_match_wait
, ST_WAIT
)
1490 match ("sync all", gfc_match_sync_all
, ST_SYNC_CALL
);
1491 match ("sync images", gfc_match_sync_images
, ST_SYNC_IMAGES
);
1492 match ("sync memory", gfc_match_sync_memory
, ST_SYNC_MEMORY
);
1493 match ("unlock", gfc_match_unlock
, ST_UNLOCK
)
1494 match ("where", match_simple_where
, ST_WHERE
)
1495 match ("write", gfc_match_write
, ST_WRITE
)
1497 /* The gfc_match_assignment() above may have returned a MATCH_NO
1498 where the assignment was to a named constant. Check that
1499 special case here. */
1500 m
= gfc_match_assignment ();
1503 gfc_error ("Cannot assign to a named constant at %C");
1504 gfc_free_expr (expr
);
1505 gfc_undo_symbols ();
1506 gfc_current_locus
= old_loc
;
1510 /* All else has failed, so give up. See if any of the matchers has
1511 stored an error message of some sort. */
1512 if (!gfc_error_check ())
1513 gfc_error ("Unclassifiable statement in IF-clause at %C");
1515 gfc_free_expr (expr
);
1520 gfc_error ("Syntax error in IF-clause at %C");
1523 gfc_free_expr (expr
);
1527 /* At this point, we've matched the single IF and the action clause
1528 is in new_st. Rearrange things so that the IF statement appears
1531 p
= gfc_get_code (EXEC_IF
);
1532 p
->next
= XCNEW (gfc_code
);
1534 p
->next
->loc
= gfc_current_locus
;
1538 gfc_clear_new_st ();
1540 new_st
.op
= EXEC_IF
;
1549 /* Match an ELSE statement. */
1552 gfc_match_else (void)
1554 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1556 if (gfc_match_eos () == MATCH_YES
)
1559 if (gfc_match_name (name
) != MATCH_YES
1560 || gfc_current_block () == NULL
1561 || gfc_match_eos () != MATCH_YES
)
1563 gfc_error ("Unexpected junk after ELSE statement at %C");
1567 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1569 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1570 name
, gfc_current_block ()->name
);
1578 /* Match an ELSE IF statement. */
1581 gfc_match_elseif (void)
1583 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1587 m
= gfc_match (" ( %e ) then", &expr
);
1591 if (gfc_match_eos () == MATCH_YES
)
1594 if (gfc_match_name (name
) != MATCH_YES
1595 || gfc_current_block () == NULL
1596 || gfc_match_eos () != MATCH_YES
)
1598 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1602 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1604 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1605 name
, gfc_current_block ()->name
);
1610 new_st
.op
= EXEC_IF
;
1611 new_st
.expr1
= expr
;
1615 gfc_free_expr (expr
);
1620 /* Free a gfc_iterator structure. */
1623 gfc_free_iterator (gfc_iterator
*iter
, int flag
)
1629 gfc_free_expr (iter
->var
);
1630 gfc_free_expr (iter
->start
);
1631 gfc_free_expr (iter
->end
);
1632 gfc_free_expr (iter
->step
);
1639 /* Match a CRITICAL statement. */
1641 gfc_match_critical (void)
1643 gfc_st_label
*label
= NULL
;
1645 if (gfc_match_label () == MATCH_ERROR
)
1648 if (gfc_match (" critical") != MATCH_YES
)
1651 if (gfc_match_st_label (&label
) == MATCH_ERROR
)
1654 if (gfc_match_eos () != MATCH_YES
)
1656 gfc_syntax_error (ST_CRITICAL
);
1660 if (gfc_pure (NULL
))
1662 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1666 if (gfc_find_state (COMP_DO_CONCURRENT
))
1668 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1673 gfc_unset_implicit_pure (NULL
);
1675 if (!gfc_notify_std (GFC_STD_F2008
, "CRITICAL statement at %C"))
1678 if (flag_coarray
== GFC_FCOARRAY_NONE
)
1680 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1685 if (gfc_find_state (COMP_CRITICAL
))
1687 gfc_error ("Nested CRITICAL block at %C");
1691 new_st
.op
= EXEC_CRITICAL
;
1694 && !gfc_reference_st_label (label
, ST_LABEL_TARGET
))
1701 /* Match a BLOCK statement. */
1704 gfc_match_block (void)
1708 if (gfc_match_label () == MATCH_ERROR
)
1711 if (gfc_match (" block") != MATCH_YES
)
1714 /* For this to be a correct BLOCK statement, the line must end now. */
1715 m
= gfc_match_eos ();
1716 if (m
== MATCH_ERROR
)
1725 /* Match an ASSOCIATE statement. */
1728 gfc_match_associate (void)
1730 if (gfc_match_label () == MATCH_ERROR
)
1733 if (gfc_match (" associate") != MATCH_YES
)
1736 /* Match the association list. */
1737 if (gfc_match_char ('(') != MATCH_YES
)
1739 gfc_error ("Expected association list at %C");
1742 new_st
.ext
.block
.assoc
= NULL
;
1745 gfc_association_list
* newAssoc
= gfc_get_association_list ();
1746 gfc_association_list
* a
;
1748 /* Match the next association. */
1749 if (gfc_match (" %n => %e", newAssoc
->name
, &newAssoc
->target
)
1752 gfc_error ("Expected association at %C");
1753 goto assocListError
;
1755 newAssoc
->where
= gfc_current_locus
;
1757 /* Check that the current name is not yet in the list. */
1758 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
1759 if (!strcmp (a
->name
, newAssoc
->name
))
1761 gfc_error ("Duplicate name %qs in association at %C",
1763 goto assocListError
;
1766 /* The target expression must not be coindexed. */
1767 if (gfc_is_coindexed (newAssoc
->target
))
1769 gfc_error ("Association target at %C must not be coindexed");
1770 goto assocListError
;
1773 /* The `variable' field is left blank for now; because the target is not
1774 yet resolved, we can't use gfc_has_vector_subscript to determine it
1775 for now. This is set during resolution. */
1777 /* Put it into the list. */
1778 newAssoc
->next
= new_st
.ext
.block
.assoc
;
1779 new_st
.ext
.block
.assoc
= newAssoc
;
1781 /* Try next one or end if closing parenthesis is found. */
1782 gfc_gobble_whitespace ();
1783 if (gfc_peek_char () == ')')
1785 if (gfc_match_char (',') != MATCH_YES
)
1787 gfc_error ("Expected %<)%> or %<,%> at %C");
1797 if (gfc_match_char (')') != MATCH_YES
)
1799 /* This should never happen as we peek above. */
1803 if (gfc_match_eos () != MATCH_YES
)
1805 gfc_error ("Junk after ASSOCIATE statement at %C");
1812 gfc_free_association_list (new_st
.ext
.block
.assoc
);
1817 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1818 an accessible derived type. */
1821 match_derived_type_spec (gfc_typespec
*ts
)
1823 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1825 gfc_symbol
*derived
;
1827 old_locus
= gfc_current_locus
;
1829 if (gfc_match ("%n", name
) != MATCH_YES
)
1831 gfc_current_locus
= old_locus
;
1835 gfc_find_symbol (name
, NULL
, 1, &derived
);
1837 if (derived
&& derived
->attr
.flavor
== FL_PROCEDURE
&& derived
->attr
.generic
)
1838 derived
= gfc_find_dt_in_generic (derived
);
1840 if (derived
&& derived
->attr
.flavor
== FL_DERIVED
)
1842 ts
->type
= BT_DERIVED
;
1843 ts
->u
.derived
= derived
;
1847 gfc_current_locus
= old_locus
;
1852 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
1853 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
1854 It only includes the intrinsic types from the Fortran 2003 standard
1855 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
1856 the implicit_flag is not needed, so it was removed. Derived types are
1857 identified by their name alone. */
1860 gfc_match_type_spec (gfc_typespec
*ts
)
1866 gfc_gobble_whitespace ();
1867 old_locus
= gfc_current_locus
;
1869 if (match_derived_type_spec (ts
) == MATCH_YES
)
1871 /* Enforce F03:C401. */
1872 if (ts
->u
.derived
->attr
.abstract
)
1874 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
1875 ts
->u
.derived
->name
, &old_locus
);
1881 if (gfc_match ("integer") == MATCH_YES
)
1883 ts
->type
= BT_INTEGER
;
1884 ts
->kind
= gfc_default_integer_kind
;
1888 if (gfc_match ("real") == MATCH_YES
)
1891 ts
->kind
= gfc_default_real_kind
;
1895 if (gfc_match ("double precision") == MATCH_YES
)
1898 ts
->kind
= gfc_default_double_kind
;
1902 if (gfc_match ("complex") == MATCH_YES
)
1904 ts
->type
= BT_COMPLEX
;
1905 ts
->kind
= gfc_default_complex_kind
;
1909 if (gfc_match ("character") == MATCH_YES
)
1911 ts
->type
= BT_CHARACTER
;
1913 m
= gfc_match_char_spec (ts
);
1921 if (gfc_match ("logical") == MATCH_YES
)
1923 ts
->type
= BT_LOGICAL
;
1924 ts
->kind
= gfc_default_logical_kind
;
1928 /* If a type is not matched, simply return MATCH_NO. */
1929 gfc_current_locus
= old_locus
;
1934 gfc_gobble_whitespace ();
1935 if (gfc_peek_ascii_char () == '*')
1937 gfc_error ("Invalid type-spec at %C");
1941 m
= gfc_match_kind_spec (ts
, false);
1944 m
= MATCH_YES
; /* No kind specifier found. */
1950 /******************** FORALL subroutines ********************/
1952 /* Free a list of FORALL iterators. */
1955 gfc_free_forall_iterator (gfc_forall_iterator
*iter
)
1957 gfc_forall_iterator
*next
;
1962 gfc_free_expr (iter
->var
);
1963 gfc_free_expr (iter
->start
);
1964 gfc_free_expr (iter
->end
);
1965 gfc_free_expr (iter
->stride
);
1972 /* Match an iterator as part of a FORALL statement. The format is:
1974 <var> = <start>:<end>[:<stride>]
1976 On MATCH_NO, the caller tests for the possibility that there is a
1977 scalar mask expression. */
1980 match_forall_iterator (gfc_forall_iterator
**result
)
1982 gfc_forall_iterator
*iter
;
1986 where
= gfc_current_locus
;
1987 iter
= XCNEW (gfc_forall_iterator
);
1989 m
= gfc_match_expr (&iter
->var
);
1993 if (gfc_match_char ('=') != MATCH_YES
1994 || iter
->var
->expr_type
!= EXPR_VARIABLE
)
2000 m
= gfc_match_expr (&iter
->start
);
2004 if (gfc_match_char (':') != MATCH_YES
)
2007 m
= gfc_match_expr (&iter
->end
);
2010 if (m
== MATCH_ERROR
)
2013 if (gfc_match_char (':') == MATCH_NO
)
2014 iter
->stride
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2017 m
= gfc_match_expr (&iter
->stride
);
2020 if (m
== MATCH_ERROR
)
2024 /* Mark the iteration variable's symbol as used as a FORALL index. */
2025 iter
->var
->symtree
->n
.sym
->forall_index
= true;
2031 gfc_error ("Syntax error in FORALL iterator at %C");
2036 gfc_current_locus
= where
;
2037 gfc_free_forall_iterator (iter
);
2042 /* Match the header of a FORALL statement. */
2045 match_forall_header (gfc_forall_iterator
**phead
, gfc_expr
**mask
)
2047 gfc_forall_iterator
*head
, *tail
, *new_iter
;
2051 gfc_gobble_whitespace ();
2056 if (gfc_match_char ('(') != MATCH_YES
)
2059 m
= match_forall_iterator (&new_iter
);
2060 if (m
== MATCH_ERROR
)
2065 head
= tail
= new_iter
;
2069 if (gfc_match_char (',') != MATCH_YES
)
2072 m
= match_forall_iterator (&new_iter
);
2073 if (m
== MATCH_ERROR
)
2078 tail
->next
= new_iter
;
2083 /* Have to have a mask expression. */
2085 m
= gfc_match_expr (&msk
);
2088 if (m
== MATCH_ERROR
)
2094 if (gfc_match_char (')') == MATCH_NO
)
2102 gfc_syntax_error (ST_FORALL
);
2105 gfc_free_expr (msk
);
2106 gfc_free_forall_iterator (head
);
2111 /* Match the rest of a simple FORALL statement that follows an
2115 match_simple_forall (void)
2117 gfc_forall_iterator
*head
;
2126 m
= match_forall_header (&head
, &mask
);
2133 m
= gfc_match_assignment ();
2135 if (m
== MATCH_ERROR
)
2139 m
= gfc_match_pointer_assignment ();
2140 if (m
== MATCH_ERROR
)
2146 c
= XCNEW (gfc_code
);
2148 c
->loc
= gfc_current_locus
;
2150 if (gfc_match_eos () != MATCH_YES
)
2153 gfc_clear_new_st ();
2154 new_st
.op
= EXEC_FORALL
;
2155 new_st
.expr1
= mask
;
2156 new_st
.ext
.forall_iterator
= head
;
2157 new_st
.block
= gfc_get_code (EXEC_FORALL
);
2158 new_st
.block
->next
= c
;
2163 gfc_syntax_error (ST_FORALL
);
2166 gfc_free_forall_iterator (head
);
2167 gfc_free_expr (mask
);
2173 /* Match a FORALL statement. */
2176 gfc_match_forall (gfc_statement
*st
)
2178 gfc_forall_iterator
*head
;
2187 m0
= gfc_match_label ();
2188 if (m0
== MATCH_ERROR
)
2191 m
= gfc_match (" forall");
2195 m
= match_forall_header (&head
, &mask
);
2196 if (m
== MATCH_ERROR
)
2201 if (gfc_match_eos () == MATCH_YES
)
2203 *st
= ST_FORALL_BLOCK
;
2204 new_st
.op
= EXEC_FORALL
;
2205 new_st
.expr1
= mask
;
2206 new_st
.ext
.forall_iterator
= head
;
2210 m
= gfc_match_assignment ();
2211 if (m
== MATCH_ERROR
)
2215 m
= gfc_match_pointer_assignment ();
2216 if (m
== MATCH_ERROR
)
2222 c
= XCNEW (gfc_code
);
2224 c
->loc
= gfc_current_locus
;
2226 gfc_clear_new_st ();
2227 new_st
.op
= EXEC_FORALL
;
2228 new_st
.expr1
= mask
;
2229 new_st
.ext
.forall_iterator
= head
;
2230 new_st
.block
= gfc_get_code (EXEC_FORALL
);
2231 new_st
.block
->next
= c
;
2237 gfc_syntax_error (ST_FORALL
);
2240 gfc_free_forall_iterator (head
);
2241 gfc_free_expr (mask
);
2242 gfc_free_statements (c
);
2247 /* Match a DO statement. */
2252 gfc_iterator iter
, *ip
;
2254 gfc_st_label
*label
;
2257 old_loc
= gfc_current_locus
;
2260 iter
.var
= iter
.start
= iter
.end
= iter
.step
= NULL
;
2262 m
= gfc_match_label ();
2263 if (m
== MATCH_ERROR
)
2266 if (gfc_match (" do") != MATCH_YES
)
2269 m
= gfc_match_st_label (&label
);
2270 if (m
== MATCH_ERROR
)
2273 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2275 if (gfc_match_eos () == MATCH_YES
)
2277 iter
.end
= gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, true);
2278 new_st
.op
= EXEC_DO_WHILE
;
2282 /* Match an optional comma, if no comma is found, a space is obligatory. */
2283 if (gfc_match_char (',') != MATCH_YES
&& gfc_match ("% ") != MATCH_YES
)
2286 /* Check for balanced parens. */
2288 if (gfc_match_parens () == MATCH_ERROR
)
2291 if (gfc_match (" concurrent") == MATCH_YES
)
2293 gfc_forall_iterator
*head
;
2296 if (!gfc_notify_std (GFC_STD_F2008
, "DO CONCURRENT construct at %C"))
2302 m
= match_forall_header (&head
, &mask
);
2306 if (m
== MATCH_ERROR
)
2307 goto concurr_cleanup
;
2309 if (gfc_match_eos () != MATCH_YES
)
2310 goto concurr_cleanup
;
2313 && !gfc_reference_st_label (label
, ST_LABEL_DO_TARGET
))
2314 goto concurr_cleanup
;
2316 new_st
.label1
= label
;
2317 new_st
.op
= EXEC_DO_CONCURRENT
;
2318 new_st
.expr1
= mask
;
2319 new_st
.ext
.forall_iterator
= head
;
2324 gfc_syntax_error (ST_DO
);
2325 gfc_free_expr (mask
);
2326 gfc_free_forall_iterator (head
);
2330 /* See if we have a DO WHILE. */
2331 if (gfc_match (" while ( %e )%t", &iter
.end
) == MATCH_YES
)
2333 new_st
.op
= EXEC_DO_WHILE
;
2337 /* The abortive DO WHILE may have done something to the symbol
2338 table, so we start over. */
2339 gfc_undo_symbols ();
2340 gfc_current_locus
= old_loc
;
2342 gfc_match_label (); /* This won't error. */
2343 gfc_match (" do "); /* This will work. */
2345 gfc_match_st_label (&label
); /* Can't error out. */
2346 gfc_match_char (','); /* Optional comma. */
2348 m
= gfc_match_iterator (&iter
, 0);
2351 if (m
== MATCH_ERROR
)
2354 iter
.var
->symtree
->n
.sym
->attr
.implied_index
= 0;
2355 gfc_check_do_variable (iter
.var
->symtree
);
2357 if (gfc_match_eos () != MATCH_YES
)
2359 gfc_syntax_error (ST_DO
);
2363 new_st
.op
= EXEC_DO
;
2367 && !gfc_reference_st_label (label
, ST_LABEL_DO_TARGET
))
2370 new_st
.label1
= label
;
2372 if (new_st
.op
== EXEC_DO_WHILE
)
2373 new_st
.expr1
= iter
.end
;
2376 new_st
.ext
.iterator
= ip
= gfc_get_iterator ();
2383 gfc_free_iterator (&iter
, 0);
2389 /* Match an EXIT or CYCLE statement. */
2392 match_exit_cycle (gfc_statement st
, gfc_exec_op op
)
2394 gfc_state_data
*p
, *o
;
2399 if (gfc_match_eos () == MATCH_YES
)
2403 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2406 m
= gfc_match ("% %n%t", name
);
2407 if (m
== MATCH_ERROR
)
2411 gfc_syntax_error (st
);
2415 /* Find the corresponding symbol. If there's a BLOCK statement
2416 between here and the label, it is not in gfc_current_ns but a parent
2418 stree
= gfc_find_symtree_in_proc (name
, gfc_current_ns
);
2421 gfc_error ("Name %qs in %s statement at %C is unknown",
2422 name
, gfc_ascii_statement (st
));
2427 if (sym
->attr
.flavor
!= FL_LABEL
)
2429 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2430 name
, gfc_ascii_statement (st
));
2435 /* Find the loop specified by the label (or lack of a label). */
2436 for (o
= NULL
, p
= gfc_state_stack
; p
; p
= p
->previous
)
2437 if (o
== NULL
&& p
->state
== COMP_OMP_STRUCTURED_BLOCK
)
2439 else if (p
->state
== COMP_CRITICAL
)
2441 gfc_error("%s statement at %C leaves CRITICAL construct",
2442 gfc_ascii_statement (st
));
2445 else if (p
->state
== COMP_DO_CONCURRENT
2446 && (op
== EXEC_EXIT
|| (sym
&& sym
!= p
->sym
)))
2448 /* F2008, C821 & C845. */
2449 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2450 gfc_ascii_statement (st
));
2453 else if ((sym
&& sym
== p
->sym
)
2454 || (!sym
&& (p
->state
== COMP_DO
2455 || p
->state
== COMP_DO_CONCURRENT
)))
2461 gfc_error ("%s statement at %C is not within a construct",
2462 gfc_ascii_statement (st
));
2464 gfc_error ("%s statement at %C is not within construct %qs",
2465 gfc_ascii_statement (st
), sym
->name
);
2470 /* Special checks for EXIT from non-loop constructs. */
2474 case COMP_DO_CONCURRENT
:
2478 /* This is already handled above. */
2481 case COMP_ASSOCIATE
:
2485 case COMP_SELECT_TYPE
:
2487 if (op
== EXEC_CYCLE
)
2489 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2490 " construct %qs", sym
->name
);
2493 gcc_assert (op
== EXEC_EXIT
);
2494 if (!gfc_notify_std (GFC_STD_F2008
, "EXIT statement with no"
2495 " do-construct-name at %C"))
2500 gfc_error ("%s statement at %C is not applicable to construct %qs",
2501 gfc_ascii_statement (st
), sym
->name
);
2507 gfc_error (is_oacc (p
)
2508 ? "%s statement at %C leaving OpenACC structured block"
2509 : "%s statement at %C leaving OpenMP structured block",
2510 gfc_ascii_statement (st
));
2514 for (o
= p
, cnt
= 0; o
->state
== COMP_DO
&& o
->previous
!= NULL
; cnt
++)
2518 && o
->state
== COMP_OMP_STRUCTURED_BLOCK
2519 && (o
->head
->op
== EXEC_OACC_LOOP
2520 || o
->head
->op
== EXEC_OACC_PARALLEL_LOOP
))
2523 gcc_assert (o
->head
->next
!= NULL
2524 && (o
->head
->next
->op
== EXEC_DO
2525 || o
->head
->next
->op
== EXEC_DO_WHILE
)
2526 && o
->previous
!= NULL
2527 && o
->previous
->tail
->op
== o
->head
->op
);
2528 if (o
->previous
->tail
->ext
.omp_clauses
!= NULL
2529 && o
->previous
->tail
->ext
.omp_clauses
->collapse
> 1)
2530 collapse
= o
->previous
->tail
->ext
.omp_clauses
->collapse
;
2531 if (st
== ST_EXIT
&& cnt
<= collapse
)
2533 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2536 if (st
== ST_CYCLE
&& cnt
< collapse
)
2538 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2539 " !$ACC LOOP loop");
2545 && (o
->state
== COMP_OMP_STRUCTURED_BLOCK
)
2546 && (o
->head
->op
== EXEC_OMP_DO
2547 || o
->head
->op
== EXEC_OMP_PARALLEL_DO
2548 || o
->head
->op
== EXEC_OMP_SIMD
2549 || o
->head
->op
== EXEC_OMP_DO_SIMD
2550 || o
->head
->op
== EXEC_OMP_PARALLEL_DO_SIMD
))
2553 gcc_assert (o
->head
->next
!= NULL
2554 && (o
->head
->next
->op
== EXEC_DO
2555 || o
->head
->next
->op
== EXEC_DO_WHILE
)
2556 && o
->previous
!= NULL
2557 && o
->previous
->tail
->op
== o
->head
->op
);
2558 if (o
->previous
->tail
->ext
.omp_clauses
!= NULL
2559 && o
->previous
->tail
->ext
.omp_clauses
->collapse
> 1)
2560 collapse
= o
->previous
->tail
->ext
.omp_clauses
->collapse
;
2561 if (st
== ST_EXIT
&& cnt
<= collapse
)
2563 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2566 if (st
== ST_CYCLE
&& cnt
< collapse
)
2568 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2574 /* Save the first statement in the construct - needed by the backend. */
2575 new_st
.ext
.which_construct
= p
->construct
;
2583 /* Match the EXIT statement. */
2586 gfc_match_exit (void)
2588 return match_exit_cycle (ST_EXIT
, EXEC_EXIT
);
2592 /* Match the CYCLE statement. */
2595 gfc_match_cycle (void)
2597 return match_exit_cycle (ST_CYCLE
, EXEC_CYCLE
);
2601 /* Match a number or character constant after an (ERROR) STOP or PAUSE
2605 gfc_match_stopcode (gfc_statement st
)
2612 if (gfc_match_eos () != MATCH_YES
)
2614 m
= gfc_match_init_expr (&e
);
2615 if (m
== MATCH_ERROR
)
2620 if (gfc_match_eos () != MATCH_YES
)
2624 if (gfc_pure (NULL
))
2626 if (st
== ST_ERROR_STOP
)
2628 if (!gfc_notify_std (GFC_STD_F2015
, "%s statement at %C in PURE "
2629 "procedure", gfc_ascii_statement (st
)))
2634 gfc_error ("%s statement not allowed in PURE procedure at %C",
2635 gfc_ascii_statement (st
));
2640 gfc_unset_implicit_pure (NULL
);
2642 if (st
== ST_STOP
&& gfc_find_state (COMP_CRITICAL
))
2644 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2647 if (st
== ST_STOP
&& gfc_find_state (COMP_DO_CONCURRENT
))
2649 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
2655 if (!(e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_INTEGER
))
2657 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2664 gfc_error ("STOP code at %L must be scalar",
2669 if (e
->ts
.type
== BT_CHARACTER
2670 && e
->ts
.kind
!= gfc_default_character_kind
)
2672 gfc_error ("STOP code at %L must be default character KIND=%d",
2673 &e
->where
, (int) gfc_default_character_kind
);
2677 if (e
->ts
.type
== BT_INTEGER
2678 && e
->ts
.kind
!= gfc_default_integer_kind
)
2680 gfc_error ("STOP code at %L must be default integer KIND=%d",
2681 &e
->where
, (int) gfc_default_integer_kind
);
2689 new_st
.op
= EXEC_STOP
;
2692 new_st
.op
= EXEC_ERROR_STOP
;
2695 new_st
.op
= EXEC_PAUSE
;
2702 new_st
.ext
.stop_code
= -1;
2707 gfc_syntax_error (st
);
2716 /* Match the (deprecated) PAUSE statement. */
2719 gfc_match_pause (void)
2723 m
= gfc_match_stopcode (ST_PAUSE
);
2726 if (!gfc_notify_std (GFC_STD_F95_DEL
, "PAUSE statement at %C"))
2733 /* Match the STOP statement. */
2736 gfc_match_stop (void)
2738 return gfc_match_stopcode (ST_STOP
);
2742 /* Match the ERROR STOP statement. */
2745 gfc_match_error_stop (void)
2747 if (!gfc_notify_std (GFC_STD_F2008
, "ERROR STOP statement at %C"))
2750 return gfc_match_stopcode (ST_ERROR_STOP
);
2754 /* Match LOCK/UNLOCK statement. Syntax:
2755 LOCK ( lock-variable [ , lock-stat-list ] )
2756 UNLOCK ( lock-variable [ , sync-stat-list ] )
2757 where lock-stat is ACQUIRED_LOCK or sync-stat
2758 and sync-stat is STAT= or ERRMSG=. */
2761 lock_unlock_statement (gfc_statement st
)
2764 gfc_expr
*tmp
, *lockvar
, *acq_lock
, *stat
, *errmsg
;
2765 bool saw_acq_lock
, saw_stat
, saw_errmsg
;
2767 tmp
= lockvar
= acq_lock
= stat
= errmsg
= NULL
;
2768 saw_acq_lock
= saw_stat
= saw_errmsg
= false;
2770 if (gfc_pure (NULL
))
2772 gfc_error ("Image control statement %s at %C in PURE procedure",
2773 st
== ST_LOCK
? "LOCK" : "UNLOCK");
2777 gfc_unset_implicit_pure (NULL
);
2779 if (flag_coarray
== GFC_FCOARRAY_NONE
)
2781 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2785 if (gfc_find_state (COMP_CRITICAL
))
2787 gfc_error ("Image control statement %s at %C in CRITICAL block",
2788 st
== ST_LOCK
? "LOCK" : "UNLOCK");
2792 if (gfc_find_state (COMP_DO_CONCURRENT
))
2794 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
2795 st
== ST_LOCK
? "LOCK" : "UNLOCK");
2799 if (gfc_match_char ('(') != MATCH_YES
)
2802 if (gfc_match ("%e", &lockvar
) != MATCH_YES
)
2804 m
= gfc_match_char (',');
2805 if (m
== MATCH_ERROR
)
2809 m
= gfc_match_char (')');
2817 m
= gfc_match (" stat = %v", &tmp
);
2818 if (m
== MATCH_ERROR
)
2824 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
2830 m
= gfc_match_char (',');
2838 m
= gfc_match (" errmsg = %v", &tmp
);
2839 if (m
== MATCH_ERROR
)
2845 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
2851 m
= gfc_match_char (',');
2859 m
= gfc_match (" acquired_lock = %v", &tmp
);
2860 if (m
== MATCH_ERROR
|| st
== ST_UNLOCK
)
2866 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L ",
2871 saw_acq_lock
= true;
2873 m
= gfc_match_char (',');
2884 if (m
== MATCH_ERROR
)
2887 if (gfc_match (" )%t") != MATCH_YES
)
2894 new_st
.op
= EXEC_LOCK
;
2897 new_st
.op
= EXEC_UNLOCK
;
2903 new_st
.expr1
= lockvar
;
2904 new_st
.expr2
= stat
;
2905 new_st
.expr3
= errmsg
;
2906 new_st
.expr4
= acq_lock
;
2911 gfc_syntax_error (st
);
2914 if (acq_lock
!= tmp
)
2915 gfc_free_expr (acq_lock
);
2917 gfc_free_expr (errmsg
);
2919 gfc_free_expr (stat
);
2921 gfc_free_expr (tmp
);
2922 gfc_free_expr (lockvar
);
2929 gfc_match_lock (void)
2931 if (!gfc_notify_std (GFC_STD_F2008
, "LOCK statement at %C"))
2934 return lock_unlock_statement (ST_LOCK
);
2939 gfc_match_unlock (void)
2941 if (!gfc_notify_std (GFC_STD_F2008
, "UNLOCK statement at %C"))
2944 return lock_unlock_statement (ST_UNLOCK
);
2948 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
2949 SYNC ALL [(sync-stat-list)]
2950 SYNC MEMORY [(sync-stat-list)]
2951 SYNC IMAGES (image-set [, sync-stat-list] )
2952 with sync-stat is int-expr or *. */
2955 sync_statement (gfc_statement st
)
2958 gfc_expr
*tmp
, *imageset
, *stat
, *errmsg
;
2959 bool saw_stat
, saw_errmsg
;
2961 tmp
= imageset
= stat
= errmsg
= NULL
;
2962 saw_stat
= saw_errmsg
= false;
2964 if (gfc_pure (NULL
))
2966 gfc_error ("Image control statement SYNC at %C in PURE procedure");
2970 gfc_unset_implicit_pure (NULL
);
2972 if (!gfc_notify_std (GFC_STD_F2008
, "SYNC statement at %C"))
2975 if (flag_coarray
== GFC_FCOARRAY_NONE
)
2977 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
2982 if (gfc_find_state (COMP_CRITICAL
))
2984 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
2988 if (gfc_find_state (COMP_DO_CONCURRENT
))
2990 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
2994 if (gfc_match_eos () == MATCH_YES
)
2996 if (st
== ST_SYNC_IMAGES
)
3001 if (gfc_match_char ('(') != MATCH_YES
)
3004 if (st
== ST_SYNC_IMAGES
)
3006 /* Denote '*' as imageset == NULL. */
3007 m
= gfc_match_char ('*');
3008 if (m
== MATCH_ERROR
)
3012 if (gfc_match ("%e", &imageset
) != MATCH_YES
)
3015 m
= gfc_match_char (',');
3016 if (m
== MATCH_ERROR
)
3020 m
= gfc_match_char (')');
3029 m
= gfc_match (" stat = %v", &tmp
);
3030 if (m
== MATCH_ERROR
)
3036 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
3042 if (gfc_match_char (',') == MATCH_YES
)
3049 m
= gfc_match (" errmsg = %v", &tmp
);
3050 if (m
== MATCH_ERROR
)
3056 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
3062 if (gfc_match_char (',') == MATCH_YES
)
3072 if (gfc_match (" )%t") != MATCH_YES
)
3079 new_st
.op
= EXEC_SYNC_ALL
;
3081 case ST_SYNC_IMAGES
:
3082 new_st
.op
= EXEC_SYNC_IMAGES
;
3084 case ST_SYNC_MEMORY
:
3085 new_st
.op
= EXEC_SYNC_MEMORY
;
3091 new_st
.expr1
= imageset
;
3092 new_st
.expr2
= stat
;
3093 new_st
.expr3
= errmsg
;
3098 gfc_syntax_error (st
);
3102 gfc_free_expr (stat
);
3104 gfc_free_expr (errmsg
);
3106 gfc_free_expr (tmp
);
3107 gfc_free_expr (imageset
);
3113 /* Match SYNC ALL statement. */
3116 gfc_match_sync_all (void)
3118 return sync_statement (ST_SYNC_ALL
);
3122 /* Match SYNC IMAGES statement. */
3125 gfc_match_sync_images (void)
3127 return sync_statement (ST_SYNC_IMAGES
);
3131 /* Match SYNC MEMORY statement. */
3134 gfc_match_sync_memory (void)
3136 return sync_statement (ST_SYNC_MEMORY
);
3140 /* Match a CONTINUE statement. */
3143 gfc_match_continue (void)
3145 if (gfc_match_eos () != MATCH_YES
)
3147 gfc_syntax_error (ST_CONTINUE
);
3151 new_st
.op
= EXEC_CONTINUE
;
3156 /* Match the (deprecated) ASSIGN statement. */
3159 gfc_match_assign (void)
3162 gfc_st_label
*label
;
3164 if (gfc_match (" %l", &label
) == MATCH_YES
)
3166 if (!gfc_reference_st_label (label
, ST_LABEL_UNKNOWN
))
3168 if (gfc_match (" to %v%t", &expr
) == MATCH_YES
)
3170 if (!gfc_notify_std (GFC_STD_F95_DEL
, "ASSIGN statement at %C"))
3173 expr
->symtree
->n
.sym
->attr
.assign
= 1;
3175 new_st
.op
= EXEC_LABEL_ASSIGN
;
3176 new_st
.label1
= label
;
3177 new_st
.expr1
= expr
;
3185 /* Match the GO TO statement. As a computed GOTO statement is
3186 matched, it is transformed into an equivalent SELECT block. No
3187 tree is necessary, and the resulting jumps-to-jumps are
3188 specifically optimized away by the back end. */
3191 gfc_match_goto (void)
3193 gfc_code
*head
, *tail
;
3196 gfc_st_label
*label
;
3200 if (gfc_match (" %l%t", &label
) == MATCH_YES
)
3202 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
3205 new_st
.op
= EXEC_GOTO
;
3206 new_st
.label1
= label
;
3210 /* The assigned GO TO statement. */
3212 if (gfc_match_variable (&expr
, 0) == MATCH_YES
)
3214 if (!gfc_notify_std (GFC_STD_F95_DEL
, "Assigned GOTO statement at %C"))
3217 new_st
.op
= EXEC_GOTO
;
3218 new_st
.expr1
= expr
;
3220 if (gfc_match_eos () == MATCH_YES
)
3223 /* Match label list. */
3224 gfc_match_char (',');
3225 if (gfc_match_char ('(') != MATCH_YES
)
3227 gfc_syntax_error (ST_GOTO
);
3234 m
= gfc_match_st_label (&label
);
3238 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
3242 head
= tail
= gfc_get_code (EXEC_GOTO
);
3245 tail
->block
= gfc_get_code (EXEC_GOTO
);
3249 tail
->label1
= label
;
3251 while (gfc_match_char (',') == MATCH_YES
);
3253 if (gfc_match (")%t") != MATCH_YES
)
3258 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3261 new_st
.block
= head
;
3266 /* Last chance is a computed GO TO statement. */
3267 if (gfc_match_char ('(') != MATCH_YES
)
3269 gfc_syntax_error (ST_GOTO
);
3278 m
= gfc_match_st_label (&label
);
3282 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
3286 head
= tail
= gfc_get_code (EXEC_SELECT
);
3289 tail
->block
= gfc_get_code (EXEC_SELECT
);
3293 cp
= gfc_get_case ();
3294 cp
->low
= cp
->high
= gfc_get_int_expr (gfc_default_integer_kind
,
3297 tail
->ext
.block
.case_list
= cp
;
3299 tail
->next
= gfc_get_code (EXEC_GOTO
);
3300 tail
->next
->label1
= label
;
3302 while (gfc_match_char (',') == MATCH_YES
);
3304 if (gfc_match_char (')') != MATCH_YES
)
3309 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3313 /* Get the rest of the statement. */
3314 gfc_match_char (',');
3316 if (gfc_match (" %e%t", &expr
) != MATCH_YES
)
3319 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Computed GOTO at %C"))
3322 /* At this point, a computed GOTO has been fully matched and an
3323 equivalent SELECT statement constructed. */
3325 new_st
.op
= EXEC_SELECT
;
3326 new_st
.expr1
= NULL
;
3328 /* Hack: For a "real" SELECT, the expression is in expr. We put
3329 it in expr2 so we can distinguish then and produce the correct
3331 new_st
.expr2
= expr
;
3332 new_st
.block
= head
;
3336 gfc_syntax_error (ST_GOTO
);
3338 gfc_free_statements (head
);
3343 /* Frees a list of gfc_alloc structures. */
3346 gfc_free_alloc_list (gfc_alloc
*p
)
3353 gfc_free_expr (p
->expr
);
3359 /* Match an ALLOCATE statement. */
3362 gfc_match_allocate (void)
3364 gfc_alloc
*head
, *tail
;
3365 gfc_expr
*stat
, *errmsg
, *tmp
, *source
, *mold
;
3369 locus old_locus
, deferred_locus
;
3370 bool saw_stat
, saw_errmsg
, saw_source
, saw_mold
, saw_deferred
, b1
, b2
, b3
;
3371 bool saw_unlimited
= false;
3374 stat
= errmsg
= source
= mold
= tmp
= NULL
;
3375 saw_stat
= saw_errmsg
= saw_source
= saw_mold
= saw_deferred
= false;
3377 if (gfc_match_char ('(') != MATCH_YES
)
3380 /* Match an optional type-spec. */
3381 old_locus
= gfc_current_locus
;
3382 m
= gfc_match_type_spec (&ts
);
3383 if (m
== MATCH_ERROR
)
3385 else if (m
== MATCH_NO
)
3387 char name
[GFC_MAX_SYMBOL_LEN
+ 3];
3389 if (gfc_match ("%n :: ", name
) == MATCH_YES
)
3391 gfc_error ("Error in type-spec at %L", &old_locus
);
3395 ts
.type
= BT_UNKNOWN
;
3399 if (gfc_match (" :: ") == MATCH_YES
)
3401 if (!gfc_notify_std (GFC_STD_F2003
, "typespec in ALLOCATE at %L",
3407 gfc_error ("Type-spec at %L cannot contain a deferred "
3408 "type parameter", &old_locus
);
3412 if (ts
.type
== BT_CHARACTER
)
3413 ts
.u
.cl
->length_from_typespec
= true;
3417 ts
.type
= BT_UNKNOWN
;
3418 gfc_current_locus
= old_locus
;
3425 head
= tail
= gfc_get_alloc ();
3428 tail
->next
= gfc_get_alloc ();
3432 m
= gfc_match_variable (&tail
->expr
, 0);
3435 if (m
== MATCH_ERROR
)
3438 if (gfc_check_do_variable (tail
->expr
->symtree
))
3441 bool impure
= gfc_impure_variable (tail
->expr
->symtree
->n
.sym
);
3442 if (impure
&& gfc_pure (NULL
))
3444 gfc_error ("Bad allocate-object at %C for a PURE procedure");
3449 gfc_unset_implicit_pure (NULL
);
3451 if (tail
->expr
->ts
.deferred
)
3453 saw_deferred
= true;
3454 deferred_locus
= tail
->expr
->where
;
3457 if (gfc_find_state (COMP_DO_CONCURRENT
)
3458 || gfc_find_state (COMP_CRITICAL
))
3461 bool coarray
= tail
->expr
->symtree
->n
.sym
->attr
.codimension
;
3462 for (ref
= tail
->expr
->ref
; ref
; ref
= ref
->next
)
3463 if (ref
->type
== REF_COMPONENT
)
3464 coarray
= ref
->u
.c
.component
->attr
.codimension
;
3466 if (coarray
&& gfc_find_state (COMP_DO_CONCURRENT
))
3468 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
3471 if (coarray
&& gfc_find_state (COMP_CRITICAL
))
3473 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
3478 /* Check for F08:C628. */
3479 sym
= tail
->expr
->symtree
->n
.sym
;
3480 b1
= !(tail
->expr
->ref
3481 && (tail
->expr
->ref
->type
== REF_COMPONENT
3482 || tail
->expr
->ref
->type
== REF_ARRAY
));
3483 if (sym
&& sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
3484 b2
= !(CLASS_DATA (sym
)->attr
.allocatable
3485 || CLASS_DATA (sym
)->attr
.class_pointer
);
3487 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
3488 || sym
->attr
.proc_pointer
);
3489 b3
= sym
&& sym
->ns
&& sym
->ns
->proc_name
3490 && (sym
->ns
->proc_name
->attr
.allocatable
3491 || sym
->ns
->proc_name
->attr
.pointer
3492 || sym
->ns
->proc_name
->attr
.proc_pointer
);
3493 if (b1
&& b2
&& !b3
)
3495 gfc_error ("Allocate-object at %L is neither a data pointer "
3496 "nor an allocatable variable", &tail
->expr
->where
);
3500 /* The ALLOCATE statement had an optional typespec. Check the
3502 if (ts
.type
!= BT_UNKNOWN
)
3504 /* Enforce F03:C624. */
3505 if (!gfc_type_compatible (&tail
->expr
->ts
, &ts
))
3507 gfc_error ("Type of entity at %L is type incompatible with "
3508 "typespec", &tail
->expr
->where
);
3512 /* Enforce F03:C627. */
3513 if (ts
.kind
!= tail
->expr
->ts
.kind
&& !UNLIMITED_POLY (tail
->expr
))
3515 gfc_error ("Kind type parameter for entity at %L differs from "
3516 "the kind type parameter of the typespec",
3517 &tail
->expr
->where
);
3522 if (tail
->expr
->ts
.type
== BT_DERIVED
)
3523 tail
->expr
->ts
.u
.derived
= gfc_use_derived (tail
->expr
->ts
.u
.derived
);
3525 saw_unlimited
= saw_unlimited
| UNLIMITED_POLY (tail
->expr
);
3527 if (gfc_peek_ascii_char () == '(' && !sym
->attr
.dimension
)
3529 gfc_error ("Shape specification for allocatable scalar at %C");
3533 if (gfc_match_char (',') != MATCH_YES
)
3538 m
= gfc_match (" stat = %v", &tmp
);
3539 if (m
== MATCH_ERROR
)
3546 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
3554 if (gfc_check_do_variable (stat
->symtree
))
3557 if (gfc_match_char (',') == MATCH_YES
)
3558 goto alloc_opt_list
;
3561 m
= gfc_match (" errmsg = %v", &tmp
);
3562 if (m
== MATCH_ERROR
)
3566 if (!gfc_notify_std (GFC_STD_F2003
, "ERRMSG tag at %L", &tmp
->where
))
3572 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
3580 if (gfc_match_char (',') == MATCH_YES
)
3581 goto alloc_opt_list
;
3584 m
= gfc_match (" source = %e", &tmp
);
3585 if (m
== MATCH_ERROR
)
3589 if (!gfc_notify_std (GFC_STD_F2003
, "SOURCE tag at %L", &tmp
->where
))
3595 gfc_error ("Redundant SOURCE tag found at %L ", &tmp
->where
);
3599 /* The next 2 conditionals check C631. */
3600 if (ts
.type
!= BT_UNKNOWN
)
3602 gfc_error_1 ("SOURCE tag at %L conflicts with the typespec at %L",
3603 &tmp
->where
, &old_locus
);
3608 && !gfc_notify_std (GFC_STD_F2008
, "SOURCE tag at %L"
3609 " with more than a single allocate object",
3617 if (gfc_match_char (',') == MATCH_YES
)
3618 goto alloc_opt_list
;
3621 m
= gfc_match (" mold = %e", &tmp
);
3622 if (m
== MATCH_ERROR
)
3626 if (!gfc_notify_std (GFC_STD_F2008
, "MOLD tag at %L", &tmp
->where
))
3629 /* Check F08:C636. */
3632 gfc_error ("Redundant MOLD tag found at %L ", &tmp
->where
);
3636 /* Check F08:C637. */
3637 if (ts
.type
!= BT_UNKNOWN
)
3639 gfc_error_1 ("MOLD tag at %L conflicts with the typespec at %L",
3640 &tmp
->where
, &old_locus
);
3649 if (gfc_match_char (',') == MATCH_YES
)
3650 goto alloc_opt_list
;
3653 gfc_gobble_whitespace ();
3655 if (gfc_peek_char () == ')')
3659 if (gfc_match (" )%t") != MATCH_YES
)
3662 /* Check F08:C637. */
3665 gfc_error_1 ("MOLD tag at %L conflicts with SOURCE tag at %L",
3666 &mold
->where
, &source
->where
);
3670 /* Check F03:C623, */
3671 if (saw_deferred
&& ts
.type
== BT_UNKNOWN
&& !source
&& !mold
)
3673 gfc_error ("Allocate-object at %L with a deferred type parameter "
3674 "requires either a type-spec or SOURCE tag or a MOLD tag",
3679 /* Check F03:C625, */
3680 if (saw_unlimited
&& ts
.type
== BT_UNKNOWN
&& !source
&& !mold
)
3682 for (tail
= head
; tail
; tail
= tail
->next
)
3684 if (UNLIMITED_POLY (tail
->expr
))
3685 gfc_error ("Unlimited polymorphic allocate-object at %L "
3686 "requires either a type-spec or SOURCE tag "
3687 "or a MOLD tag", &tail
->expr
->where
);
3692 new_st
.op
= EXEC_ALLOCATE
;
3693 new_st
.expr1
= stat
;
3694 new_st
.expr2
= errmsg
;
3696 new_st
.expr3
= source
;
3698 new_st
.expr3
= mold
;
3699 new_st
.ext
.alloc
.list
= head
;
3700 new_st
.ext
.alloc
.ts
= ts
;
3705 gfc_syntax_error (ST_ALLOCATE
);
3708 gfc_free_expr (errmsg
);
3709 gfc_free_expr (source
);
3710 gfc_free_expr (stat
);
3711 gfc_free_expr (mold
);
3712 if (tmp
&& tmp
->expr_type
) gfc_free_expr (tmp
);
3713 gfc_free_alloc_list (head
);
3718 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
3719 a set of pointer assignments to intrinsic NULL(). */
3722 gfc_match_nullify (void)
3730 if (gfc_match_char ('(') != MATCH_YES
)
3735 m
= gfc_match_variable (&p
, 0);
3736 if (m
== MATCH_ERROR
)
3741 if (gfc_check_do_variable (p
->symtree
))
3745 if (gfc_is_coindexed (p
))
3747 gfc_error ("Pointer object at %C shall not be coindexed");
3751 /* build ' => NULL() '. */
3752 e
= gfc_get_null_expr (&gfc_current_locus
);
3754 /* Chain to list. */
3758 tail
->op
= EXEC_POINTER_ASSIGN
;
3762 tail
->next
= gfc_get_code (EXEC_POINTER_ASSIGN
);
3769 if (gfc_match (" )%t") == MATCH_YES
)
3771 if (gfc_match_char (',') != MATCH_YES
)
3778 gfc_syntax_error (ST_NULLIFY
);
3781 gfc_free_statements (new_st
.next
);
3783 gfc_free_expr (new_st
.expr1
);
3784 new_st
.expr1
= NULL
;
3785 gfc_free_expr (new_st
.expr2
);
3786 new_st
.expr2
= NULL
;
3791 /* Match a DEALLOCATE statement. */
3794 gfc_match_deallocate (void)
3796 gfc_alloc
*head
, *tail
;
3797 gfc_expr
*stat
, *errmsg
, *tmp
;
3800 bool saw_stat
, saw_errmsg
, b1
, b2
;
3803 stat
= errmsg
= tmp
= NULL
;
3804 saw_stat
= saw_errmsg
= false;
3806 if (gfc_match_char ('(') != MATCH_YES
)
3812 head
= tail
= gfc_get_alloc ();
3815 tail
->next
= gfc_get_alloc ();
3819 m
= gfc_match_variable (&tail
->expr
, 0);
3820 if (m
== MATCH_ERROR
)
3825 if (gfc_check_do_variable (tail
->expr
->symtree
))
3828 sym
= tail
->expr
->symtree
->n
.sym
;
3830 bool impure
= gfc_impure_variable (sym
);
3831 if (impure
&& gfc_pure (NULL
))
3833 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
3838 gfc_unset_implicit_pure (NULL
);
3840 if (gfc_is_coarray (tail
->expr
)
3841 && gfc_find_state (COMP_DO_CONCURRENT
))
3843 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
3847 if (gfc_is_coarray (tail
->expr
)
3848 && gfc_find_state (COMP_CRITICAL
))
3850 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
3854 /* FIXME: disable the checking on derived types. */
3855 b1
= !(tail
->expr
->ref
3856 && (tail
->expr
->ref
->type
== REF_COMPONENT
3857 || tail
->expr
->ref
->type
== REF_ARRAY
));
3858 if (sym
&& sym
->ts
.type
== BT_CLASS
)
3859 b2
= !(CLASS_DATA (sym
)->attr
.allocatable
3860 || CLASS_DATA (sym
)->attr
.class_pointer
);
3862 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
3863 || sym
->attr
.proc_pointer
);
3866 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
3867 "nor an allocatable variable");
3871 if (gfc_match_char (',') != MATCH_YES
)
3876 m
= gfc_match (" stat = %v", &tmp
);
3877 if (m
== MATCH_ERROR
)
3883 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
3884 gfc_free_expr (tmp
);
3891 if (gfc_check_do_variable (stat
->symtree
))
3894 if (gfc_match_char (',') == MATCH_YES
)
3895 goto dealloc_opt_list
;
3898 m
= gfc_match (" errmsg = %v", &tmp
);
3899 if (m
== MATCH_ERROR
)
3903 if (!gfc_notify_std (GFC_STD_F2003
, "ERRMSG at %L", &tmp
->where
))
3908 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
3909 gfc_free_expr (tmp
);
3916 if (gfc_match_char (',') == MATCH_YES
)
3917 goto dealloc_opt_list
;
3920 gfc_gobble_whitespace ();
3922 if (gfc_peek_char () == ')')
3926 if (gfc_match (" )%t") != MATCH_YES
)
3929 new_st
.op
= EXEC_DEALLOCATE
;
3930 new_st
.expr1
= stat
;
3931 new_st
.expr2
= errmsg
;
3932 new_st
.ext
.alloc
.list
= head
;
3937 gfc_syntax_error (ST_DEALLOCATE
);
3940 gfc_free_expr (errmsg
);
3941 gfc_free_expr (stat
);
3942 gfc_free_alloc_list (head
);
3947 /* Match a RETURN statement. */
3950 gfc_match_return (void)
3954 gfc_compile_state s
;
3958 if (gfc_find_state (COMP_CRITICAL
))
3960 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
3964 if (gfc_find_state (COMP_DO_CONCURRENT
))
3966 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
3970 if (gfc_match_eos () == MATCH_YES
)
3973 if (!gfc_find_state (COMP_SUBROUTINE
))
3975 gfc_error ("Alternate RETURN statement at %C is only allowed within "
3980 if (gfc_current_form
== FORM_FREE
)
3982 /* The following are valid, so we can't require a blank after the
3986 char c
= gfc_peek_ascii_char ();
3987 if (ISALPHA (c
) || ISDIGIT (c
))
3991 m
= gfc_match (" %e%t", &e
);
3994 if (m
== MATCH_ERROR
)
3997 gfc_syntax_error (ST_RETURN
);
4004 gfc_enclosing_unit (&s
);
4005 if (s
== COMP_PROGRAM
4006 && !gfc_notify_std (GFC_STD_GNU
, "RETURN statement in "
4007 "main program at %C"))
4010 new_st
.op
= EXEC_RETURN
;
4017 /* Match the call of a type-bound procedure, if CALL%var has already been
4018 matched and var found to be a derived-type variable. */
4021 match_typebound_call (gfc_symtree
* varst
)
4026 base
= gfc_get_expr ();
4027 base
->expr_type
= EXPR_VARIABLE
;
4028 base
->symtree
= varst
;
4029 base
->where
= gfc_current_locus
;
4030 gfc_set_sym_referenced (varst
->n
.sym
);
4032 m
= gfc_match_varspec (base
, 0, true, true);
4034 gfc_error ("Expected component reference at %C");
4037 gfc_free_expr (base
);
4041 if (gfc_match_eos () != MATCH_YES
)
4043 gfc_error ("Junk after CALL at %C");
4044 gfc_free_expr (base
);
4048 if (base
->expr_type
== EXPR_COMPCALL
)
4049 new_st
.op
= EXEC_COMPCALL
;
4050 else if (base
->expr_type
== EXPR_PPC
)
4051 new_st
.op
= EXEC_CALL_PPC
;
4054 gfc_error ("Expected type-bound procedure or procedure pointer component "
4056 gfc_free_expr (base
);
4059 new_st
.expr1
= base
;
4065 /* Match a CALL statement. The tricky part here are possible
4066 alternate return specifiers. We handle these by having all
4067 "subroutines" actually return an integer via a register that gives
4068 the return number. If the call specifies alternate returns, we
4069 generate code for a SELECT statement whose case clauses contain
4070 GOTOs to the various labels. */
4073 gfc_match_call (void)
4075 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4076 gfc_actual_arglist
*a
, *arglist
;
4086 m
= gfc_match ("% %n", name
);
4092 if (gfc_get_ha_sym_tree (name
, &st
))
4097 /* If this is a variable of derived-type, it probably starts a type-bound
4099 if ((sym
->attr
.flavor
!= FL_PROCEDURE
4100 || gfc_is_function_return_value (sym
, gfc_current_ns
))
4101 && (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
))
4102 return match_typebound_call (st
);
4104 /* If it does not seem to be callable (include functions so that the
4105 right association is made. They are thrown out in resolution.)
4107 if (!sym
->attr
.generic
4108 && !sym
->attr
.subroutine
4109 && !sym
->attr
.function
)
4111 if (!(sym
->attr
.external
&& !sym
->attr
.referenced
))
4113 /* ...create a symbol in this scope... */
4114 if (sym
->ns
!= gfc_current_ns
4115 && gfc_get_sym_tree (name
, NULL
, &st
, false) == 1)
4118 if (sym
!= st
->n
.sym
)
4122 /* ...and then to try to make the symbol into a subroutine. */
4123 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
4127 gfc_set_sym_referenced (sym
);
4129 if (gfc_match_eos () != MATCH_YES
)
4131 m
= gfc_match_actual_arglist (1, &arglist
);
4134 if (m
== MATCH_ERROR
)
4137 if (gfc_match_eos () != MATCH_YES
)
4141 /* If any alternate return labels were found, construct a SELECT
4142 statement that will jump to the right place. */
4145 for (a
= arglist
; a
; a
= a
->next
)
4146 if (a
->expr
== NULL
)
4154 gfc_symtree
*select_st
;
4155 gfc_symbol
*select_sym
;
4156 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4158 new_st
.next
= c
= gfc_get_code (EXEC_SELECT
);
4159 sprintf (name
, "_result_%s", sym
->name
);
4160 gfc_get_ha_sym_tree (name
, &select_st
); /* Can't fail. */
4162 select_sym
= select_st
->n
.sym
;
4163 select_sym
->ts
.type
= BT_INTEGER
;
4164 select_sym
->ts
.kind
= gfc_default_integer_kind
;
4165 gfc_set_sym_referenced (select_sym
);
4166 c
->expr1
= gfc_get_expr ();
4167 c
->expr1
->expr_type
= EXPR_VARIABLE
;
4168 c
->expr1
->symtree
= select_st
;
4169 c
->expr1
->ts
= select_sym
->ts
;
4170 c
->expr1
->where
= gfc_current_locus
;
4173 for (a
= arglist
; a
; a
= a
->next
)
4175 if (a
->expr
!= NULL
)
4178 if (!gfc_reference_st_label (a
->label
, ST_LABEL_TARGET
))
4183 c
->block
= gfc_get_code (EXEC_SELECT
);
4186 new_case
= gfc_get_case ();
4187 new_case
->high
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, i
);
4188 new_case
->low
= new_case
->high
;
4189 c
->ext
.block
.case_list
= new_case
;
4191 c
->next
= gfc_get_code (EXEC_GOTO
);
4192 c
->next
->label1
= a
->label
;
4196 new_st
.op
= EXEC_CALL
;
4197 new_st
.symtree
= st
;
4198 new_st
.ext
.actual
= arglist
;
4203 gfc_syntax_error (ST_CALL
);
4206 gfc_free_actual_arglist (arglist
);
4211 /* Given a name, return a pointer to the common head structure,
4212 creating it if it does not exist. If FROM_MODULE is nonzero, we
4213 mangle the name so that it doesn't interfere with commons defined
4214 in the using namespace.
4215 TODO: Add to global symbol tree. */
4218 gfc_get_common (const char *name
, int from_module
)
4221 static int serial
= 0;
4222 char mangled_name
[GFC_MAX_SYMBOL_LEN
+ 1];
4226 /* A use associated common block is only needed to correctly layout
4227 the variables it contains. */
4228 snprintf (mangled_name
, GFC_MAX_SYMBOL_LEN
, "_%d_%s", serial
++, name
);
4229 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, mangled_name
);
4233 st
= gfc_find_symtree (gfc_current_ns
->common_root
, name
);
4236 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, name
);
4239 if (st
->n
.common
== NULL
)
4241 st
->n
.common
= gfc_get_common_head ();
4242 st
->n
.common
->where
= gfc_current_locus
;
4243 strcpy (st
->n
.common
->name
, name
);
4246 return st
->n
.common
;
4250 /* Match a common block name. */
4252 match
match_common_name (char *name
)
4256 if (gfc_match_char ('/') == MATCH_NO
)
4262 if (gfc_match_char ('/') == MATCH_YES
)
4268 m
= gfc_match_name (name
);
4270 if (m
== MATCH_ERROR
)
4272 if (m
== MATCH_YES
&& gfc_match_char ('/') == MATCH_YES
)
4275 gfc_error ("Syntax error in common block name at %C");
4280 /* Match a COMMON statement. */
4283 gfc_match_common (void)
4285 gfc_symbol
*sym
, **head
, *tail
, *other
, *old_blank_common
;
4286 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4292 old_blank_common
= gfc_current_ns
->blank_common
.head
;
4293 if (old_blank_common
)
4295 while (old_blank_common
->common_next
)
4296 old_blank_common
= old_blank_common
->common_next
;
4303 m
= match_common_name (name
);
4304 if (m
== MATCH_ERROR
)
4307 if (name
[0] == '\0')
4309 t
= &gfc_current_ns
->blank_common
;
4310 if (t
->head
== NULL
)
4311 t
->where
= gfc_current_locus
;
4315 t
= gfc_get_common (name
, 0);
4324 while (tail
->common_next
)
4325 tail
= tail
->common_next
;
4328 /* Grab the list of symbols. */
4331 m
= gfc_match_symbol (&sym
, 0);
4332 if (m
== MATCH_ERROR
)
4337 /* Store a ref to the common block for error checking. */
4338 sym
->common_block
= t
;
4339 sym
->common_block
->refs
++;
4341 /* See if we know the current common block is bind(c), and if
4342 so, then see if we can check if the symbol is (which it'll
4343 need to be). This can happen if the bind(c) attr stmt was
4344 applied to the common block, and the variable(s) already
4345 defined, before declaring the common block. */
4346 if (t
->is_bind_c
== 1)
4348 if (sym
->ts
.type
!= BT_UNKNOWN
&& sym
->ts
.is_c_interop
!= 1)
4350 /* If we find an error, just print it and continue,
4351 cause it's just semantic, and we can see if there
4353 gfc_error_now_1 ("Variable '%s' at %L in common block '%s' "
4354 "at %C must be declared with a C "
4355 "interoperable kind since common block "
4357 sym
->name
, &(sym
->declared_at
), t
->name
,
4361 if (sym
->attr
.is_bind_c
== 1)
4362 gfc_error_now ("Variable %qs in common block %qs at %C can not "
4363 "be bind(c) since it is not global", sym
->name
,
4367 if (sym
->attr
.in_common
)
4369 gfc_error ("Symbol %qs at %C is already in a COMMON block",
4374 if (((sym
->value
!= NULL
&& sym
->value
->expr_type
!= EXPR_NULL
)
4375 || sym
->attr
.data
) && gfc_current_state () != COMP_BLOCK_DATA
)
4377 if (!gfc_notify_std (GFC_STD_GNU
, "Initialized symbol %qs at "
4378 "%C can only be COMMON in BLOCK DATA",
4383 if (!gfc_add_in_common (&sym
->attr
, sym
->name
, NULL
))
4387 tail
->common_next
= sym
;
4393 /* Deal with an optional array specification after the
4395 m
= gfc_match_array_spec (&as
, true, true);
4396 if (m
== MATCH_ERROR
)
4401 if (as
->type
!= AS_EXPLICIT
)
4403 gfc_error ("Array specification for symbol %qs in COMMON "
4404 "at %C must be explicit", sym
->name
);
4408 if (!gfc_add_dimension (&sym
->attr
, sym
->name
, NULL
))
4411 if (sym
->attr
.pointer
)
4413 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
4414 "POINTER array", sym
->name
);
4423 sym
->common_head
= t
;
4425 /* Check to see if the symbol is already in an equivalence group.
4426 If it is, set the other members as being in common. */
4427 if (sym
->attr
.in_equivalence
)
4429 for (e1
= gfc_current_ns
->equiv
; e1
; e1
= e1
->next
)
4431 for (e2
= e1
; e2
; e2
= e2
->eq
)
4432 if (e2
->expr
->symtree
->n
.sym
== sym
)
4439 for (e2
= e1
; e2
; e2
= e2
->eq
)
4441 other
= e2
->expr
->symtree
->n
.sym
;
4442 if (other
->common_head
4443 && other
->common_head
!= sym
->common_head
)
4445 gfc_error ("Symbol %qs, in COMMON block %qs at "
4446 "%C is being indirectly equivalenced to "
4447 "another COMMON block %qs",
4448 sym
->name
, sym
->common_head
->name
,
4449 other
->common_head
->name
);
4452 other
->attr
.in_common
= 1;
4453 other
->common_head
= t
;
4459 gfc_gobble_whitespace ();
4460 if (gfc_match_eos () == MATCH_YES
)
4462 if (gfc_peek_ascii_char () == '/')
4464 if (gfc_match_char (',') != MATCH_YES
)
4466 gfc_gobble_whitespace ();
4467 if (gfc_peek_ascii_char () == '/')
4476 gfc_syntax_error (ST_COMMON
);
4479 gfc_free_array_spec (as
);
4484 /* Match a BLOCK DATA program unit. */
4487 gfc_match_block_data (void)
4489 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4493 if (gfc_match_eos () == MATCH_YES
)
4495 gfc_new_block
= NULL
;
4499 m
= gfc_match ("% %n%t", name
);
4503 if (gfc_get_symbol (name
, NULL
, &sym
))
4506 if (!gfc_add_flavor (&sym
->attr
, FL_BLOCK_DATA
, sym
->name
, NULL
))
4509 gfc_new_block
= sym
;
4515 /* Free a namelist structure. */
4518 gfc_free_namelist (gfc_namelist
*name
)
4522 for (; name
; name
= n
)
4530 /* Free an OpenMP namelist structure. */
4533 gfc_free_omp_namelist (gfc_omp_namelist
*name
)
4535 gfc_omp_namelist
*n
;
4537 for (; name
; name
= n
)
4539 gfc_free_expr (name
->expr
);
4542 if (name
->udr
->combiner
)
4543 gfc_free_statement (name
->udr
->combiner
);
4544 if (name
->udr
->initializer
)
4545 gfc_free_statement (name
->udr
->initializer
);
4554 /* Match a NAMELIST statement. */
4557 gfc_match_namelist (void)
4559 gfc_symbol
*group_name
, *sym
;
4563 m
= gfc_match (" / %s /", &group_name
);
4566 if (m
== MATCH_ERROR
)
4571 if (group_name
->ts
.type
!= BT_UNKNOWN
)
4573 gfc_error ("Namelist group name %qs at %C already has a basic "
4574 "type of %s", group_name
->name
,
4575 gfc_typename (&group_name
->ts
));
4579 if (group_name
->attr
.flavor
== FL_NAMELIST
4580 && group_name
->attr
.use_assoc
4581 && !gfc_notify_std (GFC_STD_GNU
, "Namelist group name %qs "
4582 "at %C already is USE associated and can"
4583 "not be respecified.", group_name
->name
))
4586 if (group_name
->attr
.flavor
!= FL_NAMELIST
4587 && !gfc_add_flavor (&group_name
->attr
, FL_NAMELIST
,
4588 group_name
->name
, NULL
))
4593 m
= gfc_match_symbol (&sym
, 1);
4596 if (m
== MATCH_ERROR
)
4599 if (sym
->attr
.in_namelist
== 0
4600 && !gfc_add_in_namelist (&sym
->attr
, sym
->name
, NULL
))
4603 /* Use gfc_error_check here, rather than goto error, so that
4604 these are the only errors for the next two lines. */
4605 if (sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
)
4607 gfc_error ("Assumed size array %qs in namelist %qs at "
4608 "%C is not allowed", sym
->name
, group_name
->name
);
4612 nl
= gfc_get_namelist ();
4616 if (group_name
->namelist
== NULL
)
4617 group_name
->namelist
= group_name
->namelist_tail
= nl
;
4620 group_name
->namelist_tail
->next
= nl
;
4621 group_name
->namelist_tail
= nl
;
4624 if (gfc_match_eos () == MATCH_YES
)
4627 m
= gfc_match_char (',');
4629 if (gfc_match_char ('/') == MATCH_YES
)
4631 m2
= gfc_match (" %s /", &group_name
);
4632 if (m2
== MATCH_YES
)
4634 if (m2
== MATCH_ERROR
)
4648 gfc_syntax_error (ST_NAMELIST
);
4655 /* Match a MODULE statement. */
4658 gfc_match_module (void)
4662 m
= gfc_match (" %s%t", &gfc_new_block
);
4666 if (!gfc_add_flavor (&gfc_new_block
->attr
, FL_MODULE
,
4667 gfc_new_block
->name
, NULL
))
4674 /* Free equivalence sets and lists. Recursively is the easiest way to
4678 gfc_free_equiv_until (gfc_equiv
*eq
, gfc_equiv
*stop
)
4683 gfc_free_equiv (eq
->eq
);
4684 gfc_free_equiv_until (eq
->next
, stop
);
4685 gfc_free_expr (eq
->expr
);
4691 gfc_free_equiv (gfc_equiv
*eq
)
4693 gfc_free_equiv_until (eq
, NULL
);
4697 /* Match an EQUIVALENCE statement. */
4700 gfc_match_equivalence (void)
4702 gfc_equiv
*eq
, *set
, *tail
;
4706 gfc_common_head
*common_head
= NULL
;
4714 eq
= gfc_get_equiv ();
4718 eq
->next
= gfc_current_ns
->equiv
;
4719 gfc_current_ns
->equiv
= eq
;
4721 if (gfc_match_char ('(') != MATCH_YES
)
4725 common_flag
= FALSE
;
4730 m
= gfc_match_equiv_variable (&set
->expr
);
4731 if (m
== MATCH_ERROR
)
4736 /* count the number of objects. */
4739 if (gfc_match_char ('%') == MATCH_YES
)
4741 gfc_error ("Derived type component %C is not a "
4742 "permitted EQUIVALENCE member");
4746 for (ref
= set
->expr
->ref
; ref
; ref
= ref
->next
)
4747 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
== AR_SECTION
)
4749 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
4750 "be an array section");
4754 sym
= set
->expr
->symtree
->n
.sym
;
4756 if (!gfc_add_in_equivalence (&sym
->attr
, sym
->name
, NULL
))
4759 if (sym
->attr
.in_common
)
4762 common_head
= sym
->common_head
;
4765 if (gfc_match_char (')') == MATCH_YES
)
4768 if (gfc_match_char (',') != MATCH_YES
)
4771 set
->eq
= gfc_get_equiv ();
4777 gfc_error ("EQUIVALENCE at %C requires two or more objects");
4781 /* If one of the members of an equivalence is in common, then
4782 mark them all as being in common. Before doing this, check
4783 that members of the equivalence group are not in different
4786 for (set
= eq
; set
; set
= set
->eq
)
4788 sym
= set
->expr
->symtree
->n
.sym
;
4789 if (sym
->common_head
&& sym
->common_head
!= common_head
)
4791 gfc_error ("Attempt to indirectly overlap COMMON "
4792 "blocks %s and %s by EQUIVALENCE at %C",
4793 sym
->common_head
->name
, common_head
->name
);
4796 sym
->attr
.in_common
= 1;
4797 sym
->common_head
= common_head
;
4800 if (gfc_match_eos () == MATCH_YES
)
4802 if (gfc_match_char (',') != MATCH_YES
)
4804 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
4812 gfc_syntax_error (ST_EQUIVALENCE
);
4818 gfc_free_equiv (gfc_current_ns
->equiv
);
4819 gfc_current_ns
->equiv
= eq
;
4825 /* Check that a statement function is not recursive. This is done by looking
4826 for the statement function symbol(sym) by looking recursively through its
4827 expression(e). If a reference to sym is found, true is returned.
4828 12.5.4 requires that any variable of function that is implicitly typed
4829 shall have that type confirmed by any subsequent type declaration. The
4830 implicit typing is conveniently done here. */
4832 recursive_stmt_fcn (gfc_expr
*, gfc_symbol
*);
4835 check_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
4841 switch (e
->expr_type
)
4844 if (e
->symtree
== NULL
)
4847 /* Check the name before testing for nested recursion! */
4848 if (sym
->name
== e
->symtree
->n
.sym
->name
)
4851 /* Catch recursion via other statement functions. */
4852 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
4853 && e
->symtree
->n
.sym
->value
4854 && recursive_stmt_fcn (e
->symtree
->n
.sym
->value
, sym
))
4857 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
4858 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
4863 if (e
->symtree
&& sym
->name
== e
->symtree
->n
.sym
->name
)
4866 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
4867 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
4879 recursive_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
)
4881 return gfc_traverse_expr (e
, sym
, check_stmt_fcn
, 0);
4885 /* Match a statement function declaration. It is so easy to match
4886 non-statement function statements with a MATCH_ERROR as opposed to
4887 MATCH_NO that we suppress error message in most cases. */
4890 gfc_match_st_function (void)
4892 gfc_error_buf old_error_1
;
4893 output_buffer old_error
;
4899 m
= gfc_match_symbol (&sym
, 0);
4903 gfc_push_error (&old_error
, &old_error_1
);
4905 if (!gfc_add_procedure (&sym
->attr
, PROC_ST_FUNCTION
, sym
->name
, NULL
))
4908 if (gfc_match_formal_arglist (sym
, 1, 0) != MATCH_YES
)
4911 m
= gfc_match (" = %e%t", &expr
);
4915 gfc_free_error (&old_error
, &old_error_1
);
4917 if (m
== MATCH_ERROR
)
4920 if (recursive_stmt_fcn (expr
, sym
))
4922 gfc_error ("Statement function at %L is recursive", &expr
->where
);
4928 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Statement function at %C"))
4934 gfc_pop_error (&old_error
, &old_error_1
);
4939 /***************** SELECT CASE subroutines ******************/
4941 /* Free a single case structure. */
4944 free_case (gfc_case
*p
)
4946 if (p
->low
== p
->high
)
4948 gfc_free_expr (p
->low
);
4949 gfc_free_expr (p
->high
);
4954 /* Free a list of case structures. */
4957 gfc_free_case_list (gfc_case
*p
)
4969 /* Match a single case selector. */
4972 match_case_selector (gfc_case
**cp
)
4977 c
= gfc_get_case ();
4978 c
->where
= gfc_current_locus
;
4980 if (gfc_match_char (':') == MATCH_YES
)
4982 m
= gfc_match_init_expr (&c
->high
);
4985 if (m
== MATCH_ERROR
)
4990 m
= gfc_match_init_expr (&c
->low
);
4991 if (m
== MATCH_ERROR
)
4996 /* If we're not looking at a ':' now, make a range out of a single
4997 target. Else get the upper bound for the case range. */
4998 if (gfc_match_char (':') != MATCH_YES
)
5002 m
= gfc_match_init_expr (&c
->high
);
5003 if (m
== MATCH_ERROR
)
5005 /* MATCH_NO is fine. It's OK if nothing is there! */
5013 gfc_error ("Expected initialization expression in CASE at %C");
5021 /* Match the end of a case statement. */
5024 match_case_eos (void)
5026 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5029 if (gfc_match_eos () == MATCH_YES
)
5032 /* If the case construct doesn't have a case-construct-name, we
5033 should have matched the EOS. */
5034 if (!gfc_current_block ())
5037 gfc_gobble_whitespace ();
5039 m
= gfc_match_name (name
);
5043 if (strcmp (name
, gfc_current_block ()->name
) != 0)
5045 gfc_error ("Expected block name %qs of SELECT construct at %C",
5046 gfc_current_block ()->name
);
5050 return gfc_match_eos ();
5054 /* Match a SELECT statement. */
5057 gfc_match_select (void)
5062 m
= gfc_match_label ();
5063 if (m
== MATCH_ERROR
)
5066 m
= gfc_match (" select case ( %e )%t", &expr
);
5070 new_st
.op
= EXEC_SELECT
;
5071 new_st
.expr1
= expr
;
5077 /* Transfer the selector typespec to the associate name. */
5080 copy_ts_from_selector_to_associate (gfc_expr
*associate
, gfc_expr
*selector
)
5083 gfc_symbol
*assoc_sym
;
5085 assoc_sym
= associate
->symtree
->n
.sym
;
5087 /* At this stage the expression rank and arrayspec dimensions have
5088 not been completely sorted out. We must get the expr2->rank
5089 right here, so that the correct class container is obtained. */
5090 ref
= selector
->ref
;
5091 while (ref
&& ref
->next
)
5094 if (selector
->ts
.type
== BT_CLASS
&& CLASS_DATA (selector
)->as
5095 && ref
&& ref
->type
== REF_ARRAY
)
5097 /* Ensure that the array reference type is set. We cannot use
5098 gfc_resolve_expr at this point, so the usable parts of
5099 resolve.c(resolve_array_ref) are employed to do it. */
5100 if (ref
->u
.ar
.type
== AR_UNKNOWN
)
5102 ref
->u
.ar
.type
= AR_ELEMENT
;
5103 for (int i
= 0; i
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; i
++)
5104 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_RANGE
5105 || ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
5106 || (ref
->u
.ar
.dimen_type
[i
] == DIMEN_UNKNOWN
5107 && ref
->u
.ar
.start
[i
] && ref
->u
.ar
.start
[i
]->rank
))
5109 ref
->u
.ar
.type
= AR_SECTION
;
5114 if (ref
->u
.ar
.type
== AR_FULL
)
5115 selector
->rank
= CLASS_DATA (selector
)->as
->rank
;
5116 else if (ref
->u
.ar
.type
== AR_SECTION
)
5117 selector
->rank
= ref
->u
.ar
.dimen
;
5124 assoc_sym
->attr
.dimension
= 1;
5125 assoc_sym
->as
= gfc_get_array_spec ();
5126 assoc_sym
->as
->rank
= selector
->rank
;
5127 assoc_sym
->as
->type
= AS_DEFERRED
;
5130 assoc_sym
->as
= NULL
;
5132 if (selector
->ts
.type
== BT_CLASS
)
5134 /* The correct class container has to be available. */
5135 assoc_sym
->ts
.type
= BT_CLASS
;
5136 assoc_sym
->ts
.u
.derived
= CLASS_DATA (selector
)->ts
.u
.derived
;
5137 assoc_sym
->attr
.pointer
= 1;
5138 gfc_build_class_symbol (&assoc_sym
->ts
, &assoc_sym
->attr
, &assoc_sym
->as
);
5143 /* Push the current selector onto the SELECT TYPE stack. */
5146 select_type_push (gfc_symbol
*sel
)
5148 gfc_select_type_stack
*top
= gfc_get_select_type_stack ();
5149 top
->selector
= sel
;
5151 top
->prev
= select_type_stack
;
5153 select_type_stack
= top
;
5157 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
5159 static gfc_symtree
*
5160 select_intrinsic_set_tmp (gfc_typespec
*ts
)
5162 char name
[GFC_MAX_SYMBOL_LEN
];
5166 if (ts
->type
== BT_CLASS
|| ts
->type
== BT_DERIVED
)
5169 if (select_type_stack
->selector
->ts
.type
== BT_CLASS
5170 && !select_type_stack
->selector
->attr
.class_ok
)
5173 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
5174 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
5175 charlen
= mpz_get_si (ts
->u
.cl
->length
->value
.integer
);
5177 if (ts
->type
!= BT_CHARACTER
)
5178 sprintf (name
, "__tmp_%s_%d", gfc_basic_typename (ts
->type
),
5181 sprintf (name
, "__tmp_%s_%d_%d", gfc_basic_typename (ts
->type
),
5184 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
5185 gfc_add_type (tmp
->n
.sym
, ts
, NULL
);
5187 /* Copy across the array spec to the selector. */
5188 if (select_type_stack
->selector
->ts
.type
== BT_CLASS
5189 && (CLASS_DATA (select_type_stack
->selector
)->attr
.dimension
5190 || CLASS_DATA (select_type_stack
->selector
)->attr
.codimension
))
5192 tmp
->n
.sym
->attr
.pointer
= 1;
5193 tmp
->n
.sym
->attr
.dimension
5194 = CLASS_DATA (select_type_stack
->selector
)->attr
.dimension
;
5195 tmp
->n
.sym
->attr
.codimension
5196 = CLASS_DATA (select_type_stack
->selector
)->attr
.codimension
;
5198 = gfc_copy_array_spec (CLASS_DATA (select_type_stack
->selector
)->as
);
5201 gfc_set_sym_referenced (tmp
->n
.sym
);
5202 gfc_add_flavor (&tmp
->n
.sym
->attr
, FL_VARIABLE
, name
, NULL
);
5203 tmp
->n
.sym
->attr
.select_type_temporary
= 1;
5209 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
5212 select_type_set_tmp (gfc_typespec
*ts
)
5214 char name
[GFC_MAX_SYMBOL_LEN
];
5215 gfc_symtree
*tmp
= NULL
;
5219 select_type_stack
->tmp
= NULL
;
5223 tmp
= select_intrinsic_set_tmp (ts
);
5230 if (ts
->type
== BT_CLASS
)
5231 sprintf (name
, "__tmp_class_%s", ts
->u
.derived
->name
);
5233 sprintf (name
, "__tmp_type_%s", ts
->u
.derived
->name
);
5234 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
5235 gfc_add_type (tmp
->n
.sym
, ts
, NULL
);
5237 if (select_type_stack
->selector
->ts
.type
== BT_CLASS
5238 && select_type_stack
->selector
->attr
.class_ok
)
5240 tmp
->n
.sym
->attr
.pointer
5241 = CLASS_DATA (select_type_stack
->selector
)->attr
.class_pointer
;
5243 /* Copy across the array spec to the selector. */
5244 if (CLASS_DATA (select_type_stack
->selector
)->attr
.dimension
5245 || CLASS_DATA (select_type_stack
->selector
)->attr
.codimension
)
5247 tmp
->n
.sym
->attr
.dimension
5248 = CLASS_DATA (select_type_stack
->selector
)->attr
.dimension
;
5249 tmp
->n
.sym
->attr
.codimension
5250 = CLASS_DATA (select_type_stack
->selector
)->attr
.codimension
;
5252 = gfc_copy_array_spec (CLASS_DATA (select_type_stack
->selector
)->as
);
5256 gfc_set_sym_referenced (tmp
->n
.sym
);
5257 gfc_add_flavor (&tmp
->n
.sym
->attr
, FL_VARIABLE
, name
, NULL
);
5258 tmp
->n
.sym
->attr
.select_type_temporary
= 1;
5260 if (ts
->type
== BT_CLASS
)
5261 gfc_build_class_symbol (&tmp
->n
.sym
->ts
, &tmp
->n
.sym
->attr
,
5265 /* Add an association for it, so the rest of the parser knows it is
5266 an associate-name. The target will be set during resolution. */
5267 tmp
->n
.sym
->assoc
= gfc_get_association_list ();
5268 tmp
->n
.sym
->assoc
->dangling
= 1;
5269 tmp
->n
.sym
->assoc
->st
= tmp
;
5271 select_type_stack
->tmp
= tmp
;
5275 /* Match a SELECT TYPE statement. */
5278 gfc_match_select_type (void)
5280 gfc_expr
*expr1
, *expr2
= NULL
;
5282 char name
[GFC_MAX_SYMBOL_LEN
];
5286 m
= gfc_match_label ();
5287 if (m
== MATCH_ERROR
)
5290 m
= gfc_match (" select type ( ");
5294 m
= gfc_match (" %n => %e", name
, &expr2
);
5297 expr1
= gfc_get_expr();
5298 expr1
->expr_type
= EXPR_VARIABLE
;
5299 if (gfc_get_sym_tree (name
, NULL
, &expr1
->symtree
, false))
5305 sym
= expr1
->symtree
->n
.sym
;
5306 if (expr2
->ts
.type
== BT_UNKNOWN
)
5307 sym
->attr
.untyped
= 1;
5309 copy_ts_from_selector_to_associate (expr1
, expr2
);
5311 sym
->attr
.flavor
= FL_VARIABLE
;
5312 sym
->attr
.referenced
= 1;
5313 sym
->attr
.class_ok
= 1;
5317 m
= gfc_match (" %e ", &expr1
);
5322 m
= gfc_match (" )%t");
5325 gfc_error ("parse error in SELECT TYPE statement at %C");
5329 /* This ghastly expression seems to be needed to distinguish a CLASS
5330 array, which can have a reference, from other expressions that
5331 have references, such as derived type components, and are not
5332 allowed by the standard.
5333 TODO: see if it is sufficient to exclude component and substring
5335 class_array
= expr1
->expr_type
== EXPR_VARIABLE
5336 && expr1
->ts
.type
== BT_CLASS
5337 && CLASS_DATA (expr1
)
5338 && (strcmp (CLASS_DATA (expr1
)->name
, "_data") == 0)
5339 && (CLASS_DATA (expr1
)->attr
.dimension
5340 || CLASS_DATA (expr1
)->attr
.codimension
)
5342 && expr1
->ref
->type
== REF_ARRAY
5343 && expr1
->ref
->next
== NULL
;
5345 /* Check for F03:C811. */
5346 if (!expr2
&& (expr1
->expr_type
!= EXPR_VARIABLE
5347 || (!class_array
&& expr1
->ref
!= NULL
)))
5349 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
5350 "use associate-name=>");
5355 new_st
.op
= EXEC_SELECT_TYPE
;
5356 new_st
.expr1
= expr1
;
5357 new_st
.expr2
= expr2
;
5358 new_st
.ext
.block
.ns
= gfc_current_ns
;
5360 select_type_push (expr1
->symtree
->n
.sym
);
5365 gfc_free_expr (expr1
);
5366 gfc_free_expr (expr2
);
5371 /* Match a CASE statement. */
5374 gfc_match_case (void)
5376 gfc_case
*c
, *head
, *tail
;
5381 if (gfc_current_state () != COMP_SELECT
)
5383 gfc_error ("Unexpected CASE statement at %C");
5387 if (gfc_match ("% default") == MATCH_YES
)
5389 m
= match_case_eos ();
5392 if (m
== MATCH_ERROR
)
5395 new_st
.op
= EXEC_SELECT
;
5396 c
= gfc_get_case ();
5397 c
->where
= gfc_current_locus
;
5398 new_st
.ext
.block
.case_list
= c
;
5402 if (gfc_match_char ('(') != MATCH_YES
)
5407 if (match_case_selector (&c
) == MATCH_ERROR
)
5417 if (gfc_match_char (')') == MATCH_YES
)
5419 if (gfc_match_char (',') != MATCH_YES
)
5423 m
= match_case_eos ();
5426 if (m
== MATCH_ERROR
)
5429 new_st
.op
= EXEC_SELECT
;
5430 new_st
.ext
.block
.case_list
= head
;
5435 gfc_error ("Syntax error in CASE specification at %C");
5438 gfc_free_case_list (head
); /* new_st is cleaned up in parse.c. */
5443 /* Match a TYPE IS statement. */
5446 gfc_match_type_is (void)
5451 if (gfc_current_state () != COMP_SELECT_TYPE
)
5453 gfc_error ("Unexpected TYPE IS statement at %C");
5457 if (gfc_match_char ('(') != MATCH_YES
)
5460 c
= gfc_get_case ();
5461 c
->where
= gfc_current_locus
;
5463 if (gfc_match_type_spec (&c
->ts
) == MATCH_ERROR
)
5466 if (gfc_match_char (')') != MATCH_YES
)
5469 m
= match_case_eos ();
5472 if (m
== MATCH_ERROR
)
5475 new_st
.op
= EXEC_SELECT_TYPE
;
5476 new_st
.ext
.block
.case_list
= c
;
5478 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
5479 && (c
->ts
.u
.derived
->attr
.sequence
5480 || c
->ts
.u
.derived
->attr
.is_bind_c
))
5482 gfc_error ("The type-spec shall not specify a sequence derived "
5483 "type or a type with the BIND attribute in SELECT "
5484 "TYPE at %C [F2003:C815]");
5488 /* Create temporary variable. */
5489 select_type_set_tmp (&c
->ts
);
5494 gfc_error ("Syntax error in TYPE IS specification at %C");
5498 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
5503 /* Match a CLASS IS or CLASS DEFAULT statement. */
5506 gfc_match_class_is (void)
5511 if (gfc_current_state () != COMP_SELECT_TYPE
)
5514 if (gfc_match ("% default") == MATCH_YES
)
5516 m
= match_case_eos ();
5519 if (m
== MATCH_ERROR
)
5522 new_st
.op
= EXEC_SELECT_TYPE
;
5523 c
= gfc_get_case ();
5524 c
->where
= gfc_current_locus
;
5525 c
->ts
.type
= BT_UNKNOWN
;
5526 new_st
.ext
.block
.case_list
= c
;
5527 select_type_set_tmp (NULL
);
5531 m
= gfc_match ("% is");
5534 if (m
== MATCH_ERROR
)
5537 if (gfc_match_char ('(') != MATCH_YES
)
5540 c
= gfc_get_case ();
5541 c
->where
= gfc_current_locus
;
5543 if (match_derived_type_spec (&c
->ts
) == MATCH_ERROR
)
5546 if (c
->ts
.type
== BT_DERIVED
)
5547 c
->ts
.type
= BT_CLASS
;
5549 if (gfc_match_char (')') != MATCH_YES
)
5552 m
= match_case_eos ();
5555 if (m
== MATCH_ERROR
)
5558 new_st
.op
= EXEC_SELECT_TYPE
;
5559 new_st
.ext
.block
.case_list
= c
;
5561 /* Create temporary variable. */
5562 select_type_set_tmp (&c
->ts
);
5567 gfc_error ("Syntax error in CLASS IS specification at %C");
5571 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
5576 /********************* WHERE subroutines ********************/
5578 /* Match the rest of a simple WHERE statement that follows an IF statement.
5582 match_simple_where (void)
5588 m
= gfc_match (" ( %e )", &expr
);
5592 m
= gfc_match_assignment ();
5595 if (m
== MATCH_ERROR
)
5598 if (gfc_match_eos () != MATCH_YES
)
5601 c
= gfc_get_code (EXEC_WHERE
);
5604 c
->next
= XCNEW (gfc_code
);
5606 gfc_clear_new_st ();
5608 new_st
.op
= EXEC_WHERE
;
5614 gfc_syntax_error (ST_WHERE
);
5617 gfc_free_expr (expr
);
5622 /* Match a WHERE statement. */
5625 gfc_match_where (gfc_statement
*st
)
5631 m0
= gfc_match_label ();
5632 if (m0
== MATCH_ERROR
)
5635 m
= gfc_match (" where ( %e )", &expr
);
5639 if (gfc_match_eos () == MATCH_YES
)
5641 *st
= ST_WHERE_BLOCK
;
5642 new_st
.op
= EXEC_WHERE
;
5643 new_st
.expr1
= expr
;
5647 m
= gfc_match_assignment ();
5649 gfc_syntax_error (ST_WHERE
);
5653 gfc_free_expr (expr
);
5657 /* We've got a simple WHERE statement. */
5659 c
= gfc_get_code (EXEC_WHERE
);
5662 c
->next
= XCNEW (gfc_code
);
5664 gfc_clear_new_st ();
5666 new_st
.op
= EXEC_WHERE
;
5673 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
5674 new_st if successful. */
5677 gfc_match_elsewhere (void)
5679 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5683 if (gfc_current_state () != COMP_WHERE
)
5685 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
5691 if (gfc_match_char ('(') == MATCH_YES
)
5693 m
= gfc_match_expr (&expr
);
5696 if (m
== MATCH_ERROR
)
5699 if (gfc_match_char (')') != MATCH_YES
)
5703 if (gfc_match_eos () != MATCH_YES
)
5705 /* Only makes sense if we have a where-construct-name. */
5706 if (!gfc_current_block ())
5711 /* Better be a name at this point. */
5712 m
= gfc_match_name (name
);
5715 if (m
== MATCH_ERROR
)
5718 if (gfc_match_eos () != MATCH_YES
)
5721 if (strcmp (name
, gfc_current_block ()->name
) != 0)
5723 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
5724 name
, gfc_current_block ()->name
);
5729 new_st
.op
= EXEC_WHERE
;
5730 new_st
.expr1
= expr
;
5734 gfc_syntax_error (ST_ELSEWHERE
);
5737 gfc_free_expr (expr
);