2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
31 /* Current statement label. Zero means no statement label. Because new_st
32 can get wiped during statement matching, we have to keep it separate. */
34 gfc_st_label
*gfc_statement_label
;
36 static locus label_locus
;
37 static jmp_buf eof_buf
;
39 gfc_state_data
*gfc_state_stack
;
41 /* TODO: Re-order functions to kill these forward decls. */
42 static void check_statement_label (gfc_statement
);
43 static void undo_new_statement (void);
44 static void reject_statement (void);
47 /* A sort of half-matching function. We try to match the word on the
48 input with the passed string. If this succeeds, we call the
49 keyword-dependent matching function that will match the rest of the
50 statement. For single keywords, the matching subroutine is
54 match_word (const char *str
, match (*subr
) (void), locus
*old_locus
)
69 gfc_current_locus
= *old_locus
;
77 /* Figure out what the next statement is, (mostly) regardless of
78 proper ordering. The do...while(0) is there to prevent if/else
81 #define match(keyword, subr, st) \
83 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
86 undo_new_statement (); \
90 /* This is a specialist version of decode_statement that is used
91 for the specification statements in a function, whose
92 characteristics are deferred into the specification statements.
93 eg.: INTEGER (king = mykind) foo ()
94 USE mymodule, ONLY mykind.....
95 The KIND parameter needs a return after USE or IMPORT, whereas
96 derived type declarations can occur anywhere, up the executable
97 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
98 out of the correct kind of specification statements. */
100 decode_specification_statement (void)
106 if (gfc_match_eos () == MATCH_YES
)
109 old_locus
= gfc_current_locus
;
111 match ("import", gfc_match_import
, ST_IMPORT
);
112 match ("use", gfc_match_use
, ST_USE
);
114 if (gfc_current_block ()->ts
.type
!= BT_DERIVED
)
117 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
118 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
119 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
121 /* General statement matching: Instead of testing every possible
122 statement, we eliminate most possibilities by peeking at the
125 c
= gfc_peek_ascii_char ();
130 match ("abstract% interface", gfc_match_abstract_interface
,
135 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
142 match ("data", gfc_match_data
, ST_DATA
);
143 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
147 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
148 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
149 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
150 match ("external", gfc_match_external
, ST_ATTR_DECL
);
154 match ("format", gfc_match_format
, ST_FORMAT
);
161 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
162 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
163 match ("interface", gfc_match_interface
, ST_INTERFACE
);
164 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
165 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
172 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
176 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
180 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
181 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
182 if (gfc_match_private (&st
) == MATCH_YES
)
184 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
185 if (gfc_match_public (&st
) == MATCH_YES
)
187 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
194 match ("save", gfc_match_save
, ST_ATTR_DECL
);
198 match ("target", gfc_match_target
, ST_ATTR_DECL
);
199 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
206 match ("value", gfc_match_value
, ST_ATTR_DECL
);
207 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
214 /* This is not a specification statement. See if any of the matchers
215 has stored an error message of some sort. */
219 gfc_buffer_error (0);
220 gfc_current_locus
= old_locus
;
222 return ST_GET_FCN_CHARACTERISTICS
;
226 /* This is the primary 'decode_statement'. */
228 decode_statement (void)
239 gfc_clear_error (); /* Clear any pending errors. */
240 gfc_clear_warning (); /* Clear any pending warnings. */
242 gfc_matching_function
= false;
244 if (gfc_match_eos () == MATCH_YES
)
247 if (gfc_current_state () == COMP_FUNCTION
248 && gfc_current_block ()->result
->ts
.kind
== -1)
249 return decode_specification_statement ();
251 old_locus
= gfc_current_locus
;
253 /* Try matching a data declaration or function declaration. The
254 input "REALFUNCTIONA(N)" can mean several things in different
255 contexts, so it (and its relatives) get special treatment. */
257 if (gfc_current_state () == COMP_NONE
258 || gfc_current_state () == COMP_INTERFACE
259 || gfc_current_state () == COMP_CONTAINS
)
261 gfc_matching_function
= true;
262 m
= gfc_match_function_decl ();
265 else if (m
== MATCH_ERROR
)
269 gfc_current_locus
= old_locus
;
271 gfc_matching_function
= false;
274 /* Match statements whose error messages are meant to be overwritten
275 by something better. */
277 match (NULL
, gfc_match_assignment
, ST_ASSIGNMENT
);
278 match (NULL
, gfc_match_pointer_assignment
, ST_POINTER_ASSIGNMENT
);
279 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
281 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
282 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
284 /* Try to match a subroutine statement, which has the same optional
285 prefixes that functions can have. */
287 if (gfc_match_subroutine () == MATCH_YES
)
288 return ST_SUBROUTINE
;
290 gfc_current_locus
= old_locus
;
292 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
293 might begin with a block label. The match functions for these
294 statements are unusual in that their keyword is not seen before
295 the matcher is called. */
297 if (gfc_match_if (&st
) == MATCH_YES
)
300 gfc_current_locus
= old_locus
;
302 if (gfc_match_where (&st
) == MATCH_YES
)
305 gfc_current_locus
= old_locus
;
307 if (gfc_match_forall (&st
) == MATCH_YES
)
310 gfc_current_locus
= old_locus
;
312 match (NULL
, gfc_match_do
, ST_DO
);
313 match (NULL
, gfc_match_select
, ST_SELECT_CASE
);
315 /* General statement matching: Instead of testing every possible
316 statement, we eliminate most possibilities by peeking at the
319 c
= gfc_peek_ascii_char ();
324 match ("abstract% interface", gfc_match_abstract_interface
,
326 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
);
327 match ("allocatable", gfc_match_allocatable
, ST_ATTR_DECL
);
328 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
);
332 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
);
333 match ("block data", gfc_match_block_data
, ST_BLOCK_DATA
);
334 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
338 match ("call", gfc_match_call
, ST_CALL
);
339 match ("close", gfc_match_close
, ST_CLOSE
);
340 match ("continue", gfc_match_continue
, ST_CONTINUE
);
341 match ("cycle", gfc_match_cycle
, ST_CYCLE
);
342 match ("case", gfc_match_case
, ST_CASE
);
343 match ("common", gfc_match_common
, ST_COMMON
);
344 match ("contains", gfc_match_eos
, ST_CONTAINS
);
348 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
);
349 match ("data", gfc_match_data
, ST_DATA
);
350 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
354 match ("end file", gfc_match_endfile
, ST_END_FILE
);
355 match ("exit", gfc_match_exit
, ST_EXIT
);
356 match ("else", gfc_match_else
, ST_ELSE
);
357 match ("else where", gfc_match_elsewhere
, ST_ELSEWHERE
);
358 match ("else if", gfc_match_elseif
, ST_ELSEIF
);
359 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
361 if (gfc_match_end (&st
) == MATCH_YES
)
364 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
365 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
366 match ("external", gfc_match_external
, ST_ATTR_DECL
);
370 match ("final", gfc_match_final_decl
, ST_FINAL
);
371 match ("flush", gfc_match_flush
, ST_FLUSH
);
372 match ("format", gfc_match_format
, ST_FORMAT
);
376 match ("generic", gfc_match_generic
, ST_GENERIC
);
377 match ("go to", gfc_match_goto
, ST_GOTO
);
381 match ("inquire", gfc_match_inquire
, ST_INQUIRE
);
382 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
383 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
384 match ("import", gfc_match_import
, ST_IMPORT
);
385 match ("interface", gfc_match_interface
, ST_INTERFACE
);
386 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
387 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
391 match ("module% procedure% ", gfc_match_modproc
, ST_MODULE_PROC
);
392 match ("module", gfc_match_module
, ST_MODULE
);
396 match ("nullify", gfc_match_nullify
, ST_NULLIFY
);
397 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
401 match ("open", gfc_match_open
, ST_OPEN
);
402 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
406 match ("print", gfc_match_print
, ST_WRITE
);
407 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
408 match ("pause", gfc_match_pause
, ST_PAUSE
);
409 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
410 if (gfc_match_private (&st
) == MATCH_YES
)
412 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
413 match ("program", gfc_match_program
, ST_PROGRAM
);
414 if (gfc_match_public (&st
) == MATCH_YES
)
416 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
420 match ("read", gfc_match_read
, ST_READ
);
421 match ("return", gfc_match_return
, ST_RETURN
);
422 match ("rewind", gfc_match_rewind
, ST_REWIND
);
426 match ("sequence", gfc_match_eos
, ST_SEQUENCE
);
427 match ("stop", gfc_match_stop
, ST_STOP
);
428 match ("save", gfc_match_save
, ST_ATTR_DECL
);
432 match ("target", gfc_match_target
, ST_ATTR_DECL
);
433 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
437 match ("use", gfc_match_use
, ST_USE
);
441 match ("value", gfc_match_value
, ST_ATTR_DECL
);
442 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
446 match ("wait", gfc_match_wait
, ST_WAIT
);
447 match ("write", gfc_match_write
, ST_WRITE
);
451 /* All else has failed, so give up. See if any of the matchers has
452 stored an error message of some sort. */
454 if (gfc_error_check () == 0)
455 gfc_error_now ("Unclassifiable statement at %C");
459 gfc_error_recovery ();
465 decode_omp_directive (void)
474 gfc_clear_error (); /* Clear any pending errors. */
475 gfc_clear_warning (); /* Clear any pending warnings. */
479 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
480 "or ELEMENTAL procedures");
481 gfc_error_recovery ();
485 old_locus
= gfc_current_locus
;
487 /* General OpenMP directive matching: Instead of testing every possible
488 statement, we eliminate most possibilities by peeking at the
491 c
= gfc_peek_ascii_char ();
496 match ("atomic", gfc_match_omp_atomic
, ST_OMP_ATOMIC
);
499 match ("barrier", gfc_match_omp_barrier
, ST_OMP_BARRIER
);
502 match ("critical", gfc_match_omp_critical
, ST_OMP_CRITICAL
);
505 match ("do", gfc_match_omp_do
, ST_OMP_DO
);
508 match ("end critical", gfc_match_omp_critical
, ST_OMP_END_CRITICAL
);
509 match ("end do", gfc_match_omp_end_nowait
, ST_OMP_END_DO
);
510 match ("end master", gfc_match_omp_eos
, ST_OMP_END_MASTER
);
511 match ("end ordered", gfc_match_omp_eos
, ST_OMP_END_ORDERED
);
512 match ("end parallel do", gfc_match_omp_eos
, ST_OMP_END_PARALLEL_DO
);
513 match ("end parallel sections", gfc_match_omp_eos
,
514 ST_OMP_END_PARALLEL_SECTIONS
);
515 match ("end parallel workshare", gfc_match_omp_eos
,
516 ST_OMP_END_PARALLEL_WORKSHARE
);
517 match ("end parallel", gfc_match_omp_eos
, ST_OMP_END_PARALLEL
);
518 match ("end sections", gfc_match_omp_end_nowait
, ST_OMP_END_SECTIONS
);
519 match ("end single", gfc_match_omp_end_single
, ST_OMP_END_SINGLE
);
520 match ("end task", gfc_match_omp_eos
, ST_OMP_END_TASK
);
521 match ("end workshare", gfc_match_omp_end_nowait
,
522 ST_OMP_END_WORKSHARE
);
525 match ("flush", gfc_match_omp_flush
, ST_OMP_FLUSH
);
528 match ("master", gfc_match_omp_master
, ST_OMP_MASTER
);
531 match ("ordered", gfc_match_omp_ordered
, ST_OMP_ORDERED
);
534 match ("parallel do", gfc_match_omp_parallel_do
, ST_OMP_PARALLEL_DO
);
535 match ("parallel sections", gfc_match_omp_parallel_sections
,
536 ST_OMP_PARALLEL_SECTIONS
);
537 match ("parallel workshare", gfc_match_omp_parallel_workshare
,
538 ST_OMP_PARALLEL_WORKSHARE
);
539 match ("parallel", gfc_match_omp_parallel
, ST_OMP_PARALLEL
);
542 match ("sections", gfc_match_omp_sections
, ST_OMP_SECTIONS
);
543 match ("section", gfc_match_omp_eos
, ST_OMP_SECTION
);
544 match ("single", gfc_match_omp_single
, ST_OMP_SINGLE
);
547 match ("task", gfc_match_omp_task
, ST_OMP_TASK
);
548 match ("taskwait", gfc_match_omp_taskwait
, ST_OMP_TASKWAIT
);
549 match ("threadprivate", gfc_match_omp_threadprivate
,
550 ST_OMP_THREADPRIVATE
);
552 match ("workshare", gfc_match_omp_workshare
, ST_OMP_WORKSHARE
);
556 /* All else has failed, so give up. See if any of the matchers has
557 stored an error message of some sort. */
559 if (gfc_error_check () == 0)
560 gfc_error_now ("Unclassifiable OpenMP directive at %C");
564 gfc_error_recovery ();
572 /* Get the next statement in free form source. */
581 at_bol
= gfc_at_bol ();
582 gfc_gobble_whitespace ();
584 c
= gfc_peek_ascii_char ();
590 /* Found a statement label? */
591 m
= gfc_match_st_label (&gfc_statement_label
);
593 d
= gfc_peek_ascii_char ();
594 if (m
!= MATCH_YES
|| !gfc_is_whitespace (d
))
596 gfc_match_small_literal_int (&i
, &cnt
);
599 gfc_error_now ("Too many digits in statement label at %C");
602 gfc_error_now ("Zero is not a valid statement label at %C");
605 c
= gfc_next_ascii_char ();
608 if (!gfc_is_whitespace (c
))
609 gfc_error_now ("Non-numeric character in statement label at %C");
615 label_locus
= gfc_current_locus
;
617 gfc_gobble_whitespace ();
619 if (at_bol
&& gfc_peek_ascii_char () == ';')
621 gfc_error_now ("Semicolon at %C needs to be preceded by "
623 gfc_next_ascii_char (); /* Eat up the semicolon. */
627 if (gfc_match_eos () == MATCH_YES
)
629 gfc_warning_now ("Ignoring statement label in empty statement "
631 gfc_free_st_label (gfc_statement_label
);
632 gfc_statement_label
= NULL
;
639 /* Comments have already been skipped by the time we get here,
640 except for OpenMP directives. */
641 if (gfc_option
.flag_openmp
)
645 c
= gfc_next_ascii_char ();
646 for (i
= 0; i
< 5; i
++, c
= gfc_next_ascii_char ())
647 gcc_assert (c
== "!$omp"[i
]);
649 gcc_assert (c
== ' ' || c
== '\t');
650 gfc_gobble_whitespace ();
651 return decode_omp_directive ();
655 if (at_bol
&& c
== ';')
657 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
658 gfc_next_ascii_char (); /* Eat up the semicolon. */
662 return decode_statement ();
666 /* Get the next statement in fixed-form source. */
671 int label
, digit_flag
, i
;
676 return decode_statement ();
678 /* Skip past the current label field, parsing a statement label if
679 one is there. This is a weird number parser, since the number is
680 contained within five columns and can have any kind of embedded
681 spaces. We also check for characters that make the rest of the
687 for (i
= 0; i
< 5; i
++)
689 c
= gfc_next_char_literal (0);
706 label
= label
* 10 + ((unsigned char) c
- '0');
707 label_locus
= gfc_current_locus
;
711 /* Comments have already been skipped by the time we get
712 here, except for OpenMP directives. */
714 if (gfc_option
.flag_openmp
)
716 for (i
= 0; i
< 5; i
++, c
= gfc_next_char_literal (0))
717 gcc_assert ((char) gfc_wide_tolower (c
) == "*$omp"[i
]);
719 if (c
!= ' ' && c
!= '0')
721 gfc_buffer_error (0);
722 gfc_error ("Bad continuation line at %C");
726 return decode_omp_directive ();
730 /* Comments have already been skipped by the time we get
731 here so don't bother checking for them. */
734 gfc_buffer_error (0);
735 gfc_error ("Non-numeric character in statement label at %C");
743 gfc_warning_now ("Zero is not a valid statement label at %C");
746 /* We've found a valid statement label. */
747 gfc_statement_label
= gfc_get_st_label (label
);
751 /* Since this line starts a statement, it cannot be a continuation
752 of a previous statement. If we see something here besides a
753 space or zero, it must be a bad continuation line. */
755 c
= gfc_next_char_literal (0);
759 if (c
!= ' ' && c
!= '0')
761 gfc_buffer_error (0);
762 gfc_error ("Bad continuation line at %C");
766 /* Now that we've taken care of the statement label columns, we have
767 to make sure that the first nonblank character is not a '!'. If
768 it is, the rest of the line is a comment. */
772 loc
= gfc_current_locus
;
773 c
= gfc_next_char_literal (0);
775 while (gfc_is_whitespace (c
));
779 gfc_current_locus
= loc
;
783 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
787 if (gfc_match_eos () == MATCH_YES
)
790 /* At this point, we've got a nonblank statement to parse. */
791 return decode_statement ();
795 gfc_warning ("Ignoring statement label in empty statement at %C");
801 /* Return the next non-ST_NONE statement to the caller. We also worry
802 about including files and the ends of include files at this stage. */
805 next_statement (void)
809 gfc_new_block
= NULL
;
811 gfc_current_ns
->old_cl_list
= gfc_current_ns
->cl_list
;
814 gfc_statement_label
= NULL
;
815 gfc_buffer_error (1);
819 if ((gfc_option
.warn_line_truncation
|| gfc_current_form
== FORM_FREE
)
820 && gfc_current_locus
.lb
821 && gfc_current_locus
.lb
->truncated
)
822 gfc_warning_now ("Line truncated at %C");
827 gfc_skip_comments ();
835 if (gfc_define_undef_line ())
838 old_locus
= gfc_current_locus
;
840 st
= (gfc_current_form
== FORM_FIXED
) ? next_fixed () : next_free ();
846 gfc_buffer_error (0);
848 if (st
== ST_GET_FCN_CHARACTERISTICS
&& gfc_statement_label
!= NULL
)
850 gfc_free_st_label (gfc_statement_label
);
851 gfc_statement_label
= NULL
;
852 gfc_current_locus
= old_locus
;
856 check_statement_label (st
);
862 /****************************** Parser ***********************************/
864 /* The parser subroutines are of type 'try' that fail if the file ends
867 /* Macros that expand to case-labels for various classes of
868 statements. Start with executable statements that directly do
871 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
872 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
873 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
874 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
875 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
876 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
877 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
878 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
879 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT
881 /* Statements that mark other executable statements. */
883 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
884 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
885 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
886 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
887 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
888 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
891 /* Declaration statements */
893 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
894 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
895 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
898 /* Block end statements. Errors associated with interchanging these
899 are detected in gfc_match_end(). */
901 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
902 case ST_END_PROGRAM: case ST_END_SUBROUTINE
905 /* Push a new state onto the stack. */
908 push_state (gfc_state_data
*p
, gfc_compile_state new_state
, gfc_symbol
*sym
)
910 p
->state
= new_state
;
911 p
->previous
= gfc_state_stack
;
913 p
->head
= p
->tail
= NULL
;
914 p
->do_variable
= NULL
;
919 /* Pop the current state. */
923 gfc_state_stack
= gfc_state_stack
->previous
;
927 /* Try to find the given state in the state stack. */
930 gfc_find_state (gfc_compile_state state
)
934 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
935 if (p
->state
== state
)
938 return (p
== NULL
) ? FAILURE
: SUCCESS
;
942 /* Starts a new level in the statement list. */
945 new_level (gfc_code
*q
)
949 p
= q
->block
= gfc_get_code ();
951 gfc_state_stack
->head
= gfc_state_stack
->tail
= p
;
957 /* Add the current new_st code structure and adds it to the current
958 program unit. As a side-effect, it zeroes the new_st. */
968 p
->loc
= gfc_current_locus
;
970 if (gfc_state_stack
->head
== NULL
)
971 gfc_state_stack
->head
= p
;
973 gfc_state_stack
->tail
->next
= p
;
975 while (p
->next
!= NULL
)
978 gfc_state_stack
->tail
= p
;
986 /* Frees everything associated with the current statement. */
989 undo_new_statement (void)
991 gfc_free_statements (new_st
.block
);
992 gfc_free_statements (new_st
.next
);
993 gfc_free_statement (&new_st
);
998 /* If the current statement has a statement label, make sure that it
999 is allowed to, or should have one. */
1002 check_statement_label (gfc_statement st
)
1006 if (gfc_statement_label
== NULL
)
1008 if (st
== ST_FORMAT
)
1009 gfc_error ("FORMAT statement at %L does not have a statement label",
1016 case ST_END_PROGRAM
:
1017 case ST_END_FUNCTION
:
1018 case ST_END_SUBROUTINE
:
1024 type
= ST_LABEL_TARGET
;
1028 type
= ST_LABEL_FORMAT
;
1031 /* Statement labels are not restricted from appearing on a
1032 particular line. However, there are plenty of situations
1033 where the resulting label can't be referenced. */
1036 type
= ST_LABEL_BAD_TARGET
;
1040 gfc_define_st_label (gfc_statement_label
, type
, &label_locus
);
1042 new_st
.here
= gfc_statement_label
;
1046 /* Figures out what the enclosing program unit is. This will be a
1047 function, subroutine, program, block data or module. */
1050 gfc_enclosing_unit (gfc_compile_state
* result
)
1054 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1055 if (p
->state
== COMP_FUNCTION
|| p
->state
== COMP_SUBROUTINE
1056 || p
->state
== COMP_MODULE
|| p
->state
== COMP_BLOCK_DATA
1057 || p
->state
== COMP_PROGRAM
)
1066 *result
= COMP_PROGRAM
;
1071 /* Translate a statement enum to a string. */
1074 gfc_ascii_statement (gfc_statement st
)
1080 case ST_ARITHMETIC_IF
:
1081 p
= _("arithmetic IF");
1087 p
= _("attribute declaration");
1117 p
= _("data declaration");
1125 case ST_DERIVED_DECL
:
1126 p
= _("derived type declaration");
1140 case ST_END_BLOCK_DATA
:
1141 p
= "END BLOCK DATA";
1152 case ST_END_FUNCTION
:
1158 case ST_END_INTERFACE
:
1159 p
= "END INTERFACE";
1164 case ST_END_PROGRAM
:
1170 case ST_END_SUBROUTINE
:
1171 p
= "END SUBROUTINE";
1182 case ST_EQUIVALENCE
:
1191 case ST_FORALL_BLOCK
: /* Fall through */
1213 case ST_IMPLICIT_NONE
:
1214 p
= "IMPLICIT NONE";
1216 case ST_IMPLIED_ENDDO
:
1217 p
= _("implied END DO");
1243 case ST_MODULE_PROC
:
1244 p
= "MODULE PROCEDURE";
1282 case ST_WHERE_BLOCK
: /* Fall through */
1293 p
= _("assignment");
1295 case ST_POINTER_ASSIGNMENT
:
1296 p
= _("pointer assignment");
1298 case ST_SELECT_CASE
:
1307 case ST_STATEMENT_FUNCTION
:
1308 p
= "STATEMENT FUNCTION";
1310 case ST_LABEL_ASSIGNMENT
:
1311 p
= "LABEL ASSIGNMENT";
1314 p
= "ENUM DEFINITION";
1317 p
= "ENUMERATOR DEFINITION";
1325 case ST_OMP_BARRIER
:
1326 p
= "!$OMP BARRIER";
1328 case ST_OMP_CRITICAL
:
1329 p
= "!$OMP CRITICAL";
1334 case ST_OMP_END_CRITICAL
:
1335 p
= "!$OMP END CRITICAL";
1340 case ST_OMP_END_MASTER
:
1341 p
= "!$OMP END MASTER";
1343 case ST_OMP_END_ORDERED
:
1344 p
= "!$OMP END ORDERED";
1346 case ST_OMP_END_PARALLEL
:
1347 p
= "!$OMP END PARALLEL";
1349 case ST_OMP_END_PARALLEL_DO
:
1350 p
= "!$OMP END PARALLEL DO";
1352 case ST_OMP_END_PARALLEL_SECTIONS
:
1353 p
= "!$OMP END PARALLEL SECTIONS";
1355 case ST_OMP_END_PARALLEL_WORKSHARE
:
1356 p
= "!$OMP END PARALLEL WORKSHARE";
1358 case ST_OMP_END_SECTIONS
:
1359 p
= "!$OMP END SECTIONS";
1361 case ST_OMP_END_SINGLE
:
1362 p
= "!$OMP END SINGLE";
1364 case ST_OMP_END_TASK
:
1365 p
= "!$OMP END TASK";
1367 case ST_OMP_END_WORKSHARE
:
1368 p
= "!$OMP END WORKSHARE";
1376 case ST_OMP_ORDERED
:
1377 p
= "!$OMP ORDERED";
1379 case ST_OMP_PARALLEL
:
1380 p
= "!$OMP PARALLEL";
1382 case ST_OMP_PARALLEL_DO
:
1383 p
= "!$OMP PARALLEL DO";
1385 case ST_OMP_PARALLEL_SECTIONS
:
1386 p
= "!$OMP PARALLEL SECTIONS";
1388 case ST_OMP_PARALLEL_WORKSHARE
:
1389 p
= "!$OMP PARALLEL WORKSHARE";
1391 case ST_OMP_SECTIONS
:
1392 p
= "!$OMP SECTIONS";
1394 case ST_OMP_SECTION
:
1395 p
= "!$OMP SECTION";
1403 case ST_OMP_TASKWAIT
:
1404 p
= "!$OMP TASKWAIT";
1406 case ST_OMP_THREADPRIVATE
:
1407 p
= "!$OMP THREADPRIVATE";
1409 case ST_OMP_WORKSHARE
:
1410 p
= "!$OMP WORKSHARE";
1413 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1420 /* Create a symbol for the main program and assign it to ns->proc_name. */
1423 main_program_symbol (gfc_namespace
*ns
, const char *name
)
1425 gfc_symbol
*main_program
;
1426 symbol_attribute attr
;
1428 gfc_get_symbol (name
, ns
, &main_program
);
1429 gfc_clear_attr (&attr
);
1430 attr
.flavor
= FL_PROGRAM
;
1431 attr
.proc
= PROC_UNKNOWN
;
1432 attr
.subroutine
= 1;
1433 attr
.access
= ACCESS_PUBLIC
;
1434 attr
.is_main_program
= 1;
1435 main_program
->attr
= attr
;
1436 main_program
->declared_at
= gfc_current_locus
;
1437 ns
->proc_name
= main_program
;
1438 gfc_commit_symbols ();
1442 /* Do whatever is necessary to accept the last statement. */
1445 accept_statement (gfc_statement st
)
1453 case ST_IMPLICIT_NONE
:
1454 gfc_set_implicit_none ();
1463 gfc_current_ns
->proc_name
= gfc_new_block
;
1466 /* If the statement is the end of a block, lay down a special code
1467 that allows a branch to the end of the block from within the
1472 if (gfc_statement_label
!= NULL
)
1474 new_st
.op
= EXEC_NOP
;
1480 /* The end-of-program unit statements do not get the special
1481 marker and require a statement of some sort if they are a
1484 case ST_END_PROGRAM
:
1485 case ST_END_FUNCTION
:
1486 case ST_END_SUBROUTINE
:
1487 if (gfc_statement_label
!= NULL
)
1489 new_st
.op
= EXEC_RETURN
;
1505 gfc_commit_symbols ();
1506 gfc_warning_check ();
1507 gfc_clear_new_st ();
1511 /* Undo anything tentative that has been built for the current
1515 reject_statement (void)
1517 /* Revert to the previous charlen chain. */
1518 gfc_free_charlen (gfc_current_ns
->cl_list
, gfc_current_ns
->old_cl_list
);
1519 gfc_current_ns
->cl_list
= gfc_current_ns
->old_cl_list
;
1521 gfc_new_block
= NULL
;
1522 gfc_undo_symbols ();
1523 gfc_clear_warning ();
1524 undo_new_statement ();
1528 /* Generic complaint about an out of order statement. We also do
1529 whatever is necessary to clean up. */
1532 unexpected_statement (gfc_statement st
)
1534 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st
));
1536 reject_statement ();
1540 /* Given the next statement seen by the matcher, make sure that it is
1541 in proper order with the last. This subroutine is initialized by
1542 calling it with an argument of ST_NONE. If there is a problem, we
1543 issue an error and return FAILURE. Otherwise we return SUCCESS.
1545 Individual parsers need to verify that the statements seen are
1546 valid before calling here, i.e., ENTRY statements are not allowed in
1547 INTERFACE blocks. The following diagram is taken from the standard:
1549 +---------------------------------------+
1550 | program subroutine function module |
1551 +---------------------------------------+
1553 +---------------------------------------+
1555 +---------------------------------------+
1557 | +-----------+------------------+
1558 | | parameter | implicit |
1559 | +-----------+------------------+
1560 | format | | derived type |
1561 | entry | parameter | interface |
1562 | | data | specification |
1563 | | | statement func |
1564 | +-----------+------------------+
1565 | | data | executable |
1566 +--------+-----------+------------------+
1568 +---------------------------------------+
1569 | internal module/subprogram |
1570 +---------------------------------------+
1572 +---------------------------------------+
1579 { ORDER_START
, ORDER_USE
, ORDER_IMPORT
, ORDER_IMPLICIT_NONE
,
1580 ORDER_IMPLICIT
, ORDER_SPEC
, ORDER_EXEC
1583 gfc_statement last_statement
;
1589 verify_st_order (st_state
*p
, gfc_statement st
, bool silent
)
1595 p
->state
= ORDER_START
;
1599 if (p
->state
> ORDER_USE
)
1601 p
->state
= ORDER_USE
;
1605 if (p
->state
> ORDER_IMPORT
)
1607 p
->state
= ORDER_IMPORT
;
1610 case ST_IMPLICIT_NONE
:
1611 if (p
->state
> ORDER_IMPLICIT_NONE
)
1614 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1615 statement disqualifies a USE but not an IMPLICIT NONE.
1616 Duplicate IMPLICIT NONEs are caught when the implicit types
1619 p
->state
= ORDER_IMPLICIT_NONE
;
1623 if (p
->state
> ORDER_IMPLICIT
)
1625 p
->state
= ORDER_IMPLICIT
;
1630 if (p
->state
< ORDER_IMPLICIT_NONE
)
1631 p
->state
= ORDER_IMPLICIT_NONE
;
1635 if (p
->state
>= ORDER_EXEC
)
1637 if (p
->state
< ORDER_IMPLICIT
)
1638 p
->state
= ORDER_IMPLICIT
;
1642 if (p
->state
< ORDER_SPEC
)
1643 p
->state
= ORDER_SPEC
;
1648 case ST_DERIVED_DECL
:
1650 if (p
->state
>= ORDER_EXEC
)
1652 if (p
->state
< ORDER_SPEC
)
1653 p
->state
= ORDER_SPEC
;
1658 if (p
->state
< ORDER_EXEC
)
1659 p
->state
= ORDER_EXEC
;
1663 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1664 gfc_ascii_statement (st
));
1667 /* All is well, record the statement in case we need it next time. */
1668 p
->where
= gfc_current_locus
;
1669 p
->last_statement
= st
;
1674 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1675 gfc_ascii_statement (st
),
1676 gfc_ascii_statement (p
->last_statement
), &p
->where
);
1682 /* Handle an unexpected end of file. This is a show-stopper... */
1684 static void unexpected_eof (void) ATTRIBUTE_NORETURN
;
1687 unexpected_eof (void)
1691 gfc_error ("Unexpected end of file in '%s'", gfc_source_file
);
1693 /* Memory cleanup. Move to "second to last". */
1694 for (p
= gfc_state_stack
; p
&& p
->previous
&& p
->previous
->previous
;
1697 gfc_current_ns
->code
= (p
&& p
->previous
) ? p
->head
: NULL
;
1700 longjmp (eof_buf
, 1);
1704 /* Parse the CONTAINS section of a derived type definition. */
1706 gfc_access gfc_typebound_default_access
;
1709 parse_derived_contains (void)
1712 bool seen_private
= false;
1713 bool seen_comps
= false;
1714 bool error_flag
= false;
1717 gcc_assert (gfc_current_state () == COMP_DERIVED
);
1718 gcc_assert (gfc_current_block ());
1720 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
1722 if (gfc_current_block ()->attr
.sequence
)
1723 gfc_error ("Derived-type '%s' with SEQUENCE must not have a CONTAINS"
1724 " section at %C", gfc_current_block ()->name
);
1725 if (gfc_current_block ()->attr
.is_bind_c
)
1726 gfc_error ("Derived-type '%s' with BIND(C) must not have a CONTAINS"
1727 " section at %C", gfc_current_block ()->name
);
1729 accept_statement (ST_CONTAINS
);
1730 push_state (&s
, COMP_DERIVED_CONTAINS
, NULL
);
1732 gfc_typebound_default_access
= ACCESS_PUBLIC
;
1738 st
= next_statement ();
1746 gfc_error ("Components in TYPE at %C must precede CONTAINS");
1751 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Type-bound"
1752 " procedure at %C") == FAILURE
)
1755 accept_statement (ST_PROCEDURE
);
1760 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: GENERIC binding"
1761 " at %C") == FAILURE
)
1764 accept_statement (ST_GENERIC
);
1769 if (gfc_notify_std (GFC_STD_F2003
,
1770 "Fortran 2003: FINAL procedure declaration"
1771 " at %C") == FAILURE
)
1774 accept_statement (ST_FINAL
);
1782 && (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: Derived type "
1783 "definition at %C with empty CONTAINS "
1784 "section") == FAILURE
))
1787 /* ST_END_TYPE is accepted by parse_derived after return. */
1791 if (gfc_find_state (COMP_MODULE
) == FAILURE
)
1793 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1801 gfc_error ("PRIVATE statement at %C must precede procedure"
1809 gfc_error ("Duplicate PRIVATE statement at %C");
1813 accept_statement (ST_PRIVATE
);
1814 gfc_typebound_default_access
= ACCESS_PRIVATE
;
1815 seen_private
= true;
1819 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
1824 gfc_error ("Already inside a CONTAINS block at %C");
1829 unexpected_statement (st
);
1835 gcc_assert (gfc_current_state () == COMP_DERIVED
);
1841 /* Parse a derived type. */
1844 parse_derived (void)
1846 int compiling_type
, seen_private
, seen_sequence
, seen_component
, error_flag
;
1849 gfc_symbol
*derived_sym
= NULL
;
1855 accept_statement (ST_DERIVED_DECL
);
1856 push_state (&s
, COMP_DERIVED
, gfc_new_block
);
1858 gfc_new_block
->component_access
= ACCESS_PUBLIC
;
1865 while (compiling_type
)
1867 st
= next_statement ();
1874 accept_statement (st
);
1879 gfc_error ("PROCEDURE binding at %C must be inside CONTAINS");
1884 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
1893 && (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Derived type "
1894 "definition at %C without components")
1898 accept_statement (ST_END_TYPE
);
1902 if (gfc_find_state (COMP_MODULE
) == FAILURE
)
1904 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1912 gfc_error ("PRIVATE statement at %C must precede "
1913 "structure components");
1920 gfc_error ("Duplicate PRIVATE statement at %C");
1924 s
.sym
->component_access
= ACCESS_PRIVATE
;
1926 accept_statement (ST_PRIVATE
);
1933 gfc_error ("SEQUENCE statement at %C must precede "
1934 "structure components");
1939 if (gfc_current_block ()->attr
.sequence
)
1940 gfc_warning ("SEQUENCE attribute at %C already specified in "
1945 gfc_error ("Duplicate SEQUENCE statement at %C");
1950 gfc_add_sequence (&gfc_current_block ()->attr
,
1951 gfc_current_block ()->name
, NULL
);
1955 if (gfc_notify_std (GFC_STD_F2003
,
1956 "Fortran 2003: CONTAINS block in derived type"
1957 " definition at %C") == FAILURE
)
1960 accept_statement (ST_CONTAINS
);
1961 if (parse_derived_contains ())
1966 unexpected_statement (st
);
1971 /* need to verify that all fields of the derived type are
1972 * interoperable with C if the type is declared to be bind(c)
1974 derived_sym
= gfc_current_block();
1976 sym
= gfc_current_block ();
1977 for (c
= sym
->components
; c
; c
= c
->next
)
1979 /* Look for allocatable components. */
1980 if (c
->attr
.allocatable
1981 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.derived
->attr
.alloc_comp
))
1982 sym
->attr
.alloc_comp
= 1;
1984 /* Look for pointer components. */
1986 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.derived
->attr
.pointer_comp
))
1987 sym
->attr
.pointer_comp
= 1;
1989 /* Look for private components. */
1990 if (sym
->component_access
== ACCESS_PRIVATE
1991 || c
->attr
.access
== ACCESS_PRIVATE
1992 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.derived
->attr
.private_comp
))
1993 sym
->attr
.private_comp
= 1;
1996 if (!seen_component
)
1997 sym
->attr
.zero_comp
= 1;
2003 /* Parse an ENUM. */
2012 int seen_enumerator
= 0;
2016 push_state (&s
, COMP_ENUM
, gfc_new_block
);
2020 while (compiling_enum
)
2022 st
= next_statement ();
2030 seen_enumerator
= 1;
2031 accept_statement (st
);
2036 if (!seen_enumerator
)
2038 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2041 accept_statement (st
);
2045 gfc_free_enum_history ();
2046 unexpected_statement (st
);
2054 /* Parse an interface. We must be able to deal with the possibility
2055 of recursive interfaces. The parse_spec() subroutine is mutually
2056 recursive with parse_interface(). */
2058 static gfc_statement
parse_spec (gfc_statement
);
2061 parse_interface (void)
2063 gfc_compile_state new_state
= COMP_NONE
, current_state
;
2064 gfc_symbol
*prog_unit
, *sym
;
2065 gfc_interface_info save
;
2066 gfc_state_data s1
, s2
;
2070 accept_statement (ST_INTERFACE
);
2072 current_interface
.ns
= gfc_current_ns
;
2073 save
= current_interface
;
2075 sym
= (current_interface
.type
== INTERFACE_GENERIC
2076 || current_interface
.type
== INTERFACE_USER_OP
)
2077 ? gfc_new_block
: NULL
;
2079 push_state (&s1
, COMP_INTERFACE
, sym
);
2080 current_state
= COMP_NONE
;
2083 gfc_current_ns
= gfc_get_namespace (current_interface
.ns
, 0);
2085 st
= next_statement ();
2093 if (st
== ST_SUBROUTINE
)
2094 new_state
= COMP_SUBROUTINE
;
2095 else if (st
== ST_FUNCTION
)
2096 new_state
= COMP_FUNCTION
;
2097 if (gfc_new_block
->attr
.pointer
)
2099 gfc_new_block
->attr
.pointer
= 0;
2100 gfc_new_block
->attr
.proc_pointer
= 1;
2102 if (gfc_add_explicit_interface (gfc_new_block
, IFSRC_IFBODY
,
2103 gfc_new_block
->formal
, NULL
) == FAILURE
)
2105 reject_statement ();
2106 gfc_free_namespace (gfc_current_ns
);
2109 if (current_interface
.type
!= INTERFACE_ABSTRACT
&&
2110 !gfc_new_block
->attr
.dummy
&&
2111 gfc_add_external (&gfc_new_block
->attr
, &gfc_current_locus
) == FAILURE
)
2113 reject_statement ();
2114 gfc_free_namespace (gfc_current_ns
);
2120 case ST_MODULE_PROC
: /* The module procedure matcher makes
2121 sure the context is correct. */
2122 accept_statement (st
);
2123 gfc_free_namespace (gfc_current_ns
);
2126 case ST_END_INTERFACE
:
2127 gfc_free_namespace (gfc_current_ns
);
2128 gfc_current_ns
= current_interface
.ns
;
2132 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2133 gfc_ascii_statement (st
));
2134 reject_statement ();
2135 gfc_free_namespace (gfc_current_ns
);
2140 /* Make sure that a generic interface has only subroutines or
2141 functions and that the generic name has the right attribute. */
2142 if (current_interface
.type
== INTERFACE_GENERIC
)
2144 if (current_state
== COMP_NONE
)
2146 if (new_state
== COMP_FUNCTION
)
2147 gfc_add_function (&sym
->attr
, sym
->name
, NULL
);
2148 else if (new_state
== COMP_SUBROUTINE
)
2149 gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
);
2151 current_state
= new_state
;
2155 if (new_state
!= current_state
)
2157 if (new_state
== COMP_SUBROUTINE
)
2158 gfc_error ("SUBROUTINE at %C does not belong in a "
2159 "generic function interface");
2161 if (new_state
== COMP_FUNCTION
)
2162 gfc_error ("FUNCTION at %C does not belong in a "
2163 "generic subroutine interface");
2168 if (current_interface
.type
== INTERFACE_ABSTRACT
)
2170 gfc_add_abstract (&gfc_new_block
->attr
, &gfc_current_locus
);
2171 if (gfc_is_intrinsic_typename (gfc_new_block
->name
))
2172 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
2173 "cannot be the same as an intrinsic type",
2174 gfc_new_block
->name
);
2177 push_state (&s2
, new_state
, gfc_new_block
);
2178 accept_statement (st
);
2179 prog_unit
= gfc_new_block
;
2180 prog_unit
->formal_ns
= gfc_current_ns
;
2181 proc_locus
= gfc_current_locus
;
2184 /* Read data declaration statements. */
2185 st
= parse_spec (ST_NONE
);
2187 /* Since the interface block does not permit an IMPLICIT statement,
2188 the default type for the function or the result must be taken
2189 from the formal namespace. */
2190 if (new_state
== COMP_FUNCTION
)
2192 if (prog_unit
->result
== prog_unit
2193 && prog_unit
->ts
.type
== BT_UNKNOWN
)
2194 gfc_set_default_type (prog_unit
, 1, prog_unit
->formal_ns
);
2195 else if (prog_unit
->result
!= prog_unit
2196 && prog_unit
->result
->ts
.type
== BT_UNKNOWN
)
2197 gfc_set_default_type (prog_unit
->result
, 1,
2198 prog_unit
->formal_ns
);
2201 if (st
!= ST_END_SUBROUTINE
&& st
!= ST_END_FUNCTION
)
2203 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2204 gfc_ascii_statement (st
));
2205 reject_statement ();
2209 current_interface
= save
;
2210 gfc_add_interface (prog_unit
);
2213 if (current_interface
.ns
2214 && current_interface
.ns
->proc_name
2215 && strcmp (current_interface
.ns
->proc_name
->name
,
2216 prog_unit
->name
) == 0)
2217 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2218 "enclosing procedure", prog_unit
->name
, &proc_locus
);
2227 /* Associate function characteristics by going back to the function
2228 declaration and rematching the prefix. */
2231 match_deferred_characteristics (gfc_typespec
* ts
)
2234 match m
= MATCH_ERROR
;
2235 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2237 loc
= gfc_current_locus
;
2239 gfc_current_locus
= gfc_current_block ()->declared_at
;
2242 gfc_buffer_error (1);
2243 m
= gfc_match_prefix (ts
);
2244 gfc_buffer_error (0);
2246 if (ts
->type
== BT_DERIVED
)
2250 if (!ts
->derived
|| !ts
->derived
->components
)
2254 /* Only permit one go at the characteristic association. */
2258 /* Set the function locus correctly. If we have not found the
2259 function name, there is an error. */
2261 && gfc_match ("function% %n", name
) == MATCH_YES
2262 && strcmp (name
, gfc_current_block ()->name
) == 0)
2264 gfc_current_block ()->declared_at
= gfc_current_locus
;
2265 gfc_commit_symbols ();
2270 gfc_current_locus
=loc
;
2275 /* Check specification-expressions in the function result of the currently
2276 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2277 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2278 scope are not yet parsed so this has to be delayed up to parse_spec. */
2281 check_function_result_typed (void)
2283 gfc_typespec
* ts
= &gfc_current_ns
->proc_name
->result
->ts
;
2285 gcc_assert (gfc_current_state () == COMP_FUNCTION
);
2286 gcc_assert (ts
->type
!= BT_UNKNOWN
);
2288 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2289 /* TODO: Extend when KIND type parameters are implemented. */
2290 if (ts
->type
== BT_CHARACTER
&& ts
->cl
&& ts
->cl
->length
)
2291 gfc_expr_check_typed (ts
->cl
->length
, gfc_current_ns
, true);
2295 /* Parse a set of specification statements. Returns the statement
2296 that doesn't fit. */
2298 static gfc_statement
2299 parse_spec (gfc_statement st
)
2302 bool function_result_typed
= false;
2303 bool bad_characteristic
= false;
2306 verify_st_order (&ss
, ST_NONE
, false);
2308 st
= next_statement ();
2310 /* If we are not inside a function or don't have a result specified so far,
2311 do nothing special about it. */
2312 if (gfc_current_state () != COMP_FUNCTION
)
2313 function_result_typed
= true;
2316 gfc_symbol
* proc
= gfc_current_ns
->proc_name
;
2319 if (proc
->result
->ts
.type
== BT_UNKNOWN
)
2320 function_result_typed
= true;
2325 /* If we find a statement that can not be followed by an IMPLICIT statement
2326 (and thus we can expect to see none any further), type the function result
2327 if it has not yet been typed. Be careful not to give the END statement
2328 to verify_st_order! */
2329 if (!function_result_typed
&& st
!= ST_GET_FCN_CHARACTERISTICS
)
2331 bool verify_now
= false;
2333 if (st
== ST_END_FUNCTION
|| st
== ST_CONTAINS
)
2338 verify_st_order (&dummyss
, ST_NONE
, false);
2339 verify_st_order (&dummyss
, st
, false);
2341 if (verify_st_order (&dummyss
, ST_IMPLICIT
, true) == FAILURE
)
2347 check_function_result_typed ();
2348 function_result_typed
= true;
2357 case ST_IMPLICIT_NONE
:
2359 if (!function_result_typed
)
2361 check_function_result_typed ();
2362 function_result_typed
= true;
2368 case ST_DATA
: /* Not allowed in interfaces */
2369 if (gfc_current_state () == COMP_INTERFACE
)
2379 case ST_DERIVED_DECL
:
2382 if (verify_st_order (&ss
, st
, false) == FAILURE
)
2384 reject_statement ();
2385 st
= next_statement ();
2395 case ST_DERIVED_DECL
:
2401 if (gfc_current_state () != COMP_MODULE
)
2403 gfc_error ("%s statement must appear in a MODULE",
2404 gfc_ascii_statement (st
));
2408 if (gfc_current_ns
->default_access
!= ACCESS_UNKNOWN
)
2410 gfc_error ("%s statement at %C follows another accessibility "
2411 "specification", gfc_ascii_statement (st
));
2415 gfc_current_ns
->default_access
= (st
== ST_PUBLIC
)
2416 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
2420 case ST_STATEMENT_FUNCTION
:
2421 if (gfc_current_state () == COMP_MODULE
)
2423 unexpected_statement (st
);
2431 accept_statement (st
);
2432 st
= next_statement ();
2436 accept_statement (st
);
2438 st
= next_statement ();
2441 case ST_GET_FCN_CHARACTERISTICS
:
2442 /* This statement triggers the association of a function's result
2444 ts
= &gfc_current_block ()->result
->ts
;
2445 if (match_deferred_characteristics (ts
) != MATCH_YES
)
2446 bad_characteristic
= true;
2448 st
= next_statement ();
2455 /* If match_deferred_characteristics failed, then there is an error. */
2456 if (bad_characteristic
)
2458 ts
= &gfc_current_block ()->result
->ts
;
2459 if (ts
->type
!= BT_DERIVED
)
2460 gfc_error ("Bad kind expression for function '%s' at %L",
2461 gfc_current_block ()->name
,
2462 &gfc_current_block ()->declared_at
);
2464 gfc_error ("The type for function '%s' at %L is not accessible",
2465 gfc_current_block ()->name
,
2466 &gfc_current_block ()->declared_at
);
2468 gfc_current_block ()->ts
.kind
= 0;
2469 /* Keep the derived type; if it's bad, it will be discovered later. */
2470 if (!(ts
->type
== BT_DERIVED
&& ts
->derived
))
2471 ts
->type
= BT_UNKNOWN
;
2478 /* Parse a WHERE block, (not a simple WHERE statement). */
2481 parse_where_block (void)
2483 int seen_empty_else
;
2488 accept_statement (ST_WHERE_BLOCK
);
2489 top
= gfc_state_stack
->tail
;
2491 push_state (&s
, COMP_WHERE
, gfc_new_block
);
2493 d
= add_statement ();
2494 d
->expr
= top
->expr
;
2500 seen_empty_else
= 0;
2504 st
= next_statement ();
2510 case ST_WHERE_BLOCK
:
2511 parse_where_block ();
2516 accept_statement (st
);
2520 if (seen_empty_else
)
2522 gfc_error ("ELSEWHERE statement at %C follows previous "
2523 "unmasked ELSEWHERE");
2527 if (new_st
.expr
== NULL
)
2528 seen_empty_else
= 1;
2530 d
= new_level (gfc_state_stack
->head
);
2532 d
->expr
= new_st
.expr
;
2534 accept_statement (st
);
2539 accept_statement (st
);
2543 gfc_error ("Unexpected %s statement in WHERE block at %C",
2544 gfc_ascii_statement (st
));
2545 reject_statement ();
2549 while (st
!= ST_END_WHERE
);
2555 /* Parse a FORALL block (not a simple FORALL statement). */
2558 parse_forall_block (void)
2564 accept_statement (ST_FORALL_BLOCK
);
2565 top
= gfc_state_stack
->tail
;
2567 push_state (&s
, COMP_FORALL
, gfc_new_block
);
2569 d
= add_statement ();
2570 d
->op
= EXEC_FORALL
;
2575 st
= next_statement ();
2580 case ST_POINTER_ASSIGNMENT
:
2583 accept_statement (st
);
2586 case ST_WHERE_BLOCK
:
2587 parse_where_block ();
2590 case ST_FORALL_BLOCK
:
2591 parse_forall_block ();
2595 accept_statement (st
);
2602 gfc_error ("Unexpected %s statement in FORALL block at %C",
2603 gfc_ascii_statement (st
));
2605 reject_statement ();
2609 while (st
!= ST_END_FORALL
);
2615 static gfc_statement
parse_executable (gfc_statement
);
2617 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2620 parse_if_block (void)
2629 accept_statement (ST_IF_BLOCK
);
2631 top
= gfc_state_stack
->tail
;
2632 push_state (&s
, COMP_IF
, gfc_new_block
);
2634 new_st
.op
= EXEC_IF
;
2635 d
= add_statement ();
2637 d
->expr
= top
->expr
;
2643 st
= parse_executable (ST_NONE
);
2653 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2654 "statement at %L", &else_locus
);
2656 reject_statement ();
2660 d
= new_level (gfc_state_stack
->head
);
2662 d
->expr
= new_st
.expr
;
2664 accept_statement (st
);
2671 gfc_error ("Duplicate ELSE statements at %L and %C",
2673 reject_statement ();
2678 else_locus
= gfc_current_locus
;
2680 d
= new_level (gfc_state_stack
->head
);
2683 accept_statement (st
);
2691 unexpected_statement (st
);
2695 while (st
!= ST_ENDIF
);
2698 accept_statement (st
);
2702 /* Parse a SELECT block. */
2705 parse_select_block (void)
2711 accept_statement (ST_SELECT_CASE
);
2713 cp
= gfc_state_stack
->tail
;
2714 push_state (&s
, COMP_SELECT
, gfc_new_block
);
2716 /* Make sure that the next statement is a CASE or END SELECT. */
2719 st
= next_statement ();
2722 if (st
== ST_END_SELECT
)
2724 /* Empty SELECT CASE is OK. */
2725 accept_statement (st
);
2732 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2735 reject_statement ();
2738 /* At this point, we're got a nonempty select block. */
2739 cp
= new_level (cp
);
2742 accept_statement (st
);
2746 st
= parse_executable (ST_NONE
);
2753 cp
= new_level (gfc_state_stack
->head
);
2755 gfc_clear_new_st ();
2757 accept_statement (st
);
2763 /* Can't have an executable statement because of
2764 parse_executable(). */
2766 unexpected_statement (st
);
2770 while (st
!= ST_END_SELECT
);
2773 accept_statement (st
);
2777 /* Given a symbol, make sure it is not an iteration variable for a DO
2778 statement. This subroutine is called when the symbol is seen in a
2779 context that causes it to become redefined. If the symbol is an
2780 iterator, we generate an error message and return nonzero. */
2783 gfc_check_do_variable (gfc_symtree
*st
)
2787 for (s
=gfc_state_stack
; s
; s
= s
->previous
)
2788 if (s
->do_variable
== st
)
2790 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2791 "loop beginning at %L", st
->name
, &s
->head
->loc
);
2799 /* Checks to see if the current statement label closes an enddo.
2800 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2801 an error) if it incorrectly closes an ENDDO. */
2804 check_do_closure (void)
2808 if (gfc_statement_label
== NULL
)
2811 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
2812 if (p
->state
== COMP_DO
)
2816 return 0; /* No loops to close */
2818 if (p
->ext
.end_do_label
== gfc_statement_label
)
2821 if (p
== gfc_state_stack
)
2824 gfc_error ("End of nonblock DO statement at %C is within another block");
2828 /* At this point, the label doesn't terminate the innermost loop.
2829 Make sure it doesn't terminate another one. */
2830 for (; p
; p
= p
->previous
)
2831 if (p
->state
== COMP_DO
&& p
->ext
.end_do_label
== gfc_statement_label
)
2833 gfc_error ("End of nonblock DO statement at %C is interwoven "
2834 "with another DO loop");
2842 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2843 handled inside of parse_executable(), because they aren't really
2847 parse_do_block (void)
2854 s
.ext
.end_do_label
= new_st
.label
;
2856 if (new_st
.ext
.iterator
!= NULL
)
2857 stree
= new_st
.ext
.iterator
->var
->symtree
;
2861 accept_statement (ST_DO
);
2863 top
= gfc_state_stack
->tail
;
2864 push_state (&s
, COMP_DO
, gfc_new_block
);
2866 s
.do_variable
= stree
;
2868 top
->block
= new_level (top
);
2869 top
->block
->op
= EXEC_DO
;
2872 st
= parse_executable (ST_NONE
);
2880 if (s
.ext
.end_do_label
!= NULL
2881 && s
.ext
.end_do_label
!= gfc_statement_label
)
2882 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
2885 if (gfc_statement_label
!= NULL
)
2887 new_st
.op
= EXEC_NOP
;
2892 case ST_IMPLIED_ENDDO
:
2893 /* If the do-stmt of this DO construct has a do-construct-name,
2894 the corresponding end-do must be an end-do-stmt (with a matching
2895 name, but in that case we must have seen ST_ENDDO first).
2896 We only complain about this in pedantic mode. */
2897 if (gfc_current_block () != NULL
)
2898 gfc_error_now ("named block DO at %L requires matching ENDDO name",
2899 &gfc_current_block()->declared_at
);
2904 unexpected_statement (st
);
2909 accept_statement (st
);
2913 /* Parse the statements of OpenMP do/parallel do. */
2915 static gfc_statement
2916 parse_omp_do (gfc_statement omp_st
)
2922 accept_statement (omp_st
);
2924 cp
= gfc_state_stack
->tail
;
2925 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
2926 np
= new_level (cp
);
2932 st
= next_statement ();
2935 else if (st
== ST_DO
)
2938 unexpected_statement (st
);
2942 if (gfc_statement_label
!= NULL
2943 && gfc_state_stack
->previous
!= NULL
2944 && gfc_state_stack
->previous
->state
== COMP_DO
2945 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
2953 there should be no !$OMP END DO. */
2955 return ST_IMPLIED_ENDDO
;
2958 check_do_closure ();
2961 st
= next_statement ();
2962 if (st
== (omp_st
== ST_OMP_DO
? ST_OMP_END_DO
: ST_OMP_END_PARALLEL_DO
))
2964 if (new_st
.op
== EXEC_OMP_END_NOWAIT
)
2965 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
2967 gcc_assert (new_st
.op
== EXEC_NOP
);
2968 gfc_clear_new_st ();
2969 gfc_commit_symbols ();
2970 gfc_warning_check ();
2971 st
= next_statement ();
2977 /* Parse the statements of OpenMP atomic directive. */
2980 parse_omp_atomic (void)
2986 accept_statement (ST_OMP_ATOMIC
);
2988 cp
= gfc_state_stack
->tail
;
2989 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
2990 np
= new_level (cp
);
2996 st
= next_statement ();
2999 else if (st
== ST_ASSIGNMENT
)
3002 unexpected_statement (st
);
3005 accept_statement (st
);
3011 /* Parse the statements of an OpenMP structured block. */
3014 parse_omp_structured_block (gfc_statement omp_st
, bool workshare_stmts_only
)
3016 gfc_statement st
, omp_end_st
;
3020 accept_statement (omp_st
);
3022 cp
= gfc_state_stack
->tail
;
3023 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3024 np
= new_level (cp
);
3030 case ST_OMP_PARALLEL
:
3031 omp_end_st
= ST_OMP_END_PARALLEL
;
3033 case ST_OMP_PARALLEL_SECTIONS
:
3034 omp_end_st
= ST_OMP_END_PARALLEL_SECTIONS
;
3036 case ST_OMP_SECTIONS
:
3037 omp_end_st
= ST_OMP_END_SECTIONS
;
3039 case ST_OMP_ORDERED
:
3040 omp_end_st
= ST_OMP_END_ORDERED
;
3042 case ST_OMP_CRITICAL
:
3043 omp_end_st
= ST_OMP_END_CRITICAL
;
3046 omp_end_st
= ST_OMP_END_MASTER
;
3049 omp_end_st
= ST_OMP_END_SINGLE
;
3052 omp_end_st
= ST_OMP_END_TASK
;
3054 case ST_OMP_WORKSHARE
:
3055 omp_end_st
= ST_OMP_END_WORKSHARE
;
3057 case ST_OMP_PARALLEL_WORKSHARE
:
3058 omp_end_st
= ST_OMP_END_PARALLEL_WORKSHARE
;
3066 if (workshare_stmts_only
)
3068 /* Inside of !$omp workshare, only
3071 where statements and constructs
3072 forall statements and constructs
3076 are allowed. For !$omp critical these
3077 restrictions apply recursively. */
3080 st
= next_statement ();
3091 accept_statement (st
);
3094 case ST_WHERE_BLOCK
:
3095 parse_where_block ();
3098 case ST_FORALL_BLOCK
:
3099 parse_forall_block ();
3102 case ST_OMP_PARALLEL
:
3103 case ST_OMP_PARALLEL_SECTIONS
:
3104 parse_omp_structured_block (st
, false);
3107 case ST_OMP_PARALLEL_WORKSHARE
:
3108 case ST_OMP_CRITICAL
:
3109 parse_omp_structured_block (st
, true);
3112 case ST_OMP_PARALLEL_DO
:
3113 st
= parse_omp_do (st
);
3117 parse_omp_atomic ();
3128 st
= next_statement ();
3132 st
= parse_executable (ST_NONE
);
3135 else if (st
== ST_OMP_SECTION
3136 && (omp_st
== ST_OMP_SECTIONS
3137 || omp_st
== ST_OMP_PARALLEL_SECTIONS
))
3139 np
= new_level (np
);
3143 else if (st
!= omp_end_st
)
3144 unexpected_statement (st
);
3146 while (st
!= omp_end_st
);
3150 case EXEC_OMP_END_NOWAIT
:
3151 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
3153 case EXEC_OMP_CRITICAL
:
3154 if (((cp
->ext
.omp_name
== NULL
) ^ (new_st
.ext
.omp_name
== NULL
))
3155 || (new_st
.ext
.omp_name
!= NULL
3156 && strcmp (cp
->ext
.omp_name
, new_st
.ext
.omp_name
) != 0))
3157 gfc_error ("Name after !$omp critical and !$omp end critical does "
3159 gfc_free (CONST_CAST (char *, new_st
.ext
.omp_name
));
3161 case EXEC_OMP_END_SINGLE
:
3162 cp
->ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
]
3163 = new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
];
3164 new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
] = NULL
;
3165 gfc_free_omp_clauses (new_st
.ext
.omp_clauses
);
3173 gfc_clear_new_st ();
3174 gfc_commit_symbols ();
3175 gfc_warning_check ();
3180 /* Accept a series of executable statements. We return the first
3181 statement that doesn't fit to the caller. Any block statements are
3182 passed on to the correct handler, which usually passes the buck
3185 static gfc_statement
3186 parse_executable (gfc_statement st
)
3191 st
= next_statement ();
3195 close_flag
= check_do_closure ();
3200 case ST_END_PROGRAM
:
3203 case ST_END_FUNCTION
:
3207 case ST_END_SUBROUTINE
:
3212 case ST_SELECT_CASE
:
3213 gfc_error ("%s statement at %C cannot terminate a non-block "
3214 "DO loop", gfc_ascii_statement (st
));
3230 accept_statement (st
);
3231 if (close_flag
== 1)
3232 return ST_IMPLIED_ENDDO
;
3239 case ST_SELECT_CASE
:
3240 parse_select_block ();
3245 if (check_do_closure () == 1)
3246 return ST_IMPLIED_ENDDO
;
3249 case ST_WHERE_BLOCK
:
3250 parse_where_block ();
3253 case ST_FORALL_BLOCK
:
3254 parse_forall_block ();
3257 case ST_OMP_PARALLEL
:
3258 case ST_OMP_PARALLEL_SECTIONS
:
3259 case ST_OMP_SECTIONS
:
3260 case ST_OMP_ORDERED
:
3261 case ST_OMP_CRITICAL
:
3265 parse_omp_structured_block (st
, false);
3268 case ST_OMP_WORKSHARE
:
3269 case ST_OMP_PARALLEL_WORKSHARE
:
3270 parse_omp_structured_block (st
, true);
3274 case ST_OMP_PARALLEL_DO
:
3275 st
= parse_omp_do (st
);
3276 if (st
== ST_IMPLIED_ENDDO
)
3281 parse_omp_atomic ();
3288 st
= next_statement ();
3293 /* Parse a series of contained program units. */
3295 static void parse_progunit (gfc_statement
);
3298 /* Fix the symbols for sibling functions. These are incorrectly added to
3299 the child namespace as the parser didn't know about this procedure. */
3302 gfc_fixup_sibling_symbols (gfc_symbol
*sym
, gfc_namespace
*siblings
)
3306 gfc_symbol
*old_sym
;
3308 sym
->attr
.referenced
= 1;
3309 for (ns
= siblings
; ns
; ns
= ns
->sibling
)
3311 gfc_find_sym_tree (sym
->name
, ns
, 0, &st
);
3313 if (!st
|| (st
->n
.sym
->attr
.dummy
&& ns
== st
->n
.sym
->ns
))
3314 goto fixup_contained
;
3316 old_sym
= st
->n
.sym
;
3317 if (old_sym
->ns
== ns
3318 && !old_sym
->attr
.contained
3320 /* By 14.6.1.3, host association should be excluded
3321 for the following. */
3322 && !(old_sym
->attr
.external
3323 || (old_sym
->ts
.type
!= BT_UNKNOWN
3324 && !old_sym
->attr
.implicit_type
)
3325 || old_sym
->attr
.flavor
== FL_PARAMETER
3326 || old_sym
->attr
.in_common
3327 || old_sym
->attr
.in_equivalence
3328 || old_sym
->attr
.data
3329 || old_sym
->attr
.dummy
3330 || old_sym
->attr
.result
3331 || old_sym
->attr
.dimension
3332 || old_sym
->attr
.allocatable
3333 || old_sym
->attr
.intrinsic
3334 || old_sym
->attr
.generic
3335 || old_sym
->attr
.flavor
== FL_NAMELIST
3336 || old_sym
->attr
.proc
== PROC_ST_FUNCTION
))
3338 /* Replace it with the symbol from the parent namespace. */
3342 /* Free the old (local) symbol. */
3344 if (old_sym
->refs
== 0)
3345 gfc_free_symbol (old_sym
);
3349 /* Do the same for any contained procedures. */
3350 gfc_fixup_sibling_symbols (sym
, ns
->contained
);
3355 parse_contained (int module
)
3357 gfc_namespace
*ns
, *parent_ns
, *tmp
;
3358 gfc_state_data s1
, s2
;
3362 int contains_statements
= 0;
3365 push_state (&s1
, COMP_CONTAINS
, NULL
);
3366 parent_ns
= gfc_current_ns
;
3370 gfc_current_ns
= gfc_get_namespace (parent_ns
, 1);
3372 gfc_current_ns
->sibling
= parent_ns
->contained
;
3373 parent_ns
->contained
= gfc_current_ns
;
3376 /* Process the next available statement. We come here if we got an error
3377 and rejected the last statement. */
3378 st
= next_statement ();
3387 contains_statements
= 1;
3388 accept_statement (st
);
3391 (st
== ST_FUNCTION
) ? COMP_FUNCTION
: COMP_SUBROUTINE
,
3394 /* For internal procedures, create/update the symbol in the
3395 parent namespace. */
3399 if (gfc_get_symbol (gfc_new_block
->name
, parent_ns
, &sym
))
3400 gfc_error ("Contained procedure '%s' at %C is already "
3401 "ambiguous", gfc_new_block
->name
);
3404 if (gfc_add_procedure (&sym
->attr
, PROC_INTERNAL
, sym
->name
,
3405 &gfc_new_block
->declared_at
) ==
3408 if (st
== ST_FUNCTION
)
3409 gfc_add_function (&sym
->attr
, sym
->name
,
3410 &gfc_new_block
->declared_at
);
3412 gfc_add_subroutine (&sym
->attr
, sym
->name
,
3413 &gfc_new_block
->declared_at
);
3417 gfc_commit_symbols ();
3420 sym
= gfc_new_block
;
3422 /* Mark this as a contained function, so it isn't replaced
3423 by other module functions. */
3424 sym
->attr
.contained
= 1;
3425 sym
->attr
.referenced
= 1;
3427 parse_progunit (ST_NONE
);
3429 /* Fix up any sibling functions that refer to this one. */
3430 gfc_fixup_sibling_symbols (sym
, gfc_current_ns
);
3431 /* Or refer to any of its alternate entry points. */
3432 for (el
= gfc_current_ns
->entries
; el
; el
= el
->next
)
3433 gfc_fixup_sibling_symbols (el
->sym
, gfc_current_ns
);
3435 gfc_current_ns
->code
= s2
.head
;
3436 gfc_current_ns
= parent_ns
;
3441 /* These statements are associated with the end of the host unit. */
3442 case ST_END_FUNCTION
:
3444 case ST_END_PROGRAM
:
3445 case ST_END_SUBROUTINE
:
3446 accept_statement (st
);
3450 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
3451 gfc_ascii_statement (st
));
3452 reject_statement ();
3458 while (st
!= ST_END_FUNCTION
&& st
!= ST_END_SUBROUTINE
3459 && st
!= ST_END_MODULE
&& st
!= ST_END_PROGRAM
);
3461 /* The first namespace in the list is guaranteed to not have
3462 anything (worthwhile) in it. */
3463 tmp
= gfc_current_ns
;
3464 gfc_current_ns
= parent_ns
;
3465 if (seen_error
&& tmp
->refs
> 1)
3466 gfc_free_namespace (tmp
);
3468 ns
= gfc_current_ns
->contained
;
3469 gfc_current_ns
->contained
= ns
->sibling
;
3470 gfc_free_namespace (ns
);
3473 if (!contains_statements
)
3474 gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: CONTAINS statement without "
3475 "FUNCTION or SUBROUTINE statement at %C");
3479 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
3482 parse_progunit (gfc_statement st
)
3487 st
= parse_spec (st
);
3497 accept_statement (st
);
3504 if (gfc_current_state () == COMP_FUNCTION
)
3505 gfc_check_function_type (gfc_current_ns
);
3510 st
= parse_executable (st
);
3521 accept_statement (st
);
3528 unexpected_statement (st
);
3529 reject_statement ();
3530 st
= next_statement ();
3536 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
3537 if (p
->state
== COMP_CONTAINS
)
3540 if (gfc_find_state (COMP_MODULE
) == SUCCESS
)
3545 gfc_error ("CONTAINS statement at %C is already in a contained "
3547 st
= next_statement ();
3551 parse_contained (0);
3554 gfc_current_ns
->code
= gfc_state_stack
->head
;
3558 /* Come here to complain about a global symbol already in use as
3562 gfc_global_used (gfc_gsymbol
*sym
, locus
*where
)
3567 where
= &gfc_current_locus
;
3577 case GSYM_SUBROUTINE
:
3578 name
= "SUBROUTINE";
3583 case GSYM_BLOCK_DATA
:
3584 name
= "BLOCK DATA";
3590 gfc_internal_error ("gfc_global_used(): Bad type");
3594 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
3595 sym
->name
, where
, name
, &sym
->where
);
3599 /* Parse a block data program unit. */
3602 parse_block_data (void)
3605 static locus blank_locus
;
3606 static int blank_block
=0;
3609 gfc_current_ns
->proc_name
= gfc_new_block
;
3610 gfc_current_ns
->is_block_data
= 1;
3612 if (gfc_new_block
== NULL
)
3615 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3616 "prior BLOCK DATA at %L", &blank_locus
);
3620 blank_locus
= gfc_current_locus
;
3625 s
= gfc_get_gsymbol (gfc_new_block
->name
);
3627 || (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_BLOCK_DATA
))
3628 gfc_global_used(s
, NULL
);
3631 s
->type
= GSYM_BLOCK_DATA
;
3632 s
->where
= gfc_current_locus
;
3637 st
= parse_spec (ST_NONE
);
3639 while (st
!= ST_END_BLOCK_DATA
)
3641 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3642 gfc_ascii_statement (st
));
3643 reject_statement ();
3644 st
= next_statement ();
3649 /* Parse a module subprogram. */
3657 s
= gfc_get_gsymbol (gfc_new_block
->name
);
3658 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_MODULE
))
3659 gfc_global_used(s
, NULL
);
3662 s
->type
= GSYM_MODULE
;
3663 s
->where
= gfc_current_locus
;
3667 st
= parse_spec (ST_NONE
);
3676 parse_contained (1);
3680 accept_statement (st
);
3684 gfc_error ("Unexpected %s statement in MODULE at %C",
3685 gfc_ascii_statement (st
));
3687 reject_statement ();
3688 st
= next_statement ();
3694 /* Add a procedure name to the global symbol table. */
3697 add_global_procedure (int sub
)
3701 s
= gfc_get_gsymbol(gfc_new_block
->name
);
3704 || (s
->type
!= GSYM_UNKNOWN
3705 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
3706 gfc_global_used(s
, NULL
);
3709 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
3710 s
->where
= gfc_current_locus
;
3716 /* Add a program to the global symbol table. */
3719 add_global_program (void)
3723 if (gfc_new_block
== NULL
)
3725 s
= gfc_get_gsymbol (gfc_new_block
->name
);
3727 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_PROGRAM
))
3728 gfc_global_used(s
, NULL
);
3731 s
->type
= GSYM_PROGRAM
;
3732 s
->where
= gfc_current_locus
;
3738 /* Top level parser. */
3741 gfc_parse_file (void)
3743 int seen_program
, errors_before
, errors
;
3744 gfc_state_data top
, s
;
3748 gfc_start_source_files ();
3750 top
.state
= COMP_NONE
;
3752 top
.previous
= NULL
;
3753 top
.head
= top
.tail
= NULL
;
3754 top
.do_variable
= NULL
;
3756 gfc_state_stack
= &top
;
3758 gfc_clear_new_st ();
3760 gfc_statement_label
= NULL
;
3762 if (setjmp (eof_buf
))
3763 return FAILURE
; /* Come here on unexpected EOF */
3767 /* Exit early for empty files. */
3773 st
= next_statement ();
3782 goto duplicate_main
;
3784 prog_locus
= gfc_current_locus
;
3786 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
3787 main_program_symbol(gfc_current_ns
, gfc_new_block
->name
);
3788 accept_statement (st
);
3789 add_global_program ();
3790 parse_progunit (ST_NONE
);
3794 add_global_procedure (1);
3795 push_state (&s
, COMP_SUBROUTINE
, gfc_new_block
);
3796 accept_statement (st
);
3797 parse_progunit (ST_NONE
);
3801 add_global_procedure (0);
3802 push_state (&s
, COMP_FUNCTION
, gfc_new_block
);
3803 accept_statement (st
);
3804 parse_progunit (ST_NONE
);
3808 push_state (&s
, COMP_BLOCK_DATA
, gfc_new_block
);
3809 accept_statement (st
);
3810 parse_block_data ();
3814 push_state (&s
, COMP_MODULE
, gfc_new_block
);
3815 accept_statement (st
);
3817 gfc_get_errors (NULL
, &errors_before
);
3821 /* Anything else starts a nameless main program block. */
3824 goto duplicate_main
;
3826 prog_locus
= gfc_current_locus
;
3828 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
3829 main_program_symbol (gfc_current_ns
, "MAIN__");
3830 parse_progunit (st
);
3834 gfc_current_ns
->code
= s
.head
;
3836 gfc_resolve (gfc_current_ns
);
3838 /* Dump the parse tree if requested. */
3839 if (gfc_option
.dump_parse_tree
)
3840 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
3842 gfc_get_errors (NULL
, &errors
);
3843 if (s
.state
== COMP_MODULE
)
3845 gfc_dump_module (s
.sym
->name
, errors_before
== errors
);
3847 gfc_generate_module_code (gfc_current_ns
);
3852 gfc_generate_code (gfc_current_ns
);
3860 gfc_end_source_files ();
3864 /* If we see a duplicate main program, shut down. If the second
3865 instance is an implied main program, i.e. data decls or executable
3866 statements, we're in for lots of errors. */
3867 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus
);
3868 reject_statement ();