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 /* See if the next character is a special character that has
108 escaped by a \ via the -fbackslash option. */
111 gfc_match_special_char (int *c)
118 switch (gfc_next_char_literal (1))
148 /* Unknown backslash codes are simply not expanded. */
157 /* In free form, match at least one space. Always matches in fixed
161 gfc_match_space (void)
166 if (gfc_current_form == FORM_FIXED)
169 old_loc = gfc_current_locus;
171 c = gfc_next_char ();
172 if (!gfc_is_whitespace (c))
174 gfc_current_locus = old_loc;
178 gfc_gobble_whitespace ();
184 /* Match an end of statement. End of statement is optional
185 whitespace, followed by a ';' or '\n' or comment '!'. If a
186 semicolon is found, we continue to eat whitespace and semicolons. */
198 old_loc = gfc_current_locus;
199 gfc_gobble_whitespace ();
201 c = gfc_next_char ();
207 c = gfc_next_char ();
224 gfc_current_locus = old_loc;
225 return (flag) ? MATCH_YES : MATCH_NO;
229 /* Match a literal integer on the input, setting the value on
230 MATCH_YES. Literal ints occur in kind-parameters as well as
231 old-style character length specifications. If cnt is non-NULL it
232 will be set to the number of digits. */
235 gfc_match_small_literal_int (int *value, int *cnt)
241 old_loc = gfc_current_locus;
243 gfc_gobble_whitespace ();
244 c = gfc_next_char ();
250 gfc_current_locus = old_loc;
259 old_loc = gfc_current_locus;
260 c = gfc_next_char ();
265 i = 10 * i + c - '0';
270 gfc_error ("Integer too large at %C");
275 gfc_current_locus = old_loc;
284 /* Match a small, constant integer expression, like in a kind
285 statement. On MATCH_YES, 'value' is set. */
288 gfc_match_small_int (int *value)
295 m = gfc_match_expr (&expr);
299 p = gfc_extract_int (expr, &i);
300 gfc_free_expr (expr);
313 /* This function is the same as the gfc_match_small_int, except that
314 we're keeping the pointer to the expr. This function could just be
315 removed and the previously mentioned one modified, though all calls
316 to it would have to be modified then (and there were a number of
317 them). Return MATCH_ERROR if fail to extract the int; otherwise,
318 return the result of gfc_match_expr(). The expr (if any) that was
319 matched is returned in the parameter expr. */
322 gfc_match_small_int_expr (int *value, gfc_expr **expr)
328 m = gfc_match_expr (expr);
332 p = gfc_extract_int (*expr, &i);
345 /* Matches a statement label. Uses gfc_match_small_literal_int() to
346 do most of the work. */
349 gfc_match_st_label (gfc_st_label **label)
355 old_loc = gfc_current_locus;
357 m = gfc_match_small_literal_int (&i, &cnt);
363 gfc_error ("Too many digits in statement label at %C");
369 gfc_error ("Statement label at %C is zero");
373 *label = gfc_get_st_label (i);
378 gfc_current_locus = old_loc;
383 /* Match and validate a label associated with a named IF, DO or SELECT
384 statement. If the symbol does not have the label attribute, we add
385 it. We also make sure the symbol does not refer to another
386 (active) block. A matched label is pointed to by gfc_new_block. */
389 gfc_match_label (void)
391 char name[GFC_MAX_SYMBOL_LEN + 1];
394 gfc_new_block = NULL;
396 m = gfc_match (" %n :", name);
400 if (gfc_get_symbol (name, NULL, &gfc_new_block))
402 gfc_error ("Label name '%s' at %C is ambiguous", name);
406 if (gfc_new_block->attr.flavor == FL_LABEL)
408 gfc_error ("Duplicate construct label '%s' at %C", name);
412 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
413 gfc_new_block->name, NULL) == FAILURE)
420 /* See if the current input looks like a name of some sort. Modifies
421 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
422 Note that options.c restricts max_identifier_length to not more
423 than GFC_MAX_SYMBOL_LEN. */
426 gfc_match_name (char *buffer)
431 old_loc = gfc_current_locus;
432 gfc_gobble_whitespace ();
434 c = gfc_next_char ();
435 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
437 if (gfc_error_flag_test() == 0)
438 gfc_error ("Invalid character in name at %C");
439 gfc_current_locus = old_loc;
449 if (i > gfc_option.max_identifier_length)
451 gfc_error ("Name at %C is too long");
455 old_loc = gfc_current_locus;
456 c = gfc_next_char ();
458 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
461 gfc_current_locus = old_loc;
467 /* Match a valid name for C, which is almost the same as for Fortran,
468 except that you can start with an underscore, etc.. It could have
469 been done by modifying the gfc_match_name, but this way other
470 things C allows can be added, such as no limits on the length.
471 Right now, the length is limited to the same thing as Fortran..
472 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
473 input characters from being automatically lower cased, since C is
474 case sensitive. The parameter, buffer, is used to return the name
475 that is matched. Return MATCH_ERROR if the name is too long
476 (though this is a self-imposed limit), MATCH_NO if what we're
477 seeing isn't a name, and MATCH_YES if we successfully match a C
481 gfc_match_name_C (char *buffer)
487 old_loc = gfc_current_locus;
488 gfc_gobble_whitespace ();
490 /* Get the next char (first possible char of name) and see if
491 it's valid for C (either a letter or an underscore). */
492 c = gfc_next_char_literal (1);
494 /* If the user put nothing expect spaces between the quotes, it is valid
495 and simply means there is no name= specifier and the name is the fortran
496 symbol name, all lowercase. */
497 if (c == '"' || c == '\'')
500 gfc_current_locus = old_loc;
504 if (!ISALPHA (c) && c != '_')
506 gfc_error ("Invalid C name in NAME= specifier at %C");
510 /* Continue to read valid variable name characters. */
515 /* C does not define a maximum length of variable names, to my
516 knowledge, but the compiler typically places a limit on them.
517 For now, i'll use the same as the fortran limit for simplicity,
518 but this may need to be changed to a dynamic buffer that can
519 be realloc'ed here if necessary, or more likely, a larger
521 if (i > gfc_option.max_identifier_length)
523 gfc_error ("Name at %C is too long");
527 old_loc = gfc_current_locus;
529 /* Get next char; param means we're in a string. */
530 c = gfc_next_char_literal (1);
531 } while (ISALNUM (c) || c == '_');
534 gfc_current_locus = old_loc;
536 /* See if we stopped because of whitespace. */
539 gfc_gobble_whitespace ();
540 c = gfc_peek_char ();
541 if (c != '"' && c != '\'')
543 gfc_error ("Embedded space in NAME= specifier at %C");
548 /* If we stopped because we had an invalid character for a C name, report
549 that to the user by returning MATCH_NO. */
550 if (c != '"' && c != '\'')
552 gfc_error ("Invalid C name in NAME= specifier at %C");
560 /* Match a symbol on the input. Modifies the pointer to the symbol
561 pointer if successful. */
564 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
566 char buffer[GFC_MAX_SYMBOL_LEN + 1];
569 m = gfc_match_name (buffer);
574 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
575 ? MATCH_ERROR : MATCH_YES;
577 if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
585 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
590 m = gfc_match_sym_tree (&st, host_assoc);
595 *matched_symbol = st->n.sym;
597 *matched_symbol = NULL;
600 *matched_symbol = NULL;
605 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
606 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
610 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
612 locus orig_loc = gfc_current_locus;
615 gfc_gobble_whitespace ();
616 ch = gfc_next_char ();
621 *result = INTRINSIC_PLUS;
626 *result = INTRINSIC_MINUS;
630 if (gfc_next_char () == '=')
633 *result = INTRINSIC_EQ;
639 if (gfc_peek_char () == '=')
643 *result = INTRINSIC_LE;
647 *result = INTRINSIC_LT;
651 if (gfc_peek_char () == '=')
655 *result = INTRINSIC_GE;
659 *result = INTRINSIC_GT;
663 if (gfc_peek_char () == '*')
667 *result = INTRINSIC_POWER;
671 *result = INTRINSIC_TIMES;
675 ch = gfc_peek_char ();
680 *result = INTRINSIC_NE;
687 *result = INTRINSIC_CONCAT;
691 *result = INTRINSIC_DIVIDE;
695 ch = gfc_next_char ();
699 if (gfc_next_char () == 'n'
700 && gfc_next_char () == 'd'
701 && gfc_next_char () == '.')
703 /* Matched ".and.". */
704 *result = INTRINSIC_AND;
710 if (gfc_next_char () == 'q')
712 ch = gfc_next_char ();
715 /* Matched ".eq.". */
716 *result = INTRINSIC_EQ_OS;
721 if (gfc_next_char () == '.')
723 /* Matched ".eqv.". */
724 *result = INTRINSIC_EQV;
732 ch = gfc_next_char ();
735 if (gfc_next_char () == '.')
737 /* Matched ".ge.". */
738 *result = INTRINSIC_GE_OS;
744 if (gfc_next_char () == '.')
746 /* Matched ".gt.". */
747 *result = INTRINSIC_GT_OS;
754 ch = gfc_next_char ();
757 if (gfc_next_char () == '.')
759 /* Matched ".le.". */
760 *result = INTRINSIC_LE_OS;
766 if (gfc_next_char () == '.')
768 /* Matched ".lt.". */
769 *result = INTRINSIC_LT_OS;
776 ch = gfc_next_char ();
779 ch = gfc_next_char ();
782 /* Matched ".ne.". */
783 *result = INTRINSIC_NE_OS;
788 if (gfc_next_char () == 'v'
789 && gfc_next_char () == '.')
791 /* Matched ".neqv.". */
792 *result = INTRINSIC_NEQV;
799 if (gfc_next_char () == 't'
800 && gfc_next_char () == '.')
802 /* Matched ".not.". */
803 *result = INTRINSIC_NOT;
810 if (gfc_next_char () == 'r'
811 && gfc_next_char () == '.')
813 /* Matched ".or.". */
814 *result = INTRINSIC_OR;
828 gfc_current_locus = orig_loc;
833 /* Match a loop control phrase:
835 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
837 If the final integer expression is not present, a constant unity
838 expression is returned. We don't return MATCH_ERROR until after
839 the equals sign is seen. */
842 gfc_match_iterator (gfc_iterator *iter, int init_flag)
844 char name[GFC_MAX_SYMBOL_LEN + 1];
845 gfc_expr *var, *e1, *e2, *e3;
849 /* Match the start of an iterator without affecting the symbol table. */
851 start = gfc_current_locus;
852 m = gfc_match (" %n =", name);
853 gfc_current_locus = start;
858 m = gfc_match_variable (&var, 0);
862 gfc_match_char ('=');
866 if (var->ref != NULL)
868 gfc_error ("Loop variable at %C cannot be a sub-component");
872 if (var->symtree->n.sym->attr.intent == INTENT_IN)
874 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
875 var->symtree->n.sym->name);
879 var->symtree->n.sym->attr.implied_index = 1;
881 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
884 if (m == MATCH_ERROR)
887 if (gfc_match_char (',') != MATCH_YES)
890 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
893 if (m == MATCH_ERROR)
896 if (gfc_match_char (',') != MATCH_YES)
898 e3 = gfc_int_expr (1);
902 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
903 if (m == MATCH_ERROR)
907 gfc_error ("Expected a step value in iterator at %C");
919 gfc_error ("Syntax error in iterator at %C");
930 /* Tries to match the next non-whitespace character on the input.
931 This subroutine does not return MATCH_ERROR. */
934 gfc_match_char (char c)
938 where = gfc_current_locus;
939 gfc_gobble_whitespace ();
941 if (gfc_next_char () == c)
944 gfc_current_locus = where;
949 /* General purpose matching subroutine. The target string is a
950 scanf-like format string in which spaces correspond to arbitrary
951 whitespace (including no whitespace), characters correspond to
952 themselves. The %-codes are:
954 %% Literal percent sign
955 %e Expression, pointer to a pointer is set
956 %s Symbol, pointer to the symbol is set
957 %n Name, character buffer is set to name
958 %t Matches end of statement.
959 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
960 %l Matches a statement label
961 %v Matches a variable expression (an lvalue)
962 % Matches a required space (in free form) and optional spaces. */
965 gfc_match (const char *target, ...)
967 gfc_st_label **label;
976 old_loc = gfc_current_locus;
977 va_start (argp, target);
987 gfc_gobble_whitespace ();
998 vp = va_arg (argp, void **);
999 n = gfc_match_expr ((gfc_expr **) vp);
1010 vp = va_arg (argp, void **);
1011 n = gfc_match_variable ((gfc_expr **) vp, 0);
1022 vp = va_arg (argp, void **);
1023 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1034 np = va_arg (argp, char *);
1035 n = gfc_match_name (np);
1046 label = va_arg (argp, gfc_st_label **);
1047 n = gfc_match_st_label (label);
1058 ip = va_arg (argp, int *);
1059 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1070 if (gfc_match_eos () != MATCH_YES)
1078 if (gfc_match_space () == MATCH_YES)
1084 break; /* Fall through to character matcher. */
1087 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1091 if (c == gfc_next_char ())
1101 /* Clean up after a failed match. */
1102 gfc_current_locus = old_loc;
1103 va_start (argp, target);
1106 for (; matches > 0; matches--)
1108 while (*p++ != '%');
1116 /* Matches that don't have to be undone */
1121 (void) va_arg (argp, void **);
1126 vp = va_arg (argp, void **);
1127 gfc_free_expr (*vp);
1140 /*********************** Statement level matching **********************/
1142 /* Matches the start of a program unit, which is the program keyword
1143 followed by an obligatory symbol. */
1146 gfc_match_program (void)
1151 m = gfc_match ("% %s%t", &sym);
1155 gfc_error ("Invalid form of PROGRAM statement at %C");
1159 if (m == MATCH_ERROR)
1162 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
1165 gfc_new_block = sym;
1171 /* Match a simple assignment statement. */
1174 gfc_match_assignment (void)
1176 gfc_expr *lvalue, *rvalue;
1180 old_loc = gfc_current_locus;
1183 m = gfc_match (" %v =", &lvalue);
1186 gfc_current_locus = old_loc;
1187 gfc_free_expr (lvalue);
1191 if (lvalue->symtree->n.sym->attr.protected
1192 && lvalue->symtree->n.sym->attr.use_assoc)
1194 gfc_current_locus = old_loc;
1195 gfc_free_expr (lvalue);
1196 gfc_error ("Setting value of PROTECTED variable at %C");
1201 m = gfc_match (" %e%t", &rvalue);
1204 gfc_current_locus = old_loc;
1205 gfc_free_expr (lvalue);
1206 gfc_free_expr (rvalue);
1210 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1212 new_st.op = EXEC_ASSIGN;
1213 new_st.expr = lvalue;
1214 new_st.expr2 = rvalue;
1216 gfc_check_do_variable (lvalue->symtree);
1222 /* Match a pointer assignment statement. */
1225 gfc_match_pointer_assignment (void)
1227 gfc_expr *lvalue, *rvalue;
1231 old_loc = gfc_current_locus;
1233 lvalue = rvalue = NULL;
1235 m = gfc_match (" %v =>", &lvalue);
1242 m = gfc_match (" %e%t", &rvalue);
1246 if (lvalue->symtree->n.sym->attr.protected
1247 && lvalue->symtree->n.sym->attr.use_assoc)
1249 gfc_error ("Assigning to a PROTECTED pointer at %C");
1254 new_st.op = EXEC_POINTER_ASSIGN;
1255 new_st.expr = lvalue;
1256 new_st.expr2 = rvalue;
1261 gfc_current_locus = old_loc;
1262 gfc_free_expr (lvalue);
1263 gfc_free_expr (rvalue);
1268 /* We try to match an easy arithmetic IF statement. This only happens
1269 when just after having encountered a simple IF statement. This code
1270 is really duplicate with parts of the gfc_match_if code, but this is
1274 match_arithmetic_if (void)
1276 gfc_st_label *l1, *l2, *l3;
1280 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1284 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1285 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1286 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1288 gfc_free_expr (expr);
1292 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF statement "
1293 "at %C") == FAILURE)
1296 new_st.op = EXEC_ARITHMETIC_IF;
1306 /* The IF statement is a bit of a pain. First of all, there are three
1307 forms of it, the simple IF, the IF that starts a block and the
1310 There is a problem with the simple IF and that is the fact that we
1311 only have a single level of undo information on symbols. What this
1312 means is for a simple IF, we must re-match the whole IF statement
1313 multiple times in order to guarantee that the symbol table ends up
1314 in the proper state. */
1316 static match match_simple_forall (void);
1317 static match match_simple_where (void);
1320 gfc_match_if (gfc_statement *if_type)
1323 gfc_st_label *l1, *l2, *l3;
1328 n = gfc_match_label ();
1329 if (n == MATCH_ERROR)
1332 old_loc = gfc_current_locus;
1334 m = gfc_match (" if ( %e", &expr);
1338 if (gfc_match_char (')') != MATCH_YES)
1340 gfc_error ("Syntax error in IF-expression at %C");
1341 gfc_free_expr (expr);
1345 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1351 gfc_error ("Block label not appropriate for arithmetic IF "
1353 gfc_free_expr (expr);
1357 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1358 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1359 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1361 gfc_free_expr (expr);
1365 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent: arithmetic IF "
1366 "statement at %C") == FAILURE)
1369 new_st.op = EXEC_ARITHMETIC_IF;
1375 *if_type = ST_ARITHMETIC_IF;
1379 if (gfc_match (" then%t") == MATCH_YES)
1381 new_st.op = EXEC_IF;
1383 *if_type = ST_IF_BLOCK;
1389 gfc_error ("Block label is not appropriate IF statement at %C");
1390 gfc_free_expr (expr);
1394 /* At this point the only thing left is a simple IF statement. At
1395 this point, n has to be MATCH_NO, so we don't have to worry about
1396 re-matching a block label. From what we've got so far, try
1397 matching an assignment. */
1399 *if_type = ST_SIMPLE_IF;
1401 m = gfc_match_assignment ();
1405 gfc_free_expr (expr);
1406 gfc_undo_symbols ();
1407 gfc_current_locus = old_loc;
1409 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1410 assignment was found. For MATCH_NO, continue to call the various
1412 if (m == MATCH_ERROR)
1415 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1417 m = gfc_match_pointer_assignment ();
1421 gfc_free_expr (expr);
1422 gfc_undo_symbols ();
1423 gfc_current_locus = old_loc;
1425 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1427 /* Look at the next keyword to see which matcher to call. Matching
1428 the keyword doesn't affect the symbol table, so we don't have to
1429 restore between tries. */
1431 #define match(string, subr, statement) \
1432 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1436 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1437 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1438 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1439 match ("call", gfc_match_call, ST_CALL)
1440 match ("close", gfc_match_close, ST_CLOSE)
1441 match ("continue", gfc_match_continue, ST_CONTINUE)
1442 match ("cycle", gfc_match_cycle, ST_CYCLE)
1443 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1444 match ("end file", gfc_match_endfile, ST_END_FILE)
1445 match ("exit", gfc_match_exit, ST_EXIT)
1446 match ("flush", gfc_match_flush, ST_FLUSH)
1447 match ("forall", match_simple_forall, ST_FORALL)
1448 match ("go to", gfc_match_goto, ST_GOTO)
1449 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1450 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1451 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1452 match ("open", gfc_match_open, ST_OPEN)
1453 match ("pause", gfc_match_pause, ST_NONE)
1454 match ("print", gfc_match_print, ST_WRITE)
1455 match ("read", gfc_match_read, ST_READ)
1456 match ("return", gfc_match_return, ST_RETURN)
1457 match ("rewind", gfc_match_rewind, ST_REWIND)
1458 match ("stop", gfc_match_stop, ST_STOP)
1459 match ("where", match_simple_where, ST_WHERE)
1460 match ("write", gfc_match_write, ST_WRITE)
1462 /* The gfc_match_assignment() above may have returned a MATCH_NO
1463 where the assignment was to a named constant. Check that
1464 special case here. */
1465 m = gfc_match_assignment ();
1468 gfc_error ("Cannot assign to a named constant at %C");
1469 gfc_free_expr (expr);
1470 gfc_undo_symbols ();
1471 gfc_current_locus = old_loc;
1475 /* All else has failed, so give up. See if any of the matchers has
1476 stored an error message of some sort. */
1477 if (gfc_error_check () == 0)
1478 gfc_error ("Unclassifiable statement in IF-clause at %C");
1480 gfc_free_expr (expr);
1485 gfc_error ("Syntax error in IF-clause at %C");
1488 gfc_free_expr (expr);
1492 /* At this point, we've matched the single IF and the action clause
1493 is in new_st. Rearrange things so that the IF statement appears
1496 p = gfc_get_code ();
1497 p->next = gfc_get_code ();
1499 p->next->loc = gfc_current_locus;
1504 gfc_clear_new_st ();
1506 new_st.op = EXEC_IF;
1515 /* Match an ELSE statement. */
1518 gfc_match_else (void)
1520 char name[GFC_MAX_SYMBOL_LEN + 1];
1522 if (gfc_match_eos () == MATCH_YES)
1525 if (gfc_match_name (name) != MATCH_YES
1526 || gfc_current_block () == NULL
1527 || gfc_match_eos () != MATCH_YES)
1529 gfc_error ("Unexpected junk after ELSE statement at %C");
1533 if (strcmp (name, gfc_current_block ()->name) != 0)
1535 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1536 name, gfc_current_block ()->name);
1544 /* Match an ELSE IF statement. */
1547 gfc_match_elseif (void)
1549 char name[GFC_MAX_SYMBOL_LEN + 1];
1553 m = gfc_match (" ( %e ) then", &expr);
1557 if (gfc_match_eos () == MATCH_YES)
1560 if (gfc_match_name (name) != MATCH_YES
1561 || gfc_current_block () == NULL
1562 || gfc_match_eos () != MATCH_YES)
1564 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1568 if (strcmp (name, gfc_current_block ()->name) != 0)
1570 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1571 name, gfc_current_block ()->name);
1576 new_st.op = EXEC_IF;
1581 gfc_free_expr (expr);
1586 /* Free a gfc_iterator structure. */
1589 gfc_free_iterator (gfc_iterator *iter, int flag)
1595 gfc_free_expr (iter->var);
1596 gfc_free_expr (iter->start);
1597 gfc_free_expr (iter->end);
1598 gfc_free_expr (iter->step);
1605 /* Match a DO statement. */
1610 gfc_iterator iter, *ip;
1612 gfc_st_label *label;
1615 old_loc = gfc_current_locus;
1618 iter.var = iter.start = iter.end = iter.step = NULL;
1620 m = gfc_match_label ();
1621 if (m == MATCH_ERROR)
1624 if (gfc_match (" do") != MATCH_YES)
1627 m = gfc_match_st_label (&label);
1628 if (m == MATCH_ERROR)
1631 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
1633 if (gfc_match_eos () == MATCH_YES)
1635 iter.end = gfc_logical_expr (1, NULL);
1636 new_st.op = EXEC_DO_WHILE;
1640 /* Match an optional comma, if no comma is found, a space is obligatory. */
1641 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
1644 /* See if we have a DO WHILE. */
1645 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1647 new_st.op = EXEC_DO_WHILE;
1651 /* The abortive DO WHILE may have done something to the symbol
1652 table, so we start over. */
1653 gfc_undo_symbols ();
1654 gfc_current_locus = old_loc;
1656 gfc_match_label (); /* This won't error. */
1657 gfc_match (" do "); /* This will work. */
1659 gfc_match_st_label (&label); /* Can't error out. */
1660 gfc_match_char (','); /* Optional comma. */
1662 m = gfc_match_iterator (&iter, 0);
1665 if (m == MATCH_ERROR)
1668 iter.var->symtree->n.sym->attr.implied_index = 0;
1669 gfc_check_do_variable (iter.var->symtree);
1671 if (gfc_match_eos () != MATCH_YES)
1673 gfc_syntax_error (ST_DO);
1677 new_st.op = EXEC_DO;
1681 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1684 new_st.label = label;
1686 if (new_st.op == EXEC_DO_WHILE)
1687 new_st.expr = iter.end;
1690 new_st.ext.iterator = ip = gfc_get_iterator ();
1697 gfc_free_iterator (&iter, 0);
1703 /* Match an EXIT or CYCLE statement. */
1706 match_exit_cycle (gfc_statement st, gfc_exec_op op)
1708 gfc_state_data *p, *o;
1712 if (gfc_match_eos () == MATCH_YES)
1716 m = gfc_match ("% %s%t", &sym);
1717 if (m == MATCH_ERROR)
1721 gfc_syntax_error (st);
1725 if (sym->attr.flavor != FL_LABEL)
1727 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1728 sym->name, gfc_ascii_statement (st));
1733 /* Find the loop mentioned specified by the label (or lack of a label). */
1734 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
1735 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1737 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1743 gfc_error ("%s statement at %C is not within a loop",
1744 gfc_ascii_statement (st));
1746 gfc_error ("%s statement at %C is not within loop '%s'",
1747 gfc_ascii_statement (st), sym->name);
1754 gfc_error ("%s statement at %C leaving OpenMP structured block",
1755 gfc_ascii_statement (st));
1758 else if (st == ST_EXIT
1759 && p->previous != NULL
1760 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1761 && (p->previous->head->op == EXEC_OMP_DO
1762 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1764 gcc_assert (p->previous->head->next != NULL);
1765 gcc_assert (p->previous->head->next->op == EXEC_DO
1766 || p->previous->head->next->op == EXEC_DO_WHILE);
1767 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1771 /* Save the first statement in the loop - needed by the backend. */
1772 new_st.ext.whichloop = p->head;
1780 /* Match the EXIT statement. */
1783 gfc_match_exit (void)
1785 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1789 /* Match the CYCLE statement. */
1792 gfc_match_cycle (void)
1794 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1798 /* Match a number or character constant after a STOP or PAUSE statement. */
1801 gfc_match_stopcode (gfc_statement st)
1811 if (gfc_match_eos () != MATCH_YES)
1813 m = gfc_match_small_literal_int (&stop_code, &cnt);
1814 if (m == MATCH_ERROR)
1817 if (m == MATCH_YES && cnt > 5)
1819 gfc_error ("Too many digits in STOP code at %C");
1825 /* Try a character constant. */
1826 m = gfc_match_expr (&e);
1827 if (m == MATCH_ERROR)
1831 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1835 if (gfc_match_eos () != MATCH_YES)
1839 if (gfc_pure (NULL))
1841 gfc_error ("%s statement not allowed in PURE procedure at %C",
1842 gfc_ascii_statement (st));
1846 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1848 new_st.ext.stop_code = stop_code;
1853 gfc_syntax_error (st);
1862 /* Match the (deprecated) PAUSE statement. */
1865 gfc_match_pause (void)
1869 m = gfc_match_stopcode (ST_PAUSE);
1872 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
1881 /* Match the STOP statement. */
1884 gfc_match_stop (void)
1886 return gfc_match_stopcode (ST_STOP);
1890 /* Match a CONTINUE statement. */
1893 gfc_match_continue (void)
1895 if (gfc_match_eos () != MATCH_YES)
1897 gfc_syntax_error (ST_CONTINUE);
1901 new_st.op = EXEC_CONTINUE;
1906 /* Match the (deprecated) ASSIGN statement. */
1909 gfc_match_assign (void)
1912 gfc_st_label *label;
1914 if (gfc_match (" %l", &label) == MATCH_YES)
1916 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1918 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1920 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
1925 expr->symtree->n.sym->attr.assign = 1;
1927 new_st.op = EXEC_LABEL_ASSIGN;
1928 new_st.label = label;
1937 /* Match the GO TO statement. As a computed GOTO statement is
1938 matched, it is transformed into an equivalent SELECT block. No
1939 tree is necessary, and the resulting jumps-to-jumps are
1940 specifically optimized away by the back end. */
1943 gfc_match_goto (void)
1945 gfc_code *head, *tail;
1948 gfc_st_label *label;
1952 if (gfc_match (" %l%t", &label) == MATCH_YES)
1954 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1957 new_st.op = EXEC_GOTO;
1958 new_st.label = label;
1962 /* The assigned GO TO statement. */
1964 if (gfc_match_variable (&expr, 0) == MATCH_YES)
1966 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
1971 new_st.op = EXEC_GOTO;
1974 if (gfc_match_eos () == MATCH_YES)
1977 /* Match label list. */
1978 gfc_match_char (',');
1979 if (gfc_match_char ('(') != MATCH_YES)
1981 gfc_syntax_error (ST_GOTO);
1988 m = gfc_match_st_label (&label);
1992 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1996 head = tail = gfc_get_code ();
1999 tail->block = gfc_get_code ();
2003 tail->label = label;
2004 tail->op = EXEC_GOTO;
2006 while (gfc_match_char (',') == MATCH_YES);
2008 if (gfc_match (")%t") != MATCH_YES)
2013 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2016 new_st.block = head;
2021 /* Last chance is a computed GO TO statement. */
2022 if (gfc_match_char ('(') != MATCH_YES)
2024 gfc_syntax_error (ST_GOTO);
2033 m = gfc_match_st_label (&label);
2037 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2041 head = tail = gfc_get_code ();
2044 tail->block = gfc_get_code ();
2048 cp = gfc_get_case ();
2049 cp->low = cp->high = gfc_int_expr (i++);
2051 tail->op = EXEC_SELECT;
2052 tail->ext.case_list = cp;
2054 tail->next = gfc_get_code ();
2055 tail->next->op = EXEC_GOTO;
2056 tail->next->label = label;
2058 while (gfc_match_char (',') == MATCH_YES);
2060 if (gfc_match_char (')') != MATCH_YES)
2065 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2069 /* Get the rest of the statement. */
2070 gfc_match_char (',');
2072 if (gfc_match (" %e%t", &expr) != MATCH_YES)
2075 /* At this point, a computed GOTO has been fully matched and an
2076 equivalent SELECT statement constructed. */
2078 new_st.op = EXEC_SELECT;
2081 /* Hack: For a "real" SELECT, the expression is in expr. We put
2082 it in expr2 so we can distinguish then and produce the correct
2084 new_st.expr2 = expr;
2085 new_st.block = head;
2089 gfc_syntax_error (ST_GOTO);
2091 gfc_free_statements (head);
2096 /* Frees a list of gfc_alloc structures. */
2099 gfc_free_alloc_list (gfc_alloc *p)
2106 gfc_free_expr (p->expr);
2112 /* Match an ALLOCATE statement. */
2115 gfc_match_allocate (void)
2117 gfc_alloc *head, *tail;
2124 if (gfc_match_char ('(') != MATCH_YES)
2130 head = tail = gfc_get_alloc ();
2133 tail->next = gfc_get_alloc ();
2137 m = gfc_match_variable (&tail->expr, 0);
2140 if (m == MATCH_ERROR)
2143 if (gfc_check_do_variable (tail->expr->symtree))
2147 && gfc_impure_variable (tail->expr->symtree->n.sym))
2149 gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
2154 if (tail->expr->ts.type == BT_DERIVED)
2155 tail->expr->ts.derived = gfc_use_derived (tail->expr->ts.derived);
2157 if (gfc_match_char (',') != MATCH_YES)
2160 m = gfc_match (" stat = %v", &stat);
2161 if (m == MATCH_ERROR)
2171 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
2173 gfc_error ("STAT variable '%s' of ALLOCATE statement at %C cannot "
2174 "be INTENT(IN)", stat->symtree->n.sym->name);
2178 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
2180 gfc_error ("Illegal STAT variable in ALLOCATE statement at %C "
2181 "for a PURE procedure");
2185 is_variable = false;
2186 if (stat->symtree->n.sym->attr.flavor == FL_VARIABLE)
2188 else if (stat->symtree->n.sym->attr.function
2189 && stat->symtree->n.sym->result == stat->symtree->n.sym
2190 && (gfc_current_ns->proc_name == stat->symtree->n.sym
2191 || (gfc_current_ns->parent
2192 && gfc_current_ns->parent->proc_name
2193 == stat->symtree->n.sym)))
2195 else if (gfc_current_ns->entries
2196 && stat->symtree->n.sym->result == stat->symtree->n.sym)
2199 for (el = gfc_current_ns->entries; el; el = el->next)
2200 if (el->sym == stat->symtree->n.sym)
2205 else if (gfc_current_ns->parent && gfc_current_ns->parent->entries
2206 && stat->symtree->n.sym->result == stat->symtree->n.sym)
2209 for (el = gfc_current_ns->parent->entries; el; el = el->next)
2210 if (el->sym == stat->symtree->n.sym)
2218 gfc_error ("STAT expression at %C must be a variable");
2222 gfc_check_do_variable(stat->symtree);
2225 if (gfc_match (" )%t") != MATCH_YES)
2228 new_st.op = EXEC_ALLOCATE;
2230 new_st.ext.alloc_list = head;
2235 gfc_syntax_error (ST_ALLOCATE);
2238 gfc_free_expr (stat);
2239 gfc_free_alloc_list (head);
2244 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
2245 a set of pointer assignments to intrinsic NULL(). */
2248 gfc_match_nullify (void)
2256 if (gfc_match_char ('(') != MATCH_YES)
2261 m = gfc_match_variable (&p, 0);
2262 if (m == MATCH_ERROR)
2267 if (gfc_check_do_variable (p->symtree))
2270 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
2272 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
2276 /* build ' => NULL() '. */
2277 e = gfc_get_expr ();
2278 e->where = gfc_current_locus;
2279 e->expr_type = EXPR_NULL;
2280 e->ts.type = BT_UNKNOWN;
2282 /* Chain to list. */
2287 tail->next = gfc_get_code ();
2291 tail->op = EXEC_POINTER_ASSIGN;
2295 if (gfc_match (" )%t") == MATCH_YES)
2297 if (gfc_match_char (',') != MATCH_YES)
2304 gfc_syntax_error (ST_NULLIFY);
2307 gfc_free_statements (new_st.next);
2312 /* Match a DEALLOCATE statement. */
2315 gfc_match_deallocate (void)
2317 gfc_alloc *head, *tail;
2324 if (gfc_match_char ('(') != MATCH_YES)
2330 head = tail = gfc_get_alloc ();
2333 tail->next = gfc_get_alloc ();
2337 m = gfc_match_variable (&tail->expr, 0);
2338 if (m == MATCH_ERROR)
2343 if (gfc_check_do_variable (tail->expr->symtree))
2347 && gfc_impure_variable (tail->expr->symtree->n.sym))
2349 gfc_error ("Illegal deallocate-expression in DEALLOCATE at %C "
2350 "for a PURE procedure");
2354 if (gfc_match_char (',') != MATCH_YES)
2357 m = gfc_match (" stat = %v", &stat);
2358 if (m == MATCH_ERROR)
2366 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
2368 gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
2369 "cannot be INTENT(IN)", stat->symtree->n.sym->name);
2373 if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
2375 gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
2376 "for a PURE procedure");
2380 if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
2382 gfc_error ("STAT expression at %C must be a variable");
2386 gfc_check_do_variable(stat->symtree);
2389 if (gfc_match (" )%t") != MATCH_YES)
2392 new_st.op = EXEC_DEALLOCATE;
2394 new_st.ext.alloc_list = head;
2399 gfc_syntax_error (ST_DEALLOCATE);
2402 gfc_free_expr (stat);
2403 gfc_free_alloc_list (head);
2408 /* Match a RETURN statement. */
2411 gfc_match_return (void)
2415 gfc_compile_state s;
2419 if (gfc_match_eos () == MATCH_YES)
2422 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2424 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2429 if (gfc_current_form == FORM_FREE)
2431 /* The following are valid, so we can't require a blank after the
2435 c = gfc_peek_char ();
2436 if (ISALPHA (c) || ISDIGIT (c))
2440 m = gfc_match (" %e%t", &e);
2443 if (m == MATCH_ERROR)
2446 gfc_syntax_error (ST_RETURN);
2453 gfc_enclosing_unit (&s);
2454 if (s == COMP_PROGRAM
2455 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2456 "main program at %C") == FAILURE)
2459 new_st.op = EXEC_RETURN;
2466 /* Match a CALL statement. The tricky part here are possible
2467 alternate return specifiers. We handle these by having all
2468 "subroutines" actually return an integer via a register that gives
2469 the return number. If the call specifies alternate returns, we
2470 generate code for a SELECT statement whose case clauses contain
2471 GOTOs to the various labels. */
2474 gfc_match_call (void)
2476 char name[GFC_MAX_SYMBOL_LEN + 1];
2477 gfc_actual_arglist *a, *arglist;
2487 m = gfc_match ("% %n", name);
2493 if (gfc_get_ha_sym_tree (name, &st))
2498 /* If it does not seem to be callable... */
2499 if (!sym->attr.generic
2500 && !sym->attr.subroutine)
2502 if (!(sym->attr.external && !sym->attr.referenced))
2504 /* ...create a symbol in this scope... */
2505 if (sym->ns != gfc_current_ns
2506 && gfc_get_sym_tree (name, NULL, &st) == 1)
2509 if (sym != st->n.sym)
2513 /* ...and then to try to make the symbol into a subroutine. */
2514 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2518 gfc_set_sym_referenced (sym);
2520 if (gfc_match_eos () != MATCH_YES)
2522 m = gfc_match_actual_arglist (1, &arglist);
2525 if (m == MATCH_ERROR)
2528 if (gfc_match_eos () != MATCH_YES)
2532 /* If any alternate return labels were found, construct a SELECT
2533 statement that will jump to the right place. */
2536 for (a = arglist; a; a = a->next)
2537 if (a->expr == NULL)
2542 gfc_symtree *select_st;
2543 gfc_symbol *select_sym;
2544 char name[GFC_MAX_SYMBOL_LEN + 1];
2546 new_st.next = c = gfc_get_code ();
2547 c->op = EXEC_SELECT;
2548 sprintf (name, "_result_%s", sym->name);
2549 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
2551 select_sym = select_st->n.sym;
2552 select_sym->ts.type = BT_INTEGER;
2553 select_sym->ts.kind = gfc_default_integer_kind;
2554 gfc_set_sym_referenced (select_sym);
2555 c->expr = gfc_get_expr ();
2556 c->expr->expr_type = EXPR_VARIABLE;
2557 c->expr->symtree = select_st;
2558 c->expr->ts = select_sym->ts;
2559 c->expr->where = gfc_current_locus;
2562 for (a = arglist; a; a = a->next)
2564 if (a->expr != NULL)
2567 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2572 c->block = gfc_get_code ();
2574 c->op = EXEC_SELECT;
2576 new_case = gfc_get_case ();
2577 new_case->high = new_case->low = gfc_int_expr (i);
2578 c->ext.case_list = new_case;
2580 c->next = gfc_get_code ();
2581 c->next->op = EXEC_GOTO;
2582 c->next->label = a->label;
2586 new_st.op = EXEC_CALL;
2587 new_st.symtree = st;
2588 new_st.ext.actual = arglist;
2593 gfc_syntax_error (ST_CALL);
2596 gfc_free_actual_arglist (arglist);
2601 /* Given a name, return a pointer to the common head structure,
2602 creating it if it does not exist. If FROM_MODULE is nonzero, we
2603 mangle the name so that it doesn't interfere with commons defined
2604 in the using namespace.
2605 TODO: Add to global symbol tree. */
2608 gfc_get_common (const char *name, int from_module)
2611 static int serial = 0;
2612 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
2616 /* A use associated common block is only needed to correctly layout
2617 the variables it contains. */
2618 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2619 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2623 st = gfc_find_symtree (gfc_current_ns->common_root, name);
2626 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2629 if (st->n.common == NULL)
2631 st->n.common = gfc_get_common_head ();
2632 st->n.common->where = gfc_current_locus;
2633 strcpy (st->n.common->name, name);
2636 return st->n.common;
2640 /* Match a common block name. */
2642 match match_common_name (char *name)
2646 if (gfc_match_char ('/') == MATCH_NO)
2652 if (gfc_match_char ('/') == MATCH_YES)
2658 m = gfc_match_name (name);
2660 if (m == MATCH_ERROR)
2662 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2665 gfc_error ("Syntax error in common block name at %C");
2670 /* Match a COMMON statement. */
2673 gfc_match_common (void)
2675 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2676 char name[GFC_MAX_SYMBOL_LEN + 1];
2683 old_blank_common = gfc_current_ns->blank_common.head;
2684 if (old_blank_common)
2686 while (old_blank_common->common_next)
2687 old_blank_common = old_blank_common->common_next;
2694 m = match_common_name (name);
2695 if (m == MATCH_ERROR)
2698 gsym = gfc_get_gsymbol (name);
2699 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2701 gfc_error ("Symbol '%s' at %C is already an external symbol that "
2702 "is not COMMON", name);
2706 if (gsym->type == GSYM_UNKNOWN)
2708 gsym->type = GSYM_COMMON;
2709 gsym->where = gfc_current_locus;
2715 if (name[0] == '\0')
2717 if (gfc_current_ns->is_block_data)
2719 gfc_warning ("BLOCK DATA unit cannot contain blank COMMON "
2722 t = &gfc_current_ns->blank_common;
2723 if (t->head == NULL)
2724 t->where = gfc_current_locus;
2728 t = gfc_get_common (name, 0);
2737 while (tail->common_next)
2738 tail = tail->common_next;
2741 /* Grab the list of symbols. */
2744 m = gfc_match_symbol (&sym, 0);
2745 if (m == MATCH_ERROR)
2750 /* Store a ref to the common block for error checking. */
2751 sym->common_block = t;
2753 /* See if we know the current common block is bind(c), and if
2754 so, then see if we can check if the symbol is (which it'll
2755 need to be). This can happen if the bind(c) attr stmt was
2756 applied to the common block, and the variable(s) already
2757 defined, before declaring the common block. */
2758 if (t->is_bind_c == 1)
2760 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
2762 /* If we find an error, just print it and continue,
2763 cause it's just semantic, and we can see if there
2765 gfc_error_now ("Variable '%s' at %L in common block '%s' "
2766 "at %C must be declared with a C "
2767 "interoperable kind since common block "
2769 sym->name, &(sym->declared_at), t->name,
2773 if (sym->attr.is_bind_c == 1)
2774 gfc_error_now ("Variable '%s' in common block "
2775 "'%s' at %C can not be bind(c) since "
2776 "it is not global", sym->name, t->name);
2779 if (sym->attr.in_common)
2781 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2786 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2790 tail->common_next = sym;
2796 /* Deal with an optional array specification after the
2798 m = gfc_match_array_spec (&as);
2799 if (m == MATCH_ERROR)
2804 if (as->type != AS_EXPLICIT)
2806 gfc_error ("Array specification for symbol '%s' in COMMON "
2807 "at %C must be explicit", sym->name);
2811 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2814 if (sym->attr.pointer)
2816 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
2817 "POINTER array", sym->name);
2826 sym->common_head = t;
2828 /* Check to see if the symbol is already in an equivalence group.
2829 If it is, set the other members as being in common. */
2830 if (sym->attr.in_equivalence)
2832 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2834 for (e2 = e1; e2; e2 = e2->eq)
2835 if (e2->expr->symtree->n.sym == sym)
2842 for (e2 = e1; e2; e2 = e2->eq)
2844 other = e2->expr->symtree->n.sym;
2845 if (other->common_head
2846 && other->common_head != sym->common_head)
2848 gfc_error ("Symbol '%s', in COMMON block '%s' at "
2849 "%C is being indirectly equivalenced to "
2850 "another COMMON block '%s'",
2851 sym->name, sym->common_head->name,
2852 other->common_head->name);
2855 other->attr.in_common = 1;
2856 other->common_head = t;
2862 gfc_gobble_whitespace ();
2863 if (gfc_match_eos () == MATCH_YES)
2865 if (gfc_peek_char () == '/')
2867 if (gfc_match_char (',') != MATCH_YES)
2869 gfc_gobble_whitespace ();
2870 if (gfc_peek_char () == '/')
2879 gfc_syntax_error (ST_COMMON);
2882 if (old_blank_common)
2883 old_blank_common->common_next = NULL;
2885 gfc_current_ns->blank_common.head = NULL;
2886 gfc_free_array_spec (as);
2891 /* Match a BLOCK DATA program unit. */
2894 gfc_match_block_data (void)
2896 char name[GFC_MAX_SYMBOL_LEN + 1];
2900 if (gfc_match_eos () == MATCH_YES)
2902 gfc_new_block = NULL;
2906 m = gfc_match ("% %n%t", name);
2910 if (gfc_get_symbol (name, NULL, &sym))
2913 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2916 gfc_new_block = sym;
2922 /* Free a namelist structure. */
2925 gfc_free_namelist (gfc_namelist *name)
2929 for (; name; name = n)
2937 /* Match a NAMELIST statement. */
2940 gfc_match_namelist (void)
2942 gfc_symbol *group_name, *sym;
2946 m = gfc_match (" / %s /", &group_name);
2949 if (m == MATCH_ERROR)
2954 if (group_name->ts.type != BT_UNKNOWN)
2956 gfc_error ("Namelist group name '%s' at %C already has a basic "
2957 "type of %s", group_name->name,
2958 gfc_typename (&group_name->ts));
2962 if (group_name->attr.flavor == FL_NAMELIST
2963 && group_name->attr.use_assoc
2964 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2965 "at %C already is USE associated and can"
2966 "not be respecified.", group_name->name)
2970 if (group_name->attr.flavor != FL_NAMELIST
2971 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2972 group_name->name, NULL) == FAILURE)
2977 m = gfc_match_symbol (&sym, 1);
2980 if (m == MATCH_ERROR)
2983 if (sym->attr.in_namelist == 0
2984 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2987 /* Use gfc_error_check here, rather than goto error, so that
2988 these are the only errors for the next two lines. */
2989 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2991 gfc_error ("Assumed size array '%s' in namelist '%s' at "
2992 "%C is not allowed", sym->name, group_name->name);
2996 if (sym->ts.type == BT_CHARACTER && sym->ts.cl->length == NULL)
2998 gfc_error ("Assumed character length '%s' in namelist '%s' at "
2999 "%C is not allowed", sym->name, group_name->name);
3003 nl = gfc_get_namelist ();
3007 if (group_name->namelist == NULL)
3008 group_name->namelist = group_name->namelist_tail = nl;
3011 group_name->namelist_tail->next = nl;
3012 group_name->namelist_tail = nl;
3015 if (gfc_match_eos () == MATCH_YES)
3018 m = gfc_match_char (',');
3020 if (gfc_match_char ('/') == MATCH_YES)
3022 m2 = gfc_match (" %s /", &group_name);
3023 if (m2 == MATCH_YES)
3025 if (m2 == MATCH_ERROR)
3039 gfc_syntax_error (ST_NAMELIST);
3046 /* Match a MODULE statement. */
3049 gfc_match_module (void)
3053 m = gfc_match (" %s%t", &gfc_new_block);
3057 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
3058 gfc_new_block->name, NULL) == FAILURE)
3065 /* Free equivalence sets and lists. Recursively is the easiest way to
3069 gfc_free_equiv (gfc_equiv *eq)
3074 gfc_free_equiv (eq->eq);
3075 gfc_free_equiv (eq->next);
3076 gfc_free_expr (eq->expr);
3081 /* Match an EQUIVALENCE statement. */
3084 gfc_match_equivalence (void)
3086 gfc_equiv *eq, *set, *tail;
3090 gfc_common_head *common_head = NULL;
3098 eq = gfc_get_equiv ();
3102 eq->next = gfc_current_ns->equiv;
3103 gfc_current_ns->equiv = eq;
3105 if (gfc_match_char ('(') != MATCH_YES)
3109 common_flag = FALSE;
3114 m = gfc_match_equiv_variable (&set->expr);
3115 if (m == MATCH_ERROR)
3120 /* count the number of objects. */
3123 if (gfc_match_char ('%') == MATCH_YES)
3125 gfc_error ("Derived type component %C is not a "
3126 "permitted EQUIVALENCE member");
3130 for (ref = set->expr->ref; ref; ref = ref->next)
3131 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3133 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
3134 "be an array section");
3138 sym = set->expr->symtree->n.sym;
3140 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
3143 if (sym->attr.in_common)
3146 common_head = sym->common_head;
3149 if (gfc_match_char (')') == MATCH_YES)
3152 if (gfc_match_char (',') != MATCH_YES)
3155 set->eq = gfc_get_equiv ();
3161 gfc_error ("EQUIVALENCE at %C requires two or more objects");
3165 /* If one of the members of an equivalence is in common, then
3166 mark them all as being in common. Before doing this, check
3167 that members of the equivalence group are not in different
3170 for (set = eq; set; set = set->eq)
3172 sym = set->expr->symtree->n.sym;
3173 if (sym->common_head && sym->common_head != common_head)
3175 gfc_error ("Attempt to indirectly overlap COMMON "
3176 "blocks %s and %s by EQUIVALENCE at %C",
3177 sym->common_head->name, common_head->name);
3180 sym->attr.in_common = 1;
3181 sym->common_head = common_head;
3184 if (gfc_match_eos () == MATCH_YES)
3186 if (gfc_match_char (',') != MATCH_YES)
3193 gfc_syntax_error (ST_EQUIVALENCE);
3199 gfc_free_equiv (gfc_current_ns->equiv);
3200 gfc_current_ns->equiv = eq;
3206 /* Check that a statement function is not recursive. This is done by looking
3207 for the statement function symbol(sym) by looking recursively through its
3208 expression(e). If a reference to sym is found, true is returned.
3209 12.5.4 requires that any variable of function that is implicitly typed
3210 shall have that type confirmed by any subsequent type declaration. The
3211 implicit typing is conveniently done here. */
3214 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
3216 gfc_actual_arglist *arg;
3223 switch (e->expr_type)
3226 for (arg = e->value.function.actual; arg; arg = arg->next)
3228 if (sym->name == arg->name || recursive_stmt_fcn (arg->expr, sym))
3232 if (e->symtree == NULL)
3235 /* Check the name before testing for nested recursion! */
3236 if (sym->name == e->symtree->n.sym->name)
3239 /* Catch recursion via other statement functions. */
3240 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
3241 && e->symtree->n.sym->value
3242 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
3245 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3246 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3251 if (e->symtree && sym->name == e->symtree->n.sym->name)
3254 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3255 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3259 if (recursive_stmt_fcn (e->value.op.op1, sym)
3260 || recursive_stmt_fcn (e->value.op.op2, sym))
3268 /* Component references do not need to be checked. */
3271 for (ref = e->ref; ref; ref = ref->next)
3276 for (i = 0; i < ref->u.ar.dimen; i++)
3278 if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
3279 || recursive_stmt_fcn (ref->u.ar.end[i], sym)
3280 || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
3286 if (recursive_stmt_fcn (ref->u.ss.start, sym)
3287 || recursive_stmt_fcn (ref->u.ss.end, sym))
3301 /* Match a statement function declaration. It is so easy to match
3302 non-statement function statements with a MATCH_ERROR as opposed to
3303 MATCH_NO that we suppress error message in most cases. */
3306 gfc_match_st_function (void)
3308 gfc_error_buf old_error;
3313 m = gfc_match_symbol (&sym, 0);
3317 gfc_push_error (&old_error);
3319 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
3320 sym->name, NULL) == FAILURE)
3323 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
3326 m = gfc_match (" = %e%t", &expr);
3330 gfc_free_error (&old_error);
3331 if (m == MATCH_ERROR)
3334 if (recursive_stmt_fcn (expr, sym))
3336 gfc_error ("Statement function at %L is recursive", &expr->where);
3345 gfc_pop_error (&old_error);
3350 /***************** SELECT CASE subroutines ******************/
3352 /* Free a single case structure. */
3355 free_case (gfc_case *p)
3357 if (p->low == p->high)
3359 gfc_free_expr (p->low);
3360 gfc_free_expr (p->high);
3365 /* Free a list of case structures. */
3368 gfc_free_case_list (gfc_case *p)
3380 /* Match a single case selector. */
3383 match_case_selector (gfc_case **cp)
3388 c = gfc_get_case ();
3389 c->where = gfc_current_locus;
3391 if (gfc_match_char (':') == MATCH_YES)
3393 m = gfc_match_init_expr (&c->high);
3396 if (m == MATCH_ERROR)
3401 m = gfc_match_init_expr (&c->low);
3402 if (m == MATCH_ERROR)
3407 /* If we're not looking at a ':' now, make a range out of a single
3408 target. Else get the upper bound for the case range. */
3409 if (gfc_match_char (':') != MATCH_YES)
3413 m = gfc_match_init_expr (&c->high);
3414 if (m == MATCH_ERROR)
3416 /* MATCH_NO is fine. It's OK if nothing is there! */
3424 gfc_error ("Expected initialization expression in CASE at %C");
3432 /* Match the end of a case statement. */
3435 match_case_eos (void)
3437 char name[GFC_MAX_SYMBOL_LEN + 1];
3440 if (gfc_match_eos () == MATCH_YES)
3443 /* If the case construct doesn't have a case-construct-name, we
3444 should have matched the EOS. */
3445 if (!gfc_current_block ())
3447 gfc_error ("Expected the name of the SELECT CASE construct at %C");
3451 gfc_gobble_whitespace ();
3453 m = gfc_match_name (name);
3457 if (strcmp (name, gfc_current_block ()->name) != 0)
3459 gfc_error ("Expected case name of '%s' at %C",
3460 gfc_current_block ()->name);
3464 return gfc_match_eos ();
3468 /* Match a SELECT statement. */
3471 gfc_match_select (void)
3476 m = gfc_match_label ();
3477 if (m == MATCH_ERROR)
3480 m = gfc_match (" select case ( %e )%t", &expr);
3484 new_st.op = EXEC_SELECT;
3491 /* Match a CASE statement. */
3494 gfc_match_case (void)
3496 gfc_case *c, *head, *tail;
3501 if (gfc_current_state () != COMP_SELECT)
3503 gfc_error ("Unexpected CASE statement at %C");
3507 if (gfc_match ("% default") == MATCH_YES)
3509 m = match_case_eos ();
3512 if (m == MATCH_ERROR)
3515 new_st.op = EXEC_SELECT;
3516 c = gfc_get_case ();
3517 c->where = gfc_current_locus;
3518 new_st.ext.case_list = c;
3522 if (gfc_match_char ('(') != MATCH_YES)
3527 if (match_case_selector (&c) == MATCH_ERROR)
3537 if (gfc_match_char (')') == MATCH_YES)
3539 if (gfc_match_char (',') != MATCH_YES)
3543 m = match_case_eos ();
3546 if (m == MATCH_ERROR)
3549 new_st.op = EXEC_SELECT;
3550 new_st.ext.case_list = head;
3555 gfc_error ("Syntax error in CASE-specification at %C");
3558 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
3562 /********************* WHERE subroutines ********************/
3564 /* Match the rest of a simple WHERE statement that follows an IF statement.
3568 match_simple_where (void)
3574 m = gfc_match (" ( %e )", &expr);
3578 m = gfc_match_assignment ();
3581 if (m == MATCH_ERROR)
3584 if (gfc_match_eos () != MATCH_YES)
3587 c = gfc_get_code ();
3591 c->next = gfc_get_code ();
3594 gfc_clear_new_st ();
3596 new_st.op = EXEC_WHERE;
3602 gfc_syntax_error (ST_WHERE);
3605 gfc_free_expr (expr);
3610 /* Match a WHERE statement. */
3613 gfc_match_where (gfc_statement *st)
3619 m0 = gfc_match_label ();
3620 if (m0 == MATCH_ERROR)
3623 m = gfc_match (" where ( %e )", &expr);
3627 if (gfc_match_eos () == MATCH_YES)
3629 *st = ST_WHERE_BLOCK;
3630 new_st.op = EXEC_WHERE;
3635 m = gfc_match_assignment ();
3637 gfc_syntax_error (ST_WHERE);
3641 gfc_free_expr (expr);
3645 /* We've got a simple WHERE statement. */
3647 c = gfc_get_code ();
3651 c->next = gfc_get_code ();
3654 gfc_clear_new_st ();
3656 new_st.op = EXEC_WHERE;
3663 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
3664 new_st if successful. */
3667 gfc_match_elsewhere (void)
3669 char name[GFC_MAX_SYMBOL_LEN + 1];
3673 if (gfc_current_state () != COMP_WHERE)
3675 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3681 if (gfc_match_char ('(') == MATCH_YES)
3683 m = gfc_match_expr (&expr);
3686 if (m == MATCH_ERROR)
3689 if (gfc_match_char (')') != MATCH_YES)
3693 if (gfc_match_eos () != MATCH_YES)
3695 /* Only makes sense if we have a where-construct-name. */
3696 if (!gfc_current_block ())
3701 /* Better be a name at this point. */
3702 m = gfc_match_name (name);
3705 if (m == MATCH_ERROR)
3708 if (gfc_match_eos () != MATCH_YES)
3711 if (strcmp (name, gfc_current_block ()->name) != 0)
3713 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3714 name, gfc_current_block ()->name);
3719 new_st.op = EXEC_WHERE;
3724 gfc_syntax_error (ST_ELSEWHERE);
3727 gfc_free_expr (expr);
3732 /******************** FORALL subroutines ********************/
3734 /* Free a list of FORALL iterators. */
3737 gfc_free_forall_iterator (gfc_forall_iterator *iter)
3739 gfc_forall_iterator *next;
3744 gfc_free_expr (iter->var);
3745 gfc_free_expr (iter->start);
3746 gfc_free_expr (iter->end);
3747 gfc_free_expr (iter->stride);
3754 /* Match an iterator as part of a FORALL statement. The format is:
3756 <var> = <start>:<end>[:<stride>]
3758 On MATCH_NO, the caller tests for the possibility that there is a
3759 scalar mask expression. */
3762 match_forall_iterator (gfc_forall_iterator **result)
3764 gfc_forall_iterator *iter;
3768 where = gfc_current_locus;
3769 iter = gfc_getmem (sizeof (gfc_forall_iterator));
3771 m = gfc_match_expr (&iter->var);
3775 if (gfc_match_char ('=') != MATCH_YES
3776 || iter->var->expr_type != EXPR_VARIABLE)
3782 m = gfc_match_expr (&iter->start);
3786 if (gfc_match_char (':') != MATCH_YES)
3789 m = gfc_match_expr (&iter->end);
3792 if (m == MATCH_ERROR)
3795 if (gfc_match_char (':') == MATCH_NO)
3796 iter->stride = gfc_int_expr (1);
3799 m = gfc_match_expr (&iter->stride);
3802 if (m == MATCH_ERROR)
3806 /* Mark the iteration variable's symbol as used as a FORALL index. */
3807 iter->var->symtree->n.sym->forall_index = true;
3813 gfc_error ("Syntax error in FORALL iterator at %C");
3818 gfc_current_locus = where;
3819 gfc_free_forall_iterator (iter);
3824 /* Match the header of a FORALL statement. */
3827 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
3829 gfc_forall_iterator *head, *tail, *new;
3833 gfc_gobble_whitespace ();
3838 if (gfc_match_char ('(') != MATCH_YES)
3841 m = match_forall_iterator (&new);
3842 if (m == MATCH_ERROR)
3851 if (gfc_match_char (',') != MATCH_YES)
3854 m = match_forall_iterator (&new);
3855 if (m == MATCH_ERROR)
3865 /* Have to have a mask expression. */
3867 m = gfc_match_expr (&msk);
3870 if (m == MATCH_ERROR)
3876 if (gfc_match_char (')') == MATCH_NO)
3884 gfc_syntax_error (ST_FORALL);
3887 gfc_free_expr (msk);
3888 gfc_free_forall_iterator (head);
3893 /* Match the rest of a simple FORALL statement that follows an
3897 match_simple_forall (void)
3899 gfc_forall_iterator *head;
3908 m = match_forall_header (&head, &mask);
3915 m = gfc_match_assignment ();
3917 if (m == MATCH_ERROR)
3921 m = gfc_match_pointer_assignment ();
3922 if (m == MATCH_ERROR)
3928 c = gfc_get_code ();
3930 c->loc = gfc_current_locus;
3932 if (gfc_match_eos () != MATCH_YES)
3935 gfc_clear_new_st ();
3936 new_st.op = EXEC_FORALL;
3938 new_st.ext.forall_iterator = head;
3939 new_st.block = gfc_get_code ();
3941 new_st.block->op = EXEC_FORALL;
3942 new_st.block->next = c;
3947 gfc_syntax_error (ST_FORALL);
3950 gfc_free_forall_iterator (head);
3951 gfc_free_expr (mask);
3957 /* Match a FORALL statement. */
3960 gfc_match_forall (gfc_statement *st)
3962 gfc_forall_iterator *head;
3971 m0 = gfc_match_label ();
3972 if (m0 == MATCH_ERROR)
3975 m = gfc_match (" forall");
3979 m = match_forall_header (&head, &mask);
3980 if (m == MATCH_ERROR)
3985 if (gfc_match_eos () == MATCH_YES)
3987 *st = ST_FORALL_BLOCK;
3988 new_st.op = EXEC_FORALL;
3990 new_st.ext.forall_iterator = head;
3994 m = gfc_match_assignment ();
3995 if (m == MATCH_ERROR)
3999 m = gfc_match_pointer_assignment ();
4000 if (m == MATCH_ERROR)
4006 c = gfc_get_code ();
4008 c->loc = gfc_current_locus;
4010 gfc_clear_new_st ();
4011 new_st.op = EXEC_FORALL;
4013 new_st.ext.forall_iterator = head;
4014 new_st.block = gfc_get_code ();
4015 new_st.block->op = EXEC_FORALL;
4016 new_st.block->next = c;
4022 gfc_syntax_error (ST_FORALL);
4025 gfc_free_forall_iterator (head);
4026 gfc_free_expr (mask);
4027 gfc_free_statements (c);