2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
30 /* Current statement label. Zero means no statement label. Because new_st
31 can get wiped during statement matching, we have to keep it separate. */
33 gfc_st_label *gfc_statement_label;
35 static locus label_locus;
36 static jmp_buf eof_buf;
38 gfc_state_data *gfc_state_stack;
40 /* TODO: Re-order functions to kill these forward decls. */
41 static void check_statement_label (gfc_statement);
42 static void undo_new_statement (void);
43 static void reject_statement (void);
46 /* A sort of half-matching function. We try to match the word on the
47 input with the passed string. If this succeeds, we call the
48 keyword-dependent matching function that will match the rest of the
49 statement. For single keywords, the matching subroutine is
53 match_word (const char *str, match (*subr) (void), locus *old_locus)
68 gfc_current_locus = *old_locus;
76 /* Figure out what the next statement is, (mostly) regardless of
77 proper ordering. The do...while(0) is there to prevent if/else
80 #define match(keyword, subr, st) \
82 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
85 undo_new_statement (); \
89 decode_statement (void)
100 gfc_clear_error (); /* Clear any pending errors. */
101 gfc_clear_warning (); /* Clear any pending warnings. */
103 if (gfc_match_eos () == MATCH_YES)
106 old_locus = gfc_current_locus;
108 /* Try matching a data declaration or function declaration. The
109 input "REALFUNCTIONA(N)" can mean several things in different
110 contexts, so it (and its relatives) get special treatment. */
112 if (gfc_current_state () == COMP_NONE
113 || gfc_current_state () == COMP_INTERFACE
114 || gfc_current_state () == COMP_CONTAINS)
116 m = gfc_match_function_decl ();
119 else if (m == MATCH_ERROR)
123 gfc_current_locus = old_locus;
126 /* Match statements whose error messages are meant to be overwritten
127 by something better. */
129 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
130 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
131 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
133 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
134 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
136 /* Try to match a subroutine statement, which has the same optional
137 prefixes that functions can have. */
139 if (gfc_match_subroutine () == MATCH_YES)
140 return ST_SUBROUTINE;
142 gfc_current_locus = old_locus;
144 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
145 might begin with a block label. The match functions for these
146 statements are unusual in that their keyword is not seen before
147 the matcher is called. */
149 if (gfc_match_if (&st) == MATCH_YES)
152 gfc_current_locus = old_locus;
154 if (gfc_match_where (&st) == MATCH_YES)
157 gfc_current_locus = old_locus;
159 if (gfc_match_forall (&st) == MATCH_YES)
162 gfc_current_locus = old_locus;
164 match (NULL, gfc_match_do, ST_DO);
165 match (NULL, gfc_match_select, ST_SELECT_CASE);
167 /* General statement matching: Instead of testing every possible
168 statement, we eliminate most possibilities by peeking at the
171 c = gfc_peek_char ();
176 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
177 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
178 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
182 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
183 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
184 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
188 match ("call", gfc_match_call, ST_CALL);
189 match ("close", gfc_match_close, ST_CLOSE);
190 match ("continue", gfc_match_continue, ST_CONTINUE);
191 match ("cycle", gfc_match_cycle, ST_CYCLE);
192 match ("case", gfc_match_case, ST_CASE);
193 match ("common", gfc_match_common, ST_COMMON);
194 match ("contains", gfc_match_eos, ST_CONTAINS);
198 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
199 match ("data", gfc_match_data, ST_DATA);
200 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
204 match ("end file", gfc_match_endfile, ST_END_FILE);
205 match ("exit", gfc_match_exit, ST_EXIT);
206 match ("else", gfc_match_else, ST_ELSE);
207 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
208 match ("else if", gfc_match_elseif, ST_ELSEIF);
209 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
211 if (gfc_match_end (&st) == MATCH_YES)
214 match ("entry% ", gfc_match_entry, ST_ENTRY);
215 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
216 match ("external", gfc_match_external, ST_ATTR_DECL);
220 match ("flush", gfc_match_flush, ST_FLUSH);
221 match ("format", gfc_match_format, ST_FORMAT);
225 match ("go to", gfc_match_goto, ST_GOTO);
229 match ("inquire", gfc_match_inquire, ST_INQUIRE);
230 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
231 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
232 match ("import", gfc_match_import, ST_IMPORT);
233 match ("interface", gfc_match_interface, ST_INTERFACE);
234 match ("intent", gfc_match_intent, ST_ATTR_DECL);
235 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
239 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
240 match ("module", gfc_match_module, ST_MODULE);
244 match ("nullify", gfc_match_nullify, ST_NULLIFY);
245 match ("namelist", gfc_match_namelist, ST_NAMELIST);
249 match ("open", gfc_match_open, ST_OPEN);
250 match ("optional", gfc_match_optional, ST_ATTR_DECL);
254 match ("print", gfc_match_print, ST_WRITE);
255 match ("parameter", gfc_match_parameter, ST_PARAMETER);
256 match ("pause", gfc_match_pause, ST_PAUSE);
257 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
258 if (gfc_match_private (&st) == MATCH_YES)
260 match ("program", gfc_match_program, ST_PROGRAM);
261 if (gfc_match_public (&st) == MATCH_YES)
263 match ("protected", gfc_match_protected, ST_ATTR_DECL);
267 match ("read", gfc_match_read, ST_READ);
268 match ("return", gfc_match_return, ST_RETURN);
269 match ("rewind", gfc_match_rewind, ST_REWIND);
273 match ("sequence", gfc_match_eos, ST_SEQUENCE);
274 match ("stop", gfc_match_stop, ST_STOP);
275 match ("save", gfc_match_save, ST_ATTR_DECL);
279 match ("target", gfc_match_target, ST_ATTR_DECL);
280 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
284 match ("use", gfc_match_use, ST_USE);
288 match ("value", gfc_match_value, ST_ATTR_DECL);
289 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
293 match ("write", gfc_match_write, ST_WRITE);
297 /* All else has failed, so give up. See if any of the matchers has
298 stored an error message of some sort. */
300 if (gfc_error_check () == 0)
301 gfc_error_now ("Unclassifiable statement at %C");
305 gfc_error_recovery ();
311 decode_omp_directive (void)
320 gfc_clear_error (); /* Clear any pending errors. */
321 gfc_clear_warning (); /* Clear any pending warnings. */
325 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
326 "or ELEMENTAL procedures");
327 gfc_error_recovery ();
331 old_locus = gfc_current_locus;
333 /* General OpenMP directive matching: Instead of testing every possible
334 statement, we eliminate most possibilities by peeking at the
337 c = gfc_peek_char ();
342 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
345 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
348 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
351 match ("do", gfc_match_omp_do, ST_OMP_DO);
354 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
355 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
356 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
357 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
358 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
359 match ("end parallel sections", gfc_match_omp_eos,
360 ST_OMP_END_PARALLEL_SECTIONS);
361 match ("end parallel workshare", gfc_match_omp_eos,
362 ST_OMP_END_PARALLEL_WORKSHARE);
363 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
364 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
365 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
366 match ("end workshare", gfc_match_omp_end_nowait,
367 ST_OMP_END_WORKSHARE);
370 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
373 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
376 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
379 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
380 match ("parallel sections", gfc_match_omp_parallel_sections,
381 ST_OMP_PARALLEL_SECTIONS);
382 match ("parallel workshare", gfc_match_omp_parallel_workshare,
383 ST_OMP_PARALLEL_WORKSHARE);
384 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
387 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
388 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
389 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
392 match ("threadprivate", gfc_match_omp_threadprivate,
393 ST_OMP_THREADPRIVATE);
395 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
399 /* All else has failed, so give up. See if any of the matchers has
400 stored an error message of some sort. */
402 if (gfc_error_check () == 0)
403 gfc_error_now ("Unclassifiable OpenMP directive at %C");
407 gfc_error_recovery ();
415 /* Get the next statement in free form source. */
421 int c, d, cnt, at_bol;
423 at_bol = gfc_at_bol ();
424 gfc_gobble_whitespace ();
426 c = gfc_peek_char ();
430 /* Found a statement label? */
431 m = gfc_match_st_label (&gfc_statement_label);
433 d = gfc_peek_char ();
434 if (m != MATCH_YES || !gfc_is_whitespace (d))
436 gfc_match_small_literal_int (&c, &cnt);
439 gfc_error_now ("Too many digits in statement label at %C");
442 gfc_error_now ("Zero is not a valid statement label at %C");
445 c = gfc_next_char ();
448 if (!gfc_is_whitespace (c))
449 gfc_error_now ("Non-numeric character in statement label at %C");
455 label_locus = gfc_current_locus;
457 gfc_gobble_whitespace ();
459 if (at_bol && gfc_peek_char () == ';')
461 gfc_error_now ("Semicolon at %C needs to be preceded by "
463 gfc_next_char (); /* Eat up the semicolon. */
467 if (gfc_match_eos () == MATCH_YES)
469 gfc_warning_now ("Ignoring statement label in empty statement "
471 gfc_free_st_label (gfc_statement_label);
472 gfc_statement_label = NULL;
479 /* Comments have already been skipped by the time we get here,
480 except for OpenMP directives. */
481 if (gfc_option.flag_openmp)
485 c = gfc_next_char ();
486 for (i = 0; i < 5; i++, c = gfc_next_char ())
487 gcc_assert (c == "!$omp"[i]);
489 gcc_assert (c == ' ');
490 gfc_gobble_whitespace ();
491 return decode_omp_directive ();
495 if (at_bol && c == ';')
497 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
498 gfc_next_char (); /* Eat up the semicolon. */
502 return decode_statement ();
506 /* Get the next statement in fixed-form source. */
511 int label, digit_flag, i;
516 return decode_statement ();
518 /* Skip past the current label field, parsing a statement label if
519 one is there. This is a weird number parser, since the number is
520 contained within five columns and can have any kind of embedded
521 spaces. We also check for characters that make the rest of the
527 for (i = 0; i < 5; i++)
529 c = gfc_next_char_literal (0);
546 label = label * 10 + c - '0';
547 label_locus = gfc_current_locus;
551 /* Comments have already been skipped by the time we get
552 here, except for OpenMP directives. */
554 if (gfc_option.flag_openmp)
556 for (i = 0; i < 5; i++, c = gfc_next_char_literal (0))
557 gcc_assert (TOLOWER (c) == "*$omp"[i]);
559 if (c != ' ' && c != '0')
561 gfc_buffer_error (0);
562 gfc_error ("Bad continuation line at %C");
566 return decode_omp_directive ();
570 /* Comments have already been skipped by the time we get
571 here so don't bother checking for them. */
574 gfc_buffer_error (0);
575 gfc_error ("Non-numeric character in statement label at %C");
583 gfc_warning_now ("Zero is not a valid statement label at %C");
586 /* We've found a valid statement label. */
587 gfc_statement_label = gfc_get_st_label (label);
591 /* Since this line starts a statement, it cannot be a continuation
592 of a previous statement. If we see something here besides a
593 space or zero, it must be a bad continuation line. */
595 c = gfc_next_char_literal (0);
599 if (c != ' ' && c != '0')
601 gfc_buffer_error (0);
602 gfc_error ("Bad continuation line at %C");
606 /* Now that we've taken care of the statement label columns, we have
607 to make sure that the first nonblank character is not a '!'. If
608 it is, the rest of the line is a comment. */
612 loc = gfc_current_locus;
613 c = gfc_next_char_literal (0);
615 while (gfc_is_whitespace (c));
619 gfc_current_locus = loc;
623 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
627 if (gfc_match_eos () == MATCH_YES)
630 /* At this point, we've got a nonblank statement to parse. */
631 return decode_statement ();
635 gfc_warning ("Ignoring statement label in empty statement at %C");
641 /* Return the next non-ST_NONE statement to the caller. We also worry
642 about including files and the ends of include files at this stage. */
645 next_statement (void)
649 gfc_new_block = NULL;
653 gfc_statement_label = NULL;
654 gfc_buffer_error (1);
658 if ((gfc_option.warn_line_truncation || gfc_current_form == FORM_FREE)
659 && gfc_current_locus.lb
660 && gfc_current_locus.lb->truncated)
661 gfc_warning_now ("Line truncated at %C");
666 gfc_skip_comments ();
674 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
680 gfc_buffer_error (0);
683 check_statement_label (st);
689 /****************************** Parser ***********************************/
691 /* The parser subroutines are of type 'try' that fail if the file ends
694 /* Macros that expand to case-labels for various classes of
695 statements. Start with executable statements that directly do
698 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
699 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
700 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
701 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
702 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
703 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
704 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
705 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
708 /* Statements that mark other executable statements. */
710 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
711 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
712 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
713 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
714 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
715 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
717 /* Declaration statements */
719 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
720 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
721 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE
723 /* Block end statements. Errors associated with interchanging these
724 are detected in gfc_match_end(). */
726 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
727 case ST_END_PROGRAM: case ST_END_SUBROUTINE
730 /* Push a new state onto the stack. */
733 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
735 p->state = new_state;
736 p->previous = gfc_state_stack;
738 p->head = p->tail = NULL;
739 p->do_variable = NULL;
744 /* Pop the current state. */
748 gfc_state_stack = gfc_state_stack->previous;
752 /* Try to find the given state in the state stack. */
755 gfc_find_state (gfc_compile_state state)
759 for (p = gfc_state_stack; p; p = p->previous)
760 if (p->state == state)
763 return (p == NULL) ? FAILURE : SUCCESS;
767 /* Starts a new level in the statement list. */
770 new_level (gfc_code *q)
774 p = q->block = gfc_get_code ();
776 gfc_state_stack->head = gfc_state_stack->tail = p;
782 /* Add the current new_st code structure and adds it to the current
783 program unit. As a side-effect, it zeroes the new_st. */
793 p->loc = gfc_current_locus;
795 if (gfc_state_stack->head == NULL)
796 gfc_state_stack->head = p;
798 gfc_state_stack->tail->next = p;
800 while (p->next != NULL)
803 gfc_state_stack->tail = p;
811 /* Frees everything associated with the current statement. */
814 undo_new_statement (void)
816 gfc_free_statements (new_st.block);
817 gfc_free_statements (new_st.next);
818 gfc_free_statement (&new_st);
823 /* If the current statement has a statement label, make sure that it
824 is allowed to, or should have one. */
827 check_statement_label (gfc_statement st)
831 if (gfc_statement_label == NULL)
834 gfc_error ("FORMAT statement at %L does not have a statement label",
842 case ST_END_FUNCTION:
843 case ST_END_SUBROUTINE:
849 type = ST_LABEL_TARGET;
853 type = ST_LABEL_FORMAT;
856 /* Statement labels are not restricted from appearing on a
857 particular line. However, there are plenty of situations
858 where the resulting label can't be referenced. */
861 type = ST_LABEL_BAD_TARGET;
865 gfc_define_st_label (gfc_statement_label, type, &label_locus);
867 new_st.here = gfc_statement_label;
871 /* Figures out what the enclosing program unit is. This will be a
872 function, subroutine, program, block data or module. */
875 gfc_enclosing_unit (gfc_compile_state * result)
879 for (p = gfc_state_stack; p; p = p->previous)
880 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
881 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
882 || p->state == COMP_PROGRAM)
891 *result = COMP_PROGRAM;
896 /* Translate a statement enum to a string. */
899 gfc_ascii_statement (gfc_statement st)
905 case ST_ARITHMETIC_IF:
906 p = _("arithmetic IF");
912 p = _("attribute declaration");
942 p = _("data declaration");
950 case ST_DERIVED_DECL:
951 p = _("derived type declaration");
965 case ST_END_BLOCK_DATA:
966 p = "END BLOCK DATA";
977 case ST_END_FUNCTION:
983 case ST_END_INTERFACE:
995 case ST_END_SUBROUTINE:
996 p = "END SUBROUTINE";
1007 case ST_EQUIVALENCE:
1016 case ST_FORALL_BLOCK: /* Fall through */
1035 case ST_IMPLICIT_NONE:
1036 p = "IMPLICIT NONE";
1038 case ST_IMPLIED_ENDDO:
1039 p = _("implied END DO");
1065 case ST_MODULE_PROC:
1066 p = "MODULE PROCEDURE";
1101 case ST_WHERE_BLOCK: /* Fall through */
1109 p = _("assignment");
1111 case ST_POINTER_ASSIGNMENT:
1112 p = _("pointer assignment");
1114 case ST_SELECT_CASE:
1123 case ST_STATEMENT_FUNCTION:
1124 p = "STATEMENT FUNCTION";
1126 case ST_LABEL_ASSIGNMENT:
1127 p = "LABEL ASSIGNMENT";
1130 p = "ENUM DEFINITION";
1133 p = "ENUMERATOR DEFINITION";
1141 case ST_OMP_BARRIER:
1142 p = "!$OMP BARRIER";
1144 case ST_OMP_CRITICAL:
1145 p = "!$OMP CRITICAL";
1150 case ST_OMP_END_CRITICAL:
1151 p = "!$OMP END CRITICAL";
1156 case ST_OMP_END_MASTER:
1157 p = "!$OMP END MASTER";
1159 case ST_OMP_END_ORDERED:
1160 p = "!$OMP END ORDERED";
1162 case ST_OMP_END_PARALLEL:
1163 p = "!$OMP END PARALLEL";
1165 case ST_OMP_END_PARALLEL_DO:
1166 p = "!$OMP END PARALLEL DO";
1168 case ST_OMP_END_PARALLEL_SECTIONS:
1169 p = "!$OMP END PARALLEL SECTIONS";
1171 case ST_OMP_END_PARALLEL_WORKSHARE:
1172 p = "!$OMP END PARALLEL WORKSHARE";
1174 case ST_OMP_END_SECTIONS:
1175 p = "!$OMP END SECTIONS";
1177 case ST_OMP_END_SINGLE:
1178 p = "!$OMP END SINGLE";
1180 case ST_OMP_END_WORKSHARE:
1181 p = "!$OMP END WORKSHARE";
1189 case ST_OMP_ORDERED:
1190 p = "!$OMP ORDERED";
1192 case ST_OMP_PARALLEL:
1193 p = "!$OMP PARALLEL";
1195 case ST_OMP_PARALLEL_DO:
1196 p = "!$OMP PARALLEL DO";
1198 case ST_OMP_PARALLEL_SECTIONS:
1199 p = "!$OMP PARALLEL SECTIONS";
1201 case ST_OMP_PARALLEL_WORKSHARE:
1202 p = "!$OMP PARALLEL WORKSHARE";
1204 case ST_OMP_SECTIONS:
1205 p = "!$OMP SECTIONS";
1207 case ST_OMP_SECTION:
1208 p = "!$OMP SECTION";
1213 case ST_OMP_THREADPRIVATE:
1214 p = "!$OMP THREADPRIVATE";
1216 case ST_OMP_WORKSHARE:
1217 p = "!$OMP WORKSHARE";
1220 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1227 /* Create a symbol for the main program and assign it to ns->proc_name. */
1230 main_program_symbol (gfc_namespace *ns)
1232 gfc_symbol *main_program;
1233 symbol_attribute attr;
1235 gfc_get_symbol ("MAIN__", ns, &main_program);
1236 gfc_clear_attr (&attr);
1237 attr.flavor = FL_PROCEDURE;
1238 attr.proc = PROC_UNKNOWN;
1239 attr.subroutine = 1;
1240 attr.access = ACCESS_PUBLIC;
1241 attr.is_main_program = 1;
1242 main_program->attr = attr;
1243 main_program->declared_at = gfc_current_locus;
1244 ns->proc_name = main_program;
1245 gfc_commit_symbols ();
1249 /* Do whatever is necessary to accept the last statement. */
1252 accept_statement (gfc_statement st)
1260 case ST_IMPLICIT_NONE:
1261 gfc_set_implicit_none ();
1270 gfc_current_ns->proc_name = gfc_new_block;
1273 /* If the statement is the end of a block, lay down a special code
1274 that allows a branch to the end of the block from within the
1279 if (gfc_statement_label != NULL)
1281 new_st.op = EXEC_NOP;
1287 /* The end-of-program unit statements do not get the special
1288 marker and require a statement of some sort if they are a
1291 case ST_END_PROGRAM:
1292 case ST_END_FUNCTION:
1293 case ST_END_SUBROUTINE:
1294 if (gfc_statement_label != NULL)
1296 new_st.op = EXEC_RETURN;
1312 gfc_commit_symbols ();
1313 gfc_warning_check ();
1314 gfc_clear_new_st ();
1318 /* Undo anything tentative that has been built for the current
1322 reject_statement (void)
1324 gfc_new_block = NULL;
1325 gfc_undo_symbols ();
1326 gfc_clear_warning ();
1327 undo_new_statement ();
1331 /* Generic complaint about an out of order statement. We also do
1332 whatever is necessary to clean up. */
1335 unexpected_statement (gfc_statement st)
1337 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1339 reject_statement ();
1343 /* Given the next statement seen by the matcher, make sure that it is
1344 in proper order with the last. This subroutine is initialized by
1345 calling it with an argument of ST_NONE. If there is a problem, we
1346 issue an error and return FAILURE. Otherwise we return SUCCESS.
1348 Individual parsers need to verify that the statements seen are
1349 valid before calling here, ie ENTRY statements are not allowed in
1350 INTERFACE blocks. The following diagram is taken from the standard:
1352 +---------------------------------------+
1353 | program subroutine function module |
1354 +---------------------------------------+
1356 +---------------------------------------+
1358 +---------------------------------------+
1360 | +-----------+------------------+
1361 | | parameter | implicit |
1362 | +-----------+------------------+
1363 | format | | derived type |
1364 | entry | parameter | interface |
1365 | | data | specification |
1366 | | | statement func |
1367 | +-----------+------------------+
1368 | | data | executable |
1369 +--------+-----------+------------------+
1371 +---------------------------------------+
1372 | internal module/subprogram |
1373 +---------------------------------------+
1375 +---------------------------------------+
1382 { ORDER_START, ORDER_USE, ORDER_IMPORT, ORDER_IMPLICIT_NONE,
1383 ORDER_IMPLICIT, ORDER_SPEC, ORDER_EXEC
1386 gfc_statement last_statement;
1392 verify_st_order (st_state *p, gfc_statement st)
1398 p->state = ORDER_START;
1402 if (p->state > ORDER_USE)
1404 p->state = ORDER_USE;
1408 if (p->state > ORDER_IMPORT)
1410 p->state = ORDER_IMPORT;
1413 case ST_IMPLICIT_NONE:
1414 if (p->state > ORDER_IMPLICIT_NONE)
1417 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1418 statement disqualifies a USE but not an IMPLICIT NONE.
1419 Duplicate IMPLICIT NONEs are caught when the implicit types
1422 p->state = ORDER_IMPLICIT_NONE;
1426 if (p->state > ORDER_IMPLICIT)
1428 p->state = ORDER_IMPLICIT;
1433 if (p->state < ORDER_IMPLICIT_NONE)
1434 p->state = ORDER_IMPLICIT_NONE;
1438 if (p->state >= ORDER_EXEC)
1440 if (p->state < ORDER_IMPLICIT)
1441 p->state = ORDER_IMPLICIT;
1445 if (p->state < ORDER_SPEC)
1446 p->state = ORDER_SPEC;
1451 case ST_DERIVED_DECL:
1453 if (p->state >= ORDER_EXEC)
1455 if (p->state < ORDER_SPEC)
1456 p->state = ORDER_SPEC;
1461 if (p->state < ORDER_EXEC)
1462 p->state = ORDER_EXEC;
1466 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1467 gfc_ascii_statement (st));
1470 /* All is well, record the statement in case we need it next time. */
1471 p->where = gfc_current_locus;
1472 p->last_statement = st;
1476 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1477 gfc_ascii_statement (st),
1478 gfc_ascii_statement (p->last_statement), &p->where);
1484 /* Handle an unexpected end of file. This is a show-stopper... */
1486 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1489 unexpected_eof (void)
1493 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1495 /* Memory cleanup. Move to "second to last". */
1496 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1499 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1502 longjmp (eof_buf, 1);
1506 /* Parse a derived type. */
1509 parse_derived (void)
1511 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1514 gfc_symbol *derived_sym = NULL;
1520 accept_statement (ST_DERIVED_DECL);
1521 push_state (&s, COMP_DERIVED, gfc_new_block);
1523 gfc_new_block->component_access = ACCESS_PUBLIC;
1530 while (compiling_type)
1532 st = next_statement ();
1539 accept_statement (st);
1546 if (!seen_component)
1548 gfc_error ("Derived type definition at %C has no components");
1552 accept_statement (ST_END_TYPE);
1556 if (gfc_find_state (COMP_MODULE) == FAILURE)
1558 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1566 gfc_error ("PRIVATE statement at %C must precede "
1567 "structure components");
1574 gfc_error ("Duplicate PRIVATE statement at %C");
1578 s.sym->component_access = ACCESS_PRIVATE;
1579 accept_statement (ST_PRIVATE);
1586 gfc_error ("SEQUENCE statement at %C must precede "
1587 "structure components");
1592 if (gfc_current_block ()->attr.sequence)
1593 gfc_warning ("SEQUENCE attribute at %C already specified in "
1598 gfc_error ("Duplicate SEQUENCE statement at %C");
1603 gfc_add_sequence (&gfc_current_block ()->attr,
1604 gfc_current_block ()->name, NULL);
1608 unexpected_statement (st);
1613 /* need to verify that all fields of the derived type are
1614 * interoperable with C if the type is declared to be bind(c)
1616 derived_sym = gfc_current_block();
1618 sym = gfc_current_block ();
1619 for (c = sym->components; c; c = c->next)
1621 /* Look for allocatable components. */
1623 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.alloc_comp))
1625 sym->attr.alloc_comp = 1;
1629 /* Look for pointer components. */
1631 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.pointer_comp))
1633 sym->attr.pointer_comp = 1;
1637 /* Look for private components. */
1638 if (sym->component_access == ACCESS_PRIVATE
1639 || c->access == ACCESS_PRIVATE
1640 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.private_comp))
1642 sym->attr.private_comp = 1;
1651 /* Parse an ENUM. */
1660 int seen_enumerator = 0;
1664 push_state (&s, COMP_ENUM, gfc_new_block);
1668 while (compiling_enum)
1670 st = next_statement ();
1678 seen_enumerator = 1;
1679 accept_statement (st);
1684 if (!seen_enumerator)
1686 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1689 accept_statement (st);
1693 gfc_free_enum_history ();
1694 unexpected_statement (st);
1702 /* Parse an interface. We must be able to deal with the possibility
1703 of recursive interfaces. The parse_spec() subroutine is mutually
1704 recursive with parse_interface(). */
1706 static gfc_statement parse_spec (gfc_statement);
1709 parse_interface (void)
1711 gfc_compile_state new_state, current_state;
1712 gfc_symbol *prog_unit, *sym;
1713 gfc_interface_info save;
1714 gfc_state_data s1, s2;
1718 accept_statement (ST_INTERFACE);
1720 current_interface.ns = gfc_current_ns;
1721 save = current_interface;
1723 sym = (current_interface.type == INTERFACE_GENERIC
1724 || current_interface.type == INTERFACE_USER_OP)
1725 ? gfc_new_block : NULL;
1727 push_state (&s1, COMP_INTERFACE, sym);
1728 current_state = COMP_NONE;
1731 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1733 st = next_statement ();
1740 new_state = COMP_SUBROUTINE;
1741 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1742 gfc_new_block->formal, NULL);
1746 new_state = COMP_FUNCTION;
1747 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1748 gfc_new_block->formal, NULL);
1751 case ST_MODULE_PROC: /* The module procedure matcher makes
1752 sure the context is correct. */
1753 accept_statement (st);
1754 gfc_free_namespace (gfc_current_ns);
1757 case ST_END_INTERFACE:
1758 gfc_free_namespace (gfc_current_ns);
1759 gfc_current_ns = current_interface.ns;
1763 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1764 gfc_ascii_statement (st));
1765 reject_statement ();
1766 gfc_free_namespace (gfc_current_ns);
1771 /* Make sure that a generic interface has only subroutines or
1772 functions and that the generic name has the right attribute. */
1773 if (current_interface.type == INTERFACE_GENERIC)
1775 if (current_state == COMP_NONE)
1777 if (new_state == COMP_FUNCTION)
1778 gfc_add_function (&sym->attr, sym->name, NULL);
1779 else if (new_state == COMP_SUBROUTINE)
1780 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1782 current_state = new_state;
1786 if (new_state != current_state)
1788 if (new_state == COMP_SUBROUTINE)
1789 gfc_error ("SUBROUTINE at %C does not belong in a "
1790 "generic function interface");
1792 if (new_state == COMP_FUNCTION)
1793 gfc_error ("FUNCTION at %C does not belong in a "
1794 "generic subroutine interface");
1799 push_state (&s2, new_state, gfc_new_block);
1800 accept_statement (st);
1801 prog_unit = gfc_new_block;
1802 prog_unit->formal_ns = gfc_current_ns;
1803 proc_locus = gfc_current_locus;
1806 /* Read data declaration statements. */
1807 st = parse_spec (ST_NONE);
1809 /* Since the interface block does not permit an IMPLICIT statement,
1810 the default type for the function or the result must be taken
1811 from the formal namespace. */
1812 if (new_state == COMP_FUNCTION)
1814 if (prog_unit->result == prog_unit
1815 && prog_unit->ts.type == BT_UNKNOWN)
1816 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
1817 else if (prog_unit->result != prog_unit
1818 && prog_unit->result->ts.type == BT_UNKNOWN)
1819 gfc_set_default_type (prog_unit->result, 1,
1820 prog_unit->formal_ns);
1823 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1825 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1826 gfc_ascii_statement (st));
1827 reject_statement ();
1831 current_interface = save;
1832 gfc_add_interface (prog_unit);
1835 if (current_interface.ns
1836 && current_interface.ns->proc_name
1837 && strcmp (current_interface.ns->proc_name->name,
1838 prog_unit->name) == 0)
1839 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
1840 "enclosing procedure", prog_unit->name, &proc_locus);
1849 /* Parse a set of specification statements. Returns the statement
1850 that doesn't fit. */
1852 static gfc_statement
1853 parse_spec (gfc_statement st)
1857 verify_st_order (&ss, ST_NONE);
1859 st = next_statement ();
1869 case ST_DATA: /* Not allowed in interfaces */
1870 if (gfc_current_state () == COMP_INTERFACE)
1877 case ST_IMPLICIT_NONE:
1882 case ST_DERIVED_DECL:
1884 if (verify_st_order (&ss, st) == FAILURE)
1886 reject_statement ();
1887 st = next_statement ();
1897 case ST_DERIVED_DECL:
1903 if (gfc_current_state () != COMP_MODULE)
1905 gfc_error ("%s statement must appear in a MODULE",
1906 gfc_ascii_statement (st));
1910 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1912 gfc_error ("%s statement at %C follows another accessibility "
1913 "specification", gfc_ascii_statement (st));
1917 gfc_current_ns->default_access = (st == ST_PUBLIC)
1918 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1922 case ST_STATEMENT_FUNCTION:
1923 if (gfc_current_state () == COMP_MODULE)
1925 unexpected_statement (st);
1933 accept_statement (st);
1934 st = next_statement ();
1938 accept_statement (st);
1940 st = next_statement ();
1951 /* Parse a WHERE block, (not a simple WHERE statement). */
1954 parse_where_block (void)
1956 int seen_empty_else;
1961 accept_statement (ST_WHERE_BLOCK);
1962 top = gfc_state_stack->tail;
1964 push_state (&s, COMP_WHERE, gfc_new_block);
1966 d = add_statement ();
1967 d->expr = top->expr;
1973 seen_empty_else = 0;
1977 st = next_statement ();
1983 case ST_WHERE_BLOCK:
1984 parse_where_block ();
1989 accept_statement (st);
1993 if (seen_empty_else)
1995 gfc_error ("ELSEWHERE statement at %C follows previous "
1996 "unmasked ELSEWHERE");
2000 if (new_st.expr == NULL)
2001 seen_empty_else = 1;
2003 d = new_level (gfc_state_stack->head);
2005 d->expr = new_st.expr;
2007 accept_statement (st);
2012 accept_statement (st);
2016 gfc_error ("Unexpected %s statement in WHERE block at %C",
2017 gfc_ascii_statement (st));
2018 reject_statement ();
2022 while (st != ST_END_WHERE);
2028 /* Parse a FORALL block (not a simple FORALL statement). */
2031 parse_forall_block (void)
2037 accept_statement (ST_FORALL_BLOCK);
2038 top = gfc_state_stack->tail;
2040 push_state (&s, COMP_FORALL, gfc_new_block);
2042 d = add_statement ();
2043 d->op = EXEC_FORALL;
2048 st = next_statement ();
2053 case ST_POINTER_ASSIGNMENT:
2056 accept_statement (st);
2059 case ST_WHERE_BLOCK:
2060 parse_where_block ();
2063 case ST_FORALL_BLOCK:
2064 parse_forall_block ();
2068 accept_statement (st);
2075 gfc_error ("Unexpected %s statement in FORALL block at %C",
2076 gfc_ascii_statement (st));
2078 reject_statement ();
2082 while (st != ST_END_FORALL);
2088 static gfc_statement parse_executable (gfc_statement);
2090 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2093 parse_if_block (void)
2102 accept_statement (ST_IF_BLOCK);
2104 top = gfc_state_stack->tail;
2105 push_state (&s, COMP_IF, gfc_new_block);
2107 new_st.op = EXEC_IF;
2108 d = add_statement ();
2110 d->expr = top->expr;
2116 st = parse_executable (ST_NONE);
2126 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2127 "statement at %L", &else_locus);
2129 reject_statement ();
2133 d = new_level (gfc_state_stack->head);
2135 d->expr = new_st.expr;
2137 accept_statement (st);
2144 gfc_error ("Duplicate ELSE statements at %L and %C",
2146 reject_statement ();
2151 else_locus = gfc_current_locus;
2153 d = new_level (gfc_state_stack->head);
2156 accept_statement (st);
2164 unexpected_statement (st);
2168 while (st != ST_ENDIF);
2171 accept_statement (st);
2175 /* Parse a SELECT block. */
2178 parse_select_block (void)
2184 accept_statement (ST_SELECT_CASE);
2186 cp = gfc_state_stack->tail;
2187 push_state (&s, COMP_SELECT, gfc_new_block);
2189 /* Make sure that the next statement is a CASE or END SELECT. */
2192 st = next_statement ();
2195 if (st == ST_END_SELECT)
2197 /* Empty SELECT CASE is OK. */
2198 accept_statement (st);
2205 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2208 reject_statement ();
2211 /* At this point, we're got a nonempty select block. */
2212 cp = new_level (cp);
2215 accept_statement (st);
2219 st = parse_executable (ST_NONE);
2226 cp = new_level (gfc_state_stack->head);
2228 gfc_clear_new_st ();
2230 accept_statement (st);
2236 /* Can't have an executable statement because of
2237 parse_executable(). */
2239 unexpected_statement (st);
2243 while (st != ST_END_SELECT);
2246 accept_statement (st);
2250 /* Given a symbol, make sure it is not an iteration variable for a DO
2251 statement. This subroutine is called when the symbol is seen in a
2252 context that causes it to become redefined. If the symbol is an
2253 iterator, we generate an error message and return nonzero. */
2256 gfc_check_do_variable (gfc_symtree *st)
2260 for (s=gfc_state_stack; s; s = s->previous)
2261 if (s->do_variable == st)
2263 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2264 "loop beginning at %L", st->name, &s->head->loc);
2272 /* Checks to see if the current statement label closes an enddo.
2273 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2274 an error) if it incorrectly closes an ENDDO. */
2277 check_do_closure (void)
2281 if (gfc_statement_label == NULL)
2284 for (p = gfc_state_stack; p; p = p->previous)
2285 if (p->state == COMP_DO)
2289 return 0; /* No loops to close */
2291 if (p->ext.end_do_label == gfc_statement_label)
2294 if (p == gfc_state_stack)
2297 gfc_error ("End of nonblock DO statement at %C is within another block");
2301 /* At this point, the label doesn't terminate the innermost loop.
2302 Make sure it doesn't terminate another one. */
2303 for (; p; p = p->previous)
2304 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
2306 gfc_error ("End of nonblock DO statement at %C is interwoven "
2307 "with another DO loop");
2315 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2316 handled inside of parse_executable(), because they aren't really
2320 parse_do_block (void)
2327 s.ext.end_do_label = new_st.label;
2329 if (new_st.ext.iterator != NULL)
2330 stree = new_st.ext.iterator->var->symtree;
2334 accept_statement (ST_DO);
2336 top = gfc_state_stack->tail;
2337 push_state (&s, COMP_DO, gfc_new_block);
2339 s.do_variable = stree;
2341 top->block = new_level (top);
2342 top->block->op = EXEC_DO;
2345 st = parse_executable (ST_NONE);
2353 if (s.ext.end_do_label != NULL
2354 && s.ext.end_do_label != gfc_statement_label)
2355 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
2358 if (gfc_statement_label != NULL)
2360 new_st.op = EXEC_NOP;
2365 case ST_IMPLIED_ENDDO:
2366 /* If the do-stmt of this DO construct has a do-construct-name,
2367 the corresponding end-do must be an end-do-stmt (with a matching
2368 name, but in that case we must have seen ST_ENDDO first).
2369 We only complain about this in pedantic mode. */
2370 if (gfc_current_block () != NULL)
2371 gfc_error_now ("named block DO at %L requires matching ENDDO name",
2372 &gfc_current_block()->declared_at);
2377 unexpected_statement (st);
2382 accept_statement (st);
2386 /* Parse the statements of OpenMP do/parallel do. */
2388 static gfc_statement
2389 parse_omp_do (gfc_statement omp_st)
2395 accept_statement (omp_st);
2397 cp = gfc_state_stack->tail;
2398 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2399 np = new_level (cp);
2405 st = next_statement ();
2408 else if (st == ST_DO)
2411 unexpected_statement (st);
2415 if (gfc_statement_label != NULL
2416 && gfc_state_stack->previous != NULL
2417 && gfc_state_stack->previous->state == COMP_DO
2418 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
2426 there should be no !$OMP END DO. */
2428 return ST_IMPLIED_ENDDO;
2431 check_do_closure ();
2434 st = next_statement ();
2435 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
2437 if (new_st.op == EXEC_OMP_END_NOWAIT)
2438 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2440 gcc_assert (new_st.op == EXEC_NOP);
2441 gfc_clear_new_st ();
2442 gfc_commit_symbols ();
2443 gfc_warning_check ();
2444 st = next_statement ();
2450 /* Parse the statements of OpenMP atomic directive. */
2453 parse_omp_atomic (void)
2459 accept_statement (ST_OMP_ATOMIC);
2461 cp = gfc_state_stack->tail;
2462 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2463 np = new_level (cp);
2469 st = next_statement ();
2472 else if (st == ST_ASSIGNMENT)
2475 unexpected_statement (st);
2478 accept_statement (st);
2484 /* Parse the statements of an OpenMP structured block. */
2487 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
2489 gfc_statement st, omp_end_st;
2493 accept_statement (omp_st);
2495 cp = gfc_state_stack->tail;
2496 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2497 np = new_level (cp);
2503 case ST_OMP_PARALLEL:
2504 omp_end_st = ST_OMP_END_PARALLEL;
2506 case ST_OMP_PARALLEL_SECTIONS:
2507 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
2509 case ST_OMP_SECTIONS:
2510 omp_end_st = ST_OMP_END_SECTIONS;
2512 case ST_OMP_ORDERED:
2513 omp_end_st = ST_OMP_END_ORDERED;
2515 case ST_OMP_CRITICAL:
2516 omp_end_st = ST_OMP_END_CRITICAL;
2519 omp_end_st = ST_OMP_END_MASTER;
2522 omp_end_st = ST_OMP_END_SINGLE;
2524 case ST_OMP_WORKSHARE:
2525 omp_end_st = ST_OMP_END_WORKSHARE;
2527 case ST_OMP_PARALLEL_WORKSHARE:
2528 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
2536 if (workshare_stmts_only)
2538 /* Inside of !$omp workshare, only
2541 where statements and constructs
2542 forall statements and constructs
2546 are allowed. For !$omp critical these
2547 restrictions apply recursively. */
2550 st = next_statement ();
2561 accept_statement (st);
2564 case ST_WHERE_BLOCK:
2565 parse_where_block ();
2568 case ST_FORALL_BLOCK:
2569 parse_forall_block ();
2572 case ST_OMP_PARALLEL:
2573 case ST_OMP_PARALLEL_SECTIONS:
2574 parse_omp_structured_block (st, false);
2577 case ST_OMP_PARALLEL_WORKSHARE:
2578 case ST_OMP_CRITICAL:
2579 parse_omp_structured_block (st, true);
2582 case ST_OMP_PARALLEL_DO:
2583 st = parse_omp_do (st);
2587 parse_omp_atomic ();
2598 st = next_statement ();
2602 st = parse_executable (ST_NONE);
2605 else if (st == ST_OMP_SECTION
2606 && (omp_st == ST_OMP_SECTIONS
2607 || omp_st == ST_OMP_PARALLEL_SECTIONS))
2609 np = new_level (np);
2613 else if (st != omp_end_st)
2614 unexpected_statement (st);
2616 while (st != omp_end_st);
2620 case EXEC_OMP_END_NOWAIT:
2621 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2623 case EXEC_OMP_CRITICAL:
2624 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
2625 || (new_st.ext.omp_name != NULL
2626 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
2627 gfc_error ("Name after !$omp critical and !$omp end critical does "
2629 gfc_free ((char *) new_st.ext.omp_name);
2631 case EXEC_OMP_END_SINGLE:
2632 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
2633 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
2634 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
2635 gfc_free_omp_clauses (new_st.ext.omp_clauses);
2643 gfc_clear_new_st ();
2644 gfc_commit_symbols ();
2645 gfc_warning_check ();
2650 /* Accept a series of executable statements. We return the first
2651 statement that doesn't fit to the caller. Any block statements are
2652 passed on to the correct handler, which usually passes the buck
2655 static gfc_statement
2656 parse_executable (gfc_statement st)
2661 st = next_statement ();
2665 close_flag = check_do_closure ();
2670 case ST_END_PROGRAM:
2673 case ST_END_FUNCTION:
2677 case ST_END_SUBROUTINE:
2682 case ST_SELECT_CASE:
2683 gfc_error ("%s statement at %C cannot terminate a non-block "
2684 "DO loop", gfc_ascii_statement (st));
2700 accept_statement (st);
2701 if (close_flag == 1)
2702 return ST_IMPLIED_ENDDO;
2709 case ST_SELECT_CASE:
2710 parse_select_block ();
2715 if (check_do_closure () == 1)
2716 return ST_IMPLIED_ENDDO;
2719 case ST_WHERE_BLOCK:
2720 parse_where_block ();
2723 case ST_FORALL_BLOCK:
2724 parse_forall_block ();
2727 case ST_OMP_PARALLEL:
2728 case ST_OMP_PARALLEL_SECTIONS:
2729 case ST_OMP_SECTIONS:
2730 case ST_OMP_ORDERED:
2731 case ST_OMP_CRITICAL:
2734 parse_omp_structured_block (st, false);
2737 case ST_OMP_WORKSHARE:
2738 case ST_OMP_PARALLEL_WORKSHARE:
2739 parse_omp_structured_block (st, true);
2743 case ST_OMP_PARALLEL_DO:
2744 st = parse_omp_do (st);
2745 if (st == ST_IMPLIED_ENDDO)
2750 parse_omp_atomic ();
2757 st = next_statement ();
2762 /* Parse a series of contained program units. */
2764 static void parse_progunit (gfc_statement);
2767 /* Fix the symbols for sibling functions. These are incorrectly added to
2768 the child namespace as the parser didn't know about this procedure. */
2771 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
2775 gfc_symbol *old_sym;
2777 sym->attr.referenced = 1;
2778 for (ns = siblings; ns; ns = ns->sibling)
2780 gfc_find_sym_tree (sym->name, ns, 0, &st);
2782 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
2785 old_sym = st->n.sym;
2786 if ((old_sym->attr.flavor == FL_PROCEDURE
2787 || old_sym->ts.type == BT_UNKNOWN)
2788 && old_sym->ns == ns
2789 && !old_sym->attr.contained
2790 && old_sym->attr.flavor != FL_NAMELIST)
2792 /* Replace it with the symbol from the parent namespace. */
2796 /* Free the old (local) symbol. */
2798 if (old_sym->refs == 0)
2799 gfc_free_symbol (old_sym);
2802 /* Do the same for any contained procedures. */
2803 gfc_fixup_sibling_symbols (sym, ns->contained);
2808 parse_contained (int module)
2810 gfc_namespace *ns, *parent_ns, *tmp;
2811 gfc_state_data s1, s2;
2815 int contains_statements = 0;
2818 push_state (&s1, COMP_CONTAINS, NULL);
2819 parent_ns = gfc_current_ns;
2823 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2825 gfc_current_ns->sibling = parent_ns->contained;
2826 parent_ns->contained = gfc_current_ns;
2829 /* Process the next available statement. We come here if we got an error
2830 and rejected the last statement. */
2831 st = next_statement ();
2840 contains_statements = 1;
2841 accept_statement (st);
2844 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2847 /* For internal procedures, create/update the symbol in the
2848 parent namespace. */
2852 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2853 gfc_error ("Contained procedure '%s' at %C is already "
2854 "ambiguous", gfc_new_block->name);
2857 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2858 &gfc_new_block->declared_at) ==
2861 if (st == ST_FUNCTION)
2862 gfc_add_function (&sym->attr, sym->name,
2863 &gfc_new_block->declared_at);
2865 gfc_add_subroutine (&sym->attr, sym->name,
2866 &gfc_new_block->declared_at);
2870 gfc_commit_symbols ();
2873 sym = gfc_new_block;
2875 /* Mark this as a contained function, so it isn't replaced
2876 by other module functions. */
2877 sym->attr.contained = 1;
2878 sym->attr.referenced = 1;
2880 parse_progunit (ST_NONE);
2882 /* Fix up any sibling functions that refer to this one. */
2883 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2884 /* Or refer to any of its alternate entry points. */
2885 for (el = gfc_current_ns->entries; el; el = el->next)
2886 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2888 gfc_current_ns->code = s2.head;
2889 gfc_current_ns = parent_ns;
2894 /* These statements are associated with the end of the host unit. */
2895 case ST_END_FUNCTION:
2897 case ST_END_PROGRAM:
2898 case ST_END_SUBROUTINE:
2899 accept_statement (st);
2903 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2904 gfc_ascii_statement (st));
2905 reject_statement ();
2911 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2912 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2914 /* The first namespace in the list is guaranteed to not have
2915 anything (worthwhile) in it. */
2916 tmp = gfc_current_ns;
2917 gfc_current_ns = parent_ns;
2918 if (seen_error && tmp->refs > 1)
2919 gfc_free_namespace (tmp);
2921 ns = gfc_current_ns->contained;
2922 gfc_current_ns->contained = ns->sibling;
2923 gfc_free_namespace (ns);
2926 if (!contains_statements)
2927 /* This is valid in Fortran 2008. */
2928 gfc_notify_std (GFC_STD_GNU, "Extension: CONTAINS statement without "
2929 "FUNCTION or SUBROUTINE statement at %C");
2933 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2936 parse_progunit (gfc_statement st)
2941 st = parse_spec (st);
2951 accept_statement (st);
2958 if (gfc_current_state () == COMP_FUNCTION)
2959 gfc_check_function_type (gfc_current_ns);
2964 st = parse_executable (st);
2975 accept_statement (st);
2982 unexpected_statement (st);
2983 reject_statement ();
2984 st = next_statement ();
2990 for (p = gfc_state_stack; p; p = p->previous)
2991 if (p->state == COMP_CONTAINS)
2994 if (gfc_find_state (COMP_MODULE) == SUCCESS)
2999 gfc_error ("CONTAINS statement at %C is already in a contained "
3001 st = next_statement ();
3005 parse_contained (0);
3008 gfc_current_ns->code = gfc_state_stack->head;
3012 /* Come here to complain about a global symbol already in use as
3016 global_used (gfc_gsymbol *sym, locus *where)
3021 where = &gfc_current_locus;
3031 case GSYM_SUBROUTINE:
3032 name = "SUBROUTINE";
3037 case GSYM_BLOCK_DATA:
3038 name = "BLOCK DATA";
3044 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
3048 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
3049 sym->name, where, name, &sym->where);
3053 /* Parse a block data program unit. */
3056 parse_block_data (void)
3059 static locus blank_locus;
3060 static int blank_block=0;
3063 gfc_current_ns->proc_name = gfc_new_block;
3064 gfc_current_ns->is_block_data = 1;
3066 if (gfc_new_block == NULL)
3069 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3070 "prior BLOCK DATA at %L", &blank_locus);
3074 blank_locus = gfc_current_locus;
3079 s = gfc_get_gsymbol (gfc_new_block->name);
3081 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
3082 global_used(s, NULL);
3085 s->type = GSYM_BLOCK_DATA;
3086 s->where = gfc_current_locus;
3091 st = parse_spec (ST_NONE);
3093 while (st != ST_END_BLOCK_DATA)
3095 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3096 gfc_ascii_statement (st));
3097 reject_statement ();
3098 st = next_statement ();
3103 /* Parse a module subprogram. */
3111 s = gfc_get_gsymbol (gfc_new_block->name);
3112 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
3113 global_used(s, NULL);
3116 s->type = GSYM_MODULE;
3117 s->where = gfc_current_locus;
3121 st = parse_spec (ST_NONE);
3130 parse_contained (1);
3134 accept_statement (st);
3138 gfc_error ("Unexpected %s statement in MODULE at %C",
3139 gfc_ascii_statement (st));
3141 reject_statement ();
3142 st = next_statement ();
3148 /* Add a procedure name to the global symbol table. */
3151 add_global_procedure (int sub)
3155 s = gfc_get_gsymbol(gfc_new_block->name);
3158 || (s->type != GSYM_UNKNOWN
3159 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3160 global_used(s, NULL);
3163 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3164 s->where = gfc_current_locus;
3170 /* Add a program to the global symbol table. */
3173 add_global_program (void)
3177 if (gfc_new_block == NULL)
3179 s = gfc_get_gsymbol (gfc_new_block->name);
3181 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
3182 global_used(s, NULL);
3185 s->type = GSYM_PROGRAM;
3186 s->where = gfc_current_locus;
3192 /* Top level parser. */
3195 gfc_parse_file (void)
3197 int seen_program, errors_before, errors;
3198 gfc_state_data top, s;
3202 top.state = COMP_NONE;
3204 top.previous = NULL;
3205 top.head = top.tail = NULL;
3206 top.do_variable = NULL;
3208 gfc_state_stack = ⊤
3210 gfc_clear_new_st ();
3212 gfc_statement_label = NULL;
3214 if (setjmp (eof_buf))
3215 return FAILURE; /* Come here on unexpected EOF */
3219 /* Exit early for empty files. */
3225 st = next_statement ();
3234 goto duplicate_main;
3236 prog_locus = gfc_current_locus;
3238 push_state (&s, COMP_PROGRAM, gfc_new_block);
3239 main_program_symbol(gfc_current_ns);
3240 accept_statement (st);
3241 add_global_program ();
3242 parse_progunit (ST_NONE);
3246 add_global_procedure (1);
3247 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
3248 accept_statement (st);
3249 parse_progunit (ST_NONE);
3253 add_global_procedure (0);
3254 push_state (&s, COMP_FUNCTION, gfc_new_block);
3255 accept_statement (st);
3256 parse_progunit (ST_NONE);
3260 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
3261 accept_statement (st);
3262 parse_block_data ();
3266 push_state (&s, COMP_MODULE, gfc_new_block);
3267 accept_statement (st);
3269 gfc_get_errors (NULL, &errors_before);
3273 /* Anything else starts a nameless main program block. */
3276 goto duplicate_main;
3278 prog_locus = gfc_current_locus;
3280 push_state (&s, COMP_PROGRAM, gfc_new_block);
3281 main_program_symbol (gfc_current_ns);
3282 parse_progunit (st);
3286 gfc_current_ns->code = s.head;
3288 gfc_resolve (gfc_current_ns);
3290 /* Dump the parse tree if requested. */
3291 if (gfc_option.verbose)
3292 gfc_show_namespace (gfc_current_ns);
3294 gfc_get_errors (NULL, &errors);
3295 if (s.state == COMP_MODULE)
3297 gfc_dump_module (s.sym->name, errors_before == errors);
3299 gfc_generate_module_code (gfc_current_ns);
3304 gfc_generate_code (gfc_current_ns);
3315 /* If we see a duplicate main program, shut down. If the second
3316 instance is an implied main program, ie data decls or executable
3317 statements, we're in for lots of errors. */
3318 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
3319 reject_statement ();