1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA
35 /* For matching and debugging purposes. Order matters here! The
36 unary operators /must/ precede the binary plus and minus, or
37 the expression parser breaks. */
39 mstring intrinsic_operators
[] = {
40 minit ("+", INTRINSIC_UPLUS
),
41 minit ("-", INTRINSIC_UMINUS
),
42 minit ("+", INTRINSIC_PLUS
),
43 minit ("-", INTRINSIC_MINUS
),
44 minit ("**", INTRINSIC_POWER
),
45 minit ("//", INTRINSIC_CONCAT
),
46 minit ("*", INTRINSIC_TIMES
),
47 minit ("/", INTRINSIC_DIVIDE
),
48 minit (".and.", INTRINSIC_AND
),
49 minit (".or.", INTRINSIC_OR
),
50 minit (".eqv.", INTRINSIC_EQV
),
51 minit (".neqv.", INTRINSIC_NEQV
),
52 minit (".eq.", INTRINSIC_EQ
),
53 minit ("==", INTRINSIC_EQ
),
54 minit (".ne.", INTRINSIC_NE
),
55 minit ("/=", INTRINSIC_NE
),
56 minit (".ge.", INTRINSIC_GE
),
57 minit (">=", INTRINSIC_GE
),
58 minit (".le.", INTRINSIC_LE
),
59 minit ("<=", INTRINSIC_LE
),
60 minit (".lt.", INTRINSIC_LT
),
61 minit ("<", INTRINSIC_LT
),
62 minit (".gt.", INTRINSIC_GT
),
63 minit (">", INTRINSIC_GT
),
64 minit (".not.", INTRINSIC_NOT
),
65 minit (NULL
, INTRINSIC_NONE
)
69 /******************** Generic matching subroutines ************************/
71 /* In free form, match at least one space. Always matches in fixed
75 gfc_match_space (void)
80 if (gfc_current_form
== FORM_FIXED
)
83 old_loc
= gfc_current_locus
;
86 if (!gfc_is_whitespace (c
))
88 gfc_current_locus
= old_loc
;
92 gfc_gobble_whitespace ();
98 /* Match an end of statement. End of statement is optional
99 whitespace, followed by a ';' or '\n' or comment '!'. If a
100 semicolon is found, we continue to eat whitespace and semicolons. */
112 old_loc
= gfc_current_locus
;
113 gfc_gobble_whitespace ();
115 c
= gfc_next_char ();
121 c
= gfc_next_char ();
138 gfc_current_locus
= old_loc
;
139 return (flag
) ? MATCH_YES
: MATCH_NO
;
143 /* Match a literal integer on the input, setting the value on
144 MATCH_YES. Literal ints occur in kind-parameters as well as
145 old-style character length specifications. */
148 gfc_match_small_literal_int (int *value
)
154 old_loc
= gfc_current_locus
;
156 gfc_gobble_whitespace ();
157 c
= gfc_next_char ();
161 gfc_current_locus
= old_loc
;
169 old_loc
= gfc_current_locus
;
170 c
= gfc_next_char ();
175 i
= 10 * i
+ c
- '0';
179 gfc_error ("Integer too large at %C");
184 gfc_current_locus
= old_loc
;
191 /* Match a small, constant integer expression, like in a kind
192 statement. On MATCH_YES, 'value' is set. */
195 gfc_match_small_int (int *value
)
202 m
= gfc_match_expr (&expr
);
206 p
= gfc_extract_int (expr
, &i
);
207 gfc_free_expr (expr
);
220 /* Matches a statement label. Uses gfc_match_small_literal_int() to
221 do most of the work. */
224 gfc_match_st_label (gfc_st_label
** label
, int allow_zero
)
230 old_loc
= gfc_current_locus
;
232 m
= gfc_match_small_literal_int (&i
);
236 if (((i
== 0) && allow_zero
) || i
<= 99999)
238 *label
= gfc_get_st_label (i
);
242 gfc_error ("Statement label at %C is out of range");
243 gfc_current_locus
= old_loc
;
248 /* Match and validate a label associated with a named IF, DO or SELECT
249 statement. If the symbol does not have the label attribute, we add
250 it. We also make sure the symbol does not refer to another
251 (active) block. A matched label is pointed to by gfc_new_block. */
254 gfc_match_label (void)
256 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
260 gfc_new_block
= NULL
;
262 m
= gfc_match (" %n :", name
);
266 if (gfc_get_symbol (name
, NULL
, &gfc_new_block
))
268 gfc_error ("Label name '%s' at %C is ambiguous", name
);
272 if (gfc_new_block
->attr
.flavor
!= FL_LABEL
273 && gfc_add_flavor (&gfc_new_block
->attr
, FL_LABEL
, NULL
) == FAILURE
)
276 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
277 if (p
->sym
== gfc_new_block
)
279 gfc_error ("Label %s at %C already in use by a parent block",
280 gfc_new_block
->name
);
288 /* Try and match the input against an array of possibilities. If one
289 potential matching string is a substring of another, the longest
290 match takes precedence. Spaces in the target strings are optional
291 spaces that do not necessarily have to be found in the input
292 stream. In fixed mode, spaces never appear. If whitespace is
293 matched, it matches unlimited whitespace in the input. For this
294 reason, the 'mp' member of the mstring structure is used to track
295 the progress of each potential match.
297 If there is no match we return the tag associated with the
298 terminating NULL mstring structure and leave the locus pointer
299 where it started. If there is a match we return the tag member of
300 the matched mstring and leave the locus pointer after the matched
303 A '%' character is a mandatory space. */
306 gfc_match_strings (mstring
* a
)
308 mstring
*p
, *best_match
;
309 int no_match
, c
, possibles
;
314 for (p
= a
; p
->string
!= NULL
; p
++)
323 match_loc
= gfc_current_locus
;
325 gfc_gobble_whitespace ();
327 while (possibles
> 0)
329 c
= gfc_next_char ();
331 /* Apply the next character to the current possibilities. */
332 for (p
= a
; p
->string
!= NULL
; p
++)
339 /* Space matches 1+ whitespace(s). */
340 if ((gfc_current_form
== FORM_FREE
)
341 && gfc_is_whitespace (c
))
359 match_loc
= gfc_current_locus
;
367 gfc_current_locus
= match_loc
;
369 return (best_match
== NULL
) ? no_match
: best_match
->tag
;
373 /* See if the current input looks like a name of some sort. Modifies
374 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long. */
377 gfc_match_name (char *buffer
)
382 old_loc
= gfc_current_locus
;
383 gfc_gobble_whitespace ();
385 c
= gfc_next_char ();
388 gfc_current_locus
= old_loc
;
398 if (i
> gfc_option
.max_identifier_length
)
400 gfc_error ("Name at %C is too long");
404 old_loc
= gfc_current_locus
;
405 c
= gfc_next_char ();
409 || (gfc_option
.flag_dollar_ok
&& c
== '$'));
412 gfc_current_locus
= old_loc
;
418 /* Match a symbol on the input. Modifies the pointer to the symbol
419 pointer if successful. */
422 gfc_match_sym_tree (gfc_symtree
** matched_symbol
, int host_assoc
)
424 char buffer
[GFC_MAX_SYMBOL_LEN
+ 1];
427 m
= gfc_match_name (buffer
);
432 return (gfc_get_ha_sym_tree (buffer
, matched_symbol
))
433 ? MATCH_ERROR
: MATCH_YES
;
435 if (gfc_get_sym_tree (buffer
, NULL
, matched_symbol
))
443 gfc_match_symbol (gfc_symbol
** matched_symbol
, int host_assoc
)
448 m
= gfc_match_sym_tree (&st
, host_assoc
);
453 *matched_symbol
= st
->n
.sym
;
455 *matched_symbol
= NULL
;
460 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
461 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
465 gfc_match_intrinsic_op (gfc_intrinsic_op
* result
)
469 op
= (gfc_intrinsic_op
) gfc_match_strings (intrinsic_operators
);
471 if (op
== INTRINSIC_NONE
)
479 /* Match a loop control phrase:
481 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
483 If the final integer expression is not present, a constant unity
484 expression is returned. We don't return MATCH_ERROR until after
485 the equals sign is seen. */
488 gfc_match_iterator (gfc_iterator
* iter
, int init_flag
)
490 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
491 gfc_expr
*var
, *e1
, *e2
, *e3
;
495 /* Match the start of an iterator without affecting the symbol
498 start
= gfc_current_locus
;
499 m
= gfc_match (" %n =", name
);
500 gfc_current_locus
= start
;
505 m
= gfc_match_variable (&var
, 0);
509 gfc_match_char ('=');
513 if (var
->ref
!= NULL
)
515 gfc_error ("Loop variable at %C cannot be a sub-component");
519 if (var
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
521 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
522 var
->symtree
->n
.sym
->name
);
526 if (var
->symtree
->n
.sym
->attr
.pointer
)
528 gfc_error ("Loop variable at %C cannot have the POINTER attribute");
532 m
= init_flag
? gfc_match_init_expr (&e1
) : gfc_match_expr (&e1
);
535 if (m
== MATCH_ERROR
)
538 if (gfc_match_char (',') != MATCH_YES
)
541 m
= init_flag
? gfc_match_init_expr (&e2
) : gfc_match_expr (&e2
);
544 if (m
== MATCH_ERROR
)
547 if (gfc_match_char (',') != MATCH_YES
)
549 e3
= gfc_int_expr (1);
553 m
= init_flag
? gfc_match_init_expr (&e3
) : gfc_match_expr (&e3
);
554 if (m
== MATCH_ERROR
)
558 gfc_error ("Expected a step value in iterator at %C");
570 gfc_error ("Syntax error in iterator at %C");
581 /* Tries to match the next non-whitespace character on the input.
582 This subroutine does not return MATCH_ERROR. */
585 gfc_match_char (char c
)
589 where
= gfc_current_locus
;
590 gfc_gobble_whitespace ();
592 if (gfc_next_char () == c
)
595 gfc_current_locus
= where
;
600 /* General purpose matching subroutine. The target string is a
601 scanf-like format string in which spaces correspond to arbitrary
602 whitespace (including no whitespace), characters correspond to
603 themselves. The %-codes are:
605 %% Literal percent sign
606 %e Expression, pointer to a pointer is set
607 %s Symbol, pointer to the symbol is set
608 %n Name, character buffer is set to name
609 %t Matches end of statement.
610 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
611 %l Matches a statement label
612 %v Matches a variable expression (an lvalue)
613 % Matches a required space (in free form) and optional spaces. */
616 gfc_match (const char *target
, ...)
618 gfc_st_label
**label
;
627 old_loc
= gfc_current_locus
;
628 va_start (argp
, target
);
638 gfc_gobble_whitespace ();
649 vp
= va_arg (argp
, void **);
650 n
= gfc_match_expr ((gfc_expr
**) vp
);
661 vp
= va_arg (argp
, void **);
662 n
= gfc_match_variable ((gfc_expr
**) vp
, 0);
673 vp
= va_arg (argp
, void **);
674 n
= gfc_match_symbol ((gfc_symbol
**) vp
, 0);
685 np
= va_arg (argp
, char *);
686 n
= gfc_match_name (np
);
697 label
= va_arg (argp
, gfc_st_label
**);
698 n
= gfc_match_st_label (label
, 0);
709 ip
= va_arg (argp
, int *);
710 n
= gfc_match_intrinsic_op ((gfc_intrinsic_op
*) ip
);
721 if (gfc_match_eos () != MATCH_YES
)
729 if (gfc_match_space () == MATCH_YES
)
735 break; /* Fall through to character matcher */
738 gfc_internal_error ("gfc_match(): Bad match code %c", c
);
742 if (c
== gfc_next_char ())
752 /* Clean up after a failed match. */
753 gfc_current_locus
= old_loc
;
754 va_start (argp
, target
);
757 for (; matches
> 0; matches
--)
767 /* Matches that don't have to be undone */
772 (void)va_arg (argp
, void **);
777 vp
= va_arg (argp
, void **);
791 /*********************** Statement level matching **********************/
793 /* Matches the start of a program unit, which is the program keyword
794 followed by an optional symbol. */
797 gfc_match_program (void)
802 m
= gfc_match_eos ();
806 m
= gfc_match ("% %s%t", &sym
);
810 gfc_error ("Invalid form of PROGRAM statement at %C");
814 if (m
== MATCH_ERROR
)
817 if (gfc_add_flavor (&sym
->attr
, FL_PROGRAM
, NULL
) == FAILURE
)
826 /* Match a simple assignment statement. */
829 gfc_match_assignment (void)
831 gfc_expr
*lvalue
, *rvalue
;
835 old_loc
= gfc_current_locus
;
837 lvalue
= rvalue
= NULL
;
838 m
= gfc_match (" %v =", &lvalue
);
842 m
= gfc_match (" %e%t", &rvalue
);
846 gfc_set_sym_referenced (lvalue
->symtree
->n
.sym
);
848 new_st
.op
= EXEC_ASSIGN
;
849 new_st
.expr
= lvalue
;
850 new_st
.expr2
= rvalue
;
855 gfc_current_locus
= old_loc
;
856 gfc_free_expr (lvalue
);
857 gfc_free_expr (rvalue
);
862 /* Match a pointer assignment statement. */
865 gfc_match_pointer_assignment (void)
867 gfc_expr
*lvalue
, *rvalue
;
871 old_loc
= gfc_current_locus
;
873 lvalue
= rvalue
= NULL
;
875 m
= gfc_match (" %v =>", &lvalue
);
882 m
= gfc_match (" %e%t", &rvalue
);
886 new_st
.op
= EXEC_POINTER_ASSIGN
;
887 new_st
.expr
= lvalue
;
888 new_st
.expr2
= rvalue
;
893 gfc_current_locus
= old_loc
;
894 gfc_free_expr (lvalue
);
895 gfc_free_expr (rvalue
);
900 /* The IF statement is a bit of a pain. First of all, there are three
901 forms of it, the simple IF, the IF that starts a block and the
904 There is a problem with the simple IF and that is the fact that we
905 only have a single level of undo information on symbols. What this
906 means is for a simple IF, we must re-match the whole IF statement
907 multiple times in order to guarantee that the symbol table ends up
908 in the proper state. */
911 gfc_match_if (gfc_statement
* if_type
)
914 gfc_st_label
*l1
, *l2
, *l3
;
919 n
= gfc_match_label ();
920 if (n
== MATCH_ERROR
)
923 old_loc
= gfc_current_locus
;
925 m
= gfc_match (" if ( %e", &expr
);
929 if (gfc_match_char (')') != MATCH_YES
)
931 gfc_error ("Syntax error in IF-expression at %C");
932 gfc_free_expr (expr
);
936 m
= gfc_match (" %l , %l , %l%t", &l1
, &l2
, &l3
);
943 ("Block label not appropriate for arithmetic IF statement "
946 gfc_free_expr (expr
);
950 if (gfc_reference_st_label (l1
, ST_LABEL_TARGET
) == FAILURE
951 || gfc_reference_st_label (l2
, ST_LABEL_TARGET
) == FAILURE
952 || gfc_reference_st_label (l3
, ST_LABEL_TARGET
) == FAILURE
)
955 gfc_free_expr (expr
);
959 new_st
.op
= EXEC_ARITHMETIC_IF
;
965 *if_type
= ST_ARITHMETIC_IF
;
969 if (gfc_match (" then %t") == MATCH_YES
)
974 *if_type
= ST_IF_BLOCK
;
980 gfc_error ("Block label is not appropriate IF statement at %C");
982 gfc_free_expr (expr
);
986 /* At this point the only thing left is a simple IF statement. At
987 this point, n has to be MATCH_NO, so we don't have to worry about
988 re-matching a block label. From what we've got so far, try
989 matching an assignment. */
991 *if_type
= ST_SIMPLE_IF
;
993 m
= gfc_match_assignment ();
997 gfc_free_expr (expr
);
999 gfc_current_locus
= old_loc
;
1001 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match */
1003 m
= gfc_match_pointer_assignment ();
1007 gfc_free_expr (expr
);
1008 gfc_undo_symbols ();
1009 gfc_current_locus
= old_loc
;
1011 gfc_match (" if ( %e ) ", &expr
); /* Guaranteed to match */
1013 /* Look at the next keyword to see which matcher to call. Matching
1014 the keyword doesn't affect the symbol table, so we don't have to
1015 restore between tries. */
1017 #define match(string, subr, statement) \
1018 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1022 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
)
1023 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
)
1024 match ("call", gfc_match_call
, ST_CALL
)
1025 match ("close", gfc_match_close
, ST_CLOSE
)
1026 match ("continue", gfc_match_continue
, ST_CONTINUE
)
1027 match ("cycle", gfc_match_cycle
, ST_CYCLE
)
1028 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
)
1029 match ("end file", gfc_match_endfile
, ST_END_FILE
)
1030 match ("exit", gfc_match_exit
, ST_EXIT
)
1031 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
)
1032 match ("go to", gfc_match_goto
, ST_GOTO
)
1033 match ("inquire", gfc_match_inquire
, ST_INQUIRE
)
1034 match ("nullify", gfc_match_nullify
, ST_NULLIFY
)
1035 match ("open", gfc_match_open
, ST_OPEN
)
1036 match ("pause", gfc_match_pause
, ST_NONE
)
1037 match ("print", gfc_match_print
, ST_WRITE
)
1038 match ("read", gfc_match_read
, ST_READ
)
1039 match ("return", gfc_match_return
, ST_RETURN
)
1040 match ("rewind", gfc_match_rewind
, ST_REWIND
)
1041 match ("pause", gfc_match_stop
, ST_PAUSE
)
1042 match ("stop", gfc_match_stop
, ST_STOP
)
1043 match ("write", gfc_match_write
, ST_WRITE
)
1045 /* All else has failed, so give up. See if any of the matchers has
1046 stored an error message of some sort. */
1047 if (gfc_error_check () == 0)
1048 gfc_error ("Unclassifiable statement in IF-clause at %C");
1050 gfc_free_expr (expr
);
1055 gfc_error ("Syntax error in IF-clause at %C");
1058 gfc_free_expr (expr
);
1062 /* At this point, we've matched the single IF and the action clause
1063 is in new_st. Rearrange things so that the IF statement appears
1066 p
= gfc_get_code ();
1067 p
->next
= gfc_get_code ();
1069 p
->next
->loc
= gfc_current_locus
;
1074 gfc_clear_new_st ();
1076 new_st
.op
= EXEC_IF
;
1085 /* Match an ELSE statement. */
1088 gfc_match_else (void)
1090 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1092 if (gfc_match_eos () == MATCH_YES
)
1095 if (gfc_match_name (name
) != MATCH_YES
1096 || gfc_current_block () == NULL
1097 || gfc_match_eos () != MATCH_YES
)
1099 gfc_error ("Unexpected junk after ELSE statement at %C");
1103 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1105 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1106 name
, gfc_current_block ()->name
);
1114 /* Match an ELSE IF statement. */
1117 gfc_match_elseif (void)
1119 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1123 m
= gfc_match (" ( %e ) then", &expr
);
1127 if (gfc_match_eos () == MATCH_YES
)
1130 if (gfc_match_name (name
) != MATCH_YES
1131 || gfc_current_block () == NULL
1132 || gfc_match_eos () != MATCH_YES
)
1134 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1138 if (strcmp (name
, gfc_current_block ()->name
) != 0)
1140 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1141 name
, gfc_current_block ()->name
);
1146 new_st
.op
= EXEC_IF
;
1151 gfc_free_expr (expr
);
1156 /* Free a gfc_iterator structure. */
1159 gfc_free_iterator (gfc_iterator
* iter
, int flag
)
1165 gfc_free_expr (iter
->var
);
1166 gfc_free_expr (iter
->start
);
1167 gfc_free_expr (iter
->end
);
1168 gfc_free_expr (iter
->step
);
1175 /* Match a DO statement. */
1180 gfc_iterator iter
, *ip
;
1182 gfc_st_label
*label
;
1185 old_loc
= gfc_current_locus
;
1188 iter
.var
= iter
.start
= iter
.end
= iter
.step
= NULL
;
1190 m
= gfc_match_label ();
1191 if (m
== MATCH_ERROR
)
1194 if (gfc_match (" do") != MATCH_YES
)
1197 m
= gfc_match_st_label (&label
, 0);
1198 if (m
== MATCH_ERROR
)
1201 /* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1203 if (gfc_match_eos () == MATCH_YES
)
1205 iter
.end
= gfc_logical_expr (1, NULL
);
1206 new_st
.op
= EXEC_DO_WHILE
;
1210 /* match an optional comma, if no comma is found a space is obligatory. */
1211 if (gfc_match_char(',') != MATCH_YES
1212 && gfc_match ("% ") != MATCH_YES
)
1215 /* See if we have a DO WHILE. */
1216 if (gfc_match (" while ( %e )%t", &iter
.end
) == MATCH_YES
)
1218 new_st
.op
= EXEC_DO_WHILE
;
1222 /* The abortive DO WHILE may have done something to the symbol
1223 table, so we start over: */
1224 gfc_undo_symbols ();
1225 gfc_current_locus
= old_loc
;
1227 gfc_match_label (); /* This won't error */
1228 gfc_match (" do "); /* This will work */
1230 gfc_match_st_label (&label
, 0); /* Can't error out */
1231 gfc_match_char (','); /* Optional comma */
1233 m
= gfc_match_iterator (&iter
, 0);
1236 if (m
== MATCH_ERROR
)
1239 if (gfc_match_eos () != MATCH_YES
)
1241 gfc_syntax_error (ST_DO
);
1245 new_st
.op
= EXEC_DO
;
1249 && gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
1252 new_st
.label
= label
;
1254 if (new_st
.op
== EXEC_DO_WHILE
)
1255 new_st
.expr
= iter
.end
;
1258 new_st
.ext
.iterator
= ip
= gfc_get_iterator ();
1265 gfc_free_iterator (&iter
, 0);
1271 /* Match an EXIT or CYCLE statement. */
1274 match_exit_cycle (gfc_statement st
, gfc_exec_op op
)
1280 if (gfc_match_eos () == MATCH_YES
)
1284 m
= gfc_match ("% %s%t", &sym
);
1285 if (m
== MATCH_ERROR
)
1289 gfc_syntax_error (st
);
1293 if (sym
->attr
.flavor
!= FL_LABEL
)
1295 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1296 sym
->name
, gfc_ascii_statement (st
));
1301 /* Find the loop mentioned specified by the label (or lack of a
1303 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1304 if (p
->state
== COMP_DO
&& (sym
== NULL
|| sym
== p
->sym
))
1310 gfc_error ("%s statement at %C is not within a loop",
1311 gfc_ascii_statement (st
));
1313 gfc_error ("%s statement at %C is not within loop '%s'",
1314 gfc_ascii_statement (st
), sym
->name
);
1319 /* Save the first statement in the loop - needed by the backend. */
1320 new_st
.ext
.whichloop
= p
->head
;
1323 /* new_st.sym = sym;*/
1329 /* Match the EXIT statement. */
1332 gfc_match_exit (void)
1335 return match_exit_cycle (ST_EXIT
, EXEC_EXIT
);
1339 /* Match the CYCLE statement. */
1342 gfc_match_cycle (void)
1345 return match_exit_cycle (ST_CYCLE
, EXEC_CYCLE
);
1349 /* Match a number or character constant after a STOP or PAUSE statement. */
1352 gfc_match_stopcode (gfc_statement st
)
1361 if (gfc_match_eos () != MATCH_YES
)
1363 m
= gfc_match_small_literal_int (&stop_code
);
1364 if (m
== MATCH_ERROR
)
1367 if (m
== MATCH_YES
&& stop_code
> 99999)
1369 gfc_error ("STOP code out of range at %C");
1375 /* Try a character constant. */
1376 m
= gfc_match_expr (&e
);
1377 if (m
== MATCH_ERROR
)
1381 if (e
->ts
.type
!= BT_CHARACTER
|| e
->expr_type
!= EXPR_CONSTANT
)
1385 if (gfc_match_eos () != MATCH_YES
)
1389 if (gfc_pure (NULL
))
1391 gfc_error ("%s statement not allowed in PURE procedure at %C",
1392 gfc_ascii_statement (st
));
1396 new_st
.op
= st
== ST_STOP
? EXEC_STOP
: EXEC_PAUSE
;
1398 new_st
.ext
.stop_code
= stop_code
;
1403 gfc_syntax_error (st
);
1411 /* Match the (deprecated) PAUSE statement. */
1414 gfc_match_pause (void)
1418 m
= gfc_match_stopcode (ST_PAUSE
);
1421 if (gfc_notify_std (GFC_STD_F95_DEL
,
1422 "Obsolete: PAUSE statement at %C")
1430 /* Match the STOP statement. */
1433 gfc_match_stop (void)
1435 return gfc_match_stopcode (ST_STOP
);
1439 /* Match a CONTINUE statement. */
1442 gfc_match_continue (void)
1445 if (gfc_match_eos () != MATCH_YES
)
1447 gfc_syntax_error (ST_CONTINUE
);
1451 new_st
.op
= EXEC_CONTINUE
;
1456 /* Match the (deprecated) ASSIGN statement. */
1459 gfc_match_assign (void)
1462 gfc_st_label
*label
;
1464 if (gfc_match (" %l", &label
) == MATCH_YES
)
1466 if (gfc_reference_st_label (label
, ST_LABEL_UNKNOWN
) == FAILURE
)
1468 if (gfc_match (" to %v%t", &expr
) == MATCH_YES
)
1470 if (gfc_notify_std (GFC_STD_F95_DEL
,
1471 "Obsolete: ASSIGN statement at %C")
1475 expr
->symtree
->n
.sym
->attr
.assign
= 1;
1477 new_st
.op
= EXEC_LABEL_ASSIGN
;
1478 new_st
.label
= label
;
1487 /* Match the GO TO statement. As a computed GOTO statement is
1488 matched, it is transformed into an equivalent SELECT block. No
1489 tree is necessary, and the resulting jumps-to-jumps are
1490 specifically optimized away by the back end. */
1493 gfc_match_goto (void)
1495 gfc_code
*head
, *tail
;
1498 gfc_st_label
*label
;
1502 if (gfc_match (" %l%t", &label
) == MATCH_YES
)
1504 if (gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
1507 new_st
.op
= EXEC_GOTO
;
1508 new_st
.label
= label
;
1512 /* The assigned GO TO statement. */
1514 if (gfc_match_variable (&expr
, 0) == MATCH_YES
)
1516 if (gfc_notify_std (GFC_STD_F95_DEL
,
1517 "Obsolete: Assigned GOTO statement at %C")
1521 expr
->symtree
->n
.sym
->attr
.assign
= 1;
1522 new_st
.op
= EXEC_GOTO
;
1525 if (gfc_match_eos () == MATCH_YES
)
1528 /* Match label list. */
1529 gfc_match_char (',');
1530 if (gfc_match_char ('(') != MATCH_YES
)
1532 gfc_syntax_error (ST_GOTO
);
1539 m
= gfc_match_st_label (&label
, 0);
1543 if (gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
1547 head
= tail
= gfc_get_code ();
1550 tail
->block
= gfc_get_code ();
1554 tail
->label
= label
;
1555 tail
->op
= EXEC_GOTO
;
1557 while (gfc_match_char (',') == MATCH_YES
);
1559 if (gfc_match (")%t") != MATCH_YES
)
1565 "Statement label list in GOTO at %C cannot be empty");
1568 new_st
.block
= head
;
1573 /* Last chance is a computed GO TO statement. */
1574 if (gfc_match_char ('(') != MATCH_YES
)
1576 gfc_syntax_error (ST_GOTO
);
1585 m
= gfc_match_st_label (&label
, 0);
1589 if (gfc_reference_st_label (label
, ST_LABEL_TARGET
) == FAILURE
)
1593 head
= tail
= gfc_get_code ();
1596 tail
->block
= gfc_get_code ();
1600 cp
= gfc_get_case ();
1601 cp
->low
= cp
->high
= gfc_int_expr (i
++);
1603 tail
->op
= EXEC_SELECT
;
1604 tail
->ext
.case_list
= cp
;
1606 tail
->next
= gfc_get_code ();
1607 tail
->next
->op
= EXEC_GOTO
;
1608 tail
->next
->label
= label
;
1610 while (gfc_match_char (',') == MATCH_YES
);
1612 if (gfc_match_char (')') != MATCH_YES
)
1617 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1621 /* Get the rest of the statement. */
1622 gfc_match_char (',');
1624 if (gfc_match (" %e%t", &expr
) != MATCH_YES
)
1627 /* At this point, a computed GOTO has been fully matched and an
1628 equivalent SELECT statement constructed. */
1630 new_st
.op
= EXEC_SELECT
;
1633 /* Hack: For a "real" SELECT, the expression is in expr. We put
1634 it in expr2 so we can distinguish then and produce the correct
1636 new_st
.expr2
= expr
;
1637 new_st
.block
= head
;
1641 gfc_syntax_error (ST_GOTO
);
1643 gfc_free_statements (head
);
1648 /* Frees a list of gfc_alloc structures. */
1651 gfc_free_alloc_list (gfc_alloc
* p
)
1658 gfc_free_expr (p
->expr
);
1664 /* Match an ALLOCATE statement. */
1667 gfc_match_allocate (void)
1669 gfc_alloc
*head
, *tail
;
1676 if (gfc_match_char ('(') != MATCH_YES
)
1682 head
= tail
= gfc_get_alloc ();
1685 tail
->next
= gfc_get_alloc ();
1689 m
= gfc_match_variable (&tail
->expr
, 0);
1692 if (m
== MATCH_ERROR
)
1696 && gfc_impure_variable (tail
->expr
->symtree
->n
.sym
))
1698 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1703 if (gfc_match_char (',') != MATCH_YES
)
1706 m
= gfc_match (" stat = %v", &stat
);
1707 if (m
== MATCH_ERROR
)
1715 if (stat
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
1718 ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1719 "INTENT(IN)", stat
->symtree
->n
.sym
->name
);
1723 if (gfc_pure (NULL
) && gfc_impure_variable (stat
->symtree
->n
.sym
))
1726 ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1732 if (gfc_match (" )%t") != MATCH_YES
)
1735 new_st
.op
= EXEC_ALLOCATE
;
1737 new_st
.ext
.alloc_list
= head
;
1742 gfc_syntax_error (ST_ALLOCATE
);
1745 gfc_free_expr (stat
);
1746 gfc_free_alloc_list (head
);
1751 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
1752 a set of pointer assignments to intrinsic NULL(). */
1755 gfc_match_nullify (void)
1763 if (gfc_match_char ('(') != MATCH_YES
)
1768 m
= gfc_match_variable (&p
, 0);
1769 if (m
== MATCH_ERROR
)
1774 if (gfc_pure (NULL
) && gfc_impure_variable (p
->symtree
->n
.sym
))
1777 ("Illegal variable in NULLIFY at %C for a PURE procedure");
1781 /* build ' => NULL() ' */
1782 e
= gfc_get_expr ();
1783 e
->where
= gfc_current_locus
;
1784 e
->expr_type
= EXPR_NULL
;
1785 e
->ts
.type
= BT_UNKNOWN
;
1792 tail
->next
= gfc_get_code ();
1796 tail
->op
= EXEC_POINTER_ASSIGN
;
1800 if (gfc_match_char (')') == MATCH_YES
)
1802 if (gfc_match_char (',') != MATCH_YES
)
1809 gfc_syntax_error (ST_NULLIFY
);
1812 gfc_free_statements (tail
);
1817 /* Match a DEALLOCATE statement. */
1820 gfc_match_deallocate (void)
1822 gfc_alloc
*head
, *tail
;
1829 if (gfc_match_char ('(') != MATCH_YES
)
1835 head
= tail
= gfc_get_alloc ();
1838 tail
->next
= gfc_get_alloc ();
1842 m
= gfc_match_variable (&tail
->expr
, 0);
1843 if (m
== MATCH_ERROR
)
1849 && gfc_impure_variable (tail
->expr
->symtree
->n
.sym
))
1852 ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1857 if (gfc_match_char (',') != MATCH_YES
)
1860 m
= gfc_match (" stat = %v", &stat
);
1861 if (m
== MATCH_ERROR
)
1867 if (stat
!= NULL
&& stat
->symtree
->n
.sym
->attr
.intent
== INTENT_IN
)
1869 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C cannot be "
1870 "INTENT(IN)", stat
->symtree
->n
.sym
->name
);
1874 if (gfc_match (" )%t") != MATCH_YES
)
1877 new_st
.op
= EXEC_DEALLOCATE
;
1879 new_st
.ext
.alloc_list
= head
;
1884 gfc_syntax_error (ST_DEALLOCATE
);
1887 gfc_free_expr (stat
);
1888 gfc_free_alloc_list (head
);
1893 /* Match a RETURN statement. */
1896 gfc_match_return (void)
1902 if (gfc_match_eos () == MATCH_YES
)
1905 if (gfc_find_state (COMP_SUBROUTINE
) == FAILURE
)
1907 gfc_error ("Alternate RETURN statement at %C is only allowed within "
1912 m
= gfc_match ("% %e%t", &e
);
1915 if (m
== MATCH_ERROR
)
1918 gfc_syntax_error (ST_RETURN
);
1925 new_st
.op
= EXEC_RETURN
;
1932 /* Match a CALL statement. The tricky part here are possible
1933 alternate return specifiers. We handle these by having all
1934 "subroutines" actually return an integer via a register that gives
1935 the return number. If the call specifies alternate returns, we
1936 generate code for a SELECT statement whose case clauses contain
1937 GOTOs to the various labels. */
1940 gfc_match_call (void)
1942 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1943 gfc_actual_arglist
*a
, *arglist
;
1953 m
= gfc_match ("% %n", name
);
1959 if (gfc_get_ha_sym_tree (name
, &st
))
1963 gfc_set_sym_referenced (sym
);
1965 if (!sym
->attr
.generic
1966 && !sym
->attr
.subroutine
1967 && gfc_add_subroutine (&sym
->attr
, NULL
) == FAILURE
)
1970 if (gfc_match_eos () != MATCH_YES
)
1972 m
= gfc_match_actual_arglist (1, &arglist
);
1975 if (m
== MATCH_ERROR
)
1978 if (gfc_match_eos () != MATCH_YES
)
1982 /* If any alternate return labels were found, construct a SELECT
1983 statement that will jump to the right place. */
1986 for (a
= arglist
; a
; a
= a
->next
)
1987 if (a
->expr
== NULL
)
1992 gfc_symtree
*select_st
;
1993 gfc_symbol
*select_sym
;
1994 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1996 new_st
.next
= c
= gfc_get_code ();
1997 c
->op
= EXEC_SELECT
;
1998 sprintf (name
, "_result_%s",sym
->name
);
1999 gfc_get_ha_sym_tree (name
, &select_st
); /* Can't fail */
2001 select_sym
= select_st
->n
.sym
;
2002 select_sym
->ts
.type
= BT_INTEGER
;
2003 select_sym
->ts
.kind
= gfc_default_integer_kind ();
2004 gfc_set_sym_referenced (select_sym
);
2005 c
->expr
= gfc_get_expr ();
2006 c
->expr
->expr_type
= EXPR_VARIABLE
;
2007 c
->expr
->symtree
= select_st
;
2008 c
->expr
->ts
= select_sym
->ts
;
2009 c
->expr
->where
= gfc_current_locus
;
2012 for (a
= arglist
; a
; a
= a
->next
)
2014 if (a
->expr
!= NULL
)
2017 if (gfc_reference_st_label (a
->label
, ST_LABEL_TARGET
) == FAILURE
)
2022 c
->block
= gfc_get_code ();
2024 c
->op
= EXEC_SELECT
;
2026 new_case
= gfc_get_case ();
2027 new_case
->high
= new_case
->low
= gfc_int_expr (i
);
2028 c
->ext
.case_list
= new_case
;
2030 c
->next
= gfc_get_code ();
2031 c
->next
->op
= EXEC_GOTO
;
2032 c
->next
->label
= a
->label
;
2036 new_st
.op
= EXEC_CALL
;
2037 new_st
.symtree
= st
;
2038 new_st
.ext
.actual
= arglist
;
2043 gfc_syntax_error (ST_CALL
);
2046 gfc_free_actual_arglist (arglist
);
2051 /* Given a name, return a pointer to the common head structure,
2052 creating it if it does not exist.
2053 TODO: Add to global symbol tree. */
2056 gfc_get_common (char *name
)
2060 st
= gfc_find_symtree (gfc_current_ns
->common_root
, name
);
2062 st
= gfc_new_symtree (&gfc_current_ns
->common_root
, name
);
2064 if (st
->n
.common
== NULL
)
2066 st
->n
.common
= gfc_get_common_head ();
2067 st
->n
.common
->where
= gfc_current_locus
;
2070 return st
->n
.common
;
2074 /* Match a common block name. */
2077 match_common_name (char *name
)
2081 if (gfc_match_char ('/') == MATCH_NO
)
2087 if (gfc_match_char ('/') == MATCH_YES
)
2093 m
= gfc_match_name (name
);
2095 if (m
== MATCH_ERROR
)
2097 if (m
== MATCH_YES
&& gfc_match_char ('/') == MATCH_YES
)
2100 gfc_error ("Syntax error in common block name at %C");
2105 /* Match a COMMON statement. */
2108 gfc_match_common (void)
2110 gfc_symbol
*sym
, **head
, *tail
, *old_blank_common
;
2111 char name
[GFC_MAX_SYMBOL_LEN
+1];
2116 old_blank_common
= gfc_current_ns
->blank_common
.head
;
2117 if (old_blank_common
)
2119 while (old_blank_common
->common_next
)
2120 old_blank_common
= old_blank_common
->common_next
;
2125 if (gfc_match_eos () == MATCH_YES
)
2130 m
= match_common_name (name
);
2131 if (m
== MATCH_ERROR
)
2134 if (name
[0] == '\0')
2136 t
= &gfc_current_ns
->blank_common
;
2137 if (t
->head
== NULL
)
2138 t
->where
= gfc_current_locus
;
2143 t
= gfc_get_common (name
);
2148 gfc_error ("COMMON block '%s' at %C has already "
2149 "been USE-associated", name
);
2159 while (tail
->common_next
)
2160 tail
= tail
->common_next
;
2163 /* Grab the list of symbols. */
2164 if (gfc_match_eos () == MATCH_YES
)
2169 m
= gfc_match_symbol (&sym
, 0);
2170 if (m
== MATCH_ERROR
)
2175 if (sym
->attr
.in_common
)
2177 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2182 if (gfc_add_in_common (&sym
->attr
, NULL
) == FAILURE
)
2185 if (sym
->value
!= NULL
2186 && (name
[0] == '\0' || !sym
->attr
.data
))
2188 if (name
[0] == '\0')
2189 gfc_error ("Previously initialized symbol '%s' in "
2190 "blank COMMON block at %C", sym
->name
);
2192 gfc_error ("Previously initialized symbol '%s' in "
2193 "COMMON block '%s' at %C", sym
->name
, name
);
2197 if (gfc_add_in_common (&sym
->attr
, NULL
) == FAILURE
)
2200 /* Derived type names must have the SEQUENCE attribute. */
2201 if (sym
->ts
.type
== BT_DERIVED
&& !sym
->ts
.derived
->attr
.sequence
)
2204 ("Derived type variable in COMMON at %C does not have the "
2205 "SEQUENCE attribute");
2210 tail
->common_next
= sym
;
2216 /* Deal with an optional array specification after the
2218 m
= gfc_match_array_spec (&as
);
2219 if (m
== MATCH_ERROR
)
2224 if (as
->type
!= AS_EXPLICIT
)
2227 ("Array specification for symbol '%s' in COMMON at %C "
2228 "must be explicit", sym
->name
);
2232 if (gfc_add_dimension (&sym
->attr
, NULL
) == FAILURE
)
2235 if (sym
->attr
.pointer
)
2238 ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2247 if (gfc_match_eos () == MATCH_YES
)
2249 if (gfc_peek_char () == '/')
2251 if (gfc_match_char (',') != MATCH_YES
)
2253 if (gfc_peek_char () == '/')
2262 gfc_syntax_error (ST_COMMON
);
2265 if (old_blank_common
)
2266 old_blank_common
->common_next
= NULL
;
2268 gfc_current_ns
->blank_common
.head
= NULL
;
2269 gfc_free_array_spec (as
);
2274 /* Match a BLOCK DATA program unit. */
2277 gfc_match_block_data (void)
2279 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2283 if (gfc_match_eos () == MATCH_YES
)
2285 gfc_new_block
= NULL
;
2289 m
= gfc_match (" %n%t", name
);
2293 if (gfc_get_symbol (name
, NULL
, &sym
))
2296 if (gfc_add_flavor (&sym
->attr
, FL_BLOCK_DATA
, NULL
) == FAILURE
)
2299 gfc_new_block
= sym
;
2305 /* Free a namelist structure. */
2308 gfc_free_namelist (gfc_namelist
* name
)
2312 for (; name
; name
= n
)
2320 /* Match a NAMELIST statement. */
2323 gfc_match_namelist (void)
2325 gfc_symbol
*group_name
, *sym
;
2329 m
= gfc_match (" / %s /", &group_name
);
2332 if (m
== MATCH_ERROR
)
2337 if (group_name
->ts
.type
!= BT_UNKNOWN
)
2340 ("Namelist group name '%s' at %C already has a basic type "
2341 "of %s", group_name
->name
, gfc_typename (&group_name
->ts
));
2345 if (group_name
->attr
.flavor
!= FL_NAMELIST
2346 && gfc_add_flavor (&group_name
->attr
, FL_NAMELIST
, NULL
) == FAILURE
)
2351 m
= gfc_match_symbol (&sym
, 1);
2354 if (m
== MATCH_ERROR
)
2357 if (sym
->attr
.in_namelist
== 0
2358 && gfc_add_in_namelist (&sym
->attr
, NULL
) == FAILURE
)
2361 /* TODO: worry about PRIVATE members of a PUBLIC namelist
2364 nl
= gfc_get_namelist ();
2367 if (group_name
->namelist
== NULL
)
2368 group_name
->namelist
= group_name
->namelist_tail
= nl
;
2371 group_name
->namelist_tail
->next
= nl
;
2372 group_name
->namelist_tail
= nl
;
2375 if (gfc_match_eos () == MATCH_YES
)
2378 m
= gfc_match_char (',');
2380 if (gfc_match_char ('/') == MATCH_YES
)
2382 m2
= gfc_match (" %s /", &group_name
);
2383 if (m2
== MATCH_YES
)
2385 if (m2
== MATCH_ERROR
)
2399 gfc_syntax_error (ST_NAMELIST
);
2406 /* Match a MODULE statement. */
2409 gfc_match_module (void)
2413 m
= gfc_match (" %s%t", &gfc_new_block
);
2417 if (gfc_add_flavor (&gfc_new_block
->attr
, FL_MODULE
, NULL
) == FAILURE
)
2424 /* Free equivalence sets and lists. Recursively is the easiest way to
2428 gfc_free_equiv (gfc_equiv
* eq
)
2434 gfc_free_equiv (eq
->eq
);
2435 gfc_free_equiv (eq
->next
);
2437 gfc_free_expr (eq
->expr
);
2442 /* Match an EQUIVALENCE statement. */
2445 gfc_match_equivalence (void)
2447 gfc_equiv
*eq
, *set
, *tail
;
2455 eq
= gfc_get_equiv ();
2459 eq
->next
= gfc_current_ns
->equiv
;
2460 gfc_current_ns
->equiv
= eq
;
2462 if (gfc_match_char ('(') != MATCH_YES
)
2469 m
= gfc_match_variable (&set
->expr
, 1);
2470 if (m
== MATCH_ERROR
)
2475 for (ref
= set
->expr
->ref
; ref
; ref
= ref
->next
)
2476 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.type
== AR_SECTION
)
2479 ("Array reference in EQUIVALENCE at %C cannot be an "
2484 if (gfc_match_char (')') == MATCH_YES
)
2486 if (gfc_match_char (',') != MATCH_YES
)
2489 set
->eq
= gfc_get_equiv ();
2493 if (gfc_match_eos () == MATCH_YES
)
2495 if (gfc_match_char (',') != MATCH_YES
)
2502 gfc_syntax_error (ST_EQUIVALENCE
);
2508 gfc_free_equiv (gfc_current_ns
->equiv
);
2509 gfc_current_ns
->equiv
= eq
;
2515 /* Match a statement function declaration. It is so easy to match
2516 non-statement function statements with a MATCH_ERROR as opposed to
2517 MATCH_NO that we suppress error message in most cases. */
2520 gfc_match_st_function (void)
2522 gfc_error_buf old_error
;
2527 m
= gfc_match_symbol (&sym
, 0);
2531 gfc_push_error (&old_error
);
2533 if (gfc_add_procedure (&sym
->attr
, PROC_ST_FUNCTION
, NULL
) == FAILURE
)
2536 if (gfc_match_formal_arglist (sym
, 1, 0) != MATCH_YES
)
2539 m
= gfc_match (" = %e%t", &expr
);
2542 if (m
== MATCH_ERROR
)
2550 gfc_pop_error (&old_error
);
2555 /********************* DATA statement subroutines *********************/
2557 /* Free a gfc_data_variable structure and everything beneath it. */
2560 free_variable (gfc_data_variable
* p
)
2562 gfc_data_variable
*q
;
2567 gfc_free_expr (p
->expr
);
2568 gfc_free_iterator (&p
->iter
, 0);
2569 free_variable (p
->list
);
2576 /* Free a gfc_data_value structure and everything beneath it. */
2579 free_value (gfc_data_value
* p
)
2586 gfc_free_expr (p
->expr
);
2592 /* Free a list of gfc_data structures. */
2595 gfc_free_data (gfc_data
* p
)
2603 free_variable (p
->var
);
2604 free_value (p
->value
);
2611 static match
var_element (gfc_data_variable
*);
2613 /* Match a list of variables terminated by an iterator and a right
2617 var_list (gfc_data_variable
* parent
)
2619 gfc_data_variable
*tail
, var
;
2622 m
= var_element (&var
);
2623 if (m
== MATCH_ERROR
)
2628 tail
= gfc_get_data_variable ();
2631 parent
->list
= tail
;
2635 if (gfc_match_char (',') != MATCH_YES
)
2638 m
= gfc_match_iterator (&parent
->iter
, 1);
2641 if (m
== MATCH_ERROR
)
2644 m
= var_element (&var
);
2645 if (m
== MATCH_ERROR
)
2650 tail
->next
= gfc_get_data_variable ();
2656 if (gfc_match_char (')') != MATCH_YES
)
2661 gfc_syntax_error (ST_DATA
);
2666 /* Match a single element in a data variable list, which can be a
2667 variable-iterator list. */
2670 var_element (gfc_data_variable
* new)
2675 memset (new, '\0', sizeof (gfc_data_variable
));
2677 if (gfc_match_char ('(') == MATCH_YES
)
2678 return var_list (new);
2680 m
= gfc_match_variable (&new->expr
, 0);
2684 sym
= new->expr
->symtree
->n
.sym
;
2686 if(sym
->value
!= NULL
)
2688 gfc_error ("Variable '%s' at %C already has an initialization",
2693 #if 0 // TODO: Find out where to move this message
2694 if (sym
->attr
.in_common
)
2695 /* See if sym is in the blank common block. */
2696 for (t
= &sym
->ns
->blank_common
; t
; t
= t
->common_next
)
2699 gfc_error ("DATA statement at %C may not initialize variable "
2700 "'%s' from blank COMMON", sym
->name
);
2705 if (gfc_add_data (&sym
->attr
, &new->expr
->where
) == FAILURE
)
2712 /* Match the top-level list of data variables. */
2715 top_var_list (gfc_data
* d
)
2717 gfc_data_variable var
, *tail
, *new;
2724 m
= var_element (&var
);
2727 if (m
== MATCH_ERROR
)
2730 new = gfc_get_data_variable ();
2740 if (gfc_match_char ('/') == MATCH_YES
)
2742 if (gfc_match_char (',') != MATCH_YES
)
2749 gfc_syntax_error (ST_DATA
);
2755 match_data_constant (gfc_expr
** result
)
2757 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2762 m
= gfc_match_literal_constant (&expr
, 1);
2769 if (m
== MATCH_ERROR
)
2772 m
= gfc_match_null (result
);
2776 m
= gfc_match_name (name
);
2780 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
2784 || (sym
->attr
.flavor
!= FL_PARAMETER
&& sym
->attr
.flavor
!= FL_DERIVED
))
2786 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
2790 else if (sym
->attr
.flavor
== FL_DERIVED
)
2791 return gfc_match_structure_constructor (sym
, result
);
2793 *result
= gfc_copy_expr (sym
->value
);
2798 /* Match a list of values in a DATA statement. The leading '/' has
2799 already been seen at this point. */
2802 top_val_list (gfc_data
* data
)
2804 gfc_data_value
*new, *tail
;
2813 m
= match_data_constant (&expr
);
2816 if (m
== MATCH_ERROR
)
2819 new = gfc_get_data_value ();
2828 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
2835 msg
= gfc_extract_int (expr
, &tail
->repeat
);
2836 gfc_free_expr (expr
);
2843 m
= match_data_constant (&tail
->expr
);
2846 if (m
== MATCH_ERROR
)
2850 if (gfc_match_char ('/') == MATCH_YES
)
2852 if (gfc_match_char (',') == MATCH_NO
)
2859 gfc_syntax_error (ST_DATA
);
2864 /* Match a DATA statement. */
2867 gfc_match_data (void)
2874 new = gfc_get_data ();
2875 new->where
= gfc_current_locus
;
2877 m
= top_var_list (new);
2881 m
= top_val_list (new);
2885 new->next
= gfc_current_ns
->data
;
2886 gfc_current_ns
->data
= new;
2888 if (gfc_match_eos () == MATCH_YES
)
2891 gfc_match_char (','); /* Optional comma */
2894 if (gfc_pure (NULL
))
2896 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
2903 gfc_free_data (new);
2908 /***************** SELECT CASE subroutines ******************/
2910 /* Free a single case structure. */
2913 free_case (gfc_case
* p
)
2915 if (p
->low
== p
->high
)
2917 gfc_free_expr (p
->low
);
2918 gfc_free_expr (p
->high
);
2923 /* Free a list of case structures. */
2926 gfc_free_case_list (gfc_case
* p
)
2938 /* Match a single case selector. */
2941 match_case_selector (gfc_case
** cp
)
2946 c
= gfc_get_case ();
2947 c
->where
= gfc_current_locus
;
2949 if (gfc_match_char (':') == MATCH_YES
)
2951 m
= gfc_match_init_expr (&c
->high
);
2954 if (m
== MATCH_ERROR
)
2960 m
= gfc_match_init_expr (&c
->low
);
2961 if (m
== MATCH_ERROR
)
2966 /* If we're not looking at a ':' now, make a range out of a single
2967 target. Else get the upper bound for the case range. */
2968 if (gfc_match_char (':') != MATCH_YES
)
2972 m
= gfc_match_init_expr (&c
->high
);
2973 if (m
== MATCH_ERROR
)
2975 /* MATCH_NO is fine. It's OK if nothing is there! */
2983 gfc_error ("Expected initialization expression in CASE at %C");
2991 /* Match the end of a case statement. */
2994 match_case_eos (void)
2996 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2999 if (gfc_match_eos () == MATCH_YES
)
3002 gfc_gobble_whitespace ();
3004 m
= gfc_match_name (name
);
3008 if (strcmp (name
, gfc_current_block ()->name
) != 0)
3010 gfc_error ("Expected case name of '%s' at %C",
3011 gfc_current_block ()->name
);
3015 return gfc_match_eos ();
3019 /* Match a SELECT statement. */
3022 gfc_match_select (void)
3027 m
= gfc_match_label ();
3028 if (m
== MATCH_ERROR
)
3031 m
= gfc_match (" select case ( %e )%t", &expr
);
3035 new_st
.op
= EXEC_SELECT
;
3042 /* Match a CASE statement. */
3045 gfc_match_case (void)
3047 gfc_case
*c
, *head
, *tail
;
3052 if (gfc_current_state () != COMP_SELECT
)
3054 gfc_error ("Unexpected CASE statement at %C");
3058 if (gfc_match ("% default") == MATCH_YES
)
3060 m
= match_case_eos ();
3063 if (m
== MATCH_ERROR
)
3066 new_st
.op
= EXEC_SELECT
;
3067 c
= gfc_get_case ();
3068 c
->where
= gfc_current_locus
;
3069 new_st
.ext
.case_list
= c
;
3073 if (gfc_match_char ('(') != MATCH_YES
)
3078 if (match_case_selector (&c
) == MATCH_ERROR
)
3088 if (gfc_match_char (')') == MATCH_YES
)
3090 if (gfc_match_char (',') != MATCH_YES
)
3094 m
= match_case_eos ();
3097 if (m
== MATCH_ERROR
)
3100 new_st
.op
= EXEC_SELECT
;
3101 new_st
.ext
.case_list
= head
;
3106 gfc_error ("Syntax error in CASE-specification at %C");
3109 gfc_free_case_list (head
); /* new_st is cleaned up in parse.c. */
3113 /********************* WHERE subroutines ********************/
3115 /* Match a WHERE statement. */
3118 gfc_match_where (gfc_statement
* st
)
3124 m0
= gfc_match_label ();
3125 if (m0
== MATCH_ERROR
)
3128 m
= gfc_match (" where ( %e )", &expr
);
3132 if (gfc_match_eos () == MATCH_YES
)
3134 *st
= ST_WHERE_BLOCK
;
3136 new_st
.op
= EXEC_WHERE
;
3141 m
= gfc_match_assignment ();
3143 gfc_syntax_error (ST_WHERE
);
3147 gfc_free_expr (expr
);
3151 /* We've got a simple WHERE statement. */
3153 c
= gfc_get_code ();
3157 c
->next
= gfc_get_code ();
3160 gfc_clear_new_st ();
3162 new_st
.op
= EXEC_WHERE
;
3169 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3170 new_st if successful. */
3173 gfc_match_elsewhere (void)
3175 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3179 if (gfc_current_state () != COMP_WHERE
)
3181 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3187 if (gfc_match_char ('(') == MATCH_YES
)
3189 m
= gfc_match_expr (&expr
);
3192 if (m
== MATCH_ERROR
)
3195 if (gfc_match_char (')') != MATCH_YES
)
3199 if (gfc_match_eos () != MATCH_YES
)
3200 { /* Better be a name at this point */
3201 m
= gfc_match_name (name
);
3204 if (m
== MATCH_ERROR
)
3207 if (gfc_match_eos () != MATCH_YES
)
3210 if (strcmp (name
, gfc_current_block ()->name
) != 0)
3212 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3213 name
, gfc_current_block ()->name
);
3218 new_st
.op
= EXEC_WHERE
;
3223 gfc_syntax_error (ST_ELSEWHERE
);
3226 gfc_free_expr (expr
);
3231 /******************** FORALL subroutines ********************/
3233 /* Free a list of FORALL iterators. */
3236 gfc_free_forall_iterator (gfc_forall_iterator
* iter
)
3238 gfc_forall_iterator
*next
;
3244 gfc_free_expr (iter
->var
);
3245 gfc_free_expr (iter
->start
);
3246 gfc_free_expr (iter
->end
);
3247 gfc_free_expr (iter
->stride
);
3255 /* Match an iterator as part of a FORALL statement. The format is:
3257 <var> = <start>:<end>[:<stride>][, <scalar mask>] */
3260 match_forall_iterator (gfc_forall_iterator
** result
)
3262 gfc_forall_iterator
*iter
;
3266 where
= gfc_current_locus
;
3267 iter
= gfc_getmem (sizeof (gfc_forall_iterator
));
3269 m
= gfc_match_variable (&iter
->var
, 0);
3273 if (gfc_match_char ('=') != MATCH_YES
)
3279 m
= gfc_match_expr (&iter
->start
);
3282 if (m
== MATCH_ERROR
)
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 a FORALL statement. */
3322 gfc_match_forall (gfc_statement
* st
)
3324 gfc_forall_iterator
*head
, *tail
, *new;
3333 m0
= gfc_match_label ();
3334 if (m0
== MATCH_ERROR
)
3337 m
= gfc_match (" forall (");
3341 m
= match_forall_iterator (&new);
3342 if (m
== MATCH_ERROR
)
3351 if (gfc_match_char (',') != MATCH_YES
)
3354 m
= match_forall_iterator (&new);
3355 if (m
== MATCH_ERROR
)
3364 /* Have to have a mask expression. */
3365 m
= gfc_match_expr (&mask
);
3368 if (m
== MATCH_ERROR
)
3374 if (gfc_match_char (')') == MATCH_NO
)
3377 if (gfc_match_eos () == MATCH_YES
)
3379 *st
= ST_FORALL_BLOCK
;
3381 new_st
.op
= EXEC_FORALL
;
3383 new_st
.ext
.forall_iterator
= head
;
3388 m
= gfc_match_assignment ();
3389 if (m
== MATCH_ERROR
)
3393 m
= gfc_match_pointer_assignment ();
3394 if (m
== MATCH_ERROR
)
3400 c
= gfc_get_code ();
3403 if (gfc_match_eos () != MATCH_YES
)
3406 gfc_clear_new_st ();
3407 new_st
.op
= EXEC_FORALL
;
3409 new_st
.ext
.forall_iterator
= head
;
3410 new_st
.block
= gfc_get_code ();
3412 new_st
.block
->op
= EXEC_FORALL
;
3413 new_st
.block
->next
= c
;
3419 gfc_syntax_error (ST_FORALL
);
3422 gfc_free_forall_iterator (head
);
3423 gfc_free_expr (mask
);
3424 gfc_free_statements (c
);