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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
29 /* Current statement label. Zero means no statement label. Because new_st
30 can get wiped during statement matching, we have to keep it separate. */
32 gfc_st_label *gfc_statement_label;
34 static locus label_locus;
35 static jmp_buf eof_buf;
37 gfc_state_data *gfc_state_stack;
39 /* TODO: Re-order functions to kill these forward decls. */
40 static void check_statement_label (gfc_statement);
41 static void undo_new_statement (void);
42 static void reject_statement (void);
45 /* A sort of half-matching function. We try to match the word on the
46 input with the passed string. If this succeeds, we call the
47 keyword-dependent matching function that will match the rest of the
48 statement. For single keywords, the matching subroutine is
52 match_word (const char *str, match (*subr) (void), locus *old_locus)
67 gfc_current_locus = *old_locus;
75 /* Figure out what the next statement is, (mostly) regardless of
76 proper ordering. The do...while(0) is there to prevent if/else
79 #define match(keyword, subr, st) \
81 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
84 undo_new_statement (); \
88 decode_statement (void)
99 gfc_clear_error (); /* Clear any pending errors. */
100 gfc_clear_warning (); /* Clear any pending warnings. */
102 if (gfc_match_eos () == MATCH_YES)
105 old_locus = gfc_current_locus;
107 /* Try matching a data declaration or function declaration. The
108 input "REALFUNCTIONA(N)" can mean several things in different
109 contexts, so it (and its relatives) get special treatment. */
111 if (gfc_current_state () == COMP_NONE
112 || gfc_current_state () == COMP_INTERFACE
113 || gfc_current_state () == COMP_CONTAINS)
115 m = gfc_match_function_decl ();
118 else if (m == MATCH_ERROR)
122 gfc_current_locus = old_locus;
125 /* Match statements whose error messages are meant to be overwritten
126 by something better. */
128 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
129 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
130 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
132 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
133 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
135 /* Try to match a subroutine statement, which has the same optional
136 prefixes that functions can have. */
138 if (gfc_match_subroutine () == MATCH_YES)
139 return ST_SUBROUTINE;
141 gfc_current_locus = old_locus;
143 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
144 might begin with a block label. The match functions for these
145 statements are unusual in that their keyword is not seen before
146 the matcher is called. */
148 if (gfc_match_if (&st) == MATCH_YES)
151 gfc_current_locus = old_locus;
153 if (gfc_match_where (&st) == MATCH_YES)
156 gfc_current_locus = old_locus;
158 if (gfc_match_forall (&st) == MATCH_YES)
161 gfc_current_locus = old_locus;
163 match (NULL, gfc_match_do, ST_DO);
164 match (NULL, gfc_match_select, ST_SELECT_CASE);
166 /* General statement matching: Instead of testing every possible
167 statement, we eliminate most possibilities by peeking at the
170 c = gfc_peek_char ();
175 match ("abstract% interface", gfc_match_abstract_interface,
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);
185 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
189 match ("call", gfc_match_call, ST_CALL);
190 match ("close", gfc_match_close, ST_CLOSE);
191 match ("continue", gfc_match_continue, ST_CONTINUE);
192 match ("cycle", gfc_match_cycle, ST_CYCLE);
193 match ("case", gfc_match_case, ST_CASE);
194 match ("common", gfc_match_common, ST_COMMON);
195 match ("contains", gfc_match_eos, ST_CONTAINS);
199 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
200 match ("data", gfc_match_data, ST_DATA);
201 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
205 match ("end file", gfc_match_endfile, ST_END_FILE);
206 match ("exit", gfc_match_exit, ST_EXIT);
207 match ("else", gfc_match_else, ST_ELSE);
208 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
209 match ("else if", gfc_match_elseif, ST_ELSEIF);
210 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
212 if (gfc_match_end (&st) == MATCH_YES)
215 match ("entry% ", gfc_match_entry, ST_ENTRY);
216 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
217 match ("external", gfc_match_external, ST_ATTR_DECL);
221 match ("flush", gfc_match_flush, ST_FLUSH);
222 match ("format", gfc_match_format, ST_FORMAT);
226 match ("go to", gfc_match_goto, ST_GOTO);
230 match ("inquire", gfc_match_inquire, ST_INQUIRE);
231 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
232 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
233 match ("import", gfc_match_import, ST_IMPORT);
234 match ("interface", gfc_match_interface, ST_INTERFACE);
235 match ("intent", gfc_match_intent, ST_ATTR_DECL);
236 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
240 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
241 match ("module", gfc_match_module, ST_MODULE);
245 match ("nullify", gfc_match_nullify, ST_NULLIFY);
246 match ("namelist", gfc_match_namelist, ST_NAMELIST);
250 match ("open", gfc_match_open, ST_OPEN);
251 match ("optional", gfc_match_optional, ST_ATTR_DECL);
255 match ("print", gfc_match_print, ST_WRITE);
256 match ("parameter", gfc_match_parameter, ST_PARAMETER);
257 match ("pause", gfc_match_pause, ST_PAUSE);
258 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
259 if (gfc_match_private (&st) == MATCH_YES)
261 match ("program", gfc_match_program, ST_PROGRAM);
262 if (gfc_match_public (&st) == MATCH_YES)
264 match ("protected", gfc_match_protected, ST_ATTR_DECL);
268 match ("read", gfc_match_read, ST_READ);
269 match ("return", gfc_match_return, ST_RETURN);
270 match ("rewind", gfc_match_rewind, ST_REWIND);
274 match ("sequence", gfc_match_eos, ST_SEQUENCE);
275 match ("stop", gfc_match_stop, ST_STOP);
276 match ("save", gfc_match_save, ST_ATTR_DECL);
280 match ("target", gfc_match_target, ST_ATTR_DECL);
281 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
285 match ("use", gfc_match_use, ST_USE);
289 match ("value", gfc_match_value, ST_ATTR_DECL);
290 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
294 match ("write", gfc_match_write, ST_WRITE);
298 /* All else has failed, so give up. See if any of the matchers has
299 stored an error message of some sort. */
301 if (gfc_error_check () == 0)
302 gfc_error_now ("Unclassifiable statement at %C");
306 gfc_error_recovery ();
312 decode_omp_directive (void)
321 gfc_clear_error (); /* Clear any pending errors. */
322 gfc_clear_warning (); /* Clear any pending warnings. */
326 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
327 "or ELEMENTAL procedures");
328 gfc_error_recovery ();
332 old_locus = gfc_current_locus;
334 /* General OpenMP directive matching: Instead of testing every possible
335 statement, we eliminate most possibilities by peeking at the
338 c = gfc_peek_char ();
343 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
346 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
349 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
352 match ("do", gfc_match_omp_do, ST_OMP_DO);
355 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
356 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
357 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
358 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
359 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
360 match ("end parallel sections", gfc_match_omp_eos,
361 ST_OMP_END_PARALLEL_SECTIONS);
362 match ("end parallel workshare", gfc_match_omp_eos,
363 ST_OMP_END_PARALLEL_WORKSHARE);
364 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
365 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
366 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
367 match ("end workshare", gfc_match_omp_end_nowait,
368 ST_OMP_END_WORKSHARE);
371 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
374 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
377 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
380 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
381 match ("parallel sections", gfc_match_omp_parallel_sections,
382 ST_OMP_PARALLEL_SECTIONS);
383 match ("parallel workshare", gfc_match_omp_parallel_workshare,
384 ST_OMP_PARALLEL_WORKSHARE);
385 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
388 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
389 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
390 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
393 match ("threadprivate", gfc_match_omp_threadprivate,
394 ST_OMP_THREADPRIVATE);
396 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
400 /* All else has failed, so give up. See if any of the matchers has
401 stored an error message of some sort. */
403 if (gfc_error_check () == 0)
404 gfc_error_now ("Unclassifiable OpenMP directive at %C");
408 gfc_error_recovery ();
416 /* Get the next statement in free form source. */
422 int c, d, cnt, at_bol;
424 at_bol = gfc_at_bol ();
425 gfc_gobble_whitespace ();
427 c = gfc_peek_char ();
431 /* Found a statement label? */
432 m = gfc_match_st_label (&gfc_statement_label);
434 d = gfc_peek_char ();
435 if (m != MATCH_YES || !gfc_is_whitespace (d))
437 gfc_match_small_literal_int (&c, &cnt);
440 gfc_error_now ("Too many digits in statement label at %C");
443 gfc_error_now ("Zero is not a valid statement label at %C");
446 c = gfc_next_char ();
449 if (!gfc_is_whitespace (c))
450 gfc_error_now ("Non-numeric character in statement label at %C");
456 label_locus = gfc_current_locus;
458 gfc_gobble_whitespace ();
460 if (at_bol && gfc_peek_char () == ';')
462 gfc_error_now ("Semicolon at %C needs to be preceded by "
464 gfc_next_char (); /* Eat up the semicolon. */
468 if (gfc_match_eos () == MATCH_YES)
470 gfc_warning_now ("Ignoring statement label in empty statement "
472 gfc_free_st_label (gfc_statement_label);
473 gfc_statement_label = NULL;
480 /* Comments have already been skipped by the time we get here,
481 except for OpenMP directives. */
482 if (gfc_option.flag_openmp)
486 c = gfc_next_char ();
487 for (i = 0; i < 5; i++, c = gfc_next_char ())
488 gcc_assert (c == "!$omp"[i]);
490 gcc_assert (c == ' ');
491 gfc_gobble_whitespace ();
492 return decode_omp_directive ();
496 if (at_bol && c == ';')
498 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
499 gfc_next_char (); /* Eat up the semicolon. */
503 return decode_statement ();
507 /* Get the next statement in fixed-form source. */
512 int label, digit_flag, i;
517 return decode_statement ();
519 /* Skip past the current label field, parsing a statement label if
520 one is there. This is a weird number parser, since the number is
521 contained within five columns and can have any kind of embedded
522 spaces. We also check for characters that make the rest of the
528 for (i = 0; i < 5; i++)
530 c = gfc_next_char_literal (0);
547 label = label * 10 + c - '0';
548 label_locus = gfc_current_locus;
552 /* Comments have already been skipped by the time we get
553 here, except for OpenMP directives. */
555 if (gfc_option.flag_openmp)
557 for (i = 0; i < 5; i++, c = gfc_next_char_literal (0))
558 gcc_assert (TOLOWER (c) == "*$omp"[i]);
560 if (c != ' ' && c != '0')
562 gfc_buffer_error (0);
563 gfc_error ("Bad continuation line at %C");
567 return decode_omp_directive ();
571 /* Comments have already been skipped by the time we get
572 here so don't bother checking for them. */
575 gfc_buffer_error (0);
576 gfc_error ("Non-numeric character in statement label at %C");
584 gfc_warning_now ("Zero is not a valid statement label at %C");
587 /* We've found a valid statement label. */
588 gfc_statement_label = gfc_get_st_label (label);
592 /* Since this line starts a statement, it cannot be a continuation
593 of a previous statement. If we see something here besides a
594 space or zero, it must be a bad continuation line. */
596 c = gfc_next_char_literal (0);
600 if (c != ' ' && c != '0')
602 gfc_buffer_error (0);
603 gfc_error ("Bad continuation line at %C");
607 /* Now that we've taken care of the statement label columns, we have
608 to make sure that the first nonblank character is not a '!'. If
609 it is, the rest of the line is a comment. */
613 loc = gfc_current_locus;
614 c = gfc_next_char_literal (0);
616 while (gfc_is_whitespace (c));
620 gfc_current_locus = loc;
624 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
628 if (gfc_match_eos () == MATCH_YES)
631 /* At this point, we've got a nonblank statement to parse. */
632 return decode_statement ();
636 gfc_warning ("Ignoring statement label in empty statement at %C");
642 /* Return the next non-ST_NONE statement to the caller. We also worry
643 about including files and the ends of include files at this stage. */
646 next_statement (void)
650 gfc_new_block = NULL;
654 gfc_statement_label = NULL;
655 gfc_buffer_error (1);
659 if ((gfc_option.warn_line_truncation || gfc_current_form == FORM_FREE)
660 && gfc_current_locus.lb
661 && gfc_current_locus.lb->truncated)
662 gfc_warning_now ("Line truncated at %C");
667 gfc_skip_comments ();
675 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
681 gfc_buffer_error (0);
684 check_statement_label (st);
690 /****************************** Parser ***********************************/
692 /* The parser subroutines are of type 'try' that fail if the file ends
695 /* Macros that expand to case-labels for various classes of
696 statements. Start with executable statements that directly do
699 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
700 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
701 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
702 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
703 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
704 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
705 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
706 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
709 /* Statements that mark other executable statements. */
711 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
712 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
713 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
714 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
715 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
716 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
718 /* Declaration statements */
720 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
721 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
722 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE
724 /* Block end statements. Errors associated with interchanging these
725 are detected in gfc_match_end(). */
727 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
728 case ST_END_PROGRAM: case ST_END_SUBROUTINE
731 /* Push a new state onto the stack. */
734 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
736 p->state = new_state;
737 p->previous = gfc_state_stack;
739 p->head = p->tail = NULL;
740 p->do_variable = NULL;
745 /* 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)
1261 case ST_IMPLICIT_NONE:
1262 gfc_set_implicit_none ();
1271 gfc_current_ns->proc_name = gfc_new_block;
1274 /* If the statement is the end of a block, lay down a special code
1275 that allows a branch to the end of the block from within the
1280 if (gfc_statement_label != NULL)
1282 new_st.op = EXEC_NOP;
1288 /* The end-of-program unit statements do not get the special
1289 marker and require a statement of some sort if they are a
1292 case ST_END_PROGRAM:
1293 case ST_END_FUNCTION:
1294 case ST_END_SUBROUTINE:
1295 if (gfc_statement_label != NULL)
1297 new_st.op = EXEC_RETURN;
1313 gfc_commit_symbols ();
1314 gfc_warning_check ();
1315 gfc_clear_new_st ();
1319 /* Undo anything tentative that has been built for the current
1323 reject_statement (void)
1325 gfc_new_block = NULL;
1326 gfc_undo_symbols ();
1327 gfc_clear_warning ();
1328 undo_new_statement ();
1332 /* Generic complaint about an out of order statement. We also do
1333 whatever is necessary to clean up. */
1336 unexpected_statement (gfc_statement st)
1338 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1340 reject_statement ();
1344 /* Given the next statement seen by the matcher, make sure that it is
1345 in proper order with the last. This subroutine is initialized by
1346 calling it with an argument of ST_NONE. If there is a problem, we
1347 issue an error and return FAILURE. Otherwise we return SUCCESS.
1349 Individual parsers need to verify that the statements seen are
1350 valid before calling here, ie ENTRY statements are not allowed in
1351 INTERFACE blocks. The following diagram is taken from the standard:
1353 +---------------------------------------+
1354 | program subroutine function module |
1355 +---------------------------------------+
1357 +---------------------------------------+
1359 +---------------------------------------+
1361 | +-----------+------------------+
1362 | | parameter | implicit |
1363 | +-----------+------------------+
1364 | format | | derived type |
1365 | entry | parameter | interface |
1366 | | data | specification |
1367 | | | statement func |
1368 | +-----------+------------------+
1369 | | data | executable |
1370 +--------+-----------+------------------+
1372 +---------------------------------------+
1373 | internal module/subprogram |
1374 +---------------------------------------+
1376 +---------------------------------------+
1383 { ORDER_START, ORDER_USE, ORDER_IMPORT, ORDER_IMPLICIT_NONE,
1384 ORDER_IMPLICIT, ORDER_SPEC, ORDER_EXEC
1387 gfc_statement last_statement;
1393 verify_st_order (st_state *p, gfc_statement st)
1399 p->state = ORDER_START;
1403 if (p->state > ORDER_USE)
1405 p->state = ORDER_USE;
1409 if (p->state > ORDER_IMPORT)
1411 p->state = ORDER_IMPORT;
1414 case ST_IMPLICIT_NONE:
1415 if (p->state > ORDER_IMPLICIT_NONE)
1418 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1419 statement disqualifies a USE but not an IMPLICIT NONE.
1420 Duplicate IMPLICIT NONEs are caught when the implicit types
1423 p->state = ORDER_IMPLICIT_NONE;
1427 if (p->state > ORDER_IMPLICIT)
1429 p->state = ORDER_IMPLICIT;
1434 if (p->state < ORDER_IMPLICIT_NONE)
1435 p->state = ORDER_IMPLICIT_NONE;
1439 if (p->state >= ORDER_EXEC)
1441 if (p->state < ORDER_IMPLICIT)
1442 p->state = ORDER_IMPLICIT;
1446 if (p->state < ORDER_SPEC)
1447 p->state = ORDER_SPEC;
1452 case ST_DERIVED_DECL:
1454 if (p->state >= ORDER_EXEC)
1456 if (p->state < ORDER_SPEC)
1457 p->state = ORDER_SPEC;
1462 if (p->state < ORDER_EXEC)
1463 p->state = ORDER_EXEC;
1467 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1468 gfc_ascii_statement (st));
1471 /* All is well, record the statement in case we need it next time. */
1472 p->where = gfc_current_locus;
1473 p->last_statement = st;
1477 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1478 gfc_ascii_statement (st),
1479 gfc_ascii_statement (p->last_statement), &p->where);
1485 /* Handle an unexpected end of file. This is a show-stopper... */
1487 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1490 unexpected_eof (void)
1494 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1496 /* Memory cleanup. Move to "second to last". */
1497 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1500 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1503 longjmp (eof_buf, 1);
1507 /* Parse a derived type. */
1510 parse_derived (void)
1512 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1515 gfc_symbol *derived_sym = NULL;
1521 accept_statement (ST_DERIVED_DECL);
1522 push_state (&s, COMP_DERIVED, gfc_new_block);
1524 gfc_new_block->component_access = ACCESS_PUBLIC;
1531 while (compiling_type)
1533 st = next_statement ();
1540 accept_statement (st);
1548 && (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Derived type "
1549 "definition at %C without components")
1553 accept_statement (ST_END_TYPE);
1557 if (gfc_find_state (COMP_MODULE) == FAILURE)
1559 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1567 gfc_error ("PRIVATE statement at %C must precede "
1568 "structure components");
1575 gfc_error ("Duplicate PRIVATE statement at %C");
1579 s.sym->component_access = ACCESS_PRIVATE;
1580 accept_statement (ST_PRIVATE);
1587 gfc_error ("SEQUENCE statement at %C must precede "
1588 "structure components");
1593 if (gfc_current_block ()->attr.sequence)
1594 gfc_warning ("SEQUENCE attribute at %C already specified in "
1599 gfc_error ("Duplicate SEQUENCE statement at %C");
1604 gfc_add_sequence (&gfc_current_block ()->attr,
1605 gfc_current_block ()->name, NULL);
1609 unexpected_statement (st);
1614 /* need to verify that all fields of the derived type are
1615 * interoperable with C if the type is declared to be bind(c)
1617 derived_sym = gfc_current_block();
1619 sym = gfc_current_block ();
1620 for (c = sym->components; c; c = c->next)
1622 /* Look for allocatable components. */
1624 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.alloc_comp))
1626 sym->attr.alloc_comp = 1;
1630 /* Look for pointer components. */
1632 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.pointer_comp))
1634 sym->attr.pointer_comp = 1;
1638 /* Look for private components. */
1639 if (sym->component_access == ACCESS_PRIVATE
1640 || c->access == ACCESS_PRIVATE
1641 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.private_comp))
1643 sym->attr.private_comp = 1;
1652 /* Parse an ENUM. */
1661 int seen_enumerator = 0;
1665 push_state (&s, COMP_ENUM, gfc_new_block);
1669 while (compiling_enum)
1671 st = next_statement ();
1679 seen_enumerator = 1;
1680 accept_statement (st);
1685 if (!seen_enumerator)
1687 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1690 accept_statement (st);
1694 gfc_free_enum_history ();
1695 unexpected_statement (st);
1703 /* Parse an interface. We must be able to deal with the possibility
1704 of recursive interfaces. The parse_spec() subroutine is mutually
1705 recursive with parse_interface(). */
1707 static gfc_statement parse_spec (gfc_statement);
1710 parse_interface (void)
1712 gfc_compile_state new_state, current_state;
1713 gfc_symbol *prog_unit, *sym;
1714 gfc_interface_info save;
1715 gfc_state_data s1, s2;
1719 accept_statement (ST_INTERFACE);
1721 current_interface.ns = gfc_current_ns;
1722 save = current_interface;
1724 sym = (current_interface.type == INTERFACE_GENERIC
1725 || current_interface.type == INTERFACE_USER_OP)
1726 ? gfc_new_block : NULL;
1728 push_state (&s1, COMP_INTERFACE, sym);
1729 current_state = COMP_NONE;
1732 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1734 st = next_statement ();
1741 new_state = COMP_SUBROUTINE;
1742 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1743 gfc_new_block->formal, NULL);
1747 new_state = COMP_FUNCTION;
1748 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1749 gfc_new_block->formal, NULL);
1752 case ST_MODULE_PROC: /* The module procedure matcher makes
1753 sure the context is correct. */
1754 accept_statement (st);
1755 gfc_free_namespace (gfc_current_ns);
1758 case ST_END_INTERFACE:
1759 gfc_free_namespace (gfc_current_ns);
1760 gfc_current_ns = current_interface.ns;
1764 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1765 gfc_ascii_statement (st));
1766 reject_statement ();
1767 gfc_free_namespace (gfc_current_ns);
1772 /* Make sure that a generic interface has only subroutines or
1773 functions and that the generic name has the right attribute. */
1774 if (current_interface.type == INTERFACE_GENERIC)
1776 if (current_state == COMP_NONE)
1778 if (new_state == COMP_FUNCTION)
1779 gfc_add_function (&sym->attr, sym->name, NULL);
1780 else if (new_state == COMP_SUBROUTINE)
1781 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1783 current_state = new_state;
1787 if (new_state != current_state)
1789 if (new_state == COMP_SUBROUTINE)
1790 gfc_error ("SUBROUTINE at %C does not belong in a "
1791 "generic function interface");
1793 if (new_state == COMP_FUNCTION)
1794 gfc_error ("FUNCTION at %C does not belong in a "
1795 "generic subroutine interface");
1800 if (current_interface.type == INTERFACE_ABSTRACT)
1802 gfc_new_block->attr.abstract = 1;
1803 if (gfc_is_intrinsic_typename (gfc_new_block->name))
1804 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
1805 "cannot be the same as an intrinsic type",
1806 gfc_new_block->name);
1809 push_state (&s2, new_state, gfc_new_block);
1810 accept_statement (st);
1811 prog_unit = gfc_new_block;
1812 prog_unit->formal_ns = gfc_current_ns;
1813 proc_locus = gfc_current_locus;
1816 /* Read data declaration statements. */
1817 st = parse_spec (ST_NONE);
1819 /* Since the interface block does not permit an IMPLICIT statement,
1820 the default type for the function or the result must be taken
1821 from the formal namespace. */
1822 if (new_state == COMP_FUNCTION)
1824 if (prog_unit->result == prog_unit
1825 && prog_unit->ts.type == BT_UNKNOWN)
1826 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
1827 else if (prog_unit->result != prog_unit
1828 && prog_unit->result->ts.type == BT_UNKNOWN)
1829 gfc_set_default_type (prog_unit->result, 1,
1830 prog_unit->formal_ns);
1833 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1835 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1836 gfc_ascii_statement (st));
1837 reject_statement ();
1841 current_interface = save;
1842 gfc_add_interface (prog_unit);
1845 if (current_interface.ns
1846 && current_interface.ns->proc_name
1847 && strcmp (current_interface.ns->proc_name->name,
1848 prog_unit->name) == 0)
1849 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
1850 "enclosing procedure", prog_unit->name, &proc_locus);
1859 /* Parse a set of specification statements. Returns the statement
1860 that doesn't fit. */
1862 static gfc_statement
1863 parse_spec (gfc_statement st)
1867 verify_st_order (&ss, ST_NONE);
1869 st = next_statement ();
1879 case ST_DATA: /* Not allowed in interfaces */
1880 if (gfc_current_state () == COMP_INTERFACE)
1887 case ST_IMPLICIT_NONE:
1892 case ST_DERIVED_DECL:
1894 if (verify_st_order (&ss, st) == FAILURE)
1896 reject_statement ();
1897 st = next_statement ();
1907 case ST_DERIVED_DECL:
1913 if (gfc_current_state () != COMP_MODULE)
1915 gfc_error ("%s statement must appear in a MODULE",
1916 gfc_ascii_statement (st));
1920 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1922 gfc_error ("%s statement at %C follows another accessibility "
1923 "specification", gfc_ascii_statement (st));
1927 gfc_current_ns->default_access = (st == ST_PUBLIC)
1928 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1932 case ST_STATEMENT_FUNCTION:
1933 if (gfc_current_state () == COMP_MODULE)
1935 unexpected_statement (st);
1943 accept_statement (st);
1944 st = next_statement ();
1948 accept_statement (st);
1950 st = next_statement ();
1961 /* Parse a WHERE block, (not a simple WHERE statement). */
1964 parse_where_block (void)
1966 int seen_empty_else;
1971 accept_statement (ST_WHERE_BLOCK);
1972 top = gfc_state_stack->tail;
1974 push_state (&s, COMP_WHERE, gfc_new_block);
1976 d = add_statement ();
1977 d->expr = top->expr;
1983 seen_empty_else = 0;
1987 st = next_statement ();
1993 case ST_WHERE_BLOCK:
1994 parse_where_block ();
1999 accept_statement (st);
2003 if (seen_empty_else)
2005 gfc_error ("ELSEWHERE statement at %C follows previous "
2006 "unmasked ELSEWHERE");
2010 if (new_st.expr == NULL)
2011 seen_empty_else = 1;
2013 d = new_level (gfc_state_stack->head);
2015 d->expr = new_st.expr;
2017 accept_statement (st);
2022 accept_statement (st);
2026 gfc_error ("Unexpected %s statement in WHERE block at %C",
2027 gfc_ascii_statement (st));
2028 reject_statement ();
2032 while (st != ST_END_WHERE);
2038 /* Parse a FORALL block (not a simple FORALL statement). */
2041 parse_forall_block (void)
2047 accept_statement (ST_FORALL_BLOCK);
2048 top = gfc_state_stack->tail;
2050 push_state (&s, COMP_FORALL, gfc_new_block);
2052 d = add_statement ();
2053 d->op = EXEC_FORALL;
2058 st = next_statement ();
2063 case ST_POINTER_ASSIGNMENT:
2066 accept_statement (st);
2069 case ST_WHERE_BLOCK:
2070 parse_where_block ();
2073 case ST_FORALL_BLOCK:
2074 parse_forall_block ();
2078 accept_statement (st);
2085 gfc_error ("Unexpected %s statement in FORALL block at %C",
2086 gfc_ascii_statement (st));
2088 reject_statement ();
2092 while (st != ST_END_FORALL);
2098 static gfc_statement parse_executable (gfc_statement);
2100 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2103 parse_if_block (void)
2112 accept_statement (ST_IF_BLOCK);
2114 top = gfc_state_stack->tail;
2115 push_state (&s, COMP_IF, gfc_new_block);
2117 new_st.op = EXEC_IF;
2118 d = add_statement ();
2120 d->expr = top->expr;
2126 st = parse_executable (ST_NONE);
2136 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2137 "statement at %L", &else_locus);
2139 reject_statement ();
2143 d = new_level (gfc_state_stack->head);
2145 d->expr = new_st.expr;
2147 accept_statement (st);
2154 gfc_error ("Duplicate ELSE statements at %L and %C",
2156 reject_statement ();
2161 else_locus = gfc_current_locus;
2163 d = new_level (gfc_state_stack->head);
2166 accept_statement (st);
2174 unexpected_statement (st);
2178 while (st != ST_ENDIF);
2181 accept_statement (st);
2185 /* Parse a SELECT block. */
2188 parse_select_block (void)
2194 accept_statement (ST_SELECT_CASE);
2196 cp = gfc_state_stack->tail;
2197 push_state (&s, COMP_SELECT, gfc_new_block);
2199 /* Make sure that the next statement is a CASE or END SELECT. */
2202 st = next_statement ();
2205 if (st == ST_END_SELECT)
2207 /* Empty SELECT CASE is OK. */
2208 accept_statement (st);
2215 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2218 reject_statement ();
2221 /* At this point, we're got a nonempty select block. */
2222 cp = new_level (cp);
2225 accept_statement (st);
2229 st = parse_executable (ST_NONE);
2236 cp = new_level (gfc_state_stack->head);
2238 gfc_clear_new_st ();
2240 accept_statement (st);
2246 /* Can't have an executable statement because of
2247 parse_executable(). */
2249 unexpected_statement (st);
2253 while (st != ST_END_SELECT);
2256 accept_statement (st);
2260 /* Given a symbol, make sure it is not an iteration variable for a DO
2261 statement. This subroutine is called when the symbol is seen in a
2262 context that causes it to become redefined. If the symbol is an
2263 iterator, we generate an error message and return nonzero. */
2266 gfc_check_do_variable (gfc_symtree *st)
2270 for (s=gfc_state_stack; s; s = s->previous)
2271 if (s->do_variable == st)
2273 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2274 "loop beginning at %L", st->name, &s->head->loc);
2282 /* Checks to see if the current statement label closes an enddo.
2283 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2284 an error) if it incorrectly closes an ENDDO. */
2287 check_do_closure (void)
2291 if (gfc_statement_label == NULL)
2294 for (p = gfc_state_stack; p; p = p->previous)
2295 if (p->state == COMP_DO)
2299 return 0; /* No loops to close */
2301 if (p->ext.end_do_label == gfc_statement_label)
2304 if (p == gfc_state_stack)
2307 gfc_error ("End of nonblock DO statement at %C is within another block");
2311 /* At this point, the label doesn't terminate the innermost loop.
2312 Make sure it doesn't terminate another one. */
2313 for (; p; p = p->previous)
2314 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
2316 gfc_error ("End of nonblock DO statement at %C is interwoven "
2317 "with another DO loop");
2325 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2326 handled inside of parse_executable(), because they aren't really
2330 parse_do_block (void)
2337 s.ext.end_do_label = new_st.label;
2339 if (new_st.ext.iterator != NULL)
2340 stree = new_st.ext.iterator->var->symtree;
2344 accept_statement (ST_DO);
2346 top = gfc_state_stack->tail;
2347 push_state (&s, COMP_DO, gfc_new_block);
2349 s.do_variable = stree;
2351 top->block = new_level (top);
2352 top->block->op = EXEC_DO;
2355 st = parse_executable (ST_NONE);
2363 if (s.ext.end_do_label != NULL
2364 && s.ext.end_do_label != gfc_statement_label)
2365 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
2368 if (gfc_statement_label != NULL)
2370 new_st.op = EXEC_NOP;
2375 case ST_IMPLIED_ENDDO:
2376 /* If the do-stmt of this DO construct has a do-construct-name,
2377 the corresponding end-do must be an end-do-stmt (with a matching
2378 name, but in that case we must have seen ST_ENDDO first).
2379 We only complain about this in pedantic mode. */
2380 if (gfc_current_block () != NULL)
2381 gfc_error_now ("named block DO at %L requires matching ENDDO name",
2382 &gfc_current_block()->declared_at);
2387 unexpected_statement (st);
2392 accept_statement (st);
2396 /* Parse the statements of OpenMP do/parallel do. */
2398 static gfc_statement
2399 parse_omp_do (gfc_statement omp_st)
2405 accept_statement (omp_st);
2407 cp = gfc_state_stack->tail;
2408 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2409 np = new_level (cp);
2415 st = next_statement ();
2418 else if (st == ST_DO)
2421 unexpected_statement (st);
2425 if (gfc_statement_label != NULL
2426 && gfc_state_stack->previous != NULL
2427 && gfc_state_stack->previous->state == COMP_DO
2428 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
2436 there should be no !$OMP END DO. */
2438 return ST_IMPLIED_ENDDO;
2441 check_do_closure ();
2444 st = next_statement ();
2445 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
2447 if (new_st.op == EXEC_OMP_END_NOWAIT)
2448 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2450 gcc_assert (new_st.op == EXEC_NOP);
2451 gfc_clear_new_st ();
2452 gfc_commit_symbols ();
2453 gfc_warning_check ();
2454 st = next_statement ();
2460 /* Parse the statements of OpenMP atomic directive. */
2463 parse_omp_atomic (void)
2469 accept_statement (ST_OMP_ATOMIC);
2471 cp = gfc_state_stack->tail;
2472 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2473 np = new_level (cp);
2479 st = next_statement ();
2482 else if (st == ST_ASSIGNMENT)
2485 unexpected_statement (st);
2488 accept_statement (st);
2494 /* Parse the statements of an OpenMP structured block. */
2497 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
2499 gfc_statement st, omp_end_st;
2503 accept_statement (omp_st);
2505 cp = gfc_state_stack->tail;
2506 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2507 np = new_level (cp);
2513 case ST_OMP_PARALLEL:
2514 omp_end_st = ST_OMP_END_PARALLEL;
2516 case ST_OMP_PARALLEL_SECTIONS:
2517 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
2519 case ST_OMP_SECTIONS:
2520 omp_end_st = ST_OMP_END_SECTIONS;
2522 case ST_OMP_ORDERED:
2523 omp_end_st = ST_OMP_END_ORDERED;
2525 case ST_OMP_CRITICAL:
2526 omp_end_st = ST_OMP_END_CRITICAL;
2529 omp_end_st = ST_OMP_END_MASTER;
2532 omp_end_st = ST_OMP_END_SINGLE;
2534 case ST_OMP_WORKSHARE:
2535 omp_end_st = ST_OMP_END_WORKSHARE;
2537 case ST_OMP_PARALLEL_WORKSHARE:
2538 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
2546 if (workshare_stmts_only)
2548 /* Inside of !$omp workshare, only
2551 where statements and constructs
2552 forall statements and constructs
2556 are allowed. For !$omp critical these
2557 restrictions apply recursively. */
2560 st = next_statement ();
2571 accept_statement (st);
2574 case ST_WHERE_BLOCK:
2575 parse_where_block ();
2578 case ST_FORALL_BLOCK:
2579 parse_forall_block ();
2582 case ST_OMP_PARALLEL:
2583 case ST_OMP_PARALLEL_SECTIONS:
2584 parse_omp_structured_block (st, false);
2587 case ST_OMP_PARALLEL_WORKSHARE:
2588 case ST_OMP_CRITICAL:
2589 parse_omp_structured_block (st, true);
2592 case ST_OMP_PARALLEL_DO:
2593 st = parse_omp_do (st);
2597 parse_omp_atomic ();
2608 st = next_statement ();
2612 st = parse_executable (ST_NONE);
2615 else if (st == ST_OMP_SECTION
2616 && (omp_st == ST_OMP_SECTIONS
2617 || omp_st == ST_OMP_PARALLEL_SECTIONS))
2619 np = new_level (np);
2623 else if (st != omp_end_st)
2624 unexpected_statement (st);
2626 while (st != omp_end_st);
2630 case EXEC_OMP_END_NOWAIT:
2631 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2633 case EXEC_OMP_CRITICAL:
2634 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
2635 || (new_st.ext.omp_name != NULL
2636 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
2637 gfc_error ("Name after !$omp critical and !$omp end critical does "
2639 gfc_free (CONST_CAST (char *, new_st.ext.omp_name));
2641 case EXEC_OMP_END_SINGLE:
2642 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
2643 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
2644 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
2645 gfc_free_omp_clauses (new_st.ext.omp_clauses);
2653 gfc_clear_new_st ();
2654 gfc_commit_symbols ();
2655 gfc_warning_check ();
2660 /* Accept a series of executable statements. We return the first
2661 statement that doesn't fit to the caller. Any block statements are
2662 passed on to the correct handler, which usually passes the buck
2665 static gfc_statement
2666 parse_executable (gfc_statement st)
2671 st = next_statement ();
2675 close_flag = check_do_closure ();
2680 case ST_END_PROGRAM:
2683 case ST_END_FUNCTION:
2687 case ST_END_SUBROUTINE:
2692 case ST_SELECT_CASE:
2693 gfc_error ("%s statement at %C cannot terminate a non-block "
2694 "DO loop", gfc_ascii_statement (st));
2710 accept_statement (st);
2711 if (close_flag == 1)
2712 return ST_IMPLIED_ENDDO;
2719 case ST_SELECT_CASE:
2720 parse_select_block ();
2725 if (check_do_closure () == 1)
2726 return ST_IMPLIED_ENDDO;
2729 case ST_WHERE_BLOCK:
2730 parse_where_block ();
2733 case ST_FORALL_BLOCK:
2734 parse_forall_block ();
2737 case ST_OMP_PARALLEL:
2738 case ST_OMP_PARALLEL_SECTIONS:
2739 case ST_OMP_SECTIONS:
2740 case ST_OMP_ORDERED:
2741 case ST_OMP_CRITICAL:
2744 parse_omp_structured_block (st, false);
2747 case ST_OMP_WORKSHARE:
2748 case ST_OMP_PARALLEL_WORKSHARE:
2749 parse_omp_structured_block (st, true);
2753 case ST_OMP_PARALLEL_DO:
2754 st = parse_omp_do (st);
2755 if (st == ST_IMPLIED_ENDDO)
2760 parse_omp_atomic ();
2767 st = next_statement ();
2772 /* Parse a series of contained program units. */
2774 static void parse_progunit (gfc_statement);
2777 /* Fix the symbols for sibling functions. These are incorrectly added to
2778 the child namespace as the parser didn't know about this procedure. */
2781 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
2785 gfc_symbol *old_sym;
2787 sym->attr.referenced = 1;
2788 for (ns = siblings; ns; ns = ns->sibling)
2790 gfc_find_sym_tree (sym->name, ns, 0, &st);
2792 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
2795 old_sym = st->n.sym;
2796 if ((old_sym->attr.flavor == FL_PROCEDURE
2797 || old_sym->ts.type == BT_UNKNOWN)
2798 && old_sym->ns == ns
2799 && !old_sym->attr.contained
2800 && old_sym->attr.flavor != FL_NAMELIST)
2802 /* Replace it with the symbol from the parent namespace. */
2806 /* Free the old (local) symbol. */
2808 if (old_sym->refs == 0)
2809 gfc_free_symbol (old_sym);
2812 /* Do the same for any contained procedures. */
2813 gfc_fixup_sibling_symbols (sym, ns->contained);
2818 parse_contained (int module)
2820 gfc_namespace *ns, *parent_ns, *tmp;
2821 gfc_state_data s1, s2;
2825 int contains_statements = 0;
2828 push_state (&s1, COMP_CONTAINS, NULL);
2829 parent_ns = gfc_current_ns;
2833 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2835 gfc_current_ns->sibling = parent_ns->contained;
2836 parent_ns->contained = gfc_current_ns;
2839 /* Process the next available statement. We come here if we got an error
2840 and rejected the last statement. */
2841 st = next_statement ();
2850 contains_statements = 1;
2851 accept_statement (st);
2854 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2857 /* For internal procedures, create/update the symbol in the
2858 parent namespace. */
2862 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2863 gfc_error ("Contained procedure '%s' at %C is already "
2864 "ambiguous", gfc_new_block->name);
2867 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2868 &gfc_new_block->declared_at) ==
2871 if (st == ST_FUNCTION)
2872 gfc_add_function (&sym->attr, sym->name,
2873 &gfc_new_block->declared_at);
2875 gfc_add_subroutine (&sym->attr, sym->name,
2876 &gfc_new_block->declared_at);
2880 gfc_commit_symbols ();
2883 sym = gfc_new_block;
2885 /* Mark this as a contained function, so it isn't replaced
2886 by other module functions. */
2887 sym->attr.contained = 1;
2888 sym->attr.referenced = 1;
2890 parse_progunit (ST_NONE);
2892 /* Fix up any sibling functions that refer to this one. */
2893 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2894 /* Or refer to any of its alternate entry points. */
2895 for (el = gfc_current_ns->entries; el; el = el->next)
2896 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2898 gfc_current_ns->code = s2.head;
2899 gfc_current_ns = parent_ns;
2904 /* These statements are associated with the end of the host unit. */
2905 case ST_END_FUNCTION:
2907 case ST_END_PROGRAM:
2908 case ST_END_SUBROUTINE:
2909 accept_statement (st);
2913 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2914 gfc_ascii_statement (st));
2915 reject_statement ();
2921 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2922 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2924 /* The first namespace in the list is guaranteed to not have
2925 anything (worthwhile) in it. */
2926 tmp = gfc_current_ns;
2927 gfc_current_ns = parent_ns;
2928 if (seen_error && tmp->refs > 1)
2929 gfc_free_namespace (tmp);
2931 ns = gfc_current_ns->contained;
2932 gfc_current_ns->contained = ns->sibling;
2933 gfc_free_namespace (ns);
2936 if (!contains_statements)
2937 /* This is valid in Fortran 2008. */
2938 gfc_notify_std (GFC_STD_GNU, "Extension: CONTAINS statement without "
2939 "FUNCTION or SUBROUTINE statement at %C");
2943 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2946 parse_progunit (gfc_statement st)
2951 st = parse_spec (st);
2961 accept_statement (st);
2968 if (gfc_current_state () == COMP_FUNCTION)
2969 gfc_check_function_type (gfc_current_ns);
2974 st = parse_executable (st);
2985 accept_statement (st);
2992 unexpected_statement (st);
2993 reject_statement ();
2994 st = next_statement ();
3000 for (p = gfc_state_stack; p; p = p->previous)
3001 if (p->state == COMP_CONTAINS)
3004 if (gfc_find_state (COMP_MODULE) == SUCCESS)
3009 gfc_error ("CONTAINS statement at %C is already in a contained "
3011 st = next_statement ();
3015 parse_contained (0);
3018 gfc_current_ns->code = gfc_state_stack->head;
3022 /* Come here to complain about a global symbol already in use as
3026 global_used (gfc_gsymbol *sym, locus *where)
3031 where = &gfc_current_locus;
3041 case GSYM_SUBROUTINE:
3042 name = "SUBROUTINE";
3047 case GSYM_BLOCK_DATA:
3048 name = "BLOCK DATA";
3054 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
3058 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
3059 sym->name, where, name, &sym->where);
3063 /* Parse a block data program unit. */
3066 parse_block_data (void)
3069 static locus blank_locus;
3070 static int blank_block=0;
3073 gfc_current_ns->proc_name = gfc_new_block;
3074 gfc_current_ns->is_block_data = 1;
3076 if (gfc_new_block == NULL)
3079 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3080 "prior BLOCK DATA at %L", &blank_locus);
3084 blank_locus = gfc_current_locus;
3089 s = gfc_get_gsymbol (gfc_new_block->name);
3091 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
3092 global_used(s, NULL);
3095 s->type = GSYM_BLOCK_DATA;
3096 s->where = gfc_current_locus;
3101 st = parse_spec (ST_NONE);
3103 while (st != ST_END_BLOCK_DATA)
3105 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3106 gfc_ascii_statement (st));
3107 reject_statement ();
3108 st = next_statement ();
3113 /* Parse a module subprogram. */
3121 s = gfc_get_gsymbol (gfc_new_block->name);
3122 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
3123 global_used(s, NULL);
3126 s->type = GSYM_MODULE;
3127 s->where = gfc_current_locus;
3131 st = parse_spec (ST_NONE);
3140 parse_contained (1);
3144 accept_statement (st);
3148 gfc_error ("Unexpected %s statement in MODULE at %C",
3149 gfc_ascii_statement (st));
3151 reject_statement ();
3152 st = next_statement ();
3158 /* Add a procedure name to the global symbol table. */
3161 add_global_procedure (int sub)
3165 s = gfc_get_gsymbol(gfc_new_block->name);
3168 || (s->type != GSYM_UNKNOWN
3169 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3170 global_used(s, NULL);
3173 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3174 s->where = gfc_current_locus;
3180 /* Add a program to the global symbol table. */
3183 add_global_program (void)
3187 if (gfc_new_block == NULL)
3189 s = gfc_get_gsymbol (gfc_new_block->name);
3191 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
3192 global_used(s, NULL);
3195 s->type = GSYM_PROGRAM;
3196 s->where = gfc_current_locus;
3202 /* Top level parser. */
3205 gfc_parse_file (void)
3207 int seen_program, errors_before, errors;
3208 gfc_state_data top, s;
3212 top.state = COMP_NONE;
3214 top.previous = NULL;
3215 top.head = top.tail = NULL;
3216 top.do_variable = NULL;
3218 gfc_state_stack = ⊤
3220 gfc_clear_new_st ();
3222 gfc_statement_label = NULL;
3224 if (setjmp (eof_buf))
3225 return FAILURE; /* Come here on unexpected EOF */
3229 /* Exit early for empty files. */
3235 st = next_statement ();
3244 goto duplicate_main;
3246 prog_locus = gfc_current_locus;
3248 push_state (&s, COMP_PROGRAM, gfc_new_block);
3249 main_program_symbol(gfc_current_ns);
3250 accept_statement (st);
3251 add_global_program ();
3252 parse_progunit (ST_NONE);
3256 add_global_procedure (1);
3257 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
3258 accept_statement (st);
3259 parse_progunit (ST_NONE);
3263 add_global_procedure (0);
3264 push_state (&s, COMP_FUNCTION, gfc_new_block);
3265 accept_statement (st);
3266 parse_progunit (ST_NONE);
3270 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
3271 accept_statement (st);
3272 parse_block_data ();
3276 push_state (&s, COMP_MODULE, gfc_new_block);
3277 accept_statement (st);
3279 gfc_get_errors (NULL, &errors_before);
3283 /* Anything else starts a nameless main program block. */
3286 goto duplicate_main;
3288 prog_locus = gfc_current_locus;
3290 push_state (&s, COMP_PROGRAM, gfc_new_block);
3291 main_program_symbol (gfc_current_ns);
3292 parse_progunit (st);
3296 gfc_current_ns->code = s.head;
3298 gfc_resolve (gfc_current_ns);
3300 /* Dump the parse tree if requested. */
3301 if (gfc_option.verbose)
3302 gfc_show_namespace (gfc_current_ns);
3304 gfc_get_errors (NULL, &errors);
3305 if (s.state == COMP_MODULE)
3307 gfc_dump_module (s.sym->name, errors_before == errors);
3309 gfc_generate_module_code (gfc_current_ns);
3314 gfc_generate_code (gfc_current_ns);
3325 /* If we see a duplicate main program, shut down. If the second
3326 instance is an implied main program, ie data decls or executable
3327 statements, we're in for lots of errors. */
3328 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
3329 reject_statement ();