2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
31 /* Current statement label. Zero means no statement label. Because new_st
32 can get wiped during statement matching, we have to keep it separate. */
34 gfc_st_label *gfc_statement_label;
36 static locus label_locus;
37 static jmp_buf eof_buf;
39 gfc_state_data *gfc_state_stack;
41 /* TODO: Re-order functions to kill these forward decls. */
42 static void check_statement_label (gfc_statement);
43 static void undo_new_statement (void);
44 static void reject_statement (void);
47 /* A sort of half-matching function. We try to match the word on the
48 input with the passed string. If this succeeds, we call the
49 keyword-dependent matching function that will match the rest of the
50 statement. For single keywords, the matching subroutine is
54 match_word (const char *str, match (*subr) (void), locus *old_locus)
69 gfc_current_locus = *old_locus;
77 /* Figure out what the next statement is, (mostly) regardless of
78 proper ordering. The do...while(0) is there to prevent if/else
81 #define match(keyword, subr, st) \
83 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
86 undo_new_statement (); \
90 /* This is a specialist version of decode_statement that is used
91 for the specification statements in a function, whose
92 characteristics are deferred into the specification statements.
93 eg.: INTEGER (king = mykind) foo ()
94 USE mymodule, ONLY mykind.....
95 The KIND parameter needs a return after USE or IMPORT, whereas
96 derived type declarations can occur anywhere, up the executable
97 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
98 out of the correct kind of specification statements. */
100 decode_specification_statement (void)
106 if (gfc_match_eos () == MATCH_YES)
109 old_locus = gfc_current_locus;
111 match ("import", gfc_match_import, ST_IMPORT);
112 match ("use", gfc_match_use, ST_USE);
114 if (gfc_current_block ()->result->ts.type != BT_DERIVED)
117 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
118 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
119 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
121 /* General statement matching: Instead of testing every possible
122 statement, we eliminate most possibilities by peeking at the
125 c = gfc_peek_ascii_char ();
130 match ("abstract% interface", gfc_match_abstract_interface,
132 match ("allocatable", gfc_match_asynchronous, ST_ATTR_DECL);
133 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
137 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
141 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
142 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
146 match ("data", gfc_match_data, ST_DATA);
147 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
151 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
152 match ("entry% ", gfc_match_entry, ST_ENTRY);
153 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
154 match ("external", gfc_match_external, ST_ATTR_DECL);
158 match ("format", gfc_match_format, ST_FORMAT);
165 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
166 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
167 match ("interface", gfc_match_interface, ST_INTERFACE);
168 match ("intent", gfc_match_intent, ST_ATTR_DECL);
169 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
176 match ("namelist", gfc_match_namelist, ST_NAMELIST);
180 match ("optional", gfc_match_optional, ST_ATTR_DECL);
184 match ("parameter", gfc_match_parameter, ST_PARAMETER);
185 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
186 if (gfc_match_private (&st) == MATCH_YES)
188 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
189 if (gfc_match_public (&st) == MATCH_YES)
191 match ("protected", gfc_match_protected, ST_ATTR_DECL);
198 match ("save", gfc_match_save, ST_ATTR_DECL);
202 match ("target", gfc_match_target, ST_ATTR_DECL);
203 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
210 match ("value", gfc_match_value, ST_ATTR_DECL);
211 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
218 /* This is not a specification statement. See if any of the matchers
219 has stored an error message of some sort. */
223 gfc_buffer_error (0);
224 gfc_current_locus = old_locus;
226 return ST_GET_FCN_CHARACTERISTICS;
230 /* This is the primary 'decode_statement'. */
232 decode_statement (void)
239 gfc_enforce_clean_symbol_state ();
241 gfc_clear_error (); /* Clear any pending errors. */
242 gfc_clear_warning (); /* Clear any pending warnings. */
244 gfc_matching_function = false;
246 if (gfc_match_eos () == MATCH_YES)
249 if (gfc_current_state () == COMP_FUNCTION
250 && gfc_current_block ()->result->ts.kind == -1)
251 return decode_specification_statement ();
253 old_locus = gfc_current_locus;
255 /* Try matching a data declaration or function declaration. The
256 input "REALFUNCTIONA(N)" can mean several things in different
257 contexts, so it (and its relatives) get special treatment. */
259 if (gfc_current_state () == COMP_NONE
260 || gfc_current_state () == COMP_INTERFACE
261 || gfc_current_state () == COMP_CONTAINS)
263 gfc_matching_function = true;
264 m = gfc_match_function_decl ();
267 else if (m == MATCH_ERROR)
271 gfc_current_locus = old_locus;
273 gfc_matching_function = false;
276 /* Match statements whose error messages are meant to be overwritten
277 by something better. */
279 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
280 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
281 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
283 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
284 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
286 /* Try to match a subroutine statement, which has the same optional
287 prefixes that functions can have. */
289 if (gfc_match_subroutine () == MATCH_YES)
290 return ST_SUBROUTINE;
292 gfc_current_locus = old_locus;
294 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
295 statements, which might begin with a block label. The match functions for
296 these statements are unusual in that their keyword is not seen before
297 the matcher is called. */
299 if (gfc_match_if (&st) == MATCH_YES)
302 gfc_current_locus = old_locus;
304 if (gfc_match_where (&st) == MATCH_YES)
307 gfc_current_locus = old_locus;
309 if (gfc_match_forall (&st) == MATCH_YES)
312 gfc_current_locus = old_locus;
314 match (NULL, gfc_match_do, ST_DO);
315 match (NULL, gfc_match_block, ST_BLOCK);
316 match (NULL, gfc_match_associate, ST_ASSOCIATE);
317 match (NULL, gfc_match_critical, ST_CRITICAL);
318 match (NULL, gfc_match_select, ST_SELECT_CASE);
319 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
321 /* General statement matching: Instead of testing every possible
322 statement, we eliminate most possibilities by peeking at the
325 c = gfc_peek_ascii_char ();
330 match ("abstract% interface", gfc_match_abstract_interface,
332 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
333 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
334 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
335 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
339 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
340 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
341 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
345 match ("call", gfc_match_call, ST_CALL);
346 match ("close", gfc_match_close, ST_CLOSE);
347 match ("continue", gfc_match_continue, ST_CONTINUE);
348 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
349 match ("cycle", gfc_match_cycle, ST_CYCLE);
350 match ("case", gfc_match_case, ST_CASE);
351 match ("common", gfc_match_common, ST_COMMON);
352 match ("contains", gfc_match_eos, ST_CONTAINS);
353 match ("class", gfc_match_class_is, ST_CLASS_IS);
354 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
358 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
359 match ("data", gfc_match_data, ST_DATA);
360 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
364 match ("end file", gfc_match_endfile, ST_END_FILE);
365 match ("exit", gfc_match_exit, ST_EXIT);
366 match ("else", gfc_match_else, ST_ELSE);
367 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
368 match ("else if", gfc_match_elseif, ST_ELSEIF);
369 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
370 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
372 if (gfc_match_end (&st) == MATCH_YES)
375 match ("entry% ", gfc_match_entry, ST_ENTRY);
376 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
377 match ("external", gfc_match_external, ST_ATTR_DECL);
381 match ("final", gfc_match_final_decl, ST_FINAL);
382 match ("flush", gfc_match_flush, ST_FLUSH);
383 match ("format", gfc_match_format, ST_FORMAT);
387 match ("generic", gfc_match_generic, ST_GENERIC);
388 match ("go to", gfc_match_goto, ST_GOTO);
392 match ("inquire", gfc_match_inquire, ST_INQUIRE);
393 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
394 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
395 match ("import", gfc_match_import, ST_IMPORT);
396 match ("interface", gfc_match_interface, ST_INTERFACE);
397 match ("intent", gfc_match_intent, ST_ATTR_DECL);
398 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
402 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
403 match ("module", gfc_match_module, ST_MODULE);
407 match ("nullify", gfc_match_nullify, ST_NULLIFY);
408 match ("namelist", gfc_match_namelist, ST_NAMELIST);
412 match ("open", gfc_match_open, ST_OPEN);
413 match ("optional", gfc_match_optional, ST_ATTR_DECL);
417 match ("print", gfc_match_print, ST_WRITE);
418 match ("parameter", gfc_match_parameter, ST_PARAMETER);
419 match ("pause", gfc_match_pause, ST_PAUSE);
420 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
421 if (gfc_match_private (&st) == MATCH_YES)
423 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
424 match ("program", gfc_match_program, ST_PROGRAM);
425 if (gfc_match_public (&st) == MATCH_YES)
427 match ("protected", gfc_match_protected, ST_ATTR_DECL);
431 match ("read", gfc_match_read, ST_READ);
432 match ("return", gfc_match_return, ST_RETURN);
433 match ("rewind", gfc_match_rewind, ST_REWIND);
437 match ("sequence", gfc_match_eos, ST_SEQUENCE);
438 match ("stop", gfc_match_stop, ST_STOP);
439 match ("save", gfc_match_save, ST_ATTR_DECL);
440 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
441 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
442 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
446 match ("target", gfc_match_target, ST_ATTR_DECL);
447 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
448 match ("type is", gfc_match_type_is, ST_TYPE_IS);
452 match ("use", gfc_match_use, ST_USE);
456 match ("value", gfc_match_value, ST_ATTR_DECL);
457 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
461 match ("wait", gfc_match_wait, ST_WAIT);
462 match ("write", gfc_match_write, ST_WRITE);
466 /* All else has failed, so give up. See if any of the matchers has
467 stored an error message of some sort. */
469 if (gfc_error_check () == 0)
470 gfc_error_now ("Unclassifiable statement at %C");
474 gfc_error_recovery ();
480 decode_omp_directive (void)
485 gfc_enforce_clean_symbol_state ();
487 gfc_clear_error (); /* Clear any pending errors. */
488 gfc_clear_warning (); /* Clear any pending warnings. */
492 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
493 "or ELEMENTAL procedures");
494 gfc_error_recovery ();
498 old_locus = gfc_current_locus;
500 /* General OpenMP directive matching: Instead of testing every possible
501 statement, we eliminate most possibilities by peeking at the
504 c = gfc_peek_ascii_char ();
509 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
512 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
515 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
518 match ("do", gfc_match_omp_do, ST_OMP_DO);
521 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
522 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
523 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
524 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
525 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
526 match ("end parallel sections", gfc_match_omp_eos,
527 ST_OMP_END_PARALLEL_SECTIONS);
528 match ("end parallel workshare", gfc_match_omp_eos,
529 ST_OMP_END_PARALLEL_WORKSHARE);
530 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
531 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
532 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
533 match ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
534 match ("end workshare", gfc_match_omp_end_nowait,
535 ST_OMP_END_WORKSHARE);
538 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
541 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
544 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
547 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
548 match ("parallel sections", gfc_match_omp_parallel_sections,
549 ST_OMP_PARALLEL_SECTIONS);
550 match ("parallel workshare", gfc_match_omp_parallel_workshare,
551 ST_OMP_PARALLEL_WORKSHARE);
552 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
555 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
556 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
557 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
560 match ("task", gfc_match_omp_task, ST_OMP_TASK);
561 match ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
562 match ("threadprivate", gfc_match_omp_threadprivate,
563 ST_OMP_THREADPRIVATE);
565 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
569 /* All else has failed, so give up. See if any of the matchers has
570 stored an error message of some sort. */
572 if (gfc_error_check () == 0)
573 gfc_error_now ("Unclassifiable OpenMP directive at %C");
577 gfc_error_recovery ();
583 decode_gcc_attribute (void)
587 gfc_enforce_clean_symbol_state ();
589 gfc_clear_error (); /* Clear any pending errors. */
590 gfc_clear_warning (); /* Clear any pending warnings. */
591 old_locus = gfc_current_locus;
593 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
595 /* All else has failed, so give up. See if any of the matchers has
596 stored an error message of some sort. */
598 if (gfc_error_check () == 0)
599 gfc_error_now ("Unclassifiable GCC directive at %C");
603 gfc_error_recovery ();
611 /* Get the next statement in free form source. */
620 at_bol = gfc_at_bol ();
621 gfc_gobble_whitespace ();
623 c = gfc_peek_ascii_char ();
629 /* Found a statement label? */
630 m = gfc_match_st_label (&gfc_statement_label);
632 d = gfc_peek_ascii_char ();
633 if (m != MATCH_YES || !gfc_is_whitespace (d))
635 gfc_match_small_literal_int (&i, &cnt);
638 gfc_error_now ("Too many digits in statement label at %C");
641 gfc_error_now ("Zero is not a valid statement label at %C");
644 c = gfc_next_ascii_char ();
647 if (!gfc_is_whitespace (c))
648 gfc_error_now ("Non-numeric character in statement label at %C");
654 label_locus = gfc_current_locus;
656 gfc_gobble_whitespace ();
658 if (at_bol && gfc_peek_ascii_char () == ';')
660 gfc_error_now ("Semicolon at %C needs to be preceded by "
662 gfc_next_ascii_char (); /* Eat up the semicolon. */
666 if (gfc_match_eos () == MATCH_YES)
668 gfc_warning_now ("Ignoring statement label in empty statement "
669 "at %L", &label_locus);
670 gfc_free_st_label (gfc_statement_label);
671 gfc_statement_label = NULL;
678 /* Comments have already been skipped by the time we get here,
679 except for GCC attributes and OpenMP directives. */
681 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
682 c = gfc_peek_ascii_char ();
688 c = gfc_next_ascii_char ();
689 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
690 gcc_assert (c == "gcc$"[i]);
692 gfc_gobble_whitespace ();
693 return decode_gcc_attribute ();
696 else if (c == '$' && gfc_option.flag_openmp)
700 c = gfc_next_ascii_char ();
701 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
702 gcc_assert (c == "$omp"[i]);
704 gcc_assert (c == ' ' || c == '\t');
705 gfc_gobble_whitespace ();
706 return decode_omp_directive ();
712 if (at_bol && c == ';')
714 if (!(gfc_option.allow_std & GFC_STD_F2008))
715 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
717 gfc_next_ascii_char (); /* Eat up the semicolon. */
721 return decode_statement ();
725 /* Get the next statement in fixed-form source. */
730 int label, digit_flag, i;
735 return decode_statement ();
737 /* Skip past the current label field, parsing a statement label if
738 one is there. This is a weird number parser, since the number is
739 contained within five columns and can have any kind of embedded
740 spaces. We also check for characters that make the rest of the
746 for (i = 0; i < 5; i++)
748 c = gfc_next_char_literal (0);
765 label = label * 10 + ((unsigned char) c - '0');
766 label_locus = gfc_current_locus;
770 /* Comments have already been skipped by the time we get
771 here, except for GCC attributes and OpenMP directives. */
774 c = gfc_next_char_literal (0);
776 if (TOLOWER (c) == 'g')
778 for (i = 0; i < 4; i++, c = gfc_next_char_literal (0))
779 gcc_assert (TOLOWER (c) == "gcc$"[i]);
781 return decode_gcc_attribute ();
783 else if (c == '$' && gfc_option.flag_openmp)
785 for (i = 0; i < 4; i++, c = gfc_next_char_literal (0))
786 gcc_assert ((char) gfc_wide_tolower (c) == "$omp"[i]);
788 if (c != ' ' && c != '0')
790 gfc_buffer_error (0);
791 gfc_error ("Bad continuation line at %C");
795 return decode_omp_directive ();
799 /* Comments have already been skipped by the time we get
800 here so don't bother checking for them. */
803 gfc_buffer_error (0);
804 gfc_error ("Non-numeric character in statement label at %C");
812 gfc_warning_now ("Zero is not a valid statement label at %C");
815 /* We've found a valid statement label. */
816 gfc_statement_label = gfc_get_st_label (label);
820 /* Since this line starts a statement, it cannot be a continuation
821 of a previous statement. If we see something here besides a
822 space or zero, it must be a bad continuation line. */
824 c = gfc_next_char_literal (0);
828 if (c != ' ' && c != '0')
830 gfc_buffer_error (0);
831 gfc_error ("Bad continuation line at %C");
835 /* Now that we've taken care of the statement label columns, we have
836 to make sure that the first nonblank character is not a '!'. If
837 it is, the rest of the line is a comment. */
841 loc = gfc_current_locus;
842 c = gfc_next_char_literal (0);
844 while (gfc_is_whitespace (c));
848 gfc_current_locus = loc;
853 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
854 else if (!(gfc_option.allow_std & GFC_STD_F2008))
855 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
860 if (gfc_match_eos () == MATCH_YES)
863 /* At this point, we've got a nonblank statement to parse. */
864 return decode_statement ();
868 gfc_warning_now ("Ignoring statement label in empty statement at %L",
871 gfc_current_locus.lb->truncated = 0;
877 /* Return the next non-ST_NONE statement to the caller. We also worry
878 about including files and the ends of include files at this stage. */
881 next_statement (void)
886 gfc_enforce_clean_symbol_state ();
888 gfc_new_block = NULL;
890 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
891 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
894 gfc_statement_label = NULL;
895 gfc_buffer_error (1);
900 gfc_skip_comments ();
908 if (gfc_define_undef_line ())
911 old_locus = gfc_current_locus;
913 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
919 gfc_buffer_error (0);
921 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
923 gfc_free_st_label (gfc_statement_label);
924 gfc_statement_label = NULL;
925 gfc_current_locus = old_locus;
929 check_statement_label (st);
935 /****************************** Parser ***********************************/
937 /* The parser subroutines are of type 'try' that fail if the file ends
940 /* Macros that expand to case-labels for various classes of
941 statements. Start with executable statements that directly do
944 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
945 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
946 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
947 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
948 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
949 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
950 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
951 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
952 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_ERROR_STOP: \
953 case ST_SYNC_ALL: case ST_SYNC_IMAGES: case ST_SYNC_MEMORY
955 /* Statements that mark other executable statements. */
957 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
958 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
959 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
960 case ST_OMP_PARALLEL: \
961 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
962 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
963 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
964 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
965 case ST_OMP_TASK: case ST_CRITICAL
967 /* Declaration statements */
969 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
970 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
971 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
974 /* Block end statements. Errors associated with interchanging these
975 are detected in gfc_match_end(). */
977 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
978 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
979 case ST_END_BLOCK: case ST_END_ASSOCIATE
982 /* Push a new state onto the stack. */
985 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
987 p->state = new_state;
988 p->previous = gfc_state_stack;
990 p->head = p->tail = NULL;
991 p->do_variable = NULL;
996 /* Pop the current state. */
1000 gfc_state_stack = gfc_state_stack->previous;
1004 /* Try to find the given state in the state stack. */
1007 gfc_find_state (gfc_compile_state state)
1011 for (p = gfc_state_stack; p; p = p->previous)
1012 if (p->state == state)
1015 return (p == NULL) ? FAILURE : SUCCESS;
1019 /* Starts a new level in the statement list. */
1022 new_level (gfc_code *q)
1026 p = q->block = gfc_get_code ();
1028 gfc_state_stack->head = gfc_state_stack->tail = p;
1034 /* Add the current new_st code structure and adds it to the current
1035 program unit. As a side-effect, it zeroes the new_st. */
1038 add_statement (void)
1042 p = gfc_get_code ();
1045 p->loc = gfc_current_locus;
1047 if (gfc_state_stack->head == NULL)
1048 gfc_state_stack->head = p;
1050 gfc_state_stack->tail->next = p;
1052 while (p->next != NULL)
1055 gfc_state_stack->tail = p;
1057 gfc_clear_new_st ();
1063 /* Frees everything associated with the current statement. */
1066 undo_new_statement (void)
1068 gfc_free_statements (new_st.block);
1069 gfc_free_statements (new_st.next);
1070 gfc_free_statement (&new_st);
1071 gfc_clear_new_st ();
1075 /* If the current statement has a statement label, make sure that it
1076 is allowed to, or should have one. */
1079 check_statement_label (gfc_statement st)
1083 if (gfc_statement_label == NULL)
1085 if (st == ST_FORMAT)
1086 gfc_error ("FORMAT statement at %L does not have a statement label",
1093 case ST_END_PROGRAM:
1094 case ST_END_FUNCTION:
1095 case ST_END_SUBROUTINE:
1099 case ST_END_CRITICAL:
1102 type = ST_LABEL_TARGET;
1106 type = ST_LABEL_FORMAT;
1109 /* Statement labels are not restricted from appearing on a
1110 particular line. However, there are plenty of situations
1111 where the resulting label can't be referenced. */
1114 type = ST_LABEL_BAD_TARGET;
1118 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1120 new_st.here = gfc_statement_label;
1124 /* Figures out what the enclosing program unit is. This will be a
1125 function, subroutine, program, block data or module. */
1128 gfc_enclosing_unit (gfc_compile_state * result)
1132 for (p = gfc_state_stack; p; p = p->previous)
1133 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1134 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
1135 || p->state == COMP_PROGRAM)
1144 *result = COMP_PROGRAM;
1149 /* Translate a statement enum to a string. */
1152 gfc_ascii_statement (gfc_statement st)
1158 case ST_ARITHMETIC_IF:
1159 p = _("arithmetic IF");
1168 p = _("attribute declaration");
1204 p = _("data declaration");
1212 case ST_DERIVED_DECL:
1213 p = _("derived type declaration");
1227 case ST_END_ASSOCIATE:
1228 p = "END ASSOCIATE";
1233 case ST_END_BLOCK_DATA:
1234 p = "END BLOCK DATA";
1236 case ST_END_CRITICAL:
1248 case ST_END_FUNCTION:
1254 case ST_END_INTERFACE:
1255 p = "END INTERFACE";
1260 case ST_END_PROGRAM:
1266 case ST_END_SUBROUTINE:
1267 p = "END SUBROUTINE";
1278 case ST_EQUIVALENCE:
1290 case ST_FORALL_BLOCK: /* Fall through */
1312 case ST_IMPLICIT_NONE:
1313 p = "IMPLICIT NONE";
1315 case ST_IMPLIED_ENDDO:
1316 p = _("implied END DO");
1342 case ST_MODULE_PROC:
1343 p = "MODULE PROCEDURE";
1375 case ST_SYNC_IMAGES:
1378 case ST_SYNC_MEMORY:
1390 case ST_WHERE_BLOCK: /* Fall through */
1401 p = _("assignment");
1403 case ST_POINTER_ASSIGNMENT:
1404 p = _("pointer assignment");
1406 case ST_SELECT_CASE:
1409 case ST_SELECT_TYPE:
1424 case ST_STATEMENT_FUNCTION:
1425 p = "STATEMENT FUNCTION";
1427 case ST_LABEL_ASSIGNMENT:
1428 p = "LABEL ASSIGNMENT";
1431 p = "ENUM DEFINITION";
1434 p = "ENUMERATOR DEFINITION";
1442 case ST_OMP_BARRIER:
1443 p = "!$OMP BARRIER";
1445 case ST_OMP_CRITICAL:
1446 p = "!$OMP CRITICAL";
1451 case ST_OMP_END_CRITICAL:
1452 p = "!$OMP END CRITICAL";
1457 case ST_OMP_END_MASTER:
1458 p = "!$OMP END MASTER";
1460 case ST_OMP_END_ORDERED:
1461 p = "!$OMP END ORDERED";
1463 case ST_OMP_END_PARALLEL:
1464 p = "!$OMP END PARALLEL";
1466 case ST_OMP_END_PARALLEL_DO:
1467 p = "!$OMP END PARALLEL DO";
1469 case ST_OMP_END_PARALLEL_SECTIONS:
1470 p = "!$OMP END PARALLEL SECTIONS";
1472 case ST_OMP_END_PARALLEL_WORKSHARE:
1473 p = "!$OMP END PARALLEL WORKSHARE";
1475 case ST_OMP_END_SECTIONS:
1476 p = "!$OMP END SECTIONS";
1478 case ST_OMP_END_SINGLE:
1479 p = "!$OMP END SINGLE";
1481 case ST_OMP_END_TASK:
1482 p = "!$OMP END TASK";
1484 case ST_OMP_END_WORKSHARE:
1485 p = "!$OMP END WORKSHARE";
1493 case ST_OMP_ORDERED:
1494 p = "!$OMP ORDERED";
1496 case ST_OMP_PARALLEL:
1497 p = "!$OMP PARALLEL";
1499 case ST_OMP_PARALLEL_DO:
1500 p = "!$OMP PARALLEL DO";
1502 case ST_OMP_PARALLEL_SECTIONS:
1503 p = "!$OMP PARALLEL SECTIONS";
1505 case ST_OMP_PARALLEL_WORKSHARE:
1506 p = "!$OMP PARALLEL WORKSHARE";
1508 case ST_OMP_SECTIONS:
1509 p = "!$OMP SECTIONS";
1511 case ST_OMP_SECTION:
1512 p = "!$OMP SECTION";
1520 case ST_OMP_TASKWAIT:
1521 p = "!$OMP TASKWAIT";
1523 case ST_OMP_THREADPRIVATE:
1524 p = "!$OMP THREADPRIVATE";
1526 case ST_OMP_WORKSHARE:
1527 p = "!$OMP WORKSHARE";
1530 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1537 /* Create a symbol for the main program and assign it to ns->proc_name. */
1540 main_program_symbol (gfc_namespace *ns, const char *name)
1542 gfc_symbol *main_program;
1543 symbol_attribute attr;
1545 gfc_get_symbol (name, ns, &main_program);
1546 gfc_clear_attr (&attr);
1547 attr.flavor = FL_PROGRAM;
1548 attr.proc = PROC_UNKNOWN;
1549 attr.subroutine = 1;
1550 attr.access = ACCESS_PUBLIC;
1551 attr.is_main_program = 1;
1552 main_program->attr = attr;
1553 main_program->declared_at = gfc_current_locus;
1554 ns->proc_name = main_program;
1555 gfc_commit_symbols ();
1559 /* Do whatever is necessary to accept the last statement. */
1562 accept_statement (gfc_statement st)
1570 case ST_IMPLICIT_NONE:
1571 gfc_set_implicit_none ();
1580 gfc_current_ns->proc_name = gfc_new_block;
1583 /* If the statement is the end of a block, lay down a special code
1584 that allows a branch to the end of the block from within the
1585 construct. IF and SELECT are treated differently from DO
1586 (where EXEC_NOP is added inside the loop) for two
1588 1. END DO has a meaning in the sense that after a GOTO to
1589 it, the loop counter must be increased.
1590 2. IF blocks and SELECT blocks can consist of multiple
1591 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
1592 Putting the label before the END IF would make the jump
1593 from, say, the ELSE IF block to the END IF illegal. */
1597 case ST_END_CRITICAL:
1598 if (gfc_statement_label != NULL)
1600 new_st.op = EXEC_END_BLOCK;
1605 /* The end-of-program unit statements do not get the special
1606 marker and require a statement of some sort if they are a
1609 case ST_END_PROGRAM:
1610 case ST_END_FUNCTION:
1611 case ST_END_SUBROUTINE:
1612 if (gfc_statement_label != NULL)
1614 new_st.op = EXEC_RETURN;
1619 new_st.op = EXEC_END_PROCEDURE;
1635 gfc_commit_symbols ();
1636 gfc_warning_check ();
1637 gfc_clear_new_st ();
1641 /* Undo anything tentative that has been built for the current
1645 reject_statement (void)
1647 /* Revert to the previous charlen chain. */
1648 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
1649 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
1651 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
1652 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
1654 gfc_new_block = NULL;
1655 gfc_undo_symbols ();
1656 gfc_clear_warning ();
1657 undo_new_statement ();
1661 /* Generic complaint about an out of order statement. We also do
1662 whatever is necessary to clean up. */
1665 unexpected_statement (gfc_statement st)
1667 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1669 reject_statement ();
1673 /* Given the next statement seen by the matcher, make sure that it is
1674 in proper order with the last. This subroutine is initialized by
1675 calling it with an argument of ST_NONE. If there is a problem, we
1676 issue an error and return FAILURE. Otherwise we return SUCCESS.
1678 Individual parsers need to verify that the statements seen are
1679 valid before calling here, i.e., ENTRY statements are not allowed in
1680 INTERFACE blocks. The following diagram is taken from the standard:
1682 +---------------------------------------+
1683 | program subroutine function module |
1684 +---------------------------------------+
1686 +---------------------------------------+
1688 +---------------------------------------+
1690 | +-----------+------------------+
1691 | | parameter | implicit |
1692 | +-----------+------------------+
1693 | format | | derived type |
1694 | entry | parameter | interface |
1695 | | data | specification |
1696 | | | statement func |
1697 | +-----------+------------------+
1698 | | data | executable |
1699 +--------+-----------+------------------+
1701 +---------------------------------------+
1702 | internal module/subprogram |
1703 +---------------------------------------+
1705 +---------------------------------------+
1714 ORDER_IMPLICIT_NONE,
1722 enum state_order state;
1723 gfc_statement last_statement;
1729 verify_st_order (st_state *p, gfc_statement st, bool silent)
1735 p->state = ORDER_START;
1739 if (p->state > ORDER_USE)
1741 p->state = ORDER_USE;
1745 if (p->state > ORDER_IMPORT)
1747 p->state = ORDER_IMPORT;
1750 case ST_IMPLICIT_NONE:
1751 if (p->state > ORDER_IMPLICIT_NONE)
1754 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1755 statement disqualifies a USE but not an IMPLICIT NONE.
1756 Duplicate IMPLICIT NONEs are caught when the implicit types
1759 p->state = ORDER_IMPLICIT_NONE;
1763 if (p->state > ORDER_IMPLICIT)
1765 p->state = ORDER_IMPLICIT;
1770 if (p->state < ORDER_IMPLICIT_NONE)
1771 p->state = ORDER_IMPLICIT_NONE;
1775 if (p->state >= ORDER_EXEC)
1777 if (p->state < ORDER_IMPLICIT)
1778 p->state = ORDER_IMPLICIT;
1782 if (p->state < ORDER_SPEC)
1783 p->state = ORDER_SPEC;
1788 case ST_DERIVED_DECL:
1790 if (p->state >= ORDER_EXEC)
1792 if (p->state < ORDER_SPEC)
1793 p->state = ORDER_SPEC;
1798 if (p->state < ORDER_EXEC)
1799 p->state = ORDER_EXEC;
1803 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1804 gfc_ascii_statement (st));
1807 /* All is well, record the statement in case we need it next time. */
1808 p->where = gfc_current_locus;
1809 p->last_statement = st;
1814 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1815 gfc_ascii_statement (st),
1816 gfc_ascii_statement (p->last_statement), &p->where);
1822 /* Handle an unexpected end of file. This is a show-stopper... */
1824 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1827 unexpected_eof (void)
1831 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1833 /* Memory cleanup. Move to "second to last". */
1834 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1837 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1840 longjmp (eof_buf, 1);
1844 /* Parse the CONTAINS section of a derived type definition. */
1846 gfc_access gfc_typebound_default_access;
1849 parse_derived_contains (void)
1852 bool seen_private = false;
1853 bool seen_comps = false;
1854 bool error_flag = false;
1857 gcc_assert (gfc_current_state () == COMP_DERIVED);
1858 gcc_assert (gfc_current_block ());
1860 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
1862 if (gfc_current_block ()->attr.sequence)
1863 gfc_error ("Derived-type '%s' with SEQUENCE must not have a CONTAINS"
1864 " section at %C", gfc_current_block ()->name);
1865 if (gfc_current_block ()->attr.is_bind_c)
1866 gfc_error ("Derived-type '%s' with BIND(C) must not have a CONTAINS"
1867 " section at %C", gfc_current_block ()->name);
1869 accept_statement (ST_CONTAINS);
1870 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
1872 gfc_typebound_default_access = ACCESS_PUBLIC;
1878 st = next_statement ();
1886 gfc_error ("Components in TYPE at %C must precede CONTAINS");
1890 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Type-bound"
1891 " procedure at %C") == FAILURE)
1894 accept_statement (ST_PROCEDURE);
1899 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: GENERIC binding"
1900 " at %C") == FAILURE)
1903 accept_statement (ST_GENERIC);
1908 if (gfc_notify_std (GFC_STD_F2003,
1909 "Fortran 2003: FINAL procedure declaration"
1910 " at %C") == FAILURE)
1913 accept_statement (ST_FINAL);
1921 && (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Derived type "
1922 "definition at %C with empty CONTAINS "
1923 "section") == FAILURE))
1926 /* ST_END_TYPE is accepted by parse_derived after return. */
1930 if (gfc_find_state (COMP_MODULE) == FAILURE)
1932 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1939 gfc_error ("PRIVATE statement at %C must precede procedure"
1946 gfc_error ("Duplicate PRIVATE statement at %C");
1950 accept_statement (ST_PRIVATE);
1951 gfc_typebound_default_access = ACCESS_PRIVATE;
1952 seen_private = true;
1956 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
1960 gfc_error ("Already inside a CONTAINS block at %C");
1964 unexpected_statement (st);
1972 reject_statement ();
1976 gcc_assert (gfc_current_state () == COMP_DERIVED);
1982 /* Parse a derived type. */
1985 parse_derived (void)
1987 int compiling_type, seen_private, seen_sequence, seen_component;
1993 accept_statement (ST_DERIVED_DECL);
1994 push_state (&s, COMP_DERIVED, gfc_new_block);
1996 gfc_new_block->component_access = ACCESS_PUBLIC;
2003 while (compiling_type)
2005 st = next_statement ();
2013 accept_statement (st);
2018 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2025 if (!seen_component)
2026 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Derived type "
2027 "definition at %C without components");
2029 accept_statement (ST_END_TYPE);
2033 if (gfc_find_state (COMP_MODULE) == FAILURE)
2035 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2042 gfc_error ("PRIVATE statement at %C must precede "
2043 "structure components");
2048 gfc_error ("Duplicate PRIVATE statement at %C");
2050 s.sym->component_access = ACCESS_PRIVATE;
2052 accept_statement (ST_PRIVATE);
2059 gfc_error ("SEQUENCE statement at %C must precede "
2060 "structure components");
2064 if (gfc_current_block ()->attr.sequence)
2065 gfc_warning ("SEQUENCE attribute at %C already specified in "
2070 gfc_error ("Duplicate SEQUENCE statement at %C");
2074 gfc_add_sequence (&gfc_current_block ()->attr,
2075 gfc_current_block ()->name, NULL);
2079 gfc_notify_std (GFC_STD_F2003,
2080 "Fortran 2003: CONTAINS block in derived type"
2081 " definition at %C");
2083 accept_statement (ST_CONTAINS);
2084 parse_derived_contains ();
2088 unexpected_statement (st);
2093 /* need to verify that all fields of the derived type are
2094 * interoperable with C if the type is declared to be bind(c)
2096 sym = gfc_current_block ();
2097 for (c = sym->components; c; c = c->next)
2099 /* Look for allocatable components. */
2100 if (c->attr.allocatable
2101 || (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.allocatable)
2102 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.alloc_comp))
2103 sym->attr.alloc_comp = 1;
2105 /* Look for pointer components. */
2107 || (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.class_pointer)
2108 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2109 sym->attr.pointer_comp = 1;
2111 /* Look for procedure pointer components. */
2112 if (c->attr.proc_pointer
2113 || (c->ts.type == BT_DERIVED
2114 && c->ts.u.derived->attr.proc_pointer_comp))
2115 sym->attr.proc_pointer_comp = 1;
2117 /* Looking for coarray components. */
2118 if (c->attr.codimension
2119 || (c->attr.coarray_comp && !c->attr.pointer && !c->attr.allocatable))
2120 sym->attr.coarray_comp = 1;
2122 /* Look for private components. */
2123 if (sym->component_access == ACCESS_PRIVATE
2124 || c->attr.access == ACCESS_PRIVATE
2125 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
2126 sym->attr.private_comp = 1;
2129 if (!seen_component)
2130 sym->attr.zero_comp = 1;
2136 /* Parse an ENUM. */
2144 int seen_enumerator = 0;
2146 push_state (&s, COMP_ENUM, gfc_new_block);
2150 while (compiling_enum)
2152 st = next_statement ();
2160 seen_enumerator = 1;
2161 accept_statement (st);
2166 if (!seen_enumerator)
2167 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2168 accept_statement (st);
2172 gfc_free_enum_history ();
2173 unexpected_statement (st);
2181 /* Parse an interface. We must be able to deal with the possibility
2182 of recursive interfaces. The parse_spec() subroutine is mutually
2183 recursive with parse_interface(). */
2185 static gfc_statement parse_spec (gfc_statement);
2188 parse_interface (void)
2190 gfc_compile_state new_state = COMP_NONE, current_state;
2191 gfc_symbol *prog_unit, *sym;
2192 gfc_interface_info save;
2193 gfc_state_data s1, s2;
2197 accept_statement (ST_INTERFACE);
2199 current_interface.ns = gfc_current_ns;
2200 save = current_interface;
2202 sym = (current_interface.type == INTERFACE_GENERIC
2203 || current_interface.type == INTERFACE_USER_OP)
2204 ? gfc_new_block : NULL;
2206 push_state (&s1, COMP_INTERFACE, sym);
2207 current_state = COMP_NONE;
2210 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
2212 st = next_statement ();
2220 if (st == ST_SUBROUTINE)
2221 new_state = COMP_SUBROUTINE;
2222 else if (st == ST_FUNCTION)
2223 new_state = COMP_FUNCTION;
2224 if (gfc_new_block->attr.pointer)
2226 gfc_new_block->attr.pointer = 0;
2227 gfc_new_block->attr.proc_pointer = 1;
2229 if (gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
2230 gfc_new_block->formal, NULL) == FAILURE)
2232 reject_statement ();
2233 gfc_free_namespace (gfc_current_ns);
2239 case ST_MODULE_PROC: /* The module procedure matcher makes
2240 sure the context is correct. */
2241 accept_statement (st);
2242 gfc_free_namespace (gfc_current_ns);
2245 case ST_END_INTERFACE:
2246 gfc_free_namespace (gfc_current_ns);
2247 gfc_current_ns = current_interface.ns;
2251 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2252 gfc_ascii_statement (st));
2253 reject_statement ();
2254 gfc_free_namespace (gfc_current_ns);
2259 /* Make sure that a generic interface has only subroutines or
2260 functions and that the generic name has the right attribute. */
2261 if (current_interface.type == INTERFACE_GENERIC)
2263 if (current_state == COMP_NONE)
2265 if (new_state == COMP_FUNCTION && sym)
2266 gfc_add_function (&sym->attr, sym->name, NULL);
2267 else if (new_state == COMP_SUBROUTINE && sym)
2268 gfc_add_subroutine (&sym->attr, sym->name, NULL);
2270 current_state = new_state;
2274 if (new_state != current_state)
2276 if (new_state == COMP_SUBROUTINE)
2277 gfc_error ("SUBROUTINE at %C does not belong in a "
2278 "generic function interface");
2280 if (new_state == COMP_FUNCTION)
2281 gfc_error ("FUNCTION at %C does not belong in a "
2282 "generic subroutine interface");
2287 if (current_interface.type == INTERFACE_ABSTRACT)
2289 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
2290 if (gfc_is_intrinsic_typename (gfc_new_block->name))
2291 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
2292 "cannot be the same as an intrinsic type",
2293 gfc_new_block->name);
2296 push_state (&s2, new_state, gfc_new_block);
2297 accept_statement (st);
2298 prog_unit = gfc_new_block;
2299 prog_unit->formal_ns = gfc_current_ns;
2300 proc_locus = gfc_current_locus;
2303 /* Read data declaration statements. */
2304 st = parse_spec (ST_NONE);
2306 /* Since the interface block does not permit an IMPLICIT statement,
2307 the default type for the function or the result must be taken
2308 from the formal namespace. */
2309 if (new_state == COMP_FUNCTION)
2311 if (prog_unit->result == prog_unit
2312 && prog_unit->ts.type == BT_UNKNOWN)
2313 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
2314 else if (prog_unit->result != prog_unit
2315 && prog_unit->result->ts.type == BT_UNKNOWN)
2316 gfc_set_default_type (prog_unit->result, 1,
2317 prog_unit->formal_ns);
2320 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
2322 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2323 gfc_ascii_statement (st));
2324 reject_statement ();
2328 /* Add EXTERNAL attribute to function or subroutine. */
2329 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
2330 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
2332 current_interface = save;
2333 gfc_add_interface (prog_unit);
2336 if (current_interface.ns
2337 && current_interface.ns->proc_name
2338 && strcmp (current_interface.ns->proc_name->name,
2339 prog_unit->name) == 0)
2340 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2341 "enclosing procedure", prog_unit->name, &proc_locus);
2350 /* Associate function characteristics by going back to the function
2351 declaration and rematching the prefix. */
2354 match_deferred_characteristics (gfc_typespec * ts)
2357 match m = MATCH_ERROR;
2358 char name[GFC_MAX_SYMBOL_LEN + 1];
2360 loc = gfc_current_locus;
2362 gfc_current_locus = gfc_current_block ()->declared_at;
2365 gfc_buffer_error (1);
2366 m = gfc_match_prefix (ts);
2367 gfc_buffer_error (0);
2369 if (ts->type == BT_DERIVED)
2377 /* Only permit one go at the characteristic association. */
2381 /* Set the function locus correctly. If we have not found the
2382 function name, there is an error. */
2384 && gfc_match ("function% %n", name) == MATCH_YES
2385 && strcmp (name, gfc_current_block ()->name) == 0)
2387 gfc_current_block ()->declared_at = gfc_current_locus;
2388 gfc_commit_symbols ();
2393 gfc_undo_symbols ();
2396 gfc_current_locus =loc;
2401 /* Check specification-expressions in the function result of the currently
2402 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2403 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2404 scope are not yet parsed so this has to be delayed up to parse_spec. */
2407 check_function_result_typed (void)
2409 gfc_typespec* ts = &gfc_current_ns->proc_name->result->ts;
2411 gcc_assert (gfc_current_state () == COMP_FUNCTION);
2412 gcc_assert (ts->type != BT_UNKNOWN);
2414 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2415 /* TODO: Extend when KIND type parameters are implemented. */
2416 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length)
2417 gfc_expr_check_typed (ts->u.cl->length, gfc_current_ns, true);
2421 /* Parse a set of specification statements. Returns the statement
2422 that doesn't fit. */
2424 static gfc_statement
2425 parse_spec (gfc_statement st)
2428 bool function_result_typed = false;
2429 bool bad_characteristic = false;
2432 verify_st_order (&ss, ST_NONE, false);
2434 st = next_statement ();
2436 /* If we are not inside a function or don't have a result specified so far,
2437 do nothing special about it. */
2438 if (gfc_current_state () != COMP_FUNCTION)
2439 function_result_typed = true;
2442 gfc_symbol* proc = gfc_current_ns->proc_name;
2445 if (proc->result->ts.type == BT_UNKNOWN)
2446 function_result_typed = true;
2451 /* If we're inside a BLOCK construct, some statements are disallowed.
2452 Check this here. Attribute declaration statements like INTENT, OPTIONAL
2453 or VALUE are also disallowed, but they don't have a particular ST_*
2454 key so we have to check for them individually in their matcher routine. */
2455 if (gfc_current_state () == COMP_BLOCK)
2459 case ST_IMPLICIT_NONE:
2462 case ST_EQUIVALENCE:
2463 case ST_STATEMENT_FUNCTION:
2464 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
2465 gfc_ascii_statement (st));
2466 reject_statement ();
2473 /* If we find a statement that can not be followed by an IMPLICIT statement
2474 (and thus we can expect to see none any further), type the function result
2475 if it has not yet been typed. Be careful not to give the END statement
2476 to verify_st_order! */
2477 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
2479 bool verify_now = false;
2481 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
2486 verify_st_order (&dummyss, ST_NONE, false);
2487 verify_st_order (&dummyss, st, false);
2489 if (verify_st_order (&dummyss, ST_IMPLICIT, true) == FAILURE)
2495 check_function_result_typed ();
2496 function_result_typed = true;
2505 case ST_IMPLICIT_NONE:
2507 if (!function_result_typed)
2509 check_function_result_typed ();
2510 function_result_typed = true;
2516 case ST_DATA: /* Not allowed in interfaces */
2517 if (gfc_current_state () == COMP_INTERFACE)
2527 case ST_DERIVED_DECL:
2530 if (verify_st_order (&ss, st, false) == FAILURE)
2532 reject_statement ();
2533 st = next_statement ();
2543 case ST_DERIVED_DECL:
2549 if (gfc_current_state () != COMP_MODULE)
2551 gfc_error ("%s statement must appear in a MODULE",
2552 gfc_ascii_statement (st));
2553 reject_statement ();
2557 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
2559 gfc_error ("%s statement at %C follows another accessibility "
2560 "specification", gfc_ascii_statement (st));
2561 reject_statement ();
2565 gfc_current_ns->default_access = (st == ST_PUBLIC)
2566 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2570 case ST_STATEMENT_FUNCTION:
2571 if (gfc_current_state () == COMP_MODULE)
2573 unexpected_statement (st);
2581 accept_statement (st);
2582 st = next_statement ();
2586 accept_statement (st);
2588 st = next_statement ();
2591 case ST_GET_FCN_CHARACTERISTICS:
2592 /* This statement triggers the association of a function's result
2594 ts = &gfc_current_block ()->result->ts;
2595 if (match_deferred_characteristics (ts) != MATCH_YES)
2596 bad_characteristic = true;
2598 st = next_statement ();
2605 /* If match_deferred_characteristics failed, then there is an error. */
2606 if (bad_characteristic)
2608 ts = &gfc_current_block ()->result->ts;
2609 if (ts->type != BT_DERIVED)
2610 gfc_error ("Bad kind expression for function '%s' at %L",
2611 gfc_current_block ()->name,
2612 &gfc_current_block ()->declared_at);
2614 gfc_error ("The type for function '%s' at %L is not accessible",
2615 gfc_current_block ()->name,
2616 &gfc_current_block ()->declared_at);
2618 gfc_current_block ()->ts.kind = 0;
2619 /* Keep the derived type; if it's bad, it will be discovered later. */
2620 if (!(ts->type == BT_DERIVED && ts->u.derived))
2621 ts->type = BT_UNKNOWN;
2628 /* Parse a WHERE block, (not a simple WHERE statement). */
2631 parse_where_block (void)
2633 int seen_empty_else;
2638 accept_statement (ST_WHERE_BLOCK);
2639 top = gfc_state_stack->tail;
2641 push_state (&s, COMP_WHERE, gfc_new_block);
2643 d = add_statement ();
2644 d->expr1 = top->expr1;
2650 seen_empty_else = 0;
2654 st = next_statement ();
2660 case ST_WHERE_BLOCK:
2661 parse_where_block ();
2666 accept_statement (st);
2670 if (seen_empty_else)
2672 gfc_error ("ELSEWHERE statement at %C follows previous "
2673 "unmasked ELSEWHERE");
2677 if (new_st.expr1 == NULL)
2678 seen_empty_else = 1;
2680 d = new_level (gfc_state_stack->head);
2682 d->expr1 = new_st.expr1;
2684 accept_statement (st);
2689 accept_statement (st);
2693 gfc_error ("Unexpected %s statement in WHERE block at %C",
2694 gfc_ascii_statement (st));
2695 reject_statement ();
2699 while (st != ST_END_WHERE);
2705 /* Parse a FORALL block (not a simple FORALL statement). */
2708 parse_forall_block (void)
2714 accept_statement (ST_FORALL_BLOCK);
2715 top = gfc_state_stack->tail;
2717 push_state (&s, COMP_FORALL, gfc_new_block);
2719 d = add_statement ();
2720 d->op = EXEC_FORALL;
2725 st = next_statement ();
2730 case ST_POINTER_ASSIGNMENT:
2733 accept_statement (st);
2736 case ST_WHERE_BLOCK:
2737 parse_where_block ();
2740 case ST_FORALL_BLOCK:
2741 parse_forall_block ();
2745 accept_statement (st);
2752 gfc_error ("Unexpected %s statement in FORALL block at %C",
2753 gfc_ascii_statement (st));
2755 reject_statement ();
2759 while (st != ST_END_FORALL);
2765 static gfc_statement parse_executable (gfc_statement);
2767 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2770 parse_if_block (void)
2779 accept_statement (ST_IF_BLOCK);
2781 top = gfc_state_stack->tail;
2782 push_state (&s, COMP_IF, gfc_new_block);
2784 new_st.op = EXEC_IF;
2785 d = add_statement ();
2787 d->expr1 = top->expr1;
2793 st = parse_executable (ST_NONE);
2803 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2804 "statement at %L", &else_locus);
2806 reject_statement ();
2810 d = new_level (gfc_state_stack->head);
2812 d->expr1 = new_st.expr1;
2814 accept_statement (st);
2821 gfc_error ("Duplicate ELSE statements at %L and %C",
2823 reject_statement ();
2828 else_locus = gfc_current_locus;
2830 d = new_level (gfc_state_stack->head);
2833 accept_statement (st);
2841 unexpected_statement (st);
2845 while (st != ST_ENDIF);
2848 accept_statement (st);
2852 /* Parse a SELECT block. */
2855 parse_select_block (void)
2861 accept_statement (ST_SELECT_CASE);
2863 cp = gfc_state_stack->tail;
2864 push_state (&s, COMP_SELECT, gfc_new_block);
2866 /* Make sure that the next statement is a CASE or END SELECT. */
2869 st = next_statement ();
2872 if (st == ST_END_SELECT)
2874 /* Empty SELECT CASE is OK. */
2875 accept_statement (st);
2882 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2885 reject_statement ();
2888 /* At this point, we're got a nonempty select block. */
2889 cp = new_level (cp);
2892 accept_statement (st);
2896 st = parse_executable (ST_NONE);
2903 cp = new_level (gfc_state_stack->head);
2905 gfc_clear_new_st ();
2907 accept_statement (st);
2913 /* Can't have an executable statement because of
2914 parse_executable(). */
2916 unexpected_statement (st);
2920 while (st != ST_END_SELECT);
2923 accept_statement (st);
2927 /* Pop the current selector from the SELECT TYPE stack. */
2930 select_type_pop (void)
2932 gfc_select_type_stack *old = select_type_stack;
2933 select_type_stack = old->prev;
2938 /* Parse a SELECT TYPE construct (F03:R821). */
2941 parse_select_type_block (void)
2947 accept_statement (ST_SELECT_TYPE);
2949 cp = gfc_state_stack->tail;
2950 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
2952 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
2956 st = next_statement ();
2959 if (st == ST_END_SELECT)
2960 /* Empty SELECT CASE is OK. */
2962 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
2965 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
2966 "following SELECT TYPE at %C");
2968 reject_statement ();
2971 /* At this point, we're got a nonempty select block. */
2972 cp = new_level (cp);
2975 accept_statement (st);
2979 st = parse_executable (ST_NONE);
2987 cp = new_level (gfc_state_stack->head);
2989 gfc_clear_new_st ();
2991 accept_statement (st);
2997 /* Can't have an executable statement because of
2998 parse_executable(). */
3000 unexpected_statement (st);
3004 while (st != ST_END_SELECT);
3008 accept_statement (st);
3009 gfc_current_ns = gfc_current_ns->parent;
3014 /* Given a symbol, make sure it is not an iteration variable for a DO
3015 statement. This subroutine is called when the symbol is seen in a
3016 context that causes it to become redefined. If the symbol is an
3017 iterator, we generate an error message and return nonzero. */
3020 gfc_check_do_variable (gfc_symtree *st)
3024 for (s=gfc_state_stack; s; s = s->previous)
3025 if (s->do_variable == st)
3027 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
3028 "loop beginning at %L", st->name, &s->head->loc);
3036 /* Checks to see if the current statement label closes an enddo.
3037 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3038 an error) if it incorrectly closes an ENDDO. */
3041 check_do_closure (void)
3045 if (gfc_statement_label == NULL)
3048 for (p = gfc_state_stack; p; p = p->previous)
3049 if (p->state == COMP_DO)
3053 return 0; /* No loops to close */
3055 if (p->ext.end_do_label == gfc_statement_label)
3057 if (p == gfc_state_stack)
3060 gfc_error ("End of nonblock DO statement at %C is within another block");
3064 /* At this point, the label doesn't terminate the innermost loop.
3065 Make sure it doesn't terminate another one. */
3066 for (; p; p = p->previous)
3067 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
3069 gfc_error ("End of nonblock DO statement at %C is interwoven "
3070 "with another DO loop");
3078 /* Parse a series of contained program units. */
3080 static void parse_progunit (gfc_statement);
3083 /* Parse a CRITICAL block. */
3086 parse_critical_block (void)
3092 s.ext.end_do_label = new_st.label1;
3094 accept_statement (ST_CRITICAL);
3095 top = gfc_state_stack->tail;
3097 push_state (&s, COMP_CRITICAL, gfc_new_block);
3099 d = add_statement ();
3100 d->op = EXEC_CRITICAL;
3105 st = parse_executable (ST_NONE);
3113 case ST_END_CRITICAL:
3114 if (s.ext.end_do_label != NULL
3115 && s.ext.end_do_label != gfc_statement_label)
3116 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3117 "match CRITIAL label");
3119 if (gfc_statement_label != NULL)
3121 new_st.op = EXEC_NOP;
3127 unexpected_statement (st);
3131 while (st != ST_END_CRITICAL);
3134 accept_statement (st);
3138 /* Set up the local namespace for a BLOCK construct. */
3141 gfc_build_block_ns (gfc_namespace *parent_ns)
3143 gfc_namespace* my_ns;
3145 my_ns = gfc_get_namespace (parent_ns, 1);
3146 my_ns->construct_entities = 1;
3148 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3149 code generation (so it must not be NULL).
3150 We set its recursive argument if our container procedure is recursive, so
3151 that local variables are accordingly placed on the stack when it
3152 will be necessary. */
3154 my_ns->proc_name = gfc_new_block;
3159 gfc_get_symbol ("block@", my_ns, &my_ns->proc_name);
3160 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
3161 my_ns->proc_name->name, NULL);
3162 gcc_assert (t == SUCCESS);
3165 if (parent_ns->proc_name)
3166 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
3172 /* Parse a BLOCK construct. */
3175 parse_block_construct (void)
3177 gfc_namespace* my_ns;
3180 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BLOCK construct at %C");
3182 my_ns = gfc_build_block_ns (gfc_current_ns);
3184 new_st.op = EXEC_BLOCK;
3185 new_st.ext.block.ns = my_ns;
3186 new_st.ext.block.assoc = NULL;
3187 accept_statement (ST_BLOCK);
3189 push_state (&s, COMP_BLOCK, my_ns->proc_name);
3190 gfc_current_ns = my_ns;
3192 parse_progunit (ST_NONE);
3194 gfc_current_ns = gfc_current_ns->parent;
3199 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3200 behind the scenes with compiler-generated variables. */
3203 parse_associate (void)
3205 gfc_namespace* my_ns;
3208 gfc_association_list* a;
3209 gfc_code* assignTail;
3211 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASSOCIATE construct at %C");
3213 my_ns = gfc_build_block_ns (gfc_current_ns);
3215 new_st.op = EXEC_BLOCK;
3216 new_st.ext.block.ns = my_ns;
3217 gcc_assert (new_st.ext.block.assoc);
3219 /* Add all associations to expressions as BLOCK variables, and create
3220 assignments to them giving their values. */
3221 gfc_current_ns = my_ns;
3223 for (a = new_st.ext.block.assoc; a; a = a->next)
3226 gfc_code* newAssign;
3228 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
3231 /* Note that in certain cases, the target-expression's type is not yet
3232 known and so we have to adapt the symbol's ts also during resolution
3234 a->st->n.sym->ts = a->target->ts;
3235 a->st->n.sym->attr.flavor = FL_VARIABLE;
3236 a->st->n.sym->assoc = a;
3237 gfc_set_sym_referenced (a->st->n.sym);
3239 /* Create the assignment to calculate the expression and set it. */
3240 newAssign = gfc_get_code ();
3241 newAssign->op = EXEC_ASSIGN;
3242 newAssign->loc = gfc_current_locus;
3243 newAssign->expr1 = gfc_get_variable_expr (a->st);
3244 newAssign->expr2 = a->target;
3248 assignTail->next = newAssign;
3250 gfc_current_ns->code = newAssign;
3251 assignTail = newAssign;
3255 gfc_error ("Association to variables is not yet supported at %C");
3258 gcc_assert (assignTail);
3260 accept_statement (ST_ASSOCIATE);
3261 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
3264 st = parse_executable (ST_NONE);
3271 accept_statement (st);
3272 assignTail->next = gfc_state_stack->head;
3276 unexpected_statement (st);
3280 gfc_current_ns = gfc_current_ns->parent;
3285 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
3286 handled inside of parse_executable(), because they aren't really
3290 parse_do_block (void)
3297 s.ext.end_do_label = new_st.label1;
3299 if (new_st.ext.iterator != NULL)
3300 stree = new_st.ext.iterator->var->symtree;
3304 accept_statement (ST_DO);
3306 top = gfc_state_stack->tail;
3307 push_state (&s, COMP_DO, gfc_new_block);
3309 s.do_variable = stree;
3311 top->block = new_level (top);
3312 top->block->op = EXEC_DO;
3315 st = parse_executable (ST_NONE);
3323 if (s.ext.end_do_label != NULL
3324 && s.ext.end_do_label != gfc_statement_label)
3325 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
3328 if (gfc_statement_label != NULL)
3330 new_st.op = EXEC_NOP;
3335 case ST_IMPLIED_ENDDO:
3336 /* If the do-stmt of this DO construct has a do-construct-name,
3337 the corresponding end-do must be an end-do-stmt (with a matching
3338 name, but in that case we must have seen ST_ENDDO first).
3339 We only complain about this in pedantic mode. */
3340 if (gfc_current_block () != NULL)
3341 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
3342 &gfc_current_block()->declared_at);
3347 unexpected_statement (st);
3352 accept_statement (st);
3356 /* Parse the statements of OpenMP do/parallel do. */
3358 static gfc_statement
3359 parse_omp_do (gfc_statement omp_st)
3365 accept_statement (omp_st);
3367 cp = gfc_state_stack->tail;
3368 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3369 np = new_level (cp);
3375 st = next_statement ();
3378 else if (st == ST_DO)
3381 unexpected_statement (st);
3385 if (gfc_statement_label != NULL
3386 && gfc_state_stack->previous != NULL
3387 && gfc_state_stack->previous->state == COMP_DO
3388 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
3396 there should be no !$OMP END DO. */
3398 return ST_IMPLIED_ENDDO;
3401 check_do_closure ();
3404 st = next_statement ();
3405 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
3407 if (new_st.op == EXEC_OMP_END_NOWAIT)
3408 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
3410 gcc_assert (new_st.op == EXEC_NOP);
3411 gfc_clear_new_st ();
3412 gfc_commit_symbols ();
3413 gfc_warning_check ();
3414 st = next_statement ();
3420 /* Parse the statements of OpenMP atomic directive. */
3423 parse_omp_atomic (void)
3429 accept_statement (ST_OMP_ATOMIC);
3431 cp = gfc_state_stack->tail;
3432 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3433 np = new_level (cp);
3439 st = next_statement ();
3442 else if (st == ST_ASSIGNMENT)
3445 unexpected_statement (st);
3448 accept_statement (st);
3454 /* Parse the statements of an OpenMP structured block. */
3457 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
3459 gfc_statement st, omp_end_st;
3463 accept_statement (omp_st);
3465 cp = gfc_state_stack->tail;
3466 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3467 np = new_level (cp);
3473 case ST_OMP_PARALLEL:
3474 omp_end_st = ST_OMP_END_PARALLEL;
3476 case ST_OMP_PARALLEL_SECTIONS:
3477 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
3479 case ST_OMP_SECTIONS:
3480 omp_end_st = ST_OMP_END_SECTIONS;
3482 case ST_OMP_ORDERED:
3483 omp_end_st = ST_OMP_END_ORDERED;
3485 case ST_OMP_CRITICAL:
3486 omp_end_st = ST_OMP_END_CRITICAL;
3489 omp_end_st = ST_OMP_END_MASTER;
3492 omp_end_st = ST_OMP_END_SINGLE;
3495 omp_end_st = ST_OMP_END_TASK;
3497 case ST_OMP_WORKSHARE:
3498 omp_end_st = ST_OMP_END_WORKSHARE;
3500 case ST_OMP_PARALLEL_WORKSHARE:
3501 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
3509 if (workshare_stmts_only)
3511 /* Inside of !$omp workshare, only
3514 where statements and constructs
3515 forall statements and constructs
3519 are allowed. For !$omp critical these
3520 restrictions apply recursively. */
3523 st = next_statement ();
3534 accept_statement (st);
3537 case ST_WHERE_BLOCK:
3538 parse_where_block ();
3541 case ST_FORALL_BLOCK:
3542 parse_forall_block ();
3545 case ST_OMP_PARALLEL:
3546 case ST_OMP_PARALLEL_SECTIONS:
3547 parse_omp_structured_block (st, false);
3550 case ST_OMP_PARALLEL_WORKSHARE:
3551 case ST_OMP_CRITICAL:
3552 parse_omp_structured_block (st, true);
3555 case ST_OMP_PARALLEL_DO:
3556 st = parse_omp_do (st);
3560 parse_omp_atomic ();
3571 st = next_statement ();
3575 st = parse_executable (ST_NONE);
3578 else if (st == ST_OMP_SECTION
3579 && (omp_st == ST_OMP_SECTIONS
3580 || omp_st == ST_OMP_PARALLEL_SECTIONS))
3582 np = new_level (np);
3586 else if (st != omp_end_st)
3587 unexpected_statement (st);
3589 while (st != omp_end_st);
3593 case EXEC_OMP_END_NOWAIT:
3594 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
3596 case EXEC_OMP_CRITICAL:
3597 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
3598 || (new_st.ext.omp_name != NULL
3599 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
3600 gfc_error ("Name after !$omp critical and !$omp end critical does "
3602 gfc_free (CONST_CAST (char *, new_st.ext.omp_name));
3604 case EXEC_OMP_END_SINGLE:
3605 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
3606 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
3607 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
3608 gfc_free_omp_clauses (new_st.ext.omp_clauses);
3616 gfc_clear_new_st ();
3617 gfc_commit_symbols ();
3618 gfc_warning_check ();
3623 /* Accept a series of executable statements. We return the first
3624 statement that doesn't fit to the caller. Any block statements are
3625 passed on to the correct handler, which usually passes the buck
3628 static gfc_statement
3629 parse_executable (gfc_statement st)
3634 st = next_statement ();
3638 close_flag = check_do_closure ();
3643 case ST_END_PROGRAM:
3646 case ST_END_FUNCTION:
3651 case ST_END_SUBROUTINE:
3656 case ST_SELECT_CASE:
3657 gfc_error ("%s statement at %C cannot terminate a non-block "
3658 "DO loop", gfc_ascii_statement (st));
3674 accept_statement (st);
3675 if (close_flag == 1)
3676 return ST_IMPLIED_ENDDO;
3680 parse_block_construct ();
3691 case ST_SELECT_CASE:
3692 parse_select_block ();
3695 case ST_SELECT_TYPE:
3696 parse_select_type_block();
3701 if (check_do_closure () == 1)
3702 return ST_IMPLIED_ENDDO;
3706 parse_critical_block ();
3709 case ST_WHERE_BLOCK:
3710 parse_where_block ();
3713 case ST_FORALL_BLOCK:
3714 parse_forall_block ();
3717 case ST_OMP_PARALLEL:
3718 case ST_OMP_PARALLEL_SECTIONS:
3719 case ST_OMP_SECTIONS:
3720 case ST_OMP_ORDERED:
3721 case ST_OMP_CRITICAL:
3725 parse_omp_structured_block (st, false);
3728 case ST_OMP_WORKSHARE:
3729 case ST_OMP_PARALLEL_WORKSHARE:
3730 parse_omp_structured_block (st, true);
3734 case ST_OMP_PARALLEL_DO:
3735 st = parse_omp_do (st);
3736 if (st == ST_IMPLIED_ENDDO)
3741 parse_omp_atomic ();
3748 st = next_statement ();
3753 /* Fix the symbols for sibling functions. These are incorrectly added to
3754 the child namespace as the parser didn't know about this procedure. */
3757 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
3761 gfc_symbol *old_sym;
3763 sym->attr.referenced = 1;
3764 for (ns = siblings; ns; ns = ns->sibling)
3766 st = gfc_find_symtree (ns->sym_root, sym->name);
3768 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
3769 goto fixup_contained;
3771 old_sym = st->n.sym;
3772 if (old_sym->ns == ns
3773 && !old_sym->attr.contained
3775 /* By 14.6.1.3, host association should be excluded
3776 for the following. */
3777 && !(old_sym->attr.external
3778 || (old_sym->ts.type != BT_UNKNOWN
3779 && !old_sym->attr.implicit_type)
3780 || old_sym->attr.flavor == FL_PARAMETER
3781 || old_sym->attr.use_assoc
3782 || old_sym->attr.in_common
3783 || old_sym->attr.in_equivalence
3784 || old_sym->attr.data
3785 || old_sym->attr.dummy
3786 || old_sym->attr.result
3787 || old_sym->attr.dimension
3788 || old_sym->attr.allocatable
3789 || old_sym->attr.intrinsic
3790 || old_sym->attr.generic
3791 || old_sym->attr.flavor == FL_NAMELIST
3792 || old_sym->attr.proc == PROC_ST_FUNCTION))
3794 /* Replace it with the symbol from the parent namespace. */
3798 gfc_release_symbol (old_sym);
3802 /* Do the same for any contained procedures. */
3803 gfc_fixup_sibling_symbols (sym, ns->contained);
3808 parse_contained (int module)
3810 gfc_namespace *ns, *parent_ns, *tmp;
3811 gfc_state_data s1, s2;
3815 int contains_statements = 0;
3818 push_state (&s1, COMP_CONTAINS, NULL);
3819 parent_ns = gfc_current_ns;
3823 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
3825 gfc_current_ns->sibling = parent_ns->contained;
3826 parent_ns->contained = gfc_current_ns;
3829 /* Process the next available statement. We come here if we got an error
3830 and rejected the last statement. */
3831 st = next_statement ();
3840 contains_statements = 1;
3841 accept_statement (st);
3844 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
3847 /* For internal procedures, create/update the symbol in the
3848 parent namespace. */
3852 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
3853 gfc_error ("Contained procedure '%s' at %C is already "
3854 "ambiguous", gfc_new_block->name);
3857 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
3858 &gfc_new_block->declared_at) ==
3861 if (st == ST_FUNCTION)
3862 gfc_add_function (&sym->attr, sym->name,
3863 &gfc_new_block->declared_at);
3865 gfc_add_subroutine (&sym->attr, sym->name,
3866 &gfc_new_block->declared_at);
3870 gfc_commit_symbols ();
3873 sym = gfc_new_block;
3875 /* Mark this as a contained function, so it isn't replaced
3876 by other module functions. */
3877 sym->attr.contained = 1;
3878 sym->attr.referenced = 1;
3880 parse_progunit (ST_NONE);
3882 /* Fix up any sibling functions that refer to this one. */
3883 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
3884 /* Or refer to any of its alternate entry points. */
3885 for (el = gfc_current_ns->entries; el; el = el->next)
3886 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
3888 gfc_current_ns->code = s2.head;
3889 gfc_current_ns = parent_ns;
3894 /* These statements are associated with the end of the host unit. */
3895 case ST_END_FUNCTION:
3897 case ST_END_PROGRAM:
3898 case ST_END_SUBROUTINE:
3899 accept_statement (st);
3903 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
3904 gfc_ascii_statement (st));
3905 reject_statement ();
3911 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
3912 && st != ST_END_MODULE && st != ST_END_PROGRAM);
3914 /* The first namespace in the list is guaranteed to not have
3915 anything (worthwhile) in it. */
3916 tmp = gfc_current_ns;
3917 gfc_current_ns = parent_ns;
3918 if (seen_error && tmp->refs > 1)
3919 gfc_free_namespace (tmp);
3921 ns = gfc_current_ns->contained;
3922 gfc_current_ns->contained = ns->sibling;
3923 gfc_free_namespace (ns);
3926 if (!contains_statements)
3927 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTAINS statement without "
3928 "FUNCTION or SUBROUTINE statement at %C");
3932 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
3935 parse_progunit (gfc_statement st)
3940 st = parse_spec (st);
3947 /* This is not allowed within BLOCK! */
3948 if (gfc_current_state () != COMP_BLOCK)
3953 accept_statement (st);
3960 if (gfc_current_state () == COMP_FUNCTION)
3961 gfc_check_function_type (gfc_current_ns);
3966 st = parse_executable (st);
3974 /* This is not allowed within BLOCK! */
3975 if (gfc_current_state () != COMP_BLOCK)
3980 accept_statement (st);
3987 unexpected_statement (st);
3988 reject_statement ();
3989 st = next_statement ();
3995 for (p = gfc_state_stack; p; p = p->previous)
3996 if (p->state == COMP_CONTAINS)
3999 if (gfc_find_state (COMP_MODULE) == SUCCESS)
4004 gfc_error ("CONTAINS statement at %C is already in a contained "
4006 reject_statement ();
4007 st = next_statement ();
4011 parse_contained (0);
4014 gfc_current_ns->code = gfc_state_stack->head;
4018 /* Come here to complain about a global symbol already in use as
4022 gfc_global_used (gfc_gsymbol *sym, locus *where)
4027 where = &gfc_current_locus;
4037 case GSYM_SUBROUTINE:
4038 name = "SUBROUTINE";
4043 case GSYM_BLOCK_DATA:
4044 name = "BLOCK DATA";
4050 gfc_internal_error ("gfc_global_used(): Bad type");
4054 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
4055 sym->name, where, name, &sym->where);
4059 /* Parse a block data program unit. */
4062 parse_block_data (void)
4065 static locus blank_locus;
4066 static int blank_block=0;
4069 gfc_current_ns->proc_name = gfc_new_block;
4070 gfc_current_ns->is_block_data = 1;
4072 if (gfc_new_block == NULL)
4075 gfc_error ("Blank BLOCK DATA at %C conflicts with "
4076 "prior BLOCK DATA at %L", &blank_locus);
4080 blank_locus = gfc_current_locus;
4085 s = gfc_get_gsymbol (gfc_new_block->name);
4087 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
4088 gfc_global_used(s, NULL);
4091 s->type = GSYM_BLOCK_DATA;
4092 s->where = gfc_current_locus;
4097 st = parse_spec (ST_NONE);
4099 while (st != ST_END_BLOCK_DATA)
4101 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
4102 gfc_ascii_statement (st));
4103 reject_statement ();
4104 st = next_statement ();
4109 /* Parse a module subprogram. */
4117 s = gfc_get_gsymbol (gfc_new_block->name);
4118 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
4119 gfc_global_used(s, NULL);
4122 s->type = GSYM_MODULE;
4123 s->where = gfc_current_locus;
4127 st = parse_spec (ST_NONE);
4136 parse_contained (1);
4140 accept_statement (st);
4144 gfc_error ("Unexpected %s statement in MODULE at %C",
4145 gfc_ascii_statement (st));
4147 reject_statement ();
4148 st = next_statement ();
4152 s->ns = gfc_current_ns;
4156 /* Add a procedure name to the global symbol table. */
4159 add_global_procedure (int sub)
4163 s = gfc_get_gsymbol(gfc_new_block->name);
4166 || (s->type != GSYM_UNKNOWN
4167 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
4168 gfc_global_used(s, NULL);
4171 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4172 s->where = gfc_current_locus;
4174 s->ns = gfc_current_ns;
4179 /* Add a program to the global symbol table. */
4182 add_global_program (void)
4186 if (gfc_new_block == NULL)
4188 s = gfc_get_gsymbol (gfc_new_block->name);
4190 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
4191 gfc_global_used(s, NULL);
4194 s->type = GSYM_PROGRAM;
4195 s->where = gfc_current_locus;
4197 s->ns = gfc_current_ns;
4202 /* Resolve all the program units when whole file scope option
4205 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
4207 gfc_free_dt_list ();
4208 gfc_current_ns = gfc_global_ns_list;
4209 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4211 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4212 gfc_resolve (gfc_current_ns);
4213 gfc_current_ns->derived_types = gfc_derived_types;
4214 gfc_derived_types = NULL;
4220 clean_up_modules (gfc_gsymbol *gsym)
4225 clean_up_modules (gsym->left);
4226 clean_up_modules (gsym->right);
4228 if (gsym->type != GSYM_MODULE || !gsym->ns)
4231 gfc_current_ns = gsym->ns;
4232 gfc_derived_types = gfc_current_ns->derived_types;
4239 /* Translate all the program units when whole file scope option
4240 is active. This could be in a different order to resolution if
4241 there are forward references in the file. */
4243 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
4247 gfc_current_ns = gfc_global_ns_list;
4248 gfc_get_errors (NULL, &errors);
4250 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4252 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4253 gfc_derived_types = gfc_current_ns->derived_types;
4254 gfc_generate_code (gfc_current_ns);
4255 gfc_current_ns->translated = 1;
4258 /* Clean up all the namespaces after translation. */
4259 gfc_current_ns = gfc_global_ns_list;
4260 for (;gfc_current_ns;)
4262 gfc_namespace *ns = gfc_current_ns->sibling;
4263 gfc_derived_types = gfc_current_ns->derived_types;
4265 gfc_current_ns = ns;
4268 clean_up_modules (gfc_gsym_root);
4272 /* Top level parser. */
4275 gfc_parse_file (void)
4277 int seen_program, errors_before, errors;
4278 gfc_state_data top, s;
4281 gfc_namespace *next;
4283 gfc_start_source_files ();
4285 top.state = COMP_NONE;
4287 top.previous = NULL;
4288 top.head = top.tail = NULL;
4289 top.do_variable = NULL;
4291 gfc_state_stack = ⊤
4293 gfc_clear_new_st ();
4295 gfc_statement_label = NULL;
4297 if (setjmp (eof_buf))
4298 return FAILURE; /* Come here on unexpected EOF */
4300 /* Prepare the global namespace that will contain the
4302 gfc_global_ns_list = next = NULL;
4306 /* Exit early for empty files. */
4312 st = next_statement ();
4321 goto duplicate_main;
4323 prog_locus = gfc_current_locus;
4325 push_state (&s, COMP_PROGRAM, gfc_new_block);
4326 main_program_symbol(gfc_current_ns, gfc_new_block->name);
4327 accept_statement (st);
4328 add_global_program ();
4329 parse_progunit (ST_NONE);
4330 if (gfc_option.flag_whole_file)
4335 add_global_procedure (1);
4336 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
4337 accept_statement (st);
4338 parse_progunit (ST_NONE);
4339 if (gfc_option.flag_whole_file)
4344 add_global_procedure (0);
4345 push_state (&s, COMP_FUNCTION, gfc_new_block);
4346 accept_statement (st);
4347 parse_progunit (ST_NONE);
4348 if (gfc_option.flag_whole_file)
4353 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
4354 accept_statement (st);
4355 parse_block_data ();
4359 push_state (&s, COMP_MODULE, gfc_new_block);
4360 accept_statement (st);
4362 gfc_get_errors (NULL, &errors_before);
4366 /* Anything else starts a nameless main program block. */
4369 goto duplicate_main;
4371 prog_locus = gfc_current_locus;
4373 push_state (&s, COMP_PROGRAM, gfc_new_block);
4374 main_program_symbol (gfc_current_ns, "MAIN__");
4375 parse_progunit (st);
4376 if (gfc_option.flag_whole_file)
4381 /* Handle the non-program units. */
4382 gfc_current_ns->code = s.head;
4384 gfc_resolve (gfc_current_ns);
4386 /* Dump the parse tree if requested. */
4387 if (gfc_option.dump_parse_tree)
4388 gfc_dump_parse_tree (gfc_current_ns, stdout);
4390 gfc_get_errors (NULL, &errors);
4391 if (s.state == COMP_MODULE)
4393 gfc_dump_module (s.sym->name, errors_before == errors);
4395 gfc_generate_module_code (gfc_current_ns);
4397 if (!gfc_option.flag_whole_file)
4401 gfc_current_ns->derived_types = gfc_derived_types;
4402 gfc_derived_types = NULL;
4403 gfc_current_ns = NULL;
4409 gfc_generate_code (gfc_current_ns);
4417 /* The main program and non-contained procedures are put
4418 in the global namespace list, so that they can be processed
4419 later and all their interfaces resolved. */
4420 gfc_current_ns->code = s.head;
4423 for (; next->sibling; next = next->sibling)
4425 next->sibling = gfc_current_ns;
4428 gfc_global_ns_list = gfc_current_ns;
4430 next = gfc_current_ns;
4437 if (!gfc_option.flag_whole_file)
4440 /* Do the resolution. */
4441 resolve_all_program_units (gfc_global_ns_list);
4443 /* Do the parse tree dump. */
4445 = gfc_option.dump_parse_tree ? gfc_global_ns_list : NULL;
4447 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4449 gfc_dump_parse_tree (gfc_current_ns, stdout);
4450 fputs ("------------------------------------------\n\n", stdout);
4453 /* Do the translation. */
4454 translate_all_program_units (gfc_global_ns_list);
4458 gfc_end_source_files ();
4462 /* If we see a duplicate main program, shut down. If the second
4463 instance is an implied main program, i.e. data decls or executable
4464 statements, we're in for lots of errors. */
4465 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
4466 reject_statement ();