2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 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 ();
304 decode_omp_directive (void)
313 gfc_clear_error (); /* Clear any pending errors. */
314 gfc_clear_warning (); /* Clear any pending warnings. */
318 gfc_error_now ("OpenMP directives at %C may not appear in PURE or ELEMENTAL procedures");
319 gfc_error_recovery ();
323 old_locus = gfc_current_locus;
325 /* General OpenMP directive matching: Instead of testing every possible
326 statement, we eliminate most possibilities by peeking at the
329 c = gfc_peek_char ();
334 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
337 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
340 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
343 match ("do", gfc_match_omp_do, ST_OMP_DO);
346 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
347 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
348 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
349 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
350 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
351 match ("end parallel sections", gfc_match_omp_eos,
352 ST_OMP_END_PARALLEL_SECTIONS);
353 match ("end parallel workshare", gfc_match_omp_eos,
354 ST_OMP_END_PARALLEL_WORKSHARE);
355 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
356 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
357 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
358 match ("end workshare", gfc_match_omp_end_nowait,
359 ST_OMP_END_WORKSHARE);
362 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
365 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
368 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
371 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
372 match ("parallel sections", gfc_match_omp_parallel_sections,
373 ST_OMP_PARALLEL_SECTIONS);
374 match ("parallel workshare", gfc_match_omp_parallel_workshare,
375 ST_OMP_PARALLEL_WORKSHARE);
376 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
379 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
380 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
381 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
384 match ("threadprivate", gfc_match_omp_threadprivate,
385 ST_OMP_THREADPRIVATE);
387 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
391 /* All else has failed, so give up. See if any of the matchers has
392 stored an error message of some sort. */
394 if (gfc_error_check () == 0)
395 gfc_error_now ("Unclassifiable OpenMP directive at %C");
399 gfc_error_recovery ();
407 /* Get the next statement in free form source. */
413 int c, d, cnt, at_bol;
415 at_bol = gfc_at_bol ();
416 gfc_gobble_whitespace ();
418 c = gfc_peek_char ();
422 /* Found a statement label? */
423 m = gfc_match_st_label (&gfc_statement_label);
425 d = gfc_peek_char ();
426 if (m != MATCH_YES || !gfc_is_whitespace (d))
428 gfc_match_small_literal_int (&c, &cnt);
431 gfc_error_now ("Too many digits in statement label at %C");
434 gfc_error_now ("Zero is not a valid statement label at %C");
437 c = gfc_next_char ();
440 if (!gfc_is_whitespace (c))
441 gfc_error_now ("Non-numeric character in statement label at %C");
447 label_locus = gfc_current_locus;
449 gfc_gobble_whitespace ();
451 if (at_bol && gfc_peek_char () == ';')
454 ("Semicolon at %C needs to be preceded by statement");
455 gfc_next_char (); /* Eat up the semicolon. */
459 if (gfc_match_eos () == MATCH_YES)
462 ("Ignoring statement label in empty statement at %C");
463 gfc_free_st_label (gfc_statement_label);
464 gfc_statement_label = NULL;
471 /* Comments have already been skipped by the time we get here,
472 except for OpenMP directives. */
473 if (gfc_option.flag_openmp)
477 c = gfc_next_char ();
478 for (i = 0; i < 5; i++, c = gfc_next_char ())
479 gcc_assert (c == "!$omp"[i]);
481 gcc_assert (c == ' ');
482 return decode_omp_directive ();
486 if (at_bol && c == ';')
488 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
489 gfc_next_char (); /* Eat up the semicolon. */
493 return decode_statement ();
497 /* Get the next statement in fixed-form source. */
502 int label, digit_flag, i;
507 return decode_statement ();
509 /* Skip past the current label field, parsing a statement label if
510 one is there. This is a weird number parser, since the number is
511 contained within five columns and can have any kind of embedded
512 spaces. We also check for characters that make the rest of the
518 for (i = 0; i < 5; i++)
520 c = gfc_next_char_literal (0);
537 label = label * 10 + c - '0';
538 label_locus = gfc_current_locus;
542 /* Comments have already been skipped by the time we get
543 here, except for OpenMP directives. */
545 if (gfc_option.flag_openmp)
547 for (i = 0; i < 5; i++, c = gfc_next_char_literal (0))
548 gcc_assert (TOLOWER (c) == "*$omp"[i]);
550 if (c != ' ' && c != '0')
552 gfc_buffer_error (0);
553 gfc_error ("Bad continuation line at %C");
557 return decode_omp_directive ();
561 /* Comments have already been skipped by the time we get
562 here so don't bother checking for them. */
565 gfc_buffer_error (0);
566 gfc_error ("Non-numeric character in statement label at %C");
574 gfc_warning_now ("Zero is not a valid statement label at %C");
577 /* We've found a valid statement label. */
578 gfc_statement_label = gfc_get_st_label (label);
582 /* Since this line starts a statement, it cannot be a continuation
583 of a previous statement. If we see something here besides a
584 space or zero, it must be a bad continuation line. */
586 c = gfc_next_char_literal (0);
590 if (c != ' ' && c != '0')
592 gfc_buffer_error (0);
593 gfc_error ("Bad continuation line at %C");
597 /* Now that we've taken care of the statement label columns, we have
598 to make sure that the first nonblank character is not a '!'. If
599 it is, the rest of the line is a comment. */
603 loc = gfc_current_locus;
604 c = gfc_next_char_literal (0);
606 while (gfc_is_whitespace (c));
610 gfc_current_locus = loc;
614 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
618 if (gfc_match_eos () == MATCH_YES)
621 /* At this point, we've got a nonblank statement to parse. */
622 return decode_statement ();
626 gfc_warning ("Ignoring statement label in empty statement at %C");
632 /* Return the next non-ST_NONE statement to the caller. We also worry
633 about including files and the ends of include files at this stage. */
636 next_statement (void)
640 gfc_new_block = NULL;
644 gfc_statement_label = NULL;
645 gfc_buffer_error (1);
649 if (gfc_option.warn_line_truncation
650 && gfc_current_locus.lb
651 && gfc_current_locus.lb->truncated)
652 gfc_warning_now ("Line truncated at %C");
657 gfc_skip_comments ();
666 (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
672 gfc_buffer_error (0);
675 check_statement_label (st);
681 /****************************** Parser ***********************************/
683 /* The parser subroutines are of type 'try' that fail if the file ends
686 /* Macros that expand to case-labels for various classes of
687 statements. Start with executable statements that directly do
690 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
691 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
692 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
693 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
694 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
695 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
696 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
697 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
700 /* Statements that mark other executable statements. */
702 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
703 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
704 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
705 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
706 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
707 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
709 /* Declaration statements */
711 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
712 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
713 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE
715 /* Block end statements. Errors associated with interchanging these
716 are detected in gfc_match_end(). */
718 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
719 case ST_END_PROGRAM: case ST_END_SUBROUTINE
722 /* Push a new state onto the stack. */
725 push_state (gfc_state_data * p, gfc_compile_state new_state, gfc_symbol * sym)
728 p->state = new_state;
729 p->previous = gfc_state_stack;
731 p->head = p->tail = NULL;
732 p->do_variable = NULL;
738 /* Pop the current state. */
744 gfc_state_stack = gfc_state_stack->previous;
748 /* Try to find the given state in the state stack. */
751 gfc_find_state (gfc_compile_state state)
755 for (p = gfc_state_stack; p; p = p->previous)
756 if (p->state == state)
759 return (p == NULL) ? FAILURE : SUCCESS;
763 /* Starts a new level in the statement list. */
766 new_level (gfc_code * q)
770 p = q->block = gfc_get_code ();
772 gfc_state_stack->head = gfc_state_stack->tail = p;
778 /* Add the current new_st code structure and adds it to the current
779 program unit. As a side-effect, it zeroes the new_st. */
789 p->loc = gfc_current_locus;
791 if (gfc_state_stack->head == NULL)
792 gfc_state_stack->head = p;
794 gfc_state_stack->tail->next = p;
796 while (p->next != NULL)
799 gfc_state_stack->tail = p;
807 /* Frees everything associated with the current statement. */
810 undo_new_statement (void)
812 gfc_free_statements (new_st.block);
813 gfc_free_statements (new_st.next);
814 gfc_free_statement (&new_st);
819 /* If the current statement has a statement label, make sure that it
820 is allowed to, or should have one. */
823 check_statement_label (gfc_statement st)
827 if (gfc_statement_label == NULL)
830 gfc_error ("FORMAT statement at %L does not have a statement label",
838 case ST_END_FUNCTION:
839 case ST_END_SUBROUTINE:
845 type = ST_LABEL_TARGET;
849 type = ST_LABEL_FORMAT;
852 /* Statement labels are not restricted from appearing on a
853 particular line. However, there are plenty of situations
854 where the resulting label can't be referenced. */
857 type = ST_LABEL_BAD_TARGET;
861 gfc_define_st_label (gfc_statement_label, type, &label_locus);
863 new_st.here = gfc_statement_label;
867 /* Figures out what the enclosing program unit is. This will be a
868 function, subroutine, program, block data or module. */
871 gfc_enclosing_unit (gfc_compile_state * result)
875 for (p = gfc_state_stack; p; p = p->previous)
876 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
877 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
878 || p->state == COMP_PROGRAM)
887 *result = COMP_PROGRAM;
892 /* Translate a statement enum to a string. */
895 gfc_ascii_statement (gfc_statement st)
901 case ST_ARITHMETIC_IF:
902 p = _("arithmetic IF");
908 p = _("attribute declaration");
938 p = _("data declaration");
946 case ST_DERIVED_DECL:
947 p = _("derived type declaration");
961 case ST_END_BLOCK_DATA:
962 p = "END BLOCK DATA";
973 case ST_END_FUNCTION:
979 case ST_END_INTERFACE:
991 case ST_END_SUBROUTINE:
992 p = "END SUBROUTINE";
1003 case ST_EQUIVALENCE:
1012 case ST_FORALL_BLOCK: /* Fall through */
1031 case ST_IMPLICIT_NONE:
1032 p = "IMPLICIT NONE";
1034 case ST_IMPLIED_ENDDO:
1035 p = _("implied END DO");
1058 case ST_MODULE_PROC:
1059 p = "MODULE PROCEDURE";
1094 case ST_WHERE_BLOCK: /* Fall through */
1102 p = _("assignment");
1104 case ST_POINTER_ASSIGNMENT:
1105 p = _("pointer assignment");
1107 case ST_SELECT_CASE:
1116 case ST_STATEMENT_FUNCTION:
1117 p = "STATEMENT FUNCTION";
1119 case ST_LABEL_ASSIGNMENT:
1120 p = "LABEL ASSIGNMENT";
1123 p = "ENUM DEFINITION";
1126 p = "ENUMERATOR DEFINITION";
1134 case ST_OMP_BARRIER:
1135 p = "!$OMP BARRIER";
1137 case ST_OMP_CRITICAL:
1138 p = "!$OMP CRITICAL";
1143 case ST_OMP_END_CRITICAL:
1144 p = "!$OMP END CRITICAL";
1149 case ST_OMP_END_MASTER:
1150 p = "!$OMP END MASTER";
1152 case ST_OMP_END_ORDERED:
1153 p = "!$OMP END ORDERED";
1155 case ST_OMP_END_PARALLEL:
1156 p = "!$OMP END PARALLEL";
1158 case ST_OMP_END_PARALLEL_DO:
1159 p = "!$OMP END PARALLEL DO";
1161 case ST_OMP_END_PARALLEL_SECTIONS:
1162 p = "!$OMP END PARALLEL SECTIONS";
1164 case ST_OMP_END_PARALLEL_WORKSHARE:
1165 p = "!$OMP END PARALLEL WORKSHARE";
1167 case ST_OMP_END_SECTIONS:
1168 p = "!$OMP END SECTIONS";
1170 case ST_OMP_END_SINGLE:
1171 p = "!$OMP END SINGLE";
1173 case ST_OMP_END_WORKSHARE:
1174 p = "!$OMP END WORKSHARE";
1182 case ST_OMP_ORDERED:
1183 p = "!$OMP ORDERED";
1185 case ST_OMP_PARALLEL:
1186 p = "!$OMP PARALLEL";
1188 case ST_OMP_PARALLEL_DO:
1189 p = "!$OMP PARALLEL DO";
1191 case ST_OMP_PARALLEL_SECTIONS:
1192 p = "!$OMP PARALLEL SECTIONS";
1194 case ST_OMP_PARALLEL_WORKSHARE:
1195 p = "!$OMP PARALLEL WORKSHARE";
1197 case ST_OMP_SECTIONS:
1198 p = "!$OMP SECTIONS";
1200 case ST_OMP_SECTION:
1201 p = "!$OMP SECTION";
1206 case ST_OMP_THREADPRIVATE:
1207 p = "!$OMP THREADPRIVATE";
1209 case ST_OMP_WORKSHARE:
1210 p = "!$OMP WORKSHARE";
1213 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1220 /* Create a symbol for the main program and assign it to ns->proc_name. */
1223 main_program_symbol (gfc_namespace * ns)
1225 gfc_symbol *main_program;
1226 symbol_attribute attr;
1228 gfc_get_symbol ("MAIN__", ns, &main_program);
1229 gfc_clear_attr (&attr);
1230 attr.flavor = FL_PROCEDURE;
1231 attr.proc = PROC_UNKNOWN;
1232 attr.subroutine = 1;
1233 attr.access = ACCESS_PUBLIC;
1234 attr.is_main_program = 1;
1235 main_program->attr = attr;
1236 main_program->declared_at = gfc_current_locus;
1237 ns->proc_name = main_program;
1238 gfc_commit_symbols ();
1242 /* Do whatever is necessary to accept the last statement. */
1245 accept_statement (gfc_statement st)
1254 case ST_IMPLICIT_NONE:
1255 gfc_set_implicit_none ();
1264 gfc_current_ns->proc_name = gfc_new_block;
1267 /* If the statement is the end of a block, lay down a special code
1268 that allows a branch to the end of the block from within the
1273 if (gfc_statement_label != NULL)
1275 new_st.op = EXEC_NOP;
1281 /* The end-of-program unit statements do not get the special
1282 marker and require a statement of some sort if they are a
1285 case ST_END_PROGRAM:
1286 case ST_END_FUNCTION:
1287 case ST_END_SUBROUTINE:
1288 if (gfc_statement_label != NULL)
1290 new_st.op = EXEC_RETURN;
1306 gfc_commit_symbols ();
1307 gfc_warning_check ();
1308 gfc_clear_new_st ();
1312 /* Undo anything tentative that has been built for the current
1316 reject_statement (void)
1318 gfc_new_block = NULL;
1319 gfc_undo_symbols ();
1320 gfc_clear_warning ();
1321 undo_new_statement ();
1325 /* Generic complaint about an out of order statement. We also do
1326 whatever is necessary to clean up. */
1329 unexpected_statement (gfc_statement st)
1332 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1334 reject_statement ();
1338 /* Given the next statement seen by the matcher, make sure that it is
1339 in proper order with the last. This subroutine is initialized by
1340 calling it with an argument of ST_NONE. If there is a problem, we
1341 issue an error and return FAILURE. Otherwise we return SUCCESS.
1343 Individual parsers need to verify that the statements seen are
1344 valid before calling here, ie ENTRY statements are not allowed in
1345 INTERFACE blocks. The following diagram is taken from the standard:
1347 +---------------------------------------+
1348 | program subroutine function module |
1349 +---------------------------------------+
1351 |---------------------------------------+
1353 | +-----------+------------------+
1354 | | parameter | implicit |
1355 | +-----------+------------------+
1356 | format | | derived type |
1357 | entry | parameter | interface |
1358 | | data | specification |
1359 | | | statement func |
1360 | +-----------+------------------+
1361 | | data | executable |
1362 +--------+-----------+------------------+
1364 +---------------------------------------+
1365 | internal module/subprogram |
1366 +---------------------------------------+
1368 +---------------------------------------+
1375 { ORDER_START, ORDER_USE, ORDER_IMPLICIT_NONE, ORDER_IMPLICIT,
1376 ORDER_SPEC, ORDER_EXEC
1379 gfc_statement last_statement;
1385 verify_st_order (st_state * p, gfc_statement st)
1391 p->state = ORDER_START;
1395 if (p->state > ORDER_USE)
1397 p->state = ORDER_USE;
1400 case ST_IMPLICIT_NONE:
1401 if (p->state > ORDER_IMPLICIT_NONE)
1404 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1405 statement disqualifies a USE but not an IMPLICIT NONE.
1406 Duplicate IMPLICIT NONEs are caught when the implicit types
1409 p->state = ORDER_IMPLICIT_NONE;
1413 if (p->state > ORDER_IMPLICIT)
1415 p->state = ORDER_IMPLICIT;
1420 if (p->state < ORDER_IMPLICIT_NONE)
1421 p->state = ORDER_IMPLICIT_NONE;
1425 if (p->state >= ORDER_EXEC)
1427 if (p->state < ORDER_IMPLICIT)
1428 p->state = ORDER_IMPLICIT;
1432 if (p->state < ORDER_SPEC)
1433 p->state = ORDER_SPEC;
1438 case ST_DERIVED_DECL:
1440 if (p->state >= ORDER_EXEC)
1442 if (p->state < ORDER_SPEC)
1443 p->state = ORDER_SPEC;
1448 if (p->state < ORDER_EXEC)
1449 p->state = ORDER_EXEC;
1454 ("Unexpected %s statement in verify_st_order() at %C",
1455 gfc_ascii_statement (st));
1458 /* All is well, record the statement in case we need it next time. */
1459 p->where = gfc_current_locus;
1460 p->last_statement = st;
1464 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1465 gfc_ascii_statement (st),
1466 gfc_ascii_statement (p->last_statement), &p->where);
1472 /* Handle an unexpected end of file. This is a show-stopper... */
1474 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1477 unexpected_eof (void)
1481 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1483 /* Memory cleanup. Move to "second to last". */
1484 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1487 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1490 longjmp (eof_buf, 1);
1494 /* Parse a derived type. */
1497 parse_derived (void)
1499 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1507 accept_statement (ST_DERIVED_DECL);
1508 push_state (&s, COMP_DERIVED, gfc_new_block);
1510 gfc_new_block->component_access = ACCESS_PUBLIC;
1517 while (compiling_type)
1519 st = next_statement ();
1526 accept_statement (st);
1533 if (!seen_component)
1535 gfc_error ("Derived type definition at %C has no components");
1539 accept_statement (ST_END_TYPE);
1543 if (gfc_find_state (COMP_MODULE) == FAILURE)
1546 ("PRIVATE statement in TYPE at %C must be inside a MODULE");
1553 gfc_error ("PRIVATE statement at %C must precede "
1554 "structure components");
1561 gfc_error ("Duplicate PRIVATE statement at %C");
1565 s.sym->component_access = ACCESS_PRIVATE;
1566 accept_statement (ST_PRIVATE);
1573 gfc_error ("SEQUENCE statement at %C must precede "
1574 "structure components");
1579 if (gfc_current_block ()->attr.sequence)
1580 gfc_warning ("SEQUENCE attribute at %C already specified in "
1585 gfc_error ("Duplicate SEQUENCE statement at %C");
1590 gfc_add_sequence (&gfc_current_block ()->attr,
1591 gfc_current_block ()->name, NULL);
1595 unexpected_statement (st);
1600 /* Look for allocatable components. */
1601 sym = gfc_current_block ();
1602 for (c = sym->components; c; c = c->next)
1604 if (c->allocatable || (c->ts.type == BT_DERIVED
1605 && c->ts.derived->attr.alloc_comp))
1607 sym->attr.alloc_comp = 1;
1617 /* Parse an ENUM. */
1626 int seen_enumerator = 0;
1630 push_state (&s, COMP_ENUM, gfc_new_block);
1634 while (compiling_enum)
1636 st = next_statement ();
1644 seen_enumerator = 1;
1645 accept_statement (st);
1650 if (!seen_enumerator)
1652 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1655 accept_statement (st);
1659 gfc_free_enum_history ();
1660 unexpected_statement (st);
1667 /* Parse an interface. We must be able to deal with the possibility
1668 of recursive interfaces. The parse_spec() subroutine is mutually
1669 recursive with parse_interface(). */
1671 static gfc_statement parse_spec (gfc_statement);
1674 parse_interface (void)
1676 gfc_compile_state new_state, current_state;
1677 gfc_symbol *prog_unit, *sym;
1678 gfc_interface_info save;
1679 gfc_state_data s1, s2;
1682 accept_statement (ST_INTERFACE);
1684 current_interface.ns = gfc_current_ns;
1685 save = current_interface;
1687 sym = (current_interface.type == INTERFACE_GENERIC
1688 || current_interface.type == INTERFACE_USER_OP) ? gfc_new_block : NULL;
1690 push_state (&s1, COMP_INTERFACE, sym);
1691 current_state = COMP_NONE;
1694 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1696 st = next_statement ();
1703 new_state = COMP_SUBROUTINE;
1704 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1705 gfc_new_block->formal, NULL);
1709 new_state = COMP_FUNCTION;
1710 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1711 gfc_new_block->formal, NULL);
1714 case ST_MODULE_PROC: /* The module procedure matcher makes
1715 sure the context is correct. */
1716 accept_statement (st);
1717 gfc_free_namespace (gfc_current_ns);
1720 case ST_END_INTERFACE:
1721 gfc_free_namespace (gfc_current_ns);
1722 gfc_current_ns = current_interface.ns;
1726 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1727 gfc_ascii_statement (st));
1728 reject_statement ();
1729 gfc_free_namespace (gfc_current_ns);
1734 /* Make sure that a generic interface has only subroutines or
1735 functions and that the generic name has the right attribute. */
1736 if (current_interface.type == INTERFACE_GENERIC)
1738 if (current_state == COMP_NONE)
1740 if (new_state == COMP_FUNCTION)
1741 gfc_add_function (&sym->attr, sym->name, NULL);
1742 else if (new_state == COMP_SUBROUTINE)
1743 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1745 current_state = new_state;
1749 if (new_state != current_state)
1751 if (new_state == COMP_SUBROUTINE)
1753 ("SUBROUTINE at %C does not belong in a generic function "
1756 if (new_state == COMP_FUNCTION)
1758 ("FUNCTION at %C does not belong in a generic subroutine "
1764 push_state (&s2, new_state, gfc_new_block);
1765 accept_statement (st);
1766 prog_unit = gfc_new_block;
1767 prog_unit->formal_ns = gfc_current_ns;
1770 /* Read data declaration statements. */
1771 st = parse_spec (ST_NONE);
1773 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1775 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1776 gfc_ascii_statement (st));
1777 reject_statement ();
1781 current_interface = save;
1782 gfc_add_interface (prog_unit);
1792 /* Parse a set of specification statements. Returns the statement
1793 that doesn't fit. */
1795 static gfc_statement
1796 parse_spec (gfc_statement st)
1800 verify_st_order (&ss, ST_NONE);
1802 st = next_statement ();
1812 case ST_DATA: /* Not allowed in interfaces */
1813 if (gfc_current_state () == COMP_INTERFACE)
1819 case ST_IMPLICIT_NONE:
1824 case ST_DERIVED_DECL:
1826 if (verify_st_order (&ss, st) == FAILURE)
1828 reject_statement ();
1829 st = next_statement ();
1839 case ST_DERIVED_DECL:
1845 if (gfc_current_state () != COMP_MODULE)
1847 gfc_error ("%s statement must appear in a MODULE",
1848 gfc_ascii_statement (st));
1852 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1854 gfc_error ("%s statement at %C follows another accessibility "
1855 "specification", gfc_ascii_statement (st));
1859 gfc_current_ns->default_access = (st == ST_PUBLIC)
1860 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1868 accept_statement (st);
1869 st = next_statement ();
1873 accept_statement (st);
1875 st = next_statement ();
1886 /* Parse a WHERE block, (not a simple WHERE statement). */
1889 parse_where_block (void)
1891 int seen_empty_else;
1896 accept_statement (ST_WHERE_BLOCK);
1897 top = gfc_state_stack->tail;
1899 push_state (&s, COMP_WHERE, gfc_new_block);
1901 d = add_statement ();
1902 d->expr = top->expr;
1908 seen_empty_else = 0;
1912 st = next_statement ();
1918 case ST_WHERE_BLOCK:
1919 parse_where_block ();
1924 accept_statement (st);
1928 if (seen_empty_else)
1931 ("ELSEWHERE statement at %C follows previous unmasked "
1936 if (new_st.expr == NULL)
1937 seen_empty_else = 1;
1939 d = new_level (gfc_state_stack->head);
1941 d->expr = new_st.expr;
1943 accept_statement (st);
1948 accept_statement (st);
1952 gfc_error ("Unexpected %s statement in WHERE block at %C",
1953 gfc_ascii_statement (st));
1954 reject_statement ();
1959 while (st != ST_END_WHERE);
1965 /* Parse a FORALL block (not a simple FORALL statement). */
1968 parse_forall_block (void)
1974 accept_statement (ST_FORALL_BLOCK);
1975 top = gfc_state_stack->tail;
1977 push_state (&s, COMP_FORALL, gfc_new_block);
1979 d = add_statement ();
1980 d->op = EXEC_FORALL;
1985 st = next_statement ();
1990 case ST_POINTER_ASSIGNMENT:
1993 accept_statement (st);
1996 case ST_WHERE_BLOCK:
1997 parse_where_block ();
2000 case ST_FORALL_BLOCK:
2001 parse_forall_block ();
2005 accept_statement (st);
2012 gfc_error ("Unexpected %s statement in FORALL block at %C",
2013 gfc_ascii_statement (st));
2015 reject_statement ();
2019 while (st != ST_END_FORALL);
2025 static gfc_statement parse_executable (gfc_statement);
2027 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2030 parse_if_block (void)
2039 accept_statement (ST_IF_BLOCK);
2041 top = gfc_state_stack->tail;
2042 push_state (&s, COMP_IF, gfc_new_block);
2044 new_st.op = EXEC_IF;
2045 d = add_statement ();
2047 d->expr = top->expr;
2053 st = parse_executable (ST_NONE);
2064 ("ELSE IF statement at %C cannot follow ELSE statement at %L",
2067 reject_statement ();
2071 d = new_level (gfc_state_stack->head);
2073 d->expr = new_st.expr;
2075 accept_statement (st);
2082 gfc_error ("Duplicate ELSE statements at %L and %C",
2084 reject_statement ();
2089 else_locus = gfc_current_locus;
2091 d = new_level (gfc_state_stack->head);
2094 accept_statement (st);
2102 unexpected_statement (st);
2106 while (st != ST_ENDIF);
2109 accept_statement (st);
2113 /* Parse a SELECT block. */
2116 parse_select_block (void)
2122 accept_statement (ST_SELECT_CASE);
2124 cp = gfc_state_stack->tail;
2125 push_state (&s, COMP_SELECT, gfc_new_block);
2127 /* Make sure that the next statement is a CASE or END SELECT. */
2130 st = next_statement ();
2133 if (st == ST_END_SELECT)
2135 /* Empty SELECT CASE is OK. */
2136 accept_statement (st);
2144 ("Expected a CASE or END SELECT statement following SELECT CASE "
2147 reject_statement ();
2150 /* At this point, we're got a nonempty select block. */
2151 cp = new_level (cp);
2154 accept_statement (st);
2158 st = parse_executable (ST_NONE);
2165 cp = new_level (gfc_state_stack->head);
2167 gfc_clear_new_st ();
2169 accept_statement (st);
2175 /* Can't have an executable statement because of
2176 parse_executable(). */
2178 unexpected_statement (st);
2182 while (st != ST_END_SELECT);
2185 accept_statement (st);
2189 /* Given a symbol, make sure it is not an iteration variable for a DO
2190 statement. This subroutine is called when the symbol is seen in a
2191 context that causes it to become redefined. If the symbol is an
2192 iterator, we generate an error message and return nonzero. */
2195 gfc_check_do_variable (gfc_symtree *st)
2199 for (s=gfc_state_stack; s; s = s->previous)
2200 if (s->do_variable == st)
2202 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2203 "loop beginning at %L", st->name, &s->head->loc);
2211 /* Checks to see if the current statement label closes an enddo.
2212 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2213 an error) if it incorrectly closes an ENDDO. */
2216 check_do_closure (void)
2220 if (gfc_statement_label == NULL)
2223 for (p = gfc_state_stack; p; p = p->previous)
2224 if (p->state == COMP_DO)
2228 return 0; /* No loops to close */
2230 if (p->ext.end_do_label == gfc_statement_label)
2233 if (p == gfc_state_stack)
2237 ("End of nonblock DO statement at %C is within another block");
2241 /* At this point, the label doesn't terminate the innermost loop.
2242 Make sure it doesn't terminate another one. */
2243 for (; p; p = p->previous)
2244 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
2246 gfc_error ("End of nonblock DO statement at %C is interwoven "
2247 "with another DO loop");
2255 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2256 handled inside of parse_executable(), because they aren't really
2260 parse_do_block (void)
2267 s.ext.end_do_label = new_st.label;
2269 if (new_st.ext.iterator != NULL)
2270 stree = new_st.ext.iterator->var->symtree;
2274 accept_statement (ST_DO);
2276 top = gfc_state_stack->tail;
2277 push_state (&s, COMP_DO, gfc_new_block);
2279 s.do_variable = stree;
2281 top->block = new_level (top);
2282 top->block->op = EXEC_DO;
2285 st = parse_executable (ST_NONE);
2293 if (s.ext.end_do_label != NULL
2294 && s.ext.end_do_label != gfc_statement_label)
2296 ("Statement label in ENDDO at %C doesn't match DO label");
2298 if (gfc_statement_label != NULL)
2300 new_st.op = EXEC_NOP;
2305 case ST_IMPLIED_ENDDO:
2306 /* If the do-stmt of this DO construct has a do-construct-name,
2307 the corresponding end-do must be an end-do-stmt (with a matching
2308 name, but in that case we must have seen ST_ENDDO first).
2309 We only complain about this in pedantic mode. */
2310 if (gfc_current_block () != NULL)
2312 ("named block DO at %L requires matching ENDDO name",
2313 &gfc_current_block()->declared_at);
2318 unexpected_statement (st);
2323 accept_statement (st);
2327 /* Parse the statements of OpenMP do/parallel do. */
2329 static gfc_statement
2330 parse_omp_do (gfc_statement omp_st)
2336 accept_statement (omp_st);
2338 cp = gfc_state_stack->tail;
2339 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2340 np = new_level (cp);
2346 st = next_statement ();
2349 else if (st == ST_DO)
2352 unexpected_statement (st);
2356 if (gfc_statement_label != NULL
2357 && gfc_state_stack->previous != NULL
2358 && gfc_state_stack->previous->state == COMP_DO
2359 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
2367 there should be no !$OMP END DO. */
2369 return ST_IMPLIED_ENDDO;
2372 check_do_closure ();
2375 st = next_statement ();
2376 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
2378 if (new_st.op == EXEC_OMP_END_NOWAIT)
2379 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2381 gcc_assert (new_st.op == EXEC_NOP);
2382 gfc_clear_new_st ();
2383 gfc_commit_symbols ();
2384 gfc_warning_check ();
2385 st = next_statement ();
2391 /* Parse the statements of OpenMP atomic directive. */
2394 parse_omp_atomic (void)
2400 accept_statement (ST_OMP_ATOMIC);
2402 cp = gfc_state_stack->tail;
2403 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2404 np = new_level (cp);
2410 st = next_statement ();
2413 else if (st == ST_ASSIGNMENT)
2416 unexpected_statement (st);
2419 accept_statement (st);
2425 /* Parse the statements of an OpenMP structured block. */
2428 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
2430 gfc_statement st, omp_end_st;
2434 accept_statement (omp_st);
2436 cp = gfc_state_stack->tail;
2437 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2438 np = new_level (cp);
2444 case ST_OMP_PARALLEL:
2445 omp_end_st = ST_OMP_END_PARALLEL;
2447 case ST_OMP_PARALLEL_SECTIONS:
2448 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
2450 case ST_OMP_SECTIONS:
2451 omp_end_st = ST_OMP_END_SECTIONS;
2453 case ST_OMP_ORDERED:
2454 omp_end_st = ST_OMP_END_ORDERED;
2456 case ST_OMP_CRITICAL:
2457 omp_end_st = ST_OMP_END_CRITICAL;
2460 omp_end_st = ST_OMP_END_MASTER;
2463 omp_end_st = ST_OMP_END_SINGLE;
2465 case ST_OMP_WORKSHARE:
2466 omp_end_st = ST_OMP_END_WORKSHARE;
2468 case ST_OMP_PARALLEL_WORKSHARE:
2469 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
2477 if (workshare_stmts_only)
2479 /* Inside of !$omp workshare, only
2482 where statements and constructs
2483 forall statements and constructs
2487 are allowed. For !$omp critical these
2488 restrictions apply recursively. */
2491 st = next_statement ();
2502 accept_statement (st);
2505 case ST_WHERE_BLOCK:
2506 parse_where_block ();
2509 case ST_FORALL_BLOCK:
2510 parse_forall_block ();
2513 case ST_OMP_PARALLEL:
2514 case ST_OMP_PARALLEL_SECTIONS:
2515 parse_omp_structured_block (st, false);
2518 case ST_OMP_PARALLEL_WORKSHARE:
2519 case ST_OMP_CRITICAL:
2520 parse_omp_structured_block (st, true);
2523 case ST_OMP_PARALLEL_DO:
2524 st = parse_omp_do (st);
2528 parse_omp_atomic ();
2539 st = next_statement ();
2543 st = parse_executable (ST_NONE);
2546 else if (st == ST_OMP_SECTION
2547 && (omp_st == ST_OMP_SECTIONS
2548 || omp_st == ST_OMP_PARALLEL_SECTIONS))
2550 np = new_level (np);
2554 else if (st != omp_end_st)
2555 unexpected_statement (st);
2557 while (st != omp_end_st);
2561 case EXEC_OMP_END_NOWAIT:
2562 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2564 case EXEC_OMP_CRITICAL:
2565 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
2566 || (new_st.ext.omp_name != NULL
2567 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
2568 gfc_error ("Name after !$omp critical and !$omp end critical does"
2569 " not match at %C");
2570 gfc_free ((char *) new_st.ext.omp_name);
2572 case EXEC_OMP_END_SINGLE:
2573 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
2574 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
2575 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
2576 gfc_free_omp_clauses (new_st.ext.omp_clauses);
2584 gfc_clear_new_st ();
2585 gfc_commit_symbols ();
2586 gfc_warning_check ();
2591 /* Accept a series of executable statements. We return the first
2592 statement that doesn't fit to the caller. Any block statements are
2593 passed on to the correct handler, which usually passes the buck
2596 static gfc_statement
2597 parse_executable (gfc_statement st)
2602 st = next_statement ();
2606 close_flag = check_do_closure ();
2611 case ST_END_PROGRAM:
2614 case ST_END_FUNCTION:
2618 case ST_END_SUBROUTINE:
2623 case ST_SELECT_CASE:
2625 ("%s statement at %C cannot terminate a non-block DO loop",
2626 gfc_ascii_statement (st));
2642 accept_statement (st);
2643 if (close_flag == 1)
2644 return ST_IMPLIED_ENDDO;
2651 case ST_SELECT_CASE:
2652 parse_select_block ();
2657 if (check_do_closure () == 1)
2658 return ST_IMPLIED_ENDDO;
2661 case ST_WHERE_BLOCK:
2662 parse_where_block ();
2665 case ST_FORALL_BLOCK:
2666 parse_forall_block ();
2669 case ST_OMP_PARALLEL:
2670 case ST_OMP_PARALLEL_SECTIONS:
2671 case ST_OMP_SECTIONS:
2672 case ST_OMP_ORDERED:
2673 case ST_OMP_CRITICAL:
2676 parse_omp_structured_block (st, false);
2679 case ST_OMP_WORKSHARE:
2680 case ST_OMP_PARALLEL_WORKSHARE:
2681 parse_omp_structured_block (st, true);
2685 case ST_OMP_PARALLEL_DO:
2686 st = parse_omp_do (st);
2687 if (st == ST_IMPLIED_ENDDO)
2692 parse_omp_atomic ();
2699 st = next_statement ();
2704 /* Parse a series of contained program units. */
2706 static void parse_progunit (gfc_statement);
2709 /* Fix the symbols for sibling functions. These are incorrectly added to
2710 the child namespace as the parser didn't know about this procedure. */
2713 gfc_fixup_sibling_symbols (gfc_symbol * sym, gfc_namespace * siblings)
2717 gfc_symbol *old_sym;
2719 sym->attr.referenced = 1;
2720 for (ns = siblings; ns; ns = ns->sibling)
2722 gfc_find_sym_tree (sym->name, ns, 0, &st);
2724 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
2727 old_sym = st->n.sym;
2728 if ((old_sym->attr.flavor == FL_PROCEDURE
2729 || old_sym->ts.type == BT_UNKNOWN)
2730 && old_sym->ns == ns
2731 && ! old_sym->attr.contained)
2733 /* Replace it with the symbol from the parent namespace. */
2737 /* Free the old (local) symbol. */
2739 if (old_sym->refs == 0)
2740 gfc_free_symbol (old_sym);
2743 /* Do the same for any contained procedures. */
2744 gfc_fixup_sibling_symbols (sym, ns->contained);
2749 parse_contained (int module)
2751 gfc_namespace *ns, *parent_ns;
2752 gfc_state_data s1, s2;
2757 push_state (&s1, COMP_CONTAINS, NULL);
2758 parent_ns = gfc_current_ns;
2762 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2764 gfc_current_ns->sibling = parent_ns->contained;
2765 parent_ns->contained = gfc_current_ns;
2767 st = next_statement ();
2776 accept_statement (st);
2779 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2782 /* For internal procedures, create/update the symbol in the
2783 parent namespace. */
2787 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2789 ("Contained procedure '%s' at %C is already ambiguous",
2790 gfc_new_block->name);
2793 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2794 &gfc_new_block->declared_at) ==
2797 if (st == ST_FUNCTION)
2798 gfc_add_function (&sym->attr, sym->name,
2799 &gfc_new_block->declared_at);
2801 gfc_add_subroutine (&sym->attr, sym->name,
2802 &gfc_new_block->declared_at);
2806 gfc_commit_symbols ();
2809 sym = gfc_new_block;
2811 /* Mark this as a contained function, so it isn't replaced
2812 by other module functions. */
2813 sym->attr.contained = 1;
2814 sym->attr.referenced = 1;
2816 parse_progunit (ST_NONE);
2818 /* Fix up any sibling functions that refer to this one. */
2819 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2820 /* Or refer to any of its alternate entry points. */
2821 for (el = gfc_current_ns->entries; el; el = el->next)
2822 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2824 gfc_current_ns->code = s2.head;
2825 gfc_current_ns = parent_ns;
2830 /* These statements are associated with the end of the host
2832 case ST_END_FUNCTION:
2834 case ST_END_PROGRAM:
2835 case ST_END_SUBROUTINE:
2836 accept_statement (st);
2840 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2841 gfc_ascii_statement (st));
2842 reject_statement ();
2846 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2847 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2849 /* The first namespace in the list is guaranteed to not have
2850 anything (worthwhile) in it. */
2852 gfc_current_ns = parent_ns;
2854 ns = gfc_current_ns->contained;
2855 gfc_current_ns->contained = ns->sibling;
2856 gfc_free_namespace (ns);
2862 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2865 parse_progunit (gfc_statement st)
2870 st = parse_spec (st);
2880 accept_statement (st);
2890 st = parse_executable (st);
2901 accept_statement (st);
2908 unexpected_statement (st);
2909 reject_statement ();
2910 st = next_statement ();
2916 for (p = gfc_state_stack; p; p = p->previous)
2917 if (p->state == COMP_CONTAINS)
2920 if (gfc_find_state (COMP_MODULE) == SUCCESS)
2925 gfc_error ("CONTAINS statement at %C is already in a contained "
2927 st = next_statement ();
2931 parse_contained (0);
2934 gfc_current_ns->code = gfc_state_stack->head;
2938 /* Come here to complain about a global symbol already in use as
2942 global_used (gfc_gsymbol *sym, locus *where)
2947 where = &gfc_current_locus;
2957 case GSYM_SUBROUTINE:
2958 name = "SUBROUTINE";
2963 case GSYM_BLOCK_DATA:
2964 name = "BLOCK DATA";
2970 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
2974 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
2975 sym->name, where, name, &sym->where);
2979 /* Parse a block data program unit. */
2982 parse_block_data (void)
2985 static locus blank_locus;
2986 static int blank_block=0;
2989 gfc_current_ns->proc_name = gfc_new_block;
2990 gfc_current_ns->is_block_data = 1;
2992 if (gfc_new_block == NULL)
2995 gfc_error ("Blank BLOCK DATA at %C conflicts with "
2996 "prior BLOCK DATA at %L", &blank_locus);
3000 blank_locus = gfc_current_locus;
3005 s = gfc_get_gsymbol (gfc_new_block->name);
3006 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
3007 global_used(s, NULL);
3010 s->type = GSYM_BLOCK_DATA;
3011 s->where = gfc_current_locus;
3016 st = parse_spec (ST_NONE);
3018 while (st != ST_END_BLOCK_DATA)
3020 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3021 gfc_ascii_statement (st));
3022 reject_statement ();
3023 st = next_statement ();
3028 /* Parse a module subprogram. */
3036 s = gfc_get_gsymbol (gfc_new_block->name);
3037 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
3038 global_used(s, NULL);
3041 s->type = GSYM_MODULE;
3042 s->where = gfc_current_locus;
3046 st = parse_spec (ST_NONE);
3055 parse_contained (1);
3059 accept_statement (st);
3063 gfc_error ("Unexpected %s statement in MODULE at %C",
3064 gfc_ascii_statement (st));
3066 reject_statement ();
3067 st = next_statement ();
3073 /* Add a procedure name to the global symbol table. */
3076 add_global_procedure (int sub)
3080 s = gfc_get_gsymbol(gfc_new_block->name);
3083 || (s->type != GSYM_UNKNOWN && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3084 global_used(s, NULL);
3087 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3088 s->where = gfc_current_locus;
3094 /* Add a program to the global symbol table. */
3097 add_global_program (void)
3101 if (gfc_new_block == NULL)
3103 s = gfc_get_gsymbol (gfc_new_block->name);
3105 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
3106 global_used(s, NULL);
3109 s->type = GSYM_PROGRAM;
3110 s->where = gfc_current_locus;
3116 /* Top level parser. */
3119 gfc_parse_file (void)
3121 int seen_program, errors_before, errors;
3122 gfc_state_data top, s;
3126 top.state = COMP_NONE;
3128 top.previous = NULL;
3129 top.head = top.tail = NULL;
3130 top.do_variable = NULL;
3132 gfc_state_stack = ⊤
3134 gfc_clear_new_st ();
3136 gfc_statement_label = NULL;
3138 if (setjmp (eof_buf))
3139 return FAILURE; /* Come here on unexpected EOF */
3143 /* Exit early for empty files. */
3149 st = next_statement ();
3158 goto duplicate_main;
3160 prog_locus = gfc_current_locus;
3162 push_state (&s, COMP_PROGRAM, gfc_new_block);
3163 main_program_symbol(gfc_current_ns);
3164 accept_statement (st);
3165 add_global_program ();
3166 parse_progunit (ST_NONE);
3170 add_global_procedure (1);
3171 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
3172 accept_statement (st);
3173 parse_progunit (ST_NONE);
3177 add_global_procedure (0);
3178 push_state (&s, COMP_FUNCTION, gfc_new_block);
3179 accept_statement (st);
3180 parse_progunit (ST_NONE);
3184 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
3185 accept_statement (st);
3186 parse_block_data ();
3190 push_state (&s, COMP_MODULE, gfc_new_block);
3191 accept_statement (st);
3193 gfc_get_errors (NULL, &errors_before);
3197 /* Anything else starts a nameless main program block. */
3200 goto duplicate_main;
3202 prog_locus = gfc_current_locus;
3204 push_state (&s, COMP_PROGRAM, gfc_new_block);
3205 main_program_symbol(gfc_current_ns);
3206 parse_progunit (st);
3210 gfc_current_ns->code = s.head;
3212 gfc_resolve (gfc_current_ns);
3214 /* Dump the parse tree if requested. */
3215 if (gfc_option.verbose)
3216 gfc_show_namespace (gfc_current_ns);
3218 gfc_get_errors (NULL, &errors);
3219 if (s.state == COMP_MODULE)
3221 gfc_dump_module (s.sym->name, errors_before == errors);
3222 if (errors == 0 && ! gfc_option.flag_no_backend)
3223 gfc_generate_module_code (gfc_current_ns);
3227 if (errors == 0 && ! gfc_option.flag_no_backend)
3228 gfc_generate_code (gfc_current_ns);
3239 /* If we see a duplicate main program, shut down. If the second
3240 instance is an implied main program, ie data decls or executable
3241 statements, we're in for lots of errors. */
3242 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
3243 reject_statement ();