2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation,
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
31 /* Current statement label. Zero means no statement label. Because
32 new_st can get wiped during statement matching, we have to keep it
35 gfc_st_label
*gfc_statement_label
;
37 static locus label_locus
;
38 static jmp_buf eof_buf
;
40 gfc_state_data
*gfc_state_stack
;
42 /* TODO: Re-order functions to kill these forward decls. */
43 static void check_statement_label (gfc_statement
);
44 static void undo_new_statement (void);
45 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 decode_statement (void)
101 gfc_clear_error (); /* Clear any pending errors. */
102 gfc_clear_warning (); /* Clear any pending warnings. */
104 if (gfc_match_eos () == MATCH_YES
)
107 old_locus
= gfc_current_locus
;
109 /* Try matching a data declaration or function declaration. The
110 input "REALFUNCTIONA(N)" can mean several things in different
111 contexts, so it (and its relatives) get special treatment. */
113 if (gfc_current_state () == COMP_NONE
114 || gfc_current_state () == COMP_INTERFACE
115 || gfc_current_state () == COMP_CONTAINS
)
117 m
= gfc_match_function_decl ();
120 else if (m
== MATCH_ERROR
)
124 gfc_current_locus
= old_locus
;
127 /* Match statements whose error messages are meant to be overwritten
128 by something better. */
130 match (NULL
, gfc_match_assignment
, ST_ASSIGNMENT
);
131 match (NULL
, gfc_match_pointer_assignment
, ST_POINTER_ASSIGNMENT
);
132 match (NULL
, gfc_match_st_function
, ST_STATEMENT_FUNCTION
);
134 match (NULL
, gfc_match_data_decl
, ST_DATA_DECL
);
135 match (NULL
, gfc_match_enumerator_def
, ST_ENUMERATOR
);
137 /* Try to match a subroutine statement, which has the same optional
138 prefixes that functions can have. */
140 if (gfc_match_subroutine () == MATCH_YES
)
141 return ST_SUBROUTINE
;
143 gfc_current_locus
= old_locus
;
145 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
146 might begin with a block label. The match functions for these
147 statements are unusual in that their keyword is not seen before
148 the matcher is called. */
150 if (gfc_match_if (&st
) == MATCH_YES
)
153 gfc_current_locus
= old_locus
;
155 if (gfc_match_where (&st
) == MATCH_YES
)
158 gfc_current_locus
= old_locus
;
160 if (gfc_match_forall (&st
) == MATCH_YES
)
163 gfc_current_locus
= old_locus
;
165 match (NULL
, gfc_match_do
, ST_DO
);
166 match (NULL
, gfc_match_select
, ST_SELECT_CASE
);
168 /* General statement matching: Instead of testing every possible
169 statement, we eliminate most possibilities by peeking at the
172 c
= gfc_peek_char ();
177 match ("allocate", gfc_match_allocate
, ST_ALLOCATE
);
178 match ("allocatable", gfc_match_allocatable
, ST_ATTR_DECL
);
179 match ("assign", gfc_match_assign
, ST_LABEL_ASSIGNMENT
);
183 match ("backspace", gfc_match_backspace
, ST_BACKSPACE
);
184 match ("block data", gfc_match_block_data
, ST_BLOCK_DATA
);
188 match ("call", gfc_match_call
, ST_CALL
);
189 match ("close", gfc_match_close
, ST_CLOSE
);
190 match ("continue", gfc_match_continue
, ST_CONTINUE
);
191 match ("cycle", gfc_match_cycle
, ST_CYCLE
);
192 match ("case", gfc_match_case
, ST_CASE
);
193 match ("common", gfc_match_common
, ST_COMMON
);
194 match ("contains", gfc_match_eos
, ST_CONTAINS
);
198 match ("deallocate", gfc_match_deallocate
, ST_DEALLOCATE
);
199 match ("data", gfc_match_data
, ST_DATA
);
200 match ("dimension", gfc_match_dimension
, ST_ATTR_DECL
);
204 match ("end file", gfc_match_endfile
, ST_END_FILE
);
205 match ("exit", gfc_match_exit
, ST_EXIT
);
206 match ("else", gfc_match_else
, ST_ELSE
);
207 match ("else where", gfc_match_elsewhere
, ST_ELSEWHERE
);
208 match ("else if", gfc_match_elseif
, ST_ELSEIF
);
209 match ("enum , bind ( c )", gfc_match_enum
, ST_ENUM
);
211 if (gfc_match_end (&st
) == MATCH_YES
)
214 match ("entry% ", gfc_match_entry
, ST_ENTRY
);
215 match ("equivalence", gfc_match_equivalence
, ST_EQUIVALENCE
);
216 match ("external", gfc_match_external
, ST_ATTR_DECL
);
220 match ("flush", gfc_match_flush
, ST_FLUSH
);
221 match ("format", gfc_match_format
, ST_FORMAT
);
225 match ("go to", gfc_match_goto
, ST_GOTO
);
229 match ("inquire", gfc_match_inquire
, ST_INQUIRE
);
230 match ("implicit", gfc_match_implicit
, ST_IMPLICIT
);
231 match ("implicit% none", gfc_match_implicit_none
, ST_IMPLICIT_NONE
);
232 match ("interface", gfc_match_interface
, ST_INTERFACE
);
233 match ("intent", gfc_match_intent
, ST_ATTR_DECL
);
234 match ("intrinsic", gfc_match_intrinsic
, ST_ATTR_DECL
);
238 match ("module% procedure% ", gfc_match_modproc
, ST_MODULE_PROC
);
239 match ("module", gfc_match_module
, ST_MODULE
);
243 match ("nullify", gfc_match_nullify
, ST_NULLIFY
);
244 match ("namelist", gfc_match_namelist
, ST_NAMELIST
);
248 match ("open", gfc_match_open
, ST_OPEN
);
249 match ("optional", gfc_match_optional
, ST_ATTR_DECL
);
253 match ("print", gfc_match_print
, ST_WRITE
);
254 match ("parameter", gfc_match_parameter
, ST_PARAMETER
);
255 match ("pause", gfc_match_pause
, ST_PAUSE
);
256 match ("pointer", gfc_match_pointer
, ST_ATTR_DECL
);
257 if (gfc_match_private (&st
) == MATCH_YES
)
259 match ("program", gfc_match_program
, ST_PROGRAM
);
260 if (gfc_match_public (&st
) == MATCH_YES
)
265 match ("read", gfc_match_read
, ST_READ
);
266 match ("return", gfc_match_return
, ST_RETURN
);
267 match ("rewind", gfc_match_rewind
, ST_REWIND
);
271 match ("sequence", gfc_match_eos
, ST_SEQUENCE
);
272 match ("stop", gfc_match_stop
, ST_STOP
);
273 match ("save", gfc_match_save
, ST_ATTR_DECL
);
277 match ("target", gfc_match_target
, ST_ATTR_DECL
);
278 match ("type", gfc_match_derived_decl
, ST_DERIVED_DECL
);
282 match ("use% ", gfc_match_use
, ST_USE
);
286 match ("write", gfc_match_write
, ST_WRITE
);
290 /* All else has failed, so give up. See if any of the matchers has
291 stored an error message of some sort. */
293 if (gfc_error_check () == 0)
294 gfc_error_now ("Unclassifiable statement at %C");
298 gfc_error_recovery ();
304 decode_omp_directive (void)
313 gfc_clear_error (); /* Clear any pending errors. */
314 gfc_clear_warning (); /* Clear any pending warnings. */
318 gfc_error_now ("OpenMP directives at %C may not appear in PURE or ELEMENTAL procedures");
319 gfc_error_recovery ();
323 old_locus
= gfc_current_locus
;
325 /* General OpenMP directive matching: Instead of testing every possible
326 statement, we eliminate most possibilities by peeking at the
329 c
= gfc_peek_char ();
334 match ("atomic", gfc_match_omp_atomic
, ST_OMP_ATOMIC
);
337 match ("barrier", gfc_match_omp_barrier
, ST_OMP_BARRIER
);
340 match ("critical", gfc_match_omp_critical
, ST_OMP_CRITICAL
);
343 match ("do", gfc_match_omp_do
, ST_OMP_DO
);
346 match ("end critical", gfc_match_omp_critical
, ST_OMP_END_CRITICAL
);
347 match ("end do", gfc_match_omp_end_nowait
, ST_OMP_END_DO
);
348 match ("end master", gfc_match_omp_eos
, ST_OMP_END_MASTER
);
349 match ("end ordered", gfc_match_omp_eos
, ST_OMP_END_ORDERED
);
350 match ("end parallel do", gfc_match_omp_eos
, ST_OMP_END_PARALLEL_DO
);
351 match ("end parallel sections", gfc_match_omp_eos
,
352 ST_OMP_END_PARALLEL_SECTIONS
);
353 match ("end parallel workshare", gfc_match_omp_eos
,
354 ST_OMP_END_PARALLEL_WORKSHARE
);
355 match ("end parallel", gfc_match_omp_eos
, ST_OMP_END_PARALLEL
);
356 match ("end sections", gfc_match_omp_end_nowait
, ST_OMP_END_SECTIONS
);
357 match ("end single", gfc_match_omp_end_single
, ST_OMP_END_SINGLE
);
358 match ("end workshare", gfc_match_omp_end_nowait
,
359 ST_OMP_END_WORKSHARE
);
362 match ("flush", gfc_match_omp_flush
, ST_OMP_FLUSH
);
365 match ("master", gfc_match_omp_master
, ST_OMP_MASTER
);
368 match ("ordered", gfc_match_omp_ordered
, ST_OMP_ORDERED
);
371 match ("parallel do", gfc_match_omp_parallel_do
, ST_OMP_PARALLEL_DO
);
372 match ("parallel sections", gfc_match_omp_parallel_sections
,
373 ST_OMP_PARALLEL_SECTIONS
);
374 match ("parallel workshare", gfc_match_omp_parallel_workshare
,
375 ST_OMP_PARALLEL_WORKSHARE
);
376 match ("parallel", gfc_match_omp_parallel
, ST_OMP_PARALLEL
);
379 match ("sections", gfc_match_omp_sections
, ST_OMP_SECTIONS
);
380 match ("section", gfc_match_omp_eos
, ST_OMP_SECTION
);
381 match ("single", gfc_match_omp_single
, ST_OMP_SINGLE
);
384 match ("threadprivate", gfc_match_omp_threadprivate
,
385 ST_OMP_THREADPRIVATE
);
387 match ("workshare", gfc_match_omp_workshare
, ST_OMP_WORKSHARE
);
391 /* All else has failed, so give up. See if any of the matchers has
392 stored an error message of some sort. */
394 if (gfc_error_check () == 0)
395 gfc_error_now ("Unclassifiable OpenMP directive at %C");
399 gfc_error_recovery ();
407 /* Get the next statement in free form source. */
415 gfc_gobble_whitespace ();
417 c
= gfc_peek_char ();
421 /* Found a statement label? */
422 m
= gfc_match_st_label (&gfc_statement_label
);
424 d
= gfc_peek_char ();
425 if (m
!= MATCH_YES
|| !gfc_is_whitespace (d
))
427 gfc_match_small_literal_int (&c
, &cnt
);
430 gfc_error_now ("Too many digits in statement label at %C");
433 gfc_error_now ("Statement label at %C is zero");
436 c
= gfc_next_char ();
439 if (!gfc_is_whitespace (c
))
440 gfc_error_now ("Non-numeric character in statement label at %C");
445 label_locus
= gfc_current_locus
;
447 gfc_gobble_whitespace ();
449 if (gfc_match_eos () == MATCH_YES
)
452 ("Ignoring statement label in empty statement at %C");
453 gfc_free_st_label (gfc_statement_label
);
454 gfc_statement_label
= NULL
;
461 /* Comments have already been skipped by the time we get here,
462 except for OpenMP directives. */
463 if (gfc_option
.flag_openmp
)
467 c
= gfc_next_char ();
468 for (i
= 0; i
< 5; i
++, c
= gfc_next_char ())
469 gcc_assert (c
== "!$omp"[i
]);
471 gcc_assert (c
== ' ');
472 return decode_omp_directive ();
476 return decode_statement ();
480 /* Get the next statement in fixed-form source. */
485 int label
, digit_flag
, i
;
490 return decode_statement ();
492 /* Skip past the current label field, parsing a statement label if
493 one is there. This is a weird number parser, since the number is
494 contained within five columns and can have any kind of embedded
495 spaces. We also check for characters that make the rest of the
501 for (i
= 0; i
< 5; i
++)
503 c
= gfc_next_char_literal (0);
520 label
= label
* 10 + c
- '0';
521 label_locus
= gfc_current_locus
;
525 /* Comments have already been skipped by the time we get
526 here, except for OpenMP directives. */
528 if (gfc_option
.flag_openmp
)
530 for (i
= 0; i
< 5; i
++, c
= gfc_next_char_literal (0))
531 gcc_assert (TOLOWER (c
) == "*$omp"[i
]);
533 if (c
!= ' ' && c
!= '0')
535 gfc_buffer_error (0);
536 gfc_error ("Bad continuation line at %C");
540 return decode_omp_directive ();
544 /* Comments have already been skipped by the time we get
545 here so don't bother checking for them. */
548 gfc_buffer_error (0);
549 gfc_error ("Non-numeric character in statement label at %C");
557 gfc_warning_now ("Zero is not a valid statement label at %C");
560 /* We've found a valid statement label. */
561 gfc_statement_label
= gfc_get_st_label (label
);
565 /* Since this line starts a statement, it cannot be a continuation
566 of a previous statement. If we see something here besides a
567 space or zero, it must be a bad continuation line. */
569 c
= gfc_next_char_literal (0);
573 if (c
!= ' ' && c
!= '0')
575 gfc_buffer_error (0);
576 gfc_error ("Bad continuation line at %C");
580 /* Now that we've taken care of the statement label columns, we have
581 to make sure that the first nonblank character is not a '!'. If
582 it is, the rest of the line is a comment. */
586 loc
= gfc_current_locus
;
587 c
= gfc_next_char_literal (0);
589 while (gfc_is_whitespace (c
));
593 gfc_current_locus
= loc
;
595 if (gfc_match_eos () == MATCH_YES
)
598 /* At this point, we've got a nonblank statement to parse. */
599 return decode_statement ();
603 gfc_warning ("Statement label in blank line will be ignored at %C");
609 /* Return the next non-ST_NONE statement to the caller. We also worry
610 about including files and the ends of include files at this stage. */
613 next_statement (void)
617 gfc_new_block
= NULL
;
621 gfc_statement_label
= NULL
;
622 gfc_buffer_error (1);
626 if (gfc_option
.warn_line_truncation
627 && gfc_current_locus
.lb
->truncated
)
628 gfc_warning_now ("Line truncated at %C");
633 gfc_skip_comments ();
642 (gfc_current_form
== FORM_FIXED
) ? next_fixed () : next_free ();
648 gfc_buffer_error (0);
651 check_statement_label (st
);
657 /****************************** Parser ***********************************/
659 /* The parser subroutines are of type 'try' that fail if the file ends
662 /* Macros that expand to case-labels for various classes of
663 statements. Start with executable statements that directly do
666 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
667 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
668 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
669 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
670 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
671 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
672 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
673 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
676 /* Statements that mark other executable statements. */
678 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
679 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
680 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
681 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
682 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
683 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
685 /* Declaration statements */
687 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
688 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
689 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE
691 /* Block end statements. Errors associated with interchanging these
692 are detected in gfc_match_end(). */
694 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
695 case ST_END_PROGRAM: case ST_END_SUBROUTINE
698 /* Push a new state onto the stack. */
701 push_state (gfc_state_data
* p
, gfc_compile_state new_state
, gfc_symbol
* sym
)
704 p
->state
= new_state
;
705 p
->previous
= gfc_state_stack
;
707 p
->head
= p
->tail
= NULL
;
708 p
->do_variable
= NULL
;
714 /* Pop the current state. */
720 gfc_state_stack
= gfc_state_stack
->previous
;
724 /* Try to find the given state in the state stack. */
727 gfc_find_state (gfc_compile_state state
)
731 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
732 if (p
->state
== state
)
735 return (p
== NULL
) ? FAILURE
: SUCCESS
;
739 /* Starts a new level in the statement list. */
742 new_level (gfc_code
* q
)
746 p
= q
->block
= gfc_get_code ();
748 gfc_state_stack
->head
= gfc_state_stack
->tail
= p
;
754 /* Add the current new_st code structure and adds it to the current
755 program unit. As a side-effect, it zeroes the new_st. */
765 p
->loc
= gfc_current_locus
;
767 if (gfc_state_stack
->head
== NULL
)
768 gfc_state_stack
->head
= p
;
770 gfc_state_stack
->tail
->next
= p
;
772 while (p
->next
!= NULL
)
775 gfc_state_stack
->tail
= p
;
783 /* Frees everything associated with the current statement. */
786 undo_new_statement (void)
788 gfc_free_statements (new_st
.block
);
789 gfc_free_statements (new_st
.next
);
790 gfc_free_statement (&new_st
);
795 /* If the current statement has a statement label, make sure that it
796 is allowed to, or should have one. */
799 check_statement_label (gfc_statement st
)
803 if (gfc_statement_label
== NULL
)
806 gfc_error ("FORMAT statement at %L does not have a statement label",
814 case ST_END_FUNCTION
:
815 case ST_END_SUBROUTINE
:
821 type
= ST_LABEL_TARGET
;
825 type
= ST_LABEL_FORMAT
;
828 /* Statement labels are not restricted from appearing on a
829 particular line. However, there are plenty of situations
830 where the resulting label can't be referenced. */
833 type
= ST_LABEL_BAD_TARGET
;
837 gfc_define_st_label (gfc_statement_label
, type
, &label_locus
);
839 new_st
.here
= gfc_statement_label
;
843 /* Figures out what the enclosing program unit is. This will be a
844 function, subroutine, program, block data or module. */
847 gfc_enclosing_unit (gfc_compile_state
* result
)
851 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
852 if (p
->state
== COMP_FUNCTION
|| p
->state
== COMP_SUBROUTINE
853 || p
->state
== COMP_MODULE
|| p
->state
== COMP_BLOCK_DATA
854 || p
->state
== COMP_PROGRAM
)
863 *result
= COMP_PROGRAM
;
868 /* Translate a statement enum to a string. */
871 gfc_ascii_statement (gfc_statement st
)
877 case ST_ARITHMETIC_IF
:
878 p
= _("arithmetic IF");
884 p
= _("attribute declaration");
914 p
= _("data declaration");
922 case ST_DERIVED_DECL
:
923 p
= _("derived type declaration");
937 case ST_END_BLOCK_DATA
:
938 p
= "END BLOCK DATA";
949 case ST_END_FUNCTION
:
955 case ST_END_INTERFACE
:
967 case ST_END_SUBROUTINE
:
968 p
= "END SUBROUTINE";
988 case ST_FORALL_BLOCK
: /* Fall through */
1007 case ST_IMPLICIT_NONE
:
1008 p
= "IMPLICIT NONE";
1010 case ST_IMPLIED_ENDDO
:
1011 p
= _("implied END DO");
1034 case ST_MODULE_PROC
:
1035 p
= "MODULE PROCEDURE";
1070 case ST_WHERE_BLOCK
: /* Fall through */
1078 p
= _("assignment");
1080 case ST_POINTER_ASSIGNMENT
:
1081 p
= _("pointer assignment");
1083 case ST_SELECT_CASE
:
1092 case ST_STATEMENT_FUNCTION
:
1093 p
= "STATEMENT FUNCTION";
1095 case ST_LABEL_ASSIGNMENT
:
1096 p
= "LABEL ASSIGNMENT";
1099 p
= "ENUM DEFINITION";
1102 p
= "ENUMERATOR DEFINITION";
1110 case ST_OMP_BARRIER
:
1111 p
= "!$OMP BARRIER";
1113 case ST_OMP_CRITICAL
:
1114 p
= "!$OMP CRITICAL";
1119 case ST_OMP_END_CRITICAL
:
1120 p
= "!$OMP END CRITICAL";
1125 case ST_OMP_END_MASTER
:
1126 p
= "!$OMP END MASTER";
1128 case ST_OMP_END_ORDERED
:
1129 p
= "!$OMP END ORDERED";
1131 case ST_OMP_END_PARALLEL
:
1132 p
= "!$OMP END PARALLEL";
1134 case ST_OMP_END_PARALLEL_DO
:
1135 p
= "!$OMP END PARALLEL DO";
1137 case ST_OMP_END_PARALLEL_SECTIONS
:
1138 p
= "!$OMP END PARALLEL SECTIONS";
1140 case ST_OMP_END_PARALLEL_WORKSHARE
:
1141 p
= "!$OMP END PARALLEL WORKSHARE";
1143 case ST_OMP_END_SECTIONS
:
1144 p
= "!$OMP END SECTIONS";
1146 case ST_OMP_END_SINGLE
:
1147 p
= "!$OMP END SINGLE";
1149 case ST_OMP_END_WORKSHARE
:
1150 p
= "!$OMP END WORKSHARE";
1158 case ST_OMP_ORDERED
:
1159 p
= "!$OMP ORDERED";
1161 case ST_OMP_PARALLEL
:
1162 p
= "!$OMP PARALLEL";
1164 case ST_OMP_PARALLEL_DO
:
1165 p
= "!$OMP PARALLEL DO";
1167 case ST_OMP_PARALLEL_SECTIONS
:
1168 p
= "!$OMP PARALLEL SECTIONS";
1170 case ST_OMP_PARALLEL_WORKSHARE
:
1171 p
= "!$OMP PARALLEL WORKSHARE";
1173 case ST_OMP_SECTIONS
:
1174 p
= "!$OMP SECTIONS";
1176 case ST_OMP_SECTION
:
1177 p
= "!$OMP SECTION";
1182 case ST_OMP_THREADPRIVATE
:
1183 p
= "!$OMP THREADPRIVATE";
1185 case ST_OMP_WORKSHARE
:
1186 p
= "!$OMP WORKSHARE";
1189 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1196 /* Create a symbol for the main program and assign it to ns->proc_name. */
1199 main_program_symbol (gfc_namespace
* ns
)
1201 gfc_symbol
*main_program
;
1202 symbol_attribute attr
;
1204 gfc_get_symbol ("MAIN__", ns
, &main_program
);
1205 gfc_clear_attr (&attr
);
1206 attr
.flavor
= FL_PROCEDURE
;
1207 attr
.proc
= PROC_UNKNOWN
;
1208 attr
.subroutine
= 1;
1209 attr
.access
= ACCESS_PUBLIC
;
1210 attr
.is_main_program
= 1;
1211 main_program
->attr
= attr
;
1212 main_program
->declared_at
= gfc_current_locus
;
1213 ns
->proc_name
= main_program
;
1214 gfc_commit_symbols ();
1218 /* Do whatever is necessary to accept the last statement. */
1221 accept_statement (gfc_statement st
)
1230 case ST_IMPLICIT_NONE
:
1231 gfc_set_implicit_none ();
1240 gfc_current_ns
->proc_name
= gfc_new_block
;
1243 /* If the statement is the end of a block, lay down a special code
1244 that allows a branch to the end of the block from within the
1249 if (gfc_statement_label
!= NULL
)
1251 new_st
.op
= EXEC_NOP
;
1257 /* The end-of-program unit statements do not get the special
1258 marker and require a statement of some sort if they are a
1261 case ST_END_PROGRAM
:
1262 case ST_END_FUNCTION
:
1263 case ST_END_SUBROUTINE
:
1264 if (gfc_statement_label
!= NULL
)
1266 new_st
.op
= EXEC_RETURN
;
1282 gfc_commit_symbols ();
1283 gfc_warning_check ();
1284 gfc_clear_new_st ();
1288 /* Undo anything tentative that has been built for the current
1292 reject_statement (void)
1295 gfc_undo_symbols ();
1296 gfc_clear_warning ();
1297 undo_new_statement ();
1301 /* Generic complaint about an out of order statement. We also do
1302 whatever is necessary to clean up. */
1305 unexpected_statement (gfc_statement st
)
1308 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st
));
1310 reject_statement ();
1314 /* Given the next statement seen by the matcher, make sure that it is
1315 in proper order with the last. This subroutine is initialized by
1316 calling it with an argument of ST_NONE. If there is a problem, we
1317 issue an error and return FAILURE. Otherwise we return SUCCESS.
1319 Individual parsers need to verify that the statements seen are
1320 valid before calling here, ie ENTRY statements are not allowed in
1321 INTERFACE blocks. The following diagram is taken from the standard:
1323 +---------------------------------------+
1324 | program subroutine function module |
1325 +---------------------------------------+
1327 |---------------------------------------+
1329 | +-----------+------------------+
1330 | | parameter | implicit |
1331 | +-----------+------------------+
1332 | format | | derived type |
1333 | entry | parameter | interface |
1334 | | data | specification |
1335 | | | statement func |
1336 | +-----------+------------------+
1337 | | data | executable |
1338 +--------+-----------+------------------+
1340 +---------------------------------------+
1341 | internal module/subprogram |
1342 +---------------------------------------+
1344 +---------------------------------------+
1351 { ORDER_START
, ORDER_USE
, ORDER_IMPLICIT_NONE
, ORDER_IMPLICIT
,
1352 ORDER_SPEC
, ORDER_EXEC
1355 gfc_statement last_statement
;
1361 verify_st_order (st_state
* p
, gfc_statement st
)
1367 p
->state
= ORDER_START
;
1371 if (p
->state
> ORDER_USE
)
1373 p
->state
= ORDER_USE
;
1376 case ST_IMPLICIT_NONE
:
1377 if (p
->state
> ORDER_IMPLICIT_NONE
)
1380 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1381 statement disqualifies a USE but not an IMPLICIT NONE.
1382 Duplicate IMPLICIT NONEs are caught when the implicit types
1385 p
->state
= ORDER_IMPLICIT_NONE
;
1389 if (p
->state
> ORDER_IMPLICIT
)
1391 p
->state
= ORDER_IMPLICIT
;
1396 if (p
->state
< ORDER_IMPLICIT_NONE
)
1397 p
->state
= ORDER_IMPLICIT_NONE
;
1401 if (p
->state
>= ORDER_EXEC
)
1403 if (p
->state
< ORDER_IMPLICIT
)
1404 p
->state
= ORDER_IMPLICIT
;
1408 if (p
->state
< ORDER_SPEC
)
1409 p
->state
= ORDER_SPEC
;
1414 case ST_DERIVED_DECL
:
1416 if (p
->state
>= ORDER_EXEC
)
1418 if (p
->state
< ORDER_SPEC
)
1419 p
->state
= ORDER_SPEC
;
1424 if (p
->state
< ORDER_EXEC
)
1425 p
->state
= ORDER_EXEC
;
1430 ("Unexpected %s statement in verify_st_order() at %C",
1431 gfc_ascii_statement (st
));
1434 /* All is well, record the statement in case we need it next time. */
1435 p
->where
= gfc_current_locus
;
1436 p
->last_statement
= st
;
1440 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1441 gfc_ascii_statement (st
),
1442 gfc_ascii_statement (p
->last_statement
), &p
->where
);
1448 /* Handle an unexpected end of file. This is a show-stopper... */
1450 static void unexpected_eof (void) ATTRIBUTE_NORETURN
;
1453 unexpected_eof (void)
1457 gfc_error ("Unexpected end of file in '%s'", gfc_source_file
);
1459 /* Memory cleanup. Move to "second to last". */
1460 for (p
= gfc_state_stack
; p
&& p
->previous
&& p
->previous
->previous
;
1463 gfc_current_ns
->code
= (p
&& p
->previous
) ? p
->head
: NULL
;
1466 longjmp (eof_buf
, 1);
1470 /* Parse a derived type. */
1473 parse_derived (void)
1475 int compiling_type
, seen_private
, seen_sequence
, seen_component
, error_flag
;
1482 accept_statement (ST_DERIVED_DECL
);
1483 push_state (&s
, COMP_DERIVED
, gfc_new_block
);
1485 gfc_new_block
->component_access
= ACCESS_PUBLIC
;
1492 while (compiling_type
)
1494 st
= next_statement ();
1501 accept_statement (st
);
1508 if (!seen_component
)
1510 gfc_error ("Derived type definition at %C has no components");
1514 accept_statement (ST_END_TYPE
);
1518 if (gfc_find_state (COMP_MODULE
) == FAILURE
)
1521 ("PRIVATE statement in TYPE at %C must be inside a MODULE");
1528 gfc_error ("PRIVATE statement at %C must precede "
1529 "structure components");
1536 gfc_error ("Duplicate PRIVATE statement at %C");
1540 s
.sym
->component_access
= ACCESS_PRIVATE
;
1541 accept_statement (ST_PRIVATE
);
1548 gfc_error ("SEQUENCE statement at %C must precede "
1549 "structure components");
1554 if (gfc_current_block ()->attr
.sequence
)
1555 gfc_warning ("SEQUENCE attribute at %C already specified in "
1560 gfc_error ("Duplicate SEQUENCE statement at %C");
1565 gfc_add_sequence (&gfc_current_block ()->attr
,
1566 gfc_current_block ()->name
, NULL
);
1570 unexpected_statement (st
);
1575 /* Sanity checks on the structure. If the structure has the
1576 SEQUENCE attribute, then all component structures must also have
1578 if (error_flag
== 0 && gfc_current_block ()->attr
.sequence
)
1579 for (c
= gfc_current_block ()->components
; c
; c
= c
->next
)
1581 if (c
->ts
.type
== BT_DERIVED
&& c
->ts
.derived
->attr
.sequence
== 0)
1584 ("Component %s of SEQUENCE type declared at %C does not "
1585 "have the SEQUENCE attribute", c
->ts
.derived
->name
);
1594 /* Parse an ENUM. */
1603 int seen_enumerator
= 0;
1607 push_state (&s
, COMP_ENUM
, gfc_new_block
);
1611 while (compiling_enum
)
1613 st
= next_statement ();
1621 seen_enumerator
= 1;
1622 accept_statement (st
);
1627 if (!seen_enumerator
)
1629 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1632 accept_statement (st
);
1636 gfc_free_enum_history ();
1637 unexpected_statement (st
);
1644 /* Parse an interface. We must be able to deal with the possibility
1645 of recursive interfaces. The parse_spec() subroutine is mutually
1646 recursive with parse_interface(). */
1648 static gfc_statement
parse_spec (gfc_statement
);
1651 parse_interface (void)
1653 gfc_compile_state new_state
, current_state
;
1654 gfc_symbol
*prog_unit
, *sym
;
1655 gfc_interface_info save
;
1656 gfc_state_data s1
, s2
;
1659 accept_statement (ST_INTERFACE
);
1661 current_interface
.ns
= gfc_current_ns
;
1662 save
= current_interface
;
1664 sym
= (current_interface
.type
== INTERFACE_GENERIC
1665 || current_interface
.type
== INTERFACE_USER_OP
) ? gfc_new_block
: NULL
;
1667 push_state (&s1
, COMP_INTERFACE
, sym
);
1668 current_state
= COMP_NONE
;
1671 gfc_current_ns
= gfc_get_namespace (current_interface
.ns
, 0);
1673 st
= next_statement ();
1680 new_state
= COMP_SUBROUTINE
;
1681 gfc_add_explicit_interface (gfc_new_block
, IFSRC_IFBODY
,
1682 gfc_new_block
->formal
, NULL
);
1686 new_state
= COMP_FUNCTION
;
1687 gfc_add_explicit_interface (gfc_new_block
, IFSRC_IFBODY
,
1688 gfc_new_block
->formal
, NULL
);
1691 case ST_MODULE_PROC
: /* The module procedure matcher makes
1692 sure the context is correct. */
1693 accept_statement (st
);
1694 gfc_free_namespace (gfc_current_ns
);
1697 case ST_END_INTERFACE
:
1698 gfc_free_namespace (gfc_current_ns
);
1699 gfc_current_ns
= current_interface
.ns
;
1703 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1704 gfc_ascii_statement (st
));
1705 reject_statement ();
1706 gfc_free_namespace (gfc_current_ns
);
1711 /* Make sure that a generic interface has only subroutines or
1712 functions and that the generic name has the right attribute. */
1713 if (current_interface
.type
== INTERFACE_GENERIC
)
1715 if (current_state
== COMP_NONE
)
1717 if (new_state
== COMP_FUNCTION
)
1718 gfc_add_function (&sym
->attr
, sym
->name
, NULL
);
1719 else if (new_state
== COMP_SUBROUTINE
)
1720 gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
);
1722 current_state
= new_state
;
1726 if (new_state
!= current_state
)
1728 if (new_state
== COMP_SUBROUTINE
)
1730 ("SUBROUTINE at %C does not belong in a generic function "
1733 if (new_state
== COMP_FUNCTION
)
1735 ("FUNCTION at %C does not belong in a generic subroutine "
1741 push_state (&s2
, new_state
, gfc_new_block
);
1742 accept_statement (st
);
1743 prog_unit
= gfc_new_block
;
1744 prog_unit
->formal_ns
= gfc_current_ns
;
1747 /* Read data declaration statements. */
1748 st
= parse_spec (ST_NONE
);
1750 if (st
!= ST_END_SUBROUTINE
&& st
!= ST_END_FUNCTION
)
1752 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1753 gfc_ascii_statement (st
));
1754 reject_statement ();
1758 current_interface
= save
;
1759 gfc_add_interface (prog_unit
);
1769 /* Parse a set of specification statements. Returns the statement
1770 that doesn't fit. */
1772 static gfc_statement
1773 parse_spec (gfc_statement st
)
1777 verify_st_order (&ss
, ST_NONE
);
1779 st
= next_statement ();
1789 case ST_DATA
: /* Not allowed in interfaces */
1790 if (gfc_current_state () == COMP_INTERFACE
)
1796 case ST_IMPLICIT_NONE
:
1801 case ST_DERIVED_DECL
:
1803 if (verify_st_order (&ss
, st
) == FAILURE
)
1805 reject_statement ();
1806 st
= next_statement ();
1816 case ST_DERIVED_DECL
:
1822 if (gfc_current_state () != COMP_MODULE
)
1824 gfc_error ("%s statement must appear in a MODULE",
1825 gfc_ascii_statement (st
));
1829 if (gfc_current_ns
->default_access
!= ACCESS_UNKNOWN
)
1831 gfc_error ("%s statement at %C follows another accessibility "
1832 "specification", gfc_ascii_statement (st
));
1836 gfc_current_ns
->default_access
= (st
== ST_PUBLIC
)
1837 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
1845 accept_statement (st
);
1846 st
= next_statement ();
1850 accept_statement (st
);
1852 st
= next_statement ();
1863 /* Parse a WHERE block, (not a simple WHERE statement). */
1866 parse_where_block (void)
1868 int seen_empty_else
;
1873 accept_statement (ST_WHERE_BLOCK
);
1874 top
= gfc_state_stack
->tail
;
1876 push_state (&s
, COMP_WHERE
, gfc_new_block
);
1878 d
= add_statement ();
1879 d
->expr
= top
->expr
;
1885 seen_empty_else
= 0;
1889 st
= next_statement ();
1895 case ST_WHERE_BLOCK
:
1896 parse_where_block ();
1901 accept_statement (st
);
1905 if (seen_empty_else
)
1908 ("ELSEWHERE statement at %C follows previous unmasked "
1913 if (new_st
.expr
== NULL
)
1914 seen_empty_else
= 1;
1916 d
= new_level (gfc_state_stack
->head
);
1918 d
->expr
= new_st
.expr
;
1920 accept_statement (st
);
1925 accept_statement (st
);
1929 gfc_error ("Unexpected %s statement in WHERE block at %C",
1930 gfc_ascii_statement (st
));
1931 reject_statement ();
1936 while (st
!= ST_END_WHERE
);
1942 /* Parse a FORALL block (not a simple FORALL statement). */
1945 parse_forall_block (void)
1951 accept_statement (ST_FORALL_BLOCK
);
1952 top
= gfc_state_stack
->tail
;
1954 push_state (&s
, COMP_FORALL
, gfc_new_block
);
1956 d
= add_statement ();
1957 d
->op
= EXEC_FORALL
;
1962 st
= next_statement ();
1967 case ST_POINTER_ASSIGNMENT
:
1970 accept_statement (st
);
1973 case ST_WHERE_BLOCK
:
1974 parse_where_block ();
1977 case ST_FORALL_BLOCK
:
1978 parse_forall_block ();
1982 accept_statement (st
);
1989 gfc_error ("Unexpected %s statement in FORALL block at %C",
1990 gfc_ascii_statement (st
));
1992 reject_statement ();
1996 while (st
!= ST_END_FORALL
);
2002 static gfc_statement
parse_executable (gfc_statement
);
2004 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2007 parse_if_block (void)
2016 accept_statement (ST_IF_BLOCK
);
2018 top
= gfc_state_stack
->tail
;
2019 push_state (&s
, COMP_IF
, gfc_new_block
);
2021 new_st
.op
= EXEC_IF
;
2022 d
= add_statement ();
2024 d
->expr
= top
->expr
;
2030 st
= parse_executable (ST_NONE
);
2041 ("ELSE IF statement at %C cannot follow ELSE statement at %L",
2044 reject_statement ();
2048 d
= new_level (gfc_state_stack
->head
);
2050 d
->expr
= new_st
.expr
;
2052 accept_statement (st
);
2059 gfc_error ("Duplicate ELSE statements at %L and %C",
2061 reject_statement ();
2066 else_locus
= gfc_current_locus
;
2068 d
= new_level (gfc_state_stack
->head
);
2071 accept_statement (st
);
2079 unexpected_statement (st
);
2083 while (st
!= ST_ENDIF
);
2086 accept_statement (st
);
2090 /* Parse a SELECT block. */
2093 parse_select_block (void)
2099 accept_statement (ST_SELECT_CASE
);
2101 cp
= gfc_state_stack
->tail
;
2102 push_state (&s
, COMP_SELECT
, gfc_new_block
);
2104 /* Make sure that the next statement is a CASE or END SELECT. */
2107 st
= next_statement ();
2110 if (st
== ST_END_SELECT
)
2112 /* Empty SELECT CASE is OK. */
2113 accept_statement (st
);
2121 ("Expected a CASE or END SELECT statement following SELECT CASE "
2124 reject_statement ();
2127 /* At this point, we're got a nonempty select block. */
2128 cp
= new_level (cp
);
2131 accept_statement (st
);
2135 st
= parse_executable (ST_NONE
);
2142 cp
= new_level (gfc_state_stack
->head
);
2144 gfc_clear_new_st ();
2146 accept_statement (st
);
2152 /* Can't have an executable statement because of
2153 parse_executable(). */
2155 unexpected_statement (st
);
2159 while (st
!= ST_END_SELECT
);
2162 accept_statement (st
);
2166 /* Given a symbol, make sure it is not an iteration variable for a DO
2167 statement. This subroutine is called when the symbol is seen in a
2168 context that causes it to become redefined. If the symbol is an
2169 iterator, we generate an error message and return nonzero. */
2172 gfc_check_do_variable (gfc_symtree
*st
)
2176 for (s
=gfc_state_stack
; s
; s
= s
->previous
)
2177 if (s
->do_variable
== st
)
2179 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2180 "loop beginning at %L", st
->name
, &s
->head
->loc
);
2188 /* Checks to see if the current statement label closes an enddo.
2189 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2190 an error) if it incorrectly closes an ENDDO. */
2193 check_do_closure (void)
2197 if (gfc_statement_label
== NULL
)
2200 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
2201 if (p
->state
== COMP_DO
)
2205 return 0; /* No loops to close */
2207 if (p
->ext
.end_do_label
== gfc_statement_label
)
2210 if (p
== gfc_state_stack
)
2214 ("End of nonblock DO statement at %C is within another block");
2218 /* At this point, the label doesn't terminate the innermost loop.
2219 Make sure it doesn't terminate another one. */
2220 for (; p
; p
= p
->previous
)
2221 if (p
->state
== COMP_DO
&& p
->ext
.end_do_label
== gfc_statement_label
)
2223 gfc_error ("End of nonblock DO statement at %C is interwoven "
2224 "with another DO loop");
2232 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2233 handled inside of parse_executable(), because they aren't really
2237 parse_do_block (void)
2244 s
.ext
.end_do_label
= new_st
.label
;
2246 if (new_st
.ext
.iterator
!= NULL
)
2247 stree
= new_st
.ext
.iterator
->var
->symtree
;
2251 accept_statement (ST_DO
);
2253 top
= gfc_state_stack
->tail
;
2254 push_state (&s
, COMP_DO
, gfc_new_block
);
2256 s
.do_variable
= stree
;
2258 top
->block
= new_level (top
);
2259 top
->block
->op
= EXEC_DO
;
2262 st
= parse_executable (ST_NONE
);
2270 if (s
.ext
.end_do_label
!= NULL
2271 && s
.ext
.end_do_label
!= gfc_statement_label
)
2273 ("Statement label in ENDDO at %C doesn't match DO label");
2275 if (gfc_statement_label
!= NULL
)
2277 new_st
.op
= EXEC_NOP
;
2282 case ST_IMPLIED_ENDDO
:
2286 unexpected_statement (st
);
2291 accept_statement (st
);
2295 /* Parse the statements of OpenMP do/parallel do. */
2297 static gfc_statement
2298 parse_omp_do (gfc_statement omp_st
)
2304 accept_statement (omp_st
);
2306 cp
= gfc_state_stack
->tail
;
2307 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
2308 np
= new_level (cp
);
2314 st
= next_statement ();
2317 else if (st
== ST_DO
)
2320 unexpected_statement (st
);
2324 if (gfc_statement_label
!= NULL
2325 && gfc_state_stack
->previous
!= NULL
2326 && gfc_state_stack
->previous
->state
== COMP_DO
2327 && gfc_state_stack
->previous
->ext
.end_do_label
== gfc_statement_label
)
2335 there should be no !$OMP END DO. */
2337 return ST_IMPLIED_ENDDO
;
2340 check_do_closure ();
2343 st
= next_statement ();
2344 if (st
== (omp_st
== ST_OMP_DO
? ST_OMP_END_DO
: ST_OMP_END_PARALLEL_DO
))
2346 if (new_st
.op
== EXEC_OMP_END_NOWAIT
)
2347 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
2349 gcc_assert (new_st
.op
== EXEC_NOP
);
2350 gfc_clear_new_st ();
2351 gfc_commit_symbols ();
2352 gfc_warning_check ();
2353 st
= next_statement ();
2359 /* Parse the statements of OpenMP atomic directive. */
2362 parse_omp_atomic (void)
2368 accept_statement (ST_OMP_ATOMIC
);
2370 cp
= gfc_state_stack
->tail
;
2371 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
2372 np
= new_level (cp
);
2378 st
= next_statement ();
2381 else if (st
== ST_ASSIGNMENT
)
2384 unexpected_statement (st
);
2387 accept_statement (st
);
2393 /* Parse the statements of an OpenMP structured block. */
2396 parse_omp_structured_block (gfc_statement omp_st
, bool workshare_stmts_only
)
2398 gfc_statement st
, omp_end_st
;
2402 accept_statement (omp_st
);
2404 cp
= gfc_state_stack
->tail
;
2405 push_state (&s
, COMP_OMP_STRUCTURED_BLOCK
, NULL
);
2406 np
= new_level (cp
);
2412 case ST_OMP_PARALLEL
:
2413 omp_end_st
= ST_OMP_END_PARALLEL
;
2415 case ST_OMP_PARALLEL_SECTIONS
:
2416 omp_end_st
= ST_OMP_END_PARALLEL_SECTIONS
;
2418 case ST_OMP_SECTIONS
:
2419 omp_end_st
= ST_OMP_END_SECTIONS
;
2421 case ST_OMP_ORDERED
:
2422 omp_end_st
= ST_OMP_END_ORDERED
;
2424 case ST_OMP_CRITICAL
:
2425 omp_end_st
= ST_OMP_END_CRITICAL
;
2428 omp_end_st
= ST_OMP_END_MASTER
;
2431 omp_end_st
= ST_OMP_END_SINGLE
;
2433 case ST_OMP_WORKSHARE
:
2434 omp_end_st
= ST_OMP_END_WORKSHARE
;
2436 case ST_OMP_PARALLEL_WORKSHARE
:
2437 omp_end_st
= ST_OMP_END_PARALLEL_WORKSHARE
;
2445 if (workshare_stmts_only
)
2447 /* Inside of !$omp workshare, only
2450 where statements and constructs
2451 forall statements and constructs
2455 are allowed. For !$omp critical these
2456 restrictions apply recursively. */
2459 st
= next_statement ();
2470 accept_statement (st
);
2473 case ST_WHERE_BLOCK
:
2474 parse_where_block ();
2477 case ST_FORALL_BLOCK
:
2478 parse_forall_block ();
2481 case ST_OMP_PARALLEL
:
2482 case ST_OMP_PARALLEL_SECTIONS
:
2483 parse_omp_structured_block (st
, false);
2486 case ST_OMP_PARALLEL_WORKSHARE
:
2487 case ST_OMP_CRITICAL
:
2488 parse_omp_structured_block (st
, true);
2491 case ST_OMP_PARALLEL_DO
:
2492 st
= parse_omp_do (st
);
2496 parse_omp_atomic ();
2507 st
= next_statement ();
2511 st
= parse_executable (ST_NONE
);
2514 else if (st
== ST_OMP_SECTION
2515 && (omp_st
== ST_OMP_SECTIONS
2516 || omp_st
== ST_OMP_PARALLEL_SECTIONS
))
2518 np
= new_level (np
);
2522 else if (st
!= omp_end_st
)
2523 unexpected_statement (st
);
2525 while (st
!= omp_end_st
);
2529 case EXEC_OMP_END_NOWAIT
:
2530 cp
->ext
.omp_clauses
->nowait
|= new_st
.ext
.omp_bool
;
2532 case EXEC_OMP_CRITICAL
:
2533 if (((cp
->ext
.omp_name
== NULL
) ^ (new_st
.ext
.omp_name
== NULL
))
2534 || (new_st
.ext
.omp_name
!= NULL
2535 && strcmp (cp
->ext
.omp_name
, new_st
.ext
.omp_name
) != 0))
2536 gfc_error ("Name after !$omp critical and !$omp end critical does"
2537 " not match at %C");
2538 gfc_free ((char *) new_st
.ext
.omp_name
);
2540 case EXEC_OMP_END_SINGLE
:
2541 cp
->ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
]
2542 = new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
];
2543 new_st
.ext
.omp_clauses
->lists
[OMP_LIST_COPYPRIVATE
] = NULL
;
2544 gfc_free_omp_clauses (new_st
.ext
.omp_clauses
);
2552 gfc_clear_new_st ();
2553 gfc_commit_symbols ();
2554 gfc_warning_check ();
2559 /* Accept a series of executable statements. We return the first
2560 statement that doesn't fit to the caller. Any block statements are
2561 passed on to the correct handler, which usually passes the buck
2564 static gfc_statement
2565 parse_executable (gfc_statement st
)
2570 st
= next_statement ();
2574 close_flag
= check_do_closure ();
2579 case ST_END_PROGRAM
:
2582 case ST_END_FUNCTION
:
2586 case ST_END_SUBROUTINE
:
2591 case ST_SELECT_CASE
:
2593 ("%s statement at %C cannot terminate a non-block DO loop",
2594 gfc_ascii_statement (st
));
2610 accept_statement (st
);
2611 if (close_flag
== 1)
2612 return ST_IMPLIED_ENDDO
;
2619 case ST_SELECT_CASE
:
2620 parse_select_block ();
2625 if (check_do_closure () == 1)
2626 return ST_IMPLIED_ENDDO
;
2629 case ST_WHERE_BLOCK
:
2630 parse_where_block ();
2633 case ST_FORALL_BLOCK
:
2634 parse_forall_block ();
2637 case ST_OMP_PARALLEL
:
2638 case ST_OMP_PARALLEL_SECTIONS
:
2639 case ST_OMP_SECTIONS
:
2640 case ST_OMP_ORDERED
:
2641 case ST_OMP_CRITICAL
:
2644 parse_omp_structured_block (st
, false);
2647 case ST_OMP_WORKSHARE
:
2648 case ST_OMP_PARALLEL_WORKSHARE
:
2649 parse_omp_structured_block (st
, true);
2653 case ST_OMP_PARALLEL_DO
:
2654 st
= parse_omp_do (st
);
2655 if (st
== ST_IMPLIED_ENDDO
)
2660 parse_omp_atomic ();
2667 st
= next_statement ();
2672 /* Parse a series of contained program units. */
2674 static void parse_progunit (gfc_statement
);
2677 /* Fix the symbols for sibling functions. These are incorrectly added to
2678 the child namespace as the parser didn't know about this procedure. */
2681 gfc_fixup_sibling_symbols (gfc_symbol
* sym
, gfc_namespace
* siblings
)
2685 gfc_symbol
*old_sym
;
2687 sym
->attr
.referenced
= 1;
2688 for (ns
= siblings
; ns
; ns
= ns
->sibling
)
2690 gfc_find_sym_tree (sym
->name
, ns
, 0, &st
);
2694 old_sym
= st
->n
.sym
;
2695 if ((old_sym
->attr
.flavor
== FL_PROCEDURE
2696 || old_sym
->ts
.type
== BT_UNKNOWN
)
2697 && old_sym
->ns
== ns
2698 && ! old_sym
->attr
.contained
)
2700 /* Replace it with the symbol from the parent namespace. */
2704 /* Free the old (local) symbol. */
2706 if (old_sym
->refs
== 0)
2707 gfc_free_symbol (old_sym
);
2710 /* Do the same for any contained procedures. */
2711 gfc_fixup_sibling_symbols (sym
, ns
->contained
);
2716 parse_contained (int module
)
2718 gfc_namespace
*ns
, *parent_ns
;
2719 gfc_state_data s1
, s2
;
2724 push_state (&s1
, COMP_CONTAINS
, NULL
);
2725 parent_ns
= gfc_current_ns
;
2729 gfc_current_ns
= gfc_get_namespace (parent_ns
, 1);
2731 gfc_current_ns
->sibling
= parent_ns
->contained
;
2732 parent_ns
->contained
= gfc_current_ns
;
2734 st
= next_statement ();
2743 accept_statement (st
);
2746 (st
== ST_FUNCTION
) ? COMP_FUNCTION
: COMP_SUBROUTINE
,
2749 /* For internal procedures, create/update the symbol in the
2750 parent namespace. */
2754 if (gfc_get_symbol (gfc_new_block
->name
, parent_ns
, &sym
))
2756 ("Contained procedure '%s' at %C is already ambiguous",
2757 gfc_new_block
->name
);
2760 if (gfc_add_procedure (&sym
->attr
, PROC_INTERNAL
, sym
->name
,
2761 &gfc_new_block
->declared_at
) ==
2764 if (st
== ST_FUNCTION
)
2765 gfc_add_function (&sym
->attr
, sym
->name
,
2766 &gfc_new_block
->declared_at
);
2768 gfc_add_subroutine (&sym
->attr
, sym
->name
,
2769 &gfc_new_block
->declared_at
);
2773 gfc_commit_symbols ();
2776 sym
= gfc_new_block
;
2778 /* Mark this as a contained function, so it isn't replaced
2779 by other module functions. */
2780 sym
->attr
.contained
= 1;
2781 sym
->attr
.referenced
= 1;
2783 parse_progunit (ST_NONE
);
2785 /* Fix up any sibling functions that refer to this one. */
2786 gfc_fixup_sibling_symbols (sym
, gfc_current_ns
);
2787 /* Or refer to any of its alternate entry points. */
2788 for (el
= gfc_current_ns
->entries
; el
; el
= el
->next
)
2789 gfc_fixup_sibling_symbols (el
->sym
, gfc_current_ns
);
2791 gfc_current_ns
->code
= s2
.head
;
2792 gfc_current_ns
= parent_ns
;
2797 /* These statements are associated with the end of the host
2799 case ST_END_FUNCTION
:
2801 case ST_END_PROGRAM
:
2802 case ST_END_SUBROUTINE
:
2803 accept_statement (st
);
2807 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2808 gfc_ascii_statement (st
));
2809 reject_statement ();
2813 while (st
!= ST_END_FUNCTION
&& st
!= ST_END_SUBROUTINE
2814 && st
!= ST_END_MODULE
&& st
!= ST_END_PROGRAM
);
2816 /* The first namespace in the list is guaranteed to not have
2817 anything (worthwhile) in it. */
2819 gfc_current_ns
= parent_ns
;
2821 ns
= gfc_current_ns
->contained
;
2822 gfc_current_ns
->contained
= ns
->sibling
;
2823 gfc_free_namespace (ns
);
2829 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2832 parse_progunit (gfc_statement st
)
2837 st
= parse_spec (st
);
2847 accept_statement (st
);
2857 st
= parse_executable (st
);
2868 accept_statement (st
);
2875 unexpected_statement (st
);
2876 reject_statement ();
2877 st
= next_statement ();
2883 for (p
= gfc_state_stack
; p
; p
= p
->previous
)
2884 if (p
->state
== COMP_CONTAINS
)
2887 if (gfc_find_state (COMP_MODULE
) == SUCCESS
)
2892 gfc_error ("CONTAINS statement at %C is already in a contained "
2894 st
= next_statement ();
2898 parse_contained (0);
2901 gfc_current_ns
->code
= gfc_state_stack
->head
;
2905 /* Come here to complain about a global symbol already in use as
2909 global_used (gfc_gsymbol
*sym
, locus
*where
)
2914 where
= &gfc_current_locus
;
2924 case GSYM_SUBROUTINE
:
2925 name
= "SUBROUTINE";
2930 case GSYM_BLOCK_DATA
:
2931 name
= "BLOCK DATA";
2937 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
2941 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
2942 sym
->name
, where
, name
, &sym
->where
);
2946 /* Parse a block data program unit. */
2949 parse_block_data (void)
2952 static locus blank_locus
;
2953 static int blank_block
=0;
2956 gfc_current_ns
->proc_name
= gfc_new_block
;
2957 gfc_current_ns
->is_block_data
= 1;
2959 if (gfc_new_block
== NULL
)
2962 gfc_error ("Blank BLOCK DATA at %C conflicts with "
2963 "prior BLOCK DATA at %L", &blank_locus
);
2967 blank_locus
= gfc_current_locus
;
2972 s
= gfc_get_gsymbol (gfc_new_block
->name
);
2973 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_BLOCK_DATA
))
2974 global_used(s
, NULL
);
2977 s
->type
= GSYM_BLOCK_DATA
;
2978 s
->where
= gfc_current_locus
;
2983 st
= parse_spec (ST_NONE
);
2985 while (st
!= ST_END_BLOCK_DATA
)
2987 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
2988 gfc_ascii_statement (st
));
2989 reject_statement ();
2990 st
= next_statement ();
2995 /* Parse a module subprogram. */
3003 s
= gfc_get_gsymbol (gfc_new_block
->name
);
3004 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_MODULE
))
3005 global_used(s
, NULL
);
3008 s
->type
= GSYM_MODULE
;
3009 s
->where
= gfc_current_locus
;
3013 st
= parse_spec (ST_NONE
);
3022 parse_contained (1);
3026 accept_statement (st
);
3030 gfc_error ("Unexpected %s statement in MODULE at %C",
3031 gfc_ascii_statement (st
));
3033 reject_statement ();
3034 st
= next_statement ();
3040 /* Add a procedure name to the global symbol table. */
3043 add_global_procedure (int sub
)
3047 s
= gfc_get_gsymbol(gfc_new_block
->name
);
3050 || (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
3051 global_used(s
, NULL
);
3054 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
3055 s
->where
= gfc_current_locus
;
3061 /* Add a program to the global symbol table. */
3064 add_global_program (void)
3068 if (gfc_new_block
== NULL
)
3070 s
= gfc_get_gsymbol (gfc_new_block
->name
);
3072 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= GSYM_PROGRAM
))
3073 global_used(s
, NULL
);
3076 s
->type
= GSYM_PROGRAM
;
3077 s
->where
= gfc_current_locus
;
3083 /* Top level parser. */
3086 gfc_parse_file (void)
3088 int seen_program
, errors_before
, errors
;
3089 gfc_state_data top
, s
;
3093 top
.state
= COMP_NONE
;
3095 top
.previous
= NULL
;
3096 top
.head
= top
.tail
= NULL
;
3097 top
.do_variable
= NULL
;
3099 gfc_state_stack
= &top
;
3101 gfc_clear_new_st ();
3103 gfc_statement_label
= NULL
;
3105 if (setjmp (eof_buf
))
3106 return FAILURE
; /* Come here on unexpected EOF */
3110 /* Exit early for empty files. */
3116 st
= next_statement ();
3125 goto duplicate_main
;
3127 prog_locus
= gfc_current_locus
;
3129 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
3130 main_program_symbol(gfc_current_ns
);
3131 accept_statement (st
);
3132 add_global_program ();
3133 parse_progunit (ST_NONE
);
3137 add_global_procedure (1);
3138 push_state (&s
, COMP_SUBROUTINE
, gfc_new_block
);
3139 accept_statement (st
);
3140 parse_progunit (ST_NONE
);
3144 add_global_procedure (0);
3145 push_state (&s
, COMP_FUNCTION
, gfc_new_block
);
3146 accept_statement (st
);
3147 parse_progunit (ST_NONE
);
3151 push_state (&s
, COMP_BLOCK_DATA
, gfc_new_block
);
3152 accept_statement (st
);
3153 parse_block_data ();
3157 push_state (&s
, COMP_MODULE
, gfc_new_block
);
3158 accept_statement (st
);
3160 gfc_get_errors (NULL
, &errors_before
);
3164 /* Anything else starts a nameless main program block. */
3167 goto duplicate_main
;
3169 prog_locus
= gfc_current_locus
;
3171 push_state (&s
, COMP_PROGRAM
, gfc_new_block
);
3172 main_program_symbol(gfc_current_ns
);
3173 parse_progunit (st
);
3177 gfc_current_ns
->code
= s
.head
;
3179 gfc_resolve (gfc_current_ns
);
3181 /* Dump the parse tree if requested. */
3182 if (gfc_option
.verbose
)
3183 gfc_show_namespace (gfc_current_ns
);
3185 gfc_get_errors (NULL
, &errors
);
3186 if (s
.state
== COMP_MODULE
)
3188 gfc_dump_module (s
.sym
->name
, errors_before
== errors
);
3189 if (errors
== 0 && ! gfc_option
.flag_no_backend
)
3190 gfc_generate_module_code (gfc_current_ns
);
3194 if (errors
== 0 && ! gfc_option
.flag_no_backend
)
3195 gfc_generate_code (gfc_current_ns
);
3206 /* If we see a duplicate main program, shut down. If the second
3207 instance is an implied main program, ie data decls or executable
3208 statements, we're in for lots of errors. */
3209 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus
);
3210 reject_statement ();