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);
135 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
137 /* Try to match a subroutine statement, which has the same optional
138 prefixes that functions can have. */
140 if (gfc_match_subroutine () == MATCH_YES)
141 return ST_SUBROUTINE;
143 gfc_current_locus = old_locus;
145 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
146 might begin with a block label. The match functions for these
147 statements are unusual in that their keyword is not seen before
148 the matcher is called. */
150 if (gfc_match_if (&st) == MATCH_YES)
153 gfc_current_locus = old_locus;
155 if (gfc_match_where (&st) == MATCH_YES)
158 gfc_current_locus = old_locus;
160 if (gfc_match_forall (&st) == MATCH_YES)
163 gfc_current_locus = old_locus;
165 match (NULL, gfc_match_do, ST_DO);
166 match (NULL, gfc_match_select, ST_SELECT_CASE);
168 /* General statement matching: Instead of testing every possible
169 statement, we eliminate most possibilities by peeking at the
172 c = gfc_peek_char ();
177 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
178 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
179 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
183 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
184 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
188 match ("call", gfc_match_call, ST_CALL);
189 match ("close", gfc_match_close, ST_CLOSE);
190 match ("continue", gfc_match_continue, ST_CONTINUE);
191 match ("cycle", gfc_match_cycle, ST_CYCLE);
192 match ("case", gfc_match_case, ST_CASE);
193 match ("common", gfc_match_common, ST_COMMON);
194 match ("contains", gfc_match_eos, ST_CONTAINS);
198 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
199 match ("data", gfc_match_data, ST_DATA);
200 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
204 match ("end file", gfc_match_endfile, ST_END_FILE);
205 match ("exit", gfc_match_exit, ST_EXIT);
206 match ("else", gfc_match_else, ST_ELSE);
207 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
208 match ("else if", gfc_match_elseif, ST_ELSEIF);
209 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
211 if (gfc_match_end (&st) == MATCH_YES)
214 match ("entry% ", gfc_match_entry, ST_ENTRY);
215 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
216 match ("external", gfc_match_external, ST_ATTR_DECL);
220 match ("flush", gfc_match_flush, ST_FLUSH);
221 match ("format", gfc_match_format, ST_FORMAT);
225 match ("go to", gfc_match_goto, ST_GOTO);
229 match ("inquire", gfc_match_inquire, ST_INQUIRE);
230 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
231 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
232 match ("interface", gfc_match_interface, ST_INTERFACE);
233 match ("intent", gfc_match_intent, ST_ATTR_DECL);
234 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
238 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
239 match ("module", gfc_match_module, ST_MODULE);
243 match ("nullify", gfc_match_nullify, ST_NULLIFY);
244 match ("namelist", gfc_match_namelist, ST_NAMELIST);
248 match ("open", gfc_match_open, ST_OPEN);
249 match ("optional", gfc_match_optional, ST_ATTR_DECL);
253 match ("print", gfc_match_print, ST_WRITE);
254 match ("parameter", gfc_match_parameter, ST_PARAMETER);
255 match ("pause", gfc_match_pause, ST_PAUSE);
256 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
257 if (gfc_match_private (&st) == MATCH_YES)
259 match ("program", gfc_match_program, ST_PROGRAM);
260 if (gfc_match_public (&st) == MATCH_YES)
265 match ("read", gfc_match_read, ST_READ);
266 match ("return", gfc_match_return, ST_RETURN);
267 match ("rewind", gfc_match_rewind, ST_REWIND);
271 match ("sequence", gfc_match_eos, ST_SEQUENCE);
272 match ("stop", gfc_match_stop, ST_STOP);
273 match ("save", gfc_match_save, ST_ATTR_DECL);
277 match ("target", gfc_match_target, ST_ATTR_DECL);
278 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
282 match ("use% ", gfc_match_use, ST_USE);
286 match ("write", gfc_match_write, ST_WRITE);
290 /* All else has failed, so give up. See if any of the matchers has
291 stored an error message of some sort. */
293 if (gfc_error_check () == 0)
294 gfc_error_now ("Unclassifiable statement at %C");
298 gfc_error_recovery ();
306 /* Get the next statement in free form source. */
314 gfc_gobble_whitespace ();
316 c = gfc_peek_char ();
320 /* Found a statement label? */
321 m = gfc_match_st_label (&gfc_statement_label);
323 d = gfc_peek_char ();
324 if (m != MATCH_YES || !gfc_is_whitespace (d))
326 gfc_match_small_literal_int (&c);
328 gfc_error_now ("Statement label at %C is zero");
330 gfc_error_now ("Statement label at %C is out of range");
333 c = gfc_next_char ();
338 label_locus = gfc_current_locus;
340 gfc_gobble_whitespace ();
342 if (gfc_match_eos () == MATCH_YES)
345 ("Ignoring statement label in empty statement at %C");
346 gfc_free_st_label (gfc_statement_label);
347 gfc_statement_label = NULL;
353 return decode_statement ();
357 /* Get the next statement in fixed-form source. */
362 int label, digit_flag, i;
367 return decode_statement ();
369 /* Skip past the current label field, parsing a statement label if
370 one is there. This is a weird number parser, since the number is
371 contained within five columns and can have any kind of embedded
372 spaces. We also check for characters that make the rest of the
378 for (i = 0; i < 5; i++)
380 c = gfc_next_char_literal (0);
397 label = label * 10 + c - '0';
398 label_locus = gfc_current_locus;
402 /* Comments have already been skipped by the time we get
403 here so don't bother checking for them. */
406 gfc_buffer_error (0);
407 gfc_error ("Non-numeric character in statement label at %C");
415 gfc_warning_now ("Zero is not a valid statement label at %C");
418 /* We've found a valid statement label. */
419 gfc_statement_label = gfc_get_st_label (label);
423 /* Since this line starts a statement, it cannot be a continuation
424 of a previous statement. If we see something here besides a
425 space or zero, it must be a bad continuation line. */
427 c = gfc_next_char_literal (0);
431 if (c != ' ' && c!= '0')
433 gfc_buffer_error (0);
434 gfc_error ("Bad continuation line at %C");
438 /* Now that we've taken care of the statement label columns, we have
439 to make sure that the first nonblank character is not a '!'. If
440 it is, the rest of the line is a comment. */
444 loc = gfc_current_locus;
445 c = gfc_next_char_literal (0);
447 while (gfc_is_whitespace (c));
451 gfc_current_locus = loc;
453 if (gfc_match_eos () == MATCH_YES)
456 /* At this point, we've got a nonblank statement to parse. */
457 return decode_statement ();
461 gfc_warning ("Statement label in blank line will be " "ignored at %C");
467 /* Return the next non-ST_NONE statement to the caller. We also worry
468 about including files and the ends of include files at this stage. */
471 next_statement (void)
475 gfc_new_block = NULL;
479 gfc_statement_label = NULL;
480 gfc_buffer_error (1);
484 if (gfc_option.warn_line_truncation
485 && gfc_current_locus.lb->truncated)
486 gfc_warning_now ("Line truncated at %C");
491 gfc_skip_comments ();
500 (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
506 gfc_buffer_error (0);
509 check_statement_label (st);
515 /****************************** Parser ***********************************/
517 /* The parser subroutines are of type 'try' that fail if the file ends
520 /* Macros that expand to case-labels for various classes of
521 statements. Start with executable statements that directly do
524 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
525 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
526 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
527 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
528 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
529 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
530 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
531 case ST_LABEL_ASSIGNMENT: case ST_FLUSH
533 /* Statements that mark other executable statements. */
535 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
536 case ST_WHERE_BLOCK: case ST_SELECT_CASE
538 /* Declaration statements */
540 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
541 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
542 case ST_TYPE: case ST_INTERFACE
544 /* Block end statements. Errors associated with interchanging these
545 are detected in gfc_match_end(). */
547 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
548 case ST_END_PROGRAM: case ST_END_SUBROUTINE
551 /* Push a new state onto the stack. */
554 push_state (gfc_state_data * p, gfc_compile_state new_state, gfc_symbol * sym)
557 p->state = new_state;
558 p->previous = gfc_state_stack;
560 p->head = p->tail = NULL;
561 p->do_variable = NULL;
567 /* Pop the current state. */
573 gfc_state_stack = gfc_state_stack->previous;
577 /* Try to find the given state in the state stack. */
580 gfc_find_state (gfc_compile_state state)
584 for (p = gfc_state_stack; p; p = p->previous)
585 if (p->state == state)
588 return (p == NULL) ? FAILURE : SUCCESS;
592 /* Starts a new level in the statement list. */
595 new_level (gfc_code * q)
599 p = q->block = gfc_get_code ();
601 gfc_state_stack->head = gfc_state_stack->tail = p;
607 /* Add the current new_st code structure and adds it to the current
608 program unit. As a side-effect, it zeroes the new_st. */
618 p->loc = gfc_current_locus;
620 if (gfc_state_stack->head == NULL)
621 gfc_state_stack->head = p;
623 gfc_state_stack->tail->next = p;
625 while (p->next != NULL)
628 gfc_state_stack->tail = p;
636 /* Frees everything associated with the current statement. */
639 undo_new_statement (void)
641 gfc_free_statements (new_st.block);
642 gfc_free_statements (new_st.next);
643 gfc_free_statement (&new_st);
648 /* If the current statement has a statement label, make sure that it
649 is allowed to, or should have one. */
652 check_statement_label (gfc_statement st)
656 if (gfc_statement_label == NULL)
659 gfc_error ("FORMAT statement at %L does not have a statement label",
667 case ST_END_FUNCTION:
668 case ST_END_SUBROUTINE:
674 type = ST_LABEL_TARGET;
678 type = ST_LABEL_FORMAT;
681 /* Statement labels are not restricted from appearing on a
682 particular line. However, there are plenty of situations
683 where the resulting label can't be referenced. */
686 type = ST_LABEL_BAD_TARGET;
690 gfc_define_st_label (gfc_statement_label, type, &label_locus);
692 new_st.here = gfc_statement_label;
696 /* Figures out what the enclosing program unit is. This will be a
697 function, subroutine, program, block data or module. */
700 gfc_enclosing_unit (gfc_compile_state * result)
704 for (p = gfc_state_stack; p; p = p->previous)
705 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
706 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
707 || p->state == COMP_PROGRAM)
716 *result = COMP_PROGRAM;
721 /* Translate a statement enum to a string. */
724 gfc_ascii_statement (gfc_statement st)
730 case ST_ARITHMETIC_IF:
731 p = _("arithmetic IF");
737 p = _("attribute declaration");
767 p = _("data declaration");
775 case ST_DERIVED_DECL:
776 p = _("derived type declaration");
790 case ST_END_BLOCK_DATA:
791 p = "END BLOCK DATA";
802 case ST_END_FUNCTION:
808 case ST_END_INTERFACE:
820 case ST_END_SUBROUTINE:
821 p = "END SUBROUTINE";
841 case ST_FORALL_BLOCK: /* Fall through */
860 case ST_IMPLICIT_NONE:
863 case ST_IMPLIED_ENDDO:
864 p = _("implied END DO");
888 p = "MODULE PROCEDURE";
923 case ST_WHERE_BLOCK: /* Fall through */
933 case ST_POINTER_ASSIGNMENT:
934 p = _("pointer assignment");
945 case ST_STATEMENT_FUNCTION:
946 p = "STATEMENT FUNCTION";
948 case ST_LABEL_ASSIGNMENT:
949 p = "LABEL ASSIGNMENT";
952 p = "ENUM DEFINITION";
955 p = "ENUMERATOR DEFINITION";
961 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
968 /* Create a symbol for the main program and assign it to ns->proc_name. */
971 main_program_symbol (gfc_namespace * ns)
973 gfc_symbol *main_program;
974 symbol_attribute attr;
976 gfc_get_symbol ("MAIN__", ns, &main_program);
977 gfc_clear_attr (&attr);
978 attr.flavor = FL_PROCEDURE;
979 attr.proc = PROC_UNKNOWN;
981 attr.access = ACCESS_PUBLIC;
982 attr.is_main_program = 1;
983 main_program->attr = attr;
984 main_program->declared_at = gfc_current_locus;
985 ns->proc_name = main_program;
986 gfc_commit_symbols ();
990 /* Do whatever is necessary to accept the last statement. */
993 accept_statement (gfc_statement st)
1002 case ST_IMPLICIT_NONE:
1003 gfc_set_implicit_none ();
1012 gfc_current_ns->proc_name = gfc_new_block;
1015 /* If the statement is the end of a block, lay down a special code
1016 that allows a branch to the end of the block from within the
1021 if (gfc_statement_label != NULL)
1023 new_st.op = EXEC_NOP;
1029 /* The end-of-program unit statements do not get the special
1030 marker and require a statement of some sort if they are a
1033 case ST_END_PROGRAM:
1034 case ST_END_FUNCTION:
1035 case ST_END_SUBROUTINE:
1036 if (gfc_statement_label != NULL)
1038 new_st.op = EXEC_RETURN;
1054 gfc_commit_symbols ();
1055 gfc_warning_check ();
1056 gfc_clear_new_st ();
1060 /* Undo anything tentative that has been built for the current
1064 reject_statement (void)
1067 gfc_undo_symbols ();
1068 gfc_clear_warning ();
1069 undo_new_statement ();
1073 /* Generic complaint about an out of order statement. We also do
1074 whatever is necessary to clean up. */
1077 unexpected_statement (gfc_statement st)
1080 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1082 reject_statement ();
1086 /* Given the next statement seen by the matcher, make sure that it is
1087 in proper order with the last. This subroutine is initialized by
1088 calling it with an argument of ST_NONE. If there is a problem, we
1089 issue an error and return FAILURE. Otherwise we return SUCCESS.
1091 Individual parsers need to verify that the statements seen are
1092 valid before calling here, ie ENTRY statements are not allowed in
1093 INTERFACE blocks. The following diagram is taken from the standard:
1095 +---------------------------------------+
1096 | program subroutine function module |
1097 +---------------------------------------+
1099 |---------------------------------------+
1101 | +-----------+------------------+
1102 | | parameter | implicit |
1103 | +-----------+------------------+
1104 | format | | derived type |
1105 | entry | parameter | interface |
1106 | | data | specification |
1107 | | | statement func |
1108 | +-----------+------------------+
1109 | | data | executable |
1110 +--------+-----------+------------------+
1112 +---------------------------------------+
1113 | internal module/subprogram |
1114 +---------------------------------------+
1116 +---------------------------------------+
1123 { ORDER_START, ORDER_USE, ORDER_IMPLICIT_NONE, ORDER_IMPLICIT,
1124 ORDER_SPEC, ORDER_EXEC
1127 gfc_statement last_statement;
1133 verify_st_order (st_state * p, gfc_statement st)
1139 p->state = ORDER_START;
1143 if (p->state > ORDER_USE)
1145 p->state = ORDER_USE;
1148 case ST_IMPLICIT_NONE:
1149 if (p->state > ORDER_IMPLICIT_NONE)
1152 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1153 statement disqualifies a USE but not an IMPLICIT NONE.
1154 Duplicate IMPLICIT NONEs are caught when the implicit types
1157 p->state = ORDER_IMPLICIT_NONE;
1161 if (p->state > ORDER_IMPLICIT)
1163 p->state = ORDER_IMPLICIT;
1168 if (p->state < ORDER_IMPLICIT_NONE)
1169 p->state = ORDER_IMPLICIT_NONE;
1173 if (p->state >= ORDER_EXEC)
1175 if (p->state < ORDER_IMPLICIT)
1176 p->state = ORDER_IMPLICIT;
1180 if (p->state < ORDER_SPEC)
1181 p->state = ORDER_SPEC;
1186 case ST_DERIVED_DECL:
1188 if (p->state >= ORDER_EXEC)
1190 if (p->state < ORDER_SPEC)
1191 p->state = ORDER_SPEC;
1196 if (p->state < ORDER_EXEC)
1197 p->state = ORDER_EXEC;
1202 ("Unexpected %s statement in verify_st_order() at %C",
1203 gfc_ascii_statement (st));
1206 /* All is well, record the statement in case we need it next time. */
1207 p->where = gfc_current_locus;
1208 p->last_statement = st;
1212 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1213 gfc_ascii_statement (st),
1214 gfc_ascii_statement (p->last_statement), &p->where);
1220 /* Handle an unexpected end of file. This is a show-stopper... */
1222 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1225 unexpected_eof (void)
1229 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1231 /* Memory cleanup. Move to "second to last". */
1232 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1235 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1238 longjmp (eof_buf, 1);
1242 /* Parse a derived type. */
1245 parse_derived (void)
1247 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1254 accept_statement (ST_DERIVED_DECL);
1255 push_state (&s, COMP_DERIVED, gfc_new_block);
1257 gfc_new_block->component_access = ACCESS_PUBLIC;
1264 while (compiling_type)
1266 st = next_statement ();
1273 accept_statement (st);
1280 if (!seen_component)
1282 gfc_error ("Derived type definition at %C has no components");
1286 accept_statement (ST_END_TYPE);
1290 if (gfc_find_state (COMP_MODULE) == FAILURE)
1293 ("PRIVATE statement in TYPE at %C must be inside a MODULE");
1300 gfc_error ("PRIVATE statement at %C must precede "
1301 "structure components");
1308 gfc_error ("Duplicate PRIVATE statement at %C");
1312 s.sym->component_access = ACCESS_PRIVATE;
1313 accept_statement (ST_PRIVATE);
1320 gfc_error ("SEQUENCE statement at %C must precede "
1321 "structure components");
1326 if (gfc_current_block ()->attr.sequence)
1327 gfc_warning ("SEQUENCE attribute at %C already specified in "
1332 gfc_error ("Duplicate SEQUENCE statement at %C");
1337 gfc_add_sequence (&gfc_current_block ()->attr,
1338 gfc_current_block ()->name, NULL);
1342 unexpected_statement (st);
1347 /* Sanity checks on the structure. If the structure has the
1348 SEQUENCE attribute, then all component structures must also have
1350 if (error_flag == 0 && gfc_current_block ()->attr.sequence)
1351 for (c = gfc_current_block ()->components; c; c = c->next)
1353 if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
1356 ("Component %s of SEQUENCE type declared at %C does not "
1357 "have the SEQUENCE attribute", c->ts.derived->name);
1366 /* Parse an ENUM. */
1375 int seen_enumerator = 0;
1379 push_state (&s, COMP_ENUM, gfc_new_block);
1383 while (compiling_enum)
1385 st = next_statement ();
1393 seen_enumerator = 1;
1394 accept_statement (st);
1399 if (!seen_enumerator)
1401 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1404 accept_statement (st);
1408 gfc_free_enum_history ();
1409 unexpected_statement (st);
1416 /* Parse an interface. We must be able to deal with the possibility
1417 of recursive interfaces. The parse_spec() subroutine is mutually
1418 recursive with parse_interface(). */
1420 static gfc_statement parse_spec (gfc_statement);
1423 parse_interface (void)
1425 gfc_compile_state new_state, current_state;
1426 gfc_symbol *prog_unit, *sym;
1427 gfc_interface_info save;
1428 gfc_state_data s1, s2;
1431 accept_statement (ST_INTERFACE);
1433 current_interface.ns = gfc_current_ns;
1434 save = current_interface;
1436 sym = (current_interface.type == INTERFACE_GENERIC
1437 || current_interface.type == INTERFACE_USER_OP) ? gfc_new_block : NULL;
1439 push_state (&s1, COMP_INTERFACE, sym);
1440 current_state = COMP_NONE;
1443 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1445 st = next_statement ();
1452 new_state = COMP_SUBROUTINE;
1453 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1454 gfc_new_block->formal, NULL);
1458 new_state = COMP_FUNCTION;
1459 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1460 gfc_new_block->formal, NULL);
1463 case ST_MODULE_PROC: /* The module procedure matcher makes
1464 sure the context is correct. */
1465 accept_statement (st);
1466 gfc_free_namespace (gfc_current_ns);
1469 case ST_END_INTERFACE:
1470 gfc_free_namespace (gfc_current_ns);
1471 gfc_current_ns = current_interface.ns;
1475 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1476 gfc_ascii_statement (st));
1477 reject_statement ();
1478 gfc_free_namespace (gfc_current_ns);
1483 /* Make sure that a generic interface has only subroutines or
1484 functions and that the generic name has the right attribute. */
1485 if (current_interface.type == INTERFACE_GENERIC)
1487 if (current_state == COMP_NONE)
1489 if (new_state == COMP_FUNCTION)
1490 gfc_add_function (&sym->attr, sym->name, NULL);
1491 else if (new_state == COMP_SUBROUTINE)
1492 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1494 current_state = new_state;
1498 if (new_state != current_state)
1500 if (new_state == COMP_SUBROUTINE)
1502 ("SUBROUTINE at %C does not belong in a generic function "
1505 if (new_state == COMP_FUNCTION)
1507 ("FUNCTION at %C does not belong in a generic subroutine "
1513 push_state (&s2, new_state, gfc_new_block);
1514 accept_statement (st);
1515 prog_unit = gfc_new_block;
1516 prog_unit->formal_ns = gfc_current_ns;
1519 /* Read data declaration statements. */
1520 st = parse_spec (ST_NONE);
1522 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1524 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1525 gfc_ascii_statement (st));
1526 reject_statement ();
1530 current_interface = save;
1531 gfc_add_interface (prog_unit);
1541 /* Parse a set of specification statements. Returns the statement
1542 that doesn't fit. */
1544 static gfc_statement
1545 parse_spec (gfc_statement st)
1549 verify_st_order (&ss, ST_NONE);
1551 st = next_statement ();
1561 case ST_DATA: /* Not allowed in interfaces */
1562 if (gfc_current_state () == COMP_INTERFACE)
1568 case ST_IMPLICIT_NONE:
1573 case ST_DERIVED_DECL:
1575 if (verify_st_order (&ss, st) == FAILURE)
1577 reject_statement ();
1578 st = next_statement ();
1588 case ST_DERIVED_DECL:
1594 if (gfc_current_state () != COMP_MODULE)
1596 gfc_error ("%s statement must appear in a MODULE",
1597 gfc_ascii_statement (st));
1601 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1603 gfc_error ("%s statement at %C follows another accessibility "
1604 "specification", gfc_ascii_statement (st));
1608 gfc_current_ns->default_access = (st == ST_PUBLIC)
1609 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1617 accept_statement (st);
1618 st = next_statement ();
1622 accept_statement (st);
1624 st = next_statement ();
1635 /* Parse a WHERE block, (not a simple WHERE statement). */
1638 parse_where_block (void)
1640 int seen_empty_else;
1645 accept_statement (ST_WHERE_BLOCK);
1646 top = gfc_state_stack->tail;
1648 push_state (&s, COMP_WHERE, gfc_new_block);
1650 d = add_statement ();
1651 d->expr = top->expr;
1657 seen_empty_else = 0;
1661 st = next_statement ();
1667 case ST_WHERE_BLOCK:
1668 parse_where_block ();
1673 accept_statement (st);
1677 if (seen_empty_else)
1680 ("ELSEWHERE statement at %C follows previous unmasked "
1685 if (new_st.expr == NULL)
1686 seen_empty_else = 1;
1688 d = new_level (gfc_state_stack->head);
1690 d->expr = new_st.expr;
1692 accept_statement (st);
1697 accept_statement (st);
1701 gfc_error ("Unexpected %s statement in WHERE block at %C",
1702 gfc_ascii_statement (st));
1703 reject_statement ();
1708 while (st != ST_END_WHERE);
1714 /* Parse a FORALL block (not a simple FORALL statement). */
1717 parse_forall_block (void)
1723 accept_statement (ST_FORALL_BLOCK);
1724 top = gfc_state_stack->tail;
1726 push_state (&s, COMP_FORALL, gfc_new_block);
1728 d = add_statement ();
1729 d->op = EXEC_FORALL;
1734 st = next_statement ();
1739 case ST_POINTER_ASSIGNMENT:
1742 accept_statement (st);
1745 case ST_WHERE_BLOCK:
1746 parse_where_block ();
1749 case ST_FORALL_BLOCK:
1750 parse_forall_block ();
1754 accept_statement (st);
1761 gfc_error ("Unexpected %s statement in FORALL block at %C",
1762 gfc_ascii_statement (st));
1764 reject_statement ();
1768 while (st != ST_END_FORALL);
1774 static gfc_statement parse_executable (gfc_statement);
1776 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
1779 parse_if_block (void)
1788 accept_statement (ST_IF_BLOCK);
1790 top = gfc_state_stack->tail;
1791 push_state (&s, COMP_IF, gfc_new_block);
1793 new_st.op = EXEC_IF;
1794 d = add_statement ();
1796 d->expr = top->expr;
1802 st = parse_executable (ST_NONE);
1813 ("ELSE IF statement at %C cannot follow ELSE statement at %L",
1816 reject_statement ();
1820 d = new_level (gfc_state_stack->head);
1822 d->expr = new_st.expr;
1824 accept_statement (st);
1831 gfc_error ("Duplicate ELSE statements at %L and %C",
1833 reject_statement ();
1838 else_locus = gfc_current_locus;
1840 d = new_level (gfc_state_stack->head);
1843 accept_statement (st);
1851 unexpected_statement (st);
1855 while (st != ST_ENDIF);
1858 accept_statement (st);
1862 /* Parse a SELECT block. */
1865 parse_select_block (void)
1871 accept_statement (ST_SELECT_CASE);
1873 cp = gfc_state_stack->tail;
1874 push_state (&s, COMP_SELECT, gfc_new_block);
1876 /* Make sure that the next statement is a CASE or END SELECT. */
1879 st = next_statement ();
1882 if (st == ST_END_SELECT)
1884 /* Empty SELECT CASE is OK. */
1885 accept_statement (st);
1893 ("Expected a CASE or END SELECT statement following SELECT CASE "
1896 reject_statement ();
1899 /* At this point, we're got a nonempty select block. */
1900 cp = new_level (cp);
1903 accept_statement (st);
1907 st = parse_executable (ST_NONE);
1914 cp = new_level (gfc_state_stack->head);
1916 gfc_clear_new_st ();
1918 accept_statement (st);
1924 /* Can't have an executable statement because of
1925 parse_executable(). */
1927 unexpected_statement (st);
1931 while (st != ST_END_SELECT);
1934 accept_statement (st);
1938 /* Given a symbol, make sure it is not an iteration variable for a DO
1939 statement. This subroutine is called when the symbol is seen in a
1940 context that causes it to become redefined. If the symbol is an
1941 iterator, we generate an error message and return nonzero. */
1944 gfc_check_do_variable (gfc_symtree *st)
1948 for (s=gfc_state_stack; s; s = s->previous)
1949 if (s->do_variable == st)
1951 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
1952 "loop beginning at %L", st->name, &s->head->loc);
1960 /* Checks to see if the current statement label closes an enddo.
1961 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
1962 an error) if it incorrectly closes an ENDDO. */
1965 check_do_closure (void)
1969 if (gfc_statement_label == NULL)
1972 for (p = gfc_state_stack; p; p = p->previous)
1973 if (p->state == COMP_DO)
1977 return 0; /* No loops to close */
1979 if (p->ext.end_do_label == gfc_statement_label)
1982 if (p == gfc_state_stack)
1986 ("End of nonblock DO statement at %C is within another block");
1990 /* At this point, the label doesn't terminate the innermost loop.
1991 Make sure it doesn't terminate another one. */
1992 for (; p; p = p->previous)
1993 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
1995 gfc_error ("End of nonblock DO statement at %C is interwoven "
1996 "with another DO loop");
2004 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2005 handled inside of parse_executable(), because they aren't really
2009 parse_do_block (void)
2016 s.ext.end_do_label = new_st.label;
2018 if (new_st.ext.iterator != NULL)
2019 stree = new_st.ext.iterator->var->symtree;
2023 accept_statement (ST_DO);
2025 top = gfc_state_stack->tail;
2026 push_state (&s, COMP_DO, gfc_new_block);
2028 s.do_variable = stree;
2030 top->block = new_level (top);
2031 top->block->op = EXEC_DO;
2034 st = parse_executable (ST_NONE);
2042 if (s.ext.end_do_label != NULL
2043 && s.ext.end_do_label != gfc_statement_label)
2045 ("Statement label in ENDDO at %C doesn't match DO label");
2047 if (gfc_statement_label != NULL)
2049 new_st.op = EXEC_NOP;
2054 case ST_IMPLIED_ENDDO:
2058 unexpected_statement (st);
2063 accept_statement (st);
2067 /* Accept a series of executable statements. We return the first
2068 statement that doesn't fit to the caller. Any block statements are
2069 passed on to the correct handler, which usually passes the buck
2072 static gfc_statement
2073 parse_executable (gfc_statement st)
2078 st = next_statement ();
2080 for (;; st = next_statement ())
2083 close_flag = check_do_closure ();
2088 case ST_END_PROGRAM:
2091 case ST_END_FUNCTION:
2095 case ST_END_SUBROUTINE:
2100 case ST_SELECT_CASE:
2102 ("%s statement at %C cannot terminate a non-block DO loop",
2103 gfc_ascii_statement (st));
2119 accept_statement (st);
2120 if (close_flag == 1)
2121 return ST_IMPLIED_ENDDO;
2128 case ST_SELECT_CASE:
2129 parse_select_block ();
2134 if (check_do_closure () == 1)
2135 return ST_IMPLIED_ENDDO;
2138 case ST_WHERE_BLOCK:
2139 parse_where_block ();
2142 case ST_FORALL_BLOCK:
2143 parse_forall_block ();
2157 /* Parse a series of contained program units. */
2159 static void parse_progunit (gfc_statement);
2162 /* Fix the symbols for sibling functions. These are incorrectly added to
2163 the child namespace as the parser didn't know about this procedure. */
2166 gfc_fixup_sibling_symbols (gfc_symbol * sym, gfc_namespace * siblings)
2170 gfc_symbol *old_sym;
2172 sym->attr.referenced = 1;
2173 for (ns = siblings; ns; ns = ns->sibling)
2175 gfc_find_sym_tree (sym->name, ns, 0, &st);
2179 old_sym = st->n.sym;
2180 if ((old_sym->attr.flavor == FL_PROCEDURE
2181 || old_sym->ts.type == BT_UNKNOWN)
2182 && old_sym->ns == ns
2183 && ! old_sym->attr.contained)
2185 /* Replace it with the symbol from the parent namespace. */
2189 /* Free the old (local) symbol. */
2191 if (old_sym->refs == 0)
2192 gfc_free_symbol (old_sym);
2195 /* Do the same for any contained procedures. */
2196 gfc_fixup_sibling_symbols (sym, ns->contained);
2201 parse_contained (int module)
2203 gfc_namespace *ns, *parent_ns;
2204 gfc_state_data s1, s2;
2209 push_state (&s1, COMP_CONTAINS, NULL);
2210 parent_ns = gfc_current_ns;
2214 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2216 gfc_current_ns->sibling = parent_ns->contained;
2217 parent_ns->contained = gfc_current_ns;
2219 st = next_statement ();
2228 accept_statement (st);
2231 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2234 /* For internal procedures, create/update the symbol in the
2235 parent namespace. */
2239 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2241 ("Contained procedure '%s' at %C is already ambiguous",
2242 gfc_new_block->name);
2245 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2246 &gfc_new_block->declared_at) ==
2249 if (st == ST_FUNCTION)
2250 gfc_add_function (&sym->attr, sym->name,
2251 &gfc_new_block->declared_at);
2253 gfc_add_subroutine (&sym->attr, sym->name,
2254 &gfc_new_block->declared_at);
2258 gfc_commit_symbols ();
2261 sym = gfc_new_block;
2263 /* Mark this as a contained function, so it isn't replaced
2264 by other module functions. */
2265 sym->attr.contained = 1;
2266 sym->attr.referenced = 1;
2268 parse_progunit (ST_NONE);
2270 /* Fix up any sibling functions that refer to this one. */
2271 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2272 /* Or refer to any of its alternate entry points. */
2273 for (el = gfc_current_ns->entries; el; el = el->next)
2274 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2276 gfc_current_ns->code = s2.head;
2277 gfc_current_ns = parent_ns;
2282 /* These statements are associated with the end of the host
2284 case ST_END_FUNCTION:
2286 case ST_END_PROGRAM:
2287 case ST_END_SUBROUTINE:
2288 accept_statement (st);
2292 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2293 gfc_ascii_statement (st));
2294 reject_statement ();
2298 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2299 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2301 /* The first namespace in the list is guaranteed to not have
2302 anything (worthwhile) in it. */
2304 gfc_current_ns = parent_ns;
2306 ns = gfc_current_ns->contained;
2307 gfc_current_ns->contained = ns->sibling;
2308 gfc_free_namespace (ns);
2314 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2317 parse_progunit (gfc_statement st)
2322 st = parse_spec (st);
2332 accept_statement (st);
2342 st = parse_executable (st);
2353 accept_statement (st);
2360 unexpected_statement (st);
2361 reject_statement ();
2362 st = next_statement ();
2368 for (p = gfc_state_stack; p; p = p->previous)
2369 if (p->state == COMP_CONTAINS)
2372 if (gfc_find_state (COMP_MODULE) == SUCCESS)
2377 gfc_error ("CONTAINS statement at %C is already in a contained "
2379 st = next_statement ();
2383 parse_contained (0);
2386 gfc_current_ns->code = gfc_state_stack->head;
2390 /* Come here to complain about a global symbol already in use as
2394 global_used (gfc_gsymbol *sym, locus *where)
2399 where = &gfc_current_locus;
2409 case GSYM_SUBROUTINE:
2410 name = "SUBROUTINE";
2415 case GSYM_BLOCK_DATA:
2416 name = "BLOCK DATA";
2422 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
2426 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
2427 gfc_new_block->name, where, name, &sym->where);
2431 /* Parse a block data program unit. */
2434 parse_block_data (void)
2437 static locus blank_locus;
2438 static int blank_block=0;
2441 gfc_current_ns->proc_name = gfc_new_block;
2442 gfc_current_ns->is_block_data = 1;
2444 if (gfc_new_block == NULL)
2447 gfc_error ("Blank BLOCK DATA at %C conflicts with "
2448 "prior BLOCK DATA at %L", &blank_locus);
2452 blank_locus = gfc_current_locus;
2457 s = gfc_get_gsymbol (gfc_new_block->name);
2458 if (s->type != GSYM_UNKNOWN)
2459 global_used(s, NULL);
2462 s->type = GSYM_BLOCK_DATA;
2463 s->where = gfc_current_locus;
2467 st = parse_spec (ST_NONE);
2469 while (st != ST_END_BLOCK_DATA)
2471 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
2472 gfc_ascii_statement (st));
2473 reject_statement ();
2474 st = next_statement ();
2479 /* Parse a module subprogram. */
2487 s = gfc_get_gsymbol (gfc_new_block->name);
2488 if (s->type != GSYM_UNKNOWN)
2489 global_used(s, NULL);
2492 s->type = GSYM_MODULE;
2493 s->where = gfc_current_locus;
2496 st = parse_spec (ST_NONE);
2505 parse_contained (1);
2509 accept_statement (st);
2513 gfc_error ("Unexpected %s statement in MODULE at %C",
2514 gfc_ascii_statement (st));
2516 reject_statement ();
2517 st = next_statement ();
2523 /* Add a procedure name to the global symbol table. */
2526 add_global_procedure (int sub)
2530 s = gfc_get_gsymbol(gfc_new_block->name);
2532 if (s->type != GSYM_UNKNOWN)
2533 global_used(s, NULL);
2536 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2537 s->where = gfc_current_locus;
2542 /* Add a program to the global symbol table. */
2545 add_global_program (void)
2549 if (gfc_new_block == NULL)
2551 s = gfc_get_gsymbol (gfc_new_block->name);
2553 if (s->type != GSYM_UNKNOWN)
2554 global_used(s, NULL);
2557 s->type = GSYM_PROGRAM;
2558 s->where = gfc_current_locus;
2563 /* Top level parser. */
2566 gfc_parse_file (void)
2568 int seen_program, errors_before, errors;
2569 gfc_state_data top, s;
2573 top.state = COMP_NONE;
2575 top.previous = NULL;
2576 top.head = top.tail = NULL;
2577 top.do_variable = NULL;
2579 gfc_state_stack = ⊤
2581 gfc_clear_new_st ();
2583 gfc_statement_label = NULL;
2585 if (setjmp (eof_buf))
2586 return FAILURE; /* Come here on unexpected EOF */
2590 /* Exit early for empty files. */
2596 st = next_statement ();
2605 goto duplicate_main;
2607 prog_locus = gfc_current_locus;
2609 push_state (&s, COMP_PROGRAM, gfc_new_block);
2610 main_program_symbol(gfc_current_ns);
2611 accept_statement (st);
2612 add_global_program ();
2613 parse_progunit (ST_NONE);
2617 add_global_procedure (1);
2618 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
2619 accept_statement (st);
2620 parse_progunit (ST_NONE);
2624 add_global_procedure (0);
2625 push_state (&s, COMP_FUNCTION, gfc_new_block);
2626 accept_statement (st);
2627 parse_progunit (ST_NONE);
2631 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
2632 accept_statement (st);
2633 parse_block_data ();
2637 push_state (&s, COMP_MODULE, gfc_new_block);
2638 accept_statement (st);
2640 gfc_get_errors (NULL, &errors_before);
2644 /* Anything else starts a nameless main program block. */
2647 goto duplicate_main;
2649 prog_locus = gfc_current_locus;
2651 push_state (&s, COMP_PROGRAM, gfc_new_block);
2652 main_program_symbol(gfc_current_ns);
2653 parse_progunit (st);
2657 gfc_current_ns->code = s.head;
2659 gfc_resolve (gfc_current_ns);
2661 /* Dump the parse tree if requested. */
2662 if (gfc_option.verbose)
2663 gfc_show_namespace (gfc_current_ns);
2665 gfc_get_errors (NULL, &errors);
2666 if (s.state == COMP_MODULE)
2668 gfc_dump_module (s.sym->name, errors_before == errors);
2669 if (errors == 0 && ! gfc_option.flag_no_backend)
2670 gfc_generate_module_code (gfc_current_ns);
2674 if (errors == 0 && ! gfc_option.flag_no_backend)
2675 gfc_generate_code (gfc_current_ns);
2686 /* If we see a duplicate main program, shut down. If the second
2687 instance is an implied main program, ie data decls or executable
2688 statements, we're in for lots of errors. */
2689 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
2690 reject_statement ();