1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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)
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 == '$'));
523 gfc_current_locus = old_loc;
529 /* Match a valid name for C, which is almost the same as for Fortran,
530 except that you can start with an underscore, etc.. It could have
531 been done by modifying the gfc_match_name, but this way other
532 things C allows can be added, such as no limits on the length.
533 Right now, the length is limited to the same thing as Fortran..
534 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
535 input characters from being automatically lower cased, since C is
536 case sensitive. The parameter, buffer, is used to return the name
537 that is matched. Return MATCH_ERROR if the name is too long
538 (though this is a self-imposed limit), MATCH_NO if what we're
539 seeing isn't a name, and MATCH_YES if we successfully match a C
543 gfc_match_name_C (char *buffer)
549 old_loc = gfc_current_locus;
550 gfc_gobble_whitespace ();
552 /* Get the next char (first possible char of name) and see if
553 it's valid for C (either a letter or an underscore). */
554 c = gfc_next_char_literal (1);
556 /* If the user put nothing expect spaces between the quotes, it is valid
557 and simply means there is no name= specifier and the name is the fortran
558 symbol name, all lowercase. */
559 if (c == '"' || c == '\'')
562 gfc_current_locus = old_loc;
566 if (!ISALPHA (c) && c != '_')
568 gfc_error ("Invalid C name in NAME= specifier at %C");
572 /* Continue to read valid variable name characters. */
577 /* C does not define a maximum length of variable names, to my
578 knowledge, but the compiler typically places a limit on them.
579 For now, i'll use the same as the fortran limit for simplicity,
580 but this may need to be changed to a dynamic buffer that can
581 be realloc'ed here if necessary, or more likely, a larger
583 if (i > gfc_option.max_identifier_length)
585 gfc_error ("Name at %C is too long");
589 old_loc = gfc_current_locus;
591 /* Get next char; param means we're in a string. */
592 c = gfc_next_char_literal (1);
593 } while (ISALNUM (c) || c == '_');
596 gfc_current_locus = old_loc;
598 /* See if we stopped because of whitespace. */
601 gfc_gobble_whitespace ();
602 c = gfc_peek_char ();
603 if (c != '"' && c != '\'')
605 gfc_error ("Embedded space in NAME= specifier at %C");
610 /* If we stopped because we had an invalid character for a C name, report
611 that to the user by returning MATCH_NO. */
612 if (c != '"' && c != '\'')
614 gfc_error ("Invalid C name in NAME= specifier at %C");
622 /* Match a symbol on the input. Modifies the pointer to the symbol
623 pointer if successful. */
626 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
628 char buffer[GFC_MAX_SYMBOL_LEN + 1];
631 m = gfc_match_name (buffer);
636 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
637 ? MATCH_ERROR : MATCH_YES;
639 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
647 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
652 m = gfc_match_sym_tree (&st, host_assoc);
657 *matched_symbol = st->n.sym;
659 *matched_symbol = NULL;
662 *matched_symbol = NULL;
667 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
668 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
672 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
674 locus orig_loc = gfc_current_locus;
677 gfc_gobble_whitespace ();
678 ch = gfc_next_char ();
683 *result = INTRINSIC_PLUS;
688 *result = INTRINSIC_MINUS;
692 if (gfc_next_char () == '=')
695 *result = INTRINSIC_EQ;
701 if (gfc_peek_char () == '=')
705 *result = INTRINSIC_LE;
709 *result = INTRINSIC_LT;
713 if (gfc_peek_char () == '=')
717 *result = INTRINSIC_GE;
721 *result = INTRINSIC_GT;
725 if (gfc_peek_char () == '*')
729 *result = INTRINSIC_POWER;
733 *result = INTRINSIC_TIMES;
737 ch = gfc_peek_char ();
742 *result = INTRINSIC_NE;
749 *result = INTRINSIC_CONCAT;
753 *result = INTRINSIC_DIVIDE;
757 ch = gfc_next_char ();
761 if (gfc_next_char () == 'n'
762 && gfc_next_char () == 'd'
763 && gfc_next_char () == '.')
765 /* Matched ".and.". */
766 *result = INTRINSIC_AND;
772 if (gfc_next_char () == 'q')
774 ch = gfc_next_char ();
777 /* Matched ".eq.". */
778 *result = INTRINSIC_EQ_OS;
783 if (gfc_next_char () == '.')
785 /* Matched ".eqv.". */
786 *result = INTRINSIC_EQV;
794 ch = gfc_next_char ();
797 if (gfc_next_char () == '.')
799 /* Matched ".ge.". */
800 *result = INTRINSIC_GE_OS;
806 if (gfc_next_char () == '.')
808 /* Matched ".gt.". */
809 *result = INTRINSIC_GT_OS;
816 ch = gfc_next_char ();
819 if (gfc_next_char () == '.')
821 /* Matched ".le.". */
822 *result = INTRINSIC_LE_OS;
828 if (gfc_next_char () == '.')
830 /* Matched ".lt.". */
831 *result = INTRINSIC_LT_OS;
838 ch = gfc_next_char ();
841 ch = gfc_next_char ();
844 /* Matched ".ne.". */
845 *result = INTRINSIC_NE_OS;
850 if (gfc_next_char () == 'v'
851 && gfc_next_char () == '.')
853 /* Matched ".neqv.". */
854 *result = INTRINSIC_NEQV;
861 if (gfc_next_char () == 't'
862 && gfc_next_char () == '.')
864 /* Matched ".not.". */
865 *result = INTRINSIC_NOT;
872 if (gfc_next_char () == 'r'
873 && gfc_next_char () == '.')
875 /* Matched ".or.". */
876 *result = INTRINSIC_OR;
890 gfc_current_locus = orig_loc;
895 /* Match a loop control phrase:
897 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
899 If the final integer expression is not present, a constant unity
900 expression is returned. We don't return MATCH_ERROR until after
901 the equals sign is seen. */
904 gfc_match_iterator (gfc_iterator *iter, int init_flag)
906 char name[GFC_MAX_SYMBOL_LEN + 1];
907 gfc_expr *var, *e1, *e2, *e3;
911 /* Match the start of an iterator without affecting the symbol table. */
913 start = gfc_current_locus;
914 m = gfc_match (" %n =", name);
915 gfc_current_locus = start;
920 m = gfc_match_variable (&var, 0);
924 gfc_match_char ('=');
928 if (var->ref != NULL)
930 gfc_error ("Loop variable at %C cannot be a sub-component");
934 if (var->symtree->n.sym->attr.intent == INTENT_IN)
936 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
937 var->symtree->n.sym->name);
941 var->symtree->n.sym->attr.implied_index = 1;
943 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
946 if (m == MATCH_ERROR)
949 if (gfc_match_char (',') != MATCH_YES)
952 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
955 if (m == MATCH_ERROR)
958 if (gfc_match_char (',') != MATCH_YES)
960 e3 = gfc_int_expr (1);
964 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
965 if (m == MATCH_ERROR)
969 gfc_error ("Expected a step value in iterator at %C");
981 gfc_error ("Syntax error in iterator at %C");
992 /* Tries to match the next non-whitespace character on the input.
993 This subroutine does not return MATCH_ERROR. */
996 gfc_match_char (char c)
1000 where = gfc_current_locus;
1001 gfc_gobble_whitespace ();
1003 if (gfc_next_char () == c)
1006 gfc_current_locus = where;
1011 /* General purpose matching subroutine. The target string is a
1012 scanf-like format string in which spaces correspond to arbitrary
1013 whitespace (including no whitespace), characters correspond to
1014 themselves. The %-codes are:
1016 %% Literal percent sign
1017 %e Expression, pointer to a pointer is set
1018 %s Symbol, pointer to the symbol is set
1019 %n Name, character buffer is set to name
1020 %t Matches end of statement.
1021 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1022 %l Matches a statement label
1023 %v Matches a variable expression (an lvalue)
1024 % Matches a required space (in free form) and optional spaces. */
1027 gfc_match (const char *target, ...)
1029 gfc_st_label **label;
1038 old_loc = gfc_current_locus;
1039 va_start (argp, target);
1049 gfc_gobble_whitespace ();
1060 vp = va_arg (argp, void **);
1061 n = gfc_match_expr ((gfc_expr **) vp);
1072 vp = va_arg (argp, void **);
1073 n = gfc_match_variable ((gfc_expr **) vp, 0);
1084 vp = va_arg (argp, void **);
1085 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1096 np = va_arg (argp, char *);
1097 n = gfc_match_name (np);
1108 label = va_arg (argp, gfc_st_label **);
1109 n = gfc_match_st_label (label);
1120 ip = va_arg (argp, int *);
1121 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1132 if (gfc_match_eos () != MATCH_YES)
1140 if (gfc_match_space () == MATCH_YES)
1146 break; /* Fall through to character matcher. */
1149 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1153 if (c == gfc_next_char ())
1163 /* Clean up after a failed match. */
1164 gfc_current_locus = old_loc;
1165 va_start (argp, target);
1168 for (; matches > 0; matches--)
1170 while (*p++ != '%');
1178 /* Matches that don't have to be undone */
1183 (void) va_arg (argp, void **);
1188 vp = va_arg (argp, void **);
1189 gfc_free_expr (*vp);
1202 /*********************** Statement level matching **********************/
1204 /* Matches the start of a program unit, which is the program keyword
1205 followed by an obligatory symbol. */
1208 gfc_match_program (void)
1213 m = gfc_match ("% %s%t", &sym);
1217 gfc_error ("Invalid form of PROGRAM statement at %C");
1221 if (m == MATCH_ERROR)
1224 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1227 gfc_new_block = sym;
1233 /* Match a simple assignment statement. */
1236 gfc_match_assignment (void)
1238 gfc_expr *lvalue, *rvalue;
1242 old_loc = gfc_current_locus;
1245 m = gfc_match (" %v =", &lvalue);
1248 gfc_current_locus = old_loc;
1249 gfc_free_expr (lvalue);
1253 if (lvalue->symtree->n.sym->attr.protected
1254 && lvalue->symtree->n.sym->attr.use_assoc)
1256 gfc_current_locus = old_loc;
1257 gfc_free_expr (lvalue);
1258 gfc_error ("Setting value of PROTECTED variable at %C");
1263 m = gfc_match (" %e%t", &rvalue);
1266 gfc_current_locus = old_loc;
1267 gfc_free_expr (lvalue);
1268 gfc_free_expr (rvalue);
1272 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1274 new_st.op = EXEC_ASSIGN;
1275 new_st.expr = lvalue;
1276 new_st.expr2 = rvalue;
1278 gfc_check_do_variable (lvalue->symtree);
1284 /* Match a pointer assignment statement. */
1287 gfc_match_pointer_assignment (void)
1289 gfc_expr *lvalue, *rvalue;
1293 old_loc = gfc_current_locus;
1295 lvalue = rvalue = NULL;
1297 m = gfc_match (" %v =>", &lvalue);
1304 m = gfc_match (" %e%t", &rvalue);
1308 if (lvalue->symtree->n.sym->attr.protected
1309 && lvalue->symtree->n.sym->attr.use_assoc)
1311 gfc_error ("Assigning to a PROTECTED pointer at %C");
1316 new_st.op = EXEC_POINTER_ASSIGN;
1317 new_st.expr = lvalue;
1318 new_st.expr2 = rvalue;
1323 gfc_current_locus = old_loc;
1324 gfc_free_expr (lvalue);
1325 gfc_free_expr (rvalue);
1330 /* We try to match an easy arithmetic IF statement. This only happens
1331 when just after having encountered a simple IF statement. This code
1332 is really duplicate with parts of the gfc_match_if code, but this is
1336 match_arithmetic_if (void)
1338 gfc_st_label *l1, *l2, *l3;
1342 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1346 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1347 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1348 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1350 gfc_free_expr (expr);
1354 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF statement "
1355 "at %C") == FAILURE)
1358 new_st.op = EXEC_ARITHMETIC_IF;
1368 /* The IF statement is a bit of a pain. First of all, there are three
1369 forms of it, the simple IF, the IF that starts a block and the
1372 There is a problem with the simple IF and that is the fact that we
1373 only have a single level of undo information on symbols. What this
1374 means is for a simple IF, we must re-match the whole IF statement
1375 multiple times in order to guarantee that the symbol table ends up
1376 in the proper state. */
1378 static match match_simple_forall (void);
1379 static match match_simple_where (void);
1382 gfc_match_if (gfc_statement *if_type)
1385 gfc_st_label *l1, *l2, *l3;
1386 locus old_loc, old_loc2;
1390 n = gfc_match_label ();
1391 if (n == MATCH_ERROR)
1394 old_loc = gfc_current_locus;
1396 m = gfc_match (" if ( %e", &expr);
1400 old_loc2 = gfc_current_locus;
1401 gfc_current_locus = old_loc;
1403 if (gfc_match_parens () == MATCH_ERROR)
1406 gfc_current_locus = old_loc2;
1408 if (gfc_match_char (')') != MATCH_YES)
1410 gfc_error ("Syntax error in IF-expression at %C");
1411 gfc_free_expr (expr);
1415 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1421 gfc_error ("Block label not appropriate for arithmetic IF "
1423 gfc_free_expr (expr);
1427 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1428 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1429 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1431 gfc_free_expr (expr);
1435 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF "
1436 "statement at %C") == FAILURE)
1439 new_st.op = EXEC_ARITHMETIC_IF;
1445 *if_type = ST_ARITHMETIC_IF;
1449 if (gfc_match (" then%t") == MATCH_YES)
1451 new_st.op = EXEC_IF;
1453 *if_type = ST_IF_BLOCK;
1459 gfc_error ("Block label is not appropriate for IF statement at %C");
1460 gfc_free_expr (expr);
1464 /* At this point the only thing left is a simple IF statement. At
1465 this point, n has to be MATCH_NO, so we don't have to worry about
1466 re-matching a block label. From what we've got so far, try
1467 matching an assignment. */
1469 *if_type = ST_SIMPLE_IF;
1471 m = gfc_match_assignment ();
1475 gfc_free_expr (expr);
1476 gfc_undo_symbols ();
1477 gfc_current_locus = old_loc;
1479 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1480 assignment was found. For MATCH_NO, continue to call the various
1482 if (m == MATCH_ERROR)
1485 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1487 m = gfc_match_pointer_assignment ();
1491 gfc_free_expr (expr);
1492 gfc_undo_symbols ();
1493 gfc_current_locus = old_loc;
1495 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1497 /* Look at the next keyword to see which matcher to call. Matching
1498 the keyword doesn't affect the symbol table, so we don't have to
1499 restore between tries. */
1501 #define match(string, subr, statement) \
1502 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1506 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1507 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1508 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1509 match ("call", gfc_match_call, ST_CALL)
1510 match ("close", gfc_match_close, ST_CLOSE)
1511 match ("continue", gfc_match_continue, ST_CONTINUE)
1512 match ("cycle", gfc_match_cycle, ST_CYCLE)
1513 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1514 match ("end file", gfc_match_endfile, ST_END_FILE)
1515 match ("exit", gfc_match_exit, ST_EXIT)
1516 match ("flush", gfc_match_flush, ST_FLUSH)
1517 match ("forall", match_simple_forall, ST_FORALL)
1518 match ("go to", gfc_match_goto, ST_GOTO)
1519 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1520 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1521 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1522 match ("open", gfc_match_open, ST_OPEN)
1523 match ("pause", gfc_match_pause, ST_NONE)
1524 match ("print", gfc_match_print, ST_WRITE)
1525 match ("read", gfc_match_read, ST_READ)
1526 match ("return", gfc_match_return, ST_RETURN)
1527 match ("rewind", gfc_match_rewind, ST_REWIND)
1528 match ("stop", gfc_match_stop, ST_STOP)
1529 match ("where", match_simple_where, ST_WHERE)
1530 match ("write", gfc_match_write, ST_WRITE)
1532 /* The gfc_match_assignment() above may have returned a MATCH_NO
1533 where the assignment was to a named constant. Check that
1534 special case here. */
1535 m = gfc_match_assignment ();
1538 gfc_error ("Cannot assign to a named constant at %C");
1539 gfc_free_expr (expr);
1540 gfc_undo_symbols ();
1541 gfc_current_locus = old_loc;
1545 /* All else has failed, so give up. See if any of the matchers has
1546 stored an error message of some sort. */
1547 if (gfc_error_check () == 0)
1548 gfc_error ("Unclassifiable statement in IF-clause at %C");
1550 gfc_free_expr (expr);
1555 gfc_error ("Syntax error in IF-clause at %C");
1558 gfc_free_expr (expr);
1562 /* At this point, we've matched the single IF and the action clause
1563 is in new_st. Rearrange things so that the IF statement appears
1566 p = gfc_get_code ();
1567 p->next = gfc_get_code ();
1569 p->next->loc = gfc_current_locus;
1574 gfc_clear_new_st ();
1576 new_st.op = EXEC_IF;
1585 /* Match an ELSE statement. */
1588 gfc_match_else (void)
1590 char name[GFC_MAX_SYMBOL_LEN + 1];
1592 if (gfc_match_eos () == MATCH_YES)
1595 if (gfc_match_name (name) != MATCH_YES
1596 || gfc_current_block () == NULL
1597 || gfc_match_eos () != MATCH_YES)
1599 gfc_error ("Unexpected junk after ELSE statement at %C");
1603 if (strcmp (name, gfc_current_block ()->name) != 0)
1605 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1606 name, gfc_current_block ()->name);
1614 /* Match an ELSE IF statement. */
1617 gfc_match_elseif (void)
1619 char name[GFC_MAX_SYMBOL_LEN + 1];
1623 m = gfc_match (" ( %e ) then", &expr);
1627 if (gfc_match_eos () == MATCH_YES)
1630 if (gfc_match_name (name) != MATCH_YES
1631 || gfc_current_block () == NULL
1632 || gfc_match_eos () != MATCH_YES)
1634 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1638 if (strcmp (name, gfc_current_block ()->name) != 0)
1640 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1641 name, gfc_current_block ()->name);
1646 new_st.op = EXEC_IF;
1651 gfc_free_expr (expr);
1656 /* Free a gfc_iterator structure. */
1659 gfc_free_iterator (gfc_iterator *iter, int flag)
1665 gfc_free_expr (iter->var);
1666 gfc_free_expr (iter->start);
1667 gfc_free_expr (iter->end);
1668 gfc_free_expr (iter->step);
1675 /* Match a DO statement. */
1680 gfc_iterator iter, *ip;
1682 gfc_st_label *label;
1685 old_loc = gfc_current_locus;
1688 iter.var = iter.start = iter.end = iter.step = NULL;
1690 m = gfc_match_label ();
1691 if (m == MATCH_ERROR)
1694 if (gfc_match (" do") != MATCH_YES)
1697 m = gfc_match_st_label (&label);
1698 if (m == MATCH_ERROR)
1701 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1703 if (gfc_match_eos () == MATCH_YES)
1705 iter.end = gfc_logical_expr (1, NULL);
1706 new_st.op = EXEC_DO_WHILE;
1710 /* Match an optional comma, if no comma is found, a space is obligatory. */
1711 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1714 /* See if we have a DO WHILE. */
1715 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1717 new_st.op = EXEC_DO_WHILE;
1721 /* The abortive DO WHILE may have done something to the symbol
1722 table, so we start over. */
1723 gfc_undo_symbols ();
1724 gfc_current_locus = old_loc;
1726 gfc_match_label (); /* This won't error. */
1727 gfc_match (" do "); /* This will work. */
1729 gfc_match_st_label (&label); /* Can't error out. */
1730 gfc_match_char (','); /* Optional comma. */
1732 m = gfc_match_iterator (&iter, 0);
1735 if (m == MATCH_ERROR)
1738 iter.var->symtree->n.sym->attr.implied_index = 0;
1739 gfc_check_do_variable (iter.var->symtree);
1741 if (gfc_match_eos () != MATCH_YES)
1743 gfc_syntax_error (ST_DO);
1747 new_st.op = EXEC_DO;
1751 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1754 new_st.label = label;
1756 if (new_st.op == EXEC_DO_WHILE)
1757 new_st.expr = iter.end;
1760 new_st.ext.iterator = ip = gfc_get_iterator ();
1767 gfc_free_iterator (&iter, 0);
1773 /* Match an EXIT or CYCLE statement. */
1776 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1778 gfc_state_data *p, *o;
1782 if (gfc_match_eos () == MATCH_YES)
1786 m = gfc_match ("% %s%t", &sym);
1787 if (m == MATCH_ERROR)
1791 gfc_syntax_error (st);
1795 if (sym->attr.flavor != FL_LABEL)
1797 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1798 sym->name, gfc_ascii_statement (st));
1803 /* Find the loop mentioned specified by the label (or lack of a label). */
1804 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1805 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1807 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1813 gfc_error ("%s statement at %C is not within a loop",
1814 gfc_ascii_statement (st));
1816 gfc_error ("%s statement at %C is not within loop '%s'",
1817 gfc_ascii_statement (st), sym->name);
1824 gfc_error ("%s statement at %C leaving OpenMP structured block",
1825 gfc_ascii_statement (st));
1828 else if (st == ST_EXIT
1829 && p->previous != NULL
1830 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1831 && (p->previous->head->op == EXEC_OMP_DO
1832 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1834 gcc_assert (p->previous->head->next != NULL);
1835 gcc_assert (p->previous->head->next->op == EXEC_DO
1836 || p->previous->head->next->op == EXEC_DO_WHILE);
1837 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1841 /* Save the first statement in the loop - needed by the backend. */
1842 new_st.ext.whichloop = p->head;
1850 /* Match the EXIT statement. */
1853 gfc_match_exit (void)
1855 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1859 /* Match the CYCLE statement. */
1862 gfc_match_cycle (void)
1864 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1868 /* Match a number or character constant after a STOP or PAUSE statement. */
1871 gfc_match_stopcode (gfc_statement st)
1881 if (gfc_match_eos () != MATCH_YES)
1883 m = gfc_match_small_literal_int (&stop_code, &cnt);
1884 if (m == MATCH_ERROR)
1887 if (m == MATCH_YES && cnt > 5)
1889 gfc_error ("Too many digits in STOP code at %C");
1895 /* Try a character constant. */
1896 m = gfc_match_expr (&e);
1897 if (m == MATCH_ERROR)
1901 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1905 if (gfc_match_eos () != MATCH_YES)
1909 if (gfc_pure (NULL))
1911 gfc_error ("%s statement not allowed in PURE procedure at %C",
1912 gfc_ascii_statement (st));
1916 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1918 new_st.ext.stop_code = stop_code;
1923 gfc_syntax_error (st);
1932 /* Match the (deprecated) PAUSE statement. */
1935 gfc_match_pause (void)
1939 m = gfc_match_stopcode (ST_PAUSE);
1942 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
1951 /* Match the STOP statement. */
1954 gfc_match_stop (void)
1956 return gfc_match_stopcode (ST_STOP);
1960 /* Match a CONTINUE statement. */
1963 gfc_match_continue (void)
1965 if (gfc_match_eos () != MATCH_YES)
1967 gfc_syntax_error (ST_CONTINUE);
1971 new_st.op = EXEC_CONTINUE;
1976 /* Match the (deprecated) ASSIGN statement. */
1979 gfc_match_assign (void)
1982 gfc_st_label *label;
1984 if (gfc_match (" %l", &label) == MATCH_YES)
1986 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1988 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1990 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
1995 expr->symtree->n.sym->attr.assign = 1;
1997 new_st.op = EXEC_LABEL_ASSIGN;
1998 new_st.label = label;
2007 /* Match the GO TO statement. As a computed GOTO statement is
2008 matched, it is transformed into an equivalent SELECT block. No
2009 tree is necessary, and the resulting jumps-to-jumps are
2010 specifically optimized away by the back end. */
2013 gfc_match_goto (void)
2015 gfc_code *head, *tail;
2018 gfc_st_label *label;
2022 if (gfc_match (" %l%t", &label) == MATCH_YES)
2024 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2027 new_st.op = EXEC_GOTO;
2028 new_st.label = label;
2032 /* The assigned GO TO statement. */
2034 if (gfc_match_variable (&expr, 0) == MATCH_YES)
2036 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
2041 new_st.op = EXEC_GOTO;
2044 if (gfc_match_eos () == MATCH_YES)
2047 /* Match label list. */
2048 gfc_match_char (',');
2049 if (gfc_match_char ('(') != MATCH_YES)
2051 gfc_syntax_error (ST_GOTO);
2058 m = gfc_match_st_label (&label);
2062 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2066 head = tail = gfc_get_code ();
2069 tail->block = gfc_get_code ();
2073 tail->label = label;
2074 tail->op = EXEC_GOTO;
2076 while (gfc_match_char (',') == MATCH_YES);
2078 if (gfc_match (")%t") != MATCH_YES)
2083 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2086 new_st.block = head;
2091 /* Last chance is a computed GO TO statement. */
2092 if (gfc_match_char ('(') != MATCH_YES)
2094 gfc_syntax_error (ST_GOTO);
2103 m = gfc_match_st_label (&label);
2107 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2111 head = tail = gfc_get_code ();
2114 tail->block = gfc_get_code ();
2118 cp = gfc_get_case ();
2119 cp->low = cp->high = gfc_int_expr (i++);
2121 tail->op = EXEC_SELECT;
2122 tail->ext.case_list = cp;
2124 tail->next = gfc_get_code ();
2125 tail->next->op = EXEC_GOTO;
2126 tail->next->label = label;
2128 while (gfc_match_char (',') == MATCH_YES);
2130 if (gfc_match_char (')') != MATCH_YES)
2135 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2139 /* Get the rest of the statement. */
2140 gfc_match_char (',');
2142 if (gfc_match (" %e%t", &expr) != MATCH_YES)
2145 /* At this point, a computed GOTO has been fully matched and an
2146 equivalent SELECT statement constructed. */
2148 new_st.op = EXEC_SELECT;
2151 /* Hack: For a "real" SELECT, the expression is in expr. We put
2152 it in expr2 so we can distinguish then and produce the correct
2154 new_st.expr2 = expr;
2155 new_st.block = head;
2159 gfc_syntax_error (ST_GOTO);
2161 gfc_free_statements (head);
2166 /* Frees a list of gfc_alloc structures. */
2169 gfc_free_alloc_list (gfc_alloc *p)
2176 gfc_free_expr (p->expr);
2182 /* Match an ALLOCATE statement. */
2185 gfc_match_allocate (void)
2187 gfc_alloc *head, *tail;
2194 if (gfc_match_char ('(') != MATCH_YES)
2200 head = tail = gfc_get_alloc ();
2203 tail->next = gfc_get_alloc ();
2207 m = gfc_match_variable (&tail->expr, 0);
2210 if (m == MATCH_ERROR)
2213 if (gfc_check_do_variable (tail->expr->symtree))
2217 && gfc_impure_variable (tail->expr->symtree->n.sym))
2219 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
2224 if (tail->expr->ts.type == BT_DERIVED)
2225 tail->expr->ts.derived = gfc_use_derived (tail->expr->ts.derived);
2227 if (gfc_match_char (',') != MATCH_YES)
2230 m = gfc_match (" stat = %v", &stat);
2231 if (m == MATCH_ERROR)
2241 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
2243 gfc_error ("STAT variable '%s' of ALLOCATE statement at %C cannot "
2244 "be INTENT(IN)", stat->symtree->n.sym->name);
2248 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
2250 gfc_error ("Illegal STAT variable in ALLOCATE statement at %C "
2251 "for a PURE procedure");
2255 is_variable = false;
2256 if (stat->symtree->n.sym->attr.flavor == FL_VARIABLE)
2258 else if (stat->symtree->n.sym->attr.function
2259 && stat->symtree->n.sym->result == stat->symtree->n.sym
2260 && (gfc_current_ns->proc_name == stat->symtree->n.sym
2261 || (gfc_current_ns->parent
2262 && gfc_current_ns->parent->proc_name
2263 == stat->symtree->n.sym)))
2265 else if (gfc_current_ns->entries
2266 && stat->symtree->n.sym->result == stat->symtree->n.sym)
2269 for (el = gfc_current_ns->entries; el; el = el->next)
2270 if (el->sym == stat->symtree->n.sym)
2275 else if (gfc_current_ns->parent && gfc_current_ns->parent->entries
2276 && stat->symtree->n.sym->result == stat->symtree->n.sym)
2279 for (el = gfc_current_ns->parent->entries; el; el = el->next)
2280 if (el->sym == stat->symtree->n.sym)
2288 gfc_error ("STAT expression at %C must be a variable");
2292 gfc_check_do_variable(stat->symtree);
2295 if (gfc_match (" )%t") != MATCH_YES)
2298 new_st.op = EXEC_ALLOCATE;
2300 new_st.ext.alloc_list = head;
2305 gfc_syntax_error (ST_ALLOCATE);
2308 gfc_free_expr (stat);
2309 gfc_free_alloc_list (head);
2314 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
2315 a set of pointer assignments to intrinsic NULL(). */
2318 gfc_match_nullify (void)
2326 if (gfc_match_char ('(') != MATCH_YES)
2331 m = gfc_match_variable (&p, 0);
2332 if (m == MATCH_ERROR)
2337 if (gfc_check_do_variable (p->symtree))
2340 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
2342 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
2346 /* build ' => NULL() '. */
2347 e = gfc_get_expr ();
2348 e->where = gfc_current_locus;
2349 e->expr_type = EXPR_NULL;
2350 e->ts.type = BT_UNKNOWN;
2352 /* Chain to list. */
2357 tail->next = gfc_get_code ();
2361 tail->op = EXEC_POINTER_ASSIGN;
2365 if (gfc_match (" )%t") == MATCH_YES)
2367 if (gfc_match_char (',') != MATCH_YES)
2374 gfc_syntax_error (ST_NULLIFY);
2377 gfc_free_statements (new_st.next);
2382 /* Match a DEALLOCATE statement. */
2385 gfc_match_deallocate (void)
2387 gfc_alloc *head, *tail;
2394 if (gfc_match_char ('(') != MATCH_YES)
2400 head = tail = gfc_get_alloc ();
2403 tail->next = gfc_get_alloc ();
2407 m = gfc_match_variable (&tail->expr, 0);
2408 if (m == MATCH_ERROR)
2413 if (gfc_check_do_variable (tail->expr->symtree))
2417 && gfc_impure_variable (tail->expr->symtree->n.sym))
2419 gfc_error ("Illegal deallocate-expression in DEALLOCATE at %C "
2420 "for a PURE procedure");
2424 if (gfc_match_char (',') != MATCH_YES)
2427 m = gfc_match (" stat = %v", &stat);
2428 if (m == MATCH_ERROR)
2436 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
2438 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
2439 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
2443 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
2445 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
2446 "for a PURE procedure");
2450 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
2452 gfc_error ("STAT expression at %C must be a variable");
2456 gfc_check_do_variable(stat->symtree);
2459 if (gfc_match (" )%t") != MATCH_YES)
2462 new_st.op = EXEC_DEALLOCATE;
2464 new_st.ext.alloc_list = head;
2469 gfc_syntax_error (ST_DEALLOCATE);
2472 gfc_free_expr (stat);
2473 gfc_free_alloc_list (head);
2478 /* Match a RETURN statement. */
2481 gfc_match_return (void)
2485 gfc_compile_state s;
2489 if (gfc_match_eos () == MATCH_YES)
2492 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2494 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2499 if (gfc_current_form == FORM_FREE)
2501 /* The following are valid, so we can't require a blank after the
2505 c = gfc_peek_char ();
2506 if (ISALPHA (c) || ISDIGIT (c))
2510 m = gfc_match (" %e%t", &e);
2513 if (m == MATCH_ERROR)
2516 gfc_syntax_error (ST_RETURN);
2523 gfc_enclosing_unit (&s);
2524 if (s == COMP_PROGRAM
2525 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2526 "main program at %C") == FAILURE)
2529 new_st.op = EXEC_RETURN;
2536 /* Match a CALL statement. The tricky part here are possible
2537 alternate return specifiers. We handle these by having all
2538 "subroutines" actually return an integer via a register that gives
2539 the return number. If the call specifies alternate returns, we
2540 generate code for a SELECT statement whose case clauses contain
2541 GOTOs to the various labels. */
2544 gfc_match_call (void)
2546 char name[GFC_MAX_SYMBOL_LEN + 1];
2547 gfc_actual_arglist *a, *arglist;
2557 m = gfc_match ("% %n", name);
2563 if (gfc_get_ha_sym_tree (name, &st))
2568 /* If it does not seem to be callable... */
2569 if (!sym->attr.generic
2570 && !sym->attr.subroutine)
2572 if (!(sym->attr.external && !sym->attr.referenced))
2574 /* ...create a symbol in this scope... */
2575 if (sym->ns != gfc_current_ns
2576 && gfc_get_sym_tree (name, NULL, &st) == 1)
2579 if (sym != st->n.sym)
2583 /* ...and then to try to make the symbol into a subroutine. */
2584 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2588 gfc_set_sym_referenced (sym);
2590 if (gfc_match_eos () != MATCH_YES)
2592 m = gfc_match_actual_arglist (1, &arglist);
2595 if (m == MATCH_ERROR)
2598 if (gfc_match_eos () != MATCH_YES)
2602 /* If any alternate return labels were found, construct a SELECT
2603 statement that will jump to the right place. */
2606 for (a = arglist; a; a = a->next)
2607 if (a->expr == NULL)
2612 gfc_symtree *select_st;
2613 gfc_symbol *select_sym;
2614 char name[GFC_MAX_SYMBOL_LEN + 1];
2616 new_st.next = c = gfc_get_code ();
2617 c->op = EXEC_SELECT;
2618 sprintf (name, "_result_%s", sym->name);
2619 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
2621 select_sym = select_st->n.sym;
2622 select_sym->ts.type = BT_INTEGER;
2623 select_sym->ts.kind = gfc_default_integer_kind;
2624 gfc_set_sym_referenced (select_sym);
2625 c->expr = gfc_get_expr ();
2626 c->expr->expr_type = EXPR_VARIABLE;
2627 c->expr->symtree = select_st;
2628 c->expr->ts = select_sym->ts;
2629 c->expr->where = gfc_current_locus;
2632 for (a = arglist; a; a = a->next)
2634 if (a->expr != NULL)
2637 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2642 c->block = gfc_get_code ();
2644 c->op = EXEC_SELECT;
2646 new_case = gfc_get_case ();
2647 new_case->high = new_case->low = gfc_int_expr (i);
2648 c->ext.case_list = new_case;
2650 c->next = gfc_get_code ();
2651 c->next->op = EXEC_GOTO;
2652 c->next->label = a->label;
2656 new_st.op = EXEC_CALL;
2657 new_st.symtree = st;
2658 new_st.ext.actual = arglist;
2663 gfc_syntax_error (ST_CALL);
2666 gfc_free_actual_arglist (arglist);
2671 /* Given a name, return a pointer to the common head structure,
2672 creating it if it does not exist. If FROM_MODULE is nonzero, we
2673 mangle the name so that it doesn't interfere with commons defined
2674 in the using namespace.
2675 TODO: Add to global symbol tree. */
2678 gfc_get_common (const char *name, int from_module)
2681 static int serial = 0;
2682 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
2686 /* A use associated common block is only needed to correctly layout
2687 the variables it contains. */
2688 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2689 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2693 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2696 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2699 if (st->n.common == NULL)
2701 st->n.common = gfc_get_common_head ();
2702 st->n.common->where = gfc_current_locus;
2703 strcpy (st->n.common->name, name);
2706 return st->n.common;
2710 /* Match a common block name. */
2712 match match_common_name (char *name)
2716 if (gfc_match_char ('/') == MATCH_NO)
2722 if (gfc_match_char ('/') == MATCH_YES)
2728 m = gfc_match_name (name);
2730 if (m == MATCH_ERROR)
2732 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2735 gfc_error ("Syntax error in common block name at %C");
2740 /* Match a COMMON statement. */
2743 gfc_match_common (void)
2745 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2746 char name[GFC_MAX_SYMBOL_LEN + 1];
2753 old_blank_common = gfc_current_ns->blank_common.head;
2754 if (old_blank_common)
2756 while (old_blank_common->common_next)
2757 old_blank_common = old_blank_common->common_next;
2764 m = match_common_name (name);
2765 if (m == MATCH_ERROR)
2768 gsym = gfc_get_gsymbol (name);
2769 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2771 gfc_error ("Symbol '%s' at %C is already an external symbol that "
2772 "is not COMMON", name);
2776 if (gsym->type == GSYM_UNKNOWN)
2778 gsym->type = GSYM_COMMON;
2779 gsym->where = gfc_current_locus;
2785 if (name[0] == '\0')
2787 if (gfc_current_ns->is_block_data)
2789 gfc_warning ("BLOCK DATA unit cannot contain blank COMMON "
2792 t = &gfc_current_ns->blank_common;
2793 if (t->head == NULL)
2794 t->where = gfc_current_locus;
2798 t = gfc_get_common (name, 0);
2807 while (tail->common_next)
2808 tail = tail->common_next;
2811 /* Grab the list of symbols. */
2814 m = gfc_match_symbol (&sym, 0);
2815 if (m == MATCH_ERROR)
2820 /* Store a ref to the common block for error checking. */
2821 sym->common_block = t;
2823 /* See if we know the current common block is bind(c), and if
2824 so, then see if we can check if the symbol is (which it'll
2825 need to be). This can happen if the bind(c) attr stmt was
2826 applied to the common block, and the variable(s) already
2827 defined, before declaring the common block. */
2828 if (t->is_bind_c == 1)
2830 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
2832 /* If we find an error, just print it and continue,
2833 cause it's just semantic, and we can see if there
2835 gfc_error_now ("Variable '%s' at %L in common block '%s' "
2836 "at %C must be declared with a C "
2837 "interoperable kind since common block "
2839 sym->name, &(sym->declared_at), t->name,
2843 if (sym->attr.is_bind_c == 1)
2844 gfc_error_now ("Variable '%s' in common block "
2845 "'%s' at %C can not be bind(c) since "
2846 "it is not global", sym->name, t->name);
2849 if (sym->attr.in_common)
2851 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2856 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
2857 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
2859 if (gfc_notify_std (GFC_STD_GNU, "Initialized symbol '%s' at %C "
2860 "can only be COMMON in "
2861 "BLOCK DATA", sym->name)
2866 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2870 tail->common_next = sym;
2876 /* Deal with an optional array specification after the
2878 m = gfc_match_array_spec (&as);
2879 if (m == MATCH_ERROR)
2884 if (as->type != AS_EXPLICIT)
2886 gfc_error ("Array specification for symbol '%s' in COMMON "
2887 "at %C must be explicit", sym->name);
2891 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2894 if (sym->attr.pointer)
2896 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
2897 "POINTER array", sym->name);
2906 sym->common_head = t;
2908 /* Check to see if the symbol is already in an equivalence group.
2909 If it is, set the other members as being in common. */
2910 if (sym->attr.in_equivalence)
2912 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2914 for (e2 = e1; e2; e2 = e2->eq)
2915 if (e2->expr->symtree->n.sym == sym)
2922 for (e2 = e1; e2; e2 = e2->eq)
2924 other = e2->expr->symtree->n.sym;
2925 if (other->common_head
2926 && other->common_head != sym->common_head)
2928 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2929 "%C is being indirectly equivalenced to "
2930 "another COMMON block '%s'",
2931 sym->name, sym->common_head->name,
2932 other->common_head->name);
2935 other->attr.in_common = 1;
2936 other->common_head = t;
2942 gfc_gobble_whitespace ();
2943 if (gfc_match_eos () == MATCH_YES)
2945 if (gfc_peek_char () == '/')
2947 if (gfc_match_char (',') != MATCH_YES)
2949 gfc_gobble_whitespace ();
2950 if (gfc_peek_char () == '/')
2959 gfc_syntax_error (ST_COMMON);
2962 if (old_blank_common)
2963 old_blank_common->common_next = NULL;
2965 gfc_current_ns->blank_common.head = NULL;
2966 gfc_free_array_spec (as);
2971 /* Match a BLOCK DATA program unit. */
2974 gfc_match_block_data (void)
2976 char name[GFC_MAX_SYMBOL_LEN + 1];
2980 if (gfc_match_eos () == MATCH_YES)
2982 gfc_new_block = NULL;
2986 m = gfc_match ("% %n%t", name);
2990 if (gfc_get_symbol (name, NULL, &sym))
2993 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2996 gfc_new_block = sym;
3002 /* Free a namelist structure. */
3005 gfc_free_namelist (gfc_namelist *name)
3009 for (; name; name = n)
3017 /* Match a NAMELIST statement. */
3020 gfc_match_namelist (void)
3022 gfc_symbol *group_name, *sym;
3026 m = gfc_match (" / %s /", &group_name);
3029 if (m == MATCH_ERROR)
3034 if (group_name->ts.type != BT_UNKNOWN)
3036 gfc_error ("Namelist group name '%s' at %C already has a basic "
3037 "type of %s", group_name->name,
3038 gfc_typename (&group_name->ts));
3042 if (group_name->attr.flavor == FL_NAMELIST
3043 && group_name->attr.use_assoc
3044 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
3045 "at %C already is USE associated and can"
3046 "not be respecified.", group_name->name)
3050 if (group_name->attr.flavor != FL_NAMELIST
3051 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
3052 group_name->name, NULL) == FAILURE)
3057 m = gfc_match_symbol (&sym, 1);
3060 if (m == MATCH_ERROR)
3063 if (sym->attr.in_namelist == 0
3064 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
3067 /* Use gfc_error_check here, rather than goto error, so that
3068 these are the only errors for the next two lines. */
3069 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
3071 gfc_error ("Assumed size array '%s' in namelist '%s' at "
3072 "%C is not allowed", sym->name, group_name->name);
3076 if (sym->ts.type == BT_CHARACTER && sym->ts.cl->length == NULL)
3078 gfc_error ("Assumed character length '%s' in namelist '%s' at "
3079 "%C is not allowed", sym->name, group_name->name);
3083 nl = gfc_get_namelist ();
3087 if (group_name->namelist == NULL)
3088 group_name->namelist = group_name->namelist_tail = nl;
3091 group_name->namelist_tail->next = nl;
3092 group_name->namelist_tail = nl;
3095 if (gfc_match_eos () == MATCH_YES)
3098 m = gfc_match_char (',');
3100 if (gfc_match_char ('/') == MATCH_YES)
3102 m2 = gfc_match (" %s /", &group_name);
3103 if (m2 == MATCH_YES)
3105 if (m2 == MATCH_ERROR)
3119 gfc_syntax_error (ST_NAMELIST);
3126 /* Match a MODULE statement. */
3129 gfc_match_module (void)
3133 m = gfc_match (" %s%t", &gfc_new_block);
3137 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
3138 gfc_new_block->name, NULL) == FAILURE)
3145 /* Free equivalence sets and lists. Recursively is the easiest way to
3149 gfc_free_equiv (gfc_equiv *eq)
3154 gfc_free_equiv (eq->eq);
3155 gfc_free_equiv (eq->next);
3156 gfc_free_expr (eq->expr);
3161 /* Match an EQUIVALENCE statement. */
3164 gfc_match_equivalence (void)
3166 gfc_equiv *eq, *set, *tail;
3170 gfc_common_head *common_head = NULL;
3178 eq = gfc_get_equiv ();
3182 eq->next = gfc_current_ns->equiv;
3183 gfc_current_ns->equiv = eq;
3185 if (gfc_match_char ('(') != MATCH_YES)
3189 common_flag = FALSE;
3194 m = gfc_match_equiv_variable (&set->expr);
3195 if (m == MATCH_ERROR)
3200 /* count the number of objects. */
3203 if (gfc_match_char ('%') == MATCH_YES)
3205 gfc_error ("Derived type component %C is not a "
3206 "permitted EQUIVALENCE member");
3210 for (ref = set->expr->ref; ref; ref = ref->next)
3211 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3213 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
3214 "be an array section");
3218 sym = set->expr->symtree->n.sym;
3220 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
3223 if (sym->attr.in_common)
3226 common_head = sym->common_head;
3229 if (gfc_match_char (')') == MATCH_YES)
3232 if (gfc_match_char (',') != MATCH_YES)
3235 set->eq = gfc_get_equiv ();
3241 gfc_error ("EQUIVALENCE at %C requires two or more objects");
3245 /* If one of the members of an equivalence is in common, then
3246 mark them all as being in common. Before doing this, check
3247 that members of the equivalence group are not in different
3250 for (set = eq; set; set = set->eq)
3252 sym = set->expr->symtree->n.sym;
3253 if (sym->common_head && sym->common_head != common_head)
3255 gfc_error ("Attempt to indirectly overlap COMMON "
3256 "blocks %s and %s by EQUIVALENCE at %C",
3257 sym->common_head->name, common_head->name);
3260 sym->attr.in_common = 1;
3261 sym->common_head = common_head;
3264 if (gfc_match_eos () == MATCH_YES)
3266 if (gfc_match_char (',') != MATCH_YES)
3273 gfc_syntax_error (ST_EQUIVALENCE);
3279 gfc_free_equiv (gfc_current_ns->equiv);
3280 gfc_current_ns->equiv = eq;
3286 /* Check that a statement function is not recursive. This is done by looking
3287 for the statement function symbol(sym) by looking recursively through its
3288 expression(e). If a reference to sym is found, true is returned.
3289 12.5.4 requires that any variable of function that is implicitly typed
3290 shall have that type confirmed by any subsequent type declaration. The
3291 implicit typing is conveniently done here. */
3293 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
3296 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
3302 switch (e->expr_type)
3305 if (e->symtree == NULL)
3308 /* Check the name before testing for nested recursion! */
3309 if (sym->name == e->symtree->n.sym->name)
3312 /* Catch recursion via other statement functions. */
3313 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
3314 && e->symtree->n.sym->value
3315 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
3318 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3319 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3324 if (e->symtree && sym->name == e->symtree->n.sym->name)
3327 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3328 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3340 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
3342 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
3346 /* Match a statement function declaration. It is so easy to match
3347 non-statement function statements with a MATCH_ERROR as opposed to
3348 MATCH_NO that we suppress error message in most cases. */
3351 gfc_match_st_function (void)
3353 gfc_error_buf old_error;
3358 m = gfc_match_symbol (&sym, 0);
3362 gfc_push_error (&old_error);
3364 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
3365 sym->name, NULL) == FAILURE)
3368 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
3371 m = gfc_match (" = %e%t", &expr);
3375 gfc_free_error (&old_error);
3376 if (m == MATCH_ERROR)
3379 if (recursive_stmt_fcn (expr, sym))
3381 gfc_error ("Statement function at %L is recursive", &expr->where);
3390 gfc_pop_error (&old_error);
3395 /***************** SELECT CASE subroutines ******************/
3397 /* Free a single case structure. */
3400 free_case (gfc_case *p)
3402 if (p->low == p->high)
3404 gfc_free_expr (p->low);
3405 gfc_free_expr (p->high);
3410 /* Free a list of case structures. */
3413 gfc_free_case_list (gfc_case *p)
3425 /* Match a single case selector. */
3428 match_case_selector (gfc_case **cp)
3433 c = gfc_get_case ();
3434 c->where = gfc_current_locus;
3436 if (gfc_match_char (':') == MATCH_YES)
3438 m = gfc_match_init_expr (&c->high);
3441 if (m == MATCH_ERROR)
3446 m = gfc_match_init_expr (&c->low);
3447 if (m == MATCH_ERROR)
3452 /* If we're not looking at a ':' now, make a range out of a single
3453 target. Else get the upper bound for the case range. */
3454 if (gfc_match_char (':') != MATCH_YES)
3458 m = gfc_match_init_expr (&c->high);
3459 if (m == MATCH_ERROR)
3461 /* MATCH_NO is fine. It's OK if nothing is there! */
3469 gfc_error ("Expected initialization expression in CASE at %C");
3477 /* Match the end of a case statement. */
3480 match_case_eos (void)
3482 char name[GFC_MAX_SYMBOL_LEN + 1];
3485 if (gfc_match_eos () == MATCH_YES)
3488 /* If the case construct doesn't have a case-construct-name, we
3489 should have matched the EOS. */
3490 if (!gfc_current_block ())
3492 gfc_error ("Expected the name of the SELECT CASE construct at %C");
3496 gfc_gobble_whitespace ();
3498 m = gfc_match_name (name);
3502 if (strcmp (name, gfc_current_block ()->name) != 0)
3504 gfc_error ("Expected case name of '%s' at %C",
3505 gfc_current_block ()->name);
3509 return gfc_match_eos ();
3513 /* Match a SELECT statement. */
3516 gfc_match_select (void)
3521 m = gfc_match_label ();
3522 if (m == MATCH_ERROR)
3525 m = gfc_match (" select case ( %e )%t", &expr);
3529 new_st.op = EXEC_SELECT;
3536 /* Match a CASE statement. */
3539 gfc_match_case (void)
3541 gfc_case *c, *head, *tail;
3546 if (gfc_current_state () != COMP_SELECT)
3548 gfc_error ("Unexpected CASE statement at %C");
3552 if (gfc_match ("% default") == MATCH_YES)
3554 m = match_case_eos ();
3557 if (m == MATCH_ERROR)
3560 new_st.op = EXEC_SELECT;
3561 c = gfc_get_case ();
3562 c->where = gfc_current_locus;
3563 new_st.ext.case_list = c;
3567 if (gfc_match_char ('(') != MATCH_YES)
3572 if (match_case_selector (&c) == MATCH_ERROR)
3582 if (gfc_match_char (')') == MATCH_YES)
3584 if (gfc_match_char (',') != MATCH_YES)
3588 m = match_case_eos ();
3591 if (m == MATCH_ERROR)
3594 new_st.op = EXEC_SELECT;
3595 new_st.ext.case_list = head;
3600 gfc_error ("Syntax error in CASE-specification at %C");
3603 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3607 /********************* WHERE subroutines ********************/
3609 /* Match the rest of a simple WHERE statement that follows an IF statement.
3613 match_simple_where (void)
3619 m = gfc_match (" ( %e )", &expr);
3623 m = gfc_match_assignment ();
3626 if (m == MATCH_ERROR)
3629 if (gfc_match_eos () != MATCH_YES)
3632 c = gfc_get_code ();
3636 c->next = gfc_get_code ();
3639 gfc_clear_new_st ();
3641 new_st.op = EXEC_WHERE;
3647 gfc_syntax_error (ST_WHERE);
3650 gfc_free_expr (expr);
3655 /* Match a WHERE statement. */
3658 gfc_match_where (gfc_statement *st)
3664 m0 = gfc_match_label ();
3665 if (m0 == MATCH_ERROR)
3668 m = gfc_match (" where ( %e )", &expr);
3672 if (gfc_match_eos () == MATCH_YES)
3674 *st = ST_WHERE_BLOCK;
3675 new_st.op = EXEC_WHERE;
3680 m = gfc_match_assignment ();
3682 gfc_syntax_error (ST_WHERE);
3686 gfc_free_expr (expr);
3690 /* We've got a simple WHERE statement. */
3692 c = gfc_get_code ();
3696 c->next = gfc_get_code ();
3699 gfc_clear_new_st ();
3701 new_st.op = EXEC_WHERE;
3708 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3709 new_st if successful. */
3712 gfc_match_elsewhere (void)
3714 char name[GFC_MAX_SYMBOL_LEN + 1];
3718 if (gfc_current_state () != COMP_WHERE)
3720 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3726 if (gfc_match_char ('(') == MATCH_YES)
3728 m = gfc_match_expr (&expr);
3731 if (m == MATCH_ERROR)
3734 if (gfc_match_char (')') != MATCH_YES)
3738 if (gfc_match_eos () != MATCH_YES)
3740 /* Only makes sense if we have a where-construct-name. */
3741 if (!gfc_current_block ())
3746 /* Better be a name at this point. */
3747 m = gfc_match_name (name);
3750 if (m == MATCH_ERROR)
3753 if (gfc_match_eos () != MATCH_YES)
3756 if (strcmp (name, gfc_current_block ()->name) != 0)
3758 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3759 name, gfc_current_block ()->name);
3764 new_st.op = EXEC_WHERE;
3769 gfc_syntax_error (ST_ELSEWHERE);
3772 gfc_free_expr (expr);
3777 /******************** FORALL subroutines ********************/
3779 /* Free a list of FORALL iterators. */
3782 gfc_free_forall_iterator (gfc_forall_iterator *iter)
3784 gfc_forall_iterator *next;
3789 gfc_free_expr (iter->var);
3790 gfc_free_expr (iter->start);
3791 gfc_free_expr (iter->end);
3792 gfc_free_expr (iter->stride);
3799 /* Match an iterator as part of a FORALL statement. The format is:
3801 <var> = <start>:<end>[:<stride>]
3803 On MATCH_NO, the caller tests for the possibility that there is a
3804 scalar mask expression. */
3807 match_forall_iterator (gfc_forall_iterator **result)
3809 gfc_forall_iterator *iter;
3813 where = gfc_current_locus;
3814 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3816 m = gfc_match_expr (&iter->var);
3820 if (gfc_match_char ('=') != MATCH_YES
3821 || iter->var->expr_type != EXPR_VARIABLE)
3827 m = gfc_match_expr (&iter->start);
3831 if (gfc_match_char (':') != MATCH_YES)
3834 m = gfc_match_expr (&iter->end);
3837 if (m == MATCH_ERROR)
3840 if (gfc_match_char (':') == MATCH_NO)
3841 iter->stride = gfc_int_expr (1);
3844 m = gfc_match_expr (&iter->stride);
3847 if (m == MATCH_ERROR)
3851 /* Mark the iteration variable's symbol as used as a FORALL index. */
3852 iter->var->symtree->n.sym->forall_index = true;
3858 gfc_error ("Syntax error in FORALL iterator at %C");
3863 gfc_current_locus = where;
3864 gfc_free_forall_iterator (iter);
3869 /* Match the header of a FORALL statement. */
3872 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
3874 gfc_forall_iterator *head, *tail, *new;
3878 gfc_gobble_whitespace ();
3883 if (gfc_match_char ('(') != MATCH_YES)
3886 m = match_forall_iterator (&new);
3887 if (m == MATCH_ERROR)
3896 if (gfc_match_char (',') != MATCH_YES)
3899 m = match_forall_iterator (&new);
3900 if (m == MATCH_ERROR)
3910 /* Have to have a mask expression. */
3912 m = gfc_match_expr (&msk);
3915 if (m == MATCH_ERROR)
3921 if (gfc_match_char (')') == MATCH_NO)
3929 gfc_syntax_error (ST_FORALL);
3932 gfc_free_expr (msk);
3933 gfc_free_forall_iterator (head);
3938 /* Match the rest of a simple FORALL statement that follows an
3942 match_simple_forall (void)
3944 gfc_forall_iterator *head;
3953 m = match_forall_header (&head, &mask);
3960 m = gfc_match_assignment ();
3962 if (m == MATCH_ERROR)
3966 m = gfc_match_pointer_assignment ();
3967 if (m == MATCH_ERROR)
3973 c = gfc_get_code ();
3975 c->loc = gfc_current_locus;
3977 if (gfc_match_eos () != MATCH_YES)
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 ();
3986 new_st.block->op = EXEC_FORALL;
3987 new_st.block->next = c;
3992 gfc_syntax_error (ST_FORALL);
3995 gfc_free_forall_iterator (head);
3996 gfc_free_expr (mask);
4002 /* Match a FORALL statement. */
4005 gfc_match_forall (gfc_statement *st)
4007 gfc_forall_iterator *head;
4016 m0 = gfc_match_label ();
4017 if (m0 == MATCH_ERROR)
4020 m = gfc_match (" forall");
4024 m = match_forall_header (&head, &mask);
4025 if (m == MATCH_ERROR)
4030 if (gfc_match_eos () == MATCH_YES)
4032 *st = ST_FORALL_BLOCK;
4033 new_st.op = EXEC_FORALL;
4035 new_st.ext.forall_iterator = head;
4039 m = gfc_match_assignment ();
4040 if (m == MATCH_ERROR)
4044 m = gfc_match_pointer_assignment ();
4045 if (m == MATCH_ERROR)
4051 c = gfc_get_code ();
4053 c->loc = gfc_current_locus;
4055 gfc_clear_new_st ();
4056 new_st.op = EXEC_FORALL;
4058 new_st.ext.forall_iterator = head;
4059 new_st.block = gfc_get_code ();
4060 new_st.block->op = EXEC_FORALL;
4061 new_st.block->next = c;
4067 gfc_syntax_error (ST_FORALL);
4070 gfc_free_forall_iterator (head);
4071 gfc_free_expr (mask);
4072 gfc_free_statements (c);