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/>. */
26 #include "coretypes.h"
32 /* Current statement label. Zero means no statement label. Because new_st
33 can get wiped during statement matching, we have to keep it separate. */
35 gfc_st_label
*gfc_statement_label
;
37 static locus label_locus
;
38 static jmp_buf eof_buf
;
40 gfc_state_data
*gfc_state_stack
;
41 static bool last_was_use_stmt
= false;
43 /* TODO: Re-order functions to kill these forward decls. */
44 static void check_statement_label (gfc_statement
);
45 static void undo_new_statement (void);
46 static void reject_statement (void);
49 /* A sort of half-matching function. We try to match the word on the
50 input with the passed string. If this succeeds, we call the
51 keyword-dependent matching function that will match the rest of the
52 statement. For single keywords, the matching subroutine is
56 match_word (const char *str
, match (*subr
) (void), locus
*old_locus
)
71 gfc_current_locus
= *old_locus
;
79 /* Load symbols from all USE statements encountered in this scoping unit. */
84 gfc_error_buf old_error
;
86 gfc_push_error (&old_error
);
90 gfc_pop_error (&old_error
);
91 gfc_commit_symbols ();
93 gfc_current_ns
->old_cl_list
= gfc_current_ns
->cl_list
;
94 gfc_current_ns
->old_equiv
= gfc_current_ns
->equiv
;
95 last_was_use_stmt
= false;
99 /* Figure out what the next statement is, (mostly) regardless of
100 proper ordering. The do...while(0) is there to prevent if/else
103 #define match(keyword, subr, st) \
105 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
108 undo_new_statement (); \
112 /* This is a specialist version of decode_statement that is used
113 for the specification statements in a function, whose
114 characteristics are deferred into the specification statements.
115 eg.: INTEGER (king = mykind) foo ()
116 USE mymodule, ONLY mykind.....
117 The KIND parameter needs a return after USE or IMPORT, whereas
118 derived type declarations can occur anywhere, up the executable
119 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
120 out of the correct kind of specification statements. */
122 decode_specification_statement (void)
128 if (gfc_match_eos () == MATCH_YES
)
131 old_locus
= gfc_current_locus
;
133 if (match_word ("use", gfc_match_use
, &old_locus
) == MATCH_YES
)
135 last_was_use_stmt
= true;
140 undo_new_statement ();
141 if (last_was_use_stmt
)
145 match ("import", gfc_match_import
, ST_IMPORT
);
147 if (gfc_current_block ()->result
->ts
.type
!= BT_DERIVED
)
150 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
151 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
152 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
154 /* General statement matching: Instead of testing every possible
155 statement, we eliminate most possibilities by peeking at the
158 c
= gfc_peek_ascii_char ();
163 match ("abstract% interface", gfc_match_abstract_interface
,
165 match ("allocatable", gfc_match_allocatable
, ST_ATTR_DECL
);
166 match ("asynchronous", gfc_match_asynchronous
, ST_ATTR_DECL
);
170 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
174 match ("codimension", gfc_match_codimension
, ST_ATTR_DECL
);
175 match ("contiguous", gfc_match_contiguous
, ST_ATTR_DECL
);
179 match ("data", gfc_match_data
, ST_DATA
);
180 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
184 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
185 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
186 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
187 match ("external", gfc_match_external
, ST_ATTR_DECL
);
191 match ("format", gfc_match_format
, ST_FORMAT
);
198 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
199 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
200 match ("interface", gfc_match_interface
, ST_INTERFACE
);
201 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
202 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
209 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
213 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
217 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
218 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
219 if (gfc_match_private (&st
) == MATCH_YES
)
221 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
222 if (gfc_match_public (&st
) == MATCH_YES
)
224 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
231 match ("save", gfc_match_save
, ST_ATTR_DECL
);
235 match ("target", gfc_match_target
, ST_ATTR_DECL
);
236 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
243 match ("value", gfc_match_value
, ST_ATTR_DECL
);
244 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
251 /* This is not a specification statement. See if any of the matchers
252 has stored an error message of some sort. */
256 gfc_buffer_error (0);
257 gfc_current_locus
= old_locus
;
259 return ST_GET_FCN_CHARACTERISTICS
;
263 /* This is the primary 'decode_statement'. */
265 decode_statement (void)
272 gfc_enforce_clean_symbol_state ();
274 gfc_clear_error (); /* Clear any pending errors. */
275 gfc_clear_warning (); /* Clear any pending warnings. */
277 gfc_matching_function
= false;
279 if (gfc_match_eos () == MATCH_YES
)
282 if (gfc_current_state () == COMP_FUNCTION
283 && gfc_current_block ()->result
->ts
.kind
== -1)
284 return decode_specification_statement ();
286 old_locus
= gfc_current_locus
;
288 c
= gfc_peek_ascii_char ();
292 if (match_word ("use", gfc_match_use
, &old_locus
) == MATCH_YES
)
294 last_was_use_stmt
= true;
298 undo_new_statement ();
301 if (last_was_use_stmt
)
304 /* Try matching a data declaration or function declaration. The
305 input "REALFUNCTIONA(N)" can mean several things in different
306 contexts, so it (and its relatives) get special treatment. */
308 if (gfc_current_state () == COMP_NONE
309 || gfc_current_state () == COMP_INTERFACE
310 || gfc_current_state () == COMP_CONTAINS
)
312 gfc_matching_function
= true;
313 m
= gfc_match_function_decl ();
316 else if (m
== MATCH_ERROR
)
320 gfc_current_locus
= old_locus
;
322 gfc_matching_function
= false;
325 /* Match statements whose error messages are meant to be overwritten
326 by something better. */
328 match (NULL
, gfc_match_assignment
, ST_ASSIGNMENT
);
329 match (NULL
, gfc_match_pointer_assignment
, ST_POINTER_ASSIGNMENT
);
330 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
332 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
333 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
335 /* Try to match a subroutine statement, which has the same optional
336 prefixes that functions can have. */
338 if (gfc_match_subroutine () == MATCH_YES
)
339 return ST_SUBROUTINE
;
341 gfc_current_locus
= old_locus
;
343 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
344 statements, which might begin with a block label. The match functions for
345 these statements are unusual in that their keyword is not seen before
346 the matcher is called. */
348 if (gfc_match_if (&st
) == MATCH_YES
)
351 gfc_current_locus
= old_locus
;
353 if (gfc_match_where (&st
) == MATCH_YES
)
356 gfc_current_locus
= old_locus
;
358 if (gfc_match_forall (&st
) == MATCH_YES
)
361 gfc_current_locus
= old_locus
;
363 match (NULL
, gfc_match_do
, ST_DO
);
364 match (NULL
, gfc_match_block
, ST_BLOCK
);
365 match (NULL
, gfc_match_associate
, ST_ASSOCIATE
);
366 match (NULL
, gfc_match_critical
, ST_CRITICAL
);
367 match (NULL
, gfc_match_select
, ST_SELECT_CASE
);
368 match (NULL
, gfc_match_select_type
, ST_SELECT_TYPE
);
370 /* General statement matching: Instead of testing every possible
371 statement, we eliminate most possibilities by peeking at the
377 match ("abstract% interface", gfc_match_abstract_interface
,
379 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
);
380 match ("allocatable", gfc_match_allocatable
, ST_ATTR_DECL
);
381 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
);
382 match ("asynchronous", gfc_match_asynchronous
, ST_ATTR_DECL
);
386 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
);
387 match ("block data", gfc_match_block_data
, ST_BLOCK_DATA
);
388 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
392 match ("call", gfc_match_call
, ST_CALL
);
393 match ("close", gfc_match_close
, ST_CLOSE
);
394 match ("continue", gfc_match_continue
, ST_CONTINUE
);
395 match ("contiguous", gfc_match_contiguous
, ST_ATTR_DECL
);
396 match ("cycle", gfc_match_cycle
, ST_CYCLE
);
397 match ("case", gfc_match_case
, ST_CASE
);
398 match ("common", gfc_match_common
, ST_COMMON
);
399 match ("contains", gfc_match_eos
, ST_CONTAINS
);
400 match ("class", gfc_match_class_is
, ST_CLASS_IS
);
401 match ("codimension", gfc_match_codimension
, ST_ATTR_DECL
);
405 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
);
406 match ("data", gfc_match_data
, ST_DATA
);
407 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
411 match ("end file", gfc_match_endfile
, ST_END_FILE
);
412 match ("exit", gfc_match_exit
, ST_EXIT
);
413 match ("else", gfc_match_else
, ST_ELSE
);
414 match ("else where", gfc_match_elsewhere
, ST_ELSEWHERE
);
415 match ("else if", gfc_match_elseif
, ST_ELSEIF
);
416 match ("error stop", gfc_match_error_stop
, ST_ERROR_STOP
);
417 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
419 if (gfc_match_end (&st
) == MATCH_YES
)
422 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
423 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
424 match ("external", gfc_match_external
, ST_ATTR_DECL
);
428 match ("final", gfc_match_final_decl
, ST_FINAL
);
429 match ("flush", gfc_match_flush
, ST_FLUSH
);
430 match ("format", gfc_match_format
, ST_FORMAT
);
434 match ("generic", gfc_match_generic
, ST_GENERIC
);
435 match ("go to", gfc_match_goto
, ST_GOTO
);
439 match ("inquire", gfc_match_inquire
, ST_INQUIRE
);
440 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
441 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
442 match ("import", gfc_match_import
, ST_IMPORT
);
443 match ("interface", gfc_match_interface
, ST_INTERFACE
);
444 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
445 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
449 match ("lock", gfc_match_lock
, ST_LOCK
);
453 match ("module% procedure", gfc_match_modproc
, ST_MODULE_PROC
);
454 match ("module", gfc_match_module
, ST_MODULE
);
458 match ("nullify", gfc_match_nullify
, ST_NULLIFY
);
459 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
463 match ("open", gfc_match_open
, ST_OPEN
);
464 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
468 match ("print", gfc_match_print
, ST_WRITE
);
469 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
470 match ("pause", gfc_match_pause
, ST_PAUSE
);
471 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
472 if (gfc_match_private (&st
) == MATCH_YES
)
474 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
475 match ("program", gfc_match_program
, ST_PROGRAM
);
476 if (gfc_match_public (&st
) == MATCH_YES
)
478 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
482 match ("read", gfc_match_read
, ST_READ
);
483 match ("return", gfc_match_return
, ST_RETURN
);
484 match ("rewind", gfc_match_rewind
, ST_REWIND
);
488 match ("sequence", gfc_match_eos
, ST_SEQUENCE
);
489 match ("stop", gfc_match_stop
, ST_STOP
);
490 match ("save", gfc_match_save
, ST_ATTR_DECL
);
491 match ("sync all", gfc_match_sync_all
, ST_SYNC_ALL
);
492 match ("sync images", gfc_match_sync_images
, ST_SYNC_IMAGES
);
493 match ("sync memory", gfc_match_sync_memory
, ST_SYNC_MEMORY
);
497 match ("target", gfc_match_target
, ST_ATTR_DECL
);
498 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
499 match ("type is", gfc_match_type_is
, ST_TYPE_IS
);
503 match ("unlock", gfc_match_unlock
, ST_UNLOCK
);
507 match ("value", gfc_match_value
, ST_ATTR_DECL
);
508 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
512 match ("wait", gfc_match_wait
, ST_WAIT
);
513 match ("write", gfc_match_write
, ST_WRITE
);
517 /* All else has failed, so give up. See if any of the matchers has
518 stored an error message of some sort. */
520 if (gfc_error_check () == 0)
521 gfc_error_now ("Unclassifiable statement at %C");
525 gfc_error_recovery ();
531 decode_omp_directive (void)
536 gfc_enforce_clean_symbol_state ();
538 gfc_clear_error (); /* Clear any pending errors. */
539 gfc_clear_warning (); /* Clear any pending warnings. */
543 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
544 "or ELEMENTAL procedures");
545 gfc_error_recovery ();
549 if (gfc_implicit_pure (NULL
))
550 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
552 old_locus
= gfc_current_locus
;
554 /* General OpenMP directive matching: Instead of testing every possible
555 statement, we eliminate most possibilities by peeking at the
558 c
= gfc_peek_ascii_char ();
563 match ("atomic", gfc_match_omp_atomic
, ST_OMP_ATOMIC
);
566 match ("barrier", gfc_match_omp_barrier
, ST_OMP_BARRIER
);
569 match ("critical", gfc_match_omp_critical
, ST_OMP_CRITICAL
);
572 match ("do", gfc_match_omp_do
, ST_OMP_DO
);
575 match ("end atomic", gfc_match_omp_eos
, ST_OMP_END_ATOMIC
);
576 match ("end critical", gfc_match_omp_critical
, ST_OMP_END_CRITICAL
);
577 match ("end do", gfc_match_omp_end_nowait
, ST_OMP_END_DO
);
578 match ("end master", gfc_match_omp_eos
, ST_OMP_END_MASTER
);
579 match ("end ordered", gfc_match_omp_eos
, ST_OMP_END_ORDERED
);
580 match ("end parallel do", gfc_match_omp_eos
, ST_OMP_END_PARALLEL_DO
);
581 match ("end parallel sections", gfc_match_omp_eos
,
582 ST_OMP_END_PARALLEL_SECTIONS
);
583 match ("end parallel workshare", gfc_match_omp_eos
,
584 ST_OMP_END_PARALLEL_WORKSHARE
);
585 match ("end parallel", gfc_match_omp_eos
, ST_OMP_END_PARALLEL
);
586 match ("end sections", gfc_match_omp_end_nowait
, ST_OMP_END_SECTIONS
);
587 match ("end single", gfc_match_omp_end_single
, ST_OMP_END_SINGLE
);
588 match ("end task", gfc_match_omp_eos
, ST_OMP_END_TASK
);
589 match ("end workshare", gfc_match_omp_end_nowait
,
590 ST_OMP_END_WORKSHARE
);
593 match ("flush", gfc_match_omp_flush
, ST_OMP_FLUSH
);
596 match ("master", gfc_match_omp_master
, ST_OMP_MASTER
);
599 match ("ordered", gfc_match_omp_ordered
, ST_OMP_ORDERED
);
602 match ("parallel do", gfc_match_omp_parallel_do
, ST_OMP_PARALLEL_DO
);
603 match ("parallel sections", gfc_match_omp_parallel_sections
,
604 ST_OMP_PARALLEL_SECTIONS
);
605 match ("parallel workshare", gfc_match_omp_parallel_workshare
,
606 ST_OMP_PARALLEL_WORKSHARE
);
607 match ("parallel", gfc_match_omp_parallel
, ST_OMP_PARALLEL
);
610 match ("sections", gfc_match_omp_sections
, ST_OMP_SECTIONS
);
611 match ("section", gfc_match_omp_eos
, ST_OMP_SECTION
);
612 match ("single", gfc_match_omp_single
, ST_OMP_SINGLE
);
615 match ("task", gfc_match_omp_task
, ST_OMP_TASK
);
616 match ("taskwait", gfc_match_omp_taskwait
, ST_OMP_TASKWAIT
);
617 match ("taskyield", gfc_match_omp_taskyield
, ST_OMP_TASKYIELD
);
618 match ("threadprivate", gfc_match_omp_threadprivate
,
619 ST_OMP_THREADPRIVATE
);
621 match ("workshare", gfc_match_omp_workshare
, ST_OMP_WORKSHARE
);
625 /* All else has failed, so give up. See if any of the matchers has
626 stored an error message of some sort. */
628 if (gfc_error_check () == 0)
629 gfc_error_now ("Unclassifiable OpenMP directive at %C");
633 gfc_error_recovery ();
639 decode_gcc_attribute (void)
643 gfc_enforce_clean_symbol_state ();
645 gfc_clear_error (); /* Clear any pending errors. */
646 gfc_clear_warning (); /* Clear any pending warnings. */
647 old_locus
= gfc_current_locus
;
649 match ("attributes", gfc_match_gcc_attributes
, ST_ATTR_DECL
);
651 /* All else has failed, so give up. See if any of the matchers has
652 stored an error message of some sort. */
654 if (gfc_error_check () == 0)
655 gfc_error_now ("Unclassifiable GCC directive at %C");
659 gfc_error_recovery ();
667 /* Get the next statement in free form source. */
676 at_bol
= gfc_at_bol ();
677 gfc_gobble_whitespace ();
679 c
= gfc_peek_ascii_char ();
685 /* Found a statement label? */
686 m
= gfc_match_st_label (&gfc_statement_label
);
688 d
= gfc_peek_ascii_char ();
689 if (m
!= MATCH_YES
|| !gfc_is_whitespace (d
))
691 gfc_match_small_literal_int (&i
, &cnt
);
694 gfc_error_now ("Too many digits in statement label at %C");
697 gfc_error_now ("Zero is not a valid statement label at %C");
700 c
= gfc_next_ascii_char ();
703 if (!gfc_is_whitespace (c
))
704 gfc_error_now ("Non-numeric character in statement label at %C");
710 label_locus
= gfc_current_locus
;
712 gfc_gobble_whitespace ();
714 if (at_bol
&& gfc_peek_ascii_char () == ';')
716 gfc_error_now ("Semicolon at %C needs to be preceded by "
718 gfc_next_ascii_char (); /* Eat up the semicolon. */
722 if (gfc_match_eos () == MATCH_YES
)
724 gfc_warning_now ("Ignoring statement label in empty statement "
725 "at %L", &label_locus
);
726 gfc_free_st_label (gfc_statement_label
);
727 gfc_statement_label
= NULL
;
734 /* Comments have already been skipped by the time we get here,
735 except for GCC attributes and OpenMP directives. */
737 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
738 c
= gfc_peek_ascii_char ();
744 c
= gfc_next_ascii_char ();
745 for (i
= 0; i
< 4; i
++, c
= gfc_next_ascii_char ())
746 gcc_assert (c
== "gcc$"[i
]);
748 gfc_gobble_whitespace ();
749 return decode_gcc_attribute ();
752 else if (c
== '$' && gfc_option
.gfc_flag_openmp
)
756 c
= gfc_next_ascii_char ();
757 for (i
= 0; i
< 4; i
++, c
= gfc_next_ascii_char ())
758 gcc_assert (c
== "$omp"[i
]);
760 gcc_assert (c
== ' ' || c
== '\t');
761 gfc_gobble_whitespace ();
762 if (last_was_use_stmt
)
764 return decode_omp_directive ();
770 if (at_bol
&& c
== ';')
772 if (!(gfc_option
.allow_std
& GFC_STD_F2008
))
773 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
775 gfc_next_ascii_char (); /* Eat up the semicolon. */
779 return decode_statement ();
783 /* Get the next statement in fixed-form source. */
788 int label
, digit_flag
, i
;
793 return decode_statement ();
795 /* Skip past the current label field, parsing a statement label if
796 one is there. This is a weird number parser, since the number is
797 contained within five columns and can have any kind of embedded
798 spaces. We also check for characters that make the rest of the
804 for (i
= 0; i
< 5; i
++)
806 c
= gfc_next_char_literal (NONSTRING
);
823 label
= label
* 10 + ((unsigned char) c
- '0');
824 label_locus
= gfc_current_locus
;
828 /* Comments have already been skipped by the time we get
829 here, except for GCC attributes and OpenMP directives. */
832 c
= gfc_next_char_literal (NONSTRING
);
834 if (TOLOWER (c
) == 'g')
836 for (i
= 0; i
< 4; i
++, c
= gfc_next_char_literal (NONSTRING
))
837 gcc_assert (TOLOWER (c
) == "gcc$"[i
]);
839 return decode_gcc_attribute ();
841 else if (c
== '$' && gfc_option
.gfc_flag_openmp
)
843 for (i
= 0; i
< 4; i
++, c
= gfc_next_char_literal (NONSTRING
))
844 gcc_assert ((char) gfc_wide_tolower (c
) == "$omp"[i
]);
846 if (c
!= ' ' && c
!= '0')
848 gfc_buffer_error (0);
849 gfc_error ("Bad continuation line at %C");
852 if (last_was_use_stmt
)
854 return decode_omp_directive ();
858 /* Comments have already been skipped by the time we get
859 here so don't bother checking for them. */
862 gfc_buffer_error (0);
863 gfc_error ("Non-numeric character in statement label at %C");
871 gfc_warning_now ("Zero is not a valid statement label at %C");
874 /* We've found a valid statement label. */
875 gfc_statement_label
= gfc_get_st_label (label
);
879 /* Since this line starts a statement, it cannot be a continuation
880 of a previous statement. If we see something here besides a
881 space or zero, it must be a bad continuation line. */
883 c
= gfc_next_char_literal (NONSTRING
);
887 if (c
!= ' ' && c
!= '0')
889 gfc_buffer_error (0);
890 gfc_error ("Bad continuation line at %C");
894 /* Now that we've taken care of the statement label columns, we have
895 to make sure that the first nonblank character is not a '!'. If
896 it is, the rest of the line is a comment. */
900 loc
= gfc_current_locus
;
901 c
= gfc_next_char_literal (NONSTRING
);
903 while (gfc_is_whitespace (c
));
907 gfc_current_locus
= loc
;
912 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
913 else if (!(gfc_option
.allow_std
& GFC_STD_F2008
))
914 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
919 if (gfc_match_eos () == MATCH_YES
)
922 /* At this point, we've got a nonblank statement to parse. */
923 return decode_statement ();
927 gfc_warning_now ("Ignoring statement label in empty statement at %L",
930 gfc_current_locus
.lb
->truncated
= 0;
936 /* Return the next non-ST_NONE statement to the caller. We also worry
937 about including files and the ends of include files at this stage. */
940 next_statement (void)
945 gfc_enforce_clean_symbol_state ();
947 gfc_new_block
= NULL
;
949 gfc_current_ns
->old_cl_list
= gfc_current_ns
->cl_list
;
950 gfc_current_ns
->old_equiv
= gfc_current_ns
->equiv
;
953 gfc_statement_label
= NULL
;
954 gfc_buffer_error (1);
959 gfc_skip_comments ();
967 if (gfc_define_undef_line ())
970 old_locus
= gfc_current_locus
;
972 st
= (gfc_current_form
== FORM_FIXED
) ? next_fixed () : next_free ();
978 gfc_buffer_error (0);
980 if (st
== ST_GET_FCN_CHARACTERISTICS
&& gfc_statement_label
!= NULL
)
982 gfc_free_st_label (gfc_statement_label
);
983 gfc_statement_label
= NULL
;
984 gfc_current_locus
= old_locus
;
988 check_statement_label (st
);
994 /****************************** Parser ***********************************/
996 /* The parser subroutines are of type 'try' that fail if the file ends
999 /* Macros that expand to case-labels for various classes of
1000 statements. Start with executable statements that directly do
1003 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1004 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1005 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1006 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1007 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1008 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1009 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1010 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1011 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1012 case ST_ERROR_STOP: case ST_SYNC_ALL: case ST_SYNC_IMAGES: \
1013 case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK
1015 /* Statements that mark other executable statements. */
1017 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1018 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1019 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1020 case ST_OMP_PARALLEL: \
1021 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1022 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1023 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1024 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1025 case ST_OMP_TASK: case ST_CRITICAL
1027 /* Declaration statements */
1029 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1030 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1031 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
1034 /* Block end statements. Errors associated with interchanging these
1035 are detected in gfc_match_end(). */
1037 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1038 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1039 case ST_END_BLOCK: case ST_END_ASSOCIATE
1042 /* Push a new state onto the stack. */
1045 push_state (gfc_state_data
*p
, gfc_compile_state new_state
, gfc_symbol
*sym
)
1047 p
->state
= new_state
;
1048 p
->previous
= gfc_state_stack
;
1050 p
->head
= p
->tail
= NULL
;
1051 p
->do_variable
= NULL
;
1053 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1054 construct statement was accepted right before pushing the state. Thus,
1055 the construct's gfc_code is available as tail of the parent state. */
1056 gcc_assert (gfc_state_stack
);
1057 p
->construct
= gfc_state_stack
->tail
;
1059 gfc_state_stack
= p
;
1063 /* Pop the current state. */
1067 gfc_state_stack
= gfc_state_stack
->previous
;
1071 /* Try to find the given state in the state stack. */
1074 gfc_find_state (gfc_compile_state state
)
1078 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1079 if (p
->state
== state
)
1082 return (p
== NULL
) ? FAILURE
: SUCCESS
;
1086 /* Starts a new level in the statement list. */
1089 new_level (gfc_code
*q
)
1093 p
= q
->block
= gfc_get_code ();
1095 gfc_state_stack
->head
= gfc_state_stack
->tail
= p
;
1101 /* Add the current new_st code structure and adds it to the current
1102 program unit. As a side-effect, it zeroes the new_st. */
1105 add_statement (void)
1109 p
= gfc_get_code ();
1112 p
->loc
= gfc_current_locus
;
1114 if (gfc_state_stack
->head
== NULL
)
1115 gfc_state_stack
->head
= p
;
1117 gfc_state_stack
->tail
->next
= p
;
1119 while (p
->next
!= NULL
)
1122 gfc_state_stack
->tail
= p
;
1124 gfc_clear_new_st ();
1130 /* Frees everything associated with the current statement. */
1133 undo_new_statement (void)
1135 gfc_free_statements (new_st
.block
);
1136 gfc_free_statements (new_st
.next
);
1137 gfc_free_statement (&new_st
);
1138 gfc_clear_new_st ();
1142 /* If the current statement has a statement label, make sure that it
1143 is allowed to, or should have one. */
1146 check_statement_label (gfc_statement st
)
1150 if (gfc_statement_label
== NULL
)
1152 if (st
== ST_FORMAT
)
1153 gfc_error ("FORMAT statement at %L does not have a statement label",
1160 case ST_END_PROGRAM
:
1161 case ST_END_FUNCTION
:
1162 case ST_END_SUBROUTINE
:
1166 case ST_END_CRITICAL
:
1168 case ST_END_ASSOCIATE
:
1171 if (st
== ST_ENDDO
|| st
== ST_CONTINUE
)
1172 type
= ST_LABEL_DO_TARGET
;
1174 type
= ST_LABEL_TARGET
;
1178 type
= ST_LABEL_FORMAT
;
1181 /* Statement labels are not restricted from appearing on a
1182 particular line. However, there are plenty of situations
1183 where the resulting label can't be referenced. */
1186 type
= ST_LABEL_BAD_TARGET
;
1190 gfc_define_st_label (gfc_statement_label
, type
, &label_locus
);
1192 new_st
.here
= gfc_statement_label
;
1196 /* Figures out what the enclosing program unit is. This will be a
1197 function, subroutine, program, block data or module. */
1200 gfc_enclosing_unit (gfc_compile_state
* result
)
1204 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1205 if (p
->state
== COMP_FUNCTION
|| p
->state
== COMP_SUBROUTINE
1206 || p
->state
== COMP_MODULE
|| p
->state
== COMP_BLOCK_DATA
1207 || p
->state
== COMP_PROGRAM
)
1216 *result
= COMP_PROGRAM
;
1221 /* Translate a statement enum to a string. */
1224 gfc_ascii_statement (gfc_statement st
)
1230 case ST_ARITHMETIC_IF
:
1231 p
= _("arithmetic IF");
1240 p
= _("attribute declaration");
1276 p
= _("data declaration");
1284 case ST_DERIVED_DECL
:
1285 p
= _("derived type declaration");
1299 case ST_END_ASSOCIATE
:
1300 p
= "END ASSOCIATE";
1305 case ST_END_BLOCK_DATA
:
1306 p
= "END BLOCK DATA";
1308 case ST_END_CRITICAL
:
1320 case ST_END_FUNCTION
:
1326 case ST_END_INTERFACE
:
1327 p
= "END INTERFACE";
1332 case ST_END_PROGRAM
:
1338 case ST_END_SUBROUTINE
:
1339 p
= "END SUBROUTINE";
1350 case ST_EQUIVALENCE
:
1362 case ST_FORALL_BLOCK
: /* Fall through */
1384 case ST_IMPLICIT_NONE
:
1385 p
= "IMPLICIT NONE";
1387 case ST_IMPLIED_ENDDO
:
1388 p
= _("implied END DO");
1417 case ST_MODULE_PROC
:
1418 p
= "MODULE PROCEDURE";
1450 case ST_SYNC_IMAGES
:
1453 case ST_SYNC_MEMORY
:
1468 case ST_WHERE_BLOCK
: /* Fall through */
1479 p
= _("assignment");
1481 case ST_POINTER_ASSIGNMENT
:
1482 p
= _("pointer assignment");
1484 case ST_SELECT_CASE
:
1487 case ST_SELECT_TYPE
:
1502 case ST_STATEMENT_FUNCTION
:
1503 p
= "STATEMENT FUNCTION";
1505 case ST_LABEL_ASSIGNMENT
:
1506 p
= "LABEL ASSIGNMENT";
1509 p
= "ENUM DEFINITION";
1512 p
= "ENUMERATOR DEFINITION";
1520 case ST_OMP_BARRIER
:
1521 p
= "!$OMP BARRIER";
1523 case ST_OMP_CRITICAL
:
1524 p
= "!$OMP CRITICAL";
1529 case ST_OMP_END_ATOMIC
:
1530 p
= "!$OMP END ATOMIC";
1532 case ST_OMP_END_CRITICAL
:
1533 p
= "!$OMP END CRITICAL";
1538 case ST_OMP_END_MASTER
:
1539 p
= "!$OMP END MASTER";
1541 case ST_OMP_END_ORDERED
:
1542 p
= "!$OMP END ORDERED";
1544 case ST_OMP_END_PARALLEL
:
1545 p
= "!$OMP END PARALLEL";
1547 case ST_OMP_END_PARALLEL_DO
:
1548 p
= "!$OMP END PARALLEL DO";
1550 case ST_OMP_END_PARALLEL_SECTIONS
:
1551 p
= "!$OMP END PARALLEL SECTIONS";
1553 case ST_OMP_END_PARALLEL_WORKSHARE
:
1554 p
= "!$OMP END PARALLEL WORKSHARE";
1556 case ST_OMP_END_SECTIONS
:
1557 p
= "!$OMP END SECTIONS";
1559 case ST_OMP_END_SINGLE
:
1560 p
= "!$OMP END SINGLE";
1562 case ST_OMP_END_TASK
:
1563 p
= "!$OMP END TASK";
1565 case ST_OMP_END_WORKSHARE
:
1566 p
= "!$OMP END WORKSHARE";
1574 case ST_OMP_ORDERED
:
1575 p
= "!$OMP ORDERED";
1577 case ST_OMP_PARALLEL
:
1578 p
= "!$OMP PARALLEL";
1580 case ST_OMP_PARALLEL_DO
:
1581 p
= "!$OMP PARALLEL DO";
1583 case ST_OMP_PARALLEL_SECTIONS
:
1584 p
= "!$OMP PARALLEL SECTIONS";
1586 case ST_OMP_PARALLEL_WORKSHARE
:
1587 p
= "!$OMP PARALLEL WORKSHARE";
1589 case ST_OMP_SECTIONS
:
1590 p
= "!$OMP SECTIONS";
1592 case ST_OMP_SECTION
:
1593 p
= "!$OMP SECTION";
1601 case ST_OMP_TASKWAIT
:
1602 p
= "!$OMP TASKWAIT";
1604 case ST_OMP_TASKYIELD
:
1605 p
= "!$OMP TASKYIELD";
1607 case ST_OMP_THREADPRIVATE
:
1608 p
= "!$OMP THREADPRIVATE";
1610 case ST_OMP_WORKSHARE
:
1611 p
= "!$OMP WORKSHARE";
1614 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1621 /* Create a symbol for the main program and assign it to ns->proc_name. */
1624 main_program_symbol (gfc_namespace
*ns
, const char *name
)
1626 gfc_symbol
*main_program
;
1627 symbol_attribute attr
;
1629 gfc_get_symbol (name
, ns
, &main_program
);
1630 gfc_clear_attr (&attr
);
1631 attr
.flavor
= FL_PROGRAM
;
1632 attr
.proc
= PROC_UNKNOWN
;
1633 attr
.subroutine
= 1;
1634 attr
.access
= ACCESS_PUBLIC
;
1635 attr
.is_main_program
= 1;
1636 main_program
->attr
= attr
;
1637 main_program
->declared_at
= gfc_current_locus
;
1638 ns
->proc_name
= main_program
;
1639 gfc_commit_symbols ();
1643 /* Do whatever is necessary to accept the last statement. */
1646 accept_statement (gfc_statement st
)
1650 case ST_IMPLICIT_NONE
:
1651 gfc_set_implicit_none ();
1660 gfc_current_ns
->proc_name
= gfc_new_block
;
1663 /* If the statement is the end of a block, lay down a special code
1664 that allows a branch to the end of the block from within the
1665 construct. IF and SELECT are treated differently from DO
1666 (where EXEC_NOP is added inside the loop) for two
1668 1. END DO has a meaning in the sense that after a GOTO to
1669 it, the loop counter must be increased.
1670 2. IF blocks and SELECT blocks can consist of multiple
1671 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
1672 Putting the label before the END IF would make the jump
1673 from, say, the ELSE IF block to the END IF illegal. */
1677 case ST_END_CRITICAL
:
1678 if (gfc_statement_label
!= NULL
)
1680 new_st
.op
= EXEC_END_NESTED_BLOCK
;
1685 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
1686 one parallel block. Thus, we add the special code to the nested block
1687 itself, instead of the parent one. */
1689 case ST_END_ASSOCIATE
:
1690 if (gfc_statement_label
!= NULL
)
1692 new_st
.op
= EXEC_END_BLOCK
;
1697 /* The end-of-program unit statements do not get the special
1698 marker and require a statement of some sort if they are a
1701 case ST_END_PROGRAM
:
1702 case ST_END_FUNCTION
:
1703 case ST_END_SUBROUTINE
:
1704 if (gfc_statement_label
!= NULL
)
1706 new_st
.op
= EXEC_RETURN
;
1711 new_st
.op
= EXEC_END_PROCEDURE
;
1727 gfc_commit_symbols ();
1728 gfc_warning_check ();
1729 gfc_clear_new_st ();
1733 /* Undo anything tentative that has been built for the current
1737 reject_statement (void)
1739 /* Revert to the previous charlen chain. */
1740 gfc_free_charlen (gfc_current_ns
->cl_list
, gfc_current_ns
->old_cl_list
);
1741 gfc_current_ns
->cl_list
= gfc_current_ns
->old_cl_list
;
1743 gfc_free_equiv_until (gfc_current_ns
->equiv
, gfc_current_ns
->old_equiv
);
1744 gfc_current_ns
->equiv
= gfc_current_ns
->old_equiv
;
1746 gfc_new_block
= NULL
;
1747 gfc_undo_symbols ();
1748 gfc_clear_warning ();
1749 undo_new_statement ();
1753 /* Generic complaint about an out of order statement. We also do
1754 whatever is necessary to clean up. */
1757 unexpected_statement (gfc_statement st
)
1759 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st
));
1761 reject_statement ();
1765 /* Given the next statement seen by the matcher, make sure that it is
1766 in proper order with the last. This subroutine is initialized by
1767 calling it with an argument of ST_NONE. If there is a problem, we
1768 issue an error and return FAILURE. Otherwise we return SUCCESS.
1770 Individual parsers need to verify that the statements seen are
1771 valid before calling here, i.e., ENTRY statements are not allowed in
1772 INTERFACE blocks. The following diagram is taken from the standard:
1774 +---------------------------------------+
1775 | program subroutine function module |
1776 +---------------------------------------+
1778 +---------------------------------------+
1780 +---------------------------------------+
1782 | +-----------+------------------+
1783 | | parameter | implicit |
1784 | +-----------+------------------+
1785 | format | | derived type |
1786 | entry | parameter | interface |
1787 | | data | specification |
1788 | | | statement func |
1789 | +-----------+------------------+
1790 | | data | executable |
1791 +--------+-----------+------------------+
1793 +---------------------------------------+
1794 | internal module/subprogram |
1795 +---------------------------------------+
1797 +---------------------------------------+
1806 ORDER_IMPLICIT_NONE
,
1814 enum state_order state
;
1815 gfc_statement last_statement
;
1821 verify_st_order (st_state
*p
, gfc_statement st
, bool silent
)
1827 p
->state
= ORDER_START
;
1831 if (p
->state
> ORDER_USE
)
1833 p
->state
= ORDER_USE
;
1837 if (p
->state
> ORDER_IMPORT
)
1839 p
->state
= ORDER_IMPORT
;
1842 case ST_IMPLICIT_NONE
:
1843 if (p
->state
> ORDER_IMPLICIT_NONE
)
1846 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1847 statement disqualifies a USE but not an IMPLICIT NONE.
1848 Duplicate IMPLICIT NONEs are caught when the implicit types
1851 p
->state
= ORDER_IMPLICIT_NONE
;
1855 if (p
->state
> ORDER_IMPLICIT
)
1857 p
->state
= ORDER_IMPLICIT
;
1862 if (p
->state
< ORDER_IMPLICIT_NONE
)
1863 p
->state
= ORDER_IMPLICIT_NONE
;
1867 if (p
->state
>= ORDER_EXEC
)
1869 if (p
->state
< ORDER_IMPLICIT
)
1870 p
->state
= ORDER_IMPLICIT
;
1874 if (p
->state
< ORDER_SPEC
)
1875 p
->state
= ORDER_SPEC
;
1880 case ST_DERIVED_DECL
:
1882 if (p
->state
>= ORDER_EXEC
)
1884 if (p
->state
< ORDER_SPEC
)
1885 p
->state
= ORDER_SPEC
;
1890 if (p
->state
< ORDER_EXEC
)
1891 p
->state
= ORDER_EXEC
;
1895 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1896 gfc_ascii_statement (st
));
1899 /* All is well, record the statement in case we need it next time. */
1900 p
->where
= gfc_current_locus
;
1901 p
->last_statement
= st
;
1906 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1907 gfc_ascii_statement (st
),
1908 gfc_ascii_statement (p
->last_statement
), &p
->where
);
1914 /* Handle an unexpected end of file. This is a show-stopper... */
1916 static void unexpected_eof (void) ATTRIBUTE_NORETURN
;
1919 unexpected_eof (void)
1923 gfc_error ("Unexpected end of file in '%s'", gfc_source_file
);
1925 /* Memory cleanup. Move to "second to last". */
1926 for (p
= gfc_state_stack
; p
&& p
->previous
&& p
->previous
->previous
;
1929 gfc_current_ns
->code
= (p
&& p
->previous
) ? p
->head
: NULL
;
1932 longjmp (eof_buf
, 1);
1936 /* Parse the CONTAINS section of a derived type definition. */
1938 gfc_access gfc_typebound_default_access
;
1941 parse_derived_contains (void)
1944 bool seen_private
= false;
1945 bool seen_comps
= false;
1946 bool error_flag
= false;
1949 gcc_assert (gfc_current_state () == COMP_DERIVED
);
1950 gcc_assert (gfc_current_block ());
1952 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
1954 if (gfc_current_block ()->attr
.sequence
)
1955 gfc_error ("Derived-type '%s' with SEQUENCE must not have a CONTAINS"
1956 " section at %C", gfc_current_block ()->name
);
1957 if (gfc_current_block ()->attr
.is_bind_c
)
1958 gfc_error ("Derived-type '%s' with BIND(C) must not have a CONTAINS"
1959 " section at %C", gfc_current_block ()->name
);
1961 accept_statement (ST_CONTAINS
);
1962 push_state (&s
, COMP_DERIVED_CONTAINS
, NULL
);
1964 gfc_typebound_default_access
= ACCESS_PUBLIC
;
1970 st
= next_statement ();
1978 gfc_error ("Components in TYPE at %C must precede CONTAINS");
1982 if (gfc_notify_std (GFC_STD_F2003
, "Type-bound"
1983 " procedure at %C") == FAILURE
)
1986 accept_statement (ST_PROCEDURE
);
1991 if (gfc_notify_std (GFC_STD_F2003
, "GENERIC binding"
1992 " at %C") == FAILURE
)
1995 accept_statement (ST_GENERIC
);
2000 if (gfc_notify_std (GFC_STD_F2003
,
2001 "FINAL procedure declaration"
2002 " at %C") == FAILURE
)
2005 accept_statement (ST_FINAL
);
2013 && (gfc_notify_std (GFC_STD_F2008
, "Derived type "
2014 "definition at %C with empty CONTAINS "
2015 "section") == FAILURE
))
2018 /* ST_END_TYPE is accepted by parse_derived after return. */
2022 if (gfc_find_state (COMP_MODULE
) == FAILURE
)
2024 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2031 gfc_error ("PRIVATE statement at %C must precede procedure"
2038 gfc_error ("Duplicate PRIVATE statement at %C");
2042 accept_statement (ST_PRIVATE
);
2043 gfc_typebound_default_access
= ACCESS_PRIVATE
;
2044 seen_private
= true;
2048 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2052 gfc_error ("Already inside a CONTAINS block at %C");
2056 unexpected_statement (st
);
2064 reject_statement ();
2068 gcc_assert (gfc_current_state () == COMP_DERIVED
);
2074 /* Parse a derived type. */
2077 parse_derived (void)
2079 int compiling_type
, seen_private
, seen_sequence
, seen_component
;
2083 gfc_component
*c
, *lock_comp
= NULL
;
2085 accept_statement (ST_DERIVED_DECL
);
2086 push_state (&s
, COMP_DERIVED
, gfc_new_block
);
2088 gfc_new_block
->component_access
= ACCESS_PUBLIC
;
2095 while (compiling_type
)
2097 st
= next_statement ();
2105 accept_statement (st
);
2110 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2117 if (!seen_component
)
2118 gfc_notify_std (GFC_STD_F2003
, "Derived type "
2119 "definition at %C without components");
2121 accept_statement (ST_END_TYPE
);
2125 if (gfc_find_state (COMP_MODULE
) == FAILURE
)
2127 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2134 gfc_error ("PRIVATE statement at %C must precede "
2135 "structure components");
2140 gfc_error ("Duplicate PRIVATE statement at %C");
2142 s
.sym
->component_access
= ACCESS_PRIVATE
;
2144 accept_statement (ST_PRIVATE
);
2151 gfc_error ("SEQUENCE statement at %C must precede "
2152 "structure components");
2156 if (gfc_current_block ()->attr
.sequence
)
2157 gfc_warning ("SEQUENCE attribute at %C already specified in "
2162 gfc_error ("Duplicate SEQUENCE statement at %C");
2166 gfc_add_sequence (&gfc_current_block ()->attr
,
2167 gfc_current_block ()->name
, NULL
);
2171 gfc_notify_std (GFC_STD_F2003
,
2172 "CONTAINS block in derived type"
2173 " definition at %C");
2175 accept_statement (ST_CONTAINS
);
2176 parse_derived_contains ();
2180 unexpected_statement (st
);
2185 /* need to verify that all fields of the derived type are
2186 * interoperable with C if the type is declared to be bind(c)
2188 sym
= gfc_current_block ();
2189 for (c
= sym
->components
; c
; c
= c
->next
)
2191 bool coarray
, lock_type
, allocatable
, pointer
;
2192 coarray
= lock_type
= allocatable
= pointer
= false;
2194 /* Look for allocatable components. */
2195 if (c
->attr
.allocatable
2196 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2197 && CLASS_DATA (c
)->attr
.allocatable
)
2198 || (c
->ts
.type
== BT_DERIVED
&& !c
->attr
.pointer
2199 && c
->ts
.u
.derived
->attr
.alloc_comp
))
2202 sym
->attr
.alloc_comp
= 1;
2205 /* Look for pointer components. */
2207 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2208 && CLASS_DATA (c
)->attr
.class_pointer
)
2209 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.pointer_comp
))
2212 sym
->attr
.pointer_comp
= 1;
2215 /* Look for procedure pointer components. */
2216 if (c
->attr
.proc_pointer
2217 || (c
->ts
.type
== BT_DERIVED
2218 && c
->ts
.u
.derived
->attr
.proc_pointer_comp
))
2219 sym
->attr
.proc_pointer_comp
= 1;
2221 /* Looking for coarray components. */
2222 if (c
->attr
.codimension
2223 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2224 && CLASS_DATA (c
)->attr
.codimension
))
2227 sym
->attr
.coarray_comp
= 1;
2230 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.coarray_comp
)
2233 if (!pointer
&& !allocatable
)
2234 sym
->attr
.coarray_comp
= 1;
2237 /* Looking for lock_type components. */
2238 if ((c
->ts
.type
== BT_DERIVED
2239 && c
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
2240 && c
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
)
2241 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2242 && CLASS_DATA (c
)->ts
.u
.derived
->from_intmod
2243 == INTMOD_ISO_FORTRAN_ENV
2244 && CLASS_DATA (c
)->ts
.u
.derived
->intmod_sym_id
2245 == ISOFORTRAN_LOCK_TYPE
)
2246 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.lock_comp
2247 && !allocatable
&& !pointer
))
2251 sym
->attr
.lock_comp
= 1;
2254 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2255 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2256 unless there are nondirect [allocatable or pointer] components
2257 involved (cf. 1.3.33.1 and 1.3.33.3). */
2259 if (pointer
&& !coarray
&& lock_type
)
2260 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2261 "codimension or be a subcomponent of a coarray, "
2262 "which is not possible as the component has the "
2263 "pointer attribute", c
->name
, &c
->loc
);
2264 else if (pointer
&& !coarray
&& c
->ts
.type
== BT_DERIVED
2265 && c
->ts
.u
.derived
->attr
.lock_comp
)
2266 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2267 "of type LOCK_TYPE, which must have a codimension or be a "
2268 "subcomponent of a coarray", c
->name
, &c
->loc
);
2270 if (lock_type
&& allocatable
&& !coarray
)
2271 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2272 "a codimension", c
->name
, &c
->loc
);
2273 else if (lock_type
&& allocatable
&& c
->ts
.type
== BT_DERIVED
2274 && c
->ts
.u
.derived
->attr
.lock_comp
)
2275 gfc_error ("Allocatable component %s at %L must have a codimension as "
2276 "it has a noncoarray subcomponent of type LOCK_TYPE",
2279 if (sym
->attr
.coarray_comp
&& !coarray
&& lock_type
)
2280 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2281 "subcomponent of type LOCK_TYPE must have a codimension or "
2282 "be a subcomponent of a coarray. (Variables of type %s may "
2283 "not have a codimension as already a coarray "
2284 "subcomponent exists)", c
->name
, &c
->loc
, sym
->name
);
2286 if (sym
->attr
.lock_comp
&& coarray
&& !lock_type
)
2287 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2288 "subcomponent of type LOCK_TYPE must have a codimension or "
2289 "be a subcomponent of a coarray. (Variables of type %s may "
2290 "not have a codimension as %s at %L has a codimension or a "
2291 "coarray subcomponent)", lock_comp
->name
, &lock_comp
->loc
,
2292 sym
->name
, c
->name
, &c
->loc
);
2294 /* Look for private components. */
2295 if (sym
->component_access
== ACCESS_PRIVATE
2296 || c
->attr
.access
== ACCESS_PRIVATE
2297 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.private_comp
))
2298 sym
->attr
.private_comp
= 1;
2301 if (!seen_component
)
2302 sym
->attr
.zero_comp
= 1;
2308 /* Parse an ENUM. */
2316 int seen_enumerator
= 0;
2318 push_state (&s
, COMP_ENUM
, gfc_new_block
);
2322 while (compiling_enum
)
2324 st
= next_statement ();
2332 seen_enumerator
= 1;
2333 accept_statement (st
);
2338 if (!seen_enumerator
)
2339 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2340 accept_statement (st
);
2344 gfc_free_enum_history ();
2345 unexpected_statement (st
);
2353 /* Parse an interface. We must be able to deal with the possibility
2354 of recursive interfaces. The parse_spec() subroutine is mutually
2355 recursive with parse_interface(). */
2357 static gfc_statement
parse_spec (gfc_statement
);
2360 parse_interface (void)
2362 gfc_compile_state new_state
= COMP_NONE
, current_state
;
2363 gfc_symbol
*prog_unit
, *sym
;
2364 gfc_interface_info save
;
2365 gfc_state_data s1
, s2
;
2368 accept_statement (ST_INTERFACE
);
2370 current_interface
.ns
= gfc_current_ns
;
2371 save
= current_interface
;
2373 sym
= (current_interface
.type
== INTERFACE_GENERIC
2374 || current_interface
.type
== INTERFACE_USER_OP
)
2375 ? gfc_new_block
: NULL
;
2377 push_state (&s1
, COMP_INTERFACE
, sym
);
2378 current_state
= COMP_NONE
;
2381 gfc_current_ns
= gfc_get_namespace (current_interface
.ns
, 0);
2383 st
= next_statement ();
2391 if (st
== ST_SUBROUTINE
)
2392 new_state
= COMP_SUBROUTINE
;
2393 else if (st
== ST_FUNCTION
)
2394 new_state
= COMP_FUNCTION
;
2395 if (gfc_new_block
->attr
.pointer
)
2397 gfc_new_block
->attr
.pointer
= 0;
2398 gfc_new_block
->attr
.proc_pointer
= 1;
2400 if (gfc_add_explicit_interface (gfc_new_block
, IFSRC_IFBODY
,
2401 gfc_new_block
->formal
, NULL
) == FAILURE
)
2403 reject_statement ();
2404 gfc_free_namespace (gfc_current_ns
);
2410 case ST_MODULE_PROC
: /* The module procedure matcher makes
2411 sure the context is correct. */
2412 accept_statement (st
);
2413 gfc_free_namespace (gfc_current_ns
);
2416 case ST_END_INTERFACE
:
2417 gfc_free_namespace (gfc_current_ns
);
2418 gfc_current_ns
= current_interface
.ns
;
2422 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2423 gfc_ascii_statement (st
));
2424 reject_statement ();
2425 gfc_free_namespace (gfc_current_ns
);
2430 /* Make sure that the generic name has the right attribute. */
2431 if (current_interface
.type
== INTERFACE_GENERIC
2432 && current_state
== COMP_NONE
)
2434 if (new_state
== COMP_FUNCTION
&& sym
)
2435 gfc_add_function (&sym
->attr
, sym
->name
, NULL
);
2436 else if (new_state
== COMP_SUBROUTINE
&& sym
)
2437 gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
);
2439 current_state
= new_state
;
2442 if (current_interface
.type
== INTERFACE_ABSTRACT
)
2444 gfc_add_abstract (&gfc_new_block
->attr
, &gfc_current_locus
);
2445 if (gfc_is_intrinsic_typename (gfc_new_block
->name
))
2446 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
2447 "cannot be the same as an intrinsic type",
2448 gfc_new_block
->name
);
2451 push_state (&s2
, new_state
, gfc_new_block
);
2452 accept_statement (st
);
2453 prog_unit
= gfc_new_block
;
2454 prog_unit
->formal_ns
= gfc_current_ns
;
2455 if (prog_unit
== prog_unit
->formal_ns
->proc_name
2456 && prog_unit
->ns
!= prog_unit
->formal_ns
)
2460 /* Read data declaration statements. */
2461 st
= parse_spec (ST_NONE
);
2463 /* Since the interface block does not permit an IMPLICIT statement,
2464 the default type for the function or the result must be taken
2465 from the formal namespace. */
2466 if (new_state
== COMP_FUNCTION
)
2468 if (prog_unit
->result
== prog_unit
2469 && prog_unit
->ts
.type
== BT_UNKNOWN
)
2470 gfc_set_default_type (prog_unit
, 1, prog_unit
->formal_ns
);
2471 else if (prog_unit
->result
!= prog_unit
2472 && prog_unit
->result
->ts
.type
== BT_UNKNOWN
)
2473 gfc_set_default_type (prog_unit
->result
, 1,
2474 prog_unit
->formal_ns
);
2477 if (st
!= ST_END_SUBROUTINE
&& st
!= ST_END_FUNCTION
)
2479 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2480 gfc_ascii_statement (st
));
2481 reject_statement ();
2485 /* Add EXTERNAL attribute to function or subroutine. */
2486 if (current_interface
.type
!= INTERFACE_ABSTRACT
&& !prog_unit
->attr
.dummy
)
2487 gfc_add_external (&prog_unit
->attr
, &gfc_current_locus
);
2489 current_interface
= save
;
2490 gfc_add_interface (prog_unit
);
2493 if (current_interface
.ns
2494 && current_interface
.ns
->proc_name
2495 && strcmp (current_interface
.ns
->proc_name
->name
,
2496 prog_unit
->name
) == 0)
2497 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2498 "enclosing procedure", prog_unit
->name
,
2499 ¤t_interface
.ns
->proc_name
->declared_at
);
2508 /* Associate function characteristics by going back to the function
2509 declaration and rematching the prefix. */
2512 match_deferred_characteristics (gfc_typespec
* ts
)
2515 match m
= MATCH_ERROR
;
2516 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2518 loc
= gfc_current_locus
;
2520 gfc_current_locus
= gfc_current_block ()->declared_at
;
2523 gfc_buffer_error (1);
2524 m
= gfc_match_prefix (ts
);
2525 gfc_buffer_error (0);
2527 if (ts
->type
== BT_DERIVED
)
2535 /* Only permit one go at the characteristic association. */
2539 /* Set the function locus correctly. If we have not found the
2540 function name, there is an error. */
2542 && gfc_match ("function% %n", name
) == MATCH_YES
2543 && strcmp (name
, gfc_current_block ()->name
) == 0)
2545 gfc_current_block ()->declared_at
= gfc_current_locus
;
2546 gfc_commit_symbols ();
2551 gfc_undo_symbols ();
2554 gfc_current_locus
=loc
;
2559 /* Check specification-expressions in the function result of the currently
2560 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2561 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2562 scope are not yet parsed so this has to be delayed up to parse_spec. */
2565 check_function_result_typed (void)
2567 gfc_typespec
* ts
= &gfc_current_ns
->proc_name
->result
->ts
;
2569 gcc_assert (gfc_current_state () == COMP_FUNCTION
);
2570 gcc_assert (ts
->type
!= BT_UNKNOWN
);
2572 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2573 /* TODO: Extend when KIND type parameters are implemented. */
2574 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
)
2575 gfc_expr_check_typed (ts
->u
.cl
->length
, gfc_current_ns
, true);
2579 /* Parse a set of specification statements. Returns the statement
2580 that doesn't fit. */
2582 static gfc_statement
2583 parse_spec (gfc_statement st
)
2586 bool function_result_typed
= false;
2587 bool bad_characteristic
= false;
2590 verify_st_order (&ss
, ST_NONE
, false);
2592 st
= next_statement ();
2594 /* If we are not inside a function or don't have a result specified so far,
2595 do nothing special about it. */
2596 if (gfc_current_state () != COMP_FUNCTION
)
2597 function_result_typed
= true;
2600 gfc_symbol
* proc
= gfc_current_ns
->proc_name
;
2603 if (proc
->result
->ts
.type
== BT_UNKNOWN
)
2604 function_result_typed
= true;
2609 /* If we're inside a BLOCK construct, some statements are disallowed.
2610 Check this here. Attribute declaration statements like INTENT, OPTIONAL
2611 or VALUE are also disallowed, but they don't have a particular ST_*
2612 key so we have to check for them individually in their matcher routine. */
2613 if (gfc_current_state () == COMP_BLOCK
)
2617 case ST_IMPLICIT_NONE
:
2620 case ST_EQUIVALENCE
:
2621 case ST_STATEMENT_FUNCTION
:
2622 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
2623 gfc_ascii_statement (st
));
2624 reject_statement ();
2631 /* If we find a statement that can not be followed by an IMPLICIT statement
2632 (and thus we can expect to see none any further), type the function result
2633 if it has not yet been typed. Be careful not to give the END statement
2634 to verify_st_order! */
2635 if (!function_result_typed
&& st
!= ST_GET_FCN_CHARACTERISTICS
)
2637 bool verify_now
= false;
2639 if (st
== ST_END_FUNCTION
|| st
== ST_CONTAINS
)
2644 verify_st_order (&dummyss
, ST_NONE
, false);
2645 verify_st_order (&dummyss
, st
, false);
2647 if (verify_st_order (&dummyss
, ST_IMPLICIT
, true) == FAILURE
)
2653 check_function_result_typed ();
2654 function_result_typed
= true;
2663 case ST_IMPLICIT_NONE
:
2665 if (!function_result_typed
)
2667 check_function_result_typed ();
2668 function_result_typed
= true;
2674 case ST_DATA
: /* Not allowed in interfaces */
2675 if (gfc_current_state () == COMP_INTERFACE
)
2685 case ST_DERIVED_DECL
:
2688 if (verify_st_order (&ss
, st
, false) == FAILURE
)
2690 reject_statement ();
2691 st
= next_statement ();
2701 case ST_DERIVED_DECL
:
2707 if (gfc_current_state () != COMP_MODULE
)
2709 gfc_error ("%s statement must appear in a MODULE",
2710 gfc_ascii_statement (st
));
2711 reject_statement ();
2715 if (gfc_current_ns
->default_access
!= ACCESS_UNKNOWN
)
2717 gfc_error ("%s statement at %C follows another accessibility "
2718 "specification", gfc_ascii_statement (st
));
2719 reject_statement ();
2723 gfc_current_ns
->default_access
= (st
== ST_PUBLIC
)
2724 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
2728 case ST_STATEMENT_FUNCTION
:
2729 if (gfc_current_state () == COMP_MODULE
)
2731 unexpected_statement (st
);
2739 accept_statement (st
);
2740 st
= next_statement ();
2744 accept_statement (st
);
2746 st
= next_statement ();
2749 case ST_GET_FCN_CHARACTERISTICS
:
2750 /* This statement triggers the association of a function's result
2752 ts
= &gfc_current_block ()->result
->ts
;
2753 if (match_deferred_characteristics (ts
) != MATCH_YES
)
2754 bad_characteristic
= true;
2756 st
= next_statement ();
2763 /* If match_deferred_characteristics failed, then there is an error. */
2764 if (bad_characteristic
)
2766 ts
= &gfc_current_block ()->result
->ts
;
2767 if (ts
->type
!= BT_DERIVED
)
2768 gfc_error ("Bad kind expression for function '%s' at %L",
2769 gfc_current_block ()->name
,
2770 &gfc_current_block ()->declared_at
);
2772 gfc_error ("The type for function '%s' at %L is not accessible",
2773 gfc_current_block ()->name
,
2774 &gfc_current_block ()->declared_at
);
2776 gfc_current_block ()->ts
.kind
= 0;
2777 /* Keep the derived type; if it's bad, it will be discovered later. */
2778 if (!(ts
->type
== BT_DERIVED
&& ts
->u
.derived
))
2779 ts
->type
= BT_UNKNOWN
;
2786 /* Parse a WHERE block, (not a simple WHERE statement). */
2789 parse_where_block (void)
2791 int seen_empty_else
;
2796 accept_statement (ST_WHERE_BLOCK
);
2797 top
= gfc_state_stack
->tail
;
2799 push_state (&s
, COMP_WHERE
, gfc_new_block
);
2801 d
= add_statement ();
2802 d
->expr1
= top
->expr1
;
2808 seen_empty_else
= 0;
2812 st
= next_statement ();
2818 case ST_WHERE_BLOCK
:
2819 parse_where_block ();
2824 accept_statement (st
);
2828 if (seen_empty_else
)
2830 gfc_error ("ELSEWHERE statement at %C follows previous "
2831 "unmasked ELSEWHERE");
2832 reject_statement ();
2836 if (new_st
.expr1
== NULL
)
2837 seen_empty_else
= 1;
2839 d
= new_level (gfc_state_stack
->head
);
2841 d
->expr1
= new_st
.expr1
;
2843 accept_statement (st
);
2848 accept_statement (st
);
2852 gfc_error ("Unexpected %s statement in WHERE block at %C",
2853 gfc_ascii_statement (st
));
2854 reject_statement ();
2858 while (st
!= ST_END_WHERE
);
2864 /* Parse a FORALL block (not a simple FORALL statement). */
2867 parse_forall_block (void)
2873 accept_statement (ST_FORALL_BLOCK
);
2874 top
= gfc_state_stack
->tail
;
2876 push_state (&s
, COMP_FORALL
, gfc_new_block
);
2878 d
= add_statement ();
2879 d
->op
= EXEC_FORALL
;
2884 st
= next_statement ();
2889 case ST_POINTER_ASSIGNMENT
:
2892 accept_statement (st
);
2895 case ST_WHERE_BLOCK
:
2896 parse_where_block ();
2899 case ST_FORALL_BLOCK
:
2900 parse_forall_block ();
2904 accept_statement (st
);
2911 gfc_error ("Unexpected %s statement in FORALL block at %C",
2912 gfc_ascii_statement (st
));
2914 reject_statement ();
2918 while (st
!= ST_END_FORALL
);
2924 static gfc_statement
parse_executable (gfc_statement
);
2926 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2929 parse_if_block (void)
2938 accept_statement (ST_IF_BLOCK
);
2940 top
= gfc_state_stack
->tail
;
2941 push_state (&s
, COMP_IF
, gfc_new_block
);
2943 new_st
.op
= EXEC_IF
;
2944 d
= add_statement ();
2946 d
->expr1
= top
->expr1
;
2952 st
= parse_executable (ST_NONE
);
2962 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2963 "statement at %L", &else_locus
);
2965 reject_statement ();
2969 d
= new_level (gfc_state_stack
->head
);
2971 d
->expr1
= new_st
.expr1
;
2973 accept_statement (st
);
2980 gfc_error ("Duplicate ELSE statements at %L and %C",
2982 reject_statement ();
2987 else_locus
= gfc_current_locus
;
2989 d
= new_level (gfc_state_stack
->head
);
2992 accept_statement (st
);
3000 unexpected_statement (st
);
3004 while (st
!= ST_ENDIF
);
3007 accept_statement (st
);
3011 /* Parse a SELECT block. */
3014 parse_select_block (void)
3020 accept_statement (ST_SELECT_CASE
);
3022 cp
= gfc_state_stack
->tail
;
3023 push_state (&s
, COMP_SELECT
, gfc_new_block
);
3025 /* Make sure that the next statement is a CASE or END SELECT. */
3028 st
= next_statement ();
3031 if (st
== ST_END_SELECT
)
3033 /* Empty SELECT CASE is OK. */
3034 accept_statement (st
);
3041 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
3044 reject_statement ();
3047 /* At this point, we're got a nonempty select block. */
3048 cp
= new_level (cp
);
3051 accept_statement (st
);
3055 st
= parse_executable (ST_NONE
);
3062 cp
= new_level (gfc_state_stack
->head
);
3064 gfc_clear_new_st ();
3066 accept_statement (st
);
3072 /* Can't have an executable statement because of
3073 parse_executable(). */
3075 unexpected_statement (st
);
3079 while (st
!= ST_END_SELECT
);
3082 accept_statement (st
);
3086 /* Pop the current selector from the SELECT TYPE stack. */
3089 select_type_pop (void)
3091 gfc_select_type_stack
*old
= select_type_stack
;
3092 select_type_stack
= old
->prev
;
3097 /* Parse a SELECT TYPE construct (F03:R821). */
3100 parse_select_type_block (void)
3106 accept_statement (ST_SELECT_TYPE
);
3108 cp
= gfc_state_stack
->tail
;
3109 push_state (&s
, COMP_SELECT_TYPE
, gfc_new_block
);
3111 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
3115 st
= next_statement ();
3118 if (st
== ST_END_SELECT
)
3119 /* Empty SELECT CASE is OK. */
3121 if (st
== ST_TYPE_IS
|| st
== ST_CLASS_IS
)
3124 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
3125 "following SELECT TYPE at %C");
3127 reject_statement ();
3130 /* At this point, we're got a nonempty select block. */
3131 cp
= new_level (cp
);
3134 accept_statement (st
);
3138 st
= parse_executable (ST_NONE
);
3146 cp
= new_level (gfc_state_stack
->head
);
3148 gfc_clear_new_st ();
3150 accept_statement (st
);
3156 /* Can't have an executable statement because of
3157 parse_executable(). */
3159 unexpected_statement (st
);
3163 while (st
!= ST_END_SELECT
);
3167 accept_statement (st
);
3168 gfc_current_ns
= gfc_current_ns
->parent
;
3173 /* Given a symbol, make sure it is not an iteration variable for a DO
3174 statement. This subroutine is called when the symbol is seen in a
3175 context that causes it to become redefined. If the symbol is an
3176 iterator, we generate an error message and return nonzero. */
3179 gfc_check_do_variable (gfc_symtree
*st
)
3183 for (s
=gfc_state_stack
; s
; s
= s
->previous
)
3184 if (s
->do_variable
== st
)
3186 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
3187 "loop beginning at %L", st
->name
, &s
->head
->loc
);
3195 /* Checks to see if the current statement label closes an enddo.
3196 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3197 an error) if it incorrectly closes an ENDDO. */
3200 check_do_closure (void)
3204 if (gfc_statement_label
== NULL
)
3207 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
3208 if (p
->state
== COMP_DO
|| p
->state
== COMP_DO_CONCURRENT
)
3212 return 0; /* No loops to close */
3214 if (p
->ext
.end_do_label
== gfc_statement_label
)
3216 if (p
== gfc_state_stack
)
3219 gfc_error ("End of nonblock DO statement at %C is within another block");
3223 /* At this point, the label doesn't terminate the innermost loop.
3224 Make sure it doesn't terminate another one. */
3225 for (; p
; p
= p
->previous
)
3226 if ((p
->state
== COMP_DO
|| p
->state
== COMP_DO_CONCURRENT
)
3227 && p
->ext
.end_do_label
== gfc_statement_label
)
3229 gfc_error ("End of nonblock DO statement at %C is interwoven "
3230 "with another DO loop");
3238 /* Parse a series of contained program units. */
3240 static void parse_progunit (gfc_statement
);
3243 /* Parse a CRITICAL block. */
3246 parse_critical_block (void)
3252 s
.ext
.end_do_label
= new_st
.label1
;
3254 accept_statement (ST_CRITICAL
);
3255 top
= gfc_state_stack
->tail
;
3257 push_state (&s
, COMP_CRITICAL
, gfc_new_block
);
3259 d
= add_statement ();
3260 d
->op
= EXEC_CRITICAL
;
3265 st
= parse_executable (ST_NONE
);
3273 case ST_END_CRITICAL
:
3274 if (s
.ext
.end_do_label
!= NULL
3275 && s
.ext
.end_do_label
!= gfc_statement_label
)
3276 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3277 "match CRITICAL label");
3279 if (gfc_statement_label
!= NULL
)
3281 new_st
.op
= EXEC_NOP
;
3287 unexpected_statement (st
);
3291 while (st
!= ST_END_CRITICAL
);
3294 accept_statement (st
);
3298 /* Set up the local namespace for a BLOCK construct. */
3301 gfc_build_block_ns (gfc_namespace
*parent_ns
)
3303 gfc_namespace
* my_ns
;
3304 static int numblock
= 1;
3306 my_ns
= gfc_get_namespace (parent_ns
, 1);
3307 my_ns
->construct_entities
= 1;
3309 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3310 code generation (so it must not be NULL).
3311 We set its recursive argument if our container procedure is recursive, so
3312 that local variables are accordingly placed on the stack when it
3313 will be necessary. */
3315 my_ns
->proc_name
= gfc_new_block
;
3319 char buffer
[20]; /* Enough to hold "block@2147483648\n". */
3321 snprintf(buffer
, sizeof(buffer
), "block@%d", numblock
++);
3322 gfc_get_symbol (buffer
, my_ns
, &my_ns
->proc_name
);
3323 t
= gfc_add_flavor (&my_ns
->proc_name
->attr
, FL_LABEL
,
3324 my_ns
->proc_name
->name
, NULL
);
3325 gcc_assert (t
== SUCCESS
);
3326 gfc_commit_symbol (my_ns
->proc_name
);
3329 if (parent_ns
->proc_name
)
3330 my_ns
->proc_name
->attr
.recursive
= parent_ns
->proc_name
->attr
.recursive
;
3336 /* Parse a BLOCK construct. */
3339 parse_block_construct (void)
3341 gfc_namespace
* my_ns
;
3344 gfc_notify_std (GFC_STD_F2008
, "BLOCK construct at %C");
3346 my_ns
= gfc_build_block_ns (gfc_current_ns
);
3348 new_st
.op
= EXEC_BLOCK
;
3349 new_st
.ext
.block
.ns
= my_ns
;
3350 new_st
.ext
.block
.assoc
= NULL
;
3351 accept_statement (ST_BLOCK
);
3353 push_state (&s
, COMP_BLOCK
, my_ns
->proc_name
);
3354 gfc_current_ns
= my_ns
;
3356 parse_progunit (ST_NONE
);
3358 gfc_current_ns
= gfc_current_ns
->parent
;
3363 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3364 behind the scenes with compiler-generated variables. */
3367 parse_associate (void)
3369 gfc_namespace
* my_ns
;
3372 gfc_association_list
* a
;
3374 gfc_notify_std (GFC_STD_F2003
, "ASSOCIATE construct at %C");
3376 my_ns
= gfc_build_block_ns (gfc_current_ns
);
3378 new_st
.op
= EXEC_BLOCK
;
3379 new_st
.ext
.block
.ns
= my_ns
;
3380 gcc_assert (new_st
.ext
.block
.assoc
);
3382 /* Add all associate-names as BLOCK variables. Creating them is enough
3383 for now, they'll get their values during trans-* phase. */
3384 gfc_current_ns
= my_ns
;
3385 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
3389 if (gfc_get_sym_tree (a
->name
, NULL
, &a
->st
, false))
3393 sym
->attr
.flavor
= FL_VARIABLE
;
3395 sym
->declared_at
= a
->where
;
3396 gfc_set_sym_referenced (sym
);
3398 /* Initialize the typespec. It is not available in all cases,
3399 however, as it may only be set on the target during resolution.
3400 Still, sometimes it helps to have it right now -- especially
3401 for parsing component references on the associate-name
3402 in case of association to a derived-type. */
3403 sym
->ts
= a
->target
->ts
;
3406 accept_statement (ST_ASSOCIATE
);
3407 push_state (&s
, COMP_ASSOCIATE
, my_ns
->proc_name
);
3410 st
= parse_executable (ST_NONE
);
3417 accept_statement (st
);
3418 my_ns
->code
= gfc_state_stack
->head
;
3422 unexpected_statement (st
);
3426 gfc_current_ns
= gfc_current_ns
->parent
;
3431 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
3432 handled inside of parse_executable(), because they aren't really
3436 parse_do_block (void)
3445 s
.ext
.end_do_label
= new_st
.label1
;
3447 if (new_st
.ext
.iterator
!= NULL
)
3448 stree
= new_st
.ext
.iterator
->var
->symtree
;
3452 accept_statement (ST_DO
);
3454 top
= gfc_state_stack
->tail
;
3455 push_state (&s
, do_op
== EXEC_DO_CONCURRENT
? COMP_DO_CONCURRENT
: COMP_DO
,
3458 s
.do_variable
= stree
;
3460 top
->block
= new_level (top
);
3461 top
->block
->op
= EXEC_DO
;
3464 st
= parse_executable (ST_NONE
);
3472 if (s
.ext
.end_do_label
!= NULL
3473 && s
.ext
.end_do_label
!= gfc_statement_label
)
3474 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
3477 if (gfc_statement_label
!= NULL
)
3479 new_st
.op
= EXEC_NOP
;
3484 case ST_IMPLIED_ENDDO
:
3485 /* If the do-stmt of this DO construct has a do-construct-name,
3486 the corresponding end-do must be an end-do-stmt (with a matching
3487 name, but in that case we must have seen ST_ENDDO first).
3488 We only complain about this in pedantic mode. */
3489 if (gfc_current_block () != NULL
)
3490 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
3491 &gfc_current_block()->declared_at
);
3496 unexpected_statement (st
);
3501 accept_statement (st
);
3505 /* Parse the statements of OpenMP do/parallel do. */
3507 static gfc_statement
3508 parse_omp_do (gfc_statement omp_st
)
3514 accept_statement (omp_st
);
3516 cp
= gfc_state_stack
->tail
;
3517 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3518 np
= new_level (cp
);
3524 st
= next_statement ();
3527 else if (st
== ST_DO
)
3530 unexpected_statement (st
);
3534 if (gfc_statement_label
!= NULL
3535 && gfc_state_stack
->previous
!= NULL
3536 && gfc_state_stack
->previous
->state
== COMP_DO
3537 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
3545 there should be no !$OMP END DO. */
3547 return ST_IMPLIED_ENDDO
;
3550 check_do_closure ();
3553 st
= next_statement ();
3554 if (st
== (omp_st
== ST_OMP_DO
? ST_OMP_END_DO
: ST_OMP_END_PARALLEL_DO
))
3556 if (new_st
.op
== EXEC_OMP_END_NOWAIT
)
3557 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
3559 gcc_assert (new_st
.op
== EXEC_NOP
);
3560 gfc_clear_new_st ();
3561 gfc_commit_symbols ();
3562 gfc_warning_check ();
3563 st
= next_statement ();
3569 /* Parse the statements of OpenMP atomic directive. */
3571 static gfc_statement
3572 parse_omp_atomic (void)
3579 accept_statement (ST_OMP_ATOMIC
);
3581 cp
= gfc_state_stack
->tail
;
3582 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3583 np
= new_level (cp
);
3586 count
= 1 + (cp
->ext
.omp_atomic
== GFC_OMP_ATOMIC_CAPTURE
);
3590 st
= next_statement ();
3593 else if (st
== ST_ASSIGNMENT
)
3595 accept_statement (st
);
3599 unexpected_statement (st
);
3604 st
= next_statement ();
3605 if (st
== ST_OMP_END_ATOMIC
)
3607 gfc_clear_new_st ();
3608 gfc_commit_symbols ();
3609 gfc_warning_check ();
3610 st
= next_statement ();
3612 else if (cp
->ext
.omp_atomic
== GFC_OMP_ATOMIC_CAPTURE
)
3613 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
3618 /* Parse the statements of an OpenMP structured block. */
3621 parse_omp_structured_block (gfc_statement omp_st
, bool workshare_stmts_only
)
3623 gfc_statement st
, omp_end_st
;
3627 accept_statement (omp_st
);
3629 cp
= gfc_state_stack
->tail
;
3630 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3631 np
= new_level (cp
);
3637 case ST_OMP_PARALLEL
:
3638 omp_end_st
= ST_OMP_END_PARALLEL
;
3640 case ST_OMP_PARALLEL_SECTIONS
:
3641 omp_end_st
= ST_OMP_END_PARALLEL_SECTIONS
;
3643 case ST_OMP_SECTIONS
:
3644 omp_end_st
= ST_OMP_END_SECTIONS
;
3646 case ST_OMP_ORDERED
:
3647 omp_end_st
= ST_OMP_END_ORDERED
;
3649 case ST_OMP_CRITICAL
:
3650 omp_end_st
= ST_OMP_END_CRITICAL
;
3653 omp_end_st
= ST_OMP_END_MASTER
;
3656 omp_end_st
= ST_OMP_END_SINGLE
;
3659 omp_end_st
= ST_OMP_END_TASK
;
3661 case ST_OMP_WORKSHARE
:
3662 omp_end_st
= ST_OMP_END_WORKSHARE
;
3664 case ST_OMP_PARALLEL_WORKSHARE
:
3665 omp_end_st
= ST_OMP_END_PARALLEL_WORKSHARE
;
3673 if (workshare_stmts_only
)
3675 /* Inside of !$omp workshare, only
3678 where statements and constructs
3679 forall statements and constructs
3683 are allowed. For !$omp critical these
3684 restrictions apply recursively. */
3687 st
= next_statement ();
3698 accept_statement (st
);
3701 case ST_WHERE_BLOCK
:
3702 parse_where_block ();
3705 case ST_FORALL_BLOCK
:
3706 parse_forall_block ();
3709 case ST_OMP_PARALLEL
:
3710 case ST_OMP_PARALLEL_SECTIONS
:
3711 parse_omp_structured_block (st
, false);
3714 case ST_OMP_PARALLEL_WORKSHARE
:
3715 case ST_OMP_CRITICAL
:
3716 parse_omp_structured_block (st
, true);
3719 case ST_OMP_PARALLEL_DO
:
3720 st
= parse_omp_do (st
);
3724 st
= parse_omp_atomic ();
3735 st
= next_statement ();
3739 st
= parse_executable (ST_NONE
);
3742 else if (st
== ST_OMP_SECTION
3743 && (omp_st
== ST_OMP_SECTIONS
3744 || omp_st
== ST_OMP_PARALLEL_SECTIONS
))
3746 np
= new_level (np
);
3750 else if (st
!= omp_end_st
)
3751 unexpected_statement (st
);
3753 while (st
!= omp_end_st
);
3757 case EXEC_OMP_END_NOWAIT
:
3758 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
3760 case EXEC_OMP_CRITICAL
:
3761 if (((cp
->ext
.omp_name
== NULL
) ^ (new_st
.ext
.omp_name
== NULL
))
3762 || (new_st
.ext
.omp_name
!= NULL
3763 && strcmp (cp
->ext
.omp_name
, new_st
.ext
.omp_name
) != 0))
3764 gfc_error ("Name after !$omp critical and !$omp end critical does "
3766 free (CONST_CAST (char *, new_st
.ext
.omp_name
));
3768 case EXEC_OMP_END_SINGLE
:
3769 cp
->ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
]
3770 = new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
];
3771 new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
] = NULL
;
3772 gfc_free_omp_clauses (new_st
.ext
.omp_clauses
);
3780 gfc_clear_new_st ();
3781 gfc_commit_symbols ();
3782 gfc_warning_check ();
3787 /* Accept a series of executable statements. We return the first
3788 statement that doesn't fit to the caller. Any block statements are
3789 passed on to the correct handler, which usually passes the buck
3792 static gfc_statement
3793 parse_executable (gfc_statement st
)
3798 st
= next_statement ();
3802 close_flag
= check_do_closure ();
3807 case ST_END_PROGRAM
:
3810 case ST_END_FUNCTION
:
3815 case ST_END_SUBROUTINE
:
3820 case ST_SELECT_CASE
:
3821 gfc_error ("%s statement at %C cannot terminate a non-block "
3822 "DO loop", gfc_ascii_statement (st
));
3835 gfc_notify_std (GFC_STD_F95_OBS
, "DATA statement at %C after the "
3836 "first executable statement");
3842 accept_statement (st
);
3843 if (close_flag
== 1)
3844 return ST_IMPLIED_ENDDO
;
3848 parse_block_construct ();
3859 case ST_SELECT_CASE
:
3860 parse_select_block ();
3863 case ST_SELECT_TYPE
:
3864 parse_select_type_block();
3869 if (check_do_closure () == 1)
3870 return ST_IMPLIED_ENDDO
;
3874 parse_critical_block ();
3877 case ST_WHERE_BLOCK
:
3878 parse_where_block ();
3881 case ST_FORALL_BLOCK
:
3882 parse_forall_block ();
3885 case ST_OMP_PARALLEL
:
3886 case ST_OMP_PARALLEL_SECTIONS
:
3887 case ST_OMP_SECTIONS
:
3888 case ST_OMP_ORDERED
:
3889 case ST_OMP_CRITICAL
:
3893 parse_omp_structured_block (st
, false);
3896 case ST_OMP_WORKSHARE
:
3897 case ST_OMP_PARALLEL_WORKSHARE
:
3898 parse_omp_structured_block (st
, true);
3902 case ST_OMP_PARALLEL_DO
:
3903 st
= parse_omp_do (st
);
3904 if (st
== ST_IMPLIED_ENDDO
)
3909 st
= parse_omp_atomic ();
3916 st
= next_statement ();
3921 /* Fix the symbols for sibling functions. These are incorrectly added to
3922 the child namespace as the parser didn't know about this procedure. */
3925 gfc_fixup_sibling_symbols (gfc_symbol
*sym
, gfc_namespace
*siblings
)
3929 gfc_symbol
*old_sym
;
3931 sym
->attr
.referenced
= 1;
3932 for (ns
= siblings
; ns
; ns
= ns
->sibling
)
3934 st
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
3936 if (!st
|| (st
->n
.sym
->attr
.dummy
&& ns
== st
->n
.sym
->ns
))
3937 goto fixup_contained
;
3939 if ((st
->n
.sym
->attr
.flavor
== FL_DERIVED
3940 && sym
->attr
.generic
&& sym
->attr
.function
)
3941 ||(sym
->attr
.flavor
== FL_DERIVED
3942 && st
->n
.sym
->attr
.generic
&& st
->n
.sym
->attr
.function
))
3943 goto fixup_contained
;
3945 old_sym
= st
->n
.sym
;
3946 if (old_sym
->ns
== ns
3947 && !old_sym
->attr
.contained
3949 /* By 14.6.1.3, host association should be excluded
3950 for the following. */
3951 && !(old_sym
->attr
.external
3952 || (old_sym
->ts
.type
!= BT_UNKNOWN
3953 && !old_sym
->attr
.implicit_type
)
3954 || old_sym
->attr
.flavor
== FL_PARAMETER
3955 || old_sym
->attr
.use_assoc
3956 || old_sym
->attr
.in_common
3957 || old_sym
->attr
.in_equivalence
3958 || old_sym
->attr
.data
3959 || old_sym
->attr
.dummy
3960 || old_sym
->attr
.result
3961 || old_sym
->attr
.dimension
3962 || old_sym
->attr
.allocatable
3963 || old_sym
->attr
.intrinsic
3964 || old_sym
->attr
.generic
3965 || old_sym
->attr
.flavor
== FL_NAMELIST
3966 || old_sym
->attr
.flavor
== FL_LABEL
3967 || old_sym
->attr
.proc
== PROC_ST_FUNCTION
))
3969 /* Replace it with the symbol from the parent namespace. */
3973 gfc_release_symbol (old_sym
);
3977 /* Do the same for any contained procedures. */
3978 gfc_fixup_sibling_symbols (sym
, ns
->contained
);
3983 parse_contained (int module
)
3985 gfc_namespace
*ns
, *parent_ns
, *tmp
;
3986 gfc_state_data s1
, s2
;
3990 int contains_statements
= 0;
3993 push_state (&s1
, COMP_CONTAINS
, NULL
);
3994 parent_ns
= gfc_current_ns
;
3998 gfc_current_ns
= gfc_get_namespace (parent_ns
, 1);
4000 gfc_current_ns
->sibling
= parent_ns
->contained
;
4001 parent_ns
->contained
= gfc_current_ns
;
4004 /* Process the next available statement. We come here if we got an error
4005 and rejected the last statement. */
4006 st
= next_statement ();
4015 contains_statements
= 1;
4016 accept_statement (st
);
4019 (st
== ST_FUNCTION
) ? COMP_FUNCTION
: COMP_SUBROUTINE
,
4022 /* For internal procedures, create/update the symbol in the
4023 parent namespace. */
4027 if (gfc_get_symbol (gfc_new_block
->name
, parent_ns
, &sym
))
4028 gfc_error ("Contained procedure '%s' at %C is already "
4029 "ambiguous", gfc_new_block
->name
);
4032 if (gfc_add_procedure (&sym
->attr
, PROC_INTERNAL
, sym
->name
,
4033 &gfc_new_block
->declared_at
) ==
4036 if (st
== ST_FUNCTION
)
4037 gfc_add_function (&sym
->attr
, sym
->name
,
4038 &gfc_new_block
->declared_at
);
4040 gfc_add_subroutine (&sym
->attr
, sym
->name
,
4041 &gfc_new_block
->declared_at
);
4045 gfc_commit_symbols ();
4048 sym
= gfc_new_block
;
4050 /* Mark this as a contained function, so it isn't replaced
4051 by other module functions. */
4052 sym
->attr
.contained
= 1;
4053 sym
->attr
.referenced
= 1;
4055 /* Set implicit_pure so that it can be reset if any of the
4056 tests for purity fail. This is used for some optimisation
4057 during translation. */
4058 if (!sym
->attr
.pure
)
4059 sym
->attr
.implicit_pure
= 1;
4061 parse_progunit (ST_NONE
);
4063 /* Fix up any sibling functions that refer to this one. */
4064 gfc_fixup_sibling_symbols (sym
, gfc_current_ns
);
4065 /* Or refer to any of its alternate entry points. */
4066 for (el
= gfc_current_ns
->entries
; el
; el
= el
->next
)
4067 gfc_fixup_sibling_symbols (el
->sym
, gfc_current_ns
);
4069 gfc_current_ns
->code
= s2
.head
;
4070 gfc_current_ns
= parent_ns
;
4075 /* These statements are associated with the end of the host unit. */
4076 case ST_END_FUNCTION
:
4078 case ST_END_PROGRAM
:
4079 case ST_END_SUBROUTINE
:
4080 accept_statement (st
);
4081 gfc_current_ns
->code
= s1
.head
;
4085 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
4086 gfc_ascii_statement (st
));
4087 reject_statement ();
4093 while (st
!= ST_END_FUNCTION
&& st
!= ST_END_SUBROUTINE
4094 && st
!= ST_END_MODULE
&& st
!= ST_END_PROGRAM
);
4096 /* The first namespace in the list is guaranteed to not have
4097 anything (worthwhile) in it. */
4098 tmp
= gfc_current_ns
;
4099 gfc_current_ns
= parent_ns
;
4100 if (seen_error
&& tmp
->refs
> 1)
4101 gfc_free_namespace (tmp
);
4103 ns
= gfc_current_ns
->contained
;
4104 gfc_current_ns
->contained
= ns
->sibling
;
4105 gfc_free_namespace (ns
);
4108 if (!contains_statements
)
4109 gfc_notify_std (GFC_STD_F2008
, "CONTAINS statement without "
4110 "FUNCTION or SUBROUTINE statement at %C");
4114 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
4117 parse_progunit (gfc_statement st
)
4122 st
= parse_spec (st
);
4129 /* This is not allowed within BLOCK! */
4130 if (gfc_current_state () != COMP_BLOCK
)
4135 accept_statement (st
);
4142 if (gfc_current_state () == COMP_FUNCTION
)
4143 gfc_check_function_type (gfc_current_ns
);
4148 st
= parse_executable (st
);
4156 /* This is not allowed within BLOCK! */
4157 if (gfc_current_state () != COMP_BLOCK
)
4162 accept_statement (st
);
4169 unexpected_statement (st
);
4170 reject_statement ();
4171 st
= next_statement ();
4177 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
4178 if (p
->state
== COMP_CONTAINS
)
4181 if (gfc_find_state (COMP_MODULE
) == SUCCESS
)
4186 gfc_error ("CONTAINS statement at %C is already in a contained "
4188 reject_statement ();
4189 st
= next_statement ();
4193 parse_contained (0);
4196 gfc_current_ns
->code
= gfc_state_stack
->head
;
4200 /* Come here to complain about a global symbol already in use as
4204 gfc_global_used (gfc_gsymbol
*sym
, locus
*where
)
4209 where
= &gfc_current_locus
;
4219 case GSYM_SUBROUTINE
:
4220 name
= "SUBROUTINE";
4225 case GSYM_BLOCK_DATA
:
4226 name
= "BLOCK DATA";
4232 gfc_internal_error ("gfc_global_used(): Bad type");
4236 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
4237 sym
->name
, where
, name
, &sym
->where
);
4241 /* Parse a block data program unit. */
4244 parse_block_data (void)
4247 static locus blank_locus
;
4248 static int blank_block
=0;
4251 gfc_current_ns
->proc_name
= gfc_new_block
;
4252 gfc_current_ns
->is_block_data
= 1;
4254 if (gfc_new_block
== NULL
)
4257 gfc_error ("Blank BLOCK DATA at %C conflicts with "
4258 "prior BLOCK DATA at %L", &blank_locus
);
4262 blank_locus
= gfc_current_locus
;
4267 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4269 || (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_BLOCK_DATA
))
4270 gfc_global_used(s
, NULL
);
4273 s
->type
= GSYM_BLOCK_DATA
;
4274 s
->where
= gfc_current_locus
;
4279 st
= parse_spec (ST_NONE
);
4281 while (st
!= ST_END_BLOCK_DATA
)
4283 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
4284 gfc_ascii_statement (st
));
4285 reject_statement ();
4286 st
= next_statement ();
4291 /* Parse a module subprogram. */
4299 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4300 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_MODULE
))
4301 gfc_global_used(s
, NULL
);
4304 s
->type
= GSYM_MODULE
;
4305 s
->where
= gfc_current_locus
;
4309 st
= parse_spec (ST_NONE
);
4318 parse_contained (1);
4322 accept_statement (st
);
4326 gfc_error ("Unexpected %s statement in MODULE at %C",
4327 gfc_ascii_statement (st
));
4329 reject_statement ();
4330 st
= next_statement ();
4334 s
->ns
= gfc_current_ns
;
4338 /* Add a procedure name to the global symbol table. */
4341 add_global_procedure (int sub
)
4345 s
= gfc_get_gsymbol(gfc_new_block
->name
);
4348 || (s
->type
!= GSYM_UNKNOWN
4349 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
4350 gfc_global_used(s
, NULL
);
4353 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
4354 s
->where
= gfc_current_locus
;
4356 s
->ns
= gfc_current_ns
;
4361 /* Add a program to the global symbol table. */
4364 add_global_program (void)
4368 if (gfc_new_block
== NULL
)
4370 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4372 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_PROGRAM
))
4373 gfc_global_used(s
, NULL
);
4376 s
->type
= GSYM_PROGRAM
;
4377 s
->where
= gfc_current_locus
;
4379 s
->ns
= gfc_current_ns
;
4384 /* Resolve all the program units when whole file scope option
4387 resolve_all_program_units (gfc_namespace
*gfc_global_ns_list
)
4389 gfc_free_dt_list ();
4390 gfc_current_ns
= gfc_global_ns_list
;
4391 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4393 if (gfc_current_ns
->proc_name
4394 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
4395 continue; /* Already resolved. */
4397 if (gfc_current_ns
->proc_name
)
4398 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4399 gfc_resolve (gfc_current_ns
);
4400 gfc_current_ns
->derived_types
= gfc_derived_types
;
4401 gfc_derived_types
= NULL
;
4407 clean_up_modules (gfc_gsymbol
*gsym
)
4412 clean_up_modules (gsym
->left
);
4413 clean_up_modules (gsym
->right
);
4415 if (gsym
->type
!= GSYM_MODULE
|| !gsym
->ns
)
4418 gfc_current_ns
= gsym
->ns
;
4419 gfc_derived_types
= gfc_current_ns
->derived_types
;
4426 /* Translate all the program units when whole file scope option
4427 is active. This could be in a different order to resolution if
4428 there are forward references in the file. */
4430 translate_all_program_units (gfc_namespace
*gfc_global_ns_list
,
4435 gfc_current_ns
= gfc_global_ns_list
;
4436 gfc_get_errors (NULL
, &errors
);
4438 /* If the main program is in the translation unit and we have
4439 -fcoarray=libs, generate the static variables. */
4440 if (gfc_option
.coarray
== GFC_FCOARRAY_LIB
&& main_in_tu
)
4441 gfc_init_coarray_decl (true);
4443 /* We first translate all modules to make sure that later parts
4444 of the program can use the decl. Then we translate the nonmodules. */
4446 for (; !errors
&& gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4448 if (!gfc_current_ns
->proc_name
4449 || gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4452 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4453 gfc_derived_types
= gfc_current_ns
->derived_types
;
4454 gfc_generate_module_code (gfc_current_ns
);
4455 gfc_current_ns
->translated
= 1;
4458 gfc_current_ns
= gfc_global_ns_list
;
4459 for (; !errors
&& gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4461 if (gfc_current_ns
->proc_name
4462 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
4465 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4466 gfc_derived_types
= gfc_current_ns
->derived_types
;
4467 gfc_generate_code (gfc_current_ns
);
4468 gfc_current_ns
->translated
= 1;
4471 /* Clean up all the namespaces after translation. */
4472 gfc_current_ns
= gfc_global_ns_list
;
4473 for (;gfc_current_ns
;)
4477 if (gfc_current_ns
->proc_name
4478 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
4480 gfc_current_ns
= gfc_current_ns
->sibling
;
4484 ns
= gfc_current_ns
->sibling
;
4485 gfc_derived_types
= gfc_current_ns
->derived_types
;
4487 gfc_current_ns
= ns
;
4490 clean_up_modules (gfc_gsym_root
);
4494 /* Top level parser. */
4497 gfc_parse_file (void)
4499 int seen_program
, errors_before
, errors
;
4500 gfc_state_data top
, s
;
4503 gfc_namespace
*next
;
4505 gfc_start_source_files ();
4507 top
.state
= COMP_NONE
;
4509 top
.previous
= NULL
;
4510 top
.head
= top
.tail
= NULL
;
4511 top
.do_variable
= NULL
;
4513 gfc_state_stack
= &top
;
4515 gfc_clear_new_st ();
4517 gfc_statement_label
= NULL
;
4519 if (setjmp (eof_buf
))
4520 return FAILURE
; /* Come here on unexpected EOF */
4522 /* Prepare the global namespace that will contain the
4524 gfc_global_ns_list
= next
= NULL
;
4529 /* Exit early for empty files. */
4535 st
= next_statement ();
4544 goto duplicate_main
;
4546 prog_locus
= gfc_current_locus
;
4548 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
4549 main_program_symbol(gfc_current_ns
, gfc_new_block
->name
);
4550 accept_statement (st
);
4551 add_global_program ();
4552 parse_progunit (ST_NONE
);
4553 if (gfc_option
.flag_whole_file
)
4558 add_global_procedure (1);
4559 push_state (&s
, COMP_SUBROUTINE
, gfc_new_block
);
4560 accept_statement (st
);
4561 parse_progunit (ST_NONE
);
4562 if (gfc_option
.flag_whole_file
)
4567 add_global_procedure (0);
4568 push_state (&s
, COMP_FUNCTION
, gfc_new_block
);
4569 accept_statement (st
);
4570 parse_progunit (ST_NONE
);
4571 if (gfc_option
.flag_whole_file
)
4576 push_state (&s
, COMP_BLOCK_DATA
, gfc_new_block
);
4577 accept_statement (st
);
4578 parse_block_data ();
4582 push_state (&s
, COMP_MODULE
, gfc_new_block
);
4583 accept_statement (st
);
4585 gfc_get_errors (NULL
, &errors_before
);
4589 /* Anything else starts a nameless main program block. */
4592 goto duplicate_main
;
4594 prog_locus
= gfc_current_locus
;
4596 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
4597 main_program_symbol (gfc_current_ns
, "MAIN__");
4598 parse_progunit (st
);
4599 if (gfc_option
.flag_whole_file
)
4604 /* Handle the non-program units. */
4605 gfc_current_ns
->code
= s
.head
;
4607 gfc_resolve (gfc_current_ns
);
4609 /* Dump the parse tree if requested. */
4610 if (gfc_option
.dump_fortran_original
)
4611 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
4613 gfc_get_errors (NULL
, &errors
);
4614 if (s
.state
== COMP_MODULE
)
4616 gfc_dump_module (s
.sym
->name
, errors_before
== errors
);
4617 if (!gfc_option
.flag_whole_file
)
4620 gfc_generate_module_code (gfc_current_ns
);
4626 gfc_current_ns
->derived_types
= gfc_derived_types
;
4627 gfc_derived_types
= NULL
;
4634 gfc_generate_code (gfc_current_ns
);
4642 /* The main program and non-contained procedures are put
4643 in the global namespace list, so that they can be processed
4644 later and all their interfaces resolved. */
4645 gfc_current_ns
->code
= s
.head
;
4648 for (; next
->sibling
; next
= next
->sibling
)
4650 next
->sibling
= gfc_current_ns
;
4653 gfc_global_ns_list
= gfc_current_ns
;
4655 next
= gfc_current_ns
;
4662 if (!gfc_option
.flag_whole_file
)
4665 /* Do the resolution. */
4666 resolve_all_program_units (gfc_global_ns_list
);
4668 /* Do the parse tree dump. */
4670 = gfc_option
.dump_fortran_original
? gfc_global_ns_list
: NULL
;
4672 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4673 if (!gfc_current_ns
->proc_name
4674 || gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4676 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
4677 fputs ("------------------------------------------\n\n", stdout
);
4680 /* Do the translation. */
4681 translate_all_program_units (gfc_global_ns_list
, seen_program
);
4685 gfc_end_source_files ();
4689 /* If we see a duplicate main program, shut down. If the second
4690 instance is an implied main program, i.e. data decls or executable
4691 statements, we're in for lots of errors. */
4692 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus
);
4693 reject_statement ();