2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
31 /* Current statement label. Zero means no statement label. Because new_st
32 can get wiped during statement matching, we have to keep it separate. */
34 gfc_st_label
*gfc_statement_label
;
36 static locus label_locus
;
37 static jmp_buf eof_buf
;
39 gfc_state_data
*gfc_state_stack
;
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 /* Figure out what the next statement is, (mostly) regardless of
78 proper ordering. The do...while(0) is there to prevent if/else
81 #define match(keyword, subr, st) \
83 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
86 undo_new_statement (); \
90 /* This is a specialist version of decode_statement that is used
91 for the specification statements in a function, whose
92 characteristics are deferred into the specification statements.
93 eg.: INTEGER (king = mykind) foo ()
94 USE mymodule, ONLY mykind.....
95 The KIND parameter needs a return after USE or IMPORT, whereas
96 derived type declarations can occur anywhere, up the executable
97 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
98 out of the correct kind of specification statements. */
100 decode_specification_statement (void)
106 if (gfc_match_eos () == MATCH_YES
)
109 old_locus
= gfc_current_locus
;
111 match ("import", gfc_match_import
, ST_IMPORT
);
112 match ("use", gfc_match_use
, ST_USE
);
114 if (gfc_current_block ()->result
->ts
.type
!= BT_DERIVED
)
117 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
118 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
119 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
121 /* General statement matching: Instead of testing every possible
122 statement, we eliminate most possibilities by peeking at the
125 c
= gfc_peek_ascii_char ();
130 match ("abstract% interface", gfc_match_abstract_interface
,
132 match ("allocatable", gfc_match_asynchronous
, ST_ATTR_DECL
);
133 match ("asynchronous", gfc_match_asynchronous
, ST_ATTR_DECL
);
137 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
144 match ("data", gfc_match_data
, ST_DATA
);
145 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
149 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
150 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
151 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
152 match ("external", gfc_match_external
, ST_ATTR_DECL
);
156 match ("format", gfc_match_format
, ST_FORMAT
);
163 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
164 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
165 match ("interface", gfc_match_interface
, ST_INTERFACE
);
166 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
167 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
174 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
178 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
182 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
183 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
184 if (gfc_match_private (&st
) == MATCH_YES
)
186 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
187 if (gfc_match_public (&st
) == MATCH_YES
)
189 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
196 match ("save", gfc_match_save
, ST_ATTR_DECL
);
200 match ("target", gfc_match_target
, ST_ATTR_DECL
);
201 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
208 match ("value", gfc_match_value
, ST_ATTR_DECL
);
209 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
216 /* This is not a specification statement. See if any of the matchers
217 has stored an error message of some sort. */
221 gfc_buffer_error (0);
222 gfc_current_locus
= old_locus
;
224 return ST_GET_FCN_CHARACTERISTICS
;
228 /* This is the primary 'decode_statement'. */
230 decode_statement (void)
241 gfc_clear_error (); /* Clear any pending errors. */
242 gfc_clear_warning (); /* Clear any pending warnings. */
244 gfc_matching_function
= false;
246 if (gfc_match_eos () == MATCH_YES
)
249 if (gfc_current_state () == COMP_FUNCTION
250 && gfc_current_block ()->result
->ts
.kind
== -1)
251 return decode_specification_statement ();
253 old_locus
= gfc_current_locus
;
255 /* Try matching a data declaration or function declaration. The
256 input "REALFUNCTIONA(N)" can mean several things in different
257 contexts, so it (and its relatives) get special treatment. */
259 if (gfc_current_state () == COMP_NONE
260 || gfc_current_state () == COMP_INTERFACE
261 || gfc_current_state () == COMP_CONTAINS
)
263 gfc_matching_function
= true;
264 m
= gfc_match_function_decl ();
267 else if (m
== MATCH_ERROR
)
271 gfc_current_locus
= old_locus
;
273 gfc_matching_function
= false;
276 /* Match statements whose error messages are meant to be overwritten
277 by something better. */
279 match (NULL
, gfc_match_assignment
, ST_ASSIGNMENT
);
280 match (NULL
, gfc_match_pointer_assignment
, ST_POINTER_ASSIGNMENT
);
281 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
283 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
284 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
286 /* Try to match a subroutine statement, which has the same optional
287 prefixes that functions can have. */
289 if (gfc_match_subroutine () == MATCH_YES
)
290 return ST_SUBROUTINE
;
292 gfc_current_locus
= old_locus
;
294 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, and BLOCK
295 statements, which might begin with a block label. The match functions for
296 these statements are unusual in that their keyword is not seen before
297 the matcher is called. */
299 if (gfc_match_if (&st
) == MATCH_YES
)
302 gfc_current_locus
= old_locus
;
304 if (gfc_match_where (&st
) == MATCH_YES
)
307 gfc_current_locus
= old_locus
;
309 if (gfc_match_forall (&st
) == MATCH_YES
)
312 gfc_current_locus
= old_locus
;
314 match (NULL
, gfc_match_do
, ST_DO
);
315 match (NULL
, gfc_match_block
, ST_BLOCK
);
316 match (NULL
, gfc_match_critical
, ST_CRITICAL
);
317 match (NULL
, gfc_match_select
, ST_SELECT_CASE
);
318 match (NULL
, gfc_match_select_type
, ST_SELECT_TYPE
);
320 /* General statement matching: Instead of testing every possible
321 statement, we eliminate most possibilities by peeking at the
324 c
= gfc_peek_ascii_char ();
329 match ("abstract% interface", gfc_match_abstract_interface
,
331 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
);
332 match ("allocatable", gfc_match_allocatable
, ST_ATTR_DECL
);
333 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
);
334 match ("asynchronous", gfc_match_asynchronous
, ST_ATTR_DECL
);
338 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
);
339 match ("block data", gfc_match_block_data
, ST_BLOCK_DATA
);
340 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
344 match ("call", gfc_match_call
, ST_CALL
);
345 match ("close", gfc_match_close
, ST_CLOSE
);
346 match ("continue", gfc_match_continue
, ST_CONTINUE
);
347 match ("cycle", gfc_match_cycle
, ST_CYCLE
);
348 match ("case", gfc_match_case
, ST_CASE
);
349 match ("common", gfc_match_common
, ST_COMMON
);
350 match ("contains", gfc_match_eos
, ST_CONTAINS
);
351 match ("class", gfc_match_class_is
, ST_CLASS_IS
);
355 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
);
356 match ("data", gfc_match_data
, ST_DATA
);
357 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
361 match ("end file", gfc_match_endfile
, ST_END_FILE
);
362 match ("exit", gfc_match_exit
, ST_EXIT
);
363 match ("else", gfc_match_else
, ST_ELSE
);
364 match ("else where", gfc_match_elsewhere
, ST_ELSEWHERE
);
365 match ("else if", gfc_match_elseif
, ST_ELSEIF
);
366 match ("error stop", gfc_match_error_stop
, ST_ERROR_STOP
);
367 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
369 if (gfc_match_end (&st
) == MATCH_YES
)
372 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
373 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
374 match ("external", gfc_match_external
, ST_ATTR_DECL
);
378 match ("final", gfc_match_final_decl
, ST_FINAL
);
379 match ("flush", gfc_match_flush
, ST_FLUSH
);
380 match ("format", gfc_match_format
, ST_FORMAT
);
384 match ("generic", gfc_match_generic
, ST_GENERIC
);
385 match ("go to", gfc_match_goto
, ST_GOTO
);
389 match ("inquire", gfc_match_inquire
, ST_INQUIRE
);
390 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
391 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
392 match ("import", gfc_match_import
, ST_IMPORT
);
393 match ("interface", gfc_match_interface
, ST_INTERFACE
);
394 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
395 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
399 match ("module% procedure% ", gfc_match_modproc
, ST_MODULE_PROC
);
400 match ("module", gfc_match_module
, ST_MODULE
);
404 match ("nullify", gfc_match_nullify
, ST_NULLIFY
);
405 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
409 match ("open", gfc_match_open
, ST_OPEN
);
410 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
414 match ("print", gfc_match_print
, ST_WRITE
);
415 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
416 match ("pause", gfc_match_pause
, ST_PAUSE
);
417 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
418 if (gfc_match_private (&st
) == MATCH_YES
)
420 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
421 match ("program", gfc_match_program
, ST_PROGRAM
);
422 if (gfc_match_public (&st
) == MATCH_YES
)
424 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
428 match ("read", gfc_match_read
, ST_READ
);
429 match ("return", gfc_match_return
, ST_RETURN
);
430 match ("rewind", gfc_match_rewind
, ST_REWIND
);
434 match ("sequence", gfc_match_eos
, ST_SEQUENCE
);
435 match ("stop", gfc_match_stop
, ST_STOP
);
436 match ("save", gfc_match_save
, ST_ATTR_DECL
);
437 match ("sync all", gfc_match_sync_all
, ST_SYNC_ALL
);
438 match ("sync images", gfc_match_sync_images
, ST_SYNC_IMAGES
);
439 match ("sync memory", gfc_match_sync_memory
, ST_SYNC_MEMORY
);
443 match ("target", gfc_match_target
, ST_ATTR_DECL
);
444 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
445 match ("type is", gfc_match_type_is
, ST_TYPE_IS
);
449 match ("use", gfc_match_use
, ST_USE
);
453 match ("value", gfc_match_value
, ST_ATTR_DECL
);
454 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
458 match ("wait", gfc_match_wait
, ST_WAIT
);
459 match ("write", gfc_match_write
, ST_WRITE
);
463 /* All else has failed, so give up. See if any of the matchers has
464 stored an error message of some sort. */
466 if (gfc_error_check () == 0)
467 gfc_error_now ("Unclassifiable statement at %C");
471 gfc_error_recovery ();
477 decode_omp_directive (void)
486 gfc_clear_error (); /* Clear any pending errors. */
487 gfc_clear_warning (); /* Clear any pending warnings. */
491 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
492 "or ELEMENTAL procedures");
493 gfc_error_recovery ();
497 old_locus
= gfc_current_locus
;
499 /* General OpenMP directive matching: Instead of testing every possible
500 statement, we eliminate most possibilities by peeking at the
503 c
= gfc_peek_ascii_char ();
508 match ("atomic", gfc_match_omp_atomic
, ST_OMP_ATOMIC
);
511 match ("barrier", gfc_match_omp_barrier
, ST_OMP_BARRIER
);
514 match ("critical", gfc_match_omp_critical
, ST_OMP_CRITICAL
);
517 match ("do", gfc_match_omp_do
, ST_OMP_DO
);
520 match ("end critical", gfc_match_omp_critical
, ST_OMP_END_CRITICAL
);
521 match ("end do", gfc_match_omp_end_nowait
, ST_OMP_END_DO
);
522 match ("end master", gfc_match_omp_eos
, ST_OMP_END_MASTER
);
523 match ("end ordered", gfc_match_omp_eos
, ST_OMP_END_ORDERED
);
524 match ("end parallel do", gfc_match_omp_eos
, ST_OMP_END_PARALLEL_DO
);
525 match ("end parallel sections", gfc_match_omp_eos
,
526 ST_OMP_END_PARALLEL_SECTIONS
);
527 match ("end parallel workshare", gfc_match_omp_eos
,
528 ST_OMP_END_PARALLEL_WORKSHARE
);
529 match ("end parallel", gfc_match_omp_eos
, ST_OMP_END_PARALLEL
);
530 match ("end sections", gfc_match_omp_end_nowait
, ST_OMP_END_SECTIONS
);
531 match ("end single", gfc_match_omp_end_single
, ST_OMP_END_SINGLE
);
532 match ("end task", gfc_match_omp_eos
, ST_OMP_END_TASK
);
533 match ("end workshare", gfc_match_omp_end_nowait
,
534 ST_OMP_END_WORKSHARE
);
537 match ("flush", gfc_match_omp_flush
, ST_OMP_FLUSH
);
540 match ("master", gfc_match_omp_master
, ST_OMP_MASTER
);
543 match ("ordered", gfc_match_omp_ordered
, ST_OMP_ORDERED
);
546 match ("parallel do", gfc_match_omp_parallel_do
, ST_OMP_PARALLEL_DO
);
547 match ("parallel sections", gfc_match_omp_parallel_sections
,
548 ST_OMP_PARALLEL_SECTIONS
);
549 match ("parallel workshare", gfc_match_omp_parallel_workshare
,
550 ST_OMP_PARALLEL_WORKSHARE
);
551 match ("parallel", gfc_match_omp_parallel
, ST_OMP_PARALLEL
);
554 match ("sections", gfc_match_omp_sections
, ST_OMP_SECTIONS
);
555 match ("section", gfc_match_omp_eos
, ST_OMP_SECTION
);
556 match ("single", gfc_match_omp_single
, ST_OMP_SINGLE
);
559 match ("task", gfc_match_omp_task
, ST_OMP_TASK
);
560 match ("taskwait", gfc_match_omp_taskwait
, ST_OMP_TASKWAIT
);
561 match ("threadprivate", gfc_match_omp_threadprivate
,
562 ST_OMP_THREADPRIVATE
);
564 match ("workshare", gfc_match_omp_workshare
, ST_OMP_WORKSHARE
);
568 /* All else has failed, so give up. See if any of the matchers has
569 stored an error message of some sort. */
571 if (gfc_error_check () == 0)
572 gfc_error_now ("Unclassifiable OpenMP directive at %C");
576 gfc_error_recovery ();
582 decode_gcc_attribute (void)
590 gfc_clear_error (); /* Clear any pending errors. */
591 gfc_clear_warning (); /* Clear any pending warnings. */
592 old_locus
= gfc_current_locus
;
594 match ("attributes", gfc_match_gcc_attributes
, ST_ATTR_DECL
);
596 /* All else has failed, so give up. See if any of the matchers has
597 stored an error message of some sort. */
599 if (gfc_error_check () == 0)
600 gfc_error_now ("Unclassifiable GCC directive at %C");
604 gfc_error_recovery ();
612 /* Get the next statement in free form source. */
621 at_bol
= gfc_at_bol ();
622 gfc_gobble_whitespace ();
624 c
= gfc_peek_ascii_char ();
630 /* Found a statement label? */
631 m
= gfc_match_st_label (&gfc_statement_label
);
633 d
= gfc_peek_ascii_char ();
634 if (m
!= MATCH_YES
|| !gfc_is_whitespace (d
))
636 gfc_match_small_literal_int (&i
, &cnt
);
639 gfc_error_now ("Too many digits in statement label at %C");
642 gfc_error_now ("Zero is not a valid statement label at %C");
645 c
= gfc_next_ascii_char ();
648 if (!gfc_is_whitespace (c
))
649 gfc_error_now ("Non-numeric character in statement label at %C");
655 label_locus
= gfc_current_locus
;
657 gfc_gobble_whitespace ();
659 if (at_bol
&& gfc_peek_ascii_char () == ';')
661 gfc_error_now ("Semicolon at %C needs to be preceded by "
663 gfc_next_ascii_char (); /* Eat up the semicolon. */
667 if (gfc_match_eos () == MATCH_YES
)
669 gfc_warning_now ("Ignoring statement label in empty statement "
670 "at %L", &label_locus
);
671 gfc_free_st_label (gfc_statement_label
);
672 gfc_statement_label
= NULL
;
679 /* Comments have already been skipped by the time we get here,
680 except for GCC attributes and OpenMP directives. */
682 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
683 c
= gfc_peek_ascii_char ();
689 c
= gfc_next_ascii_char ();
690 for (i
= 0; i
< 4; i
++, c
= gfc_next_ascii_char ())
691 gcc_assert (c
== "gcc$"[i
]);
693 gfc_gobble_whitespace ();
694 return decode_gcc_attribute ();
697 else if (c
== '$' && gfc_option
.flag_openmp
)
701 c
= gfc_next_ascii_char ();
702 for (i
= 0; i
< 4; i
++, c
= gfc_next_ascii_char ())
703 gcc_assert (c
== "$omp"[i
]);
705 gcc_assert (c
== ' ' || c
== '\t');
706 gfc_gobble_whitespace ();
707 return decode_omp_directive ();
713 if (at_bol
&& c
== ';')
715 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
716 gfc_next_ascii_char (); /* Eat up the semicolon. */
720 return decode_statement ();
724 /* Get the next statement in fixed-form source. */
729 int label
, digit_flag
, i
;
734 return decode_statement ();
736 /* Skip past the current label field, parsing a statement label if
737 one is there. This is a weird number parser, since the number is
738 contained within five columns and can have any kind of embedded
739 spaces. We also check for characters that make the rest of the
745 for (i
= 0; i
< 5; i
++)
747 c
= gfc_next_char_literal (0);
764 label
= label
* 10 + ((unsigned char) c
- '0');
765 label_locus
= gfc_current_locus
;
769 /* Comments have already been skipped by the time we get
770 here, except for GCC attributes and OpenMP directives. */
773 c
= gfc_next_char_literal (0);
775 if (TOLOWER (c
) == 'g')
777 for (i
= 0; i
< 4; i
++, c
= gfc_next_char_literal (0))
778 gcc_assert (TOLOWER (c
) == "gcc$"[i
]);
780 return decode_gcc_attribute ();
782 else if (c
== '$' && gfc_option
.flag_openmp
)
784 for (i
= 0; i
< 4; i
++, c
= gfc_next_char_literal (0))
785 gcc_assert ((char) gfc_wide_tolower (c
) == "$omp"[i
]);
787 if (c
!= ' ' && c
!= '0')
789 gfc_buffer_error (0);
790 gfc_error ("Bad continuation line at %C");
794 return decode_omp_directive ();
798 /* Comments have already been skipped by the time we get
799 here so don't bother checking for them. */
802 gfc_buffer_error (0);
803 gfc_error ("Non-numeric character in statement label at %C");
811 gfc_warning_now ("Zero is not a valid statement label at %C");
814 /* We've found a valid statement label. */
815 gfc_statement_label
= gfc_get_st_label (label
);
819 /* Since this line starts a statement, it cannot be a continuation
820 of a previous statement. If we see something here besides a
821 space or zero, it must be a bad continuation line. */
823 c
= gfc_next_char_literal (0);
827 if (c
!= ' ' && c
!= '0')
829 gfc_buffer_error (0);
830 gfc_error ("Bad continuation line at %C");
834 /* Now that we've taken care of the statement label columns, we have
835 to make sure that the first nonblank character is not a '!'. If
836 it is, the rest of the line is a comment. */
840 loc
= gfc_current_locus
;
841 c
= gfc_next_char_literal (0);
843 while (gfc_is_whitespace (c
));
847 gfc_current_locus
= loc
;
851 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
855 if (gfc_match_eos () == MATCH_YES
)
858 /* At this point, we've got a nonblank statement to parse. */
859 return decode_statement ();
863 gfc_warning_now ("Ignoring statement label in empty statement at %L",
866 gfc_current_locus
.lb
->truncated
= 0;
872 /* Return the next non-ST_NONE statement to the caller. We also worry
873 about including files and the ends of include files at this stage. */
876 next_statement (void)
881 gfc_new_block
= NULL
;
883 gfc_current_ns
->old_cl_list
= gfc_current_ns
->cl_list
;
886 gfc_statement_label
= NULL
;
887 gfc_buffer_error (1);
892 gfc_skip_comments ();
900 if (gfc_define_undef_line ())
903 old_locus
= gfc_current_locus
;
905 st
= (gfc_current_form
== FORM_FIXED
) ? next_fixed () : next_free ();
911 gfc_buffer_error (0);
913 if (st
== ST_GET_FCN_CHARACTERISTICS
&& gfc_statement_label
!= NULL
)
915 gfc_free_st_label (gfc_statement_label
);
916 gfc_statement_label
= NULL
;
917 gfc_current_locus
= old_locus
;
921 check_statement_label (st
);
927 /****************************** Parser ***********************************/
929 /* The parser subroutines are of type 'try' that fail if the file ends
932 /* Macros that expand to case-labels for various classes of
933 statements. Start with executable statements that directly do
936 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
937 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
938 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
939 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
940 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
941 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
942 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
943 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
944 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_ERROR_STOP: \
945 case ST_SYNC_ALL: case ST_SYNC_IMAGES: case ST_SYNC_MEMORY
947 /* Statements that mark other executable statements. */
949 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
950 case ST_IF_BLOCK: case ST_BLOCK: \
951 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
952 case ST_OMP_PARALLEL: \
953 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
954 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
955 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
956 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
957 case ST_OMP_TASK: case ST_CRITICAL
959 /* Declaration statements */
961 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
962 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
963 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
966 /* Block end statements. Errors associated with interchanging these
967 are detected in gfc_match_end(). */
969 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
970 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
974 /* Push a new state onto the stack. */
977 push_state (gfc_state_data
*p
, gfc_compile_state new_state
, gfc_symbol
*sym
)
979 p
->state
= new_state
;
980 p
->previous
= gfc_state_stack
;
982 p
->head
= p
->tail
= NULL
;
983 p
->do_variable
= NULL
;
988 /* Pop the current state. */
992 gfc_state_stack
= gfc_state_stack
->previous
;
996 /* Try to find the given state in the state stack. */
999 gfc_find_state (gfc_compile_state state
)
1003 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1004 if (p
->state
== state
)
1007 return (p
== NULL
) ? FAILURE
: SUCCESS
;
1011 /* Starts a new level in the statement list. */
1014 new_level (gfc_code
*q
)
1018 p
= q
->block
= gfc_get_code ();
1020 gfc_state_stack
->head
= gfc_state_stack
->tail
= p
;
1026 /* Add the current new_st code structure and adds it to the current
1027 program unit. As a side-effect, it zeroes the new_st. */
1030 add_statement (void)
1034 p
= gfc_get_code ();
1037 p
->loc
= gfc_current_locus
;
1039 if (gfc_state_stack
->head
== NULL
)
1040 gfc_state_stack
->head
= p
;
1042 gfc_state_stack
->tail
->next
= p
;
1044 while (p
->next
!= NULL
)
1047 gfc_state_stack
->tail
= p
;
1049 gfc_clear_new_st ();
1055 /* Frees everything associated with the current statement. */
1058 undo_new_statement (void)
1060 gfc_free_statements (new_st
.block
);
1061 gfc_free_statements (new_st
.next
);
1062 gfc_free_statement (&new_st
);
1063 gfc_clear_new_st ();
1067 /* If the current statement has a statement label, make sure that it
1068 is allowed to, or should have one. */
1071 check_statement_label (gfc_statement st
)
1075 if (gfc_statement_label
== NULL
)
1077 if (st
== ST_FORMAT
)
1078 gfc_error ("FORMAT statement at %L does not have a statement label",
1085 case ST_END_PROGRAM
:
1086 case ST_END_FUNCTION
:
1087 case ST_END_SUBROUTINE
:
1091 case ST_END_CRITICAL
:
1094 type
= ST_LABEL_TARGET
;
1098 type
= ST_LABEL_FORMAT
;
1101 /* Statement labels are not restricted from appearing on a
1102 particular line. However, there are plenty of situations
1103 where the resulting label can't be referenced. */
1106 type
= ST_LABEL_BAD_TARGET
;
1110 gfc_define_st_label (gfc_statement_label
, type
, &label_locus
);
1112 new_st
.here
= gfc_statement_label
;
1116 /* Figures out what the enclosing program unit is. This will be a
1117 function, subroutine, program, block data or module. */
1120 gfc_enclosing_unit (gfc_compile_state
* result
)
1124 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1125 if (p
->state
== COMP_FUNCTION
|| p
->state
== COMP_SUBROUTINE
1126 || p
->state
== COMP_MODULE
|| p
->state
== COMP_BLOCK_DATA
1127 || p
->state
== COMP_PROGRAM
)
1136 *result
= COMP_PROGRAM
;
1141 /* Translate a statement enum to a string. */
1144 gfc_ascii_statement (gfc_statement st
)
1150 case ST_ARITHMETIC_IF
:
1151 p
= _("arithmetic IF");
1157 p
= _("attribute declaration");
1193 p
= _("data declaration");
1201 case ST_DERIVED_DECL
:
1202 p
= _("derived type declaration");
1219 case ST_END_BLOCK_DATA
:
1220 p
= "END BLOCK DATA";
1222 case ST_END_CRITICAL
:
1234 case ST_END_FUNCTION
:
1240 case ST_END_INTERFACE
:
1241 p
= "END INTERFACE";
1246 case ST_END_PROGRAM
:
1252 case ST_END_SUBROUTINE
:
1253 p
= "END SUBROUTINE";
1264 case ST_EQUIVALENCE
:
1276 case ST_FORALL_BLOCK
: /* Fall through */
1298 case ST_IMPLICIT_NONE
:
1299 p
= "IMPLICIT NONE";
1301 case ST_IMPLIED_ENDDO
:
1302 p
= _("implied END DO");
1328 case ST_MODULE_PROC
:
1329 p
= "MODULE PROCEDURE";
1361 case ST_SYNC_IMAGES
:
1364 case ST_SYNC_MEMORY
:
1376 case ST_WHERE_BLOCK
: /* Fall through */
1387 p
= _("assignment");
1389 case ST_POINTER_ASSIGNMENT
:
1390 p
= _("pointer assignment");
1392 case ST_SELECT_CASE
:
1395 case ST_SELECT_TYPE
:
1410 case ST_STATEMENT_FUNCTION
:
1411 p
= "STATEMENT FUNCTION";
1413 case ST_LABEL_ASSIGNMENT
:
1414 p
= "LABEL ASSIGNMENT";
1417 p
= "ENUM DEFINITION";
1420 p
= "ENUMERATOR DEFINITION";
1428 case ST_OMP_BARRIER
:
1429 p
= "!$OMP BARRIER";
1431 case ST_OMP_CRITICAL
:
1432 p
= "!$OMP CRITICAL";
1437 case ST_OMP_END_CRITICAL
:
1438 p
= "!$OMP END CRITICAL";
1443 case ST_OMP_END_MASTER
:
1444 p
= "!$OMP END MASTER";
1446 case ST_OMP_END_ORDERED
:
1447 p
= "!$OMP END ORDERED";
1449 case ST_OMP_END_PARALLEL
:
1450 p
= "!$OMP END PARALLEL";
1452 case ST_OMP_END_PARALLEL_DO
:
1453 p
= "!$OMP END PARALLEL DO";
1455 case ST_OMP_END_PARALLEL_SECTIONS
:
1456 p
= "!$OMP END PARALLEL SECTIONS";
1458 case ST_OMP_END_PARALLEL_WORKSHARE
:
1459 p
= "!$OMP END PARALLEL WORKSHARE";
1461 case ST_OMP_END_SECTIONS
:
1462 p
= "!$OMP END SECTIONS";
1464 case ST_OMP_END_SINGLE
:
1465 p
= "!$OMP END SINGLE";
1467 case ST_OMP_END_TASK
:
1468 p
= "!$OMP END TASK";
1470 case ST_OMP_END_WORKSHARE
:
1471 p
= "!$OMP END WORKSHARE";
1479 case ST_OMP_ORDERED
:
1480 p
= "!$OMP ORDERED";
1482 case ST_OMP_PARALLEL
:
1483 p
= "!$OMP PARALLEL";
1485 case ST_OMP_PARALLEL_DO
:
1486 p
= "!$OMP PARALLEL DO";
1488 case ST_OMP_PARALLEL_SECTIONS
:
1489 p
= "!$OMP PARALLEL SECTIONS";
1491 case ST_OMP_PARALLEL_WORKSHARE
:
1492 p
= "!$OMP PARALLEL WORKSHARE";
1494 case ST_OMP_SECTIONS
:
1495 p
= "!$OMP SECTIONS";
1497 case ST_OMP_SECTION
:
1498 p
= "!$OMP SECTION";
1506 case ST_OMP_TASKWAIT
:
1507 p
= "!$OMP TASKWAIT";
1509 case ST_OMP_THREADPRIVATE
:
1510 p
= "!$OMP THREADPRIVATE";
1512 case ST_OMP_WORKSHARE
:
1513 p
= "!$OMP WORKSHARE";
1516 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1523 /* Create a symbol for the main program and assign it to ns->proc_name. */
1526 main_program_symbol (gfc_namespace
*ns
, const char *name
)
1528 gfc_symbol
*main_program
;
1529 symbol_attribute attr
;
1531 gfc_get_symbol (name
, ns
, &main_program
);
1532 gfc_clear_attr (&attr
);
1533 attr
.flavor
= FL_PROGRAM
;
1534 attr
.proc
= PROC_UNKNOWN
;
1535 attr
.subroutine
= 1;
1536 attr
.access
= ACCESS_PUBLIC
;
1537 attr
.is_main_program
= 1;
1538 main_program
->attr
= attr
;
1539 main_program
->declared_at
= gfc_current_locus
;
1540 ns
->proc_name
= main_program
;
1541 gfc_commit_symbols ();
1545 /* Do whatever is necessary to accept the last statement. */
1548 accept_statement (gfc_statement st
)
1556 case ST_IMPLICIT_NONE
:
1557 gfc_set_implicit_none ();
1566 gfc_current_ns
->proc_name
= gfc_new_block
;
1569 /* If the statement is the end of a block, lay down a special code
1570 that allows a branch to the end of the block from within the
1571 construct. IF and SELECT are treated differently from DO
1572 (where EXEC_NOP is added inside the loop) for two
1574 1. END DO has a meaning in the sense that after a GOTO to
1575 it, the loop counter must be increased.
1576 2. IF blocks and SELECT blocks can consist of multiple
1577 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
1578 Putting the label before the END IF would make the jump
1579 from, say, the ELSE IF block to the END IF illegal. */
1583 case ST_END_CRITICAL
:
1584 if (gfc_statement_label
!= NULL
)
1586 new_st
.op
= EXEC_END_BLOCK
;
1591 /* The end-of-program unit statements do not get the special
1592 marker and require a statement of some sort if they are a
1595 case ST_END_PROGRAM
:
1596 case ST_END_FUNCTION
:
1597 case ST_END_SUBROUTINE
:
1598 if (gfc_statement_label
!= NULL
)
1600 new_st
.op
= EXEC_RETURN
;
1605 new_st
.op
= EXEC_END_PROCEDURE
;
1621 gfc_commit_symbols ();
1622 gfc_warning_check ();
1623 gfc_clear_new_st ();
1627 /* Undo anything tentative that has been built for the current
1631 reject_statement (void)
1633 /* Revert to the previous charlen chain. */
1634 gfc_free_charlen (gfc_current_ns
->cl_list
, gfc_current_ns
->old_cl_list
);
1635 gfc_current_ns
->cl_list
= gfc_current_ns
->old_cl_list
;
1637 gfc_new_block
= NULL
;
1638 gfc_undo_symbols ();
1639 gfc_clear_warning ();
1640 undo_new_statement ();
1644 /* Generic complaint about an out of order statement. We also do
1645 whatever is necessary to clean up. */
1648 unexpected_statement (gfc_statement st
)
1650 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st
));
1652 reject_statement ();
1656 /* Given the next statement seen by the matcher, make sure that it is
1657 in proper order with the last. This subroutine is initialized by
1658 calling it with an argument of ST_NONE. If there is a problem, we
1659 issue an error and return FAILURE. Otherwise we return SUCCESS.
1661 Individual parsers need to verify that the statements seen are
1662 valid before calling here, i.e., ENTRY statements are not allowed in
1663 INTERFACE blocks. The following diagram is taken from the standard:
1665 +---------------------------------------+
1666 | program subroutine function module |
1667 +---------------------------------------+
1669 +---------------------------------------+
1671 +---------------------------------------+
1673 | +-----------+------------------+
1674 | | parameter | implicit |
1675 | +-----------+------------------+
1676 | format | | derived type |
1677 | entry | parameter | interface |
1678 | | data | specification |
1679 | | | statement func |
1680 | +-----------+------------------+
1681 | | data | executable |
1682 +--------+-----------+------------------+
1684 +---------------------------------------+
1685 | internal module/subprogram |
1686 +---------------------------------------+
1688 +---------------------------------------+
1697 ORDER_IMPLICIT_NONE
,
1705 enum state_order state
;
1706 gfc_statement last_statement
;
1712 verify_st_order (st_state
*p
, gfc_statement st
, bool silent
)
1718 p
->state
= ORDER_START
;
1722 if (p
->state
> ORDER_USE
)
1724 p
->state
= ORDER_USE
;
1728 if (p
->state
> ORDER_IMPORT
)
1730 p
->state
= ORDER_IMPORT
;
1733 case ST_IMPLICIT_NONE
:
1734 if (p
->state
> ORDER_IMPLICIT_NONE
)
1737 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1738 statement disqualifies a USE but not an IMPLICIT NONE.
1739 Duplicate IMPLICIT NONEs are caught when the implicit types
1742 p
->state
= ORDER_IMPLICIT_NONE
;
1746 if (p
->state
> ORDER_IMPLICIT
)
1748 p
->state
= ORDER_IMPLICIT
;
1753 if (p
->state
< ORDER_IMPLICIT_NONE
)
1754 p
->state
= ORDER_IMPLICIT_NONE
;
1758 if (p
->state
>= ORDER_EXEC
)
1760 if (p
->state
< ORDER_IMPLICIT
)
1761 p
->state
= ORDER_IMPLICIT
;
1765 if (p
->state
< ORDER_SPEC
)
1766 p
->state
= ORDER_SPEC
;
1771 case ST_DERIVED_DECL
:
1773 if (p
->state
>= ORDER_EXEC
)
1775 if (p
->state
< ORDER_SPEC
)
1776 p
->state
= ORDER_SPEC
;
1781 if (p
->state
< ORDER_EXEC
)
1782 p
->state
= ORDER_EXEC
;
1786 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1787 gfc_ascii_statement (st
));
1790 /* All is well, record the statement in case we need it next time. */
1791 p
->where
= gfc_current_locus
;
1792 p
->last_statement
= st
;
1797 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1798 gfc_ascii_statement (st
),
1799 gfc_ascii_statement (p
->last_statement
), &p
->where
);
1805 /* Handle an unexpected end of file. This is a show-stopper... */
1807 static void unexpected_eof (void) ATTRIBUTE_NORETURN
;
1810 unexpected_eof (void)
1814 gfc_error ("Unexpected end of file in '%s'", gfc_source_file
);
1816 /* Memory cleanup. Move to "second to last". */
1817 for (p
= gfc_state_stack
; p
&& p
->previous
&& p
->previous
->previous
;
1820 gfc_current_ns
->code
= (p
&& p
->previous
) ? p
->head
: NULL
;
1823 longjmp (eof_buf
, 1);
1827 /* Parse the CONTAINS section of a derived type definition. */
1829 gfc_access gfc_typebound_default_access
;
1832 parse_derived_contains (void)
1835 bool seen_private
= false;
1836 bool seen_comps
= false;
1837 bool error_flag
= false;
1840 gcc_assert (gfc_current_state () == COMP_DERIVED
);
1841 gcc_assert (gfc_current_block ());
1843 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
1845 if (gfc_current_block ()->attr
.sequence
)
1846 gfc_error ("Derived-type '%s' with SEQUENCE must not have a CONTAINS"
1847 " section at %C", gfc_current_block ()->name
);
1848 if (gfc_current_block ()->attr
.is_bind_c
)
1849 gfc_error ("Derived-type '%s' with BIND(C) must not have a CONTAINS"
1850 " section at %C", gfc_current_block ()->name
);
1852 accept_statement (ST_CONTAINS
);
1853 push_state (&s
, COMP_DERIVED_CONTAINS
, NULL
);
1855 gfc_typebound_default_access
= ACCESS_PUBLIC
;
1861 st
= next_statement ();
1869 gfc_error ("Components in TYPE at %C must precede CONTAINS");
1874 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Type-bound"
1875 " procedure at %C") == FAILURE
)
1878 accept_statement (ST_PROCEDURE
);
1883 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: GENERIC binding"
1884 " at %C") == FAILURE
)
1887 accept_statement (ST_GENERIC
);
1892 if (gfc_notify_std (GFC_STD_F2003
,
1893 "Fortran 2003: FINAL procedure declaration"
1894 " at %C") == FAILURE
)
1897 accept_statement (ST_FINAL
);
1905 && (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: Derived type "
1906 "definition at %C with empty CONTAINS "
1907 "section") == FAILURE
))
1910 /* ST_END_TYPE is accepted by parse_derived after return. */
1914 if (gfc_find_state (COMP_MODULE
) == FAILURE
)
1916 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1924 gfc_error ("PRIVATE statement at %C must precede procedure"
1932 gfc_error ("Duplicate PRIVATE statement at %C");
1936 accept_statement (ST_PRIVATE
);
1937 gfc_typebound_default_access
= ACCESS_PRIVATE
;
1938 seen_private
= true;
1942 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
1947 gfc_error ("Already inside a CONTAINS block at %C");
1952 unexpected_statement (st
);
1958 gcc_assert (gfc_current_state () == COMP_DERIVED
);
1964 /* Parse a derived type. */
1967 parse_derived (void)
1969 int compiling_type
, seen_private
, seen_sequence
, seen_component
, error_flag
;
1977 accept_statement (ST_DERIVED_DECL
);
1978 push_state (&s
, COMP_DERIVED
, gfc_new_block
);
1980 gfc_new_block
->component_access
= ACCESS_PUBLIC
;
1987 while (compiling_type
)
1989 st
= next_statement ();
1997 accept_statement (st
);
2002 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2011 && (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Derived type "
2012 "definition at %C without components")
2016 accept_statement (ST_END_TYPE
);
2020 if (gfc_find_state (COMP_MODULE
) == FAILURE
)
2022 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2030 gfc_error ("PRIVATE statement at %C must precede "
2031 "structure components");
2038 gfc_error ("Duplicate PRIVATE statement at %C");
2042 s
.sym
->component_access
= ACCESS_PRIVATE
;
2044 accept_statement (ST_PRIVATE
);
2051 gfc_error ("SEQUENCE statement at %C must precede "
2052 "structure components");
2057 if (gfc_current_block ()->attr
.sequence
)
2058 gfc_warning ("SEQUENCE attribute at %C already specified in "
2063 gfc_error ("Duplicate SEQUENCE statement at %C");
2068 gfc_add_sequence (&gfc_current_block ()->attr
,
2069 gfc_current_block ()->name
, NULL
);
2073 if (gfc_notify_std (GFC_STD_F2003
,
2074 "Fortran 2003: CONTAINS block in derived type"
2075 " definition at %C") == FAILURE
)
2078 accept_statement (ST_CONTAINS
);
2079 if (parse_derived_contains ())
2084 unexpected_statement (st
);
2089 /* need to verify that all fields of the derived type are
2090 * interoperable with C if the type is declared to be bind(c)
2092 sym
= gfc_current_block ();
2093 for (c
= sym
->components
; c
; c
= c
->next
)
2095 /* Look for allocatable components. */
2096 if (c
->attr
.allocatable
2097 || (c
->ts
.type
== BT_CLASS
2098 && c
->ts
.u
.derived
->components
->attr
.allocatable
)
2099 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.alloc_comp
))
2100 sym
->attr
.alloc_comp
= 1;
2102 /* Look for pointer components. */
2104 || (c
->ts
.type
== BT_CLASS
2105 && c
->ts
.u
.derived
->components
->attr
.pointer
)
2106 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.pointer_comp
))
2107 sym
->attr
.pointer_comp
= 1;
2109 /* Look for procedure pointer components. */
2110 if (c
->attr
.proc_pointer
2111 || (c
->ts
.type
== BT_DERIVED
2112 && c
->ts
.u
.derived
->attr
.proc_pointer_comp
))
2113 sym
->attr
.proc_pointer_comp
= 1;
2115 /* Look for private components. */
2116 if (sym
->component_access
== ACCESS_PRIVATE
2117 || c
->attr
.access
== ACCESS_PRIVATE
2118 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.u
.derived
->attr
.private_comp
))
2119 sym
->attr
.private_comp
= 1;
2122 if (!seen_component
)
2123 sym
->attr
.zero_comp
= 1;
2129 /* Parse an ENUM. */
2138 int seen_enumerator
= 0;
2142 push_state (&s
, COMP_ENUM
, gfc_new_block
);
2146 while (compiling_enum
)
2148 st
= next_statement ();
2156 seen_enumerator
= 1;
2157 accept_statement (st
);
2162 if (!seen_enumerator
)
2164 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2167 accept_statement (st
);
2171 gfc_free_enum_history ();
2172 unexpected_statement (st
);
2180 /* Parse an interface. We must be able to deal with the possibility
2181 of recursive interfaces. The parse_spec() subroutine is mutually
2182 recursive with parse_interface(). */
2184 static gfc_statement
parse_spec (gfc_statement
);
2187 parse_interface (void)
2189 gfc_compile_state new_state
= COMP_NONE
, current_state
;
2190 gfc_symbol
*prog_unit
, *sym
;
2191 gfc_interface_info save
;
2192 gfc_state_data s1
, s2
;
2196 accept_statement (ST_INTERFACE
);
2198 current_interface
.ns
= gfc_current_ns
;
2199 save
= current_interface
;
2201 sym
= (current_interface
.type
== INTERFACE_GENERIC
2202 || current_interface
.type
== INTERFACE_USER_OP
)
2203 ? gfc_new_block
: NULL
;
2205 push_state (&s1
, COMP_INTERFACE
, sym
);
2206 current_state
= COMP_NONE
;
2209 gfc_current_ns
= gfc_get_namespace (current_interface
.ns
, 0);
2211 st
= next_statement ();
2219 if (st
== ST_SUBROUTINE
)
2220 new_state
= COMP_SUBROUTINE
;
2221 else if (st
== ST_FUNCTION
)
2222 new_state
= COMP_FUNCTION
;
2223 if (gfc_new_block
->attr
.pointer
)
2225 gfc_new_block
->attr
.pointer
= 0;
2226 gfc_new_block
->attr
.proc_pointer
= 1;
2228 if (gfc_add_explicit_interface (gfc_new_block
, IFSRC_IFBODY
,
2229 gfc_new_block
->formal
, NULL
) == FAILURE
)
2231 reject_statement ();
2232 gfc_free_namespace (gfc_current_ns
);
2238 case ST_MODULE_PROC
: /* The module procedure matcher makes
2239 sure the context is correct. */
2240 accept_statement (st
);
2241 gfc_free_namespace (gfc_current_ns
);
2244 case ST_END_INTERFACE
:
2245 gfc_free_namespace (gfc_current_ns
);
2246 gfc_current_ns
= current_interface
.ns
;
2250 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2251 gfc_ascii_statement (st
));
2252 reject_statement ();
2253 gfc_free_namespace (gfc_current_ns
);
2258 /* Make sure that a generic interface has only subroutines or
2259 functions and that the generic name has the right attribute. */
2260 if (current_interface
.type
== INTERFACE_GENERIC
)
2262 if (current_state
== COMP_NONE
)
2264 if (new_state
== COMP_FUNCTION
)
2265 gfc_add_function (&sym
->attr
, sym
->name
, NULL
);
2266 else if (new_state
== COMP_SUBROUTINE
)
2267 gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
);
2269 current_state
= new_state
;
2273 if (new_state
!= current_state
)
2275 if (new_state
== COMP_SUBROUTINE
)
2276 gfc_error ("SUBROUTINE at %C does not belong in a "
2277 "generic function interface");
2279 if (new_state
== COMP_FUNCTION
)
2280 gfc_error ("FUNCTION at %C does not belong in a "
2281 "generic subroutine interface");
2286 if (current_interface
.type
== INTERFACE_ABSTRACT
)
2288 gfc_add_abstract (&gfc_new_block
->attr
, &gfc_current_locus
);
2289 if (gfc_is_intrinsic_typename (gfc_new_block
->name
))
2290 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
2291 "cannot be the same as an intrinsic type",
2292 gfc_new_block
->name
);
2295 push_state (&s2
, new_state
, gfc_new_block
);
2296 accept_statement (st
);
2297 prog_unit
= gfc_new_block
;
2298 prog_unit
->formal_ns
= gfc_current_ns
;
2299 proc_locus
= gfc_current_locus
;
2302 /* Read data declaration statements. */
2303 st
= parse_spec (ST_NONE
);
2305 /* Since the interface block does not permit an IMPLICIT statement,
2306 the default type for the function or the result must be taken
2307 from the formal namespace. */
2308 if (new_state
== COMP_FUNCTION
)
2310 if (prog_unit
->result
== prog_unit
2311 && prog_unit
->ts
.type
== BT_UNKNOWN
)
2312 gfc_set_default_type (prog_unit
, 1, prog_unit
->formal_ns
);
2313 else if (prog_unit
->result
!= prog_unit
2314 && prog_unit
->result
->ts
.type
== BT_UNKNOWN
)
2315 gfc_set_default_type (prog_unit
->result
, 1,
2316 prog_unit
->formal_ns
);
2319 if (st
!= ST_END_SUBROUTINE
&& st
!= ST_END_FUNCTION
)
2321 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2322 gfc_ascii_statement (st
));
2323 reject_statement ();
2327 /* Add EXTERNAL attribute to function or subroutine. */
2328 if (current_interface
.type
!= INTERFACE_ABSTRACT
&& !prog_unit
->attr
.dummy
)
2329 gfc_add_external (&prog_unit
->attr
, &gfc_current_locus
);
2331 current_interface
= save
;
2332 gfc_add_interface (prog_unit
);
2335 if (current_interface
.ns
2336 && current_interface
.ns
->proc_name
2337 && strcmp (current_interface
.ns
->proc_name
->name
,
2338 prog_unit
->name
) == 0)
2339 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2340 "enclosing procedure", prog_unit
->name
, &proc_locus
);
2349 /* Associate function characteristics by going back to the function
2350 declaration and rematching the prefix. */
2353 match_deferred_characteristics (gfc_typespec
* ts
)
2356 match m
= MATCH_ERROR
;
2357 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2359 loc
= gfc_current_locus
;
2361 gfc_current_locus
= gfc_current_block ()->declared_at
;
2364 gfc_buffer_error (1);
2365 m
= gfc_match_prefix (ts
);
2366 gfc_buffer_error (0);
2368 if (ts
->type
== BT_DERIVED
)
2376 /* Only permit one go at the characteristic association. */
2380 /* Set the function locus correctly. If we have not found the
2381 function name, there is an error. */
2383 && gfc_match ("function% %n", name
) == MATCH_YES
2384 && strcmp (name
, gfc_current_block ()->name
) == 0)
2386 gfc_current_block ()->declared_at
= gfc_current_locus
;
2387 gfc_commit_symbols ();
2392 gfc_current_locus
=loc
;
2397 /* Check specification-expressions in the function result of the currently
2398 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2399 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2400 scope are not yet parsed so this has to be delayed up to parse_spec. */
2403 check_function_result_typed (void)
2405 gfc_typespec
* ts
= &gfc_current_ns
->proc_name
->result
->ts
;
2407 gcc_assert (gfc_current_state () == COMP_FUNCTION
);
2408 gcc_assert (ts
->type
!= BT_UNKNOWN
);
2410 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2411 /* TODO: Extend when KIND type parameters are implemented. */
2412 if (ts
->type
== BT_CHARACTER
&& ts
->u
.cl
&& ts
->u
.cl
->length
)
2413 gfc_expr_check_typed (ts
->u
.cl
->length
, gfc_current_ns
, true);
2417 /* Parse a set of specification statements. Returns the statement
2418 that doesn't fit. */
2420 static gfc_statement
2421 parse_spec (gfc_statement st
)
2424 bool function_result_typed
= false;
2425 bool bad_characteristic
= false;
2428 verify_st_order (&ss
, ST_NONE
, false);
2430 st
= next_statement ();
2432 /* If we are not inside a function or don't have a result specified so far,
2433 do nothing special about it. */
2434 if (gfc_current_state () != COMP_FUNCTION
)
2435 function_result_typed
= true;
2438 gfc_symbol
* proc
= gfc_current_ns
->proc_name
;
2441 if (proc
->result
->ts
.type
== BT_UNKNOWN
)
2442 function_result_typed
= true;
2447 /* If we're inside a BLOCK construct, some statements are disallowed.
2448 Check this here. Attribute declaration statements like INTENT, OPTIONAL
2449 or VALUE are also disallowed, but they don't have a particular ST_*
2450 key so we have to check for them individually in their matcher routine. */
2451 if (gfc_current_state () == COMP_BLOCK
)
2455 case ST_IMPLICIT_NONE
:
2458 case ST_EQUIVALENCE
:
2459 case ST_STATEMENT_FUNCTION
:
2460 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
2461 gfc_ascii_statement (st
));
2468 /* If we find a statement that can not be followed by an IMPLICIT statement
2469 (and thus we can expect to see none any further), type the function result
2470 if it has not yet been typed. Be careful not to give the END statement
2471 to verify_st_order! */
2472 if (!function_result_typed
&& st
!= ST_GET_FCN_CHARACTERISTICS
)
2474 bool verify_now
= false;
2476 if (st
== ST_END_FUNCTION
|| st
== ST_CONTAINS
)
2481 verify_st_order (&dummyss
, ST_NONE
, false);
2482 verify_st_order (&dummyss
, st
, false);
2484 if (verify_st_order (&dummyss
, ST_IMPLICIT
, true) == FAILURE
)
2490 check_function_result_typed ();
2491 function_result_typed
= true;
2500 case ST_IMPLICIT_NONE
:
2502 if (!function_result_typed
)
2504 check_function_result_typed ();
2505 function_result_typed
= true;
2511 case ST_DATA
: /* Not allowed in interfaces */
2512 if (gfc_current_state () == COMP_INTERFACE
)
2522 case ST_DERIVED_DECL
:
2525 if (verify_st_order (&ss
, st
, false) == FAILURE
)
2527 reject_statement ();
2528 st
= next_statement ();
2538 case ST_DERIVED_DECL
:
2544 if (gfc_current_state () != COMP_MODULE
)
2546 gfc_error ("%s statement must appear in a MODULE",
2547 gfc_ascii_statement (st
));
2551 if (gfc_current_ns
->default_access
!= ACCESS_UNKNOWN
)
2553 gfc_error ("%s statement at %C follows another accessibility "
2554 "specification", gfc_ascii_statement (st
));
2558 gfc_current_ns
->default_access
= (st
== ST_PUBLIC
)
2559 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
2563 case ST_STATEMENT_FUNCTION
:
2564 if (gfc_current_state () == COMP_MODULE
)
2566 unexpected_statement (st
);
2574 accept_statement (st
);
2575 st
= next_statement ();
2579 accept_statement (st
);
2581 st
= next_statement ();
2584 case ST_GET_FCN_CHARACTERISTICS
:
2585 /* This statement triggers the association of a function's result
2587 ts
= &gfc_current_block ()->result
->ts
;
2588 if (match_deferred_characteristics (ts
) != MATCH_YES
)
2589 bad_characteristic
= true;
2591 st
= next_statement ();
2598 /* If match_deferred_characteristics failed, then there is an error. */
2599 if (bad_characteristic
)
2601 ts
= &gfc_current_block ()->result
->ts
;
2602 if (ts
->type
!= BT_DERIVED
)
2603 gfc_error ("Bad kind expression for function '%s' at %L",
2604 gfc_current_block ()->name
,
2605 &gfc_current_block ()->declared_at
);
2607 gfc_error ("The type for function '%s' at %L is not accessible",
2608 gfc_current_block ()->name
,
2609 &gfc_current_block ()->declared_at
);
2611 gfc_current_block ()->ts
.kind
= 0;
2612 /* Keep the derived type; if it's bad, it will be discovered later. */
2613 if (!(ts
->type
== BT_DERIVED
&& ts
->u
.derived
))
2614 ts
->type
= BT_UNKNOWN
;
2621 /* Parse a WHERE block, (not a simple WHERE statement). */
2624 parse_where_block (void)
2626 int seen_empty_else
;
2631 accept_statement (ST_WHERE_BLOCK
);
2632 top
= gfc_state_stack
->tail
;
2634 push_state (&s
, COMP_WHERE
, gfc_new_block
);
2636 d
= add_statement ();
2637 d
->expr1
= top
->expr1
;
2643 seen_empty_else
= 0;
2647 st
= next_statement ();
2653 case ST_WHERE_BLOCK
:
2654 parse_where_block ();
2659 accept_statement (st
);
2663 if (seen_empty_else
)
2665 gfc_error ("ELSEWHERE statement at %C follows previous "
2666 "unmasked ELSEWHERE");
2670 if (new_st
.expr1
== NULL
)
2671 seen_empty_else
= 1;
2673 d
= new_level (gfc_state_stack
->head
);
2675 d
->expr1
= new_st
.expr1
;
2677 accept_statement (st
);
2682 accept_statement (st
);
2686 gfc_error ("Unexpected %s statement in WHERE block at %C",
2687 gfc_ascii_statement (st
));
2688 reject_statement ();
2692 while (st
!= ST_END_WHERE
);
2698 /* Parse a FORALL block (not a simple FORALL statement). */
2701 parse_forall_block (void)
2707 accept_statement (ST_FORALL_BLOCK
);
2708 top
= gfc_state_stack
->tail
;
2710 push_state (&s
, COMP_FORALL
, gfc_new_block
);
2712 d
= add_statement ();
2713 d
->op
= EXEC_FORALL
;
2718 st
= next_statement ();
2723 case ST_POINTER_ASSIGNMENT
:
2726 accept_statement (st
);
2729 case ST_WHERE_BLOCK
:
2730 parse_where_block ();
2733 case ST_FORALL_BLOCK
:
2734 parse_forall_block ();
2738 accept_statement (st
);
2745 gfc_error ("Unexpected %s statement in FORALL block at %C",
2746 gfc_ascii_statement (st
));
2748 reject_statement ();
2752 while (st
!= ST_END_FORALL
);
2758 static gfc_statement
parse_executable (gfc_statement
);
2760 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2763 parse_if_block (void)
2772 accept_statement (ST_IF_BLOCK
);
2774 top
= gfc_state_stack
->tail
;
2775 push_state (&s
, COMP_IF
, gfc_new_block
);
2777 new_st
.op
= EXEC_IF
;
2778 d
= add_statement ();
2780 d
->expr1
= top
->expr1
;
2786 st
= parse_executable (ST_NONE
);
2796 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2797 "statement at %L", &else_locus
);
2799 reject_statement ();
2803 d
= new_level (gfc_state_stack
->head
);
2805 d
->expr1
= new_st
.expr1
;
2807 accept_statement (st
);
2814 gfc_error ("Duplicate ELSE statements at %L and %C",
2816 reject_statement ();
2821 else_locus
= gfc_current_locus
;
2823 d
= new_level (gfc_state_stack
->head
);
2826 accept_statement (st
);
2834 unexpected_statement (st
);
2838 while (st
!= ST_ENDIF
);
2841 accept_statement (st
);
2845 /* Parse a SELECT block. */
2848 parse_select_block (void)
2854 accept_statement (ST_SELECT_CASE
);
2856 cp
= gfc_state_stack
->tail
;
2857 push_state (&s
, COMP_SELECT
, gfc_new_block
);
2859 /* Make sure that the next statement is a CASE or END SELECT. */
2862 st
= next_statement ();
2865 if (st
== ST_END_SELECT
)
2867 /* Empty SELECT CASE is OK. */
2868 accept_statement (st
);
2875 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2878 reject_statement ();
2881 /* At this point, we're got a nonempty select block. */
2882 cp
= new_level (cp
);
2885 accept_statement (st
);
2889 st
= parse_executable (ST_NONE
);
2896 cp
= new_level (gfc_state_stack
->head
);
2898 gfc_clear_new_st ();
2900 accept_statement (st
);
2906 /* Can't have an executable statement because of
2907 parse_executable(). */
2909 unexpected_statement (st
);
2913 while (st
!= ST_END_SELECT
);
2916 accept_statement (st
);
2920 /* Pop the current selector from the SELECT TYPE stack. */
2923 select_type_pop (void)
2925 gfc_select_type_stack
*old
= select_type_stack
;
2926 select_type_stack
= old
->prev
;
2931 /* Parse a SELECT TYPE construct (F03:R821). */
2934 parse_select_type_block (void)
2940 accept_statement (ST_SELECT_TYPE
);
2942 cp
= gfc_state_stack
->tail
;
2943 push_state (&s
, COMP_SELECT_TYPE
, gfc_new_block
);
2945 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
2949 st
= next_statement ();
2952 if (st
== ST_END_SELECT
)
2953 /* Empty SELECT CASE is OK. */
2955 if (st
== ST_TYPE_IS
|| st
== ST_CLASS_IS
)
2958 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
2959 "following SELECT TYPE at %C");
2961 reject_statement ();
2964 /* At this point, we're got a nonempty select block. */
2965 cp
= new_level (cp
);
2968 accept_statement (st
);
2972 st
= parse_executable (ST_NONE
);
2980 cp
= new_level (gfc_state_stack
->head
);
2982 gfc_clear_new_st ();
2984 accept_statement (st
);
2990 /* Can't have an executable statement because of
2991 parse_executable(). */
2993 unexpected_statement (st
);
2997 while (st
!= ST_END_SELECT
);
3001 accept_statement (st
);
3002 gfc_current_ns
= gfc_current_ns
->parent
;
3007 /* Given a symbol, make sure it is not an iteration variable for a DO
3008 statement. This subroutine is called when the symbol is seen in a
3009 context that causes it to become redefined. If the symbol is an
3010 iterator, we generate an error message and return nonzero. */
3013 gfc_check_do_variable (gfc_symtree
*st
)
3017 for (s
=gfc_state_stack
; s
; s
= s
->previous
)
3018 if (s
->do_variable
== st
)
3020 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
3021 "loop beginning at %L", st
->name
, &s
->head
->loc
);
3029 /* Checks to see if the current statement label closes an enddo.
3030 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3031 an error) if it incorrectly closes an ENDDO. */
3034 check_do_closure (void)
3038 if (gfc_statement_label
== NULL
)
3041 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
3042 if (p
->state
== COMP_DO
)
3046 return 0; /* No loops to close */
3048 if (p
->ext
.end_do_label
== gfc_statement_label
)
3050 if (p
== gfc_state_stack
)
3053 gfc_error ("End of nonblock DO statement at %C is within another block");
3057 /* At this point, the label doesn't terminate the innermost loop.
3058 Make sure it doesn't terminate another one. */
3059 for (; p
; p
= p
->previous
)
3060 if (p
->state
== COMP_DO
&& p
->ext
.end_do_label
== gfc_statement_label
)
3062 gfc_error ("End of nonblock DO statement at %C is interwoven "
3063 "with another DO loop");
3071 /* Parse a series of contained program units. */
3073 static void parse_progunit (gfc_statement
);
3076 /* Parse a CRITICAL block. */
3079 parse_critical_block (void)
3085 s
.ext
.end_do_label
= new_st
.label1
;
3087 accept_statement (ST_CRITICAL
);
3088 top
= gfc_state_stack
->tail
;
3090 push_state (&s
, COMP_CRITICAL
, gfc_new_block
);
3092 d
= add_statement ();
3093 d
->op
= EXEC_CRITICAL
;
3098 st
= parse_executable (ST_NONE
);
3106 case ST_END_CRITICAL
:
3107 if (s
.ext
.end_do_label
!= NULL
3108 && s
.ext
.end_do_label
!= gfc_statement_label
)
3109 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3110 "match CRITIAL label");
3112 if (gfc_statement_label
!= NULL
)
3114 new_st
.op
= EXEC_NOP
;
3120 unexpected_statement (st
);
3124 while (st
!= ST_END_CRITICAL
);
3127 accept_statement (st
);
3131 /* Set up the local namespace for a BLOCK construct. */
3134 gfc_build_block_ns (gfc_namespace
*parent_ns
)
3136 gfc_namespace
* my_ns
;
3138 my_ns
= gfc_get_namespace (parent_ns
, 1);
3139 my_ns
->construct_entities
= 1;
3141 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3142 code generation (so it must not be NULL).
3143 We set its recursive argument if our container procedure is recursive, so
3144 that local variables are accordingly placed on the stack when it
3145 will be necessary. */
3147 my_ns
->proc_name
= gfc_new_block
;
3152 gfc_get_symbol ("block@", my_ns
, &my_ns
->proc_name
);
3153 t
= gfc_add_flavor (&my_ns
->proc_name
->attr
, FL_LABEL
,
3154 my_ns
->proc_name
->name
, NULL
);
3155 gcc_assert (t
== SUCCESS
);
3158 if (parent_ns
->proc_name
)
3159 my_ns
->proc_name
->attr
.recursive
= parent_ns
->proc_name
->attr
.recursive
;
3165 /* Parse a BLOCK construct. */
3168 parse_block_construct (void)
3170 gfc_namespace
* my_ns
;
3173 gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BLOCK construct at %C");
3175 my_ns
= gfc_build_block_ns (gfc_current_ns
);
3177 new_st
.op
= EXEC_BLOCK
;
3178 new_st
.ext
.ns
= my_ns
;
3179 accept_statement (ST_BLOCK
);
3181 push_state (&s
, COMP_BLOCK
, my_ns
->proc_name
);
3182 gfc_current_ns
= my_ns
;
3184 parse_progunit (ST_NONE
);
3186 gfc_current_ns
= gfc_current_ns
->parent
;
3191 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
3192 handled inside of parse_executable(), because they aren't really
3196 parse_do_block (void)
3203 s
.ext
.end_do_label
= new_st
.label1
;
3205 if (new_st
.ext
.iterator
!= NULL
)
3206 stree
= new_st
.ext
.iterator
->var
->symtree
;
3210 accept_statement (ST_DO
);
3212 top
= gfc_state_stack
->tail
;
3213 push_state (&s
, COMP_DO
, gfc_new_block
);
3215 s
.do_variable
= stree
;
3217 top
->block
= new_level (top
);
3218 top
->block
->op
= EXEC_DO
;
3221 st
= parse_executable (ST_NONE
);
3229 if (s
.ext
.end_do_label
!= NULL
3230 && s
.ext
.end_do_label
!= gfc_statement_label
)
3231 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
3234 if (gfc_statement_label
!= NULL
)
3236 new_st
.op
= EXEC_NOP
;
3241 case ST_IMPLIED_ENDDO
:
3242 /* If the do-stmt of this DO construct has a do-construct-name,
3243 the corresponding end-do must be an end-do-stmt (with a matching
3244 name, but in that case we must have seen ST_ENDDO first).
3245 We only complain about this in pedantic mode. */
3246 if (gfc_current_block () != NULL
)
3247 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
3248 &gfc_current_block()->declared_at
);
3253 unexpected_statement (st
);
3258 accept_statement (st
);
3262 /* Parse the statements of OpenMP do/parallel do. */
3264 static gfc_statement
3265 parse_omp_do (gfc_statement omp_st
)
3271 accept_statement (omp_st
);
3273 cp
= gfc_state_stack
->tail
;
3274 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3275 np
= new_level (cp
);
3281 st
= next_statement ();
3284 else if (st
== ST_DO
)
3287 unexpected_statement (st
);
3291 if (gfc_statement_label
!= NULL
3292 && gfc_state_stack
->previous
!= NULL
3293 && gfc_state_stack
->previous
->state
== COMP_DO
3294 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
3302 there should be no !$OMP END DO. */
3304 return ST_IMPLIED_ENDDO
;
3307 check_do_closure ();
3310 st
= next_statement ();
3311 if (st
== (omp_st
== ST_OMP_DO
? ST_OMP_END_DO
: ST_OMP_END_PARALLEL_DO
))
3313 if (new_st
.op
== EXEC_OMP_END_NOWAIT
)
3314 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
3316 gcc_assert (new_st
.op
== EXEC_NOP
);
3317 gfc_clear_new_st ();
3318 gfc_commit_symbols ();
3319 gfc_warning_check ();
3320 st
= next_statement ();
3326 /* Parse the statements of OpenMP atomic directive. */
3329 parse_omp_atomic (void)
3335 accept_statement (ST_OMP_ATOMIC
);
3337 cp
= gfc_state_stack
->tail
;
3338 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3339 np
= new_level (cp
);
3345 st
= next_statement ();
3348 else if (st
== ST_ASSIGNMENT
)
3351 unexpected_statement (st
);
3354 accept_statement (st
);
3360 /* Parse the statements of an OpenMP structured block. */
3363 parse_omp_structured_block (gfc_statement omp_st
, bool workshare_stmts_only
)
3365 gfc_statement st
, omp_end_st
;
3369 accept_statement (omp_st
);
3371 cp
= gfc_state_stack
->tail
;
3372 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3373 np
= new_level (cp
);
3379 case ST_OMP_PARALLEL
:
3380 omp_end_st
= ST_OMP_END_PARALLEL
;
3382 case ST_OMP_PARALLEL_SECTIONS
:
3383 omp_end_st
= ST_OMP_END_PARALLEL_SECTIONS
;
3385 case ST_OMP_SECTIONS
:
3386 omp_end_st
= ST_OMP_END_SECTIONS
;
3388 case ST_OMP_ORDERED
:
3389 omp_end_st
= ST_OMP_END_ORDERED
;
3391 case ST_OMP_CRITICAL
:
3392 omp_end_st
= ST_OMP_END_CRITICAL
;
3395 omp_end_st
= ST_OMP_END_MASTER
;
3398 omp_end_st
= ST_OMP_END_SINGLE
;
3401 omp_end_st
= ST_OMP_END_TASK
;
3403 case ST_OMP_WORKSHARE
:
3404 omp_end_st
= ST_OMP_END_WORKSHARE
;
3406 case ST_OMP_PARALLEL_WORKSHARE
:
3407 omp_end_st
= ST_OMP_END_PARALLEL_WORKSHARE
;
3415 if (workshare_stmts_only
)
3417 /* Inside of !$omp workshare, only
3420 where statements and constructs
3421 forall statements and constructs
3425 are allowed. For !$omp critical these
3426 restrictions apply recursively. */
3429 st
= next_statement ();
3440 accept_statement (st
);
3443 case ST_WHERE_BLOCK
:
3444 parse_where_block ();
3447 case ST_FORALL_BLOCK
:
3448 parse_forall_block ();
3451 case ST_OMP_PARALLEL
:
3452 case ST_OMP_PARALLEL_SECTIONS
:
3453 parse_omp_structured_block (st
, false);
3456 case ST_OMP_PARALLEL_WORKSHARE
:
3457 case ST_OMP_CRITICAL
:
3458 parse_omp_structured_block (st
, true);
3461 case ST_OMP_PARALLEL_DO
:
3462 st
= parse_omp_do (st
);
3466 parse_omp_atomic ();
3477 st
= next_statement ();
3481 st
= parse_executable (ST_NONE
);
3484 else if (st
== ST_OMP_SECTION
3485 && (omp_st
== ST_OMP_SECTIONS
3486 || omp_st
== ST_OMP_PARALLEL_SECTIONS
))
3488 np
= new_level (np
);
3492 else if (st
!= omp_end_st
)
3493 unexpected_statement (st
);
3495 while (st
!= omp_end_st
);
3499 case EXEC_OMP_END_NOWAIT
:
3500 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
3502 case EXEC_OMP_CRITICAL
:
3503 if (((cp
->ext
.omp_name
== NULL
) ^ (new_st
.ext
.omp_name
== NULL
))
3504 || (new_st
.ext
.omp_name
!= NULL
3505 && strcmp (cp
->ext
.omp_name
, new_st
.ext
.omp_name
) != 0))
3506 gfc_error ("Name after !$omp critical and !$omp end critical does "
3508 gfc_free (CONST_CAST (char *, new_st
.ext
.omp_name
));
3510 case EXEC_OMP_END_SINGLE
:
3511 cp
->ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
]
3512 = new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
];
3513 new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
] = NULL
;
3514 gfc_free_omp_clauses (new_st
.ext
.omp_clauses
);
3522 gfc_clear_new_st ();
3523 gfc_commit_symbols ();
3524 gfc_warning_check ();
3529 /* Accept a series of executable statements. We return the first
3530 statement that doesn't fit to the caller. Any block statements are
3531 passed on to the correct handler, which usually passes the buck
3534 static gfc_statement
3535 parse_executable (gfc_statement st
)
3540 st
= next_statement ();
3544 close_flag
= check_do_closure ();
3549 case ST_END_PROGRAM
:
3552 case ST_END_FUNCTION
:
3557 case ST_END_SUBROUTINE
:
3564 case ST_SELECT_CASE
:
3565 gfc_error ("%s statement at %C cannot terminate a non-block "
3566 "DO loop", gfc_ascii_statement (st
));
3582 accept_statement (st
);
3583 if (close_flag
== 1)
3584 return ST_IMPLIED_ENDDO
;
3588 parse_block_construct ();
3595 case ST_SELECT_CASE
:
3596 parse_select_block ();
3599 case ST_SELECT_TYPE
:
3600 parse_select_type_block();
3605 if (check_do_closure () == 1)
3606 return ST_IMPLIED_ENDDO
;
3610 parse_critical_block ();
3613 case ST_WHERE_BLOCK
:
3614 parse_where_block ();
3617 case ST_FORALL_BLOCK
:
3618 parse_forall_block ();
3621 case ST_OMP_PARALLEL
:
3622 case ST_OMP_PARALLEL_SECTIONS
:
3623 case ST_OMP_SECTIONS
:
3624 case ST_OMP_ORDERED
:
3625 case ST_OMP_CRITICAL
:
3629 parse_omp_structured_block (st
, false);
3632 case ST_OMP_WORKSHARE
:
3633 case ST_OMP_PARALLEL_WORKSHARE
:
3634 parse_omp_structured_block (st
, true);
3638 case ST_OMP_PARALLEL_DO
:
3639 st
= parse_omp_do (st
);
3640 if (st
== ST_IMPLIED_ENDDO
)
3645 parse_omp_atomic ();
3652 st
= next_statement ();
3657 /* Fix the symbols for sibling functions. These are incorrectly added to
3658 the child namespace as the parser didn't know about this procedure. */
3661 gfc_fixup_sibling_symbols (gfc_symbol
*sym
, gfc_namespace
*siblings
)
3665 gfc_symbol
*old_sym
;
3667 sym
->attr
.referenced
= 1;
3668 for (ns
= siblings
; ns
; ns
= ns
->sibling
)
3670 st
= gfc_find_symtree (ns
->sym_root
, sym
->name
);
3672 if (!st
|| (st
->n
.sym
->attr
.dummy
&& ns
== st
->n
.sym
->ns
))
3673 goto fixup_contained
;
3675 old_sym
= st
->n
.sym
;
3676 if (old_sym
->ns
== ns
3677 && !old_sym
->attr
.contained
3679 /* By 14.6.1.3, host association should be excluded
3680 for the following. */
3681 && !(old_sym
->attr
.external
3682 || (old_sym
->ts
.type
!= BT_UNKNOWN
3683 && !old_sym
->attr
.implicit_type
)
3684 || old_sym
->attr
.flavor
== FL_PARAMETER
3685 || old_sym
->attr
.in_common
3686 || old_sym
->attr
.in_equivalence
3687 || old_sym
->attr
.data
3688 || old_sym
->attr
.dummy
3689 || old_sym
->attr
.result
3690 || old_sym
->attr
.dimension
3691 || old_sym
->attr
.allocatable
3692 || old_sym
->attr
.intrinsic
3693 || old_sym
->attr
.generic
3694 || old_sym
->attr
.flavor
== FL_NAMELIST
3695 || old_sym
->attr
.proc
== PROC_ST_FUNCTION
))
3697 /* Replace it with the symbol from the parent namespace. */
3701 /* Free the old (local) symbol. */
3703 if (old_sym
->refs
== 0)
3704 gfc_free_symbol (old_sym
);
3708 /* Do the same for any contained procedures. */
3709 gfc_fixup_sibling_symbols (sym
, ns
->contained
);
3714 parse_contained (int module
)
3716 gfc_namespace
*ns
, *parent_ns
, *tmp
;
3717 gfc_state_data s1
, s2
;
3721 int contains_statements
= 0;
3724 push_state (&s1
, COMP_CONTAINS
, NULL
);
3725 parent_ns
= gfc_current_ns
;
3729 gfc_current_ns
= gfc_get_namespace (parent_ns
, 1);
3731 gfc_current_ns
->sibling
= parent_ns
->contained
;
3732 parent_ns
->contained
= gfc_current_ns
;
3735 /* Process the next available statement. We come here if we got an error
3736 and rejected the last statement. */
3737 st
= next_statement ();
3746 contains_statements
= 1;
3747 accept_statement (st
);
3750 (st
== ST_FUNCTION
) ? COMP_FUNCTION
: COMP_SUBROUTINE
,
3753 /* For internal procedures, create/update the symbol in the
3754 parent namespace. */
3758 if (gfc_get_symbol (gfc_new_block
->name
, parent_ns
, &sym
))
3759 gfc_error ("Contained procedure '%s' at %C is already "
3760 "ambiguous", gfc_new_block
->name
);
3763 if (gfc_add_procedure (&sym
->attr
, PROC_INTERNAL
, sym
->name
,
3764 &gfc_new_block
->declared_at
) ==
3767 if (st
== ST_FUNCTION
)
3768 gfc_add_function (&sym
->attr
, sym
->name
,
3769 &gfc_new_block
->declared_at
);
3771 gfc_add_subroutine (&sym
->attr
, sym
->name
,
3772 &gfc_new_block
->declared_at
);
3776 gfc_commit_symbols ();
3779 sym
= gfc_new_block
;
3781 /* Mark this as a contained function, so it isn't replaced
3782 by other module functions. */
3783 sym
->attr
.contained
= 1;
3784 sym
->attr
.referenced
= 1;
3786 parse_progunit (ST_NONE
);
3788 /* Fix up any sibling functions that refer to this one. */
3789 gfc_fixup_sibling_symbols (sym
, gfc_current_ns
);
3790 /* Or refer to any of its alternate entry points. */
3791 for (el
= gfc_current_ns
->entries
; el
; el
= el
->next
)
3792 gfc_fixup_sibling_symbols (el
->sym
, gfc_current_ns
);
3794 gfc_current_ns
->code
= s2
.head
;
3795 gfc_current_ns
= parent_ns
;
3800 /* These statements are associated with the end of the host unit. */
3801 case ST_END_FUNCTION
:
3803 case ST_END_PROGRAM
:
3804 case ST_END_SUBROUTINE
:
3805 accept_statement (st
);
3809 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
3810 gfc_ascii_statement (st
));
3811 reject_statement ();
3817 while (st
!= ST_END_FUNCTION
&& st
!= ST_END_SUBROUTINE
3818 && st
!= ST_END_MODULE
&& st
!= ST_END_PROGRAM
);
3820 /* The first namespace in the list is guaranteed to not have
3821 anything (worthwhile) in it. */
3822 tmp
= gfc_current_ns
;
3823 gfc_current_ns
= parent_ns
;
3824 if (seen_error
&& tmp
->refs
> 1)
3825 gfc_free_namespace (tmp
);
3827 ns
= gfc_current_ns
->contained
;
3828 gfc_current_ns
->contained
= ns
->sibling
;
3829 gfc_free_namespace (ns
);
3832 if (!contains_statements
)
3833 gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: CONTAINS statement without "
3834 "FUNCTION or SUBROUTINE statement at %C");
3838 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
3841 parse_progunit (gfc_statement st
)
3846 st
= parse_spec (st
);
3853 /* This is not allowed within BLOCK! */
3854 if (gfc_current_state () != COMP_BLOCK
)
3859 accept_statement (st
);
3866 if (gfc_current_state () == COMP_FUNCTION
)
3867 gfc_check_function_type (gfc_current_ns
);
3872 st
= parse_executable (st
);
3880 /* This is not allowed within BLOCK! */
3881 if (gfc_current_state () != COMP_BLOCK
)
3886 accept_statement (st
);
3893 unexpected_statement (st
);
3894 reject_statement ();
3895 st
= next_statement ();
3901 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
3902 if (p
->state
== COMP_CONTAINS
)
3905 if (gfc_find_state (COMP_MODULE
) == SUCCESS
)
3910 gfc_error ("CONTAINS statement at %C is already in a contained "
3912 st
= next_statement ();
3916 parse_contained (0);
3919 gfc_current_ns
->code
= gfc_state_stack
->head
;
3923 /* Come here to complain about a global symbol already in use as
3927 gfc_global_used (gfc_gsymbol
*sym
, locus
*where
)
3932 where
= &gfc_current_locus
;
3942 case GSYM_SUBROUTINE
:
3943 name
= "SUBROUTINE";
3948 case GSYM_BLOCK_DATA
:
3949 name
= "BLOCK DATA";
3955 gfc_internal_error ("gfc_global_used(): Bad type");
3959 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
3960 sym
->name
, where
, name
, &sym
->where
);
3964 /* Parse a block data program unit. */
3967 parse_block_data (void)
3970 static locus blank_locus
;
3971 static int blank_block
=0;
3974 gfc_current_ns
->proc_name
= gfc_new_block
;
3975 gfc_current_ns
->is_block_data
= 1;
3977 if (gfc_new_block
== NULL
)
3980 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3981 "prior BLOCK DATA at %L", &blank_locus
);
3985 blank_locus
= gfc_current_locus
;
3990 s
= gfc_get_gsymbol (gfc_new_block
->name
);
3992 || (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_BLOCK_DATA
))
3993 gfc_global_used(s
, NULL
);
3996 s
->type
= GSYM_BLOCK_DATA
;
3997 s
->where
= gfc_current_locus
;
4002 st
= parse_spec (ST_NONE
);
4004 while (st
!= ST_END_BLOCK_DATA
)
4006 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
4007 gfc_ascii_statement (st
));
4008 reject_statement ();
4009 st
= next_statement ();
4014 /* Parse a module subprogram. */
4022 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4023 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_MODULE
))
4024 gfc_global_used(s
, NULL
);
4027 s
->type
= GSYM_MODULE
;
4028 s
->where
= gfc_current_locus
;
4032 st
= parse_spec (ST_NONE
);
4041 parse_contained (1);
4045 accept_statement (st
);
4049 gfc_error ("Unexpected %s statement in MODULE at %C",
4050 gfc_ascii_statement (st
));
4052 reject_statement ();
4053 st
= next_statement ();
4057 s
->ns
= gfc_current_ns
;
4061 /* Add a procedure name to the global symbol table. */
4064 add_global_procedure (int sub
)
4068 s
= gfc_get_gsymbol(gfc_new_block
->name
);
4071 || (s
->type
!= GSYM_UNKNOWN
4072 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
4073 gfc_global_used(s
, NULL
);
4076 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
4077 s
->where
= gfc_current_locus
;
4079 s
->ns
= gfc_current_ns
;
4084 /* Add a program to the global symbol table. */
4087 add_global_program (void)
4091 if (gfc_new_block
== NULL
)
4093 s
= gfc_get_gsymbol (gfc_new_block
->name
);
4095 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_PROGRAM
))
4096 gfc_global_used(s
, NULL
);
4099 s
->type
= GSYM_PROGRAM
;
4100 s
->where
= gfc_current_locus
;
4102 s
->ns
= gfc_current_ns
;
4107 /* Resolve all the program units when whole file scope option
4110 resolve_all_program_units (gfc_namespace
*gfc_global_ns_list
)
4112 gfc_free_dt_list ();
4113 gfc_current_ns
= gfc_global_ns_list
;
4114 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4116 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4117 gfc_resolve (gfc_current_ns
);
4118 gfc_current_ns
->derived_types
= gfc_derived_types
;
4119 gfc_derived_types
= NULL
;
4125 clean_up_modules (gfc_gsymbol
*gsym
)
4130 clean_up_modules (gsym
->left
);
4131 clean_up_modules (gsym
->right
);
4133 if (gsym
->type
!= GSYM_MODULE
|| !gsym
->ns
)
4136 gfc_current_ns
= gsym
->ns
;
4137 gfc_derived_types
= gfc_current_ns
->derived_types
;
4144 /* Translate all the program units when whole file scope option
4145 is active. This could be in a different order to resolution if
4146 there are forward references in the file. */
4148 translate_all_program_units (gfc_namespace
*gfc_global_ns_list
)
4152 gfc_current_ns
= gfc_global_ns_list
;
4153 gfc_get_errors (NULL
, &errors
);
4155 for (; !errors
&& gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4157 gfc_current_locus
= gfc_current_ns
->proc_name
->declared_at
;
4158 gfc_derived_types
= gfc_current_ns
->derived_types
;
4159 gfc_generate_code (gfc_current_ns
);
4160 gfc_current_ns
->translated
= 1;
4163 /* Clean up all the namespaces after translation. */
4164 gfc_current_ns
= gfc_global_ns_list
;
4165 for (;gfc_current_ns
;)
4167 gfc_namespace
*ns
= gfc_current_ns
->sibling
;
4168 gfc_derived_types
= gfc_current_ns
->derived_types
;
4170 gfc_current_ns
= ns
;
4173 clean_up_modules (gfc_gsym_root
);
4177 /* Top level parser. */
4180 gfc_parse_file (void)
4182 int seen_program
, errors_before
, errors
;
4183 gfc_state_data top
, s
;
4186 gfc_namespace
*next
;
4188 gfc_start_source_files ();
4190 top
.state
= COMP_NONE
;
4192 top
.previous
= NULL
;
4193 top
.head
= top
.tail
= NULL
;
4194 top
.do_variable
= NULL
;
4196 gfc_state_stack
= &top
;
4198 gfc_clear_new_st ();
4200 gfc_statement_label
= NULL
;
4202 if (setjmp (eof_buf
))
4203 return FAILURE
; /* Come here on unexpected EOF */
4205 /* Prepare the global namespace that will contain the
4207 gfc_global_ns_list
= next
= NULL
;
4211 /* Exit early for empty files. */
4217 st
= next_statement ();
4226 goto duplicate_main
;
4228 prog_locus
= gfc_current_locus
;
4230 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
4231 main_program_symbol(gfc_current_ns
, gfc_new_block
->name
);
4232 accept_statement (st
);
4233 add_global_program ();
4234 parse_progunit (ST_NONE
);
4235 if (gfc_option
.flag_whole_file
)
4240 add_global_procedure (1);
4241 push_state (&s
, COMP_SUBROUTINE
, gfc_new_block
);
4242 accept_statement (st
);
4243 parse_progunit (ST_NONE
);
4244 if (gfc_option
.flag_whole_file
)
4249 add_global_procedure (0);
4250 push_state (&s
, COMP_FUNCTION
, gfc_new_block
);
4251 accept_statement (st
);
4252 parse_progunit (ST_NONE
);
4253 if (gfc_option
.flag_whole_file
)
4258 push_state (&s
, COMP_BLOCK_DATA
, gfc_new_block
);
4259 accept_statement (st
);
4260 parse_block_data ();
4264 push_state (&s
, COMP_MODULE
, gfc_new_block
);
4265 accept_statement (st
);
4267 gfc_get_errors (NULL
, &errors_before
);
4271 /* Anything else starts a nameless main program block. */
4274 goto duplicate_main
;
4276 prog_locus
= gfc_current_locus
;
4278 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
4279 main_program_symbol (gfc_current_ns
, "MAIN__");
4280 parse_progunit (st
);
4281 if (gfc_option
.flag_whole_file
)
4286 /* Handle the non-program units. */
4287 gfc_current_ns
->code
= s
.head
;
4289 gfc_resolve (gfc_current_ns
);
4291 /* Dump the parse tree if requested. */
4292 if (gfc_option
.dump_parse_tree
)
4293 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
4295 gfc_get_errors (NULL
, &errors
);
4296 if (s
.state
== COMP_MODULE
)
4298 gfc_dump_module (s
.sym
->name
, errors_before
== errors
);
4300 gfc_generate_module_code (gfc_current_ns
);
4302 if (!gfc_option
.flag_whole_file
)
4306 gfc_current_ns
->derived_types
= gfc_derived_types
;
4307 gfc_derived_types
= NULL
;
4308 gfc_current_ns
= NULL
;
4314 gfc_generate_code (gfc_current_ns
);
4322 /* The main program and non-contained procedures are put
4323 in the global namespace list, so that they can be processed
4324 later and all their interfaces resolved. */
4325 gfc_current_ns
->code
= s
.head
;
4327 next
->sibling
= gfc_current_ns
;
4329 gfc_global_ns_list
= gfc_current_ns
;
4331 next
= gfc_current_ns
;
4338 if (!gfc_option
.flag_whole_file
)
4341 /* Do the resolution. */
4342 resolve_all_program_units (gfc_global_ns_list
);
4344 /* Do the parse tree dump. */
4346 = gfc_option
.dump_parse_tree
? gfc_global_ns_list
: NULL
;
4348 for (; gfc_current_ns
; gfc_current_ns
= gfc_current_ns
->sibling
)
4350 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
4351 fputs ("------------------------------------------\n\n", stdout
);
4354 /* Do the translation. */
4355 translate_all_program_units (gfc_global_ns_list
);
4359 gfc_end_source_files ();
4363 /* If we see a duplicate main program, shut down. If the second
4364 instance is an implied main program, i.e. data decls or executable
4365 statements, we're in for lots of errors. */
4366 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus
);
4367 reject_statement ();