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
->ts
.u
.derived
->attr
.alloc_comp
))
2201 sym
->attr
.alloc_comp
= 1;
2204 /* Look for pointer components. */
2206 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2207 && CLASS_DATA (c
)->attr
.class_pointer
)
2208 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.pointer_comp
))
2211 sym
->attr
.pointer_comp
= 1;
2214 /* Look for procedure pointer components. */
2215 if (c
->attr
.proc_pointer
2216 || (c
->ts
.type
== BT_DERIVED
2217 && c
->ts
.u
.derived
->attr
.proc_pointer_comp
))
2218 sym
->attr
.proc_pointer_comp
= 1;
2220 /* Looking for coarray components. */
2221 if (c
->attr
.codimension
2222 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2223 && CLASS_DATA (c
)->attr
.codimension
))
2226 sym
->attr
.coarray_comp
= 1;
2229 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.coarray_comp
)
2232 if (!pointer
&& !allocatable
)
2233 sym
->attr
.coarray_comp
= 1;
2236 /* Looking for lock_type components. */
2237 if ((c
->ts
.type
== BT_DERIVED
2238 && c
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
2239 && c
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
)
2240 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2241 && CLASS_DATA (c
)->ts
.u
.derived
->from_intmod
2242 == INTMOD_ISO_FORTRAN_ENV
2243 && CLASS_DATA (c
)->ts
.u
.derived
->intmod_sym_id
2244 == ISOFORTRAN_LOCK_TYPE
)
2245 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.lock_comp
2246 && !allocatable
&& !pointer
))
2250 sym
->attr
.lock_comp
= 1;
2253 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2254 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2255 unless there are nondirect [allocatable or pointer] components
2256 involved (cf. 1.3.33.1 and 1.3.33.3). */
2258 if (pointer
&& !coarray
&& lock_type
)
2259 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2260 "codimension or be a subcomponent of a coarray, "
2261 "which is not possible as the component has the "
2262 "pointer attribute", c
->name
, &c
->loc
);
2263 else if (pointer
&& !coarray
&& c
->ts
.type
== BT_DERIVED
2264 && c
->ts
.u
.derived
->attr
.lock_comp
)
2265 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2266 "of type LOCK_TYPE, which must have a codimension or be a "
2267 "subcomponent of a coarray", c
->name
, &c
->loc
);
2269 if (lock_type
&& allocatable
&& !coarray
)
2270 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2271 "a codimension", c
->name
, &c
->loc
);
2272 else if (lock_type
&& allocatable
&& c
->ts
.type
== BT_DERIVED
2273 && c
->ts
.u
.derived
->attr
.lock_comp
)
2274 gfc_error ("Allocatable component %s at %L must have a codimension as "
2275 "it has a noncoarray subcomponent of type LOCK_TYPE",
2278 if (sym
->attr
.coarray_comp
&& !coarray
&& lock_type
)
2279 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2280 "subcomponent of type LOCK_TYPE must have a codimension or "
2281 "be a subcomponent of a coarray. (Variables of type %s may "
2282 "not have a codimension as already a coarray "
2283 "subcomponent exists)", c
->name
, &c
->loc
, sym
->name
);
2285 if (sym
->attr
.lock_comp
&& coarray
&& !lock_type
)
2286 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2287 "subcomponent of type LOCK_TYPE must have a codimension or "
2288 "be a subcomponent of a coarray. (Variables of type %s may "
2289 "not have a codimension as %s at %L has a codimension or a "
2290 "coarray subcomponent)", lock_comp
->name
, &lock_comp
->loc
,
2291 sym
->name
, c
->name
, &c
->loc
);
2293 /* Look for private components. */
2294 if (sym
->component_access
== ACCESS_PRIVATE
2295 || c
->attr
.access
== ACCESS_PRIVATE
2296 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.private_comp
))
2297 sym
->attr
.private_comp
= 1;
2300 if (!seen_component
)
2301 sym
->attr
.zero_comp
= 1;
2307 /* Parse an ENUM. */
2315 int seen_enumerator
= 0;
2317 push_state (&s
, COMP_ENUM
, gfc_new_block
);
2321 while (compiling_enum
)
2323 st
= next_statement ();
2331 seen_enumerator
= 1;
2332 accept_statement (st
);
2337 if (!seen_enumerator
)
2338 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2339 accept_statement (st
);
2343 gfc_free_enum_history ();
2344 unexpected_statement (st
);
2352 /* Parse an interface. We must be able to deal with the possibility
2353 of recursive interfaces. The parse_spec() subroutine is mutually
2354 recursive with parse_interface(). */
2356 static gfc_statement
parse_spec (gfc_statement
);
2359 parse_interface (void)
2361 gfc_compile_state new_state
= COMP_NONE
, current_state
;
2362 gfc_symbol
*prog_unit
, *sym
;
2363 gfc_interface_info save
;
2364 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 proc_locus
= gfc_current_locus
;
2458 /* Read data declaration statements. */
2459 st
= parse_spec (ST_NONE
);
2461 /* Since the interface block does not permit an IMPLICIT statement,
2462 the default type for the function or the result must be taken
2463 from the formal namespace. */
2464 if (new_state
== COMP_FUNCTION
)
2466 if (prog_unit
->result
== prog_unit
2467 && prog_unit
->ts
.type
== BT_UNKNOWN
)
2468 gfc_set_default_type (prog_unit
, 1, prog_unit
->formal_ns
);
2469 else if (prog_unit
->result
!= prog_unit
2470 && prog_unit
->result
->ts
.type
== BT_UNKNOWN
)
2471 gfc_set_default_type (prog_unit
->result
, 1,
2472 prog_unit
->formal_ns
);
2475 if (st
!= ST_END_SUBROUTINE
&& st
!= ST_END_FUNCTION
)
2477 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2478 gfc_ascii_statement (st
));
2479 reject_statement ();
2483 /* Add EXTERNAL attribute to function or subroutine. */
2484 if (current_interface
.type
!= INTERFACE_ABSTRACT
&& !prog_unit
->attr
.dummy
)
2485 gfc_add_external (&prog_unit
->attr
, &gfc_current_locus
);
2487 current_interface
= save
;
2488 gfc_add_interface (prog_unit
);
2491 if (current_interface
.ns
2492 && current_interface
.ns
->proc_name
2493 && strcmp (current_interface
.ns
->proc_name
->name
,
2494 prog_unit
->name
) == 0)
2495 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2496 "enclosing procedure", prog_unit
->name
, &proc_locus
);
2505 /* Associate function characteristics by going back to the function
2506 declaration and rematching the prefix. */
2509 match_deferred_characteristics (gfc_typespec
* ts
)
2512 match m
= MATCH_ERROR
;
2513 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2515 loc
= gfc_current_locus
;
2517 gfc_current_locus
= gfc_current_block ()->declared_at
;
2520 gfc_buffer_error (1);
2521 m
= gfc_match_prefix (ts
);
2522 gfc_buffer_error (0);
2524 if (ts
->type
== BT_DERIVED
)
2532 /* Only permit one go at the characteristic association. */
2536 /* Set the function locus correctly. If we have not found the
2537 function name, there is an error. */
2539 && gfc_match ("function% %n", name
) == MATCH_YES
2540 && strcmp (name
, gfc_current_block ()->name
) == 0)
2542 gfc_current_block ()->declared_at
= gfc_current_locus
;
2543 gfc_commit_symbols ();
2548 gfc_undo_symbols ();
2551 gfc_current_locus
=loc
;
2556 /* Check specification-expressions in the function result of the currently
2557 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2558 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2559 scope are not yet parsed so this has to be delayed up to parse_spec. */
2562 check_function_result_typed (void)
2564 gfc_typespec
* ts
= &gfc_current_ns
->proc_name
->result
->ts
;
2566 gcc_assert (gfc_current_state () == COMP_FUNCTION
);
2567 gcc_assert (ts
->type
!= BT_UNKNOWN
);
2569 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2570 /* TODO: Extend when KIND type parameters are implemented. */
2571 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
)
2572 gfc_expr_check_typed (ts
->u
.cl
->length
, gfc_current_ns
, true);
2576 /* Parse a set of specification statements. Returns the statement
2577 that doesn't fit. */
2579 static gfc_statement
2580 parse_spec (gfc_statement st
)
2583 bool function_result_typed
= false;
2584 bool bad_characteristic
= false;
2587 verify_st_order (&ss
, ST_NONE
, false);
2589 st
= next_statement ();
2591 /* If we are not inside a function or don't have a result specified so far,
2592 do nothing special about it. */
2593 if (gfc_current_state () != COMP_FUNCTION
)
2594 function_result_typed
= true;
2597 gfc_symbol
* proc
= gfc_current_ns
->proc_name
;
2600 if (proc
->result
->ts
.type
== BT_UNKNOWN
)
2601 function_result_typed
= true;
2606 /* If we're inside a BLOCK construct, some statements are disallowed.
2607 Check this here. Attribute declaration statements like INTENT, OPTIONAL
2608 or VALUE are also disallowed, but they don't have a particular ST_*
2609 key so we have to check for them individually in their matcher routine. */
2610 if (gfc_current_state () == COMP_BLOCK
)
2614 case ST_IMPLICIT_NONE
:
2617 case ST_EQUIVALENCE
:
2618 case ST_STATEMENT_FUNCTION
:
2619 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
2620 gfc_ascii_statement (st
));
2621 reject_statement ();
2628 /* If we find a statement that can not be followed by an IMPLICIT statement
2629 (and thus we can expect to see none any further), type the function result
2630 if it has not yet been typed. Be careful not to give the END statement
2631 to verify_st_order! */
2632 if (!function_result_typed
&& st
!= ST_GET_FCN_CHARACTERISTICS
)
2634 bool verify_now
= false;
2636 if (st
== ST_END_FUNCTION
|| st
== ST_CONTAINS
)
2641 verify_st_order (&dummyss
, ST_NONE
, false);
2642 verify_st_order (&dummyss
, st
, false);
2644 if (verify_st_order (&dummyss
, ST_IMPLICIT
, true) == FAILURE
)
2650 check_function_result_typed ();
2651 function_result_typed
= true;
2660 case ST_IMPLICIT_NONE
:
2662 if (!function_result_typed
)
2664 check_function_result_typed ();
2665 function_result_typed
= true;
2671 case ST_DATA
: /* Not allowed in interfaces */
2672 if (gfc_current_state () == COMP_INTERFACE
)
2682 case ST_DERIVED_DECL
:
2685 if (verify_st_order (&ss
, st
, false) == FAILURE
)
2687 reject_statement ();
2688 st
= next_statement ();
2698 case ST_DERIVED_DECL
:
2704 if (gfc_current_state () != COMP_MODULE
)
2706 gfc_error ("%s statement must appear in a MODULE",
2707 gfc_ascii_statement (st
));
2708 reject_statement ();
2712 if (gfc_current_ns
->default_access
!= ACCESS_UNKNOWN
)
2714 gfc_error ("%s statement at %C follows another accessibility "
2715 "specification", gfc_ascii_statement (st
));
2716 reject_statement ();
2720 gfc_current_ns
->default_access
= (st
== ST_PUBLIC
)
2721 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
2725 case ST_STATEMENT_FUNCTION
:
2726 if (gfc_current_state () == COMP_MODULE
)
2728 unexpected_statement (st
);
2736 accept_statement (st
);
2737 st
= next_statement ();
2741 accept_statement (st
);
2743 st
= next_statement ();
2746 case ST_GET_FCN_CHARACTERISTICS
:
2747 /* This statement triggers the association of a function's result
2749 ts
= &gfc_current_block ()->result
->ts
;
2750 if (match_deferred_characteristics (ts
) != MATCH_YES
)
2751 bad_characteristic
= true;
2753 st
= next_statement ();
2760 /* If match_deferred_characteristics failed, then there is an error. */
2761 if (bad_characteristic
)
2763 ts
= &gfc_current_block ()->result
->ts
;
2764 if (ts
->type
!= BT_DERIVED
)
2765 gfc_error ("Bad kind expression for function '%s' at %L",
2766 gfc_current_block ()->name
,
2767 &gfc_current_block ()->declared_at
);
2769 gfc_error ("The type for function '%s' at %L is not accessible",
2770 gfc_current_block ()->name
,
2771 &gfc_current_block ()->declared_at
);
2773 gfc_current_block ()->ts
.kind
= 0;
2774 /* Keep the derived type; if it's bad, it will be discovered later. */
2775 if (!(ts
->type
== BT_DERIVED
&& ts
->u
.derived
))
2776 ts
->type
= BT_UNKNOWN
;
2783 /* Parse a WHERE block, (not a simple WHERE statement). */
2786 parse_where_block (void)
2788 int seen_empty_else
;
2793 accept_statement (ST_WHERE_BLOCK
);
2794 top
= gfc_state_stack
->tail
;
2796 push_state (&s
, COMP_WHERE
, gfc_new_block
);
2798 d
= add_statement ();
2799 d
->expr1
= top
->expr1
;
2805 seen_empty_else
= 0;
2809 st
= next_statement ();
2815 case ST_WHERE_BLOCK
:
2816 parse_where_block ();
2821 accept_statement (st
);
2825 if (seen_empty_else
)
2827 gfc_error ("ELSEWHERE statement at %C follows previous "
2828 "unmasked ELSEWHERE");
2829 reject_statement ();
2833 if (new_st
.expr1
== NULL
)
2834 seen_empty_else
= 1;
2836 d
= new_level (gfc_state_stack
->head
);
2838 d
->expr1
= new_st
.expr1
;
2840 accept_statement (st
);
2845 accept_statement (st
);
2849 gfc_error ("Unexpected %s statement in WHERE block at %C",
2850 gfc_ascii_statement (st
));
2851 reject_statement ();
2855 while (st
!= ST_END_WHERE
);
2861 /* Parse a FORALL block (not a simple FORALL statement). */
2864 parse_forall_block (void)
2870 accept_statement (ST_FORALL_BLOCK
);
2871 top
= gfc_state_stack
->tail
;
2873 push_state (&s
, COMP_FORALL
, gfc_new_block
);
2875 d
= add_statement ();
2876 d
->op
= EXEC_FORALL
;
2881 st
= next_statement ();
2886 case ST_POINTER_ASSIGNMENT
:
2889 accept_statement (st
);
2892 case ST_WHERE_BLOCK
:
2893 parse_where_block ();
2896 case ST_FORALL_BLOCK
:
2897 parse_forall_block ();
2901 accept_statement (st
);
2908 gfc_error ("Unexpected %s statement in FORALL block at %C",
2909 gfc_ascii_statement (st
));
2911 reject_statement ();
2915 while (st
!= ST_END_FORALL
);
2921 static gfc_statement
parse_executable (gfc_statement
);
2923 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2926 parse_if_block (void)
2935 accept_statement (ST_IF_BLOCK
);
2937 top
= gfc_state_stack
->tail
;
2938 push_state (&s
, COMP_IF
, gfc_new_block
);
2940 new_st
.op
= EXEC_IF
;
2941 d
= add_statement ();
2943 d
->expr1
= top
->expr1
;
2949 st
= parse_executable (ST_NONE
);
2959 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2960 "statement at %L", &else_locus
);
2962 reject_statement ();
2966 d
= new_level (gfc_state_stack
->head
);
2968 d
->expr1
= new_st
.expr1
;
2970 accept_statement (st
);
2977 gfc_error ("Duplicate ELSE statements at %L and %C",
2979 reject_statement ();
2984 else_locus
= gfc_current_locus
;
2986 d
= new_level (gfc_state_stack
->head
);
2989 accept_statement (st
);
2997 unexpected_statement (st
);
3001 while (st
!= ST_ENDIF
);
3004 accept_statement (st
);
3008 /* Parse a SELECT block. */
3011 parse_select_block (void)
3017 accept_statement (ST_SELECT_CASE
);
3019 cp
= gfc_state_stack
->tail
;
3020 push_state (&s
, COMP_SELECT
, gfc_new_block
);
3022 /* Make sure that the next statement is a CASE or END SELECT. */
3025 st
= next_statement ();
3028 if (st
== ST_END_SELECT
)
3030 /* Empty SELECT CASE is OK. */
3031 accept_statement (st
);
3038 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
3041 reject_statement ();
3044 /* At this point, we're got a nonempty select block. */
3045 cp
= new_level (cp
);
3048 accept_statement (st
);
3052 st
= parse_executable (ST_NONE
);
3059 cp
= new_level (gfc_state_stack
->head
);
3061 gfc_clear_new_st ();
3063 accept_statement (st
);
3069 /* Can't have an executable statement because of
3070 parse_executable(). */
3072 unexpected_statement (st
);
3076 while (st
!= ST_END_SELECT
);
3079 accept_statement (st
);
3083 /* Pop the current selector from the SELECT TYPE stack. */
3086 select_type_pop (void)
3088 gfc_select_type_stack
*old
= select_type_stack
;
3089 select_type_stack
= old
->prev
;
3094 /* Parse a SELECT TYPE construct (F03:R821). */
3097 parse_select_type_block (void)
3103 accept_statement (ST_SELECT_TYPE
);
3105 cp
= gfc_state_stack
->tail
;
3106 push_state (&s
, COMP_SELECT_TYPE
, gfc_new_block
);
3108 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
3112 st
= next_statement ();
3115 if (st
== ST_END_SELECT
)
3116 /* Empty SELECT CASE is OK. */
3118 if (st
== ST_TYPE_IS
|| st
== ST_CLASS_IS
)
3121 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
3122 "following SELECT TYPE at %C");
3124 reject_statement ();
3127 /* At this point, we're got a nonempty select block. */
3128 cp
= new_level (cp
);
3131 accept_statement (st
);
3135 st
= parse_executable (ST_NONE
);
3143 cp
= new_level (gfc_state_stack
->head
);
3145 gfc_clear_new_st ();
3147 accept_statement (st
);
3153 /* Can't have an executable statement because of
3154 parse_executable(). */
3156 unexpected_statement (st
);
3160 while (st
!= ST_END_SELECT
);
3164 accept_statement (st
);
3165 gfc_current_ns
= gfc_current_ns
->parent
;
3170 /* Given a symbol, make sure it is not an iteration variable for a DO
3171 statement. This subroutine is called when the symbol is seen in a
3172 context that causes it to become redefined. If the symbol is an
3173 iterator, we generate an error message and return nonzero. */
3176 gfc_check_do_variable (gfc_symtree
*st
)
3180 for (s
=gfc_state_stack
; s
; s
= s
->previous
)
3181 if (s
->do_variable
== st
)
3183 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
3184 "loop beginning at %L", st
->name
, &s
->head
->loc
);
3192 /* Checks to see if the current statement label closes an enddo.
3193 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3194 an error) if it incorrectly closes an ENDDO. */
3197 check_do_closure (void)
3201 if (gfc_statement_label
== NULL
)
3204 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
3205 if (p
->state
== COMP_DO
|| p
->state
== COMP_DO_CONCURRENT
)
3209 return 0; /* No loops to close */
3211 if (p
->ext
.end_do_label
== gfc_statement_label
)
3213 if (p
== gfc_state_stack
)
3216 gfc_error ("End of nonblock DO statement at %C is within another block");
3220 /* At this point, the label doesn't terminate the innermost loop.
3221 Make sure it doesn't terminate another one. */
3222 for (; p
; p
= p
->previous
)
3223 if ((p
->state
== COMP_DO
|| p
->state
== COMP_DO_CONCURRENT
)
3224 && p
->ext
.end_do_label
== gfc_statement_label
)
3226 gfc_error ("End of nonblock DO statement at %C is interwoven "
3227 "with another DO loop");
3235 /* Parse a series of contained program units. */
3237 static void parse_progunit (gfc_statement
);
3240 /* Parse a CRITICAL block. */
3243 parse_critical_block (void)
3249 s
.ext
.end_do_label
= new_st
.label1
;
3251 accept_statement (ST_CRITICAL
);
3252 top
= gfc_state_stack
->tail
;
3254 push_state (&s
, COMP_CRITICAL
, gfc_new_block
);
3256 d
= add_statement ();
3257 d
->op
= EXEC_CRITICAL
;
3262 st
= parse_executable (ST_NONE
);
3270 case ST_END_CRITICAL
:
3271 if (s
.ext
.end_do_label
!= NULL
3272 && s
.ext
.end_do_label
!= gfc_statement_label
)
3273 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3274 "match CRITICAL label");
3276 if (gfc_statement_label
!= NULL
)
3278 new_st
.op
= EXEC_NOP
;
3284 unexpected_statement (st
);
3288 while (st
!= ST_END_CRITICAL
);
3291 accept_statement (st
);
3295 /* Set up the local namespace for a BLOCK construct. */
3298 gfc_build_block_ns (gfc_namespace
*parent_ns
)
3300 gfc_namespace
* my_ns
;
3301 static int numblock
= 1;
3303 my_ns
= gfc_get_namespace (parent_ns
, 1);
3304 my_ns
->construct_entities
= 1;
3306 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3307 code generation (so it must not be NULL).
3308 We set its recursive argument if our container procedure is recursive, so
3309 that local variables are accordingly placed on the stack when it
3310 will be necessary. */
3312 my_ns
->proc_name
= gfc_new_block
;
3316 char buffer
[20]; /* Enough to hold "block@2147483648\n". */
3318 snprintf(buffer
, sizeof(buffer
), "block@%d", numblock
++);
3319 gfc_get_symbol (buffer
, my_ns
, &my_ns
->proc_name
);
3320 t
= gfc_add_flavor (&my_ns
->proc_name
->attr
, FL_LABEL
,
3321 my_ns
->proc_name
->name
, NULL
);
3322 gcc_assert (t
== SUCCESS
);
3323 gfc_commit_symbol (my_ns
->proc_name
);
3326 if (parent_ns
->proc_name
)
3327 my_ns
->proc_name
->attr
.recursive
= parent_ns
->proc_name
->attr
.recursive
;
3333 /* Parse a BLOCK construct. */
3336 parse_block_construct (void)
3338 gfc_namespace
* my_ns
;
3341 gfc_notify_std (GFC_STD_F2008
, "BLOCK construct at %C");
3343 my_ns
= gfc_build_block_ns (gfc_current_ns
);
3345 new_st
.op
= EXEC_BLOCK
;
3346 new_st
.ext
.block
.ns
= my_ns
;
3347 new_st
.ext
.block
.assoc
= NULL
;
3348 accept_statement (ST_BLOCK
);
3350 push_state (&s
, COMP_BLOCK
, my_ns
->proc_name
);
3351 gfc_current_ns
= my_ns
;
3353 parse_progunit (ST_NONE
);
3355 gfc_current_ns
= gfc_current_ns
->parent
;
3360 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3361 behind the scenes with compiler-generated variables. */
3364 parse_associate (void)
3366 gfc_namespace
* my_ns
;
3369 gfc_association_list
* a
;
3371 gfc_notify_std (GFC_STD_F2003
, "ASSOCIATE construct at %C");
3373 my_ns
= gfc_build_block_ns (gfc_current_ns
);
3375 new_st
.op
= EXEC_BLOCK
;
3376 new_st
.ext
.block
.ns
= my_ns
;
3377 gcc_assert (new_st
.ext
.block
.assoc
);
3379 /* Add all associate-names as BLOCK variables. Creating them is enough
3380 for now, they'll get their values during trans-* phase. */
3381 gfc_current_ns
= my_ns
;
3382 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
3386 if (gfc_get_sym_tree (a
->name
, NULL
, &a
->st
, false))
3390 sym
->attr
.flavor
= FL_VARIABLE
;
3392 sym
->declared_at
= a
->where
;
3393 gfc_set_sym_referenced (sym
);
3395 /* Initialize the typespec. It is not available in all cases,
3396 however, as it may only be set on the target during resolution.
3397 Still, sometimes it helps to have it right now -- especially
3398 for parsing component references on the associate-name
3399 in case of association to a derived-type. */
3400 sym
->ts
= a
->target
->ts
;
3403 accept_statement (ST_ASSOCIATE
);
3404 push_state (&s
, COMP_ASSOCIATE
, my_ns
->proc_name
);
3407 st
= parse_executable (ST_NONE
);
3414 accept_statement (st
);
3415 my_ns
->code
= gfc_state_stack
->head
;
3419 unexpected_statement (st
);
3423 gfc_current_ns
= gfc_current_ns
->parent
;
3428 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
3429 handled inside of parse_executable(), because they aren't really
3433 parse_do_block (void)
3442 s
.ext
.end_do_label
= new_st
.label1
;
3444 if (new_st
.ext
.iterator
!= NULL
)
3445 stree
= new_st
.ext
.iterator
->var
->symtree
;
3449 accept_statement (ST_DO
);
3451 top
= gfc_state_stack
->tail
;
3452 push_state (&s
, do_op
== EXEC_DO_CONCURRENT
? COMP_DO_CONCURRENT
: COMP_DO
,
3455 s
.do_variable
= stree
;
3457 top
->block
= new_level (top
);
3458 top
->block
->op
= EXEC_DO
;
3461 st
= parse_executable (ST_NONE
);
3469 if (s
.ext
.end_do_label
!= NULL
3470 && s
.ext
.end_do_label
!= gfc_statement_label
)
3471 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
3474 if (gfc_statement_label
!= NULL
)
3476 new_st
.op
= EXEC_NOP
;
3481 case ST_IMPLIED_ENDDO
:
3482 /* If the do-stmt of this DO construct has a do-construct-name,
3483 the corresponding end-do must be an end-do-stmt (with a matching
3484 name, but in that case we must have seen ST_ENDDO first).
3485 We only complain about this in pedantic mode. */
3486 if (gfc_current_block () != NULL
)
3487 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
3488 &gfc_current_block()->declared_at
);
3493 unexpected_statement (st
);
3498 accept_statement (st
);
3502 /* Parse the statements of OpenMP do/parallel do. */
3504 static gfc_statement
3505 parse_omp_do (gfc_statement omp_st
)
3511 accept_statement (omp_st
);
3513 cp
= gfc_state_stack
->tail
;
3514 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3515 np
= new_level (cp
);
3521 st
= next_statement ();
3524 else if (st
== ST_DO
)
3527 unexpected_statement (st
);
3531 if (gfc_statement_label
!= NULL
3532 && gfc_state_stack
->previous
!= NULL
3533 && gfc_state_stack
->previous
->state
== COMP_DO
3534 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
3542 there should be no !$OMP END DO. */
3544 return ST_IMPLIED_ENDDO
;
3547 check_do_closure ();
3550 st
= next_statement ();
3551 if (st
== (omp_st
== ST_OMP_DO
? ST_OMP_END_DO
: ST_OMP_END_PARALLEL_DO
))
3553 if (new_st
.op
== EXEC_OMP_END_NOWAIT
)
3554 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
3556 gcc_assert (new_st
.op
== EXEC_NOP
);
3557 gfc_clear_new_st ();
3558 gfc_commit_symbols ();
3559 gfc_warning_check ();
3560 st
= next_statement ();
3566 /* Parse the statements of OpenMP atomic directive. */
3568 static gfc_statement
3569 parse_omp_atomic (void)
3576 accept_statement (ST_OMP_ATOMIC
);
3578 cp
= gfc_state_stack
->tail
;
3579 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3580 np
= new_level (cp
);
3583 count
= 1 + (cp
->ext
.omp_atomic
== GFC_OMP_ATOMIC_CAPTURE
);
3587 st
= next_statement ();
3590 else if (st
== ST_ASSIGNMENT
)
3592 accept_statement (st
);
3596 unexpected_statement (st
);
3601 st
= next_statement ();
3602 if (st
== ST_OMP_END_ATOMIC
)
3604 gfc_clear_new_st ();
3605 gfc_commit_symbols ();
3606 gfc_warning_check ();
3607 st
= next_statement ();
3609 else if (cp
->ext
.omp_atomic
== GFC_OMP_ATOMIC_CAPTURE
)
3610 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
3615 /* Parse the statements of an OpenMP structured block. */
3618 parse_omp_structured_block (gfc_statement omp_st
, bool workshare_stmts_only
)
3620 gfc_statement st
, omp_end_st
;
3624 accept_statement (omp_st
);
3626 cp
= gfc_state_stack
->tail
;
3627 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3628 np
= new_level (cp
);
3634 case ST_OMP_PARALLEL
:
3635 omp_end_st
= ST_OMP_END_PARALLEL
;
3637 case ST_OMP_PARALLEL_SECTIONS
:
3638 omp_end_st
= ST_OMP_END_PARALLEL_SECTIONS
;
3640 case ST_OMP_SECTIONS
:
3641 omp_end_st
= ST_OMP_END_SECTIONS
;
3643 case ST_OMP_ORDERED
:
3644 omp_end_st
= ST_OMP_END_ORDERED
;
3646 case ST_OMP_CRITICAL
:
3647 omp_end_st
= ST_OMP_END_CRITICAL
;
3650 omp_end_st
= ST_OMP_END_MASTER
;
3653 omp_end_st
= ST_OMP_END_SINGLE
;
3656 omp_end_st
= ST_OMP_END_TASK
;
3658 case ST_OMP_WORKSHARE
:
3659 omp_end_st
= ST_OMP_END_WORKSHARE
;
3661 case ST_OMP_PARALLEL_WORKSHARE
:
3662 omp_end_st
= ST_OMP_END_PARALLEL_WORKSHARE
;
3670 if (workshare_stmts_only
)
3672 /* Inside of !$omp workshare, only
3675 where statements and constructs
3676 forall statements and constructs
3680 are allowed. For !$omp critical these
3681 restrictions apply recursively. */
3684 st
= next_statement ();
3695 accept_statement (st
);
3698 case ST_WHERE_BLOCK
:
3699 parse_where_block ();
3702 case ST_FORALL_BLOCK
:
3703 parse_forall_block ();
3706 case ST_OMP_PARALLEL
:
3707 case ST_OMP_PARALLEL_SECTIONS
:
3708 parse_omp_structured_block (st
, false);
3711 case ST_OMP_PARALLEL_WORKSHARE
:
3712 case ST_OMP_CRITICAL
:
3713 parse_omp_structured_block (st
, true);
3716 case ST_OMP_PARALLEL_DO
:
3717 st
= parse_omp_do (st
);
3721 st
= parse_omp_atomic ();
3732 st
= next_statement ();
3736 st
= parse_executable (ST_NONE
);
3739 else if (st
== ST_OMP_SECTION
3740 && (omp_st
== ST_OMP_SECTIONS
3741 || omp_st
== ST_OMP_PARALLEL_SECTIONS
))
3743 np
= new_level (np
);
3747 else if (st
!= omp_end_st
)
3748 unexpected_statement (st
);
3750 while (st
!= omp_end_st
);
3754 case EXEC_OMP_END_NOWAIT
:
3755 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
3757 case EXEC_OMP_CRITICAL
:
3758 if (((cp
->ext
.omp_name
== NULL
) ^ (new_st
.ext
.omp_name
== NULL
))
3759 || (new_st
.ext
.omp_name
!= NULL
3760 && strcmp (cp
->ext
.omp_name
, new_st
.ext
.omp_name
) != 0))
3761 gfc_error ("Name after !$omp critical and !$omp end critical does "
3763 free (CONST_CAST (char *, new_st
.ext
.omp_name
));
3765 case EXEC_OMP_END_SINGLE
:
3766 cp
->ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
]
3767 = new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
];
3768 new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
] = NULL
;
3769 gfc_free_omp_clauses (new_st
.ext
.omp_clauses
);
3777 gfc_clear_new_st ();
3778 gfc_commit_symbols ();
3779 gfc_warning_check ();
3784 /* Accept a series of executable statements. We return the first
3785 statement that doesn't fit to the caller. Any block statements are
3786 passed on to the correct handler, which usually passes the buck
3789 static gfc_statement
3790 parse_executable (gfc_statement st
)
3795 st
= next_statement ();
3799 close_flag
= check_do_closure ();
3804 case ST_END_PROGRAM
:
3807 case ST_END_FUNCTION
:
3812 case ST_END_SUBROUTINE
:
3817 case ST_SELECT_CASE
:
3818 gfc_error ("%s statement at %C cannot terminate a non-block "
3819 "DO loop", gfc_ascii_statement (st
));
3832 gfc_notify_std (GFC_STD_F95_OBS
, "DATA statement at %C after the "
3833 "first executable statement");
3839 accept_statement (st
);
3840 if (close_flag
== 1)
3841 return ST_IMPLIED_ENDDO
;
3845 parse_block_construct ();
3856 case ST_SELECT_CASE
:
3857 parse_select_block ();
3860 case ST_SELECT_TYPE
:
3861 parse_select_type_block();
3866 if (check_do_closure () == 1)
3867 return ST_IMPLIED_ENDDO
;
3871 parse_critical_block ();
3874 case ST_WHERE_BLOCK
:
3875 parse_where_block ();
3878 case ST_FORALL_BLOCK
:
3879 parse_forall_block ();
3882 case ST_OMP_PARALLEL
:
3883 case ST_OMP_PARALLEL_SECTIONS
:
3884 case ST_OMP_SECTIONS
:
3885 case ST_OMP_ORDERED
:
3886 case ST_OMP_CRITICAL
:
3890 parse_omp_structured_block (st
, false);
3893 case ST_OMP_WORKSHARE
:
3894 case ST_OMP_PARALLEL_WORKSHARE
:
3895 parse_omp_structured_block (st
, true);
3899 case ST_OMP_PARALLEL_DO
:
3900 st
= parse_omp_do (st
);
3901 if (st
== ST_IMPLIED_ENDDO
)
3906 st
= parse_omp_atomic ();
3913 st
= next_statement ();
3918 /* Fix the symbols for sibling functions. These are incorrectly added to
3919 the child namespace as the parser didn't know about this procedure. */
3922 gfc_fixup_sibling_symbols (gfc_symbol
*sym
, gfc_namespace
*siblings
)
3926 gfc_symbol
*old_sym
;
3928 sym
->attr
.referenced
= 1;
3929 for (ns
= siblings
; ns
; ns
= ns
->sibling
)
3931 st
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
3933 if (!st
|| (st
->n
.sym
->attr
.dummy
&& ns
== st
->n
.sym
->ns
))
3934 goto fixup_contained
;
3936 if ((st
->n
.sym
->attr
.flavor
== FL_DERIVED
3937 && sym
->attr
.generic
&& sym
->attr
.function
)
3938 ||(sym
->attr
.flavor
== FL_DERIVED
3939 && st
->n
.sym
->attr
.generic
&& st
->n
.sym
->attr
.function
))
3940 goto fixup_contained
;
3942 old_sym
= st
->n
.sym
;
3943 if (old_sym
->ns
== ns
3944 && !old_sym
->attr
.contained
3946 /* By 14.6.1.3, host association should be excluded
3947 for the following. */
3948 && !(old_sym
->attr
.external
3949 || (old_sym
->ts
.type
!= BT_UNKNOWN
3950 && !old_sym
->attr
.implicit_type
)
3951 || old_sym
->attr
.flavor
== FL_PARAMETER
3952 || old_sym
->attr
.use_assoc
3953 || old_sym
->attr
.in_common
3954 || old_sym
->attr
.in_equivalence
3955 || old_sym
->attr
.data
3956 || old_sym
->attr
.dummy
3957 || old_sym
->attr
.result
3958 || old_sym
->attr
.dimension
3959 || old_sym
->attr
.allocatable
3960 || old_sym
->attr
.intrinsic
3961 || old_sym
->attr
.generic
3962 || old_sym
->attr
.flavor
== FL_NAMELIST
3963 || old_sym
->attr
.flavor
== FL_LABEL
3964 || old_sym
->attr
.proc
== PROC_ST_FUNCTION
))
3966 /* Replace it with the symbol from the parent namespace. */
3970 gfc_release_symbol (old_sym
);
3974 /* Do the same for any contained procedures. */
3975 gfc_fixup_sibling_symbols (sym
, ns
->contained
);
3980 parse_contained (int module
)
3982 gfc_namespace
*ns
, *parent_ns
, *tmp
;
3983 gfc_state_data s1
, s2
;
3987 int contains_statements
= 0;
3990 push_state (&s1
, COMP_CONTAINS
, NULL
);
3991 parent_ns
= gfc_current_ns
;
3995 gfc_current_ns
= gfc_get_namespace (parent_ns
, 1);
3997 gfc_current_ns
->sibling
= parent_ns
->contained
;
3998 parent_ns
->contained
= gfc_current_ns
;
4001 /* Process the next available statement. We come here if we got an error
4002 and rejected the last statement. */
4003 st
= next_statement ();
4012 contains_statements
= 1;
4013 accept_statement (st
);
4016 (st
== ST_FUNCTION
) ? COMP_FUNCTION
: COMP_SUBROUTINE
,
4019 /* For internal procedures, create/update the symbol in the
4020 parent namespace. */
4024 if (gfc_get_symbol (gfc_new_block
->name
, parent_ns
, &sym
))
4025 gfc_error ("Contained procedure '%s' at %C is already "
4026 "ambiguous", gfc_new_block
->name
);
4029 if (gfc_add_procedure (&sym
->attr
, PROC_INTERNAL
, sym
->name
,
4030 &gfc_new_block
->declared_at
) ==
4033 if (st
== ST_FUNCTION
)
4034 gfc_add_function (&sym
->attr
, sym
->name
,
4035 &gfc_new_block
->declared_at
);
4037 gfc_add_subroutine (&sym
->attr
, sym
->name
,
4038 &gfc_new_block
->declared_at
);
4042 gfc_commit_symbols ();
4045 sym
= gfc_new_block
;
4047 /* Mark this as a contained function, so it isn't replaced
4048 by other module functions. */
4049 sym
->attr
.contained
= 1;
4050 sym
->attr
.referenced
= 1;
4052 /* Set implicit_pure so that it can be reset if any of the
4053 tests for purity fail. This is used for some optimisation
4054 during translation. */
4055 if (!sym
->attr
.pure
)
4056 sym
->attr
.implicit_pure
= 1;
4058 parse_progunit (ST_NONE
);
4060 /* Fix up any sibling functions that refer to this one. */
4061 gfc_fixup_sibling_symbols (sym
, gfc_current_ns
);
4062 /* Or refer to any of its alternate entry points. */
4063 for (el
= gfc_current_ns
->entries
; el
; el
= el
->next
)
4064 gfc_fixup_sibling_symbols (el
->sym
, gfc_current_ns
);
4066 gfc_current_ns
->code
= s2
.head
;
4067 gfc_current_ns
= parent_ns
;
4072 /* These statements are associated with the end of the host unit. */
4073 case ST_END_FUNCTION
:
4075 case ST_END_PROGRAM
:
4076 case ST_END_SUBROUTINE
:
4077 accept_statement (st
);
4081 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
4082 gfc_ascii_statement (st
));
4083 reject_statement ();
4089 while (st
!= ST_END_FUNCTION
&& st
!= ST_END_SUBROUTINE
4090 && st
!= ST_END_MODULE
&& st
!= ST_END_PROGRAM
);
4092 /* The first namespace in the list is guaranteed to not have
4093 anything (worthwhile) in it. */
4094 tmp
= gfc_current_ns
;
4095 gfc_current_ns
= parent_ns
;
4096 if (seen_error
&& tmp
->refs
> 1)
4097 gfc_free_namespace (tmp
);
4099 ns
= gfc_current_ns
->contained
;
4100 gfc_current_ns
->contained
= ns
->sibling
;
4101 gfc_free_namespace (ns
);
4104 if (!contains_statements
)
4105 gfc_notify_std (GFC_STD_F2008
, "CONTAINS statement without "
4106 "FUNCTION or SUBROUTINE statement at %C");
4110 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
4113 parse_progunit (gfc_statement st
)
4118 st
= parse_spec (st
);
4125 /* This is not allowed within BLOCK! */
4126 if (gfc_current_state () != COMP_BLOCK
)
4131 accept_statement (st
);
4138 if (gfc_current_state () == COMP_FUNCTION
)
4139 gfc_check_function_type (gfc_current_ns
);
4144 st
= parse_executable (st
);
4152 /* This is not allowed within BLOCK! */
4153 if (gfc_current_state () != COMP_BLOCK
)
4158 accept_statement (st
);
4165 unexpected_statement (st
);
4166 reject_statement ();
4167 st
= next_statement ();
4173 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
4174 if (p
->state
== COMP_CONTAINS
)
4177 if (gfc_find_state (COMP_MODULE
) == SUCCESS
)
4182 gfc_error ("CONTAINS statement at %C is already in a contained "
4184 reject_statement ();
4185 st
= next_statement ();
4189 parse_contained (0);
4192 gfc_current_ns
->code
= gfc_state_stack
->head
;
4196 /* Come here to complain about a global symbol already in use as
4200 gfc_global_used (gfc_gsymbol
*sym
, locus
*where
)
4205 where
= &gfc_current_locus
;
4215 case GSYM_SUBROUTINE
:
4216 name
= "SUBROUTINE";
4221 case GSYM_BLOCK_DATA
:
4222 name
= "BLOCK DATA";
4228 gfc_internal_error ("gfc_global_used(): Bad type");
4232 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
4233 sym
->name
, where
, name
, &sym
->where
);
4237 /* Parse a block data program unit. */
4240 parse_block_data (void)
4243 static locus blank_locus
;
4244 static int blank_block
=0;
4247 gfc_current_ns
->proc_name
= gfc_new_block
;
4248 gfc_current_ns
->is_block_data
= 1;
4250 if (gfc_new_block
== NULL
)
4253 gfc_error ("Blank BLOCK DATA at %C conflicts with "
4254 "prior BLOCK DATA at %L", &blank_locus
);
4258 blank_locus
= gfc_current_locus
;
4263 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4265 || (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_BLOCK_DATA
))
4266 gfc_global_used(s
, NULL
);
4269 s
->type
= GSYM_BLOCK_DATA
;
4270 s
->where
= gfc_current_locus
;
4275 st
= parse_spec (ST_NONE
);
4277 while (st
!= ST_END_BLOCK_DATA
)
4279 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
4280 gfc_ascii_statement (st
));
4281 reject_statement ();
4282 st
= next_statement ();
4287 /* Parse a module subprogram. */
4295 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4296 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_MODULE
))
4297 gfc_global_used(s
, NULL
);
4300 s
->type
= GSYM_MODULE
;
4301 s
->where
= gfc_current_locus
;
4305 st
= parse_spec (ST_NONE
);
4314 parse_contained (1);
4318 accept_statement (st
);
4322 gfc_error ("Unexpected %s statement in MODULE at %C",
4323 gfc_ascii_statement (st
));
4325 reject_statement ();
4326 st
= next_statement ();
4330 s
->ns
= gfc_current_ns
;
4334 /* Add a procedure name to the global symbol table. */
4337 add_global_procedure (int sub
)
4341 s
= gfc_get_gsymbol(gfc_new_block
->name
);
4344 || (s
->type
!= GSYM_UNKNOWN
4345 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
4346 gfc_global_used(s
, NULL
);
4349 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
4350 s
->where
= gfc_current_locus
;
4352 s
->ns
= gfc_current_ns
;
4357 /* Add a program to the global symbol table. */
4360 add_global_program (void)
4364 if (gfc_new_block
== NULL
)
4366 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4368 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_PROGRAM
))
4369 gfc_global_used(s
, NULL
);
4372 s
->type
= GSYM_PROGRAM
;
4373 s
->where
= gfc_current_locus
;
4375 s
->ns
= gfc_current_ns
;
4380 /* Resolve all the program units when whole file scope option
4383 resolve_all_program_units (gfc_namespace
*gfc_global_ns_list
)
4385 gfc_free_dt_list ();
4386 gfc_current_ns
= gfc_global_ns_list
;
4387 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4389 if (gfc_current_ns
->proc_name
4390 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
4391 continue; /* Already resolved. */
4393 if (gfc_current_ns
->proc_name
)
4394 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4395 gfc_resolve (gfc_current_ns
);
4396 gfc_current_ns
->derived_types
= gfc_derived_types
;
4397 gfc_derived_types
= NULL
;
4403 clean_up_modules (gfc_gsymbol
*gsym
)
4408 clean_up_modules (gsym
->left
);
4409 clean_up_modules (gsym
->right
);
4411 if (gsym
->type
!= GSYM_MODULE
|| !gsym
->ns
)
4414 gfc_current_ns
= gsym
->ns
;
4415 gfc_derived_types
= gfc_current_ns
->derived_types
;
4422 /* Translate all the program units when whole file scope option
4423 is active. This could be in a different order to resolution if
4424 there are forward references in the file. */
4426 translate_all_program_units (gfc_namespace
*gfc_global_ns_list
,
4431 gfc_current_ns
= gfc_global_ns_list
;
4432 gfc_get_errors (NULL
, &errors
);
4434 /* If the main program is in the translation unit and we have
4435 -fcoarray=libs, generate the static variables. */
4436 if (gfc_option
.coarray
== GFC_FCOARRAY_LIB
&& main_in_tu
)
4437 gfc_init_coarray_decl (true);
4439 /* We first translate all modules to make sure that later parts
4440 of the program can use the decl. Then we translate the nonmodules. */
4442 for (; !errors
&& gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4444 if (!gfc_current_ns
->proc_name
4445 || gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4448 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4449 gfc_derived_types
= gfc_current_ns
->derived_types
;
4450 gfc_generate_module_code (gfc_current_ns
);
4451 gfc_current_ns
->translated
= 1;
4454 gfc_current_ns
= gfc_global_ns_list
;
4455 for (; !errors
&& gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4457 if (gfc_current_ns
->proc_name
4458 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
4461 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4462 gfc_derived_types
= gfc_current_ns
->derived_types
;
4463 gfc_generate_code (gfc_current_ns
);
4464 gfc_current_ns
->translated
= 1;
4467 /* Clean up all the namespaces after translation. */
4468 gfc_current_ns
= gfc_global_ns_list
;
4469 for (;gfc_current_ns
;)
4473 if (gfc_current_ns
->proc_name
4474 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
4476 gfc_current_ns
= gfc_current_ns
->sibling
;
4480 ns
= gfc_current_ns
->sibling
;
4481 gfc_derived_types
= gfc_current_ns
->derived_types
;
4483 gfc_current_ns
= ns
;
4486 clean_up_modules (gfc_gsym_root
);
4490 /* Top level parser. */
4493 gfc_parse_file (void)
4495 int seen_program
, errors_before
, errors
;
4496 gfc_state_data top
, s
;
4499 gfc_namespace
*next
;
4501 gfc_start_source_files ();
4503 top
.state
= COMP_NONE
;
4505 top
.previous
= NULL
;
4506 top
.head
= top
.tail
= NULL
;
4507 top
.do_variable
= NULL
;
4509 gfc_state_stack
= &top
;
4511 gfc_clear_new_st ();
4513 gfc_statement_label
= NULL
;
4515 if (setjmp (eof_buf
))
4516 return FAILURE
; /* Come here on unexpected EOF */
4518 /* Prepare the global namespace that will contain the
4520 gfc_global_ns_list
= next
= NULL
;
4525 /* Exit early for empty files. */
4531 st
= next_statement ();
4540 goto duplicate_main
;
4542 prog_locus
= gfc_current_locus
;
4544 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
4545 main_program_symbol(gfc_current_ns
, gfc_new_block
->name
);
4546 accept_statement (st
);
4547 add_global_program ();
4548 parse_progunit (ST_NONE
);
4549 if (gfc_option
.flag_whole_file
)
4554 add_global_procedure (1);
4555 push_state (&s
, COMP_SUBROUTINE
, gfc_new_block
);
4556 accept_statement (st
);
4557 parse_progunit (ST_NONE
);
4558 if (gfc_option
.flag_whole_file
)
4563 add_global_procedure (0);
4564 push_state (&s
, COMP_FUNCTION
, gfc_new_block
);
4565 accept_statement (st
);
4566 parse_progunit (ST_NONE
);
4567 if (gfc_option
.flag_whole_file
)
4572 push_state (&s
, COMP_BLOCK_DATA
, gfc_new_block
);
4573 accept_statement (st
);
4574 parse_block_data ();
4578 push_state (&s
, COMP_MODULE
, gfc_new_block
);
4579 accept_statement (st
);
4581 gfc_get_errors (NULL
, &errors_before
);
4585 /* Anything else starts a nameless main program block. */
4588 goto duplicate_main
;
4590 prog_locus
= gfc_current_locus
;
4592 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
4593 main_program_symbol (gfc_current_ns
, "MAIN__");
4594 parse_progunit (st
);
4595 if (gfc_option
.flag_whole_file
)
4600 /* Handle the non-program units. */
4601 gfc_current_ns
->code
= s
.head
;
4603 gfc_resolve (gfc_current_ns
);
4605 /* Dump the parse tree if requested. */
4606 if (gfc_option
.dump_fortran_original
)
4607 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
4609 gfc_get_errors (NULL
, &errors
);
4610 if (s
.state
== COMP_MODULE
)
4612 gfc_dump_module (s
.sym
->name
, errors_before
== errors
);
4613 if (!gfc_option
.flag_whole_file
)
4616 gfc_generate_module_code (gfc_current_ns
);
4622 gfc_current_ns
->derived_types
= gfc_derived_types
;
4623 gfc_derived_types
= NULL
;
4630 gfc_generate_code (gfc_current_ns
);
4638 /* The main program and non-contained procedures are put
4639 in the global namespace list, so that they can be processed
4640 later and all their interfaces resolved. */
4641 gfc_current_ns
->code
= s
.head
;
4644 for (; next
->sibling
; next
= next
->sibling
)
4646 next
->sibling
= gfc_current_ns
;
4649 gfc_global_ns_list
= gfc_current_ns
;
4651 next
= gfc_current_ns
;
4658 if (!gfc_option
.flag_whole_file
)
4661 /* Do the resolution. */
4662 resolve_all_program_units (gfc_global_ns_list
);
4664 /* Do the parse tree dump. */
4666 = gfc_option
.dump_fortran_original
? gfc_global_ns_list
: NULL
;
4668 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4669 if (!gfc_current_ns
->proc_name
4670 || gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4672 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
4673 fputs ("------------------------------------------\n\n", stdout
);
4676 /* Do the translation. */
4677 translate_all_program_units (gfc_global_ns_list
, seen_program
);
4681 gfc_end_source_files ();
4685 /* If we see a duplicate main program, shut down. If the second
4686 instance is an implied main program, i.e. data decls or executable
4687 statements, we're in for lots of errors. */
4688 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus
);
4689 reject_statement ();