1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2019 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"
29 int gfc_matching_ptr_assignment
= 0;
30 int gfc_matching_procptr_assignment
= 0;
31 bool gfc_matching_prefix
= false;
33 /* Stack of SELECT TYPE statements. */
34 gfc_select_type_stack
*select_type_stack
= NULL
;
36 /* List of type parameter expressions. */
37 gfc_actual_arglist
*type_param_spec_list
;
39 /* For debugging and diagnostic purposes. Return the textual representation
40 of the intrinsic operator OP. */
42 gfc_op2string (gfc_intrinsic_op op
)
50 case INTRINSIC_UMINUS
:
56 case INTRINSIC_CONCAT
:
60 case INTRINSIC_DIVIDE
:
99 case INTRINSIC_ASSIGN
:
102 case INTRINSIC_PARENTHESES
:
109 case INTRINSIC_FORMATTED
:
111 case INTRINSIC_UNFORMATTED
:
112 return "unformatted";
118 gfc_internal_error ("gfc_op2string(): Bad code");
123 /******************** Generic matching subroutines ************************/
125 /* Matches a member separator. With standard FORTRAN this is '%', but with
126 DEC structures we must carefully match dot ('.').
127 Because operators are spelled ".op.", a dotted string such as "x.y.z..."
128 can be either a component reference chain or a combination of binary
130 There is no real way to win because the string may be grammatically
131 ambiguous. The following rules help avoid ambiguities - they match
132 some behavior of other (older) compilers. If the rules here are changed
133 the test cases should be updated. If the user has problems with these rules
134 they probably deserve the consequences. Consider "x.y.z":
135 (1) If any user defined operator ".y." exists, this is always y(x,z)
136 (even if ".y." is the wrong type and/or x has a member y).
137 (2) Otherwise if x has a member y, and y is itself a derived type,
138 this is (x->y)->z, even if an intrinsic operator exists which
140 (3) If x has no member y or (x->y) is not a derived type but ".y."
141 is an intrinsic operator (such as ".eq."), this is y(x,z).
142 (4) Lastly if there is no operator ".y." and x has no member "y", it is an
144 It is worth noting that the logic here does not support mixed use of member
145 accessors within a single string. That is, even if x has component y and y
146 has component z, the following are all syntax errors:
147 "x%y.z" "x.y%z" "(x.y).z" "(x%y)%z"
151 gfc_match_member_sep(gfc_symbol
*sym
)
153 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
154 locus dot_loc
, start_loc
;
155 gfc_intrinsic_op iop
;
158 gfc_component
*c
= NULL
;
160 /* What a relief: '%' is an unambiguous member separator. */
161 if (gfc_match_char ('%') == MATCH_YES
)
164 /* Beware ye who enter here. */
165 if (!flag_dec_structure
|| !sym
)
170 /* We may be given either a derived type variable or the derived type
171 declaration itself (which actually contains the components);
172 we need the latter to search for components. */
173 if (gfc_fl_struct (sym
->attr
.flavor
))
175 else if (gfc_bt_struct (sym
->ts
.type
))
176 tsym
= sym
->ts
.u
.derived
;
178 iop
= INTRINSIC_NONE
;
182 /* If we have to reject come back here later. */
183 start_loc
= gfc_current_locus
;
185 /* Look for a component access next. */
186 if (gfc_match_char ('.') != MATCH_YES
)
189 /* If we accept, come back here. */
190 dot_loc
= gfc_current_locus
;
192 /* Try to match a symbol name following the dot. */
193 if (gfc_match_name (name
) != MATCH_YES
)
195 gfc_error ("Expected structure component or operator name "
200 /* If no dot follows we have "x.y" which should be a component access. */
201 if (gfc_match_char ('.') != MATCH_YES
)
204 /* Now we have a string "x.y.z" which could be a nested member access
205 (x->y)->z or a binary operation y on x and z. */
207 /* First use any user-defined operators ".y." */
208 if (gfc_find_uop (name
, sym
->ns
) != NULL
)
211 /* Match accesses to existing derived-type components for
212 derived-type vars: "x.y.z" = (x->y)->z */
213 c
= gfc_find_component(tsym
, name
, false, true, NULL
);
214 if (c
&& (gfc_bt_struct (c
->ts
.type
) || c
->ts
.type
== BT_CLASS
))
217 /* If y is not a component or has no members, try intrinsic operators. */
218 gfc_current_locus
= start_loc
;
219 if (gfc_match_intrinsic_op (&iop
) != MATCH_YES
)
221 /* If ".y." is not an intrinsic operator but y was a valid non-
222 structure component, match and leave the trailing dot to be
227 gfc_error ("%qs is neither a defined operator nor a "
228 "structure component in dotted string at %C", name
);
232 /* .y. is an intrinsic operator, overriding any possible member access. */
235 /* Return keeping the current locus consistent with the match result. */
239 gfc_current_locus
= start_loc
;
242 gfc_current_locus
= dot_loc
;
247 /* This function scans the current statement counting the opened and closed
248 parenthesis to make sure they are balanced. */
251 gfc_match_parens (void)
253 locus old_loc
, where
;
255 gfc_instring instring
;
258 old_loc
= gfc_current_locus
;
260 instring
= NONSTRING
;
266 where
= gfc_current_locus
;
267 c
= gfc_next_char_literal (instring
);
270 if (quote
== ' ' && ((c
== '\'') || (c
== '"')))
273 instring
= INSTRING_WARN
;
276 if (quote
!= ' ' && c
== quote
)
279 instring
= NONSTRING
;
283 if (c
== '(' && quote
== ' ')
287 if (c
== ')' && quote
== ' ')
290 where
= gfc_current_locus
;
294 gfc_current_locus
= old_loc
;
298 gfc_error ("Missing %qs in statement at or before %L",
299 count
> 0? ")":"(", &where
);
307 /* See if the next character is a special character that has
308 escaped by a \ via the -fbackslash option. */
311 gfc_match_special_char (gfc_char_t
*res
)
319 switch ((c
= gfc_next_char_literal (INSTRING_WARN
)))
352 /* Hexadecimal form of wide characters. */
353 len
= (c
== 'x' ? 2 : (c
== 'u' ? 4 : 8));
355 for (i
= 0; i
< len
; i
++)
357 char buf
[2] = { '\0', '\0' };
359 c
= gfc_next_char_literal (INSTRING_WARN
);
360 if (!gfc_wide_fits_in_byte (c
)
361 || !gfc_check_digit ((unsigned char) c
, 16))
364 buf
[0] = (unsigned char) c
;
366 n
+= strtol (buf
, NULL
, 16);
372 /* Unknown backslash codes are simply not expanded. */
381 /* In free form, match at least one space. Always matches in fixed
385 gfc_match_space (void)
390 if (gfc_current_form
== FORM_FIXED
)
393 old_loc
= gfc_current_locus
;
395 c
= gfc_next_ascii_char ();
396 if (!gfc_is_whitespace (c
))
398 gfc_current_locus
= old_loc
;
402 gfc_gobble_whitespace ();
408 /* Match an end of statement. End of statement is optional
409 whitespace, followed by a ';' or '\n' or comment '!'. If a
410 semicolon is found, we continue to eat whitespace and semicolons. */
423 old_loc
= gfc_current_locus
;
424 gfc_gobble_whitespace ();
426 c
= gfc_next_ascii_char ();
432 c
= gfc_next_ascii_char ();
449 gfc_current_locus
= old_loc
;
450 return (flag
) ? MATCH_YES
: MATCH_NO
;
454 /* Match a literal integer on the input, setting the value on
455 MATCH_YES. Literal ints occur in kind-parameters as well as
456 old-style character length specifications. If cnt is non-NULL it
457 will be set to the number of digits. */
460 gfc_match_small_literal_int (int *value
, int *cnt
)
466 old_loc
= gfc_current_locus
;
469 gfc_gobble_whitespace ();
470 c
= gfc_next_ascii_char ();
476 gfc_current_locus
= old_loc
;
485 old_loc
= gfc_current_locus
;
486 c
= gfc_next_ascii_char ();
491 i
= 10 * i
+ c
- '0';
496 gfc_error ("Integer too large at %C");
501 gfc_current_locus
= old_loc
;
510 /* Match a small, constant integer expression, like in a kind
511 statement. On MATCH_YES, 'value' is set. */
514 gfc_match_small_int (int *value
)
520 m
= gfc_match_expr (&expr
);
524 if (gfc_extract_int (expr
, &i
, 1))
526 gfc_free_expr (expr
);
533 /* This function is the same as the gfc_match_small_int, except that
534 we're keeping the pointer to the expr. This function could just be
535 removed and the previously mentioned one modified, though all calls
536 to it would have to be modified then (and there were a number of
537 them). Return MATCH_ERROR if fail to extract the int; otherwise,
538 return the result of gfc_match_expr(). The expr (if any) that was
539 matched is returned in the parameter expr. */
542 gfc_match_small_int_expr (int *value
, gfc_expr
**expr
)
547 m
= gfc_match_expr (expr
);
551 if (gfc_extract_int (*expr
, &i
, 1))
559 /* Matches a statement label. Uses gfc_match_small_literal_int() to
560 do most of the work. */
563 gfc_match_st_label (gfc_st_label
**label
)
569 old_loc
= gfc_current_locus
;
571 m
= gfc_match_small_literal_int (&i
, &cnt
);
577 gfc_error ("Too many digits in statement label at %C");
583 gfc_error ("Statement label at %C is zero");
587 *label
= gfc_get_st_label (i
);
592 gfc_current_locus
= old_loc
;
597 /* Match and validate a label associated with a named IF, DO or SELECT
598 statement. If the symbol does not have the label attribute, we add
599 it. We also make sure the symbol does not refer to another
600 (active) block. A matched label is pointed to by gfc_new_block. */
603 gfc_match_label (void)
605 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
608 gfc_new_block
= NULL
;
610 m
= gfc_match (" %n :", name
);
614 if (gfc_get_symbol (name
, NULL
, &gfc_new_block
))
616 gfc_error ("Label name %qs at %C is ambiguous", name
);
620 if (gfc_new_block
->attr
.flavor
== FL_LABEL
)
622 gfc_error ("Duplicate construct label %qs at %C", name
);
626 if (!gfc_add_flavor (&gfc_new_block
->attr
, FL_LABEL
,
627 gfc_new_block
->name
, NULL
))
634 /* See if the current input looks like a name of some sort. Modifies
635 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
636 Note that options.c restricts max_identifier_length to not more
637 than GFC_MAX_SYMBOL_LEN. */
640 gfc_match_name (char *buffer
)
646 old_loc
= gfc_current_locus
;
647 gfc_gobble_whitespace ();
649 c
= gfc_next_ascii_char ();
650 if (!(ISALPHA (c
) || (c
== '_' && flag_allow_leading_underscore
)))
652 /* Special cases for unary minus and plus, which allows for a sensible
653 error message for code of the form 'c = exp(-a*b) )' where an
654 extra ')' appears at the end of statement. */
655 if (!gfc_error_flag_test () && c
!= '(' && c
!= '-' && c
!= '+')
656 gfc_error ("Invalid character in name at %C");
657 gfc_current_locus
= old_loc
;
667 if (i
> gfc_option
.max_identifier_length
)
669 gfc_error ("Name at %C is too long");
673 old_loc
= gfc_current_locus
;
674 c
= gfc_next_ascii_char ();
676 while (ISALNUM (c
) || c
== '_' || (flag_dollar_ok
&& c
== '$'));
678 if (c
== '$' && !flag_dollar_ok
)
680 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
681 "allow it as an extension", &old_loc
);
686 gfc_current_locus
= old_loc
;
692 /* Match a symbol on the input. Modifies the pointer to the symbol
693 pointer if successful. */
696 gfc_match_sym_tree (gfc_symtree
**matched_symbol
, int host_assoc
)
698 char buffer
[GFC_MAX_SYMBOL_LEN
+ 1];
701 m
= gfc_match_name (buffer
);
706 return (gfc_get_ha_sym_tree (buffer
, matched_symbol
))
707 ? MATCH_ERROR
: MATCH_YES
;
709 if (gfc_get_sym_tree (buffer
, NULL
, matched_symbol
, false))
717 gfc_match_symbol (gfc_symbol
**matched_symbol
, int host_assoc
)
722 m
= gfc_match_sym_tree (&st
, host_assoc
);
727 *matched_symbol
= st
->n
.sym
;
729 *matched_symbol
= NULL
;
732 *matched_symbol
= NULL
;
737 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
738 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
742 gfc_match_intrinsic_op (gfc_intrinsic_op
*result
)
744 locus orig_loc
= gfc_current_locus
;
747 gfc_gobble_whitespace ();
748 ch
= gfc_next_ascii_char ();
753 *result
= INTRINSIC_PLUS
;
758 *result
= INTRINSIC_MINUS
;
762 if (gfc_next_ascii_char () == '=')
765 *result
= INTRINSIC_EQ
;
771 if (gfc_peek_ascii_char () == '=')
774 gfc_next_ascii_char ();
775 *result
= INTRINSIC_LE
;
779 *result
= INTRINSIC_LT
;
783 if (gfc_peek_ascii_char () == '=')
786 gfc_next_ascii_char ();
787 *result
= INTRINSIC_GE
;
791 *result
= INTRINSIC_GT
;
795 if (gfc_peek_ascii_char () == '*')
798 gfc_next_ascii_char ();
799 *result
= INTRINSIC_POWER
;
803 *result
= INTRINSIC_TIMES
;
807 ch
= gfc_peek_ascii_char ();
811 gfc_next_ascii_char ();
812 *result
= INTRINSIC_NE
;
818 gfc_next_ascii_char ();
819 *result
= INTRINSIC_CONCAT
;
823 *result
= INTRINSIC_DIVIDE
;
827 ch
= gfc_next_ascii_char ();
831 if (gfc_next_ascii_char () == 'n'
832 && gfc_next_ascii_char () == 'd'
833 && gfc_next_ascii_char () == '.')
835 /* Matched ".and.". */
836 *result
= INTRINSIC_AND
;
842 if (gfc_next_ascii_char () == 'q')
844 ch
= gfc_next_ascii_char ();
847 /* Matched ".eq.". */
848 *result
= INTRINSIC_EQ_OS
;
853 if (gfc_next_ascii_char () == '.')
855 /* Matched ".eqv.". */
856 *result
= INTRINSIC_EQV
;
864 ch
= gfc_next_ascii_char ();
867 if (gfc_next_ascii_char () == '.')
869 /* Matched ".ge.". */
870 *result
= INTRINSIC_GE_OS
;
876 if (gfc_next_ascii_char () == '.')
878 /* Matched ".gt.". */
879 *result
= INTRINSIC_GT_OS
;
886 ch
= gfc_next_ascii_char ();
889 if (gfc_next_ascii_char () == '.')
891 /* Matched ".le.". */
892 *result
= INTRINSIC_LE_OS
;
898 if (gfc_next_ascii_char () == '.')
900 /* Matched ".lt.". */
901 *result
= INTRINSIC_LT_OS
;
908 ch
= gfc_next_ascii_char ();
911 ch
= gfc_next_ascii_char ();
914 /* Matched ".ne.". */
915 *result
= INTRINSIC_NE_OS
;
920 if (gfc_next_ascii_char () == 'v'
921 && gfc_next_ascii_char () == '.')
923 /* Matched ".neqv.". */
924 *result
= INTRINSIC_NEQV
;
931 if (gfc_next_ascii_char () == 't'
932 && gfc_next_ascii_char () == '.')
934 /* Matched ".not.". */
935 *result
= INTRINSIC_NOT
;
942 if (gfc_next_ascii_char () == 'r'
943 && gfc_next_ascii_char () == '.')
945 /* Matched ".or.". */
946 *result
= INTRINSIC_OR
;
952 if (gfc_next_ascii_char () == 'o'
953 && gfc_next_ascii_char () == 'r'
954 && gfc_next_ascii_char () == '.')
956 if (!gfc_notify_std (GFC_STD_LEGACY
, ".XOR. operator at %C"))
958 /* Matched ".xor." - equivalent to ".neqv.". */
959 *result
= INTRINSIC_NEQV
;
973 gfc_current_locus
= orig_loc
;
978 /* Match a loop control phrase:
980 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
982 If the final integer expression is not present, a constant unity
983 expression is returned. We don't return MATCH_ERROR until after
984 the equals sign is seen. */
987 gfc_match_iterator (gfc_iterator
*iter
, int init_flag
)
989 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
990 gfc_expr
*var
, *e1
, *e2
, *e3
;
996 /* Match the start of an iterator without affecting the symbol table. */
998 start
= gfc_current_locus
;
999 m
= gfc_match (" %n =", name
);
1000 gfc_current_locus
= start
;
1005 m
= gfc_match_variable (&var
, 0);
1009 if (var
->symtree
->n
.sym
->attr
.dimension
)
1011 gfc_error ("Loop variable at %C cannot be an array");
1015 /* F2008, C617 & C565. */
1016 if (var
->symtree
->n
.sym
->attr
.codimension
)
1018 gfc_error ("Loop variable at %C cannot be a coarray");
1022 if (var
->ref
!= NULL
)
1024 gfc_error ("Loop variable at %C cannot be a sub-component");
1028 gfc_match_char ('=');
1030 var
->symtree
->n
.sym
->attr
.implied_index
= 1;
1032 m
= init_flag
? gfc_match_init_expr (&e1
) : gfc_match_expr (&e1
);
1035 if (m
== MATCH_ERROR
)
1038 if (gfc_match_char (',') != MATCH_YES
)
1041 m
= init_flag
? gfc_match_init_expr (&e2
) : gfc_match_expr (&e2
);
1044 if (m
== MATCH_ERROR
)
1047 if (gfc_match_char (',') != MATCH_YES
)
1049 e3
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
1053 m
= init_flag
? gfc_match_init_expr (&e3
) : gfc_match_expr (&e3
);
1054 if (m
== MATCH_ERROR
)
1058 gfc_error ("Expected a step value in iterator at %C");
1070 gfc_error ("Syntax error in iterator at %C");
1081 /* Tries to match the next non-whitespace character on the input.
1082 This subroutine does not return MATCH_ERROR. */
1085 gfc_match_char (char c
)
1089 where
= gfc_current_locus
;
1090 gfc_gobble_whitespace ();
1092 if (gfc_next_ascii_char () == c
)
1095 gfc_current_locus
= where
;
1100 /* General purpose matching subroutine. The target string is a
1101 scanf-like format string in which spaces correspond to arbitrary
1102 whitespace (including no whitespace), characters correspond to
1103 themselves. The %-codes are:
1105 %% Literal percent sign
1106 %e Expression, pointer to a pointer is set
1107 %s Symbol, pointer to the symbol is set
1108 %n Name, character buffer is set to name
1109 %t Matches end of statement.
1110 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1111 %l Matches a statement label
1112 %v Matches a variable expression (an lvalue)
1113 % Matches a required space (in free form) and optional spaces. */
1116 gfc_match (const char *target
, ...)
1118 gfc_st_label
**label
;
1127 old_loc
= gfc_current_locus
;
1128 va_start (argp
, target
);
1138 gfc_gobble_whitespace ();
1149 vp
= va_arg (argp
, void **);
1150 n
= gfc_match_expr ((gfc_expr
**) vp
);
1161 vp
= va_arg (argp
, void **);
1162 n
= gfc_match_variable ((gfc_expr
**) vp
, 0);
1173 vp
= va_arg (argp
, void **);
1174 n
= gfc_match_symbol ((gfc_symbol
**) vp
, 0);
1185 np
= va_arg (argp
, char *);
1186 n
= gfc_match_name (np
);
1197 label
= va_arg (argp
, gfc_st_label
**);
1198 n
= gfc_match_st_label (label
);
1209 ip
= va_arg (argp
, int *);
1210 n
= gfc_match_intrinsic_op ((gfc_intrinsic_op
*) ip
);
1221 if (gfc_match_eos () != MATCH_YES
)
1229 if (gfc_match_space () == MATCH_YES
)
1235 break; /* Fall through to character matcher. */
1238 gfc_internal_error ("gfc_match(): Bad match code %c", c
);
1244 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1245 expect an upper case character here! */
1246 gcc_assert (TOLOWER (c
) == c
);
1248 if (c
== gfc_next_ascii_char ())
1258 /* Clean up after a failed match. */
1259 gfc_current_locus
= old_loc
;
1260 va_start (argp
, target
);
1263 for (; matches
> 0; matches
--)
1265 while (*p
++ != '%');
1273 /* Matches that don't have to be undone */
1278 (void) va_arg (argp
, void **);
1283 vp
= va_arg (argp
, void **);
1284 gfc_free_expr ((struct gfc_expr
*)*vp
);
1297 /*********************** Statement level matching **********************/
1299 /* Matches the start of a program unit, which is the program keyword
1300 followed by an obligatory symbol. */
1303 gfc_match_program (void)
1308 m
= gfc_match ("% %s%t", &sym
);
1312 gfc_error ("Invalid form of PROGRAM statement at %C");
1316 if (m
== MATCH_ERROR
)
1319 if (!gfc_add_flavor (&sym
->attr
, FL_PROGRAM
, sym
->name
, NULL
))
1322 gfc_new_block
= sym
;
1328 /* Match a simple assignment statement. */
1331 gfc_match_assignment (void)
1333 gfc_expr
*lvalue
, *rvalue
;
1337 old_loc
= gfc_current_locus
;
1340 m
= gfc_match (" %v =", &lvalue
);
1343 gfc_current_locus
= old_loc
;
1344 gfc_free_expr (lvalue
);
1349 m
= gfc_match (" %e%t", &rvalue
);
1351 if (lvalue
->expr_type
== EXPR_CONSTANT
)
1353 /* This clobbers %len and %kind. */
1355 gfc_error ("Assignment to a constant expression at %C");
1360 gfc_current_locus
= old_loc
;
1361 gfc_free_expr (lvalue
);
1362 gfc_free_expr (rvalue
);
1366 gfc_set_sym_referenced (lvalue
->symtree
->n
.sym
);
1368 new_st
.op
= EXEC_ASSIGN
;
1369 new_st
.expr1
= lvalue
;
1370 new_st
.expr2
= rvalue
;
1372 gfc_check_do_variable (lvalue
->symtree
);
1374 if (lvalue
->ts
.type
== BT_CLASS
)
1375 gfc_find_vtab (&rvalue
->ts
);
1381 /* Match a pointer assignment statement. */
1384 gfc_match_pointer_assignment (void)
1386 gfc_expr
*lvalue
, *rvalue
;
1390 old_loc
= gfc_current_locus
;
1392 lvalue
= rvalue
= NULL
;
1393 gfc_matching_ptr_assignment
= 0;
1394 gfc_matching_procptr_assignment
= 0;
1396 m
= gfc_match (" %v =>", &lvalue
);
1403 if (lvalue
->symtree
->n
.sym
->attr
.proc_pointer
1404 || gfc_is_proc_ptr_comp (lvalue
))
1405 gfc_matching_procptr_assignment
= 1;
1407 gfc_matching_ptr_assignment
= 1;
1409 m
= gfc_match (" %e%t", &rvalue
);
1410 gfc_matching_ptr_assignment
= 0;
1411 gfc_matching_procptr_assignment
= 0;
1415 new_st
.op
= EXEC_POINTER_ASSIGN
;
1416 new_st
.expr1
= lvalue
;
1417 new_st
.expr2
= rvalue
;
1422 gfc_current_locus
= old_loc
;
1423 gfc_free_expr (lvalue
);
1424 gfc_free_expr (rvalue
);
1429 /* We try to match an easy arithmetic IF statement. This only happens
1430 when just after having encountered a simple IF statement. This code
1431 is really duplicate with parts of the gfc_match_if code, but this is
1435 match_arithmetic_if (void)
1437 gfc_st_label
*l1
, *l2
, *l3
;
1441 m
= gfc_match (" ( %e ) %l , %l , %l%t", &expr
, &l1
, &l2
, &l3
);
1445 if (!gfc_reference_st_label (l1
, ST_LABEL_TARGET
)
1446 || !gfc_reference_st_label (l2
, ST_LABEL_TARGET
)
1447 || !gfc_reference_st_label (l3
, ST_LABEL_TARGET
))
1449 gfc_free_expr (expr
);
1453 if (!gfc_notify_std (GFC_STD_F95_OBS
| GFC_STD_F2018_DEL
,
1454 "Arithmetic IF statement at %C"))
1457 new_st
.op
= EXEC_ARITHMETIC_IF
;
1458 new_st
.expr1
= expr
;
1467 /* The IF statement is a bit of a pain. First of all, there are three
1468 forms of it, the simple IF, the IF that starts a block and the
1471 There is a problem with the simple IF and that is the fact that we
1472 only have a single level of undo information on symbols. What this
1473 means is for a simple IF, we must re-match the whole IF statement
1474 multiple times in order to guarantee that the symbol table ends up
1475 in the proper state. */
1477 static match
match_simple_forall (void);
1478 static match
match_simple_where (void);
1481 gfc_match_if (gfc_statement
*if_type
)
1484 gfc_st_label
*l1
, *l2
, *l3
;
1485 locus old_loc
, old_loc2
;
1489 n
= gfc_match_label ();
1490 if (n
== MATCH_ERROR
)
1493 old_loc
= gfc_current_locus
;
1495 m
= gfc_match (" if ", &expr
);
1499 if (gfc_match_char ('(') != MATCH_YES
)
1501 gfc_error ("Missing %<(%> in IF-expression at %C");
1505 m
= gfc_match ("%e", &expr
);
1509 old_loc2
= gfc_current_locus
;
1510 gfc_current_locus
= old_loc
;
1512 if (gfc_match_parens () == MATCH_ERROR
)
1515 gfc_current_locus
= old_loc2
;
1517 if (gfc_match_char (')') != MATCH_YES
)
1519 gfc_error ("Syntax error in IF-expression at %C");
1520 gfc_free_expr (expr
);
1524 m
= gfc_match (" %l , %l , %l%t", &l1
, &l2
, &l3
);
1530 gfc_error ("Block label not appropriate for arithmetic IF "
1532 gfc_free_expr (expr
);
1536 if (!gfc_reference_st_label (l1
, ST_LABEL_TARGET
)
1537 || !gfc_reference_st_label (l2
, ST_LABEL_TARGET
)
1538 || !gfc_reference_st_label (l3
, ST_LABEL_TARGET
))
1540 gfc_free_expr (expr
);
1544 if (!gfc_notify_std (GFC_STD_F95_OBS
| GFC_STD_F2018_DEL
,
1545 "Arithmetic IF statement at %C"))
1548 new_st
.op
= EXEC_ARITHMETIC_IF
;
1549 new_st
.expr1
= expr
;
1554 *if_type
= ST_ARITHMETIC_IF
;
1558 if (gfc_match (" then%t") == MATCH_YES
)
1560 new_st
.op
= EXEC_IF
;
1561 new_st
.expr1
= expr
;
1562 *if_type
= ST_IF_BLOCK
;
1568 gfc_error ("Block label is not appropriate for IF statement at %C");
1569 gfc_free_expr (expr
);
1573 /* At this point the only thing left is a simple IF statement. At
1574 this point, n has to be MATCH_NO, so we don't have to worry about
1575 re-matching a block label. From what we've got so far, try
1576 matching an assignment. */
1578 *if_type
= ST_SIMPLE_IF
;
1580 m
= gfc_match_assignment ();
1584 gfc_free_expr (expr
);
1585 gfc_undo_symbols ();
1586 gfc_current_locus
= old_loc
;
1588 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1589 assignment was found. For MATCH_NO, continue to call the various
1591 if (m
== MATCH_ERROR
)
1594 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match. */
1596 m
= gfc_match_pointer_assignment ();
1600 gfc_free_expr (expr
);
1601 gfc_undo_symbols ();
1602 gfc_current_locus
= old_loc
;
1604 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match. */
1606 /* Look at the next keyword to see which matcher to call. Matching
1607 the keyword doesn't affect the symbol table, so we don't have to
1608 restore between tries. */
1610 #define match(string, subr, statement) \
1611 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1615 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
)
1616 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
)
1617 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
)
1618 match ("call", gfc_match_call
, ST_CALL
)
1619 match ("change team", gfc_match_change_team
, ST_CHANGE_TEAM
)
1620 match ("close", gfc_match_close
, ST_CLOSE
)
1621 match ("continue", gfc_match_continue
, ST_CONTINUE
)
1622 match ("cycle", gfc_match_cycle
, ST_CYCLE
)
1623 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
)
1624 match ("end file", gfc_match_endfile
, ST_END_FILE
)
1625 match ("end team", gfc_match_end_team
, ST_END_TEAM
)
1626 match ("error stop", gfc_match_error_stop
, ST_ERROR_STOP
)
1627 match ("event post", gfc_match_event_post
, ST_EVENT_POST
)
1628 match ("event wait", gfc_match_event_wait
, ST_EVENT_WAIT
)
1629 match ("exit", gfc_match_exit
, ST_EXIT
)
1630 match ("fail image", gfc_match_fail_image
, ST_FAIL_IMAGE
)
1631 match ("flush", gfc_match_flush
, ST_FLUSH
)
1632 match ("forall", match_simple_forall
, ST_FORALL
)
1633 match ("form team", gfc_match_form_team
, ST_FORM_TEAM
)
1634 match ("go to", gfc_match_goto
, ST_GOTO
)
1635 match ("if", match_arithmetic_if
, ST_ARITHMETIC_IF
)
1636 match ("inquire", gfc_match_inquire
, ST_INQUIRE
)
1637 match ("lock", gfc_match_lock
, ST_LOCK
)
1638 match ("nullify", gfc_match_nullify
, ST_NULLIFY
)
1639 match ("open", gfc_match_open
, ST_OPEN
)
1640 match ("pause", gfc_match_pause
, ST_NONE
)
1641 match ("print", gfc_match_print
, ST_WRITE
)
1642 match ("read", gfc_match_read
, ST_READ
)
1643 match ("return", gfc_match_return
, ST_RETURN
)
1644 match ("rewind", gfc_match_rewind
, ST_REWIND
)
1645 match ("stop", gfc_match_stop
, ST_STOP
)
1646 match ("wait", gfc_match_wait
, ST_WAIT
)
1647 match ("sync all", gfc_match_sync_all
, ST_SYNC_CALL
);
1648 match ("sync images", gfc_match_sync_images
, ST_SYNC_IMAGES
);
1649 match ("sync memory", gfc_match_sync_memory
, ST_SYNC_MEMORY
);
1650 match ("sync team", gfc_match_sync_team
, ST_SYNC_TEAM
)
1651 match ("unlock", gfc_match_unlock
, ST_UNLOCK
)
1652 match ("where", match_simple_where
, ST_WHERE
)
1653 match ("write", gfc_match_write
, ST_WRITE
)
1656 match ("type", gfc_match_print
, ST_WRITE
)
1658 /* All else has failed, so give up. See if any of the matchers has
1659 stored an error message of some sort. */
1660 if (!gfc_error_check ())
1661 gfc_error ("Syntax error in IF-clause after %C");
1663 gfc_free_expr (expr
);
1668 gfc_error ("Syntax error in IF-clause after %C");
1671 gfc_free_expr (expr
);
1675 /* At this point, we've matched the single IF and the action clause
1676 is in new_st. Rearrange things so that the IF statement appears
1679 p
= gfc_get_code (EXEC_IF
);
1680 p
->next
= XCNEW (gfc_code
);
1682 p
->next
->loc
= gfc_current_locus
;
1686 gfc_clear_new_st ();
1688 new_st
.op
= EXEC_IF
;
1697 /* Match an ELSE statement. */
1700 gfc_match_else (void)
1702 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1704 if (gfc_match_eos () == MATCH_YES
)
1707 if (gfc_match_name (name
) != MATCH_YES
1708 || gfc_current_block () == NULL
1709 || gfc_match_eos () != MATCH_YES
)
1711 gfc_error ("Invalid character(s) in ELSE statement after %C");
1715 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1717 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1718 name
, gfc_current_block ()->name
);
1726 /* Match an ELSE IF statement. */
1729 gfc_match_elseif (void)
1731 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1732 gfc_expr
*expr
, *then
;
1736 if (gfc_match_char ('(') != MATCH_YES
)
1738 gfc_error ("Missing %<(%> in ELSE IF expression at %C");
1742 m
= gfc_match (" %e ", &expr
);
1746 if (gfc_match_char (')') != MATCH_YES
)
1748 gfc_error ("Missing %<)%> in ELSE IF expression at %C");
1752 m
= gfc_match (" then ", &then
);
1754 where
= gfc_current_locus
;
1756 if (m
== MATCH_YES
&& (gfc_match_eos () == MATCH_YES
1757 || (gfc_current_block ()
1758 && gfc_match_name (name
) == MATCH_YES
)))
1761 if (gfc_match_eos () == MATCH_YES
)
1763 gfc_error ("Missing THEN in ELSE IF statement after %L", &where
);
1767 if (gfc_match_name (name
) != MATCH_YES
1768 || gfc_current_block () == NULL
1769 || gfc_match_eos () != MATCH_YES
)
1771 gfc_error ("Syntax error in ELSE IF statement after %L", &where
);
1775 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1777 gfc_error ("Label %qs after %L doesn't match IF label %qs",
1778 name
, &where
, gfc_current_block ()->name
);
1786 new_st
.op
= EXEC_IF
;
1787 new_st
.expr1
= expr
;
1791 gfc_free_expr (expr
);
1796 /* Free a gfc_iterator structure. */
1799 gfc_free_iterator (gfc_iterator
*iter
, int flag
)
1805 gfc_free_expr (iter
->var
);
1806 gfc_free_expr (iter
->start
);
1807 gfc_free_expr (iter
->end
);
1808 gfc_free_expr (iter
->step
);
1815 /* Match a CRITICAL statement. */
1817 gfc_match_critical (void)
1819 gfc_st_label
*label
= NULL
;
1821 if (gfc_match_label () == MATCH_ERROR
)
1824 if (gfc_match (" critical") != MATCH_YES
)
1827 if (gfc_match_st_label (&label
) == MATCH_ERROR
)
1830 if (gfc_match_eos () != MATCH_YES
)
1832 gfc_syntax_error (ST_CRITICAL
);
1836 if (gfc_pure (NULL
))
1838 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1842 if (gfc_find_state (COMP_DO_CONCURRENT
))
1844 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1849 gfc_unset_implicit_pure (NULL
);
1851 if (!gfc_notify_std (GFC_STD_F2008
, "CRITICAL statement at %C"))
1854 if (flag_coarray
== GFC_FCOARRAY_NONE
)
1856 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1861 if (gfc_find_state (COMP_CRITICAL
))
1863 gfc_error ("Nested CRITICAL block at %C");
1867 new_st
.op
= EXEC_CRITICAL
;
1870 && !gfc_reference_st_label (label
, ST_LABEL_TARGET
))
1877 /* Match a BLOCK statement. */
1880 gfc_match_block (void)
1884 if (gfc_match_label () == MATCH_ERROR
)
1887 if (gfc_match (" block") != MATCH_YES
)
1890 /* For this to be a correct BLOCK statement, the line must end now. */
1891 m
= gfc_match_eos ();
1892 if (m
== MATCH_ERROR
)
1901 /* Match an ASSOCIATE statement. */
1904 gfc_match_associate (void)
1906 if (gfc_match_label () == MATCH_ERROR
)
1909 if (gfc_match (" associate") != MATCH_YES
)
1912 /* Match the association list. */
1913 if (gfc_match_char ('(') != MATCH_YES
)
1915 gfc_error ("Expected association list at %C");
1918 new_st
.ext
.block
.assoc
= NULL
;
1921 gfc_association_list
* newAssoc
= gfc_get_association_list ();
1922 gfc_association_list
* a
;
1924 /* Match the next association. */
1925 if (gfc_match (" %n =>", newAssoc
->name
) != MATCH_YES
)
1927 gfc_error ("Expected association at %C");
1928 goto assocListError
;
1931 if (gfc_match (" %e", &newAssoc
->target
) != MATCH_YES
)
1933 /* Have another go, allowing for procedure pointer selectors. */
1934 gfc_matching_procptr_assignment
= 1;
1935 if (gfc_match (" %e", &newAssoc
->target
) != MATCH_YES
)
1937 gfc_error ("Invalid association target at %C");
1938 goto assocListError
;
1940 gfc_matching_procptr_assignment
= 0;
1942 newAssoc
->where
= gfc_current_locus
;
1944 /* Check that the current name is not yet in the list. */
1945 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
1946 if (!strcmp (a
->name
, newAssoc
->name
))
1948 gfc_error ("Duplicate name %qs in association at %C",
1950 goto assocListError
;
1953 /* The target expression must not be coindexed. */
1954 if (gfc_is_coindexed (newAssoc
->target
))
1956 gfc_error ("Association target at %C must not be coindexed");
1957 goto assocListError
;
1960 /* The `variable' field is left blank for now; because the target is not
1961 yet resolved, we can't use gfc_has_vector_subscript to determine it
1962 for now. This is set during resolution. */
1964 /* Put it into the list. */
1965 newAssoc
->next
= new_st
.ext
.block
.assoc
;
1966 new_st
.ext
.block
.assoc
= newAssoc
;
1968 /* Try next one or end if closing parenthesis is found. */
1969 gfc_gobble_whitespace ();
1970 if (gfc_peek_char () == ')')
1972 if (gfc_match_char (',') != MATCH_YES
)
1974 gfc_error ("Expected %<)%> or %<,%> at %C");
1984 if (gfc_match_char (')') != MATCH_YES
)
1986 /* This should never happen as we peek above. */
1990 if (gfc_match_eos () != MATCH_YES
)
1992 gfc_error ("Junk after ASSOCIATE statement at %C");
1999 gfc_free_association_list (new_st
.ext
.block
.assoc
);
2004 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
2005 an accessible derived type. */
2008 match_derived_type_spec (gfc_typespec
*ts
)
2010 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2012 gfc_symbol
*derived
, *der_type
;
2013 match m
= MATCH_YES
;
2014 gfc_actual_arglist
*decl_type_param_list
= NULL
;
2015 bool is_pdt_template
= false;
2017 old_locus
= gfc_current_locus
;
2019 if (gfc_match ("%n", name
) != MATCH_YES
)
2021 gfc_current_locus
= old_locus
;
2025 gfc_find_symbol (name
, NULL
, 1, &derived
);
2027 /* Match the PDT spec list, if there. */
2028 if (derived
&& derived
->attr
.flavor
== FL_PROCEDURE
)
2030 gfc_find_symbol (gfc_dt_upper_string (name
), NULL
, 1, &der_type
);
2031 is_pdt_template
= der_type
2032 && der_type
->attr
.flavor
== FL_DERIVED
2033 && der_type
->attr
.pdt_template
;
2036 if (is_pdt_template
)
2037 m
= gfc_match_actual_arglist (1, &decl_type_param_list
, true);
2039 if (m
== MATCH_ERROR
)
2041 gfc_free_actual_arglist (decl_type_param_list
);
2045 if (derived
&& derived
->attr
.flavor
== FL_PROCEDURE
&& derived
->attr
.generic
)
2046 derived
= gfc_find_dt_in_generic (derived
);
2048 /* If this is a PDT, find the specific instance. */
2049 if (m
== MATCH_YES
&& is_pdt_template
)
2051 gfc_namespace
*old_ns
;
2053 old_ns
= gfc_current_ns
;
2054 while (gfc_current_ns
&& gfc_current_ns
->parent
)
2055 gfc_current_ns
= gfc_current_ns
->parent
;
2057 if (type_param_spec_list
)
2058 gfc_free_actual_arglist (type_param_spec_list
);
2059 m
= gfc_get_pdt_instance (decl_type_param_list
, &der_type
,
2060 &type_param_spec_list
);
2061 gfc_free_actual_arglist (decl_type_param_list
);
2066 gcc_assert (!derived
->attr
.pdt_template
&& derived
->attr
.pdt_type
);
2067 gfc_set_sym_referenced (derived
);
2069 gfc_current_ns
= old_ns
;
2072 if (derived
&& derived
->attr
.flavor
== FL_DERIVED
)
2074 ts
->type
= BT_DERIVED
;
2075 ts
->u
.derived
= derived
;
2079 gfc_current_locus
= old_locus
;
2084 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2085 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
2086 It only includes the intrinsic types from the Fortran 2003 standard
2087 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2088 the implicit_flag is not needed, so it was removed. Derived types are
2089 identified by their name alone. */
2092 gfc_match_type_spec (gfc_typespec
*ts
)
2096 char c
, name
[GFC_MAX_SYMBOL_LEN
+ 1];
2099 gfc_gobble_whitespace ();
2100 old_locus
= gfc_current_locus
;
2102 /* If c isn't [a-z], then return immediately. */
2103 c
= gfc_peek_ascii_char ();
2107 type_param_spec_list
= NULL
;
2109 if (match_derived_type_spec (ts
) == MATCH_YES
)
2111 /* Enforce F03:C401. */
2112 if (ts
->u
.derived
->attr
.abstract
)
2114 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2115 ts
->u
.derived
->name
, &old_locus
);
2121 if (gfc_match ("integer") == MATCH_YES
)
2123 ts
->type
= BT_INTEGER
;
2124 ts
->kind
= gfc_default_integer_kind
;
2128 if (gfc_match ("double precision") == MATCH_YES
)
2131 ts
->kind
= gfc_default_double_kind
;
2135 if (gfc_match ("complex") == MATCH_YES
)
2137 ts
->type
= BT_COMPLEX
;
2138 ts
->kind
= gfc_default_complex_kind
;
2142 if (gfc_match ("character") == MATCH_YES
)
2144 ts
->type
= BT_CHARACTER
;
2146 m
= gfc_match_char_spec (ts
);
2154 /* REAL is a real pain because it can be a type, intrinsic subprogram,
2155 or list item in a type-list of an OpenMP reduction clause. Need to
2156 differentiate REAL([KIND]=scalar-int-initialization-expr) from
2157 REAL(A,[KIND]) and REAL(KIND,A). Logically, when this code was
2158 written the use of LOGICAL as a type-spec or intrinsic subprogram
2161 m
= gfc_match (" %n", name
);
2163 && (strcmp (name
, "real") == 0 || strcmp (name
, "logical") == 0))
2172 ts
->kind
= gfc_default_real_kind
;
2176 ts
->type
= BT_LOGICAL
;
2177 ts
->kind
= gfc_default_logical_kind
;
2180 gfc_gobble_whitespace ();
2182 /* Prevent REAL*4, etc. */
2183 c
= gfc_peek_ascii_char ();
2186 gfc_error ("Invalid type-spec at %C");
2190 /* Found leading colon in REAL::, a trailing ')' in for example
2191 TYPE IS (REAL), or REAL, for an OpenMP list-item. */
2192 if (c
== ':' || c
== ')' || (flag_openmp
&& c
== ','))
2195 /* Found something other than the opening '(' in REAL(... */
2199 gfc_next_char (); /* Burn the '('. */
2201 /* Look for the optional KIND=. */
2202 where
= gfc_current_locus
;
2203 m
= gfc_match ("%n", name
);
2206 gfc_gobble_whitespace ();
2207 c
= gfc_next_char ();
2210 if (strcmp(name
, "a") == 0 || strcmp(name
, "l") == 0)
2212 else if (strcmp(name
, "kind") == 0)
2218 gfc_current_locus
= where
;
2221 gfc_current_locus
= where
;
2225 m
= gfc_match_init_expr (&e
);
2226 if (m
== MATCH_NO
|| m
== MATCH_ERROR
)
2229 /* If a comma appears, it is an intrinsic subprogram. */
2230 gfc_gobble_whitespace ();
2231 c
= gfc_peek_ascii_char ();
2238 /* If ')' appears, we have REAL(initialization-expr), here check for
2239 a scalar integer initialization-expr and valid kind parameter. */
2242 if (e
->ts
.type
!= BT_INTEGER
|| e
->rank
> 0)
2248 if (e
->expr_type
!= EXPR_CONSTANT
)
2251 gfc_next_char (); /* Burn the ')'. */
2252 ts
->kind
= (int) mpz_get_si (e
->value
.integer
);
2253 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) == -1)
2255 gfc_error ("Invalid type-spec at %C");
2267 /* If a type is not matched, simply return MATCH_NO. */
2268 gfc_current_locus
= old_locus
;
2273 gfc_gobble_whitespace ();
2275 /* This prevents INTEGER*4, etc. */
2276 if (gfc_peek_ascii_char () == '*')
2278 gfc_error ("Invalid type-spec at %C");
2282 m
= gfc_match_kind_spec (ts
, false);
2284 /* No kind specifier found. */
2292 /******************** FORALL subroutines ********************/
2294 /* Free a list of FORALL iterators. */
2297 gfc_free_forall_iterator (gfc_forall_iterator
*iter
)
2299 gfc_forall_iterator
*next
;
2304 gfc_free_expr (iter
->var
);
2305 gfc_free_expr (iter
->start
);
2306 gfc_free_expr (iter
->end
);
2307 gfc_free_expr (iter
->stride
);
2314 /* Match an iterator as part of a FORALL statement. The format is:
2316 <var> = <start>:<end>[:<stride>]
2318 On MATCH_NO, the caller tests for the possibility that there is a
2319 scalar mask expression. */
2322 match_forall_iterator (gfc_forall_iterator
**result
)
2324 gfc_forall_iterator
*iter
;
2328 where
= gfc_current_locus
;
2329 iter
= XCNEW (gfc_forall_iterator
);
2331 m
= gfc_match_expr (&iter
->var
);
2335 if (gfc_match_char ('=') != MATCH_YES
2336 || iter
->var
->expr_type
!= EXPR_VARIABLE
)
2342 m
= gfc_match_expr (&iter
->start
);
2346 if (gfc_match_char (':') != MATCH_YES
)
2349 m
= gfc_match_expr (&iter
->end
);
2352 if (m
== MATCH_ERROR
)
2355 if (gfc_match_char (':') == MATCH_NO
)
2356 iter
->stride
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2359 m
= gfc_match_expr (&iter
->stride
);
2362 if (m
== MATCH_ERROR
)
2366 /* Mark the iteration variable's symbol as used as a FORALL index. */
2367 iter
->var
->symtree
->n
.sym
->forall_index
= true;
2373 gfc_error ("Syntax error in FORALL iterator at %C");
2378 gfc_current_locus
= where
;
2379 gfc_free_forall_iterator (iter
);
2384 /* Match the header of a FORALL statement. */
2387 match_forall_header (gfc_forall_iterator
**phead
, gfc_expr
**mask
)
2389 gfc_forall_iterator
*head
, *tail
, *new_iter
;
2393 gfc_gobble_whitespace ();
2398 if (gfc_match_char ('(') != MATCH_YES
)
2401 m
= match_forall_iterator (&new_iter
);
2402 if (m
== MATCH_ERROR
)
2407 head
= tail
= new_iter
;
2411 if (gfc_match_char (',') != MATCH_YES
)
2414 m
= match_forall_iterator (&new_iter
);
2415 if (m
== MATCH_ERROR
)
2420 tail
->next
= new_iter
;
2425 /* Have to have a mask expression. */
2427 m
= gfc_match_expr (&msk
);
2430 if (m
== MATCH_ERROR
)
2436 if (gfc_match_char (')') == MATCH_NO
)
2444 gfc_syntax_error (ST_FORALL
);
2447 gfc_free_expr (msk
);
2448 gfc_free_forall_iterator (head
);
2453 /* Match the rest of a simple FORALL statement that follows an
2457 match_simple_forall (void)
2459 gfc_forall_iterator
*head
;
2468 m
= match_forall_header (&head
, &mask
);
2475 m
= gfc_match_assignment ();
2477 if (m
== MATCH_ERROR
)
2481 m
= gfc_match_pointer_assignment ();
2482 if (m
== MATCH_ERROR
)
2488 c
= XCNEW (gfc_code
);
2490 c
->loc
= gfc_current_locus
;
2492 if (gfc_match_eos () != MATCH_YES
)
2495 gfc_clear_new_st ();
2496 new_st
.op
= EXEC_FORALL
;
2497 new_st
.expr1
= mask
;
2498 new_st
.ext
.forall_iterator
= head
;
2499 new_st
.block
= gfc_get_code (EXEC_FORALL
);
2500 new_st
.block
->next
= c
;
2505 gfc_syntax_error (ST_FORALL
);
2508 gfc_free_forall_iterator (head
);
2509 gfc_free_expr (mask
);
2515 /* Match a FORALL statement. */
2518 gfc_match_forall (gfc_statement
*st
)
2520 gfc_forall_iterator
*head
;
2529 m0
= gfc_match_label ();
2530 if (m0
== MATCH_ERROR
)
2533 m
= gfc_match (" forall");
2537 m
= match_forall_header (&head
, &mask
);
2538 if (m
== MATCH_ERROR
)
2543 if (gfc_match_eos () == MATCH_YES
)
2545 *st
= ST_FORALL_BLOCK
;
2546 new_st
.op
= EXEC_FORALL
;
2547 new_st
.expr1
= mask
;
2548 new_st
.ext
.forall_iterator
= head
;
2552 m
= gfc_match_assignment ();
2553 if (m
== MATCH_ERROR
)
2557 m
= gfc_match_pointer_assignment ();
2558 if (m
== MATCH_ERROR
)
2564 c
= XCNEW (gfc_code
);
2566 c
->loc
= gfc_current_locus
;
2568 gfc_clear_new_st ();
2569 new_st
.op
= EXEC_FORALL
;
2570 new_st
.expr1
= mask
;
2571 new_st
.ext
.forall_iterator
= head
;
2572 new_st
.block
= gfc_get_code (EXEC_FORALL
);
2573 new_st
.block
->next
= c
;
2579 gfc_syntax_error (ST_FORALL
);
2582 gfc_free_forall_iterator (head
);
2583 gfc_free_expr (mask
);
2584 gfc_free_statements (c
);
2589 /* Match a DO statement. */
2594 gfc_iterator iter
, *ip
;
2596 gfc_st_label
*label
;
2599 old_loc
= gfc_current_locus
;
2601 memset (&iter
, '\0', sizeof (gfc_iterator
));
2604 m
= gfc_match_label ();
2605 if (m
== MATCH_ERROR
)
2608 if (gfc_match (" do") != MATCH_YES
)
2611 m
= gfc_match_st_label (&label
);
2612 if (m
== MATCH_ERROR
)
2615 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2617 if (gfc_match_eos () == MATCH_YES
)
2619 iter
.end
= gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, true);
2620 new_st
.op
= EXEC_DO_WHILE
;
2624 /* Match an optional comma, if no comma is found, a space is obligatory. */
2625 if (gfc_match_char (',') != MATCH_YES
&& gfc_match ("% ") != MATCH_YES
)
2628 /* Check for balanced parens. */
2630 if (gfc_match_parens () == MATCH_ERROR
)
2633 if (gfc_match (" concurrent") == MATCH_YES
)
2635 gfc_forall_iterator
*head
;
2638 if (!gfc_notify_std (GFC_STD_F2008
, "DO CONCURRENT construct at %C"))
2644 m
= match_forall_header (&head
, &mask
);
2648 if (m
== MATCH_ERROR
)
2649 goto concurr_cleanup
;
2651 if (gfc_match_eos () != MATCH_YES
)
2652 goto concurr_cleanup
;
2655 && !gfc_reference_st_label (label
, ST_LABEL_DO_TARGET
))
2656 goto concurr_cleanup
;
2658 new_st
.label1
= label
;
2659 new_st
.op
= EXEC_DO_CONCURRENT
;
2660 new_st
.expr1
= mask
;
2661 new_st
.ext
.forall_iterator
= head
;
2666 gfc_syntax_error (ST_DO
);
2667 gfc_free_expr (mask
);
2668 gfc_free_forall_iterator (head
);
2672 /* See if we have a DO WHILE. */
2673 if (gfc_match (" while ( %e )%t", &iter
.end
) == MATCH_YES
)
2675 new_st
.op
= EXEC_DO_WHILE
;
2679 /* The abortive DO WHILE may have done something to the symbol
2680 table, so we start over. */
2681 gfc_undo_symbols ();
2682 gfc_current_locus
= old_loc
;
2684 gfc_match_label (); /* This won't error. */
2685 gfc_match (" do "); /* This will work. */
2687 gfc_match_st_label (&label
); /* Can't error out. */
2688 gfc_match_char (','); /* Optional comma. */
2690 m
= gfc_match_iterator (&iter
, 0);
2693 if (m
== MATCH_ERROR
)
2696 iter
.var
->symtree
->n
.sym
->attr
.implied_index
= 0;
2697 gfc_check_do_variable (iter
.var
->symtree
);
2699 if (gfc_match_eos () != MATCH_YES
)
2701 gfc_syntax_error (ST_DO
);
2705 new_st
.op
= EXEC_DO
;
2709 && !gfc_reference_st_label (label
, ST_LABEL_DO_TARGET
))
2712 new_st
.label1
= label
;
2714 if (new_st
.op
== EXEC_DO_WHILE
)
2715 new_st
.expr1
= iter
.end
;
2718 new_st
.ext
.iterator
= ip
= gfc_get_iterator ();
2725 gfc_free_iterator (&iter
, 0);
2731 /* Match an EXIT or CYCLE statement. */
2734 match_exit_cycle (gfc_statement st
, gfc_exec_op op
)
2736 gfc_state_data
*p
, *o
;
2741 if (gfc_match_eos () == MATCH_YES
)
2745 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2748 m
= gfc_match ("% %n%t", name
);
2749 if (m
== MATCH_ERROR
)
2753 gfc_syntax_error (st
);
2757 /* Find the corresponding symbol. If there's a BLOCK statement
2758 between here and the label, it is not in gfc_current_ns but a parent
2760 stree
= gfc_find_symtree_in_proc (name
, gfc_current_ns
);
2763 gfc_error ("Name %qs in %s statement at %C is unknown",
2764 name
, gfc_ascii_statement (st
));
2769 if (sym
->attr
.flavor
!= FL_LABEL
)
2771 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2772 name
, gfc_ascii_statement (st
));
2777 /* Find the loop specified by the label (or lack of a label). */
2778 for (o
= NULL
, p
= gfc_state_stack
; p
; p
= p
->previous
)
2779 if (o
== NULL
&& p
->state
== COMP_OMP_STRUCTURED_BLOCK
)
2781 else if (p
->state
== COMP_CRITICAL
)
2783 gfc_error("%s statement at %C leaves CRITICAL construct",
2784 gfc_ascii_statement (st
));
2787 else if (p
->state
== COMP_DO_CONCURRENT
2788 && (op
== EXEC_EXIT
|| (sym
&& sym
!= p
->sym
)))
2790 /* F2008, C821 & C845. */
2791 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2792 gfc_ascii_statement (st
));
2795 else if ((sym
&& sym
== p
->sym
)
2796 || (!sym
&& (p
->state
== COMP_DO
2797 || p
->state
== COMP_DO_CONCURRENT
)))
2803 gfc_error ("%s statement at %C is not within a construct",
2804 gfc_ascii_statement (st
));
2806 gfc_error ("%s statement at %C is not within construct %qs",
2807 gfc_ascii_statement (st
), sym
->name
);
2812 /* Special checks for EXIT from non-loop constructs. */
2816 case COMP_DO_CONCURRENT
:
2820 /* This is already handled above. */
2823 case COMP_ASSOCIATE
:
2827 case COMP_SELECT_TYPE
:
2828 case COMP_SELECT_RANK
:
2830 if (op
== EXEC_CYCLE
)
2832 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2833 " construct %qs", sym
->name
);
2836 gcc_assert (op
== EXEC_EXIT
);
2837 if (!gfc_notify_std (GFC_STD_F2008
, "EXIT statement with no"
2838 " do-construct-name at %C"))
2843 gfc_error ("%s statement at %C is not applicable to construct %qs",
2844 gfc_ascii_statement (st
), sym
->name
);
2850 gfc_error (is_oacc (p
)
2851 ? G_("%s statement at %C leaving OpenACC structured block")
2852 : G_("%s statement at %C leaving OpenMP structured block"),
2853 gfc_ascii_statement (st
));
2857 for (o
= p
, cnt
= 0; o
->state
== COMP_DO
&& o
->previous
!= NULL
; cnt
++)
2861 && o
->state
== COMP_OMP_STRUCTURED_BLOCK
2862 && (o
->head
->op
== EXEC_OACC_LOOP
2863 || o
->head
->op
== EXEC_OACC_PARALLEL_LOOP
))
2866 gcc_assert (o
->head
->next
!= NULL
2867 && (o
->head
->next
->op
== EXEC_DO
2868 || o
->head
->next
->op
== EXEC_DO_WHILE
)
2869 && o
->previous
!= NULL
2870 && o
->previous
->tail
->op
== o
->head
->op
);
2871 if (o
->previous
->tail
->ext
.omp_clauses
!= NULL
2872 && o
->previous
->tail
->ext
.omp_clauses
->collapse
> 1)
2873 collapse
= o
->previous
->tail
->ext
.omp_clauses
->collapse
;
2874 if (st
== ST_EXIT
&& cnt
<= collapse
)
2876 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2879 if (st
== ST_CYCLE
&& cnt
< collapse
)
2881 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2882 " !$ACC LOOP loop");
2888 && (o
->state
== COMP_OMP_STRUCTURED_BLOCK
)
2889 && (o
->head
->op
== EXEC_OMP_DO
2890 || o
->head
->op
== EXEC_OMP_PARALLEL_DO
2891 || o
->head
->op
== EXEC_OMP_SIMD
2892 || o
->head
->op
== EXEC_OMP_DO_SIMD
2893 || o
->head
->op
== EXEC_OMP_PARALLEL_DO_SIMD
))
2896 gcc_assert (o
->head
->next
!= NULL
2897 && (o
->head
->next
->op
== EXEC_DO
2898 || o
->head
->next
->op
== EXEC_DO_WHILE
)
2899 && o
->previous
!= NULL
2900 && o
->previous
->tail
->op
== o
->head
->op
);
2901 if (o
->previous
->tail
->ext
.omp_clauses
!= NULL
)
2903 if (o
->previous
->tail
->ext
.omp_clauses
->collapse
> 1)
2904 count
= o
->previous
->tail
->ext
.omp_clauses
->collapse
;
2905 if (o
->previous
->tail
->ext
.omp_clauses
->orderedc
)
2906 count
= o
->previous
->tail
->ext
.omp_clauses
->orderedc
;
2908 if (st
== ST_EXIT
&& cnt
<= count
)
2910 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2913 if (st
== ST_CYCLE
&& cnt
< count
)
2915 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2921 /* Save the first statement in the construct - needed by the backend. */
2922 new_st
.ext
.which_construct
= p
->construct
;
2930 /* Match the EXIT statement. */
2933 gfc_match_exit (void)
2935 return match_exit_cycle (ST_EXIT
, EXEC_EXIT
);
2939 /* Match the CYCLE statement. */
2942 gfc_match_cycle (void)
2944 return match_exit_cycle (ST_CYCLE
, EXEC_CYCLE
);
2948 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
2949 requirements for a stop-code differ in the standards.
2953 R840 stop-stmt is STOP [ stop-code ]
2954 R841 stop-code is scalar-char-constant
2955 or digit [ digit [ digit [ digit [ digit ] ] ] ]
2957 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
2960 R855 stop-stmt is STOP [ stop-code ]
2961 R856 allstop-stmt is ALL STOP [ stop-code ]
2962 R857 stop-code is scalar-default-char-constant-expr
2963 or scalar-int-constant-expr
2965 For free-form source code, all standards contain a statement of the form:
2967 A blank shall be used to separate names, constants, or labels from
2968 adjacent keywords, names, constants, or labels.
2970 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
2974 is valid, but it is invalid Fortran 2008. */
2977 gfc_match_stopcode (gfc_statement st
)
2983 /* Set f95 for -std=f95. */
2984 f95
= (gfc_option
.allow_std
== GFC_STD_OPT_F95
);
2986 /* Set f03 for -std=f2003. */
2987 f03
= (gfc_option
.allow_std
== GFC_STD_OPT_F03
);
2989 /* Set f08 for -std=f2008. */
2990 f08
= (gfc_option
.allow_std
== GFC_STD_OPT_F08
);
2992 /* Look for a blank between STOP and the stop-code for F2008 or later. */
2993 if (gfc_current_form
!= FORM_FIXED
&& !(f95
|| f03
))
2995 char c
= gfc_peek_ascii_char ();
2997 /* Look for end-of-statement. There is no stop-code. */
2998 if (c
== '\n' || c
== '!' || c
== ';')
3003 gfc_error ("Blank required in %s statement near %C",
3004 gfc_ascii_statement (st
));
3009 if (gfc_match_eos () != MATCH_YES
)
3014 /* First look for the F95 or F2003 digit [...] construct. */
3015 old_locus
= gfc_current_locus
;
3016 m
= gfc_match_small_int (&stopcode
);
3017 if (m
== MATCH_YES
&& (f95
|| f03
))
3021 gfc_error ("STOP code at %C cannot be negative");
3025 if (stopcode
> 99999)
3027 gfc_error ("STOP code at %C contains too many digits");
3032 /* Reset the locus and now load gfc_expr. */
3033 gfc_current_locus
= old_locus
;
3034 m
= gfc_match_expr (&e
);
3035 if (m
== MATCH_ERROR
)
3040 if (gfc_match_eos () != MATCH_YES
)
3044 if (gfc_pure (NULL
))
3046 if (st
== ST_ERROR_STOP
)
3048 if (!gfc_notify_std (GFC_STD_F2018
, "%s statement at %C in PURE "
3049 "procedure", gfc_ascii_statement (st
)))
3054 gfc_error ("%s statement not allowed in PURE procedure at %C",
3055 gfc_ascii_statement (st
));
3060 gfc_unset_implicit_pure (NULL
);
3062 if (st
== ST_STOP
&& gfc_find_state (COMP_CRITICAL
))
3064 gfc_error ("Image control statement STOP at %C in CRITICAL block");
3067 if (st
== ST_STOP
&& gfc_find_state (COMP_DO_CONCURRENT
))
3069 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
3075 gfc_simplify_expr (e
, 0);
3077 /* Test for F95 and F2003 style STOP stop-code. */
3078 if (e
->expr_type
!= EXPR_CONSTANT
&& (f95
|| f03
))
3080 gfc_error ("STOP code at %L must be a scalar CHARACTER constant "
3081 "or digit[digit[digit[digit[digit]]]]", &e
->where
);
3085 /* Use the machinery for an initialization expression to reduce the
3086 stop-code to a constant. */
3087 gfc_init_expr_flag
= true;
3088 gfc_reduce_init_expr (e
);
3089 gfc_init_expr_flag
= false;
3091 /* Test for F2008 style STOP stop-code. */
3092 if (e
->expr_type
!= EXPR_CONSTANT
&& f08
)
3094 gfc_error ("STOP code at %L must be a scalar default CHARACTER or "
3095 "INTEGER constant expression", &e
->where
);
3099 if (!(e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_INTEGER
))
3101 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
3108 gfc_error ("STOP code at %L must be scalar", &e
->where
);
3112 if (e
->ts
.type
== BT_CHARACTER
3113 && e
->ts
.kind
!= gfc_default_character_kind
)
3115 gfc_error ("STOP code at %L must be default character KIND=%d",
3116 &e
->where
, (int) gfc_default_character_kind
);
3120 if (e
->ts
.type
== BT_INTEGER
&& e
->ts
.kind
!= gfc_default_integer_kind
)
3122 gfc_error ("STOP code at %L must be default integer KIND=%d",
3123 &e
->where
, (int) gfc_default_integer_kind
);
3133 new_st
.op
= EXEC_STOP
;
3136 new_st
.op
= EXEC_ERROR_STOP
;
3139 new_st
.op
= EXEC_PAUSE
;
3146 new_st
.ext
.stop_code
= -1;
3151 gfc_syntax_error (st
);
3160 /* Match the (deprecated) PAUSE statement. */
3163 gfc_match_pause (void)
3167 m
= gfc_match_stopcode (ST_PAUSE
);
3170 if (!gfc_notify_std (GFC_STD_F95_DEL
, "PAUSE statement at %C"))
3177 /* Match the STOP statement. */
3180 gfc_match_stop (void)
3182 return gfc_match_stopcode (ST_STOP
);
3186 /* Match the ERROR STOP statement. */
3189 gfc_match_error_stop (void)
3191 if (!gfc_notify_std (GFC_STD_F2008
, "ERROR STOP statement at %C"))
3194 return gfc_match_stopcode (ST_ERROR_STOP
);
3197 /* Match EVENT POST/WAIT statement. Syntax:
3198 EVENT POST ( event-variable [, sync-stat-list] )
3199 EVENT WAIT ( event-variable [, wait-spec-list] )
3201 wait-spec-list is sync-stat-list or until-spec
3202 until-spec is UNTIL_COUNT = scalar-int-expr
3203 sync-stat is STAT= or ERRMSG=. */
3206 event_statement (gfc_statement st
)
3209 gfc_expr
*tmp
, *eventvar
, *until_count
, *stat
, *errmsg
;
3210 bool saw_until_count
, saw_stat
, saw_errmsg
;
3212 tmp
= eventvar
= until_count
= stat
= errmsg
= NULL
;
3213 saw_until_count
= saw_stat
= saw_errmsg
= false;
3215 if (gfc_pure (NULL
))
3217 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3218 st
== ST_EVENT_POST
? "POST" : "WAIT");
3222 gfc_unset_implicit_pure (NULL
);
3224 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3226 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3230 if (gfc_find_state (COMP_CRITICAL
))
3232 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3233 st
== ST_EVENT_POST
? "POST" : "WAIT");
3237 if (gfc_find_state (COMP_DO_CONCURRENT
))
3239 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3240 "block", st
== ST_EVENT_POST
? "POST" : "WAIT");
3244 if (gfc_match_char ('(') != MATCH_YES
)
3247 if (gfc_match ("%e", &eventvar
) != MATCH_YES
)
3249 m
= gfc_match_char (',');
3250 if (m
== MATCH_ERROR
)
3254 m
= gfc_match_char (')');
3262 m
= gfc_match (" stat = %v", &tmp
);
3263 if (m
== MATCH_ERROR
)
3269 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
3275 m
= gfc_match_char (',');
3283 m
= gfc_match (" errmsg = %v", &tmp
);
3284 if (m
== MATCH_ERROR
)
3290 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
3296 m
= gfc_match_char (',');
3304 m
= gfc_match (" until_count = %e", &tmp
);
3305 if (m
== MATCH_ERROR
|| st
== ST_EVENT_POST
)
3309 if (saw_until_count
)
3311 gfc_error ("Redundant UNTIL_COUNT tag found at %L",
3316 saw_until_count
= true;
3318 m
= gfc_match_char (',');
3329 if (m
== MATCH_ERROR
)
3332 if (gfc_match (" )%t") != MATCH_YES
)
3339 new_st
.op
= EXEC_EVENT_POST
;
3342 new_st
.op
= EXEC_EVENT_WAIT
;
3348 new_st
.expr1
= eventvar
;
3349 new_st
.expr2
= stat
;
3350 new_st
.expr3
= errmsg
;
3351 new_st
.expr4
= until_count
;
3356 gfc_syntax_error (st
);
3359 if (until_count
!= tmp
)
3360 gfc_free_expr (until_count
);
3362 gfc_free_expr (errmsg
);
3364 gfc_free_expr (stat
);
3366 gfc_free_expr (tmp
);
3367 gfc_free_expr (eventvar
);
3375 gfc_match_event_post (void)
3377 if (!gfc_notify_std (GFC_STD_F2018
, "EVENT POST statement at %C"))
3380 return event_statement (ST_EVENT_POST
);
3385 gfc_match_event_wait (void)
3387 if (!gfc_notify_std (GFC_STD_F2018
, "EVENT WAIT statement at %C"))
3390 return event_statement (ST_EVENT_WAIT
);
3394 /* Match a FAIL IMAGE statement. */
3397 gfc_match_fail_image (void)
3399 if (!gfc_notify_std (GFC_STD_F2018
, "FAIL IMAGE statement at %C"))
3402 if (gfc_match_char ('(') == MATCH_YES
)
3405 new_st
.op
= EXEC_FAIL_IMAGE
;
3410 gfc_syntax_error (ST_FAIL_IMAGE
);
3415 /* Match a FORM TEAM statement. */
3418 gfc_match_form_team (void)
3421 gfc_expr
*teamid
,*team
;
3423 if (!gfc_notify_std (GFC_STD_F2018
, "FORM TEAM statement at %C"))
3426 if (gfc_match_char ('(') == MATCH_NO
)
3429 new_st
.op
= EXEC_FORM_TEAM
;
3431 if (gfc_match ("%e", &teamid
) != MATCH_YES
)
3433 m
= gfc_match_char (',');
3434 if (m
== MATCH_ERROR
)
3436 if (gfc_match ("%e", &team
) != MATCH_YES
)
3439 m
= gfc_match_char (')');
3443 new_st
.expr1
= teamid
;
3444 new_st
.expr2
= team
;
3449 gfc_syntax_error (ST_FORM_TEAM
);
3454 /* Match a CHANGE TEAM statement. */
3457 gfc_match_change_team (void)
3462 if (!gfc_notify_std (GFC_STD_F2018
, "CHANGE TEAM statement at %C"))
3465 if (gfc_match_char ('(') == MATCH_NO
)
3468 new_st
.op
= EXEC_CHANGE_TEAM
;
3470 if (gfc_match ("%e", &team
) != MATCH_YES
)
3473 m
= gfc_match_char (')');
3477 new_st
.expr1
= team
;
3482 gfc_syntax_error (ST_CHANGE_TEAM
);
3487 /* Match a END TEAM statement. */
3490 gfc_match_end_team (void)
3492 if (!gfc_notify_std (GFC_STD_F2018
, "END TEAM statement at %C"))
3495 if (gfc_match_char ('(') == MATCH_YES
)
3498 new_st
.op
= EXEC_END_TEAM
;
3503 gfc_syntax_error (ST_END_TEAM
);
3508 /* Match a SYNC TEAM statement. */
3511 gfc_match_sync_team (void)
3516 if (!gfc_notify_std (GFC_STD_F2018
, "SYNC TEAM statement at %C"))
3519 if (gfc_match_char ('(') == MATCH_NO
)
3522 new_st
.op
= EXEC_SYNC_TEAM
;
3524 if (gfc_match ("%e", &team
) != MATCH_YES
)
3527 m
= gfc_match_char (')');
3531 new_st
.expr1
= team
;
3536 gfc_syntax_error (ST_SYNC_TEAM
);
3541 /* Match LOCK/UNLOCK statement. Syntax:
3542 LOCK ( lock-variable [ , lock-stat-list ] )
3543 UNLOCK ( lock-variable [ , sync-stat-list ] )
3544 where lock-stat is ACQUIRED_LOCK or sync-stat
3545 and sync-stat is STAT= or ERRMSG=. */
3548 lock_unlock_statement (gfc_statement st
)
3551 gfc_expr
*tmp
, *lockvar
, *acq_lock
, *stat
, *errmsg
;
3552 bool saw_acq_lock
, saw_stat
, saw_errmsg
;
3554 tmp
= lockvar
= acq_lock
= stat
= errmsg
= NULL
;
3555 saw_acq_lock
= saw_stat
= saw_errmsg
= false;
3557 if (gfc_pure (NULL
))
3559 gfc_error ("Image control statement %s at %C in PURE procedure",
3560 st
== ST_LOCK
? "LOCK" : "UNLOCK");
3564 gfc_unset_implicit_pure (NULL
);
3566 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3568 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3572 if (gfc_find_state (COMP_CRITICAL
))
3574 gfc_error ("Image control statement %s at %C in CRITICAL block",
3575 st
== ST_LOCK
? "LOCK" : "UNLOCK");
3579 if (gfc_find_state (COMP_DO_CONCURRENT
))
3581 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3582 st
== ST_LOCK
? "LOCK" : "UNLOCK");
3586 if (gfc_match_char ('(') != MATCH_YES
)
3589 if (gfc_match ("%e", &lockvar
) != MATCH_YES
)
3591 m
= gfc_match_char (',');
3592 if (m
== MATCH_ERROR
)
3596 m
= gfc_match_char (')');
3604 m
= gfc_match (" stat = %v", &tmp
);
3605 if (m
== MATCH_ERROR
)
3611 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
3617 m
= gfc_match_char (',');
3625 m
= gfc_match (" errmsg = %v", &tmp
);
3626 if (m
== MATCH_ERROR
)
3632 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
3638 m
= gfc_match_char (',');
3646 m
= gfc_match (" acquired_lock = %v", &tmp
);
3647 if (m
== MATCH_ERROR
|| st
== ST_UNLOCK
)
3653 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L",
3658 saw_acq_lock
= true;
3660 m
= gfc_match_char (',');
3671 if (m
== MATCH_ERROR
)
3674 if (gfc_match (" )%t") != MATCH_YES
)
3681 new_st
.op
= EXEC_LOCK
;
3684 new_st
.op
= EXEC_UNLOCK
;
3690 new_st
.expr1
= lockvar
;
3691 new_st
.expr2
= stat
;
3692 new_st
.expr3
= errmsg
;
3693 new_st
.expr4
= acq_lock
;
3698 gfc_syntax_error (st
);
3701 if (acq_lock
!= tmp
)
3702 gfc_free_expr (acq_lock
);
3704 gfc_free_expr (errmsg
);
3706 gfc_free_expr (stat
);
3708 gfc_free_expr (tmp
);
3709 gfc_free_expr (lockvar
);
3716 gfc_match_lock (void)
3718 if (!gfc_notify_std (GFC_STD_F2008
, "LOCK statement at %C"))
3721 return lock_unlock_statement (ST_LOCK
);
3726 gfc_match_unlock (void)
3728 if (!gfc_notify_std (GFC_STD_F2008
, "UNLOCK statement at %C"))
3731 return lock_unlock_statement (ST_UNLOCK
);
3735 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3736 SYNC ALL [(sync-stat-list)]
3737 SYNC MEMORY [(sync-stat-list)]
3738 SYNC IMAGES (image-set [, sync-stat-list] )
3739 with sync-stat is int-expr or *. */
3742 sync_statement (gfc_statement st
)
3745 gfc_expr
*tmp
, *imageset
, *stat
, *errmsg
;
3746 bool saw_stat
, saw_errmsg
;
3748 tmp
= imageset
= stat
= errmsg
= NULL
;
3749 saw_stat
= saw_errmsg
= false;
3751 if (gfc_pure (NULL
))
3753 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3757 gfc_unset_implicit_pure (NULL
);
3759 if (!gfc_notify_std (GFC_STD_F2008
, "SYNC statement at %C"))
3762 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3764 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3769 if (gfc_find_state (COMP_CRITICAL
))
3771 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3775 if (gfc_find_state (COMP_DO_CONCURRENT
))
3777 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3781 if (gfc_match_eos () == MATCH_YES
)
3783 if (st
== ST_SYNC_IMAGES
)
3788 if (gfc_match_char ('(') != MATCH_YES
)
3791 if (st
== ST_SYNC_IMAGES
)
3793 /* Denote '*' as imageset == NULL. */
3794 m
= gfc_match_char ('*');
3795 if (m
== MATCH_ERROR
)
3799 if (gfc_match ("%e", &imageset
) != MATCH_YES
)
3802 m
= gfc_match_char (',');
3803 if (m
== MATCH_ERROR
)
3807 m
= gfc_match_char (')');
3816 m
= gfc_match (" stat = %v", &tmp
);
3817 if (m
== MATCH_ERROR
)
3823 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
3829 if (gfc_match_char (',') == MATCH_YES
)
3836 m
= gfc_match (" errmsg = %v", &tmp
);
3837 if (m
== MATCH_ERROR
)
3843 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
3849 if (gfc_match_char (',') == MATCH_YES
)
3859 if (gfc_match (" )%t") != MATCH_YES
)
3866 new_st
.op
= EXEC_SYNC_ALL
;
3868 case ST_SYNC_IMAGES
:
3869 new_st
.op
= EXEC_SYNC_IMAGES
;
3871 case ST_SYNC_MEMORY
:
3872 new_st
.op
= EXEC_SYNC_MEMORY
;
3878 new_st
.expr1
= imageset
;
3879 new_st
.expr2
= stat
;
3880 new_st
.expr3
= errmsg
;
3885 gfc_syntax_error (st
);
3889 gfc_free_expr (stat
);
3891 gfc_free_expr (errmsg
);
3893 gfc_free_expr (tmp
);
3894 gfc_free_expr (imageset
);
3900 /* Match SYNC ALL statement. */
3903 gfc_match_sync_all (void)
3905 return sync_statement (ST_SYNC_ALL
);
3909 /* Match SYNC IMAGES statement. */
3912 gfc_match_sync_images (void)
3914 return sync_statement (ST_SYNC_IMAGES
);
3918 /* Match SYNC MEMORY statement. */
3921 gfc_match_sync_memory (void)
3923 return sync_statement (ST_SYNC_MEMORY
);
3927 /* Match a CONTINUE statement. */
3930 gfc_match_continue (void)
3932 if (gfc_match_eos () != MATCH_YES
)
3934 gfc_syntax_error (ST_CONTINUE
);
3938 new_st
.op
= EXEC_CONTINUE
;
3943 /* Match the (deprecated) ASSIGN statement. */
3946 gfc_match_assign (void)
3949 gfc_st_label
*label
;
3951 if (gfc_match (" %l", &label
) == MATCH_YES
)
3953 if (!gfc_reference_st_label (label
, ST_LABEL_UNKNOWN
))
3955 if (gfc_match (" to %v%t", &expr
) == MATCH_YES
)
3957 if (!gfc_notify_std (GFC_STD_F95_DEL
, "ASSIGN statement at %C"))
3960 expr
->symtree
->n
.sym
->attr
.assign
= 1;
3962 new_st
.op
= EXEC_LABEL_ASSIGN
;
3963 new_st
.label1
= label
;
3964 new_st
.expr1
= expr
;
3972 /* Match the GO TO statement. As a computed GOTO statement is
3973 matched, it is transformed into an equivalent SELECT block. No
3974 tree is necessary, and the resulting jumps-to-jumps are
3975 specifically optimized away by the back end. */
3978 gfc_match_goto (void)
3980 gfc_code
*head
, *tail
;
3983 gfc_st_label
*label
;
3987 if (gfc_match (" %l%t", &label
) == MATCH_YES
)
3989 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
3992 new_st
.op
= EXEC_GOTO
;
3993 new_st
.label1
= label
;
3997 /* The assigned GO TO statement. */
3999 if (gfc_match_variable (&expr
, 0) == MATCH_YES
)
4001 if (!gfc_notify_std (GFC_STD_F95_DEL
, "Assigned GOTO statement at %C"))
4004 new_st
.op
= EXEC_GOTO
;
4005 new_st
.expr1
= expr
;
4007 if (gfc_match_eos () == MATCH_YES
)
4010 /* Match label list. */
4011 gfc_match_char (',');
4012 if (gfc_match_char ('(') != MATCH_YES
)
4014 gfc_syntax_error (ST_GOTO
);
4021 m
= gfc_match_st_label (&label
);
4025 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
4029 head
= tail
= gfc_get_code (EXEC_GOTO
);
4032 tail
->block
= gfc_get_code (EXEC_GOTO
);
4036 tail
->label1
= label
;
4038 while (gfc_match_char (',') == MATCH_YES
);
4040 if (gfc_match (")%t") != MATCH_YES
)
4045 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4048 new_st
.block
= head
;
4053 /* Last chance is a computed GO TO statement. */
4054 if (gfc_match_char ('(') != MATCH_YES
)
4056 gfc_syntax_error (ST_GOTO
);
4065 m
= gfc_match_st_label (&label
);
4069 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
4073 head
= tail
= gfc_get_code (EXEC_SELECT
);
4076 tail
->block
= gfc_get_code (EXEC_SELECT
);
4080 cp
= gfc_get_case ();
4081 cp
->low
= cp
->high
= gfc_get_int_expr (gfc_default_integer_kind
,
4084 tail
->ext
.block
.case_list
= cp
;
4086 tail
->next
= gfc_get_code (EXEC_GOTO
);
4087 tail
->next
->label1
= label
;
4089 while (gfc_match_char (',') == MATCH_YES
);
4091 if (gfc_match_char (')') != MATCH_YES
)
4096 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4100 /* Get the rest of the statement. */
4101 gfc_match_char (',');
4103 if (gfc_match (" %e%t", &expr
) != MATCH_YES
)
4106 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Computed GOTO at %C"))
4109 /* At this point, a computed GOTO has been fully matched and an
4110 equivalent SELECT statement constructed. */
4112 new_st
.op
= EXEC_SELECT
;
4113 new_st
.expr1
= NULL
;
4115 /* Hack: For a "real" SELECT, the expression is in expr. We put
4116 it in expr2 so we can distinguish then and produce the correct
4118 new_st
.expr2
= expr
;
4119 new_st
.block
= head
;
4123 gfc_syntax_error (ST_GOTO
);
4125 gfc_free_statements (head
);
4130 /* Frees a list of gfc_alloc structures. */
4133 gfc_free_alloc_list (gfc_alloc
*p
)
4140 gfc_free_expr (p
->expr
);
4146 /* Match an ALLOCATE statement. */
4149 gfc_match_allocate (void)
4151 gfc_alloc
*head
, *tail
;
4152 gfc_expr
*stat
, *errmsg
, *tmp
, *source
, *mold
;
4156 locus old_locus
, deferred_locus
, assumed_locus
;
4157 bool saw_stat
, saw_errmsg
, saw_source
, saw_mold
, saw_deferred
, b1
, b2
, b3
;
4158 bool saw_unlimited
= false, saw_assumed
= false;
4161 stat
= errmsg
= source
= mold
= tmp
= NULL
;
4162 saw_stat
= saw_errmsg
= saw_source
= saw_mold
= saw_deferred
= false;
4164 if (gfc_match_char ('(') != MATCH_YES
)
4166 gfc_syntax_error (ST_ALLOCATE
);
4170 /* Match an optional type-spec. */
4171 old_locus
= gfc_current_locus
;
4172 m
= gfc_match_type_spec (&ts
);
4173 if (m
== MATCH_ERROR
)
4175 else if (m
== MATCH_NO
)
4177 char name
[GFC_MAX_SYMBOL_LEN
+ 3];
4179 if (gfc_match ("%n :: ", name
) == MATCH_YES
)
4181 gfc_error ("Error in type-spec at %L", &old_locus
);
4185 ts
.type
= BT_UNKNOWN
;
4189 /* Needed for the F2008:C631 check below. */
4190 assumed_locus
= gfc_current_locus
;
4192 if (gfc_match (" :: ") == MATCH_YES
)
4194 if (!gfc_notify_std (GFC_STD_F2003
, "typespec in ALLOCATE at %L",
4200 gfc_error ("Type-spec at %L cannot contain a deferred "
4201 "type parameter", &old_locus
);
4205 if (ts
.type
== BT_CHARACTER
)
4207 if (!ts
.u
.cl
->length
)
4210 ts
.u
.cl
->length_from_typespec
= true;
4213 if (type_param_spec_list
4214 && gfc_spec_list_type (type_param_spec_list
, NULL
)
4217 gfc_error ("The type parameter spec list in the type-spec at "
4218 "%L cannot contain DEFERRED parameters", &old_locus
);
4224 ts
.type
= BT_UNKNOWN
;
4225 gfc_current_locus
= old_locus
;
4232 head
= tail
= gfc_get_alloc ();
4235 tail
->next
= gfc_get_alloc ();
4239 m
= gfc_match_variable (&tail
->expr
, 0);
4242 if (m
== MATCH_ERROR
)
4245 if (gfc_check_do_variable (tail
->expr
->symtree
))
4248 bool impure
= gfc_impure_variable (tail
->expr
->symtree
->n
.sym
);
4249 if (impure
&& gfc_pure (NULL
))
4251 gfc_error ("Bad allocate-object at %C for a PURE procedure");
4256 gfc_unset_implicit_pure (NULL
);
4258 /* F2008:C631 (R626) A type-param-value in a type-spec shall be an
4259 asterisk if and only if each allocate-object is a dummy argument
4260 for which the corresponding type parameter is assumed. */
4262 && (tail
->expr
->ts
.deferred
4263 || (tail
->expr
->ts
.u
.cl
&& tail
->expr
->ts
.u
.cl
->length
)
4264 || tail
->expr
->symtree
->n
.sym
->attr
.dummy
== 0))
4266 gfc_error ("Incompatible allocate-object at %C for CHARACTER "
4267 "type-spec at %L", &assumed_locus
);
4271 if (tail
->expr
->ts
.deferred
)
4273 saw_deferred
= true;
4274 deferred_locus
= tail
->expr
->where
;
4277 if (gfc_find_state (COMP_DO_CONCURRENT
)
4278 || gfc_find_state (COMP_CRITICAL
))
4281 bool coarray
= tail
->expr
->symtree
->n
.sym
->attr
.codimension
;
4282 for (ref
= tail
->expr
->ref
; ref
; ref
= ref
->next
)
4283 if (ref
->type
== REF_COMPONENT
)
4284 coarray
= ref
->u
.c
.component
->attr
.codimension
;
4286 if (coarray
&& gfc_find_state (COMP_DO_CONCURRENT
))
4288 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
4291 if (coarray
&& gfc_find_state (COMP_CRITICAL
))
4293 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
4298 /* Check for F08:C628. */
4299 sym
= tail
->expr
->symtree
->n
.sym
;
4300 b1
= !(tail
->expr
->ref
4301 && (tail
->expr
->ref
->type
== REF_COMPONENT
4302 || tail
->expr
->ref
->type
== REF_ARRAY
));
4303 if (sym
&& sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
4304 b2
= !(CLASS_DATA (sym
)->attr
.allocatable
4305 || CLASS_DATA (sym
)->attr
.class_pointer
);
4307 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
4308 || sym
->attr
.proc_pointer
);
4309 b3
= sym
&& sym
->ns
&& sym
->ns
->proc_name
4310 && (sym
->ns
->proc_name
->attr
.allocatable
4311 || sym
->ns
->proc_name
->attr
.pointer
4312 || sym
->ns
->proc_name
->attr
.proc_pointer
);
4313 if (b1
&& b2
&& !b3
)
4315 gfc_error ("Allocate-object at %L is neither a data pointer "
4316 "nor an allocatable variable", &tail
->expr
->where
);
4320 /* The ALLOCATE statement had an optional typespec. Check the
4322 if (ts
.type
!= BT_UNKNOWN
)
4324 /* Enforce F03:C624. */
4325 if (!gfc_type_compatible (&tail
->expr
->ts
, &ts
))
4327 gfc_error ("Type of entity at %L is type incompatible with "
4328 "typespec", &tail
->expr
->where
);
4332 /* Enforce F03:C627. */
4333 if (ts
.kind
!= tail
->expr
->ts
.kind
&& !UNLIMITED_POLY (tail
->expr
))
4335 gfc_error ("Kind type parameter for entity at %L differs from "
4336 "the kind type parameter of the typespec",
4337 &tail
->expr
->where
);
4342 if (tail
->expr
->ts
.type
== BT_DERIVED
)
4343 tail
->expr
->ts
.u
.derived
= gfc_use_derived (tail
->expr
->ts
.u
.derived
);
4345 if (type_param_spec_list
)
4346 tail
->expr
->param_list
= gfc_copy_actual_arglist (type_param_spec_list
);
4348 saw_unlimited
= saw_unlimited
| UNLIMITED_POLY (tail
->expr
);
4350 if (gfc_peek_ascii_char () == '(' && !sym
->attr
.dimension
)
4352 gfc_error ("Shape specification for allocatable scalar at %C");
4356 if (gfc_match_char (',') != MATCH_YES
)
4361 m
= gfc_match (" stat = %v", &tmp
);
4362 if (m
== MATCH_ERROR
)
4369 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
4377 if (gfc_check_do_variable (stat
->symtree
))
4380 if (gfc_match_char (',') == MATCH_YES
)
4381 goto alloc_opt_list
;
4384 m
= gfc_match (" errmsg = %v", &tmp
);
4385 if (m
== MATCH_ERROR
)
4389 if (!gfc_notify_std (GFC_STD_F2003
, "ERRMSG tag at %L", &tmp
->where
))
4395 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
4403 if (gfc_match_char (',') == MATCH_YES
)
4404 goto alloc_opt_list
;
4407 m
= gfc_match (" source = %e", &tmp
);
4408 if (m
== MATCH_ERROR
)
4412 if (!gfc_notify_std (GFC_STD_F2003
, "SOURCE tag at %L", &tmp
->where
))
4418 gfc_error ("Redundant SOURCE tag found at %L", &tmp
->where
);
4422 /* The next 2 conditionals check C631. */
4423 if (ts
.type
!= BT_UNKNOWN
)
4425 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4426 &tmp
->where
, &old_locus
);
4431 && !gfc_notify_std (GFC_STD_F2008
, "SOURCE tag at %L"
4432 " with more than a single allocate object",
4440 if (gfc_match_char (',') == MATCH_YES
)
4441 goto alloc_opt_list
;
4444 m
= gfc_match (" mold = %e", &tmp
);
4445 if (m
== MATCH_ERROR
)
4449 if (!gfc_notify_std (GFC_STD_F2008
, "MOLD tag at %L", &tmp
->where
))
4452 /* Check F08:C636. */
4455 gfc_error ("Redundant MOLD tag found at %L", &tmp
->where
);
4459 /* Check F08:C637. */
4460 if (ts
.type
!= BT_UNKNOWN
)
4462 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4463 &tmp
->where
, &old_locus
);
4472 if (gfc_match_char (',') == MATCH_YES
)
4473 goto alloc_opt_list
;
4476 gfc_gobble_whitespace ();
4478 if (gfc_peek_char () == ')')
4482 if (gfc_match (" )%t") != MATCH_YES
)
4485 /* Check F08:C637. */
4488 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4489 &mold
->where
, &source
->where
);
4493 /* Check F03:C623, */
4494 if (saw_deferred
&& ts
.type
== BT_UNKNOWN
&& !source
&& !mold
)
4496 gfc_error ("Allocate-object at %L with a deferred type parameter "
4497 "requires either a type-spec or SOURCE tag or a MOLD tag",
4502 /* Check F03:C625, */
4503 if (saw_unlimited
&& ts
.type
== BT_UNKNOWN
&& !source
&& !mold
)
4505 for (tail
= head
; tail
; tail
= tail
->next
)
4507 if (UNLIMITED_POLY (tail
->expr
))
4508 gfc_error ("Unlimited polymorphic allocate-object at %L "
4509 "requires either a type-spec or SOURCE tag "
4510 "or a MOLD tag", &tail
->expr
->where
);
4515 new_st
.op
= EXEC_ALLOCATE
;
4516 new_st
.expr1
= stat
;
4517 new_st
.expr2
= errmsg
;
4519 new_st
.expr3
= source
;
4521 new_st
.expr3
= mold
;
4522 new_st
.ext
.alloc
.list
= head
;
4523 new_st
.ext
.alloc
.ts
= ts
;
4525 if (type_param_spec_list
)
4526 gfc_free_actual_arglist (type_param_spec_list
);
4531 gfc_syntax_error (ST_ALLOCATE
);
4534 gfc_free_expr (errmsg
);
4535 gfc_free_expr (source
);
4536 gfc_free_expr (stat
);
4537 gfc_free_expr (mold
);
4538 if (tmp
&& tmp
->expr_type
) gfc_free_expr (tmp
);
4539 gfc_free_alloc_list (head
);
4540 if (type_param_spec_list
)
4541 gfc_free_actual_arglist (type_param_spec_list
);
4546 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4547 a set of pointer assignments to intrinsic NULL(). */
4550 gfc_match_nullify (void)
4558 if (gfc_match_char ('(') != MATCH_YES
)
4563 m
= gfc_match_variable (&p
, 0);
4564 if (m
== MATCH_ERROR
)
4569 if (gfc_check_do_variable (p
->symtree
))
4573 if (gfc_is_coindexed (p
))
4575 gfc_error ("Pointer object at %C shall not be coindexed");
4579 /* build ' => NULL() '. */
4580 e
= gfc_get_null_expr (&gfc_current_locus
);
4582 /* Chain to list. */
4586 tail
->op
= EXEC_POINTER_ASSIGN
;
4590 tail
->next
= gfc_get_code (EXEC_POINTER_ASSIGN
);
4597 if (gfc_match (" )%t") == MATCH_YES
)
4599 if (gfc_match_char (',') != MATCH_YES
)
4606 gfc_syntax_error (ST_NULLIFY
);
4609 gfc_free_statements (new_st
.next
);
4611 gfc_free_expr (new_st
.expr1
);
4612 new_st
.expr1
= NULL
;
4613 gfc_free_expr (new_st
.expr2
);
4614 new_st
.expr2
= NULL
;
4619 /* Match a DEALLOCATE statement. */
4622 gfc_match_deallocate (void)
4624 gfc_alloc
*head
, *tail
;
4625 gfc_expr
*stat
, *errmsg
, *tmp
;
4628 bool saw_stat
, saw_errmsg
, b1
, b2
;
4631 stat
= errmsg
= tmp
= NULL
;
4632 saw_stat
= saw_errmsg
= false;
4634 if (gfc_match_char ('(') != MATCH_YES
)
4640 head
= tail
= gfc_get_alloc ();
4643 tail
->next
= gfc_get_alloc ();
4647 m
= gfc_match_variable (&tail
->expr
, 0);
4648 if (m
== MATCH_ERROR
)
4653 if (gfc_check_do_variable (tail
->expr
->symtree
))
4656 sym
= tail
->expr
->symtree
->n
.sym
;
4658 bool impure
= gfc_impure_variable (sym
);
4659 if (impure
&& gfc_pure (NULL
))
4661 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4666 gfc_unset_implicit_pure (NULL
);
4668 if (gfc_is_coarray (tail
->expr
)
4669 && gfc_find_state (COMP_DO_CONCURRENT
))
4671 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4675 if (gfc_is_coarray (tail
->expr
)
4676 && gfc_find_state (COMP_CRITICAL
))
4678 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4682 /* FIXME: disable the checking on derived types. */
4683 b1
= !(tail
->expr
->ref
4684 && (tail
->expr
->ref
->type
== REF_COMPONENT
4685 || tail
->expr
->ref
->type
== REF_ARRAY
));
4686 if (sym
&& sym
->ts
.type
== BT_CLASS
)
4687 b2
= !(CLASS_DATA (sym
) && (CLASS_DATA (sym
)->attr
.allocatable
4688 || CLASS_DATA (sym
)->attr
.class_pointer
));
4690 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
4691 || sym
->attr
.proc_pointer
);
4694 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4695 "nor an allocatable variable");
4699 if (gfc_match_char (',') != MATCH_YES
)
4704 m
= gfc_match (" stat = %v", &tmp
);
4705 if (m
== MATCH_ERROR
)
4711 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
4712 gfc_free_expr (tmp
);
4719 if (gfc_check_do_variable (stat
->symtree
))
4722 if (gfc_match_char (',') == MATCH_YES
)
4723 goto dealloc_opt_list
;
4726 m
= gfc_match (" errmsg = %v", &tmp
);
4727 if (m
== MATCH_ERROR
)
4731 if (!gfc_notify_std (GFC_STD_F2003
, "ERRMSG at %L", &tmp
->where
))
4736 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
4737 gfc_free_expr (tmp
);
4744 if (gfc_match_char (',') == MATCH_YES
)
4745 goto dealloc_opt_list
;
4748 gfc_gobble_whitespace ();
4750 if (gfc_peek_char () == ')')
4754 if (gfc_match (" )%t") != MATCH_YES
)
4757 new_st
.op
= EXEC_DEALLOCATE
;
4758 new_st
.expr1
= stat
;
4759 new_st
.expr2
= errmsg
;
4760 new_st
.ext
.alloc
.list
= head
;
4765 gfc_syntax_error (ST_DEALLOCATE
);
4768 gfc_free_expr (errmsg
);
4769 gfc_free_expr (stat
);
4770 gfc_free_alloc_list (head
);
4775 /* Match a RETURN statement. */
4778 gfc_match_return (void)
4782 gfc_compile_state s
;
4786 if (gfc_find_state (COMP_CRITICAL
))
4788 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4792 if (gfc_find_state (COMP_DO_CONCURRENT
))
4794 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4798 if (gfc_match_eos () == MATCH_YES
)
4801 if (!gfc_find_state (COMP_SUBROUTINE
))
4803 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4808 if (gfc_current_form
== FORM_FREE
)
4810 /* The following are valid, so we can't require a blank after the
4814 char c
= gfc_peek_ascii_char ();
4815 if (ISALPHA (c
) || ISDIGIT (c
))
4819 m
= gfc_match (" %e%t", &e
);
4822 if (m
== MATCH_ERROR
)
4825 gfc_syntax_error (ST_RETURN
);
4832 gfc_enclosing_unit (&s
);
4833 if (s
== COMP_PROGRAM
4834 && !gfc_notify_std (GFC_STD_GNU
, "RETURN statement in "
4835 "main program at %C"))
4838 new_st
.op
= EXEC_RETURN
;
4845 /* Match the call of a type-bound procedure, if CALL%var has already been
4846 matched and var found to be a derived-type variable. */
4849 match_typebound_call (gfc_symtree
* varst
)
4854 base
= gfc_get_expr ();
4855 base
->expr_type
= EXPR_VARIABLE
;
4856 base
->symtree
= varst
;
4857 base
->where
= gfc_current_locus
;
4858 gfc_set_sym_referenced (varst
->n
.sym
);
4860 m
= gfc_match_varspec (base
, 0, true, true);
4862 gfc_error ("Expected component reference at %C");
4865 gfc_free_expr (base
);
4869 if (gfc_match_eos () != MATCH_YES
)
4871 gfc_error ("Junk after CALL at %C");
4872 gfc_free_expr (base
);
4876 if (base
->expr_type
== EXPR_COMPCALL
)
4877 new_st
.op
= EXEC_COMPCALL
;
4878 else if (base
->expr_type
== EXPR_PPC
)
4879 new_st
.op
= EXEC_CALL_PPC
;
4882 gfc_error ("Expected type-bound procedure or procedure pointer component "
4884 gfc_free_expr (base
);
4887 new_st
.expr1
= base
;
4893 /* Match a CALL statement. The tricky part here are possible
4894 alternate return specifiers. We handle these by having all
4895 "subroutines" actually return an integer via a register that gives
4896 the return number. If the call specifies alternate returns, we
4897 generate code for a SELECT statement whose case clauses contain
4898 GOTOs to the various labels. */
4901 gfc_match_call (void)
4903 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4904 gfc_actual_arglist
*a
, *arglist
;
4914 m
= gfc_match ("% %n", name
);
4920 if (gfc_get_ha_sym_tree (name
, &st
))
4925 /* If this is a variable of derived-type, it probably starts a type-bound
4927 if ((sym
->attr
.flavor
!= FL_PROCEDURE
4928 || gfc_is_function_return_value (sym
, gfc_current_ns
))
4929 && (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
))
4930 return match_typebound_call (st
);
4932 /* If it does not seem to be callable (include functions so that the
4933 right association is made. They are thrown out in resolution.)
4935 if (!sym
->attr
.generic
4936 && !sym
->attr
.subroutine
4937 && !sym
->attr
.function
)
4939 if (!(sym
->attr
.external
&& !sym
->attr
.referenced
))
4941 /* ...create a symbol in this scope... */
4942 if (sym
->ns
!= gfc_current_ns
4943 && gfc_get_sym_tree (name
, NULL
, &st
, false) == 1)
4946 if (sym
!= st
->n
.sym
)
4950 /* ...and then to try to make the symbol into a subroutine. */
4951 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
4955 gfc_set_sym_referenced (sym
);
4957 if (gfc_match_eos () != MATCH_YES
)
4959 m
= gfc_match_actual_arglist (1, &arglist
);
4962 if (m
== MATCH_ERROR
)
4965 if (gfc_match_eos () != MATCH_YES
)
4969 /* If any alternate return labels were found, construct a SELECT
4970 statement that will jump to the right place. */
4973 for (a
= arglist
; a
; a
= a
->next
)
4974 if (a
->expr
== NULL
)
4982 gfc_symtree
*select_st
;
4983 gfc_symbol
*select_sym
;
4984 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4986 new_st
.next
= c
= gfc_get_code (EXEC_SELECT
);
4987 sprintf (name
, "_result_%s", sym
->name
);
4988 gfc_get_ha_sym_tree (name
, &select_st
); /* Can't fail. */
4990 select_sym
= select_st
->n
.sym
;
4991 select_sym
->ts
.type
= BT_INTEGER
;
4992 select_sym
->ts
.kind
= gfc_default_integer_kind
;
4993 gfc_set_sym_referenced (select_sym
);
4994 c
->expr1
= gfc_get_expr ();
4995 c
->expr1
->expr_type
= EXPR_VARIABLE
;
4996 c
->expr1
->symtree
= select_st
;
4997 c
->expr1
->ts
= select_sym
->ts
;
4998 c
->expr1
->where
= gfc_current_locus
;
5001 for (a
= arglist
; a
; a
= a
->next
)
5003 if (a
->expr
!= NULL
)
5006 if (!gfc_reference_st_label (a
->label
, ST_LABEL_TARGET
))
5011 c
->block
= gfc_get_code (EXEC_SELECT
);
5014 new_case
= gfc_get_case ();
5015 new_case
->high
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, i
);
5016 new_case
->low
= new_case
->high
;
5017 c
->ext
.block
.case_list
= new_case
;
5019 c
->next
= gfc_get_code (EXEC_GOTO
);
5020 c
->next
->label1
= a
->label
;
5024 new_st
.op
= EXEC_CALL
;
5025 new_st
.symtree
= st
;
5026 new_st
.ext
.actual
= arglist
;
5031 gfc_syntax_error (ST_CALL
);
5034 gfc_free_actual_arglist (arglist
);
5039 /* Given a name, return a pointer to the common head structure,
5040 creating it if it does not exist. If FROM_MODULE is nonzero, we
5041 mangle the name so that it doesn't interfere with commons defined
5042 in the using namespace.
5043 TODO: Add to global symbol tree. */
5046 gfc_get_common (const char *name
, int from_module
)
5049 static int serial
= 0;
5050 char mangled_name
[GFC_MAX_SYMBOL_LEN
+ 1];
5054 /* A use associated common block is only needed to correctly layout
5055 the variables it contains. */
5056 snprintf (mangled_name
, GFC_MAX_SYMBOL_LEN
, "_%d_%s", serial
++, name
);
5057 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, mangled_name
);
5061 st
= gfc_find_symtree (gfc_current_ns
->common_root
, name
);
5064 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, name
);
5067 if (st
->n
.common
== NULL
)
5069 st
->n
.common
= gfc_get_common_head ();
5070 st
->n
.common
->where
= gfc_current_locus
;
5071 strcpy (st
->n
.common
->name
, name
);
5074 return st
->n
.common
;
5078 /* Match a common block name. */
5080 match
match_common_name (char *name
)
5084 if (gfc_match_char ('/') == MATCH_NO
)
5090 if (gfc_match_char ('/') == MATCH_YES
)
5096 m
= gfc_match_name (name
);
5098 if (m
== MATCH_ERROR
)
5100 if (m
== MATCH_YES
&& gfc_match_char ('/') == MATCH_YES
)
5103 gfc_error ("Syntax error in common block name at %C");
5108 /* Match a COMMON statement. */
5111 gfc_match_common (void)
5113 gfc_symbol
*sym
, **head
, *tail
, *other
;
5114 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5121 /* COMMON has been matched. In free form source code, the next character
5122 needs to be whitespace or '/'. Check that here. Fixed form source
5123 code needs to be checked below. */
5124 c
= gfc_peek_ascii_char ();
5125 if (gfc_current_form
== FORM_FREE
&& !gfc_is_whitespace (c
) && c
!= '/')
5132 m
= match_common_name (name
);
5133 if (m
== MATCH_ERROR
)
5136 if (name
[0] == '\0')
5138 t
= &gfc_current_ns
->blank_common
;
5139 if (t
->head
== NULL
)
5140 t
->where
= gfc_current_locus
;
5144 t
= gfc_get_common (name
, 0);
5153 while (tail
->common_next
)
5154 tail
= tail
->common_next
;
5157 /* Grab the list of symbols. */
5160 m
= gfc_match_symbol (&sym
, 0);
5161 if (m
== MATCH_ERROR
)
5166 /* See if we know the current common block is bind(c), and if
5167 so, then see if we can check if the symbol is (which it'll
5168 need to be). This can happen if the bind(c) attr stmt was
5169 applied to the common block, and the variable(s) already
5170 defined, before declaring the common block. */
5171 if (t
->is_bind_c
== 1)
5173 if (sym
->ts
.type
!= BT_UNKNOWN
&& sym
->ts
.is_c_interop
!= 1)
5175 /* If we find an error, just print it and continue,
5176 cause it's just semantic, and we can see if there
5178 gfc_error_now ("Variable %qs at %L in common block %qs "
5179 "at %C must be declared with a C "
5180 "interoperable kind since common block "
5182 sym
->name
, &(sym
->declared_at
), t
->name
,
5186 if (sym
->attr
.is_bind_c
== 1)
5187 gfc_error_now ("Variable %qs in common block %qs at %C cannot "
5188 "be bind(c) since it is not global", sym
->name
,
5192 if (sym
->attr
.in_common
)
5194 gfc_error ("Symbol %qs at %C is already in a COMMON block",
5199 if (((sym
->value
!= NULL
&& sym
->value
->expr_type
!= EXPR_NULL
)
5200 || sym
->attr
.data
) && gfc_current_state () != COMP_BLOCK_DATA
)
5202 if (!gfc_notify_std (GFC_STD_GNU
, "Initialized symbol %qs at "
5203 "%C can only be COMMON in BLOCK DATA",
5208 /* Deal with an optional array specification after the
5210 m
= gfc_match_array_spec (&as
, true, true);
5211 if (m
== MATCH_ERROR
)
5216 if (as
->type
!= AS_EXPLICIT
)
5218 gfc_error ("Array specification for symbol %qs in COMMON "
5219 "at %C must be explicit", sym
->name
);
5223 if (!gfc_add_dimension (&sym
->attr
, sym
->name
, NULL
))
5226 if (sym
->attr
.pointer
)
5228 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5229 "POINTER array", sym
->name
);
5238 /* Add the in_common attribute, but ignore the reported errors
5239 if any, and continue matching. */
5240 gfc_add_in_common (&sym
->attr
, sym
->name
, NULL
);
5242 sym
->common_block
= t
;
5243 sym
->common_block
->refs
++;
5246 tail
->common_next
= sym
;
5252 sym
->common_head
= t
;
5254 /* Check to see if the symbol is already in an equivalence group.
5255 If it is, set the other members as being in common. */
5256 if (sym
->attr
.in_equivalence
)
5258 for (e1
= gfc_current_ns
->equiv
; e1
; e1
= e1
->next
)
5260 for (e2
= e1
; e2
; e2
= e2
->eq
)
5261 if (e2
->expr
->symtree
->n
.sym
== sym
)
5268 for (e2
= e1
; e2
; e2
= e2
->eq
)
5270 other
= e2
->expr
->symtree
->n
.sym
;
5271 if (other
->common_head
5272 && other
->common_head
!= sym
->common_head
)
5274 gfc_error ("Symbol %qs, in COMMON block %qs at "
5275 "%C is being indirectly equivalenced to "
5276 "another COMMON block %qs",
5277 sym
->name
, sym
->common_head
->name
,
5278 other
->common_head
->name
);
5281 other
->attr
.in_common
= 1;
5282 other
->common_head
= t
;
5288 gfc_gobble_whitespace ();
5289 if (gfc_match_eos () == MATCH_YES
)
5291 c
= gfc_peek_ascii_char ();
5296 /* In Fixed form source code, gfortran can end up here for an
5297 expression of the form COMMONI = RHS. This may not be an
5298 error, so return MATCH_NO. */
5299 if (gfc_current_form
== FORM_FIXED
&& c
== '=')
5301 gfc_free_array_spec (as
);
5307 gfc_match_char (',');
5309 gfc_gobble_whitespace ();
5310 if (gfc_peek_ascii_char () == '/')
5319 gfc_syntax_error (ST_COMMON
);
5322 gfc_free_array_spec (as
);
5327 /* Match a BLOCK DATA program unit. */
5330 gfc_match_block_data (void)
5332 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5336 if (!gfc_notify_std (GFC_STD_F2018_OBS
, "BLOCK DATA construct at %L",
5337 &gfc_current_locus
))
5340 if (gfc_match_eos () == MATCH_YES
)
5342 gfc_new_block
= NULL
;
5346 m
= gfc_match ("% %n%t", name
);
5350 if (gfc_get_symbol (name
, NULL
, &sym
))
5353 if (!gfc_add_flavor (&sym
->attr
, FL_BLOCK_DATA
, sym
->name
, NULL
))
5356 gfc_new_block
= sym
;
5362 /* Free a namelist structure. */
5365 gfc_free_namelist (gfc_namelist
*name
)
5369 for (; name
; name
= n
)
5377 /* Free an OpenMP namelist structure. */
5380 gfc_free_omp_namelist (gfc_omp_namelist
*name
)
5382 gfc_omp_namelist
*n
;
5384 for (; name
; name
= n
)
5386 gfc_free_expr (name
->expr
);
5389 if (name
->udr
->combiner
)
5390 gfc_free_statement (name
->udr
->combiner
);
5391 if (name
->udr
->initializer
)
5392 gfc_free_statement (name
->udr
->initializer
);
5401 /* Match a NAMELIST statement. */
5404 gfc_match_namelist (void)
5406 gfc_symbol
*group_name
, *sym
;
5410 m
= gfc_match (" / %s /", &group_name
);
5413 if (m
== MATCH_ERROR
)
5418 if (group_name
->ts
.type
!= BT_UNKNOWN
)
5420 gfc_error ("Namelist group name %qs at %C already has a basic "
5421 "type of %s", group_name
->name
,
5422 gfc_typename (&group_name
->ts
));
5426 if (group_name
->attr
.flavor
== FL_NAMELIST
5427 && group_name
->attr
.use_assoc
5428 && !gfc_notify_std (GFC_STD_GNU
, "Namelist group name %qs "
5429 "at %C already is USE associated and can"
5430 "not be respecified.", group_name
->name
))
5433 if (group_name
->attr
.flavor
!= FL_NAMELIST
5434 && !gfc_add_flavor (&group_name
->attr
, FL_NAMELIST
,
5435 group_name
->name
, NULL
))
5440 m
= gfc_match_symbol (&sym
, 1);
5443 if (m
== MATCH_ERROR
)
5446 if (sym
->attr
.in_namelist
== 0
5447 && !gfc_add_in_namelist (&sym
->attr
, sym
->name
, NULL
))
5450 /* Use gfc_error_check here, rather than goto error, so that
5451 these are the only errors for the next two lines. */
5452 if (sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
)
5454 gfc_error ("Assumed size array %qs in namelist %qs at "
5455 "%C is not allowed", sym
->name
, group_name
->name
);
5459 nl
= gfc_get_namelist ();
5463 if (group_name
->namelist
== NULL
)
5464 group_name
->namelist
= group_name
->namelist_tail
= nl
;
5467 group_name
->namelist_tail
->next
= nl
;
5468 group_name
->namelist_tail
= nl
;
5471 if (gfc_match_eos () == MATCH_YES
)
5474 m
= gfc_match_char (',');
5476 if (gfc_match_char ('/') == MATCH_YES
)
5478 m2
= gfc_match (" %s /", &group_name
);
5479 if (m2
== MATCH_YES
)
5481 if (m2
== MATCH_ERROR
)
5495 gfc_syntax_error (ST_NAMELIST
);
5502 /* Match a MODULE statement. */
5505 gfc_match_module (void)
5509 m
= gfc_match (" %s%t", &gfc_new_block
);
5513 if (!gfc_add_flavor (&gfc_new_block
->attr
, FL_MODULE
,
5514 gfc_new_block
->name
, NULL
))
5521 /* Free equivalence sets and lists. Recursively is the easiest way to
5525 gfc_free_equiv_until (gfc_equiv
*eq
, gfc_equiv
*stop
)
5530 gfc_free_equiv (eq
->eq
);
5531 gfc_free_equiv_until (eq
->next
, stop
);
5532 gfc_free_expr (eq
->expr
);
5538 gfc_free_equiv (gfc_equiv
*eq
)
5540 gfc_free_equiv_until (eq
, NULL
);
5544 /* Match an EQUIVALENCE statement. */
5547 gfc_match_equivalence (void)
5549 gfc_equiv
*eq
, *set
, *tail
;
5553 gfc_common_head
*common_head
= NULL
;
5558 /* EQUIVALENCE has been matched. After gobbling any possible whitespace,
5559 the next character needs to be '('. Check that here, and return
5560 MATCH_NO for a variable of the form equivalencej. */
5561 gfc_gobble_whitespace ();
5562 c
= gfc_peek_ascii_char ();
5570 eq
= gfc_get_equiv ();
5574 eq
->next
= gfc_current_ns
->equiv
;
5575 gfc_current_ns
->equiv
= eq
;
5577 if (gfc_match_char ('(') != MATCH_YES
)
5581 common_flag
= FALSE
;
5586 m
= gfc_match_equiv_variable (&set
->expr
);
5587 if (m
== MATCH_ERROR
)
5592 /* count the number of objects. */
5595 if (gfc_match_char ('%') == MATCH_YES
)
5597 gfc_error ("Derived type component %C is not a "
5598 "permitted EQUIVALENCE member");
5602 for (ref
= set
->expr
->ref
; ref
; ref
= ref
->next
)
5603 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
== AR_SECTION
)
5605 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5606 "be an array section");
5610 sym
= set
->expr
->symtree
->n
.sym
;
5612 if (!gfc_add_in_equivalence (&sym
->attr
, sym
->name
, NULL
))
5615 if (sym
->attr
.in_common
)
5618 common_head
= sym
->common_head
;
5621 if (gfc_match_char (')') == MATCH_YES
)
5624 if (gfc_match_char (',') != MATCH_YES
)
5627 set
->eq
= gfc_get_equiv ();
5633 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5637 /* If one of the members of an equivalence is in common, then
5638 mark them all as being in common. Before doing this, check
5639 that members of the equivalence group are not in different
5642 for (set
= eq
; set
; set
= set
->eq
)
5644 sym
= set
->expr
->symtree
->n
.sym
;
5645 if (sym
->common_head
&& sym
->common_head
!= common_head
)
5647 gfc_error ("Attempt to indirectly overlap COMMON "
5648 "blocks %s and %s by EQUIVALENCE at %C",
5649 sym
->common_head
->name
, common_head
->name
);
5652 sym
->attr
.in_common
= 1;
5653 sym
->common_head
= common_head
;
5656 if (gfc_match_eos () == MATCH_YES
)
5658 if (gfc_match_char (',') != MATCH_YES
)
5660 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5665 if (!gfc_notify_std (GFC_STD_F2018_OBS
, "EQUIVALENCE statement at %C"))
5671 gfc_syntax_error (ST_EQUIVALENCE
);
5677 gfc_free_equiv (gfc_current_ns
->equiv
);
5678 gfc_current_ns
->equiv
= eq
;
5684 /* Check that a statement function is not recursive. This is done by looking
5685 for the statement function symbol(sym) by looking recursively through its
5686 expression(e). If a reference to sym is found, true is returned.
5687 12.5.4 requires that any variable of function that is implicitly typed
5688 shall have that type confirmed by any subsequent type declaration. The
5689 implicit typing is conveniently done here. */
5691 recursive_stmt_fcn (gfc_expr
*, gfc_symbol
*);
5694 check_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
5700 switch (e
->expr_type
)
5703 if (e
->symtree
== NULL
)
5706 /* Check the name before testing for nested recursion! */
5707 if (sym
->name
== e
->symtree
->n
.sym
->name
)
5710 /* Catch recursion via other statement functions. */
5711 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
5712 && e
->symtree
->n
.sym
->value
5713 && recursive_stmt_fcn (e
->symtree
->n
.sym
->value
, sym
))
5716 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
5717 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
5722 if (e
->symtree
&& sym
->name
== e
->symtree
->n
.sym
->name
)
5725 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
5726 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
5738 recursive_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
)
5740 return gfc_traverse_expr (e
, sym
, check_stmt_fcn
, 0);
5744 /* Match a statement function declaration. It is so easy to match
5745 non-statement function statements with a MATCH_ERROR as opposed to
5746 MATCH_NO that we suppress error message in most cases. */
5749 gfc_match_st_function (void)
5751 gfc_error_buffer old_error
;
5755 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5758 gfc_formal_arglist
*ptr
;
5760 /* Read the possible statement function name, and then check to see if
5761 a symbol is already present in the namespace. Record if it is a
5762 function and whether it has been referenced. */
5765 old_locus
= gfc_current_locus
;
5766 m
= gfc_match_name (name
);
5769 gfc_find_symbol (name
, NULL
, 1, &sym
);
5770 if (sym
&& sym
->attr
.function
&& !sym
->attr
.referenced
)
5777 gfc_current_locus
= old_locus
;
5778 m
= gfc_match_symbol (&sym
, 0);
5782 gfc_push_error (&old_error
);
5784 if (!gfc_add_procedure (&sym
->attr
, PROC_ST_FUNCTION
, sym
->name
, NULL
))
5787 if (gfc_match_formal_arglist (sym
, 1, 0) != MATCH_YES
)
5790 m
= gfc_match (" = %e%t", &expr
);
5794 gfc_free_error (&old_error
);
5796 if (m
== MATCH_ERROR
)
5799 if (recursive_stmt_fcn (expr
, sym
))
5801 gfc_error ("Statement function at %L is recursive", &expr
->where
);
5805 if (fcn
&& ptr
!= sym
->formal
)
5807 gfc_error ("Statement function %qs at %L conflicts with function name",
5808 sym
->name
, &expr
->where
);
5814 if ((gfc_current_state () == COMP_FUNCTION
5815 || gfc_current_state () == COMP_SUBROUTINE
)
5816 && gfc_state_stack
->previous
->state
== COMP_INTERFACE
)
5818 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
5823 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Statement function at %C"))
5829 gfc_pop_error (&old_error
);
5834 /* Match an assignment to a pointer function (F2008). This could, in
5835 general be ambiguous with a statement function. In this implementation
5836 it remains so if it is the first statement after the specification
5840 gfc_match_ptr_fcn_assign (void)
5842 gfc_error_buffer old_error
;
5847 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5849 old_loc
= gfc_current_locus
;
5850 m
= gfc_match_name (name
);
5854 gfc_find_symbol (name
, NULL
, 1, &sym
);
5855 if (sym
&& sym
->attr
.flavor
!= FL_PROCEDURE
)
5858 gfc_push_error (&old_error
);
5860 if (sym
&& sym
->attr
.function
)
5861 goto match_actual_arglist
;
5863 gfc_current_locus
= old_loc
;
5864 m
= gfc_match_symbol (&sym
, 0);
5868 if (!gfc_add_procedure (&sym
->attr
, PROC_UNKNOWN
, sym
->name
, NULL
))
5871 match_actual_arglist
:
5872 gfc_current_locus
= old_loc
;
5873 m
= gfc_match (" %e", &expr
);
5877 new_st
.op
= EXEC_ASSIGN
;
5878 new_st
.expr1
= expr
;
5881 m
= gfc_match (" = %e%t", &expr
);
5885 new_st
.expr2
= expr
;
5889 gfc_pop_error (&old_error
);
5894 /***************** SELECT CASE subroutines ******************/
5896 /* Free a single case structure. */
5899 free_case (gfc_case
*p
)
5901 if (p
->low
== p
->high
)
5903 gfc_free_expr (p
->low
);
5904 gfc_free_expr (p
->high
);
5909 /* Free a list of case structures. */
5912 gfc_free_case_list (gfc_case
*p
)
5924 /* Match a single case selector. Combining the requirements of F08:C830
5925 and F08:C832 (R838) means that the case-value must have either CHARACTER,
5926 INTEGER, or LOGICAL type. */
5929 match_case_selector (gfc_case
**cp
)
5934 c
= gfc_get_case ();
5935 c
->where
= gfc_current_locus
;
5937 if (gfc_match_char (':') == MATCH_YES
)
5939 m
= gfc_match_init_expr (&c
->high
);
5942 if (m
== MATCH_ERROR
)
5945 if (c
->high
->ts
.type
!= BT_LOGICAL
&& c
->high
->ts
.type
!= BT_INTEGER
5946 && c
->high
->ts
.type
!= BT_CHARACTER
)
5948 gfc_error ("Expression in CASE selector at %L cannot be %s",
5949 &c
->high
->where
, gfc_typename (&c
->high
->ts
));
5955 m
= gfc_match_init_expr (&c
->low
);
5956 if (m
== MATCH_ERROR
)
5961 if (c
->low
->ts
.type
!= BT_LOGICAL
&& c
->low
->ts
.type
!= BT_INTEGER
5962 && c
->low
->ts
.type
!= BT_CHARACTER
)
5964 gfc_error ("Expression in CASE selector at %L cannot be %s",
5965 &c
->low
->where
, gfc_typename (&c
->low
->ts
));
5969 /* If we're not looking at a ':' now, make a range out of a single
5970 target. Else get the upper bound for the case range. */
5971 if (gfc_match_char (':') != MATCH_YES
)
5975 m
= gfc_match_init_expr (&c
->high
);
5976 if (m
== MATCH_ERROR
)
5978 /* MATCH_NO is fine. It's OK if nothing is there! */
5986 gfc_error ("Expected initialization expression in CASE at %C");
5994 /* Match the end of a case statement. */
5997 match_case_eos (void)
5999 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6002 if (gfc_match_eos () == MATCH_YES
)
6005 /* If the case construct doesn't have a case-construct-name, we
6006 should have matched the EOS. */
6007 if (!gfc_current_block ())
6010 gfc_gobble_whitespace ();
6012 m
= gfc_match_name (name
);
6016 if (strcmp (name
, gfc_current_block ()->name
) != 0)
6018 gfc_error ("Expected block name %qs of SELECT construct at %C",
6019 gfc_current_block ()->name
);
6023 return gfc_match_eos ();
6027 /* Match a SELECT statement. */
6030 gfc_match_select (void)
6035 m
= gfc_match_label ();
6036 if (m
== MATCH_ERROR
)
6039 m
= gfc_match (" select case ( %e )%t", &expr
);
6043 new_st
.op
= EXEC_SELECT
;
6044 new_st
.expr1
= expr
;
6050 /* Transfer the selector typespec to the associate name. */
6053 copy_ts_from_selector_to_associate (gfc_expr
*associate
, gfc_expr
*selector
)
6056 gfc_symbol
*assoc_sym
;
6059 assoc_sym
= associate
->symtree
->n
.sym
;
6061 /* At this stage the expression rank and arrayspec dimensions have
6062 not been completely sorted out. We must get the expr2->rank
6063 right here, so that the correct class container is obtained. */
6064 ref
= selector
->ref
;
6065 while (ref
&& ref
->next
)
6068 if (selector
->ts
.type
== BT_CLASS
&& CLASS_DATA (selector
)->as
6069 && CLASS_DATA (selector
)->as
->type
== AS_ASSUMED_RANK
)
6071 assoc_sym
->attr
.dimension
= 1;
6072 assoc_sym
->as
= gfc_copy_array_spec (CLASS_DATA (selector
)->as
);
6073 goto build_class_sym
;
6075 else if (selector
->ts
.type
== BT_CLASS
&& CLASS_DATA (selector
)->as
6076 && ref
&& ref
->type
== REF_ARRAY
)
6078 /* Ensure that the array reference type is set. We cannot use
6079 gfc_resolve_expr at this point, so the usable parts of
6080 resolve.c(resolve_array_ref) are employed to do it. */
6081 if (ref
->u
.ar
.type
== AR_UNKNOWN
)
6083 ref
->u
.ar
.type
= AR_ELEMENT
;
6084 for (int i
= 0; i
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; i
++)
6085 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_RANGE
6086 || ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
6087 || (ref
->u
.ar
.dimen_type
[i
] == DIMEN_UNKNOWN
6088 && ref
->u
.ar
.start
[i
] && ref
->u
.ar
.start
[i
]->rank
))
6090 ref
->u
.ar
.type
= AR_SECTION
;
6095 if (ref
->u
.ar
.type
== AR_FULL
)
6096 selector
->rank
= CLASS_DATA (selector
)->as
->rank
;
6097 else if (ref
->u
.ar
.type
== AR_SECTION
)
6098 selector
->rank
= ref
->u
.ar
.dimen
;
6102 rank
= selector
->rank
;
6107 for (int i
= 0; i
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; i
++)
6108 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_ELEMENT
6109 || (ref
->u
.ar
.dimen_type
[i
] == DIMEN_UNKNOWN
6110 && ref
->u
.ar
.end
[i
] == NULL
6111 && ref
->u
.ar
.stride
[i
] == NULL
))
6116 assoc_sym
->attr
.dimension
= 1;
6117 assoc_sym
->as
= gfc_get_array_spec ();
6118 assoc_sym
->as
->rank
= rank
;
6119 assoc_sym
->as
->type
= AS_DEFERRED
;
6122 assoc_sym
->as
= NULL
;
6125 assoc_sym
->as
= NULL
;
6128 if (selector
->ts
.type
== BT_CLASS
)
6130 /* The correct class container has to be available. */
6131 assoc_sym
->ts
.type
= BT_CLASS
;
6132 assoc_sym
->ts
.u
.derived
= CLASS_DATA (selector
)->ts
.u
.derived
;
6133 assoc_sym
->attr
.pointer
= 1;
6134 gfc_build_class_symbol (&assoc_sym
->ts
, &assoc_sym
->attr
, &assoc_sym
->as
);
6139 /* Push the current selector onto the SELECT TYPE stack. */
6142 select_type_push (gfc_symbol
*sel
)
6144 gfc_select_type_stack
*top
= gfc_get_select_type_stack ();
6145 top
->selector
= sel
;
6147 top
->prev
= select_type_stack
;
6149 select_type_stack
= top
;
6153 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
6155 static gfc_symtree
*
6156 select_intrinsic_set_tmp (gfc_typespec
*ts
)
6158 char name
[GFC_MAX_SYMBOL_LEN
];
6160 HOST_WIDE_INT charlen
= 0;
6161 gfc_symbol
*selector
= select_type_stack
->selector
;
6164 if (ts
->type
== BT_CLASS
|| ts
->type
== BT_DERIVED
)
6167 if (selector
->ts
.type
== BT_CLASS
&& !selector
->attr
.class_ok
)
6170 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6171 the values correspond to SELECT rank cases. */
6172 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
6173 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
6174 charlen
= gfc_mpz_get_hwi (ts
->u
.cl
->length
->value
.integer
);
6176 if (ts
->type
!= BT_CHARACTER
)
6177 sprintf (name
, "__tmp_%s_%d", gfc_basic_typename (ts
->type
),
6180 snprintf (name
, sizeof (name
),
6181 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC
"_%d",
6182 gfc_basic_typename (ts
->type
), charlen
, ts
->kind
);
6184 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
6186 gfc_add_type (sym
, ts
, NULL
);
6188 /* Copy across the array spec to the selector. */
6189 if (selector
->ts
.type
== BT_CLASS
6190 && (CLASS_DATA (selector
)->attr
.dimension
6191 || CLASS_DATA (selector
)->attr
.codimension
))
6193 sym
->attr
.pointer
= 1;
6194 sym
->attr
.dimension
= CLASS_DATA (selector
)->attr
.dimension
;
6195 sym
->attr
.codimension
= CLASS_DATA (selector
)->attr
.codimension
;
6196 sym
->as
= gfc_copy_array_spec (CLASS_DATA (selector
)->as
);
6199 gfc_set_sym_referenced (sym
);
6200 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, name
, NULL
);
6201 sym
->attr
.select_type_temporary
= 1;
6207 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
6210 select_type_set_tmp (gfc_typespec
*ts
)
6212 char name
[GFC_MAX_SYMBOL_LEN
];
6213 gfc_symtree
*tmp
= NULL
;
6214 gfc_symbol
*selector
= select_type_stack
->selector
;
6219 select_type_stack
->tmp
= NULL
;
6223 tmp
= select_intrinsic_set_tmp (ts
);
6230 if (ts
->type
== BT_CLASS
)
6231 sprintf (name
, "__tmp_class_%s", ts
->u
.derived
->name
);
6233 sprintf (name
, "__tmp_type_%s", ts
->u
.derived
->name
);
6235 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
6237 gfc_add_type (sym
, ts
, NULL
);
6239 if (selector
->ts
.type
== BT_CLASS
&& selector
->attr
.class_ok
)
6242 = CLASS_DATA (selector
)->attr
.class_pointer
;
6244 /* Copy across the array spec to the selector. */
6245 if (CLASS_DATA (selector
)->attr
.dimension
6246 || CLASS_DATA (selector
)->attr
.codimension
)
6249 = CLASS_DATA (selector
)->attr
.dimension
;
6250 sym
->attr
.codimension
6251 = CLASS_DATA (selector
)->attr
.codimension
;
6253 = gfc_copy_array_spec (CLASS_DATA (selector
)->as
);
6257 gfc_set_sym_referenced (sym
);
6258 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, name
, NULL
);
6259 sym
->attr
.select_type_temporary
= 1;
6261 if (ts
->type
== BT_CLASS
)
6262 gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
6268 /* Add an association for it, so the rest of the parser knows it is
6269 an associate-name. The target will be set during resolution. */
6270 sym
->assoc
= gfc_get_association_list ();
6271 sym
->assoc
->dangling
= 1;
6272 sym
->assoc
->st
= tmp
;
6274 select_type_stack
->tmp
= tmp
;
6278 /* Match a SELECT TYPE statement. */
6281 gfc_match_select_type (void)
6283 gfc_expr
*expr1
, *expr2
= NULL
;
6285 char name
[GFC_MAX_SYMBOL_LEN
];
6288 gfc_namespace
*ns
= gfc_current_ns
;
6290 m
= gfc_match_label ();
6291 if (m
== MATCH_ERROR
)
6294 m
= gfc_match (" select type ( ");
6298 if (gfc_current_state() == COMP_MODULE
6299 || gfc_current_state() == COMP_SUBMODULE
)
6301 gfc_error ("SELECT TYPE at %C cannot appear in this scope");
6305 gfc_current_ns
= gfc_build_block_ns (ns
);
6306 m
= gfc_match (" %n => %e", name
, &expr2
);
6309 expr1
= gfc_get_expr ();
6310 expr1
->expr_type
= EXPR_VARIABLE
;
6311 expr1
->where
= expr2
->where
;
6312 if (gfc_get_sym_tree (name
, NULL
, &expr1
->symtree
, false))
6318 sym
= expr1
->symtree
->n
.sym
;
6319 if (expr2
->ts
.type
== BT_UNKNOWN
)
6320 sym
->attr
.untyped
= 1;
6322 copy_ts_from_selector_to_associate (expr1
, expr2
);
6324 sym
->attr
.flavor
= FL_VARIABLE
;
6325 sym
->attr
.referenced
= 1;
6326 sym
->attr
.class_ok
= 1;
6330 m
= gfc_match (" %e ", &expr1
);
6333 std::swap (ns
, gfc_current_ns
);
6334 gfc_free_namespace (ns
);
6339 m
= gfc_match (" )%t");
6342 gfc_error ("parse error in SELECT TYPE statement at %C");
6346 /* This ghastly expression seems to be needed to distinguish a CLASS
6347 array, which can have a reference, from other expressions that
6348 have references, such as derived type components, and are not
6349 allowed by the standard.
6350 TODO: see if it is sufficient to exclude component and substring
6352 class_array
= (expr1
->expr_type
== EXPR_VARIABLE
6353 && expr1
->ts
.type
== BT_CLASS
6354 && CLASS_DATA (expr1
)
6355 && (strcmp (CLASS_DATA (expr1
)->name
, "_data") == 0)
6356 && (CLASS_DATA (expr1
)->attr
.dimension
6357 || CLASS_DATA (expr1
)->attr
.codimension
)
6359 && expr1
->ref
->type
== REF_ARRAY
6360 && expr1
->ref
->u
.ar
.type
== AR_FULL
6361 && expr1
->ref
->next
== NULL
);
6363 /* Check for F03:C811 (F08:C835). */
6364 if (!expr2
&& (expr1
->expr_type
!= EXPR_VARIABLE
6365 || (!class_array
&& expr1
->ref
!= NULL
)))
6367 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
6368 "use associate-name=>");
6373 new_st
.op
= EXEC_SELECT_TYPE
;
6374 new_st
.expr1
= expr1
;
6375 new_st
.expr2
= expr2
;
6376 new_st
.ext
.block
.ns
= gfc_current_ns
;
6378 select_type_push (expr1
->symtree
->n
.sym
);
6379 gfc_current_ns
= ns
;
6384 gfc_free_expr (expr1
);
6385 gfc_free_expr (expr2
);
6386 gfc_undo_symbols ();
6387 std::swap (ns
, gfc_current_ns
);
6388 gfc_free_namespace (ns
);
6393 /* Set the temporary for the current intrinsic SELECT RANK selector. */
6396 select_rank_set_tmp (gfc_typespec
*ts
, int *case_value
)
6398 char name
[2 * GFC_MAX_SYMBOL_LEN
];
6399 char tname
[GFC_MAX_SYMBOL_LEN
];
6401 gfc_symbol
*selector
= select_type_stack
->selector
;
6404 HOST_WIDE_INT charlen
= 0;
6406 if (case_value
== NULL
)
6409 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
6410 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
6411 charlen
= gfc_mpz_get_hwi (ts
->u
.cl
->length
->value
.integer
);
6413 if (ts
->type
== BT_CLASS
)
6414 sprintf (tname
, "class_%s", ts
->u
.derived
->name
);
6415 else if (ts
->type
== BT_DERIVED
)
6416 sprintf (tname
, "type_%s", ts
->u
.derived
->name
);
6417 else if (ts
->type
!= BT_CHARACTER
)
6418 sprintf (tname
, "%s_%d", gfc_basic_typename (ts
->type
), ts
->kind
);
6420 sprintf (tname
, "%s_" HOST_WIDE_INT_PRINT_DEC
"_%d",
6421 gfc_basic_typename (ts
->type
), charlen
, ts
->kind
);
6423 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6424 the values correspond to SELECT rank cases. */
6425 if (*case_value
>=0)
6426 sprintf (name
, "__tmp_%s_rank_%d", tname
, *case_value
);
6428 sprintf (name
, "__tmp_%s_rank_m%d", tname
, -*case_value
);
6430 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
6434 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
6436 gfc_add_type (sym
, ts
, NULL
);
6438 /* Copy across the array spec to the selector. */
6439 if (selector
->ts
.type
== BT_CLASS
)
6441 sym
->ts
.u
.derived
= CLASS_DATA (selector
)->ts
.u
.derived
;
6442 sym
->attr
.pointer
= CLASS_DATA (selector
)->attr
.pointer
;
6443 sym
->attr
.allocatable
= CLASS_DATA (selector
)->attr
.allocatable
;
6444 sym
->attr
.target
= CLASS_DATA (selector
)->attr
.target
;
6445 sym
->attr
.class_ok
= 0;
6446 if (case_value
&& *case_value
!= 0)
6448 sym
->attr
.dimension
= 1;
6449 sym
->as
= gfc_copy_array_spec (CLASS_DATA (selector
)->as
);
6450 if (*case_value
> 0)
6452 sym
->as
->type
= AS_DEFERRED
;
6453 sym
->as
->rank
= *case_value
;
6455 else if (*case_value
== -1)
6457 sym
->as
->type
= AS_ASSUMED_SIZE
;
6464 sym
->attr
.pointer
= selector
->attr
.pointer
;
6465 sym
->attr
.allocatable
= selector
->attr
.allocatable
;
6466 sym
->attr
.target
= selector
->attr
.target
;
6467 if (case_value
&& *case_value
!= 0)
6469 sym
->attr
.dimension
= 1;
6470 sym
->as
= gfc_copy_array_spec (selector
->as
);
6471 if (*case_value
> 0)
6473 sym
->as
->type
= AS_DEFERRED
;
6474 sym
->as
->rank
= *case_value
;
6476 else if (*case_value
== -1)
6478 sym
->as
->type
= AS_ASSUMED_SIZE
;
6484 gfc_set_sym_referenced (sym
);
6485 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, name
, NULL
);
6486 sym
->attr
.select_type_temporary
= 1;
6488 sym
->attr
.select_rank_temporary
= 1;
6490 if (ts
->type
== BT_CLASS
)
6491 gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
6493 /* Add an association for it, so the rest of the parser knows it is
6494 an associate-name. The target will be set during resolution. */
6495 sym
->assoc
= gfc_get_association_list ();
6496 sym
->assoc
->dangling
= 1;
6497 sym
->assoc
->st
= tmp
;
6499 select_type_stack
->tmp
= tmp
;
6503 /* Match a SELECT RANK statement. */
6506 gfc_match_select_rank (void)
6508 gfc_expr
*expr1
, *expr2
= NULL
;
6510 char name
[GFC_MAX_SYMBOL_LEN
];
6511 gfc_symbol
*sym
, *sym2
;
6512 gfc_namespace
*ns
= gfc_current_ns
;
6513 gfc_array_spec
*as
= NULL
;
6515 m
= gfc_match_label ();
6516 if (m
== MATCH_ERROR
)
6519 m
= gfc_match (" select rank ( ");
6523 if (!gfc_notify_std (GFC_STD_F2018
, "SELECT RANK statement at %C"))
6526 gfc_current_ns
= gfc_build_block_ns (ns
);
6527 m
= gfc_match (" %n => %e", name
, &expr2
);
6530 expr1
= gfc_get_expr ();
6531 expr1
->expr_type
= EXPR_VARIABLE
;
6532 expr1
->where
= expr2
->where
;
6533 expr1
->ref
= gfc_copy_ref (expr2
->ref
);
6534 if (gfc_get_sym_tree (name
, NULL
, &expr1
->symtree
, false))
6540 sym
= expr1
->symtree
->n
.sym
;
6544 sym2
= expr2
->symtree
->n
.sym
;
6545 as
= sym2
->ts
.type
== BT_CLASS
? CLASS_DATA (sym2
)->as
: sym2
->as
;
6548 if (expr2
->expr_type
!= EXPR_VARIABLE
6549 || !(as
&& as
->type
== AS_ASSUMED_RANK
))
6551 gfc_error ("The SELECT RANK selector at %C must be an assumed "
6557 if (expr2
->ts
.type
== BT_CLASS
)
6559 copy_ts_from_selector_to_associate (expr1
, expr2
);
6561 sym
->attr
.flavor
= FL_VARIABLE
;
6562 sym
->attr
.referenced
= 1;
6563 sym
->attr
.class_ok
= 1;
6564 CLASS_DATA (sym
)->attr
.allocatable
= CLASS_DATA (sym2
)->attr
.allocatable
;
6565 CLASS_DATA (sym
)->attr
.pointer
= CLASS_DATA (sym2
)->attr
.pointer
;
6566 CLASS_DATA (sym
)->attr
.target
= CLASS_DATA (sym2
)->attr
.target
;
6567 sym
->attr
.pointer
= 1;
6572 sym
->as
= gfc_copy_array_spec (sym2
->as
);
6573 sym
->attr
.dimension
= 1;
6575 sym
->attr
.flavor
= FL_VARIABLE
;
6576 sym
->attr
.referenced
= 1;
6577 sym
->attr
.class_ok
= sym2
->attr
.class_ok
;
6578 sym
->attr
.allocatable
= sym2
->attr
.allocatable
;
6579 sym
->attr
.pointer
= sym2
->attr
.pointer
;
6580 sym
->attr
.target
= sym2
->attr
.target
;
6585 m
= gfc_match (" %e ", &expr1
);
6589 std::swap (ns
, gfc_current_ns
);
6590 gfc_free_namespace (ns
);
6596 sym
= expr1
->symtree
->n
.sym
;
6597 as
= sym
->ts
.type
== BT_CLASS
? CLASS_DATA (sym
)->as
: sym
->as
;
6600 if (expr1
->expr_type
!= EXPR_VARIABLE
6601 || !(as
&& as
->type
== AS_ASSUMED_RANK
))
6603 gfc_error("The SELECT RANK selector at %C must be an assumed "
6610 m
= gfc_match (" )%t");
6613 gfc_error ("parse error in SELECT RANK statement at %C");
6617 new_st
.op
= EXEC_SELECT_RANK
;
6618 new_st
.expr1
= expr1
;
6619 new_st
.expr2
= expr2
;
6620 new_st
.ext
.block
.ns
= gfc_current_ns
;
6622 select_type_push (expr1
->symtree
->n
.sym
);
6623 gfc_current_ns
= ns
;
6628 gfc_free_expr (expr1
);
6629 gfc_free_expr (expr2
);
6630 gfc_undo_symbols ();
6631 std::swap (ns
, gfc_current_ns
);
6632 gfc_free_namespace (ns
);
6637 /* Match a CASE statement. */
6640 gfc_match_case (void)
6642 gfc_case
*c
, *head
, *tail
;
6647 if (gfc_current_state () != COMP_SELECT
)
6649 gfc_error ("Unexpected CASE statement at %C");
6653 if (gfc_match ("% default") == MATCH_YES
)
6655 m
= match_case_eos ();
6658 if (m
== MATCH_ERROR
)
6661 new_st
.op
= EXEC_SELECT
;
6662 c
= gfc_get_case ();
6663 c
->where
= gfc_current_locus
;
6664 new_st
.ext
.block
.case_list
= c
;
6668 if (gfc_match_char ('(') != MATCH_YES
)
6673 if (match_case_selector (&c
) == MATCH_ERROR
)
6683 if (gfc_match_char (')') == MATCH_YES
)
6685 if (gfc_match_char (',') != MATCH_YES
)
6689 m
= match_case_eos ();
6692 if (m
== MATCH_ERROR
)
6695 new_st
.op
= EXEC_SELECT
;
6696 new_st
.ext
.block
.case_list
= head
;
6701 gfc_error ("Syntax error in CASE specification at %C");
6704 gfc_free_case_list (head
); /* new_st is cleaned up in parse.c. */
6709 /* Match a TYPE IS statement. */
6712 gfc_match_type_is (void)
6717 if (gfc_current_state () != COMP_SELECT_TYPE
)
6719 gfc_error ("Unexpected TYPE IS statement at %C");
6723 if (gfc_match_char ('(') != MATCH_YES
)
6726 c
= gfc_get_case ();
6727 c
->where
= gfc_current_locus
;
6729 m
= gfc_match_type_spec (&c
->ts
);
6732 if (m
== MATCH_ERROR
)
6735 if (gfc_match_char (')') != MATCH_YES
)
6738 m
= match_case_eos ();
6741 if (m
== MATCH_ERROR
)
6744 new_st
.op
= EXEC_SELECT_TYPE
;
6745 new_st
.ext
.block
.case_list
= c
;
6747 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
6748 && (c
->ts
.u
.derived
->attr
.sequence
6749 || c
->ts
.u
.derived
->attr
.is_bind_c
))
6751 gfc_error ("The type-spec shall not specify a sequence derived "
6752 "type or a type with the BIND attribute in SELECT "
6753 "TYPE at %C [F2003:C815]");
6757 if (c
->ts
.type
== BT_DERIVED
6758 && c
->ts
.u
.derived
&& c
->ts
.u
.derived
->attr
.pdt_type
6759 && gfc_spec_list_type (type_param_spec_list
, c
->ts
.u
.derived
)
6762 gfc_error ("All the LEN type parameters in the TYPE IS statement "
6763 "at %C must be ASSUMED");
6767 /* Create temporary variable. */
6768 select_type_set_tmp (&c
->ts
);
6773 gfc_error ("Syntax error in TYPE IS specification at %C");
6777 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
6782 /* Match a CLASS IS or CLASS DEFAULT statement. */
6785 gfc_match_class_is (void)
6790 if (gfc_current_state () != COMP_SELECT_TYPE
)
6793 if (gfc_match ("% default") == MATCH_YES
)
6795 m
= match_case_eos ();
6798 if (m
== MATCH_ERROR
)
6801 new_st
.op
= EXEC_SELECT_TYPE
;
6802 c
= gfc_get_case ();
6803 c
->where
= gfc_current_locus
;
6804 c
->ts
.type
= BT_UNKNOWN
;
6805 new_st
.ext
.block
.case_list
= c
;
6806 select_type_set_tmp (NULL
);
6810 m
= gfc_match ("% is");
6813 if (m
== MATCH_ERROR
)
6816 if (gfc_match_char ('(') != MATCH_YES
)
6819 c
= gfc_get_case ();
6820 c
->where
= gfc_current_locus
;
6822 m
= match_derived_type_spec (&c
->ts
);
6825 if (m
== MATCH_ERROR
)
6828 if (c
->ts
.type
== BT_DERIVED
)
6829 c
->ts
.type
= BT_CLASS
;
6831 if (gfc_match_char (')') != MATCH_YES
)
6834 m
= match_case_eos ();
6837 if (m
== MATCH_ERROR
)
6840 new_st
.op
= EXEC_SELECT_TYPE
;
6841 new_st
.ext
.block
.case_list
= c
;
6843 /* Create temporary variable. */
6844 select_type_set_tmp (&c
->ts
);
6849 gfc_error ("Syntax error in CLASS IS specification at %C");
6853 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
6858 /* Match a RANK statement. */
6861 gfc_match_rank_is (void)
6867 if (gfc_current_state () != COMP_SELECT_RANK
)
6869 gfc_error ("Unexpected RANK statement at %C");
6873 if (gfc_match ("% default") == MATCH_YES
)
6875 m
= match_case_eos ();
6878 if (m
== MATCH_ERROR
)
6881 new_st
.op
= EXEC_SELECT_RANK
;
6882 c
= gfc_get_case ();
6883 c
->ts
.type
= BT_UNKNOWN
;
6884 c
->where
= gfc_current_locus
;
6885 new_st
.ext
.block
.case_list
= c
;
6886 select_type_stack
->tmp
= NULL
;
6890 if (gfc_match_char ('(') != MATCH_YES
)
6893 c
= gfc_get_case ();
6894 c
->where
= gfc_current_locus
;
6895 c
->ts
= select_type_stack
->selector
->ts
;
6897 m
= gfc_match_expr (&c
->low
);
6900 if (gfc_match_char ('*') == MATCH_YES
)
6901 c
->low
= gfc_get_int_expr (gfc_default_integer_kind
,
6908 else if (m
== MATCH_YES
)
6911 if (c
->low
->expr_type
!= EXPR_CONSTANT
6912 || c
->low
->ts
.type
!= BT_INTEGER
6915 gfc_error ("The SELECT RANK CASE expression at %C must be a "
6916 "scalar, integer constant");
6920 case_value
= (int) mpz_get_si (c
->low
->value
.integer
);
6922 if ((case_value
< 0) || (case_value
> GFC_MAX_DIMENSIONS
))
6924 gfc_error ("The value of the SELECT RANK CASE expression at "
6925 "%C must not be less than zero or greater than %d",
6926 GFC_MAX_DIMENSIONS
);
6933 if (gfc_match_char (')') != MATCH_YES
)
6936 m
= match_case_eos ();
6939 if (m
== MATCH_ERROR
)
6942 new_st
.op
= EXEC_SELECT_RANK
;
6943 new_st
.ext
.block
.case_list
= c
;
6945 /* Create temporary variable. Recycle the select type code. */
6946 select_rank_set_tmp (&c
->ts
, &case_value
);
6951 gfc_error ("Syntax error in RANK specification at %C");
6955 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
6959 /********************* WHERE subroutines ********************/
6961 /* Match the rest of a simple WHERE statement that follows an IF statement.
6965 match_simple_where (void)
6971 m
= gfc_match (" ( %e )", &expr
);
6975 m
= gfc_match_assignment ();
6978 if (m
== MATCH_ERROR
)
6981 if (gfc_match_eos () != MATCH_YES
)
6984 c
= gfc_get_code (EXEC_WHERE
);
6987 c
->next
= XCNEW (gfc_code
);
6989 c
->next
->loc
= gfc_current_locus
;
6990 gfc_clear_new_st ();
6992 new_st
.op
= EXEC_WHERE
;
6998 gfc_syntax_error (ST_WHERE
);
7001 gfc_free_expr (expr
);
7006 /* Match a WHERE statement. */
7009 gfc_match_where (gfc_statement
*st
)
7015 m0
= gfc_match_label ();
7016 if (m0
== MATCH_ERROR
)
7019 m
= gfc_match (" where ( %e )", &expr
);
7023 if (gfc_match_eos () == MATCH_YES
)
7025 *st
= ST_WHERE_BLOCK
;
7026 new_st
.op
= EXEC_WHERE
;
7027 new_st
.expr1
= expr
;
7031 m
= gfc_match_assignment ();
7033 gfc_syntax_error (ST_WHERE
);
7037 gfc_free_expr (expr
);
7041 /* We've got a simple WHERE statement. */
7043 c
= gfc_get_code (EXEC_WHERE
);
7046 /* Put in the assignment. It will not be processed by add_statement, so we
7047 need to copy the location here. */
7049 c
->next
= XCNEW (gfc_code
);
7051 c
->next
->loc
= gfc_current_locus
;
7052 gfc_clear_new_st ();
7054 new_st
.op
= EXEC_WHERE
;
7061 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
7062 new_st if successful. */
7065 gfc_match_elsewhere (void)
7067 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7071 if (gfc_current_state () != COMP_WHERE
)
7073 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
7079 if (gfc_match_char ('(') == MATCH_YES
)
7081 m
= gfc_match_expr (&expr
);
7084 if (m
== MATCH_ERROR
)
7087 if (gfc_match_char (')') != MATCH_YES
)
7091 if (gfc_match_eos () != MATCH_YES
)
7093 /* Only makes sense if we have a where-construct-name. */
7094 if (!gfc_current_block ())
7099 /* Better be a name at this point. */
7100 m
= gfc_match_name (name
);
7103 if (m
== MATCH_ERROR
)
7106 if (gfc_match_eos () != MATCH_YES
)
7109 if (strcmp (name
, gfc_current_block ()->name
) != 0)
7111 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
7112 name
, gfc_current_block ()->name
);
7117 new_st
.op
= EXEC_WHERE
;
7118 new_st
.expr1
= expr
;
7122 gfc_syntax_error (ST_ELSEWHERE
);
7125 gfc_free_expr (expr
);