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 ("import", gfc_match_import, ST_IMPORT);
233 match ("interface", gfc_match_interface, ST_INTERFACE);
234 match ("intent", gfc_match_intent, ST_ATTR_DECL);
235 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
239 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
240 match ("module", gfc_match_module, ST_MODULE);
244 match ("nullify", gfc_match_nullify, ST_NULLIFY);
245 match ("namelist", gfc_match_namelist, ST_NAMELIST);
249 match ("open", gfc_match_open, ST_OPEN);
250 match ("optional", gfc_match_optional, ST_ATTR_DECL);
254 match ("print", gfc_match_print, ST_WRITE);
255 match ("parameter", gfc_match_parameter, ST_PARAMETER);
256 match ("pause", gfc_match_pause, ST_PAUSE);
257 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
258 if (gfc_match_private (&st) == MATCH_YES)
260 match ("program", gfc_match_program, ST_PROGRAM);
261 if (gfc_match_public (&st) == MATCH_YES)
266 match ("read", gfc_match_read, ST_READ);
267 match ("return", gfc_match_return, ST_RETURN);
268 match ("rewind", gfc_match_rewind, ST_REWIND);
272 match ("sequence", gfc_match_eos, ST_SEQUENCE);
273 match ("stop", gfc_match_stop, ST_STOP);
274 match ("save", gfc_match_save, ST_ATTR_DECL);
278 match ("target", gfc_match_target, ST_ATTR_DECL);
279 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
283 match ("use% ", gfc_match_use, ST_USE);
287 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
291 match ("write", gfc_match_write, ST_WRITE);
295 /* All else has failed, so give up. See if any of the matchers has
296 stored an error message of some sort. */
298 if (gfc_error_check () == 0)
299 gfc_error_now ("Unclassifiable statement at %C");
303 gfc_error_recovery ();
309 decode_omp_directive (void)
318 gfc_clear_error (); /* Clear any pending errors. */
319 gfc_clear_warning (); /* Clear any pending warnings. */
323 gfc_error_now ("OpenMP directives at %C may not appear in PURE or ELEMENTAL procedures");
324 gfc_error_recovery ();
328 old_locus = gfc_current_locus;
330 /* General OpenMP directive matching: Instead of testing every possible
331 statement, we eliminate most possibilities by peeking at the
334 c = gfc_peek_char ();
339 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
342 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
345 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
348 match ("do", gfc_match_omp_do, ST_OMP_DO);
351 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
352 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
353 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
354 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
355 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
356 match ("end parallel sections", gfc_match_omp_eos,
357 ST_OMP_END_PARALLEL_SECTIONS);
358 match ("end parallel workshare", gfc_match_omp_eos,
359 ST_OMP_END_PARALLEL_WORKSHARE);
360 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
361 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
362 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
363 match ("end workshare", gfc_match_omp_end_nowait,
364 ST_OMP_END_WORKSHARE);
367 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
370 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
373 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
376 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
377 match ("parallel sections", gfc_match_omp_parallel_sections,
378 ST_OMP_PARALLEL_SECTIONS);
379 match ("parallel workshare", gfc_match_omp_parallel_workshare,
380 ST_OMP_PARALLEL_WORKSHARE);
381 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
384 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
385 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
386 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
389 match ("threadprivate", gfc_match_omp_threadprivate,
390 ST_OMP_THREADPRIVATE);
392 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
396 /* All else has failed, so give up. See if any of the matchers has
397 stored an error message of some sort. */
399 if (gfc_error_check () == 0)
400 gfc_error_now ("Unclassifiable OpenMP directive at %C");
404 gfc_error_recovery ();
412 /* Get the next statement in free form source. */
418 int c, d, cnt, at_bol;
420 at_bol = gfc_at_bol ();
421 gfc_gobble_whitespace ();
423 c = gfc_peek_char ();
427 /* Found a statement label? */
428 m = gfc_match_st_label (&gfc_statement_label);
430 d = gfc_peek_char ();
431 if (m != MATCH_YES || !gfc_is_whitespace (d))
433 gfc_match_small_literal_int (&c, &cnt);
436 gfc_error_now ("Too many digits in statement label at %C");
439 gfc_error_now ("Zero is not a valid statement label at %C");
442 c = gfc_next_char ();
445 if (!gfc_is_whitespace (c))
446 gfc_error_now ("Non-numeric character in statement label at %C");
452 label_locus = gfc_current_locus;
454 gfc_gobble_whitespace ();
456 if (at_bol && gfc_peek_char () == ';')
459 ("Semicolon at %C needs to be preceded by statement");
460 gfc_next_char (); /* Eat up the semicolon. */
464 if (gfc_match_eos () == MATCH_YES)
467 ("Ignoring statement label in empty statement at %C");
468 gfc_free_st_label (gfc_statement_label);
469 gfc_statement_label = NULL;
476 /* Comments have already been skipped by the time we get here,
477 except for OpenMP directives. */
478 if (gfc_option.flag_openmp)
482 c = gfc_next_char ();
483 for (i = 0; i < 5; i++, c = gfc_next_char ())
484 gcc_assert (c == "!$omp"[i]);
486 gcc_assert (c == ' ');
487 return decode_omp_directive ();
491 if (at_bol && c == ';')
493 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
494 gfc_next_char (); /* Eat up the semicolon. */
498 return decode_statement ();
502 /* Get the next statement in fixed-form source. */
507 int label, digit_flag, i;
512 return decode_statement ();
514 /* Skip past the current label field, parsing a statement label if
515 one is there. This is a weird number parser, since the number is
516 contained within five columns and can have any kind of embedded
517 spaces. We also check for characters that make the rest of the
523 for (i = 0; i < 5; i++)
525 c = gfc_next_char_literal (0);
542 label = label * 10 + c - '0';
543 label_locus = gfc_current_locus;
547 /* Comments have already been skipped by the time we get
548 here, except for OpenMP directives. */
550 if (gfc_option.flag_openmp)
552 for (i = 0; i < 5; i++, c = gfc_next_char_literal (0))
553 gcc_assert (TOLOWER (c) == "*$omp"[i]);
555 if (c != ' ' && c != '0')
557 gfc_buffer_error (0);
558 gfc_error ("Bad continuation line at %C");
562 return decode_omp_directive ();
566 /* Comments have already been skipped by the time we get
567 here so don't bother checking for them. */
570 gfc_buffer_error (0);
571 gfc_error ("Non-numeric character in statement label at %C");
579 gfc_warning_now ("Zero is not a valid statement label at %C");
582 /* We've found a valid statement label. */
583 gfc_statement_label = gfc_get_st_label (label);
587 /* Since this line starts a statement, it cannot be a continuation
588 of a previous statement. If we see something here besides a
589 space or zero, it must be a bad continuation line. */
591 c = gfc_next_char_literal (0);
595 if (c != ' ' && c != '0')
597 gfc_buffer_error (0);
598 gfc_error ("Bad continuation line at %C");
602 /* Now that we've taken care of the statement label columns, we have
603 to make sure that the first nonblank character is not a '!'. If
604 it is, the rest of the line is a comment. */
608 loc = gfc_current_locus;
609 c = gfc_next_char_literal (0);
611 while (gfc_is_whitespace (c));
615 gfc_current_locus = loc;
619 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
623 if (gfc_match_eos () == MATCH_YES)
626 /* At this point, we've got a nonblank statement to parse. */
627 return decode_statement ();
631 gfc_warning ("Ignoring statement label in empty statement at %C");
637 /* Return the next non-ST_NONE statement to the caller. We also worry
638 about including files and the ends of include files at this stage. */
641 next_statement (void)
645 gfc_new_block = NULL;
649 gfc_statement_label = NULL;
650 gfc_buffer_error (1);
654 if (gfc_option.warn_line_truncation
655 && gfc_current_locus.lb
656 && gfc_current_locus.lb->truncated)
657 gfc_warning_now ("Line truncated at %C");
662 gfc_skip_comments ();
671 (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
677 gfc_buffer_error (0);
680 check_statement_label (st);
686 /****************************** Parser ***********************************/
688 /* The parser subroutines are of type 'try' that fail if the file ends
691 /* Macros that expand to case-labels for various classes of
692 statements. Start with executable statements that directly do
695 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
696 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
697 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
698 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
699 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
700 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
701 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
702 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
705 /* Statements that mark other executable statements. */
707 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
708 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
709 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
710 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
711 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
712 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
714 /* Declaration statements */
716 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
717 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
718 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE
720 /* Block end statements. Errors associated with interchanging these
721 are detected in gfc_match_end(). */
723 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
724 case ST_END_PROGRAM: case ST_END_SUBROUTINE
727 /* Push a new state onto the stack. */
730 push_state (gfc_state_data * p, gfc_compile_state new_state, gfc_symbol * sym)
733 p->state = new_state;
734 p->previous = gfc_state_stack;
736 p->head = p->tail = NULL;
737 p->do_variable = NULL;
743 /* Pop the current state. */
749 gfc_state_stack = gfc_state_stack->previous;
753 /* Try to find the given state in the state stack. */
756 gfc_find_state (gfc_compile_state state)
760 for (p = gfc_state_stack; p; p = p->previous)
761 if (p->state == state)
764 return (p == NULL) ? FAILURE : SUCCESS;
768 /* Starts a new level in the statement list. */
771 new_level (gfc_code * q)
775 p = q->block = gfc_get_code ();
777 gfc_state_stack->head = gfc_state_stack->tail = p;
783 /* Add the current new_st code structure and adds it to the current
784 program unit. As a side-effect, it zeroes the new_st. */
794 p->loc = gfc_current_locus;
796 if (gfc_state_stack->head == NULL)
797 gfc_state_stack->head = p;
799 gfc_state_stack->tail->next = p;
801 while (p->next != NULL)
804 gfc_state_stack->tail = p;
812 /* Frees everything associated with the current statement. */
815 undo_new_statement (void)
817 gfc_free_statements (new_st.block);
818 gfc_free_statements (new_st.next);
819 gfc_free_statement (&new_st);
824 /* If the current statement has a statement label, make sure that it
825 is allowed to, or should have one. */
828 check_statement_label (gfc_statement st)
832 if (gfc_statement_label == NULL)
835 gfc_error ("FORMAT statement at %L does not have a statement label",
843 case ST_END_FUNCTION:
844 case ST_END_SUBROUTINE:
850 type = ST_LABEL_TARGET;
854 type = ST_LABEL_FORMAT;
857 /* Statement labels are not restricted from appearing on a
858 particular line. However, there are plenty of situations
859 where the resulting label can't be referenced. */
862 type = ST_LABEL_BAD_TARGET;
866 gfc_define_st_label (gfc_statement_label, type, &label_locus);
868 new_st.here = gfc_statement_label;
872 /* Figures out what the enclosing program unit is. This will be a
873 function, subroutine, program, block data or module. */
876 gfc_enclosing_unit (gfc_compile_state * result)
880 for (p = gfc_state_stack; p; p = p->previous)
881 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
882 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
883 || p->state == COMP_PROGRAM)
892 *result = COMP_PROGRAM;
897 /* Translate a statement enum to a string. */
900 gfc_ascii_statement (gfc_statement st)
906 case ST_ARITHMETIC_IF:
907 p = _("arithmetic IF");
913 p = _("attribute declaration");
943 p = _("data declaration");
951 case ST_DERIVED_DECL:
952 p = _("derived type declaration");
966 case ST_END_BLOCK_DATA:
967 p = "END BLOCK DATA";
978 case ST_END_FUNCTION:
984 case ST_END_INTERFACE:
996 case ST_END_SUBROUTINE:
997 p = "END SUBROUTINE";
1008 case ST_EQUIVALENCE:
1017 case ST_FORALL_BLOCK: /* Fall through */
1036 case ST_IMPLICIT_NONE:
1037 p = "IMPLICIT NONE";
1039 case ST_IMPLIED_ENDDO:
1040 p = _("implied END DO");
1066 case ST_MODULE_PROC:
1067 p = "MODULE PROCEDURE";
1102 case ST_WHERE_BLOCK: /* Fall through */
1110 p = _("assignment");
1112 case ST_POINTER_ASSIGNMENT:
1113 p = _("pointer assignment");
1115 case ST_SELECT_CASE:
1124 case ST_STATEMENT_FUNCTION:
1125 p = "STATEMENT FUNCTION";
1127 case ST_LABEL_ASSIGNMENT:
1128 p = "LABEL ASSIGNMENT";
1131 p = "ENUM DEFINITION";
1134 p = "ENUMERATOR DEFINITION";
1142 case ST_OMP_BARRIER:
1143 p = "!$OMP BARRIER";
1145 case ST_OMP_CRITICAL:
1146 p = "!$OMP CRITICAL";
1151 case ST_OMP_END_CRITICAL:
1152 p = "!$OMP END CRITICAL";
1157 case ST_OMP_END_MASTER:
1158 p = "!$OMP END MASTER";
1160 case ST_OMP_END_ORDERED:
1161 p = "!$OMP END ORDERED";
1163 case ST_OMP_END_PARALLEL:
1164 p = "!$OMP END PARALLEL";
1166 case ST_OMP_END_PARALLEL_DO:
1167 p = "!$OMP END PARALLEL DO";
1169 case ST_OMP_END_PARALLEL_SECTIONS:
1170 p = "!$OMP END PARALLEL SECTIONS";
1172 case ST_OMP_END_PARALLEL_WORKSHARE:
1173 p = "!$OMP END PARALLEL WORKSHARE";
1175 case ST_OMP_END_SECTIONS:
1176 p = "!$OMP END SECTIONS";
1178 case ST_OMP_END_SINGLE:
1179 p = "!$OMP END SINGLE";
1181 case ST_OMP_END_WORKSHARE:
1182 p = "!$OMP END WORKSHARE";
1190 case ST_OMP_ORDERED:
1191 p = "!$OMP ORDERED";
1193 case ST_OMP_PARALLEL:
1194 p = "!$OMP PARALLEL";
1196 case ST_OMP_PARALLEL_DO:
1197 p = "!$OMP PARALLEL DO";
1199 case ST_OMP_PARALLEL_SECTIONS:
1200 p = "!$OMP PARALLEL SECTIONS";
1202 case ST_OMP_PARALLEL_WORKSHARE:
1203 p = "!$OMP PARALLEL WORKSHARE";
1205 case ST_OMP_SECTIONS:
1206 p = "!$OMP SECTIONS";
1208 case ST_OMP_SECTION:
1209 p = "!$OMP SECTION";
1214 case ST_OMP_THREADPRIVATE:
1215 p = "!$OMP THREADPRIVATE";
1217 case ST_OMP_WORKSHARE:
1218 p = "!$OMP WORKSHARE";
1221 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1228 /* Create a symbol for the main program and assign it to ns->proc_name. */
1231 main_program_symbol (gfc_namespace * ns)
1233 gfc_symbol *main_program;
1234 symbol_attribute attr;
1236 gfc_get_symbol ("MAIN__", ns, &main_program);
1237 gfc_clear_attr (&attr);
1238 attr.flavor = FL_PROCEDURE;
1239 attr.proc = PROC_UNKNOWN;
1240 attr.subroutine = 1;
1241 attr.access = ACCESS_PUBLIC;
1242 attr.is_main_program = 1;
1243 main_program->attr = attr;
1244 main_program->declared_at = gfc_current_locus;
1245 ns->proc_name = main_program;
1246 gfc_commit_symbols ();
1250 /* Do whatever is necessary to accept the last statement. */
1253 accept_statement (gfc_statement st)
1262 case ST_IMPLICIT_NONE:
1263 gfc_set_implicit_none ();
1272 gfc_current_ns->proc_name = gfc_new_block;
1275 /* If the statement is the end of a block, lay down a special code
1276 that allows a branch to the end of the block from within the
1281 if (gfc_statement_label != NULL)
1283 new_st.op = EXEC_NOP;
1289 /* The end-of-program unit statements do not get the special
1290 marker and require a statement of some sort if they are a
1293 case ST_END_PROGRAM:
1294 case ST_END_FUNCTION:
1295 case ST_END_SUBROUTINE:
1296 if (gfc_statement_label != NULL)
1298 new_st.op = EXEC_RETURN;
1314 gfc_commit_symbols ();
1315 gfc_warning_check ();
1316 gfc_clear_new_st ();
1320 /* Undo anything tentative that has been built for the current
1324 reject_statement (void)
1326 gfc_new_block = NULL;
1327 gfc_undo_symbols ();
1328 gfc_clear_warning ();
1329 undo_new_statement ();
1333 /* Generic complaint about an out of order statement. We also do
1334 whatever is necessary to clean up. */
1337 unexpected_statement (gfc_statement st)
1340 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1342 reject_statement ();
1346 /* Given the next statement seen by the matcher, make sure that it is
1347 in proper order with the last. This subroutine is initialized by
1348 calling it with an argument of ST_NONE. If there is a problem, we
1349 issue an error and return FAILURE. Otherwise we return SUCCESS.
1351 Individual parsers need to verify that the statements seen are
1352 valid before calling here, ie ENTRY statements are not allowed in
1353 INTERFACE blocks. The following diagram is taken from the standard:
1355 +---------------------------------------+
1356 | program subroutine function module |
1357 +---------------------------------------+
1359 +---------------------------------------+
1361 +---------------------------------------+
1363 | +-----------+------------------+
1364 | | parameter | implicit |
1365 | +-----------+------------------+
1366 | format | | derived type |
1367 | entry | parameter | interface |
1368 | | data | specification |
1369 | | | statement func |
1370 | +-----------+------------------+
1371 | | data | executable |
1372 +--------+-----------+------------------+
1374 +---------------------------------------+
1375 | internal module/subprogram |
1376 +---------------------------------------+
1378 +---------------------------------------+
1385 { ORDER_START, ORDER_USE, ORDER_IMPORT, ORDER_IMPLICIT_NONE,
1386 ORDER_IMPLICIT, ORDER_SPEC, ORDER_EXEC
1389 gfc_statement last_statement;
1395 verify_st_order (st_state * p, gfc_statement st)
1401 p->state = ORDER_START;
1405 if (p->state > ORDER_USE)
1407 p->state = ORDER_USE;
1411 if (p->state > ORDER_IMPORT)
1413 p->state = ORDER_IMPORT;
1416 case ST_IMPLICIT_NONE:
1417 if (p->state > ORDER_IMPLICIT_NONE)
1420 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1421 statement disqualifies a USE but not an IMPLICIT NONE.
1422 Duplicate IMPLICIT NONEs are caught when the implicit types
1425 p->state = ORDER_IMPLICIT_NONE;
1429 if (p->state > ORDER_IMPLICIT)
1431 p->state = ORDER_IMPLICIT;
1436 if (p->state < ORDER_IMPLICIT_NONE)
1437 p->state = ORDER_IMPLICIT_NONE;
1441 if (p->state >= ORDER_EXEC)
1443 if (p->state < ORDER_IMPLICIT)
1444 p->state = ORDER_IMPLICIT;
1448 if (p->state < ORDER_SPEC)
1449 p->state = ORDER_SPEC;
1454 case ST_DERIVED_DECL:
1456 if (p->state >= ORDER_EXEC)
1458 if (p->state < ORDER_SPEC)
1459 p->state = ORDER_SPEC;
1464 if (p->state < ORDER_EXEC)
1465 p->state = ORDER_EXEC;
1470 ("Unexpected %s statement in verify_st_order() at %C",
1471 gfc_ascii_statement (st));
1474 /* All is well, record the statement in case we need it next time. */
1475 p->where = gfc_current_locus;
1476 p->last_statement = st;
1480 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1481 gfc_ascii_statement (st),
1482 gfc_ascii_statement (p->last_statement), &p->where);
1488 /* Handle an unexpected end of file. This is a show-stopper... */
1490 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1493 unexpected_eof (void)
1497 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1499 /* Memory cleanup. Move to "second to last". */
1500 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1503 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1506 longjmp (eof_buf, 1);
1510 /* Parse a derived type. */
1513 parse_derived (void)
1515 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1523 accept_statement (ST_DERIVED_DECL);
1524 push_state (&s, COMP_DERIVED, gfc_new_block);
1526 gfc_new_block->component_access = ACCESS_PUBLIC;
1533 while (compiling_type)
1535 st = next_statement ();
1542 accept_statement (st);
1549 if (!seen_component)
1551 gfc_error ("Derived type definition at %C has no components");
1555 accept_statement (ST_END_TYPE);
1559 if (gfc_find_state (COMP_MODULE) == FAILURE)
1562 ("PRIVATE statement in TYPE at %C must be inside a MODULE");
1569 gfc_error ("PRIVATE statement at %C must precede "
1570 "structure components");
1577 gfc_error ("Duplicate PRIVATE statement at %C");
1581 s.sym->component_access = ACCESS_PRIVATE;
1582 accept_statement (ST_PRIVATE);
1589 gfc_error ("SEQUENCE statement at %C must precede "
1590 "structure components");
1595 if (gfc_current_block ()->attr.sequence)
1596 gfc_warning ("SEQUENCE attribute at %C already specified in "
1601 gfc_error ("Duplicate SEQUENCE statement at %C");
1606 gfc_add_sequence (&gfc_current_block ()->attr,
1607 gfc_current_block ()->name, NULL);
1611 unexpected_statement (st);
1616 /* Look for allocatable components. */
1617 sym = gfc_current_block ();
1618 for (c = sym->components; c; c = c->next)
1620 if (c->allocatable || (c->ts.type == BT_DERIVED
1621 && c->ts.derived->attr.alloc_comp))
1623 sym->attr.alloc_comp = 1;
1633 /* Parse an ENUM. */
1642 int seen_enumerator = 0;
1646 push_state (&s, COMP_ENUM, gfc_new_block);
1650 while (compiling_enum)
1652 st = next_statement ();
1660 seen_enumerator = 1;
1661 accept_statement (st);
1666 if (!seen_enumerator)
1668 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1671 accept_statement (st);
1675 gfc_free_enum_history ();
1676 unexpected_statement (st);
1683 /* Parse an interface. We must be able to deal with the possibility
1684 of recursive interfaces. The parse_spec() subroutine is mutually
1685 recursive with parse_interface(). */
1687 static gfc_statement parse_spec (gfc_statement);
1690 parse_interface (void)
1692 gfc_compile_state new_state, current_state;
1693 gfc_symbol *prog_unit, *sym;
1694 gfc_interface_info save;
1695 gfc_state_data s1, s2;
1698 accept_statement (ST_INTERFACE);
1700 current_interface.ns = gfc_current_ns;
1701 save = current_interface;
1703 sym = (current_interface.type == INTERFACE_GENERIC
1704 || current_interface.type == INTERFACE_USER_OP) ? gfc_new_block : NULL;
1706 push_state (&s1, COMP_INTERFACE, sym);
1707 current_state = COMP_NONE;
1710 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1712 st = next_statement ();
1719 new_state = COMP_SUBROUTINE;
1720 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1721 gfc_new_block->formal, NULL);
1725 new_state = COMP_FUNCTION;
1726 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1727 gfc_new_block->formal, NULL);
1730 case ST_MODULE_PROC: /* The module procedure matcher makes
1731 sure the context is correct. */
1732 accept_statement (st);
1733 gfc_free_namespace (gfc_current_ns);
1736 case ST_END_INTERFACE:
1737 gfc_free_namespace (gfc_current_ns);
1738 gfc_current_ns = current_interface.ns;
1742 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1743 gfc_ascii_statement (st));
1744 reject_statement ();
1745 gfc_free_namespace (gfc_current_ns);
1750 /* Make sure that a generic interface has only subroutines or
1751 functions and that the generic name has the right attribute. */
1752 if (current_interface.type == INTERFACE_GENERIC)
1754 if (current_state == COMP_NONE)
1756 if (new_state == COMP_FUNCTION)
1757 gfc_add_function (&sym->attr, sym->name, NULL);
1758 else if (new_state == COMP_SUBROUTINE)
1759 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1761 current_state = new_state;
1765 if (new_state != current_state)
1767 if (new_state == COMP_SUBROUTINE)
1769 ("SUBROUTINE at %C does not belong in a generic function "
1772 if (new_state == COMP_FUNCTION)
1774 ("FUNCTION at %C does not belong in a generic subroutine "
1780 push_state (&s2, new_state, gfc_new_block);
1781 accept_statement (st);
1782 prog_unit = gfc_new_block;
1783 prog_unit->formal_ns = gfc_current_ns;
1786 /* Read data declaration statements. */
1787 st = parse_spec (ST_NONE);
1789 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1791 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1792 gfc_ascii_statement (st));
1793 reject_statement ();
1797 current_interface = save;
1798 gfc_add_interface (prog_unit);
1808 /* Parse a set of specification statements. Returns the statement
1809 that doesn't fit. */
1811 static gfc_statement
1812 parse_spec (gfc_statement st)
1816 verify_st_order (&ss, ST_NONE);
1818 st = next_statement ();
1828 case ST_DATA: /* Not allowed in interfaces */
1829 if (gfc_current_state () == COMP_INTERFACE)
1836 case ST_IMPLICIT_NONE:
1841 case ST_DERIVED_DECL:
1843 if (verify_st_order (&ss, st) == FAILURE)
1845 reject_statement ();
1846 st = next_statement ();
1856 case ST_DERIVED_DECL:
1862 if (gfc_current_state () != COMP_MODULE)
1864 gfc_error ("%s statement must appear in a MODULE",
1865 gfc_ascii_statement (st));
1869 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1871 gfc_error ("%s statement at %C follows another accessibility "
1872 "specification", gfc_ascii_statement (st));
1876 gfc_current_ns->default_access = (st == ST_PUBLIC)
1877 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1885 accept_statement (st);
1886 st = next_statement ();
1890 accept_statement (st);
1892 st = next_statement ();
1903 /* Parse a WHERE block, (not a simple WHERE statement). */
1906 parse_where_block (void)
1908 int seen_empty_else;
1913 accept_statement (ST_WHERE_BLOCK);
1914 top = gfc_state_stack->tail;
1916 push_state (&s, COMP_WHERE, gfc_new_block);
1918 d = add_statement ();
1919 d->expr = top->expr;
1925 seen_empty_else = 0;
1929 st = next_statement ();
1935 case ST_WHERE_BLOCK:
1936 parse_where_block ();
1941 accept_statement (st);
1945 if (seen_empty_else)
1948 ("ELSEWHERE statement at %C follows previous unmasked "
1953 if (new_st.expr == NULL)
1954 seen_empty_else = 1;
1956 d = new_level (gfc_state_stack->head);
1958 d->expr = new_st.expr;
1960 accept_statement (st);
1965 accept_statement (st);
1969 gfc_error ("Unexpected %s statement in WHERE block at %C",
1970 gfc_ascii_statement (st));
1971 reject_statement ();
1976 while (st != ST_END_WHERE);
1982 /* Parse a FORALL block (not a simple FORALL statement). */
1985 parse_forall_block (void)
1991 accept_statement (ST_FORALL_BLOCK);
1992 top = gfc_state_stack->tail;
1994 push_state (&s, COMP_FORALL, gfc_new_block);
1996 d = add_statement ();
1997 d->op = EXEC_FORALL;
2002 st = next_statement ();
2007 case ST_POINTER_ASSIGNMENT:
2010 accept_statement (st);
2013 case ST_WHERE_BLOCK:
2014 parse_where_block ();
2017 case ST_FORALL_BLOCK:
2018 parse_forall_block ();
2022 accept_statement (st);
2029 gfc_error ("Unexpected %s statement in FORALL block at %C",
2030 gfc_ascii_statement (st));
2032 reject_statement ();
2036 while (st != ST_END_FORALL);
2042 static gfc_statement parse_executable (gfc_statement);
2044 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2047 parse_if_block (void)
2056 accept_statement (ST_IF_BLOCK);
2058 top = gfc_state_stack->tail;
2059 push_state (&s, COMP_IF, gfc_new_block);
2061 new_st.op = EXEC_IF;
2062 d = add_statement ();
2064 d->expr = top->expr;
2070 st = parse_executable (ST_NONE);
2081 ("ELSE IF statement at %C cannot follow ELSE statement at %L",
2084 reject_statement ();
2088 d = new_level (gfc_state_stack->head);
2090 d->expr = new_st.expr;
2092 accept_statement (st);
2099 gfc_error ("Duplicate ELSE statements at %L and %C",
2101 reject_statement ();
2106 else_locus = gfc_current_locus;
2108 d = new_level (gfc_state_stack->head);
2111 accept_statement (st);
2119 unexpected_statement (st);
2123 while (st != ST_ENDIF);
2126 accept_statement (st);
2130 /* Parse a SELECT block. */
2133 parse_select_block (void)
2139 accept_statement (ST_SELECT_CASE);
2141 cp = gfc_state_stack->tail;
2142 push_state (&s, COMP_SELECT, gfc_new_block);
2144 /* Make sure that the next statement is a CASE or END SELECT. */
2147 st = next_statement ();
2150 if (st == ST_END_SELECT)
2152 /* Empty SELECT CASE is OK. */
2153 accept_statement (st);
2161 ("Expected a CASE or END SELECT statement following SELECT CASE "
2164 reject_statement ();
2167 /* At this point, we're got a nonempty select block. */
2168 cp = new_level (cp);
2171 accept_statement (st);
2175 st = parse_executable (ST_NONE);
2182 cp = new_level (gfc_state_stack->head);
2184 gfc_clear_new_st ();
2186 accept_statement (st);
2192 /* Can't have an executable statement because of
2193 parse_executable(). */
2195 unexpected_statement (st);
2199 while (st != ST_END_SELECT);
2202 accept_statement (st);
2206 /* Given a symbol, make sure it is not an iteration variable for a DO
2207 statement. This subroutine is called when the symbol is seen in a
2208 context that causes it to become redefined. If the symbol is an
2209 iterator, we generate an error message and return nonzero. */
2212 gfc_check_do_variable (gfc_symtree *st)
2216 for (s=gfc_state_stack; s; s = s->previous)
2217 if (s->do_variable == st)
2219 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2220 "loop beginning at %L", st->name, &s->head->loc);
2228 /* Checks to see if the current statement label closes an enddo.
2229 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2230 an error) if it incorrectly closes an ENDDO. */
2233 check_do_closure (void)
2237 if (gfc_statement_label == NULL)
2240 for (p = gfc_state_stack; p; p = p->previous)
2241 if (p->state == COMP_DO)
2245 return 0; /* No loops to close */
2247 if (p->ext.end_do_label == gfc_statement_label)
2250 if (p == gfc_state_stack)
2254 ("End of nonblock DO statement at %C is within another block");
2258 /* At this point, the label doesn't terminate the innermost loop.
2259 Make sure it doesn't terminate another one. */
2260 for (; p; p = p->previous)
2261 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
2263 gfc_error ("End of nonblock DO statement at %C is interwoven "
2264 "with another DO loop");
2272 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2273 handled inside of parse_executable(), because they aren't really
2277 parse_do_block (void)
2284 s.ext.end_do_label = new_st.label;
2286 if (new_st.ext.iterator != NULL)
2287 stree = new_st.ext.iterator->var->symtree;
2291 accept_statement (ST_DO);
2293 top = gfc_state_stack->tail;
2294 push_state (&s, COMP_DO, gfc_new_block);
2296 s.do_variable = stree;
2298 top->block = new_level (top);
2299 top->block->op = EXEC_DO;
2302 st = parse_executable (ST_NONE);
2310 if (s.ext.end_do_label != NULL
2311 && s.ext.end_do_label != gfc_statement_label)
2313 ("Statement label in ENDDO at %C doesn't match DO label");
2315 if (gfc_statement_label != NULL)
2317 new_st.op = EXEC_NOP;
2322 case ST_IMPLIED_ENDDO:
2323 /* If the do-stmt of this DO construct has a do-construct-name,
2324 the corresponding end-do must be an end-do-stmt (with a matching
2325 name, but in that case we must have seen ST_ENDDO first).
2326 We only complain about this in pedantic mode. */
2327 if (gfc_current_block () != NULL)
2329 ("named block DO at %L requires matching ENDDO name",
2330 &gfc_current_block()->declared_at);
2335 unexpected_statement (st);
2340 accept_statement (st);
2344 /* Parse the statements of OpenMP do/parallel do. */
2346 static gfc_statement
2347 parse_omp_do (gfc_statement omp_st)
2353 accept_statement (omp_st);
2355 cp = gfc_state_stack->tail;
2356 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2357 np = new_level (cp);
2363 st = next_statement ();
2366 else if (st == ST_DO)
2369 unexpected_statement (st);
2373 if (gfc_statement_label != NULL
2374 && gfc_state_stack->previous != NULL
2375 && gfc_state_stack->previous->state == COMP_DO
2376 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
2384 there should be no !$OMP END DO. */
2386 return ST_IMPLIED_ENDDO;
2389 check_do_closure ();
2392 st = next_statement ();
2393 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
2395 if (new_st.op == EXEC_OMP_END_NOWAIT)
2396 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2398 gcc_assert (new_st.op == EXEC_NOP);
2399 gfc_clear_new_st ();
2400 gfc_commit_symbols ();
2401 gfc_warning_check ();
2402 st = next_statement ();
2408 /* Parse the statements of OpenMP atomic directive. */
2411 parse_omp_atomic (void)
2417 accept_statement (ST_OMP_ATOMIC);
2419 cp = gfc_state_stack->tail;
2420 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2421 np = new_level (cp);
2427 st = next_statement ();
2430 else if (st == ST_ASSIGNMENT)
2433 unexpected_statement (st);
2436 accept_statement (st);
2442 /* Parse the statements of an OpenMP structured block. */
2445 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
2447 gfc_statement st, omp_end_st;
2451 accept_statement (omp_st);
2453 cp = gfc_state_stack->tail;
2454 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2455 np = new_level (cp);
2461 case ST_OMP_PARALLEL:
2462 omp_end_st = ST_OMP_END_PARALLEL;
2464 case ST_OMP_PARALLEL_SECTIONS:
2465 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
2467 case ST_OMP_SECTIONS:
2468 omp_end_st = ST_OMP_END_SECTIONS;
2470 case ST_OMP_ORDERED:
2471 omp_end_st = ST_OMP_END_ORDERED;
2473 case ST_OMP_CRITICAL:
2474 omp_end_st = ST_OMP_END_CRITICAL;
2477 omp_end_st = ST_OMP_END_MASTER;
2480 omp_end_st = ST_OMP_END_SINGLE;
2482 case ST_OMP_WORKSHARE:
2483 omp_end_st = ST_OMP_END_WORKSHARE;
2485 case ST_OMP_PARALLEL_WORKSHARE:
2486 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
2494 if (workshare_stmts_only)
2496 /* Inside of !$omp workshare, only
2499 where statements and constructs
2500 forall statements and constructs
2504 are allowed. For !$omp critical these
2505 restrictions apply recursively. */
2508 st = next_statement ();
2519 accept_statement (st);
2522 case ST_WHERE_BLOCK:
2523 parse_where_block ();
2526 case ST_FORALL_BLOCK:
2527 parse_forall_block ();
2530 case ST_OMP_PARALLEL:
2531 case ST_OMP_PARALLEL_SECTIONS:
2532 parse_omp_structured_block (st, false);
2535 case ST_OMP_PARALLEL_WORKSHARE:
2536 case ST_OMP_CRITICAL:
2537 parse_omp_structured_block (st, true);
2540 case ST_OMP_PARALLEL_DO:
2541 st = parse_omp_do (st);
2545 parse_omp_atomic ();
2556 st = next_statement ();
2560 st = parse_executable (ST_NONE);
2563 else if (st == ST_OMP_SECTION
2564 && (omp_st == ST_OMP_SECTIONS
2565 || omp_st == ST_OMP_PARALLEL_SECTIONS))
2567 np = new_level (np);
2571 else if (st != omp_end_st)
2572 unexpected_statement (st);
2574 while (st != omp_end_st);
2578 case EXEC_OMP_END_NOWAIT:
2579 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2581 case EXEC_OMP_CRITICAL:
2582 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
2583 || (new_st.ext.omp_name != NULL
2584 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
2585 gfc_error ("Name after !$omp critical and !$omp end critical does"
2586 " not match at %C");
2587 gfc_free ((char *) new_st.ext.omp_name);
2589 case EXEC_OMP_END_SINGLE:
2590 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
2591 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
2592 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
2593 gfc_free_omp_clauses (new_st.ext.omp_clauses);
2601 gfc_clear_new_st ();
2602 gfc_commit_symbols ();
2603 gfc_warning_check ();
2608 /* Accept a series of executable statements. We return the first
2609 statement that doesn't fit to the caller. Any block statements are
2610 passed on to the correct handler, which usually passes the buck
2613 static gfc_statement
2614 parse_executable (gfc_statement st)
2619 st = next_statement ();
2623 close_flag = check_do_closure ();
2628 case ST_END_PROGRAM:
2631 case ST_END_FUNCTION:
2635 case ST_END_SUBROUTINE:
2640 case ST_SELECT_CASE:
2642 ("%s statement at %C cannot terminate a non-block DO loop",
2643 gfc_ascii_statement (st));
2659 accept_statement (st);
2660 if (close_flag == 1)
2661 return ST_IMPLIED_ENDDO;
2668 case ST_SELECT_CASE:
2669 parse_select_block ();
2674 if (check_do_closure () == 1)
2675 return ST_IMPLIED_ENDDO;
2678 case ST_WHERE_BLOCK:
2679 parse_where_block ();
2682 case ST_FORALL_BLOCK:
2683 parse_forall_block ();
2686 case ST_OMP_PARALLEL:
2687 case ST_OMP_PARALLEL_SECTIONS:
2688 case ST_OMP_SECTIONS:
2689 case ST_OMP_ORDERED:
2690 case ST_OMP_CRITICAL:
2693 parse_omp_structured_block (st, false);
2696 case ST_OMP_WORKSHARE:
2697 case ST_OMP_PARALLEL_WORKSHARE:
2698 parse_omp_structured_block (st, true);
2702 case ST_OMP_PARALLEL_DO:
2703 st = parse_omp_do (st);
2704 if (st == ST_IMPLIED_ENDDO)
2709 parse_omp_atomic ();
2716 st = next_statement ();
2721 /* Parse a series of contained program units. */
2723 static void parse_progunit (gfc_statement);
2726 /* Fix the symbols for sibling functions. These are incorrectly added to
2727 the child namespace as the parser didn't know about this procedure. */
2730 gfc_fixup_sibling_symbols (gfc_symbol * sym, gfc_namespace * siblings)
2734 gfc_symbol *old_sym;
2736 sym->attr.referenced = 1;
2737 for (ns = siblings; ns; ns = ns->sibling)
2739 gfc_find_sym_tree (sym->name, ns, 0, &st);
2741 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
2744 old_sym = st->n.sym;
2745 if ((old_sym->attr.flavor == FL_PROCEDURE
2746 || old_sym->ts.type == BT_UNKNOWN)
2747 && old_sym->ns == ns
2748 && ! old_sym->attr.contained)
2750 /* Replace it with the symbol from the parent namespace. */
2754 /* Free the old (local) symbol. */
2756 if (old_sym->refs == 0)
2757 gfc_free_symbol (old_sym);
2760 /* Do the same for any contained procedures. */
2761 gfc_fixup_sibling_symbols (sym, ns->contained);
2766 parse_contained (int module)
2768 gfc_namespace *ns, *parent_ns;
2769 gfc_state_data s1, s2;
2773 int contains_statements = 0;
2775 push_state (&s1, COMP_CONTAINS, NULL);
2776 parent_ns = gfc_current_ns;
2780 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2782 gfc_current_ns->sibling = parent_ns->contained;
2783 parent_ns->contained = gfc_current_ns;
2785 st = next_statement ();
2794 contains_statements = 1;
2795 accept_statement (st);
2798 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2801 /* For internal procedures, create/update the symbol in the
2802 parent namespace. */
2806 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2808 ("Contained procedure '%s' at %C is already ambiguous",
2809 gfc_new_block->name);
2812 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2813 &gfc_new_block->declared_at) ==
2816 if (st == ST_FUNCTION)
2817 gfc_add_function (&sym->attr, sym->name,
2818 &gfc_new_block->declared_at);
2820 gfc_add_subroutine (&sym->attr, sym->name,
2821 &gfc_new_block->declared_at);
2825 gfc_commit_symbols ();
2828 sym = gfc_new_block;
2830 /* Mark this as a contained function, so it isn't replaced
2831 by other module functions. */
2832 sym->attr.contained = 1;
2833 sym->attr.referenced = 1;
2835 parse_progunit (ST_NONE);
2837 /* Fix up any sibling functions that refer to this one. */
2838 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2839 /* Or refer to any of its alternate entry points. */
2840 for (el = gfc_current_ns->entries; el; el = el->next)
2841 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2843 gfc_current_ns->code = s2.head;
2844 gfc_current_ns = parent_ns;
2849 /* These statements are associated with the end of the host
2851 case ST_END_FUNCTION:
2853 case ST_END_PROGRAM:
2854 case ST_END_SUBROUTINE:
2855 accept_statement (st);
2859 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2860 gfc_ascii_statement (st));
2861 reject_statement ();
2865 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2866 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2868 /* The first namespace in the list is guaranteed to not have
2869 anything (worthwhile) in it. */
2871 gfc_current_ns = parent_ns;
2873 ns = gfc_current_ns->contained;
2874 gfc_current_ns->contained = ns->sibling;
2875 gfc_free_namespace (ns);
2878 if (!contains_statements)
2879 /* This is valid in Fortran 2008. */
2880 gfc_notify_std (GFC_STD_GNU, "Extension: "
2881 "CONTAINS statement without FUNCTION "
2882 "or SUBROUTINE statement at %C");
2886 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2889 parse_progunit (gfc_statement st)
2894 st = parse_spec (st);
2904 accept_statement (st);
2914 st = parse_executable (st);
2925 accept_statement (st);
2932 unexpected_statement (st);
2933 reject_statement ();
2934 st = next_statement ();
2940 for (p = gfc_state_stack; p; p = p->previous)
2941 if (p->state == COMP_CONTAINS)
2944 if (gfc_find_state (COMP_MODULE) == SUCCESS)
2949 gfc_error ("CONTAINS statement at %C is already in a contained "
2951 st = next_statement ();
2955 parse_contained (0);
2958 gfc_current_ns->code = gfc_state_stack->head;
2962 /* Come here to complain about a global symbol already in use as
2966 global_used (gfc_gsymbol *sym, locus *where)
2971 where = &gfc_current_locus;
2981 case GSYM_SUBROUTINE:
2982 name = "SUBROUTINE";
2987 case GSYM_BLOCK_DATA:
2988 name = "BLOCK DATA";
2994 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
2998 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
2999 sym->name, where, name, &sym->where);
3003 /* Parse a block data program unit. */
3006 parse_block_data (void)
3009 static locus blank_locus;
3010 static int blank_block=0;
3013 gfc_current_ns->proc_name = gfc_new_block;
3014 gfc_current_ns->is_block_data = 1;
3016 if (gfc_new_block == NULL)
3019 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3020 "prior BLOCK DATA at %L", &blank_locus);
3024 blank_locus = gfc_current_locus;
3029 s = gfc_get_gsymbol (gfc_new_block->name);
3030 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
3031 global_used(s, NULL);
3034 s->type = GSYM_BLOCK_DATA;
3035 s->where = gfc_current_locus;
3040 st = parse_spec (ST_NONE);
3042 while (st != ST_END_BLOCK_DATA)
3044 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3045 gfc_ascii_statement (st));
3046 reject_statement ();
3047 st = next_statement ();
3052 /* Parse a module subprogram. */
3060 s = gfc_get_gsymbol (gfc_new_block->name);
3061 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
3062 global_used(s, NULL);
3065 s->type = GSYM_MODULE;
3066 s->where = gfc_current_locus;
3070 st = parse_spec (ST_NONE);
3079 parse_contained (1);
3083 accept_statement (st);
3087 gfc_error ("Unexpected %s statement in MODULE at %C",
3088 gfc_ascii_statement (st));
3090 reject_statement ();
3091 st = next_statement ();
3097 /* Add a procedure name to the global symbol table. */
3100 add_global_procedure (int sub)
3104 s = gfc_get_gsymbol(gfc_new_block->name);
3107 || (s->type != GSYM_UNKNOWN && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3108 global_used(s, NULL);
3111 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3112 s->where = gfc_current_locus;
3118 /* Add a program to the global symbol table. */
3121 add_global_program (void)
3125 if (gfc_new_block == NULL)
3127 s = gfc_get_gsymbol (gfc_new_block->name);
3129 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
3130 global_used(s, NULL);
3133 s->type = GSYM_PROGRAM;
3134 s->where = gfc_current_locus;
3140 /* Top level parser. */
3143 gfc_parse_file (void)
3145 int seen_program, errors_before, errors;
3146 gfc_state_data top, s;
3150 top.state = COMP_NONE;
3152 top.previous = NULL;
3153 top.head = top.tail = NULL;
3154 top.do_variable = NULL;
3156 gfc_state_stack = ⊤
3158 gfc_clear_new_st ();
3160 gfc_statement_label = NULL;
3162 if (setjmp (eof_buf))
3163 return FAILURE; /* Come here on unexpected EOF */
3167 /* Exit early for empty files. */
3173 st = next_statement ();
3182 goto duplicate_main;
3184 prog_locus = gfc_current_locus;
3186 push_state (&s, COMP_PROGRAM, gfc_new_block);
3187 main_program_symbol(gfc_current_ns);
3188 accept_statement (st);
3189 add_global_program ();
3190 parse_progunit (ST_NONE);
3194 add_global_procedure (1);
3195 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
3196 accept_statement (st);
3197 parse_progunit (ST_NONE);
3201 add_global_procedure (0);
3202 push_state (&s, COMP_FUNCTION, gfc_new_block);
3203 accept_statement (st);
3204 parse_progunit (ST_NONE);
3208 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
3209 accept_statement (st);
3210 parse_block_data ();
3214 push_state (&s, COMP_MODULE, gfc_new_block);
3215 accept_statement (st);
3217 gfc_get_errors (NULL, &errors_before);
3221 /* Anything else starts a nameless main program block. */
3224 goto duplicate_main;
3226 prog_locus = gfc_current_locus;
3228 push_state (&s, COMP_PROGRAM, gfc_new_block);
3229 main_program_symbol(gfc_current_ns);
3230 parse_progunit (st);
3234 gfc_current_ns->code = s.head;
3236 gfc_resolve (gfc_current_ns);
3238 /* Dump the parse tree if requested. */
3239 if (gfc_option.verbose)
3240 gfc_show_namespace (gfc_current_ns);
3242 gfc_get_errors (NULL, &errors);
3243 if (s.state == COMP_MODULE)
3245 gfc_dump_module (s.sym->name, errors_before == errors);
3247 gfc_generate_module_code (gfc_current_ns);
3252 gfc_generate_code (gfc_current_ns);
3263 /* If we see a duplicate main program, shut down. If the second
3264 instance is an implied main program, ie data decls or executable
3265 statements, we're in for lots of errors. */
3266 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
3267 reject_statement ();