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);
145 match ("data", gfc_match_data, ST_DATA);
146 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
150 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
151 match ("entry% ", gfc_match_entry, ST_ENTRY);
152 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
153 match ("external", gfc_match_external, ST_ATTR_DECL);
157 match ("format", gfc_match_format, ST_FORMAT);
164 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
165 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
166 match ("interface", gfc_match_interface, ST_INTERFACE);
167 match ("intent", gfc_match_intent, ST_ATTR_DECL);
168 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
175 match ("namelist", gfc_match_namelist, ST_NAMELIST);
179 match ("optional", gfc_match_optional, ST_ATTR_DECL);
183 match ("parameter", gfc_match_parameter, ST_PARAMETER);
184 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
185 if (gfc_match_private (&st) == MATCH_YES)
187 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
188 if (gfc_match_public (&st) == MATCH_YES)
190 match ("protected", gfc_match_protected, ST_ATTR_DECL);
197 match ("save", gfc_match_save, ST_ATTR_DECL);
201 match ("target", gfc_match_target, ST_ATTR_DECL);
202 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
209 match ("value", gfc_match_value, ST_ATTR_DECL);
210 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
217 /* This is not a specification statement. See if any of the matchers
218 has stored an error message of some sort. */
222 gfc_buffer_error (0);
223 gfc_current_locus = old_locus;
225 return ST_GET_FCN_CHARACTERISTICS;
229 /* This is the primary 'decode_statement'. */
231 decode_statement (void)
242 gfc_clear_error (); /* Clear any pending errors. */
243 gfc_clear_warning (); /* Clear any pending warnings. */
245 gfc_matching_function = false;
247 if (gfc_match_eos () == MATCH_YES)
250 if (gfc_current_state () == COMP_FUNCTION
251 && gfc_current_block ()->result->ts.kind == -1)
252 return decode_specification_statement ();
254 old_locus = gfc_current_locus;
256 /* Try matching a data declaration or function declaration. The
257 input "REALFUNCTIONA(N)" can mean several things in different
258 contexts, so it (and its relatives) get special treatment. */
260 if (gfc_current_state () == COMP_NONE
261 || gfc_current_state () == COMP_INTERFACE
262 || gfc_current_state () == COMP_CONTAINS)
264 gfc_matching_function = true;
265 m = gfc_match_function_decl ();
268 else if (m == MATCH_ERROR)
272 gfc_current_locus = old_locus;
274 gfc_matching_function = false;
277 /* Match statements whose error messages are meant to be overwritten
278 by something better. */
280 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
281 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
282 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
284 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
285 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
287 /* Try to match a subroutine statement, which has the same optional
288 prefixes that functions can have. */
290 if (gfc_match_subroutine () == MATCH_YES)
291 return ST_SUBROUTINE;
293 gfc_current_locus = old_locus;
295 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, and BLOCK
296 statements, which might begin with a block label. The match functions for
297 these statements are unusual in that their keyword is not seen before
298 the matcher is called. */
300 if (gfc_match_if (&st) == MATCH_YES)
303 gfc_current_locus = old_locus;
305 if (gfc_match_where (&st) == MATCH_YES)
308 gfc_current_locus = old_locus;
310 if (gfc_match_forall (&st) == MATCH_YES)
313 gfc_current_locus = old_locus;
315 match (NULL, gfc_match_do, ST_DO);
316 match (NULL, gfc_match_block, ST_BLOCK);
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 ("cycle", gfc_match_cycle, ST_CYCLE);
349 match ("case", gfc_match_case, ST_CASE);
350 match ("common", gfc_match_common, ST_COMMON);
351 match ("contains", gfc_match_eos, ST_CONTAINS);
352 match ("class", gfc_match_class_is, ST_CLASS_IS);
353 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
357 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
358 match ("data", gfc_match_data, ST_DATA);
359 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
363 match ("end file", gfc_match_endfile, ST_END_FILE);
364 match ("exit", gfc_match_exit, ST_EXIT);
365 match ("else", gfc_match_else, ST_ELSE);
366 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
367 match ("else if", gfc_match_elseif, ST_ELSEIF);
368 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
369 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
371 if (gfc_match_end (&st) == MATCH_YES)
374 match ("entry% ", gfc_match_entry, ST_ENTRY);
375 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
376 match ("external", gfc_match_external, ST_ATTR_DECL);
380 match ("final", gfc_match_final_decl, ST_FINAL);
381 match ("flush", gfc_match_flush, ST_FLUSH);
382 match ("format", gfc_match_format, ST_FORMAT);
386 match ("generic", gfc_match_generic, ST_GENERIC);
387 match ("go to", gfc_match_goto, ST_GOTO);
391 match ("inquire", gfc_match_inquire, ST_INQUIRE);
392 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
393 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
394 match ("import", gfc_match_import, ST_IMPORT);
395 match ("interface", gfc_match_interface, ST_INTERFACE);
396 match ("intent", gfc_match_intent, ST_ATTR_DECL);
397 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
401 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
402 match ("module", gfc_match_module, ST_MODULE);
406 match ("nullify", gfc_match_nullify, ST_NULLIFY);
407 match ("namelist", gfc_match_namelist, ST_NAMELIST);
411 match ("open", gfc_match_open, ST_OPEN);
412 match ("optional", gfc_match_optional, ST_ATTR_DECL);
416 match ("print", gfc_match_print, ST_WRITE);
417 match ("parameter", gfc_match_parameter, ST_PARAMETER);
418 match ("pause", gfc_match_pause, ST_PAUSE);
419 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
420 if (gfc_match_private (&st) == MATCH_YES)
422 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
423 match ("program", gfc_match_program, ST_PROGRAM);
424 if (gfc_match_public (&st) == MATCH_YES)
426 match ("protected", gfc_match_protected, ST_ATTR_DECL);
430 match ("read", gfc_match_read, ST_READ);
431 match ("return", gfc_match_return, ST_RETURN);
432 match ("rewind", gfc_match_rewind, ST_REWIND);
436 match ("sequence", gfc_match_eos, ST_SEQUENCE);
437 match ("stop", gfc_match_stop, ST_STOP);
438 match ("save", gfc_match_save, ST_ATTR_DECL);
439 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
440 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
441 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
445 match ("target", gfc_match_target, ST_ATTR_DECL);
446 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
447 match ("type is", gfc_match_type_is, ST_TYPE_IS);
451 match ("use", gfc_match_use, ST_USE);
455 match ("value", gfc_match_value, ST_ATTR_DECL);
456 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
460 match ("wait", gfc_match_wait, ST_WAIT);
461 match ("write", gfc_match_write, ST_WRITE);
465 /* All else has failed, so give up. See if any of the matchers has
466 stored an error message of some sort. */
468 if (gfc_error_check () == 0)
469 gfc_error_now ("Unclassifiable statement at %C");
473 gfc_error_recovery ();
479 decode_omp_directive (void)
488 gfc_clear_error (); /* Clear any pending errors. */
489 gfc_clear_warning (); /* Clear any pending warnings. */
493 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
494 "or ELEMENTAL procedures");
495 gfc_error_recovery ();
499 old_locus = gfc_current_locus;
501 /* General OpenMP directive matching: Instead of testing every possible
502 statement, we eliminate most possibilities by peeking at the
505 c = gfc_peek_ascii_char ();
510 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
513 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
516 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
519 match ("do", gfc_match_omp_do, ST_OMP_DO);
522 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
523 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
524 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
525 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
526 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
527 match ("end parallel sections", gfc_match_omp_eos,
528 ST_OMP_END_PARALLEL_SECTIONS);
529 match ("end parallel workshare", gfc_match_omp_eos,
530 ST_OMP_END_PARALLEL_WORKSHARE);
531 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
532 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
533 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
534 match ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
535 match ("end workshare", gfc_match_omp_end_nowait,
536 ST_OMP_END_WORKSHARE);
539 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
542 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
545 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
548 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
549 match ("parallel sections", gfc_match_omp_parallel_sections,
550 ST_OMP_PARALLEL_SECTIONS);
551 match ("parallel workshare", gfc_match_omp_parallel_workshare,
552 ST_OMP_PARALLEL_WORKSHARE);
553 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
556 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
557 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
558 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
561 match ("task", gfc_match_omp_task, ST_OMP_TASK);
562 match ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
563 match ("threadprivate", gfc_match_omp_threadprivate,
564 ST_OMP_THREADPRIVATE);
566 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
570 /* All else has failed, so give up. See if any of the matchers has
571 stored an error message of some sort. */
573 if (gfc_error_check () == 0)
574 gfc_error_now ("Unclassifiable OpenMP directive at %C");
578 gfc_error_recovery ();
584 decode_gcc_attribute (void)
592 gfc_clear_error (); /* Clear any pending errors. */
593 gfc_clear_warning (); /* Clear any pending warnings. */
594 old_locus = gfc_current_locus;
596 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
598 /* All else has failed, so give up. See if any of the matchers has
599 stored an error message of some sort. */
601 if (gfc_error_check () == 0)
602 gfc_error_now ("Unclassifiable GCC directive at %C");
606 gfc_error_recovery ();
614 /* Get the next statement in free form source. */
623 at_bol = gfc_at_bol ();
624 gfc_gobble_whitespace ();
626 c = gfc_peek_ascii_char ();
632 /* Found a statement label? */
633 m = gfc_match_st_label (&gfc_statement_label);
635 d = gfc_peek_ascii_char ();
636 if (m != MATCH_YES || !gfc_is_whitespace (d))
638 gfc_match_small_literal_int (&i, &cnt);
641 gfc_error_now ("Too many digits in statement label at %C");
644 gfc_error_now ("Zero is not a valid statement label at %C");
647 c = gfc_next_ascii_char ();
650 if (!gfc_is_whitespace (c))
651 gfc_error_now ("Non-numeric character in statement label at %C");
657 label_locus = gfc_current_locus;
659 gfc_gobble_whitespace ();
661 if (at_bol && gfc_peek_ascii_char () == ';')
663 gfc_error_now ("Semicolon at %C needs to be preceded by "
665 gfc_next_ascii_char (); /* Eat up the semicolon. */
669 if (gfc_match_eos () == MATCH_YES)
671 gfc_warning_now ("Ignoring statement label in empty statement "
672 "at %L", &label_locus);
673 gfc_free_st_label (gfc_statement_label);
674 gfc_statement_label = NULL;
681 /* Comments have already been skipped by the time we get here,
682 except for GCC attributes and OpenMP directives. */
684 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
685 c = gfc_peek_ascii_char ();
691 c = gfc_next_ascii_char ();
692 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
693 gcc_assert (c == "gcc$"[i]);
695 gfc_gobble_whitespace ();
696 return decode_gcc_attribute ();
699 else if (c == '$' && gfc_option.flag_openmp)
703 c = gfc_next_ascii_char ();
704 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
705 gcc_assert (c == "$omp"[i]);
707 gcc_assert (c == ' ' || c == '\t');
708 gfc_gobble_whitespace ();
709 return decode_omp_directive ();
715 if (at_bol && c == ';')
717 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
718 gfc_next_ascii_char (); /* Eat up the semicolon. */
722 return decode_statement ();
726 /* Get the next statement in fixed-form source. */
731 int label, digit_flag, i;
736 return decode_statement ();
738 /* Skip past the current label field, parsing a statement label if
739 one is there. This is a weird number parser, since the number is
740 contained within five columns and can have any kind of embedded
741 spaces. We also check for characters that make the rest of the
747 for (i = 0; i < 5; i++)
749 c = gfc_next_char_literal (0);
766 label = label * 10 + ((unsigned char) c - '0');
767 label_locus = gfc_current_locus;
771 /* Comments have already been skipped by the time we get
772 here, except for GCC attributes and OpenMP directives. */
775 c = gfc_next_char_literal (0);
777 if (TOLOWER (c) == 'g')
779 for (i = 0; i < 4; i++, c = gfc_next_char_literal (0))
780 gcc_assert (TOLOWER (c) == "gcc$"[i]);
782 return decode_gcc_attribute ();
784 else if (c == '$' && gfc_option.flag_openmp)
786 for (i = 0; i < 4; i++, c = gfc_next_char_literal (0))
787 gcc_assert ((char) gfc_wide_tolower (c) == "$omp"[i]);
789 if (c != ' ' && c != '0')
791 gfc_buffer_error (0);
792 gfc_error ("Bad continuation line at %C");
796 return decode_omp_directive ();
800 /* Comments have already been skipped by the time we get
801 here so don't bother checking for them. */
804 gfc_buffer_error (0);
805 gfc_error ("Non-numeric character in statement label at %C");
813 gfc_warning_now ("Zero is not a valid statement label at %C");
816 /* We've found a valid statement label. */
817 gfc_statement_label = gfc_get_st_label (label);
821 /* Since this line starts a statement, it cannot be a continuation
822 of a previous statement. If we see something here besides a
823 space or zero, it must be a bad continuation line. */
825 c = gfc_next_char_literal (0);
829 if (c != ' ' && c != '0')
831 gfc_buffer_error (0);
832 gfc_error ("Bad continuation line at %C");
836 /* Now that we've taken care of the statement label columns, we have
837 to make sure that the first nonblank character is not a '!'. If
838 it is, the rest of the line is a comment. */
842 loc = gfc_current_locus;
843 c = gfc_next_char_literal (0);
845 while (gfc_is_whitespace (c));
849 gfc_current_locus = loc;
853 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
857 if (gfc_match_eos () == MATCH_YES)
860 /* At this point, we've got a nonblank statement to parse. */
861 return decode_statement ();
865 gfc_warning_now ("Ignoring statement label in empty statement at %L",
868 gfc_current_locus.lb->truncated = 0;
874 /* Return the next non-ST_NONE statement to the caller. We also worry
875 about including files and the ends of include files at this stage. */
878 next_statement (void)
883 gfc_new_block = NULL;
885 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
888 gfc_statement_label = NULL;
889 gfc_buffer_error (1);
894 gfc_skip_comments ();
902 if (gfc_define_undef_line ())
905 old_locus = gfc_current_locus;
907 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
913 gfc_buffer_error (0);
915 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
917 gfc_free_st_label (gfc_statement_label);
918 gfc_statement_label = NULL;
919 gfc_current_locus = old_locus;
923 check_statement_label (st);
929 /****************************** Parser ***********************************/
931 /* The parser subroutines are of type 'try' that fail if the file ends
934 /* Macros that expand to case-labels for various classes of
935 statements. Start with executable statements that directly do
938 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
939 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
940 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
941 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
942 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
943 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
944 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
945 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
946 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_ERROR_STOP: \
947 case ST_SYNC_ALL: case ST_SYNC_IMAGES: case ST_SYNC_MEMORY
949 /* Statements that mark other executable statements. */
951 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
952 case ST_IF_BLOCK: case ST_BLOCK: \
953 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
954 case ST_OMP_PARALLEL: \
955 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
956 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
957 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
958 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
959 case ST_OMP_TASK: case ST_CRITICAL
961 /* Declaration statements */
963 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
964 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
965 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
968 /* Block end statements. Errors associated with interchanging these
969 are detected in gfc_match_end(). */
971 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
972 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
976 /* Push a new state onto the stack. */
979 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
981 p->state = new_state;
982 p->previous = gfc_state_stack;
984 p->head = p->tail = NULL;
985 p->do_variable = NULL;
990 /* Pop the current state. */
994 gfc_state_stack = gfc_state_stack->previous;
998 /* Try to find the given state in the state stack. */
1001 gfc_find_state (gfc_compile_state state)
1005 for (p = gfc_state_stack; p; p = p->previous)
1006 if (p->state == state)
1009 return (p == NULL) ? FAILURE : SUCCESS;
1013 /* Starts a new level in the statement list. */
1016 new_level (gfc_code *q)
1020 p = q->block = gfc_get_code ();
1022 gfc_state_stack->head = gfc_state_stack->tail = p;
1028 /* Add the current new_st code structure and adds it to the current
1029 program unit. As a side-effect, it zeroes the new_st. */
1032 add_statement (void)
1036 p = gfc_get_code ();
1039 p->loc = gfc_current_locus;
1041 if (gfc_state_stack->head == NULL)
1042 gfc_state_stack->head = p;
1044 gfc_state_stack->tail->next = p;
1046 while (p->next != NULL)
1049 gfc_state_stack->tail = p;
1051 gfc_clear_new_st ();
1057 /* Frees everything associated with the current statement. */
1060 undo_new_statement (void)
1062 gfc_free_statements (new_st.block);
1063 gfc_free_statements (new_st.next);
1064 gfc_free_statement (&new_st);
1065 gfc_clear_new_st ();
1069 /* If the current statement has a statement label, make sure that it
1070 is allowed to, or should have one. */
1073 check_statement_label (gfc_statement st)
1077 if (gfc_statement_label == NULL)
1079 if (st == ST_FORMAT)
1080 gfc_error ("FORMAT statement at %L does not have a statement label",
1087 case ST_END_PROGRAM:
1088 case ST_END_FUNCTION:
1089 case ST_END_SUBROUTINE:
1093 case ST_END_CRITICAL:
1096 type = ST_LABEL_TARGET;
1100 type = ST_LABEL_FORMAT;
1103 /* Statement labels are not restricted from appearing on a
1104 particular line. However, there are plenty of situations
1105 where the resulting label can't be referenced. */
1108 type = ST_LABEL_BAD_TARGET;
1112 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1114 new_st.here = gfc_statement_label;
1118 /* Figures out what the enclosing program unit is. This will be a
1119 function, subroutine, program, block data or module. */
1122 gfc_enclosing_unit (gfc_compile_state * result)
1126 for (p = gfc_state_stack; p; p = p->previous)
1127 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1128 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
1129 || p->state == COMP_PROGRAM)
1138 *result = COMP_PROGRAM;
1143 /* Translate a statement enum to a string. */
1146 gfc_ascii_statement (gfc_statement st)
1152 case ST_ARITHMETIC_IF:
1153 p = _("arithmetic IF");
1159 p = _("attribute declaration");
1195 p = _("data declaration");
1203 case ST_DERIVED_DECL:
1204 p = _("derived type declaration");
1221 case ST_END_BLOCK_DATA:
1222 p = "END BLOCK DATA";
1224 case ST_END_CRITICAL:
1236 case ST_END_FUNCTION:
1242 case ST_END_INTERFACE:
1243 p = "END INTERFACE";
1248 case ST_END_PROGRAM:
1254 case ST_END_SUBROUTINE:
1255 p = "END SUBROUTINE";
1266 case ST_EQUIVALENCE:
1278 case ST_FORALL_BLOCK: /* Fall through */
1300 case ST_IMPLICIT_NONE:
1301 p = "IMPLICIT NONE";
1303 case ST_IMPLIED_ENDDO:
1304 p = _("implied END DO");
1330 case ST_MODULE_PROC:
1331 p = "MODULE PROCEDURE";
1363 case ST_SYNC_IMAGES:
1366 case ST_SYNC_MEMORY:
1378 case ST_WHERE_BLOCK: /* Fall through */
1389 p = _("assignment");
1391 case ST_POINTER_ASSIGNMENT:
1392 p = _("pointer assignment");
1394 case ST_SELECT_CASE:
1397 case ST_SELECT_TYPE:
1412 case ST_STATEMENT_FUNCTION:
1413 p = "STATEMENT FUNCTION";
1415 case ST_LABEL_ASSIGNMENT:
1416 p = "LABEL ASSIGNMENT";
1419 p = "ENUM DEFINITION";
1422 p = "ENUMERATOR DEFINITION";
1430 case ST_OMP_BARRIER:
1431 p = "!$OMP BARRIER";
1433 case ST_OMP_CRITICAL:
1434 p = "!$OMP CRITICAL";
1439 case ST_OMP_END_CRITICAL:
1440 p = "!$OMP END CRITICAL";
1445 case ST_OMP_END_MASTER:
1446 p = "!$OMP END MASTER";
1448 case ST_OMP_END_ORDERED:
1449 p = "!$OMP END ORDERED";
1451 case ST_OMP_END_PARALLEL:
1452 p = "!$OMP END PARALLEL";
1454 case ST_OMP_END_PARALLEL_DO:
1455 p = "!$OMP END PARALLEL DO";
1457 case ST_OMP_END_PARALLEL_SECTIONS:
1458 p = "!$OMP END PARALLEL SECTIONS";
1460 case ST_OMP_END_PARALLEL_WORKSHARE:
1461 p = "!$OMP END PARALLEL WORKSHARE";
1463 case ST_OMP_END_SECTIONS:
1464 p = "!$OMP END SECTIONS";
1466 case ST_OMP_END_SINGLE:
1467 p = "!$OMP END SINGLE";
1469 case ST_OMP_END_TASK:
1470 p = "!$OMP END TASK";
1472 case ST_OMP_END_WORKSHARE:
1473 p = "!$OMP END WORKSHARE";
1481 case ST_OMP_ORDERED:
1482 p = "!$OMP ORDERED";
1484 case ST_OMP_PARALLEL:
1485 p = "!$OMP PARALLEL";
1487 case ST_OMP_PARALLEL_DO:
1488 p = "!$OMP PARALLEL DO";
1490 case ST_OMP_PARALLEL_SECTIONS:
1491 p = "!$OMP PARALLEL SECTIONS";
1493 case ST_OMP_PARALLEL_WORKSHARE:
1494 p = "!$OMP PARALLEL WORKSHARE";
1496 case ST_OMP_SECTIONS:
1497 p = "!$OMP SECTIONS";
1499 case ST_OMP_SECTION:
1500 p = "!$OMP SECTION";
1508 case ST_OMP_TASKWAIT:
1509 p = "!$OMP TASKWAIT";
1511 case ST_OMP_THREADPRIVATE:
1512 p = "!$OMP THREADPRIVATE";
1514 case ST_OMP_WORKSHARE:
1515 p = "!$OMP WORKSHARE";
1518 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1525 /* Create a symbol for the main program and assign it to ns->proc_name. */
1528 main_program_symbol (gfc_namespace *ns, const char *name)
1530 gfc_symbol *main_program;
1531 symbol_attribute attr;
1533 gfc_get_symbol (name, ns, &main_program);
1534 gfc_clear_attr (&attr);
1535 attr.flavor = FL_PROGRAM;
1536 attr.proc = PROC_UNKNOWN;
1537 attr.subroutine = 1;
1538 attr.access = ACCESS_PUBLIC;
1539 attr.is_main_program = 1;
1540 main_program->attr = attr;
1541 main_program->declared_at = gfc_current_locus;
1542 ns->proc_name = main_program;
1543 gfc_commit_symbols ();
1547 /* Do whatever is necessary to accept the last statement. */
1550 accept_statement (gfc_statement st)
1558 case ST_IMPLICIT_NONE:
1559 gfc_set_implicit_none ();
1568 gfc_current_ns->proc_name = gfc_new_block;
1571 /* If the statement is the end of a block, lay down a special code
1572 that allows a branch to the end of the block from within the
1573 construct. IF and SELECT are treated differently from DO
1574 (where EXEC_NOP is added inside the loop) for two
1576 1. END DO has a meaning in the sense that after a GOTO to
1577 it, the loop counter must be increased.
1578 2. IF blocks and SELECT blocks can consist of multiple
1579 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
1580 Putting the label before the END IF would make the jump
1581 from, say, the ELSE IF block to the END IF illegal. */
1585 case ST_END_CRITICAL:
1586 if (gfc_statement_label != NULL)
1588 new_st.op = EXEC_END_BLOCK;
1593 /* The end-of-program unit statements do not get the special
1594 marker and require a statement of some sort if they are a
1597 case ST_END_PROGRAM:
1598 case ST_END_FUNCTION:
1599 case ST_END_SUBROUTINE:
1600 if (gfc_statement_label != NULL)
1602 new_st.op = EXEC_RETURN;
1607 new_st.op = EXEC_END_PROCEDURE;
1623 gfc_commit_symbols ();
1624 gfc_warning_check ();
1625 gfc_clear_new_st ();
1629 /* Undo anything tentative that has been built for the current
1633 reject_statement (void)
1635 /* Revert to the previous charlen chain. */
1636 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
1637 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
1639 gfc_new_block = NULL;
1640 gfc_undo_symbols ();
1641 gfc_clear_warning ();
1642 undo_new_statement ();
1646 /* Generic complaint about an out of order statement. We also do
1647 whatever is necessary to clean up. */
1650 unexpected_statement (gfc_statement st)
1652 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1654 reject_statement ();
1658 /* Given the next statement seen by the matcher, make sure that it is
1659 in proper order with the last. This subroutine is initialized by
1660 calling it with an argument of ST_NONE. If there is a problem, we
1661 issue an error and return FAILURE. Otherwise we return SUCCESS.
1663 Individual parsers need to verify that the statements seen are
1664 valid before calling here, i.e., ENTRY statements are not allowed in
1665 INTERFACE blocks. The following diagram is taken from the standard:
1667 +---------------------------------------+
1668 | program subroutine function module |
1669 +---------------------------------------+
1671 +---------------------------------------+
1673 +---------------------------------------+
1675 | +-----------+------------------+
1676 | | parameter | implicit |
1677 | +-----------+------------------+
1678 | format | | derived type |
1679 | entry | parameter | interface |
1680 | | data | specification |
1681 | | | statement func |
1682 | +-----------+------------------+
1683 | | data | executable |
1684 +--------+-----------+------------------+
1686 +---------------------------------------+
1687 | internal module/subprogram |
1688 +---------------------------------------+
1690 +---------------------------------------+
1699 ORDER_IMPLICIT_NONE,
1707 enum state_order state;
1708 gfc_statement last_statement;
1714 verify_st_order (st_state *p, gfc_statement st, bool silent)
1720 p->state = ORDER_START;
1724 if (p->state > ORDER_USE)
1726 p->state = ORDER_USE;
1730 if (p->state > ORDER_IMPORT)
1732 p->state = ORDER_IMPORT;
1735 case ST_IMPLICIT_NONE:
1736 if (p->state > ORDER_IMPLICIT_NONE)
1739 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1740 statement disqualifies a USE but not an IMPLICIT NONE.
1741 Duplicate IMPLICIT NONEs are caught when the implicit types
1744 p->state = ORDER_IMPLICIT_NONE;
1748 if (p->state > ORDER_IMPLICIT)
1750 p->state = ORDER_IMPLICIT;
1755 if (p->state < ORDER_IMPLICIT_NONE)
1756 p->state = ORDER_IMPLICIT_NONE;
1760 if (p->state >= ORDER_EXEC)
1762 if (p->state < ORDER_IMPLICIT)
1763 p->state = ORDER_IMPLICIT;
1767 if (p->state < ORDER_SPEC)
1768 p->state = ORDER_SPEC;
1773 case ST_DERIVED_DECL:
1775 if (p->state >= ORDER_EXEC)
1777 if (p->state < ORDER_SPEC)
1778 p->state = ORDER_SPEC;
1783 if (p->state < ORDER_EXEC)
1784 p->state = ORDER_EXEC;
1788 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1789 gfc_ascii_statement (st));
1792 /* All is well, record the statement in case we need it next time. */
1793 p->where = gfc_current_locus;
1794 p->last_statement = st;
1799 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1800 gfc_ascii_statement (st),
1801 gfc_ascii_statement (p->last_statement), &p->where);
1807 /* Handle an unexpected end of file. This is a show-stopper... */
1809 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1812 unexpected_eof (void)
1816 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1818 /* Memory cleanup. Move to "second to last". */
1819 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1822 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1825 longjmp (eof_buf, 1);
1829 /* Parse the CONTAINS section of a derived type definition. */
1831 gfc_access gfc_typebound_default_access;
1834 parse_derived_contains (void)
1837 bool seen_private = false;
1838 bool seen_comps = false;
1839 bool error_flag = false;
1842 gcc_assert (gfc_current_state () == COMP_DERIVED);
1843 gcc_assert (gfc_current_block ());
1845 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
1847 if (gfc_current_block ()->attr.sequence)
1848 gfc_error ("Derived-type '%s' with SEQUENCE must not have a CONTAINS"
1849 " section at %C", gfc_current_block ()->name);
1850 if (gfc_current_block ()->attr.is_bind_c)
1851 gfc_error ("Derived-type '%s' with BIND(C) must not have a CONTAINS"
1852 " section at %C", gfc_current_block ()->name);
1854 accept_statement (ST_CONTAINS);
1855 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
1857 gfc_typebound_default_access = ACCESS_PUBLIC;
1863 st = next_statement ();
1871 gfc_error ("Components in TYPE at %C must precede CONTAINS");
1876 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Type-bound"
1877 " procedure at %C") == FAILURE)
1880 accept_statement (ST_PROCEDURE);
1885 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: GENERIC binding"
1886 " at %C") == FAILURE)
1889 accept_statement (ST_GENERIC);
1894 if (gfc_notify_std (GFC_STD_F2003,
1895 "Fortran 2003: FINAL procedure declaration"
1896 " at %C") == FAILURE)
1899 accept_statement (ST_FINAL);
1907 && (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Derived type "
1908 "definition at %C with empty CONTAINS "
1909 "section") == FAILURE))
1912 /* ST_END_TYPE is accepted by parse_derived after return. */
1916 if (gfc_find_state (COMP_MODULE) == FAILURE)
1918 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1926 gfc_error ("PRIVATE statement at %C must precede procedure"
1934 gfc_error ("Duplicate PRIVATE statement at %C");
1938 accept_statement (ST_PRIVATE);
1939 gfc_typebound_default_access = ACCESS_PRIVATE;
1940 seen_private = true;
1944 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
1949 gfc_error ("Already inside a CONTAINS block at %C");
1954 unexpected_statement (st);
1960 gcc_assert (gfc_current_state () == COMP_DERIVED);
1966 /* Parse a derived type. */
1969 parse_derived (void)
1971 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1979 accept_statement (ST_DERIVED_DECL);
1980 push_state (&s, COMP_DERIVED, gfc_new_block);
1982 gfc_new_block->component_access = ACCESS_PUBLIC;
1989 while (compiling_type)
1991 st = next_statement ();
1999 accept_statement (st);
2004 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2013 && (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Derived type "
2014 "definition at %C without components")
2018 accept_statement (ST_END_TYPE);
2022 if (gfc_find_state (COMP_MODULE) == FAILURE)
2024 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2032 gfc_error ("PRIVATE statement at %C must precede "
2033 "structure components");
2040 gfc_error ("Duplicate PRIVATE statement at %C");
2044 s.sym->component_access = ACCESS_PRIVATE;
2046 accept_statement (ST_PRIVATE);
2053 gfc_error ("SEQUENCE statement at %C must precede "
2054 "structure components");
2059 if (gfc_current_block ()->attr.sequence)
2060 gfc_warning ("SEQUENCE attribute at %C already specified in "
2065 gfc_error ("Duplicate SEQUENCE statement at %C");
2070 gfc_add_sequence (&gfc_current_block ()->attr,
2071 gfc_current_block ()->name, NULL);
2075 if (gfc_notify_std (GFC_STD_F2003,
2076 "Fortran 2003: CONTAINS block in derived type"
2077 " definition at %C") == FAILURE)
2080 accept_statement (ST_CONTAINS);
2081 if (parse_derived_contains ())
2086 unexpected_statement (st);
2091 /* need to verify that all fields of the derived type are
2092 * interoperable with C if the type is declared to be bind(c)
2094 sym = gfc_current_block ();
2095 for (c = sym->components; c; c = c->next)
2097 /* Look for allocatable components. */
2098 if (c->attr.allocatable
2099 || (c->ts.type == BT_CLASS
2100 && c->ts.u.derived->components->attr.allocatable)
2101 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.alloc_comp))
2102 sym->attr.alloc_comp = 1;
2104 /* Look for pointer components. */
2106 || (c->ts.type == BT_CLASS
2107 && c->ts.u.derived->components->attr.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 || c->attr.coarray_comp)
2119 sym->attr.coarray_comp = 1;
2121 /* Look for private components. */
2122 if (sym->component_access == ACCESS_PRIVATE
2123 || c->attr.access == ACCESS_PRIVATE
2124 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
2125 sym->attr.private_comp = 1;
2128 if (!seen_component)
2129 sym->attr.zero_comp = 1;
2135 /* Parse an ENUM. */
2144 int seen_enumerator = 0;
2148 push_state (&s, COMP_ENUM, gfc_new_block);
2152 while (compiling_enum)
2154 st = next_statement ();
2162 seen_enumerator = 1;
2163 accept_statement (st);
2168 if (!seen_enumerator)
2170 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2173 accept_statement (st);
2177 gfc_free_enum_history ();
2178 unexpected_statement (st);
2186 /* Parse an interface. We must be able to deal with the possibility
2187 of recursive interfaces. The parse_spec() subroutine is mutually
2188 recursive with parse_interface(). */
2190 static gfc_statement parse_spec (gfc_statement);
2193 parse_interface (void)
2195 gfc_compile_state new_state = COMP_NONE, current_state;
2196 gfc_symbol *prog_unit, *sym;
2197 gfc_interface_info save;
2198 gfc_state_data s1, s2;
2202 accept_statement (ST_INTERFACE);
2204 current_interface.ns = gfc_current_ns;
2205 save = current_interface;
2207 sym = (current_interface.type == INTERFACE_GENERIC
2208 || current_interface.type == INTERFACE_USER_OP)
2209 ? gfc_new_block : NULL;
2211 push_state (&s1, COMP_INTERFACE, sym);
2212 current_state = COMP_NONE;
2215 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
2217 st = next_statement ();
2225 if (st == ST_SUBROUTINE)
2226 new_state = COMP_SUBROUTINE;
2227 else if (st == ST_FUNCTION)
2228 new_state = COMP_FUNCTION;
2229 if (gfc_new_block->attr.pointer)
2231 gfc_new_block->attr.pointer = 0;
2232 gfc_new_block->attr.proc_pointer = 1;
2234 if (gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
2235 gfc_new_block->formal, NULL) == FAILURE)
2237 reject_statement ();
2238 gfc_free_namespace (gfc_current_ns);
2244 case ST_MODULE_PROC: /* The module procedure matcher makes
2245 sure the context is correct. */
2246 accept_statement (st);
2247 gfc_free_namespace (gfc_current_ns);
2250 case ST_END_INTERFACE:
2251 gfc_free_namespace (gfc_current_ns);
2252 gfc_current_ns = current_interface.ns;
2256 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2257 gfc_ascii_statement (st));
2258 reject_statement ();
2259 gfc_free_namespace (gfc_current_ns);
2264 /* Make sure that a generic interface has only subroutines or
2265 functions and that the generic name has the right attribute. */
2266 if (current_interface.type == INTERFACE_GENERIC)
2268 if (current_state == COMP_NONE)
2270 if (new_state == COMP_FUNCTION)
2271 gfc_add_function (&sym->attr, sym->name, NULL);
2272 else if (new_state == COMP_SUBROUTINE)
2273 gfc_add_subroutine (&sym->attr, sym->name, NULL);
2275 current_state = new_state;
2279 if (new_state != current_state)
2281 if (new_state == COMP_SUBROUTINE)
2282 gfc_error ("SUBROUTINE at %C does not belong in a "
2283 "generic function interface");
2285 if (new_state == COMP_FUNCTION)
2286 gfc_error ("FUNCTION at %C does not belong in a "
2287 "generic subroutine interface");
2292 if (current_interface.type == INTERFACE_ABSTRACT)
2294 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
2295 if (gfc_is_intrinsic_typename (gfc_new_block->name))
2296 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
2297 "cannot be the same as an intrinsic type",
2298 gfc_new_block->name);
2301 push_state (&s2, new_state, gfc_new_block);
2302 accept_statement (st);
2303 prog_unit = gfc_new_block;
2304 prog_unit->formal_ns = gfc_current_ns;
2305 proc_locus = gfc_current_locus;
2308 /* Read data declaration statements. */
2309 st = parse_spec (ST_NONE);
2311 /* Since the interface block does not permit an IMPLICIT statement,
2312 the default type for the function or the result must be taken
2313 from the formal namespace. */
2314 if (new_state == COMP_FUNCTION)
2316 if (prog_unit->result == prog_unit
2317 && prog_unit->ts.type == BT_UNKNOWN)
2318 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
2319 else if (prog_unit->result != prog_unit
2320 && prog_unit->result->ts.type == BT_UNKNOWN)
2321 gfc_set_default_type (prog_unit->result, 1,
2322 prog_unit->formal_ns);
2325 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
2327 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2328 gfc_ascii_statement (st));
2329 reject_statement ();
2333 /* Add EXTERNAL attribute to function or subroutine. */
2334 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
2335 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
2337 current_interface = save;
2338 gfc_add_interface (prog_unit);
2341 if (current_interface.ns
2342 && current_interface.ns->proc_name
2343 && strcmp (current_interface.ns->proc_name->name,
2344 prog_unit->name) == 0)
2345 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2346 "enclosing procedure", prog_unit->name, &proc_locus);
2355 /* Associate function characteristics by going back to the function
2356 declaration and rematching the prefix. */
2359 match_deferred_characteristics (gfc_typespec * ts)
2362 match m = MATCH_ERROR;
2363 char name[GFC_MAX_SYMBOL_LEN + 1];
2365 loc = gfc_current_locus;
2367 gfc_current_locus = gfc_current_block ()->declared_at;
2370 gfc_buffer_error (1);
2371 m = gfc_match_prefix (ts);
2372 gfc_buffer_error (0);
2374 if (ts->type == BT_DERIVED)
2382 /* Only permit one go at the characteristic association. */
2386 /* Set the function locus correctly. If we have not found the
2387 function name, there is an error. */
2389 && gfc_match ("function% %n", name) == MATCH_YES
2390 && strcmp (name, gfc_current_block ()->name) == 0)
2392 gfc_current_block ()->declared_at = gfc_current_locus;
2393 gfc_commit_symbols ();
2398 gfc_current_locus =loc;
2403 /* Check specification-expressions in the function result of the currently
2404 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2405 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2406 scope are not yet parsed so this has to be delayed up to parse_spec. */
2409 check_function_result_typed (void)
2411 gfc_typespec* ts = &gfc_current_ns->proc_name->result->ts;
2413 gcc_assert (gfc_current_state () == COMP_FUNCTION);
2414 gcc_assert (ts->type != BT_UNKNOWN);
2416 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2417 /* TODO: Extend when KIND type parameters are implemented. */
2418 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length)
2419 gfc_expr_check_typed (ts->u.cl->length, gfc_current_ns, true);
2423 /* Parse a set of specification statements. Returns the statement
2424 that doesn't fit. */
2426 static gfc_statement
2427 parse_spec (gfc_statement st)
2430 bool function_result_typed = false;
2431 bool bad_characteristic = false;
2434 verify_st_order (&ss, ST_NONE, false);
2436 st = next_statement ();
2438 /* If we are not inside a function or don't have a result specified so far,
2439 do nothing special about it. */
2440 if (gfc_current_state () != COMP_FUNCTION)
2441 function_result_typed = true;
2444 gfc_symbol* proc = gfc_current_ns->proc_name;
2447 if (proc->result->ts.type == BT_UNKNOWN)
2448 function_result_typed = true;
2453 /* If we're inside a BLOCK construct, some statements are disallowed.
2454 Check this here. Attribute declaration statements like INTENT, OPTIONAL
2455 or VALUE are also disallowed, but they don't have a particular ST_*
2456 key so we have to check for them individually in their matcher routine. */
2457 if (gfc_current_state () == COMP_BLOCK)
2461 case ST_IMPLICIT_NONE:
2464 case ST_EQUIVALENCE:
2465 case ST_STATEMENT_FUNCTION:
2466 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
2467 gfc_ascii_statement (st));
2474 /* If we find a statement that can not be followed by an IMPLICIT statement
2475 (and thus we can expect to see none any further), type the function result
2476 if it has not yet been typed. Be careful not to give the END statement
2477 to verify_st_order! */
2478 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
2480 bool verify_now = false;
2482 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
2487 verify_st_order (&dummyss, ST_NONE, false);
2488 verify_st_order (&dummyss, st, false);
2490 if (verify_st_order (&dummyss, ST_IMPLICIT, true) == FAILURE)
2496 check_function_result_typed ();
2497 function_result_typed = true;
2506 case ST_IMPLICIT_NONE:
2508 if (!function_result_typed)
2510 check_function_result_typed ();
2511 function_result_typed = true;
2517 case ST_DATA: /* Not allowed in interfaces */
2518 if (gfc_current_state () == COMP_INTERFACE)
2528 case ST_DERIVED_DECL:
2531 if (verify_st_order (&ss, st, false) == FAILURE)
2533 reject_statement ();
2534 st = next_statement ();
2544 case ST_DERIVED_DECL:
2550 if (gfc_current_state () != COMP_MODULE)
2552 gfc_error ("%s statement must appear in a MODULE",
2553 gfc_ascii_statement (st));
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));
2564 gfc_current_ns->default_access = (st == ST_PUBLIC)
2565 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2569 case ST_STATEMENT_FUNCTION:
2570 if (gfc_current_state () == COMP_MODULE)
2572 unexpected_statement (st);
2580 accept_statement (st);
2581 st = next_statement ();
2585 accept_statement (st);
2587 st = next_statement ();
2590 case ST_GET_FCN_CHARACTERISTICS:
2591 /* This statement triggers the association of a function's result
2593 ts = &gfc_current_block ()->result->ts;
2594 if (match_deferred_characteristics (ts) != MATCH_YES)
2595 bad_characteristic = true;
2597 st = next_statement ();
2604 /* If match_deferred_characteristics failed, then there is an error. */
2605 if (bad_characteristic)
2607 ts = &gfc_current_block ()->result->ts;
2608 if (ts->type != BT_DERIVED)
2609 gfc_error ("Bad kind expression for function '%s' at %L",
2610 gfc_current_block ()->name,
2611 &gfc_current_block ()->declared_at);
2613 gfc_error ("The type for function '%s' at %L is not accessible",
2614 gfc_current_block ()->name,
2615 &gfc_current_block ()->declared_at);
2617 gfc_current_block ()->ts.kind = 0;
2618 /* Keep the derived type; if it's bad, it will be discovered later. */
2619 if (!(ts->type == BT_DERIVED && ts->u.derived))
2620 ts->type = BT_UNKNOWN;
2627 /* Parse a WHERE block, (not a simple WHERE statement). */
2630 parse_where_block (void)
2632 int seen_empty_else;
2637 accept_statement (ST_WHERE_BLOCK);
2638 top = gfc_state_stack->tail;
2640 push_state (&s, COMP_WHERE, gfc_new_block);
2642 d = add_statement ();
2643 d->expr1 = top->expr1;
2649 seen_empty_else = 0;
2653 st = next_statement ();
2659 case ST_WHERE_BLOCK:
2660 parse_where_block ();
2665 accept_statement (st);
2669 if (seen_empty_else)
2671 gfc_error ("ELSEWHERE statement at %C follows previous "
2672 "unmasked ELSEWHERE");
2676 if (new_st.expr1 == NULL)
2677 seen_empty_else = 1;
2679 d = new_level (gfc_state_stack->head);
2681 d->expr1 = new_st.expr1;
2683 accept_statement (st);
2688 accept_statement (st);
2692 gfc_error ("Unexpected %s statement in WHERE block at %C",
2693 gfc_ascii_statement (st));
2694 reject_statement ();
2698 while (st != ST_END_WHERE);
2704 /* Parse a FORALL block (not a simple FORALL statement). */
2707 parse_forall_block (void)
2713 accept_statement (ST_FORALL_BLOCK);
2714 top = gfc_state_stack->tail;
2716 push_state (&s, COMP_FORALL, gfc_new_block);
2718 d = add_statement ();
2719 d->op = EXEC_FORALL;
2724 st = next_statement ();
2729 case ST_POINTER_ASSIGNMENT:
2732 accept_statement (st);
2735 case ST_WHERE_BLOCK:
2736 parse_where_block ();
2739 case ST_FORALL_BLOCK:
2740 parse_forall_block ();
2744 accept_statement (st);
2751 gfc_error ("Unexpected %s statement in FORALL block at %C",
2752 gfc_ascii_statement (st));
2754 reject_statement ();
2758 while (st != ST_END_FORALL);
2764 static gfc_statement parse_executable (gfc_statement);
2766 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2769 parse_if_block (void)
2778 accept_statement (ST_IF_BLOCK);
2780 top = gfc_state_stack->tail;
2781 push_state (&s, COMP_IF, gfc_new_block);
2783 new_st.op = EXEC_IF;
2784 d = add_statement ();
2786 d->expr1 = top->expr1;
2792 st = parse_executable (ST_NONE);
2802 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2803 "statement at %L", &else_locus);
2805 reject_statement ();
2809 d = new_level (gfc_state_stack->head);
2811 d->expr1 = new_st.expr1;
2813 accept_statement (st);
2820 gfc_error ("Duplicate ELSE statements at %L and %C",
2822 reject_statement ();
2827 else_locus = gfc_current_locus;
2829 d = new_level (gfc_state_stack->head);
2832 accept_statement (st);
2840 unexpected_statement (st);
2844 while (st != ST_ENDIF);
2847 accept_statement (st);
2851 /* Parse a SELECT block. */
2854 parse_select_block (void)
2860 accept_statement (ST_SELECT_CASE);
2862 cp = gfc_state_stack->tail;
2863 push_state (&s, COMP_SELECT, gfc_new_block);
2865 /* Make sure that the next statement is a CASE or END SELECT. */
2868 st = next_statement ();
2871 if (st == ST_END_SELECT)
2873 /* Empty SELECT CASE is OK. */
2874 accept_statement (st);
2881 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2884 reject_statement ();
2887 /* At this point, we're got a nonempty select block. */
2888 cp = new_level (cp);
2891 accept_statement (st);
2895 st = parse_executable (ST_NONE);
2902 cp = new_level (gfc_state_stack->head);
2904 gfc_clear_new_st ();
2906 accept_statement (st);
2912 /* Can't have an executable statement because of
2913 parse_executable(). */
2915 unexpected_statement (st);
2919 while (st != ST_END_SELECT);
2922 accept_statement (st);
2926 /* Pop the current selector from the SELECT TYPE stack. */
2929 select_type_pop (void)
2931 gfc_select_type_stack *old = select_type_stack;
2932 select_type_stack = old->prev;
2937 /* Parse a SELECT TYPE construct (F03:R821). */
2940 parse_select_type_block (void)
2946 accept_statement (ST_SELECT_TYPE);
2948 cp = gfc_state_stack->tail;
2949 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
2951 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
2955 st = next_statement ();
2958 if (st == ST_END_SELECT)
2959 /* Empty SELECT CASE is OK. */
2961 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
2964 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
2965 "following SELECT TYPE at %C");
2967 reject_statement ();
2970 /* At this point, we're got a nonempty select block. */
2971 cp = new_level (cp);
2974 accept_statement (st);
2978 st = parse_executable (ST_NONE);
2986 cp = new_level (gfc_state_stack->head);
2988 gfc_clear_new_st ();
2990 accept_statement (st);
2996 /* Can't have an executable statement because of
2997 parse_executable(). */
2999 unexpected_statement (st);
3003 while (st != ST_END_SELECT);
3007 accept_statement (st);
3008 gfc_current_ns = gfc_current_ns->parent;
3013 /* Given a symbol, make sure it is not an iteration variable for a DO
3014 statement. This subroutine is called when the symbol is seen in a
3015 context that causes it to become redefined. If the symbol is an
3016 iterator, we generate an error message and return nonzero. */
3019 gfc_check_do_variable (gfc_symtree *st)
3023 for (s=gfc_state_stack; s; s = s->previous)
3024 if (s->do_variable == st)
3026 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
3027 "loop beginning at %L", st->name, &s->head->loc);
3035 /* Checks to see if the current statement label closes an enddo.
3036 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3037 an error) if it incorrectly closes an ENDDO. */
3040 check_do_closure (void)
3044 if (gfc_statement_label == NULL)
3047 for (p = gfc_state_stack; p; p = p->previous)
3048 if (p->state == COMP_DO)
3052 return 0; /* No loops to close */
3054 if (p->ext.end_do_label == gfc_statement_label)
3056 if (p == gfc_state_stack)
3059 gfc_error ("End of nonblock DO statement at %C is within another block");
3063 /* At this point, the label doesn't terminate the innermost loop.
3064 Make sure it doesn't terminate another one. */
3065 for (; p; p = p->previous)
3066 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
3068 gfc_error ("End of nonblock DO statement at %C is interwoven "
3069 "with another DO loop");
3077 /* Parse a series of contained program units. */
3079 static void parse_progunit (gfc_statement);
3082 /* Parse a CRITICAL block. */
3085 parse_critical_block (void)
3091 s.ext.end_do_label = new_st.label1;
3093 accept_statement (ST_CRITICAL);
3094 top = gfc_state_stack->tail;
3096 push_state (&s, COMP_CRITICAL, gfc_new_block);
3098 d = add_statement ();
3099 d->op = EXEC_CRITICAL;
3104 st = parse_executable (ST_NONE);
3112 case ST_END_CRITICAL:
3113 if (s.ext.end_do_label != NULL
3114 && s.ext.end_do_label != gfc_statement_label)
3115 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3116 "match CRITIAL label");
3118 if (gfc_statement_label != NULL)
3120 new_st.op = EXEC_NOP;
3126 unexpected_statement (st);
3130 while (st != ST_END_CRITICAL);
3133 accept_statement (st);
3137 /* Set up the local namespace for a BLOCK construct. */
3140 gfc_build_block_ns (gfc_namespace *parent_ns)
3142 gfc_namespace* my_ns;
3144 my_ns = gfc_get_namespace (parent_ns, 1);
3145 my_ns->construct_entities = 1;
3147 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3148 code generation (so it must not be NULL).
3149 We set its recursive argument if our container procedure is recursive, so
3150 that local variables are accordingly placed on the stack when it
3151 will be necessary. */
3153 my_ns->proc_name = gfc_new_block;
3158 gfc_get_symbol ("block@", my_ns, &my_ns->proc_name);
3159 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
3160 my_ns->proc_name->name, NULL);
3161 gcc_assert (t == SUCCESS);
3164 if (parent_ns->proc_name)
3165 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
3171 /* Parse a BLOCK construct. */
3174 parse_block_construct (void)
3176 gfc_namespace* my_ns;
3179 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BLOCK construct at %C");
3181 my_ns = gfc_build_block_ns (gfc_current_ns);
3183 new_st.op = EXEC_BLOCK;
3184 new_st.ext.ns = my_ns;
3185 accept_statement (ST_BLOCK);
3187 push_state (&s, COMP_BLOCK, my_ns->proc_name);
3188 gfc_current_ns = my_ns;
3190 parse_progunit (ST_NONE);
3192 gfc_current_ns = gfc_current_ns->parent;
3197 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
3198 handled inside of parse_executable(), because they aren't really
3202 parse_do_block (void)
3209 s.ext.end_do_label = new_st.label1;
3211 if (new_st.ext.iterator != NULL)
3212 stree = new_st.ext.iterator->var->symtree;
3216 accept_statement (ST_DO);
3218 top = gfc_state_stack->tail;
3219 push_state (&s, COMP_DO, gfc_new_block);
3221 s.do_variable = stree;
3223 top->block = new_level (top);
3224 top->block->op = EXEC_DO;
3227 st = parse_executable (ST_NONE);
3235 if (s.ext.end_do_label != NULL
3236 && s.ext.end_do_label != gfc_statement_label)
3237 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
3240 if (gfc_statement_label != NULL)
3242 new_st.op = EXEC_NOP;
3247 case ST_IMPLIED_ENDDO:
3248 /* If the do-stmt of this DO construct has a do-construct-name,
3249 the corresponding end-do must be an end-do-stmt (with a matching
3250 name, but in that case we must have seen ST_ENDDO first).
3251 We only complain about this in pedantic mode. */
3252 if (gfc_current_block () != NULL)
3253 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
3254 &gfc_current_block()->declared_at);
3259 unexpected_statement (st);
3264 accept_statement (st);
3268 /* Parse the statements of OpenMP do/parallel do. */
3270 static gfc_statement
3271 parse_omp_do (gfc_statement omp_st)
3277 accept_statement (omp_st);
3279 cp = gfc_state_stack->tail;
3280 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3281 np = new_level (cp);
3287 st = next_statement ();
3290 else if (st == ST_DO)
3293 unexpected_statement (st);
3297 if (gfc_statement_label != NULL
3298 && gfc_state_stack->previous != NULL
3299 && gfc_state_stack->previous->state == COMP_DO
3300 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
3308 there should be no !$OMP END DO. */
3310 return ST_IMPLIED_ENDDO;
3313 check_do_closure ();
3316 st = next_statement ();
3317 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
3319 if (new_st.op == EXEC_OMP_END_NOWAIT)
3320 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
3322 gcc_assert (new_st.op == EXEC_NOP);
3323 gfc_clear_new_st ();
3324 gfc_commit_symbols ();
3325 gfc_warning_check ();
3326 st = next_statement ();
3332 /* Parse the statements of OpenMP atomic directive. */
3335 parse_omp_atomic (void)
3341 accept_statement (ST_OMP_ATOMIC);
3343 cp = gfc_state_stack->tail;
3344 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3345 np = new_level (cp);
3351 st = next_statement ();
3354 else if (st == ST_ASSIGNMENT)
3357 unexpected_statement (st);
3360 accept_statement (st);
3366 /* Parse the statements of an OpenMP structured block. */
3369 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
3371 gfc_statement st, omp_end_st;
3375 accept_statement (omp_st);
3377 cp = gfc_state_stack->tail;
3378 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3379 np = new_level (cp);
3385 case ST_OMP_PARALLEL:
3386 omp_end_st = ST_OMP_END_PARALLEL;
3388 case ST_OMP_PARALLEL_SECTIONS:
3389 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
3391 case ST_OMP_SECTIONS:
3392 omp_end_st = ST_OMP_END_SECTIONS;
3394 case ST_OMP_ORDERED:
3395 omp_end_st = ST_OMP_END_ORDERED;
3397 case ST_OMP_CRITICAL:
3398 omp_end_st = ST_OMP_END_CRITICAL;
3401 omp_end_st = ST_OMP_END_MASTER;
3404 omp_end_st = ST_OMP_END_SINGLE;
3407 omp_end_st = ST_OMP_END_TASK;
3409 case ST_OMP_WORKSHARE:
3410 omp_end_st = ST_OMP_END_WORKSHARE;
3412 case ST_OMP_PARALLEL_WORKSHARE:
3413 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
3421 if (workshare_stmts_only)
3423 /* Inside of !$omp workshare, only
3426 where statements and constructs
3427 forall statements and constructs
3431 are allowed. For !$omp critical these
3432 restrictions apply recursively. */
3435 st = next_statement ();
3446 accept_statement (st);
3449 case ST_WHERE_BLOCK:
3450 parse_where_block ();
3453 case ST_FORALL_BLOCK:
3454 parse_forall_block ();
3457 case ST_OMP_PARALLEL:
3458 case ST_OMP_PARALLEL_SECTIONS:
3459 parse_omp_structured_block (st, false);
3462 case ST_OMP_PARALLEL_WORKSHARE:
3463 case ST_OMP_CRITICAL:
3464 parse_omp_structured_block (st, true);
3467 case ST_OMP_PARALLEL_DO:
3468 st = parse_omp_do (st);
3472 parse_omp_atomic ();
3483 st = next_statement ();
3487 st = parse_executable (ST_NONE);
3490 else if (st == ST_OMP_SECTION
3491 && (omp_st == ST_OMP_SECTIONS
3492 || omp_st == ST_OMP_PARALLEL_SECTIONS))
3494 np = new_level (np);
3498 else if (st != omp_end_st)
3499 unexpected_statement (st);
3501 while (st != omp_end_st);
3505 case EXEC_OMP_END_NOWAIT:
3506 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
3508 case EXEC_OMP_CRITICAL:
3509 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
3510 || (new_st.ext.omp_name != NULL
3511 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
3512 gfc_error ("Name after !$omp critical and !$omp end critical does "
3514 gfc_free (CONST_CAST (char *, new_st.ext.omp_name));
3516 case EXEC_OMP_END_SINGLE:
3517 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
3518 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
3519 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
3520 gfc_free_omp_clauses (new_st.ext.omp_clauses);
3528 gfc_clear_new_st ();
3529 gfc_commit_symbols ();
3530 gfc_warning_check ();
3535 /* Accept a series of executable statements. We return the first
3536 statement that doesn't fit to the caller. Any block statements are
3537 passed on to the correct handler, which usually passes the buck
3540 static gfc_statement
3541 parse_executable (gfc_statement st)
3546 st = next_statement ();
3550 close_flag = check_do_closure ();
3555 case ST_END_PROGRAM:
3558 case ST_END_FUNCTION:
3563 case ST_END_SUBROUTINE:
3570 case ST_SELECT_CASE:
3571 gfc_error ("%s statement at %C cannot terminate a non-block "
3572 "DO loop", gfc_ascii_statement (st));
3588 accept_statement (st);
3589 if (close_flag == 1)
3590 return ST_IMPLIED_ENDDO;
3594 parse_block_construct ();
3601 case ST_SELECT_CASE:
3602 parse_select_block ();
3605 case ST_SELECT_TYPE:
3606 parse_select_type_block();
3611 if (check_do_closure () == 1)
3612 return ST_IMPLIED_ENDDO;
3616 parse_critical_block ();
3619 case ST_WHERE_BLOCK:
3620 parse_where_block ();
3623 case ST_FORALL_BLOCK:
3624 parse_forall_block ();
3627 case ST_OMP_PARALLEL:
3628 case ST_OMP_PARALLEL_SECTIONS:
3629 case ST_OMP_SECTIONS:
3630 case ST_OMP_ORDERED:
3631 case ST_OMP_CRITICAL:
3635 parse_omp_structured_block (st, false);
3638 case ST_OMP_WORKSHARE:
3639 case ST_OMP_PARALLEL_WORKSHARE:
3640 parse_omp_structured_block (st, true);
3644 case ST_OMP_PARALLEL_DO:
3645 st = parse_omp_do (st);
3646 if (st == ST_IMPLIED_ENDDO)
3651 parse_omp_atomic ();
3658 st = next_statement ();
3663 /* Fix the symbols for sibling functions. These are incorrectly added to
3664 the child namespace as the parser didn't know about this procedure. */
3667 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
3671 gfc_symbol *old_sym;
3673 sym->attr.referenced = 1;
3674 for (ns = siblings; ns; ns = ns->sibling)
3676 st = gfc_find_symtree (ns->sym_root, sym->name);
3678 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
3679 goto fixup_contained;
3681 old_sym = st->n.sym;
3682 if (old_sym->ns == ns
3683 && !old_sym->attr.contained
3685 /* By 14.6.1.3, host association should be excluded
3686 for the following. */
3687 && !(old_sym->attr.external
3688 || (old_sym->ts.type != BT_UNKNOWN
3689 && !old_sym->attr.implicit_type)
3690 || old_sym->attr.flavor == FL_PARAMETER
3691 || old_sym->attr.in_common
3692 || old_sym->attr.in_equivalence
3693 || old_sym->attr.data
3694 || old_sym->attr.dummy
3695 || old_sym->attr.result
3696 || old_sym->attr.dimension
3697 || old_sym->attr.allocatable
3698 || old_sym->attr.intrinsic
3699 || old_sym->attr.generic
3700 || old_sym->attr.flavor == FL_NAMELIST
3701 || old_sym->attr.proc == PROC_ST_FUNCTION))
3703 /* Replace it with the symbol from the parent namespace. */
3707 /* Free the old (local) symbol. */
3709 if (old_sym->refs == 0)
3710 gfc_free_symbol (old_sym);
3714 /* Do the same for any contained procedures. */
3715 gfc_fixup_sibling_symbols (sym, ns->contained);
3720 parse_contained (int module)
3722 gfc_namespace *ns, *parent_ns, *tmp;
3723 gfc_state_data s1, s2;
3727 int contains_statements = 0;
3730 push_state (&s1, COMP_CONTAINS, NULL);
3731 parent_ns = gfc_current_ns;
3735 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
3737 gfc_current_ns->sibling = parent_ns->contained;
3738 parent_ns->contained = gfc_current_ns;
3741 /* Process the next available statement. We come here if we got an error
3742 and rejected the last statement. */
3743 st = next_statement ();
3752 contains_statements = 1;
3753 accept_statement (st);
3756 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
3759 /* For internal procedures, create/update the symbol in the
3760 parent namespace. */
3764 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
3765 gfc_error ("Contained procedure '%s' at %C is already "
3766 "ambiguous", gfc_new_block->name);
3769 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
3770 &gfc_new_block->declared_at) ==
3773 if (st == ST_FUNCTION)
3774 gfc_add_function (&sym->attr, sym->name,
3775 &gfc_new_block->declared_at);
3777 gfc_add_subroutine (&sym->attr, sym->name,
3778 &gfc_new_block->declared_at);
3782 gfc_commit_symbols ();
3785 sym = gfc_new_block;
3787 /* Mark this as a contained function, so it isn't replaced
3788 by other module functions. */
3789 sym->attr.contained = 1;
3790 sym->attr.referenced = 1;
3792 parse_progunit (ST_NONE);
3794 /* Fix up any sibling functions that refer to this one. */
3795 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
3796 /* Or refer to any of its alternate entry points. */
3797 for (el = gfc_current_ns->entries; el; el = el->next)
3798 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
3800 gfc_current_ns->code = s2.head;
3801 gfc_current_ns = parent_ns;
3806 /* These statements are associated with the end of the host unit. */
3807 case ST_END_FUNCTION:
3809 case ST_END_PROGRAM:
3810 case ST_END_SUBROUTINE:
3811 accept_statement (st);
3815 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
3816 gfc_ascii_statement (st));
3817 reject_statement ();
3823 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
3824 && st != ST_END_MODULE && st != ST_END_PROGRAM);
3826 /* The first namespace in the list is guaranteed to not have
3827 anything (worthwhile) in it. */
3828 tmp = gfc_current_ns;
3829 gfc_current_ns = parent_ns;
3830 if (seen_error && tmp->refs > 1)
3831 gfc_free_namespace (tmp);
3833 ns = gfc_current_ns->contained;
3834 gfc_current_ns->contained = ns->sibling;
3835 gfc_free_namespace (ns);
3838 if (!contains_statements)
3839 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTAINS statement without "
3840 "FUNCTION or SUBROUTINE statement at %C");
3844 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
3847 parse_progunit (gfc_statement st)
3852 st = parse_spec (st);
3859 /* This is not allowed within BLOCK! */
3860 if (gfc_current_state () != COMP_BLOCK)
3865 accept_statement (st);
3872 if (gfc_current_state () == COMP_FUNCTION)
3873 gfc_check_function_type (gfc_current_ns);
3878 st = parse_executable (st);
3886 /* This is not allowed within BLOCK! */
3887 if (gfc_current_state () != COMP_BLOCK)
3892 accept_statement (st);
3899 unexpected_statement (st);
3900 reject_statement ();
3901 st = next_statement ();
3907 for (p = gfc_state_stack; p; p = p->previous)
3908 if (p->state == COMP_CONTAINS)
3911 if (gfc_find_state (COMP_MODULE) == SUCCESS)
3916 gfc_error ("CONTAINS statement at %C is already in a contained "
3918 st = next_statement ();
3922 parse_contained (0);
3925 gfc_current_ns->code = gfc_state_stack->head;
3929 /* Come here to complain about a global symbol already in use as
3933 gfc_global_used (gfc_gsymbol *sym, locus *where)
3938 where = &gfc_current_locus;
3948 case GSYM_SUBROUTINE:
3949 name = "SUBROUTINE";
3954 case GSYM_BLOCK_DATA:
3955 name = "BLOCK DATA";
3961 gfc_internal_error ("gfc_global_used(): Bad type");
3965 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
3966 sym->name, where, name, &sym->where);
3970 /* Parse a block data program unit. */
3973 parse_block_data (void)
3976 static locus blank_locus;
3977 static int blank_block=0;
3980 gfc_current_ns->proc_name = gfc_new_block;
3981 gfc_current_ns->is_block_data = 1;
3983 if (gfc_new_block == NULL)
3986 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3987 "prior BLOCK DATA at %L", &blank_locus);
3991 blank_locus = gfc_current_locus;
3996 s = gfc_get_gsymbol (gfc_new_block->name);
3998 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
3999 gfc_global_used(s, NULL);
4002 s->type = GSYM_BLOCK_DATA;
4003 s->where = gfc_current_locus;
4008 st = parse_spec (ST_NONE);
4010 while (st != ST_END_BLOCK_DATA)
4012 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
4013 gfc_ascii_statement (st));
4014 reject_statement ();
4015 st = next_statement ();
4020 /* Parse a module subprogram. */
4028 s = gfc_get_gsymbol (gfc_new_block->name);
4029 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
4030 gfc_global_used(s, NULL);
4033 s->type = GSYM_MODULE;
4034 s->where = gfc_current_locus;
4038 st = parse_spec (ST_NONE);
4047 parse_contained (1);
4051 accept_statement (st);
4055 gfc_error ("Unexpected %s statement in MODULE at %C",
4056 gfc_ascii_statement (st));
4058 reject_statement ();
4059 st = next_statement ();
4063 s->ns = gfc_current_ns;
4067 /* Add a procedure name to the global symbol table. */
4070 add_global_procedure (int sub)
4074 s = gfc_get_gsymbol(gfc_new_block->name);
4077 || (s->type != GSYM_UNKNOWN
4078 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
4079 gfc_global_used(s, NULL);
4082 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4083 s->where = gfc_current_locus;
4085 s->ns = gfc_current_ns;
4090 /* Add a program to the global symbol table. */
4093 add_global_program (void)
4097 if (gfc_new_block == NULL)
4099 s = gfc_get_gsymbol (gfc_new_block->name);
4101 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
4102 gfc_global_used(s, NULL);
4105 s->type = GSYM_PROGRAM;
4106 s->where = gfc_current_locus;
4108 s->ns = gfc_current_ns;
4113 /* Resolve all the program units when whole file scope option
4116 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
4118 gfc_free_dt_list ();
4119 gfc_current_ns = gfc_global_ns_list;
4120 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4122 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4123 gfc_resolve (gfc_current_ns);
4124 gfc_current_ns->derived_types = gfc_derived_types;
4125 gfc_derived_types = NULL;
4131 clean_up_modules (gfc_gsymbol *gsym)
4136 clean_up_modules (gsym->left);
4137 clean_up_modules (gsym->right);
4139 if (gsym->type != GSYM_MODULE || !gsym->ns)
4142 gfc_current_ns = gsym->ns;
4143 gfc_derived_types = gfc_current_ns->derived_types;
4150 /* Translate all the program units when whole file scope option
4151 is active. This could be in a different order to resolution if
4152 there are forward references in the file. */
4154 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
4158 gfc_current_ns = gfc_global_ns_list;
4159 gfc_get_errors (NULL, &errors);
4161 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4163 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4164 gfc_derived_types = gfc_current_ns->derived_types;
4165 gfc_generate_code (gfc_current_ns);
4166 gfc_current_ns->translated = 1;
4169 /* Clean up all the namespaces after translation. */
4170 gfc_current_ns = gfc_global_ns_list;
4171 for (;gfc_current_ns;)
4173 gfc_namespace *ns = gfc_current_ns->sibling;
4174 gfc_derived_types = gfc_current_ns->derived_types;
4176 gfc_current_ns = ns;
4179 clean_up_modules (gfc_gsym_root);
4183 /* Top level parser. */
4186 gfc_parse_file (void)
4188 int seen_program, errors_before, errors;
4189 gfc_state_data top, s;
4192 gfc_namespace *next;
4194 gfc_start_source_files ();
4196 top.state = COMP_NONE;
4198 top.previous = NULL;
4199 top.head = top.tail = NULL;
4200 top.do_variable = NULL;
4202 gfc_state_stack = ⊤
4204 gfc_clear_new_st ();
4206 gfc_statement_label = NULL;
4208 if (setjmp (eof_buf))
4209 return FAILURE; /* Come here on unexpected EOF */
4211 /* Prepare the global namespace that will contain the
4213 gfc_global_ns_list = next = NULL;
4217 /* Exit early for empty files. */
4223 st = next_statement ();
4232 goto duplicate_main;
4234 prog_locus = gfc_current_locus;
4236 push_state (&s, COMP_PROGRAM, gfc_new_block);
4237 main_program_symbol(gfc_current_ns, gfc_new_block->name);
4238 accept_statement (st);
4239 add_global_program ();
4240 parse_progunit (ST_NONE);
4241 if (gfc_option.flag_whole_file)
4246 add_global_procedure (1);
4247 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
4248 accept_statement (st);
4249 parse_progunit (ST_NONE);
4250 if (gfc_option.flag_whole_file)
4255 add_global_procedure (0);
4256 push_state (&s, COMP_FUNCTION, gfc_new_block);
4257 accept_statement (st);
4258 parse_progunit (ST_NONE);
4259 if (gfc_option.flag_whole_file)
4264 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
4265 accept_statement (st);
4266 parse_block_data ();
4270 push_state (&s, COMP_MODULE, gfc_new_block);
4271 accept_statement (st);
4273 gfc_get_errors (NULL, &errors_before);
4277 /* Anything else starts a nameless main program block. */
4280 goto duplicate_main;
4282 prog_locus = gfc_current_locus;
4284 push_state (&s, COMP_PROGRAM, gfc_new_block);
4285 main_program_symbol (gfc_current_ns, "MAIN__");
4286 parse_progunit (st);
4287 if (gfc_option.flag_whole_file)
4292 /* Handle the non-program units. */
4293 gfc_current_ns->code = s.head;
4295 gfc_resolve (gfc_current_ns);
4297 /* Dump the parse tree if requested. */
4298 if (gfc_option.dump_parse_tree)
4299 gfc_dump_parse_tree (gfc_current_ns, stdout);
4301 gfc_get_errors (NULL, &errors);
4302 if (s.state == COMP_MODULE)
4304 gfc_dump_module (s.sym->name, errors_before == errors);
4306 gfc_generate_module_code (gfc_current_ns);
4308 if (!gfc_option.flag_whole_file)
4312 gfc_current_ns->derived_types = gfc_derived_types;
4313 gfc_derived_types = NULL;
4314 gfc_current_ns = NULL;
4320 gfc_generate_code (gfc_current_ns);
4328 /* The main program and non-contained procedures are put
4329 in the global namespace list, so that they can be processed
4330 later and all their interfaces resolved. */
4331 gfc_current_ns->code = s.head;
4333 next->sibling = gfc_current_ns;
4335 gfc_global_ns_list = gfc_current_ns;
4337 next = gfc_current_ns;
4344 if (!gfc_option.flag_whole_file)
4347 /* Do the resolution. */
4348 resolve_all_program_units (gfc_global_ns_list);
4350 /* Do the parse tree dump. */
4352 = gfc_option.dump_parse_tree ? gfc_global_ns_list : NULL;
4354 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4356 gfc_dump_parse_tree (gfc_current_ns, stdout);
4357 fputs ("------------------------------------------\n\n", stdout);
4360 /* Do the translation. */
4361 translate_all_program_units (gfc_global_ns_list);
4365 gfc_end_source_files ();
4369 /* If we see a duplicate main program, shut down. If the second
4370 instance is an implied main program, i.e. data decls or executable
4371 statements, we're in for lots of errors. */
4372 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
4373 reject_statement ();