1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2021 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 /* Matches a statement label. Uses gfc_match_small_literal_int() to
534 do most of the work. */
537 gfc_match_st_label (gfc_st_label
**label
)
543 old_loc
= gfc_current_locus
;
545 m
= gfc_match_small_literal_int (&i
, &cnt
);
551 gfc_error ("Too many digits in statement label at %C");
557 gfc_error ("Statement label at %C is zero");
561 *label
= gfc_get_st_label (i
);
566 gfc_current_locus
= old_loc
;
571 /* Match and validate a label associated with a named IF, DO or SELECT
572 statement. If the symbol does not have the label attribute, we add
573 it. We also make sure the symbol does not refer to another
574 (active) block. A matched label is pointed to by gfc_new_block. */
577 gfc_match_label (void)
579 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
582 gfc_new_block
= NULL
;
584 m
= gfc_match (" %n :", name
);
588 if (gfc_get_symbol (name
, NULL
, &gfc_new_block
))
590 gfc_error ("Label name %qs at %C is ambiguous", name
);
594 if (gfc_new_block
->attr
.flavor
== FL_LABEL
)
596 gfc_error ("Duplicate construct label %qs at %C", name
);
600 if (!gfc_add_flavor (&gfc_new_block
->attr
, FL_LABEL
,
601 gfc_new_block
->name
, NULL
))
608 /* See if the current input looks like a name of some sort. Modifies
609 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
610 Note that options.c restricts max_identifier_length to not more
611 than GFC_MAX_SYMBOL_LEN. */
614 gfc_match_name (char *buffer
)
620 old_loc
= gfc_current_locus
;
621 gfc_gobble_whitespace ();
623 c
= gfc_next_ascii_char ();
624 if (!(ISALPHA (c
) || (c
== '_' && flag_allow_leading_underscore
)))
626 /* Special cases for unary minus and plus, which allows for a sensible
627 error message for code of the form 'c = exp(-a*b) )' where an
628 extra ')' appears at the end of statement. */
629 if (!gfc_error_flag_test () && c
!= '(' && c
!= '-' && c
!= '+')
630 gfc_error ("Invalid character in name at %C");
631 gfc_current_locus
= old_loc
;
641 if (i
> gfc_option
.max_identifier_length
)
643 gfc_error ("Name at %C is too long");
647 old_loc
= gfc_current_locus
;
648 c
= gfc_next_ascii_char ();
650 while (ISALNUM (c
) || c
== '_' || (flag_dollar_ok
&& c
== '$'));
652 if (c
== '$' && !flag_dollar_ok
)
654 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
655 "allow it as an extension", &old_loc
);
660 gfc_current_locus
= old_loc
;
666 /* Match a symbol on the input. Modifies the pointer to the symbol
667 pointer if successful. */
670 gfc_match_sym_tree (gfc_symtree
**matched_symbol
, int host_assoc
)
672 char buffer
[GFC_MAX_SYMBOL_LEN
+ 1];
675 m
= gfc_match_name (buffer
);
680 return (gfc_get_ha_sym_tree (buffer
, matched_symbol
))
681 ? MATCH_ERROR
: MATCH_YES
;
683 if (gfc_get_sym_tree (buffer
, NULL
, matched_symbol
, false))
691 gfc_match_symbol (gfc_symbol
**matched_symbol
, int host_assoc
)
696 m
= gfc_match_sym_tree (&st
, host_assoc
);
701 *matched_symbol
= st
->n
.sym
;
703 *matched_symbol
= NULL
;
706 *matched_symbol
= NULL
;
711 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
712 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
716 gfc_match_intrinsic_op (gfc_intrinsic_op
*result
)
718 locus orig_loc
= gfc_current_locus
;
721 gfc_gobble_whitespace ();
722 ch
= gfc_next_ascii_char ();
727 *result
= INTRINSIC_PLUS
;
732 *result
= INTRINSIC_MINUS
;
736 if (gfc_next_ascii_char () == '=')
739 *result
= INTRINSIC_EQ
;
745 if (gfc_peek_ascii_char () == '=')
748 gfc_next_ascii_char ();
749 *result
= INTRINSIC_LE
;
753 *result
= INTRINSIC_LT
;
757 if (gfc_peek_ascii_char () == '=')
760 gfc_next_ascii_char ();
761 *result
= INTRINSIC_GE
;
765 *result
= INTRINSIC_GT
;
769 if (gfc_peek_ascii_char () == '*')
772 gfc_next_ascii_char ();
773 *result
= INTRINSIC_POWER
;
777 *result
= INTRINSIC_TIMES
;
781 ch
= gfc_peek_ascii_char ();
785 gfc_next_ascii_char ();
786 *result
= INTRINSIC_NE
;
792 gfc_next_ascii_char ();
793 *result
= INTRINSIC_CONCAT
;
797 *result
= INTRINSIC_DIVIDE
;
801 ch
= gfc_next_ascii_char ();
805 if (gfc_next_ascii_char () == 'n'
806 && gfc_next_ascii_char () == 'd'
807 && gfc_next_ascii_char () == '.')
809 /* Matched ".and.". */
810 *result
= INTRINSIC_AND
;
816 if (gfc_next_ascii_char () == 'q')
818 ch
= gfc_next_ascii_char ();
821 /* Matched ".eq.". */
822 *result
= INTRINSIC_EQ_OS
;
827 if (gfc_next_ascii_char () == '.')
829 /* Matched ".eqv.". */
830 *result
= INTRINSIC_EQV
;
838 ch
= gfc_next_ascii_char ();
841 if (gfc_next_ascii_char () == '.')
843 /* Matched ".ge.". */
844 *result
= INTRINSIC_GE_OS
;
850 if (gfc_next_ascii_char () == '.')
852 /* Matched ".gt.". */
853 *result
= INTRINSIC_GT_OS
;
860 ch
= gfc_next_ascii_char ();
863 if (gfc_next_ascii_char () == '.')
865 /* Matched ".le.". */
866 *result
= INTRINSIC_LE_OS
;
872 if (gfc_next_ascii_char () == '.')
874 /* Matched ".lt.". */
875 *result
= INTRINSIC_LT_OS
;
882 ch
= gfc_next_ascii_char ();
885 ch
= gfc_next_ascii_char ();
888 /* Matched ".ne.". */
889 *result
= INTRINSIC_NE_OS
;
894 if (gfc_next_ascii_char () == 'v'
895 && gfc_next_ascii_char () == '.')
897 /* Matched ".neqv.". */
898 *result
= INTRINSIC_NEQV
;
905 if (gfc_next_ascii_char () == 't'
906 && gfc_next_ascii_char () == '.')
908 /* Matched ".not.". */
909 *result
= INTRINSIC_NOT
;
916 if (gfc_next_ascii_char () == 'r'
917 && gfc_next_ascii_char () == '.')
919 /* Matched ".or.". */
920 *result
= INTRINSIC_OR
;
926 if (gfc_next_ascii_char () == 'o'
927 && gfc_next_ascii_char () == 'r'
928 && gfc_next_ascii_char () == '.')
930 if (!gfc_notify_std (GFC_STD_LEGACY
, ".XOR. operator at %C"))
932 /* Matched ".xor." - equivalent to ".neqv.". */
933 *result
= INTRINSIC_NEQV
;
947 gfc_current_locus
= orig_loc
;
952 /* Match a loop control phrase:
954 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
956 If the final integer expression is not present, a constant unity
957 expression is returned. We don't return MATCH_ERROR until after
958 the equals sign is seen. */
961 gfc_match_iterator (gfc_iterator
*iter
, int init_flag
)
963 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
964 gfc_expr
*var
, *e1
, *e2
, *e3
;
970 /* Match the start of an iterator without affecting the symbol table. */
972 start
= gfc_current_locus
;
973 m
= gfc_match (" %n =", name
);
974 gfc_current_locus
= start
;
979 m
= gfc_match_variable (&var
, 0);
983 if (var
->symtree
->n
.sym
->attr
.dimension
)
985 gfc_error ("Loop variable at %C cannot be an array");
989 /* F2008, C617 & C565. */
990 if (var
->symtree
->n
.sym
->attr
.codimension
)
992 gfc_error ("Loop variable at %C cannot be a coarray");
996 if (var
->ref
!= NULL
)
998 gfc_error ("Loop variable at %C cannot be a sub-component");
1002 gfc_match_char ('=');
1004 var
->symtree
->n
.sym
->attr
.implied_index
= 1;
1006 m
= init_flag
? gfc_match_init_expr (&e1
) : gfc_match_expr (&e1
);
1009 if (m
== MATCH_ERROR
)
1012 if (gfc_match_char (',') != MATCH_YES
)
1015 m
= init_flag
? gfc_match_init_expr (&e2
) : gfc_match_expr (&e2
);
1018 if (m
== MATCH_ERROR
)
1021 if (gfc_match_char (',') != MATCH_YES
)
1023 e3
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
1027 m
= init_flag
? gfc_match_init_expr (&e3
) : gfc_match_expr (&e3
);
1028 if (m
== MATCH_ERROR
)
1032 gfc_error ("Expected a step value in iterator at %C");
1044 gfc_error ("Syntax error in iterator at %C");
1055 /* Tries to match the next non-whitespace character on the input.
1056 This subroutine does not return MATCH_ERROR. */
1059 gfc_match_char (char c
)
1063 where
= gfc_current_locus
;
1064 gfc_gobble_whitespace ();
1066 if (gfc_next_ascii_char () == c
)
1069 gfc_current_locus
= where
;
1074 /* General purpose matching subroutine. The target string is a
1075 scanf-like format string in which spaces correspond to arbitrary
1076 whitespace (including no whitespace), characters correspond to
1077 themselves. The %-codes are:
1079 %% Literal percent sign
1080 %e Expression, pointer to a pointer is set
1081 %s Symbol, pointer to the symbol is set
1082 %n Name, character buffer is set to name
1083 %t Matches end of statement.
1084 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1085 %l Matches a statement label
1086 %v Matches a variable expression (an lvalue, except function references
1087 having a data pointer result)
1088 % Matches a required space (in free form) and optional spaces. */
1091 gfc_match (const char *target
, ...)
1093 gfc_st_label
**label
;
1102 old_loc
= gfc_current_locus
;
1103 va_start (argp
, target
);
1113 gfc_gobble_whitespace ();
1124 vp
= va_arg (argp
, void **);
1125 n
= gfc_match_expr ((gfc_expr
**) vp
);
1136 vp
= va_arg (argp
, void **);
1137 n
= gfc_match_variable ((gfc_expr
**) vp
, 0);
1148 vp
= va_arg (argp
, void **);
1149 n
= gfc_match_symbol ((gfc_symbol
**) vp
, 0);
1160 np
= va_arg (argp
, char *);
1161 n
= gfc_match_name (np
);
1172 label
= va_arg (argp
, gfc_st_label
**);
1173 n
= gfc_match_st_label (label
);
1184 ip
= va_arg (argp
, int *);
1185 n
= gfc_match_intrinsic_op ((gfc_intrinsic_op
*) ip
);
1196 if (gfc_match_eos () != MATCH_YES
)
1204 if (gfc_match_space () == MATCH_YES
)
1210 break; /* Fall through to character matcher. */
1213 gfc_internal_error ("gfc_match(): Bad match code %c", c
);
1219 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1220 expect an upper case character here! */
1221 gcc_assert (TOLOWER (c
) == c
);
1223 if (c
== gfc_next_ascii_char ())
1233 /* Clean up after a failed match. */
1234 gfc_current_locus
= old_loc
;
1235 va_start (argp
, target
);
1238 for (; matches
> 0; matches
--)
1240 while (*p
++ != '%');
1248 /* Matches that don't have to be undone */
1253 (void) va_arg (argp
, void **);
1258 vp
= va_arg (argp
, void **);
1259 gfc_free_expr ((struct gfc_expr
*)*vp
);
1272 /*********************** Statement level matching **********************/
1274 /* Matches the start of a program unit, which is the program keyword
1275 followed by an obligatory symbol. */
1278 gfc_match_program (void)
1283 m
= gfc_match ("% %s%t", &sym
);
1287 gfc_error ("Invalid form of PROGRAM statement at %C");
1291 if (m
== MATCH_ERROR
)
1294 if (!gfc_add_flavor (&sym
->attr
, FL_PROGRAM
, sym
->name
, NULL
))
1297 gfc_new_block
= sym
;
1303 /* Match a simple assignment statement. */
1306 gfc_match_assignment (void)
1308 gfc_expr
*lvalue
, *rvalue
;
1312 old_loc
= gfc_current_locus
;
1315 m
= gfc_match (" %v =", &lvalue
);
1318 gfc_current_locus
= old_loc
;
1319 gfc_free_expr (lvalue
);
1324 m
= gfc_match (" %e%t", &rvalue
);
1327 && rvalue
->ts
.type
== BT_BOZ
1328 && lvalue
->ts
.type
== BT_CLASS
)
1331 gfc_error ("BOZ literal constant at %L is neither a DATA statement "
1332 "value nor an actual argument of INT/REAL/DBLE/CMPLX "
1333 "intrinsic subprogram", &rvalue
->where
);
1336 if (lvalue
->expr_type
== EXPR_CONSTANT
)
1338 /* This clobbers %len and %kind. */
1340 gfc_error ("Assignment to a constant expression at %C");
1345 gfc_current_locus
= old_loc
;
1346 gfc_free_expr (lvalue
);
1347 gfc_free_expr (rvalue
);
1351 if (!lvalue
->symtree
)
1353 gfc_free_expr (lvalue
);
1354 gfc_free_expr (rvalue
);
1359 gfc_set_sym_referenced (lvalue
->symtree
->n
.sym
);
1361 new_st
.op
= EXEC_ASSIGN
;
1362 new_st
.expr1
= lvalue
;
1363 new_st
.expr2
= rvalue
;
1365 gfc_check_do_variable (lvalue
->symtree
);
1371 /* Match a pointer assignment statement. */
1374 gfc_match_pointer_assignment (void)
1376 gfc_expr
*lvalue
, *rvalue
;
1380 old_loc
= gfc_current_locus
;
1382 lvalue
= rvalue
= NULL
;
1383 gfc_matching_ptr_assignment
= 0;
1384 gfc_matching_procptr_assignment
= 0;
1386 m
= gfc_match (" %v =>", &lvalue
);
1387 if (m
!= MATCH_YES
|| !lvalue
->symtree
)
1393 if (lvalue
->symtree
->n
.sym
->attr
.proc_pointer
1394 || gfc_is_proc_ptr_comp (lvalue
))
1395 gfc_matching_procptr_assignment
= 1;
1397 gfc_matching_ptr_assignment
= 1;
1399 m
= gfc_match (" %e%t", &rvalue
);
1400 gfc_matching_ptr_assignment
= 0;
1401 gfc_matching_procptr_assignment
= 0;
1405 new_st
.op
= EXEC_POINTER_ASSIGN
;
1406 new_st
.expr1
= lvalue
;
1407 new_st
.expr2
= rvalue
;
1412 gfc_current_locus
= old_loc
;
1413 gfc_free_expr (lvalue
);
1414 gfc_free_expr (rvalue
);
1419 /* We try to match an easy arithmetic IF statement. This only happens
1420 when just after having encountered a simple IF statement. This code
1421 is really duplicate with parts of the gfc_match_if code, but this is
1425 match_arithmetic_if (void)
1427 gfc_st_label
*l1
, *l2
, *l3
;
1431 m
= gfc_match (" ( %e ) %l , %l , %l%t", &expr
, &l1
, &l2
, &l3
);
1435 if (!gfc_reference_st_label (l1
, ST_LABEL_TARGET
)
1436 || !gfc_reference_st_label (l2
, ST_LABEL_TARGET
)
1437 || !gfc_reference_st_label (l3
, ST_LABEL_TARGET
))
1439 gfc_free_expr (expr
);
1443 if (!gfc_notify_std (GFC_STD_F95_OBS
| GFC_STD_F2018_DEL
,
1444 "Arithmetic IF statement at %C"))
1447 new_st
.op
= EXEC_ARITHMETIC_IF
;
1448 new_st
.expr1
= expr
;
1457 /* The IF statement is a bit of a pain. First of all, there are three
1458 forms of it, the simple IF, the IF that starts a block and the
1461 There is a problem with the simple IF and that is the fact that we
1462 only have a single level of undo information on symbols. What this
1463 means is for a simple IF, we must re-match the whole IF statement
1464 multiple times in order to guarantee that the symbol table ends up
1465 in the proper state. */
1467 static match
match_simple_forall (void);
1468 static match
match_simple_where (void);
1471 gfc_match_if (gfc_statement
*if_type
)
1474 gfc_st_label
*l1
, *l2
, *l3
;
1475 locus old_loc
, old_loc2
;
1479 n
= gfc_match_label ();
1480 if (n
== MATCH_ERROR
)
1483 old_loc
= gfc_current_locus
;
1485 m
= gfc_match (" if ", &expr
);
1489 if (gfc_match_char ('(') != MATCH_YES
)
1491 gfc_error ("Missing %<(%> in IF-expression at %C");
1495 m
= gfc_match ("%e", &expr
);
1499 old_loc2
= gfc_current_locus
;
1500 gfc_current_locus
= old_loc
;
1502 if (gfc_match_parens () == MATCH_ERROR
)
1505 gfc_current_locus
= old_loc2
;
1507 if (gfc_match_char (')') != MATCH_YES
)
1509 gfc_error ("Syntax error in IF-expression at %C");
1510 gfc_free_expr (expr
);
1514 m
= gfc_match (" %l , %l , %l%t", &l1
, &l2
, &l3
);
1520 gfc_error ("Block label not appropriate for arithmetic IF "
1522 gfc_free_expr (expr
);
1526 if (!gfc_reference_st_label (l1
, ST_LABEL_TARGET
)
1527 || !gfc_reference_st_label (l2
, ST_LABEL_TARGET
)
1528 || !gfc_reference_st_label (l3
, ST_LABEL_TARGET
))
1530 gfc_free_expr (expr
);
1534 if (!gfc_notify_std (GFC_STD_F95_OBS
| GFC_STD_F2018_DEL
,
1535 "Arithmetic IF statement at %C"))
1538 new_st
.op
= EXEC_ARITHMETIC_IF
;
1539 new_st
.expr1
= expr
;
1544 *if_type
= ST_ARITHMETIC_IF
;
1548 if (gfc_match (" then%t") == MATCH_YES
)
1550 new_st
.op
= EXEC_IF
;
1551 new_st
.expr1
= expr
;
1552 *if_type
= ST_IF_BLOCK
;
1558 gfc_error ("Block label is not appropriate for IF statement at %C");
1559 gfc_free_expr (expr
);
1563 /* At this point the only thing left is a simple IF statement. At
1564 this point, n has to be MATCH_NO, so we don't have to worry about
1565 re-matching a block label. From what we've got so far, try
1566 matching an assignment. */
1568 *if_type
= ST_SIMPLE_IF
;
1570 m
= gfc_match_assignment ();
1574 gfc_free_expr (expr
);
1575 gfc_undo_symbols ();
1576 gfc_current_locus
= old_loc
;
1578 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1579 assignment was found. For MATCH_NO, continue to call the various
1581 if (m
== MATCH_ERROR
)
1584 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match. */
1586 m
= gfc_match_pointer_assignment ();
1590 gfc_free_expr (expr
);
1591 gfc_undo_symbols ();
1592 gfc_current_locus
= old_loc
;
1594 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match. */
1596 /* Look at the next keyword to see which matcher to call. Matching
1597 the keyword doesn't affect the symbol table, so we don't have to
1598 restore between tries. */
1600 #define match(string, subr, statement) \
1601 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1605 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
)
1606 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
)
1607 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
)
1608 match ("call", gfc_match_call
, ST_CALL
)
1609 match ("change team", gfc_match_change_team
, ST_CHANGE_TEAM
)
1610 match ("close", gfc_match_close
, ST_CLOSE
)
1611 match ("continue", gfc_match_continue
, ST_CONTINUE
)
1612 match ("cycle", gfc_match_cycle
, ST_CYCLE
)
1613 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
)
1614 match ("end file", gfc_match_endfile
, ST_END_FILE
)
1615 match ("end team", gfc_match_end_team
, ST_END_TEAM
)
1616 match ("error stop", gfc_match_error_stop
, ST_ERROR_STOP
)
1617 match ("event post", gfc_match_event_post
, ST_EVENT_POST
)
1618 match ("event wait", gfc_match_event_wait
, ST_EVENT_WAIT
)
1619 match ("exit", gfc_match_exit
, ST_EXIT
)
1620 match ("fail image", gfc_match_fail_image
, ST_FAIL_IMAGE
)
1621 match ("flush", gfc_match_flush
, ST_FLUSH
)
1622 match ("forall", match_simple_forall
, ST_FORALL
)
1623 match ("form team", gfc_match_form_team
, ST_FORM_TEAM
)
1624 match ("go to", gfc_match_goto
, ST_GOTO
)
1625 match ("if", match_arithmetic_if
, ST_ARITHMETIC_IF
)
1626 match ("inquire", gfc_match_inquire
, ST_INQUIRE
)
1627 match ("lock", gfc_match_lock
, ST_LOCK
)
1628 match ("nullify", gfc_match_nullify
, ST_NULLIFY
)
1629 match ("open", gfc_match_open
, ST_OPEN
)
1630 match ("pause", gfc_match_pause
, ST_NONE
)
1631 match ("print", gfc_match_print
, ST_WRITE
)
1632 match ("read", gfc_match_read
, ST_READ
)
1633 match ("return", gfc_match_return
, ST_RETURN
)
1634 match ("rewind", gfc_match_rewind
, ST_REWIND
)
1635 match ("stop", gfc_match_stop
, ST_STOP
)
1636 match ("wait", gfc_match_wait
, ST_WAIT
)
1637 match ("sync all", gfc_match_sync_all
, ST_SYNC_CALL
);
1638 match ("sync images", gfc_match_sync_images
, ST_SYNC_IMAGES
);
1639 match ("sync memory", gfc_match_sync_memory
, ST_SYNC_MEMORY
);
1640 match ("sync team", gfc_match_sync_team
, ST_SYNC_TEAM
)
1641 match ("unlock", gfc_match_unlock
, ST_UNLOCK
)
1642 match ("where", match_simple_where
, ST_WHERE
)
1643 match ("write", gfc_match_write
, ST_WRITE
)
1646 match ("type", gfc_match_print
, ST_WRITE
)
1648 /* All else has failed, so give up. See if any of the matchers has
1649 stored an error message of some sort. */
1650 if (!gfc_error_check ())
1651 gfc_error ("Syntax error in IF-clause after %C");
1653 gfc_free_expr (expr
);
1658 gfc_error ("Syntax error in IF-clause after %C");
1661 gfc_free_expr (expr
);
1665 /* At this point, we've matched the single IF and the action clause
1666 is in new_st. Rearrange things so that the IF statement appears
1669 p
= gfc_get_code (EXEC_IF
);
1670 p
->next
= XCNEW (gfc_code
);
1672 p
->next
->loc
= gfc_current_locus
;
1676 gfc_clear_new_st ();
1678 new_st
.op
= EXEC_IF
;
1687 /* Match an ELSE statement. */
1690 gfc_match_else (void)
1692 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1694 if (gfc_match_eos () == MATCH_YES
)
1697 if (gfc_match_name (name
) != MATCH_YES
1698 || gfc_current_block () == NULL
1699 || gfc_match_eos () != MATCH_YES
)
1701 gfc_error ("Invalid character(s) in ELSE statement after %C");
1705 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1707 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1708 name
, gfc_current_block ()->name
);
1716 /* Match an ELSE IF statement. */
1719 gfc_match_elseif (void)
1721 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1722 gfc_expr
*expr
, *then
;
1726 if (gfc_match_char ('(') != MATCH_YES
)
1728 gfc_error ("Missing %<(%> in ELSE IF expression at %C");
1732 m
= gfc_match (" %e ", &expr
);
1736 if (gfc_match_char (')') != MATCH_YES
)
1738 gfc_error ("Missing %<)%> in ELSE IF expression at %C");
1742 m
= gfc_match (" then ", &then
);
1744 where
= gfc_current_locus
;
1746 if (m
== MATCH_YES
&& (gfc_match_eos () == MATCH_YES
1747 || (gfc_current_block ()
1748 && gfc_match_name (name
) == MATCH_YES
)))
1751 if (gfc_match_eos () == MATCH_YES
)
1753 gfc_error ("Missing THEN in ELSE IF statement after %L", &where
);
1757 if (gfc_match_name (name
) != MATCH_YES
1758 || gfc_current_block () == NULL
1759 || gfc_match_eos () != MATCH_YES
)
1761 gfc_error ("Syntax error in ELSE IF statement after %L", &where
);
1765 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1767 gfc_error ("Label %qs after %L doesn't match IF label %qs",
1768 name
, &where
, gfc_current_block ()->name
);
1776 new_st
.op
= EXEC_IF
;
1777 new_st
.expr1
= expr
;
1781 gfc_free_expr (expr
);
1786 /* Free a gfc_iterator structure. */
1789 gfc_free_iterator (gfc_iterator
*iter
, int flag
)
1795 gfc_free_expr (iter
->var
);
1796 gfc_free_expr (iter
->start
);
1797 gfc_free_expr (iter
->end
);
1798 gfc_free_expr (iter
->step
);
1805 /* Match a CRITICAL statement. */
1807 gfc_match_critical (void)
1809 gfc_st_label
*label
= NULL
;
1811 if (gfc_match_label () == MATCH_ERROR
)
1814 if (gfc_match (" critical") != MATCH_YES
)
1817 if (gfc_match_st_label (&label
) == MATCH_ERROR
)
1820 if (gfc_match_eos () != MATCH_YES
)
1822 gfc_syntax_error (ST_CRITICAL
);
1826 if (gfc_pure (NULL
))
1828 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1832 if (gfc_find_state (COMP_DO_CONCURRENT
))
1834 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1839 gfc_unset_implicit_pure (NULL
);
1841 if (!gfc_notify_std (GFC_STD_F2008
, "CRITICAL statement at %C"))
1844 if (flag_coarray
== GFC_FCOARRAY_NONE
)
1846 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1851 if (gfc_find_state (COMP_CRITICAL
))
1853 gfc_error ("Nested CRITICAL block at %C");
1857 new_st
.op
= EXEC_CRITICAL
;
1860 && !gfc_reference_st_label (label
, ST_LABEL_TARGET
))
1867 /* Match a BLOCK statement. */
1870 gfc_match_block (void)
1874 if (gfc_match_label () == MATCH_ERROR
)
1877 if (gfc_match (" block") != MATCH_YES
)
1880 /* For this to be a correct BLOCK statement, the line must end now. */
1881 m
= gfc_match_eos ();
1882 if (m
== MATCH_ERROR
)
1891 /* Match an ASSOCIATE statement. */
1894 gfc_match_associate (void)
1896 if (gfc_match_label () == MATCH_ERROR
)
1899 if (gfc_match (" associate") != MATCH_YES
)
1902 /* Match the association list. */
1903 if (gfc_match_char ('(') != MATCH_YES
)
1905 gfc_error ("Expected association list at %C");
1908 new_st
.ext
.block
.assoc
= NULL
;
1911 gfc_association_list
* newAssoc
= gfc_get_association_list ();
1912 gfc_association_list
* a
;
1914 /* Match the next association. */
1915 if (gfc_match (" %n =>", newAssoc
->name
) != MATCH_YES
)
1917 gfc_error ("Expected association at %C");
1918 goto assocListError
;
1921 if (gfc_match (" %e", &newAssoc
->target
) != MATCH_YES
)
1923 /* Have another go, allowing for procedure pointer selectors. */
1924 gfc_matching_procptr_assignment
= 1;
1925 if (gfc_match (" %e", &newAssoc
->target
) != MATCH_YES
)
1927 gfc_error ("Invalid association target at %C");
1928 goto assocListError
;
1930 gfc_matching_procptr_assignment
= 0;
1932 newAssoc
->where
= gfc_current_locus
;
1934 /* Check that the current name is not yet in the list. */
1935 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
1936 if (!strcmp (a
->name
, newAssoc
->name
))
1938 gfc_error ("Duplicate name %qs in association at %C",
1940 goto assocListError
;
1943 /* The target expression must not be coindexed. */
1944 if (gfc_is_coindexed (newAssoc
->target
))
1946 gfc_error ("Association target at %C must not be coindexed");
1947 goto assocListError
;
1950 /* The target expression cannot be a BOZ literal constant. */
1951 if (newAssoc
->target
->ts
.type
== BT_BOZ
)
1953 gfc_error ("Association target at %L cannot be a BOZ literal "
1954 "constant", &newAssoc
->target
->where
);
1955 goto assocListError
;
1958 /* The `variable' field is left blank for now; because the target is not
1959 yet resolved, we can't use gfc_has_vector_subscript to determine it
1960 for now. This is set during resolution. */
1962 /* Put it into the list. */
1963 newAssoc
->next
= new_st
.ext
.block
.assoc
;
1964 new_st
.ext
.block
.assoc
= newAssoc
;
1966 /* Try next one or end if closing parenthesis is found. */
1967 gfc_gobble_whitespace ();
1968 if (gfc_peek_char () == ')')
1970 if (gfc_match_char (',') != MATCH_YES
)
1972 gfc_error ("Expected %<)%> or %<,%> at %C");
1982 if (gfc_match_char (')') != MATCH_YES
)
1984 /* This should never happen as we peek above. */
1988 if (gfc_match_eos () != MATCH_YES
)
1990 gfc_error ("Junk after ASSOCIATE statement at %C");
1997 gfc_free_association_list (new_st
.ext
.block
.assoc
);
2002 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
2003 an accessible derived type. */
2006 match_derived_type_spec (gfc_typespec
*ts
)
2008 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2010 gfc_symbol
*derived
, *der_type
;
2011 match m
= MATCH_YES
;
2012 gfc_actual_arglist
*decl_type_param_list
= NULL
;
2013 bool is_pdt_template
= false;
2015 old_locus
= gfc_current_locus
;
2017 if (gfc_match ("%n", name
) != MATCH_YES
)
2019 gfc_current_locus
= old_locus
;
2023 gfc_find_symbol (name
, NULL
, 1, &derived
);
2025 /* Match the PDT spec list, if there. */
2026 if (derived
&& derived
->attr
.flavor
== FL_PROCEDURE
)
2028 gfc_find_symbol (gfc_dt_upper_string (name
), NULL
, 1, &der_type
);
2029 is_pdt_template
= der_type
2030 && der_type
->attr
.flavor
== FL_DERIVED
2031 && der_type
->attr
.pdt_template
;
2034 if (is_pdt_template
)
2035 m
= gfc_match_actual_arglist (1, &decl_type_param_list
, true);
2037 if (m
== MATCH_ERROR
)
2039 gfc_free_actual_arglist (decl_type_param_list
);
2043 if (derived
&& derived
->attr
.flavor
== FL_PROCEDURE
&& derived
->attr
.generic
)
2044 derived
= gfc_find_dt_in_generic (derived
);
2046 /* If this is a PDT, find the specific instance. */
2047 if (m
== MATCH_YES
&& is_pdt_template
)
2049 gfc_namespace
*old_ns
;
2051 old_ns
= gfc_current_ns
;
2052 while (gfc_current_ns
&& gfc_current_ns
->parent
)
2053 gfc_current_ns
= gfc_current_ns
->parent
;
2055 if (type_param_spec_list
)
2056 gfc_free_actual_arglist (type_param_spec_list
);
2057 m
= gfc_get_pdt_instance (decl_type_param_list
, &der_type
,
2058 &type_param_spec_list
);
2059 gfc_free_actual_arglist (decl_type_param_list
);
2064 gcc_assert (!derived
->attr
.pdt_template
&& derived
->attr
.pdt_type
);
2065 gfc_set_sym_referenced (derived
);
2067 gfc_current_ns
= old_ns
;
2070 if (derived
&& derived
->attr
.flavor
== FL_DERIVED
)
2072 ts
->type
= BT_DERIVED
;
2073 ts
->u
.derived
= derived
;
2077 gfc_current_locus
= old_locus
;
2082 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2083 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
2084 It only includes the intrinsic types from the Fortran 2003 standard
2085 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2086 the implicit_flag is not needed, so it was removed. Derived types are
2087 identified by their name alone. */
2090 gfc_match_type_spec (gfc_typespec
*ts
)
2094 char c
, name
[GFC_MAX_SYMBOL_LEN
+ 1];
2097 gfc_gobble_whitespace ();
2098 old_locus
= gfc_current_locus
;
2100 /* If c isn't [a-z], then return immediately. */
2101 c
= gfc_peek_ascii_char ();
2105 type_param_spec_list
= NULL
;
2107 if (match_derived_type_spec (ts
) == MATCH_YES
)
2109 /* Enforce F03:C401. */
2110 if (ts
->u
.derived
->attr
.abstract
)
2112 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2113 ts
->u
.derived
->name
, &old_locus
);
2119 if (gfc_match ("integer") == MATCH_YES
)
2121 ts
->type
= BT_INTEGER
;
2122 ts
->kind
= gfc_default_integer_kind
;
2126 if (gfc_match ("double precision") == MATCH_YES
)
2129 ts
->kind
= gfc_default_double_kind
;
2133 if (gfc_match ("complex") == MATCH_YES
)
2135 ts
->type
= BT_COMPLEX
;
2136 ts
->kind
= gfc_default_complex_kind
;
2140 if (gfc_match ("character") == MATCH_YES
)
2142 ts
->type
= BT_CHARACTER
;
2144 m
= gfc_match_char_spec (ts
);
2152 /* REAL is a real pain because it can be a type, intrinsic subprogram,
2153 or list item in a type-list of an OpenMP reduction clause. Need to
2154 differentiate REAL([KIND]=scalar-int-initialization-expr) from
2155 REAL(A,[KIND]) and REAL(KIND,A). Logically, when this code was
2156 written the use of LOGICAL as a type-spec or intrinsic subprogram
2159 m
= gfc_match (" %n", name
);
2161 && (strcmp (name
, "real") == 0 || strcmp (name
, "logical") == 0))
2170 ts
->kind
= gfc_default_real_kind
;
2174 ts
->type
= BT_LOGICAL
;
2175 ts
->kind
= gfc_default_logical_kind
;
2178 gfc_gobble_whitespace ();
2180 /* Prevent REAL*4, etc. */
2181 c
= gfc_peek_ascii_char ();
2184 gfc_error ("Invalid type-spec at %C");
2188 /* Found leading colon in REAL::, a trailing ')' in for example
2189 TYPE IS (REAL), or REAL, for an OpenMP list-item. */
2190 if (c
== ':' || c
== ')' || (flag_openmp
&& c
== ','))
2193 /* Found something other than the opening '(' in REAL(... */
2197 gfc_next_char (); /* Burn the '('. */
2199 /* Look for the optional KIND=. */
2200 where
= gfc_current_locus
;
2201 m
= gfc_match ("%n", name
);
2204 gfc_gobble_whitespace ();
2205 c
= gfc_next_char ();
2208 if (strcmp(name
, "a") == 0 || strcmp(name
, "l") == 0)
2210 else if (strcmp(name
, "kind") == 0)
2216 gfc_current_locus
= where
;
2219 gfc_current_locus
= where
;
2223 m
= gfc_match_expr (&e
);
2224 if (m
== MATCH_NO
|| m
== MATCH_ERROR
)
2227 /* If a comma appears, it is an intrinsic subprogram. */
2228 gfc_gobble_whitespace ();
2229 c
= gfc_peek_ascii_char ();
2236 /* If ')' appears, we have REAL(initialization-expr), here check for
2237 a scalar integer initialization-expr and valid kind parameter. */
2241 if (e
->expr_type
!= EXPR_CONSTANT
&& e
->expr_type
!= EXPR_VARIABLE
)
2242 ok
= gfc_reduce_init_expr (e
);
2243 if (!ok
|| e
->ts
.type
!= BT_INTEGER
|| e
->rank
> 0)
2249 if (e
->expr_type
!= EXPR_CONSTANT
)
2252 gfc_next_char (); /* Burn the ')'. */
2253 ts
->kind
= (int) mpz_get_si (e
->value
.integer
);
2254 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) == -1)
2256 gfc_error ("Invalid type-spec at %C");
2268 /* If a type is not matched, simply return MATCH_NO. */
2269 gfc_current_locus
= old_locus
;
2274 gfc_gobble_whitespace ();
2276 /* This prevents INTEGER*4, etc. */
2277 if (gfc_peek_ascii_char () == '*')
2279 gfc_error ("Invalid type-spec at %C");
2283 m
= gfc_match_kind_spec (ts
, false);
2285 /* No kind specifier found. */
2293 /******************** FORALL subroutines ********************/
2295 /* Free a list of FORALL iterators. */
2298 gfc_free_forall_iterator (gfc_forall_iterator
*iter
)
2300 gfc_forall_iterator
*next
;
2305 gfc_free_expr (iter
->var
);
2306 gfc_free_expr (iter
->start
);
2307 gfc_free_expr (iter
->end
);
2308 gfc_free_expr (iter
->stride
);
2315 /* Match an iterator as part of a FORALL statement. The format is:
2317 <var> = <start>:<end>[:<stride>]
2319 On MATCH_NO, the caller tests for the possibility that there is a
2320 scalar mask expression. */
2323 match_forall_iterator (gfc_forall_iterator
**result
)
2325 gfc_forall_iterator
*iter
;
2329 where
= gfc_current_locus
;
2330 iter
= XCNEW (gfc_forall_iterator
);
2332 m
= gfc_match_expr (&iter
->var
);
2336 if (gfc_match_char ('=') != MATCH_YES
2337 || iter
->var
->expr_type
!= EXPR_VARIABLE
)
2343 m
= gfc_match_expr (&iter
->start
);
2347 if (gfc_match_char (':') != MATCH_YES
)
2350 m
= gfc_match_expr (&iter
->end
);
2353 if (m
== MATCH_ERROR
)
2356 if (gfc_match_char (':') == MATCH_NO
)
2357 iter
->stride
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2360 m
= gfc_match_expr (&iter
->stride
);
2363 if (m
== MATCH_ERROR
)
2367 /* Mark the iteration variable's symbol as used as a FORALL index. */
2368 iter
->var
->symtree
->n
.sym
->forall_index
= true;
2374 gfc_error ("Syntax error in FORALL iterator at %C");
2379 gfc_current_locus
= where
;
2380 gfc_free_forall_iterator (iter
);
2385 /* Match the header of a FORALL statement. */
2388 match_forall_header (gfc_forall_iterator
**phead
, gfc_expr
**mask
)
2390 gfc_forall_iterator
*head
, *tail
, *new_iter
;
2394 gfc_gobble_whitespace ();
2399 if (gfc_match_char ('(') != MATCH_YES
)
2402 m
= match_forall_iterator (&new_iter
);
2403 if (m
== MATCH_ERROR
)
2408 head
= tail
= new_iter
;
2412 if (gfc_match_char (',') != MATCH_YES
)
2415 m
= match_forall_iterator (&new_iter
);
2416 if (m
== MATCH_ERROR
)
2421 tail
->next
= new_iter
;
2426 /* Have to have a mask expression. */
2428 m
= gfc_match_expr (&msk
);
2431 if (m
== MATCH_ERROR
)
2437 if (gfc_match_char (')') == MATCH_NO
)
2445 gfc_syntax_error (ST_FORALL
);
2448 gfc_free_expr (msk
);
2449 gfc_free_forall_iterator (head
);
2454 /* Match the rest of a simple FORALL statement that follows an
2458 match_simple_forall (void)
2460 gfc_forall_iterator
*head
;
2469 m
= match_forall_header (&head
, &mask
);
2476 m
= gfc_match_assignment ();
2478 if (m
== MATCH_ERROR
)
2482 m
= gfc_match_pointer_assignment ();
2483 if (m
== MATCH_ERROR
)
2489 c
= XCNEW (gfc_code
);
2491 c
->loc
= gfc_current_locus
;
2493 if (gfc_match_eos () != MATCH_YES
)
2496 gfc_clear_new_st ();
2497 new_st
.op
= EXEC_FORALL
;
2498 new_st
.expr1
= mask
;
2499 new_st
.ext
.forall_iterator
= head
;
2500 new_st
.block
= gfc_get_code (EXEC_FORALL
);
2501 new_st
.block
->next
= c
;
2506 gfc_syntax_error (ST_FORALL
);
2509 gfc_free_forall_iterator (head
);
2510 gfc_free_expr (mask
);
2516 /* Match a FORALL statement. */
2519 gfc_match_forall (gfc_statement
*st
)
2521 gfc_forall_iterator
*head
;
2530 m0
= gfc_match_label ();
2531 if (m0
== MATCH_ERROR
)
2534 m
= gfc_match (" forall");
2538 m
= match_forall_header (&head
, &mask
);
2539 if (m
== MATCH_ERROR
)
2544 if (gfc_match_eos () == MATCH_YES
)
2546 *st
= ST_FORALL_BLOCK
;
2547 new_st
.op
= EXEC_FORALL
;
2548 new_st
.expr1
= mask
;
2549 new_st
.ext
.forall_iterator
= head
;
2553 m
= gfc_match_assignment ();
2554 if (m
== MATCH_ERROR
)
2558 m
= gfc_match_pointer_assignment ();
2559 if (m
== MATCH_ERROR
)
2565 c
= XCNEW (gfc_code
);
2567 c
->loc
= gfc_current_locus
;
2569 gfc_clear_new_st ();
2570 new_st
.op
= EXEC_FORALL
;
2571 new_st
.expr1
= mask
;
2572 new_st
.ext
.forall_iterator
= head
;
2573 new_st
.block
= gfc_get_code (EXEC_FORALL
);
2574 new_st
.block
->next
= c
;
2580 gfc_syntax_error (ST_FORALL
);
2583 gfc_free_forall_iterator (head
);
2584 gfc_free_expr (mask
);
2585 gfc_free_statements (c
);
2590 /* Match a DO statement. */
2595 gfc_iterator iter
, *ip
;
2597 gfc_st_label
*label
;
2600 old_loc
= gfc_current_locus
;
2602 memset (&iter
, '\0', sizeof (gfc_iterator
));
2605 m
= gfc_match_label ();
2606 if (m
== MATCH_ERROR
)
2609 if (gfc_match (" do") != MATCH_YES
)
2612 m
= gfc_match_st_label (&label
);
2613 if (m
== MATCH_ERROR
)
2616 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2618 if (gfc_match_eos () == MATCH_YES
)
2620 iter
.end
= gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, true);
2621 new_st
.op
= EXEC_DO_WHILE
;
2625 /* Match an optional comma, if no comma is found, a space is obligatory. */
2626 if (gfc_match_char (',') != MATCH_YES
&& gfc_match ("% ") != MATCH_YES
)
2629 /* Check for balanced parens. */
2631 if (gfc_match_parens () == MATCH_ERROR
)
2634 if (gfc_match (" concurrent") == MATCH_YES
)
2636 gfc_forall_iterator
*head
;
2639 if (!gfc_notify_std (GFC_STD_F2008
, "DO CONCURRENT construct at %C"))
2645 m
= match_forall_header (&head
, &mask
);
2649 if (m
== MATCH_ERROR
)
2650 goto concurr_cleanup
;
2652 if (gfc_match_eos () != MATCH_YES
)
2653 goto concurr_cleanup
;
2656 && !gfc_reference_st_label (label
, ST_LABEL_DO_TARGET
))
2657 goto concurr_cleanup
;
2659 new_st
.label1
= label
;
2660 new_st
.op
= EXEC_DO_CONCURRENT
;
2661 new_st
.expr1
= mask
;
2662 new_st
.ext
.forall_iterator
= head
;
2667 gfc_syntax_error (ST_DO
);
2668 gfc_free_expr (mask
);
2669 gfc_free_forall_iterator (head
);
2673 /* See if we have a DO WHILE. */
2674 if (gfc_match (" while ( %e )%t", &iter
.end
) == MATCH_YES
)
2676 new_st
.op
= EXEC_DO_WHILE
;
2680 /* The abortive DO WHILE may have done something to the symbol
2681 table, so we start over. */
2682 gfc_undo_symbols ();
2683 gfc_current_locus
= old_loc
;
2685 gfc_match_label (); /* This won't error. */
2686 gfc_match (" do "); /* This will work. */
2688 gfc_match_st_label (&label
); /* Can't error out. */
2689 gfc_match_char (','); /* Optional comma. */
2691 m
= gfc_match_iterator (&iter
, 0);
2694 if (m
== MATCH_ERROR
)
2697 iter
.var
->symtree
->n
.sym
->attr
.implied_index
= 0;
2698 gfc_check_do_variable (iter
.var
->symtree
);
2700 if (gfc_match_eos () != MATCH_YES
)
2702 gfc_syntax_error (ST_DO
);
2706 new_st
.op
= EXEC_DO
;
2710 && !gfc_reference_st_label (label
, ST_LABEL_DO_TARGET
))
2713 new_st
.label1
= label
;
2715 if (new_st
.op
== EXEC_DO_WHILE
)
2716 new_st
.expr1
= iter
.end
;
2719 new_st
.ext
.iterator
= ip
= gfc_get_iterator ();
2726 gfc_free_iterator (&iter
, 0);
2732 /* Match an EXIT or CYCLE statement. */
2735 match_exit_cycle (gfc_statement st
, gfc_exec_op op
)
2737 gfc_state_data
*p
, *o
;
2742 if (gfc_match_eos () == MATCH_YES
)
2746 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2749 m
= gfc_match ("% %n%t", name
);
2750 if (m
== MATCH_ERROR
)
2754 gfc_syntax_error (st
);
2758 /* Find the corresponding symbol. If there's a BLOCK statement
2759 between here and the label, it is not in gfc_current_ns but a parent
2761 stree
= gfc_find_symtree_in_proc (name
, gfc_current_ns
);
2764 gfc_error ("Name %qs in %s statement at %C is unknown",
2765 name
, gfc_ascii_statement (st
));
2770 if (sym
->attr
.flavor
!= FL_LABEL
)
2772 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2773 name
, gfc_ascii_statement (st
));
2778 /* Find the loop specified by the label (or lack of a label). */
2779 for (o
= NULL
, p
= gfc_state_stack
; p
; p
= p
->previous
)
2780 if (o
== NULL
&& p
->state
== COMP_OMP_STRUCTURED_BLOCK
)
2782 else if (p
->state
== COMP_CRITICAL
)
2784 gfc_error("%s statement at %C leaves CRITICAL construct",
2785 gfc_ascii_statement (st
));
2788 else if (p
->state
== COMP_DO_CONCURRENT
2789 && (op
== EXEC_EXIT
|| (sym
&& sym
!= p
->sym
)))
2791 /* F2008, C821 & C845. */
2792 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2793 gfc_ascii_statement (st
));
2796 else if ((sym
&& sym
== p
->sym
)
2797 || (!sym
&& (p
->state
== COMP_DO
2798 || p
->state
== COMP_DO_CONCURRENT
)))
2804 gfc_error ("%s statement at %C is not within a construct",
2805 gfc_ascii_statement (st
));
2807 gfc_error ("%s statement at %C is not within construct %qs",
2808 gfc_ascii_statement (st
), sym
->name
);
2813 /* Special checks for EXIT from non-loop constructs. */
2817 case COMP_DO_CONCURRENT
:
2821 /* This is already handled above. */
2824 case COMP_ASSOCIATE
:
2828 case COMP_SELECT_TYPE
:
2829 case COMP_SELECT_RANK
:
2831 if (op
== EXEC_CYCLE
)
2833 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2834 " construct %qs", sym
->name
);
2837 gcc_assert (op
== EXEC_EXIT
);
2838 if (!gfc_notify_std (GFC_STD_F2008
, "EXIT statement with no"
2839 " do-construct-name at %C"))
2844 gfc_error ("%s statement at %C is not applicable to construct %qs",
2845 gfc_ascii_statement (st
), sym
->name
);
2851 gfc_error (is_oacc (p
)
2852 ? G_("%s statement at %C leaving OpenACC structured block")
2853 : G_("%s statement at %C leaving OpenMP structured block"),
2854 gfc_ascii_statement (st
));
2858 for (o
= p
, cnt
= 0; o
->state
== COMP_DO
&& o
->previous
!= NULL
; cnt
++)
2862 && o
->state
== COMP_OMP_STRUCTURED_BLOCK
2863 && (o
->head
->op
== EXEC_OACC_LOOP
2864 || o
->head
->op
== EXEC_OACC_KERNELS_LOOP
2865 || o
->head
->op
== EXEC_OACC_PARALLEL_LOOP
2866 || o
->head
->op
== EXEC_OACC_SERIAL_LOOP
))
2869 gcc_assert (o
->head
->next
!= NULL
2870 && (o
->head
->next
->op
== EXEC_DO
2871 || o
->head
->next
->op
== EXEC_DO_WHILE
)
2872 && o
->previous
!= NULL
2873 && o
->previous
->tail
->op
== o
->head
->op
);
2874 if (o
->previous
->tail
->ext
.omp_clauses
!= NULL
)
2876 /* Both collapsed and tiled loops are lowered the same way, but are not
2877 compatible. In gfc_trans_omp_do, the tile is prioritized. */
2878 if (o
->previous
->tail
->ext
.omp_clauses
->tile_list
)
2881 gfc_expr_list
*el
= o
->previous
->tail
->ext
.omp_clauses
->tile_list
;
2882 for ( ; el
; el
= el
->next
)
2885 else if (o
->previous
->tail
->ext
.omp_clauses
->collapse
> 1)
2886 collapse
= o
->previous
->tail
->ext
.omp_clauses
->collapse
;
2888 if (st
== ST_EXIT
&& cnt
<= collapse
)
2890 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2893 if (st
== ST_CYCLE
&& cnt
< collapse
)
2895 gfc_error (o
->previous
->tail
->ext
.omp_clauses
->tile_list
2896 ? G_("CYCLE statement at %C to non-innermost tiled"
2898 : G_("CYCLE statement at %C to non-innermost collapsed"
2899 " !$ACC LOOP loop"));
2905 && (o
->state
== COMP_OMP_STRUCTURED_BLOCK
)
2906 && (o
->head
->op
== EXEC_OMP_DO
2907 || o
->head
->op
== EXEC_OMP_PARALLEL_DO
2908 || o
->head
->op
== EXEC_OMP_SIMD
2909 || o
->head
->op
== EXEC_OMP_DO_SIMD
2910 || o
->head
->op
== EXEC_OMP_PARALLEL_DO_SIMD
))
2913 gcc_assert (o
->head
->next
!= NULL
2914 && (o
->head
->next
->op
== EXEC_DO
2915 || o
->head
->next
->op
== EXEC_DO_WHILE
)
2916 && o
->previous
!= NULL
2917 && o
->previous
->tail
->op
== o
->head
->op
);
2918 if (o
->previous
->tail
->ext
.omp_clauses
!= NULL
)
2920 if (o
->previous
->tail
->ext
.omp_clauses
->collapse
> 1)
2921 count
= o
->previous
->tail
->ext
.omp_clauses
->collapse
;
2922 if (o
->previous
->tail
->ext
.omp_clauses
->orderedc
)
2923 count
= o
->previous
->tail
->ext
.omp_clauses
->orderedc
;
2925 if (st
== ST_EXIT
&& cnt
<= count
)
2927 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2930 if (st
== ST_CYCLE
&& cnt
< count
)
2932 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2938 /* Save the first statement in the construct - needed by the backend. */
2939 new_st
.ext
.which_construct
= p
->construct
;
2947 /* Match the EXIT statement. */
2950 gfc_match_exit (void)
2952 return match_exit_cycle (ST_EXIT
, EXEC_EXIT
);
2956 /* Match the CYCLE statement. */
2959 gfc_match_cycle (void)
2961 return match_exit_cycle (ST_CYCLE
, EXEC_CYCLE
);
2965 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
2966 requirements for a stop-code differ in the standards.
2970 R840 stop-stmt is STOP [ stop-code ]
2971 R841 stop-code is scalar-char-constant
2972 or digit [ digit [ digit [ digit [ digit ] ] ] ]
2974 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
2977 R855 stop-stmt is STOP [ stop-code ]
2978 R856 allstop-stmt is ALL STOP [ stop-code ]
2979 R857 stop-code is scalar-default-char-constant-expr
2980 or scalar-int-constant-expr
2982 For free-form source code, all standards contain a statement of the form:
2984 A blank shall be used to separate names, constants, or labels from
2985 adjacent keywords, names, constants, or labels.
2987 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
2991 is valid, but it is invalid Fortran 2008. */
2994 gfc_match_stopcode (gfc_statement st
)
3000 /* Set f95 for -std=f95. */
3001 f95
= (gfc_option
.allow_std
== GFC_STD_OPT_F95
);
3003 /* Set f03 for -std=f2003. */
3004 f03
= (gfc_option
.allow_std
== GFC_STD_OPT_F03
);
3006 /* Set f08 for -std=f2008. */
3007 f08
= (gfc_option
.allow_std
== GFC_STD_OPT_F08
);
3009 /* Look for a blank between STOP and the stop-code for F2008 or later. */
3010 if (gfc_current_form
!= FORM_FIXED
&& !(f95
|| f03
))
3012 char c
= gfc_peek_ascii_char ();
3014 /* Look for end-of-statement. There is no stop-code. */
3015 if (c
== '\n' || c
== '!' || c
== ';')
3020 gfc_error ("Blank required in %s statement near %C",
3021 gfc_ascii_statement (st
));
3026 if (gfc_match_eos () != MATCH_YES
)
3031 /* First look for the F95 or F2003 digit [...] construct. */
3032 old_locus
= gfc_current_locus
;
3033 m
= gfc_match_small_int (&stopcode
);
3034 if (m
== MATCH_YES
&& (f95
|| f03
))
3038 gfc_error ("STOP code at %C cannot be negative");
3042 if (stopcode
> 99999)
3044 gfc_error ("STOP code at %C contains too many digits");
3049 /* Reset the locus and now load gfc_expr. */
3050 gfc_current_locus
= old_locus
;
3051 m
= gfc_match_expr (&e
);
3052 if (m
== MATCH_ERROR
)
3057 if (gfc_match_eos () != MATCH_YES
)
3061 if (gfc_pure (NULL
))
3063 if (st
== ST_ERROR_STOP
)
3065 if (!gfc_notify_std (GFC_STD_F2018
, "%s statement at %C in PURE "
3066 "procedure", gfc_ascii_statement (st
)))
3071 gfc_error ("%s statement not allowed in PURE procedure at %C",
3072 gfc_ascii_statement (st
));
3077 gfc_unset_implicit_pure (NULL
);
3079 if (st
== ST_STOP
&& gfc_find_state (COMP_CRITICAL
))
3081 gfc_error ("Image control statement STOP at %C in CRITICAL block");
3084 if (st
== ST_STOP
&& gfc_find_state (COMP_DO_CONCURRENT
))
3086 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
3092 if (!gfc_simplify_expr (e
, 0))
3095 /* Test for F95 and F2003 style STOP stop-code. */
3096 if (e
->expr_type
!= EXPR_CONSTANT
&& (f95
|| f03
))
3098 gfc_error ("STOP code at %L must be a scalar CHARACTER constant "
3099 "or digit[digit[digit[digit[digit]]]]", &e
->where
);
3103 /* Use the machinery for an initialization expression to reduce the
3104 stop-code to a constant. */
3105 gfc_reduce_init_expr (e
);
3107 /* Test for F2008 style STOP stop-code. */
3108 if (e
->expr_type
!= EXPR_CONSTANT
&& f08
)
3110 gfc_error ("STOP code at %L must be a scalar default CHARACTER or "
3111 "INTEGER constant expression", &e
->where
);
3115 if (!(e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_INTEGER
))
3117 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
3124 gfc_error ("STOP code at %L must be scalar", &e
->where
);
3128 if (e
->ts
.type
== BT_CHARACTER
3129 && e
->ts
.kind
!= gfc_default_character_kind
)
3131 gfc_error ("STOP code at %L must be default character KIND=%d",
3132 &e
->where
, (int) gfc_default_character_kind
);
3136 if (e
->ts
.type
== BT_INTEGER
&& e
->ts
.kind
!= gfc_default_integer_kind
)
3138 gfc_error ("STOP code at %L must be default integer KIND=%d",
3139 &e
->where
, (int) gfc_default_integer_kind
);
3149 new_st
.op
= EXEC_STOP
;
3152 new_st
.op
= EXEC_ERROR_STOP
;
3155 new_st
.op
= EXEC_PAUSE
;
3162 new_st
.ext
.stop_code
= -1;
3167 gfc_syntax_error (st
);
3176 /* Match the (deprecated) PAUSE statement. */
3179 gfc_match_pause (void)
3183 m
= gfc_match_stopcode (ST_PAUSE
);
3186 if (!gfc_notify_std (GFC_STD_F95_DEL
, "PAUSE statement at %C"))
3193 /* Match the STOP statement. */
3196 gfc_match_stop (void)
3198 return gfc_match_stopcode (ST_STOP
);
3202 /* Match the ERROR STOP statement. */
3205 gfc_match_error_stop (void)
3207 if (!gfc_notify_std (GFC_STD_F2008
, "ERROR STOP statement at %C"))
3210 return gfc_match_stopcode (ST_ERROR_STOP
);
3213 /* Match EVENT POST/WAIT statement. Syntax:
3214 EVENT POST ( event-variable [, sync-stat-list] )
3215 EVENT WAIT ( event-variable [, wait-spec-list] )
3217 wait-spec-list is sync-stat-list or until-spec
3218 until-spec is UNTIL_COUNT = scalar-int-expr
3219 sync-stat is STAT= or ERRMSG=. */
3222 event_statement (gfc_statement st
)
3225 gfc_expr
*tmp
, *eventvar
, *until_count
, *stat
, *errmsg
;
3226 bool saw_until_count
, saw_stat
, saw_errmsg
;
3228 tmp
= eventvar
= until_count
= stat
= errmsg
= NULL
;
3229 saw_until_count
= saw_stat
= saw_errmsg
= false;
3231 if (gfc_pure (NULL
))
3233 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3234 st
== ST_EVENT_POST
? "POST" : "WAIT");
3238 gfc_unset_implicit_pure (NULL
);
3240 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3242 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3246 if (gfc_find_state (COMP_CRITICAL
))
3248 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3249 st
== ST_EVENT_POST
? "POST" : "WAIT");
3253 if (gfc_find_state (COMP_DO_CONCURRENT
))
3255 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3256 "block", st
== ST_EVENT_POST
? "POST" : "WAIT");
3260 if (gfc_match_char ('(') != MATCH_YES
)
3263 if (gfc_match ("%e", &eventvar
) != MATCH_YES
)
3265 m
= gfc_match_char (',');
3266 if (m
== MATCH_ERROR
)
3270 m
= gfc_match_char (')');
3278 m
= gfc_match (" stat = %v", &tmp
);
3279 if (m
== MATCH_ERROR
)
3285 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
3291 m
= gfc_match_char (',');
3299 m
= gfc_match (" errmsg = %v", &tmp
);
3300 if (m
== MATCH_ERROR
)
3306 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
3312 m
= gfc_match_char (',');
3320 m
= gfc_match (" until_count = %e", &tmp
);
3321 if (m
== MATCH_ERROR
|| st
== ST_EVENT_POST
)
3325 if (saw_until_count
)
3327 gfc_error ("Redundant UNTIL_COUNT tag found at %L",
3332 saw_until_count
= true;
3334 m
= gfc_match_char (',');
3345 if (m
== MATCH_ERROR
)
3348 if (gfc_match (" )%t") != MATCH_YES
)
3355 new_st
.op
= EXEC_EVENT_POST
;
3358 new_st
.op
= EXEC_EVENT_WAIT
;
3364 new_st
.expr1
= eventvar
;
3365 new_st
.expr2
= stat
;
3366 new_st
.expr3
= errmsg
;
3367 new_st
.expr4
= until_count
;
3372 gfc_syntax_error (st
);
3375 if (until_count
!= tmp
)
3376 gfc_free_expr (until_count
);
3378 gfc_free_expr (errmsg
);
3380 gfc_free_expr (stat
);
3382 gfc_free_expr (tmp
);
3383 gfc_free_expr (eventvar
);
3391 gfc_match_event_post (void)
3393 if (!gfc_notify_std (GFC_STD_F2018
, "EVENT POST statement at %C"))
3396 return event_statement (ST_EVENT_POST
);
3401 gfc_match_event_wait (void)
3403 if (!gfc_notify_std (GFC_STD_F2018
, "EVENT WAIT statement at %C"))
3406 return event_statement (ST_EVENT_WAIT
);
3410 /* Match a FAIL IMAGE statement. */
3413 gfc_match_fail_image (void)
3415 if (!gfc_notify_std (GFC_STD_F2018
, "FAIL IMAGE statement at %C"))
3418 if (gfc_match_char ('(') == MATCH_YES
)
3421 new_st
.op
= EXEC_FAIL_IMAGE
;
3426 gfc_syntax_error (ST_FAIL_IMAGE
);
3431 /* Match a FORM TEAM statement. */
3434 gfc_match_form_team (void)
3437 gfc_expr
*teamid
,*team
;
3439 if (!gfc_notify_std (GFC_STD_F2018
, "FORM TEAM statement at %C"))
3442 if (gfc_match_char ('(') == MATCH_NO
)
3445 new_st
.op
= EXEC_FORM_TEAM
;
3447 if (gfc_match ("%e", &teamid
) != MATCH_YES
)
3449 m
= gfc_match_char (',');
3450 if (m
== MATCH_ERROR
)
3452 if (gfc_match ("%e", &team
) != MATCH_YES
)
3455 m
= gfc_match_char (')');
3459 new_st
.expr1
= teamid
;
3460 new_st
.expr2
= team
;
3465 gfc_syntax_error (ST_FORM_TEAM
);
3470 /* Match a CHANGE TEAM statement. */
3473 gfc_match_change_team (void)
3478 if (!gfc_notify_std (GFC_STD_F2018
, "CHANGE TEAM statement at %C"))
3481 if (gfc_match_char ('(') == MATCH_NO
)
3484 new_st
.op
= EXEC_CHANGE_TEAM
;
3486 if (gfc_match ("%e", &team
) != MATCH_YES
)
3489 m
= gfc_match_char (')');
3493 new_st
.expr1
= team
;
3498 gfc_syntax_error (ST_CHANGE_TEAM
);
3503 /* Match a END TEAM statement. */
3506 gfc_match_end_team (void)
3508 if (!gfc_notify_std (GFC_STD_F2018
, "END TEAM statement at %C"))
3511 if (gfc_match_char ('(') == MATCH_YES
)
3514 new_st
.op
= EXEC_END_TEAM
;
3519 gfc_syntax_error (ST_END_TEAM
);
3524 /* Match a SYNC TEAM statement. */
3527 gfc_match_sync_team (void)
3532 if (!gfc_notify_std (GFC_STD_F2018
, "SYNC TEAM statement at %C"))
3535 if (gfc_match_char ('(') == MATCH_NO
)
3538 new_st
.op
= EXEC_SYNC_TEAM
;
3540 if (gfc_match ("%e", &team
) != MATCH_YES
)
3543 m
= gfc_match_char (')');
3547 new_st
.expr1
= team
;
3552 gfc_syntax_error (ST_SYNC_TEAM
);
3557 /* Match LOCK/UNLOCK statement. Syntax:
3558 LOCK ( lock-variable [ , lock-stat-list ] )
3559 UNLOCK ( lock-variable [ , sync-stat-list ] )
3560 where lock-stat is ACQUIRED_LOCK or sync-stat
3561 and sync-stat is STAT= or ERRMSG=. */
3564 lock_unlock_statement (gfc_statement st
)
3567 gfc_expr
*tmp
, *lockvar
, *acq_lock
, *stat
, *errmsg
;
3568 bool saw_acq_lock
, saw_stat
, saw_errmsg
;
3570 tmp
= lockvar
= acq_lock
= stat
= errmsg
= NULL
;
3571 saw_acq_lock
= saw_stat
= saw_errmsg
= false;
3573 if (gfc_pure (NULL
))
3575 gfc_error ("Image control statement %s at %C in PURE procedure",
3576 st
== ST_LOCK
? "LOCK" : "UNLOCK");
3580 gfc_unset_implicit_pure (NULL
);
3582 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3584 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3588 if (gfc_find_state (COMP_CRITICAL
))
3590 gfc_error ("Image control statement %s at %C in CRITICAL block",
3591 st
== ST_LOCK
? "LOCK" : "UNLOCK");
3595 if (gfc_find_state (COMP_DO_CONCURRENT
))
3597 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3598 st
== ST_LOCK
? "LOCK" : "UNLOCK");
3602 if (gfc_match_char ('(') != MATCH_YES
)
3605 if (gfc_match ("%e", &lockvar
) != MATCH_YES
)
3607 m
= gfc_match_char (',');
3608 if (m
== MATCH_ERROR
)
3612 m
= gfc_match_char (')');
3620 m
= gfc_match (" stat = %v", &tmp
);
3621 if (m
== MATCH_ERROR
)
3627 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
3633 m
= gfc_match_char (',');
3641 m
= gfc_match (" errmsg = %v", &tmp
);
3642 if (m
== MATCH_ERROR
)
3648 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
3654 m
= gfc_match_char (',');
3662 m
= gfc_match (" acquired_lock = %v", &tmp
);
3663 if (m
== MATCH_ERROR
|| st
== ST_UNLOCK
)
3669 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L",
3674 saw_acq_lock
= true;
3676 m
= gfc_match_char (',');
3687 if (m
== MATCH_ERROR
)
3690 if (gfc_match (" )%t") != MATCH_YES
)
3697 new_st
.op
= EXEC_LOCK
;
3700 new_st
.op
= EXEC_UNLOCK
;
3706 new_st
.expr1
= lockvar
;
3707 new_st
.expr2
= stat
;
3708 new_st
.expr3
= errmsg
;
3709 new_st
.expr4
= acq_lock
;
3714 gfc_syntax_error (st
);
3717 if (acq_lock
!= tmp
)
3718 gfc_free_expr (acq_lock
);
3720 gfc_free_expr (errmsg
);
3722 gfc_free_expr (stat
);
3724 gfc_free_expr (tmp
);
3725 gfc_free_expr (lockvar
);
3732 gfc_match_lock (void)
3734 if (!gfc_notify_std (GFC_STD_F2008
, "LOCK statement at %C"))
3737 return lock_unlock_statement (ST_LOCK
);
3742 gfc_match_unlock (void)
3744 if (!gfc_notify_std (GFC_STD_F2008
, "UNLOCK statement at %C"))
3747 return lock_unlock_statement (ST_UNLOCK
);
3751 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3752 SYNC ALL [(sync-stat-list)]
3753 SYNC MEMORY [(sync-stat-list)]
3754 SYNC IMAGES (image-set [, sync-stat-list] )
3755 with sync-stat is int-expr or *. */
3758 sync_statement (gfc_statement st
)
3761 gfc_expr
*tmp
, *imageset
, *stat
, *errmsg
;
3762 bool saw_stat
, saw_errmsg
;
3764 tmp
= imageset
= stat
= errmsg
= NULL
;
3765 saw_stat
= saw_errmsg
= false;
3767 if (gfc_pure (NULL
))
3769 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3773 gfc_unset_implicit_pure (NULL
);
3775 if (!gfc_notify_std (GFC_STD_F2008
, "SYNC statement at %C"))
3778 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3780 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3785 if (gfc_find_state (COMP_CRITICAL
))
3787 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3791 if (gfc_find_state (COMP_DO_CONCURRENT
))
3793 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3797 if (gfc_match_eos () == MATCH_YES
)
3799 if (st
== ST_SYNC_IMAGES
)
3804 if (gfc_match_char ('(') != MATCH_YES
)
3807 if (st
== ST_SYNC_IMAGES
)
3809 /* Denote '*' as imageset == NULL. */
3810 m
= gfc_match_char ('*');
3811 if (m
== MATCH_ERROR
)
3815 if (gfc_match ("%e", &imageset
) != MATCH_YES
)
3818 m
= gfc_match_char (',');
3819 if (m
== MATCH_ERROR
)
3823 m
= gfc_match_char (')');
3832 m
= gfc_match (" stat = %e", &tmp
);
3833 if (m
== MATCH_ERROR
)
3839 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
3845 if (gfc_match_char (',') == MATCH_YES
)
3852 m
= gfc_match (" errmsg = %e", &tmp
);
3853 if (m
== MATCH_ERROR
)
3859 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
3865 if (gfc_match_char (',') == MATCH_YES
)
3875 if (gfc_match (" )%t") != MATCH_YES
)
3882 new_st
.op
= EXEC_SYNC_ALL
;
3884 case ST_SYNC_IMAGES
:
3885 new_st
.op
= EXEC_SYNC_IMAGES
;
3887 case ST_SYNC_MEMORY
:
3888 new_st
.op
= EXEC_SYNC_MEMORY
;
3894 new_st
.expr1
= imageset
;
3895 new_st
.expr2
= stat
;
3896 new_st
.expr3
= errmsg
;
3901 gfc_syntax_error (st
);
3905 gfc_free_expr (stat
);
3907 gfc_free_expr (errmsg
);
3909 gfc_free_expr (tmp
);
3910 gfc_free_expr (imageset
);
3916 /* Match SYNC ALL statement. */
3919 gfc_match_sync_all (void)
3921 return sync_statement (ST_SYNC_ALL
);
3925 /* Match SYNC IMAGES statement. */
3928 gfc_match_sync_images (void)
3930 return sync_statement (ST_SYNC_IMAGES
);
3934 /* Match SYNC MEMORY statement. */
3937 gfc_match_sync_memory (void)
3939 return sync_statement (ST_SYNC_MEMORY
);
3943 /* Match a CONTINUE statement. */
3946 gfc_match_continue (void)
3948 if (gfc_match_eos () != MATCH_YES
)
3950 gfc_syntax_error (ST_CONTINUE
);
3954 new_st
.op
= EXEC_CONTINUE
;
3959 /* Match the (deprecated) ASSIGN statement. */
3962 gfc_match_assign (void)
3965 gfc_st_label
*label
;
3967 if (gfc_match (" %l", &label
) == MATCH_YES
)
3969 if (!gfc_reference_st_label (label
, ST_LABEL_UNKNOWN
))
3971 if (gfc_match (" to %v%t", &expr
) == MATCH_YES
)
3973 if (!gfc_notify_std (GFC_STD_F95_DEL
, "ASSIGN statement at %C"))
3976 expr
->symtree
->n
.sym
->attr
.assign
= 1;
3978 new_st
.op
= EXEC_LABEL_ASSIGN
;
3979 new_st
.label1
= label
;
3980 new_st
.expr1
= expr
;
3988 /* Match the GO TO statement. As a computed GOTO statement is
3989 matched, it is transformed into an equivalent SELECT block. No
3990 tree is necessary, and the resulting jumps-to-jumps are
3991 specifically optimized away by the back end. */
3994 gfc_match_goto (void)
3996 gfc_code
*head
, *tail
;
3999 gfc_st_label
*label
;
4003 if (gfc_match (" %l%t", &label
) == MATCH_YES
)
4005 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
4008 new_st
.op
= EXEC_GOTO
;
4009 new_st
.label1
= label
;
4013 /* The assigned GO TO statement. */
4015 if (gfc_match_variable (&expr
, 0) == MATCH_YES
)
4017 if (!gfc_notify_std (GFC_STD_F95_DEL
, "Assigned GOTO statement at %C"))
4020 new_st
.op
= EXEC_GOTO
;
4021 new_st
.expr1
= expr
;
4023 if (gfc_match_eos () == MATCH_YES
)
4026 /* Match label list. */
4027 gfc_match_char (',');
4028 if (gfc_match_char ('(') != MATCH_YES
)
4030 gfc_syntax_error (ST_GOTO
);
4037 m
= gfc_match_st_label (&label
);
4041 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
4045 head
= tail
= gfc_get_code (EXEC_GOTO
);
4048 tail
->block
= gfc_get_code (EXEC_GOTO
);
4052 tail
->label1
= label
;
4054 while (gfc_match_char (',') == MATCH_YES
);
4056 if (gfc_match (" )%t") != MATCH_YES
)
4061 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4064 new_st
.block
= head
;
4069 /* Last chance is a computed GO TO statement. */
4070 if (gfc_match_char ('(') != MATCH_YES
)
4072 gfc_syntax_error (ST_GOTO
);
4081 m
= gfc_match_st_label (&label
);
4085 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
4089 head
= tail
= gfc_get_code (EXEC_SELECT
);
4092 tail
->block
= gfc_get_code (EXEC_SELECT
);
4096 cp
= gfc_get_case ();
4097 cp
->low
= cp
->high
= gfc_get_int_expr (gfc_default_integer_kind
,
4100 tail
->ext
.block
.case_list
= cp
;
4102 tail
->next
= gfc_get_code (EXEC_GOTO
);
4103 tail
->next
->label1
= label
;
4105 while (gfc_match_char (',') == MATCH_YES
);
4107 if (gfc_match_char (')') != MATCH_YES
)
4112 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4116 /* Get the rest of the statement. */
4117 gfc_match_char (',');
4119 if (gfc_match (" %e%t", &expr
) != MATCH_YES
)
4122 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Computed GOTO at %C"))
4125 /* At this point, a computed GOTO has been fully matched and an
4126 equivalent SELECT statement constructed. */
4128 new_st
.op
= EXEC_SELECT
;
4129 new_st
.expr1
= NULL
;
4131 /* Hack: For a "real" SELECT, the expression is in expr. We put
4132 it in expr2 so we can distinguish then and produce the correct
4134 new_st
.expr2
= expr
;
4135 new_st
.block
= head
;
4139 gfc_syntax_error (ST_GOTO
);
4141 gfc_free_statements (head
);
4146 /* Frees a list of gfc_alloc structures. */
4149 gfc_free_alloc_list (gfc_alloc
*p
)
4156 gfc_free_expr (p
->expr
);
4162 /* Match an ALLOCATE statement. */
4165 gfc_match_allocate (void)
4167 gfc_alloc
*head
, *tail
;
4168 gfc_expr
*stat
, *errmsg
, *tmp
, *source
, *mold
;
4172 locus old_locus
, deferred_locus
, assumed_locus
;
4173 bool saw_stat
, saw_errmsg
, saw_source
, saw_mold
, saw_deferred
, b1
, b2
, b3
;
4174 bool saw_unlimited
= false, saw_assumed
= false;
4177 stat
= errmsg
= source
= mold
= tmp
= NULL
;
4178 saw_stat
= saw_errmsg
= saw_source
= saw_mold
= saw_deferred
= false;
4180 if (gfc_match_char ('(') != MATCH_YES
)
4182 gfc_syntax_error (ST_ALLOCATE
);
4186 /* Match an optional type-spec. */
4187 old_locus
= gfc_current_locus
;
4188 m
= gfc_match_type_spec (&ts
);
4189 if (m
== MATCH_ERROR
)
4191 else if (m
== MATCH_NO
)
4193 char name
[GFC_MAX_SYMBOL_LEN
+ 3];
4195 if (gfc_match ("%n :: ", name
) == MATCH_YES
)
4197 gfc_error ("Error in type-spec at %L", &old_locus
);
4201 ts
.type
= BT_UNKNOWN
;
4205 /* Needed for the F2008:C631 check below. */
4206 assumed_locus
= gfc_current_locus
;
4208 if (gfc_match (" :: ") == MATCH_YES
)
4210 if (!gfc_notify_std (GFC_STD_F2003
, "typespec in ALLOCATE at %L",
4216 gfc_error ("Type-spec at %L cannot contain a deferred "
4217 "type parameter", &old_locus
);
4221 if (ts
.type
== BT_CHARACTER
)
4223 if (!ts
.u
.cl
->length
)
4226 ts
.u
.cl
->length_from_typespec
= true;
4229 if (type_param_spec_list
4230 && gfc_spec_list_type (type_param_spec_list
, NULL
)
4233 gfc_error ("The type parameter spec list in the type-spec at "
4234 "%L cannot contain DEFERRED parameters", &old_locus
);
4240 ts
.type
= BT_UNKNOWN
;
4241 gfc_current_locus
= old_locus
;
4248 head
= tail
= gfc_get_alloc ();
4251 tail
->next
= gfc_get_alloc ();
4255 m
= gfc_match_variable (&tail
->expr
, 0);
4258 if (m
== MATCH_ERROR
)
4261 if (tail
->expr
->expr_type
== EXPR_CONSTANT
)
4263 gfc_error ("Unexpected constant at %C");
4267 if (gfc_check_do_variable (tail
->expr
->symtree
))
4270 bool impure
= gfc_impure_variable (tail
->expr
->symtree
->n
.sym
);
4271 if (impure
&& gfc_pure (NULL
))
4273 gfc_error ("Bad allocate-object at %C for a PURE procedure");
4278 gfc_unset_implicit_pure (NULL
);
4280 /* F2008:C631 (R626) A type-param-value in a type-spec shall be an
4281 asterisk if and only if each allocate-object is a dummy argument
4282 for which the corresponding type parameter is assumed. */
4284 && (tail
->expr
->ts
.deferred
4285 || (tail
->expr
->ts
.u
.cl
&& tail
->expr
->ts
.u
.cl
->length
)
4286 || tail
->expr
->symtree
->n
.sym
->attr
.dummy
== 0))
4288 gfc_error ("Incompatible allocate-object at %C for CHARACTER "
4289 "type-spec at %L", &assumed_locus
);
4293 if (tail
->expr
->ts
.deferred
)
4295 saw_deferred
= true;
4296 deferred_locus
= tail
->expr
->where
;
4299 if (gfc_find_state (COMP_DO_CONCURRENT
)
4300 || gfc_find_state (COMP_CRITICAL
))
4303 bool coarray
= tail
->expr
->symtree
->n
.sym
->attr
.codimension
;
4304 for (ref
= tail
->expr
->ref
; ref
; ref
= ref
->next
)
4305 if (ref
->type
== REF_COMPONENT
)
4306 coarray
= ref
->u
.c
.component
->attr
.codimension
;
4308 if (coarray
&& gfc_find_state (COMP_DO_CONCURRENT
))
4310 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
4313 if (coarray
&& gfc_find_state (COMP_CRITICAL
))
4315 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
4320 /* Check for F08:C628. */
4321 sym
= tail
->expr
->symtree
->n
.sym
;
4322 b1
= !(tail
->expr
->ref
4323 && (tail
->expr
->ref
->type
== REF_COMPONENT
4324 || tail
->expr
->ref
->type
== REF_ARRAY
));
4325 if (sym
&& sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
4326 b2
= !(CLASS_DATA (sym
)->attr
.allocatable
4327 || CLASS_DATA (sym
)->attr
.class_pointer
);
4329 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
4330 || sym
->attr
.proc_pointer
);
4331 b3
= sym
&& sym
->ns
&& sym
->ns
->proc_name
4332 && (sym
->ns
->proc_name
->attr
.allocatable
4333 || sym
->ns
->proc_name
->attr
.pointer
4334 || sym
->ns
->proc_name
->attr
.proc_pointer
);
4335 if (b1
&& b2
&& !b3
)
4337 gfc_error ("Allocate-object at %L is neither a data pointer "
4338 "nor an allocatable variable", &tail
->expr
->where
);
4342 /* The ALLOCATE statement had an optional typespec. Check the
4344 if (ts
.type
!= BT_UNKNOWN
)
4346 /* Enforce F03:C624. */
4347 if (!gfc_type_compatible (&tail
->expr
->ts
, &ts
))
4349 gfc_error ("Type of entity at %L is type incompatible with "
4350 "typespec", &tail
->expr
->where
);
4354 /* Enforce F03:C627. */
4355 if (ts
.kind
!= tail
->expr
->ts
.kind
&& !UNLIMITED_POLY (tail
->expr
))
4357 gfc_error ("Kind type parameter for entity at %L differs from "
4358 "the kind type parameter of the typespec",
4359 &tail
->expr
->where
);
4364 if (tail
->expr
->ts
.type
== BT_DERIVED
)
4365 tail
->expr
->ts
.u
.derived
= gfc_use_derived (tail
->expr
->ts
.u
.derived
);
4367 if (type_param_spec_list
)
4368 tail
->expr
->param_list
= gfc_copy_actual_arglist (type_param_spec_list
);
4370 saw_unlimited
= saw_unlimited
| UNLIMITED_POLY (tail
->expr
);
4372 if (gfc_peek_ascii_char () == '(' && !sym
->attr
.dimension
)
4374 gfc_error ("Shape specification for allocatable scalar at %C");
4378 if (gfc_match_char (',') != MATCH_YES
)
4383 m
= gfc_match (" stat = %e", &tmp
);
4384 if (m
== MATCH_ERROR
)
4391 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
4399 if (stat
->expr_type
== EXPR_CONSTANT
)
4401 gfc_error ("STAT tag at %L cannot be a constant", &stat
->where
);
4405 if (gfc_check_do_variable (stat
->symtree
))
4408 if (gfc_match_char (',') == MATCH_YES
)
4409 goto alloc_opt_list
;
4412 m
= gfc_match (" errmsg = %e", &tmp
);
4413 if (m
== MATCH_ERROR
)
4417 if (!gfc_notify_std (GFC_STD_F2003
, "ERRMSG tag at %L", &tmp
->where
))
4423 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
4431 if (gfc_match_char (',') == MATCH_YES
)
4432 goto alloc_opt_list
;
4435 m
= gfc_match (" source = %e", &tmp
);
4436 if (m
== MATCH_ERROR
)
4440 if (!gfc_notify_std (GFC_STD_F2003
, "SOURCE tag at %L", &tmp
->where
))
4446 gfc_error ("Redundant SOURCE tag found at %L", &tmp
->where
);
4450 /* The next 2 conditionals check C631. */
4451 if (ts
.type
!= BT_UNKNOWN
)
4453 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4454 &tmp
->where
, &old_locus
);
4459 && !gfc_notify_std (GFC_STD_F2008
, "SOURCE tag at %L"
4460 " with more than a single allocate object",
4468 if (gfc_match_char (',') == MATCH_YES
)
4469 goto alloc_opt_list
;
4472 m
= gfc_match (" mold = %e", &tmp
);
4473 if (m
== MATCH_ERROR
)
4477 if (!gfc_notify_std (GFC_STD_F2008
, "MOLD tag at %L", &tmp
->where
))
4480 /* Check F08:C636. */
4483 gfc_error ("Redundant MOLD tag found at %L", &tmp
->where
);
4487 /* Check F08:C637. */
4488 if (ts
.type
!= BT_UNKNOWN
)
4490 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4491 &tmp
->where
, &old_locus
);
4500 if (gfc_match_char (',') == MATCH_YES
)
4501 goto alloc_opt_list
;
4504 gfc_gobble_whitespace ();
4506 if (gfc_peek_char () == ')')
4510 if (gfc_match (" )%t") != MATCH_YES
)
4513 /* Check F08:C637. */
4516 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4517 &mold
->where
, &source
->where
);
4521 /* Check F03:C623, */
4522 if (saw_deferred
&& ts
.type
== BT_UNKNOWN
&& !source
&& !mold
)
4524 gfc_error ("Allocate-object at %L with a deferred type parameter "
4525 "requires either a type-spec or SOURCE tag or a MOLD tag",
4530 /* Check F03:C625, */
4531 if (saw_unlimited
&& ts
.type
== BT_UNKNOWN
&& !source
&& !mold
)
4533 for (tail
= head
; tail
; tail
= tail
->next
)
4535 if (UNLIMITED_POLY (tail
->expr
))
4536 gfc_error ("Unlimited polymorphic allocate-object at %L "
4537 "requires either a type-spec or SOURCE tag "
4538 "or a MOLD tag", &tail
->expr
->where
);
4543 new_st
.op
= EXEC_ALLOCATE
;
4544 new_st
.expr1
= stat
;
4545 new_st
.expr2
= errmsg
;
4547 new_st
.expr3
= source
;
4549 new_st
.expr3
= mold
;
4550 new_st
.ext
.alloc
.list
= head
;
4551 new_st
.ext
.alloc
.ts
= ts
;
4553 if (type_param_spec_list
)
4554 gfc_free_actual_arglist (type_param_spec_list
);
4559 gfc_syntax_error (ST_ALLOCATE
);
4562 gfc_free_expr (errmsg
);
4563 gfc_free_expr (source
);
4564 gfc_free_expr (stat
);
4565 gfc_free_expr (mold
);
4566 if (tmp
&& tmp
->expr_type
) gfc_free_expr (tmp
);
4567 gfc_free_alloc_list (head
);
4568 if (type_param_spec_list
)
4569 gfc_free_actual_arglist (type_param_spec_list
);
4574 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4575 a set of pointer assignments to intrinsic NULL(). */
4578 gfc_match_nullify (void)
4586 if (gfc_match_char ('(') != MATCH_YES
)
4591 m
= gfc_match_variable (&p
, 0);
4592 if (m
== MATCH_ERROR
)
4597 if (gfc_check_do_variable (p
->symtree
))
4601 if (gfc_is_coindexed (p
))
4603 gfc_error ("Pointer object at %C shall not be coindexed");
4607 /* Check for valid array pointer object. Bounds remapping is not
4608 allowed with NULLIFY. */
4611 gfc_ref
*remap
= p
->ref
;
4612 for (; remap
; remap
= remap
->next
)
4613 if (!remap
->next
&& remap
->type
== REF_ARRAY
4614 && remap
->u
.ar
.type
!= AR_FULL
)
4618 gfc_error ("NULLIFY does not allow bounds remapping for "
4619 "pointer object at %C");
4624 /* build ' => NULL() '. */
4625 e
= gfc_get_null_expr (&gfc_current_locus
);
4627 /* Chain to list. */
4631 tail
->op
= EXEC_POINTER_ASSIGN
;
4635 tail
->next
= gfc_get_code (EXEC_POINTER_ASSIGN
);
4642 if (gfc_match (" )%t") == MATCH_YES
)
4644 if (gfc_match_char (',') != MATCH_YES
)
4651 gfc_syntax_error (ST_NULLIFY
);
4654 gfc_free_statements (new_st
.next
);
4656 gfc_free_expr (new_st
.expr1
);
4657 new_st
.expr1
= NULL
;
4658 gfc_free_expr (new_st
.expr2
);
4659 new_st
.expr2
= NULL
;
4664 /* Match a DEALLOCATE statement. */
4667 gfc_match_deallocate (void)
4669 gfc_alloc
*head
, *tail
;
4670 gfc_expr
*stat
, *errmsg
, *tmp
;
4673 bool saw_stat
, saw_errmsg
, b1
, b2
;
4676 stat
= errmsg
= tmp
= NULL
;
4677 saw_stat
= saw_errmsg
= false;
4679 if (gfc_match_char ('(') != MATCH_YES
)
4685 head
= tail
= gfc_get_alloc ();
4688 tail
->next
= gfc_get_alloc ();
4692 m
= gfc_match_variable (&tail
->expr
, 0);
4693 if (m
== MATCH_ERROR
)
4698 if (tail
->expr
->expr_type
== EXPR_CONSTANT
)
4700 gfc_error ("Unexpected constant at %C");
4704 if (gfc_check_do_variable (tail
->expr
->symtree
))
4707 sym
= tail
->expr
->symtree
->n
.sym
;
4709 bool impure
= gfc_impure_variable (sym
);
4710 if (impure
&& gfc_pure (NULL
))
4712 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4717 gfc_unset_implicit_pure (NULL
);
4719 if (gfc_is_coarray (tail
->expr
)
4720 && gfc_find_state (COMP_DO_CONCURRENT
))
4722 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4726 if (gfc_is_coarray (tail
->expr
)
4727 && gfc_find_state (COMP_CRITICAL
))
4729 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4733 /* FIXME: disable the checking on derived types. */
4734 b1
= !(tail
->expr
->ref
4735 && (tail
->expr
->ref
->type
== REF_COMPONENT
4736 || tail
->expr
->ref
->type
== REF_ARRAY
));
4737 if (sym
&& sym
->ts
.type
== BT_CLASS
)
4738 b2
= !(CLASS_DATA (sym
) && (CLASS_DATA (sym
)->attr
.allocatable
4739 || CLASS_DATA (sym
)->attr
.class_pointer
));
4741 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
4742 || sym
->attr
.proc_pointer
);
4745 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4746 "nor an allocatable variable");
4750 if (gfc_match_char (',') != MATCH_YES
)
4755 m
= gfc_match (" stat = %e", &tmp
);
4756 if (m
== MATCH_ERROR
)
4762 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
4763 gfc_free_expr (tmp
);
4770 if (gfc_check_do_variable (stat
->symtree
))
4773 if (gfc_match_char (',') == MATCH_YES
)
4774 goto dealloc_opt_list
;
4777 m
= gfc_match (" errmsg = %e", &tmp
);
4778 if (m
== MATCH_ERROR
)
4782 if (!gfc_notify_std (GFC_STD_F2003
, "ERRMSG at %L", &tmp
->where
))
4787 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
4788 gfc_free_expr (tmp
);
4795 if (gfc_match_char (',') == MATCH_YES
)
4796 goto dealloc_opt_list
;
4799 gfc_gobble_whitespace ();
4801 if (gfc_peek_char () == ')')
4805 if (gfc_match (" )%t") != MATCH_YES
)
4808 new_st
.op
= EXEC_DEALLOCATE
;
4809 new_st
.expr1
= stat
;
4810 new_st
.expr2
= errmsg
;
4811 new_st
.ext
.alloc
.list
= head
;
4816 gfc_syntax_error (ST_DEALLOCATE
);
4819 gfc_free_expr (errmsg
);
4820 gfc_free_expr (stat
);
4821 gfc_free_alloc_list (head
);
4826 /* Match a RETURN statement. */
4829 gfc_match_return (void)
4833 gfc_compile_state s
;
4837 if (gfc_find_state (COMP_CRITICAL
))
4839 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4843 if (gfc_find_state (COMP_DO_CONCURRENT
))
4845 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4849 if (gfc_match_eos () == MATCH_YES
)
4852 if (!gfc_find_state (COMP_SUBROUTINE
))
4854 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4859 if (gfc_current_form
== FORM_FREE
)
4861 /* The following are valid, so we can't require a blank after the
4865 char c
= gfc_peek_ascii_char ();
4866 if (ISALPHA (c
) || ISDIGIT (c
))
4870 m
= gfc_match (" %e%t", &e
);
4873 if (m
== MATCH_ERROR
)
4876 gfc_syntax_error (ST_RETURN
);
4883 gfc_enclosing_unit (&s
);
4884 if (s
== COMP_PROGRAM
4885 && !gfc_notify_std (GFC_STD_GNU
, "RETURN statement in "
4886 "main program at %C"))
4889 new_st
.op
= EXEC_RETURN
;
4896 /* Match the call of a type-bound procedure, if CALL%var has already been
4897 matched and var found to be a derived-type variable. */
4900 match_typebound_call (gfc_symtree
* varst
)
4905 base
= gfc_get_expr ();
4906 base
->expr_type
= EXPR_VARIABLE
;
4907 base
->symtree
= varst
;
4908 base
->where
= gfc_current_locus
;
4909 gfc_set_sym_referenced (varst
->n
.sym
);
4911 m
= gfc_match_varspec (base
, 0, true, true);
4913 gfc_error ("Expected component reference at %C");
4916 gfc_free_expr (base
);
4920 if (gfc_match_eos () != MATCH_YES
)
4922 gfc_error ("Junk after CALL at %C");
4923 gfc_free_expr (base
);
4927 if (base
->expr_type
== EXPR_COMPCALL
)
4928 new_st
.op
= EXEC_COMPCALL
;
4929 else if (base
->expr_type
== EXPR_PPC
)
4930 new_st
.op
= EXEC_CALL_PPC
;
4933 gfc_error ("Expected type-bound procedure or procedure pointer component "
4935 gfc_free_expr (base
);
4938 new_st
.expr1
= base
;
4944 /* Match a CALL statement. The tricky part here are possible
4945 alternate return specifiers. We handle these by having all
4946 "subroutines" actually return an integer via a register that gives
4947 the return number. If the call specifies alternate returns, we
4948 generate code for a SELECT statement whose case clauses contain
4949 GOTOs to the various labels. */
4952 gfc_match_call (void)
4954 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4955 gfc_actual_arglist
*a
, *arglist
;
4965 m
= gfc_match ("% %n", name
);
4971 if (gfc_get_ha_sym_tree (name
, &st
))
4976 /* If this is a variable of derived-type, it probably starts a type-bound
4977 procedure call. Associate variable targets have to be resolved for the
4979 if (((sym
->attr
.flavor
!= FL_PROCEDURE
4980 || gfc_is_function_return_value (sym
, gfc_current_ns
))
4981 && (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
))
4983 (sym
->assoc
&& sym
->assoc
->target
4984 && gfc_resolve_expr (sym
->assoc
->target
)
4985 && (sym
->assoc
->target
->ts
.type
== BT_DERIVED
4986 || sym
->assoc
->target
->ts
.type
== BT_CLASS
)))
4987 return match_typebound_call (st
);
4989 /* If it does not seem to be callable (include functions so that the
4990 right association is made. They are thrown out in resolution.)
4992 if (!sym
->attr
.generic
4993 && !sym
->attr
.subroutine
4994 && !sym
->attr
.function
)
4996 if (!(sym
->attr
.external
&& !sym
->attr
.referenced
))
4998 /* ...create a symbol in this scope... */
4999 if (sym
->ns
!= gfc_current_ns
5000 && gfc_get_sym_tree (name
, NULL
, &st
, false) == 1)
5003 if (sym
!= st
->n
.sym
)
5007 /* ...and then to try to make the symbol into a subroutine. */
5008 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
5012 gfc_set_sym_referenced (sym
);
5014 if (gfc_match_eos () != MATCH_YES
)
5016 m
= gfc_match_actual_arglist (1, &arglist
);
5019 if (m
== MATCH_ERROR
)
5022 if (gfc_match_eos () != MATCH_YES
)
5026 /* Walk the argument list looking for invalid BOZ. */
5027 for (a
= arglist
; a
; a
= a
->next
)
5028 if (a
->expr
&& a
->expr
->ts
.type
== BT_BOZ
)
5030 gfc_error ("A BOZ literal constant at %L cannot appear as an actual "
5031 "argument in a subroutine reference", &a
->expr
->where
);
5036 /* If any alternate return labels were found, construct a SELECT
5037 statement that will jump to the right place. */
5040 for (a
= arglist
; a
; a
= a
->next
)
5041 if (a
->expr
== NULL
)
5049 gfc_symtree
*select_st
;
5050 gfc_symbol
*select_sym
;
5051 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5053 new_st
.next
= c
= gfc_get_code (EXEC_SELECT
);
5054 sprintf (name
, "_result_%s", sym
->name
);
5055 gfc_get_ha_sym_tree (name
, &select_st
); /* Can't fail. */
5057 select_sym
= select_st
->n
.sym
;
5058 select_sym
->ts
.type
= BT_INTEGER
;
5059 select_sym
->ts
.kind
= gfc_default_integer_kind
;
5060 gfc_set_sym_referenced (select_sym
);
5061 c
->expr1
= gfc_get_expr ();
5062 c
->expr1
->expr_type
= EXPR_VARIABLE
;
5063 c
->expr1
->symtree
= select_st
;
5064 c
->expr1
->ts
= select_sym
->ts
;
5065 c
->expr1
->where
= gfc_current_locus
;
5068 for (a
= arglist
; a
; a
= a
->next
)
5070 if (a
->expr
!= NULL
)
5073 if (!gfc_reference_st_label (a
->label
, ST_LABEL_TARGET
))
5078 c
->block
= gfc_get_code (EXEC_SELECT
);
5081 new_case
= gfc_get_case ();
5082 new_case
->high
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, i
);
5083 new_case
->low
= new_case
->high
;
5084 c
->ext
.block
.case_list
= new_case
;
5086 c
->next
= gfc_get_code (EXEC_GOTO
);
5087 c
->next
->label1
= a
->label
;
5091 new_st
.op
= EXEC_CALL
;
5092 new_st
.symtree
= st
;
5093 new_st
.ext
.actual
= arglist
;
5098 gfc_syntax_error (ST_CALL
);
5101 gfc_free_actual_arglist (arglist
);
5106 /* Given a name, return a pointer to the common head structure,
5107 creating it if it does not exist. If FROM_MODULE is nonzero, we
5108 mangle the name so that it doesn't interfere with commons defined
5109 in the using namespace.
5110 TODO: Add to global symbol tree. */
5113 gfc_get_common (const char *name
, int from_module
)
5116 static int serial
= 0;
5117 char mangled_name
[GFC_MAX_SYMBOL_LEN
+ 1];
5121 /* A use associated common block is only needed to correctly layout
5122 the variables it contains. */
5123 snprintf (mangled_name
, GFC_MAX_SYMBOL_LEN
, "_%d_%s", serial
++, name
);
5124 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, mangled_name
);
5128 st
= gfc_find_symtree (gfc_current_ns
->common_root
, name
);
5131 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, name
);
5134 if (st
->n
.common
== NULL
)
5136 st
->n
.common
= gfc_get_common_head ();
5137 st
->n
.common
->where
= gfc_current_locus
;
5138 strcpy (st
->n
.common
->name
, name
);
5141 return st
->n
.common
;
5145 /* Match a common block name. */
5148 gfc_match_common_name (char *name
)
5152 if (gfc_match_char ('/') == MATCH_NO
)
5158 if (gfc_match_char ('/') == MATCH_YES
)
5164 m
= gfc_match_name (name
);
5166 if (m
== MATCH_ERROR
)
5168 if (m
== MATCH_YES
&& gfc_match_char ('/') == MATCH_YES
)
5171 gfc_error ("Syntax error in common block name at %C");
5176 /* Match a COMMON statement. */
5179 gfc_match_common (void)
5181 gfc_symbol
*sym
, **head
, *tail
, *other
;
5182 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5189 /* COMMON has been matched. In free form source code, the next character
5190 needs to be whitespace or '/'. Check that here. Fixed form source
5191 code needs to be checked below. */
5192 c
= gfc_peek_ascii_char ();
5193 if (gfc_current_form
== FORM_FREE
&& !gfc_is_whitespace (c
) && c
!= '/')
5200 m
= gfc_match_common_name (name
);
5201 if (m
== MATCH_ERROR
)
5204 if (name
[0] == '\0')
5206 t
= &gfc_current_ns
->blank_common
;
5207 if (t
->head
== NULL
)
5208 t
->where
= gfc_current_locus
;
5212 t
= gfc_get_common (name
, 0);
5221 while (tail
->common_next
)
5222 tail
= tail
->common_next
;
5225 /* Grab the list of symbols. */
5228 m
= gfc_match_symbol (&sym
, 0);
5229 if (m
== MATCH_ERROR
)
5234 /* See if we know the current common block is bind(c), and if
5235 so, then see if we can check if the symbol is (which it'll
5236 need to be). This can happen if the bind(c) attr stmt was
5237 applied to the common block, and the variable(s) already
5238 defined, before declaring the common block. */
5239 if (t
->is_bind_c
== 1)
5241 if (sym
->ts
.type
!= BT_UNKNOWN
&& sym
->ts
.is_c_interop
!= 1)
5243 /* If we find an error, just print it and continue,
5244 cause it's just semantic, and we can see if there
5246 gfc_error_now ("Variable %qs at %L in common block %qs "
5247 "at %C must be declared with a C "
5248 "interoperable kind since common block "
5250 sym
->name
, &(sym
->declared_at
), t
->name
,
5254 if (sym
->attr
.is_bind_c
== 1)
5255 gfc_error_now ("Variable %qs in common block %qs at %C cannot "
5256 "be bind(c) since it is not global", sym
->name
,
5260 if (sym
->attr
.in_common
)
5262 gfc_error ("Symbol %qs at %C is already in a COMMON block",
5267 if (((sym
->value
!= NULL
&& sym
->value
->expr_type
!= EXPR_NULL
)
5268 || sym
->attr
.data
) && gfc_current_state () != COMP_BLOCK_DATA
)
5270 if (!gfc_notify_std (GFC_STD_GNU
, "Initialized symbol %qs at "
5271 "%C can only be COMMON in BLOCK DATA",
5276 /* Deal with an optional array specification after the
5278 m
= gfc_match_array_spec (&as
, true, true);
5279 if (m
== MATCH_ERROR
)
5284 if (as
->type
!= AS_EXPLICIT
)
5286 gfc_error ("Array specification for symbol %qs in COMMON "
5287 "at %C must be explicit", sym
->name
);
5293 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5294 "coarray", sym
->name
);
5298 if (!gfc_add_dimension (&sym
->attr
, sym
->name
, NULL
))
5301 if (sym
->attr
.pointer
)
5303 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5304 "POINTER array", sym
->name
);
5313 /* Add the in_common attribute, but ignore the reported errors
5314 if any, and continue matching. */
5315 gfc_add_in_common (&sym
->attr
, sym
->name
, NULL
);
5317 sym
->common_block
= t
;
5318 sym
->common_block
->refs
++;
5321 tail
->common_next
= sym
;
5327 sym
->common_head
= t
;
5329 /* Check to see if the symbol is already in an equivalence group.
5330 If it is, set the other members as being in common. */
5331 if (sym
->attr
.in_equivalence
)
5333 for (e1
= gfc_current_ns
->equiv
; e1
; e1
= e1
->next
)
5335 for (e2
= e1
; e2
; e2
= e2
->eq
)
5336 if (e2
->expr
->symtree
->n
.sym
== sym
)
5343 for (e2
= e1
; e2
; e2
= e2
->eq
)
5345 other
= e2
->expr
->symtree
->n
.sym
;
5346 if (other
->common_head
5347 && other
->common_head
!= sym
->common_head
)
5349 gfc_error ("Symbol %qs, in COMMON block %qs at "
5350 "%C is being indirectly equivalenced to "
5351 "another COMMON block %qs",
5352 sym
->name
, sym
->common_head
->name
,
5353 other
->common_head
->name
);
5356 other
->attr
.in_common
= 1;
5357 other
->common_head
= t
;
5363 gfc_gobble_whitespace ();
5364 if (gfc_match_eos () == MATCH_YES
)
5366 c
= gfc_peek_ascii_char ();
5371 /* In Fixed form source code, gfortran can end up here for an
5372 expression of the form COMMONI = RHS. This may not be an
5373 error, so return MATCH_NO. */
5374 if (gfc_current_form
== FORM_FIXED
&& c
== '=')
5376 gfc_free_array_spec (as
);
5382 gfc_match_char (',');
5384 gfc_gobble_whitespace ();
5385 if (gfc_peek_ascii_char () == '/')
5394 gfc_syntax_error (ST_COMMON
);
5397 gfc_free_array_spec (as
);
5402 /* Match a BLOCK DATA program unit. */
5405 gfc_match_block_data (void)
5407 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5411 if (!gfc_notify_std (GFC_STD_F2018_OBS
, "BLOCK DATA construct at %L",
5412 &gfc_current_locus
))
5415 if (gfc_match_eos () == MATCH_YES
)
5417 gfc_new_block
= NULL
;
5421 m
= gfc_match ("% %n%t", name
);
5425 if (gfc_get_symbol (name
, NULL
, &sym
))
5428 if (!gfc_add_flavor (&sym
->attr
, FL_BLOCK_DATA
, sym
->name
, NULL
))
5431 gfc_new_block
= sym
;
5437 /* Free a namelist structure. */
5440 gfc_free_namelist (gfc_namelist
*name
)
5444 for (; name
; name
= n
)
5452 /* Free an OpenMP namelist structure. */
5455 gfc_free_omp_namelist (gfc_omp_namelist
*name
, bool free_ns
)
5457 gfc_omp_namelist
*n
;
5459 for (; name
; name
= n
)
5461 gfc_free_expr (name
->expr
);
5463 gfc_free_namespace (name
->u2
.ns
);
5464 else if (name
->u2
.udr
)
5466 if (name
->u2
.udr
->combiner
)
5467 gfc_free_statement (name
->u2
.udr
->combiner
);
5468 if (name
->u2
.udr
->initializer
)
5469 gfc_free_statement (name
->u2
.udr
->initializer
);
5470 free (name
->u2
.udr
);
5478 /* Match a NAMELIST statement. */
5481 gfc_match_namelist (void)
5483 gfc_symbol
*group_name
, *sym
;
5487 m
= gfc_match (" / %s /", &group_name
);
5490 if (m
== MATCH_ERROR
)
5495 if (group_name
->ts
.type
!= BT_UNKNOWN
)
5497 gfc_error ("Namelist group name %qs at %C already has a basic "
5498 "type of %s", group_name
->name
,
5499 gfc_typename (&group_name
->ts
));
5503 if (group_name
->attr
.flavor
== FL_NAMELIST
5504 && group_name
->attr
.use_assoc
5505 && !gfc_notify_std (GFC_STD_GNU
, "Namelist group name %qs "
5506 "at %C already is USE associated and can"
5507 "not be respecified.", group_name
->name
))
5510 if (group_name
->attr
.flavor
!= FL_NAMELIST
5511 && !gfc_add_flavor (&group_name
->attr
, FL_NAMELIST
,
5512 group_name
->name
, NULL
))
5517 m
= gfc_match_symbol (&sym
, 1);
5520 if (m
== MATCH_ERROR
)
5523 if (sym
->ts
.type
== BT_UNKNOWN
)
5525 if (gfc_current_ns
->seen_implicit_none
)
5527 /* It is required that members of a namelist be declared
5528 before the namelist. We check this by checking if the
5529 symbol has a defined type for IMPLICIT NONE. */
5530 gfc_error ("Symbol %qs in namelist %qs at %C must be "
5531 "declared before the namelist is declared.",
5532 sym
->name
, group_name
->name
);
5536 /* If the type is not set already, we set it here to the
5537 implicit default type. It is not allowed to set it
5538 later to any other type. */
5539 gfc_set_default_type (sym
, 0, gfc_current_ns
);
5541 if (sym
->attr
.in_namelist
== 0
5542 && !gfc_add_in_namelist (&sym
->attr
, sym
->name
, NULL
))
5545 /* Use gfc_error_check here, rather than goto error, so that
5546 these are the only errors for the next two lines. */
5547 if (sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
)
5549 gfc_error ("Assumed size array %qs in namelist %qs at "
5550 "%C is not allowed", sym
->name
, group_name
->name
);
5554 nl
= gfc_get_namelist ();
5558 if (group_name
->namelist
== NULL
)
5559 group_name
->namelist
= group_name
->namelist_tail
= nl
;
5562 group_name
->namelist_tail
->next
= nl
;
5563 group_name
->namelist_tail
= nl
;
5566 if (gfc_match_eos () == MATCH_YES
)
5569 m
= gfc_match_char (',');
5571 if (gfc_match_char ('/') == MATCH_YES
)
5573 m2
= gfc_match (" %s /", &group_name
);
5574 if (m2
== MATCH_YES
)
5576 if (m2
== MATCH_ERROR
)
5590 gfc_syntax_error (ST_NAMELIST
);
5597 /* Match a MODULE statement. */
5600 gfc_match_module (void)
5604 m
= gfc_match (" %s%t", &gfc_new_block
);
5608 if (!gfc_add_flavor (&gfc_new_block
->attr
, FL_MODULE
,
5609 gfc_new_block
->name
, NULL
))
5616 /* Free equivalence sets and lists. Recursively is the easiest way to
5620 gfc_free_equiv_until (gfc_equiv
*eq
, gfc_equiv
*stop
)
5625 gfc_free_equiv (eq
->eq
);
5626 gfc_free_equiv_until (eq
->next
, stop
);
5627 gfc_free_expr (eq
->expr
);
5633 gfc_free_equiv (gfc_equiv
*eq
)
5635 gfc_free_equiv_until (eq
, NULL
);
5639 /* Match an EQUIVALENCE statement. */
5642 gfc_match_equivalence (void)
5644 gfc_equiv
*eq
, *set
, *tail
;
5648 gfc_common_head
*common_head
= NULL
;
5653 /* EQUIVALENCE has been matched. After gobbling any possible whitespace,
5654 the next character needs to be '('. Check that here, and return
5655 MATCH_NO for a variable of the form equivalencej. */
5656 gfc_gobble_whitespace ();
5657 c
= gfc_peek_ascii_char ();
5665 eq
= gfc_get_equiv ();
5669 eq
->next
= gfc_current_ns
->equiv
;
5670 gfc_current_ns
->equiv
= eq
;
5672 if (gfc_match_char ('(') != MATCH_YES
)
5676 common_flag
= FALSE
;
5681 m
= gfc_match_equiv_variable (&set
->expr
);
5682 if (m
== MATCH_ERROR
)
5687 /* count the number of objects. */
5690 if (gfc_match_char ('%') == MATCH_YES
)
5692 gfc_error ("Derived type component %C is not a "
5693 "permitted EQUIVALENCE member");
5697 for (ref
= set
->expr
->ref
; ref
; ref
= ref
->next
)
5698 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
== AR_SECTION
)
5700 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5701 "be an array section");
5705 sym
= set
->expr
->symtree
->n
.sym
;
5707 if (!gfc_add_in_equivalence (&sym
->attr
, sym
->name
, NULL
))
5709 if (sym
->ts
.type
== BT_CLASS
5711 && !gfc_add_in_equivalence (&CLASS_DATA (sym
)->attr
,
5715 if (sym
->attr
.in_common
)
5718 common_head
= sym
->common_head
;
5721 if (gfc_match_char (')') == MATCH_YES
)
5724 if (gfc_match_char (',') != MATCH_YES
)
5727 set
->eq
= gfc_get_equiv ();
5733 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5737 /* If one of the members of an equivalence is in common, then
5738 mark them all as being in common. Before doing this, check
5739 that members of the equivalence group are not in different
5742 for (set
= eq
; set
; set
= set
->eq
)
5744 sym
= set
->expr
->symtree
->n
.sym
;
5745 if (sym
->common_head
&& sym
->common_head
!= common_head
)
5747 gfc_error ("Attempt to indirectly overlap COMMON "
5748 "blocks %s and %s by EQUIVALENCE at %C",
5749 sym
->common_head
->name
, common_head
->name
);
5752 sym
->attr
.in_common
= 1;
5753 sym
->common_head
= common_head
;
5756 if (gfc_match_eos () == MATCH_YES
)
5758 if (gfc_match_char (',') != MATCH_YES
)
5760 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5765 if (!gfc_notify_std (GFC_STD_F2018_OBS
, "EQUIVALENCE statement at %C"))
5771 gfc_syntax_error (ST_EQUIVALENCE
);
5777 gfc_free_equiv (gfc_current_ns
->equiv
);
5778 gfc_current_ns
->equiv
= eq
;
5784 /* Check that a statement function is not recursive. This is done by looking
5785 for the statement function symbol(sym) by looking recursively through its
5786 expression(e). If a reference to sym is found, true is returned.
5787 12.5.4 requires that any variable of function that is implicitly typed
5788 shall have that type confirmed by any subsequent type declaration. The
5789 implicit typing is conveniently done here. */
5791 recursive_stmt_fcn (gfc_expr
*, gfc_symbol
*);
5794 check_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
5800 switch (e
->expr_type
)
5803 if (e
->symtree
== NULL
)
5806 /* Check the name before testing for nested recursion! */
5807 if (sym
->name
== e
->symtree
->n
.sym
->name
)
5810 /* Catch recursion via other statement functions. */
5811 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
5812 && e
->symtree
->n
.sym
->value
5813 && recursive_stmt_fcn (e
->symtree
->n
.sym
->value
, sym
))
5816 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
5817 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
5822 if (e
->symtree
&& sym
->name
== e
->symtree
->n
.sym
->name
)
5825 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
5826 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
5838 recursive_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
)
5840 return gfc_traverse_expr (e
, sym
, check_stmt_fcn
, 0);
5844 /* Match a statement function declaration. It is so easy to match
5845 non-statement function statements with a MATCH_ERROR as opposed to
5846 MATCH_NO that we suppress error message in most cases. */
5849 gfc_match_st_function (void)
5851 gfc_error_buffer old_error
;
5855 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5858 gfc_formal_arglist
*ptr
;
5860 /* Read the possible statement function name, and then check to see if
5861 a symbol is already present in the namespace. Record if it is a
5862 function and whether it has been referenced. */
5865 old_locus
= gfc_current_locus
;
5866 m
= gfc_match_name (name
);
5869 gfc_find_symbol (name
, NULL
, 1, &sym
);
5870 if (sym
&& sym
->attr
.function
&& !sym
->attr
.referenced
)
5877 gfc_current_locus
= old_locus
;
5878 m
= gfc_match_symbol (&sym
, 0);
5882 gfc_push_error (&old_error
);
5884 if (!gfc_add_procedure (&sym
->attr
, PROC_ST_FUNCTION
, sym
->name
, NULL
))
5887 if (gfc_match_formal_arglist (sym
, 1, 0) != MATCH_YES
)
5890 m
= gfc_match (" = %e%t", &expr
);
5894 gfc_free_error (&old_error
);
5896 if (m
== MATCH_ERROR
)
5899 if (recursive_stmt_fcn (expr
, sym
))
5901 gfc_error ("Statement function at %L is recursive", &expr
->where
);
5905 if (fcn
&& ptr
!= sym
->formal
)
5907 gfc_error ("Statement function %qs at %L conflicts with function name",
5908 sym
->name
, &expr
->where
);
5914 if ((gfc_current_state () == COMP_FUNCTION
5915 || gfc_current_state () == COMP_SUBROUTINE
)
5916 && gfc_state_stack
->previous
->state
== COMP_INTERFACE
)
5918 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
5923 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Statement function at %C"))
5929 gfc_pop_error (&old_error
);
5934 /* Match an assignment to a pointer function (F2008). This could, in
5935 general be ambiguous with a statement function. In this implementation
5936 it remains so if it is the first statement after the specification
5940 gfc_match_ptr_fcn_assign (void)
5942 gfc_error_buffer old_error
;
5947 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5949 old_loc
= gfc_current_locus
;
5950 m
= gfc_match_name (name
);
5954 gfc_find_symbol (name
, NULL
, 1, &sym
);
5955 if (sym
&& sym
->attr
.flavor
!= FL_PROCEDURE
)
5958 gfc_push_error (&old_error
);
5960 if (sym
&& sym
->attr
.function
)
5961 goto match_actual_arglist
;
5963 gfc_current_locus
= old_loc
;
5964 m
= gfc_match_symbol (&sym
, 0);
5968 if (!gfc_add_procedure (&sym
->attr
, PROC_UNKNOWN
, sym
->name
, NULL
))
5971 match_actual_arglist
:
5972 gfc_current_locus
= old_loc
;
5973 m
= gfc_match (" %e", &expr
);
5977 new_st
.op
= EXEC_ASSIGN
;
5978 new_st
.expr1
= expr
;
5981 m
= gfc_match (" = %e%t", &expr
);
5985 new_st
.expr2
= expr
;
5989 gfc_pop_error (&old_error
);
5994 /***************** SELECT CASE subroutines ******************/
5996 /* Free a single case structure. */
5999 free_case (gfc_case
*p
)
6001 if (p
->low
== p
->high
)
6003 gfc_free_expr (p
->low
);
6004 gfc_free_expr (p
->high
);
6009 /* Free a list of case structures. */
6012 gfc_free_case_list (gfc_case
*p
)
6024 /* Match a single case selector. Combining the requirements of F08:C830
6025 and F08:C832 (R838) means that the case-value must have either CHARACTER,
6026 INTEGER, or LOGICAL type. */
6029 match_case_selector (gfc_case
**cp
)
6034 c
= gfc_get_case ();
6035 c
->where
= gfc_current_locus
;
6037 if (gfc_match_char (':') == MATCH_YES
)
6039 m
= gfc_match_init_expr (&c
->high
);
6042 if (m
== MATCH_ERROR
)
6045 if (c
->high
->ts
.type
!= BT_LOGICAL
&& c
->high
->ts
.type
!= BT_INTEGER
6046 && c
->high
->ts
.type
!= BT_CHARACTER
)
6048 gfc_error ("Expression in CASE selector at %L cannot be %s",
6049 &c
->high
->where
, gfc_typename (&c
->high
->ts
));
6055 m
= gfc_match_init_expr (&c
->low
);
6056 if (m
== MATCH_ERROR
)
6061 if (c
->low
->ts
.type
!= BT_LOGICAL
&& c
->low
->ts
.type
!= BT_INTEGER
6062 && c
->low
->ts
.type
!= BT_CHARACTER
)
6064 gfc_error ("Expression in CASE selector at %L cannot be %s",
6065 &c
->low
->where
, gfc_typename (&c
->low
->ts
));
6069 /* If we're not looking at a ':' now, make a range out of a single
6070 target. Else get the upper bound for the case range. */
6071 if (gfc_match_char (':') != MATCH_YES
)
6075 m
= gfc_match_init_expr (&c
->high
);
6076 if (m
== MATCH_ERROR
)
6078 /* MATCH_NO is fine. It's OK if nothing is there! */
6086 gfc_error ("Expected initialization expression in CASE at %C");
6094 /* Match the end of a case statement. */
6097 match_case_eos (void)
6099 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6102 if (gfc_match_eos () == MATCH_YES
)
6105 /* If the case construct doesn't have a case-construct-name, we
6106 should have matched the EOS. */
6107 if (!gfc_current_block ())
6110 gfc_gobble_whitespace ();
6112 m
= gfc_match_name (name
);
6116 if (strcmp (name
, gfc_current_block ()->name
) != 0)
6118 gfc_error ("Expected block name %qs of SELECT construct at %C",
6119 gfc_current_block ()->name
);
6123 return gfc_match_eos ();
6127 /* Match a SELECT statement. */
6130 gfc_match_select (void)
6135 m
= gfc_match_label ();
6136 if (m
== MATCH_ERROR
)
6139 m
= gfc_match (" select case ( %e )%t", &expr
);
6143 new_st
.op
= EXEC_SELECT
;
6144 new_st
.expr1
= expr
;
6150 /* Transfer the selector typespec to the associate name. */
6153 copy_ts_from_selector_to_associate (gfc_expr
*associate
, gfc_expr
*selector
)
6156 gfc_symbol
*assoc_sym
;
6159 assoc_sym
= associate
->symtree
->n
.sym
;
6161 /* At this stage the expression rank and arrayspec dimensions have
6162 not been completely sorted out. We must get the expr2->rank
6163 right here, so that the correct class container is obtained. */
6164 ref
= selector
->ref
;
6165 while (ref
&& ref
->next
)
6168 if (selector
->ts
.type
== BT_CLASS
6169 && CLASS_DATA (selector
)
6170 && CLASS_DATA (selector
)->as
6171 && CLASS_DATA (selector
)->as
->type
== AS_ASSUMED_RANK
)
6173 assoc_sym
->attr
.dimension
= 1;
6174 assoc_sym
->as
= gfc_copy_array_spec (CLASS_DATA (selector
)->as
);
6175 goto build_class_sym
;
6177 else if (selector
->ts
.type
== BT_CLASS
6178 && CLASS_DATA (selector
)
6179 && CLASS_DATA (selector
)->as
6180 && ref
&& ref
->type
== REF_ARRAY
)
6182 /* Ensure that the array reference type is set. We cannot use
6183 gfc_resolve_expr at this point, so the usable parts of
6184 resolve.c(resolve_array_ref) are employed to do it. */
6185 if (ref
->u
.ar
.type
== AR_UNKNOWN
)
6187 ref
->u
.ar
.type
= AR_ELEMENT
;
6188 for (int i
= 0; i
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; i
++)
6189 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_RANGE
6190 || ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
6191 || (ref
->u
.ar
.dimen_type
[i
] == DIMEN_UNKNOWN
6192 && ref
->u
.ar
.start
[i
] && ref
->u
.ar
.start
[i
]->rank
))
6194 ref
->u
.ar
.type
= AR_SECTION
;
6199 if (ref
->u
.ar
.type
== AR_FULL
)
6200 selector
->rank
= CLASS_DATA (selector
)->as
->rank
;
6201 else if (ref
->u
.ar
.type
== AR_SECTION
)
6202 selector
->rank
= ref
->u
.ar
.dimen
;
6206 rank
= selector
->rank
;
6211 for (int i
= 0; i
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; i
++)
6212 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_ELEMENT
6213 || (ref
->u
.ar
.dimen_type
[i
] == DIMEN_UNKNOWN
6214 && ref
->u
.ar
.end
[i
] == NULL
6215 && ref
->u
.ar
.stride
[i
] == NULL
))
6220 assoc_sym
->attr
.dimension
= 1;
6221 assoc_sym
->as
= gfc_get_array_spec ();
6222 assoc_sym
->as
->rank
= rank
;
6223 assoc_sym
->as
->type
= AS_DEFERRED
;
6226 assoc_sym
->as
= NULL
;
6229 assoc_sym
->as
= NULL
;
6232 if (selector
->ts
.type
== BT_CLASS
)
6234 /* The correct class container has to be available. */
6235 assoc_sym
->ts
.type
= BT_CLASS
;
6236 assoc_sym
->ts
.u
.derived
= CLASS_DATA (selector
)
6237 ? CLASS_DATA (selector
)->ts
.u
.derived
: selector
->ts
.u
.derived
;
6238 assoc_sym
->attr
.pointer
= 1;
6239 gfc_build_class_symbol (&assoc_sym
->ts
, &assoc_sym
->attr
, &assoc_sym
->as
);
6244 /* Push the current selector onto the SELECT TYPE stack. */
6247 select_type_push (gfc_symbol
*sel
)
6249 gfc_select_type_stack
*top
= gfc_get_select_type_stack ();
6250 top
->selector
= sel
;
6252 top
->prev
= select_type_stack
;
6254 select_type_stack
= top
;
6258 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
6260 static gfc_symtree
*
6261 select_intrinsic_set_tmp (gfc_typespec
*ts
)
6263 char name
[GFC_MAX_SYMBOL_LEN
];
6265 HOST_WIDE_INT charlen
= 0;
6266 gfc_symbol
*selector
= select_type_stack
->selector
;
6269 if (ts
->type
== BT_CLASS
|| ts
->type
== BT_DERIVED
)
6272 if (selector
->ts
.type
== BT_CLASS
&& !selector
->attr
.class_ok
)
6275 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6276 the values correspond to SELECT rank cases. */
6277 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
6278 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
6279 charlen
= gfc_mpz_get_hwi (ts
->u
.cl
->length
->value
.integer
);
6281 if (ts
->type
!= BT_CHARACTER
)
6282 sprintf (name
, "__tmp_%s_%d", gfc_basic_typename (ts
->type
),
6285 snprintf (name
, sizeof (name
),
6286 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC
"_%d",
6287 gfc_basic_typename (ts
->type
), charlen
, ts
->kind
);
6289 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
6291 gfc_add_type (sym
, ts
, NULL
);
6293 /* Copy across the array spec to the selector. */
6294 if (selector
->ts
.type
== BT_CLASS
6295 && (CLASS_DATA (selector
)->attr
.dimension
6296 || CLASS_DATA (selector
)->attr
.codimension
))
6298 sym
->attr
.pointer
= 1;
6299 sym
->attr
.dimension
= CLASS_DATA (selector
)->attr
.dimension
;
6300 sym
->attr
.codimension
= CLASS_DATA (selector
)->attr
.codimension
;
6301 sym
->as
= gfc_copy_array_spec (CLASS_DATA (selector
)->as
);
6304 gfc_set_sym_referenced (sym
);
6305 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, name
, NULL
);
6306 sym
->attr
.select_type_temporary
= 1;
6312 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
6315 select_type_set_tmp (gfc_typespec
*ts
)
6317 char name
[GFC_MAX_SYMBOL_LEN
+ 12 + 1];
6318 gfc_symtree
*tmp
= NULL
;
6319 gfc_symbol
*selector
= select_type_stack
->selector
;
6324 select_type_stack
->tmp
= NULL
;
6328 tmp
= select_intrinsic_set_tmp (ts
);
6335 if (ts
->type
== BT_CLASS
)
6336 sprintf (name
, "__tmp_class_%s", ts
->u
.derived
->name
);
6338 sprintf (name
, "__tmp_type_%s", ts
->u
.derived
->name
);
6340 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
6342 gfc_add_type (sym
, ts
, NULL
);
6344 if (selector
->ts
.type
== BT_CLASS
&& selector
->attr
.class_ok
)
6347 = CLASS_DATA (selector
)->attr
.class_pointer
;
6349 /* Copy across the array spec to the selector. */
6350 if (CLASS_DATA (selector
)->attr
.dimension
6351 || CLASS_DATA (selector
)->attr
.codimension
)
6354 = CLASS_DATA (selector
)->attr
.dimension
;
6355 sym
->attr
.codimension
6356 = CLASS_DATA (selector
)->attr
.codimension
;
6357 if (CLASS_DATA (selector
)->as
->type
!= AS_EXPLICIT
)
6358 sym
->as
= gfc_copy_array_spec (CLASS_DATA (selector
)->as
);
6361 sym
->as
= gfc_get_array_spec();
6362 sym
->as
->rank
= CLASS_DATA (selector
)->as
->rank
;
6363 sym
->as
->type
= AS_DEFERRED
;
6368 gfc_set_sym_referenced (sym
);
6369 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, name
, NULL
);
6370 sym
->attr
.select_type_temporary
= 1;
6372 if (ts
->type
== BT_CLASS
)
6373 gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
6379 /* Add an association for it, so the rest of the parser knows it is
6380 an associate-name. The target will be set during resolution. */
6381 sym
->assoc
= gfc_get_association_list ();
6382 sym
->assoc
->dangling
= 1;
6383 sym
->assoc
->st
= tmp
;
6385 select_type_stack
->tmp
= tmp
;
6389 /* Match a SELECT TYPE statement. */
6392 gfc_match_select_type (void)
6394 gfc_expr
*expr1
, *expr2
= NULL
;
6396 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6399 gfc_namespace
*ns
= gfc_current_ns
;
6401 m
= gfc_match_label ();
6402 if (m
== MATCH_ERROR
)
6405 m
= gfc_match (" select type ( ");
6409 if (gfc_current_state() == COMP_MODULE
6410 || gfc_current_state() == COMP_SUBMODULE
)
6412 gfc_error ("SELECT TYPE at %C cannot appear in this scope");
6416 gfc_current_ns
= gfc_build_block_ns (ns
);
6417 m
= gfc_match (" %n => %e", name
, &expr2
);
6420 expr1
= gfc_get_expr ();
6421 expr1
->expr_type
= EXPR_VARIABLE
;
6422 expr1
->where
= expr2
->where
;
6423 if (gfc_get_sym_tree (name
, NULL
, &expr1
->symtree
, false))
6429 sym
= expr1
->symtree
->n
.sym
;
6430 if (expr2
->ts
.type
== BT_UNKNOWN
)
6431 sym
->attr
.untyped
= 1;
6433 copy_ts_from_selector_to_associate (expr1
, expr2
);
6435 sym
->attr
.flavor
= FL_VARIABLE
;
6436 sym
->attr
.referenced
= 1;
6437 sym
->attr
.class_ok
= 1;
6441 m
= gfc_match (" %e ", &expr1
);
6444 std::swap (ns
, gfc_current_ns
);
6445 gfc_free_namespace (ns
);
6450 m
= gfc_match (" )%t");
6453 gfc_error ("parse error in SELECT TYPE statement at %C");
6457 /* This ghastly expression seems to be needed to distinguish a CLASS
6458 array, which can have a reference, from other expressions that
6459 have references, such as derived type components, and are not
6460 allowed by the standard.
6461 TODO: see if it is sufficient to exclude component and substring
6463 class_array
= (expr1
->expr_type
== EXPR_VARIABLE
6464 && expr1
->ts
.type
== BT_CLASS
6465 && CLASS_DATA (expr1
)
6466 && (strcmp (CLASS_DATA (expr1
)->name
, "_data") == 0)
6467 && (CLASS_DATA (expr1
)->attr
.dimension
6468 || CLASS_DATA (expr1
)->attr
.codimension
)
6470 && expr1
->ref
->type
== REF_ARRAY
6471 && expr1
->ref
->u
.ar
.type
== AR_FULL
6472 && expr1
->ref
->next
== NULL
);
6474 /* Check for F03:C811 (F08:C835). */
6475 if (!expr2
&& (expr1
->expr_type
!= EXPR_VARIABLE
6476 || (!class_array
&& expr1
->ref
!= NULL
)))
6478 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
6479 "use associate-name=>");
6484 new_st
.op
= EXEC_SELECT_TYPE
;
6485 new_st
.expr1
= expr1
;
6486 new_st
.expr2
= expr2
;
6487 new_st
.ext
.block
.ns
= gfc_current_ns
;
6489 select_type_push (expr1
->symtree
->n
.sym
);
6490 gfc_current_ns
= ns
;
6495 gfc_free_expr (expr1
);
6496 gfc_free_expr (expr2
);
6497 gfc_undo_symbols ();
6498 std::swap (ns
, gfc_current_ns
);
6499 gfc_free_namespace (ns
);
6504 /* Set the temporary for the current intrinsic SELECT RANK selector. */
6507 select_rank_set_tmp (gfc_typespec
*ts
, int *case_value
)
6509 char name
[2 * GFC_MAX_SYMBOL_LEN
];
6510 char tname
[GFC_MAX_SYMBOL_LEN
+ 7];
6512 gfc_symbol
*selector
= select_type_stack
->selector
;
6515 HOST_WIDE_INT charlen
= 0;
6517 if (case_value
== NULL
)
6520 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
6521 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
6522 charlen
= gfc_mpz_get_hwi (ts
->u
.cl
->length
->value
.integer
);
6524 if (ts
->type
== BT_CLASS
)
6525 sprintf (tname
, "class_%s", ts
->u
.derived
->name
);
6526 else if (ts
->type
== BT_DERIVED
)
6527 sprintf (tname
, "type_%s", ts
->u
.derived
->name
);
6528 else if (ts
->type
!= BT_CHARACTER
)
6529 sprintf (tname
, "%s_%d", gfc_basic_typename (ts
->type
), ts
->kind
);
6531 sprintf (tname
, "%s_" HOST_WIDE_INT_PRINT_DEC
"_%d",
6532 gfc_basic_typename (ts
->type
), charlen
, ts
->kind
);
6534 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6535 the values correspond to SELECT rank cases. */
6536 if (*case_value
>=0)
6537 sprintf (name
, "__tmp_%s_rank_%d", tname
, *case_value
);
6539 sprintf (name
, "__tmp_%s_rank_m%d", tname
, -*case_value
);
6541 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
6545 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
6547 gfc_add_type (sym
, ts
, NULL
);
6549 /* Copy across the array spec to the selector. */
6550 if (selector
->ts
.type
== BT_CLASS
)
6552 sym
->ts
.u
.derived
= CLASS_DATA (selector
)->ts
.u
.derived
;
6553 sym
->attr
.pointer
= CLASS_DATA (selector
)->attr
.pointer
;
6554 sym
->attr
.allocatable
= CLASS_DATA (selector
)->attr
.allocatable
;
6555 sym
->attr
.target
= CLASS_DATA (selector
)->attr
.target
;
6556 sym
->attr
.class_ok
= 0;
6557 if (case_value
&& *case_value
!= 0)
6559 sym
->attr
.dimension
= 1;
6560 sym
->as
= gfc_copy_array_spec (CLASS_DATA (selector
)->as
);
6561 if (*case_value
> 0)
6563 sym
->as
->type
= AS_DEFERRED
;
6564 sym
->as
->rank
= *case_value
;
6566 else if (*case_value
== -1)
6568 sym
->as
->type
= AS_ASSUMED_SIZE
;
6575 sym
->attr
.pointer
= selector
->attr
.pointer
;
6576 sym
->attr
.allocatable
= selector
->attr
.allocatable
;
6577 sym
->attr
.target
= selector
->attr
.target
;
6578 if (case_value
&& *case_value
!= 0)
6580 sym
->attr
.dimension
= 1;
6581 sym
->as
= gfc_copy_array_spec (selector
->as
);
6582 if (*case_value
> 0)
6584 sym
->as
->type
= AS_DEFERRED
;
6585 sym
->as
->rank
= *case_value
;
6587 else if (*case_value
== -1)
6589 sym
->as
->type
= AS_ASSUMED_SIZE
;
6595 gfc_set_sym_referenced (sym
);
6596 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, name
, NULL
);
6597 sym
->attr
.select_type_temporary
= 1;
6599 sym
->attr
.select_rank_temporary
= 1;
6601 if (ts
->type
== BT_CLASS
)
6602 gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
6604 /* Add an association for it, so the rest of the parser knows it is
6605 an associate-name. The target will be set during resolution. */
6606 sym
->assoc
= gfc_get_association_list ();
6607 sym
->assoc
->dangling
= 1;
6608 sym
->assoc
->st
= tmp
;
6610 select_type_stack
->tmp
= tmp
;
6614 /* Match a SELECT RANK statement. */
6617 gfc_match_select_rank (void)
6619 gfc_expr
*expr1
, *expr2
= NULL
;
6621 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6622 gfc_symbol
*sym
, *sym2
;
6623 gfc_namespace
*ns
= gfc_current_ns
;
6624 gfc_array_spec
*as
= NULL
;
6626 m
= gfc_match_label ();
6627 if (m
== MATCH_ERROR
)
6630 m
= gfc_match (" select rank ( ");
6634 if (!gfc_notify_std (GFC_STD_F2018
, "SELECT RANK statement at %C"))
6637 gfc_current_ns
= gfc_build_block_ns (ns
);
6638 m
= gfc_match (" %n => %e", name
, &expr2
);
6641 expr1
= gfc_get_expr ();
6642 expr1
->expr_type
= EXPR_VARIABLE
;
6643 expr1
->where
= expr2
->where
;
6644 expr1
->ref
= gfc_copy_ref (expr2
->ref
);
6645 if (gfc_get_sym_tree (name
, NULL
, &expr1
->symtree
, false))
6651 sym
= expr1
->symtree
->n
.sym
;
6655 sym2
= expr2
->symtree
->n
.sym
;
6656 as
= (sym2
->ts
.type
== BT_CLASS
6657 && CLASS_DATA (sym2
)) ? CLASS_DATA (sym2
)->as
: sym2
->as
;
6660 if (expr2
->expr_type
!= EXPR_VARIABLE
6661 || !(as
&& as
->type
== AS_ASSUMED_RANK
))
6663 gfc_error ("The SELECT RANK selector at %C must be an assumed "
6669 if (expr2
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym2
))
6671 copy_ts_from_selector_to_associate (expr1
, expr2
);
6673 sym
->attr
.flavor
= FL_VARIABLE
;
6674 sym
->attr
.referenced
= 1;
6675 sym
->attr
.class_ok
= 1;
6676 CLASS_DATA (sym
)->attr
.allocatable
= CLASS_DATA (sym2
)->attr
.allocatable
;
6677 CLASS_DATA (sym
)->attr
.pointer
= CLASS_DATA (sym2
)->attr
.pointer
;
6678 CLASS_DATA (sym
)->attr
.target
= CLASS_DATA (sym2
)->attr
.target
;
6679 sym
->attr
.pointer
= 1;
6684 sym
->as
= gfc_copy_array_spec (sym2
->as
);
6685 sym
->attr
.dimension
= 1;
6687 sym
->attr
.flavor
= FL_VARIABLE
;
6688 sym
->attr
.referenced
= 1;
6689 sym
->attr
.class_ok
= sym2
->attr
.class_ok
;
6690 sym
->attr
.allocatable
= sym2
->attr
.allocatable
;
6691 sym
->attr
.pointer
= sym2
->attr
.pointer
;
6692 sym
->attr
.target
= sym2
->attr
.target
;
6697 m
= gfc_match (" %e ", &expr1
);
6701 gfc_undo_symbols ();
6702 std::swap (ns
, gfc_current_ns
);
6703 gfc_free_namespace (ns
);
6709 sym
= expr1
->symtree
->n
.sym
;
6710 as
= (sym
->ts
.type
== BT_CLASS
6711 && CLASS_DATA (sym
)) ? CLASS_DATA (sym
)->as
: sym
->as
;
6714 if (expr1
->expr_type
!= EXPR_VARIABLE
6715 || !(as
&& as
->type
== AS_ASSUMED_RANK
))
6717 gfc_error("The SELECT RANK selector at %C must be an assumed "
6724 m
= gfc_match (" )%t");
6727 gfc_error ("parse error in SELECT RANK statement at %C");
6731 new_st
.op
= EXEC_SELECT_RANK
;
6732 new_st
.expr1
= expr1
;
6733 new_st
.expr2
= expr2
;
6734 new_st
.ext
.block
.ns
= gfc_current_ns
;
6736 select_type_push (expr1
->symtree
->n
.sym
);
6737 gfc_current_ns
= ns
;
6742 gfc_free_expr (expr1
);
6743 gfc_free_expr (expr2
);
6744 gfc_undo_symbols ();
6745 std::swap (ns
, gfc_current_ns
);
6746 gfc_free_namespace (ns
);
6751 /* Match a CASE statement. */
6754 gfc_match_case (void)
6756 gfc_case
*c
, *head
, *tail
;
6761 if (gfc_current_state () != COMP_SELECT
)
6763 gfc_error ("Unexpected CASE statement at %C");
6767 if (gfc_match ("% default") == MATCH_YES
)
6769 m
= match_case_eos ();
6772 if (m
== MATCH_ERROR
)
6775 new_st
.op
= EXEC_SELECT
;
6776 c
= gfc_get_case ();
6777 c
->where
= gfc_current_locus
;
6778 new_st
.ext
.block
.case_list
= c
;
6782 if (gfc_match_char ('(') != MATCH_YES
)
6787 if (match_case_selector (&c
) == MATCH_ERROR
)
6797 if (gfc_match_char (')') == MATCH_YES
)
6799 if (gfc_match_char (',') != MATCH_YES
)
6803 m
= match_case_eos ();
6806 if (m
== MATCH_ERROR
)
6809 new_st
.op
= EXEC_SELECT
;
6810 new_st
.ext
.block
.case_list
= head
;
6815 gfc_error ("Syntax error in CASE specification at %C");
6818 gfc_free_case_list (head
); /* new_st is cleaned up in parse.c. */
6823 /* Match a TYPE IS statement. */
6826 gfc_match_type_is (void)
6831 if (gfc_current_state () != COMP_SELECT_TYPE
)
6833 gfc_error ("Unexpected TYPE IS statement at %C");
6837 if (gfc_match_char ('(') != MATCH_YES
)
6840 c
= gfc_get_case ();
6841 c
->where
= gfc_current_locus
;
6843 m
= gfc_match_type_spec (&c
->ts
);
6846 if (m
== MATCH_ERROR
)
6849 if (gfc_match_char (')') != MATCH_YES
)
6852 m
= match_case_eos ();
6855 if (m
== MATCH_ERROR
)
6858 new_st
.op
= EXEC_SELECT_TYPE
;
6859 new_st
.ext
.block
.case_list
= c
;
6861 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
6862 && (c
->ts
.u
.derived
->attr
.sequence
6863 || c
->ts
.u
.derived
->attr
.is_bind_c
))
6865 gfc_error ("The type-spec shall not specify a sequence derived "
6866 "type or a type with the BIND attribute in SELECT "
6867 "TYPE at %C [F2003:C815]");
6871 if (c
->ts
.type
== BT_DERIVED
6872 && c
->ts
.u
.derived
&& c
->ts
.u
.derived
->attr
.pdt_type
6873 && gfc_spec_list_type (type_param_spec_list
, c
->ts
.u
.derived
)
6876 gfc_error ("All the LEN type parameters in the TYPE IS statement "
6877 "at %C must be ASSUMED");
6881 /* Create temporary variable. */
6882 select_type_set_tmp (&c
->ts
);
6887 gfc_error ("Syntax error in TYPE IS specification at %C");
6891 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
6896 /* Match a CLASS IS or CLASS DEFAULT statement. */
6899 gfc_match_class_is (void)
6904 if (gfc_current_state () != COMP_SELECT_TYPE
)
6907 if (gfc_match ("% default") == MATCH_YES
)
6909 m
= match_case_eos ();
6912 if (m
== MATCH_ERROR
)
6915 new_st
.op
= EXEC_SELECT_TYPE
;
6916 c
= gfc_get_case ();
6917 c
->where
= gfc_current_locus
;
6918 c
->ts
.type
= BT_UNKNOWN
;
6919 new_st
.ext
.block
.case_list
= c
;
6920 select_type_set_tmp (NULL
);
6924 m
= gfc_match ("% is");
6927 if (m
== MATCH_ERROR
)
6930 if (gfc_match_char ('(') != MATCH_YES
)
6933 c
= gfc_get_case ();
6934 c
->where
= gfc_current_locus
;
6936 m
= match_derived_type_spec (&c
->ts
);
6939 if (m
== MATCH_ERROR
)
6942 if (c
->ts
.type
== BT_DERIVED
)
6943 c
->ts
.type
= BT_CLASS
;
6945 if (gfc_match_char (')') != MATCH_YES
)
6948 m
= match_case_eos ();
6951 if (m
== MATCH_ERROR
)
6954 new_st
.op
= EXEC_SELECT_TYPE
;
6955 new_st
.ext
.block
.case_list
= c
;
6957 /* Create temporary variable. */
6958 select_type_set_tmp (&c
->ts
);
6963 gfc_error ("Syntax error in CLASS IS specification at %C");
6967 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
6972 /* Match a RANK statement. */
6975 gfc_match_rank_is (void)
6981 if (gfc_current_state () != COMP_SELECT_RANK
)
6983 gfc_error ("Unexpected RANK statement at %C");
6987 if (gfc_match ("% default") == MATCH_YES
)
6989 m
= match_case_eos ();
6992 if (m
== MATCH_ERROR
)
6995 new_st
.op
= EXEC_SELECT_RANK
;
6996 c
= gfc_get_case ();
6997 c
->ts
.type
= BT_UNKNOWN
;
6998 c
->where
= gfc_current_locus
;
6999 new_st
.ext
.block
.case_list
= c
;
7000 select_type_stack
->tmp
= NULL
;
7004 if (gfc_match_char ('(') != MATCH_YES
)
7007 c
= gfc_get_case ();
7008 c
->where
= gfc_current_locus
;
7009 c
->ts
= select_type_stack
->selector
->ts
;
7011 m
= gfc_match_expr (&c
->low
);
7014 if (gfc_match_char ('*') == MATCH_YES
)
7015 c
->low
= gfc_get_int_expr (gfc_default_integer_kind
,
7022 else if (m
== MATCH_YES
)
7025 if (c
->low
->expr_type
!= EXPR_CONSTANT
7026 || c
->low
->ts
.type
!= BT_INTEGER
7029 gfc_error ("The SELECT RANK CASE expression at %C must be a "
7030 "scalar, integer constant");
7034 case_value
= (int) mpz_get_si (c
->low
->value
.integer
);
7036 if ((case_value
< 0) || (case_value
> GFC_MAX_DIMENSIONS
))
7038 gfc_error ("The value of the SELECT RANK CASE expression at "
7039 "%C must not be less than zero or greater than %d",
7040 GFC_MAX_DIMENSIONS
);
7047 if (gfc_match_char (')') != MATCH_YES
)
7050 m
= match_case_eos ();
7053 if (m
== MATCH_ERROR
)
7056 new_st
.op
= EXEC_SELECT_RANK
;
7057 new_st
.ext
.block
.case_list
= c
;
7059 /* Create temporary variable. Recycle the select type code. */
7060 select_rank_set_tmp (&c
->ts
, &case_value
);
7065 gfc_error ("Syntax error in RANK specification at %C");
7069 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
7073 /********************* WHERE subroutines ********************/
7075 /* Match the rest of a simple WHERE statement that follows an IF statement.
7079 match_simple_where (void)
7085 m
= gfc_match (" ( %e )", &expr
);
7089 m
= gfc_match_assignment ();
7092 if (m
== MATCH_ERROR
)
7095 if (gfc_match_eos () != MATCH_YES
)
7098 c
= gfc_get_code (EXEC_WHERE
);
7101 c
->next
= XCNEW (gfc_code
);
7103 c
->next
->loc
= gfc_current_locus
;
7104 gfc_clear_new_st ();
7106 new_st
.op
= EXEC_WHERE
;
7112 gfc_syntax_error (ST_WHERE
);
7115 gfc_free_expr (expr
);
7120 /* Match a WHERE statement. */
7123 gfc_match_where (gfc_statement
*st
)
7129 m0
= gfc_match_label ();
7130 if (m0
== MATCH_ERROR
)
7133 m
= gfc_match (" where ( %e )", &expr
);
7137 if (gfc_match_eos () == MATCH_YES
)
7139 *st
= ST_WHERE_BLOCK
;
7140 new_st
.op
= EXEC_WHERE
;
7141 new_st
.expr1
= expr
;
7145 m
= gfc_match_assignment ();
7147 gfc_syntax_error (ST_WHERE
);
7151 gfc_free_expr (expr
);
7155 /* We've got a simple WHERE statement. */
7157 c
= gfc_get_code (EXEC_WHERE
);
7160 /* Put in the assignment. It will not be processed by add_statement, so we
7161 need to copy the location here. */
7163 c
->next
= XCNEW (gfc_code
);
7165 c
->next
->loc
= gfc_current_locus
;
7166 gfc_clear_new_st ();
7168 new_st
.op
= EXEC_WHERE
;
7175 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
7176 new_st if successful. */
7179 gfc_match_elsewhere (void)
7181 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7185 if (gfc_current_state () != COMP_WHERE
)
7187 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
7193 if (gfc_match_char ('(') == MATCH_YES
)
7195 m
= gfc_match_expr (&expr
);
7198 if (m
== MATCH_ERROR
)
7201 if (gfc_match_char (')') != MATCH_YES
)
7205 if (gfc_match_eos () != MATCH_YES
)
7207 /* Only makes sense if we have a where-construct-name. */
7208 if (!gfc_current_block ())
7213 /* Better be a name at this point. */
7214 m
= gfc_match_name (name
);
7217 if (m
== MATCH_ERROR
)
7220 if (gfc_match_eos () != MATCH_YES
)
7223 if (strcmp (name
, gfc_current_block ()->name
) != 0)
7225 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
7226 name
, gfc_current_block ()->name
);
7231 new_st
.op
= EXEC_WHERE
;
7232 new_st
.expr1
= expr
;
7236 gfc_syntax_error (ST_ELSEWHERE
);
7239 gfc_free_expr (expr
);