1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
30 /* For debugging and diagnostic purposes. Return the textual representation
31 of the intrinsic operator OP. */
33 gfc_op2string (gfc_intrinsic_op op)
41 case INTRINSIC_UMINUS:
47 case INTRINSIC_CONCAT:
51 case INTRINSIC_DIVIDE:
90 case INTRINSIC_ASSIGN:
93 case INTRINSIC_PARENTHESES:
100 gfc_internal_error ("gfc_op2string(): Bad code");
105 /******************** Generic matching subroutines ************************/
107 /* This function scans the current statement counting the opened and closed
108 parenthesis to make sure they are balanced. */
111 gfc_match_parens (void)
113 locus old_loc, where;
114 int c, count, instring;
117 old_loc = gfc_current_locus;
124 c = gfc_next_char_literal (instring);
127 if (quote == ' ' && ((c == '\'') || (c == '"')))
133 if (quote != ' ' && c == quote)
140 if (c == '(' && quote == ' ')
143 where = gfc_current_locus;
145 if (c == ')' && quote == ' ')
148 where = gfc_current_locus;
152 gfc_current_locus = old_loc;
156 gfc_error ("Missing ')' in statement before %L", &where);
161 gfc_error ("Missing '(' in statement before %L", &where);
169 /* See if the next character is a special character that has
170 escaped by a \ via the -fbackslash option. */
173 gfc_match_special_char (int *c)
180 switch (gfc_next_char_literal (1))
210 /* Unknown backslash codes are simply not expanded. */
219 /* In free form, match at least one space. Always matches in fixed
223 gfc_match_space (void)
228 if (gfc_current_form == FORM_FIXED)
231 old_loc = gfc_current_locus;
233 c = gfc_next_char ();
234 if (!gfc_is_whitespace (c))
236 gfc_current_locus = old_loc;
240 gfc_gobble_whitespace ();
246 /* Match an end of statement. End of statement is optional
247 whitespace, followed by a ';' or '\n' or comment '!'. If a
248 semicolon is found, we continue to eat whitespace and semicolons. */
260 old_loc = gfc_current_locus;
261 gfc_gobble_whitespace ();
263 c = gfc_next_char ();
269 c = gfc_next_char ();
286 gfc_current_locus = old_loc;
287 return (flag) ? MATCH_YES : MATCH_NO;
291 /* Match a literal integer on the input, setting the value on
292 MATCH_YES. Literal ints occur in kind-parameters as well as
293 old-style character length specifications. If cnt is non-NULL it
294 will be set to the number of digits. */
297 gfc_match_small_literal_int (int *value, int *cnt)
303 old_loc = gfc_current_locus;
305 gfc_gobble_whitespace ();
306 c = gfc_next_char ();
312 gfc_current_locus = old_loc;
321 old_loc = gfc_current_locus;
322 c = gfc_next_char ();
327 i = 10 * i + c - '0';
332 gfc_error ("Integer too large at %C");
337 gfc_current_locus = old_loc;
346 /* Match a small, constant integer expression, like in a kind
347 statement. On MATCH_YES, 'value' is set. */
350 gfc_match_small_int (int *value)
357 m = gfc_match_expr (&expr);
361 p = gfc_extract_int (expr, &i);
362 gfc_free_expr (expr);
375 /* This function is the same as the gfc_match_small_int, except that
376 we're keeping the pointer to the expr. This function could just be
377 removed and the previously mentioned one modified, though all calls
378 to it would have to be modified then (and there were a number of
379 them). Return MATCH_ERROR if fail to extract the int; otherwise,
380 return the result of gfc_match_expr(). The expr (if any) that was
381 matched is returned in the parameter expr. */
384 gfc_match_small_int_expr (int *value, gfc_expr **expr)
390 m = gfc_match_expr (expr);
394 p = gfc_extract_int (*expr, &i);
407 /* Matches a statement label. Uses gfc_match_small_literal_int() to
408 do most of the work. */
411 gfc_match_st_label (gfc_st_label **label)
417 old_loc = gfc_current_locus;
419 m = gfc_match_small_literal_int (&i, &cnt);
425 gfc_error ("Too many digits in statement label at %C");
431 gfc_error ("Statement label at %C is zero");
435 *label = gfc_get_st_label (i);
440 gfc_current_locus = old_loc;
445 /* Match and validate a label associated with a named IF, DO or SELECT
446 statement. If the symbol does not have the label attribute, we add
447 it. We also make sure the symbol does not refer to another
448 (active) block. A matched label is pointed to by gfc_new_block. */
451 gfc_match_label (void)
453 char name[GFC_MAX_SYMBOL_LEN + 1];
456 gfc_new_block = NULL;
458 m = gfc_match (" %n :", name);
462 if (gfc_get_symbol (name, NULL, &gfc_new_block))
464 gfc_error ("Label name '%s' at %C is ambiguous", name);
468 if (gfc_new_block->attr.flavor == FL_LABEL)
470 gfc_error ("Duplicate construct label '%s' at %C", name);
474 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
475 gfc_new_block->name, NULL) == FAILURE)
482 /* See if the current input looks like a name of some sort. Modifies
483 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
484 Note that options.c restricts max_identifier_length to not more
485 than GFC_MAX_SYMBOL_LEN. */
488 gfc_match_name (char *buffer)
493 old_loc = gfc_current_locus;
494 gfc_gobble_whitespace ();
496 c = gfc_next_char ();
497 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
499 if (gfc_error_flag_test() == 0 && c != '(')
500 gfc_error ("Invalid character in name at %C");
501 gfc_current_locus = old_loc;
511 if (i > gfc_option.max_identifier_length)
513 gfc_error ("Name at %C is too long");
517 old_loc = gfc_current_locus;
518 c = gfc_next_char ();
520 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
522 if (c == '$' && !gfc_option.flag_dollar_ok)
524 gfc_error ("Invalid character '$' at %C. Use -fdollar-ok to allow it as an extension");
530 gfc_current_locus = old_loc;
536 /* Match a valid name for C, which is almost the same as for Fortran,
537 except that you can start with an underscore, etc.. It could have
538 been done by modifying the gfc_match_name, but this way other
539 things C allows can be added, such as no limits on the length.
540 Right now, the length is limited to the same thing as Fortran..
541 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
542 input characters from being automatically lower cased, since C is
543 case sensitive. The parameter, buffer, is used to return the name
544 that is matched. Return MATCH_ERROR if the name is too long
545 (though this is a self-imposed limit), MATCH_NO if what we're
546 seeing isn't a name, and MATCH_YES if we successfully match a C
550 gfc_match_name_C (char *buffer)
556 old_loc = gfc_current_locus;
557 gfc_gobble_whitespace ();
559 /* Get the next char (first possible char of name) and see if
560 it's valid for C (either a letter or an underscore). */
561 c = gfc_next_char_literal (1);
563 /* If the user put nothing expect spaces between the quotes, it is valid
564 and simply means there is no name= specifier and the name is the fortran
565 symbol name, all lowercase. */
566 if (c == '"' || c == '\'')
569 gfc_current_locus = old_loc;
573 if (!ISALPHA (c) && c != '_')
575 gfc_error ("Invalid C name in NAME= specifier at %C");
579 /* Continue to read valid variable name characters. */
584 /* C does not define a maximum length of variable names, to my
585 knowledge, but the compiler typically places a limit on them.
586 For now, i'll use the same as the fortran limit for simplicity,
587 but this may need to be changed to a dynamic buffer that can
588 be realloc'ed here if necessary, or more likely, a larger
590 if (i > gfc_option.max_identifier_length)
592 gfc_error ("Name at %C is too long");
596 old_loc = gfc_current_locus;
598 /* Get next char; param means we're in a string. */
599 c = gfc_next_char_literal (1);
600 } while (ISALNUM (c) || c == '_');
603 gfc_current_locus = old_loc;
605 /* See if we stopped because of whitespace. */
608 gfc_gobble_whitespace ();
609 c = gfc_peek_char ();
610 if (c != '"' && c != '\'')
612 gfc_error ("Embedded space in NAME= specifier at %C");
617 /* If we stopped because we had an invalid character for a C name, report
618 that to the user by returning MATCH_NO. */
619 if (c != '"' && c != '\'')
621 gfc_error ("Invalid C name in NAME= specifier at %C");
629 /* Match a symbol on the input. Modifies the pointer to the symbol
630 pointer if successful. */
633 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
635 char buffer[GFC_MAX_SYMBOL_LEN + 1];
638 m = gfc_match_name (buffer);
643 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
644 ? MATCH_ERROR : MATCH_YES;
646 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
654 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
659 m = gfc_match_sym_tree (&st, host_assoc);
664 *matched_symbol = st->n.sym;
666 *matched_symbol = NULL;
669 *matched_symbol = NULL;
674 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
675 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
679 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
681 locus orig_loc = gfc_current_locus;
684 gfc_gobble_whitespace ();
685 ch = gfc_next_char ();
690 *result = INTRINSIC_PLUS;
695 *result = INTRINSIC_MINUS;
699 if (gfc_next_char () == '=')
702 *result = INTRINSIC_EQ;
708 if (gfc_peek_char () == '=')
712 *result = INTRINSIC_LE;
716 *result = INTRINSIC_LT;
720 if (gfc_peek_char () == '=')
724 *result = INTRINSIC_GE;
728 *result = INTRINSIC_GT;
732 if (gfc_peek_char () == '*')
736 *result = INTRINSIC_POWER;
740 *result = INTRINSIC_TIMES;
744 ch = gfc_peek_char ();
749 *result = INTRINSIC_NE;
756 *result = INTRINSIC_CONCAT;
760 *result = INTRINSIC_DIVIDE;
764 ch = gfc_next_char ();
768 if (gfc_next_char () == 'n'
769 && gfc_next_char () == 'd'
770 && gfc_next_char () == '.')
772 /* Matched ".and.". */
773 *result = INTRINSIC_AND;
779 if (gfc_next_char () == 'q')
781 ch = gfc_next_char ();
784 /* Matched ".eq.". */
785 *result = INTRINSIC_EQ_OS;
790 if (gfc_next_char () == '.')
792 /* Matched ".eqv.". */
793 *result = INTRINSIC_EQV;
801 ch = gfc_next_char ();
804 if (gfc_next_char () == '.')
806 /* Matched ".ge.". */
807 *result = INTRINSIC_GE_OS;
813 if (gfc_next_char () == '.')
815 /* Matched ".gt.". */
816 *result = INTRINSIC_GT_OS;
823 ch = gfc_next_char ();
826 if (gfc_next_char () == '.')
828 /* Matched ".le.". */
829 *result = INTRINSIC_LE_OS;
835 if (gfc_next_char () == '.')
837 /* Matched ".lt.". */
838 *result = INTRINSIC_LT_OS;
845 ch = gfc_next_char ();
848 ch = gfc_next_char ();
851 /* Matched ".ne.". */
852 *result = INTRINSIC_NE_OS;
857 if (gfc_next_char () == 'v'
858 && gfc_next_char () == '.')
860 /* Matched ".neqv.". */
861 *result = INTRINSIC_NEQV;
868 if (gfc_next_char () == 't'
869 && gfc_next_char () == '.')
871 /* Matched ".not.". */
872 *result = INTRINSIC_NOT;
879 if (gfc_next_char () == 'r'
880 && gfc_next_char () == '.')
882 /* Matched ".or.". */
883 *result = INTRINSIC_OR;
897 gfc_current_locus = orig_loc;
902 /* Match a loop control phrase:
904 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
906 If the final integer expression is not present, a constant unity
907 expression is returned. We don't return MATCH_ERROR until after
908 the equals sign is seen. */
911 gfc_match_iterator (gfc_iterator *iter, int init_flag)
913 char name[GFC_MAX_SYMBOL_LEN + 1];
914 gfc_expr *var, *e1, *e2, *e3;
918 /* Match the start of an iterator without affecting the symbol table. */
920 start = gfc_current_locus;
921 m = gfc_match (" %n =", name);
922 gfc_current_locus = start;
927 m = gfc_match_variable (&var, 0);
931 gfc_match_char ('=');
935 if (var->ref != NULL)
937 gfc_error ("Loop variable at %C cannot be a sub-component");
941 if (var->symtree->n.sym->attr.intent == INTENT_IN)
943 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
944 var->symtree->n.sym->name);
948 var->symtree->n.sym->attr.implied_index = 1;
950 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
953 if (m == MATCH_ERROR)
956 if (gfc_match_char (',') != MATCH_YES)
959 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
962 if (m == MATCH_ERROR)
965 if (gfc_match_char (',') != MATCH_YES)
967 e3 = gfc_int_expr (1);
971 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
972 if (m == MATCH_ERROR)
976 gfc_error ("Expected a step value in iterator at %C");
988 gfc_error ("Syntax error in iterator at %C");
999 /* Tries to match the next non-whitespace character on the input.
1000 This subroutine does not return MATCH_ERROR. */
1003 gfc_match_char (char c)
1007 where = gfc_current_locus;
1008 gfc_gobble_whitespace ();
1010 if (gfc_next_char () == c)
1013 gfc_current_locus = where;
1018 /* General purpose matching subroutine. The target string is a
1019 scanf-like format string in which spaces correspond to arbitrary
1020 whitespace (including no whitespace), characters correspond to
1021 themselves. The %-codes are:
1023 %% Literal percent sign
1024 %e Expression, pointer to a pointer is set
1025 %s Symbol, pointer to the symbol is set
1026 %n Name, character buffer is set to name
1027 %t Matches end of statement.
1028 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1029 %l Matches a statement label
1030 %v Matches a variable expression (an lvalue)
1031 % Matches a required space (in free form) and optional spaces. */
1034 gfc_match (const char *target, ...)
1036 gfc_st_label **label;
1045 old_loc = gfc_current_locus;
1046 va_start (argp, target);
1056 gfc_gobble_whitespace ();
1067 vp = va_arg (argp, void **);
1068 n = gfc_match_expr ((gfc_expr **) vp);
1079 vp = va_arg (argp, void **);
1080 n = gfc_match_variable ((gfc_expr **) vp, 0);
1091 vp = va_arg (argp, void **);
1092 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1103 np = va_arg (argp, char *);
1104 n = gfc_match_name (np);
1115 label = va_arg (argp, gfc_st_label **);
1116 n = gfc_match_st_label (label);
1127 ip = va_arg (argp, int *);
1128 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1139 if (gfc_match_eos () != MATCH_YES)
1147 if (gfc_match_space () == MATCH_YES)
1153 break; /* Fall through to character matcher. */
1156 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1160 if (c == gfc_next_char ())
1170 /* Clean up after a failed match. */
1171 gfc_current_locus = old_loc;
1172 va_start (argp, target);
1175 for (; matches > 0; matches--)
1177 while (*p++ != '%');
1185 /* Matches that don't have to be undone */
1190 (void) va_arg (argp, void **);
1195 vp = va_arg (argp, void **);
1196 gfc_free_expr (*vp);
1209 /*********************** Statement level matching **********************/
1211 /* Matches the start of a program unit, which is the program keyword
1212 followed by an obligatory symbol. */
1215 gfc_match_program (void)
1220 m = gfc_match ("% %s%t", &sym);
1224 gfc_error ("Invalid form of PROGRAM statement at %C");
1228 if (m == MATCH_ERROR)
1231 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1234 gfc_new_block = sym;
1240 /* Match a simple assignment statement. */
1243 gfc_match_assignment (void)
1245 gfc_expr *lvalue, *rvalue;
1249 old_loc = gfc_current_locus;
1252 m = gfc_match (" %v =", &lvalue);
1255 gfc_current_locus = old_loc;
1256 gfc_free_expr (lvalue);
1260 if (lvalue->symtree->n.sym->attr.protected
1261 && lvalue->symtree->n.sym->attr.use_assoc)
1263 gfc_current_locus = old_loc;
1264 gfc_free_expr (lvalue);
1265 gfc_error ("Setting value of PROTECTED variable at %C");
1270 m = gfc_match (" %e%t", &rvalue);
1273 gfc_current_locus = old_loc;
1274 gfc_free_expr (lvalue);
1275 gfc_free_expr (rvalue);
1279 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1281 new_st.op = EXEC_ASSIGN;
1282 new_st.expr = lvalue;
1283 new_st.expr2 = rvalue;
1285 gfc_check_do_variable (lvalue->symtree);
1291 /* Match a pointer assignment statement. */
1294 gfc_match_pointer_assignment (void)
1296 gfc_expr *lvalue, *rvalue;
1300 old_loc = gfc_current_locus;
1302 lvalue = rvalue = NULL;
1304 m = gfc_match (" %v =>", &lvalue);
1311 m = gfc_match (" %e%t", &rvalue);
1315 if (lvalue->symtree->n.sym->attr.protected
1316 && lvalue->symtree->n.sym->attr.use_assoc)
1318 gfc_error ("Assigning to a PROTECTED pointer at %C");
1323 new_st.op = EXEC_POINTER_ASSIGN;
1324 new_st.expr = lvalue;
1325 new_st.expr2 = rvalue;
1330 gfc_current_locus = old_loc;
1331 gfc_free_expr (lvalue);
1332 gfc_free_expr (rvalue);
1337 /* We try to match an easy arithmetic IF statement. This only happens
1338 when just after having encountered a simple IF statement. This code
1339 is really duplicate with parts of the gfc_match_if code, but this is
1343 match_arithmetic_if (void)
1345 gfc_st_label *l1, *l2, *l3;
1349 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1353 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1354 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1355 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1357 gfc_free_expr (expr);
1361 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF statement "
1362 "at %C") == FAILURE)
1365 new_st.op = EXEC_ARITHMETIC_IF;
1375 /* The IF statement is a bit of a pain. First of all, there are three
1376 forms of it, the simple IF, the IF that starts a block and the
1379 There is a problem with the simple IF and that is the fact that we
1380 only have a single level of undo information on symbols. What this
1381 means is for a simple IF, we must re-match the whole IF statement
1382 multiple times in order to guarantee that the symbol table ends up
1383 in the proper state. */
1385 static match match_simple_forall (void);
1386 static match match_simple_where (void);
1389 gfc_match_if (gfc_statement *if_type)
1392 gfc_st_label *l1, *l2, *l3;
1393 locus old_loc, old_loc2;
1397 n = gfc_match_label ();
1398 if (n == MATCH_ERROR)
1401 old_loc = gfc_current_locus;
1403 m = gfc_match (" if ( %e", &expr);
1407 old_loc2 = gfc_current_locus;
1408 gfc_current_locus = old_loc;
1410 if (gfc_match_parens () == MATCH_ERROR)
1413 gfc_current_locus = old_loc2;
1415 if (gfc_match_char (')') != MATCH_YES)
1417 gfc_error ("Syntax error in IF-expression at %C");
1418 gfc_free_expr (expr);
1422 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1428 gfc_error ("Block label not appropriate for arithmetic IF "
1430 gfc_free_expr (expr);
1434 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1435 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1436 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1438 gfc_free_expr (expr);
1442 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF "
1443 "statement at %C") == FAILURE)
1446 new_st.op = EXEC_ARITHMETIC_IF;
1452 *if_type = ST_ARITHMETIC_IF;
1456 if (gfc_match (" then%t") == MATCH_YES)
1458 new_st.op = EXEC_IF;
1460 *if_type = ST_IF_BLOCK;
1466 gfc_error ("Block label is not appropriate for IF statement at %C");
1467 gfc_free_expr (expr);
1471 /* At this point the only thing left is a simple IF statement. At
1472 this point, n has to be MATCH_NO, so we don't have to worry about
1473 re-matching a block label. From what we've got so far, try
1474 matching an assignment. */
1476 *if_type = ST_SIMPLE_IF;
1478 m = gfc_match_assignment ();
1482 gfc_free_expr (expr);
1483 gfc_undo_symbols ();
1484 gfc_current_locus = old_loc;
1486 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1487 assignment was found. For MATCH_NO, continue to call the various
1489 if (m == MATCH_ERROR)
1492 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1494 m = gfc_match_pointer_assignment ();
1498 gfc_free_expr (expr);
1499 gfc_undo_symbols ();
1500 gfc_current_locus = old_loc;
1502 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1504 /* Look at the next keyword to see which matcher to call. Matching
1505 the keyword doesn't affect the symbol table, so we don't have to
1506 restore between tries. */
1508 #define match(string, subr, statement) \
1509 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1513 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1514 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1515 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1516 match ("call", gfc_match_call, ST_CALL)
1517 match ("close", gfc_match_close, ST_CLOSE)
1518 match ("continue", gfc_match_continue, ST_CONTINUE)
1519 match ("cycle", gfc_match_cycle, ST_CYCLE)
1520 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1521 match ("end file", gfc_match_endfile, ST_END_FILE)
1522 match ("exit", gfc_match_exit, ST_EXIT)
1523 match ("flush", gfc_match_flush, ST_FLUSH)
1524 match ("forall", match_simple_forall, ST_FORALL)
1525 match ("go to", gfc_match_goto, ST_GOTO)
1526 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1527 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1528 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1529 match ("open", gfc_match_open, ST_OPEN)
1530 match ("pause", gfc_match_pause, ST_NONE)
1531 match ("print", gfc_match_print, ST_WRITE)
1532 match ("read", gfc_match_read, ST_READ)
1533 match ("return", gfc_match_return, ST_RETURN)
1534 match ("rewind", gfc_match_rewind, ST_REWIND)
1535 match ("stop", gfc_match_stop, ST_STOP)
1536 match ("wait", gfc_match_wait, ST_WAIT)
1537 match ("where", match_simple_where, ST_WHERE)
1538 match ("write", gfc_match_write, ST_WRITE)
1540 /* The gfc_match_assignment() above may have returned a MATCH_NO
1541 where the assignment was to a named constant. Check that
1542 special case here. */
1543 m = gfc_match_assignment ();
1546 gfc_error ("Cannot assign to a named constant at %C");
1547 gfc_free_expr (expr);
1548 gfc_undo_symbols ();
1549 gfc_current_locus = old_loc;
1553 /* All else has failed, so give up. See if any of the matchers has
1554 stored an error message of some sort. */
1555 if (gfc_error_check () == 0)
1556 gfc_error ("Unclassifiable statement in IF-clause at %C");
1558 gfc_free_expr (expr);
1563 gfc_error ("Syntax error in IF-clause at %C");
1566 gfc_free_expr (expr);
1570 /* At this point, we've matched the single IF and the action clause
1571 is in new_st. Rearrange things so that the IF statement appears
1574 p = gfc_get_code ();
1575 p->next = gfc_get_code ();
1577 p->next->loc = gfc_current_locus;
1582 gfc_clear_new_st ();
1584 new_st.op = EXEC_IF;
1593 /* Match an ELSE statement. */
1596 gfc_match_else (void)
1598 char name[GFC_MAX_SYMBOL_LEN + 1];
1600 if (gfc_match_eos () == MATCH_YES)
1603 if (gfc_match_name (name) != MATCH_YES
1604 || gfc_current_block () == NULL
1605 || gfc_match_eos () != MATCH_YES)
1607 gfc_error ("Unexpected junk after ELSE statement at %C");
1611 if (strcmp (name, gfc_current_block ()->name) != 0)
1613 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1614 name, gfc_current_block ()->name);
1622 /* Match an ELSE IF statement. */
1625 gfc_match_elseif (void)
1627 char name[GFC_MAX_SYMBOL_LEN + 1];
1631 m = gfc_match (" ( %e ) then", &expr);
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 IF 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);
1654 new_st.op = EXEC_IF;
1659 gfc_free_expr (expr);
1664 /* Free a gfc_iterator structure. */
1667 gfc_free_iterator (gfc_iterator *iter, int flag)
1673 gfc_free_expr (iter->var);
1674 gfc_free_expr (iter->start);
1675 gfc_free_expr (iter->end);
1676 gfc_free_expr (iter->step);
1683 /* Match a DO statement. */
1688 gfc_iterator iter, *ip;
1690 gfc_st_label *label;
1693 old_loc = gfc_current_locus;
1696 iter.var = iter.start = iter.end = iter.step = NULL;
1698 m = gfc_match_label ();
1699 if (m == MATCH_ERROR)
1702 if (gfc_match (" do") != MATCH_YES)
1705 m = gfc_match_st_label (&label);
1706 if (m == MATCH_ERROR)
1709 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1711 if (gfc_match_eos () == MATCH_YES)
1713 iter.end = gfc_logical_expr (1, NULL);
1714 new_st.op = EXEC_DO_WHILE;
1718 /* Match an optional comma, if no comma is found, a space is obligatory. */
1719 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1722 /* See if we have a DO WHILE. */
1723 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1725 new_st.op = EXEC_DO_WHILE;
1729 /* The abortive DO WHILE may have done something to the symbol
1730 table, so we start over. */
1731 gfc_undo_symbols ();
1732 gfc_current_locus = old_loc;
1734 gfc_match_label (); /* This won't error. */
1735 gfc_match (" do "); /* This will work. */
1737 gfc_match_st_label (&label); /* Can't error out. */
1738 gfc_match_char (','); /* Optional comma. */
1740 m = gfc_match_iterator (&iter, 0);
1743 if (m == MATCH_ERROR)
1746 iter.var->symtree->n.sym->attr.implied_index = 0;
1747 gfc_check_do_variable (iter.var->symtree);
1749 if (gfc_match_eos () != MATCH_YES)
1751 gfc_syntax_error (ST_DO);
1755 new_st.op = EXEC_DO;
1759 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1762 new_st.label = label;
1764 if (new_st.op == EXEC_DO_WHILE)
1765 new_st.expr = iter.end;
1768 new_st.ext.iterator = ip = gfc_get_iterator ();
1775 gfc_free_iterator (&iter, 0);
1781 /* Match an EXIT or CYCLE statement. */
1784 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1786 gfc_state_data *p, *o;
1790 if (gfc_match_eos () == MATCH_YES)
1794 m = gfc_match ("% %s%t", &sym);
1795 if (m == MATCH_ERROR)
1799 gfc_syntax_error (st);
1803 if (sym->attr.flavor != FL_LABEL)
1805 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1806 sym->name, gfc_ascii_statement (st));
1811 /* Find the loop mentioned specified by the label (or lack of a label). */
1812 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1813 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1815 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1821 gfc_error ("%s statement at %C is not within a loop",
1822 gfc_ascii_statement (st));
1824 gfc_error ("%s statement at %C is not within loop '%s'",
1825 gfc_ascii_statement (st), sym->name);
1832 gfc_error ("%s statement at %C leaving OpenMP structured block",
1833 gfc_ascii_statement (st));
1836 else if (st == ST_EXIT
1837 && p->previous != NULL
1838 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1839 && (p->previous->head->op == EXEC_OMP_DO
1840 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1842 gcc_assert (p->previous->head->next != NULL);
1843 gcc_assert (p->previous->head->next->op == EXEC_DO
1844 || p->previous->head->next->op == EXEC_DO_WHILE);
1845 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1849 /* Save the first statement in the loop - needed by the backend. */
1850 new_st.ext.whichloop = p->head;
1858 /* Match the EXIT statement. */
1861 gfc_match_exit (void)
1863 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1867 /* Match the CYCLE statement. */
1870 gfc_match_cycle (void)
1872 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1876 /* Match a number or character constant after a STOP or PAUSE statement. */
1879 gfc_match_stopcode (gfc_statement st)
1889 if (gfc_match_eos () != MATCH_YES)
1891 m = gfc_match_small_literal_int (&stop_code, &cnt);
1892 if (m == MATCH_ERROR)
1895 if (m == MATCH_YES && cnt > 5)
1897 gfc_error ("Too many digits in STOP code at %C");
1903 /* Try a character constant. */
1904 m = gfc_match_expr (&e);
1905 if (m == MATCH_ERROR)
1909 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1913 if (gfc_match_eos () != MATCH_YES)
1917 if (gfc_pure (NULL))
1919 gfc_error ("%s statement not allowed in PURE procedure at %C",
1920 gfc_ascii_statement (st));
1924 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1926 new_st.ext.stop_code = stop_code;
1931 gfc_syntax_error (st);
1940 /* Match the (deprecated) PAUSE statement. */
1943 gfc_match_pause (void)
1947 m = gfc_match_stopcode (ST_PAUSE);
1950 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
1959 /* Match the STOP statement. */
1962 gfc_match_stop (void)
1964 return gfc_match_stopcode (ST_STOP);
1968 /* Match a CONTINUE statement. */
1971 gfc_match_continue (void)
1973 if (gfc_match_eos () != MATCH_YES)
1975 gfc_syntax_error (ST_CONTINUE);
1979 new_st.op = EXEC_CONTINUE;
1984 /* Match the (deprecated) ASSIGN statement. */
1987 gfc_match_assign (void)
1990 gfc_st_label *label;
1992 if (gfc_match (" %l", &label) == MATCH_YES)
1994 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1996 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1998 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
2003 expr->symtree->n.sym->attr.assign = 1;
2005 new_st.op = EXEC_LABEL_ASSIGN;
2006 new_st.label = label;
2015 /* Match the GO TO statement. As a computed GOTO statement is
2016 matched, it is transformed into an equivalent SELECT block. No
2017 tree is necessary, and the resulting jumps-to-jumps are
2018 specifically optimized away by the back end. */
2021 gfc_match_goto (void)
2023 gfc_code *head, *tail;
2026 gfc_st_label *label;
2030 if (gfc_match (" %l%t", &label) == MATCH_YES)
2032 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2035 new_st.op = EXEC_GOTO;
2036 new_st.label = label;
2040 /* The assigned GO TO statement. */
2042 if (gfc_match_variable (&expr, 0) == MATCH_YES)
2044 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
2049 new_st.op = EXEC_GOTO;
2052 if (gfc_match_eos () == MATCH_YES)
2055 /* Match label list. */
2056 gfc_match_char (',');
2057 if (gfc_match_char ('(') != MATCH_YES)
2059 gfc_syntax_error (ST_GOTO);
2066 m = gfc_match_st_label (&label);
2070 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2074 head = tail = gfc_get_code ();
2077 tail->block = gfc_get_code ();
2081 tail->label = label;
2082 tail->op = EXEC_GOTO;
2084 while (gfc_match_char (',') == MATCH_YES);
2086 if (gfc_match (")%t") != MATCH_YES)
2091 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2094 new_st.block = head;
2099 /* Last chance is a computed GO TO statement. */
2100 if (gfc_match_char ('(') != MATCH_YES)
2102 gfc_syntax_error (ST_GOTO);
2111 m = gfc_match_st_label (&label);
2115 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2119 head = tail = gfc_get_code ();
2122 tail->block = gfc_get_code ();
2126 cp = gfc_get_case ();
2127 cp->low = cp->high = gfc_int_expr (i++);
2129 tail->op = EXEC_SELECT;
2130 tail->ext.case_list = cp;
2132 tail->next = gfc_get_code ();
2133 tail->next->op = EXEC_GOTO;
2134 tail->next->label = label;
2136 while (gfc_match_char (',') == MATCH_YES);
2138 if (gfc_match_char (')') != MATCH_YES)
2143 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2147 /* Get the rest of the statement. */
2148 gfc_match_char (',');
2150 if (gfc_match (" %e%t", &expr) != MATCH_YES)
2153 /* At this point, a computed GOTO has been fully matched and an
2154 equivalent SELECT statement constructed. */
2156 new_st.op = EXEC_SELECT;
2159 /* Hack: For a "real" SELECT, the expression is in expr. We put
2160 it in expr2 so we can distinguish then and produce the correct
2162 new_st.expr2 = expr;
2163 new_st.block = head;
2167 gfc_syntax_error (ST_GOTO);
2169 gfc_free_statements (head);
2174 /* Frees a list of gfc_alloc structures. */
2177 gfc_free_alloc_list (gfc_alloc *p)
2184 gfc_free_expr (p->expr);
2190 /* Match an ALLOCATE statement. */
2193 gfc_match_allocate (void)
2195 gfc_alloc *head, *tail;
2202 if (gfc_match_char ('(') != MATCH_YES)
2208 head = tail = gfc_get_alloc ();
2211 tail->next = gfc_get_alloc ();
2215 m = gfc_match_variable (&tail->expr, 0);
2218 if (m == MATCH_ERROR)
2221 if (gfc_check_do_variable (tail->expr->symtree))
2225 && gfc_impure_variable (tail->expr->symtree->n.sym))
2227 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
2232 if (tail->expr->ts.type == BT_DERIVED)
2233 tail->expr->ts.derived = gfc_use_derived (tail->expr->ts.derived);
2235 if (gfc_match_char (',') != MATCH_YES)
2238 m = gfc_match (" stat = %v", &stat);
2239 if (m == MATCH_ERROR)
2246 gfc_check_do_variable(stat->symtree);
2248 if (gfc_match (" )%t") != MATCH_YES)
2251 new_st.op = EXEC_ALLOCATE;
2253 new_st.ext.alloc_list = head;
2258 gfc_syntax_error (ST_ALLOCATE);
2261 gfc_free_expr (stat);
2262 gfc_free_alloc_list (head);
2267 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
2268 a set of pointer assignments to intrinsic NULL(). */
2271 gfc_match_nullify (void)
2279 if (gfc_match_char ('(') != MATCH_YES)
2284 m = gfc_match_variable (&p, 0);
2285 if (m == MATCH_ERROR)
2290 if (gfc_check_do_variable (p->symtree))
2293 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
2295 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
2299 /* build ' => NULL() '. */
2300 e = gfc_get_expr ();
2301 e->where = gfc_current_locus;
2302 e->expr_type = EXPR_NULL;
2303 e->ts.type = BT_UNKNOWN;
2305 /* Chain to list. */
2310 tail->next = gfc_get_code ();
2314 tail->op = EXEC_POINTER_ASSIGN;
2318 if (gfc_match (" )%t") == MATCH_YES)
2320 if (gfc_match_char (',') != MATCH_YES)
2327 gfc_syntax_error (ST_NULLIFY);
2330 gfc_free_statements (new_st.next);
2335 /* Match a DEALLOCATE statement. */
2338 gfc_match_deallocate (void)
2340 gfc_alloc *head, *tail;
2347 if (gfc_match_char ('(') != MATCH_YES)
2353 head = tail = gfc_get_alloc ();
2356 tail->next = gfc_get_alloc ();
2360 m = gfc_match_variable (&tail->expr, 0);
2361 if (m == MATCH_ERROR)
2366 if (gfc_check_do_variable (tail->expr->symtree))
2370 && gfc_impure_variable (tail->expr->symtree->n.sym))
2372 gfc_error ("Illegal deallocate-expression in DEALLOCATE at %C "
2373 "for a PURE procedure");
2377 if (gfc_match_char (',') != MATCH_YES)
2380 m = gfc_match (" stat = %v", &stat);
2381 if (m == MATCH_ERROR)
2388 gfc_check_do_variable(stat->symtree);
2390 if (gfc_match (" )%t") != MATCH_YES)
2393 new_st.op = EXEC_DEALLOCATE;
2395 new_st.ext.alloc_list = head;
2400 gfc_syntax_error (ST_DEALLOCATE);
2403 gfc_free_expr (stat);
2404 gfc_free_alloc_list (head);
2409 /* Match a RETURN statement. */
2412 gfc_match_return (void)
2416 gfc_compile_state s;
2420 if (gfc_match_eos () == MATCH_YES)
2423 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2425 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2430 if (gfc_current_form == FORM_FREE)
2432 /* The following are valid, so we can't require a blank after the
2436 c = gfc_peek_char ();
2437 if (ISALPHA (c) || ISDIGIT (c))
2441 m = gfc_match (" %e%t", &e);
2444 if (m == MATCH_ERROR)
2447 gfc_syntax_error (ST_RETURN);
2454 gfc_enclosing_unit (&s);
2455 if (s == COMP_PROGRAM
2456 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2457 "main program at %C") == FAILURE)
2460 new_st.op = EXEC_RETURN;
2467 /* Match a CALL statement. The tricky part here are possible
2468 alternate return specifiers. We handle these by having all
2469 "subroutines" actually return an integer via a register that gives
2470 the return number. If the call specifies alternate returns, we
2471 generate code for a SELECT statement whose case clauses contain
2472 GOTOs to the various labels. */
2475 gfc_match_call (void)
2477 char name[GFC_MAX_SYMBOL_LEN + 1];
2478 gfc_actual_arglist *a, *arglist;
2488 m = gfc_match ("% %n", name);
2494 if (gfc_get_ha_sym_tree (name, &st))
2499 /* If it does not seem to be callable... */
2500 if (!sym->attr.generic
2501 && !sym->attr.subroutine)
2503 if (!(sym->attr.external && !sym->attr.referenced))
2505 /* ...create a symbol in this scope... */
2506 if (sym->ns != gfc_current_ns
2507 && gfc_get_sym_tree (name, NULL, &st) == 1)
2510 if (sym != st->n.sym)
2514 /* ...and then to try to make the symbol into a subroutine. */
2515 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2519 gfc_set_sym_referenced (sym);
2521 if (gfc_match_eos () != MATCH_YES)
2523 m = gfc_match_actual_arglist (1, &arglist);
2526 if (m == MATCH_ERROR)
2529 if (gfc_match_eos () != MATCH_YES)
2533 /* If any alternate return labels were found, construct a SELECT
2534 statement that will jump to the right place. */
2537 for (a = arglist; a; a = a->next)
2538 if (a->expr == NULL)
2543 gfc_symtree *select_st;
2544 gfc_symbol *select_sym;
2545 char name[GFC_MAX_SYMBOL_LEN + 1];
2547 new_st.next = c = gfc_get_code ();
2548 c->op = EXEC_SELECT;
2549 sprintf (name, "_result_%s", sym->name);
2550 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
2552 select_sym = select_st->n.sym;
2553 select_sym->ts.type = BT_INTEGER;
2554 select_sym->ts.kind = gfc_default_integer_kind;
2555 gfc_set_sym_referenced (select_sym);
2556 c->expr = gfc_get_expr ();
2557 c->expr->expr_type = EXPR_VARIABLE;
2558 c->expr->symtree = select_st;
2559 c->expr->ts = select_sym->ts;
2560 c->expr->where = gfc_current_locus;
2563 for (a = arglist; a; a = a->next)
2565 if (a->expr != NULL)
2568 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2573 c->block = gfc_get_code ();
2575 c->op = EXEC_SELECT;
2577 new_case = gfc_get_case ();
2578 new_case->high = new_case->low = gfc_int_expr (i);
2579 c->ext.case_list = new_case;
2581 c->next = gfc_get_code ();
2582 c->next->op = EXEC_GOTO;
2583 c->next->label = a->label;
2587 new_st.op = EXEC_CALL;
2588 new_st.symtree = st;
2589 new_st.ext.actual = arglist;
2594 gfc_syntax_error (ST_CALL);
2597 gfc_free_actual_arglist (arglist);
2602 /* Given a name, return a pointer to the common head structure,
2603 creating it if it does not exist. If FROM_MODULE is nonzero, we
2604 mangle the name so that it doesn't interfere with commons defined
2605 in the using namespace.
2606 TODO: Add to global symbol tree. */
2609 gfc_get_common (const char *name, int from_module)
2612 static int serial = 0;
2613 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
2617 /* A use associated common block is only needed to correctly layout
2618 the variables it contains. */
2619 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2620 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2624 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2627 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2630 if (st->n.common == NULL)
2632 st->n.common = gfc_get_common_head ();
2633 st->n.common->where = gfc_current_locus;
2634 strcpy (st->n.common->name, name);
2637 return st->n.common;
2641 /* Match a common block name. */
2643 match match_common_name (char *name)
2647 if (gfc_match_char ('/') == MATCH_NO)
2653 if (gfc_match_char ('/') == MATCH_YES)
2659 m = gfc_match_name (name);
2661 if (m == MATCH_ERROR)
2663 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2666 gfc_error ("Syntax error in common block name at %C");
2671 /* Match a COMMON statement. */
2674 gfc_match_common (void)
2676 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2677 char name[GFC_MAX_SYMBOL_LEN + 1];
2684 old_blank_common = gfc_current_ns->blank_common.head;
2685 if (old_blank_common)
2687 while (old_blank_common->common_next)
2688 old_blank_common = old_blank_common->common_next;
2695 m = match_common_name (name);
2696 if (m == MATCH_ERROR)
2699 gsym = gfc_get_gsymbol (name);
2700 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2702 gfc_error ("Symbol '%s' at %C is already an external symbol that "
2703 "is not COMMON", name);
2707 if (gsym->type == GSYM_UNKNOWN)
2709 gsym->type = GSYM_COMMON;
2710 gsym->where = gfc_current_locus;
2716 if (name[0] == '\0')
2718 t = &gfc_current_ns->blank_common;
2719 if (t->head == NULL)
2720 t->where = gfc_current_locus;
2724 t = gfc_get_common (name, 0);
2733 while (tail->common_next)
2734 tail = tail->common_next;
2737 /* Grab the list of symbols. */
2740 m = gfc_match_symbol (&sym, 0);
2741 if (m == MATCH_ERROR)
2746 /* Store a ref to the common block for error checking. */
2747 sym->common_block = t;
2749 /* See if we know the current common block is bind(c), and if
2750 so, then see if we can check if the symbol is (which it'll
2751 need to be). This can happen if the bind(c) attr stmt was
2752 applied to the common block, and the variable(s) already
2753 defined, before declaring the common block. */
2754 if (t->is_bind_c == 1)
2756 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
2758 /* If we find an error, just print it and continue,
2759 cause it's just semantic, and we can see if there
2761 gfc_error_now ("Variable '%s' at %L in common block '%s' "
2762 "at %C must be declared with a C "
2763 "interoperable kind since common block "
2765 sym->name, &(sym->declared_at), t->name,
2769 if (sym->attr.is_bind_c == 1)
2770 gfc_error_now ("Variable '%s' in common block "
2771 "'%s' at %C can not be bind(c) since "
2772 "it is not global", sym->name, t->name);
2775 if (sym->attr.in_common)
2777 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2782 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
2783 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
2785 if (gfc_notify_std (GFC_STD_GNU, "Initialized symbol '%s' at %C "
2786 "can only be COMMON in "
2787 "BLOCK DATA", sym->name)
2792 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2796 tail->common_next = sym;
2802 /* Deal with an optional array specification after the
2804 m = gfc_match_array_spec (&as);
2805 if (m == MATCH_ERROR)
2810 if (as->type != AS_EXPLICIT)
2812 gfc_error ("Array specification for symbol '%s' in COMMON "
2813 "at %C must be explicit", sym->name);
2817 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2820 if (sym->attr.pointer)
2822 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
2823 "POINTER array", sym->name);
2832 sym->common_head = t;
2834 /* Check to see if the symbol is already in an equivalence group.
2835 If it is, set the other members as being in common. */
2836 if (sym->attr.in_equivalence)
2838 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2840 for (e2 = e1; e2; e2 = e2->eq)
2841 if (e2->expr->symtree->n.sym == sym)
2848 for (e2 = e1; e2; e2 = e2->eq)
2850 other = e2->expr->symtree->n.sym;
2851 if (other->common_head
2852 && other->common_head != sym->common_head)
2854 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2855 "%C is being indirectly equivalenced to "
2856 "another COMMON block '%s'",
2857 sym->name, sym->common_head->name,
2858 other->common_head->name);
2861 other->attr.in_common = 1;
2862 other->common_head = t;
2868 gfc_gobble_whitespace ();
2869 if (gfc_match_eos () == MATCH_YES)
2871 if (gfc_peek_char () == '/')
2873 if (gfc_match_char (',') != MATCH_YES)
2875 gfc_gobble_whitespace ();
2876 if (gfc_peek_char () == '/')
2885 gfc_syntax_error (ST_COMMON);
2888 if (old_blank_common)
2889 old_blank_common->common_next = NULL;
2891 gfc_current_ns->blank_common.head = NULL;
2892 gfc_free_array_spec (as);
2897 /* Match a BLOCK DATA program unit. */
2900 gfc_match_block_data (void)
2902 char name[GFC_MAX_SYMBOL_LEN + 1];
2906 if (gfc_match_eos () == MATCH_YES)
2908 gfc_new_block = NULL;
2912 m = gfc_match ("% %n%t", name);
2916 if (gfc_get_symbol (name, NULL, &sym))
2919 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2922 gfc_new_block = sym;
2928 /* Free a namelist structure. */
2931 gfc_free_namelist (gfc_namelist *name)
2935 for (; name; name = n)
2943 /* Match a NAMELIST statement. */
2946 gfc_match_namelist (void)
2948 gfc_symbol *group_name, *sym;
2952 m = gfc_match (" / %s /", &group_name);
2955 if (m == MATCH_ERROR)
2960 if (group_name->ts.type != BT_UNKNOWN)
2962 gfc_error ("Namelist group name '%s' at %C already has a basic "
2963 "type of %s", group_name->name,
2964 gfc_typename (&group_name->ts));
2968 if (group_name->attr.flavor == FL_NAMELIST
2969 && group_name->attr.use_assoc
2970 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2971 "at %C already is USE associated and can"
2972 "not be respecified.", group_name->name)
2976 if (group_name->attr.flavor != FL_NAMELIST
2977 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2978 group_name->name, NULL) == FAILURE)
2983 m = gfc_match_symbol (&sym, 1);
2986 if (m == MATCH_ERROR)
2989 if (sym->attr.in_namelist == 0
2990 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2993 /* Use gfc_error_check here, rather than goto error, so that
2994 these are the only errors for the next two lines. */
2995 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2997 gfc_error ("Assumed size array '%s' in namelist '%s' at "
2998 "%C is not allowed", sym->name, group_name->name);
3002 if (sym->ts.type == BT_CHARACTER && sym->ts.cl->length == NULL)
3004 gfc_error ("Assumed character length '%s' in namelist '%s' at "
3005 "%C is not allowed", sym->name, group_name->name);
3009 nl = gfc_get_namelist ();
3013 if (group_name->namelist == NULL)
3014 group_name->namelist = group_name->namelist_tail = nl;
3017 group_name->namelist_tail->next = nl;
3018 group_name->namelist_tail = nl;
3021 if (gfc_match_eos () == MATCH_YES)
3024 m = gfc_match_char (',');
3026 if (gfc_match_char ('/') == MATCH_YES)
3028 m2 = gfc_match (" %s /", &group_name);
3029 if (m2 == MATCH_YES)
3031 if (m2 == MATCH_ERROR)
3045 gfc_syntax_error (ST_NAMELIST);
3052 /* Match a MODULE statement. */
3055 gfc_match_module (void)
3059 m = gfc_match (" %s%t", &gfc_new_block);
3063 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
3064 gfc_new_block->name, NULL) == FAILURE)
3071 /* Free equivalence sets and lists. Recursively is the easiest way to
3075 gfc_free_equiv (gfc_equiv *eq)
3080 gfc_free_equiv (eq->eq);
3081 gfc_free_equiv (eq->next);
3082 gfc_free_expr (eq->expr);
3087 /* Match an EQUIVALENCE statement. */
3090 gfc_match_equivalence (void)
3092 gfc_equiv *eq, *set, *tail;
3096 gfc_common_head *common_head = NULL;
3104 eq = gfc_get_equiv ();
3108 eq->next = gfc_current_ns->equiv;
3109 gfc_current_ns->equiv = eq;
3111 if (gfc_match_char ('(') != MATCH_YES)
3115 common_flag = FALSE;
3120 m = gfc_match_equiv_variable (&set->expr);
3121 if (m == MATCH_ERROR)
3126 /* count the number of objects. */
3129 if (gfc_match_char ('%') == MATCH_YES)
3131 gfc_error ("Derived type component %C is not a "
3132 "permitted EQUIVALENCE member");
3136 for (ref = set->expr->ref; ref; ref = ref->next)
3137 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3139 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
3140 "be an array section");
3144 sym = set->expr->symtree->n.sym;
3146 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
3149 if (sym->attr.in_common)
3152 common_head = sym->common_head;
3155 if (gfc_match_char (')') == MATCH_YES)
3158 if (gfc_match_char (',') != MATCH_YES)
3161 set->eq = gfc_get_equiv ();
3167 gfc_error ("EQUIVALENCE at %C requires two or more objects");
3171 /* If one of the members of an equivalence is in common, then
3172 mark them all as being in common. Before doing this, check
3173 that members of the equivalence group are not in different
3176 for (set = eq; set; set = set->eq)
3178 sym = set->expr->symtree->n.sym;
3179 if (sym->common_head && sym->common_head != common_head)
3181 gfc_error ("Attempt to indirectly overlap COMMON "
3182 "blocks %s and %s by EQUIVALENCE at %C",
3183 sym->common_head->name, common_head->name);
3186 sym->attr.in_common = 1;
3187 sym->common_head = common_head;
3190 if (gfc_match_eos () == MATCH_YES)
3192 if (gfc_match_char (',') != MATCH_YES)
3199 gfc_syntax_error (ST_EQUIVALENCE);
3205 gfc_free_equiv (gfc_current_ns->equiv);
3206 gfc_current_ns->equiv = eq;
3212 /* Check that a statement function is not recursive. This is done by looking
3213 for the statement function symbol(sym) by looking recursively through its
3214 expression(e). If a reference to sym is found, true is returned.
3215 12.5.4 requires that any variable of function that is implicitly typed
3216 shall have that type confirmed by any subsequent type declaration. The
3217 implicit typing is conveniently done here. */
3219 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
3222 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
3228 switch (e->expr_type)
3231 if (e->symtree == NULL)
3234 /* Check the name before testing for nested recursion! */
3235 if (sym->name == e->symtree->n.sym->name)
3238 /* Catch recursion via other statement functions. */
3239 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
3240 && e->symtree->n.sym->value
3241 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
3244 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3245 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3250 if (e->symtree && sym->name == e->symtree->n.sym->name)
3253 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3254 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3266 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
3268 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
3272 /* Match a statement function declaration. It is so easy to match
3273 non-statement function statements with a MATCH_ERROR as opposed to
3274 MATCH_NO that we suppress error message in most cases. */
3277 gfc_match_st_function (void)
3279 gfc_error_buf old_error;
3284 m = gfc_match_symbol (&sym, 0);
3288 gfc_push_error (&old_error);
3290 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
3291 sym->name, NULL) == FAILURE)
3294 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
3297 m = gfc_match (" = %e%t", &expr);
3301 gfc_free_error (&old_error);
3302 if (m == MATCH_ERROR)
3305 if (recursive_stmt_fcn (expr, sym))
3307 gfc_error ("Statement function at %L is recursive", &expr->where);
3316 gfc_pop_error (&old_error);
3321 /***************** SELECT CASE subroutines ******************/
3323 /* Free a single case structure. */
3326 free_case (gfc_case *p)
3328 if (p->low == p->high)
3330 gfc_free_expr (p->low);
3331 gfc_free_expr (p->high);
3336 /* Free a list of case structures. */
3339 gfc_free_case_list (gfc_case *p)
3351 /* Match a single case selector. */
3354 match_case_selector (gfc_case **cp)
3359 c = gfc_get_case ();
3360 c->where = gfc_current_locus;
3362 if (gfc_match_char (':') == MATCH_YES)
3364 m = gfc_match_init_expr (&c->high);
3367 if (m == MATCH_ERROR)
3372 m = gfc_match_init_expr (&c->low);
3373 if (m == MATCH_ERROR)
3378 /* If we're not looking at a ':' now, make a range out of a single
3379 target. Else get the upper bound for the case range. */
3380 if (gfc_match_char (':') != MATCH_YES)
3384 m = gfc_match_init_expr (&c->high);
3385 if (m == MATCH_ERROR)
3387 /* MATCH_NO is fine. It's OK if nothing is there! */
3395 gfc_error ("Expected initialization expression in CASE at %C");
3403 /* Match the end of a case statement. */
3406 match_case_eos (void)
3408 char name[GFC_MAX_SYMBOL_LEN + 1];
3411 if (gfc_match_eos () == MATCH_YES)
3414 /* If the case construct doesn't have a case-construct-name, we
3415 should have matched the EOS. */
3416 if (!gfc_current_block ())
3418 gfc_error ("Expected the name of the SELECT CASE construct at %C");
3422 gfc_gobble_whitespace ();
3424 m = gfc_match_name (name);
3428 if (strcmp (name, gfc_current_block ()->name) != 0)
3430 gfc_error ("Expected case name of '%s' at %C",
3431 gfc_current_block ()->name);
3435 return gfc_match_eos ();
3439 /* Match a SELECT statement. */
3442 gfc_match_select (void)
3447 m = gfc_match_label ();
3448 if (m == MATCH_ERROR)
3451 m = gfc_match (" select case ( %e )%t", &expr);
3455 new_st.op = EXEC_SELECT;
3462 /* Match a CASE statement. */
3465 gfc_match_case (void)
3467 gfc_case *c, *head, *tail;
3472 if (gfc_current_state () != COMP_SELECT)
3474 gfc_error ("Unexpected CASE statement at %C");
3478 if (gfc_match ("% default") == MATCH_YES)
3480 m = match_case_eos ();
3483 if (m == MATCH_ERROR)
3486 new_st.op = EXEC_SELECT;
3487 c = gfc_get_case ();
3488 c->where = gfc_current_locus;
3489 new_st.ext.case_list = c;
3493 if (gfc_match_char ('(') != MATCH_YES)
3498 if (match_case_selector (&c) == MATCH_ERROR)
3508 if (gfc_match_char (')') == MATCH_YES)
3510 if (gfc_match_char (',') != MATCH_YES)
3514 m = match_case_eos ();
3517 if (m == MATCH_ERROR)
3520 new_st.op = EXEC_SELECT;
3521 new_st.ext.case_list = head;
3526 gfc_error ("Syntax error in CASE-specification at %C");
3529 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3533 /********************* WHERE subroutines ********************/
3535 /* Match the rest of a simple WHERE statement that follows an IF statement.
3539 match_simple_where (void)
3545 m = gfc_match (" ( %e )", &expr);
3549 m = gfc_match_assignment ();
3552 if (m == MATCH_ERROR)
3555 if (gfc_match_eos () != MATCH_YES)
3558 c = gfc_get_code ();
3562 c->next = gfc_get_code ();
3565 gfc_clear_new_st ();
3567 new_st.op = EXEC_WHERE;
3573 gfc_syntax_error (ST_WHERE);
3576 gfc_free_expr (expr);
3581 /* Match a WHERE statement. */
3584 gfc_match_where (gfc_statement *st)
3590 m0 = gfc_match_label ();
3591 if (m0 == MATCH_ERROR)
3594 m = gfc_match (" where ( %e )", &expr);
3598 if (gfc_match_eos () == MATCH_YES)
3600 *st = ST_WHERE_BLOCK;
3601 new_st.op = EXEC_WHERE;
3606 m = gfc_match_assignment ();
3608 gfc_syntax_error (ST_WHERE);
3612 gfc_free_expr (expr);
3616 /* We've got a simple WHERE statement. */
3618 c = gfc_get_code ();
3622 c->next = gfc_get_code ();
3625 gfc_clear_new_st ();
3627 new_st.op = EXEC_WHERE;
3634 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3635 new_st if successful. */
3638 gfc_match_elsewhere (void)
3640 char name[GFC_MAX_SYMBOL_LEN + 1];
3644 if (gfc_current_state () != COMP_WHERE)
3646 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3652 if (gfc_match_char ('(') == MATCH_YES)
3654 m = gfc_match_expr (&expr);
3657 if (m == MATCH_ERROR)
3660 if (gfc_match_char (')') != MATCH_YES)
3664 if (gfc_match_eos () != MATCH_YES)
3666 /* Only makes sense if we have a where-construct-name. */
3667 if (!gfc_current_block ())
3672 /* Better be a name at this point. */
3673 m = gfc_match_name (name);
3676 if (m == MATCH_ERROR)
3679 if (gfc_match_eos () != MATCH_YES)
3682 if (strcmp (name, gfc_current_block ()->name) != 0)
3684 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3685 name, gfc_current_block ()->name);
3690 new_st.op = EXEC_WHERE;
3695 gfc_syntax_error (ST_ELSEWHERE);
3698 gfc_free_expr (expr);
3703 /******************** FORALL subroutines ********************/
3705 /* Free a list of FORALL iterators. */
3708 gfc_free_forall_iterator (gfc_forall_iterator *iter)
3710 gfc_forall_iterator *next;
3715 gfc_free_expr (iter->var);
3716 gfc_free_expr (iter->start);
3717 gfc_free_expr (iter->end);
3718 gfc_free_expr (iter->stride);
3725 /* Match an iterator as part of a FORALL statement. The format is:
3727 <var> = <start>:<end>[:<stride>]
3729 On MATCH_NO, the caller tests for the possibility that there is a
3730 scalar mask expression. */
3733 match_forall_iterator (gfc_forall_iterator **result)
3735 gfc_forall_iterator *iter;
3739 where = gfc_current_locus;
3740 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3742 m = gfc_match_expr (&iter->var);
3746 if (gfc_match_char ('=') != MATCH_YES
3747 || iter->var->expr_type != EXPR_VARIABLE)
3753 m = gfc_match_expr (&iter->start);
3757 if (gfc_match_char (':') != MATCH_YES)
3760 m = gfc_match_expr (&iter->end);
3763 if (m == MATCH_ERROR)
3766 if (gfc_match_char (':') == MATCH_NO)
3767 iter->stride = gfc_int_expr (1);
3770 m = gfc_match_expr (&iter->stride);
3773 if (m == MATCH_ERROR)
3777 /* Mark the iteration variable's symbol as used as a FORALL index. */
3778 iter->var->symtree->n.sym->forall_index = true;
3784 gfc_error ("Syntax error in FORALL iterator at %C");
3789 gfc_current_locus = where;
3790 gfc_free_forall_iterator (iter);
3795 /* Match the header of a FORALL statement. */
3798 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
3800 gfc_forall_iterator *head, *tail, *new;
3804 gfc_gobble_whitespace ();
3809 if (gfc_match_char ('(') != MATCH_YES)
3812 m = match_forall_iterator (&new);
3813 if (m == MATCH_ERROR)
3822 if (gfc_match_char (',') != MATCH_YES)
3825 m = match_forall_iterator (&new);
3826 if (m == MATCH_ERROR)
3836 /* Have to have a mask expression. */
3838 m = gfc_match_expr (&msk);
3841 if (m == MATCH_ERROR)
3847 if (gfc_match_char (')') == MATCH_NO)
3855 gfc_syntax_error (ST_FORALL);
3858 gfc_free_expr (msk);
3859 gfc_free_forall_iterator (head);
3864 /* Match the rest of a simple FORALL statement that follows an
3868 match_simple_forall (void)
3870 gfc_forall_iterator *head;
3879 m = match_forall_header (&head, &mask);
3886 m = gfc_match_assignment ();
3888 if (m == MATCH_ERROR)
3892 m = gfc_match_pointer_assignment ();
3893 if (m == MATCH_ERROR)
3899 c = gfc_get_code ();
3901 c->loc = gfc_current_locus;
3903 if (gfc_match_eos () != MATCH_YES)
3906 gfc_clear_new_st ();
3907 new_st.op = EXEC_FORALL;
3909 new_st.ext.forall_iterator = head;
3910 new_st.block = gfc_get_code ();
3912 new_st.block->op = EXEC_FORALL;
3913 new_st.block->next = c;
3918 gfc_syntax_error (ST_FORALL);
3921 gfc_free_forall_iterator (head);
3922 gfc_free_expr (mask);
3928 /* Match a FORALL statement. */
3931 gfc_match_forall (gfc_statement *st)
3933 gfc_forall_iterator *head;
3942 m0 = gfc_match_label ();
3943 if (m0 == MATCH_ERROR)
3946 m = gfc_match (" forall");
3950 m = match_forall_header (&head, &mask);
3951 if (m == MATCH_ERROR)
3956 if (gfc_match_eos () == MATCH_YES)
3958 *st = ST_FORALL_BLOCK;
3959 new_st.op = EXEC_FORALL;
3961 new_st.ext.forall_iterator = head;
3965 m = gfc_match_assignment ();
3966 if (m == MATCH_ERROR)
3970 m = gfc_match_pointer_assignment ();
3971 if (m == MATCH_ERROR)
3977 c = gfc_get_code ();
3979 c->loc = gfc_current_locus;
3981 gfc_clear_new_st ();
3982 new_st.op = EXEC_FORALL;
3984 new_st.ext.forall_iterator = head;
3985 new_st.block = gfc_get_code ();
3986 new_st.block->op = EXEC_FORALL;
3987 new_st.block->next = c;
3993 gfc_syntax_error (ST_FORALL);
3996 gfc_free_forall_iterator (head);
3997 gfc_free_expr (mask);
3998 gfc_free_statements (c);