2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
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
30 /* Current statement label. Zero means no statement label. Because new_st
31 can get wiped during statement matching, we have to keep it separate. */
33 gfc_st_label *gfc_statement_label;
35 static locus label_locus;
36 static jmp_buf eof_buf;
38 gfc_state_data *gfc_state_stack;
40 /* TODO: Re-order functions to kill these forward decls. */
41 static void check_statement_label (gfc_statement);
42 static void undo_new_statement (void);
43 static void reject_statement (void);
46 /* A sort of half-matching function. We try to match the word on the
47 input with the passed string. If this succeeds, we call the
48 keyword-dependent matching function that will match the rest of the
49 statement. For single keywords, the matching subroutine is
53 match_word (const char *str, match (*subr) (void), locus *old_locus)
68 gfc_current_locus = *old_locus;
76 /* Figure out what the next statement is, (mostly) regardless of
77 proper ordering. The do...while(0) is there to prevent if/else
80 #define match(keyword, subr, st) \
82 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
85 undo_new_statement (); \
89 decode_statement (void)
100 gfc_clear_error (); /* Clear any pending errors. */
101 gfc_clear_warning (); /* Clear any pending warnings. */
103 if (gfc_match_eos () == MATCH_YES)
106 old_locus = gfc_current_locus;
108 /* Try matching a data declaration or function declaration. The
109 input "REALFUNCTIONA(N)" can mean several things in different
110 contexts, so it (and its relatives) get special treatment. */
112 if (gfc_current_state () == COMP_NONE
113 || gfc_current_state () == COMP_INTERFACE
114 || gfc_current_state () == COMP_CONTAINS)
116 m = gfc_match_function_decl ();
119 else if (m == MATCH_ERROR)
123 gfc_current_locus = old_locus;
126 /* Match statements whose error messages are meant to be overwritten
127 by something better. */
129 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
130 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
131 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
133 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
134 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
136 /* Try to match a subroutine statement, which has the same optional
137 prefixes that functions can have. */
139 if (gfc_match_subroutine () == MATCH_YES)
140 return ST_SUBROUTINE;
142 gfc_current_locus = old_locus;
144 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
145 might begin with a block label. The match functions for these
146 statements are unusual in that their keyword is not seen before
147 the matcher is called. */
149 if (gfc_match_if (&st) == MATCH_YES)
152 gfc_current_locus = old_locus;
154 if (gfc_match_where (&st) == MATCH_YES)
157 gfc_current_locus = old_locus;
159 if (gfc_match_forall (&st) == MATCH_YES)
162 gfc_current_locus = old_locus;
164 match (NULL, gfc_match_do, ST_DO);
165 match (NULL, gfc_match_select, ST_SELECT_CASE);
167 /* General statement matching: Instead of testing every possible
168 statement, we eliminate most possibilities by peeking at the
171 c = gfc_peek_char ();
176 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
177 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
178 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
182 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
183 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
184 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
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)
263 match ("protected", gfc_match_protected, ST_ATTR_DECL);
267 match ("read", gfc_match_read, ST_READ);
268 match ("return", gfc_match_return, ST_RETURN);
269 match ("rewind", gfc_match_rewind, ST_REWIND);
273 match ("sequence", gfc_match_eos, ST_SEQUENCE);
274 match ("stop", gfc_match_stop, ST_STOP);
275 match ("save", gfc_match_save, ST_ATTR_DECL);
279 match ("target", gfc_match_target, ST_ATTR_DECL);
280 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
284 match ("use", gfc_match_use, ST_USE);
288 match ("value", gfc_match_value, ST_ATTR_DECL);
289 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
293 match ("write", gfc_match_write, ST_WRITE);
297 /* All else has failed, so give up. See if any of the matchers has
298 stored an error message of some sort. */
300 if (gfc_error_check () == 0)
301 gfc_error_now ("Unclassifiable statement at %C");
305 gfc_error_recovery ();
311 decode_omp_directive (void)
320 gfc_clear_error (); /* Clear any pending errors. */
321 gfc_clear_warning (); /* Clear any pending warnings. */
325 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
326 "or ELEMENTAL procedures");
327 gfc_error_recovery ();
331 old_locus = gfc_current_locus;
333 /* General OpenMP directive matching: Instead of testing every possible
334 statement, we eliminate most possibilities by peeking at the
337 c = gfc_peek_char ();
342 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
345 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
348 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
351 match ("do", gfc_match_omp_do, ST_OMP_DO);
354 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
355 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
356 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
357 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
358 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
359 match ("end parallel sections", gfc_match_omp_eos,
360 ST_OMP_END_PARALLEL_SECTIONS);
361 match ("end parallel workshare", gfc_match_omp_eos,
362 ST_OMP_END_PARALLEL_WORKSHARE);
363 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
364 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
365 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
366 match ("end workshare", gfc_match_omp_end_nowait,
367 ST_OMP_END_WORKSHARE);
370 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
373 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
376 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
379 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
380 match ("parallel sections", gfc_match_omp_parallel_sections,
381 ST_OMP_PARALLEL_SECTIONS);
382 match ("parallel workshare", gfc_match_omp_parallel_workshare,
383 ST_OMP_PARALLEL_WORKSHARE);
384 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
387 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
388 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
389 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
392 match ("threadprivate", gfc_match_omp_threadprivate,
393 ST_OMP_THREADPRIVATE);
395 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
399 /* All else has failed, so give up. See if any of the matchers has
400 stored an error message of some sort. */
402 if (gfc_error_check () == 0)
403 gfc_error_now ("Unclassifiable OpenMP directive at %C");
407 gfc_error_recovery ();
415 /* Get the next statement in free form source. */
421 int c, d, cnt, at_bol;
423 at_bol = gfc_at_bol ();
424 gfc_gobble_whitespace ();
426 c = gfc_peek_char ();
430 /* Found a statement label? */
431 m = gfc_match_st_label (&gfc_statement_label);
433 d = gfc_peek_char ();
434 if (m != MATCH_YES || !gfc_is_whitespace (d))
436 gfc_match_small_literal_int (&c, &cnt);
439 gfc_error_now ("Too many digits in statement label at %C");
442 gfc_error_now ("Zero is not a valid statement label at %C");
445 c = gfc_next_char ();
448 if (!gfc_is_whitespace (c))
449 gfc_error_now ("Non-numeric character in statement label at %C");
455 label_locus = gfc_current_locus;
457 gfc_gobble_whitespace ();
459 if (at_bol && gfc_peek_char () == ';')
461 gfc_error_now ("Semicolon at %C needs to be preceded by "
463 gfc_next_char (); /* Eat up the semicolon. */
467 if (gfc_match_eos () == MATCH_YES)
469 gfc_warning_now ("Ignoring statement label in empty statement "
471 gfc_free_st_label (gfc_statement_label);
472 gfc_statement_label = NULL;
479 /* Comments have already been skipped by the time we get here,
480 except for OpenMP directives. */
481 if (gfc_option.flag_openmp)
485 c = gfc_next_char ();
486 for (i = 0; i < 5; i++, c = gfc_next_char ())
487 gcc_assert (c == "!$omp"[i]);
489 gcc_assert (c == ' ');
490 gfc_gobble_whitespace ();
491 return decode_omp_directive ();
495 if (at_bol && c == ';')
497 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
498 gfc_next_char (); /* Eat up the semicolon. */
502 return decode_statement ();
506 /* Get the next statement in fixed-form source. */
511 int label, digit_flag, i;
516 return decode_statement ();
518 /* Skip past the current label field, parsing a statement label if
519 one is there. This is a weird number parser, since the number is
520 contained within five columns and can have any kind of embedded
521 spaces. We also check for characters that make the rest of the
527 for (i = 0; i < 5; i++)
529 c = gfc_next_char_literal (0);
546 label = label * 10 + c - '0';
547 label_locus = gfc_current_locus;
551 /* Comments have already been skipped by the time we get
552 here, except for OpenMP directives. */
554 if (gfc_option.flag_openmp)
556 for (i = 0; i < 5; i++, c = gfc_next_char_literal (0))
557 gcc_assert (TOLOWER (c) == "*$omp"[i]);
559 if (c != ' ' && c != '0')
561 gfc_buffer_error (0);
562 gfc_error ("Bad continuation line at %C");
566 return decode_omp_directive ();
570 /* Comments have already been skipped by the time we get
571 here so don't bother checking for them. */
574 gfc_buffer_error (0);
575 gfc_error ("Non-numeric character in statement label at %C");
583 gfc_warning_now ("Zero is not a valid statement label at %C");
586 /* We've found a valid statement label. */
587 gfc_statement_label = gfc_get_st_label (label);
591 /* Since this line starts a statement, it cannot be a continuation
592 of a previous statement. If we see something here besides a
593 space or zero, it must be a bad continuation line. */
595 c = gfc_next_char_literal (0);
599 if (c != ' ' && c != '0')
601 gfc_buffer_error (0);
602 gfc_error ("Bad continuation line at %C");
606 /* Now that we've taken care of the statement label columns, we have
607 to make sure that the first nonblank character is not a '!'. If
608 it is, the rest of the line is a comment. */
612 loc = gfc_current_locus;
613 c = gfc_next_char_literal (0);
615 while (gfc_is_whitespace (c));
619 gfc_current_locus = loc;
623 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
627 if (gfc_match_eos () == MATCH_YES)
630 /* At this point, we've got a nonblank statement to parse. */
631 return decode_statement ();
635 gfc_warning ("Ignoring statement label in empty statement at %C");
641 /* Return the next non-ST_NONE statement to the caller. We also worry
642 about including files and the ends of include files at this stage. */
645 next_statement (void)
649 gfc_new_block = NULL;
653 gfc_statement_label = NULL;
654 gfc_buffer_error (1);
658 if ((gfc_option.warn_line_truncation || gfc_current_form == FORM_FREE)
659 && gfc_current_locus.lb
660 && gfc_current_locus.lb->truncated)
661 gfc_warning_now ("Line truncated at %C");
666 gfc_skip_comments ();
674 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
680 gfc_buffer_error (0);
683 check_statement_label (st);
689 /****************************** Parser ***********************************/
691 /* The parser subroutines are of type 'try' that fail if the file ends
694 /* Macros that expand to case-labels for various classes of
695 statements. Start with executable statements that directly do
698 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
699 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
700 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
701 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
702 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
703 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
704 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
705 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
708 /* Statements that mark other executable statements. */
710 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
711 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
712 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
713 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
714 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
715 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
717 /* Declaration statements */
719 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
720 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
721 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE
723 /* Block end statements. Errors associated with interchanging these
724 are detected in gfc_match_end(). */
726 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
727 case ST_END_PROGRAM: case ST_END_SUBROUTINE
730 /* Push a new state onto the stack. */
733 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
735 p->state = new_state;
736 p->previous = gfc_state_stack;
738 p->head = p->tail = NULL;
739 p->do_variable = NULL;
744 /* 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");
1065 case ST_MODULE_PROC:
1066 p = "MODULE PROCEDURE";
1101 case ST_WHERE_BLOCK: /* Fall through */
1109 p = _("assignment");
1111 case ST_POINTER_ASSIGNMENT:
1112 p = _("pointer assignment");
1114 case ST_SELECT_CASE:
1123 case ST_STATEMENT_FUNCTION:
1124 p = "STATEMENT FUNCTION";
1126 case ST_LABEL_ASSIGNMENT:
1127 p = "LABEL ASSIGNMENT";
1130 p = "ENUM DEFINITION";
1133 p = "ENUMERATOR DEFINITION";
1141 case ST_OMP_BARRIER:
1142 p = "!$OMP BARRIER";
1144 case ST_OMP_CRITICAL:
1145 p = "!$OMP CRITICAL";
1150 case ST_OMP_END_CRITICAL:
1151 p = "!$OMP END CRITICAL";
1156 case ST_OMP_END_MASTER:
1157 p = "!$OMP END MASTER";
1159 case ST_OMP_END_ORDERED:
1160 p = "!$OMP END ORDERED";
1162 case ST_OMP_END_PARALLEL:
1163 p = "!$OMP END PARALLEL";
1165 case ST_OMP_END_PARALLEL_DO:
1166 p = "!$OMP END PARALLEL DO";
1168 case ST_OMP_END_PARALLEL_SECTIONS:
1169 p = "!$OMP END PARALLEL SECTIONS";
1171 case ST_OMP_END_PARALLEL_WORKSHARE:
1172 p = "!$OMP END PARALLEL WORKSHARE";
1174 case ST_OMP_END_SECTIONS:
1175 p = "!$OMP END SECTIONS";
1177 case ST_OMP_END_SINGLE:
1178 p = "!$OMP END SINGLE";
1180 case ST_OMP_END_WORKSHARE:
1181 p = "!$OMP END WORKSHARE";
1189 case ST_OMP_ORDERED:
1190 p = "!$OMP ORDERED";
1192 case ST_OMP_PARALLEL:
1193 p = "!$OMP PARALLEL";
1195 case ST_OMP_PARALLEL_DO:
1196 p = "!$OMP PARALLEL DO";
1198 case ST_OMP_PARALLEL_SECTIONS:
1199 p = "!$OMP PARALLEL SECTIONS";
1201 case ST_OMP_PARALLEL_WORKSHARE:
1202 p = "!$OMP PARALLEL WORKSHARE";
1204 case ST_OMP_SECTIONS:
1205 p = "!$OMP SECTIONS";
1207 case ST_OMP_SECTION:
1208 p = "!$OMP SECTION";
1213 case ST_OMP_THREADPRIVATE:
1214 p = "!$OMP THREADPRIVATE";
1216 case ST_OMP_WORKSHARE:
1217 p = "!$OMP WORKSHARE";
1220 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1227 /* Create a symbol for the main program and assign it to ns->proc_name. */
1230 main_program_symbol (gfc_namespace *ns)
1232 gfc_symbol *main_program;
1233 symbol_attribute attr;
1235 gfc_get_symbol ("MAIN__", ns, &main_program);
1236 gfc_clear_attr (&attr);
1237 attr.flavor = FL_PROCEDURE;
1238 attr.proc = PROC_UNKNOWN;
1239 attr.subroutine = 1;
1240 attr.access = ACCESS_PUBLIC;
1241 attr.is_main_program = 1;
1242 main_program->attr = attr;
1243 main_program->declared_at = gfc_current_locus;
1244 ns->proc_name = main_program;
1245 gfc_commit_symbols ();
1249 /* Do whatever is necessary to accept the last statement. */
1252 accept_statement (gfc_statement st)
1260 case ST_IMPLICIT_NONE:
1261 gfc_set_implicit_none ();
1270 gfc_current_ns->proc_name = gfc_new_block;
1273 /* If the statement is the end of a block, lay down a special code
1274 that allows a branch to the end of the block from within the
1279 if (gfc_statement_label != NULL)
1281 new_st.op = EXEC_NOP;
1287 /* The end-of-program unit statements do not get the special
1288 marker and require a statement of some sort if they are a
1291 case ST_END_PROGRAM:
1292 case ST_END_FUNCTION:
1293 case ST_END_SUBROUTINE:
1294 if (gfc_statement_label != NULL)
1296 new_st.op = EXEC_RETURN;
1312 gfc_commit_symbols ();
1313 gfc_warning_check ();
1314 gfc_clear_new_st ();
1318 /* Undo anything tentative that has been built for the current
1322 reject_statement (void)
1324 gfc_new_block = NULL;
1325 gfc_undo_symbols ();
1326 gfc_clear_warning ();
1327 undo_new_statement ();
1331 /* Generic complaint about an out of order statement. We also do
1332 whatever is necessary to clean up. */
1335 unexpected_statement (gfc_statement st)
1337 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1339 reject_statement ();
1343 /* Given the next statement seen by the matcher, make sure that it is
1344 in proper order with the last. This subroutine is initialized by
1345 calling it with an argument of ST_NONE. If there is a problem, we
1346 issue an error and return FAILURE. Otherwise we return SUCCESS.
1348 Individual parsers need to verify that the statements seen are
1349 valid before calling here, ie ENTRY statements are not allowed in
1350 INTERFACE blocks. The following diagram is taken from the standard:
1352 +---------------------------------------+
1353 | program subroutine function module |
1354 +---------------------------------------+
1356 +---------------------------------------+
1358 +---------------------------------------+
1360 | +-----------+------------------+
1361 | | parameter | implicit |
1362 | +-----------+------------------+
1363 | format | | derived type |
1364 | entry | parameter | interface |
1365 | | data | specification |
1366 | | | statement func |
1367 | +-----------+------------------+
1368 | | data | executable |
1369 +--------+-----------+------------------+
1371 +---------------------------------------+
1372 | internal module/subprogram |
1373 +---------------------------------------+
1375 +---------------------------------------+
1382 { ORDER_START, ORDER_USE, ORDER_IMPORT, ORDER_IMPLICIT_NONE,
1383 ORDER_IMPLICIT, ORDER_SPEC, ORDER_EXEC
1386 gfc_statement last_statement;
1392 verify_st_order (st_state *p, gfc_statement st)
1398 p->state = ORDER_START;
1402 if (p->state > ORDER_USE)
1404 p->state = ORDER_USE;
1408 if (p->state > ORDER_IMPORT)
1410 p->state = ORDER_IMPORT;
1413 case ST_IMPLICIT_NONE:
1414 if (p->state > ORDER_IMPLICIT_NONE)
1417 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1418 statement disqualifies a USE but not an IMPLICIT NONE.
1419 Duplicate IMPLICIT NONEs are caught when the implicit types
1422 p->state = ORDER_IMPLICIT_NONE;
1426 if (p->state > ORDER_IMPLICIT)
1428 p->state = ORDER_IMPLICIT;
1433 if (p->state < ORDER_IMPLICIT_NONE)
1434 p->state = ORDER_IMPLICIT_NONE;
1438 if (p->state >= ORDER_EXEC)
1440 if (p->state < ORDER_IMPLICIT)
1441 p->state = ORDER_IMPLICIT;
1445 if (p->state < ORDER_SPEC)
1446 p->state = ORDER_SPEC;
1451 case ST_DERIVED_DECL:
1453 if (p->state >= ORDER_EXEC)
1455 if (p->state < ORDER_SPEC)
1456 p->state = ORDER_SPEC;
1461 if (p->state < ORDER_EXEC)
1462 p->state = ORDER_EXEC;
1466 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1467 gfc_ascii_statement (st));
1470 /* All is well, record the statement in case we need it next time. */
1471 p->where = gfc_current_locus;
1472 p->last_statement = st;
1476 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1477 gfc_ascii_statement (st),
1478 gfc_ascii_statement (p->last_statement), &p->where);
1484 /* Handle an unexpected end of file. This is a show-stopper... */
1486 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1489 unexpected_eof (void)
1493 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1495 /* Memory cleanup. Move to "second to last". */
1496 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1499 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1502 longjmp (eof_buf, 1);
1506 /* Parse a derived type. */
1509 parse_derived (void)
1511 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1514 gfc_symbol *derived_sym = NULL;
1520 accept_statement (ST_DERIVED_DECL);
1521 push_state (&s, COMP_DERIVED, gfc_new_block);
1523 gfc_new_block->component_access = ACCESS_PUBLIC;
1530 while (compiling_type)
1532 st = next_statement ();
1539 accept_statement (st);
1546 if (!seen_component)
1548 gfc_error ("Derived type definition at %C has no components");
1552 accept_statement (ST_END_TYPE);
1556 if (gfc_find_state (COMP_MODULE) == FAILURE)
1558 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1566 gfc_error ("PRIVATE statement at %C must precede "
1567 "structure components");
1574 gfc_error ("Duplicate PRIVATE statement at %C");
1578 s.sym->component_access = ACCESS_PRIVATE;
1579 accept_statement (ST_PRIVATE);
1586 gfc_error ("SEQUENCE statement at %C must precede "
1587 "structure components");
1592 if (gfc_current_block ()->attr.sequence)
1593 gfc_warning ("SEQUENCE attribute at %C already specified in "
1598 gfc_error ("Duplicate SEQUENCE statement at %C");
1603 gfc_add_sequence (&gfc_current_block ()->attr,
1604 gfc_current_block ()->name, NULL);
1608 unexpected_statement (st);
1613 /* need to verify that all fields of the derived type are
1614 * interoperable with C if the type is declared to be bind(c)
1616 derived_sym = gfc_current_block();
1618 /* Look for allocatable components. */
1619 sym = gfc_current_block ();
1620 for (c = sym->components; c; c = c->next)
1623 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.alloc_comp))
1625 sym->attr.alloc_comp = 1;
1634 /* Parse an ENUM. */
1643 int seen_enumerator = 0;
1647 push_state (&s, COMP_ENUM, gfc_new_block);
1651 while (compiling_enum)
1653 st = next_statement ();
1661 seen_enumerator = 1;
1662 accept_statement (st);
1667 if (!seen_enumerator)
1669 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1672 accept_statement (st);
1676 gfc_free_enum_history ();
1677 unexpected_statement (st);
1685 /* Parse an interface. We must be able to deal with the possibility
1686 of recursive interfaces. The parse_spec() subroutine is mutually
1687 recursive with parse_interface(). */
1689 static gfc_statement parse_spec (gfc_statement);
1692 parse_interface (void)
1694 gfc_compile_state new_state, current_state;
1695 gfc_symbol *prog_unit, *sym;
1696 gfc_interface_info save;
1697 gfc_state_data s1, s2;
1701 accept_statement (ST_INTERFACE);
1703 current_interface.ns = gfc_current_ns;
1704 save = current_interface;
1706 sym = (current_interface.type == INTERFACE_GENERIC
1707 || current_interface.type == INTERFACE_USER_OP)
1708 ? gfc_new_block : NULL;
1710 push_state (&s1, COMP_INTERFACE, sym);
1711 current_state = COMP_NONE;
1714 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1716 st = next_statement ();
1723 new_state = COMP_SUBROUTINE;
1724 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1725 gfc_new_block->formal, NULL);
1729 new_state = COMP_FUNCTION;
1730 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1731 gfc_new_block->formal, NULL);
1734 case ST_MODULE_PROC: /* The module procedure matcher makes
1735 sure the context is correct. */
1736 accept_statement (st);
1737 gfc_free_namespace (gfc_current_ns);
1740 case ST_END_INTERFACE:
1741 gfc_free_namespace (gfc_current_ns);
1742 gfc_current_ns = current_interface.ns;
1746 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1747 gfc_ascii_statement (st));
1748 reject_statement ();
1749 gfc_free_namespace (gfc_current_ns);
1754 /* Make sure that a generic interface has only subroutines or
1755 functions and that the generic name has the right attribute. */
1756 if (current_interface.type == INTERFACE_GENERIC)
1758 if (current_state == COMP_NONE)
1760 if (new_state == COMP_FUNCTION)
1761 gfc_add_function (&sym->attr, sym->name, NULL);
1762 else if (new_state == COMP_SUBROUTINE)
1763 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1765 current_state = new_state;
1769 if (new_state != current_state)
1771 if (new_state == COMP_SUBROUTINE)
1772 gfc_error ("SUBROUTINE at %C does not belong in a "
1773 "generic function interface");
1775 if (new_state == COMP_FUNCTION)
1776 gfc_error ("FUNCTION at %C does not belong in a "
1777 "generic subroutine interface");
1782 push_state (&s2, new_state, gfc_new_block);
1783 accept_statement (st);
1784 prog_unit = gfc_new_block;
1785 prog_unit->formal_ns = gfc_current_ns;
1786 proc_locus = gfc_current_locus;
1789 /* Read data declaration statements. */
1790 st = parse_spec (ST_NONE);
1792 /* Since the interface block does not permit an IMPLICIT statement,
1793 the default type for the function or the result must be taken
1794 from the formal namespace. */
1795 if (new_state == COMP_FUNCTION)
1797 if (prog_unit->result == prog_unit
1798 && prog_unit->ts.type == BT_UNKNOWN)
1799 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
1800 else if (prog_unit->result != prog_unit
1801 && prog_unit->result->ts.type == BT_UNKNOWN)
1802 gfc_set_default_type (prog_unit->result, 1,
1803 prog_unit->formal_ns);
1806 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1808 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1809 gfc_ascii_statement (st));
1810 reject_statement ();
1814 current_interface = save;
1815 gfc_add_interface (prog_unit);
1818 if (current_interface.ns
1819 && current_interface.ns->proc_name
1820 && strcmp (current_interface.ns->proc_name->name,
1821 prog_unit->name) == 0)
1822 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
1823 "enclosing procedure", prog_unit->name, &proc_locus);
1832 /* Parse a set of specification statements. Returns the statement
1833 that doesn't fit. */
1835 static gfc_statement
1836 parse_spec (gfc_statement st)
1840 verify_st_order (&ss, ST_NONE);
1842 st = next_statement ();
1852 case ST_DATA: /* Not allowed in interfaces */
1853 if (gfc_current_state () == COMP_INTERFACE)
1860 case ST_IMPLICIT_NONE:
1865 case ST_DERIVED_DECL:
1867 if (verify_st_order (&ss, st) == FAILURE)
1869 reject_statement ();
1870 st = next_statement ();
1880 case ST_DERIVED_DECL:
1886 if (gfc_current_state () != COMP_MODULE)
1888 gfc_error ("%s statement must appear in a MODULE",
1889 gfc_ascii_statement (st));
1893 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1895 gfc_error ("%s statement at %C follows another accessibility "
1896 "specification", gfc_ascii_statement (st));
1900 gfc_current_ns->default_access = (st == ST_PUBLIC)
1901 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1905 case ST_STATEMENT_FUNCTION:
1906 if (gfc_current_state () == COMP_MODULE)
1908 unexpected_statement (st);
1916 accept_statement (st);
1917 st = next_statement ();
1921 accept_statement (st);
1923 st = next_statement ();
1934 /* Parse a WHERE block, (not a simple WHERE statement). */
1937 parse_where_block (void)
1939 int seen_empty_else;
1944 accept_statement (ST_WHERE_BLOCK);
1945 top = gfc_state_stack->tail;
1947 push_state (&s, COMP_WHERE, gfc_new_block);
1949 d = add_statement ();
1950 d->expr = top->expr;
1956 seen_empty_else = 0;
1960 st = next_statement ();
1966 case ST_WHERE_BLOCK:
1967 parse_where_block ();
1972 accept_statement (st);
1976 if (seen_empty_else)
1978 gfc_error ("ELSEWHERE statement at %C follows previous "
1979 "unmasked ELSEWHERE");
1983 if (new_st.expr == NULL)
1984 seen_empty_else = 1;
1986 d = new_level (gfc_state_stack->head);
1988 d->expr = new_st.expr;
1990 accept_statement (st);
1995 accept_statement (st);
1999 gfc_error ("Unexpected %s statement in WHERE block at %C",
2000 gfc_ascii_statement (st));
2001 reject_statement ();
2005 while (st != ST_END_WHERE);
2011 /* Parse a FORALL block (not a simple FORALL statement). */
2014 parse_forall_block (void)
2020 accept_statement (ST_FORALL_BLOCK);
2021 top = gfc_state_stack->tail;
2023 push_state (&s, COMP_FORALL, gfc_new_block);
2025 d = add_statement ();
2026 d->op = EXEC_FORALL;
2031 st = next_statement ();
2036 case ST_POINTER_ASSIGNMENT:
2039 accept_statement (st);
2042 case ST_WHERE_BLOCK:
2043 parse_where_block ();
2046 case ST_FORALL_BLOCK:
2047 parse_forall_block ();
2051 accept_statement (st);
2058 gfc_error ("Unexpected %s statement in FORALL block at %C",
2059 gfc_ascii_statement (st));
2061 reject_statement ();
2065 while (st != ST_END_FORALL);
2071 static gfc_statement parse_executable (gfc_statement);
2073 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2076 parse_if_block (void)
2085 accept_statement (ST_IF_BLOCK);
2087 top = gfc_state_stack->tail;
2088 push_state (&s, COMP_IF, gfc_new_block);
2090 new_st.op = EXEC_IF;
2091 d = add_statement ();
2093 d->expr = top->expr;
2099 st = parse_executable (ST_NONE);
2109 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2110 "statement at %L", &else_locus);
2112 reject_statement ();
2116 d = new_level (gfc_state_stack->head);
2118 d->expr = new_st.expr;
2120 accept_statement (st);
2127 gfc_error ("Duplicate ELSE statements at %L and %C",
2129 reject_statement ();
2134 else_locus = gfc_current_locus;
2136 d = new_level (gfc_state_stack->head);
2139 accept_statement (st);
2147 unexpected_statement (st);
2151 while (st != ST_ENDIF);
2154 accept_statement (st);
2158 /* Parse a SELECT block. */
2161 parse_select_block (void)
2167 accept_statement (ST_SELECT_CASE);
2169 cp = gfc_state_stack->tail;
2170 push_state (&s, COMP_SELECT, gfc_new_block);
2172 /* Make sure that the next statement is a CASE or END SELECT. */
2175 st = next_statement ();
2178 if (st == ST_END_SELECT)
2180 /* Empty SELECT CASE is OK. */
2181 accept_statement (st);
2188 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2191 reject_statement ();
2194 /* At this point, we're got a nonempty select block. */
2195 cp = new_level (cp);
2198 accept_statement (st);
2202 st = parse_executable (ST_NONE);
2209 cp = new_level (gfc_state_stack->head);
2211 gfc_clear_new_st ();
2213 accept_statement (st);
2219 /* Can't have an executable statement because of
2220 parse_executable(). */
2222 unexpected_statement (st);
2226 while (st != ST_END_SELECT);
2229 accept_statement (st);
2233 /* Given a symbol, make sure it is not an iteration variable for a DO
2234 statement. This subroutine is called when the symbol is seen in a
2235 context that causes it to become redefined. If the symbol is an
2236 iterator, we generate an error message and return nonzero. */
2239 gfc_check_do_variable (gfc_symtree *st)
2243 for (s=gfc_state_stack; s; s = s->previous)
2244 if (s->do_variable == st)
2246 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2247 "loop beginning at %L", st->name, &s->head->loc);
2255 /* Checks to see if the current statement label closes an enddo.
2256 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2257 an error) if it incorrectly closes an ENDDO. */
2260 check_do_closure (void)
2264 if (gfc_statement_label == NULL)
2267 for (p = gfc_state_stack; p; p = p->previous)
2268 if (p->state == COMP_DO)
2272 return 0; /* No loops to close */
2274 if (p->ext.end_do_label == gfc_statement_label)
2277 if (p == gfc_state_stack)
2280 gfc_error ("End of nonblock DO statement at %C is within another block");
2284 /* At this point, the label doesn't terminate the innermost loop.
2285 Make sure it doesn't terminate another one. */
2286 for (; p; p = p->previous)
2287 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
2289 gfc_error ("End of nonblock DO statement at %C is interwoven "
2290 "with another DO loop");
2298 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2299 handled inside of parse_executable(), because they aren't really
2303 parse_do_block (void)
2310 s.ext.end_do_label = new_st.label;
2312 if (new_st.ext.iterator != NULL)
2313 stree = new_st.ext.iterator->var->symtree;
2317 accept_statement (ST_DO);
2319 top = gfc_state_stack->tail;
2320 push_state (&s, COMP_DO, gfc_new_block);
2322 s.do_variable = stree;
2324 top->block = new_level (top);
2325 top->block->op = EXEC_DO;
2328 st = parse_executable (ST_NONE);
2336 if (s.ext.end_do_label != NULL
2337 && s.ext.end_do_label != gfc_statement_label)
2338 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
2341 if (gfc_statement_label != NULL)
2343 new_st.op = EXEC_NOP;
2348 case ST_IMPLIED_ENDDO:
2349 /* If the do-stmt of this DO construct has a do-construct-name,
2350 the corresponding end-do must be an end-do-stmt (with a matching
2351 name, but in that case we must have seen ST_ENDDO first).
2352 We only complain about this in pedantic mode. */
2353 if (gfc_current_block () != NULL)
2354 gfc_error_now ("named block DO at %L requires matching ENDDO name",
2355 &gfc_current_block()->declared_at);
2360 unexpected_statement (st);
2365 accept_statement (st);
2369 /* Parse the statements of OpenMP do/parallel do. */
2371 static gfc_statement
2372 parse_omp_do (gfc_statement omp_st)
2378 accept_statement (omp_st);
2380 cp = gfc_state_stack->tail;
2381 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2382 np = new_level (cp);
2388 st = next_statement ();
2391 else if (st == ST_DO)
2394 unexpected_statement (st);
2398 if (gfc_statement_label != NULL
2399 && gfc_state_stack->previous != NULL
2400 && gfc_state_stack->previous->state == COMP_DO
2401 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
2409 there should be no !$OMP END DO. */
2411 return ST_IMPLIED_ENDDO;
2414 check_do_closure ();
2417 st = next_statement ();
2418 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
2420 if (new_st.op == EXEC_OMP_END_NOWAIT)
2421 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2423 gcc_assert (new_st.op == EXEC_NOP);
2424 gfc_clear_new_st ();
2425 gfc_commit_symbols ();
2426 gfc_warning_check ();
2427 st = next_statement ();
2433 /* Parse the statements of OpenMP atomic directive. */
2436 parse_omp_atomic (void)
2442 accept_statement (ST_OMP_ATOMIC);
2444 cp = gfc_state_stack->tail;
2445 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2446 np = new_level (cp);
2452 st = next_statement ();
2455 else if (st == ST_ASSIGNMENT)
2458 unexpected_statement (st);
2461 accept_statement (st);
2467 /* Parse the statements of an OpenMP structured block. */
2470 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
2472 gfc_statement st, omp_end_st;
2476 accept_statement (omp_st);
2478 cp = gfc_state_stack->tail;
2479 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2480 np = new_level (cp);
2486 case ST_OMP_PARALLEL:
2487 omp_end_st = ST_OMP_END_PARALLEL;
2489 case ST_OMP_PARALLEL_SECTIONS:
2490 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
2492 case ST_OMP_SECTIONS:
2493 omp_end_st = ST_OMP_END_SECTIONS;
2495 case ST_OMP_ORDERED:
2496 omp_end_st = ST_OMP_END_ORDERED;
2498 case ST_OMP_CRITICAL:
2499 omp_end_st = ST_OMP_END_CRITICAL;
2502 omp_end_st = ST_OMP_END_MASTER;
2505 omp_end_st = ST_OMP_END_SINGLE;
2507 case ST_OMP_WORKSHARE:
2508 omp_end_st = ST_OMP_END_WORKSHARE;
2510 case ST_OMP_PARALLEL_WORKSHARE:
2511 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
2519 if (workshare_stmts_only)
2521 /* Inside of !$omp workshare, only
2524 where statements and constructs
2525 forall statements and constructs
2529 are allowed. For !$omp critical these
2530 restrictions apply recursively. */
2533 st = next_statement ();
2544 accept_statement (st);
2547 case ST_WHERE_BLOCK:
2548 parse_where_block ();
2551 case ST_FORALL_BLOCK:
2552 parse_forall_block ();
2555 case ST_OMP_PARALLEL:
2556 case ST_OMP_PARALLEL_SECTIONS:
2557 parse_omp_structured_block (st, false);
2560 case ST_OMP_PARALLEL_WORKSHARE:
2561 case ST_OMP_CRITICAL:
2562 parse_omp_structured_block (st, true);
2565 case ST_OMP_PARALLEL_DO:
2566 st = parse_omp_do (st);
2570 parse_omp_atomic ();
2581 st = next_statement ();
2585 st = parse_executable (ST_NONE);
2588 else if (st == ST_OMP_SECTION
2589 && (omp_st == ST_OMP_SECTIONS
2590 || omp_st == ST_OMP_PARALLEL_SECTIONS))
2592 np = new_level (np);
2596 else if (st != omp_end_st)
2597 unexpected_statement (st);
2599 while (st != omp_end_st);
2603 case EXEC_OMP_END_NOWAIT:
2604 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2606 case EXEC_OMP_CRITICAL:
2607 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
2608 || (new_st.ext.omp_name != NULL
2609 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
2610 gfc_error ("Name after !$omp critical and !$omp end critical does "
2612 gfc_free ((char *) new_st.ext.omp_name);
2614 case EXEC_OMP_END_SINGLE:
2615 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
2616 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
2617 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
2618 gfc_free_omp_clauses (new_st.ext.omp_clauses);
2626 gfc_clear_new_st ();
2627 gfc_commit_symbols ();
2628 gfc_warning_check ();
2633 /* Accept a series of executable statements. We return the first
2634 statement that doesn't fit to the caller. Any block statements are
2635 passed on to the correct handler, which usually passes the buck
2638 static gfc_statement
2639 parse_executable (gfc_statement st)
2644 st = next_statement ();
2648 close_flag = check_do_closure ();
2653 case ST_END_PROGRAM:
2656 case ST_END_FUNCTION:
2660 case ST_END_SUBROUTINE:
2665 case ST_SELECT_CASE:
2666 gfc_error ("%s statement at %C cannot terminate a non-block "
2667 "DO loop", gfc_ascii_statement (st));
2683 accept_statement (st);
2684 if (close_flag == 1)
2685 return ST_IMPLIED_ENDDO;
2692 case ST_SELECT_CASE:
2693 parse_select_block ();
2698 if (check_do_closure () == 1)
2699 return ST_IMPLIED_ENDDO;
2702 case ST_WHERE_BLOCK:
2703 parse_where_block ();
2706 case ST_FORALL_BLOCK:
2707 parse_forall_block ();
2710 case ST_OMP_PARALLEL:
2711 case ST_OMP_PARALLEL_SECTIONS:
2712 case ST_OMP_SECTIONS:
2713 case ST_OMP_ORDERED:
2714 case ST_OMP_CRITICAL:
2717 parse_omp_structured_block (st, false);
2720 case ST_OMP_WORKSHARE:
2721 case ST_OMP_PARALLEL_WORKSHARE:
2722 parse_omp_structured_block (st, true);
2726 case ST_OMP_PARALLEL_DO:
2727 st = parse_omp_do (st);
2728 if (st == ST_IMPLIED_ENDDO)
2733 parse_omp_atomic ();
2740 st = next_statement ();
2745 /* Parse a series of contained program units. */
2747 static void parse_progunit (gfc_statement);
2750 /* Fix the symbols for sibling functions. These are incorrectly added to
2751 the child namespace as the parser didn't know about this procedure. */
2754 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
2758 gfc_symbol *old_sym;
2760 sym->attr.referenced = 1;
2761 for (ns = siblings; ns; ns = ns->sibling)
2763 gfc_find_sym_tree (sym->name, ns, 0, &st);
2765 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
2768 old_sym = st->n.sym;
2769 if ((old_sym->attr.flavor == FL_PROCEDURE
2770 || old_sym->ts.type == BT_UNKNOWN)
2771 && old_sym->ns == ns
2772 && !old_sym->attr.contained)
2774 /* Replace it with the symbol from the parent namespace. */
2778 /* Free the old (local) symbol. */
2780 if (old_sym->refs == 0)
2781 gfc_free_symbol (old_sym);
2784 /* Do the same for any contained procedures. */
2785 gfc_fixup_sibling_symbols (sym, ns->contained);
2790 parse_contained (int module)
2792 gfc_namespace *ns, *parent_ns, *tmp;
2793 gfc_state_data s1, s2;
2797 int contains_statements = 0;
2800 push_state (&s1, COMP_CONTAINS, NULL);
2801 parent_ns = gfc_current_ns;
2805 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2807 gfc_current_ns->sibling = parent_ns->contained;
2808 parent_ns->contained = gfc_current_ns;
2811 /* Process the next available statement. We come here if we got an error
2812 and rejected the last statement. */
2813 st = next_statement ();
2822 contains_statements = 1;
2823 accept_statement (st);
2826 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2829 /* For internal procedures, create/update the symbol in the
2830 parent namespace. */
2834 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2835 gfc_error ("Contained procedure '%s' at %C is already "
2836 "ambiguous", gfc_new_block->name);
2839 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2840 &gfc_new_block->declared_at) ==
2843 if (st == ST_FUNCTION)
2844 gfc_add_function (&sym->attr, sym->name,
2845 &gfc_new_block->declared_at);
2847 gfc_add_subroutine (&sym->attr, sym->name,
2848 &gfc_new_block->declared_at);
2852 gfc_commit_symbols ();
2855 sym = gfc_new_block;
2857 /* Mark this as a contained function, so it isn't replaced
2858 by other module functions. */
2859 sym->attr.contained = 1;
2860 sym->attr.referenced = 1;
2862 parse_progunit (ST_NONE);
2864 /* Fix up any sibling functions that refer to this one. */
2865 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2866 /* Or refer to any of its alternate entry points. */
2867 for (el = gfc_current_ns->entries; el; el = el->next)
2868 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2870 gfc_current_ns->code = s2.head;
2871 gfc_current_ns = parent_ns;
2876 /* These statements are associated with the end of the host unit. */
2877 case ST_END_FUNCTION:
2879 case ST_END_PROGRAM:
2880 case ST_END_SUBROUTINE:
2881 accept_statement (st);
2885 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2886 gfc_ascii_statement (st));
2887 reject_statement ();
2893 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2894 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2896 /* The first namespace in the list is guaranteed to not have
2897 anything (worthwhile) in it. */
2898 tmp = gfc_current_ns;
2899 gfc_current_ns = parent_ns;
2900 if (seen_error && tmp->refs > 1)
2901 gfc_free_namespace (tmp);
2903 ns = gfc_current_ns->contained;
2904 gfc_current_ns->contained = ns->sibling;
2905 gfc_free_namespace (ns);
2908 if (!contains_statements)
2909 /* This is valid in Fortran 2008. */
2910 gfc_notify_std (GFC_STD_GNU, "Extension: CONTAINS statement without "
2911 "FUNCTION or SUBROUTINE statement at %C");
2915 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2918 parse_progunit (gfc_statement st)
2923 st = parse_spec (st);
2933 accept_statement (st);
2940 if (gfc_current_state () == COMP_FUNCTION)
2941 gfc_check_function_type (gfc_current_ns);
2946 st = parse_executable (st);
2957 accept_statement (st);
2964 unexpected_statement (st);
2965 reject_statement ();
2966 st = next_statement ();
2972 for (p = gfc_state_stack; p; p = p->previous)
2973 if (p->state == COMP_CONTAINS)
2976 if (gfc_find_state (COMP_MODULE) == SUCCESS)
2981 gfc_error ("CONTAINS statement at %C is already in a contained "
2983 st = next_statement ();
2987 parse_contained (0);
2990 gfc_current_ns->code = gfc_state_stack->head;
2994 /* Come here to complain about a global symbol already in use as
2998 global_used (gfc_gsymbol *sym, locus *where)
3003 where = &gfc_current_locus;
3013 case GSYM_SUBROUTINE:
3014 name = "SUBROUTINE";
3019 case GSYM_BLOCK_DATA:
3020 name = "BLOCK DATA";
3026 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
3030 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
3031 sym->name, where, name, &sym->where);
3035 /* Parse a block data program unit. */
3038 parse_block_data (void)
3041 static locus blank_locus;
3042 static int blank_block=0;
3045 gfc_current_ns->proc_name = gfc_new_block;
3046 gfc_current_ns->is_block_data = 1;
3048 if (gfc_new_block == NULL)
3051 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3052 "prior BLOCK DATA at %L", &blank_locus);
3056 blank_locus = gfc_current_locus;
3061 s = gfc_get_gsymbol (gfc_new_block->name);
3063 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
3064 global_used(s, NULL);
3067 s->type = GSYM_BLOCK_DATA;
3068 s->where = gfc_current_locus;
3073 st = parse_spec (ST_NONE);
3075 while (st != ST_END_BLOCK_DATA)
3077 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3078 gfc_ascii_statement (st));
3079 reject_statement ();
3080 st = next_statement ();
3085 /* Parse a module subprogram. */
3093 s = gfc_get_gsymbol (gfc_new_block->name);
3094 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
3095 global_used(s, NULL);
3098 s->type = GSYM_MODULE;
3099 s->where = gfc_current_locus;
3103 st = parse_spec (ST_NONE);
3112 parse_contained (1);
3116 accept_statement (st);
3120 gfc_error ("Unexpected %s statement in MODULE at %C",
3121 gfc_ascii_statement (st));
3123 reject_statement ();
3124 st = next_statement ();
3130 /* Add a procedure name to the global symbol table. */
3133 add_global_procedure (int sub)
3137 s = gfc_get_gsymbol(gfc_new_block->name);
3140 || (s->type != GSYM_UNKNOWN
3141 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3142 global_used(s, NULL);
3145 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3146 s->where = gfc_current_locus;
3152 /* Add a program to the global symbol table. */
3155 add_global_program (void)
3159 if (gfc_new_block == NULL)
3161 s = gfc_get_gsymbol (gfc_new_block->name);
3163 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
3164 global_used(s, NULL);
3167 s->type = GSYM_PROGRAM;
3168 s->where = gfc_current_locus;
3174 /* Top level parser. */
3177 gfc_parse_file (void)
3179 int seen_program, errors_before, errors;
3180 gfc_state_data top, s;
3184 top.state = COMP_NONE;
3186 top.previous = NULL;
3187 top.head = top.tail = NULL;
3188 top.do_variable = NULL;
3190 gfc_state_stack = ⊤
3192 gfc_clear_new_st ();
3194 gfc_statement_label = NULL;
3196 if (setjmp (eof_buf))
3197 return FAILURE; /* Come here on unexpected EOF */
3201 /* Exit early for empty files. */
3207 st = next_statement ();
3216 goto duplicate_main;
3218 prog_locus = gfc_current_locus;
3220 push_state (&s, COMP_PROGRAM, gfc_new_block);
3221 main_program_symbol(gfc_current_ns);
3222 accept_statement (st);
3223 add_global_program ();
3224 parse_progunit (ST_NONE);
3228 add_global_procedure (1);
3229 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
3230 accept_statement (st);
3231 parse_progunit (ST_NONE);
3235 add_global_procedure (0);
3236 push_state (&s, COMP_FUNCTION, gfc_new_block);
3237 accept_statement (st);
3238 parse_progunit (ST_NONE);
3242 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
3243 accept_statement (st);
3244 parse_block_data ();
3248 push_state (&s, COMP_MODULE, gfc_new_block);
3249 accept_statement (st);
3251 gfc_get_errors (NULL, &errors_before);
3255 /* Anything else starts a nameless main program block. */
3258 goto duplicate_main;
3260 prog_locus = gfc_current_locus;
3262 push_state (&s, COMP_PROGRAM, gfc_new_block);
3263 main_program_symbol (gfc_current_ns);
3264 parse_progunit (st);
3268 gfc_current_ns->code = s.head;
3270 gfc_resolve (gfc_current_ns);
3272 /* Dump the parse tree if requested. */
3273 if (gfc_option.verbose)
3274 gfc_show_namespace (gfc_current_ns);
3276 gfc_get_errors (NULL, &errors);
3277 if (s.state == COMP_MODULE)
3279 gfc_dump_module (s.sym->name, errors_before == errors);
3281 gfc_generate_module_code (gfc_current_ns);
3286 gfc_generate_code (gfc_current_ns);
3297 /* If we see a duplicate main program, shut down. If the second
3298 instance is an implied main program, ie data decls or executable
3299 statements, we're in for lots of errors. */
3300 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
3301 reject_statement ();