2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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
;
40 /* TODO: Re-order functions to kill these forward decls. */
41 static void check_statement_label (gfc_statement
);
42 static void undo_new_statement (void);
43 static void reject_statement (void);
46 /* A sort of half-matching function. We try to match the word on the
47 input with the passed string. If this succeeds, we call the
48 keyword-dependent matching function that will match the rest of the
49 statement. For single keywords, the matching subroutine is
53 match_word (const char *str
, match (*subr
) (void), locus
*old_locus
)
68 gfc_current_locus
= *old_locus
;
76 /* Figure out what the next statement is, (mostly) regardless of
77 proper ordering. The do...while(0) is there to prevent if/else
80 #define match(keyword, subr, st) \
82 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
85 undo_new_statement (); \
89 /* This is a specialist version of decode_statement that is used
90 for the specification statements in a function, whose
91 characteristics are deferred into the specification statements.
92 eg.: INTEGER (king = mykind) foo ()
93 USE mymodule, ONLY mykind.....
94 The KIND parameter needs a return after USE or IMPORT, whereas
95 derived type declarations can occur anywhere, up the executable
96 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
97 out of the correct kind of specification statements. */
99 decode_specification_statement (void)
105 if (gfc_match_eos () == MATCH_YES
)
108 old_locus
= gfc_current_locus
;
110 match ("import", gfc_match_import
, ST_IMPORT
);
111 match ("use", gfc_match_use
, ST_USE
);
113 if (gfc_current_block ()->ts
.type
!= BT_DERIVED
)
116 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
117 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
118 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
120 /* General statement matching: Instead of testing every possible
121 statement, we eliminate most possibilities by peeking at the
124 c
= gfc_peek_ascii_char ();
129 match ("abstract% interface", gfc_match_abstract_interface
,
134 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
141 match ("data", gfc_match_data
, ST_DATA
);
142 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
146 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
147 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
148 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
149 match ("external", gfc_match_external
, ST_ATTR_DECL
);
153 match ("format", gfc_match_format
, ST_FORMAT
);
160 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
161 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
162 match ("interface", gfc_match_interface
, ST_INTERFACE
);
163 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
164 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
171 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
175 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
179 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
180 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
181 if (gfc_match_private (&st
) == MATCH_YES
)
183 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
184 if (gfc_match_public (&st
) == MATCH_YES
)
186 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
193 match ("save", gfc_match_save
, ST_ATTR_DECL
);
197 match ("target", gfc_match_target
, ST_ATTR_DECL
);
198 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
205 match ("value", gfc_match_value
, ST_ATTR_DECL
);
206 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
213 /* This is not a specification statement. See if any of the matchers
214 has stored an error message of some sort. */
218 gfc_buffer_error (0);
219 gfc_current_locus
= old_locus
;
221 return ST_GET_FCN_CHARACTERISTICS
;
225 /* This is the primary 'decode_statement'. */
227 decode_statement (void)
238 gfc_clear_error (); /* Clear any pending errors. */
239 gfc_clear_warning (); /* Clear any pending warnings. */
241 gfc_matching_function
= false;
243 if (gfc_match_eos () == MATCH_YES
)
246 if (gfc_current_state () == COMP_FUNCTION
247 && gfc_current_block ()->result
->ts
.kind
== -1)
248 return decode_specification_statement ();
250 old_locus
= gfc_current_locus
;
252 /* Try matching a data declaration or function declaration. The
253 input "REALFUNCTIONA(N)" can mean several things in different
254 contexts, so it (and its relatives) get special treatment. */
256 if (gfc_current_state () == COMP_NONE
257 || gfc_current_state () == COMP_INTERFACE
258 || gfc_current_state () == COMP_CONTAINS
)
260 gfc_matching_function
= true;
261 m
= gfc_match_function_decl ();
264 else if (m
== MATCH_ERROR
)
268 gfc_current_locus
= old_locus
;
270 gfc_matching_function
= false;
273 /* Match statements whose error messages are meant to be overwritten
274 by something better. */
276 match (NULL
, gfc_match_assignment
, ST_ASSIGNMENT
);
277 match (NULL
, gfc_match_pointer_assignment
, ST_POINTER_ASSIGNMENT
);
278 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
280 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
281 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
283 /* Try to match a subroutine statement, which has the same optional
284 prefixes that functions can have. */
286 if (gfc_match_subroutine () == MATCH_YES
)
287 return ST_SUBROUTINE
;
289 gfc_current_locus
= old_locus
;
291 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
292 might begin with a block label. The match functions for these
293 statements are unusual in that their keyword is not seen before
294 the matcher is called. */
296 if (gfc_match_if (&st
) == MATCH_YES
)
299 gfc_current_locus
= old_locus
;
301 if (gfc_match_where (&st
) == MATCH_YES
)
304 gfc_current_locus
= old_locus
;
306 if (gfc_match_forall (&st
) == MATCH_YES
)
309 gfc_current_locus
= old_locus
;
311 match (NULL
, gfc_match_do
, ST_DO
);
312 match (NULL
, gfc_match_select
, ST_SELECT_CASE
);
314 /* General statement matching: Instead of testing every possible
315 statement, we eliminate most possibilities by peeking at the
318 c
= gfc_peek_ascii_char ();
323 match ("abstract% interface", gfc_match_abstract_interface
,
325 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
);
326 match ("allocatable", gfc_match_allocatable
, ST_ATTR_DECL
);
327 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
);
331 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
);
332 match ("block data", gfc_match_block_data
, ST_BLOCK_DATA
);
333 match (NULL
, gfc_match_bind_c_stmt
, ST_ATTR_DECL
);
337 match ("call", gfc_match_call
, ST_CALL
);
338 match ("close", gfc_match_close
, ST_CLOSE
);
339 match ("continue", gfc_match_continue
, ST_CONTINUE
);
340 match ("cycle", gfc_match_cycle
, ST_CYCLE
);
341 match ("case", gfc_match_case
, ST_CASE
);
342 match ("common", gfc_match_common
, ST_COMMON
);
343 match ("contains", gfc_match_eos
, ST_CONTAINS
);
347 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
);
348 match ("data", gfc_match_data
, ST_DATA
);
349 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
353 match ("end file", gfc_match_endfile
, ST_END_FILE
);
354 match ("exit", gfc_match_exit
, ST_EXIT
);
355 match ("else", gfc_match_else
, ST_ELSE
);
356 match ("else where", gfc_match_elsewhere
, ST_ELSEWHERE
);
357 match ("else if", gfc_match_elseif
, ST_ELSEIF
);
358 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
360 if (gfc_match_end (&st
) == MATCH_YES
)
363 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
364 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
365 match ("external", gfc_match_external
, ST_ATTR_DECL
);
369 match ("final", gfc_match_final_decl
, ST_FINAL
);
370 match ("flush", gfc_match_flush
, ST_FLUSH
);
371 match ("format", gfc_match_format
, ST_FORMAT
);
375 match ("go to", gfc_match_goto
, ST_GOTO
);
379 match ("inquire", gfc_match_inquire
, ST_INQUIRE
);
380 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
381 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
382 match ("import", gfc_match_import
, ST_IMPORT
);
383 match ("interface", gfc_match_interface
, ST_INTERFACE
);
384 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
385 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
389 match ("module% procedure% ", gfc_match_modproc
, ST_MODULE_PROC
);
390 match ("module", gfc_match_module
, ST_MODULE
);
394 match ("nullify", gfc_match_nullify
, ST_NULLIFY
);
395 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
399 match ("open", gfc_match_open
, ST_OPEN
);
400 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
404 match ("print", gfc_match_print
, ST_WRITE
);
405 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
406 match ("pause", gfc_match_pause
, ST_PAUSE
);
407 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
408 if (gfc_match_private (&st
) == MATCH_YES
)
410 match ("procedure", gfc_match_procedure
, ST_PROCEDURE
);
411 match ("program", gfc_match_program
, ST_PROGRAM
);
412 if (gfc_match_public (&st
) == MATCH_YES
)
414 match ("protected", gfc_match_protected
, ST_ATTR_DECL
);
418 match ("read", gfc_match_read
, ST_READ
);
419 match ("return", gfc_match_return
, ST_RETURN
);
420 match ("rewind", gfc_match_rewind
, ST_REWIND
);
424 match ("sequence", gfc_match_eos
, ST_SEQUENCE
);
425 match ("stop", gfc_match_stop
, ST_STOP
);
426 match ("save", gfc_match_save
, ST_ATTR_DECL
);
430 match ("target", gfc_match_target
, ST_ATTR_DECL
);
431 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
435 match ("use", gfc_match_use
, ST_USE
);
439 match ("value", gfc_match_value
, ST_ATTR_DECL
);
440 match ("volatile", gfc_match_volatile
, ST_ATTR_DECL
);
444 match ("wait", gfc_match_wait
, ST_WAIT
);
445 match ("write", gfc_match_write
, ST_WRITE
);
449 /* All else has failed, so give up. See if any of the matchers has
450 stored an error message of some sort. */
452 if (gfc_error_check () == 0)
453 gfc_error_now ("Unclassifiable statement at %C");
457 gfc_error_recovery ();
463 decode_omp_directive (void)
472 gfc_clear_error (); /* Clear any pending errors. */
473 gfc_clear_warning (); /* Clear any pending warnings. */
477 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
478 "or ELEMENTAL procedures");
479 gfc_error_recovery ();
483 old_locus
= gfc_current_locus
;
485 /* General OpenMP directive matching: Instead of testing every possible
486 statement, we eliminate most possibilities by peeking at the
489 c
= gfc_peek_ascii_char ();
494 match ("atomic", gfc_match_omp_atomic
, ST_OMP_ATOMIC
);
497 match ("barrier", gfc_match_omp_barrier
, ST_OMP_BARRIER
);
500 match ("critical", gfc_match_omp_critical
, ST_OMP_CRITICAL
);
503 match ("do", gfc_match_omp_do
, ST_OMP_DO
);
506 match ("end critical", gfc_match_omp_critical
, ST_OMP_END_CRITICAL
);
507 match ("end do", gfc_match_omp_end_nowait
, ST_OMP_END_DO
);
508 match ("end master", gfc_match_omp_eos
, ST_OMP_END_MASTER
);
509 match ("end ordered", gfc_match_omp_eos
, ST_OMP_END_ORDERED
);
510 match ("end parallel do", gfc_match_omp_eos
, ST_OMP_END_PARALLEL_DO
);
511 match ("end parallel sections", gfc_match_omp_eos
,
512 ST_OMP_END_PARALLEL_SECTIONS
);
513 match ("end parallel workshare", gfc_match_omp_eos
,
514 ST_OMP_END_PARALLEL_WORKSHARE
);
515 match ("end parallel", gfc_match_omp_eos
, ST_OMP_END_PARALLEL
);
516 match ("end sections", gfc_match_omp_end_nowait
, ST_OMP_END_SECTIONS
);
517 match ("end single", gfc_match_omp_end_single
, ST_OMP_END_SINGLE
);
518 match ("end task", gfc_match_omp_eos
, ST_OMP_END_TASK
);
519 match ("end workshare", gfc_match_omp_end_nowait
,
520 ST_OMP_END_WORKSHARE
);
523 match ("flush", gfc_match_omp_flush
, ST_OMP_FLUSH
);
526 match ("master", gfc_match_omp_master
, ST_OMP_MASTER
);
529 match ("ordered", gfc_match_omp_ordered
, ST_OMP_ORDERED
);
532 match ("parallel do", gfc_match_omp_parallel_do
, ST_OMP_PARALLEL_DO
);
533 match ("parallel sections", gfc_match_omp_parallel_sections
,
534 ST_OMP_PARALLEL_SECTIONS
);
535 match ("parallel workshare", gfc_match_omp_parallel_workshare
,
536 ST_OMP_PARALLEL_WORKSHARE
);
537 match ("parallel", gfc_match_omp_parallel
, ST_OMP_PARALLEL
);
540 match ("sections", gfc_match_omp_sections
, ST_OMP_SECTIONS
);
541 match ("section", gfc_match_omp_eos
, ST_OMP_SECTION
);
542 match ("single", gfc_match_omp_single
, ST_OMP_SINGLE
);
545 match ("task", gfc_match_omp_task
, ST_OMP_TASK
);
546 match ("taskwait", gfc_match_omp_taskwait
, ST_OMP_TASKWAIT
);
547 match ("threadprivate", gfc_match_omp_threadprivate
,
548 ST_OMP_THREADPRIVATE
);
550 match ("workshare", gfc_match_omp_workshare
, ST_OMP_WORKSHARE
);
554 /* All else has failed, so give up. See if any of the matchers has
555 stored an error message of some sort. */
557 if (gfc_error_check () == 0)
558 gfc_error_now ("Unclassifiable OpenMP directive at %C");
562 gfc_error_recovery ();
570 /* Get the next statement in free form source. */
579 at_bol
= gfc_at_bol ();
580 gfc_gobble_whitespace ();
582 c
= gfc_peek_ascii_char ();
588 /* Found a statement label? */
589 m
= gfc_match_st_label (&gfc_statement_label
);
591 d
= gfc_peek_ascii_char ();
592 if (m
!= MATCH_YES
|| !gfc_is_whitespace (d
))
594 gfc_match_small_literal_int (&i
, &cnt
);
597 gfc_error_now ("Too many digits in statement label at %C");
600 gfc_error_now ("Zero is not a valid statement label at %C");
603 c
= gfc_next_ascii_char ();
606 if (!gfc_is_whitespace (c
))
607 gfc_error_now ("Non-numeric character in statement label at %C");
613 label_locus
= gfc_current_locus
;
615 gfc_gobble_whitespace ();
617 if (at_bol
&& gfc_peek_ascii_char () == ';')
619 gfc_error_now ("Semicolon at %C needs to be preceded by "
621 gfc_next_ascii_char (); /* Eat up the semicolon. */
625 if (gfc_match_eos () == MATCH_YES
)
627 gfc_warning_now ("Ignoring statement label in empty statement "
629 gfc_free_st_label (gfc_statement_label
);
630 gfc_statement_label
= NULL
;
637 /* Comments have already been skipped by the time we get here,
638 except for OpenMP directives. */
639 if (gfc_option
.flag_openmp
)
643 c
= gfc_next_ascii_char ();
644 for (i
= 0; i
< 5; i
++, c
= gfc_next_ascii_char ())
645 gcc_assert (c
== "!$omp"[i
]);
647 gcc_assert (c
== ' ' || c
== '\t');
648 gfc_gobble_whitespace ();
649 return decode_omp_directive ();
653 if (at_bol
&& c
== ';')
655 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
656 gfc_next_ascii_char (); /* Eat up the semicolon. */
660 return decode_statement ();
664 /* Get the next statement in fixed-form source. */
669 int label
, digit_flag
, i
;
674 return decode_statement ();
676 /* Skip past the current label field, parsing a statement label if
677 one is there. This is a weird number parser, since the number is
678 contained within five columns and can have any kind of embedded
679 spaces. We also check for characters that make the rest of the
685 for (i
= 0; i
< 5; i
++)
687 c
= gfc_next_char_literal (0);
704 label
= label
* 10 + ((unsigned char) c
- '0');
705 label_locus
= gfc_current_locus
;
709 /* Comments have already been skipped by the time we get
710 here, except for OpenMP directives. */
712 if (gfc_option
.flag_openmp
)
714 for (i
= 0; i
< 5; i
++, c
= gfc_next_char_literal (0))
715 gcc_assert ((char) gfc_wide_tolower (c
) == "*$omp"[i
]);
717 if (c
!= ' ' && c
!= '0')
719 gfc_buffer_error (0);
720 gfc_error ("Bad continuation line at %C");
724 return decode_omp_directive ();
728 /* Comments have already been skipped by the time we get
729 here so don't bother checking for them. */
732 gfc_buffer_error (0);
733 gfc_error ("Non-numeric character in statement label at %C");
741 gfc_warning_now ("Zero is not a valid statement label at %C");
744 /* We've found a valid statement label. */
745 gfc_statement_label
= gfc_get_st_label (label
);
749 /* Since this line starts a statement, it cannot be a continuation
750 of a previous statement. If we see something here besides a
751 space or zero, it must be a bad continuation line. */
753 c
= gfc_next_char_literal (0);
757 if (c
!= ' ' && c
!= '0')
759 gfc_buffer_error (0);
760 gfc_error ("Bad continuation line at %C");
764 /* Now that we've taken care of the statement label columns, we have
765 to make sure that the first nonblank character is not a '!'. If
766 it is, the rest of the line is a comment. */
770 loc
= gfc_current_locus
;
771 c
= gfc_next_char_literal (0);
773 while (gfc_is_whitespace (c
));
777 gfc_current_locus
= loc
;
781 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
785 if (gfc_match_eos () == MATCH_YES
)
788 /* At this point, we've got a nonblank statement to parse. */
789 return decode_statement ();
793 gfc_warning ("Ignoring statement label in empty statement at %C");
799 /* Return the next non-ST_NONE statement to the caller. We also worry
800 about including files and the ends of include files at this stage. */
803 next_statement (void)
807 gfc_new_block
= NULL
;
811 gfc_statement_label
= NULL
;
812 gfc_buffer_error (1);
816 if ((gfc_option
.warn_line_truncation
|| gfc_current_form
== FORM_FREE
)
817 && gfc_current_locus
.lb
818 && gfc_current_locus
.lb
->truncated
)
819 gfc_warning_now ("Line truncated at %C");
824 gfc_skip_comments ();
832 if (gfc_define_undef_line ())
835 old_locus
= gfc_current_locus
;
837 st
= (gfc_current_form
== FORM_FIXED
) ? next_fixed () : next_free ();
843 gfc_buffer_error (0);
845 if (st
== ST_GET_FCN_CHARACTERISTICS
&& gfc_statement_label
!= NULL
)
847 gfc_free_st_label (gfc_statement_label
);
848 gfc_statement_label
= NULL
;
849 gfc_current_locus
= old_locus
;
853 check_statement_label (st
);
859 /****************************** Parser ***********************************/
861 /* The parser subroutines are of type 'try' that fail if the file ends
864 /* Macros that expand to case-labels for various classes of
865 statements. Start with executable statements that directly do
868 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
869 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
870 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
871 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
872 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
873 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
874 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
875 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
876 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT
878 /* Statements that mark other executable statements. */
880 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
881 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
882 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
883 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
884 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
885 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
888 /* Declaration statements */
890 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
891 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
892 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
895 /* Block end statements. Errors associated with interchanging these
896 are detected in gfc_match_end(). */
898 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
899 case ST_END_PROGRAM: case ST_END_SUBROUTINE
902 /* Push a new state onto the stack. */
905 push_state (gfc_state_data
*p
, gfc_compile_state new_state
, gfc_symbol
*sym
)
907 p
->state
= new_state
;
908 p
->previous
= gfc_state_stack
;
910 p
->head
= p
->tail
= NULL
;
911 p
->do_variable
= NULL
;
916 /* Pop the current state. */
920 gfc_state_stack
= gfc_state_stack
->previous
;
924 /* Try to find the given state in the state stack. */
927 gfc_find_state (gfc_compile_state state
)
931 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
932 if (p
->state
== state
)
935 return (p
== NULL
) ? FAILURE
: SUCCESS
;
939 /* Starts a new level in the statement list. */
942 new_level (gfc_code
*q
)
946 p
= q
->block
= gfc_get_code ();
948 gfc_state_stack
->head
= gfc_state_stack
->tail
= p
;
954 /* Add the current new_st code structure and adds it to the current
955 program unit. As a side-effect, it zeroes the new_st. */
965 p
->loc
= gfc_current_locus
;
967 if (gfc_state_stack
->head
== NULL
)
968 gfc_state_stack
->head
= p
;
970 gfc_state_stack
->tail
->next
= p
;
972 while (p
->next
!= NULL
)
975 gfc_state_stack
->tail
= p
;
983 /* Frees everything associated with the current statement. */
986 undo_new_statement (void)
988 gfc_free_statements (new_st
.block
);
989 gfc_free_statements (new_st
.next
);
990 gfc_free_statement (&new_st
);
995 /* If the current statement has a statement label, make sure that it
996 is allowed to, or should have one. */
999 check_statement_label (gfc_statement st
)
1003 if (gfc_statement_label
== NULL
)
1005 if (st
== ST_FORMAT
)
1006 gfc_error ("FORMAT statement at %L does not have a statement label",
1013 case ST_END_PROGRAM
:
1014 case ST_END_FUNCTION
:
1015 case ST_END_SUBROUTINE
:
1021 type
= ST_LABEL_TARGET
;
1025 type
= ST_LABEL_FORMAT
;
1028 /* Statement labels are not restricted from appearing on a
1029 particular line. However, there are plenty of situations
1030 where the resulting label can't be referenced. */
1033 type
= ST_LABEL_BAD_TARGET
;
1037 gfc_define_st_label (gfc_statement_label
, type
, &label_locus
);
1039 new_st
.here
= gfc_statement_label
;
1043 /* Figures out what the enclosing program unit is. This will be a
1044 function, subroutine, program, block data or module. */
1047 gfc_enclosing_unit (gfc_compile_state
* result
)
1051 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
1052 if (p
->state
== COMP_FUNCTION
|| p
->state
== COMP_SUBROUTINE
1053 || p
->state
== COMP_MODULE
|| p
->state
== COMP_BLOCK_DATA
1054 || p
->state
== COMP_PROGRAM
)
1063 *result
= COMP_PROGRAM
;
1068 /* Translate a statement enum to a string. */
1071 gfc_ascii_statement (gfc_statement st
)
1077 case ST_ARITHMETIC_IF
:
1078 p
= _("arithmetic IF");
1084 p
= _("attribute declaration");
1114 p
= _("data declaration");
1122 case ST_DERIVED_DECL
:
1123 p
= _("derived type declaration");
1137 case ST_END_BLOCK_DATA
:
1138 p
= "END BLOCK DATA";
1149 case ST_END_FUNCTION
:
1155 case ST_END_INTERFACE
:
1156 p
= "END INTERFACE";
1161 case ST_END_PROGRAM
:
1167 case ST_END_SUBROUTINE
:
1168 p
= "END SUBROUTINE";
1179 case ST_EQUIVALENCE
:
1188 case ST_FORALL_BLOCK
: /* Fall through */
1207 case ST_IMPLICIT_NONE
:
1208 p
= "IMPLICIT NONE";
1210 case ST_IMPLIED_ENDDO
:
1211 p
= _("implied END DO");
1237 case ST_MODULE_PROC
:
1238 p
= "MODULE PROCEDURE";
1276 case ST_WHERE_BLOCK
: /* Fall through */
1287 p
= _("assignment");
1289 case ST_POINTER_ASSIGNMENT
:
1290 p
= _("pointer assignment");
1292 case ST_SELECT_CASE
:
1301 case ST_STATEMENT_FUNCTION
:
1302 p
= "STATEMENT FUNCTION";
1304 case ST_LABEL_ASSIGNMENT
:
1305 p
= "LABEL ASSIGNMENT";
1308 p
= "ENUM DEFINITION";
1311 p
= "ENUMERATOR DEFINITION";
1319 case ST_OMP_BARRIER
:
1320 p
= "!$OMP BARRIER";
1322 case ST_OMP_CRITICAL
:
1323 p
= "!$OMP CRITICAL";
1328 case ST_OMP_END_CRITICAL
:
1329 p
= "!$OMP END CRITICAL";
1334 case ST_OMP_END_MASTER
:
1335 p
= "!$OMP END MASTER";
1337 case ST_OMP_END_ORDERED
:
1338 p
= "!$OMP END ORDERED";
1340 case ST_OMP_END_PARALLEL
:
1341 p
= "!$OMP END PARALLEL";
1343 case ST_OMP_END_PARALLEL_DO
:
1344 p
= "!$OMP END PARALLEL DO";
1346 case ST_OMP_END_PARALLEL_SECTIONS
:
1347 p
= "!$OMP END PARALLEL SECTIONS";
1349 case ST_OMP_END_PARALLEL_WORKSHARE
:
1350 p
= "!$OMP END PARALLEL WORKSHARE";
1352 case ST_OMP_END_SECTIONS
:
1353 p
= "!$OMP END SECTIONS";
1355 case ST_OMP_END_SINGLE
:
1356 p
= "!$OMP END SINGLE";
1358 case ST_OMP_END_TASK
:
1359 p
= "!$OMP END TASK";
1361 case ST_OMP_END_WORKSHARE
:
1362 p
= "!$OMP END WORKSHARE";
1370 case ST_OMP_ORDERED
:
1371 p
= "!$OMP ORDERED";
1373 case ST_OMP_PARALLEL
:
1374 p
= "!$OMP PARALLEL";
1376 case ST_OMP_PARALLEL_DO
:
1377 p
= "!$OMP PARALLEL DO";
1379 case ST_OMP_PARALLEL_SECTIONS
:
1380 p
= "!$OMP PARALLEL SECTIONS";
1382 case ST_OMP_PARALLEL_WORKSHARE
:
1383 p
= "!$OMP PARALLEL WORKSHARE";
1385 case ST_OMP_SECTIONS
:
1386 p
= "!$OMP SECTIONS";
1388 case ST_OMP_SECTION
:
1389 p
= "!$OMP SECTION";
1397 case ST_OMP_TASKWAIT
:
1398 p
= "!$OMP TASKWAIT";
1400 case ST_OMP_THREADPRIVATE
:
1401 p
= "!$OMP THREADPRIVATE";
1403 case ST_OMP_WORKSHARE
:
1404 p
= "!$OMP WORKSHARE";
1407 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1414 /* Create a symbol for the main program and assign it to ns->proc_name. */
1417 main_program_symbol (gfc_namespace
*ns
, const char *name
)
1419 gfc_symbol
*main_program
;
1420 symbol_attribute attr
;
1422 gfc_get_symbol (name
, ns
, &main_program
);
1423 gfc_clear_attr (&attr
);
1424 attr
.flavor
= FL_PROGRAM
;
1425 attr
.proc
= PROC_UNKNOWN
;
1426 attr
.subroutine
= 1;
1427 attr
.access
= ACCESS_PUBLIC
;
1428 attr
.is_main_program
= 1;
1429 main_program
->attr
= attr
;
1430 main_program
->declared_at
= gfc_current_locus
;
1431 ns
->proc_name
= main_program
;
1432 gfc_commit_symbols ();
1436 /* Do whatever is necessary to accept the last statement. */
1439 accept_statement (gfc_statement st
)
1447 case ST_IMPLICIT_NONE
:
1448 gfc_set_implicit_none ();
1457 gfc_current_ns
->proc_name
= gfc_new_block
;
1460 /* If the statement is the end of a block, lay down a special code
1461 that allows a branch to the end of the block from within the
1466 if (gfc_statement_label
!= NULL
)
1468 new_st
.op
= EXEC_NOP
;
1474 /* The end-of-program unit statements do not get the special
1475 marker and require a statement of some sort if they are a
1478 case ST_END_PROGRAM
:
1479 case ST_END_FUNCTION
:
1480 case ST_END_SUBROUTINE
:
1481 if (gfc_statement_label
!= NULL
)
1483 new_st
.op
= EXEC_RETURN
;
1499 gfc_commit_symbols ();
1500 gfc_warning_check ();
1501 gfc_clear_new_st ();
1505 /* Undo anything tentative that has been built for the current
1509 reject_statement (void)
1511 gfc_new_block
= NULL
;
1512 gfc_undo_symbols ();
1513 gfc_clear_warning ();
1514 undo_new_statement ();
1518 /* Generic complaint about an out of order statement. We also do
1519 whatever is necessary to clean up. */
1522 unexpected_statement (gfc_statement st
)
1524 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st
));
1526 reject_statement ();
1530 /* Given the next statement seen by the matcher, make sure that it is
1531 in proper order with the last. This subroutine is initialized by
1532 calling it with an argument of ST_NONE. If there is a problem, we
1533 issue an error and return FAILURE. Otherwise we return SUCCESS.
1535 Individual parsers need to verify that the statements seen are
1536 valid before calling here, i.e., ENTRY statements are not allowed in
1537 INTERFACE blocks. The following diagram is taken from the standard:
1539 +---------------------------------------+
1540 | program subroutine function module |
1541 +---------------------------------------+
1543 +---------------------------------------+
1545 +---------------------------------------+
1547 | +-----------+------------------+
1548 | | parameter | implicit |
1549 | +-----------+------------------+
1550 | format | | derived type |
1551 | entry | parameter | interface |
1552 | | data | specification |
1553 | | | statement func |
1554 | +-----------+------------------+
1555 | | data | executable |
1556 +--------+-----------+------------------+
1558 +---------------------------------------+
1559 | internal module/subprogram |
1560 +---------------------------------------+
1562 +---------------------------------------+
1569 { ORDER_START
, ORDER_USE
, ORDER_IMPORT
, ORDER_IMPLICIT_NONE
,
1570 ORDER_IMPLICIT
, ORDER_SPEC
, ORDER_EXEC
1573 gfc_statement last_statement
;
1579 verify_st_order (st_state
*p
, gfc_statement st
, bool silent
)
1585 p
->state
= ORDER_START
;
1589 if (p
->state
> ORDER_USE
)
1591 p
->state
= ORDER_USE
;
1595 if (p
->state
> ORDER_IMPORT
)
1597 p
->state
= ORDER_IMPORT
;
1600 case ST_IMPLICIT_NONE
:
1601 if (p
->state
> ORDER_IMPLICIT_NONE
)
1604 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1605 statement disqualifies a USE but not an IMPLICIT NONE.
1606 Duplicate IMPLICIT NONEs are caught when the implicit types
1609 p
->state
= ORDER_IMPLICIT_NONE
;
1613 if (p
->state
> ORDER_IMPLICIT
)
1615 p
->state
= ORDER_IMPLICIT
;
1620 if (p
->state
< ORDER_IMPLICIT_NONE
)
1621 p
->state
= ORDER_IMPLICIT_NONE
;
1625 if (p
->state
>= ORDER_EXEC
)
1627 if (p
->state
< ORDER_IMPLICIT
)
1628 p
->state
= ORDER_IMPLICIT
;
1632 if (p
->state
< ORDER_SPEC
)
1633 p
->state
= ORDER_SPEC
;
1638 case ST_DERIVED_DECL
:
1640 if (p
->state
>= ORDER_EXEC
)
1642 if (p
->state
< ORDER_SPEC
)
1643 p
->state
= ORDER_SPEC
;
1648 if (p
->state
< ORDER_EXEC
)
1649 p
->state
= ORDER_EXEC
;
1653 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1654 gfc_ascii_statement (st
));
1657 /* All is well, record the statement in case we need it next time. */
1658 p
->where
= gfc_current_locus
;
1659 p
->last_statement
= st
;
1664 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1665 gfc_ascii_statement (st
),
1666 gfc_ascii_statement (p
->last_statement
), &p
->where
);
1672 /* Handle an unexpected end of file. This is a show-stopper... */
1674 static void unexpected_eof (void) ATTRIBUTE_NORETURN
;
1677 unexpected_eof (void)
1681 gfc_error ("Unexpected end of file in '%s'", gfc_source_file
);
1683 /* Memory cleanup. Move to "second to last". */
1684 for (p
= gfc_state_stack
; p
&& p
->previous
&& p
->previous
->previous
;
1687 gfc_current_ns
->code
= (p
&& p
->previous
) ? p
->head
: NULL
;
1690 longjmp (eof_buf
, 1);
1694 /* Set the default access attribute for a typebound procedure; this is used
1695 as callback for gfc_traverse_symtree. */
1697 static gfc_access typebound_default_access
;
1700 set_typebound_default_access (gfc_symtree
* stree
)
1702 if (stree
->typebound
&& stree
->typebound
->access
== ACCESS_UNKNOWN
)
1703 stree
->typebound
->access
= typebound_default_access
;
1707 /* Parse the CONTAINS section of a derived type definition. */
1710 parse_derived_contains (void)
1713 bool seen_private
= false;
1714 bool seen_comps
= false;
1715 bool error_flag
= false;
1718 accept_statement (ST_CONTAINS
);
1719 gcc_assert (gfc_current_state () == COMP_DERIVED
);
1720 push_state (&s
, COMP_DERIVED_CONTAINS
, NULL
);
1726 st
= next_statement ();
1734 gfc_error ("Components in TYPE at %C must precede CONTAINS");
1739 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Type-bound"
1740 " procedure at %C") == FAILURE
)
1743 accept_statement (ST_PROCEDURE
);
1748 if (gfc_notify_std (GFC_STD_F2003
,
1749 "Fortran 2003: FINAL procedure declaration"
1750 " at %C") == FAILURE
)
1753 accept_statement (ST_FINAL
);
1761 && (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: Derived type "
1762 "definition at %C with empty CONTAINS "
1763 "section") == FAILURE
))
1766 /* ST_END_TYPE is accepted by parse_derived after return. */
1770 if (gfc_find_state (COMP_MODULE
) == FAILURE
)
1772 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1780 gfc_error ("PRIVATE statement at %C must precede procedure"
1788 gfc_error ("Duplicate PRIVATE statement at %C");
1792 accept_statement (ST_PRIVATE
);
1793 seen_private
= true;
1797 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
1802 gfc_error ("Already inside a CONTAINS block at %C");
1807 unexpected_statement (st
);
1813 gcc_assert (gfc_current_state () == COMP_DERIVED
);
1815 /* Walk the parsed type-bound procedures and set ACCESS_UNKNOWN attributes
1816 to PUBLIC or PRIVATE depending on seen_private. */
1817 typebound_default_access
= (seen_private
? ACCESS_PRIVATE
: ACCESS_PUBLIC
);
1818 gfc_traverse_symtree (gfc_current_block ()->f2k_derived
->sym_root
,
1819 &set_typebound_default_access
);
1825 /* Parse a derived type. */
1828 parse_derived (void)
1830 int compiling_type
, seen_private
, seen_sequence
, seen_component
, error_flag
;
1833 gfc_symbol
*derived_sym
= NULL
;
1839 accept_statement (ST_DERIVED_DECL
);
1840 push_state (&s
, COMP_DERIVED
, gfc_new_block
);
1842 gfc_new_block
->component_access
= ACCESS_PUBLIC
;
1849 while (compiling_type
)
1851 st
= next_statement ();
1858 accept_statement (st
);
1863 gfc_error ("PROCEDURE binding at %C must be inside CONTAINS");
1868 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
1877 && (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Derived type "
1878 "definition at %C without components")
1882 accept_statement (ST_END_TYPE
);
1886 if (gfc_find_state (COMP_MODULE
) == FAILURE
)
1888 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1896 gfc_error ("PRIVATE statement at %C must precede "
1897 "structure components");
1904 gfc_error ("Duplicate PRIVATE statement at %C");
1908 s
.sym
->component_access
= ACCESS_PRIVATE
;
1910 accept_statement (ST_PRIVATE
);
1917 gfc_error ("SEQUENCE statement at %C must precede "
1918 "structure components");
1923 if (gfc_current_block ()->attr
.sequence
)
1924 gfc_warning ("SEQUENCE attribute at %C already specified in "
1929 gfc_error ("Duplicate SEQUENCE statement at %C");
1934 gfc_add_sequence (&gfc_current_block ()->attr
,
1935 gfc_current_block ()->name
, NULL
);
1939 if (gfc_notify_std (GFC_STD_F2003
,
1940 "Fortran 2003: CONTAINS block in derived type"
1941 " definition at %C") == FAILURE
)
1944 accept_statement (ST_CONTAINS
);
1945 if (parse_derived_contains ())
1950 unexpected_statement (st
);
1955 /* need to verify that all fields of the derived type are
1956 * interoperable with C if the type is declared to be bind(c)
1958 derived_sym
= gfc_current_block();
1960 sym
= gfc_current_block ();
1961 for (c
= sym
->components
; c
; c
= c
->next
)
1963 /* Look for allocatable components. */
1964 if (c
->attr
.allocatable
1965 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.derived
->attr
.alloc_comp
))
1967 sym
->attr
.alloc_comp
= 1;
1971 /* Look for pointer components. */
1973 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.derived
->attr
.pointer_comp
))
1975 sym
->attr
.pointer_comp
= 1;
1979 /* Look for private components. */
1980 if (sym
->component_access
== ACCESS_PRIVATE
1981 || c
->attr
.access
== ACCESS_PRIVATE
1982 || (c
->ts
.type
== BT_DERIVED
&& c
->ts
.derived
->attr
.private_comp
))
1984 sym
->attr
.private_comp
= 1;
1989 if (!seen_component
)
1990 sym
->attr
.zero_comp
= 1;
1996 /* Parse an ENUM. */
2005 int seen_enumerator
= 0;
2009 push_state (&s
, COMP_ENUM
, gfc_new_block
);
2013 while (compiling_enum
)
2015 st
= next_statement ();
2023 seen_enumerator
= 1;
2024 accept_statement (st
);
2029 if (!seen_enumerator
)
2031 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2034 accept_statement (st
);
2038 gfc_free_enum_history ();
2039 unexpected_statement (st
);
2047 /* Parse an interface. We must be able to deal with the possibility
2048 of recursive interfaces. The parse_spec() subroutine is mutually
2049 recursive with parse_interface(). */
2051 static gfc_statement
parse_spec (gfc_statement
);
2054 parse_interface (void)
2056 gfc_compile_state new_state
, current_state
;
2057 gfc_symbol
*prog_unit
, *sym
;
2058 gfc_interface_info save
;
2059 gfc_state_data s1
, s2
;
2063 accept_statement (ST_INTERFACE
);
2065 current_interface
.ns
= gfc_current_ns
;
2066 save
= current_interface
;
2068 sym
= (current_interface
.type
== INTERFACE_GENERIC
2069 || current_interface
.type
== INTERFACE_USER_OP
)
2070 ? gfc_new_block
: NULL
;
2072 push_state (&s1
, COMP_INTERFACE
, sym
);
2073 current_state
= COMP_NONE
;
2076 gfc_current_ns
= gfc_get_namespace (current_interface
.ns
, 0);
2078 st
= next_statement ();
2086 if (st
== ST_SUBROUTINE
)
2087 new_state
= COMP_SUBROUTINE
;
2088 else if (st
== ST_FUNCTION
)
2089 new_state
= COMP_FUNCTION
;
2090 if (gfc_new_block
->attr
.pointer
)
2092 gfc_new_block
->attr
.pointer
= 0;
2093 gfc_new_block
->attr
.proc_pointer
= 1;
2095 if (gfc_add_explicit_interface (gfc_new_block
, IFSRC_IFBODY
,
2096 gfc_new_block
->formal
, NULL
) == FAILURE
)
2098 reject_statement ();
2099 gfc_free_namespace (gfc_current_ns
);
2102 if (current_interface
.type
!= INTERFACE_ABSTRACT
&&
2103 !gfc_new_block
->attr
.dummy
&&
2104 gfc_add_external (&gfc_new_block
->attr
, &gfc_current_locus
) == FAILURE
)
2106 reject_statement ();
2107 gfc_free_namespace (gfc_current_ns
);
2113 case ST_MODULE_PROC
: /* The module procedure matcher makes
2114 sure the context is correct. */
2115 accept_statement (st
);
2116 gfc_free_namespace (gfc_current_ns
);
2119 case ST_END_INTERFACE
:
2120 gfc_free_namespace (gfc_current_ns
);
2121 gfc_current_ns
= current_interface
.ns
;
2125 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2126 gfc_ascii_statement (st
));
2127 reject_statement ();
2128 gfc_free_namespace (gfc_current_ns
);
2133 /* Make sure that a generic interface has only subroutines or
2134 functions and that the generic name has the right attribute. */
2135 if (current_interface
.type
== INTERFACE_GENERIC
)
2137 if (current_state
== COMP_NONE
)
2139 if (new_state
== COMP_FUNCTION
)
2140 gfc_add_function (&sym
->attr
, sym
->name
, NULL
);
2141 else if (new_state
== COMP_SUBROUTINE
)
2142 gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
);
2144 current_state
= new_state
;
2148 if (new_state
!= current_state
)
2150 if (new_state
== COMP_SUBROUTINE
)
2151 gfc_error ("SUBROUTINE at %C does not belong in a "
2152 "generic function interface");
2154 if (new_state
== COMP_FUNCTION
)
2155 gfc_error ("FUNCTION at %C does not belong in a "
2156 "generic subroutine interface");
2161 if (current_interface
.type
== INTERFACE_ABSTRACT
)
2163 gfc_new_block
->attr
.abstract
= 1;
2164 if (gfc_is_intrinsic_typename (gfc_new_block
->name
))
2165 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
2166 "cannot be the same as an intrinsic type",
2167 gfc_new_block
->name
);
2170 push_state (&s2
, new_state
, gfc_new_block
);
2171 accept_statement (st
);
2172 prog_unit
= gfc_new_block
;
2173 prog_unit
->formal_ns
= gfc_current_ns
;
2174 proc_locus
= gfc_current_locus
;
2177 /* Read data declaration statements. */
2178 st
= parse_spec (ST_NONE
);
2180 /* Since the interface block does not permit an IMPLICIT statement,
2181 the default type for the function or the result must be taken
2182 from the formal namespace. */
2183 if (new_state
== COMP_FUNCTION
)
2185 if (prog_unit
->result
== prog_unit
2186 && prog_unit
->ts
.type
== BT_UNKNOWN
)
2187 gfc_set_default_type (prog_unit
, 1, prog_unit
->formal_ns
);
2188 else if (prog_unit
->result
!= prog_unit
2189 && prog_unit
->result
->ts
.type
== BT_UNKNOWN
)
2190 gfc_set_default_type (prog_unit
->result
, 1,
2191 prog_unit
->formal_ns
);
2194 if (st
!= ST_END_SUBROUTINE
&& st
!= ST_END_FUNCTION
)
2196 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2197 gfc_ascii_statement (st
));
2198 reject_statement ();
2202 current_interface
= save
;
2203 gfc_add_interface (prog_unit
);
2206 if (current_interface
.ns
2207 && current_interface
.ns
->proc_name
2208 && strcmp (current_interface
.ns
->proc_name
->name
,
2209 prog_unit
->name
) == 0)
2210 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2211 "enclosing procedure", prog_unit
->name
, &proc_locus
);
2220 /* Associate function characteristics by going back to the function
2221 declaration and rematching the prefix. */
2224 match_deferred_characteristics (gfc_typespec
* ts
)
2227 match m
= MATCH_ERROR
;
2228 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2230 loc
= gfc_current_locus
;
2232 gfc_current_locus
= gfc_current_block ()->declared_at
;
2235 gfc_buffer_error (1);
2236 m
= gfc_match_prefix (ts
);
2237 gfc_buffer_error (0);
2239 if (ts
->type
== BT_DERIVED
)
2243 if (!ts
->derived
|| !ts
->derived
->components
)
2247 /* Only permit one go at the characteristic association. */
2251 /* Set the function locus correctly. If we have not found the
2252 function name, there is an error. */
2253 gfc_match ("function% %n", name
);
2254 if (m
== MATCH_YES
&& strcmp (name
, gfc_current_block ()->name
) == 0)
2256 gfc_current_block ()->declared_at
= gfc_current_locus
;
2257 gfc_commit_symbols ();
2262 gfc_current_locus
=loc
;
2267 /* Check specification-expressions in the function result of the currently
2268 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2269 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2270 scope are not yet parsed so this has to be delayed up to parse_spec. */
2273 check_function_result_typed (void)
2275 gfc_typespec
* ts
= &gfc_current_ns
->proc_name
->result
->ts
;
2277 gcc_assert (gfc_current_state () == COMP_FUNCTION
);
2278 gcc_assert (ts
->type
!= BT_UNKNOWN
);
2280 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2281 /* TODO: Extend when KIND type parameters are implemented. */
2282 if (ts
->type
== BT_CHARACTER
&& ts
->cl
&& ts
->cl
->length
)
2283 gfc_expr_check_typed (ts
->cl
->length
, gfc_current_ns
, true);
2287 /* Parse a set of specification statements. Returns the statement
2288 that doesn't fit. */
2290 static gfc_statement
2291 parse_spec (gfc_statement st
)
2294 bool function_result_typed
= false;
2295 bool bad_characteristic
= false;
2298 verify_st_order (&ss
, ST_NONE
, false);
2300 st
= next_statement ();
2302 /* If we are not inside a function or don't have a result specified so far,
2303 do nothing special about it. */
2304 if (gfc_current_state () != COMP_FUNCTION
)
2305 function_result_typed
= true;
2308 gfc_symbol
* proc
= gfc_current_ns
->proc_name
;
2311 if (proc
->result
->ts
.type
== BT_UNKNOWN
)
2312 function_result_typed
= true;
2317 /* If we find a statement that can not be followed by an IMPLICIT statement
2318 (and thus we can expect to see none any further), type the function result
2319 if it has not yet been typed. Be careful not to give the END statement
2320 to verify_st_order! */
2321 if (!function_result_typed
&& st
!= ST_GET_FCN_CHARACTERISTICS
)
2323 bool verify_now
= false;
2325 if (st
== ST_END_FUNCTION
)
2330 verify_st_order (&dummyss
, ST_NONE
, false);
2331 verify_st_order (&dummyss
, st
, false);
2333 if (verify_st_order (&dummyss
, ST_IMPLICIT
, true) == FAILURE
)
2339 check_function_result_typed ();
2340 function_result_typed
= true;
2349 case ST_IMPLICIT_NONE
:
2351 if (!function_result_typed
)
2353 check_function_result_typed ();
2354 function_result_typed
= true;
2360 case ST_DATA
: /* Not allowed in interfaces */
2361 if (gfc_current_state () == COMP_INTERFACE
)
2371 case ST_DERIVED_DECL
:
2374 if (verify_st_order (&ss
, st
, false) == FAILURE
)
2376 reject_statement ();
2377 st
= next_statement ();
2387 case ST_DERIVED_DECL
:
2393 if (gfc_current_state () != COMP_MODULE
)
2395 gfc_error ("%s statement must appear in a MODULE",
2396 gfc_ascii_statement (st
));
2400 if (gfc_current_ns
->default_access
!= ACCESS_UNKNOWN
)
2402 gfc_error ("%s statement at %C follows another accessibility "
2403 "specification", gfc_ascii_statement (st
));
2407 gfc_current_ns
->default_access
= (st
== ST_PUBLIC
)
2408 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
2412 case ST_STATEMENT_FUNCTION
:
2413 if (gfc_current_state () == COMP_MODULE
)
2415 unexpected_statement (st
);
2423 accept_statement (st
);
2424 st
= next_statement ();
2428 accept_statement (st
);
2430 st
= next_statement ();
2433 case ST_GET_FCN_CHARACTERISTICS
:
2434 /* This statement triggers the association of a function's result
2436 ts
= &gfc_current_block ()->result
->ts
;
2437 if (match_deferred_characteristics (ts
) != MATCH_YES
)
2438 bad_characteristic
= true;
2440 st
= next_statement ();
2447 /* If match_deferred_characteristics failed, then there is an error. */
2448 if (bad_characteristic
)
2450 ts
= &gfc_current_block ()->result
->ts
;
2451 if (ts
->type
!= BT_DERIVED
)
2452 gfc_error ("Bad kind expression for function '%s' at %L",
2453 gfc_current_block ()->name
,
2454 &gfc_current_block ()->declared_at
);
2456 gfc_error ("The type for function '%s' at %L is not accessible",
2457 gfc_current_block ()->name
,
2458 &gfc_current_block ()->declared_at
);
2460 gfc_current_block ()->ts
.kind
= 0;
2461 /* Keep the derived type; if it's bad, it will be discovered later. */
2462 if (!(ts
->type
== BT_DERIVED
&& ts
->derived
))
2463 ts
->type
= BT_UNKNOWN
;
2470 /* Parse a WHERE block, (not a simple WHERE statement). */
2473 parse_where_block (void)
2475 int seen_empty_else
;
2480 accept_statement (ST_WHERE_BLOCK
);
2481 top
= gfc_state_stack
->tail
;
2483 push_state (&s
, COMP_WHERE
, gfc_new_block
);
2485 d
= add_statement ();
2486 d
->expr
= top
->expr
;
2492 seen_empty_else
= 0;
2496 st
= next_statement ();
2502 case ST_WHERE_BLOCK
:
2503 parse_where_block ();
2508 accept_statement (st
);
2512 if (seen_empty_else
)
2514 gfc_error ("ELSEWHERE statement at %C follows previous "
2515 "unmasked ELSEWHERE");
2519 if (new_st
.expr
== NULL
)
2520 seen_empty_else
= 1;
2522 d
= new_level (gfc_state_stack
->head
);
2524 d
->expr
= new_st
.expr
;
2526 accept_statement (st
);
2531 accept_statement (st
);
2535 gfc_error ("Unexpected %s statement in WHERE block at %C",
2536 gfc_ascii_statement (st
));
2537 reject_statement ();
2541 while (st
!= ST_END_WHERE
);
2547 /* Parse a FORALL block (not a simple FORALL statement). */
2550 parse_forall_block (void)
2556 accept_statement (ST_FORALL_BLOCK
);
2557 top
= gfc_state_stack
->tail
;
2559 push_state (&s
, COMP_FORALL
, gfc_new_block
);
2561 d
= add_statement ();
2562 d
->op
= EXEC_FORALL
;
2567 st
= next_statement ();
2572 case ST_POINTER_ASSIGNMENT
:
2575 accept_statement (st
);
2578 case ST_WHERE_BLOCK
:
2579 parse_where_block ();
2582 case ST_FORALL_BLOCK
:
2583 parse_forall_block ();
2587 accept_statement (st
);
2594 gfc_error ("Unexpected %s statement in FORALL block at %C",
2595 gfc_ascii_statement (st
));
2597 reject_statement ();
2601 while (st
!= ST_END_FORALL
);
2607 static gfc_statement
parse_executable (gfc_statement
);
2609 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2612 parse_if_block (void)
2621 accept_statement (ST_IF_BLOCK
);
2623 top
= gfc_state_stack
->tail
;
2624 push_state (&s
, COMP_IF
, gfc_new_block
);
2626 new_st
.op
= EXEC_IF
;
2627 d
= add_statement ();
2629 d
->expr
= top
->expr
;
2635 st
= parse_executable (ST_NONE
);
2645 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2646 "statement at %L", &else_locus
);
2648 reject_statement ();
2652 d
= new_level (gfc_state_stack
->head
);
2654 d
->expr
= new_st
.expr
;
2656 accept_statement (st
);
2663 gfc_error ("Duplicate ELSE statements at %L and %C",
2665 reject_statement ();
2670 else_locus
= gfc_current_locus
;
2672 d
= new_level (gfc_state_stack
->head
);
2675 accept_statement (st
);
2683 unexpected_statement (st
);
2687 while (st
!= ST_ENDIF
);
2690 accept_statement (st
);
2694 /* Parse a SELECT block. */
2697 parse_select_block (void)
2703 accept_statement (ST_SELECT_CASE
);
2705 cp
= gfc_state_stack
->tail
;
2706 push_state (&s
, COMP_SELECT
, gfc_new_block
);
2708 /* Make sure that the next statement is a CASE or END SELECT. */
2711 st
= next_statement ();
2714 if (st
== ST_END_SELECT
)
2716 /* Empty SELECT CASE is OK. */
2717 accept_statement (st
);
2724 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2727 reject_statement ();
2730 /* At this point, we're got a nonempty select block. */
2731 cp
= new_level (cp
);
2734 accept_statement (st
);
2738 st
= parse_executable (ST_NONE
);
2745 cp
= new_level (gfc_state_stack
->head
);
2747 gfc_clear_new_st ();
2749 accept_statement (st
);
2755 /* Can't have an executable statement because of
2756 parse_executable(). */
2758 unexpected_statement (st
);
2762 while (st
!= ST_END_SELECT
);
2765 accept_statement (st
);
2769 /* Given a symbol, make sure it is not an iteration variable for a DO
2770 statement. This subroutine is called when the symbol is seen in a
2771 context that causes it to become redefined. If the symbol is an
2772 iterator, we generate an error message and return nonzero. */
2775 gfc_check_do_variable (gfc_symtree
*st
)
2779 for (s
=gfc_state_stack
; s
; s
= s
->previous
)
2780 if (s
->do_variable
== st
)
2782 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2783 "loop beginning at %L", st
->name
, &s
->head
->loc
);
2791 /* Checks to see if the current statement label closes an enddo.
2792 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2793 an error) if it incorrectly closes an ENDDO. */
2796 check_do_closure (void)
2800 if (gfc_statement_label
== NULL
)
2803 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
2804 if (p
->state
== COMP_DO
)
2808 return 0; /* No loops to close */
2810 if (p
->ext
.end_do_label
== gfc_statement_label
)
2813 if (p
== gfc_state_stack
)
2816 gfc_error ("End of nonblock DO statement at %C is within another block");
2820 /* At this point, the label doesn't terminate the innermost loop.
2821 Make sure it doesn't terminate another one. */
2822 for (; p
; p
= p
->previous
)
2823 if (p
->state
== COMP_DO
&& p
->ext
.end_do_label
== gfc_statement_label
)
2825 gfc_error ("End of nonblock DO statement at %C is interwoven "
2826 "with another DO loop");
2834 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2835 handled inside of parse_executable(), because they aren't really
2839 parse_do_block (void)
2846 s
.ext
.end_do_label
= new_st
.label
;
2848 if (new_st
.ext
.iterator
!= NULL
)
2849 stree
= new_st
.ext
.iterator
->var
->symtree
;
2853 accept_statement (ST_DO
);
2855 top
= gfc_state_stack
->tail
;
2856 push_state (&s
, COMP_DO
, gfc_new_block
);
2858 s
.do_variable
= stree
;
2860 top
->block
= new_level (top
);
2861 top
->block
->op
= EXEC_DO
;
2864 st
= parse_executable (ST_NONE
);
2872 if (s
.ext
.end_do_label
!= NULL
2873 && s
.ext
.end_do_label
!= gfc_statement_label
)
2874 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
2877 if (gfc_statement_label
!= NULL
)
2879 new_st
.op
= EXEC_NOP
;
2884 case ST_IMPLIED_ENDDO
:
2885 /* If the do-stmt of this DO construct has a do-construct-name,
2886 the corresponding end-do must be an end-do-stmt (with a matching
2887 name, but in that case we must have seen ST_ENDDO first).
2888 We only complain about this in pedantic mode. */
2889 if (gfc_current_block () != NULL
)
2890 gfc_error_now ("named block DO at %L requires matching ENDDO name",
2891 &gfc_current_block()->declared_at
);
2896 unexpected_statement (st
);
2901 accept_statement (st
);
2905 /* Parse the statements of OpenMP do/parallel do. */
2907 static gfc_statement
2908 parse_omp_do (gfc_statement omp_st
)
2914 accept_statement (omp_st
);
2916 cp
= gfc_state_stack
->tail
;
2917 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
2918 np
= new_level (cp
);
2924 st
= next_statement ();
2927 else if (st
== ST_DO
)
2930 unexpected_statement (st
);
2934 if (gfc_statement_label
!= NULL
2935 && gfc_state_stack
->previous
!= NULL
2936 && gfc_state_stack
->previous
->state
== COMP_DO
2937 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
2945 there should be no !$OMP END DO. */
2947 return ST_IMPLIED_ENDDO
;
2950 check_do_closure ();
2953 st
= next_statement ();
2954 if (st
== (omp_st
== ST_OMP_DO
? ST_OMP_END_DO
: ST_OMP_END_PARALLEL_DO
))
2956 if (new_st
.op
== EXEC_OMP_END_NOWAIT
)
2957 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
2959 gcc_assert (new_st
.op
== EXEC_NOP
);
2960 gfc_clear_new_st ();
2961 gfc_commit_symbols ();
2962 gfc_warning_check ();
2963 st
= next_statement ();
2969 /* Parse the statements of OpenMP atomic directive. */
2972 parse_omp_atomic (void)
2978 accept_statement (ST_OMP_ATOMIC
);
2980 cp
= gfc_state_stack
->tail
;
2981 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
2982 np
= new_level (cp
);
2988 st
= next_statement ();
2991 else if (st
== ST_ASSIGNMENT
)
2994 unexpected_statement (st
);
2997 accept_statement (st
);
3003 /* Parse the statements of an OpenMP structured block. */
3006 parse_omp_structured_block (gfc_statement omp_st
, bool workshare_stmts_only
)
3008 gfc_statement st
, omp_end_st
;
3012 accept_statement (omp_st
);
3014 cp
= gfc_state_stack
->tail
;
3015 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
3016 np
= new_level (cp
);
3022 case ST_OMP_PARALLEL
:
3023 omp_end_st
= ST_OMP_END_PARALLEL
;
3025 case ST_OMP_PARALLEL_SECTIONS
:
3026 omp_end_st
= ST_OMP_END_PARALLEL_SECTIONS
;
3028 case ST_OMP_SECTIONS
:
3029 omp_end_st
= ST_OMP_END_SECTIONS
;
3031 case ST_OMP_ORDERED
:
3032 omp_end_st
= ST_OMP_END_ORDERED
;
3034 case ST_OMP_CRITICAL
:
3035 omp_end_st
= ST_OMP_END_CRITICAL
;
3038 omp_end_st
= ST_OMP_END_MASTER
;
3041 omp_end_st
= ST_OMP_END_SINGLE
;
3044 omp_end_st
= ST_OMP_END_TASK
;
3046 case ST_OMP_WORKSHARE
:
3047 omp_end_st
= ST_OMP_END_WORKSHARE
;
3049 case ST_OMP_PARALLEL_WORKSHARE
:
3050 omp_end_st
= ST_OMP_END_PARALLEL_WORKSHARE
;
3058 if (workshare_stmts_only
)
3060 /* Inside of !$omp workshare, only
3063 where statements and constructs
3064 forall statements and constructs
3068 are allowed. For !$omp critical these
3069 restrictions apply recursively. */
3072 st
= next_statement ();
3083 accept_statement (st
);
3086 case ST_WHERE_BLOCK
:
3087 parse_where_block ();
3090 case ST_FORALL_BLOCK
:
3091 parse_forall_block ();
3094 case ST_OMP_PARALLEL
:
3095 case ST_OMP_PARALLEL_SECTIONS
:
3096 parse_omp_structured_block (st
, false);
3099 case ST_OMP_PARALLEL_WORKSHARE
:
3100 case ST_OMP_CRITICAL
:
3101 parse_omp_structured_block (st
, true);
3104 case ST_OMP_PARALLEL_DO
:
3105 st
= parse_omp_do (st
);
3109 parse_omp_atomic ();
3120 st
= next_statement ();
3124 st
= parse_executable (ST_NONE
);
3127 else if (st
== ST_OMP_SECTION
3128 && (omp_st
== ST_OMP_SECTIONS
3129 || omp_st
== ST_OMP_PARALLEL_SECTIONS
))
3131 np
= new_level (np
);
3135 else if (st
!= omp_end_st
)
3136 unexpected_statement (st
);
3138 while (st
!= omp_end_st
);
3142 case EXEC_OMP_END_NOWAIT
:
3143 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
3145 case EXEC_OMP_CRITICAL
:
3146 if (((cp
->ext
.omp_name
== NULL
) ^ (new_st
.ext
.omp_name
== NULL
))
3147 || (new_st
.ext
.omp_name
!= NULL
3148 && strcmp (cp
->ext
.omp_name
, new_st
.ext
.omp_name
) != 0))
3149 gfc_error ("Name after !$omp critical and !$omp end critical does "
3151 gfc_free (CONST_CAST (char *, new_st
.ext
.omp_name
));
3153 case EXEC_OMP_END_SINGLE
:
3154 cp
->ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
]
3155 = new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
];
3156 new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
] = NULL
;
3157 gfc_free_omp_clauses (new_st
.ext
.omp_clauses
);
3165 gfc_clear_new_st ();
3166 gfc_commit_symbols ();
3167 gfc_warning_check ();
3172 /* Accept a series of executable statements. We return the first
3173 statement that doesn't fit to the caller. Any block statements are
3174 passed on to the correct handler, which usually passes the buck
3177 static gfc_statement
3178 parse_executable (gfc_statement st
)
3183 st
= next_statement ();
3187 close_flag
= check_do_closure ();
3192 case ST_END_PROGRAM
:
3195 case ST_END_FUNCTION
:
3199 case ST_END_SUBROUTINE
:
3204 case ST_SELECT_CASE
:
3205 gfc_error ("%s statement at %C cannot terminate a non-block "
3206 "DO loop", gfc_ascii_statement (st
));
3222 accept_statement (st
);
3223 if (close_flag
== 1)
3224 return ST_IMPLIED_ENDDO
;
3231 case ST_SELECT_CASE
:
3232 parse_select_block ();
3237 if (check_do_closure () == 1)
3238 return ST_IMPLIED_ENDDO
;
3241 case ST_WHERE_BLOCK
:
3242 parse_where_block ();
3245 case ST_FORALL_BLOCK
:
3246 parse_forall_block ();
3249 case ST_OMP_PARALLEL
:
3250 case ST_OMP_PARALLEL_SECTIONS
:
3251 case ST_OMP_SECTIONS
:
3252 case ST_OMP_ORDERED
:
3253 case ST_OMP_CRITICAL
:
3257 parse_omp_structured_block (st
, false);
3260 case ST_OMP_WORKSHARE
:
3261 case ST_OMP_PARALLEL_WORKSHARE
:
3262 parse_omp_structured_block (st
, true);
3266 case ST_OMP_PARALLEL_DO
:
3267 st
= parse_omp_do (st
);
3268 if (st
== ST_IMPLIED_ENDDO
)
3273 parse_omp_atomic ();
3280 st
= next_statement ();
3285 /* Parse a series of contained program units. */
3287 static void parse_progunit (gfc_statement
);
3290 /* Fix the symbols for sibling functions. These are incorrectly added to
3291 the child namespace as the parser didn't know about this procedure. */
3294 gfc_fixup_sibling_symbols (gfc_symbol
*sym
, gfc_namespace
*siblings
)
3298 gfc_symbol
*old_sym
;
3300 sym
->attr
.referenced
= 1;
3301 for (ns
= siblings
; ns
; ns
= ns
->sibling
)
3303 gfc_find_sym_tree (sym
->name
, ns
, 0, &st
);
3305 if (!st
|| (st
->n
.sym
->attr
.dummy
&& ns
== st
->n
.sym
->ns
))
3308 old_sym
= st
->n
.sym
;
3309 if (old_sym
->ns
== ns
3310 && !old_sym
->attr
.contained
3312 /* By 14.6.1.3, host association should be excluded
3313 for the following. */
3314 && !(old_sym
->attr
.external
3315 || (old_sym
->ts
.type
!= BT_UNKNOWN
3316 && !old_sym
->attr
.implicit_type
)
3317 || old_sym
->attr
.flavor
== FL_PARAMETER
3318 || old_sym
->attr
.in_common
3319 || old_sym
->attr
.in_equivalence
3320 || old_sym
->attr
.data
3321 || old_sym
->attr
.dummy
3322 || old_sym
->attr
.result
3323 || old_sym
->attr
.dimension
3324 || old_sym
->attr
.allocatable
3325 || old_sym
->attr
.intrinsic
3326 || old_sym
->attr
.generic
3327 || old_sym
->attr
.flavor
== FL_NAMELIST
3328 || old_sym
->attr
.proc
== PROC_ST_FUNCTION
))
3330 /* Replace it with the symbol from the parent namespace. */
3334 /* Free the old (local) symbol. */
3336 if (old_sym
->refs
== 0)
3337 gfc_free_symbol (old_sym
);
3340 /* Do the same for any contained procedures. */
3341 gfc_fixup_sibling_symbols (sym
, ns
->contained
);
3346 parse_contained (int module
)
3348 gfc_namespace
*ns
, *parent_ns
, *tmp
;
3349 gfc_state_data s1
, s2
;
3353 int contains_statements
= 0;
3356 push_state (&s1
, COMP_CONTAINS
, NULL
);
3357 parent_ns
= gfc_current_ns
;
3361 gfc_current_ns
= gfc_get_namespace (parent_ns
, 1);
3363 gfc_current_ns
->sibling
= parent_ns
->contained
;
3364 parent_ns
->contained
= gfc_current_ns
;
3367 /* Process the next available statement. We come here if we got an error
3368 and rejected the last statement. */
3369 st
= next_statement ();
3378 contains_statements
= 1;
3379 accept_statement (st
);
3382 (st
== ST_FUNCTION
) ? COMP_FUNCTION
: COMP_SUBROUTINE
,
3385 /* For internal procedures, create/update the symbol in the
3386 parent namespace. */
3390 if (gfc_get_symbol (gfc_new_block
->name
, parent_ns
, &sym
))
3391 gfc_error ("Contained procedure '%s' at %C is already "
3392 "ambiguous", gfc_new_block
->name
);
3395 if (gfc_add_procedure (&sym
->attr
, PROC_INTERNAL
, sym
->name
,
3396 &gfc_new_block
->declared_at
) ==
3399 if (st
== ST_FUNCTION
)
3400 gfc_add_function (&sym
->attr
, sym
->name
,
3401 &gfc_new_block
->declared_at
);
3403 gfc_add_subroutine (&sym
->attr
, sym
->name
,
3404 &gfc_new_block
->declared_at
);
3408 gfc_commit_symbols ();
3411 sym
= gfc_new_block
;
3413 /* Mark this as a contained function, so it isn't replaced
3414 by other module functions. */
3415 sym
->attr
.contained
= 1;
3416 sym
->attr
.referenced
= 1;
3418 parse_progunit (ST_NONE
);
3420 /* Fix up any sibling functions that refer to this one. */
3421 gfc_fixup_sibling_symbols (sym
, gfc_current_ns
);
3422 /* Or refer to any of its alternate entry points. */
3423 for (el
= gfc_current_ns
->entries
; el
; el
= el
->next
)
3424 gfc_fixup_sibling_symbols (el
->sym
, gfc_current_ns
);
3426 gfc_current_ns
->code
= s2
.head
;
3427 gfc_current_ns
= parent_ns
;
3432 /* These statements are associated with the end of the host unit. */
3433 case ST_END_FUNCTION
:
3435 case ST_END_PROGRAM
:
3436 case ST_END_SUBROUTINE
:
3437 accept_statement (st
);
3441 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
3442 gfc_ascii_statement (st
));
3443 reject_statement ();
3449 while (st
!= ST_END_FUNCTION
&& st
!= ST_END_SUBROUTINE
3450 && st
!= ST_END_MODULE
&& st
!= ST_END_PROGRAM
);
3452 /* The first namespace in the list is guaranteed to not have
3453 anything (worthwhile) in it. */
3454 tmp
= gfc_current_ns
;
3455 gfc_current_ns
= parent_ns
;
3456 if (seen_error
&& tmp
->refs
> 1)
3457 gfc_free_namespace (tmp
);
3459 ns
= gfc_current_ns
->contained
;
3460 gfc_current_ns
->contained
= ns
->sibling
;
3461 gfc_free_namespace (ns
);
3464 if (!contains_statements
)
3465 gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: CONTAINS statement without "
3466 "FUNCTION or SUBROUTINE statement at %C");
3470 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
3473 parse_progunit (gfc_statement st
)
3478 st
= parse_spec (st
);
3488 accept_statement (st
);
3495 if (gfc_current_state () == COMP_FUNCTION
)
3496 gfc_check_function_type (gfc_current_ns
);
3501 st
= parse_executable (st
);
3512 accept_statement (st
);
3519 unexpected_statement (st
);
3520 reject_statement ();
3521 st
= next_statement ();
3527 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
3528 if (p
->state
== COMP_CONTAINS
)
3531 if (gfc_find_state (COMP_MODULE
) == SUCCESS
)
3536 gfc_error ("CONTAINS statement at %C is already in a contained "
3538 st
= next_statement ();
3542 parse_contained (0);
3545 gfc_current_ns
->code
= gfc_state_stack
->head
;
3549 /* Come here to complain about a global symbol already in use as
3553 gfc_global_used (gfc_gsymbol
*sym
, locus
*where
)
3558 where
= &gfc_current_locus
;
3568 case GSYM_SUBROUTINE
:
3569 name
= "SUBROUTINE";
3574 case GSYM_BLOCK_DATA
:
3575 name
= "BLOCK DATA";
3581 gfc_internal_error ("gfc_global_used(): Bad type");
3585 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
3586 sym
->name
, where
, name
, &sym
->where
);
3590 /* Parse a block data program unit. */
3593 parse_block_data (void)
3596 static locus blank_locus
;
3597 static int blank_block
=0;
3600 gfc_current_ns
->proc_name
= gfc_new_block
;
3601 gfc_current_ns
->is_block_data
= 1;
3603 if (gfc_new_block
== NULL
)
3606 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3607 "prior BLOCK DATA at %L", &blank_locus
);
3611 blank_locus
= gfc_current_locus
;
3616 s
= gfc_get_gsymbol (gfc_new_block
->name
);
3618 || (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_BLOCK_DATA
))
3619 gfc_global_used(s
, NULL
);
3622 s
->type
= GSYM_BLOCK_DATA
;
3623 s
->where
= gfc_current_locus
;
3628 st
= parse_spec (ST_NONE
);
3630 while (st
!= ST_END_BLOCK_DATA
)
3632 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3633 gfc_ascii_statement (st
));
3634 reject_statement ();
3635 st
= next_statement ();
3640 /* Parse a module subprogram. */
3648 s
= gfc_get_gsymbol (gfc_new_block
->name
);
3649 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_MODULE
))
3650 gfc_global_used(s
, NULL
);
3653 s
->type
= GSYM_MODULE
;
3654 s
->where
= gfc_current_locus
;
3658 st
= parse_spec (ST_NONE
);
3667 parse_contained (1);
3671 accept_statement (st
);
3675 gfc_error ("Unexpected %s statement in MODULE at %C",
3676 gfc_ascii_statement (st
));
3678 reject_statement ();
3679 st
= next_statement ();
3685 /* Add a procedure name to the global symbol table. */
3688 add_global_procedure (int sub
)
3692 s
= gfc_get_gsymbol(gfc_new_block
->name
);
3695 || (s
->type
!= GSYM_UNKNOWN
3696 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
3697 gfc_global_used(s
, NULL
);
3700 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
3701 s
->where
= gfc_current_locus
;
3707 /* Add a program to the global symbol table. */
3710 add_global_program (void)
3714 if (gfc_new_block
== NULL
)
3716 s
= gfc_get_gsymbol (gfc_new_block
->name
);
3718 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_PROGRAM
))
3719 gfc_global_used(s
, NULL
);
3722 s
->type
= GSYM_PROGRAM
;
3723 s
->where
= gfc_current_locus
;
3729 /* Top level parser. */
3732 gfc_parse_file (void)
3734 int seen_program
, errors_before
, errors
;
3735 gfc_state_data top
, s
;
3739 gfc_start_source_files ();
3741 top
.state
= COMP_NONE
;
3743 top
.previous
= NULL
;
3744 top
.head
= top
.tail
= NULL
;
3745 top
.do_variable
= NULL
;
3747 gfc_state_stack
= &top
;
3749 gfc_clear_new_st ();
3751 gfc_statement_label
= NULL
;
3753 if (setjmp (eof_buf
))
3754 return FAILURE
; /* Come here on unexpected EOF */
3758 /* Exit early for empty files. */
3764 st
= next_statement ();
3773 goto duplicate_main
;
3775 prog_locus
= gfc_current_locus
;
3777 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
3778 main_program_symbol(gfc_current_ns
, gfc_new_block
->name
);
3779 accept_statement (st
);
3780 add_global_program ();
3781 parse_progunit (ST_NONE
);
3785 add_global_procedure (1);
3786 push_state (&s
, COMP_SUBROUTINE
, gfc_new_block
);
3787 accept_statement (st
);
3788 parse_progunit (ST_NONE
);
3792 add_global_procedure (0);
3793 push_state (&s
, COMP_FUNCTION
, gfc_new_block
);
3794 accept_statement (st
);
3795 parse_progunit (ST_NONE
);
3799 push_state (&s
, COMP_BLOCK_DATA
, gfc_new_block
);
3800 accept_statement (st
);
3801 parse_block_data ();
3805 push_state (&s
, COMP_MODULE
, gfc_new_block
);
3806 accept_statement (st
);
3808 gfc_get_errors (NULL
, &errors_before
);
3812 /* Anything else starts a nameless main program block. */
3815 goto duplicate_main
;
3817 prog_locus
= gfc_current_locus
;
3819 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
3820 main_program_symbol (gfc_current_ns
, "MAIN__");
3821 parse_progunit (st
);
3825 gfc_current_ns
->code
= s
.head
;
3827 gfc_resolve (gfc_current_ns
);
3829 /* Dump the parse tree if requested. */
3830 if (gfc_option
.dump_parse_tree
)
3831 gfc_dump_parse_tree (gfc_current_ns
, stdout
);
3833 gfc_get_errors (NULL
, &errors
);
3834 if (s
.state
== COMP_MODULE
)
3836 gfc_dump_module (s
.sym
->name
, errors_before
== errors
);
3838 gfc_generate_module_code (gfc_current_ns
);
3843 gfc_generate_code (gfc_current_ns
);
3851 gfc_end_source_files ();
3855 /* If we see a duplicate main program, shut down. If the second
3856 instance is an implied main program, i.e. data decls or executable
3857 statements, we're in for lots of errors. */
3858 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus
);
3859 reject_statement ();