1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2024 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 "
196 "after %<.%> at %C");
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.
458 When gobble_ws is false, do not skip over leading blanks. */
461 gfc_match_small_literal_int (int *value
, int *cnt
, bool gobble_ws
)
467 old_loc
= gfc_current_locus
;
471 gfc_gobble_whitespace ();
472 c
= gfc_next_ascii_char ();
478 gfc_current_locus
= old_loc
;
487 old_loc
= gfc_current_locus
;
488 c
= gfc_next_ascii_char ();
493 i
= 10 * i
+ c
- '0';
498 gfc_error ("Integer too large at %C");
503 gfc_current_locus
= old_loc
;
512 /* Match a small, constant integer expression, like in a kind
513 statement. On MATCH_YES, 'value' is set. */
516 gfc_match_small_int (int *value
)
522 m
= gfc_match_expr (&expr
);
526 if (gfc_extract_int (expr
, &i
, 1))
528 gfc_free_expr (expr
);
535 /* Matches a statement label. Uses gfc_match_small_literal_int() to
536 do most of the work. */
539 gfc_match_st_label (gfc_st_label
**label
)
545 old_loc
= gfc_current_locus
;
547 m
= gfc_match_small_literal_int (&i
, &cnt
);
553 gfc_error ("Too many digits in statement label at %C");
559 gfc_error ("Statement label at %C is zero");
563 *label
= gfc_get_st_label (i
);
568 gfc_current_locus
= old_loc
;
573 /* Match and validate a label associated with a named IF, DO or SELECT
574 statement. If the symbol does not have the label attribute, we add
575 it. We also make sure the symbol does not refer to another
576 (active) block. A matched label is pointed to by gfc_new_block. */
579 gfc_match_label (void)
581 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
584 gfc_new_block
= NULL
;
586 m
= gfc_match (" %n :", name
);
590 if (gfc_get_symbol (name
, NULL
, &gfc_new_block
))
592 gfc_error ("Label name %qs at %C is ambiguous", name
);
596 if (gfc_new_block
->attr
.flavor
== FL_LABEL
)
598 gfc_error ("Duplicate construct label %qs at %C", name
);
602 if (!gfc_add_flavor (&gfc_new_block
->attr
, FL_LABEL
,
603 gfc_new_block
->name
, NULL
))
610 /* See if the current input looks like a name of some sort. Modifies
611 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
612 Note that options.cc restricts max_identifier_length to not more
613 than GFC_MAX_SYMBOL_LEN.
614 When gobble_ws is false, do not skip over leading blanks. */
617 gfc_match_name (char *buffer
, bool gobble_ws
)
623 old_loc
= gfc_current_locus
;
625 gfc_gobble_whitespace ();
627 c
= gfc_next_ascii_char ();
628 if (!(ISALPHA (c
) || (c
== '_' && flag_allow_leading_underscore
)))
630 /* Special cases for unary minus and plus, which allows for a sensible
631 error message for code of the form 'c = exp(-a*b) )' where an
632 extra ')' appears at the end of statement. */
633 if (!gfc_error_flag_test () && c
!= '(' && c
!= '-' && c
!= '+')
634 gfc_error ("Invalid character in name at %C");
635 gfc_current_locus
= old_loc
;
645 if (i
> gfc_option
.max_identifier_length
)
647 gfc_error ("Name at %C is too long");
651 old_loc
= gfc_current_locus
;
652 c
= gfc_next_ascii_char ();
654 while (ISALNUM (c
) || c
== '_' || (flag_dollar_ok
&& c
== '$'));
656 if (c
== '$' && !flag_dollar_ok
)
658 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
659 "allow it as an extension", &old_loc
);
664 gfc_current_locus
= old_loc
;
670 /* Match a symbol on the input. Modifies the pointer to the symbol
671 pointer if successful. */
674 gfc_match_sym_tree (gfc_symtree
**matched_symbol
, int host_assoc
)
676 char buffer
[GFC_MAX_SYMBOL_LEN
+ 1];
679 m
= gfc_match_name (buffer
);
684 return (gfc_get_ha_sym_tree (buffer
, matched_symbol
))
685 ? MATCH_ERROR
: MATCH_YES
;
687 if (gfc_get_sym_tree (buffer
, NULL
, matched_symbol
, false))
695 gfc_match_symbol (gfc_symbol
**matched_symbol
, int host_assoc
)
700 m
= gfc_match_sym_tree (&st
, host_assoc
);
705 *matched_symbol
= st
->n
.sym
;
707 *matched_symbol
= NULL
;
710 *matched_symbol
= NULL
;
715 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
716 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
720 gfc_match_intrinsic_op (gfc_intrinsic_op
*result
)
722 locus orig_loc
= gfc_current_locus
;
725 gfc_gobble_whitespace ();
726 ch
= gfc_next_ascii_char ();
731 *result
= INTRINSIC_PLUS
;
736 *result
= INTRINSIC_MINUS
;
740 if (gfc_next_ascii_char () == '=')
743 *result
= INTRINSIC_EQ
;
749 if (gfc_peek_ascii_char () == '=')
752 gfc_next_ascii_char ();
753 *result
= INTRINSIC_LE
;
757 *result
= INTRINSIC_LT
;
761 if (gfc_peek_ascii_char () == '=')
764 gfc_next_ascii_char ();
765 *result
= INTRINSIC_GE
;
769 *result
= INTRINSIC_GT
;
773 if (gfc_peek_ascii_char () == '*')
776 gfc_next_ascii_char ();
777 *result
= INTRINSIC_POWER
;
781 *result
= INTRINSIC_TIMES
;
785 ch
= gfc_peek_ascii_char ();
789 gfc_next_ascii_char ();
790 *result
= INTRINSIC_NE
;
796 gfc_next_ascii_char ();
797 *result
= INTRINSIC_CONCAT
;
801 *result
= INTRINSIC_DIVIDE
;
805 ch
= gfc_next_ascii_char ();
809 if (gfc_next_ascii_char () == 'n'
810 && gfc_next_ascii_char () == 'd'
811 && gfc_next_ascii_char () == '.')
813 /* Matched ".and.". */
814 *result
= INTRINSIC_AND
;
820 if (gfc_next_ascii_char () == 'q')
822 ch
= gfc_next_ascii_char ();
825 /* Matched ".eq.". */
826 *result
= INTRINSIC_EQ_OS
;
831 if (gfc_next_ascii_char () == '.')
833 /* Matched ".eqv.". */
834 *result
= INTRINSIC_EQV
;
842 ch
= gfc_next_ascii_char ();
845 if (gfc_next_ascii_char () == '.')
847 /* Matched ".ge.". */
848 *result
= INTRINSIC_GE_OS
;
854 if (gfc_next_ascii_char () == '.')
856 /* Matched ".gt.". */
857 *result
= INTRINSIC_GT_OS
;
864 ch
= gfc_next_ascii_char ();
867 if (gfc_next_ascii_char () == '.')
869 /* Matched ".le.". */
870 *result
= INTRINSIC_LE_OS
;
876 if (gfc_next_ascii_char () == '.')
878 /* Matched ".lt.". */
879 *result
= INTRINSIC_LT_OS
;
886 ch
= gfc_next_ascii_char ();
889 ch
= gfc_next_ascii_char ();
892 /* Matched ".ne.". */
893 *result
= INTRINSIC_NE_OS
;
898 if (gfc_next_ascii_char () == 'v'
899 && gfc_next_ascii_char () == '.')
901 /* Matched ".neqv.". */
902 *result
= INTRINSIC_NEQV
;
909 if (gfc_next_ascii_char () == 't'
910 && gfc_next_ascii_char () == '.')
912 /* Matched ".not.". */
913 *result
= INTRINSIC_NOT
;
920 if (gfc_next_ascii_char () == 'r'
921 && gfc_next_ascii_char () == '.')
923 /* Matched ".or.". */
924 *result
= INTRINSIC_OR
;
930 if (gfc_next_ascii_char () == 'o'
931 && gfc_next_ascii_char () == 'r'
932 && gfc_next_ascii_char () == '.')
934 if (!gfc_notify_std (GFC_STD_LEGACY
, ".XOR. operator at %C"))
936 /* Matched ".xor." - equivalent to ".neqv.". */
937 *result
= INTRINSIC_NEQV
;
951 gfc_current_locus
= orig_loc
;
956 /* Match a loop control phrase:
958 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
960 If the final integer expression is not present, a constant unity
961 expression is returned. We don't return MATCH_ERROR until after
962 the equals sign is seen. */
965 gfc_match_iterator (gfc_iterator
*iter
, int init_flag
)
967 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
968 gfc_expr
*var
, *e1
, *e2
, *e3
;
974 /* Match the start of an iterator without affecting the symbol table. */
976 start
= gfc_current_locus
;
977 m
= gfc_match (" %n =", name
);
978 gfc_current_locus
= start
;
983 m
= gfc_match_variable (&var
, 0);
987 if (var
->symtree
->n
.sym
->attr
.dimension
)
989 gfc_error ("Loop variable at %C cannot be an array");
993 /* F2008, C617 & C565. */
994 if (var
->symtree
->n
.sym
->attr
.codimension
)
996 gfc_error ("Loop variable at %C cannot be a coarray");
1000 if (var
->ref
!= NULL
)
1002 gfc_error ("Loop variable at %C cannot be a sub-component");
1006 gfc_match_char ('=');
1008 var
->symtree
->n
.sym
->attr
.implied_index
= 1;
1010 m
= init_flag
? gfc_match_init_expr (&e1
) : gfc_match_expr (&e1
);
1013 if (m
== MATCH_ERROR
)
1016 if (gfc_match_char (',') != MATCH_YES
)
1019 m
= init_flag
? gfc_match_init_expr (&e2
) : gfc_match_expr (&e2
);
1022 if (m
== MATCH_ERROR
)
1025 if (gfc_match_char (',') != MATCH_YES
)
1027 e3
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
1031 m
= init_flag
? gfc_match_init_expr (&e3
) : gfc_match_expr (&e3
);
1032 if (m
== MATCH_ERROR
)
1036 gfc_error ("Expected a step value in iterator at %C");
1048 gfc_error ("Syntax error in iterator at %C");
1059 /* Tries to match the next non-whitespace character on the input.
1060 This subroutine does not return MATCH_ERROR.
1061 When gobble_ws is false, do not skip over leading blanks. */
1064 gfc_match_char (char c
, bool gobble_ws
)
1068 where
= gfc_current_locus
;
1070 gfc_gobble_whitespace ();
1072 if (gfc_next_ascii_char () == c
)
1075 gfc_current_locus
= where
;
1080 /* General purpose matching subroutine. The target string is a
1081 scanf-like format string in which spaces correspond to arbitrary
1082 whitespace (including no whitespace), characters correspond to
1083 themselves. The %-codes are:
1085 %% Literal percent sign
1086 %e Expression, pointer to a pointer is set
1087 %s Symbol, pointer to the symbol is set (host_assoc = 0)
1088 %S Symbol, pointer to the symbol is set (host_assoc = 1)
1089 %n Name, character buffer is set to name
1090 %t Matches end of statement.
1091 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1092 %l Matches a statement label
1093 %v Matches a variable expression (an lvalue, except function references
1094 having a data pointer result)
1095 % Matches a required space (in free form) and optional spaces. */
1098 gfc_match (const char *target
, ...)
1100 gfc_st_label
**label
;
1109 old_loc
= gfc_current_locus
;
1110 va_start (argp
, target
);
1120 gfc_gobble_whitespace ();
1131 vp
= va_arg (argp
, void **);
1132 n
= gfc_match_expr ((gfc_expr
**) vp
);
1143 vp
= va_arg (argp
, void **);
1144 n
= gfc_match_variable ((gfc_expr
**) vp
, 0);
1156 vp
= va_arg (argp
, void **);
1157 n
= gfc_match_symbol ((gfc_symbol
**) vp
, c
== 'S');
1168 np
= va_arg (argp
, char *);
1169 n
= gfc_match_name (np
);
1180 label
= va_arg (argp
, gfc_st_label
**);
1181 n
= gfc_match_st_label (label
);
1192 ip
= va_arg (argp
, int *);
1193 n
= gfc_match_intrinsic_op ((gfc_intrinsic_op
*) ip
);
1204 if (gfc_match_eos () != MATCH_YES
)
1212 if (gfc_match_space () == MATCH_YES
)
1218 break; /* Fall through to character matcher. */
1221 gfc_internal_error ("gfc_match(): Bad match code %c", c
);
1227 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1228 expect an upper case character here! */
1229 gcc_assert (TOLOWER (c
) == c
);
1231 if (c
== gfc_next_ascii_char ())
1241 /* Clean up after a failed match. */
1242 gfc_current_locus
= old_loc
;
1243 va_start (argp
, target
);
1246 for (; matches
> 0; matches
--)
1248 while (*p
++ != '%');
1256 /* Matches that don't have to be undone */
1261 (void) va_arg (argp
, void **);
1266 vp
= va_arg (argp
, void **);
1267 gfc_free_expr ((struct gfc_expr
*)*vp
);
1280 /*********************** Statement level matching **********************/
1282 /* Matches the start of a program unit, which is the program keyword
1283 followed by an obligatory symbol. */
1286 gfc_match_program (void)
1291 m
= gfc_match ("% %s%t", &sym
);
1295 gfc_error ("Invalid form of PROGRAM statement at %C");
1299 if (m
== MATCH_ERROR
)
1302 if (!gfc_add_flavor (&sym
->attr
, FL_PROGRAM
, sym
->name
, NULL
))
1305 gfc_new_block
= sym
;
1311 /* Match a simple assignment statement. */
1314 gfc_match_assignment (void)
1316 gfc_expr
*lvalue
, *rvalue
;
1320 old_loc
= gfc_current_locus
;
1323 m
= gfc_match (" %v =", &lvalue
);
1326 gfc_current_locus
= old_loc
;
1327 gfc_free_expr (lvalue
);
1332 m
= gfc_match (" %e%t", &rvalue
);
1335 && rvalue
->ts
.type
== BT_BOZ
1336 && lvalue
->ts
.type
== BT_CLASS
)
1339 gfc_error ("BOZ literal constant at %L is neither a DATA statement "
1340 "value nor an actual argument of INT/REAL/DBLE/CMPLX "
1341 "intrinsic subprogram", &rvalue
->where
);
1344 if (lvalue
->expr_type
== EXPR_CONSTANT
)
1346 /* This clobbers %len and %kind. */
1348 gfc_error ("Assignment to a constant expression at %C");
1353 gfc_current_locus
= old_loc
;
1354 gfc_free_expr (lvalue
);
1355 gfc_free_expr (rvalue
);
1359 if (!lvalue
->symtree
)
1361 gfc_free_expr (lvalue
);
1362 gfc_free_expr (rvalue
);
1367 gfc_set_sym_referenced (lvalue
->symtree
->n
.sym
);
1369 new_st
.op
= EXEC_ASSIGN
;
1370 new_st
.expr1
= lvalue
;
1371 new_st
.expr2
= rvalue
;
1373 gfc_check_do_variable (lvalue
->symtree
);
1379 /* Match a pointer assignment statement. */
1382 gfc_match_pointer_assignment (void)
1384 gfc_expr
*lvalue
, *rvalue
;
1388 old_loc
= gfc_current_locus
;
1390 lvalue
= rvalue
= NULL
;
1391 gfc_matching_ptr_assignment
= 0;
1392 gfc_matching_procptr_assignment
= 0;
1394 m
= gfc_match (" %v =>", &lvalue
);
1395 if (m
!= MATCH_YES
|| !lvalue
->symtree
)
1401 if (lvalue
->symtree
->n
.sym
->attr
.proc_pointer
1402 || gfc_is_proc_ptr_comp (lvalue
))
1403 gfc_matching_procptr_assignment
= 1;
1405 gfc_matching_ptr_assignment
= 1;
1407 m
= gfc_match (" %e%t", &rvalue
);
1408 gfc_matching_ptr_assignment
= 0;
1409 gfc_matching_procptr_assignment
= 0;
1413 new_st
.op
= EXEC_POINTER_ASSIGN
;
1414 new_st
.expr1
= lvalue
;
1415 new_st
.expr2
= rvalue
;
1420 gfc_current_locus
= old_loc
;
1421 gfc_free_expr (lvalue
);
1422 gfc_free_expr (rvalue
);
1427 /* We try to match an easy arithmetic IF statement. This only happens
1428 when just after having encountered a simple IF statement. This code
1429 is really duplicate with parts of the gfc_match_if code, but this is
1433 match_arithmetic_if (void)
1435 gfc_st_label
*l1
, *l2
, *l3
;
1439 m
= gfc_match (" ( %e ) %l , %l , %l%t", &expr
, &l1
, &l2
, &l3
);
1443 if (!gfc_reference_st_label (l1
, ST_LABEL_TARGET
)
1444 || !gfc_reference_st_label (l2
, ST_LABEL_TARGET
)
1445 || !gfc_reference_st_label (l3
, ST_LABEL_TARGET
))
1447 gfc_free_expr (expr
);
1451 if (!gfc_notify_std (GFC_STD_F95_OBS
| GFC_STD_F2018_DEL
,
1452 "Arithmetic IF statement at %C"))
1455 new_st
.op
= EXEC_ARITHMETIC_IF
;
1456 new_st
.expr1
= expr
;
1465 /* The IF statement is a bit of a pain. First of all, there are three
1466 forms of it, the simple IF, the IF that starts a block and the
1469 There is a problem with the simple IF and that is the fact that we
1470 only have a single level of undo information on symbols. What this
1471 means is for a simple IF, we must re-match the whole IF statement
1472 multiple times in order to guarantee that the symbol table ends up
1473 in the proper state. */
1475 static match
match_simple_forall (void);
1476 static match
match_simple_where (void);
1479 gfc_match_if (gfc_statement
*if_type
)
1482 gfc_st_label
*l1
, *l2
, *l3
;
1483 locus old_loc
, old_loc2
;
1487 n
= gfc_match_label ();
1488 if (n
== MATCH_ERROR
)
1491 old_loc
= gfc_current_locus
;
1493 m
= gfc_match (" if ", &expr
);
1497 if (gfc_match_char ('(') != MATCH_YES
)
1499 gfc_error ("Missing %<(%> in IF-expression at %C");
1503 m
= gfc_match ("%e", &expr
);
1507 old_loc2
= gfc_current_locus
;
1508 gfc_current_locus
= old_loc
;
1510 if (gfc_match_parens () == MATCH_ERROR
)
1513 gfc_current_locus
= old_loc2
;
1515 if (gfc_match_char (')') != MATCH_YES
)
1517 gfc_error ("Syntax error in IF-expression at %C");
1518 gfc_free_expr (expr
);
1522 m
= gfc_match (" %l , %l , %l%t", &l1
, &l2
, &l3
);
1528 gfc_error ("Block label not appropriate for arithmetic IF "
1530 gfc_free_expr (expr
);
1534 if (!gfc_reference_st_label (l1
, ST_LABEL_TARGET
)
1535 || !gfc_reference_st_label (l2
, ST_LABEL_TARGET
)
1536 || !gfc_reference_st_label (l3
, ST_LABEL_TARGET
))
1538 gfc_free_expr (expr
);
1542 if (!gfc_notify_std (GFC_STD_F95_OBS
| GFC_STD_F2018_DEL
,
1543 "Arithmetic IF statement at %C"))
1546 new_st
.op
= EXEC_ARITHMETIC_IF
;
1547 new_st
.expr1
= expr
;
1552 *if_type
= ST_ARITHMETIC_IF
;
1556 if (gfc_match (" then%t") == MATCH_YES
)
1558 new_st
.op
= EXEC_IF
;
1559 new_st
.expr1
= expr
;
1560 *if_type
= ST_IF_BLOCK
;
1566 gfc_error ("Block label is not appropriate for IF statement at %C");
1567 gfc_free_expr (expr
);
1571 /* At this point the only thing left is a simple IF statement. At
1572 this point, n has to be MATCH_NO, so we don't have to worry about
1573 re-matching a block label. From what we've got so far, try
1574 matching an assignment. */
1576 *if_type
= ST_SIMPLE_IF
;
1578 m
= gfc_match_assignment ();
1582 gfc_free_expr (expr
);
1583 gfc_undo_symbols ();
1584 gfc_current_locus
= old_loc
;
1586 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1587 assignment was found. For MATCH_NO, continue to call the various
1589 if (m
== MATCH_ERROR
)
1592 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match. */
1594 m
= gfc_match_pointer_assignment ();
1598 gfc_free_expr (expr
);
1599 gfc_undo_symbols ();
1600 gfc_current_locus
= old_loc
;
1602 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match. */
1604 /* Look at the next keyword to see which matcher to call. Matching
1605 the keyword doesn't affect the symbol table, so we don't have to
1606 restore between tries. */
1608 #define match(string, subr, statement) \
1609 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1613 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
)
1614 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
)
1615 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
)
1616 match ("call", gfc_match_call
, ST_CALL
)
1617 match ("change% team", gfc_match_change_team
, ST_CHANGE_TEAM
)
1618 match ("close", gfc_match_close
, ST_CLOSE
)
1619 match ("continue", gfc_match_continue
, ST_CONTINUE
)
1620 match ("cycle", gfc_match_cycle
, ST_CYCLE
)
1621 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
)
1622 match ("end file", gfc_match_endfile
, ST_END_FILE
)
1623 match ("end team", gfc_match_end_team
, ST_END_TEAM
)
1624 match ("error% stop", gfc_match_error_stop
, ST_ERROR_STOP
)
1625 match ("event% post", gfc_match_event_post
, ST_EVENT_POST
)
1626 match ("event% wait", gfc_match_event_wait
, ST_EVENT_WAIT
)
1627 match ("exit", gfc_match_exit
, ST_EXIT
)
1628 match ("fail% image", gfc_match_fail_image
, ST_FAIL_IMAGE
)
1629 match ("flush", gfc_match_flush
, ST_FLUSH
)
1630 match ("forall", match_simple_forall
, ST_FORALL
)
1631 match ("form% team", gfc_match_form_team
, ST_FORM_TEAM
)
1632 match ("go to", gfc_match_goto
, ST_GOTO
)
1633 match ("if", match_arithmetic_if
, ST_ARITHMETIC_IF
)
1634 match ("inquire", gfc_match_inquire
, ST_INQUIRE
)
1635 match ("lock", gfc_match_lock
, ST_LOCK
)
1636 match ("nullify", gfc_match_nullify
, ST_NULLIFY
)
1637 match ("open", gfc_match_open
, ST_OPEN
)
1638 match ("pause", gfc_match_pause
, ST_NONE
)
1639 match ("print", gfc_match_print
, ST_WRITE
)
1640 match ("read", gfc_match_read
, ST_READ
)
1641 match ("return", gfc_match_return
, ST_RETURN
)
1642 match ("rewind", gfc_match_rewind
, ST_REWIND
)
1643 match ("stop", gfc_match_stop
, ST_STOP
)
1644 match ("wait", gfc_match_wait
, ST_WAIT
)
1645 match ("sync% all", gfc_match_sync_all
, ST_SYNC_CALL
);
1646 match ("sync% images", gfc_match_sync_images
, ST_SYNC_IMAGES
);
1647 match ("sync% memory", gfc_match_sync_memory
, ST_SYNC_MEMORY
);
1648 match ("sync% team", gfc_match_sync_team
, ST_SYNC_TEAM
)
1649 match ("unlock", gfc_match_unlock
, ST_UNLOCK
)
1650 match ("where", match_simple_where
, ST_WHERE
)
1651 match ("write", gfc_match_write
, ST_WRITE
)
1654 match ("type", gfc_match_print
, ST_WRITE
)
1656 /* All else has failed, so give up. See if any of the matchers has
1657 stored an error message of some sort. */
1658 if (!gfc_error_check ())
1659 gfc_error ("Syntax error in IF-clause after %C");
1661 gfc_free_expr (expr
);
1666 gfc_error ("Syntax error in IF-clause after %C");
1669 gfc_free_expr (expr
);
1673 /* At this point, we've matched the single IF and the action clause
1674 is in new_st. Rearrange things so that the IF statement appears
1677 p
= gfc_get_code (EXEC_IF
);
1678 p
->next
= XCNEW (gfc_code
);
1680 p
->next
->loc
= gfc_current_locus
;
1684 gfc_clear_new_st ();
1686 new_st
.op
= EXEC_IF
;
1695 /* Match an ELSE statement. */
1698 gfc_match_else (void)
1700 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1702 if (gfc_match_eos () == MATCH_YES
)
1705 if (gfc_match_name (name
) != MATCH_YES
1706 || gfc_current_block () == NULL
1707 || gfc_match_eos () != MATCH_YES
)
1709 gfc_error ("Invalid character(s) in ELSE statement after %C");
1713 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1715 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1716 name
, gfc_current_block ()->name
);
1724 /* Match an ELSE IF statement. */
1727 gfc_match_elseif (void)
1729 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1730 gfc_expr
*expr
, *then
;
1734 if (gfc_match_char ('(') != MATCH_YES
)
1736 gfc_error ("Missing %<(%> in ELSE IF expression at %C");
1740 m
= gfc_match (" %e ", &expr
);
1744 if (gfc_match_char (')') != MATCH_YES
)
1746 gfc_error ("Missing %<)%> in ELSE IF expression at %C");
1750 m
= gfc_match (" then ", &then
);
1752 where
= gfc_current_locus
;
1754 if (m
== MATCH_YES
&& (gfc_match_eos () == MATCH_YES
1755 || (gfc_current_block ()
1756 && gfc_match_name (name
) == MATCH_YES
)))
1759 if (gfc_match_eos () == MATCH_YES
)
1761 gfc_error ("Missing THEN in ELSE IF statement after %L", &where
);
1765 if (gfc_match_name (name
) != MATCH_YES
1766 || gfc_current_block () == NULL
1767 || gfc_match_eos () != MATCH_YES
)
1769 gfc_error ("Syntax error in ELSE IF statement after %L", &where
);
1773 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1775 gfc_error ("Label %qs after %L doesn't match IF label %qs",
1776 name
, &where
, gfc_current_block ()->name
);
1784 new_st
.op
= EXEC_IF
;
1785 new_st
.expr1
= expr
;
1789 gfc_free_expr (expr
);
1794 /* Free a gfc_iterator structure. */
1797 gfc_free_iterator (gfc_iterator
*iter
, int flag
)
1803 gfc_free_expr (iter
->var
);
1804 gfc_free_expr (iter
->start
);
1805 gfc_free_expr (iter
->end
);
1806 gfc_free_expr (iter
->step
);
1813 /* Match a CRITICAL statement. */
1815 gfc_match_critical (void)
1817 gfc_st_label
*label
= NULL
;
1819 if (gfc_match_label () == MATCH_ERROR
)
1822 if (gfc_match (" critical") != MATCH_YES
)
1825 if (gfc_match_st_label (&label
) == MATCH_ERROR
)
1828 if (gfc_match_eos () != MATCH_YES
)
1830 gfc_syntax_error (ST_CRITICAL
);
1834 if (gfc_pure (NULL
))
1836 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1840 if (gfc_find_state (COMP_DO_CONCURRENT
))
1842 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1847 gfc_unset_implicit_pure (NULL
);
1849 if (!gfc_notify_std (GFC_STD_F2008
, "CRITICAL statement at %C"))
1852 if (flag_coarray
== GFC_FCOARRAY_NONE
)
1854 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1859 if (gfc_find_state (COMP_CRITICAL
))
1861 gfc_error ("Nested CRITICAL block at %C");
1865 new_st
.op
= EXEC_CRITICAL
;
1868 && !gfc_reference_st_label (label
, ST_LABEL_TARGET
))
1875 /* Match a BLOCK statement. */
1878 gfc_match_block (void)
1882 if (gfc_match_label () == MATCH_ERROR
)
1885 if (gfc_match (" block") != MATCH_YES
)
1888 /* For this to be a correct BLOCK statement, the line must end now. */
1889 m
= gfc_match_eos ();
1890 if (m
== MATCH_ERROR
)
1899 /* Match an ASSOCIATE statement. */
1902 gfc_match_associate (void)
1904 if (gfc_match_label () == MATCH_ERROR
)
1907 if (gfc_match (" associate") != MATCH_YES
)
1910 /* Match the association list. */
1911 if (gfc_match_char ('(') != MATCH_YES
)
1913 gfc_error ("Expected association list at %C");
1916 new_st
.ext
.block
.assoc
= NULL
;
1919 gfc_association_list
* newAssoc
= gfc_get_association_list ();
1920 gfc_association_list
* a
;
1922 /* Match the next association. */
1923 if (gfc_match (" %n =>", newAssoc
->name
) != MATCH_YES
)
1925 gfc_error ("Expected association at %C");
1926 goto assocListError
;
1929 if (gfc_match (" %e", &newAssoc
->target
) != MATCH_YES
)
1931 /* Have another go, allowing for procedure pointer selectors. */
1932 gfc_matching_procptr_assignment
= 1;
1933 if (gfc_match (" %e", &newAssoc
->target
) != MATCH_YES
)
1935 gfc_matching_procptr_assignment
= 0;
1936 gfc_error ("Invalid association target at %C");
1937 goto assocListError
;
1939 gfc_matching_procptr_assignment
= 0;
1941 newAssoc
->where
= gfc_current_locus
;
1943 /* Check that the current name is not yet in the list. */
1944 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
1945 if (!strcmp (a
->name
, newAssoc
->name
))
1947 gfc_error ("Duplicate name %qs in association at %C",
1949 goto assocListError
;
1952 /* The target expression must not be coindexed. */
1953 if (gfc_is_coindexed (newAssoc
->target
))
1955 gfc_error ("Association target at %C must not be coindexed");
1956 goto assocListError
;
1959 /* The target expression cannot be a BOZ literal constant. */
1960 if (newAssoc
->target
->ts
.type
== BT_BOZ
)
1962 gfc_error ("Association target at %L cannot be a BOZ literal "
1963 "constant", &newAssoc
->target
->where
);
1964 goto assocListError
;
1967 /* The `variable' field is left blank for now; because the target is not
1968 yet resolved, we can't use gfc_has_vector_subscript to determine it
1969 for now. This is set during resolution. */
1971 /* Put it into the list. */
1972 newAssoc
->next
= new_st
.ext
.block
.assoc
;
1973 new_st
.ext
.block
.assoc
= newAssoc
;
1975 /* Try next one or end if closing parenthesis is found. */
1976 gfc_gobble_whitespace ();
1977 if (gfc_peek_char () == ')')
1979 if (gfc_match_char (',') != MATCH_YES
)
1981 gfc_error ("Expected %<)%> or %<,%> at %C");
1991 if (gfc_match_char (')') != MATCH_YES
)
1993 /* This should never happen as we peek above. */
1997 if (gfc_match_eos () != MATCH_YES
)
1999 gfc_error ("Junk after ASSOCIATE statement at %C");
2006 gfc_free_association_list (new_st
.ext
.block
.assoc
);
2011 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
2012 an accessible derived type. */
2015 match_derived_type_spec (gfc_typespec
*ts
)
2017 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2019 gfc_symbol
*derived
, *der_type
;
2020 match m
= MATCH_YES
;
2021 gfc_actual_arglist
*decl_type_param_list
= NULL
;
2022 bool is_pdt_template
= false;
2024 old_locus
= gfc_current_locus
;
2026 if (gfc_match ("%n", name
) != MATCH_YES
)
2028 gfc_current_locus
= old_locus
;
2032 gfc_find_symbol (name
, NULL
, 1, &derived
);
2034 /* Match the PDT spec list, if there. */
2035 if (derived
&& derived
->attr
.flavor
== FL_PROCEDURE
)
2037 gfc_find_symbol (gfc_dt_upper_string (name
), NULL
, 1, &der_type
);
2038 is_pdt_template
= der_type
2039 && der_type
->attr
.flavor
== FL_DERIVED
2040 && der_type
->attr
.pdt_template
;
2043 if (is_pdt_template
)
2044 m
= gfc_match_actual_arglist (1, &decl_type_param_list
, true);
2046 if (m
== MATCH_ERROR
)
2048 gfc_free_actual_arglist (decl_type_param_list
);
2052 if (derived
&& derived
->attr
.flavor
== FL_PROCEDURE
&& derived
->attr
.generic
)
2053 derived
= gfc_find_dt_in_generic (derived
);
2055 /* If this is a PDT, find the specific instance. */
2056 if (m
== MATCH_YES
&& is_pdt_template
)
2058 gfc_namespace
*old_ns
;
2060 old_ns
= gfc_current_ns
;
2061 while (gfc_current_ns
&& gfc_current_ns
->parent
)
2062 gfc_current_ns
= gfc_current_ns
->parent
;
2064 if (type_param_spec_list
)
2065 gfc_free_actual_arglist (type_param_spec_list
);
2066 m
= gfc_get_pdt_instance (decl_type_param_list
, &der_type
,
2067 &type_param_spec_list
);
2068 gfc_free_actual_arglist (decl_type_param_list
);
2073 gcc_assert (!derived
->attr
.pdt_template
&& derived
->attr
.pdt_type
);
2074 gfc_set_sym_referenced (derived
);
2076 gfc_current_ns
= old_ns
;
2079 if (derived
&& derived
->attr
.flavor
== FL_DERIVED
)
2081 ts
->type
= BT_DERIVED
;
2082 ts
->u
.derived
= derived
;
2086 gfc_current_locus
= old_locus
;
2091 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2092 gfc_match_decl_type_spec() from decl.cc, with the following exceptions:
2093 It only includes the intrinsic types from the Fortran 2003 standard
2094 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2095 the implicit_flag is not needed, so it was removed. Derived types are
2096 identified by their name alone. */
2099 gfc_match_type_spec (gfc_typespec
*ts
)
2103 char c
, name
[GFC_MAX_SYMBOL_LEN
+ 1];
2106 gfc_gobble_whitespace ();
2107 old_locus
= gfc_current_locus
;
2109 /* If c isn't [a-z], then return immediately. */
2110 c
= gfc_peek_ascii_char ();
2114 type_param_spec_list
= NULL
;
2116 if (match_derived_type_spec (ts
) == MATCH_YES
)
2118 /* Enforce F03:C401. */
2119 if (ts
->u
.derived
->attr
.abstract
)
2121 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2122 ts
->u
.derived
->name
, &old_locus
);
2128 if (gfc_match ("integer") == MATCH_YES
)
2130 ts
->type
= BT_INTEGER
;
2131 ts
->kind
= gfc_default_integer_kind
;
2135 if (flag_unsigned
&& gfc_match ("unsigned") == MATCH_YES
)
2137 ts
->type
= BT_UNSIGNED
;
2138 ts
->kind
= gfc_default_integer_kind
;
2142 if (gfc_match ("double precision") == MATCH_YES
)
2145 ts
->kind
= gfc_default_double_kind
;
2149 if (gfc_match ("complex") == MATCH_YES
)
2151 ts
->type
= BT_COMPLEX
;
2152 ts
->kind
= gfc_default_complex_kind
;
2156 if (gfc_match ("character") == MATCH_YES
)
2158 ts
->type
= BT_CHARACTER
;
2160 m
= gfc_match_char_spec (ts
);
2168 /* REAL is a real pain because it can be a type, intrinsic subprogram,
2169 or list item in a type-list of an OpenMP reduction clause. Need to
2170 differentiate REAL([KIND]=scalar-int-initialization-expr) from
2171 REAL(A,[KIND]) and REAL(KIND,A). Logically, when this code was
2172 written the use of LOGICAL as a type-spec or intrinsic subprogram
2175 m
= gfc_match (" %n", name
);
2177 && (strcmp (name
, "real") == 0 || strcmp (name
, "logical") == 0))
2186 ts
->kind
= gfc_default_real_kind
;
2190 ts
->type
= BT_LOGICAL
;
2191 ts
->kind
= gfc_default_logical_kind
;
2194 gfc_gobble_whitespace ();
2196 /* Prevent REAL*4, etc. */
2197 c
= gfc_peek_ascii_char ();
2200 gfc_error ("Invalid type-spec at %C");
2204 /* Found leading colon in REAL::, a trailing ')' in for example
2205 TYPE IS (REAL), or REAL, for an OpenMP list-item. */
2206 if (c
== ':' || c
== ')' || (flag_openmp
&& c
== ','))
2209 /* Found something other than the opening '(' in REAL(... */
2213 gfc_next_char (); /* Burn the '('. */
2215 /* Look for the optional KIND=. */
2216 where
= gfc_current_locus
;
2217 m
= gfc_match ("%n", name
);
2220 gfc_gobble_whitespace ();
2221 c
= gfc_next_char ();
2224 if (strcmp(name
, "a") == 0 || strcmp(name
, "l") == 0)
2226 else if (strcmp(name
, "kind") == 0)
2232 gfc_current_locus
= where
;
2235 gfc_current_locus
= where
;
2239 m
= gfc_match_expr (&e
);
2240 if (m
== MATCH_NO
|| m
== MATCH_ERROR
)
2243 /* If a comma appears, it is an intrinsic subprogram. */
2244 gfc_gobble_whitespace ();
2245 c
= gfc_peek_ascii_char ();
2252 /* If ')' appears, we have REAL(initialization-expr), here check for
2253 a scalar integer initialization-expr and valid kind parameter. */
2257 if (e
->expr_type
!= EXPR_CONSTANT
&& e
->expr_type
!= EXPR_VARIABLE
)
2258 ok
= gfc_reduce_init_expr (e
);
2259 if (!ok
|| e
->ts
.type
!= BT_INTEGER
|| e
->rank
> 0)
2265 if (e
->expr_type
!= EXPR_CONSTANT
)
2268 gfc_next_char (); /* Burn the ')'. */
2269 ts
->kind
= (int) mpz_get_si (e
->value
.integer
);
2270 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) == -1)
2272 gfc_error ("Invalid type-spec at %C");
2284 /* If a type is not matched, simply return MATCH_NO. */
2285 gfc_current_locus
= old_locus
;
2290 gfc_gobble_whitespace ();
2292 /* This prevents INTEGER*4, etc. */
2293 if (gfc_peek_ascii_char () == '*')
2295 gfc_error ("Invalid type-spec at %C");
2299 m
= gfc_match_kind_spec (ts
, false);
2301 /* No kind specifier found. */
2309 /******************** FORALL subroutines ********************/
2311 /* Free a list of FORALL iterators. */
2314 gfc_free_forall_iterator (gfc_forall_iterator
*iter
)
2316 gfc_forall_iterator
*next
;
2321 gfc_free_expr (iter
->var
);
2322 gfc_free_expr (iter
->start
);
2323 gfc_free_expr (iter
->end
);
2324 gfc_free_expr (iter
->stride
);
2331 /* Match an iterator as part of a FORALL statement. The format is:
2333 <var> = <start>:<end>[:<stride>]
2335 On MATCH_NO, the caller tests for the possibility that there is a
2336 scalar mask expression. */
2339 match_forall_iterator (gfc_forall_iterator
**result
)
2341 gfc_forall_iterator
*iter
;
2345 where
= gfc_current_locus
;
2346 iter
= XCNEW (gfc_forall_iterator
);
2348 m
= gfc_match_expr (&iter
->var
);
2352 if (gfc_match_char ('=') != MATCH_YES
2353 || iter
->var
->expr_type
!= EXPR_VARIABLE
)
2359 m
= gfc_match_expr (&iter
->start
);
2363 if (gfc_match_char (':') != MATCH_YES
)
2366 m
= gfc_match_expr (&iter
->end
);
2369 if (m
== MATCH_ERROR
)
2372 if (gfc_match_char (':') == MATCH_NO
)
2373 iter
->stride
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2376 m
= gfc_match_expr (&iter
->stride
);
2379 if (m
== MATCH_ERROR
)
2383 /* Mark the iteration variable's symbol as used as a FORALL index. */
2384 iter
->var
->symtree
->n
.sym
->forall_index
= true;
2390 gfc_error ("Syntax error in FORALL iterator at %C");
2395 gfc_current_locus
= where
;
2396 gfc_free_forall_iterator (iter
);
2401 /* Match the header of a FORALL statement. */
2404 match_forall_header (gfc_forall_iterator
**phead
, gfc_expr
**mask
)
2406 gfc_forall_iterator
*head
, *tail
, *new_iter
;
2410 gfc_gobble_whitespace ();
2415 if (gfc_match_char ('(') != MATCH_YES
)
2418 m
= match_forall_iterator (&new_iter
);
2419 if (m
== MATCH_ERROR
)
2424 head
= tail
= new_iter
;
2428 if (gfc_match_char (',') != MATCH_YES
)
2431 m
= match_forall_iterator (&new_iter
);
2432 if (m
== MATCH_ERROR
)
2437 tail
->next
= new_iter
;
2442 /* Have to have a mask expression. */
2444 m
= gfc_match_expr (&msk
);
2447 if (m
== MATCH_ERROR
)
2453 if (gfc_match_char (')') == MATCH_NO
)
2461 gfc_syntax_error (ST_FORALL
);
2464 gfc_free_expr (msk
);
2465 gfc_free_forall_iterator (head
);
2470 /* Match the rest of a simple FORALL statement that follows an
2474 match_simple_forall (void)
2476 gfc_forall_iterator
*head
;
2485 m
= match_forall_header (&head
, &mask
);
2492 m
= gfc_match_assignment ();
2494 if (m
== MATCH_ERROR
)
2498 m
= gfc_match_pointer_assignment ();
2499 if (m
== MATCH_ERROR
)
2505 c
= XCNEW (gfc_code
);
2507 c
->loc
= gfc_current_locus
;
2509 if (gfc_match_eos () != MATCH_YES
)
2512 gfc_clear_new_st ();
2513 new_st
.op
= EXEC_FORALL
;
2514 new_st
.expr1
= mask
;
2515 new_st
.ext
.forall_iterator
= head
;
2516 new_st
.block
= gfc_get_code (EXEC_FORALL
);
2517 new_st
.block
->next
= c
;
2522 gfc_syntax_error (ST_FORALL
);
2525 gfc_free_forall_iterator (head
);
2526 gfc_free_expr (mask
);
2532 /* Match a FORALL statement. */
2535 gfc_match_forall (gfc_statement
*st
)
2537 gfc_forall_iterator
*head
;
2546 m0
= gfc_match_label ();
2547 if (m0
== MATCH_ERROR
)
2550 m
= gfc_match (" forall");
2554 m
= match_forall_header (&head
, &mask
);
2555 if (m
== MATCH_ERROR
)
2560 if (gfc_match_eos () == MATCH_YES
)
2562 *st
= ST_FORALL_BLOCK
;
2563 new_st
.op
= EXEC_FORALL
;
2564 new_st
.expr1
= mask
;
2565 new_st
.ext
.forall_iterator
= head
;
2569 m
= gfc_match_assignment ();
2570 if (m
== MATCH_ERROR
)
2574 m
= gfc_match_pointer_assignment ();
2575 if (m
== MATCH_ERROR
)
2581 c
= XCNEW (gfc_code
);
2583 c
->loc
= gfc_current_locus
;
2585 gfc_clear_new_st ();
2586 new_st
.op
= EXEC_FORALL
;
2587 new_st
.expr1
= mask
;
2588 new_st
.ext
.forall_iterator
= head
;
2589 new_st
.block
= gfc_get_code (EXEC_FORALL
);
2590 new_st
.block
->next
= c
;
2596 gfc_syntax_error (ST_FORALL
);
2599 gfc_free_forall_iterator (head
);
2600 gfc_free_expr (mask
);
2601 gfc_free_statements (c
);
2606 /* Match a DO statement. */
2611 gfc_iterator iter
, *ip
;
2613 gfc_st_label
*label
;
2616 old_loc
= gfc_current_locus
;
2618 memset (&iter
, '\0', sizeof (gfc_iterator
));
2621 m
= gfc_match_label ();
2622 if (m
== MATCH_ERROR
)
2625 if (gfc_match (" do") != MATCH_YES
)
2628 m
= gfc_match_st_label (&label
);
2629 if (m
== MATCH_ERROR
)
2632 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2634 if (gfc_match_eos () == MATCH_YES
)
2636 iter
.end
= gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, true);
2637 new_st
.op
= EXEC_DO_WHILE
;
2641 /* Match an optional comma, if no comma is found, a space is obligatory. */
2642 if (gfc_match_char (',') != MATCH_YES
&& gfc_match ("% ") != MATCH_YES
)
2645 /* Check for balanced parens. */
2647 if (gfc_match_parens () == MATCH_ERROR
)
2650 if (gfc_match (" concurrent") == MATCH_YES
)
2652 gfc_forall_iterator
*head
;
2655 if (!gfc_notify_std (GFC_STD_F2008
, "DO CONCURRENT construct at %C"))
2661 m
= match_forall_header (&head
, &mask
);
2665 if (m
== MATCH_ERROR
)
2666 goto concurr_cleanup
;
2668 if (gfc_match_eos () != MATCH_YES
)
2669 goto concurr_cleanup
;
2672 && !gfc_reference_st_label (label
, ST_LABEL_DO_TARGET
))
2673 goto concurr_cleanup
;
2675 new_st
.label1
= label
;
2676 new_st
.op
= EXEC_DO_CONCURRENT
;
2677 new_st
.expr1
= mask
;
2678 new_st
.ext
.forall_iterator
= head
;
2683 gfc_syntax_error (ST_DO
);
2684 gfc_free_expr (mask
);
2685 gfc_free_forall_iterator (head
);
2689 /* See if we have a DO WHILE. */
2690 if (gfc_match (" while ( %e )%t", &iter
.end
) == MATCH_YES
)
2692 new_st
.op
= EXEC_DO_WHILE
;
2696 /* The abortive DO WHILE may have done something to the symbol
2697 table, so we start over. */
2698 gfc_undo_symbols ();
2699 gfc_current_locus
= old_loc
;
2701 gfc_match_label (); /* This won't error. */
2702 gfc_match (" do "); /* This will work. */
2704 gfc_match_st_label (&label
); /* Can't error out. */
2705 gfc_match_char (','); /* Optional comma. */
2707 m
= gfc_match_iterator (&iter
, 0);
2710 if (m
== MATCH_ERROR
)
2713 iter
.var
->symtree
->n
.sym
->attr
.implied_index
= 0;
2714 gfc_check_do_variable (iter
.var
->symtree
);
2716 if (gfc_match_eos () != MATCH_YES
)
2718 gfc_syntax_error (ST_DO
);
2722 new_st
.op
= EXEC_DO
;
2726 && !gfc_reference_st_label (label
, ST_LABEL_DO_TARGET
))
2729 new_st
.label1
= label
;
2731 if (new_st
.op
== EXEC_DO_WHILE
)
2732 new_st
.expr1
= iter
.end
;
2735 new_st
.ext
.iterator
= ip
= gfc_get_iterator ();
2742 gfc_free_iterator (&iter
, 0);
2748 /* Match an EXIT or CYCLE statement. */
2751 match_exit_cycle (gfc_statement st
, gfc_exec_op op
)
2753 gfc_state_data
*p
, *o
;
2758 if (gfc_match_eos () == MATCH_YES
)
2762 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2765 m
= gfc_match ("% %n%t", name
);
2766 if (m
== MATCH_ERROR
)
2770 gfc_syntax_error (st
);
2774 /* Find the corresponding symbol. If there's a BLOCK statement
2775 between here and the label, it is not in gfc_current_ns but a parent
2777 stree
= gfc_find_symtree_in_proc (name
, gfc_current_ns
);
2780 gfc_error ("Name %qs in %s statement at %C is unknown",
2781 name
, gfc_ascii_statement (st
));
2786 if (sym
->attr
.flavor
!= FL_LABEL
)
2788 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2789 name
, gfc_ascii_statement (st
));
2794 /* Find the loop specified by the label (or lack of a label). */
2795 for (o
= NULL
, p
= gfc_state_stack
; p
; p
= p
->previous
)
2796 if (o
== NULL
&& p
->state
== COMP_OMP_STRUCTURED_BLOCK
)
2798 else if (p
->state
== COMP_CRITICAL
)
2800 gfc_error("%s statement at %C leaves CRITICAL construct",
2801 gfc_ascii_statement (st
));
2804 else if (p
->state
== COMP_DO_CONCURRENT
2805 && (op
== EXEC_EXIT
|| (sym
&& sym
!= p
->sym
)))
2807 /* F2008, C821 & C845. */
2808 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2809 gfc_ascii_statement (st
));
2812 else if ((sym
&& sym
== p
->sym
)
2813 || (!sym
&& (p
->state
== COMP_DO
2814 || p
->state
== COMP_DO_CONCURRENT
)))
2820 gfc_error ("%s statement at %C is not within a construct",
2821 gfc_ascii_statement (st
));
2823 gfc_error ("%s statement at %C is not within construct %qs",
2824 gfc_ascii_statement (st
), sym
->name
);
2829 /* Special checks for EXIT from non-loop constructs. */
2833 case COMP_DO_CONCURRENT
:
2837 /* This is already handled above. */
2840 case COMP_ASSOCIATE
:
2844 case COMP_SELECT_TYPE
:
2845 case COMP_SELECT_RANK
:
2847 if (op
== EXEC_CYCLE
)
2849 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2850 " construct %qs", sym
->name
);
2853 gcc_assert (op
== EXEC_EXIT
);
2854 if (!gfc_notify_std (GFC_STD_F2008
, "EXIT statement with no"
2855 " do-construct-name at %C"))
2860 gfc_error ("%s statement at %C is not applicable to construct %qs",
2861 gfc_ascii_statement (st
), sym
->name
);
2867 gfc_error (is_oacc (p
)
2868 ? G_("%s statement at %C leaving OpenACC structured block")
2869 : G_("%s statement at %C leaving OpenMP structured block"),
2870 gfc_ascii_statement (st
));
2874 for (o
= p
, cnt
= 0; o
->state
== COMP_DO
&& o
->previous
!= NULL
; cnt
++)
2880 && o
->state
== COMP_OMP_STRUCTURED_BLOCK
)
2881 switch (o
->head
->op
)
2883 case EXEC_OACC_LOOP
:
2884 case EXEC_OACC_KERNELS_LOOP
:
2885 case EXEC_OACC_PARALLEL_LOOP
:
2886 case EXEC_OACC_SERIAL_LOOP
:
2887 gcc_assert (o
->head
->next
!= NULL
2888 && (o
->head
->next
->op
== EXEC_DO
2889 || o
->head
->next
->op
== EXEC_DO_WHILE
)
2890 && o
->previous
!= NULL
2891 && o
->previous
->tail
->op
== o
->head
->op
);
2892 if (o
->previous
->tail
->ext
.omp_clauses
!= NULL
)
2894 /* Both collapsed and tiled loops are lowered the same way, but are
2895 not compatible. In gfc_trans_omp_do, the tile is prioritized. */
2896 if (o
->previous
->tail
->ext
.omp_clauses
->tile_list
)
2900 = o
->previous
->tail
->ext
.omp_clauses
->tile_list
;
2901 for ( ; el
; el
= el
->next
)
2904 else if (o
->previous
->tail
->ext
.omp_clauses
->collapse
> 1)
2905 count
= o
->previous
->tail
->ext
.omp_clauses
->collapse
;
2907 if (st
== ST_EXIT
&& cnt
<= count
)
2909 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2912 if (st
== ST_CYCLE
&& cnt
< count
)
2914 gfc_error (o
->previous
->tail
->ext
.omp_clauses
->tile_list
2915 ? G_("CYCLE statement at %C to non-innermost tiled "
2917 : G_("CYCLE statement at %C to non-innermost collapsed "
2918 "!$ACC LOOP loop"));
2922 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
2923 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD
:
2924 case EXEC_OMP_TARGET_SIMD
:
2925 case EXEC_OMP_TASKLOOP_SIMD
:
2926 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD
:
2927 case EXEC_OMP_MASTER_TASKLOOP_SIMD
:
2928 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD
:
2929 case EXEC_OMP_MASKED_TASKLOOP_SIMD
:
2930 case EXEC_OMP_PARALLEL_DO_SIMD
:
2931 case EXEC_OMP_DISTRIBUTE_SIMD
:
2932 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
2933 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD
:
2934 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
2936 case EXEC_OMP_PARALLEL_LOOP
:
2937 case EXEC_OMP_TEAMS_LOOP
:
2938 case EXEC_OMP_TARGET_PARALLEL_LOOP
:
2939 case EXEC_OMP_TARGET_TEAMS_LOOP
:
2941 case EXEC_OMP_PARALLEL_DO
:
2943 case EXEC_OMP_DO_SIMD
:
2944 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO
:
2945 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
2946 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
2947 case EXEC_OMP_TARGET_PARALLEL_DO
:
2948 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
2950 gcc_assert (o
->head
->next
!= NULL
2951 && (o
->head
->next
->op
== EXEC_DO
2952 || o
->head
->next
->op
== EXEC_DO_WHILE
)
2953 && o
->previous
!= NULL
2954 && o
->previous
->tail
->op
== o
->head
->op
);
2955 if (o
->previous
->tail
->ext
.omp_clauses
!= NULL
)
2957 if (o
->previous
->tail
->ext
.omp_clauses
->collapse
> 1)
2958 count
= o
->previous
->tail
->ext
.omp_clauses
->collapse
;
2959 if (o
->previous
->tail
->ext
.omp_clauses
->orderedc
)
2960 count
= o
->previous
->tail
->ext
.omp_clauses
->orderedc
;
2962 if (st
== ST_EXIT
&& cnt
<= count
)
2964 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2967 if (st
== ST_CYCLE
&& cnt
< count
)
2969 gfc_error ("CYCLE statement at %C to non-innermost collapsed "
2978 /* Save the first statement in the construct - needed by the backend. */
2979 new_st
.ext
.which_construct
= p
->construct
;
2987 /* Match the EXIT statement. */
2990 gfc_match_exit (void)
2992 return match_exit_cycle (ST_EXIT
, EXEC_EXIT
);
2996 /* Match the CYCLE statement. */
2999 gfc_match_cycle (void)
3001 return match_exit_cycle (ST_CYCLE
, EXEC_CYCLE
);
3005 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
3006 requirements for a stop-code differ in the standards.
3010 R840 stop-stmt is STOP [ stop-code ]
3011 R841 stop-code is scalar-char-constant
3012 or digit [ digit [ digit [ digit [ digit ] ] ] ]
3014 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
3017 R855 stop-stmt is STOP [ stop-code ]
3018 R856 allstop-stmt is ALL STOP [ stop-code ]
3019 R857 stop-code is scalar-default-char-constant-expr
3020 or scalar-int-constant-expr
3023 R1160 stop-stmt is STOP [ stop-code ] [ , QUIET = scalar-logical-expr]
3024 R1161 error-stop-stmt is
3025 ERROR STOP [ stop-code ] [ , QUIET = scalar-logical-expr]
3026 R1162 stop-code is scalar-default-char-expr
3029 For free-form source code, all standards contain a statement of the form:
3031 A blank shall be used to separate names, constants, or labels from
3032 adjacent keywords, names, constants, or labels.
3034 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
3038 is valid, but it is invalid Fortran 2008. */
3041 gfc_match_stopcode (gfc_statement st
)
3044 gfc_expr
*quiet
= NULL
;
3049 /* Set f95 for -std=f95. */
3050 f95
= (gfc_option
.allow_std
== GFC_STD_OPT_F95
);
3052 /* Set f03 for -std=f2003. */
3053 f03
= (gfc_option
.allow_std
== GFC_STD_OPT_F03
);
3055 /* Set f08 for -std=f2008. */
3056 f08
= (gfc_option
.allow_std
== GFC_STD_OPT_F08
);
3058 /* Plain STOP statement? */
3059 if (gfc_match_eos () == MATCH_YES
)
3062 /* Look for a blank between STOP and the stop-code for F2008 or later.
3063 But allow for F2018's ,QUIET= specifier. */
3064 c
= gfc_peek_ascii_char ();
3066 if (gfc_current_form
!= FORM_FIXED
&& !(f95
|| f03
) && c
!= ',')
3068 /* Look for end-of-statement. There is no stop-code. */
3069 if (c
== '\n' || c
== '!' || c
== ';')
3074 gfc_error ("Blank required in %s statement near %C",
3075 gfc_ascii_statement (st
));
3082 gfc_gobble_whitespace ();
3083 c
= gfc_peek_ascii_char ();
3090 /* First look for the F95 or F2003 digit [...] construct. */
3091 old_locus
= gfc_current_locus
;
3092 m
= gfc_match_small_int (&stopcode
);
3093 if (m
== MATCH_YES
&& (f95
|| f03
))
3097 gfc_error ("STOP code at %C cannot be negative");
3101 if (stopcode
> 99999)
3103 gfc_error ("STOP code at %C contains too many digits");
3108 /* Reset the locus and now load gfc_expr. */
3109 gfc_current_locus
= old_locus
;
3110 m
= gfc_match_expr (&e
);
3111 if (m
== MATCH_ERROR
)
3117 if (gfc_match (" , quiet = %e", &quiet
) == MATCH_YES
)
3119 if (!gfc_notify_std (GFC_STD_F2018
, "QUIET= specifier for %s at %L",
3120 gfc_ascii_statement (st
), &quiet
->where
))
3124 if (gfc_match_eos () != MATCH_YES
)
3129 if (gfc_pure (NULL
))
3131 if (st
== ST_ERROR_STOP
)
3133 if (!gfc_notify_std (GFC_STD_F2018
, "%s statement at %C in PURE "
3134 "procedure", gfc_ascii_statement (st
)))
3139 gfc_error ("%s statement not allowed in PURE procedure at %C",
3140 gfc_ascii_statement (st
));
3145 gfc_unset_implicit_pure (NULL
);
3147 if (st
== ST_STOP
&& gfc_find_state (COMP_CRITICAL
))
3149 gfc_error ("Image control statement STOP at %C in CRITICAL block");
3152 if (st
== ST_STOP
&& gfc_find_state (COMP_DO_CONCURRENT
))
3154 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
3160 if (!gfc_simplify_expr (e
, 0))
3163 /* Test for F95 and F2003 style STOP stop-code. */
3164 if (e
->expr_type
!= EXPR_CONSTANT
&& (f95
|| f03
))
3166 gfc_error ("STOP code at %L must be a scalar CHARACTER constant "
3167 "or digit[digit[digit[digit[digit]]]]", &e
->where
);
3171 /* Use the machinery for an initialization expression to reduce the
3172 stop-code to a constant. */
3173 gfc_reduce_init_expr (e
);
3175 /* Test for F2008 style STOP stop-code. */
3176 if (e
->expr_type
!= EXPR_CONSTANT
&& f08
)
3178 gfc_error ("STOP code at %L must be a scalar default CHARACTER or "
3179 "INTEGER constant expression", &e
->where
);
3183 if (!(e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_INTEGER
))
3185 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
3192 gfc_error ("STOP code at %L must be scalar", &e
->where
);
3196 if (e
->ts
.type
== BT_CHARACTER
3197 && e
->ts
.kind
!= gfc_default_character_kind
)
3199 gfc_error ("STOP code at %L must be default character KIND=%d",
3200 &e
->where
, (int) gfc_default_character_kind
);
3204 if (e
->ts
.type
== BT_INTEGER
&& e
->ts
.kind
!= gfc_default_integer_kind
3205 && !gfc_notify_std (GFC_STD_F2018
,
3206 "STOP code at %L must be default integer KIND=%d",
3207 &e
->where
, (int) gfc_default_integer_kind
))
3213 if (!gfc_simplify_expr (quiet
, 0))
3216 if (quiet
->rank
!= 0)
3218 gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
3229 new_st
.op
= EXEC_STOP
;
3232 new_st
.op
= EXEC_ERROR_STOP
;
3235 new_st
.op
= EXEC_PAUSE
;
3242 new_st
.expr2
= quiet
;
3243 new_st
.ext
.stop_code
= -1;
3248 gfc_syntax_error (st
);
3253 gfc_free_expr (quiet
);
3258 /* Match the (deprecated) PAUSE statement. */
3261 gfc_match_pause (void)
3265 m
= gfc_match_stopcode (ST_PAUSE
);
3268 if (!gfc_notify_std (GFC_STD_F95_DEL
, "PAUSE statement at %C"))
3275 /* Match the STOP statement. */
3278 gfc_match_stop (void)
3280 return gfc_match_stopcode (ST_STOP
);
3284 /* Match the ERROR STOP statement. */
3287 gfc_match_error_stop (void)
3289 if (!gfc_notify_std (GFC_STD_F2008
, "ERROR STOP statement at %C"))
3292 return gfc_match_stopcode (ST_ERROR_STOP
);
3295 /* Match EVENT POST/WAIT statement. Syntax:
3296 EVENT POST ( event-variable [, sync-stat-list] )
3297 EVENT WAIT ( event-variable [, wait-spec-list] )
3299 wait-spec-list is sync-stat-list or until-spec
3300 until-spec is UNTIL_COUNT = scalar-int-expr
3301 sync-stat is STAT= or ERRMSG=. */
3304 event_statement (gfc_statement st
)
3307 gfc_expr
*tmp
, *eventvar
, *until_count
, *stat
, *errmsg
;
3308 bool saw_until_count
, saw_stat
, saw_errmsg
;
3310 tmp
= eventvar
= until_count
= stat
= errmsg
= NULL
;
3311 saw_until_count
= saw_stat
= saw_errmsg
= false;
3313 if (gfc_pure (NULL
))
3315 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3316 st
== ST_EVENT_POST
? "POST" : "WAIT");
3320 gfc_unset_implicit_pure (NULL
);
3322 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3324 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3328 if (gfc_find_state (COMP_CRITICAL
))
3330 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3331 st
== ST_EVENT_POST
? "POST" : "WAIT");
3335 if (gfc_find_state (COMP_DO_CONCURRENT
))
3337 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3338 "block", st
== ST_EVENT_POST
? "POST" : "WAIT");
3342 if (gfc_match_char ('(') != MATCH_YES
)
3345 if (gfc_match ("%e", &eventvar
) != MATCH_YES
)
3347 m
= gfc_match_char (',');
3348 if (m
== MATCH_ERROR
)
3352 m
= gfc_match_char (')');
3360 m
= gfc_match (" stat = %v", &tmp
);
3361 if (m
== MATCH_ERROR
)
3367 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
3373 m
= gfc_match_char (',');
3381 m
= gfc_match (" errmsg = %v", &tmp
);
3382 if (m
== MATCH_ERROR
)
3388 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
3394 m
= gfc_match_char (',');
3402 m
= gfc_match (" until_count = %e", &tmp
);
3403 if (m
== MATCH_ERROR
|| st
== ST_EVENT_POST
)
3407 if (saw_until_count
)
3409 gfc_error ("Redundant UNTIL_COUNT tag found at %L",
3414 saw_until_count
= true;
3416 m
= gfc_match_char (',');
3427 if (m
== MATCH_ERROR
)
3430 if (gfc_match (" )%t") != MATCH_YES
)
3437 new_st
.op
= EXEC_EVENT_POST
;
3440 new_st
.op
= EXEC_EVENT_WAIT
;
3446 new_st
.expr1
= eventvar
;
3447 new_st
.expr2
= stat
;
3448 new_st
.expr3
= errmsg
;
3449 new_st
.expr4
= until_count
;
3454 gfc_syntax_error (st
);
3457 if (until_count
!= tmp
)
3458 gfc_free_expr (until_count
);
3460 gfc_free_expr (errmsg
);
3462 gfc_free_expr (stat
);
3464 gfc_free_expr (tmp
);
3465 gfc_free_expr (eventvar
);
3473 gfc_match_event_post (void)
3475 if (!gfc_notify_std (GFC_STD_F2018
, "EVENT POST statement at %C"))
3478 return event_statement (ST_EVENT_POST
);
3483 gfc_match_event_wait (void)
3485 if (!gfc_notify_std (GFC_STD_F2018
, "EVENT WAIT statement at %C"))
3488 return event_statement (ST_EVENT_WAIT
);
3492 /* Match a FAIL IMAGE statement. */
3495 gfc_match_fail_image (void)
3497 if (!gfc_notify_std (GFC_STD_F2018
, "FAIL IMAGE statement at %C"))
3500 if (gfc_match_char ('(') == MATCH_YES
)
3503 new_st
.op
= EXEC_FAIL_IMAGE
;
3508 gfc_syntax_error (ST_FAIL_IMAGE
);
3513 /* Match a FORM TEAM statement. */
3516 gfc_match_form_team (void)
3519 gfc_expr
*teamid
,*team
;
3521 if (!gfc_notify_std (GFC_STD_F2018
, "FORM TEAM statement at %C"))
3524 if (gfc_match_char ('(') == MATCH_NO
)
3527 new_st
.op
= EXEC_FORM_TEAM
;
3529 if (gfc_match ("%e", &teamid
) != MATCH_YES
)
3531 m
= gfc_match_char (',');
3532 if (m
== MATCH_ERROR
)
3534 if (gfc_match ("%e", &team
) != MATCH_YES
)
3537 m
= gfc_match_char (')');
3541 new_st
.expr1
= teamid
;
3542 new_st
.expr2
= team
;
3547 gfc_syntax_error (ST_FORM_TEAM
);
3552 /* Match a CHANGE TEAM statement. */
3555 gfc_match_change_team (void)
3560 if (!gfc_notify_std (GFC_STD_F2018
, "CHANGE TEAM statement at %C"))
3563 if (gfc_match_char ('(') == MATCH_NO
)
3566 new_st
.op
= EXEC_CHANGE_TEAM
;
3568 if (gfc_match ("%e", &team
) != MATCH_YES
)
3571 m
= gfc_match_char (')');
3575 new_st
.expr1
= team
;
3580 gfc_syntax_error (ST_CHANGE_TEAM
);
3585 /* Match a END TEAM statement. */
3588 gfc_match_end_team (void)
3590 if (!gfc_notify_std (GFC_STD_F2018
, "END TEAM statement at %C"))
3593 if (gfc_match_char ('(') == MATCH_YES
)
3596 new_st
.op
= EXEC_END_TEAM
;
3601 gfc_syntax_error (ST_END_TEAM
);
3606 /* Match a SYNC TEAM statement. */
3609 gfc_match_sync_team (void)
3614 if (!gfc_notify_std (GFC_STD_F2018
, "SYNC TEAM statement at %C"))
3617 if (gfc_match_char ('(') == MATCH_NO
)
3620 new_st
.op
= EXEC_SYNC_TEAM
;
3622 if (gfc_match ("%e", &team
) != MATCH_YES
)
3625 m
= gfc_match_char (')');
3629 new_st
.expr1
= team
;
3634 gfc_syntax_error (ST_SYNC_TEAM
);
3639 /* Match LOCK/UNLOCK statement. Syntax:
3640 LOCK ( lock-variable [ , lock-stat-list ] )
3641 UNLOCK ( lock-variable [ , sync-stat-list ] )
3642 where lock-stat is ACQUIRED_LOCK or sync-stat
3643 and sync-stat is STAT= or ERRMSG=. */
3646 lock_unlock_statement (gfc_statement st
)
3649 gfc_expr
*tmp
, *lockvar
, *acq_lock
, *stat
, *errmsg
;
3650 bool saw_acq_lock
, saw_stat
, saw_errmsg
;
3652 tmp
= lockvar
= acq_lock
= stat
= errmsg
= NULL
;
3653 saw_acq_lock
= saw_stat
= saw_errmsg
= false;
3655 if (gfc_pure (NULL
))
3657 gfc_error ("Image control statement %s at %C in PURE procedure",
3658 st
== ST_LOCK
? "LOCK" : "UNLOCK");
3662 gfc_unset_implicit_pure (NULL
);
3664 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3666 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3670 if (gfc_find_state (COMP_CRITICAL
))
3672 gfc_error ("Image control statement %s at %C in CRITICAL block",
3673 st
== ST_LOCK
? "LOCK" : "UNLOCK");
3677 if (gfc_find_state (COMP_DO_CONCURRENT
))
3679 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3680 st
== ST_LOCK
? "LOCK" : "UNLOCK");
3684 if (gfc_match_char ('(') != MATCH_YES
)
3687 if (gfc_match ("%e", &lockvar
) != MATCH_YES
)
3689 m
= gfc_match_char (',');
3690 if (m
== MATCH_ERROR
)
3694 m
= gfc_match_char (')');
3702 m
= gfc_match (" stat = %v", &tmp
);
3703 if (m
== MATCH_ERROR
)
3709 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
3715 m
= gfc_match_char (',');
3723 m
= gfc_match (" errmsg = %v", &tmp
);
3724 if (m
== MATCH_ERROR
)
3730 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
3736 m
= gfc_match_char (',');
3744 m
= gfc_match (" acquired_lock = %v", &tmp
);
3745 if (m
== MATCH_ERROR
|| st
== ST_UNLOCK
)
3751 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L",
3756 saw_acq_lock
= true;
3758 m
= gfc_match_char (',');
3769 if (m
== MATCH_ERROR
)
3772 if (gfc_match (" )%t") != MATCH_YES
)
3779 new_st
.op
= EXEC_LOCK
;
3782 new_st
.op
= EXEC_UNLOCK
;
3788 new_st
.expr1
= lockvar
;
3789 new_st
.expr2
= stat
;
3790 new_st
.expr3
= errmsg
;
3791 new_st
.expr4
= acq_lock
;
3796 gfc_syntax_error (st
);
3799 if (acq_lock
!= tmp
)
3800 gfc_free_expr (acq_lock
);
3802 gfc_free_expr (errmsg
);
3804 gfc_free_expr (stat
);
3806 gfc_free_expr (tmp
);
3807 gfc_free_expr (lockvar
);
3814 gfc_match_lock (void)
3816 if (!gfc_notify_std (GFC_STD_F2008
, "LOCK statement at %C"))
3819 return lock_unlock_statement (ST_LOCK
);
3824 gfc_match_unlock (void)
3826 if (!gfc_notify_std (GFC_STD_F2008
, "UNLOCK statement at %C"))
3829 return lock_unlock_statement (ST_UNLOCK
);
3833 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3834 SYNC ALL [(sync-stat-list)]
3835 SYNC MEMORY [(sync-stat-list)]
3836 SYNC IMAGES (image-set [, sync-stat-list] )
3837 with sync-stat is int-expr or *. */
3840 sync_statement (gfc_statement st
)
3843 gfc_expr
*tmp
, *imageset
, *stat
, *errmsg
;
3844 bool saw_stat
, saw_errmsg
;
3846 tmp
= imageset
= stat
= errmsg
= NULL
;
3847 saw_stat
= saw_errmsg
= false;
3849 if (gfc_pure (NULL
))
3851 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3855 gfc_unset_implicit_pure (NULL
);
3857 if (!gfc_notify_std (GFC_STD_F2008
, "SYNC statement at %C"))
3860 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3862 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3867 if (gfc_find_state (COMP_CRITICAL
))
3869 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3873 if (gfc_find_state (COMP_DO_CONCURRENT
))
3875 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3879 if (gfc_match_eos () == MATCH_YES
)
3881 if (st
== ST_SYNC_IMAGES
)
3886 if (gfc_match_char ('(') != MATCH_YES
)
3889 if (st
== ST_SYNC_IMAGES
)
3891 /* Denote '*' as imageset == NULL. */
3892 m
= gfc_match_char ('*');
3893 if (m
== MATCH_ERROR
)
3897 if (gfc_match ("%e", &imageset
) != MATCH_YES
)
3900 m
= gfc_match_char (',');
3901 if (m
== MATCH_ERROR
)
3905 m
= gfc_match_char (')');
3914 m
= gfc_match (" stat = %e", &tmp
);
3915 if (m
== MATCH_ERROR
)
3921 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
3927 if (gfc_match_char (',') == MATCH_YES
)
3934 m
= gfc_match (" errmsg = %e", &tmp
);
3935 if (m
== MATCH_ERROR
)
3941 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
3947 if (gfc_match_char (',') == MATCH_YES
)
3957 if (gfc_match (" )%t") != MATCH_YES
)
3964 new_st
.op
= EXEC_SYNC_ALL
;
3966 case ST_SYNC_IMAGES
:
3967 new_st
.op
= EXEC_SYNC_IMAGES
;
3969 case ST_SYNC_MEMORY
:
3970 new_st
.op
= EXEC_SYNC_MEMORY
;
3976 new_st
.expr1
= imageset
;
3977 new_st
.expr2
= stat
;
3978 new_st
.expr3
= errmsg
;
3983 gfc_syntax_error (st
);
3987 gfc_free_expr (stat
);
3989 gfc_free_expr (errmsg
);
3991 gfc_free_expr (tmp
);
3992 gfc_free_expr (imageset
);
3998 /* Match SYNC ALL statement. */
4001 gfc_match_sync_all (void)
4003 return sync_statement (ST_SYNC_ALL
);
4007 /* Match SYNC IMAGES statement. */
4010 gfc_match_sync_images (void)
4012 return sync_statement (ST_SYNC_IMAGES
);
4016 /* Match SYNC MEMORY statement. */
4019 gfc_match_sync_memory (void)
4021 return sync_statement (ST_SYNC_MEMORY
);
4025 /* Match a CONTINUE statement. */
4028 gfc_match_continue (void)
4030 if (gfc_match_eos () != MATCH_YES
)
4032 gfc_syntax_error (ST_CONTINUE
);
4036 new_st
.op
= EXEC_CONTINUE
;
4041 /* Match the (deprecated) ASSIGN statement. */
4044 gfc_match_assign (void)
4047 gfc_st_label
*label
;
4049 if (gfc_match (" %l", &label
) == MATCH_YES
)
4051 if (!gfc_reference_st_label (label
, ST_LABEL_UNKNOWN
))
4053 if (gfc_match (" to %v%t", &expr
) == MATCH_YES
)
4055 if (!gfc_notify_std (GFC_STD_F95_DEL
, "ASSIGN statement at %C"))
4058 expr
->symtree
->n
.sym
->attr
.assign
= 1;
4060 new_st
.op
= EXEC_LABEL_ASSIGN
;
4061 new_st
.label1
= label
;
4062 new_st
.expr1
= expr
;
4070 /* Match the GO TO statement. As a computed GOTO statement is
4071 matched, it is transformed into an equivalent SELECT block. No
4072 tree is necessary, and the resulting jumps-to-jumps are
4073 specifically optimized away by the back end. */
4076 gfc_match_goto (void)
4078 gfc_code
*head
, *tail
;
4081 gfc_st_label
*label
;
4085 if (gfc_match (" %l%t", &label
) == MATCH_YES
)
4087 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
4090 new_st
.op
= EXEC_GOTO
;
4091 new_st
.label1
= label
;
4095 /* The assigned GO TO statement. */
4097 if (gfc_match_variable (&expr
, 0) == MATCH_YES
)
4099 if (!gfc_notify_std (GFC_STD_F95_DEL
, "Assigned GOTO statement at %C"))
4102 new_st
.op
= EXEC_GOTO
;
4103 new_st
.expr1
= expr
;
4105 if (gfc_match_eos () == MATCH_YES
)
4108 /* Match label list. */
4109 gfc_match_char (',');
4110 if (gfc_match_char ('(') != MATCH_YES
)
4112 gfc_syntax_error (ST_GOTO
);
4119 m
= gfc_match_st_label (&label
);
4123 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
4127 head
= tail
= gfc_get_code (EXEC_GOTO
);
4130 tail
->block
= gfc_get_code (EXEC_GOTO
);
4134 tail
->label1
= label
;
4136 while (gfc_match_char (',') == MATCH_YES
);
4138 if (gfc_match (" )%t") != MATCH_YES
)
4143 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4146 new_st
.block
= head
;
4151 /* Last chance is a computed GO TO statement. */
4152 if (gfc_match_char ('(') != MATCH_YES
)
4154 gfc_syntax_error (ST_GOTO
);
4163 m
= gfc_match_st_label (&label
);
4167 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
4171 head
= tail
= gfc_get_code (EXEC_SELECT
);
4174 tail
->block
= gfc_get_code (EXEC_SELECT
);
4178 cp
= gfc_get_case ();
4179 cp
->low
= cp
->high
= gfc_get_int_expr (gfc_default_integer_kind
,
4182 tail
->ext
.block
.case_list
= cp
;
4184 tail
->next
= gfc_get_code (EXEC_GOTO
);
4185 tail
->next
->label1
= label
;
4187 while (gfc_match_char (',') == MATCH_YES
);
4189 if (gfc_match_char (')') != MATCH_YES
)
4194 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4198 /* Get the rest of the statement. */
4199 gfc_match_char (',');
4201 if (gfc_match (" %e%t", &expr
) != MATCH_YES
)
4204 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Computed GOTO at %C"))
4207 /* At this point, a computed GOTO has been fully matched and an
4208 equivalent SELECT statement constructed. */
4210 new_st
.op
= EXEC_SELECT
;
4211 new_st
.expr1
= NULL
;
4213 /* Hack: For a "real" SELECT, the expression is in expr. We put
4214 it in expr2 so we can distinguish then and produce the correct
4216 new_st
.expr2
= expr
;
4217 new_st
.block
= head
;
4221 gfc_syntax_error (ST_GOTO
);
4223 gfc_free_statements (head
);
4228 /* Frees a list of gfc_alloc structures. */
4231 gfc_free_alloc_list (gfc_alloc
*p
)
4238 gfc_free_expr (p
->expr
);
4244 /* Match an ALLOCATE statement. */
4247 gfc_match_allocate (void)
4249 gfc_alloc
*head
, *tail
;
4250 gfc_expr
*stat
, *errmsg
, *tmp
, *source
, *mold
;
4254 locus old_locus
, deferred_locus
, assumed_locus
;
4255 bool saw_stat
, saw_errmsg
, saw_source
, saw_mold
, saw_deferred
, b1
, b2
, b3
;
4256 bool saw_unlimited
= false, saw_assumed
= false;
4259 stat
= errmsg
= source
= mold
= tmp
= NULL
;
4260 saw_stat
= saw_errmsg
= saw_source
= saw_mold
= saw_deferred
= false;
4262 if (gfc_match_char ('(') != MATCH_YES
)
4264 gfc_syntax_error (ST_ALLOCATE
);
4268 /* Match an optional type-spec. */
4269 old_locus
= gfc_current_locus
;
4270 m
= gfc_match_type_spec (&ts
);
4271 if (m
== MATCH_ERROR
)
4273 else if (m
== MATCH_NO
)
4275 char name
[GFC_MAX_SYMBOL_LEN
+ 3];
4277 if (gfc_match ("%n :: ", name
) == MATCH_YES
)
4279 gfc_error ("Error in type-spec at %L", &old_locus
);
4283 ts
.type
= BT_UNKNOWN
;
4287 /* Needed for the F2008:C631 check below. */
4288 assumed_locus
= gfc_current_locus
;
4290 if (gfc_match (" :: ") == MATCH_YES
)
4292 if (!gfc_notify_std (GFC_STD_F2003
, "typespec in ALLOCATE at %L",
4298 gfc_error ("Type-spec at %L cannot contain a deferred "
4299 "type parameter", &old_locus
);
4303 if (ts
.type
== BT_CHARACTER
)
4305 if (!ts
.u
.cl
->length
)
4308 ts
.u
.cl
->length_from_typespec
= true;
4311 if (type_param_spec_list
4312 && gfc_spec_list_type (type_param_spec_list
, NULL
)
4315 gfc_error ("The type parameter spec list in the type-spec at "
4316 "%L cannot contain DEFERRED parameters", &old_locus
);
4322 ts
.type
= BT_UNKNOWN
;
4323 gfc_current_locus
= old_locus
;
4330 head
= tail
= gfc_get_alloc ();
4333 tail
->next
= gfc_get_alloc ();
4337 m
= gfc_match_variable (&tail
->expr
, 0);
4340 if (m
== MATCH_ERROR
)
4343 if (tail
->expr
->expr_type
== EXPR_CONSTANT
)
4345 gfc_error ("Unexpected constant at %C");
4349 if (gfc_check_do_variable (tail
->expr
->symtree
))
4352 bool impure
= gfc_impure_variable (tail
->expr
->symtree
->n
.sym
);
4353 if (impure
&& gfc_pure (NULL
))
4355 gfc_error ("Bad allocate-object at %C for a PURE procedure");
4360 gfc_unset_implicit_pure (NULL
);
4362 /* F2008:C631 (R626) A type-param-value in a type-spec shall be an
4363 asterisk if and only if each allocate-object is a dummy argument
4364 for which the corresponding type parameter is assumed. */
4366 && (tail
->expr
->ts
.deferred
4367 || (tail
->expr
->ts
.u
.cl
&& tail
->expr
->ts
.u
.cl
->length
)
4368 || tail
->expr
->symtree
->n
.sym
->attr
.dummy
== 0))
4370 gfc_error ("Incompatible allocate-object at %C for CHARACTER "
4371 "type-spec at %L", &assumed_locus
);
4375 if (tail
->expr
->ts
.deferred
)
4377 saw_deferred
= true;
4378 deferred_locus
= tail
->expr
->where
;
4381 if (gfc_find_state (COMP_DO_CONCURRENT
)
4382 || gfc_find_state (COMP_CRITICAL
))
4385 bool coarray
= tail
->expr
->symtree
->n
.sym
->attr
.codimension
;
4386 for (ref
= tail
->expr
->ref
; ref
; ref
= ref
->next
)
4387 if (ref
->type
== REF_COMPONENT
)
4388 coarray
= ref
->u
.c
.component
->attr
.codimension
;
4390 if (coarray
&& gfc_find_state (COMP_DO_CONCURRENT
))
4392 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
4395 if (coarray
&& gfc_find_state (COMP_CRITICAL
))
4397 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
4402 /* Check for F08:C628. */
4403 sym
= tail
->expr
->symtree
->n
.sym
;
4404 b1
= !(tail
->expr
->ref
4405 && (tail
->expr
->ref
->type
== REF_COMPONENT
4406 || tail
->expr
->ref
->type
== REF_ARRAY
));
4407 if (sym
&& sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
4408 b2
= !(CLASS_DATA (sym
)->attr
.allocatable
4409 || CLASS_DATA (sym
)->attr
.class_pointer
);
4411 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
4412 || sym
->attr
.proc_pointer
);
4413 b3
= sym
&& sym
->ns
&& sym
->ns
->proc_name
4414 && (sym
->ns
->proc_name
->attr
.allocatable
4415 || sym
->ns
->proc_name
->attr
.pointer
4416 || sym
->ns
->proc_name
->attr
.proc_pointer
);
4417 if (b1
&& b2
&& !b3
)
4419 gfc_error ("Allocate-object at %L is neither a data pointer "
4420 "nor an allocatable variable", &tail
->expr
->where
);
4424 /* The ALLOCATE statement had an optional typespec. Check the
4426 if (ts
.type
!= BT_UNKNOWN
)
4428 /* Enforce F03:C624. */
4429 if (!gfc_type_compatible (&tail
->expr
->ts
, &ts
))
4431 gfc_error ("Type of entity at %L is type incompatible with "
4432 "typespec", &tail
->expr
->where
);
4436 /* Enforce F03:C627. */
4437 if (ts
.kind
!= tail
->expr
->ts
.kind
&& !UNLIMITED_POLY (tail
->expr
))
4439 gfc_error ("Kind type parameter for entity at %L differs from "
4440 "the kind type parameter of the typespec",
4441 &tail
->expr
->where
);
4446 if (tail
->expr
->ts
.type
== BT_DERIVED
)
4447 tail
->expr
->ts
.u
.derived
= gfc_use_derived (tail
->expr
->ts
.u
.derived
);
4449 if (type_param_spec_list
)
4450 tail
->expr
->param_list
= gfc_copy_actual_arglist (type_param_spec_list
);
4452 saw_unlimited
= saw_unlimited
| UNLIMITED_POLY (tail
->expr
);
4454 if (gfc_peek_ascii_char () == '(' && !sym
->attr
.dimension
)
4456 gfc_error ("Shape specification for allocatable scalar at %C");
4460 if (gfc_match_char (',') != MATCH_YES
)
4465 m
= gfc_match (" stat = %e", &tmp
);
4466 if (m
== MATCH_ERROR
)
4473 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
4481 if (stat
->expr_type
== EXPR_CONSTANT
)
4483 gfc_error ("STAT tag at %L cannot be a constant", &stat
->where
);
4487 if (gfc_check_do_variable (stat
->symtree
))
4490 if (gfc_match_char (',') == MATCH_YES
)
4491 goto alloc_opt_list
;
4494 m
= gfc_match (" errmsg = %e", &tmp
);
4495 if (m
== MATCH_ERROR
)
4499 if (!gfc_notify_std (GFC_STD_F2003
, "ERRMSG tag at %L", &tmp
->where
))
4505 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
4513 if (gfc_match_char (',') == MATCH_YES
)
4514 goto alloc_opt_list
;
4517 m
= gfc_match (" source = %e", &tmp
);
4518 if (m
== MATCH_ERROR
)
4522 if (!gfc_notify_std (GFC_STD_F2003
, "SOURCE tag at %L", &tmp
->where
))
4528 gfc_error ("Redundant SOURCE tag found at %L", &tmp
->where
);
4532 /* The next 2 conditionals check C631. */
4533 if (ts
.type
!= BT_UNKNOWN
)
4535 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4536 &tmp
->where
, &old_locus
);
4541 && !gfc_notify_std (GFC_STD_F2008
, "SOURCE tag at %L"
4542 " with more than a single allocate object",
4550 if (gfc_match_char (',') == MATCH_YES
)
4551 goto alloc_opt_list
;
4554 m
= gfc_match (" mold = %e", &tmp
);
4555 if (m
== MATCH_ERROR
)
4559 if (!gfc_notify_std (GFC_STD_F2008
, "MOLD tag at %L", &tmp
->where
))
4562 /* Check F08:C636. */
4565 gfc_error ("Redundant MOLD tag found at %L", &tmp
->where
);
4569 /* Check F08:C637. */
4570 if (ts
.type
!= BT_UNKNOWN
)
4572 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4573 &tmp
->where
, &old_locus
);
4582 if (gfc_match_char (',') == MATCH_YES
)
4583 goto alloc_opt_list
;
4586 gfc_gobble_whitespace ();
4588 if (gfc_peek_char () == ')')
4592 if (gfc_match (" )%t") != MATCH_YES
)
4595 /* Check F08:C637. */
4598 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4599 &mold
->where
, &source
->where
);
4603 /* Check F03:C623, */
4604 if (saw_deferred
&& ts
.type
== BT_UNKNOWN
&& !source
&& !mold
)
4606 gfc_error ("Allocate-object at %L with a deferred type parameter "
4607 "requires either a type-spec or SOURCE tag or a MOLD tag",
4612 /* Check F03:C625, */
4613 if (saw_unlimited
&& ts
.type
== BT_UNKNOWN
&& !source
&& !mold
)
4615 for (tail
= head
; tail
; tail
= tail
->next
)
4617 if (UNLIMITED_POLY (tail
->expr
))
4618 gfc_error ("Unlimited polymorphic allocate-object at %L "
4619 "requires either a type-spec or SOURCE tag "
4620 "or a MOLD tag", &tail
->expr
->where
);
4625 new_st
.op
= EXEC_ALLOCATE
;
4626 new_st
.expr1
= stat
;
4627 new_st
.expr2
= errmsg
;
4629 new_st
.expr3
= source
;
4631 new_st
.expr3
= mold
;
4632 new_st
.ext
.alloc
.list
= head
;
4633 new_st
.ext
.alloc
.ts
= ts
;
4635 if (type_param_spec_list
)
4636 gfc_free_actual_arglist (type_param_spec_list
);
4641 gfc_syntax_error (ST_ALLOCATE
);
4644 gfc_free_expr (errmsg
);
4645 gfc_free_expr (source
);
4646 gfc_free_expr (stat
);
4647 gfc_free_expr (mold
);
4648 if (tmp
&& tmp
->expr_type
) gfc_free_expr (tmp
);
4649 gfc_free_alloc_list (head
);
4650 if (type_param_spec_list
)
4651 gfc_free_actual_arglist (type_param_spec_list
);
4656 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4657 a set of pointer assignments to intrinsic NULL(). */
4660 gfc_match_nullify (void)
4668 if (gfc_match_char ('(') != MATCH_YES
)
4673 m
= gfc_match_variable (&p
, 0);
4674 if (m
== MATCH_ERROR
)
4679 if (gfc_check_do_variable (p
->symtree
))
4683 if (gfc_is_coindexed (p
))
4685 gfc_error ("Pointer object at %C shall not be coindexed");
4689 /* Check for valid array pointer object. Bounds remapping is not
4690 allowed with NULLIFY. */
4693 gfc_ref
*remap
= p
->ref
;
4694 for (; remap
; remap
= remap
->next
)
4695 if (!remap
->next
&& remap
->type
== REF_ARRAY
4696 && remap
->u
.ar
.type
!= AR_FULL
)
4700 gfc_error ("NULLIFY does not allow bounds remapping for "
4701 "pointer object at %C");
4706 /* build ' => NULL() '. */
4707 e
= gfc_get_null_expr (&gfc_current_locus
);
4709 /* Chain to list. */
4713 tail
->op
= EXEC_POINTER_ASSIGN
;
4717 tail
->next
= gfc_get_code (EXEC_POINTER_ASSIGN
);
4724 if (gfc_match (" )%t") == MATCH_YES
)
4726 if (gfc_match_char (',') != MATCH_YES
)
4733 gfc_syntax_error (ST_NULLIFY
);
4736 gfc_free_statements (new_st
.next
);
4738 gfc_free_expr (new_st
.expr1
);
4739 new_st
.expr1
= NULL
;
4740 gfc_free_expr (new_st
.expr2
);
4741 new_st
.expr2
= NULL
;
4746 /* Match a DEALLOCATE statement. */
4749 gfc_match_deallocate (void)
4751 gfc_alloc
*head
, *tail
;
4752 gfc_expr
*stat
, *errmsg
, *tmp
;
4755 bool saw_stat
, saw_errmsg
, b1
, b2
;
4758 stat
= errmsg
= tmp
= NULL
;
4759 saw_stat
= saw_errmsg
= false;
4761 if (gfc_match_char ('(') != MATCH_YES
)
4767 head
= tail
= gfc_get_alloc ();
4770 tail
->next
= gfc_get_alloc ();
4774 m
= gfc_match_variable (&tail
->expr
, 0);
4775 if (m
== MATCH_ERROR
)
4780 if (tail
->expr
->expr_type
== EXPR_CONSTANT
)
4782 gfc_error ("Unexpected constant at %C");
4786 if (gfc_check_do_variable (tail
->expr
->symtree
))
4789 sym
= tail
->expr
->symtree
->n
.sym
;
4791 bool impure
= gfc_impure_variable (sym
);
4792 if (impure
&& gfc_pure (NULL
))
4794 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4799 gfc_unset_implicit_pure (NULL
);
4801 if (gfc_is_coarray (tail
->expr
)
4802 && gfc_find_state (COMP_DO_CONCURRENT
))
4804 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4808 if (gfc_is_coarray (tail
->expr
)
4809 && gfc_find_state (COMP_CRITICAL
))
4811 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4815 /* FIXME: disable the checking on derived types. */
4816 b1
= !(tail
->expr
->ref
4817 && (tail
->expr
->ref
->type
== REF_COMPONENT
4818 || tail
->expr
->ref
->type
== REF_ARRAY
));
4819 if (sym
&& sym
->ts
.type
== BT_CLASS
)
4820 b2
= !(CLASS_DATA (sym
) && (CLASS_DATA (sym
)->attr
.allocatable
4821 || CLASS_DATA (sym
)->attr
.class_pointer
));
4823 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
4824 || sym
->attr
.proc_pointer
);
4827 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4828 "nor an allocatable variable");
4832 if (gfc_match_char (',') != MATCH_YES
)
4837 m
= gfc_match (" stat = %e", &tmp
);
4838 if (m
== MATCH_ERROR
)
4844 gfc_error ("Redundant STAT tag found at %L", &tmp
->where
);
4845 gfc_free_expr (tmp
);
4852 if (gfc_check_do_variable (stat
->symtree
))
4855 if (gfc_match_char (',') == MATCH_YES
)
4856 goto dealloc_opt_list
;
4859 m
= gfc_match (" errmsg = %e", &tmp
);
4860 if (m
== MATCH_ERROR
)
4864 if (!gfc_notify_std (GFC_STD_F2003
, "ERRMSG at %L", &tmp
->where
))
4869 gfc_error ("Redundant ERRMSG tag found at %L", &tmp
->where
);
4870 gfc_free_expr (tmp
);
4877 if (gfc_match_char (',') == MATCH_YES
)
4878 goto dealloc_opt_list
;
4881 gfc_gobble_whitespace ();
4883 if (gfc_peek_char () == ')')
4887 if (gfc_match (" )%t") != MATCH_YES
)
4890 new_st
.op
= EXEC_DEALLOCATE
;
4891 new_st
.expr1
= stat
;
4892 new_st
.expr2
= errmsg
;
4893 new_st
.ext
.alloc
.list
= head
;
4898 gfc_syntax_error (ST_DEALLOCATE
);
4901 gfc_free_expr (errmsg
);
4902 gfc_free_expr (stat
);
4903 gfc_free_alloc_list (head
);
4908 /* Match a RETURN statement. */
4911 gfc_match_return (void)
4915 gfc_compile_state s
;
4919 if (gfc_find_state (COMP_CRITICAL
))
4921 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4925 if (gfc_find_state (COMP_DO_CONCURRENT
))
4927 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4931 if (gfc_match_eos () == MATCH_YES
)
4934 if (!gfc_find_state (COMP_SUBROUTINE
))
4936 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4941 if (gfc_current_form
== FORM_FREE
)
4943 /* The following are valid, so we can't require a blank after the
4947 char c
= gfc_peek_ascii_char ();
4948 if (ISALPHA (c
) || ISDIGIT (c
))
4952 m
= gfc_match (" %e%t", &e
);
4955 if (m
== MATCH_ERROR
)
4958 gfc_syntax_error (ST_RETURN
);
4965 gfc_enclosing_unit (&s
);
4966 if (s
== COMP_PROGRAM
4967 && !gfc_notify_std (GFC_STD_GNU
, "RETURN statement in "
4968 "main program at %C"))
4971 new_st
.op
= EXEC_RETURN
;
4978 /* Match the call of a type-bound procedure, if CALL%var has already been
4979 matched and var found to be a derived-type variable. */
4982 match_typebound_call (gfc_symtree
* varst
)
4987 base
= gfc_get_expr ();
4988 base
->expr_type
= EXPR_VARIABLE
;
4989 base
->symtree
= varst
;
4990 base
->where
= gfc_current_locus
;
4991 gfc_set_sym_referenced (varst
->n
.sym
);
4993 m
= gfc_match_varspec (base
, 0, true, true);
4995 gfc_error ("Expected component reference at %C");
4998 gfc_free_expr (base
);
5002 if (gfc_match_eos () != MATCH_YES
)
5004 gfc_error ("Junk after CALL at %C");
5005 gfc_free_expr (base
);
5009 if (base
->expr_type
== EXPR_COMPCALL
)
5010 new_st
.op
= EXEC_COMPCALL
;
5011 else if (base
->expr_type
== EXPR_PPC
)
5012 new_st
.op
= EXEC_CALL_PPC
;
5015 gfc_error ("Expected type-bound procedure or procedure pointer component "
5017 gfc_free_expr (base
);
5020 new_st
.expr1
= base
;
5026 /* Match a CALL statement. The tricky part here are possible
5027 alternate return specifiers. We handle these by having all
5028 "subroutines" actually return an integer via a register that gives
5029 the return number. If the call specifies alternate returns, we
5030 generate code for a SELECT statement whose case clauses contain
5031 GOTOs to the various labels. */
5034 gfc_match_call (void)
5036 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5037 gfc_actual_arglist
*a
, *arglist
;
5047 m
= gfc_match ("% %n", name
);
5053 if (gfc_get_ha_sym_tree (name
, &st
))
5058 /* If this is a variable of derived-type, it probably starts a type-bound
5059 procedure call. Associate variable targets have to be resolved for the
5061 if (((sym
->attr
.flavor
!= FL_PROCEDURE
5062 || gfc_is_function_return_value (sym
, gfc_current_ns
))
5063 && (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
))
5065 (sym
->assoc
&& sym
->assoc
->target
5066 && gfc_resolve_expr (sym
->assoc
->target
)
5067 && (sym
->assoc
->target
->ts
.type
== BT_DERIVED
5068 || sym
->assoc
->target
->ts
.type
== BT_CLASS
)))
5069 return match_typebound_call (st
);
5071 /* If it does not seem to be callable (include functions so that the
5072 right association is made. They are thrown out in resolution.)
5074 if (!sym
->attr
.generic
5075 && !sym
->attr
.proc_pointer
5076 && !sym
->attr
.subroutine
5077 && !sym
->attr
.function
)
5079 if (!(sym
->attr
.external
&& !sym
->attr
.referenced
))
5081 /* ...create a symbol in this scope... */
5082 if (sym
->ns
!= gfc_current_ns
5083 && gfc_get_sym_tree (name
, NULL
, &st
, false) == 1)
5086 if (sym
!= st
->n
.sym
)
5090 /* ...and then to try to make the symbol into a subroutine. */
5091 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
5095 gfc_set_sym_referenced (sym
);
5097 if (gfc_match_eos () != MATCH_YES
)
5099 m
= gfc_match_actual_arglist (1, &arglist
);
5102 if (m
== MATCH_ERROR
)
5105 if (gfc_match_eos () != MATCH_YES
)
5109 /* Walk the argument list looking for invalid BOZ. */
5110 for (a
= arglist
; a
; a
= a
->next
)
5111 if (a
->expr
&& a
->expr
->ts
.type
== BT_BOZ
)
5113 gfc_error ("A BOZ literal constant at %L cannot appear as an actual "
5114 "argument in a subroutine reference", &a
->expr
->where
);
5119 /* If any alternate return labels were found, construct a SELECT
5120 statement that will jump to the right place. */
5123 for (a
= arglist
; a
; a
= a
->next
)
5124 if (a
->expr
== NULL
)
5132 gfc_symtree
*select_st
;
5133 gfc_symbol
*select_sym
;
5134 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5136 new_st
.next
= c
= gfc_get_code (EXEC_SELECT
);
5137 sprintf (name
, "_result_%s", sym
->name
);
5138 gfc_get_ha_sym_tree (name
, &select_st
); /* Can't fail. */
5140 select_sym
= select_st
->n
.sym
;
5141 select_sym
->ts
.type
= BT_INTEGER
;
5142 select_sym
->ts
.kind
= gfc_default_integer_kind
;
5143 gfc_set_sym_referenced (select_sym
);
5144 c
->expr1
= gfc_get_expr ();
5145 c
->expr1
->expr_type
= EXPR_VARIABLE
;
5146 c
->expr1
->symtree
= select_st
;
5147 c
->expr1
->ts
= select_sym
->ts
;
5148 c
->expr1
->where
= gfc_current_locus
;
5151 for (a
= arglist
; a
; a
= a
->next
)
5153 if (a
->expr
!= NULL
)
5156 if (!gfc_reference_st_label (a
->label
, ST_LABEL_TARGET
))
5161 c
->block
= gfc_get_code (EXEC_SELECT
);
5164 new_case
= gfc_get_case ();
5165 new_case
->high
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, i
);
5166 new_case
->low
= new_case
->high
;
5167 c
->ext
.block
.case_list
= new_case
;
5169 c
->next
= gfc_get_code (EXEC_GOTO
);
5170 c
->next
->label1
= a
->label
;
5174 new_st
.op
= EXEC_CALL
;
5175 new_st
.symtree
= st
;
5176 new_st
.ext
.actual
= arglist
;
5181 gfc_syntax_error (ST_CALL
);
5184 gfc_free_actual_arglist (arglist
);
5189 /* Given a name, return a pointer to the common head structure,
5190 creating it if it does not exist. If FROM_MODULE is nonzero, we
5191 mangle the name so that it doesn't interfere with commons defined
5192 in the using namespace.
5193 TODO: Add to global symbol tree. */
5196 gfc_get_common (const char *name
, int from_module
)
5199 static int serial
= 0;
5200 char mangled_name
[GFC_MAX_SYMBOL_LEN
+ 1];
5204 /* A use associated common block is only needed to correctly layout
5205 the variables it contains. */
5206 snprintf (mangled_name
, GFC_MAX_SYMBOL_LEN
, "_%d_%s", serial
++, name
);
5207 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, mangled_name
);
5211 st
= gfc_find_symtree (gfc_current_ns
->common_root
, name
);
5214 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, name
);
5217 if (st
->n
.common
== NULL
)
5219 st
->n
.common
= gfc_get_common_head ();
5220 st
->n
.common
->where
= gfc_current_locus
;
5221 strcpy (st
->n
.common
->name
, name
);
5224 return st
->n
.common
;
5228 /* Match a common block name. */
5231 gfc_match_common_name (char *name
)
5235 if (gfc_match_char ('/') == MATCH_NO
)
5241 if (gfc_match_char ('/') == MATCH_YES
)
5247 m
= gfc_match_name (name
);
5249 if (m
== MATCH_ERROR
)
5251 if (m
== MATCH_YES
&& gfc_match_char ('/') == MATCH_YES
)
5254 gfc_error ("Syntax error in common block name at %C");
5259 /* Match a COMMON statement. */
5262 gfc_match_common (void)
5264 gfc_symbol
*sym
, **head
, *tail
, *other
;
5265 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5272 /* COMMON has been matched. In free form source code, the next character
5273 needs to be whitespace or '/'. Check that here. Fixed form source
5274 code needs to be checked below. */
5275 c
= gfc_peek_ascii_char ();
5276 if (gfc_current_form
== FORM_FREE
&& !gfc_is_whitespace (c
) && c
!= '/')
5283 m
= gfc_match_common_name (name
);
5284 if (m
== MATCH_ERROR
)
5287 if (name
[0] == '\0')
5289 t
= &gfc_current_ns
->blank_common
;
5290 if (t
->head
== NULL
)
5291 t
->where
= gfc_current_locus
;
5295 t
= gfc_get_common (name
, 0);
5304 while (tail
->common_next
)
5305 tail
= tail
->common_next
;
5308 /* Grab the list of symbols. */
5311 m
= gfc_match_symbol (&sym
, 0);
5312 if (m
== MATCH_ERROR
)
5317 /* See if we know the current common block is bind(c), and if
5318 so, then see if we can check if the symbol is (which it'll
5319 need to be). This can happen if the bind(c) attr stmt was
5320 applied to the common block, and the variable(s) already
5321 defined, before declaring the common block. */
5322 if (t
->is_bind_c
== 1)
5324 if (sym
->ts
.type
!= BT_UNKNOWN
&& sym
->ts
.is_c_interop
!= 1)
5326 /* If we find an error, just print it and continue,
5327 cause it's just semantic, and we can see if there
5329 gfc_error_now ("Variable %qs at %L in common block %qs "
5330 "at %C must be declared with a C "
5331 "interoperable kind since common block "
5333 sym
->name
, &(sym
->declared_at
), t
->name
,
5337 if (sym
->attr
.is_bind_c
== 1)
5338 gfc_error_now ("Variable %qs in common block %qs at %C cannot "
5339 "be bind(c) since it is not global", sym
->name
,
5343 if (sym
->attr
.in_common
)
5345 gfc_error ("Symbol %qs at %C is already in a COMMON block",
5350 if (((sym
->value
!= NULL
&& sym
->value
->expr_type
!= EXPR_NULL
)
5351 || sym
->attr
.data
) && gfc_current_state () != COMP_BLOCK_DATA
)
5353 if (!gfc_notify_std (GFC_STD_GNU
, "Initialized symbol %qs at "
5354 "%C can only be COMMON in BLOCK DATA",
5359 /* F2018:R874: common-block-object is variable-name [ (array-spec) ]
5360 F2018:C8121: A variable-name shall not be a name made accessible
5361 by use association. */
5362 if (sym
->attr
.use_assoc
)
5364 gfc_error ("Symbol %qs at %C is USE associated from module %qs "
5365 "and cannot occur in COMMON", sym
->name
, sym
->module
);
5369 /* Deal with an optional array specification after the
5371 m
= gfc_match_array_spec (&as
, true, true);
5372 if (m
== MATCH_ERROR
)
5377 if (as
->type
!= AS_EXPLICIT
)
5379 gfc_error ("Array specification for symbol %qs in COMMON "
5380 "at %C must be explicit", sym
->name
);
5386 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5387 "coarray", sym
->name
);
5391 if (!gfc_add_dimension (&sym
->attr
, sym
->name
, NULL
))
5394 if (sym
->attr
.pointer
)
5396 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5397 "POINTER array", sym
->name
);
5406 /* Add the in_common attribute, but ignore the reported errors
5407 if any, and continue matching. */
5408 gfc_add_in_common (&sym
->attr
, sym
->name
, NULL
);
5410 sym
->common_block
= t
;
5411 sym
->common_block
->refs
++;
5414 tail
->common_next
= sym
;
5420 sym
->common_head
= t
;
5422 /* Check to see if the symbol is already in an equivalence group.
5423 If it is, set the other members as being in common. */
5424 if (sym
->attr
.in_equivalence
)
5426 for (e1
= gfc_current_ns
->equiv
; e1
; e1
= e1
->next
)
5428 for (e2
= e1
; e2
; e2
= e2
->eq
)
5429 if (e2
->expr
->symtree
->n
.sym
== sym
)
5436 for (e2
= e1
; e2
; e2
= e2
->eq
)
5438 other
= e2
->expr
->symtree
->n
.sym
;
5439 if (other
->common_head
5440 && other
->common_head
!= sym
->common_head
)
5442 gfc_error ("Symbol %qs, in COMMON block %qs at "
5443 "%C is being indirectly equivalenced to "
5444 "another COMMON block %qs",
5445 sym
->name
, sym
->common_head
->name
,
5446 other
->common_head
->name
);
5449 other
->attr
.in_common
= 1;
5450 other
->common_head
= t
;
5456 gfc_gobble_whitespace ();
5457 if (gfc_match_eos () == MATCH_YES
)
5459 c
= gfc_peek_ascii_char ();
5464 /* In Fixed form source code, gfortran can end up here for an
5465 expression of the form COMMONI = RHS. This may not be an
5466 error, so return MATCH_NO. */
5467 if (gfc_current_form
== FORM_FIXED
&& c
== '=')
5469 gfc_free_array_spec (as
);
5475 gfc_match_char (',');
5477 gfc_gobble_whitespace ();
5478 if (gfc_peek_ascii_char () == '/')
5487 gfc_syntax_error (ST_COMMON
);
5490 gfc_free_array_spec (as
);
5495 /* Match a BLOCK DATA program unit. */
5498 gfc_match_block_data (void)
5500 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5504 if (!gfc_notify_std (GFC_STD_F2018_OBS
, "BLOCK DATA construct at %L",
5505 &gfc_current_locus
))
5508 if (gfc_match_eos () == MATCH_YES
)
5510 gfc_new_block
= NULL
;
5514 m
= gfc_match ("% %n%t", name
);
5518 if (gfc_get_symbol (name
, NULL
, &sym
))
5521 if (!gfc_add_flavor (&sym
->attr
, FL_BLOCK_DATA
, sym
->name
, NULL
))
5524 gfc_new_block
= sym
;
5530 /* Free a namelist structure. */
5533 gfc_free_namelist (gfc_namelist
*name
)
5537 for (; name
; name
= n
)
5545 /* Free an OpenMP namelist structure. */
5548 gfc_free_omp_namelist (gfc_omp_namelist
*name
, bool free_ns
,
5549 bool free_align_allocator
,
5550 bool free_mem_traits_space
, bool free_init
)
5552 gfc_omp_namelist
*n
;
5553 gfc_expr
*last_allocator
= NULL
;
5554 char *last_init_attr
= NULL
;
5556 for (; name
; name
= n
)
5558 gfc_free_expr (name
->expr
);
5559 if (free_align_allocator
)
5560 gfc_free_expr (name
->u
.align
);
5561 else if (free_mem_traits_space
)
5562 { } /* name->u.memspace_sym: shall not call gfc_free_symbol here. */
5565 gfc_free_namespace (name
->u2
.ns
);
5566 else if (free_align_allocator
)
5568 if (last_allocator
!= name
->u2
.allocator
)
5570 last_allocator
= name
->u2
.allocator
;
5571 gfc_free_expr (name
->u2
.allocator
);
5574 else if (free_mem_traits_space
)
5575 { } /* name->u2.traits_sym: shall not call gfc_free_symbol here. */
5578 if (name
->u
.init
.attr
!= last_init_attr
)
5580 last_init_attr
= name
->u
.init
.attr
;
5581 free (name
->u
.init
.attr
);
5582 free (name
->u2
.init_interop_fr
);
5585 else if (name
->u2
.udr
)
5587 if (name
->u2
.udr
->combiner
)
5588 gfc_free_statement (name
->u2
.udr
->combiner
);
5589 if (name
->u2
.udr
->initializer
)
5590 gfc_free_statement (name
->u2
.udr
->initializer
);
5591 free (name
->u2
.udr
);
5599 /* Match a NAMELIST statement. */
5602 gfc_match_namelist (void)
5604 gfc_symbol
*group_name
, *sym
;
5608 m
= gfc_match (" / %s /", &group_name
);
5611 if (m
== MATCH_ERROR
)
5616 if (group_name
->ts
.type
!= BT_UNKNOWN
)
5618 gfc_error ("Namelist group name %qs at %C already has a basic "
5619 "type of %s", group_name
->name
,
5620 gfc_typename (&group_name
->ts
));
5624 /* A use associated name shall not be used as a namelist group name
5625 (e.g. F2003:C581). It is only supported as a legacy extension. */
5626 if (group_name
->attr
.flavor
== FL_NAMELIST
5627 && group_name
->attr
.use_assoc
5628 && !gfc_notify_std (GFC_STD_LEGACY
, "Namelist group name %qs "
5629 "at %C already is USE associated and can"
5630 "not be respecified.", group_name
->name
))
5633 if (group_name
->attr
.flavor
!= FL_NAMELIST
5634 && !gfc_add_flavor (&group_name
->attr
, FL_NAMELIST
,
5635 group_name
->name
, NULL
))
5640 m
= gfc_match_symbol (&sym
, 1);
5643 if (m
== MATCH_ERROR
)
5646 if (sym
->ts
.type
== BT_UNKNOWN
)
5648 if (gfc_current_ns
->seen_implicit_none
)
5650 /* It is required that members of a namelist be declared
5651 before the namelist. We check this by checking if the
5652 symbol has a defined type for IMPLICIT NONE. */
5653 gfc_error ("Symbol %qs in namelist %qs at %C must be "
5654 "declared before the namelist is declared.",
5655 sym
->name
, group_name
->name
);
5660 /* Before the symbol is given an implicit type, check to
5661 see if the symbol is already available in the namespace,
5662 possibly through host association. Importantly, the
5663 symbol may be a user defined type. */
5667 gfc_find_symbol (sym
->name
, NULL
, 1, &tmp
);
5668 if (tmp
&& tmp
->attr
.generic
5669 && (tmp
= gfc_find_dt_in_generic (tmp
)))
5671 if (tmp
->attr
.flavor
== FL_DERIVED
)
5673 gfc_error ("Derived type %qs at %L conflicts with "
5674 "namelist object %qs at %C",
5675 tmp
->name
, &tmp
->declared_at
, sym
->name
);
5680 /* Set type of the symbol to its implicit default type. It is
5681 not allowed to set it later to any other type. */
5682 gfc_set_default_type (sym
, 0, gfc_current_ns
);
5685 if (sym
->attr
.in_namelist
== 0
5686 && !gfc_add_in_namelist (&sym
->attr
, sym
->name
, NULL
))
5689 /* Use gfc_error_check here, rather than goto error, so that
5690 these are the only errors for the next two lines. */
5691 if (sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
)
5693 gfc_error ("Assumed size array %qs in namelist %qs at "
5694 "%C is not allowed", sym
->name
, group_name
->name
);
5698 nl
= gfc_get_namelist ();
5702 if (group_name
->namelist
== NULL
)
5703 group_name
->namelist
= group_name
->namelist_tail
= nl
;
5706 group_name
->namelist_tail
->next
= nl
;
5707 group_name
->namelist_tail
= nl
;
5710 if (gfc_match_eos () == MATCH_YES
)
5713 m
= gfc_match_char (',');
5715 if (gfc_match_char ('/') == MATCH_YES
)
5717 m2
= gfc_match (" %s /", &group_name
);
5718 if (m2
== MATCH_YES
)
5720 if (m2
== MATCH_ERROR
)
5734 gfc_syntax_error (ST_NAMELIST
);
5741 /* Match a MODULE statement. */
5744 gfc_match_module (void)
5748 m
= gfc_match (" %s%t", &gfc_new_block
);
5752 if (!gfc_add_flavor (&gfc_new_block
->attr
, FL_MODULE
,
5753 gfc_new_block
->name
, NULL
))
5760 /* Free equivalence sets and lists. Recursively is the easiest way to
5764 gfc_free_equiv_until (gfc_equiv
*eq
, gfc_equiv
*stop
)
5769 gfc_free_equiv (eq
->eq
);
5770 gfc_free_equiv_until (eq
->next
, stop
);
5771 gfc_free_expr (eq
->expr
);
5777 gfc_free_equiv (gfc_equiv
*eq
)
5779 gfc_free_equiv_until (eq
, NULL
);
5783 /* Match an EQUIVALENCE statement. */
5786 gfc_match_equivalence (void)
5788 gfc_equiv
*eq
, *set
, *tail
;
5792 gfc_common_head
*common_head
= NULL
;
5797 /* EQUIVALENCE has been matched. After gobbling any possible whitespace,
5798 the next character needs to be '('. Check that here, and return
5799 MATCH_NO for a variable of the form equivalence. */
5800 gfc_gobble_whitespace ();
5801 c
= gfc_peek_ascii_char ();
5809 eq
= gfc_get_equiv ();
5813 eq
->next
= gfc_current_ns
->equiv
;
5814 gfc_current_ns
->equiv
= eq
;
5816 if (gfc_match_char ('(') != MATCH_YES
)
5820 common_flag
= false;
5825 m
= gfc_match_equiv_variable (&set
->expr
);
5826 if (m
== MATCH_ERROR
)
5831 /* count the number of objects. */
5834 if (gfc_match_char ('%') == MATCH_YES
)
5836 gfc_error ("Derived type component %C is not a "
5837 "permitted EQUIVALENCE member");
5841 for (ref
= set
->expr
->ref
; ref
; ref
= ref
->next
)
5842 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
== AR_SECTION
)
5844 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5845 "be an array section");
5849 sym
= set
->expr
->symtree
->n
.sym
;
5851 if (!gfc_add_in_equivalence (&sym
->attr
, sym
->name
, NULL
))
5853 if (sym
->ts
.type
== BT_CLASS
5855 && !gfc_add_in_equivalence (&CLASS_DATA (sym
)->attr
,
5859 if (sym
->attr
.in_common
)
5862 common_head
= sym
->common_head
;
5865 if (gfc_match_char (')') == MATCH_YES
)
5868 if (gfc_match_char (',') != MATCH_YES
)
5871 set
->eq
= gfc_get_equiv ();
5877 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5881 /* If one of the members of an equivalence is in common, then
5882 mark them all as being in common. Before doing this, check
5883 that members of the equivalence group are not in different
5886 for (set
= eq
; set
; set
= set
->eq
)
5888 sym
= set
->expr
->symtree
->n
.sym
;
5889 if (sym
->common_head
&& sym
->common_head
!= common_head
)
5891 gfc_error ("Attempt to indirectly overlap COMMON "
5892 "blocks %s and %s by EQUIVALENCE at %C",
5893 sym
->common_head
->name
, common_head
->name
);
5896 sym
->attr
.in_common
= 1;
5897 sym
->common_head
= common_head
;
5900 if (gfc_match_eos () == MATCH_YES
)
5902 if (gfc_match_char (',') != MATCH_YES
)
5904 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5909 if (!gfc_notify_std (GFC_STD_F2018_OBS
, "EQUIVALENCE statement at %C"))
5915 gfc_syntax_error (ST_EQUIVALENCE
);
5921 gfc_free_equiv (gfc_current_ns
->equiv
);
5922 gfc_current_ns
->equiv
= eq
;
5928 /* Check that a statement function is not recursive. This is done by looking
5929 for the statement function symbol(sym) by looking recursively through its
5930 expression(e). If a reference to sym is found, true is returned.
5931 12.5.4 requires that any variable of function that is implicitly typed
5932 shall have that type confirmed by any subsequent type declaration. The
5933 implicit typing is conveniently done here. */
5935 recursive_stmt_fcn (gfc_expr
*, gfc_symbol
*);
5938 check_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
5944 switch (e
->expr_type
)
5947 if (e
->symtree
== NULL
)
5950 /* Check the name before testing for nested recursion! */
5951 if (sym
->name
== e
->symtree
->n
.sym
->name
)
5954 /* Catch recursion via other statement functions. */
5955 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
5956 && e
->symtree
->n
.sym
->value
5957 && recursive_stmt_fcn (e
->symtree
->n
.sym
->value
, sym
))
5960 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
5961 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
5966 if (e
->symtree
&& sym
->name
== e
->symtree
->n
.sym
->name
)
5969 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
5970 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
5982 recursive_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
)
5984 return gfc_traverse_expr (e
, sym
, check_stmt_fcn
, 0);
5988 /* Check for invalid uses of statement function dummy arguments in body. */
5991 chk_stmt_fcn_body (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
5993 gfc_formal_arglist
*formal
;
5995 if (e
== NULL
|| e
->symtree
== NULL
|| e
->expr_type
!= EXPR_FUNCTION
)
5998 for (formal
= sym
->formal
; formal
; formal
= formal
->next
)
6000 if (formal
->sym
== e
->symtree
->n
.sym
)
6002 gfc_error ("Invalid use of statement function argument at %L",
6012 /* Match a statement function declaration. It is so easy to match
6013 non-statement function statements with a MATCH_ERROR as opposed to
6014 MATCH_NO that we suppress error message in most cases. */
6017 gfc_match_st_function (void)
6019 gfc_error_buffer old_error
;
6023 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6026 gfc_formal_arglist
*ptr
;
6028 /* Read the possible statement function name, and then check to see if
6029 a symbol is already present in the namespace. Record if it is a
6030 function and whether it has been referenced. */
6033 old_locus
= gfc_current_locus
;
6034 m
= gfc_match_name (name
);
6037 gfc_find_symbol (name
, NULL
, 1, &sym
);
6038 if (sym
&& sym
->attr
.function
&& !sym
->attr
.referenced
)
6045 gfc_current_locus
= old_locus
;
6046 m
= gfc_match_symbol (&sym
, 0);
6050 gfc_push_error (&old_error
);
6052 if (!gfc_add_procedure (&sym
->attr
, PROC_ST_FUNCTION
, sym
->name
, NULL
))
6055 if (gfc_match_formal_arglist (sym
, 1, 0) != MATCH_YES
)
6058 m
= gfc_match (" = %e%t", &expr
);
6062 gfc_free_error (&old_error
);
6064 if (m
== MATCH_ERROR
)
6067 if (recursive_stmt_fcn (expr
, sym
))
6069 gfc_error ("Statement function at %L is recursive", &expr
->where
);
6073 if (fcn
&& ptr
!= sym
->formal
)
6075 gfc_error ("Statement function %qs at %L conflicts with function name",
6076 sym
->name
, &expr
->where
);
6080 if (gfc_traverse_expr (expr
, sym
, chk_stmt_fcn_body
, 0))
6085 if ((gfc_current_state () == COMP_FUNCTION
6086 || gfc_current_state () == COMP_SUBROUTINE
)
6087 && gfc_state_stack
->previous
->state
== COMP_INTERFACE
)
6089 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
6094 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Statement function at %C"))
6100 gfc_pop_error (&old_error
);
6105 /* Match an assignment to a pointer function (F2008). This could, in
6106 general be ambiguous with a statement function. In this implementation
6107 it remains so if it is the first statement after the specification
6111 gfc_match_ptr_fcn_assign (void)
6113 gfc_error_buffer old_error
;
6118 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6120 old_loc
= gfc_current_locus
;
6121 m
= gfc_match_name (name
);
6125 gfc_find_symbol (name
, NULL
, 1, &sym
);
6126 if (sym
&& sym
->attr
.flavor
!= FL_PROCEDURE
)
6129 gfc_push_error (&old_error
);
6131 if (sym
&& sym
->attr
.function
)
6132 goto match_actual_arglist
;
6134 gfc_current_locus
= old_loc
;
6135 m
= gfc_match_symbol (&sym
, 0);
6139 if (!gfc_add_procedure (&sym
->attr
, PROC_UNKNOWN
, sym
->name
, NULL
))
6142 match_actual_arglist
:
6143 gfc_current_locus
= old_loc
;
6144 m
= gfc_match (" %e", &expr
);
6148 new_st
.op
= EXEC_ASSIGN
;
6149 new_st
.expr1
= expr
;
6152 m
= gfc_match (" = %e%t", &expr
);
6156 new_st
.expr2
= expr
;
6160 gfc_pop_error (&old_error
);
6165 /***************** SELECT CASE subroutines ******************/
6167 /* Free a single case structure. */
6170 free_case (gfc_case
*p
)
6172 if (p
->low
== p
->high
)
6174 gfc_free_expr (p
->low
);
6175 gfc_free_expr (p
->high
);
6180 /* Free a list of case structures. */
6183 gfc_free_case_list (gfc_case
*p
)
6195 /* Match a single case selector. Combining the requirements of F08:C830
6196 and F08:C832 (R838) means that the case-value must have either CHARACTER,
6197 INTEGER, or LOGICAL type. */
6200 match_case_selector (gfc_case
**cp
)
6205 c
= gfc_get_case ();
6206 c
->where
= gfc_current_locus
;
6208 if (gfc_match_char (':') == MATCH_YES
)
6210 m
= gfc_match_init_expr (&c
->high
);
6213 if (m
== MATCH_ERROR
)
6216 if (c
->high
->ts
.type
!= BT_LOGICAL
&& c
->high
->ts
.type
!= BT_INTEGER
6217 && c
->high
->ts
.type
!= BT_CHARACTER
6219 || (flag_unsigned
&& c
->high
->ts
.type
!= BT_UNSIGNED
)))
6221 gfc_error ("Expression in CASE selector at %L cannot be %s",
6222 &c
->high
->where
, gfc_typename (&c
->high
->ts
));
6228 m
= gfc_match_init_expr (&c
->low
);
6229 if (m
== MATCH_ERROR
)
6234 if (c
->low
->ts
.type
!= BT_LOGICAL
&& c
->low
->ts
.type
!= BT_INTEGER
6235 && c
->low
->ts
.type
!= BT_CHARACTER
6237 || (flag_unsigned
&& c
->low
->ts
.type
!= BT_UNSIGNED
)))
6239 gfc_error ("Expression in CASE selector at %L cannot be %s",
6240 &c
->low
->where
, gfc_typename (&c
->low
->ts
));
6244 /* If we're not looking at a ':' now, make a range out of a single
6245 target. Else get the upper bound for the case range. */
6246 if (gfc_match_char (':') != MATCH_YES
)
6250 m
= gfc_match_init_expr (&c
->high
);
6251 if (m
== MATCH_ERROR
)
6254 && c
->high
->ts
.type
!= BT_LOGICAL
6255 && c
->high
->ts
.type
!= BT_INTEGER
6256 && c
->high
->ts
.type
!= BT_CHARACTER
6258 || (flag_unsigned
&& c
->high
->ts
.type
!= BT_UNSIGNED
)))
6260 gfc_error ("Expression in CASE selector at %L cannot be %s",
6261 &c
->high
->where
, gfc_typename (c
->high
));
6264 /* MATCH_NO is fine. It's OK if nothing is there! */
6268 if (c
->low
&& c
->low
->rank
!= 0)
6270 gfc_error ("Expression in CASE selector at %L must be scalar",
6274 if (c
->high
&& c
->high
->rank
!= 0)
6276 gfc_error ("Expression in CASE selector at %L must be scalar",
6285 gfc_error ("Expected initialization expression in CASE at %C");
6293 /* Match the end of a case statement. */
6296 match_case_eos (void)
6298 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6301 if (gfc_match_eos () == MATCH_YES
)
6304 /* If the case construct doesn't have a case-construct-name, we
6305 should have matched the EOS. */
6306 if (!gfc_current_block ())
6309 gfc_gobble_whitespace ();
6311 m
= gfc_match_name (name
);
6315 if (strcmp (name
, gfc_current_block ()->name
) != 0)
6317 gfc_error ("Expected block name %qs of SELECT construct at %C",
6318 gfc_current_block ()->name
);
6322 return gfc_match_eos ();
6326 /* Match a SELECT statement. */
6329 gfc_match_select (void)
6334 m
= gfc_match_label ();
6335 if (m
== MATCH_ERROR
)
6338 m
= gfc_match (" select case ( %e )%t", &expr
);
6342 new_st
.op
= EXEC_SELECT
;
6343 new_st
.expr1
= expr
;
6349 /* Transfer the selector typespec to the associate name. */
6352 copy_ts_from_selector_to_associate (gfc_expr
*associate
, gfc_expr
*selector
,
6353 bool select_type
= false)
6356 gfc_symbol
*assoc_sym
;
6357 int rank
= 0, corank
= 0;
6359 assoc_sym
= associate
->symtree
->n
.sym
;
6361 /* At this stage the expression rank and arrayspec dimensions have
6362 not been completely sorted out. We must get the expr2->rank
6363 right here, so that the correct class container is obtained. */
6364 ref
= selector
->ref
;
6365 while (ref
&& ref
->next
)
6368 if (selector
->ts
.type
== BT_CLASS
6369 && CLASS_DATA (selector
)
6370 && CLASS_DATA (selector
)->as
6371 && CLASS_DATA (selector
)->as
->type
== AS_ASSUMED_RANK
)
6373 assoc_sym
->attr
.dimension
= 1;
6374 assoc_sym
->as
= gfc_copy_array_spec (CLASS_DATA (selector
)->as
);
6375 corank
= assoc_sym
->as
->corank
;
6376 goto build_class_sym
;
6378 else if (selector
->ts
.type
== BT_CLASS
6379 && CLASS_DATA (selector
)
6380 && CLASS_DATA (selector
)->as
6381 && ((ref
&& ref
->type
== REF_ARRAY
)
6382 || selector
->expr_type
== EXPR_OP
))
6384 /* Ensure that the array reference type is set. We cannot use
6385 gfc_resolve_expr at this point, so the usable parts of
6386 resolve.cc(resolve_array_ref) are employed to do it. */
6387 if (ref
&& ref
->u
.ar
.type
== AR_UNKNOWN
)
6389 ref
->u
.ar
.type
= AR_ELEMENT
;
6390 for (int i
= 0; i
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; i
++)
6391 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_RANGE
6392 || ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
6393 || (ref
->u
.ar
.dimen_type
[i
] == DIMEN_UNKNOWN
6394 && ref
->u
.ar
.start
[i
] && ref
->u
.ar
.start
[i
]->rank
))
6396 ref
->u
.ar
.type
= AR_SECTION
;
6401 if (!ref
|| ref
->u
.ar
.type
== AR_FULL
)
6403 selector
->rank
= CLASS_DATA (selector
)->as
->rank
;
6404 selector
->corank
= CLASS_DATA (selector
)->as
->corank
;
6406 else if (ref
->u
.ar
.type
== AR_SECTION
)
6408 selector
->rank
= ref
->u
.ar
.dimen
;
6409 selector
->corank
= ref
->u
.ar
.codimen
;
6414 rank
= selector
->rank
;
6415 corank
= selector
->corank
;
6422 for (int i
= 0; i
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; i
++)
6423 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_ELEMENT
6424 || (ref
->u
.ar
.dimen_type
[i
] == DIMEN_UNKNOWN
6425 && ref
->u
.ar
.end
[i
] == NULL
6426 && ref
->u
.ar
.stride
[i
] == NULL
))
6432 assoc_sym
->attr
.dimension
= 1;
6433 assoc_sym
->as
= gfc_get_array_spec ();
6434 assoc_sym
->as
->rank
= rank
;
6435 assoc_sym
->as
->type
= AS_DEFERRED
;
6439 if (corank
!= 0 && rank
== 0)
6442 assoc_sym
->as
= gfc_get_array_spec ();
6443 assoc_sym
->as
->corank
= corank
;
6444 assoc_sym
->attr
.codimension
= 1;
6446 else if (corank
== 0 && rank
== 0 && assoc_sym
->as
)
6448 free (assoc_sym
->as
);
6449 assoc_sym
->as
= NULL
;
6452 /* Deal with the very specific case of a SELECT_TYPE selector being an
6453 associate_name whose type has been identified by component references.
6454 It must be assumed that it will be identified as a CLASS expression,
6455 so convert it now. */
6457 && IS_INFERRED_TYPE (selector
)
6458 && selector
->ts
.type
== BT_DERIVED
)
6460 gfc_find_derived_vtab (selector
->ts
.u
.derived
);
6461 /* The correct class container has to be available. */
6462 assoc_sym
->ts
.u
.derived
= selector
->ts
.u
.derived
;
6463 assoc_sym
->ts
.type
= BT_CLASS
;
6464 assoc_sym
->attr
.pointer
= 1;
6465 if (!selector
->ts
.u
.derived
->attr
.is_class
)
6466 gfc_build_class_symbol (&assoc_sym
->ts
, &assoc_sym
->attr
, &assoc_sym
->as
);
6467 associate
->ts
= assoc_sym
->ts
;
6469 else if (selector
->ts
.type
== BT_CLASS
)
6471 /* The correct class container has to be available. */
6472 assoc_sym
->ts
.type
= BT_CLASS
;
6473 assoc_sym
->ts
.u
.derived
= CLASS_DATA (selector
)
6474 ? CLASS_DATA (selector
)->ts
.u
.derived
6475 : selector
->ts
.u
.derived
;
6476 assoc_sym
->attr
.pointer
= 1;
6477 gfc_build_class_symbol (&assoc_sym
->ts
, &assoc_sym
->attr
, &assoc_sym
->as
);
6482 /* Build the associate name */
6484 build_associate_name (const char *name
, gfc_expr
**e1
, gfc_expr
**e2
)
6486 gfc_expr
*expr1
= *e1
;
6487 gfc_expr
*expr2
= *e2
;
6490 /* For the case where the associate name is already an associate name. */
6493 expr1
= gfc_get_expr ();
6494 expr1
->expr_type
= EXPR_VARIABLE
;
6495 expr1
->where
= expr2
->where
;
6496 if (gfc_get_sym_tree (name
, NULL
, &expr1
->symtree
, false))
6499 sym
= expr1
->symtree
->n
.sym
;
6500 if (expr2
->ts
.type
== BT_UNKNOWN
)
6501 sym
->attr
.untyped
= 1;
6503 copy_ts_from_selector_to_associate (expr1
, expr2
, true);
6505 sym
->attr
.flavor
= FL_VARIABLE
;
6506 sym
->attr
.referenced
= 1;
6507 sym
->attr
.class_ok
= 1;
6515 /* Push the current selector onto the SELECT TYPE stack. */
6518 select_type_push (gfc_symbol
*sel
)
6520 gfc_select_type_stack
*top
= gfc_get_select_type_stack ();
6521 top
->selector
= sel
;
6523 top
->prev
= select_type_stack
;
6525 select_type_stack
= top
;
6529 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
6531 static gfc_symtree
*
6532 select_intrinsic_set_tmp (gfc_typespec
*ts
)
6534 char name
[GFC_MAX_SYMBOL_LEN
];
6536 HOST_WIDE_INT charlen
= 0;
6537 gfc_symbol
*selector
= select_type_stack
->selector
;
6540 if (ts
->type
== BT_CLASS
|| ts
->type
== BT_DERIVED
)
6543 if (selector
->ts
.type
== BT_CLASS
&& !selector
->attr
.class_ok
)
6546 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6547 the values correspond to SELECT rank cases. */
6548 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
6549 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
6550 charlen
= gfc_mpz_get_hwi (ts
->u
.cl
->length
->value
.integer
);
6552 if (ts
->type
!= BT_CHARACTER
)
6553 sprintf (name
, "__tmp_%s_%d", gfc_basic_typename (ts
->type
),
6556 snprintf (name
, sizeof (name
),
6557 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC
"_%d",
6558 gfc_basic_typename (ts
->type
), charlen
, ts
->kind
);
6560 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
6562 gfc_add_type (sym
, ts
, NULL
);
6564 /* Copy across the array spec to the selector. */
6565 if (selector
->ts
.type
== BT_CLASS
6566 && (CLASS_DATA (selector
)->attr
.dimension
6567 || CLASS_DATA (selector
)->attr
.codimension
))
6569 sym
->attr
.pointer
= 1;
6570 sym
->attr
.dimension
= CLASS_DATA (selector
)->attr
.dimension
;
6571 sym
->attr
.codimension
= CLASS_DATA (selector
)->attr
.codimension
;
6572 sym
->as
= gfc_copy_array_spec (CLASS_DATA (selector
)->as
);
6575 gfc_set_sym_referenced (sym
);
6576 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, name
, NULL
);
6577 sym
->attr
.select_type_temporary
= 1;
6583 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
6586 select_type_set_tmp (gfc_typespec
*ts
)
6588 char name
[GFC_MAX_SYMBOL_LEN
+ 12 + 1];
6589 gfc_symtree
*tmp
= NULL
;
6590 gfc_symbol
*selector
= select_type_stack
->selector
;
6596 select_type_stack
->tmp
= NULL
;
6600 tmp
= select_intrinsic_set_tmp (ts
);
6607 if (ts
->type
== BT_CLASS
)
6608 sprintf (name
, "__tmp_class_%s", ts
->u
.derived
->name
);
6610 sprintf (name
, "__tmp_type_%s", ts
->u
.derived
->name
);
6612 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
6614 gfc_add_type (sym
, ts
, NULL
);
6616 /* If the SELECT TYPE selector is a function we might be able to obtain
6617 a typespec from the result. Since the function might not have been
6618 parsed yet we have to check that there is indeed a result symbol. */
6619 if (selector
->ts
.type
== BT_UNKNOWN
6620 && gfc_state_stack
->construct
6622 && (expr2
= gfc_state_stack
->construct
->expr2
)
6623 && expr2
->expr_type
== EXPR_FUNCTION
6625 && expr2
->symtree
->n
.sym
&& expr2
->symtree
->n
.sym
->result
)
6626 selector
->ts
= expr2
->symtree
->n
.sym
->result
->ts
;
6628 if (selector
->ts
.type
== BT_CLASS
6629 && selector
->attr
.class_ok
6630 && selector
->ts
.u
.derived
&& CLASS_DATA (selector
))
6633 = CLASS_DATA (selector
)->attr
.class_pointer
;
6635 /* Copy across the array spec to the selector. */
6636 if (CLASS_DATA (selector
)->attr
.dimension
6637 || CLASS_DATA (selector
)->attr
.codimension
)
6640 = CLASS_DATA (selector
)->attr
.dimension
;
6641 sym
->attr
.codimension
6642 = CLASS_DATA (selector
)->attr
.codimension
;
6643 if (CLASS_DATA (selector
)->as
->type
!= AS_EXPLICIT
)
6644 sym
->as
= gfc_copy_array_spec (CLASS_DATA (selector
)->as
);
6647 sym
->as
= gfc_get_array_spec();
6648 sym
->as
->rank
= CLASS_DATA (selector
)->as
->rank
;
6649 sym
->as
->type
= AS_DEFERRED
;
6654 gfc_set_sym_referenced (sym
);
6655 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, name
, NULL
);
6656 sym
->attr
.select_type_temporary
= 1;
6658 if (ts
->type
== BT_CLASS
)
6659 gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
6665 /* Add an association for it, so the rest of the parser knows it is
6666 an associate-name. The target will be set during resolution. */
6667 sym
->assoc
= gfc_get_association_list ();
6668 sym
->assoc
->dangling
= 1;
6669 sym
->assoc
->st
= tmp
;
6671 select_type_stack
->tmp
= tmp
;
6675 /* Match a SELECT TYPE statement. */
6678 gfc_match_select_type (void)
6680 gfc_expr
*expr1
, *expr2
= NULL
;
6682 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6684 gfc_namespace
*ns
= gfc_current_ns
;
6686 m
= gfc_match_label ();
6687 if (m
== MATCH_ERROR
)
6690 m
= gfc_match (" select type ( ");
6694 if (gfc_current_state() == COMP_MODULE
6695 || gfc_current_state() == COMP_SUBMODULE
)
6697 gfc_error ("SELECT TYPE at %C cannot appear in this scope");
6701 gfc_current_ns
= gfc_build_block_ns (ns
);
6702 m
= gfc_match (" %n => %e", name
, &expr2
);
6705 if (build_associate_name (name
, &expr1
, &expr2
))
6713 m
= gfc_match (" %e ", &expr1
);
6716 std::swap (ns
, gfc_current_ns
);
6717 gfc_free_namespace (ns
);
6722 m
= gfc_match (" )%t");
6725 gfc_error ("parse error in SELECT TYPE statement at %C");
6729 /* This ghastly expression seems to be needed to distinguish a CLASS
6730 array, which can have a reference, from other expressions that
6731 have references, such as derived type components, and are not
6732 allowed by the standard.
6733 TODO: see if it is sufficient to exclude component and substring
6735 class_array
= (expr1
->expr_type
== EXPR_VARIABLE
6736 && expr1
->ts
.type
== BT_CLASS
6737 && CLASS_DATA (expr1
)
6738 && (strcmp (CLASS_DATA (expr1
)->name
, "_data") == 0)
6739 && (CLASS_DATA (expr1
)->attr
.dimension
6740 || CLASS_DATA (expr1
)->attr
.codimension
)
6742 && expr1
->ref
->type
== REF_ARRAY
6743 && expr1
->ref
->u
.ar
.type
== AR_FULL
6744 && expr1
->ref
->next
== NULL
);
6746 /* Check for F03:C811 (F08:C835). */
6747 if (!expr2
&& (expr1
->expr_type
!= EXPR_VARIABLE
6748 || (!class_array
&& expr1
->ref
!= NULL
)))
6750 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
6751 "use associate-name=>");
6756 /* Prevent an existing associate name from reuse here by pushing expr1 to
6757 expr2 and building a new associate name. */
6758 if (!expr2
&& expr1
->symtree
->n
.sym
->assoc
6759 && !expr1
->symtree
->n
.sym
->attr
.select_type_temporary
6760 && !expr1
->symtree
->n
.sym
->attr
.select_rank_temporary
6761 && build_associate_name (expr1
->symtree
->n
.sym
->name
, &expr1
, &expr2
))
6767 /* Select type namespaces are not filled until resolution. Therefore, the
6768 namespace must be marked as having an inferred type associate name if
6769 either expr1 is an inferred type variable or expr2 is. In the latter
6770 case, as well as the symbol being marked as inferred type, it might be
6771 that it has not been detected to be so. In this case the target has
6772 unknown type. Once the namespace is marked, the fixups in resolution can
6775 && expr1
->symtree
->n
.sym
->assoc
6776 && expr1
->symtree
->n
.sym
->assoc
->inferred_type
)
6777 gfc_current_ns
->assoc_name_inferred
= 1;
6778 else if (expr2
&& expr2
->expr_type
== EXPR_VARIABLE
6779 && expr2
->symtree
->n
.sym
->assoc
)
6781 if (expr2
->symtree
->n
.sym
->assoc
->inferred_type
)
6782 gfc_current_ns
->assoc_name_inferred
= 1;
6783 else if (expr2
->symtree
->n
.sym
->assoc
->target
6784 && expr2
->symtree
->n
.sym
->assoc
->target
->ts
.type
== BT_UNKNOWN
)
6785 gfc_current_ns
->assoc_name_inferred
= 1;
6788 new_st
.op
= EXEC_SELECT_TYPE
;
6789 new_st
.expr1
= expr1
;
6790 new_st
.expr2
= expr2
;
6791 new_st
.ext
.block
.ns
= gfc_current_ns
;
6793 select_type_push (expr1
->symtree
->n
.sym
);
6794 gfc_current_ns
= ns
;
6799 gfc_free_expr (expr1
);
6800 gfc_free_expr (expr2
);
6801 gfc_undo_symbols ();
6802 std::swap (ns
, gfc_current_ns
);
6803 gfc_free_namespace (ns
);
6808 /* Set the temporary for the current intrinsic SELECT RANK selector. */
6811 select_rank_set_tmp (gfc_typespec
*ts
, int *case_value
)
6813 char name
[2 * GFC_MAX_SYMBOL_LEN
];
6814 char tname
[GFC_MAX_SYMBOL_LEN
+ 7];
6816 gfc_symbol
*selector
= select_type_stack
->selector
;
6819 HOST_WIDE_INT charlen
= 0;
6821 if (case_value
== NULL
)
6824 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
6825 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
6826 charlen
= gfc_mpz_get_hwi (ts
->u
.cl
->length
->value
.integer
);
6828 if (ts
->type
== BT_CLASS
)
6829 sprintf (tname
, "class_%s", ts
->u
.derived
->name
);
6830 else if (ts
->type
== BT_DERIVED
)
6831 sprintf (tname
, "type_%s", ts
->u
.derived
->name
);
6832 else if (ts
->type
!= BT_CHARACTER
)
6833 sprintf (tname
, "%s_%d", gfc_basic_typename (ts
->type
), ts
->kind
);
6835 sprintf (tname
, "%s_" HOST_WIDE_INT_PRINT_DEC
"_%d",
6836 gfc_basic_typename (ts
->type
), charlen
, ts
->kind
);
6838 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6839 the values correspond to SELECT rank cases. */
6840 if (*case_value
>=0)
6841 sprintf (name
, "__tmp_%s_rank_%d", tname
, *case_value
);
6843 sprintf (name
, "__tmp_%s_rank_m%d", tname
, -*case_value
);
6845 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
6849 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
6851 gfc_add_type (sym
, ts
, NULL
);
6853 /* Copy across the array spec to the selector. */
6854 if (selector
->ts
.type
== BT_CLASS
)
6856 sym
->ts
.u
.derived
= CLASS_DATA (selector
)->ts
.u
.derived
;
6857 sym
->attr
.pointer
= CLASS_DATA (selector
)->attr
.pointer
;
6858 sym
->attr
.allocatable
= CLASS_DATA (selector
)->attr
.allocatable
;
6859 sym
->attr
.target
= CLASS_DATA (selector
)->attr
.target
;
6860 sym
->attr
.class_ok
= 0;
6861 if (case_value
&& *case_value
!= 0)
6863 sym
->attr
.dimension
= 1;
6864 sym
->as
= gfc_copy_array_spec (CLASS_DATA (selector
)->as
);
6865 if (*case_value
> 0)
6867 sym
->as
->type
= AS_DEFERRED
;
6868 sym
->as
->rank
= *case_value
;
6870 else if (*case_value
== -1)
6872 sym
->as
->type
= AS_ASSUMED_SIZE
;
6879 sym
->attr
.pointer
= selector
->attr
.pointer
;
6880 sym
->attr
.allocatable
= selector
->attr
.allocatable
;
6881 sym
->attr
.target
= selector
->attr
.target
;
6882 if (case_value
&& *case_value
!= 0)
6884 sym
->attr
.dimension
= 1;
6885 sym
->as
= gfc_copy_array_spec (selector
->as
);
6886 if (*case_value
> 0)
6888 sym
->as
->type
= AS_DEFERRED
;
6889 sym
->as
->rank
= *case_value
;
6891 else if (*case_value
== -1)
6893 sym
->as
->type
= AS_ASSUMED_SIZE
;
6899 gfc_set_sym_referenced (sym
);
6900 gfc_add_flavor (&sym
->attr
, FL_VARIABLE
, name
, NULL
);
6901 sym
->attr
.select_type_temporary
= 1;
6903 sym
->attr
.select_rank_temporary
= 1;
6905 if (ts
->type
== BT_CLASS
)
6906 gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
6908 /* Add an association for it, so the rest of the parser knows it is
6909 an associate-name. The target will be set during resolution. */
6910 sym
->assoc
= gfc_get_association_list ();
6911 sym
->assoc
->dangling
= 1;
6912 sym
->assoc
->st
= tmp
;
6914 select_type_stack
->tmp
= tmp
;
6918 /* Match a SELECT RANK statement. */
6921 gfc_match_select_rank (void)
6923 gfc_expr
*expr1
, *expr2
= NULL
;
6925 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6926 gfc_symbol
*sym
, *sym2
;
6927 gfc_namespace
*ns
= gfc_current_ns
;
6928 gfc_array_spec
*as
= NULL
;
6930 m
= gfc_match_label ();
6931 if (m
== MATCH_ERROR
)
6934 m
= gfc_match (" select% rank ( ");
6938 if (!gfc_notify_std (GFC_STD_F2018
, "SELECT RANK statement at %C"))
6941 gfc_current_ns
= gfc_build_block_ns (ns
);
6942 m
= gfc_match (" %n => %e", name
, &expr2
);
6946 /* If expr2 corresponds to an implicitly typed variable, then the
6947 actual type of the variable may not have been set. Set it here. */
6948 if (!gfc_current_ns
->seen_implicit_none
6949 && expr2
->expr_type
== EXPR_VARIABLE
6950 && expr2
->ts
.type
== BT_UNKNOWN
6951 && expr2
->symtree
&& expr2
->symtree
->n
.sym
)
6953 gfc_set_default_type (expr2
->symtree
->n
.sym
, 0, gfc_current_ns
);
6954 expr2
->ts
.type
= expr2
->symtree
->n
.sym
->ts
.type
;
6957 expr1
= gfc_get_expr ();
6958 expr1
->expr_type
= EXPR_VARIABLE
;
6959 expr1
->where
= expr2
->where
;
6960 expr1
->ref
= gfc_copy_ref (expr2
->ref
);
6961 if (gfc_get_sym_tree (name
, NULL
, &expr1
->symtree
, false))
6967 sym
= expr1
->symtree
->n
.sym
;
6971 sym2
= expr2
->symtree
->n
.sym
;
6972 as
= (sym2
->ts
.type
== BT_CLASS
6973 && CLASS_DATA (sym2
)) ? CLASS_DATA (sym2
)->as
: sym2
->as
;
6976 if (expr2
->expr_type
!= EXPR_VARIABLE
6977 || !(as
&& as
->type
== AS_ASSUMED_RANK
))
6979 gfc_error ("The SELECT RANK selector at %C must be an assumed "
6985 if (expr2
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym2
))
6987 copy_ts_from_selector_to_associate (expr1
, expr2
);
6989 sym
->attr
.flavor
= FL_VARIABLE
;
6990 sym
->attr
.referenced
= 1;
6991 sym
->attr
.class_ok
= 1;
6992 CLASS_DATA (sym
)->attr
.allocatable
= CLASS_DATA (sym2
)->attr
.allocatable
;
6993 CLASS_DATA (sym
)->attr
.pointer
= CLASS_DATA (sym2
)->attr
.pointer
;
6994 CLASS_DATA (sym
)->attr
.target
= CLASS_DATA (sym2
)->attr
.target
;
6995 sym
->attr
.pointer
= 1;
7000 sym
->as
= gfc_copy_array_spec (sym2
->as
);
7001 sym
->attr
.dimension
= 1;
7003 sym
->attr
.flavor
= FL_VARIABLE
;
7004 sym
->attr
.referenced
= 1;
7005 sym
->attr
.class_ok
= sym2
->attr
.class_ok
;
7006 sym
->attr
.allocatable
= sym2
->attr
.allocatable
;
7007 sym
->attr
.pointer
= sym2
->attr
.pointer
;
7008 sym
->attr
.target
= sym2
->attr
.target
;
7013 m
= gfc_match (" %e ", &expr1
);
7017 gfc_undo_symbols ();
7018 std::swap (ns
, gfc_current_ns
);
7019 gfc_free_namespace (ns
);
7025 sym
= expr1
->symtree
->n
.sym
;
7026 as
= (sym
->ts
.type
== BT_CLASS
7027 && CLASS_DATA (sym
)) ? CLASS_DATA (sym
)->as
: sym
->as
;
7030 if (expr1
->expr_type
!= EXPR_VARIABLE
7031 || !(as
&& as
->type
== AS_ASSUMED_RANK
))
7033 gfc_error("The SELECT RANK selector at %C must be an assumed "
7040 m
= gfc_match (" )%t");
7043 gfc_error ("parse error in SELECT RANK statement at %C");
7047 new_st
.op
= EXEC_SELECT_RANK
;
7048 new_st
.expr1
= expr1
;
7049 new_st
.expr2
= expr2
;
7050 new_st
.ext
.block
.ns
= gfc_current_ns
;
7052 select_type_push (expr1
->symtree
->n
.sym
);
7053 gfc_current_ns
= ns
;
7058 gfc_free_expr (expr1
);
7059 gfc_free_expr (expr2
);
7060 gfc_undo_symbols ();
7061 std::swap (ns
, gfc_current_ns
);
7062 gfc_free_namespace (ns
);
7067 /* Match a CASE statement. */
7070 gfc_match_case (void)
7072 gfc_case
*c
, *head
, *tail
;
7077 if (gfc_current_state () != COMP_SELECT
)
7079 gfc_error ("Unexpected CASE statement at %C");
7083 if (gfc_match ("% default") == MATCH_YES
)
7085 m
= match_case_eos ();
7088 if (m
== MATCH_ERROR
)
7091 new_st
.op
= EXEC_SELECT
;
7092 c
= gfc_get_case ();
7093 c
->where
= gfc_current_locus
;
7094 new_st
.ext
.block
.case_list
= c
;
7098 if (gfc_match_char ('(') != MATCH_YES
)
7103 if (match_case_selector (&c
) == MATCH_ERROR
)
7113 if (gfc_match_char (')') == MATCH_YES
)
7115 if (gfc_match_char (',') != MATCH_YES
)
7119 m
= match_case_eos ();
7122 if (m
== MATCH_ERROR
)
7125 new_st
.op
= EXEC_SELECT
;
7126 new_st
.ext
.block
.case_list
= head
;
7131 gfc_error ("Syntax error in CASE specification at %C");
7134 gfc_free_case_list (head
); /* new_st is cleaned up in parse.cc. */
7139 /* Match a TYPE IS statement. */
7142 gfc_match_type_is (void)
7147 if (gfc_current_state () != COMP_SELECT_TYPE
)
7149 gfc_error ("Unexpected TYPE IS statement at %C");
7153 if (gfc_match_char ('(') != MATCH_YES
)
7156 c
= gfc_get_case ();
7157 c
->where
= gfc_current_locus
;
7159 m
= gfc_match_type_spec (&c
->ts
);
7162 if (m
== MATCH_ERROR
)
7165 if (gfc_match_char (')') != MATCH_YES
)
7168 m
= match_case_eos ();
7171 if (m
== MATCH_ERROR
)
7174 new_st
.op
= EXEC_SELECT_TYPE
;
7175 new_st
.ext
.block
.case_list
= c
;
7177 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
7178 && (c
->ts
.u
.derived
->attr
.sequence
7179 || c
->ts
.u
.derived
->attr
.is_bind_c
))
7181 gfc_error ("The type-spec shall not specify a sequence derived "
7182 "type or a type with the BIND attribute in SELECT "
7183 "TYPE at %C [F2003:C815]");
7187 if (c
->ts
.type
== BT_DERIVED
7188 && c
->ts
.u
.derived
&& c
->ts
.u
.derived
->attr
.pdt_type
7189 && gfc_spec_list_type (type_param_spec_list
, c
->ts
.u
.derived
)
7192 gfc_error ("All the LEN type parameters in the TYPE IS statement "
7193 "at %C must be ASSUMED");
7197 /* Create temporary variable. */
7198 select_type_set_tmp (&c
->ts
);
7203 gfc_error ("Syntax error in TYPE IS specification at %C");
7207 gfc_free_case_list (c
); /* new_st is cleaned up in parse.cc. */
7212 /* Match a CLASS IS or CLASS DEFAULT statement. */
7215 gfc_match_class_is (void)
7220 if (gfc_current_state () != COMP_SELECT_TYPE
)
7223 if (gfc_match ("% default") == MATCH_YES
)
7225 m
= match_case_eos ();
7228 if (m
== MATCH_ERROR
)
7231 new_st
.op
= EXEC_SELECT_TYPE
;
7232 c
= gfc_get_case ();
7233 c
->where
= gfc_current_locus
;
7234 c
->ts
.type
= BT_UNKNOWN
;
7235 new_st
.ext
.block
.case_list
= c
;
7236 select_type_set_tmp (NULL
);
7240 m
= gfc_match ("% is");
7243 if (m
== MATCH_ERROR
)
7246 if (gfc_match_char ('(') != MATCH_YES
)
7249 c
= gfc_get_case ();
7250 c
->where
= gfc_current_locus
;
7252 m
= match_derived_type_spec (&c
->ts
);
7255 if (m
== MATCH_ERROR
)
7258 if (c
->ts
.type
== BT_DERIVED
)
7259 c
->ts
.type
= BT_CLASS
;
7261 if (gfc_match_char (')') != MATCH_YES
)
7264 m
= match_case_eos ();
7267 if (m
== MATCH_ERROR
)
7270 new_st
.op
= EXEC_SELECT_TYPE
;
7271 new_st
.ext
.block
.case_list
= c
;
7273 /* Create temporary variable. */
7274 select_type_set_tmp (&c
->ts
);
7279 gfc_error ("Syntax error in CLASS IS specification at %C");
7283 gfc_free_case_list (c
); /* new_st is cleaned up in parse.cc. */
7288 /* Match a RANK statement. */
7291 gfc_match_rank_is (void)
7297 if (gfc_current_state () != COMP_SELECT_RANK
)
7299 gfc_error ("Unexpected RANK statement at %C");
7303 if (gfc_match ("% default") == MATCH_YES
)
7305 m
= match_case_eos ();
7308 if (m
== MATCH_ERROR
)
7311 new_st
.op
= EXEC_SELECT_RANK
;
7312 c
= gfc_get_case ();
7313 c
->ts
.type
= BT_UNKNOWN
;
7314 c
->where
= gfc_current_locus
;
7315 new_st
.ext
.block
.case_list
= c
;
7316 select_type_stack
->tmp
= NULL
;
7320 if (gfc_match_char ('(') != MATCH_YES
)
7323 c
= gfc_get_case ();
7324 c
->where
= gfc_current_locus
;
7325 c
->ts
= select_type_stack
->selector
->ts
;
7327 m
= gfc_match_expr (&c
->low
);
7330 if (gfc_match_char ('*') == MATCH_YES
)
7331 c
->low
= gfc_get_int_expr (gfc_default_integer_kind
,
7338 else if (m
== MATCH_YES
)
7341 if (c
->low
->expr_type
!= EXPR_CONSTANT
7342 || c
->low
->ts
.type
!= BT_INTEGER
7345 gfc_error ("The SELECT RANK CASE expression at %C must be a "
7346 "scalar, integer constant");
7350 case_value
= (int) mpz_get_si (c
->low
->value
.integer
);
7352 if ((case_value
< 0) || (case_value
> GFC_MAX_DIMENSIONS
))
7354 gfc_error ("The value of the SELECT RANK CASE expression at "
7355 "%C must not be less than zero or greater than %d",
7356 GFC_MAX_DIMENSIONS
);
7363 if (gfc_match_char (')') != MATCH_YES
)
7366 m
= match_case_eos ();
7369 if (m
== MATCH_ERROR
)
7372 new_st
.op
= EXEC_SELECT_RANK
;
7373 new_st
.ext
.block
.case_list
= c
;
7375 /* Create temporary variable. Recycle the select type code. */
7376 select_rank_set_tmp (&c
->ts
, &case_value
);
7381 gfc_error ("Syntax error in RANK specification at %C");
7385 gfc_free_case_list (c
); /* new_st is cleaned up in parse.cc. */
7389 /********************* WHERE subroutines ********************/
7391 /* Match the rest of a simple WHERE statement that follows an IF statement.
7395 match_simple_where (void)
7401 m
= gfc_match (" ( %e )", &expr
);
7405 m
= gfc_match_assignment ();
7408 if (m
== MATCH_ERROR
)
7411 if (gfc_match_eos () != MATCH_YES
)
7414 c
= gfc_get_code (EXEC_WHERE
);
7417 c
->next
= XCNEW (gfc_code
);
7419 c
->next
->loc
= gfc_current_locus
;
7420 gfc_clear_new_st ();
7422 new_st
.op
= EXEC_WHERE
;
7428 gfc_syntax_error (ST_WHERE
);
7431 gfc_free_expr (expr
);
7436 /* Match a WHERE statement. */
7439 gfc_match_where (gfc_statement
*st
)
7445 m0
= gfc_match_label ();
7446 if (m0
== MATCH_ERROR
)
7449 m
= gfc_match (" where ( %e )", &expr
);
7453 if (gfc_match_eos () == MATCH_YES
)
7455 *st
= ST_WHERE_BLOCK
;
7456 new_st
.op
= EXEC_WHERE
;
7457 new_st
.expr1
= expr
;
7461 m
= gfc_match_assignment ();
7463 gfc_syntax_error (ST_WHERE
);
7467 gfc_free_expr (expr
);
7471 /* We've got a simple WHERE statement. */
7473 c
= gfc_get_code (EXEC_WHERE
);
7476 /* Put in the assignment. It will not be processed by add_statement, so we
7477 need to copy the location here. */
7479 c
->next
= XCNEW (gfc_code
);
7481 c
->next
->loc
= gfc_current_locus
;
7482 gfc_clear_new_st ();
7484 new_st
.op
= EXEC_WHERE
;
7491 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
7492 new_st if successful. */
7495 gfc_match_elsewhere (void)
7497 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7501 if (gfc_current_state () != COMP_WHERE
)
7503 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
7509 if (gfc_match_char ('(') == MATCH_YES
)
7511 m
= gfc_match_expr (&expr
);
7514 if (m
== MATCH_ERROR
)
7517 if (gfc_match_char (')') != MATCH_YES
)
7521 if (gfc_match_eos () != MATCH_YES
)
7523 /* Only makes sense if we have a where-construct-name. */
7524 if (!gfc_current_block ())
7529 /* Better be a name at this point. */
7530 m
= gfc_match_name (name
);
7533 if (m
== MATCH_ERROR
)
7536 if (gfc_match_eos () != MATCH_YES
)
7539 if (strcmp (name
, gfc_current_block ()->name
) != 0)
7541 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
7542 name
, gfc_current_block ()->name
);
7547 new_st
.op
= EXEC_WHERE
;
7548 new_st
.expr1
= expr
;
7552 gfc_syntax_error (ST_ELSEWHERE
);
7555 gfc_free_expr (expr
);