2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 /* This is a specialist version of decode_statement that is used
90 for the specification statements in a function, whose
91 characteristics are deferred into the specification statements.
92 eg.: INTEGER (king = mykind) foo ()
93 USE mymodule, ONLY mykind.....
94 The KIND parameter needs a return after USE or IMPORT, whereas
95 derived type declarations can occur anywhere, up the executable
96 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
97 out of the correct kind of specification statements. */
99 decode_specification_statement (void)
105 if (gfc_match_eos () == MATCH_YES)
108 old_locus = gfc_current_locus;
110 match ("import", gfc_match_import, ST_IMPORT);
111 match ("use", gfc_match_use, ST_USE);
113 if (gfc_current_block ()->ts.type != BT_DERIVED)
116 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
117 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
118 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
120 /* General statement matching: Instead of testing every possible
121 statement, we eliminate most possibilities by peeking at the
124 c = gfc_peek_char ();
129 match ("abstract% interface", gfc_match_abstract_interface,
134 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
141 match ("data", gfc_match_data, ST_DATA);
142 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
146 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
147 match ("entry% ", gfc_match_entry, ST_ENTRY);
148 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
149 match ("external", gfc_match_external, ST_ATTR_DECL);
153 match ("format", gfc_match_format, ST_FORMAT);
160 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
161 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
162 match ("interface", gfc_match_interface, ST_INTERFACE);
163 match ("intent", gfc_match_intent, ST_ATTR_DECL);
164 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
171 match ("namelist", gfc_match_namelist, ST_NAMELIST);
175 match ("optional", gfc_match_optional, ST_ATTR_DECL);
179 match ("parameter", gfc_match_parameter, ST_PARAMETER);
180 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
181 if (gfc_match_private (&st) == MATCH_YES)
183 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
184 if (gfc_match_public (&st) == MATCH_YES)
186 match ("protected", gfc_match_protected, ST_ATTR_DECL);
193 match ("save", gfc_match_save, ST_ATTR_DECL);
197 match ("target", gfc_match_target, ST_ATTR_DECL);
198 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
205 match ("value", gfc_match_value, ST_ATTR_DECL);
206 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
213 /* This is not a specification statement. See if any of the matchers
214 has stored an error message of some sort. */
218 gfc_buffer_error (0);
219 gfc_current_locus = old_locus;
221 return ST_GET_FCN_CHARACTERISTICS;
225 /* This is the primary 'decode_statement'. */
227 decode_statement (void)
238 gfc_clear_error (); /* Clear any pending errors. */
239 gfc_clear_warning (); /* Clear any pending warnings. */
241 gfc_matching_function = false;
243 if (gfc_match_eos () == MATCH_YES)
246 if (gfc_current_state () == COMP_FUNCTION
247 && gfc_current_block ()->result->ts.kind == -1)
248 return decode_specification_statement ();
250 old_locus = gfc_current_locus;
252 /* Try matching a data declaration or function declaration. The
253 input "REALFUNCTIONA(N)" can mean several things in different
254 contexts, so it (and its relatives) get special treatment. */
256 if (gfc_current_state () == COMP_NONE
257 || gfc_current_state () == COMP_INTERFACE
258 || gfc_current_state () == COMP_CONTAINS)
260 gfc_matching_function = true;
261 m = gfc_match_function_decl ();
264 else if (m == MATCH_ERROR)
268 gfc_current_locus = old_locus;
270 gfc_matching_function = false;
273 /* Match statements whose error messages are meant to be overwritten
274 by something better. */
276 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
277 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
278 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
280 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
281 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
283 /* Try to match a subroutine statement, which has the same optional
284 prefixes that functions can have. */
286 if (gfc_match_subroutine () == MATCH_YES)
287 return ST_SUBROUTINE;
289 gfc_current_locus = old_locus;
291 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
292 might begin with a block label. The match functions for these
293 statements are unusual in that their keyword is not seen before
294 the matcher is called. */
296 if (gfc_match_if (&st) == MATCH_YES)
299 gfc_current_locus = old_locus;
301 if (gfc_match_where (&st) == MATCH_YES)
304 gfc_current_locus = old_locus;
306 if (gfc_match_forall (&st) == MATCH_YES)
309 gfc_current_locus = old_locus;
311 match (NULL, gfc_match_do, ST_DO);
312 match (NULL, gfc_match_select, ST_SELECT_CASE);
314 /* General statement matching: Instead of testing every possible
315 statement, we eliminate most possibilities by peeking at the
318 c = gfc_peek_char ();
323 match ("abstract% interface", gfc_match_abstract_interface,
325 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
326 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
327 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
331 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
332 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
333 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
337 match ("call", gfc_match_call, ST_CALL);
338 match ("close", gfc_match_close, ST_CLOSE);
339 match ("continue", gfc_match_continue, ST_CONTINUE);
340 match ("cycle", gfc_match_cycle, ST_CYCLE);
341 match ("case", gfc_match_case, ST_CASE);
342 match ("common", gfc_match_common, ST_COMMON);
343 match ("contains", gfc_match_eos, ST_CONTAINS);
347 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
348 match ("data", gfc_match_data, ST_DATA);
349 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
353 match ("end file", gfc_match_endfile, ST_END_FILE);
354 match ("exit", gfc_match_exit, ST_EXIT);
355 match ("else", gfc_match_else, ST_ELSE);
356 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
357 match ("else if", gfc_match_elseif, ST_ELSEIF);
358 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
360 if (gfc_match_end (&st) == MATCH_YES)
363 match ("entry% ", gfc_match_entry, ST_ENTRY);
364 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
365 match ("external", gfc_match_external, ST_ATTR_DECL);
369 match ("flush", gfc_match_flush, ST_FLUSH);
370 match ("format", gfc_match_format, ST_FORMAT);
374 match ("go to", gfc_match_goto, ST_GOTO);
378 match ("inquire", gfc_match_inquire, ST_INQUIRE);
379 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
380 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
381 match ("import", gfc_match_import, ST_IMPORT);
382 match ("interface", gfc_match_interface, ST_INTERFACE);
383 match ("intent", gfc_match_intent, ST_ATTR_DECL);
384 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
388 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
389 match ("module", gfc_match_module, ST_MODULE);
393 match ("nullify", gfc_match_nullify, ST_NULLIFY);
394 match ("namelist", gfc_match_namelist, ST_NAMELIST);
398 match ("open", gfc_match_open, ST_OPEN);
399 match ("optional", gfc_match_optional, ST_ATTR_DECL);
403 match ("print", gfc_match_print, ST_WRITE);
404 match ("parameter", gfc_match_parameter, ST_PARAMETER);
405 match ("pause", gfc_match_pause, ST_PAUSE);
406 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
407 if (gfc_match_private (&st) == MATCH_YES)
409 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
410 match ("program", gfc_match_program, ST_PROGRAM);
411 if (gfc_match_public (&st) == MATCH_YES)
413 match ("protected", gfc_match_protected, ST_ATTR_DECL);
417 match ("read", gfc_match_read, ST_READ);
418 match ("return", gfc_match_return, ST_RETURN);
419 match ("rewind", gfc_match_rewind, ST_REWIND);
423 match ("sequence", gfc_match_eos, ST_SEQUENCE);
424 match ("stop", gfc_match_stop, ST_STOP);
425 match ("save", gfc_match_save, ST_ATTR_DECL);
429 match ("target", gfc_match_target, ST_ATTR_DECL);
430 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
434 match ("use", gfc_match_use, ST_USE);
438 match ("value", gfc_match_value, ST_ATTR_DECL);
439 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
443 match ("wait", gfc_match_wait, ST_WAIT);
444 match ("write", gfc_match_write, ST_WRITE);
448 /* All else has failed, so give up. See if any of the matchers has
449 stored an error message of some sort. */
451 if (gfc_error_check () == 0)
452 gfc_error_now ("Unclassifiable statement at %C");
456 gfc_error_recovery ();
462 decode_omp_directive (void)
471 gfc_clear_error (); /* Clear any pending errors. */
472 gfc_clear_warning (); /* Clear any pending warnings. */
476 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
477 "or ELEMENTAL procedures");
478 gfc_error_recovery ();
482 old_locus = gfc_current_locus;
484 /* General OpenMP directive matching: Instead of testing every possible
485 statement, we eliminate most possibilities by peeking at the
488 c = gfc_peek_char ();
493 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
496 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
499 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
502 match ("do", gfc_match_omp_do, ST_OMP_DO);
505 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
506 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
507 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
508 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
509 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
510 match ("end parallel sections", gfc_match_omp_eos,
511 ST_OMP_END_PARALLEL_SECTIONS);
512 match ("end parallel workshare", gfc_match_omp_eos,
513 ST_OMP_END_PARALLEL_WORKSHARE);
514 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
515 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
516 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
517 match ("end workshare", gfc_match_omp_end_nowait,
518 ST_OMP_END_WORKSHARE);
521 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
524 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
527 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
530 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
531 match ("parallel sections", gfc_match_omp_parallel_sections,
532 ST_OMP_PARALLEL_SECTIONS);
533 match ("parallel workshare", gfc_match_omp_parallel_workshare,
534 ST_OMP_PARALLEL_WORKSHARE);
535 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
538 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
539 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
540 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
543 match ("threadprivate", gfc_match_omp_threadprivate,
544 ST_OMP_THREADPRIVATE);
546 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
550 /* All else has failed, so give up. See if any of the matchers has
551 stored an error message of some sort. */
553 if (gfc_error_check () == 0)
554 gfc_error_now ("Unclassifiable OpenMP directive at %C");
558 gfc_error_recovery ();
566 /* Get the next statement in free form source. */
572 int c, d, cnt, at_bol;
574 at_bol = gfc_at_bol ();
575 gfc_gobble_whitespace ();
577 c = gfc_peek_char ();
581 /* Found a statement label? */
582 m = gfc_match_st_label (&gfc_statement_label);
584 d = gfc_peek_char ();
585 if (m != MATCH_YES || !gfc_is_whitespace (d))
587 gfc_match_small_literal_int (&c, &cnt);
590 gfc_error_now ("Too many digits in statement label at %C");
593 gfc_error_now ("Zero is not a valid statement label at %C");
596 c = gfc_next_char ();
599 if (!gfc_is_whitespace (c))
600 gfc_error_now ("Non-numeric character in statement label at %C");
606 label_locus = gfc_current_locus;
608 gfc_gobble_whitespace ();
610 if (at_bol && gfc_peek_char () == ';')
612 gfc_error_now ("Semicolon at %C needs to be preceded by "
614 gfc_next_char (); /* Eat up the semicolon. */
618 if (gfc_match_eos () == MATCH_YES)
620 gfc_warning_now ("Ignoring statement label in empty statement "
622 gfc_free_st_label (gfc_statement_label);
623 gfc_statement_label = NULL;
630 /* Comments have already been skipped by the time we get here,
631 except for OpenMP directives. */
632 if (gfc_option.flag_openmp)
636 c = gfc_next_char ();
637 for (i = 0; i < 5; i++, c = gfc_next_char ())
638 gcc_assert (c == "!$omp"[i]);
640 gcc_assert (c == ' ');
641 gfc_gobble_whitespace ();
642 return decode_omp_directive ();
646 if (at_bol && c == ';')
648 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
649 gfc_next_char (); /* Eat up the semicolon. */
653 return decode_statement ();
657 /* Get the next statement in fixed-form source. */
662 int label, digit_flag, i;
667 return decode_statement ();
669 /* Skip past the current label field, parsing a statement label if
670 one is there. This is a weird number parser, since the number is
671 contained within five columns and can have any kind of embedded
672 spaces. We also check for characters that make the rest of the
678 for (i = 0; i < 5; i++)
680 c = gfc_next_char_literal (0);
697 label = label * 10 + c - '0';
698 label_locus = gfc_current_locus;
702 /* Comments have already been skipped by the time we get
703 here, except for OpenMP directives. */
705 if (gfc_option.flag_openmp)
707 for (i = 0; i < 5; i++, c = gfc_next_char_literal (0))
708 gcc_assert (TOLOWER (c) == "*$omp"[i]);
710 if (c != ' ' && c != '0')
712 gfc_buffer_error (0);
713 gfc_error ("Bad continuation line at %C");
717 return decode_omp_directive ();
721 /* Comments have already been skipped by the time we get
722 here so don't bother checking for them. */
725 gfc_buffer_error (0);
726 gfc_error ("Non-numeric character in statement label at %C");
734 gfc_warning_now ("Zero is not a valid statement label at %C");
737 /* We've found a valid statement label. */
738 gfc_statement_label = gfc_get_st_label (label);
742 /* Since this line starts a statement, it cannot be a continuation
743 of a previous statement. If we see something here besides a
744 space or zero, it must be a bad continuation line. */
746 c = gfc_next_char_literal (0);
750 if (c != ' ' && c != '0')
752 gfc_buffer_error (0);
753 gfc_error ("Bad continuation line at %C");
757 /* Now that we've taken care of the statement label columns, we have
758 to make sure that the first nonblank character is not a '!'. If
759 it is, the rest of the line is a comment. */
763 loc = gfc_current_locus;
764 c = gfc_next_char_literal (0);
766 while (gfc_is_whitespace (c));
770 gfc_current_locus = loc;
774 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
778 if (gfc_match_eos () == MATCH_YES)
781 /* At this point, we've got a nonblank statement to parse. */
782 return decode_statement ();
786 gfc_warning ("Ignoring statement label in empty statement at %C");
792 /* Return the next non-ST_NONE statement to the caller. We also worry
793 about including files and the ends of include files at this stage. */
796 next_statement (void)
800 gfc_new_block = NULL;
804 gfc_statement_label = NULL;
805 gfc_buffer_error (1);
809 if ((gfc_option.warn_line_truncation || gfc_current_form == FORM_FREE)
810 && gfc_current_locus.lb
811 && gfc_current_locus.lb->truncated)
812 gfc_warning_now ("Line truncated at %C");
817 gfc_skip_comments ();
825 if (gfc_define_undef_line ())
828 old_locus = gfc_current_locus;
830 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
836 gfc_buffer_error (0);
838 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
840 gfc_free_st_label (gfc_statement_label);
841 gfc_statement_label = NULL;
842 gfc_current_locus = old_locus;
846 check_statement_label (st);
852 /****************************** Parser ***********************************/
854 /* The parser subroutines are of type 'try' that fail if the file ends
857 /* Macros that expand to case-labels for various classes of
858 statements. Start with executable statements that directly do
861 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
862 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
863 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
864 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
865 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
866 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
867 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
868 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
871 /* Statements that mark other executable statements. */
873 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
874 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
875 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
876 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
877 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
878 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
880 /* Declaration statements */
882 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
883 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
884 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
887 /* Block end statements. Errors associated with interchanging these
888 are detected in gfc_match_end(). */
890 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
891 case ST_END_PROGRAM: case ST_END_SUBROUTINE
894 /* Push a new state onto the stack. */
897 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
899 p->state = new_state;
900 p->previous = gfc_state_stack;
902 p->head = p->tail = NULL;
903 p->do_variable = NULL;
908 /* Pop the current state. */
912 gfc_state_stack = gfc_state_stack->previous;
916 /* Try to find the given state in the state stack. */
919 gfc_find_state (gfc_compile_state state)
923 for (p = gfc_state_stack; p; p = p->previous)
924 if (p->state == state)
927 return (p == NULL) ? FAILURE : SUCCESS;
931 /* Starts a new level in the statement list. */
934 new_level (gfc_code *q)
938 p = q->block = gfc_get_code ();
940 gfc_state_stack->head = gfc_state_stack->tail = p;
946 /* Add the current new_st code structure and adds it to the current
947 program unit. As a side-effect, it zeroes the new_st. */
957 p->loc = gfc_current_locus;
959 if (gfc_state_stack->head == NULL)
960 gfc_state_stack->head = p;
962 gfc_state_stack->tail->next = p;
964 while (p->next != NULL)
967 gfc_state_stack->tail = p;
975 /* Frees everything associated with the current statement. */
978 undo_new_statement (void)
980 gfc_free_statements (new_st.block);
981 gfc_free_statements (new_st.next);
982 gfc_free_statement (&new_st);
987 /* If the current statement has a statement label, make sure that it
988 is allowed to, or should have one. */
991 check_statement_label (gfc_statement st)
995 if (gfc_statement_label == NULL)
998 gfc_error ("FORMAT statement at %L does not have a statement label",
1005 case ST_END_PROGRAM:
1006 case ST_END_FUNCTION:
1007 case ST_END_SUBROUTINE:
1013 type = ST_LABEL_TARGET;
1017 type = ST_LABEL_FORMAT;
1020 /* Statement labels are not restricted from appearing on a
1021 particular line. However, there are plenty of situations
1022 where the resulting label can't be referenced. */
1025 type = ST_LABEL_BAD_TARGET;
1029 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1031 new_st.here = gfc_statement_label;
1035 /* Figures out what the enclosing program unit is. This will be a
1036 function, subroutine, program, block data or module. */
1039 gfc_enclosing_unit (gfc_compile_state * result)
1043 for (p = gfc_state_stack; p; p = p->previous)
1044 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1045 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
1046 || p->state == COMP_PROGRAM)
1055 *result = COMP_PROGRAM;
1060 /* Translate a statement enum to a string. */
1063 gfc_ascii_statement (gfc_statement st)
1069 case ST_ARITHMETIC_IF:
1070 p = _("arithmetic IF");
1076 p = _("attribute declaration");
1106 p = _("data declaration");
1114 case ST_DERIVED_DECL:
1115 p = _("derived type declaration");
1129 case ST_END_BLOCK_DATA:
1130 p = "END BLOCK DATA";
1141 case ST_END_FUNCTION:
1147 case ST_END_INTERFACE:
1148 p = "END INTERFACE";
1153 case ST_END_PROGRAM:
1159 case ST_END_SUBROUTINE:
1160 p = "END SUBROUTINE";
1171 case ST_EQUIVALENCE:
1180 case ST_FORALL_BLOCK: /* Fall through */
1199 case ST_IMPLICIT_NONE:
1200 p = "IMPLICIT NONE";
1202 case ST_IMPLIED_ENDDO:
1203 p = _("implied END DO");
1229 case ST_MODULE_PROC:
1230 p = "MODULE PROCEDURE";
1268 case ST_WHERE_BLOCK: /* Fall through */
1279 p = _("assignment");
1281 case ST_POINTER_ASSIGNMENT:
1282 p = _("pointer assignment");
1284 case ST_SELECT_CASE:
1293 case ST_STATEMENT_FUNCTION:
1294 p = "STATEMENT FUNCTION";
1296 case ST_LABEL_ASSIGNMENT:
1297 p = "LABEL ASSIGNMENT";
1300 p = "ENUM DEFINITION";
1303 p = "ENUMERATOR DEFINITION";
1311 case ST_OMP_BARRIER:
1312 p = "!$OMP BARRIER";
1314 case ST_OMP_CRITICAL:
1315 p = "!$OMP CRITICAL";
1320 case ST_OMP_END_CRITICAL:
1321 p = "!$OMP END CRITICAL";
1326 case ST_OMP_END_MASTER:
1327 p = "!$OMP END MASTER";
1329 case ST_OMP_END_ORDERED:
1330 p = "!$OMP END ORDERED";
1332 case ST_OMP_END_PARALLEL:
1333 p = "!$OMP END PARALLEL";
1335 case ST_OMP_END_PARALLEL_DO:
1336 p = "!$OMP END PARALLEL DO";
1338 case ST_OMP_END_PARALLEL_SECTIONS:
1339 p = "!$OMP END PARALLEL SECTIONS";
1341 case ST_OMP_END_PARALLEL_WORKSHARE:
1342 p = "!$OMP END PARALLEL WORKSHARE";
1344 case ST_OMP_END_SECTIONS:
1345 p = "!$OMP END SECTIONS";
1347 case ST_OMP_END_SINGLE:
1348 p = "!$OMP END SINGLE";
1350 case ST_OMP_END_WORKSHARE:
1351 p = "!$OMP END WORKSHARE";
1359 case ST_OMP_ORDERED:
1360 p = "!$OMP ORDERED";
1362 case ST_OMP_PARALLEL:
1363 p = "!$OMP PARALLEL";
1365 case ST_OMP_PARALLEL_DO:
1366 p = "!$OMP PARALLEL DO";
1368 case ST_OMP_PARALLEL_SECTIONS:
1369 p = "!$OMP PARALLEL SECTIONS";
1371 case ST_OMP_PARALLEL_WORKSHARE:
1372 p = "!$OMP PARALLEL WORKSHARE";
1374 case ST_OMP_SECTIONS:
1375 p = "!$OMP SECTIONS";
1377 case ST_OMP_SECTION:
1378 p = "!$OMP SECTION";
1383 case ST_OMP_THREADPRIVATE:
1384 p = "!$OMP THREADPRIVATE";
1386 case ST_OMP_WORKSHARE:
1387 p = "!$OMP WORKSHARE";
1390 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1397 /* Create a symbol for the main program and assign it to ns->proc_name. */
1400 main_program_symbol (gfc_namespace *ns, const char *name)
1402 gfc_symbol *main_program;
1403 symbol_attribute attr;
1405 gfc_get_symbol (name, ns, &main_program);
1406 gfc_clear_attr (&attr);
1407 attr.flavor = FL_PROGRAM;
1408 attr.proc = PROC_UNKNOWN;
1409 attr.subroutine = 1;
1410 attr.access = ACCESS_PUBLIC;
1411 attr.is_main_program = 1;
1412 main_program->attr = attr;
1413 main_program->declared_at = gfc_current_locus;
1414 ns->proc_name = main_program;
1415 gfc_commit_symbols ();
1419 /* Do whatever is necessary to accept the last statement. */
1422 accept_statement (gfc_statement st)
1430 case ST_IMPLICIT_NONE:
1431 gfc_set_implicit_none ();
1440 gfc_current_ns->proc_name = gfc_new_block;
1443 /* If the statement is the end of a block, lay down a special code
1444 that allows a branch to the end of the block from within the
1449 if (gfc_statement_label != NULL)
1451 new_st.op = EXEC_NOP;
1457 /* The end-of-program unit statements do not get the special
1458 marker and require a statement of some sort if they are a
1461 case ST_END_PROGRAM:
1462 case ST_END_FUNCTION:
1463 case ST_END_SUBROUTINE:
1464 if (gfc_statement_label != NULL)
1466 new_st.op = EXEC_RETURN;
1482 gfc_commit_symbols ();
1483 gfc_warning_check ();
1484 gfc_clear_new_st ();
1488 /* Undo anything tentative that has been built for the current
1492 reject_statement (void)
1494 gfc_new_block = NULL;
1495 gfc_undo_symbols ();
1496 gfc_clear_warning ();
1497 undo_new_statement ();
1501 /* Generic complaint about an out of order statement. We also do
1502 whatever is necessary to clean up. */
1505 unexpected_statement (gfc_statement st)
1507 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1509 reject_statement ();
1513 /* Given the next statement seen by the matcher, make sure that it is
1514 in proper order with the last. This subroutine is initialized by
1515 calling it with an argument of ST_NONE. If there is a problem, we
1516 issue an error and return FAILURE. Otherwise we return SUCCESS.
1518 Individual parsers need to verify that the statements seen are
1519 valid before calling here, ie ENTRY statements are not allowed in
1520 INTERFACE blocks. The following diagram is taken from the standard:
1522 +---------------------------------------+
1523 | program subroutine function module |
1524 +---------------------------------------+
1526 +---------------------------------------+
1528 +---------------------------------------+
1530 | +-----------+------------------+
1531 | | parameter | implicit |
1532 | +-----------+------------------+
1533 | format | | derived type |
1534 | entry | parameter | interface |
1535 | | data | specification |
1536 | | | statement func |
1537 | +-----------+------------------+
1538 | | data | executable |
1539 +--------+-----------+------------------+
1541 +---------------------------------------+
1542 | internal module/subprogram |
1543 +---------------------------------------+
1545 +---------------------------------------+
1552 { ORDER_START, ORDER_USE, ORDER_IMPORT, ORDER_IMPLICIT_NONE,
1553 ORDER_IMPLICIT, ORDER_SPEC, ORDER_EXEC
1556 gfc_statement last_statement;
1562 verify_st_order (st_state *p, gfc_statement st)
1568 p->state = ORDER_START;
1572 if (p->state > ORDER_USE)
1574 p->state = ORDER_USE;
1578 if (p->state > ORDER_IMPORT)
1580 p->state = ORDER_IMPORT;
1583 case ST_IMPLICIT_NONE:
1584 if (p->state > ORDER_IMPLICIT_NONE)
1587 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1588 statement disqualifies a USE but not an IMPLICIT NONE.
1589 Duplicate IMPLICIT NONEs are caught when the implicit types
1592 p->state = ORDER_IMPLICIT_NONE;
1596 if (p->state > ORDER_IMPLICIT)
1598 p->state = ORDER_IMPLICIT;
1603 if (p->state < ORDER_IMPLICIT_NONE)
1604 p->state = ORDER_IMPLICIT_NONE;
1608 if (p->state >= ORDER_EXEC)
1610 if (p->state < ORDER_IMPLICIT)
1611 p->state = ORDER_IMPLICIT;
1615 if (p->state < ORDER_SPEC)
1616 p->state = ORDER_SPEC;
1621 case ST_DERIVED_DECL:
1623 if (p->state >= ORDER_EXEC)
1625 if (p->state < ORDER_SPEC)
1626 p->state = ORDER_SPEC;
1631 if (p->state < ORDER_EXEC)
1632 p->state = ORDER_EXEC;
1636 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1637 gfc_ascii_statement (st));
1640 /* All is well, record the statement in case we need it next time. */
1641 p->where = gfc_current_locus;
1642 p->last_statement = st;
1646 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1647 gfc_ascii_statement (st),
1648 gfc_ascii_statement (p->last_statement), &p->where);
1654 /* Handle an unexpected end of file. This is a show-stopper... */
1656 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1659 unexpected_eof (void)
1663 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1665 /* Memory cleanup. Move to "second to last". */
1666 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1669 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1672 longjmp (eof_buf, 1);
1676 /* Parse a derived type. */
1679 parse_derived (void)
1681 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1684 gfc_symbol *derived_sym = NULL;
1690 accept_statement (ST_DERIVED_DECL);
1691 push_state (&s, COMP_DERIVED, gfc_new_block);
1693 gfc_new_block->component_access = ACCESS_PUBLIC;
1700 while (compiling_type)
1702 st = next_statement ();
1710 accept_statement (st);
1718 && (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Derived type "
1719 "definition at %C without components")
1723 accept_statement (ST_END_TYPE);
1727 if (gfc_find_state (COMP_MODULE) == FAILURE)
1729 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1737 gfc_error ("PRIVATE statement at %C must precede "
1738 "structure components");
1745 gfc_error ("Duplicate PRIVATE statement at %C");
1749 s.sym->component_access = ACCESS_PRIVATE;
1750 accept_statement (ST_PRIVATE);
1757 gfc_error ("SEQUENCE statement at %C must precede "
1758 "structure components");
1763 if (gfc_current_block ()->attr.sequence)
1764 gfc_warning ("SEQUENCE attribute at %C already specified in "
1769 gfc_error ("Duplicate SEQUENCE statement at %C");
1774 gfc_add_sequence (&gfc_current_block ()->attr,
1775 gfc_current_block ()->name, NULL);
1779 unexpected_statement (st);
1784 /* need to verify that all fields of the derived type are
1785 * interoperable with C if the type is declared to be bind(c)
1787 derived_sym = gfc_current_block();
1789 sym = gfc_current_block ();
1790 for (c = sym->components; c; c = c->next)
1792 /* Look for allocatable components. */
1794 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.alloc_comp))
1796 sym->attr.alloc_comp = 1;
1800 /* Look for pointer components. */
1802 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.pointer_comp))
1804 sym->attr.pointer_comp = 1;
1808 /* Look for private components. */
1809 if (sym->component_access == ACCESS_PRIVATE
1810 || c->access == ACCESS_PRIVATE
1811 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.private_comp))
1813 sym->attr.private_comp = 1;
1818 if (!seen_component)
1819 sym->attr.zero_comp = 1;
1825 /* Parse an ENUM. */
1834 int seen_enumerator = 0;
1838 push_state (&s, COMP_ENUM, gfc_new_block);
1842 while (compiling_enum)
1844 st = next_statement ();
1852 seen_enumerator = 1;
1853 accept_statement (st);
1858 if (!seen_enumerator)
1860 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1863 accept_statement (st);
1867 gfc_free_enum_history ();
1868 unexpected_statement (st);
1876 /* Parse an interface. We must be able to deal with the possibility
1877 of recursive interfaces. The parse_spec() subroutine is mutually
1878 recursive with parse_interface(). */
1880 static gfc_statement parse_spec (gfc_statement);
1883 parse_interface (void)
1885 gfc_compile_state new_state, current_state;
1886 gfc_symbol *prog_unit, *sym;
1887 gfc_interface_info save;
1888 gfc_state_data s1, s2;
1892 accept_statement (ST_INTERFACE);
1894 current_interface.ns = gfc_current_ns;
1895 save = current_interface;
1897 sym = (current_interface.type == INTERFACE_GENERIC
1898 || current_interface.type == INTERFACE_USER_OP)
1899 ? gfc_new_block : NULL;
1901 push_state (&s1, COMP_INTERFACE, sym);
1902 current_state = COMP_NONE;
1905 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1907 st = next_statement ();
1914 new_state = COMP_SUBROUTINE;
1915 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1916 gfc_new_block->formal, NULL);
1920 new_state = COMP_FUNCTION;
1921 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1922 gfc_new_block->formal, NULL);
1926 case ST_MODULE_PROC: /* The module procedure matcher makes
1927 sure the context is correct. */
1928 accept_statement (st);
1929 gfc_free_namespace (gfc_current_ns);
1932 case ST_END_INTERFACE:
1933 gfc_free_namespace (gfc_current_ns);
1934 gfc_current_ns = current_interface.ns;
1938 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1939 gfc_ascii_statement (st));
1940 reject_statement ();
1941 gfc_free_namespace (gfc_current_ns);
1946 /* Make sure that a generic interface has only subroutines or
1947 functions and that the generic name has the right attribute. */
1948 if (current_interface.type == INTERFACE_GENERIC)
1950 if (current_state == COMP_NONE)
1952 if (new_state == COMP_FUNCTION)
1953 gfc_add_function (&sym->attr, sym->name, NULL);
1954 else if (new_state == COMP_SUBROUTINE)
1955 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1957 current_state = new_state;
1961 if (new_state != current_state)
1963 if (new_state == COMP_SUBROUTINE)
1964 gfc_error ("SUBROUTINE at %C does not belong in a "
1965 "generic function interface");
1967 if (new_state == COMP_FUNCTION)
1968 gfc_error ("FUNCTION at %C does not belong in a "
1969 "generic subroutine interface");
1974 if (current_interface.type == INTERFACE_ABSTRACT)
1976 gfc_new_block->attr.abstract = 1;
1977 if (gfc_is_intrinsic_typename (gfc_new_block->name))
1978 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
1979 "cannot be the same as an intrinsic type",
1980 gfc_new_block->name);
1983 push_state (&s2, new_state, gfc_new_block);
1984 accept_statement (st);
1985 prog_unit = gfc_new_block;
1986 prog_unit->formal_ns = gfc_current_ns;
1987 proc_locus = gfc_current_locus;
1990 /* Read data declaration statements. */
1991 st = parse_spec (ST_NONE);
1993 /* Since the interface block does not permit an IMPLICIT statement,
1994 the default type for the function or the result must be taken
1995 from the formal namespace. */
1996 if (new_state == COMP_FUNCTION)
1998 if (prog_unit->result == prog_unit
1999 && prog_unit->ts.type == BT_UNKNOWN)
2000 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
2001 else if (prog_unit->result != prog_unit
2002 && prog_unit->result->ts.type == BT_UNKNOWN)
2003 gfc_set_default_type (prog_unit->result, 1,
2004 prog_unit->formal_ns);
2007 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
2009 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2010 gfc_ascii_statement (st));
2011 reject_statement ();
2015 current_interface = save;
2016 gfc_add_interface (prog_unit);
2019 if (current_interface.ns
2020 && current_interface.ns->proc_name
2021 && strcmp (current_interface.ns->proc_name->name,
2022 prog_unit->name) == 0)
2023 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2024 "enclosing procedure", prog_unit->name, &proc_locus);
2033 /* Associate function characteristics by going back to the function
2034 declaration and rematching the prefix. */
2037 match_deferred_characteristics (gfc_typespec * ts)
2040 match m = MATCH_ERROR;
2041 char name[GFC_MAX_SYMBOL_LEN + 1];
2043 loc = gfc_current_locus;
2045 gfc_current_locus = gfc_current_block ()->declared_at;
2048 gfc_buffer_error (1);
2049 m = gfc_match_prefix (ts);
2050 gfc_buffer_error (0);
2052 if (ts->type == BT_DERIVED)
2056 if (!ts->derived || !ts->derived->components)
2060 /* Only permit one go at the characteristic association. */
2064 /* Set the function locus correctly. If we have not found the
2065 function name, there is an error. */
2066 gfc_match ("function% %n", name);
2067 if (m == MATCH_YES && strcmp (name, gfc_current_block ()->name) == 0)
2069 gfc_current_block ()->declared_at = gfc_current_locus;
2070 gfc_commit_symbols ();
2075 gfc_current_locus =loc;
2080 /* Parse a set of specification statements. Returns the statement
2081 that doesn't fit. */
2083 static gfc_statement
2084 parse_spec (gfc_statement st)
2087 bool bad_characteristic = false;
2090 verify_st_order (&ss, ST_NONE);
2092 st = next_statement ();
2102 case ST_DATA: /* Not allowed in interfaces */
2103 if (gfc_current_state () == COMP_INTERFACE)
2110 case ST_IMPLICIT_NONE:
2115 case ST_DERIVED_DECL:
2117 if (verify_st_order (&ss, st) == FAILURE)
2119 reject_statement ();
2120 st = next_statement ();
2130 case ST_DERIVED_DECL:
2136 if (gfc_current_state () != COMP_MODULE)
2138 gfc_error ("%s statement must appear in a MODULE",
2139 gfc_ascii_statement (st));
2143 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
2145 gfc_error ("%s statement at %C follows another accessibility "
2146 "specification", gfc_ascii_statement (st));
2150 gfc_current_ns->default_access = (st == ST_PUBLIC)
2151 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2155 case ST_STATEMENT_FUNCTION:
2156 if (gfc_current_state () == COMP_MODULE)
2158 unexpected_statement (st);
2166 accept_statement (st);
2167 st = next_statement ();
2171 accept_statement (st);
2173 st = next_statement ();
2176 case ST_GET_FCN_CHARACTERISTICS:
2177 /* This statement triggers the association of a function's result
2179 ts = &gfc_current_block ()->result->ts;
2180 if (match_deferred_characteristics (ts) != MATCH_YES)
2181 bad_characteristic = true;
2183 st = next_statement ();
2190 /* If match_deferred_characteristics failed, then there is an error. */
2191 if (bad_characteristic)
2193 ts = &gfc_current_block ()->result->ts;
2194 if (ts->type != BT_DERIVED)
2195 gfc_error ("Bad kind expression for function '%s' at %L",
2196 gfc_current_block ()->name,
2197 &gfc_current_block ()->declared_at);
2199 gfc_error ("The type for function '%s' at %L is not accessible",
2200 gfc_current_block ()->name,
2201 &gfc_current_block ()->declared_at);
2203 gfc_current_block ()->ts.kind = 0;
2204 /* Keep the derived type; if it's bad, it will be discovered later. */
2205 if (!(ts->type == BT_DERIVED && ts->derived))
2206 ts->type = BT_UNKNOWN;
2213 /* Parse a WHERE block, (not a simple WHERE statement). */
2216 parse_where_block (void)
2218 int seen_empty_else;
2223 accept_statement (ST_WHERE_BLOCK);
2224 top = gfc_state_stack->tail;
2226 push_state (&s, COMP_WHERE, gfc_new_block);
2228 d = add_statement ();
2229 d->expr = top->expr;
2235 seen_empty_else = 0;
2239 st = next_statement ();
2245 case ST_WHERE_BLOCK:
2246 parse_where_block ();
2251 accept_statement (st);
2255 if (seen_empty_else)
2257 gfc_error ("ELSEWHERE statement at %C follows previous "
2258 "unmasked ELSEWHERE");
2262 if (new_st.expr == NULL)
2263 seen_empty_else = 1;
2265 d = new_level (gfc_state_stack->head);
2267 d->expr = new_st.expr;
2269 accept_statement (st);
2274 accept_statement (st);
2278 gfc_error ("Unexpected %s statement in WHERE block at %C",
2279 gfc_ascii_statement (st));
2280 reject_statement ();
2284 while (st != ST_END_WHERE);
2290 /* Parse a FORALL block (not a simple FORALL statement). */
2293 parse_forall_block (void)
2299 accept_statement (ST_FORALL_BLOCK);
2300 top = gfc_state_stack->tail;
2302 push_state (&s, COMP_FORALL, gfc_new_block);
2304 d = add_statement ();
2305 d->op = EXEC_FORALL;
2310 st = next_statement ();
2315 case ST_POINTER_ASSIGNMENT:
2318 accept_statement (st);
2321 case ST_WHERE_BLOCK:
2322 parse_where_block ();
2325 case ST_FORALL_BLOCK:
2326 parse_forall_block ();
2330 accept_statement (st);
2337 gfc_error ("Unexpected %s statement in FORALL block at %C",
2338 gfc_ascii_statement (st));
2340 reject_statement ();
2344 while (st != ST_END_FORALL);
2350 static gfc_statement parse_executable (gfc_statement);
2352 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2355 parse_if_block (void)
2364 accept_statement (ST_IF_BLOCK);
2366 top = gfc_state_stack->tail;
2367 push_state (&s, COMP_IF, gfc_new_block);
2369 new_st.op = EXEC_IF;
2370 d = add_statement ();
2372 d->expr = top->expr;
2378 st = parse_executable (ST_NONE);
2388 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2389 "statement at %L", &else_locus);
2391 reject_statement ();
2395 d = new_level (gfc_state_stack->head);
2397 d->expr = new_st.expr;
2399 accept_statement (st);
2406 gfc_error ("Duplicate ELSE statements at %L and %C",
2408 reject_statement ();
2413 else_locus = gfc_current_locus;
2415 d = new_level (gfc_state_stack->head);
2418 accept_statement (st);
2426 unexpected_statement (st);
2430 while (st != ST_ENDIF);
2433 accept_statement (st);
2437 /* Parse a SELECT block. */
2440 parse_select_block (void)
2446 accept_statement (ST_SELECT_CASE);
2448 cp = gfc_state_stack->tail;
2449 push_state (&s, COMP_SELECT, gfc_new_block);
2451 /* Make sure that the next statement is a CASE or END SELECT. */
2454 st = next_statement ();
2457 if (st == ST_END_SELECT)
2459 /* Empty SELECT CASE is OK. */
2460 accept_statement (st);
2467 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2470 reject_statement ();
2473 /* At this point, we're got a nonempty select block. */
2474 cp = new_level (cp);
2477 accept_statement (st);
2481 st = parse_executable (ST_NONE);
2488 cp = new_level (gfc_state_stack->head);
2490 gfc_clear_new_st ();
2492 accept_statement (st);
2498 /* Can't have an executable statement because of
2499 parse_executable(). */
2501 unexpected_statement (st);
2505 while (st != ST_END_SELECT);
2508 accept_statement (st);
2512 /* Given a symbol, make sure it is not an iteration variable for a DO
2513 statement. This subroutine is called when the symbol is seen in a
2514 context that causes it to become redefined. If the symbol is an
2515 iterator, we generate an error message and return nonzero. */
2518 gfc_check_do_variable (gfc_symtree *st)
2522 for (s=gfc_state_stack; s; s = s->previous)
2523 if (s->do_variable == st)
2525 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2526 "loop beginning at %L", st->name, &s->head->loc);
2534 /* Checks to see if the current statement label closes an enddo.
2535 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2536 an error) if it incorrectly closes an ENDDO. */
2539 check_do_closure (void)
2543 if (gfc_statement_label == NULL)
2546 for (p = gfc_state_stack; p; p = p->previous)
2547 if (p->state == COMP_DO)
2551 return 0; /* No loops to close */
2553 if (p->ext.end_do_label == gfc_statement_label)
2556 if (p == gfc_state_stack)
2559 gfc_error ("End of nonblock DO statement at %C is within another block");
2563 /* At this point, the label doesn't terminate the innermost loop.
2564 Make sure it doesn't terminate another one. */
2565 for (; p; p = p->previous)
2566 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
2568 gfc_error ("End of nonblock DO statement at %C is interwoven "
2569 "with another DO loop");
2577 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2578 handled inside of parse_executable(), because they aren't really
2582 parse_do_block (void)
2589 s.ext.end_do_label = new_st.label;
2591 if (new_st.ext.iterator != NULL)
2592 stree = new_st.ext.iterator->var->symtree;
2596 accept_statement (ST_DO);
2598 top = gfc_state_stack->tail;
2599 push_state (&s, COMP_DO, gfc_new_block);
2601 s.do_variable = stree;
2603 top->block = new_level (top);
2604 top->block->op = EXEC_DO;
2607 st = parse_executable (ST_NONE);
2615 if (s.ext.end_do_label != NULL
2616 && s.ext.end_do_label != gfc_statement_label)
2617 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
2620 if (gfc_statement_label != NULL)
2622 new_st.op = EXEC_NOP;
2627 case ST_IMPLIED_ENDDO:
2628 /* If the do-stmt of this DO construct has a do-construct-name,
2629 the corresponding end-do must be an end-do-stmt (with a matching
2630 name, but in that case we must have seen ST_ENDDO first).
2631 We only complain about this in pedantic mode. */
2632 if (gfc_current_block () != NULL)
2633 gfc_error_now ("named block DO at %L requires matching ENDDO name",
2634 &gfc_current_block()->declared_at);
2639 unexpected_statement (st);
2644 accept_statement (st);
2648 /* Parse the statements of OpenMP do/parallel do. */
2650 static gfc_statement
2651 parse_omp_do (gfc_statement omp_st)
2657 accept_statement (omp_st);
2659 cp = gfc_state_stack->tail;
2660 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2661 np = new_level (cp);
2667 st = next_statement ();
2670 else if (st == ST_DO)
2673 unexpected_statement (st);
2677 if (gfc_statement_label != NULL
2678 && gfc_state_stack->previous != NULL
2679 && gfc_state_stack->previous->state == COMP_DO
2680 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
2688 there should be no !$OMP END DO. */
2690 return ST_IMPLIED_ENDDO;
2693 check_do_closure ();
2696 st = next_statement ();
2697 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
2699 if (new_st.op == EXEC_OMP_END_NOWAIT)
2700 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2702 gcc_assert (new_st.op == EXEC_NOP);
2703 gfc_clear_new_st ();
2704 gfc_commit_symbols ();
2705 gfc_warning_check ();
2706 st = next_statement ();
2712 /* Parse the statements of OpenMP atomic directive. */
2715 parse_omp_atomic (void)
2721 accept_statement (ST_OMP_ATOMIC);
2723 cp = gfc_state_stack->tail;
2724 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2725 np = new_level (cp);
2731 st = next_statement ();
2734 else if (st == ST_ASSIGNMENT)
2737 unexpected_statement (st);
2740 accept_statement (st);
2746 /* Parse the statements of an OpenMP structured block. */
2749 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
2751 gfc_statement st, omp_end_st;
2755 accept_statement (omp_st);
2757 cp = gfc_state_stack->tail;
2758 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2759 np = new_level (cp);
2765 case ST_OMP_PARALLEL:
2766 omp_end_st = ST_OMP_END_PARALLEL;
2768 case ST_OMP_PARALLEL_SECTIONS:
2769 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
2771 case ST_OMP_SECTIONS:
2772 omp_end_st = ST_OMP_END_SECTIONS;
2774 case ST_OMP_ORDERED:
2775 omp_end_st = ST_OMP_END_ORDERED;
2777 case ST_OMP_CRITICAL:
2778 omp_end_st = ST_OMP_END_CRITICAL;
2781 omp_end_st = ST_OMP_END_MASTER;
2784 omp_end_st = ST_OMP_END_SINGLE;
2786 case ST_OMP_WORKSHARE:
2787 omp_end_st = ST_OMP_END_WORKSHARE;
2789 case ST_OMP_PARALLEL_WORKSHARE:
2790 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
2798 if (workshare_stmts_only)
2800 /* Inside of !$omp workshare, only
2803 where statements and constructs
2804 forall statements and constructs
2808 are allowed. For !$omp critical these
2809 restrictions apply recursively. */
2812 st = next_statement ();
2823 accept_statement (st);
2826 case ST_WHERE_BLOCK:
2827 parse_where_block ();
2830 case ST_FORALL_BLOCK:
2831 parse_forall_block ();
2834 case ST_OMP_PARALLEL:
2835 case ST_OMP_PARALLEL_SECTIONS:
2836 parse_omp_structured_block (st, false);
2839 case ST_OMP_PARALLEL_WORKSHARE:
2840 case ST_OMP_CRITICAL:
2841 parse_omp_structured_block (st, true);
2844 case ST_OMP_PARALLEL_DO:
2845 st = parse_omp_do (st);
2849 parse_omp_atomic ();
2860 st = next_statement ();
2864 st = parse_executable (ST_NONE);
2867 else if (st == ST_OMP_SECTION
2868 && (omp_st == ST_OMP_SECTIONS
2869 || omp_st == ST_OMP_PARALLEL_SECTIONS))
2871 np = new_level (np);
2875 else if (st != omp_end_st)
2876 unexpected_statement (st);
2878 while (st != omp_end_st);
2882 case EXEC_OMP_END_NOWAIT:
2883 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2885 case EXEC_OMP_CRITICAL:
2886 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
2887 || (new_st.ext.omp_name != NULL
2888 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
2889 gfc_error ("Name after !$omp critical and !$omp end critical does "
2891 gfc_free (CONST_CAST (char *, new_st.ext.omp_name));
2893 case EXEC_OMP_END_SINGLE:
2894 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
2895 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
2896 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
2897 gfc_free_omp_clauses (new_st.ext.omp_clauses);
2905 gfc_clear_new_st ();
2906 gfc_commit_symbols ();
2907 gfc_warning_check ();
2912 /* Accept a series of executable statements. We return the first
2913 statement that doesn't fit to the caller. Any block statements are
2914 passed on to the correct handler, which usually passes the buck
2917 static gfc_statement
2918 parse_executable (gfc_statement st)
2923 st = next_statement ();
2927 close_flag = check_do_closure ();
2932 case ST_END_PROGRAM:
2935 case ST_END_FUNCTION:
2939 case ST_END_SUBROUTINE:
2944 case ST_SELECT_CASE:
2945 gfc_error ("%s statement at %C cannot terminate a non-block "
2946 "DO loop", gfc_ascii_statement (st));
2962 accept_statement (st);
2963 if (close_flag == 1)
2964 return ST_IMPLIED_ENDDO;
2971 case ST_SELECT_CASE:
2972 parse_select_block ();
2977 if (check_do_closure () == 1)
2978 return ST_IMPLIED_ENDDO;
2981 case ST_WHERE_BLOCK:
2982 parse_where_block ();
2985 case ST_FORALL_BLOCK:
2986 parse_forall_block ();
2989 case ST_OMP_PARALLEL:
2990 case ST_OMP_PARALLEL_SECTIONS:
2991 case ST_OMP_SECTIONS:
2992 case ST_OMP_ORDERED:
2993 case ST_OMP_CRITICAL:
2996 parse_omp_structured_block (st, false);
2999 case ST_OMP_WORKSHARE:
3000 case ST_OMP_PARALLEL_WORKSHARE:
3001 parse_omp_structured_block (st, true);
3005 case ST_OMP_PARALLEL_DO:
3006 st = parse_omp_do (st);
3007 if (st == ST_IMPLIED_ENDDO)
3012 parse_omp_atomic ();
3019 st = next_statement ();
3024 /* Parse a series of contained program units. */
3026 static void parse_progunit (gfc_statement);
3029 /* Fix the symbols for sibling functions. These are incorrectly added to
3030 the child namespace as the parser didn't know about this procedure. */
3033 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
3037 gfc_symbol *old_sym;
3039 sym->attr.referenced = 1;
3040 for (ns = siblings; ns; ns = ns->sibling)
3042 gfc_find_sym_tree (sym->name, ns, 0, &st);
3044 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
3047 old_sym = st->n.sym;
3048 if (old_sym->ns == ns
3049 && !old_sym->attr.contained
3051 /* By 14.6.1.3, host association should be excluded
3052 for the following. */
3053 && !(old_sym->attr.external
3054 || (old_sym->ts.type != BT_UNKNOWN
3055 && !old_sym->attr.implicit_type)
3056 || old_sym->attr.flavor == FL_PARAMETER
3057 || old_sym->attr.in_common
3058 || old_sym->attr.in_equivalence
3059 || old_sym->attr.data
3060 || old_sym->attr.dummy
3061 || old_sym->attr.result
3062 || old_sym->attr.dimension
3063 || old_sym->attr.allocatable
3064 || old_sym->attr.intrinsic
3065 || old_sym->attr.generic
3066 || old_sym->attr.flavor == FL_NAMELIST
3067 || old_sym->attr.proc == PROC_ST_FUNCTION))
3069 /* Replace it with the symbol from the parent namespace. */
3073 /* Free the old (local) symbol. */
3075 if (old_sym->refs == 0)
3076 gfc_free_symbol (old_sym);
3079 /* Do the same for any contained procedures. */
3080 gfc_fixup_sibling_symbols (sym, ns->contained);
3085 parse_contained (int module)
3087 gfc_namespace *ns, *parent_ns, *tmp;
3088 gfc_state_data s1, s2;
3092 int contains_statements = 0;
3095 push_state (&s1, COMP_CONTAINS, NULL);
3096 parent_ns = gfc_current_ns;
3100 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
3102 gfc_current_ns->sibling = parent_ns->contained;
3103 parent_ns->contained = gfc_current_ns;
3106 /* Process the next available statement. We come here if we got an error
3107 and rejected the last statement. */
3108 st = next_statement ();
3117 contains_statements = 1;
3118 accept_statement (st);
3121 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
3124 /* For internal procedures, create/update the symbol in the
3125 parent namespace. */
3129 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
3130 gfc_error ("Contained procedure '%s' at %C is already "
3131 "ambiguous", gfc_new_block->name);
3134 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
3135 &gfc_new_block->declared_at) ==
3138 if (st == ST_FUNCTION)
3139 gfc_add_function (&sym->attr, sym->name,
3140 &gfc_new_block->declared_at);
3142 gfc_add_subroutine (&sym->attr, sym->name,
3143 &gfc_new_block->declared_at);
3147 gfc_commit_symbols ();
3150 sym = gfc_new_block;
3152 /* Mark this as a contained function, so it isn't replaced
3153 by other module functions. */
3154 sym->attr.contained = 1;
3155 sym->attr.referenced = 1;
3157 parse_progunit (ST_NONE);
3159 /* Fix up any sibling functions that refer to this one. */
3160 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
3161 /* Or refer to any of its alternate entry points. */
3162 for (el = gfc_current_ns->entries; el; el = el->next)
3163 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
3165 gfc_current_ns->code = s2.head;
3166 gfc_current_ns = parent_ns;
3171 /* These statements are associated with the end of the host unit. */
3172 case ST_END_FUNCTION:
3174 case ST_END_PROGRAM:
3175 case ST_END_SUBROUTINE:
3176 accept_statement (st);
3180 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
3181 gfc_ascii_statement (st));
3182 reject_statement ();
3188 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
3189 && st != ST_END_MODULE && st != ST_END_PROGRAM);
3191 /* The first namespace in the list is guaranteed to not have
3192 anything (worthwhile) in it. */
3193 tmp = gfc_current_ns;
3194 gfc_current_ns = parent_ns;
3195 if (seen_error && tmp->refs > 1)
3196 gfc_free_namespace (tmp);
3198 ns = gfc_current_ns->contained;
3199 gfc_current_ns->contained = ns->sibling;
3200 gfc_free_namespace (ns);
3203 if (!contains_statements)
3204 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTAINS statement without "
3205 "FUNCTION or SUBROUTINE statement at %C");
3209 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
3212 parse_progunit (gfc_statement st)
3217 st = parse_spec (st);
3227 accept_statement (st);
3234 if (gfc_current_state () == COMP_FUNCTION)
3235 gfc_check_function_type (gfc_current_ns);
3240 st = parse_executable (st);
3251 accept_statement (st);
3258 unexpected_statement (st);
3259 reject_statement ();
3260 st = next_statement ();
3266 for (p = gfc_state_stack; p; p = p->previous)
3267 if (p->state == COMP_CONTAINS)
3270 if (gfc_find_state (COMP_MODULE) == SUCCESS)
3275 gfc_error ("CONTAINS statement at %C is already in a contained "
3277 st = next_statement ();
3281 parse_contained (0);
3284 gfc_current_ns->code = gfc_state_stack->head;
3288 /* Come here to complain about a global symbol already in use as
3292 gfc_global_used (gfc_gsymbol *sym, locus *where)
3297 where = &gfc_current_locus;
3307 case GSYM_SUBROUTINE:
3308 name = "SUBROUTINE";
3313 case GSYM_BLOCK_DATA:
3314 name = "BLOCK DATA";
3320 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
3324 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
3325 sym->name, where, name, &sym->where);
3329 /* Parse a block data program unit. */
3332 parse_block_data (void)
3335 static locus blank_locus;
3336 static int blank_block=0;
3339 gfc_current_ns->proc_name = gfc_new_block;
3340 gfc_current_ns->is_block_data = 1;
3342 if (gfc_new_block == NULL)
3345 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3346 "prior BLOCK DATA at %L", &blank_locus);
3350 blank_locus = gfc_current_locus;
3355 s = gfc_get_gsymbol (gfc_new_block->name);
3357 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
3358 gfc_global_used(s, NULL);
3361 s->type = GSYM_BLOCK_DATA;
3362 s->where = gfc_current_locus;
3367 st = parse_spec (ST_NONE);
3369 while (st != ST_END_BLOCK_DATA)
3371 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3372 gfc_ascii_statement (st));
3373 reject_statement ();
3374 st = next_statement ();
3379 /* Parse a module subprogram. */
3387 s = gfc_get_gsymbol (gfc_new_block->name);
3388 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
3389 gfc_global_used(s, NULL);
3392 s->type = GSYM_MODULE;
3393 s->where = gfc_current_locus;
3397 st = parse_spec (ST_NONE);
3406 parse_contained (1);
3410 accept_statement (st);
3414 gfc_error ("Unexpected %s statement in MODULE at %C",
3415 gfc_ascii_statement (st));
3417 reject_statement ();
3418 st = next_statement ();
3424 /* Add a procedure name to the global symbol table. */
3427 add_global_procedure (int sub)
3431 s = gfc_get_gsymbol(gfc_new_block->name);
3434 || (s->type != GSYM_UNKNOWN
3435 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3436 gfc_global_used(s, NULL);
3439 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3440 s->where = gfc_current_locus;
3446 /* Add a program to the global symbol table. */
3449 add_global_program (void)
3453 if (gfc_new_block == NULL)
3455 s = gfc_get_gsymbol (gfc_new_block->name);
3457 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
3458 gfc_global_used(s, NULL);
3461 s->type = GSYM_PROGRAM;
3462 s->where = gfc_current_locus;
3468 /* Top level parser. */
3471 gfc_parse_file (void)
3473 int seen_program, errors_before, errors;
3474 gfc_state_data top, s;
3478 gfc_start_source_files ();
3480 top.state = COMP_NONE;
3482 top.previous = NULL;
3483 top.head = top.tail = NULL;
3484 top.do_variable = NULL;
3486 gfc_state_stack = ⊤
3488 gfc_clear_new_st ();
3490 gfc_statement_label = NULL;
3492 if (setjmp (eof_buf))
3493 return FAILURE; /* Come here on unexpected EOF */
3497 /* Exit early for empty files. */
3503 st = next_statement ();
3512 goto duplicate_main;
3514 prog_locus = gfc_current_locus;
3516 push_state (&s, COMP_PROGRAM, gfc_new_block);
3517 main_program_symbol(gfc_current_ns, gfc_new_block->name);
3518 accept_statement (st);
3519 add_global_program ();
3520 parse_progunit (ST_NONE);
3524 add_global_procedure (1);
3525 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
3526 accept_statement (st);
3527 parse_progunit (ST_NONE);
3531 add_global_procedure (0);
3532 push_state (&s, COMP_FUNCTION, gfc_new_block);
3533 accept_statement (st);
3534 parse_progunit (ST_NONE);
3538 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
3539 accept_statement (st);
3540 parse_block_data ();
3544 push_state (&s, COMP_MODULE, gfc_new_block);
3545 accept_statement (st);
3547 gfc_get_errors (NULL, &errors_before);
3551 /* Anything else starts a nameless main program block. */
3554 goto duplicate_main;
3556 prog_locus = gfc_current_locus;
3558 push_state (&s, COMP_PROGRAM, gfc_new_block);
3559 main_program_symbol (gfc_current_ns, "MAIN__");
3560 parse_progunit (st);
3564 gfc_current_ns->code = s.head;
3566 gfc_resolve (gfc_current_ns);
3568 /* Dump the parse tree if requested. */
3569 if (gfc_option.dump_parse_tree)
3570 gfc_dump_parse_tree (gfc_current_ns, stdout);
3572 gfc_get_errors (NULL, &errors);
3573 if (s.state == COMP_MODULE)
3575 gfc_dump_module (s.sym->name, errors_before == errors);
3577 gfc_generate_module_code (gfc_current_ns);
3582 gfc_generate_code (gfc_current_ns);
3590 gfc_end_source_files ();
3594 /* If we see a duplicate main program, shut down. If the second
3595 instance is an implied main program, ie data decls or executable
3596 statements, we're in for lots of errors. */
3597 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
3598 reject_statement ();