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 ("volatile", gfc_match_volatile, ST_ATTR_DECL);
290 match ("write", gfc_match_write, ST_WRITE);
294 /* All else has failed, so give up. See if any of the matchers has
295 stored an error message of some sort. */
297 if (gfc_error_check () == 0)
298 gfc_error_now ("Unclassifiable statement at %C");
302 gfc_error_recovery ();
308 decode_omp_directive (void)
317 gfc_clear_error (); /* Clear any pending errors. */
318 gfc_clear_warning (); /* Clear any pending warnings. */
322 gfc_error_now ("OpenMP directives at %C may not appear in PURE or ELEMENTAL procedures");
323 gfc_error_recovery ();
327 old_locus = gfc_current_locus;
329 /* General OpenMP directive matching: Instead of testing every possible
330 statement, we eliminate most possibilities by peeking at the
333 c = gfc_peek_char ();
338 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
341 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
344 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
347 match ("do", gfc_match_omp_do, ST_OMP_DO);
350 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
351 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
352 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
353 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
354 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
355 match ("end parallel sections", gfc_match_omp_eos,
356 ST_OMP_END_PARALLEL_SECTIONS);
357 match ("end parallel workshare", gfc_match_omp_eos,
358 ST_OMP_END_PARALLEL_WORKSHARE);
359 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
360 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
361 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
362 match ("end workshare", gfc_match_omp_end_nowait,
363 ST_OMP_END_WORKSHARE);
366 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
369 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
372 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
375 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
376 match ("parallel sections", gfc_match_omp_parallel_sections,
377 ST_OMP_PARALLEL_SECTIONS);
378 match ("parallel workshare", gfc_match_omp_parallel_workshare,
379 ST_OMP_PARALLEL_WORKSHARE);
380 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
383 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
384 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
385 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
388 match ("threadprivate", gfc_match_omp_threadprivate,
389 ST_OMP_THREADPRIVATE);
391 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
395 /* All else has failed, so give up. See if any of the matchers has
396 stored an error message of some sort. */
398 if (gfc_error_check () == 0)
399 gfc_error_now ("Unclassifiable OpenMP directive at %C");
403 gfc_error_recovery ();
411 /* Get the next statement in free form source. */
417 int c, d, cnt, at_bol;
419 at_bol = gfc_at_bol ();
420 gfc_gobble_whitespace ();
422 c = gfc_peek_char ();
426 /* Found a statement label? */
427 m = gfc_match_st_label (&gfc_statement_label);
429 d = gfc_peek_char ();
430 if (m != MATCH_YES || !gfc_is_whitespace (d))
432 gfc_match_small_literal_int (&c, &cnt);
435 gfc_error_now ("Too many digits in statement label at %C");
438 gfc_error_now ("Zero is not a valid statement label at %C");
441 c = gfc_next_char ();
444 if (!gfc_is_whitespace (c))
445 gfc_error_now ("Non-numeric character in statement label at %C");
451 label_locus = gfc_current_locus;
453 gfc_gobble_whitespace ();
455 if (at_bol && gfc_peek_char () == ';')
458 ("Semicolon at %C needs to be preceded by statement");
459 gfc_next_char (); /* Eat up the semicolon. */
463 if (gfc_match_eos () == MATCH_YES)
466 ("Ignoring statement label in empty statement at %C");
467 gfc_free_st_label (gfc_statement_label);
468 gfc_statement_label = NULL;
475 /* Comments have already been skipped by the time we get here,
476 except for OpenMP directives. */
477 if (gfc_option.flag_openmp)
481 c = gfc_next_char ();
482 for (i = 0; i < 5; i++, c = gfc_next_char ())
483 gcc_assert (c == "!$omp"[i]);
485 gcc_assert (c == ' ');
486 return decode_omp_directive ();
490 if (at_bol && c == ';')
492 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
493 gfc_next_char (); /* Eat up the semicolon. */
497 return decode_statement ();
501 /* Get the next statement in fixed-form source. */
506 int label, digit_flag, i;
511 return decode_statement ();
513 /* Skip past the current label field, parsing a statement label if
514 one is there. This is a weird number parser, since the number is
515 contained within five columns and can have any kind of embedded
516 spaces. We also check for characters that make the rest of the
522 for (i = 0; i < 5; i++)
524 c = gfc_next_char_literal (0);
541 label = label * 10 + c - '0';
542 label_locus = gfc_current_locus;
546 /* Comments have already been skipped by the time we get
547 here, except for OpenMP directives. */
549 if (gfc_option.flag_openmp)
551 for (i = 0; i < 5; i++, c = gfc_next_char_literal (0))
552 gcc_assert (TOLOWER (c) == "*$omp"[i]);
554 if (c != ' ' && c != '0')
556 gfc_buffer_error (0);
557 gfc_error ("Bad continuation line at %C");
561 return decode_omp_directive ();
565 /* Comments have already been skipped by the time we get
566 here so don't bother checking for them. */
569 gfc_buffer_error (0);
570 gfc_error ("Non-numeric character in statement label at %C");
578 gfc_warning_now ("Zero is not a valid statement label at %C");
581 /* We've found a valid statement label. */
582 gfc_statement_label = gfc_get_st_label (label);
586 /* Since this line starts a statement, it cannot be a continuation
587 of a previous statement. If we see something here besides a
588 space or zero, it must be a bad continuation line. */
590 c = gfc_next_char_literal (0);
594 if (c != ' ' && c != '0')
596 gfc_buffer_error (0);
597 gfc_error ("Bad continuation line at %C");
601 /* Now that we've taken care of the statement label columns, we have
602 to make sure that the first nonblank character is not a '!'. If
603 it is, the rest of the line is a comment. */
607 loc = gfc_current_locus;
608 c = gfc_next_char_literal (0);
610 while (gfc_is_whitespace (c));
614 gfc_current_locus = loc;
618 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
622 if (gfc_match_eos () == MATCH_YES)
625 /* At this point, we've got a nonblank statement to parse. */
626 return decode_statement ();
630 gfc_warning ("Ignoring statement label in empty statement at %C");
636 /* Return the next non-ST_NONE statement to the caller. We also worry
637 about including files and the ends of include files at this stage. */
640 next_statement (void)
644 gfc_new_block = NULL;
648 gfc_statement_label = NULL;
649 gfc_buffer_error (1);
653 if (gfc_option.warn_line_truncation
654 && gfc_current_locus.lb
655 && gfc_current_locus.lb->truncated)
656 gfc_warning_now ("Line truncated at %C");
661 gfc_skip_comments ();
670 (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
676 gfc_buffer_error (0);
679 check_statement_label (st);
685 /****************************** Parser ***********************************/
687 /* The parser subroutines are of type 'try' that fail if the file ends
690 /* Macros that expand to case-labels for various classes of
691 statements. Start with executable statements that directly do
694 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
695 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
696 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
697 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
698 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
699 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
700 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
701 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
704 /* Statements that mark other executable statements. */
706 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
707 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
708 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
709 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
710 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
711 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
713 /* Declaration statements */
715 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
716 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
717 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE
719 /* Block end statements. Errors associated with interchanging these
720 are detected in gfc_match_end(). */
722 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
723 case ST_END_PROGRAM: case ST_END_SUBROUTINE
726 /* Push a new state onto the stack. */
729 push_state (gfc_state_data * p, gfc_compile_state new_state, gfc_symbol * sym)
732 p->state = new_state;
733 p->previous = gfc_state_stack;
735 p->head = p->tail = NULL;
736 p->do_variable = NULL;
742 /* Pop the current state. */
748 gfc_state_stack = gfc_state_stack->previous;
752 /* Try to find the given state in the state stack. */
755 gfc_find_state (gfc_compile_state state)
759 for (p = gfc_state_stack; p; p = p->previous)
760 if (p->state == state)
763 return (p == NULL) ? FAILURE : SUCCESS;
767 /* Starts a new level in the statement list. */
770 new_level (gfc_code * q)
774 p = q->block = gfc_get_code ();
776 gfc_state_stack->head = gfc_state_stack->tail = p;
782 /* Add the current new_st code structure and adds it to the current
783 program unit. As a side-effect, it zeroes the new_st. */
793 p->loc = gfc_current_locus;
795 if (gfc_state_stack->head == NULL)
796 gfc_state_stack->head = p;
798 gfc_state_stack->tail->next = p;
800 while (p->next != NULL)
803 gfc_state_stack->tail = p;
811 /* Frees everything associated with the current statement. */
814 undo_new_statement (void)
816 gfc_free_statements (new_st.block);
817 gfc_free_statements (new_st.next);
818 gfc_free_statement (&new_st);
823 /* If the current statement has a statement label, make sure that it
824 is allowed to, or should have one. */
827 check_statement_label (gfc_statement st)
831 if (gfc_statement_label == NULL)
834 gfc_error ("FORMAT statement at %L does not have a statement label",
842 case ST_END_FUNCTION:
843 case ST_END_SUBROUTINE:
849 type = ST_LABEL_TARGET;
853 type = ST_LABEL_FORMAT;
856 /* Statement labels are not restricted from appearing on a
857 particular line. However, there are plenty of situations
858 where the resulting label can't be referenced. */
861 type = ST_LABEL_BAD_TARGET;
865 gfc_define_st_label (gfc_statement_label, type, &label_locus);
867 new_st.here = gfc_statement_label;
871 /* Figures out what the enclosing program unit is. This will be a
872 function, subroutine, program, block data or module. */
875 gfc_enclosing_unit (gfc_compile_state * result)
879 for (p = gfc_state_stack; p; p = p->previous)
880 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
881 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
882 || p->state == COMP_PROGRAM)
891 *result = COMP_PROGRAM;
896 /* Translate a statement enum to a string. */
899 gfc_ascii_statement (gfc_statement st)
905 case ST_ARITHMETIC_IF:
906 p = _("arithmetic IF");
912 p = _("attribute declaration");
942 p = _("data declaration");
950 case ST_DERIVED_DECL:
951 p = _("derived type declaration");
965 case ST_END_BLOCK_DATA:
966 p = "END BLOCK DATA";
977 case ST_END_FUNCTION:
983 case ST_END_INTERFACE:
995 case ST_END_SUBROUTINE:
996 p = "END SUBROUTINE";
1007 case ST_EQUIVALENCE:
1016 case ST_FORALL_BLOCK: /* Fall through */
1035 case ST_IMPLICIT_NONE:
1036 p = "IMPLICIT NONE";
1038 case ST_IMPLIED_ENDDO:
1039 p = _("implied END DO");
1062 case ST_MODULE_PROC:
1063 p = "MODULE PROCEDURE";
1098 case ST_WHERE_BLOCK: /* Fall through */
1106 p = _("assignment");
1108 case ST_POINTER_ASSIGNMENT:
1109 p = _("pointer assignment");
1111 case ST_SELECT_CASE:
1120 case ST_STATEMENT_FUNCTION:
1121 p = "STATEMENT FUNCTION";
1123 case ST_LABEL_ASSIGNMENT:
1124 p = "LABEL ASSIGNMENT";
1127 p = "ENUM DEFINITION";
1130 p = "ENUMERATOR DEFINITION";
1138 case ST_OMP_BARRIER:
1139 p = "!$OMP BARRIER";
1141 case ST_OMP_CRITICAL:
1142 p = "!$OMP CRITICAL";
1147 case ST_OMP_END_CRITICAL:
1148 p = "!$OMP END CRITICAL";
1153 case ST_OMP_END_MASTER:
1154 p = "!$OMP END MASTER";
1156 case ST_OMP_END_ORDERED:
1157 p = "!$OMP END ORDERED";
1159 case ST_OMP_END_PARALLEL:
1160 p = "!$OMP END PARALLEL";
1162 case ST_OMP_END_PARALLEL_DO:
1163 p = "!$OMP END PARALLEL DO";
1165 case ST_OMP_END_PARALLEL_SECTIONS:
1166 p = "!$OMP END PARALLEL SECTIONS";
1168 case ST_OMP_END_PARALLEL_WORKSHARE:
1169 p = "!$OMP END PARALLEL WORKSHARE";
1171 case ST_OMP_END_SECTIONS:
1172 p = "!$OMP END SECTIONS";
1174 case ST_OMP_END_SINGLE:
1175 p = "!$OMP END SINGLE";
1177 case ST_OMP_END_WORKSHARE:
1178 p = "!$OMP END WORKSHARE";
1186 case ST_OMP_ORDERED:
1187 p = "!$OMP ORDERED";
1189 case ST_OMP_PARALLEL:
1190 p = "!$OMP PARALLEL";
1192 case ST_OMP_PARALLEL_DO:
1193 p = "!$OMP PARALLEL DO";
1195 case ST_OMP_PARALLEL_SECTIONS:
1196 p = "!$OMP PARALLEL SECTIONS";
1198 case ST_OMP_PARALLEL_WORKSHARE:
1199 p = "!$OMP PARALLEL WORKSHARE";
1201 case ST_OMP_SECTIONS:
1202 p = "!$OMP SECTIONS";
1204 case ST_OMP_SECTION:
1205 p = "!$OMP SECTION";
1210 case ST_OMP_THREADPRIVATE:
1211 p = "!$OMP THREADPRIVATE";
1213 case ST_OMP_WORKSHARE:
1214 p = "!$OMP WORKSHARE";
1217 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1224 /* Create a symbol for the main program and assign it to ns->proc_name. */
1227 main_program_symbol (gfc_namespace * ns)
1229 gfc_symbol *main_program;
1230 symbol_attribute attr;
1232 gfc_get_symbol ("MAIN__", ns, &main_program);
1233 gfc_clear_attr (&attr);
1234 attr.flavor = FL_PROCEDURE;
1235 attr.proc = PROC_UNKNOWN;
1236 attr.subroutine = 1;
1237 attr.access = ACCESS_PUBLIC;
1238 attr.is_main_program = 1;
1239 main_program->attr = attr;
1240 main_program->declared_at = gfc_current_locus;
1241 ns->proc_name = main_program;
1242 gfc_commit_symbols ();
1246 /* Do whatever is necessary to accept the last statement. */
1249 accept_statement (gfc_statement st)
1258 case ST_IMPLICIT_NONE:
1259 gfc_set_implicit_none ();
1268 gfc_current_ns->proc_name = gfc_new_block;
1271 /* If the statement is the end of a block, lay down a special code
1272 that allows a branch to the end of the block from within the
1277 if (gfc_statement_label != NULL)
1279 new_st.op = EXEC_NOP;
1285 /* The end-of-program unit statements do not get the special
1286 marker and require a statement of some sort if they are a
1289 case ST_END_PROGRAM:
1290 case ST_END_FUNCTION:
1291 case ST_END_SUBROUTINE:
1292 if (gfc_statement_label != NULL)
1294 new_st.op = EXEC_RETURN;
1310 gfc_commit_symbols ();
1311 gfc_warning_check ();
1312 gfc_clear_new_st ();
1316 /* Undo anything tentative that has been built for the current
1320 reject_statement (void)
1322 gfc_new_block = NULL;
1323 gfc_undo_symbols ();
1324 gfc_clear_warning ();
1325 undo_new_statement ();
1329 /* Generic complaint about an out of order statement. We also do
1330 whatever is necessary to clean up. */
1333 unexpected_statement (gfc_statement st)
1336 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1338 reject_statement ();
1342 /* Given the next statement seen by the matcher, make sure that it is
1343 in proper order with the last. This subroutine is initialized by
1344 calling it with an argument of ST_NONE. If there is a problem, we
1345 issue an error and return FAILURE. Otherwise we return SUCCESS.
1347 Individual parsers need to verify that the statements seen are
1348 valid before calling here, ie ENTRY statements are not allowed in
1349 INTERFACE blocks. The following diagram is taken from the standard:
1351 +---------------------------------------+
1352 | program subroutine function module |
1353 +---------------------------------------+
1355 |---------------------------------------+
1357 | +-----------+------------------+
1358 | | parameter | implicit |
1359 | +-----------+------------------+
1360 | format | | derived type |
1361 | entry | parameter | interface |
1362 | | data | specification |
1363 | | | statement func |
1364 | +-----------+------------------+
1365 | | data | executable |
1366 +--------+-----------+------------------+
1368 +---------------------------------------+
1369 | internal module/subprogram |
1370 +---------------------------------------+
1372 +---------------------------------------+
1379 { ORDER_START, ORDER_USE, ORDER_IMPLICIT_NONE, ORDER_IMPLICIT,
1380 ORDER_SPEC, ORDER_EXEC
1383 gfc_statement last_statement;
1389 verify_st_order (st_state * p, gfc_statement st)
1395 p->state = ORDER_START;
1399 if (p->state > ORDER_USE)
1401 p->state = ORDER_USE;
1404 case ST_IMPLICIT_NONE:
1405 if (p->state > ORDER_IMPLICIT_NONE)
1408 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1409 statement disqualifies a USE but not an IMPLICIT NONE.
1410 Duplicate IMPLICIT NONEs are caught when the implicit types
1413 p->state = ORDER_IMPLICIT_NONE;
1417 if (p->state > ORDER_IMPLICIT)
1419 p->state = ORDER_IMPLICIT;
1424 if (p->state < ORDER_IMPLICIT_NONE)
1425 p->state = ORDER_IMPLICIT_NONE;
1429 if (p->state >= ORDER_EXEC)
1431 if (p->state < ORDER_IMPLICIT)
1432 p->state = ORDER_IMPLICIT;
1436 if (p->state < ORDER_SPEC)
1437 p->state = ORDER_SPEC;
1442 case ST_DERIVED_DECL:
1444 if (p->state >= ORDER_EXEC)
1446 if (p->state < ORDER_SPEC)
1447 p->state = ORDER_SPEC;
1452 if (p->state < ORDER_EXEC)
1453 p->state = ORDER_EXEC;
1458 ("Unexpected %s statement in verify_st_order() at %C",
1459 gfc_ascii_statement (st));
1462 /* All is well, record the statement in case we need it next time. */
1463 p->where = gfc_current_locus;
1464 p->last_statement = st;
1468 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1469 gfc_ascii_statement (st),
1470 gfc_ascii_statement (p->last_statement), &p->where);
1476 /* Handle an unexpected end of file. This is a show-stopper... */
1478 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1481 unexpected_eof (void)
1485 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1487 /* Memory cleanup. Move to "second to last". */
1488 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1491 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1494 longjmp (eof_buf, 1);
1498 /* Parse a derived type. */
1501 parse_derived (void)
1503 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1511 accept_statement (ST_DERIVED_DECL);
1512 push_state (&s, COMP_DERIVED, gfc_new_block);
1514 gfc_new_block->component_access = ACCESS_PUBLIC;
1521 while (compiling_type)
1523 st = next_statement ();
1530 accept_statement (st);
1537 if (!seen_component)
1539 gfc_error ("Derived type definition at %C has no components");
1543 accept_statement (ST_END_TYPE);
1547 if (gfc_find_state (COMP_MODULE) == FAILURE)
1550 ("PRIVATE statement in TYPE at %C must be inside a MODULE");
1557 gfc_error ("PRIVATE statement at %C must precede "
1558 "structure components");
1565 gfc_error ("Duplicate PRIVATE statement at %C");
1569 s.sym->component_access = ACCESS_PRIVATE;
1570 accept_statement (ST_PRIVATE);
1577 gfc_error ("SEQUENCE statement at %C must precede "
1578 "structure components");
1583 if (gfc_current_block ()->attr.sequence)
1584 gfc_warning ("SEQUENCE attribute at %C already specified in "
1589 gfc_error ("Duplicate SEQUENCE statement at %C");
1594 gfc_add_sequence (&gfc_current_block ()->attr,
1595 gfc_current_block ()->name, NULL);
1599 unexpected_statement (st);
1604 /* Look for allocatable components. */
1605 sym = gfc_current_block ();
1606 for (c = sym->components; c; c = c->next)
1608 if (c->allocatable || (c->ts.type == BT_DERIVED
1609 && c->ts.derived->attr.alloc_comp))
1611 sym->attr.alloc_comp = 1;
1621 /* Parse an ENUM. */
1630 int seen_enumerator = 0;
1634 push_state (&s, COMP_ENUM, gfc_new_block);
1638 while (compiling_enum)
1640 st = next_statement ();
1648 seen_enumerator = 1;
1649 accept_statement (st);
1654 if (!seen_enumerator)
1656 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1659 accept_statement (st);
1663 gfc_free_enum_history ();
1664 unexpected_statement (st);
1671 /* Parse an interface. We must be able to deal with the possibility
1672 of recursive interfaces. The parse_spec() subroutine is mutually
1673 recursive with parse_interface(). */
1675 static gfc_statement parse_spec (gfc_statement);
1678 parse_interface (void)
1680 gfc_compile_state new_state, current_state;
1681 gfc_symbol *prog_unit, *sym;
1682 gfc_interface_info save;
1683 gfc_state_data s1, s2;
1686 accept_statement (ST_INTERFACE);
1688 current_interface.ns = gfc_current_ns;
1689 save = current_interface;
1691 sym = (current_interface.type == INTERFACE_GENERIC
1692 || current_interface.type == INTERFACE_USER_OP) ? gfc_new_block : NULL;
1694 push_state (&s1, COMP_INTERFACE, sym);
1695 current_state = COMP_NONE;
1698 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1700 st = next_statement ();
1707 new_state = COMP_SUBROUTINE;
1708 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1709 gfc_new_block->formal, NULL);
1713 new_state = COMP_FUNCTION;
1714 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1715 gfc_new_block->formal, NULL);
1718 case ST_MODULE_PROC: /* The module procedure matcher makes
1719 sure the context is correct. */
1720 accept_statement (st);
1721 gfc_free_namespace (gfc_current_ns);
1724 case ST_END_INTERFACE:
1725 gfc_free_namespace (gfc_current_ns);
1726 gfc_current_ns = current_interface.ns;
1730 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1731 gfc_ascii_statement (st));
1732 reject_statement ();
1733 gfc_free_namespace (gfc_current_ns);
1738 /* Make sure that a generic interface has only subroutines or
1739 functions and that the generic name has the right attribute. */
1740 if (current_interface.type == INTERFACE_GENERIC)
1742 if (current_state == COMP_NONE)
1744 if (new_state == COMP_FUNCTION)
1745 gfc_add_function (&sym->attr, sym->name, NULL);
1746 else if (new_state == COMP_SUBROUTINE)
1747 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1749 current_state = new_state;
1753 if (new_state != current_state)
1755 if (new_state == COMP_SUBROUTINE)
1757 ("SUBROUTINE at %C does not belong in a generic function "
1760 if (new_state == COMP_FUNCTION)
1762 ("FUNCTION at %C does not belong in a generic subroutine "
1768 push_state (&s2, new_state, gfc_new_block);
1769 accept_statement (st);
1770 prog_unit = gfc_new_block;
1771 prog_unit->formal_ns = gfc_current_ns;
1774 /* Read data declaration statements. */
1775 st = parse_spec (ST_NONE);
1777 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1779 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1780 gfc_ascii_statement (st));
1781 reject_statement ();
1785 current_interface = save;
1786 gfc_add_interface (prog_unit);
1796 /* Parse a set of specification statements. Returns the statement
1797 that doesn't fit. */
1799 static gfc_statement
1800 parse_spec (gfc_statement st)
1804 verify_st_order (&ss, ST_NONE);
1806 st = next_statement ();
1816 case ST_DATA: /* Not allowed in interfaces */
1817 if (gfc_current_state () == COMP_INTERFACE)
1823 case ST_IMPLICIT_NONE:
1828 case ST_DERIVED_DECL:
1830 if (verify_st_order (&ss, st) == FAILURE)
1832 reject_statement ();
1833 st = next_statement ();
1843 case ST_DERIVED_DECL:
1849 if (gfc_current_state () != COMP_MODULE)
1851 gfc_error ("%s statement must appear in a MODULE",
1852 gfc_ascii_statement (st));
1856 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1858 gfc_error ("%s statement at %C follows another accessibility "
1859 "specification", gfc_ascii_statement (st));
1863 gfc_current_ns->default_access = (st == ST_PUBLIC)
1864 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1872 accept_statement (st);
1873 st = next_statement ();
1877 accept_statement (st);
1879 st = next_statement ();
1890 /* Parse a WHERE block, (not a simple WHERE statement). */
1893 parse_where_block (void)
1895 int seen_empty_else;
1900 accept_statement (ST_WHERE_BLOCK);
1901 top = gfc_state_stack->tail;
1903 push_state (&s, COMP_WHERE, gfc_new_block);
1905 d = add_statement ();
1906 d->expr = top->expr;
1912 seen_empty_else = 0;
1916 st = next_statement ();
1922 case ST_WHERE_BLOCK:
1923 parse_where_block ();
1928 accept_statement (st);
1932 if (seen_empty_else)
1935 ("ELSEWHERE statement at %C follows previous unmasked "
1940 if (new_st.expr == NULL)
1941 seen_empty_else = 1;
1943 d = new_level (gfc_state_stack->head);
1945 d->expr = new_st.expr;
1947 accept_statement (st);
1952 accept_statement (st);
1956 gfc_error ("Unexpected %s statement in WHERE block at %C",
1957 gfc_ascii_statement (st));
1958 reject_statement ();
1963 while (st != ST_END_WHERE);
1969 /* Parse a FORALL block (not a simple FORALL statement). */
1972 parse_forall_block (void)
1978 accept_statement (ST_FORALL_BLOCK);
1979 top = gfc_state_stack->tail;
1981 push_state (&s, COMP_FORALL, gfc_new_block);
1983 d = add_statement ();
1984 d->op = EXEC_FORALL;
1989 st = next_statement ();
1994 case ST_POINTER_ASSIGNMENT:
1997 accept_statement (st);
2000 case ST_WHERE_BLOCK:
2001 parse_where_block ();
2004 case ST_FORALL_BLOCK:
2005 parse_forall_block ();
2009 accept_statement (st);
2016 gfc_error ("Unexpected %s statement in FORALL block at %C",
2017 gfc_ascii_statement (st));
2019 reject_statement ();
2023 while (st != ST_END_FORALL);
2029 static gfc_statement parse_executable (gfc_statement);
2031 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2034 parse_if_block (void)
2043 accept_statement (ST_IF_BLOCK);
2045 top = gfc_state_stack->tail;
2046 push_state (&s, COMP_IF, gfc_new_block);
2048 new_st.op = EXEC_IF;
2049 d = add_statement ();
2051 d->expr = top->expr;
2057 st = parse_executable (ST_NONE);
2068 ("ELSE IF statement at %C cannot follow ELSE statement at %L",
2071 reject_statement ();
2075 d = new_level (gfc_state_stack->head);
2077 d->expr = new_st.expr;
2079 accept_statement (st);
2086 gfc_error ("Duplicate ELSE statements at %L and %C",
2088 reject_statement ();
2093 else_locus = gfc_current_locus;
2095 d = new_level (gfc_state_stack->head);
2098 accept_statement (st);
2106 unexpected_statement (st);
2110 while (st != ST_ENDIF);
2113 accept_statement (st);
2117 /* Parse a SELECT block. */
2120 parse_select_block (void)
2126 accept_statement (ST_SELECT_CASE);
2128 cp = gfc_state_stack->tail;
2129 push_state (&s, COMP_SELECT, gfc_new_block);
2131 /* Make sure that the next statement is a CASE or END SELECT. */
2134 st = next_statement ();
2137 if (st == ST_END_SELECT)
2139 /* Empty SELECT CASE is OK. */
2140 accept_statement (st);
2148 ("Expected a CASE or END SELECT statement following SELECT CASE "
2151 reject_statement ();
2154 /* At this point, we're got a nonempty select block. */
2155 cp = new_level (cp);
2158 accept_statement (st);
2162 st = parse_executable (ST_NONE);
2169 cp = new_level (gfc_state_stack->head);
2171 gfc_clear_new_st ();
2173 accept_statement (st);
2179 /* Can't have an executable statement because of
2180 parse_executable(). */
2182 unexpected_statement (st);
2186 while (st != ST_END_SELECT);
2189 accept_statement (st);
2193 /* Given a symbol, make sure it is not an iteration variable for a DO
2194 statement. This subroutine is called when the symbol is seen in a
2195 context that causes it to become redefined. If the symbol is an
2196 iterator, we generate an error message and return nonzero. */
2199 gfc_check_do_variable (gfc_symtree *st)
2203 for (s=gfc_state_stack; s; s = s->previous)
2204 if (s->do_variable == st)
2206 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2207 "loop beginning at %L", st->name, &s->head->loc);
2215 /* Checks to see if the current statement label closes an enddo.
2216 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2217 an error) if it incorrectly closes an ENDDO. */
2220 check_do_closure (void)
2224 if (gfc_statement_label == NULL)
2227 for (p = gfc_state_stack; p; p = p->previous)
2228 if (p->state == COMP_DO)
2232 return 0; /* No loops to close */
2234 if (p->ext.end_do_label == gfc_statement_label)
2237 if (p == gfc_state_stack)
2241 ("End of nonblock DO statement at %C is within another block");
2245 /* At this point, the label doesn't terminate the innermost loop.
2246 Make sure it doesn't terminate another one. */
2247 for (; p; p = p->previous)
2248 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
2250 gfc_error ("End of nonblock DO statement at %C is interwoven "
2251 "with another DO loop");
2259 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2260 handled inside of parse_executable(), because they aren't really
2264 parse_do_block (void)
2271 s.ext.end_do_label = new_st.label;
2273 if (new_st.ext.iterator != NULL)
2274 stree = new_st.ext.iterator->var->symtree;
2278 accept_statement (ST_DO);
2280 top = gfc_state_stack->tail;
2281 push_state (&s, COMP_DO, gfc_new_block);
2283 s.do_variable = stree;
2285 top->block = new_level (top);
2286 top->block->op = EXEC_DO;
2289 st = parse_executable (ST_NONE);
2297 if (s.ext.end_do_label != NULL
2298 && s.ext.end_do_label != gfc_statement_label)
2300 ("Statement label in ENDDO at %C doesn't match DO label");
2302 if (gfc_statement_label != NULL)
2304 new_st.op = EXEC_NOP;
2309 case ST_IMPLIED_ENDDO:
2310 /* If the do-stmt of this DO construct has a do-construct-name,
2311 the corresponding end-do must be an end-do-stmt (with a matching
2312 name, but in that case we must have seen ST_ENDDO first).
2313 We only complain about this in pedantic mode. */
2314 if (gfc_current_block () != NULL)
2316 ("named block DO at %L requires matching ENDDO name",
2317 &gfc_current_block()->declared_at);
2322 unexpected_statement (st);
2327 accept_statement (st);
2331 /* Parse the statements of OpenMP do/parallel do. */
2333 static gfc_statement
2334 parse_omp_do (gfc_statement omp_st)
2340 accept_statement (omp_st);
2342 cp = gfc_state_stack->tail;
2343 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2344 np = new_level (cp);
2350 st = next_statement ();
2353 else if (st == ST_DO)
2356 unexpected_statement (st);
2360 if (gfc_statement_label != NULL
2361 && gfc_state_stack->previous != NULL
2362 && gfc_state_stack->previous->state == COMP_DO
2363 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
2371 there should be no !$OMP END DO. */
2373 return ST_IMPLIED_ENDDO;
2376 check_do_closure ();
2379 st = next_statement ();
2380 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
2382 if (new_st.op == EXEC_OMP_END_NOWAIT)
2383 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2385 gcc_assert (new_st.op == EXEC_NOP);
2386 gfc_clear_new_st ();
2387 gfc_commit_symbols ();
2388 gfc_warning_check ();
2389 st = next_statement ();
2395 /* Parse the statements of OpenMP atomic directive. */
2398 parse_omp_atomic (void)
2404 accept_statement (ST_OMP_ATOMIC);
2406 cp = gfc_state_stack->tail;
2407 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2408 np = new_level (cp);
2414 st = next_statement ();
2417 else if (st == ST_ASSIGNMENT)
2420 unexpected_statement (st);
2423 accept_statement (st);
2429 /* Parse the statements of an OpenMP structured block. */
2432 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
2434 gfc_statement st, omp_end_st;
2438 accept_statement (omp_st);
2440 cp = gfc_state_stack->tail;
2441 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2442 np = new_level (cp);
2448 case ST_OMP_PARALLEL:
2449 omp_end_st = ST_OMP_END_PARALLEL;
2451 case ST_OMP_PARALLEL_SECTIONS:
2452 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
2454 case ST_OMP_SECTIONS:
2455 omp_end_st = ST_OMP_END_SECTIONS;
2457 case ST_OMP_ORDERED:
2458 omp_end_st = ST_OMP_END_ORDERED;
2460 case ST_OMP_CRITICAL:
2461 omp_end_st = ST_OMP_END_CRITICAL;
2464 omp_end_st = ST_OMP_END_MASTER;
2467 omp_end_st = ST_OMP_END_SINGLE;
2469 case ST_OMP_WORKSHARE:
2470 omp_end_st = ST_OMP_END_WORKSHARE;
2472 case ST_OMP_PARALLEL_WORKSHARE:
2473 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
2481 if (workshare_stmts_only)
2483 /* Inside of !$omp workshare, only
2486 where statements and constructs
2487 forall statements and constructs
2491 are allowed. For !$omp critical these
2492 restrictions apply recursively. */
2495 st = next_statement ();
2506 accept_statement (st);
2509 case ST_WHERE_BLOCK:
2510 parse_where_block ();
2513 case ST_FORALL_BLOCK:
2514 parse_forall_block ();
2517 case ST_OMP_PARALLEL:
2518 case ST_OMP_PARALLEL_SECTIONS:
2519 parse_omp_structured_block (st, false);
2522 case ST_OMP_PARALLEL_WORKSHARE:
2523 case ST_OMP_CRITICAL:
2524 parse_omp_structured_block (st, true);
2527 case ST_OMP_PARALLEL_DO:
2528 st = parse_omp_do (st);
2532 parse_omp_atomic ();
2543 st = next_statement ();
2547 st = parse_executable (ST_NONE);
2550 else if (st == ST_OMP_SECTION
2551 && (omp_st == ST_OMP_SECTIONS
2552 || omp_st == ST_OMP_PARALLEL_SECTIONS))
2554 np = new_level (np);
2558 else if (st != omp_end_st)
2559 unexpected_statement (st);
2561 while (st != omp_end_st);
2565 case EXEC_OMP_END_NOWAIT:
2566 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2568 case EXEC_OMP_CRITICAL:
2569 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
2570 || (new_st.ext.omp_name != NULL
2571 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
2572 gfc_error ("Name after !$omp critical and !$omp end critical does"
2573 " not match at %C");
2574 gfc_free ((char *) new_st.ext.omp_name);
2576 case EXEC_OMP_END_SINGLE:
2577 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
2578 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
2579 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
2580 gfc_free_omp_clauses (new_st.ext.omp_clauses);
2588 gfc_clear_new_st ();
2589 gfc_commit_symbols ();
2590 gfc_warning_check ();
2595 /* Accept a series of executable statements. We return the first
2596 statement that doesn't fit to the caller. Any block statements are
2597 passed on to the correct handler, which usually passes the buck
2600 static gfc_statement
2601 parse_executable (gfc_statement st)
2606 st = next_statement ();
2610 close_flag = check_do_closure ();
2615 case ST_END_PROGRAM:
2618 case ST_END_FUNCTION:
2622 case ST_END_SUBROUTINE:
2627 case ST_SELECT_CASE:
2629 ("%s statement at %C cannot terminate a non-block DO loop",
2630 gfc_ascii_statement (st));
2646 accept_statement (st);
2647 if (close_flag == 1)
2648 return ST_IMPLIED_ENDDO;
2655 case ST_SELECT_CASE:
2656 parse_select_block ();
2661 if (check_do_closure () == 1)
2662 return ST_IMPLIED_ENDDO;
2665 case ST_WHERE_BLOCK:
2666 parse_where_block ();
2669 case ST_FORALL_BLOCK:
2670 parse_forall_block ();
2673 case ST_OMP_PARALLEL:
2674 case ST_OMP_PARALLEL_SECTIONS:
2675 case ST_OMP_SECTIONS:
2676 case ST_OMP_ORDERED:
2677 case ST_OMP_CRITICAL:
2680 parse_omp_structured_block (st, false);
2683 case ST_OMP_WORKSHARE:
2684 case ST_OMP_PARALLEL_WORKSHARE:
2685 parse_omp_structured_block (st, true);
2689 case ST_OMP_PARALLEL_DO:
2690 st = parse_omp_do (st);
2691 if (st == ST_IMPLIED_ENDDO)
2696 parse_omp_atomic ();
2703 st = next_statement ();
2708 /* Parse a series of contained program units. */
2710 static void parse_progunit (gfc_statement);
2713 /* Fix the symbols for sibling functions. These are incorrectly added to
2714 the child namespace as the parser didn't know about this procedure. */
2717 gfc_fixup_sibling_symbols (gfc_symbol * sym, gfc_namespace * siblings)
2721 gfc_symbol *old_sym;
2723 sym->attr.referenced = 1;
2724 for (ns = siblings; ns; ns = ns->sibling)
2726 gfc_find_sym_tree (sym->name, ns, 0, &st);
2728 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
2731 old_sym = st->n.sym;
2732 if ((old_sym->attr.flavor == FL_PROCEDURE
2733 || old_sym->ts.type == BT_UNKNOWN)
2734 && old_sym->ns == ns
2735 && ! old_sym->attr.contained)
2737 /* Replace it with the symbol from the parent namespace. */
2741 /* Free the old (local) symbol. */
2743 if (old_sym->refs == 0)
2744 gfc_free_symbol (old_sym);
2747 /* Do the same for any contained procedures. */
2748 gfc_fixup_sibling_symbols (sym, ns->contained);
2753 parse_contained (int module)
2755 gfc_namespace *ns, *parent_ns;
2756 gfc_state_data s1, s2;
2760 int contains_statements = 0;
2762 push_state (&s1, COMP_CONTAINS, NULL);
2763 parent_ns = gfc_current_ns;
2767 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2769 gfc_current_ns->sibling = parent_ns->contained;
2770 parent_ns->contained = gfc_current_ns;
2772 st = next_statement ();
2781 contains_statements = 1;
2782 accept_statement (st);
2785 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2788 /* For internal procedures, create/update the symbol in the
2789 parent namespace. */
2793 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2795 ("Contained procedure '%s' at %C is already ambiguous",
2796 gfc_new_block->name);
2799 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2800 &gfc_new_block->declared_at) ==
2803 if (st == ST_FUNCTION)
2804 gfc_add_function (&sym->attr, sym->name,
2805 &gfc_new_block->declared_at);
2807 gfc_add_subroutine (&sym->attr, sym->name,
2808 &gfc_new_block->declared_at);
2812 gfc_commit_symbols ();
2815 sym = gfc_new_block;
2817 /* Mark this as a contained function, so it isn't replaced
2818 by other module functions. */
2819 sym->attr.contained = 1;
2820 sym->attr.referenced = 1;
2822 parse_progunit (ST_NONE);
2824 /* Fix up any sibling functions that refer to this one. */
2825 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2826 /* Or refer to any of its alternate entry points. */
2827 for (el = gfc_current_ns->entries; el; el = el->next)
2828 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2830 gfc_current_ns->code = s2.head;
2831 gfc_current_ns = parent_ns;
2836 /* These statements are associated with the end of the host
2838 case ST_END_FUNCTION:
2840 case ST_END_PROGRAM:
2841 case ST_END_SUBROUTINE:
2842 accept_statement (st);
2846 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2847 gfc_ascii_statement (st));
2848 reject_statement ();
2852 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2853 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2855 /* The first namespace in the list is guaranteed to not have
2856 anything (worthwhile) in it. */
2858 gfc_current_ns = parent_ns;
2860 ns = gfc_current_ns->contained;
2861 gfc_current_ns->contained = ns->sibling;
2862 gfc_free_namespace (ns);
2865 if (!contains_statements)
2866 /* This is valid in Fortran 2008. */
2867 gfc_notify_std (GFC_STD_GNU, "Extension: "
2868 "CONTAINS statement without FUNCTION "
2869 "or SUBROUTINE statement at %C");
2873 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2876 parse_progunit (gfc_statement st)
2881 st = parse_spec (st);
2891 accept_statement (st);
2901 st = parse_executable (st);
2912 accept_statement (st);
2919 unexpected_statement (st);
2920 reject_statement ();
2921 st = next_statement ();
2927 for (p = gfc_state_stack; p; p = p->previous)
2928 if (p->state == COMP_CONTAINS)
2931 if (gfc_find_state (COMP_MODULE) == SUCCESS)
2936 gfc_error ("CONTAINS statement at %C is already in a contained "
2938 st = next_statement ();
2942 parse_contained (0);
2945 gfc_current_ns->code = gfc_state_stack->head;
2949 /* Come here to complain about a global symbol already in use as
2953 global_used (gfc_gsymbol *sym, locus *where)
2958 where = &gfc_current_locus;
2968 case GSYM_SUBROUTINE:
2969 name = "SUBROUTINE";
2974 case GSYM_BLOCK_DATA:
2975 name = "BLOCK DATA";
2981 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
2985 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
2986 sym->name, where, name, &sym->where);
2990 /* Parse a block data program unit. */
2993 parse_block_data (void)
2996 static locus blank_locus;
2997 static int blank_block=0;
3000 gfc_current_ns->proc_name = gfc_new_block;
3001 gfc_current_ns->is_block_data = 1;
3003 if (gfc_new_block == NULL)
3006 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3007 "prior BLOCK DATA at %L", &blank_locus);
3011 blank_locus = gfc_current_locus;
3016 s = gfc_get_gsymbol (gfc_new_block->name);
3017 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
3018 global_used(s, NULL);
3021 s->type = GSYM_BLOCK_DATA;
3022 s->where = gfc_current_locus;
3027 st = parse_spec (ST_NONE);
3029 while (st != ST_END_BLOCK_DATA)
3031 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3032 gfc_ascii_statement (st));
3033 reject_statement ();
3034 st = next_statement ();
3039 /* Parse a module subprogram. */
3047 s = gfc_get_gsymbol (gfc_new_block->name);
3048 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
3049 global_used(s, NULL);
3052 s->type = GSYM_MODULE;
3053 s->where = gfc_current_locus;
3057 st = parse_spec (ST_NONE);
3066 parse_contained (1);
3070 accept_statement (st);
3074 gfc_error ("Unexpected %s statement in MODULE at %C",
3075 gfc_ascii_statement (st));
3077 reject_statement ();
3078 st = next_statement ();
3084 /* Add a procedure name to the global symbol table. */
3087 add_global_procedure (int sub)
3091 s = gfc_get_gsymbol(gfc_new_block->name);
3094 || (s->type != GSYM_UNKNOWN && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3095 global_used(s, NULL);
3098 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3099 s->where = gfc_current_locus;
3105 /* Add a program to the global symbol table. */
3108 add_global_program (void)
3112 if (gfc_new_block == NULL)
3114 s = gfc_get_gsymbol (gfc_new_block->name);
3116 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
3117 global_used(s, NULL);
3120 s->type = GSYM_PROGRAM;
3121 s->where = gfc_current_locus;
3127 /* Top level parser. */
3130 gfc_parse_file (void)
3132 int seen_program, errors_before, errors;
3133 gfc_state_data top, s;
3137 top.state = COMP_NONE;
3139 top.previous = NULL;
3140 top.head = top.tail = NULL;
3141 top.do_variable = NULL;
3143 gfc_state_stack = ⊤
3145 gfc_clear_new_st ();
3147 gfc_statement_label = NULL;
3149 if (setjmp (eof_buf))
3150 return FAILURE; /* Come here on unexpected EOF */
3154 /* Exit early for empty files. */
3160 st = next_statement ();
3169 goto duplicate_main;
3171 prog_locus = gfc_current_locus;
3173 push_state (&s, COMP_PROGRAM, gfc_new_block);
3174 main_program_symbol(gfc_current_ns);
3175 accept_statement (st);
3176 add_global_program ();
3177 parse_progunit (ST_NONE);
3181 add_global_procedure (1);
3182 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
3183 accept_statement (st);
3184 parse_progunit (ST_NONE);
3188 add_global_procedure (0);
3189 push_state (&s, COMP_FUNCTION, gfc_new_block);
3190 accept_statement (st);
3191 parse_progunit (ST_NONE);
3195 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
3196 accept_statement (st);
3197 parse_block_data ();
3201 push_state (&s, COMP_MODULE, gfc_new_block);
3202 accept_statement (st);
3204 gfc_get_errors (NULL, &errors_before);
3208 /* Anything else starts a nameless main program block. */
3211 goto duplicate_main;
3213 prog_locus = gfc_current_locus;
3215 push_state (&s, COMP_PROGRAM, gfc_new_block);
3216 main_program_symbol(gfc_current_ns);
3217 parse_progunit (st);
3221 gfc_current_ns->code = s.head;
3223 gfc_resolve (gfc_current_ns);
3225 /* Dump the parse tree if requested. */
3226 if (gfc_option.verbose)
3227 gfc_show_namespace (gfc_current_ns);
3229 gfc_get_errors (NULL, &errors);
3230 if (s.state == COMP_MODULE)
3232 gfc_dump_module (s.sym->name, errors_before == errors);
3234 gfc_generate_module_code (gfc_current_ns);
3239 gfc_generate_code (gfc_current_ns);
3250 /* If we see a duplicate main program, shut down. If the second
3251 instance is an implied main program, ie data decls or executable
3252 statements, we're in for lots of errors. */
3253 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
3254 reject_statement ();