1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
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
30 /* For matching and debugging purposes. Order matters here! The
31 unary operators /must/ precede the binary plus and minus, or
32 the expression parser breaks. */
34 mstring intrinsic_operators[] = {
35 minit ("+", INTRINSIC_UPLUS),
36 minit ("-", INTRINSIC_UMINUS),
37 minit ("+", INTRINSIC_PLUS),
38 minit ("-", INTRINSIC_MINUS),
39 minit ("**", INTRINSIC_POWER),
40 minit ("//", INTRINSIC_CONCAT),
41 minit ("*", INTRINSIC_TIMES),
42 minit ("/", INTRINSIC_DIVIDE),
43 minit (".and.", INTRINSIC_AND),
44 minit (".or.", INTRINSIC_OR),
45 minit (".eqv.", INTRINSIC_EQV),
46 minit (".neqv.", INTRINSIC_NEQV),
47 minit (".eq.", INTRINSIC_EQ),
48 minit ("==", INTRINSIC_EQ),
49 minit (".ne.", INTRINSIC_NE),
50 minit ("/=", INTRINSIC_NE),
51 minit (".ge.", INTRINSIC_GE),
52 minit (">=", INTRINSIC_GE),
53 minit (".le.", INTRINSIC_LE),
54 minit ("<=", INTRINSIC_LE),
55 minit (".lt.", INTRINSIC_LT),
56 minit ("<", INTRINSIC_LT),
57 minit (".gt.", INTRINSIC_GT),
58 minit (">", INTRINSIC_GT),
59 minit (".not.", INTRINSIC_NOT),
60 minit ("parens", INTRINSIC_PARENTHESES),
61 minit (NULL, INTRINSIC_NONE)
65 /******************** Generic matching subroutines ************************/
67 /* See if the next character is a special character that has
68 escaped by a \ via the -fbackslash option. */
71 gfc_match_special_char (int *c)
78 switch (gfc_next_char_literal (1))
108 /* Unknown backslash codes are simply not expanded. */
117 /* In free form, match at least one space. Always matches in fixed
121 gfc_match_space (void)
126 if (gfc_current_form == FORM_FIXED)
129 old_loc = gfc_current_locus;
131 c = gfc_next_char ();
132 if (!gfc_is_whitespace (c))
134 gfc_current_locus = old_loc;
138 gfc_gobble_whitespace ();
144 /* Match an end of statement. End of statement is optional
145 whitespace, followed by a ';' or '\n' or comment '!'. If a
146 semicolon is found, we continue to eat whitespace and semicolons. */
158 old_loc = gfc_current_locus;
159 gfc_gobble_whitespace ();
161 c = gfc_next_char ();
167 c = gfc_next_char ();
184 gfc_current_locus = old_loc;
185 return (flag) ? MATCH_YES : MATCH_NO;
189 /* Match a literal integer on the input, setting the value on
190 MATCH_YES. Literal ints occur in kind-parameters as well as
191 old-style character length specifications. If cnt is non-NULL it
192 will be set to the number of digits. */
195 gfc_match_small_literal_int (int *value, int *cnt)
201 old_loc = gfc_current_locus;
203 gfc_gobble_whitespace ();
204 c = gfc_next_char ();
210 gfc_current_locus = old_loc;
219 old_loc = gfc_current_locus;
220 c = gfc_next_char ();
225 i = 10 * i + c - '0';
230 gfc_error ("Integer too large at %C");
235 gfc_current_locus = old_loc;
244 /* Match a small, constant integer expression, like in a kind
245 statement. On MATCH_YES, 'value' is set. */
248 gfc_match_small_int (int *value)
255 m = gfc_match_expr (&expr);
259 p = gfc_extract_int (expr, &i);
260 gfc_free_expr (expr);
273 /* This function is the same as the gfc_match_small_int, except that
274 we're keeping the pointer to the expr. This function could just be
275 removed and the previously mentioned one modified, though all calls
276 to it would have to be modified then (and there were a number of
277 them). Return MATCH_ERROR if fail to extract the int; otherwise,
278 return the result of gfc_match_expr(). The expr (if any) that was
279 matched is returned in the parameter expr. */
282 gfc_match_small_int_expr (int *value, gfc_expr **expr)
288 m = gfc_match_expr (expr);
292 p = gfc_extract_int (*expr, &i);
305 /* Matches a statement label. Uses gfc_match_small_literal_int() to
306 do most of the work. */
309 gfc_match_st_label (gfc_st_label **label)
315 old_loc = gfc_current_locus;
317 m = gfc_match_small_literal_int (&i, &cnt);
323 gfc_error ("Too many digits in statement label at %C");
329 gfc_error ("Statement label at %C is zero");
333 *label = gfc_get_st_label (i);
338 gfc_current_locus = old_loc;
343 /* Match and validate a label associated with a named IF, DO or SELECT
344 statement. If the symbol does not have the label attribute, we add
345 it. We also make sure the symbol does not refer to another
346 (active) block. A matched label is pointed to by gfc_new_block. */
349 gfc_match_label (void)
351 char name[GFC_MAX_SYMBOL_LEN + 1];
354 gfc_new_block = NULL;
356 m = gfc_match (" %n :", name);
360 if (gfc_get_symbol (name, NULL, &gfc_new_block))
362 gfc_error ("Label name '%s' at %C is ambiguous", name);
366 if (gfc_new_block->attr.flavor == FL_LABEL)
368 gfc_error ("Duplicate construct label '%s' at %C", name);
372 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
373 gfc_new_block->name, NULL) == FAILURE)
380 /* Try and match the input against an array of possibilities. If one
381 potential matching string is a substring of another, the longest
382 match takes precedence. Spaces in the target strings are optional
383 spaces that do not necessarily have to be found in the input
384 stream. In fixed mode, spaces never appear. If whitespace is
385 matched, it matches unlimited whitespace in the input. For this
386 reason, the 'mp' member of the mstring structure is used to track
387 the progress of each potential match.
389 If there is no match we return the tag associated with the
390 terminating NULL mstring structure and leave the locus pointer
391 where it started. If there is a match we return the tag member of
392 the matched mstring and leave the locus pointer after the matched
395 A '%' character is a mandatory space. */
398 gfc_match_strings (mstring *a)
400 mstring *p, *best_match;
401 int no_match, c, possibles;
406 for (p = a; p->string != NULL; p++)
415 match_loc = gfc_current_locus;
417 gfc_gobble_whitespace ();
419 while (possibles > 0)
421 c = gfc_next_char ();
423 /* Apply the next character to the current possibilities. */
424 for (p = a; p->string != NULL; p++)
431 /* Space matches 1+ whitespace(s). */
432 if ((gfc_current_form == FORM_FREE) && gfc_is_whitespace (c))
450 match_loc = gfc_current_locus;
458 gfc_current_locus = match_loc;
460 return (best_match == NULL) ? no_match : best_match->tag;
464 /* See if the current input looks like a name of some sort. Modifies
465 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
466 Note that options.c restricts max_identifier_length to not more
467 than GFC_MAX_SYMBOL_LEN. */
470 gfc_match_name (char *buffer)
475 old_loc = gfc_current_locus;
476 gfc_gobble_whitespace ();
478 c = gfc_next_char ();
479 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
481 if (gfc_error_flag_test() == 0)
482 gfc_error ("Invalid character in name at %C");
483 gfc_current_locus = old_loc;
493 if (i > gfc_option.max_identifier_length)
495 gfc_error ("Name at %C is too long");
499 old_loc = gfc_current_locus;
500 c = gfc_next_char ();
502 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
505 gfc_current_locus = old_loc;
511 /* Match a valid name for C, which is almost the same as for Fortran,
512 except that you can start with an underscore, etc.. It could have
513 been done by modifying the gfc_match_name, but this way other
514 things C allows can be added, such as no limits on the length.
515 Right now, the length is limited to the same thing as Fortran..
516 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
517 input characters from being automatically lower cased, since C is
518 case sensitive. The parameter, buffer, is used to return the name
519 that is matched. Return MATCH_ERROR if the name is too long
520 (though this is a self-imposed limit), MATCH_NO if what we're
521 seeing isn't a name, and MATCH_YES if we successfully match a C
525 gfc_match_name_C (char *buffer)
531 old_loc = gfc_current_locus;
532 gfc_gobble_whitespace ();
534 /* Get the next char (first possible char of name) and see if
535 it's valid for C (either a letter or an underscore). */
536 c = gfc_next_char_literal (1);
538 /* If the user put nothing expect spaces between the quotes, it is valid
539 and simply means there is no name= specifier and the name is the fortran
540 symbol name, all lowercase. */
541 if (c == '"' || c == '\'')
544 gfc_current_locus = old_loc;
548 if (!ISALPHA (c) && c != '_')
550 gfc_error ("Invalid C name in NAME= specifier at %C");
554 /* Continue to read valid variable name characters. */
559 /* C does not define a maximum length of variable names, to my
560 knowledge, but the compiler typically places a limit on them.
561 For now, i'll use the same as the fortran limit for simplicity,
562 but this may need to be changed to a dynamic buffer that can
563 be realloc'ed here if necessary, or more likely, a larger
565 if (i > gfc_option.max_identifier_length)
567 gfc_error ("Name at %C is too long");
571 old_loc = gfc_current_locus;
573 /* Get next char; param means we're in a string. */
574 c = gfc_next_char_literal (1);
575 } while (ISALNUM (c) || c == '_');
578 gfc_current_locus = old_loc;
580 /* See if we stopped because of whitespace. */
583 gfc_gobble_whitespace ();
584 c = gfc_peek_char ();
585 if (c != '"' && c != '\'')
587 gfc_error ("Embedded space in NAME= specifier at %C");
592 /* If we stopped because we had an invalid character for a C name, report
593 that to the user by returning MATCH_NO. */
594 if (c != '"' && c != '\'')
596 gfc_error ("Invalid C name in NAME= specifier at %C");
604 /* Match a symbol on the input. Modifies the pointer to the symbol
605 pointer if successful. */
608 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
610 char buffer[GFC_MAX_SYMBOL_LEN + 1];
613 m = gfc_match_name (buffer);
618 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
619 ? MATCH_ERROR : MATCH_YES;
621 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
629 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
634 m = gfc_match_sym_tree (&st, host_assoc);
639 *matched_symbol = st->n.sym;
641 *matched_symbol = NULL;
644 *matched_symbol = NULL;
649 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
650 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
654 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
658 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
660 if (op == INTRINSIC_NONE)
668 /* Match a loop control phrase:
670 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
672 If the final integer expression is not present, a constant unity
673 expression is returned. We don't return MATCH_ERROR until after
674 the equals sign is seen. */
677 gfc_match_iterator (gfc_iterator *iter, int init_flag)
679 char name[GFC_MAX_SYMBOL_LEN + 1];
680 gfc_expr *var, *e1, *e2, *e3;
684 /* Match the start of an iterator without affecting the symbol table. */
686 start = gfc_current_locus;
687 m = gfc_match (" %n =", name);
688 gfc_current_locus = start;
693 m = gfc_match_variable (&var, 0);
697 gfc_match_char ('=');
701 if (var->ref != NULL)
703 gfc_error ("Loop variable at %C cannot be a sub-component");
707 if (var->symtree->n.sym->attr.intent == INTENT_IN)
709 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
710 var->symtree->n.sym->name);
714 var->symtree->n.sym->attr.implied_index = 1;
716 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
719 if (m == MATCH_ERROR)
722 if (gfc_match_char (',') != MATCH_YES)
725 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
728 if (m == MATCH_ERROR)
731 if (gfc_match_char (',') != MATCH_YES)
733 e3 = gfc_int_expr (1);
737 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
738 if (m == MATCH_ERROR)
742 gfc_error ("Expected a step value in iterator at %C");
754 gfc_error ("Syntax error in iterator at %C");
765 /* Tries to match the next non-whitespace character on the input.
766 This subroutine does not return MATCH_ERROR. */
769 gfc_match_char (char c)
773 where = gfc_current_locus;
774 gfc_gobble_whitespace ();
776 if (gfc_next_char () == c)
779 gfc_current_locus = where;
784 /* General purpose matching subroutine. The target string is a
785 scanf-like format string in which spaces correspond to arbitrary
786 whitespace (including no whitespace), characters correspond to
787 themselves. The %-codes are:
789 %% Literal percent sign
790 %e Expression, pointer to a pointer is set
791 %s Symbol, pointer to the symbol is set
792 %n Name, character buffer is set to name
793 %t Matches end of statement.
794 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
795 %l Matches a statement label
796 %v Matches a variable expression (an lvalue)
797 % Matches a required space (in free form) and optional spaces. */
800 gfc_match (const char *target, ...)
802 gfc_st_label **label;
811 old_loc = gfc_current_locus;
812 va_start (argp, target);
822 gfc_gobble_whitespace ();
833 vp = va_arg (argp, void **);
834 n = gfc_match_expr ((gfc_expr **) vp);
845 vp = va_arg (argp, void **);
846 n = gfc_match_variable ((gfc_expr **) vp, 0);
857 vp = va_arg (argp, void **);
858 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
869 np = va_arg (argp, char *);
870 n = gfc_match_name (np);
881 label = va_arg (argp, gfc_st_label **);
882 n = gfc_match_st_label (label);
893 ip = va_arg (argp, int *);
894 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
905 if (gfc_match_eos () != MATCH_YES)
913 if (gfc_match_space () == MATCH_YES)
919 break; /* Fall through to character matcher. */
922 gfc_internal_error ("gfc_match(): Bad match code %c", c);
926 if (c == gfc_next_char ())
936 /* Clean up after a failed match. */
937 gfc_current_locus = old_loc;
938 va_start (argp, target);
941 for (; matches > 0; matches--)
951 /* Matches that don't have to be undone */
956 (void) va_arg (argp, void **);
961 vp = va_arg (argp, void **);
975 /*********************** Statement level matching **********************/
977 /* Matches the start of a program unit, which is the program keyword
978 followed by an obligatory symbol. */
981 gfc_match_program (void)
986 m = gfc_match ("% %s%t", &sym);
990 gfc_error ("Invalid form of PROGRAM statement at %C");
994 if (m == MATCH_ERROR)
997 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1000 gfc_new_block = sym;
1006 /* Match a simple assignment statement. */
1009 gfc_match_assignment (void)
1011 gfc_expr *lvalue, *rvalue;
1015 old_loc = gfc_current_locus;
1018 m = gfc_match (" %v =", &lvalue);
1021 gfc_current_locus = old_loc;
1022 gfc_free_expr (lvalue);
1026 if (lvalue->symtree->n.sym->attr.protected
1027 && lvalue->symtree->n.sym->attr.use_assoc)
1029 gfc_current_locus = old_loc;
1030 gfc_free_expr (lvalue);
1031 gfc_error ("Setting value of PROTECTED variable at %C");
1036 m = gfc_match (" %e%t", &rvalue);
1039 gfc_current_locus = old_loc;
1040 gfc_free_expr (lvalue);
1041 gfc_free_expr (rvalue);
1045 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1047 new_st.op = EXEC_ASSIGN;
1048 new_st.expr = lvalue;
1049 new_st.expr2 = rvalue;
1051 gfc_check_do_variable (lvalue->symtree);
1057 /* Match a pointer assignment statement. */
1060 gfc_match_pointer_assignment (void)
1062 gfc_expr *lvalue, *rvalue;
1066 old_loc = gfc_current_locus;
1068 lvalue = rvalue = NULL;
1070 m = gfc_match (" %v =>", &lvalue);
1077 m = gfc_match (" %e%t", &rvalue);
1081 if (lvalue->symtree->n.sym->attr.protected
1082 && lvalue->symtree->n.sym->attr.use_assoc)
1084 gfc_error ("Assigning to a PROTECTED pointer at %C");
1089 new_st.op = EXEC_POINTER_ASSIGN;
1090 new_st.expr = lvalue;
1091 new_st.expr2 = rvalue;
1096 gfc_current_locus = old_loc;
1097 gfc_free_expr (lvalue);
1098 gfc_free_expr (rvalue);
1103 /* We try to match an easy arithmetic IF statement. This only happens
1104 when just after having encountered a simple IF statement. This code
1105 is really duplicate with parts of the gfc_match_if code, but this is
1109 match_arithmetic_if (void)
1111 gfc_st_label *l1, *l2, *l3;
1115 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1119 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1120 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1121 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1123 gfc_free_expr (expr);
1127 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF statement "
1128 "at %C") == FAILURE)
1131 new_st.op = EXEC_ARITHMETIC_IF;
1141 /* The IF statement is a bit of a pain. First of all, there are three
1142 forms of it, the simple IF, the IF that starts a block and the
1145 There is a problem with the simple IF and that is the fact that we
1146 only have a single level of undo information on symbols. What this
1147 means is for a simple IF, we must re-match the whole IF statement
1148 multiple times in order to guarantee that the symbol table ends up
1149 in the proper state. */
1151 static match match_simple_forall (void);
1152 static match match_simple_where (void);
1155 gfc_match_if (gfc_statement *if_type)
1158 gfc_st_label *l1, *l2, *l3;
1163 n = gfc_match_label ();
1164 if (n == MATCH_ERROR)
1167 old_loc = gfc_current_locus;
1169 m = gfc_match (" if ( %e", &expr);
1173 if (gfc_match_char (')') != MATCH_YES)
1175 gfc_error ("Syntax error in IF-expression at %C");
1176 gfc_free_expr (expr);
1180 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1186 gfc_error ("Block label not appropriate for arithmetic IF "
1188 gfc_free_expr (expr);
1192 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1193 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1194 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1196 gfc_free_expr (expr);
1200 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF "
1201 "statement at %C") == FAILURE)
1204 new_st.op = EXEC_ARITHMETIC_IF;
1210 *if_type = ST_ARITHMETIC_IF;
1214 if (gfc_match (" then%t") == MATCH_YES)
1216 new_st.op = EXEC_IF;
1218 *if_type = ST_IF_BLOCK;
1224 gfc_error ("Block label is not appropriate IF statement at %C");
1225 gfc_free_expr (expr);
1229 /* At this point the only thing left is a simple IF statement. At
1230 this point, n has to be MATCH_NO, so we don't have to worry about
1231 re-matching a block label. From what we've got so far, try
1232 matching an assignment. */
1234 *if_type = ST_SIMPLE_IF;
1236 m = gfc_match_assignment ();
1240 gfc_free_expr (expr);
1241 gfc_undo_symbols ();
1242 gfc_current_locus = old_loc;
1244 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1245 assignment was found. For MATCH_NO, continue to call the various
1247 if (m == MATCH_ERROR)
1250 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1252 m = gfc_match_pointer_assignment ();
1256 gfc_free_expr (expr);
1257 gfc_undo_symbols ();
1258 gfc_current_locus = old_loc;
1260 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1262 /* Look at the next keyword to see which matcher to call. Matching
1263 the keyword doesn't affect the symbol table, so we don't have to
1264 restore between tries. */
1266 #define match(string, subr, statement) \
1267 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1271 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1272 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1273 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1274 match ("call", gfc_match_call, ST_CALL)
1275 match ("close", gfc_match_close, ST_CLOSE)
1276 match ("continue", gfc_match_continue, ST_CONTINUE)
1277 match ("cycle", gfc_match_cycle, ST_CYCLE)
1278 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1279 match ("end file", gfc_match_endfile, ST_END_FILE)
1280 match ("exit", gfc_match_exit, ST_EXIT)
1281 match ("flush", gfc_match_flush, ST_FLUSH)
1282 match ("forall", match_simple_forall, ST_FORALL)
1283 match ("go to", gfc_match_goto, ST_GOTO)
1284 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1285 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1286 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1287 match ("open", gfc_match_open, ST_OPEN)
1288 match ("pause", gfc_match_pause, ST_NONE)
1289 match ("print", gfc_match_print, ST_WRITE)
1290 match ("read", gfc_match_read, ST_READ)
1291 match ("return", gfc_match_return, ST_RETURN)
1292 match ("rewind", gfc_match_rewind, ST_REWIND)
1293 match ("stop", gfc_match_stop, ST_STOP)
1294 match ("where", match_simple_where, ST_WHERE)
1295 match ("write", gfc_match_write, ST_WRITE)
1297 /* The gfc_match_assignment() above may have returned a MATCH_NO
1298 where the assignment was to a named constant. Check that
1299 special case here. */
1300 m = gfc_match_assignment ();
1303 gfc_error ("Cannot assign to a named constant at %C");
1304 gfc_free_expr (expr);
1305 gfc_undo_symbols ();
1306 gfc_current_locus = old_loc;
1310 /* All else has failed, so give up. See if any of the matchers has
1311 stored an error message of some sort. */
1312 if (gfc_error_check () == 0)
1313 gfc_error ("Unclassifiable statement in IF-clause at %C");
1315 gfc_free_expr (expr);
1320 gfc_error ("Syntax error in IF-clause at %C");
1323 gfc_free_expr (expr);
1327 /* At this point, we've matched the single IF and the action clause
1328 is in new_st. Rearrange things so that the IF statement appears
1331 p = gfc_get_code ();
1332 p->next = gfc_get_code ();
1334 p->next->loc = gfc_current_locus;
1339 gfc_clear_new_st ();
1341 new_st.op = EXEC_IF;
1350 /* Match an ELSE statement. */
1353 gfc_match_else (void)
1355 char name[GFC_MAX_SYMBOL_LEN + 1];
1357 if (gfc_match_eos () == MATCH_YES)
1360 if (gfc_match_name (name) != MATCH_YES
1361 || gfc_current_block () == NULL
1362 || gfc_match_eos () != MATCH_YES)
1364 gfc_error ("Unexpected junk after ELSE statement at %C");
1368 if (strcmp (name, gfc_current_block ()->name) != 0)
1370 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1371 name, gfc_current_block ()->name);
1379 /* Match an ELSE IF statement. */
1382 gfc_match_elseif (void)
1384 char name[GFC_MAX_SYMBOL_LEN + 1];
1388 m = gfc_match (" ( %e ) then", &expr);
1392 if (gfc_match_eos () == MATCH_YES)
1395 if (gfc_match_name (name) != MATCH_YES
1396 || gfc_current_block () == NULL
1397 || gfc_match_eos () != MATCH_YES)
1399 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1403 if (strcmp (name, gfc_current_block ()->name) != 0)
1405 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1406 name, gfc_current_block ()->name);
1411 new_st.op = EXEC_IF;
1416 gfc_free_expr (expr);
1421 /* Free a gfc_iterator structure. */
1424 gfc_free_iterator (gfc_iterator *iter, int flag)
1430 gfc_free_expr (iter->var);
1431 gfc_free_expr (iter->start);
1432 gfc_free_expr (iter->end);
1433 gfc_free_expr (iter->step);
1440 /* Match a DO statement. */
1445 gfc_iterator iter, *ip;
1447 gfc_st_label *label;
1450 old_loc = gfc_current_locus;
1453 iter.var = iter.start = iter.end = iter.step = NULL;
1455 m = gfc_match_label ();
1456 if (m == MATCH_ERROR)
1459 if (gfc_match (" do") != MATCH_YES)
1462 m = gfc_match_st_label (&label);
1463 if (m == MATCH_ERROR)
1466 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1468 if (gfc_match_eos () == MATCH_YES)
1470 iter.end = gfc_logical_expr (1, NULL);
1471 new_st.op = EXEC_DO_WHILE;
1475 /* Match an optional comma, if no comma is found, a space is obligatory. */
1476 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1479 /* See if we have a DO WHILE. */
1480 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1482 new_st.op = EXEC_DO_WHILE;
1486 /* The abortive DO WHILE may have done something to the symbol
1487 table, so we start over. */
1488 gfc_undo_symbols ();
1489 gfc_current_locus = old_loc;
1491 gfc_match_label (); /* This won't error. */
1492 gfc_match (" do "); /* This will work. */
1494 gfc_match_st_label (&label); /* Can't error out. */
1495 gfc_match_char (','); /* Optional comma. */
1497 m = gfc_match_iterator (&iter, 0);
1500 if (m == MATCH_ERROR)
1503 gfc_check_do_variable (iter.var->symtree);
1505 if (gfc_match_eos () != MATCH_YES)
1507 gfc_syntax_error (ST_DO);
1511 new_st.op = EXEC_DO;
1515 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1518 new_st.label = label;
1520 if (new_st.op == EXEC_DO_WHILE)
1521 new_st.expr = iter.end;
1524 new_st.ext.iterator = ip = gfc_get_iterator ();
1531 gfc_free_iterator (&iter, 0);
1537 /* Match an EXIT or CYCLE statement. */
1540 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1542 gfc_state_data *p, *o;
1546 if (gfc_match_eos () == MATCH_YES)
1550 m = gfc_match ("% %s%t", &sym);
1551 if (m == MATCH_ERROR)
1555 gfc_syntax_error (st);
1559 if (sym->attr.flavor != FL_LABEL)
1561 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1562 sym->name, gfc_ascii_statement (st));
1567 /* Find the loop mentioned specified by the label (or lack of a label). */
1568 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1569 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1571 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1577 gfc_error ("%s statement at %C is not within a loop",
1578 gfc_ascii_statement (st));
1580 gfc_error ("%s statement at %C is not within loop '%s'",
1581 gfc_ascii_statement (st), sym->name);
1588 gfc_error ("%s statement at %C leaving OpenMP structured block",
1589 gfc_ascii_statement (st));
1592 else if (st == ST_EXIT
1593 && p->previous != NULL
1594 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1595 && (p->previous->head->op == EXEC_OMP_DO
1596 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1598 gcc_assert (p->previous->head->next != NULL);
1599 gcc_assert (p->previous->head->next->op == EXEC_DO
1600 || p->previous->head->next->op == EXEC_DO_WHILE);
1601 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1605 /* Save the first statement in the loop - needed by the backend. */
1606 new_st.ext.whichloop = p->head;
1614 /* Match the EXIT statement. */
1617 gfc_match_exit (void)
1619 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1623 /* Match the CYCLE statement. */
1626 gfc_match_cycle (void)
1628 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1632 /* Match a number or character constant after a STOP or PAUSE statement. */
1635 gfc_match_stopcode (gfc_statement st)
1645 if (gfc_match_eos () != MATCH_YES)
1647 m = gfc_match_small_literal_int (&stop_code, &cnt);
1648 if (m == MATCH_ERROR)
1651 if (m == MATCH_YES && cnt > 5)
1653 gfc_error ("Too many digits in STOP code at %C");
1659 /* Try a character constant. */
1660 m = gfc_match_expr (&e);
1661 if (m == MATCH_ERROR)
1665 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1669 if (gfc_match_eos () != MATCH_YES)
1673 if (gfc_pure (NULL))
1675 gfc_error ("%s statement not allowed in PURE procedure at %C",
1676 gfc_ascii_statement (st));
1680 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1682 new_st.ext.stop_code = stop_code;
1687 gfc_syntax_error (st);
1696 /* Match the (deprecated) PAUSE statement. */
1699 gfc_match_pause (void)
1703 m = gfc_match_stopcode (ST_PAUSE);
1706 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
1715 /* Match the STOP statement. */
1718 gfc_match_stop (void)
1720 return gfc_match_stopcode (ST_STOP);
1724 /* Match a CONTINUE statement. */
1727 gfc_match_continue (void)
1729 if (gfc_match_eos () != MATCH_YES)
1731 gfc_syntax_error (ST_CONTINUE);
1735 new_st.op = EXEC_CONTINUE;
1740 /* Match the (deprecated) ASSIGN statement. */
1743 gfc_match_assign (void)
1746 gfc_st_label *label;
1748 if (gfc_match (" %l", &label) == MATCH_YES)
1750 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1752 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1754 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
1759 expr->symtree->n.sym->attr.assign = 1;
1761 new_st.op = EXEC_LABEL_ASSIGN;
1762 new_st.label = label;
1771 /* Match the GO TO statement. As a computed GOTO statement is
1772 matched, it is transformed into an equivalent SELECT block. No
1773 tree is necessary, and the resulting jumps-to-jumps are
1774 specifically optimized away by the back end. */
1777 gfc_match_goto (void)
1779 gfc_code *head, *tail;
1782 gfc_st_label *label;
1786 if (gfc_match (" %l%t", &label) == MATCH_YES)
1788 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1791 new_st.op = EXEC_GOTO;
1792 new_st.label = label;
1796 /* The assigned GO TO statement. */
1798 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1800 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
1805 new_st.op = EXEC_GOTO;
1808 if (gfc_match_eos () == MATCH_YES)
1811 /* Match label list. */
1812 gfc_match_char (',');
1813 if (gfc_match_char ('(') != MATCH_YES)
1815 gfc_syntax_error (ST_GOTO);
1822 m = gfc_match_st_label (&label);
1826 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1830 head = tail = gfc_get_code ();
1833 tail->block = gfc_get_code ();
1837 tail->label = label;
1838 tail->op = EXEC_GOTO;
1840 while (gfc_match_char (',') == MATCH_YES);
1842 if (gfc_match (")%t") != MATCH_YES)
1847 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1850 new_st.block = head;
1855 /* Last chance is a computed GO TO statement. */
1856 if (gfc_match_char ('(') != MATCH_YES)
1858 gfc_syntax_error (ST_GOTO);
1867 m = gfc_match_st_label (&label);
1871 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1875 head = tail = gfc_get_code ();
1878 tail->block = gfc_get_code ();
1882 cp = gfc_get_case ();
1883 cp->low = cp->high = gfc_int_expr (i++);
1885 tail->op = EXEC_SELECT;
1886 tail->ext.case_list = cp;
1888 tail->next = gfc_get_code ();
1889 tail->next->op = EXEC_GOTO;
1890 tail->next->label = label;
1892 while (gfc_match_char (',') == MATCH_YES);
1894 if (gfc_match_char (')') != MATCH_YES)
1899 gfc_error ("Statement label list in GOTO at %C cannot be empty");
1903 /* Get the rest of the statement. */
1904 gfc_match_char (',');
1906 if (gfc_match (" %e%t", &expr) != MATCH_YES)
1909 /* At this point, a computed GOTO has been fully matched and an
1910 equivalent SELECT statement constructed. */
1912 new_st.op = EXEC_SELECT;
1915 /* Hack: For a "real" SELECT, the expression is in expr. We put
1916 it in expr2 so we can distinguish then and produce the correct
1918 new_st.expr2 = expr;
1919 new_st.block = head;
1923 gfc_syntax_error (ST_GOTO);
1925 gfc_free_statements (head);
1930 /* Frees a list of gfc_alloc structures. */
1933 gfc_free_alloc_list (gfc_alloc *p)
1940 gfc_free_expr (p->expr);
1946 /* Match an ALLOCATE statement. */
1949 gfc_match_allocate (void)
1951 gfc_alloc *head, *tail;
1958 if (gfc_match_char ('(') != MATCH_YES)
1964 head = tail = gfc_get_alloc ();
1967 tail->next = gfc_get_alloc ();
1971 m = gfc_match_variable (&tail->expr, 0);
1974 if (m == MATCH_ERROR)
1977 if (gfc_check_do_variable (tail->expr->symtree))
1981 && gfc_impure_variable (tail->expr->symtree->n.sym))
1983 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1988 if (tail->expr->ts.type == BT_DERIVED)
1989 tail->expr->ts.derived = gfc_use_derived (tail->expr->ts.derived);
1991 if (gfc_match_char (',') != MATCH_YES)
1994 m = gfc_match (" stat = %v", &stat);
1995 if (m == MATCH_ERROR)
2003 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
2005 gfc_error ("STAT variable '%s' of ALLOCATE statement at %C cannot "
2006 "be INTENT(IN)", stat->symtree->n.sym->name);
2010 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
2012 gfc_error ("Illegal STAT variable in ALLOCATE statement at %C "
2013 "for a PURE procedure");
2017 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
2019 gfc_error ("STAT expression at %C must be a variable");
2023 gfc_check_do_variable(stat->symtree);
2026 if (gfc_match (" )%t") != MATCH_YES)
2029 new_st.op = EXEC_ALLOCATE;
2031 new_st.ext.alloc_list = head;
2036 gfc_syntax_error (ST_ALLOCATE);
2039 gfc_free_expr (stat);
2040 gfc_free_alloc_list (head);
2045 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
2046 a set of pointer assignments to intrinsic NULL(). */
2049 gfc_match_nullify (void)
2057 if (gfc_match_char ('(') != MATCH_YES)
2062 m = gfc_match_variable (&p, 0);
2063 if (m == MATCH_ERROR)
2068 if (gfc_check_do_variable (p->symtree))
2071 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
2073 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
2077 /* build ' => NULL() '. */
2078 e = gfc_get_expr ();
2079 e->where = gfc_current_locus;
2080 e->expr_type = EXPR_NULL;
2081 e->ts.type = BT_UNKNOWN;
2083 /* Chain to list. */
2088 tail->next = gfc_get_code ();
2092 tail->op = EXEC_POINTER_ASSIGN;
2096 if (gfc_match (" )%t") == MATCH_YES)
2098 if (gfc_match_char (',') != MATCH_YES)
2105 gfc_syntax_error (ST_NULLIFY);
2108 gfc_free_statements (new_st.next);
2113 /* Match a DEALLOCATE statement. */
2116 gfc_match_deallocate (void)
2118 gfc_alloc *head, *tail;
2125 if (gfc_match_char ('(') != MATCH_YES)
2131 head = tail = gfc_get_alloc ();
2134 tail->next = gfc_get_alloc ();
2138 m = gfc_match_variable (&tail->expr, 0);
2139 if (m == MATCH_ERROR)
2144 if (gfc_check_do_variable (tail->expr->symtree))
2148 && gfc_impure_variable (tail->expr->symtree->n.sym))
2150 gfc_error ("Illegal deallocate-expression in DEALLOCATE at %C "
2151 "for a PURE procedure");
2155 if (gfc_match_char (',') != MATCH_YES)
2158 m = gfc_match (" stat = %v", &stat);
2159 if (m == MATCH_ERROR)
2167 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
2169 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
2170 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
2174 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
2176 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
2177 "for a PURE procedure");
2181 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
2183 gfc_error ("STAT expression at %C must be a variable");
2187 gfc_check_do_variable(stat->symtree);
2190 if (gfc_match (" )%t") != MATCH_YES)
2193 new_st.op = EXEC_DEALLOCATE;
2195 new_st.ext.alloc_list = head;
2200 gfc_syntax_error (ST_DEALLOCATE);
2203 gfc_free_expr (stat);
2204 gfc_free_alloc_list (head);
2209 /* Match a RETURN statement. */
2212 gfc_match_return (void)
2216 gfc_compile_state s;
2220 if (gfc_match_eos () == MATCH_YES)
2223 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2225 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2230 if (gfc_current_form == FORM_FREE)
2232 /* The following are valid, so we can't require a blank after the
2236 c = gfc_peek_char ();
2237 if (ISALPHA (c) || ISDIGIT (c))
2241 m = gfc_match (" %e%t", &e);
2244 if (m == MATCH_ERROR)
2247 gfc_syntax_error (ST_RETURN);
2254 gfc_enclosing_unit (&s);
2255 if (s == COMP_PROGRAM
2256 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2257 "main program at %C") == FAILURE)
2260 new_st.op = EXEC_RETURN;
2267 /* Match a CALL statement. The tricky part here are possible
2268 alternate return specifiers. We handle these by having all
2269 "subroutines" actually return an integer via a register that gives
2270 the return number. If the call specifies alternate returns, we
2271 generate code for a SELECT statement whose case clauses contain
2272 GOTOs to the various labels. */
2275 gfc_match_call (void)
2277 char name[GFC_MAX_SYMBOL_LEN + 1];
2278 gfc_actual_arglist *a, *arglist;
2288 m = gfc_match ("% %n", name);
2294 if (gfc_get_ha_sym_tree (name, &st))
2299 if (sym->ns != gfc_current_ns
2300 && !sym->attr.generic
2301 && !sym->attr.subroutine
2302 && gfc_get_sym_tree (name, NULL, &st) == 1)
2307 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2310 gfc_set_sym_referenced (sym);
2312 if (gfc_match_eos () != MATCH_YES)
2314 m = gfc_match_actual_arglist (1, &arglist);
2317 if (m == MATCH_ERROR)
2320 if (gfc_match_eos () != MATCH_YES)
2324 /* If any alternate return labels were found, construct a SELECT
2325 statement that will jump to the right place. */
2328 for (a = arglist; a; a = a->next)
2329 if (a->expr == NULL)
2334 gfc_symtree *select_st;
2335 gfc_symbol *select_sym;
2336 char name[GFC_MAX_SYMBOL_LEN + 1];
2338 new_st.next = c = gfc_get_code ();
2339 c->op = EXEC_SELECT;
2340 sprintf (name, "_result_%s", sym->name);
2341 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
2343 select_sym = select_st->n.sym;
2344 select_sym->ts.type = BT_INTEGER;
2345 select_sym->ts.kind = gfc_default_integer_kind;
2346 gfc_set_sym_referenced (select_sym);
2347 c->expr = gfc_get_expr ();
2348 c->expr->expr_type = EXPR_VARIABLE;
2349 c->expr->symtree = select_st;
2350 c->expr->ts = select_sym->ts;
2351 c->expr->where = gfc_current_locus;
2354 for (a = arglist; a; a = a->next)
2356 if (a->expr != NULL)
2359 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2364 c->block = gfc_get_code ();
2366 c->op = EXEC_SELECT;
2368 new_case = gfc_get_case ();
2369 new_case->high = new_case->low = gfc_int_expr (i);
2370 c->ext.case_list = new_case;
2372 c->next = gfc_get_code ();
2373 c->next->op = EXEC_GOTO;
2374 c->next->label = a->label;
2378 new_st.op = EXEC_CALL;
2379 new_st.symtree = st;
2380 new_st.ext.actual = arglist;
2385 gfc_syntax_error (ST_CALL);
2388 gfc_free_actual_arglist (arglist);
2393 /* Given a name, return a pointer to the common head structure,
2394 creating it if it does not exist. If FROM_MODULE is nonzero, we
2395 mangle the name so that it doesn't interfere with commons defined
2396 in the using namespace.
2397 TODO: Add to global symbol tree. */
2400 gfc_get_common (const char *name, int from_module)
2403 static int serial = 0;
2404 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
2408 /* A use associated common block is only needed to correctly layout
2409 the variables it contains. */
2410 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2411 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2415 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2418 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2421 if (st->n.common == NULL)
2423 st->n.common = gfc_get_common_head ();
2424 st->n.common->where = gfc_current_locus;
2425 strcpy (st->n.common->name, name);
2428 return st->n.common;
2432 /* Match a common block name. */
2434 match match_common_name (char *name)
2438 if (gfc_match_char ('/') == MATCH_NO)
2444 if (gfc_match_char ('/') == MATCH_YES)
2450 m = gfc_match_name (name);
2452 if (m == MATCH_ERROR)
2454 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2457 gfc_error ("Syntax error in common block name at %C");
2462 /* Match a COMMON statement. */
2465 gfc_match_common (void)
2467 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2468 char name[GFC_MAX_SYMBOL_LEN + 1];
2475 old_blank_common = gfc_current_ns->blank_common.head;
2476 if (old_blank_common)
2478 while (old_blank_common->common_next)
2479 old_blank_common = old_blank_common->common_next;
2486 m = match_common_name (name);
2487 if (m == MATCH_ERROR)
2490 gsym = gfc_get_gsymbol (name);
2491 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2493 gfc_error ("Symbol '%s' at %C is already an external symbol that "
2494 "is not COMMON", name);
2498 if (gsym->type == GSYM_UNKNOWN)
2500 gsym->type = GSYM_COMMON;
2501 gsym->where = gfc_current_locus;
2507 if (name[0] == '\0')
2509 if (gfc_current_ns->is_block_data)
2511 gfc_warning ("BLOCK DATA unit cannot contain blank COMMON "
2514 t = &gfc_current_ns->blank_common;
2515 if (t->head == NULL)
2516 t->where = gfc_current_locus;
2520 t = gfc_get_common (name, 0);
2529 while (tail->common_next)
2530 tail = tail->common_next;
2533 /* Grab the list of symbols. */
2536 m = gfc_match_symbol (&sym, 0);
2537 if (m == MATCH_ERROR)
2542 /* Store a ref to the common block for error checking. */
2543 sym->common_block = t;
2545 /* See if we know the current common block is bind(c), and if
2546 so, then see if we can check if the symbol is (which it'll
2547 need to be). This can happen if the bind(c) attr stmt was
2548 applied to the common block, and the variable(s) already
2549 defined, before declaring the common block. */
2550 if (t->is_bind_c == 1)
2552 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
2554 /* If we find an error, just print it and continue,
2555 cause it's just semantic, and we can see if there
2557 gfc_error_now ("Variable '%s' at %L in common block '%s' "
2558 "at %C must be declared with a C "
2559 "interoperable kind since common block "
2561 sym->name, &(sym->declared_at), t->name,
2565 if (sym->attr.is_bind_c == 1)
2566 gfc_error_now ("Variable '%s' in common block "
2567 "'%s' at %C can not be bind(c) since "
2568 "it is not global", sym->name, t->name);
2571 if (sym->attr.in_common)
2573 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2578 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2581 if (sym->value != NULL && sym->value->expr_type != EXPR_NULL
2582 && (name[0] == '\0' || !sym->attr.data))
2584 if (name[0] == '\0')
2585 gfc_error ("Previously initialized symbol '%s' in "
2586 "blank COMMON block at %C", sym->name);
2588 gfc_error ("Previously initialized symbol '%s' in "
2589 "COMMON block '%s' at %C", sym->name, name);
2593 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2596 /* Derived type names must have the SEQUENCE attribute. */
2597 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2599 gfc_error ("Derived type variable in COMMON at %C does not "
2600 "have the SEQUENCE attribute");
2605 tail->common_next = sym;
2611 /* Deal with an optional array specification after the
2613 m = gfc_match_array_spec (&as);
2614 if (m == MATCH_ERROR)
2619 if (as->type != AS_EXPLICIT)
2621 gfc_error ("Array specification for symbol '%s' in COMMON "
2622 "at %C must be explicit", sym->name);
2626 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2629 if (sym->attr.pointer)
2631 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
2632 "POINTER array", sym->name);
2641 sym->common_head = t;
2643 /* Check to see if the symbol is already in an equivalence group.
2644 If it is, set the other members as being in common. */
2645 if (sym->attr.in_equivalence)
2647 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2649 for (e2 = e1; e2; e2 = e2->eq)
2650 if (e2->expr->symtree->n.sym == sym)
2657 for (e2 = e1; e2; e2 = e2->eq)
2659 other = e2->expr->symtree->n.sym;
2660 if (other->common_head
2661 && other->common_head != sym->common_head)
2663 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2664 "%C is being indirectly equivalenced to "
2665 "another COMMON block '%s'",
2666 sym->name, sym->common_head->name,
2667 other->common_head->name);
2670 other->attr.in_common = 1;
2671 other->common_head = t;
2677 gfc_gobble_whitespace ();
2678 if (gfc_match_eos () == MATCH_YES)
2680 if (gfc_peek_char () == '/')
2682 if (gfc_match_char (',') != MATCH_YES)
2684 gfc_gobble_whitespace ();
2685 if (gfc_peek_char () == '/')
2694 gfc_syntax_error (ST_COMMON);
2697 if (old_blank_common)
2698 old_blank_common->common_next = NULL;
2700 gfc_current_ns->blank_common.head = NULL;
2701 gfc_free_array_spec (as);
2706 /* Match a BLOCK DATA program unit. */
2709 gfc_match_block_data (void)
2711 char name[GFC_MAX_SYMBOL_LEN + 1];
2715 if (gfc_match_eos () == MATCH_YES)
2717 gfc_new_block = NULL;
2721 m = gfc_match ("% %n%t", name);
2725 if (gfc_get_symbol (name, NULL, &sym))
2728 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2731 gfc_new_block = sym;
2737 /* Free a namelist structure. */
2740 gfc_free_namelist (gfc_namelist *name)
2744 for (; name; name = n)
2752 /* Match a NAMELIST statement. */
2755 gfc_match_namelist (void)
2757 gfc_symbol *group_name, *sym;
2761 m = gfc_match (" / %s /", &group_name);
2764 if (m == MATCH_ERROR)
2769 if (group_name->ts.type != BT_UNKNOWN)
2771 gfc_error ("Namelist group name '%s' at %C already has a basic "
2772 "type of %s", group_name->name,
2773 gfc_typename (&group_name->ts));
2777 if (group_name->attr.flavor == FL_NAMELIST
2778 && group_name->attr.use_assoc
2779 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2780 "at %C already is USE associated and can"
2781 "not be respecified.", group_name->name)
2785 if (group_name->attr.flavor != FL_NAMELIST
2786 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2787 group_name->name, NULL) == FAILURE)
2792 m = gfc_match_symbol (&sym, 1);
2795 if (m == MATCH_ERROR)
2798 if (sym->attr.in_namelist == 0
2799 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2802 /* Use gfc_error_check here, rather than goto error, so that
2803 these are the only errors for the next two lines. */
2804 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2806 gfc_error ("Assumed size array '%s' in namelist '%s' at "
2807 "%C is not allowed", sym->name, group_name->name);
2811 if (sym->ts.type == BT_CHARACTER && sym->ts.cl->length == NULL)
2813 gfc_error ("Assumed character length '%s' in namelist '%s' at "
2814 "%C is not allowed", sym->name, group_name->name);
2818 if (sym->as && sym->as->type == AS_ASSUMED_SHAPE
2819 && gfc_notify_std (GFC_STD_GNU, "Assumed shape array '%s' in "
2820 "namelist '%s' at %C is an extension.",
2821 sym->name, group_name->name) == FAILURE)
2824 nl = gfc_get_namelist ();
2828 if (group_name->namelist == NULL)
2829 group_name->namelist = group_name->namelist_tail = nl;
2832 group_name->namelist_tail->next = nl;
2833 group_name->namelist_tail = nl;
2836 if (gfc_match_eos () == MATCH_YES)
2839 m = gfc_match_char (',');
2841 if (gfc_match_char ('/') == MATCH_YES)
2843 m2 = gfc_match (" %s /", &group_name);
2844 if (m2 == MATCH_YES)
2846 if (m2 == MATCH_ERROR)
2860 gfc_syntax_error (ST_NAMELIST);
2867 /* Match a MODULE statement. */
2870 gfc_match_module (void)
2874 m = gfc_match (" %s%t", &gfc_new_block);
2878 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2879 gfc_new_block->name, NULL) == FAILURE)
2886 /* Free equivalence sets and lists. Recursively is the easiest way to
2890 gfc_free_equiv (gfc_equiv *eq)
2895 gfc_free_equiv (eq->eq);
2896 gfc_free_equiv (eq->next);
2897 gfc_free_expr (eq->expr);
2902 /* Match an EQUIVALENCE statement. */
2905 gfc_match_equivalence (void)
2907 gfc_equiv *eq, *set, *tail;
2911 gfc_common_head *common_head = NULL;
2919 eq = gfc_get_equiv ();
2923 eq->next = gfc_current_ns->equiv;
2924 gfc_current_ns->equiv = eq;
2926 if (gfc_match_char ('(') != MATCH_YES)
2930 common_flag = FALSE;
2935 m = gfc_match_equiv_variable (&set->expr);
2936 if (m == MATCH_ERROR)
2941 /* count the number of objects. */
2944 if (gfc_match_char ('%') == MATCH_YES)
2946 gfc_error ("Derived type component %C is not a "
2947 "permitted EQUIVALENCE member");
2951 for (ref = set->expr->ref; ref; ref = ref->next)
2952 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2954 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
2955 "be an array section");
2959 sym = set->expr->symtree->n.sym;
2961 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
2964 if (sym->attr.in_common)
2967 common_head = sym->common_head;
2970 if (gfc_match_char (')') == MATCH_YES)
2973 if (gfc_match_char (',') != MATCH_YES)
2976 set->eq = gfc_get_equiv ();
2982 gfc_error ("EQUIVALENCE at %C requires two or more objects");
2986 /* If one of the members of an equivalence is in common, then
2987 mark them all as being in common. Before doing this, check
2988 that members of the equivalence group are not in different
2991 for (set = eq; set; set = set->eq)
2993 sym = set->expr->symtree->n.sym;
2994 if (sym->common_head && sym->common_head != common_head)
2996 gfc_error ("Attempt to indirectly overlap COMMON "
2997 "blocks %s and %s by EQUIVALENCE at %C",
2998 sym->common_head->name, common_head->name);
3001 sym->attr.in_common = 1;
3002 sym->common_head = common_head;
3005 if (gfc_match_eos () == MATCH_YES)
3007 if (gfc_match_char (',') != MATCH_YES)
3014 gfc_syntax_error (ST_EQUIVALENCE);
3020 gfc_free_equiv (gfc_current_ns->equiv);
3021 gfc_current_ns->equiv = eq;
3027 /* Check that a statement function is not recursive. This is done by looking
3028 for the statement function symbol(sym) by looking recursively through its
3029 expression(e). If a reference to sym is found, true is returned.
3030 12.5.4 requires that any variable of function that is implicitly typed
3031 shall have that type confirmed by any subsequent type declaration. The
3032 implicit typing is conveniently done here. */
3035 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
3037 gfc_actual_arglist *arg;
3044 switch (e->expr_type)
3047 for (arg = e->value.function.actual; arg; arg = arg->next)
3049 if (sym->name == arg->name || recursive_stmt_fcn (arg->expr, sym))
3053 if (e->symtree == NULL)
3056 /* Check the name before testing for nested recursion! */
3057 if (sym->name == e->symtree->n.sym->name)
3060 /* Catch recursion via other statement functions. */
3061 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
3062 && e->symtree->n.sym->value
3063 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
3066 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3067 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3072 if (e->symtree && sym->name == e->symtree->n.sym->name)
3075 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3076 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3080 if (recursive_stmt_fcn (e->value.op.op1, sym)
3081 || recursive_stmt_fcn (e->value.op.op2, sym))
3089 /* Component references do not need to be checked. */
3092 for (ref = e->ref; ref; ref = ref->next)
3097 for (i = 0; i < ref->u.ar.dimen; i++)
3099 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
3100 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
3101 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
3107 if (recursive_stmt_fcn (ref->u.ss.start, sym)
3108 || recursive_stmt_fcn (ref->u.ss.end, sym))
3122 /* Match a statement function declaration. It is so easy to match
3123 non-statement function statements with a MATCH_ERROR as opposed to
3124 MATCH_NO that we suppress error message in most cases. */
3127 gfc_match_st_function (void)
3129 gfc_error_buf old_error;
3134 m = gfc_match_symbol (&sym, 0);
3138 gfc_push_error (&old_error);
3140 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
3141 sym->name, NULL) == FAILURE)
3144 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
3147 m = gfc_match (" = %e%t", &expr);
3151 gfc_free_error (&old_error);
3152 if (m == MATCH_ERROR)
3155 if (recursive_stmt_fcn (expr, sym))
3157 gfc_error ("Statement function at %L is recursive", &expr->where);
3166 gfc_pop_error (&old_error);
3171 /***************** SELECT CASE subroutines ******************/
3173 /* Free a single case structure. */
3176 free_case (gfc_case *p)
3178 if (p->low == p->high)
3180 gfc_free_expr (p->low);
3181 gfc_free_expr (p->high);
3186 /* Free a list of case structures. */
3189 gfc_free_case_list (gfc_case *p)
3201 /* Match a single case selector. */
3204 match_case_selector (gfc_case **cp)
3209 c = gfc_get_case ();
3210 c->where = gfc_current_locus;
3212 if (gfc_match_char (':') == MATCH_YES)
3214 m = gfc_match_init_expr (&c->high);
3217 if (m == MATCH_ERROR)
3222 m = gfc_match_init_expr (&c->low);
3223 if (m == MATCH_ERROR)
3228 /* If we're not looking at a ':' now, make a range out of a single
3229 target. Else get the upper bound for the case range. */
3230 if (gfc_match_char (':') != MATCH_YES)
3234 m = gfc_match_init_expr (&c->high);
3235 if (m == MATCH_ERROR)
3237 /* MATCH_NO is fine. It's OK if nothing is there! */
3245 gfc_error ("Expected initialization expression in CASE at %C");
3253 /* Match the end of a case statement. */
3256 match_case_eos (void)
3258 char name[GFC_MAX_SYMBOL_LEN + 1];
3261 if (gfc_match_eos () == MATCH_YES)
3264 /* If the case construct doesn't have a case-construct-name, we
3265 should have matched the EOS. */
3266 if (!gfc_current_block ())
3268 gfc_error ("Expected the name of the SELECT CASE construct at %C");
3272 gfc_gobble_whitespace ();
3274 m = gfc_match_name (name);
3278 if (strcmp (name, gfc_current_block ()->name) != 0)
3280 gfc_error ("Expected case name of '%s' at %C",
3281 gfc_current_block ()->name);
3285 return gfc_match_eos ();
3289 /* Match a SELECT statement. */
3292 gfc_match_select (void)
3297 m = gfc_match_label ();
3298 if (m == MATCH_ERROR)
3301 m = gfc_match (" select case ( %e )%t", &expr);
3305 new_st.op = EXEC_SELECT;
3312 /* Match a CASE statement. */
3315 gfc_match_case (void)
3317 gfc_case *c, *head, *tail;
3322 if (gfc_current_state () != COMP_SELECT)
3324 gfc_error ("Unexpected CASE statement at %C");
3328 if (gfc_match ("% default") == MATCH_YES)
3330 m = match_case_eos ();
3333 if (m == MATCH_ERROR)
3336 new_st.op = EXEC_SELECT;
3337 c = gfc_get_case ();
3338 c->where = gfc_current_locus;
3339 new_st.ext.case_list = c;
3343 if (gfc_match_char ('(') != MATCH_YES)
3348 if (match_case_selector (&c) == MATCH_ERROR)
3358 if (gfc_match_char (')') == MATCH_YES)
3360 if (gfc_match_char (',') != MATCH_YES)
3364 m = match_case_eos ();
3367 if (m == MATCH_ERROR)
3370 new_st.op = EXEC_SELECT;
3371 new_st.ext.case_list = head;
3376 gfc_error ("Syntax error in CASE-specification at %C");
3379 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3383 /********************* WHERE subroutines ********************/
3385 /* Match the rest of a simple WHERE statement that follows an IF statement.
3389 match_simple_where (void)
3395 m = gfc_match (" ( %e )", &expr);
3399 m = gfc_match_assignment ();
3402 if (m == MATCH_ERROR)
3405 if (gfc_match_eos () != MATCH_YES)
3408 c = gfc_get_code ();
3412 c->next = gfc_get_code ();
3415 gfc_clear_new_st ();
3417 new_st.op = EXEC_WHERE;
3423 gfc_syntax_error (ST_WHERE);
3426 gfc_free_expr (expr);
3431 /* Match a WHERE statement. */
3434 gfc_match_where (gfc_statement *st)
3440 m0 = gfc_match_label ();
3441 if (m0 == MATCH_ERROR)
3444 m = gfc_match (" where ( %e )", &expr);
3448 if (gfc_match_eos () == MATCH_YES)
3450 *st = ST_WHERE_BLOCK;
3451 new_st.op = EXEC_WHERE;
3456 m = gfc_match_assignment ();
3458 gfc_syntax_error (ST_WHERE);
3462 gfc_free_expr (expr);
3466 /* We've got a simple WHERE statement. */
3468 c = gfc_get_code ();
3472 c->next = gfc_get_code ();
3475 gfc_clear_new_st ();
3477 new_st.op = EXEC_WHERE;
3484 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3485 new_st if successful. */
3488 gfc_match_elsewhere (void)
3490 char name[GFC_MAX_SYMBOL_LEN + 1];
3494 if (gfc_current_state () != COMP_WHERE)
3496 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3502 if (gfc_match_char ('(') == MATCH_YES)
3504 m = gfc_match_expr (&expr);
3507 if (m == MATCH_ERROR)
3510 if (gfc_match_char (')') != MATCH_YES)
3514 if (gfc_match_eos () != MATCH_YES)
3516 /* Only makes sense if we have a where-construct-name. */
3517 if (!gfc_current_block ())
3522 /* Better be a name at this point. */
3523 m = gfc_match_name (name);
3526 if (m == MATCH_ERROR)
3529 if (gfc_match_eos () != MATCH_YES)
3532 if (strcmp (name, gfc_current_block ()->name) != 0)
3534 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3535 name, gfc_current_block ()->name);
3540 new_st.op = EXEC_WHERE;
3545 gfc_syntax_error (ST_ELSEWHERE);
3548 gfc_free_expr (expr);
3553 /******************** FORALL subroutines ********************/
3555 /* Free a list of FORALL iterators. */
3558 gfc_free_forall_iterator (gfc_forall_iterator *iter)
3560 gfc_forall_iterator *next;
3565 gfc_free_expr (iter->var);
3566 gfc_free_expr (iter->start);
3567 gfc_free_expr (iter->end);
3568 gfc_free_expr (iter->stride);
3575 /* Match an iterator as part of a FORALL statement. The format is:
3577 <var> = <start>:<end>[:<stride>]
3579 On MATCH_NO, the caller tests for the possibility that there is a
3580 scalar mask expression. */
3583 match_forall_iterator (gfc_forall_iterator **result)
3585 gfc_forall_iterator *iter;
3589 where = gfc_current_locus;
3590 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3592 m = gfc_match_expr (&iter->var);
3596 if (gfc_match_char ('=') != MATCH_YES
3597 || iter->var->expr_type != EXPR_VARIABLE)
3603 m = gfc_match_expr (&iter->start);
3607 if (gfc_match_char (':') != MATCH_YES)
3610 m = gfc_match_expr (&iter->end);
3613 if (m == MATCH_ERROR)
3616 if (gfc_match_char (':') == MATCH_NO)
3617 iter->stride = gfc_int_expr (1);
3620 m = gfc_match_expr (&iter->stride);
3623 if (m == MATCH_ERROR)
3627 /* Mark the iteration variable's symbol as used as a FORALL index. */
3628 iter->var->symtree->n.sym->forall_index = true;
3634 gfc_error ("Syntax error in FORALL iterator at %C");
3639 gfc_current_locus = where;
3640 gfc_free_forall_iterator (iter);
3645 /* Match the header of a FORALL statement. */
3648 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
3650 gfc_forall_iterator *head, *tail, *new;
3654 gfc_gobble_whitespace ();
3659 if (gfc_match_char ('(') != MATCH_YES)
3662 m = match_forall_iterator (&new);
3663 if (m == MATCH_ERROR)
3672 if (gfc_match_char (',') != MATCH_YES)
3675 m = match_forall_iterator (&new);
3676 if (m == MATCH_ERROR)
3686 /* Have to have a mask expression. */
3688 m = gfc_match_expr (&msk);
3691 if (m == MATCH_ERROR)
3697 if (gfc_match_char (')') == MATCH_NO)
3705 gfc_syntax_error (ST_FORALL);
3708 gfc_free_expr (msk);
3709 gfc_free_forall_iterator (head);
3714 /* Match the rest of a simple FORALL statement that follows an
3718 match_simple_forall (void)
3720 gfc_forall_iterator *head;
3729 m = match_forall_header (&head, &mask);
3736 m = gfc_match_assignment ();
3738 if (m == MATCH_ERROR)
3742 m = gfc_match_pointer_assignment ();
3743 if (m == MATCH_ERROR)
3749 c = gfc_get_code ();
3751 c->loc = gfc_current_locus;
3753 if (gfc_match_eos () != MATCH_YES)
3756 gfc_clear_new_st ();
3757 new_st.op = EXEC_FORALL;
3759 new_st.ext.forall_iterator = head;
3760 new_st.block = gfc_get_code ();
3762 new_st.block->op = EXEC_FORALL;
3763 new_st.block->next = c;
3768 gfc_syntax_error (ST_FORALL);
3771 gfc_free_forall_iterator (head);
3772 gfc_free_expr (mask);
3778 /* Match a FORALL statement. */
3781 gfc_match_forall (gfc_statement *st)
3783 gfc_forall_iterator *head;
3792 m0 = gfc_match_label ();
3793 if (m0 == MATCH_ERROR)
3796 m = gfc_match (" forall");
3800 m = match_forall_header (&head, &mask);
3801 if (m == MATCH_ERROR)
3806 if (gfc_match_eos () == MATCH_YES)
3808 *st = ST_FORALL_BLOCK;
3809 new_st.op = EXEC_FORALL;
3811 new_st.ext.forall_iterator = head;
3815 m = gfc_match_assignment ();
3816 if (m == MATCH_ERROR)
3820 m = gfc_match_pointer_assignment ();
3821 if (m == MATCH_ERROR)
3827 c = gfc_get_code ();
3829 c->loc = gfc_current_locus;
3831 gfc_clear_new_st ();
3832 new_st.op = EXEC_FORALL;
3834 new_st.ext.forall_iterator = head;
3835 new_st.block = gfc_get_code ();
3836 new_st.block->op = EXEC_FORALL;
3837 new_st.block->next = c;
3843 gfc_syntax_error (ST_FORALL);
3846 gfc_free_forall_iterator (head);
3847 gfc_free_expr (mask);
3848 gfc_free_statements (c);