1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
31 /* For matching and debugging purposes. Order matters here! The
32 unary operators /must/ precede the binary plus and minus, or
33 the expression parser breaks. */
35 mstring intrinsic_operators
[] = {
36 minit ("+", INTRINSIC_UPLUS
),
37 minit ("-", INTRINSIC_UMINUS
),
38 minit ("+", INTRINSIC_PLUS
),
39 minit ("-", INTRINSIC_MINUS
),
40 minit ("**", INTRINSIC_POWER
),
41 minit ("//", INTRINSIC_CONCAT
),
42 minit ("*", INTRINSIC_TIMES
),
43 minit ("/", INTRINSIC_DIVIDE
),
44 minit (".and.", INTRINSIC_AND
),
45 minit (".or.", INTRINSIC_OR
),
46 minit (".eqv.", INTRINSIC_EQV
),
47 minit (".neqv.", INTRINSIC_NEQV
),
48 minit (".eq.", INTRINSIC_EQ
),
49 minit ("==", INTRINSIC_EQ
),
50 minit (".ne.", INTRINSIC_NE
),
51 minit ("/=", INTRINSIC_NE
),
52 minit (".ge.", INTRINSIC_GE
),
53 minit (">=", INTRINSIC_GE
),
54 minit (".le.", INTRINSIC_LE
),
55 minit ("<=", INTRINSIC_LE
),
56 minit (".lt.", INTRINSIC_LT
),
57 minit ("<", INTRINSIC_LT
),
58 minit (".gt.", INTRINSIC_GT
),
59 minit (">", INTRINSIC_GT
),
60 minit (".not.", INTRINSIC_NOT
),
61 minit (NULL
, INTRINSIC_NONE
)
65 /******************** Generic matching subroutines ************************/
67 /* In free form, match at least one space. Always matches in fixed
71 gfc_match_space (void)
76 if (gfc_current_form
== FORM_FIXED
)
79 old_loc
= gfc_current_locus
;
82 if (!gfc_is_whitespace (c
))
84 gfc_current_locus
= old_loc
;
88 gfc_gobble_whitespace ();
94 /* Match an end of statement. End of statement is optional
95 whitespace, followed by a ';' or '\n' or comment '!'. If a
96 semicolon is found, we continue to eat whitespace and semicolons. */
108 old_loc
= gfc_current_locus
;
109 gfc_gobble_whitespace ();
111 c
= gfc_next_char ();
117 c
= gfc_next_char ();
134 gfc_current_locus
= old_loc
;
135 return (flag
) ? MATCH_YES
: MATCH_NO
;
139 /* Match a literal integer on the input, setting the value on
140 MATCH_YES. Literal ints occur in kind-parameters as well as
141 old-style character length specifications. */
144 gfc_match_small_literal_int (int *value
, int *cnt
)
150 old_loc
= gfc_current_locus
;
152 gfc_gobble_whitespace ();
153 c
= gfc_next_char ();
157 gfc_current_locus
= old_loc
;
166 old_loc
= gfc_current_locus
;
167 c
= gfc_next_char ();
172 i
= 10 * i
+ c
- '0';
177 gfc_error ("Integer too large at %C");
182 gfc_current_locus
= old_loc
;
190 /* Match a small, constant integer expression, like in a kind
191 statement. On MATCH_YES, 'value' is set. */
194 gfc_match_small_int (int *value
)
201 m
= gfc_match_expr (&expr
);
205 p
= gfc_extract_int (expr
, &i
);
206 gfc_free_expr (expr
);
219 /* Matches a statement label. Uses gfc_match_small_literal_int() to
220 do most of the work. */
223 gfc_match_st_label (gfc_st_label
** label
)
229 old_loc
= gfc_current_locus
;
231 m
= gfc_match_small_literal_int (&i
, &cnt
);
237 gfc_error ("Too many digits in statement label at %C");
243 gfc_error ("Statement label at %C is zero");
247 *label
= gfc_get_st_label (i
);
252 gfc_current_locus
= old_loc
;
257 /* Match and validate a label associated with a named IF, DO or SELECT
258 statement. If the symbol does not have the label attribute, we add
259 it. We also make sure the symbol does not refer to another
260 (active) block. A matched label is pointed to by gfc_new_block. */
263 gfc_match_label (void)
265 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
268 gfc_new_block
= NULL
;
270 m
= gfc_match (" %n :", name
);
274 if (gfc_get_symbol (name
, NULL
, &gfc_new_block
))
276 gfc_error ("Label name '%s' at %C is ambiguous", name
);
280 if (gfc_new_block
->attr
.flavor
== FL_LABEL
)
282 gfc_error ("Duplicate construct label '%s' at %C", name
);
286 if (gfc_add_flavor (&gfc_new_block
->attr
, FL_LABEL
,
287 gfc_new_block
->name
, NULL
) == FAILURE
)
294 /* Try and match the input against an array of possibilities. If one
295 potential matching string is a substring of another, the longest
296 match takes precedence. Spaces in the target strings are optional
297 spaces that do not necessarily have to be found in the input
298 stream. In fixed mode, spaces never appear. If whitespace is
299 matched, it matches unlimited whitespace in the input. For this
300 reason, the 'mp' member of the mstring structure is used to track
301 the progress of each potential match.
303 If there is no match we return the tag associated with the
304 terminating NULL mstring structure and leave the locus pointer
305 where it started. If there is a match we return the tag member of
306 the matched mstring and leave the locus pointer after the matched
309 A '%' character is a mandatory space. */
312 gfc_match_strings (mstring
* a
)
314 mstring
*p
, *best_match
;
315 int no_match
, c
, possibles
;
320 for (p
= a
; p
->string
!= NULL
; p
++)
329 match_loc
= gfc_current_locus
;
331 gfc_gobble_whitespace ();
333 while (possibles
> 0)
335 c
= gfc_next_char ();
337 /* Apply the next character to the current possibilities. */
338 for (p
= a
; p
->string
!= NULL
; p
++)
345 /* Space matches 1+ whitespace(s). */
346 if ((gfc_current_form
== FORM_FREE
)
347 && gfc_is_whitespace (c
))
365 match_loc
= gfc_current_locus
;
373 gfc_current_locus
= match_loc
;
375 return (best_match
== NULL
) ? no_match
: best_match
->tag
;
379 /* See if the current input looks like a name of some sort. Modifies
380 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long. */
383 gfc_match_name (char *buffer
)
388 old_loc
= gfc_current_locus
;
389 gfc_gobble_whitespace ();
391 c
= gfc_next_char ();
394 gfc_current_locus
= old_loc
;
404 if (i
> gfc_option
.max_identifier_length
)
406 gfc_error ("Name at %C is too long");
410 old_loc
= gfc_current_locus
;
411 c
= gfc_next_char ();
415 || (gfc_option
.flag_dollar_ok
&& c
== '$'));
418 gfc_current_locus
= old_loc
;
424 /* Match a symbol on the input. Modifies the pointer to the symbol
425 pointer if successful. */
428 gfc_match_sym_tree (gfc_symtree
** matched_symbol
, int host_assoc
)
430 char buffer
[GFC_MAX_SYMBOL_LEN
+ 1];
433 m
= gfc_match_name (buffer
);
438 return (gfc_get_ha_sym_tree (buffer
, matched_symbol
))
439 ? MATCH_ERROR
: MATCH_YES
;
441 if (gfc_get_sym_tree (buffer
, NULL
, matched_symbol
))
449 gfc_match_symbol (gfc_symbol
** matched_symbol
, int host_assoc
)
454 m
= gfc_match_sym_tree (&st
, host_assoc
);
459 *matched_symbol
= st
->n
.sym
;
461 *matched_symbol
= NULL
;
464 *matched_symbol
= NULL
;
468 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
469 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
473 gfc_match_intrinsic_op (gfc_intrinsic_op
* result
)
477 op
= (gfc_intrinsic_op
) gfc_match_strings (intrinsic_operators
);
479 if (op
== INTRINSIC_NONE
)
487 /* Match a loop control phrase:
489 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
491 If the final integer expression is not present, a constant unity
492 expression is returned. We don't return MATCH_ERROR until after
493 the equals sign is seen. */
496 gfc_match_iterator (gfc_iterator
* iter
, int init_flag
)
498 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
499 gfc_expr
*var
, *e1
, *e2
, *e3
;
503 /* Match the start of an iterator without affecting the symbol
506 start
= gfc_current_locus
;
507 m
= gfc_match (" %n =", name
);
508 gfc_current_locus
= start
;
513 m
= gfc_match_variable (&var
, 0);
517 gfc_match_char ('=');
521 if (var
->ref
!= NULL
)
523 gfc_error ("Loop variable at %C cannot be a sub-component");
527 if (var
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
529 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
530 var
->symtree
->n
.sym
->name
);
534 if (var
->symtree
->n
.sym
->attr
.pointer
)
536 gfc_error ("Loop variable at %C cannot have the POINTER attribute");
540 m
= init_flag
? gfc_match_init_expr (&e1
) : gfc_match_expr (&e1
);
543 if (m
== MATCH_ERROR
)
546 if (gfc_match_char (',') != MATCH_YES
)
549 m
= init_flag
? gfc_match_init_expr (&e2
) : gfc_match_expr (&e2
);
552 if (m
== MATCH_ERROR
)
555 if (gfc_match_char (',') != MATCH_YES
)
557 e3
= gfc_int_expr (1);
561 m
= init_flag
? gfc_match_init_expr (&e3
) : gfc_match_expr (&e3
);
562 if (m
== MATCH_ERROR
)
566 gfc_error ("Expected a step value in iterator at %C");
578 gfc_error ("Syntax error in iterator at %C");
589 /* Tries to match the next non-whitespace character on the input.
590 This subroutine does not return MATCH_ERROR. */
593 gfc_match_char (char c
)
597 where
= gfc_current_locus
;
598 gfc_gobble_whitespace ();
600 if (gfc_next_char () == c
)
603 gfc_current_locus
= where
;
608 /* General purpose matching subroutine. The target string is a
609 scanf-like format string in which spaces correspond to arbitrary
610 whitespace (including no whitespace), characters correspond to
611 themselves. The %-codes are:
613 %% Literal percent sign
614 %e Expression, pointer to a pointer is set
615 %s Symbol, pointer to the symbol is set
616 %n Name, character buffer is set to name
617 %t Matches end of statement.
618 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
619 %l Matches a statement label
620 %v Matches a variable expression (an lvalue)
621 % Matches a required space (in free form) and optional spaces. */
624 gfc_match (const char *target
, ...)
626 gfc_st_label
**label
;
635 old_loc
= gfc_current_locus
;
636 va_start (argp
, target
);
646 gfc_gobble_whitespace ();
657 vp
= va_arg (argp
, void **);
658 n
= gfc_match_expr ((gfc_expr
**) vp
);
669 vp
= va_arg (argp
, void **);
670 n
= gfc_match_variable ((gfc_expr
**) vp
, 0);
681 vp
= va_arg (argp
, void **);
682 n
= gfc_match_symbol ((gfc_symbol
**) vp
, 0);
693 np
= va_arg (argp
, char *);
694 n
= gfc_match_name (np
);
705 label
= va_arg (argp
, gfc_st_label
**);
706 n
= gfc_match_st_label (label
);
717 ip
= va_arg (argp
, int *);
718 n
= gfc_match_intrinsic_op ((gfc_intrinsic_op
*) ip
);
729 if (gfc_match_eos () != MATCH_YES
)
737 if (gfc_match_space () == MATCH_YES
)
743 break; /* Fall through to character matcher */
746 gfc_internal_error ("gfc_match(): Bad match code %c", c
);
750 if (c
== gfc_next_char ())
760 /* Clean up after a failed match. */
761 gfc_current_locus
= old_loc
;
762 va_start (argp
, target
);
765 for (; matches
> 0; matches
--)
775 /* Matches that don't have to be undone */
780 (void)va_arg (argp
, void **);
785 vp
= va_arg (argp
, void **);
799 /*********************** Statement level matching **********************/
801 /* Matches the start of a program unit, which is the program keyword
802 followed by an obligatory symbol. */
805 gfc_match_program (void)
810 m
= gfc_match ("% %s%t", &sym
);
814 gfc_error ("Invalid form of PROGRAM statement at %C");
818 if (m
== MATCH_ERROR
)
821 if (gfc_add_flavor (&sym
->attr
, FL_PROGRAM
, sym
->name
, NULL
) == FAILURE
)
830 /* Match a simple assignment statement. */
833 gfc_match_assignment (void)
835 gfc_expr
*lvalue
, *rvalue
;
839 old_loc
= gfc_current_locus
;
841 lvalue
= rvalue
= NULL
;
842 m
= gfc_match (" %v =", &lvalue
);
846 if (lvalue
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
848 gfc_error ("Cannot assign to a PARAMETER variable at %C");
853 m
= gfc_match (" %e%t", &rvalue
);
857 gfc_set_sym_referenced (lvalue
->symtree
->n
.sym
);
859 new_st
.op
= EXEC_ASSIGN
;
860 new_st
.expr
= lvalue
;
861 new_st
.expr2
= rvalue
;
863 gfc_check_do_variable (lvalue
->symtree
);
868 gfc_current_locus
= old_loc
;
869 gfc_free_expr (lvalue
);
870 gfc_free_expr (rvalue
);
875 /* Match a pointer assignment statement. */
878 gfc_match_pointer_assignment (void)
880 gfc_expr
*lvalue
, *rvalue
;
884 old_loc
= gfc_current_locus
;
886 lvalue
= rvalue
= NULL
;
888 m
= gfc_match (" %v =>", &lvalue
);
895 m
= gfc_match (" %e%t", &rvalue
);
899 new_st
.op
= EXEC_POINTER_ASSIGN
;
900 new_st
.expr
= lvalue
;
901 new_st
.expr2
= rvalue
;
906 gfc_current_locus
= old_loc
;
907 gfc_free_expr (lvalue
);
908 gfc_free_expr (rvalue
);
913 /* We try to match an easy arithmetic IF statement. This only happens
914 when just after having encountered a simple IF statement. This code
915 is really duplicate with parts of the gfc_match_if code, but this is
918 match_arithmetic_if (void)
920 gfc_st_label
*l1
, *l2
, *l3
;
924 m
= gfc_match (" ( %e ) %l , %l , %l%t", &expr
, &l1
, &l2
, &l3
);
928 if (gfc_reference_st_label (l1
, ST_LABEL_TARGET
) == FAILURE
929 || gfc_reference_st_label (l2
, ST_LABEL_TARGET
) == FAILURE
930 || gfc_reference_st_label (l3
, ST_LABEL_TARGET
) == FAILURE
)
932 gfc_free_expr (expr
);
936 if (gfc_notify_std (GFC_STD_F95_DEL
,
937 "Obsolete: arithmetic IF statement at %C") == FAILURE
)
940 new_st
.op
= EXEC_ARITHMETIC_IF
;
950 /* The IF statement is a bit of a pain. First of all, there are three
951 forms of it, the simple IF, the IF that starts a block and the
954 There is a problem with the simple IF and that is the fact that we
955 only have a single level of undo information on symbols. What this
956 means is for a simple IF, we must re-match the whole IF statement
957 multiple times in order to guarantee that the symbol table ends up
958 in the proper state. */
960 static match
match_simple_forall (void);
961 static match
match_simple_where (void);
964 gfc_match_if (gfc_statement
* if_type
)
967 gfc_st_label
*l1
, *l2
, *l3
;
972 n
= gfc_match_label ();
973 if (n
== MATCH_ERROR
)
976 old_loc
= gfc_current_locus
;
978 m
= gfc_match (" if ( %e", &expr
);
982 if (gfc_match_char (')') != MATCH_YES
)
984 gfc_error ("Syntax error in IF-expression at %C");
985 gfc_free_expr (expr
);
989 m
= gfc_match (" %l , %l , %l%t", &l1
, &l2
, &l3
);
996 ("Block label not appropriate for arithmetic IF statement "
999 gfc_free_expr (expr
);
1003 if (gfc_reference_st_label (l1
, ST_LABEL_TARGET
) == FAILURE
1004 || gfc_reference_st_label (l2
, ST_LABEL_TARGET
) == FAILURE
1005 || gfc_reference_st_label (l3
, ST_LABEL_TARGET
) == FAILURE
)
1008 gfc_free_expr (expr
);
1012 if (gfc_notify_std (GFC_STD_F95_DEL
,
1013 "Obsolete: arithmetic IF statement at %C")
1017 new_st
.op
= EXEC_ARITHMETIC_IF
;
1023 *if_type
= ST_ARITHMETIC_IF
;
1027 if (gfc_match (" then%t") == MATCH_YES
)
1029 new_st
.op
= EXEC_IF
;
1032 *if_type
= ST_IF_BLOCK
;
1038 gfc_error ("Block label is not appropriate IF statement at %C");
1040 gfc_free_expr (expr
);
1044 /* At this point the only thing left is a simple IF statement. At
1045 this point, n has to be MATCH_NO, so we don't have to worry about
1046 re-matching a block label. From what we've got so far, try
1047 matching an assignment. */
1049 *if_type
= ST_SIMPLE_IF
;
1051 m
= gfc_match_assignment ();
1055 gfc_free_expr (expr
);
1056 gfc_undo_symbols ();
1057 gfc_current_locus
= old_loc
;
1059 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match */
1061 m
= gfc_match_pointer_assignment ();
1065 gfc_free_expr (expr
);
1066 gfc_undo_symbols ();
1067 gfc_current_locus
= old_loc
;
1069 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match */
1071 /* Look at the next keyword to see which matcher to call. Matching
1072 the keyword doesn't affect the symbol table, so we don't have to
1073 restore between tries. */
1075 #define match(string, subr, statement) \
1076 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1080 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
)
1081 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
)
1082 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
)
1083 match ("call", gfc_match_call
, ST_CALL
)
1084 match ("close", gfc_match_close
, ST_CLOSE
)
1085 match ("continue", gfc_match_continue
, ST_CONTINUE
)
1086 match ("cycle", gfc_match_cycle
, ST_CYCLE
)
1087 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
)
1088 match ("end file", gfc_match_endfile
, ST_END_FILE
)
1089 match ("exit", gfc_match_exit
, ST_EXIT
)
1090 match ("flush", gfc_match_flush
, ST_FLUSH
)
1091 match ("forall", match_simple_forall
, ST_FORALL
)
1092 match ("go to", gfc_match_goto
, ST_GOTO
)
1093 match ("if", match_arithmetic_if
, ST_ARITHMETIC_IF
)
1094 match ("inquire", gfc_match_inquire
, ST_INQUIRE
)
1095 match ("nullify", gfc_match_nullify
, ST_NULLIFY
)
1096 match ("open", gfc_match_open
, ST_OPEN
)
1097 match ("pause", gfc_match_pause
, ST_NONE
)
1098 match ("print", gfc_match_print
, ST_WRITE
)
1099 match ("read", gfc_match_read
, ST_READ
)
1100 match ("return", gfc_match_return
, ST_RETURN
)
1101 match ("rewind", gfc_match_rewind
, ST_REWIND
)
1102 match ("stop", gfc_match_stop
, ST_STOP
)
1103 match ("where", match_simple_where
, ST_WHERE
)
1104 match ("write", gfc_match_write
, ST_WRITE
)
1106 /* All else has failed, so give up. See if any of the matchers has
1107 stored an error message of some sort. */
1108 if (gfc_error_check () == 0)
1109 gfc_error ("Unclassifiable statement in IF-clause at %C");
1111 gfc_free_expr (expr
);
1116 gfc_error ("Syntax error in IF-clause at %C");
1119 gfc_free_expr (expr
);
1123 /* At this point, we've matched the single IF and the action clause
1124 is in new_st. Rearrange things so that the IF statement appears
1127 p
= gfc_get_code ();
1128 p
->next
= gfc_get_code ();
1130 p
->next
->loc
= gfc_current_locus
;
1135 gfc_clear_new_st ();
1137 new_st
.op
= EXEC_IF
;
1146 /* Match an ELSE statement. */
1149 gfc_match_else (void)
1151 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1153 if (gfc_match_eos () == MATCH_YES
)
1156 if (gfc_match_name (name
) != MATCH_YES
1157 || gfc_current_block () == NULL
1158 || gfc_match_eos () != MATCH_YES
)
1160 gfc_error ("Unexpected junk after ELSE statement at %C");
1164 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1166 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1167 name
, gfc_current_block ()->name
);
1175 /* Match an ELSE IF statement. */
1178 gfc_match_elseif (void)
1180 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1184 m
= gfc_match (" ( %e ) then", &expr
);
1188 if (gfc_match_eos () == MATCH_YES
)
1191 if (gfc_match_name (name
) != MATCH_YES
1192 || gfc_current_block () == NULL
1193 || gfc_match_eos () != MATCH_YES
)
1195 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1199 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1201 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1202 name
, gfc_current_block ()->name
);
1207 new_st
.op
= EXEC_IF
;
1212 gfc_free_expr (expr
);
1217 /* Free a gfc_iterator structure. */
1220 gfc_free_iterator (gfc_iterator
* iter
, int flag
)
1226 gfc_free_expr (iter
->var
);
1227 gfc_free_expr (iter
->start
);
1228 gfc_free_expr (iter
->end
);
1229 gfc_free_expr (iter
->step
);
1236 /* Match a DO statement. */
1241 gfc_iterator iter
, *ip
;
1243 gfc_st_label
*label
;
1246 old_loc
= gfc_current_locus
;
1249 iter
.var
= iter
.start
= iter
.end
= iter
.step
= NULL
;
1251 m
= gfc_match_label ();
1252 if (m
== MATCH_ERROR
)
1255 if (gfc_match (" do") != MATCH_YES
)
1258 m
= gfc_match_st_label (&label
);
1259 if (m
== MATCH_ERROR
)
1262 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1264 if (gfc_match_eos () == MATCH_YES
)
1266 iter
.end
= gfc_logical_expr (1, NULL
);
1267 new_st
.op
= EXEC_DO_WHILE
;
1271 /* match an optional comma, if no comma is found a space is obligatory. */
1272 if (gfc_match_char(',') != MATCH_YES
1273 && gfc_match ("% ") != MATCH_YES
)
1276 /* See if we have a DO WHILE. */
1277 if (gfc_match (" while ( %e )%t", &iter
.end
) == MATCH_YES
)
1279 new_st
.op
= EXEC_DO_WHILE
;
1283 /* The abortive DO WHILE may have done something to the symbol
1284 table, so we start over: */
1285 gfc_undo_symbols ();
1286 gfc_current_locus
= old_loc
;
1288 gfc_match_label (); /* This won't error */
1289 gfc_match (" do "); /* This will work */
1291 gfc_match_st_label (&label
); /* Can't error out */
1292 gfc_match_char (','); /* Optional comma */
1294 m
= gfc_match_iterator (&iter
, 0);
1297 if (m
== MATCH_ERROR
)
1300 gfc_check_do_variable (iter
.var
->symtree
);
1302 if (gfc_match_eos () != MATCH_YES
)
1304 gfc_syntax_error (ST_DO
);
1308 new_st
.op
= EXEC_DO
;
1312 && gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
1315 new_st
.label
= label
;
1317 if (new_st
.op
== EXEC_DO_WHILE
)
1318 new_st
.expr
= iter
.end
;
1321 new_st
.ext
.iterator
= ip
= gfc_get_iterator ();
1328 gfc_free_iterator (&iter
, 0);
1334 /* Match an EXIT or CYCLE statement. */
1337 match_exit_cycle (gfc_statement st
, gfc_exec_op op
)
1343 if (gfc_match_eos () == MATCH_YES
)
1347 m
= gfc_match ("% %s%t", &sym
);
1348 if (m
== MATCH_ERROR
)
1352 gfc_syntax_error (st
);
1356 if (sym
->attr
.flavor
!= FL_LABEL
)
1358 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1359 sym
->name
, gfc_ascii_statement (st
));
1364 /* Find the loop mentioned specified by the label (or lack of a
1366 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1367 if (p
->state
== COMP_DO
&& (sym
== NULL
|| sym
== p
->sym
))
1373 gfc_error ("%s statement at %C is not within a loop",
1374 gfc_ascii_statement (st
));
1376 gfc_error ("%s statement at %C is not within loop '%s'",
1377 gfc_ascii_statement (st
), sym
->name
);
1382 /* Save the first statement in the loop - needed by the backend. */
1383 new_st
.ext
.whichloop
= p
->head
;
1386 /* new_st.sym = sym;*/
1392 /* Match the EXIT statement. */
1395 gfc_match_exit (void)
1398 return match_exit_cycle (ST_EXIT
, EXEC_EXIT
);
1402 /* Match the CYCLE statement. */
1405 gfc_match_cycle (void)
1408 return match_exit_cycle (ST_CYCLE
, EXEC_CYCLE
);
1412 /* Match a number or character constant after a STOP or PAUSE statement. */
1415 gfc_match_stopcode (gfc_statement st
)
1425 if (gfc_match_eos () != MATCH_YES
)
1427 m
= gfc_match_small_literal_int (&stop_code
, &cnt
);
1428 if (m
== MATCH_ERROR
)
1431 if (m
== MATCH_YES
&& cnt
> 5)
1433 gfc_error ("Too many digits in STOP code at %C");
1439 /* Try a character constant. */
1440 m
= gfc_match_expr (&e
);
1441 if (m
== MATCH_ERROR
)
1445 if (e
->ts
.type
!= BT_CHARACTER
|| e
->expr_type
!= EXPR_CONSTANT
)
1449 if (gfc_match_eos () != MATCH_YES
)
1453 if (gfc_pure (NULL
))
1455 gfc_error ("%s statement not allowed in PURE procedure at %C",
1456 gfc_ascii_statement (st
));
1460 new_st
.op
= st
== ST_STOP
? EXEC_STOP
: EXEC_PAUSE
;
1462 new_st
.ext
.stop_code
= stop_code
;
1467 gfc_syntax_error (st
);
1475 /* Match the (deprecated) PAUSE statement. */
1478 gfc_match_pause (void)
1482 m
= gfc_match_stopcode (ST_PAUSE
);
1485 if (gfc_notify_std (GFC_STD_F95_DEL
,
1486 "Obsolete: PAUSE statement at %C")
1494 /* Match the STOP statement. */
1497 gfc_match_stop (void)
1499 return gfc_match_stopcode (ST_STOP
);
1503 /* Match a CONTINUE statement. */
1506 gfc_match_continue (void)
1509 if (gfc_match_eos () != MATCH_YES
)
1511 gfc_syntax_error (ST_CONTINUE
);
1515 new_st
.op
= EXEC_CONTINUE
;
1520 /* Match the (deprecated) ASSIGN statement. */
1523 gfc_match_assign (void)
1526 gfc_st_label
*label
;
1528 if (gfc_match (" %l", &label
) == MATCH_YES
)
1530 if (gfc_reference_st_label (label
, ST_LABEL_UNKNOWN
) == FAILURE
)
1532 if (gfc_match (" to %v%t", &expr
) == MATCH_YES
)
1534 if (gfc_notify_std (GFC_STD_F95_DEL
,
1535 "Obsolete: ASSIGN statement at %C")
1539 expr
->symtree
->n
.sym
->attr
.assign
= 1;
1541 new_st
.op
= EXEC_LABEL_ASSIGN
;
1542 new_st
.label
= label
;
1551 /* Match the GO TO statement. As a computed GOTO statement is
1552 matched, it is transformed into an equivalent SELECT block. No
1553 tree is necessary, and the resulting jumps-to-jumps are
1554 specifically optimized away by the back end. */
1557 gfc_match_goto (void)
1559 gfc_code
*head
, *tail
;
1562 gfc_st_label
*label
;
1566 if (gfc_match (" %l%t", &label
) == MATCH_YES
)
1568 if (gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
1571 new_st
.op
= EXEC_GOTO
;
1572 new_st
.label
= label
;
1576 /* The assigned GO TO statement. */
1578 if (gfc_match_variable (&expr
, 0) == MATCH_YES
)
1580 if (gfc_notify_std (GFC_STD_F95_DEL
,
1581 "Obsolete: Assigned GOTO statement at %C")
1585 new_st
.op
= EXEC_GOTO
;
1588 if (gfc_match_eos () == MATCH_YES
)
1591 /* Match label list. */
1592 gfc_match_char (',');
1593 if (gfc_match_char ('(') != MATCH_YES
)
1595 gfc_syntax_error (ST_GOTO
);
1602 m
= gfc_match_st_label (&label
);
1606 if (gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
1610 head
= tail
= gfc_get_code ();
1613 tail
->block
= gfc_get_code ();
1617 tail
->label
= label
;
1618 tail
->op
= EXEC_GOTO
;
1620 while (gfc_match_char (',') == MATCH_YES
);
1622 if (gfc_match (")%t") != MATCH_YES
)
1628 "Statement label list in GOTO at %C cannot be empty");
1631 new_st
.block
= head
;
1636 /* Last chance is a computed GO TO statement. */
1637 if (gfc_match_char ('(') != MATCH_YES
)
1639 gfc_syntax_error (ST_GOTO
);
1648 m
= gfc_match_st_label (&label
);
1652 if (gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
1656 head
= tail
= gfc_get_code ();
1659 tail
->block
= gfc_get_code ();
1663 cp
= gfc_get_case ();
1664 cp
->low
= cp
->high
= gfc_int_expr (i
++);
1666 tail
->op
= EXEC_SELECT
;
1667 tail
->ext
.case_list
= cp
;
1669 tail
->next
= gfc_get_code ();
1670 tail
->next
->op
= EXEC_GOTO
;
1671 tail
->next
->label
= label
;
1673 while (gfc_match_char (',') == MATCH_YES
);
1675 if (gfc_match_char (')') != MATCH_YES
)
1680 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1684 /* Get the rest of the statement. */
1685 gfc_match_char (',');
1687 if (gfc_match (" %e%t", &expr
) != MATCH_YES
)
1690 /* At this point, a computed GOTO has been fully matched and an
1691 equivalent SELECT statement constructed. */
1693 new_st
.op
= EXEC_SELECT
;
1696 /* Hack: For a "real" SELECT, the expression is in expr. We put
1697 it in expr2 so we can distinguish then and produce the correct
1699 new_st
.expr2
= expr
;
1700 new_st
.block
= head
;
1704 gfc_syntax_error (ST_GOTO
);
1706 gfc_free_statements (head
);
1711 /* Frees a list of gfc_alloc structures. */
1714 gfc_free_alloc_list (gfc_alloc
* p
)
1721 gfc_free_expr (p
->expr
);
1727 /* Match an ALLOCATE statement. */
1730 gfc_match_allocate (void)
1732 gfc_alloc
*head
, *tail
;
1739 if (gfc_match_char ('(') != MATCH_YES
)
1745 head
= tail
= gfc_get_alloc ();
1748 tail
->next
= gfc_get_alloc ();
1752 m
= gfc_match_variable (&tail
->expr
, 0);
1755 if (m
== MATCH_ERROR
)
1758 if (gfc_check_do_variable (tail
->expr
->symtree
))
1762 && gfc_impure_variable (tail
->expr
->symtree
->n
.sym
))
1764 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1769 if (gfc_match_char (',') != MATCH_YES
)
1772 m
= gfc_match (" stat = %v", &stat
);
1773 if (m
== MATCH_ERROR
)
1781 if (stat
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
1784 ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1785 "INTENT(IN)", stat
->symtree
->n
.sym
->name
);
1789 if (gfc_pure (NULL
) && gfc_impure_variable (stat
->symtree
->n
.sym
))
1792 ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1797 if (stat
->symtree
->n
.sym
->attr
.flavor
!= FL_VARIABLE
)
1799 gfc_error("STAT expression at %C must be a variable");
1803 gfc_check_do_variable(stat
->symtree
);
1806 if (gfc_match (" )%t") != MATCH_YES
)
1809 new_st
.op
= EXEC_ALLOCATE
;
1811 new_st
.ext
.alloc_list
= head
;
1816 gfc_syntax_error (ST_ALLOCATE
);
1819 gfc_free_expr (stat
);
1820 gfc_free_alloc_list (head
);
1825 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1826 a set of pointer assignments to intrinsic NULL(). */
1829 gfc_match_nullify (void)
1837 if (gfc_match_char ('(') != MATCH_YES
)
1842 m
= gfc_match_variable (&p
, 0);
1843 if (m
== MATCH_ERROR
)
1848 if (gfc_check_do_variable(p
->symtree
))
1851 if (gfc_pure (NULL
) && gfc_impure_variable (p
->symtree
->n
.sym
))
1854 ("Illegal variable in NULLIFY at %C for a PURE procedure");
1858 /* build ' => NULL() ' */
1859 e
= gfc_get_expr ();
1860 e
->where
= gfc_current_locus
;
1861 e
->expr_type
= EXPR_NULL
;
1862 e
->ts
.type
= BT_UNKNOWN
;
1869 tail
->next
= gfc_get_code ();
1873 tail
->op
= EXEC_POINTER_ASSIGN
;
1877 if (gfc_match (" )%t") == MATCH_YES
)
1879 if (gfc_match_char (',') != MATCH_YES
)
1886 gfc_syntax_error (ST_NULLIFY
);
1889 gfc_free_statements (tail
);
1894 /* Match a DEALLOCATE statement. */
1897 gfc_match_deallocate (void)
1899 gfc_alloc
*head
, *tail
;
1906 if (gfc_match_char ('(') != MATCH_YES
)
1912 head
= tail
= gfc_get_alloc ();
1915 tail
->next
= gfc_get_alloc ();
1919 m
= gfc_match_variable (&tail
->expr
, 0);
1920 if (m
== MATCH_ERROR
)
1925 if (gfc_check_do_variable (tail
->expr
->symtree
))
1929 && gfc_impure_variable (tail
->expr
->symtree
->n
.sym
))
1932 ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1937 if (gfc_match_char (',') != MATCH_YES
)
1940 m
= gfc_match (" stat = %v", &stat
);
1941 if (m
== MATCH_ERROR
)
1949 if (stat
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
1951 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1952 "cannot be INTENT(IN)", stat
->symtree
->n
.sym
->name
);
1956 if (gfc_pure(NULL
) && gfc_impure_variable (stat
->symtree
->n
.sym
))
1958 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
1959 "for a PURE procedure");
1963 if (stat
->symtree
->n
.sym
->attr
.flavor
!= FL_VARIABLE
)
1965 gfc_error("STAT expression at %C must be a variable");
1969 gfc_check_do_variable(stat
->symtree
);
1972 if (gfc_match (" )%t") != MATCH_YES
)
1975 new_st
.op
= EXEC_DEALLOCATE
;
1977 new_st
.ext
.alloc_list
= head
;
1982 gfc_syntax_error (ST_DEALLOCATE
);
1985 gfc_free_expr (stat
);
1986 gfc_free_alloc_list (head
);
1991 /* Match a RETURN statement. */
1994 gfc_match_return (void)
1998 gfc_compile_state s
;
2002 if (gfc_match_eos () == MATCH_YES
)
2005 if (gfc_find_state (COMP_SUBROUTINE
) == FAILURE
)
2007 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2012 if (gfc_current_form
== FORM_FREE
)
2014 /* The following are valid, so we can't require a blank after the
2018 c
= gfc_peek_char ();
2019 if (ISALPHA (c
) || ISDIGIT (c
))
2023 m
= gfc_match (" %e%t", &e
);
2026 if (m
== MATCH_ERROR
)
2029 gfc_syntax_error (ST_RETURN
);
2036 gfc_enclosing_unit (&s
);
2037 if (s
== COMP_PROGRAM
2038 && gfc_notify_std (GFC_STD_GNU
, "Extension: RETURN statement in "
2039 "main program at %C") == FAILURE
)
2042 new_st
.op
= EXEC_RETURN
;
2049 /* Match a CALL statement. The tricky part here are possible
2050 alternate return specifiers. We handle these by having all
2051 "subroutines" actually return an integer via a register that gives
2052 the return number. If the call specifies alternate returns, we
2053 generate code for a SELECT statement whose case clauses contain
2054 GOTOs to the various labels. */
2057 gfc_match_call (void)
2059 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2060 gfc_actual_arglist
*a
, *arglist
;
2070 m
= gfc_match ("% %n", name
);
2076 if (gfc_get_ha_sym_tree (name
, &st
))
2080 gfc_set_sym_referenced (sym
);
2082 if (!sym
->attr
.generic
2083 && !sym
->attr
.subroutine
2084 && gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
2087 if (gfc_match_eos () != MATCH_YES
)
2089 m
= gfc_match_actual_arglist (1, &arglist
);
2092 if (m
== MATCH_ERROR
)
2095 if (gfc_match_eos () != MATCH_YES
)
2099 /* If any alternate return labels were found, construct a SELECT
2100 statement that will jump to the right place. */
2103 for (a
= arglist
; a
; a
= a
->next
)
2104 if (a
->expr
== NULL
)
2109 gfc_symtree
*select_st
;
2110 gfc_symbol
*select_sym
;
2111 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2113 new_st
.next
= c
= gfc_get_code ();
2114 c
->op
= EXEC_SELECT
;
2115 sprintf (name
, "_result_%s",sym
->name
);
2116 gfc_get_ha_sym_tree (name
, &select_st
); /* Can't fail */
2118 select_sym
= select_st
->n
.sym
;
2119 select_sym
->ts
.type
= BT_INTEGER
;
2120 select_sym
->ts
.kind
= gfc_default_integer_kind
;
2121 gfc_set_sym_referenced (select_sym
);
2122 c
->expr
= gfc_get_expr ();
2123 c
->expr
->expr_type
= EXPR_VARIABLE
;
2124 c
->expr
->symtree
= select_st
;
2125 c
->expr
->ts
= select_sym
->ts
;
2126 c
->expr
->where
= gfc_current_locus
;
2129 for (a
= arglist
; a
; a
= a
->next
)
2131 if (a
->expr
!= NULL
)
2134 if (gfc_reference_st_label (a
->label
, ST_LABEL_TARGET
) == FAILURE
)
2139 c
->block
= gfc_get_code ();
2141 c
->op
= EXEC_SELECT
;
2143 new_case
= gfc_get_case ();
2144 new_case
->high
= new_case
->low
= gfc_int_expr (i
);
2145 c
->ext
.case_list
= new_case
;
2147 c
->next
= gfc_get_code ();
2148 c
->next
->op
= EXEC_GOTO
;
2149 c
->next
->label
= a
->label
;
2153 new_st
.op
= EXEC_CALL
;
2154 new_st
.symtree
= st
;
2155 new_st
.ext
.actual
= arglist
;
2160 gfc_syntax_error (ST_CALL
);
2163 gfc_free_actual_arglist (arglist
);
2168 /* Given a name, return a pointer to the common head structure,
2169 creating it if it does not exist. If FROM_MODULE is nonzero, we
2170 mangle the name so that it doesn't interfere with commons defined
2171 in the using namespace.
2172 TODO: Add to global symbol tree. */
2175 gfc_get_common (const char *name
, int from_module
)
2178 static int serial
= 0;
2179 char mangled_name
[GFC_MAX_SYMBOL_LEN
+1];
2183 /* A use associated common block is only needed to correctly layout
2184 the variables it contains. */
2185 snprintf(mangled_name
, GFC_MAX_SYMBOL_LEN
, "_%d_%s", serial
++, name
);
2186 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, mangled_name
);
2190 st
= gfc_find_symtree (gfc_current_ns
->common_root
, name
);
2193 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, name
);
2196 if (st
->n
.common
== NULL
)
2198 st
->n
.common
= gfc_get_common_head ();
2199 st
->n
.common
->where
= gfc_current_locus
;
2200 strcpy (st
->n
.common
->name
, name
);
2203 return st
->n
.common
;
2207 /* Match a common block name. */
2210 match_common_name (char *name
)
2214 if (gfc_match_char ('/') == MATCH_NO
)
2220 if (gfc_match_char ('/') == MATCH_YES
)
2226 m
= gfc_match_name (name
);
2228 if (m
== MATCH_ERROR
)
2230 if (m
== MATCH_YES
&& gfc_match_char ('/') == MATCH_YES
)
2233 gfc_error ("Syntax error in common block name at %C");
2238 /* Match a COMMON statement. */
2241 gfc_match_common (void)
2243 gfc_symbol
*sym
, **head
, *tail
, *other
, *old_blank_common
;
2244 char name
[GFC_MAX_SYMBOL_LEN
+1];
2247 gfc_equiv
* e1
, * e2
;
2250 old_blank_common
= gfc_current_ns
->blank_common
.head
;
2251 if (old_blank_common
)
2253 while (old_blank_common
->common_next
)
2254 old_blank_common
= old_blank_common
->common_next
;
2261 m
= match_common_name (name
);
2262 if (m
== MATCH_ERROR
)
2265 if (name
[0] == '\0')
2267 t
= &gfc_current_ns
->blank_common
;
2268 if (t
->head
== NULL
)
2269 t
->where
= gfc_current_locus
;
2274 t
= gfc_get_common (name
, 0);
2283 while (tail
->common_next
)
2284 tail
= tail
->common_next
;
2287 /* Grab the list of symbols. */
2290 m
= gfc_match_symbol (&sym
, 0);
2291 if (m
== MATCH_ERROR
)
2296 if (sym
->attr
.in_common
)
2298 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2303 if (gfc_add_in_common (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
2306 if (sym
->value
!= NULL
2307 && (name
[0] == '\0' || !sym
->attr
.data
))
2309 if (name
[0] == '\0')
2310 gfc_error ("Previously initialized symbol '%s' in "
2311 "blank COMMON block at %C", sym
->name
);
2313 gfc_error ("Previously initialized symbol '%s' in "
2314 "COMMON block '%s' at %C", sym
->name
, name
);
2318 if (gfc_add_in_common (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
2321 /* Derived type names must have the SEQUENCE attribute. */
2322 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->ts
.derived
->attr
.sequence
)
2325 ("Derived type variable in COMMON at %C does not have the "
2326 "SEQUENCE attribute");
2331 tail
->common_next
= sym
;
2337 /* Deal with an optional array specification after the
2339 m
= gfc_match_array_spec (&as
);
2340 if (m
== MATCH_ERROR
)
2345 if (as
->type
!= AS_EXPLICIT
)
2348 ("Array specification for symbol '%s' in COMMON at %C "
2349 "must be explicit", sym
->name
);
2353 if (gfc_add_dimension (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
2356 if (sym
->attr
.pointer
)
2359 ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2369 sym
->common_head
= t
;
2371 /* Check to see if the symbol is already in an equivalence group.
2372 If it is, set the other members as being in common. */
2373 if (sym
->attr
.in_equivalence
)
2375 for (e1
= gfc_current_ns
->equiv
; e1
; e1
= e1
->next
)
2377 for (e2
= e1
; e2
; e2
= e2
->eq
)
2378 if (e2
->expr
->symtree
->n
.sym
== sym
)
2385 for (e2
= e1
; e2
; e2
= e2
->eq
)
2387 other
= e2
->expr
->symtree
->n
.sym
;
2388 if (other
->common_head
2389 && other
->common_head
!= sym
->common_head
)
2391 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2392 "%C is being indirectly equivalenced to "
2393 "another COMMON block '%s'",
2395 sym
->common_head
->name
,
2396 other
->common_head
->name
);
2399 other
->attr
.in_common
= 1;
2400 other
->common_head
= t
;
2406 gfc_gobble_whitespace ();
2407 if (gfc_match_eos () == MATCH_YES
)
2409 if (gfc_peek_char () == '/')
2411 if (gfc_match_char (',') != MATCH_YES
)
2413 gfc_gobble_whitespace ();
2414 if (gfc_peek_char () == '/')
2423 gfc_syntax_error (ST_COMMON
);
2426 if (old_blank_common
)
2427 old_blank_common
->common_next
= NULL
;
2429 gfc_current_ns
->blank_common
.head
= NULL
;
2430 gfc_free_array_spec (as
);
2435 /* Match a BLOCK DATA program unit. */
2438 gfc_match_block_data (void)
2440 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2444 if (gfc_match_eos () == MATCH_YES
)
2446 gfc_new_block
= NULL
;
2450 m
= gfc_match ("% %n%t", name
);
2454 if (gfc_get_symbol (name
, NULL
, &sym
))
2457 if (gfc_add_flavor (&sym
->attr
, FL_BLOCK_DATA
, sym
->name
, NULL
) == FAILURE
)
2460 gfc_new_block
= sym
;
2466 /* Free a namelist structure. */
2469 gfc_free_namelist (gfc_namelist
* name
)
2473 for (; name
; name
= n
)
2481 /* Match a NAMELIST statement. */
2484 gfc_match_namelist (void)
2486 gfc_symbol
*group_name
, *sym
;
2490 m
= gfc_match (" / %s /", &group_name
);
2493 if (m
== MATCH_ERROR
)
2498 if (group_name
->ts
.type
!= BT_UNKNOWN
)
2501 ("Namelist group name '%s' at %C already has a basic type "
2502 "of %s", group_name
->name
, gfc_typename (&group_name
->ts
));
2506 if (group_name
->attr
.flavor
!= FL_NAMELIST
2507 && gfc_add_flavor (&group_name
->attr
, FL_NAMELIST
,
2508 group_name
->name
, NULL
) == FAILURE
)
2513 m
= gfc_match_symbol (&sym
, 1);
2516 if (m
== MATCH_ERROR
)
2519 if (sym
->attr
.in_namelist
== 0
2520 && gfc_add_in_namelist (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
2523 nl
= gfc_get_namelist ();
2526 if (group_name
->namelist
== NULL
)
2527 group_name
->namelist
= group_name
->namelist_tail
= nl
;
2530 group_name
->namelist_tail
->next
= nl
;
2531 group_name
->namelist_tail
= nl
;
2534 if (gfc_match_eos () == MATCH_YES
)
2537 m
= gfc_match_char (',');
2539 if (gfc_match_char ('/') == MATCH_YES
)
2541 m2
= gfc_match (" %s /", &group_name
);
2542 if (m2
== MATCH_YES
)
2544 if (m2
== MATCH_ERROR
)
2558 gfc_syntax_error (ST_NAMELIST
);
2565 /* Match a MODULE statement. */
2568 gfc_match_module (void)
2572 m
= gfc_match (" %s%t", &gfc_new_block
);
2576 if (gfc_add_flavor (&gfc_new_block
->attr
, FL_MODULE
,
2577 gfc_new_block
->name
, NULL
) == FAILURE
)
2584 /* Free equivalence sets and lists. Recursively is the easiest way to
2588 gfc_free_equiv (gfc_equiv
* eq
)
2594 gfc_free_equiv (eq
->eq
);
2595 gfc_free_equiv (eq
->next
);
2597 gfc_free_expr (eq
->expr
);
2602 /* Match an EQUIVALENCE statement. */
2605 gfc_match_equivalence (void)
2607 gfc_equiv
*eq
, *set
, *tail
;
2611 gfc_common_head
*common_head
= NULL
;
2619 eq
= gfc_get_equiv ();
2623 eq
->next
= gfc_current_ns
->equiv
;
2624 gfc_current_ns
->equiv
= eq
;
2626 if (gfc_match_char ('(') != MATCH_YES
)
2630 common_flag
= FALSE
;
2635 m
= gfc_match_equiv_variable (&set
->expr
);
2636 if (m
== MATCH_ERROR
)
2641 /* count the number of objects. */
2644 if (gfc_match_char ('%') == MATCH_YES
)
2646 gfc_error ("Derived type component %C is not a "
2647 "permitted EQUIVALENCE member");
2651 for (ref
= set
->expr
->ref
; ref
; ref
= ref
->next
)
2652 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
== AR_SECTION
)
2655 ("Array reference in EQUIVALENCE at %C cannot be an "
2660 sym
= set
->expr
->symtree
->n
.sym
;
2662 if (gfc_add_in_equivalence (&sym
->attr
, sym
->name
, NULL
)
2666 if (sym
->attr
.in_common
)
2669 common_head
= sym
->common_head
;
2672 if (gfc_match_char (')') == MATCH_YES
)
2675 if (gfc_match_char (',') != MATCH_YES
)
2678 set
->eq
= gfc_get_equiv ();
2684 gfc_error ("EQUIVALENCE at %C requires two or more objects");
2688 /* If one of the members of an equivalence is in common, then
2689 mark them all as being in common. Before doing this, check
2690 that members of the equivalence group are not in different
2693 for (set
= eq
; set
; set
= set
->eq
)
2695 sym
= set
->expr
->symtree
->n
.sym
;
2696 if (sym
->common_head
&& sym
->common_head
!= common_head
)
2698 gfc_error ("Attempt to indirectly overlap COMMON "
2699 "blocks %s and %s by EQUIVALENCE at %C",
2700 sym
->common_head
->name
,
2704 sym
->attr
.in_common
= 1;
2705 sym
->common_head
= common_head
;
2708 if (gfc_match_eos () == MATCH_YES
)
2710 if (gfc_match_char (',') != MATCH_YES
)
2717 gfc_syntax_error (ST_EQUIVALENCE
);
2723 gfc_free_equiv (gfc_current_ns
->equiv
);
2724 gfc_current_ns
->equiv
= eq
;
2729 /* Check that a statement function is not recursive. This is done by looking
2730 for the statement function symbol(sym) by looking recursively through its
2731 expression(e). If a reference to sym is found, true is returned. */
2733 recursive_stmt_fcn (gfc_expr
*e
, gfc_symbol
*sym
)
2735 gfc_actual_arglist
*arg
;
2742 switch (e
->expr_type
)
2745 for (arg
= e
->value
.function
.actual
; arg
; arg
= arg
->next
)
2747 if (sym
->name
== arg
->name
2748 || recursive_stmt_fcn (arg
->expr
, sym
))
2752 if (e
->symtree
== NULL
)
2755 /* Check the name before testing for nested recursion! */
2756 if (sym
->name
== e
->symtree
->n
.sym
->name
)
2759 /* Catch recursion via other statement functions. */
2760 if (e
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
2761 && e
->symtree
->n
.sym
->value
2762 && recursive_stmt_fcn (e
->symtree
->n
.sym
->value
, sym
))
2768 if (e
->symtree
&& sym
->name
== e
->symtree
->n
.sym
->name
)
2773 if (recursive_stmt_fcn (e
->value
.op
.op1
, sym
)
2774 || recursive_stmt_fcn (e
->value
.op
.op2
, sym
))
2782 /* Component references do not need to be checked. */
2785 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
2790 for (i
= 0; i
< ref
->u
.ar
.dimen
; i
++)
2792 if (recursive_stmt_fcn (ref
->u
.ar
.start
[i
], sym
)
2793 || recursive_stmt_fcn (ref
->u
.ar
.end
[i
], sym
)
2794 || recursive_stmt_fcn (ref
->u
.ar
.stride
[i
], sym
))
2800 if (recursive_stmt_fcn (ref
->u
.ss
.start
, sym
)
2801 || recursive_stmt_fcn (ref
->u
.ss
.end
, sym
))
2815 /* Match a statement function declaration. It is so easy to match
2816 non-statement function statements with a MATCH_ERROR as opposed to
2817 MATCH_NO that we suppress error message in most cases. */
2820 gfc_match_st_function (void)
2822 gfc_error_buf old_error
;
2827 m
= gfc_match_symbol (&sym
, 0);
2831 gfc_push_error (&old_error
);
2833 if (gfc_add_procedure (&sym
->attr
, PROC_ST_FUNCTION
,
2834 sym
->name
, NULL
) == FAILURE
)
2837 if (gfc_match_formal_arglist (sym
, 1, 0) != MATCH_YES
)
2840 m
= gfc_match (" = %e%t", &expr
);
2844 gfc_free_error (&old_error
);
2845 if (m
== MATCH_ERROR
)
2848 if (recursive_stmt_fcn (expr
, sym
))
2850 gfc_error ("Statement function at %L is recursive",
2860 gfc_pop_error (&old_error
);
2865 /***************** SELECT CASE subroutines ******************/
2867 /* Free a single case structure. */
2870 free_case (gfc_case
* p
)
2872 if (p
->low
== p
->high
)
2874 gfc_free_expr (p
->low
);
2875 gfc_free_expr (p
->high
);
2880 /* Free a list of case structures. */
2883 gfc_free_case_list (gfc_case
* p
)
2895 /* Match a single case selector. */
2898 match_case_selector (gfc_case
** cp
)
2903 c
= gfc_get_case ();
2904 c
->where
= gfc_current_locus
;
2906 if (gfc_match_char (':') == MATCH_YES
)
2908 m
= gfc_match_init_expr (&c
->high
);
2911 if (m
== MATCH_ERROR
)
2917 m
= gfc_match_init_expr (&c
->low
);
2918 if (m
== MATCH_ERROR
)
2923 /* If we're not looking at a ':' now, make a range out of a single
2924 target. Else get the upper bound for the case range. */
2925 if (gfc_match_char (':') != MATCH_YES
)
2929 m
= gfc_match_init_expr (&c
->high
);
2930 if (m
== MATCH_ERROR
)
2932 /* MATCH_NO is fine. It's OK if nothing is there! */
2940 gfc_error ("Expected initialization expression in CASE at %C");
2948 /* Match the end of a case statement. */
2951 match_case_eos (void)
2953 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2956 if (gfc_match_eos () == MATCH_YES
)
2959 gfc_gobble_whitespace ();
2961 m
= gfc_match_name (name
);
2965 if (strcmp (name
, gfc_current_block ()->name
) != 0)
2967 gfc_error ("Expected case name of '%s' at %C",
2968 gfc_current_block ()->name
);
2972 return gfc_match_eos ();
2976 /* Match a SELECT statement. */
2979 gfc_match_select (void)
2984 m
= gfc_match_label ();
2985 if (m
== MATCH_ERROR
)
2988 m
= gfc_match (" select case ( %e )%t", &expr
);
2992 new_st
.op
= EXEC_SELECT
;
2999 /* Match a CASE statement. */
3002 gfc_match_case (void)
3004 gfc_case
*c
, *head
, *tail
;
3009 if (gfc_current_state () != COMP_SELECT
)
3011 gfc_error ("Unexpected CASE statement at %C");
3015 if (gfc_match ("% default") == MATCH_YES
)
3017 m
= match_case_eos ();
3020 if (m
== MATCH_ERROR
)
3023 new_st
.op
= EXEC_SELECT
;
3024 c
= gfc_get_case ();
3025 c
->where
= gfc_current_locus
;
3026 new_st
.ext
.case_list
= c
;
3030 if (gfc_match_char ('(') != MATCH_YES
)
3035 if (match_case_selector (&c
) == MATCH_ERROR
)
3045 if (gfc_match_char (')') == MATCH_YES
)
3047 if (gfc_match_char (',') != MATCH_YES
)
3051 m
= match_case_eos ();
3054 if (m
== MATCH_ERROR
)
3057 new_st
.op
= EXEC_SELECT
;
3058 new_st
.ext
.case_list
= head
;
3063 gfc_error ("Syntax error in CASE-specification at %C");
3066 gfc_free_case_list (head
); /* new_st is cleaned up in parse.c. */
3070 /********************* WHERE subroutines ********************/
3072 /* Match the rest of a simple WHERE statement that follows an IF statement.
3076 match_simple_where (void)
3082 m
= gfc_match (" ( %e )", &expr
);
3086 m
= gfc_match_assignment ();
3089 if (m
== MATCH_ERROR
)
3092 if (gfc_match_eos () != MATCH_YES
)
3095 c
= gfc_get_code ();
3099 c
->next
= gfc_get_code ();
3102 gfc_clear_new_st ();
3104 new_st
.op
= EXEC_WHERE
;
3110 gfc_syntax_error (ST_WHERE
);
3113 gfc_free_expr (expr
);
3117 /* Match a WHERE statement. */
3120 gfc_match_where (gfc_statement
* st
)
3126 m0
= gfc_match_label ();
3127 if (m0
== MATCH_ERROR
)
3130 m
= gfc_match (" where ( %e )", &expr
);
3134 if (gfc_match_eos () == MATCH_YES
)
3136 *st
= ST_WHERE_BLOCK
;
3138 new_st
.op
= EXEC_WHERE
;
3143 m
= gfc_match_assignment ();
3145 gfc_syntax_error (ST_WHERE
);
3149 gfc_free_expr (expr
);
3153 /* We've got a simple WHERE statement. */
3155 c
= gfc_get_code ();
3159 c
->next
= gfc_get_code ();
3162 gfc_clear_new_st ();
3164 new_st
.op
= EXEC_WHERE
;
3171 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3172 new_st if successful. */
3175 gfc_match_elsewhere (void)
3177 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3181 if (gfc_current_state () != COMP_WHERE
)
3183 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3189 if (gfc_match_char ('(') == MATCH_YES
)
3191 m
= gfc_match_expr (&expr
);
3194 if (m
== MATCH_ERROR
)
3197 if (gfc_match_char (')') != MATCH_YES
)
3201 if (gfc_match_eos () != MATCH_YES
)
3202 { /* Better be a name at this point */
3203 m
= gfc_match_name (name
);
3206 if (m
== MATCH_ERROR
)
3209 if (gfc_match_eos () != MATCH_YES
)
3212 if (strcmp (name
, gfc_current_block ()->name
) != 0)
3214 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3215 name
, gfc_current_block ()->name
);
3220 new_st
.op
= EXEC_WHERE
;
3225 gfc_syntax_error (ST_ELSEWHERE
);
3228 gfc_free_expr (expr
);
3233 /******************** FORALL subroutines ********************/
3235 /* Free a list of FORALL iterators. */
3238 gfc_free_forall_iterator (gfc_forall_iterator
* iter
)
3240 gfc_forall_iterator
*next
;
3246 gfc_free_expr (iter
->var
);
3247 gfc_free_expr (iter
->start
);
3248 gfc_free_expr (iter
->end
);
3249 gfc_free_expr (iter
->stride
);
3257 /* Match an iterator as part of a FORALL statement. The format is:
3259 <var> = <start>:<end>[:<stride>][, <scalar mask>] */
3262 match_forall_iterator (gfc_forall_iterator
** result
)
3264 gfc_forall_iterator
*iter
;
3268 where
= gfc_current_locus
;
3269 iter
= gfc_getmem (sizeof (gfc_forall_iterator
));
3271 m
= gfc_match_variable (&iter
->var
, 0);
3275 if (gfc_match_char ('=') != MATCH_YES
)
3281 m
= gfc_match_expr (&iter
->start
);
3285 if (gfc_match_char (':') != MATCH_YES
)
3288 m
= gfc_match_expr (&iter
->end
);
3291 if (m
== MATCH_ERROR
)
3294 if (gfc_match_char (':') == MATCH_NO
)
3295 iter
->stride
= gfc_int_expr (1);
3298 m
= gfc_match_expr (&iter
->stride
);
3301 if (m
== MATCH_ERROR
)
3309 gfc_error ("Syntax error in FORALL iterator at %C");
3313 gfc_current_locus
= where
;
3314 gfc_free_forall_iterator (iter
);
3319 /* Match the header of a FORALL statement. */
3322 match_forall_header (gfc_forall_iterator
** phead
, gfc_expr
** mask
)
3324 gfc_forall_iterator
*head
, *tail
, *new;
3327 gfc_gobble_whitespace ();
3332 if (gfc_match_char ('(') != MATCH_YES
)
3335 m
= match_forall_iterator (&new);
3336 if (m
== MATCH_ERROR
)
3345 if (gfc_match_char (',') != MATCH_YES
)
3348 m
= match_forall_iterator (&new);
3349 if (m
== MATCH_ERROR
)
3358 /* Have to have a mask expression */
3360 m
= gfc_match_expr (mask
);
3363 if (m
== MATCH_ERROR
)
3369 if (gfc_match_char (')') == MATCH_NO
)
3376 gfc_syntax_error (ST_FORALL
);
3379 gfc_free_expr (*mask
);
3380 gfc_free_forall_iterator (head
);
3385 /* Match the rest of a simple FORALL statement that follows an IF statement.
3389 match_simple_forall (void)
3391 gfc_forall_iterator
*head
;
3400 m
= match_forall_header (&head
, &mask
);
3407 m
= gfc_match_assignment ();
3409 if (m
== MATCH_ERROR
)
3413 m
= gfc_match_pointer_assignment ();
3414 if (m
== MATCH_ERROR
)
3420 c
= gfc_get_code ();
3422 c
->loc
= gfc_current_locus
;
3424 if (gfc_match_eos () != MATCH_YES
)
3427 gfc_clear_new_st ();
3428 new_st
.op
= EXEC_FORALL
;
3430 new_st
.ext
.forall_iterator
= head
;
3431 new_st
.block
= gfc_get_code ();
3433 new_st
.block
->op
= EXEC_FORALL
;
3434 new_st
.block
->next
= c
;
3439 gfc_syntax_error (ST_FORALL
);
3442 gfc_free_forall_iterator (head
);
3443 gfc_free_expr (mask
);
3449 /* Match a FORALL statement. */
3452 gfc_match_forall (gfc_statement
* st
)
3454 gfc_forall_iterator
*head
;
3463 m0
= gfc_match_label ();
3464 if (m0
== MATCH_ERROR
)
3467 m
= gfc_match (" forall");
3471 m
= match_forall_header (&head
, &mask
);
3472 if (m
== MATCH_ERROR
)
3477 if (gfc_match_eos () == MATCH_YES
)
3479 *st
= ST_FORALL_BLOCK
;
3481 new_st
.op
= EXEC_FORALL
;
3483 new_st
.ext
.forall_iterator
= head
;
3488 m
= gfc_match_assignment ();
3489 if (m
== MATCH_ERROR
)
3493 m
= gfc_match_pointer_assignment ();
3494 if (m
== MATCH_ERROR
)
3500 c
= gfc_get_code ();
3503 if (gfc_match_eos () != MATCH_YES
)
3506 gfc_clear_new_st ();
3507 new_st
.op
= EXEC_FORALL
;
3509 new_st
.ext
.forall_iterator
= head
;
3510 new_st
.block
= gfc_get_code ();
3512 new_st
.block
->op
= EXEC_FORALL
;
3513 new_st
.block
->next
= c
;
3519 gfc_syntax_error (ST_FORALL
);
3522 gfc_free_forall_iterator (head
);
3523 gfc_free_expr (mask
);
3524 gfc_free_statements (c
);