1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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/>. */
30 /* For debugging and diagnostic purposes. Return the textual representation
31 of the intrinsic operator OP. */
33 gfc_op2string (gfc_intrinsic_op op)
41 case INTRINSIC_UMINUS:
47 case INTRINSIC_CONCAT:
51 case INTRINSIC_DIVIDE:
90 case INTRINSIC_ASSIGN:
93 case INTRINSIC_PARENTHESES:
100 gfc_internal_error ("gfc_op2string(): Bad code");
105 /******************** Generic matching subroutines ************************/
107 /* This function scans the current statement counting the opened and closed
108 parenthesis to make sure they are balanced. */
111 gfc_match_parens (void)
113 locus old_loc, where;
114 int c, count, instring;
117 old_loc = gfc_current_locus;
124 c = gfc_next_char_literal (instring);
127 if (quote == ' ' && ((c == '\'') || (c == '"')))
133 if (quote != ' ' && c == quote)
140 if (c == '(' && quote == ' ')
143 where = gfc_current_locus;
145 if (c == ')' && quote == ' ')
148 where = gfc_current_locus;
152 gfc_current_locus = old_loc;
156 gfc_error ("Missing ')' in statement before %L", &where);
161 gfc_error ("Missing '(' in statement before %L", &where);
169 /* See if the next character is a special character that has
170 escaped by a \ via the -fbackslash option. */
173 gfc_match_special_char (int *c)
180 switch (gfc_next_char_literal (1))
210 /* Unknown backslash codes are simply not expanded. */
219 /* In free form, match at least one space. Always matches in fixed
223 gfc_match_space (void)
228 if (gfc_current_form == FORM_FIXED)
231 old_loc = gfc_current_locus;
233 c = gfc_next_char ();
234 if (!gfc_is_whitespace (c))
236 gfc_current_locus = old_loc;
240 gfc_gobble_whitespace ();
246 /* Match an end of statement. End of statement is optional
247 whitespace, followed by a ';' or '\n' or comment '!'. If a
248 semicolon is found, we continue to eat whitespace and semicolons. */
260 old_loc = gfc_current_locus;
261 gfc_gobble_whitespace ();
263 c = gfc_next_char ();
269 c = gfc_next_char ();
286 gfc_current_locus = old_loc;
287 return (flag) ? MATCH_YES : MATCH_NO;
291 /* Match a literal integer on the input, setting the value on
292 MATCH_YES. Literal ints occur in kind-parameters as well as
293 old-style character length specifications. If cnt is non-NULL it
294 will be set to the number of digits. */
297 gfc_match_small_literal_int (int *value, int *cnt)
303 old_loc = gfc_current_locus;
305 gfc_gobble_whitespace ();
306 c = gfc_next_char ();
312 gfc_current_locus = old_loc;
321 old_loc = gfc_current_locus;
322 c = gfc_next_char ();
327 i = 10 * i + c - '0';
332 gfc_error ("Integer too large at %C");
337 gfc_current_locus = old_loc;
346 /* Match a small, constant integer expression, like in a kind
347 statement. On MATCH_YES, 'value' is set. */
350 gfc_match_small_int (int *value)
357 m = gfc_match_expr (&expr);
361 p = gfc_extract_int (expr, &i);
362 gfc_free_expr (expr);
375 /* This function is the same as the gfc_match_small_int, except that
376 we're keeping the pointer to the expr. This function could just be
377 removed and the previously mentioned one modified, though all calls
378 to it would have to be modified then (and there were a number of
379 them). Return MATCH_ERROR if fail to extract the int; otherwise,
380 return the result of gfc_match_expr(). The expr (if any) that was
381 matched is returned in the parameter expr. */
384 gfc_match_small_int_expr (int *value, gfc_expr **expr)
390 m = gfc_match_expr (expr);
394 p = gfc_extract_int (*expr, &i);
407 /* Matches a statement label. Uses gfc_match_small_literal_int() to
408 do most of the work. */
411 gfc_match_st_label (gfc_st_label **label)
417 old_loc = gfc_current_locus;
419 m = gfc_match_small_literal_int (&i, &cnt);
425 gfc_error ("Too many digits in statement label at %C");
431 gfc_error ("Statement label at %C is zero");
435 *label = gfc_get_st_label (i);
440 gfc_current_locus = old_loc;
445 /* Match and validate a label associated with a named IF, DO or SELECT
446 statement. If the symbol does not have the label attribute, we add
447 it. We also make sure the symbol does not refer to another
448 (active) block. A matched label is pointed to by gfc_new_block. */
451 gfc_match_label (void)
453 char name[GFC_MAX_SYMBOL_LEN + 1];
456 gfc_new_block = NULL;
458 m = gfc_match (" %n :", name);
462 if (gfc_get_symbol (name, NULL, &gfc_new_block))
464 gfc_error ("Label name '%s' at %C is ambiguous", name);
468 if (gfc_new_block->attr.flavor == FL_LABEL)
470 gfc_error ("Duplicate construct label '%s' at %C", name);
474 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
475 gfc_new_block->name, NULL) == FAILURE)
482 /* See if the current input looks like a name of some sort. Modifies
483 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
484 Note that options.c restricts max_identifier_length to not more
485 than GFC_MAX_SYMBOL_LEN. */
488 gfc_match_name (char *buffer)
493 old_loc = gfc_current_locus;
494 gfc_gobble_whitespace ();
496 c = gfc_next_char ();
497 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
499 if (gfc_error_flag_test() == 0 && c != '(')
500 gfc_error ("Invalid character in name at %C");
501 gfc_current_locus = old_loc;
511 if (i > gfc_option.max_identifier_length)
513 gfc_error ("Name at %C is too long");
517 old_loc = gfc_current_locus;
518 c = gfc_next_char ();
520 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
522 if (c == '$' && !gfc_option.flag_dollar_ok)
524 gfc_error ("Invalid character '$' at %C. Use -fdollar-ok to allow it as an extension");
530 gfc_current_locus = old_loc;
536 /* Match a valid name for C, which is almost the same as for Fortran,
537 except that you can start with an underscore, etc.. It could have
538 been done by modifying the gfc_match_name, but this way other
539 things C allows can be added, such as no limits on the length.
540 Right now, the length is limited to the same thing as Fortran..
541 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
542 input characters from being automatically lower cased, since C is
543 case sensitive. The parameter, buffer, is used to return the name
544 that is matched. Return MATCH_ERROR if the name is too long
545 (though this is a self-imposed limit), MATCH_NO if what we're
546 seeing isn't a name, and MATCH_YES if we successfully match a C
550 gfc_match_name_C (char *buffer)
556 old_loc = gfc_current_locus;
557 gfc_gobble_whitespace ();
559 /* Get the next char (first possible char of name) and see if
560 it's valid for C (either a letter or an underscore). */
561 c = gfc_next_char_literal (1);
563 /* If the user put nothing expect spaces between the quotes, it is valid
564 and simply means there is no name= specifier and the name is the fortran
565 symbol name, all lowercase. */
566 if (c == '"' || c == '\'')
569 gfc_current_locus = old_loc;
573 if (!ISALPHA (c) && c != '_')
575 gfc_error ("Invalid C name in NAME= specifier at %C");
579 /* Continue to read valid variable name characters. */
584 /* C does not define a maximum length of variable names, to my
585 knowledge, but the compiler typically places a limit on them.
586 For now, i'll use the same as the fortran limit for simplicity,
587 but this may need to be changed to a dynamic buffer that can
588 be realloc'ed here if necessary, or more likely, a larger
590 if (i > gfc_option.max_identifier_length)
592 gfc_error ("Name at %C is too long");
596 old_loc = gfc_current_locus;
598 /* Get next char; param means we're in a string. */
599 c = gfc_next_char_literal (1);
600 } while (ISALNUM (c) || c == '_');
603 gfc_current_locus = old_loc;
605 /* See if we stopped because of whitespace. */
608 gfc_gobble_whitespace ();
609 c = gfc_peek_char ();
610 if (c != '"' && c != '\'')
612 gfc_error ("Embedded space in NAME= specifier at %C");
617 /* If we stopped because we had an invalid character for a C name, report
618 that to the user by returning MATCH_NO. */
619 if (c != '"' && c != '\'')
621 gfc_error ("Invalid C name in NAME= specifier at %C");
629 /* Match a symbol on the input. Modifies the pointer to the symbol
630 pointer if successful. */
633 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
635 char buffer[GFC_MAX_SYMBOL_LEN + 1];
638 m = gfc_match_name (buffer);
643 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
644 ? MATCH_ERROR : MATCH_YES;
646 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
654 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
659 m = gfc_match_sym_tree (&st, host_assoc);
664 *matched_symbol = st->n.sym;
666 *matched_symbol = NULL;
669 *matched_symbol = NULL;
674 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
675 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
679 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
681 locus orig_loc = gfc_current_locus;
684 gfc_gobble_whitespace ();
685 ch = gfc_next_char ();
690 *result = INTRINSIC_PLUS;
695 *result = INTRINSIC_MINUS;
699 if (gfc_next_char () == '=')
702 *result = INTRINSIC_EQ;
708 if (gfc_peek_char () == '=')
712 *result = INTRINSIC_LE;
716 *result = INTRINSIC_LT;
720 if (gfc_peek_char () == '=')
724 *result = INTRINSIC_GE;
728 *result = INTRINSIC_GT;
732 if (gfc_peek_char () == '*')
736 *result = INTRINSIC_POWER;
740 *result = INTRINSIC_TIMES;
744 ch = gfc_peek_char ();
749 *result = INTRINSIC_NE;
756 *result = INTRINSIC_CONCAT;
760 *result = INTRINSIC_DIVIDE;
764 ch = gfc_next_char ();
768 if (gfc_next_char () == 'n'
769 && gfc_next_char () == 'd'
770 && gfc_next_char () == '.')
772 /* Matched ".and.". */
773 *result = INTRINSIC_AND;
779 if (gfc_next_char () == 'q')
781 ch = gfc_next_char ();
784 /* Matched ".eq.". */
785 *result = INTRINSIC_EQ_OS;
790 if (gfc_next_char () == '.')
792 /* Matched ".eqv.". */
793 *result = INTRINSIC_EQV;
801 ch = gfc_next_char ();
804 if (gfc_next_char () == '.')
806 /* Matched ".ge.". */
807 *result = INTRINSIC_GE_OS;
813 if (gfc_next_char () == '.')
815 /* Matched ".gt.". */
816 *result = INTRINSIC_GT_OS;
823 ch = gfc_next_char ();
826 if (gfc_next_char () == '.')
828 /* Matched ".le.". */
829 *result = INTRINSIC_LE_OS;
835 if (gfc_next_char () == '.')
837 /* Matched ".lt.". */
838 *result = INTRINSIC_LT_OS;
845 ch = gfc_next_char ();
848 ch = gfc_next_char ();
851 /* Matched ".ne.". */
852 *result = INTRINSIC_NE_OS;
857 if (gfc_next_char () == 'v'
858 && gfc_next_char () == '.')
860 /* Matched ".neqv.". */
861 *result = INTRINSIC_NEQV;
868 if (gfc_next_char () == 't'
869 && gfc_next_char () == '.')
871 /* Matched ".not.". */
872 *result = INTRINSIC_NOT;
879 if (gfc_next_char () == 'r'
880 && gfc_next_char () == '.')
882 /* Matched ".or.". */
883 *result = INTRINSIC_OR;
897 gfc_current_locus = orig_loc;
902 /* Match a loop control phrase:
904 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
906 If the final integer expression is not present, a constant unity
907 expression is returned. We don't return MATCH_ERROR until after
908 the equals sign is seen. */
911 gfc_match_iterator (gfc_iterator *iter, int init_flag)
913 char name[GFC_MAX_SYMBOL_LEN + 1];
914 gfc_expr *var, *e1, *e2, *e3;
918 /* Match the start of an iterator without affecting the symbol table. */
920 start = gfc_current_locus;
921 m = gfc_match (" %n =", name);
922 gfc_current_locus = start;
927 m = gfc_match_variable (&var, 0);
931 gfc_match_char ('=');
935 if (var->ref != NULL)
937 gfc_error ("Loop variable at %C cannot be a sub-component");
941 if (var->symtree->n.sym->attr.intent == INTENT_IN)
943 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
944 var->symtree->n.sym->name);
948 var->symtree->n.sym->attr.implied_index = 1;
950 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
953 if (m == MATCH_ERROR)
956 if (gfc_match_char (',') != MATCH_YES)
959 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
962 if (m == MATCH_ERROR)
965 if (gfc_match_char (',') != MATCH_YES)
967 e3 = gfc_int_expr (1);
971 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
972 if (m == MATCH_ERROR)
976 gfc_error ("Expected a step value in iterator at %C");
988 gfc_error ("Syntax error in iterator at %C");
999 /* Tries to match the next non-whitespace character on the input.
1000 This subroutine does not return MATCH_ERROR. */
1003 gfc_match_char (char c)
1007 where = gfc_current_locus;
1008 gfc_gobble_whitespace ();
1010 if (gfc_next_char () == c)
1013 gfc_current_locus = where;
1018 /* General purpose matching subroutine. The target string is a
1019 scanf-like format string in which spaces correspond to arbitrary
1020 whitespace (including no whitespace), characters correspond to
1021 themselves. The %-codes are:
1023 %% Literal percent sign
1024 %e Expression, pointer to a pointer is set
1025 %s Symbol, pointer to the symbol is set
1026 %n Name, character buffer is set to name
1027 %t Matches end of statement.
1028 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1029 %l Matches a statement label
1030 %v Matches a variable expression (an lvalue)
1031 % Matches a required space (in free form) and optional spaces. */
1034 gfc_match (const char *target, ...)
1036 gfc_st_label **label;
1045 old_loc = gfc_current_locus;
1046 va_start (argp, target);
1056 gfc_gobble_whitespace ();
1067 vp = va_arg (argp, void **);
1068 n = gfc_match_expr ((gfc_expr **) vp);
1079 vp = va_arg (argp, void **);
1080 n = gfc_match_variable ((gfc_expr **) vp, 0);
1091 vp = va_arg (argp, void **);
1092 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1103 np = va_arg (argp, char *);
1104 n = gfc_match_name (np);
1115 label = va_arg (argp, gfc_st_label **);
1116 n = gfc_match_st_label (label);
1127 ip = va_arg (argp, int *);
1128 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1139 if (gfc_match_eos () != MATCH_YES)
1147 if (gfc_match_space () == MATCH_YES)
1153 break; /* Fall through to character matcher. */
1156 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1160 if (c == gfc_next_char ())
1170 /* Clean up after a failed match. */
1171 gfc_current_locus = old_loc;
1172 va_start (argp, target);
1175 for (; matches > 0; matches--)
1177 while (*p++ != '%');
1185 /* Matches that don't have to be undone */
1190 (void) va_arg (argp, void **);
1195 vp = va_arg (argp, void **);
1196 gfc_free_expr (*vp);
1209 /*********************** Statement level matching **********************/
1211 /* Matches the start of a program unit, which is the program keyword
1212 followed by an obligatory symbol. */
1215 gfc_match_program (void)
1220 m = gfc_match ("% %s%t", &sym);
1224 gfc_error ("Invalid form of PROGRAM statement at %C");
1228 if (m == MATCH_ERROR)
1231 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1234 gfc_new_block = sym;
1240 /* Match a simple assignment statement. */
1243 gfc_match_assignment (void)
1245 gfc_expr *lvalue, *rvalue;
1249 old_loc = gfc_current_locus;
1252 m = gfc_match (" %v =", &lvalue);
1255 gfc_current_locus = old_loc;
1256 gfc_free_expr (lvalue);
1260 if (lvalue->symtree->n.sym->attr.protected
1261 && lvalue->symtree->n.sym->attr.use_assoc)
1263 gfc_current_locus = old_loc;
1264 gfc_free_expr (lvalue);
1265 gfc_error ("Setting value of PROTECTED variable at %C");
1270 m = gfc_match (" %e%t", &rvalue);
1273 gfc_current_locus = old_loc;
1274 gfc_free_expr (lvalue);
1275 gfc_free_expr (rvalue);
1279 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1281 new_st.op = EXEC_ASSIGN;
1282 new_st.expr = lvalue;
1283 new_st.expr2 = rvalue;
1285 gfc_check_do_variable (lvalue->symtree);
1291 /* Match a pointer assignment statement. */
1294 gfc_match_pointer_assignment (void)
1296 gfc_expr *lvalue, *rvalue;
1300 old_loc = gfc_current_locus;
1302 lvalue = rvalue = NULL;
1304 m = gfc_match (" %v =>", &lvalue);
1311 m = gfc_match (" %e%t", &rvalue);
1315 if (lvalue->symtree->n.sym->attr.protected
1316 && lvalue->symtree->n.sym->attr.use_assoc)
1318 gfc_error ("Assigning to a PROTECTED pointer at %C");
1323 new_st.op = EXEC_POINTER_ASSIGN;
1324 new_st.expr = lvalue;
1325 new_st.expr2 = rvalue;
1330 gfc_current_locus = old_loc;
1331 gfc_free_expr (lvalue);
1332 gfc_free_expr (rvalue);
1337 /* We try to match an easy arithmetic IF statement. This only happens
1338 when just after having encountered a simple IF statement. This code
1339 is really duplicate with parts of the gfc_match_if code, but this is
1343 match_arithmetic_if (void)
1345 gfc_st_label *l1, *l2, *l3;
1349 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1353 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1354 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1355 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1357 gfc_free_expr (expr);
1361 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF statement "
1362 "at %C") == FAILURE)
1365 new_st.op = EXEC_ARITHMETIC_IF;
1375 /* The IF statement is a bit of a pain. First of all, there are three
1376 forms of it, the simple IF, the IF that starts a block and the
1379 There is a problem with the simple IF and that is the fact that we
1380 only have a single level of undo information on symbols. What this
1381 means is for a simple IF, we must re-match the whole IF statement
1382 multiple times in order to guarantee that the symbol table ends up
1383 in the proper state. */
1385 static match match_simple_forall (void);
1386 static match match_simple_where (void);
1389 gfc_match_if (gfc_statement *if_type)
1392 gfc_st_label *l1, *l2, *l3;
1393 locus old_loc, old_loc2;
1397 n = gfc_match_label ();
1398 if (n == MATCH_ERROR)
1401 old_loc = gfc_current_locus;
1403 m = gfc_match (" if ( %e", &expr);
1407 old_loc2 = gfc_current_locus;
1408 gfc_current_locus = old_loc;
1410 if (gfc_match_parens () == MATCH_ERROR)
1413 gfc_current_locus = old_loc2;
1415 if (gfc_match_char (')') != MATCH_YES)
1417 gfc_error ("Syntax error in IF-expression at %C");
1418 gfc_free_expr (expr);
1422 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1428 gfc_error ("Block label not appropriate for arithmetic IF "
1430 gfc_free_expr (expr);
1434 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1435 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1436 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1438 gfc_free_expr (expr);
1442 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF "
1443 "statement at %C") == FAILURE)
1446 new_st.op = EXEC_ARITHMETIC_IF;
1452 *if_type = ST_ARITHMETIC_IF;
1456 if (gfc_match (" then%t") == MATCH_YES)
1458 new_st.op = EXEC_IF;
1460 *if_type = ST_IF_BLOCK;
1466 gfc_error ("Block label is not appropriate for IF statement at %C");
1467 gfc_free_expr (expr);
1471 /* At this point the only thing left is a simple IF statement. At
1472 this point, n has to be MATCH_NO, so we don't have to worry about
1473 re-matching a block label. From what we've got so far, try
1474 matching an assignment. */
1476 *if_type = ST_SIMPLE_IF;
1478 m = gfc_match_assignment ();
1482 gfc_free_expr (expr);
1483 gfc_undo_symbols ();
1484 gfc_current_locus = old_loc;
1486 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1487 assignment was found. For MATCH_NO, continue to call the various
1489 if (m == MATCH_ERROR)
1492 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1494 m = gfc_match_pointer_assignment ();
1498 gfc_free_expr (expr);
1499 gfc_undo_symbols ();
1500 gfc_current_locus = old_loc;
1502 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1504 /* Look at the next keyword to see which matcher to call. Matching
1505 the keyword doesn't affect the symbol table, so we don't have to
1506 restore between tries. */
1508 #define match(string, subr, statement) \
1509 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1513 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1514 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1515 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1516 match ("call", gfc_match_call, ST_CALL)
1517 match ("close", gfc_match_close, ST_CLOSE)
1518 match ("continue", gfc_match_continue, ST_CONTINUE)
1519 match ("cycle", gfc_match_cycle, ST_CYCLE)
1520 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1521 match ("end file", gfc_match_endfile, ST_END_FILE)
1522 match ("exit", gfc_match_exit, ST_EXIT)
1523 match ("flush", gfc_match_flush, ST_FLUSH)
1524 match ("forall", match_simple_forall, ST_FORALL)
1525 match ("go to", gfc_match_goto, ST_GOTO)
1526 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1527 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1528 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1529 match ("open", gfc_match_open, ST_OPEN)
1530 match ("pause", gfc_match_pause, ST_NONE)
1531 match ("print", gfc_match_print, ST_WRITE)
1532 match ("read", gfc_match_read, ST_READ)
1533 match ("return", gfc_match_return, ST_RETURN)
1534 match ("rewind", gfc_match_rewind, ST_REWIND)
1535 match ("stop", gfc_match_stop, ST_STOP)
1536 match ("where", match_simple_where, ST_WHERE)
1537 match ("write", gfc_match_write, ST_WRITE)
1539 /* The gfc_match_assignment() above may have returned a MATCH_NO
1540 where the assignment was to a named constant. Check that
1541 special case here. */
1542 m = gfc_match_assignment ();
1545 gfc_error ("Cannot assign to a named constant at %C");
1546 gfc_free_expr (expr);
1547 gfc_undo_symbols ();
1548 gfc_current_locus = old_loc;
1552 /* All else has failed, so give up. See if any of the matchers has
1553 stored an error message of some sort. */
1554 if (gfc_error_check () == 0)
1555 gfc_error ("Unclassifiable statement in IF-clause at %C");
1557 gfc_free_expr (expr);
1562 gfc_error ("Syntax error in IF-clause at %C");
1565 gfc_free_expr (expr);
1569 /* At this point, we've matched the single IF and the action clause
1570 is in new_st. Rearrange things so that the IF statement appears
1573 p = gfc_get_code ();
1574 p->next = gfc_get_code ();
1576 p->next->loc = gfc_current_locus;
1581 gfc_clear_new_st ();
1583 new_st.op = EXEC_IF;
1592 /* Match an ELSE statement. */
1595 gfc_match_else (void)
1597 char name[GFC_MAX_SYMBOL_LEN + 1];
1599 if (gfc_match_eos () == MATCH_YES)
1602 if (gfc_match_name (name) != MATCH_YES
1603 || gfc_current_block () == NULL
1604 || gfc_match_eos () != MATCH_YES)
1606 gfc_error ("Unexpected junk after ELSE statement at %C");
1610 if (strcmp (name, gfc_current_block ()->name) != 0)
1612 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1613 name, gfc_current_block ()->name);
1621 /* Match an ELSE IF statement. */
1624 gfc_match_elseif (void)
1626 char name[GFC_MAX_SYMBOL_LEN + 1];
1630 m = gfc_match (" ( %e ) then", &expr);
1634 if (gfc_match_eos () == MATCH_YES)
1637 if (gfc_match_name (name) != MATCH_YES
1638 || gfc_current_block () == NULL
1639 || gfc_match_eos () != MATCH_YES)
1641 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1645 if (strcmp (name, gfc_current_block ()->name) != 0)
1647 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1648 name, gfc_current_block ()->name);
1653 new_st.op = EXEC_IF;
1658 gfc_free_expr (expr);
1663 /* Free a gfc_iterator structure. */
1666 gfc_free_iterator (gfc_iterator *iter, int flag)
1672 gfc_free_expr (iter->var);
1673 gfc_free_expr (iter->start);
1674 gfc_free_expr (iter->end);
1675 gfc_free_expr (iter->step);
1682 /* Match a DO statement. */
1687 gfc_iterator iter, *ip;
1689 gfc_st_label *label;
1692 old_loc = gfc_current_locus;
1695 iter.var = iter.start = iter.end = iter.step = NULL;
1697 m = gfc_match_label ();
1698 if (m == MATCH_ERROR)
1701 if (gfc_match (" do") != MATCH_YES)
1704 m = gfc_match_st_label (&label);
1705 if (m == MATCH_ERROR)
1708 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1710 if (gfc_match_eos () == MATCH_YES)
1712 iter.end = gfc_logical_expr (1, NULL);
1713 new_st.op = EXEC_DO_WHILE;
1717 /* Match an optional comma, if no comma is found, a space is obligatory. */
1718 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1721 /* See if we have a DO WHILE. */
1722 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1724 new_st.op = EXEC_DO_WHILE;
1728 /* The abortive DO WHILE may have done something to the symbol
1729 table, so we start over. */
1730 gfc_undo_symbols ();
1731 gfc_current_locus = old_loc;
1733 gfc_match_label (); /* This won't error. */
1734 gfc_match (" do "); /* This will work. */
1736 gfc_match_st_label (&label); /* Can't error out. */
1737 gfc_match_char (','); /* Optional comma. */
1739 m = gfc_match_iterator (&iter, 0);
1742 if (m == MATCH_ERROR)
1745 iter.var->symtree->n.sym->attr.implied_index = 0;
1746 gfc_check_do_variable (iter.var->symtree);
1748 if (gfc_match_eos () != MATCH_YES)
1750 gfc_syntax_error (ST_DO);
1754 new_st.op = EXEC_DO;
1758 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1761 new_st.label = label;
1763 if (new_st.op == EXEC_DO_WHILE)
1764 new_st.expr = iter.end;
1767 new_st.ext.iterator = ip = gfc_get_iterator ();
1774 gfc_free_iterator (&iter, 0);
1780 /* Match an EXIT or CYCLE statement. */
1783 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1785 gfc_state_data *p, *o;
1789 if (gfc_match_eos () == MATCH_YES)
1793 m = gfc_match ("% %s%t", &sym);
1794 if (m == MATCH_ERROR)
1798 gfc_syntax_error (st);
1802 if (sym->attr.flavor != FL_LABEL)
1804 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1805 sym->name, gfc_ascii_statement (st));
1810 /* Find the loop mentioned specified by the label (or lack of a label). */
1811 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1812 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1814 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1820 gfc_error ("%s statement at %C is not within a loop",
1821 gfc_ascii_statement (st));
1823 gfc_error ("%s statement at %C is not within loop '%s'",
1824 gfc_ascii_statement (st), sym->name);
1831 gfc_error ("%s statement at %C leaving OpenMP structured block",
1832 gfc_ascii_statement (st));
1835 else if (st == ST_EXIT
1836 && p->previous != NULL
1837 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1838 && (p->previous->head->op == EXEC_OMP_DO
1839 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1841 gcc_assert (p->previous->head->next != NULL);
1842 gcc_assert (p->previous->head->next->op == EXEC_DO
1843 || p->previous->head->next->op == EXEC_DO_WHILE);
1844 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1848 /* Save the first statement in the loop - needed by the backend. */
1849 new_st.ext.whichloop = p->head;
1857 /* Match the EXIT statement. */
1860 gfc_match_exit (void)
1862 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1866 /* Match the CYCLE statement. */
1869 gfc_match_cycle (void)
1871 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1875 /* Match a number or character constant after a STOP or PAUSE statement. */
1878 gfc_match_stopcode (gfc_statement st)
1888 if (gfc_match_eos () != MATCH_YES)
1890 m = gfc_match_small_literal_int (&stop_code, &cnt);
1891 if (m == MATCH_ERROR)
1894 if (m == MATCH_YES && cnt > 5)
1896 gfc_error ("Too many digits in STOP code at %C");
1902 /* Try a character constant. */
1903 m = gfc_match_expr (&e);
1904 if (m == MATCH_ERROR)
1908 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1912 if (gfc_match_eos () != MATCH_YES)
1916 if (gfc_pure (NULL))
1918 gfc_error ("%s statement not allowed in PURE procedure at %C",
1919 gfc_ascii_statement (st));
1923 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1925 new_st.ext.stop_code = stop_code;
1930 gfc_syntax_error (st);
1939 /* Match the (deprecated) PAUSE statement. */
1942 gfc_match_pause (void)
1946 m = gfc_match_stopcode (ST_PAUSE);
1949 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
1958 /* Match the STOP statement. */
1961 gfc_match_stop (void)
1963 return gfc_match_stopcode (ST_STOP);
1967 /* Match a CONTINUE statement. */
1970 gfc_match_continue (void)
1972 if (gfc_match_eos () != MATCH_YES)
1974 gfc_syntax_error (ST_CONTINUE);
1978 new_st.op = EXEC_CONTINUE;
1983 /* Match the (deprecated) ASSIGN statement. */
1986 gfc_match_assign (void)
1989 gfc_st_label *label;
1991 if (gfc_match (" %l", &label) == MATCH_YES)
1993 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1995 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1997 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
2002 expr->symtree->n.sym->attr.assign = 1;
2004 new_st.op = EXEC_LABEL_ASSIGN;
2005 new_st.label = label;
2014 /* Match the GO TO statement. As a computed GOTO statement is
2015 matched, it is transformed into an equivalent SELECT block. No
2016 tree is necessary, and the resulting jumps-to-jumps are
2017 specifically optimized away by the back end. */
2020 gfc_match_goto (void)
2022 gfc_code *head, *tail;
2025 gfc_st_label *label;
2029 if (gfc_match (" %l%t", &label) == MATCH_YES)
2031 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2034 new_st.op = EXEC_GOTO;
2035 new_st.label = label;
2039 /* The assigned GO TO statement. */
2041 if (gfc_match_variable (&expr, 0) == MATCH_YES)
2043 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
2048 new_st.op = EXEC_GOTO;
2051 if (gfc_match_eos () == MATCH_YES)
2054 /* Match label list. */
2055 gfc_match_char (',');
2056 if (gfc_match_char ('(') != MATCH_YES)
2058 gfc_syntax_error (ST_GOTO);
2065 m = gfc_match_st_label (&label);
2069 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2073 head = tail = gfc_get_code ();
2076 tail->block = gfc_get_code ();
2080 tail->label = label;
2081 tail->op = EXEC_GOTO;
2083 while (gfc_match_char (',') == MATCH_YES);
2085 if (gfc_match (")%t") != MATCH_YES)
2090 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2093 new_st.block = head;
2098 /* Last chance is a computed GO TO statement. */
2099 if (gfc_match_char ('(') != MATCH_YES)
2101 gfc_syntax_error (ST_GOTO);
2110 m = gfc_match_st_label (&label);
2114 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2118 head = tail = gfc_get_code ();
2121 tail->block = gfc_get_code ();
2125 cp = gfc_get_case ();
2126 cp->low = cp->high = gfc_int_expr (i++);
2128 tail->op = EXEC_SELECT;
2129 tail->ext.case_list = cp;
2131 tail->next = gfc_get_code ();
2132 tail->next->op = EXEC_GOTO;
2133 tail->next->label = label;
2135 while (gfc_match_char (',') == MATCH_YES);
2137 if (gfc_match_char (')') != MATCH_YES)
2142 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2146 /* Get the rest of the statement. */
2147 gfc_match_char (',');
2149 if (gfc_match (" %e%t", &expr) != MATCH_YES)
2152 /* At this point, a computed GOTO has been fully matched and an
2153 equivalent SELECT statement constructed. */
2155 new_st.op = EXEC_SELECT;
2158 /* Hack: For a "real" SELECT, the expression is in expr. We put
2159 it in expr2 so we can distinguish then and produce the correct
2161 new_st.expr2 = expr;
2162 new_st.block = head;
2166 gfc_syntax_error (ST_GOTO);
2168 gfc_free_statements (head);
2173 /* Frees a list of gfc_alloc structures. */
2176 gfc_free_alloc_list (gfc_alloc *p)
2183 gfc_free_expr (p->expr);
2189 /* Match an ALLOCATE statement. */
2192 gfc_match_allocate (void)
2194 gfc_alloc *head, *tail;
2201 if (gfc_match_char ('(') != MATCH_YES)
2207 head = tail = gfc_get_alloc ();
2210 tail->next = gfc_get_alloc ();
2214 m = gfc_match_variable (&tail->expr, 0);
2217 if (m == MATCH_ERROR)
2220 if (gfc_check_do_variable (tail->expr->symtree))
2224 && gfc_impure_variable (tail->expr->symtree->n.sym))
2226 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
2231 if (tail->expr->ts.type == BT_DERIVED)
2232 tail->expr->ts.derived = gfc_use_derived (tail->expr->ts.derived);
2234 if (gfc_match_char (',') != MATCH_YES)
2237 m = gfc_match (" stat = %v", &stat);
2238 if (m == MATCH_ERROR)
2245 gfc_check_do_variable(stat->symtree);
2247 if (gfc_match (" )%t") != MATCH_YES)
2250 new_st.op = EXEC_ALLOCATE;
2252 new_st.ext.alloc_list = head;
2257 gfc_syntax_error (ST_ALLOCATE);
2260 gfc_free_expr (stat);
2261 gfc_free_alloc_list (head);
2266 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
2267 a set of pointer assignments to intrinsic NULL(). */
2270 gfc_match_nullify (void)
2278 if (gfc_match_char ('(') != MATCH_YES)
2283 m = gfc_match_variable (&p, 0);
2284 if (m == MATCH_ERROR)
2289 if (gfc_check_do_variable (p->symtree))
2292 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
2294 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
2298 /* build ' => NULL() '. */
2299 e = gfc_get_expr ();
2300 e->where = gfc_current_locus;
2301 e->expr_type = EXPR_NULL;
2302 e->ts.type = BT_UNKNOWN;
2304 /* Chain to list. */
2309 tail->next = gfc_get_code ();
2313 tail->op = EXEC_POINTER_ASSIGN;
2317 if (gfc_match (" )%t") == MATCH_YES)
2319 if (gfc_match_char (',') != MATCH_YES)
2326 gfc_syntax_error (ST_NULLIFY);
2329 gfc_free_statements (new_st.next);
2334 /* Match a DEALLOCATE statement. */
2337 gfc_match_deallocate (void)
2339 gfc_alloc *head, *tail;
2346 if (gfc_match_char ('(') != MATCH_YES)
2352 head = tail = gfc_get_alloc ();
2355 tail->next = gfc_get_alloc ();
2359 m = gfc_match_variable (&tail->expr, 0);
2360 if (m == MATCH_ERROR)
2365 if (gfc_check_do_variable (tail->expr->symtree))
2369 && gfc_impure_variable (tail->expr->symtree->n.sym))
2371 gfc_error ("Illegal deallocate-expression in DEALLOCATE at %C "
2372 "for a PURE procedure");
2376 if (gfc_match_char (',') != MATCH_YES)
2379 m = gfc_match (" stat = %v", &stat);
2380 if (m == MATCH_ERROR)
2387 gfc_check_do_variable(stat->symtree);
2389 if (gfc_match (" )%t") != MATCH_YES)
2392 new_st.op = EXEC_DEALLOCATE;
2394 new_st.ext.alloc_list = head;
2399 gfc_syntax_error (ST_DEALLOCATE);
2402 gfc_free_expr (stat);
2403 gfc_free_alloc_list (head);
2408 /* Match a RETURN statement. */
2411 gfc_match_return (void)
2415 gfc_compile_state s;
2419 if (gfc_match_eos () == MATCH_YES)
2422 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2424 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2429 if (gfc_current_form == FORM_FREE)
2431 /* The following are valid, so we can't require a blank after the
2435 c = gfc_peek_char ();
2436 if (ISALPHA (c) || ISDIGIT (c))
2440 m = gfc_match (" %e%t", &e);
2443 if (m == MATCH_ERROR)
2446 gfc_syntax_error (ST_RETURN);
2453 gfc_enclosing_unit (&s);
2454 if (s == COMP_PROGRAM
2455 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2456 "main program at %C") == FAILURE)
2459 new_st.op = EXEC_RETURN;
2466 /* Match a CALL statement. The tricky part here are possible
2467 alternate return specifiers. We handle these by having all
2468 "subroutines" actually return an integer via a register that gives
2469 the return number. If the call specifies alternate returns, we
2470 generate code for a SELECT statement whose case clauses contain
2471 GOTOs to the various labels. */
2474 gfc_match_call (void)
2476 char name[GFC_MAX_SYMBOL_LEN + 1];
2477 gfc_actual_arglist *a, *arglist;
2487 m = gfc_match ("% %n", name);
2493 if (gfc_get_ha_sym_tree (name, &st))
2498 /* If it does not seem to be callable... */
2499 if (!sym->attr.generic
2500 && !sym->attr.subroutine)
2502 if (!(sym->attr.external && !sym->attr.referenced))
2504 /* ...create a symbol in this scope... */
2505 if (sym->ns != gfc_current_ns
2506 && gfc_get_sym_tree (name, NULL, &st) == 1)
2509 if (sym != st->n.sym)
2513 /* ...and then to try to make the symbol into a subroutine. */
2514 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2518 gfc_set_sym_referenced (sym);
2520 if (gfc_match_eos () != MATCH_YES)
2522 m = gfc_match_actual_arglist (1, &arglist);
2525 if (m == MATCH_ERROR)
2528 if (gfc_match_eos () != MATCH_YES)
2532 /* If any alternate return labels were found, construct a SELECT
2533 statement that will jump to the right place. */
2536 for (a = arglist; a; a = a->next)
2537 if (a->expr == NULL)
2542 gfc_symtree *select_st;
2543 gfc_symbol *select_sym;
2544 char name[GFC_MAX_SYMBOL_LEN + 1];
2546 new_st.next = c = gfc_get_code ();
2547 c->op = EXEC_SELECT;
2548 sprintf (name, "_result_%s", sym->name);
2549 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
2551 select_sym = select_st->n.sym;
2552 select_sym->ts.type = BT_INTEGER;
2553 select_sym->ts.kind = gfc_default_integer_kind;
2554 gfc_set_sym_referenced (select_sym);
2555 c->expr = gfc_get_expr ();
2556 c->expr->expr_type = EXPR_VARIABLE;
2557 c->expr->symtree = select_st;
2558 c->expr->ts = select_sym->ts;
2559 c->expr->where = gfc_current_locus;
2562 for (a = arglist; a; a = a->next)
2564 if (a->expr != NULL)
2567 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2572 c->block = gfc_get_code ();
2574 c->op = EXEC_SELECT;
2576 new_case = gfc_get_case ();
2577 new_case->high = new_case->low = gfc_int_expr (i);
2578 c->ext.case_list = new_case;
2580 c->next = gfc_get_code ();
2581 c->next->op = EXEC_GOTO;
2582 c->next->label = a->label;
2586 new_st.op = EXEC_CALL;
2587 new_st.symtree = st;
2588 new_st.ext.actual = arglist;
2593 gfc_syntax_error (ST_CALL);
2596 gfc_free_actual_arglist (arglist);
2601 /* Given a name, return a pointer to the common head structure,
2602 creating it if it does not exist. If FROM_MODULE is nonzero, we
2603 mangle the name so that it doesn't interfere with commons defined
2604 in the using namespace.
2605 TODO: Add to global symbol tree. */
2608 gfc_get_common (const char *name, int from_module)
2611 static int serial = 0;
2612 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
2616 /* A use associated common block is only needed to correctly layout
2617 the variables it contains. */
2618 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2619 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2623 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2626 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2629 if (st->n.common == NULL)
2631 st->n.common = gfc_get_common_head ();
2632 st->n.common->where = gfc_current_locus;
2633 strcpy (st->n.common->name, name);
2636 return st->n.common;
2640 /* Match a common block name. */
2642 match match_common_name (char *name)
2646 if (gfc_match_char ('/') == MATCH_NO)
2652 if (gfc_match_char ('/') == MATCH_YES)
2658 m = gfc_match_name (name);
2660 if (m == MATCH_ERROR)
2662 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2665 gfc_error ("Syntax error in common block name at %C");
2670 /* Match a COMMON statement. */
2673 gfc_match_common (void)
2675 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2676 char name[GFC_MAX_SYMBOL_LEN + 1];
2683 old_blank_common = gfc_current_ns->blank_common.head;
2684 if (old_blank_common)
2686 while (old_blank_common->common_next)
2687 old_blank_common = old_blank_common->common_next;
2694 m = match_common_name (name);
2695 if (m == MATCH_ERROR)
2698 gsym = gfc_get_gsymbol (name);
2699 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2701 gfc_error ("Symbol '%s' at %C is already an external symbol that "
2702 "is not COMMON", name);
2706 if (gsym->type == GSYM_UNKNOWN)
2708 gsym->type = GSYM_COMMON;
2709 gsym->where = gfc_current_locus;
2715 if (name[0] == '\0')
2717 t = &gfc_current_ns->blank_common;
2718 if (t->head == NULL)
2719 t->where = gfc_current_locus;
2723 t = gfc_get_common (name, 0);
2732 while (tail->common_next)
2733 tail = tail->common_next;
2736 /* Grab the list of symbols. */
2739 m = gfc_match_symbol (&sym, 0);
2740 if (m == MATCH_ERROR)
2745 /* Store a ref to the common block for error checking. */
2746 sym->common_block = t;
2748 /* See if we know the current common block is bind(c), and if
2749 so, then see if we can check if the symbol is (which it'll
2750 need to be). This can happen if the bind(c) attr stmt was
2751 applied to the common block, and the variable(s) already
2752 defined, before declaring the common block. */
2753 if (t->is_bind_c == 1)
2755 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
2757 /* If we find an error, just print it and continue,
2758 cause it's just semantic, and we can see if there
2760 gfc_error_now ("Variable '%s' at %L in common block '%s' "
2761 "at %C must be declared with a C "
2762 "interoperable kind since common block "
2764 sym->name, &(sym->declared_at), t->name,
2768 if (sym->attr.is_bind_c == 1)
2769 gfc_error_now ("Variable '%s' in common block "
2770 "'%s' at %C can not be bind(c) since "
2771 "it is not global", sym->name, t->name);
2774 if (sym->attr.in_common)
2776 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2781 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
2782 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
2784 if (gfc_notify_std (GFC_STD_GNU, "Initialized symbol '%s' at %C "
2785 "can only be COMMON in "
2786 "BLOCK DATA", sym->name)
2791 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2795 tail->common_next = sym;
2801 /* Deal with an optional array specification after the
2803 m = gfc_match_array_spec (&as);
2804 if (m == MATCH_ERROR)
2809 if (as->type != AS_EXPLICIT)
2811 gfc_error ("Array specification for symbol '%s' in COMMON "
2812 "at %C must be explicit", sym->name);
2816 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2819 if (sym->attr.pointer)
2821 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
2822 "POINTER array", sym->name);
2831 sym->common_head = t;
2833 /* Check to see if the symbol is already in an equivalence group.
2834 If it is, set the other members as being in common. */
2835 if (sym->attr.in_equivalence)
2837 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2839 for (e2 = e1; e2; e2 = e2->eq)
2840 if (e2->expr->symtree->n.sym == sym)
2847 for (e2 = e1; e2; e2 = e2->eq)
2849 other = e2->expr->symtree->n.sym;
2850 if (other->common_head
2851 && other->common_head != sym->common_head)
2853 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2854 "%C is being indirectly equivalenced to "
2855 "another COMMON block '%s'",
2856 sym->name, sym->common_head->name,
2857 other->common_head->name);
2860 other->attr.in_common = 1;
2861 other->common_head = t;
2867 gfc_gobble_whitespace ();
2868 if (gfc_match_eos () == MATCH_YES)
2870 if (gfc_peek_char () == '/')
2872 if (gfc_match_char (',') != MATCH_YES)
2874 gfc_gobble_whitespace ();
2875 if (gfc_peek_char () == '/')
2884 gfc_syntax_error (ST_COMMON);
2887 if (old_blank_common)
2888 old_blank_common->common_next = NULL;
2890 gfc_current_ns->blank_common.head = NULL;
2891 gfc_free_array_spec (as);
2896 /* Match a BLOCK DATA program unit. */
2899 gfc_match_block_data (void)
2901 char name[GFC_MAX_SYMBOL_LEN + 1];
2905 if (gfc_match_eos () == MATCH_YES)
2907 gfc_new_block = NULL;
2911 m = gfc_match ("% %n%t", name);
2915 if (gfc_get_symbol (name, NULL, &sym))
2918 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2921 gfc_new_block = sym;
2927 /* Free a namelist structure. */
2930 gfc_free_namelist (gfc_namelist *name)
2934 for (; name; name = n)
2942 /* Match a NAMELIST statement. */
2945 gfc_match_namelist (void)
2947 gfc_symbol *group_name, *sym;
2951 m = gfc_match (" / %s /", &group_name);
2954 if (m == MATCH_ERROR)
2959 if (group_name->ts.type != BT_UNKNOWN)
2961 gfc_error ("Namelist group name '%s' at %C already has a basic "
2962 "type of %s", group_name->name,
2963 gfc_typename (&group_name->ts));
2967 if (group_name->attr.flavor == FL_NAMELIST
2968 && group_name->attr.use_assoc
2969 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2970 "at %C already is USE associated and can"
2971 "not be respecified.", group_name->name)
2975 if (group_name->attr.flavor != FL_NAMELIST
2976 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2977 group_name->name, NULL) == FAILURE)
2982 m = gfc_match_symbol (&sym, 1);
2985 if (m == MATCH_ERROR)
2988 if (sym->attr.in_namelist == 0
2989 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2992 /* Use gfc_error_check here, rather than goto error, so that
2993 these are the only errors for the next two lines. */
2994 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2996 gfc_error ("Assumed size array '%s' in namelist '%s' at "
2997 "%C is not allowed", sym->name, group_name->name);
3001 if (sym->ts.type == BT_CHARACTER && sym->ts.cl->length == NULL)
3003 gfc_error ("Assumed character length '%s' in namelist '%s' at "
3004 "%C is not allowed", sym->name, group_name->name);
3008 nl = gfc_get_namelist ();
3012 if (group_name->namelist == NULL)
3013 group_name->namelist = group_name->namelist_tail = nl;
3016 group_name->namelist_tail->next = nl;
3017 group_name->namelist_tail = nl;
3020 if (gfc_match_eos () == MATCH_YES)
3023 m = gfc_match_char (',');
3025 if (gfc_match_char ('/') == MATCH_YES)
3027 m2 = gfc_match (" %s /", &group_name);
3028 if (m2 == MATCH_YES)
3030 if (m2 == MATCH_ERROR)
3044 gfc_syntax_error (ST_NAMELIST);
3051 /* Match a MODULE statement. */
3054 gfc_match_module (void)
3058 m = gfc_match (" %s%t", &gfc_new_block);
3062 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
3063 gfc_new_block->name, NULL) == FAILURE)
3070 /* Free equivalence sets and lists. Recursively is the easiest way to
3074 gfc_free_equiv (gfc_equiv *eq)
3079 gfc_free_equiv (eq->eq);
3080 gfc_free_equiv (eq->next);
3081 gfc_free_expr (eq->expr);
3086 /* Match an EQUIVALENCE statement. */
3089 gfc_match_equivalence (void)
3091 gfc_equiv *eq, *set, *tail;
3095 gfc_common_head *common_head = NULL;
3103 eq = gfc_get_equiv ();
3107 eq->next = gfc_current_ns->equiv;
3108 gfc_current_ns->equiv = eq;
3110 if (gfc_match_char ('(') != MATCH_YES)
3114 common_flag = FALSE;
3119 m = gfc_match_equiv_variable (&set->expr);
3120 if (m == MATCH_ERROR)
3125 /* count the number of objects. */
3128 if (gfc_match_char ('%') == MATCH_YES)
3130 gfc_error ("Derived type component %C is not a "
3131 "permitted EQUIVALENCE member");
3135 for (ref = set->expr->ref; ref; ref = ref->next)
3136 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3138 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
3139 "be an array section");
3143 sym = set->expr->symtree->n.sym;
3145 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
3148 if (sym->attr.in_common)
3151 common_head = sym->common_head;
3154 if (gfc_match_char (')') == MATCH_YES)
3157 if (gfc_match_char (',') != MATCH_YES)
3160 set->eq = gfc_get_equiv ();
3166 gfc_error ("EQUIVALENCE at %C requires two or more objects");
3170 /* If one of the members of an equivalence is in common, then
3171 mark them all as being in common. Before doing this, check
3172 that members of the equivalence group are not in different
3175 for (set = eq; set; set = set->eq)
3177 sym = set->expr->symtree->n.sym;
3178 if (sym->common_head && sym->common_head != common_head)
3180 gfc_error ("Attempt to indirectly overlap COMMON "
3181 "blocks %s and %s by EQUIVALENCE at %C",
3182 sym->common_head->name, common_head->name);
3185 sym->attr.in_common = 1;
3186 sym->common_head = common_head;
3189 if (gfc_match_eos () == MATCH_YES)
3191 if (gfc_match_char (',') != MATCH_YES)
3198 gfc_syntax_error (ST_EQUIVALENCE);
3204 gfc_free_equiv (gfc_current_ns->equiv);
3205 gfc_current_ns->equiv = eq;
3211 /* Check that a statement function is not recursive. This is done by looking
3212 for the statement function symbol(sym) by looking recursively through its
3213 expression(e). If a reference to sym is found, true is returned.
3214 12.5.4 requires that any variable of function that is implicitly typed
3215 shall have that type confirmed by any subsequent type declaration. The
3216 implicit typing is conveniently done here. */
3218 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
3221 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
3227 switch (e->expr_type)
3230 if (e->symtree == NULL)
3233 /* Check the name before testing for nested recursion! */
3234 if (sym->name == e->symtree->n.sym->name)
3237 /* Catch recursion via other statement functions. */
3238 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
3239 && e->symtree->n.sym->value
3240 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
3243 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3244 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3249 if (e->symtree && sym->name == e->symtree->n.sym->name)
3252 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3253 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3265 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
3267 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
3271 /* Match a statement function declaration. It is so easy to match
3272 non-statement function statements with a MATCH_ERROR as opposed to
3273 MATCH_NO that we suppress error message in most cases. */
3276 gfc_match_st_function (void)
3278 gfc_error_buf old_error;
3283 m = gfc_match_symbol (&sym, 0);
3287 gfc_push_error (&old_error);
3289 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
3290 sym->name, NULL) == FAILURE)
3293 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
3296 m = gfc_match (" = %e%t", &expr);
3300 gfc_free_error (&old_error);
3301 if (m == MATCH_ERROR)
3304 if (recursive_stmt_fcn (expr, sym))
3306 gfc_error ("Statement function at %L is recursive", &expr->where);
3315 gfc_pop_error (&old_error);
3320 /***************** SELECT CASE subroutines ******************/
3322 /* Free a single case structure. */
3325 free_case (gfc_case *p)
3327 if (p->low == p->high)
3329 gfc_free_expr (p->low);
3330 gfc_free_expr (p->high);
3335 /* Free a list of case structures. */
3338 gfc_free_case_list (gfc_case *p)
3350 /* Match a single case selector. */
3353 match_case_selector (gfc_case **cp)
3358 c = gfc_get_case ();
3359 c->where = gfc_current_locus;
3361 if (gfc_match_char (':') == MATCH_YES)
3363 m = gfc_match_init_expr (&c->high);
3366 if (m == MATCH_ERROR)
3371 m = gfc_match_init_expr (&c->low);
3372 if (m == MATCH_ERROR)
3377 /* If we're not looking at a ':' now, make a range out of a single
3378 target. Else get the upper bound for the case range. */
3379 if (gfc_match_char (':') != MATCH_YES)
3383 m = gfc_match_init_expr (&c->high);
3384 if (m == MATCH_ERROR)
3386 /* MATCH_NO is fine. It's OK if nothing is there! */
3394 gfc_error ("Expected initialization expression in CASE at %C");
3402 /* Match the end of a case statement. */
3405 match_case_eos (void)
3407 char name[GFC_MAX_SYMBOL_LEN + 1];
3410 if (gfc_match_eos () == MATCH_YES)
3413 /* If the case construct doesn't have a case-construct-name, we
3414 should have matched the EOS. */
3415 if (!gfc_current_block ())
3417 gfc_error ("Expected the name of the SELECT CASE construct at %C");
3421 gfc_gobble_whitespace ();
3423 m = gfc_match_name (name);
3427 if (strcmp (name, gfc_current_block ()->name) != 0)
3429 gfc_error ("Expected case name of '%s' at %C",
3430 gfc_current_block ()->name);
3434 return gfc_match_eos ();
3438 /* Match a SELECT statement. */
3441 gfc_match_select (void)
3446 m = gfc_match_label ();
3447 if (m == MATCH_ERROR)
3450 m = gfc_match (" select case ( %e )%t", &expr);
3454 new_st.op = EXEC_SELECT;
3461 /* Match a CASE statement. */
3464 gfc_match_case (void)
3466 gfc_case *c, *head, *tail;
3471 if (gfc_current_state () != COMP_SELECT)
3473 gfc_error ("Unexpected CASE statement at %C");
3477 if (gfc_match ("% default") == MATCH_YES)
3479 m = match_case_eos ();
3482 if (m == MATCH_ERROR)
3485 new_st.op = EXEC_SELECT;
3486 c = gfc_get_case ();
3487 c->where = gfc_current_locus;
3488 new_st.ext.case_list = c;
3492 if (gfc_match_char ('(') != MATCH_YES)
3497 if (match_case_selector (&c) == MATCH_ERROR)
3507 if (gfc_match_char (')') == MATCH_YES)
3509 if (gfc_match_char (',') != MATCH_YES)
3513 m = match_case_eos ();
3516 if (m == MATCH_ERROR)
3519 new_st.op = EXEC_SELECT;
3520 new_st.ext.case_list = head;
3525 gfc_error ("Syntax error in CASE-specification at %C");
3528 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3532 /********************* WHERE subroutines ********************/
3534 /* Match the rest of a simple WHERE statement that follows an IF statement.
3538 match_simple_where (void)
3544 m = gfc_match (" ( %e )", &expr);
3548 m = gfc_match_assignment ();
3551 if (m == MATCH_ERROR)
3554 if (gfc_match_eos () != MATCH_YES)
3557 c = gfc_get_code ();
3561 c->next = gfc_get_code ();
3564 gfc_clear_new_st ();
3566 new_st.op = EXEC_WHERE;
3572 gfc_syntax_error (ST_WHERE);
3575 gfc_free_expr (expr);
3580 /* Match a WHERE statement. */
3583 gfc_match_where (gfc_statement *st)
3589 m0 = gfc_match_label ();
3590 if (m0 == MATCH_ERROR)
3593 m = gfc_match (" where ( %e )", &expr);
3597 if (gfc_match_eos () == MATCH_YES)
3599 *st = ST_WHERE_BLOCK;
3600 new_st.op = EXEC_WHERE;
3605 m = gfc_match_assignment ();
3607 gfc_syntax_error (ST_WHERE);
3611 gfc_free_expr (expr);
3615 /* We've got a simple WHERE statement. */
3617 c = gfc_get_code ();
3621 c->next = gfc_get_code ();
3624 gfc_clear_new_st ();
3626 new_st.op = EXEC_WHERE;
3633 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3634 new_st if successful. */
3637 gfc_match_elsewhere (void)
3639 char name[GFC_MAX_SYMBOL_LEN + 1];
3643 if (gfc_current_state () != COMP_WHERE)
3645 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3651 if (gfc_match_char ('(') == MATCH_YES)
3653 m = gfc_match_expr (&expr);
3656 if (m == MATCH_ERROR)
3659 if (gfc_match_char (')') != MATCH_YES)
3663 if (gfc_match_eos () != MATCH_YES)
3665 /* Only makes sense if we have a where-construct-name. */
3666 if (!gfc_current_block ())
3671 /* Better be a name at this point. */
3672 m = gfc_match_name (name);
3675 if (m == MATCH_ERROR)
3678 if (gfc_match_eos () != MATCH_YES)
3681 if (strcmp (name, gfc_current_block ()->name) != 0)
3683 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3684 name, gfc_current_block ()->name);
3689 new_st.op = EXEC_WHERE;
3694 gfc_syntax_error (ST_ELSEWHERE);
3697 gfc_free_expr (expr);
3702 /******************** FORALL subroutines ********************/
3704 /* Free a list of FORALL iterators. */
3707 gfc_free_forall_iterator (gfc_forall_iterator *iter)
3709 gfc_forall_iterator *next;
3714 gfc_free_expr (iter->var);
3715 gfc_free_expr (iter->start);
3716 gfc_free_expr (iter->end);
3717 gfc_free_expr (iter->stride);
3724 /* Match an iterator as part of a FORALL statement. The format is:
3726 <var> = <start>:<end>[:<stride>]
3728 On MATCH_NO, the caller tests for the possibility that there is a
3729 scalar mask expression. */
3732 match_forall_iterator (gfc_forall_iterator **result)
3734 gfc_forall_iterator *iter;
3738 where = gfc_current_locus;
3739 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3741 m = gfc_match_expr (&iter->var);
3745 if (gfc_match_char ('=') != MATCH_YES
3746 || iter->var->expr_type != EXPR_VARIABLE)
3752 m = gfc_match_expr (&iter->start);
3756 if (gfc_match_char (':') != MATCH_YES)
3759 m = gfc_match_expr (&iter->end);
3762 if (m == MATCH_ERROR)
3765 if (gfc_match_char (':') == MATCH_NO)
3766 iter->stride = gfc_int_expr (1);
3769 m = gfc_match_expr (&iter->stride);
3772 if (m == MATCH_ERROR)
3776 /* Mark the iteration variable's symbol as used as a FORALL index. */
3777 iter->var->symtree->n.sym->forall_index = true;
3783 gfc_error ("Syntax error in FORALL iterator at %C");
3788 gfc_current_locus = where;
3789 gfc_free_forall_iterator (iter);
3794 /* Match the header of a FORALL statement. */
3797 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
3799 gfc_forall_iterator *head, *tail, *new;
3803 gfc_gobble_whitespace ();
3808 if (gfc_match_char ('(') != MATCH_YES)
3811 m = match_forall_iterator (&new);
3812 if (m == MATCH_ERROR)
3821 if (gfc_match_char (',') != MATCH_YES)
3824 m = match_forall_iterator (&new);
3825 if (m == MATCH_ERROR)
3835 /* Have to have a mask expression. */
3837 m = gfc_match_expr (&msk);
3840 if (m == MATCH_ERROR)
3846 if (gfc_match_char (')') == MATCH_NO)
3854 gfc_syntax_error (ST_FORALL);
3857 gfc_free_expr (msk);
3858 gfc_free_forall_iterator (head);
3863 /* Match the rest of a simple FORALL statement that follows an
3867 match_simple_forall (void)
3869 gfc_forall_iterator *head;
3878 m = match_forall_header (&head, &mask);
3885 m = gfc_match_assignment ();
3887 if (m == MATCH_ERROR)
3891 m = gfc_match_pointer_assignment ();
3892 if (m == MATCH_ERROR)
3898 c = gfc_get_code ();
3900 c->loc = gfc_current_locus;
3902 if (gfc_match_eos () != MATCH_YES)
3905 gfc_clear_new_st ();
3906 new_st.op = EXEC_FORALL;
3908 new_st.ext.forall_iterator = head;
3909 new_st.block = gfc_get_code ();
3911 new_st.block->op = EXEC_FORALL;
3912 new_st.block->next = c;
3917 gfc_syntax_error (ST_FORALL);
3920 gfc_free_forall_iterator (head);
3921 gfc_free_expr (mask);
3927 /* Match a FORALL statement. */
3930 gfc_match_forall (gfc_statement *st)
3932 gfc_forall_iterator *head;
3941 m0 = gfc_match_label ();
3942 if (m0 == MATCH_ERROR)
3945 m = gfc_match (" forall");
3949 m = match_forall_header (&head, &mask);
3950 if (m == MATCH_ERROR)
3955 if (gfc_match_eos () == MATCH_YES)
3957 *st = ST_FORALL_BLOCK;
3958 new_st.op = EXEC_FORALL;
3960 new_st.ext.forall_iterator = head;
3964 m = gfc_match_assignment ();
3965 if (m == MATCH_ERROR)
3969 m = gfc_match_pointer_assignment ();
3970 if (m == MATCH_ERROR)
3976 c = gfc_get_code ();
3978 c->loc = gfc_current_locus;
3980 gfc_clear_new_st ();
3981 new_st.op = EXEC_FORALL;
3983 new_st.ext.forall_iterator = head;
3984 new_st.block = gfc_get_code ();
3985 new_st.block->op = EXEC_FORALL;
3986 new_st.block->next = c;
3992 gfc_syntax_error (ST_FORALL);
3995 gfc_free_forall_iterator (head);
3996 gfc_free_expr (mask);
3997 gfc_free_statements (c);