2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
30 /* Current statement label. Zero means no statement label. Because new_st
31 can get wiped during statement matching, we have to keep it separate. */
33 gfc_st_label
*gfc_statement_label
;
35 static locus label_locus
;
36 static jmp_buf eof_buf
;
38 gfc_state_data
*gfc_state_stack
;
39 static bool last_was_use_stmt
= false;
41 /* TODO: Re-order functions to kill these forward decls. */
42 static void check_statement_label (gfc_statement
);
43 static void undo_new_statement (void);
44 static void reject_statement (void);
47 /* A sort of half-matching function. We try to match the word on the
48 input with the passed string. If this succeeds, we call the
49 keyword-dependent matching function that will match the rest of the
50 statement. For single keywords, the matching subroutine is
54 match_word (const char *str
, match (*subr
) (void), locus
*old_locus
)
69 gfc_current_locus
= *old_locus
;
77 /* Like match_word, but if str is matched, set a flag that it
80 match_word_omp_simd (const char *str
, match (*subr
) (void), locus
*old_locus
,
97 gfc_current_locus
= *old_locus
;
105 /* Load symbols from all USE statements encountered in this scoping unit. */
110 gfc_error_buffer old_error
;
112 gfc_push_error (&old_error
);
113 gfc_buffer_error (false);
115 gfc_buffer_error (true);
116 gfc_pop_error (&old_error
);
117 gfc_commit_symbols ();
118 gfc_warning_check ();
119 gfc_current_ns
->old_cl_list
= gfc_current_ns
->cl_list
;
120 gfc_current_ns
->old_equiv
= gfc_current_ns
->equiv
;
121 gfc_current_ns
->old_data
= gfc_current_ns
->data
;
122 last_was_use_stmt
= false;
126 /* Figure out what the next statement is, (mostly) regardless of
127 proper ordering. The do...while(0) is there to prevent if/else
130 #define match(keyword, subr, st) \
132 if (match_word (keyword, subr, &old_locus) == MATCH_YES) \
135 undo_new_statement (); \
139 /* This is a specialist version of decode_statement that is used
140 for the specification statements in a function, whose
141 characteristics are deferred into the specification statements.
142 eg.: INTEGER (king = mykind) foo ()
143 USE mymodule, ONLY mykind.....
144 The KIND parameter needs a return after USE or IMPORT, whereas
145 derived type declarations can occur anywhere, up the executable
146 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
147 out of the correct kind of specification statements. */
149 decode_specification_statement (void)
155 if (gfc_match_eos () == MATCH_YES
)
158 old_locus
= gfc_current_locus
;
160 if (match_word ("use", gfc_match_use
, &old_locus
) == MATCH_YES
)
162 last_was_use_stmt
= true;
167 undo_new_statement ();
168 if (last_was_use_stmt
)
172 match ("import", gfc_match_import
, ST_IMPORT
);
174 if (gfc_current_block ()->result
->ts
.type
!= BT_DERIVED
)
177 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
178 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
179 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
181 /* General statement matching: Instead of testing every possible
182 statement, we eliminate most possibilities by peeking at the
185 c
= gfc_peek_ascii_char ();
190 match ("abstract% interface", gfc_match_abstract_interface
,
192 match ("allocatable", gfc_match_allocatable
, ST_ATTR_DECL
);
193 match ("asynchronous", gfc_match_asynchronous
, ST_ATTR_DECL
);
197 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
201 match ("codimension", gfc_match_codimension
, ST_ATTR_DECL
);
202 match ("contiguous", gfc_match_contiguous
, ST_ATTR_DECL
);
206 match ("data", gfc_match_data
, ST_DATA
);
207 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
211 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
212 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
213 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
214 match ("external", gfc_match_external
, ST_ATTR_DECL
);
218 match ("format", gfc_match_format
, ST_FORMAT
);
225 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
226 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
227 match ("interface", gfc_match_interface
, ST_INTERFACE
);
228 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
229 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
236 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
240 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
244 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
245 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
246 if (gfc_match_private (&st
) == MATCH_YES
)
248 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
249 if (gfc_match_public (&st
) == MATCH_YES
)
251 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
258 match ("save", gfc_match_save
, ST_ATTR_DECL
);
262 match ("target", gfc_match_target
, ST_ATTR_DECL
);
263 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
270 match ("value", gfc_match_value
, ST_ATTR_DECL
);
271 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
278 /* This is not a specification statement. See if any of the matchers
279 has stored an error message of some sort. */
283 gfc_buffer_error (false);
284 gfc_current_locus
= old_locus
;
286 return ST_GET_FCN_CHARACTERISTICS
;
289 static bool in_specification_block
;
291 /* This is the primary 'decode_statement'. */
293 decode_statement (void)
301 gfc_enforce_clean_symbol_state ();
303 gfc_clear_error (); /* Clear any pending errors. */
304 gfc_clear_warning (); /* Clear any pending warnings. */
306 gfc_matching_function
= false;
308 if (gfc_match_eos () == MATCH_YES
)
311 if (gfc_current_state () == COMP_FUNCTION
312 && gfc_current_block ()->result
->ts
.kind
== -1)
313 return decode_specification_statement ();
315 old_locus
= gfc_current_locus
;
317 c
= gfc_peek_ascii_char ();
321 if (match_word ("use", gfc_match_use
, &old_locus
) == MATCH_YES
)
323 last_was_use_stmt
= true;
327 undo_new_statement ();
330 if (last_was_use_stmt
)
333 /* Try matching a data declaration or function declaration. The
334 input "REALFUNCTIONA(N)" can mean several things in different
335 contexts, so it (and its relatives) get special treatment. */
337 if (gfc_current_state () == COMP_NONE
338 || gfc_current_state () == COMP_INTERFACE
339 || gfc_current_state () == COMP_CONTAINS
)
341 gfc_matching_function
= true;
342 m
= gfc_match_function_decl ();
345 else if (m
== MATCH_ERROR
)
349 gfc_current_locus
= old_locus
;
351 gfc_matching_function
= false;
354 /* Match statements whose error messages are meant to be overwritten
355 by something better. */
357 match (NULL
, gfc_match_assignment
, ST_ASSIGNMENT
);
358 match (NULL
, gfc_match_pointer_assignment
, ST_POINTER_ASSIGNMENT
);
360 if (in_specification_block
)
362 m
= match_word (NULL
, gfc_match_st_function
, &old_locus
);
364 return ST_STATEMENT_FUNCTION
;
367 if (!(in_specification_block
&& m
== MATCH_ERROR
))
369 match (NULL
, gfc_match_ptr_fcn_assign
, ST_ASSIGNMENT
);
372 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
373 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
375 /* Try to match a subroutine statement, which has the same optional
376 prefixes that functions can have. */
378 if (gfc_match_subroutine () == MATCH_YES
)
379 return ST_SUBROUTINE
;
381 gfc_current_locus
= old_locus
;
383 if (gfc_match_submod_proc () == MATCH_YES
)
385 if (gfc_new_block
->attr
.subroutine
)
386 return ST_SUBROUTINE
;
387 else if (gfc_new_block
->attr
.function
)
391 gfc_current_locus
= old_locus
;
393 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
394 statements, which might begin with a block label. The match functions for
395 these statements are unusual in that their keyword is not seen before
396 the matcher is called. */
398 if (gfc_match_if (&st
) == MATCH_YES
)
401 gfc_current_locus
= old_locus
;
403 if (gfc_match_where (&st
) == MATCH_YES
)
406 gfc_current_locus
= old_locus
;
408 if (gfc_match_forall (&st
) == MATCH_YES
)
411 gfc_current_locus
= old_locus
;
413 match (NULL
, gfc_match_do
, ST_DO
);
414 match (NULL
, gfc_match_block
, ST_BLOCK
);
415 match (NULL
, gfc_match_associate
, ST_ASSOCIATE
);
416 match (NULL
, gfc_match_critical
, ST_CRITICAL
);
417 match (NULL
, gfc_match_select
, ST_SELECT_CASE
);
419 gfc_current_ns
= gfc_build_block_ns (gfc_current_ns
);
420 match (NULL
, gfc_match_select_type
, ST_SELECT_TYPE
);
422 gfc_current_ns
= gfc_current_ns
->parent
;
423 gfc_free_namespace (ns
);
425 /* General statement matching: Instead of testing every possible
426 statement, we eliminate most possibilities by peeking at the
432 match ("abstract% interface", gfc_match_abstract_interface
,
434 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
);
435 match ("allocatable", gfc_match_allocatable
, ST_ATTR_DECL
);
436 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
);
437 match ("asynchronous", gfc_match_asynchronous
, ST_ATTR_DECL
);
441 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
);
442 match ("block data", gfc_match_block_data
, ST_BLOCK_DATA
);
443 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
447 match ("call", gfc_match_call
, ST_CALL
);
448 match ("close", gfc_match_close
, ST_CLOSE
);
449 match ("continue", gfc_match_continue
, ST_CONTINUE
);
450 match ("contiguous", gfc_match_contiguous
, ST_ATTR_DECL
);
451 match ("cycle", gfc_match_cycle
, ST_CYCLE
);
452 match ("case", gfc_match_case
, ST_CASE
);
453 match ("common", gfc_match_common
, ST_COMMON
);
454 match ("contains", gfc_match_eos
, ST_CONTAINS
);
455 match ("class", gfc_match_class_is
, ST_CLASS_IS
);
456 match ("codimension", gfc_match_codimension
, ST_ATTR_DECL
);
460 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
);
461 match ("data", gfc_match_data
, ST_DATA
);
462 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
466 match ("end file", gfc_match_endfile
, ST_END_FILE
);
467 match ("exit", gfc_match_exit
, ST_EXIT
);
468 match ("else", gfc_match_else
, ST_ELSE
);
469 match ("else where", gfc_match_elsewhere
, ST_ELSEWHERE
);
470 match ("else if", gfc_match_elseif
, ST_ELSEIF
);
471 match ("error stop", gfc_match_error_stop
, ST_ERROR_STOP
);
472 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
474 if (gfc_match_end (&st
) == MATCH_YES
)
477 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
478 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
479 match ("external", gfc_match_external
, ST_ATTR_DECL
);
480 match ("event post", gfc_match_event_post
, ST_EVENT_POST
);
481 match ("event wait", gfc_match_event_wait
, ST_EVENT_WAIT
);
485 match ("final", gfc_match_final_decl
, ST_FINAL
);
486 match ("flush", gfc_match_flush
, ST_FLUSH
);
487 match ("format", gfc_match_format
, ST_FORMAT
);
491 match ("generic", gfc_match_generic
, ST_GENERIC
);
492 match ("go to", gfc_match_goto
, ST_GOTO
);
496 match ("inquire", gfc_match_inquire
, ST_INQUIRE
);
497 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
498 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
499 match ("import", gfc_match_import
, ST_IMPORT
);
500 match ("interface", gfc_match_interface
, ST_INTERFACE
);
501 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
502 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
506 match ("lock", gfc_match_lock
, ST_LOCK
);
510 match ("module% procedure", gfc_match_modproc
, ST_MODULE_PROC
);
511 match ("module", gfc_match_module
, ST_MODULE
);
515 match ("nullify", gfc_match_nullify
, ST_NULLIFY
);
516 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
520 match ("open", gfc_match_open
, ST_OPEN
);
521 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
525 match ("print", gfc_match_print
, ST_WRITE
);
526 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
527 match ("pause", gfc_match_pause
, ST_PAUSE
);
528 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
529 if (gfc_match_private (&st
) == MATCH_YES
)
531 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
532 match ("program", gfc_match_program
, ST_PROGRAM
);
533 if (gfc_match_public (&st
) == MATCH_YES
)
535 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
539 match ("read", gfc_match_read
, ST_READ
);
540 match ("return", gfc_match_return
, ST_RETURN
);
541 match ("rewind", gfc_match_rewind
, ST_REWIND
);
545 match ("sequence", gfc_match_eos
, ST_SEQUENCE
);
546 match ("stop", gfc_match_stop
, ST_STOP
);
547 match ("save", gfc_match_save
, ST_ATTR_DECL
);
548 match ("submodule", gfc_match_submodule
, ST_SUBMODULE
);
549 match ("sync all", gfc_match_sync_all
, ST_SYNC_ALL
);
550 match ("sync images", gfc_match_sync_images
, ST_SYNC_IMAGES
);
551 match ("sync memory", gfc_match_sync_memory
, ST_SYNC_MEMORY
);
555 match ("target", gfc_match_target
, ST_ATTR_DECL
);
556 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
557 match ("type is", gfc_match_type_is
, ST_TYPE_IS
);
561 match ("unlock", gfc_match_unlock
, ST_UNLOCK
);
565 match ("value", gfc_match_value
, ST_ATTR_DECL
);
566 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
570 match ("wait", gfc_match_wait
, ST_WAIT
);
571 match ("write", gfc_match_write
, ST_WRITE
);
575 /* All else has failed, so give up. See if any of the matchers has
576 stored an error message of some sort. */
578 if (!gfc_error_check ())
579 gfc_error_now ("Unclassifiable statement at %C");
583 gfc_error_recovery ();
588 /* Like match, but set a flag simd_matched if keyword matched. */
589 #define matchs(keyword, subr, st) \
591 if (match_word_omp_simd (keyword, subr, &old_locus, \
592 &simd_matched) == MATCH_YES) \
595 undo_new_statement (); \
598 /* Like match, but don't match anything if not -fopenmp. */
599 #define matcho(keyword, subr, st) \
603 else if (match_word (keyword, subr, &old_locus) \
607 undo_new_statement (); \
611 decode_oacc_directive (void)
616 gfc_enforce_clean_symbol_state ();
618 gfc_clear_error (); /* Clear any pending errors. */
619 gfc_clear_warning (); /* Clear any pending warnings. */
623 gfc_error_now ("OpenACC directives at %C may not appear in PURE "
625 gfc_error_recovery ();
629 gfc_unset_implicit_pure (NULL
);
631 old_locus
= gfc_current_locus
;
633 /* General OpenACC directive matching: Instead of testing every possible
634 statement, we eliminate most possibilities by peeking at the
637 c
= gfc_peek_ascii_char ();
642 match ("atomic", gfc_match_oacc_atomic
, ST_OACC_ATOMIC
);
645 match ("cache", gfc_match_oacc_cache
, ST_OACC_CACHE
);
648 match ("data", gfc_match_oacc_data
, ST_OACC_DATA
);
649 match ("declare", gfc_match_oacc_declare
, ST_OACC_DECLARE
);
652 match ("end atomic", gfc_match_omp_eos
, ST_OACC_END_ATOMIC
);
653 match ("end data", gfc_match_omp_eos
, ST_OACC_END_DATA
);
654 match ("end host_data", gfc_match_omp_eos
, ST_OACC_END_HOST_DATA
);
655 match ("end kernels loop", gfc_match_omp_eos
, ST_OACC_END_KERNELS_LOOP
);
656 match ("end kernels", gfc_match_omp_eos
, ST_OACC_END_KERNELS
);
657 match ("end loop", gfc_match_omp_eos
, ST_OACC_END_LOOP
);
658 match ("end parallel loop", gfc_match_omp_eos
, ST_OACC_END_PARALLEL_LOOP
);
659 match ("end parallel", gfc_match_omp_eos
, ST_OACC_END_PARALLEL
);
660 match ("enter data", gfc_match_oacc_enter_data
, ST_OACC_ENTER_DATA
);
661 match ("exit data", gfc_match_oacc_exit_data
, ST_OACC_EXIT_DATA
);
664 match ("host_data", gfc_match_oacc_host_data
, ST_OACC_HOST_DATA
);
667 match ("parallel loop", gfc_match_oacc_parallel_loop
, ST_OACC_PARALLEL_LOOP
);
668 match ("parallel", gfc_match_oacc_parallel
, ST_OACC_PARALLEL
);
671 match ("kernels loop", gfc_match_oacc_kernels_loop
, ST_OACC_KERNELS_LOOP
);
672 match ("kernels", gfc_match_oacc_kernels
, ST_OACC_KERNELS
);
675 match ("loop", gfc_match_oacc_loop
, ST_OACC_LOOP
);
678 match ("routine", gfc_match_oacc_routine
, ST_OACC_ROUTINE
);
681 match ("update", gfc_match_oacc_update
, ST_OACC_UPDATE
);
684 match ("wait", gfc_match_oacc_wait
, ST_OACC_WAIT
);
688 /* Directive not found or stored an error message.
689 Check and give up. */
691 if (gfc_error_check () == 0)
692 gfc_error_now ("Unclassifiable OpenACC directive at %C");
696 gfc_error_recovery ();
702 decode_omp_directive (void)
706 bool simd_matched
= false;
708 gfc_enforce_clean_symbol_state ();
710 gfc_clear_error (); /* Clear any pending errors. */
711 gfc_clear_warning (); /* Clear any pending warnings. */
715 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
716 "or ELEMENTAL procedures");
717 gfc_error_recovery ();
721 gfc_unset_implicit_pure (NULL
);
723 old_locus
= gfc_current_locus
;
725 /* General OpenMP directive matching: Instead of testing every possible
726 statement, we eliminate most possibilities by peeking at the
729 c
= gfc_peek_ascii_char ();
731 /* match is for directives that should be recognized only if
732 -fopenmp, matchs for directives that should be recognized
733 if either -fopenmp or -fopenmp-simd. */
737 matcho ("atomic", gfc_match_omp_atomic
, ST_OMP_ATOMIC
);
740 matcho ("barrier", gfc_match_omp_barrier
, ST_OMP_BARRIER
);
743 matcho ("cancellation% point", gfc_match_omp_cancellation_point
,
744 ST_OMP_CANCELLATION_POINT
);
745 matcho ("cancel", gfc_match_omp_cancel
, ST_OMP_CANCEL
);
746 matcho ("critical", gfc_match_omp_critical
, ST_OMP_CRITICAL
);
749 matchs ("declare reduction", gfc_match_omp_declare_reduction
,
750 ST_OMP_DECLARE_REDUCTION
);
751 matchs ("declare simd", gfc_match_omp_declare_simd
,
752 ST_OMP_DECLARE_SIMD
);
753 matcho ("declare target", gfc_match_omp_declare_target
,
754 ST_OMP_DECLARE_TARGET
);
755 matchs ("distribute parallel do simd",
756 gfc_match_omp_distribute_parallel_do_simd
,
757 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
);
758 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do
,
759 ST_OMP_DISTRIBUTE_PARALLEL_DO
);
760 matchs ("distribute simd", gfc_match_omp_distribute_simd
,
761 ST_OMP_DISTRIBUTE_SIMD
);
762 matcho ("distribute", gfc_match_omp_distribute
, ST_OMP_DISTRIBUTE
);
763 matchs ("do simd", gfc_match_omp_do_simd
, ST_OMP_DO_SIMD
);
764 matcho ("do", gfc_match_omp_do
, ST_OMP_DO
);
767 matcho ("end atomic", gfc_match_omp_eos
, ST_OMP_END_ATOMIC
);
768 matcho ("end critical", gfc_match_omp_critical
, ST_OMP_END_CRITICAL
);
769 matchs ("end distribute parallel do simd", gfc_match_omp_eos
,
770 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD
);
771 matcho ("end distribute parallel do", gfc_match_omp_eos
,
772 ST_OMP_END_DISTRIBUTE_PARALLEL_DO
);
773 matchs ("end distribute simd", gfc_match_omp_eos
,
774 ST_OMP_END_DISTRIBUTE_SIMD
);
775 matcho ("end distribute", gfc_match_omp_eos
, ST_OMP_END_DISTRIBUTE
);
776 matchs ("end do simd", gfc_match_omp_end_nowait
, ST_OMP_END_DO_SIMD
);
777 matcho ("end do", gfc_match_omp_end_nowait
, ST_OMP_END_DO
);
778 matchs ("end simd", gfc_match_omp_eos
, ST_OMP_END_SIMD
);
779 matcho ("end master", gfc_match_omp_eos
, ST_OMP_END_MASTER
);
780 matcho ("end ordered", gfc_match_omp_eos
, ST_OMP_END_ORDERED
);
781 matchs ("end parallel do simd", gfc_match_omp_eos
,
782 ST_OMP_END_PARALLEL_DO_SIMD
);
783 matcho ("end parallel do", gfc_match_omp_eos
, ST_OMP_END_PARALLEL_DO
);
784 matcho ("end parallel sections", gfc_match_omp_eos
,
785 ST_OMP_END_PARALLEL_SECTIONS
);
786 matcho ("end parallel workshare", gfc_match_omp_eos
,
787 ST_OMP_END_PARALLEL_WORKSHARE
);
788 matcho ("end parallel", gfc_match_omp_eos
, ST_OMP_END_PARALLEL
);
789 matcho ("end sections", gfc_match_omp_end_nowait
, ST_OMP_END_SECTIONS
);
790 matcho ("end single", gfc_match_omp_end_single
, ST_OMP_END_SINGLE
);
791 matcho ("end target data", gfc_match_omp_eos
, ST_OMP_END_TARGET_DATA
);
792 matchs ("end target teams distribute parallel do simd",
794 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
);
795 matcho ("end target teams distribute parallel do", gfc_match_omp_eos
,
796 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
);
797 matchs ("end target teams distribute simd", gfc_match_omp_eos
,
798 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD
);
799 matcho ("end target teams distribute", gfc_match_omp_eos
,
800 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE
);
801 matcho ("end target teams", gfc_match_omp_eos
, ST_OMP_END_TARGET_TEAMS
);
802 matcho ("end target", gfc_match_omp_eos
, ST_OMP_END_TARGET
);
803 matcho ("end taskgroup", gfc_match_omp_eos
, ST_OMP_END_TASKGROUP
);
804 matcho ("end task", gfc_match_omp_eos
, ST_OMP_END_TASK
);
805 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos
,
806 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
);
807 matcho ("end teams distribute parallel do", gfc_match_omp_eos
,
808 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO
);
809 matchs ("end teams distribute simd", gfc_match_omp_eos
,
810 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD
);
811 matcho ("end teams distribute", gfc_match_omp_eos
,
812 ST_OMP_END_TEAMS_DISTRIBUTE
);
813 matcho ("end teams", gfc_match_omp_eos
, ST_OMP_END_TEAMS
);
814 matcho ("end workshare", gfc_match_omp_end_nowait
,
815 ST_OMP_END_WORKSHARE
);
818 matcho ("flush", gfc_match_omp_flush
, ST_OMP_FLUSH
);
821 matcho ("master", gfc_match_omp_master
, ST_OMP_MASTER
);
824 matcho ("ordered", gfc_match_omp_ordered
, ST_OMP_ORDERED
);
827 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd
,
828 ST_OMP_PARALLEL_DO_SIMD
);
829 matcho ("parallel do", gfc_match_omp_parallel_do
, ST_OMP_PARALLEL_DO
);
830 matcho ("parallel sections", gfc_match_omp_parallel_sections
,
831 ST_OMP_PARALLEL_SECTIONS
);
832 matcho ("parallel workshare", gfc_match_omp_parallel_workshare
,
833 ST_OMP_PARALLEL_WORKSHARE
);
834 matcho ("parallel", gfc_match_omp_parallel
, ST_OMP_PARALLEL
);
837 matcho ("sections", gfc_match_omp_sections
, ST_OMP_SECTIONS
);
838 matcho ("section", gfc_match_omp_eos
, ST_OMP_SECTION
);
839 matchs ("simd", gfc_match_omp_simd
, ST_OMP_SIMD
);
840 matcho ("single", gfc_match_omp_single
, ST_OMP_SINGLE
);
843 matcho ("target data", gfc_match_omp_target_data
, ST_OMP_TARGET_DATA
);
844 matchs ("target teams distribute parallel do simd",
845 gfc_match_omp_target_teams_distribute_parallel_do_simd
,
846 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
);
847 matcho ("target teams distribute parallel do",
848 gfc_match_omp_target_teams_distribute_parallel_do
,
849 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
);
850 matchs ("target teams distribute simd",
851 gfc_match_omp_target_teams_distribute_simd
,
852 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
);
853 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute
,
854 ST_OMP_TARGET_TEAMS_DISTRIBUTE
);
855 matcho ("target teams", gfc_match_omp_target_teams
, ST_OMP_TARGET_TEAMS
);
856 matcho ("target update", gfc_match_omp_target_update
,
857 ST_OMP_TARGET_UPDATE
);
858 matcho ("target", gfc_match_omp_target
, ST_OMP_TARGET
);
859 matcho ("taskgroup", gfc_match_omp_taskgroup
, ST_OMP_TASKGROUP
);
860 matcho ("taskwait", gfc_match_omp_taskwait
, ST_OMP_TASKWAIT
);
861 matcho ("taskyield", gfc_match_omp_taskyield
, ST_OMP_TASKYIELD
);
862 matcho ("task", gfc_match_omp_task
, ST_OMP_TASK
);
863 matchs ("teams distribute parallel do simd",
864 gfc_match_omp_teams_distribute_parallel_do_simd
,
865 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
);
866 matcho ("teams distribute parallel do",
867 gfc_match_omp_teams_distribute_parallel_do
,
868 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
);
869 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd
,
870 ST_OMP_TEAMS_DISTRIBUTE_SIMD
);
871 matcho ("teams distribute", gfc_match_omp_teams_distribute
,
872 ST_OMP_TEAMS_DISTRIBUTE
);
873 matcho ("teams", gfc_match_omp_teams
, ST_OMP_TEAMS
);
874 matcho ("threadprivate", gfc_match_omp_threadprivate
,
875 ST_OMP_THREADPRIVATE
);
878 matcho ("workshare", gfc_match_omp_workshare
, ST_OMP_WORKSHARE
);
882 /* All else has failed, so give up. See if any of the matchers has
883 stored an error message of some sort. Don't error out if
884 not -fopenmp and simd_matched is false, i.e. if a directive other
885 than one marked with match has been seen. */
887 if (flag_openmp
|| simd_matched
)
889 if (!gfc_error_check ())
890 gfc_error_now ("Unclassifiable OpenMP directive at %C");
895 gfc_error_recovery ();
901 decode_gcc_attribute (void)
905 gfc_enforce_clean_symbol_state ();
907 gfc_clear_error (); /* Clear any pending errors. */
908 gfc_clear_warning (); /* Clear any pending warnings. */
909 old_locus
= gfc_current_locus
;
911 match ("attributes", gfc_match_gcc_attributes
, ST_ATTR_DECL
);
913 /* All else has failed, so give up. See if any of the matchers has
914 stored an error message of some sort. */
916 if (!gfc_error_check ())
917 gfc_error_now ("Unclassifiable GCC directive at %C");
921 gfc_error_recovery ();
928 /* Assert next length characters to be equal to token in free form. */
931 verify_token_free (const char* token
, int length
, bool last_was_use_stmt
)
936 c
= gfc_next_ascii_char ();
937 for (i
= 0; i
< length
; i
++, c
= gfc_next_ascii_char ())
938 gcc_assert (c
== token
[i
]);
940 gcc_assert (gfc_is_whitespace(c
));
941 gfc_gobble_whitespace ();
942 if (last_was_use_stmt
)
946 /* Get the next statement in free form source. */
955 at_bol
= gfc_at_bol ();
956 gfc_gobble_whitespace ();
958 c
= gfc_peek_ascii_char ();
964 /* Found a statement label? */
965 m
= gfc_match_st_label (&gfc_statement_label
);
967 d
= gfc_peek_ascii_char ();
968 if (m
!= MATCH_YES
|| !gfc_is_whitespace (d
))
970 gfc_match_small_literal_int (&i
, &cnt
);
973 gfc_error_now ("Too many digits in statement label at %C");
976 gfc_error_now ("Zero is not a valid statement label at %C");
979 c
= gfc_next_ascii_char ();
982 if (!gfc_is_whitespace (c
))
983 gfc_error_now ("Non-numeric character in statement label at %C");
989 label_locus
= gfc_current_locus
;
991 gfc_gobble_whitespace ();
993 if (at_bol
&& gfc_peek_ascii_char () == ';')
995 gfc_error_now ("Semicolon at %C needs to be preceded by "
997 gfc_next_ascii_char (); /* Eat up the semicolon. */
1001 if (gfc_match_eos () == MATCH_YES
)
1003 gfc_warning_now (0, "Ignoring statement label in empty statement "
1004 "at %L", &label_locus
);
1005 gfc_free_st_label (gfc_statement_label
);
1006 gfc_statement_label
= NULL
;
1013 /* Comments have already been skipped by the time we get here,
1014 except for GCC attributes and OpenMP/OpenACC directives. */
1016 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
1017 c
= gfc_peek_ascii_char ();
1023 c
= gfc_next_ascii_char ();
1024 for (i
= 0; i
< 4; i
++, c
= gfc_next_ascii_char ())
1025 gcc_assert (c
== "gcc$"[i
]);
1027 gfc_gobble_whitespace ();
1028 return decode_gcc_attribute ();
1033 /* Since both OpenMP and OpenACC directives starts with
1034 !$ character sequence, we must check all flags combinations */
1035 if ((flag_openmp
|| flag_openmp_simd
)
1038 verify_token_free ("$omp", 4, last_was_use_stmt
);
1039 return decode_omp_directive ();
1041 else if ((flag_openmp
|| flag_openmp_simd
)
1044 gfc_next_ascii_char (); /* Eat up dollar character */
1045 c
= gfc_peek_ascii_char ();
1049 verify_token_free ("omp", 3, last_was_use_stmt
);
1050 return decode_omp_directive ();
1054 verify_token_free ("acc", 3, last_was_use_stmt
);
1055 return decode_oacc_directive ();
1058 else if (flag_openacc
)
1060 verify_token_free ("$acc", 4, last_was_use_stmt
);
1061 return decode_oacc_directive ();
1067 if (at_bol
&& c
== ';')
1069 if (!(gfc_option
.allow_std
& GFC_STD_F2008
))
1070 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1072 gfc_next_ascii_char (); /* Eat up the semicolon. */
1076 return decode_statement ();
1079 /* Assert next length characters to be equal to token in fixed form. */
1082 verify_token_fixed (const char *token
, int length
, bool last_was_use_stmt
)
1085 char c
= gfc_next_char_literal (NONSTRING
);
1087 for (i
= 0; i
< length
; i
++, c
= gfc_next_char_literal (NONSTRING
))
1088 gcc_assert ((char) gfc_wide_tolower (c
) == token
[i
]);
1090 if (c
!= ' ' && c
!= '0')
1092 gfc_buffer_error (false);
1093 gfc_error ("Bad continuation line at %C");
1096 if (last_was_use_stmt
)
1102 /* Get the next statement in fixed-form source. */
1104 static gfc_statement
1107 int label
, digit_flag
, i
;
1112 return decode_statement ();
1114 /* Skip past the current label field, parsing a statement label if
1115 one is there. This is a weird number parser, since the number is
1116 contained within five columns and can have any kind of embedded
1117 spaces. We also check for characters that make the rest of the
1123 for (i
= 0; i
< 5; i
++)
1125 c
= gfc_next_char_literal (NONSTRING
);
1142 label
= label
* 10 + ((unsigned char) c
- '0');
1143 label_locus
= gfc_current_locus
;
1147 /* Comments have already been skipped by the time we get
1148 here, except for GCC attributes and OpenMP directives. */
1151 c
= gfc_next_char_literal (NONSTRING
);
1153 if (TOLOWER (c
) == 'g')
1155 for (i
= 0; i
< 4; i
++, c
= gfc_next_char_literal (NONSTRING
))
1156 gcc_assert (TOLOWER (c
) == "gcc$"[i
]);
1158 return decode_gcc_attribute ();
1162 if ((flag_openmp
|| flag_openmp_simd
)
1165 if (!verify_token_fixed ("omp", 3, last_was_use_stmt
))
1167 return decode_omp_directive ();
1169 else if ((flag_openmp
|| flag_openmp_simd
)
1172 c
= gfc_next_char_literal(NONSTRING
);
1173 if (c
== 'o' || c
== 'O')
1175 if (!verify_token_fixed ("mp", 2, last_was_use_stmt
))
1177 return decode_omp_directive ();
1179 else if (c
== 'a' || c
== 'A')
1181 if (!verify_token_fixed ("cc", 2, last_was_use_stmt
))
1183 return decode_oacc_directive ();
1186 else if (flag_openacc
)
1188 if (!verify_token_fixed ("acc", 3, last_was_use_stmt
))
1190 return decode_oacc_directive ();
1195 /* Comments have already been skipped by the time we get
1196 here so don't bother checking for them. */
1199 gfc_buffer_error (false);
1200 gfc_error ("Non-numeric character in statement label at %C");
1208 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1211 /* We've found a valid statement label. */
1212 gfc_statement_label
= gfc_get_st_label (label
);
1216 /* Since this line starts a statement, it cannot be a continuation
1217 of a previous statement. If we see something here besides a
1218 space or zero, it must be a bad continuation line. */
1220 c
= gfc_next_char_literal (NONSTRING
);
1224 if (c
!= ' ' && c
!= '0')
1226 gfc_buffer_error (false);
1227 gfc_error ("Bad continuation line at %C");
1231 /* Now that we've taken care of the statement label columns, we have
1232 to make sure that the first nonblank character is not a '!'. If
1233 it is, the rest of the line is a comment. */
1237 loc
= gfc_current_locus
;
1238 c
= gfc_next_char_literal (NONSTRING
);
1240 while (gfc_is_whitespace (c
));
1244 gfc_current_locus
= loc
;
1249 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1250 else if (!(gfc_option
.allow_std
& GFC_STD_F2008
))
1251 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1256 if (gfc_match_eos () == MATCH_YES
)
1259 /* At this point, we've got a nonblank statement to parse. */
1260 return decode_statement ();
1264 gfc_warning_now (0, "Ignoring statement label in empty statement at %L",
1267 gfc_current_locus
.lb
->truncated
= 0;
1268 gfc_advance_line ();
1273 /* Return the next non-ST_NONE statement to the caller. We also worry
1274 about including files and the ends of include files at this stage. */
1276 static gfc_statement
1277 next_statement (void)
1282 gfc_enforce_clean_symbol_state ();
1284 gfc_new_block
= NULL
;
1286 gfc_current_ns
->old_cl_list
= gfc_current_ns
->cl_list
;
1287 gfc_current_ns
->old_equiv
= gfc_current_ns
->equiv
;
1288 gfc_current_ns
->old_data
= gfc_current_ns
->data
;
1291 gfc_statement_label
= NULL
;
1292 gfc_buffer_error (true);
1295 gfc_advance_line ();
1297 gfc_skip_comments ();
1305 if (gfc_define_undef_line ())
1308 old_locus
= gfc_current_locus
;
1310 st
= (gfc_current_form
== FORM_FIXED
) ? next_fixed () : next_free ();
1316 gfc_buffer_error (false);
1318 if (st
== ST_GET_FCN_CHARACTERISTICS
&& gfc_statement_label
!= NULL
)
1320 gfc_free_st_label (gfc_statement_label
);
1321 gfc_statement_label
= NULL
;
1322 gfc_current_locus
= old_locus
;
1326 check_statement_label (st
);
1332 /****************************** Parser ***********************************/
1334 /* The parser subroutines are of type 'try' that fail if the file ends
1337 /* Macros that expand to case-labels for various classes of
1338 statements. Start with executable statements that directly do
1341 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1342 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1343 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1344 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1345 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1346 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1347 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1348 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1349 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1350 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1351 case ST_OMP_TARGET_UPDATE: case ST_ERROR_STOP: case ST_SYNC_ALL: \
1352 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1353 case ST_EVENT_POST: case ST_EVENT_WAIT: \
1354 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1355 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1357 /* Statements that mark other executable statements. */
1359 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1360 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1361 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1362 case ST_OMP_PARALLEL: \
1363 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1364 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1365 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1366 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1367 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1368 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1369 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1370 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1371 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1372 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1373 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1374 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1375 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1376 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1377 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1378 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1379 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: \
1381 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1382 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: \
1383 case ST_OACC_KERNELS_LOOP: case ST_OACC_ATOMIC
1385 /* Declaration statements */
1387 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1388 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1389 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
1390 case ST_PROCEDURE: case ST_OMP_DECLARE_SIMD: case ST_OMP_DECLARE_REDUCTION: \
1391 case ST_OMP_DECLARE_TARGET: case ST_OACC_ROUTINE: case ST_OACC_DECLARE
1393 /* Block end statements. Errors associated with interchanging these
1394 are detected in gfc_match_end(). */
1396 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1397 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1398 case ST_END_BLOCK: case ST_END_ASSOCIATE
1401 /* Push a new state onto the stack. */
1404 push_state (gfc_state_data
*p
, gfc_compile_state new_state
, gfc_symbol
*sym
)
1406 p
->state
= new_state
;
1407 p
->previous
= gfc_state_stack
;
1409 p
->head
= p
->tail
= NULL
;
1410 p
->do_variable
= NULL
;
1411 if (p
->state
!= COMP_DO
&& p
->state
!= COMP_DO_CONCURRENT
)
1412 p
->ext
.oacc_declare_clauses
= NULL
;
1414 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1415 construct statement was accepted right before pushing the state. Thus,
1416 the construct's gfc_code is available as tail of the parent state. */
1417 gcc_assert (gfc_state_stack
);
1418 p
->construct
= gfc_state_stack
->tail
;
1420 gfc_state_stack
= p
;
1424 /* Pop the current state. */
1428 gfc_state_stack
= gfc_state_stack
->previous
;
1432 /* Try to find the given state in the state stack. */
1435 gfc_find_state (gfc_compile_state state
)
1439 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1440 if (p
->state
== state
)
1443 return (p
== NULL
) ? false : true;
1447 /* Starts a new level in the statement list. */
1450 new_level (gfc_code
*q
)
1454 p
= q
->block
= gfc_get_code (EXEC_NOP
);
1456 gfc_state_stack
->head
= gfc_state_stack
->tail
= p
;
1462 /* Add the current new_st code structure and adds it to the current
1463 program unit. As a side-effect, it zeroes the new_st. */
1466 add_statement (void)
1470 p
= XCNEW (gfc_code
);
1473 p
->loc
= gfc_current_locus
;
1475 if (gfc_state_stack
->head
== NULL
)
1476 gfc_state_stack
->head
= p
;
1478 gfc_state_stack
->tail
->next
= p
;
1480 while (p
->next
!= NULL
)
1483 gfc_state_stack
->tail
= p
;
1485 gfc_clear_new_st ();
1491 /* Frees everything associated with the current statement. */
1494 undo_new_statement (void)
1496 gfc_free_statements (new_st
.block
);
1497 gfc_free_statements (new_st
.next
);
1498 gfc_free_statement (&new_st
);
1499 gfc_clear_new_st ();
1503 /* If the current statement has a statement label, make sure that it
1504 is allowed to, or should have one. */
1507 check_statement_label (gfc_statement st
)
1511 if (gfc_statement_label
== NULL
)
1513 if (st
== ST_FORMAT
)
1514 gfc_error ("FORMAT statement at %L does not have a statement label",
1521 case ST_END_PROGRAM
:
1522 case ST_END_FUNCTION
:
1523 case ST_END_SUBROUTINE
:
1527 case ST_END_CRITICAL
:
1529 case ST_END_ASSOCIATE
:
1532 if (st
== ST_ENDDO
|| st
== ST_CONTINUE
)
1533 type
= ST_LABEL_DO_TARGET
;
1535 type
= ST_LABEL_TARGET
;
1539 type
= ST_LABEL_FORMAT
;
1542 /* Statement labels are not restricted from appearing on a
1543 particular line. However, there are plenty of situations
1544 where the resulting label can't be referenced. */
1547 type
= ST_LABEL_BAD_TARGET
;
1551 gfc_define_st_label (gfc_statement_label
, type
, &label_locus
);
1553 new_st
.here
= gfc_statement_label
;
1557 /* Figures out what the enclosing program unit is. This will be a
1558 function, subroutine, program, block data or module. */
1561 gfc_enclosing_unit (gfc_compile_state
* result
)
1565 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1566 if (p
->state
== COMP_FUNCTION
|| p
->state
== COMP_SUBROUTINE
1567 || p
->state
== COMP_MODULE
|| p
->state
== COMP_SUBMODULE
1568 || p
->state
== COMP_BLOCK_DATA
|| p
->state
== COMP_PROGRAM
)
1577 *result
= COMP_PROGRAM
;
1582 /* Translate a statement enum to a string. */
1585 gfc_ascii_statement (gfc_statement st
)
1591 case ST_ARITHMETIC_IF
:
1592 p
= _("arithmetic IF");
1601 p
= _("attribute declaration");
1637 p
= _("data declaration");
1645 case ST_DERIVED_DECL
:
1646 p
= _("derived type declaration");
1666 case ST_END_ASSOCIATE
:
1667 p
= "END ASSOCIATE";
1672 case ST_END_BLOCK_DATA
:
1673 p
= "END BLOCK DATA";
1675 case ST_END_CRITICAL
:
1687 case ST_END_FUNCTION
:
1693 case ST_END_INTERFACE
:
1694 p
= "END INTERFACE";
1699 case ST_END_SUBMODULE
:
1700 p
= "END SUBMODULE";
1702 case ST_END_PROGRAM
:
1708 case ST_END_SUBROUTINE
:
1709 p
= "END SUBROUTINE";
1720 case ST_EQUIVALENCE
:
1732 case ST_FORALL_BLOCK
: /* Fall through */
1754 case ST_IMPLICIT_NONE
:
1755 p
= "IMPLICIT NONE";
1757 case ST_IMPLIED_ENDDO
:
1758 p
= _("implied END DO");
1790 case ST_MODULE_PROC
:
1791 p
= "MODULE PROCEDURE";
1823 case ST_SYNC_IMAGES
:
1826 case ST_SYNC_MEMORY
:
1841 case ST_WHERE_BLOCK
: /* Fall through */
1852 p
= _("assignment");
1854 case ST_POINTER_ASSIGNMENT
:
1855 p
= _("pointer assignment");
1857 case ST_SELECT_CASE
:
1860 case ST_SELECT_TYPE
:
1875 case ST_STATEMENT_FUNCTION
:
1876 p
= "STATEMENT FUNCTION";
1878 case ST_LABEL_ASSIGNMENT
:
1879 p
= "LABEL ASSIGNMENT";
1882 p
= "ENUM DEFINITION";
1885 p
= "ENUMERATOR DEFINITION";
1890 case ST_OACC_PARALLEL_LOOP
:
1891 p
= "!$ACC PARALLEL LOOP";
1893 case ST_OACC_END_PARALLEL_LOOP
:
1894 p
= "!$ACC END PARALLEL LOOP";
1896 case ST_OACC_PARALLEL
:
1897 p
= "!$ACC PARALLEL";
1899 case ST_OACC_END_PARALLEL
:
1900 p
= "!$ACC END PARALLEL";
1902 case ST_OACC_KERNELS
:
1903 p
= "!$ACC KERNELS";
1905 case ST_OACC_END_KERNELS
:
1906 p
= "!$ACC END KERNELS";
1908 case ST_OACC_KERNELS_LOOP
:
1909 p
= "!$ACC KERNELS LOOP";
1911 case ST_OACC_END_KERNELS_LOOP
:
1912 p
= "!$ACC END KERNELS LOOP";
1917 case ST_OACC_END_DATA
:
1918 p
= "!$ACC END DATA";
1920 case ST_OACC_HOST_DATA
:
1921 p
= "!$ACC HOST_DATA";
1923 case ST_OACC_END_HOST_DATA
:
1924 p
= "!$ACC END HOST_DATA";
1929 case ST_OACC_END_LOOP
:
1930 p
= "!$ACC END LOOP";
1932 case ST_OACC_DECLARE
:
1933 p
= "!$ACC DECLARE";
1935 case ST_OACC_UPDATE
:
1944 case ST_OACC_ENTER_DATA
:
1945 p
= "!$ACC ENTER DATA";
1947 case ST_OACC_EXIT_DATA
:
1948 p
= "!$ACC EXIT DATA";
1950 case ST_OACC_ROUTINE
:
1951 p
= "!$ACC ROUTINE";
1953 case ST_OACC_ATOMIC
:
1956 case ST_OACC_END_ATOMIC
:
1957 p
= "!ACC END ATOMIC";
1962 case ST_OMP_BARRIER
:
1963 p
= "!$OMP BARRIER";
1968 case ST_OMP_CANCELLATION_POINT
:
1969 p
= "!$OMP CANCELLATION POINT";
1971 case ST_OMP_CRITICAL
:
1972 p
= "!$OMP CRITICAL";
1974 case ST_OMP_DECLARE_REDUCTION
:
1975 p
= "!$OMP DECLARE REDUCTION";
1977 case ST_OMP_DECLARE_SIMD
:
1978 p
= "!$OMP DECLARE SIMD";
1980 case ST_OMP_DECLARE_TARGET
:
1981 p
= "!$OMP DECLARE TARGET";
1983 case ST_OMP_DISTRIBUTE
:
1984 p
= "!$OMP DISTRIBUTE";
1986 case ST_OMP_DISTRIBUTE_PARALLEL_DO
:
1987 p
= "!$OMP DISTRIBUTE PARALLEL DO";
1989 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
1990 p
= "!$OMP DISTRIBUTE PARALLEL DO SIMD";
1992 case ST_OMP_DISTRIBUTE_SIMD
:
1993 p
= "!$OMP DISTRIBUTE SIMD";
1998 case ST_OMP_DO_SIMD
:
1999 p
= "!$OMP DO SIMD";
2001 case ST_OMP_END_ATOMIC
:
2002 p
= "!$OMP END ATOMIC";
2004 case ST_OMP_END_CRITICAL
:
2005 p
= "!$OMP END CRITICAL";
2007 case ST_OMP_END_DISTRIBUTE
:
2008 p
= "!$OMP END DISTRIBUTE";
2010 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO
:
2011 p
= "!$OMP END DISTRIBUTE PARALLEL DO";
2013 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD
:
2014 p
= "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
2016 case ST_OMP_END_DISTRIBUTE_SIMD
:
2017 p
= "!$OMP END DISTRIBUTE SIMD";
2022 case ST_OMP_END_DO_SIMD
:
2023 p
= "!$OMP END DO SIMD";
2025 case ST_OMP_END_SIMD
:
2026 p
= "!$OMP END SIMD";
2028 case ST_OMP_END_MASTER
:
2029 p
= "!$OMP END MASTER";
2031 case ST_OMP_END_ORDERED
:
2032 p
= "!$OMP END ORDERED";
2034 case ST_OMP_END_PARALLEL
:
2035 p
= "!$OMP END PARALLEL";
2037 case ST_OMP_END_PARALLEL_DO
:
2038 p
= "!$OMP END PARALLEL DO";
2040 case ST_OMP_END_PARALLEL_DO_SIMD
:
2041 p
= "!$OMP END PARALLEL DO SIMD";
2043 case ST_OMP_END_PARALLEL_SECTIONS
:
2044 p
= "!$OMP END PARALLEL SECTIONS";
2046 case ST_OMP_END_PARALLEL_WORKSHARE
:
2047 p
= "!$OMP END PARALLEL WORKSHARE";
2049 case ST_OMP_END_SECTIONS
:
2050 p
= "!$OMP END SECTIONS";
2052 case ST_OMP_END_SINGLE
:
2053 p
= "!$OMP END SINGLE";
2055 case ST_OMP_END_TASK
:
2056 p
= "!$OMP END TASK";
2058 case ST_OMP_END_TARGET
:
2059 p
= "!$OMP END TARGET";
2061 case ST_OMP_END_TARGET_DATA
:
2062 p
= "!$OMP END TARGET DATA";
2064 case ST_OMP_END_TARGET_TEAMS
:
2065 p
= "!$OMP END TARGET TEAMS";
2067 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE
:
2068 p
= "!$OMP END TARGET TEAMS DISTRIBUTE";
2070 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
2071 p
= "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2073 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
2074 p
= "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2076 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD
:
2077 p
= "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2079 case ST_OMP_END_TASKGROUP
:
2080 p
= "!$OMP END TASKGROUP";
2082 case ST_OMP_END_TEAMS
:
2083 p
= "!$OMP END TEAMS";
2085 case ST_OMP_END_TEAMS_DISTRIBUTE
:
2086 p
= "!$OMP END TEAMS DISTRIBUTE";
2088 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO
:
2089 p
= "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2091 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
2092 p
= "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2094 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD
:
2095 p
= "!$OMP END TEAMS DISTRIBUTE SIMD";
2097 case ST_OMP_END_WORKSHARE
:
2098 p
= "!$OMP END WORKSHARE";
2106 case ST_OMP_ORDERED
:
2107 p
= "!$OMP ORDERED";
2109 case ST_OMP_PARALLEL
:
2110 p
= "!$OMP PARALLEL";
2112 case ST_OMP_PARALLEL_DO
:
2113 p
= "!$OMP PARALLEL DO";
2115 case ST_OMP_PARALLEL_DO_SIMD
:
2116 p
= "!$OMP PARALLEL DO SIMD";
2118 case ST_OMP_PARALLEL_SECTIONS
:
2119 p
= "!$OMP PARALLEL SECTIONS";
2121 case ST_OMP_PARALLEL_WORKSHARE
:
2122 p
= "!$OMP PARALLEL WORKSHARE";
2124 case ST_OMP_SECTIONS
:
2125 p
= "!$OMP SECTIONS";
2127 case ST_OMP_SECTION
:
2128 p
= "!$OMP SECTION";
2139 case ST_OMP_TARGET_DATA
:
2140 p
= "!$OMP TARGET DATA";
2142 case ST_OMP_TARGET_TEAMS
:
2143 p
= "!$OMP TARGET TEAMS";
2145 case ST_OMP_TARGET_TEAMS_DISTRIBUTE
:
2146 p
= "!$OMP TARGET TEAMS DISTRIBUTE";
2148 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
2149 p
= "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2151 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
2152 p
= "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2154 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
2155 p
= "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2157 case ST_OMP_TARGET_UPDATE
:
2158 p
= "!$OMP TARGET UPDATE";
2163 case ST_OMP_TASKGROUP
:
2164 p
= "!$OMP TASKGROUP";
2166 case ST_OMP_TASKWAIT
:
2167 p
= "!$OMP TASKWAIT";
2169 case ST_OMP_TASKYIELD
:
2170 p
= "!$OMP TASKYIELD";
2175 case ST_OMP_TEAMS_DISTRIBUTE
:
2176 p
= "!$OMP TEAMS DISTRIBUTE";
2178 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
2179 p
= "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2181 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
2182 p
= "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2184 case ST_OMP_TEAMS_DISTRIBUTE_SIMD
:
2185 p
= "!$OMP TEAMS DISTRIBUTE SIMD";
2187 case ST_OMP_THREADPRIVATE
:
2188 p
= "!$OMP THREADPRIVATE";
2190 case ST_OMP_WORKSHARE
:
2191 p
= "!$OMP WORKSHARE";
2194 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2201 /* Create a symbol for the main program and assign it to ns->proc_name. */
2204 main_program_symbol (gfc_namespace
*ns
, const char *name
)
2206 gfc_symbol
*main_program
;
2207 symbol_attribute attr
;
2209 gfc_get_symbol (name
, ns
, &main_program
);
2210 gfc_clear_attr (&attr
);
2211 attr
.flavor
= FL_PROGRAM
;
2212 attr
.proc
= PROC_UNKNOWN
;
2213 attr
.subroutine
= 1;
2214 attr
.access
= ACCESS_PUBLIC
;
2215 attr
.is_main_program
= 1;
2216 main_program
->attr
= attr
;
2217 main_program
->declared_at
= gfc_current_locus
;
2218 ns
->proc_name
= main_program
;
2219 gfc_commit_symbols ();
2223 /* Do whatever is necessary to accept the last statement. */
2226 accept_statement (gfc_statement st
)
2230 case ST_IMPLICIT_NONE
:
2238 gfc_current_ns
->proc_name
= gfc_new_block
;
2241 /* If the statement is the end of a block, lay down a special code
2242 that allows a branch to the end of the block from within the
2243 construct. IF and SELECT are treated differently from DO
2244 (where EXEC_NOP is added inside the loop) for two
2246 1. END DO has a meaning in the sense that after a GOTO to
2247 it, the loop counter must be increased.
2248 2. IF blocks and SELECT blocks can consist of multiple
2249 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2250 Putting the label before the END IF would make the jump
2251 from, say, the ELSE IF block to the END IF illegal. */
2255 case ST_END_CRITICAL
:
2256 if (gfc_statement_label
!= NULL
)
2258 new_st
.op
= EXEC_END_NESTED_BLOCK
;
2263 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2264 one parallel block. Thus, we add the special code to the nested block
2265 itself, instead of the parent one. */
2267 case ST_END_ASSOCIATE
:
2268 if (gfc_statement_label
!= NULL
)
2270 new_st
.op
= EXEC_END_BLOCK
;
2275 /* The end-of-program unit statements do not get the special
2276 marker and require a statement of some sort if they are a
2279 case ST_END_PROGRAM
:
2280 case ST_END_FUNCTION
:
2281 case ST_END_SUBROUTINE
:
2282 if (gfc_statement_label
!= NULL
)
2284 new_st
.op
= EXEC_RETURN
;
2289 new_st
.op
= EXEC_END_PROCEDURE
;
2305 gfc_commit_symbols ();
2306 gfc_warning_check ();
2307 gfc_clear_new_st ();
2311 /* Undo anything tentative that has been built for the current
2315 reject_statement (void)
2317 /* Revert to the previous charlen chain. */
2318 gfc_free_charlen (gfc_current_ns
->cl_list
, gfc_current_ns
->old_cl_list
);
2319 gfc_current_ns
->cl_list
= gfc_current_ns
->old_cl_list
;
2321 gfc_free_equiv_until (gfc_current_ns
->equiv
, gfc_current_ns
->old_equiv
);
2322 gfc_current_ns
->equiv
= gfc_current_ns
->old_equiv
;
2324 gfc_reject_data (gfc_current_ns
);
2326 gfc_new_block
= NULL
;
2327 gfc_undo_symbols ();
2328 gfc_clear_warning ();
2329 undo_new_statement ();
2333 /* Generic complaint about an out of order statement. We also do
2334 whatever is necessary to clean up. */
2337 unexpected_statement (gfc_statement st
)
2339 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st
));
2341 reject_statement ();
2345 /* Given the next statement seen by the matcher, make sure that it is
2346 in proper order with the last. This subroutine is initialized by
2347 calling it with an argument of ST_NONE. If there is a problem, we
2348 issue an error and return false. Otherwise we return true.
2350 Individual parsers need to verify that the statements seen are
2351 valid before calling here, i.e., ENTRY statements are not allowed in
2352 INTERFACE blocks. The following diagram is taken from the standard:
2354 +---------------------------------------+
2355 | program subroutine function module |
2356 +---------------------------------------+
2358 +---------------------------------------+
2360 +---------------------------------------+
2362 | +-----------+------------------+
2363 | | parameter | implicit |
2364 | +-----------+------------------+
2365 | format | | derived type |
2366 | entry | parameter | interface |
2367 | | data | specification |
2368 | | | statement func |
2369 | +-----------+------------------+
2370 | | data | executable |
2371 +--------+-----------+------------------+
2373 +---------------------------------------+
2374 | internal module/subprogram |
2375 +---------------------------------------+
2377 +---------------------------------------+
2386 ORDER_IMPLICIT_NONE
,
2394 enum state_order state
;
2395 gfc_statement last_statement
;
2401 verify_st_order (st_state
*p
, gfc_statement st
, bool silent
)
2407 p
->state
= ORDER_START
;
2411 if (p
->state
> ORDER_USE
)
2413 p
->state
= ORDER_USE
;
2417 if (p
->state
> ORDER_IMPORT
)
2419 p
->state
= ORDER_IMPORT
;
2422 case ST_IMPLICIT_NONE
:
2423 if (p
->state
> ORDER_IMPLICIT
)
2426 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2427 statement disqualifies a USE but not an IMPLICIT NONE.
2428 Duplicate IMPLICIT NONEs are caught when the implicit types
2431 p
->state
= ORDER_IMPLICIT_NONE
;
2435 if (p
->state
> ORDER_IMPLICIT
)
2437 p
->state
= ORDER_IMPLICIT
;
2442 if (p
->state
< ORDER_IMPLICIT_NONE
)
2443 p
->state
= ORDER_IMPLICIT_NONE
;
2447 if (p
->state
>= ORDER_EXEC
)
2449 if (p
->state
< ORDER_IMPLICIT
)
2450 p
->state
= ORDER_IMPLICIT
;
2454 if (p
->state
< ORDER_SPEC
)
2455 p
->state
= ORDER_SPEC
;
2460 case ST_DERIVED_DECL
:
2462 if (p
->state
>= ORDER_EXEC
)
2464 if (p
->state
< ORDER_SPEC
)
2465 p
->state
= ORDER_SPEC
;
2470 if (p
->state
< ORDER_EXEC
)
2471 p
->state
= ORDER_EXEC
;
2478 /* All is well, record the statement in case we need it next time. */
2479 p
->where
= gfc_current_locus
;
2480 p
->last_statement
= st
;
2485 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2486 gfc_ascii_statement (st
),
2487 gfc_ascii_statement (p
->last_statement
), &p
->where
);
2493 /* Handle an unexpected end of file. This is a show-stopper... */
2495 static void unexpected_eof (void) ATTRIBUTE_NORETURN
;
2498 unexpected_eof (void)
2502 gfc_error ("Unexpected end of file in %qs", gfc_source_file
);
2504 /* Memory cleanup. Move to "second to last". */
2505 for (p
= gfc_state_stack
; p
&& p
->previous
&& p
->previous
->previous
;
2508 gfc_current_ns
->code
= (p
&& p
->previous
) ? p
->head
: NULL
;
2511 longjmp (eof_buf
, 1);
2515 /* Parse the CONTAINS section of a derived type definition. */
2517 gfc_access gfc_typebound_default_access
;
2520 parse_derived_contains (void)
2523 bool seen_private
= false;
2524 bool seen_comps
= false;
2525 bool error_flag
= false;
2528 gcc_assert (gfc_current_state () == COMP_DERIVED
);
2529 gcc_assert (gfc_current_block ());
2531 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2533 if (gfc_current_block ()->attr
.sequence
)
2534 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2535 " section at %C", gfc_current_block ()->name
);
2536 if (gfc_current_block ()->attr
.is_bind_c
)
2537 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2538 " section at %C", gfc_current_block ()->name
);
2540 accept_statement (ST_CONTAINS
);
2541 push_state (&s
, COMP_DERIVED_CONTAINS
, NULL
);
2543 gfc_typebound_default_access
= ACCESS_PUBLIC
;
2549 st
= next_statement ();
2557 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2561 if (!gfc_notify_std (GFC_STD_F2003
, "Type-bound procedure at %C"))
2564 accept_statement (ST_PROCEDURE
);
2569 if (!gfc_notify_std (GFC_STD_F2003
, "GENERIC binding at %C"))
2572 accept_statement (ST_GENERIC
);
2577 if (!gfc_notify_std (GFC_STD_F2003
, "FINAL procedure declaration"
2581 accept_statement (ST_FINAL
);
2589 && (!gfc_notify_std(GFC_STD_F2008
, "Derived type definition "
2590 "at %C with empty CONTAINS section")))
2593 /* ST_END_TYPE is accepted by parse_derived after return. */
2597 if (!gfc_find_state (COMP_MODULE
))
2599 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2606 gfc_error ("PRIVATE statement at %C must precede procedure"
2613 gfc_error ("Duplicate PRIVATE statement at %C");
2617 accept_statement (ST_PRIVATE
);
2618 gfc_typebound_default_access
= ACCESS_PRIVATE
;
2619 seen_private
= true;
2623 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2627 gfc_error ("Already inside a CONTAINS block at %C");
2631 unexpected_statement (st
);
2639 reject_statement ();
2643 gcc_assert (gfc_current_state () == COMP_DERIVED
);
2649 /* Parse a derived type. */
2652 parse_derived (void)
2654 int compiling_type
, seen_private
, seen_sequence
, seen_component
;
2658 gfc_component
*c
, *lock_comp
= NULL
, *event_comp
= NULL
;
2660 accept_statement (ST_DERIVED_DECL
);
2661 push_state (&s
, COMP_DERIVED
, gfc_new_block
);
2663 gfc_new_block
->component_access
= ACCESS_PUBLIC
;
2670 while (compiling_type
)
2672 st
= next_statement ();
2680 accept_statement (st
);
2685 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2692 if (!seen_component
)
2693 gfc_notify_std (GFC_STD_F2003
, "Derived type "
2694 "definition at %C without components");
2696 accept_statement (ST_END_TYPE
);
2700 if (!gfc_find_state (COMP_MODULE
))
2702 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2709 gfc_error ("PRIVATE statement at %C must precede "
2710 "structure components");
2715 gfc_error ("Duplicate PRIVATE statement at %C");
2717 s
.sym
->component_access
= ACCESS_PRIVATE
;
2719 accept_statement (ST_PRIVATE
);
2726 gfc_error ("SEQUENCE statement at %C must precede "
2727 "structure components");
2731 if (gfc_current_block ()->attr
.sequence
)
2732 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
2737 gfc_error ("Duplicate SEQUENCE statement at %C");
2741 gfc_add_sequence (&gfc_current_block ()->attr
,
2742 gfc_current_block ()->name
, NULL
);
2746 gfc_notify_std (GFC_STD_F2003
,
2747 "CONTAINS block in derived type"
2748 " definition at %C");
2750 accept_statement (ST_CONTAINS
);
2751 parse_derived_contains ();
2755 unexpected_statement (st
);
2760 /* need to verify that all fields of the derived type are
2761 * interoperable with C if the type is declared to be bind(c)
2763 sym
= gfc_current_block ();
2764 for (c
= sym
->components
; c
; c
= c
->next
)
2766 bool coarray
, lock_type
, event_type
, allocatable
, pointer
;
2767 coarray
= lock_type
= event_type
= allocatable
= pointer
= false;
2769 /* Look for allocatable components. */
2770 if (c
->attr
.allocatable
2771 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2772 && CLASS_DATA (c
)->attr
.allocatable
)
2773 || (c
->ts
.type
== BT_DERIVED
&& !c
->attr
.pointer
2774 && c
->ts
.u
.derived
->attr
.alloc_comp
))
2777 sym
->attr
.alloc_comp
= 1;
2780 /* Look for pointer components. */
2782 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2783 && CLASS_DATA (c
)->attr
.class_pointer
)
2784 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.pointer_comp
))
2787 sym
->attr
.pointer_comp
= 1;
2790 /* Look for procedure pointer components. */
2791 if (c
->attr
.proc_pointer
2792 || (c
->ts
.type
== BT_DERIVED
2793 && c
->ts
.u
.derived
->attr
.proc_pointer_comp
))
2794 sym
->attr
.proc_pointer_comp
= 1;
2796 /* Looking for coarray components. */
2797 if (c
->attr
.codimension
2798 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2799 && CLASS_DATA (c
)->attr
.codimension
))
2802 sym
->attr
.coarray_comp
= 1;
2805 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.coarray_comp
2806 && !c
->attr
.pointer
)
2809 sym
->attr
.coarray_comp
= 1;
2812 /* Looking for lock_type components. */
2813 if ((c
->ts
.type
== BT_DERIVED
2814 && c
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
2815 && c
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
)
2816 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2817 && CLASS_DATA (c
)->ts
.u
.derived
->from_intmod
2818 == INTMOD_ISO_FORTRAN_ENV
2819 && CLASS_DATA (c
)->ts
.u
.derived
->intmod_sym_id
2820 == ISOFORTRAN_LOCK_TYPE
)
2821 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.lock_comp
2822 && !allocatable
&& !pointer
))
2826 sym
->attr
.lock_comp
= 1;
2829 /* Looking for event_type components. */
2830 if ((c
->ts
.type
== BT_DERIVED
2831 && c
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
2832 && c
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
)
2833 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2834 && CLASS_DATA (c
)->ts
.u
.derived
->from_intmod
2835 == INTMOD_ISO_FORTRAN_ENV
2836 && CLASS_DATA (c
)->ts
.u
.derived
->intmod_sym_id
2837 == ISOFORTRAN_EVENT_TYPE
)
2838 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.event_comp
2839 && !allocatable
&& !pointer
))
2843 sym
->attr
.event_comp
= 1;
2846 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2847 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2848 unless there are nondirect [allocatable or pointer] components
2849 involved (cf. 1.3.33.1 and 1.3.33.3). */
2851 if (pointer
&& !coarray
&& lock_type
)
2852 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2853 "codimension or be a subcomponent of a coarray, "
2854 "which is not possible as the component has the "
2855 "pointer attribute", c
->name
, &c
->loc
);
2856 else if (pointer
&& !coarray
&& c
->ts
.type
== BT_DERIVED
2857 && c
->ts
.u
.derived
->attr
.lock_comp
)
2858 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2859 "of type LOCK_TYPE, which must have a codimension or be a "
2860 "subcomponent of a coarray", c
->name
, &c
->loc
);
2862 if (lock_type
&& allocatable
&& !coarray
)
2863 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2864 "a codimension", c
->name
, &c
->loc
);
2865 else if (lock_type
&& allocatable
&& c
->ts
.type
== BT_DERIVED
2866 && c
->ts
.u
.derived
->attr
.lock_comp
)
2867 gfc_error ("Allocatable component %s at %L must have a codimension as "
2868 "it has a noncoarray subcomponent of type LOCK_TYPE",
2871 if (sym
->attr
.coarray_comp
&& !coarray
&& lock_type
)
2872 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2873 "subcomponent of type LOCK_TYPE must have a codimension or "
2874 "be a subcomponent of a coarray. (Variables of type %s may "
2875 "not have a codimension as already a coarray "
2876 "subcomponent exists)", c
->name
, &c
->loc
, sym
->name
);
2878 if (sym
->attr
.lock_comp
&& coarray
&& !lock_type
)
2879 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2880 "subcomponent of type LOCK_TYPE must have a codimension or "
2881 "be a subcomponent of a coarray. (Variables of type %s may "
2882 "not have a codimension as %s at %L has a codimension or a "
2883 "coarray subcomponent)", lock_comp
->name
, &lock_comp
->loc
,
2884 sym
->name
, c
->name
, &c
->loc
);
2886 /* Similarly for EVENT TYPE. */
2888 if (pointer
&& !coarray
&& event_type
)
2889 gfc_error ("Component %s at %L of type EVENT_TYPE must have a "
2890 "codimension or be a subcomponent of a coarray, "
2891 "which is not possible as the component has the "
2892 "pointer attribute", c
->name
, &c
->loc
);
2893 else if (pointer
&& !coarray
&& c
->ts
.type
== BT_DERIVED
2894 && c
->ts
.u
.derived
->attr
.event_comp
)
2895 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2896 "of type EVENT_TYPE, which must have a codimension or be a "
2897 "subcomponent of a coarray", c
->name
, &c
->loc
);
2899 if (event_type
&& allocatable
&& !coarray
)
2900 gfc_error ("Allocatable component %s at %L of type EVENT_TYPE must have "
2901 "a codimension", c
->name
, &c
->loc
);
2902 else if (event_type
&& allocatable
&& c
->ts
.type
== BT_DERIVED
2903 && c
->ts
.u
.derived
->attr
.event_comp
)
2904 gfc_error ("Allocatable component %s at %L must have a codimension as "
2905 "it has a noncoarray subcomponent of type EVENT_TYPE",
2908 if (sym
->attr
.coarray_comp
&& !coarray
&& event_type
)
2909 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
2910 "subcomponent of type EVENT_TYPE must have a codimension or "
2911 "be a subcomponent of a coarray. (Variables of type %s may "
2912 "not have a codimension as already a coarray "
2913 "subcomponent exists)", c
->name
, &c
->loc
, sym
->name
);
2915 if (sym
->attr
.event_comp
&& coarray
&& !event_type
)
2916 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
2917 "subcomponent of type EVENT_TYPE must have a codimension or "
2918 "be a subcomponent of a coarray. (Variables of type %s may "
2919 "not have a codimension as %s at %L has a codimension or a "
2920 "coarray subcomponent)", event_comp
->name
, &event_comp
->loc
,
2921 sym
->name
, c
->name
, &c
->loc
);
2923 /* Look for private components. */
2924 if (sym
->component_access
== ACCESS_PRIVATE
2925 || c
->attr
.access
== ACCESS_PRIVATE
2926 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.private_comp
))
2927 sym
->attr
.private_comp
= 1;
2930 if (!seen_component
)
2931 sym
->attr
.zero_comp
= 1;
2937 /* Parse an ENUM. */
2945 int seen_enumerator
= 0;
2947 push_state (&s
, COMP_ENUM
, gfc_new_block
);
2951 while (compiling_enum
)
2953 st
= next_statement ();
2961 seen_enumerator
= 1;
2962 accept_statement (st
);
2967 if (!seen_enumerator
)
2968 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2969 accept_statement (st
);
2973 gfc_free_enum_history ();
2974 unexpected_statement (st
);
2982 /* Parse an interface. We must be able to deal with the possibility
2983 of recursive interfaces. The parse_spec() subroutine is mutually
2984 recursive with parse_interface(). */
2986 static gfc_statement
parse_spec (gfc_statement
);
2989 parse_interface (void)
2991 gfc_compile_state new_state
= COMP_NONE
, current_state
;
2992 gfc_symbol
*prog_unit
, *sym
;
2993 gfc_interface_info save
;
2994 gfc_state_data s1
, s2
;
2997 accept_statement (ST_INTERFACE
);
2999 current_interface
.ns
= gfc_current_ns
;
3000 save
= current_interface
;
3002 sym
= (current_interface
.type
== INTERFACE_GENERIC
3003 || current_interface
.type
== INTERFACE_USER_OP
)
3004 ? gfc_new_block
: NULL
;
3006 push_state (&s1
, COMP_INTERFACE
, sym
);
3007 current_state
= COMP_NONE
;
3010 gfc_current_ns
= gfc_get_namespace (current_interface
.ns
, 0);
3012 st
= next_statement ();
3020 if (st
== ST_SUBROUTINE
)
3021 new_state
= COMP_SUBROUTINE
;
3022 else if (st
== ST_FUNCTION
)
3023 new_state
= COMP_FUNCTION
;
3024 if (gfc_new_block
->attr
.pointer
)
3026 gfc_new_block
->attr
.pointer
= 0;
3027 gfc_new_block
->attr
.proc_pointer
= 1;
3029 if (!gfc_add_explicit_interface (gfc_new_block
, IFSRC_IFBODY
,
3030 gfc_new_block
->formal
, NULL
))
3032 reject_statement ();
3033 gfc_free_namespace (gfc_current_ns
);
3036 /* F2008 C1210 forbids the IMPORT statement in module procedure
3037 interface bodies and the flag is set to import symbols. */
3038 if (gfc_new_block
->attr
.module_procedure
)
3039 gfc_current_ns
->has_import_set
= 1;
3043 case ST_MODULE_PROC
: /* The module procedure matcher makes
3044 sure the context is correct. */
3045 accept_statement (st
);
3046 gfc_free_namespace (gfc_current_ns
);
3049 case ST_END_INTERFACE
:
3050 gfc_free_namespace (gfc_current_ns
);
3051 gfc_current_ns
= current_interface
.ns
;
3055 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
3056 gfc_ascii_statement (st
));
3057 reject_statement ();
3058 gfc_free_namespace (gfc_current_ns
);
3063 /* Make sure that the generic name has the right attribute. */
3064 if (current_interface
.type
== INTERFACE_GENERIC
3065 && current_state
== COMP_NONE
)
3067 if (new_state
== COMP_FUNCTION
&& sym
)
3068 gfc_add_function (&sym
->attr
, sym
->name
, NULL
);
3069 else if (new_state
== COMP_SUBROUTINE
&& sym
)
3070 gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
);
3072 current_state
= new_state
;
3075 if (current_interface
.type
== INTERFACE_ABSTRACT
)
3077 gfc_add_abstract (&gfc_new_block
->attr
, &gfc_current_locus
);
3078 if (gfc_is_intrinsic_typename (gfc_new_block
->name
))
3079 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
3080 "cannot be the same as an intrinsic type",
3081 gfc_new_block
->name
);
3084 push_state (&s2
, new_state
, gfc_new_block
);
3085 accept_statement (st
);
3086 prog_unit
= gfc_new_block
;
3087 prog_unit
->formal_ns
= gfc_current_ns
;
3088 if (prog_unit
== prog_unit
->formal_ns
->proc_name
3089 && prog_unit
->ns
!= prog_unit
->formal_ns
)
3093 /* Read data declaration statements. */
3094 st
= parse_spec (ST_NONE
);
3095 in_specification_block
= true;
3097 /* Since the interface block does not permit an IMPLICIT statement,
3098 the default type for the function or the result must be taken
3099 from the formal namespace. */
3100 if (new_state
== COMP_FUNCTION
)
3102 if (prog_unit
->result
== prog_unit
3103 && prog_unit
->ts
.type
== BT_UNKNOWN
)
3104 gfc_set_default_type (prog_unit
, 1, prog_unit
->formal_ns
);
3105 else if (prog_unit
->result
!= prog_unit
3106 && prog_unit
->result
->ts
.type
== BT_UNKNOWN
)
3107 gfc_set_default_type (prog_unit
->result
, 1,
3108 prog_unit
->formal_ns
);
3111 if (st
!= ST_END_SUBROUTINE
&& st
!= ST_END_FUNCTION
)
3113 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3114 gfc_ascii_statement (st
));
3115 reject_statement ();
3119 /* Add EXTERNAL attribute to function or subroutine. */
3120 if (current_interface
.type
!= INTERFACE_ABSTRACT
&& !prog_unit
->attr
.dummy
)
3121 gfc_add_external (&prog_unit
->attr
, &gfc_current_locus
);
3123 current_interface
= save
;
3124 gfc_add_interface (prog_unit
);
3127 if (current_interface
.ns
3128 && current_interface
.ns
->proc_name
3129 && strcmp (current_interface
.ns
->proc_name
->name
,
3130 prog_unit
->name
) == 0)
3131 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3132 "enclosing procedure", prog_unit
->name
,
3133 ¤t_interface
.ns
->proc_name
->declared_at
);
3142 /* Associate function characteristics by going back to the function
3143 declaration and rematching the prefix. */
3146 match_deferred_characteristics (gfc_typespec
* ts
)
3149 match m
= MATCH_ERROR
;
3150 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3152 loc
= gfc_current_locus
;
3154 gfc_current_locus
= gfc_current_block ()->declared_at
;
3157 gfc_buffer_error (true);
3158 m
= gfc_match_prefix (ts
);
3159 gfc_buffer_error (false);
3161 if (ts
->type
== BT_DERIVED
)
3169 /* Only permit one go at the characteristic association. */
3173 /* Set the function locus correctly. If we have not found the
3174 function name, there is an error. */
3176 && gfc_match ("function% %n", name
) == MATCH_YES
3177 && strcmp (name
, gfc_current_block ()->name
) == 0)
3179 gfc_current_block ()->declared_at
= gfc_current_locus
;
3180 gfc_commit_symbols ();
3185 gfc_undo_symbols ();
3188 gfc_current_locus
=loc
;
3193 /* Check specification-expressions in the function result of the currently
3194 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3195 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3196 scope are not yet parsed so this has to be delayed up to parse_spec. */
3199 check_function_result_typed (void)
3203 gcc_assert (gfc_current_state () == COMP_FUNCTION
);
3205 if (!gfc_current_ns
->proc_name
->result
) return;
3207 ts
= gfc_current_ns
->proc_name
->result
->ts
;
3209 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3210 /* TODO: Extend when KIND type parameters are implemented. */
3211 if (ts
.type
== BT_CHARACTER
&& ts
.u
.cl
&& ts
.u
.cl
->length
)
3212 gfc_expr_check_typed (ts
.u
.cl
->length
, gfc_current_ns
, true);
3216 /* Parse a set of specification statements. Returns the statement
3217 that doesn't fit. */
3219 static gfc_statement
3220 parse_spec (gfc_statement st
)
3223 bool function_result_typed
= false;
3224 bool bad_characteristic
= false;
3227 in_specification_block
= true;
3229 verify_st_order (&ss
, ST_NONE
, false);
3231 st
= next_statement ();
3233 /* If we are not inside a function or don't have a result specified so far,
3234 do nothing special about it. */
3235 if (gfc_current_state () != COMP_FUNCTION
)
3236 function_result_typed
= true;
3239 gfc_symbol
* proc
= gfc_current_ns
->proc_name
;
3242 if (proc
->result
->ts
.type
== BT_UNKNOWN
)
3243 function_result_typed
= true;
3248 /* If we're inside a BLOCK construct, some statements are disallowed.
3249 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3250 or VALUE are also disallowed, but they don't have a particular ST_*
3251 key so we have to check for them individually in their matcher routine. */
3252 if (gfc_current_state () == COMP_BLOCK
)
3256 case ST_IMPLICIT_NONE
:
3259 case ST_EQUIVALENCE
:
3260 case ST_STATEMENT_FUNCTION
:
3261 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3262 gfc_ascii_statement (st
));
3263 reject_statement ();
3269 else if (gfc_current_state () == COMP_BLOCK_DATA
)
3270 /* Fortran 2008, C1116. */
3277 case ST_END_BLOCK_DATA
:
3279 case ST_EQUIVALENCE
:
3282 case ST_IMPLICIT_NONE
:
3283 case ST_DERIVED_DECL
:
3291 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3292 gfc_ascii_statement (st
));
3293 reject_statement ();
3297 /* If we find a statement that can not be followed by an IMPLICIT statement
3298 (and thus we can expect to see none any further), type the function result
3299 if it has not yet been typed. Be careful not to give the END statement
3300 to verify_st_order! */
3301 if (!function_result_typed
&& st
!= ST_GET_FCN_CHARACTERISTICS
)
3303 bool verify_now
= false;
3305 if (st
== ST_END_FUNCTION
|| st
== ST_CONTAINS
)
3310 verify_st_order (&dummyss
, ST_NONE
, false);
3311 verify_st_order (&dummyss
, st
, false);
3313 if (!verify_st_order (&dummyss
, ST_IMPLICIT
, true))
3319 check_function_result_typed ();
3320 function_result_typed
= true;
3329 case ST_IMPLICIT_NONE
:
3331 if (!function_result_typed
)
3333 check_function_result_typed ();
3334 function_result_typed
= true;
3340 case ST_DATA
: /* Not allowed in interfaces */
3341 if (gfc_current_state () == COMP_INTERFACE
)
3351 case ST_DERIVED_DECL
:
3354 if (!verify_st_order (&ss
, st
, false))
3356 reject_statement ();
3357 st
= next_statement ();
3367 case ST_DERIVED_DECL
:
3373 if (gfc_current_state () != COMP_MODULE
)
3375 gfc_error ("%s statement must appear in a MODULE",
3376 gfc_ascii_statement (st
));
3377 reject_statement ();
3381 if (gfc_current_ns
->default_access
!= ACCESS_UNKNOWN
)
3383 gfc_error ("%s statement at %C follows another accessibility "
3384 "specification", gfc_ascii_statement (st
));
3385 reject_statement ();
3389 gfc_current_ns
->default_access
= (st
== ST_PUBLIC
)
3390 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
3394 case ST_STATEMENT_FUNCTION
:
3395 if (gfc_current_state () == COMP_MODULE
3396 || gfc_current_state () == COMP_SUBMODULE
)
3398 unexpected_statement (st
);
3406 accept_statement (st
);
3407 st
= next_statement ();
3411 accept_statement (st
);
3413 st
= next_statement ();
3416 case ST_GET_FCN_CHARACTERISTICS
:
3417 /* This statement triggers the association of a function's result
3419 ts
= &gfc_current_block ()->result
->ts
;
3420 if (match_deferred_characteristics (ts
) != MATCH_YES
)
3421 bad_characteristic
= true;
3423 st
= next_statement ();
3430 /* If match_deferred_characteristics failed, then there is an error. */
3431 if (bad_characteristic
)
3433 ts
= &gfc_current_block ()->result
->ts
;
3434 if (ts
->type
!= BT_DERIVED
)
3435 gfc_error ("Bad kind expression for function %qs at %L",
3436 gfc_current_block ()->name
,
3437 &gfc_current_block ()->declared_at
);
3439 gfc_error ("The type for function %qs at %L is not accessible",
3440 gfc_current_block ()->name
,
3441 &gfc_current_block ()->declared_at
);
3443 gfc_current_block ()->ts
.kind
= 0;
3444 /* Keep the derived type; if it's bad, it will be discovered later. */
3445 if (!(ts
->type
== BT_DERIVED
&& ts
->u
.derived
))
3446 ts
->type
= BT_UNKNOWN
;
3449 in_specification_block
= false;
3455 /* Parse a WHERE block, (not a simple WHERE statement). */
3458 parse_where_block (void)
3460 int seen_empty_else
;
3465 accept_statement (ST_WHERE_BLOCK
);
3466 top
= gfc_state_stack
->tail
;
3468 push_state (&s
, COMP_WHERE
, gfc_new_block
);
3470 d
= add_statement ();
3471 d
->expr1
= top
->expr1
;
3477 seen_empty_else
= 0;
3481 st
= next_statement ();
3487 case ST_WHERE_BLOCK
:
3488 parse_where_block ();
3493 accept_statement (st
);
3497 if (seen_empty_else
)
3499 gfc_error ("ELSEWHERE statement at %C follows previous "
3500 "unmasked ELSEWHERE");
3501 reject_statement ();
3505 if (new_st
.expr1
== NULL
)
3506 seen_empty_else
= 1;
3508 d
= new_level (gfc_state_stack
->head
);
3510 d
->expr1
= new_st
.expr1
;
3512 accept_statement (st
);
3517 accept_statement (st
);
3521 gfc_error ("Unexpected %s statement in WHERE block at %C",
3522 gfc_ascii_statement (st
));
3523 reject_statement ();
3527 while (st
!= ST_END_WHERE
);
3533 /* Parse a FORALL block (not a simple FORALL statement). */
3536 parse_forall_block (void)
3542 accept_statement (ST_FORALL_BLOCK
);
3543 top
= gfc_state_stack
->tail
;
3545 push_state (&s
, COMP_FORALL
, gfc_new_block
);
3547 d
= add_statement ();
3548 d
->op
= EXEC_FORALL
;
3553 st
= next_statement ();
3558 case ST_POINTER_ASSIGNMENT
:
3561 accept_statement (st
);
3564 case ST_WHERE_BLOCK
:
3565 parse_where_block ();
3568 case ST_FORALL_BLOCK
:
3569 parse_forall_block ();
3573 accept_statement (st
);
3580 gfc_error ("Unexpected %s statement in FORALL block at %C",
3581 gfc_ascii_statement (st
));
3583 reject_statement ();
3587 while (st
!= ST_END_FORALL
);
3593 static gfc_statement
parse_executable (gfc_statement
);
3595 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
3598 parse_if_block (void)
3607 accept_statement (ST_IF_BLOCK
);
3609 top
= gfc_state_stack
->tail
;
3610 push_state (&s
, COMP_IF
, gfc_new_block
);
3612 new_st
.op
= EXEC_IF
;
3613 d
= add_statement ();
3615 d
->expr1
= top
->expr1
;
3621 st
= parse_executable (ST_NONE
);
3631 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
3632 "statement at %L", &else_locus
);
3634 reject_statement ();
3638 d
= new_level (gfc_state_stack
->head
);
3640 d
->expr1
= new_st
.expr1
;
3642 accept_statement (st
);
3649 gfc_error ("Duplicate ELSE statements at %L and %C",
3651 reject_statement ();
3656 else_locus
= gfc_current_locus
;
3658 d
= new_level (gfc_state_stack
->head
);
3661 accept_statement (st
);
3669 unexpected_statement (st
);
3673 while (st
!= ST_ENDIF
);
3676 accept_statement (st
);
3680 /* Parse a SELECT block. */
3683 parse_select_block (void)
3689 accept_statement (ST_SELECT_CASE
);
3691 cp
= gfc_state_stack
->tail
;
3692 push_state (&s
, COMP_SELECT
, gfc_new_block
);
3694 /* Make sure that the next statement is a CASE or END SELECT. */
3697 st
= next_statement ();
3700 if (st
== ST_END_SELECT
)
3702 /* Empty SELECT CASE is OK. */
3703 accept_statement (st
);
3710 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
3713 reject_statement ();
3716 /* At this point, we're got a nonempty select block. */
3717 cp
= new_level (cp
);
3720 accept_statement (st
);
3724 st
= parse_executable (ST_NONE
);
3731 cp
= new_level (gfc_state_stack
->head
);
3733 gfc_clear_new_st ();
3735 accept_statement (st
);
3741 /* Can't have an executable statement because of
3742 parse_executable(). */
3744 unexpected_statement (st
);
3748 while (st
!= ST_END_SELECT
);
3751 accept_statement (st
);
3755 /* Pop the current selector from the SELECT TYPE stack. */
3758 select_type_pop (void)
3760 gfc_select_type_stack
*old
= select_type_stack
;
3761 select_type_stack
= old
->prev
;
3766 /* Parse a SELECT TYPE construct (F03:R821). */
3769 parse_select_type_block (void)
3775 accept_statement (ST_SELECT_TYPE
);
3777 cp
= gfc_state_stack
->tail
;
3778 push_state (&s
, COMP_SELECT_TYPE
, gfc_new_block
);
3780 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
3784 st
= next_statement ();
3787 if (st
== ST_END_SELECT
)
3788 /* Empty SELECT CASE is OK. */
3790 if (st
== ST_TYPE_IS
|| st
== ST_CLASS_IS
)
3793 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
3794 "following SELECT TYPE at %C");
3796 reject_statement ();
3799 /* At this point, we're got a nonempty select block. */
3800 cp
= new_level (cp
);
3803 accept_statement (st
);
3807 st
= parse_executable (ST_NONE
);
3815 cp
= new_level (gfc_state_stack
->head
);
3817 gfc_clear_new_st ();
3819 accept_statement (st
);
3825 /* Can't have an executable statement because of
3826 parse_executable(). */
3828 unexpected_statement (st
);
3832 while (st
!= ST_END_SELECT
);
3836 accept_statement (st
);
3837 gfc_current_ns
= gfc_current_ns
->parent
;
3842 /* Given a symbol, make sure it is not an iteration variable for a DO
3843 statement. This subroutine is called when the symbol is seen in a
3844 context that causes it to become redefined. If the symbol is an
3845 iterator, we generate an error message and return nonzero. */
3848 gfc_check_do_variable (gfc_symtree
*st
)
3852 for (s
=gfc_state_stack
; s
; s
= s
->previous
)
3853 if (s
->do_variable
== st
)
3855 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
3856 "loop beginning at %L", st
->name
, &s
->head
->loc
);
3864 /* Checks to see if the current statement label closes an enddo.
3865 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3866 an error) if it incorrectly closes an ENDDO. */
3869 check_do_closure (void)
3873 if (gfc_statement_label
== NULL
)
3876 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
3877 if (p
->state
== COMP_DO
|| p
->state
== COMP_DO_CONCURRENT
)
3881 return 0; /* No loops to close */
3883 if (p
->ext
.end_do_label
== gfc_statement_label
)
3885 if (p
== gfc_state_stack
)
3888 gfc_error ("End of nonblock DO statement at %C is within another block");
3892 /* At this point, the label doesn't terminate the innermost loop.
3893 Make sure it doesn't terminate another one. */
3894 for (; p
; p
= p
->previous
)
3895 if ((p
->state
== COMP_DO
|| p
->state
== COMP_DO_CONCURRENT
)
3896 && p
->ext
.end_do_label
== gfc_statement_label
)
3898 gfc_error ("End of nonblock DO statement at %C is interwoven "
3899 "with another DO loop");
3907 /* Parse a series of contained program units. */
3909 static void parse_progunit (gfc_statement
);
3912 /* Parse a CRITICAL block. */
3915 parse_critical_block (void)
3918 gfc_state_data s
, *sd
;
3921 for (sd
= gfc_state_stack
; sd
; sd
= sd
->previous
)
3922 if (sd
->state
== COMP_OMP_STRUCTURED_BLOCK
)
3923 gfc_error_now (is_oacc (sd
)
3924 ? "CRITICAL block inside of OpenACC region at %C"
3925 : "CRITICAL block inside of OpenMP region at %C");
3927 s
.ext
.end_do_label
= new_st
.label1
;
3929 accept_statement (ST_CRITICAL
);
3930 top
= gfc_state_stack
->tail
;
3932 push_state (&s
, COMP_CRITICAL
, gfc_new_block
);
3934 d
= add_statement ();
3935 d
->op
= EXEC_CRITICAL
;
3940 st
= parse_executable (ST_NONE
);
3948 case ST_END_CRITICAL
:
3949 if (s
.ext
.end_do_label
!= NULL
3950 && s
.ext
.end_do_label
!= gfc_statement_label
)
3951 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3952 "match CRITICAL label");
3954 if (gfc_statement_label
!= NULL
)
3956 new_st
.op
= EXEC_NOP
;
3962 unexpected_statement (st
);
3966 while (st
!= ST_END_CRITICAL
);
3969 accept_statement (st
);
3973 /* Set up the local namespace for a BLOCK construct. */
3976 gfc_build_block_ns (gfc_namespace
*parent_ns
)
3978 gfc_namespace
* my_ns
;
3979 static int numblock
= 1;
3981 my_ns
= gfc_get_namespace (parent_ns
, 1);
3982 my_ns
->construct_entities
= 1;
3984 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3985 code generation (so it must not be NULL).
3986 We set its recursive argument if our container procedure is recursive, so
3987 that local variables are accordingly placed on the stack when it
3988 will be necessary. */
3990 my_ns
->proc_name
= gfc_new_block
;
3994 char buffer
[20]; /* Enough to hold "block@2147483648\n". */
3996 snprintf(buffer
, sizeof(buffer
), "block@%d", numblock
++);
3997 gfc_get_symbol (buffer
, my_ns
, &my_ns
->proc_name
);
3998 t
= gfc_add_flavor (&my_ns
->proc_name
->attr
, FL_LABEL
,
3999 my_ns
->proc_name
->name
, NULL
);
4001 gfc_commit_symbol (my_ns
->proc_name
);
4004 if (parent_ns
->proc_name
)
4005 my_ns
->proc_name
->attr
.recursive
= parent_ns
->proc_name
->attr
.recursive
;
4011 /* Parse a BLOCK construct. */
4014 parse_block_construct (void)
4016 gfc_namespace
* my_ns
;
4017 gfc_namespace
* my_parent
;
4020 gfc_notify_std (GFC_STD_F2008
, "BLOCK construct at %C");
4022 my_ns
= gfc_build_block_ns (gfc_current_ns
);
4024 new_st
.op
= EXEC_BLOCK
;
4025 new_st
.ext
.block
.ns
= my_ns
;
4026 new_st
.ext
.block
.assoc
= NULL
;
4027 accept_statement (ST_BLOCK
);
4029 push_state (&s
, COMP_BLOCK
, my_ns
->proc_name
);
4030 gfc_current_ns
= my_ns
;
4031 my_parent
= my_ns
->parent
;
4033 parse_progunit (ST_NONE
);
4035 /* Don't depend on the value of gfc_current_ns; it might have been
4036 reset if the block had errors and was cleaned up. */
4037 gfc_current_ns
= my_parent
;
4043 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
4044 behind the scenes with compiler-generated variables. */
4047 parse_associate (void)
4049 gfc_namespace
* my_ns
;
4052 gfc_association_list
* a
;
4054 gfc_notify_std (GFC_STD_F2003
, "ASSOCIATE construct at %C");
4056 my_ns
= gfc_build_block_ns (gfc_current_ns
);
4058 new_st
.op
= EXEC_BLOCK
;
4059 new_st
.ext
.block
.ns
= my_ns
;
4060 gcc_assert (new_st
.ext
.block
.assoc
);
4062 /* Add all associate-names as BLOCK variables. Creating them is enough
4063 for now, they'll get their values during trans-* phase. */
4064 gfc_current_ns
= my_ns
;
4065 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
4069 gfc_array_ref
*array_ref
;
4071 if (gfc_get_sym_tree (a
->name
, NULL
, &a
->st
, false))
4075 sym
->attr
.flavor
= FL_VARIABLE
;
4077 sym
->declared_at
= a
->where
;
4078 gfc_set_sym_referenced (sym
);
4080 /* Initialize the typespec. It is not available in all cases,
4081 however, as it may only be set on the target during resolution.
4082 Still, sometimes it helps to have it right now -- especially
4083 for parsing component references on the associate-name
4084 in case of association to a derived-type. */
4085 sym
->ts
= a
->target
->ts
;
4087 /* Check if the target expression is array valued. This can not always
4088 be done by looking at target.rank, because that might not have been
4089 set yet. Therefore traverse the chain of refs, looking for the last
4090 array ref and evaluate that. */
4092 for (ref
= a
->target
->ref
; ref
; ref
= ref
->next
)
4093 if (ref
->type
== REF_ARRAY
)
4094 array_ref
= &ref
->u
.ar
;
4095 if (array_ref
|| a
->target
->rank
)
4102 /* Count the dimension, that have a non-scalar extend. */
4103 for (dim
= 0; dim
< array_ref
->dimen
; ++dim
)
4104 if (array_ref
->dimen_type
[dim
] != DIMEN_ELEMENT
4105 && !(array_ref
->dimen_type
[dim
] == DIMEN_UNKNOWN
4106 && array_ref
->end
[dim
] == NULL
4107 && array_ref
->start
[dim
] != NULL
))
4111 rank
= a
->target
->rank
;
4112 /* When the rank is greater than zero then sym will be an array. */
4113 if (sym
->ts
.type
== BT_CLASS
)
4115 if ((!CLASS_DATA (sym
)->as
&& rank
!= 0)
4116 || (CLASS_DATA (sym
)->as
4117 && CLASS_DATA (sym
)->as
->rank
!= rank
))
4119 /* Don't just (re-)set the attr and as in the sym.ts,
4120 because this modifies the target's attr and as. Copy the
4121 data and do a build_class_symbol. */
4122 symbol_attribute attr
= CLASS_DATA (a
->target
)->attr
;
4123 int corank
= gfc_get_corank (a
->target
);
4128 as
= gfc_get_array_spec ();
4129 as
->type
= AS_DEFERRED
;
4131 as
->corank
= corank
;
4132 attr
.dimension
= rank
? 1 : 0;
4133 attr
.codimension
= corank
? 1 : 0;
4138 attr
.dimension
= attr
.codimension
= 0;
4141 type
= CLASS_DATA (sym
)->ts
;
4142 if (!gfc_build_class_symbol (&type
,
4146 sym
->ts
.type
= BT_CLASS
;
4147 sym
->attr
.class_ok
= 1;
4150 sym
->attr
.class_ok
= 1;
4152 else if ((!sym
->as
&& rank
!= 0)
4153 || (sym
->as
&& sym
->as
->rank
!= rank
))
4155 as
= gfc_get_array_spec ();
4156 as
->type
= AS_DEFERRED
;
4158 as
->corank
= gfc_get_corank (a
->target
);
4160 sym
->attr
.dimension
= 1;
4162 sym
->attr
.codimension
= 1;
4167 accept_statement (ST_ASSOCIATE
);
4168 push_state (&s
, COMP_ASSOCIATE
, my_ns
->proc_name
);
4171 st
= parse_executable (ST_NONE
);
4178 accept_statement (st
);
4179 my_ns
->code
= gfc_state_stack
->head
;
4183 unexpected_statement (st
);
4187 gfc_current_ns
= gfc_current_ns
->parent
;
4192 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4193 handled inside of parse_executable(), because they aren't really
4197 parse_do_block (void)
4206 s
.ext
.end_do_label
= new_st
.label1
;
4208 if (new_st
.ext
.iterator
!= NULL
)
4209 stree
= new_st
.ext
.iterator
->var
->symtree
;
4213 accept_statement (ST_DO
);
4215 top
= gfc_state_stack
->tail
;
4216 push_state (&s
, do_op
== EXEC_DO_CONCURRENT
? COMP_DO_CONCURRENT
: COMP_DO
,
4219 s
.do_variable
= stree
;
4221 top
->block
= new_level (top
);
4222 top
->block
->op
= EXEC_DO
;
4225 st
= parse_executable (ST_NONE
);
4233 if (s
.ext
.end_do_label
!= NULL
4234 && s
.ext
.end_do_label
!= gfc_statement_label
)
4235 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4238 if (gfc_statement_label
!= NULL
)
4240 new_st
.op
= EXEC_NOP
;
4245 case ST_IMPLIED_ENDDO
:
4246 /* If the do-stmt of this DO construct has a do-construct-name,
4247 the corresponding end-do must be an end-do-stmt (with a matching
4248 name, but in that case we must have seen ST_ENDDO first).
4249 We only complain about this in pedantic mode. */
4250 if (gfc_current_block () != NULL
)
4251 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4252 &gfc_current_block()->declared_at
);
4257 unexpected_statement (st
);
4262 accept_statement (st
);
4266 /* Parse the statements of OpenMP do/parallel do. */
4268 static gfc_statement
4269 parse_omp_do (gfc_statement omp_st
)
4275 accept_statement (omp_st
);
4277 cp
= gfc_state_stack
->tail
;
4278 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
4279 np
= new_level (cp
);
4285 st
= next_statement ();
4288 else if (st
== ST_DO
)
4291 unexpected_statement (st
);
4295 if (gfc_statement_label
!= NULL
4296 && gfc_state_stack
->previous
!= NULL
4297 && gfc_state_stack
->previous
->state
== COMP_DO
4298 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
4306 there should be no !$OMP END DO. */
4308 return ST_IMPLIED_ENDDO
;
4311 check_do_closure ();
4314 st
= next_statement ();
4315 gfc_statement omp_end_st
= ST_OMP_END_DO
;
4318 case ST_OMP_DISTRIBUTE
: omp_end_st
= ST_OMP_END_DISTRIBUTE
; break;
4319 case ST_OMP_DISTRIBUTE_PARALLEL_DO
:
4320 omp_end_st
= ST_OMP_END_DISTRIBUTE_PARALLEL_DO
;
4322 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
4323 omp_end_st
= ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD
;
4325 case ST_OMP_DISTRIBUTE_SIMD
:
4326 omp_end_st
= ST_OMP_END_DISTRIBUTE_SIMD
;
4328 case ST_OMP_DO
: omp_end_st
= ST_OMP_END_DO
; break;
4329 case ST_OMP_DO_SIMD
: omp_end_st
= ST_OMP_END_DO_SIMD
; break;
4330 case ST_OMP_PARALLEL_DO
: omp_end_st
= ST_OMP_END_PARALLEL_DO
; break;
4331 case ST_OMP_PARALLEL_DO_SIMD
:
4332 omp_end_st
= ST_OMP_END_PARALLEL_DO_SIMD
;
4334 case ST_OMP_SIMD
: omp_end_st
= ST_OMP_END_SIMD
; break;
4335 case ST_OMP_TARGET_TEAMS_DISTRIBUTE
:
4336 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE
;
4338 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4339 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
;
4341 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4342 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
;
4344 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
4345 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD
;
4347 case ST_OMP_TEAMS_DISTRIBUTE
:
4348 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE
;
4350 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4351 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO
;
4353 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4354 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
;
4356 case ST_OMP_TEAMS_DISTRIBUTE_SIMD
:
4357 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_SIMD
;
4359 default: gcc_unreachable ();
4361 if (st
== omp_end_st
)
4363 if (new_st
.op
== EXEC_OMP_END_NOWAIT
)
4364 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
4366 gcc_assert (new_st
.op
== EXEC_NOP
);
4367 gfc_clear_new_st ();
4368 gfc_commit_symbols ();
4369 gfc_warning_check ();
4370 st
= next_statement ();
4376 /* Parse the statements of OpenMP atomic directive. */
4378 static gfc_statement
4379 parse_omp_oacc_atomic (bool omp_p
)
4381 gfc_statement st
, st_atomic
, st_end_atomic
;
4388 st_atomic
= ST_OMP_ATOMIC
;
4389 st_end_atomic
= ST_OMP_END_ATOMIC
;
4393 st_atomic
= ST_OACC_ATOMIC
;
4394 st_end_atomic
= ST_OACC_END_ATOMIC
;
4396 accept_statement (st_atomic
);
4398 cp
= gfc_state_stack
->tail
;
4399 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
4400 np
= new_level (cp
);
4403 count
= 1 + ((cp
->ext
.omp_atomic
& GFC_OMP_ATOMIC_MASK
)
4404 == GFC_OMP_ATOMIC_CAPTURE
);
4408 st
= next_statement ();
4411 else if (st
== ST_ASSIGNMENT
)
4413 accept_statement (st
);
4417 unexpected_statement (st
);
4422 st
= next_statement ();
4423 if (st
== st_end_atomic
)
4425 gfc_clear_new_st ();
4426 gfc_commit_symbols ();
4427 gfc_warning_check ();
4428 st
= next_statement ();
4430 else if ((cp
->ext
.omp_atomic
& GFC_OMP_ATOMIC_MASK
)
4431 == GFC_OMP_ATOMIC_CAPTURE
)
4432 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4437 /* Parse the statements of an OpenACC structured block. */
4440 parse_oacc_structured_block (gfc_statement acc_st
)
4442 gfc_statement st
, acc_end_st
;
4444 gfc_state_data s
, *sd
;
4446 for (sd
= gfc_state_stack
; sd
; sd
= sd
->previous
)
4447 if (sd
->state
== COMP_CRITICAL
)
4448 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4450 accept_statement (acc_st
);
4452 cp
= gfc_state_stack
->tail
;
4453 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
4454 np
= new_level (cp
);
4459 case ST_OACC_PARALLEL
:
4460 acc_end_st
= ST_OACC_END_PARALLEL
;
4462 case ST_OACC_KERNELS
:
4463 acc_end_st
= ST_OACC_END_KERNELS
;
4466 acc_end_st
= ST_OACC_END_DATA
;
4468 case ST_OACC_HOST_DATA
:
4469 acc_end_st
= ST_OACC_END_HOST_DATA
;
4477 st
= parse_executable (ST_NONE
);
4480 else if (st
!= acc_end_st
)
4482 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st
));
4483 reject_statement ();
4486 while (st
!= acc_end_st
);
4488 gcc_assert (new_st
.op
== EXEC_NOP
);
4490 gfc_clear_new_st ();
4491 gfc_commit_symbols ();
4492 gfc_warning_check ();
4496 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4498 static gfc_statement
4499 parse_oacc_loop (gfc_statement acc_st
)
4503 gfc_state_data s
, *sd
;
4505 for (sd
= gfc_state_stack
; sd
; sd
= sd
->previous
)
4506 if (sd
->state
== COMP_CRITICAL
)
4507 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4509 accept_statement (acc_st
);
4511 cp
= gfc_state_stack
->tail
;
4512 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
4513 np
= new_level (cp
);
4519 st
= next_statement ();
4522 else if (st
== ST_DO
)
4526 gfc_error ("Expected DO loop at %C");
4527 reject_statement ();
4532 if (gfc_statement_label
!= NULL
4533 && gfc_state_stack
->previous
!= NULL
4534 && gfc_state_stack
->previous
->state
== COMP_DO
4535 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
4538 return ST_IMPLIED_ENDDO
;
4541 check_do_closure ();
4544 st
= next_statement ();
4545 if (st
== ST_OACC_END_LOOP
)
4546 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
4547 if ((acc_st
== ST_OACC_PARALLEL_LOOP
&& st
== ST_OACC_END_PARALLEL_LOOP
) ||
4548 (acc_st
== ST_OACC_KERNELS_LOOP
&& st
== ST_OACC_END_KERNELS_LOOP
) ||
4549 (acc_st
== ST_OACC_LOOP
&& st
== ST_OACC_END_LOOP
))
4551 gcc_assert (new_st
.op
== EXEC_NOP
);
4552 gfc_clear_new_st ();
4553 gfc_commit_symbols ();
4554 gfc_warning_check ();
4555 st
= next_statement ();
4561 /* Parse the statements of an OpenMP structured block. */
4564 parse_omp_structured_block (gfc_statement omp_st
, bool workshare_stmts_only
)
4566 gfc_statement st
, omp_end_st
;
4570 accept_statement (omp_st
);
4572 cp
= gfc_state_stack
->tail
;
4573 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
4574 np
= new_level (cp
);
4580 case ST_OMP_PARALLEL
:
4581 omp_end_st
= ST_OMP_END_PARALLEL
;
4583 case ST_OMP_PARALLEL_SECTIONS
:
4584 omp_end_st
= ST_OMP_END_PARALLEL_SECTIONS
;
4586 case ST_OMP_SECTIONS
:
4587 omp_end_st
= ST_OMP_END_SECTIONS
;
4589 case ST_OMP_ORDERED
:
4590 omp_end_st
= ST_OMP_END_ORDERED
;
4592 case ST_OMP_CRITICAL
:
4593 omp_end_st
= ST_OMP_END_CRITICAL
;
4596 omp_end_st
= ST_OMP_END_MASTER
;
4599 omp_end_st
= ST_OMP_END_SINGLE
;
4602 omp_end_st
= ST_OMP_END_TARGET
;
4604 case ST_OMP_TARGET_DATA
:
4605 omp_end_st
= ST_OMP_END_TARGET_DATA
;
4607 case ST_OMP_TARGET_TEAMS
:
4608 omp_end_st
= ST_OMP_END_TARGET_TEAMS
;
4610 case ST_OMP_TARGET_TEAMS_DISTRIBUTE
:
4611 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE
;
4613 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4614 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
;
4616 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4617 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
;
4619 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
4620 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD
;
4623 omp_end_st
= ST_OMP_END_TASK
;
4625 case ST_OMP_TASKGROUP
:
4626 omp_end_st
= ST_OMP_END_TASKGROUP
;
4629 omp_end_st
= ST_OMP_END_TEAMS
;
4631 case ST_OMP_TEAMS_DISTRIBUTE
:
4632 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE
;
4634 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4635 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO
;
4637 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4638 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
;
4640 case ST_OMP_TEAMS_DISTRIBUTE_SIMD
:
4641 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_SIMD
;
4643 case ST_OMP_DISTRIBUTE
:
4644 omp_end_st
= ST_OMP_END_DISTRIBUTE
;
4646 case ST_OMP_DISTRIBUTE_PARALLEL_DO
:
4647 omp_end_st
= ST_OMP_END_DISTRIBUTE_PARALLEL_DO
;
4649 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
4650 omp_end_st
= ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD
;
4652 case ST_OMP_DISTRIBUTE_SIMD
:
4653 omp_end_st
= ST_OMP_END_DISTRIBUTE_SIMD
;
4655 case ST_OMP_WORKSHARE
:
4656 omp_end_st
= ST_OMP_END_WORKSHARE
;
4658 case ST_OMP_PARALLEL_WORKSHARE
:
4659 omp_end_st
= ST_OMP_END_PARALLEL_WORKSHARE
;
4667 if (workshare_stmts_only
)
4669 /* Inside of !$omp workshare, only
4672 where statements and constructs
4673 forall statements and constructs
4677 are allowed. For !$omp critical these
4678 restrictions apply recursively. */
4681 st
= next_statement ();
4692 accept_statement (st
);
4695 case ST_WHERE_BLOCK
:
4696 parse_where_block ();
4699 case ST_FORALL_BLOCK
:
4700 parse_forall_block ();
4703 case ST_OMP_PARALLEL
:
4704 case ST_OMP_PARALLEL_SECTIONS
:
4705 parse_omp_structured_block (st
, false);
4708 case ST_OMP_PARALLEL_WORKSHARE
:
4709 case ST_OMP_CRITICAL
:
4710 parse_omp_structured_block (st
, true);
4713 case ST_OMP_PARALLEL_DO
:
4714 case ST_OMP_PARALLEL_DO_SIMD
:
4715 st
= parse_omp_do (st
);
4719 st
= parse_omp_oacc_atomic (true);
4730 st
= next_statement ();
4734 st
= parse_executable (ST_NONE
);
4737 else if (st
== ST_OMP_SECTION
4738 && (omp_st
== ST_OMP_SECTIONS
4739 || omp_st
== ST_OMP_PARALLEL_SECTIONS
))
4741 np
= new_level (np
);
4745 else if (st
!= omp_end_st
)
4746 unexpected_statement (st
);
4748 while (st
!= omp_end_st
);
4752 case EXEC_OMP_END_NOWAIT
:
4753 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
4755 case EXEC_OMP_CRITICAL
:
4756 if (((cp
->ext
.omp_name
== NULL
) ^ (new_st
.ext
.omp_name
== NULL
))
4757 || (new_st
.ext
.omp_name
!= NULL
4758 && strcmp (cp
->ext
.omp_name
, new_st
.ext
.omp_name
) != 0))
4759 gfc_error ("Name after !$omp critical and !$omp end critical does "
4761 free (CONST_CAST (char *, new_st
.ext
.omp_name
));
4763 case EXEC_OMP_END_SINGLE
:
4764 cp
->ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
]
4765 = new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
];
4766 new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
] = NULL
;
4767 gfc_free_omp_clauses (new_st
.ext
.omp_clauses
);
4775 gfc_clear_new_st ();
4776 gfc_commit_symbols ();
4777 gfc_warning_check ();
4782 /* Accept a series of executable statements. We return the first
4783 statement that doesn't fit to the caller. Any block statements are
4784 passed on to the correct handler, which usually passes the buck
4787 static gfc_statement
4788 parse_executable (gfc_statement st
)
4793 st
= next_statement ();
4797 close_flag
= check_do_closure ();
4802 case ST_END_PROGRAM
:
4805 case ST_END_FUNCTION
:
4810 case ST_END_SUBROUTINE
:
4815 case ST_SELECT_CASE
:
4816 gfc_error ("%s statement at %C cannot terminate a non-block "
4817 "DO loop", gfc_ascii_statement (st
));
4830 gfc_notify_std (GFC_STD_F95_OBS
, "DATA statement at %C after the "
4831 "first executable statement");
4837 accept_statement (st
);
4838 if (close_flag
== 1)
4839 return ST_IMPLIED_ENDDO
;
4843 parse_block_construct ();
4854 case ST_SELECT_CASE
:
4855 parse_select_block ();
4858 case ST_SELECT_TYPE
:
4859 parse_select_type_block();
4864 if (check_do_closure () == 1)
4865 return ST_IMPLIED_ENDDO
;
4869 parse_critical_block ();
4872 case ST_WHERE_BLOCK
:
4873 parse_where_block ();
4876 case ST_FORALL_BLOCK
:
4877 parse_forall_block ();
4880 case ST_OACC_PARALLEL_LOOP
:
4881 case ST_OACC_KERNELS_LOOP
:
4883 st
= parse_oacc_loop (st
);
4884 if (st
== ST_IMPLIED_ENDDO
)
4888 case ST_OACC_PARALLEL
:
4889 case ST_OACC_KERNELS
:
4891 case ST_OACC_HOST_DATA
:
4892 parse_oacc_structured_block (st
);
4895 case ST_OMP_PARALLEL
:
4896 case ST_OMP_PARALLEL_SECTIONS
:
4897 case ST_OMP_SECTIONS
:
4898 case ST_OMP_ORDERED
:
4899 case ST_OMP_CRITICAL
:
4903 case ST_OMP_TARGET_DATA
:
4904 case ST_OMP_TARGET_TEAMS
:
4907 case ST_OMP_TASKGROUP
:
4908 parse_omp_structured_block (st
, false);
4911 case ST_OMP_WORKSHARE
:
4912 case ST_OMP_PARALLEL_WORKSHARE
:
4913 parse_omp_structured_block (st
, true);
4916 case ST_OMP_DISTRIBUTE
:
4917 case ST_OMP_DISTRIBUTE_PARALLEL_DO
:
4918 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
4919 case ST_OMP_DISTRIBUTE_SIMD
:
4921 case ST_OMP_DO_SIMD
:
4922 case ST_OMP_PARALLEL_DO
:
4923 case ST_OMP_PARALLEL_DO_SIMD
:
4925 case ST_OMP_TARGET_TEAMS_DISTRIBUTE
:
4926 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4927 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4928 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
4929 case ST_OMP_TEAMS_DISTRIBUTE
:
4930 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4931 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4932 case ST_OMP_TEAMS_DISTRIBUTE_SIMD
:
4933 st
= parse_omp_do (st
);
4934 if (st
== ST_IMPLIED_ENDDO
)
4938 case ST_OACC_ATOMIC
:
4939 st
= parse_omp_oacc_atomic (false);
4943 st
= parse_omp_oacc_atomic (true);
4950 st
= next_statement ();
4955 /* Fix the symbols for sibling functions. These are incorrectly added to
4956 the child namespace as the parser didn't know about this procedure. */
4959 gfc_fixup_sibling_symbols (gfc_symbol
*sym
, gfc_namespace
*siblings
)
4963 gfc_symbol
*old_sym
;
4965 for (ns
= siblings
; ns
; ns
= ns
->sibling
)
4967 st
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
4969 if (!st
|| (st
->n
.sym
->attr
.dummy
&& ns
== st
->n
.sym
->ns
))
4970 goto fixup_contained
;
4972 if ((st
->n
.sym
->attr
.flavor
== FL_DERIVED
4973 && sym
->attr
.generic
&& sym
->attr
.function
)
4974 ||(sym
->attr
.flavor
== FL_DERIVED
4975 && st
->n
.sym
->attr
.generic
&& st
->n
.sym
->attr
.function
))
4976 goto fixup_contained
;
4978 old_sym
= st
->n
.sym
;
4979 if (old_sym
->ns
== ns
4980 && !old_sym
->attr
.contained
4982 /* By 14.6.1.3, host association should be excluded
4983 for the following. */
4984 && !(old_sym
->attr
.external
4985 || (old_sym
->ts
.type
!= BT_UNKNOWN
4986 && !old_sym
->attr
.implicit_type
)
4987 || old_sym
->attr
.flavor
== FL_PARAMETER
4988 || old_sym
->attr
.use_assoc
4989 || old_sym
->attr
.in_common
4990 || old_sym
->attr
.in_equivalence
4991 || old_sym
->attr
.data
4992 || old_sym
->attr
.dummy
4993 || old_sym
->attr
.result
4994 || old_sym
->attr
.dimension
4995 || old_sym
->attr
.allocatable
4996 || old_sym
->attr
.intrinsic
4997 || old_sym
->attr
.generic
4998 || old_sym
->attr
.flavor
== FL_NAMELIST
4999 || old_sym
->attr
.flavor
== FL_LABEL
5000 || old_sym
->attr
.proc
== PROC_ST_FUNCTION
))
5002 /* Replace it with the symbol from the parent namespace. */
5006 gfc_release_symbol (old_sym
);
5010 /* Do the same for any contained procedures. */
5011 gfc_fixup_sibling_symbols (sym
, ns
->contained
);
5016 parse_contained (int module
)
5018 gfc_namespace
*ns
, *parent_ns
, *tmp
;
5019 gfc_state_data s1
, s2
;
5023 int contains_statements
= 0;
5026 push_state (&s1
, COMP_CONTAINS
, NULL
);
5027 parent_ns
= gfc_current_ns
;
5031 gfc_current_ns
= gfc_get_namespace (parent_ns
, 1);
5033 gfc_current_ns
->sibling
= parent_ns
->contained
;
5034 parent_ns
->contained
= gfc_current_ns
;
5037 /* Process the next available statement. We come here if we got an error
5038 and rejected the last statement. */
5039 st
= next_statement ();
5048 contains_statements
= 1;
5049 accept_statement (st
);
5052 (st
== ST_FUNCTION
) ? COMP_FUNCTION
: COMP_SUBROUTINE
,
5055 /* For internal procedures, create/update the symbol in the
5056 parent namespace. */
5060 if (gfc_get_symbol (gfc_new_block
->name
, parent_ns
, &sym
))
5061 gfc_error ("Contained procedure %qs at %C is already "
5062 "ambiguous", gfc_new_block
->name
);
5065 if (gfc_add_procedure (&sym
->attr
, PROC_INTERNAL
,
5067 &gfc_new_block
->declared_at
))
5069 if (st
== ST_FUNCTION
)
5070 gfc_add_function (&sym
->attr
, sym
->name
,
5071 &gfc_new_block
->declared_at
);
5073 gfc_add_subroutine (&sym
->attr
, sym
->name
,
5074 &gfc_new_block
->declared_at
);
5078 gfc_commit_symbols ();
5081 sym
= gfc_new_block
;
5083 /* Mark this as a contained function, so it isn't replaced
5084 by other module functions. */
5085 sym
->attr
.contained
= 1;
5087 /* Set implicit_pure so that it can be reset if any of the
5088 tests for purity fail. This is used for some optimisation
5089 during translation. */
5090 if (!sym
->attr
.pure
)
5091 sym
->attr
.implicit_pure
= 1;
5093 parse_progunit (ST_NONE
);
5095 /* Fix up any sibling functions that refer to this one. */
5096 gfc_fixup_sibling_symbols (sym
, gfc_current_ns
);
5097 /* Or refer to any of its alternate entry points. */
5098 for (el
= gfc_current_ns
->entries
; el
; el
= el
->next
)
5099 gfc_fixup_sibling_symbols (el
->sym
, gfc_current_ns
);
5101 gfc_current_ns
->code
= s2
.head
;
5102 gfc_current_ns
= parent_ns
;
5107 /* These statements are associated with the end of the host unit. */
5108 case ST_END_FUNCTION
:
5110 case ST_END_SUBMODULE
:
5111 case ST_END_PROGRAM
:
5112 case ST_END_SUBROUTINE
:
5113 accept_statement (st
);
5114 gfc_current_ns
->code
= s1
.head
;
5118 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
5119 gfc_ascii_statement (st
));
5120 reject_statement ();
5126 while (st
!= ST_END_FUNCTION
&& st
!= ST_END_SUBROUTINE
5127 && st
!= ST_END_MODULE
&& st
!= ST_END_SUBMODULE
5128 && st
!= ST_END_PROGRAM
);
5130 /* The first namespace in the list is guaranteed to not have
5131 anything (worthwhile) in it. */
5132 tmp
= gfc_current_ns
;
5133 gfc_current_ns
= parent_ns
;
5134 if (seen_error
&& tmp
->refs
> 1)
5135 gfc_free_namespace (tmp
);
5137 ns
= gfc_current_ns
->contained
;
5138 gfc_current_ns
->contained
= ns
->sibling
;
5139 gfc_free_namespace (ns
);
5142 if (!contains_statements
)
5143 gfc_notify_std (GFC_STD_F2008
, "CONTAINS statement without "
5144 "FUNCTION or SUBROUTINE statement at %C");
5148 /* The result variable in a MODULE PROCEDURE needs to be created and
5149 its characteristics copied from the interface since it is neither
5150 declared in the procedure declaration nor in the specification
5154 get_modproc_result (void)
5157 if (gfc_state_stack
->previous
5158 && gfc_state_stack
->previous
->state
== COMP_CONTAINS
5159 && gfc_state_stack
->previous
->previous
->state
== COMP_SUBMODULE
)
5161 proc
= gfc_current_ns
->proc_name
? gfc_current_ns
->proc_name
: NULL
;
5163 && proc
->attr
.function
5164 && proc
->ts
.interface
5165 && proc
->ts
.interface
->result
5166 && proc
->ts
.interface
->result
!= proc
->ts
.interface
)
5168 gfc_copy_dummy_sym (&proc
->result
, proc
->ts
.interface
->result
, 1);
5169 gfc_set_sym_referenced (proc
->result
);
5170 proc
->result
->attr
.if_source
= IFSRC_DECL
;
5171 gfc_commit_symbol (proc
->result
);
5177 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
5180 parse_progunit (gfc_statement st
)
5186 && gfc_new_block
->abr_modproc_decl
5187 && gfc_new_block
->attr
.function
)
5188 get_modproc_result ();
5190 st
= parse_spec (st
);
5197 /* This is not allowed within BLOCK! */
5198 if (gfc_current_state () != COMP_BLOCK
)
5203 accept_statement (st
);
5210 if (gfc_current_state () == COMP_FUNCTION
)
5211 gfc_check_function_type (gfc_current_ns
);
5216 st
= parse_executable (st
);
5224 /* This is not allowed within BLOCK! */
5225 if (gfc_current_state () != COMP_BLOCK
)
5230 accept_statement (st
);
5237 unexpected_statement (st
);
5238 reject_statement ();
5239 st
= next_statement ();
5245 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
5246 if (p
->state
== COMP_CONTAINS
)
5249 if (gfc_find_state (COMP_MODULE
) == true
5250 || gfc_find_state (COMP_SUBMODULE
) == true)
5255 gfc_error ("CONTAINS statement at %C is already in a contained "
5257 reject_statement ();
5258 st
= next_statement ();
5262 parse_contained (0);
5265 gfc_current_ns
->code
= gfc_state_stack
->head
;
5269 /* Come here to complain about a global symbol already in use as
5273 gfc_global_used (gfc_gsymbol
*sym
, locus
*where
)
5278 where
= &gfc_current_locus
;
5288 case GSYM_SUBROUTINE
:
5289 name
= "SUBROUTINE";
5294 case GSYM_BLOCK_DATA
:
5295 name
= "BLOCK DATA";
5301 gfc_internal_error ("gfc_global_used(): Bad type");
5305 if (sym
->binding_label
)
5306 gfc_error ("Global binding name %qs at %L is already being used as a %s "
5307 "at %L", sym
->binding_label
, where
, name
, &sym
->where
);
5309 gfc_error ("Global name %qs at %L is already being used as a %s at %L",
5310 sym
->name
, where
, name
, &sym
->where
);
5314 /* Parse a block data program unit. */
5317 parse_block_data (void)
5320 static locus blank_locus
;
5321 static int blank_block
=0;
5324 gfc_current_ns
->proc_name
= gfc_new_block
;
5325 gfc_current_ns
->is_block_data
= 1;
5327 if (gfc_new_block
== NULL
)
5330 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5331 "prior BLOCK DATA at %L", &blank_locus
);
5335 blank_locus
= gfc_current_locus
;
5340 s
= gfc_get_gsymbol (gfc_new_block
->name
);
5342 || (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_BLOCK_DATA
))
5343 gfc_global_used (s
, &gfc_new_block
->declared_at
);
5346 s
->type
= GSYM_BLOCK_DATA
;
5347 s
->where
= gfc_new_block
->declared_at
;
5352 st
= parse_spec (ST_NONE
);
5354 while (st
!= ST_END_BLOCK_DATA
)
5356 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5357 gfc_ascii_statement (st
));
5358 reject_statement ();
5359 st
= next_statement ();
5364 /* Following the association of the ancestor (sub)module symbols, they
5365 must be set host rather than use associated and all must be public.
5366 They are flagged up by 'used_in_submodule' so that they can be set
5367 DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl). Otherwise the
5368 linker chokes on multiple symbol definitions. */
5371 set_syms_host_assoc (gfc_symbol
*sym
)
5378 if (sym
->attr
.module_procedure
)
5379 sym
->attr
.external
= 0;
5381 /* sym->attr.access = ACCESS_PUBLIC; */
5383 sym
->attr
.use_assoc
= 0;
5384 sym
->attr
.host_assoc
= 1;
5385 sym
->attr
.used_in_submodule
=1;
5387 if (sym
->attr
.flavor
== FL_DERIVED
)
5389 for (c
= sym
->components
; c
; c
= c
->next
)
5390 c
->attr
.access
= ACCESS_PUBLIC
;
5394 /* Parse a module subprogram. */
5403 s
= gfc_get_gsymbol (gfc_new_block
->name
);
5404 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_MODULE
))
5405 gfc_global_used (s
, &gfc_new_block
->declared_at
);
5408 s
->type
= GSYM_MODULE
;
5409 s
->where
= gfc_new_block
->declared_at
;
5413 /* Something is nulling the module_list after this point. This is good
5414 since it allows us to 'USE' the parent modules that the submodule
5415 inherits and to set (most) of the symbols as host associated. */
5416 if (gfc_current_state () == COMP_SUBMODULE
)
5419 gfc_traverse_ns (gfc_current_ns
, set_syms_host_assoc
);
5422 st
= parse_spec (ST_NONE
);
5432 parse_contained (1);
5436 case ST_END_SUBMODULE
:
5437 accept_statement (st
);
5441 gfc_error ("Unexpected %s statement in MODULE at %C",
5442 gfc_ascii_statement (st
));
5445 reject_statement ();
5446 st
= next_statement ();
5450 /* Make sure not to free the namespace twice on error. */
5452 s
->ns
= gfc_current_ns
;
5456 /* Add a procedure name to the global symbol table. */
5459 add_global_procedure (bool sub
)
5463 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5464 name is a global identifier. */
5465 if (!gfc_new_block
->binding_label
|| gfc_notification_std (GFC_STD_F2008
))
5467 s
= gfc_get_gsymbol (gfc_new_block
->name
);
5470 || (s
->type
!= GSYM_UNKNOWN
5471 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
5473 gfc_global_used (s
, &gfc_new_block
->declared_at
);
5474 /* Silence follow-up errors. */
5475 gfc_new_block
->binding_label
= NULL
;
5479 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5480 s
->sym_name
= gfc_new_block
->name
;
5481 s
->where
= gfc_new_block
->declared_at
;
5483 s
->ns
= gfc_current_ns
;
5487 /* Don't add the symbol multiple times. */
5488 if (gfc_new_block
->binding_label
5489 && (!gfc_notification_std (GFC_STD_F2008
)
5490 || strcmp (gfc_new_block
->name
, gfc_new_block
->binding_label
) != 0))
5492 s
= gfc_get_gsymbol (gfc_new_block
->binding_label
);
5495 || (s
->type
!= GSYM_UNKNOWN
5496 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
5498 gfc_global_used (s
, &gfc_new_block
->declared_at
);
5499 /* Silence follow-up errors. */
5500 gfc_new_block
->binding_label
= NULL
;
5504 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5505 s
->sym_name
= gfc_new_block
->name
;
5506 s
->binding_label
= gfc_new_block
->binding_label
;
5507 s
->where
= gfc_new_block
->declared_at
;
5509 s
->ns
= gfc_current_ns
;
5515 /* Add a program to the global symbol table. */
5518 add_global_program (void)
5522 if (gfc_new_block
== NULL
)
5524 s
= gfc_get_gsymbol (gfc_new_block
->name
);
5526 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_PROGRAM
))
5527 gfc_global_used (s
, &gfc_new_block
->declared_at
);
5530 s
->type
= GSYM_PROGRAM
;
5531 s
->where
= gfc_new_block
->declared_at
;
5533 s
->ns
= gfc_current_ns
;
5538 /* Resolve all the program units. */
5540 resolve_all_program_units (gfc_namespace
*gfc_global_ns_list
)
5542 gfc_free_dt_list ();
5543 gfc_current_ns
= gfc_global_ns_list
;
5544 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
5546 if (gfc_current_ns
->proc_name
5547 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
5548 continue; /* Already resolved. */
5550 if (gfc_current_ns
->proc_name
)
5551 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
5552 gfc_resolve (gfc_current_ns
);
5553 gfc_current_ns
->derived_types
= gfc_derived_types
;
5554 gfc_derived_types
= NULL
;
5560 clean_up_modules (gfc_gsymbol
*gsym
)
5565 clean_up_modules (gsym
->left
);
5566 clean_up_modules (gsym
->right
);
5568 if (gsym
->type
!= GSYM_MODULE
|| !gsym
->ns
)
5571 gfc_current_ns
= gsym
->ns
;
5572 gfc_derived_types
= gfc_current_ns
->derived_types
;
5579 /* Translate all the program units. This could be in a different order
5580 to resolution if there are forward references in the file. */
5582 translate_all_program_units (gfc_namespace
*gfc_global_ns_list
)
5586 gfc_current_ns
= gfc_global_ns_list
;
5587 gfc_get_errors (NULL
, &errors
);
5589 /* We first translate all modules to make sure that later parts
5590 of the program can use the decl. Then we translate the nonmodules. */
5592 for (; !errors
&& gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
5594 if (!gfc_current_ns
->proc_name
5595 || gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5598 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
5599 gfc_derived_types
= gfc_current_ns
->derived_types
;
5600 gfc_generate_module_code (gfc_current_ns
);
5601 gfc_current_ns
->translated
= 1;
5604 gfc_current_ns
= gfc_global_ns_list
;
5605 for (; !errors
&& gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
5607 if (gfc_current_ns
->proc_name
5608 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
5611 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
5612 gfc_derived_types
= gfc_current_ns
->derived_types
;
5613 gfc_generate_code (gfc_current_ns
);
5614 gfc_current_ns
->translated
= 1;
5617 /* Clean up all the namespaces after translation. */
5618 gfc_current_ns
= gfc_global_ns_list
;
5619 for (;gfc_current_ns
;)
5623 if (gfc_current_ns
->proc_name
5624 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
5626 gfc_current_ns
= gfc_current_ns
->sibling
;
5630 ns
= gfc_current_ns
->sibling
;
5631 gfc_derived_types
= gfc_current_ns
->derived_types
;
5633 gfc_current_ns
= ns
;
5636 clean_up_modules (gfc_gsym_root
);
5640 /* Top level parser. */
5643 gfc_parse_file (void)
5645 int seen_program
, errors_before
, errors
;
5646 gfc_state_data top
, s
;
5649 gfc_namespace
*next
;
5651 gfc_start_source_files ();
5653 top
.state
= COMP_NONE
;
5655 top
.previous
= NULL
;
5656 top
.head
= top
.tail
= NULL
;
5657 top
.do_variable
= NULL
;
5659 gfc_state_stack
= &top
;
5661 gfc_clear_new_st ();
5663 gfc_statement_label
= NULL
;
5665 if (setjmp (eof_buf
))
5666 return false; /* Come here on unexpected EOF */
5668 /* Prepare the global namespace that will contain the
5670 gfc_global_ns_list
= next
= NULL
;
5675 /* Exit early for empty files. */
5679 in_specification_block
= true;
5682 st
= next_statement ();
5691 goto duplicate_main
;
5693 prog_locus
= gfc_current_locus
;
5695 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
5696 main_program_symbol(gfc_current_ns
, gfc_new_block
->name
);
5697 accept_statement (st
);
5698 add_global_program ();
5699 parse_progunit (ST_NONE
);
5704 add_global_procedure (true);
5705 push_state (&s
, COMP_SUBROUTINE
, gfc_new_block
);
5706 accept_statement (st
);
5707 parse_progunit (ST_NONE
);
5712 add_global_procedure (false);
5713 push_state (&s
, COMP_FUNCTION
, gfc_new_block
);
5714 accept_statement (st
);
5715 parse_progunit (ST_NONE
);
5720 push_state (&s
, COMP_BLOCK_DATA
, gfc_new_block
);
5721 accept_statement (st
);
5722 parse_block_data ();
5726 push_state (&s
, COMP_MODULE
, gfc_new_block
);
5727 accept_statement (st
);
5729 gfc_get_errors (NULL
, &errors_before
);
5734 push_state (&s
, COMP_SUBMODULE
, gfc_new_block
);
5735 accept_statement (st
);
5737 gfc_get_errors (NULL
, &errors_before
);
5741 /* Anything else starts a nameless main program block. */
5744 goto duplicate_main
;
5746 prog_locus
= gfc_current_locus
;
5748 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
5749 main_program_symbol (gfc_current_ns
, "MAIN__");
5750 parse_progunit (st
);
5755 /* Handle the non-program units. */
5756 gfc_current_ns
->code
= s
.head
;
5758 gfc_resolve (gfc_current_ns
);
5760 /* Dump the parse tree if requested. */
5761 if (flag_dump_fortran_original
)
5762 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
5764 gfc_get_errors (NULL
, &errors
);
5765 if (s
.state
== COMP_MODULE
|| s
.state
== COMP_SUBMODULE
)
5767 gfc_dump_module (s
.sym
->name
, errors_before
== errors
);
5768 gfc_current_ns
->derived_types
= gfc_derived_types
;
5769 gfc_derived_types
= NULL
;
5775 gfc_generate_code (gfc_current_ns
);
5783 /* The main program and non-contained procedures are put
5784 in the global namespace list, so that they can be processed
5785 later and all their interfaces resolved. */
5786 gfc_current_ns
->code
= s
.head
;
5789 for (; next
->sibling
; next
= next
->sibling
)
5791 next
->sibling
= gfc_current_ns
;
5794 gfc_global_ns_list
= gfc_current_ns
;
5796 next
= gfc_current_ns
;
5803 /* Do the resolution. */
5804 resolve_all_program_units (gfc_global_ns_list
);
5806 /* Do the parse tree dump. */
5808 = flag_dump_fortran_original
? gfc_global_ns_list
: NULL
;
5810 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
5811 if (!gfc_current_ns
->proc_name
5812 || gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5814 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
5815 fputs ("------------------------------------------\n\n", stdout
);
5818 /* Do the translation. */
5819 translate_all_program_units (gfc_global_ns_list
);
5821 gfc_end_source_files ();
5825 /* If we see a duplicate main program, shut down. If the second
5826 instance is an implied main program, i.e. data decls or executable
5827 statements, we're in for lots of errors. */
5828 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus
);
5829 reject_statement ();
5834 /* Return true if this state data represents an OpenACC region. */
5836 is_oacc (gfc_state_data
*sd
)
5838 switch (sd
->construct
->op
)
5840 case EXEC_OACC_PARALLEL_LOOP
:
5841 case EXEC_OACC_PARALLEL
:
5842 case EXEC_OACC_KERNELS_LOOP
:
5843 case EXEC_OACC_KERNELS
:
5844 case EXEC_OACC_DATA
:
5845 case EXEC_OACC_HOST_DATA
:
5846 case EXEC_OACC_LOOP
:
5847 case EXEC_OACC_UPDATE
:
5848 case EXEC_OACC_WAIT
:
5849 case EXEC_OACC_CACHE
:
5850 case EXEC_OACC_ENTER_DATA
:
5851 case EXEC_OACC_EXIT_DATA
:
5852 case EXEC_OACC_ATOMIC
:
5853 case EXEC_OACC_ROUTINE
: