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 ()->result
->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
,
132 match ("allocatable", gfc_match_asynchronous
, ST_ATTR_DECL
);
133 match ("asynchronous", gfc_match_asynchronous
, ST_ATTR_DECL
);
137 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
141 match ("codimension", gfc_match_codimension
, ST_ATTR_DECL
);
142 match ("contiguous", gfc_match_contiguous
, ST_ATTR_DECL
);
146 match ("data", gfc_match_data
, ST_DATA
);
147 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
151 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
152 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
153 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
154 match ("external", gfc_match_external
, ST_ATTR_DECL
);
158 match ("format", gfc_match_format
, ST_FORMAT
);
165 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
166 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
167 match ("interface", gfc_match_interface
, ST_INTERFACE
);
168 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
169 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
176 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
180 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
184 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
185 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
186 if (gfc_match_private (&st
) == MATCH_YES
)
188 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
189 if (gfc_match_public (&st
) == MATCH_YES
)
191 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
198 match ("save", gfc_match_save
, ST_ATTR_DECL
);
202 match ("target", gfc_match_target
, ST_ATTR_DECL
);
203 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
210 match ("value", gfc_match_value
, ST_ATTR_DECL
);
211 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
218 /* This is not a specification statement. See if any of the matchers
219 has stored an error message of some sort. */
223 gfc_buffer_error (0);
224 gfc_current_locus
= old_locus
;
226 return ST_GET_FCN_CHARACTERISTICS
;
230 /* This is the primary 'decode_statement'. */
232 decode_statement (void)
239 gfc_enforce_clean_symbol_state ();
241 gfc_clear_error (); /* Clear any pending errors. */
242 gfc_clear_warning (); /* Clear any pending warnings. */
244 gfc_matching_function
= false;
246 if (gfc_match_eos () == MATCH_YES
)
249 if (gfc_current_state () == COMP_FUNCTION
250 && gfc_current_block ()->result
->ts
.kind
== -1)
251 return decode_specification_statement ();
253 old_locus
= gfc_current_locus
;
255 /* Try matching a data declaration or function declaration. The
256 input "REALFUNCTIONA(N)" can mean several things in different
257 contexts, so it (and its relatives) get special treatment. */
259 if (gfc_current_state () == COMP_NONE
260 || gfc_current_state () == COMP_INTERFACE
261 || gfc_current_state () == COMP_CONTAINS
)
263 gfc_matching_function
= true;
264 m
= gfc_match_function_decl ();
267 else if (m
== MATCH_ERROR
)
271 gfc_current_locus
= old_locus
;
273 gfc_matching_function
= false;
276 /* Match statements whose error messages are meant to be overwritten
277 by something better. */
279 match (NULL
, gfc_match_assignment
, ST_ASSIGNMENT
);
280 match (NULL
, gfc_match_pointer_assignment
, ST_POINTER_ASSIGNMENT
);
281 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
283 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
284 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
286 /* Try to match a subroutine statement, which has the same optional
287 prefixes that functions can have. */
289 if (gfc_match_subroutine () == MATCH_YES
)
290 return ST_SUBROUTINE
;
292 gfc_current_locus
= old_locus
;
294 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
295 statements, which might begin with a block label. The match functions for
296 these statements are unusual in that their keyword is not seen before
297 the matcher is called. */
299 if (gfc_match_if (&st
) == MATCH_YES
)
302 gfc_current_locus
= old_locus
;
304 if (gfc_match_where (&st
) == MATCH_YES
)
307 gfc_current_locus
= old_locus
;
309 if (gfc_match_forall (&st
) == MATCH_YES
)
312 gfc_current_locus
= old_locus
;
314 match (NULL
, gfc_match_do
, ST_DO
);
315 match (NULL
, gfc_match_block
, ST_BLOCK
);
316 match (NULL
, gfc_match_associate
, ST_ASSOCIATE
);
317 match (NULL
, gfc_match_critical
, ST_CRITICAL
);
318 match (NULL
, gfc_match_select
, ST_SELECT_CASE
);
319 match (NULL
, gfc_match_select_type
, ST_SELECT_TYPE
);
321 /* General statement matching: Instead of testing every possible
322 statement, we eliminate most possibilities by peeking at the
325 c
= gfc_peek_ascii_char ();
330 match ("abstract% interface", gfc_match_abstract_interface
,
332 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
);
333 match ("allocatable", gfc_match_allocatable
, ST_ATTR_DECL
);
334 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
);
335 match ("asynchronous", gfc_match_asynchronous
, ST_ATTR_DECL
);
339 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
);
340 match ("block data", gfc_match_block_data
, ST_BLOCK_DATA
);
341 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
345 match ("call", gfc_match_call
, ST_CALL
);
346 match ("close", gfc_match_close
, ST_CLOSE
);
347 match ("continue", gfc_match_continue
, ST_CONTINUE
);
348 match ("contiguous", gfc_match_contiguous
, ST_ATTR_DECL
);
349 match ("cycle", gfc_match_cycle
, ST_CYCLE
);
350 match ("case", gfc_match_case
, ST_CASE
);
351 match ("common", gfc_match_common
, ST_COMMON
);
352 match ("contains", gfc_match_eos
, ST_CONTAINS
);
353 match ("class", gfc_match_class_is
, ST_CLASS_IS
);
354 match ("codimension", gfc_match_codimension
, ST_ATTR_DECL
);
358 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
);
359 match ("data", gfc_match_data
, ST_DATA
);
360 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
364 match ("end file", gfc_match_endfile
, ST_END_FILE
);
365 match ("exit", gfc_match_exit
, ST_EXIT
);
366 match ("else", gfc_match_else
, ST_ELSE
);
367 match ("else where", gfc_match_elsewhere
, ST_ELSEWHERE
);
368 match ("else if", gfc_match_elseif
, ST_ELSEIF
);
369 match ("error stop", gfc_match_error_stop
, ST_ERROR_STOP
);
370 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
372 if (gfc_match_end (&st
) == MATCH_YES
)
375 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
376 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
377 match ("external", gfc_match_external
, ST_ATTR_DECL
);
381 match ("final", gfc_match_final_decl
, ST_FINAL
);
382 match ("flush", gfc_match_flush
, ST_FLUSH
);
383 match ("format", gfc_match_format
, ST_FORMAT
);
387 match ("generic", gfc_match_generic
, ST_GENERIC
);
388 match ("go to", gfc_match_goto
, ST_GOTO
);
392 match ("inquire", gfc_match_inquire
, ST_INQUIRE
);
393 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
394 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
395 match ("import", gfc_match_import
, ST_IMPORT
);
396 match ("interface", gfc_match_interface
, ST_INTERFACE
);
397 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
398 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
402 match ("module% procedure% ", gfc_match_modproc
, ST_MODULE_PROC
);
403 match ("module", gfc_match_module
, ST_MODULE
);
407 match ("nullify", gfc_match_nullify
, ST_NULLIFY
);
408 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
412 match ("open", gfc_match_open
, ST_OPEN
);
413 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
417 match ("print", gfc_match_print
, ST_WRITE
);
418 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
419 match ("pause", gfc_match_pause
, ST_PAUSE
);
420 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
421 if (gfc_match_private (&st
) == MATCH_YES
)
423 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
424 match ("program", gfc_match_program
, ST_PROGRAM
);
425 if (gfc_match_public (&st
) == MATCH_YES
)
427 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
431 match ("read", gfc_match_read
, ST_READ
);
432 match ("return", gfc_match_return
, ST_RETURN
);
433 match ("rewind", gfc_match_rewind
, ST_REWIND
);
437 match ("sequence", gfc_match_eos
, ST_SEQUENCE
);
438 match ("stop", gfc_match_stop
, ST_STOP
);
439 match ("save", gfc_match_save
, ST_ATTR_DECL
);
440 match ("sync all", gfc_match_sync_all
, ST_SYNC_ALL
);
441 match ("sync images", gfc_match_sync_images
, ST_SYNC_IMAGES
);
442 match ("sync memory", gfc_match_sync_memory
, ST_SYNC_MEMORY
);
446 match ("target", gfc_match_target
, ST_ATTR_DECL
);
447 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
448 match ("type is", gfc_match_type_is
, ST_TYPE_IS
);
452 match ("use", gfc_match_use
, ST_USE
);
456 match ("value", gfc_match_value
, ST_ATTR_DECL
);
457 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
461 match ("wait", gfc_match_wait
, ST_WAIT
);
462 match ("write", gfc_match_write
, ST_WRITE
);
466 /* All else has failed, so give up. See if any of the matchers has
467 stored an error message of some sort. */
469 if (gfc_error_check () == 0)
470 gfc_error_now ("Unclassifiable statement at %C");
474 gfc_error_recovery ();
480 decode_omp_directive (void)
485 gfc_enforce_clean_symbol_state ();
487 gfc_clear_error (); /* Clear any pending errors. */
488 gfc_clear_warning (); /* Clear any pending warnings. */
492 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
493 "or ELEMENTAL procedures");
494 gfc_error_recovery ();
498 if (gfc_implicit_pure (NULL
))
499 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
501 old_locus
= gfc_current_locus
;
503 /* General OpenMP directive matching: Instead of testing every possible
504 statement, we eliminate most possibilities by peeking at the
507 c
= gfc_peek_ascii_char ();
512 match ("atomic", gfc_match_omp_atomic
, ST_OMP_ATOMIC
);
515 match ("barrier", gfc_match_omp_barrier
, ST_OMP_BARRIER
);
518 match ("critical", gfc_match_omp_critical
, ST_OMP_CRITICAL
);
521 match ("do", gfc_match_omp_do
, ST_OMP_DO
);
524 match ("end critical", gfc_match_omp_critical
, ST_OMP_END_CRITICAL
);
525 match ("end do", gfc_match_omp_end_nowait
, ST_OMP_END_DO
);
526 match ("end master", gfc_match_omp_eos
, ST_OMP_END_MASTER
);
527 match ("end ordered", gfc_match_omp_eos
, ST_OMP_END_ORDERED
);
528 match ("end parallel do", gfc_match_omp_eos
, ST_OMP_END_PARALLEL_DO
);
529 match ("end parallel sections", gfc_match_omp_eos
,
530 ST_OMP_END_PARALLEL_SECTIONS
);
531 match ("end parallel workshare", gfc_match_omp_eos
,
532 ST_OMP_END_PARALLEL_WORKSHARE
);
533 match ("end parallel", gfc_match_omp_eos
, ST_OMP_END_PARALLEL
);
534 match ("end sections", gfc_match_omp_end_nowait
, ST_OMP_END_SECTIONS
);
535 match ("end single", gfc_match_omp_end_single
, ST_OMP_END_SINGLE
);
536 match ("end task", gfc_match_omp_eos
, ST_OMP_END_TASK
);
537 match ("end workshare", gfc_match_omp_end_nowait
,
538 ST_OMP_END_WORKSHARE
);
541 match ("flush", gfc_match_omp_flush
, ST_OMP_FLUSH
);
544 match ("master", gfc_match_omp_master
, ST_OMP_MASTER
);
547 match ("ordered", gfc_match_omp_ordered
, ST_OMP_ORDERED
);
550 match ("parallel do", gfc_match_omp_parallel_do
, ST_OMP_PARALLEL_DO
);
551 match ("parallel sections", gfc_match_omp_parallel_sections
,
552 ST_OMP_PARALLEL_SECTIONS
);
553 match ("parallel workshare", gfc_match_omp_parallel_workshare
,
554 ST_OMP_PARALLEL_WORKSHARE
);
555 match ("parallel", gfc_match_omp_parallel
, ST_OMP_PARALLEL
);
558 match ("sections", gfc_match_omp_sections
, ST_OMP_SECTIONS
);
559 match ("section", gfc_match_omp_eos
, ST_OMP_SECTION
);
560 match ("single", gfc_match_omp_single
, ST_OMP_SINGLE
);
563 match ("task", gfc_match_omp_task
, ST_OMP_TASK
);
564 match ("taskwait", gfc_match_omp_taskwait
, ST_OMP_TASKWAIT
);
565 match ("threadprivate", gfc_match_omp_threadprivate
,
566 ST_OMP_THREADPRIVATE
);
568 match ("workshare", gfc_match_omp_workshare
, ST_OMP_WORKSHARE
);
572 /* All else has failed, so give up. See if any of the matchers has
573 stored an error message of some sort. */
575 if (gfc_error_check () == 0)
576 gfc_error_now ("Unclassifiable OpenMP directive at %C");
580 gfc_error_recovery ();
586 decode_gcc_attribute (void)
590 gfc_enforce_clean_symbol_state ();
592 gfc_clear_error (); /* Clear any pending errors. */
593 gfc_clear_warning (); /* Clear any pending warnings. */
594 old_locus
= gfc_current_locus
;
596 match ("attributes", gfc_match_gcc_attributes
, ST_ATTR_DECL
);
598 /* All else has failed, so give up. See if any of the matchers has
599 stored an error message of some sort. */
601 if (gfc_error_check () == 0)
602 gfc_error_now ("Unclassifiable GCC directive at %C");
606 gfc_error_recovery ();
614 /* Get the next statement in free form source. */
623 at_bol
= gfc_at_bol ();
624 gfc_gobble_whitespace ();
626 c
= gfc_peek_ascii_char ();
632 /* Found a statement label? */
633 m
= gfc_match_st_label (&gfc_statement_label
);
635 d
= gfc_peek_ascii_char ();
636 if (m
!= MATCH_YES
|| !gfc_is_whitespace (d
))
638 gfc_match_small_literal_int (&i
, &cnt
);
641 gfc_error_now ("Too many digits in statement label at %C");
644 gfc_error_now ("Zero is not a valid statement label at %C");
647 c
= gfc_next_ascii_char ();
650 if (!gfc_is_whitespace (c
))
651 gfc_error_now ("Non-numeric character in statement label at %C");
657 label_locus
= gfc_current_locus
;
659 gfc_gobble_whitespace ();
661 if (at_bol
&& gfc_peek_ascii_char () == ';')
663 gfc_error_now ("Semicolon at %C needs to be preceded by "
665 gfc_next_ascii_char (); /* Eat up the semicolon. */
669 if (gfc_match_eos () == MATCH_YES
)
671 gfc_warning_now ("Ignoring statement label in empty statement "
672 "at %L", &label_locus
);
673 gfc_free_st_label (gfc_statement_label
);
674 gfc_statement_label
= NULL
;
681 /* Comments have already been skipped by the time we get here,
682 except for GCC attributes and OpenMP directives. */
684 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
685 c
= gfc_peek_ascii_char ();
691 c
= gfc_next_ascii_char ();
692 for (i
= 0; i
< 4; i
++, c
= gfc_next_ascii_char ())
693 gcc_assert (c
== "gcc$"[i
]);
695 gfc_gobble_whitespace ();
696 return decode_gcc_attribute ();
699 else if (c
== '$' && gfc_option
.gfc_flag_openmp
)
703 c
= gfc_next_ascii_char ();
704 for (i
= 0; i
< 4; i
++, c
= gfc_next_ascii_char ())
705 gcc_assert (c
== "$omp"[i
]);
707 gcc_assert (c
== ' ' || c
== '\t');
708 gfc_gobble_whitespace ();
709 return decode_omp_directive ();
715 if (at_bol
&& c
== ';')
717 if (!(gfc_option
.allow_std
& GFC_STD_F2008
))
718 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
720 gfc_next_ascii_char (); /* Eat up the semicolon. */
724 return decode_statement ();
728 /* Get the next statement in fixed-form source. */
733 int label
, digit_flag
, i
;
738 return decode_statement ();
740 /* Skip past the current label field, parsing a statement label if
741 one is there. This is a weird number parser, since the number is
742 contained within five columns and can have any kind of embedded
743 spaces. We also check for characters that make the rest of the
749 for (i
= 0; i
< 5; i
++)
751 c
= gfc_next_char_literal (NONSTRING
);
768 label
= label
* 10 + ((unsigned char) c
- '0');
769 label_locus
= gfc_current_locus
;
773 /* Comments have already been skipped by the time we get
774 here, except for GCC attributes and OpenMP directives. */
777 c
= gfc_next_char_literal (NONSTRING
);
779 if (TOLOWER (c
) == 'g')
781 for (i
= 0; i
< 4; i
++, c
= gfc_next_char_literal (NONSTRING
))
782 gcc_assert (TOLOWER (c
) == "gcc$"[i
]);
784 return decode_gcc_attribute ();
786 else if (c
== '$' && gfc_option
.gfc_flag_openmp
)
788 for (i
= 0; i
< 4; i
++, c
= gfc_next_char_literal (NONSTRING
))
789 gcc_assert ((char) gfc_wide_tolower (c
) == "$omp"[i
]);
791 if (c
!= ' ' && c
!= '0')
793 gfc_buffer_error (0);
794 gfc_error ("Bad continuation line at %C");
798 return decode_omp_directive ();
802 /* Comments have already been skipped by the time we get
803 here so don't bother checking for them. */
806 gfc_buffer_error (0);
807 gfc_error ("Non-numeric character in statement label at %C");
815 gfc_warning_now ("Zero is not a valid statement label at %C");
818 /* We've found a valid statement label. */
819 gfc_statement_label
= gfc_get_st_label (label
);
823 /* Since this line starts a statement, it cannot be a continuation
824 of a previous statement. If we see something here besides a
825 space or zero, it must be a bad continuation line. */
827 c
= gfc_next_char_literal (NONSTRING
);
831 if (c
!= ' ' && c
!= '0')
833 gfc_buffer_error (0);
834 gfc_error ("Bad continuation line at %C");
838 /* Now that we've taken care of the statement label columns, we have
839 to make sure that the first nonblank character is not a '!'. If
840 it is, the rest of the line is a comment. */
844 loc
= gfc_current_locus
;
845 c
= gfc_next_char_literal (NONSTRING
);
847 while (gfc_is_whitespace (c
));
851 gfc_current_locus
= loc
;
856 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
857 else if (!(gfc_option
.allow_std
& GFC_STD_F2008
))
858 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
863 if (gfc_match_eos () == MATCH_YES
)
866 /* At this point, we've got a nonblank statement to parse. */
867 return decode_statement ();
871 gfc_warning_now ("Ignoring statement label in empty statement at %L",
874 gfc_current_locus
.lb
->truncated
= 0;
880 /* Return the next non-ST_NONE statement to the caller. We also worry
881 about including files and the ends of include files at this stage. */
884 next_statement (void)
889 gfc_enforce_clean_symbol_state ();
891 gfc_new_block
= NULL
;
893 gfc_current_ns
->old_cl_list
= gfc_current_ns
->cl_list
;
894 gfc_current_ns
->old_equiv
= gfc_current_ns
->equiv
;
897 gfc_statement_label
= NULL
;
898 gfc_buffer_error (1);
903 gfc_skip_comments ();
911 if (gfc_define_undef_line ())
914 old_locus
= gfc_current_locus
;
916 st
= (gfc_current_form
== FORM_FIXED
) ? next_fixed () : next_free ();
922 gfc_buffer_error (0);
924 if (st
== ST_GET_FCN_CHARACTERISTICS
&& gfc_statement_label
!= NULL
)
926 gfc_free_st_label (gfc_statement_label
);
927 gfc_statement_label
= NULL
;
928 gfc_current_locus
= old_locus
;
932 check_statement_label (st
);
938 /****************************** Parser ***********************************/
940 /* The parser subroutines are of type 'try' that fail if the file ends
943 /* Macros that expand to case-labels for various classes of
944 statements. Start with executable statements that directly do
947 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
948 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
949 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
950 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
951 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
952 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
953 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
954 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
955 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_ERROR_STOP: \
956 case ST_SYNC_ALL: case ST_SYNC_IMAGES: case ST_SYNC_MEMORY
958 /* Statements that mark other executable statements. */
960 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
961 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
962 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
963 case ST_OMP_PARALLEL: \
964 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
965 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
966 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
967 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
968 case ST_OMP_TASK: case ST_CRITICAL
970 /* Declaration statements */
972 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
973 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
974 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
977 /* Block end statements. Errors associated with interchanging these
978 are detected in gfc_match_end(). */
980 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
981 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
982 case ST_END_BLOCK: case ST_END_ASSOCIATE
985 /* Push a new state onto the stack. */
988 push_state (gfc_state_data
*p
, gfc_compile_state new_state
, gfc_symbol
*sym
)
990 p
->state
= new_state
;
991 p
->previous
= gfc_state_stack
;
993 p
->head
= p
->tail
= NULL
;
994 p
->do_variable
= NULL
;
996 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
997 construct statement was accepted right before pushing the state. Thus,
998 the construct's gfc_code is available as tail of the parent state. */
999 gcc_assert (gfc_state_stack
);
1000 p
->construct
= gfc_state_stack
->tail
;
1002 gfc_state_stack
= p
;
1006 /* Pop the current state. */
1010 gfc_state_stack
= gfc_state_stack
->previous
;
1014 /* Try to find the given state in the state stack. */
1017 gfc_find_state (gfc_compile_state state
)
1021 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1022 if (p
->state
== state
)
1025 return (p
== NULL
) ? FAILURE
: SUCCESS
;
1029 /* Starts a new level in the statement list. */
1032 new_level (gfc_code
*q
)
1036 p
= q
->block
= gfc_get_code ();
1038 gfc_state_stack
->head
= gfc_state_stack
->tail
= p
;
1044 /* Add the current new_st code structure and adds it to the current
1045 program unit. As a side-effect, it zeroes the new_st. */
1048 add_statement (void)
1052 p
= gfc_get_code ();
1055 p
->loc
= gfc_current_locus
;
1057 if (gfc_state_stack
->head
== NULL
)
1058 gfc_state_stack
->head
= p
;
1060 gfc_state_stack
->tail
->next
= p
;
1062 while (p
->next
!= NULL
)
1065 gfc_state_stack
->tail
= p
;
1067 gfc_clear_new_st ();
1073 /* Frees everything associated with the current statement. */
1076 undo_new_statement (void)
1078 gfc_free_statements (new_st
.block
);
1079 gfc_free_statements (new_st
.next
);
1080 gfc_free_statement (&new_st
);
1081 gfc_clear_new_st ();
1085 /* If the current statement has a statement label, make sure that it
1086 is allowed to, or should have one. */
1089 check_statement_label (gfc_statement st
)
1093 if (gfc_statement_label
== NULL
)
1095 if (st
== ST_FORMAT
)
1096 gfc_error ("FORMAT statement at %L does not have a statement label",
1103 case ST_END_PROGRAM
:
1104 case ST_END_FUNCTION
:
1105 case ST_END_SUBROUTINE
:
1109 case ST_END_CRITICAL
:
1112 type
= ST_LABEL_TARGET
;
1116 type
= ST_LABEL_FORMAT
;
1119 /* Statement labels are not restricted from appearing on a
1120 particular line. However, there are plenty of situations
1121 where the resulting label can't be referenced. */
1124 type
= ST_LABEL_BAD_TARGET
;
1128 gfc_define_st_label (gfc_statement_label
, type
, &label_locus
);
1130 new_st
.here
= gfc_statement_label
;
1134 /* Figures out what the enclosing program unit is. This will be a
1135 function, subroutine, program, block data or module. */
1138 gfc_enclosing_unit (gfc_compile_state
* result
)
1142 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1143 if (p
->state
== COMP_FUNCTION
|| p
->state
== COMP_SUBROUTINE
1144 || p
->state
== COMP_MODULE
|| p
->state
== COMP_BLOCK_DATA
1145 || p
->state
== COMP_PROGRAM
)
1154 *result
= COMP_PROGRAM
;
1159 /* Translate a statement enum to a string. */
1162 gfc_ascii_statement (gfc_statement st
)
1168 case ST_ARITHMETIC_IF
:
1169 p
= _("arithmetic IF");
1178 p
= _("attribute declaration");
1214 p
= _("data declaration");
1222 case ST_DERIVED_DECL
:
1223 p
= _("derived type declaration");
1237 case ST_END_ASSOCIATE
:
1238 p
= "END ASSOCIATE";
1243 case ST_END_BLOCK_DATA
:
1244 p
= "END BLOCK DATA";
1246 case ST_END_CRITICAL
:
1258 case ST_END_FUNCTION
:
1264 case ST_END_INTERFACE
:
1265 p
= "END INTERFACE";
1270 case ST_END_PROGRAM
:
1276 case ST_END_SUBROUTINE
:
1277 p
= "END SUBROUTINE";
1288 case ST_EQUIVALENCE
:
1300 case ST_FORALL_BLOCK
: /* Fall through */
1322 case ST_IMPLICIT_NONE
:
1323 p
= "IMPLICIT NONE";
1325 case ST_IMPLIED_ENDDO
:
1326 p
= _("implied END DO");
1352 case ST_MODULE_PROC
:
1353 p
= "MODULE PROCEDURE";
1385 case ST_SYNC_IMAGES
:
1388 case ST_SYNC_MEMORY
:
1400 case ST_WHERE_BLOCK
: /* Fall through */
1411 p
= _("assignment");
1413 case ST_POINTER_ASSIGNMENT
:
1414 p
= _("pointer assignment");
1416 case ST_SELECT_CASE
:
1419 case ST_SELECT_TYPE
:
1434 case ST_STATEMENT_FUNCTION
:
1435 p
= "STATEMENT FUNCTION";
1437 case ST_LABEL_ASSIGNMENT
:
1438 p
= "LABEL ASSIGNMENT";
1441 p
= "ENUM DEFINITION";
1444 p
= "ENUMERATOR DEFINITION";
1452 case ST_OMP_BARRIER
:
1453 p
= "!$OMP BARRIER";
1455 case ST_OMP_CRITICAL
:
1456 p
= "!$OMP CRITICAL";
1461 case ST_OMP_END_CRITICAL
:
1462 p
= "!$OMP END CRITICAL";
1467 case ST_OMP_END_MASTER
:
1468 p
= "!$OMP END MASTER";
1470 case ST_OMP_END_ORDERED
:
1471 p
= "!$OMP END ORDERED";
1473 case ST_OMP_END_PARALLEL
:
1474 p
= "!$OMP END PARALLEL";
1476 case ST_OMP_END_PARALLEL_DO
:
1477 p
= "!$OMP END PARALLEL DO";
1479 case ST_OMP_END_PARALLEL_SECTIONS
:
1480 p
= "!$OMP END PARALLEL SECTIONS";
1482 case ST_OMP_END_PARALLEL_WORKSHARE
:
1483 p
= "!$OMP END PARALLEL WORKSHARE";
1485 case ST_OMP_END_SECTIONS
:
1486 p
= "!$OMP END SECTIONS";
1488 case ST_OMP_END_SINGLE
:
1489 p
= "!$OMP END SINGLE";
1491 case ST_OMP_END_TASK
:
1492 p
= "!$OMP END TASK";
1494 case ST_OMP_END_WORKSHARE
:
1495 p
= "!$OMP END WORKSHARE";
1503 case ST_OMP_ORDERED
:
1504 p
= "!$OMP ORDERED";
1506 case ST_OMP_PARALLEL
:
1507 p
= "!$OMP PARALLEL";
1509 case ST_OMP_PARALLEL_DO
:
1510 p
= "!$OMP PARALLEL DO";
1512 case ST_OMP_PARALLEL_SECTIONS
:
1513 p
= "!$OMP PARALLEL SECTIONS";
1515 case ST_OMP_PARALLEL_WORKSHARE
:
1516 p
= "!$OMP PARALLEL WORKSHARE";
1518 case ST_OMP_SECTIONS
:
1519 p
= "!$OMP SECTIONS";
1521 case ST_OMP_SECTION
:
1522 p
= "!$OMP SECTION";
1530 case ST_OMP_TASKWAIT
:
1531 p
= "!$OMP TASKWAIT";
1533 case ST_OMP_THREADPRIVATE
:
1534 p
= "!$OMP THREADPRIVATE";
1536 case ST_OMP_WORKSHARE
:
1537 p
= "!$OMP WORKSHARE";
1540 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1547 /* Create a symbol for the main program and assign it to ns->proc_name. */
1550 main_program_symbol (gfc_namespace
*ns
, const char *name
)
1552 gfc_symbol
*main_program
;
1553 symbol_attribute attr
;
1555 gfc_get_symbol (name
, ns
, &main_program
);
1556 gfc_clear_attr (&attr
);
1557 attr
.flavor
= FL_PROGRAM
;
1558 attr
.proc
= PROC_UNKNOWN
;
1559 attr
.subroutine
= 1;
1560 attr
.access
= ACCESS_PUBLIC
;
1561 attr
.is_main_program
= 1;
1562 main_program
->attr
= attr
;
1563 main_program
->declared_at
= gfc_current_locus
;
1564 ns
->proc_name
= main_program
;
1565 gfc_commit_symbols ();
1569 /* Do whatever is necessary to accept the last statement. */
1572 accept_statement (gfc_statement st
)
1580 case ST_IMPLICIT_NONE
:
1581 gfc_set_implicit_none ();
1590 gfc_current_ns
->proc_name
= gfc_new_block
;
1593 /* If the statement is the end of a block, lay down a special code
1594 that allows a branch to the end of the block from within the
1595 construct. IF and SELECT are treated differently from DO
1596 (where EXEC_NOP is added inside the loop) for two
1598 1. END DO has a meaning in the sense that after a GOTO to
1599 it, the loop counter must be increased.
1600 2. IF blocks and SELECT blocks can consist of multiple
1601 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
1602 Putting the label before the END IF would make the jump
1603 from, say, the ELSE IF block to the END IF illegal. */
1607 case ST_END_CRITICAL
:
1608 if (gfc_statement_label
!= NULL
)
1610 new_st
.op
= EXEC_END_BLOCK
;
1615 /* The end-of-program unit statements do not get the special
1616 marker and require a statement of some sort if they are a
1619 case ST_END_PROGRAM
:
1620 case ST_END_FUNCTION
:
1621 case ST_END_SUBROUTINE
:
1622 if (gfc_statement_label
!= NULL
)
1624 new_st
.op
= EXEC_RETURN
;
1629 new_st
.op
= EXEC_END_PROCEDURE
;
1645 gfc_commit_symbols ();
1646 gfc_warning_check ();
1647 gfc_clear_new_st ();
1651 /* Undo anything tentative that has been built for the current
1655 reject_statement (void)
1657 /* Revert to the previous charlen chain. */
1658 gfc_free_charlen (gfc_current_ns
->cl_list
, gfc_current_ns
->old_cl_list
);
1659 gfc_current_ns
->cl_list
= gfc_current_ns
->old_cl_list
;
1661 gfc_free_equiv_until (gfc_current_ns
->equiv
, gfc_current_ns
->old_equiv
);
1662 gfc_current_ns
->equiv
= gfc_current_ns
->old_equiv
;
1664 gfc_new_block
= NULL
;
1665 gfc_undo_symbols ();
1666 gfc_clear_warning ();
1667 undo_new_statement ();
1671 /* Generic complaint about an out of order statement. We also do
1672 whatever is necessary to clean up. */
1675 unexpected_statement (gfc_statement st
)
1677 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st
));
1679 reject_statement ();
1683 /* Given the next statement seen by the matcher, make sure that it is
1684 in proper order with the last. This subroutine is initialized by
1685 calling it with an argument of ST_NONE. If there is a problem, we
1686 issue an error and return FAILURE. Otherwise we return SUCCESS.
1688 Individual parsers need to verify that the statements seen are
1689 valid before calling here, i.e., ENTRY statements are not allowed in
1690 INTERFACE blocks. The following diagram is taken from the standard:
1692 +---------------------------------------+
1693 | program subroutine function module |
1694 +---------------------------------------+
1696 +---------------------------------------+
1698 +---------------------------------------+
1700 | +-----------+------------------+
1701 | | parameter | implicit |
1702 | +-----------+------------------+
1703 | format | | derived type |
1704 | entry | parameter | interface |
1705 | | data | specification |
1706 | | | statement func |
1707 | +-----------+------------------+
1708 | | data | executable |
1709 +--------+-----------+------------------+
1711 +---------------------------------------+
1712 | internal module/subprogram |
1713 +---------------------------------------+
1715 +---------------------------------------+
1724 ORDER_IMPLICIT_NONE
,
1732 enum state_order state
;
1733 gfc_statement last_statement
;
1739 verify_st_order (st_state
*p
, gfc_statement st
, bool silent
)
1745 p
->state
= ORDER_START
;
1749 if (p
->state
> ORDER_USE
)
1751 p
->state
= ORDER_USE
;
1755 if (p
->state
> ORDER_IMPORT
)
1757 p
->state
= ORDER_IMPORT
;
1760 case ST_IMPLICIT_NONE
:
1761 if (p
->state
> ORDER_IMPLICIT_NONE
)
1764 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1765 statement disqualifies a USE but not an IMPLICIT NONE.
1766 Duplicate IMPLICIT NONEs are caught when the implicit types
1769 p
->state
= ORDER_IMPLICIT_NONE
;
1773 if (p
->state
> ORDER_IMPLICIT
)
1775 p
->state
= ORDER_IMPLICIT
;
1780 if (p
->state
< ORDER_IMPLICIT_NONE
)
1781 p
->state
= ORDER_IMPLICIT_NONE
;
1785 if (p
->state
>= ORDER_EXEC
)
1787 if (p
->state
< ORDER_IMPLICIT
)
1788 p
->state
= ORDER_IMPLICIT
;
1792 if (p
->state
< ORDER_SPEC
)
1793 p
->state
= ORDER_SPEC
;
1798 case ST_DERIVED_DECL
:
1800 if (p
->state
>= ORDER_EXEC
)
1802 if (p
->state
< ORDER_SPEC
)
1803 p
->state
= ORDER_SPEC
;
1808 if (p
->state
< ORDER_EXEC
)
1809 p
->state
= ORDER_EXEC
;
1813 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1814 gfc_ascii_statement (st
));
1817 /* All is well, record the statement in case we need it next time. */
1818 p
->where
= gfc_current_locus
;
1819 p
->last_statement
= st
;
1824 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1825 gfc_ascii_statement (st
),
1826 gfc_ascii_statement (p
->last_statement
), &p
->where
);
1832 /* Handle an unexpected end of file. This is a show-stopper... */
1834 static void unexpected_eof (void) ATTRIBUTE_NORETURN
;
1837 unexpected_eof (void)
1841 gfc_error ("Unexpected end of file in '%s'", gfc_source_file
);
1843 /* Memory cleanup. Move to "second to last". */
1844 for (p
= gfc_state_stack
; p
&& p
->previous
&& p
->previous
->previous
;
1847 gfc_current_ns
->code
= (p
&& p
->previous
) ? p
->head
: NULL
;
1850 longjmp (eof_buf
, 1);
1854 /* Parse the CONTAINS section of a derived type definition. */
1856 gfc_access gfc_typebound_default_access
;
1859 parse_derived_contains (void)
1862 bool seen_private
= false;
1863 bool seen_comps
= false;
1864 bool error_flag
= false;
1867 gcc_assert (gfc_current_state () == COMP_DERIVED
);
1868 gcc_assert (gfc_current_block ());
1870 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
1872 if (gfc_current_block ()->attr
.sequence
)
1873 gfc_error ("Derived-type '%s' with SEQUENCE must not have a CONTAINS"
1874 " section at %C", gfc_current_block ()->name
);
1875 if (gfc_current_block ()->attr
.is_bind_c
)
1876 gfc_error ("Derived-type '%s' with BIND(C) must not have a CONTAINS"
1877 " section at %C", gfc_current_block ()->name
);
1879 accept_statement (ST_CONTAINS
);
1880 push_state (&s
, COMP_DERIVED_CONTAINS
, NULL
);
1882 gfc_typebound_default_access
= ACCESS_PUBLIC
;
1888 st
= next_statement ();
1896 gfc_error ("Components in TYPE at %C must precede CONTAINS");
1900 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Type-bound"
1901 " procedure at %C") == FAILURE
)
1904 accept_statement (ST_PROCEDURE
);
1909 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: GENERIC binding"
1910 " at %C") == FAILURE
)
1913 accept_statement (ST_GENERIC
);
1918 if (gfc_notify_std (GFC_STD_F2003
,
1919 "Fortran 2003: FINAL procedure declaration"
1920 " at %C") == FAILURE
)
1923 accept_statement (ST_FINAL
);
1931 && (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: Derived type "
1932 "definition at %C with empty CONTAINS "
1933 "section") == FAILURE
))
1936 /* ST_END_TYPE is accepted by parse_derived after return. */
1940 if (gfc_find_state (COMP_MODULE
) == FAILURE
)
1942 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1949 gfc_error ("PRIVATE statement at %C must precede procedure"
1956 gfc_error ("Duplicate PRIVATE statement at %C");
1960 accept_statement (ST_PRIVATE
);
1961 gfc_typebound_default_access
= ACCESS_PRIVATE
;
1962 seen_private
= true;
1966 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
1970 gfc_error ("Already inside a CONTAINS block at %C");
1974 unexpected_statement (st
);
1982 reject_statement ();
1986 gcc_assert (gfc_current_state () == COMP_DERIVED
);
1992 /* Parse a derived type. */
1995 parse_derived (void)
1997 int compiling_type
, seen_private
, seen_sequence
, seen_component
;
2003 accept_statement (ST_DERIVED_DECL
);
2004 push_state (&s
, COMP_DERIVED
, gfc_new_block
);
2006 gfc_new_block
->component_access
= ACCESS_PUBLIC
;
2013 while (compiling_type
)
2015 st
= next_statement ();
2023 accept_statement (st
);
2028 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2035 if (!seen_component
)
2036 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Derived type "
2037 "definition at %C without components");
2039 accept_statement (ST_END_TYPE
);
2043 if (gfc_find_state (COMP_MODULE
) == FAILURE
)
2045 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2052 gfc_error ("PRIVATE statement at %C must precede "
2053 "structure components");
2058 gfc_error ("Duplicate PRIVATE statement at %C");
2060 s
.sym
->component_access
= ACCESS_PRIVATE
;
2062 accept_statement (ST_PRIVATE
);
2069 gfc_error ("SEQUENCE statement at %C must precede "
2070 "structure components");
2074 if (gfc_current_block ()->attr
.sequence
)
2075 gfc_warning ("SEQUENCE attribute at %C already specified in "
2080 gfc_error ("Duplicate SEQUENCE statement at %C");
2084 gfc_add_sequence (&gfc_current_block ()->attr
,
2085 gfc_current_block ()->name
, NULL
);
2089 gfc_notify_std (GFC_STD_F2003
,
2090 "Fortran 2003: CONTAINS block in derived type"
2091 " definition at %C");
2093 accept_statement (ST_CONTAINS
);
2094 parse_derived_contains ();
2098 unexpected_statement (st
);
2103 /* need to verify that all fields of the derived type are
2104 * interoperable with C if the type is declared to be bind(c)
2106 sym
= gfc_current_block ();
2107 for (c
= sym
->components
; c
; c
= c
->next
)
2109 /* Look for allocatable components. */
2110 if (c
->attr
.allocatable
2111 || (c
->ts
.type
== BT_CLASS
&& CLASS_DATA (c
)->attr
.allocatable
)
2112 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.alloc_comp
))
2113 sym
->attr
.alloc_comp
= 1;
2115 /* Look for pointer components. */
2117 || (c
->ts
.type
== BT_CLASS
&& CLASS_DATA (c
)->attr
.class_pointer
)
2118 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.pointer_comp
))
2119 sym
->attr
.pointer_comp
= 1;
2121 /* Look for procedure pointer components. */
2122 if (c
->attr
.proc_pointer
2123 || (c
->ts
.type
== BT_DERIVED
2124 && c
->ts
.u
.derived
->attr
.proc_pointer_comp
))
2125 sym
->attr
.proc_pointer_comp
= 1;
2127 /* Looking for coarray components. */
2128 if (c
->attr
.codimension
2129 || (c
->attr
.coarray_comp
&& !c
->attr
.pointer
&& !c
->attr
.allocatable
))
2130 sym
->attr
.coarray_comp
= 1;
2132 /* Look for private components. */
2133 if (sym
->component_access
== ACCESS_PRIVATE
2134 || c
->attr
.access
== ACCESS_PRIVATE
2135 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.private_comp
))
2136 sym
->attr
.private_comp
= 1;
2139 if (!seen_component
)
2140 sym
->attr
.zero_comp
= 1;
2146 /* Parse an ENUM. */
2154 int seen_enumerator
= 0;
2156 push_state (&s
, COMP_ENUM
, gfc_new_block
);
2160 while (compiling_enum
)
2162 st
= next_statement ();
2170 seen_enumerator
= 1;
2171 accept_statement (st
);
2176 if (!seen_enumerator
)
2177 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2178 accept_statement (st
);
2182 gfc_free_enum_history ();
2183 unexpected_statement (st
);
2191 /* Parse an interface. We must be able to deal with the possibility
2192 of recursive interfaces. The parse_spec() subroutine is mutually
2193 recursive with parse_interface(). */
2195 static gfc_statement
parse_spec (gfc_statement
);
2198 parse_interface (void)
2200 gfc_compile_state new_state
= COMP_NONE
, current_state
;
2201 gfc_symbol
*prog_unit
, *sym
;
2202 gfc_interface_info save
;
2203 gfc_state_data s1
, s2
;
2207 accept_statement (ST_INTERFACE
);
2209 current_interface
.ns
= gfc_current_ns
;
2210 save
= current_interface
;
2212 sym
= (current_interface
.type
== INTERFACE_GENERIC
2213 || current_interface
.type
== INTERFACE_USER_OP
)
2214 ? gfc_new_block
: NULL
;
2216 push_state (&s1
, COMP_INTERFACE
, sym
);
2217 current_state
= COMP_NONE
;
2220 gfc_current_ns
= gfc_get_namespace (current_interface
.ns
, 0);
2222 st
= next_statement ();
2230 if (st
== ST_SUBROUTINE
)
2231 new_state
= COMP_SUBROUTINE
;
2232 else if (st
== ST_FUNCTION
)
2233 new_state
= COMP_FUNCTION
;
2234 if (gfc_new_block
->attr
.pointer
)
2236 gfc_new_block
->attr
.pointer
= 0;
2237 gfc_new_block
->attr
.proc_pointer
= 1;
2239 if (gfc_add_explicit_interface (gfc_new_block
, IFSRC_IFBODY
,
2240 gfc_new_block
->formal
, NULL
) == FAILURE
)
2242 reject_statement ();
2243 gfc_free_namespace (gfc_current_ns
);
2249 case ST_MODULE_PROC
: /* The module procedure matcher makes
2250 sure the context is correct. */
2251 accept_statement (st
);
2252 gfc_free_namespace (gfc_current_ns
);
2255 case ST_END_INTERFACE
:
2256 gfc_free_namespace (gfc_current_ns
);
2257 gfc_current_ns
= current_interface
.ns
;
2261 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2262 gfc_ascii_statement (st
));
2263 reject_statement ();
2264 gfc_free_namespace (gfc_current_ns
);
2269 /* Make sure that the generic name has the right attribute. */
2270 if (current_interface
.type
== INTERFACE_GENERIC
2271 && current_state
== COMP_NONE
)
2273 if (new_state
== COMP_FUNCTION
&& sym
)
2274 gfc_add_function (&sym
->attr
, sym
->name
, NULL
);
2275 else if (new_state
== COMP_SUBROUTINE
&& sym
)
2276 gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
);
2278 current_state
= new_state
;
2281 if (current_interface
.type
== INTERFACE_ABSTRACT
)
2283 gfc_add_abstract (&gfc_new_block
->attr
, &gfc_current_locus
);
2284 if (gfc_is_intrinsic_typename (gfc_new_block
->name
))
2285 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
2286 "cannot be the same as an intrinsic type",
2287 gfc_new_block
->name
);
2290 push_state (&s2
, new_state
, gfc_new_block
);
2291 accept_statement (st
);
2292 prog_unit
= gfc_new_block
;
2293 prog_unit
->formal_ns
= gfc_current_ns
;
2294 proc_locus
= gfc_current_locus
;
2297 /* Read data declaration statements. */
2298 st
= parse_spec (ST_NONE
);
2300 /* Since the interface block does not permit an IMPLICIT statement,
2301 the default type for the function or the result must be taken
2302 from the formal namespace. */
2303 if (new_state
== COMP_FUNCTION
)
2305 if (prog_unit
->result
== prog_unit
2306 && prog_unit
->ts
.type
== BT_UNKNOWN
)
2307 gfc_set_default_type (prog_unit
, 1, prog_unit
->formal_ns
);
2308 else if (prog_unit
->result
!= prog_unit
2309 && prog_unit
->result
->ts
.type
== BT_UNKNOWN
)
2310 gfc_set_default_type (prog_unit
->result
, 1,
2311 prog_unit
->formal_ns
);
2314 if (st
!= ST_END_SUBROUTINE
&& st
!= ST_END_FUNCTION
)
2316 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2317 gfc_ascii_statement (st
));
2318 reject_statement ();
2322 /* Add EXTERNAL attribute to function or subroutine. */
2323 if (current_interface
.type
!= INTERFACE_ABSTRACT
&& !prog_unit
->attr
.dummy
)
2324 gfc_add_external (&prog_unit
->attr
, &gfc_current_locus
);
2326 current_interface
= save
;
2327 gfc_add_interface (prog_unit
);
2330 if (current_interface
.ns
2331 && current_interface
.ns
->proc_name
2332 && strcmp (current_interface
.ns
->proc_name
->name
,
2333 prog_unit
->name
) == 0)
2334 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2335 "enclosing procedure", prog_unit
->name
, &proc_locus
);
2344 /* Associate function characteristics by going back to the function
2345 declaration and rematching the prefix. */
2348 match_deferred_characteristics (gfc_typespec
* ts
)
2351 match m
= MATCH_ERROR
;
2352 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2354 loc
= gfc_current_locus
;
2356 gfc_current_locus
= gfc_current_block ()->declared_at
;
2359 gfc_buffer_error (1);
2360 m
= gfc_match_prefix (ts
);
2361 gfc_buffer_error (0);
2363 if (ts
->type
== BT_DERIVED
)
2371 /* Only permit one go at the characteristic association. */
2375 /* Set the function locus correctly. If we have not found the
2376 function name, there is an error. */
2378 && gfc_match ("function% %n", name
) == MATCH_YES
2379 && strcmp (name
, gfc_current_block ()->name
) == 0)
2381 gfc_current_block ()->declared_at
= gfc_current_locus
;
2382 gfc_commit_symbols ();
2387 gfc_undo_symbols ();
2390 gfc_current_locus
=loc
;
2395 /* Check specification-expressions in the function result of the currently
2396 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2397 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2398 scope are not yet parsed so this has to be delayed up to parse_spec. */
2401 check_function_result_typed (void)
2403 gfc_typespec
* ts
= &gfc_current_ns
->proc_name
->result
->ts
;
2405 gcc_assert (gfc_current_state () == COMP_FUNCTION
);
2406 gcc_assert (ts
->type
!= BT_UNKNOWN
);
2408 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2409 /* TODO: Extend when KIND type parameters are implemented. */
2410 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
)
2411 gfc_expr_check_typed (ts
->u
.cl
->length
, gfc_current_ns
, true);
2415 /* Parse a set of specification statements. Returns the statement
2416 that doesn't fit. */
2418 static gfc_statement
2419 parse_spec (gfc_statement st
)
2422 bool function_result_typed
= false;
2423 bool bad_characteristic
= false;
2426 verify_st_order (&ss
, ST_NONE
, false);
2428 st
= next_statement ();
2430 /* If we are not inside a function or don't have a result specified so far,
2431 do nothing special about it. */
2432 if (gfc_current_state () != COMP_FUNCTION
)
2433 function_result_typed
= true;
2436 gfc_symbol
* proc
= gfc_current_ns
->proc_name
;
2439 if (proc
->result
->ts
.type
== BT_UNKNOWN
)
2440 function_result_typed
= true;
2445 /* If we're inside a BLOCK construct, some statements are disallowed.
2446 Check this here. Attribute declaration statements like INTENT, OPTIONAL
2447 or VALUE are also disallowed, but they don't have a particular ST_*
2448 key so we have to check for them individually in their matcher routine. */
2449 if (gfc_current_state () == COMP_BLOCK
)
2453 case ST_IMPLICIT_NONE
:
2456 case ST_EQUIVALENCE
:
2457 case ST_STATEMENT_FUNCTION
:
2458 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
2459 gfc_ascii_statement (st
));
2460 reject_statement ();
2467 /* If we find a statement that can not be followed by an IMPLICIT statement
2468 (and thus we can expect to see none any further), type the function result
2469 if it has not yet been typed. Be careful not to give the END statement
2470 to verify_st_order! */
2471 if (!function_result_typed
&& st
!= ST_GET_FCN_CHARACTERISTICS
)
2473 bool verify_now
= false;
2475 if (st
== ST_END_FUNCTION
|| st
== ST_CONTAINS
)
2480 verify_st_order (&dummyss
, ST_NONE
, false);
2481 verify_st_order (&dummyss
, st
, false);
2483 if (verify_st_order (&dummyss
, ST_IMPLICIT
, true) == FAILURE
)
2489 check_function_result_typed ();
2490 function_result_typed
= true;
2499 case ST_IMPLICIT_NONE
:
2501 if (!function_result_typed
)
2503 check_function_result_typed ();
2504 function_result_typed
= true;
2510 case ST_DATA
: /* Not allowed in interfaces */
2511 if (gfc_current_state () == COMP_INTERFACE
)
2521 case ST_DERIVED_DECL
:
2524 if (verify_st_order (&ss
, st
, false) == FAILURE
)
2526 reject_statement ();
2527 st
= next_statement ();
2537 case ST_DERIVED_DECL
:
2543 if (gfc_current_state () != COMP_MODULE
)
2545 gfc_error ("%s statement must appear in a MODULE",
2546 gfc_ascii_statement (st
));
2547 reject_statement ();
2551 if (gfc_current_ns
->default_access
!= ACCESS_UNKNOWN
)
2553 gfc_error ("%s statement at %C follows another accessibility "
2554 "specification", gfc_ascii_statement (st
));
2555 reject_statement ();
2559 gfc_current_ns
->default_access
= (st
== ST_PUBLIC
)
2560 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
2564 case ST_STATEMENT_FUNCTION
:
2565 if (gfc_current_state () == COMP_MODULE
)
2567 unexpected_statement (st
);
2575 accept_statement (st
);
2576 st
= next_statement ();
2580 accept_statement (st
);
2582 st
= next_statement ();
2585 case ST_GET_FCN_CHARACTERISTICS
:
2586 /* This statement triggers the association of a function's result
2588 ts
= &gfc_current_block ()->result
->ts
;
2589 if (match_deferred_characteristics (ts
) != MATCH_YES
)
2590 bad_characteristic
= true;
2592 st
= next_statement ();
2599 /* If match_deferred_characteristics failed, then there is an error. */
2600 if (bad_characteristic
)
2602 ts
= &gfc_current_block ()->result
->ts
;
2603 if (ts
->type
!= BT_DERIVED
)
2604 gfc_error ("Bad kind expression for function '%s' at %L",
2605 gfc_current_block ()->name
,
2606 &gfc_current_block ()->declared_at
);
2608 gfc_error ("The type for function '%s' at %L is not accessible",
2609 gfc_current_block ()->name
,
2610 &gfc_current_block ()->declared_at
);
2612 gfc_current_block ()->ts
.kind
= 0;
2613 /* Keep the derived type; if it's bad, it will be discovered later. */
2614 if (!(ts
->type
== BT_DERIVED
&& ts
->u
.derived
))
2615 ts
->type
= BT_UNKNOWN
;
2622 /* Parse a WHERE block, (not a simple WHERE statement). */
2625 parse_where_block (void)
2627 int seen_empty_else
;
2632 accept_statement (ST_WHERE_BLOCK
);
2633 top
= gfc_state_stack
->tail
;
2635 push_state (&s
, COMP_WHERE
, gfc_new_block
);
2637 d
= add_statement ();
2638 d
->expr1
= top
->expr1
;
2644 seen_empty_else
= 0;
2648 st
= next_statement ();
2654 case ST_WHERE_BLOCK
:
2655 parse_where_block ();
2660 accept_statement (st
);
2664 if (seen_empty_else
)
2666 gfc_error ("ELSEWHERE statement at %C follows previous "
2667 "unmasked ELSEWHERE");
2671 if (new_st
.expr1
== NULL
)
2672 seen_empty_else
= 1;
2674 d
= new_level (gfc_state_stack
->head
);
2676 d
->expr1
= new_st
.expr1
;
2678 accept_statement (st
);
2683 accept_statement (st
);
2687 gfc_error ("Unexpected %s statement in WHERE block at %C",
2688 gfc_ascii_statement (st
));
2689 reject_statement ();
2693 while (st
!= ST_END_WHERE
);
2699 /* Parse a FORALL block (not a simple FORALL statement). */
2702 parse_forall_block (void)
2708 accept_statement (ST_FORALL_BLOCK
);
2709 top
= gfc_state_stack
->tail
;
2711 push_state (&s
, COMP_FORALL
, gfc_new_block
);
2713 d
= add_statement ();
2714 d
->op
= EXEC_FORALL
;
2719 st
= next_statement ();
2724 case ST_POINTER_ASSIGNMENT
:
2727 accept_statement (st
);
2730 case ST_WHERE_BLOCK
:
2731 parse_where_block ();
2734 case ST_FORALL_BLOCK
:
2735 parse_forall_block ();
2739 accept_statement (st
);
2746 gfc_error ("Unexpected %s statement in FORALL block at %C",
2747 gfc_ascii_statement (st
));
2749 reject_statement ();
2753 while (st
!= ST_END_FORALL
);
2759 static gfc_statement
parse_executable (gfc_statement
);
2761 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2764 parse_if_block (void)
2773 accept_statement (ST_IF_BLOCK
);
2775 top
= gfc_state_stack
->tail
;
2776 push_state (&s
, COMP_IF
, gfc_new_block
);
2778 new_st
.op
= EXEC_IF
;
2779 d
= add_statement ();
2781 d
->expr1
= top
->expr1
;
2787 st
= parse_executable (ST_NONE
);
2797 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2798 "statement at %L", &else_locus
);
2800 reject_statement ();
2804 d
= new_level (gfc_state_stack
->head
);
2806 d
->expr1
= new_st
.expr1
;
2808 accept_statement (st
);
2815 gfc_error ("Duplicate ELSE statements at %L and %C",
2817 reject_statement ();
2822 else_locus
= gfc_current_locus
;
2824 d
= new_level (gfc_state_stack
->head
);
2827 accept_statement (st
);
2835 unexpected_statement (st
);
2839 while (st
!= ST_ENDIF
);
2842 accept_statement (st
);
2846 /* Parse a SELECT block. */
2849 parse_select_block (void)
2855 accept_statement (ST_SELECT_CASE
);
2857 cp
= gfc_state_stack
->tail
;
2858 push_state (&s
, COMP_SELECT
, gfc_new_block
);
2860 /* Make sure that the next statement is a CASE or END SELECT. */
2863 st
= next_statement ();
2866 if (st
== ST_END_SELECT
)
2868 /* Empty SELECT CASE is OK. */
2869 accept_statement (st
);
2876 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2879 reject_statement ();
2882 /* At this point, we're got a nonempty select block. */
2883 cp
= new_level (cp
);
2886 accept_statement (st
);
2890 st
= parse_executable (ST_NONE
);
2897 cp
= new_level (gfc_state_stack
->head
);
2899 gfc_clear_new_st ();
2901 accept_statement (st
);
2907 /* Can't have an executable statement because of
2908 parse_executable(). */
2910 unexpected_statement (st
);
2914 while (st
!= ST_END_SELECT
);
2917 accept_statement (st
);
2921 /* Pop the current selector from the SELECT TYPE stack. */
2924 select_type_pop (void)
2926 gfc_select_type_stack
*old
= select_type_stack
;
2927 select_type_stack
= old
->prev
;
2932 /* Parse a SELECT TYPE construct (F03:R821). */
2935 parse_select_type_block (void)
2941 accept_statement (ST_SELECT_TYPE
);
2943 cp
= gfc_state_stack
->tail
;
2944 push_state (&s
, COMP_SELECT_TYPE
, gfc_new_block
);
2946 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
2950 st
= next_statement ();
2953 if (st
== ST_END_SELECT
)
2954 /* Empty SELECT CASE is OK. */
2956 if (st
== ST_TYPE_IS
|| st
== ST_CLASS_IS
)
2959 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
2960 "following SELECT TYPE at %C");
2962 reject_statement ();
2965 /* At this point, we're got a nonempty select block. */
2966 cp
= new_level (cp
);
2969 accept_statement (st
);
2973 st
= parse_executable (ST_NONE
);
2981 cp
= new_level (gfc_state_stack
->head
);
2983 gfc_clear_new_st ();
2985 accept_statement (st
);
2991 /* Can't have an executable statement because of
2992 parse_executable(). */
2994 unexpected_statement (st
);
2998 while (st
!= ST_END_SELECT
);
3002 accept_statement (st
);
3003 gfc_current_ns
= gfc_current_ns
->parent
;
3008 /* Given a symbol, make sure it is not an iteration variable for a DO
3009 statement. This subroutine is called when the symbol is seen in a
3010 context that causes it to become redefined. If the symbol is an
3011 iterator, we generate an error message and return nonzero. */
3014 gfc_check_do_variable (gfc_symtree
*st
)
3018 for (s
=gfc_state_stack
; s
; s
= s
->previous
)
3019 if (s
->do_variable
== st
)
3021 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
3022 "loop beginning at %L", st
->name
, &s
->head
->loc
);
3030 /* Checks to see if the current statement label closes an enddo.
3031 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3032 an error) if it incorrectly closes an ENDDO. */
3035 check_do_closure (void)
3039 if (gfc_statement_label
== NULL
)
3042 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
3043 if (p
->state
== COMP_DO
)
3047 return 0; /* No loops to close */
3049 if (p
->ext
.end_do_label
== gfc_statement_label
)
3051 if (p
== gfc_state_stack
)
3054 gfc_error ("End of nonblock DO statement at %C is within another block");
3058 /* At this point, the label doesn't terminate the innermost loop.
3059 Make sure it doesn't terminate another one. */
3060 for (; p
; p
= p
->previous
)
3061 if (p
->state
== COMP_DO
&& p
->ext
.end_do_label
== gfc_statement_label
)
3063 gfc_error ("End of nonblock DO statement at %C is interwoven "
3064 "with another DO loop");
3072 /* Parse a series of contained program units. */
3074 static void parse_progunit (gfc_statement
);
3077 /* Parse a CRITICAL block. */
3080 parse_critical_block (void)
3086 s
.ext
.end_do_label
= new_st
.label1
;
3088 accept_statement (ST_CRITICAL
);
3089 top
= gfc_state_stack
->tail
;
3091 push_state (&s
, COMP_CRITICAL
, gfc_new_block
);
3093 d
= add_statement ();
3094 d
->op
= EXEC_CRITICAL
;
3099 st
= parse_executable (ST_NONE
);
3107 case ST_END_CRITICAL
:
3108 if (s
.ext
.end_do_label
!= NULL
3109 && s
.ext
.end_do_label
!= gfc_statement_label
)
3110 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3111 "match CRITIAL label");
3113 if (gfc_statement_label
!= NULL
)
3115 new_st
.op
= EXEC_NOP
;
3121 unexpected_statement (st
);
3125 while (st
!= ST_END_CRITICAL
);
3128 accept_statement (st
);
3132 /* Set up the local namespace for a BLOCK construct. */
3135 gfc_build_block_ns (gfc_namespace
*parent_ns
)
3137 gfc_namespace
* my_ns
;
3139 my_ns
= gfc_get_namespace (parent_ns
, 1);
3140 my_ns
->construct_entities
= 1;
3142 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3143 code generation (so it must not be NULL).
3144 We set its recursive argument if our container procedure is recursive, so
3145 that local variables are accordingly placed on the stack when it
3146 will be necessary. */
3148 my_ns
->proc_name
= gfc_new_block
;
3153 gfc_get_symbol ("block@", my_ns
, &my_ns
->proc_name
);
3154 t
= gfc_add_flavor (&my_ns
->proc_name
->attr
, FL_LABEL
,
3155 my_ns
->proc_name
->name
, NULL
);
3156 gcc_assert (t
== SUCCESS
);
3157 gfc_commit_symbol (my_ns
->proc_name
);
3160 if (parent_ns
->proc_name
)
3161 my_ns
->proc_name
->attr
.recursive
= parent_ns
->proc_name
->attr
.recursive
;
3167 /* Parse a BLOCK construct. */
3170 parse_block_construct (void)
3172 gfc_namespace
* my_ns
;
3175 gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BLOCK construct at %C");
3177 my_ns
= gfc_build_block_ns (gfc_current_ns
);
3179 new_st
.op
= EXEC_BLOCK
;
3180 new_st
.ext
.block
.ns
= my_ns
;
3181 new_st
.ext
.block
.assoc
= NULL
;
3182 accept_statement (ST_BLOCK
);
3184 push_state (&s
, COMP_BLOCK
, my_ns
->proc_name
);
3185 gfc_current_ns
= my_ns
;
3187 parse_progunit (ST_NONE
);
3189 gfc_current_ns
= gfc_current_ns
->parent
;
3194 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3195 behind the scenes with compiler-generated variables. */
3198 parse_associate (void)
3200 gfc_namespace
* my_ns
;
3203 gfc_association_list
* a
;
3205 gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ASSOCIATE construct at %C");
3207 my_ns
= gfc_build_block_ns (gfc_current_ns
);
3209 new_st
.op
= EXEC_BLOCK
;
3210 new_st
.ext
.block
.ns
= my_ns
;
3211 gcc_assert (new_st
.ext
.block
.assoc
);
3213 /* Add all associate-names as BLOCK variables. Creating them is enough
3214 for now, they'll get their values during trans-* phase. */
3215 gfc_current_ns
= my_ns
;
3216 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
3220 if (gfc_get_sym_tree (a
->name
, NULL
, &a
->st
, false))
3224 sym
->attr
.flavor
= FL_VARIABLE
;
3226 sym
->declared_at
= a
->where
;
3227 gfc_set_sym_referenced (sym
);
3229 /* Initialize the typespec. It is not available in all cases,
3230 however, as it may only be set on the target during resolution.
3231 Still, sometimes it helps to have it right now -- especially
3232 for parsing component references on the associate-name
3233 in case of assication to a derived-type. */
3234 sym
->ts
= a
->target
->ts
;
3237 accept_statement (ST_ASSOCIATE
);
3238 push_state (&s
, COMP_ASSOCIATE
, my_ns
->proc_name
);
3241 st
= parse_executable (ST_NONE
);
3248 accept_statement (st
);
3249 my_ns
->code
= gfc_state_stack
->head
;
3253 unexpected_statement (st
);
3257 gfc_current_ns
= gfc_current_ns
->parent
;
3262 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
3263 handled inside of parse_executable(), because they aren't really
3267 parse_do_block (void)
3274 s
.ext
.end_do_label
= new_st
.label1
;
3276 if (new_st
.ext
.iterator
!= NULL
)
3277 stree
= new_st
.ext
.iterator
->var
->symtree
;
3281 accept_statement (ST_DO
);
3283 top
= gfc_state_stack
->tail
;
3284 push_state (&s
, COMP_DO
, gfc_new_block
);
3286 s
.do_variable
= stree
;
3288 top
->block
= new_level (top
);
3289 top
->block
->op
= EXEC_DO
;
3292 st
= parse_executable (ST_NONE
);
3300 if (s
.ext
.end_do_label
!= NULL
3301 && s
.ext
.end_do_label
!= gfc_statement_label
)
3302 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
3305 if (gfc_statement_label
!= NULL
)
3307 new_st
.op
= EXEC_NOP
;
3312 case ST_IMPLIED_ENDDO
:
3313 /* If the do-stmt of this DO construct has a do-construct-name,
3314 the corresponding end-do must be an end-do-stmt (with a matching
3315 name, but in that case we must have seen ST_ENDDO first).
3316 We only complain about this in pedantic mode. */
3317 if (gfc_current_block () != NULL
)
3318 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
3319 &gfc_current_block()->declared_at
);
3324 unexpected_statement (st
);
3329 accept_statement (st
);
3333 /* Parse the statements of OpenMP do/parallel do. */
3335 static gfc_statement
3336 parse_omp_do (gfc_statement omp_st
)
3342 accept_statement (omp_st
);
3344 cp
= gfc_state_stack
->tail
;
3345 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3346 np
= new_level (cp
);
3352 st
= next_statement ();
3355 else if (st
== ST_DO
)
3358 unexpected_statement (st
);
3362 if (gfc_statement_label
!= NULL
3363 && gfc_state_stack
->previous
!= NULL
3364 && gfc_state_stack
->previous
->state
== COMP_DO
3365 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
3373 there should be no !$OMP END DO. */
3375 return ST_IMPLIED_ENDDO
;
3378 check_do_closure ();
3381 st
= next_statement ();
3382 if (st
== (omp_st
== ST_OMP_DO
? ST_OMP_END_DO
: ST_OMP_END_PARALLEL_DO
))
3384 if (new_st
.op
== EXEC_OMP_END_NOWAIT
)
3385 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
3387 gcc_assert (new_st
.op
== EXEC_NOP
);
3388 gfc_clear_new_st ();
3389 gfc_commit_symbols ();
3390 gfc_warning_check ();
3391 st
= next_statement ();
3397 /* Parse the statements of OpenMP atomic directive. */
3400 parse_omp_atomic (void)
3406 accept_statement (ST_OMP_ATOMIC
);
3408 cp
= gfc_state_stack
->tail
;
3409 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3410 np
= new_level (cp
);
3416 st
= next_statement ();
3419 else if (st
== ST_ASSIGNMENT
)
3422 unexpected_statement (st
);
3425 accept_statement (st
);
3431 /* Parse the statements of an OpenMP structured block. */
3434 parse_omp_structured_block (gfc_statement omp_st
, bool workshare_stmts_only
)
3436 gfc_statement st
, omp_end_st
;
3440 accept_statement (omp_st
);
3442 cp
= gfc_state_stack
->tail
;
3443 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3444 np
= new_level (cp
);
3450 case ST_OMP_PARALLEL
:
3451 omp_end_st
= ST_OMP_END_PARALLEL
;
3453 case ST_OMP_PARALLEL_SECTIONS
:
3454 omp_end_st
= ST_OMP_END_PARALLEL_SECTIONS
;
3456 case ST_OMP_SECTIONS
:
3457 omp_end_st
= ST_OMP_END_SECTIONS
;
3459 case ST_OMP_ORDERED
:
3460 omp_end_st
= ST_OMP_END_ORDERED
;
3462 case ST_OMP_CRITICAL
:
3463 omp_end_st
= ST_OMP_END_CRITICAL
;
3466 omp_end_st
= ST_OMP_END_MASTER
;
3469 omp_end_st
= ST_OMP_END_SINGLE
;
3472 omp_end_st
= ST_OMP_END_TASK
;
3474 case ST_OMP_WORKSHARE
:
3475 omp_end_st
= ST_OMP_END_WORKSHARE
;
3477 case ST_OMP_PARALLEL_WORKSHARE
:
3478 omp_end_st
= ST_OMP_END_PARALLEL_WORKSHARE
;
3486 if (workshare_stmts_only
)
3488 /* Inside of !$omp workshare, only
3491 where statements and constructs
3492 forall statements and constructs
3496 are allowed. For !$omp critical these
3497 restrictions apply recursively. */
3500 st
= next_statement ();
3511 accept_statement (st
);
3514 case ST_WHERE_BLOCK
:
3515 parse_where_block ();
3518 case ST_FORALL_BLOCK
:
3519 parse_forall_block ();
3522 case ST_OMP_PARALLEL
:
3523 case ST_OMP_PARALLEL_SECTIONS
:
3524 parse_omp_structured_block (st
, false);
3527 case ST_OMP_PARALLEL_WORKSHARE
:
3528 case ST_OMP_CRITICAL
:
3529 parse_omp_structured_block (st
, true);
3532 case ST_OMP_PARALLEL_DO
:
3533 st
= parse_omp_do (st
);
3537 parse_omp_atomic ();
3548 st
= next_statement ();
3552 st
= parse_executable (ST_NONE
);
3555 else if (st
== ST_OMP_SECTION
3556 && (omp_st
== ST_OMP_SECTIONS
3557 || omp_st
== ST_OMP_PARALLEL_SECTIONS
))
3559 np
= new_level (np
);
3563 else if (st
!= omp_end_st
)
3564 unexpected_statement (st
);
3566 while (st
!= omp_end_st
);
3570 case EXEC_OMP_END_NOWAIT
:
3571 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
3573 case EXEC_OMP_CRITICAL
:
3574 if (((cp
->ext
.omp_name
== NULL
) ^ (new_st
.ext
.omp_name
== NULL
))
3575 || (new_st
.ext
.omp_name
!= NULL
3576 && strcmp (cp
->ext
.omp_name
, new_st
.ext
.omp_name
) != 0))
3577 gfc_error ("Name after !$omp critical and !$omp end critical does "
3579 gfc_free (CONST_CAST (char *, new_st
.ext
.omp_name
));
3581 case EXEC_OMP_END_SINGLE
:
3582 cp
->ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
]
3583 = new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
];
3584 new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
] = NULL
;
3585 gfc_free_omp_clauses (new_st
.ext
.omp_clauses
);
3593 gfc_clear_new_st ();
3594 gfc_commit_symbols ();
3595 gfc_warning_check ();
3600 /* Accept a series of executable statements. We return the first
3601 statement that doesn't fit to the caller. Any block statements are
3602 passed on to the correct handler, which usually passes the buck
3605 static gfc_statement
3606 parse_executable (gfc_statement st
)
3611 st
= next_statement ();
3615 close_flag
= check_do_closure ();
3620 case ST_END_PROGRAM
:
3623 case ST_END_FUNCTION
:
3628 case ST_END_SUBROUTINE
:
3633 case ST_SELECT_CASE
:
3634 gfc_error ("%s statement at %C cannot terminate a non-block "
3635 "DO loop", gfc_ascii_statement (st
));
3651 accept_statement (st
);
3652 if (close_flag
== 1)
3653 return ST_IMPLIED_ENDDO
;
3657 parse_block_construct ();
3668 case ST_SELECT_CASE
:
3669 parse_select_block ();
3672 case ST_SELECT_TYPE
:
3673 parse_select_type_block();
3678 if (check_do_closure () == 1)
3679 return ST_IMPLIED_ENDDO
;
3683 parse_critical_block ();
3686 case ST_WHERE_BLOCK
:
3687 parse_where_block ();
3690 case ST_FORALL_BLOCK
:
3691 parse_forall_block ();
3694 case ST_OMP_PARALLEL
:
3695 case ST_OMP_PARALLEL_SECTIONS
:
3696 case ST_OMP_SECTIONS
:
3697 case ST_OMP_ORDERED
:
3698 case ST_OMP_CRITICAL
:
3702 parse_omp_structured_block (st
, false);
3705 case ST_OMP_WORKSHARE
:
3706 case ST_OMP_PARALLEL_WORKSHARE
:
3707 parse_omp_structured_block (st
, true);
3711 case ST_OMP_PARALLEL_DO
:
3712 st
= parse_omp_do (st
);
3713 if (st
== ST_IMPLIED_ENDDO
)
3718 parse_omp_atomic ();
3725 st
= next_statement ();
3730 /* Fix the symbols for sibling functions. These are incorrectly added to
3731 the child namespace as the parser didn't know about this procedure. */
3734 gfc_fixup_sibling_symbols (gfc_symbol
*sym
, gfc_namespace
*siblings
)
3738 gfc_symbol
*old_sym
;
3740 sym
->attr
.referenced
= 1;
3741 for (ns
= siblings
; ns
; ns
= ns
->sibling
)
3743 st
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
3745 if (!st
|| (st
->n
.sym
->attr
.dummy
&& ns
== st
->n
.sym
->ns
))
3746 goto fixup_contained
;
3748 old_sym
= st
->n
.sym
;
3749 if (old_sym
->ns
== ns
3750 && !old_sym
->attr
.contained
3752 /* By 14.6.1.3, host association should be excluded
3753 for the following. */
3754 && !(old_sym
->attr
.external
3755 || (old_sym
->ts
.type
!= BT_UNKNOWN
3756 && !old_sym
->attr
.implicit_type
)
3757 || old_sym
->attr
.flavor
== FL_PARAMETER
3758 || old_sym
->attr
.use_assoc
3759 || old_sym
->attr
.in_common
3760 || old_sym
->attr
.in_equivalence
3761 || old_sym
->attr
.data
3762 || old_sym
->attr
.dummy
3763 || old_sym
->attr
.result
3764 || old_sym
->attr
.dimension
3765 || old_sym
->attr
.allocatable
3766 || old_sym
->attr
.intrinsic
3767 || old_sym
->attr
.generic
3768 || old_sym
->attr
.flavor
== FL_NAMELIST
3769 || old_sym
->attr
.proc
== PROC_ST_FUNCTION
))
3771 /* Replace it with the symbol from the parent namespace. */
3775 gfc_release_symbol (old_sym
);
3779 /* Do the same for any contained procedures. */
3780 gfc_fixup_sibling_symbols (sym
, ns
->contained
);
3785 parse_contained (int module
)
3787 gfc_namespace
*ns
, *parent_ns
, *tmp
;
3788 gfc_state_data s1
, s2
;
3792 int contains_statements
= 0;
3795 push_state (&s1
, COMP_CONTAINS
, NULL
);
3796 parent_ns
= gfc_current_ns
;
3800 gfc_current_ns
= gfc_get_namespace (parent_ns
, 1);
3802 gfc_current_ns
->sibling
= parent_ns
->contained
;
3803 parent_ns
->contained
= gfc_current_ns
;
3806 /* Process the next available statement. We come here if we got an error
3807 and rejected the last statement. */
3808 st
= next_statement ();
3817 contains_statements
= 1;
3818 accept_statement (st
);
3821 (st
== ST_FUNCTION
) ? COMP_FUNCTION
: COMP_SUBROUTINE
,
3824 /* For internal procedures, create/update the symbol in the
3825 parent namespace. */
3829 if (gfc_get_symbol (gfc_new_block
->name
, parent_ns
, &sym
))
3830 gfc_error ("Contained procedure '%s' at %C is already "
3831 "ambiguous", gfc_new_block
->name
);
3834 if (gfc_add_procedure (&sym
->attr
, PROC_INTERNAL
, sym
->name
,
3835 &gfc_new_block
->declared_at
) ==
3838 if (st
== ST_FUNCTION
)
3839 gfc_add_function (&sym
->attr
, sym
->name
,
3840 &gfc_new_block
->declared_at
);
3842 gfc_add_subroutine (&sym
->attr
, sym
->name
,
3843 &gfc_new_block
->declared_at
);
3847 gfc_commit_symbols ();
3850 sym
= gfc_new_block
;
3852 /* Mark this as a contained function, so it isn't replaced
3853 by other module functions. */
3854 sym
->attr
.contained
= 1;
3855 sym
->attr
.referenced
= 1;
3857 /* Set implicit_pure so that it can be reset if any of the
3858 tests for purity fail. This is used for some optimisation
3859 during translation. */
3860 if (!sym
->attr
.pure
)
3861 sym
->attr
.implicit_pure
= 1;
3863 parse_progunit (ST_NONE
);
3865 /* Fix up any sibling functions that refer to this one. */
3866 gfc_fixup_sibling_symbols (sym
, gfc_current_ns
);
3867 /* Or refer to any of its alternate entry points. */
3868 for (el
= gfc_current_ns
->entries
; el
; el
= el
->next
)
3869 gfc_fixup_sibling_symbols (el
->sym
, gfc_current_ns
);
3871 gfc_current_ns
->code
= s2
.head
;
3872 gfc_current_ns
= parent_ns
;
3877 /* These statements are associated with the end of the host unit. */
3878 case ST_END_FUNCTION
:
3880 case ST_END_PROGRAM
:
3881 case ST_END_SUBROUTINE
:
3882 accept_statement (st
);
3886 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
3887 gfc_ascii_statement (st
));
3888 reject_statement ();
3894 while (st
!= ST_END_FUNCTION
&& st
!= ST_END_SUBROUTINE
3895 && st
!= ST_END_MODULE
&& st
!= ST_END_PROGRAM
);
3897 /* The first namespace in the list is guaranteed to not have
3898 anything (worthwhile) in it. */
3899 tmp
= gfc_current_ns
;
3900 gfc_current_ns
= parent_ns
;
3901 if (seen_error
&& tmp
->refs
> 1)
3902 gfc_free_namespace (tmp
);
3904 ns
= gfc_current_ns
->contained
;
3905 gfc_current_ns
->contained
= ns
->sibling
;
3906 gfc_free_namespace (ns
);
3909 if (!contains_statements
)
3910 gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: CONTAINS statement without "
3911 "FUNCTION or SUBROUTINE statement at %C");
3915 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
3918 parse_progunit (gfc_statement st
)
3923 st
= parse_spec (st
);
3930 /* This is not allowed within BLOCK! */
3931 if (gfc_current_state () != COMP_BLOCK
)
3936 accept_statement (st
);
3943 if (gfc_current_state () == COMP_FUNCTION
)
3944 gfc_check_function_type (gfc_current_ns
);
3949 st
= parse_executable (st
);
3957 /* This is not allowed within BLOCK! */
3958 if (gfc_current_state () != COMP_BLOCK
)
3963 accept_statement (st
);
3970 unexpected_statement (st
);
3971 reject_statement ();
3972 st
= next_statement ();
3978 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
3979 if (p
->state
== COMP_CONTAINS
)
3982 if (gfc_find_state (COMP_MODULE
) == SUCCESS
)
3987 gfc_error ("CONTAINS statement at %C is already in a contained "
3989 reject_statement ();
3990 st
= next_statement ();
3994 parse_contained (0);
3997 gfc_current_ns
->code
= gfc_state_stack
->head
;
4001 /* Come here to complain about a global symbol already in use as
4005 gfc_global_used (gfc_gsymbol
*sym
, locus
*where
)
4010 where
= &gfc_current_locus
;
4020 case GSYM_SUBROUTINE
:
4021 name
= "SUBROUTINE";
4026 case GSYM_BLOCK_DATA
:
4027 name
= "BLOCK DATA";
4033 gfc_internal_error ("gfc_global_used(): Bad type");
4037 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
4038 sym
->name
, where
, name
, &sym
->where
);
4042 /* Parse a block data program unit. */
4045 parse_block_data (void)
4048 static locus blank_locus
;
4049 static int blank_block
=0;
4052 gfc_current_ns
->proc_name
= gfc_new_block
;
4053 gfc_current_ns
->is_block_data
= 1;
4055 if (gfc_new_block
== NULL
)
4058 gfc_error ("Blank BLOCK DATA at %C conflicts with "
4059 "prior BLOCK DATA at %L", &blank_locus
);
4063 blank_locus
= gfc_current_locus
;
4068 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4070 || (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_BLOCK_DATA
))
4071 gfc_global_used(s
, NULL
);
4074 s
->type
= GSYM_BLOCK_DATA
;
4075 s
->where
= gfc_current_locus
;
4080 st
= parse_spec (ST_NONE
);
4082 while (st
!= ST_END_BLOCK_DATA
)
4084 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
4085 gfc_ascii_statement (st
));
4086 reject_statement ();
4087 st
= next_statement ();
4092 /* Parse a module subprogram. */
4100 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4101 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_MODULE
))
4102 gfc_global_used(s
, NULL
);
4105 s
->type
= GSYM_MODULE
;
4106 s
->where
= gfc_current_locus
;
4110 st
= parse_spec (ST_NONE
);
4119 parse_contained (1);
4123 accept_statement (st
);
4127 gfc_error ("Unexpected %s statement in MODULE at %C",
4128 gfc_ascii_statement (st
));
4130 reject_statement ();
4131 st
= next_statement ();
4135 s
->ns
= gfc_current_ns
;
4139 /* Add a procedure name to the global symbol table. */
4142 add_global_procedure (int sub
)
4146 s
= gfc_get_gsymbol(gfc_new_block
->name
);
4149 || (s
->type
!= GSYM_UNKNOWN
4150 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
4151 gfc_global_used(s
, NULL
);
4154 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
4155 s
->where
= gfc_current_locus
;
4157 s
->ns
= gfc_current_ns
;
4162 /* Add a program to the global symbol table. */
4165 add_global_program (void)
4169 if (gfc_new_block
== NULL
)
4171 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4173 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_PROGRAM
))
4174 gfc_global_used(s
, NULL
);
4177 s
->type
= GSYM_PROGRAM
;
4178 s
->where
= gfc_current_locus
;
4180 s
->ns
= gfc_current_ns
;
4185 /* Resolve all the program units when whole file scope option
4188 resolve_all_program_units (gfc_namespace
*gfc_global_ns_list
)
4190 gfc_free_dt_list ();
4191 gfc_current_ns
= gfc_global_ns_list
;
4192 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4194 if (gfc_current_ns
->proc_name
)
4195 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4196 gfc_resolve (gfc_current_ns
);
4197 gfc_current_ns
->derived_types
= gfc_derived_types
;
4198 gfc_derived_types
= NULL
;
4204 clean_up_modules (gfc_gsymbol
*gsym
)
4209 clean_up_modules (gsym
->left
);
4210 clean_up_modules (gsym
->right
);
4212 if (gsym
->type
!= GSYM_MODULE
|| !gsym
->ns
)
4215 gfc_current_ns
= gsym
->ns
;
4216 gfc_derived_types
= gfc_current_ns
->derived_types
;
4223 /* Translate all the program units when whole file scope option
4224 is active. This could be in a different order to resolution if
4225 there are forward references in the file. */
4227 translate_all_program_units (gfc_namespace
*gfc_global_ns_list
)
4231 gfc_current_ns
= gfc_global_ns_list
;
4232 gfc_get_errors (NULL
, &errors
);
4234 for (; !errors
&& gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4236 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4237 gfc_derived_types
= gfc_current_ns
->derived_types
;
4238 gfc_generate_code (gfc_current_ns
);
4239 gfc_current_ns
->translated
= 1;
4242 /* Clean up all the namespaces after translation. */
4243 gfc_current_ns
= gfc_global_ns_list
;
4244 for (;gfc_current_ns
;)
4246 gfc_namespace
*ns
= gfc_current_ns
->sibling
;
4247 gfc_derived_types
= gfc_current_ns
->derived_types
;
4249 gfc_current_ns
= ns
;
4252 clean_up_modules (gfc_gsym_root
);
4256 /* Top level parser. */
4259 gfc_parse_file (void)
4261 int seen_program
, errors_before
, errors
;
4262 gfc_state_data top
, s
;
4265 gfc_namespace
*next
;
4267 gfc_start_source_files ();
4269 top
.state
= COMP_NONE
;
4271 top
.previous
= NULL
;
4272 top
.head
= top
.tail
= NULL
;
4273 top
.do_variable
= NULL
;
4275 gfc_state_stack
= &top
;
4277 gfc_clear_new_st ();
4279 gfc_statement_label
= NULL
;
4281 if (setjmp (eof_buf
))
4282 return FAILURE
; /* Come here on unexpected EOF */
4284 /* Prepare the global namespace that will contain the
4286 gfc_global_ns_list
= next
= NULL
;
4290 /* Exit early for empty files. */
4296 st
= next_statement ();
4305 goto duplicate_main
;
4307 prog_locus
= gfc_current_locus
;
4309 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
4310 main_program_symbol(gfc_current_ns
, gfc_new_block
->name
);
4311 accept_statement (st
);
4312 add_global_program ();
4313 parse_progunit (ST_NONE
);
4314 if (gfc_option
.flag_whole_file
)
4319 add_global_procedure (1);
4320 push_state (&s
, COMP_SUBROUTINE
, gfc_new_block
);
4321 accept_statement (st
);
4322 parse_progunit (ST_NONE
);
4323 if (gfc_option
.flag_whole_file
)
4328 add_global_procedure (0);
4329 push_state (&s
, COMP_FUNCTION
, gfc_new_block
);
4330 accept_statement (st
);
4331 parse_progunit (ST_NONE
);
4332 if (gfc_option
.flag_whole_file
)
4337 push_state (&s
, COMP_BLOCK_DATA
, gfc_new_block
);
4338 accept_statement (st
);
4339 parse_block_data ();
4343 push_state (&s
, COMP_MODULE
, gfc_new_block
);
4344 accept_statement (st
);
4346 gfc_get_errors (NULL
, &errors_before
);
4350 /* Anything else starts a nameless main program block. */
4353 goto duplicate_main
;
4355 prog_locus
= gfc_current_locus
;
4357 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
4358 main_program_symbol (gfc_current_ns
, "MAIN__");
4359 parse_progunit (st
);
4360 if (gfc_option
.flag_whole_file
)
4365 /* Handle the non-program units. */
4366 gfc_current_ns
->code
= s
.head
;
4368 gfc_resolve (gfc_current_ns
);
4370 /* Dump the parse tree if requested. */
4371 if (gfc_option
.dump_fortran_original
)
4372 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
4374 gfc_get_errors (NULL
, &errors
);
4375 if (s
.state
== COMP_MODULE
)
4377 gfc_dump_module (s
.sym
->name
, errors_before
== errors
);
4379 gfc_generate_module_code (gfc_current_ns
);
4381 if (!gfc_option
.flag_whole_file
)
4385 gfc_current_ns
->derived_types
= gfc_derived_types
;
4386 gfc_derived_types
= NULL
;
4387 gfc_current_ns
= NULL
;
4393 gfc_generate_code (gfc_current_ns
);
4401 /* The main program and non-contained procedures are put
4402 in the global namespace list, so that they can be processed
4403 later and all their interfaces resolved. */
4404 gfc_current_ns
->code
= s
.head
;
4407 for (; next
->sibling
; next
= next
->sibling
)
4409 next
->sibling
= gfc_current_ns
;
4412 gfc_global_ns_list
= gfc_current_ns
;
4414 next
= gfc_current_ns
;
4421 if (!gfc_option
.flag_whole_file
)
4424 /* Do the resolution. */
4425 resolve_all_program_units (gfc_global_ns_list
);
4427 /* Do the parse tree dump. */
4429 = gfc_option
.dump_fortran_original
? gfc_global_ns_list
: NULL
;
4431 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4433 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
4434 fputs ("------------------------------------------\n\n", stdout
);
4437 /* Do the translation. */
4438 translate_all_program_units (gfc_global_ns_list
);
4442 gfc_end_source_files ();
4446 /* If we see a duplicate main program, shut down. If the second
4447 instance is an implied main program, i.e. data decls or executable
4448 statements, we're in for lots of errors. */
4449 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus
);
4450 reject_statement ();