2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
31 /* Current statement label. Zero means no statement label. Because
32 new_st can get wiped during statement matching, we have to keep it
35 gfc_st_label *gfc_statement_label;
37 static locus label_locus;
38 static jmp_buf eof_buf;
40 gfc_state_data *gfc_state_stack;
42 /* TODO: Re-order functions to kill these forward decls. */
43 static void check_statement_label (gfc_statement);
44 static void undo_new_statement (void);
45 static void reject_statement (void);
47 /* A sort of half-matching function. We try to match the word on the
48 input with the passed string. If this succeeds, we call the
49 keyword-dependent matching function that will match the rest of the
50 statement. For single keywords, the matching subroutine is
54 match_word (const char *str, match (*subr) (void), locus * old_locus)
69 gfc_current_locus = *old_locus;
77 /* Figure out what the next statement is, (mostly) regardless of
78 proper ordering. The do...while(0) is there to prevent if/else
81 #define match(keyword, subr, st) \
83 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
86 undo_new_statement (); \
90 decode_statement (void)
101 gfc_clear_error (); /* Clear any pending errors. */
102 gfc_clear_warning (); /* Clear any pending warnings. */
104 if (gfc_match_eos () == MATCH_YES)
107 old_locus = gfc_current_locus;
109 /* Try matching a data declaration or function declaration. The
110 input "REALFUNCTIONA(N)" can mean several things in different
111 contexts, so it (and its relatives) get special treatment. */
113 if (gfc_current_state () == COMP_NONE
114 || gfc_current_state () == COMP_INTERFACE
115 || gfc_current_state () == COMP_CONTAINS)
117 m = gfc_match_function_decl ();
120 else if (m == MATCH_ERROR)
124 gfc_current_locus = old_locus;
127 /* Match statements whose error messages are meant to be overwritten
128 by something better. */
130 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
131 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
132 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
134 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
136 /* Try to match a subroutine statement, which has the same optional
137 prefixes that functions can have. */
139 if (gfc_match_subroutine () == MATCH_YES)
140 return ST_SUBROUTINE;
142 gfc_current_locus = old_locus;
144 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
145 might begin with a block label. The match functions for these
146 statements are unusual in that their keyword is not seen before
147 the matcher is called. */
149 if (gfc_match_if (&st) == MATCH_YES)
152 gfc_current_locus = old_locus;
154 if (gfc_match_where (&st) == MATCH_YES)
157 gfc_current_locus = old_locus;
159 if (gfc_match_forall (&st) == MATCH_YES)
162 gfc_current_locus = old_locus;
164 match (NULL, gfc_match_do, ST_DO);
165 match (NULL, gfc_match_select, ST_SELECT_CASE);
167 /* General statement matching: Instead of testing every possible
168 statement, we eliminate most possibilities by peeking at the
171 c = gfc_peek_char ();
176 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
177 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
178 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
182 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
183 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
187 match ("call", gfc_match_call, ST_CALL);
188 match ("close", gfc_match_close, ST_CLOSE);
189 match ("continue", gfc_match_continue, ST_CONTINUE);
190 match ("cycle", gfc_match_cycle, ST_CYCLE);
191 match ("case", gfc_match_case, ST_CASE);
192 match ("common", gfc_match_common, ST_COMMON);
193 match ("contains", gfc_match_eos, ST_CONTAINS);
197 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
198 match ("data", gfc_match_data, ST_DATA);
199 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
203 match ("end file", gfc_match_endfile, ST_END_FILE);
204 match ("exit", gfc_match_exit, ST_EXIT);
205 match ("else", gfc_match_else, ST_ELSE);
206 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
207 match ("else if", gfc_match_elseif, ST_ELSEIF);
209 if (gfc_match_end (&st) == MATCH_YES)
212 match ("entry% ", gfc_match_entry, ST_ENTRY);
213 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
214 match ("external", gfc_match_external, ST_ATTR_DECL);
218 match ("flush", gfc_match_flush, ST_FLUSH);
219 match ("format", gfc_match_format, ST_FORMAT);
223 match ("go to", gfc_match_goto, ST_GOTO);
227 match ("inquire", gfc_match_inquire, ST_INQUIRE);
228 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
229 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
230 match ("interface", gfc_match_interface, ST_INTERFACE);
231 match ("intent", gfc_match_intent, ST_ATTR_DECL);
232 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
236 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
237 match ("module", gfc_match_module, ST_MODULE);
241 match ("nullify", gfc_match_nullify, ST_NULLIFY);
242 match ("namelist", gfc_match_namelist, ST_NAMELIST);
246 match ("open", gfc_match_open, ST_OPEN);
247 match ("optional", gfc_match_optional, ST_ATTR_DECL);
251 match ("print", gfc_match_print, ST_WRITE);
252 match ("parameter", gfc_match_parameter, ST_PARAMETER);
253 match ("pause", gfc_match_pause, ST_PAUSE);
254 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
255 if (gfc_match_private (&st) == MATCH_YES)
257 match ("program", gfc_match_program, ST_PROGRAM);
258 if (gfc_match_public (&st) == MATCH_YES)
263 match ("read", gfc_match_read, ST_READ);
264 match ("return", gfc_match_return, ST_RETURN);
265 match ("rewind", gfc_match_rewind, ST_REWIND);
269 match ("sequence", gfc_match_eos, ST_SEQUENCE);
270 match ("stop", gfc_match_stop, ST_STOP);
271 match ("save", gfc_match_save, ST_ATTR_DECL);
275 match ("target", gfc_match_target, ST_ATTR_DECL);
276 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
280 match ("use% ", gfc_match_use, ST_USE);
284 match ("write", gfc_match_write, ST_WRITE);
288 /* All else has failed, so give up. See if any of the matchers has
289 stored an error message of some sort. */
291 if (gfc_error_check () == 0)
292 gfc_error_now ("Unclassifiable statement at %C");
296 gfc_error_recovery ();
304 /* Get the next statement in free form source. */
312 gfc_gobble_whitespace ();
314 c = gfc_peek_char ();
318 /* Found a statement label? */
319 m = gfc_match_st_label (&gfc_statement_label, 0);
321 d = gfc_peek_char ();
322 if (m != MATCH_YES || !gfc_is_whitespace (d))
326 /* Skip the bad statement label. */
327 gfc_warning_now ("Ignoring bad statement label at %C");
328 c = gfc_next_char ();
334 label_locus = gfc_current_locus;
336 if (gfc_statement_label->value == 0)
338 gfc_warning_now ("Ignoring statement label of zero at %C");
339 gfc_free_st_label (gfc_statement_label);
340 gfc_statement_label = NULL;
343 gfc_gobble_whitespace ();
345 if (gfc_match_eos () == MATCH_YES)
348 ("Ignoring statement label in empty statement at %C");
349 gfc_free_st_label (gfc_statement_label);
350 gfc_statement_label = NULL;
356 return decode_statement ();
360 /* Get the next statement in fixed-form source. */
365 int label, digit_flag, i;
370 return decode_statement ();
372 /* Skip past the current label field, parsing a statement label if
373 one is there. This is a weird number parser, since the number is
374 contained within five columns and can have any kind of embedded
375 spaces. We also check for characters that make the rest of the
381 for (i = 0; i < 5; i++)
383 c = gfc_next_char_literal (0);
400 label = label * 10 + c - '0';
401 label_locus = gfc_current_locus;
405 /* Comments have already been skipped by the time we get
406 here so don't bother checking for them. */
409 gfc_buffer_error (0);
410 gfc_error ("Non-numeric character in statement label at %C");
418 gfc_warning_now ("Zero is not a valid statement label at %C");
421 /* We've found a valid statement label. */
422 gfc_statement_label = gfc_get_st_label (label);
426 /* Since this line starts a statement, it cannot be a continuation
427 of a previous statement. If we see something here besides a
428 space or zero, it must be a bad continuation line. */
430 c = gfc_next_char_literal (0);
434 if (c != ' ' && c!= '0')
436 gfc_buffer_error (0);
437 gfc_error ("Bad continuation line at %C");
441 /* Now that we've taken care of the statement label columns, we have
442 to make sure that the first nonblank character is not a '!'. If
443 it is, the rest of the line is a comment. */
447 loc = gfc_current_locus;
448 c = gfc_next_char_literal (0);
450 while (gfc_is_whitespace (c));
454 gfc_current_locus = loc;
456 if (gfc_match_eos () == MATCH_YES)
459 /* At this point, we've got a nonblank statement to parse. */
460 return decode_statement ();
464 gfc_warning ("Statement label in blank line will be " "ignored at %C");
470 /* Return the next non-ST_NONE statement to the caller. We also worry
471 about including files and the ends of include files at this stage. */
474 next_statement (void)
478 gfc_new_block = NULL;
482 gfc_statement_label = NULL;
483 gfc_buffer_error (1);
487 if (gfc_option.warn_line_truncation
488 && gfc_current_locus.lb->truncated)
489 gfc_warning_now ("Line truncated at %C");
494 gfc_skip_comments ();
503 (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
509 gfc_buffer_error (0);
512 check_statement_label (st);
518 /****************************** Parser ***********************************/
520 /* The parser subroutines are of type 'try' that fail if the file ends
523 /* Macros that expand to case-labels for various classes of
524 statements. Start with executable statements that directly do
527 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
528 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
529 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
530 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
531 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
532 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
533 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
534 case ST_LABEL_ASSIGNMENT: case ST_FLUSH
536 /* Statements that mark other executable statements. */
538 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
539 case ST_WHERE_BLOCK: case ST_SELECT_CASE
541 /* Declaration statements */
543 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
544 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
545 case ST_TYPE: case ST_INTERFACE
547 /* Block end statements. Errors associated with interchanging these
548 are detected in gfc_match_end(). */
550 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
551 case ST_END_PROGRAM: case ST_END_SUBROUTINE
554 /* Push a new state onto the stack. */
557 push_state (gfc_state_data * p, gfc_compile_state new_state, gfc_symbol * sym)
560 p->state = new_state;
561 p->previous = gfc_state_stack;
563 p->head = p->tail = NULL;
564 p->do_variable = NULL;
570 /* Pop the current state. */
576 gfc_state_stack = gfc_state_stack->previous;
580 /* Try to find the given state in the state stack. */
583 gfc_find_state (gfc_compile_state state)
587 for (p = gfc_state_stack; p; p = p->previous)
588 if (p->state == state)
591 return (p == NULL) ? FAILURE : SUCCESS;
595 /* Starts a new level in the statement list. */
598 new_level (gfc_code * q)
602 p = q->block = gfc_get_code ();
604 gfc_state_stack->head = gfc_state_stack->tail = p;
610 /* Add the current new_st code structure and adds it to the current
611 program unit. As a side-effect, it zeroes the new_st. */
621 p->loc = gfc_current_locus;
623 if (gfc_state_stack->head == NULL)
624 gfc_state_stack->head = p;
626 gfc_state_stack->tail->next = p;
628 while (p->next != NULL)
631 gfc_state_stack->tail = p;
639 /* Frees everything associated with the current statement. */
642 undo_new_statement (void)
644 gfc_free_statements (new_st.block);
645 gfc_free_statements (new_st.next);
646 gfc_free_statement (&new_st);
651 /* If the current statement has a statement label, make sure that it
652 is allowed to, or should have one. */
655 check_statement_label (gfc_statement st)
659 if (gfc_statement_label == NULL)
662 gfc_error ("FORMAT statement at %L does not have a statement label",
670 case ST_END_FUNCTION:
671 case ST_END_SUBROUTINE:
677 type = ST_LABEL_TARGET;
681 type = ST_LABEL_FORMAT;
684 /* Statement labels are not restricted from appearing on a
685 particular line. However, there are plenty of situations
686 where the resulting label can't be referenced. */
689 type = ST_LABEL_BAD_TARGET;
693 gfc_define_st_label (gfc_statement_label, type, &label_locus);
695 new_st.here = gfc_statement_label;
699 /* Figures out what the enclosing program unit is. This will be a
700 function, subroutine, program, block data or module. */
703 gfc_enclosing_unit (gfc_compile_state * result)
707 for (p = gfc_state_stack; p; p = p->previous)
708 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
709 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
710 || p->state == COMP_PROGRAM)
719 *result = COMP_PROGRAM;
724 /* Translate a statement enum to a string. */
727 gfc_ascii_statement (gfc_statement st)
733 case ST_ARITHMETIC_IF:
734 p = _("arithmetic IF");
740 p = _("attribute declaration");
770 p = _("data declaration");
778 case ST_DERIVED_DECL:
779 p = _("derived type declaration");
793 case ST_END_BLOCK_DATA:
794 p = "END BLOCK DATA";
805 case ST_END_FUNCTION:
811 case ST_END_INTERFACE:
823 case ST_END_SUBROUTINE:
824 p = "END SUBROUTINE";
844 case ST_FORALL_BLOCK: /* Fall through */
863 case ST_IMPLICIT_NONE:
866 case ST_IMPLIED_ENDDO:
867 p = _("implied END DO");
891 p = "MODULE PROCEDURE";
926 case ST_WHERE_BLOCK: /* Fall through */
936 case ST_POINTER_ASSIGNMENT:
937 p = _("pointer assignment");
948 case ST_STATEMENT_FUNCTION:
949 p = "STATEMENT FUNCTION";
951 case ST_LABEL_ASSIGNMENT:
952 p = "LABEL ASSIGNMENT";
955 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
962 /* Return the name of a compile state. */
965 gfc_state_name (gfc_compile_state state)
977 case COMP_SUBROUTINE:
978 p = _("a SUBROUTINE");
983 case COMP_BLOCK_DATA:
984 p = _("a BLOCK DATA");
987 p = _("an INTERFACE");
990 p = _("a DERIVED TYPE block");
993 p = _("an IF-THEN block");
999 p = _("a SELECT block");
1002 p = _("a FORALL block");
1005 p = _("a WHERE block");
1008 p = _("a contained subprogram");
1012 gfc_internal_error ("gfc_state_name(): Bad state");
1019 /* Do whatever is necessary to accept the last statement. */
1022 accept_statement (gfc_statement st)
1031 case ST_IMPLICIT_NONE:
1032 gfc_set_implicit_none ();
1041 gfc_current_ns->proc_name = gfc_new_block;
1044 /* If the statement is the end of a block, lay down a special code
1045 that allows a branch to the end of the block from within the
1050 if (gfc_statement_label != NULL)
1052 new_st.op = EXEC_NOP;
1058 /* The end-of-program unit statements do not get the special
1059 marker and require a statement of some sort if they are a
1062 case ST_END_PROGRAM:
1063 case ST_END_FUNCTION:
1064 case ST_END_SUBROUTINE:
1065 if (gfc_statement_label != NULL)
1067 new_st.op = EXEC_RETURN;
1083 gfc_commit_symbols ();
1084 gfc_warning_check ();
1085 gfc_clear_new_st ();
1089 /* Undo anything tentative that has been built for the current
1093 reject_statement (void)
1096 gfc_undo_symbols ();
1097 gfc_clear_warning ();
1098 undo_new_statement ();
1102 /* Generic complaint about an out of order statement. We also do
1103 whatever is necessary to clean up. */
1106 unexpected_statement (gfc_statement st)
1109 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1111 reject_statement ();
1115 /* Given the next statement seen by the matcher, make sure that it is
1116 in proper order with the last. This subroutine is initialized by
1117 calling it with an argument of ST_NONE. If there is a problem, we
1118 issue an error and return FAILURE. Otherwise we return SUCCESS.
1120 Individual parsers need to verify that the statements seen are
1121 valid before calling here, ie ENTRY statements are not allowed in
1122 INTERFACE blocks. The following diagram is taken from the standard:
1124 +---------------------------------------+
1125 | program subroutine function module |
1126 +---------------------------------------+
1128 |---------------------------------------+
1130 | +-----------+------------------+
1131 | | parameter | implicit |
1132 | +-----------+------------------+
1133 | format | | derived type |
1134 | entry | parameter | interface |
1135 | | data | specification |
1136 | | | statement func |
1137 | +-----------+------------------+
1138 | | data | executable |
1139 +--------+-----------+------------------+
1141 +---------------------------------------+
1142 | internal module/subprogram |
1143 +---------------------------------------+
1145 +---------------------------------------+
1152 { ORDER_START, ORDER_USE, ORDER_IMPLICIT_NONE, ORDER_IMPLICIT,
1153 ORDER_SPEC, ORDER_EXEC
1156 gfc_statement last_statement;
1162 verify_st_order (st_state * p, gfc_statement st)
1168 p->state = ORDER_START;
1172 if (p->state > ORDER_USE)
1174 p->state = ORDER_USE;
1177 case ST_IMPLICIT_NONE:
1178 if (p->state > ORDER_IMPLICIT_NONE)
1181 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1182 statement disqualifies a USE but not an IMPLICIT NONE.
1183 Duplicate IMPLICIT NONEs are caught when the implicit types
1186 p->state = ORDER_IMPLICIT_NONE;
1190 if (p->state > ORDER_IMPLICIT)
1192 p->state = ORDER_IMPLICIT;
1197 if (p->state < ORDER_IMPLICIT_NONE)
1198 p->state = ORDER_IMPLICIT_NONE;
1202 if (p->state >= ORDER_EXEC)
1204 if (p->state < ORDER_IMPLICIT)
1205 p->state = ORDER_IMPLICIT;
1209 if (p->state < ORDER_SPEC)
1210 p->state = ORDER_SPEC;
1215 case ST_DERIVED_DECL:
1217 if (p->state >= ORDER_EXEC)
1219 if (p->state < ORDER_SPEC)
1220 p->state = ORDER_SPEC;
1225 if (p->state < ORDER_EXEC)
1226 p->state = ORDER_EXEC;
1231 ("Unexpected %s statement in verify_st_order() at %C",
1232 gfc_ascii_statement (st));
1235 /* All is well, record the statement in case we need it next time. */
1236 p->where = gfc_current_locus;
1237 p->last_statement = st;
1241 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1242 gfc_ascii_statement (st),
1243 gfc_ascii_statement (p->last_statement), &p->where);
1249 /* Handle an unexpected end of file. This is a show-stopper... */
1251 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1254 unexpected_eof (void)
1258 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1260 /* Memory cleanup. Move to "second to last". */
1261 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1264 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1267 longjmp (eof_buf, 1);
1271 /* Parse a derived type. */
1274 parse_derived (void)
1276 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1283 accept_statement (ST_DERIVED_DECL);
1284 push_state (&s, COMP_DERIVED, gfc_new_block);
1286 gfc_new_block->component_access = ACCESS_PUBLIC;
1293 while (compiling_type)
1295 st = next_statement ();
1302 accept_statement (st);
1309 if (!seen_component)
1311 gfc_error ("Derived type definition at %C has no components");
1315 accept_statement (ST_END_TYPE);
1319 if (gfc_find_state (COMP_MODULE) == FAILURE)
1322 ("PRIVATE statement in TYPE at %C must be inside a MODULE");
1329 gfc_error ("PRIVATE statement at %C must precede "
1330 "structure components");
1337 gfc_error ("Duplicate PRIVATE statement at %C");
1341 s.sym->component_access = ACCESS_PRIVATE;
1342 accept_statement (ST_PRIVATE);
1349 gfc_error ("SEQUENCE statement at %C must precede "
1350 "structure components");
1355 if (gfc_current_block ()->attr.sequence)
1356 gfc_warning ("SEQUENCE attribute at %C already specified in "
1361 gfc_error ("Duplicate SEQUENCE statement at %C");
1366 gfc_add_sequence (&gfc_current_block ()->attr,
1367 gfc_current_block ()->name, NULL);
1371 unexpected_statement (st);
1376 /* Sanity checks on the structure. If the structure has the
1377 SEQUENCE attribute, then all component structures must also have
1379 if (error_flag == 0 && gfc_current_block ()->attr.sequence)
1380 for (c = gfc_current_block ()->components; c; c = c->next)
1382 if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
1385 ("Component %s of SEQUENCE type declared at %C does not "
1386 "have the SEQUENCE attribute", c->ts.derived->name);
1395 /* Parse an interface. We must be able to deal with the possibility
1396 of recursive interfaces. The parse_spec() subroutine is mutually
1397 recursive with parse_interface(). */
1399 static gfc_statement parse_spec (gfc_statement);
1402 parse_interface (void)
1404 gfc_compile_state new_state, current_state;
1405 gfc_symbol *prog_unit, *sym;
1406 gfc_interface_info save;
1407 gfc_state_data s1, s2;
1410 accept_statement (ST_INTERFACE);
1412 current_interface.ns = gfc_current_ns;
1413 save = current_interface;
1415 sym = (current_interface.type == INTERFACE_GENERIC
1416 || current_interface.type == INTERFACE_USER_OP) ? gfc_new_block : NULL;
1418 push_state (&s1, COMP_INTERFACE, sym);
1419 current_state = COMP_NONE;
1422 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1424 st = next_statement ();
1431 new_state = COMP_SUBROUTINE;
1432 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1433 gfc_new_block->formal, NULL);
1437 new_state = COMP_FUNCTION;
1438 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1439 gfc_new_block->formal, NULL);
1442 case ST_MODULE_PROC: /* The module procedure matcher makes
1443 sure the context is correct. */
1444 accept_statement (st);
1445 gfc_free_namespace (gfc_current_ns);
1448 case ST_END_INTERFACE:
1449 gfc_free_namespace (gfc_current_ns);
1450 gfc_current_ns = current_interface.ns;
1454 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1455 gfc_ascii_statement (st));
1456 reject_statement ();
1457 gfc_free_namespace (gfc_current_ns);
1462 /* Make sure that a generic interface has only subroutines or
1463 functions and that the generic name has the right attribute. */
1464 if (current_interface.type == INTERFACE_GENERIC)
1466 if (current_state == COMP_NONE)
1468 if (new_state == COMP_FUNCTION)
1469 gfc_add_function (&sym->attr, sym->name, NULL);
1470 else if (new_state == COMP_SUBROUTINE)
1471 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1473 current_state = new_state;
1477 if (new_state != current_state)
1479 if (new_state == COMP_SUBROUTINE)
1481 ("SUBROUTINE at %C does not belong in a generic function "
1484 if (new_state == COMP_FUNCTION)
1486 ("FUNCTION at %C does not belong in a generic subroutine "
1492 push_state (&s2, new_state, gfc_new_block);
1493 accept_statement (st);
1494 prog_unit = gfc_new_block;
1495 prog_unit->formal_ns = gfc_current_ns;
1498 /* Read data declaration statements. */
1499 st = parse_spec (ST_NONE);
1501 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1503 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1504 gfc_ascii_statement (st));
1505 reject_statement ();
1509 current_interface = save;
1510 gfc_add_interface (prog_unit);
1520 /* Parse a set of specification statements. Returns the statement
1521 that doesn't fit. */
1523 static gfc_statement
1524 parse_spec (gfc_statement st)
1528 verify_st_order (&ss, ST_NONE);
1530 st = next_statement ();
1540 case ST_DATA: /* Not allowed in interfaces */
1541 if (gfc_current_state () == COMP_INTERFACE)
1547 case ST_IMPLICIT_NONE:
1552 case ST_DERIVED_DECL:
1554 if (verify_st_order (&ss, st) == FAILURE)
1556 reject_statement ();
1557 st = next_statement ();
1567 case ST_DERIVED_DECL:
1573 if (gfc_current_state () != COMP_MODULE)
1575 gfc_error ("%s statement must appear in a MODULE",
1576 gfc_ascii_statement (st));
1580 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1582 gfc_error ("%s statement at %C follows another accessibility "
1583 "specification", gfc_ascii_statement (st));
1587 gfc_current_ns->default_access = (st == ST_PUBLIC)
1588 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1596 accept_statement (st);
1597 st = next_statement ();
1608 /* Parse a WHERE block, (not a simple WHERE statement). */
1611 parse_where_block (void)
1613 int seen_empty_else;
1618 accept_statement (ST_WHERE_BLOCK);
1619 top = gfc_state_stack->tail;
1621 push_state (&s, COMP_WHERE, gfc_new_block);
1623 d = add_statement ();
1624 d->expr = top->expr;
1630 seen_empty_else = 0;
1634 st = next_statement ();
1640 case ST_WHERE_BLOCK:
1641 parse_where_block ();
1646 accept_statement (st);
1650 if (seen_empty_else)
1653 ("ELSEWHERE statement at %C follows previous unmasked "
1658 if (new_st.expr == NULL)
1659 seen_empty_else = 1;
1661 d = new_level (gfc_state_stack->head);
1663 d->expr = new_st.expr;
1665 accept_statement (st);
1670 accept_statement (st);
1674 gfc_error ("Unexpected %s statement in WHERE block at %C",
1675 gfc_ascii_statement (st));
1676 reject_statement ();
1681 while (st != ST_END_WHERE);
1687 /* Parse a FORALL block (not a simple FORALL statement). */
1690 parse_forall_block (void)
1696 accept_statement (ST_FORALL_BLOCK);
1697 top = gfc_state_stack->tail;
1699 push_state (&s, COMP_FORALL, gfc_new_block);
1701 d = add_statement ();
1702 d->op = EXEC_FORALL;
1707 st = next_statement ();
1712 case ST_POINTER_ASSIGNMENT:
1715 accept_statement (st);
1718 case ST_WHERE_BLOCK:
1719 parse_where_block ();
1722 case ST_FORALL_BLOCK:
1723 parse_forall_block ();
1727 accept_statement (st);
1734 gfc_error ("Unexpected %s statement in FORALL block at %C",
1735 gfc_ascii_statement (st));
1737 reject_statement ();
1741 while (st != ST_END_FORALL);
1747 static gfc_statement parse_executable (gfc_statement);
1749 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
1752 parse_if_block (void)
1761 accept_statement (ST_IF_BLOCK);
1763 top = gfc_state_stack->tail;
1764 push_state (&s, COMP_IF, gfc_new_block);
1766 new_st.op = EXEC_IF;
1767 d = add_statement ();
1769 d->expr = top->expr;
1775 st = parse_executable (ST_NONE);
1786 ("ELSE IF statement at %C cannot follow ELSE statement at %L",
1789 reject_statement ();
1793 d = new_level (gfc_state_stack->head);
1795 d->expr = new_st.expr;
1797 accept_statement (st);
1804 gfc_error ("Duplicate ELSE statements at %L and %C",
1806 reject_statement ();
1811 else_locus = gfc_current_locus;
1813 d = new_level (gfc_state_stack->head);
1816 accept_statement (st);
1824 unexpected_statement (st);
1828 while (st != ST_ENDIF);
1831 accept_statement (st);
1835 /* Parse a SELECT block. */
1838 parse_select_block (void)
1844 accept_statement (ST_SELECT_CASE);
1846 cp = gfc_state_stack->tail;
1847 push_state (&s, COMP_SELECT, gfc_new_block);
1849 /* Make sure that the next statement is a CASE or END SELECT. */
1852 st = next_statement ();
1855 if (st == ST_END_SELECT)
1857 /* Empty SELECT CASE is OK. */
1858 accept_statement (st);
1866 ("Expected a CASE or END SELECT statement following SELECT CASE "
1869 reject_statement ();
1872 /* At this point, we're got a nonempty select block. */
1873 cp = new_level (cp);
1876 accept_statement (st);
1880 st = parse_executable (ST_NONE);
1887 cp = new_level (gfc_state_stack->head);
1889 gfc_clear_new_st ();
1891 accept_statement (st);
1897 /* Can't have an executable statement because of
1898 parse_executable(). */
1900 unexpected_statement (st);
1904 while (st != ST_END_SELECT);
1907 accept_statement (st);
1911 /* Given a symbol, make sure it is not an iteration variable for a DO
1912 statement. This subroutine is called when the symbol is seen in a
1913 context that causes it to become redefined. If the symbol is an
1914 iterator, we generate an error message and return nonzero. */
1917 gfc_check_do_variable (gfc_symtree *st)
1921 for (s=gfc_state_stack; s; s = s->previous)
1922 if (s->do_variable == st)
1924 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
1925 "loop beginning at %L", st->name, &s->head->loc);
1933 /* Checks to see if the current statement label closes an enddo.
1934 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
1935 an error) if it incorrectly closes an ENDDO. */
1938 check_do_closure (void)
1942 if (gfc_statement_label == NULL)
1945 for (p = gfc_state_stack; p; p = p->previous)
1946 if (p->state == COMP_DO)
1950 return 0; /* No loops to close */
1952 if (p->ext.end_do_label == gfc_statement_label)
1955 if (p == gfc_state_stack)
1959 ("End of nonblock DO statement at %C is within another block");
1963 /* At this point, the label doesn't terminate the innermost loop.
1964 Make sure it doesn't terminate another one. */
1965 for (; p; p = p->previous)
1966 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
1968 gfc_error ("End of nonblock DO statement at %C is interwoven "
1969 "with another DO loop");
1977 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
1978 handled inside of parse_executable(), because they aren't really
1982 parse_do_block (void)
1989 s.ext.end_do_label = new_st.label;
1991 if (new_st.ext.iterator != NULL)
1992 stree = new_st.ext.iterator->var->symtree;
1996 accept_statement (ST_DO);
1998 top = gfc_state_stack->tail;
1999 push_state (&s, COMP_DO, gfc_new_block);
2001 s.do_variable = stree;
2003 top->block = new_level (top);
2004 top->block->op = EXEC_DO;
2007 st = parse_executable (ST_NONE);
2015 if (s.ext.end_do_label != NULL
2016 && s.ext.end_do_label != gfc_statement_label)
2018 ("Statement label in ENDDO at %C doesn't match DO label");
2020 if (gfc_statement_label != NULL)
2022 new_st.op = EXEC_NOP;
2027 case ST_IMPLIED_ENDDO:
2031 unexpected_statement (st);
2036 accept_statement (st);
2040 /* Accept a series of executable statements. We return the first
2041 statement that doesn't fit to the caller. Any block statements are
2042 passed on to the correct handler, which usually passes the buck
2045 static gfc_statement
2046 parse_executable (gfc_statement st)
2051 st = next_statement ();
2053 for (;; st = next_statement ())
2056 close_flag = check_do_closure ();
2061 case ST_END_PROGRAM:
2064 case ST_END_FUNCTION:
2068 case ST_END_SUBROUTINE:
2073 case ST_SELECT_CASE:
2075 ("%s statement at %C cannot terminate a non-block DO loop",
2076 gfc_ascii_statement (st));
2092 accept_statement (st);
2093 if (close_flag == 1)
2094 return ST_IMPLIED_ENDDO;
2101 case ST_SELECT_CASE:
2102 parse_select_block ();
2107 if (check_do_closure () == 1)
2108 return ST_IMPLIED_ENDDO;
2111 case ST_WHERE_BLOCK:
2112 parse_where_block ();
2115 case ST_FORALL_BLOCK:
2116 parse_forall_block ();
2130 /* Parse a series of contained program units. */
2132 static void parse_progunit (gfc_statement);
2135 /* Fix the symbols for sibling functions. These are incorrectly added to
2136 the child namespace as the parser didn't know about this procedure. */
2139 gfc_fixup_sibling_symbols (gfc_symbol * sym, gfc_namespace * siblings)
2143 gfc_symbol *old_sym;
2145 sym->attr.referenced = 1;
2146 for (ns = siblings; ns; ns = ns->sibling)
2148 gfc_find_sym_tree (sym->name, ns, 0, &st);
2152 old_sym = st->n.sym;
2153 if ((old_sym->attr.flavor == FL_PROCEDURE
2154 || old_sym->ts.type == BT_UNKNOWN)
2155 && old_sym->ns == ns
2156 && ! old_sym->attr.contained)
2158 /* Replace it with the symbol from the parent namespace. */
2162 /* Free the old (local) symbol. */
2164 if (old_sym->refs == 0)
2165 gfc_free_symbol (old_sym);
2168 /* Do the same for any contained procedures. */
2169 gfc_fixup_sibling_symbols (sym, ns->contained);
2174 parse_contained (int module)
2176 gfc_namespace *ns, *parent_ns;
2177 gfc_state_data s1, s2;
2182 push_state (&s1, COMP_CONTAINS, NULL);
2183 parent_ns = gfc_current_ns;
2187 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2189 gfc_current_ns->sibling = parent_ns->contained;
2190 parent_ns->contained = gfc_current_ns;
2192 st = next_statement ();
2201 accept_statement (st);
2204 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2207 /* For internal procedures, create/update the symbol in the
2208 parent namespace. */
2212 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2214 ("Contained procedure '%s' at %C is already ambiguous",
2215 gfc_new_block->name);
2218 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2219 &gfc_new_block->declared_at) ==
2222 if (st == ST_FUNCTION)
2223 gfc_add_function (&sym->attr, sym->name,
2224 &gfc_new_block->declared_at);
2226 gfc_add_subroutine (&sym->attr, sym->name,
2227 &gfc_new_block->declared_at);
2231 gfc_commit_symbols ();
2234 sym = gfc_new_block;
2236 /* Mark this as a contained function, so it isn't replaced
2237 by other module functions. */
2238 sym->attr.contained = 1;
2239 sym->attr.referenced = 1;
2241 parse_progunit (ST_NONE);
2243 /* Fix up any sibling functions that refer to this one. */
2244 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2245 /* Or refer to any of its alternate entry points. */
2246 for (el = gfc_current_ns->entries; el; el = el->next)
2247 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2249 gfc_current_ns->code = s2.head;
2250 gfc_current_ns = parent_ns;
2255 /* These statements are associated with the end of the host
2257 case ST_END_FUNCTION:
2259 case ST_END_PROGRAM:
2260 case ST_END_SUBROUTINE:
2261 accept_statement (st);
2265 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2266 gfc_ascii_statement (st));
2267 reject_statement ();
2271 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2272 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2274 /* The first namespace in the list is guaranteed to not have
2275 anything (worthwhile) in it. */
2277 gfc_current_ns = parent_ns;
2279 ns = gfc_current_ns->contained;
2280 gfc_current_ns->contained = ns->sibling;
2281 gfc_free_namespace (ns);
2287 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2290 parse_progunit (gfc_statement st)
2295 st = parse_spec (st);
2305 accept_statement (st);
2315 st = parse_executable (st);
2326 accept_statement (st);
2333 unexpected_statement (st);
2334 reject_statement ();
2335 st = next_statement ();
2341 for (p = gfc_state_stack; p; p = p->previous)
2342 if (p->state == COMP_CONTAINS)
2345 if (gfc_find_state (COMP_MODULE) == SUCCESS)
2350 gfc_error ("CONTAINS statement at %C is already in a contained "
2352 st = next_statement ();
2356 parse_contained (0);
2359 gfc_current_ns->code = gfc_state_stack->head;
2363 /* Come here to complain about a global symbol already in use as
2367 global_used (gfc_gsymbol *sym, locus *where)
2372 where = &gfc_current_locus;
2382 case GSYM_SUBROUTINE:
2383 name = "SUBROUTINE";
2388 case GSYM_BLOCK_DATA:
2389 name = "BLOCK DATA";
2395 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
2399 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
2400 gfc_new_block->name, where, name, &sym->where);
2404 /* Parse a block data program unit. */
2407 parse_block_data (void)
2410 static locus blank_locus;
2411 static int blank_block=0;
2414 gfc_current_ns->proc_name = gfc_new_block;
2415 gfc_current_ns->is_block_data = 1;
2417 if (gfc_new_block == NULL)
2420 gfc_error ("Blank BLOCK DATA at %C conflicts with "
2421 "prior BLOCK DATA at %L", &blank_locus);
2425 blank_locus = gfc_current_locus;
2430 s = gfc_get_gsymbol (gfc_new_block->name);
2431 if (s->type != GSYM_UNKNOWN)
2432 global_used(s, NULL);
2435 s->type = GSYM_BLOCK_DATA;
2436 s->where = gfc_current_locus;
2440 st = parse_spec (ST_NONE);
2442 while (st != ST_END_BLOCK_DATA)
2444 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
2445 gfc_ascii_statement (st));
2446 reject_statement ();
2447 st = next_statement ();
2452 /* Parse a module subprogram. */
2460 s = gfc_get_gsymbol (gfc_new_block->name);
2461 if (s->type != GSYM_UNKNOWN)
2462 global_used(s, NULL);
2465 s->type = GSYM_MODULE;
2466 s->where = gfc_current_locus;
2469 st = parse_spec (ST_NONE);
2478 parse_contained (1);
2482 accept_statement (st);
2486 gfc_error ("Unexpected %s statement in MODULE at %C",
2487 gfc_ascii_statement (st));
2489 reject_statement ();
2490 st = next_statement ();
2496 /* Add a procedure name to the global symbol table. */
2499 add_global_procedure (int sub)
2503 s = gfc_get_gsymbol(gfc_new_block->name);
2505 if (s->type != GSYM_UNKNOWN)
2506 global_used(s, NULL);
2509 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2510 s->where = gfc_current_locus;
2515 /* Add a program to the global symbol table. */
2518 add_global_program (void)
2522 if (gfc_new_block == NULL)
2524 s = gfc_get_gsymbol (gfc_new_block->name);
2526 if (s->type != GSYM_UNKNOWN)
2527 global_used(s, NULL);
2530 s->type = GSYM_PROGRAM;
2531 s->where = gfc_current_locus;
2536 /* Top level parser. */
2539 gfc_parse_file (void)
2541 int seen_program, errors_before, errors;
2542 gfc_state_data top, s;
2546 top.state = COMP_NONE;
2548 top.previous = NULL;
2549 top.head = top.tail = NULL;
2550 top.do_variable = NULL;
2552 gfc_state_stack = ⊤
2554 gfc_clear_new_st ();
2556 gfc_statement_label = NULL;
2558 if (setjmp (eof_buf))
2559 return FAILURE; /* Come here on unexpected EOF */
2565 st = next_statement ();
2574 goto duplicate_main;
2576 prog_locus = gfc_current_locus;
2578 push_state (&s, COMP_PROGRAM, gfc_new_block);
2579 accept_statement (st);
2580 add_global_program ();
2581 parse_progunit (ST_NONE);
2585 add_global_procedure (1);
2586 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
2587 accept_statement (st);
2588 parse_progunit (ST_NONE);
2592 add_global_procedure (0);
2593 push_state (&s, COMP_FUNCTION, gfc_new_block);
2594 accept_statement (st);
2595 parse_progunit (ST_NONE);
2599 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
2600 accept_statement (st);
2601 parse_block_data ();
2605 push_state (&s, COMP_MODULE, gfc_new_block);
2606 accept_statement (st);
2608 gfc_get_errors (NULL, &errors_before);
2612 /* Anything else starts a nameless main program block. */
2615 goto duplicate_main;
2617 prog_locus = gfc_current_locus;
2619 push_state (&s, COMP_PROGRAM, gfc_new_block);
2620 parse_progunit (st);
2624 gfc_current_ns->code = s.head;
2626 gfc_resolve (gfc_current_ns);
2628 /* Dump the parse tree if requested. */
2629 if (gfc_option.verbose)
2630 gfc_show_namespace (gfc_current_ns);
2632 gfc_get_errors (NULL, &errors);
2633 if (s.state == COMP_MODULE)
2635 gfc_dump_module (s.sym->name, errors_before == errors);
2636 if (errors == 0 && ! gfc_option.flag_no_backend)
2637 gfc_generate_module_code (gfc_current_ns);
2641 if (errors == 0 && ! gfc_option.flag_no_backend)
2642 gfc_generate_code (gfc_current_ns);
2653 /* If we see a duplicate main program, shut down. If the second
2654 instance is an implied main program, ie data decls or executable
2655 statements, we're in for lots of errors. */
2656 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
2657 reject_statement ();