1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
30 int gfc_matching_ptr_assignment
= 0;
31 int gfc_matching_procptr_assignment
= 0;
32 bool gfc_matching_prefix
= false;
34 /* Stack of SELECT TYPE statements. */
35 gfc_select_type_stack
*select_type_stack
= NULL
;
37 /* For debugging and diagnostic purposes. Return the textual representation
38 of the intrinsic operator OP. */
40 gfc_op2string (gfc_intrinsic_op op
)
48 case INTRINSIC_UMINUS
:
54 case INTRINSIC_CONCAT
:
58 case INTRINSIC_DIVIDE
:
97 case INTRINSIC_ASSIGN
:
100 case INTRINSIC_PARENTHESES
:
107 gfc_internal_error ("gfc_op2string(): Bad code");
112 /******************** Generic matching subroutines ************************/
114 /* This function scans the current statement counting the opened and closed
115 parenthesis to make sure they are balanced. */
118 gfc_match_parens (void)
120 locus old_loc
, where
;
122 gfc_instring instring
;
125 old_loc
= gfc_current_locus
;
127 instring
= NONSTRING
;
132 c
= gfc_next_char_literal (instring
);
135 if (quote
== ' ' && ((c
== '\'') || (c
== '"')))
138 instring
= INSTRING_WARN
;
141 if (quote
!= ' ' && c
== quote
)
144 instring
= NONSTRING
;
148 if (c
== '(' && quote
== ' ')
151 where
= gfc_current_locus
;
153 if (c
== ')' && quote
== ' ')
156 where
= gfc_current_locus
;
160 gfc_current_locus
= old_loc
;
164 gfc_error ("Missing ')' in statement at or before %L", &where
);
169 gfc_error ("Missing '(' in statement at or before %L", &where
);
177 /* See if the next character is a special character that has
178 escaped by a \ via the -fbackslash option. */
181 gfc_match_special_char (gfc_char_t
*res
)
189 switch ((c
= gfc_next_char_literal (INSTRING_WARN
)))
222 /* Hexadecimal form of wide characters. */
223 len
= (c
== 'x' ? 2 : (c
== 'u' ? 4 : 8));
225 for (i
= 0; i
< len
; i
++)
227 char buf
[2] = { '\0', '\0' };
229 c
= gfc_next_char_literal (INSTRING_WARN
);
230 if (!gfc_wide_fits_in_byte (c
)
231 || !gfc_check_digit ((unsigned char) c
, 16))
234 buf
[0] = (unsigned char) c
;
236 n
+= strtol (buf
, NULL
, 16);
242 /* Unknown backslash codes are simply not expanded. */
251 /* In free form, match at least one space. Always matches in fixed
255 gfc_match_space (void)
260 if (gfc_current_form
== FORM_FIXED
)
263 old_loc
= gfc_current_locus
;
265 c
= gfc_next_ascii_char ();
266 if (!gfc_is_whitespace (c
))
268 gfc_current_locus
= old_loc
;
272 gfc_gobble_whitespace ();
278 /* Match an end of statement. End of statement is optional
279 whitespace, followed by a ';' or '\n' or comment '!'. If a
280 semicolon is found, we continue to eat whitespace and semicolons. */
293 old_loc
= gfc_current_locus
;
294 gfc_gobble_whitespace ();
296 c
= gfc_next_ascii_char ();
302 c
= gfc_next_ascii_char ();
319 gfc_current_locus
= old_loc
;
320 return (flag
) ? MATCH_YES
: MATCH_NO
;
324 /* Match a literal integer on the input, setting the value on
325 MATCH_YES. Literal ints occur in kind-parameters as well as
326 old-style character length specifications. If cnt is non-NULL it
327 will be set to the number of digits. */
330 gfc_match_small_literal_int (int *value
, int *cnt
)
336 old_loc
= gfc_current_locus
;
339 gfc_gobble_whitespace ();
340 c
= gfc_next_ascii_char ();
346 gfc_current_locus
= old_loc
;
355 old_loc
= gfc_current_locus
;
356 c
= gfc_next_ascii_char ();
361 i
= 10 * i
+ c
- '0';
366 gfc_error ("Integer too large at %C");
371 gfc_current_locus
= old_loc
;
380 /* Match a small, constant integer expression, like in a kind
381 statement. On MATCH_YES, 'value' is set. */
384 gfc_match_small_int (int *value
)
391 m
= gfc_match_expr (&expr
);
395 p
= gfc_extract_int (expr
, &i
);
396 gfc_free_expr (expr
);
409 /* This function is the same as the gfc_match_small_int, except that
410 we're keeping the pointer to the expr. This function could just be
411 removed and the previously mentioned one modified, though all calls
412 to it would have to be modified then (and there were a number of
413 them). Return MATCH_ERROR if fail to extract the int; otherwise,
414 return the result of gfc_match_expr(). The expr (if any) that was
415 matched is returned in the parameter expr. */
418 gfc_match_small_int_expr (int *value
, gfc_expr
**expr
)
424 m
= gfc_match_expr (expr
);
428 p
= gfc_extract_int (*expr
, &i
);
441 /* Matches a statement label. Uses gfc_match_small_literal_int() to
442 do most of the work. */
445 gfc_match_st_label (gfc_st_label
**label
)
451 old_loc
= gfc_current_locus
;
453 m
= gfc_match_small_literal_int (&i
, &cnt
);
459 gfc_error ("Too many digits in statement label at %C");
465 gfc_error ("Statement label at %C is zero");
469 *label
= gfc_get_st_label (i
);
474 gfc_current_locus
= old_loc
;
479 /* Match and validate a label associated with a named IF, DO or SELECT
480 statement. If the symbol does not have the label attribute, we add
481 it. We also make sure the symbol does not refer to another
482 (active) block. A matched label is pointed to by gfc_new_block. */
485 gfc_match_label (void)
487 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
490 gfc_new_block
= NULL
;
492 m
= gfc_match (" %n :", name
);
496 if (gfc_get_symbol (name
, NULL
, &gfc_new_block
))
498 gfc_error ("Label name '%s' at %C is ambiguous", name
);
502 if (gfc_new_block
->attr
.flavor
== FL_LABEL
)
504 gfc_error ("Duplicate construct label '%s' at %C", name
);
508 if (gfc_add_flavor (&gfc_new_block
->attr
, FL_LABEL
,
509 gfc_new_block
->name
, NULL
) == FAILURE
)
516 /* See if the current input looks like a name of some sort. Modifies
517 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
518 Note that options.c restricts max_identifier_length to not more
519 than GFC_MAX_SYMBOL_LEN. */
522 gfc_match_name (char *buffer
)
528 old_loc
= gfc_current_locus
;
529 gfc_gobble_whitespace ();
531 c
= gfc_next_ascii_char ();
532 if (!(ISALPHA (c
) || (c
== '_' && gfc_option
.flag_allow_leading_underscore
)))
534 if (gfc_error_flag_test() == 0 && c
!= '(')
535 gfc_error ("Invalid character in name at %C");
536 gfc_current_locus
= old_loc
;
546 if (i
> gfc_option
.max_identifier_length
)
548 gfc_error ("Name at %C is too long");
552 old_loc
= gfc_current_locus
;
553 c
= gfc_next_ascii_char ();
555 while (ISALNUM (c
) || c
== '_' || (gfc_option
.flag_dollar_ok
&& c
== '$'));
557 if (c
== '$' && !gfc_option
.flag_dollar_ok
)
559 gfc_error ("Invalid character '$' at %C. Use -fdollar-ok to allow it "
565 gfc_current_locus
= old_loc
;
571 /* Match a valid name for C, which is almost the same as for Fortran,
572 except that you can start with an underscore, etc.. It could have
573 been done by modifying the gfc_match_name, but this way other
574 things C allows can be added, such as no limits on the length.
575 Right now, the length is limited to the same thing as Fortran..
576 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
577 input characters from being automatically lower cased, since C is
578 case sensitive. The parameter, buffer, is used to return the name
579 that is matched. Return MATCH_ERROR if the name is too long
580 (though this is a self-imposed limit), MATCH_NO if what we're
581 seeing isn't a name, and MATCH_YES if we successfully match a C
585 gfc_match_name_C (char *buffer
)
591 old_loc
= gfc_current_locus
;
592 gfc_gobble_whitespace ();
594 /* Get the next char (first possible char of name) and see if
595 it's valid for C (either a letter or an underscore). */
596 c
= gfc_next_char_literal (INSTRING_WARN
);
598 /* If the user put nothing expect spaces between the quotes, it is valid
599 and simply means there is no name= specifier and the name is the fortran
600 symbol name, all lowercase. */
601 if (c
== '"' || c
== '\'')
604 gfc_current_locus
= old_loc
;
608 if (!ISALPHA (c
) && c
!= '_')
610 gfc_error ("Invalid C name in NAME= specifier at %C");
614 /* Continue to read valid variable name characters. */
617 gcc_assert (gfc_wide_fits_in_byte (c
));
619 buffer
[i
++] = (unsigned char) c
;
621 /* C does not define a maximum length of variable names, to my
622 knowledge, but the compiler typically places a limit on them.
623 For now, i'll use the same as the fortran limit for simplicity,
624 but this may need to be changed to a dynamic buffer that can
625 be realloc'ed here if necessary, or more likely, a larger
627 if (i
> gfc_option
.max_identifier_length
)
629 gfc_error ("Name at %C is too long");
633 old_loc
= gfc_current_locus
;
635 /* Get next char; param means we're in a string. */
636 c
= gfc_next_char_literal (INSTRING_WARN
);
637 } while (ISALNUM (c
) || c
== '_');
640 gfc_current_locus
= old_loc
;
642 /* See if we stopped because of whitespace. */
645 gfc_gobble_whitespace ();
646 c
= gfc_peek_ascii_char ();
647 if (c
!= '"' && c
!= '\'')
649 gfc_error ("Embedded space in NAME= specifier at %C");
654 /* If we stopped because we had an invalid character for a C name, report
655 that to the user by returning MATCH_NO. */
656 if (c
!= '"' && c
!= '\'')
658 gfc_error ("Invalid C name in NAME= specifier at %C");
666 /* Match a symbol on the input. Modifies the pointer to the symbol
667 pointer if successful. */
670 gfc_match_sym_tree (gfc_symtree
**matched_symbol
, int host_assoc
)
672 char buffer
[GFC_MAX_SYMBOL_LEN
+ 1];
675 m
= gfc_match_name (buffer
);
680 return (gfc_get_ha_sym_tree (buffer
, matched_symbol
))
681 ? MATCH_ERROR
: MATCH_YES
;
683 if (gfc_get_sym_tree (buffer
, NULL
, matched_symbol
, false))
691 gfc_match_symbol (gfc_symbol
**matched_symbol
, int host_assoc
)
696 m
= gfc_match_sym_tree (&st
, host_assoc
);
701 *matched_symbol
= st
->n
.sym
;
703 *matched_symbol
= NULL
;
706 *matched_symbol
= NULL
;
711 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
712 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
716 gfc_match_intrinsic_op (gfc_intrinsic_op
*result
)
718 locus orig_loc
= gfc_current_locus
;
721 gfc_gobble_whitespace ();
722 ch
= gfc_next_ascii_char ();
727 *result
= INTRINSIC_PLUS
;
732 *result
= INTRINSIC_MINUS
;
736 if (gfc_next_ascii_char () == '=')
739 *result
= INTRINSIC_EQ
;
745 if (gfc_peek_ascii_char () == '=')
748 gfc_next_ascii_char ();
749 *result
= INTRINSIC_LE
;
753 *result
= INTRINSIC_LT
;
757 if (gfc_peek_ascii_char () == '=')
760 gfc_next_ascii_char ();
761 *result
= INTRINSIC_GE
;
765 *result
= INTRINSIC_GT
;
769 if (gfc_peek_ascii_char () == '*')
772 gfc_next_ascii_char ();
773 *result
= INTRINSIC_POWER
;
777 *result
= INTRINSIC_TIMES
;
781 ch
= gfc_peek_ascii_char ();
785 gfc_next_ascii_char ();
786 *result
= INTRINSIC_NE
;
792 gfc_next_ascii_char ();
793 *result
= INTRINSIC_CONCAT
;
797 *result
= INTRINSIC_DIVIDE
;
801 ch
= gfc_next_ascii_char ();
805 if (gfc_next_ascii_char () == 'n'
806 && gfc_next_ascii_char () == 'd'
807 && gfc_next_ascii_char () == '.')
809 /* Matched ".and.". */
810 *result
= INTRINSIC_AND
;
816 if (gfc_next_ascii_char () == 'q')
818 ch
= gfc_next_ascii_char ();
821 /* Matched ".eq.". */
822 *result
= INTRINSIC_EQ_OS
;
827 if (gfc_next_ascii_char () == '.')
829 /* Matched ".eqv.". */
830 *result
= INTRINSIC_EQV
;
838 ch
= gfc_next_ascii_char ();
841 if (gfc_next_ascii_char () == '.')
843 /* Matched ".ge.". */
844 *result
= INTRINSIC_GE_OS
;
850 if (gfc_next_ascii_char () == '.')
852 /* Matched ".gt.". */
853 *result
= INTRINSIC_GT_OS
;
860 ch
= gfc_next_ascii_char ();
863 if (gfc_next_ascii_char () == '.')
865 /* Matched ".le.". */
866 *result
= INTRINSIC_LE_OS
;
872 if (gfc_next_ascii_char () == '.')
874 /* Matched ".lt.". */
875 *result
= INTRINSIC_LT_OS
;
882 ch
= gfc_next_ascii_char ();
885 ch
= gfc_next_ascii_char ();
888 /* Matched ".ne.". */
889 *result
= INTRINSIC_NE_OS
;
894 if (gfc_next_ascii_char () == 'v'
895 && gfc_next_ascii_char () == '.')
897 /* Matched ".neqv.". */
898 *result
= INTRINSIC_NEQV
;
905 if (gfc_next_ascii_char () == 't'
906 && gfc_next_ascii_char () == '.')
908 /* Matched ".not.". */
909 *result
= INTRINSIC_NOT
;
916 if (gfc_next_ascii_char () == 'r'
917 && gfc_next_ascii_char () == '.')
919 /* Matched ".or.". */
920 *result
= INTRINSIC_OR
;
934 gfc_current_locus
= orig_loc
;
939 /* Match a loop control phrase:
941 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
943 If the final integer expression is not present, a constant unity
944 expression is returned. We don't return MATCH_ERROR until after
945 the equals sign is seen. */
948 gfc_match_iterator (gfc_iterator
*iter
, int init_flag
)
950 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
951 gfc_expr
*var
, *e1
, *e2
, *e3
;
957 /* Match the start of an iterator without affecting the symbol table. */
959 start
= gfc_current_locus
;
960 m
= gfc_match (" %n =", name
);
961 gfc_current_locus
= start
;
966 m
= gfc_match_variable (&var
, 0);
970 /* F2008, C617 & C565. */
971 if (var
->symtree
->n
.sym
->attr
.codimension
)
973 gfc_error ("Loop variable at %C cannot be a coarray");
977 if (var
->ref
!= NULL
)
979 gfc_error ("Loop variable at %C cannot be a sub-component");
983 gfc_match_char ('=');
985 var
->symtree
->n
.sym
->attr
.implied_index
= 1;
987 m
= init_flag
? gfc_match_init_expr (&e1
) : gfc_match_expr (&e1
);
990 if (m
== MATCH_ERROR
)
993 if (gfc_match_char (',') != MATCH_YES
)
996 m
= init_flag
? gfc_match_init_expr (&e2
) : gfc_match_expr (&e2
);
999 if (m
== MATCH_ERROR
)
1002 if (gfc_match_char (',') != MATCH_YES
)
1004 e3
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
1008 m
= init_flag
? gfc_match_init_expr (&e3
) : gfc_match_expr (&e3
);
1009 if (m
== MATCH_ERROR
)
1013 gfc_error ("Expected a step value in iterator at %C");
1025 gfc_error ("Syntax error in iterator at %C");
1036 /* Tries to match the next non-whitespace character on the input.
1037 This subroutine does not return MATCH_ERROR. */
1040 gfc_match_char (char c
)
1044 where
= gfc_current_locus
;
1045 gfc_gobble_whitespace ();
1047 if (gfc_next_ascii_char () == c
)
1050 gfc_current_locus
= where
;
1055 /* General purpose matching subroutine. The target string is a
1056 scanf-like format string in which spaces correspond to arbitrary
1057 whitespace (including no whitespace), characters correspond to
1058 themselves. The %-codes are:
1060 %% Literal percent sign
1061 %e Expression, pointer to a pointer is set
1062 %s Symbol, pointer to the symbol is set
1063 %n Name, character buffer is set to name
1064 %t Matches end of statement.
1065 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1066 %l Matches a statement label
1067 %v Matches a variable expression (an lvalue)
1068 % Matches a required space (in free form) and optional spaces. */
1071 gfc_match (const char *target
, ...)
1073 gfc_st_label
**label
;
1082 old_loc
= gfc_current_locus
;
1083 va_start (argp
, target
);
1093 gfc_gobble_whitespace ();
1104 vp
= va_arg (argp
, void **);
1105 n
= gfc_match_expr ((gfc_expr
**) vp
);
1116 vp
= va_arg (argp
, void **);
1117 n
= gfc_match_variable ((gfc_expr
**) vp
, 0);
1128 vp
= va_arg (argp
, void **);
1129 n
= gfc_match_symbol ((gfc_symbol
**) vp
, 0);
1140 np
= va_arg (argp
, char *);
1141 n
= gfc_match_name (np
);
1152 label
= va_arg (argp
, gfc_st_label
**);
1153 n
= gfc_match_st_label (label
);
1164 ip
= va_arg (argp
, int *);
1165 n
= gfc_match_intrinsic_op ((gfc_intrinsic_op
*) ip
);
1176 if (gfc_match_eos () != MATCH_YES
)
1184 if (gfc_match_space () == MATCH_YES
)
1190 break; /* Fall through to character matcher. */
1193 gfc_internal_error ("gfc_match(): Bad match code %c", c
);
1198 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1199 expect an upper case character here! */
1200 gcc_assert (TOLOWER (c
) == c
);
1202 if (c
== gfc_next_ascii_char ())
1212 /* Clean up after a failed match. */
1213 gfc_current_locus
= old_loc
;
1214 va_start (argp
, target
);
1217 for (; matches
> 0; matches
--)
1219 while (*p
++ != '%');
1227 /* Matches that don't have to be undone */
1232 (void) va_arg (argp
, void **);
1237 vp
= va_arg (argp
, void **);
1238 gfc_free_expr ((struct gfc_expr
*)*vp
);
1251 /*********************** Statement level matching **********************/
1253 /* Matches the start of a program unit, which is the program keyword
1254 followed by an obligatory symbol. */
1257 gfc_match_program (void)
1262 m
= gfc_match ("% %s%t", &sym
);
1266 gfc_error ("Invalid form of PROGRAM statement at %C");
1270 if (m
== MATCH_ERROR
)
1273 if (gfc_add_flavor (&sym
->attr
, FL_PROGRAM
, sym
->name
, NULL
) == FAILURE
)
1276 gfc_new_block
= sym
;
1282 /* Match a simple assignment statement. */
1285 gfc_match_assignment (void)
1287 gfc_expr
*lvalue
, *rvalue
;
1291 old_loc
= gfc_current_locus
;
1294 m
= gfc_match (" %v =", &lvalue
);
1297 gfc_current_locus
= old_loc
;
1298 gfc_free_expr (lvalue
);
1303 m
= gfc_match (" %e%t", &rvalue
);
1306 gfc_current_locus
= old_loc
;
1307 gfc_free_expr (lvalue
);
1308 gfc_free_expr (rvalue
);
1312 gfc_set_sym_referenced (lvalue
->symtree
->n
.sym
);
1314 new_st
.op
= EXEC_ASSIGN
;
1315 new_st
.expr1
= lvalue
;
1316 new_st
.expr2
= rvalue
;
1318 gfc_check_do_variable (lvalue
->symtree
);
1324 /* Match a pointer assignment statement. */
1327 gfc_match_pointer_assignment (void)
1329 gfc_expr
*lvalue
, *rvalue
;
1333 old_loc
= gfc_current_locus
;
1335 lvalue
= rvalue
= NULL
;
1336 gfc_matching_ptr_assignment
= 0;
1337 gfc_matching_procptr_assignment
= 0;
1339 m
= gfc_match (" %v =>", &lvalue
);
1346 if (lvalue
->symtree
->n
.sym
->attr
.proc_pointer
1347 || gfc_is_proc_ptr_comp (lvalue
, NULL
))
1348 gfc_matching_procptr_assignment
= 1;
1350 gfc_matching_ptr_assignment
= 1;
1352 m
= gfc_match (" %e%t", &rvalue
);
1353 gfc_matching_ptr_assignment
= 0;
1354 gfc_matching_procptr_assignment
= 0;
1358 new_st
.op
= EXEC_POINTER_ASSIGN
;
1359 new_st
.expr1
= lvalue
;
1360 new_st
.expr2
= rvalue
;
1365 gfc_current_locus
= old_loc
;
1366 gfc_free_expr (lvalue
);
1367 gfc_free_expr (rvalue
);
1372 /* We try to match an easy arithmetic IF statement. This only happens
1373 when just after having encountered a simple IF statement. This code
1374 is really duplicate with parts of the gfc_match_if code, but this is
1378 match_arithmetic_if (void)
1380 gfc_st_label
*l1
, *l2
, *l3
;
1384 m
= gfc_match (" ( %e ) %l , %l , %l%t", &expr
, &l1
, &l2
, &l3
);
1388 if (gfc_reference_st_label (l1
, ST_LABEL_TARGET
) == FAILURE
1389 || gfc_reference_st_label (l2
, ST_LABEL_TARGET
) == FAILURE
1390 || gfc_reference_st_label (l3
, ST_LABEL_TARGET
) == FAILURE
)
1392 gfc_free_expr (expr
);
1396 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: Arithmetic IF "
1397 "statement at %C") == FAILURE
)
1400 new_st
.op
= EXEC_ARITHMETIC_IF
;
1401 new_st
.expr1
= expr
;
1410 /* The IF statement is a bit of a pain. First of all, there are three
1411 forms of it, the simple IF, the IF that starts a block and the
1414 There is a problem with the simple IF and that is the fact that we
1415 only have a single level of undo information on symbols. What this
1416 means is for a simple IF, we must re-match the whole IF statement
1417 multiple times in order to guarantee that the symbol table ends up
1418 in the proper state. */
1420 static match
match_simple_forall (void);
1421 static match
match_simple_where (void);
1424 gfc_match_if (gfc_statement
*if_type
)
1427 gfc_st_label
*l1
, *l2
, *l3
;
1428 locus old_loc
, old_loc2
;
1432 n
= gfc_match_label ();
1433 if (n
== MATCH_ERROR
)
1436 old_loc
= gfc_current_locus
;
1438 m
= gfc_match (" if ( %e", &expr
);
1442 old_loc2
= gfc_current_locus
;
1443 gfc_current_locus
= old_loc
;
1445 if (gfc_match_parens () == MATCH_ERROR
)
1448 gfc_current_locus
= old_loc2
;
1450 if (gfc_match_char (')') != MATCH_YES
)
1452 gfc_error ("Syntax error in IF-expression at %C");
1453 gfc_free_expr (expr
);
1457 m
= gfc_match (" %l , %l , %l%t", &l1
, &l2
, &l3
);
1463 gfc_error ("Block label not appropriate for arithmetic IF "
1465 gfc_free_expr (expr
);
1469 if (gfc_reference_st_label (l1
, ST_LABEL_TARGET
) == FAILURE
1470 || gfc_reference_st_label (l2
, ST_LABEL_TARGET
) == FAILURE
1471 || gfc_reference_st_label (l3
, ST_LABEL_TARGET
) == FAILURE
)
1473 gfc_free_expr (expr
);
1477 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: Arithmetic IF "
1478 "statement at %C") == FAILURE
)
1481 new_st
.op
= EXEC_ARITHMETIC_IF
;
1482 new_st
.expr1
= expr
;
1487 *if_type
= ST_ARITHMETIC_IF
;
1491 if (gfc_match (" then%t") == MATCH_YES
)
1493 new_st
.op
= EXEC_IF
;
1494 new_st
.expr1
= expr
;
1495 *if_type
= ST_IF_BLOCK
;
1501 gfc_error ("Block label is not appropriate for IF statement at %C");
1502 gfc_free_expr (expr
);
1506 /* At this point the only thing left is a simple IF statement. At
1507 this point, n has to be MATCH_NO, so we don't have to worry about
1508 re-matching a block label. From what we've got so far, try
1509 matching an assignment. */
1511 *if_type
= ST_SIMPLE_IF
;
1513 m
= gfc_match_assignment ();
1517 gfc_free_expr (expr
);
1518 gfc_undo_symbols ();
1519 gfc_current_locus
= old_loc
;
1521 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1522 assignment was found. For MATCH_NO, continue to call the various
1524 if (m
== MATCH_ERROR
)
1527 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match. */
1529 m
= gfc_match_pointer_assignment ();
1533 gfc_free_expr (expr
);
1534 gfc_undo_symbols ();
1535 gfc_current_locus
= old_loc
;
1537 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match. */
1539 /* Look at the next keyword to see which matcher to call. Matching
1540 the keyword doesn't affect the symbol table, so we don't have to
1541 restore between tries. */
1543 #define match(string, subr, statement) \
1544 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1548 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
)
1549 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
)
1550 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
)
1551 match ("call", gfc_match_call
, ST_CALL
)
1552 match ("close", gfc_match_close
, ST_CLOSE
)
1553 match ("continue", gfc_match_continue
, ST_CONTINUE
)
1554 match ("cycle", gfc_match_cycle
, ST_CYCLE
)
1555 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
)
1556 match ("end file", gfc_match_endfile
, ST_END_FILE
)
1557 match ("error stop", gfc_match_error_stop
, ST_ERROR_STOP
)
1558 match ("exit", gfc_match_exit
, ST_EXIT
)
1559 match ("flush", gfc_match_flush
, ST_FLUSH
)
1560 match ("forall", match_simple_forall
, ST_FORALL
)
1561 match ("go to", gfc_match_goto
, ST_GOTO
)
1562 match ("if", match_arithmetic_if
, ST_ARITHMETIC_IF
)
1563 match ("inquire", gfc_match_inquire
, ST_INQUIRE
)
1564 match ("nullify", gfc_match_nullify
, ST_NULLIFY
)
1565 match ("open", gfc_match_open
, ST_OPEN
)
1566 match ("pause", gfc_match_pause
, ST_NONE
)
1567 match ("print", gfc_match_print
, ST_WRITE
)
1568 match ("read", gfc_match_read
, ST_READ
)
1569 match ("return", gfc_match_return
, ST_RETURN
)
1570 match ("rewind", gfc_match_rewind
, ST_REWIND
)
1571 match ("stop", gfc_match_stop
, ST_STOP
)
1572 match ("wait", gfc_match_wait
, ST_WAIT
)
1573 match ("sync all", gfc_match_sync_all
, ST_SYNC_CALL
);
1574 match ("sync images", gfc_match_sync_images
, ST_SYNC_IMAGES
);
1575 match ("sync memory", gfc_match_sync_memory
, ST_SYNC_MEMORY
);
1576 match ("where", match_simple_where
, ST_WHERE
)
1577 match ("write", gfc_match_write
, ST_WRITE
)
1579 /* The gfc_match_assignment() above may have returned a MATCH_NO
1580 where the assignment was to a named constant. Check that
1581 special case here. */
1582 m
= gfc_match_assignment ();
1585 gfc_error ("Cannot assign to a named constant at %C");
1586 gfc_free_expr (expr
);
1587 gfc_undo_symbols ();
1588 gfc_current_locus
= old_loc
;
1592 /* All else has failed, so give up. See if any of the matchers has
1593 stored an error message of some sort. */
1594 if (gfc_error_check () == 0)
1595 gfc_error ("Unclassifiable statement in IF-clause at %C");
1597 gfc_free_expr (expr
);
1602 gfc_error ("Syntax error in IF-clause at %C");
1605 gfc_free_expr (expr
);
1609 /* At this point, we've matched the single IF and the action clause
1610 is in new_st. Rearrange things so that the IF statement appears
1613 p
= gfc_get_code ();
1614 p
->next
= gfc_get_code ();
1616 p
->next
->loc
= gfc_current_locus
;
1621 gfc_clear_new_st ();
1623 new_st
.op
= EXEC_IF
;
1632 /* Match an ELSE statement. */
1635 gfc_match_else (void)
1637 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1639 if (gfc_match_eos () == MATCH_YES
)
1642 if (gfc_match_name (name
) != MATCH_YES
1643 || gfc_current_block () == NULL
1644 || gfc_match_eos () != MATCH_YES
)
1646 gfc_error ("Unexpected junk after ELSE statement at %C");
1650 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1652 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1653 name
, gfc_current_block ()->name
);
1661 /* Match an ELSE IF statement. */
1664 gfc_match_elseif (void)
1666 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1670 m
= gfc_match (" ( %e ) then", &expr
);
1674 if (gfc_match_eos () == MATCH_YES
)
1677 if (gfc_match_name (name
) != MATCH_YES
1678 || gfc_current_block () == NULL
1679 || gfc_match_eos () != MATCH_YES
)
1681 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1685 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1687 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1688 name
, gfc_current_block ()->name
);
1693 new_st
.op
= EXEC_IF
;
1694 new_st
.expr1
= expr
;
1698 gfc_free_expr (expr
);
1703 /* Free a gfc_iterator structure. */
1706 gfc_free_iterator (gfc_iterator
*iter
, int flag
)
1712 gfc_free_expr (iter
->var
);
1713 gfc_free_expr (iter
->start
);
1714 gfc_free_expr (iter
->end
);
1715 gfc_free_expr (iter
->step
);
1722 /* Match a CRITICAL statement. */
1724 gfc_match_critical (void)
1726 gfc_st_label
*label
= NULL
;
1728 if (gfc_match_label () == MATCH_ERROR
)
1731 if (gfc_match (" critical") != MATCH_YES
)
1734 if (gfc_match_st_label (&label
) == MATCH_ERROR
)
1737 if (gfc_match_eos () != MATCH_YES
)
1739 gfc_syntax_error (ST_CRITICAL
);
1743 if (gfc_pure (NULL
))
1745 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1749 if (gfc_implicit_pure (NULL
))
1750 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
1752 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: CRITICAL statement at %C")
1756 if (gfc_option
.coarray
== GFC_FCOARRAY_NONE
)
1758 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
1762 if (gfc_find_state (COMP_CRITICAL
) == SUCCESS
)
1764 gfc_error ("Nested CRITICAL block at %C");
1768 new_st
.op
= EXEC_CRITICAL
;
1771 && gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
1778 /* Match a BLOCK statement. */
1781 gfc_match_block (void)
1785 if (gfc_match_label () == MATCH_ERROR
)
1788 if (gfc_match (" block") != MATCH_YES
)
1791 /* For this to be a correct BLOCK statement, the line must end now. */
1792 m
= gfc_match_eos ();
1793 if (m
== MATCH_ERROR
)
1802 /* Match an ASSOCIATE statement. */
1805 gfc_match_associate (void)
1807 if (gfc_match_label () == MATCH_ERROR
)
1810 if (gfc_match (" associate") != MATCH_YES
)
1813 /* Match the association list. */
1814 if (gfc_match_char ('(') != MATCH_YES
)
1816 gfc_error ("Expected association list at %C");
1819 new_st
.ext
.block
.assoc
= NULL
;
1822 gfc_association_list
* newAssoc
= gfc_get_association_list ();
1823 gfc_association_list
* a
;
1825 /* Match the next association. */
1826 if (gfc_match (" %n => %e", newAssoc
->name
, &newAssoc
->target
)
1829 gfc_error ("Expected association at %C");
1830 goto assocListError
;
1832 newAssoc
->where
= gfc_current_locus
;
1834 /* Check that the current name is not yet in the list. */
1835 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
1836 if (!strcmp (a
->name
, newAssoc
->name
))
1838 gfc_error ("Duplicate name '%s' in association at %C",
1840 goto assocListError
;
1843 /* The target expression must not be coindexed. */
1844 if (gfc_is_coindexed (newAssoc
->target
))
1846 gfc_error ("Association target at %C must not be coindexed");
1847 goto assocListError
;
1850 /* The `variable' field is left blank for now; because the target is not
1851 yet resolved, we can't use gfc_has_vector_subscript to determine it
1852 for now. This is set during resolution. */
1854 /* Put it into the list. */
1855 newAssoc
->next
= new_st
.ext
.block
.assoc
;
1856 new_st
.ext
.block
.assoc
= newAssoc
;
1858 /* Try next one or end if closing parenthesis is found. */
1859 gfc_gobble_whitespace ();
1860 if (gfc_peek_char () == ')')
1862 if (gfc_match_char (',') != MATCH_YES
)
1864 gfc_error ("Expected ')' or ',' at %C");
1871 gfc_free (newAssoc
);
1874 if (gfc_match_char (')') != MATCH_YES
)
1876 /* This should never happen as we peek above. */
1880 if (gfc_match_eos () != MATCH_YES
)
1882 gfc_error ("Junk after ASSOCIATE statement at %C");
1889 gfc_free_association_list (new_st
.ext
.block
.assoc
);
1894 /* Match a DO statement. */
1899 gfc_iterator iter
, *ip
;
1901 gfc_st_label
*label
;
1904 old_loc
= gfc_current_locus
;
1907 iter
.var
= iter
.start
= iter
.end
= iter
.step
= NULL
;
1909 m
= gfc_match_label ();
1910 if (m
== MATCH_ERROR
)
1913 if (gfc_match (" do") != MATCH_YES
)
1916 m
= gfc_match_st_label (&label
);
1917 if (m
== MATCH_ERROR
)
1920 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1922 if (gfc_match_eos () == MATCH_YES
)
1924 iter
.end
= gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, true);
1925 new_st
.op
= EXEC_DO_WHILE
;
1929 /* Match an optional comma, if no comma is found, a space is obligatory. */
1930 if (gfc_match_char (',') != MATCH_YES
&& gfc_match ("% ") != MATCH_YES
)
1933 /* Check for balanced parens. */
1935 if (gfc_match_parens () == MATCH_ERROR
)
1938 /* See if we have a DO WHILE. */
1939 if (gfc_match (" while ( %e )%t", &iter
.end
) == MATCH_YES
)
1941 new_st
.op
= EXEC_DO_WHILE
;
1945 /* The abortive DO WHILE may have done something to the symbol
1946 table, so we start over. */
1947 gfc_undo_symbols ();
1948 gfc_current_locus
= old_loc
;
1950 gfc_match_label (); /* This won't error. */
1951 gfc_match (" do "); /* This will work. */
1953 gfc_match_st_label (&label
); /* Can't error out. */
1954 gfc_match_char (','); /* Optional comma. */
1956 m
= gfc_match_iterator (&iter
, 0);
1959 if (m
== MATCH_ERROR
)
1962 iter
.var
->symtree
->n
.sym
->attr
.implied_index
= 0;
1963 gfc_check_do_variable (iter
.var
->symtree
);
1965 if (gfc_match_eos () != MATCH_YES
)
1967 gfc_syntax_error (ST_DO
);
1971 new_st
.op
= EXEC_DO
;
1975 && gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
1978 new_st
.label1
= label
;
1980 if (new_st
.op
== EXEC_DO_WHILE
)
1981 new_st
.expr1
= iter
.end
;
1984 new_st
.ext
.iterator
= ip
= gfc_get_iterator ();
1991 gfc_free_iterator (&iter
, 0);
1997 /* Match an EXIT or CYCLE statement. */
2000 match_exit_cycle (gfc_statement st
, gfc_exec_op op
)
2002 gfc_state_data
*p
, *o
;
2007 if (gfc_match_eos () == MATCH_YES
)
2011 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2014 m
= gfc_match ("% %n%t", name
);
2015 if (m
== MATCH_ERROR
)
2019 gfc_syntax_error (st
);
2023 /* Find the corresponding symbol. If there's a BLOCK statement
2024 between here and the label, it is not in gfc_current_ns but a parent
2026 stree
= gfc_find_symtree_in_proc (name
, gfc_current_ns
);
2029 gfc_error ("Name '%s' in %s statement at %C is unknown",
2030 name
, gfc_ascii_statement (st
));
2035 if (sym
->attr
.flavor
!= FL_LABEL
)
2037 gfc_error ("Name '%s' in %s statement at %C is not a construct name",
2038 name
, gfc_ascii_statement (st
));
2043 /* Find the loop specified by the label (or lack of a label). */
2044 for (o
= NULL
, p
= gfc_state_stack
; p
; p
= p
->previous
)
2045 if (o
== NULL
&& p
->state
== COMP_OMP_STRUCTURED_BLOCK
)
2047 else if (p
->state
== COMP_CRITICAL
)
2049 gfc_error("%s statement at %C leaves CRITICAL construct",
2050 gfc_ascii_statement (st
));
2053 else if ((sym
&& sym
== p
->sym
) || (!sym
&& p
->state
== COMP_DO
))
2059 gfc_error ("%s statement at %C is not within a construct",
2060 gfc_ascii_statement (st
));
2062 gfc_error ("%s statement at %C is not within construct '%s'",
2063 gfc_ascii_statement (st
), sym
->name
);
2068 /* Special checks for EXIT from non-loop constructs. */
2075 /* This is already handled above. */
2078 case COMP_ASSOCIATE
:
2082 case COMP_SELECT_TYPE
:
2084 if (op
== EXEC_CYCLE
)
2086 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2087 " construct '%s'", sym
->name
);
2090 gcc_assert (op
== EXEC_EXIT
);
2091 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: EXIT statement with no"
2092 " do-construct-name at %C") == FAILURE
)
2097 gfc_error ("%s statement at %C is not applicable to construct '%s'",
2098 gfc_ascii_statement (st
), sym
->name
);
2104 gfc_error ("%s statement at %C leaving OpenMP structured block",
2105 gfc_ascii_statement (st
));
2109 for (o
= p
, cnt
= 0; o
->state
== COMP_DO
&& o
->previous
!= NULL
; cnt
++)
2113 && o
->state
== COMP_OMP_STRUCTURED_BLOCK
2114 && (o
->head
->op
== EXEC_OMP_DO
2115 || o
->head
->op
== EXEC_OMP_PARALLEL_DO
))
2118 gcc_assert (o
->head
->next
!= NULL
2119 && (o
->head
->next
->op
== EXEC_DO
2120 || o
->head
->next
->op
== EXEC_DO_WHILE
)
2121 && o
->previous
!= NULL
2122 && o
->previous
->tail
->op
== o
->head
->op
);
2123 if (o
->previous
->tail
->ext
.omp_clauses
!= NULL
2124 && o
->previous
->tail
->ext
.omp_clauses
->collapse
> 1)
2125 collapse
= o
->previous
->tail
->ext
.omp_clauses
->collapse
;
2126 if (st
== ST_EXIT
&& cnt
<= collapse
)
2128 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2131 if (st
== ST_CYCLE
&& cnt
< collapse
)
2133 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2139 /* Save the first statement in the construct - needed by the backend. */
2140 new_st
.ext
.which_construct
= p
->construct
;
2148 /* Match the EXIT statement. */
2151 gfc_match_exit (void)
2153 return match_exit_cycle (ST_EXIT
, EXEC_EXIT
);
2157 /* Match the CYCLE statement. */
2160 gfc_match_cycle (void)
2162 return match_exit_cycle (ST_CYCLE
, EXEC_CYCLE
);
2166 /* Match a number or character constant after an (ALL) STOP or PAUSE statement. */
2169 gfc_match_stopcode (gfc_statement st
)
2176 if (gfc_match_eos () != MATCH_YES
)
2178 m
= gfc_match_init_expr (&e
);
2179 if (m
== MATCH_ERROR
)
2184 if (gfc_match_eos () != MATCH_YES
)
2188 if (gfc_pure (NULL
))
2190 gfc_error ("%s statement not allowed in PURE procedure at %C",
2191 gfc_ascii_statement (st
));
2195 if (gfc_implicit_pure (NULL
))
2196 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
2198 if (st
== ST_STOP
&& gfc_find_state (COMP_CRITICAL
) == SUCCESS
)
2200 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2206 if (!(e
->ts
.type
== BT_CHARACTER
|| e
->ts
.type
== BT_INTEGER
))
2208 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2215 gfc_error ("STOP code at %L must be scalar",
2220 if (e
->ts
.type
== BT_CHARACTER
2221 && e
->ts
.kind
!= gfc_default_character_kind
)
2223 gfc_error ("STOP code at %L must be default character KIND=%d",
2224 &e
->where
, (int) gfc_default_character_kind
);
2228 if (e
->ts
.type
== BT_INTEGER
2229 && e
->ts
.kind
!= gfc_default_integer_kind
)
2231 gfc_error ("STOP code at %L must be default integer KIND=%d",
2232 &e
->where
, (int) gfc_default_integer_kind
);
2240 new_st
.op
= EXEC_STOP
;
2243 new_st
.op
= EXEC_ERROR_STOP
;
2246 new_st
.op
= EXEC_PAUSE
;
2253 new_st
.ext
.stop_code
= -1;
2258 gfc_syntax_error (st
);
2267 /* Match the (deprecated) PAUSE statement. */
2270 gfc_match_pause (void)
2274 m
= gfc_match_stopcode (ST_PAUSE
);
2277 if (gfc_notify_std (GFC_STD_F95_DEL
, "Deleted feature: PAUSE statement"
2286 /* Match the STOP statement. */
2289 gfc_match_stop (void)
2291 return gfc_match_stopcode (ST_STOP
);
2295 /* Match the ERROR STOP statement. */
2298 gfc_match_error_stop (void)
2300 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: ERROR STOP statement at %C")
2304 return gfc_match_stopcode (ST_ERROR_STOP
);
2308 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
2309 SYNC ALL [(sync-stat-list)]
2310 SYNC MEMORY [(sync-stat-list)]
2311 SYNC IMAGES (image-set [, sync-stat-list] )
2312 with sync-stat is int-expr or *. */
2315 sync_statement (gfc_statement st
)
2318 gfc_expr
*tmp
, *imageset
, *stat
, *errmsg
;
2319 bool saw_stat
, saw_errmsg
;
2321 tmp
= imageset
= stat
= errmsg
= NULL
;
2322 saw_stat
= saw_errmsg
= false;
2324 if (gfc_pure (NULL
))
2326 gfc_error ("Image control statement SYNC at %C in PURE procedure");
2330 if (gfc_implicit_pure (NULL
))
2331 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
2333 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: SYNC statement at %C")
2337 if (gfc_option
.coarray
== GFC_FCOARRAY_NONE
)
2339 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
2343 if (gfc_find_state (COMP_CRITICAL
) == SUCCESS
)
2345 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
2349 if (gfc_match_eos () == MATCH_YES
)
2351 if (st
== ST_SYNC_IMAGES
)
2356 if (gfc_match_char ('(') != MATCH_YES
)
2359 if (st
== ST_SYNC_IMAGES
)
2361 /* Denote '*' as imageset == NULL. */
2362 m
= gfc_match_char ('*');
2363 if (m
== MATCH_ERROR
)
2367 if (gfc_match ("%e", &imageset
) != MATCH_YES
)
2370 m
= gfc_match_char (',');
2371 if (m
== MATCH_ERROR
)
2375 m
= gfc_match_char (')');
2384 m
= gfc_match (" stat = %v", &tmp
);
2385 if (m
== MATCH_ERROR
)
2391 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
2397 if (gfc_match_char (',') == MATCH_YES
)
2401 m
= gfc_match (" errmsg = %v", &tmp
);
2402 if (m
== MATCH_ERROR
)
2408 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
2414 if (gfc_match_char (',') == MATCH_YES
)
2418 gfc_gobble_whitespace ();
2420 if (gfc_peek_char () == ')')
2426 if (gfc_match (" )%t") != MATCH_YES
)
2433 new_st
.op
= EXEC_SYNC_ALL
;
2435 case ST_SYNC_IMAGES
:
2436 new_st
.op
= EXEC_SYNC_IMAGES
;
2438 case ST_SYNC_MEMORY
:
2439 new_st
.op
= EXEC_SYNC_MEMORY
;
2445 new_st
.expr1
= imageset
;
2446 new_st
.expr2
= stat
;
2447 new_st
.expr3
= errmsg
;
2452 gfc_syntax_error (st
);
2455 gfc_free_expr (tmp
);
2456 gfc_free_expr (imageset
);
2457 gfc_free_expr (stat
);
2458 gfc_free_expr (errmsg
);
2464 /* Match SYNC ALL statement. */
2467 gfc_match_sync_all (void)
2469 return sync_statement (ST_SYNC_ALL
);
2473 /* Match SYNC IMAGES statement. */
2476 gfc_match_sync_images (void)
2478 return sync_statement (ST_SYNC_IMAGES
);
2482 /* Match SYNC MEMORY statement. */
2485 gfc_match_sync_memory (void)
2487 return sync_statement (ST_SYNC_MEMORY
);
2491 /* Match a CONTINUE statement. */
2494 gfc_match_continue (void)
2496 if (gfc_match_eos () != MATCH_YES
)
2498 gfc_syntax_error (ST_CONTINUE
);
2502 new_st
.op
= EXEC_CONTINUE
;
2507 /* Match the (deprecated) ASSIGN statement. */
2510 gfc_match_assign (void)
2513 gfc_st_label
*label
;
2515 if (gfc_match (" %l", &label
) == MATCH_YES
)
2517 if (gfc_reference_st_label (label
, ST_LABEL_UNKNOWN
) == FAILURE
)
2519 if (gfc_match (" to %v%t", &expr
) == MATCH_YES
)
2521 if (gfc_notify_std (GFC_STD_F95_DEL
, "Deleted feature: ASSIGN "
2526 expr
->symtree
->n
.sym
->attr
.assign
= 1;
2528 new_st
.op
= EXEC_LABEL_ASSIGN
;
2529 new_st
.label1
= label
;
2530 new_st
.expr1
= expr
;
2538 /* Match the GO TO statement. As a computed GOTO statement is
2539 matched, it is transformed into an equivalent SELECT block. No
2540 tree is necessary, and the resulting jumps-to-jumps are
2541 specifically optimized away by the back end. */
2544 gfc_match_goto (void)
2546 gfc_code
*head
, *tail
;
2549 gfc_st_label
*label
;
2553 if (gfc_match (" %l%t", &label
) == MATCH_YES
)
2555 if (gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
2558 new_st
.op
= EXEC_GOTO
;
2559 new_st
.label1
= label
;
2563 /* The assigned GO TO statement. */
2565 if (gfc_match_variable (&expr
, 0) == MATCH_YES
)
2567 if (gfc_notify_std (GFC_STD_F95_DEL
, "Deleted feature: Assigned GOTO "
2572 new_st
.op
= EXEC_GOTO
;
2573 new_st
.expr1
= expr
;
2575 if (gfc_match_eos () == MATCH_YES
)
2578 /* Match label list. */
2579 gfc_match_char (',');
2580 if (gfc_match_char ('(') != MATCH_YES
)
2582 gfc_syntax_error (ST_GOTO
);
2589 m
= gfc_match_st_label (&label
);
2593 if (gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
2597 head
= tail
= gfc_get_code ();
2600 tail
->block
= gfc_get_code ();
2604 tail
->label1
= label
;
2605 tail
->op
= EXEC_GOTO
;
2607 while (gfc_match_char (',') == MATCH_YES
);
2609 if (gfc_match (")%t") != MATCH_YES
)
2614 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2617 new_st
.block
= head
;
2622 /* Last chance is a computed GO TO statement. */
2623 if (gfc_match_char ('(') != MATCH_YES
)
2625 gfc_syntax_error (ST_GOTO
);
2634 m
= gfc_match_st_label (&label
);
2638 if (gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
2642 head
= tail
= gfc_get_code ();
2645 tail
->block
= gfc_get_code ();
2649 cp
= gfc_get_case ();
2650 cp
->low
= cp
->high
= gfc_get_int_expr (gfc_default_integer_kind
,
2653 tail
->op
= EXEC_SELECT
;
2654 tail
->ext
.block
.case_list
= cp
;
2656 tail
->next
= gfc_get_code ();
2657 tail
->next
->op
= EXEC_GOTO
;
2658 tail
->next
->label1
= label
;
2660 while (gfc_match_char (',') == MATCH_YES
);
2662 if (gfc_match_char (')') != MATCH_YES
)
2667 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2671 /* Get the rest of the statement. */
2672 gfc_match_char (',');
2674 if (gfc_match (" %e%t", &expr
) != MATCH_YES
)
2677 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: Computed GOTO "
2678 "at %C") == FAILURE
)
2681 /* At this point, a computed GOTO has been fully matched and an
2682 equivalent SELECT statement constructed. */
2684 new_st
.op
= EXEC_SELECT
;
2685 new_st
.expr1
= NULL
;
2687 /* Hack: For a "real" SELECT, the expression is in expr. We put
2688 it in expr2 so we can distinguish then and produce the correct
2690 new_st
.expr2
= expr
;
2691 new_st
.block
= head
;
2695 gfc_syntax_error (ST_GOTO
);
2697 gfc_free_statements (head
);
2702 /* Frees a list of gfc_alloc structures. */
2705 gfc_free_alloc_list (gfc_alloc
*p
)
2712 gfc_free_expr (p
->expr
);
2718 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
2719 an accessible derived type. */
2722 match_derived_type_spec (gfc_typespec
*ts
)
2724 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2726 gfc_symbol
*derived
;
2728 old_locus
= gfc_current_locus
;
2730 if (gfc_match ("%n", name
) != MATCH_YES
)
2732 gfc_current_locus
= old_locus
;
2736 gfc_find_symbol (name
, NULL
, 1, &derived
);
2738 if (derived
&& derived
->attr
.flavor
== FL_DERIVED
)
2740 ts
->type
= BT_DERIVED
;
2741 ts
->u
.derived
= derived
;
2745 gfc_current_locus
= old_locus
;
2750 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2751 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
2752 It only includes the intrinsic types from the Fortran 2003 standard
2753 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2754 the implicit_flag is not needed, so it was removed. Derived types are
2755 identified by their name alone. */
2758 match_type_spec (gfc_typespec
*ts
)
2764 gfc_gobble_whitespace ();
2765 old_locus
= gfc_current_locus
;
2767 if (match_derived_type_spec (ts
) == MATCH_YES
)
2769 /* Enforce F03:C401. */
2770 if (ts
->u
.derived
->attr
.abstract
)
2772 gfc_error ("Derived type '%s' at %L may not be ABSTRACT",
2773 ts
->u
.derived
->name
, &old_locus
);
2779 if (gfc_match ("integer") == MATCH_YES
)
2781 ts
->type
= BT_INTEGER
;
2782 ts
->kind
= gfc_default_integer_kind
;
2786 if (gfc_match ("real") == MATCH_YES
)
2789 ts
->kind
= gfc_default_real_kind
;
2793 if (gfc_match ("double precision") == MATCH_YES
)
2796 ts
->kind
= gfc_default_double_kind
;
2800 if (gfc_match ("complex") == MATCH_YES
)
2802 ts
->type
= BT_COMPLEX
;
2803 ts
->kind
= gfc_default_complex_kind
;
2807 if (gfc_match ("character") == MATCH_YES
)
2809 ts
->type
= BT_CHARACTER
;
2811 m
= gfc_match_char_spec (ts
);
2819 if (gfc_match ("logical") == MATCH_YES
)
2821 ts
->type
= BT_LOGICAL
;
2822 ts
->kind
= gfc_default_logical_kind
;
2826 /* If a type is not matched, simply return MATCH_NO. */
2827 gfc_current_locus
= old_locus
;
2832 gfc_gobble_whitespace ();
2833 if (gfc_peek_ascii_char () == '*')
2835 gfc_error ("Invalid type-spec at %C");
2839 m
= gfc_match_kind_spec (ts
, false);
2842 m
= MATCH_YES
; /* No kind specifier found. */
2848 /* Match an ALLOCATE statement. */
2851 gfc_match_allocate (void)
2853 gfc_alloc
*head
, *tail
;
2854 gfc_expr
*stat
, *errmsg
, *tmp
, *source
, *mold
;
2858 locus old_locus
, deferred_locus
;
2859 bool saw_stat
, saw_errmsg
, saw_source
, saw_mold
, saw_deferred
, b1
, b2
, b3
;
2862 stat
= errmsg
= source
= mold
= tmp
= NULL
;
2863 saw_stat
= saw_errmsg
= saw_source
= saw_mold
= saw_deferred
= false;
2865 if (gfc_match_char ('(') != MATCH_YES
)
2868 /* Match an optional type-spec. */
2869 old_locus
= gfc_current_locus
;
2870 m
= match_type_spec (&ts
);
2871 if (m
== MATCH_ERROR
)
2873 else if (m
== MATCH_NO
)
2875 char name
[GFC_MAX_SYMBOL_LEN
+ 3];
2877 if (gfc_match ("%n :: ", name
) == MATCH_YES
)
2879 gfc_error ("Error in type-spec at %L", &old_locus
);
2883 ts
.type
= BT_UNKNOWN
;
2887 if (gfc_match (" :: ") == MATCH_YES
)
2889 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: typespec in "
2890 "ALLOCATE at %L", &old_locus
) == FAILURE
)
2895 gfc_error ("Type-spec at %L cannot contain a deferred "
2896 "type parameter", &old_locus
);
2902 ts
.type
= BT_UNKNOWN
;
2903 gfc_current_locus
= old_locus
;
2910 head
= tail
= gfc_get_alloc ();
2913 tail
->next
= gfc_get_alloc ();
2917 m
= gfc_match_variable (&tail
->expr
, 0);
2920 if (m
== MATCH_ERROR
)
2923 if (gfc_check_do_variable (tail
->expr
->symtree
))
2926 if (gfc_pure (NULL
) && gfc_impure_variable (tail
->expr
->symtree
->n
.sym
))
2928 gfc_error ("Bad allocate-object at %C for a PURE procedure");
2932 if (gfc_implicit_pure (NULL
)
2933 && gfc_impure_variable (tail
->expr
->symtree
->n
.sym
))
2934 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
2936 if (tail
->expr
->ts
.deferred
)
2938 saw_deferred
= true;
2939 deferred_locus
= tail
->expr
->where
;
2942 /* The ALLOCATE statement had an optional typespec. Check the
2944 if (ts
.type
!= BT_UNKNOWN
)
2946 /* Enforce F03:C624. */
2947 if (!gfc_type_compatible (&tail
->expr
->ts
, &ts
))
2949 gfc_error ("Type of entity at %L is type incompatible with "
2950 "typespec", &tail
->expr
->where
);
2954 /* Enforce F03:C627. */
2955 if (ts
.kind
!= tail
->expr
->ts
.kind
)
2957 gfc_error ("Kind type parameter for entity at %L differs from "
2958 "the kind type parameter of the typespec",
2959 &tail
->expr
->where
);
2964 if (tail
->expr
->ts
.type
== BT_DERIVED
)
2965 tail
->expr
->ts
.u
.derived
= gfc_use_derived (tail
->expr
->ts
.u
.derived
);
2967 /* FIXME: disable the checking on derived types and arrays. */
2968 sym
= tail
->expr
->symtree
->n
.sym
;
2969 b1
= !(tail
->expr
->ref
2970 && (tail
->expr
->ref
->type
== REF_COMPONENT
2971 || tail
->expr
->ref
->type
== REF_ARRAY
));
2972 if (sym
&& sym
->ts
.type
== BT_CLASS
&& sym
->attr
.class_ok
)
2973 b2
= !(CLASS_DATA (sym
)->attr
.allocatable
2974 || CLASS_DATA (sym
)->attr
.class_pointer
);
2976 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
2977 || sym
->attr
.proc_pointer
);
2978 b3
= sym
&& sym
->ns
&& sym
->ns
->proc_name
2979 && (sym
->ns
->proc_name
->attr
.allocatable
2980 || sym
->ns
->proc_name
->attr
.pointer
2981 || sym
->ns
->proc_name
->attr
.proc_pointer
);
2982 if (b1
&& b2
&& !b3
)
2984 gfc_error ("Allocate-object at %L is not a nonprocedure pointer "
2985 "or an allocatable variable", &tail
->expr
->where
);
2989 if (gfc_peek_ascii_char () == '(' && !sym
->attr
.dimension
)
2991 gfc_error ("Shape specification for allocatable scalar at %C");
2995 if (gfc_match_char (',') != MATCH_YES
)
3000 m
= gfc_match (" stat = %v", &tmp
);
3001 if (m
== MATCH_ERROR
)
3008 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
3016 if (gfc_check_do_variable (stat
->symtree
))
3019 if (gfc_match_char (',') == MATCH_YES
)
3020 goto alloc_opt_list
;
3023 m
= gfc_match (" errmsg = %v", &tmp
);
3024 if (m
== MATCH_ERROR
)
3028 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ERRMSG tag at %L",
3029 &tmp
->where
) == FAILURE
)
3035 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
3043 if (gfc_match_char (',') == MATCH_YES
)
3044 goto alloc_opt_list
;
3047 m
= gfc_match (" source = %e", &tmp
);
3048 if (m
== MATCH_ERROR
)
3052 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: SOURCE tag at %L",
3053 &tmp
->where
) == FAILURE
)
3059 gfc_error ("Redundant SOURCE tag found at %L ", &tmp
->where
);
3063 /* The next 2 conditionals check C631. */
3064 if (ts
.type
!= BT_UNKNOWN
)
3066 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
3067 &tmp
->where
, &old_locus
);
3073 gfc_error ("SOURCE tag at %L requires only a single entity in "
3074 "the allocation-list", &tmp
->where
);
3082 if (gfc_match_char (',') == MATCH_YES
)
3083 goto alloc_opt_list
;
3086 m
= gfc_match (" mold = %e", &tmp
);
3087 if (m
== MATCH_ERROR
)
3091 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: MOLD tag at %L",
3092 &tmp
->where
) == FAILURE
)
3095 /* Check F08:C636. */
3098 gfc_error ("Redundant MOLD tag found at %L ", &tmp
->where
);
3102 /* Check F08:C637. */
3103 if (ts
.type
!= BT_UNKNOWN
)
3105 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
3106 &tmp
->where
, &old_locus
);
3115 if (gfc_match_char (',') == MATCH_YES
)
3116 goto alloc_opt_list
;
3119 gfc_gobble_whitespace ();
3121 if (gfc_peek_char () == ')')
3125 if (gfc_match (" )%t") != MATCH_YES
)
3128 /* Check F08:C637. */
3131 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
3132 &mold
->where
, &source
->where
);
3136 /* Check F03:C623, */
3137 if (saw_deferred
&& ts
.type
== BT_UNKNOWN
&& !source
&& !mold
)
3139 gfc_error ("Allocate-object at %L with a deferred type parameter "
3140 "requires either a type-spec or SOURCE tag or a MOLD tag",
3145 new_st
.op
= EXEC_ALLOCATE
;
3146 new_st
.expr1
= stat
;
3147 new_st
.expr2
= errmsg
;
3149 new_st
.expr3
= source
;
3151 new_st
.expr3
= mold
;
3152 new_st
.ext
.alloc
.list
= head
;
3153 new_st
.ext
.alloc
.ts
= ts
;
3158 gfc_syntax_error (ST_ALLOCATE
);
3161 gfc_free_expr (errmsg
);
3162 gfc_free_expr (source
);
3163 gfc_free_expr (stat
);
3164 gfc_free_expr (mold
);
3165 if (tmp
&& tmp
->expr_type
) gfc_free_expr (tmp
);
3166 gfc_free_alloc_list (head
);
3171 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
3172 a set of pointer assignments to intrinsic NULL(). */
3175 gfc_match_nullify (void)
3183 if (gfc_match_char ('(') != MATCH_YES
)
3188 m
= gfc_match_variable (&p
, 0);
3189 if (m
== MATCH_ERROR
)
3194 if (gfc_check_do_variable (p
->symtree
))
3197 /* build ' => NULL() '. */
3198 e
= gfc_get_null_expr (&gfc_current_locus
);
3200 /* Chain to list. */
3205 tail
->next
= gfc_get_code ();
3209 tail
->op
= EXEC_POINTER_ASSIGN
;
3213 if (gfc_match (" )%t") == MATCH_YES
)
3215 if (gfc_match_char (',') != MATCH_YES
)
3222 gfc_syntax_error (ST_NULLIFY
);
3225 gfc_free_statements (new_st
.next
);
3227 gfc_free_expr (new_st
.expr1
);
3228 new_st
.expr1
= NULL
;
3229 gfc_free_expr (new_st
.expr2
);
3230 new_st
.expr2
= NULL
;
3235 /* Match a DEALLOCATE statement. */
3238 gfc_match_deallocate (void)
3240 gfc_alloc
*head
, *tail
;
3241 gfc_expr
*stat
, *errmsg
, *tmp
;
3244 bool saw_stat
, saw_errmsg
, b1
, b2
;
3247 stat
= errmsg
= tmp
= NULL
;
3248 saw_stat
= saw_errmsg
= false;
3250 if (gfc_match_char ('(') != MATCH_YES
)
3256 head
= tail
= gfc_get_alloc ();
3259 tail
->next
= gfc_get_alloc ();
3263 m
= gfc_match_variable (&tail
->expr
, 0);
3264 if (m
== MATCH_ERROR
)
3269 if (gfc_check_do_variable (tail
->expr
->symtree
))
3272 sym
= tail
->expr
->symtree
->n
.sym
;
3274 if (gfc_pure (NULL
) && gfc_impure_variable (sym
))
3276 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
3280 if (gfc_implicit_pure (NULL
) && gfc_impure_variable (sym
))
3281 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
3283 /* FIXME: disable the checking on derived types. */
3284 b1
= !(tail
->expr
->ref
3285 && (tail
->expr
->ref
->type
== REF_COMPONENT
3286 || tail
->expr
->ref
->type
== REF_ARRAY
));
3287 if (sym
&& sym
->ts
.type
== BT_CLASS
)
3288 b2
= !(CLASS_DATA (sym
)->attr
.allocatable
3289 || CLASS_DATA (sym
)->attr
.class_pointer
);
3291 b2
= sym
&& !(sym
->attr
.allocatable
|| sym
->attr
.pointer
3292 || sym
->attr
.proc_pointer
);
3295 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
3296 "or an allocatable variable");
3300 if (gfc_match_char (',') != MATCH_YES
)
3305 m
= gfc_match (" stat = %v", &tmp
);
3306 if (m
== MATCH_ERROR
)
3312 gfc_error ("Redundant STAT tag found at %L ", &tmp
->where
);
3313 gfc_free_expr (tmp
);
3320 if (gfc_check_do_variable (stat
->symtree
))
3323 if (gfc_match_char (',') == MATCH_YES
)
3324 goto dealloc_opt_list
;
3327 m
= gfc_match (" errmsg = %v", &tmp
);
3328 if (m
== MATCH_ERROR
)
3332 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ERRMSG at %L",
3333 &tmp
->where
) == FAILURE
)
3338 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp
->where
);
3339 gfc_free_expr (tmp
);
3346 if (gfc_match_char (',') == MATCH_YES
)
3347 goto dealloc_opt_list
;
3350 gfc_gobble_whitespace ();
3352 if (gfc_peek_char () == ')')
3356 if (gfc_match (" )%t") != MATCH_YES
)
3359 new_st
.op
= EXEC_DEALLOCATE
;
3360 new_st
.expr1
= stat
;
3361 new_st
.expr2
= errmsg
;
3362 new_st
.ext
.alloc
.list
= head
;
3367 gfc_syntax_error (ST_DEALLOCATE
);
3370 gfc_free_expr (errmsg
);
3371 gfc_free_expr (stat
);
3372 gfc_free_alloc_list (head
);
3377 /* Match a RETURN statement. */
3380 gfc_match_return (void)
3384 gfc_compile_state s
;
3388 if (gfc_find_state (COMP_CRITICAL
) == SUCCESS
)
3390 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
3394 if (gfc_match_eos () == MATCH_YES
)
3397 if (gfc_find_state (COMP_SUBROUTINE
) == FAILURE
)
3399 gfc_error ("Alternate RETURN statement at %C is only allowed within "
3404 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: Alternate RETURN "
3405 "at %C") == FAILURE
)
3408 if (gfc_current_form
== FORM_FREE
)
3410 /* The following are valid, so we can't require a blank after the
3414 char c
= gfc_peek_ascii_char ();
3415 if (ISALPHA (c
) || ISDIGIT (c
))
3419 m
= gfc_match (" %e%t", &e
);
3422 if (m
== MATCH_ERROR
)
3425 gfc_syntax_error (ST_RETURN
);
3432 gfc_enclosing_unit (&s
);
3433 if (s
== COMP_PROGRAM
3434 && gfc_notify_std (GFC_STD_GNU
, "Extension: RETURN statement in "
3435 "main program at %C") == FAILURE
)
3438 new_st
.op
= EXEC_RETURN
;
3445 /* Match the call of a type-bound procedure, if CALL%var has already been
3446 matched and var found to be a derived-type variable. */
3449 match_typebound_call (gfc_symtree
* varst
)
3454 base
= gfc_get_expr ();
3455 base
->expr_type
= EXPR_VARIABLE
;
3456 base
->symtree
= varst
;
3457 base
->where
= gfc_current_locus
;
3458 gfc_set_sym_referenced (varst
->n
.sym
);
3460 m
= gfc_match_varspec (base
, 0, true, true);
3462 gfc_error ("Expected component reference at %C");
3466 if (gfc_match_eos () != MATCH_YES
)
3468 gfc_error ("Junk after CALL at %C");
3472 if (base
->expr_type
== EXPR_COMPCALL
)
3473 new_st
.op
= EXEC_COMPCALL
;
3474 else if (base
->expr_type
== EXPR_PPC
)
3475 new_st
.op
= EXEC_CALL_PPC
;
3478 gfc_error ("Expected type-bound procedure or procedure pointer component "
3482 new_st
.expr1
= base
;
3488 /* Match a CALL statement. The tricky part here are possible
3489 alternate return specifiers. We handle these by having all
3490 "subroutines" actually return an integer via a register that gives
3491 the return number. If the call specifies alternate returns, we
3492 generate code for a SELECT statement whose case clauses contain
3493 GOTOs to the various labels. */
3496 gfc_match_call (void)
3498 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3499 gfc_actual_arglist
*a
, *arglist
;
3509 m
= gfc_match ("% %n", name
);
3515 if (gfc_get_ha_sym_tree (name
, &st
))
3520 /* If this is a variable of derived-type, it probably starts a type-bound
3522 if ((sym
->attr
.flavor
!= FL_PROCEDURE
3523 || gfc_is_function_return_value (sym
, gfc_current_ns
))
3524 && (sym
->ts
.type
== BT_DERIVED
|| sym
->ts
.type
== BT_CLASS
))
3525 return match_typebound_call (st
);
3527 /* If it does not seem to be callable (include functions so that the
3528 right association is made. They are thrown out in resolution.)
3530 if (!sym
->attr
.generic
3531 && !sym
->attr
.subroutine
3532 && !sym
->attr
.function
)
3534 if (!(sym
->attr
.external
&& !sym
->attr
.referenced
))
3536 /* ...create a symbol in this scope... */
3537 if (sym
->ns
!= gfc_current_ns
3538 && gfc_get_sym_tree (name
, NULL
, &st
, false) == 1)
3541 if (sym
!= st
->n
.sym
)
3545 /* ...and then to try to make the symbol into a subroutine. */
3546 if (gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
3550 gfc_set_sym_referenced (sym
);
3552 if (gfc_match_eos () != MATCH_YES
)
3554 m
= gfc_match_actual_arglist (1, &arglist
);
3557 if (m
== MATCH_ERROR
)
3560 if (gfc_match_eos () != MATCH_YES
)
3564 /* If any alternate return labels were found, construct a SELECT
3565 statement that will jump to the right place. */
3568 for (a
= arglist
; a
; a
= a
->next
)
3569 if (a
->expr
== NULL
)
3574 gfc_symtree
*select_st
;
3575 gfc_symbol
*select_sym
;
3576 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3578 new_st
.next
= c
= gfc_get_code ();
3579 c
->op
= EXEC_SELECT
;
3580 sprintf (name
, "_result_%s", sym
->name
);
3581 gfc_get_ha_sym_tree (name
, &select_st
); /* Can't fail. */
3583 select_sym
= select_st
->n
.sym
;
3584 select_sym
->ts
.type
= BT_INTEGER
;
3585 select_sym
->ts
.kind
= gfc_default_integer_kind
;
3586 gfc_set_sym_referenced (select_sym
);
3587 c
->expr1
= gfc_get_expr ();
3588 c
->expr1
->expr_type
= EXPR_VARIABLE
;
3589 c
->expr1
->symtree
= select_st
;
3590 c
->expr1
->ts
= select_sym
->ts
;
3591 c
->expr1
->where
= gfc_current_locus
;
3594 for (a
= arglist
; a
; a
= a
->next
)
3596 if (a
->expr
!= NULL
)
3599 if (gfc_reference_st_label (a
->label
, ST_LABEL_TARGET
) == FAILURE
)
3604 c
->block
= gfc_get_code ();
3606 c
->op
= EXEC_SELECT
;
3608 new_case
= gfc_get_case ();
3609 new_case
->high
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, i
);
3610 new_case
->low
= new_case
->high
;
3611 c
->ext
.block
.case_list
= new_case
;
3613 c
->next
= gfc_get_code ();
3614 c
->next
->op
= EXEC_GOTO
;
3615 c
->next
->label1
= a
->label
;
3619 new_st
.op
= EXEC_CALL
;
3620 new_st
.symtree
= st
;
3621 new_st
.ext
.actual
= arglist
;
3626 gfc_syntax_error (ST_CALL
);
3629 gfc_free_actual_arglist (arglist
);
3634 /* Given a name, return a pointer to the common head structure,
3635 creating it if it does not exist. If FROM_MODULE is nonzero, we
3636 mangle the name so that it doesn't interfere with commons defined
3637 in the using namespace.
3638 TODO: Add to global symbol tree. */
3641 gfc_get_common (const char *name
, int from_module
)
3644 static int serial
= 0;
3645 char mangled_name
[GFC_MAX_SYMBOL_LEN
+ 1];
3649 /* A use associated common block is only needed to correctly layout
3650 the variables it contains. */
3651 snprintf (mangled_name
, GFC_MAX_SYMBOL_LEN
, "_%d_%s", serial
++, name
);
3652 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, mangled_name
);
3656 st
= gfc_find_symtree (gfc_current_ns
->common_root
, name
);
3659 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, name
);
3662 if (st
->n
.common
== NULL
)
3664 st
->n
.common
= gfc_get_common_head ();
3665 st
->n
.common
->where
= gfc_current_locus
;
3666 strcpy (st
->n
.common
->name
, name
);
3669 return st
->n
.common
;
3673 /* Match a common block name. */
3675 match
match_common_name (char *name
)
3679 if (gfc_match_char ('/') == MATCH_NO
)
3685 if (gfc_match_char ('/') == MATCH_YES
)
3691 m
= gfc_match_name (name
);
3693 if (m
== MATCH_ERROR
)
3695 if (m
== MATCH_YES
&& gfc_match_char ('/') == MATCH_YES
)
3698 gfc_error ("Syntax error in common block name at %C");
3703 /* Match a COMMON statement. */
3706 gfc_match_common (void)
3708 gfc_symbol
*sym
, **head
, *tail
, *other
, *old_blank_common
;
3709 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3716 old_blank_common
= gfc_current_ns
->blank_common
.head
;
3717 if (old_blank_common
)
3719 while (old_blank_common
->common_next
)
3720 old_blank_common
= old_blank_common
->common_next
;
3727 m
= match_common_name (name
);
3728 if (m
== MATCH_ERROR
)
3731 gsym
= gfc_get_gsymbol (name
);
3732 if (gsym
->type
!= GSYM_UNKNOWN
&& gsym
->type
!= GSYM_COMMON
)
3734 gfc_error ("Symbol '%s' at %C is already an external symbol that "
3735 "is not COMMON", name
);
3739 if (gsym
->type
== GSYM_UNKNOWN
)
3741 gsym
->type
= GSYM_COMMON
;
3742 gsym
->where
= gfc_current_locus
;
3748 if (name
[0] == '\0')
3750 t
= &gfc_current_ns
->blank_common
;
3751 if (t
->head
== NULL
)
3752 t
->where
= gfc_current_locus
;
3756 t
= gfc_get_common (name
, 0);
3765 while (tail
->common_next
)
3766 tail
= tail
->common_next
;
3769 /* Grab the list of symbols. */
3772 m
= gfc_match_symbol (&sym
, 0);
3773 if (m
== MATCH_ERROR
)
3778 /* Store a ref to the common block for error checking. */
3779 sym
->common_block
= t
;
3781 /* See if we know the current common block is bind(c), and if
3782 so, then see if we can check if the symbol is (which it'll
3783 need to be). This can happen if the bind(c) attr stmt was
3784 applied to the common block, and the variable(s) already
3785 defined, before declaring the common block. */
3786 if (t
->is_bind_c
== 1)
3788 if (sym
->ts
.type
!= BT_UNKNOWN
&& sym
->ts
.is_c_interop
!= 1)
3790 /* If we find an error, just print it and continue,
3791 cause it's just semantic, and we can see if there
3793 gfc_error_now ("Variable '%s' at %L in common block '%s' "
3794 "at %C must be declared with a C "
3795 "interoperable kind since common block "
3797 sym
->name
, &(sym
->declared_at
), t
->name
,
3801 if (sym
->attr
.is_bind_c
== 1)
3802 gfc_error_now ("Variable '%s' in common block "
3803 "'%s' at %C can not be bind(c) since "
3804 "it is not global", sym
->name
, t
->name
);
3807 if (sym
->attr
.in_common
)
3809 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
3814 if (((sym
->value
!= NULL
&& sym
->value
->expr_type
!= EXPR_NULL
)
3815 || sym
->attr
.data
) && gfc_current_state () != COMP_BLOCK_DATA
)
3817 if (gfc_notify_std (GFC_STD_GNU
, "Initialized symbol '%s' at %C "
3818 "can only be COMMON in "
3819 "BLOCK DATA", sym
->name
)
3824 if (gfc_add_in_common (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
3828 tail
->common_next
= sym
;
3834 /* Deal with an optional array specification after the
3836 m
= gfc_match_array_spec (&as
, true, true);
3837 if (m
== MATCH_ERROR
)
3842 if (as
->type
!= AS_EXPLICIT
)
3844 gfc_error ("Array specification for symbol '%s' in COMMON "
3845 "at %C must be explicit", sym
->name
);
3849 if (gfc_add_dimension (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
3852 if (sym
->attr
.pointer
)
3854 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
3855 "POINTER array", sym
->name
);
3864 sym
->common_head
= t
;
3866 /* Check to see if the symbol is already in an equivalence group.
3867 If it is, set the other members as being in common. */
3868 if (sym
->attr
.in_equivalence
)
3870 for (e1
= gfc_current_ns
->equiv
; e1
; e1
= e1
->next
)
3872 for (e2
= e1
; e2
; e2
= e2
->eq
)
3873 if (e2
->expr
->symtree
->n
.sym
== sym
)
3880 for (e2
= e1
; e2
; e2
= e2
->eq
)
3882 other
= e2
->expr
->symtree
->n
.sym
;
3883 if (other
->common_head
3884 && other
->common_head
!= sym
->common_head
)
3886 gfc_error ("Symbol '%s', in COMMON block '%s' at "
3887 "%C is being indirectly equivalenced to "
3888 "another COMMON block '%s'",
3889 sym
->name
, sym
->common_head
->name
,
3890 other
->common_head
->name
);
3893 other
->attr
.in_common
= 1;
3894 other
->common_head
= t
;
3900 gfc_gobble_whitespace ();
3901 if (gfc_match_eos () == MATCH_YES
)
3903 if (gfc_peek_ascii_char () == '/')
3905 if (gfc_match_char (',') != MATCH_YES
)
3907 gfc_gobble_whitespace ();
3908 if (gfc_peek_ascii_char () == '/')
3917 gfc_syntax_error (ST_COMMON
);
3920 if (old_blank_common
)
3921 old_blank_common
->common_next
= NULL
;
3923 gfc_current_ns
->blank_common
.head
= NULL
;
3924 gfc_free_array_spec (as
);
3929 /* Match a BLOCK DATA program unit. */
3932 gfc_match_block_data (void)
3934 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3938 if (gfc_match_eos () == MATCH_YES
)
3940 gfc_new_block
= NULL
;
3944 m
= gfc_match ("% %n%t", name
);
3948 if (gfc_get_symbol (name
, NULL
, &sym
))
3951 if (gfc_add_flavor (&sym
->attr
, FL_BLOCK_DATA
, sym
->name
, NULL
) == FAILURE
)
3954 gfc_new_block
= sym
;
3960 /* Free a namelist structure. */
3963 gfc_free_namelist (gfc_namelist
*name
)
3967 for (; name
; name
= n
)
3975 /* Match a NAMELIST statement. */
3978 gfc_match_namelist (void)
3980 gfc_symbol
*group_name
, *sym
;
3984 m
= gfc_match (" / %s /", &group_name
);
3987 if (m
== MATCH_ERROR
)
3992 if (group_name
->ts
.type
!= BT_UNKNOWN
)
3994 gfc_error ("Namelist group name '%s' at %C already has a basic "
3995 "type of %s", group_name
->name
,
3996 gfc_typename (&group_name
->ts
));
4000 if (group_name
->attr
.flavor
== FL_NAMELIST
4001 && group_name
->attr
.use_assoc
4002 && gfc_notify_std (GFC_STD_GNU
, "Namelist group name '%s' "
4003 "at %C already is USE associated and can"
4004 "not be respecified.", group_name
->name
)
4008 if (group_name
->attr
.flavor
!= FL_NAMELIST
4009 && gfc_add_flavor (&group_name
->attr
, FL_NAMELIST
,
4010 group_name
->name
, NULL
) == FAILURE
)
4015 m
= gfc_match_symbol (&sym
, 1);
4018 if (m
== MATCH_ERROR
)
4021 if (sym
->attr
.in_namelist
== 0
4022 && gfc_add_in_namelist (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4025 /* Use gfc_error_check here, rather than goto error, so that
4026 these are the only errors for the next two lines. */
4027 if (sym
->as
&& sym
->as
->type
== AS_ASSUMED_SIZE
)
4029 gfc_error ("Assumed size array '%s' in namelist '%s' at "
4030 "%C is not allowed", sym
->name
, group_name
->name
);
4034 nl
= gfc_get_namelist ();
4038 if (group_name
->namelist
== NULL
)
4039 group_name
->namelist
= group_name
->namelist_tail
= nl
;
4042 group_name
->namelist_tail
->next
= nl
;
4043 group_name
->namelist_tail
= nl
;
4046 if (gfc_match_eos () == MATCH_YES
)
4049 m
= gfc_match_char (',');
4051 if (gfc_match_char ('/') == MATCH_YES
)
4053 m2
= gfc_match (" %s /", &group_name
);
4054 if (m2
== MATCH_YES
)
4056 if (m2
== MATCH_ERROR
)
4070 gfc_syntax_error (ST_NAMELIST
);
4077 /* Match a MODULE statement. */
4080 gfc_match_module (void)
4084 m
= gfc_match (" %s%t", &gfc_new_block
);
4088 if (gfc_add_flavor (&gfc_new_block
->attr
, FL_MODULE
,
4089 gfc_new_block
->name
, NULL
) == FAILURE
)
4096 /* Free equivalence sets and lists. Recursively is the easiest way to
4100 gfc_free_equiv_until (gfc_equiv
*eq
, gfc_equiv
*stop
)
4105 gfc_free_equiv (eq
->eq
);
4106 gfc_free_equiv_until (eq
->next
, stop
);
4107 gfc_free_expr (eq
->expr
);
4113 gfc_free_equiv (gfc_equiv
*eq
)
4115 gfc_free_equiv_until (eq
, NULL
);
4119 /* Match an EQUIVALENCE statement. */
4122 gfc_match_equivalence (void)
4124 gfc_equiv
*eq
, *set
, *tail
;
4128 gfc_common_head
*common_head
= NULL
;
4136 eq
= gfc_get_equiv ();
4140 eq
->next
= gfc_current_ns
->equiv
;
4141 gfc_current_ns
->equiv
= eq
;
4143 if (gfc_match_char ('(') != MATCH_YES
)
4147 common_flag
= FALSE
;
4152 m
= gfc_match_equiv_variable (&set
->expr
);
4153 if (m
== MATCH_ERROR
)
4158 /* count the number of objects. */
4161 if (gfc_match_char ('%') == MATCH_YES
)
4163 gfc_error ("Derived type component %C is not a "
4164 "permitted EQUIVALENCE member");
4168 for (ref
= set
->expr
->ref
; ref
; ref
= ref
->next
)
4169 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
== AR_SECTION
)
4171 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
4172 "be an array section");
4176 sym
= set
->expr
->symtree
->n
.sym
;
4178 if (gfc_add_in_equivalence (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4181 if (sym
->attr
.in_common
)
4184 common_head
= sym
->common_head
;
4187 if (gfc_match_char (')') == MATCH_YES
)
4190 if (gfc_match_char (',') != MATCH_YES
)
4193 set
->eq
= gfc_get_equiv ();
4199 gfc_error ("EQUIVALENCE at %C requires two or more objects");
4203 /* If one of the members of an equivalence is in common, then
4204 mark them all as being in common. Before doing this, check
4205 that members of the equivalence group are not in different
4208 for (set
= eq
; set
; set
= set
->eq
)
4210 sym
= set
->expr
->symtree
->n
.sym
;
4211 if (sym
->common_head
&& sym
->common_head
!= common_head
)
4213 gfc_error ("Attempt to indirectly overlap COMMON "
4214 "blocks %s and %s by EQUIVALENCE at %C",
4215 sym
->common_head
->name
, common_head
->name
);
4218 sym
->attr
.in_common
= 1;
4219 sym
->common_head
= common_head
;
4222 if (gfc_match_eos () == MATCH_YES
)
4224 if (gfc_match_char (',') != MATCH_YES
)
4226 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
4234 gfc_syntax_error (ST_EQUIVALENCE
);
4240 gfc_free_equiv (gfc_current_ns
->equiv
);
4241 gfc_current_ns
->equiv
= eq
;
4247 /* Check that a statement function is not recursive. This is done by looking
4248 for the statement function symbol(sym) by looking recursively through its
4249 expression(e). If a reference to sym is found, true is returned.
4250 12.5.4 requires that any variable of function that is implicitly typed
4251 shall have that type confirmed by any subsequent type declaration. The
4252 implicit typing is conveniently done here. */
4254 recursive_stmt_fcn (gfc_expr
*, gfc_symbol
*);
4257 check_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
, int *f ATTRIBUTE_UNUSED
)
4263 switch (e
->expr_type
)
4266 if (e
->symtree
== NULL
)
4269 /* Check the name before testing for nested recursion! */
4270 if (sym
->name
== e
->symtree
->n
.sym
->name
)
4273 /* Catch recursion via other statement functions. */
4274 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
4275 && e
->symtree
->n
.sym
->value
4276 && recursive_stmt_fcn (e
->symtree
->n
.sym
->value
, sym
))
4279 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
4280 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
4285 if (e
->symtree
&& sym
->name
== e
->symtree
->n
.sym
->name
)
4288 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
4289 gfc_set_default_type (e
->symtree
->n
.sym
, 0, NULL
);
4301 recursive_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
)
4303 return gfc_traverse_expr (e
, sym
, check_stmt_fcn
, 0);
4307 /* Match a statement function declaration. It is so easy to match
4308 non-statement function statements with a MATCH_ERROR as opposed to
4309 MATCH_NO that we suppress error message in most cases. */
4312 gfc_match_st_function (void)
4314 gfc_error_buf old_error
;
4319 m
= gfc_match_symbol (&sym
, 0);
4323 gfc_push_error (&old_error
);
4325 if (gfc_add_procedure (&sym
->attr
, PROC_ST_FUNCTION
,
4326 sym
->name
, NULL
) == FAILURE
)
4329 if (gfc_match_formal_arglist (sym
, 1, 0) != MATCH_YES
)
4332 m
= gfc_match (" = %e%t", &expr
);
4336 gfc_free_error (&old_error
);
4337 if (m
== MATCH_ERROR
)
4340 if (recursive_stmt_fcn (expr
, sym
))
4342 gfc_error ("Statement function at %L is recursive", &expr
->where
);
4348 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: "
4349 "Statement function at %C") == FAILURE
)
4355 gfc_pop_error (&old_error
);
4360 /***************** SELECT CASE subroutines ******************/
4362 /* Free a single case structure. */
4365 free_case (gfc_case
*p
)
4367 if (p
->low
== p
->high
)
4369 gfc_free_expr (p
->low
);
4370 gfc_free_expr (p
->high
);
4375 /* Free a list of case structures. */
4378 gfc_free_case_list (gfc_case
*p
)
4390 /* Match a single case selector. */
4393 match_case_selector (gfc_case
**cp
)
4398 c
= gfc_get_case ();
4399 c
->where
= gfc_current_locus
;
4401 if (gfc_match_char (':') == MATCH_YES
)
4403 m
= gfc_match_init_expr (&c
->high
);
4406 if (m
== MATCH_ERROR
)
4411 m
= gfc_match_init_expr (&c
->low
);
4412 if (m
== MATCH_ERROR
)
4417 /* If we're not looking at a ':' now, make a range out of a single
4418 target. Else get the upper bound for the case range. */
4419 if (gfc_match_char (':') != MATCH_YES
)
4423 m
= gfc_match_init_expr (&c
->high
);
4424 if (m
== MATCH_ERROR
)
4426 /* MATCH_NO is fine. It's OK if nothing is there! */
4434 gfc_error ("Expected initialization expression in CASE at %C");
4442 /* Match the end of a case statement. */
4445 match_case_eos (void)
4447 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4450 if (gfc_match_eos () == MATCH_YES
)
4453 /* If the case construct doesn't have a case-construct-name, we
4454 should have matched the EOS. */
4455 if (!gfc_current_block ())
4458 gfc_gobble_whitespace ();
4460 m
= gfc_match_name (name
);
4464 if (strcmp (name
, gfc_current_block ()->name
) != 0)
4466 gfc_error ("Expected block name '%s' of SELECT construct at %C",
4467 gfc_current_block ()->name
);
4471 return gfc_match_eos ();
4475 /* Match a SELECT statement. */
4478 gfc_match_select (void)
4483 m
= gfc_match_label ();
4484 if (m
== MATCH_ERROR
)
4487 m
= gfc_match (" select case ( %e )%t", &expr
);
4491 new_st
.op
= EXEC_SELECT
;
4492 new_st
.expr1
= expr
;
4498 /* Push the current selector onto the SELECT TYPE stack. */
4501 select_type_push (gfc_symbol
*sel
)
4503 gfc_select_type_stack
*top
= gfc_get_select_type_stack ();
4504 top
->selector
= sel
;
4506 top
->prev
= select_type_stack
;
4508 select_type_stack
= top
;
4512 /* Set the temporary for the current SELECT TYPE selector. */
4515 select_type_set_tmp (gfc_typespec
*ts
)
4517 char name
[GFC_MAX_SYMBOL_LEN
];
4522 select_type_stack
->tmp
= NULL
;
4526 if (!gfc_type_is_extensible (ts
->u
.derived
))
4529 if (ts
->type
== BT_CLASS
)
4530 sprintf (name
, "__tmp_class_%s", ts
->u
.derived
->name
);
4532 sprintf (name
, "__tmp_type_%s", ts
->u
.derived
->name
);
4533 gfc_get_sym_tree (name
, gfc_current_ns
, &tmp
, false);
4534 gfc_add_type (tmp
->n
.sym
, ts
, NULL
);
4535 gfc_set_sym_referenced (tmp
->n
.sym
);
4536 gfc_add_pointer (&tmp
->n
.sym
->attr
, NULL
);
4537 gfc_add_flavor (&tmp
->n
.sym
->attr
, FL_VARIABLE
, name
, NULL
);
4538 if (ts
->type
== BT_CLASS
)
4539 gfc_build_class_symbol (&tmp
->n
.sym
->ts
, &tmp
->n
.sym
->attr
,
4540 &tmp
->n
.sym
->as
, false);
4541 tmp
->n
.sym
->attr
.select_type_temporary
= 1;
4543 /* Add an association for it, so the rest of the parser knows it is
4544 an associate-name. The target will be set during resolution. */
4545 tmp
->n
.sym
->assoc
= gfc_get_association_list ();
4546 tmp
->n
.sym
->assoc
->dangling
= 1;
4547 tmp
->n
.sym
->assoc
->st
= tmp
;
4549 select_type_stack
->tmp
= tmp
;
4553 /* Match a SELECT TYPE statement. */
4556 gfc_match_select_type (void)
4558 gfc_expr
*expr1
, *expr2
= NULL
;
4560 char name
[GFC_MAX_SYMBOL_LEN
];
4562 m
= gfc_match_label ();
4563 if (m
== MATCH_ERROR
)
4566 m
= gfc_match (" select type ( ");
4570 gfc_current_ns
= gfc_build_block_ns (gfc_current_ns
);
4572 m
= gfc_match (" %n => %e", name
, &expr2
);
4575 expr1
= gfc_get_expr();
4576 expr1
->expr_type
= EXPR_VARIABLE
;
4577 if (gfc_get_sym_tree (name
, NULL
, &expr1
->symtree
, false))
4582 if (expr2
->ts
.type
== BT_UNKNOWN
)
4583 expr1
->symtree
->n
.sym
->attr
.untyped
= 1;
4585 expr1
->symtree
->n
.sym
->ts
= expr2
->ts
;
4586 expr1
->symtree
->n
.sym
->attr
.flavor
= FL_VARIABLE
;
4587 expr1
->symtree
->n
.sym
->attr
.referenced
= 1;
4588 expr1
->symtree
->n
.sym
->attr
.class_ok
= 1;
4592 m
= gfc_match (" %e ", &expr1
);
4597 m
= gfc_match (" )%t");
4601 /* Check for F03:C811. */
4602 if (!expr2
&& (expr1
->expr_type
!= EXPR_VARIABLE
|| expr1
->ref
!= NULL
))
4604 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
4605 "use associate-name=>");
4610 new_st
.op
= EXEC_SELECT_TYPE
;
4611 new_st
.expr1
= expr1
;
4612 new_st
.expr2
= expr2
;
4613 new_st
.ext
.block
.ns
= gfc_current_ns
;
4615 select_type_push (expr1
->symtree
->n
.sym
);
4620 gfc_current_ns
= gfc_current_ns
->parent
;
4625 /* Match a CASE statement. */
4628 gfc_match_case (void)
4630 gfc_case
*c
, *head
, *tail
;
4635 if (gfc_current_state () != COMP_SELECT
)
4637 gfc_error ("Unexpected CASE statement at %C");
4641 if (gfc_match ("% default") == MATCH_YES
)
4643 m
= match_case_eos ();
4646 if (m
== MATCH_ERROR
)
4649 new_st
.op
= EXEC_SELECT
;
4650 c
= gfc_get_case ();
4651 c
->where
= gfc_current_locus
;
4652 new_st
.ext
.block
.case_list
= c
;
4656 if (gfc_match_char ('(') != MATCH_YES
)
4661 if (match_case_selector (&c
) == MATCH_ERROR
)
4671 if (gfc_match_char (')') == MATCH_YES
)
4673 if (gfc_match_char (',') != MATCH_YES
)
4677 m
= match_case_eos ();
4680 if (m
== MATCH_ERROR
)
4683 new_st
.op
= EXEC_SELECT
;
4684 new_st
.ext
.block
.case_list
= head
;
4689 gfc_error ("Syntax error in CASE specification at %C");
4692 gfc_free_case_list (head
); /* new_st is cleaned up in parse.c. */
4697 /* Match a TYPE IS statement. */
4700 gfc_match_type_is (void)
4705 if (gfc_current_state () != COMP_SELECT_TYPE
)
4707 gfc_error ("Unexpected TYPE IS statement at %C");
4711 if (gfc_match_char ('(') != MATCH_YES
)
4714 c
= gfc_get_case ();
4715 c
->where
= gfc_current_locus
;
4717 /* TODO: Once unlimited polymorphism is implemented, we will need to call
4718 match_type_spec here. */
4719 if (match_derived_type_spec (&c
->ts
) == MATCH_ERROR
)
4722 if (gfc_match_char (')') != MATCH_YES
)
4725 m
= match_case_eos ();
4728 if (m
== MATCH_ERROR
)
4731 new_st
.op
= EXEC_SELECT_TYPE
;
4732 new_st
.ext
.block
.case_list
= c
;
4734 /* Create temporary variable. */
4735 select_type_set_tmp (&c
->ts
);
4740 gfc_error ("Syntax error in TYPE IS specification at %C");
4744 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
4749 /* Match a CLASS IS or CLASS DEFAULT statement. */
4752 gfc_match_class_is (void)
4757 if (gfc_current_state () != COMP_SELECT_TYPE
)
4760 if (gfc_match ("% default") == MATCH_YES
)
4762 m
= match_case_eos ();
4765 if (m
== MATCH_ERROR
)
4768 new_st
.op
= EXEC_SELECT_TYPE
;
4769 c
= gfc_get_case ();
4770 c
->where
= gfc_current_locus
;
4771 c
->ts
.type
= BT_UNKNOWN
;
4772 new_st
.ext
.block
.case_list
= c
;
4773 select_type_set_tmp (NULL
);
4777 m
= gfc_match ("% is");
4780 if (m
== MATCH_ERROR
)
4783 if (gfc_match_char ('(') != MATCH_YES
)
4786 c
= gfc_get_case ();
4787 c
->where
= gfc_current_locus
;
4789 if (match_derived_type_spec (&c
->ts
) == MATCH_ERROR
)
4792 if (c
->ts
.type
== BT_DERIVED
)
4793 c
->ts
.type
= BT_CLASS
;
4795 if (gfc_match_char (')') != MATCH_YES
)
4798 m
= match_case_eos ();
4801 if (m
== MATCH_ERROR
)
4804 new_st
.op
= EXEC_SELECT_TYPE
;
4805 new_st
.ext
.block
.case_list
= c
;
4807 /* Create temporary variable. */
4808 select_type_set_tmp (&c
->ts
);
4813 gfc_error ("Syntax error in CLASS IS specification at %C");
4817 gfc_free_case_list (c
); /* new_st is cleaned up in parse.c. */
4822 /********************* WHERE subroutines ********************/
4824 /* Match the rest of a simple WHERE statement that follows an IF statement.
4828 match_simple_where (void)
4834 m
= gfc_match (" ( %e )", &expr
);
4838 m
= gfc_match_assignment ();
4841 if (m
== MATCH_ERROR
)
4844 if (gfc_match_eos () != MATCH_YES
)
4847 c
= gfc_get_code ();
4851 c
->next
= gfc_get_code ();
4854 gfc_clear_new_st ();
4856 new_st
.op
= EXEC_WHERE
;
4862 gfc_syntax_error (ST_WHERE
);
4865 gfc_free_expr (expr
);
4870 /* Match a WHERE statement. */
4873 gfc_match_where (gfc_statement
*st
)
4879 m0
= gfc_match_label ();
4880 if (m0
== MATCH_ERROR
)
4883 m
= gfc_match (" where ( %e )", &expr
);
4887 if (gfc_match_eos () == MATCH_YES
)
4889 *st
= ST_WHERE_BLOCK
;
4890 new_st
.op
= EXEC_WHERE
;
4891 new_st
.expr1
= expr
;
4895 m
= gfc_match_assignment ();
4897 gfc_syntax_error (ST_WHERE
);
4901 gfc_free_expr (expr
);
4905 /* We've got a simple WHERE statement. */
4907 c
= gfc_get_code ();
4911 c
->next
= gfc_get_code ();
4914 gfc_clear_new_st ();
4916 new_st
.op
= EXEC_WHERE
;
4923 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
4924 new_st if successful. */
4927 gfc_match_elsewhere (void)
4929 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4933 if (gfc_current_state () != COMP_WHERE
)
4935 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
4941 if (gfc_match_char ('(') == MATCH_YES
)
4943 m
= gfc_match_expr (&expr
);
4946 if (m
== MATCH_ERROR
)
4949 if (gfc_match_char (')') != MATCH_YES
)
4953 if (gfc_match_eos () != MATCH_YES
)
4955 /* Only makes sense if we have a where-construct-name. */
4956 if (!gfc_current_block ())
4961 /* Better be a name at this point. */
4962 m
= gfc_match_name (name
);
4965 if (m
== MATCH_ERROR
)
4968 if (gfc_match_eos () != MATCH_YES
)
4971 if (strcmp (name
, gfc_current_block ()->name
) != 0)
4973 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
4974 name
, gfc_current_block ()->name
);
4979 new_st
.op
= EXEC_WHERE
;
4980 new_st
.expr1
= expr
;
4984 gfc_syntax_error (ST_ELSEWHERE
);
4987 gfc_free_expr (expr
);
4992 /******************** FORALL subroutines ********************/
4994 /* Free a list of FORALL iterators. */
4997 gfc_free_forall_iterator (gfc_forall_iterator
*iter
)
4999 gfc_forall_iterator
*next
;
5004 gfc_free_expr (iter
->var
);
5005 gfc_free_expr (iter
->start
);
5006 gfc_free_expr (iter
->end
);
5007 gfc_free_expr (iter
->stride
);
5014 /* Match an iterator as part of a FORALL statement. The format is:
5016 <var> = <start>:<end>[:<stride>]
5018 On MATCH_NO, the caller tests for the possibility that there is a
5019 scalar mask expression. */
5022 match_forall_iterator (gfc_forall_iterator
**result
)
5024 gfc_forall_iterator
*iter
;
5028 where
= gfc_current_locus
;
5029 iter
= XCNEW (gfc_forall_iterator
);
5031 m
= gfc_match_expr (&iter
->var
);
5035 if (gfc_match_char ('=') != MATCH_YES
5036 || iter
->var
->expr_type
!= EXPR_VARIABLE
)
5042 m
= gfc_match_expr (&iter
->start
);
5046 if (gfc_match_char (':') != MATCH_YES
)
5049 m
= gfc_match_expr (&iter
->end
);
5052 if (m
== MATCH_ERROR
)
5055 if (gfc_match_char (':') == MATCH_NO
)
5056 iter
->stride
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
5059 m
= gfc_match_expr (&iter
->stride
);
5062 if (m
== MATCH_ERROR
)
5066 /* Mark the iteration variable's symbol as used as a FORALL index. */
5067 iter
->var
->symtree
->n
.sym
->forall_index
= true;
5073 gfc_error ("Syntax error in FORALL iterator at %C");
5078 gfc_current_locus
= where
;
5079 gfc_free_forall_iterator (iter
);
5084 /* Match the header of a FORALL statement. */
5087 match_forall_header (gfc_forall_iterator
**phead
, gfc_expr
**mask
)
5089 gfc_forall_iterator
*head
, *tail
, *new_iter
;
5093 gfc_gobble_whitespace ();
5098 if (gfc_match_char ('(') != MATCH_YES
)
5101 m
= match_forall_iterator (&new_iter
);
5102 if (m
== MATCH_ERROR
)
5107 head
= tail
= new_iter
;
5111 if (gfc_match_char (',') != MATCH_YES
)
5114 m
= match_forall_iterator (&new_iter
);
5115 if (m
== MATCH_ERROR
)
5120 tail
->next
= new_iter
;
5125 /* Have to have a mask expression. */
5127 m
= gfc_match_expr (&msk
);
5130 if (m
== MATCH_ERROR
)
5136 if (gfc_match_char (')') == MATCH_NO
)
5144 gfc_syntax_error (ST_FORALL
);
5147 gfc_free_expr (msk
);
5148 gfc_free_forall_iterator (head
);
5153 /* Match the rest of a simple FORALL statement that follows an
5157 match_simple_forall (void)
5159 gfc_forall_iterator
*head
;
5168 m
= match_forall_header (&head
, &mask
);
5175 m
= gfc_match_assignment ();
5177 if (m
== MATCH_ERROR
)
5181 m
= gfc_match_pointer_assignment ();
5182 if (m
== MATCH_ERROR
)
5188 c
= gfc_get_code ();
5190 c
->loc
= gfc_current_locus
;
5192 if (gfc_match_eos () != MATCH_YES
)
5195 gfc_clear_new_st ();
5196 new_st
.op
= EXEC_FORALL
;
5197 new_st
.expr1
= mask
;
5198 new_st
.ext
.forall_iterator
= head
;
5199 new_st
.block
= gfc_get_code ();
5201 new_st
.block
->op
= EXEC_FORALL
;
5202 new_st
.block
->next
= c
;
5207 gfc_syntax_error (ST_FORALL
);
5210 gfc_free_forall_iterator (head
);
5211 gfc_free_expr (mask
);
5217 /* Match a FORALL statement. */
5220 gfc_match_forall (gfc_statement
*st
)
5222 gfc_forall_iterator
*head
;
5231 m0
= gfc_match_label ();
5232 if (m0
== MATCH_ERROR
)
5235 m
= gfc_match (" forall");
5239 m
= match_forall_header (&head
, &mask
);
5240 if (m
== MATCH_ERROR
)
5245 if (gfc_match_eos () == MATCH_YES
)
5247 *st
= ST_FORALL_BLOCK
;
5248 new_st
.op
= EXEC_FORALL
;
5249 new_st
.expr1
= mask
;
5250 new_st
.ext
.forall_iterator
= head
;
5254 m
= gfc_match_assignment ();
5255 if (m
== MATCH_ERROR
)
5259 m
= gfc_match_pointer_assignment ();
5260 if (m
== MATCH_ERROR
)
5266 c
= gfc_get_code ();
5268 c
->loc
= gfc_current_locus
;
5270 gfc_clear_new_st ();
5271 new_st
.op
= EXEC_FORALL
;
5272 new_st
.expr1
= mask
;
5273 new_st
.ext
.forall_iterator
= head
;
5274 new_st
.block
= gfc_get_code ();
5275 new_st
.block
->op
= EXEC_FORALL
;
5276 new_st
.block
->next
= c
;
5282 gfc_syntax_error (ST_FORALL
);
5285 gfc_free_forall_iterator (head
);
5286 gfc_free_expr (mask
);
5287 gfc_free_statements (c
);