2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation,
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
31 /* Current statement label. Zero means no statement label. Because
32 new_st can get wiped during statement matching, we have to keep it
35 gfc_st_label *gfc_statement_label;
37 static locus label_locus;
38 static jmp_buf eof_buf;
40 gfc_state_data *gfc_state_stack;
42 /* TODO: Re-order functions to kill these forward decls. */
43 static void check_statement_label (gfc_statement);
44 static void undo_new_statement (void);
45 static void reject_statement (void);
47 /* A sort of half-matching function. We try to match the word on the
48 input with the passed string. If this succeeds, we call the
49 keyword-dependent matching function that will match the rest of the
50 statement. For single keywords, the matching subroutine is
54 match_word (const char *str, match (*subr) (void), locus * old_locus)
69 gfc_current_locus = *old_locus;
77 /* Figure out what the next statement is, (mostly) regardless of
78 proper ordering. The do...while(0) is there to prevent if/else
81 #define match(keyword, subr, st) \
83 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
86 undo_new_statement (); \
90 decode_statement (void)
101 gfc_clear_error (); /* Clear any pending errors. */
102 gfc_clear_warning (); /* Clear any pending warnings. */
104 if (gfc_match_eos () == MATCH_YES)
107 old_locus = gfc_current_locus;
109 /* Try matching a data declaration or function declaration. The
110 input "REALFUNCTIONA(N)" can mean several things in different
111 contexts, so it (and its relatives) get special treatment. */
113 if (gfc_current_state () == COMP_NONE
114 || gfc_current_state () == COMP_INTERFACE
115 || gfc_current_state () == COMP_CONTAINS)
117 m = gfc_match_function_decl ();
120 else if (m == MATCH_ERROR)
124 gfc_current_locus = old_locus;
127 /* Match statements whose error messages are meant to be overwritten
128 by something better. */
130 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
131 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
132 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
134 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
135 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
137 /* Try to match a subroutine statement, which has the same optional
138 prefixes that functions can have. */
140 if (gfc_match_subroutine () == MATCH_YES)
141 return ST_SUBROUTINE;
143 gfc_current_locus = old_locus;
145 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
146 might begin with a block label. The match functions for these
147 statements are unusual in that their keyword is not seen before
148 the matcher is called. */
150 if (gfc_match_if (&st) == MATCH_YES)
153 gfc_current_locus = old_locus;
155 if (gfc_match_where (&st) == MATCH_YES)
158 gfc_current_locus = old_locus;
160 if (gfc_match_forall (&st) == MATCH_YES)
163 gfc_current_locus = old_locus;
165 match (NULL, gfc_match_do, ST_DO);
166 match (NULL, gfc_match_select, ST_SELECT_CASE);
168 /* General statement matching: Instead of testing every possible
169 statement, we eliminate most possibilities by peeking at the
172 c = gfc_peek_char ();
177 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
178 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
179 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
183 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
184 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
188 match ("call", gfc_match_call, ST_CALL);
189 match ("close", gfc_match_close, ST_CLOSE);
190 match ("continue", gfc_match_continue, ST_CONTINUE);
191 match ("cycle", gfc_match_cycle, ST_CYCLE);
192 match ("case", gfc_match_case, ST_CASE);
193 match ("common", gfc_match_common, ST_COMMON);
194 match ("contains", gfc_match_eos, ST_CONTAINS);
198 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
199 match ("data", gfc_match_data, ST_DATA);
200 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
204 match ("end file", gfc_match_endfile, ST_END_FILE);
205 match ("exit", gfc_match_exit, ST_EXIT);
206 match ("else", gfc_match_else, ST_ELSE);
207 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
208 match ("else if", gfc_match_elseif, ST_ELSEIF);
209 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
211 if (gfc_match_end (&st) == MATCH_YES)
214 match ("entry% ", gfc_match_entry, ST_ENTRY);
215 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
216 match ("external", gfc_match_external, ST_ATTR_DECL);
220 match ("flush", gfc_match_flush, ST_FLUSH);
221 match ("format", gfc_match_format, ST_FORMAT);
225 match ("go to", gfc_match_goto, ST_GOTO);
229 match ("inquire", gfc_match_inquire, ST_INQUIRE);
230 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
231 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
232 match ("import", gfc_match_import, ST_IMPORT);
233 match ("interface", gfc_match_interface, ST_INTERFACE);
234 match ("intent", gfc_match_intent, ST_ATTR_DECL);
235 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
239 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
240 match ("module", gfc_match_module, ST_MODULE);
244 match ("nullify", gfc_match_nullify, ST_NULLIFY);
245 match ("namelist", gfc_match_namelist, ST_NAMELIST);
249 match ("open", gfc_match_open, ST_OPEN);
250 match ("optional", gfc_match_optional, ST_ATTR_DECL);
254 match ("print", gfc_match_print, ST_WRITE);
255 match ("parameter", gfc_match_parameter, ST_PARAMETER);
256 match ("pause", gfc_match_pause, ST_PAUSE);
257 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
258 if (gfc_match_private (&st) == MATCH_YES)
260 match ("program", gfc_match_program, ST_PROGRAM);
261 if (gfc_match_public (&st) == MATCH_YES)
263 match ("protected", gfc_match_protected, ST_ATTR_DECL);
267 match ("read", gfc_match_read, ST_READ);
268 match ("return", gfc_match_return, ST_RETURN);
269 match ("rewind", gfc_match_rewind, ST_REWIND);
273 match ("sequence", gfc_match_eos, ST_SEQUENCE);
274 match ("stop", gfc_match_stop, ST_STOP);
275 match ("save", gfc_match_save, ST_ATTR_DECL);
279 match ("target", gfc_match_target, ST_ATTR_DECL);
280 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
284 match ("use", gfc_match_use, ST_USE);
288 match ("value", gfc_match_value, ST_ATTR_DECL);
289 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
293 match ("write", gfc_match_write, ST_WRITE);
297 /* All else has failed, so give up. See if any of the matchers has
298 stored an error message of some sort. */
300 if (gfc_error_check () == 0)
301 gfc_error_now ("Unclassifiable statement at %C");
305 gfc_error_recovery ();
311 decode_omp_directive (void)
320 gfc_clear_error (); /* Clear any pending errors. */
321 gfc_clear_warning (); /* Clear any pending warnings. */
325 gfc_error_now ("OpenMP directives at %C may not appear in PURE or ELEMENTAL procedures");
326 gfc_error_recovery ();
330 old_locus = gfc_current_locus;
332 /* General OpenMP directive matching: Instead of testing every possible
333 statement, we eliminate most possibilities by peeking at the
336 c = gfc_peek_char ();
341 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
344 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
347 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
350 match ("do", gfc_match_omp_do, ST_OMP_DO);
353 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
354 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
355 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
356 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
357 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
358 match ("end parallel sections", gfc_match_omp_eos,
359 ST_OMP_END_PARALLEL_SECTIONS);
360 match ("end parallel workshare", gfc_match_omp_eos,
361 ST_OMP_END_PARALLEL_WORKSHARE);
362 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
363 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
364 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
365 match ("end workshare", gfc_match_omp_end_nowait,
366 ST_OMP_END_WORKSHARE);
369 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
372 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
375 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
378 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
379 match ("parallel sections", gfc_match_omp_parallel_sections,
380 ST_OMP_PARALLEL_SECTIONS);
381 match ("parallel workshare", gfc_match_omp_parallel_workshare,
382 ST_OMP_PARALLEL_WORKSHARE);
383 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
386 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
387 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
388 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
391 match ("threadprivate", gfc_match_omp_threadprivate,
392 ST_OMP_THREADPRIVATE);
394 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
398 /* All else has failed, so give up. See if any of the matchers has
399 stored an error message of some sort. */
401 if (gfc_error_check () == 0)
402 gfc_error_now ("Unclassifiable OpenMP directive at %C");
406 gfc_error_recovery ();
414 /* Get the next statement in free form source. */
420 int c, d, cnt, at_bol;
422 at_bol = gfc_at_bol ();
423 gfc_gobble_whitespace ();
425 c = gfc_peek_char ();
429 /* Found a statement label? */
430 m = gfc_match_st_label (&gfc_statement_label);
432 d = gfc_peek_char ();
433 if (m != MATCH_YES || !gfc_is_whitespace (d))
435 gfc_match_small_literal_int (&c, &cnt);
438 gfc_error_now ("Too many digits in statement label at %C");
441 gfc_error_now ("Zero is not a valid statement label at %C");
444 c = gfc_next_char ();
447 if (!gfc_is_whitespace (c))
448 gfc_error_now ("Non-numeric character in statement label at %C");
454 label_locus = gfc_current_locus;
456 gfc_gobble_whitespace ();
458 if (at_bol && gfc_peek_char () == ';')
461 ("Semicolon at %C needs to be preceded by statement");
462 gfc_next_char (); /* Eat up the semicolon. */
466 if (gfc_match_eos () == MATCH_YES)
469 ("Ignoring statement label in empty statement at %C");
470 gfc_free_st_label (gfc_statement_label);
471 gfc_statement_label = NULL;
478 /* Comments have already been skipped by the time we get here,
479 except for OpenMP directives. */
480 if (gfc_option.flag_openmp)
484 c = gfc_next_char ();
485 for (i = 0; i < 5; i++, c = gfc_next_char ())
486 gcc_assert (c == "!$omp"[i]);
488 gcc_assert (c == ' ');
489 return decode_omp_directive ();
493 if (at_bol && c == ';')
495 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
496 gfc_next_char (); /* Eat up the semicolon. */
500 return decode_statement ();
504 /* Get the next statement in fixed-form source. */
509 int label, digit_flag, i;
514 return decode_statement ();
516 /* Skip past the current label field, parsing a statement label if
517 one is there. This is a weird number parser, since the number is
518 contained within five columns and can have any kind of embedded
519 spaces. We also check for characters that make the rest of the
525 for (i = 0; i < 5; i++)
527 c = gfc_next_char_literal (0);
544 label = label * 10 + c - '0';
545 label_locus = gfc_current_locus;
549 /* Comments have already been skipped by the time we get
550 here, except for OpenMP directives. */
552 if (gfc_option.flag_openmp)
554 for (i = 0; i < 5; i++, c = gfc_next_char_literal (0))
555 gcc_assert (TOLOWER (c) == "*$omp"[i]);
557 if (c != ' ' && c != '0')
559 gfc_buffer_error (0);
560 gfc_error ("Bad continuation line at %C");
564 return decode_omp_directive ();
568 /* Comments have already been skipped by the time we get
569 here so don't bother checking for them. */
572 gfc_buffer_error (0);
573 gfc_error ("Non-numeric character in statement label at %C");
581 gfc_warning_now ("Zero is not a valid statement label at %C");
584 /* We've found a valid statement label. */
585 gfc_statement_label = gfc_get_st_label (label);
589 /* Since this line starts a statement, it cannot be a continuation
590 of a previous statement. If we see something here besides a
591 space or zero, it must be a bad continuation line. */
593 c = gfc_next_char_literal (0);
597 if (c != ' ' && c != '0')
599 gfc_buffer_error (0);
600 gfc_error ("Bad continuation line at %C");
604 /* Now that we've taken care of the statement label columns, we have
605 to make sure that the first nonblank character is not a '!'. If
606 it is, the rest of the line is a comment. */
610 loc = gfc_current_locus;
611 c = gfc_next_char_literal (0);
613 while (gfc_is_whitespace (c));
617 gfc_current_locus = loc;
621 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
625 if (gfc_match_eos () == MATCH_YES)
628 /* At this point, we've got a nonblank statement to parse. */
629 return decode_statement ();
633 gfc_warning ("Ignoring statement label in empty statement at %C");
639 /* Return the next non-ST_NONE statement to the caller. We also worry
640 about including files and the ends of include files at this stage. */
643 next_statement (void)
647 gfc_new_block = NULL;
651 gfc_statement_label = NULL;
652 gfc_buffer_error (1);
656 if (gfc_option.warn_line_truncation
657 && gfc_current_locus.lb
658 && gfc_current_locus.lb->truncated)
659 gfc_warning_now ("Line truncated at %C");
664 gfc_skip_comments ();
673 (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
679 gfc_buffer_error (0);
682 check_statement_label (st);
688 /****************************** Parser ***********************************/
690 /* The parser subroutines are of type 'try' that fail if the file ends
693 /* Macros that expand to case-labels for various classes of
694 statements. Start with executable statements that directly do
697 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
698 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
699 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
700 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
701 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
702 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
703 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
704 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
707 /* Statements that mark other executable statements. */
709 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
710 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
711 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
712 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
713 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
714 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
716 /* Declaration statements */
718 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
719 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
720 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE
722 /* Block end statements. Errors associated with interchanging these
723 are detected in gfc_match_end(). */
725 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
726 case ST_END_PROGRAM: case ST_END_SUBROUTINE
729 /* Push a new state onto the stack. */
732 push_state (gfc_state_data * p, gfc_compile_state new_state, gfc_symbol * sym)
735 p->state = new_state;
736 p->previous = gfc_state_stack;
738 p->head = p->tail = NULL;
739 p->do_variable = NULL;
745 /* Pop the current state. */
751 gfc_state_stack = gfc_state_stack->previous;
755 /* Try to find the given state in the state stack. */
758 gfc_find_state (gfc_compile_state state)
762 for (p = gfc_state_stack; p; p = p->previous)
763 if (p->state == state)
766 return (p == NULL) ? FAILURE : SUCCESS;
770 /* Starts a new level in the statement list. */
773 new_level (gfc_code * q)
777 p = q->block = gfc_get_code ();
779 gfc_state_stack->head = gfc_state_stack->tail = p;
785 /* Add the current new_st code structure and adds it to the current
786 program unit. As a side-effect, it zeroes the new_st. */
796 p->loc = gfc_current_locus;
798 if (gfc_state_stack->head == NULL)
799 gfc_state_stack->head = p;
801 gfc_state_stack->tail->next = p;
803 while (p->next != NULL)
806 gfc_state_stack->tail = p;
814 /* Frees everything associated with the current statement. */
817 undo_new_statement (void)
819 gfc_free_statements (new_st.block);
820 gfc_free_statements (new_st.next);
821 gfc_free_statement (&new_st);
826 /* If the current statement has a statement label, make sure that it
827 is allowed to, or should have one. */
830 check_statement_label (gfc_statement st)
834 if (gfc_statement_label == NULL)
837 gfc_error ("FORMAT statement at %L does not have a statement label",
845 case ST_END_FUNCTION:
846 case ST_END_SUBROUTINE:
852 type = ST_LABEL_TARGET;
856 type = ST_LABEL_FORMAT;
859 /* Statement labels are not restricted from appearing on a
860 particular line. However, there are plenty of situations
861 where the resulting label can't be referenced. */
864 type = ST_LABEL_BAD_TARGET;
868 gfc_define_st_label (gfc_statement_label, type, &label_locus);
870 new_st.here = gfc_statement_label;
874 /* Figures out what the enclosing program unit is. This will be a
875 function, subroutine, program, block data or module. */
878 gfc_enclosing_unit (gfc_compile_state * result)
882 for (p = gfc_state_stack; p; p = p->previous)
883 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
884 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
885 || p->state == COMP_PROGRAM)
894 *result = COMP_PROGRAM;
899 /* Translate a statement enum to a string. */
902 gfc_ascii_statement (gfc_statement st)
908 case ST_ARITHMETIC_IF:
909 p = _("arithmetic IF");
915 p = _("attribute declaration");
945 p = _("data declaration");
953 case ST_DERIVED_DECL:
954 p = _("derived type declaration");
968 case ST_END_BLOCK_DATA:
969 p = "END BLOCK DATA";
980 case ST_END_FUNCTION:
986 case ST_END_INTERFACE:
998 case ST_END_SUBROUTINE:
999 p = "END SUBROUTINE";
1010 case ST_EQUIVALENCE:
1019 case ST_FORALL_BLOCK: /* Fall through */
1038 case ST_IMPLICIT_NONE:
1039 p = "IMPLICIT NONE";
1041 case ST_IMPLIED_ENDDO:
1042 p = _("implied END DO");
1068 case ST_MODULE_PROC:
1069 p = "MODULE PROCEDURE";
1104 case ST_WHERE_BLOCK: /* Fall through */
1112 p = _("assignment");
1114 case ST_POINTER_ASSIGNMENT:
1115 p = _("pointer assignment");
1117 case ST_SELECT_CASE:
1126 case ST_STATEMENT_FUNCTION:
1127 p = "STATEMENT FUNCTION";
1129 case ST_LABEL_ASSIGNMENT:
1130 p = "LABEL ASSIGNMENT";
1133 p = "ENUM DEFINITION";
1136 p = "ENUMERATOR DEFINITION";
1144 case ST_OMP_BARRIER:
1145 p = "!$OMP BARRIER";
1147 case ST_OMP_CRITICAL:
1148 p = "!$OMP CRITICAL";
1153 case ST_OMP_END_CRITICAL:
1154 p = "!$OMP END CRITICAL";
1159 case ST_OMP_END_MASTER:
1160 p = "!$OMP END MASTER";
1162 case ST_OMP_END_ORDERED:
1163 p = "!$OMP END ORDERED";
1165 case ST_OMP_END_PARALLEL:
1166 p = "!$OMP END PARALLEL";
1168 case ST_OMP_END_PARALLEL_DO:
1169 p = "!$OMP END PARALLEL DO";
1171 case ST_OMP_END_PARALLEL_SECTIONS:
1172 p = "!$OMP END PARALLEL SECTIONS";
1174 case ST_OMP_END_PARALLEL_WORKSHARE:
1175 p = "!$OMP END PARALLEL WORKSHARE";
1177 case ST_OMP_END_SECTIONS:
1178 p = "!$OMP END SECTIONS";
1180 case ST_OMP_END_SINGLE:
1181 p = "!$OMP END SINGLE";
1183 case ST_OMP_END_WORKSHARE:
1184 p = "!$OMP END WORKSHARE";
1192 case ST_OMP_ORDERED:
1193 p = "!$OMP ORDERED";
1195 case ST_OMP_PARALLEL:
1196 p = "!$OMP PARALLEL";
1198 case ST_OMP_PARALLEL_DO:
1199 p = "!$OMP PARALLEL DO";
1201 case ST_OMP_PARALLEL_SECTIONS:
1202 p = "!$OMP PARALLEL SECTIONS";
1204 case ST_OMP_PARALLEL_WORKSHARE:
1205 p = "!$OMP PARALLEL WORKSHARE";
1207 case ST_OMP_SECTIONS:
1208 p = "!$OMP SECTIONS";
1210 case ST_OMP_SECTION:
1211 p = "!$OMP SECTION";
1216 case ST_OMP_THREADPRIVATE:
1217 p = "!$OMP THREADPRIVATE";
1219 case ST_OMP_WORKSHARE:
1220 p = "!$OMP WORKSHARE";
1223 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1230 /* Create a symbol for the main program and assign it to ns->proc_name. */
1233 main_program_symbol (gfc_namespace * ns)
1235 gfc_symbol *main_program;
1236 symbol_attribute attr;
1238 gfc_get_symbol ("MAIN__", ns, &main_program);
1239 gfc_clear_attr (&attr);
1240 attr.flavor = FL_PROCEDURE;
1241 attr.proc = PROC_UNKNOWN;
1242 attr.subroutine = 1;
1243 attr.access = ACCESS_PUBLIC;
1244 attr.is_main_program = 1;
1245 main_program->attr = attr;
1246 main_program->declared_at = gfc_current_locus;
1247 ns->proc_name = main_program;
1248 gfc_commit_symbols ();
1252 /* Do whatever is necessary to accept the last statement. */
1255 accept_statement (gfc_statement st)
1264 case ST_IMPLICIT_NONE:
1265 gfc_set_implicit_none ();
1274 gfc_current_ns->proc_name = gfc_new_block;
1277 /* If the statement is the end of a block, lay down a special code
1278 that allows a branch to the end of the block from within the
1283 if (gfc_statement_label != NULL)
1285 new_st.op = EXEC_NOP;
1291 /* The end-of-program unit statements do not get the special
1292 marker and require a statement of some sort if they are a
1295 case ST_END_PROGRAM:
1296 case ST_END_FUNCTION:
1297 case ST_END_SUBROUTINE:
1298 if (gfc_statement_label != NULL)
1300 new_st.op = EXEC_RETURN;
1316 gfc_commit_symbols ();
1317 gfc_warning_check ();
1318 gfc_clear_new_st ();
1322 /* Undo anything tentative that has been built for the current
1326 reject_statement (void)
1328 gfc_new_block = NULL;
1329 gfc_undo_symbols ();
1330 gfc_clear_warning ();
1331 undo_new_statement ();
1335 /* Generic complaint about an out of order statement. We also do
1336 whatever is necessary to clean up. */
1339 unexpected_statement (gfc_statement st)
1342 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1344 reject_statement ();
1348 /* Given the next statement seen by the matcher, make sure that it is
1349 in proper order with the last. This subroutine is initialized by
1350 calling it with an argument of ST_NONE. If there is a problem, we
1351 issue an error and return FAILURE. Otherwise we return SUCCESS.
1353 Individual parsers need to verify that the statements seen are
1354 valid before calling here, ie ENTRY statements are not allowed in
1355 INTERFACE blocks. The following diagram is taken from the standard:
1357 +---------------------------------------+
1358 | program subroutine function module |
1359 +---------------------------------------+
1361 +---------------------------------------+
1363 +---------------------------------------+
1365 | +-----------+------------------+
1366 | | parameter | implicit |
1367 | +-----------+------------------+
1368 | format | | derived type |
1369 | entry | parameter | interface |
1370 | | data | specification |
1371 | | | statement func |
1372 | +-----------+------------------+
1373 | | data | executable |
1374 +--------+-----------+------------------+
1376 +---------------------------------------+
1377 | internal module/subprogram |
1378 +---------------------------------------+
1380 +---------------------------------------+
1387 { ORDER_START, ORDER_USE, ORDER_IMPORT, ORDER_IMPLICIT_NONE,
1388 ORDER_IMPLICIT, ORDER_SPEC, ORDER_EXEC
1391 gfc_statement last_statement;
1397 verify_st_order (st_state * p, gfc_statement st)
1403 p->state = ORDER_START;
1407 if (p->state > ORDER_USE)
1409 p->state = ORDER_USE;
1413 if (p->state > ORDER_IMPORT)
1415 p->state = ORDER_IMPORT;
1418 case ST_IMPLICIT_NONE:
1419 if (p->state > ORDER_IMPLICIT_NONE)
1422 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1423 statement disqualifies a USE but not an IMPLICIT NONE.
1424 Duplicate IMPLICIT NONEs are caught when the implicit types
1427 p->state = ORDER_IMPLICIT_NONE;
1431 if (p->state > ORDER_IMPLICIT)
1433 p->state = ORDER_IMPLICIT;
1438 if (p->state < ORDER_IMPLICIT_NONE)
1439 p->state = ORDER_IMPLICIT_NONE;
1443 if (p->state >= ORDER_EXEC)
1445 if (p->state < ORDER_IMPLICIT)
1446 p->state = ORDER_IMPLICIT;
1450 if (p->state < ORDER_SPEC)
1451 p->state = ORDER_SPEC;
1456 case ST_DERIVED_DECL:
1458 if (p->state >= ORDER_EXEC)
1460 if (p->state < ORDER_SPEC)
1461 p->state = ORDER_SPEC;
1466 if (p->state < ORDER_EXEC)
1467 p->state = ORDER_EXEC;
1472 ("Unexpected %s statement in verify_st_order() at %C",
1473 gfc_ascii_statement (st));
1476 /* All is well, record the statement in case we need it next time. */
1477 p->where = gfc_current_locus;
1478 p->last_statement = st;
1482 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1483 gfc_ascii_statement (st),
1484 gfc_ascii_statement (p->last_statement), &p->where);
1490 /* Handle an unexpected end of file. This is a show-stopper... */
1492 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1495 unexpected_eof (void)
1499 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1501 /* Memory cleanup. Move to "second to last". */
1502 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1505 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1508 longjmp (eof_buf, 1);
1512 /* Parse a derived type. */
1515 parse_derived (void)
1517 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1525 accept_statement (ST_DERIVED_DECL);
1526 push_state (&s, COMP_DERIVED, gfc_new_block);
1528 gfc_new_block->component_access = ACCESS_PUBLIC;
1535 while (compiling_type)
1537 st = next_statement ();
1544 accept_statement (st);
1551 if (!seen_component)
1553 gfc_error ("Derived type definition at %C has no components");
1557 accept_statement (ST_END_TYPE);
1561 if (gfc_find_state (COMP_MODULE) == FAILURE)
1564 ("PRIVATE statement in TYPE at %C must be inside a MODULE");
1571 gfc_error ("PRIVATE statement at %C must precede "
1572 "structure components");
1579 gfc_error ("Duplicate PRIVATE statement at %C");
1583 s.sym->component_access = ACCESS_PRIVATE;
1584 accept_statement (ST_PRIVATE);
1591 gfc_error ("SEQUENCE statement at %C must precede "
1592 "structure components");
1597 if (gfc_current_block ()->attr.sequence)
1598 gfc_warning ("SEQUENCE attribute at %C already specified in "
1603 gfc_error ("Duplicate SEQUENCE statement at %C");
1608 gfc_add_sequence (&gfc_current_block ()->attr,
1609 gfc_current_block ()->name, NULL);
1613 unexpected_statement (st);
1618 /* Look for allocatable components. */
1619 sym = gfc_current_block ();
1620 for (c = sym->components; c; c = c->next)
1622 if (c->allocatable || (c->ts.type == BT_DERIVED
1623 && c->ts.derived->attr.alloc_comp))
1625 sym->attr.alloc_comp = 1;
1635 /* Parse an ENUM. */
1644 int seen_enumerator = 0;
1648 push_state (&s, COMP_ENUM, gfc_new_block);
1652 while (compiling_enum)
1654 st = next_statement ();
1662 seen_enumerator = 1;
1663 accept_statement (st);
1668 if (!seen_enumerator)
1670 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1673 accept_statement (st);
1677 gfc_free_enum_history ();
1678 unexpected_statement (st);
1685 /* Parse an interface. We must be able to deal with the possibility
1686 of recursive interfaces. The parse_spec() subroutine is mutually
1687 recursive with parse_interface(). */
1689 static gfc_statement parse_spec (gfc_statement);
1692 parse_interface (void)
1694 gfc_compile_state new_state, current_state;
1695 gfc_symbol *prog_unit, *sym;
1696 gfc_interface_info save;
1697 gfc_state_data s1, s2;
1701 accept_statement (ST_INTERFACE);
1703 current_interface.ns = gfc_current_ns;
1704 save = current_interface;
1706 sym = (current_interface.type == INTERFACE_GENERIC
1707 || current_interface.type == INTERFACE_USER_OP) ? gfc_new_block : NULL;
1709 push_state (&s1, COMP_INTERFACE, sym);
1710 current_state = COMP_NONE;
1713 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1715 st = next_statement ();
1722 new_state = COMP_SUBROUTINE;
1723 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1724 gfc_new_block->formal, NULL);
1728 new_state = COMP_FUNCTION;
1729 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1730 gfc_new_block->formal, NULL);
1733 case ST_MODULE_PROC: /* The module procedure matcher makes
1734 sure the context is correct. */
1735 accept_statement (st);
1736 gfc_free_namespace (gfc_current_ns);
1739 case ST_END_INTERFACE:
1740 gfc_free_namespace (gfc_current_ns);
1741 gfc_current_ns = current_interface.ns;
1745 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1746 gfc_ascii_statement (st));
1747 reject_statement ();
1748 gfc_free_namespace (gfc_current_ns);
1753 /* Make sure that a generic interface has only subroutines or
1754 functions and that the generic name has the right attribute. */
1755 if (current_interface.type == INTERFACE_GENERIC)
1757 if (current_state == COMP_NONE)
1759 if (new_state == COMP_FUNCTION)
1760 gfc_add_function (&sym->attr, sym->name, NULL);
1761 else if (new_state == COMP_SUBROUTINE)
1762 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1764 current_state = new_state;
1768 if (new_state != current_state)
1770 if (new_state == COMP_SUBROUTINE)
1772 ("SUBROUTINE at %C does not belong in a generic function "
1775 if (new_state == COMP_FUNCTION)
1777 ("FUNCTION at %C does not belong in a generic subroutine "
1783 push_state (&s2, new_state, gfc_new_block);
1784 accept_statement (st);
1785 prog_unit = gfc_new_block;
1786 prog_unit->formal_ns = gfc_current_ns;
1787 proc_locus = gfc_current_locus;
1790 /* Read data declaration statements. */
1791 st = parse_spec (ST_NONE);
1793 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1795 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1796 gfc_ascii_statement (st));
1797 reject_statement ();
1801 current_interface = save;
1802 gfc_add_interface (prog_unit);
1805 if (current_interface.ns
1806 && current_interface.ns->proc_name
1807 && strcmp (current_interface.ns->proc_name->name,
1808 prog_unit->name) == 0)
1809 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
1810 "enclosing procedure", prog_unit->name, &proc_locus);
1819 /* Parse a set of specification statements. Returns the statement
1820 that doesn't fit. */
1822 static gfc_statement
1823 parse_spec (gfc_statement st)
1827 verify_st_order (&ss, ST_NONE);
1829 st = next_statement ();
1839 case ST_DATA: /* Not allowed in interfaces */
1840 if (gfc_current_state () == COMP_INTERFACE)
1847 case ST_IMPLICIT_NONE:
1852 case ST_DERIVED_DECL:
1854 if (verify_st_order (&ss, st) == FAILURE)
1856 reject_statement ();
1857 st = next_statement ();
1867 case ST_DERIVED_DECL:
1873 if (gfc_current_state () != COMP_MODULE)
1875 gfc_error ("%s statement must appear in a MODULE",
1876 gfc_ascii_statement (st));
1880 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1882 gfc_error ("%s statement at %C follows another accessibility "
1883 "specification", gfc_ascii_statement (st));
1887 gfc_current_ns->default_access = (st == ST_PUBLIC)
1888 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1896 accept_statement (st);
1897 st = next_statement ();
1901 accept_statement (st);
1903 st = next_statement ();
1914 /* Parse a WHERE block, (not a simple WHERE statement). */
1917 parse_where_block (void)
1919 int seen_empty_else;
1924 accept_statement (ST_WHERE_BLOCK);
1925 top = gfc_state_stack->tail;
1927 push_state (&s, COMP_WHERE, gfc_new_block);
1929 d = add_statement ();
1930 d->expr = top->expr;
1936 seen_empty_else = 0;
1940 st = next_statement ();
1946 case ST_WHERE_BLOCK:
1947 parse_where_block ();
1952 accept_statement (st);
1956 if (seen_empty_else)
1959 ("ELSEWHERE statement at %C follows previous unmasked "
1964 if (new_st.expr == NULL)
1965 seen_empty_else = 1;
1967 d = new_level (gfc_state_stack->head);
1969 d->expr = new_st.expr;
1971 accept_statement (st);
1976 accept_statement (st);
1980 gfc_error ("Unexpected %s statement in WHERE block at %C",
1981 gfc_ascii_statement (st));
1982 reject_statement ();
1987 while (st != ST_END_WHERE);
1993 /* Parse a FORALL block (not a simple FORALL statement). */
1996 parse_forall_block (void)
2002 accept_statement (ST_FORALL_BLOCK);
2003 top = gfc_state_stack->tail;
2005 push_state (&s, COMP_FORALL, gfc_new_block);
2007 d = add_statement ();
2008 d->op = EXEC_FORALL;
2013 st = next_statement ();
2018 case ST_POINTER_ASSIGNMENT:
2021 accept_statement (st);
2024 case ST_WHERE_BLOCK:
2025 parse_where_block ();
2028 case ST_FORALL_BLOCK:
2029 parse_forall_block ();
2033 accept_statement (st);
2040 gfc_error ("Unexpected %s statement in FORALL block at %C",
2041 gfc_ascii_statement (st));
2043 reject_statement ();
2047 while (st != ST_END_FORALL);
2053 static gfc_statement parse_executable (gfc_statement);
2055 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2058 parse_if_block (void)
2067 accept_statement (ST_IF_BLOCK);
2069 top = gfc_state_stack->tail;
2070 push_state (&s, COMP_IF, gfc_new_block);
2072 new_st.op = EXEC_IF;
2073 d = add_statement ();
2075 d->expr = top->expr;
2081 st = parse_executable (ST_NONE);
2092 ("ELSE IF statement at %C cannot follow ELSE statement at %L",
2095 reject_statement ();
2099 d = new_level (gfc_state_stack->head);
2101 d->expr = new_st.expr;
2103 accept_statement (st);
2110 gfc_error ("Duplicate ELSE statements at %L and %C",
2112 reject_statement ();
2117 else_locus = gfc_current_locus;
2119 d = new_level (gfc_state_stack->head);
2122 accept_statement (st);
2130 unexpected_statement (st);
2134 while (st != ST_ENDIF);
2137 accept_statement (st);
2141 /* Parse a SELECT block. */
2144 parse_select_block (void)
2150 accept_statement (ST_SELECT_CASE);
2152 cp = gfc_state_stack->tail;
2153 push_state (&s, COMP_SELECT, gfc_new_block);
2155 /* Make sure that the next statement is a CASE or END SELECT. */
2158 st = next_statement ();
2161 if (st == ST_END_SELECT)
2163 /* Empty SELECT CASE is OK. */
2164 accept_statement (st);
2172 ("Expected a CASE or END SELECT statement following SELECT CASE "
2175 reject_statement ();
2178 /* At this point, we're got a nonempty select block. */
2179 cp = new_level (cp);
2182 accept_statement (st);
2186 st = parse_executable (ST_NONE);
2193 cp = new_level (gfc_state_stack->head);
2195 gfc_clear_new_st ();
2197 accept_statement (st);
2203 /* Can't have an executable statement because of
2204 parse_executable(). */
2206 unexpected_statement (st);
2210 while (st != ST_END_SELECT);
2213 accept_statement (st);
2217 /* Given a symbol, make sure it is not an iteration variable for a DO
2218 statement. This subroutine is called when the symbol is seen in a
2219 context that causes it to become redefined. If the symbol is an
2220 iterator, we generate an error message and return nonzero. */
2223 gfc_check_do_variable (gfc_symtree *st)
2227 for (s=gfc_state_stack; s; s = s->previous)
2228 if (s->do_variable == st)
2230 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2231 "loop beginning at %L", st->name, &s->head->loc);
2239 /* Checks to see if the current statement label closes an enddo.
2240 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2241 an error) if it incorrectly closes an ENDDO. */
2244 check_do_closure (void)
2248 if (gfc_statement_label == NULL)
2251 for (p = gfc_state_stack; p; p = p->previous)
2252 if (p->state == COMP_DO)
2256 return 0; /* No loops to close */
2258 if (p->ext.end_do_label == gfc_statement_label)
2261 if (p == gfc_state_stack)
2265 ("End of nonblock DO statement at %C is within another block");
2269 /* At this point, the label doesn't terminate the innermost loop.
2270 Make sure it doesn't terminate another one. */
2271 for (; p; p = p->previous)
2272 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
2274 gfc_error ("End of nonblock DO statement at %C is interwoven "
2275 "with another DO loop");
2283 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2284 handled inside of parse_executable(), because they aren't really
2288 parse_do_block (void)
2295 s.ext.end_do_label = new_st.label;
2297 if (new_st.ext.iterator != NULL)
2298 stree = new_st.ext.iterator->var->symtree;
2302 accept_statement (ST_DO);
2304 top = gfc_state_stack->tail;
2305 push_state (&s, COMP_DO, gfc_new_block);
2307 s.do_variable = stree;
2309 top->block = new_level (top);
2310 top->block->op = EXEC_DO;
2313 st = parse_executable (ST_NONE);
2321 if (s.ext.end_do_label != NULL
2322 && s.ext.end_do_label != gfc_statement_label)
2324 ("Statement label in ENDDO at %C doesn't match DO label");
2326 if (gfc_statement_label != NULL)
2328 new_st.op = EXEC_NOP;
2333 case ST_IMPLIED_ENDDO:
2334 /* If the do-stmt of this DO construct has a do-construct-name,
2335 the corresponding end-do must be an end-do-stmt (with a matching
2336 name, but in that case we must have seen ST_ENDDO first).
2337 We only complain about this in pedantic mode. */
2338 if (gfc_current_block () != NULL)
2340 ("named block DO at %L requires matching ENDDO name",
2341 &gfc_current_block()->declared_at);
2346 unexpected_statement (st);
2351 accept_statement (st);
2355 /* Parse the statements of OpenMP do/parallel do. */
2357 static gfc_statement
2358 parse_omp_do (gfc_statement omp_st)
2364 accept_statement (omp_st);
2366 cp = gfc_state_stack->tail;
2367 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2368 np = new_level (cp);
2374 st = next_statement ();
2377 else if (st == ST_DO)
2380 unexpected_statement (st);
2384 if (gfc_statement_label != NULL
2385 && gfc_state_stack->previous != NULL
2386 && gfc_state_stack->previous->state == COMP_DO
2387 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
2395 there should be no !$OMP END DO. */
2397 return ST_IMPLIED_ENDDO;
2400 check_do_closure ();
2403 st = next_statement ();
2404 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
2406 if (new_st.op == EXEC_OMP_END_NOWAIT)
2407 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2409 gcc_assert (new_st.op == EXEC_NOP);
2410 gfc_clear_new_st ();
2411 gfc_commit_symbols ();
2412 gfc_warning_check ();
2413 st = next_statement ();
2419 /* Parse the statements of OpenMP atomic directive. */
2422 parse_omp_atomic (void)
2428 accept_statement (ST_OMP_ATOMIC);
2430 cp = gfc_state_stack->tail;
2431 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2432 np = new_level (cp);
2438 st = next_statement ();
2441 else if (st == ST_ASSIGNMENT)
2444 unexpected_statement (st);
2447 accept_statement (st);
2453 /* Parse the statements of an OpenMP structured block. */
2456 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
2458 gfc_statement st, omp_end_st;
2462 accept_statement (omp_st);
2464 cp = gfc_state_stack->tail;
2465 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2466 np = new_level (cp);
2472 case ST_OMP_PARALLEL:
2473 omp_end_st = ST_OMP_END_PARALLEL;
2475 case ST_OMP_PARALLEL_SECTIONS:
2476 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
2478 case ST_OMP_SECTIONS:
2479 omp_end_st = ST_OMP_END_SECTIONS;
2481 case ST_OMP_ORDERED:
2482 omp_end_st = ST_OMP_END_ORDERED;
2484 case ST_OMP_CRITICAL:
2485 omp_end_st = ST_OMP_END_CRITICAL;
2488 omp_end_st = ST_OMP_END_MASTER;
2491 omp_end_st = ST_OMP_END_SINGLE;
2493 case ST_OMP_WORKSHARE:
2494 omp_end_st = ST_OMP_END_WORKSHARE;
2496 case ST_OMP_PARALLEL_WORKSHARE:
2497 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
2505 if (workshare_stmts_only)
2507 /* Inside of !$omp workshare, only
2510 where statements and constructs
2511 forall statements and constructs
2515 are allowed. For !$omp critical these
2516 restrictions apply recursively. */
2519 st = next_statement ();
2530 accept_statement (st);
2533 case ST_WHERE_BLOCK:
2534 parse_where_block ();
2537 case ST_FORALL_BLOCK:
2538 parse_forall_block ();
2541 case ST_OMP_PARALLEL:
2542 case ST_OMP_PARALLEL_SECTIONS:
2543 parse_omp_structured_block (st, false);
2546 case ST_OMP_PARALLEL_WORKSHARE:
2547 case ST_OMP_CRITICAL:
2548 parse_omp_structured_block (st, true);
2551 case ST_OMP_PARALLEL_DO:
2552 st = parse_omp_do (st);
2556 parse_omp_atomic ();
2567 st = next_statement ();
2571 st = parse_executable (ST_NONE);
2574 else if (st == ST_OMP_SECTION
2575 && (omp_st == ST_OMP_SECTIONS
2576 || omp_st == ST_OMP_PARALLEL_SECTIONS))
2578 np = new_level (np);
2582 else if (st != omp_end_st)
2583 unexpected_statement (st);
2585 while (st != omp_end_st);
2589 case EXEC_OMP_END_NOWAIT:
2590 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2592 case EXEC_OMP_CRITICAL:
2593 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
2594 || (new_st.ext.omp_name != NULL
2595 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
2596 gfc_error ("Name after !$omp critical and !$omp end critical does"
2597 " not match at %C");
2598 gfc_free ((char *) new_st.ext.omp_name);
2600 case EXEC_OMP_END_SINGLE:
2601 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
2602 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
2603 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
2604 gfc_free_omp_clauses (new_st.ext.omp_clauses);
2612 gfc_clear_new_st ();
2613 gfc_commit_symbols ();
2614 gfc_warning_check ();
2619 /* Accept a series of executable statements. We return the first
2620 statement that doesn't fit to the caller. Any block statements are
2621 passed on to the correct handler, which usually passes the buck
2624 static gfc_statement
2625 parse_executable (gfc_statement st)
2630 st = next_statement ();
2634 close_flag = check_do_closure ();
2639 case ST_END_PROGRAM:
2642 case ST_END_FUNCTION:
2646 case ST_END_SUBROUTINE:
2651 case ST_SELECT_CASE:
2653 ("%s statement at %C cannot terminate a non-block DO loop",
2654 gfc_ascii_statement (st));
2670 accept_statement (st);
2671 if (close_flag == 1)
2672 return ST_IMPLIED_ENDDO;
2679 case ST_SELECT_CASE:
2680 parse_select_block ();
2685 if (check_do_closure () == 1)
2686 return ST_IMPLIED_ENDDO;
2689 case ST_WHERE_BLOCK:
2690 parse_where_block ();
2693 case ST_FORALL_BLOCK:
2694 parse_forall_block ();
2697 case ST_OMP_PARALLEL:
2698 case ST_OMP_PARALLEL_SECTIONS:
2699 case ST_OMP_SECTIONS:
2700 case ST_OMP_ORDERED:
2701 case ST_OMP_CRITICAL:
2704 parse_omp_structured_block (st, false);
2707 case ST_OMP_WORKSHARE:
2708 case ST_OMP_PARALLEL_WORKSHARE:
2709 parse_omp_structured_block (st, true);
2713 case ST_OMP_PARALLEL_DO:
2714 st = parse_omp_do (st);
2715 if (st == ST_IMPLIED_ENDDO)
2720 parse_omp_atomic ();
2727 st = next_statement ();
2732 /* Parse a series of contained program units. */
2734 static void parse_progunit (gfc_statement);
2737 /* Fix the symbols for sibling functions. These are incorrectly added to
2738 the child namespace as the parser didn't know about this procedure. */
2741 gfc_fixup_sibling_symbols (gfc_symbol * sym, gfc_namespace * siblings)
2745 gfc_symbol *old_sym;
2747 sym->attr.referenced = 1;
2748 for (ns = siblings; ns; ns = ns->sibling)
2750 gfc_find_sym_tree (sym->name, ns, 0, &st);
2752 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
2755 old_sym = st->n.sym;
2756 if ((old_sym->attr.flavor == FL_PROCEDURE
2757 || old_sym->ts.type == BT_UNKNOWN)
2758 && old_sym->ns == ns
2759 && ! old_sym->attr.contained)
2761 /* Replace it with the symbol from the parent namespace. */
2765 /* Free the old (local) symbol. */
2767 if (old_sym->refs == 0)
2768 gfc_free_symbol (old_sym);
2771 /* Do the same for any contained procedures. */
2772 gfc_fixup_sibling_symbols (sym, ns->contained);
2777 parse_contained (int module)
2779 gfc_namespace *ns, *parent_ns;
2780 gfc_state_data s1, s2;
2784 int contains_statements = 0;
2786 push_state (&s1, COMP_CONTAINS, NULL);
2787 parent_ns = gfc_current_ns;
2791 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2793 gfc_current_ns->sibling = parent_ns->contained;
2794 parent_ns->contained = gfc_current_ns;
2796 st = next_statement ();
2805 contains_statements = 1;
2806 accept_statement (st);
2809 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2812 /* For internal procedures, create/update the symbol in the
2813 parent namespace. */
2817 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2819 ("Contained procedure '%s' at %C is already ambiguous",
2820 gfc_new_block->name);
2823 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2824 &gfc_new_block->declared_at) ==
2827 if (st == ST_FUNCTION)
2828 gfc_add_function (&sym->attr, sym->name,
2829 &gfc_new_block->declared_at);
2831 gfc_add_subroutine (&sym->attr, sym->name,
2832 &gfc_new_block->declared_at);
2836 gfc_commit_symbols ();
2839 sym = gfc_new_block;
2841 /* Mark this as a contained function, so it isn't replaced
2842 by other module functions. */
2843 sym->attr.contained = 1;
2844 sym->attr.referenced = 1;
2846 parse_progunit (ST_NONE);
2848 /* Fix up any sibling functions that refer to this one. */
2849 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2850 /* Or refer to any of its alternate entry points. */
2851 for (el = gfc_current_ns->entries; el; el = el->next)
2852 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2854 gfc_current_ns->code = s2.head;
2855 gfc_current_ns = parent_ns;
2860 /* These statements are associated with the end of the host
2862 case ST_END_FUNCTION:
2864 case ST_END_PROGRAM:
2865 case ST_END_SUBROUTINE:
2866 accept_statement (st);
2870 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2871 gfc_ascii_statement (st));
2872 reject_statement ();
2876 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2877 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2879 /* The first namespace in the list is guaranteed to not have
2880 anything (worthwhile) in it. */
2882 gfc_current_ns = parent_ns;
2884 ns = gfc_current_ns->contained;
2885 gfc_current_ns->contained = ns->sibling;
2886 gfc_free_namespace (ns);
2889 if (!contains_statements)
2890 /* This is valid in Fortran 2008. */
2891 gfc_notify_std (GFC_STD_GNU, "Extension: "
2892 "CONTAINS statement without FUNCTION "
2893 "or SUBROUTINE statement at %C");
2897 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2900 parse_progunit (gfc_statement st)
2905 st = parse_spec (st);
2915 accept_statement (st);
2925 st = parse_executable (st);
2936 accept_statement (st);
2943 unexpected_statement (st);
2944 reject_statement ();
2945 st = next_statement ();
2951 for (p = gfc_state_stack; p; p = p->previous)
2952 if (p->state == COMP_CONTAINS)
2955 if (gfc_find_state (COMP_MODULE) == SUCCESS)
2960 gfc_error ("CONTAINS statement at %C is already in a contained "
2962 st = next_statement ();
2966 parse_contained (0);
2969 gfc_current_ns->code = gfc_state_stack->head;
2973 /* Come here to complain about a global symbol already in use as
2977 global_used (gfc_gsymbol *sym, locus *where)
2982 where = &gfc_current_locus;
2992 case GSYM_SUBROUTINE:
2993 name = "SUBROUTINE";
2998 case GSYM_BLOCK_DATA:
2999 name = "BLOCK DATA";
3005 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
3009 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
3010 sym->name, where, name, &sym->where);
3014 /* Parse a block data program unit. */
3017 parse_block_data (void)
3020 static locus blank_locus;
3021 static int blank_block=0;
3024 gfc_current_ns->proc_name = gfc_new_block;
3025 gfc_current_ns->is_block_data = 1;
3027 if (gfc_new_block == NULL)
3030 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3031 "prior BLOCK DATA at %L", &blank_locus);
3035 blank_locus = gfc_current_locus;
3040 s = gfc_get_gsymbol (gfc_new_block->name);
3041 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
3042 global_used(s, NULL);
3045 s->type = GSYM_BLOCK_DATA;
3046 s->where = gfc_current_locus;
3051 st = parse_spec (ST_NONE);
3053 while (st != ST_END_BLOCK_DATA)
3055 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3056 gfc_ascii_statement (st));
3057 reject_statement ();
3058 st = next_statement ();
3063 /* Parse a module subprogram. */
3071 s = gfc_get_gsymbol (gfc_new_block->name);
3072 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
3073 global_used(s, NULL);
3076 s->type = GSYM_MODULE;
3077 s->where = gfc_current_locus;
3081 st = parse_spec (ST_NONE);
3090 parse_contained (1);
3094 accept_statement (st);
3098 gfc_error ("Unexpected %s statement in MODULE at %C",
3099 gfc_ascii_statement (st));
3101 reject_statement ();
3102 st = next_statement ();
3108 /* Add a procedure name to the global symbol table. */
3111 add_global_procedure (int sub)
3115 s = gfc_get_gsymbol(gfc_new_block->name);
3118 || (s->type != GSYM_UNKNOWN && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3119 global_used(s, NULL);
3122 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3123 s->where = gfc_current_locus;
3129 /* Add a program to the global symbol table. */
3132 add_global_program (void)
3136 if (gfc_new_block == NULL)
3138 s = gfc_get_gsymbol (gfc_new_block->name);
3140 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
3141 global_used(s, NULL);
3144 s->type = GSYM_PROGRAM;
3145 s->where = gfc_current_locus;
3151 /* Top level parser. */
3154 gfc_parse_file (void)
3156 int seen_program, errors_before, errors;
3157 gfc_state_data top, s;
3161 top.state = COMP_NONE;
3163 top.previous = NULL;
3164 top.head = top.tail = NULL;
3165 top.do_variable = NULL;
3167 gfc_state_stack = ⊤
3169 gfc_clear_new_st ();
3171 gfc_statement_label = NULL;
3173 if (setjmp (eof_buf))
3174 return FAILURE; /* Come here on unexpected EOF */
3178 /* Exit early for empty files. */
3184 st = next_statement ();
3193 goto duplicate_main;
3195 prog_locus = gfc_current_locus;
3197 push_state (&s, COMP_PROGRAM, gfc_new_block);
3198 main_program_symbol(gfc_current_ns);
3199 accept_statement (st);
3200 add_global_program ();
3201 parse_progunit (ST_NONE);
3205 add_global_procedure (1);
3206 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
3207 accept_statement (st);
3208 parse_progunit (ST_NONE);
3212 add_global_procedure (0);
3213 push_state (&s, COMP_FUNCTION, gfc_new_block);
3214 accept_statement (st);
3215 parse_progunit (ST_NONE);
3219 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
3220 accept_statement (st);
3221 parse_block_data ();
3225 push_state (&s, COMP_MODULE, gfc_new_block);
3226 accept_statement (st);
3228 gfc_get_errors (NULL, &errors_before);
3232 /* Anything else starts a nameless main program block. */
3235 goto duplicate_main;
3237 prog_locus = gfc_current_locus;
3239 push_state (&s, COMP_PROGRAM, gfc_new_block);
3240 main_program_symbol(gfc_current_ns);
3241 parse_progunit (st);
3245 gfc_current_ns->code = s.head;
3247 gfc_resolve (gfc_current_ns);
3249 /* Dump the parse tree if requested. */
3250 if (gfc_option.verbose)
3251 gfc_show_namespace (gfc_current_ns);
3253 gfc_get_errors (NULL, &errors);
3254 if (s.state == COMP_MODULE)
3256 gfc_dump_module (s.sym->name, errors_before == errors);
3258 gfc_generate_module_code (gfc_current_ns);
3263 gfc_generate_code (gfc_current_ns);
3274 /* If we see a duplicate main program, shut down. If the second
3275 instance is an implied main program, ie data decls or executable
3276 statements, we're in for lots of errors. */
3277 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
3278 reject_statement ();