2 Copyright (C) 2000-2014 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/>. */
24 #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_buf old_error
;
112 gfc_push_error (&old_error
);
113 gfc_buffer_error (0);
115 gfc_buffer_error (1);
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 last_was_use_stmt
= false;
125 /* Figure out what the next statement is, (mostly) regardless of
126 proper ordering. The do...while(0) is there to prevent if/else
129 #define match(keyword, subr, st) \
131 if (match_word (keyword, subr, &old_locus) == MATCH_YES) \
134 undo_new_statement (); \
138 /* This is a specialist version of decode_statement that is used
139 for the specification statements in a function, whose
140 characteristics are deferred into the specification statements.
141 eg.: INTEGER (king = mykind) foo ()
142 USE mymodule, ONLY mykind.....
143 The KIND parameter needs a return after USE or IMPORT, whereas
144 derived type declarations can occur anywhere, up the executable
145 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
146 out of the correct kind of specification statements. */
148 decode_specification_statement (void)
154 if (gfc_match_eos () == MATCH_YES
)
157 old_locus
= gfc_current_locus
;
159 if (match_word ("use", gfc_match_use
, &old_locus
) == MATCH_YES
)
161 last_was_use_stmt
= true;
166 undo_new_statement ();
167 if (last_was_use_stmt
)
171 match ("import", gfc_match_import
, ST_IMPORT
);
173 if (gfc_current_block ()->result
->ts
.type
!= BT_DERIVED
)
176 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
177 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
178 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
180 /* General statement matching: Instead of testing every possible
181 statement, we eliminate most possibilities by peeking at the
184 c
= gfc_peek_ascii_char ();
189 match ("abstract% interface", gfc_match_abstract_interface
,
191 match ("allocatable", gfc_match_allocatable
, ST_ATTR_DECL
);
192 match ("asynchronous", gfc_match_asynchronous
, ST_ATTR_DECL
);
196 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
200 match ("codimension", gfc_match_codimension
, ST_ATTR_DECL
);
201 match ("contiguous", gfc_match_contiguous
, ST_ATTR_DECL
);
205 match ("data", gfc_match_data
, ST_DATA
);
206 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
210 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
211 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
212 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
213 match ("external", gfc_match_external
, ST_ATTR_DECL
);
217 match ("format", gfc_match_format
, ST_FORMAT
);
224 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
225 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
226 match ("interface", gfc_match_interface
, ST_INTERFACE
);
227 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
228 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
235 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
239 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
243 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
244 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
245 if (gfc_match_private (&st
) == MATCH_YES
)
247 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
248 if (gfc_match_public (&st
) == MATCH_YES
)
250 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
257 match ("save", gfc_match_save
, ST_ATTR_DECL
);
261 match ("target", gfc_match_target
, ST_ATTR_DECL
);
262 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
269 match ("value", gfc_match_value
, ST_ATTR_DECL
);
270 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
277 /* This is not a specification statement. See if any of the matchers
278 has stored an error message of some sort. */
282 gfc_buffer_error (0);
283 gfc_current_locus
= old_locus
;
285 return ST_GET_FCN_CHARACTERISTICS
;
289 /* This is the primary 'decode_statement'. */
291 decode_statement (void)
299 gfc_enforce_clean_symbol_state ();
301 gfc_clear_error (); /* Clear any pending errors. */
302 gfc_clear_warning (); /* Clear any pending warnings. */
304 gfc_matching_function
= false;
306 if (gfc_match_eos () == MATCH_YES
)
309 if (gfc_current_state () == COMP_FUNCTION
310 && gfc_current_block ()->result
->ts
.kind
== -1)
311 return decode_specification_statement ();
313 old_locus
= gfc_current_locus
;
315 c
= gfc_peek_ascii_char ();
319 if (match_word ("use", gfc_match_use
, &old_locus
) == MATCH_YES
)
321 last_was_use_stmt
= true;
325 undo_new_statement ();
328 if (last_was_use_stmt
)
331 /* Try matching a data declaration or function declaration. The
332 input "REALFUNCTIONA(N)" can mean several things in different
333 contexts, so it (and its relatives) get special treatment. */
335 if (gfc_current_state () == COMP_NONE
336 || gfc_current_state () == COMP_INTERFACE
337 || gfc_current_state () == COMP_CONTAINS
)
339 gfc_matching_function
= true;
340 m
= gfc_match_function_decl ();
343 else if (m
== MATCH_ERROR
)
347 gfc_current_locus
= old_locus
;
349 gfc_matching_function
= false;
352 /* Match statements whose error messages are meant to be overwritten
353 by something better. */
355 match (NULL
, gfc_match_assignment
, ST_ASSIGNMENT
);
356 match (NULL
, gfc_match_pointer_assignment
, ST_POINTER_ASSIGNMENT
);
357 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
359 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
360 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
362 /* Try to match a subroutine statement, which has the same optional
363 prefixes that functions can have. */
365 if (gfc_match_subroutine () == MATCH_YES
)
366 return ST_SUBROUTINE
;
368 gfc_current_locus
= old_locus
;
370 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
371 statements, which might begin with a block label. The match functions for
372 these statements are unusual in that their keyword is not seen before
373 the matcher is called. */
375 if (gfc_match_if (&st
) == MATCH_YES
)
378 gfc_current_locus
= old_locus
;
380 if (gfc_match_where (&st
) == MATCH_YES
)
383 gfc_current_locus
= old_locus
;
385 if (gfc_match_forall (&st
) == MATCH_YES
)
388 gfc_current_locus
= old_locus
;
390 match (NULL
, gfc_match_do
, ST_DO
);
391 match (NULL
, gfc_match_block
, ST_BLOCK
);
392 match (NULL
, gfc_match_associate
, ST_ASSOCIATE
);
393 match (NULL
, gfc_match_critical
, ST_CRITICAL
);
394 match (NULL
, gfc_match_select
, ST_SELECT_CASE
);
396 gfc_current_ns
= gfc_build_block_ns (gfc_current_ns
);
397 match (NULL
, gfc_match_select_type
, ST_SELECT_TYPE
);
399 gfc_current_ns
= gfc_current_ns
->parent
;
400 gfc_free_namespace (ns
);
402 /* General statement matching: Instead of testing every possible
403 statement, we eliminate most possibilities by peeking at the
409 match ("abstract% interface", gfc_match_abstract_interface
,
411 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
);
412 match ("allocatable", gfc_match_allocatable
, ST_ATTR_DECL
);
413 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
);
414 match ("asynchronous", gfc_match_asynchronous
, ST_ATTR_DECL
);
418 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
);
419 match ("block data", gfc_match_block_data
, ST_BLOCK_DATA
);
420 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
424 match ("call", gfc_match_call
, ST_CALL
);
425 match ("close", gfc_match_close
, ST_CLOSE
);
426 match ("continue", gfc_match_continue
, ST_CONTINUE
);
427 match ("contiguous", gfc_match_contiguous
, ST_ATTR_DECL
);
428 match ("cycle", gfc_match_cycle
, ST_CYCLE
);
429 match ("case", gfc_match_case
, ST_CASE
);
430 match ("common", gfc_match_common
, ST_COMMON
);
431 match ("contains", gfc_match_eos
, ST_CONTAINS
);
432 match ("class", gfc_match_class_is
, ST_CLASS_IS
);
433 match ("codimension", gfc_match_codimension
, ST_ATTR_DECL
);
437 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
);
438 match ("data", gfc_match_data
, ST_DATA
);
439 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
443 match ("end file", gfc_match_endfile
, ST_END_FILE
);
444 match ("exit", gfc_match_exit
, ST_EXIT
);
445 match ("else", gfc_match_else
, ST_ELSE
);
446 match ("else where", gfc_match_elsewhere
, ST_ELSEWHERE
);
447 match ("else if", gfc_match_elseif
, ST_ELSEIF
);
448 match ("error stop", gfc_match_error_stop
, ST_ERROR_STOP
);
449 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
451 if (gfc_match_end (&st
) == MATCH_YES
)
454 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
455 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
456 match ("external", gfc_match_external
, ST_ATTR_DECL
);
460 match ("final", gfc_match_final_decl
, ST_FINAL
);
461 match ("flush", gfc_match_flush
, ST_FLUSH
);
462 match ("format", gfc_match_format
, ST_FORMAT
);
466 match ("generic", gfc_match_generic
, ST_GENERIC
);
467 match ("go to", gfc_match_goto
, ST_GOTO
);
471 match ("inquire", gfc_match_inquire
, ST_INQUIRE
);
472 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
473 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
474 match ("import", gfc_match_import
, ST_IMPORT
);
475 match ("interface", gfc_match_interface
, ST_INTERFACE
);
476 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
477 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
481 match ("lock", gfc_match_lock
, ST_LOCK
);
485 match ("module% procedure", gfc_match_modproc
, ST_MODULE_PROC
);
486 match ("module", gfc_match_module
, ST_MODULE
);
490 match ("nullify", gfc_match_nullify
, ST_NULLIFY
);
491 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
495 match ("open", gfc_match_open
, ST_OPEN
);
496 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
500 match ("print", gfc_match_print
, ST_WRITE
);
501 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
502 match ("pause", gfc_match_pause
, ST_PAUSE
);
503 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
504 if (gfc_match_private (&st
) == MATCH_YES
)
506 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
507 match ("program", gfc_match_program
, ST_PROGRAM
);
508 if (gfc_match_public (&st
) == MATCH_YES
)
510 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
514 match ("read", gfc_match_read
, ST_READ
);
515 match ("return", gfc_match_return
, ST_RETURN
);
516 match ("rewind", gfc_match_rewind
, ST_REWIND
);
520 match ("sequence", gfc_match_eos
, ST_SEQUENCE
);
521 match ("stop", gfc_match_stop
, ST_STOP
);
522 match ("save", gfc_match_save
, ST_ATTR_DECL
);
523 match ("sync all", gfc_match_sync_all
, ST_SYNC_ALL
);
524 match ("sync images", gfc_match_sync_images
, ST_SYNC_IMAGES
);
525 match ("sync memory", gfc_match_sync_memory
, ST_SYNC_MEMORY
);
529 match ("target", gfc_match_target
, ST_ATTR_DECL
);
530 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
531 match ("type is", gfc_match_type_is
, ST_TYPE_IS
);
535 match ("unlock", gfc_match_unlock
, ST_UNLOCK
);
539 match ("value", gfc_match_value
, ST_ATTR_DECL
);
540 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
544 match ("wait", gfc_match_wait
, ST_WAIT
);
545 match ("write", gfc_match_write
, ST_WRITE
);
549 /* All else has failed, so give up. See if any of the matchers has
550 stored an error message of some sort. */
552 if (gfc_error_check () == 0)
553 gfc_error_now_2 ("Unclassifiable statement at %C");
557 gfc_error_recovery ();
562 /* Like match, but set a flag simd_matched if keyword matched. */
563 #define matchs(keyword, subr, st) \
565 if (match_word_omp_simd (keyword, subr, &old_locus, \
566 &simd_matched) == MATCH_YES) \
569 undo_new_statement (); \
572 /* Like match, but don't match anything if not -fopenmp. */
573 #define matcho(keyword, subr, st) \
575 if (!gfc_option.gfc_flag_openmp) \
577 else if (match_word (keyword, subr, &old_locus) \
581 undo_new_statement (); \
585 decode_omp_directive (void)
589 bool simd_matched
= false;
591 gfc_enforce_clean_symbol_state ();
593 gfc_clear_error (); /* Clear any pending errors. */
594 gfc_clear_warning (); /* Clear any pending warnings. */
598 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
599 "or ELEMENTAL procedures");
600 gfc_error_recovery ();
604 gfc_unset_implicit_pure (NULL
);
606 old_locus
= gfc_current_locus
;
608 /* General OpenMP directive matching: Instead of testing every possible
609 statement, we eliminate most possibilities by peeking at the
612 c
= gfc_peek_ascii_char ();
614 /* match is for directives that should be recognized only if
615 -fopenmp, matchs for directives that should be recognized
616 if either -fopenmp or -fopenmp-simd. */
620 matcho ("atomic", gfc_match_omp_atomic
, ST_OMP_ATOMIC
);
623 matcho ("barrier", gfc_match_omp_barrier
, ST_OMP_BARRIER
);
626 matcho ("cancellation% point", gfc_match_omp_cancellation_point
,
627 ST_OMP_CANCELLATION_POINT
);
628 matcho ("cancel", gfc_match_omp_cancel
, ST_OMP_CANCEL
);
629 matcho ("critical", gfc_match_omp_critical
, ST_OMP_CRITICAL
);
632 matchs ("declare reduction", gfc_match_omp_declare_reduction
,
633 ST_OMP_DECLARE_REDUCTION
);
634 matchs ("declare simd", gfc_match_omp_declare_simd
,
635 ST_OMP_DECLARE_SIMD
);
636 matcho ("declare target", gfc_match_omp_declare_target
,
637 ST_OMP_DECLARE_TARGET
);
638 matchs ("distribute parallel do simd",
639 gfc_match_omp_distribute_parallel_do_simd
,
640 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
);
641 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do
,
642 ST_OMP_DISTRIBUTE_PARALLEL_DO
);
643 matchs ("distribute simd", gfc_match_omp_distribute_simd
,
644 ST_OMP_DISTRIBUTE_SIMD
);
645 matcho ("distribute", gfc_match_omp_distribute
, ST_OMP_DISTRIBUTE
);
646 matchs ("do simd", gfc_match_omp_do_simd
, ST_OMP_DO_SIMD
);
647 matcho ("do", gfc_match_omp_do
, ST_OMP_DO
);
650 matcho ("end atomic", gfc_match_omp_eos
, ST_OMP_END_ATOMIC
);
651 matcho ("end critical", gfc_match_omp_critical
, ST_OMP_END_CRITICAL
);
652 matchs ("end distribute parallel do simd", gfc_match_omp_eos
,
653 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD
);
654 matcho ("end distribute parallel do", gfc_match_omp_eos
,
655 ST_OMP_END_DISTRIBUTE_PARALLEL_DO
);
656 matchs ("end distribute simd", gfc_match_omp_eos
,
657 ST_OMP_END_DISTRIBUTE_SIMD
);
658 matcho ("end distribute", gfc_match_omp_eos
, ST_OMP_END_DISTRIBUTE
);
659 matchs ("end do simd", gfc_match_omp_end_nowait
, ST_OMP_END_DO_SIMD
);
660 matcho ("end do", gfc_match_omp_end_nowait
, ST_OMP_END_DO
);
661 matchs ("end simd", gfc_match_omp_eos
, ST_OMP_END_SIMD
);
662 matcho ("end master", gfc_match_omp_eos
, ST_OMP_END_MASTER
);
663 matcho ("end ordered", gfc_match_omp_eos
, ST_OMP_END_ORDERED
);
664 matchs ("end parallel do simd", gfc_match_omp_eos
,
665 ST_OMP_END_PARALLEL_DO_SIMD
);
666 matcho ("end parallel do", gfc_match_omp_eos
, ST_OMP_END_PARALLEL_DO
);
667 matcho ("end parallel sections", gfc_match_omp_eos
,
668 ST_OMP_END_PARALLEL_SECTIONS
);
669 matcho ("end parallel workshare", gfc_match_omp_eos
,
670 ST_OMP_END_PARALLEL_WORKSHARE
);
671 matcho ("end parallel", gfc_match_omp_eos
, ST_OMP_END_PARALLEL
);
672 matcho ("end sections", gfc_match_omp_end_nowait
, ST_OMP_END_SECTIONS
);
673 matcho ("end single", gfc_match_omp_end_single
, ST_OMP_END_SINGLE
);
674 matcho ("end target data", gfc_match_omp_eos
, ST_OMP_END_TARGET_DATA
);
675 matchs ("end target teams distribute parallel do simd",
677 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
);
678 matcho ("end target teams distribute parallel do", gfc_match_omp_eos
,
679 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
);
680 matchs ("end target teams distribute simd", gfc_match_omp_eos
,
681 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD
);
682 matcho ("end target teams distribute", gfc_match_omp_eos
,
683 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE
);
684 matcho ("end target teams", gfc_match_omp_eos
, ST_OMP_END_TARGET_TEAMS
);
685 matcho ("end target", gfc_match_omp_eos
, ST_OMP_END_TARGET
);
686 matcho ("end taskgroup", gfc_match_omp_eos
, ST_OMP_END_TASKGROUP
);
687 matcho ("end task", gfc_match_omp_eos
, ST_OMP_END_TASK
);
688 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos
,
689 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
);
690 matcho ("end teams distribute parallel do", gfc_match_omp_eos
,
691 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO
);
692 matchs ("end teams distribute simd", gfc_match_omp_eos
,
693 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD
);
694 matcho ("end teams distribute", gfc_match_omp_eos
,
695 ST_OMP_END_TEAMS_DISTRIBUTE
);
696 matcho ("end teams", gfc_match_omp_eos
, ST_OMP_END_TEAMS
);
697 matcho ("end workshare", gfc_match_omp_end_nowait
,
698 ST_OMP_END_WORKSHARE
);
701 matcho ("flush", gfc_match_omp_flush
, ST_OMP_FLUSH
);
704 matcho ("master", gfc_match_omp_master
, ST_OMP_MASTER
);
707 matcho ("ordered", gfc_match_omp_ordered
, ST_OMP_ORDERED
);
710 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd
,
711 ST_OMP_PARALLEL_DO_SIMD
);
712 matcho ("parallel do", gfc_match_omp_parallel_do
, ST_OMP_PARALLEL_DO
);
713 matcho ("parallel sections", gfc_match_omp_parallel_sections
,
714 ST_OMP_PARALLEL_SECTIONS
);
715 matcho ("parallel workshare", gfc_match_omp_parallel_workshare
,
716 ST_OMP_PARALLEL_WORKSHARE
);
717 matcho ("parallel", gfc_match_omp_parallel
, ST_OMP_PARALLEL
);
720 matcho ("sections", gfc_match_omp_sections
, ST_OMP_SECTIONS
);
721 matcho ("section", gfc_match_omp_eos
, ST_OMP_SECTION
);
722 matchs ("simd", gfc_match_omp_simd
, ST_OMP_SIMD
);
723 matcho ("single", gfc_match_omp_single
, ST_OMP_SINGLE
);
726 matcho ("target data", gfc_match_omp_target_data
, ST_OMP_TARGET_DATA
);
727 matchs ("target teams distribute parallel do simd",
728 gfc_match_omp_target_teams_distribute_parallel_do_simd
,
729 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
);
730 matcho ("target teams distribute parallel do",
731 gfc_match_omp_target_teams_distribute_parallel_do
,
732 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
);
733 matchs ("target teams distribute simd",
734 gfc_match_omp_target_teams_distribute_simd
,
735 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
);
736 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute
,
737 ST_OMP_TARGET_TEAMS_DISTRIBUTE
);
738 matcho ("target teams", gfc_match_omp_target_teams
, ST_OMP_TARGET_TEAMS
);
739 matcho ("target update", gfc_match_omp_target_update
,
740 ST_OMP_TARGET_UPDATE
);
741 matcho ("target", gfc_match_omp_target
, ST_OMP_TARGET
);
742 matcho ("taskgroup", gfc_match_omp_taskgroup
, ST_OMP_TASKGROUP
);
743 matcho ("taskwait", gfc_match_omp_taskwait
, ST_OMP_TASKWAIT
);
744 matcho ("taskyield", gfc_match_omp_taskyield
, ST_OMP_TASKYIELD
);
745 matcho ("task", gfc_match_omp_task
, ST_OMP_TASK
);
746 matchs ("teams distribute parallel do simd",
747 gfc_match_omp_teams_distribute_parallel_do_simd
,
748 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
);
749 matcho ("teams distribute parallel do",
750 gfc_match_omp_teams_distribute_parallel_do
,
751 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
);
752 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd
,
753 ST_OMP_TEAMS_DISTRIBUTE_SIMD
);
754 matcho ("teams distribute", gfc_match_omp_teams_distribute
,
755 ST_OMP_TEAMS_DISTRIBUTE
);
756 matcho ("teams", gfc_match_omp_teams
, ST_OMP_TEAMS
);
757 matcho ("threadprivate", gfc_match_omp_threadprivate
,
758 ST_OMP_THREADPRIVATE
);
761 matcho ("workshare", gfc_match_omp_workshare
, ST_OMP_WORKSHARE
);
765 /* All else has failed, so give up. See if any of the matchers has
766 stored an error message of some sort. Don't error out if
767 not -fopenmp and simd_matched is false, i.e. if a directive other
768 than one marked with match has been seen. */
770 if (gfc_option
.gfc_flag_openmp
|| simd_matched
)
772 if (gfc_error_check () == 0)
773 gfc_error_now ("Unclassifiable OpenMP directive at %C");
778 gfc_error_recovery ();
784 decode_gcc_attribute (void)
788 gfc_enforce_clean_symbol_state ();
790 gfc_clear_error (); /* Clear any pending errors. */
791 gfc_clear_warning (); /* Clear any pending warnings. */
792 old_locus
= gfc_current_locus
;
794 match ("attributes", gfc_match_gcc_attributes
, ST_ATTR_DECL
);
796 /* All else has failed, so give up. See if any of the matchers has
797 stored an error message of some sort. */
799 if (gfc_error_check () == 0)
800 gfc_error_now_2 ("Unclassifiable GCC directive at %C");
804 gfc_error_recovery ();
812 /* Get the next statement in free form source. */
821 at_bol
= gfc_at_bol ();
822 gfc_gobble_whitespace ();
824 c
= gfc_peek_ascii_char ();
830 /* Found a statement label? */
831 m
= gfc_match_st_label (&gfc_statement_label
);
833 d
= gfc_peek_ascii_char ();
834 if (m
!= MATCH_YES
|| !gfc_is_whitespace (d
))
836 gfc_match_small_literal_int (&i
, &cnt
);
839 gfc_error_now_2 ("Too many digits in statement label at %C");
842 gfc_error_now_2 ("Zero is not a valid statement label at %C");
845 c
= gfc_next_ascii_char ();
848 if (!gfc_is_whitespace (c
))
849 gfc_error_now_2 ("Non-numeric character in statement label at %C");
855 label_locus
= gfc_current_locus
;
857 gfc_gobble_whitespace ();
859 if (at_bol
&& gfc_peek_ascii_char () == ';')
861 gfc_error_now_2 ("Semicolon at %C needs to be preceded by "
863 gfc_next_ascii_char (); /* Eat up the semicolon. */
867 if (gfc_match_eos () == MATCH_YES
)
869 gfc_warning_now ("Ignoring statement label in empty statement "
870 "at %L", &label_locus
);
871 gfc_free_st_label (gfc_statement_label
);
872 gfc_statement_label
= NULL
;
879 /* Comments have already been skipped by the time we get here,
880 except for GCC attributes and OpenMP directives. */
882 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
883 c
= gfc_peek_ascii_char ();
889 c
= gfc_next_ascii_char ();
890 for (i
= 0; i
< 4; i
++, c
= gfc_next_ascii_char ())
891 gcc_assert (c
== "gcc$"[i
]);
893 gfc_gobble_whitespace ();
894 return decode_gcc_attribute ();
898 && (gfc_option
.gfc_flag_openmp
899 || gfc_option
.gfc_flag_openmp_simd
))
903 c
= gfc_next_ascii_char ();
904 for (i
= 0; i
< 4; i
++, c
= gfc_next_ascii_char ())
905 gcc_assert (c
== "$omp"[i
]);
907 gcc_assert (c
== ' ' || c
== '\t');
908 gfc_gobble_whitespace ();
909 if (last_was_use_stmt
)
911 return decode_omp_directive ();
917 if (at_bol
&& c
== ';')
919 if (!(gfc_option
.allow_std
& GFC_STD_F2008
))
920 gfc_error_now_2 ("Fortran 2008: Semicolon at %C without preceding "
922 gfc_next_ascii_char (); /* Eat up the semicolon. */
926 return decode_statement ();
930 /* Get the next statement in fixed-form source. */
935 int label
, digit_flag
, i
;
940 return decode_statement ();
942 /* Skip past the current label field, parsing a statement label if
943 one is there. This is a weird number parser, since the number is
944 contained within five columns and can have any kind of embedded
945 spaces. We also check for characters that make the rest of the
951 for (i
= 0; i
< 5; i
++)
953 c
= gfc_next_char_literal (NONSTRING
);
970 label
= label
* 10 + ((unsigned char) c
- '0');
971 label_locus
= gfc_current_locus
;
975 /* Comments have already been skipped by the time we get
976 here, except for GCC attributes and OpenMP directives. */
979 c
= gfc_next_char_literal (NONSTRING
);
981 if (TOLOWER (c
) == 'g')
983 for (i
= 0; i
< 4; i
++, c
= gfc_next_char_literal (NONSTRING
))
984 gcc_assert (TOLOWER (c
) == "gcc$"[i
]);
986 return decode_gcc_attribute ();
989 && (gfc_option
.gfc_flag_openmp
990 || gfc_option
.gfc_flag_openmp_simd
))
992 for (i
= 0; i
< 4; i
++, c
= gfc_next_char_literal (NONSTRING
))
993 gcc_assert ((char) gfc_wide_tolower (c
) == "$omp"[i
]);
995 if (c
!= ' ' && c
!= '0')
997 gfc_buffer_error (0);
998 gfc_error ("Bad continuation line at %C");
1001 if (last_was_use_stmt
)
1003 return decode_omp_directive ();
1007 /* Comments have already been skipped by the time we get
1008 here so don't bother checking for them. */
1011 gfc_buffer_error (0);
1012 gfc_error ("Non-numeric character in statement label at %C");
1020 gfc_warning_now_2 ("Zero is not a valid statement label at %C");
1023 /* We've found a valid statement label. */
1024 gfc_statement_label
= gfc_get_st_label (label
);
1028 /* Since this line starts a statement, it cannot be a continuation
1029 of a previous statement. If we see something here besides a
1030 space or zero, it must be a bad continuation line. */
1032 c
= gfc_next_char_literal (NONSTRING
);
1036 if (c
!= ' ' && c
!= '0')
1038 gfc_buffer_error (0);
1039 gfc_error ("Bad continuation line at %C");
1043 /* Now that we've taken care of the statement label columns, we have
1044 to make sure that the first nonblank character is not a '!'. If
1045 it is, the rest of the line is a comment. */
1049 loc
= gfc_current_locus
;
1050 c
= gfc_next_char_literal (NONSTRING
);
1052 while (gfc_is_whitespace (c
));
1056 gfc_current_locus
= loc
;
1061 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1062 else if (!(gfc_option
.allow_std
& GFC_STD_F2008
))
1063 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1068 if (gfc_match_eos () == MATCH_YES
)
1071 /* At this point, we've got a nonblank statement to parse. */
1072 return decode_statement ();
1076 gfc_warning_now ("Ignoring statement label in empty statement at %L",
1079 gfc_current_locus
.lb
->truncated
= 0;
1080 gfc_advance_line ();
1085 /* Return the next non-ST_NONE statement to the caller. We also worry
1086 about including files and the ends of include files at this stage. */
1088 static gfc_statement
1089 next_statement (void)
1094 gfc_enforce_clean_symbol_state ();
1096 gfc_new_block
= NULL
;
1098 gfc_current_ns
->old_cl_list
= gfc_current_ns
->cl_list
;
1099 gfc_current_ns
->old_equiv
= gfc_current_ns
->equiv
;
1102 gfc_statement_label
= NULL
;
1103 gfc_buffer_error (1);
1106 gfc_advance_line ();
1108 gfc_skip_comments ();
1116 if (gfc_define_undef_line ())
1119 old_locus
= gfc_current_locus
;
1121 st
= (gfc_current_form
== FORM_FIXED
) ? next_fixed () : next_free ();
1127 gfc_buffer_error (0);
1129 if (st
== ST_GET_FCN_CHARACTERISTICS
&& gfc_statement_label
!= NULL
)
1131 gfc_free_st_label (gfc_statement_label
);
1132 gfc_statement_label
= NULL
;
1133 gfc_current_locus
= old_locus
;
1137 check_statement_label (st
);
1143 /****************************** Parser ***********************************/
1145 /* The parser subroutines are of type 'try' that fail if the file ends
1148 /* Macros that expand to case-labels for various classes of
1149 statements. Start with executable statements that directly do
1152 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1153 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1154 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1155 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1156 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1157 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1158 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1159 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1160 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1161 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1162 case ST_OMP_TARGET_UPDATE: case ST_ERROR_STOP: case ST_SYNC_ALL: \
1163 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK
1165 /* Statements that mark other executable statements. */
1167 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1168 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1169 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1170 case ST_OMP_PARALLEL: \
1171 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1172 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1173 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1174 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1175 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1176 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1177 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1178 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1179 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1180 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1181 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1182 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1183 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1184 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1185 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1186 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1187 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: \
1190 /* Declaration statements */
1192 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1193 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1194 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
1195 case ST_PROCEDURE: case ST_OMP_DECLARE_SIMD: case ST_OMP_DECLARE_REDUCTION: \
1196 case ST_OMP_DECLARE_TARGET
1198 /* Block end statements. Errors associated with interchanging these
1199 are detected in gfc_match_end(). */
1201 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1202 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1203 case ST_END_BLOCK: case ST_END_ASSOCIATE
1206 /* Push a new state onto the stack. */
1209 push_state (gfc_state_data
*p
, gfc_compile_state new_state
, gfc_symbol
*sym
)
1211 p
->state
= new_state
;
1212 p
->previous
= gfc_state_stack
;
1214 p
->head
= p
->tail
= NULL
;
1215 p
->do_variable
= NULL
;
1217 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1218 construct statement was accepted right before pushing the state. Thus,
1219 the construct's gfc_code is available as tail of the parent state. */
1220 gcc_assert (gfc_state_stack
);
1221 p
->construct
= gfc_state_stack
->tail
;
1223 gfc_state_stack
= p
;
1227 /* Pop the current state. */
1231 gfc_state_stack
= gfc_state_stack
->previous
;
1235 /* Try to find the given state in the state stack. */
1238 gfc_find_state (gfc_compile_state state
)
1242 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1243 if (p
->state
== state
)
1246 return (p
== NULL
) ? false : true;
1250 /* Starts a new level in the statement list. */
1253 new_level (gfc_code
*q
)
1257 p
= q
->block
= gfc_get_code (EXEC_NOP
);
1259 gfc_state_stack
->head
= gfc_state_stack
->tail
= p
;
1265 /* Add the current new_st code structure and adds it to the current
1266 program unit. As a side-effect, it zeroes the new_st. */
1269 add_statement (void)
1273 p
= XCNEW (gfc_code
);
1276 p
->loc
= gfc_current_locus
;
1278 if (gfc_state_stack
->head
== NULL
)
1279 gfc_state_stack
->head
= p
;
1281 gfc_state_stack
->tail
->next
= p
;
1283 while (p
->next
!= NULL
)
1286 gfc_state_stack
->tail
= p
;
1288 gfc_clear_new_st ();
1294 /* Frees everything associated with the current statement. */
1297 undo_new_statement (void)
1299 gfc_free_statements (new_st
.block
);
1300 gfc_free_statements (new_st
.next
);
1301 gfc_free_statement (&new_st
);
1302 gfc_clear_new_st ();
1306 /* If the current statement has a statement label, make sure that it
1307 is allowed to, or should have one. */
1310 check_statement_label (gfc_statement st
)
1314 if (gfc_statement_label
== NULL
)
1316 if (st
== ST_FORMAT
)
1317 gfc_error ("FORMAT statement at %L does not have a statement label",
1324 case ST_END_PROGRAM
:
1325 case ST_END_FUNCTION
:
1326 case ST_END_SUBROUTINE
:
1330 case ST_END_CRITICAL
:
1332 case ST_END_ASSOCIATE
:
1335 if (st
== ST_ENDDO
|| st
== ST_CONTINUE
)
1336 type
= ST_LABEL_DO_TARGET
;
1338 type
= ST_LABEL_TARGET
;
1342 type
= ST_LABEL_FORMAT
;
1345 /* Statement labels are not restricted from appearing on a
1346 particular line. However, there are plenty of situations
1347 where the resulting label can't be referenced. */
1350 type
= ST_LABEL_BAD_TARGET
;
1354 gfc_define_st_label (gfc_statement_label
, type
, &label_locus
);
1356 new_st
.here
= gfc_statement_label
;
1360 /* Figures out what the enclosing program unit is. This will be a
1361 function, subroutine, program, block data or module. */
1364 gfc_enclosing_unit (gfc_compile_state
* result
)
1368 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1369 if (p
->state
== COMP_FUNCTION
|| p
->state
== COMP_SUBROUTINE
1370 || p
->state
== COMP_MODULE
|| p
->state
== COMP_BLOCK_DATA
1371 || p
->state
== COMP_PROGRAM
)
1380 *result
= COMP_PROGRAM
;
1385 /* Translate a statement enum to a string. */
1388 gfc_ascii_statement (gfc_statement st
)
1394 case ST_ARITHMETIC_IF
:
1395 p
= _("arithmetic IF");
1404 p
= _("attribute declaration");
1440 p
= _("data declaration");
1448 case ST_DERIVED_DECL
:
1449 p
= _("derived type declaration");
1463 case ST_END_ASSOCIATE
:
1464 p
= "END ASSOCIATE";
1469 case ST_END_BLOCK_DATA
:
1470 p
= "END BLOCK DATA";
1472 case ST_END_CRITICAL
:
1484 case ST_END_FUNCTION
:
1490 case ST_END_INTERFACE
:
1491 p
= "END INTERFACE";
1496 case ST_END_PROGRAM
:
1502 case ST_END_SUBROUTINE
:
1503 p
= "END SUBROUTINE";
1514 case ST_EQUIVALENCE
:
1526 case ST_FORALL_BLOCK
: /* Fall through */
1548 case ST_IMPLICIT_NONE
:
1549 p
= "IMPLICIT NONE";
1551 case ST_IMPLIED_ENDDO
:
1552 p
= _("implied END DO");
1581 case ST_MODULE_PROC
:
1582 p
= "MODULE PROCEDURE";
1614 case ST_SYNC_IMAGES
:
1617 case ST_SYNC_MEMORY
:
1632 case ST_WHERE_BLOCK
: /* Fall through */
1643 p
= _("assignment");
1645 case ST_POINTER_ASSIGNMENT
:
1646 p
= _("pointer assignment");
1648 case ST_SELECT_CASE
:
1651 case ST_SELECT_TYPE
:
1666 case ST_STATEMENT_FUNCTION
:
1667 p
= "STATEMENT FUNCTION";
1669 case ST_LABEL_ASSIGNMENT
:
1670 p
= "LABEL ASSIGNMENT";
1673 p
= "ENUM DEFINITION";
1676 p
= "ENUMERATOR DEFINITION";
1684 case ST_OMP_BARRIER
:
1685 p
= "!$OMP BARRIER";
1690 case ST_OMP_CANCELLATION_POINT
:
1691 p
= "!$OMP CANCELLATION POINT";
1693 case ST_OMP_CRITICAL
:
1694 p
= "!$OMP CRITICAL";
1696 case ST_OMP_DECLARE_REDUCTION
:
1697 p
= "!$OMP DECLARE REDUCTION";
1699 case ST_OMP_DECLARE_SIMD
:
1700 p
= "!$OMP DECLARE SIMD";
1702 case ST_OMP_DECLARE_TARGET
:
1703 p
= "!$OMP DECLARE TARGET";
1705 case ST_OMP_DISTRIBUTE
:
1706 p
= "!$OMP DISTRIBUTE";
1708 case ST_OMP_DISTRIBUTE_PARALLEL_DO
:
1709 p
= "!$OMP DISTRIBUTE PARALLEL DO";
1711 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
1712 p
= "!$OMP DISTRIBUTE PARALLEL DO SIMD";
1714 case ST_OMP_DISTRIBUTE_SIMD
:
1715 p
= "!$OMP DISTRIBUTE SIMD";
1720 case ST_OMP_DO_SIMD
:
1721 p
= "!$OMP DO SIMD";
1723 case ST_OMP_END_ATOMIC
:
1724 p
= "!$OMP END ATOMIC";
1726 case ST_OMP_END_CRITICAL
:
1727 p
= "!$OMP END CRITICAL";
1729 case ST_OMP_END_DISTRIBUTE
:
1730 p
= "!$OMP END DISTRIBUTE";
1732 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO
:
1733 p
= "!$OMP END DISTRIBUTE PARALLEL DO";
1735 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD
:
1736 p
= "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
1738 case ST_OMP_END_DISTRIBUTE_SIMD
:
1739 p
= "!$OMP END DISTRIBUTE SIMD";
1744 case ST_OMP_END_DO_SIMD
:
1745 p
= "!$OMP END DO SIMD";
1747 case ST_OMP_END_SIMD
:
1748 p
= "!$OMP END SIMD";
1750 case ST_OMP_END_MASTER
:
1751 p
= "!$OMP END MASTER";
1753 case ST_OMP_END_ORDERED
:
1754 p
= "!$OMP END ORDERED";
1756 case ST_OMP_END_PARALLEL
:
1757 p
= "!$OMP END PARALLEL";
1759 case ST_OMP_END_PARALLEL_DO
:
1760 p
= "!$OMP END PARALLEL DO";
1762 case ST_OMP_END_PARALLEL_DO_SIMD
:
1763 p
= "!$OMP END PARALLEL DO SIMD";
1765 case ST_OMP_END_PARALLEL_SECTIONS
:
1766 p
= "!$OMP END PARALLEL SECTIONS";
1768 case ST_OMP_END_PARALLEL_WORKSHARE
:
1769 p
= "!$OMP END PARALLEL WORKSHARE";
1771 case ST_OMP_END_SECTIONS
:
1772 p
= "!$OMP END SECTIONS";
1774 case ST_OMP_END_SINGLE
:
1775 p
= "!$OMP END SINGLE";
1777 case ST_OMP_END_TASK
:
1778 p
= "!$OMP END TASK";
1780 case ST_OMP_END_TARGET
:
1781 p
= "!$OMP END TARGET";
1783 case ST_OMP_END_TARGET_DATA
:
1784 p
= "!$OMP END TARGET DATA";
1786 case ST_OMP_END_TARGET_TEAMS
:
1787 p
= "!$OMP END TARGET TEAMS";
1789 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE
:
1790 p
= "!$OMP END TARGET TEAMS DISTRIBUTE";
1792 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
1793 p
= "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
1795 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
1796 p
= "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
1798 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD
:
1799 p
= "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
1801 case ST_OMP_END_TASKGROUP
:
1802 p
= "!$OMP END TASKGROUP";
1804 case ST_OMP_END_TEAMS
:
1805 p
= "!$OMP END TEAMS";
1807 case ST_OMP_END_TEAMS_DISTRIBUTE
:
1808 p
= "!$OMP END TEAMS DISTRIBUTE";
1810 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO
:
1811 p
= "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
1813 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
1814 p
= "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
1816 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD
:
1817 p
= "!$OMP END TEAMS DISTRIBUTE SIMD";
1819 case ST_OMP_END_WORKSHARE
:
1820 p
= "!$OMP END WORKSHARE";
1828 case ST_OMP_ORDERED
:
1829 p
= "!$OMP ORDERED";
1831 case ST_OMP_PARALLEL
:
1832 p
= "!$OMP PARALLEL";
1834 case ST_OMP_PARALLEL_DO
:
1835 p
= "!$OMP PARALLEL DO";
1837 case ST_OMP_PARALLEL_DO_SIMD
:
1838 p
= "!$OMP PARALLEL DO SIMD";
1840 case ST_OMP_PARALLEL_SECTIONS
:
1841 p
= "!$OMP PARALLEL SECTIONS";
1843 case ST_OMP_PARALLEL_WORKSHARE
:
1844 p
= "!$OMP PARALLEL WORKSHARE";
1846 case ST_OMP_SECTIONS
:
1847 p
= "!$OMP SECTIONS";
1849 case ST_OMP_SECTION
:
1850 p
= "!$OMP SECTION";
1861 case ST_OMP_TARGET_DATA
:
1862 p
= "!$OMP TARGET DATA";
1864 case ST_OMP_TARGET_TEAMS
:
1865 p
= "!$OMP TARGET TEAMS";
1867 case ST_OMP_TARGET_TEAMS_DISTRIBUTE
:
1868 p
= "!$OMP TARGET TEAMS DISTRIBUTE";
1870 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
1871 p
= "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
1873 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
1874 p
= "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
1876 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
1877 p
= "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
1879 case ST_OMP_TARGET_UPDATE
:
1880 p
= "!$OMP TARGET UPDATE";
1885 case ST_OMP_TASKGROUP
:
1886 p
= "!$OMP TASKGROUP";
1888 case ST_OMP_TASKWAIT
:
1889 p
= "!$OMP TASKWAIT";
1891 case ST_OMP_TASKYIELD
:
1892 p
= "!$OMP TASKYIELD";
1897 case ST_OMP_TEAMS_DISTRIBUTE
:
1898 p
= "!$OMP TEAMS DISTRIBUTE";
1900 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
1901 p
= "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
1903 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
1904 p
= "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
1906 case ST_OMP_TEAMS_DISTRIBUTE_SIMD
:
1907 p
= "!$OMP TEAMS DISTRIBUTE SIMD";
1909 case ST_OMP_THREADPRIVATE
:
1910 p
= "!$OMP THREADPRIVATE";
1912 case ST_OMP_WORKSHARE
:
1913 p
= "!$OMP WORKSHARE";
1916 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1923 /* Create a symbol for the main program and assign it to ns->proc_name. */
1926 main_program_symbol (gfc_namespace
*ns
, const char *name
)
1928 gfc_symbol
*main_program
;
1929 symbol_attribute attr
;
1931 gfc_get_symbol (name
, ns
, &main_program
);
1932 gfc_clear_attr (&attr
);
1933 attr
.flavor
= FL_PROGRAM
;
1934 attr
.proc
= PROC_UNKNOWN
;
1935 attr
.subroutine
= 1;
1936 attr
.access
= ACCESS_PUBLIC
;
1937 attr
.is_main_program
= 1;
1938 main_program
->attr
= attr
;
1939 main_program
->declared_at
= gfc_current_locus
;
1940 ns
->proc_name
= main_program
;
1941 gfc_commit_symbols ();
1945 /* Do whatever is necessary to accept the last statement. */
1948 accept_statement (gfc_statement st
)
1952 case ST_IMPLICIT_NONE
:
1959 gfc_current_ns
->proc_name
= gfc_new_block
;
1962 /* If the statement is the end of a block, lay down a special code
1963 that allows a branch to the end of the block from within the
1964 construct. IF and SELECT are treated differently from DO
1965 (where EXEC_NOP is added inside the loop) for two
1967 1. END DO has a meaning in the sense that after a GOTO to
1968 it, the loop counter must be increased.
1969 2. IF blocks and SELECT blocks can consist of multiple
1970 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
1971 Putting the label before the END IF would make the jump
1972 from, say, the ELSE IF block to the END IF illegal. */
1976 case ST_END_CRITICAL
:
1977 if (gfc_statement_label
!= NULL
)
1979 new_st
.op
= EXEC_END_NESTED_BLOCK
;
1984 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
1985 one parallel block. Thus, we add the special code to the nested block
1986 itself, instead of the parent one. */
1988 case ST_END_ASSOCIATE
:
1989 if (gfc_statement_label
!= NULL
)
1991 new_st
.op
= EXEC_END_BLOCK
;
1996 /* The end-of-program unit statements do not get the special
1997 marker and require a statement of some sort if they are a
2000 case ST_END_PROGRAM
:
2001 case ST_END_FUNCTION
:
2002 case ST_END_SUBROUTINE
:
2003 if (gfc_statement_label
!= NULL
)
2005 new_st
.op
= EXEC_RETURN
;
2010 new_st
.op
= EXEC_END_PROCEDURE
;
2026 gfc_commit_symbols ();
2027 gfc_warning_check ();
2028 gfc_clear_new_st ();
2032 /* Undo anything tentative that has been built for the current
2036 reject_statement (void)
2038 /* Revert to the previous charlen chain. */
2039 gfc_free_charlen (gfc_current_ns
->cl_list
, gfc_current_ns
->old_cl_list
);
2040 gfc_current_ns
->cl_list
= gfc_current_ns
->old_cl_list
;
2042 gfc_free_equiv_until (gfc_current_ns
->equiv
, gfc_current_ns
->old_equiv
);
2043 gfc_current_ns
->equiv
= gfc_current_ns
->old_equiv
;
2045 gfc_new_block
= NULL
;
2046 gfc_undo_symbols ();
2047 gfc_clear_warning ();
2048 undo_new_statement ();
2052 /* Generic complaint about an out of order statement. We also do
2053 whatever is necessary to clean up. */
2056 unexpected_statement (gfc_statement st
)
2058 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st
));
2060 reject_statement ();
2064 /* Given the next statement seen by the matcher, make sure that it is
2065 in proper order with the last. This subroutine is initialized by
2066 calling it with an argument of ST_NONE. If there is a problem, we
2067 issue an error and return false. Otherwise we return true.
2069 Individual parsers need to verify that the statements seen are
2070 valid before calling here, i.e., ENTRY statements are not allowed in
2071 INTERFACE blocks. The following diagram is taken from the standard:
2073 +---------------------------------------+
2074 | program subroutine function module |
2075 +---------------------------------------+
2077 +---------------------------------------+
2079 +---------------------------------------+
2081 | +-----------+------------------+
2082 | | parameter | implicit |
2083 | +-----------+------------------+
2084 | format | | derived type |
2085 | entry | parameter | interface |
2086 | | data | specification |
2087 | | | statement func |
2088 | +-----------+------------------+
2089 | | data | executable |
2090 +--------+-----------+------------------+
2092 +---------------------------------------+
2093 | internal module/subprogram |
2094 +---------------------------------------+
2096 +---------------------------------------+
2105 ORDER_IMPLICIT_NONE
,
2113 enum state_order state
;
2114 gfc_statement last_statement
;
2120 verify_st_order (st_state
*p
, gfc_statement st
, bool silent
)
2126 p
->state
= ORDER_START
;
2130 if (p
->state
> ORDER_USE
)
2132 p
->state
= ORDER_USE
;
2136 if (p
->state
> ORDER_IMPORT
)
2138 p
->state
= ORDER_IMPORT
;
2141 case ST_IMPLICIT_NONE
:
2142 if (p
->state
> ORDER_IMPLICIT
)
2145 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2146 statement disqualifies a USE but not an IMPLICIT NONE.
2147 Duplicate IMPLICIT NONEs are caught when the implicit types
2150 p
->state
= ORDER_IMPLICIT_NONE
;
2154 if (p
->state
> ORDER_IMPLICIT
)
2156 p
->state
= ORDER_IMPLICIT
;
2161 if (p
->state
< ORDER_IMPLICIT_NONE
)
2162 p
->state
= ORDER_IMPLICIT_NONE
;
2166 if (p
->state
>= ORDER_EXEC
)
2168 if (p
->state
< ORDER_IMPLICIT
)
2169 p
->state
= ORDER_IMPLICIT
;
2173 if (p
->state
< ORDER_SPEC
)
2174 p
->state
= ORDER_SPEC
;
2179 case ST_DERIVED_DECL
:
2181 if (p
->state
>= ORDER_EXEC
)
2183 if (p
->state
< ORDER_SPEC
)
2184 p
->state
= ORDER_SPEC
;
2189 if (p
->state
< ORDER_EXEC
)
2190 p
->state
= ORDER_EXEC
;
2194 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
2195 gfc_ascii_statement (st
));
2198 /* All is well, record the statement in case we need it next time. */
2199 p
->where
= gfc_current_locus
;
2200 p
->last_statement
= st
;
2205 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2206 gfc_ascii_statement (st
),
2207 gfc_ascii_statement (p
->last_statement
), &p
->where
);
2213 /* Handle an unexpected end of file. This is a show-stopper... */
2215 static void unexpected_eof (void) ATTRIBUTE_NORETURN
;
2218 unexpected_eof (void)
2222 gfc_error ("Unexpected end of file in '%s'", gfc_source_file
);
2224 /* Memory cleanup. Move to "second to last". */
2225 for (p
= gfc_state_stack
; p
&& p
->previous
&& p
->previous
->previous
;
2228 gfc_current_ns
->code
= (p
&& p
->previous
) ? p
->head
: NULL
;
2231 longjmp (eof_buf
, 1);
2235 /* Parse the CONTAINS section of a derived type definition. */
2237 gfc_access gfc_typebound_default_access
;
2240 parse_derived_contains (void)
2243 bool seen_private
= false;
2244 bool seen_comps
= false;
2245 bool error_flag
= false;
2248 gcc_assert (gfc_current_state () == COMP_DERIVED
);
2249 gcc_assert (gfc_current_block ());
2251 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2253 if (gfc_current_block ()->attr
.sequence
)
2254 gfc_error ("Derived-type '%s' with SEQUENCE must not have a CONTAINS"
2255 " section at %C", gfc_current_block ()->name
);
2256 if (gfc_current_block ()->attr
.is_bind_c
)
2257 gfc_error ("Derived-type '%s' with BIND(C) must not have a CONTAINS"
2258 " section at %C", gfc_current_block ()->name
);
2260 accept_statement (ST_CONTAINS
);
2261 push_state (&s
, COMP_DERIVED_CONTAINS
, NULL
);
2263 gfc_typebound_default_access
= ACCESS_PUBLIC
;
2269 st
= next_statement ();
2277 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2281 if (!gfc_notify_std (GFC_STD_F2003
, "Type-bound procedure at %C"))
2284 accept_statement (ST_PROCEDURE
);
2289 if (!gfc_notify_std (GFC_STD_F2003
, "GENERIC binding at %C"))
2292 accept_statement (ST_GENERIC
);
2297 if (!gfc_notify_std (GFC_STD_F2003
, "FINAL procedure declaration"
2301 accept_statement (ST_FINAL
);
2309 && (!gfc_notify_std(GFC_STD_F2008
, "Derived type definition "
2310 "at %C with empty CONTAINS section")))
2313 /* ST_END_TYPE is accepted by parse_derived after return. */
2317 if (!gfc_find_state (COMP_MODULE
))
2319 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2326 gfc_error ("PRIVATE statement at %C must precede procedure"
2333 gfc_error ("Duplicate PRIVATE statement at %C");
2337 accept_statement (ST_PRIVATE
);
2338 gfc_typebound_default_access
= ACCESS_PRIVATE
;
2339 seen_private
= true;
2343 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2347 gfc_error ("Already inside a CONTAINS block at %C");
2351 unexpected_statement (st
);
2359 reject_statement ();
2363 gcc_assert (gfc_current_state () == COMP_DERIVED
);
2369 /* Parse a derived type. */
2372 parse_derived (void)
2374 int compiling_type
, seen_private
, seen_sequence
, seen_component
;
2378 gfc_component
*c
, *lock_comp
= NULL
;
2380 accept_statement (ST_DERIVED_DECL
);
2381 push_state (&s
, COMP_DERIVED
, gfc_new_block
);
2383 gfc_new_block
->component_access
= ACCESS_PUBLIC
;
2390 while (compiling_type
)
2392 st
= next_statement ();
2400 accept_statement (st
);
2405 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2412 if (!seen_component
)
2413 gfc_notify_std (GFC_STD_F2003
, "Derived type "
2414 "definition at %C without components");
2416 accept_statement (ST_END_TYPE
);
2420 if (!gfc_find_state (COMP_MODULE
))
2422 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2429 gfc_error ("PRIVATE statement at %C must precede "
2430 "structure components");
2435 gfc_error ("Duplicate PRIVATE statement at %C");
2437 s
.sym
->component_access
= ACCESS_PRIVATE
;
2439 accept_statement (ST_PRIVATE
);
2446 gfc_error ("SEQUENCE statement at %C must precede "
2447 "structure components");
2451 if (gfc_current_block ()->attr
.sequence
)
2452 gfc_warning ("SEQUENCE attribute at %C already specified in "
2457 gfc_error ("Duplicate SEQUENCE statement at %C");
2461 gfc_add_sequence (&gfc_current_block ()->attr
,
2462 gfc_current_block ()->name
, NULL
);
2466 gfc_notify_std (GFC_STD_F2003
,
2467 "CONTAINS block in derived type"
2468 " definition at %C");
2470 accept_statement (ST_CONTAINS
);
2471 parse_derived_contains ();
2475 unexpected_statement (st
);
2480 /* need to verify that all fields of the derived type are
2481 * interoperable with C if the type is declared to be bind(c)
2483 sym
= gfc_current_block ();
2484 for (c
= sym
->components
; c
; c
= c
->next
)
2486 bool coarray
, lock_type
, allocatable
, pointer
;
2487 coarray
= lock_type
= allocatable
= pointer
= false;
2489 /* Look for allocatable components. */
2490 if (c
->attr
.allocatable
2491 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2492 && CLASS_DATA (c
)->attr
.allocatable
)
2493 || (c
->ts
.type
== BT_DERIVED
&& !c
->attr
.pointer
2494 && c
->ts
.u
.derived
->attr
.alloc_comp
))
2497 sym
->attr
.alloc_comp
= 1;
2500 /* Look for pointer components. */
2502 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2503 && CLASS_DATA (c
)->attr
.class_pointer
)
2504 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.pointer_comp
))
2507 sym
->attr
.pointer_comp
= 1;
2510 /* Look for procedure pointer components. */
2511 if (c
->attr
.proc_pointer
2512 || (c
->ts
.type
== BT_DERIVED
2513 && c
->ts
.u
.derived
->attr
.proc_pointer_comp
))
2514 sym
->attr
.proc_pointer_comp
= 1;
2516 /* Looking for coarray components. */
2517 if (c
->attr
.codimension
2518 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2519 && CLASS_DATA (c
)->attr
.codimension
))
2522 sym
->attr
.coarray_comp
= 1;
2525 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.coarray_comp
2526 && !c
->attr
.pointer
)
2529 sym
->attr
.coarray_comp
= 1;
2532 /* Looking for lock_type components. */
2533 if ((c
->ts
.type
== BT_DERIVED
2534 && c
->ts
.u
.derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
2535 && c
->ts
.u
.derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
)
2536 || (c
->ts
.type
== BT_CLASS
&& c
->attr
.class_ok
2537 && CLASS_DATA (c
)->ts
.u
.derived
->from_intmod
2538 == INTMOD_ISO_FORTRAN_ENV
2539 && CLASS_DATA (c
)->ts
.u
.derived
->intmod_sym_id
2540 == ISOFORTRAN_LOCK_TYPE
)
2541 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.lock_comp
2542 && !allocatable
&& !pointer
))
2546 sym
->attr
.lock_comp
= 1;
2549 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2550 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2551 unless there are nondirect [allocatable or pointer] components
2552 involved (cf. 1.3.33.1 and 1.3.33.3). */
2554 if (pointer
&& !coarray
&& lock_type
)
2555 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2556 "codimension or be a subcomponent of a coarray, "
2557 "which is not possible as the component has the "
2558 "pointer attribute", c
->name
, &c
->loc
);
2559 else if (pointer
&& !coarray
&& c
->ts
.type
== BT_DERIVED
2560 && c
->ts
.u
.derived
->attr
.lock_comp
)
2561 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2562 "of type LOCK_TYPE, which must have a codimension or be a "
2563 "subcomponent of a coarray", c
->name
, &c
->loc
);
2565 if (lock_type
&& allocatable
&& !coarray
)
2566 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2567 "a codimension", c
->name
, &c
->loc
);
2568 else if (lock_type
&& allocatable
&& c
->ts
.type
== BT_DERIVED
2569 && c
->ts
.u
.derived
->attr
.lock_comp
)
2570 gfc_error ("Allocatable component %s at %L must have a codimension as "
2571 "it has a noncoarray subcomponent of type LOCK_TYPE",
2574 if (sym
->attr
.coarray_comp
&& !coarray
&& lock_type
)
2575 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2576 "subcomponent of type LOCK_TYPE must have a codimension or "
2577 "be a subcomponent of a coarray. (Variables of type %s may "
2578 "not have a codimension as already a coarray "
2579 "subcomponent exists)", c
->name
, &c
->loc
, sym
->name
);
2581 if (sym
->attr
.lock_comp
&& coarray
&& !lock_type
)
2582 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2583 "subcomponent of type LOCK_TYPE must have a codimension or "
2584 "be a subcomponent of a coarray. (Variables of type %s may "
2585 "not have a codimension as %s at %L has a codimension or a "
2586 "coarray subcomponent)", lock_comp
->name
, &lock_comp
->loc
,
2587 sym
->name
, c
->name
, &c
->loc
);
2589 /* Look for private components. */
2590 if (sym
->component_access
== ACCESS_PRIVATE
2591 || c
->attr
.access
== ACCESS_PRIVATE
2592 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.private_comp
))
2593 sym
->attr
.private_comp
= 1;
2596 if (!seen_component
)
2597 sym
->attr
.zero_comp
= 1;
2603 /* Parse an ENUM. */
2611 int seen_enumerator
= 0;
2613 push_state (&s
, COMP_ENUM
, gfc_new_block
);
2617 while (compiling_enum
)
2619 st
= next_statement ();
2627 seen_enumerator
= 1;
2628 accept_statement (st
);
2633 if (!seen_enumerator
)
2634 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2635 accept_statement (st
);
2639 gfc_free_enum_history ();
2640 unexpected_statement (st
);
2648 /* Parse an interface. We must be able to deal with the possibility
2649 of recursive interfaces. The parse_spec() subroutine is mutually
2650 recursive with parse_interface(). */
2652 static gfc_statement
parse_spec (gfc_statement
);
2655 parse_interface (void)
2657 gfc_compile_state new_state
= COMP_NONE
, current_state
;
2658 gfc_symbol
*prog_unit
, *sym
;
2659 gfc_interface_info save
;
2660 gfc_state_data s1
, s2
;
2663 accept_statement (ST_INTERFACE
);
2665 current_interface
.ns
= gfc_current_ns
;
2666 save
= current_interface
;
2668 sym
= (current_interface
.type
== INTERFACE_GENERIC
2669 || current_interface
.type
== INTERFACE_USER_OP
)
2670 ? gfc_new_block
: NULL
;
2672 push_state (&s1
, COMP_INTERFACE
, sym
);
2673 current_state
= COMP_NONE
;
2676 gfc_current_ns
= gfc_get_namespace (current_interface
.ns
, 0);
2678 st
= next_statement ();
2686 if (st
== ST_SUBROUTINE
)
2687 new_state
= COMP_SUBROUTINE
;
2688 else if (st
== ST_FUNCTION
)
2689 new_state
= COMP_FUNCTION
;
2690 if (gfc_new_block
->attr
.pointer
)
2692 gfc_new_block
->attr
.pointer
= 0;
2693 gfc_new_block
->attr
.proc_pointer
= 1;
2695 if (!gfc_add_explicit_interface (gfc_new_block
, IFSRC_IFBODY
,
2696 gfc_new_block
->formal
, NULL
))
2698 reject_statement ();
2699 gfc_free_namespace (gfc_current_ns
);
2705 case ST_MODULE_PROC
: /* The module procedure matcher makes
2706 sure the context is correct. */
2707 accept_statement (st
);
2708 gfc_free_namespace (gfc_current_ns
);
2711 case ST_END_INTERFACE
:
2712 gfc_free_namespace (gfc_current_ns
);
2713 gfc_current_ns
= current_interface
.ns
;
2717 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2718 gfc_ascii_statement (st
));
2719 reject_statement ();
2720 gfc_free_namespace (gfc_current_ns
);
2725 /* Make sure that the generic name has the right attribute. */
2726 if (current_interface
.type
== INTERFACE_GENERIC
2727 && current_state
== COMP_NONE
)
2729 if (new_state
== COMP_FUNCTION
&& sym
)
2730 gfc_add_function (&sym
->attr
, sym
->name
, NULL
);
2731 else if (new_state
== COMP_SUBROUTINE
&& sym
)
2732 gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
);
2734 current_state
= new_state
;
2737 if (current_interface
.type
== INTERFACE_ABSTRACT
)
2739 gfc_add_abstract (&gfc_new_block
->attr
, &gfc_current_locus
);
2740 if (gfc_is_intrinsic_typename (gfc_new_block
->name
))
2741 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
2742 "cannot be the same as an intrinsic type",
2743 gfc_new_block
->name
);
2746 push_state (&s2
, new_state
, gfc_new_block
);
2747 accept_statement (st
);
2748 prog_unit
= gfc_new_block
;
2749 prog_unit
->formal_ns
= gfc_current_ns
;
2750 if (prog_unit
== prog_unit
->formal_ns
->proc_name
2751 && prog_unit
->ns
!= prog_unit
->formal_ns
)
2755 /* Read data declaration statements. */
2756 st
= parse_spec (ST_NONE
);
2758 /* Since the interface block does not permit an IMPLICIT statement,
2759 the default type for the function or the result must be taken
2760 from the formal namespace. */
2761 if (new_state
== COMP_FUNCTION
)
2763 if (prog_unit
->result
== prog_unit
2764 && prog_unit
->ts
.type
== BT_UNKNOWN
)
2765 gfc_set_default_type (prog_unit
, 1, prog_unit
->formal_ns
);
2766 else if (prog_unit
->result
!= prog_unit
2767 && prog_unit
->result
->ts
.type
== BT_UNKNOWN
)
2768 gfc_set_default_type (prog_unit
->result
, 1,
2769 prog_unit
->formal_ns
);
2772 if (st
!= ST_END_SUBROUTINE
&& st
!= ST_END_FUNCTION
)
2774 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2775 gfc_ascii_statement (st
));
2776 reject_statement ();
2780 /* Add EXTERNAL attribute to function or subroutine. */
2781 if (current_interface
.type
!= INTERFACE_ABSTRACT
&& !prog_unit
->attr
.dummy
)
2782 gfc_add_external (&prog_unit
->attr
, &gfc_current_locus
);
2784 current_interface
= save
;
2785 gfc_add_interface (prog_unit
);
2788 if (current_interface
.ns
2789 && current_interface
.ns
->proc_name
2790 && strcmp (current_interface
.ns
->proc_name
->name
,
2791 prog_unit
->name
) == 0)
2792 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2793 "enclosing procedure", prog_unit
->name
,
2794 ¤t_interface
.ns
->proc_name
->declared_at
);
2803 /* Associate function characteristics by going back to the function
2804 declaration and rematching the prefix. */
2807 match_deferred_characteristics (gfc_typespec
* ts
)
2810 match m
= MATCH_ERROR
;
2811 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2813 loc
= gfc_current_locus
;
2815 gfc_current_locus
= gfc_current_block ()->declared_at
;
2818 gfc_buffer_error (1);
2819 m
= gfc_match_prefix (ts
);
2820 gfc_buffer_error (0);
2822 if (ts
->type
== BT_DERIVED
)
2830 /* Only permit one go at the characteristic association. */
2834 /* Set the function locus correctly. If we have not found the
2835 function name, there is an error. */
2837 && gfc_match ("function% %n", name
) == MATCH_YES
2838 && strcmp (name
, gfc_current_block ()->name
) == 0)
2840 gfc_current_block ()->declared_at
= gfc_current_locus
;
2841 gfc_commit_symbols ();
2846 gfc_undo_symbols ();
2849 gfc_current_locus
=loc
;
2854 /* Check specification-expressions in the function result of the currently
2855 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2856 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2857 scope are not yet parsed so this has to be delayed up to parse_spec. */
2860 check_function_result_typed (void)
2862 gfc_typespec
* ts
= &gfc_current_ns
->proc_name
->result
->ts
;
2864 gcc_assert (gfc_current_state () == COMP_FUNCTION
);
2865 gcc_assert (ts
->type
!= BT_UNKNOWN
);
2867 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2868 /* TODO: Extend when KIND type parameters are implemented. */
2869 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
)
2870 gfc_expr_check_typed (ts
->u
.cl
->length
, gfc_current_ns
, true);
2874 /* Parse a set of specification statements. Returns the statement
2875 that doesn't fit. */
2877 static gfc_statement
2878 parse_spec (gfc_statement st
)
2881 bool function_result_typed
= false;
2882 bool bad_characteristic
= false;
2885 verify_st_order (&ss
, ST_NONE
, false);
2887 st
= next_statement ();
2889 /* If we are not inside a function or don't have a result specified so far,
2890 do nothing special about it. */
2891 if (gfc_current_state () != COMP_FUNCTION
)
2892 function_result_typed
= true;
2895 gfc_symbol
* proc
= gfc_current_ns
->proc_name
;
2898 if (proc
->result
->ts
.type
== BT_UNKNOWN
)
2899 function_result_typed
= true;
2904 /* If we're inside a BLOCK construct, some statements are disallowed.
2905 Check this here. Attribute declaration statements like INTENT, OPTIONAL
2906 or VALUE are also disallowed, but they don't have a particular ST_*
2907 key so we have to check for them individually in their matcher routine. */
2908 if (gfc_current_state () == COMP_BLOCK
)
2912 case ST_IMPLICIT_NONE
:
2915 case ST_EQUIVALENCE
:
2916 case ST_STATEMENT_FUNCTION
:
2917 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
2918 gfc_ascii_statement (st
));
2919 reject_statement ();
2925 else if (gfc_current_state () == COMP_BLOCK_DATA
)
2926 /* Fortran 2008, C1116. */
2933 case ST_END_BLOCK_DATA
:
2935 case ST_EQUIVALENCE
:
2938 case ST_IMPLICIT_NONE
:
2939 case ST_DERIVED_DECL
:
2947 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
2948 gfc_ascii_statement (st
));
2949 reject_statement ();
2953 /* If we find a statement that can not be followed by an IMPLICIT statement
2954 (and thus we can expect to see none any further), type the function result
2955 if it has not yet been typed. Be careful not to give the END statement
2956 to verify_st_order! */
2957 if (!function_result_typed
&& st
!= ST_GET_FCN_CHARACTERISTICS
)
2959 bool verify_now
= false;
2961 if (st
== ST_END_FUNCTION
|| st
== ST_CONTAINS
)
2966 verify_st_order (&dummyss
, ST_NONE
, false);
2967 verify_st_order (&dummyss
, st
, false);
2969 if (!verify_st_order (&dummyss
, ST_IMPLICIT
, true))
2975 check_function_result_typed ();
2976 function_result_typed
= true;
2985 case ST_IMPLICIT_NONE
:
2987 if (!function_result_typed
)
2989 check_function_result_typed ();
2990 function_result_typed
= true;
2996 case ST_DATA
: /* Not allowed in interfaces */
2997 if (gfc_current_state () == COMP_INTERFACE
)
3007 case ST_DERIVED_DECL
:
3010 if (!verify_st_order (&ss
, st
, false))
3012 reject_statement ();
3013 st
= next_statement ();
3023 case ST_DERIVED_DECL
:
3029 if (gfc_current_state () != COMP_MODULE
)
3031 gfc_error ("%s statement must appear in a MODULE",
3032 gfc_ascii_statement (st
));
3033 reject_statement ();
3037 if (gfc_current_ns
->default_access
!= ACCESS_UNKNOWN
)
3039 gfc_error ("%s statement at %C follows another accessibility "
3040 "specification", gfc_ascii_statement (st
));
3041 reject_statement ();
3045 gfc_current_ns
->default_access
= (st
== ST_PUBLIC
)
3046 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
3050 case ST_STATEMENT_FUNCTION
:
3051 if (gfc_current_state () == COMP_MODULE
)
3053 unexpected_statement (st
);
3061 accept_statement (st
);
3062 st
= next_statement ();
3066 accept_statement (st
);
3068 st
= next_statement ();
3071 case ST_GET_FCN_CHARACTERISTICS
:
3072 /* This statement triggers the association of a function's result
3074 ts
= &gfc_current_block ()->result
->ts
;
3075 if (match_deferred_characteristics (ts
) != MATCH_YES
)
3076 bad_characteristic
= true;
3078 st
= next_statement ();
3085 /* If match_deferred_characteristics failed, then there is an error. */
3086 if (bad_characteristic
)
3088 ts
= &gfc_current_block ()->result
->ts
;
3089 if (ts
->type
!= BT_DERIVED
)
3090 gfc_error ("Bad kind expression for function '%s' at %L",
3091 gfc_current_block ()->name
,
3092 &gfc_current_block ()->declared_at
);
3094 gfc_error ("The type for function '%s' at %L is not accessible",
3095 gfc_current_block ()->name
,
3096 &gfc_current_block ()->declared_at
);
3098 gfc_current_block ()->ts
.kind
= 0;
3099 /* Keep the derived type; if it's bad, it will be discovered later. */
3100 if (!(ts
->type
== BT_DERIVED
&& ts
->u
.derived
))
3101 ts
->type
= BT_UNKNOWN
;
3108 /* Parse a WHERE block, (not a simple WHERE statement). */
3111 parse_where_block (void)
3113 int seen_empty_else
;
3118 accept_statement (ST_WHERE_BLOCK
);
3119 top
= gfc_state_stack
->tail
;
3121 push_state (&s
, COMP_WHERE
, gfc_new_block
);
3123 d
= add_statement ();
3124 d
->expr1
= top
->expr1
;
3130 seen_empty_else
= 0;
3134 st
= next_statement ();
3140 case ST_WHERE_BLOCK
:
3141 parse_where_block ();
3146 accept_statement (st
);
3150 if (seen_empty_else
)
3152 gfc_error ("ELSEWHERE statement at %C follows previous "
3153 "unmasked ELSEWHERE");
3154 reject_statement ();
3158 if (new_st
.expr1
== NULL
)
3159 seen_empty_else
= 1;
3161 d
= new_level (gfc_state_stack
->head
);
3163 d
->expr1
= new_st
.expr1
;
3165 accept_statement (st
);
3170 accept_statement (st
);
3174 gfc_error ("Unexpected %s statement in WHERE block at %C",
3175 gfc_ascii_statement (st
));
3176 reject_statement ();
3180 while (st
!= ST_END_WHERE
);
3186 /* Parse a FORALL block (not a simple FORALL statement). */
3189 parse_forall_block (void)
3195 accept_statement (ST_FORALL_BLOCK
);
3196 top
= gfc_state_stack
->tail
;
3198 push_state (&s
, COMP_FORALL
, gfc_new_block
);
3200 d
= add_statement ();
3201 d
->op
= EXEC_FORALL
;
3206 st
= next_statement ();
3211 case ST_POINTER_ASSIGNMENT
:
3214 accept_statement (st
);
3217 case ST_WHERE_BLOCK
:
3218 parse_where_block ();
3221 case ST_FORALL_BLOCK
:
3222 parse_forall_block ();
3226 accept_statement (st
);
3233 gfc_error ("Unexpected %s statement in FORALL block at %C",
3234 gfc_ascii_statement (st
));
3236 reject_statement ();
3240 while (st
!= ST_END_FORALL
);
3246 static gfc_statement
parse_executable (gfc_statement
);
3248 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
3251 parse_if_block (void)
3260 accept_statement (ST_IF_BLOCK
);
3262 top
= gfc_state_stack
->tail
;
3263 push_state (&s
, COMP_IF
, gfc_new_block
);
3265 new_st
.op
= EXEC_IF
;
3266 d
= add_statement ();
3268 d
->expr1
= top
->expr1
;
3274 st
= parse_executable (ST_NONE
);
3284 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
3285 "statement at %L", &else_locus
);
3287 reject_statement ();
3291 d
= new_level (gfc_state_stack
->head
);
3293 d
->expr1
= new_st
.expr1
;
3295 accept_statement (st
);
3302 gfc_error ("Duplicate ELSE statements at %L and %C",
3304 reject_statement ();
3309 else_locus
= gfc_current_locus
;
3311 d
= new_level (gfc_state_stack
->head
);
3314 accept_statement (st
);
3322 unexpected_statement (st
);
3326 while (st
!= ST_ENDIF
);
3329 accept_statement (st
);
3333 /* Parse a SELECT block. */
3336 parse_select_block (void)
3342 accept_statement (ST_SELECT_CASE
);
3344 cp
= gfc_state_stack
->tail
;
3345 push_state (&s
, COMP_SELECT
, gfc_new_block
);
3347 /* Make sure that the next statement is a CASE or END SELECT. */
3350 st
= next_statement ();
3353 if (st
== ST_END_SELECT
)
3355 /* Empty SELECT CASE is OK. */
3356 accept_statement (st
);
3363 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
3366 reject_statement ();
3369 /* At this point, we're got a nonempty select block. */
3370 cp
= new_level (cp
);
3373 accept_statement (st
);
3377 st
= parse_executable (ST_NONE
);
3384 cp
= new_level (gfc_state_stack
->head
);
3386 gfc_clear_new_st ();
3388 accept_statement (st
);
3394 /* Can't have an executable statement because of
3395 parse_executable(). */
3397 unexpected_statement (st
);
3401 while (st
!= ST_END_SELECT
);
3404 accept_statement (st
);
3408 /* Pop the current selector from the SELECT TYPE stack. */
3411 select_type_pop (void)
3413 gfc_select_type_stack
*old
= select_type_stack
;
3414 select_type_stack
= old
->prev
;
3419 /* Parse a SELECT TYPE construct (F03:R821). */
3422 parse_select_type_block (void)
3428 accept_statement (ST_SELECT_TYPE
);
3430 cp
= gfc_state_stack
->tail
;
3431 push_state (&s
, COMP_SELECT_TYPE
, gfc_new_block
);
3433 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
3437 st
= next_statement ();
3440 if (st
== ST_END_SELECT
)
3441 /* Empty SELECT CASE is OK. */
3443 if (st
== ST_TYPE_IS
|| st
== ST_CLASS_IS
)
3446 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
3447 "following SELECT TYPE at %C");
3449 reject_statement ();
3452 /* At this point, we're got a nonempty select block. */
3453 cp
= new_level (cp
);
3456 accept_statement (st
);
3460 st
= parse_executable (ST_NONE
);
3468 cp
= new_level (gfc_state_stack
->head
);
3470 gfc_clear_new_st ();
3472 accept_statement (st
);
3478 /* Can't have an executable statement because of
3479 parse_executable(). */
3481 unexpected_statement (st
);
3485 while (st
!= ST_END_SELECT
);
3489 accept_statement (st
);
3490 gfc_current_ns
= gfc_current_ns
->parent
;
3495 /* Given a symbol, make sure it is not an iteration variable for a DO
3496 statement. This subroutine is called when the symbol is seen in a
3497 context that causes it to become redefined. If the symbol is an
3498 iterator, we generate an error message and return nonzero. */
3501 gfc_check_do_variable (gfc_symtree
*st
)
3505 for (s
=gfc_state_stack
; s
; s
= s
->previous
)
3506 if (s
->do_variable
== st
)
3508 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
3509 "loop beginning at %L", st
->name
, &s
->head
->loc
);
3517 /* Checks to see if the current statement label closes an enddo.
3518 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3519 an error) if it incorrectly closes an ENDDO. */
3522 check_do_closure (void)
3526 if (gfc_statement_label
== NULL
)
3529 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
3530 if (p
->state
== COMP_DO
|| p
->state
== COMP_DO_CONCURRENT
)
3534 return 0; /* No loops to close */
3536 if (p
->ext
.end_do_label
== gfc_statement_label
)
3538 if (p
== gfc_state_stack
)
3541 gfc_error ("End of nonblock DO statement at %C is within another block");
3545 /* At this point, the label doesn't terminate the innermost loop.
3546 Make sure it doesn't terminate another one. */
3547 for (; p
; p
= p
->previous
)
3548 if ((p
->state
== COMP_DO
|| p
->state
== COMP_DO_CONCURRENT
)
3549 && p
->ext
.end_do_label
== gfc_statement_label
)
3551 gfc_error ("End of nonblock DO statement at %C is interwoven "
3552 "with another DO loop");
3560 /* Parse a series of contained program units. */
3562 static void parse_progunit (gfc_statement
);
3565 /* Parse a CRITICAL block. */
3568 parse_critical_block (void)
3574 s
.ext
.end_do_label
= new_st
.label1
;
3576 accept_statement (ST_CRITICAL
);
3577 top
= gfc_state_stack
->tail
;
3579 push_state (&s
, COMP_CRITICAL
, gfc_new_block
);
3581 d
= add_statement ();
3582 d
->op
= EXEC_CRITICAL
;
3587 st
= parse_executable (ST_NONE
);
3595 case ST_END_CRITICAL
:
3596 if (s
.ext
.end_do_label
!= NULL
3597 && s
.ext
.end_do_label
!= gfc_statement_label
)
3598 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3599 "match CRITICAL label");
3601 if (gfc_statement_label
!= NULL
)
3603 new_st
.op
= EXEC_NOP
;
3609 unexpected_statement (st
);
3613 while (st
!= ST_END_CRITICAL
);
3616 accept_statement (st
);
3620 /* Set up the local namespace for a BLOCK construct. */
3623 gfc_build_block_ns (gfc_namespace
*parent_ns
)
3625 gfc_namespace
* my_ns
;
3626 static int numblock
= 1;
3628 my_ns
= gfc_get_namespace (parent_ns
, 1);
3629 my_ns
->construct_entities
= 1;
3631 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3632 code generation (so it must not be NULL).
3633 We set its recursive argument if our container procedure is recursive, so
3634 that local variables are accordingly placed on the stack when it
3635 will be necessary. */
3637 my_ns
->proc_name
= gfc_new_block
;
3641 char buffer
[20]; /* Enough to hold "block@2147483648\n". */
3643 snprintf(buffer
, sizeof(buffer
), "block@%d", numblock
++);
3644 gfc_get_symbol (buffer
, my_ns
, &my_ns
->proc_name
);
3645 t
= gfc_add_flavor (&my_ns
->proc_name
->attr
, FL_LABEL
,
3646 my_ns
->proc_name
->name
, NULL
);
3648 gfc_commit_symbol (my_ns
->proc_name
);
3651 if (parent_ns
->proc_name
)
3652 my_ns
->proc_name
->attr
.recursive
= parent_ns
->proc_name
->attr
.recursive
;
3658 /* Parse a BLOCK construct. */
3661 parse_block_construct (void)
3663 gfc_namespace
* my_ns
;
3666 gfc_notify_std (GFC_STD_F2008
, "BLOCK construct at %C");
3668 my_ns
= gfc_build_block_ns (gfc_current_ns
);
3670 new_st
.op
= EXEC_BLOCK
;
3671 new_st
.ext
.block
.ns
= my_ns
;
3672 new_st
.ext
.block
.assoc
= NULL
;
3673 accept_statement (ST_BLOCK
);
3675 push_state (&s
, COMP_BLOCK
, my_ns
->proc_name
);
3676 gfc_current_ns
= my_ns
;
3678 parse_progunit (ST_NONE
);
3680 gfc_current_ns
= gfc_current_ns
->parent
;
3685 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3686 behind the scenes with compiler-generated variables. */
3689 parse_associate (void)
3691 gfc_namespace
* my_ns
;
3694 gfc_association_list
* a
;
3696 gfc_notify_std (GFC_STD_F2003
, "ASSOCIATE construct at %C");
3698 my_ns
= gfc_build_block_ns (gfc_current_ns
);
3700 new_st
.op
= EXEC_BLOCK
;
3701 new_st
.ext
.block
.ns
= my_ns
;
3702 gcc_assert (new_st
.ext
.block
.assoc
);
3704 /* Add all associate-names as BLOCK variables. Creating them is enough
3705 for now, they'll get their values during trans-* phase. */
3706 gfc_current_ns
= my_ns
;
3707 for (a
= new_st
.ext
.block
.assoc
; a
; a
= a
->next
)
3711 if (gfc_get_sym_tree (a
->name
, NULL
, &a
->st
, false))
3715 sym
->attr
.flavor
= FL_VARIABLE
;
3717 sym
->declared_at
= a
->where
;
3718 gfc_set_sym_referenced (sym
);
3720 /* Initialize the typespec. It is not available in all cases,
3721 however, as it may only be set on the target during resolution.
3722 Still, sometimes it helps to have it right now -- especially
3723 for parsing component references on the associate-name
3724 in case of association to a derived-type. */
3725 sym
->ts
= a
->target
->ts
;
3728 accept_statement (ST_ASSOCIATE
);
3729 push_state (&s
, COMP_ASSOCIATE
, my_ns
->proc_name
);
3732 st
= parse_executable (ST_NONE
);
3739 accept_statement (st
);
3740 my_ns
->code
= gfc_state_stack
->head
;
3744 unexpected_statement (st
);
3748 gfc_current_ns
= gfc_current_ns
->parent
;
3753 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
3754 handled inside of parse_executable(), because they aren't really
3758 parse_do_block (void)
3767 s
.ext
.end_do_label
= new_st
.label1
;
3769 if (new_st
.ext
.iterator
!= NULL
)
3770 stree
= new_st
.ext
.iterator
->var
->symtree
;
3774 accept_statement (ST_DO
);
3776 top
= gfc_state_stack
->tail
;
3777 push_state (&s
, do_op
== EXEC_DO_CONCURRENT
? COMP_DO_CONCURRENT
: COMP_DO
,
3780 s
.do_variable
= stree
;
3782 top
->block
= new_level (top
);
3783 top
->block
->op
= EXEC_DO
;
3786 st
= parse_executable (ST_NONE
);
3794 if (s
.ext
.end_do_label
!= NULL
3795 && s
.ext
.end_do_label
!= gfc_statement_label
)
3796 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
3799 if (gfc_statement_label
!= NULL
)
3801 new_st
.op
= EXEC_NOP
;
3806 case ST_IMPLIED_ENDDO
:
3807 /* If the do-stmt of this DO construct has a do-construct-name,
3808 the corresponding end-do must be an end-do-stmt (with a matching
3809 name, but in that case we must have seen ST_ENDDO first).
3810 We only complain about this in pedantic mode. */
3811 if (gfc_current_block () != NULL
)
3812 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
3813 &gfc_current_block()->declared_at
);
3818 unexpected_statement (st
);
3823 accept_statement (st
);
3827 /* Parse the statements of OpenMP do/parallel do. */
3829 static gfc_statement
3830 parse_omp_do (gfc_statement omp_st
)
3836 accept_statement (omp_st
);
3838 cp
= gfc_state_stack
->tail
;
3839 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3840 np
= new_level (cp
);
3846 st
= next_statement ();
3849 else if (st
== ST_DO
)
3852 unexpected_statement (st
);
3856 if (gfc_statement_label
!= NULL
3857 && gfc_state_stack
->previous
!= NULL
3858 && gfc_state_stack
->previous
->state
== COMP_DO
3859 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
3867 there should be no !$OMP END DO. */
3869 return ST_IMPLIED_ENDDO
;
3872 check_do_closure ();
3875 st
= next_statement ();
3876 gfc_statement omp_end_st
= ST_OMP_END_DO
;
3879 case ST_OMP_DISTRIBUTE
: omp_end_st
= ST_OMP_END_DISTRIBUTE
; break;
3880 case ST_OMP_DISTRIBUTE_PARALLEL_DO
:
3881 omp_end_st
= ST_OMP_END_DISTRIBUTE_PARALLEL_DO
;
3883 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
3884 omp_end_st
= ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD
;
3886 case ST_OMP_DISTRIBUTE_SIMD
:
3887 omp_end_st
= ST_OMP_END_DISTRIBUTE_SIMD
;
3889 case ST_OMP_DO
: omp_end_st
= ST_OMP_END_DO
; break;
3890 case ST_OMP_DO_SIMD
: omp_end_st
= ST_OMP_END_DO_SIMD
; break;
3891 case ST_OMP_PARALLEL_DO
: omp_end_st
= ST_OMP_END_PARALLEL_DO
; break;
3892 case ST_OMP_PARALLEL_DO_SIMD
:
3893 omp_end_st
= ST_OMP_END_PARALLEL_DO_SIMD
;
3895 case ST_OMP_SIMD
: omp_end_st
= ST_OMP_END_SIMD
; break;
3896 case ST_OMP_TARGET_TEAMS_DISTRIBUTE
:
3897 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE
;
3899 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
3900 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
;
3902 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
3903 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
;
3905 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
3906 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD
;
3908 case ST_OMP_TEAMS_DISTRIBUTE
:
3909 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE
;
3911 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
3912 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO
;
3914 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
3915 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
;
3917 case ST_OMP_TEAMS_DISTRIBUTE_SIMD
:
3918 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_SIMD
;
3920 default: gcc_unreachable ();
3922 if (st
== omp_end_st
)
3924 if (new_st
.op
== EXEC_OMP_END_NOWAIT
)
3925 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
3927 gcc_assert (new_st
.op
== EXEC_NOP
);
3928 gfc_clear_new_st ();
3929 gfc_commit_symbols ();
3930 gfc_warning_check ();
3931 st
= next_statement ();
3937 /* Parse the statements of OpenMP atomic directive. */
3939 static gfc_statement
3940 parse_omp_atomic (void)
3947 accept_statement (ST_OMP_ATOMIC
);
3949 cp
= gfc_state_stack
->tail
;
3950 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3951 np
= new_level (cp
);
3954 count
= 1 + ((cp
->ext
.omp_atomic
& GFC_OMP_ATOMIC_MASK
)
3955 == GFC_OMP_ATOMIC_CAPTURE
);
3959 st
= next_statement ();
3962 else if (st
== ST_ASSIGNMENT
)
3964 accept_statement (st
);
3968 unexpected_statement (st
);
3973 st
= next_statement ();
3974 if (st
== ST_OMP_END_ATOMIC
)
3976 gfc_clear_new_st ();
3977 gfc_commit_symbols ();
3978 gfc_warning_check ();
3979 st
= next_statement ();
3981 else if ((cp
->ext
.omp_atomic
& GFC_OMP_ATOMIC_MASK
)
3982 == GFC_OMP_ATOMIC_CAPTURE
)
3983 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
3988 /* Parse the statements of an OpenMP structured block. */
3991 parse_omp_structured_block (gfc_statement omp_st
, bool workshare_stmts_only
)
3993 gfc_statement st
, omp_end_st
;
3997 accept_statement (omp_st
);
3999 cp
= gfc_state_stack
->tail
;
4000 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
4001 np
= new_level (cp
);
4007 case ST_OMP_PARALLEL
:
4008 omp_end_st
= ST_OMP_END_PARALLEL
;
4010 case ST_OMP_PARALLEL_SECTIONS
:
4011 omp_end_st
= ST_OMP_END_PARALLEL_SECTIONS
;
4013 case ST_OMP_SECTIONS
:
4014 omp_end_st
= ST_OMP_END_SECTIONS
;
4016 case ST_OMP_ORDERED
:
4017 omp_end_st
= ST_OMP_END_ORDERED
;
4019 case ST_OMP_CRITICAL
:
4020 omp_end_st
= ST_OMP_END_CRITICAL
;
4023 omp_end_st
= ST_OMP_END_MASTER
;
4026 omp_end_st
= ST_OMP_END_SINGLE
;
4029 omp_end_st
= ST_OMP_END_TARGET
;
4031 case ST_OMP_TARGET_DATA
:
4032 omp_end_st
= ST_OMP_END_TARGET_DATA
;
4034 case ST_OMP_TARGET_TEAMS
:
4035 omp_end_st
= ST_OMP_END_TARGET_TEAMS
;
4037 case ST_OMP_TARGET_TEAMS_DISTRIBUTE
:
4038 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE
;
4040 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4041 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
;
4043 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4044 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
;
4046 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
4047 omp_end_st
= ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD
;
4050 omp_end_st
= ST_OMP_END_TASK
;
4052 case ST_OMP_TASKGROUP
:
4053 omp_end_st
= ST_OMP_END_TASKGROUP
;
4056 omp_end_st
= ST_OMP_END_TEAMS
;
4058 case ST_OMP_TEAMS_DISTRIBUTE
:
4059 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE
;
4061 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4062 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO
;
4064 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4065 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
;
4067 case ST_OMP_TEAMS_DISTRIBUTE_SIMD
:
4068 omp_end_st
= ST_OMP_END_TEAMS_DISTRIBUTE_SIMD
;
4070 case ST_OMP_DISTRIBUTE
:
4071 omp_end_st
= ST_OMP_END_DISTRIBUTE
;
4073 case ST_OMP_DISTRIBUTE_PARALLEL_DO
:
4074 omp_end_st
= ST_OMP_END_DISTRIBUTE_PARALLEL_DO
;
4076 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
4077 omp_end_st
= ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD
;
4079 case ST_OMP_DISTRIBUTE_SIMD
:
4080 omp_end_st
= ST_OMP_END_DISTRIBUTE_SIMD
;
4082 case ST_OMP_WORKSHARE
:
4083 omp_end_st
= ST_OMP_END_WORKSHARE
;
4085 case ST_OMP_PARALLEL_WORKSHARE
:
4086 omp_end_st
= ST_OMP_END_PARALLEL_WORKSHARE
;
4094 if (workshare_stmts_only
)
4096 /* Inside of !$omp workshare, only
4099 where statements and constructs
4100 forall statements and constructs
4104 are allowed. For !$omp critical these
4105 restrictions apply recursively. */
4108 st
= next_statement ();
4119 accept_statement (st
);
4122 case ST_WHERE_BLOCK
:
4123 parse_where_block ();
4126 case ST_FORALL_BLOCK
:
4127 parse_forall_block ();
4130 case ST_OMP_PARALLEL
:
4131 case ST_OMP_PARALLEL_SECTIONS
:
4132 parse_omp_structured_block (st
, false);
4135 case ST_OMP_PARALLEL_WORKSHARE
:
4136 case ST_OMP_CRITICAL
:
4137 parse_omp_structured_block (st
, true);
4140 case ST_OMP_PARALLEL_DO
:
4141 case ST_OMP_PARALLEL_DO_SIMD
:
4142 st
= parse_omp_do (st
);
4146 st
= parse_omp_atomic ();
4157 st
= next_statement ();
4161 st
= parse_executable (ST_NONE
);
4164 else if (st
== ST_OMP_SECTION
4165 && (omp_st
== ST_OMP_SECTIONS
4166 || omp_st
== ST_OMP_PARALLEL_SECTIONS
))
4168 np
= new_level (np
);
4172 else if (st
!= omp_end_st
)
4173 unexpected_statement (st
);
4175 while (st
!= omp_end_st
);
4179 case EXEC_OMP_END_NOWAIT
:
4180 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
4182 case EXEC_OMP_CRITICAL
:
4183 if (((cp
->ext
.omp_name
== NULL
) ^ (new_st
.ext
.omp_name
== NULL
))
4184 || (new_st
.ext
.omp_name
!= NULL
4185 && strcmp (cp
->ext
.omp_name
, new_st
.ext
.omp_name
) != 0))
4186 gfc_error ("Name after !$omp critical and !$omp end critical does "
4188 free (CONST_CAST (char *, new_st
.ext
.omp_name
));
4190 case EXEC_OMP_END_SINGLE
:
4191 cp
->ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
]
4192 = new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
];
4193 new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
] = NULL
;
4194 gfc_free_omp_clauses (new_st
.ext
.omp_clauses
);
4202 gfc_clear_new_st ();
4203 gfc_commit_symbols ();
4204 gfc_warning_check ();
4209 /* Accept a series of executable statements. We return the first
4210 statement that doesn't fit to the caller. Any block statements are
4211 passed on to the correct handler, which usually passes the buck
4214 static gfc_statement
4215 parse_executable (gfc_statement st
)
4220 st
= next_statement ();
4224 close_flag
= check_do_closure ();
4229 case ST_END_PROGRAM
:
4232 case ST_END_FUNCTION
:
4237 case ST_END_SUBROUTINE
:
4242 case ST_SELECT_CASE
:
4243 gfc_error ("%s statement at %C cannot terminate a non-block "
4244 "DO loop", gfc_ascii_statement (st
));
4257 gfc_notify_std (GFC_STD_F95_OBS
, "DATA statement at %C after the "
4258 "first executable statement");
4264 accept_statement (st
);
4265 if (close_flag
== 1)
4266 return ST_IMPLIED_ENDDO
;
4270 parse_block_construct ();
4281 case ST_SELECT_CASE
:
4282 parse_select_block ();
4285 case ST_SELECT_TYPE
:
4286 parse_select_type_block();
4291 if (check_do_closure () == 1)
4292 return ST_IMPLIED_ENDDO
;
4296 parse_critical_block ();
4299 case ST_WHERE_BLOCK
:
4300 parse_where_block ();
4303 case ST_FORALL_BLOCK
:
4304 parse_forall_block ();
4307 case ST_OMP_PARALLEL
:
4308 case ST_OMP_PARALLEL_SECTIONS
:
4309 case ST_OMP_SECTIONS
:
4310 case ST_OMP_ORDERED
:
4311 case ST_OMP_CRITICAL
:
4315 case ST_OMP_TARGET_DATA
:
4316 case ST_OMP_TARGET_TEAMS
:
4319 case ST_OMP_TASKGROUP
:
4320 parse_omp_structured_block (st
, false);
4323 case ST_OMP_WORKSHARE
:
4324 case ST_OMP_PARALLEL_WORKSHARE
:
4325 parse_omp_structured_block (st
, true);
4328 case ST_OMP_DISTRIBUTE
:
4329 case ST_OMP_DISTRIBUTE_PARALLEL_DO
:
4330 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD
:
4331 case ST_OMP_DISTRIBUTE_SIMD
:
4333 case ST_OMP_DO_SIMD
:
4334 case ST_OMP_PARALLEL_DO
:
4335 case ST_OMP_PARALLEL_DO_SIMD
:
4337 case ST_OMP_TARGET_TEAMS_DISTRIBUTE
:
4338 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4339 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4340 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
:
4341 case ST_OMP_TEAMS_DISTRIBUTE
:
4342 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO
:
4343 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
:
4344 case ST_OMP_TEAMS_DISTRIBUTE_SIMD
:
4345 st
= parse_omp_do (st
);
4346 if (st
== ST_IMPLIED_ENDDO
)
4351 st
= parse_omp_atomic ();
4358 st
= next_statement ();
4363 /* Fix the symbols for sibling functions. These are incorrectly added to
4364 the child namespace as the parser didn't know about this procedure. */
4367 gfc_fixup_sibling_symbols (gfc_symbol
*sym
, gfc_namespace
*siblings
)
4371 gfc_symbol
*old_sym
;
4373 for (ns
= siblings
; ns
; ns
= ns
->sibling
)
4375 st
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
4377 if (!st
|| (st
->n
.sym
->attr
.dummy
&& ns
== st
->n
.sym
->ns
))
4378 goto fixup_contained
;
4380 if ((st
->n
.sym
->attr
.flavor
== FL_DERIVED
4381 && sym
->attr
.generic
&& sym
->attr
.function
)
4382 ||(sym
->attr
.flavor
== FL_DERIVED
4383 && st
->n
.sym
->attr
.generic
&& st
->n
.sym
->attr
.function
))
4384 goto fixup_contained
;
4386 old_sym
= st
->n
.sym
;
4387 if (old_sym
->ns
== ns
4388 && !old_sym
->attr
.contained
4390 /* By 14.6.1.3, host association should be excluded
4391 for the following. */
4392 && !(old_sym
->attr
.external
4393 || (old_sym
->ts
.type
!= BT_UNKNOWN
4394 && !old_sym
->attr
.implicit_type
)
4395 || old_sym
->attr
.flavor
== FL_PARAMETER
4396 || old_sym
->attr
.use_assoc
4397 || old_sym
->attr
.in_common
4398 || old_sym
->attr
.in_equivalence
4399 || old_sym
->attr
.data
4400 || old_sym
->attr
.dummy
4401 || old_sym
->attr
.result
4402 || old_sym
->attr
.dimension
4403 || old_sym
->attr
.allocatable
4404 || old_sym
->attr
.intrinsic
4405 || old_sym
->attr
.generic
4406 || old_sym
->attr
.flavor
== FL_NAMELIST
4407 || old_sym
->attr
.flavor
== FL_LABEL
4408 || old_sym
->attr
.proc
== PROC_ST_FUNCTION
))
4410 /* Replace it with the symbol from the parent namespace. */
4414 gfc_release_symbol (old_sym
);
4418 /* Do the same for any contained procedures. */
4419 gfc_fixup_sibling_symbols (sym
, ns
->contained
);
4424 parse_contained (int module
)
4426 gfc_namespace
*ns
, *parent_ns
, *tmp
;
4427 gfc_state_data s1
, s2
;
4431 int contains_statements
= 0;
4434 push_state (&s1
, COMP_CONTAINS
, NULL
);
4435 parent_ns
= gfc_current_ns
;
4439 gfc_current_ns
= gfc_get_namespace (parent_ns
, 1);
4441 gfc_current_ns
->sibling
= parent_ns
->contained
;
4442 parent_ns
->contained
= gfc_current_ns
;
4445 /* Process the next available statement. We come here if we got an error
4446 and rejected the last statement. */
4447 st
= next_statement ();
4456 contains_statements
= 1;
4457 accept_statement (st
);
4460 (st
== ST_FUNCTION
) ? COMP_FUNCTION
: COMP_SUBROUTINE
,
4463 /* For internal procedures, create/update the symbol in the
4464 parent namespace. */
4468 if (gfc_get_symbol (gfc_new_block
->name
, parent_ns
, &sym
))
4469 gfc_error ("Contained procedure '%s' at %C is already "
4470 "ambiguous", gfc_new_block
->name
);
4473 if (gfc_add_procedure (&sym
->attr
, PROC_INTERNAL
,
4475 &gfc_new_block
->declared_at
))
4477 if (st
== ST_FUNCTION
)
4478 gfc_add_function (&sym
->attr
, sym
->name
,
4479 &gfc_new_block
->declared_at
);
4481 gfc_add_subroutine (&sym
->attr
, sym
->name
,
4482 &gfc_new_block
->declared_at
);
4486 gfc_commit_symbols ();
4489 sym
= gfc_new_block
;
4491 /* Mark this as a contained function, so it isn't replaced
4492 by other module functions. */
4493 sym
->attr
.contained
= 1;
4495 /* Set implicit_pure so that it can be reset if any of the
4496 tests for purity fail. This is used for some optimisation
4497 during translation. */
4498 if (!sym
->attr
.pure
)
4499 sym
->attr
.implicit_pure
= 1;
4501 parse_progunit (ST_NONE
);
4503 /* Fix up any sibling functions that refer to this one. */
4504 gfc_fixup_sibling_symbols (sym
, gfc_current_ns
);
4505 /* Or refer to any of its alternate entry points. */
4506 for (el
= gfc_current_ns
->entries
; el
; el
= el
->next
)
4507 gfc_fixup_sibling_symbols (el
->sym
, gfc_current_ns
);
4509 gfc_current_ns
->code
= s2
.head
;
4510 gfc_current_ns
= parent_ns
;
4515 /* These statements are associated with the end of the host unit. */
4516 case ST_END_FUNCTION
:
4518 case ST_END_PROGRAM
:
4519 case ST_END_SUBROUTINE
:
4520 accept_statement (st
);
4521 gfc_current_ns
->code
= s1
.head
;
4525 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
4526 gfc_ascii_statement (st
));
4527 reject_statement ();
4533 while (st
!= ST_END_FUNCTION
&& st
!= ST_END_SUBROUTINE
4534 && st
!= ST_END_MODULE
&& st
!= ST_END_PROGRAM
);
4536 /* The first namespace in the list is guaranteed to not have
4537 anything (worthwhile) in it. */
4538 tmp
= gfc_current_ns
;
4539 gfc_current_ns
= parent_ns
;
4540 if (seen_error
&& tmp
->refs
> 1)
4541 gfc_free_namespace (tmp
);
4543 ns
= gfc_current_ns
->contained
;
4544 gfc_current_ns
->contained
= ns
->sibling
;
4545 gfc_free_namespace (ns
);
4548 if (!contains_statements
)
4549 gfc_notify_std (GFC_STD_F2008
, "CONTAINS statement without "
4550 "FUNCTION or SUBROUTINE statement at %C");
4554 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
4557 parse_progunit (gfc_statement st
)
4562 st
= parse_spec (st
);
4569 /* This is not allowed within BLOCK! */
4570 if (gfc_current_state () != COMP_BLOCK
)
4575 accept_statement (st
);
4582 if (gfc_current_state () == COMP_FUNCTION
)
4583 gfc_check_function_type (gfc_current_ns
);
4588 st
= parse_executable (st
);
4596 /* This is not allowed within BLOCK! */
4597 if (gfc_current_state () != COMP_BLOCK
)
4602 accept_statement (st
);
4609 unexpected_statement (st
);
4610 reject_statement ();
4611 st
= next_statement ();
4617 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
4618 if (p
->state
== COMP_CONTAINS
)
4621 if (gfc_find_state (COMP_MODULE
) == true)
4626 gfc_error ("CONTAINS statement at %C is already in a contained "
4628 reject_statement ();
4629 st
= next_statement ();
4633 parse_contained (0);
4636 gfc_current_ns
->code
= gfc_state_stack
->head
;
4640 /* Come here to complain about a global symbol already in use as
4644 gfc_global_used (gfc_gsymbol
*sym
, locus
*where
)
4649 where
= &gfc_current_locus
;
4659 case GSYM_SUBROUTINE
:
4660 name
= "SUBROUTINE";
4665 case GSYM_BLOCK_DATA
:
4666 name
= "BLOCK DATA";
4672 gfc_internal_error ("gfc_global_used(): Bad type");
4676 if (sym
->binding_label
)
4677 gfc_error ("Global binding name '%s' at %L is already being used as a %s "
4678 "at %L", sym
->binding_label
, where
, name
, &sym
->where
);
4680 gfc_error ("Global name '%s' at %L is already being used as a %s at %L",
4681 sym
->name
, where
, name
, &sym
->where
);
4685 /* Parse a block data program unit. */
4688 parse_block_data (void)
4691 static locus blank_locus
;
4692 static int blank_block
=0;
4695 gfc_current_ns
->proc_name
= gfc_new_block
;
4696 gfc_current_ns
->is_block_data
= 1;
4698 if (gfc_new_block
== NULL
)
4701 gfc_error ("Blank BLOCK DATA at %C conflicts with "
4702 "prior BLOCK DATA at %L", &blank_locus
);
4706 blank_locus
= gfc_current_locus
;
4711 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4713 || (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_BLOCK_DATA
))
4714 gfc_global_used (s
, &gfc_new_block
->declared_at
);
4717 s
->type
= GSYM_BLOCK_DATA
;
4718 s
->where
= gfc_new_block
->declared_at
;
4723 st
= parse_spec (ST_NONE
);
4725 while (st
!= ST_END_BLOCK_DATA
)
4727 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
4728 gfc_ascii_statement (st
));
4729 reject_statement ();
4730 st
= next_statement ();
4735 /* Parse a module subprogram. */
4744 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4745 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_MODULE
))
4746 gfc_global_used (s
, &gfc_new_block
->declared_at
);
4749 s
->type
= GSYM_MODULE
;
4750 s
->where
= gfc_new_block
->declared_at
;
4754 st
= parse_spec (ST_NONE
);
4764 parse_contained (1);
4768 accept_statement (st
);
4772 gfc_error ("Unexpected %s statement in MODULE at %C",
4773 gfc_ascii_statement (st
));
4776 reject_statement ();
4777 st
= next_statement ();
4781 /* Make sure not to free the namespace twice on error. */
4783 s
->ns
= gfc_current_ns
;
4787 /* Add a procedure name to the global symbol table. */
4790 add_global_procedure (bool sub
)
4794 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
4795 name is a global identifier. */
4796 if (!gfc_new_block
->binding_label
|| gfc_notification_std (GFC_STD_F2008
))
4798 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4801 || (s
->type
!= GSYM_UNKNOWN
4802 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
4804 gfc_global_used (s
, &gfc_new_block
->declared_at
);
4805 /* Silence follow-up errors. */
4806 gfc_new_block
->binding_label
= NULL
;
4810 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
4811 s
->sym_name
= gfc_new_block
->name
;
4812 s
->where
= gfc_new_block
->declared_at
;
4814 s
->ns
= gfc_current_ns
;
4818 /* Don't add the symbol multiple times. */
4819 if (gfc_new_block
->binding_label
4820 && (!gfc_notification_std (GFC_STD_F2008
)
4821 || strcmp (gfc_new_block
->name
, gfc_new_block
->binding_label
) != 0))
4823 s
= gfc_get_gsymbol (gfc_new_block
->binding_label
);
4826 || (s
->type
!= GSYM_UNKNOWN
4827 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
4829 gfc_global_used (s
, &gfc_new_block
->declared_at
);
4830 /* Silence follow-up errors. */
4831 gfc_new_block
->binding_label
= NULL
;
4835 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
4836 s
->sym_name
= gfc_new_block
->name
;
4837 s
->binding_label
= gfc_new_block
->binding_label
;
4838 s
->where
= gfc_new_block
->declared_at
;
4840 s
->ns
= gfc_current_ns
;
4846 /* Add a program to the global symbol table. */
4849 add_global_program (void)
4853 if (gfc_new_block
== NULL
)
4855 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4857 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_PROGRAM
))
4858 gfc_global_used (s
, &gfc_new_block
->declared_at
);
4861 s
->type
= GSYM_PROGRAM
;
4862 s
->where
= gfc_new_block
->declared_at
;
4864 s
->ns
= gfc_current_ns
;
4869 /* Resolve all the program units. */
4871 resolve_all_program_units (gfc_namespace
*gfc_global_ns_list
)
4873 gfc_free_dt_list ();
4874 gfc_current_ns
= gfc_global_ns_list
;
4875 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4877 if (gfc_current_ns
->proc_name
4878 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
4879 continue; /* Already resolved. */
4881 if (gfc_current_ns
->proc_name
)
4882 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4883 gfc_resolve (gfc_current_ns
);
4884 gfc_current_ns
->derived_types
= gfc_derived_types
;
4885 gfc_derived_types
= NULL
;
4891 clean_up_modules (gfc_gsymbol
*gsym
)
4896 clean_up_modules (gsym
->left
);
4897 clean_up_modules (gsym
->right
);
4899 if (gsym
->type
!= GSYM_MODULE
|| !gsym
->ns
)
4902 gfc_current_ns
= gsym
->ns
;
4903 gfc_derived_types
= gfc_current_ns
->derived_types
;
4910 /* Translate all the program units. This could be in a different order
4911 to resolution if there are forward references in the file. */
4913 translate_all_program_units (gfc_namespace
*gfc_global_ns_list
)
4917 gfc_current_ns
= gfc_global_ns_list
;
4918 gfc_get_errors (NULL
, &errors
);
4920 /* We first translate all modules to make sure that later parts
4921 of the program can use the decl. Then we translate the nonmodules. */
4923 for (; !errors
&& gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4925 if (!gfc_current_ns
->proc_name
4926 || gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4929 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4930 gfc_derived_types
= gfc_current_ns
->derived_types
;
4931 gfc_generate_module_code (gfc_current_ns
);
4932 gfc_current_ns
->translated
= 1;
4935 gfc_current_ns
= gfc_global_ns_list
;
4936 for (; !errors
&& gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4938 if (gfc_current_ns
->proc_name
4939 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
4942 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4943 gfc_derived_types
= gfc_current_ns
->derived_types
;
4944 gfc_generate_code (gfc_current_ns
);
4945 gfc_current_ns
->translated
= 1;
4948 /* Clean up all the namespaces after translation. */
4949 gfc_current_ns
= gfc_global_ns_list
;
4950 for (;gfc_current_ns
;)
4954 if (gfc_current_ns
->proc_name
4955 && gfc_current_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
4957 gfc_current_ns
= gfc_current_ns
->sibling
;
4961 ns
= gfc_current_ns
->sibling
;
4962 gfc_derived_types
= gfc_current_ns
->derived_types
;
4964 gfc_current_ns
= ns
;
4967 clean_up_modules (gfc_gsym_root
);
4971 /* Top level parser. */
4974 gfc_parse_file (void)
4976 int seen_program
, errors_before
, errors
;
4977 gfc_state_data top
, s
;
4980 gfc_namespace
*next
;
4982 gfc_start_source_files ();
4984 top
.state
= COMP_NONE
;
4986 top
.previous
= NULL
;
4987 top
.head
= top
.tail
= NULL
;
4988 top
.do_variable
= NULL
;
4990 gfc_state_stack
= &top
;
4992 gfc_clear_new_st ();
4994 gfc_statement_label
= NULL
;
4996 if (setjmp (eof_buf
))
4997 return false; /* Come here on unexpected EOF */
4999 /* Prepare the global namespace that will contain the
5001 gfc_global_ns_list
= next
= NULL
;
5006 /* Exit early for empty files. */
5012 st
= next_statement ();
5021 goto duplicate_main
;
5023 prog_locus
= gfc_current_locus
;
5025 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
5026 main_program_symbol(gfc_current_ns
, gfc_new_block
->name
);
5027 accept_statement (st
);
5028 add_global_program ();
5029 parse_progunit (ST_NONE
);
5034 add_global_procedure (true);
5035 push_state (&s
, COMP_SUBROUTINE
, gfc_new_block
);
5036 accept_statement (st
);
5037 parse_progunit (ST_NONE
);
5042 add_global_procedure (false);
5043 push_state (&s
, COMP_FUNCTION
, gfc_new_block
);
5044 accept_statement (st
);
5045 parse_progunit (ST_NONE
);
5050 push_state (&s
, COMP_BLOCK_DATA
, gfc_new_block
);
5051 accept_statement (st
);
5052 parse_block_data ();
5056 push_state (&s
, COMP_MODULE
, gfc_new_block
);
5057 accept_statement (st
);
5059 gfc_get_errors (NULL
, &errors_before
);
5063 /* Anything else starts a nameless main program block. */
5066 goto duplicate_main
;
5068 prog_locus
= gfc_current_locus
;
5070 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
5071 main_program_symbol (gfc_current_ns
, "MAIN__");
5072 parse_progunit (st
);
5077 /* Handle the non-program units. */
5078 gfc_current_ns
->code
= s
.head
;
5080 gfc_resolve (gfc_current_ns
);
5082 /* Dump the parse tree if requested. */
5083 if (gfc_option
.dump_fortran_original
)
5084 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
5086 gfc_get_errors (NULL
, &errors
);
5087 if (s
.state
== COMP_MODULE
)
5089 gfc_dump_module (s
.sym
->name
, errors_before
== errors
);
5090 gfc_current_ns
->derived_types
= gfc_derived_types
;
5091 gfc_derived_types
= NULL
;
5097 gfc_generate_code (gfc_current_ns
);
5105 /* The main program and non-contained procedures are put
5106 in the global namespace list, so that they can be processed
5107 later and all their interfaces resolved. */
5108 gfc_current_ns
->code
= s
.head
;
5111 for (; next
->sibling
; next
= next
->sibling
)
5113 next
->sibling
= gfc_current_ns
;
5116 gfc_global_ns_list
= gfc_current_ns
;
5118 next
= gfc_current_ns
;
5125 /* Do the resolution. */
5126 resolve_all_program_units (gfc_global_ns_list
);
5128 /* Do the parse tree dump. */
5130 = gfc_option
.dump_fortran_original
? gfc_global_ns_list
: NULL
;
5132 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
5133 if (!gfc_current_ns
->proc_name
5134 || gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5136 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
5137 fputs ("------------------------------------------\n\n", stdout
);
5140 /* Do the translation. */
5141 translate_all_program_units (gfc_global_ns_list
);
5143 gfc_end_source_files ();
5147 /* If we see a duplicate main program, shut down. If the second
5148 instance is an implied main program, i.e. data decls or executable
5149 statements, we're in for lots of errors. */
5150 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus
);
5151 reject_statement ();