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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
29 /* For matching and debugging purposes. Order matters here! The
30 unary operators /must/ precede the binary plus and minus, or
31 the expression parser breaks. */
33 mstring intrinsic_operators[] = {
34 minit ("+", INTRINSIC_UPLUS),
35 minit ("-", INTRINSIC_UMINUS),
36 minit ("+", INTRINSIC_PLUS),
37 minit ("-", INTRINSIC_MINUS),
38 minit ("**", INTRINSIC_POWER),
39 minit ("//", INTRINSIC_CONCAT),
40 minit ("*", INTRINSIC_TIMES),
41 minit ("/", INTRINSIC_DIVIDE),
42 minit (".and.", INTRINSIC_AND),
43 minit (".or.", INTRINSIC_OR),
44 minit (".eqv.", INTRINSIC_EQV),
45 minit (".neqv.", INTRINSIC_NEQV),
46 minit (".eq.", INTRINSIC_EQ_OS),
47 minit ("==", INTRINSIC_EQ),
48 minit (".ne.", INTRINSIC_NE_OS),
49 minit ("/=", INTRINSIC_NE),
50 minit (".ge.", INTRINSIC_GE_OS),
51 minit (">=", INTRINSIC_GE),
52 minit (".le.", INTRINSIC_LE_OS),
53 minit ("<=", INTRINSIC_LE),
54 minit (".lt.", INTRINSIC_LT_OS),
55 minit ("<", INTRINSIC_LT),
56 minit (".gt.", INTRINSIC_GT_OS),
57 minit (">", INTRINSIC_GT),
58 minit (".not.", INTRINSIC_NOT),
59 minit ("parens", INTRINSIC_PARENTHESES),
60 minit (NULL, INTRINSIC_NONE)
64 /******************** Generic matching subroutines ************************/
66 /* See if the next character is a special character that has
67 escaped by a \ via the -fbackslash option. */
70 gfc_match_special_char (int *c)
77 switch (gfc_next_char_literal (1))
107 /* Unknown backslash codes are simply not expanded. */
116 /* In free form, match at least one space. Always matches in fixed
120 gfc_match_space (void)
125 if (gfc_current_form == FORM_FIXED)
128 old_loc = gfc_current_locus;
130 c = gfc_next_char ();
131 if (!gfc_is_whitespace (c))
133 gfc_current_locus = old_loc;
137 gfc_gobble_whitespace ();
143 /* Match an end of statement. End of statement is optional
144 whitespace, followed by a ';' or '\n' or comment '!'. If a
145 semicolon is found, we continue to eat whitespace and semicolons. */
157 old_loc = gfc_current_locus;
158 gfc_gobble_whitespace ();
160 c = gfc_next_char ();
166 c = gfc_next_char ();
183 gfc_current_locus = old_loc;
184 return (flag) ? MATCH_YES : MATCH_NO;
188 /* Match a literal integer on the input, setting the value on
189 MATCH_YES. Literal ints occur in kind-parameters as well as
190 old-style character length specifications. If cnt is non-NULL it
191 will be set to the number of digits. */
194 gfc_match_small_literal_int (int *value, int *cnt)
200 old_loc = gfc_current_locus;
202 gfc_gobble_whitespace ();
203 c = gfc_next_char ();
209 gfc_current_locus = old_loc;
218 old_loc = gfc_current_locus;
219 c = gfc_next_char ();
224 i = 10 * i + c - '0';
229 gfc_error ("Integer too large at %C");
234 gfc_current_locus = old_loc;
243 /* Match a small, constant integer expression, like in a kind
244 statement. On MATCH_YES, 'value' is set. */
247 gfc_match_small_int (int *value)
254 m = gfc_match_expr (&expr);
258 p = gfc_extract_int (expr, &i);
259 gfc_free_expr (expr);
272 /* This function is the same as the gfc_match_small_int, except that
273 we're keeping the pointer to the expr. This function could just be
274 removed and the previously mentioned one modified, though all calls
275 to it would have to be modified then (and there were a number of
276 them). Return MATCH_ERROR if fail to extract the int; otherwise,
277 return the result of gfc_match_expr(). The expr (if any) that was
278 matched is returned in the parameter expr. */
281 gfc_match_small_int_expr (int *value, gfc_expr **expr)
287 m = gfc_match_expr (expr);
291 p = gfc_extract_int (*expr, &i);
304 /* Matches a statement label. Uses gfc_match_small_literal_int() to
305 do most of the work. */
308 gfc_match_st_label (gfc_st_label **label)
314 old_loc = gfc_current_locus;
316 m = gfc_match_small_literal_int (&i, &cnt);
322 gfc_error ("Too many digits in statement label at %C");
328 gfc_error ("Statement label at %C is zero");
332 *label = gfc_get_st_label (i);
337 gfc_current_locus = old_loc;
342 /* Match and validate a label associated with a named IF, DO or SELECT
343 statement. If the symbol does not have the label attribute, we add
344 it. We also make sure the symbol does not refer to another
345 (active) block. A matched label is pointed to by gfc_new_block. */
348 gfc_match_label (void)
350 char name[GFC_MAX_SYMBOL_LEN + 1];
353 gfc_new_block = NULL;
355 m = gfc_match (" %n :", name);
359 if (gfc_get_symbol (name, NULL, &gfc_new_block))
361 gfc_error ("Label name '%s' at %C is ambiguous", name);
365 if (gfc_new_block->attr.flavor == FL_LABEL)
367 gfc_error ("Duplicate construct label '%s' at %C", name);
371 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
372 gfc_new_block->name, NULL) == FAILURE)
379 /* Try and match the input against an array of possibilities. If one
380 potential matching string is a substring of another, the longest
381 match takes precedence. Spaces in the target strings are optional
382 spaces that do not necessarily have to be found in the input
383 stream. In fixed mode, spaces never appear. If whitespace is
384 matched, it matches unlimited whitespace in the input. For this
385 reason, the 'mp' member of the mstring structure is used to track
386 the progress of each potential match.
388 If there is no match we return the tag associated with the
389 terminating NULL mstring structure and leave the locus pointer
390 where it started. If there is a match we return the tag member of
391 the matched mstring and leave the locus pointer after the matched
394 A '%' character is a mandatory space. */
397 gfc_match_strings (mstring *a)
399 mstring *p, *best_match;
400 int no_match, c, possibles;
405 for (p = a; p->string != NULL; p++)
414 match_loc = gfc_current_locus;
416 gfc_gobble_whitespace ();
418 while (possibles > 0)
420 c = gfc_next_char ();
422 /* Apply the next character to the current possibilities. */
423 for (p = a; p->string != NULL; p++)
430 /* Space matches 1+ whitespace(s). */
431 if ((gfc_current_form == FORM_FREE) && gfc_is_whitespace (c))
449 match_loc = gfc_current_locus;
457 gfc_current_locus = match_loc;
459 return (best_match == NULL) ? no_match : best_match->tag;
463 /* See if the current input looks like a name of some sort. Modifies
464 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
465 Note that options.c restricts max_identifier_length to not more
466 than GFC_MAX_SYMBOL_LEN. */
469 gfc_match_name (char *buffer)
474 old_loc = gfc_current_locus;
475 gfc_gobble_whitespace ();
477 c = gfc_next_char ();
478 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
480 if (gfc_error_flag_test() == 0)
481 gfc_error ("Invalid character in name at %C");
482 gfc_current_locus = old_loc;
492 if (i > gfc_option.max_identifier_length)
494 gfc_error ("Name at %C is too long");
498 old_loc = gfc_current_locus;
499 c = gfc_next_char ();
501 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
504 gfc_current_locus = old_loc;
510 /* Match a valid name for C, which is almost the same as for Fortran,
511 except that you can start with an underscore, etc.. It could have
512 been done by modifying the gfc_match_name, but this way other
513 things C allows can be added, such as no limits on the length.
514 Right now, the length is limited to the same thing as Fortran..
515 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
516 input characters from being automatically lower cased, since C is
517 case sensitive. The parameter, buffer, is used to return the name
518 that is matched. Return MATCH_ERROR if the name is too long
519 (though this is a self-imposed limit), MATCH_NO if what we're
520 seeing isn't a name, and MATCH_YES if we successfully match a C
524 gfc_match_name_C (char *buffer)
530 old_loc = gfc_current_locus;
531 gfc_gobble_whitespace ();
533 /* Get the next char (first possible char of name) and see if
534 it's valid for C (either a letter or an underscore). */
535 c = gfc_next_char_literal (1);
537 /* If the user put nothing expect spaces between the quotes, it is valid
538 and simply means there is no name= specifier and the name is the fortran
539 symbol name, all lowercase. */
540 if (c == '"' || c == '\'')
543 gfc_current_locus = old_loc;
547 if (!ISALPHA (c) && c != '_')
549 gfc_error ("Invalid C name in NAME= specifier at %C");
553 /* Continue to read valid variable name characters. */
558 /* C does not define a maximum length of variable names, to my
559 knowledge, but the compiler typically places a limit on them.
560 For now, i'll use the same as the fortran limit for simplicity,
561 but this may need to be changed to a dynamic buffer that can
562 be realloc'ed here if necessary, or more likely, a larger
564 if (i > gfc_option.max_identifier_length)
566 gfc_error ("Name at %C is too long");
570 old_loc = gfc_current_locus;
572 /* Get next char; param means we're in a string. */
573 c = gfc_next_char_literal (1);
574 } while (ISALNUM (c) || c == '_');
577 gfc_current_locus = old_loc;
579 /* See if we stopped because of whitespace. */
582 gfc_gobble_whitespace ();
583 c = gfc_peek_char ();
584 if (c != '"' && c != '\'')
586 gfc_error ("Embedded space in NAME= specifier at %C");
591 /* If we stopped because we had an invalid character for a C name, report
592 that to the user by returning MATCH_NO. */
593 if (c != '"' && c != '\'')
595 gfc_error ("Invalid C name in NAME= specifier at %C");
603 /* Match a symbol on the input. Modifies the pointer to the symbol
604 pointer if successful. */
607 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
609 char buffer[GFC_MAX_SYMBOL_LEN + 1];
612 m = gfc_match_name (buffer);
617 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
618 ? MATCH_ERROR : MATCH_YES;
620 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
628 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
633 m = gfc_match_sym_tree (&st, host_assoc);
638 *matched_symbol = st->n.sym;
640 *matched_symbol = NULL;
643 *matched_symbol = NULL;
648 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
649 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
653 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
657 op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
659 if (op == INTRINSIC_NONE)
667 /* Match a loop control phrase:
669 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
671 If the final integer expression is not present, a constant unity
672 expression is returned. We don't return MATCH_ERROR until after
673 the equals sign is seen. */
676 gfc_match_iterator (gfc_iterator *iter, int init_flag)
678 char name[GFC_MAX_SYMBOL_LEN + 1];
679 gfc_expr *var, *e1, *e2, *e3;
683 /* Match the start of an iterator without affecting the symbol table. */
685 start = gfc_current_locus;
686 m = gfc_match (" %n =", name);
687 gfc_current_locus = start;
692 m = gfc_match_variable (&var, 0);
696 gfc_match_char ('=');
700 if (var->ref != NULL)
702 gfc_error ("Loop variable at %C cannot be a sub-component");
706 if (var->symtree->n.sym->attr.intent == INTENT_IN)
708 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
709 var->symtree->n.sym->name);
713 var->symtree->n.sym->attr.implied_index = 1;
715 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
718 if (m == MATCH_ERROR)
721 if (gfc_match_char (',') != MATCH_YES)
724 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
727 if (m == MATCH_ERROR)
730 if (gfc_match_char (',') != MATCH_YES)
732 e3 = gfc_int_expr (1);
736 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
737 if (m == MATCH_ERROR)
741 gfc_error ("Expected a step value in iterator at %C");
753 gfc_error ("Syntax error in iterator at %C");
764 /* Tries to match the next non-whitespace character on the input.
765 This subroutine does not return MATCH_ERROR. */
768 gfc_match_char (char c)
772 where = gfc_current_locus;
773 gfc_gobble_whitespace ();
775 if (gfc_next_char () == c)
778 gfc_current_locus = where;
783 /* General purpose matching subroutine. The target string is a
784 scanf-like format string in which spaces correspond to arbitrary
785 whitespace (including no whitespace), characters correspond to
786 themselves. The %-codes are:
788 %% Literal percent sign
789 %e Expression, pointer to a pointer is set
790 %s Symbol, pointer to the symbol is set
791 %n Name, character buffer is set to name
792 %t Matches end of statement.
793 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
794 %l Matches a statement label
795 %v Matches a variable expression (an lvalue)
796 % Matches a required space (in free form) and optional spaces. */
799 gfc_match (const char *target, ...)
801 gfc_st_label **label;
810 old_loc = gfc_current_locus;
811 va_start (argp, target);
821 gfc_gobble_whitespace ();
832 vp = va_arg (argp, void **);
833 n = gfc_match_expr ((gfc_expr **) vp);
844 vp = va_arg (argp, void **);
845 n = gfc_match_variable ((gfc_expr **) vp, 0);
856 vp = va_arg (argp, void **);
857 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
868 np = va_arg (argp, char *);
869 n = gfc_match_name (np);
880 label = va_arg (argp, gfc_st_label **);
881 n = gfc_match_st_label (label);
892 ip = va_arg (argp, int *);
893 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
904 if (gfc_match_eos () != MATCH_YES)
912 if (gfc_match_space () == MATCH_YES)
918 break; /* Fall through to character matcher. */
921 gfc_internal_error ("gfc_match(): Bad match code %c", c);
925 if (c == gfc_next_char ())
935 /* Clean up after a failed match. */
936 gfc_current_locus = old_loc;
937 va_start (argp, target);
940 for (; matches > 0; matches--)
950 /* Matches that don't have to be undone */
955 (void) va_arg (argp, void **);
960 vp = va_arg (argp, void **);
974 /*********************** Statement level matching **********************/
976 /* Matches the start of a program unit, which is the program keyword
977 followed by an obligatory symbol. */
980 gfc_match_program (void)
985 m = gfc_match ("% %s%t", &sym);
989 gfc_error ("Invalid form of PROGRAM statement at %C");
993 if (m == MATCH_ERROR)
996 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1005 /* Match a simple assignment statement. */
1008 gfc_match_assignment (void)
1010 gfc_expr *lvalue, *rvalue;
1014 old_loc = gfc_current_locus;
1017 m = gfc_match (" %v =", &lvalue);
1020 gfc_current_locus = old_loc;
1021 gfc_free_expr (lvalue);
1025 if (lvalue->symtree->n.sym->attr.protected
1026 && lvalue->symtree->n.sym->attr.use_assoc)
1028 gfc_current_locus = old_loc;
1029 gfc_free_expr (lvalue);
1030 gfc_error ("Setting value of PROTECTED variable at %C");
1035 m = gfc_match (" %e%t", &rvalue);
1038 gfc_current_locus = old_loc;
1039 gfc_free_expr (lvalue);
1040 gfc_free_expr (rvalue);
1044 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1046 new_st.op = EXEC_ASSIGN;
1047 new_st.expr = lvalue;
1048 new_st.expr2 = rvalue;
1050 gfc_check_do_variable (lvalue->symtree);
1056 /* Match a pointer assignment statement. */
1059 gfc_match_pointer_assignment (void)
1061 gfc_expr *lvalue, *rvalue;
1065 old_loc = gfc_current_locus;
1067 lvalue = rvalue = NULL;
1069 m = gfc_match (" %v =>", &lvalue);
1076 m = gfc_match (" %e%t", &rvalue);
1080 if (lvalue->symtree->n.sym->attr.protected
1081 && lvalue->symtree->n.sym->attr.use_assoc)
1083 gfc_error ("Assigning to a PROTECTED pointer at %C");
1088 new_st.op = EXEC_POINTER_ASSIGN;
1089 new_st.expr = lvalue;
1090 new_st.expr2 = rvalue;
1095 gfc_current_locus = old_loc;
1096 gfc_free_expr (lvalue);
1097 gfc_free_expr (rvalue);
1102 /* We try to match an easy arithmetic IF statement. This only happens
1103 when just after having encountered a simple IF statement. This code
1104 is really duplicate with parts of the gfc_match_if code, but this is
1108 match_arithmetic_if (void)
1110 gfc_st_label *l1, *l2, *l3;
1114 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1118 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1119 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1120 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1122 gfc_free_expr (expr);
1126 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF statement "
1127 "at %C") == FAILURE)
1130 new_st.op = EXEC_ARITHMETIC_IF;
1140 /* The IF statement is a bit of a pain. First of all, there are three
1141 forms of it, the simple IF, the IF that starts a block and the
1144 There is a problem with the simple IF and that is the fact that we
1145 only have a single level of undo information on symbols. What this
1146 means is for a simple IF, we must re-match the whole IF statement
1147 multiple times in order to guarantee that the symbol table ends up
1148 in the proper state. */
1150 static match match_simple_forall (void);
1151 static match match_simple_where (void);
1154 gfc_match_if (gfc_statement *if_type)
1157 gfc_st_label *l1, *l2, *l3;
1162 n = gfc_match_label ();
1163 if (n == MATCH_ERROR)
1166 old_loc = gfc_current_locus;
1168 m = gfc_match (" if ( %e", &expr);
1172 if (gfc_match_char (')') != MATCH_YES)
1174 gfc_error ("Syntax error in IF-expression at %C");
1175 gfc_free_expr (expr);
1179 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1185 gfc_error ("Block label not appropriate for arithmetic IF "
1187 gfc_free_expr (expr);
1191 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1192 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1193 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1195 gfc_free_expr (expr);
1199 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF "
1200 "statement at %C") == FAILURE)
1203 new_st.op = EXEC_ARITHMETIC_IF;
1209 *if_type = ST_ARITHMETIC_IF;
1213 if (gfc_match (" then%t") == MATCH_YES)
1215 new_st.op = EXEC_IF;
1217 *if_type = ST_IF_BLOCK;
1223 gfc_error ("Block label is not appropriate IF statement at %C");
1224 gfc_free_expr (expr);
1228 /* At this point the only thing left is a simple IF statement. At
1229 this point, n has to be MATCH_NO, so we don't have to worry about
1230 re-matching a block label. From what we've got so far, try
1231 matching an assignment. */
1233 *if_type = ST_SIMPLE_IF;
1235 m = gfc_match_assignment ();
1239 gfc_free_expr (expr);
1240 gfc_undo_symbols ();
1241 gfc_current_locus = old_loc;
1243 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1244 assignment was found. For MATCH_NO, continue to call the various
1246 if (m == MATCH_ERROR)
1249 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1251 m = gfc_match_pointer_assignment ();
1255 gfc_free_expr (expr);
1256 gfc_undo_symbols ();
1257 gfc_current_locus = old_loc;
1259 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1261 /* Look at the next keyword to see which matcher to call. Matching
1262 the keyword doesn't affect the symbol table, so we don't have to
1263 restore between tries. */
1265 #define match(string, subr, statement) \
1266 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1270 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1271 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1272 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1273 match ("call", gfc_match_call, ST_CALL)
1274 match ("close", gfc_match_close, ST_CLOSE)
1275 match ("continue", gfc_match_continue, ST_CONTINUE)
1276 match ("cycle", gfc_match_cycle, ST_CYCLE)
1277 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1278 match ("end file", gfc_match_endfile, ST_END_FILE)
1279 match ("exit", gfc_match_exit, ST_EXIT)
1280 match ("flush", gfc_match_flush, ST_FLUSH)
1281 match ("forall", match_simple_forall, ST_FORALL)
1282 match ("go to", gfc_match_goto, ST_GOTO)
1283 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1284 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1285 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1286 match ("open", gfc_match_open, ST_OPEN)
1287 match ("pause", gfc_match_pause, ST_NONE)
1288 match ("print", gfc_match_print, ST_WRITE)
1289 match ("read", gfc_match_read, ST_READ)
1290 match ("return", gfc_match_return, ST_RETURN)
1291 match ("rewind", gfc_match_rewind, ST_REWIND)
1292 match ("stop", gfc_match_stop, ST_STOP)
1293 match ("where", match_simple_where, ST_WHERE)
1294 match ("write", gfc_match_write, ST_WRITE)
1296 /* The gfc_match_assignment() above may have returned a MATCH_NO
1297 where the assignment was to a named constant. Check that
1298 special case here. */
1299 m = gfc_match_assignment ();
1302 gfc_error ("Cannot assign to a named constant at %C");
1303 gfc_free_expr (expr);
1304 gfc_undo_symbols ();
1305 gfc_current_locus = old_loc;
1309 /* All else has failed, so give up. See if any of the matchers has
1310 stored an error message of some sort. */
1311 if (gfc_error_check () == 0)
1312 gfc_error ("Unclassifiable statement in IF-clause at %C");
1314 gfc_free_expr (expr);
1319 gfc_error ("Syntax error in IF-clause at %C");
1322 gfc_free_expr (expr);
1326 /* At this point, we've matched the single IF and the action clause
1327 is in new_st. Rearrange things so that the IF statement appears
1330 p = gfc_get_code ();
1331 p->next = gfc_get_code ();
1333 p->next->loc = gfc_current_locus;
1338 gfc_clear_new_st ();
1340 new_st.op = EXEC_IF;
1349 /* Match an ELSE statement. */
1352 gfc_match_else (void)
1354 char name[GFC_MAX_SYMBOL_LEN + 1];
1356 if (gfc_match_eos () == MATCH_YES)
1359 if (gfc_match_name (name) != MATCH_YES
1360 || gfc_current_block () == NULL
1361 || gfc_match_eos () != MATCH_YES)
1363 gfc_error ("Unexpected junk after ELSE statement at %C");
1367 if (strcmp (name, gfc_current_block ()->name) != 0)
1369 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1370 name, gfc_current_block ()->name);
1378 /* Match an ELSE IF statement. */
1381 gfc_match_elseif (void)
1383 char name[GFC_MAX_SYMBOL_LEN + 1];
1387 m = gfc_match (" ( %e ) then", &expr);
1391 if (gfc_match_eos () == MATCH_YES)
1394 if (gfc_match_name (name) != MATCH_YES
1395 || gfc_current_block () == NULL
1396 || gfc_match_eos () != MATCH_YES)
1398 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1402 if (strcmp (name, gfc_current_block ()->name) != 0)
1404 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1405 name, gfc_current_block ()->name);
1410 new_st.op = EXEC_IF;
1415 gfc_free_expr (expr);
1420 /* Free a gfc_iterator structure. */
1423 gfc_free_iterator (gfc_iterator *iter, int flag)
1429 gfc_free_expr (iter->var);
1430 gfc_free_expr (iter->start);
1431 gfc_free_expr (iter->end);
1432 gfc_free_expr (iter->step);
1439 /* Match a DO statement. */
1444 gfc_iterator iter, *ip;
1446 gfc_st_label *label;
1449 old_loc = gfc_current_locus;
1452 iter.var = iter.start = iter.end = iter.step = NULL;
1454 m = gfc_match_label ();
1455 if (m == MATCH_ERROR)
1458 if (gfc_match (" do") != MATCH_YES)
1461 m = gfc_match_st_label (&label);
1462 if (m == MATCH_ERROR)
1465 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1467 if (gfc_match_eos () == MATCH_YES)
1469 iter.end = gfc_logical_expr (1, NULL);
1470 new_st.op = EXEC_DO_WHILE;
1474 /* Match an optional comma, if no comma is found, a space is obligatory. */
1475 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1478 /* See if we have a DO WHILE. */
1479 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1481 new_st.op = EXEC_DO_WHILE;
1485 /* The abortive DO WHILE may have done something to the symbol
1486 table, so we start over. */
1487 gfc_undo_symbols ();
1488 gfc_current_locus = old_loc;
1490 gfc_match_label (); /* This won't error. */
1491 gfc_match (" do "); /* This will work. */
1493 gfc_match_st_label (&label); /* Can't error out. */
1494 gfc_match_char (','); /* Optional comma. */
1496 m = gfc_match_iterator (&iter, 0);
1499 if (m == MATCH_ERROR)
1502 iter.var->symtree->n.sym->attr.implied_index = 0;
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)
2005 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
2007 gfc_error ("STAT variable '%s' of ALLOCATE statement at %C cannot "
2008 "be INTENT(IN)", stat->symtree->n.sym->name);
2012 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
2014 gfc_error ("Illegal STAT variable in ALLOCATE statement at %C "
2015 "for a PURE procedure");
2019 is_variable = false;
2020 if (stat->symtree->n.sym->attr.flavor == FL_VARIABLE)
2022 else if (stat->symtree->n.sym->attr.function
2023 && stat->symtree->n.sym->result == stat->symtree->n.sym
2024 && (gfc_current_ns->proc_name == stat->symtree->n.sym
2025 || (gfc_current_ns->parent
2026 && gfc_current_ns->parent->proc_name
2027 == stat->symtree->n.sym)))
2029 else if (gfc_current_ns->entries
2030 && stat->symtree->n.sym->result == stat->symtree->n.sym)
2033 for (el = gfc_current_ns->entries; el; el = el->next)
2034 if (el->sym == stat->symtree->n.sym)
2039 else if (gfc_current_ns->parent && gfc_current_ns->parent->entries
2040 && stat->symtree->n.sym->result == stat->symtree->n.sym)
2043 for (el = gfc_current_ns->parent->entries; el; el = el->next)
2044 if (el->sym == stat->symtree->n.sym)
2052 gfc_error ("STAT expression at %C must be a variable");
2056 gfc_check_do_variable(stat->symtree);
2059 if (gfc_match (" )%t") != MATCH_YES)
2062 new_st.op = EXEC_ALLOCATE;
2064 new_st.ext.alloc_list = head;
2069 gfc_syntax_error (ST_ALLOCATE);
2072 gfc_free_expr (stat);
2073 gfc_free_alloc_list (head);
2078 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
2079 a set of pointer assignments to intrinsic NULL(). */
2082 gfc_match_nullify (void)
2090 if (gfc_match_char ('(') != MATCH_YES)
2095 m = gfc_match_variable (&p, 0);
2096 if (m == MATCH_ERROR)
2101 if (gfc_check_do_variable (p->symtree))
2104 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
2106 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
2110 /* build ' => NULL() '. */
2111 e = gfc_get_expr ();
2112 e->where = gfc_current_locus;
2113 e->expr_type = EXPR_NULL;
2114 e->ts.type = BT_UNKNOWN;
2116 /* Chain to list. */
2121 tail->next = gfc_get_code ();
2125 tail->op = EXEC_POINTER_ASSIGN;
2129 if (gfc_match (" )%t") == MATCH_YES)
2131 if (gfc_match_char (',') != MATCH_YES)
2138 gfc_syntax_error (ST_NULLIFY);
2141 gfc_free_statements (new_st.next);
2146 /* Match a DEALLOCATE statement. */
2149 gfc_match_deallocate (void)
2151 gfc_alloc *head, *tail;
2158 if (gfc_match_char ('(') != MATCH_YES)
2164 head = tail = gfc_get_alloc ();
2167 tail->next = gfc_get_alloc ();
2171 m = gfc_match_variable (&tail->expr, 0);
2172 if (m == MATCH_ERROR)
2177 if (gfc_check_do_variable (tail->expr->symtree))
2181 && gfc_impure_variable (tail->expr->symtree->n.sym))
2183 gfc_error ("Illegal deallocate-expression in DEALLOCATE at %C "
2184 "for a PURE procedure");
2188 if (gfc_match_char (',') != MATCH_YES)
2191 m = gfc_match (" stat = %v", &stat);
2192 if (m == MATCH_ERROR)
2200 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
2202 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
2203 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
2207 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
2209 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
2210 "for a PURE procedure");
2214 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
2216 gfc_error ("STAT expression at %C must be a variable");
2220 gfc_check_do_variable(stat->symtree);
2223 if (gfc_match (" )%t") != MATCH_YES)
2226 new_st.op = EXEC_DEALLOCATE;
2228 new_st.ext.alloc_list = head;
2233 gfc_syntax_error (ST_DEALLOCATE);
2236 gfc_free_expr (stat);
2237 gfc_free_alloc_list (head);
2242 /* Match a RETURN statement. */
2245 gfc_match_return (void)
2249 gfc_compile_state s;
2253 if (gfc_match_eos () == MATCH_YES)
2256 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2258 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2263 if (gfc_current_form == FORM_FREE)
2265 /* The following are valid, so we can't require a blank after the
2269 c = gfc_peek_char ();
2270 if (ISALPHA (c) || ISDIGIT (c))
2274 m = gfc_match (" %e%t", &e);
2277 if (m == MATCH_ERROR)
2280 gfc_syntax_error (ST_RETURN);
2287 gfc_enclosing_unit (&s);
2288 if (s == COMP_PROGRAM
2289 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2290 "main program at %C") == FAILURE)
2293 new_st.op = EXEC_RETURN;
2300 /* Match a CALL statement. The tricky part here are possible
2301 alternate return specifiers. We handle these by having all
2302 "subroutines" actually return an integer via a register that gives
2303 the return number. If the call specifies alternate returns, we
2304 generate code for a SELECT statement whose case clauses contain
2305 GOTOs to the various labels. */
2308 gfc_match_call (void)
2310 char name[GFC_MAX_SYMBOL_LEN + 1];
2311 gfc_actual_arglist *a, *arglist;
2321 m = gfc_match ("% %n", name);
2327 if (gfc_get_ha_sym_tree (name, &st))
2332 /* If it does not seem to be callable... */
2333 if (!sym->attr.generic
2334 && !sym->attr.subroutine)
2336 /* ...create a symbol in this scope... */
2337 if (sym->ns != gfc_current_ns
2338 && gfc_get_sym_tree (name, NULL, &st) == 1)
2341 if (sym != st->n.sym)
2344 /* ...and then to try to make the symbol into a subroutine. */
2345 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2349 gfc_set_sym_referenced (sym);
2351 if (gfc_match_eos () != MATCH_YES)
2353 m = gfc_match_actual_arglist (1, &arglist);
2356 if (m == MATCH_ERROR)
2359 if (gfc_match_eos () != MATCH_YES)
2363 /* If any alternate return labels were found, construct a SELECT
2364 statement that will jump to the right place. */
2367 for (a = arglist; a; a = a->next)
2368 if (a->expr == NULL)
2373 gfc_symtree *select_st;
2374 gfc_symbol *select_sym;
2375 char name[GFC_MAX_SYMBOL_LEN + 1];
2377 new_st.next = c = gfc_get_code ();
2378 c->op = EXEC_SELECT;
2379 sprintf (name, "_result_%s", sym->name);
2380 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
2382 select_sym = select_st->n.sym;
2383 select_sym->ts.type = BT_INTEGER;
2384 select_sym->ts.kind = gfc_default_integer_kind;
2385 gfc_set_sym_referenced (select_sym);
2386 c->expr = gfc_get_expr ();
2387 c->expr->expr_type = EXPR_VARIABLE;
2388 c->expr->symtree = select_st;
2389 c->expr->ts = select_sym->ts;
2390 c->expr->where = gfc_current_locus;
2393 for (a = arglist; a; a = a->next)
2395 if (a->expr != NULL)
2398 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2403 c->block = gfc_get_code ();
2405 c->op = EXEC_SELECT;
2407 new_case = gfc_get_case ();
2408 new_case->high = new_case->low = gfc_int_expr (i);
2409 c->ext.case_list = new_case;
2411 c->next = gfc_get_code ();
2412 c->next->op = EXEC_GOTO;
2413 c->next->label = a->label;
2417 new_st.op = EXEC_CALL;
2418 new_st.symtree = st;
2419 new_st.ext.actual = arglist;
2424 gfc_syntax_error (ST_CALL);
2427 gfc_free_actual_arglist (arglist);
2432 /* Given a name, return a pointer to the common head structure,
2433 creating it if it does not exist. If FROM_MODULE is nonzero, we
2434 mangle the name so that it doesn't interfere with commons defined
2435 in the using namespace.
2436 TODO: Add to global symbol tree. */
2439 gfc_get_common (const char *name, int from_module)
2442 static int serial = 0;
2443 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
2447 /* A use associated common block is only needed to correctly layout
2448 the variables it contains. */
2449 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2450 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2454 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2457 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2460 if (st->n.common == NULL)
2462 st->n.common = gfc_get_common_head ();
2463 st->n.common->where = gfc_current_locus;
2464 strcpy (st->n.common->name, name);
2467 return st->n.common;
2471 /* Match a common block name. */
2473 match match_common_name (char *name)
2477 if (gfc_match_char ('/') == MATCH_NO)
2483 if (gfc_match_char ('/') == MATCH_YES)
2489 m = gfc_match_name (name);
2491 if (m == MATCH_ERROR)
2493 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2496 gfc_error ("Syntax error in common block name at %C");
2501 /* Match a COMMON statement. */
2504 gfc_match_common (void)
2506 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2507 char name[GFC_MAX_SYMBOL_LEN + 1];
2514 old_blank_common = gfc_current_ns->blank_common.head;
2515 if (old_blank_common)
2517 while (old_blank_common->common_next)
2518 old_blank_common = old_blank_common->common_next;
2525 m = match_common_name (name);
2526 if (m == MATCH_ERROR)
2529 gsym = gfc_get_gsymbol (name);
2530 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2532 gfc_error ("Symbol '%s' at %C is already an external symbol that "
2533 "is not COMMON", name);
2537 if (gsym->type == GSYM_UNKNOWN)
2539 gsym->type = GSYM_COMMON;
2540 gsym->where = gfc_current_locus;
2546 if (name[0] == '\0')
2548 if (gfc_current_ns->is_block_data)
2550 gfc_warning ("BLOCK DATA unit cannot contain blank COMMON "
2553 t = &gfc_current_ns->blank_common;
2554 if (t->head == NULL)
2555 t->where = gfc_current_locus;
2559 t = gfc_get_common (name, 0);
2568 while (tail->common_next)
2569 tail = tail->common_next;
2572 /* Grab the list of symbols. */
2575 m = gfc_match_symbol (&sym, 0);
2576 if (m == MATCH_ERROR)
2581 /* Store a ref to the common block for error checking. */
2582 sym->common_block = t;
2584 /* See if we know the current common block is bind(c), and if
2585 so, then see if we can check if the symbol is (which it'll
2586 need to be). This can happen if the bind(c) attr stmt was
2587 applied to the common block, and the variable(s) already
2588 defined, before declaring the common block. */
2589 if (t->is_bind_c == 1)
2591 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
2593 /* If we find an error, just print it and continue,
2594 cause it's just semantic, and we can see if there
2596 gfc_error_now ("Variable '%s' at %L in common block '%s' "
2597 "at %C must be declared with a C "
2598 "interoperable kind since common block "
2600 sym->name, &(sym->declared_at), t->name,
2604 if (sym->attr.is_bind_c == 1)
2605 gfc_error_now ("Variable '%s' in common block "
2606 "'%s' at %C can not be bind(c) since "
2607 "it is not global", sym->name, t->name);
2610 if (sym->attr.in_common)
2612 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2617 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2620 if (sym->value != NULL && sym->value->expr_type != EXPR_NULL
2621 && (name[0] == '\0' || !sym->attr.data))
2623 if (name[0] == '\0')
2624 gfc_error ("Previously initialized symbol '%s' in "
2625 "blank COMMON block at %C", sym->name);
2627 gfc_error ("Previously initialized symbol '%s' in "
2628 "COMMON block '%s' at %C", sym->name, name);
2632 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2635 /* Derived type names must have the SEQUENCE attribute. */
2636 if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2638 gfc_error ("Derived type variable in COMMON at %C does not "
2639 "have the SEQUENCE attribute");
2644 tail->common_next = sym;
2650 /* Deal with an optional array specification after the
2652 m = gfc_match_array_spec (&as);
2653 if (m == MATCH_ERROR)
2658 if (as->type != AS_EXPLICIT)
2660 gfc_error ("Array specification for symbol '%s' in COMMON "
2661 "at %C must be explicit", sym->name);
2665 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2668 if (sym->attr.pointer)
2670 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
2671 "POINTER array", sym->name);
2680 sym->common_head = t;
2682 /* Check to see if the symbol is already in an equivalence group.
2683 If it is, set the other members as being in common. */
2684 if (sym->attr.in_equivalence)
2686 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2688 for (e2 = e1; e2; e2 = e2->eq)
2689 if (e2->expr->symtree->n.sym == sym)
2696 for (e2 = e1; e2; e2 = e2->eq)
2698 other = e2->expr->symtree->n.sym;
2699 if (other->common_head
2700 && other->common_head != sym->common_head)
2702 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2703 "%C is being indirectly equivalenced to "
2704 "another COMMON block '%s'",
2705 sym->name, sym->common_head->name,
2706 other->common_head->name);
2709 other->attr.in_common = 1;
2710 other->common_head = t;
2716 gfc_gobble_whitespace ();
2717 if (gfc_match_eos () == MATCH_YES)
2719 if (gfc_peek_char () == '/')
2721 if (gfc_match_char (',') != MATCH_YES)
2723 gfc_gobble_whitespace ();
2724 if (gfc_peek_char () == '/')
2733 gfc_syntax_error (ST_COMMON);
2736 if (old_blank_common)
2737 old_blank_common->common_next = NULL;
2739 gfc_current_ns->blank_common.head = NULL;
2740 gfc_free_array_spec (as);
2745 /* Match a BLOCK DATA program unit. */
2748 gfc_match_block_data (void)
2750 char name[GFC_MAX_SYMBOL_LEN + 1];
2754 if (gfc_match_eos () == MATCH_YES)
2756 gfc_new_block = NULL;
2760 m = gfc_match ("% %n%t", name);
2764 if (gfc_get_symbol (name, NULL, &sym))
2767 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2770 gfc_new_block = sym;
2776 /* Free a namelist structure. */
2779 gfc_free_namelist (gfc_namelist *name)
2783 for (; name; name = n)
2791 /* Match a NAMELIST statement. */
2794 gfc_match_namelist (void)
2796 gfc_symbol *group_name, *sym;
2800 m = gfc_match (" / %s /", &group_name);
2803 if (m == MATCH_ERROR)
2808 if (group_name->ts.type != BT_UNKNOWN)
2810 gfc_error ("Namelist group name '%s' at %C already has a basic "
2811 "type of %s", group_name->name,
2812 gfc_typename (&group_name->ts));
2816 if (group_name->attr.flavor == FL_NAMELIST
2817 && group_name->attr.use_assoc
2818 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2819 "at %C already is USE associated and can"
2820 "not be respecified.", group_name->name)
2824 if (group_name->attr.flavor != FL_NAMELIST
2825 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2826 group_name->name, NULL) == FAILURE)
2831 m = gfc_match_symbol (&sym, 1);
2834 if (m == MATCH_ERROR)
2837 if (sym->attr.in_namelist == 0
2838 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2841 /* Use gfc_error_check here, rather than goto error, so that
2842 these are the only errors for the next two lines. */
2843 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2845 gfc_error ("Assumed size array '%s' in namelist '%s' at "
2846 "%C is not allowed", sym->name, group_name->name);
2850 if (sym->ts.type == BT_CHARACTER && sym->ts.cl->length == NULL)
2852 gfc_error ("Assumed character length '%s' in namelist '%s' at "
2853 "%C is not allowed", sym->name, group_name->name);
2857 nl = gfc_get_namelist ();
2861 if (group_name->namelist == NULL)
2862 group_name->namelist = group_name->namelist_tail = nl;
2865 group_name->namelist_tail->next = nl;
2866 group_name->namelist_tail = nl;
2869 if (gfc_match_eos () == MATCH_YES)
2872 m = gfc_match_char (',');
2874 if (gfc_match_char ('/') == MATCH_YES)
2876 m2 = gfc_match (" %s /", &group_name);
2877 if (m2 == MATCH_YES)
2879 if (m2 == MATCH_ERROR)
2893 gfc_syntax_error (ST_NAMELIST);
2900 /* Match a MODULE statement. */
2903 gfc_match_module (void)
2907 m = gfc_match (" %s%t", &gfc_new_block);
2911 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2912 gfc_new_block->name, NULL) == FAILURE)
2919 /* Free equivalence sets and lists. Recursively is the easiest way to
2923 gfc_free_equiv (gfc_equiv *eq)
2928 gfc_free_equiv (eq->eq);
2929 gfc_free_equiv (eq->next);
2930 gfc_free_expr (eq->expr);
2935 /* Match an EQUIVALENCE statement. */
2938 gfc_match_equivalence (void)
2940 gfc_equiv *eq, *set, *tail;
2944 gfc_common_head *common_head = NULL;
2952 eq = gfc_get_equiv ();
2956 eq->next = gfc_current_ns->equiv;
2957 gfc_current_ns->equiv = eq;
2959 if (gfc_match_char ('(') != MATCH_YES)
2963 common_flag = FALSE;
2968 m = gfc_match_equiv_variable (&set->expr);
2969 if (m == MATCH_ERROR)
2974 /* count the number of objects. */
2977 if (gfc_match_char ('%') == MATCH_YES)
2979 gfc_error ("Derived type component %C is not a "
2980 "permitted EQUIVALENCE member");
2984 for (ref = set->expr->ref; ref; ref = ref->next)
2985 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2987 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
2988 "be an array section");
2992 sym = set->expr->symtree->n.sym;
2994 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
2997 if (sym->attr.in_common)
3000 common_head = sym->common_head;
3003 if (gfc_match_char (')') == MATCH_YES)
3006 if (gfc_match_char (',') != MATCH_YES)
3009 set->eq = gfc_get_equiv ();
3015 gfc_error ("EQUIVALENCE at %C requires two or more objects");
3019 /* If one of the members of an equivalence is in common, then
3020 mark them all as being in common. Before doing this, check
3021 that members of the equivalence group are not in different
3024 for (set = eq; set; set = set->eq)
3026 sym = set->expr->symtree->n.sym;
3027 if (sym->common_head && sym->common_head != common_head)
3029 gfc_error ("Attempt to indirectly overlap COMMON "
3030 "blocks %s and %s by EQUIVALENCE at %C",
3031 sym->common_head->name, common_head->name);
3034 sym->attr.in_common = 1;
3035 sym->common_head = common_head;
3038 if (gfc_match_eos () == MATCH_YES)
3040 if (gfc_match_char (',') != MATCH_YES)
3047 gfc_syntax_error (ST_EQUIVALENCE);
3053 gfc_free_equiv (gfc_current_ns->equiv);
3054 gfc_current_ns->equiv = eq;
3060 /* Check that a statement function is not recursive. This is done by looking
3061 for the statement function symbol(sym) by looking recursively through its
3062 expression(e). If a reference to sym is found, true is returned.
3063 12.5.4 requires that any variable of function that is implicitly typed
3064 shall have that type confirmed by any subsequent type declaration. The
3065 implicit typing is conveniently done here. */
3068 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
3070 gfc_actual_arglist *arg;
3077 switch (e->expr_type)
3080 for (arg = e->value.function.actual; arg; arg = arg->next)
3082 if (sym->name == arg->name || recursive_stmt_fcn (arg->expr, sym))
3086 if (e->symtree == NULL)
3089 /* Check the name before testing for nested recursion! */
3090 if (sym->name == e->symtree->n.sym->name)
3093 /* Catch recursion via other statement functions. */
3094 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
3095 && e->symtree->n.sym->value
3096 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
3099 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3100 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3105 if (e->symtree && sym->name == e->symtree->n.sym->name)
3108 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3109 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3113 if (recursive_stmt_fcn (e->value.op.op1, sym)
3114 || recursive_stmt_fcn (e->value.op.op2, sym))
3122 /* Component references do not need to be checked. */
3125 for (ref = e->ref; ref; ref = ref->next)
3130 for (i = 0; i < ref->u.ar.dimen; i++)
3132 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
3133 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
3134 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
3140 if (recursive_stmt_fcn (ref->u.ss.start, sym)
3141 || recursive_stmt_fcn (ref->u.ss.end, sym))
3155 /* Match a statement function declaration. It is so easy to match
3156 non-statement function statements with a MATCH_ERROR as opposed to
3157 MATCH_NO that we suppress error message in most cases. */
3160 gfc_match_st_function (void)
3162 gfc_error_buf old_error;
3167 m = gfc_match_symbol (&sym, 0);
3171 gfc_push_error (&old_error);
3173 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
3174 sym->name, NULL) == FAILURE)
3177 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
3180 m = gfc_match (" = %e%t", &expr);
3184 gfc_free_error (&old_error);
3185 if (m == MATCH_ERROR)
3188 if (recursive_stmt_fcn (expr, sym))
3190 gfc_error ("Statement function at %L is recursive", &expr->where);
3199 gfc_pop_error (&old_error);
3204 /***************** SELECT CASE subroutines ******************/
3206 /* Free a single case structure. */
3209 free_case (gfc_case *p)
3211 if (p->low == p->high)
3213 gfc_free_expr (p->low);
3214 gfc_free_expr (p->high);
3219 /* Free a list of case structures. */
3222 gfc_free_case_list (gfc_case *p)
3234 /* Match a single case selector. */
3237 match_case_selector (gfc_case **cp)
3242 c = gfc_get_case ();
3243 c->where = gfc_current_locus;
3245 if (gfc_match_char (':') == MATCH_YES)
3247 m = gfc_match_init_expr (&c->high);
3250 if (m == MATCH_ERROR)
3255 m = gfc_match_init_expr (&c->low);
3256 if (m == MATCH_ERROR)
3261 /* If we're not looking at a ':' now, make a range out of a single
3262 target. Else get the upper bound for the case range. */
3263 if (gfc_match_char (':') != MATCH_YES)
3267 m = gfc_match_init_expr (&c->high);
3268 if (m == MATCH_ERROR)
3270 /* MATCH_NO is fine. It's OK if nothing is there! */
3278 gfc_error ("Expected initialization expression in CASE at %C");
3286 /* Match the end of a case statement. */
3289 match_case_eos (void)
3291 char name[GFC_MAX_SYMBOL_LEN + 1];
3294 if (gfc_match_eos () == MATCH_YES)
3297 /* If the case construct doesn't have a case-construct-name, we
3298 should have matched the EOS. */
3299 if (!gfc_current_block ())
3301 gfc_error ("Expected the name of the SELECT CASE construct at %C");
3305 gfc_gobble_whitespace ();
3307 m = gfc_match_name (name);
3311 if (strcmp (name, gfc_current_block ()->name) != 0)
3313 gfc_error ("Expected case name of '%s' at %C",
3314 gfc_current_block ()->name);
3318 return gfc_match_eos ();
3322 /* Match a SELECT statement. */
3325 gfc_match_select (void)
3330 m = gfc_match_label ();
3331 if (m == MATCH_ERROR)
3334 m = gfc_match (" select case ( %e )%t", &expr);
3338 new_st.op = EXEC_SELECT;
3345 /* Match a CASE statement. */
3348 gfc_match_case (void)
3350 gfc_case *c, *head, *tail;
3355 if (gfc_current_state () != COMP_SELECT)
3357 gfc_error ("Unexpected CASE statement at %C");
3361 if (gfc_match ("% default") == MATCH_YES)
3363 m = match_case_eos ();
3366 if (m == MATCH_ERROR)
3369 new_st.op = EXEC_SELECT;
3370 c = gfc_get_case ();
3371 c->where = gfc_current_locus;
3372 new_st.ext.case_list = c;
3376 if (gfc_match_char ('(') != MATCH_YES)
3381 if (match_case_selector (&c) == MATCH_ERROR)
3391 if (gfc_match_char (')') == MATCH_YES)
3393 if (gfc_match_char (',') != MATCH_YES)
3397 m = match_case_eos ();
3400 if (m == MATCH_ERROR)
3403 new_st.op = EXEC_SELECT;
3404 new_st.ext.case_list = head;
3409 gfc_error ("Syntax error in CASE-specification at %C");
3412 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3416 /********************* WHERE subroutines ********************/
3418 /* Match the rest of a simple WHERE statement that follows an IF statement.
3422 match_simple_where (void)
3428 m = gfc_match (" ( %e )", &expr);
3432 m = gfc_match_assignment ();
3435 if (m == MATCH_ERROR)
3438 if (gfc_match_eos () != MATCH_YES)
3441 c = gfc_get_code ();
3445 c->next = gfc_get_code ();
3448 gfc_clear_new_st ();
3450 new_st.op = EXEC_WHERE;
3456 gfc_syntax_error (ST_WHERE);
3459 gfc_free_expr (expr);
3464 /* Match a WHERE statement. */
3467 gfc_match_where (gfc_statement *st)
3473 m0 = gfc_match_label ();
3474 if (m0 == MATCH_ERROR)
3477 m = gfc_match (" where ( %e )", &expr);
3481 if (gfc_match_eos () == MATCH_YES)
3483 *st = ST_WHERE_BLOCK;
3484 new_st.op = EXEC_WHERE;
3489 m = gfc_match_assignment ();
3491 gfc_syntax_error (ST_WHERE);
3495 gfc_free_expr (expr);
3499 /* We've got a simple WHERE statement. */
3501 c = gfc_get_code ();
3505 c->next = gfc_get_code ();
3508 gfc_clear_new_st ();
3510 new_st.op = EXEC_WHERE;
3517 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3518 new_st if successful. */
3521 gfc_match_elsewhere (void)
3523 char name[GFC_MAX_SYMBOL_LEN + 1];
3527 if (gfc_current_state () != COMP_WHERE)
3529 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3535 if (gfc_match_char ('(') == MATCH_YES)
3537 m = gfc_match_expr (&expr);
3540 if (m == MATCH_ERROR)
3543 if (gfc_match_char (')') != MATCH_YES)
3547 if (gfc_match_eos () != MATCH_YES)
3549 /* Only makes sense if we have a where-construct-name. */
3550 if (!gfc_current_block ())
3555 /* Better be a name at this point. */
3556 m = gfc_match_name (name);
3559 if (m == MATCH_ERROR)
3562 if (gfc_match_eos () != MATCH_YES)
3565 if (strcmp (name, gfc_current_block ()->name) != 0)
3567 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3568 name, gfc_current_block ()->name);
3573 new_st.op = EXEC_WHERE;
3578 gfc_syntax_error (ST_ELSEWHERE);
3581 gfc_free_expr (expr);
3586 /******************** FORALL subroutines ********************/
3588 /* Free a list of FORALL iterators. */
3591 gfc_free_forall_iterator (gfc_forall_iterator *iter)
3593 gfc_forall_iterator *next;
3598 gfc_free_expr (iter->var);
3599 gfc_free_expr (iter->start);
3600 gfc_free_expr (iter->end);
3601 gfc_free_expr (iter->stride);
3608 /* Match an iterator as part of a FORALL statement. The format is:
3610 <var> = <start>:<end>[:<stride>]
3612 On MATCH_NO, the caller tests for the possibility that there is a
3613 scalar mask expression. */
3616 match_forall_iterator (gfc_forall_iterator **result)
3618 gfc_forall_iterator *iter;
3622 where = gfc_current_locus;
3623 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3625 m = gfc_match_expr (&iter->var);
3629 if (gfc_match_char ('=') != MATCH_YES
3630 || iter->var->expr_type != EXPR_VARIABLE)
3636 m = gfc_match_expr (&iter->start);
3640 if (gfc_match_char (':') != MATCH_YES)
3643 m = gfc_match_expr (&iter->end);
3646 if (m == MATCH_ERROR)
3649 if (gfc_match_char (':') == MATCH_NO)
3650 iter->stride = gfc_int_expr (1);
3653 m = gfc_match_expr (&iter->stride);
3656 if (m == MATCH_ERROR)
3660 /* Mark the iteration variable's symbol as used as a FORALL index. */
3661 iter->var->symtree->n.sym->forall_index = true;
3667 gfc_error ("Syntax error in FORALL iterator at %C");
3672 gfc_current_locus = where;
3673 gfc_free_forall_iterator (iter);
3678 /* Match the header of a FORALL statement. */
3681 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
3683 gfc_forall_iterator *head, *tail, *new;
3687 gfc_gobble_whitespace ();
3692 if (gfc_match_char ('(') != MATCH_YES)
3695 m = match_forall_iterator (&new);
3696 if (m == MATCH_ERROR)
3705 if (gfc_match_char (',') != MATCH_YES)
3708 m = match_forall_iterator (&new);
3709 if (m == MATCH_ERROR)
3719 /* Have to have a mask expression. */
3721 m = gfc_match_expr (&msk);
3724 if (m == MATCH_ERROR)
3730 if (gfc_match_char (')') == MATCH_NO)
3738 gfc_syntax_error (ST_FORALL);
3741 gfc_free_expr (msk);
3742 gfc_free_forall_iterator (head);
3747 /* Match the rest of a simple FORALL statement that follows an
3751 match_simple_forall (void)
3753 gfc_forall_iterator *head;
3762 m = match_forall_header (&head, &mask);
3769 m = gfc_match_assignment ();
3771 if (m == MATCH_ERROR)
3775 m = gfc_match_pointer_assignment ();
3776 if (m == MATCH_ERROR)
3782 c = gfc_get_code ();
3784 c->loc = gfc_current_locus;
3786 if (gfc_match_eos () != MATCH_YES)
3789 gfc_clear_new_st ();
3790 new_st.op = EXEC_FORALL;
3792 new_st.ext.forall_iterator = head;
3793 new_st.block = gfc_get_code ();
3795 new_st.block->op = EXEC_FORALL;
3796 new_st.block->next = c;
3801 gfc_syntax_error (ST_FORALL);
3804 gfc_free_forall_iterator (head);
3805 gfc_free_expr (mask);
3811 /* Match a FORALL statement. */
3814 gfc_match_forall (gfc_statement *st)
3816 gfc_forall_iterator *head;
3825 m0 = gfc_match_label ();
3826 if (m0 == MATCH_ERROR)
3829 m = gfc_match (" forall");
3833 m = match_forall_header (&head, &mask);
3834 if (m == MATCH_ERROR)
3839 if (gfc_match_eos () == MATCH_YES)
3841 *st = ST_FORALL_BLOCK;
3842 new_st.op = EXEC_FORALL;
3844 new_st.ext.forall_iterator = head;
3848 m = gfc_match_assignment ();
3849 if (m == MATCH_ERROR)
3853 m = gfc_match_pointer_assignment ();
3854 if (m == MATCH_ERROR)
3860 c = gfc_get_code ();
3862 c->loc = gfc_current_locus;
3864 gfc_clear_new_st ();
3865 new_st.op = EXEC_FORALL;
3867 new_st.ext.forall_iterator = head;
3868 new_st.block = gfc_get_code ();
3869 new_st.block->op = EXEC_FORALL;
3870 new_st.block->next = c;
3876 gfc_syntax_error (ST_FORALL);
3879 gfc_free_forall_iterator (head);
3880 gfc_free_expr (mask);
3881 gfc_free_statements (c);