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/>. */
29 int gfc_matching_procptr_assignment = 0;
31 /* For debugging and diagnostic purposes. Return the textual representation
32 of the intrinsic operator OP. */
34 gfc_op2string (gfc_intrinsic_op op)
42 case INTRINSIC_UMINUS:
48 case INTRINSIC_CONCAT:
52 case INTRINSIC_DIVIDE:
91 case INTRINSIC_ASSIGN:
94 case INTRINSIC_PARENTHESES:
101 gfc_internal_error ("gfc_op2string(): Bad code");
106 /******************** Generic matching subroutines ************************/
108 /* This function scans the current statement counting the opened and closed
109 parenthesis to make sure they are balanced. */
112 gfc_match_parens (void)
114 locus old_loc, where;
118 old_loc = gfc_current_locus;
125 c = gfc_next_char_literal (instring);
128 if (quote == ' ' && ((c == '\'') || (c == '"')))
134 if (quote != ' ' && c == quote)
141 if (c == '(' && quote == ' ')
144 where = gfc_current_locus;
146 if (c == ')' && quote == ' ')
149 where = gfc_current_locus;
153 gfc_current_locus = old_loc;
157 gfc_error ("Missing ')' in statement at or before %L", &where);
162 gfc_error ("Missing '(' in statement at or before %L", &where);
170 /* See if the next character is a special character that has
171 escaped by a \ via the -fbackslash option. */
174 gfc_match_special_char (gfc_char_t *res)
182 switch ((c = gfc_next_char_literal (1)))
215 /* Hexadecimal form of wide characters. */
216 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
218 for (i = 0; i < len; i++)
220 char buf[2] = { '\0', '\0' };
222 c = gfc_next_char_literal (1);
223 if (!gfc_wide_fits_in_byte (c)
224 || !gfc_check_digit ((unsigned char) c, 16))
227 buf[0] = (unsigned char) c;
229 n += strtol (buf, NULL, 16);
235 /* Unknown backslash codes are simply not expanded. */
244 /* In free form, match at least one space. Always matches in fixed
248 gfc_match_space (void)
253 if (gfc_current_form == FORM_FIXED)
256 old_loc = gfc_current_locus;
258 c = gfc_next_ascii_char ();
259 if (!gfc_is_whitespace (c))
261 gfc_current_locus = old_loc;
265 gfc_gobble_whitespace ();
271 /* Match an end of statement. End of statement is optional
272 whitespace, followed by a ';' or '\n' or comment '!'. If a
273 semicolon is found, we continue to eat whitespace and semicolons. */
286 old_loc = gfc_current_locus;
287 gfc_gobble_whitespace ();
289 c = gfc_next_ascii_char ();
295 c = gfc_next_ascii_char ();
312 gfc_current_locus = old_loc;
313 return (flag) ? MATCH_YES : MATCH_NO;
317 /* Match a literal integer on the input, setting the value on
318 MATCH_YES. Literal ints occur in kind-parameters as well as
319 old-style character length specifications. If cnt is non-NULL it
320 will be set to the number of digits. */
323 gfc_match_small_literal_int (int *value, int *cnt)
329 old_loc = gfc_current_locus;
332 gfc_gobble_whitespace ();
333 c = gfc_next_ascii_char ();
339 gfc_current_locus = old_loc;
348 old_loc = gfc_current_locus;
349 c = gfc_next_ascii_char ();
354 i = 10 * i + c - '0';
359 gfc_error ("Integer too large at %C");
364 gfc_current_locus = old_loc;
373 /* Match a small, constant integer expression, like in a kind
374 statement. On MATCH_YES, 'value' is set. */
377 gfc_match_small_int (int *value)
384 m = gfc_match_expr (&expr);
388 p = gfc_extract_int (expr, &i);
389 gfc_free_expr (expr);
402 /* This function is the same as the gfc_match_small_int, except that
403 we're keeping the pointer to the expr. This function could just be
404 removed and the previously mentioned one modified, though all calls
405 to it would have to be modified then (and there were a number of
406 them). Return MATCH_ERROR if fail to extract the int; otherwise,
407 return the result of gfc_match_expr(). The expr (if any) that was
408 matched is returned in the parameter expr. */
411 gfc_match_small_int_expr (int *value, gfc_expr **expr)
417 m = gfc_match_expr (expr);
421 p = gfc_extract_int (*expr, &i);
434 /* Matches a statement label. Uses gfc_match_small_literal_int() to
435 do most of the work. */
438 gfc_match_st_label (gfc_st_label **label)
444 old_loc = gfc_current_locus;
446 m = gfc_match_small_literal_int (&i, &cnt);
452 gfc_error ("Too many digits in statement label at %C");
458 gfc_error ("Statement label at %C is zero");
462 *label = gfc_get_st_label (i);
467 gfc_current_locus = old_loc;
472 /* Match and validate a label associated with a named IF, DO or SELECT
473 statement. If the symbol does not have the label attribute, we add
474 it. We also make sure the symbol does not refer to another
475 (active) block. A matched label is pointed to by gfc_new_block. */
478 gfc_match_label (void)
480 char name[GFC_MAX_SYMBOL_LEN + 1];
483 gfc_new_block = NULL;
485 m = gfc_match (" %n :", name);
489 if (gfc_get_symbol (name, NULL, &gfc_new_block))
491 gfc_error ("Label name '%s' at %C is ambiguous", name);
495 if (gfc_new_block->attr.flavor == FL_LABEL)
497 gfc_error ("Duplicate construct label '%s' at %C", name);
501 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
502 gfc_new_block->name, NULL) == FAILURE)
509 /* See if the current input looks like a name of some sort. Modifies
510 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
511 Note that options.c restricts max_identifier_length to not more
512 than GFC_MAX_SYMBOL_LEN. */
515 gfc_match_name (char *buffer)
521 old_loc = gfc_current_locus;
522 gfc_gobble_whitespace ();
524 c = gfc_next_ascii_char ();
525 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
527 if (gfc_error_flag_test() == 0 && c != '(')
528 gfc_error ("Invalid character in name at %C");
529 gfc_current_locus = old_loc;
539 if (i > gfc_option.max_identifier_length)
541 gfc_error ("Name at %C is too long");
545 old_loc = gfc_current_locus;
546 c = gfc_next_ascii_char ();
548 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
550 if (c == '$' && !gfc_option.flag_dollar_ok)
552 gfc_error ("Invalid character '$' at %C. Use -fdollar-ok to allow it "
558 gfc_current_locus = old_loc;
564 /* Match a valid name for C, which is almost the same as for Fortran,
565 except that you can start with an underscore, etc.. It could have
566 been done by modifying the gfc_match_name, but this way other
567 things C allows can be added, such as no limits on the length.
568 Right now, the length is limited to the same thing as Fortran..
569 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
570 input characters from being automatically lower cased, since C is
571 case sensitive. The parameter, buffer, is used to return the name
572 that is matched. Return MATCH_ERROR if the name is too long
573 (though this is a self-imposed limit), MATCH_NO if what we're
574 seeing isn't a name, and MATCH_YES if we successfully match a C
578 gfc_match_name_C (char *buffer)
584 old_loc = gfc_current_locus;
585 gfc_gobble_whitespace ();
587 /* Get the next char (first possible char of name) and see if
588 it's valid for C (either a letter or an underscore). */
589 c = gfc_next_char_literal (1);
591 /* If the user put nothing expect spaces between the quotes, it is valid
592 and simply means there is no name= specifier and the name is the fortran
593 symbol name, all lowercase. */
594 if (c == '"' || c == '\'')
597 gfc_current_locus = old_loc;
601 if (!ISALPHA (c) && c != '_')
603 gfc_error ("Invalid C name in NAME= specifier at %C");
607 /* Continue to read valid variable name characters. */
610 gcc_assert (gfc_wide_fits_in_byte (c));
612 buffer[i++] = (unsigned char) c;
614 /* C does not define a maximum length of variable names, to my
615 knowledge, but the compiler typically places a limit on them.
616 For now, i'll use the same as the fortran limit for simplicity,
617 but this may need to be changed to a dynamic buffer that can
618 be realloc'ed here if necessary, or more likely, a larger
620 if (i > gfc_option.max_identifier_length)
622 gfc_error ("Name at %C is too long");
626 old_loc = gfc_current_locus;
628 /* Get next char; param means we're in a string. */
629 c = gfc_next_char_literal (1);
630 } while (ISALNUM (c) || c == '_');
633 gfc_current_locus = old_loc;
635 /* See if we stopped because of whitespace. */
638 gfc_gobble_whitespace ();
639 c = gfc_peek_ascii_char ();
640 if (c != '"' && c != '\'')
642 gfc_error ("Embedded space in NAME= specifier at %C");
647 /* If we stopped because we had an invalid character for a C name, report
648 that to the user by returning MATCH_NO. */
649 if (c != '"' && c != '\'')
651 gfc_error ("Invalid C name in NAME= specifier at %C");
659 /* Match a symbol on the input. Modifies the pointer to the symbol
660 pointer if successful. */
663 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
665 char buffer[GFC_MAX_SYMBOL_LEN + 1];
668 m = gfc_match_name (buffer);
673 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
674 ? MATCH_ERROR : MATCH_YES;
676 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
684 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
689 m = gfc_match_sym_tree (&st, host_assoc);
694 *matched_symbol = st->n.sym;
696 *matched_symbol = NULL;
699 *matched_symbol = NULL;
704 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
705 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
709 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
711 locus orig_loc = gfc_current_locus;
714 gfc_gobble_whitespace ();
715 ch = gfc_next_ascii_char ();
720 *result = INTRINSIC_PLUS;
725 *result = INTRINSIC_MINUS;
729 if (gfc_next_ascii_char () == '=')
732 *result = INTRINSIC_EQ;
738 if (gfc_peek_ascii_char () == '=')
741 gfc_next_ascii_char ();
742 *result = INTRINSIC_LE;
746 *result = INTRINSIC_LT;
750 if (gfc_peek_ascii_char () == '=')
753 gfc_next_ascii_char ();
754 *result = INTRINSIC_GE;
758 *result = INTRINSIC_GT;
762 if (gfc_peek_ascii_char () == '*')
765 gfc_next_ascii_char ();
766 *result = INTRINSIC_POWER;
770 *result = INTRINSIC_TIMES;
774 ch = gfc_peek_ascii_char ();
778 gfc_next_ascii_char ();
779 *result = INTRINSIC_NE;
785 gfc_next_ascii_char ();
786 *result = INTRINSIC_CONCAT;
790 *result = INTRINSIC_DIVIDE;
794 ch = gfc_next_ascii_char ();
798 if (gfc_next_ascii_char () == 'n'
799 && gfc_next_ascii_char () == 'd'
800 && gfc_next_ascii_char () == '.')
802 /* Matched ".and.". */
803 *result = INTRINSIC_AND;
809 if (gfc_next_ascii_char () == 'q')
811 ch = gfc_next_ascii_char ();
814 /* Matched ".eq.". */
815 *result = INTRINSIC_EQ_OS;
820 if (gfc_next_ascii_char () == '.')
822 /* Matched ".eqv.". */
823 *result = INTRINSIC_EQV;
831 ch = gfc_next_ascii_char ();
834 if (gfc_next_ascii_char () == '.')
836 /* Matched ".ge.". */
837 *result = INTRINSIC_GE_OS;
843 if (gfc_next_ascii_char () == '.')
845 /* Matched ".gt.". */
846 *result = INTRINSIC_GT_OS;
853 ch = gfc_next_ascii_char ();
856 if (gfc_next_ascii_char () == '.')
858 /* Matched ".le.". */
859 *result = INTRINSIC_LE_OS;
865 if (gfc_next_ascii_char () == '.')
867 /* Matched ".lt.". */
868 *result = INTRINSIC_LT_OS;
875 ch = gfc_next_ascii_char ();
878 ch = gfc_next_ascii_char ();
881 /* Matched ".ne.". */
882 *result = INTRINSIC_NE_OS;
887 if (gfc_next_ascii_char () == 'v'
888 && gfc_next_ascii_char () == '.')
890 /* Matched ".neqv.". */
891 *result = INTRINSIC_NEQV;
898 if (gfc_next_ascii_char () == 't'
899 && gfc_next_ascii_char () == '.')
901 /* Matched ".not.". */
902 *result = INTRINSIC_NOT;
909 if (gfc_next_ascii_char () == 'r'
910 && gfc_next_ascii_char () == '.')
912 /* Matched ".or.". */
913 *result = INTRINSIC_OR;
927 gfc_current_locus = orig_loc;
932 /* Match a loop control phrase:
934 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
936 If the final integer expression is not present, a constant unity
937 expression is returned. We don't return MATCH_ERROR until after
938 the equals sign is seen. */
941 gfc_match_iterator (gfc_iterator *iter, int init_flag)
943 char name[GFC_MAX_SYMBOL_LEN + 1];
944 gfc_expr *var, *e1, *e2, *e3;
948 /* Match the start of an iterator without affecting the symbol table. */
950 start = gfc_current_locus;
951 m = gfc_match (" %n =", name);
952 gfc_current_locus = start;
957 m = gfc_match_variable (&var, 0);
961 gfc_match_char ('=');
965 if (var->ref != NULL)
967 gfc_error ("Loop variable at %C cannot be a sub-component");
971 if (var->symtree->n.sym->attr.intent == INTENT_IN)
973 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
974 var->symtree->n.sym->name);
978 var->symtree->n.sym->attr.implied_index = 1;
980 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
983 if (m == MATCH_ERROR)
986 if (gfc_match_char (',') != MATCH_YES)
989 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
992 if (m == MATCH_ERROR)
995 if (gfc_match_char (',') != MATCH_YES)
997 e3 = gfc_int_expr (1);
1001 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1002 if (m == MATCH_ERROR)
1006 gfc_error ("Expected a step value in iterator at %C");
1018 gfc_error ("Syntax error in iterator at %C");
1029 /* Tries to match the next non-whitespace character on the input.
1030 This subroutine does not return MATCH_ERROR. */
1033 gfc_match_char (char c)
1037 where = gfc_current_locus;
1038 gfc_gobble_whitespace ();
1040 if (gfc_next_ascii_char () == c)
1043 gfc_current_locus = where;
1048 /* General purpose matching subroutine. The target string is a
1049 scanf-like format string in which spaces correspond to arbitrary
1050 whitespace (including no whitespace), characters correspond to
1051 themselves. The %-codes are:
1053 %% Literal percent sign
1054 %e Expression, pointer to a pointer is set
1055 %s Symbol, pointer to the symbol is set
1056 %n Name, character buffer is set to name
1057 %t Matches end of statement.
1058 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1059 %l Matches a statement label
1060 %v Matches a variable expression (an lvalue)
1061 % Matches a required space (in free form) and optional spaces. */
1064 gfc_match (const char *target, ...)
1066 gfc_st_label **label;
1075 old_loc = gfc_current_locus;
1076 va_start (argp, target);
1086 gfc_gobble_whitespace ();
1097 vp = va_arg (argp, void **);
1098 n = gfc_match_expr ((gfc_expr **) vp);
1109 vp = va_arg (argp, void **);
1110 n = gfc_match_variable ((gfc_expr **) vp, 0);
1121 vp = va_arg (argp, void **);
1122 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1133 np = va_arg (argp, char *);
1134 n = gfc_match_name (np);
1145 label = va_arg (argp, gfc_st_label **);
1146 n = gfc_match_st_label (label);
1157 ip = va_arg (argp, int *);
1158 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1169 if (gfc_match_eos () != MATCH_YES)
1177 if (gfc_match_space () == MATCH_YES)
1183 break; /* Fall through to character matcher. */
1186 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1190 if (c == gfc_next_ascii_char ())
1200 /* Clean up after a failed match. */
1201 gfc_current_locus = old_loc;
1202 va_start (argp, target);
1205 for (; matches > 0; matches--)
1207 while (*p++ != '%');
1215 /* Matches that don't have to be undone */
1220 (void) va_arg (argp, void **);
1225 vp = va_arg (argp, void **);
1226 gfc_free_expr ((struct gfc_expr *)*vp);
1239 /*********************** Statement level matching **********************/
1241 /* Matches the start of a program unit, which is the program keyword
1242 followed by an obligatory symbol. */
1245 gfc_match_program (void)
1250 m = gfc_match ("% %s%t", &sym);
1254 gfc_error ("Invalid form of PROGRAM statement at %C");
1258 if (m == MATCH_ERROR)
1261 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1264 gfc_new_block = sym;
1270 /* Match a simple assignment statement. */
1273 gfc_match_assignment (void)
1275 gfc_expr *lvalue, *rvalue;
1279 old_loc = gfc_current_locus;
1282 m = gfc_match (" %v =", &lvalue);
1285 gfc_current_locus = old_loc;
1286 gfc_free_expr (lvalue);
1290 if (lvalue->symtree->n.sym->attr.protected
1291 && lvalue->symtree->n.sym->attr.use_assoc)
1293 gfc_current_locus = old_loc;
1294 gfc_free_expr (lvalue);
1295 gfc_error ("Setting value of PROTECTED variable at %C");
1300 m = gfc_match (" %e%t", &rvalue);
1303 gfc_current_locus = old_loc;
1304 gfc_free_expr (lvalue);
1305 gfc_free_expr (rvalue);
1309 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1311 new_st.op = EXEC_ASSIGN;
1312 new_st.expr = lvalue;
1313 new_st.expr2 = rvalue;
1315 gfc_check_do_variable (lvalue->symtree);
1321 /* Match a pointer assignment statement. */
1324 gfc_match_pointer_assignment (void)
1326 gfc_expr *lvalue, *rvalue;
1330 old_loc = gfc_current_locus;
1332 lvalue = rvalue = NULL;
1333 gfc_matching_procptr_assignment = 0;
1335 m = gfc_match (" %v =>", &lvalue);
1342 if (lvalue->symtree->n.sym->attr.proc_pointer)
1343 gfc_matching_procptr_assignment = 1;
1345 m = gfc_match (" %e%t", &rvalue);
1346 gfc_matching_procptr_assignment = 0;
1350 if (lvalue->symtree->n.sym->attr.protected
1351 && lvalue->symtree->n.sym->attr.use_assoc)
1353 gfc_error ("Assigning to a PROTECTED pointer at %C");
1358 new_st.op = EXEC_POINTER_ASSIGN;
1359 new_st.expr = lvalue;
1360 new_st.expr2 = rvalue;
1365 gfc_current_locus = old_loc;
1366 gfc_free_expr (lvalue);
1367 gfc_free_expr (rvalue);
1372 /* We try to match an easy arithmetic IF statement. This only happens
1373 when just after having encountered a simple IF statement. This code
1374 is really duplicate with parts of the gfc_match_if code, but this is
1378 match_arithmetic_if (void)
1380 gfc_st_label *l1, *l2, *l3;
1384 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1388 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1389 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1390 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1392 gfc_free_expr (expr);
1396 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF statement "
1397 "at %C") == FAILURE)
1400 new_st.op = EXEC_ARITHMETIC_IF;
1410 /* The IF statement is a bit of a pain. First of all, there are three
1411 forms of it, the simple IF, the IF that starts a block and the
1414 There is a problem with the simple IF and that is the fact that we
1415 only have a single level of undo information on symbols. What this
1416 means is for a simple IF, we must re-match the whole IF statement
1417 multiple times in order to guarantee that the symbol table ends up
1418 in the proper state. */
1420 static match match_simple_forall (void);
1421 static match match_simple_where (void);
1424 gfc_match_if (gfc_statement *if_type)
1427 gfc_st_label *l1, *l2, *l3;
1428 locus old_loc, old_loc2;
1432 n = gfc_match_label ();
1433 if (n == MATCH_ERROR)
1436 old_loc = gfc_current_locus;
1438 m = gfc_match (" if ( %e", &expr);
1442 old_loc2 = gfc_current_locus;
1443 gfc_current_locus = old_loc;
1445 if (gfc_match_parens () == MATCH_ERROR)
1448 gfc_current_locus = old_loc2;
1450 if (gfc_match_char (')') != MATCH_YES)
1452 gfc_error ("Syntax error in IF-expression at %C");
1453 gfc_free_expr (expr);
1457 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1463 gfc_error ("Block label not appropriate for arithmetic IF "
1465 gfc_free_expr (expr);
1469 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1470 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1471 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1473 gfc_free_expr (expr);
1477 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF "
1478 "statement at %C") == FAILURE)
1481 new_st.op = EXEC_ARITHMETIC_IF;
1487 *if_type = ST_ARITHMETIC_IF;
1491 if (gfc_match (" then%t") == MATCH_YES)
1493 new_st.op = EXEC_IF;
1495 *if_type = ST_IF_BLOCK;
1501 gfc_error ("Block label is not appropriate for IF statement at %C");
1502 gfc_free_expr (expr);
1506 /* At this point the only thing left is a simple IF statement. At
1507 this point, n has to be MATCH_NO, so we don't have to worry about
1508 re-matching a block label. From what we've got so far, try
1509 matching an assignment. */
1511 *if_type = ST_SIMPLE_IF;
1513 m = gfc_match_assignment ();
1517 gfc_free_expr (expr);
1518 gfc_undo_symbols ();
1519 gfc_current_locus = old_loc;
1521 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1522 assignment was found. For MATCH_NO, continue to call the various
1524 if (m == MATCH_ERROR)
1527 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1529 m = gfc_match_pointer_assignment ();
1533 gfc_free_expr (expr);
1534 gfc_undo_symbols ();
1535 gfc_current_locus = old_loc;
1537 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1539 /* Look at the next keyword to see which matcher to call. Matching
1540 the keyword doesn't affect the symbol table, so we don't have to
1541 restore between tries. */
1543 #define match(string, subr, statement) \
1544 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1548 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1549 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1550 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1551 match ("call", gfc_match_call, ST_CALL)
1552 match ("close", gfc_match_close, ST_CLOSE)
1553 match ("continue", gfc_match_continue, ST_CONTINUE)
1554 match ("cycle", gfc_match_cycle, ST_CYCLE)
1555 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1556 match ("end file", gfc_match_endfile, ST_END_FILE)
1557 match ("exit", gfc_match_exit, ST_EXIT)
1558 match ("flush", gfc_match_flush, ST_FLUSH)
1559 match ("forall", match_simple_forall, ST_FORALL)
1560 match ("go to", gfc_match_goto, ST_GOTO)
1561 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1562 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1563 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1564 match ("open", gfc_match_open, ST_OPEN)
1565 match ("pause", gfc_match_pause, ST_NONE)
1566 match ("print", gfc_match_print, ST_WRITE)
1567 match ("read", gfc_match_read, ST_READ)
1568 match ("return", gfc_match_return, ST_RETURN)
1569 match ("rewind", gfc_match_rewind, ST_REWIND)
1570 match ("stop", gfc_match_stop, ST_STOP)
1571 match ("wait", gfc_match_wait, ST_WAIT)
1572 match ("where", match_simple_where, ST_WHERE)
1573 match ("write", gfc_match_write, ST_WRITE)
1575 /* The gfc_match_assignment() above may have returned a MATCH_NO
1576 where the assignment was to a named constant. Check that
1577 special case here. */
1578 m = gfc_match_assignment ();
1581 gfc_error ("Cannot assign to a named constant at %C");
1582 gfc_free_expr (expr);
1583 gfc_undo_symbols ();
1584 gfc_current_locus = old_loc;
1588 /* All else has failed, so give up. See if any of the matchers has
1589 stored an error message of some sort. */
1590 if (gfc_error_check () == 0)
1591 gfc_error ("Unclassifiable statement in IF-clause at %C");
1593 gfc_free_expr (expr);
1598 gfc_error ("Syntax error in IF-clause at %C");
1601 gfc_free_expr (expr);
1605 /* At this point, we've matched the single IF and the action clause
1606 is in new_st. Rearrange things so that the IF statement appears
1609 p = gfc_get_code ();
1610 p->next = gfc_get_code ();
1612 p->next->loc = gfc_current_locus;
1617 gfc_clear_new_st ();
1619 new_st.op = EXEC_IF;
1628 /* Match an ELSE statement. */
1631 gfc_match_else (void)
1633 char name[GFC_MAX_SYMBOL_LEN + 1];
1635 if (gfc_match_eos () == MATCH_YES)
1638 if (gfc_match_name (name) != MATCH_YES
1639 || gfc_current_block () == NULL
1640 || gfc_match_eos () != MATCH_YES)
1642 gfc_error ("Unexpected junk after ELSE statement at %C");
1646 if (strcmp (name, gfc_current_block ()->name) != 0)
1648 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1649 name, gfc_current_block ()->name);
1657 /* Match an ELSE IF statement. */
1660 gfc_match_elseif (void)
1662 char name[GFC_MAX_SYMBOL_LEN + 1];
1666 m = gfc_match (" ( %e ) then", &expr);
1670 if (gfc_match_eos () == MATCH_YES)
1673 if (gfc_match_name (name) != MATCH_YES
1674 || gfc_current_block () == NULL
1675 || gfc_match_eos () != MATCH_YES)
1677 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1681 if (strcmp (name, gfc_current_block ()->name) != 0)
1683 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1684 name, gfc_current_block ()->name);
1689 new_st.op = EXEC_IF;
1694 gfc_free_expr (expr);
1699 /* Free a gfc_iterator structure. */
1702 gfc_free_iterator (gfc_iterator *iter, int flag)
1708 gfc_free_expr (iter->var);
1709 gfc_free_expr (iter->start);
1710 gfc_free_expr (iter->end);
1711 gfc_free_expr (iter->step);
1718 /* Match a DO statement. */
1723 gfc_iterator iter, *ip;
1725 gfc_st_label *label;
1728 old_loc = gfc_current_locus;
1731 iter.var = iter.start = iter.end = iter.step = NULL;
1733 m = gfc_match_label ();
1734 if (m == MATCH_ERROR)
1737 if (gfc_match (" do") != MATCH_YES)
1740 m = gfc_match_st_label (&label);
1741 if (m == MATCH_ERROR)
1744 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1746 if (gfc_match_eos () == MATCH_YES)
1748 iter.end = gfc_logical_expr (1, NULL);
1749 new_st.op = EXEC_DO_WHILE;
1753 /* Match an optional comma, if no comma is found, a space is obligatory. */
1754 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1757 /* Check for balanced parens. */
1759 if (gfc_match_parens () == MATCH_ERROR)
1762 /* See if we have a DO WHILE. */
1763 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1765 new_st.op = EXEC_DO_WHILE;
1769 /* The abortive DO WHILE may have done something to the symbol
1770 table, so we start over. */
1771 gfc_undo_symbols ();
1772 gfc_current_locus = old_loc;
1774 gfc_match_label (); /* This won't error. */
1775 gfc_match (" do "); /* This will work. */
1777 gfc_match_st_label (&label); /* Can't error out. */
1778 gfc_match_char (','); /* Optional comma. */
1780 m = gfc_match_iterator (&iter, 0);
1783 if (m == MATCH_ERROR)
1786 iter.var->symtree->n.sym->attr.implied_index = 0;
1787 gfc_check_do_variable (iter.var->symtree);
1789 if (gfc_match_eos () != MATCH_YES)
1791 gfc_syntax_error (ST_DO);
1795 new_st.op = EXEC_DO;
1799 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1802 new_st.label = label;
1804 if (new_st.op == EXEC_DO_WHILE)
1805 new_st.expr = iter.end;
1808 new_st.ext.iterator = ip = gfc_get_iterator ();
1815 gfc_free_iterator (&iter, 0);
1821 /* Match an EXIT or CYCLE statement. */
1824 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1826 gfc_state_data *p, *o;
1830 if (gfc_match_eos () == MATCH_YES)
1834 m = gfc_match ("% %s%t", &sym);
1835 if (m == MATCH_ERROR)
1839 gfc_syntax_error (st);
1843 if (sym->attr.flavor != FL_LABEL)
1845 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1846 sym->name, gfc_ascii_statement (st));
1851 /* Find the loop mentioned specified by the label (or lack of a label). */
1852 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1853 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1855 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1861 gfc_error ("%s statement at %C is not within a loop",
1862 gfc_ascii_statement (st));
1864 gfc_error ("%s statement at %C is not within loop '%s'",
1865 gfc_ascii_statement (st), sym->name);
1872 gfc_error ("%s statement at %C leaving OpenMP structured block",
1873 gfc_ascii_statement (st));
1876 else if (st == ST_EXIT
1877 && p->previous != NULL
1878 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1879 && (p->previous->head->op == EXEC_OMP_DO
1880 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1882 gcc_assert (p->previous->head->next != NULL);
1883 gcc_assert (p->previous->head->next->op == EXEC_DO
1884 || p->previous->head->next->op == EXEC_DO_WHILE);
1885 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1889 /* Save the first statement in the loop - needed by the backend. */
1890 new_st.ext.whichloop = p->head;
1898 /* Match the EXIT statement. */
1901 gfc_match_exit (void)
1903 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1907 /* Match the CYCLE statement. */
1910 gfc_match_cycle (void)
1912 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1916 /* Match a number or character constant after a STOP or PAUSE statement. */
1919 gfc_match_stopcode (gfc_statement st)
1929 if (gfc_match_eos () != MATCH_YES)
1931 m = gfc_match_small_literal_int (&stop_code, &cnt);
1932 if (m == MATCH_ERROR)
1935 if (m == MATCH_YES && cnt > 5)
1937 gfc_error ("Too many digits in STOP code at %C");
1943 /* Try a character constant. */
1944 m = gfc_match_expr (&e);
1945 if (m == MATCH_ERROR)
1949 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1953 if (gfc_match_eos () != MATCH_YES)
1957 if (gfc_pure (NULL))
1959 gfc_error ("%s statement not allowed in PURE procedure at %C",
1960 gfc_ascii_statement (st));
1964 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1966 new_st.ext.stop_code = stop_code;
1971 gfc_syntax_error (st);
1980 /* Match the (deprecated) PAUSE statement. */
1983 gfc_match_pause (void)
1987 m = gfc_match_stopcode (ST_PAUSE);
1990 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
1999 /* Match the STOP statement. */
2002 gfc_match_stop (void)
2004 return gfc_match_stopcode (ST_STOP);
2008 /* Match a CONTINUE statement. */
2011 gfc_match_continue (void)
2013 if (gfc_match_eos () != MATCH_YES)
2015 gfc_syntax_error (ST_CONTINUE);
2019 new_st.op = EXEC_CONTINUE;
2024 /* Match the (deprecated) ASSIGN statement. */
2027 gfc_match_assign (void)
2030 gfc_st_label *label;
2032 if (gfc_match (" %l", &label) == MATCH_YES)
2034 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
2036 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
2038 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
2043 expr->symtree->n.sym->attr.assign = 1;
2045 new_st.op = EXEC_LABEL_ASSIGN;
2046 new_st.label = label;
2055 /* Match the GO TO statement. As a computed GOTO statement is
2056 matched, it is transformed into an equivalent SELECT block. No
2057 tree is necessary, and the resulting jumps-to-jumps are
2058 specifically optimized away by the back end. */
2061 gfc_match_goto (void)
2063 gfc_code *head, *tail;
2066 gfc_st_label *label;
2070 if (gfc_match (" %l%t", &label) == MATCH_YES)
2072 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2075 new_st.op = EXEC_GOTO;
2076 new_st.label = label;
2080 /* The assigned GO TO statement. */
2082 if (gfc_match_variable (&expr, 0) == MATCH_YES)
2084 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
2089 new_st.op = EXEC_GOTO;
2092 if (gfc_match_eos () == MATCH_YES)
2095 /* Match label list. */
2096 gfc_match_char (',');
2097 if (gfc_match_char ('(') != MATCH_YES)
2099 gfc_syntax_error (ST_GOTO);
2106 m = gfc_match_st_label (&label);
2110 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2114 head = tail = gfc_get_code ();
2117 tail->block = gfc_get_code ();
2121 tail->label = label;
2122 tail->op = EXEC_GOTO;
2124 while (gfc_match_char (',') == MATCH_YES);
2126 if (gfc_match (")%t") != MATCH_YES)
2131 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2134 new_st.block = head;
2139 /* Last chance is a computed GO TO statement. */
2140 if (gfc_match_char ('(') != MATCH_YES)
2142 gfc_syntax_error (ST_GOTO);
2151 m = gfc_match_st_label (&label);
2155 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2159 head = tail = gfc_get_code ();
2162 tail->block = gfc_get_code ();
2166 cp = gfc_get_case ();
2167 cp->low = cp->high = gfc_int_expr (i++);
2169 tail->op = EXEC_SELECT;
2170 tail->ext.case_list = cp;
2172 tail->next = gfc_get_code ();
2173 tail->next->op = EXEC_GOTO;
2174 tail->next->label = label;
2176 while (gfc_match_char (',') == MATCH_YES);
2178 if (gfc_match_char (')') != MATCH_YES)
2183 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2187 /* Get the rest of the statement. */
2188 gfc_match_char (',');
2190 if (gfc_match (" %e%t", &expr) != MATCH_YES)
2193 /* At this point, a computed GOTO has been fully matched and an
2194 equivalent SELECT statement constructed. */
2196 new_st.op = EXEC_SELECT;
2199 /* Hack: For a "real" SELECT, the expression is in expr. We put
2200 it in expr2 so we can distinguish then and produce the correct
2202 new_st.expr2 = expr;
2203 new_st.block = head;
2207 gfc_syntax_error (ST_GOTO);
2209 gfc_free_statements (head);
2214 /* Frees a list of gfc_alloc structures. */
2217 gfc_free_alloc_list (gfc_alloc *p)
2224 gfc_free_expr (p->expr);
2230 /* Match an ALLOCATE statement. */
2233 gfc_match_allocate (void)
2235 gfc_alloc *head, *tail;
2242 if (gfc_match_char ('(') != MATCH_YES)
2248 head = tail = gfc_get_alloc ();
2251 tail->next = gfc_get_alloc ();
2255 m = gfc_match_variable (&tail->expr, 0);
2258 if (m == MATCH_ERROR)
2261 if (gfc_check_do_variable (tail->expr->symtree))
2265 && gfc_impure_variable (tail->expr->symtree->n.sym))
2267 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
2272 if (tail->expr->ts.type == BT_DERIVED)
2273 tail->expr->ts.derived = gfc_use_derived (tail->expr->ts.derived);
2275 if (gfc_match_char (',') != MATCH_YES)
2278 m = gfc_match (" stat = %v", &stat);
2279 if (m == MATCH_ERROR)
2286 gfc_check_do_variable(stat->symtree);
2288 if (gfc_match (" )%t") != MATCH_YES)
2291 new_st.op = EXEC_ALLOCATE;
2293 new_st.ext.alloc_list = head;
2298 gfc_syntax_error (ST_ALLOCATE);
2301 gfc_free_expr (stat);
2302 gfc_free_alloc_list (head);
2307 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
2308 a set of pointer assignments to intrinsic NULL(). */
2311 gfc_match_nullify (void)
2319 if (gfc_match_char ('(') != MATCH_YES)
2324 m = gfc_match_variable (&p, 0);
2325 if (m == MATCH_ERROR)
2330 if (gfc_check_do_variable (p->symtree))
2333 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
2335 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
2339 /* build ' => NULL() '. */
2340 e = gfc_get_expr ();
2341 e->where = gfc_current_locus;
2342 e->expr_type = EXPR_NULL;
2343 e->ts.type = BT_UNKNOWN;
2345 /* Chain to list. */
2350 tail->next = gfc_get_code ();
2354 tail->op = EXEC_POINTER_ASSIGN;
2358 if (gfc_match (" )%t") == MATCH_YES)
2360 if (gfc_match_char (',') != MATCH_YES)
2367 gfc_syntax_error (ST_NULLIFY);
2370 gfc_free_statements (new_st.next);
2375 /* Match a DEALLOCATE statement. */
2378 gfc_match_deallocate (void)
2380 gfc_alloc *head, *tail;
2387 if (gfc_match_char ('(') != MATCH_YES)
2393 head = tail = gfc_get_alloc ();
2396 tail->next = gfc_get_alloc ();
2400 m = gfc_match_variable (&tail->expr, 0);
2401 if (m == MATCH_ERROR)
2406 if (gfc_check_do_variable (tail->expr->symtree))
2410 && gfc_impure_variable (tail->expr->symtree->n.sym))
2412 gfc_error ("Illegal deallocate-expression in DEALLOCATE at %C "
2413 "for a PURE procedure");
2417 if (gfc_match_char (',') != MATCH_YES)
2420 m = gfc_match (" stat = %v", &stat);
2421 if (m == MATCH_ERROR)
2428 gfc_check_do_variable(stat->symtree);
2430 if (gfc_match (" )%t") != MATCH_YES)
2433 new_st.op = EXEC_DEALLOCATE;
2435 new_st.ext.alloc_list = head;
2440 gfc_syntax_error (ST_DEALLOCATE);
2443 gfc_free_expr (stat);
2444 gfc_free_alloc_list (head);
2449 /* Match a RETURN statement. */
2452 gfc_match_return (void)
2456 gfc_compile_state s;
2459 if (gfc_match_eos () == MATCH_YES)
2462 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2464 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2469 if (gfc_current_form == FORM_FREE)
2471 /* The following are valid, so we can't require a blank after the
2475 char c = gfc_peek_ascii_char ();
2476 if (ISALPHA (c) || ISDIGIT (c))
2480 m = gfc_match (" %e%t", &e);
2483 if (m == MATCH_ERROR)
2486 gfc_syntax_error (ST_RETURN);
2493 gfc_enclosing_unit (&s);
2494 if (s == COMP_PROGRAM
2495 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2496 "main program at %C") == FAILURE)
2499 new_st.op = EXEC_RETURN;
2506 /* Match a CALL statement. The tricky part here are possible
2507 alternate return specifiers. We handle these by having all
2508 "subroutines" actually return an integer via a register that gives
2509 the return number. If the call specifies alternate returns, we
2510 generate code for a SELECT statement whose case clauses contain
2511 GOTOs to the various labels. */
2514 gfc_match_call (void)
2516 char name[GFC_MAX_SYMBOL_LEN + 1];
2517 gfc_actual_arglist *a, *arglist;
2527 m = gfc_match ("% %n", name);
2533 if (gfc_get_ha_sym_tree (name, &st))
2538 /* If it does not seem to be callable... */
2539 if (!sym->attr.generic
2540 && !sym->attr.subroutine)
2542 if (!(sym->attr.external && !sym->attr.referenced))
2544 /* ...create a symbol in this scope... */
2545 if (sym->ns != gfc_current_ns
2546 && gfc_get_sym_tree (name, NULL, &st) == 1)
2549 if (sym != st->n.sym)
2553 /* ...and then to try to make the symbol into a subroutine. */
2554 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2558 gfc_set_sym_referenced (sym);
2560 if (gfc_match_eos () != MATCH_YES)
2562 m = gfc_match_actual_arglist (1, &arglist);
2565 if (m == MATCH_ERROR)
2568 if (gfc_match_eos () != MATCH_YES)
2572 /* If any alternate return labels were found, construct a SELECT
2573 statement that will jump to the right place. */
2576 for (a = arglist; a; a = a->next)
2577 if (a->expr == NULL)
2582 gfc_symtree *select_st;
2583 gfc_symbol *select_sym;
2584 char name[GFC_MAX_SYMBOL_LEN + 1];
2586 new_st.next = c = gfc_get_code ();
2587 c->op = EXEC_SELECT;
2588 sprintf (name, "_result_%s", sym->name);
2589 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
2591 select_sym = select_st->n.sym;
2592 select_sym->ts.type = BT_INTEGER;
2593 select_sym->ts.kind = gfc_default_integer_kind;
2594 gfc_set_sym_referenced (select_sym);
2595 c->expr = gfc_get_expr ();
2596 c->expr->expr_type = EXPR_VARIABLE;
2597 c->expr->symtree = select_st;
2598 c->expr->ts = select_sym->ts;
2599 c->expr->where = gfc_current_locus;
2602 for (a = arglist; a; a = a->next)
2604 if (a->expr != NULL)
2607 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2612 c->block = gfc_get_code ();
2614 c->op = EXEC_SELECT;
2616 new_case = gfc_get_case ();
2617 new_case->high = new_case->low = gfc_int_expr (i);
2618 c->ext.case_list = new_case;
2620 c->next = gfc_get_code ();
2621 c->next->op = EXEC_GOTO;
2622 c->next->label = a->label;
2626 new_st.op = EXEC_CALL;
2627 new_st.symtree = st;
2628 new_st.ext.actual = arglist;
2633 gfc_syntax_error (ST_CALL);
2636 gfc_free_actual_arglist (arglist);
2641 /* Given a name, return a pointer to the common head structure,
2642 creating it if it does not exist. If FROM_MODULE is nonzero, we
2643 mangle the name so that it doesn't interfere with commons defined
2644 in the using namespace.
2645 TODO: Add to global symbol tree. */
2648 gfc_get_common (const char *name, int from_module)
2651 static int serial = 0;
2652 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
2656 /* A use associated common block is only needed to correctly layout
2657 the variables it contains. */
2658 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2659 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2663 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2666 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2669 if (st->n.common == NULL)
2671 st->n.common = gfc_get_common_head ();
2672 st->n.common->where = gfc_current_locus;
2673 strcpy (st->n.common->name, name);
2676 return st->n.common;
2680 /* Match a common block name. */
2682 match match_common_name (char *name)
2686 if (gfc_match_char ('/') == MATCH_NO)
2692 if (gfc_match_char ('/') == MATCH_YES)
2698 m = gfc_match_name (name);
2700 if (m == MATCH_ERROR)
2702 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2705 gfc_error ("Syntax error in common block name at %C");
2710 /* Match a COMMON statement. */
2713 gfc_match_common (void)
2715 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2716 char name[GFC_MAX_SYMBOL_LEN + 1];
2723 old_blank_common = gfc_current_ns->blank_common.head;
2724 if (old_blank_common)
2726 while (old_blank_common->common_next)
2727 old_blank_common = old_blank_common->common_next;
2734 m = match_common_name (name);
2735 if (m == MATCH_ERROR)
2738 gsym = gfc_get_gsymbol (name);
2739 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2741 gfc_error ("Symbol '%s' at %C is already an external symbol that "
2742 "is not COMMON", name);
2746 if (gsym->type == GSYM_UNKNOWN)
2748 gsym->type = GSYM_COMMON;
2749 gsym->where = gfc_current_locus;
2755 if (name[0] == '\0')
2757 t = &gfc_current_ns->blank_common;
2758 if (t->head == NULL)
2759 t->where = gfc_current_locus;
2763 t = gfc_get_common (name, 0);
2772 while (tail->common_next)
2773 tail = tail->common_next;
2776 /* Grab the list of symbols. */
2779 m = gfc_match_symbol (&sym, 0);
2780 if (m == MATCH_ERROR)
2785 /* Store a ref to the common block for error checking. */
2786 sym->common_block = t;
2788 /* See if we know the current common block is bind(c), and if
2789 so, then see if we can check if the symbol is (which it'll
2790 need to be). This can happen if the bind(c) attr stmt was
2791 applied to the common block, and the variable(s) already
2792 defined, before declaring the common block. */
2793 if (t->is_bind_c == 1)
2795 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
2797 /* If we find an error, just print it and continue,
2798 cause it's just semantic, and we can see if there
2800 gfc_error_now ("Variable '%s' at %L in common block '%s' "
2801 "at %C must be declared with a C "
2802 "interoperable kind since common block "
2804 sym->name, &(sym->declared_at), t->name,
2808 if (sym->attr.is_bind_c == 1)
2809 gfc_error_now ("Variable '%s' in common block "
2810 "'%s' at %C can not be bind(c) since "
2811 "it is not global", sym->name, t->name);
2814 if (sym->attr.in_common)
2816 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2821 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
2822 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
2824 if (gfc_notify_std (GFC_STD_GNU, "Initialized symbol '%s' at %C "
2825 "can only be COMMON in "
2826 "BLOCK DATA", sym->name)
2831 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2835 tail->common_next = sym;
2841 /* Deal with an optional array specification after the
2843 m = gfc_match_array_spec (&as);
2844 if (m == MATCH_ERROR)
2849 if (as->type != AS_EXPLICIT)
2851 gfc_error ("Array specification for symbol '%s' in COMMON "
2852 "at %C must be explicit", sym->name);
2856 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2859 if (sym->attr.pointer)
2861 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
2862 "POINTER array", sym->name);
2871 sym->common_head = t;
2873 /* Check to see if the symbol is already in an equivalence group.
2874 If it is, set the other members as being in common. */
2875 if (sym->attr.in_equivalence)
2877 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2879 for (e2 = e1; e2; e2 = e2->eq)
2880 if (e2->expr->symtree->n.sym == sym)
2887 for (e2 = e1; e2; e2 = e2->eq)
2889 other = e2->expr->symtree->n.sym;
2890 if (other->common_head
2891 && other->common_head != sym->common_head)
2893 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2894 "%C is being indirectly equivalenced to "
2895 "another COMMON block '%s'",
2896 sym->name, sym->common_head->name,
2897 other->common_head->name);
2900 other->attr.in_common = 1;
2901 other->common_head = t;
2907 gfc_gobble_whitespace ();
2908 if (gfc_match_eos () == MATCH_YES)
2910 if (gfc_peek_ascii_char () == '/')
2912 if (gfc_match_char (',') != MATCH_YES)
2914 gfc_gobble_whitespace ();
2915 if (gfc_peek_ascii_char () == '/')
2924 gfc_syntax_error (ST_COMMON);
2927 if (old_blank_common)
2928 old_blank_common->common_next = NULL;
2930 gfc_current_ns->blank_common.head = NULL;
2931 gfc_free_array_spec (as);
2936 /* Match a BLOCK DATA program unit. */
2939 gfc_match_block_data (void)
2941 char name[GFC_MAX_SYMBOL_LEN + 1];
2945 if (gfc_match_eos () == MATCH_YES)
2947 gfc_new_block = NULL;
2951 m = gfc_match ("% %n%t", name);
2955 if (gfc_get_symbol (name, NULL, &sym))
2958 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2961 gfc_new_block = sym;
2967 /* Free a namelist structure. */
2970 gfc_free_namelist (gfc_namelist *name)
2974 for (; name; name = n)
2982 /* Match a NAMELIST statement. */
2985 gfc_match_namelist (void)
2987 gfc_symbol *group_name, *sym;
2991 m = gfc_match (" / %s /", &group_name);
2994 if (m == MATCH_ERROR)
2999 if (group_name->ts.type != BT_UNKNOWN)
3001 gfc_error ("Namelist group name '%s' at %C already has a basic "
3002 "type of %s", group_name->name,
3003 gfc_typename (&group_name->ts));
3007 if (group_name->attr.flavor == FL_NAMELIST
3008 && group_name->attr.use_assoc
3009 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
3010 "at %C already is USE associated and can"
3011 "not be respecified.", group_name->name)
3015 if (group_name->attr.flavor != FL_NAMELIST
3016 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
3017 group_name->name, NULL) == FAILURE)
3022 m = gfc_match_symbol (&sym, 1);
3025 if (m == MATCH_ERROR)
3028 if (sym->attr.in_namelist == 0
3029 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
3032 /* Use gfc_error_check here, rather than goto error, so that
3033 these are the only errors for the next two lines. */
3034 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
3036 gfc_error ("Assumed size array '%s' in namelist '%s' at "
3037 "%C is not allowed", sym->name, group_name->name);
3041 if (sym->ts.type == BT_CHARACTER && sym->ts.cl->length == NULL)
3043 gfc_error ("Assumed character length '%s' in namelist '%s' at "
3044 "%C is not allowed", sym->name, group_name->name);
3048 nl = gfc_get_namelist ();
3052 if (group_name->namelist == NULL)
3053 group_name->namelist = group_name->namelist_tail = nl;
3056 group_name->namelist_tail->next = nl;
3057 group_name->namelist_tail = nl;
3060 if (gfc_match_eos () == MATCH_YES)
3063 m = gfc_match_char (',');
3065 if (gfc_match_char ('/') == MATCH_YES)
3067 m2 = gfc_match (" %s /", &group_name);
3068 if (m2 == MATCH_YES)
3070 if (m2 == MATCH_ERROR)
3084 gfc_syntax_error (ST_NAMELIST);
3091 /* Match a MODULE statement. */
3094 gfc_match_module (void)
3098 m = gfc_match (" %s%t", &gfc_new_block);
3102 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
3103 gfc_new_block->name, NULL) == FAILURE)
3110 /* Free equivalence sets and lists. Recursively is the easiest way to
3114 gfc_free_equiv (gfc_equiv *eq)
3119 gfc_free_equiv (eq->eq);
3120 gfc_free_equiv (eq->next);
3121 gfc_free_expr (eq->expr);
3126 /* Match an EQUIVALENCE statement. */
3129 gfc_match_equivalence (void)
3131 gfc_equiv *eq, *set, *tail;
3135 gfc_common_head *common_head = NULL;
3143 eq = gfc_get_equiv ();
3147 eq->next = gfc_current_ns->equiv;
3148 gfc_current_ns->equiv = eq;
3150 if (gfc_match_char ('(') != MATCH_YES)
3154 common_flag = FALSE;
3159 m = gfc_match_equiv_variable (&set->expr);
3160 if (m == MATCH_ERROR)
3165 /* count the number of objects. */
3168 if (gfc_match_char ('%') == MATCH_YES)
3170 gfc_error ("Derived type component %C is not a "
3171 "permitted EQUIVALENCE member");
3175 for (ref = set->expr->ref; ref; ref = ref->next)
3176 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3178 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
3179 "be an array section");
3183 sym = set->expr->symtree->n.sym;
3185 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
3188 if (sym->attr.in_common)
3191 common_head = sym->common_head;
3194 if (gfc_match_char (')') == MATCH_YES)
3197 if (gfc_match_char (',') != MATCH_YES)
3200 set->eq = gfc_get_equiv ();
3206 gfc_error ("EQUIVALENCE at %C requires two or more objects");
3210 /* If one of the members of an equivalence is in common, then
3211 mark them all as being in common. Before doing this, check
3212 that members of the equivalence group are not in different
3215 for (set = eq; set; set = set->eq)
3217 sym = set->expr->symtree->n.sym;
3218 if (sym->common_head && sym->common_head != common_head)
3220 gfc_error ("Attempt to indirectly overlap COMMON "
3221 "blocks %s and %s by EQUIVALENCE at %C",
3222 sym->common_head->name, common_head->name);
3225 sym->attr.in_common = 1;
3226 sym->common_head = common_head;
3229 if (gfc_match_eos () == MATCH_YES)
3231 if (gfc_match_char (',') != MATCH_YES)
3238 gfc_syntax_error (ST_EQUIVALENCE);
3244 gfc_free_equiv (gfc_current_ns->equiv);
3245 gfc_current_ns->equiv = eq;
3251 /* Check that a statement function is not recursive. This is done by looking
3252 for the statement function symbol(sym) by looking recursively through its
3253 expression(e). If a reference to sym is found, true is returned.
3254 12.5.4 requires that any variable of function that is implicitly typed
3255 shall have that type confirmed by any subsequent type declaration. The
3256 implicit typing is conveniently done here. */
3258 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
3261 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
3267 switch (e->expr_type)
3270 if (e->symtree == NULL)
3273 /* Check the name before testing for nested recursion! */
3274 if (sym->name == e->symtree->n.sym->name)
3277 /* Catch recursion via other statement functions. */
3278 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
3279 && e->symtree->n.sym->value
3280 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
3283 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3284 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3289 if (e->symtree && sym->name == e->symtree->n.sym->name)
3292 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3293 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3305 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
3307 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
3311 /* Match a statement function declaration. It is so easy to match
3312 non-statement function statements with a MATCH_ERROR as opposed to
3313 MATCH_NO that we suppress error message in most cases. */
3316 gfc_match_st_function (void)
3318 gfc_error_buf old_error;
3323 m = gfc_match_symbol (&sym, 0);
3327 gfc_push_error (&old_error);
3329 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
3330 sym->name, NULL) == FAILURE)
3333 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
3336 m = gfc_match (" = %e%t", &expr);
3340 gfc_free_error (&old_error);
3341 if (m == MATCH_ERROR)
3344 if (recursive_stmt_fcn (expr, sym))
3346 gfc_error ("Statement function at %L is recursive", &expr->where);
3355 gfc_pop_error (&old_error);
3360 /***************** SELECT CASE subroutines ******************/
3362 /* Free a single case structure. */
3365 free_case (gfc_case *p)
3367 if (p->low == p->high)
3369 gfc_free_expr (p->low);
3370 gfc_free_expr (p->high);
3375 /* Free a list of case structures. */
3378 gfc_free_case_list (gfc_case *p)
3390 /* Match a single case selector. */
3393 match_case_selector (gfc_case **cp)
3398 c = gfc_get_case ();
3399 c->where = gfc_current_locus;
3401 if (gfc_match_char (':') == MATCH_YES)
3403 m = gfc_match_init_expr (&c->high);
3406 if (m == MATCH_ERROR)
3411 m = gfc_match_init_expr (&c->low);
3412 if (m == MATCH_ERROR)
3417 /* If we're not looking at a ':' now, make a range out of a single
3418 target. Else get the upper bound for the case range. */
3419 if (gfc_match_char (':') != MATCH_YES)
3423 m = gfc_match_init_expr (&c->high);
3424 if (m == MATCH_ERROR)
3426 /* MATCH_NO is fine. It's OK if nothing is there! */
3434 gfc_error ("Expected initialization expression in CASE at %C");
3442 /* Match the end of a case statement. */
3445 match_case_eos (void)
3447 char name[GFC_MAX_SYMBOL_LEN + 1];
3450 if (gfc_match_eos () == MATCH_YES)
3453 /* If the case construct doesn't have a case-construct-name, we
3454 should have matched the EOS. */
3455 if (!gfc_current_block ())
3457 gfc_error ("Expected the name of the SELECT CASE construct at %C");
3461 gfc_gobble_whitespace ();
3463 m = gfc_match_name (name);
3467 if (strcmp (name, gfc_current_block ()->name) != 0)
3469 gfc_error ("Expected case name of '%s' at %C",
3470 gfc_current_block ()->name);
3474 return gfc_match_eos ();
3478 /* Match a SELECT statement. */
3481 gfc_match_select (void)
3486 m = gfc_match_label ();
3487 if (m == MATCH_ERROR)
3490 m = gfc_match (" select case ( %e )%t", &expr);
3494 new_st.op = EXEC_SELECT;
3501 /* Match a CASE statement. */
3504 gfc_match_case (void)
3506 gfc_case *c, *head, *tail;
3511 if (gfc_current_state () != COMP_SELECT)
3513 gfc_error ("Unexpected CASE statement at %C");
3517 if (gfc_match ("% default") == MATCH_YES)
3519 m = match_case_eos ();
3522 if (m == MATCH_ERROR)
3525 new_st.op = EXEC_SELECT;
3526 c = gfc_get_case ();
3527 c->where = gfc_current_locus;
3528 new_st.ext.case_list = c;
3532 if (gfc_match_char ('(') != MATCH_YES)
3537 if (match_case_selector (&c) == MATCH_ERROR)
3547 if (gfc_match_char (')') == MATCH_YES)
3549 if (gfc_match_char (',') != MATCH_YES)
3553 m = match_case_eos ();
3556 if (m == MATCH_ERROR)
3559 new_st.op = EXEC_SELECT;
3560 new_st.ext.case_list = head;
3565 gfc_error ("Syntax error in CASE-specification at %C");
3568 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3572 /********************* WHERE subroutines ********************/
3574 /* Match the rest of a simple WHERE statement that follows an IF statement.
3578 match_simple_where (void)
3584 m = gfc_match (" ( %e )", &expr);
3588 m = gfc_match_assignment ();
3591 if (m == MATCH_ERROR)
3594 if (gfc_match_eos () != MATCH_YES)
3597 c = gfc_get_code ();
3601 c->next = gfc_get_code ();
3604 gfc_clear_new_st ();
3606 new_st.op = EXEC_WHERE;
3612 gfc_syntax_error (ST_WHERE);
3615 gfc_free_expr (expr);
3620 /* Match a WHERE statement. */
3623 gfc_match_where (gfc_statement *st)
3629 m0 = gfc_match_label ();
3630 if (m0 == MATCH_ERROR)
3633 m = gfc_match (" where ( %e )", &expr);
3637 if (gfc_match_eos () == MATCH_YES)
3639 *st = ST_WHERE_BLOCK;
3640 new_st.op = EXEC_WHERE;
3645 m = gfc_match_assignment ();
3647 gfc_syntax_error (ST_WHERE);
3651 gfc_free_expr (expr);
3655 /* We've got a simple WHERE statement. */
3657 c = gfc_get_code ();
3661 c->next = gfc_get_code ();
3664 gfc_clear_new_st ();
3666 new_st.op = EXEC_WHERE;
3673 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3674 new_st if successful. */
3677 gfc_match_elsewhere (void)
3679 char name[GFC_MAX_SYMBOL_LEN + 1];
3683 if (gfc_current_state () != COMP_WHERE)
3685 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3691 if (gfc_match_char ('(') == MATCH_YES)
3693 m = gfc_match_expr (&expr);
3696 if (m == MATCH_ERROR)
3699 if (gfc_match_char (')') != MATCH_YES)
3703 if (gfc_match_eos () != MATCH_YES)
3705 /* Only makes sense if we have a where-construct-name. */
3706 if (!gfc_current_block ())
3711 /* Better be a name at this point. */
3712 m = gfc_match_name (name);
3715 if (m == MATCH_ERROR)
3718 if (gfc_match_eos () != MATCH_YES)
3721 if (strcmp (name, gfc_current_block ()->name) != 0)
3723 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3724 name, gfc_current_block ()->name);
3729 new_st.op = EXEC_WHERE;
3734 gfc_syntax_error (ST_ELSEWHERE);
3737 gfc_free_expr (expr);
3742 /******************** FORALL subroutines ********************/
3744 /* Free a list of FORALL iterators. */
3747 gfc_free_forall_iterator (gfc_forall_iterator *iter)
3749 gfc_forall_iterator *next;
3754 gfc_free_expr (iter->var);
3755 gfc_free_expr (iter->start);
3756 gfc_free_expr (iter->end);
3757 gfc_free_expr (iter->stride);
3764 /* Match an iterator as part of a FORALL statement. The format is:
3766 <var> = <start>:<end>[:<stride>]
3768 On MATCH_NO, the caller tests for the possibility that there is a
3769 scalar mask expression. */
3772 match_forall_iterator (gfc_forall_iterator **result)
3774 gfc_forall_iterator *iter;
3778 where = gfc_current_locus;
3779 iter = XCNEW (gfc_forall_iterator);
3781 m = gfc_match_expr (&iter->var);
3785 if (gfc_match_char ('=') != MATCH_YES
3786 || iter->var->expr_type != EXPR_VARIABLE)
3792 m = gfc_match_expr (&iter->start);
3796 if (gfc_match_char (':') != MATCH_YES)
3799 m = gfc_match_expr (&iter->end);
3802 if (m == MATCH_ERROR)
3805 if (gfc_match_char (':') == MATCH_NO)
3806 iter->stride = gfc_int_expr (1);
3809 m = gfc_match_expr (&iter->stride);
3812 if (m == MATCH_ERROR)
3816 /* Mark the iteration variable's symbol as used as a FORALL index. */
3817 iter->var->symtree->n.sym->forall_index = true;
3823 gfc_error ("Syntax error in FORALL iterator at %C");
3828 gfc_current_locus = where;
3829 gfc_free_forall_iterator (iter);
3834 /* Match the header of a FORALL statement. */
3837 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
3839 gfc_forall_iterator *head, *tail, *new;
3843 gfc_gobble_whitespace ();
3848 if (gfc_match_char ('(') != MATCH_YES)
3851 m = match_forall_iterator (&new);
3852 if (m == MATCH_ERROR)
3861 if (gfc_match_char (',') != MATCH_YES)
3864 m = match_forall_iterator (&new);
3865 if (m == MATCH_ERROR)
3875 /* Have to have a mask expression. */
3877 m = gfc_match_expr (&msk);
3880 if (m == MATCH_ERROR)
3886 if (gfc_match_char (')') == MATCH_NO)
3894 gfc_syntax_error (ST_FORALL);
3897 gfc_free_expr (msk);
3898 gfc_free_forall_iterator (head);
3903 /* Match the rest of a simple FORALL statement that follows an
3907 match_simple_forall (void)
3909 gfc_forall_iterator *head;
3918 m = match_forall_header (&head, &mask);
3925 m = gfc_match_assignment ();
3927 if (m == MATCH_ERROR)
3931 m = gfc_match_pointer_assignment ();
3932 if (m == MATCH_ERROR)
3938 c = gfc_get_code ();
3940 c->loc = gfc_current_locus;
3942 if (gfc_match_eos () != MATCH_YES)
3945 gfc_clear_new_st ();
3946 new_st.op = EXEC_FORALL;
3948 new_st.ext.forall_iterator = head;
3949 new_st.block = gfc_get_code ();
3951 new_st.block->op = EXEC_FORALL;
3952 new_st.block->next = c;
3957 gfc_syntax_error (ST_FORALL);
3960 gfc_free_forall_iterator (head);
3961 gfc_free_expr (mask);
3967 /* Match a FORALL statement. */
3970 gfc_match_forall (gfc_statement *st)
3972 gfc_forall_iterator *head;
3981 m0 = gfc_match_label ();
3982 if (m0 == MATCH_ERROR)
3985 m = gfc_match (" forall");
3989 m = match_forall_header (&head, &mask);
3990 if (m == MATCH_ERROR)
3995 if (gfc_match_eos () == MATCH_YES)
3997 *st = ST_FORALL_BLOCK;
3998 new_st.op = EXEC_FORALL;
4000 new_st.ext.forall_iterator = head;
4004 m = gfc_match_assignment ();
4005 if (m == MATCH_ERROR)
4009 m = gfc_match_pointer_assignment ();
4010 if (m == MATCH_ERROR)
4016 c = gfc_get_code ();
4018 c->loc = gfc_current_locus;
4020 gfc_clear_new_st ();
4021 new_st.op = EXEC_FORALL;
4023 new_st.ext.forall_iterator = head;
4024 new_st.block = gfc_get_code ();
4025 new_st.block->op = EXEC_FORALL;
4026 new_st.block->next = c;
4032 gfc_syntax_error (ST_FORALL);
4035 gfc_free_forall_iterator (head);
4036 gfc_free_expr (mask);
4037 gfc_free_statements (c);