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);
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 ("allocate", gfc_match_allocate, ST_ALLOCATE);
176 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
177 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
181 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
182 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
186 match ("call", gfc_match_call, ST_CALL);
187 match ("close", gfc_match_close, ST_CLOSE);
188 match ("continue", gfc_match_continue, ST_CONTINUE);
189 match ("cycle", gfc_match_cycle, ST_CYCLE);
190 match ("case", gfc_match_case, ST_CASE);
191 match ("common", gfc_match_common, ST_COMMON);
192 match ("contains", gfc_match_eos, ST_CONTAINS);
196 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
197 match ("data", gfc_match_data, ST_DATA);
198 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
202 match ("end file", gfc_match_endfile, ST_END_FILE);
203 match ("exit", gfc_match_exit, ST_EXIT);
204 match ("else", gfc_match_else, ST_ELSE);
205 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
206 match ("else if", gfc_match_elseif, ST_ELSEIF);
207 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
209 if (gfc_match_end (&st) == MATCH_YES)
212 match ("entry% ", gfc_match_entry, ST_ENTRY);
213 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
214 match ("external", gfc_match_external, ST_ATTR_DECL);
218 match ("flush", gfc_match_flush, ST_FLUSH);
219 match ("format", gfc_match_format, ST_FORMAT);
223 match ("go to", gfc_match_goto, ST_GOTO);
227 match ("inquire", gfc_match_inquire, ST_INQUIRE);
228 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
229 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
230 match ("import", gfc_match_import, ST_IMPORT);
231 match ("interface", gfc_match_interface, ST_INTERFACE);
232 match ("intent", gfc_match_intent, ST_ATTR_DECL);
233 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
237 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
238 match ("module", gfc_match_module, ST_MODULE);
242 match ("nullify", gfc_match_nullify, ST_NULLIFY);
243 match ("namelist", gfc_match_namelist, ST_NAMELIST);
247 match ("open", gfc_match_open, ST_OPEN);
248 match ("optional", gfc_match_optional, ST_ATTR_DECL);
252 match ("print", gfc_match_print, ST_WRITE);
253 match ("parameter", gfc_match_parameter, ST_PARAMETER);
254 match ("pause", gfc_match_pause, ST_PAUSE);
255 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
256 if (gfc_match_private (&st) == MATCH_YES)
258 match ("program", gfc_match_program, ST_PROGRAM);
259 if (gfc_match_public (&st) == MATCH_YES)
261 match ("protected", gfc_match_protected, ST_ATTR_DECL);
265 match ("read", gfc_match_read, ST_READ);
266 match ("return", gfc_match_return, ST_RETURN);
267 match ("rewind", gfc_match_rewind, ST_REWIND);
271 match ("sequence", gfc_match_eos, ST_SEQUENCE);
272 match ("stop", gfc_match_stop, ST_STOP);
273 match ("save", gfc_match_save, ST_ATTR_DECL);
277 match ("target", gfc_match_target, ST_ATTR_DECL);
278 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
282 match ("use", gfc_match_use, ST_USE);
286 match ("value", gfc_match_value, ST_ATTR_DECL);
287 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
291 match ("write", gfc_match_write, ST_WRITE);
295 /* All else has failed, so give up. See if any of the matchers has
296 stored an error message of some sort. */
298 if (gfc_error_check () == 0)
299 gfc_error_now ("Unclassifiable statement at %C");
303 gfc_error_recovery ();
309 decode_omp_directive (void)
318 gfc_clear_error (); /* Clear any pending errors. */
319 gfc_clear_warning (); /* Clear any pending warnings. */
323 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
324 "or ELEMENTAL procedures");
325 gfc_error_recovery ();
329 old_locus = gfc_current_locus;
331 /* General OpenMP directive matching: Instead of testing every possible
332 statement, we eliminate most possibilities by peeking at the
335 c = gfc_peek_char ();
340 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
343 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
346 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
349 match ("do", gfc_match_omp_do, ST_OMP_DO);
352 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
353 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
354 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
355 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
356 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
357 match ("end parallel sections", gfc_match_omp_eos,
358 ST_OMP_END_PARALLEL_SECTIONS);
359 match ("end parallel workshare", gfc_match_omp_eos,
360 ST_OMP_END_PARALLEL_WORKSHARE);
361 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
362 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
363 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
364 match ("end workshare", gfc_match_omp_end_nowait,
365 ST_OMP_END_WORKSHARE);
368 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
371 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
374 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
377 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
378 match ("parallel sections", gfc_match_omp_parallel_sections,
379 ST_OMP_PARALLEL_SECTIONS);
380 match ("parallel workshare", gfc_match_omp_parallel_workshare,
381 ST_OMP_PARALLEL_WORKSHARE);
382 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
385 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
386 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
387 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
390 match ("threadprivate", gfc_match_omp_threadprivate,
391 ST_OMP_THREADPRIVATE);
393 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
397 /* All else has failed, so give up. See if any of the matchers has
398 stored an error message of some sort. */
400 if (gfc_error_check () == 0)
401 gfc_error_now ("Unclassifiable OpenMP directive at %C");
405 gfc_error_recovery ();
413 /* Get the next statement in free form source. */
419 int c, d, cnt, at_bol;
421 at_bol = gfc_at_bol ();
422 gfc_gobble_whitespace ();
424 c = gfc_peek_char ();
428 /* Found a statement label? */
429 m = gfc_match_st_label (&gfc_statement_label);
431 d = gfc_peek_char ();
432 if (m != MATCH_YES || !gfc_is_whitespace (d))
434 gfc_match_small_literal_int (&c, &cnt);
437 gfc_error_now ("Too many digits in statement label at %C");
440 gfc_error_now ("Zero is not a valid statement label at %C");
443 c = gfc_next_char ();
446 if (!gfc_is_whitespace (c))
447 gfc_error_now ("Non-numeric character in statement label at %C");
453 label_locus = gfc_current_locus;
455 gfc_gobble_whitespace ();
457 if (at_bol && gfc_peek_char () == ';')
459 gfc_error_now ("Semicolon at %C needs to be preceded by "
461 gfc_next_char (); /* Eat up the semicolon. */
465 if (gfc_match_eos () == MATCH_YES)
467 gfc_warning_now ("Ignoring statement label in empty statement "
469 gfc_free_st_label (gfc_statement_label);
470 gfc_statement_label = NULL;
477 /* Comments have already been skipped by the time we get here,
478 except for OpenMP directives. */
479 if (gfc_option.flag_openmp)
483 c = gfc_next_char ();
484 for (i = 0; i < 5; i++, c = gfc_next_char ())
485 gcc_assert (c == "!$omp"[i]);
487 gcc_assert (c == ' ');
488 return decode_omp_directive ();
492 if (at_bol && c == ';')
494 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
495 gfc_next_char (); /* Eat up the semicolon. */
499 return decode_statement ();
503 /* Get the next statement in fixed-form source. */
508 int label, digit_flag, i;
513 return decode_statement ();
515 /* Skip past the current label field, parsing a statement label if
516 one is there. This is a weird number parser, since the number is
517 contained within five columns and can have any kind of embedded
518 spaces. We also check for characters that make the rest of the
524 for (i = 0; i < 5; i++)
526 c = gfc_next_char_literal (0);
543 label = label * 10 + c - '0';
544 label_locus = gfc_current_locus;
548 /* Comments have already been skipped by the time we get
549 here, except for OpenMP directives. */
551 if (gfc_option.flag_openmp)
553 for (i = 0; i < 5; i++, c = gfc_next_char_literal (0))
554 gcc_assert (TOLOWER (c) == "*$omp"[i]);
556 if (c != ' ' && c != '0')
558 gfc_buffer_error (0);
559 gfc_error ("Bad continuation line at %C");
563 return decode_omp_directive ();
567 /* Comments have already been skipped by the time we get
568 here so don't bother checking for them. */
571 gfc_buffer_error (0);
572 gfc_error ("Non-numeric character in statement label at %C");
580 gfc_warning_now ("Zero is not a valid statement label at %C");
583 /* We've found a valid statement label. */
584 gfc_statement_label = gfc_get_st_label (label);
588 /* Since this line starts a statement, it cannot be a continuation
589 of a previous statement. If we see something here besides a
590 space or zero, it must be a bad continuation line. */
592 c = gfc_next_char_literal (0);
596 if (c != ' ' && c != '0')
598 gfc_buffer_error (0);
599 gfc_error ("Bad continuation line at %C");
603 /* Now that we've taken care of the statement label columns, we have
604 to make sure that the first nonblank character is not a '!'. If
605 it is, the rest of the line is a comment. */
609 loc = gfc_current_locus;
610 c = gfc_next_char_literal (0);
612 while (gfc_is_whitespace (c));
616 gfc_current_locus = loc;
620 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
624 if (gfc_match_eos () == MATCH_YES)
627 /* At this point, we've got a nonblank statement to parse. */
628 return decode_statement ();
632 gfc_warning ("Ignoring statement label in empty statement at %C");
638 /* Return the next non-ST_NONE statement to the caller. We also worry
639 about including files and the ends of include files at this stage. */
642 next_statement (void)
646 gfc_new_block = NULL;
650 gfc_statement_label = NULL;
651 gfc_buffer_error (1);
655 if (gfc_option.warn_line_truncation
656 && gfc_current_locus.lb
657 && gfc_current_locus.lb->truncated)
658 gfc_warning_now ("Line truncated at %C");
663 gfc_skip_comments ();
671 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
677 gfc_buffer_error (0);
680 check_statement_label (st);
686 /****************************** Parser ***********************************/
688 /* The parser subroutines are of type 'try' that fail if the file ends
691 /* Macros that expand to case-labels for various classes of
692 statements. Start with executable statements that directly do
695 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
696 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
697 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
698 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
699 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
700 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
701 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
702 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
705 /* Statements that mark other executable statements. */
707 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
708 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
709 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
710 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
711 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
712 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
714 /* Declaration statements */
716 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
717 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
718 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE
720 /* Block end statements. Errors associated with interchanging these
721 are detected in gfc_match_end(). */
723 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
724 case ST_END_PROGRAM: case ST_END_SUBROUTINE
727 /* Push a new state onto the stack. */
730 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
732 p->state = new_state;
733 p->previous = gfc_state_stack;
735 p->head = p->tail = NULL;
736 p->do_variable = NULL;
741 /* Pop the current state. */
746 gfc_state_stack = gfc_state_stack->previous;
750 /* Try to find the given state in the state stack. */
753 gfc_find_state (gfc_compile_state state)
757 for (p = gfc_state_stack; p; p = p->previous)
758 if (p->state == state)
761 return (p == NULL) ? FAILURE : SUCCESS;
765 /* Starts a new level in the statement list. */
768 new_level (gfc_code *q)
772 p = q->block = gfc_get_code ();
774 gfc_state_stack->head = gfc_state_stack->tail = p;
780 /* Add the current new_st code structure and adds it to the current
781 program unit. As a side-effect, it zeroes the new_st. */
791 p->loc = gfc_current_locus;
793 if (gfc_state_stack->head == NULL)
794 gfc_state_stack->head = p;
796 gfc_state_stack->tail->next = p;
798 while (p->next != NULL)
801 gfc_state_stack->tail = p;
809 /* Frees everything associated with the current statement. */
812 undo_new_statement (void)
814 gfc_free_statements (new_st.block);
815 gfc_free_statements (new_st.next);
816 gfc_free_statement (&new_st);
821 /* If the current statement has a statement label, make sure that it
822 is allowed to, or should have one. */
825 check_statement_label (gfc_statement st)
829 if (gfc_statement_label == NULL)
832 gfc_error ("FORMAT statement at %L does not have a statement label",
840 case ST_END_FUNCTION:
841 case ST_END_SUBROUTINE:
847 type = ST_LABEL_TARGET;
851 type = ST_LABEL_FORMAT;
854 /* Statement labels are not restricted from appearing on a
855 particular line. However, there are plenty of situations
856 where the resulting label can't be referenced. */
859 type = ST_LABEL_BAD_TARGET;
863 gfc_define_st_label (gfc_statement_label, type, &label_locus);
865 new_st.here = gfc_statement_label;
869 /* Figures out what the enclosing program unit is. This will be a
870 function, subroutine, program, block data or module. */
873 gfc_enclosing_unit (gfc_compile_state * result)
877 for (p = gfc_state_stack; p; p = p->previous)
878 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
879 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
880 || p->state == COMP_PROGRAM)
889 *result = COMP_PROGRAM;
894 /* Translate a statement enum to a string. */
897 gfc_ascii_statement (gfc_statement st)
903 case ST_ARITHMETIC_IF:
904 p = _("arithmetic IF");
910 p = _("attribute declaration");
940 p = _("data declaration");
948 case ST_DERIVED_DECL:
949 p = _("derived type declaration");
963 case ST_END_BLOCK_DATA:
964 p = "END BLOCK DATA";
975 case ST_END_FUNCTION:
981 case ST_END_INTERFACE:
993 case ST_END_SUBROUTINE:
994 p = "END SUBROUTINE";
1005 case ST_EQUIVALENCE:
1014 case ST_FORALL_BLOCK: /* Fall through */
1033 case ST_IMPLICIT_NONE:
1034 p = "IMPLICIT NONE";
1036 case ST_IMPLIED_ENDDO:
1037 p = _("implied END DO");
1063 case ST_MODULE_PROC:
1064 p = "MODULE PROCEDURE";
1099 case ST_WHERE_BLOCK: /* Fall through */
1107 p = _("assignment");
1109 case ST_POINTER_ASSIGNMENT:
1110 p = _("pointer assignment");
1112 case ST_SELECT_CASE:
1121 case ST_STATEMENT_FUNCTION:
1122 p = "STATEMENT FUNCTION";
1124 case ST_LABEL_ASSIGNMENT:
1125 p = "LABEL ASSIGNMENT";
1128 p = "ENUM DEFINITION";
1131 p = "ENUMERATOR DEFINITION";
1139 case ST_OMP_BARRIER:
1140 p = "!$OMP BARRIER";
1142 case ST_OMP_CRITICAL:
1143 p = "!$OMP CRITICAL";
1148 case ST_OMP_END_CRITICAL:
1149 p = "!$OMP END CRITICAL";
1154 case ST_OMP_END_MASTER:
1155 p = "!$OMP END MASTER";
1157 case ST_OMP_END_ORDERED:
1158 p = "!$OMP END ORDERED";
1160 case ST_OMP_END_PARALLEL:
1161 p = "!$OMP END PARALLEL";
1163 case ST_OMP_END_PARALLEL_DO:
1164 p = "!$OMP END PARALLEL DO";
1166 case ST_OMP_END_PARALLEL_SECTIONS:
1167 p = "!$OMP END PARALLEL SECTIONS";
1169 case ST_OMP_END_PARALLEL_WORKSHARE:
1170 p = "!$OMP END PARALLEL WORKSHARE";
1172 case ST_OMP_END_SECTIONS:
1173 p = "!$OMP END SECTIONS";
1175 case ST_OMP_END_SINGLE:
1176 p = "!$OMP END SINGLE";
1178 case ST_OMP_END_WORKSHARE:
1179 p = "!$OMP END WORKSHARE";
1187 case ST_OMP_ORDERED:
1188 p = "!$OMP ORDERED";
1190 case ST_OMP_PARALLEL:
1191 p = "!$OMP PARALLEL";
1193 case ST_OMP_PARALLEL_DO:
1194 p = "!$OMP PARALLEL DO";
1196 case ST_OMP_PARALLEL_SECTIONS:
1197 p = "!$OMP PARALLEL SECTIONS";
1199 case ST_OMP_PARALLEL_WORKSHARE:
1200 p = "!$OMP PARALLEL WORKSHARE";
1202 case ST_OMP_SECTIONS:
1203 p = "!$OMP SECTIONS";
1205 case ST_OMP_SECTION:
1206 p = "!$OMP SECTION";
1211 case ST_OMP_THREADPRIVATE:
1212 p = "!$OMP THREADPRIVATE";
1214 case ST_OMP_WORKSHARE:
1215 p = "!$OMP WORKSHARE";
1218 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1225 /* Create a symbol for the main program and assign it to ns->proc_name. */
1228 main_program_symbol (gfc_namespace *ns)
1230 gfc_symbol *main_program;
1231 symbol_attribute attr;
1233 gfc_get_symbol ("MAIN__", ns, &main_program);
1234 gfc_clear_attr (&attr);
1235 attr.flavor = FL_PROCEDURE;
1236 attr.proc = PROC_UNKNOWN;
1237 attr.subroutine = 1;
1238 attr.access = ACCESS_PUBLIC;
1239 attr.is_main_program = 1;
1240 main_program->attr = attr;
1241 main_program->declared_at = gfc_current_locus;
1242 ns->proc_name = main_program;
1243 gfc_commit_symbols ();
1247 /* Do whatever is necessary to accept the last statement. */
1250 accept_statement (gfc_statement st)
1258 case ST_IMPLICIT_NONE:
1259 gfc_set_implicit_none ();
1268 gfc_current_ns->proc_name = gfc_new_block;
1271 /* If the statement is the end of a block, lay down a special code
1272 that allows a branch to the end of the block from within the
1277 if (gfc_statement_label != NULL)
1279 new_st.op = EXEC_NOP;
1285 /* The end-of-program unit statements do not get the special
1286 marker and require a statement of some sort if they are a
1289 case ST_END_PROGRAM:
1290 case ST_END_FUNCTION:
1291 case ST_END_SUBROUTINE:
1292 if (gfc_statement_label != NULL)
1294 new_st.op = EXEC_RETURN;
1310 gfc_commit_symbols ();
1311 gfc_warning_check ();
1312 gfc_clear_new_st ();
1316 /* Undo anything tentative that has been built for the current
1320 reject_statement (void)
1322 gfc_new_block = NULL;
1323 gfc_undo_symbols ();
1324 gfc_clear_warning ();
1325 undo_new_statement ();
1329 /* Generic complaint about an out of order statement. We also do
1330 whatever is necessary to clean up. */
1333 unexpected_statement (gfc_statement st)
1335 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1337 reject_statement ();
1341 /* Given the next statement seen by the matcher, make sure that it is
1342 in proper order with the last. This subroutine is initialized by
1343 calling it with an argument of ST_NONE. If there is a problem, we
1344 issue an error and return FAILURE. Otherwise we return SUCCESS.
1346 Individual parsers need to verify that the statements seen are
1347 valid before calling here, ie ENTRY statements are not allowed in
1348 INTERFACE blocks. The following diagram is taken from the standard:
1350 +---------------------------------------+
1351 | program subroutine function module |
1352 +---------------------------------------+
1354 +---------------------------------------+
1356 +---------------------------------------+
1358 | +-----------+------------------+
1359 | | parameter | implicit |
1360 | +-----------+------------------+
1361 | format | | derived type |
1362 | entry | parameter | interface |
1363 | | data | specification |
1364 | | | statement func |
1365 | +-----------+------------------+
1366 | | data | executable |
1367 +--------+-----------+------------------+
1369 +---------------------------------------+
1370 | internal module/subprogram |
1371 +---------------------------------------+
1373 +---------------------------------------+
1380 { ORDER_START, ORDER_USE, ORDER_IMPORT, ORDER_IMPLICIT_NONE,
1381 ORDER_IMPLICIT, ORDER_SPEC, ORDER_EXEC
1384 gfc_statement last_statement;
1390 verify_st_order (st_state *p, gfc_statement st)
1396 p->state = ORDER_START;
1400 if (p->state > ORDER_USE)
1402 p->state = ORDER_USE;
1406 if (p->state > ORDER_IMPORT)
1408 p->state = ORDER_IMPORT;
1411 case ST_IMPLICIT_NONE:
1412 if (p->state > ORDER_IMPLICIT_NONE)
1415 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1416 statement disqualifies a USE but not an IMPLICIT NONE.
1417 Duplicate IMPLICIT NONEs are caught when the implicit types
1420 p->state = ORDER_IMPLICIT_NONE;
1424 if (p->state > ORDER_IMPLICIT)
1426 p->state = ORDER_IMPLICIT;
1431 if (p->state < ORDER_IMPLICIT_NONE)
1432 p->state = ORDER_IMPLICIT_NONE;
1436 if (p->state >= ORDER_EXEC)
1438 if (p->state < ORDER_IMPLICIT)
1439 p->state = ORDER_IMPLICIT;
1443 if (p->state < ORDER_SPEC)
1444 p->state = ORDER_SPEC;
1449 case ST_DERIVED_DECL:
1451 if (p->state >= ORDER_EXEC)
1453 if (p->state < ORDER_SPEC)
1454 p->state = ORDER_SPEC;
1459 if (p->state < ORDER_EXEC)
1460 p->state = ORDER_EXEC;
1464 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1465 gfc_ascii_statement (st));
1468 /* All is well, record the statement in case we need it next time. */
1469 p->where = gfc_current_locus;
1470 p->last_statement = st;
1474 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1475 gfc_ascii_statement (st),
1476 gfc_ascii_statement (p->last_statement), &p->where);
1482 /* Handle an unexpected end of file. This is a show-stopper... */
1484 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1487 unexpected_eof (void)
1491 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1493 /* Memory cleanup. Move to "second to last". */
1494 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1497 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1500 longjmp (eof_buf, 1);
1504 /* Parse a derived type. */
1507 parse_derived (void)
1509 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1517 accept_statement (ST_DERIVED_DECL);
1518 push_state (&s, COMP_DERIVED, gfc_new_block);
1520 gfc_new_block->component_access = ACCESS_PUBLIC;
1527 while (compiling_type)
1529 st = next_statement ();
1536 accept_statement (st);
1543 if (!seen_component)
1545 gfc_error ("Derived type definition at %C has no components");
1549 accept_statement (ST_END_TYPE);
1553 if (gfc_find_state (COMP_MODULE) == FAILURE)
1555 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1563 gfc_error ("PRIVATE statement at %C must precede "
1564 "structure components");
1571 gfc_error ("Duplicate PRIVATE statement at %C");
1575 s.sym->component_access = ACCESS_PRIVATE;
1576 accept_statement (ST_PRIVATE);
1583 gfc_error ("SEQUENCE statement at %C must precede "
1584 "structure components");
1589 if (gfc_current_block ()->attr.sequence)
1590 gfc_warning ("SEQUENCE attribute at %C already specified in "
1595 gfc_error ("Duplicate SEQUENCE statement at %C");
1600 gfc_add_sequence (&gfc_current_block ()->attr,
1601 gfc_current_block ()->name, NULL);
1605 unexpected_statement (st);
1610 /* Look for allocatable components. */
1611 sym = gfc_current_block ();
1612 for (c = sym->components; c; c = c->next)
1615 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.alloc_comp))
1617 sym->attr.alloc_comp = 1;
1626 /* Parse an ENUM. */
1635 int seen_enumerator = 0;
1639 push_state (&s, COMP_ENUM, gfc_new_block);
1643 while (compiling_enum)
1645 st = next_statement ();
1653 seen_enumerator = 1;
1654 accept_statement (st);
1659 if (!seen_enumerator)
1661 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1664 accept_statement (st);
1668 gfc_free_enum_history ();
1669 unexpected_statement (st);
1677 /* Parse an interface. We must be able to deal with the possibility
1678 of recursive interfaces. The parse_spec() subroutine is mutually
1679 recursive with parse_interface(). */
1681 static gfc_statement parse_spec (gfc_statement);
1684 parse_interface (void)
1686 gfc_compile_state new_state, current_state;
1687 gfc_symbol *prog_unit, *sym;
1688 gfc_interface_info save;
1689 gfc_state_data s1, s2;
1693 accept_statement (ST_INTERFACE);
1695 current_interface.ns = gfc_current_ns;
1696 save = current_interface;
1698 sym = (current_interface.type == INTERFACE_GENERIC
1699 || current_interface.type == INTERFACE_USER_OP)
1700 ? gfc_new_block : NULL;
1702 push_state (&s1, COMP_INTERFACE, sym);
1703 current_state = COMP_NONE;
1706 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1708 st = next_statement ();
1715 new_state = COMP_SUBROUTINE;
1716 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1717 gfc_new_block->formal, NULL);
1721 new_state = COMP_FUNCTION;
1722 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1723 gfc_new_block->formal, NULL);
1726 case ST_MODULE_PROC: /* The module procedure matcher makes
1727 sure the context is correct. */
1728 accept_statement (st);
1729 gfc_free_namespace (gfc_current_ns);
1732 case ST_END_INTERFACE:
1733 gfc_free_namespace (gfc_current_ns);
1734 gfc_current_ns = current_interface.ns;
1738 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1739 gfc_ascii_statement (st));
1740 reject_statement ();
1741 gfc_free_namespace (gfc_current_ns);
1746 /* Make sure that a generic interface has only subroutines or
1747 functions and that the generic name has the right attribute. */
1748 if (current_interface.type == INTERFACE_GENERIC)
1750 if (current_state == COMP_NONE)
1752 if (new_state == COMP_FUNCTION)
1753 gfc_add_function (&sym->attr, sym->name, NULL);
1754 else if (new_state == COMP_SUBROUTINE)
1755 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1757 current_state = new_state;
1761 if (new_state != current_state)
1763 if (new_state == COMP_SUBROUTINE)
1764 gfc_error ("SUBROUTINE at %C does not belong in a "
1765 "generic function interface");
1767 if (new_state == COMP_FUNCTION)
1768 gfc_error ("FUNCTION at %C does not belong in a "
1769 "generic subroutine interface");
1774 push_state (&s2, new_state, gfc_new_block);
1775 accept_statement (st);
1776 prog_unit = gfc_new_block;
1777 prog_unit->formal_ns = gfc_current_ns;
1778 proc_locus = gfc_current_locus;
1781 /* Read data declaration statements. */
1782 st = parse_spec (ST_NONE);
1784 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1786 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1787 gfc_ascii_statement (st));
1788 reject_statement ();
1792 current_interface = save;
1793 gfc_add_interface (prog_unit);
1796 if (current_interface.ns
1797 && current_interface.ns->proc_name
1798 && strcmp (current_interface.ns->proc_name->name,
1799 prog_unit->name) == 0)
1800 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
1801 "enclosing procedure", prog_unit->name, &proc_locus);
1810 /* Parse a set of specification statements. Returns the statement
1811 that doesn't fit. */
1813 static gfc_statement
1814 parse_spec (gfc_statement st)
1818 verify_st_order (&ss, ST_NONE);
1820 st = next_statement ();
1830 case ST_DATA: /* Not allowed in interfaces */
1831 if (gfc_current_state () == COMP_INTERFACE)
1838 case ST_IMPLICIT_NONE:
1843 case ST_DERIVED_DECL:
1845 if (verify_st_order (&ss, st) == FAILURE)
1847 reject_statement ();
1848 st = next_statement ();
1858 case ST_DERIVED_DECL:
1864 if (gfc_current_state () != COMP_MODULE)
1866 gfc_error ("%s statement must appear in a MODULE",
1867 gfc_ascii_statement (st));
1871 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1873 gfc_error ("%s statement at %C follows another accessibility "
1874 "specification", gfc_ascii_statement (st));
1878 gfc_current_ns->default_access = (st == ST_PUBLIC)
1879 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1887 accept_statement (st);
1888 st = next_statement ();
1892 accept_statement (st);
1894 st = next_statement ();
1905 /* Parse a WHERE block, (not a simple WHERE statement). */
1908 parse_where_block (void)
1910 int seen_empty_else;
1915 accept_statement (ST_WHERE_BLOCK);
1916 top = gfc_state_stack->tail;
1918 push_state (&s, COMP_WHERE, gfc_new_block);
1920 d = add_statement ();
1921 d->expr = top->expr;
1927 seen_empty_else = 0;
1931 st = next_statement ();
1937 case ST_WHERE_BLOCK:
1938 parse_where_block ();
1943 accept_statement (st);
1947 if (seen_empty_else)
1949 gfc_error ("ELSEWHERE statement at %C follows previous "
1950 "unmasked ELSEWHERE");
1954 if (new_st.expr == NULL)
1955 seen_empty_else = 1;
1957 d = new_level (gfc_state_stack->head);
1959 d->expr = new_st.expr;
1961 accept_statement (st);
1966 accept_statement (st);
1970 gfc_error ("Unexpected %s statement in WHERE block at %C",
1971 gfc_ascii_statement (st));
1972 reject_statement ();
1976 while (st != ST_END_WHERE);
1982 /* Parse a FORALL block (not a simple FORALL statement). */
1985 parse_forall_block (void)
1991 accept_statement (ST_FORALL_BLOCK);
1992 top = gfc_state_stack->tail;
1994 push_state (&s, COMP_FORALL, gfc_new_block);
1996 d = add_statement ();
1997 d->op = EXEC_FORALL;
2002 st = next_statement ();
2007 case ST_POINTER_ASSIGNMENT:
2010 accept_statement (st);
2013 case ST_WHERE_BLOCK:
2014 parse_where_block ();
2017 case ST_FORALL_BLOCK:
2018 parse_forall_block ();
2022 accept_statement (st);
2029 gfc_error ("Unexpected %s statement in FORALL block at %C",
2030 gfc_ascii_statement (st));
2032 reject_statement ();
2036 while (st != ST_END_FORALL);
2042 static gfc_statement parse_executable (gfc_statement);
2044 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2047 parse_if_block (void)
2056 accept_statement (ST_IF_BLOCK);
2058 top = gfc_state_stack->tail;
2059 push_state (&s, COMP_IF, gfc_new_block);
2061 new_st.op = EXEC_IF;
2062 d = add_statement ();
2064 d->expr = top->expr;
2070 st = parse_executable (ST_NONE);
2080 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2081 "statement at %L", &else_locus);
2083 reject_statement ();
2087 d = new_level (gfc_state_stack->head);
2089 d->expr = new_st.expr;
2091 accept_statement (st);
2098 gfc_error ("Duplicate ELSE statements at %L and %C",
2100 reject_statement ();
2105 else_locus = gfc_current_locus;
2107 d = new_level (gfc_state_stack->head);
2110 accept_statement (st);
2118 unexpected_statement (st);
2122 while (st != ST_ENDIF);
2125 accept_statement (st);
2129 /* Parse a SELECT block. */
2132 parse_select_block (void)
2138 accept_statement (ST_SELECT_CASE);
2140 cp = gfc_state_stack->tail;
2141 push_state (&s, COMP_SELECT, gfc_new_block);
2143 /* Make sure that the next statement is a CASE or END SELECT. */
2146 st = next_statement ();
2149 if (st == ST_END_SELECT)
2151 /* Empty SELECT CASE is OK. */
2152 accept_statement (st);
2159 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2162 reject_statement ();
2165 /* At this point, we're got a nonempty select block. */
2166 cp = new_level (cp);
2169 accept_statement (st);
2173 st = parse_executable (ST_NONE);
2180 cp = new_level (gfc_state_stack->head);
2182 gfc_clear_new_st ();
2184 accept_statement (st);
2190 /* Can't have an executable statement because of
2191 parse_executable(). */
2193 unexpected_statement (st);
2197 while (st != ST_END_SELECT);
2200 accept_statement (st);
2204 /* Given a symbol, make sure it is not an iteration variable for a DO
2205 statement. This subroutine is called when the symbol is seen in a
2206 context that causes it to become redefined. If the symbol is an
2207 iterator, we generate an error message and return nonzero. */
2210 gfc_check_do_variable (gfc_symtree *st)
2214 for (s=gfc_state_stack; s; s = s->previous)
2215 if (s->do_variable == st)
2217 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2218 "loop beginning at %L", st->name, &s->head->loc);
2226 /* Checks to see if the current statement label closes an enddo.
2227 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2228 an error) if it incorrectly closes an ENDDO. */
2231 check_do_closure (void)
2235 if (gfc_statement_label == NULL)
2238 for (p = gfc_state_stack; p; p = p->previous)
2239 if (p->state == COMP_DO)
2243 return 0; /* No loops to close */
2245 if (p->ext.end_do_label == gfc_statement_label)
2248 if (p == gfc_state_stack)
2251 gfc_error ("End of nonblock DO statement at %C is within another block");
2255 /* At this point, the label doesn't terminate the innermost loop.
2256 Make sure it doesn't terminate another one. */
2257 for (; p; p = p->previous)
2258 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
2260 gfc_error ("End of nonblock DO statement at %C is interwoven "
2261 "with another DO loop");
2269 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2270 handled inside of parse_executable(), because they aren't really
2274 parse_do_block (void)
2281 s.ext.end_do_label = new_st.label;
2283 if (new_st.ext.iterator != NULL)
2284 stree = new_st.ext.iterator->var->symtree;
2288 accept_statement (ST_DO);
2290 top = gfc_state_stack->tail;
2291 push_state (&s, COMP_DO, gfc_new_block);
2293 s.do_variable = stree;
2295 top->block = new_level (top);
2296 top->block->op = EXEC_DO;
2299 st = parse_executable (ST_NONE);
2307 if (s.ext.end_do_label != NULL
2308 && s.ext.end_do_label != gfc_statement_label)
2309 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
2312 if (gfc_statement_label != NULL)
2314 new_st.op = EXEC_NOP;
2319 case ST_IMPLIED_ENDDO:
2320 /* If the do-stmt of this DO construct has a do-construct-name,
2321 the corresponding end-do must be an end-do-stmt (with a matching
2322 name, but in that case we must have seen ST_ENDDO first).
2323 We only complain about this in pedantic mode. */
2324 if (gfc_current_block () != NULL)
2325 gfc_error_now ("named block DO at %L requires matching ENDDO name",
2326 &gfc_current_block()->declared_at);
2331 unexpected_statement (st);
2336 accept_statement (st);
2340 /* Parse the statements of OpenMP do/parallel do. */
2342 static gfc_statement
2343 parse_omp_do (gfc_statement omp_st)
2349 accept_statement (omp_st);
2351 cp = gfc_state_stack->tail;
2352 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2353 np = new_level (cp);
2359 st = next_statement ();
2362 else if (st == ST_DO)
2365 unexpected_statement (st);
2369 if (gfc_statement_label != NULL
2370 && gfc_state_stack->previous != NULL
2371 && gfc_state_stack->previous->state == COMP_DO
2372 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
2380 there should be no !$OMP END DO. */
2382 return ST_IMPLIED_ENDDO;
2385 check_do_closure ();
2388 st = next_statement ();
2389 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
2391 if (new_st.op == EXEC_OMP_END_NOWAIT)
2392 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2394 gcc_assert (new_st.op == EXEC_NOP);
2395 gfc_clear_new_st ();
2396 gfc_commit_symbols ();
2397 gfc_warning_check ();
2398 st = next_statement ();
2404 /* Parse the statements of OpenMP atomic directive. */
2407 parse_omp_atomic (void)
2413 accept_statement (ST_OMP_ATOMIC);
2415 cp = gfc_state_stack->tail;
2416 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2417 np = new_level (cp);
2423 st = next_statement ();
2426 else if (st == ST_ASSIGNMENT)
2429 unexpected_statement (st);
2432 accept_statement (st);
2438 /* Parse the statements of an OpenMP structured block. */
2441 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
2443 gfc_statement st, omp_end_st;
2447 accept_statement (omp_st);
2449 cp = gfc_state_stack->tail;
2450 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2451 np = new_level (cp);
2457 case ST_OMP_PARALLEL:
2458 omp_end_st = ST_OMP_END_PARALLEL;
2460 case ST_OMP_PARALLEL_SECTIONS:
2461 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
2463 case ST_OMP_SECTIONS:
2464 omp_end_st = ST_OMP_END_SECTIONS;
2466 case ST_OMP_ORDERED:
2467 omp_end_st = ST_OMP_END_ORDERED;
2469 case ST_OMP_CRITICAL:
2470 omp_end_st = ST_OMP_END_CRITICAL;
2473 omp_end_st = ST_OMP_END_MASTER;
2476 omp_end_st = ST_OMP_END_SINGLE;
2478 case ST_OMP_WORKSHARE:
2479 omp_end_st = ST_OMP_END_WORKSHARE;
2481 case ST_OMP_PARALLEL_WORKSHARE:
2482 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
2490 if (workshare_stmts_only)
2492 /* Inside of !$omp workshare, only
2495 where statements and constructs
2496 forall statements and constructs
2500 are allowed. For !$omp critical these
2501 restrictions apply recursively. */
2504 st = next_statement ();
2515 accept_statement (st);
2518 case ST_WHERE_BLOCK:
2519 parse_where_block ();
2522 case ST_FORALL_BLOCK:
2523 parse_forall_block ();
2526 case ST_OMP_PARALLEL:
2527 case ST_OMP_PARALLEL_SECTIONS:
2528 parse_omp_structured_block (st, false);
2531 case ST_OMP_PARALLEL_WORKSHARE:
2532 case ST_OMP_CRITICAL:
2533 parse_omp_structured_block (st, true);
2536 case ST_OMP_PARALLEL_DO:
2537 st = parse_omp_do (st);
2541 parse_omp_atomic ();
2552 st = next_statement ();
2556 st = parse_executable (ST_NONE);
2559 else if (st == ST_OMP_SECTION
2560 && (omp_st == ST_OMP_SECTIONS
2561 || omp_st == ST_OMP_PARALLEL_SECTIONS))
2563 np = new_level (np);
2567 else if (st != omp_end_st)
2568 unexpected_statement (st);
2570 while (st != omp_end_st);
2574 case EXEC_OMP_END_NOWAIT:
2575 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2577 case EXEC_OMP_CRITICAL:
2578 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
2579 || (new_st.ext.omp_name != NULL
2580 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
2581 gfc_error ("Name after !$omp critical and !$omp end critical does "
2583 gfc_free ((char *) new_st.ext.omp_name);
2585 case EXEC_OMP_END_SINGLE:
2586 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
2587 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
2588 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
2589 gfc_free_omp_clauses (new_st.ext.omp_clauses);
2597 gfc_clear_new_st ();
2598 gfc_commit_symbols ();
2599 gfc_warning_check ();
2604 /* Accept a series of executable statements. We return the first
2605 statement that doesn't fit to the caller. Any block statements are
2606 passed on to the correct handler, which usually passes the buck
2609 static gfc_statement
2610 parse_executable (gfc_statement st)
2615 st = next_statement ();
2619 close_flag = check_do_closure ();
2624 case ST_END_PROGRAM:
2627 case ST_END_FUNCTION:
2631 case ST_END_SUBROUTINE:
2636 case ST_SELECT_CASE:
2637 gfc_error ("%s statement at %C cannot terminate a non-block "
2638 "DO loop", gfc_ascii_statement (st));
2654 accept_statement (st);
2655 if (close_flag == 1)
2656 return ST_IMPLIED_ENDDO;
2663 case ST_SELECT_CASE:
2664 parse_select_block ();
2669 if (check_do_closure () == 1)
2670 return ST_IMPLIED_ENDDO;
2673 case ST_WHERE_BLOCK:
2674 parse_where_block ();
2677 case ST_FORALL_BLOCK:
2678 parse_forall_block ();
2681 case ST_OMP_PARALLEL:
2682 case ST_OMP_PARALLEL_SECTIONS:
2683 case ST_OMP_SECTIONS:
2684 case ST_OMP_ORDERED:
2685 case ST_OMP_CRITICAL:
2688 parse_omp_structured_block (st, false);
2691 case ST_OMP_WORKSHARE:
2692 case ST_OMP_PARALLEL_WORKSHARE:
2693 parse_omp_structured_block (st, true);
2697 case ST_OMP_PARALLEL_DO:
2698 st = parse_omp_do (st);
2699 if (st == ST_IMPLIED_ENDDO)
2704 parse_omp_atomic ();
2711 st = next_statement ();
2716 /* Parse a series of contained program units. */
2718 static void parse_progunit (gfc_statement);
2721 /* Fix the symbols for sibling functions. These are incorrectly added to
2722 the child namespace as the parser didn't know about this procedure. */
2725 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
2729 gfc_symbol *old_sym;
2731 sym->attr.referenced = 1;
2732 for (ns = siblings; ns; ns = ns->sibling)
2734 gfc_find_sym_tree (sym->name, ns, 0, &st);
2736 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
2739 old_sym = st->n.sym;
2740 if ((old_sym->attr.flavor == FL_PROCEDURE
2741 || old_sym->ts.type == BT_UNKNOWN)
2742 && old_sym->ns == ns
2743 && !old_sym->attr.contained)
2745 /* Replace it with the symbol from the parent namespace. */
2749 /* Free the old (local) symbol. */
2751 if (old_sym->refs == 0)
2752 gfc_free_symbol (old_sym);
2755 /* Do the same for any contained procedures. */
2756 gfc_fixup_sibling_symbols (sym, ns->contained);
2761 parse_contained (int module)
2763 gfc_namespace *ns, *parent_ns;
2764 gfc_state_data s1, s2;
2768 int contains_statements = 0;
2770 push_state (&s1, COMP_CONTAINS, NULL);
2771 parent_ns = gfc_current_ns;
2775 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2777 gfc_current_ns->sibling = parent_ns->contained;
2778 parent_ns->contained = gfc_current_ns;
2780 st = next_statement ();
2789 contains_statements = 1;
2790 accept_statement (st);
2793 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2796 /* For internal procedures, create/update the symbol in the
2797 parent namespace. */
2801 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2802 gfc_error ("Contained procedure '%s' at %C is already "
2803 "ambiguous", gfc_new_block->name);
2806 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2807 &gfc_new_block->declared_at) ==
2810 if (st == ST_FUNCTION)
2811 gfc_add_function (&sym->attr, sym->name,
2812 &gfc_new_block->declared_at);
2814 gfc_add_subroutine (&sym->attr, sym->name,
2815 &gfc_new_block->declared_at);
2819 gfc_commit_symbols ();
2822 sym = gfc_new_block;
2824 /* Mark this as a contained function, so it isn't replaced
2825 by other module functions. */
2826 sym->attr.contained = 1;
2827 sym->attr.referenced = 1;
2829 parse_progunit (ST_NONE);
2831 /* Fix up any sibling functions that refer to this one. */
2832 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2833 /* Or refer to any of its alternate entry points. */
2834 for (el = gfc_current_ns->entries; el; el = el->next)
2835 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2837 gfc_current_ns->code = s2.head;
2838 gfc_current_ns = parent_ns;
2843 /* These statements are associated with the end of the host unit. */
2844 case ST_END_FUNCTION:
2846 case ST_END_PROGRAM:
2847 case ST_END_SUBROUTINE:
2848 accept_statement (st);
2852 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2853 gfc_ascii_statement (st));
2854 reject_statement ();
2858 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2859 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2861 /* The first namespace in the list is guaranteed to not have
2862 anything (worthwhile) in it. */
2864 gfc_current_ns = parent_ns;
2866 ns = gfc_current_ns->contained;
2867 gfc_current_ns->contained = ns->sibling;
2868 gfc_free_namespace (ns);
2871 if (!contains_statements)
2872 /* This is valid in Fortran 2008. */
2873 gfc_notify_std (GFC_STD_GNU, "Extension: CONTAINS statement without "
2874 "FUNCTION or SUBROUTINE statement at %C");
2878 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2881 parse_progunit (gfc_statement st)
2886 st = parse_spec (st);
2896 accept_statement (st);
2906 st = parse_executable (st);
2917 accept_statement (st);
2924 unexpected_statement (st);
2925 reject_statement ();
2926 st = next_statement ();
2932 for (p = gfc_state_stack; p; p = p->previous)
2933 if (p->state == COMP_CONTAINS)
2936 if (gfc_find_state (COMP_MODULE) == SUCCESS)
2941 gfc_error ("CONTAINS statement at %C is already in a contained "
2943 st = next_statement ();
2947 parse_contained (0);
2950 gfc_current_ns->code = gfc_state_stack->head;
2954 /* Come here to complain about a global symbol already in use as
2958 global_used (gfc_gsymbol *sym, locus *where)
2963 where = &gfc_current_locus;
2973 case GSYM_SUBROUTINE:
2974 name = "SUBROUTINE";
2979 case GSYM_BLOCK_DATA:
2980 name = "BLOCK DATA";
2986 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
2990 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
2991 sym->name, where, name, &sym->where);
2995 /* Parse a block data program unit. */
2998 parse_block_data (void)
3001 static locus blank_locus;
3002 static int blank_block=0;
3005 gfc_current_ns->proc_name = gfc_new_block;
3006 gfc_current_ns->is_block_data = 1;
3008 if (gfc_new_block == NULL)
3011 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3012 "prior BLOCK DATA at %L", &blank_locus);
3016 blank_locus = gfc_current_locus;
3021 s = gfc_get_gsymbol (gfc_new_block->name);
3023 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
3024 global_used(s, NULL);
3027 s->type = GSYM_BLOCK_DATA;
3028 s->where = gfc_current_locus;
3033 st = parse_spec (ST_NONE);
3035 while (st != ST_END_BLOCK_DATA)
3037 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3038 gfc_ascii_statement (st));
3039 reject_statement ();
3040 st = next_statement ();
3045 /* Parse a module subprogram. */
3053 s = gfc_get_gsymbol (gfc_new_block->name);
3054 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
3055 global_used(s, NULL);
3058 s->type = GSYM_MODULE;
3059 s->where = gfc_current_locus;
3063 st = parse_spec (ST_NONE);
3072 parse_contained (1);
3076 accept_statement (st);
3080 gfc_error ("Unexpected %s statement in MODULE at %C",
3081 gfc_ascii_statement (st));
3083 reject_statement ();
3084 st = next_statement ();
3090 /* Add a procedure name to the global symbol table. */
3093 add_global_procedure (int sub)
3097 s = gfc_get_gsymbol(gfc_new_block->name);
3100 || (s->type != GSYM_UNKNOWN
3101 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3102 global_used(s, NULL);
3105 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3106 s->where = gfc_current_locus;
3112 /* Add a program to the global symbol table. */
3115 add_global_program (void)
3119 if (gfc_new_block == NULL)
3121 s = gfc_get_gsymbol (gfc_new_block->name);
3123 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
3124 global_used(s, NULL);
3127 s->type = GSYM_PROGRAM;
3128 s->where = gfc_current_locus;
3134 /* Top level parser. */
3137 gfc_parse_file (void)
3139 int seen_program, errors_before, errors;
3140 gfc_state_data top, s;
3144 top.state = COMP_NONE;
3146 top.previous = NULL;
3147 top.head = top.tail = NULL;
3148 top.do_variable = NULL;
3150 gfc_state_stack = ⊤
3152 gfc_clear_new_st ();
3154 gfc_statement_label = NULL;
3156 if (setjmp (eof_buf))
3157 return FAILURE; /* Come here on unexpected EOF */
3161 /* Exit early for empty files. */
3167 st = next_statement ();
3176 goto duplicate_main;
3178 prog_locus = gfc_current_locus;
3180 push_state (&s, COMP_PROGRAM, gfc_new_block);
3181 main_program_symbol(gfc_current_ns);
3182 accept_statement (st);
3183 add_global_program ();
3184 parse_progunit (ST_NONE);
3188 add_global_procedure (1);
3189 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
3190 accept_statement (st);
3191 parse_progunit (ST_NONE);
3195 add_global_procedure (0);
3196 push_state (&s, COMP_FUNCTION, gfc_new_block);
3197 accept_statement (st);
3198 parse_progunit (ST_NONE);
3202 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
3203 accept_statement (st);
3204 parse_block_data ();
3208 push_state (&s, COMP_MODULE, gfc_new_block);
3209 accept_statement (st);
3211 gfc_get_errors (NULL, &errors_before);
3215 /* Anything else starts a nameless main program block. */
3218 goto duplicate_main;
3220 prog_locus = gfc_current_locus;
3222 push_state (&s, COMP_PROGRAM, gfc_new_block);
3223 main_program_symbol (gfc_current_ns);
3224 parse_progunit (st);
3228 gfc_current_ns->code = s.head;
3230 gfc_resolve (gfc_current_ns);
3232 /* Dump the parse tree if requested. */
3233 if (gfc_option.verbose)
3234 gfc_show_namespace (gfc_current_ns);
3236 gfc_get_errors (NULL, &errors);
3237 if (s.state == COMP_MODULE)
3239 gfc_dump_module (s.sym->name, errors_before == errors);
3241 gfc_generate_module_code (gfc_current_ns);
3246 gfc_generate_code (gfc_current_ns);
3257 /* If we see a duplicate main program, shut down. If the second
3258 instance is an implied main program, ie data decls or executable
3259 statements, we're in for lots of errors. */
3260 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
3261 reject_statement ();