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, 0);
323 d = gfc_peek_char ();
324 if (m != MATCH_YES || !gfc_is_whitespace (d))
328 /* Skip the bad statement label. */
329 gfc_warning_now ("Ignoring bad statement label at %C");
330 c = gfc_next_char ();
336 label_locus = gfc_current_locus;
338 if (gfc_statement_label->value == 0)
340 gfc_warning_now ("Ignoring statement label of zero at %C");
341 gfc_free_st_label (gfc_statement_label);
342 gfc_statement_label = NULL;
345 gfc_gobble_whitespace ();
347 if (gfc_match_eos () == MATCH_YES)
350 ("Ignoring statement label in empty statement at %C");
351 gfc_free_st_label (gfc_statement_label);
352 gfc_statement_label = NULL;
358 return decode_statement ();
362 /* Get the next statement in fixed-form source. */
367 int label, digit_flag, i;
372 return decode_statement ();
374 /* Skip past the current label field, parsing a statement label if
375 one is there. This is a weird number parser, since the number is
376 contained within five columns and can have any kind of embedded
377 spaces. We also check for characters that make the rest of the
383 for (i = 0; i < 5; i++)
385 c = gfc_next_char_literal (0);
402 label = label * 10 + c - '0';
403 label_locus = gfc_current_locus;
407 /* Comments have already been skipped by the time we get
408 here so don't bother checking for them. */
411 gfc_buffer_error (0);
412 gfc_error ("Non-numeric character in statement label at %C");
420 gfc_warning_now ("Zero is not a valid statement label at %C");
423 /* We've found a valid statement label. */
424 gfc_statement_label = gfc_get_st_label (label);
428 /* Since this line starts a statement, it cannot be a continuation
429 of a previous statement. If we see something here besides a
430 space or zero, it must be a bad continuation line. */
432 c = gfc_next_char_literal (0);
436 if (c != ' ' && c!= '0')
438 gfc_buffer_error (0);
439 gfc_error ("Bad continuation line at %C");
443 /* Now that we've taken care of the statement label columns, we have
444 to make sure that the first nonblank character is not a '!'. If
445 it is, the rest of the line is a comment. */
449 loc = gfc_current_locus;
450 c = gfc_next_char_literal (0);
452 while (gfc_is_whitespace (c));
456 gfc_current_locus = loc;
458 if (gfc_match_eos () == MATCH_YES)
461 /* At this point, we've got a nonblank statement to parse. */
462 return decode_statement ();
466 gfc_warning ("Statement label in blank line will be " "ignored at %C");
472 /* Return the next non-ST_NONE statement to the caller. We also worry
473 about including files and the ends of include files at this stage. */
476 next_statement (void)
480 gfc_new_block = NULL;
484 gfc_statement_label = NULL;
485 gfc_buffer_error (1);
489 if (gfc_option.warn_line_truncation
490 && gfc_current_locus.lb->truncated)
491 gfc_warning_now ("Line truncated at %C");
496 gfc_skip_comments ();
505 (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
511 gfc_buffer_error (0);
514 check_statement_label (st);
520 /****************************** Parser ***********************************/
522 /* The parser subroutines are of type 'try' that fail if the file ends
525 /* Macros that expand to case-labels for various classes of
526 statements. Start with executable statements that directly do
529 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
530 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
531 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
532 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
533 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
534 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
535 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
536 case ST_LABEL_ASSIGNMENT: case ST_FLUSH
538 /* Statements that mark other executable statements. */
540 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
541 case ST_WHERE_BLOCK: case ST_SELECT_CASE
543 /* Declaration statements */
545 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
546 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
547 case ST_TYPE: case ST_INTERFACE
549 /* Block end statements. Errors associated with interchanging these
550 are detected in gfc_match_end(). */
552 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
553 case ST_END_PROGRAM: case ST_END_SUBROUTINE
556 /* Push a new state onto the stack. */
559 push_state (gfc_state_data * p, gfc_compile_state new_state, gfc_symbol * sym)
562 p->state = new_state;
563 p->previous = gfc_state_stack;
565 p->head = p->tail = NULL;
566 p->do_variable = NULL;
572 /* Pop the current state. */
578 gfc_state_stack = gfc_state_stack->previous;
582 /* Try to find the given state in the state stack. */
585 gfc_find_state (gfc_compile_state state)
589 for (p = gfc_state_stack; p; p = p->previous)
590 if (p->state == state)
593 return (p == NULL) ? FAILURE : SUCCESS;
597 /* Starts a new level in the statement list. */
600 new_level (gfc_code * q)
604 p = q->block = gfc_get_code ();
606 gfc_state_stack->head = gfc_state_stack->tail = p;
612 /* Add the current new_st code structure and adds it to the current
613 program unit. As a side-effect, it zeroes the new_st. */
623 p->loc = gfc_current_locus;
625 if (gfc_state_stack->head == NULL)
626 gfc_state_stack->head = p;
628 gfc_state_stack->tail->next = p;
630 while (p->next != NULL)
633 gfc_state_stack->tail = p;
641 /* Frees everything associated with the current statement. */
644 undo_new_statement (void)
646 gfc_free_statements (new_st.block);
647 gfc_free_statements (new_st.next);
648 gfc_free_statement (&new_st);
653 /* If the current statement has a statement label, make sure that it
654 is allowed to, or should have one. */
657 check_statement_label (gfc_statement st)
661 if (gfc_statement_label == NULL)
664 gfc_error ("FORMAT statement at %L does not have a statement label",
672 case ST_END_FUNCTION:
673 case ST_END_SUBROUTINE:
679 type = ST_LABEL_TARGET;
683 type = ST_LABEL_FORMAT;
686 /* Statement labels are not restricted from appearing on a
687 particular line. However, there are plenty of situations
688 where the resulting label can't be referenced. */
691 type = ST_LABEL_BAD_TARGET;
695 gfc_define_st_label (gfc_statement_label, type, &label_locus);
697 new_st.here = gfc_statement_label;
701 /* Figures out what the enclosing program unit is. This will be a
702 function, subroutine, program, block data or module. */
705 gfc_enclosing_unit (gfc_compile_state * result)
709 for (p = gfc_state_stack; p; p = p->previous)
710 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
711 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
712 || p->state == COMP_PROGRAM)
721 *result = COMP_PROGRAM;
726 /* Translate a statement enum to a string. */
729 gfc_ascii_statement (gfc_statement st)
735 case ST_ARITHMETIC_IF:
736 p = _("arithmetic IF");
742 p = _("attribute declaration");
772 p = _("data declaration");
780 case ST_DERIVED_DECL:
781 p = _("derived type declaration");
795 case ST_END_BLOCK_DATA:
796 p = "END BLOCK DATA";
807 case ST_END_FUNCTION:
813 case ST_END_INTERFACE:
825 case ST_END_SUBROUTINE:
826 p = "END SUBROUTINE";
846 case ST_FORALL_BLOCK: /* Fall through */
865 case ST_IMPLICIT_NONE:
868 case ST_IMPLIED_ENDDO:
869 p = _("implied END DO");
893 p = "MODULE PROCEDURE";
928 case ST_WHERE_BLOCK: /* Fall through */
938 case ST_POINTER_ASSIGNMENT:
939 p = _("pointer assignment");
950 case ST_STATEMENT_FUNCTION:
951 p = "STATEMENT FUNCTION";
953 case ST_LABEL_ASSIGNMENT:
954 p = "LABEL ASSIGNMENT";
957 p = "ENUM DEFINITION";
960 p = "ENUMERATOR DEFINITION";
966 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
973 /* Do whatever is necessary to accept the last statement. */
976 accept_statement (gfc_statement st)
985 case ST_IMPLICIT_NONE:
986 gfc_set_implicit_none ();
995 gfc_current_ns->proc_name = gfc_new_block;
998 /* If the statement is the end of a block, lay down a special code
999 that allows a branch to the end of the block from within the
1004 if (gfc_statement_label != NULL)
1006 new_st.op = EXEC_NOP;
1012 /* The end-of-program unit statements do not get the special
1013 marker and require a statement of some sort if they are a
1016 case ST_END_PROGRAM:
1017 case ST_END_FUNCTION:
1018 case ST_END_SUBROUTINE:
1019 if (gfc_statement_label != NULL)
1021 new_st.op = EXEC_RETURN;
1037 gfc_commit_symbols ();
1038 gfc_warning_check ();
1039 gfc_clear_new_st ();
1043 /* Undo anything tentative that has been built for the current
1047 reject_statement (void)
1050 gfc_undo_symbols ();
1051 gfc_clear_warning ();
1052 undo_new_statement ();
1056 /* Generic complaint about an out of order statement. We also do
1057 whatever is necessary to clean up. */
1060 unexpected_statement (gfc_statement st)
1063 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1065 reject_statement ();
1069 /* Given the next statement seen by the matcher, make sure that it is
1070 in proper order with the last. This subroutine is initialized by
1071 calling it with an argument of ST_NONE. If there is a problem, we
1072 issue an error and return FAILURE. Otherwise we return SUCCESS.
1074 Individual parsers need to verify that the statements seen are
1075 valid before calling here, ie ENTRY statements are not allowed in
1076 INTERFACE blocks. The following diagram is taken from the standard:
1078 +---------------------------------------+
1079 | program subroutine function module |
1080 +---------------------------------------+
1082 |---------------------------------------+
1084 | +-----------+------------------+
1085 | | parameter | implicit |
1086 | +-----------+------------------+
1087 | format | | derived type |
1088 | entry | parameter | interface |
1089 | | data | specification |
1090 | | | statement func |
1091 | +-----------+------------------+
1092 | | data | executable |
1093 +--------+-----------+------------------+
1095 +---------------------------------------+
1096 | internal module/subprogram |
1097 +---------------------------------------+
1099 +---------------------------------------+
1106 { ORDER_START, ORDER_USE, ORDER_IMPLICIT_NONE, ORDER_IMPLICIT,
1107 ORDER_SPEC, ORDER_EXEC
1110 gfc_statement last_statement;
1116 verify_st_order (st_state * p, gfc_statement st)
1122 p->state = ORDER_START;
1126 if (p->state > ORDER_USE)
1128 p->state = ORDER_USE;
1131 case ST_IMPLICIT_NONE:
1132 if (p->state > ORDER_IMPLICIT_NONE)
1135 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1136 statement disqualifies a USE but not an IMPLICIT NONE.
1137 Duplicate IMPLICIT NONEs are caught when the implicit types
1140 p->state = ORDER_IMPLICIT_NONE;
1144 if (p->state > ORDER_IMPLICIT)
1146 p->state = ORDER_IMPLICIT;
1151 if (p->state < ORDER_IMPLICIT_NONE)
1152 p->state = ORDER_IMPLICIT_NONE;
1156 if (p->state >= ORDER_EXEC)
1158 if (p->state < ORDER_IMPLICIT)
1159 p->state = ORDER_IMPLICIT;
1163 if (p->state < ORDER_SPEC)
1164 p->state = ORDER_SPEC;
1169 case ST_DERIVED_DECL:
1171 if (p->state >= ORDER_EXEC)
1173 if (p->state < ORDER_SPEC)
1174 p->state = ORDER_SPEC;
1179 if (p->state < ORDER_EXEC)
1180 p->state = ORDER_EXEC;
1185 ("Unexpected %s statement in verify_st_order() at %C",
1186 gfc_ascii_statement (st));
1189 /* All is well, record the statement in case we need it next time. */
1190 p->where = gfc_current_locus;
1191 p->last_statement = st;
1195 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1196 gfc_ascii_statement (st),
1197 gfc_ascii_statement (p->last_statement), &p->where);
1203 /* Handle an unexpected end of file. This is a show-stopper... */
1205 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1208 unexpected_eof (void)
1212 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1214 /* Memory cleanup. Move to "second to last". */
1215 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1218 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1221 longjmp (eof_buf, 1);
1225 /* Parse a derived type. */
1228 parse_derived (void)
1230 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1237 accept_statement (ST_DERIVED_DECL);
1238 push_state (&s, COMP_DERIVED, gfc_new_block);
1240 gfc_new_block->component_access = ACCESS_PUBLIC;
1247 while (compiling_type)
1249 st = next_statement ();
1256 accept_statement (st);
1263 if (!seen_component)
1265 gfc_error ("Derived type definition at %C has no components");
1269 accept_statement (ST_END_TYPE);
1273 if (gfc_find_state (COMP_MODULE) == FAILURE)
1276 ("PRIVATE statement in TYPE at %C must be inside a MODULE");
1283 gfc_error ("PRIVATE statement at %C must precede "
1284 "structure components");
1291 gfc_error ("Duplicate PRIVATE statement at %C");
1295 s.sym->component_access = ACCESS_PRIVATE;
1296 accept_statement (ST_PRIVATE);
1303 gfc_error ("SEQUENCE statement at %C must precede "
1304 "structure components");
1309 if (gfc_current_block ()->attr.sequence)
1310 gfc_warning ("SEQUENCE attribute at %C already specified in "
1315 gfc_error ("Duplicate SEQUENCE statement at %C");
1320 gfc_add_sequence (&gfc_current_block ()->attr,
1321 gfc_current_block ()->name, NULL);
1325 unexpected_statement (st);
1330 /* Sanity checks on the structure. If the structure has the
1331 SEQUENCE attribute, then all component structures must also have
1333 if (error_flag == 0 && gfc_current_block ()->attr.sequence)
1334 for (c = gfc_current_block ()->components; c; c = c->next)
1336 if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
1339 ("Component %s of SEQUENCE type declared at %C does not "
1340 "have the SEQUENCE attribute", c->ts.derived->name);
1349 /* Parse an ENUM. */
1358 int seen_enumerator = 0;
1362 push_state (&s, COMP_ENUM, gfc_new_block);
1366 while (compiling_enum)
1368 st = next_statement ();
1376 seen_enumerator = 1;
1377 accept_statement (st);
1382 if (!seen_enumerator)
1384 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1387 accept_statement (st);
1391 gfc_free_enum_history ();
1392 unexpected_statement (st);
1399 /* Parse an interface. We must be able to deal with the possibility
1400 of recursive interfaces. The parse_spec() subroutine is mutually
1401 recursive with parse_interface(). */
1403 static gfc_statement parse_spec (gfc_statement);
1406 parse_interface (void)
1408 gfc_compile_state new_state, current_state;
1409 gfc_symbol *prog_unit, *sym;
1410 gfc_interface_info save;
1411 gfc_state_data s1, s2;
1414 accept_statement (ST_INTERFACE);
1416 current_interface.ns = gfc_current_ns;
1417 save = current_interface;
1419 sym = (current_interface.type == INTERFACE_GENERIC
1420 || current_interface.type == INTERFACE_USER_OP) ? gfc_new_block : NULL;
1422 push_state (&s1, COMP_INTERFACE, sym);
1423 current_state = COMP_NONE;
1426 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1428 st = next_statement ();
1435 new_state = COMP_SUBROUTINE;
1436 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1437 gfc_new_block->formal, NULL);
1441 new_state = COMP_FUNCTION;
1442 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1443 gfc_new_block->formal, NULL);
1446 case ST_MODULE_PROC: /* The module procedure matcher makes
1447 sure the context is correct. */
1448 accept_statement (st);
1449 gfc_free_namespace (gfc_current_ns);
1452 case ST_END_INTERFACE:
1453 gfc_free_namespace (gfc_current_ns);
1454 gfc_current_ns = current_interface.ns;
1458 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1459 gfc_ascii_statement (st));
1460 reject_statement ();
1461 gfc_free_namespace (gfc_current_ns);
1466 /* Make sure that a generic interface has only subroutines or
1467 functions and that the generic name has the right attribute. */
1468 if (current_interface.type == INTERFACE_GENERIC)
1470 if (current_state == COMP_NONE)
1472 if (new_state == COMP_FUNCTION)
1473 gfc_add_function (&sym->attr, sym->name, NULL);
1474 else if (new_state == COMP_SUBROUTINE)
1475 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1477 current_state = new_state;
1481 if (new_state != current_state)
1483 if (new_state == COMP_SUBROUTINE)
1485 ("SUBROUTINE at %C does not belong in a generic function "
1488 if (new_state == COMP_FUNCTION)
1490 ("FUNCTION at %C does not belong in a generic subroutine "
1496 push_state (&s2, new_state, gfc_new_block);
1497 accept_statement (st);
1498 prog_unit = gfc_new_block;
1499 prog_unit->formal_ns = gfc_current_ns;
1502 /* Read data declaration statements. */
1503 st = parse_spec (ST_NONE);
1505 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1507 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1508 gfc_ascii_statement (st));
1509 reject_statement ();
1513 current_interface = save;
1514 gfc_add_interface (prog_unit);
1524 /* Parse a set of specification statements. Returns the statement
1525 that doesn't fit. */
1527 static gfc_statement
1528 parse_spec (gfc_statement st)
1532 verify_st_order (&ss, ST_NONE);
1534 st = next_statement ();
1544 case ST_DATA: /* Not allowed in interfaces */
1545 if (gfc_current_state () == COMP_INTERFACE)
1551 case ST_IMPLICIT_NONE:
1556 case ST_DERIVED_DECL:
1558 if (verify_st_order (&ss, st) == FAILURE)
1560 reject_statement ();
1561 st = next_statement ();
1571 case ST_DERIVED_DECL:
1577 if (gfc_current_state () != COMP_MODULE)
1579 gfc_error ("%s statement must appear in a MODULE",
1580 gfc_ascii_statement (st));
1584 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1586 gfc_error ("%s statement at %C follows another accessibility "
1587 "specification", gfc_ascii_statement (st));
1591 gfc_current_ns->default_access = (st == ST_PUBLIC)
1592 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1600 accept_statement (st);
1601 st = next_statement ();
1605 accept_statement (st);
1607 st = next_statement ();
1618 /* Parse a WHERE block, (not a simple WHERE statement). */
1621 parse_where_block (void)
1623 int seen_empty_else;
1628 accept_statement (ST_WHERE_BLOCK);
1629 top = gfc_state_stack->tail;
1631 push_state (&s, COMP_WHERE, gfc_new_block);
1633 d = add_statement ();
1634 d->expr = top->expr;
1640 seen_empty_else = 0;
1644 st = next_statement ();
1650 case ST_WHERE_BLOCK:
1651 parse_where_block ();
1656 accept_statement (st);
1660 if (seen_empty_else)
1663 ("ELSEWHERE statement at %C follows previous unmasked "
1668 if (new_st.expr == NULL)
1669 seen_empty_else = 1;
1671 d = new_level (gfc_state_stack->head);
1673 d->expr = new_st.expr;
1675 accept_statement (st);
1680 accept_statement (st);
1684 gfc_error ("Unexpected %s statement in WHERE block at %C",
1685 gfc_ascii_statement (st));
1686 reject_statement ();
1691 while (st != ST_END_WHERE);
1697 /* Parse a FORALL block (not a simple FORALL statement). */
1700 parse_forall_block (void)
1706 accept_statement (ST_FORALL_BLOCK);
1707 top = gfc_state_stack->tail;
1709 push_state (&s, COMP_FORALL, gfc_new_block);
1711 d = add_statement ();
1712 d->op = EXEC_FORALL;
1717 st = next_statement ();
1722 case ST_POINTER_ASSIGNMENT:
1725 accept_statement (st);
1728 case ST_WHERE_BLOCK:
1729 parse_where_block ();
1732 case ST_FORALL_BLOCK:
1733 parse_forall_block ();
1737 accept_statement (st);
1744 gfc_error ("Unexpected %s statement in FORALL block at %C",
1745 gfc_ascii_statement (st));
1747 reject_statement ();
1751 while (st != ST_END_FORALL);
1757 static gfc_statement parse_executable (gfc_statement);
1759 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
1762 parse_if_block (void)
1771 accept_statement (ST_IF_BLOCK);
1773 top = gfc_state_stack->tail;
1774 push_state (&s, COMP_IF, gfc_new_block);
1776 new_st.op = EXEC_IF;
1777 d = add_statement ();
1779 d->expr = top->expr;
1785 st = parse_executable (ST_NONE);
1796 ("ELSE IF statement at %C cannot follow ELSE statement at %L",
1799 reject_statement ();
1803 d = new_level (gfc_state_stack->head);
1805 d->expr = new_st.expr;
1807 accept_statement (st);
1814 gfc_error ("Duplicate ELSE statements at %L and %C",
1816 reject_statement ();
1821 else_locus = gfc_current_locus;
1823 d = new_level (gfc_state_stack->head);
1826 accept_statement (st);
1834 unexpected_statement (st);
1838 while (st != ST_ENDIF);
1841 accept_statement (st);
1845 /* Parse a SELECT block. */
1848 parse_select_block (void)
1854 accept_statement (ST_SELECT_CASE);
1856 cp = gfc_state_stack->tail;
1857 push_state (&s, COMP_SELECT, gfc_new_block);
1859 /* Make sure that the next statement is a CASE or END SELECT. */
1862 st = next_statement ();
1865 if (st == ST_END_SELECT)
1867 /* Empty SELECT CASE is OK. */
1868 accept_statement (st);
1876 ("Expected a CASE or END SELECT statement following SELECT CASE "
1879 reject_statement ();
1882 /* At this point, we're got a nonempty select block. */
1883 cp = new_level (cp);
1886 accept_statement (st);
1890 st = parse_executable (ST_NONE);
1897 cp = new_level (gfc_state_stack->head);
1899 gfc_clear_new_st ();
1901 accept_statement (st);
1907 /* Can't have an executable statement because of
1908 parse_executable(). */
1910 unexpected_statement (st);
1914 while (st != ST_END_SELECT);
1917 accept_statement (st);
1921 /* Given a symbol, make sure it is not an iteration variable for a DO
1922 statement. This subroutine is called when the symbol is seen in a
1923 context that causes it to become redefined. If the symbol is an
1924 iterator, we generate an error message and return nonzero. */
1927 gfc_check_do_variable (gfc_symtree *st)
1931 for (s=gfc_state_stack; s; s = s->previous)
1932 if (s->do_variable == st)
1934 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
1935 "loop beginning at %L", st->name, &s->head->loc);
1943 /* Checks to see if the current statement label closes an enddo.
1944 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
1945 an error) if it incorrectly closes an ENDDO. */
1948 check_do_closure (void)
1952 if (gfc_statement_label == NULL)
1955 for (p = gfc_state_stack; p; p = p->previous)
1956 if (p->state == COMP_DO)
1960 return 0; /* No loops to close */
1962 if (p->ext.end_do_label == gfc_statement_label)
1965 if (p == gfc_state_stack)
1969 ("End of nonblock DO statement at %C is within another block");
1973 /* At this point, the label doesn't terminate the innermost loop.
1974 Make sure it doesn't terminate another one. */
1975 for (; p; p = p->previous)
1976 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
1978 gfc_error ("End of nonblock DO statement at %C is interwoven "
1979 "with another DO loop");
1987 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
1988 handled inside of parse_executable(), because they aren't really
1992 parse_do_block (void)
1999 s.ext.end_do_label = new_st.label;
2001 if (new_st.ext.iterator != NULL)
2002 stree = new_st.ext.iterator->var->symtree;
2006 accept_statement (ST_DO);
2008 top = gfc_state_stack->tail;
2009 push_state (&s, COMP_DO, gfc_new_block);
2011 s.do_variable = stree;
2013 top->block = new_level (top);
2014 top->block->op = EXEC_DO;
2017 st = parse_executable (ST_NONE);
2025 if (s.ext.end_do_label != NULL
2026 && s.ext.end_do_label != gfc_statement_label)
2028 ("Statement label in ENDDO at %C doesn't match DO label");
2030 if (gfc_statement_label != NULL)
2032 new_st.op = EXEC_NOP;
2037 case ST_IMPLIED_ENDDO:
2041 unexpected_statement (st);
2046 accept_statement (st);
2050 /* Accept a series of executable statements. We return the first
2051 statement that doesn't fit to the caller. Any block statements are
2052 passed on to the correct handler, which usually passes the buck
2055 static gfc_statement
2056 parse_executable (gfc_statement st)
2061 st = next_statement ();
2063 for (;; st = next_statement ())
2066 close_flag = check_do_closure ();
2071 case ST_END_PROGRAM:
2074 case ST_END_FUNCTION:
2078 case ST_END_SUBROUTINE:
2083 case ST_SELECT_CASE:
2085 ("%s statement at %C cannot terminate a non-block DO loop",
2086 gfc_ascii_statement (st));
2102 accept_statement (st);
2103 if (close_flag == 1)
2104 return ST_IMPLIED_ENDDO;
2111 case ST_SELECT_CASE:
2112 parse_select_block ();
2117 if (check_do_closure () == 1)
2118 return ST_IMPLIED_ENDDO;
2121 case ST_WHERE_BLOCK:
2122 parse_where_block ();
2125 case ST_FORALL_BLOCK:
2126 parse_forall_block ();
2140 /* Parse a series of contained program units. */
2142 static void parse_progunit (gfc_statement);
2145 /* Fix the symbols for sibling functions. These are incorrectly added to
2146 the child namespace as the parser didn't know about this procedure. */
2149 gfc_fixup_sibling_symbols (gfc_symbol * sym, gfc_namespace * siblings)
2153 gfc_symbol *old_sym;
2155 sym->attr.referenced = 1;
2156 for (ns = siblings; ns; ns = ns->sibling)
2158 gfc_find_sym_tree (sym->name, ns, 0, &st);
2162 old_sym = st->n.sym;
2163 if ((old_sym->attr.flavor == FL_PROCEDURE
2164 || old_sym->ts.type == BT_UNKNOWN)
2165 && old_sym->ns == ns
2166 && ! old_sym->attr.contained)
2168 /* Replace it with the symbol from the parent namespace. */
2172 /* Free the old (local) symbol. */
2174 if (old_sym->refs == 0)
2175 gfc_free_symbol (old_sym);
2178 /* Do the same for any contained procedures. */
2179 gfc_fixup_sibling_symbols (sym, ns->contained);
2184 parse_contained (int module)
2186 gfc_namespace *ns, *parent_ns;
2187 gfc_state_data s1, s2;
2192 push_state (&s1, COMP_CONTAINS, NULL);
2193 parent_ns = gfc_current_ns;
2197 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2199 gfc_current_ns->sibling = parent_ns->contained;
2200 parent_ns->contained = gfc_current_ns;
2202 st = next_statement ();
2211 accept_statement (st);
2214 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2217 /* For internal procedures, create/update the symbol in the
2218 parent namespace. */
2222 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2224 ("Contained procedure '%s' at %C is already ambiguous",
2225 gfc_new_block->name);
2228 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2229 &gfc_new_block->declared_at) ==
2232 if (st == ST_FUNCTION)
2233 gfc_add_function (&sym->attr, sym->name,
2234 &gfc_new_block->declared_at);
2236 gfc_add_subroutine (&sym->attr, sym->name,
2237 &gfc_new_block->declared_at);
2241 gfc_commit_symbols ();
2244 sym = gfc_new_block;
2246 /* Mark this as a contained function, so it isn't replaced
2247 by other module functions. */
2248 sym->attr.contained = 1;
2249 sym->attr.referenced = 1;
2251 parse_progunit (ST_NONE);
2253 /* Fix up any sibling functions that refer to this one. */
2254 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2255 /* Or refer to any of its alternate entry points. */
2256 for (el = gfc_current_ns->entries; el; el = el->next)
2257 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2259 gfc_current_ns->code = s2.head;
2260 gfc_current_ns = parent_ns;
2265 /* These statements are associated with the end of the host
2267 case ST_END_FUNCTION:
2269 case ST_END_PROGRAM:
2270 case ST_END_SUBROUTINE:
2271 accept_statement (st);
2275 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2276 gfc_ascii_statement (st));
2277 reject_statement ();
2281 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2282 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2284 /* The first namespace in the list is guaranteed to not have
2285 anything (worthwhile) in it. */
2287 gfc_current_ns = parent_ns;
2289 ns = gfc_current_ns->contained;
2290 gfc_current_ns->contained = ns->sibling;
2291 gfc_free_namespace (ns);
2297 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2300 parse_progunit (gfc_statement st)
2305 st = parse_spec (st);
2315 accept_statement (st);
2325 st = parse_executable (st);
2336 accept_statement (st);
2343 unexpected_statement (st);
2344 reject_statement ();
2345 st = next_statement ();
2351 for (p = gfc_state_stack; p; p = p->previous)
2352 if (p->state == COMP_CONTAINS)
2355 if (gfc_find_state (COMP_MODULE) == SUCCESS)
2360 gfc_error ("CONTAINS statement at %C is already in a contained "
2362 st = next_statement ();
2366 parse_contained (0);
2369 gfc_current_ns->code = gfc_state_stack->head;
2373 /* Come here to complain about a global symbol already in use as
2377 global_used (gfc_gsymbol *sym, locus *where)
2382 where = &gfc_current_locus;
2392 case GSYM_SUBROUTINE:
2393 name = "SUBROUTINE";
2398 case GSYM_BLOCK_DATA:
2399 name = "BLOCK DATA";
2405 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
2409 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
2410 gfc_new_block->name, where, name, &sym->where);
2414 /* Parse a block data program unit. */
2417 parse_block_data (void)
2420 static locus blank_locus;
2421 static int blank_block=0;
2424 gfc_current_ns->proc_name = gfc_new_block;
2425 gfc_current_ns->is_block_data = 1;
2427 if (gfc_new_block == NULL)
2430 gfc_error ("Blank BLOCK DATA at %C conflicts with "
2431 "prior BLOCK DATA at %L", &blank_locus);
2435 blank_locus = gfc_current_locus;
2440 s = gfc_get_gsymbol (gfc_new_block->name);
2441 if (s->type != GSYM_UNKNOWN)
2442 global_used(s, NULL);
2445 s->type = GSYM_BLOCK_DATA;
2446 s->where = gfc_current_locus;
2450 st = parse_spec (ST_NONE);
2452 while (st != ST_END_BLOCK_DATA)
2454 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
2455 gfc_ascii_statement (st));
2456 reject_statement ();
2457 st = next_statement ();
2462 /* Parse a module subprogram. */
2470 s = gfc_get_gsymbol (gfc_new_block->name);
2471 if (s->type != GSYM_UNKNOWN)
2472 global_used(s, NULL);
2475 s->type = GSYM_MODULE;
2476 s->where = gfc_current_locus;
2479 st = parse_spec (ST_NONE);
2488 parse_contained (1);
2492 accept_statement (st);
2496 gfc_error ("Unexpected %s statement in MODULE at %C",
2497 gfc_ascii_statement (st));
2499 reject_statement ();
2500 st = next_statement ();
2506 /* Add a procedure name to the global symbol table. */
2509 add_global_procedure (int sub)
2513 s = gfc_get_gsymbol(gfc_new_block->name);
2515 if (s->type != GSYM_UNKNOWN)
2516 global_used(s, NULL);
2519 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2520 s->where = gfc_current_locus;
2525 /* Add a program to the global symbol table. */
2528 add_global_program (void)
2532 if (gfc_new_block == NULL)
2534 s = gfc_get_gsymbol (gfc_new_block->name);
2536 if (s->type != GSYM_UNKNOWN)
2537 global_used(s, NULL);
2540 s->type = GSYM_PROGRAM;
2541 s->where = gfc_current_locus;
2546 /* Top level parser. */
2549 gfc_parse_file (void)
2551 int seen_program, errors_before, errors;
2552 gfc_state_data top, s;
2556 top.state = COMP_NONE;
2558 top.previous = NULL;
2559 top.head = top.tail = NULL;
2560 top.do_variable = NULL;
2562 gfc_state_stack = ⊤
2564 gfc_clear_new_st ();
2566 gfc_statement_label = NULL;
2568 if (setjmp (eof_buf))
2569 return FAILURE; /* Come here on unexpected EOF */
2573 /* Exit early for empty files. */
2579 st = next_statement ();
2588 goto duplicate_main;
2590 prog_locus = gfc_current_locus;
2592 push_state (&s, COMP_PROGRAM, gfc_new_block);
2593 accept_statement (st);
2594 add_global_program ();
2595 parse_progunit (ST_NONE);
2599 add_global_procedure (1);
2600 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
2601 accept_statement (st);
2602 parse_progunit (ST_NONE);
2606 add_global_procedure (0);
2607 push_state (&s, COMP_FUNCTION, gfc_new_block);
2608 accept_statement (st);
2609 parse_progunit (ST_NONE);
2613 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
2614 accept_statement (st);
2615 parse_block_data ();
2619 push_state (&s, COMP_MODULE, gfc_new_block);
2620 accept_statement (st);
2622 gfc_get_errors (NULL, &errors_before);
2626 /* Anything else starts a nameless main program block. */
2629 goto duplicate_main;
2631 prog_locus = gfc_current_locus;
2633 push_state (&s, COMP_PROGRAM, gfc_new_block);
2634 parse_progunit (st);
2638 gfc_current_ns->code = s.head;
2640 gfc_resolve (gfc_current_ns);
2642 /* Dump the parse tree if requested. */
2643 if (gfc_option.verbose)
2644 gfc_show_namespace (gfc_current_ns);
2646 gfc_get_errors (NULL, &errors);
2647 if (s.state == COMP_MODULE)
2649 gfc_dump_module (s.sym->name, errors_before == errors);
2650 if (errors == 0 && ! gfc_option.flag_no_backend)
2651 gfc_generate_module_code (gfc_current_ns);
2655 if (errors == 0 && ! gfc_option.flag_no_backend)
2656 gfc_generate_code (gfc_current_ns);
2667 /* If we see a duplicate main program, shut down. If the second
2668 instance is an implied main program, ie data decls or executable
2669 statements, we're in for lots of errors. */
2670 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
2671 reject_statement ();