2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
31 /* Current statement label. Zero means no statement label. Because new_st
32 can get wiped during statement matching, we have to keep it separate. */
34 gfc_st_label *gfc_statement_label;
36 static locus label_locus;
37 static jmp_buf eof_buf;
39 gfc_state_data *gfc_state_stack;
40 static bool last_was_use_stmt = false;
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);
48 /* A sort of half-matching function. We try to match the word on the
49 input with the passed string. If this succeeds, we call the
50 keyword-dependent matching function that will match the rest of the
51 statement. For single keywords, the matching subroutine is
55 match_word (const char *str, match (*subr) (void), locus *old_locus)
70 gfc_current_locus = *old_locus;
78 /* Load symbols from all USE statements encounted in this scoping unit. */
83 gfc_error_buf old_error;
85 gfc_push_error (&old_error);
89 gfc_pop_error (&old_error);
90 gfc_commit_symbols ();
92 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
93 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
94 last_was_use_stmt = false;
98 /* Figure out what the next statement is, (mostly) regardless of
99 proper ordering. The do...while(0) is there to prevent if/else
102 #define match(keyword, subr, st) \
104 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
107 undo_new_statement (); \
111 /* This is a specialist version of decode_statement that is used
112 for the specification statements in a function, whose
113 characteristics are deferred into the specification statements.
114 eg.: INTEGER (king = mykind) foo ()
115 USE mymodule, ONLY mykind.....
116 The KIND parameter needs a return after USE or IMPORT, whereas
117 derived type declarations can occur anywhere, up the executable
118 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
119 out of the correct kind of specification statements. */
121 decode_specification_statement (void)
127 if (gfc_match_eos () == MATCH_YES)
130 old_locus = gfc_current_locus;
132 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
134 last_was_use_stmt = true;
139 undo_new_statement ();
140 if (last_was_use_stmt)
144 match ("import", gfc_match_import, ST_IMPORT);
146 if (gfc_current_block ()->result->ts.type != BT_DERIVED)
149 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
150 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
151 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
153 /* General statement matching: Instead of testing every possible
154 statement, we eliminate most possibilities by peeking at the
157 c = gfc_peek_ascii_char ();
162 match ("abstract% interface", gfc_match_abstract_interface,
164 match ("allocatable", gfc_match_asynchronous, ST_ATTR_DECL);
165 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
169 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
173 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
174 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
178 match ("data", gfc_match_data, ST_DATA);
179 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
183 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
184 match ("entry% ", gfc_match_entry, ST_ENTRY);
185 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
186 match ("external", gfc_match_external, ST_ATTR_DECL);
190 match ("format", gfc_match_format, ST_FORMAT);
197 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
198 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
199 match ("interface", gfc_match_interface, ST_INTERFACE);
200 match ("intent", gfc_match_intent, ST_ATTR_DECL);
201 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
208 match ("namelist", gfc_match_namelist, ST_NAMELIST);
212 match ("optional", gfc_match_optional, ST_ATTR_DECL);
216 match ("parameter", gfc_match_parameter, ST_PARAMETER);
217 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
218 if (gfc_match_private (&st) == MATCH_YES)
220 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
221 if (gfc_match_public (&st) == MATCH_YES)
223 match ("protected", gfc_match_protected, ST_ATTR_DECL);
230 match ("save", gfc_match_save, ST_ATTR_DECL);
234 match ("target", gfc_match_target, ST_ATTR_DECL);
235 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
242 match ("value", gfc_match_value, ST_ATTR_DECL);
243 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
250 /* This is not a specification statement. See if any of the matchers
251 has stored an error message of some sort. */
255 gfc_buffer_error (0);
256 gfc_current_locus = old_locus;
258 return ST_GET_FCN_CHARACTERISTICS;
262 /* This is the primary 'decode_statement'. */
264 decode_statement (void)
271 gfc_enforce_clean_symbol_state ();
273 gfc_clear_error (); /* Clear any pending errors. */
274 gfc_clear_warning (); /* Clear any pending warnings. */
276 gfc_matching_function = false;
278 if (gfc_match_eos () == MATCH_YES)
281 if (gfc_current_state () == COMP_FUNCTION
282 && gfc_current_block ()->result->ts.kind == -1)
283 return decode_specification_statement ();
285 old_locus = gfc_current_locus;
287 c = gfc_peek_ascii_char ();
291 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
293 last_was_use_stmt = true;
297 undo_new_statement ();
300 if (last_was_use_stmt)
303 /* Try matching a data declaration or function declaration. The
304 input "REALFUNCTIONA(N)" can mean several things in different
305 contexts, so it (and its relatives) get special treatment. */
307 if (gfc_current_state () == COMP_NONE
308 || gfc_current_state () == COMP_INTERFACE
309 || gfc_current_state () == COMP_CONTAINS)
311 gfc_matching_function = true;
312 m = gfc_match_function_decl ();
315 else if (m == MATCH_ERROR)
319 gfc_current_locus = old_locus;
321 gfc_matching_function = false;
324 /* Match statements whose error messages are meant to be overwritten
325 by something better. */
327 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
328 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
329 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
331 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
332 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
334 /* Try to match a subroutine statement, which has the same optional
335 prefixes that functions can have. */
337 if (gfc_match_subroutine () == MATCH_YES)
338 return ST_SUBROUTINE;
340 gfc_current_locus = old_locus;
342 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
343 statements, which might begin with a block label. The match functions for
344 these statements are unusual in that their keyword is not seen before
345 the matcher is called. */
347 if (gfc_match_if (&st) == MATCH_YES)
350 gfc_current_locus = old_locus;
352 if (gfc_match_where (&st) == MATCH_YES)
355 gfc_current_locus = old_locus;
357 if (gfc_match_forall (&st) == MATCH_YES)
360 gfc_current_locus = old_locus;
362 match (NULL, gfc_match_do, ST_DO);
363 match (NULL, gfc_match_block, ST_BLOCK);
364 match (NULL, gfc_match_associate, ST_ASSOCIATE);
365 match (NULL, gfc_match_critical, ST_CRITICAL);
366 match (NULL, gfc_match_select, ST_SELECT_CASE);
367 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
369 /* General statement matching: Instead of testing every possible
370 statement, we eliminate most possibilities by peeking at the
376 match ("abstract% interface", gfc_match_abstract_interface,
378 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
379 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
380 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
381 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
385 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
386 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
387 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
391 match ("call", gfc_match_call, ST_CALL);
392 match ("close", gfc_match_close, ST_CLOSE);
393 match ("continue", gfc_match_continue, ST_CONTINUE);
394 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
395 match ("cycle", gfc_match_cycle, ST_CYCLE);
396 match ("case", gfc_match_case, ST_CASE);
397 match ("common", gfc_match_common, ST_COMMON);
398 match ("contains", gfc_match_eos, ST_CONTAINS);
399 match ("class", gfc_match_class_is, ST_CLASS_IS);
400 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
404 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
405 match ("data", gfc_match_data, ST_DATA);
406 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
410 match ("end file", gfc_match_endfile, ST_END_FILE);
411 match ("exit", gfc_match_exit, ST_EXIT);
412 match ("else", gfc_match_else, ST_ELSE);
413 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
414 match ("else if", gfc_match_elseif, ST_ELSEIF);
415 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
416 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
418 if (gfc_match_end (&st) == MATCH_YES)
421 match ("entry% ", gfc_match_entry, ST_ENTRY);
422 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
423 match ("external", gfc_match_external, ST_ATTR_DECL);
427 match ("final", gfc_match_final_decl, ST_FINAL);
428 match ("flush", gfc_match_flush, ST_FLUSH);
429 match ("format", gfc_match_format, ST_FORMAT);
433 match ("generic", gfc_match_generic, ST_GENERIC);
434 match ("go to", gfc_match_goto, ST_GOTO);
438 match ("inquire", gfc_match_inquire, ST_INQUIRE);
439 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
440 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
441 match ("import", gfc_match_import, ST_IMPORT);
442 match ("interface", gfc_match_interface, ST_INTERFACE);
443 match ("intent", gfc_match_intent, ST_ATTR_DECL);
444 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
448 match ("lock", gfc_match_lock, ST_LOCK);
452 match ("module% procedure", gfc_match_modproc, ST_MODULE_PROC);
453 match ("module", gfc_match_module, ST_MODULE);
457 match ("nullify", gfc_match_nullify, ST_NULLIFY);
458 match ("namelist", gfc_match_namelist, ST_NAMELIST);
462 match ("open", gfc_match_open, ST_OPEN);
463 match ("optional", gfc_match_optional, ST_ATTR_DECL);
467 match ("print", gfc_match_print, ST_WRITE);
468 match ("parameter", gfc_match_parameter, ST_PARAMETER);
469 match ("pause", gfc_match_pause, ST_PAUSE);
470 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
471 if (gfc_match_private (&st) == MATCH_YES)
473 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
474 match ("program", gfc_match_program, ST_PROGRAM);
475 if (gfc_match_public (&st) == MATCH_YES)
477 match ("protected", gfc_match_protected, ST_ATTR_DECL);
481 match ("read", gfc_match_read, ST_READ);
482 match ("return", gfc_match_return, ST_RETURN);
483 match ("rewind", gfc_match_rewind, ST_REWIND);
487 match ("sequence", gfc_match_eos, ST_SEQUENCE);
488 match ("stop", gfc_match_stop, ST_STOP);
489 match ("save", gfc_match_save, ST_ATTR_DECL);
490 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
491 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
492 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
496 match ("target", gfc_match_target, ST_ATTR_DECL);
497 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
498 match ("type is", gfc_match_type_is, ST_TYPE_IS);
502 match ("unlock", gfc_match_unlock, ST_UNLOCK);
506 match ("value", gfc_match_value, ST_ATTR_DECL);
507 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
511 match ("wait", gfc_match_wait, ST_WAIT);
512 match ("write", gfc_match_write, ST_WRITE);
516 /* All else has failed, so give up. See if any of the matchers has
517 stored an error message of some sort. */
519 if (gfc_error_check () == 0)
520 gfc_error_now ("Unclassifiable statement at %C");
524 gfc_error_recovery ();
530 decode_omp_directive (void)
535 gfc_enforce_clean_symbol_state ();
537 gfc_clear_error (); /* Clear any pending errors. */
538 gfc_clear_warning (); /* Clear any pending warnings. */
542 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
543 "or ELEMENTAL procedures");
544 gfc_error_recovery ();
548 if (gfc_implicit_pure (NULL))
549 gfc_current_ns->proc_name->attr.implicit_pure = 0;
551 old_locus = gfc_current_locus;
553 /* General OpenMP directive matching: Instead of testing every possible
554 statement, we eliminate most possibilities by peeking at the
557 c = gfc_peek_ascii_char ();
562 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
565 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
568 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
571 match ("do", gfc_match_omp_do, ST_OMP_DO);
574 match ("end atomic", gfc_match_omp_eos, ST_OMP_END_ATOMIC);
575 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
576 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
577 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
578 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
579 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
580 match ("end parallel sections", gfc_match_omp_eos,
581 ST_OMP_END_PARALLEL_SECTIONS);
582 match ("end parallel workshare", gfc_match_omp_eos,
583 ST_OMP_END_PARALLEL_WORKSHARE);
584 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
585 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
586 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
587 match ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
588 match ("end workshare", gfc_match_omp_end_nowait,
589 ST_OMP_END_WORKSHARE);
592 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
595 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
598 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
601 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
602 match ("parallel sections", gfc_match_omp_parallel_sections,
603 ST_OMP_PARALLEL_SECTIONS);
604 match ("parallel workshare", gfc_match_omp_parallel_workshare,
605 ST_OMP_PARALLEL_WORKSHARE);
606 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
609 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
610 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
611 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
614 match ("task", gfc_match_omp_task, ST_OMP_TASK);
615 match ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
616 match ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
617 match ("threadprivate", gfc_match_omp_threadprivate,
618 ST_OMP_THREADPRIVATE);
620 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
624 /* All else has failed, so give up. See if any of the matchers has
625 stored an error message of some sort. */
627 if (gfc_error_check () == 0)
628 gfc_error_now ("Unclassifiable OpenMP directive at %C");
632 gfc_error_recovery ();
638 decode_gcc_attribute (void)
642 gfc_enforce_clean_symbol_state ();
644 gfc_clear_error (); /* Clear any pending errors. */
645 gfc_clear_warning (); /* Clear any pending warnings. */
646 old_locus = gfc_current_locus;
648 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
650 /* All else has failed, so give up. See if any of the matchers has
651 stored an error message of some sort. */
653 if (gfc_error_check () == 0)
654 gfc_error_now ("Unclassifiable GCC directive at %C");
658 gfc_error_recovery ();
666 /* Get the next statement in free form source. */
675 at_bol = gfc_at_bol ();
676 gfc_gobble_whitespace ();
678 c = gfc_peek_ascii_char ();
684 /* Found a statement label? */
685 m = gfc_match_st_label (&gfc_statement_label);
687 d = gfc_peek_ascii_char ();
688 if (m != MATCH_YES || !gfc_is_whitespace (d))
690 gfc_match_small_literal_int (&i, &cnt);
693 gfc_error_now ("Too many digits in statement label at %C");
696 gfc_error_now ("Zero is not a valid statement label at %C");
699 c = gfc_next_ascii_char ();
702 if (!gfc_is_whitespace (c))
703 gfc_error_now ("Non-numeric character in statement label at %C");
709 label_locus = gfc_current_locus;
711 gfc_gobble_whitespace ();
713 if (at_bol && gfc_peek_ascii_char () == ';')
715 gfc_error_now ("Semicolon at %C needs to be preceded by "
717 gfc_next_ascii_char (); /* Eat up the semicolon. */
721 if (gfc_match_eos () == MATCH_YES)
723 gfc_warning_now ("Ignoring statement label in empty statement "
724 "at %L", &label_locus);
725 gfc_free_st_label (gfc_statement_label);
726 gfc_statement_label = NULL;
733 /* Comments have already been skipped by the time we get here,
734 except for GCC attributes and OpenMP directives. */
736 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
737 c = gfc_peek_ascii_char ();
743 c = gfc_next_ascii_char ();
744 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
745 gcc_assert (c == "gcc$"[i]);
747 gfc_gobble_whitespace ();
748 return decode_gcc_attribute ();
751 else if (c == '$' && gfc_option.gfc_flag_openmp)
755 c = gfc_next_ascii_char ();
756 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
757 gcc_assert (c == "$omp"[i]);
759 gcc_assert (c == ' ' || c == '\t');
760 gfc_gobble_whitespace ();
761 if (last_was_use_stmt)
763 return decode_omp_directive ();
769 if (at_bol && c == ';')
771 if (!(gfc_option.allow_std & GFC_STD_F2008))
772 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
774 gfc_next_ascii_char (); /* Eat up the semicolon. */
778 return decode_statement ();
782 /* Get the next statement in fixed-form source. */
787 int label, digit_flag, i;
792 return decode_statement ();
794 /* Skip past the current label field, parsing a statement label if
795 one is there. This is a weird number parser, since the number is
796 contained within five columns and can have any kind of embedded
797 spaces. We also check for characters that make the rest of the
803 for (i = 0; i < 5; i++)
805 c = gfc_next_char_literal (NONSTRING);
822 label = label * 10 + ((unsigned char) c - '0');
823 label_locus = gfc_current_locus;
827 /* Comments have already been skipped by the time we get
828 here, except for GCC attributes and OpenMP directives. */
831 c = gfc_next_char_literal (NONSTRING);
833 if (TOLOWER (c) == 'g')
835 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
836 gcc_assert (TOLOWER (c) == "gcc$"[i]);
838 return decode_gcc_attribute ();
840 else if (c == '$' && gfc_option.gfc_flag_openmp)
842 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
843 gcc_assert ((char) gfc_wide_tolower (c) == "$omp"[i]);
845 if (c != ' ' && c != '0')
847 gfc_buffer_error (0);
848 gfc_error ("Bad continuation line at %C");
851 if (last_was_use_stmt)
853 return decode_omp_directive ();
857 /* Comments have already been skipped by the time we get
858 here so don't bother checking for them. */
861 gfc_buffer_error (0);
862 gfc_error ("Non-numeric character in statement label at %C");
870 gfc_warning_now ("Zero is not a valid statement label at %C");
873 /* We've found a valid statement label. */
874 gfc_statement_label = gfc_get_st_label (label);
878 /* Since this line starts a statement, it cannot be a continuation
879 of a previous statement. If we see something here besides a
880 space or zero, it must be a bad continuation line. */
882 c = gfc_next_char_literal (NONSTRING);
886 if (c != ' ' && c != '0')
888 gfc_buffer_error (0);
889 gfc_error ("Bad continuation line at %C");
893 /* Now that we've taken care of the statement label columns, we have
894 to make sure that the first nonblank character is not a '!'. If
895 it is, the rest of the line is a comment. */
899 loc = gfc_current_locus;
900 c = gfc_next_char_literal (NONSTRING);
902 while (gfc_is_whitespace (c));
906 gfc_current_locus = loc;
911 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
912 else if (!(gfc_option.allow_std & GFC_STD_F2008))
913 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
918 if (gfc_match_eos () == MATCH_YES)
921 /* At this point, we've got a nonblank statement to parse. */
922 return decode_statement ();
926 gfc_warning_now ("Ignoring statement label in empty statement at %L",
929 gfc_current_locus.lb->truncated = 0;
935 /* Return the next non-ST_NONE statement to the caller. We also worry
936 about including files and the ends of include files at this stage. */
939 next_statement (void)
944 gfc_enforce_clean_symbol_state ();
946 gfc_new_block = NULL;
948 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
949 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
952 gfc_statement_label = NULL;
953 gfc_buffer_error (1);
958 gfc_skip_comments ();
966 if (gfc_define_undef_line ())
969 old_locus = gfc_current_locus;
971 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
977 gfc_buffer_error (0);
979 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
981 gfc_free_st_label (gfc_statement_label);
982 gfc_statement_label = NULL;
983 gfc_current_locus = old_locus;
987 check_statement_label (st);
993 /****************************** Parser ***********************************/
995 /* The parser subroutines are of type 'try' that fail if the file ends
998 /* Macros that expand to case-labels for various classes of
999 statements. Start with executable statements that directly do
1002 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1003 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1004 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1005 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1006 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1007 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1008 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1009 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1010 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1011 case ST_ERROR_STOP: case ST_SYNC_ALL: case ST_SYNC_IMAGES: \
1012 case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK
1014 /* Statements that mark other executable statements. */
1016 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1017 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1018 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1019 case ST_OMP_PARALLEL: \
1020 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1021 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1022 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1023 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1024 case ST_OMP_TASK: case ST_CRITICAL
1026 /* Declaration statements */
1028 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1029 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1030 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
1033 /* Block end statements. Errors associated with interchanging these
1034 are detected in gfc_match_end(). */
1036 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1037 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1038 case ST_END_BLOCK: case ST_END_ASSOCIATE
1041 /* Push a new state onto the stack. */
1044 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
1046 p->state = new_state;
1047 p->previous = gfc_state_stack;
1049 p->head = p->tail = NULL;
1050 p->do_variable = NULL;
1052 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1053 construct statement was accepted right before pushing the state. Thus,
1054 the construct's gfc_code is available as tail of the parent state. */
1055 gcc_assert (gfc_state_stack);
1056 p->construct = gfc_state_stack->tail;
1058 gfc_state_stack = p;
1062 /* Pop the current state. */
1066 gfc_state_stack = gfc_state_stack->previous;
1070 /* Try to find the given state in the state stack. */
1073 gfc_find_state (gfc_compile_state state)
1077 for (p = gfc_state_stack; p; p = p->previous)
1078 if (p->state == state)
1081 return (p == NULL) ? FAILURE : SUCCESS;
1085 /* Starts a new level in the statement list. */
1088 new_level (gfc_code *q)
1092 p = q->block = gfc_get_code ();
1094 gfc_state_stack->head = gfc_state_stack->tail = p;
1100 /* Add the current new_st code structure and adds it to the current
1101 program unit. As a side-effect, it zeroes the new_st. */
1104 add_statement (void)
1108 p = gfc_get_code ();
1111 p->loc = gfc_current_locus;
1113 if (gfc_state_stack->head == NULL)
1114 gfc_state_stack->head = p;
1116 gfc_state_stack->tail->next = p;
1118 while (p->next != NULL)
1121 gfc_state_stack->tail = p;
1123 gfc_clear_new_st ();
1129 /* Frees everything associated with the current statement. */
1132 undo_new_statement (void)
1134 gfc_free_statements (new_st.block);
1135 gfc_free_statements (new_st.next);
1136 gfc_free_statement (&new_st);
1137 gfc_clear_new_st ();
1141 /* If the current statement has a statement label, make sure that it
1142 is allowed to, or should have one. */
1145 check_statement_label (gfc_statement st)
1149 if (gfc_statement_label == NULL)
1151 if (st == ST_FORMAT)
1152 gfc_error ("FORMAT statement at %L does not have a statement label",
1159 case ST_END_PROGRAM:
1160 case ST_END_FUNCTION:
1161 case ST_END_SUBROUTINE:
1165 case ST_END_CRITICAL:
1167 case ST_END_ASSOCIATE:
1170 type = ST_LABEL_TARGET;
1174 type = ST_LABEL_FORMAT;
1177 /* Statement labels are not restricted from appearing on a
1178 particular line. However, there are plenty of situations
1179 where the resulting label can't be referenced. */
1182 type = ST_LABEL_BAD_TARGET;
1186 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1188 new_st.here = gfc_statement_label;
1192 /* Figures out what the enclosing program unit is. This will be a
1193 function, subroutine, program, block data or module. */
1196 gfc_enclosing_unit (gfc_compile_state * result)
1200 for (p = gfc_state_stack; p; p = p->previous)
1201 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1202 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
1203 || p->state == COMP_PROGRAM)
1212 *result = COMP_PROGRAM;
1217 /* Translate a statement enum to a string. */
1220 gfc_ascii_statement (gfc_statement st)
1226 case ST_ARITHMETIC_IF:
1227 p = _("arithmetic IF");
1236 p = _("attribute declaration");
1272 p = _("data declaration");
1280 case ST_DERIVED_DECL:
1281 p = _("derived type declaration");
1295 case ST_END_ASSOCIATE:
1296 p = "END ASSOCIATE";
1301 case ST_END_BLOCK_DATA:
1302 p = "END BLOCK DATA";
1304 case ST_END_CRITICAL:
1316 case ST_END_FUNCTION:
1322 case ST_END_INTERFACE:
1323 p = "END INTERFACE";
1328 case ST_END_PROGRAM:
1334 case ST_END_SUBROUTINE:
1335 p = "END SUBROUTINE";
1346 case ST_EQUIVALENCE:
1358 case ST_FORALL_BLOCK: /* Fall through */
1380 case ST_IMPLICIT_NONE:
1381 p = "IMPLICIT NONE";
1383 case ST_IMPLIED_ENDDO:
1384 p = _("implied END DO");
1413 case ST_MODULE_PROC:
1414 p = "MODULE PROCEDURE";
1446 case ST_SYNC_IMAGES:
1449 case ST_SYNC_MEMORY:
1464 case ST_WHERE_BLOCK: /* Fall through */
1475 p = _("assignment");
1477 case ST_POINTER_ASSIGNMENT:
1478 p = _("pointer assignment");
1480 case ST_SELECT_CASE:
1483 case ST_SELECT_TYPE:
1498 case ST_STATEMENT_FUNCTION:
1499 p = "STATEMENT FUNCTION";
1501 case ST_LABEL_ASSIGNMENT:
1502 p = "LABEL ASSIGNMENT";
1505 p = "ENUM DEFINITION";
1508 p = "ENUMERATOR DEFINITION";
1516 case ST_OMP_BARRIER:
1517 p = "!$OMP BARRIER";
1519 case ST_OMP_CRITICAL:
1520 p = "!$OMP CRITICAL";
1525 case ST_OMP_END_ATOMIC:
1526 p = "!$OMP END ATOMIC";
1528 case ST_OMP_END_CRITICAL:
1529 p = "!$OMP END CRITICAL";
1534 case ST_OMP_END_MASTER:
1535 p = "!$OMP END MASTER";
1537 case ST_OMP_END_ORDERED:
1538 p = "!$OMP END ORDERED";
1540 case ST_OMP_END_PARALLEL:
1541 p = "!$OMP END PARALLEL";
1543 case ST_OMP_END_PARALLEL_DO:
1544 p = "!$OMP END PARALLEL DO";
1546 case ST_OMP_END_PARALLEL_SECTIONS:
1547 p = "!$OMP END PARALLEL SECTIONS";
1549 case ST_OMP_END_PARALLEL_WORKSHARE:
1550 p = "!$OMP END PARALLEL WORKSHARE";
1552 case ST_OMP_END_SECTIONS:
1553 p = "!$OMP END SECTIONS";
1555 case ST_OMP_END_SINGLE:
1556 p = "!$OMP END SINGLE";
1558 case ST_OMP_END_TASK:
1559 p = "!$OMP END TASK";
1561 case ST_OMP_END_WORKSHARE:
1562 p = "!$OMP END WORKSHARE";
1570 case ST_OMP_ORDERED:
1571 p = "!$OMP ORDERED";
1573 case ST_OMP_PARALLEL:
1574 p = "!$OMP PARALLEL";
1576 case ST_OMP_PARALLEL_DO:
1577 p = "!$OMP PARALLEL DO";
1579 case ST_OMP_PARALLEL_SECTIONS:
1580 p = "!$OMP PARALLEL SECTIONS";
1582 case ST_OMP_PARALLEL_WORKSHARE:
1583 p = "!$OMP PARALLEL WORKSHARE";
1585 case ST_OMP_SECTIONS:
1586 p = "!$OMP SECTIONS";
1588 case ST_OMP_SECTION:
1589 p = "!$OMP SECTION";
1597 case ST_OMP_TASKWAIT:
1598 p = "!$OMP TASKWAIT";
1600 case ST_OMP_TASKYIELD:
1601 p = "!$OMP TASKYIELD";
1603 case ST_OMP_THREADPRIVATE:
1604 p = "!$OMP THREADPRIVATE";
1606 case ST_OMP_WORKSHARE:
1607 p = "!$OMP WORKSHARE";
1610 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1617 /* Create a symbol for the main program and assign it to ns->proc_name. */
1620 main_program_symbol (gfc_namespace *ns, const char *name)
1622 gfc_symbol *main_program;
1623 symbol_attribute attr;
1625 gfc_get_symbol (name, ns, &main_program);
1626 gfc_clear_attr (&attr);
1627 attr.flavor = FL_PROGRAM;
1628 attr.proc = PROC_UNKNOWN;
1629 attr.subroutine = 1;
1630 attr.access = ACCESS_PUBLIC;
1631 attr.is_main_program = 1;
1632 main_program->attr = attr;
1633 main_program->declared_at = gfc_current_locus;
1634 ns->proc_name = main_program;
1635 gfc_commit_symbols ();
1639 /* Do whatever is necessary to accept the last statement. */
1642 accept_statement (gfc_statement st)
1646 case ST_IMPLICIT_NONE:
1647 gfc_set_implicit_none ();
1656 gfc_current_ns->proc_name = gfc_new_block;
1659 /* If the statement is the end of a block, lay down a special code
1660 that allows a branch to the end of the block from within the
1661 construct. IF and SELECT are treated differently from DO
1662 (where EXEC_NOP is added inside the loop) for two
1664 1. END DO has a meaning in the sense that after a GOTO to
1665 it, the loop counter must be increased.
1666 2. IF blocks and SELECT blocks can consist of multiple
1667 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
1668 Putting the label before the END IF would make the jump
1669 from, say, the ELSE IF block to the END IF illegal. */
1673 case ST_END_CRITICAL:
1674 if (gfc_statement_label != NULL)
1676 new_st.op = EXEC_END_NESTED_BLOCK;
1681 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
1682 one parallel block. Thus, we add the special code to the nested block
1683 itself, instead of the parent one. */
1685 case ST_END_ASSOCIATE:
1686 if (gfc_statement_label != NULL)
1688 new_st.op = EXEC_END_BLOCK;
1693 /* The end-of-program unit statements do not get the special
1694 marker and require a statement of some sort if they are a
1697 case ST_END_PROGRAM:
1698 case ST_END_FUNCTION:
1699 case ST_END_SUBROUTINE:
1700 if (gfc_statement_label != NULL)
1702 new_st.op = EXEC_RETURN;
1707 new_st.op = EXEC_END_PROCEDURE;
1723 gfc_commit_symbols ();
1724 gfc_warning_check ();
1725 gfc_clear_new_st ();
1729 /* Undo anything tentative that has been built for the current
1733 reject_statement (void)
1735 /* Revert to the previous charlen chain. */
1736 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
1737 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
1739 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
1740 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
1742 gfc_new_block = NULL;
1743 gfc_undo_symbols ();
1744 gfc_clear_warning ();
1745 undo_new_statement ();
1749 /* Generic complaint about an out of order statement. We also do
1750 whatever is necessary to clean up. */
1753 unexpected_statement (gfc_statement st)
1755 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1757 reject_statement ();
1761 /* Given the next statement seen by the matcher, make sure that it is
1762 in proper order with the last. This subroutine is initialized by
1763 calling it with an argument of ST_NONE. If there is a problem, we
1764 issue an error and return FAILURE. Otherwise we return SUCCESS.
1766 Individual parsers need to verify that the statements seen are
1767 valid before calling here, i.e., ENTRY statements are not allowed in
1768 INTERFACE blocks. The following diagram is taken from the standard:
1770 +---------------------------------------+
1771 | program subroutine function module |
1772 +---------------------------------------+
1774 +---------------------------------------+
1776 +---------------------------------------+
1778 | +-----------+------------------+
1779 | | parameter | implicit |
1780 | +-----------+------------------+
1781 | format | | derived type |
1782 | entry | parameter | interface |
1783 | | data | specification |
1784 | | | statement func |
1785 | +-----------+------------------+
1786 | | data | executable |
1787 +--------+-----------+------------------+
1789 +---------------------------------------+
1790 | internal module/subprogram |
1791 +---------------------------------------+
1793 +---------------------------------------+
1802 ORDER_IMPLICIT_NONE,
1810 enum state_order state;
1811 gfc_statement last_statement;
1817 verify_st_order (st_state *p, gfc_statement st, bool silent)
1823 p->state = ORDER_START;
1827 if (p->state > ORDER_USE)
1829 p->state = ORDER_USE;
1833 if (p->state > ORDER_IMPORT)
1835 p->state = ORDER_IMPORT;
1838 case ST_IMPLICIT_NONE:
1839 if (p->state > ORDER_IMPLICIT_NONE)
1842 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1843 statement disqualifies a USE but not an IMPLICIT NONE.
1844 Duplicate IMPLICIT NONEs are caught when the implicit types
1847 p->state = ORDER_IMPLICIT_NONE;
1851 if (p->state > ORDER_IMPLICIT)
1853 p->state = ORDER_IMPLICIT;
1858 if (p->state < ORDER_IMPLICIT_NONE)
1859 p->state = ORDER_IMPLICIT_NONE;
1863 if (p->state >= ORDER_EXEC)
1865 if (p->state < ORDER_IMPLICIT)
1866 p->state = ORDER_IMPLICIT;
1870 if (p->state < ORDER_SPEC)
1871 p->state = ORDER_SPEC;
1876 case ST_DERIVED_DECL:
1878 if (p->state >= ORDER_EXEC)
1880 if (p->state < ORDER_SPEC)
1881 p->state = ORDER_SPEC;
1886 if (p->state < ORDER_EXEC)
1887 p->state = ORDER_EXEC;
1891 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1892 gfc_ascii_statement (st));
1895 /* All is well, record the statement in case we need it next time. */
1896 p->where = gfc_current_locus;
1897 p->last_statement = st;
1902 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1903 gfc_ascii_statement (st),
1904 gfc_ascii_statement (p->last_statement), &p->where);
1910 /* Handle an unexpected end of file. This is a show-stopper... */
1912 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1915 unexpected_eof (void)
1919 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1921 /* Memory cleanup. Move to "second to last". */
1922 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1925 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1928 longjmp (eof_buf, 1);
1932 /* Parse the CONTAINS section of a derived type definition. */
1934 gfc_access gfc_typebound_default_access;
1937 parse_derived_contains (void)
1940 bool seen_private = false;
1941 bool seen_comps = false;
1942 bool error_flag = false;
1945 gcc_assert (gfc_current_state () == COMP_DERIVED);
1946 gcc_assert (gfc_current_block ());
1948 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
1950 if (gfc_current_block ()->attr.sequence)
1951 gfc_error ("Derived-type '%s' with SEQUENCE must not have a CONTAINS"
1952 " section at %C", gfc_current_block ()->name);
1953 if (gfc_current_block ()->attr.is_bind_c)
1954 gfc_error ("Derived-type '%s' with BIND(C) must not have a CONTAINS"
1955 " section at %C", gfc_current_block ()->name);
1957 accept_statement (ST_CONTAINS);
1958 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
1960 gfc_typebound_default_access = ACCESS_PUBLIC;
1966 st = next_statement ();
1974 gfc_error ("Components in TYPE at %C must precede CONTAINS");
1978 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Type-bound"
1979 " procedure at %C") == FAILURE)
1982 accept_statement (ST_PROCEDURE);
1987 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: GENERIC binding"
1988 " at %C") == FAILURE)
1991 accept_statement (ST_GENERIC);
1996 if (gfc_notify_std (GFC_STD_F2003,
1997 "Fortran 2003: FINAL procedure declaration"
1998 " at %C") == FAILURE)
2001 accept_statement (ST_FINAL);
2009 && (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Derived type "
2010 "definition at %C with empty CONTAINS "
2011 "section") == FAILURE))
2014 /* ST_END_TYPE is accepted by parse_derived after return. */
2018 if (gfc_find_state (COMP_MODULE) == FAILURE)
2020 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2027 gfc_error ("PRIVATE statement at %C must precede procedure"
2034 gfc_error ("Duplicate PRIVATE statement at %C");
2038 accept_statement (ST_PRIVATE);
2039 gfc_typebound_default_access = ACCESS_PRIVATE;
2040 seen_private = true;
2044 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2048 gfc_error ("Already inside a CONTAINS block at %C");
2052 unexpected_statement (st);
2060 reject_statement ();
2064 gcc_assert (gfc_current_state () == COMP_DERIVED);
2070 /* Parse a derived type. */
2073 parse_derived (void)
2075 int compiling_type, seen_private, seen_sequence, seen_component;
2079 gfc_component *c, *lock_comp = NULL;
2081 accept_statement (ST_DERIVED_DECL);
2082 push_state (&s, COMP_DERIVED, gfc_new_block);
2084 gfc_new_block->component_access = ACCESS_PUBLIC;
2091 while (compiling_type)
2093 st = next_statement ();
2101 accept_statement (st);
2106 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2113 if (!seen_component)
2114 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Derived type "
2115 "definition at %C without components");
2117 accept_statement (ST_END_TYPE);
2121 if (gfc_find_state (COMP_MODULE) == FAILURE)
2123 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2130 gfc_error ("PRIVATE statement at %C must precede "
2131 "structure components");
2136 gfc_error ("Duplicate PRIVATE statement at %C");
2138 s.sym->component_access = ACCESS_PRIVATE;
2140 accept_statement (ST_PRIVATE);
2147 gfc_error ("SEQUENCE statement at %C must precede "
2148 "structure components");
2152 if (gfc_current_block ()->attr.sequence)
2153 gfc_warning ("SEQUENCE attribute at %C already specified in "
2158 gfc_error ("Duplicate SEQUENCE statement at %C");
2162 gfc_add_sequence (&gfc_current_block ()->attr,
2163 gfc_current_block ()->name, NULL);
2167 gfc_notify_std (GFC_STD_F2003,
2168 "Fortran 2003: CONTAINS block in derived type"
2169 " definition at %C");
2171 accept_statement (ST_CONTAINS);
2172 parse_derived_contains ();
2176 unexpected_statement (st);
2181 /* need to verify that all fields of the derived type are
2182 * interoperable with C if the type is declared to be bind(c)
2184 sym = gfc_current_block ();
2185 for (c = sym->components; c; c = c->next)
2187 bool coarray, lock_type, allocatable, pointer;
2188 coarray = lock_type = allocatable = pointer = false;
2190 /* Look for allocatable components. */
2191 if (c->attr.allocatable
2192 || (c->ts.type == BT_CLASS && c->attr.class_ok
2193 && CLASS_DATA (c)->attr.allocatable)
2194 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.alloc_comp))
2197 sym->attr.alloc_comp = 1;
2200 /* Look for pointer components. */
2202 || (c->ts.type == BT_CLASS && c->attr.class_ok
2203 && CLASS_DATA (c)->attr.class_pointer)
2204 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2207 sym->attr.pointer_comp = 1;
2210 /* Look for procedure pointer components. */
2211 if (c->attr.proc_pointer
2212 || (c->ts.type == BT_DERIVED
2213 && c->ts.u.derived->attr.proc_pointer_comp))
2214 sym->attr.proc_pointer_comp = 1;
2216 /* Looking for coarray components. */
2217 if (c->attr.codimension
2218 || (c->ts.type == BT_CLASS && c->attr.class_ok
2219 && CLASS_DATA (c)->attr.codimension))
2222 sym->attr.coarray_comp = 1;
2225 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp)
2228 if (!pointer && !allocatable)
2229 sym->attr.coarray_comp = 1;
2232 /* Looking for lock_type components. */
2233 if ((c->ts.type == BT_DERIVED
2234 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2235 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2236 || (c->ts.type == BT_CLASS && c->attr.class_ok
2237 && CLASS_DATA (c)->ts.u.derived->from_intmod
2238 == INTMOD_ISO_FORTRAN_ENV
2239 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2240 == ISOFORTRAN_LOCK_TYPE)
2241 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
2242 && !allocatable && !pointer))
2246 sym->attr.lock_comp = 1;
2249 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2250 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2251 unless there are nondirect [allocatable or pointer] components
2252 involved (cf. 1.3.33.1 and 1.3.33.3). */
2254 if (pointer && !coarray && lock_type)
2255 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2256 "codimension or be a subcomponent of a coarray, "
2257 "which is not possible as the component has the "
2258 "pointer attribute", c->name, &c->loc);
2259 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2260 && c->ts.u.derived->attr.lock_comp)
2261 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2262 "of type LOCK_TYPE, which must have a codimension or be a "
2263 "subcomponent of a coarray", c->name, &c->loc);
2265 if (lock_type && allocatable && !coarray)
2266 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2267 "a codimension", c->name, &c->loc);
2268 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
2269 && c->ts.u.derived->attr.lock_comp)
2270 gfc_error ("Allocatable component %s at %L must have a codimension as "
2271 "it has a noncoarray subcomponent of type LOCK_TYPE",
2274 if (sym->attr.coarray_comp && !coarray && lock_type)
2275 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2276 "subcomponent of type LOCK_TYPE must have a codimension or "
2277 "be a subcomponent of a coarray. (Variables of type %s may "
2278 "not have a codimension as already a coarray "
2279 "subcomponent exists)", c->name, &c->loc, sym->name);
2281 if (sym->attr.lock_comp && coarray && !lock_type)
2282 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2283 "subcomponent of type LOCK_TYPE must have a codimension or "
2284 "be a subcomponent of a coarray. (Variables of type %s may "
2285 "not have a codimension as %s at %L has a codimension or a "
2286 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
2287 sym->name, c->name, &c->loc);
2289 /* Look for private components. */
2290 if (sym->component_access == ACCESS_PRIVATE
2291 || c->attr.access == ACCESS_PRIVATE
2292 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
2293 sym->attr.private_comp = 1;
2296 if (!seen_component)
2297 sym->attr.zero_comp = 1;
2303 /* Parse an ENUM. */
2311 int seen_enumerator = 0;
2313 push_state (&s, COMP_ENUM, gfc_new_block);
2317 while (compiling_enum)
2319 st = next_statement ();
2327 seen_enumerator = 1;
2328 accept_statement (st);
2333 if (!seen_enumerator)
2334 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2335 accept_statement (st);
2339 gfc_free_enum_history ();
2340 unexpected_statement (st);
2348 /* Parse an interface. We must be able to deal with the possibility
2349 of recursive interfaces. The parse_spec() subroutine is mutually
2350 recursive with parse_interface(). */
2352 static gfc_statement parse_spec (gfc_statement);
2355 parse_interface (void)
2357 gfc_compile_state new_state = COMP_NONE, current_state;
2358 gfc_symbol *prog_unit, *sym;
2359 gfc_interface_info save;
2360 gfc_state_data s1, s2;
2364 accept_statement (ST_INTERFACE);
2366 current_interface.ns = gfc_current_ns;
2367 save = current_interface;
2369 sym = (current_interface.type == INTERFACE_GENERIC
2370 || current_interface.type == INTERFACE_USER_OP)
2371 ? gfc_new_block : NULL;
2373 push_state (&s1, COMP_INTERFACE, sym);
2374 current_state = COMP_NONE;
2377 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
2379 st = next_statement ();
2387 if (st == ST_SUBROUTINE)
2388 new_state = COMP_SUBROUTINE;
2389 else if (st == ST_FUNCTION)
2390 new_state = COMP_FUNCTION;
2391 if (gfc_new_block->attr.pointer)
2393 gfc_new_block->attr.pointer = 0;
2394 gfc_new_block->attr.proc_pointer = 1;
2396 if (gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
2397 gfc_new_block->formal, NULL) == FAILURE)
2399 reject_statement ();
2400 gfc_free_namespace (gfc_current_ns);
2406 case ST_MODULE_PROC: /* The module procedure matcher makes
2407 sure the context is correct. */
2408 accept_statement (st);
2409 gfc_free_namespace (gfc_current_ns);
2412 case ST_END_INTERFACE:
2413 gfc_free_namespace (gfc_current_ns);
2414 gfc_current_ns = current_interface.ns;
2418 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2419 gfc_ascii_statement (st));
2420 reject_statement ();
2421 gfc_free_namespace (gfc_current_ns);
2426 /* Make sure that the generic name has the right attribute. */
2427 if (current_interface.type == INTERFACE_GENERIC
2428 && current_state == COMP_NONE)
2430 if (new_state == COMP_FUNCTION && sym)
2431 gfc_add_function (&sym->attr, sym->name, NULL);
2432 else if (new_state == COMP_SUBROUTINE && sym)
2433 gfc_add_subroutine (&sym->attr, sym->name, NULL);
2435 current_state = new_state;
2438 if (current_interface.type == INTERFACE_ABSTRACT)
2440 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
2441 if (gfc_is_intrinsic_typename (gfc_new_block->name))
2442 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
2443 "cannot be the same as an intrinsic type",
2444 gfc_new_block->name);
2447 push_state (&s2, new_state, gfc_new_block);
2448 accept_statement (st);
2449 prog_unit = gfc_new_block;
2450 prog_unit->formal_ns = gfc_current_ns;
2451 proc_locus = gfc_current_locus;
2454 /* Read data declaration statements. */
2455 st = parse_spec (ST_NONE);
2457 /* Since the interface block does not permit an IMPLICIT statement,
2458 the default type for the function or the result must be taken
2459 from the formal namespace. */
2460 if (new_state == COMP_FUNCTION)
2462 if (prog_unit->result == prog_unit
2463 && prog_unit->ts.type == BT_UNKNOWN)
2464 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
2465 else if (prog_unit->result != prog_unit
2466 && prog_unit->result->ts.type == BT_UNKNOWN)
2467 gfc_set_default_type (prog_unit->result, 1,
2468 prog_unit->formal_ns);
2471 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
2473 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2474 gfc_ascii_statement (st));
2475 reject_statement ();
2479 /* Add EXTERNAL attribute to function or subroutine. */
2480 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
2481 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
2483 current_interface = save;
2484 gfc_add_interface (prog_unit);
2487 if (current_interface.ns
2488 && current_interface.ns->proc_name
2489 && strcmp (current_interface.ns->proc_name->name,
2490 prog_unit->name) == 0)
2491 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2492 "enclosing procedure", prog_unit->name, &proc_locus);
2501 /* Associate function characteristics by going back to the function
2502 declaration and rematching the prefix. */
2505 match_deferred_characteristics (gfc_typespec * ts)
2508 match m = MATCH_ERROR;
2509 char name[GFC_MAX_SYMBOL_LEN + 1];
2511 loc = gfc_current_locus;
2513 gfc_current_locus = gfc_current_block ()->declared_at;
2516 gfc_buffer_error (1);
2517 m = gfc_match_prefix (ts);
2518 gfc_buffer_error (0);
2520 if (ts->type == BT_DERIVED)
2528 /* Only permit one go at the characteristic association. */
2532 /* Set the function locus correctly. If we have not found the
2533 function name, there is an error. */
2535 && gfc_match ("function% %n", name) == MATCH_YES
2536 && strcmp (name, gfc_current_block ()->name) == 0)
2538 gfc_current_block ()->declared_at = gfc_current_locus;
2539 gfc_commit_symbols ();
2544 gfc_undo_symbols ();
2547 gfc_current_locus =loc;
2552 /* Check specification-expressions in the function result of the currently
2553 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2554 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2555 scope are not yet parsed so this has to be delayed up to parse_spec. */
2558 check_function_result_typed (void)
2560 gfc_typespec* ts = &gfc_current_ns->proc_name->result->ts;
2562 gcc_assert (gfc_current_state () == COMP_FUNCTION);
2563 gcc_assert (ts->type != BT_UNKNOWN);
2565 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2566 /* TODO: Extend when KIND type parameters are implemented. */
2567 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length)
2568 gfc_expr_check_typed (ts->u.cl->length, gfc_current_ns, true);
2572 /* Parse a set of specification statements. Returns the statement
2573 that doesn't fit. */
2575 static gfc_statement
2576 parse_spec (gfc_statement st)
2579 bool function_result_typed = false;
2580 bool bad_characteristic = false;
2583 verify_st_order (&ss, ST_NONE, false);
2585 st = next_statement ();
2587 /* If we are not inside a function or don't have a result specified so far,
2588 do nothing special about it. */
2589 if (gfc_current_state () != COMP_FUNCTION)
2590 function_result_typed = true;
2593 gfc_symbol* proc = gfc_current_ns->proc_name;
2596 if (proc->result->ts.type == BT_UNKNOWN)
2597 function_result_typed = true;
2602 /* If we're inside a BLOCK construct, some statements are disallowed.
2603 Check this here. Attribute declaration statements like INTENT, OPTIONAL
2604 or VALUE are also disallowed, but they don't have a particular ST_*
2605 key so we have to check for them individually in their matcher routine. */
2606 if (gfc_current_state () == COMP_BLOCK)
2610 case ST_IMPLICIT_NONE:
2613 case ST_EQUIVALENCE:
2614 case ST_STATEMENT_FUNCTION:
2615 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
2616 gfc_ascii_statement (st));
2617 reject_statement ();
2624 /* If we find a statement that can not be followed by an IMPLICIT statement
2625 (and thus we can expect to see none any further), type the function result
2626 if it has not yet been typed. Be careful not to give the END statement
2627 to verify_st_order! */
2628 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
2630 bool verify_now = false;
2632 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
2637 verify_st_order (&dummyss, ST_NONE, false);
2638 verify_st_order (&dummyss, st, false);
2640 if (verify_st_order (&dummyss, ST_IMPLICIT, true) == FAILURE)
2646 check_function_result_typed ();
2647 function_result_typed = true;
2656 case ST_IMPLICIT_NONE:
2658 if (!function_result_typed)
2660 check_function_result_typed ();
2661 function_result_typed = true;
2667 case ST_DATA: /* Not allowed in interfaces */
2668 if (gfc_current_state () == COMP_INTERFACE)
2678 case ST_DERIVED_DECL:
2681 if (verify_st_order (&ss, st, false) == FAILURE)
2683 reject_statement ();
2684 st = next_statement ();
2694 case ST_DERIVED_DECL:
2700 if (gfc_current_state () != COMP_MODULE)
2702 gfc_error ("%s statement must appear in a MODULE",
2703 gfc_ascii_statement (st));
2704 reject_statement ();
2708 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
2710 gfc_error ("%s statement at %C follows another accessibility "
2711 "specification", gfc_ascii_statement (st));
2712 reject_statement ();
2716 gfc_current_ns->default_access = (st == ST_PUBLIC)
2717 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2721 case ST_STATEMENT_FUNCTION:
2722 if (gfc_current_state () == COMP_MODULE)
2724 unexpected_statement (st);
2732 accept_statement (st);
2733 st = next_statement ();
2737 accept_statement (st);
2739 st = next_statement ();
2742 case ST_GET_FCN_CHARACTERISTICS:
2743 /* This statement triggers the association of a function's result
2745 ts = &gfc_current_block ()->result->ts;
2746 if (match_deferred_characteristics (ts) != MATCH_YES)
2747 bad_characteristic = true;
2749 st = next_statement ();
2756 /* If match_deferred_characteristics failed, then there is an error. */
2757 if (bad_characteristic)
2759 ts = &gfc_current_block ()->result->ts;
2760 if (ts->type != BT_DERIVED)
2761 gfc_error ("Bad kind expression for function '%s' at %L",
2762 gfc_current_block ()->name,
2763 &gfc_current_block ()->declared_at);
2765 gfc_error ("The type for function '%s' at %L is not accessible",
2766 gfc_current_block ()->name,
2767 &gfc_current_block ()->declared_at);
2769 gfc_current_block ()->ts.kind = 0;
2770 /* Keep the derived type; if it's bad, it will be discovered later. */
2771 if (!(ts->type == BT_DERIVED && ts->u.derived))
2772 ts->type = BT_UNKNOWN;
2779 /* Parse a WHERE block, (not a simple WHERE statement). */
2782 parse_where_block (void)
2784 int seen_empty_else;
2789 accept_statement (ST_WHERE_BLOCK);
2790 top = gfc_state_stack->tail;
2792 push_state (&s, COMP_WHERE, gfc_new_block);
2794 d = add_statement ();
2795 d->expr1 = top->expr1;
2801 seen_empty_else = 0;
2805 st = next_statement ();
2811 case ST_WHERE_BLOCK:
2812 parse_where_block ();
2817 accept_statement (st);
2821 if (seen_empty_else)
2823 gfc_error ("ELSEWHERE statement at %C follows previous "
2824 "unmasked ELSEWHERE");
2825 reject_statement ();
2829 if (new_st.expr1 == NULL)
2830 seen_empty_else = 1;
2832 d = new_level (gfc_state_stack->head);
2834 d->expr1 = new_st.expr1;
2836 accept_statement (st);
2841 accept_statement (st);
2845 gfc_error ("Unexpected %s statement in WHERE block at %C",
2846 gfc_ascii_statement (st));
2847 reject_statement ();
2851 while (st != ST_END_WHERE);
2857 /* Parse a FORALL block (not a simple FORALL statement). */
2860 parse_forall_block (void)
2866 accept_statement (ST_FORALL_BLOCK);
2867 top = gfc_state_stack->tail;
2869 push_state (&s, COMP_FORALL, gfc_new_block);
2871 d = add_statement ();
2872 d->op = EXEC_FORALL;
2877 st = next_statement ();
2882 case ST_POINTER_ASSIGNMENT:
2885 accept_statement (st);
2888 case ST_WHERE_BLOCK:
2889 parse_where_block ();
2892 case ST_FORALL_BLOCK:
2893 parse_forall_block ();
2897 accept_statement (st);
2904 gfc_error ("Unexpected %s statement in FORALL block at %C",
2905 gfc_ascii_statement (st));
2907 reject_statement ();
2911 while (st != ST_END_FORALL);
2917 static gfc_statement parse_executable (gfc_statement);
2919 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2922 parse_if_block (void)
2931 accept_statement (ST_IF_BLOCK);
2933 top = gfc_state_stack->tail;
2934 push_state (&s, COMP_IF, gfc_new_block);
2936 new_st.op = EXEC_IF;
2937 d = add_statement ();
2939 d->expr1 = top->expr1;
2945 st = parse_executable (ST_NONE);
2955 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2956 "statement at %L", &else_locus);
2958 reject_statement ();
2962 d = new_level (gfc_state_stack->head);
2964 d->expr1 = new_st.expr1;
2966 accept_statement (st);
2973 gfc_error ("Duplicate ELSE statements at %L and %C",
2975 reject_statement ();
2980 else_locus = gfc_current_locus;
2982 d = new_level (gfc_state_stack->head);
2985 accept_statement (st);
2993 unexpected_statement (st);
2997 while (st != ST_ENDIF);
3000 accept_statement (st);
3004 /* Parse a SELECT block. */
3007 parse_select_block (void)
3013 accept_statement (ST_SELECT_CASE);
3015 cp = gfc_state_stack->tail;
3016 push_state (&s, COMP_SELECT, gfc_new_block);
3018 /* Make sure that the next statement is a CASE or END SELECT. */
3021 st = next_statement ();
3024 if (st == ST_END_SELECT)
3026 /* Empty SELECT CASE is OK. */
3027 accept_statement (st);
3034 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
3037 reject_statement ();
3040 /* At this point, we're got a nonempty select block. */
3041 cp = new_level (cp);
3044 accept_statement (st);
3048 st = parse_executable (ST_NONE);
3055 cp = new_level (gfc_state_stack->head);
3057 gfc_clear_new_st ();
3059 accept_statement (st);
3065 /* Can't have an executable statement because of
3066 parse_executable(). */
3068 unexpected_statement (st);
3072 while (st != ST_END_SELECT);
3075 accept_statement (st);
3079 /* Pop the current selector from the SELECT TYPE stack. */
3082 select_type_pop (void)
3084 gfc_select_type_stack *old = select_type_stack;
3085 select_type_stack = old->prev;
3090 /* Parse a SELECT TYPE construct (F03:R821). */
3093 parse_select_type_block (void)
3099 accept_statement (ST_SELECT_TYPE);
3101 cp = gfc_state_stack->tail;
3102 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
3104 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
3108 st = next_statement ();
3111 if (st == ST_END_SELECT)
3112 /* Empty SELECT CASE is OK. */
3114 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
3117 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
3118 "following SELECT TYPE at %C");
3120 reject_statement ();
3123 /* At this point, we're got a nonempty select block. */
3124 cp = new_level (cp);
3127 accept_statement (st);
3131 st = parse_executable (ST_NONE);
3139 cp = new_level (gfc_state_stack->head);
3141 gfc_clear_new_st ();
3143 accept_statement (st);
3149 /* Can't have an executable statement because of
3150 parse_executable(). */
3152 unexpected_statement (st);
3156 while (st != ST_END_SELECT);
3160 accept_statement (st);
3161 gfc_current_ns = gfc_current_ns->parent;
3166 /* Given a symbol, make sure it is not an iteration variable for a DO
3167 statement. This subroutine is called when the symbol is seen in a
3168 context that causes it to become redefined. If the symbol is an
3169 iterator, we generate an error message and return nonzero. */
3172 gfc_check_do_variable (gfc_symtree *st)
3176 for (s=gfc_state_stack; s; s = s->previous)
3177 if (s->do_variable == st)
3179 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
3180 "loop beginning at %L", st->name, &s->head->loc);
3188 /* Checks to see if the current statement label closes an enddo.
3189 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3190 an error) if it incorrectly closes an ENDDO. */
3193 check_do_closure (void)
3197 if (gfc_statement_label == NULL)
3200 for (p = gfc_state_stack; p; p = p->previous)
3201 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
3205 return 0; /* No loops to close */
3207 if (p->ext.end_do_label == gfc_statement_label)
3209 if (p == gfc_state_stack)
3212 gfc_error ("End of nonblock DO statement at %C is within another block");
3216 /* At this point, the label doesn't terminate the innermost loop.
3217 Make sure it doesn't terminate another one. */
3218 for (; p; p = p->previous)
3219 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
3220 && p->ext.end_do_label == gfc_statement_label)
3222 gfc_error ("End of nonblock DO statement at %C is interwoven "
3223 "with another DO loop");
3231 /* Parse a series of contained program units. */
3233 static void parse_progunit (gfc_statement);
3236 /* Parse a CRITICAL block. */
3239 parse_critical_block (void)
3245 s.ext.end_do_label = new_st.label1;
3247 accept_statement (ST_CRITICAL);
3248 top = gfc_state_stack->tail;
3250 push_state (&s, COMP_CRITICAL, gfc_new_block);
3252 d = add_statement ();
3253 d->op = EXEC_CRITICAL;
3258 st = parse_executable (ST_NONE);
3266 case ST_END_CRITICAL:
3267 if (s.ext.end_do_label != NULL
3268 && s.ext.end_do_label != gfc_statement_label)
3269 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3270 "match CRITIAL label");
3272 if (gfc_statement_label != NULL)
3274 new_st.op = EXEC_NOP;
3280 unexpected_statement (st);
3284 while (st != ST_END_CRITICAL);
3287 accept_statement (st);
3291 /* Set up the local namespace for a BLOCK construct. */
3294 gfc_build_block_ns (gfc_namespace *parent_ns)
3296 gfc_namespace* my_ns;
3297 static int numblock = 1;
3299 my_ns = gfc_get_namespace (parent_ns, 1);
3300 my_ns->construct_entities = 1;
3302 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3303 code generation (so it must not be NULL).
3304 We set its recursive argument if our container procedure is recursive, so
3305 that local variables are accordingly placed on the stack when it
3306 will be necessary. */
3308 my_ns->proc_name = gfc_new_block;
3312 char buffer[20]; /* Enough to hold "block@2147483648\n". */
3314 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
3315 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
3316 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
3317 my_ns->proc_name->name, NULL);
3318 gcc_assert (t == SUCCESS);
3319 gfc_commit_symbol (my_ns->proc_name);
3322 if (parent_ns->proc_name)
3323 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
3329 /* Parse a BLOCK construct. */
3332 parse_block_construct (void)
3334 gfc_namespace* my_ns;
3337 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BLOCK construct at %C");
3339 my_ns = gfc_build_block_ns (gfc_current_ns);
3341 new_st.op = EXEC_BLOCK;
3342 new_st.ext.block.ns = my_ns;
3343 new_st.ext.block.assoc = NULL;
3344 accept_statement (ST_BLOCK);
3346 push_state (&s, COMP_BLOCK, my_ns->proc_name);
3347 gfc_current_ns = my_ns;
3349 parse_progunit (ST_NONE);
3351 gfc_current_ns = gfc_current_ns->parent;
3356 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3357 behind the scenes with compiler-generated variables. */
3360 parse_associate (void)
3362 gfc_namespace* my_ns;
3365 gfc_association_list* a;
3367 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASSOCIATE construct at %C");
3369 my_ns = gfc_build_block_ns (gfc_current_ns);
3371 new_st.op = EXEC_BLOCK;
3372 new_st.ext.block.ns = my_ns;
3373 gcc_assert (new_st.ext.block.assoc);
3375 /* Add all associate-names as BLOCK variables. Creating them is enough
3376 for now, they'll get their values during trans-* phase. */
3377 gfc_current_ns = my_ns;
3378 for (a = new_st.ext.block.assoc; a; a = a->next)
3382 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
3386 sym->attr.flavor = FL_VARIABLE;
3388 sym->declared_at = a->where;
3389 gfc_set_sym_referenced (sym);
3391 /* Initialize the typespec. It is not available in all cases,
3392 however, as it may only be set on the target during resolution.
3393 Still, sometimes it helps to have it right now -- especially
3394 for parsing component references on the associate-name
3395 in case of assication to a derived-type. */
3396 sym->ts = a->target->ts;
3399 accept_statement (ST_ASSOCIATE);
3400 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
3403 st = parse_executable (ST_NONE);
3410 accept_statement (st);
3411 my_ns->code = gfc_state_stack->head;
3415 unexpected_statement (st);
3419 gfc_current_ns = gfc_current_ns->parent;
3424 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
3425 handled inside of parse_executable(), because they aren't really
3429 parse_do_block (void)
3438 s.ext.end_do_label = new_st.label1;
3440 if (new_st.ext.iterator != NULL)
3441 stree = new_st.ext.iterator->var->symtree;
3445 accept_statement (ST_DO);
3447 top = gfc_state_stack->tail;
3448 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
3451 s.do_variable = stree;
3453 top->block = new_level (top);
3454 top->block->op = EXEC_DO;
3457 st = parse_executable (ST_NONE);
3465 if (s.ext.end_do_label != NULL
3466 && s.ext.end_do_label != gfc_statement_label)
3467 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
3470 if (gfc_statement_label != NULL)
3472 new_st.op = EXEC_NOP;
3477 case ST_IMPLIED_ENDDO:
3478 /* If the do-stmt of this DO construct has a do-construct-name,
3479 the corresponding end-do must be an end-do-stmt (with a matching
3480 name, but in that case we must have seen ST_ENDDO first).
3481 We only complain about this in pedantic mode. */
3482 if (gfc_current_block () != NULL)
3483 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
3484 &gfc_current_block()->declared_at);
3489 unexpected_statement (st);
3494 accept_statement (st);
3498 /* Parse the statements of OpenMP do/parallel do. */
3500 static gfc_statement
3501 parse_omp_do (gfc_statement omp_st)
3507 accept_statement (omp_st);
3509 cp = gfc_state_stack->tail;
3510 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3511 np = new_level (cp);
3517 st = next_statement ();
3520 else if (st == ST_DO)
3523 unexpected_statement (st);
3527 if (gfc_statement_label != NULL
3528 && gfc_state_stack->previous != NULL
3529 && gfc_state_stack->previous->state == COMP_DO
3530 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
3538 there should be no !$OMP END DO. */
3540 return ST_IMPLIED_ENDDO;
3543 check_do_closure ();
3546 st = next_statement ();
3547 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
3549 if (new_st.op == EXEC_OMP_END_NOWAIT)
3550 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
3552 gcc_assert (new_st.op == EXEC_NOP);
3553 gfc_clear_new_st ();
3554 gfc_commit_symbols ();
3555 gfc_warning_check ();
3556 st = next_statement ();
3562 /* Parse the statements of OpenMP atomic directive. */
3564 static gfc_statement
3565 parse_omp_atomic (void)
3572 accept_statement (ST_OMP_ATOMIC);
3574 cp = gfc_state_stack->tail;
3575 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3576 np = new_level (cp);
3579 count = 1 + (cp->ext.omp_atomic == GFC_OMP_ATOMIC_CAPTURE);
3583 st = next_statement ();
3586 else if (st == ST_ASSIGNMENT)
3588 accept_statement (st);
3592 unexpected_statement (st);
3597 st = next_statement ();
3598 if (st == ST_OMP_END_ATOMIC)
3600 gfc_clear_new_st ();
3601 gfc_commit_symbols ();
3602 gfc_warning_check ();
3603 st = next_statement ();
3605 else if (cp->ext.omp_atomic == GFC_OMP_ATOMIC_CAPTURE)
3606 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
3611 /* Parse the statements of an OpenMP structured block. */
3614 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
3616 gfc_statement st, omp_end_st;
3620 accept_statement (omp_st);
3622 cp = gfc_state_stack->tail;
3623 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3624 np = new_level (cp);
3630 case ST_OMP_PARALLEL:
3631 omp_end_st = ST_OMP_END_PARALLEL;
3633 case ST_OMP_PARALLEL_SECTIONS:
3634 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
3636 case ST_OMP_SECTIONS:
3637 omp_end_st = ST_OMP_END_SECTIONS;
3639 case ST_OMP_ORDERED:
3640 omp_end_st = ST_OMP_END_ORDERED;
3642 case ST_OMP_CRITICAL:
3643 omp_end_st = ST_OMP_END_CRITICAL;
3646 omp_end_st = ST_OMP_END_MASTER;
3649 omp_end_st = ST_OMP_END_SINGLE;
3652 omp_end_st = ST_OMP_END_TASK;
3654 case ST_OMP_WORKSHARE:
3655 omp_end_st = ST_OMP_END_WORKSHARE;
3657 case ST_OMP_PARALLEL_WORKSHARE:
3658 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
3666 if (workshare_stmts_only)
3668 /* Inside of !$omp workshare, only
3671 where statements and constructs
3672 forall statements and constructs
3676 are allowed. For !$omp critical these
3677 restrictions apply recursively. */
3680 st = next_statement ();
3691 accept_statement (st);
3694 case ST_WHERE_BLOCK:
3695 parse_where_block ();
3698 case ST_FORALL_BLOCK:
3699 parse_forall_block ();
3702 case ST_OMP_PARALLEL:
3703 case ST_OMP_PARALLEL_SECTIONS:
3704 parse_omp_structured_block (st, false);
3707 case ST_OMP_PARALLEL_WORKSHARE:
3708 case ST_OMP_CRITICAL:
3709 parse_omp_structured_block (st, true);
3712 case ST_OMP_PARALLEL_DO:
3713 st = parse_omp_do (st);
3717 st = parse_omp_atomic ();
3728 st = next_statement ();
3732 st = parse_executable (ST_NONE);
3735 else if (st == ST_OMP_SECTION
3736 && (omp_st == ST_OMP_SECTIONS
3737 || omp_st == ST_OMP_PARALLEL_SECTIONS))
3739 np = new_level (np);
3743 else if (st != omp_end_st)
3744 unexpected_statement (st);
3746 while (st != omp_end_st);
3750 case EXEC_OMP_END_NOWAIT:
3751 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
3753 case EXEC_OMP_CRITICAL:
3754 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
3755 || (new_st.ext.omp_name != NULL
3756 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
3757 gfc_error ("Name after !$omp critical and !$omp end critical does "
3759 free (CONST_CAST (char *, new_st.ext.omp_name));
3761 case EXEC_OMP_END_SINGLE:
3762 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
3763 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
3764 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
3765 gfc_free_omp_clauses (new_st.ext.omp_clauses);
3773 gfc_clear_new_st ();
3774 gfc_commit_symbols ();
3775 gfc_warning_check ();
3780 /* Accept a series of executable statements. We return the first
3781 statement that doesn't fit to the caller. Any block statements are
3782 passed on to the correct handler, which usually passes the buck
3785 static gfc_statement
3786 parse_executable (gfc_statement st)
3791 st = next_statement ();
3795 close_flag = check_do_closure ();
3800 case ST_END_PROGRAM:
3803 case ST_END_FUNCTION:
3808 case ST_END_SUBROUTINE:
3813 case ST_SELECT_CASE:
3814 gfc_error ("%s statement at %C cannot terminate a non-block "
3815 "DO loop", gfc_ascii_statement (st));
3831 accept_statement (st);
3832 if (close_flag == 1)
3833 return ST_IMPLIED_ENDDO;
3837 parse_block_construct ();
3848 case ST_SELECT_CASE:
3849 parse_select_block ();
3852 case ST_SELECT_TYPE:
3853 parse_select_type_block();
3858 if (check_do_closure () == 1)
3859 return ST_IMPLIED_ENDDO;
3863 parse_critical_block ();
3866 case ST_WHERE_BLOCK:
3867 parse_where_block ();
3870 case ST_FORALL_BLOCK:
3871 parse_forall_block ();
3874 case ST_OMP_PARALLEL:
3875 case ST_OMP_PARALLEL_SECTIONS:
3876 case ST_OMP_SECTIONS:
3877 case ST_OMP_ORDERED:
3878 case ST_OMP_CRITICAL:
3882 parse_omp_structured_block (st, false);
3885 case ST_OMP_WORKSHARE:
3886 case ST_OMP_PARALLEL_WORKSHARE:
3887 parse_omp_structured_block (st, true);
3891 case ST_OMP_PARALLEL_DO:
3892 st = parse_omp_do (st);
3893 if (st == ST_IMPLIED_ENDDO)
3898 st = parse_omp_atomic ();
3905 st = next_statement ();
3910 /* Fix the symbols for sibling functions. These are incorrectly added to
3911 the child namespace as the parser didn't know about this procedure. */
3914 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
3918 gfc_symbol *old_sym;
3920 sym->attr.referenced = 1;
3921 for (ns = siblings; ns; ns = ns->sibling)
3923 st = gfc_find_symtree (ns->sym_root, sym->name);
3925 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
3926 goto fixup_contained;
3928 if ((st->n.sym->attr.flavor == FL_DERIVED
3929 && sym->attr.generic && sym->attr.function)
3930 ||(sym->attr.flavor == FL_DERIVED
3931 && st->n.sym->attr.generic && st->n.sym->attr.function))
3932 goto fixup_contained;
3934 old_sym = st->n.sym;
3935 if (old_sym->ns == ns
3936 && !old_sym->attr.contained
3938 /* By 14.6.1.3, host association should be excluded
3939 for the following. */
3940 && !(old_sym->attr.external
3941 || (old_sym->ts.type != BT_UNKNOWN
3942 && !old_sym->attr.implicit_type)
3943 || old_sym->attr.flavor == FL_PARAMETER
3944 || old_sym->attr.use_assoc
3945 || old_sym->attr.in_common
3946 || old_sym->attr.in_equivalence
3947 || old_sym->attr.data
3948 || old_sym->attr.dummy
3949 || old_sym->attr.result
3950 || old_sym->attr.dimension
3951 || old_sym->attr.allocatable
3952 || old_sym->attr.intrinsic
3953 || old_sym->attr.generic
3954 || old_sym->attr.flavor == FL_NAMELIST
3955 || old_sym->attr.flavor == FL_LABEL
3956 || old_sym->attr.proc == PROC_ST_FUNCTION))
3958 /* Replace it with the symbol from the parent namespace. */
3962 gfc_release_symbol (old_sym);
3966 /* Do the same for any contained procedures. */
3967 gfc_fixup_sibling_symbols (sym, ns->contained);
3972 parse_contained (int module)
3974 gfc_namespace *ns, *parent_ns, *tmp;
3975 gfc_state_data s1, s2;
3979 int contains_statements = 0;
3982 push_state (&s1, COMP_CONTAINS, NULL);
3983 parent_ns = gfc_current_ns;
3987 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
3989 gfc_current_ns->sibling = parent_ns->contained;
3990 parent_ns->contained = gfc_current_ns;
3993 /* Process the next available statement. We come here if we got an error
3994 and rejected the last statement. */
3995 st = next_statement ();
4004 contains_statements = 1;
4005 accept_statement (st);
4008 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
4011 /* For internal procedures, create/update the symbol in the
4012 parent namespace. */
4016 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
4017 gfc_error ("Contained procedure '%s' at %C is already "
4018 "ambiguous", gfc_new_block->name);
4021 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
4022 &gfc_new_block->declared_at) ==
4025 if (st == ST_FUNCTION)
4026 gfc_add_function (&sym->attr, sym->name,
4027 &gfc_new_block->declared_at);
4029 gfc_add_subroutine (&sym->attr, sym->name,
4030 &gfc_new_block->declared_at);
4034 gfc_commit_symbols ();
4037 sym = gfc_new_block;
4039 /* Mark this as a contained function, so it isn't replaced
4040 by other module functions. */
4041 sym->attr.contained = 1;
4042 sym->attr.referenced = 1;
4044 /* Set implicit_pure so that it can be reset if any of the
4045 tests for purity fail. This is used for some optimisation
4046 during translation. */
4047 if (!sym->attr.pure)
4048 sym->attr.implicit_pure = 1;
4050 parse_progunit (ST_NONE);
4052 /* Fix up any sibling functions that refer to this one. */
4053 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
4054 /* Or refer to any of its alternate entry points. */
4055 for (el = gfc_current_ns->entries; el; el = el->next)
4056 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
4058 gfc_current_ns->code = s2.head;
4059 gfc_current_ns = parent_ns;
4064 /* These statements are associated with the end of the host unit. */
4065 case ST_END_FUNCTION:
4067 case ST_END_PROGRAM:
4068 case ST_END_SUBROUTINE:
4069 accept_statement (st);
4073 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
4074 gfc_ascii_statement (st));
4075 reject_statement ();
4081 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
4082 && st != ST_END_MODULE && st != ST_END_PROGRAM);
4084 /* The first namespace in the list is guaranteed to not have
4085 anything (worthwhile) in it. */
4086 tmp = gfc_current_ns;
4087 gfc_current_ns = parent_ns;
4088 if (seen_error && tmp->refs > 1)
4089 gfc_free_namespace (tmp);
4091 ns = gfc_current_ns->contained;
4092 gfc_current_ns->contained = ns->sibling;
4093 gfc_free_namespace (ns);
4096 if (!contains_statements)
4097 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTAINS statement without "
4098 "FUNCTION or SUBROUTINE statement at %C");
4102 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
4105 parse_progunit (gfc_statement st)
4110 st = parse_spec (st);
4117 /* This is not allowed within BLOCK! */
4118 if (gfc_current_state () != COMP_BLOCK)
4123 accept_statement (st);
4130 if (gfc_current_state () == COMP_FUNCTION)
4131 gfc_check_function_type (gfc_current_ns);
4136 st = parse_executable (st);
4144 /* This is not allowed within BLOCK! */
4145 if (gfc_current_state () != COMP_BLOCK)
4150 accept_statement (st);
4157 unexpected_statement (st);
4158 reject_statement ();
4159 st = next_statement ();
4165 for (p = gfc_state_stack; p; p = p->previous)
4166 if (p->state == COMP_CONTAINS)
4169 if (gfc_find_state (COMP_MODULE) == SUCCESS)
4174 gfc_error ("CONTAINS statement at %C is already in a contained "
4176 reject_statement ();
4177 st = next_statement ();
4181 parse_contained (0);
4184 gfc_current_ns->code = gfc_state_stack->head;
4188 /* Come here to complain about a global symbol already in use as
4192 gfc_global_used (gfc_gsymbol *sym, locus *where)
4197 where = &gfc_current_locus;
4207 case GSYM_SUBROUTINE:
4208 name = "SUBROUTINE";
4213 case GSYM_BLOCK_DATA:
4214 name = "BLOCK DATA";
4220 gfc_internal_error ("gfc_global_used(): Bad type");
4224 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
4225 sym->name, where, name, &sym->where);
4229 /* Parse a block data program unit. */
4232 parse_block_data (void)
4235 static locus blank_locus;
4236 static int blank_block=0;
4239 gfc_current_ns->proc_name = gfc_new_block;
4240 gfc_current_ns->is_block_data = 1;
4242 if (gfc_new_block == NULL)
4245 gfc_error ("Blank BLOCK DATA at %C conflicts with "
4246 "prior BLOCK DATA at %L", &blank_locus);
4250 blank_locus = gfc_current_locus;
4255 s = gfc_get_gsymbol (gfc_new_block->name);
4257 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
4258 gfc_global_used(s, NULL);
4261 s->type = GSYM_BLOCK_DATA;
4262 s->where = gfc_current_locus;
4267 st = parse_spec (ST_NONE);
4269 while (st != ST_END_BLOCK_DATA)
4271 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
4272 gfc_ascii_statement (st));
4273 reject_statement ();
4274 st = next_statement ();
4279 /* Parse a module subprogram. */
4287 s = gfc_get_gsymbol (gfc_new_block->name);
4288 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
4289 gfc_global_used(s, NULL);
4292 s->type = GSYM_MODULE;
4293 s->where = gfc_current_locus;
4297 st = parse_spec (ST_NONE);
4306 parse_contained (1);
4310 accept_statement (st);
4314 gfc_error ("Unexpected %s statement in MODULE at %C",
4315 gfc_ascii_statement (st));
4317 reject_statement ();
4318 st = next_statement ();
4322 s->ns = gfc_current_ns;
4326 /* Add a procedure name to the global symbol table. */
4329 add_global_procedure (int sub)
4333 s = gfc_get_gsymbol(gfc_new_block->name);
4336 || (s->type != GSYM_UNKNOWN
4337 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
4338 gfc_global_used(s, NULL);
4341 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4342 s->where = gfc_current_locus;
4344 s->ns = gfc_current_ns;
4349 /* Add a program to the global symbol table. */
4352 add_global_program (void)
4356 if (gfc_new_block == NULL)
4358 s = gfc_get_gsymbol (gfc_new_block->name);
4360 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
4361 gfc_global_used(s, NULL);
4364 s->type = GSYM_PROGRAM;
4365 s->where = gfc_current_locus;
4367 s->ns = gfc_current_ns;
4372 /* Resolve all the program units when whole file scope option
4375 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
4377 gfc_free_dt_list ();
4378 gfc_current_ns = gfc_global_ns_list;
4379 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4381 if (gfc_current_ns->proc_name
4382 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
4383 continue; /* Already resolved. */
4385 if (gfc_current_ns->proc_name)
4386 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4387 gfc_resolve (gfc_current_ns);
4388 gfc_current_ns->derived_types = gfc_derived_types;
4389 gfc_derived_types = NULL;
4395 clean_up_modules (gfc_gsymbol *gsym)
4400 clean_up_modules (gsym->left);
4401 clean_up_modules (gsym->right);
4403 if (gsym->type != GSYM_MODULE || !gsym->ns)
4406 gfc_current_ns = gsym->ns;
4407 gfc_derived_types = gfc_current_ns->derived_types;
4414 /* Translate all the program units when whole file scope option
4415 is active. This could be in a different order to resolution if
4416 there are forward references in the file. */
4418 translate_all_program_units (gfc_namespace *gfc_global_ns_list,
4423 gfc_current_ns = gfc_global_ns_list;
4424 gfc_get_errors (NULL, &errors);
4426 /* If the main program is in the translation unit and we have
4427 -fcoarray=libs, generate the static variables. */
4428 if (gfc_option.coarray == GFC_FCOARRAY_LIB && main_in_tu)
4429 gfc_init_coarray_decl (true);
4431 /* We first translate all modules to make sure that later parts
4432 of the program can use the decl. Then we translate the nonmodules. */
4434 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4436 if (!gfc_current_ns->proc_name
4437 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
4440 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4441 gfc_derived_types = gfc_current_ns->derived_types;
4442 gfc_generate_module_code (gfc_current_ns);
4443 gfc_current_ns->translated = 1;
4446 gfc_current_ns = gfc_global_ns_list;
4447 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4449 if (gfc_current_ns->proc_name
4450 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
4453 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4454 gfc_derived_types = gfc_current_ns->derived_types;
4455 gfc_generate_code (gfc_current_ns);
4456 gfc_current_ns->translated = 1;
4459 /* Clean up all the namespaces after translation. */
4460 gfc_current_ns = gfc_global_ns_list;
4461 for (;gfc_current_ns;)
4465 if (gfc_current_ns->proc_name
4466 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
4468 gfc_current_ns = gfc_current_ns->sibling;
4472 ns = gfc_current_ns->sibling;
4473 gfc_derived_types = gfc_current_ns->derived_types;
4475 gfc_current_ns = ns;
4478 clean_up_modules (gfc_gsym_root);
4482 /* Top level parser. */
4485 gfc_parse_file (void)
4487 int seen_program, errors_before, errors;
4488 gfc_state_data top, s;
4491 gfc_namespace *next;
4493 gfc_start_source_files ();
4495 top.state = COMP_NONE;
4497 top.previous = NULL;
4498 top.head = top.tail = NULL;
4499 top.do_variable = NULL;
4501 gfc_state_stack = ⊤
4503 gfc_clear_new_st ();
4505 gfc_statement_label = NULL;
4507 if (setjmp (eof_buf))
4508 return FAILURE; /* Come here on unexpected EOF */
4510 /* Prepare the global namespace that will contain the
4512 gfc_global_ns_list = next = NULL;
4516 /* Exit early for empty files. */
4522 st = next_statement ();
4531 goto duplicate_main;
4533 prog_locus = gfc_current_locus;
4535 push_state (&s, COMP_PROGRAM, gfc_new_block);
4536 main_program_symbol(gfc_current_ns, gfc_new_block->name);
4537 accept_statement (st);
4538 add_global_program ();
4539 parse_progunit (ST_NONE);
4540 if (gfc_option.flag_whole_file)
4545 add_global_procedure (1);
4546 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
4547 accept_statement (st);
4548 parse_progunit (ST_NONE);
4549 if (gfc_option.flag_whole_file)
4554 add_global_procedure (0);
4555 push_state (&s, COMP_FUNCTION, gfc_new_block);
4556 accept_statement (st);
4557 parse_progunit (ST_NONE);
4558 if (gfc_option.flag_whole_file)
4563 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
4564 accept_statement (st);
4565 parse_block_data ();
4569 push_state (&s, COMP_MODULE, gfc_new_block);
4570 accept_statement (st);
4572 gfc_get_errors (NULL, &errors_before);
4576 /* Anything else starts a nameless main program block. */
4579 goto duplicate_main;
4581 prog_locus = gfc_current_locus;
4583 push_state (&s, COMP_PROGRAM, gfc_new_block);
4584 main_program_symbol (gfc_current_ns, "MAIN__");
4585 parse_progunit (st);
4586 if (gfc_option.flag_whole_file)
4591 /* Handle the non-program units. */
4592 gfc_current_ns->code = s.head;
4594 gfc_resolve (gfc_current_ns);
4596 /* Dump the parse tree if requested. */
4597 if (gfc_option.dump_fortran_original)
4598 gfc_dump_parse_tree (gfc_current_ns, stdout);
4600 gfc_get_errors (NULL, &errors);
4601 if (s.state == COMP_MODULE)
4603 gfc_dump_module (s.sym->name, errors_before == errors);
4604 if (!gfc_option.flag_whole_file)
4607 gfc_generate_module_code (gfc_current_ns);
4613 gfc_current_ns->derived_types = gfc_derived_types;
4614 gfc_derived_types = NULL;
4621 gfc_generate_code (gfc_current_ns);
4629 /* The main program and non-contained procedures are put
4630 in the global namespace list, so that they can be processed
4631 later and all their interfaces resolved. */
4632 gfc_current_ns->code = s.head;
4635 for (; next->sibling; next = next->sibling)
4637 next->sibling = gfc_current_ns;
4640 gfc_global_ns_list = gfc_current_ns;
4642 next = gfc_current_ns;
4649 if (!gfc_option.flag_whole_file)
4652 /* Do the resolution. */
4653 resolve_all_program_units (gfc_global_ns_list);
4655 /* Do the parse tree dump. */
4657 = gfc_option.dump_fortran_original ? gfc_global_ns_list : NULL;
4659 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4660 if (!gfc_current_ns->proc_name
4661 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
4663 gfc_dump_parse_tree (gfc_current_ns, stdout);
4664 fputs ("------------------------------------------\n\n", stdout);
4667 /* Do the translation. */
4668 translate_all_program_units (gfc_global_ns_list, seen_program);
4672 gfc_end_source_files ();
4676 /* If we see a duplicate main program, shut down. If the second
4677 instance is an implied main program, i.e. data decls or executable
4678 statements, we're in for lots of errors. */
4679 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
4680 reject_statement ();