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_ascii_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_ascii_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_ascii_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. */
575 at_bol
= gfc_at_bol ();
576 gfc_gobble_whitespace ();
578 c
= gfc_peek_ascii_char ();
584 /* Found a statement label? */
585 m
= gfc_match_st_label (&gfc_statement_label
);
587 d
= gfc_peek_ascii_char ();
588 if (m
!= MATCH_YES
|| !gfc_is_whitespace (d
))
590 gfc_match_small_literal_int (&i
, &cnt
);
593 gfc_error_now ("Too many digits in statement label at %C");
596 gfc_error_now ("Zero is not a valid statement label at %C");
599 c
= gfc_next_ascii_char ();
602 if (!gfc_is_whitespace (c
))
603 gfc_error_now ("Non-numeric character in statement label at %C");
609 label_locus
= gfc_current_locus
;
611 gfc_gobble_whitespace ();
613 if (at_bol
&& gfc_peek_ascii_char () == ';')
615 gfc_error_now ("Semicolon at %C needs to be preceded by "
617 gfc_next_ascii_char (); /* Eat up the semicolon. */
621 if (gfc_match_eos () == MATCH_YES
)
623 gfc_warning_now ("Ignoring statement label in empty statement "
625 gfc_free_st_label (gfc_statement_label
);
626 gfc_statement_label
= NULL
;
633 /* Comments have already been skipped by the time we get here,
634 except for OpenMP directives. */
635 if (gfc_option
.flag_openmp
)
639 c
= gfc_next_ascii_char ();
640 for (i
= 0; i
< 5; i
++, c
= gfc_next_ascii_char ())
641 gcc_assert (c
== "!$omp"[i
]);
643 gcc_assert (c
== ' ');
644 gfc_gobble_whitespace ();
645 return decode_omp_directive ();
649 if (at_bol
&& c
== ';')
651 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
652 gfc_next_ascii_char (); /* Eat up the semicolon. */
656 return decode_statement ();
660 /* Get the next statement in fixed-form source. */
665 int label
, digit_flag
, i
;
670 return decode_statement ();
672 /* Skip past the current label field, parsing a statement label if
673 one is there. This is a weird number parser, since the number is
674 contained within five columns and can have any kind of embedded
675 spaces. We also check for characters that make the rest of the
681 for (i
= 0; i
< 5; i
++)
683 c
= gfc_next_char_literal (0);
700 label
= label
* 10 + ((unsigned char) c
- '0');
701 label_locus
= gfc_current_locus
;
705 /* Comments have already been skipped by the time we get
706 here, except for OpenMP directives. */
708 if (gfc_option
.flag_openmp
)
710 for (i
= 0; i
< 5; i
++, c
= gfc_next_char_literal (0))
711 gcc_assert ((char) gfc_wide_tolower (c
) == "*$omp"[i
]);
713 if (c
!= ' ' && c
!= '0')
715 gfc_buffer_error (0);
716 gfc_error ("Bad continuation line at %C");
720 return decode_omp_directive ();
724 /* Comments have already been skipped by the time we get
725 here so don't bother checking for them. */
728 gfc_buffer_error (0);
729 gfc_error ("Non-numeric character in statement label at %C");
737 gfc_warning_now ("Zero is not a valid statement label at %C");
740 /* We've found a valid statement label. */
741 gfc_statement_label
= gfc_get_st_label (label
);
745 /* Since this line starts a statement, it cannot be a continuation
746 of a previous statement. If we see something here besides a
747 space or zero, it must be a bad continuation line. */
749 c
= gfc_next_char_literal (0);
753 if (c
!= ' ' && c
!= '0')
755 gfc_buffer_error (0);
756 gfc_error ("Bad continuation line at %C");
760 /* Now that we've taken care of the statement label columns, we have
761 to make sure that the first nonblank character is not a '!'. If
762 it is, the rest of the line is a comment. */
766 loc
= gfc_current_locus
;
767 c
= gfc_next_char_literal (0);
769 while (gfc_is_whitespace (c
));
773 gfc_current_locus
= loc
;
777 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
781 if (gfc_match_eos () == MATCH_YES
)
784 /* At this point, we've got a nonblank statement to parse. */
785 return decode_statement ();
789 gfc_warning ("Ignoring statement label in empty statement at %C");
795 /* Return the next non-ST_NONE statement to the caller. We also worry
796 about including files and the ends of include files at this stage. */
799 next_statement (void)
803 gfc_new_block
= NULL
;
807 gfc_statement_label
= NULL
;
808 gfc_buffer_error (1);
812 if ((gfc_option
.warn_line_truncation
|| gfc_current_form
== FORM_FREE
)
813 && gfc_current_locus
.lb
814 && gfc_current_locus
.lb
->truncated
)
815 gfc_warning_now ("Line truncated at %C");
820 gfc_skip_comments ();
828 if (gfc_define_undef_line ())
831 old_locus
= gfc_current_locus
;
833 st
= (gfc_current_form
== FORM_FIXED
) ? next_fixed () : next_free ();
839 gfc_buffer_error (0);
841 if (st
== ST_GET_FCN_CHARACTERISTICS
&& gfc_statement_label
!= NULL
)
843 gfc_free_st_label (gfc_statement_label
);
844 gfc_statement_label
= NULL
;
845 gfc_current_locus
= old_locus
;
849 check_statement_label (st
);
855 /****************************** Parser ***********************************/
857 /* The parser subroutines are of type 'try' that fail if the file ends
860 /* Macros that expand to case-labels for various classes of
861 statements. Start with executable statements that directly do
864 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
865 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
866 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
867 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
868 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
869 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
870 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
871 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
874 /* Statements that mark other executable statements. */
876 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
877 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
878 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
879 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
880 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
881 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
883 /* Declaration statements */
885 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
886 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
887 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
890 /* Block end statements. Errors associated with interchanging these
891 are detected in gfc_match_end(). */
893 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
894 case ST_END_PROGRAM: case ST_END_SUBROUTINE
897 /* Push a new state onto the stack. */
900 push_state (gfc_state_data
*p
, gfc_compile_state new_state
, gfc_symbol
*sym
)
902 p
->state
= new_state
;
903 p
->previous
= gfc_state_stack
;
905 p
->head
= p
->tail
= NULL
;
906 p
->do_variable
= NULL
;
911 /* Pop the current state. */
915 gfc_state_stack
= gfc_state_stack
->previous
;
919 /* Try to find the given state in the state stack. */
922 gfc_find_state (gfc_compile_state state
)
926 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
927 if (p
->state
== state
)
930 return (p
== NULL
) ? FAILURE
: SUCCESS
;
934 /* Starts a new level in the statement list. */
937 new_level (gfc_code
*q
)
941 p
= q
->block
= gfc_get_code ();
943 gfc_state_stack
->head
= gfc_state_stack
->tail
= p
;
949 /* Add the current new_st code structure and adds it to the current
950 program unit. As a side-effect, it zeroes the new_st. */
960 p
->loc
= gfc_current_locus
;
962 if (gfc_state_stack
->head
== NULL
)
963 gfc_state_stack
->head
= p
;
965 gfc_state_stack
->tail
->next
= p
;
967 while (p
->next
!= NULL
)
970 gfc_state_stack
->tail
= p
;
978 /* Frees everything associated with the current statement. */
981 undo_new_statement (void)
983 gfc_free_statements (new_st
.block
);
984 gfc_free_statements (new_st
.next
);
985 gfc_free_statement (&new_st
);
990 /* If the current statement has a statement label, make sure that it
991 is allowed to, or should have one. */
994 check_statement_label (gfc_statement st
)
998 if (gfc_statement_label
== NULL
)
1000 if (st
== ST_FORMAT
)
1001 gfc_error ("FORMAT statement at %L does not have a statement label",
1008 case ST_END_PROGRAM
:
1009 case ST_END_FUNCTION
:
1010 case ST_END_SUBROUTINE
:
1016 type
= ST_LABEL_TARGET
;
1020 type
= ST_LABEL_FORMAT
;
1023 /* Statement labels are not restricted from appearing on a
1024 particular line. However, there are plenty of situations
1025 where the resulting label can't be referenced. */
1028 type
= ST_LABEL_BAD_TARGET
;
1032 gfc_define_st_label (gfc_statement_label
, type
, &label_locus
);
1034 new_st
.here
= gfc_statement_label
;
1038 /* Figures out what the enclosing program unit is. This will be a
1039 function, subroutine, program, block data or module. */
1042 gfc_enclosing_unit (gfc_compile_state
* result
)
1046 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1047 if (p
->state
== COMP_FUNCTION
|| p
->state
== COMP_SUBROUTINE
1048 || p
->state
== COMP_MODULE
|| p
->state
== COMP_BLOCK_DATA
1049 || p
->state
== COMP_PROGRAM
)
1058 *result
= COMP_PROGRAM
;
1063 /* Translate a statement enum to a string. */
1066 gfc_ascii_statement (gfc_statement st
)
1072 case ST_ARITHMETIC_IF
:
1073 p
= _("arithmetic IF");
1079 p
= _("attribute declaration");
1109 p
= _("data declaration");
1117 case ST_DERIVED_DECL
:
1118 p
= _("derived type declaration");
1132 case ST_END_BLOCK_DATA
:
1133 p
= "END BLOCK DATA";
1144 case ST_END_FUNCTION
:
1150 case ST_END_INTERFACE
:
1151 p
= "END INTERFACE";
1156 case ST_END_PROGRAM
:
1162 case ST_END_SUBROUTINE
:
1163 p
= "END SUBROUTINE";
1174 case ST_EQUIVALENCE
:
1183 case ST_FORALL_BLOCK
: /* Fall through */
1202 case ST_IMPLICIT_NONE
:
1203 p
= "IMPLICIT NONE";
1205 case ST_IMPLIED_ENDDO
:
1206 p
= _("implied END DO");
1232 case ST_MODULE_PROC
:
1233 p
= "MODULE PROCEDURE";
1271 case ST_WHERE_BLOCK
: /* Fall through */
1282 p
= _("assignment");
1284 case ST_POINTER_ASSIGNMENT
:
1285 p
= _("pointer assignment");
1287 case ST_SELECT_CASE
:
1296 case ST_STATEMENT_FUNCTION
:
1297 p
= "STATEMENT FUNCTION";
1299 case ST_LABEL_ASSIGNMENT
:
1300 p
= "LABEL ASSIGNMENT";
1303 p
= "ENUM DEFINITION";
1306 p
= "ENUMERATOR DEFINITION";
1314 case ST_OMP_BARRIER
:
1315 p
= "!$OMP BARRIER";
1317 case ST_OMP_CRITICAL
:
1318 p
= "!$OMP CRITICAL";
1323 case ST_OMP_END_CRITICAL
:
1324 p
= "!$OMP END CRITICAL";
1329 case ST_OMP_END_MASTER
:
1330 p
= "!$OMP END MASTER";
1332 case ST_OMP_END_ORDERED
:
1333 p
= "!$OMP END ORDERED";
1335 case ST_OMP_END_PARALLEL
:
1336 p
= "!$OMP END PARALLEL";
1338 case ST_OMP_END_PARALLEL_DO
:
1339 p
= "!$OMP END PARALLEL DO";
1341 case ST_OMP_END_PARALLEL_SECTIONS
:
1342 p
= "!$OMP END PARALLEL SECTIONS";
1344 case ST_OMP_END_PARALLEL_WORKSHARE
:
1345 p
= "!$OMP END PARALLEL WORKSHARE";
1347 case ST_OMP_END_SECTIONS
:
1348 p
= "!$OMP END SECTIONS";
1350 case ST_OMP_END_SINGLE
:
1351 p
= "!$OMP END SINGLE";
1353 case ST_OMP_END_WORKSHARE
:
1354 p
= "!$OMP END WORKSHARE";
1362 case ST_OMP_ORDERED
:
1363 p
= "!$OMP ORDERED";
1365 case ST_OMP_PARALLEL
:
1366 p
= "!$OMP PARALLEL";
1368 case ST_OMP_PARALLEL_DO
:
1369 p
= "!$OMP PARALLEL DO";
1371 case ST_OMP_PARALLEL_SECTIONS
:
1372 p
= "!$OMP PARALLEL SECTIONS";
1374 case ST_OMP_PARALLEL_WORKSHARE
:
1375 p
= "!$OMP PARALLEL WORKSHARE";
1377 case ST_OMP_SECTIONS
:
1378 p
= "!$OMP SECTIONS";
1380 case ST_OMP_SECTION
:
1381 p
= "!$OMP SECTION";
1386 case ST_OMP_THREADPRIVATE
:
1387 p
= "!$OMP THREADPRIVATE";
1389 case ST_OMP_WORKSHARE
:
1390 p
= "!$OMP WORKSHARE";
1393 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1400 /* Create a symbol for the main program and assign it to ns->proc_name. */
1403 main_program_symbol (gfc_namespace
*ns
, const char *name
)
1405 gfc_symbol
*main_program
;
1406 symbol_attribute attr
;
1408 gfc_get_symbol (name
, ns
, &main_program
);
1409 gfc_clear_attr (&attr
);
1410 attr
.flavor
= FL_PROGRAM
;
1411 attr
.proc
= PROC_UNKNOWN
;
1412 attr
.subroutine
= 1;
1413 attr
.access
= ACCESS_PUBLIC
;
1414 attr
.is_main_program
= 1;
1415 main_program
->attr
= attr
;
1416 main_program
->declared_at
= gfc_current_locus
;
1417 ns
->proc_name
= main_program
;
1418 gfc_commit_symbols ();
1422 /* Do whatever is necessary to accept the last statement. */
1425 accept_statement (gfc_statement st
)
1433 case ST_IMPLICIT_NONE
:
1434 gfc_set_implicit_none ();
1443 gfc_current_ns
->proc_name
= gfc_new_block
;
1446 /* If the statement is the end of a block, lay down a special code
1447 that allows a branch to the end of the block from within the
1452 if (gfc_statement_label
!= NULL
)
1454 new_st
.op
= EXEC_NOP
;
1460 /* The end-of-program unit statements do not get the special
1461 marker and require a statement of some sort if they are a
1464 case ST_END_PROGRAM
:
1465 case ST_END_FUNCTION
:
1466 case ST_END_SUBROUTINE
:
1467 if (gfc_statement_label
!= NULL
)
1469 new_st
.op
= EXEC_RETURN
;
1485 gfc_commit_symbols ();
1486 gfc_warning_check ();
1487 gfc_clear_new_st ();
1491 /* Undo anything tentative that has been built for the current
1495 reject_statement (void)
1497 gfc_new_block
= NULL
;
1498 gfc_undo_symbols ();
1499 gfc_clear_warning ();
1500 undo_new_statement ();
1504 /* Generic complaint about an out of order statement. We also do
1505 whatever is necessary to clean up. */
1508 unexpected_statement (gfc_statement st
)
1510 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st
));
1512 reject_statement ();
1516 /* Given the next statement seen by the matcher, make sure that it is
1517 in proper order with the last. This subroutine is initialized by
1518 calling it with an argument of ST_NONE. If there is a problem, we
1519 issue an error and return FAILURE. Otherwise we return SUCCESS.
1521 Individual parsers need to verify that the statements seen are
1522 valid before calling here, ie ENTRY statements are not allowed in
1523 INTERFACE blocks. The following diagram is taken from the standard:
1525 +---------------------------------------+
1526 | program subroutine function module |
1527 +---------------------------------------+
1529 +---------------------------------------+
1531 +---------------------------------------+
1533 | +-----------+------------------+
1534 | | parameter | implicit |
1535 | +-----------+------------------+
1536 | format | | derived type |
1537 | entry | parameter | interface |
1538 | | data | specification |
1539 | | | statement func |
1540 | +-----------+------------------+
1541 | | data | executable |
1542 +--------+-----------+------------------+
1544 +---------------------------------------+
1545 | internal module/subprogram |
1546 +---------------------------------------+
1548 +---------------------------------------+
1555 { ORDER_START
, ORDER_USE
, ORDER_IMPORT
, ORDER_IMPLICIT_NONE
,
1556 ORDER_IMPLICIT
, ORDER_SPEC
, ORDER_EXEC
1559 gfc_statement last_statement
;
1565 verify_st_order (st_state
*p
, gfc_statement st
)
1571 p
->state
= ORDER_START
;
1575 if (p
->state
> ORDER_USE
)
1577 p
->state
= ORDER_USE
;
1581 if (p
->state
> ORDER_IMPORT
)
1583 p
->state
= ORDER_IMPORT
;
1586 case ST_IMPLICIT_NONE
:
1587 if (p
->state
> ORDER_IMPLICIT_NONE
)
1590 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1591 statement disqualifies a USE but not an IMPLICIT NONE.
1592 Duplicate IMPLICIT NONEs are caught when the implicit types
1595 p
->state
= ORDER_IMPLICIT_NONE
;
1599 if (p
->state
> ORDER_IMPLICIT
)
1601 p
->state
= ORDER_IMPLICIT
;
1606 if (p
->state
< ORDER_IMPLICIT_NONE
)
1607 p
->state
= ORDER_IMPLICIT_NONE
;
1611 if (p
->state
>= ORDER_EXEC
)
1613 if (p
->state
< ORDER_IMPLICIT
)
1614 p
->state
= ORDER_IMPLICIT
;
1618 if (p
->state
< ORDER_SPEC
)
1619 p
->state
= ORDER_SPEC
;
1624 case ST_DERIVED_DECL
:
1626 if (p
->state
>= ORDER_EXEC
)
1628 if (p
->state
< ORDER_SPEC
)
1629 p
->state
= ORDER_SPEC
;
1634 if (p
->state
< ORDER_EXEC
)
1635 p
->state
= ORDER_EXEC
;
1639 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1640 gfc_ascii_statement (st
));
1643 /* All is well, record the statement in case we need it next time. */
1644 p
->where
= gfc_current_locus
;
1645 p
->last_statement
= st
;
1649 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1650 gfc_ascii_statement (st
),
1651 gfc_ascii_statement (p
->last_statement
), &p
->where
);
1657 /* Handle an unexpected end of file. This is a show-stopper... */
1659 static void unexpected_eof (void) ATTRIBUTE_NORETURN
;
1662 unexpected_eof (void)
1666 gfc_error ("Unexpected end of file in '%s'", gfc_source_file
);
1668 /* Memory cleanup. Move to "second to last". */
1669 for (p
= gfc_state_stack
; p
&& p
->previous
&& p
->previous
->previous
;
1672 gfc_current_ns
->code
= (p
&& p
->previous
) ? p
->head
: NULL
;
1675 longjmp (eof_buf
, 1);
1679 /* Parse a derived type. */
1682 parse_derived (void)
1684 int compiling_type
, seen_private
, seen_sequence
, seen_component
, error_flag
;
1687 gfc_symbol
*derived_sym
= NULL
;
1693 accept_statement (ST_DERIVED_DECL
);
1694 push_state (&s
, COMP_DERIVED
, gfc_new_block
);
1696 gfc_new_block
->component_access
= ACCESS_PUBLIC
;
1703 while (compiling_type
)
1705 st
= next_statement ();
1713 accept_statement (st
);
1721 && (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Derived type "
1722 "definition at %C without components")
1726 accept_statement (ST_END_TYPE
);
1730 if (gfc_find_state (COMP_MODULE
) == FAILURE
)
1732 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1740 gfc_error ("PRIVATE statement at %C must precede "
1741 "structure components");
1748 gfc_error ("Duplicate PRIVATE statement at %C");
1752 s
.sym
->component_access
= ACCESS_PRIVATE
;
1753 accept_statement (ST_PRIVATE
);
1760 gfc_error ("SEQUENCE statement at %C must precede "
1761 "structure components");
1766 if (gfc_current_block ()->attr
.sequence
)
1767 gfc_warning ("SEQUENCE attribute at %C already specified in "
1772 gfc_error ("Duplicate SEQUENCE statement at %C");
1777 gfc_add_sequence (&gfc_current_block ()->attr
,
1778 gfc_current_block ()->name
, NULL
);
1782 unexpected_statement (st
);
1787 /* need to verify that all fields of the derived type are
1788 * interoperable with C if the type is declared to be bind(c)
1790 derived_sym
= gfc_current_block();
1792 sym
= gfc_current_block ();
1793 for (c
= sym
->components
; c
; c
= c
->next
)
1795 /* Look for allocatable components. */
1797 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.derived
->attr
.alloc_comp
))
1799 sym
->attr
.alloc_comp
= 1;
1803 /* Look for pointer components. */
1805 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.derived
->attr
.pointer_comp
))
1807 sym
->attr
.pointer_comp
= 1;
1811 /* Look for private components. */
1812 if (sym
->component_access
== ACCESS_PRIVATE
1813 || c
->access
== ACCESS_PRIVATE
1814 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.derived
->attr
.private_comp
))
1816 sym
->attr
.private_comp
= 1;
1821 if (!seen_component
)
1822 sym
->attr
.zero_comp
= 1;
1828 /* Parse an ENUM. */
1837 int seen_enumerator
= 0;
1841 push_state (&s
, COMP_ENUM
, gfc_new_block
);
1845 while (compiling_enum
)
1847 st
= next_statement ();
1855 seen_enumerator
= 1;
1856 accept_statement (st
);
1861 if (!seen_enumerator
)
1863 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1866 accept_statement (st
);
1870 gfc_free_enum_history ();
1871 unexpected_statement (st
);
1879 /* Parse an interface. We must be able to deal with the possibility
1880 of recursive interfaces. The parse_spec() subroutine is mutually
1881 recursive with parse_interface(). */
1883 static gfc_statement
parse_spec (gfc_statement
);
1886 parse_interface (void)
1888 gfc_compile_state new_state
, current_state
;
1889 gfc_symbol
*prog_unit
, *sym
;
1890 gfc_interface_info save
;
1891 gfc_state_data s1
, s2
;
1895 accept_statement (ST_INTERFACE
);
1897 current_interface
.ns
= gfc_current_ns
;
1898 save
= current_interface
;
1900 sym
= (current_interface
.type
== INTERFACE_GENERIC
1901 || current_interface
.type
== INTERFACE_USER_OP
)
1902 ? gfc_new_block
: NULL
;
1904 push_state (&s1
, COMP_INTERFACE
, sym
);
1905 current_state
= COMP_NONE
;
1908 gfc_current_ns
= gfc_get_namespace (current_interface
.ns
, 0);
1910 st
= next_statement ();
1917 new_state
= COMP_SUBROUTINE
;
1918 gfc_add_explicit_interface (gfc_new_block
, IFSRC_IFBODY
,
1919 gfc_new_block
->formal
, NULL
);
1923 new_state
= COMP_FUNCTION
;
1924 gfc_add_explicit_interface (gfc_new_block
, IFSRC_IFBODY
,
1925 gfc_new_block
->formal
, NULL
);
1929 case ST_MODULE_PROC
: /* The module procedure matcher makes
1930 sure the context is correct. */
1931 accept_statement (st
);
1932 gfc_free_namespace (gfc_current_ns
);
1935 case ST_END_INTERFACE
:
1936 gfc_free_namespace (gfc_current_ns
);
1937 gfc_current_ns
= current_interface
.ns
;
1941 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1942 gfc_ascii_statement (st
));
1943 reject_statement ();
1944 gfc_free_namespace (gfc_current_ns
);
1949 /* Make sure that a generic interface has only subroutines or
1950 functions and that the generic name has the right attribute. */
1951 if (current_interface
.type
== INTERFACE_GENERIC
)
1953 if (current_state
== COMP_NONE
)
1955 if (new_state
== COMP_FUNCTION
)
1956 gfc_add_function (&sym
->attr
, sym
->name
, NULL
);
1957 else if (new_state
== COMP_SUBROUTINE
)
1958 gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
);
1960 current_state
= new_state
;
1964 if (new_state
!= current_state
)
1966 if (new_state
== COMP_SUBROUTINE
)
1967 gfc_error ("SUBROUTINE at %C does not belong in a "
1968 "generic function interface");
1970 if (new_state
== COMP_FUNCTION
)
1971 gfc_error ("FUNCTION at %C does not belong in a "
1972 "generic subroutine interface");
1977 if (current_interface
.type
== INTERFACE_ABSTRACT
)
1979 gfc_new_block
->attr
.abstract
= 1;
1980 if (gfc_is_intrinsic_typename (gfc_new_block
->name
))
1981 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
1982 "cannot be the same as an intrinsic type",
1983 gfc_new_block
->name
);
1986 push_state (&s2
, new_state
, gfc_new_block
);
1987 accept_statement (st
);
1988 prog_unit
= gfc_new_block
;
1989 prog_unit
->formal_ns
= gfc_current_ns
;
1990 proc_locus
= gfc_current_locus
;
1993 /* Read data declaration statements. */
1994 st
= parse_spec (ST_NONE
);
1996 /* Since the interface block does not permit an IMPLICIT statement,
1997 the default type for the function or the result must be taken
1998 from the formal namespace. */
1999 if (new_state
== COMP_FUNCTION
)
2001 if (prog_unit
->result
== prog_unit
2002 && prog_unit
->ts
.type
== BT_UNKNOWN
)
2003 gfc_set_default_type (prog_unit
, 1, prog_unit
->formal_ns
);
2004 else if (prog_unit
->result
!= prog_unit
2005 && prog_unit
->result
->ts
.type
== BT_UNKNOWN
)
2006 gfc_set_default_type (prog_unit
->result
, 1,
2007 prog_unit
->formal_ns
);
2010 if (st
!= ST_END_SUBROUTINE
&& st
!= ST_END_FUNCTION
)
2012 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2013 gfc_ascii_statement (st
));
2014 reject_statement ();
2018 current_interface
= save
;
2019 gfc_add_interface (prog_unit
);
2022 if (current_interface
.ns
2023 && current_interface
.ns
->proc_name
2024 && strcmp (current_interface
.ns
->proc_name
->name
,
2025 prog_unit
->name
) == 0)
2026 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2027 "enclosing procedure", prog_unit
->name
, &proc_locus
);
2036 /* Associate function characteristics by going back to the function
2037 declaration and rematching the prefix. */
2040 match_deferred_characteristics (gfc_typespec
* ts
)
2043 match m
= MATCH_ERROR
;
2044 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2046 loc
= gfc_current_locus
;
2048 gfc_current_locus
= gfc_current_block ()->declared_at
;
2051 gfc_buffer_error (1);
2052 m
= gfc_match_prefix (ts
);
2053 gfc_buffer_error (0);
2055 if (ts
->type
== BT_DERIVED
)
2059 if (!ts
->derived
|| !ts
->derived
->components
)
2063 /* Only permit one go at the characteristic association. */
2067 /* Set the function locus correctly. If we have not found the
2068 function name, there is an error. */
2069 gfc_match ("function% %n", name
);
2070 if (m
== MATCH_YES
&& strcmp (name
, gfc_current_block ()->name
) == 0)
2072 gfc_current_block ()->declared_at
= gfc_current_locus
;
2073 gfc_commit_symbols ();
2078 gfc_current_locus
=loc
;
2083 /* Parse a set of specification statements. Returns the statement
2084 that doesn't fit. */
2086 static gfc_statement
2087 parse_spec (gfc_statement st
)
2090 bool bad_characteristic
= false;
2093 verify_st_order (&ss
, ST_NONE
);
2095 st
= next_statement ();
2105 case ST_DATA
: /* Not allowed in interfaces */
2106 if (gfc_current_state () == COMP_INTERFACE
)
2113 case ST_IMPLICIT_NONE
:
2118 case ST_DERIVED_DECL
:
2120 if (verify_st_order (&ss
, st
) == FAILURE
)
2122 reject_statement ();
2123 st
= next_statement ();
2133 case ST_DERIVED_DECL
:
2139 if (gfc_current_state () != COMP_MODULE
)
2141 gfc_error ("%s statement must appear in a MODULE",
2142 gfc_ascii_statement (st
));
2146 if (gfc_current_ns
->default_access
!= ACCESS_UNKNOWN
)
2148 gfc_error ("%s statement at %C follows another accessibility "
2149 "specification", gfc_ascii_statement (st
));
2153 gfc_current_ns
->default_access
= (st
== ST_PUBLIC
)
2154 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
2158 case ST_STATEMENT_FUNCTION
:
2159 if (gfc_current_state () == COMP_MODULE
)
2161 unexpected_statement (st
);
2169 accept_statement (st
);
2170 st
= next_statement ();
2174 accept_statement (st
);
2176 st
= next_statement ();
2179 case ST_GET_FCN_CHARACTERISTICS
:
2180 /* This statement triggers the association of a function's result
2182 ts
= &gfc_current_block ()->result
->ts
;
2183 if (match_deferred_characteristics (ts
) != MATCH_YES
)
2184 bad_characteristic
= true;
2186 st
= next_statement ();
2193 /* If match_deferred_characteristics failed, then there is an error. */
2194 if (bad_characteristic
)
2196 ts
= &gfc_current_block ()->result
->ts
;
2197 if (ts
->type
!= BT_DERIVED
)
2198 gfc_error ("Bad kind expression for function '%s' at %L",
2199 gfc_current_block ()->name
,
2200 &gfc_current_block ()->declared_at
);
2202 gfc_error ("The type for function '%s' at %L is not accessible",
2203 gfc_current_block ()->name
,
2204 &gfc_current_block ()->declared_at
);
2206 gfc_current_block ()->ts
.kind
= 0;
2207 /* Keep the derived type; if it's bad, it will be discovered later. */
2208 if (!(ts
->type
== BT_DERIVED
&& ts
->derived
))
2209 ts
->type
= BT_UNKNOWN
;
2216 /* Parse a WHERE block, (not a simple WHERE statement). */
2219 parse_where_block (void)
2221 int seen_empty_else
;
2226 accept_statement (ST_WHERE_BLOCK
);
2227 top
= gfc_state_stack
->tail
;
2229 push_state (&s
, COMP_WHERE
, gfc_new_block
);
2231 d
= add_statement ();
2232 d
->expr
= top
->expr
;
2238 seen_empty_else
= 0;
2242 st
= next_statement ();
2248 case ST_WHERE_BLOCK
:
2249 parse_where_block ();
2254 accept_statement (st
);
2258 if (seen_empty_else
)
2260 gfc_error ("ELSEWHERE statement at %C follows previous "
2261 "unmasked ELSEWHERE");
2265 if (new_st
.expr
== NULL
)
2266 seen_empty_else
= 1;
2268 d
= new_level (gfc_state_stack
->head
);
2270 d
->expr
= new_st
.expr
;
2272 accept_statement (st
);
2277 accept_statement (st
);
2281 gfc_error ("Unexpected %s statement in WHERE block at %C",
2282 gfc_ascii_statement (st
));
2283 reject_statement ();
2287 while (st
!= ST_END_WHERE
);
2293 /* Parse a FORALL block (not a simple FORALL statement). */
2296 parse_forall_block (void)
2302 accept_statement (ST_FORALL_BLOCK
);
2303 top
= gfc_state_stack
->tail
;
2305 push_state (&s
, COMP_FORALL
, gfc_new_block
);
2307 d
= add_statement ();
2308 d
->op
= EXEC_FORALL
;
2313 st
= next_statement ();
2318 case ST_POINTER_ASSIGNMENT
:
2321 accept_statement (st
);
2324 case ST_WHERE_BLOCK
:
2325 parse_where_block ();
2328 case ST_FORALL_BLOCK
:
2329 parse_forall_block ();
2333 accept_statement (st
);
2340 gfc_error ("Unexpected %s statement in FORALL block at %C",
2341 gfc_ascii_statement (st
));
2343 reject_statement ();
2347 while (st
!= ST_END_FORALL
);
2353 static gfc_statement
parse_executable (gfc_statement
);
2355 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2358 parse_if_block (void)
2367 accept_statement (ST_IF_BLOCK
);
2369 top
= gfc_state_stack
->tail
;
2370 push_state (&s
, COMP_IF
, gfc_new_block
);
2372 new_st
.op
= EXEC_IF
;
2373 d
= add_statement ();
2375 d
->expr
= top
->expr
;
2381 st
= parse_executable (ST_NONE
);
2391 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2392 "statement at %L", &else_locus
);
2394 reject_statement ();
2398 d
= new_level (gfc_state_stack
->head
);
2400 d
->expr
= new_st
.expr
;
2402 accept_statement (st
);
2409 gfc_error ("Duplicate ELSE statements at %L and %C",
2411 reject_statement ();
2416 else_locus
= gfc_current_locus
;
2418 d
= new_level (gfc_state_stack
->head
);
2421 accept_statement (st
);
2429 unexpected_statement (st
);
2433 while (st
!= ST_ENDIF
);
2436 accept_statement (st
);
2440 /* Parse a SELECT block. */
2443 parse_select_block (void)
2449 accept_statement (ST_SELECT_CASE
);
2451 cp
= gfc_state_stack
->tail
;
2452 push_state (&s
, COMP_SELECT
, gfc_new_block
);
2454 /* Make sure that the next statement is a CASE or END SELECT. */
2457 st
= next_statement ();
2460 if (st
== ST_END_SELECT
)
2462 /* Empty SELECT CASE is OK. */
2463 accept_statement (st
);
2470 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2473 reject_statement ();
2476 /* At this point, we're got a nonempty select block. */
2477 cp
= new_level (cp
);
2480 accept_statement (st
);
2484 st
= parse_executable (ST_NONE
);
2491 cp
= new_level (gfc_state_stack
->head
);
2493 gfc_clear_new_st ();
2495 accept_statement (st
);
2501 /* Can't have an executable statement because of
2502 parse_executable(). */
2504 unexpected_statement (st
);
2508 while (st
!= ST_END_SELECT
);
2511 accept_statement (st
);
2515 /* Given a symbol, make sure it is not an iteration variable for a DO
2516 statement. This subroutine is called when the symbol is seen in a
2517 context that causes it to become redefined. If the symbol is an
2518 iterator, we generate an error message and return nonzero. */
2521 gfc_check_do_variable (gfc_symtree
*st
)
2525 for (s
=gfc_state_stack
; s
; s
= s
->previous
)
2526 if (s
->do_variable
== st
)
2528 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2529 "loop beginning at %L", st
->name
, &s
->head
->loc
);
2537 /* Checks to see if the current statement label closes an enddo.
2538 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2539 an error) if it incorrectly closes an ENDDO. */
2542 check_do_closure (void)
2546 if (gfc_statement_label
== NULL
)
2549 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
2550 if (p
->state
== COMP_DO
)
2554 return 0; /* No loops to close */
2556 if (p
->ext
.end_do_label
== gfc_statement_label
)
2559 if (p
== gfc_state_stack
)
2562 gfc_error ("End of nonblock DO statement at %C is within another block");
2566 /* At this point, the label doesn't terminate the innermost loop.
2567 Make sure it doesn't terminate another one. */
2568 for (; p
; p
= p
->previous
)
2569 if (p
->state
== COMP_DO
&& p
->ext
.end_do_label
== gfc_statement_label
)
2571 gfc_error ("End of nonblock DO statement at %C is interwoven "
2572 "with another DO loop");
2580 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2581 handled inside of parse_executable(), because they aren't really
2585 parse_do_block (void)
2592 s
.ext
.end_do_label
= new_st
.label
;
2594 if (new_st
.ext
.iterator
!= NULL
)
2595 stree
= new_st
.ext
.iterator
->var
->symtree
;
2599 accept_statement (ST_DO
);
2601 top
= gfc_state_stack
->tail
;
2602 push_state (&s
, COMP_DO
, gfc_new_block
);
2604 s
.do_variable
= stree
;
2606 top
->block
= new_level (top
);
2607 top
->block
->op
= EXEC_DO
;
2610 st
= parse_executable (ST_NONE
);
2618 if (s
.ext
.end_do_label
!= NULL
2619 && s
.ext
.end_do_label
!= gfc_statement_label
)
2620 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
2623 if (gfc_statement_label
!= NULL
)
2625 new_st
.op
= EXEC_NOP
;
2630 case ST_IMPLIED_ENDDO
:
2631 /* If the do-stmt of this DO construct has a do-construct-name,
2632 the corresponding end-do must be an end-do-stmt (with a matching
2633 name, but in that case we must have seen ST_ENDDO first).
2634 We only complain about this in pedantic mode. */
2635 if (gfc_current_block () != NULL
)
2636 gfc_error_now ("named block DO at %L requires matching ENDDO name",
2637 &gfc_current_block()->declared_at
);
2642 unexpected_statement (st
);
2647 accept_statement (st
);
2651 /* Parse the statements of OpenMP do/parallel do. */
2653 static gfc_statement
2654 parse_omp_do (gfc_statement omp_st
)
2660 accept_statement (omp_st
);
2662 cp
= gfc_state_stack
->tail
;
2663 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
2664 np
= new_level (cp
);
2670 st
= next_statement ();
2673 else if (st
== ST_DO
)
2676 unexpected_statement (st
);
2680 if (gfc_statement_label
!= NULL
2681 && gfc_state_stack
->previous
!= NULL
2682 && gfc_state_stack
->previous
->state
== COMP_DO
2683 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
2691 there should be no !$OMP END DO. */
2693 return ST_IMPLIED_ENDDO
;
2696 check_do_closure ();
2699 st
= next_statement ();
2700 if (st
== (omp_st
== ST_OMP_DO
? ST_OMP_END_DO
: ST_OMP_END_PARALLEL_DO
))
2702 if (new_st
.op
== EXEC_OMP_END_NOWAIT
)
2703 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
2705 gcc_assert (new_st
.op
== EXEC_NOP
);
2706 gfc_clear_new_st ();
2707 gfc_commit_symbols ();
2708 gfc_warning_check ();
2709 st
= next_statement ();
2715 /* Parse the statements of OpenMP atomic directive. */
2718 parse_omp_atomic (void)
2724 accept_statement (ST_OMP_ATOMIC
);
2726 cp
= gfc_state_stack
->tail
;
2727 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
2728 np
= new_level (cp
);
2734 st
= next_statement ();
2737 else if (st
== ST_ASSIGNMENT
)
2740 unexpected_statement (st
);
2743 accept_statement (st
);
2749 /* Parse the statements of an OpenMP structured block. */
2752 parse_omp_structured_block (gfc_statement omp_st
, bool workshare_stmts_only
)
2754 gfc_statement st
, omp_end_st
;
2758 accept_statement (omp_st
);
2760 cp
= gfc_state_stack
->tail
;
2761 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
2762 np
= new_level (cp
);
2768 case ST_OMP_PARALLEL
:
2769 omp_end_st
= ST_OMP_END_PARALLEL
;
2771 case ST_OMP_PARALLEL_SECTIONS
:
2772 omp_end_st
= ST_OMP_END_PARALLEL_SECTIONS
;
2774 case ST_OMP_SECTIONS
:
2775 omp_end_st
= ST_OMP_END_SECTIONS
;
2777 case ST_OMP_ORDERED
:
2778 omp_end_st
= ST_OMP_END_ORDERED
;
2780 case ST_OMP_CRITICAL
:
2781 omp_end_st
= ST_OMP_END_CRITICAL
;
2784 omp_end_st
= ST_OMP_END_MASTER
;
2787 omp_end_st
= ST_OMP_END_SINGLE
;
2789 case ST_OMP_WORKSHARE
:
2790 omp_end_st
= ST_OMP_END_WORKSHARE
;
2792 case ST_OMP_PARALLEL_WORKSHARE
:
2793 omp_end_st
= ST_OMP_END_PARALLEL_WORKSHARE
;
2801 if (workshare_stmts_only
)
2803 /* Inside of !$omp workshare, only
2806 where statements and constructs
2807 forall statements and constructs
2811 are allowed. For !$omp critical these
2812 restrictions apply recursively. */
2815 st
= next_statement ();
2826 accept_statement (st
);
2829 case ST_WHERE_BLOCK
:
2830 parse_where_block ();
2833 case ST_FORALL_BLOCK
:
2834 parse_forall_block ();
2837 case ST_OMP_PARALLEL
:
2838 case ST_OMP_PARALLEL_SECTIONS
:
2839 parse_omp_structured_block (st
, false);
2842 case ST_OMP_PARALLEL_WORKSHARE
:
2843 case ST_OMP_CRITICAL
:
2844 parse_omp_structured_block (st
, true);
2847 case ST_OMP_PARALLEL_DO
:
2848 st
= parse_omp_do (st
);
2852 parse_omp_atomic ();
2863 st
= next_statement ();
2867 st
= parse_executable (ST_NONE
);
2870 else if (st
== ST_OMP_SECTION
2871 && (omp_st
== ST_OMP_SECTIONS
2872 || omp_st
== ST_OMP_PARALLEL_SECTIONS
))
2874 np
= new_level (np
);
2878 else if (st
!= omp_end_st
)
2879 unexpected_statement (st
);
2881 while (st
!= omp_end_st
);
2885 case EXEC_OMP_END_NOWAIT
:
2886 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
2888 case EXEC_OMP_CRITICAL
:
2889 if (((cp
->ext
.omp_name
== NULL
) ^ (new_st
.ext
.omp_name
== NULL
))
2890 || (new_st
.ext
.omp_name
!= NULL
2891 && strcmp (cp
->ext
.omp_name
, new_st
.ext
.omp_name
) != 0))
2892 gfc_error ("Name after !$omp critical and !$omp end critical does "
2894 gfc_free (CONST_CAST (char *, new_st
.ext
.omp_name
));
2896 case EXEC_OMP_END_SINGLE
:
2897 cp
->ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
]
2898 = new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
];
2899 new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
] = NULL
;
2900 gfc_free_omp_clauses (new_st
.ext
.omp_clauses
);
2908 gfc_clear_new_st ();
2909 gfc_commit_symbols ();
2910 gfc_warning_check ();
2915 /* Accept a series of executable statements. We return the first
2916 statement that doesn't fit to the caller. Any block statements are
2917 passed on to the correct handler, which usually passes the buck
2920 static gfc_statement
2921 parse_executable (gfc_statement st
)
2926 st
= next_statement ();
2930 close_flag
= check_do_closure ();
2935 case ST_END_PROGRAM
:
2938 case ST_END_FUNCTION
:
2942 case ST_END_SUBROUTINE
:
2947 case ST_SELECT_CASE
:
2948 gfc_error ("%s statement at %C cannot terminate a non-block "
2949 "DO loop", gfc_ascii_statement (st
));
2965 accept_statement (st
);
2966 if (close_flag
== 1)
2967 return ST_IMPLIED_ENDDO
;
2974 case ST_SELECT_CASE
:
2975 parse_select_block ();
2980 if (check_do_closure () == 1)
2981 return ST_IMPLIED_ENDDO
;
2984 case ST_WHERE_BLOCK
:
2985 parse_where_block ();
2988 case ST_FORALL_BLOCK
:
2989 parse_forall_block ();
2992 case ST_OMP_PARALLEL
:
2993 case ST_OMP_PARALLEL_SECTIONS
:
2994 case ST_OMP_SECTIONS
:
2995 case ST_OMP_ORDERED
:
2996 case ST_OMP_CRITICAL
:
2999 parse_omp_structured_block (st
, false);
3002 case ST_OMP_WORKSHARE
:
3003 case ST_OMP_PARALLEL_WORKSHARE
:
3004 parse_omp_structured_block (st
, true);
3008 case ST_OMP_PARALLEL_DO
:
3009 st
= parse_omp_do (st
);
3010 if (st
== ST_IMPLIED_ENDDO
)
3015 parse_omp_atomic ();
3022 st
= next_statement ();
3027 /* Parse a series of contained program units. */
3029 static void parse_progunit (gfc_statement
);
3032 /* Fix the symbols for sibling functions. These are incorrectly added to
3033 the child namespace as the parser didn't know about this procedure. */
3036 gfc_fixup_sibling_symbols (gfc_symbol
*sym
, gfc_namespace
*siblings
)
3040 gfc_symbol
*old_sym
;
3042 sym
->attr
.referenced
= 1;
3043 for (ns
= siblings
; ns
; ns
= ns
->sibling
)
3045 gfc_find_sym_tree (sym
->name
, ns
, 0, &st
);
3047 if (!st
|| (st
->n
.sym
->attr
.dummy
&& ns
== st
->n
.sym
->ns
))
3050 old_sym
= st
->n
.sym
;
3051 if (old_sym
->ns
== ns
3052 && !old_sym
->attr
.contained
3054 /* By 14.6.1.3, host association should be excluded
3055 for the following. */
3056 && !(old_sym
->attr
.external
3057 || (old_sym
->ts
.type
!= BT_UNKNOWN
3058 && !old_sym
->attr
.implicit_type
)
3059 || old_sym
->attr
.flavor
== FL_PARAMETER
3060 || old_sym
->attr
.in_common
3061 || old_sym
->attr
.in_equivalence
3062 || old_sym
->attr
.data
3063 || old_sym
->attr
.dummy
3064 || old_sym
->attr
.result
3065 || old_sym
->attr
.dimension
3066 || old_sym
->attr
.allocatable
3067 || old_sym
->attr
.intrinsic
3068 || old_sym
->attr
.generic
3069 || old_sym
->attr
.flavor
== FL_NAMELIST
3070 || old_sym
->attr
.proc
== PROC_ST_FUNCTION
))
3072 /* Replace it with the symbol from the parent namespace. */
3076 /* Free the old (local) symbol. */
3078 if (old_sym
->refs
== 0)
3079 gfc_free_symbol (old_sym
);
3082 /* Do the same for any contained procedures. */
3083 gfc_fixup_sibling_symbols (sym
, ns
->contained
);
3088 parse_contained (int module
)
3090 gfc_namespace
*ns
, *parent_ns
, *tmp
;
3091 gfc_state_data s1
, s2
;
3095 int contains_statements
= 0;
3098 push_state (&s1
, COMP_CONTAINS
, NULL
);
3099 parent_ns
= gfc_current_ns
;
3103 gfc_current_ns
= gfc_get_namespace (parent_ns
, 1);
3105 gfc_current_ns
->sibling
= parent_ns
->contained
;
3106 parent_ns
->contained
= gfc_current_ns
;
3109 /* Process the next available statement. We come here if we got an error
3110 and rejected the last statement. */
3111 st
= next_statement ();
3120 contains_statements
= 1;
3121 accept_statement (st
);
3124 (st
== ST_FUNCTION
) ? COMP_FUNCTION
: COMP_SUBROUTINE
,
3127 /* For internal procedures, create/update the symbol in the
3128 parent namespace. */
3132 if (gfc_get_symbol (gfc_new_block
->name
, parent_ns
, &sym
))
3133 gfc_error ("Contained procedure '%s' at %C is already "
3134 "ambiguous", gfc_new_block
->name
);
3137 if (gfc_add_procedure (&sym
->attr
, PROC_INTERNAL
, sym
->name
,
3138 &gfc_new_block
->declared_at
) ==
3141 if (st
== ST_FUNCTION
)
3142 gfc_add_function (&sym
->attr
, sym
->name
,
3143 &gfc_new_block
->declared_at
);
3145 gfc_add_subroutine (&sym
->attr
, sym
->name
,
3146 &gfc_new_block
->declared_at
);
3150 gfc_commit_symbols ();
3153 sym
= gfc_new_block
;
3155 /* Mark this as a contained function, so it isn't replaced
3156 by other module functions. */
3157 sym
->attr
.contained
= 1;
3158 sym
->attr
.referenced
= 1;
3160 parse_progunit (ST_NONE
);
3162 /* Fix up any sibling functions that refer to this one. */
3163 gfc_fixup_sibling_symbols (sym
, gfc_current_ns
);
3164 /* Or refer to any of its alternate entry points. */
3165 for (el
= gfc_current_ns
->entries
; el
; el
= el
->next
)
3166 gfc_fixup_sibling_symbols (el
->sym
, gfc_current_ns
);
3168 gfc_current_ns
->code
= s2
.head
;
3169 gfc_current_ns
= parent_ns
;
3174 /* These statements are associated with the end of the host unit. */
3175 case ST_END_FUNCTION
:
3177 case ST_END_PROGRAM
:
3178 case ST_END_SUBROUTINE
:
3179 accept_statement (st
);
3183 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
3184 gfc_ascii_statement (st
));
3185 reject_statement ();
3191 while (st
!= ST_END_FUNCTION
&& st
!= ST_END_SUBROUTINE
3192 && st
!= ST_END_MODULE
&& st
!= ST_END_PROGRAM
);
3194 /* The first namespace in the list is guaranteed to not have
3195 anything (worthwhile) in it. */
3196 tmp
= gfc_current_ns
;
3197 gfc_current_ns
= parent_ns
;
3198 if (seen_error
&& tmp
->refs
> 1)
3199 gfc_free_namespace (tmp
);
3201 ns
= gfc_current_ns
->contained
;
3202 gfc_current_ns
->contained
= ns
->sibling
;
3203 gfc_free_namespace (ns
);
3206 if (!contains_statements
)
3207 gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: CONTAINS statement without "
3208 "FUNCTION or SUBROUTINE statement at %C");
3212 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
3215 parse_progunit (gfc_statement st
)
3220 st
= parse_spec (st
);
3230 accept_statement (st
);
3237 if (gfc_current_state () == COMP_FUNCTION
)
3238 gfc_check_function_type (gfc_current_ns
);
3243 st
= parse_executable (st
);
3254 accept_statement (st
);
3261 unexpected_statement (st
);
3262 reject_statement ();
3263 st
= next_statement ();
3269 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
3270 if (p
->state
== COMP_CONTAINS
)
3273 if (gfc_find_state (COMP_MODULE
) == SUCCESS
)
3278 gfc_error ("CONTAINS statement at %C is already in a contained "
3280 st
= next_statement ();
3284 parse_contained (0);
3287 gfc_current_ns
->code
= gfc_state_stack
->head
;
3291 /* Come here to complain about a global symbol already in use as
3295 gfc_global_used (gfc_gsymbol
*sym
, locus
*where
)
3300 where
= &gfc_current_locus
;
3310 case GSYM_SUBROUTINE
:
3311 name
= "SUBROUTINE";
3316 case GSYM_BLOCK_DATA
:
3317 name
= "BLOCK DATA";
3323 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
3327 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
3328 sym
->name
, where
, name
, &sym
->where
);
3332 /* Parse a block data program unit. */
3335 parse_block_data (void)
3338 static locus blank_locus
;
3339 static int blank_block
=0;
3342 gfc_current_ns
->proc_name
= gfc_new_block
;
3343 gfc_current_ns
->is_block_data
= 1;
3345 if (gfc_new_block
== NULL
)
3348 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3349 "prior BLOCK DATA at %L", &blank_locus
);
3353 blank_locus
= gfc_current_locus
;
3358 s
= gfc_get_gsymbol (gfc_new_block
->name
);
3360 || (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_BLOCK_DATA
))
3361 gfc_global_used(s
, NULL
);
3364 s
->type
= GSYM_BLOCK_DATA
;
3365 s
->where
= gfc_current_locus
;
3370 st
= parse_spec (ST_NONE
);
3372 while (st
!= ST_END_BLOCK_DATA
)
3374 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3375 gfc_ascii_statement (st
));
3376 reject_statement ();
3377 st
= next_statement ();
3382 /* Parse a module subprogram. */
3390 s
= gfc_get_gsymbol (gfc_new_block
->name
);
3391 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_MODULE
))
3392 gfc_global_used(s
, NULL
);
3395 s
->type
= GSYM_MODULE
;
3396 s
->where
= gfc_current_locus
;
3400 st
= parse_spec (ST_NONE
);
3409 parse_contained (1);
3413 accept_statement (st
);
3417 gfc_error ("Unexpected %s statement in MODULE at %C",
3418 gfc_ascii_statement (st
));
3420 reject_statement ();
3421 st
= next_statement ();
3427 /* Add a procedure name to the global symbol table. */
3430 add_global_procedure (int sub
)
3434 s
= gfc_get_gsymbol(gfc_new_block
->name
);
3437 || (s
->type
!= GSYM_UNKNOWN
3438 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
3439 gfc_global_used(s
, NULL
);
3442 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
3443 s
->where
= gfc_current_locus
;
3449 /* Add a program to the global symbol table. */
3452 add_global_program (void)
3456 if (gfc_new_block
== NULL
)
3458 s
= gfc_get_gsymbol (gfc_new_block
->name
);
3460 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_PROGRAM
))
3461 gfc_global_used(s
, NULL
);
3464 s
->type
= GSYM_PROGRAM
;
3465 s
->where
= gfc_current_locus
;
3471 /* Top level parser. */
3474 gfc_parse_file (void)
3476 int seen_program
, errors_before
, errors
;
3477 gfc_state_data top
, s
;
3481 gfc_start_source_files ();
3483 top
.state
= COMP_NONE
;
3485 top
.previous
= NULL
;
3486 top
.head
= top
.tail
= NULL
;
3487 top
.do_variable
= NULL
;
3489 gfc_state_stack
= &top
;
3491 gfc_clear_new_st ();
3493 gfc_statement_label
= NULL
;
3495 if (setjmp (eof_buf
))
3496 return FAILURE
; /* Come here on unexpected EOF */
3500 /* Exit early for empty files. */
3506 st
= next_statement ();
3515 goto duplicate_main
;
3517 prog_locus
= gfc_current_locus
;
3519 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
3520 main_program_symbol(gfc_current_ns
, gfc_new_block
->name
);
3521 accept_statement (st
);
3522 add_global_program ();
3523 parse_progunit (ST_NONE
);
3527 add_global_procedure (1);
3528 push_state (&s
, COMP_SUBROUTINE
, gfc_new_block
);
3529 accept_statement (st
);
3530 parse_progunit (ST_NONE
);
3534 add_global_procedure (0);
3535 push_state (&s
, COMP_FUNCTION
, gfc_new_block
);
3536 accept_statement (st
);
3537 parse_progunit (ST_NONE
);
3541 push_state (&s
, COMP_BLOCK_DATA
, gfc_new_block
);
3542 accept_statement (st
);
3543 parse_block_data ();
3547 push_state (&s
, COMP_MODULE
, gfc_new_block
);
3548 accept_statement (st
);
3550 gfc_get_errors (NULL
, &errors_before
);
3554 /* Anything else starts a nameless main program block. */
3557 goto duplicate_main
;
3559 prog_locus
= gfc_current_locus
;
3561 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
3562 main_program_symbol (gfc_current_ns
, "MAIN__");
3563 parse_progunit (st
);
3567 gfc_current_ns
->code
= s
.head
;
3569 gfc_resolve (gfc_current_ns
);
3571 /* Dump the parse tree if requested. */
3572 if (gfc_option
.dump_parse_tree
)
3573 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
3575 gfc_get_errors (NULL
, &errors
);
3576 if (s
.state
== COMP_MODULE
)
3578 gfc_dump_module (s
.sym
->name
, errors_before
== errors
);
3580 gfc_generate_module_code (gfc_current_ns
);
3585 gfc_generate_code (gfc_current_ns
);
3593 gfc_end_source_files ();
3597 /* If we see a duplicate main program, shut down. If the second
3598 instance is an implied main program, ie data decls or executable
3599 statements, we're in for lots of errors. */
3600 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus
);
3601 reject_statement ();