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
);
259 match ("structure", gfc_match_structure_decl
, ST_STRUCTURE_DECL
);
263 match ("target", gfc_match_target
, ST_ATTR_DECL
);
264 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
271 match ("value", gfc_match_value
, ST_ATTR_DECL
);
272 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
279 /* This is not a specification statement. See if any of the matchers
280 has stored an error message of some sort. */
284 gfc_buffer_error (false);
285 gfc_current_locus
= old_locus
;
287 return ST_GET_FCN_CHARACTERISTICS
;
290 static bool in_specification_block
;
292 /* This is the primary 'decode_statement'. */
294 decode_statement (void)
302 gfc_enforce_clean_symbol_state ();
304 gfc_clear_error (); /* Clear any pending errors. */
305 gfc_clear_warning (); /* Clear any pending warnings. */
307 gfc_matching_function
= false;
309 if (gfc_match_eos () == MATCH_YES
)
312 if (gfc_current_state () == COMP_FUNCTION
313 && gfc_current_block ()->result
->ts
.kind
== -1)
314 return decode_specification_statement ();
316 old_locus
= gfc_current_locus
;
318 c
= gfc_peek_ascii_char ();
322 if (match_word ("use", gfc_match_use
, &old_locus
) == MATCH_YES
)
324 last_was_use_stmt
= true;
328 undo_new_statement ();
331 if (last_was_use_stmt
)
334 /* Try matching a data declaration or function declaration. The
335 input "REALFUNCTIONA(N)" can mean several things in different
336 contexts, so it (and its relatives) get special treatment. */
338 if (gfc_current_state () == COMP_NONE
339 || gfc_current_state () == COMP_INTERFACE
340 || gfc_current_state () == COMP_CONTAINS
)
342 gfc_matching_function
= true;
343 m
= gfc_match_function_decl ();
346 else if (m
== MATCH_ERROR
)
350 gfc_current_locus
= old_locus
;
352 gfc_matching_function
= false;
355 /* Match statements whose error messages are meant to be overwritten
356 by something better. */
358 match (NULL
, gfc_match_assignment
, ST_ASSIGNMENT
);
359 match (NULL
, gfc_match_pointer_assignment
, ST_POINTER_ASSIGNMENT
);
361 if (in_specification_block
)
363 m
= match_word (NULL
, gfc_match_st_function
, &old_locus
);
365 return ST_STATEMENT_FUNCTION
;
368 if (!(in_specification_block
&& m
== MATCH_ERROR
))
370 match (NULL
, gfc_match_ptr_fcn_assign
, ST_ASSIGNMENT
);
373 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
374 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
376 /* Try to match a subroutine statement, which has the same optional
377 prefixes that functions can have. */
379 if (gfc_match_subroutine () == MATCH_YES
)
380 return ST_SUBROUTINE
;
382 gfc_current_locus
= old_locus
;
384 if (gfc_match_submod_proc () == MATCH_YES
)
386 if (gfc_new_block
->attr
.subroutine
)
387 return ST_SUBROUTINE
;
388 else if (gfc_new_block
->attr
.function
)
392 gfc_current_locus
= old_locus
;
394 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
395 statements, which might begin with a block label. The match functions for
396 these statements are unusual in that their keyword is not seen before
397 the matcher is called. */
399 if (gfc_match_if (&st
) == MATCH_YES
)
402 gfc_current_locus
= old_locus
;
404 if (gfc_match_where (&st
) == MATCH_YES
)
407 gfc_current_locus
= old_locus
;
409 if (gfc_match_forall (&st
) == MATCH_YES
)
412 gfc_current_locus
= old_locus
;
414 match (NULL
, gfc_match_do
, ST_DO
);
415 match (NULL
, gfc_match_block
, ST_BLOCK
);
416 match (NULL
, gfc_match_associate
, ST_ASSOCIATE
);
417 match (NULL
, gfc_match_critical
, ST_CRITICAL
);
418 match (NULL
, gfc_match_select
, ST_SELECT_CASE
);
420 gfc_current_ns
= gfc_build_block_ns (gfc_current_ns
);
421 match (NULL
, gfc_match_select_type
, ST_SELECT_TYPE
);
423 gfc_current_ns
= gfc_current_ns
->parent
;
424 gfc_free_namespace (ns
);
426 /* General statement matching: Instead of testing every possible
427 statement, we eliminate most possibilities by peeking at the
433 match ("abstract% interface", gfc_match_abstract_interface
,
435 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
);
436 match ("allocatable", gfc_match_allocatable
, ST_ATTR_DECL
);
437 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
);
438 match ("asynchronous", gfc_match_asynchronous
, ST_ATTR_DECL
);
442 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
);
443 match ("block data", gfc_match_block_data
, ST_BLOCK_DATA
);
444 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
448 match ("call", gfc_match_call
, ST_CALL
);
449 match ("close", gfc_match_close
, ST_CLOSE
);
450 match ("continue", gfc_match_continue
, ST_CONTINUE
);
451 match ("contiguous", gfc_match_contiguous
, ST_ATTR_DECL
);
452 match ("cycle", gfc_match_cycle
, ST_CYCLE
);
453 match ("case", gfc_match_case
, ST_CASE
);
454 match ("common", gfc_match_common
, ST_COMMON
);
455 match ("contains", gfc_match_eos
, ST_CONTAINS
);
456 match ("class", gfc_match_class_is
, ST_CLASS_IS
);
457 match ("codimension", gfc_match_codimension
, ST_ATTR_DECL
);
461 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
);
462 match ("data", gfc_match_data
, ST_DATA
);
463 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
467 match ("end file", gfc_match_endfile
, ST_END_FILE
);
468 match ("exit", gfc_match_exit
, ST_EXIT
);
469 match ("else", gfc_match_else
, ST_ELSE
);
470 match ("else where", gfc_match_elsewhere
, ST_ELSEWHERE
);
471 match ("else if", gfc_match_elseif
, ST_ELSEIF
);
472 match ("error stop", gfc_match_error_stop
, ST_ERROR_STOP
);
473 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
475 if (gfc_match_end (&st
) == MATCH_YES
)
478 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
479 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
480 match ("external", gfc_match_external
, ST_ATTR_DECL
);
481 match ("event post", gfc_match_event_post
, ST_EVENT_POST
);
482 match ("event wait", gfc_match_event_wait
, ST_EVENT_WAIT
);
486 match ("final", gfc_match_final_decl
, ST_FINAL
);
487 match ("flush", gfc_match_flush
, ST_FLUSH
);
488 match ("format", gfc_match_format
, ST_FORMAT
);
492 match ("generic", gfc_match_generic
, ST_GENERIC
);
493 match ("go to", gfc_match_goto
, ST_GOTO
);
497 match ("inquire", gfc_match_inquire
, ST_INQUIRE
);
498 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
499 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
500 match ("import", gfc_match_import
, ST_IMPORT
);
501 match ("interface", gfc_match_interface
, ST_INTERFACE
);
502 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
503 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
507 match ("lock", gfc_match_lock
, ST_LOCK
);
511 match ("map", gfc_match_map
, ST_MAP
);
512 match ("module% procedure", gfc_match_modproc
, ST_MODULE_PROC
);
513 match ("module", gfc_match_module
, ST_MODULE
);
517 match ("nullify", gfc_match_nullify
, ST_NULLIFY
);
518 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
522 match ("open", gfc_match_open
, ST_OPEN
);
523 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
527 match ("print", gfc_match_print
, ST_WRITE
);
528 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
529 match ("pause", gfc_match_pause
, ST_PAUSE
);
530 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
531 if (gfc_match_private (&st
) == MATCH_YES
)
533 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
534 match ("program", gfc_match_program
, ST_PROGRAM
);
535 if (gfc_match_public (&st
) == MATCH_YES
)
537 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
541 match ("read", gfc_match_read
, ST_READ
);
542 match ("return", gfc_match_return
, ST_RETURN
);
543 match ("rewind", gfc_match_rewind
, ST_REWIND
);
547 match ("structure", gfc_match_structure_decl
, ST_STRUCTURE_DECL
);
548 match ("sequence", gfc_match_eos
, ST_SEQUENCE
);
549 match ("stop", gfc_match_stop
, ST_STOP
);
550 match ("save", gfc_match_save
, ST_ATTR_DECL
);
551 match ("submodule", gfc_match_submodule
, ST_SUBMODULE
);
552 match ("sync all", gfc_match_sync_all
, ST_SYNC_ALL
);
553 match ("sync images", gfc_match_sync_images
, ST_SYNC_IMAGES
);
554 match ("sync memory", gfc_match_sync_memory
, ST_SYNC_MEMORY
);
558 match ("target", gfc_match_target
, ST_ATTR_DECL
);
559 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
560 match ("type is", gfc_match_type_is
, ST_TYPE_IS
);
564 match ("union", gfc_match_union
, ST_UNION
);
565 match ("unlock", gfc_match_unlock
, ST_UNLOCK
);
569 match ("value", gfc_match_value
, ST_ATTR_DECL
);
570 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
574 match ("wait", gfc_match_wait
, ST_WAIT
);
575 match ("write", gfc_match_write
, ST_WRITE
);
579 /* All else has failed, so give up. See if any of the matchers has
580 stored an error message of some sort. */
582 if (!gfc_error_check ())
583 gfc_error_now ("Unclassifiable statement at %C");
587 gfc_error_recovery ();
592 /* Like match, but set a flag simd_matched if keyword matched. */
593 #define matchs(keyword, subr, st) \
595 if (match_word_omp_simd (keyword, subr, &old_locus, \
596 &simd_matched) == MATCH_YES) \
599 undo_new_statement (); \
602 /* Like match, but don't match anything if not -fopenmp. */
603 #define matcho(keyword, subr, st) \
607 else if (match_word (keyword, subr, &old_locus) \
611 undo_new_statement (); \
615 decode_oacc_directive (void)
620 gfc_enforce_clean_symbol_state ();
622 gfc_clear_error (); /* Clear any pending errors. */
623 gfc_clear_warning (); /* Clear any pending warnings. */
627 gfc_error_now ("OpenACC directives at %C may not appear in PURE "
629 gfc_error_recovery ();
633 gfc_unset_implicit_pure (NULL
);
635 old_locus
= gfc_current_locus
;
637 /* General OpenACC directive matching: Instead of testing every possible
638 statement, we eliminate most possibilities by peeking at the
641 c
= gfc_peek_ascii_char ();
646 match ("atomic", gfc_match_oacc_atomic
, ST_OACC_ATOMIC
);
649 match ("cache", gfc_match_oacc_cache
, ST_OACC_CACHE
);
652 match ("data", gfc_match_oacc_data
, ST_OACC_DATA
);
653 match ("declare", gfc_match_oacc_declare
, ST_OACC_DECLARE
);
656 match ("end atomic", gfc_match_omp_eos
, ST_OACC_END_ATOMIC
);
657 match ("end data", gfc_match_omp_eos
, ST_OACC_END_DATA
);
658 match ("end host_data", gfc_match_omp_eos
, ST_OACC_END_HOST_DATA
);
659 match ("end kernels loop", gfc_match_omp_eos
, ST_OACC_END_KERNELS_LOOP
);
660 match ("end kernels", gfc_match_omp_eos
, ST_OACC_END_KERNELS
);
661 match ("end loop", gfc_match_omp_eos
, ST_OACC_END_LOOP
);
662 match ("end parallel loop", gfc_match_omp_eos
, ST_OACC_END_PARALLEL_LOOP
);
663 match ("end parallel", gfc_match_omp_eos
, ST_OACC_END_PARALLEL
);
664 match ("enter data", gfc_match_oacc_enter_data
, ST_OACC_ENTER_DATA
);
665 match ("exit data", gfc_match_oacc_exit_data
, ST_OACC_EXIT_DATA
);
668 match ("host_data", gfc_match_oacc_host_data
, ST_OACC_HOST_DATA
);
671 match ("parallel loop", gfc_match_oacc_parallel_loop
, ST_OACC_PARALLEL_LOOP
);
672 match ("parallel", gfc_match_oacc_parallel
, ST_OACC_PARALLEL
);
675 match ("kernels loop", gfc_match_oacc_kernels_loop
, ST_OACC_KERNELS_LOOP
);
676 match ("kernels", gfc_match_oacc_kernels
, ST_OACC_KERNELS
);
679 match ("loop", gfc_match_oacc_loop
, ST_OACC_LOOP
);
682 match ("routine", gfc_match_oacc_routine
, ST_OACC_ROUTINE
);
685 match ("update", gfc_match_oacc_update
, ST_OACC_UPDATE
);
688 match ("wait", gfc_match_oacc_wait
, ST_OACC_WAIT
);
692 /* Directive not found or stored an error message.
693 Check and give up. */
695 if (gfc_error_check () == 0)
696 gfc_error_now ("Unclassifiable OpenACC directive at %C");
700 gfc_error_recovery ();
706 decode_omp_directive (void)
710 bool simd_matched
= false;
712 gfc_enforce_clean_symbol_state ();
714 gfc_clear_error (); /* Clear any pending errors. */
715 gfc_clear_warning (); /* Clear any pending warnings. */
719 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
720 "or ELEMENTAL procedures");
721 gfc_error_recovery ();
725 gfc_unset_implicit_pure (NULL
);
727 old_locus
= gfc_current_locus
;
729 /* General OpenMP directive matching: Instead of testing every possible
730 statement, we eliminate most possibilities by peeking at the
733 c
= gfc_peek_ascii_char ();
735 /* match is for directives that should be recognized only if
736 -fopenmp, matchs for directives that should be recognized
737 if either -fopenmp or -fopenmp-simd. */
741 matcho ("atomic", gfc_match_omp_atomic
, ST_OMP_ATOMIC
);
744 matcho ("barrier", gfc_match_omp_barrier
, ST_OMP_BARRIER
);
747 matcho ("cancellation% point", gfc_match_omp_cancellation_point
,
748 ST_OMP_CANCELLATION_POINT
);
749 matcho ("cancel", gfc_match_omp_cancel
, ST_OMP_CANCEL
);
750 matcho ("critical", gfc_match_omp_critical
, ST_OMP_CRITICAL
);
753 matchs ("declare reduction", gfc_match_omp_declare_reduction
,
754 ST_OMP_DECLARE_REDUCTION
);
755 matchs ("declare simd", gfc_match_omp_declare_simd
,
756 ST_OMP_DECLARE_SIMD
);
757 matcho ("declare target", gfc_match_omp_declare_target
,
758 ST_OMP_DECLARE_TARGET
);
759 matchs ("distribute parallel do simd",
760 gfc_match_omp_distribute_parallel_do_simd
,
761 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
);
762 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do
,
763 ST_OMP_DISTRIBUTE_PARALLEL_DO
);
764 matchs ("distribute simd", gfc_match_omp_distribute_simd
,
765 ST_OMP_DISTRIBUTE_SIMD
);
766 matcho ("distribute", gfc_match_omp_distribute
, ST_OMP_DISTRIBUTE
);
767 matchs ("do simd", gfc_match_omp_do_simd
, ST_OMP_DO_SIMD
);
768 matcho ("do", gfc_match_omp_do
, ST_OMP_DO
);
771 matcho ("end atomic", gfc_match_omp_eos
, ST_OMP_END_ATOMIC
);
772 matcho ("end critical", gfc_match_omp_critical
, ST_OMP_END_CRITICAL
);
773 matchs ("end distribute parallel do simd", gfc_match_omp_eos
,
774 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD
);
775 matcho ("end distribute parallel do", gfc_match_omp_eos
,
776 ST_OMP_END_DISTRIBUTE_PARALLEL_DO
);
777 matchs ("end distribute simd", gfc_match_omp_eos
,
778 ST_OMP_END_DISTRIBUTE_SIMD
);
779 matcho ("end distribute", gfc_match_omp_eos
, ST_OMP_END_DISTRIBUTE
);
780 matchs ("end do simd", gfc_match_omp_end_nowait
, ST_OMP_END_DO_SIMD
);
781 matcho ("end do", gfc_match_omp_end_nowait
, ST_OMP_END_DO
);
782 matchs ("end simd", gfc_match_omp_eos
, ST_OMP_END_SIMD
);
783 matcho ("end master", gfc_match_omp_eos
, ST_OMP_END_MASTER
);
784 matcho ("end ordered", gfc_match_omp_eos
, ST_OMP_END_ORDERED
);
785 matchs ("end parallel do simd", gfc_match_omp_eos
,
786 ST_OMP_END_PARALLEL_DO_SIMD
);
787 matcho ("end parallel do", gfc_match_omp_eos
, ST_OMP_END_PARALLEL_DO
);
788 matcho ("end parallel sections", gfc_match_omp_eos
,
789 ST_OMP_END_PARALLEL_SECTIONS
);
790 matcho ("end parallel workshare", gfc_match_omp_eos
,
791 ST_OMP_END_PARALLEL_WORKSHARE
);
792 matcho ("end parallel", gfc_match_omp_eos
, ST_OMP_END_PARALLEL
);
793 matcho ("end sections", gfc_match_omp_end_nowait
, ST_OMP_END_SECTIONS
);
794 matcho ("end single", gfc_match_omp_end_single
, ST_OMP_END_SINGLE
);
795 matcho ("end target data", gfc_match_omp_eos
, ST_OMP_END_TARGET_DATA
);
796 matchs ("end target teams distribute parallel do simd",
798 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
);
799 matcho ("end target teams distribute parallel do", gfc_match_omp_eos
,
800 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
);
801 matchs ("end target teams distribute simd", gfc_match_omp_eos
,
802 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD
);
803 matcho ("end target teams distribute", gfc_match_omp_eos
,
804 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE
);
805 matcho ("end target teams", gfc_match_omp_eos
, ST_OMP_END_TARGET_TEAMS
);
806 matcho ("end target", gfc_match_omp_eos
, ST_OMP_END_TARGET
);
807 matcho ("end taskgroup", gfc_match_omp_eos
, ST_OMP_END_TASKGROUP
);
808 matcho ("end task", gfc_match_omp_eos
, ST_OMP_END_TASK
);
809 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos
,
810 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
);
811 matcho ("end teams distribute parallel do", gfc_match_omp_eos
,
812 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO
);
813 matchs ("end teams distribute simd", gfc_match_omp_eos
,
814 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD
);
815 matcho ("end teams distribute", gfc_match_omp_eos
,
816 ST_OMP_END_TEAMS_DISTRIBUTE
);
817 matcho ("end teams", gfc_match_omp_eos
, ST_OMP_END_TEAMS
);
818 matcho ("end workshare", gfc_match_omp_end_nowait
,
819 ST_OMP_END_WORKSHARE
);
822 matcho ("flush", gfc_match_omp_flush
, ST_OMP_FLUSH
);
825 matcho ("master", gfc_match_omp_master
, ST_OMP_MASTER
);
828 matcho ("ordered", gfc_match_omp_ordered
, ST_OMP_ORDERED
);
831 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd
,
832 ST_OMP_PARALLEL_DO_SIMD
);
833 matcho ("parallel do", gfc_match_omp_parallel_do
, ST_OMP_PARALLEL_DO
);
834 matcho ("parallel sections", gfc_match_omp_parallel_sections
,
835 ST_OMP_PARALLEL_SECTIONS
);
836 matcho ("parallel workshare", gfc_match_omp_parallel_workshare
,
837 ST_OMP_PARALLEL_WORKSHARE
);
838 matcho ("parallel", gfc_match_omp_parallel
, ST_OMP_PARALLEL
);
841 matcho ("sections", gfc_match_omp_sections
, ST_OMP_SECTIONS
);
842 matcho ("section", gfc_match_omp_eos
, ST_OMP_SECTION
);
843 matchs ("simd", gfc_match_omp_simd
, ST_OMP_SIMD
);
844 matcho ("single", gfc_match_omp_single
, ST_OMP_SINGLE
);
847 matcho ("target data", gfc_match_omp_target_data
, ST_OMP_TARGET_DATA
);
848 matchs ("target teams distribute parallel do simd",
849 gfc_match_omp_target_teams_distribute_parallel_do_simd
,
850 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
);
851 matcho ("target teams distribute parallel do",
852 gfc_match_omp_target_teams_distribute_parallel_do
,
853 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
);
854 matchs ("target teams distribute simd",
855 gfc_match_omp_target_teams_distribute_simd
,
856 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
);
857 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute
,
858 ST_OMP_TARGET_TEAMS_DISTRIBUTE
);
859 matcho ("target teams", gfc_match_omp_target_teams
, ST_OMP_TARGET_TEAMS
);
860 matcho ("target update", gfc_match_omp_target_update
,
861 ST_OMP_TARGET_UPDATE
);
862 matcho ("target", gfc_match_omp_target
, ST_OMP_TARGET
);
863 matcho ("taskgroup", gfc_match_omp_taskgroup
, ST_OMP_TASKGROUP
);
864 matcho ("taskwait", gfc_match_omp_taskwait
, ST_OMP_TASKWAIT
);
865 matcho ("taskyield", gfc_match_omp_taskyield
, ST_OMP_TASKYIELD
);
866 matcho ("task", gfc_match_omp_task
, ST_OMP_TASK
);
867 matchs ("teams distribute parallel do simd",
868 gfc_match_omp_teams_distribute_parallel_do_simd
,
869 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
);
870 matcho ("teams distribute parallel do",
871 gfc_match_omp_teams_distribute_parallel_do
,
872 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
);
873 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd
,
874 ST_OMP_TEAMS_DISTRIBUTE_SIMD
);
875 matcho ("teams distribute", gfc_match_omp_teams_distribute
,
876 ST_OMP_TEAMS_DISTRIBUTE
);
877 matcho ("teams", gfc_match_omp_teams
, ST_OMP_TEAMS
);
878 matcho ("threadprivate", gfc_match_omp_threadprivate
,
879 ST_OMP_THREADPRIVATE
);
882 matcho ("workshare", gfc_match_omp_workshare
, ST_OMP_WORKSHARE
);
886 /* All else has failed, so give up. See if any of the matchers has
887 stored an error message of some sort. Don't error out if
888 not -fopenmp and simd_matched is false, i.e. if a directive other
889 than one marked with match has been seen. */
891 if (flag_openmp
|| simd_matched
)
893 if (!gfc_error_check ())
894 gfc_error_now ("Unclassifiable OpenMP directive at %C");
899 gfc_error_recovery ();
905 decode_gcc_attribute (void)
909 gfc_enforce_clean_symbol_state ();
911 gfc_clear_error (); /* Clear any pending errors. */
912 gfc_clear_warning (); /* Clear any pending warnings. */
913 old_locus
= gfc_current_locus
;
915 match ("attributes", gfc_match_gcc_attributes
, ST_ATTR_DECL
);
917 /* All else has failed, so give up. See if any of the matchers has
918 stored an error message of some sort. */
920 if (!gfc_error_check ())
921 gfc_error_now ("Unclassifiable GCC directive at %C");
925 gfc_error_recovery ();
932 /* Assert next length characters to be equal to token in free form. */
935 verify_token_free (const char* token
, int length
, bool last_was_use_stmt
)
940 c
= gfc_next_ascii_char ();
941 for (i
= 0; i
< length
; i
++, c
= gfc_next_ascii_char ())
942 gcc_assert (c
== token
[i
]);
944 gcc_assert (gfc_is_whitespace(c
));
945 gfc_gobble_whitespace ();
946 if (last_was_use_stmt
)
950 /* Get the next statement in free form source. */
959 at_bol
= gfc_at_bol ();
960 gfc_gobble_whitespace ();
962 c
= gfc_peek_ascii_char ();
968 /* Found a statement label? */
969 m
= gfc_match_st_label (&gfc_statement_label
);
971 d
= gfc_peek_ascii_char ();
972 if (m
!= MATCH_YES
|| !gfc_is_whitespace (d
))
974 gfc_match_small_literal_int (&i
, &cnt
);
977 gfc_error_now ("Too many digits in statement label at %C");
980 gfc_error_now ("Zero is not a valid statement label at %C");
983 c
= gfc_next_ascii_char ();
986 if (!gfc_is_whitespace (c
))
987 gfc_error_now ("Non-numeric character in statement label at %C");
993 label_locus
= gfc_current_locus
;
995 gfc_gobble_whitespace ();
997 if (at_bol
&& gfc_peek_ascii_char () == ';')
999 gfc_error_now ("Semicolon at %C needs to be preceded by "
1001 gfc_next_ascii_char (); /* Eat up the semicolon. */
1005 if (gfc_match_eos () == MATCH_YES
)
1007 gfc_warning_now (0, "Ignoring statement label in empty statement "
1008 "at %L", &label_locus
);
1009 gfc_free_st_label (gfc_statement_label
);
1010 gfc_statement_label
= NULL
;
1017 /* Comments have already been skipped by the time we get here,
1018 except for GCC attributes and OpenMP/OpenACC directives. */
1020 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
1021 c
= gfc_peek_ascii_char ();
1027 c
= gfc_next_ascii_char ();
1028 for (i
= 0; i
< 4; i
++, c
= gfc_next_ascii_char ())
1029 gcc_assert (c
== "gcc$"[i
]);
1031 gfc_gobble_whitespace ();
1032 return decode_gcc_attribute ();
1037 /* Since both OpenMP and OpenACC directives starts with
1038 !$ character sequence, we must check all flags combinations */
1039 if ((flag_openmp
|| flag_openmp_simd
)
1042 verify_token_free ("$omp", 4, last_was_use_stmt
);
1043 return decode_omp_directive ();
1045 else if ((flag_openmp
|| flag_openmp_simd
)
1048 gfc_next_ascii_char (); /* Eat up dollar character */
1049 c
= gfc_peek_ascii_char ();
1053 verify_token_free ("omp", 3, last_was_use_stmt
);
1054 return decode_omp_directive ();
1058 verify_token_free ("acc", 3, last_was_use_stmt
);
1059 return decode_oacc_directive ();
1062 else if (flag_openacc
)
1064 verify_token_free ("$acc", 4, last_was_use_stmt
);
1065 return decode_oacc_directive ();
1071 if (at_bol
&& c
== ';')
1073 if (!(gfc_option
.allow_std
& GFC_STD_F2008
))
1074 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1076 gfc_next_ascii_char (); /* Eat up the semicolon. */
1080 return decode_statement ();
1083 /* Assert next length characters to be equal to token in fixed form. */
1086 verify_token_fixed (const char *token
, int length
, bool last_was_use_stmt
)
1089 char c
= gfc_next_char_literal (NONSTRING
);
1091 for (i
= 0; i
< length
; i
++, c
= gfc_next_char_literal (NONSTRING
))
1092 gcc_assert ((char) gfc_wide_tolower (c
) == token
[i
]);
1094 if (c
!= ' ' && c
!= '0')
1096 gfc_buffer_error (false);
1097 gfc_error ("Bad continuation line at %C");
1100 if (last_was_use_stmt
)
1106 /* Get the next statement in fixed-form source. */
1108 static gfc_statement
1111 int label
, digit_flag
, i
;
1116 return decode_statement ();
1118 /* Skip past the current label field, parsing a statement label if
1119 one is there. This is a weird number parser, since the number is
1120 contained within five columns and can have any kind of embedded
1121 spaces. We also check for characters that make the rest of the
1127 for (i
= 0; i
< 5; i
++)
1129 c
= gfc_next_char_literal (NONSTRING
);
1146 label
= label
* 10 + ((unsigned char) c
- '0');
1147 label_locus
= gfc_current_locus
;
1151 /* Comments have already been skipped by the time we get
1152 here, except for GCC attributes and OpenMP directives. */
1155 c
= gfc_next_char_literal (NONSTRING
);
1157 if (TOLOWER (c
) == 'g')
1159 for (i
= 0; i
< 4; i
++, c
= gfc_next_char_literal (NONSTRING
))
1160 gcc_assert (TOLOWER (c
) == "gcc$"[i
]);
1162 return decode_gcc_attribute ();
1166 if ((flag_openmp
|| flag_openmp_simd
)
1169 if (!verify_token_fixed ("omp", 3, last_was_use_stmt
))
1171 return decode_omp_directive ();
1173 else if ((flag_openmp
|| flag_openmp_simd
)
1176 c
= gfc_next_char_literal(NONSTRING
);
1177 if (c
== 'o' || c
== 'O')
1179 if (!verify_token_fixed ("mp", 2, last_was_use_stmt
))
1181 return decode_omp_directive ();
1183 else if (c
== 'a' || c
== 'A')
1185 if (!verify_token_fixed ("cc", 2, last_was_use_stmt
))
1187 return decode_oacc_directive ();
1190 else if (flag_openacc
)
1192 if (!verify_token_fixed ("acc", 3, last_was_use_stmt
))
1194 return decode_oacc_directive ();
1199 /* Comments have already been skipped by the time we get
1200 here so don't bother checking for them. */
1203 gfc_buffer_error (false);
1204 gfc_error ("Non-numeric character in statement label at %C");
1212 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1215 /* We've found a valid statement label. */
1216 gfc_statement_label
= gfc_get_st_label (label
);
1220 /* Since this line starts a statement, it cannot be a continuation
1221 of a previous statement. If we see something here besides a
1222 space or zero, it must be a bad continuation line. */
1224 c
= gfc_next_char_literal (NONSTRING
);
1228 if (c
!= ' ' && c
!= '0')
1230 gfc_buffer_error (false);
1231 gfc_error ("Bad continuation line at %C");
1235 /* Now that we've taken care of the statement label columns, we have
1236 to make sure that the first nonblank character is not a '!'. If
1237 it is, the rest of the line is a comment. */
1241 loc
= gfc_current_locus
;
1242 c
= gfc_next_char_literal (NONSTRING
);
1244 while (gfc_is_whitespace (c
));
1248 gfc_current_locus
= loc
;
1253 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1254 else if (!(gfc_option
.allow_std
& GFC_STD_F2008
))
1255 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1260 if (gfc_match_eos () == MATCH_YES
)
1263 /* At this point, we've got a nonblank statement to parse. */
1264 return decode_statement ();
1268 gfc_warning_now (0, "Ignoring statement label in empty statement at %L",
1271 gfc_current_locus
.lb
->truncated
= 0;
1272 gfc_advance_line ();
1277 /* Return the next non-ST_NONE statement to the caller. We also worry
1278 about including files and the ends of include files at this stage. */
1280 static gfc_statement
1281 next_statement (void)
1286 gfc_enforce_clean_symbol_state ();
1288 gfc_new_block
= NULL
;
1290 gfc_current_ns
->old_cl_list
= gfc_current_ns
->cl_list
;
1291 gfc_current_ns
->old_equiv
= gfc_current_ns
->equiv
;
1292 gfc_current_ns
->old_data
= gfc_current_ns
->data
;
1295 gfc_statement_label
= NULL
;
1296 gfc_buffer_error (true);
1299 gfc_advance_line ();
1301 gfc_skip_comments ();
1309 if (gfc_define_undef_line ())
1312 old_locus
= gfc_current_locus
;
1314 st
= (gfc_current_form
== FORM_FIXED
) ? next_fixed () : next_free ();
1320 gfc_buffer_error (false);
1322 if (st
== ST_GET_FCN_CHARACTERISTICS
&& gfc_statement_label
!= NULL
)
1324 gfc_free_st_label (gfc_statement_label
);
1325 gfc_statement_label
= NULL
;
1326 gfc_current_locus
= old_locus
;
1330 check_statement_label (st
);
1336 /****************************** Parser ***********************************/
1338 /* The parser subroutines are of type 'try' that fail if the file ends
1341 /* Macros that expand to case-labels for various classes of
1342 statements. Start with executable statements that directly do
1345 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1346 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1347 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1348 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1349 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1350 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1351 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1352 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1353 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1354 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1355 case ST_OMP_TARGET_UPDATE: case ST_ERROR_STOP: case ST_SYNC_ALL: \
1356 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1357 case ST_EVENT_POST: case ST_EVENT_WAIT: \
1358 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1359 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1361 /* Statements that mark other executable statements. */
1363 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1364 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1365 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1366 case ST_OMP_PARALLEL: \
1367 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1368 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1369 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1370 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1371 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1372 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1373 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1374 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1375 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1376 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1377 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1378 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1379 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1380 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1381 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1382 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1383 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: \
1385 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1386 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: \
1387 case ST_OACC_KERNELS_LOOP: case ST_OACC_ATOMIC
1389 /* Declaration statements */
1391 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1392 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1393 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
1394 case ST_PROCEDURE: case ST_OMP_DECLARE_SIMD: case ST_OMP_DECLARE_REDUCTION: \
1395 case ST_OMP_DECLARE_TARGET: case ST_OACC_ROUTINE: case ST_OACC_DECLARE
1397 /* Block end statements. Errors associated with interchanging these
1398 are detected in gfc_match_end(). */
1400 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1401 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1402 case ST_END_BLOCK: case ST_END_ASSOCIATE
1405 /* Push a new state onto the stack. */
1408 push_state (gfc_state_data
*p
, gfc_compile_state new_state
, gfc_symbol
*sym
)
1410 p
->state
= new_state
;
1411 p
->previous
= gfc_state_stack
;
1413 p
->head
= p
->tail
= NULL
;
1414 p
->do_variable
= NULL
;
1415 if (p
->state
!= COMP_DO
&& p
->state
!= COMP_DO_CONCURRENT
)
1416 p
->ext
.oacc_declare_clauses
= NULL
;
1418 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1419 construct statement was accepted right before pushing the state. Thus,
1420 the construct's gfc_code is available as tail of the parent state. */
1421 gcc_assert (gfc_state_stack
);
1422 p
->construct
= gfc_state_stack
->tail
;
1424 gfc_state_stack
= p
;
1428 /* Pop the current state. */
1432 gfc_state_stack
= gfc_state_stack
->previous
;
1436 /* Try to find the given state in the state stack. */
1439 gfc_find_state (gfc_compile_state state
)
1443 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1444 if (p
->state
== state
)
1447 return (p
== NULL
) ? false : true;
1451 /* Starts a new level in the statement list. */
1454 new_level (gfc_code
*q
)
1458 p
= q
->block
= gfc_get_code (EXEC_NOP
);
1460 gfc_state_stack
->head
= gfc_state_stack
->tail
= p
;
1466 /* Add the current new_st code structure and adds it to the current
1467 program unit. As a side-effect, it zeroes the new_st. */
1470 add_statement (void)
1474 p
= XCNEW (gfc_code
);
1477 p
->loc
= gfc_current_locus
;
1479 if (gfc_state_stack
->head
== NULL
)
1480 gfc_state_stack
->head
= p
;
1482 gfc_state_stack
->tail
->next
= p
;
1484 while (p
->next
!= NULL
)
1487 gfc_state_stack
->tail
= p
;
1489 gfc_clear_new_st ();
1495 /* Frees everything associated with the current statement. */
1498 undo_new_statement (void)
1500 gfc_free_statements (new_st
.block
);
1501 gfc_free_statements (new_st
.next
);
1502 gfc_free_statement (&new_st
);
1503 gfc_clear_new_st ();
1507 /* If the current statement has a statement label, make sure that it
1508 is allowed to, or should have one. */
1511 check_statement_label (gfc_statement st
)
1515 if (gfc_statement_label
== NULL
)
1517 if (st
== ST_FORMAT
)
1518 gfc_error ("FORMAT statement at %L does not have a statement label",
1525 case ST_END_PROGRAM
:
1526 case ST_END_FUNCTION
:
1527 case ST_END_SUBROUTINE
:
1531 case ST_END_CRITICAL
:
1533 case ST_END_ASSOCIATE
:
1536 if (st
== ST_ENDDO
|| st
== ST_CONTINUE
)
1537 type
= ST_LABEL_DO_TARGET
;
1539 type
= ST_LABEL_TARGET
;
1543 type
= ST_LABEL_FORMAT
;
1546 /* Statement labels are not restricted from appearing on a
1547 particular line. However, there are plenty of situations
1548 where the resulting label can't be referenced. */
1551 type
= ST_LABEL_BAD_TARGET
;
1555 gfc_define_st_label (gfc_statement_label
, type
, &label_locus
);
1557 new_st
.here
= gfc_statement_label
;
1561 /* Figures out what the enclosing program unit is. This will be a
1562 function, subroutine, program, block data or module. */
1565 gfc_enclosing_unit (gfc_compile_state
* result
)
1569 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1570 if (p
->state
== COMP_FUNCTION
|| p
->state
== COMP_SUBROUTINE
1571 || p
->state
== COMP_MODULE
|| p
->state
== COMP_SUBMODULE
1572 || p
->state
== COMP_BLOCK_DATA
|| p
->state
== COMP_PROGRAM
)
1581 *result
= COMP_PROGRAM
;
1586 /* Translate a statement enum to a string. */
1589 gfc_ascii_statement (gfc_statement st
)
1595 case ST_ARITHMETIC_IF
:
1596 p
= _("arithmetic IF");
1605 p
= _("attribute declaration");
1641 p
= _("data declaration");
1655 case ST_STRUCTURE_DECL
:
1658 case ST_DERIVED_DECL
:
1659 p
= _("derived type declaration");
1679 case ST_END_ASSOCIATE
:
1680 p
= "END ASSOCIATE";
1685 case ST_END_BLOCK_DATA
:
1686 p
= "END BLOCK DATA";
1688 case ST_END_CRITICAL
:
1700 case ST_END_FUNCTION
:
1706 case ST_END_INTERFACE
:
1707 p
= "END INTERFACE";
1712 case ST_END_SUBMODULE
:
1713 p
= "END SUBMODULE";
1715 case ST_END_PROGRAM
:
1721 case ST_END_SUBROUTINE
:
1722 p
= "END SUBROUTINE";
1727 case ST_END_STRUCTURE
:
1728 p
= "END STRUCTURE";
1742 case ST_EQUIVALENCE
:
1754 case ST_FORALL_BLOCK
: /* Fall through */
1776 case ST_IMPLICIT_NONE
:
1777 p
= "IMPLICIT NONE";
1779 case ST_IMPLIED_ENDDO
:
1780 p
= _("implied END DO");
1812 case ST_MODULE_PROC
:
1813 p
= "MODULE PROCEDURE";
1845 case ST_SYNC_IMAGES
:
1848 case ST_SYNC_MEMORY
:
1863 case ST_WHERE_BLOCK
: /* Fall through */
1874 p
= _("assignment");
1876 case ST_POINTER_ASSIGNMENT
:
1877 p
= _("pointer assignment");
1879 case ST_SELECT_CASE
:
1882 case ST_SELECT_TYPE
:
1897 case ST_STATEMENT_FUNCTION
:
1898 p
= "STATEMENT FUNCTION";
1900 case ST_LABEL_ASSIGNMENT
:
1901 p
= "LABEL ASSIGNMENT";
1904 p
= "ENUM DEFINITION";
1907 p
= "ENUMERATOR DEFINITION";
1912 case ST_OACC_PARALLEL_LOOP
:
1913 p
= "!$ACC PARALLEL LOOP";
1915 case ST_OACC_END_PARALLEL_LOOP
:
1916 p
= "!$ACC END PARALLEL LOOP";
1918 case ST_OACC_PARALLEL
:
1919 p
= "!$ACC PARALLEL";
1921 case ST_OACC_END_PARALLEL
:
1922 p
= "!$ACC END PARALLEL";
1924 case ST_OACC_KERNELS
:
1925 p
= "!$ACC KERNELS";
1927 case ST_OACC_END_KERNELS
:
1928 p
= "!$ACC END KERNELS";
1930 case ST_OACC_KERNELS_LOOP
:
1931 p
= "!$ACC KERNELS LOOP";
1933 case ST_OACC_END_KERNELS_LOOP
:
1934 p
= "!$ACC END KERNELS LOOP";
1939 case ST_OACC_END_DATA
:
1940 p
= "!$ACC END DATA";
1942 case ST_OACC_HOST_DATA
:
1943 p
= "!$ACC HOST_DATA";
1945 case ST_OACC_END_HOST_DATA
:
1946 p
= "!$ACC END HOST_DATA";
1951 case ST_OACC_END_LOOP
:
1952 p
= "!$ACC END LOOP";
1954 case ST_OACC_DECLARE
:
1955 p
= "!$ACC DECLARE";
1957 case ST_OACC_UPDATE
:
1966 case ST_OACC_ENTER_DATA
:
1967 p
= "!$ACC ENTER DATA";
1969 case ST_OACC_EXIT_DATA
:
1970 p
= "!$ACC EXIT DATA";
1972 case ST_OACC_ROUTINE
:
1973 p
= "!$ACC ROUTINE";
1975 case ST_OACC_ATOMIC
:
1978 case ST_OACC_END_ATOMIC
:
1979 p
= "!ACC END ATOMIC";
1984 case ST_OMP_BARRIER
:
1985 p
= "!$OMP BARRIER";
1990 case ST_OMP_CANCELLATION_POINT
:
1991 p
= "!$OMP CANCELLATION POINT";
1993 case ST_OMP_CRITICAL
:
1994 p
= "!$OMP CRITICAL";
1996 case ST_OMP_DECLARE_REDUCTION
:
1997 p
= "!$OMP DECLARE REDUCTION";
1999 case ST_OMP_DECLARE_SIMD
:
2000 p
= "!$OMP DECLARE SIMD";
2002 case ST_OMP_DECLARE_TARGET
:
2003 p
= "!$OMP DECLARE TARGET";
2005 case ST_OMP_DISTRIBUTE
:
2006 p
= "!$OMP DISTRIBUTE";
2008 case ST_OMP_DISTRIBUTE_PARALLEL_DO
:
2009 p
= "!$OMP DISTRIBUTE PARALLEL DO";
2011 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
2012 p
= "!$OMP DISTRIBUTE PARALLEL DO SIMD";
2014 case ST_OMP_DISTRIBUTE_SIMD
:
2015 p
= "!$OMP DISTRIBUTE SIMD";
2020 case ST_OMP_DO_SIMD
:
2021 p
= "!$OMP DO SIMD";
2023 case ST_OMP_END_ATOMIC
:
2024 p
= "!$OMP END ATOMIC";
2026 case ST_OMP_END_CRITICAL
:
2027 p
= "!$OMP END CRITICAL";
2029 case ST_OMP_END_DISTRIBUTE
:
2030 p
= "!$OMP END DISTRIBUTE";
2032 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO
:
2033 p
= "!$OMP END DISTRIBUTE PARALLEL DO";
2035 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD
:
2036 p
= "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
2038 case ST_OMP_END_DISTRIBUTE_SIMD
:
2039 p
= "!$OMP END DISTRIBUTE SIMD";
2044 case ST_OMP_END_DO_SIMD
:
2045 p
= "!$OMP END DO SIMD";
2047 case ST_OMP_END_SIMD
:
2048 p
= "!$OMP END SIMD";
2050 case ST_OMP_END_MASTER
:
2051 p
= "!$OMP END MASTER";
2053 case ST_OMP_END_ORDERED
:
2054 p
= "!$OMP END ORDERED";
2056 case ST_OMP_END_PARALLEL
:
2057 p
= "!$OMP END PARALLEL";
2059 case ST_OMP_END_PARALLEL_DO
:
2060 p
= "!$OMP END PARALLEL DO";
2062 case ST_OMP_END_PARALLEL_DO_SIMD
:
2063 p
= "!$OMP END PARALLEL DO SIMD";
2065 case ST_OMP_END_PARALLEL_SECTIONS
:
2066 p
= "!$OMP END PARALLEL SECTIONS";
2068 case ST_OMP_END_PARALLEL_WORKSHARE
:
2069 p
= "!$OMP END PARALLEL WORKSHARE";
2071 case ST_OMP_END_SECTIONS
:
2072 p
= "!$OMP END SECTIONS";
2074 case ST_OMP_END_SINGLE
:
2075 p
= "!$OMP END SINGLE";
2077 case ST_OMP_END_TASK
:
2078 p
= "!$OMP END TASK";
2080 case ST_OMP_END_TARGET
:
2081 p
= "!$OMP END TARGET";
2083 case ST_OMP_END_TARGET_DATA
:
2084 p
= "!$OMP END TARGET DATA";
2086 case ST_OMP_END_TARGET_TEAMS
:
2087 p
= "!$OMP END TARGET TEAMS";
2089 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE
:
2090 p
= "!$OMP END TARGET TEAMS DISTRIBUTE";
2092 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
2093 p
= "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2095 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
2096 p
= "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2098 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD
:
2099 p
= "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2101 case ST_OMP_END_TASKGROUP
:
2102 p
= "!$OMP END TASKGROUP";
2104 case ST_OMP_END_TEAMS
:
2105 p
= "!$OMP END TEAMS";
2107 case ST_OMP_END_TEAMS_DISTRIBUTE
:
2108 p
= "!$OMP END TEAMS DISTRIBUTE";
2110 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO
:
2111 p
= "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2113 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
2114 p
= "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2116 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD
:
2117 p
= "!$OMP END TEAMS DISTRIBUTE SIMD";
2119 case ST_OMP_END_WORKSHARE
:
2120 p
= "!$OMP END WORKSHARE";
2128 case ST_OMP_ORDERED
:
2129 p
= "!$OMP ORDERED";
2131 case ST_OMP_PARALLEL
:
2132 p
= "!$OMP PARALLEL";
2134 case ST_OMP_PARALLEL_DO
:
2135 p
= "!$OMP PARALLEL DO";
2137 case ST_OMP_PARALLEL_DO_SIMD
:
2138 p
= "!$OMP PARALLEL DO SIMD";
2140 case ST_OMP_PARALLEL_SECTIONS
:
2141 p
= "!$OMP PARALLEL SECTIONS";
2143 case ST_OMP_PARALLEL_WORKSHARE
:
2144 p
= "!$OMP PARALLEL WORKSHARE";
2146 case ST_OMP_SECTIONS
:
2147 p
= "!$OMP SECTIONS";
2149 case ST_OMP_SECTION
:
2150 p
= "!$OMP SECTION";
2161 case ST_OMP_TARGET_DATA
:
2162 p
= "!$OMP TARGET DATA";
2164 case ST_OMP_TARGET_TEAMS
:
2165 p
= "!$OMP TARGET TEAMS";
2167 case ST_OMP_TARGET_TEAMS_DISTRIBUTE
:
2168 p
= "!$OMP TARGET TEAMS DISTRIBUTE";
2170 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
2171 p
= "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2173 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
2174 p
= "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2176 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
2177 p
= "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2179 case ST_OMP_TARGET_UPDATE
:
2180 p
= "!$OMP TARGET UPDATE";
2185 case ST_OMP_TASKGROUP
:
2186 p
= "!$OMP TASKGROUP";
2188 case ST_OMP_TASKWAIT
:
2189 p
= "!$OMP TASKWAIT";
2191 case ST_OMP_TASKYIELD
:
2192 p
= "!$OMP TASKYIELD";
2197 case ST_OMP_TEAMS_DISTRIBUTE
:
2198 p
= "!$OMP TEAMS DISTRIBUTE";
2200 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
2201 p
= "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2203 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
2204 p
= "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2206 case ST_OMP_TEAMS_DISTRIBUTE_SIMD
:
2207 p
= "!$OMP TEAMS DISTRIBUTE SIMD";
2209 case ST_OMP_THREADPRIVATE
:
2210 p
= "!$OMP THREADPRIVATE";
2212 case ST_OMP_WORKSHARE
:
2213 p
= "!$OMP WORKSHARE";
2216 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2223 /* Create a symbol for the main program and assign it to ns->proc_name. */
2226 main_program_symbol (gfc_namespace
*ns
, const char *name
)
2228 gfc_symbol
*main_program
;
2229 symbol_attribute attr
;
2231 gfc_get_symbol (name
, ns
, &main_program
);
2232 gfc_clear_attr (&attr
);
2233 attr
.flavor
= FL_PROGRAM
;
2234 attr
.proc
= PROC_UNKNOWN
;
2235 attr
.subroutine
= 1;
2236 attr
.access
= ACCESS_PUBLIC
;
2237 attr
.is_main_program
= 1;
2238 main_program
->attr
= attr
;
2239 main_program
->declared_at
= gfc_current_locus
;
2240 ns
->proc_name
= main_program
;
2241 gfc_commit_symbols ();
2245 /* Do whatever is necessary to accept the last statement. */
2248 accept_statement (gfc_statement st
)
2252 case ST_IMPLICIT_NONE
:
2260 gfc_current_ns
->proc_name
= gfc_new_block
;
2263 /* If the statement is the end of a block, lay down a special code
2264 that allows a branch to the end of the block from within the
2265 construct. IF and SELECT are treated differently from DO
2266 (where EXEC_NOP is added inside the loop) for two
2268 1. END DO has a meaning in the sense that after a GOTO to
2269 it, the loop counter must be increased.
2270 2. IF blocks and SELECT blocks can consist of multiple
2271 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2272 Putting the label before the END IF would make the jump
2273 from, say, the ELSE IF block to the END IF illegal. */
2277 case ST_END_CRITICAL
:
2278 if (gfc_statement_label
!= NULL
)
2280 new_st
.op
= EXEC_END_NESTED_BLOCK
;
2285 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2286 one parallel block. Thus, we add the special code to the nested block
2287 itself, instead of the parent one. */
2289 case ST_END_ASSOCIATE
:
2290 if (gfc_statement_label
!= NULL
)
2292 new_st
.op
= EXEC_END_BLOCK
;
2297 /* The end-of-program unit statements do not get the special
2298 marker and require a statement of some sort if they are a
2301 case ST_END_PROGRAM
:
2302 case ST_END_FUNCTION
:
2303 case ST_END_SUBROUTINE
:
2304 if (gfc_statement_label
!= NULL
)
2306 new_st
.op
= EXEC_RETURN
;
2311 new_st
.op
= EXEC_END_PROCEDURE
;
2327 gfc_commit_symbols ();
2328 gfc_warning_check ();
2329 gfc_clear_new_st ();
2333 /* Undo anything tentative that has been built for the current
2337 reject_statement (void)
2339 /* Revert to the previous charlen chain. */
2340 gfc_free_charlen (gfc_current_ns
->cl_list
, gfc_current_ns
->old_cl_list
);
2341 gfc_current_ns
->cl_list
= gfc_current_ns
->old_cl_list
;
2343 gfc_free_equiv_until (gfc_current_ns
->equiv
, gfc_current_ns
->old_equiv
);
2344 gfc_current_ns
->equiv
= gfc_current_ns
->old_equiv
;
2346 gfc_reject_data (gfc_current_ns
);
2348 gfc_new_block
= NULL
;
2349 gfc_undo_symbols ();
2350 gfc_clear_warning ();
2351 undo_new_statement ();
2355 /* Generic complaint about an out of order statement. We also do
2356 whatever is necessary to clean up. */
2359 unexpected_statement (gfc_statement st
)
2361 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st
));
2363 reject_statement ();
2367 /* Given the next statement seen by the matcher, make sure that it is
2368 in proper order with the last. This subroutine is initialized by
2369 calling it with an argument of ST_NONE. If there is a problem, we
2370 issue an error and return false. Otherwise we return true.
2372 Individual parsers need to verify that the statements seen are
2373 valid before calling here, i.e., ENTRY statements are not allowed in
2374 INTERFACE blocks. The following diagram is taken from the standard:
2376 +---------------------------------------+
2377 | program subroutine function module |
2378 +---------------------------------------+
2380 +---------------------------------------+
2382 +---------------------------------------+
2384 | +-----------+------------------+
2385 | | parameter | implicit |
2386 | +-----------+------------------+
2387 | format | | derived type |
2388 | entry | parameter | interface |
2389 | | data | specification |
2390 | | | statement func |
2391 | +-----------+------------------+
2392 | | data | executable |
2393 +--------+-----------+------------------+
2395 +---------------------------------------+
2396 | internal module/subprogram |
2397 +---------------------------------------+
2399 +---------------------------------------+
2408 ORDER_IMPLICIT_NONE
,
2416 enum state_order state
;
2417 gfc_statement last_statement
;
2423 verify_st_order (st_state
*p
, gfc_statement st
, bool silent
)
2429 p
->state
= ORDER_START
;
2433 if (p
->state
> ORDER_USE
)
2435 p
->state
= ORDER_USE
;
2439 if (p
->state
> ORDER_IMPORT
)
2441 p
->state
= ORDER_IMPORT
;
2444 case ST_IMPLICIT_NONE
:
2445 if (p
->state
> ORDER_IMPLICIT
)
2448 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2449 statement disqualifies a USE but not an IMPLICIT NONE.
2450 Duplicate IMPLICIT NONEs are caught when the implicit types
2453 p
->state
= ORDER_IMPLICIT_NONE
;
2457 if (p
->state
> ORDER_IMPLICIT
)
2459 p
->state
= ORDER_IMPLICIT
;
2464 if (p
->state
< ORDER_IMPLICIT_NONE
)
2465 p
->state
= ORDER_IMPLICIT_NONE
;
2469 if (p
->state
>= ORDER_EXEC
)
2471 if (p
->state
< ORDER_IMPLICIT
)
2472 p
->state
= ORDER_IMPLICIT
;
2476 if (p
->state
< ORDER_SPEC
)
2477 p
->state
= ORDER_SPEC
;
2482 case ST_STRUCTURE_DECL
:
2483 case ST_DERIVED_DECL
:
2485 if (p
->state
>= ORDER_EXEC
)
2487 if (p
->state
< ORDER_SPEC
)
2488 p
->state
= ORDER_SPEC
;
2493 if (p
->state
< ORDER_EXEC
)
2494 p
->state
= ORDER_EXEC
;
2501 /* All is well, record the statement in case we need it next time. */
2502 p
->where
= gfc_current_locus
;
2503 p
->last_statement
= st
;
2508 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2509 gfc_ascii_statement (st
),
2510 gfc_ascii_statement (p
->last_statement
), &p
->where
);
2516 /* Handle an unexpected end of file. This is a show-stopper... */
2518 static void unexpected_eof (void) ATTRIBUTE_NORETURN
;
2521 unexpected_eof (void)
2525 gfc_error ("Unexpected end of file in %qs", gfc_source_file
);
2527 /* Memory cleanup. Move to "second to last". */
2528 for (p
= gfc_state_stack
; p
&& p
->previous
&& p
->previous
->previous
;
2531 gfc_current_ns
->code
= (p
&& p
->previous
) ? p
->head
: NULL
;
2534 longjmp (eof_buf
, 1);
2538 /* Parse the CONTAINS section of a derived type definition. */
2540 gfc_access gfc_typebound_default_access
;
2543 parse_derived_contains (void)
2546 bool seen_private
= false;
2547 bool seen_comps
= false;
2548 bool error_flag
= false;
2551 gcc_assert (gfc_current_state () == COMP_DERIVED
);
2552 gcc_assert (gfc_current_block ());
2554 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2556 if (gfc_current_block ()->attr
.sequence
)
2557 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2558 " section at %C", gfc_current_block ()->name
);
2559 if (gfc_current_block ()->attr
.is_bind_c
)
2560 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2561 " section at %C", gfc_current_block ()->name
);
2563 accept_statement (ST_CONTAINS
);
2564 push_state (&s
, COMP_DERIVED_CONTAINS
, NULL
);
2566 gfc_typebound_default_access
= ACCESS_PUBLIC
;
2572 st
= next_statement ();
2580 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2584 if (!gfc_notify_std (GFC_STD_F2003
, "Type-bound procedure at %C"))
2587 accept_statement (ST_PROCEDURE
);
2592 if (!gfc_notify_std (GFC_STD_F2003
, "GENERIC binding at %C"))
2595 accept_statement (ST_GENERIC
);
2600 if (!gfc_notify_std (GFC_STD_F2003
, "FINAL procedure declaration"
2604 accept_statement (ST_FINAL
);
2612 && (!gfc_notify_std(GFC_STD_F2008
, "Derived type definition "
2613 "at %C with empty CONTAINS section")))
2616 /* ST_END_TYPE is accepted by parse_derived after return. */
2620 if (!gfc_find_state (COMP_MODULE
))
2622 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2629 gfc_error ("PRIVATE statement at %C must precede procedure"
2636 gfc_error ("Duplicate PRIVATE statement at %C");
2640 accept_statement (ST_PRIVATE
);
2641 gfc_typebound_default_access
= ACCESS_PRIVATE
;
2642 seen_private
= true;
2646 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2650 gfc_error ("Already inside a CONTAINS block at %C");
2654 unexpected_statement (st
);
2662 reject_statement ();
2666 gcc_assert (gfc_current_state () == COMP_DERIVED
);
2672 /* Set attributes for the parent symbol based on the attributes of a component
2673 and raise errors if conflicting attributes are found for the component. */
2676 check_component (gfc_symbol
*sym
, gfc_component
*c
, gfc_component
**lockp
,
2677 gfc_component
**eventp
)
2679 bool coarray
, lock_type
, event_type
, allocatable
, pointer
;
2680 coarray
= lock_type
= event_type
= allocatable
= pointer
= false;
2681 gfc_component
*lock_comp
= NULL
, *event_comp
= NULL
;
2683 if (lockp
) lock_comp
= *lockp
;
2684 if (eventp
) event_comp
= *eventp
;
2686 /* Look for allocatable components. */
2687 if (c
->attr
.allocatable
2688 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2689 && CLASS_DATA (c
)->attr
.allocatable
)
2690 || (c
->ts
.type
== BT_DERIVED
&& !c
->attr
.pointer
2691 && c
->ts
.u
.derived
->attr
.alloc_comp
))
2694 sym
->attr
.alloc_comp
= 1;
2697 /* Look for pointer components. */
2699 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2700 && CLASS_DATA (c
)->attr
.class_pointer
)
2701 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.pointer_comp
))
2704 sym
->attr
.pointer_comp
= 1;
2707 /* Look for procedure pointer components. */
2708 if (c
->attr
.proc_pointer
2709 || (c
->ts
.type
== BT_DERIVED
2710 && c
->ts
.u
.derived
->attr
.proc_pointer_comp
))
2711 sym
->attr
.proc_pointer_comp
= 1;
2713 /* Looking for coarray components. */
2714 if (c
->attr
.codimension
2715 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2716 && CLASS_DATA (c
)->attr
.codimension
))
2719 sym
->attr
.coarray_comp
= 1;
2722 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.coarray_comp
2723 && !c
->attr
.pointer
)
2726 sym
->attr
.coarray_comp
= 1;
2729 /* Looking for lock_type components. */
2730 if ((c
->ts
.type
== BT_DERIVED
2731 && c
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
2732 && c
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
)
2733 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2734 && CLASS_DATA (c
)->ts
.u
.derived
->from_intmod
2735 == INTMOD_ISO_FORTRAN_ENV
2736 && CLASS_DATA (c
)->ts
.u
.derived
->intmod_sym_id
2737 == ISOFORTRAN_LOCK_TYPE
)
2738 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.lock_comp
2739 && !allocatable
&& !pointer
))
2743 sym
->attr
.lock_comp
= 1;
2746 /* Looking for event_type components. */
2747 if ((c
->ts
.type
== BT_DERIVED
2748 && c
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
2749 && c
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
)
2750 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2751 && CLASS_DATA (c
)->ts
.u
.derived
->from_intmod
2752 == INTMOD_ISO_FORTRAN_ENV
2753 && CLASS_DATA (c
)->ts
.u
.derived
->intmod_sym_id
2754 == ISOFORTRAN_EVENT_TYPE
)
2755 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.event_comp
2756 && !allocatable
&& !pointer
))
2760 sym
->attr
.event_comp
= 1;
2763 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2764 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2765 unless there are nondirect [allocatable or pointer] components
2766 involved (cf. 1.3.33.1 and 1.3.33.3). */
2768 if (pointer
&& !coarray
&& lock_type
)
2769 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2770 "codimension or be a subcomponent of a coarray, "
2771 "which is not possible as the component has the "
2772 "pointer attribute", c
->name
, &c
->loc
);
2773 else if (pointer
&& !coarray
&& c
->ts
.type
== BT_DERIVED
2774 && c
->ts
.u
.derived
->attr
.lock_comp
)
2775 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2776 "of type LOCK_TYPE, which must have a codimension or be a "
2777 "subcomponent of a coarray", c
->name
, &c
->loc
);
2779 if (lock_type
&& allocatable
&& !coarray
)
2780 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2781 "a codimension", c
->name
, &c
->loc
);
2782 else if (lock_type
&& allocatable
&& c
->ts
.type
== BT_DERIVED
2783 && c
->ts
.u
.derived
->attr
.lock_comp
)
2784 gfc_error ("Allocatable component %s at %L must have a codimension as "
2785 "it has a noncoarray subcomponent of type LOCK_TYPE",
2788 if (sym
->attr
.coarray_comp
&& !coarray
&& lock_type
)
2789 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2790 "subcomponent of type LOCK_TYPE must have a codimension or "
2791 "be a subcomponent of a coarray. (Variables of type %s may "
2792 "not have a codimension as already a coarray "
2793 "subcomponent exists)", c
->name
, &c
->loc
, sym
->name
);
2795 if (sym
->attr
.lock_comp
&& coarray
&& !lock_type
)
2796 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2797 "subcomponent of type LOCK_TYPE must have a codimension or "
2798 "be a subcomponent of a coarray. (Variables of type %s may "
2799 "not have a codimension as %s at %L has a codimension or a "
2800 "coarray subcomponent)", lock_comp
->name
, &lock_comp
->loc
,
2801 sym
->name
, c
->name
, &c
->loc
);
2803 /* Similarly for EVENT TYPE. */
2805 if (pointer
&& !coarray
&& event_type
)
2806 gfc_error ("Component %s at %L of type EVENT_TYPE must have a "
2807 "codimension or be a subcomponent of a coarray, "
2808 "which is not possible as the component has the "
2809 "pointer attribute", c
->name
, &c
->loc
);
2810 else if (pointer
&& !coarray
&& c
->ts
.type
== BT_DERIVED
2811 && c
->ts
.u
.derived
->attr
.event_comp
)
2812 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2813 "of type EVENT_TYPE, which must have a codimension or be a "
2814 "subcomponent of a coarray", c
->name
, &c
->loc
);
2816 if (event_type
&& allocatable
&& !coarray
)
2817 gfc_error ("Allocatable component %s at %L of type EVENT_TYPE must have "
2818 "a codimension", c
->name
, &c
->loc
);
2819 else if (event_type
&& allocatable
&& c
->ts
.type
== BT_DERIVED
2820 && c
->ts
.u
.derived
->attr
.event_comp
)
2821 gfc_error ("Allocatable component %s at %L must have a codimension as "
2822 "it has a noncoarray subcomponent of type EVENT_TYPE",
2825 if (sym
->attr
.coarray_comp
&& !coarray
&& event_type
)
2826 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
2827 "subcomponent of type EVENT_TYPE must have a codimension or "
2828 "be a subcomponent of a coarray. (Variables of type %s may "
2829 "not have a codimension as already a coarray "
2830 "subcomponent exists)", c
->name
, &c
->loc
, sym
->name
);
2832 if (sym
->attr
.event_comp
&& coarray
&& !event_type
)
2833 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
2834 "subcomponent of type EVENT_TYPE must have a codimension or "
2835 "be a subcomponent of a coarray. (Variables of type %s may "
2836 "not have a codimension as %s at %L has a codimension or a "
2837 "coarray subcomponent)", event_comp
->name
, &event_comp
->loc
,
2838 sym
->name
, c
->name
, &c
->loc
);
2840 /* Look for private components. */
2841 if (sym
->component_access
== ACCESS_PRIVATE
2842 || c
->attr
.access
== ACCESS_PRIVATE
2843 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.private_comp
))
2844 sym
->attr
.private_comp
= 1;
2846 if (lockp
) *lockp
= lock_comp
;
2847 if (eventp
) *eventp
= event_comp
;
2851 static void parse_struct_map (gfc_statement
);
2853 /* Parse a union component definition within a structure definition. */
2861 gfc_component
*c
, *lock_comp
= NULL
, *event_comp
= NULL
;
2864 accept_statement(ST_UNION
);
2865 push_state (&s
, COMP_UNION
, gfc_new_block
);
2872 st
= next_statement ();
2873 /* Only MAP declarations valid within a union. */
2880 accept_statement (ST_MAP
);
2881 parse_struct_map (ST_MAP
);
2882 /* Add a component to the union for each map. */
2883 if (!gfc_add_component (un
, gfc_new_block
->name
, &c
))
2885 gfc_internal_error ("failed to create map component '%s'",
2886 gfc_new_block
->name
);
2887 reject_statement ();
2890 c
->ts
.type
= BT_DERIVED
;
2891 c
->ts
.u
.derived
= gfc_new_block
;
2892 /* Normally components get their initialization expressions when they
2893 are created in decl.c (build_struct) so we can look through the
2894 flat component list for initializers during resolution. Unions and
2895 maps create components along with their type definitions so we
2896 have to generate initializers here. */
2897 c
->initializer
= gfc_default_initializer (&c
->ts
);
2902 accept_statement (ST_END_UNION
);
2906 unexpected_statement (st
);
2911 for (c
= un
->components
; c
; c
= c
->next
)
2912 check_component (un
, c
, &lock_comp
, &event_comp
);
2914 /* Add the union as a component in its parent structure. */
2916 if (!gfc_add_component (gfc_current_block (), un
->name
, &c
))
2918 gfc_internal_error ("failed to create union component '%s'", un
->name
);
2919 reject_statement ();
2922 c
->ts
.type
= BT_UNION
;
2923 c
->ts
.u
.derived
= un
;
2924 c
->initializer
= gfc_default_initializer (&c
->ts
);
2926 un
->attr
.zero_comp
= un
->components
== NULL
;
2930 /* Parse a STRUCTURE or MAP. */
2933 parse_struct_map (gfc_statement block
)
2939 gfc_component
*c
, *lock_comp
= NULL
, *event_comp
= NULL
;
2940 gfc_compile_state comp
;
2943 if (block
== ST_STRUCTURE_DECL
)
2945 comp
= COMP_STRUCTURE
;
2946 ends
= ST_END_STRUCTURE
;
2950 gcc_assert (block
== ST_MAP
);
2955 accept_statement(block
);
2956 push_state (&s
, comp
, gfc_new_block
);
2958 gfc_new_block
->component_access
= ACCESS_PUBLIC
;
2961 while (compiling_type
)
2963 st
= next_statement ();
2969 /* Nested structure declarations will be captured as ST_DATA_DECL. */
2970 case ST_STRUCTURE_DECL
:
2971 /* Let a more specific error make it to decode_statement(). */
2972 if (gfc_error_check () == 0)
2973 gfc_error ("Syntax error in nested structure declaration at %C");
2974 reject_statement ();
2975 /* Skip the rest of this statement. */
2976 gfc_error_recovery ();
2980 accept_statement (ST_UNION
);
2985 /* The data declaration was a nested/ad-hoc STRUCTURE field. */
2986 accept_statement (ST_DATA_DECL
);
2987 if (gfc_new_block
&& gfc_new_block
!= gfc_current_block ()
2988 && gfc_new_block
->attr
.flavor
== FL_STRUCT
)
2989 parse_struct_map (ST_STRUCTURE_DECL
);
2992 case ST_END_STRUCTURE
:
2996 accept_statement (st
);
3000 unexpected_statement (st
);
3004 unexpected_statement (st
);
3009 /* Validate each component. */
3010 sym
= gfc_current_block ();
3011 for (c
= sym
->components
; c
; c
= c
->next
)
3012 check_component (sym
, c
, &lock_comp
, &event_comp
);
3014 sym
->attr
.zero_comp
= (sym
->components
== NULL
);
3016 /* Allow parse_union to find this structure to add to its list of maps. */
3017 if (block
== ST_MAP
)
3018 gfc_new_block
= gfc_current_block ();
3024 /* Parse a derived type. */
3027 parse_derived (void)
3029 int compiling_type
, seen_private
, seen_sequence
, seen_component
;
3033 gfc_component
*c
, *lock_comp
= NULL
, *event_comp
= NULL
;
3035 accept_statement (ST_DERIVED_DECL
);
3036 push_state (&s
, COMP_DERIVED
, gfc_new_block
);
3038 gfc_new_block
->component_access
= ACCESS_PUBLIC
;
3045 while (compiling_type
)
3047 st
= next_statement ();
3055 accept_statement (st
);
3060 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
3067 if (!seen_component
)
3068 gfc_notify_std (GFC_STD_F2003
, "Derived type "
3069 "definition at %C without components");
3071 accept_statement (ST_END_TYPE
);
3075 if (!gfc_find_state (COMP_MODULE
))
3077 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
3084 gfc_error ("PRIVATE statement at %C must precede "
3085 "structure components");
3090 gfc_error ("Duplicate PRIVATE statement at %C");
3092 s
.sym
->component_access
= ACCESS_PRIVATE
;
3094 accept_statement (ST_PRIVATE
);
3101 gfc_error ("SEQUENCE statement at %C must precede "
3102 "structure components");
3106 if (gfc_current_block ()->attr
.sequence
)
3107 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
3112 gfc_error ("Duplicate SEQUENCE statement at %C");
3116 gfc_add_sequence (&gfc_current_block ()->attr
,
3117 gfc_current_block ()->name
, NULL
);
3121 gfc_notify_std (GFC_STD_F2003
,
3122 "CONTAINS block in derived type"
3123 " definition at %C");
3125 accept_statement (ST_CONTAINS
);
3126 parse_derived_contains ();
3130 unexpected_statement (st
);
3135 /* need to verify that all fields of the derived type are
3136 * interoperable with C if the type is declared to be bind(c)
3138 sym
= gfc_current_block ();
3139 for (c
= sym
->components
; c
; c
= c
->next
)
3140 check_component (sym
, c
, &lock_comp
, &event_comp
);
3142 if (!seen_component
)
3143 sym
->attr
.zero_comp
= 1;
3149 /* Parse an ENUM. */
3157 int seen_enumerator
= 0;
3159 push_state (&s
, COMP_ENUM
, gfc_new_block
);
3163 while (compiling_enum
)
3165 st
= next_statement ();
3173 seen_enumerator
= 1;
3174 accept_statement (st
);
3179 if (!seen_enumerator
)
3180 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
3181 accept_statement (st
);
3185 gfc_free_enum_history ();
3186 unexpected_statement (st
);
3194 /* Parse an interface. We must be able to deal with the possibility
3195 of recursive interfaces. The parse_spec() subroutine is mutually
3196 recursive with parse_interface(). */
3198 static gfc_statement
parse_spec (gfc_statement
);
3201 parse_interface (void)
3203 gfc_compile_state new_state
= COMP_NONE
, current_state
;
3204 gfc_symbol
*prog_unit
, *sym
;
3205 gfc_interface_info save
;
3206 gfc_state_data s1
, s2
;
3209 accept_statement (ST_INTERFACE
);
3211 current_interface
.ns
= gfc_current_ns
;
3212 save
= current_interface
;
3214 sym
= (current_interface
.type
== INTERFACE_GENERIC
3215 || current_interface
.type
== INTERFACE_USER_OP
)
3216 ? gfc_new_block
: NULL
;
3218 push_state (&s1
, COMP_INTERFACE
, sym
);
3219 current_state
= COMP_NONE
;
3222 gfc_current_ns
= gfc_get_namespace (current_interface
.ns
, 0);
3224 st
= next_statement ();
3232 if (st
== ST_SUBROUTINE
)
3233 new_state
= COMP_SUBROUTINE
;
3234 else if (st
== ST_FUNCTION
)
3235 new_state
= COMP_FUNCTION
;
3236 if (gfc_new_block
->attr
.pointer
)
3238 gfc_new_block
->attr
.pointer
= 0;
3239 gfc_new_block
->attr
.proc_pointer
= 1;
3241 if (!gfc_add_explicit_interface (gfc_new_block
, IFSRC_IFBODY
,
3242 gfc_new_block
->formal
, NULL
))
3244 reject_statement ();
3245 gfc_free_namespace (gfc_current_ns
);
3248 /* F2008 C1210 forbids the IMPORT statement in module procedure
3249 interface bodies and the flag is set to import symbols. */
3250 if (gfc_new_block
->attr
.module_procedure
)
3251 gfc_current_ns
->has_import_set
= 1;
3255 case ST_MODULE_PROC
: /* The module procedure matcher makes
3256 sure the context is correct. */
3257 accept_statement (st
);
3258 gfc_free_namespace (gfc_current_ns
);
3261 case ST_END_INTERFACE
:
3262 gfc_free_namespace (gfc_current_ns
);
3263 gfc_current_ns
= current_interface
.ns
;
3267 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
3268 gfc_ascii_statement (st
));
3269 reject_statement ();
3270 gfc_free_namespace (gfc_current_ns
);
3275 /* Make sure that the generic name has the right attribute. */
3276 if (current_interface
.type
== INTERFACE_GENERIC
3277 && current_state
== COMP_NONE
)
3279 if (new_state
== COMP_FUNCTION
&& sym
)
3280 gfc_add_function (&sym
->attr
, sym
->name
, NULL
);
3281 else if (new_state
== COMP_SUBROUTINE
&& sym
)
3282 gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
);
3284 current_state
= new_state
;
3287 if (current_interface
.type
== INTERFACE_ABSTRACT
)
3289 gfc_add_abstract (&gfc_new_block
->attr
, &gfc_current_locus
);
3290 if (gfc_is_intrinsic_typename (gfc_new_block
->name
))
3291 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
3292 "cannot be the same as an intrinsic type",
3293 gfc_new_block
->name
);
3296 push_state (&s2
, new_state
, gfc_new_block
);
3297 accept_statement (st
);
3298 prog_unit
= gfc_new_block
;
3299 prog_unit
->formal_ns
= gfc_current_ns
;
3300 if (prog_unit
== prog_unit
->formal_ns
->proc_name
3301 && prog_unit
->ns
!= prog_unit
->formal_ns
)
3305 /* Read data declaration statements. */
3306 st
= parse_spec (ST_NONE
);
3307 in_specification_block
= true;
3309 /* Since the interface block does not permit an IMPLICIT statement,
3310 the default type for the function or the result must be taken
3311 from the formal namespace. */
3312 if (new_state
== COMP_FUNCTION
)
3314 if (prog_unit
->result
== prog_unit
3315 && prog_unit
->ts
.type
== BT_UNKNOWN
)
3316 gfc_set_default_type (prog_unit
, 1, prog_unit
->formal_ns
);
3317 else if (prog_unit
->result
!= prog_unit
3318 && prog_unit
->result
->ts
.type
== BT_UNKNOWN
)
3319 gfc_set_default_type (prog_unit
->result
, 1,
3320 prog_unit
->formal_ns
);
3323 if (st
!= ST_END_SUBROUTINE
&& st
!= ST_END_FUNCTION
)
3325 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3326 gfc_ascii_statement (st
));
3327 reject_statement ();
3331 /* Add EXTERNAL attribute to function or subroutine. */
3332 if (current_interface
.type
!= INTERFACE_ABSTRACT
&& !prog_unit
->attr
.dummy
)
3333 gfc_add_external (&prog_unit
->attr
, &gfc_current_locus
);
3335 current_interface
= save
;
3336 gfc_add_interface (prog_unit
);
3339 if (current_interface
.ns
3340 && current_interface
.ns
->proc_name
3341 && strcmp (current_interface
.ns
->proc_name
->name
,
3342 prog_unit
->name
) == 0)
3343 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3344 "enclosing procedure", prog_unit
->name
,
3345 ¤t_interface
.ns
->proc_name
->declared_at
);
3354 /* Associate function characteristics by going back to the function
3355 declaration and rematching the prefix. */
3358 match_deferred_characteristics (gfc_typespec
* ts
)
3361 match m
= MATCH_ERROR
;
3362 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3364 loc
= gfc_current_locus
;
3366 gfc_current_locus
= gfc_current_block ()->declared_at
;
3369 gfc_buffer_error (true);
3370 m
= gfc_match_prefix (ts
);
3371 gfc_buffer_error (false);
3373 if (ts
->type
== BT_DERIVED
)
3381 /* Only permit one go at the characteristic association. */
3385 /* Set the function locus correctly. If we have not found the
3386 function name, there is an error. */
3388 && gfc_match ("function% %n", name
) == MATCH_YES
3389 && strcmp (name
, gfc_current_block ()->name
) == 0)
3391 gfc_current_block ()->declared_at
= gfc_current_locus
;
3392 gfc_commit_symbols ();
3397 gfc_undo_symbols ();
3400 gfc_current_locus
=loc
;
3405 /* Check specification-expressions in the function result of the currently
3406 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3407 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3408 scope are not yet parsed so this has to be delayed up to parse_spec. */
3411 check_function_result_typed (void)
3415 gcc_assert (gfc_current_state () == COMP_FUNCTION
);
3417 if (!gfc_current_ns
->proc_name
->result
) return;
3419 ts
= gfc_current_ns
->proc_name
->result
->ts
;
3421 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3422 /* TODO: Extend when KIND type parameters are implemented. */
3423 if (ts
.type
== BT_CHARACTER
&& ts
.u
.cl
&& ts
.u
.cl
->length
)
3424 gfc_expr_check_typed (ts
.u
.cl
->length
, gfc_current_ns
, true);
3428 /* Parse a set of specification statements. Returns the statement
3429 that doesn't fit. */
3431 static gfc_statement
3432 parse_spec (gfc_statement st
)
3435 bool function_result_typed
= false;
3436 bool bad_characteristic
= false;
3439 in_specification_block
= true;
3441 verify_st_order (&ss
, ST_NONE
, false);
3443 st
= next_statement ();
3445 /* If we are not inside a function or don't have a result specified so far,
3446 do nothing special about it. */
3447 if (gfc_current_state () != COMP_FUNCTION
)
3448 function_result_typed
= true;
3451 gfc_symbol
* proc
= gfc_current_ns
->proc_name
;
3454 if (proc
->result
->ts
.type
== BT_UNKNOWN
)
3455 function_result_typed
= true;
3460 /* If we're inside a BLOCK construct, some statements are disallowed.
3461 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3462 or VALUE are also disallowed, but they don't have a particular ST_*
3463 key so we have to check for them individually in their matcher routine. */
3464 if (gfc_current_state () == COMP_BLOCK
)
3468 case ST_IMPLICIT_NONE
:
3471 case ST_EQUIVALENCE
:
3472 case ST_STATEMENT_FUNCTION
:
3473 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3474 gfc_ascii_statement (st
));
3475 reject_statement ();
3481 else if (gfc_current_state () == COMP_BLOCK_DATA
)
3482 /* Fortran 2008, C1116. */
3489 case ST_END_BLOCK_DATA
:
3491 case ST_EQUIVALENCE
:
3494 case ST_IMPLICIT_NONE
:
3495 case ST_DERIVED_DECL
:
3503 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3504 gfc_ascii_statement (st
));
3505 reject_statement ();
3509 /* If we find a statement that can not be followed by an IMPLICIT statement
3510 (and thus we can expect to see none any further), type the function result
3511 if it has not yet been typed. Be careful not to give the END statement
3512 to verify_st_order! */
3513 if (!function_result_typed
&& st
!= ST_GET_FCN_CHARACTERISTICS
)
3515 bool verify_now
= false;
3517 if (st
== ST_END_FUNCTION
|| st
== ST_CONTAINS
)
3522 verify_st_order (&dummyss
, ST_NONE
, false);
3523 verify_st_order (&dummyss
, st
, false);
3525 if (!verify_st_order (&dummyss
, ST_IMPLICIT
, true))
3531 check_function_result_typed ();
3532 function_result_typed
= true;
3541 case ST_IMPLICIT_NONE
:
3543 if (!function_result_typed
)
3545 check_function_result_typed ();
3546 function_result_typed
= true;
3552 case ST_DATA
: /* Not allowed in interfaces */
3553 if (gfc_current_state () == COMP_INTERFACE
)
3563 case ST_STRUCTURE_DECL
:
3564 case ST_DERIVED_DECL
:
3567 if (!verify_st_order (&ss
, st
, false))
3569 reject_statement ();
3570 st
= next_statement ();
3580 case ST_STRUCTURE_DECL
:
3581 parse_struct_map (ST_STRUCTURE_DECL
);
3584 case ST_DERIVED_DECL
:
3590 if (gfc_current_state () != COMP_MODULE
)
3592 gfc_error ("%s statement must appear in a MODULE",
3593 gfc_ascii_statement (st
));
3594 reject_statement ();
3598 if (gfc_current_ns
->default_access
!= ACCESS_UNKNOWN
)
3600 gfc_error ("%s statement at %C follows another accessibility "
3601 "specification", gfc_ascii_statement (st
));
3602 reject_statement ();
3606 gfc_current_ns
->default_access
= (st
== ST_PUBLIC
)
3607 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
3611 case ST_STATEMENT_FUNCTION
:
3612 if (gfc_current_state () == COMP_MODULE
3613 || gfc_current_state () == COMP_SUBMODULE
)
3615 unexpected_statement (st
);
3623 accept_statement (st
);
3624 st
= next_statement ();
3628 accept_statement (st
);
3630 st
= next_statement ();
3633 case ST_GET_FCN_CHARACTERISTICS
:
3634 /* This statement triggers the association of a function's result
3636 ts
= &gfc_current_block ()->result
->ts
;
3637 if (match_deferred_characteristics (ts
) != MATCH_YES
)
3638 bad_characteristic
= true;
3640 st
= next_statement ();
3647 /* If match_deferred_characteristics failed, then there is an error. */
3648 if (bad_characteristic
)
3650 ts
= &gfc_current_block ()->result
->ts
;
3651 if (ts
->type
!= BT_DERIVED
)
3652 gfc_error ("Bad kind expression for function %qs at %L",
3653 gfc_current_block ()->name
,
3654 &gfc_current_block ()->declared_at
);
3656 gfc_error ("The type for function %qs at %L is not accessible",
3657 gfc_current_block ()->name
,
3658 &gfc_current_block ()->declared_at
);
3660 gfc_current_block ()->ts
.kind
= 0;
3661 /* Keep the derived type; if it's bad, it will be discovered later. */
3662 if (!(ts
->type
== BT_DERIVED
&& ts
->u
.derived
))
3663 ts
->type
= BT_UNKNOWN
;
3666 in_specification_block
= false;
3672 /* Parse a WHERE block, (not a simple WHERE statement). */
3675 parse_where_block (void)
3677 int seen_empty_else
;
3682 accept_statement (ST_WHERE_BLOCK
);
3683 top
= gfc_state_stack
->tail
;
3685 push_state (&s
, COMP_WHERE
, gfc_new_block
);
3687 d
= add_statement ();
3688 d
->expr1
= top
->expr1
;
3694 seen_empty_else
= 0;
3698 st
= next_statement ();
3704 case ST_WHERE_BLOCK
:
3705 parse_where_block ();
3710 accept_statement (st
);
3714 if (seen_empty_else
)
3716 gfc_error ("ELSEWHERE statement at %C follows previous "
3717 "unmasked ELSEWHERE");
3718 reject_statement ();
3722 if (new_st
.expr1
== NULL
)
3723 seen_empty_else
= 1;
3725 d
= new_level (gfc_state_stack
->head
);
3727 d
->expr1
= new_st
.expr1
;
3729 accept_statement (st
);
3734 accept_statement (st
);
3738 gfc_error ("Unexpected %s statement in WHERE block at %C",
3739 gfc_ascii_statement (st
));
3740 reject_statement ();
3744 while (st
!= ST_END_WHERE
);
3750 /* Parse a FORALL block (not a simple FORALL statement). */
3753 parse_forall_block (void)
3759 accept_statement (ST_FORALL_BLOCK
);
3760 top
= gfc_state_stack
->tail
;
3762 push_state (&s
, COMP_FORALL
, gfc_new_block
);
3764 d
= add_statement ();
3765 d
->op
= EXEC_FORALL
;
3770 st
= next_statement ();
3775 case ST_POINTER_ASSIGNMENT
:
3778 accept_statement (st
);
3781 case ST_WHERE_BLOCK
:
3782 parse_where_block ();
3785 case ST_FORALL_BLOCK
:
3786 parse_forall_block ();
3790 accept_statement (st
);
3797 gfc_error ("Unexpected %s statement in FORALL block at %C",
3798 gfc_ascii_statement (st
));
3800 reject_statement ();
3804 while (st
!= ST_END_FORALL
);
3810 static gfc_statement
parse_executable (gfc_statement
);
3812 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
3815 parse_if_block (void)
3824 accept_statement (ST_IF_BLOCK
);
3826 top
= gfc_state_stack
->tail
;
3827 push_state (&s
, COMP_IF
, gfc_new_block
);
3829 new_st
.op
= EXEC_IF
;
3830 d
= add_statement ();
3832 d
->expr1
= top
->expr1
;
3838 st
= parse_executable (ST_NONE
);
3848 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
3849 "statement at %L", &else_locus
);
3851 reject_statement ();
3855 d
= new_level (gfc_state_stack
->head
);
3857 d
->expr1
= new_st
.expr1
;
3859 accept_statement (st
);
3866 gfc_error ("Duplicate ELSE statements at %L and %C",
3868 reject_statement ();
3873 else_locus
= gfc_current_locus
;
3875 d
= new_level (gfc_state_stack
->head
);
3878 accept_statement (st
);
3886 unexpected_statement (st
);
3890 while (st
!= ST_ENDIF
);
3893 accept_statement (st
);
3897 /* Parse a SELECT block. */
3900 parse_select_block (void)
3906 accept_statement (ST_SELECT_CASE
);
3908 cp
= gfc_state_stack
->tail
;
3909 push_state (&s
, COMP_SELECT
, gfc_new_block
);
3911 /* Make sure that the next statement is a CASE or END SELECT. */
3914 st
= next_statement ();
3917 if (st
== ST_END_SELECT
)
3919 /* Empty SELECT CASE is OK. */
3920 accept_statement (st
);
3927 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
3930 reject_statement ();
3933 /* At this point, we're got a nonempty select block. */
3934 cp
= new_level (cp
);
3937 accept_statement (st
);
3941 st
= parse_executable (ST_NONE
);
3948 cp
= new_level (gfc_state_stack
->head
);
3950 gfc_clear_new_st ();
3952 accept_statement (st
);
3958 /* Can't have an executable statement because of
3959 parse_executable(). */
3961 unexpected_statement (st
);
3965 while (st
!= ST_END_SELECT
);
3968 accept_statement (st
);
3972 /* Pop the current selector from the SELECT TYPE stack. */
3975 select_type_pop (void)
3977 gfc_select_type_stack
*old
= select_type_stack
;
3978 select_type_stack
= old
->prev
;
3983 /* Parse a SELECT TYPE construct (F03:R821). */
3986 parse_select_type_block (void)
3992 accept_statement (ST_SELECT_TYPE
);
3994 cp
= gfc_state_stack
->tail
;
3995 push_state (&s
, COMP_SELECT_TYPE
, gfc_new_block
);
3997 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
4001 st
= next_statement ();
4004 if (st
== ST_END_SELECT
)
4005 /* Empty SELECT CASE is OK. */
4007 if (st
== ST_TYPE_IS
|| st
== ST_CLASS_IS
)
4010 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
4011 "following SELECT TYPE at %C");
4013 reject_statement ();
4016 /* At this point, we're got a nonempty select block. */
4017 cp
= new_level (cp
);
4020 accept_statement (st
);
4024 st
= parse_executable (ST_NONE
);
4032 cp
= new_level (gfc_state_stack
->head
);
4034 gfc_clear_new_st ();
4036 accept_statement (st
);
4042 /* Can't have an executable statement because of
4043 parse_executable(). */
4045 unexpected_statement (st
);
4049 while (st
!= ST_END_SELECT
);
4053 accept_statement (st
);
4054 gfc_current_ns
= gfc_current_ns
->parent
;
4059 /* Given a symbol, make sure it is not an iteration variable for a DO
4060 statement. This subroutine is called when the symbol is seen in a
4061 context that causes it to become redefined. If the symbol is an
4062 iterator, we generate an error message and return nonzero. */
4065 gfc_check_do_variable (gfc_symtree
*st
)
4069 for (s
=gfc_state_stack
; s
; s
= s
->previous
)
4070 if (s
->do_variable
== st
)
4072 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
4073 "loop beginning at %L", st
->name
, &s
->head
->loc
);
4081 /* Checks to see if the current statement label closes an enddo.
4082 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
4083 an error) if it incorrectly closes an ENDDO. */
4086 check_do_closure (void)
4090 if (gfc_statement_label
== NULL
)
4093 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
4094 if (p
->state
== COMP_DO
|| p
->state
== COMP_DO_CONCURRENT
)
4098 return 0; /* No loops to close */
4100 if (p
->ext
.end_do_label
== gfc_statement_label
)
4102 if (p
== gfc_state_stack
)
4105 gfc_error ("End of nonblock DO statement at %C is within another block");
4109 /* At this point, the label doesn't terminate the innermost loop.
4110 Make sure it doesn't terminate another one. */
4111 for (; p
; p
= p
->previous
)
4112 if ((p
->state
== COMP_DO
|| p
->state
== COMP_DO_CONCURRENT
)
4113 && p
->ext
.end_do_label
== gfc_statement_label
)
4115 gfc_error ("End of nonblock DO statement at %C is interwoven "
4116 "with another DO loop");
4124 /* Parse a series of contained program units. */
4126 static void parse_progunit (gfc_statement
);
4129 /* Parse a CRITICAL block. */
4132 parse_critical_block (void)
4135 gfc_state_data s
, *sd
;
4138 for (sd
= gfc_state_stack
; sd
; sd
= sd
->previous
)
4139 if (sd
->state
== COMP_OMP_STRUCTURED_BLOCK
)
4140 gfc_error_now (is_oacc (sd
)
4141 ? "CRITICAL block inside of OpenACC region at %C"
4142 : "CRITICAL block inside of OpenMP region at %C");
4144 s
.ext
.end_do_label
= new_st
.label1
;
4146 accept_statement (ST_CRITICAL
);
4147 top
= gfc_state_stack
->tail
;
4149 push_state (&s
, COMP_CRITICAL
, gfc_new_block
);
4151 d
= add_statement ();
4152 d
->op
= EXEC_CRITICAL
;
4157 st
= parse_executable (ST_NONE
);
4165 case ST_END_CRITICAL
:
4166 if (s
.ext
.end_do_label
!= NULL
4167 && s
.ext
.end_do_label
!= gfc_statement_label
)
4168 gfc_error_now ("Statement label in END CRITICAL at %C does not "
4169 "match CRITICAL label");
4171 if (gfc_statement_label
!= NULL
)
4173 new_st
.op
= EXEC_NOP
;
4179 unexpected_statement (st
);
4183 while (st
!= ST_END_CRITICAL
);
4186 accept_statement (st
);
4190 /* Set up the local namespace for a BLOCK construct. */
4193 gfc_build_block_ns (gfc_namespace
*parent_ns
)
4195 gfc_namespace
* my_ns
;
4196 static int numblock
= 1;
4198 my_ns
= gfc_get_namespace (parent_ns
, 1);
4199 my_ns
->construct_entities
= 1;
4201 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
4202 code generation (so it must not be NULL).
4203 We set its recursive argument if our container procedure is recursive, so
4204 that local variables are accordingly placed on the stack when it
4205 will be necessary. */
4207 my_ns
->proc_name
= gfc_new_block
;
4211 char buffer
[20]; /* Enough to hold "block@2147483648\n". */
4213 snprintf(buffer
, sizeof(buffer
), "block@%d", numblock
++);
4214 gfc_get_symbol (buffer
, my_ns
, &my_ns
->proc_name
);
4215 t
= gfc_add_flavor (&my_ns
->proc_name
->attr
, FL_LABEL
,
4216 my_ns
->proc_name
->name
, NULL
);
4218 gfc_commit_symbol (my_ns
->proc_name
);
4221 if (parent_ns
->proc_name
)
4222 my_ns
->proc_name
->attr
.recursive
= parent_ns
->proc_name
->attr
.recursive
;
4228 /* Parse a BLOCK construct. */
4231 parse_block_construct (void)
4233 gfc_namespace
* my_ns
;
4234 gfc_namespace
* my_parent
;
4237 gfc_notify_std (GFC_STD_F2008
, "BLOCK construct at %C");
4239 my_ns
= gfc_build_block_ns (gfc_current_ns
);
4241 new_st
.op
= EXEC_BLOCK
;
4242 new_st
.ext
.block
.ns
= my_ns
;
4243 new_st
.ext
.block
.assoc
= NULL
;
4244 accept_statement (ST_BLOCK
);
4246 push_state (&s
, COMP_BLOCK
, my_ns
->proc_name
);
4247 gfc_current_ns
= my_ns
;
4248 my_parent
= my_ns
->parent
;
4250 parse_progunit (ST_NONE
);
4252 /* Don't depend on the value of gfc_current_ns; it might have been
4253 reset if the block had errors and was cleaned up. */
4254 gfc_current_ns
= my_parent
;
4260 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
4261 behind the scenes with compiler-generated variables. */
4264 parse_associate (void)
4266 gfc_namespace
* my_ns
;
4269 gfc_association_list
* a
;
4271 gfc_notify_std (GFC_STD_F2003
, "ASSOCIATE construct at %C");
4273 my_ns
= gfc_build_block_ns (gfc_current_ns
);
4275 new_st
.op
= EXEC_BLOCK
;
4276 new_st
.ext
.block
.ns
= my_ns
;
4277 gcc_assert (new_st
.ext
.block
.assoc
);
4279 /* Add all associate-names as BLOCK variables. Creating them is enough
4280 for now, they'll get their values during trans-* phase. */
4281 gfc_current_ns
= my_ns
;
4282 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
4286 gfc_array_ref
*array_ref
;
4288 if (gfc_get_sym_tree (a
->name
, NULL
, &a
->st
, false))
4292 sym
->attr
.flavor
= FL_VARIABLE
;
4294 sym
->declared_at
= a
->where
;
4295 gfc_set_sym_referenced (sym
);
4297 /* Initialize the typespec. It is not available in all cases,
4298 however, as it may only be set on the target during resolution.
4299 Still, sometimes it helps to have it right now -- especially
4300 for parsing component references on the associate-name
4301 in case of association to a derived-type. */
4302 sym
->ts
= a
->target
->ts
;
4304 /* Check if the target expression is array valued. This can not always
4305 be done by looking at target.rank, because that might not have been
4306 set yet. Therefore traverse the chain of refs, looking for the last
4307 array ref and evaluate that. */
4309 for (ref
= a
->target
->ref
; ref
; ref
= ref
->next
)
4310 if (ref
->type
== REF_ARRAY
)
4311 array_ref
= &ref
->u
.ar
;
4312 if (array_ref
|| a
->target
->rank
)
4319 /* Count the dimension, that have a non-scalar extend. */
4320 for (dim
= 0; dim
< array_ref
->dimen
; ++dim
)
4321 if (array_ref
->dimen_type
[dim
] != DIMEN_ELEMENT
4322 && !(array_ref
->dimen_type
[dim
] == DIMEN_UNKNOWN
4323 && array_ref
->end
[dim
] == NULL
4324 && array_ref
->start
[dim
] != NULL
))
4328 rank
= a
->target
->rank
;
4329 /* When the rank is greater than zero then sym will be an array. */
4330 if (sym
->ts
.type
== BT_CLASS
)
4332 if ((!CLASS_DATA (sym
)->as
&& rank
!= 0)
4333 || (CLASS_DATA (sym
)->as
4334 && CLASS_DATA (sym
)->as
->rank
!= rank
))
4336 /* Don't just (re-)set the attr and as in the sym.ts,
4337 because this modifies the target's attr and as. Copy the
4338 data and do a build_class_symbol. */
4339 symbol_attribute attr
= CLASS_DATA (a
->target
)->attr
;
4340 int corank
= gfc_get_corank (a
->target
);
4345 as
= gfc_get_array_spec ();
4346 as
->type
= AS_DEFERRED
;
4348 as
->corank
= corank
;
4349 attr
.dimension
= rank
? 1 : 0;
4350 attr
.codimension
= corank
? 1 : 0;
4355 attr
.dimension
= attr
.codimension
= 0;
4358 type
= CLASS_DATA (sym
)->ts
;
4359 if (!gfc_build_class_symbol (&type
,
4363 sym
->ts
.type
= BT_CLASS
;
4364 sym
->attr
.class_ok
= 1;
4367 sym
->attr
.class_ok
= 1;
4369 else if ((!sym
->as
&& rank
!= 0)
4370 || (sym
->as
&& sym
->as
->rank
!= rank
))
4372 as
= gfc_get_array_spec ();
4373 as
->type
= AS_DEFERRED
;
4375 as
->corank
= gfc_get_corank (a
->target
);
4377 sym
->attr
.dimension
= 1;
4379 sym
->attr
.codimension
= 1;
4384 accept_statement (ST_ASSOCIATE
);
4385 push_state (&s
, COMP_ASSOCIATE
, my_ns
->proc_name
);
4388 st
= parse_executable (ST_NONE
);
4395 accept_statement (st
);
4396 my_ns
->code
= gfc_state_stack
->head
;
4400 unexpected_statement (st
);
4404 gfc_current_ns
= gfc_current_ns
->parent
;
4409 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4410 handled inside of parse_executable(), because they aren't really
4414 parse_do_block (void)
4423 s
.ext
.end_do_label
= new_st
.label1
;
4425 if (new_st
.ext
.iterator
!= NULL
)
4426 stree
= new_st
.ext
.iterator
->var
->symtree
;
4430 accept_statement (ST_DO
);
4432 top
= gfc_state_stack
->tail
;
4433 push_state (&s
, do_op
== EXEC_DO_CONCURRENT
? COMP_DO_CONCURRENT
: COMP_DO
,
4436 s
.do_variable
= stree
;
4438 top
->block
= new_level (top
);
4439 top
->block
->op
= EXEC_DO
;
4442 st
= parse_executable (ST_NONE
);
4450 if (s
.ext
.end_do_label
!= NULL
4451 && s
.ext
.end_do_label
!= gfc_statement_label
)
4452 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4455 if (gfc_statement_label
!= NULL
)
4457 new_st
.op
= EXEC_NOP
;
4462 case ST_IMPLIED_ENDDO
:
4463 /* If the do-stmt of this DO construct has a do-construct-name,
4464 the corresponding end-do must be an end-do-stmt (with a matching
4465 name, but in that case we must have seen ST_ENDDO first).
4466 We only complain about this in pedantic mode. */
4467 if (gfc_current_block () != NULL
)
4468 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4469 &gfc_current_block()->declared_at
);
4474 unexpected_statement (st
);
4479 accept_statement (st
);
4483 /* Parse the statements of OpenMP do/parallel do. */
4485 static gfc_statement
4486 parse_omp_do (gfc_statement omp_st
)
4492 accept_statement (omp_st
);
4494 cp
= gfc_state_stack
->tail
;
4495 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
4496 np
= new_level (cp
);
4502 st
= next_statement ();
4505 else if (st
== ST_DO
)
4508 unexpected_statement (st
);
4512 if (gfc_statement_label
!= NULL
4513 && gfc_state_stack
->previous
!= NULL
4514 && gfc_state_stack
->previous
->state
== COMP_DO
4515 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
4523 there should be no !$OMP END DO. */
4525 return ST_IMPLIED_ENDDO
;
4528 check_do_closure ();
4531 st
= next_statement ();
4532 gfc_statement omp_end_st
= ST_OMP_END_DO
;
4535 case ST_OMP_DISTRIBUTE
: omp_end_st
= ST_OMP_END_DISTRIBUTE
; break;
4536 case ST_OMP_DISTRIBUTE_PARALLEL_DO
:
4537 omp_end_st
= ST_OMP_END_DISTRIBUTE_PARALLEL_DO
;
4539 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
4540 omp_end_st
= ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD
;
4542 case ST_OMP_DISTRIBUTE_SIMD
:
4543 omp_end_st
= ST_OMP_END_DISTRIBUTE_SIMD
;
4545 case ST_OMP_DO
: omp_end_st
= ST_OMP_END_DO
; break;
4546 case ST_OMP_DO_SIMD
: omp_end_st
= ST_OMP_END_DO_SIMD
; break;
4547 case ST_OMP_PARALLEL_DO
: omp_end_st
= ST_OMP_END_PARALLEL_DO
; break;
4548 case ST_OMP_PARALLEL_DO_SIMD
:
4549 omp_end_st
= ST_OMP_END_PARALLEL_DO_SIMD
;
4551 case ST_OMP_SIMD
: omp_end_st
= ST_OMP_END_SIMD
; break;
4552 case ST_OMP_TARGET_TEAMS_DISTRIBUTE
:
4553 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE
;
4555 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4556 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
;
4558 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4559 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
;
4561 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
4562 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD
;
4564 case ST_OMP_TEAMS_DISTRIBUTE
:
4565 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE
;
4567 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4568 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO
;
4570 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4571 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
;
4573 case ST_OMP_TEAMS_DISTRIBUTE_SIMD
:
4574 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_SIMD
;
4576 default: gcc_unreachable ();
4578 if (st
== omp_end_st
)
4580 if (new_st
.op
== EXEC_OMP_END_NOWAIT
)
4581 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
4583 gcc_assert (new_st
.op
== EXEC_NOP
);
4584 gfc_clear_new_st ();
4585 gfc_commit_symbols ();
4586 gfc_warning_check ();
4587 st
= next_statement ();
4593 /* Parse the statements of OpenMP atomic directive. */
4595 static gfc_statement
4596 parse_omp_oacc_atomic (bool omp_p
)
4598 gfc_statement st
, st_atomic
, st_end_atomic
;
4605 st_atomic
= ST_OMP_ATOMIC
;
4606 st_end_atomic
= ST_OMP_END_ATOMIC
;
4610 st_atomic
= ST_OACC_ATOMIC
;
4611 st_end_atomic
= ST_OACC_END_ATOMIC
;
4613 accept_statement (st_atomic
);
4615 cp
= gfc_state_stack
->tail
;
4616 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
4617 np
= new_level (cp
);
4620 count
= 1 + ((cp
->ext
.omp_atomic
& GFC_OMP_ATOMIC_MASK
)
4621 == GFC_OMP_ATOMIC_CAPTURE
);
4625 st
= next_statement ();
4628 else if (st
== ST_ASSIGNMENT
)
4630 accept_statement (st
);
4634 unexpected_statement (st
);
4639 st
= next_statement ();
4640 if (st
== st_end_atomic
)
4642 gfc_clear_new_st ();
4643 gfc_commit_symbols ();
4644 gfc_warning_check ();
4645 st
= next_statement ();
4647 else if ((cp
->ext
.omp_atomic
& GFC_OMP_ATOMIC_MASK
)
4648 == GFC_OMP_ATOMIC_CAPTURE
)
4649 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4654 /* Parse the statements of an OpenACC structured block. */
4657 parse_oacc_structured_block (gfc_statement acc_st
)
4659 gfc_statement st
, acc_end_st
;
4661 gfc_state_data s
, *sd
;
4663 for (sd
= gfc_state_stack
; sd
; sd
= sd
->previous
)
4664 if (sd
->state
== COMP_CRITICAL
)
4665 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4667 accept_statement (acc_st
);
4669 cp
= gfc_state_stack
->tail
;
4670 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
4671 np
= new_level (cp
);
4676 case ST_OACC_PARALLEL
:
4677 acc_end_st
= ST_OACC_END_PARALLEL
;
4679 case ST_OACC_KERNELS
:
4680 acc_end_st
= ST_OACC_END_KERNELS
;
4683 acc_end_st
= ST_OACC_END_DATA
;
4685 case ST_OACC_HOST_DATA
:
4686 acc_end_st
= ST_OACC_END_HOST_DATA
;
4694 st
= parse_executable (ST_NONE
);
4697 else if (st
!= acc_end_st
)
4699 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st
));
4700 reject_statement ();
4703 while (st
!= acc_end_st
);
4705 gcc_assert (new_st
.op
== EXEC_NOP
);
4707 gfc_clear_new_st ();
4708 gfc_commit_symbols ();
4709 gfc_warning_check ();
4713 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4715 static gfc_statement
4716 parse_oacc_loop (gfc_statement acc_st
)
4720 gfc_state_data s
, *sd
;
4722 for (sd
= gfc_state_stack
; sd
; sd
= sd
->previous
)
4723 if (sd
->state
== COMP_CRITICAL
)
4724 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4726 accept_statement (acc_st
);
4728 cp
= gfc_state_stack
->tail
;
4729 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
4730 np
= new_level (cp
);
4736 st
= next_statement ();
4739 else if (st
== ST_DO
)
4743 gfc_error ("Expected DO loop at %C");
4744 reject_statement ();
4749 if (gfc_statement_label
!= NULL
4750 && gfc_state_stack
->previous
!= NULL
4751 && gfc_state_stack
->previous
->state
== COMP_DO
4752 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
4755 return ST_IMPLIED_ENDDO
;
4758 check_do_closure ();
4761 st
= next_statement ();
4762 if (st
== ST_OACC_END_LOOP
)
4763 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
4764 if ((acc_st
== ST_OACC_PARALLEL_LOOP
&& st
== ST_OACC_END_PARALLEL_LOOP
) ||
4765 (acc_st
== ST_OACC_KERNELS_LOOP
&& st
== ST_OACC_END_KERNELS_LOOP
) ||
4766 (acc_st
== ST_OACC_LOOP
&& st
== ST_OACC_END_LOOP
))
4768 gcc_assert (new_st
.op
== EXEC_NOP
);
4769 gfc_clear_new_st ();
4770 gfc_commit_symbols ();
4771 gfc_warning_check ();
4772 st
= next_statement ();
4778 /* Parse the statements of an OpenMP structured block. */
4781 parse_omp_structured_block (gfc_statement omp_st
, bool workshare_stmts_only
)
4783 gfc_statement st
, omp_end_st
;
4787 accept_statement (omp_st
);
4789 cp
= gfc_state_stack
->tail
;
4790 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
4791 np
= new_level (cp
);
4797 case ST_OMP_PARALLEL
:
4798 omp_end_st
= ST_OMP_END_PARALLEL
;
4800 case ST_OMP_PARALLEL_SECTIONS
:
4801 omp_end_st
= ST_OMP_END_PARALLEL_SECTIONS
;
4803 case ST_OMP_SECTIONS
:
4804 omp_end_st
= ST_OMP_END_SECTIONS
;
4806 case ST_OMP_ORDERED
:
4807 omp_end_st
= ST_OMP_END_ORDERED
;
4809 case ST_OMP_CRITICAL
:
4810 omp_end_st
= ST_OMP_END_CRITICAL
;
4813 omp_end_st
= ST_OMP_END_MASTER
;
4816 omp_end_st
= ST_OMP_END_SINGLE
;
4819 omp_end_st
= ST_OMP_END_TARGET
;
4821 case ST_OMP_TARGET_DATA
:
4822 omp_end_st
= ST_OMP_END_TARGET_DATA
;
4824 case ST_OMP_TARGET_TEAMS
:
4825 omp_end_st
= ST_OMP_END_TARGET_TEAMS
;
4827 case ST_OMP_TARGET_TEAMS_DISTRIBUTE
:
4828 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE
;
4830 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4831 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
;
4833 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4834 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
;
4836 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
4837 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD
;
4840 omp_end_st
= ST_OMP_END_TASK
;
4842 case ST_OMP_TASKGROUP
:
4843 omp_end_st
= ST_OMP_END_TASKGROUP
;
4846 omp_end_st
= ST_OMP_END_TEAMS
;
4848 case ST_OMP_TEAMS_DISTRIBUTE
:
4849 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE
;
4851 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4852 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO
;
4854 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4855 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
;
4857 case ST_OMP_TEAMS_DISTRIBUTE_SIMD
:
4858 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_SIMD
;
4860 case ST_OMP_DISTRIBUTE
:
4861 omp_end_st
= ST_OMP_END_DISTRIBUTE
;
4863 case ST_OMP_DISTRIBUTE_PARALLEL_DO
:
4864 omp_end_st
= ST_OMP_END_DISTRIBUTE_PARALLEL_DO
;
4866 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
4867 omp_end_st
= ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD
;
4869 case ST_OMP_DISTRIBUTE_SIMD
:
4870 omp_end_st
= ST_OMP_END_DISTRIBUTE_SIMD
;
4872 case ST_OMP_WORKSHARE
:
4873 omp_end_st
= ST_OMP_END_WORKSHARE
;
4875 case ST_OMP_PARALLEL_WORKSHARE
:
4876 omp_end_st
= ST_OMP_END_PARALLEL_WORKSHARE
;
4884 if (workshare_stmts_only
)
4886 /* Inside of !$omp workshare, only
4889 where statements and constructs
4890 forall statements and constructs
4894 are allowed. For !$omp critical these
4895 restrictions apply recursively. */
4898 st
= next_statement ();
4909 accept_statement (st
);
4912 case ST_WHERE_BLOCK
:
4913 parse_where_block ();
4916 case ST_FORALL_BLOCK
:
4917 parse_forall_block ();
4920 case ST_OMP_PARALLEL
:
4921 case ST_OMP_PARALLEL_SECTIONS
:
4922 parse_omp_structured_block (st
, false);
4925 case ST_OMP_PARALLEL_WORKSHARE
:
4926 case ST_OMP_CRITICAL
:
4927 parse_omp_structured_block (st
, true);
4930 case ST_OMP_PARALLEL_DO
:
4931 case ST_OMP_PARALLEL_DO_SIMD
:
4932 st
= parse_omp_do (st
);
4936 st
= parse_omp_oacc_atomic (true);
4947 st
= next_statement ();
4951 st
= parse_executable (ST_NONE
);
4954 else if (st
== ST_OMP_SECTION
4955 && (omp_st
== ST_OMP_SECTIONS
4956 || omp_st
== ST_OMP_PARALLEL_SECTIONS
))
4958 np
= new_level (np
);
4962 else if (st
!= omp_end_st
)
4963 unexpected_statement (st
);
4965 while (st
!= omp_end_st
);
4969 case EXEC_OMP_END_NOWAIT
:
4970 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
4972 case EXEC_OMP_CRITICAL
:
4973 if (((cp
->ext
.omp_name
== NULL
) ^ (new_st
.ext
.omp_name
== NULL
))
4974 || (new_st
.ext
.omp_name
!= NULL
4975 && strcmp (cp
->ext
.omp_name
, new_st
.ext
.omp_name
) != 0))
4976 gfc_error ("Name after !$omp critical and !$omp end critical does "
4978 free (CONST_CAST (char *, new_st
.ext
.omp_name
));
4980 case EXEC_OMP_END_SINGLE
:
4981 cp
->ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
]
4982 = new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
];
4983 new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
] = NULL
;
4984 gfc_free_omp_clauses (new_st
.ext
.omp_clauses
);
4992 gfc_clear_new_st ();
4993 gfc_commit_symbols ();
4994 gfc_warning_check ();
4999 /* Accept a series of executable statements. We return the first
5000 statement that doesn't fit to the caller. Any block statements are
5001 passed on to the correct handler, which usually passes the buck
5004 static gfc_statement
5005 parse_executable (gfc_statement st
)
5010 st
= next_statement ();
5014 close_flag
= check_do_closure ();
5019 case ST_END_PROGRAM
:
5022 case ST_END_FUNCTION
:
5027 case ST_END_SUBROUTINE
:
5032 case ST_SELECT_CASE
:
5033 gfc_error ("%s statement at %C cannot terminate a non-block "
5034 "DO loop", gfc_ascii_statement (st
));
5047 gfc_notify_std (GFC_STD_F95_OBS
, "DATA statement at %C after the "
5048 "first executable statement");
5054 accept_statement (st
);
5055 if (close_flag
== 1)
5056 return ST_IMPLIED_ENDDO
;
5060 parse_block_construct ();
5071 case ST_SELECT_CASE
:
5072 parse_select_block ();
5075 case ST_SELECT_TYPE
:
5076 parse_select_type_block();
5081 if (check_do_closure () == 1)
5082 return ST_IMPLIED_ENDDO
;
5086 parse_critical_block ();
5089 case ST_WHERE_BLOCK
:
5090 parse_where_block ();
5093 case ST_FORALL_BLOCK
:
5094 parse_forall_block ();
5097 case ST_OACC_PARALLEL_LOOP
:
5098 case ST_OACC_KERNELS_LOOP
:
5100 st
= parse_oacc_loop (st
);
5101 if (st
== ST_IMPLIED_ENDDO
)
5105 case ST_OACC_PARALLEL
:
5106 case ST_OACC_KERNELS
:
5108 case ST_OACC_HOST_DATA
:
5109 parse_oacc_structured_block (st
);
5112 case ST_OMP_PARALLEL
:
5113 case ST_OMP_PARALLEL_SECTIONS
:
5114 case ST_OMP_SECTIONS
:
5115 case ST_OMP_ORDERED
:
5116 case ST_OMP_CRITICAL
:
5120 case ST_OMP_TARGET_DATA
:
5121 case ST_OMP_TARGET_TEAMS
:
5124 case ST_OMP_TASKGROUP
:
5125 parse_omp_structured_block (st
, false);
5128 case ST_OMP_WORKSHARE
:
5129 case ST_OMP_PARALLEL_WORKSHARE
:
5130 parse_omp_structured_block (st
, true);
5133 case ST_OMP_DISTRIBUTE
:
5134 case ST_OMP_DISTRIBUTE_PARALLEL_DO
:
5135 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
5136 case ST_OMP_DISTRIBUTE_SIMD
:
5138 case ST_OMP_DO_SIMD
:
5139 case ST_OMP_PARALLEL_DO
:
5140 case ST_OMP_PARALLEL_DO_SIMD
:
5142 case ST_OMP_TARGET_TEAMS_DISTRIBUTE
:
5143 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
5144 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
5145 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
5146 case ST_OMP_TEAMS_DISTRIBUTE
:
5147 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
5148 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
5149 case ST_OMP_TEAMS_DISTRIBUTE_SIMD
:
5150 st
= parse_omp_do (st
);
5151 if (st
== ST_IMPLIED_ENDDO
)
5155 case ST_OACC_ATOMIC
:
5156 st
= parse_omp_oacc_atomic (false);
5160 st
= parse_omp_oacc_atomic (true);
5167 st
= next_statement ();
5172 /* Fix the symbols for sibling functions. These are incorrectly added to
5173 the child namespace as the parser didn't know about this procedure. */
5176 gfc_fixup_sibling_symbols (gfc_symbol
*sym
, gfc_namespace
*siblings
)
5180 gfc_symbol
*old_sym
;
5182 for (ns
= siblings
; ns
; ns
= ns
->sibling
)
5184 st
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
5186 if (!st
|| (st
->n
.sym
->attr
.dummy
&& ns
== st
->n
.sym
->ns
))
5187 goto fixup_contained
;
5189 if ((st
->n
.sym
->attr
.flavor
== FL_DERIVED
5190 && sym
->attr
.generic
&& sym
->attr
.function
)
5191 ||(sym
->attr
.flavor
== FL_DERIVED
5192 && st
->n
.sym
->attr
.generic
&& st
->n
.sym
->attr
.function
))
5193 goto fixup_contained
;
5195 old_sym
= st
->n
.sym
;
5196 if (old_sym
->ns
== ns
5197 && !old_sym
->attr
.contained
5199 /* By 14.6.1.3, host association should be excluded
5200 for the following. */
5201 && !(old_sym
->attr
.external
5202 || (old_sym
->ts
.type
!= BT_UNKNOWN
5203 && !old_sym
->attr
.implicit_type
)
5204 || old_sym
->attr
.flavor
== FL_PARAMETER
5205 || old_sym
->attr
.use_assoc
5206 || old_sym
->attr
.in_common
5207 || old_sym
->attr
.in_equivalence
5208 || old_sym
->attr
.data
5209 || old_sym
->attr
.dummy
5210 || old_sym
->attr
.result
5211 || old_sym
->attr
.dimension
5212 || old_sym
->attr
.allocatable
5213 || old_sym
->attr
.intrinsic
5214 || old_sym
->attr
.generic
5215 || old_sym
->attr
.flavor
== FL_NAMELIST
5216 || old_sym
->attr
.flavor
== FL_LABEL
5217 || old_sym
->attr
.proc
== PROC_ST_FUNCTION
))
5219 /* Replace it with the symbol from the parent namespace. */
5223 gfc_release_symbol (old_sym
);
5227 /* Do the same for any contained procedures. */
5228 gfc_fixup_sibling_symbols (sym
, ns
->contained
);
5233 parse_contained (int module
)
5235 gfc_namespace
*ns
, *parent_ns
, *tmp
;
5236 gfc_state_data s1
, s2
;
5240 int contains_statements
= 0;
5243 push_state (&s1
, COMP_CONTAINS
, NULL
);
5244 parent_ns
= gfc_current_ns
;
5248 gfc_current_ns
= gfc_get_namespace (parent_ns
, 1);
5250 gfc_current_ns
->sibling
= parent_ns
->contained
;
5251 parent_ns
->contained
= gfc_current_ns
;
5254 /* Process the next available statement. We come here if we got an error
5255 and rejected the last statement. */
5256 st
= next_statement ();
5265 contains_statements
= 1;
5266 accept_statement (st
);
5269 (st
== ST_FUNCTION
) ? COMP_FUNCTION
: COMP_SUBROUTINE
,
5272 /* For internal procedures, create/update the symbol in the
5273 parent namespace. */
5277 if (gfc_get_symbol (gfc_new_block
->name
, parent_ns
, &sym
))
5278 gfc_error ("Contained procedure %qs at %C is already "
5279 "ambiguous", gfc_new_block
->name
);
5282 if (gfc_add_procedure (&sym
->attr
, PROC_INTERNAL
,
5284 &gfc_new_block
->declared_at
))
5286 if (st
== ST_FUNCTION
)
5287 gfc_add_function (&sym
->attr
, sym
->name
,
5288 &gfc_new_block
->declared_at
);
5290 gfc_add_subroutine (&sym
->attr
, sym
->name
,
5291 &gfc_new_block
->declared_at
);
5295 gfc_commit_symbols ();
5298 sym
= gfc_new_block
;
5300 /* Mark this as a contained function, so it isn't replaced
5301 by other module functions. */
5302 sym
->attr
.contained
= 1;
5304 /* Set implicit_pure so that it can be reset if any of the
5305 tests for purity fail. This is used for some optimisation
5306 during translation. */
5307 if (!sym
->attr
.pure
)
5308 sym
->attr
.implicit_pure
= 1;
5310 parse_progunit (ST_NONE
);
5312 /* Fix up any sibling functions that refer to this one. */
5313 gfc_fixup_sibling_symbols (sym
, gfc_current_ns
);
5314 /* Or refer to any of its alternate entry points. */
5315 for (el
= gfc_current_ns
->entries
; el
; el
= el
->next
)
5316 gfc_fixup_sibling_symbols (el
->sym
, gfc_current_ns
);
5318 gfc_current_ns
->code
= s2
.head
;
5319 gfc_current_ns
= parent_ns
;
5324 /* These statements are associated with the end of the host unit. */
5325 case ST_END_FUNCTION
:
5327 case ST_END_SUBMODULE
:
5328 case ST_END_PROGRAM
:
5329 case ST_END_SUBROUTINE
:
5330 accept_statement (st
);
5331 gfc_current_ns
->code
= s1
.head
;
5335 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
5336 gfc_ascii_statement (st
));
5337 reject_statement ();
5343 while (st
!= ST_END_FUNCTION
&& st
!= ST_END_SUBROUTINE
5344 && st
!= ST_END_MODULE
&& st
!= ST_END_SUBMODULE
5345 && st
!= ST_END_PROGRAM
);
5347 /* The first namespace in the list is guaranteed to not have
5348 anything (worthwhile) in it. */
5349 tmp
= gfc_current_ns
;
5350 gfc_current_ns
= parent_ns
;
5351 if (seen_error
&& tmp
->refs
> 1)
5352 gfc_free_namespace (tmp
);
5354 ns
= gfc_current_ns
->contained
;
5355 gfc_current_ns
->contained
= ns
->sibling
;
5356 gfc_free_namespace (ns
);
5359 if (!contains_statements
)
5360 gfc_notify_std (GFC_STD_F2008
, "CONTAINS statement without "
5361 "FUNCTION or SUBROUTINE statement at %C");
5365 /* The result variable in a MODULE PROCEDURE needs to be created and
5366 its characteristics copied from the interface since it is neither
5367 declared in the procedure declaration nor in the specification
5371 get_modproc_result (void)
5374 if (gfc_state_stack
->previous
5375 && gfc_state_stack
->previous
->state
== COMP_CONTAINS
5376 && gfc_state_stack
->previous
->previous
->state
== COMP_SUBMODULE
)
5378 proc
= gfc_current_ns
->proc_name
? gfc_current_ns
->proc_name
: NULL
;
5380 && proc
->attr
.function
5381 && proc
->ts
.interface
5382 && proc
->ts
.interface
->result
5383 && proc
->ts
.interface
->result
!= proc
->ts
.interface
)
5385 gfc_copy_dummy_sym (&proc
->result
, proc
->ts
.interface
->result
, 1);
5386 gfc_set_sym_referenced (proc
->result
);
5387 proc
->result
->attr
.if_source
= IFSRC_DECL
;
5388 gfc_commit_symbol (proc
->result
);
5394 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
5397 parse_progunit (gfc_statement st
)
5403 && gfc_new_block
->abr_modproc_decl
5404 && gfc_new_block
->attr
.function
)
5405 get_modproc_result ();
5407 st
= parse_spec (st
);
5414 /* This is not allowed within BLOCK! */
5415 if (gfc_current_state () != COMP_BLOCK
)
5420 accept_statement (st
);
5427 if (gfc_current_state () == COMP_FUNCTION
)
5428 gfc_check_function_type (gfc_current_ns
);
5433 st
= parse_executable (st
);
5441 /* This is not allowed within BLOCK! */
5442 if (gfc_current_state () != COMP_BLOCK
)
5447 accept_statement (st
);
5454 unexpected_statement (st
);
5455 reject_statement ();
5456 st
= next_statement ();
5462 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
5463 if (p
->state
== COMP_CONTAINS
)
5466 if (gfc_find_state (COMP_MODULE
) == true
5467 || gfc_find_state (COMP_SUBMODULE
) == true)
5472 gfc_error ("CONTAINS statement at %C is already in a contained "
5474 reject_statement ();
5475 st
= next_statement ();
5479 parse_contained (0);
5482 gfc_current_ns
->code
= gfc_state_stack
->head
;
5486 /* Come here to complain about a global symbol already in use as
5490 gfc_global_used (gfc_gsymbol
*sym
, locus
*where
)
5495 where
= &gfc_current_locus
;
5505 case GSYM_SUBROUTINE
:
5506 name
= "SUBROUTINE";
5511 case GSYM_BLOCK_DATA
:
5512 name
= "BLOCK DATA";
5518 gfc_internal_error ("gfc_global_used(): Bad type");
5522 if (sym
->binding_label
)
5523 gfc_error ("Global binding name %qs at %L is already being used as a %s "
5524 "at %L", sym
->binding_label
, where
, name
, &sym
->where
);
5526 gfc_error ("Global name %qs at %L is already being used as a %s at %L",
5527 sym
->name
, where
, name
, &sym
->where
);
5531 /* Parse a block data program unit. */
5534 parse_block_data (void)
5537 static locus blank_locus
;
5538 static int blank_block
=0;
5541 gfc_current_ns
->proc_name
= gfc_new_block
;
5542 gfc_current_ns
->is_block_data
= 1;
5544 if (gfc_new_block
== NULL
)
5547 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5548 "prior BLOCK DATA at %L", &blank_locus
);
5552 blank_locus
= gfc_current_locus
;
5557 s
= gfc_get_gsymbol (gfc_new_block
->name
);
5559 || (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_BLOCK_DATA
))
5560 gfc_global_used (s
, &gfc_new_block
->declared_at
);
5563 s
->type
= GSYM_BLOCK_DATA
;
5564 s
->where
= gfc_new_block
->declared_at
;
5569 st
= parse_spec (ST_NONE
);
5571 while (st
!= ST_END_BLOCK_DATA
)
5573 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5574 gfc_ascii_statement (st
));
5575 reject_statement ();
5576 st
= next_statement ();
5581 /* Following the association of the ancestor (sub)module symbols, they
5582 must be set host rather than use associated and all must be public.
5583 They are flagged up by 'used_in_submodule' so that they can be set
5584 DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl). Otherwise the
5585 linker chokes on multiple symbol definitions. */
5588 set_syms_host_assoc (gfc_symbol
*sym
)
5595 if (sym
->attr
.module_procedure
)
5596 sym
->attr
.external
= 0;
5598 /* sym->attr.access = ACCESS_PUBLIC; */
5600 sym
->attr
.use_assoc
= 0;
5601 sym
->attr
.host_assoc
= 1;
5602 sym
->attr
.used_in_submodule
=1;
5604 if (sym
->attr
.flavor
== FL_DERIVED
)
5606 for (c
= sym
->components
; c
; c
= c
->next
)
5607 c
->attr
.access
= ACCESS_PUBLIC
;
5611 /* Parse a module subprogram. */
5620 s
= gfc_get_gsymbol (gfc_new_block
->name
);
5621 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_MODULE
))
5622 gfc_global_used (s
, &gfc_new_block
->declared_at
);
5625 s
->type
= GSYM_MODULE
;
5626 s
->where
= gfc_new_block
->declared_at
;
5630 /* Something is nulling the module_list after this point. This is good
5631 since it allows us to 'USE' the parent modules that the submodule
5632 inherits and to set (most) of the symbols as host associated. */
5633 if (gfc_current_state () == COMP_SUBMODULE
)
5636 gfc_traverse_ns (gfc_current_ns
, set_syms_host_assoc
);
5639 st
= parse_spec (ST_NONE
);
5649 parse_contained (1);
5653 case ST_END_SUBMODULE
:
5654 accept_statement (st
);
5658 gfc_error ("Unexpected %s statement in MODULE at %C",
5659 gfc_ascii_statement (st
));
5662 reject_statement ();
5663 st
= next_statement ();
5667 /* Make sure not to free the namespace twice on error. */
5669 s
->ns
= gfc_current_ns
;
5673 /* Add a procedure name to the global symbol table. */
5676 add_global_procedure (bool sub
)
5680 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5681 name is a global identifier. */
5682 if (!gfc_new_block
->binding_label
|| gfc_notification_std (GFC_STD_F2008
))
5684 s
= gfc_get_gsymbol (gfc_new_block
->name
);
5687 || (s
->type
!= GSYM_UNKNOWN
5688 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
5690 gfc_global_used (s
, &gfc_new_block
->declared_at
);
5691 /* Silence follow-up errors. */
5692 gfc_new_block
->binding_label
= NULL
;
5696 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5697 s
->sym_name
= gfc_new_block
->name
;
5698 s
->where
= gfc_new_block
->declared_at
;
5700 s
->ns
= gfc_current_ns
;
5704 /* Don't add the symbol multiple times. */
5705 if (gfc_new_block
->binding_label
5706 && (!gfc_notification_std (GFC_STD_F2008
)
5707 || strcmp (gfc_new_block
->name
, gfc_new_block
->binding_label
) != 0))
5709 s
= gfc_get_gsymbol (gfc_new_block
->binding_label
);
5712 || (s
->type
!= GSYM_UNKNOWN
5713 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
5715 gfc_global_used (s
, &gfc_new_block
->declared_at
);
5716 /* Silence follow-up errors. */
5717 gfc_new_block
->binding_label
= NULL
;
5721 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5722 s
->sym_name
= gfc_new_block
->name
;
5723 s
->binding_label
= gfc_new_block
->binding_label
;
5724 s
->where
= gfc_new_block
->declared_at
;
5726 s
->ns
= gfc_current_ns
;
5732 /* Add a program to the global symbol table. */
5735 add_global_program (void)
5739 if (gfc_new_block
== NULL
)
5741 s
= gfc_get_gsymbol (gfc_new_block
->name
);
5743 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_PROGRAM
))
5744 gfc_global_used (s
, &gfc_new_block
->declared_at
);
5747 s
->type
= GSYM_PROGRAM
;
5748 s
->where
= gfc_new_block
->declared_at
;
5750 s
->ns
= gfc_current_ns
;
5755 /* Resolve all the program units. */
5757 resolve_all_program_units (gfc_namespace
*gfc_global_ns_list
)
5759 gfc_free_dt_list ();
5760 gfc_current_ns
= gfc_global_ns_list
;
5761 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
5763 if (gfc_current_ns
->proc_name
5764 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
5765 continue; /* Already resolved. */
5767 if (gfc_current_ns
->proc_name
)
5768 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
5769 gfc_resolve (gfc_current_ns
);
5770 gfc_current_ns
->derived_types
= gfc_derived_types
;
5771 gfc_derived_types
= NULL
;
5777 clean_up_modules (gfc_gsymbol
*gsym
)
5782 clean_up_modules (gsym
->left
);
5783 clean_up_modules (gsym
->right
);
5785 if (gsym
->type
!= GSYM_MODULE
|| !gsym
->ns
)
5788 gfc_current_ns
= gsym
->ns
;
5789 gfc_derived_types
= gfc_current_ns
->derived_types
;
5796 /* Translate all the program units. This could be in a different order
5797 to resolution if there are forward references in the file. */
5799 translate_all_program_units (gfc_namespace
*gfc_global_ns_list
)
5803 gfc_current_ns
= gfc_global_ns_list
;
5804 gfc_get_errors (NULL
, &errors
);
5806 /* We first translate all modules to make sure that later parts
5807 of the program can use the decl. Then we translate the nonmodules. */
5809 for (; !errors
&& 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
)
5815 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
5816 gfc_derived_types
= gfc_current_ns
->derived_types
;
5817 gfc_generate_module_code (gfc_current_ns
);
5818 gfc_current_ns
->translated
= 1;
5821 gfc_current_ns
= gfc_global_ns_list
;
5822 for (; !errors
&& gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
5824 if (gfc_current_ns
->proc_name
5825 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
5828 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
5829 gfc_derived_types
= gfc_current_ns
->derived_types
;
5830 gfc_generate_code (gfc_current_ns
);
5831 gfc_current_ns
->translated
= 1;
5834 /* Clean up all the namespaces after translation. */
5835 gfc_current_ns
= gfc_global_ns_list
;
5836 for (;gfc_current_ns
;)
5840 if (gfc_current_ns
->proc_name
5841 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
5843 gfc_current_ns
= gfc_current_ns
->sibling
;
5847 ns
= gfc_current_ns
->sibling
;
5848 gfc_derived_types
= gfc_current_ns
->derived_types
;
5850 gfc_current_ns
= ns
;
5853 clean_up_modules (gfc_gsym_root
);
5857 /* Top level parser. */
5860 gfc_parse_file (void)
5862 int seen_program
, errors_before
, errors
;
5863 gfc_state_data top
, s
;
5866 gfc_namespace
*next
;
5868 gfc_start_source_files ();
5870 top
.state
= COMP_NONE
;
5872 top
.previous
= NULL
;
5873 top
.head
= top
.tail
= NULL
;
5874 top
.do_variable
= NULL
;
5876 gfc_state_stack
= &top
;
5878 gfc_clear_new_st ();
5880 gfc_statement_label
= NULL
;
5882 if (setjmp (eof_buf
))
5883 return false; /* Come here on unexpected EOF */
5885 /* Prepare the global namespace that will contain the
5887 gfc_global_ns_list
= next
= NULL
;
5892 /* Exit early for empty files. */
5896 in_specification_block
= true;
5899 st
= next_statement ();
5908 goto duplicate_main
;
5910 prog_locus
= gfc_current_locus
;
5912 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
5913 main_program_symbol(gfc_current_ns
, gfc_new_block
->name
);
5914 accept_statement (st
);
5915 add_global_program ();
5916 parse_progunit (ST_NONE
);
5921 add_global_procedure (true);
5922 push_state (&s
, COMP_SUBROUTINE
, gfc_new_block
);
5923 accept_statement (st
);
5924 parse_progunit (ST_NONE
);
5929 add_global_procedure (false);
5930 push_state (&s
, COMP_FUNCTION
, gfc_new_block
);
5931 accept_statement (st
);
5932 parse_progunit (ST_NONE
);
5937 push_state (&s
, COMP_BLOCK_DATA
, gfc_new_block
);
5938 accept_statement (st
);
5939 parse_block_data ();
5943 push_state (&s
, COMP_MODULE
, gfc_new_block
);
5944 accept_statement (st
);
5946 gfc_get_errors (NULL
, &errors_before
);
5951 push_state (&s
, COMP_SUBMODULE
, gfc_new_block
);
5952 accept_statement (st
);
5954 gfc_get_errors (NULL
, &errors_before
);
5958 /* Anything else starts a nameless main program block. */
5961 goto duplicate_main
;
5963 prog_locus
= gfc_current_locus
;
5965 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
5966 main_program_symbol (gfc_current_ns
, "MAIN__");
5967 parse_progunit (st
);
5972 /* Handle the non-program units. */
5973 gfc_current_ns
->code
= s
.head
;
5975 gfc_resolve (gfc_current_ns
);
5977 /* Dump the parse tree if requested. */
5978 if (flag_dump_fortran_original
)
5979 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
5981 gfc_get_errors (NULL
, &errors
);
5982 if (s
.state
== COMP_MODULE
|| s
.state
== COMP_SUBMODULE
)
5984 gfc_dump_module (s
.sym
->name
, errors_before
== errors
);
5985 gfc_current_ns
->derived_types
= gfc_derived_types
;
5986 gfc_derived_types
= NULL
;
5992 gfc_generate_code (gfc_current_ns
);
6000 /* The main program and non-contained procedures are put
6001 in the global namespace list, so that they can be processed
6002 later and all their interfaces resolved. */
6003 gfc_current_ns
->code
= s
.head
;
6006 for (; next
->sibling
; next
= next
->sibling
)
6008 next
->sibling
= gfc_current_ns
;
6011 gfc_global_ns_list
= gfc_current_ns
;
6013 next
= gfc_current_ns
;
6020 /* Do the resolution. */
6021 resolve_all_program_units (gfc_global_ns_list
);
6023 /* Do the parse tree dump. */
6025 = flag_dump_fortran_original
? gfc_global_ns_list
: NULL
;
6027 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
6028 if (!gfc_current_ns
->proc_name
6029 || gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6031 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
6032 fputs ("------------------------------------------\n\n", stdout
);
6035 /* Do the translation. */
6036 translate_all_program_units (gfc_global_ns_list
);
6038 gfc_end_source_files ();
6042 /* If we see a duplicate main program, shut down. If the second
6043 instance is an implied main program, i.e. data decls or executable
6044 statements, we're in for lots of errors. */
6045 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus
);
6046 reject_statement ();
6051 /* Return true if this state data represents an OpenACC region. */
6053 is_oacc (gfc_state_data
*sd
)
6055 switch (sd
->construct
->op
)
6057 case EXEC_OACC_PARALLEL_LOOP
:
6058 case EXEC_OACC_PARALLEL
:
6059 case EXEC_OACC_KERNELS_LOOP
:
6060 case EXEC_OACC_KERNELS
:
6061 case EXEC_OACC_DATA
:
6062 case EXEC_OACC_HOST_DATA
:
6063 case EXEC_OACC_LOOP
:
6064 case EXEC_OACC_UPDATE
:
6065 case EXEC_OACC_WAIT
:
6066 case EXEC_OACC_CACHE
:
6067 case EXEC_OACC_ENTER_DATA
:
6068 case EXEC_OACC_EXIT_DATA
:
6069 case EXEC_OACC_ATOMIC
:
6070 case EXEC_OACC_ROUTINE
: