1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2016 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 /* For debugging and diagnostic purposes. Return the textual representation
37 of the intrinsic operator OP. */
39 gfc_op2string (gfc_intrinsic_op op
)
47 case INTRINSIC_UMINUS
:
53 case INTRINSIC_CONCAT
:
57 case INTRINSIC_DIVIDE
:
96 case INTRINSIC_ASSIGN
:
99 case INTRINSIC_PARENTHESES
:
106 case INTRINSIC_FORMATTED
:
108 case INTRINSIC_UNFORMATTED
:
109 return "unformatted";
115 gfc_internal_error ("gfc_op2string(): Bad code");
120 /******************** Generic matching subroutines ************************/
122 /* Matches a member separator. With standard FORTRAN this is '%', but with
123 DEC structures we must carefully match dot ('.').
124 Because operators are spelled ".op.", a dotted string such as "x.y.z..."
125 can be either a component reference chain or a combination of binary
127 There is no real way to win because the string may be grammatically
128 ambiguous. The following rules help avoid ambiguities - they match
129 some behavior of other (older) compilers. If the rules here are changed
130 the test cases should be updated. If the user has problems with these rules
131 they probably deserve the consequences. Consider "x.y.z":
132 (1) If any user defined operator ".y." exists, this is always y(x,z)
133 (even if ".y." is the wrong type and/or x has a member y).
134 (2) Otherwise if x has a member y, and y is itself a derived type,
135 this is (x->y)->z, even if an intrinsic operator exists which
137 (3) If x has no member y or (x->y) is not a derived type but ".y."
138 is an intrinsic operator (such as ".eq."), this is y(x,z).
139 (4) Lastly if there is no operator ".y." and x has no member "y", it is an
141 It is worth noting that the logic here does not support mixed use of member
142 accessors within a single string. That is, even if x has component y and y
143 has component z, the following are all syntax errors:
144 "x%y.z" "x.y%z" "(x.y).z" "(x%y)%z"
148 gfc_match_member_sep(gfc_symbol
*sym
)
150 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
151 locus dot_loc
, start_loc
;
152 gfc_intrinsic_op iop
;
155 gfc_component
*c
= NULL
;
157 /* What a relief: '%' is an unambiguous member separator. */
158 if (gfc_match_char ('%') == MATCH_YES
)
161 /* Beware ye who enter here. */
162 if (!gfc_option
.flag_dec_structure
|| !sym
)
167 /* We may be given either a derived type variable or the derived type
168 declaration itself (which actually contains the components);
169 we need the latter to search for components. */
170 if (gfc_fl_struct (sym
->attr
.flavor
))
172 else if (gfc_bt_struct (sym
->ts
.type
))
173 tsym
= sym
->ts
.u
.derived
;
175 iop
= INTRINSIC_NONE
;
179 /* If we have to reject come back here later. */
180 start_loc
= gfc_current_locus
;
182 /* Look for a component access next. */
183 if (gfc_match_char ('.') != MATCH_YES
)
186 /* If we accept, come back here. */
187 dot_loc
= gfc_current_locus
;
189 /* Try to match a symbol name following the dot. */
190 if (gfc_match_name (name
) != MATCH_YES
)
192 gfc_error ("Expected structure component or operator name "
197 /* If no dot follows we have "x.y" which should be a component access. */
198 if (gfc_match_char ('.') != MATCH_YES
)
201 /* Now we have a string "x.y.z" which could be a nested member access
202 (x->y)->z or a binary operation y on x and z. */
204 /* First use any user-defined operators ".y." */
205 if (gfc_find_uop (name
, sym
->ns
) != NULL
)
208 /* Match accesses to existing derived-type components for
209 derived-type vars: "x.y.z" = (x->y)->z */
210 c
= gfc_find_component(tsym
, name
, false, true, NULL
);
211 if (c
&& (gfc_bt_struct (c
->ts
.type
) || c
->ts
.type
== BT_CLASS
))
214 /* If y is not a component or has no members, try intrinsic operators. */
215 gfc_current_locus
= start_loc
;
216 if (gfc_match_intrinsic_op (&iop
) != MATCH_YES
)
218 /* If ".y." is not an intrinsic operator but y was a valid non-
219 structure component, match and leave the trailing dot to be
224 gfc_error ("'%s' is neither a defined operator nor a "
225 "structure component in dotted string at %C", name
);
229 /* .y. is an intrinsic operator, overriding any possible member access. */
232 /* Return keeping the current locus consistent with the match result. */
236 gfc_current_locus
= start_loc
;
239 gfc_current_locus
= dot_loc
;
244 /* This function scans the current statement counting the opened and closed
245 parenthesis to make sure they are balanced. */
248 gfc_match_parens (void)
250 locus old_loc
, where
;
252 gfc_instring instring
;
255 old_loc
= gfc_current_locus
;
257 instring
= NONSTRING
;
262 c
= gfc_next_char_literal (instring
);
265 if (quote
== ' ' && ((c
== '\'') || (c
== '"')))
268 instring
= INSTRING_WARN
;
271 if (quote
!= ' ' && c
== quote
)
274 instring
= NONSTRING
;
278 if (c
== '(' && quote
== ' ')
281 where
= gfc_current_locus
;
283 if (c
== ')' && quote
== ' ')
286 where
= gfc_current_locus
;
290 gfc_current_locus
= old_loc
;
294 gfc_error ("Missing %<)%> in statement at or before %L", &where
);
299 gfc_error ("Missing %<(%> in statement at or before %L", &where
);
307 /* See if the next character is a special character that has
308 escaped by a \ via the -fbackslash option. */
311 gfc_match_special_char (gfc_char_t
*res
)
319 switch ((c
= gfc_next_char_literal (INSTRING_WARN
)))
352 /* Hexadecimal form of wide characters. */
353 len
= (c
== 'x' ? 2 : (c
== 'u' ? 4 : 8));
355 for (i
= 0; i
< len
; i
++)
357 char buf
[2] = { '\0', '\0' };
359 c
= gfc_next_char_literal (INSTRING_WARN
);
360 if (!gfc_wide_fits_in_byte (c
)
361 || !gfc_check_digit ((unsigned char) c
, 16))
364 buf
[0] = (unsigned char) c
;
366 n
+= strtol (buf
, NULL
, 16);
372 /* Unknown backslash codes are simply not expanded. */
381 /* In free form, match at least one space. Always matches in fixed
385 gfc_match_space (void)
390 if (gfc_current_form
== FORM_FIXED
)
393 old_loc
= gfc_current_locus
;
395 c
= gfc_next_ascii_char ();
396 if (!gfc_is_whitespace (c
))
398 gfc_current_locus
= old_loc
;
402 gfc_gobble_whitespace ();
408 /* Match an end of statement. End of statement is optional
409 whitespace, followed by a ';' or '\n' or comment '!'. If a
410 semicolon is found, we continue to eat whitespace and semicolons. */
423 old_loc
= gfc_current_locus
;
424 gfc_gobble_whitespace ();
426 c
= gfc_next_ascii_char ();
432 c
= gfc_next_ascii_char ();
449 gfc_current_locus
= old_loc
;
450 return (flag
) ? MATCH_YES
: MATCH_NO
;
454 /* Match a literal integer on the input, setting the value on
455 MATCH_YES. Literal ints occur in kind-parameters as well as
456 old-style character length specifications. If cnt is non-NULL it
457 will be set to the number of digits. */
460 gfc_match_small_literal_int (int *value
, int *cnt
)
466 old_loc
= gfc_current_locus
;
469 gfc_gobble_whitespace ();
470 c
= gfc_next_ascii_char ();
476 gfc_current_locus
= old_loc
;
485 old_loc
= gfc_current_locus
;
486 c
= gfc_next_ascii_char ();
491 i
= 10 * i
+ c
- '0';
496 gfc_error ("Integer too large at %C");
501 gfc_current_locus
= old_loc
;
510 /* Match a small, constant integer expression, like in a kind
511 statement. On MATCH_YES, 'value' is set. */
514 gfc_match_small_int (int *value
)
521 m
= gfc_match_expr (&expr
);
525 p
= gfc_extract_int (expr
, &i
);
526 gfc_free_expr (expr
);
539 /* This function is the same as the gfc_match_small_int, except that
540 we're keeping the pointer to the expr. This function could just be
541 removed and the previously mentioned one modified, though all calls
542 to it would have to be modified then (and there were a number of
543 them). Return MATCH_ERROR if fail to extract the int; otherwise,
544 return the result of gfc_match_expr(). The expr (if any) that was
545 matched is returned in the parameter expr. */
548 gfc_match_small_int_expr (int *value
, gfc_expr
**expr
)
554 m
= gfc_match_expr (expr
);
558 p
= gfc_extract_int (*expr
, &i
);
571 /* Matches a statement label. Uses gfc_match_small_literal_int() to
572 do most of the work. */
575 gfc_match_st_label (gfc_st_label
**label
)
581 old_loc
= gfc_current_locus
;
583 m
= gfc_match_small_literal_int (&i
, &cnt
);
589 gfc_error ("Too many digits in statement label at %C");
595 gfc_error ("Statement label at %C is zero");
599 *label
= gfc_get_st_label (i
);
604 gfc_current_locus
= old_loc
;
609 /* Match and validate a label associated with a named IF, DO or SELECT
610 statement. If the symbol does not have the label attribute, we add
611 it. We also make sure the symbol does not refer to another
612 (active) block. A matched label is pointed to by gfc_new_block. */
615 gfc_match_label (void)
617 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
620 gfc_new_block
= NULL
;
622 m
= gfc_match (" %n :", name
);
626 if (gfc_get_symbol (name
, NULL
, &gfc_new_block
))
628 gfc_error ("Label name %qs at %C is ambiguous", name
);
632 if (gfc_new_block
->attr
.flavor
== FL_LABEL
)
634 gfc_error ("Duplicate construct label %qs at %C", name
);
638 if (!gfc_add_flavor (&gfc_new_block
->attr
, FL_LABEL
,
639 gfc_new_block
->name
, NULL
))
646 /* See if the current input looks like a name of some sort. Modifies
647 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
648 Note that options.c restricts max_identifier_length to not more
649 than GFC_MAX_SYMBOL_LEN. */
652 gfc_match_name (char *buffer
)
658 old_loc
= gfc_current_locus
;
659 gfc_gobble_whitespace ();
661 c
= gfc_next_ascii_char ();
662 if (!(ISALPHA (c
) || (c
== '_' && flag_allow_leading_underscore
)))
664 /* Special cases for unary minus and plus, which allows for a sensible
665 error message for code of the form 'c = exp(-a*b) )' where an
666 extra ')' appears at the end of statement. */
667 if (!gfc_error_flag_test () && c
!= '(' && c
!= '-' && c
!= '+')
668 gfc_error ("Invalid character in name at %C");
669 gfc_current_locus
= old_loc
;
679 if (i
> gfc_option
.max_identifier_length
)
681 gfc_error ("Name at %C is too long");
685 old_loc
= gfc_current_locus
;
686 c
= gfc_next_ascii_char ();
688 while (ISALNUM (c
) || c
== '_' || (flag_dollar_ok
&& c
== '$'));
690 if (c
== '$' && !flag_dollar_ok
)
692 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
693 "allow it as an extension", &old_loc
);
698 gfc_current_locus
= old_loc
;
704 /* Match a symbol on the input. Modifies the pointer to the symbol
705 pointer if successful. */
708 gfc_match_sym_tree (gfc_symtree
**matched_symbol
, int host_assoc
)
710 char buffer
[GFC_MAX_SYMBOL_LEN
+ 1];
713 m
= gfc_match_name (buffer
);
718 return (gfc_get_ha_sym_tree (buffer
, matched_symbol
))
719 ? MATCH_ERROR
: MATCH_YES
;
721 if (gfc_get_sym_tree (buffer
, NULL
, matched_symbol
, false))
729 gfc_match_symbol (gfc_symbol
**matched_symbol
, int host_assoc
)
734 m
= gfc_match_sym_tree (&st
, host_assoc
);
739 *matched_symbol
= st
->n
.sym
;
741 *matched_symbol
= NULL
;
744 *matched_symbol
= NULL
;
749 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
750 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
754 gfc_match_intrinsic_op (gfc_intrinsic_op
*result
)
756 locus orig_loc
= gfc_current_locus
;
759 gfc_gobble_whitespace ();
760 ch
= gfc_next_ascii_char ();
765 *result
= INTRINSIC_PLUS
;
770 *result
= INTRINSIC_MINUS
;
774 if (gfc_next_ascii_char () == '=')
777 *result
= INTRINSIC_EQ
;
783 if (gfc_peek_ascii_char () == '=')
786 gfc_next_ascii_char ();
787 *result
= INTRINSIC_LE
;
791 *result
= INTRINSIC_LT
;
795 if (gfc_peek_ascii_char () == '=')
798 gfc_next_ascii_char ();
799 *result
= INTRINSIC_GE
;
803 *result
= INTRINSIC_GT
;
807 if (gfc_peek_ascii_char () == '*')
810 gfc_next_ascii_char ();
811 *result
= INTRINSIC_POWER
;
815 *result
= INTRINSIC_TIMES
;
819 ch
= gfc_peek_ascii_char ();
823 gfc_next_ascii_char ();
824 *result
= INTRINSIC_NE
;
830 gfc_next_ascii_char ();
831 *result
= INTRINSIC_CONCAT
;
835 *result
= INTRINSIC_DIVIDE
;
839 ch
= gfc_next_ascii_char ();
843 if (gfc_next_ascii_char () == 'n'
844 && gfc_next_ascii_char () == 'd'
845 && gfc_next_ascii_char () == '.')
847 /* Matched ".and.". */
848 *result
= INTRINSIC_AND
;
854 if (gfc_next_ascii_char () == 'q')
856 ch
= gfc_next_ascii_char ();
859 /* Matched ".eq.". */
860 *result
= INTRINSIC_EQ_OS
;
865 if (gfc_next_ascii_char () == '.')
867 /* Matched ".eqv.". */
868 *result
= INTRINSIC_EQV
;
876 ch
= gfc_next_ascii_char ();
879 if (gfc_next_ascii_char () == '.')
881 /* Matched ".ge.". */
882 *result
= INTRINSIC_GE_OS
;
888 if (gfc_next_ascii_char () == '.')
890 /* Matched ".gt.". */
891 *result
= INTRINSIC_GT_OS
;
898 ch
= gfc_next_ascii_char ();
901 if (gfc_next_ascii_char () == '.')
903 /* Matched ".le.". */
904 *result
= INTRINSIC_LE_OS
;
910 if (gfc_next_ascii_char () == '.')
912 /* Matched ".lt.". */
913 *result
= INTRINSIC_LT_OS
;
920 ch
= gfc_next_ascii_char ();
923 ch
= gfc_next_ascii_char ();
926 /* Matched ".ne.". */
927 *result
= INTRINSIC_NE_OS
;
932 if (gfc_next_ascii_char () == 'v'
933 && gfc_next_ascii_char () == '.')
935 /* Matched ".neqv.". */
936 *result
= INTRINSIC_NEQV
;
943 if (gfc_next_ascii_char () == 't'
944 && gfc_next_ascii_char () == '.')
946 /* Matched ".not.". */
947 *result
= INTRINSIC_NOT
;
954 if (gfc_next_ascii_char () == 'r'
955 && gfc_next_ascii_char () == '.')
957 /* Matched ".or.". */
958 *result
= INTRINSIC_OR
;
972 gfc_current_locus
= orig_loc
;
977 /* Match a loop control phrase:
979 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
981 If the final integer expression is not present, a constant unity
982 expression is returned. We don't return MATCH_ERROR until after
983 the equals sign is seen. */
986 gfc_match_iterator (gfc_iterator
*iter
, int init_flag
)
988 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
989 gfc_expr
*var
, *e1
, *e2
, *e3
;
995 /* Match the start of an iterator without affecting the symbol table. */
997 start
= gfc_current_locus
;
998 m
= gfc_match (" %n =", name
);
999 gfc_current_locus
= start
;
1004 m
= gfc_match_variable (&var
, 0);
1008 if (var
->symtree
->n
.sym
->attr
.dimension
)
1010 gfc_error ("Loop variable at %C cannot be an array");
1014 /* F2008, C617 & C565. */
1015 if (var
->symtree
->n
.sym
->attr
.codimension
)
1017 gfc_error ("Loop variable at %C cannot be a coarray");
1021 if (var
->ref
!= NULL
)
1023 gfc_error ("Loop variable at %C cannot be a sub-component");
1027 gfc_match_char ('=');
1029 var
->symtree
->n
.sym
->attr
.implied_index
= 1;
1031 m
= init_flag
? gfc_match_init_expr (&e1
) : gfc_match_expr (&e1
);
1034 if (m
== MATCH_ERROR
)
1037 if (gfc_match_char (',') != MATCH_YES
)
1040 m
= init_flag
? gfc_match_init_expr (&e2
) : gfc_match_expr (&e2
);
1043 if (m
== MATCH_ERROR
)
1046 if (gfc_match_char (',') != MATCH_YES
)
1048 e3
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
1052 m
= init_flag
? gfc_match_init_expr (&e3
) : gfc_match_expr (&e3
);
1053 if (m
== MATCH_ERROR
)
1057 gfc_error ("Expected a step value in iterator at %C");
1069 gfc_error ("Syntax error in iterator at %C");
1080 /* Tries to match the next non-whitespace character on the input.
1081 This subroutine does not return MATCH_ERROR. */
1084 gfc_match_char (char c
)
1088 where
= gfc_current_locus
;
1089 gfc_gobble_whitespace ();
1091 if (gfc_next_ascii_char () == c
)
1094 gfc_current_locus
= where
;
1099 /* General purpose matching subroutine. The target string is a
1100 scanf-like format string in which spaces correspond to arbitrary
1101 whitespace (including no whitespace), characters correspond to
1102 themselves. The %-codes are:
1104 %% Literal percent sign
1105 %e Expression, pointer to a pointer is set
1106 %s Symbol, pointer to the symbol is set
1107 %n Name, character buffer is set to name
1108 %t Matches end of statement.
1109 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1110 %l Matches a statement label
1111 %v Matches a variable expression (an lvalue)
1112 % Matches a required space (in free form) and optional spaces. */
1115 gfc_match (const char *target
, ...)
1117 gfc_st_label
**label
;
1126 old_loc
= gfc_current_locus
;
1127 va_start (argp
, target
);
1137 gfc_gobble_whitespace ();
1148 vp
= va_arg (argp
, void **);
1149 n
= gfc_match_expr ((gfc_expr
**) vp
);
1160 vp
= va_arg (argp
, void **);
1161 n
= gfc_match_variable ((gfc_expr
**) vp
, 0);
1172 vp
= va_arg (argp
, void **);
1173 n
= gfc_match_symbol ((gfc_symbol
**) vp
, 0);
1184 np
= va_arg (argp
, char *);
1185 n
= gfc_match_name (np
);
1196 label
= va_arg (argp
, gfc_st_label
**);
1197 n
= gfc_match_st_label (label
);
1208 ip
= va_arg (argp
, int *);
1209 n
= gfc_match_intrinsic_op ((gfc_intrinsic_op
*) ip
);
1220 if (gfc_match_eos () != MATCH_YES
)
1228 if (gfc_match_space () == MATCH_YES
)
1234 break; /* Fall through to character matcher. */
1237 gfc_internal_error ("gfc_match(): Bad match code %c", c
);
1242 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1243 expect an upper case character here! */
1244 gcc_assert (TOLOWER (c
) == c
);
1246 if (c
== gfc_next_ascii_char ())
1256 /* Clean up after a failed match. */
1257 gfc_current_locus
= old_loc
;
1258 va_start (argp
, target
);
1261 for (; matches
> 0; matches
--)
1263 while (*p
++ != '%');
1271 /* Matches that don't have to be undone */
1276 (void) va_arg (argp
, void **);
1281 vp
= va_arg (argp
, void **);
1282 gfc_free_expr ((struct gfc_expr
*)*vp
);
1295 /*********************** Statement level matching **********************/
1297 /* Matches the start of a program unit, which is the program keyword
1298 followed by an obligatory symbol. */
1301 gfc_match_program (void)
1306 m
= gfc_match ("% %s%t", &sym
);
1310 gfc_error ("Invalid form of PROGRAM statement at %C");
1314 if (m
== MATCH_ERROR
)
1317 if (!gfc_add_flavor (&sym
->attr
, FL_PROGRAM
, sym
->name
, NULL
))
1320 gfc_new_block
= sym
;
1326 /* Match a simple assignment statement. */
1329 gfc_match_assignment (void)
1331 gfc_expr
*lvalue
, *rvalue
;
1335 old_loc
= gfc_current_locus
;
1338 m
= gfc_match (" %v =", &lvalue
);
1341 gfc_current_locus
= old_loc
;
1342 gfc_free_expr (lvalue
);
1347 m
= gfc_match (" %e%t", &rvalue
);
1350 gfc_current_locus
= old_loc
;
1351 gfc_free_expr (lvalue
);
1352 gfc_free_expr (rvalue
);
1356 gfc_set_sym_referenced (lvalue
->symtree
->n
.sym
);
1358 new_st
.op
= EXEC_ASSIGN
;
1359 new_st
.expr1
= lvalue
;
1360 new_st
.expr2
= rvalue
;
1362 gfc_check_do_variable (lvalue
->symtree
);
1368 /* Match a pointer assignment statement. */
1371 gfc_match_pointer_assignment (void)
1373 gfc_expr
*lvalue
, *rvalue
;
1377 old_loc
= gfc_current_locus
;
1379 lvalue
= rvalue
= NULL
;
1380 gfc_matching_ptr_assignment
= 0;
1381 gfc_matching_procptr_assignment
= 0;
1383 m
= gfc_match (" %v =>", &lvalue
);
1390 if (lvalue
->symtree
->n
.sym
->attr
.proc_pointer
1391 || gfc_is_proc_ptr_comp (lvalue
))
1392 gfc_matching_procptr_assignment
= 1;
1394 gfc_matching_ptr_assignment
= 1;
1396 m
= gfc_match (" %e%t", &rvalue
);
1397 gfc_matching_ptr_assignment
= 0;
1398 gfc_matching_procptr_assignment
= 0;
1402 new_st
.op
= EXEC_POINTER_ASSIGN
;
1403 new_st
.expr1
= lvalue
;
1404 new_st
.expr2
= rvalue
;
1409 gfc_current_locus
= old_loc
;
1410 gfc_free_expr (lvalue
);
1411 gfc_free_expr (rvalue
);
1416 /* We try to match an easy arithmetic IF statement. This only happens
1417 when just after having encountered a simple IF statement. This code
1418 is really duplicate with parts of the gfc_match_if code, but this is
1422 match_arithmetic_if (void)
1424 gfc_st_label
*l1
, *l2
, *l3
;
1428 m
= gfc_match (" ( %e ) %l , %l , %l%t", &expr
, &l1
, &l2
, &l3
);
1432 if (!gfc_reference_st_label (l1
, ST_LABEL_TARGET
)
1433 || !gfc_reference_st_label (l2
, ST_LABEL_TARGET
)
1434 || !gfc_reference_st_label (l3
, ST_LABEL_TARGET
))
1436 gfc_free_expr (expr
);
1440 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Arithmetic IF statement at %C"))
1443 new_st
.op
= EXEC_ARITHMETIC_IF
;
1444 new_st
.expr1
= expr
;
1453 /* The IF statement is a bit of a pain. First of all, there are three
1454 forms of it, the simple IF, the IF that starts a block and the
1457 There is a problem with the simple IF and that is the fact that we
1458 only have a single level of undo information on symbols. What this
1459 means is for a simple IF, we must re-match the whole IF statement
1460 multiple times in order to guarantee that the symbol table ends up
1461 in the proper state. */
1463 static match
match_simple_forall (void);
1464 static match
match_simple_where (void);
1467 gfc_match_if (gfc_statement
*if_type
)
1470 gfc_st_label
*l1
, *l2
, *l3
;
1471 locus old_loc
, old_loc2
;
1475 n
= gfc_match_label ();
1476 if (n
== MATCH_ERROR
)
1479 old_loc
= gfc_current_locus
;
1481 m
= gfc_match (" if ( %e", &expr
);
1485 old_loc2
= gfc_current_locus
;
1486 gfc_current_locus
= old_loc
;
1488 if (gfc_match_parens () == MATCH_ERROR
)
1491 gfc_current_locus
= old_loc2
;
1493 if (gfc_match_char (')') != MATCH_YES
)
1495 gfc_error ("Syntax error in IF-expression at %C");
1496 gfc_free_expr (expr
);
1500 m
= gfc_match (" %l , %l , %l%t", &l1
, &l2
, &l3
);
1506 gfc_error ("Block label not appropriate for arithmetic IF "
1508 gfc_free_expr (expr
);
1512 if (!gfc_reference_st_label (l1
, ST_LABEL_TARGET
)
1513 || !gfc_reference_st_label (l2
, ST_LABEL_TARGET
)
1514 || !gfc_reference_st_label (l3
, ST_LABEL_TARGET
))
1516 gfc_free_expr (expr
);
1520 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Arithmetic IF statement at %C"))
1523 new_st
.op
= EXEC_ARITHMETIC_IF
;
1524 new_st
.expr1
= expr
;
1529 *if_type
= ST_ARITHMETIC_IF
;
1533 if (gfc_match (" then%t") == MATCH_YES
)
1535 new_st
.op
= EXEC_IF
;
1536 new_st
.expr1
= expr
;
1537 *if_type
= ST_IF_BLOCK
;
1543 gfc_error ("Block label is not appropriate for IF statement at %C");
1544 gfc_free_expr (expr
);
1548 /* At this point the only thing left is a simple IF statement. At
1549 this point, n has to be MATCH_NO, so we don't have to worry about
1550 re-matching a block label. From what we've got so far, try
1551 matching an assignment. */
1553 *if_type
= ST_SIMPLE_IF
;
1555 m
= gfc_match_assignment ();
1559 gfc_free_expr (expr
);
1560 gfc_undo_symbols ();
1561 gfc_current_locus
= old_loc
;
1563 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1564 assignment was found. For MATCH_NO, continue to call the various
1566 if (m
== MATCH_ERROR
)
1569 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match. */
1571 m
= gfc_match_pointer_assignment ();
1575 gfc_free_expr (expr
);
1576 gfc_undo_symbols ();
1577 gfc_current_locus
= old_loc
;
1579 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match. */
1581 /* Look at the next keyword to see which matcher to call. Matching
1582 the keyword doesn't affect the symbol table, so we don't have to
1583 restore between tries. */
1585 #define match(string, subr, statement) \
1586 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1590 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
)
1591 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
)
1592 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
)
1593 match ("call", gfc_match_call
, ST_CALL
)
1594 match ("close", gfc_match_close
, ST_CLOSE
)
1595 match ("continue", gfc_match_continue
, ST_CONTINUE
)
1596 match ("cycle", gfc_match_cycle
, ST_CYCLE
)
1597 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
)
1598 match ("end file", gfc_match_endfile
, ST_END_FILE
)
1599 match ("error stop", gfc_match_error_stop
, ST_ERROR_STOP
)
1600 match ("event post", gfc_match_event_post
, ST_EVENT_POST
)
1601 match ("event wait", gfc_match_event_wait
, ST_EVENT_WAIT
)
1602 match ("exit", gfc_match_exit
, ST_EXIT
)
1603 match ("flush", gfc_match_flush
, ST_FLUSH
)
1604 match ("forall", match_simple_forall
, ST_FORALL
)
1605 match ("go to", gfc_match_goto
, ST_GOTO
)
1606 match ("if", match_arithmetic_if
, ST_ARITHMETIC_IF
)
1607 match ("inquire", gfc_match_inquire
, ST_INQUIRE
)
1608 match ("lock", gfc_match_lock
, ST_LOCK
)
1609 match ("nullify", gfc_match_nullify
, ST_NULLIFY
)
1610 match ("open", gfc_match_open
, ST_OPEN
)
1611 match ("pause", gfc_match_pause
, ST_NONE
)
1612 match ("print", gfc_match_print
, ST_WRITE
)
1613 match ("read", gfc_match_read
, ST_READ
)
1614 match ("return", gfc_match_return
, ST_RETURN
)
1615 match ("rewind", gfc_match_rewind
, ST_REWIND
)
1616 match ("stop", gfc_match_stop
, ST_STOP
)
1617 match ("wait", gfc_match_wait
, ST_WAIT
)
1618 match ("sync all", gfc_match_sync_all
, ST_SYNC_CALL
);
1619 match ("sync images", gfc_match_sync_images
, ST_SYNC_IMAGES
);
1620 match ("sync memory", gfc_match_sync_memory
, ST_SYNC_MEMORY
);
1621 match ("unlock", gfc_match_unlock
, ST_UNLOCK
)
1622 match ("where", match_simple_where
, ST_WHERE
)
1623 match ("write", gfc_match_write
, ST_WRITE
)
1625 /* The gfc_match_assignment() above may have returned a MATCH_NO
1626 where the assignment was to a named constant. Check that
1627 special case here. */
1628 m
= gfc_match_assignment ();
1631 gfc_error ("Cannot assign to a named constant at %C");
1632 gfc_free_expr (expr
);
1633 gfc_undo_symbols ();
1634 gfc_current_locus
= old_loc
;
1638 /* All else has failed, so give up. See if any of the matchers has
1639 stored an error message of some sort. */
1640 if (!gfc_error_check ())
1641 gfc_error ("Unclassifiable statement in IF-clause at %C");
1643 gfc_free_expr (expr
);
1648 gfc_error ("Syntax error in IF-clause at %C");
1651 gfc_free_expr (expr
);
1655 /* At this point, we've matched the single IF and the action clause
1656 is in new_st. Rearrange things so that the IF statement appears
1659 p
= gfc_get_code (EXEC_IF
);
1660 p
->next
= XCNEW (gfc_code
);
1662 p
->next
->loc
= gfc_current_locus
;
1666 gfc_clear_new_st ();
1668 new_st
.op
= EXEC_IF
;
1677 /* Match an ELSE statement. */
1680 gfc_match_else (void)
1682 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1684 if (gfc_match_eos () == MATCH_YES
)
1687 if (gfc_match_name (name
) != MATCH_YES
1688 || gfc_current_block () == NULL
1689 || gfc_match_eos () != MATCH_YES
)
1691 gfc_error ("Unexpected junk after ELSE statement at %C");
1695 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1697 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1698 name
, gfc_current_block ()->name
);
1706 /* Match an ELSE IF statement. */
1709 gfc_match_elseif (void)
1711 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1715 m
= gfc_match (" ( %e ) then", &expr
);
1719 if (gfc_match_eos () == MATCH_YES
)
1722 if (gfc_match_name (name
) != MATCH_YES
1723 || gfc_current_block () == NULL
1724 || gfc_match_eos () != MATCH_YES
)
1726 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1730 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1732 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1733 name
, gfc_current_block ()->name
);
1738 new_st
.op
= EXEC_IF
;
1739 new_st
.expr1
= expr
;
1743 gfc_free_expr (expr
);
1748 /* Free a gfc_iterator structure. */
1751 gfc_free_iterator (gfc_iterator
*iter
, int flag
)
1757 gfc_free_expr (iter
->var
);
1758 gfc_free_expr (iter
->start
);
1759 gfc_free_expr (iter
->end
);
1760 gfc_free_expr (iter
->step
);
1767 /* Match a CRITICAL statement. */
1769 gfc_match_critical (void)
1771 gfc_st_label
*label
= NULL
;
1773 if (gfc_match_label () == MATCH_ERROR
)
1776 if (gfc_match (" critical") != MATCH_YES
)
1779 if (gfc_match_st_label (&label
) == MATCH_ERROR
)
1782 if (gfc_match_eos () != MATCH_YES
)
1784 gfc_syntax_error (ST_CRITICAL
);
1788 if (gfc_pure (NULL
))
1790 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1794 if (gfc_find_state (COMP_DO_CONCURRENT
))
1796 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1801 gfc_unset_implicit_pure (NULL
);
1803 if (!gfc_notify_std (GFC_STD_F2008
, "CRITICAL statement at %C"))
1806 if (flag_coarray
== GFC_FCOARRAY_NONE
)
1808 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1813 if (gfc_find_state (COMP_CRITICAL
))
1815 gfc_error ("Nested CRITICAL block at %C");
1819 new_st
.op
= EXEC_CRITICAL
;
1822 && !gfc_reference_st_label (label
, ST_LABEL_TARGET
))
1829 /* Match a BLOCK statement. */
1832 gfc_match_block (void)
1836 if (gfc_match_label () == MATCH_ERROR
)
1839 if (gfc_match (" block") != MATCH_YES
)
1842 /* For this to be a correct BLOCK statement, the line must end now. */
1843 m
= gfc_match_eos ();
1844 if (m
== MATCH_ERROR
)
1853 /* Match an ASSOCIATE statement. */
1856 gfc_match_associate (void)
1858 if (gfc_match_label () == MATCH_ERROR
)
1861 if (gfc_match (" associate") != MATCH_YES
)
1864 /* Match the association list. */
1865 if (gfc_match_char ('(') != MATCH_YES
)
1867 gfc_error ("Expected association list at %C");
1870 new_st
.ext
.block
.assoc
= NULL
;
1873 gfc_association_list
* newAssoc
= gfc_get_association_list ();
1874 gfc_association_list
* a
;
1876 /* Match the next association. */
1877 if (gfc_match (" %n => %e", newAssoc
->name
, &newAssoc
->target
)
1880 gfc_error ("Expected association at %C");
1881 goto assocListError
;
1883 newAssoc
->where
= gfc_current_locus
;
1885 /* Check that the current name is not yet in the list. */
1886 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
1887 if (!strcmp (a
->name
, newAssoc
->name
))
1889 gfc_error ("Duplicate name %qs in association at %C",
1891 goto assocListError
;
1894 /* The target expression must not be coindexed. */
1895 if (gfc_is_coindexed (newAssoc
->target
))
1897 gfc_error ("Association target at %C must not be coindexed");
1898 goto assocListError
;
1901 /* The `variable' field is left blank for now; because the target is not
1902 yet resolved, we can't use gfc_has_vector_subscript to determine it
1903 for now. This is set during resolution. */
1905 /* Put it into the list. */
1906 newAssoc
->next
= new_st
.ext
.block
.assoc
;
1907 new_st
.ext
.block
.assoc
= newAssoc
;
1909 /* Try next one or end if closing parenthesis is found. */
1910 gfc_gobble_whitespace ();
1911 if (gfc_peek_char () == ')')
1913 if (gfc_match_char (',') != MATCH_YES
)
1915 gfc_error ("Expected %<)%> or %<,%> at %C");
1925 if (gfc_match_char (')') != MATCH_YES
)
1927 /* This should never happen as we peek above. */
1931 if (gfc_match_eos () != MATCH_YES
)
1933 gfc_error ("Junk after ASSOCIATE statement at %C");
1940 gfc_free_association_list (new_st
.ext
.block
.assoc
);
1945 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1946 an accessible derived type. */
1949 match_derived_type_spec (gfc_typespec
*ts
)
1951 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1953 gfc_symbol
*derived
;
1955 old_locus
= gfc_current_locus
;
1957 if (gfc_match ("%n", name
) != MATCH_YES
)
1959 gfc_current_locus
= old_locus
;
1963 gfc_find_symbol (name
, NULL
, 1, &derived
);
1965 if (derived
&& derived
->attr
.flavor
== FL_PROCEDURE
&& derived
->attr
.generic
)
1966 derived
= gfc_find_dt_in_generic (derived
);
1968 if (derived
&& derived
->attr
.flavor
== FL_DERIVED
)
1970 ts
->type
= BT_DERIVED
;
1971 ts
->u
.derived
= derived
;
1975 gfc_current_locus
= old_locus
;
1980 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
1981 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
1982 It only includes the intrinsic types from the Fortran 2003 standard
1983 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
1984 the implicit_flag is not needed, so it was removed. Derived types are
1985 identified by their name alone. */
1988 gfc_match_type_spec (gfc_typespec
*ts
)
1994 gfc_gobble_whitespace ();
1995 old_locus
= gfc_current_locus
;
1997 if (match_derived_type_spec (ts
) == MATCH_YES
)
1999 /* Enforce F03:C401. */
2000 if (ts
->u
.derived
->attr
.abstract
)
2002 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2003 ts
->u
.derived
->name
, &old_locus
);
2009 if (gfc_match ("integer") == MATCH_YES
)
2011 ts
->type
= BT_INTEGER
;
2012 ts
->kind
= gfc_default_integer_kind
;
2016 if (gfc_match ("real") == MATCH_YES
)
2019 ts
->kind
= gfc_default_real_kind
;
2023 if (gfc_match ("double precision") == MATCH_YES
)
2026 ts
->kind
= gfc_default_double_kind
;
2030 if (gfc_match ("complex") == MATCH_YES
)
2032 ts
->type
= BT_COMPLEX
;
2033 ts
->kind
= gfc_default_complex_kind
;
2037 if (gfc_match ("character") == MATCH_YES
)
2039 ts
->type
= BT_CHARACTER
;
2041 m
= gfc_match_char_spec (ts
);
2049 if (gfc_match ("logical") == MATCH_YES
)
2051 ts
->type
= BT_LOGICAL
;
2052 ts
->kind
= gfc_default_logical_kind
;
2056 /* If a type is not matched, simply return MATCH_NO. */
2057 gfc_current_locus
= old_locus
;
2062 gfc_gobble_whitespace ();
2063 if (gfc_peek_ascii_char () == '*')
2065 gfc_error ("Invalid type-spec at %C");
2069 m
= gfc_match_kind_spec (ts
, false);
2072 m
= MATCH_YES
; /* No kind specifier found. */
2074 /* gfortran may have matched REAL(a=1), which is the keyword form of the
2075 intrinsic procedure. */
2076 if (ts
->type
== BT_REAL
&& m
== MATCH_ERROR
)
2083 /******************** FORALL subroutines ********************/
2085 /* Free a list of FORALL iterators. */
2088 gfc_free_forall_iterator (gfc_forall_iterator
*iter
)
2090 gfc_forall_iterator
*next
;
2095 gfc_free_expr (iter
->var
);
2096 gfc_free_expr (iter
->start
);
2097 gfc_free_expr (iter
->end
);
2098 gfc_free_expr (iter
->stride
);
2105 /* Match an iterator as part of a FORALL statement. The format is:
2107 <var> = <start>:<end>[:<stride>]
2109 On MATCH_NO, the caller tests for the possibility that there is a
2110 scalar mask expression. */
2113 match_forall_iterator (gfc_forall_iterator
**result
)
2115 gfc_forall_iterator
*iter
;
2119 where
= gfc_current_locus
;
2120 iter
= XCNEW (gfc_forall_iterator
);
2122 m
= gfc_match_expr (&iter
->var
);
2126 if (gfc_match_char ('=') != MATCH_YES
2127 || iter
->var
->expr_type
!= EXPR_VARIABLE
)
2133 m
= gfc_match_expr (&iter
->start
);
2137 if (gfc_match_char (':') != MATCH_YES
)
2140 m
= gfc_match_expr (&iter
->end
);
2143 if (m
== MATCH_ERROR
)
2146 if (gfc_match_char (':') == MATCH_NO
)
2147 iter
->stride
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2150 m
= gfc_match_expr (&iter
->stride
);
2153 if (m
== MATCH_ERROR
)
2157 /* Mark the iteration variable's symbol as used as a FORALL index. */
2158 iter
->var
->symtree
->n
.sym
->forall_index
= true;
2164 gfc_error ("Syntax error in FORALL iterator at %C");
2169 gfc_current_locus
= where
;
2170 gfc_free_forall_iterator (iter
);
2175 /* Match the header of a FORALL statement. */
2178 match_forall_header (gfc_forall_iterator
**phead
, gfc_expr
**mask
)
2180 gfc_forall_iterator
*head
, *tail
, *new_iter
;
2184 gfc_gobble_whitespace ();
2189 if (gfc_match_char ('(') != MATCH_YES
)
2192 m
= match_forall_iterator (&new_iter
);
2193 if (m
== MATCH_ERROR
)
2198 head
= tail
= new_iter
;
2202 if (gfc_match_char (',') != MATCH_YES
)
2205 m
= match_forall_iterator (&new_iter
);
2206 if (m
== MATCH_ERROR
)
2211 tail
->next
= new_iter
;
2216 /* Have to have a mask expression. */
2218 m
= gfc_match_expr (&msk
);
2221 if (m
== MATCH_ERROR
)
2227 if (gfc_match_char (')') == MATCH_NO
)
2235 gfc_syntax_error (ST_FORALL
);
2238 gfc_free_expr (msk
);
2239 gfc_free_forall_iterator (head
);
2244 /* Match the rest of a simple FORALL statement that follows an
2248 match_simple_forall (void)
2250 gfc_forall_iterator
*head
;
2259 m
= match_forall_header (&head
, &mask
);
2266 m
= gfc_match_assignment ();
2268 if (m
== MATCH_ERROR
)
2272 m
= gfc_match_pointer_assignment ();
2273 if (m
== MATCH_ERROR
)
2279 c
= XCNEW (gfc_code
);
2281 c
->loc
= gfc_current_locus
;
2283 if (gfc_match_eos () != MATCH_YES
)
2286 gfc_clear_new_st ();
2287 new_st
.op
= EXEC_FORALL
;
2288 new_st
.expr1
= mask
;
2289 new_st
.ext
.forall_iterator
= head
;
2290 new_st
.block
= gfc_get_code (EXEC_FORALL
);
2291 new_st
.block
->next
= c
;
2296 gfc_syntax_error (ST_FORALL
);
2299 gfc_free_forall_iterator (head
);
2300 gfc_free_expr (mask
);
2306 /* Match a FORALL statement. */
2309 gfc_match_forall (gfc_statement
*st
)
2311 gfc_forall_iterator
*head
;
2320 m0
= gfc_match_label ();
2321 if (m0
== MATCH_ERROR
)
2324 m
= gfc_match (" forall");
2328 m
= match_forall_header (&head
, &mask
);
2329 if (m
== MATCH_ERROR
)
2334 if (gfc_match_eos () == MATCH_YES
)
2336 *st
= ST_FORALL_BLOCK
;
2337 new_st
.op
= EXEC_FORALL
;
2338 new_st
.expr1
= mask
;
2339 new_st
.ext
.forall_iterator
= head
;
2343 m
= gfc_match_assignment ();
2344 if (m
== MATCH_ERROR
)
2348 m
= gfc_match_pointer_assignment ();
2349 if (m
== MATCH_ERROR
)
2355 c
= XCNEW (gfc_code
);
2357 c
->loc
= gfc_current_locus
;
2359 gfc_clear_new_st ();
2360 new_st
.op
= EXEC_FORALL
;
2361 new_st
.expr1
= mask
;
2362 new_st
.ext
.forall_iterator
= head
;
2363 new_st
.block
= gfc_get_code (EXEC_FORALL
);
2364 new_st
.block
->next
= c
;
2370 gfc_syntax_error (ST_FORALL
);
2373 gfc_free_forall_iterator (head
);
2374 gfc_free_expr (mask
);
2375 gfc_free_statements (c
);
2380 /* Match a DO statement. */
2385 gfc_iterator iter
, *ip
;
2387 gfc_st_label
*label
;
2390 old_loc
= gfc_current_locus
;
2393 iter
.var
= iter
.start
= iter
.end
= iter
.step
= NULL
;
2395 m
= gfc_match_label ();
2396 if (m
== MATCH_ERROR
)
2399 if (gfc_match (" do") != MATCH_YES
)
2402 m
= gfc_match_st_label (&label
);
2403 if (m
== MATCH_ERROR
)
2406 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2408 if (gfc_match_eos () == MATCH_YES
)
2410 iter
.end
= gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, true);
2411 new_st
.op
= EXEC_DO_WHILE
;
2415 /* Match an optional comma, if no comma is found, a space is obligatory. */
2416 if (gfc_match_char (',') != MATCH_YES
&& gfc_match ("% ") != MATCH_YES
)
2419 /* Check for balanced parens. */
2421 if (gfc_match_parens () == MATCH_ERROR
)
2424 if (gfc_match (" concurrent") == MATCH_YES
)
2426 gfc_forall_iterator
*head
;
2429 if (!gfc_notify_std (GFC_STD_F2008
, "DO CONCURRENT construct at %C"))
2435 m
= match_forall_header (&head
, &mask
);
2439 if (m
== MATCH_ERROR
)
2440 goto concurr_cleanup
;
2442 if (gfc_match_eos () != MATCH_YES
)
2443 goto concurr_cleanup
;
2446 && !gfc_reference_st_label (label
, ST_LABEL_DO_TARGET
))
2447 goto concurr_cleanup
;
2449 new_st
.label1
= label
;
2450 new_st
.op
= EXEC_DO_CONCURRENT
;
2451 new_st
.expr1
= mask
;
2452 new_st
.ext
.forall_iterator
= head
;
2457 gfc_syntax_error (ST_DO
);
2458 gfc_free_expr (mask
);
2459 gfc_free_forall_iterator (head
);
2463 /* See if we have a DO WHILE. */
2464 if (gfc_match (" while ( %e )%t", &iter
.end
) == MATCH_YES
)
2466 new_st
.op
= EXEC_DO_WHILE
;
2470 /* The abortive DO WHILE may have done something to the symbol
2471 table, so we start over. */
2472 gfc_undo_symbols ();
2473 gfc_current_locus
= old_loc
;
2475 gfc_match_label (); /* This won't error. */
2476 gfc_match (" do "); /* This will work. */
2478 gfc_match_st_label (&label
); /* Can't error out. */
2479 gfc_match_char (','); /* Optional comma. */
2481 m
= gfc_match_iterator (&iter
, 0);
2484 if (m
== MATCH_ERROR
)
2487 iter
.var
->symtree
->n
.sym
->attr
.implied_index
= 0;
2488 gfc_check_do_variable (iter
.var
->symtree
);
2490 if (gfc_match_eos () != MATCH_YES
)
2492 gfc_syntax_error (ST_DO
);
2496 new_st
.op
= EXEC_DO
;
2500 && !gfc_reference_st_label (label
, ST_LABEL_DO_TARGET
))
2503 new_st
.label1
= label
;
2505 if (new_st
.op
== EXEC_DO_WHILE
)
2506 new_st
.expr1
= iter
.end
;
2509 new_st
.ext
.iterator
= ip
= gfc_get_iterator ();
2516 gfc_free_iterator (&iter
, 0);
2522 /* Match an EXIT or CYCLE statement. */
2525 match_exit_cycle (gfc_statement st
, gfc_exec_op op
)
2527 gfc_state_data
*p
, *o
;
2532 if (gfc_match_eos () == MATCH_YES
)
2536 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2539 m
= gfc_match ("% %n%t", name
);
2540 if (m
== MATCH_ERROR
)
2544 gfc_syntax_error (st
);
2548 /* Find the corresponding symbol. If there's a BLOCK statement
2549 between here and the label, it is not in gfc_current_ns but a parent
2551 stree
= gfc_find_symtree_in_proc (name
, gfc_current_ns
);
2554 gfc_error ("Name %qs in %s statement at %C is unknown",
2555 name
, gfc_ascii_statement (st
));
2560 if (sym
->attr
.flavor
!= FL_LABEL
)
2562 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2563 name
, gfc_ascii_statement (st
));
2568 /* Find the loop specified by the label (or lack of a label). */
2569 for (o
= NULL
, p
= gfc_state_stack
; p
; p
= p
->previous
)
2570 if (o
== NULL
&& p
->state
== COMP_OMP_STRUCTURED_BLOCK
)
2572 else if (p
->state
== COMP_CRITICAL
)
2574 gfc_error("%s statement at %C leaves CRITICAL construct",
2575 gfc_ascii_statement (st
));
2578 else if (p
->state
== COMP_DO_CONCURRENT
2579 && (op
== EXEC_EXIT
|| (sym
&& sym
!= p
->sym
)))
2581 /* F2008, C821 & C845. */
2582 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2583 gfc_ascii_statement (st
));
2586 else if ((sym
&& sym
== p
->sym
)
2587 || (!sym
&& (p
->state
== COMP_DO
2588 || p
->state
== COMP_DO_CONCURRENT
)))
2594 gfc_error ("%s statement at %C is not within a construct",
2595 gfc_ascii_statement (st
));
2597 gfc_error ("%s statement at %C is not within construct %qs",
2598 gfc_ascii_statement (st
), sym
->name
);
2603 /* Special checks for EXIT from non-loop constructs. */
2607 case COMP_DO_CONCURRENT
:
2611 /* This is already handled above. */
2614 case COMP_ASSOCIATE
:
2618 case COMP_SELECT_TYPE
:
2620 if (op
== EXEC_CYCLE
)
2622 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2623 " construct %qs", sym
->name
);
2626 gcc_assert (op
== EXEC_EXIT
);
2627 if (!gfc_notify_std (GFC_STD_F2008
, "EXIT statement with no"
2628 " do-construct-name at %C"))
2633 gfc_error ("%s statement at %C is not applicable to construct %qs",
2634 gfc_ascii_statement (st
), sym
->name
);
2640 gfc_error (is_oacc (p
)
2641 ? "%s statement at %C leaving OpenACC structured block"
2642 : "%s statement at %C leaving OpenMP structured block",
2643 gfc_ascii_statement (st
));
2647 for (o
= p
, cnt
= 0; o
->state
== COMP_DO
&& o
->previous
!= NULL
; cnt
++)
2651 && o
->state
== COMP_OMP_STRUCTURED_BLOCK
2652 && (o
->head
->op
== EXEC_OACC_LOOP
2653 || o
->head
->op
== EXEC_OACC_PARALLEL_LOOP
))
2656 gcc_assert (o
->head
->next
!= NULL
2657 && (o
->head
->next
->op
== EXEC_DO
2658 || o
->head
->next
->op
== EXEC_DO_WHILE
)
2659 && o
->previous
!= NULL
2660 && o
->previous
->tail
->op
== o
->head
->op
);
2661 if (o
->previous
->tail
->ext
.omp_clauses
!= NULL
2662 && o
->previous
->tail
->ext
.omp_clauses
->collapse
> 1)
2663 collapse
= o
->previous
->tail
->ext
.omp_clauses
->collapse
;
2664 if (st
== ST_EXIT
&& cnt
<= collapse
)
2666 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2669 if (st
== ST_CYCLE
&& cnt
< collapse
)
2671 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2672 " !$ACC LOOP loop");
2678 && (o
->state
== COMP_OMP_STRUCTURED_BLOCK
)
2679 && (o
->head
->op
== EXEC_OMP_DO
2680 || o
->head
->op
== EXEC_OMP_PARALLEL_DO
2681 || o
->head
->op
== EXEC_OMP_SIMD
2682 || o
->head
->op
== EXEC_OMP_DO_SIMD
2683 || o
->head
->op
== EXEC_OMP_PARALLEL_DO_SIMD
))
2686 gcc_assert (o
->head
->next
!= NULL
2687 && (o
->head
->next
->op
== EXEC_DO
2688 || o
->head
->next
->op
== EXEC_DO_WHILE
)
2689 && o
->previous
!= NULL
2690 && o
->previous
->tail
->op
== o
->head
->op
);
2691 if (o
->previous
->tail
->ext
.omp_clauses
!= NULL
2692 && o
->previous
->tail
->ext
.omp_clauses
->collapse
> 1)
2693 collapse
= o
->previous
->tail
->ext
.omp_clauses
->collapse
;
2694 if (st
== ST_EXIT
&& cnt
<= collapse
)
2696 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2699 if (st
== ST_CYCLE
&& cnt
< collapse
)
2701 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2707 /* Save the first statement in the construct - needed by the backend. */
2708 new_st
.ext
.which_construct
= p
->construct
;
2716 /* Match the EXIT statement. */
2719 gfc_match_exit (void)
2721 return match_exit_cycle (ST_EXIT
, EXEC_EXIT
);
2725 /* Match the CYCLE statement. */
2728 gfc_match_cycle (void)
2730 return match_exit_cycle (ST_CYCLE
, EXEC_CYCLE
);
2734 /* Match a number or character constant after an (ERROR) STOP or PAUSE
2738 gfc_match_stopcode (gfc_statement st
)
2745 if (gfc_match_eos () != MATCH_YES
)
2747 m
= gfc_match_init_expr (&e
);
2748 if (m
== MATCH_ERROR
)
2753 if (gfc_match_eos () != MATCH_YES
)
2757 if (gfc_pure (NULL
))
2759 if (st
== ST_ERROR_STOP
)
2761 if (!gfc_notify_std (GFC_STD_F2015
, "%s statement at %C in PURE "
2762 "procedure", gfc_ascii_statement (st
)))
2767 gfc_error ("%s statement not allowed in PURE procedure at %C",
2768 gfc_ascii_statement (st
));
2773 gfc_unset_implicit_pure (NULL
);
2775 if (st
== ST_STOP
&& gfc_find_state (COMP_CRITICAL
))
2777 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2780 if (st
== ST_STOP
&& gfc_find_state (COMP_DO_CONCURRENT
))
2782 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
2788 if (!(e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_INTEGER
))
2790 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2797 gfc_error ("STOP code at %L must be scalar",
2802 if (e
->ts
.type
== BT_CHARACTER
2803 && e
->ts
.kind
!= gfc_default_character_kind
)
2805 gfc_error ("STOP code at %L must be default character KIND=%d",
2806 &e
->where
, (int) gfc_default_character_kind
);
2810 if (e
->ts
.type
== BT_INTEGER
2811 && e
->ts
.kind
!= gfc_default_integer_kind
)
2813 gfc_error ("STOP code at %L must be default integer KIND=%d",
2814 &e
->where
, (int) gfc_default_integer_kind
);
2822 new_st
.op
= EXEC_STOP
;
2825 new_st
.op
= EXEC_ERROR_STOP
;
2828 new_st
.op
= EXEC_PAUSE
;
2835 new_st
.ext
.stop_code
= -1;
2840 gfc_syntax_error (st
);
2849 /* Match the (deprecated) PAUSE statement. */
2852 gfc_match_pause (void)
2856 m
= gfc_match_stopcode (ST_PAUSE
);
2859 if (!gfc_notify_std (GFC_STD_F95_DEL
, "PAUSE statement at %C"))
2866 /* Match the STOP statement. */
2869 gfc_match_stop (void)
2871 return gfc_match_stopcode (ST_STOP
);
2875 /* Match the ERROR STOP statement. */
2878 gfc_match_error_stop (void)
2880 if (!gfc_notify_std (GFC_STD_F2008
, "ERROR STOP statement at %C"))
2883 return gfc_match_stopcode (ST_ERROR_STOP
);
2886 /* Match EVENT POST/WAIT statement. Syntax:
2887 EVENT POST ( event-variable [, sync-stat-list] )
2888 EVENT WAIT ( event-variable [, wait-spec-list] )
2890 wait-spec-list is sync-stat-list or until-spec
2891 until-spec is UNTIL_COUNT = scalar-int-expr
2892 sync-stat is STAT= or ERRMSG=. */
2895 event_statement (gfc_statement st
)
2898 gfc_expr
*tmp
, *eventvar
, *until_count
, *stat
, *errmsg
;
2899 bool saw_until_count
, saw_stat
, saw_errmsg
;
2901 tmp
= eventvar
= until_count
= stat
= errmsg
= NULL
;
2902 saw_until_count
= saw_stat
= saw_errmsg
= false;
2904 if (gfc_pure (NULL
))
2906 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
2907 st
== ST_EVENT_POST
? "POST" : "WAIT");
2911 gfc_unset_implicit_pure (NULL
);
2913 if (flag_coarray
== GFC_FCOARRAY_NONE
)
2915 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2919 if (gfc_find_state (COMP_CRITICAL
))
2921 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
2922 st
== ST_EVENT_POST
? "POST" : "WAIT");
2926 if (gfc_find_state (COMP_DO_CONCURRENT
))
2928 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
2929 "block", st
== ST_EVENT_POST
? "POST" : "WAIT");
2933 if (gfc_match_char ('(') != MATCH_YES
)
2936 if (gfc_match ("%e", &eventvar
) != MATCH_YES
)
2938 m
= gfc_match_char (',');
2939 if (m
== MATCH_ERROR
)
2943 m
= gfc_match_char (')');
2951 m
= gfc_match (" stat = %v", &tmp
);
2952 if (m
== MATCH_ERROR
)
2958 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
2964 m
= gfc_match_char (',');
2972 m
= gfc_match (" errmsg = %v", &tmp
);
2973 if (m
== MATCH_ERROR
)
2979 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
2985 m
= gfc_match_char (',');
2993 m
= gfc_match (" until_count = %e", &tmp
);
2994 if (m
== MATCH_ERROR
|| st
== ST_EVENT_POST
)
2998 if (saw_until_count
)
3000 gfc_error ("Redundant UNTIL_COUNT tag found at %L ",
3005 saw_until_count
= true;
3007 m
= gfc_match_char (',');
3018 if (m
== MATCH_ERROR
)
3021 if (gfc_match (" )%t") != MATCH_YES
)
3028 new_st
.op
= EXEC_EVENT_POST
;
3031 new_st
.op
= EXEC_EVENT_WAIT
;
3037 new_st
.expr1
= eventvar
;
3038 new_st
.expr2
= stat
;
3039 new_st
.expr3
= errmsg
;
3040 new_st
.expr4
= until_count
;
3045 gfc_syntax_error (st
);
3048 if (until_count
!= tmp
)
3049 gfc_free_expr (until_count
);
3051 gfc_free_expr (errmsg
);
3053 gfc_free_expr (stat
);
3055 gfc_free_expr (tmp
);
3056 gfc_free_expr (eventvar
);
3064 gfc_match_event_post (void)
3066 if (!gfc_notify_std (GFC_STD_F2008_TS
, "EVENT POST statement at %C"))
3069 return event_statement (ST_EVENT_POST
);
3074 gfc_match_event_wait (void)
3076 if (!gfc_notify_std (GFC_STD_F2008_TS
, "EVENT WAIT statement at %C"))
3079 return event_statement (ST_EVENT_WAIT
);
3083 /* Match LOCK/UNLOCK statement. Syntax:
3084 LOCK ( lock-variable [ , lock-stat-list ] )
3085 UNLOCK ( lock-variable [ , sync-stat-list ] )
3086 where lock-stat is ACQUIRED_LOCK or sync-stat
3087 and sync-stat is STAT= or ERRMSG=. */
3090 lock_unlock_statement (gfc_statement st
)
3093 gfc_expr
*tmp
, *lockvar
, *acq_lock
, *stat
, *errmsg
;
3094 bool saw_acq_lock
, saw_stat
, saw_errmsg
;
3096 tmp
= lockvar
= acq_lock
= stat
= errmsg
= NULL
;
3097 saw_acq_lock
= saw_stat
= saw_errmsg
= false;
3099 if (gfc_pure (NULL
))
3101 gfc_error ("Image control statement %s at %C in PURE procedure",
3102 st
== ST_LOCK
? "LOCK" : "UNLOCK");
3106 gfc_unset_implicit_pure (NULL
);
3108 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3110 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3114 if (gfc_find_state (COMP_CRITICAL
))
3116 gfc_error ("Image control statement %s at %C in CRITICAL block",
3117 st
== ST_LOCK
? "LOCK" : "UNLOCK");
3121 if (gfc_find_state (COMP_DO_CONCURRENT
))
3123 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3124 st
== ST_LOCK
? "LOCK" : "UNLOCK");
3128 if (gfc_match_char ('(') != MATCH_YES
)
3131 if (gfc_match ("%e", &lockvar
) != MATCH_YES
)
3133 m
= gfc_match_char (',');
3134 if (m
== MATCH_ERROR
)
3138 m
= gfc_match_char (')');
3146 m
= gfc_match (" stat = %v", &tmp
);
3147 if (m
== MATCH_ERROR
)
3153 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
3159 m
= gfc_match_char (',');
3167 m
= gfc_match (" errmsg = %v", &tmp
);
3168 if (m
== MATCH_ERROR
)
3174 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
3180 m
= gfc_match_char (',');
3188 m
= gfc_match (" acquired_lock = %v", &tmp
);
3189 if (m
== MATCH_ERROR
|| st
== ST_UNLOCK
)
3195 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L ",
3200 saw_acq_lock
= true;
3202 m
= gfc_match_char (',');
3213 if (m
== MATCH_ERROR
)
3216 if (gfc_match (" )%t") != MATCH_YES
)
3223 new_st
.op
= EXEC_LOCK
;
3226 new_st
.op
= EXEC_UNLOCK
;
3232 new_st
.expr1
= lockvar
;
3233 new_st
.expr2
= stat
;
3234 new_st
.expr3
= errmsg
;
3235 new_st
.expr4
= acq_lock
;
3240 gfc_syntax_error (st
);
3243 if (acq_lock
!= tmp
)
3244 gfc_free_expr (acq_lock
);
3246 gfc_free_expr (errmsg
);
3248 gfc_free_expr (stat
);
3250 gfc_free_expr (tmp
);
3251 gfc_free_expr (lockvar
);
3258 gfc_match_lock (void)
3260 if (!gfc_notify_std (GFC_STD_F2008
, "LOCK statement at %C"))
3263 return lock_unlock_statement (ST_LOCK
);
3268 gfc_match_unlock (void)
3270 if (!gfc_notify_std (GFC_STD_F2008
, "UNLOCK statement at %C"))
3273 return lock_unlock_statement (ST_UNLOCK
);
3277 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3278 SYNC ALL [(sync-stat-list)]
3279 SYNC MEMORY [(sync-stat-list)]
3280 SYNC IMAGES (image-set [, sync-stat-list] )
3281 with sync-stat is int-expr or *. */
3284 sync_statement (gfc_statement st
)
3287 gfc_expr
*tmp
, *imageset
, *stat
, *errmsg
;
3288 bool saw_stat
, saw_errmsg
;
3290 tmp
= imageset
= stat
= errmsg
= NULL
;
3291 saw_stat
= saw_errmsg
= false;
3293 if (gfc_pure (NULL
))
3295 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3299 gfc_unset_implicit_pure (NULL
);
3301 if (!gfc_notify_std (GFC_STD_F2008
, "SYNC statement at %C"))
3304 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3306 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3311 if (gfc_find_state (COMP_CRITICAL
))
3313 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3317 if (gfc_find_state (COMP_DO_CONCURRENT
))
3319 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3323 if (gfc_match_eos () == MATCH_YES
)
3325 if (st
== ST_SYNC_IMAGES
)
3330 if (gfc_match_char ('(') != MATCH_YES
)
3333 if (st
== ST_SYNC_IMAGES
)
3335 /* Denote '*' as imageset == NULL. */
3336 m
= gfc_match_char ('*');
3337 if (m
== MATCH_ERROR
)
3341 if (gfc_match ("%e", &imageset
) != MATCH_YES
)
3344 m
= gfc_match_char (',');
3345 if (m
== MATCH_ERROR
)
3349 m
= gfc_match_char (')');
3358 m
= gfc_match (" stat = %v", &tmp
);
3359 if (m
== MATCH_ERROR
)
3365 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
3371 if (gfc_match_char (',') == MATCH_YES
)
3378 m
= gfc_match (" errmsg = %v", &tmp
);
3379 if (m
== MATCH_ERROR
)
3385 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
3391 if (gfc_match_char (',') == MATCH_YES
)
3401 if (gfc_match (" )%t") != MATCH_YES
)
3408 new_st
.op
= EXEC_SYNC_ALL
;
3410 case ST_SYNC_IMAGES
:
3411 new_st
.op
= EXEC_SYNC_IMAGES
;
3413 case ST_SYNC_MEMORY
:
3414 new_st
.op
= EXEC_SYNC_MEMORY
;
3420 new_st
.expr1
= imageset
;
3421 new_st
.expr2
= stat
;
3422 new_st
.expr3
= errmsg
;
3427 gfc_syntax_error (st
);
3431 gfc_free_expr (stat
);
3433 gfc_free_expr (errmsg
);
3435 gfc_free_expr (tmp
);
3436 gfc_free_expr (imageset
);
3442 /* Match SYNC ALL statement. */
3445 gfc_match_sync_all (void)
3447 return sync_statement (ST_SYNC_ALL
);
3451 /* Match SYNC IMAGES statement. */
3454 gfc_match_sync_images (void)
3456 return sync_statement (ST_SYNC_IMAGES
);
3460 /* Match SYNC MEMORY statement. */
3463 gfc_match_sync_memory (void)
3465 return sync_statement (ST_SYNC_MEMORY
);
3469 /* Match a CONTINUE statement. */
3472 gfc_match_continue (void)
3474 if (gfc_match_eos () != MATCH_YES
)
3476 gfc_syntax_error (ST_CONTINUE
);
3480 new_st
.op
= EXEC_CONTINUE
;
3485 /* Match the (deprecated) ASSIGN statement. */
3488 gfc_match_assign (void)
3491 gfc_st_label
*label
;
3493 if (gfc_match (" %l", &label
) == MATCH_YES
)
3495 if (!gfc_reference_st_label (label
, ST_LABEL_UNKNOWN
))
3497 if (gfc_match (" to %v%t", &expr
) == MATCH_YES
)
3499 if (!gfc_notify_std (GFC_STD_F95_DEL
, "ASSIGN statement at %C"))
3502 expr
->symtree
->n
.sym
->attr
.assign
= 1;
3504 new_st
.op
= EXEC_LABEL_ASSIGN
;
3505 new_st
.label1
= label
;
3506 new_st
.expr1
= expr
;
3514 /* Match the GO TO statement. As a computed GOTO statement is
3515 matched, it is transformed into an equivalent SELECT block. No
3516 tree is necessary, and the resulting jumps-to-jumps are
3517 specifically optimized away by the back end. */
3520 gfc_match_goto (void)
3522 gfc_code
*head
, *tail
;
3525 gfc_st_label
*label
;
3529 if (gfc_match (" %l%t", &label
) == MATCH_YES
)
3531 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
3534 new_st
.op
= EXEC_GOTO
;
3535 new_st
.label1
= label
;
3539 /* The assigned GO TO statement. */
3541 if (gfc_match_variable (&expr
, 0) == MATCH_YES
)
3543 if (!gfc_notify_std (GFC_STD_F95_DEL
, "Assigned GOTO statement at %C"))
3546 new_st
.op
= EXEC_GOTO
;
3547 new_st
.expr1
= expr
;
3549 if (gfc_match_eos () == MATCH_YES
)
3552 /* Match label list. */
3553 gfc_match_char (',');
3554 if (gfc_match_char ('(') != MATCH_YES
)
3556 gfc_syntax_error (ST_GOTO
);
3563 m
= gfc_match_st_label (&label
);
3567 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
3571 head
= tail
= gfc_get_code (EXEC_GOTO
);
3574 tail
->block
= gfc_get_code (EXEC_GOTO
);
3578 tail
->label1
= label
;
3580 while (gfc_match_char (',') == MATCH_YES
);
3582 if (gfc_match (")%t") != MATCH_YES
)
3587 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3590 new_st
.block
= head
;
3595 /* Last chance is a computed GO TO statement. */
3596 if (gfc_match_char ('(') != MATCH_YES
)
3598 gfc_syntax_error (ST_GOTO
);
3607 m
= gfc_match_st_label (&label
);
3611 if (!gfc_reference_st_label (label
, ST_LABEL_TARGET
))
3615 head
= tail
= gfc_get_code (EXEC_SELECT
);
3618 tail
->block
= gfc_get_code (EXEC_SELECT
);
3622 cp
= gfc_get_case ();
3623 cp
->low
= cp
->high
= gfc_get_int_expr (gfc_default_integer_kind
,
3626 tail
->ext
.block
.case_list
= cp
;
3628 tail
->next
= gfc_get_code (EXEC_GOTO
);
3629 tail
->next
->label1
= label
;
3631 while (gfc_match_char (',') == MATCH_YES
);
3633 if (gfc_match_char (')') != MATCH_YES
)
3638 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3642 /* Get the rest of the statement. */
3643 gfc_match_char (',');
3645 if (gfc_match (" %e%t", &expr
) != MATCH_YES
)
3648 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Computed GOTO at %C"))
3651 /* At this point, a computed GOTO has been fully matched and an
3652 equivalent SELECT statement constructed. */
3654 new_st
.op
= EXEC_SELECT
;
3655 new_st
.expr1
= NULL
;
3657 /* Hack: For a "real" SELECT, the expression is in expr. We put
3658 it in expr2 so we can distinguish then and produce the correct
3660 new_st
.expr2
= expr
;
3661 new_st
.block
= head
;
3665 gfc_syntax_error (ST_GOTO
);
3667 gfc_free_statements (head
);
3672 /* Frees a list of gfc_alloc structures. */
3675 gfc_free_alloc_list (gfc_alloc
*p
)
3682 gfc_free_expr (p
->expr
);
3688 /* Match an ALLOCATE statement. */
3691 gfc_match_allocate (void)
3693 gfc_alloc
*head
, *tail
;
3694 gfc_expr
*stat
, *errmsg
, *tmp
, *source
, *mold
;
3698 locus old_locus
, deferred_locus
;
3699 bool saw_stat
, saw_errmsg
, saw_source
, saw_mold
, saw_deferred
, b1
, b2
, b3
;
3700 bool saw_unlimited
= false;
3703 stat
= errmsg
= source
= mold
= tmp
= NULL
;
3704 saw_stat
= saw_errmsg
= saw_source
= saw_mold
= saw_deferred
= false;
3706 if (gfc_match_char ('(') != MATCH_YES
)
3709 /* Match an optional type-spec. */
3710 old_locus
= gfc_current_locus
;
3711 m
= gfc_match_type_spec (&ts
);
3712 if (m
== MATCH_ERROR
)
3714 else if (m
== MATCH_NO
)
3716 char name
[GFC_MAX_SYMBOL_LEN
+ 3];
3718 if (gfc_match ("%n :: ", name
) == MATCH_YES
)
3720 gfc_error ("Error in type-spec at %L", &old_locus
);
3724 ts
.type
= BT_UNKNOWN
;
3728 if (gfc_match (" :: ") == MATCH_YES
)
3730 if (!gfc_notify_std (GFC_STD_F2003
, "typespec in ALLOCATE at %L",
3736 gfc_error ("Type-spec at %L cannot contain a deferred "
3737 "type parameter", &old_locus
);
3741 if (ts
.type
== BT_CHARACTER
)
3742 ts
.u
.cl
->length_from_typespec
= true;
3746 ts
.type
= BT_UNKNOWN
;
3747 gfc_current_locus
= old_locus
;
3754 head
= tail
= gfc_get_alloc ();
3757 tail
->next
= gfc_get_alloc ();
3761 m
= gfc_match_variable (&tail
->expr
, 0);
3764 if (m
== MATCH_ERROR
)
3767 if (gfc_check_do_variable (tail
->expr
->symtree
))
3770 bool impure
= gfc_impure_variable (tail
->expr
->symtree
->n
.sym
);
3771 if (impure
&& gfc_pure (NULL
))
3773 gfc_error ("Bad allocate-object at %C for a PURE procedure");
3778 gfc_unset_implicit_pure (NULL
);
3780 if (tail
->expr
->ts
.deferred
)
3782 saw_deferred
= true;
3783 deferred_locus
= tail
->expr
->where
;
3786 if (gfc_find_state (COMP_DO_CONCURRENT
)
3787 || gfc_find_state (COMP_CRITICAL
))
3790 bool coarray
= tail
->expr
->symtree
->n
.sym
->attr
.codimension
;
3791 for (ref
= tail
->expr
->ref
; ref
; ref
= ref
->next
)
3792 if (ref
->type
== REF_COMPONENT
)
3793 coarray
= ref
->u
.c
.component
->attr
.codimension
;
3795 if (coarray
&& gfc_find_state (COMP_DO_CONCURRENT
))
3797 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
3800 if (coarray
&& gfc_find_state (COMP_CRITICAL
))
3802 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
3807 /* Check for F08:C628. */
3808 sym
= tail
->expr
->symtree
->n
.sym
;
3809 b1
= !(tail
->expr
->ref
3810 && (tail
->expr
->ref
->type
== REF_COMPONENT
3811 || tail
->expr
->ref
->type
== REF_ARRAY
));
3812 if (sym
&& sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
3813 b2
= !(CLASS_DATA (sym
)->attr
.allocatable
3814 || CLASS_DATA (sym
)->attr
.class_pointer
);
3816 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
3817 || sym
->attr
.proc_pointer
);
3818 b3
= sym
&& sym
->ns
&& sym
->ns
->proc_name
3819 && (sym
->ns
->proc_name
->attr
.allocatable
3820 || sym
->ns
->proc_name
->attr
.pointer
3821 || sym
->ns
->proc_name
->attr
.proc_pointer
);
3822 if (b1
&& b2
&& !b3
)
3824 gfc_error ("Allocate-object at %L is neither a data pointer "
3825 "nor an allocatable variable", &tail
->expr
->where
);
3829 /* The ALLOCATE statement had an optional typespec. Check the
3831 if (ts
.type
!= BT_UNKNOWN
)
3833 /* Enforce F03:C624. */
3834 if (!gfc_type_compatible (&tail
->expr
->ts
, &ts
))
3836 gfc_error ("Type of entity at %L is type incompatible with "
3837 "typespec", &tail
->expr
->where
);
3841 /* Enforce F03:C627. */
3842 if (ts
.kind
!= tail
->expr
->ts
.kind
&& !UNLIMITED_POLY (tail
->expr
))
3844 gfc_error ("Kind type parameter for entity at %L differs from "
3845 "the kind type parameter of the typespec",
3846 &tail
->expr
->where
);
3851 if (tail
->expr
->ts
.type
== BT_DERIVED
)
3852 tail
->expr
->ts
.u
.derived
= gfc_use_derived (tail
->expr
->ts
.u
.derived
);
3854 saw_unlimited
= saw_unlimited
| UNLIMITED_POLY (tail
->expr
);
3856 if (gfc_peek_ascii_char () == '(' && !sym
->attr
.dimension
)
3858 gfc_error ("Shape specification for allocatable scalar at %C");
3862 if (gfc_match_char (',') != MATCH_YES
)
3867 m
= gfc_match (" stat = %v", &tmp
);
3868 if (m
== MATCH_ERROR
)
3875 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
3883 if (gfc_check_do_variable (stat
->symtree
))
3886 if (gfc_match_char (',') == MATCH_YES
)
3887 goto alloc_opt_list
;
3890 m
= gfc_match (" errmsg = %v", &tmp
);
3891 if (m
== MATCH_ERROR
)
3895 if (!gfc_notify_std (GFC_STD_F2003
, "ERRMSG tag at %L", &tmp
->where
))
3901 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
3909 if (gfc_match_char (',') == MATCH_YES
)
3910 goto alloc_opt_list
;
3913 m
= gfc_match (" source = %e", &tmp
);
3914 if (m
== MATCH_ERROR
)
3918 if (!gfc_notify_std (GFC_STD_F2003
, "SOURCE tag at %L", &tmp
->where
))
3924 gfc_error ("Redundant SOURCE tag found at %L ", &tmp
->where
);
3928 /* The next 2 conditionals check C631. */
3929 if (ts
.type
!= BT_UNKNOWN
)
3931 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
3932 &tmp
->where
, &old_locus
);
3937 && !gfc_notify_std (GFC_STD_F2008
, "SOURCE tag at %L"
3938 " with more than a single allocate object",
3946 if (gfc_match_char (',') == MATCH_YES
)
3947 goto alloc_opt_list
;
3950 m
= gfc_match (" mold = %e", &tmp
);
3951 if (m
== MATCH_ERROR
)
3955 if (!gfc_notify_std (GFC_STD_F2008
, "MOLD tag at %L", &tmp
->where
))
3958 /* Check F08:C636. */
3961 gfc_error ("Redundant MOLD tag found at %L ", &tmp
->where
);
3965 /* Check F08:C637. */
3966 if (ts
.type
!= BT_UNKNOWN
)
3968 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
3969 &tmp
->where
, &old_locus
);
3978 if (gfc_match_char (',') == MATCH_YES
)
3979 goto alloc_opt_list
;
3982 gfc_gobble_whitespace ();
3984 if (gfc_peek_char () == ')')
3988 if (gfc_match (" )%t") != MATCH_YES
)
3991 /* Check F08:C637. */
3994 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
3995 &mold
->where
, &source
->where
);
3999 /* Check F03:C623, */
4000 if (saw_deferred
&& ts
.type
== BT_UNKNOWN
&& !source
&& !mold
)
4002 gfc_error ("Allocate-object at %L with a deferred type parameter "
4003 "requires either a type-spec or SOURCE tag or a MOLD tag",
4008 /* Check F03:C625, */
4009 if (saw_unlimited
&& ts
.type
== BT_UNKNOWN
&& !source
&& !mold
)
4011 for (tail
= head
; tail
; tail
= tail
->next
)
4013 if (UNLIMITED_POLY (tail
->expr
))
4014 gfc_error ("Unlimited polymorphic allocate-object at %L "
4015 "requires either a type-spec or SOURCE tag "
4016 "or a MOLD tag", &tail
->expr
->where
);
4021 new_st
.op
= EXEC_ALLOCATE
;
4022 new_st
.expr1
= stat
;
4023 new_st
.expr2
= errmsg
;
4025 new_st
.expr3
= source
;
4027 new_st
.expr3
= mold
;
4028 new_st
.ext
.alloc
.list
= head
;
4029 new_st
.ext
.alloc
.ts
= ts
;
4034 gfc_syntax_error (ST_ALLOCATE
);
4037 gfc_free_expr (errmsg
);
4038 gfc_free_expr (source
);
4039 gfc_free_expr (stat
);
4040 gfc_free_expr (mold
);
4041 if (tmp
&& tmp
->expr_type
) gfc_free_expr (tmp
);
4042 gfc_free_alloc_list (head
);
4047 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4048 a set of pointer assignments to intrinsic NULL(). */
4051 gfc_match_nullify (void)
4059 if (gfc_match_char ('(') != MATCH_YES
)
4064 m
= gfc_match_variable (&p
, 0);
4065 if (m
== MATCH_ERROR
)
4070 if (gfc_check_do_variable (p
->symtree
))
4074 if (gfc_is_coindexed (p
))
4076 gfc_error ("Pointer object at %C shall not be coindexed");
4080 /* build ' => NULL() '. */
4081 e
= gfc_get_null_expr (&gfc_current_locus
);
4083 /* Chain to list. */
4087 tail
->op
= EXEC_POINTER_ASSIGN
;
4091 tail
->next
= gfc_get_code (EXEC_POINTER_ASSIGN
);
4098 if (gfc_match (" )%t") == MATCH_YES
)
4100 if (gfc_match_char (',') != MATCH_YES
)
4107 gfc_syntax_error (ST_NULLIFY
);
4110 gfc_free_statements (new_st
.next
);
4112 gfc_free_expr (new_st
.expr1
);
4113 new_st
.expr1
= NULL
;
4114 gfc_free_expr (new_st
.expr2
);
4115 new_st
.expr2
= NULL
;
4120 /* Match a DEALLOCATE statement. */
4123 gfc_match_deallocate (void)
4125 gfc_alloc
*head
, *tail
;
4126 gfc_expr
*stat
, *errmsg
, *tmp
;
4129 bool saw_stat
, saw_errmsg
, b1
, b2
;
4132 stat
= errmsg
= tmp
= NULL
;
4133 saw_stat
= saw_errmsg
= false;
4135 if (gfc_match_char ('(') != MATCH_YES
)
4141 head
= tail
= gfc_get_alloc ();
4144 tail
->next
= gfc_get_alloc ();
4148 m
= gfc_match_variable (&tail
->expr
, 0);
4149 if (m
== MATCH_ERROR
)
4154 if (gfc_check_do_variable (tail
->expr
->symtree
))
4157 sym
= tail
->expr
->symtree
->n
.sym
;
4159 bool impure
= gfc_impure_variable (sym
);
4160 if (impure
&& gfc_pure (NULL
))
4162 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4167 gfc_unset_implicit_pure (NULL
);
4169 if (gfc_is_coarray (tail
->expr
)
4170 && gfc_find_state (COMP_DO_CONCURRENT
))
4172 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4176 if (gfc_is_coarray (tail
->expr
)
4177 && gfc_find_state (COMP_CRITICAL
))
4179 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4183 /* FIXME: disable the checking on derived types. */
4184 b1
= !(tail
->expr
->ref
4185 && (tail
->expr
->ref
->type
== REF_COMPONENT
4186 || tail
->expr
->ref
->type
== REF_ARRAY
));
4187 if (sym
&& sym
->ts
.type
== BT_CLASS
)
4188 b2
= !(CLASS_DATA (sym
)->attr
.allocatable
4189 || CLASS_DATA (sym
)->attr
.class_pointer
);
4191 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
4192 || sym
->attr
.proc_pointer
);
4195 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4196 "nor an allocatable variable");
4200 if (gfc_match_char (',') != MATCH_YES
)
4205 m
= gfc_match (" stat = %v", &tmp
);
4206 if (m
== MATCH_ERROR
)
4212 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
4213 gfc_free_expr (tmp
);
4220 if (gfc_check_do_variable (stat
->symtree
))
4223 if (gfc_match_char (',') == MATCH_YES
)
4224 goto dealloc_opt_list
;
4227 m
= gfc_match (" errmsg = %v", &tmp
);
4228 if (m
== MATCH_ERROR
)
4232 if (!gfc_notify_std (GFC_STD_F2003
, "ERRMSG at %L", &tmp
->where
))
4237 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
4238 gfc_free_expr (tmp
);
4245 if (gfc_match_char (',') == MATCH_YES
)
4246 goto dealloc_opt_list
;
4249 gfc_gobble_whitespace ();
4251 if (gfc_peek_char () == ')')
4255 if (gfc_match (" )%t") != MATCH_YES
)
4258 new_st
.op
= EXEC_DEALLOCATE
;
4259 new_st
.expr1
= stat
;
4260 new_st
.expr2
= errmsg
;
4261 new_st
.ext
.alloc
.list
= head
;
4266 gfc_syntax_error (ST_DEALLOCATE
);
4269 gfc_free_expr (errmsg
);
4270 gfc_free_expr (stat
);
4271 gfc_free_alloc_list (head
);
4276 /* Match a RETURN statement. */
4279 gfc_match_return (void)
4283 gfc_compile_state s
;
4287 if (gfc_find_state (COMP_CRITICAL
))
4289 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4293 if (gfc_find_state (COMP_DO_CONCURRENT
))
4295 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4299 if (gfc_match_eos () == MATCH_YES
)
4302 if (!gfc_find_state (COMP_SUBROUTINE
))
4304 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4309 if (gfc_current_form
== FORM_FREE
)
4311 /* The following are valid, so we can't require a blank after the
4315 char c
= gfc_peek_ascii_char ();
4316 if (ISALPHA (c
) || ISDIGIT (c
))
4320 m
= gfc_match (" %e%t", &e
);
4323 if (m
== MATCH_ERROR
)
4326 gfc_syntax_error (ST_RETURN
);
4333 gfc_enclosing_unit (&s
);
4334 if (s
== COMP_PROGRAM
4335 && !gfc_notify_std (GFC_STD_GNU
, "RETURN statement in "
4336 "main program at %C"))
4339 new_st
.op
= EXEC_RETURN
;
4346 /* Match the call of a type-bound procedure, if CALL%var has already been
4347 matched and var found to be a derived-type variable. */
4350 match_typebound_call (gfc_symtree
* varst
)
4355 base
= gfc_get_expr ();
4356 base
->expr_type
= EXPR_VARIABLE
;
4357 base
->symtree
= varst
;
4358 base
->where
= gfc_current_locus
;
4359 gfc_set_sym_referenced (varst
->n
.sym
);
4361 m
= gfc_match_varspec (base
, 0, true, true);
4363 gfc_error ("Expected component reference at %C");
4366 gfc_free_expr (base
);
4370 if (gfc_match_eos () != MATCH_YES
)
4372 gfc_error ("Junk after CALL at %C");
4373 gfc_free_expr (base
);
4377 if (base
->expr_type
== EXPR_COMPCALL
)
4378 new_st
.op
= EXEC_COMPCALL
;
4379 else if (base
->expr_type
== EXPR_PPC
)
4380 new_st
.op
= EXEC_CALL_PPC
;
4383 gfc_error ("Expected type-bound procedure or procedure pointer component "
4385 gfc_free_expr (base
);
4388 new_st
.expr1
= base
;
4394 /* Match a CALL statement. The tricky part here are possible
4395 alternate return specifiers. We handle these by having all
4396 "subroutines" actually return an integer via a register that gives
4397 the return number. If the call specifies alternate returns, we
4398 generate code for a SELECT statement whose case clauses contain
4399 GOTOs to the various labels. */
4402 gfc_match_call (void)
4404 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4405 gfc_actual_arglist
*a
, *arglist
;
4415 m
= gfc_match ("% %n", name
);
4421 if (gfc_get_ha_sym_tree (name
, &st
))
4426 /* If this is a variable of derived-type, it probably starts a type-bound
4428 if ((sym
->attr
.flavor
!= FL_PROCEDURE
4429 || gfc_is_function_return_value (sym
, gfc_current_ns
))
4430 && (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
))
4431 return match_typebound_call (st
);
4433 /* If it does not seem to be callable (include functions so that the
4434 right association is made. They are thrown out in resolution.)
4436 if (!sym
->attr
.generic
4437 && !sym
->attr
.subroutine
4438 && !sym
->attr
.function
)
4440 if (!(sym
->attr
.external
&& !sym
->attr
.referenced
))
4442 /* ...create a symbol in this scope... */
4443 if (sym
->ns
!= gfc_current_ns
4444 && gfc_get_sym_tree (name
, NULL
, &st
, false) == 1)
4447 if (sym
!= st
->n
.sym
)
4451 /* ...and then to try to make the symbol into a subroutine. */
4452 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
4456 gfc_set_sym_referenced (sym
);
4458 if (gfc_match_eos () != MATCH_YES
)
4460 m
= gfc_match_actual_arglist (1, &arglist
);
4463 if (m
== MATCH_ERROR
)
4466 if (gfc_match_eos () != MATCH_YES
)
4470 /* If any alternate return labels were found, construct a SELECT
4471 statement that will jump to the right place. */
4474 for (a
= arglist
; a
; a
= a
->next
)
4475 if (a
->expr
== NULL
)
4483 gfc_symtree
*select_st
;
4484 gfc_symbol
*select_sym
;
4485 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4487 new_st
.next
= c
= gfc_get_code (EXEC_SELECT
);
4488 sprintf (name
, "_result_%s", sym
->name
);
4489 gfc_get_ha_sym_tree (name
, &select_st
); /* Can't fail. */
4491 select_sym
= select_st
->n
.sym
;
4492 select_sym
->ts
.type
= BT_INTEGER
;
4493 select_sym
->ts
.kind
= gfc_default_integer_kind
;
4494 gfc_set_sym_referenced (select_sym
);
4495 c
->expr1
= gfc_get_expr ();
4496 c
->expr1
->expr_type
= EXPR_VARIABLE
;
4497 c
->expr1
->symtree
= select_st
;
4498 c
->expr1
->ts
= select_sym
->ts
;
4499 c
->expr1
->where
= gfc_current_locus
;
4502 for (a
= arglist
; a
; a
= a
->next
)
4504 if (a
->expr
!= NULL
)
4507 if (!gfc_reference_st_label (a
->label
, ST_LABEL_TARGET
))
4512 c
->block
= gfc_get_code (EXEC_SELECT
);
4515 new_case
= gfc_get_case ();
4516 new_case
->high
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, i
);
4517 new_case
->low
= new_case
->high
;
4518 c
->ext
.block
.case_list
= new_case
;
4520 c
->next
= gfc_get_code (EXEC_GOTO
);
4521 c
->next
->label1
= a
->label
;
4525 new_st
.op
= EXEC_CALL
;
4526 new_st
.symtree
= st
;
4527 new_st
.ext
.actual
= arglist
;
4532 gfc_syntax_error (ST_CALL
);
4535 gfc_free_actual_arglist (arglist
);
4540 /* Given a name, return a pointer to the common head structure,
4541 creating it if it does not exist. If FROM_MODULE is nonzero, we
4542 mangle the name so that it doesn't interfere with commons defined
4543 in the using namespace.
4544 TODO: Add to global symbol tree. */
4547 gfc_get_common (const char *name
, int from_module
)
4550 static int serial
= 0;
4551 char mangled_name
[GFC_MAX_SYMBOL_LEN
+ 1];
4555 /* A use associated common block is only needed to correctly layout
4556 the variables it contains. */
4557 snprintf (mangled_name
, GFC_MAX_SYMBOL_LEN
, "_%d_%s", serial
++, name
);
4558 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, mangled_name
);
4562 st
= gfc_find_symtree (gfc_current_ns
->common_root
, name
);
4565 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, name
);
4568 if (st
->n
.common
== NULL
)
4570 st
->n
.common
= gfc_get_common_head ();
4571 st
->n
.common
->where
= gfc_current_locus
;
4572 strcpy (st
->n
.common
->name
, name
);
4575 return st
->n
.common
;
4579 /* Match a common block name. */
4581 match
match_common_name (char *name
)
4585 if (gfc_match_char ('/') == MATCH_NO
)
4591 if (gfc_match_char ('/') == MATCH_YES
)
4597 m
= gfc_match_name (name
);
4599 if (m
== MATCH_ERROR
)
4601 if (m
== MATCH_YES
&& gfc_match_char ('/') == MATCH_YES
)
4604 gfc_error ("Syntax error in common block name at %C");
4609 /* Match a COMMON statement. */
4612 gfc_match_common (void)
4614 gfc_symbol
*sym
, **head
, *tail
, *other
;
4615 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4625 m
= match_common_name (name
);
4626 if (m
== MATCH_ERROR
)
4629 if (name
[0] == '\0')
4631 t
= &gfc_current_ns
->blank_common
;
4632 if (t
->head
== NULL
)
4633 t
->where
= gfc_current_locus
;
4637 t
= gfc_get_common (name
, 0);
4646 while (tail
->common_next
)
4647 tail
= tail
->common_next
;
4650 /* Grab the list of symbols. */
4653 m
= gfc_match_symbol (&sym
, 0);
4654 if (m
== MATCH_ERROR
)
4659 /* See if we know the current common block is bind(c), and if
4660 so, then see if we can check if the symbol is (which it'll
4661 need to be). This can happen if the bind(c) attr stmt was
4662 applied to the common block, and the variable(s) already
4663 defined, before declaring the common block. */
4664 if (t
->is_bind_c
== 1)
4666 if (sym
->ts
.type
!= BT_UNKNOWN
&& sym
->ts
.is_c_interop
!= 1)
4668 /* If we find an error, just print it and continue,
4669 cause it's just semantic, and we can see if there
4671 gfc_error_now ("Variable %qs at %L in common block %qs "
4672 "at %C must be declared with a C "
4673 "interoperable kind since common block "
4675 sym
->name
, &(sym
->declared_at
), t
->name
,
4679 if (sym
->attr
.is_bind_c
== 1)
4680 gfc_error_now ("Variable %qs in common block %qs at %C can not "
4681 "be bind(c) since it is not global", sym
->name
,
4685 if (sym
->attr
.in_common
)
4687 gfc_error ("Symbol %qs at %C is already in a COMMON block",
4692 if (((sym
->value
!= NULL
&& sym
->value
->expr_type
!= EXPR_NULL
)
4693 || sym
->attr
.data
) && gfc_current_state () != COMP_BLOCK_DATA
)
4695 if (!gfc_notify_std (GFC_STD_GNU
, "Initialized symbol %qs at "
4696 "%C can only be COMMON in BLOCK DATA",
4701 /* Deal with an optional array specification after the
4703 m
= gfc_match_array_spec (&as
, true, true);
4704 if (m
== MATCH_ERROR
)
4709 if (as
->type
!= AS_EXPLICIT
)
4711 gfc_error ("Array specification for symbol %qs in COMMON "
4712 "at %C must be explicit", sym
->name
);
4716 if (!gfc_add_dimension (&sym
->attr
, sym
->name
, NULL
))
4719 if (sym
->attr
.pointer
)
4721 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
4722 "POINTER array", sym
->name
);
4731 /* Add the in_common attribute, but ignore the reported errors
4732 if any, and continue matching. */
4733 gfc_add_in_common (&sym
->attr
, sym
->name
, NULL
);
4735 sym
->common_block
= t
;
4736 sym
->common_block
->refs
++;
4739 tail
->common_next
= sym
;
4745 sym
->common_head
= t
;
4747 /* Check to see if the symbol is already in an equivalence group.
4748 If it is, set the other members as being in common. */
4749 if (sym
->attr
.in_equivalence
)
4751 for (e1
= gfc_current_ns
->equiv
; e1
; e1
= e1
->next
)
4753 for (e2
= e1
; e2
; e2
= e2
->eq
)
4754 if (e2
->expr
->symtree
->n
.sym
== sym
)
4761 for (e2
= e1
; e2
; e2
= e2
->eq
)
4763 other
= e2
->expr
->symtree
->n
.sym
;
4764 if (other
->common_head
4765 && other
->common_head
!= sym
->common_head
)
4767 gfc_error ("Symbol %qs, in COMMON block %qs at "
4768 "%C is being indirectly equivalenced to "
4769 "another COMMON block %qs",
4770 sym
->name
, sym
->common_head
->name
,
4771 other
->common_head
->name
);
4774 other
->attr
.in_common
= 1;
4775 other
->common_head
= t
;
4781 gfc_gobble_whitespace ();
4782 if (gfc_match_eos () == MATCH_YES
)
4784 if (gfc_peek_ascii_char () == '/')
4786 if (gfc_match_char (',') != MATCH_YES
)
4788 gfc_gobble_whitespace ();
4789 if (gfc_peek_ascii_char () == '/')
4798 gfc_syntax_error (ST_COMMON
);
4801 gfc_free_array_spec (as
);
4806 /* Match a BLOCK DATA program unit. */
4809 gfc_match_block_data (void)
4811 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4815 if (gfc_match_eos () == MATCH_YES
)
4817 gfc_new_block
= NULL
;
4821 m
= gfc_match ("% %n%t", name
);
4825 if (gfc_get_symbol (name
, NULL
, &sym
))
4828 if (!gfc_add_flavor (&sym
->attr
, FL_BLOCK_DATA
, sym
->name
, NULL
))
4831 gfc_new_block
= sym
;
4837 /* Free a namelist structure. */
4840 gfc_free_namelist (gfc_namelist
*name
)
4844 for (; name
; name
= n
)
4852 /* Free an OpenMP namelist structure. */
4855 gfc_free_omp_namelist (gfc_omp_namelist
*name
)
4857 gfc_omp_namelist
*n
;
4859 for (; name
; name
= n
)
4861 gfc_free_expr (name
->expr
);
4864 if (name
->udr
->combiner
)
4865 gfc_free_statement (name
->udr
->combiner
);
4866 if (name
->udr
->initializer
)
4867 gfc_free_statement (name
->udr
->initializer
);
4876 /* Match a NAMELIST statement. */
4879 gfc_match_namelist (void)
4881 gfc_symbol
*group_name
, *sym
;
4885 m
= gfc_match (" / %s /", &group_name
);
4888 if (m
== MATCH_ERROR
)
4893 if (group_name
->ts
.type
!= BT_UNKNOWN
)
4895 gfc_error ("Namelist group name %qs at %C already has a basic "
4896 "type of %s", group_name
->name
,
4897 gfc_typename (&group_name
->ts
));
4901 if (group_name
->attr
.flavor
== FL_NAMELIST
4902 && group_name
->attr
.use_assoc
4903 && !gfc_notify_std (GFC_STD_GNU
, "Namelist group name %qs "
4904 "at %C already is USE associated and can"
4905 "not be respecified.", group_name
->name
))
4908 if (group_name
->attr
.flavor
!= FL_NAMELIST
4909 && !gfc_add_flavor (&group_name
->attr
, FL_NAMELIST
,
4910 group_name
->name
, NULL
))
4915 m
= gfc_match_symbol (&sym
, 1);
4918 if (m
== MATCH_ERROR
)
4921 if (sym
->attr
.in_namelist
== 0
4922 && !gfc_add_in_namelist (&sym
->attr
, sym
->name
, NULL
))
4925 /* Use gfc_error_check here, rather than goto error, so that
4926 these are the only errors for the next two lines. */
4927 if (sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
)
4929 gfc_error ("Assumed size array %qs in namelist %qs at "
4930 "%C is not allowed", sym
->name
, group_name
->name
);
4934 nl
= gfc_get_namelist ();
4938 if (group_name
->namelist
== NULL
)
4939 group_name
->namelist
= group_name
->namelist_tail
= nl
;
4942 group_name
->namelist_tail
->next
= nl
;
4943 group_name
->namelist_tail
= nl
;
4946 if (gfc_match_eos () == MATCH_YES
)
4949 m
= gfc_match_char (',');
4951 if (gfc_match_char ('/') == MATCH_YES
)
4953 m2
= gfc_match (" %s /", &group_name
);
4954 if (m2
== MATCH_YES
)
4956 if (m2
== MATCH_ERROR
)
4970 gfc_syntax_error (ST_NAMELIST
);
4977 /* Match a MODULE statement. */
4980 gfc_match_module (void)
4984 m
= gfc_match (" %s%t", &gfc_new_block
);
4988 if (!gfc_add_flavor (&gfc_new_block
->attr
, FL_MODULE
,
4989 gfc_new_block
->name
, NULL
))
4996 /* Free equivalence sets and lists. Recursively is the easiest way to
5000 gfc_free_equiv_until (gfc_equiv
*eq
, gfc_equiv
*stop
)
5005 gfc_free_equiv (eq
->eq
);
5006 gfc_free_equiv_until (eq
->next
, stop
);
5007 gfc_free_expr (eq
->expr
);
5013 gfc_free_equiv (gfc_equiv
*eq
)
5015 gfc_free_equiv_until (eq
, NULL
);
5019 /* Match an EQUIVALENCE statement. */
5022 gfc_match_equivalence (void)
5024 gfc_equiv
*eq
, *set
, *tail
;
5028 gfc_common_head
*common_head
= NULL
;
5036 eq
= gfc_get_equiv ();
5040 eq
->next
= gfc_current_ns
->equiv
;
5041 gfc_current_ns
->equiv
= eq
;
5043 if (gfc_match_char ('(') != MATCH_YES
)
5047 common_flag
= FALSE
;
5052 m
= gfc_match_equiv_variable (&set
->expr
);
5053 if (m
== MATCH_ERROR
)
5058 /* count the number of objects. */
5061 if (gfc_match_char ('%') == MATCH_YES
)
5063 gfc_error ("Derived type component %C is not a "
5064 "permitted EQUIVALENCE member");
5068 for (ref
= set
->expr
->ref
; ref
; ref
= ref
->next
)
5069 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
== AR_SECTION
)
5071 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5072 "be an array section");
5076 sym
= set
->expr
->symtree
->n
.sym
;
5078 if (!gfc_add_in_equivalence (&sym
->attr
, sym
->name
, NULL
))
5081 if (sym
->attr
.in_common
)
5084 common_head
= sym
->common_head
;
5087 if (gfc_match_char (')') == MATCH_YES
)
5090 if (gfc_match_char (',') != MATCH_YES
)
5093 set
->eq
= gfc_get_equiv ();
5099 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5103 /* If one of the members of an equivalence is in common, then
5104 mark them all as being in common. Before doing this, check
5105 that members of the equivalence group are not in different
5108 for (set
= eq
; set
; set
= set
->eq
)
5110 sym
= set
->expr
->symtree
->n
.sym
;
5111 if (sym
->common_head
&& sym
->common_head
!= common_head
)
5113 gfc_error ("Attempt to indirectly overlap COMMON "
5114 "blocks %s and %s by EQUIVALENCE at %C",
5115 sym
->common_head
->name
, common_head
->name
);
5118 sym
->attr
.in_common
= 1;
5119 sym
->common_head
= common_head
;
5122 if (gfc_match_eos () == MATCH_YES
)
5124 if (gfc_match_char (',') != MATCH_YES
)
5126 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5134 gfc_syntax_error (ST_EQUIVALENCE
);
5140 gfc_free_equiv (gfc_current_ns
->equiv
);
5141 gfc_current_ns
->equiv
= eq
;
5147 /* Check that a statement function is not recursive. This is done by looking
5148 for the statement function symbol(sym) by looking recursively through its
5149 expression(e). If a reference to sym is found, true is returned.
5150 12.5.4 requires that any variable of function that is implicitly typed
5151 shall have that type confirmed by any subsequent type declaration. The
5152 implicit typing is conveniently done here. */
5154 recursive_stmt_fcn (gfc_expr
*, gfc_symbol
*);
5157 check_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
5163 switch (e
->expr_type
)
5166 if (e
->symtree
== NULL
)
5169 /* Check the name before testing for nested recursion! */
5170 if (sym
->name
== e
->symtree
->n
.sym
->name
)
5173 /* Catch recursion via other statement functions. */
5174 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
5175 && e
->symtree
->n
.sym
->value
5176 && recursive_stmt_fcn (e
->symtree
->n
.sym
->value
, sym
))
5179 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
5180 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
5185 if (e
->symtree
&& sym
->name
== e
->symtree
->n
.sym
->name
)
5188 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
5189 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
5201 recursive_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
)
5203 return gfc_traverse_expr (e
, sym
, check_stmt_fcn
, 0);
5207 /* Match a statement function declaration. It is so easy to match
5208 non-statement function statements with a MATCH_ERROR as opposed to
5209 MATCH_NO that we suppress error message in most cases. */
5212 gfc_match_st_function (void)
5214 gfc_error_buffer old_error
;
5219 m
= gfc_match_symbol (&sym
, 0);
5223 gfc_push_error (&old_error
);
5225 if (!gfc_add_procedure (&sym
->attr
, PROC_ST_FUNCTION
, sym
->name
, NULL
))
5228 if (gfc_match_formal_arglist (sym
, 1, 0) != MATCH_YES
)
5231 m
= gfc_match (" = %e%t", &expr
);
5235 gfc_free_error (&old_error
);
5237 if (m
== MATCH_ERROR
)
5240 if (recursive_stmt_fcn (expr
, sym
))
5242 gfc_error ("Statement function at %L is recursive", &expr
->where
);
5248 if ((gfc_current_state () == COMP_FUNCTION
5249 || gfc_current_state () == COMP_SUBROUTINE
)
5250 && gfc_state_stack
->previous
->state
== COMP_INTERFACE
)
5252 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
5257 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Statement function at %C"))
5263 gfc_pop_error (&old_error
);
5268 /* Match an assignment to a pointer function (F2008). This could, in
5269 general be ambiguous with a statement function. In this implementation
5270 it remains so if it is the first statement after the specification
5274 gfc_match_ptr_fcn_assign (void)
5276 gfc_error_buffer old_error
;
5281 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5283 old_loc
= gfc_current_locus
;
5284 m
= gfc_match_name (name
);
5288 gfc_find_symbol (name
, NULL
, 1, &sym
);
5289 if (sym
&& sym
->attr
.flavor
!= FL_PROCEDURE
)
5292 gfc_push_error (&old_error
);
5294 if (sym
&& sym
->attr
.function
)
5295 goto match_actual_arglist
;
5297 gfc_current_locus
= old_loc
;
5298 m
= gfc_match_symbol (&sym
, 0);
5302 if (!gfc_add_procedure (&sym
->attr
, PROC_UNKNOWN
, sym
->name
, NULL
))
5305 match_actual_arglist
:
5306 gfc_current_locus
= old_loc
;
5307 m
= gfc_match (" %e", &expr
);
5311 new_st
.op
= EXEC_ASSIGN
;
5312 new_st
.expr1
= expr
;
5315 m
= gfc_match (" = %e%t", &expr
);
5319 new_st
.expr2
= expr
;
5323 gfc_pop_error (&old_error
);
5328 /***************** SELECT CASE subroutines ******************/
5330 /* Free a single case structure. */
5333 free_case (gfc_case
*p
)
5335 if (p
->low
== p
->high
)
5337 gfc_free_expr (p
->low
);
5338 gfc_free_expr (p
->high
);
5343 /* Free a list of case structures. */
5346 gfc_free_case_list (gfc_case
*p
)
5358 /* Match a single case selector. Combining the requirements of F08:C830
5359 and F08:C832 (R838) means that the case-value must have either CHARACTER,
5360 INTEGER, or LOGICAL type. */
5363 match_case_selector (gfc_case
**cp
)
5368 c
= gfc_get_case ();
5369 c
->where
= gfc_current_locus
;
5371 if (gfc_match_char (':') == MATCH_YES
)
5373 m
= gfc_match_init_expr (&c
->high
);
5376 if (m
== MATCH_ERROR
)
5379 if (c
->high
->ts
.type
!= BT_LOGICAL
&& c
->high
->ts
.type
!= BT_INTEGER
5380 && c
->high
->ts
.type
!= BT_CHARACTER
)
5382 gfc_error ("Expression in CASE selector at %L cannot be %s",
5383 &c
->high
->where
, gfc_typename (&c
->high
->ts
));
5389 m
= gfc_match_init_expr (&c
->low
);
5390 if (m
== MATCH_ERROR
)
5395 if (c
->low
->ts
.type
!= BT_LOGICAL
&& c
->low
->ts
.type
!= BT_INTEGER
5396 && c
->low
->ts
.type
!= BT_CHARACTER
)
5398 gfc_error ("Expression in CASE selector at %L cannot be %s",
5399 &c
->low
->where
, gfc_typename (&c
->low
->ts
));
5403 /* If we're not looking at a ':' now, make a range out of a single
5404 target. Else get the upper bound for the case range. */
5405 if (gfc_match_char (':') != MATCH_YES
)
5409 m
= gfc_match_init_expr (&c
->high
);
5410 if (m
== MATCH_ERROR
)
5412 /* MATCH_NO is fine. It's OK if nothing is there! */
5420 gfc_error ("Expected initialization expression in CASE at %C");
5428 /* Match the end of a case statement. */
5431 match_case_eos (void)
5433 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5436 if (gfc_match_eos () == MATCH_YES
)
5439 /* If the case construct doesn't have a case-construct-name, we
5440 should have matched the EOS. */
5441 if (!gfc_current_block ())
5444 gfc_gobble_whitespace ();
5446 m
= gfc_match_name (name
);
5450 if (strcmp (name
, gfc_current_block ()->name
) != 0)
5452 gfc_error ("Expected block name %qs of SELECT construct at %C",
5453 gfc_current_block ()->name
);
5457 return gfc_match_eos ();
5461 /* Match a SELECT statement. */
5464 gfc_match_select (void)
5469 m
= gfc_match_label ();
5470 if (m
== MATCH_ERROR
)
5473 m
= gfc_match (" select case ( %e )%t", &expr
);
5477 new_st
.op
= EXEC_SELECT
;
5478 new_st
.expr1
= expr
;
5484 /* Transfer the selector typespec to the associate name. */
5487 copy_ts_from_selector_to_associate (gfc_expr
*associate
, gfc_expr
*selector
)
5490 gfc_symbol
*assoc_sym
;
5492 assoc_sym
= associate
->symtree
->n
.sym
;
5494 /* At this stage the expression rank and arrayspec dimensions have
5495 not been completely sorted out. We must get the expr2->rank
5496 right here, so that the correct class container is obtained. */
5497 ref
= selector
->ref
;
5498 while (ref
&& ref
->next
)
5501 if (selector
->ts
.type
== BT_CLASS
&& CLASS_DATA (selector
)->as
5502 && ref
&& ref
->type
== REF_ARRAY
)
5504 /* Ensure that the array reference type is set. We cannot use
5505 gfc_resolve_expr at this point, so the usable parts of
5506 resolve.c(resolve_array_ref) are employed to do it. */
5507 if (ref
->u
.ar
.type
== AR_UNKNOWN
)
5509 ref
->u
.ar
.type
= AR_ELEMENT
;
5510 for (int i
= 0; i
< ref
->u
.ar
.dimen
+ ref
->u
.ar
.codimen
; i
++)
5511 if (ref
->u
.ar
.dimen_type
[i
] == DIMEN_RANGE
5512 || ref
->u
.ar
.dimen_type
[i
] == DIMEN_VECTOR
5513 || (ref
->u
.ar
.dimen_type
[i
] == DIMEN_UNKNOWN
5514 && ref
->u
.ar
.start
[i
] && ref
->u
.ar
.start
[i
]->rank
))
5516 ref
->u
.ar
.type
= AR_SECTION
;
5521 if (ref
->u
.ar
.type
== AR_FULL
)
5522 selector
->rank
= CLASS_DATA (selector
)->as
->rank
;
5523 else if (ref
->u
.ar
.type
== AR_SECTION
)
5524 selector
->rank
= ref
->u
.ar
.dimen
;
5531 assoc_sym
->attr
.dimension
= 1;
5532 assoc_sym
->as
= gfc_get_array_spec ();
5533 assoc_sym
->as
->rank
= selector
->rank
;
5534 assoc_sym
->as
->type
= AS_DEFERRED
;
5537 assoc_sym
->as
= NULL
;
5539 if (selector
->ts
.type
== BT_CLASS
)
5541 /* The correct class container has to be available. */
5542 assoc_sym
->ts
.type
= BT_CLASS
;
5543 assoc_sym
->ts
.u
.derived
= CLASS_DATA (selector
)->ts
.u
.derived
;
5544 assoc_sym
->attr
.pointer
= 1;
5545 gfc_build_class_symbol (&assoc_sym
->ts
, &assoc_sym
->attr
, &assoc_sym
->as
);
5550 /* Push the current selector onto the SELECT TYPE stack. */
5553 select_type_push (gfc_symbol
*sel
)
5555 gfc_select_type_stack
*top
= gfc_get_select_type_stack ();
5556 top
->selector
= sel
;
5558 top
->prev
= select_type_stack
;
5560 select_type_stack
= top
;
5564 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
5566 static gfc_symtree
*
5567 select_intrinsic_set_tmp (gfc_typespec
*ts
)
5569 char name
[GFC_MAX_SYMBOL_LEN
];
5573 if (ts
->type
== BT_CLASS
|| ts
->type
== BT_DERIVED
)
5576 if (select_type_stack
->selector
->ts
.type
== BT_CLASS
5577 && !select_type_stack
->selector
->attr
.class_ok
)
5580 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
5581 && ts
->u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
5582 charlen
= mpz_get_si (ts
->u
.cl
->length
->value
.integer
);
5584 if (ts
->type
!= BT_CHARACTER
)
5585 sprintf (name
, "__tmp_%s_%d", gfc_basic_typename (ts
->type
),
5588 sprintf (name
, "__tmp_%s_%d_%d", gfc_basic_typename (ts
->type
),
5591 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
5592 gfc_add_type (tmp
->n
.sym
, ts
, NULL
);
5594 /* Copy across the array spec to the selector. */
5595 if (select_type_stack
->selector
->ts
.type
== BT_CLASS
5596 && (CLASS_DATA (select_type_stack
->selector
)->attr
.dimension
5597 || CLASS_DATA (select_type_stack
->selector
)->attr
.codimension
))
5599 tmp
->n
.sym
->attr
.pointer
= 1;
5600 tmp
->n
.sym
->attr
.dimension
5601 = CLASS_DATA (select_type_stack
->selector
)->attr
.dimension
;
5602 tmp
->n
.sym
->attr
.codimension
5603 = CLASS_DATA (select_type_stack
->selector
)->attr
.codimension
;
5605 = gfc_copy_array_spec (CLASS_DATA (select_type_stack
->selector
)->as
);
5608 gfc_set_sym_referenced (tmp
->n
.sym
);
5609 gfc_add_flavor (&tmp
->n
.sym
->attr
, FL_VARIABLE
, name
, NULL
);
5610 tmp
->n
.sym
->attr
.select_type_temporary
= 1;
5616 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
5619 select_type_set_tmp (gfc_typespec
*ts
)
5621 char name
[GFC_MAX_SYMBOL_LEN
];
5622 gfc_symtree
*tmp
= NULL
;
5626 select_type_stack
->tmp
= NULL
;
5630 tmp
= select_intrinsic_set_tmp (ts
);
5637 if (ts
->type
== BT_CLASS
)
5638 sprintf (name
, "__tmp_class_%s", ts
->u
.derived
->name
);
5640 sprintf (name
, "__tmp_type_%s", ts
->u
.derived
->name
);
5641 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
5642 gfc_add_type (tmp
->n
.sym
, ts
, NULL
);
5644 if (select_type_stack
->selector
->ts
.type
== BT_CLASS
5645 && select_type_stack
->selector
->attr
.class_ok
)
5647 tmp
->n
.sym
->attr
.pointer
5648 = CLASS_DATA (select_type_stack
->selector
)->attr
.class_pointer
;
5650 /* Copy across the array spec to the selector. */
5651 if (CLASS_DATA (select_type_stack
->selector
)->attr
.dimension
5652 || CLASS_DATA (select_type_stack
->selector
)->attr
.codimension
)
5654 tmp
->n
.sym
->attr
.dimension
5655 = CLASS_DATA (select_type_stack
->selector
)->attr
.dimension
;
5656 tmp
->n
.sym
->attr
.codimension
5657 = CLASS_DATA (select_type_stack
->selector
)->attr
.codimension
;
5659 = gfc_copy_array_spec (CLASS_DATA (select_type_stack
->selector
)->as
);
5663 gfc_set_sym_referenced (tmp
->n
.sym
);
5664 gfc_add_flavor (&tmp
->n
.sym
->attr
, FL_VARIABLE
, name
, NULL
);
5665 tmp
->n
.sym
->attr
.select_type_temporary
= 1;
5667 if (ts
->type
== BT_CLASS
)
5668 gfc_build_class_symbol (&tmp
->n
.sym
->ts
, &tmp
->n
.sym
->attr
,
5672 /* Add an association for it, so the rest of the parser knows it is
5673 an associate-name. The target will be set during resolution. */
5674 tmp
->n
.sym
->assoc
= gfc_get_association_list ();
5675 tmp
->n
.sym
->assoc
->dangling
= 1;
5676 tmp
->n
.sym
->assoc
->st
= tmp
;
5678 select_type_stack
->tmp
= tmp
;
5682 /* Match a SELECT TYPE statement. */
5685 gfc_match_select_type (void)
5687 gfc_expr
*expr1
, *expr2
= NULL
;
5689 char name
[GFC_MAX_SYMBOL_LEN
];
5693 m
= gfc_match_label ();
5694 if (m
== MATCH_ERROR
)
5697 m
= gfc_match (" select type ( ");
5701 m
= gfc_match (" %n => %e", name
, &expr2
);
5704 expr1
= gfc_get_expr();
5705 expr1
->expr_type
= EXPR_VARIABLE
;
5706 if (gfc_get_sym_tree (name
, NULL
, &expr1
->symtree
, false))
5712 sym
= expr1
->symtree
->n
.sym
;
5713 if (expr2
->ts
.type
== BT_UNKNOWN
)
5714 sym
->attr
.untyped
= 1;
5716 copy_ts_from_selector_to_associate (expr1
, expr2
);
5718 sym
->attr
.flavor
= FL_VARIABLE
;
5719 sym
->attr
.referenced
= 1;
5720 sym
->attr
.class_ok
= 1;
5724 m
= gfc_match (" %e ", &expr1
);
5729 m
= gfc_match (" )%t");
5732 gfc_error ("parse error in SELECT TYPE statement at %C");
5736 /* This ghastly expression seems to be needed to distinguish a CLASS
5737 array, which can have a reference, from other expressions that
5738 have references, such as derived type components, and are not
5739 allowed by the standard.
5740 TODO: see if it is sufficient to exclude component and substring
5742 class_array
= expr1
->expr_type
== EXPR_VARIABLE
5743 && expr1
->ts
.type
== BT_CLASS
5744 && CLASS_DATA (expr1
)
5745 && (strcmp (CLASS_DATA (expr1
)->name
, "_data") == 0)
5746 && (CLASS_DATA (expr1
)->attr
.dimension
5747 || CLASS_DATA (expr1
)->attr
.codimension
)
5749 && expr1
->ref
->type
== REF_ARRAY
5750 && expr1
->ref
->next
== NULL
;
5752 /* Check for F03:C811. */
5753 if (!expr2
&& (expr1
->expr_type
!= EXPR_VARIABLE
5754 || (!class_array
&& expr1
->ref
!= NULL
)))
5756 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
5757 "use associate-name=>");
5762 new_st
.op
= EXEC_SELECT_TYPE
;
5763 new_st
.expr1
= expr1
;
5764 new_st
.expr2
= expr2
;
5765 new_st
.ext
.block
.ns
= gfc_current_ns
;
5767 select_type_push (expr1
->symtree
->n
.sym
);
5772 gfc_free_expr (expr1
);
5773 gfc_free_expr (expr2
);
5778 /* Match a CASE statement. */
5781 gfc_match_case (void)
5783 gfc_case
*c
, *head
, *tail
;
5788 if (gfc_current_state () != COMP_SELECT
)
5790 gfc_error ("Unexpected CASE statement at %C");
5794 if (gfc_match ("% default") == MATCH_YES
)
5796 m
= match_case_eos ();
5799 if (m
== MATCH_ERROR
)
5802 new_st
.op
= EXEC_SELECT
;
5803 c
= gfc_get_case ();
5804 c
->where
= gfc_current_locus
;
5805 new_st
.ext
.block
.case_list
= c
;
5809 if (gfc_match_char ('(') != MATCH_YES
)
5814 if (match_case_selector (&c
) == MATCH_ERROR
)
5824 if (gfc_match_char (')') == MATCH_YES
)
5826 if (gfc_match_char (',') != MATCH_YES
)
5830 m
= match_case_eos ();
5833 if (m
== MATCH_ERROR
)
5836 new_st
.op
= EXEC_SELECT
;
5837 new_st
.ext
.block
.case_list
= head
;
5842 gfc_error ("Syntax error in CASE specification at %C");
5845 gfc_free_case_list (head
); /* new_st is cleaned up in parse.c. */
5850 /* Match a TYPE IS statement. */
5853 gfc_match_type_is (void)
5858 if (gfc_current_state () != COMP_SELECT_TYPE
)
5860 gfc_error ("Unexpected TYPE IS statement at %C");
5864 if (gfc_match_char ('(') != MATCH_YES
)
5867 c
= gfc_get_case ();
5868 c
->where
= gfc_current_locus
;
5870 m
= gfc_match_type_spec (&c
->ts
);
5873 if (m
== MATCH_ERROR
)
5876 if (gfc_match_char (')') != MATCH_YES
)
5879 m
= match_case_eos ();
5882 if (m
== MATCH_ERROR
)
5885 new_st
.op
= EXEC_SELECT_TYPE
;
5886 new_st
.ext
.block
.case_list
= c
;
5888 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
5889 && (c
->ts
.u
.derived
->attr
.sequence
5890 || c
->ts
.u
.derived
->attr
.is_bind_c
))
5892 gfc_error ("The type-spec shall not specify a sequence derived "
5893 "type or a type with the BIND attribute in SELECT "
5894 "TYPE at %C [F2003:C815]");
5898 /* Create temporary variable. */
5899 select_type_set_tmp (&c
->ts
);
5904 gfc_error ("Syntax error in TYPE IS specification at %C");
5908 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
5913 /* Match a CLASS IS or CLASS DEFAULT statement. */
5916 gfc_match_class_is (void)
5921 if (gfc_current_state () != COMP_SELECT_TYPE
)
5924 if (gfc_match ("% default") == MATCH_YES
)
5926 m
= match_case_eos ();
5929 if (m
== MATCH_ERROR
)
5932 new_st
.op
= EXEC_SELECT_TYPE
;
5933 c
= gfc_get_case ();
5934 c
->where
= gfc_current_locus
;
5935 c
->ts
.type
= BT_UNKNOWN
;
5936 new_st
.ext
.block
.case_list
= c
;
5937 select_type_set_tmp (NULL
);
5941 m
= gfc_match ("% is");
5944 if (m
== MATCH_ERROR
)
5947 if (gfc_match_char ('(') != MATCH_YES
)
5950 c
= gfc_get_case ();
5951 c
->where
= gfc_current_locus
;
5953 m
= match_derived_type_spec (&c
->ts
);
5956 if (m
== MATCH_ERROR
)
5959 if (c
->ts
.type
== BT_DERIVED
)
5960 c
->ts
.type
= BT_CLASS
;
5962 if (gfc_match_char (')') != MATCH_YES
)
5965 m
= match_case_eos ();
5968 if (m
== MATCH_ERROR
)
5971 new_st
.op
= EXEC_SELECT_TYPE
;
5972 new_st
.ext
.block
.case_list
= c
;
5974 /* Create temporary variable. */
5975 select_type_set_tmp (&c
->ts
);
5980 gfc_error ("Syntax error in CLASS IS specification at %C");
5984 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
5989 /********************* WHERE subroutines ********************/
5991 /* Match the rest of a simple WHERE statement that follows an IF statement.
5995 match_simple_where (void)
6001 m
= gfc_match (" ( %e )", &expr
);
6005 m
= gfc_match_assignment ();
6008 if (m
== MATCH_ERROR
)
6011 if (gfc_match_eos () != MATCH_YES
)
6014 c
= gfc_get_code (EXEC_WHERE
);
6017 c
->next
= XCNEW (gfc_code
);
6019 gfc_clear_new_st ();
6021 new_st
.op
= EXEC_WHERE
;
6027 gfc_syntax_error (ST_WHERE
);
6030 gfc_free_expr (expr
);
6035 /* Match a WHERE statement. */
6038 gfc_match_where (gfc_statement
*st
)
6044 m0
= gfc_match_label ();
6045 if (m0
== MATCH_ERROR
)
6048 m
= gfc_match (" where ( %e )", &expr
);
6052 if (gfc_match_eos () == MATCH_YES
)
6054 *st
= ST_WHERE_BLOCK
;
6055 new_st
.op
= EXEC_WHERE
;
6056 new_st
.expr1
= expr
;
6060 m
= gfc_match_assignment ();
6062 gfc_syntax_error (ST_WHERE
);
6066 gfc_free_expr (expr
);
6070 /* We've got a simple WHERE statement. */
6072 c
= gfc_get_code (EXEC_WHERE
);
6075 c
->next
= XCNEW (gfc_code
);
6077 gfc_clear_new_st ();
6079 new_st
.op
= EXEC_WHERE
;
6086 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
6087 new_st if successful. */
6090 gfc_match_elsewhere (void)
6092 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6096 if (gfc_current_state () != COMP_WHERE
)
6098 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
6104 if (gfc_match_char ('(') == MATCH_YES
)
6106 m
= gfc_match_expr (&expr
);
6109 if (m
== MATCH_ERROR
)
6112 if (gfc_match_char (')') != MATCH_YES
)
6116 if (gfc_match_eos () != MATCH_YES
)
6118 /* Only makes sense if we have a where-construct-name. */
6119 if (!gfc_current_block ())
6124 /* Better be a name at this point. */
6125 m
= gfc_match_name (name
);
6128 if (m
== MATCH_ERROR
)
6131 if (gfc_match_eos () != MATCH_YES
)
6134 if (strcmp (name
, gfc_current_block ()->name
) != 0)
6136 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
6137 name
, gfc_current_block ()->name
);
6142 new_st
.op
= EXEC_WHERE
;
6143 new_st
.expr1
= expr
;
6147 gfc_syntax_error (ST_ELSEWHERE
);
6150 gfc_free_expr (expr
);