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, BLOCK and ASSOCIATE
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_associate, ST_ASSOCIATE);
318 match (NULL, gfc_match_critical, ST_CRITICAL);
319 match (NULL, gfc_match_select, ST_SELECT_CASE);
320 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
322 /* General statement matching: Instead of testing every possible
323 statement, we eliminate most possibilities by peeking at the
326 c = gfc_peek_ascii_char ();
331 match ("abstract% interface", gfc_match_abstract_interface,
333 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
334 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
335 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
336 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
340 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
341 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
342 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
346 match ("call", gfc_match_call, ST_CALL);
347 match ("close", gfc_match_close, ST_CLOSE);
348 match ("continue", gfc_match_continue, ST_CONTINUE);
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)
489 gfc_clear_error (); /* Clear any pending errors. */
490 gfc_clear_warning (); /* Clear any pending warnings. */
494 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
495 "or ELEMENTAL procedures");
496 gfc_error_recovery ();
500 old_locus = gfc_current_locus;
502 /* General OpenMP directive matching: Instead of testing every possible
503 statement, we eliminate most possibilities by peeking at the
506 c = gfc_peek_ascii_char ();
511 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
514 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
517 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
520 match ("do", gfc_match_omp_do, ST_OMP_DO);
523 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
524 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
525 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
526 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
527 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
528 match ("end parallel sections", gfc_match_omp_eos,
529 ST_OMP_END_PARALLEL_SECTIONS);
530 match ("end parallel workshare", gfc_match_omp_eos,
531 ST_OMP_END_PARALLEL_WORKSHARE);
532 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
533 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
534 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
535 match ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
536 match ("end workshare", gfc_match_omp_end_nowait,
537 ST_OMP_END_WORKSHARE);
540 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
543 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
546 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
549 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
550 match ("parallel sections", gfc_match_omp_parallel_sections,
551 ST_OMP_PARALLEL_SECTIONS);
552 match ("parallel workshare", gfc_match_omp_parallel_workshare,
553 ST_OMP_PARALLEL_WORKSHARE);
554 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
557 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
558 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
559 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
562 match ("task", gfc_match_omp_task, ST_OMP_TASK);
563 match ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
564 match ("threadprivate", gfc_match_omp_threadprivate,
565 ST_OMP_THREADPRIVATE);
567 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
571 /* All else has failed, so give up. See if any of the matchers has
572 stored an error message of some sort. */
574 if (gfc_error_check () == 0)
575 gfc_error_now ("Unclassifiable OpenMP directive at %C");
579 gfc_error_recovery ();
585 decode_gcc_attribute (void)
593 gfc_clear_error (); /* Clear any pending errors. */
594 gfc_clear_warning (); /* Clear any pending warnings. */
595 old_locus = gfc_current_locus;
597 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
599 /* All else has failed, so give up. See if any of the matchers has
600 stored an error message of some sort. */
602 if (gfc_error_check () == 0)
603 gfc_error_now ("Unclassifiable GCC directive at %C");
607 gfc_error_recovery ();
615 /* Get the next statement in free form source. */
624 at_bol = gfc_at_bol ();
625 gfc_gobble_whitespace ();
627 c = gfc_peek_ascii_char ();
633 /* Found a statement label? */
634 m = gfc_match_st_label (&gfc_statement_label);
636 d = gfc_peek_ascii_char ();
637 if (m != MATCH_YES || !gfc_is_whitespace (d))
639 gfc_match_small_literal_int (&i, &cnt);
642 gfc_error_now ("Too many digits in statement label at %C");
645 gfc_error_now ("Zero is not a valid statement label at %C");
648 c = gfc_next_ascii_char ();
651 if (!gfc_is_whitespace (c))
652 gfc_error_now ("Non-numeric character in statement label at %C");
658 label_locus = gfc_current_locus;
660 gfc_gobble_whitespace ();
662 if (at_bol && gfc_peek_ascii_char () == ';')
664 gfc_error_now ("Semicolon at %C needs to be preceded by "
666 gfc_next_ascii_char (); /* Eat up the semicolon. */
670 if (gfc_match_eos () == MATCH_YES)
672 gfc_warning_now ("Ignoring statement label in empty statement "
673 "at %L", &label_locus);
674 gfc_free_st_label (gfc_statement_label);
675 gfc_statement_label = NULL;
682 /* Comments have already been skipped by the time we get here,
683 except for GCC attributes and OpenMP directives. */
685 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
686 c = gfc_peek_ascii_char ();
692 c = gfc_next_ascii_char ();
693 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
694 gcc_assert (c == "gcc$"[i]);
696 gfc_gobble_whitespace ();
697 return decode_gcc_attribute ();
700 else if (c == '$' && gfc_option.flag_openmp)
704 c = gfc_next_ascii_char ();
705 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
706 gcc_assert (c == "$omp"[i]);
708 gcc_assert (c == ' ' || c == '\t');
709 gfc_gobble_whitespace ();
710 return decode_omp_directive ();
716 if (at_bol && c == ';')
718 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
719 gfc_next_ascii_char (); /* Eat up the semicolon. */
723 return decode_statement ();
727 /* Get the next statement in fixed-form source. */
732 int label, digit_flag, i;
737 return decode_statement ();
739 /* Skip past the current label field, parsing a statement label if
740 one is there. This is a weird number parser, since the number is
741 contained within five columns and can have any kind of embedded
742 spaces. We also check for characters that make the rest of the
748 for (i = 0; i < 5; i++)
750 c = gfc_next_char_literal (0);
767 label = label * 10 + ((unsigned char) c - '0');
768 label_locus = gfc_current_locus;
772 /* Comments have already been skipped by the time we get
773 here, except for GCC attributes and OpenMP directives. */
776 c = gfc_next_char_literal (0);
778 if (TOLOWER (c) == 'g')
780 for (i = 0; i < 4; i++, c = gfc_next_char_literal (0))
781 gcc_assert (TOLOWER (c) == "gcc$"[i]);
783 return decode_gcc_attribute ();
785 else if (c == '$' && gfc_option.flag_openmp)
787 for (i = 0; i < 4; i++, c = gfc_next_char_literal (0))
788 gcc_assert ((char) gfc_wide_tolower (c) == "$omp"[i]);
790 if (c != ' ' && c != '0')
792 gfc_buffer_error (0);
793 gfc_error ("Bad continuation line at %C");
797 return decode_omp_directive ();
801 /* Comments have already been skipped by the time we get
802 here so don't bother checking for them. */
805 gfc_buffer_error (0);
806 gfc_error ("Non-numeric character in statement label at %C");
814 gfc_warning_now ("Zero is not a valid statement label at %C");
817 /* We've found a valid statement label. */
818 gfc_statement_label = gfc_get_st_label (label);
822 /* Since this line starts a statement, it cannot be a continuation
823 of a previous statement. If we see something here besides a
824 space or zero, it must be a bad continuation line. */
826 c = gfc_next_char_literal (0);
830 if (c != ' ' && c != '0')
832 gfc_buffer_error (0);
833 gfc_error ("Bad continuation line at %C");
837 /* Now that we've taken care of the statement label columns, we have
838 to make sure that the first nonblank character is not a '!'. If
839 it is, the rest of the line is a comment. */
843 loc = gfc_current_locus;
844 c = gfc_next_char_literal (0);
846 while (gfc_is_whitespace (c));
850 gfc_current_locus = loc;
854 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
858 if (gfc_match_eos () == MATCH_YES)
861 /* At this point, we've got a nonblank statement to parse. */
862 return decode_statement ();
866 gfc_warning_now ("Ignoring statement label in empty statement at %L",
869 gfc_current_locus.lb->truncated = 0;
875 /* Return the next non-ST_NONE statement to the caller. We also worry
876 about including files and the ends of include files at this stage. */
879 next_statement (void)
884 gfc_new_block = NULL;
886 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
889 gfc_statement_label = NULL;
890 gfc_buffer_error (1);
895 gfc_skip_comments ();
903 if (gfc_define_undef_line ())
906 old_locus = gfc_current_locus;
908 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
914 gfc_buffer_error (0);
916 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
918 gfc_free_st_label (gfc_statement_label);
919 gfc_statement_label = NULL;
920 gfc_current_locus = old_locus;
924 check_statement_label (st);
930 /****************************** Parser ***********************************/
932 /* The parser subroutines are of type 'try' that fail if the file ends
935 /* Macros that expand to case-labels for various classes of
936 statements. Start with executable statements that directly do
939 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
940 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
941 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
942 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
943 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
944 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
945 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
946 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
947 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_ERROR_STOP: \
948 case ST_SYNC_ALL: case ST_SYNC_IMAGES: case ST_SYNC_MEMORY
950 /* Statements that mark other executable statements. */
952 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
953 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
954 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
955 case ST_OMP_PARALLEL: \
956 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
957 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
958 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
959 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
960 case ST_OMP_TASK: case ST_CRITICAL
962 /* Declaration statements */
964 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
965 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
966 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
969 /* Block end statements. Errors associated with interchanging these
970 are detected in gfc_match_end(). */
972 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
973 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
974 case ST_END_BLOCK: case ST_END_ASSOCIATE
977 /* Push a new state onto the stack. */
980 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
982 p->state = new_state;
983 p->previous = gfc_state_stack;
985 p->head = p->tail = NULL;
986 p->do_variable = NULL;
991 /* Pop the current state. */
995 gfc_state_stack = gfc_state_stack->previous;
999 /* Try to find the given state in the state stack. */
1002 gfc_find_state (gfc_compile_state state)
1006 for (p = gfc_state_stack; p; p = p->previous)
1007 if (p->state == state)
1010 return (p == NULL) ? FAILURE : SUCCESS;
1014 /* Starts a new level in the statement list. */
1017 new_level (gfc_code *q)
1021 p = q->block = gfc_get_code ();
1023 gfc_state_stack->head = gfc_state_stack->tail = p;
1029 /* Add the current new_st code structure and adds it to the current
1030 program unit. As a side-effect, it zeroes the new_st. */
1033 add_statement (void)
1037 p = gfc_get_code ();
1040 p->loc = gfc_current_locus;
1042 if (gfc_state_stack->head == NULL)
1043 gfc_state_stack->head = p;
1045 gfc_state_stack->tail->next = p;
1047 while (p->next != NULL)
1050 gfc_state_stack->tail = p;
1052 gfc_clear_new_st ();
1058 /* Frees everything associated with the current statement. */
1061 undo_new_statement (void)
1063 gfc_free_statements (new_st.block);
1064 gfc_free_statements (new_st.next);
1065 gfc_free_statement (&new_st);
1066 gfc_clear_new_st ();
1070 /* If the current statement has a statement label, make sure that it
1071 is allowed to, or should have one. */
1074 check_statement_label (gfc_statement st)
1078 if (gfc_statement_label == NULL)
1080 if (st == ST_FORMAT)
1081 gfc_error ("FORMAT statement at %L does not have a statement label",
1088 case ST_END_PROGRAM:
1089 case ST_END_FUNCTION:
1090 case ST_END_SUBROUTINE:
1094 case ST_END_CRITICAL:
1097 type = ST_LABEL_TARGET;
1101 type = ST_LABEL_FORMAT;
1104 /* Statement labels are not restricted from appearing on a
1105 particular line. However, there are plenty of situations
1106 where the resulting label can't be referenced. */
1109 type = ST_LABEL_BAD_TARGET;
1113 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1115 new_st.here = gfc_statement_label;
1119 /* Figures out what the enclosing program unit is. This will be a
1120 function, subroutine, program, block data or module. */
1123 gfc_enclosing_unit (gfc_compile_state * result)
1127 for (p = gfc_state_stack; p; p = p->previous)
1128 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1129 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
1130 || p->state == COMP_PROGRAM)
1139 *result = COMP_PROGRAM;
1144 /* Translate a statement enum to a string. */
1147 gfc_ascii_statement (gfc_statement st)
1153 case ST_ARITHMETIC_IF:
1154 p = _("arithmetic IF");
1163 p = _("attribute declaration");
1199 p = _("data declaration");
1207 case ST_DERIVED_DECL:
1208 p = _("derived type declaration");
1222 case ST_END_ASSOCIATE:
1223 p = "END ASSOCIATE";
1228 case ST_END_BLOCK_DATA:
1229 p = "END BLOCK DATA";
1231 case ST_END_CRITICAL:
1243 case ST_END_FUNCTION:
1249 case ST_END_INTERFACE:
1250 p = "END INTERFACE";
1255 case ST_END_PROGRAM:
1261 case ST_END_SUBROUTINE:
1262 p = "END SUBROUTINE";
1273 case ST_EQUIVALENCE:
1285 case ST_FORALL_BLOCK: /* Fall through */
1307 case ST_IMPLICIT_NONE:
1308 p = "IMPLICIT NONE";
1310 case ST_IMPLIED_ENDDO:
1311 p = _("implied END DO");
1337 case ST_MODULE_PROC:
1338 p = "MODULE PROCEDURE";
1370 case ST_SYNC_IMAGES:
1373 case ST_SYNC_MEMORY:
1385 case ST_WHERE_BLOCK: /* Fall through */
1396 p = _("assignment");
1398 case ST_POINTER_ASSIGNMENT:
1399 p = _("pointer assignment");
1401 case ST_SELECT_CASE:
1404 case ST_SELECT_TYPE:
1419 case ST_STATEMENT_FUNCTION:
1420 p = "STATEMENT FUNCTION";
1422 case ST_LABEL_ASSIGNMENT:
1423 p = "LABEL ASSIGNMENT";
1426 p = "ENUM DEFINITION";
1429 p = "ENUMERATOR DEFINITION";
1437 case ST_OMP_BARRIER:
1438 p = "!$OMP BARRIER";
1440 case ST_OMP_CRITICAL:
1441 p = "!$OMP CRITICAL";
1446 case ST_OMP_END_CRITICAL:
1447 p = "!$OMP END CRITICAL";
1452 case ST_OMP_END_MASTER:
1453 p = "!$OMP END MASTER";
1455 case ST_OMP_END_ORDERED:
1456 p = "!$OMP END ORDERED";
1458 case ST_OMP_END_PARALLEL:
1459 p = "!$OMP END PARALLEL";
1461 case ST_OMP_END_PARALLEL_DO:
1462 p = "!$OMP END PARALLEL DO";
1464 case ST_OMP_END_PARALLEL_SECTIONS:
1465 p = "!$OMP END PARALLEL SECTIONS";
1467 case ST_OMP_END_PARALLEL_WORKSHARE:
1468 p = "!$OMP END PARALLEL WORKSHARE";
1470 case ST_OMP_END_SECTIONS:
1471 p = "!$OMP END SECTIONS";
1473 case ST_OMP_END_SINGLE:
1474 p = "!$OMP END SINGLE";
1476 case ST_OMP_END_TASK:
1477 p = "!$OMP END TASK";
1479 case ST_OMP_END_WORKSHARE:
1480 p = "!$OMP END WORKSHARE";
1488 case ST_OMP_ORDERED:
1489 p = "!$OMP ORDERED";
1491 case ST_OMP_PARALLEL:
1492 p = "!$OMP PARALLEL";
1494 case ST_OMP_PARALLEL_DO:
1495 p = "!$OMP PARALLEL DO";
1497 case ST_OMP_PARALLEL_SECTIONS:
1498 p = "!$OMP PARALLEL SECTIONS";
1500 case ST_OMP_PARALLEL_WORKSHARE:
1501 p = "!$OMP PARALLEL WORKSHARE";
1503 case ST_OMP_SECTIONS:
1504 p = "!$OMP SECTIONS";
1506 case ST_OMP_SECTION:
1507 p = "!$OMP SECTION";
1515 case ST_OMP_TASKWAIT:
1516 p = "!$OMP TASKWAIT";
1518 case ST_OMP_THREADPRIVATE:
1519 p = "!$OMP THREADPRIVATE";
1521 case ST_OMP_WORKSHARE:
1522 p = "!$OMP WORKSHARE";
1525 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1532 /* Create a symbol for the main program and assign it to ns->proc_name. */
1535 main_program_symbol (gfc_namespace *ns, const char *name)
1537 gfc_symbol *main_program;
1538 symbol_attribute attr;
1540 gfc_get_symbol (name, ns, &main_program);
1541 gfc_clear_attr (&attr);
1542 attr.flavor = FL_PROGRAM;
1543 attr.proc = PROC_UNKNOWN;
1544 attr.subroutine = 1;
1545 attr.access = ACCESS_PUBLIC;
1546 attr.is_main_program = 1;
1547 main_program->attr = attr;
1548 main_program->declared_at = gfc_current_locus;
1549 ns->proc_name = main_program;
1550 gfc_commit_symbols ();
1554 /* Do whatever is necessary to accept the last statement. */
1557 accept_statement (gfc_statement st)
1565 case ST_IMPLICIT_NONE:
1566 gfc_set_implicit_none ();
1575 gfc_current_ns->proc_name = gfc_new_block;
1578 /* If the statement is the end of a block, lay down a special code
1579 that allows a branch to the end of the block from within the
1580 construct. IF and SELECT are treated differently from DO
1581 (where EXEC_NOP is added inside the loop) for two
1583 1. END DO has a meaning in the sense that after a GOTO to
1584 it, the loop counter must be increased.
1585 2. IF blocks and SELECT blocks can consist of multiple
1586 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
1587 Putting the label before the END IF would make the jump
1588 from, say, the ELSE IF block to the END IF illegal. */
1592 case ST_END_CRITICAL:
1593 if (gfc_statement_label != NULL)
1595 new_st.op = EXEC_END_BLOCK;
1600 /* The end-of-program unit statements do not get the special
1601 marker and require a statement of some sort if they are a
1604 case ST_END_PROGRAM:
1605 case ST_END_FUNCTION:
1606 case ST_END_SUBROUTINE:
1607 if (gfc_statement_label != NULL)
1609 new_st.op = EXEC_RETURN;
1614 new_st.op = EXEC_END_PROCEDURE;
1630 gfc_commit_symbols ();
1631 gfc_warning_check ();
1632 gfc_clear_new_st ();
1636 /* Undo anything tentative that has been built for the current
1640 reject_statement (void)
1642 /* Revert to the previous charlen chain. */
1643 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
1644 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
1646 gfc_new_block = NULL;
1647 gfc_undo_symbols ();
1648 gfc_clear_warning ();
1649 undo_new_statement ();
1653 /* Generic complaint about an out of order statement. We also do
1654 whatever is necessary to clean up. */
1657 unexpected_statement (gfc_statement st)
1659 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1661 reject_statement ();
1665 /* Given the next statement seen by the matcher, make sure that it is
1666 in proper order with the last. This subroutine is initialized by
1667 calling it with an argument of ST_NONE. If there is a problem, we
1668 issue an error and return FAILURE. Otherwise we return SUCCESS.
1670 Individual parsers need to verify that the statements seen are
1671 valid before calling here, i.e., ENTRY statements are not allowed in
1672 INTERFACE blocks. The following diagram is taken from the standard:
1674 +---------------------------------------+
1675 | program subroutine function module |
1676 +---------------------------------------+
1678 +---------------------------------------+
1680 +---------------------------------------+
1682 | +-----------+------------------+
1683 | | parameter | implicit |
1684 | +-----------+------------------+
1685 | format | | derived type |
1686 | entry | parameter | interface |
1687 | | data | specification |
1688 | | | statement func |
1689 | +-----------+------------------+
1690 | | data | executable |
1691 +--------+-----------+------------------+
1693 +---------------------------------------+
1694 | internal module/subprogram |
1695 +---------------------------------------+
1697 +---------------------------------------+
1706 ORDER_IMPLICIT_NONE,
1714 enum state_order state;
1715 gfc_statement last_statement;
1721 verify_st_order (st_state *p, gfc_statement st, bool silent)
1727 p->state = ORDER_START;
1731 if (p->state > ORDER_USE)
1733 p->state = ORDER_USE;
1737 if (p->state > ORDER_IMPORT)
1739 p->state = ORDER_IMPORT;
1742 case ST_IMPLICIT_NONE:
1743 if (p->state > ORDER_IMPLICIT_NONE)
1746 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1747 statement disqualifies a USE but not an IMPLICIT NONE.
1748 Duplicate IMPLICIT NONEs are caught when the implicit types
1751 p->state = ORDER_IMPLICIT_NONE;
1755 if (p->state > ORDER_IMPLICIT)
1757 p->state = ORDER_IMPLICIT;
1762 if (p->state < ORDER_IMPLICIT_NONE)
1763 p->state = ORDER_IMPLICIT_NONE;
1767 if (p->state >= ORDER_EXEC)
1769 if (p->state < ORDER_IMPLICIT)
1770 p->state = ORDER_IMPLICIT;
1774 if (p->state < ORDER_SPEC)
1775 p->state = ORDER_SPEC;
1780 case ST_DERIVED_DECL:
1782 if (p->state >= ORDER_EXEC)
1784 if (p->state < ORDER_SPEC)
1785 p->state = ORDER_SPEC;
1790 if (p->state < ORDER_EXEC)
1791 p->state = ORDER_EXEC;
1795 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1796 gfc_ascii_statement (st));
1799 /* All is well, record the statement in case we need it next time. */
1800 p->where = gfc_current_locus;
1801 p->last_statement = st;
1806 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1807 gfc_ascii_statement (st),
1808 gfc_ascii_statement (p->last_statement), &p->where);
1814 /* Handle an unexpected end of file. This is a show-stopper... */
1816 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1819 unexpected_eof (void)
1823 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1825 /* Memory cleanup. Move to "second to last". */
1826 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1829 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1832 longjmp (eof_buf, 1);
1836 /* Parse the CONTAINS section of a derived type definition. */
1838 gfc_access gfc_typebound_default_access;
1841 parse_derived_contains (void)
1844 bool seen_private = false;
1845 bool seen_comps = false;
1846 bool error_flag = false;
1849 gcc_assert (gfc_current_state () == COMP_DERIVED);
1850 gcc_assert (gfc_current_block ());
1852 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
1854 if (gfc_current_block ()->attr.sequence)
1855 gfc_error ("Derived-type '%s' with SEQUENCE must not have a CONTAINS"
1856 " section at %C", gfc_current_block ()->name);
1857 if (gfc_current_block ()->attr.is_bind_c)
1858 gfc_error ("Derived-type '%s' with BIND(C) must not have a CONTAINS"
1859 " section at %C", gfc_current_block ()->name);
1861 accept_statement (ST_CONTAINS);
1862 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
1864 gfc_typebound_default_access = ACCESS_PUBLIC;
1870 st = next_statement ();
1878 gfc_error ("Components in TYPE at %C must precede CONTAINS");
1883 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Type-bound"
1884 " procedure at %C") == FAILURE)
1887 accept_statement (ST_PROCEDURE);
1892 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: GENERIC binding"
1893 " at %C") == FAILURE)
1896 accept_statement (ST_GENERIC);
1901 if (gfc_notify_std (GFC_STD_F2003,
1902 "Fortran 2003: FINAL procedure declaration"
1903 " at %C") == FAILURE)
1906 accept_statement (ST_FINAL);
1914 && (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Derived type "
1915 "definition at %C with empty CONTAINS "
1916 "section") == FAILURE))
1919 /* ST_END_TYPE is accepted by parse_derived after return. */
1923 if (gfc_find_state (COMP_MODULE) == FAILURE)
1925 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1933 gfc_error ("PRIVATE statement at %C must precede procedure"
1941 gfc_error ("Duplicate PRIVATE statement at %C");
1945 accept_statement (ST_PRIVATE);
1946 gfc_typebound_default_access = ACCESS_PRIVATE;
1947 seen_private = true;
1951 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
1956 gfc_error ("Already inside a CONTAINS block at %C");
1961 unexpected_statement (st);
1967 gcc_assert (gfc_current_state () == COMP_DERIVED);
1973 /* Parse a derived type. */
1976 parse_derived (void)
1978 int compiling_type, seen_private, seen_sequence, seen_component;
1984 accept_statement (ST_DERIVED_DECL);
1985 push_state (&s, COMP_DERIVED, gfc_new_block);
1987 gfc_new_block->component_access = ACCESS_PUBLIC;
1994 while (compiling_type)
1996 st = next_statement ();
2004 accept_statement (st);
2009 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2016 if (!seen_component)
2017 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Derived type "
2018 "definition at %C without components");
2020 accept_statement (ST_END_TYPE);
2024 if (gfc_find_state (COMP_MODULE) == FAILURE)
2026 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2033 gfc_error ("PRIVATE statement at %C must precede "
2034 "structure components");
2039 gfc_error ("Duplicate PRIVATE statement at %C");
2041 s.sym->component_access = ACCESS_PRIVATE;
2043 accept_statement (ST_PRIVATE);
2050 gfc_error ("SEQUENCE statement at %C must precede "
2051 "structure components");
2055 if (gfc_current_block ()->attr.sequence)
2056 gfc_warning ("SEQUENCE attribute at %C already specified in "
2061 gfc_error ("Duplicate SEQUENCE statement at %C");
2065 gfc_add_sequence (&gfc_current_block ()->attr,
2066 gfc_current_block ()->name, NULL);
2070 gfc_notify_std (GFC_STD_F2003,
2071 "Fortran 2003: CONTAINS block in derived type"
2072 " definition at %C");
2074 accept_statement (ST_CONTAINS);
2075 parse_derived_contains ();
2079 unexpected_statement (st);
2084 /* need to verify that all fields of the derived type are
2085 * interoperable with C if the type is declared to be bind(c)
2087 sym = gfc_current_block ();
2088 for (c = sym->components; c; c = c->next)
2090 /* Look for allocatable components. */
2091 if (c->attr.allocatable
2092 || (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.allocatable)
2093 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.alloc_comp))
2094 sym->attr.alloc_comp = 1;
2096 /* Look for pointer components. */
2098 || (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.pointer)
2099 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2100 sym->attr.pointer_comp = 1;
2102 /* Look for procedure pointer components. */
2103 if (c->attr.proc_pointer
2104 || (c->ts.type == BT_DERIVED
2105 && c->ts.u.derived->attr.proc_pointer_comp))
2106 sym->attr.proc_pointer_comp = 1;
2108 /* Looking for coarray components. */
2109 if (c->attr.codimension
2110 || (c->attr.coarray_comp && !c->attr.pointer && !c->attr.allocatable))
2111 sym->attr.coarray_comp = 1;
2113 /* Look for private components. */
2114 if (sym->component_access == ACCESS_PRIVATE
2115 || c->attr.access == ACCESS_PRIVATE
2116 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
2117 sym->attr.private_comp = 1;
2120 if (!seen_component)
2121 sym->attr.zero_comp = 1;
2127 /* Parse an ENUM. */
2135 int seen_enumerator = 0;
2137 push_state (&s, COMP_ENUM, gfc_new_block);
2141 while (compiling_enum)
2143 st = next_statement ();
2151 seen_enumerator = 1;
2152 accept_statement (st);
2157 if (!seen_enumerator)
2158 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2159 accept_statement (st);
2163 gfc_free_enum_history ();
2164 unexpected_statement (st);
2172 /* Parse an interface. We must be able to deal with the possibility
2173 of recursive interfaces. The parse_spec() subroutine is mutually
2174 recursive with parse_interface(). */
2176 static gfc_statement parse_spec (gfc_statement);
2179 parse_interface (void)
2181 gfc_compile_state new_state = COMP_NONE, current_state;
2182 gfc_symbol *prog_unit, *sym;
2183 gfc_interface_info save;
2184 gfc_state_data s1, s2;
2188 accept_statement (ST_INTERFACE);
2190 current_interface.ns = gfc_current_ns;
2191 save = current_interface;
2193 sym = (current_interface.type == INTERFACE_GENERIC
2194 || current_interface.type == INTERFACE_USER_OP)
2195 ? gfc_new_block : NULL;
2197 push_state (&s1, COMP_INTERFACE, sym);
2198 current_state = COMP_NONE;
2201 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
2203 st = next_statement ();
2211 if (st == ST_SUBROUTINE)
2212 new_state = COMP_SUBROUTINE;
2213 else if (st == ST_FUNCTION)
2214 new_state = COMP_FUNCTION;
2215 if (gfc_new_block->attr.pointer)
2217 gfc_new_block->attr.pointer = 0;
2218 gfc_new_block->attr.proc_pointer = 1;
2220 if (gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
2221 gfc_new_block->formal, NULL) == FAILURE)
2223 reject_statement ();
2224 gfc_free_namespace (gfc_current_ns);
2230 case ST_MODULE_PROC: /* The module procedure matcher makes
2231 sure the context is correct. */
2232 accept_statement (st);
2233 gfc_free_namespace (gfc_current_ns);
2236 case ST_END_INTERFACE:
2237 gfc_free_namespace (gfc_current_ns);
2238 gfc_current_ns = current_interface.ns;
2242 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2243 gfc_ascii_statement (st));
2244 reject_statement ();
2245 gfc_free_namespace (gfc_current_ns);
2250 /* Make sure that a generic interface has only subroutines or
2251 functions and that the generic name has the right attribute. */
2252 if (current_interface.type == INTERFACE_GENERIC)
2254 if (current_state == COMP_NONE)
2256 if (new_state == COMP_FUNCTION && sym)
2257 gfc_add_function (&sym->attr, sym->name, NULL);
2258 else if (new_state == COMP_SUBROUTINE && sym)
2259 gfc_add_subroutine (&sym->attr, sym->name, NULL);
2261 current_state = new_state;
2265 if (new_state != current_state)
2267 if (new_state == COMP_SUBROUTINE)
2268 gfc_error ("SUBROUTINE at %C does not belong in a "
2269 "generic function interface");
2271 if (new_state == COMP_FUNCTION)
2272 gfc_error ("FUNCTION at %C does not belong in a "
2273 "generic subroutine interface");
2278 if (current_interface.type == INTERFACE_ABSTRACT)
2280 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
2281 if (gfc_is_intrinsic_typename (gfc_new_block->name))
2282 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
2283 "cannot be the same as an intrinsic type",
2284 gfc_new_block->name);
2287 push_state (&s2, new_state, gfc_new_block);
2288 accept_statement (st);
2289 prog_unit = gfc_new_block;
2290 prog_unit->formal_ns = gfc_current_ns;
2291 proc_locus = gfc_current_locus;
2294 /* Read data declaration statements. */
2295 st = parse_spec (ST_NONE);
2297 /* Since the interface block does not permit an IMPLICIT statement,
2298 the default type for the function or the result must be taken
2299 from the formal namespace. */
2300 if (new_state == COMP_FUNCTION)
2302 if (prog_unit->result == prog_unit
2303 && prog_unit->ts.type == BT_UNKNOWN)
2304 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
2305 else if (prog_unit->result != prog_unit
2306 && prog_unit->result->ts.type == BT_UNKNOWN)
2307 gfc_set_default_type (prog_unit->result, 1,
2308 prog_unit->formal_ns);
2311 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
2313 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2314 gfc_ascii_statement (st));
2315 reject_statement ();
2319 /* Add EXTERNAL attribute to function or subroutine. */
2320 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
2321 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
2323 current_interface = save;
2324 gfc_add_interface (prog_unit);
2327 if (current_interface.ns
2328 && current_interface.ns->proc_name
2329 && strcmp (current_interface.ns->proc_name->name,
2330 prog_unit->name) == 0)
2331 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2332 "enclosing procedure", prog_unit->name, &proc_locus);
2341 /* Associate function characteristics by going back to the function
2342 declaration and rematching the prefix. */
2345 match_deferred_characteristics (gfc_typespec * ts)
2348 match m = MATCH_ERROR;
2349 char name[GFC_MAX_SYMBOL_LEN + 1];
2351 loc = gfc_current_locus;
2353 gfc_current_locus = gfc_current_block ()->declared_at;
2356 gfc_buffer_error (1);
2357 m = gfc_match_prefix (ts);
2358 gfc_buffer_error (0);
2360 if (ts->type == BT_DERIVED)
2368 /* Only permit one go at the characteristic association. */
2372 /* Set the function locus correctly. If we have not found the
2373 function name, there is an error. */
2375 && gfc_match ("function% %n", name) == MATCH_YES
2376 && strcmp (name, gfc_current_block ()->name) == 0)
2378 gfc_current_block ()->declared_at = gfc_current_locus;
2379 gfc_commit_symbols ();
2384 gfc_current_locus =loc;
2389 /* Check specification-expressions in the function result of the currently
2390 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2391 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2392 scope are not yet parsed so this has to be delayed up to parse_spec. */
2395 check_function_result_typed (void)
2397 gfc_typespec* ts = &gfc_current_ns->proc_name->result->ts;
2399 gcc_assert (gfc_current_state () == COMP_FUNCTION);
2400 gcc_assert (ts->type != BT_UNKNOWN);
2402 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2403 /* TODO: Extend when KIND type parameters are implemented. */
2404 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length)
2405 gfc_expr_check_typed (ts->u.cl->length, gfc_current_ns, true);
2409 /* Parse a set of specification statements. Returns the statement
2410 that doesn't fit. */
2412 static gfc_statement
2413 parse_spec (gfc_statement st)
2416 bool function_result_typed = false;
2417 bool bad_characteristic = false;
2420 verify_st_order (&ss, ST_NONE, false);
2422 st = next_statement ();
2424 /* If we are not inside a function or don't have a result specified so far,
2425 do nothing special about it. */
2426 if (gfc_current_state () != COMP_FUNCTION)
2427 function_result_typed = true;
2430 gfc_symbol* proc = gfc_current_ns->proc_name;
2433 if (proc->result->ts.type == BT_UNKNOWN)
2434 function_result_typed = true;
2439 /* If we're inside a BLOCK construct, some statements are disallowed.
2440 Check this here. Attribute declaration statements like INTENT, OPTIONAL
2441 or VALUE are also disallowed, but they don't have a particular ST_*
2442 key so we have to check for them individually in their matcher routine. */
2443 if (gfc_current_state () == COMP_BLOCK)
2447 case ST_IMPLICIT_NONE:
2450 case ST_EQUIVALENCE:
2451 case ST_STATEMENT_FUNCTION:
2452 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
2453 gfc_ascii_statement (st));
2460 /* If we find a statement that can not be followed by an IMPLICIT statement
2461 (and thus we can expect to see none any further), type the function result
2462 if it has not yet been typed. Be careful not to give the END statement
2463 to verify_st_order! */
2464 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
2466 bool verify_now = false;
2468 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
2473 verify_st_order (&dummyss, ST_NONE, false);
2474 verify_st_order (&dummyss, st, false);
2476 if (verify_st_order (&dummyss, ST_IMPLICIT, true) == FAILURE)
2482 check_function_result_typed ();
2483 function_result_typed = true;
2492 case ST_IMPLICIT_NONE:
2494 if (!function_result_typed)
2496 check_function_result_typed ();
2497 function_result_typed = true;
2503 case ST_DATA: /* Not allowed in interfaces */
2504 if (gfc_current_state () == COMP_INTERFACE)
2514 case ST_DERIVED_DECL:
2517 if (verify_st_order (&ss, st, false) == FAILURE)
2519 reject_statement ();
2520 st = next_statement ();
2530 case ST_DERIVED_DECL:
2536 if (gfc_current_state () != COMP_MODULE)
2538 gfc_error ("%s statement must appear in a MODULE",
2539 gfc_ascii_statement (st));
2543 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
2545 gfc_error ("%s statement at %C follows another accessibility "
2546 "specification", gfc_ascii_statement (st));
2550 gfc_current_ns->default_access = (st == ST_PUBLIC)
2551 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2555 case ST_STATEMENT_FUNCTION:
2556 if (gfc_current_state () == COMP_MODULE)
2558 unexpected_statement (st);
2566 accept_statement (st);
2567 st = next_statement ();
2571 accept_statement (st);
2573 st = next_statement ();
2576 case ST_GET_FCN_CHARACTERISTICS:
2577 /* This statement triggers the association of a function's result
2579 ts = &gfc_current_block ()->result->ts;
2580 if (match_deferred_characteristics (ts) != MATCH_YES)
2581 bad_characteristic = true;
2583 st = next_statement ();
2590 /* If match_deferred_characteristics failed, then there is an error. */
2591 if (bad_characteristic)
2593 ts = &gfc_current_block ()->result->ts;
2594 if (ts->type != BT_DERIVED)
2595 gfc_error ("Bad kind expression for function '%s' at %L",
2596 gfc_current_block ()->name,
2597 &gfc_current_block ()->declared_at);
2599 gfc_error ("The type for function '%s' at %L is not accessible",
2600 gfc_current_block ()->name,
2601 &gfc_current_block ()->declared_at);
2603 gfc_current_block ()->ts.kind = 0;
2604 /* Keep the derived type; if it's bad, it will be discovered later. */
2605 if (!(ts->type == BT_DERIVED && ts->u.derived))
2606 ts->type = BT_UNKNOWN;
2613 /* Parse a WHERE block, (not a simple WHERE statement). */
2616 parse_where_block (void)
2618 int seen_empty_else;
2623 accept_statement (ST_WHERE_BLOCK);
2624 top = gfc_state_stack->tail;
2626 push_state (&s, COMP_WHERE, gfc_new_block);
2628 d = add_statement ();
2629 d->expr1 = top->expr1;
2635 seen_empty_else = 0;
2639 st = next_statement ();
2645 case ST_WHERE_BLOCK:
2646 parse_where_block ();
2651 accept_statement (st);
2655 if (seen_empty_else)
2657 gfc_error ("ELSEWHERE statement at %C follows previous "
2658 "unmasked ELSEWHERE");
2662 if (new_st.expr1 == NULL)
2663 seen_empty_else = 1;
2665 d = new_level (gfc_state_stack->head);
2667 d->expr1 = new_st.expr1;
2669 accept_statement (st);
2674 accept_statement (st);
2678 gfc_error ("Unexpected %s statement in WHERE block at %C",
2679 gfc_ascii_statement (st));
2680 reject_statement ();
2684 while (st != ST_END_WHERE);
2690 /* Parse a FORALL block (not a simple FORALL statement). */
2693 parse_forall_block (void)
2699 accept_statement (ST_FORALL_BLOCK);
2700 top = gfc_state_stack->tail;
2702 push_state (&s, COMP_FORALL, gfc_new_block);
2704 d = add_statement ();
2705 d->op = EXEC_FORALL;
2710 st = next_statement ();
2715 case ST_POINTER_ASSIGNMENT:
2718 accept_statement (st);
2721 case ST_WHERE_BLOCK:
2722 parse_where_block ();
2725 case ST_FORALL_BLOCK:
2726 parse_forall_block ();
2730 accept_statement (st);
2737 gfc_error ("Unexpected %s statement in FORALL block at %C",
2738 gfc_ascii_statement (st));
2740 reject_statement ();
2744 while (st != ST_END_FORALL);
2750 static gfc_statement parse_executable (gfc_statement);
2752 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2755 parse_if_block (void)
2764 accept_statement (ST_IF_BLOCK);
2766 top = gfc_state_stack->tail;
2767 push_state (&s, COMP_IF, gfc_new_block);
2769 new_st.op = EXEC_IF;
2770 d = add_statement ();
2772 d->expr1 = top->expr1;
2778 st = parse_executable (ST_NONE);
2788 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2789 "statement at %L", &else_locus);
2791 reject_statement ();
2795 d = new_level (gfc_state_stack->head);
2797 d->expr1 = new_st.expr1;
2799 accept_statement (st);
2806 gfc_error ("Duplicate ELSE statements at %L and %C",
2808 reject_statement ();
2813 else_locus = gfc_current_locus;
2815 d = new_level (gfc_state_stack->head);
2818 accept_statement (st);
2826 unexpected_statement (st);
2830 while (st != ST_ENDIF);
2833 accept_statement (st);
2837 /* Parse a SELECT block. */
2840 parse_select_block (void)
2846 accept_statement (ST_SELECT_CASE);
2848 cp = gfc_state_stack->tail;
2849 push_state (&s, COMP_SELECT, gfc_new_block);
2851 /* Make sure that the next statement is a CASE or END SELECT. */
2854 st = next_statement ();
2857 if (st == ST_END_SELECT)
2859 /* Empty SELECT CASE is OK. */
2860 accept_statement (st);
2867 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2870 reject_statement ();
2873 /* At this point, we're got a nonempty select block. */
2874 cp = new_level (cp);
2877 accept_statement (st);
2881 st = parse_executable (ST_NONE);
2888 cp = new_level (gfc_state_stack->head);
2890 gfc_clear_new_st ();
2892 accept_statement (st);
2898 /* Can't have an executable statement because of
2899 parse_executable(). */
2901 unexpected_statement (st);
2905 while (st != ST_END_SELECT);
2908 accept_statement (st);
2912 /* Pop the current selector from the SELECT TYPE stack. */
2915 select_type_pop (void)
2917 gfc_select_type_stack *old = select_type_stack;
2918 select_type_stack = old->prev;
2923 /* Parse a SELECT TYPE construct (F03:R821). */
2926 parse_select_type_block (void)
2932 accept_statement (ST_SELECT_TYPE);
2934 cp = gfc_state_stack->tail;
2935 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
2937 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
2941 st = next_statement ();
2944 if (st == ST_END_SELECT)
2945 /* Empty SELECT CASE is OK. */
2947 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
2950 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
2951 "following SELECT TYPE at %C");
2953 reject_statement ();
2956 /* At this point, we're got a nonempty select block. */
2957 cp = new_level (cp);
2960 accept_statement (st);
2964 st = parse_executable (ST_NONE);
2972 cp = new_level (gfc_state_stack->head);
2974 gfc_clear_new_st ();
2976 accept_statement (st);
2982 /* Can't have an executable statement because of
2983 parse_executable(). */
2985 unexpected_statement (st);
2989 while (st != ST_END_SELECT);
2993 accept_statement (st);
2994 gfc_current_ns = gfc_current_ns->parent;
2999 /* Given a symbol, make sure it is not an iteration variable for a DO
3000 statement. This subroutine is called when the symbol is seen in a
3001 context that causes it to become redefined. If the symbol is an
3002 iterator, we generate an error message and return nonzero. */
3005 gfc_check_do_variable (gfc_symtree *st)
3009 for (s=gfc_state_stack; s; s = s->previous)
3010 if (s->do_variable == st)
3012 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
3013 "loop beginning at %L", st->name, &s->head->loc);
3021 /* Checks to see if the current statement label closes an enddo.
3022 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3023 an error) if it incorrectly closes an ENDDO. */
3026 check_do_closure (void)
3030 if (gfc_statement_label == NULL)
3033 for (p = gfc_state_stack; p; p = p->previous)
3034 if (p->state == COMP_DO)
3038 return 0; /* No loops to close */
3040 if (p->ext.end_do_label == gfc_statement_label)
3042 if (p == gfc_state_stack)
3045 gfc_error ("End of nonblock DO statement at %C is within another block");
3049 /* At this point, the label doesn't terminate the innermost loop.
3050 Make sure it doesn't terminate another one. */
3051 for (; p; p = p->previous)
3052 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
3054 gfc_error ("End of nonblock DO statement at %C is interwoven "
3055 "with another DO loop");
3063 /* Parse a series of contained program units. */
3065 static void parse_progunit (gfc_statement);
3068 /* Parse a CRITICAL block. */
3071 parse_critical_block (void)
3077 s.ext.end_do_label = new_st.label1;
3079 accept_statement (ST_CRITICAL);
3080 top = gfc_state_stack->tail;
3082 push_state (&s, COMP_CRITICAL, gfc_new_block);
3084 d = add_statement ();
3085 d->op = EXEC_CRITICAL;
3090 st = parse_executable (ST_NONE);
3098 case ST_END_CRITICAL:
3099 if (s.ext.end_do_label != NULL
3100 && s.ext.end_do_label != gfc_statement_label)
3101 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3102 "match CRITIAL label");
3104 if (gfc_statement_label != NULL)
3106 new_st.op = EXEC_NOP;
3112 unexpected_statement (st);
3116 while (st != ST_END_CRITICAL);
3119 accept_statement (st);
3123 /* Set up the local namespace for a BLOCK construct. */
3126 gfc_build_block_ns (gfc_namespace *parent_ns)
3128 gfc_namespace* my_ns;
3130 my_ns = gfc_get_namespace (parent_ns, 1);
3131 my_ns->construct_entities = 1;
3133 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3134 code generation (so it must not be NULL).
3135 We set its recursive argument if our container procedure is recursive, so
3136 that local variables are accordingly placed on the stack when it
3137 will be necessary. */
3139 my_ns->proc_name = gfc_new_block;
3144 gfc_get_symbol ("block@", my_ns, &my_ns->proc_name);
3145 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
3146 my_ns->proc_name->name, NULL);
3147 gcc_assert (t == SUCCESS);
3150 if (parent_ns->proc_name)
3151 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
3157 /* Parse a BLOCK construct. */
3160 parse_block_construct (void)
3162 gfc_namespace* my_ns;
3165 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BLOCK construct at %C");
3167 my_ns = gfc_build_block_ns (gfc_current_ns);
3169 new_st.op = EXEC_BLOCK;
3170 new_st.ext.block.ns = my_ns;
3171 new_st.ext.block.assoc = NULL;
3172 accept_statement (ST_BLOCK);
3174 push_state (&s, COMP_BLOCK, my_ns->proc_name);
3175 gfc_current_ns = my_ns;
3177 parse_progunit (ST_NONE);
3179 gfc_current_ns = gfc_current_ns->parent;
3184 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3185 behind the scenes with compiler-generated variables. */
3188 parse_associate (void)
3190 gfc_namespace* my_ns;
3193 gfc_association_list* a;
3194 gfc_code* assignTail;
3196 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASSOCIATE construct at %C");
3198 my_ns = gfc_build_block_ns (gfc_current_ns);
3200 new_st.op = EXEC_BLOCK;
3201 new_st.ext.block.ns = my_ns;
3202 gcc_assert (new_st.ext.block.assoc);
3204 /* Add all associations to expressions as BLOCK variables, and create
3205 assignments to them giving their values. */
3206 gfc_current_ns = my_ns;
3208 for (a = new_st.ext.block.assoc; a; a = a->next)
3211 gfc_code* newAssign;
3213 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
3216 /* Note that in certain cases, the target-expression's type is not yet
3217 known and so we have to adapt the symbol's ts also during resolution
3219 a->st->n.sym->ts = a->target->ts;
3220 a->st->n.sym->attr.flavor = FL_VARIABLE;
3221 a->st->n.sym->assoc = a;
3222 gfc_set_sym_referenced (a->st->n.sym);
3224 /* Create the assignment to calculate the expression and set it. */
3225 newAssign = gfc_get_code ();
3226 newAssign->op = EXEC_ASSIGN;
3227 newAssign->loc = gfc_current_locus;
3228 newAssign->expr1 = gfc_get_variable_expr (a->st);
3229 newAssign->expr2 = a->target;
3233 assignTail->next = newAssign;
3235 gfc_current_ns->code = newAssign;
3236 assignTail = newAssign;
3240 gfc_error ("Association to variables is not yet supported at %C");
3243 gcc_assert (assignTail);
3245 accept_statement (ST_ASSOCIATE);
3246 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
3249 st = parse_executable (ST_NONE);
3256 accept_statement (st);
3257 assignTail->next = gfc_state_stack->head;
3261 unexpected_statement (st);
3265 gfc_current_ns = gfc_current_ns->parent;
3270 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
3271 handled inside of parse_executable(), because they aren't really
3275 parse_do_block (void)
3282 s.ext.end_do_label = new_st.label1;
3284 if (new_st.ext.iterator != NULL)
3285 stree = new_st.ext.iterator->var->symtree;
3289 accept_statement (ST_DO);
3291 top = gfc_state_stack->tail;
3292 push_state (&s, COMP_DO, gfc_new_block);
3294 s.do_variable = stree;
3296 top->block = new_level (top);
3297 top->block->op = EXEC_DO;
3300 st = parse_executable (ST_NONE);
3308 if (s.ext.end_do_label != NULL
3309 && s.ext.end_do_label != gfc_statement_label)
3310 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
3313 if (gfc_statement_label != NULL)
3315 new_st.op = EXEC_NOP;
3320 case ST_IMPLIED_ENDDO:
3321 /* If the do-stmt of this DO construct has a do-construct-name,
3322 the corresponding end-do must be an end-do-stmt (with a matching
3323 name, but in that case we must have seen ST_ENDDO first).
3324 We only complain about this in pedantic mode. */
3325 if (gfc_current_block () != NULL)
3326 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
3327 &gfc_current_block()->declared_at);
3332 unexpected_statement (st);
3337 accept_statement (st);
3341 /* Parse the statements of OpenMP do/parallel do. */
3343 static gfc_statement
3344 parse_omp_do (gfc_statement omp_st)
3350 accept_statement (omp_st);
3352 cp = gfc_state_stack->tail;
3353 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3354 np = new_level (cp);
3360 st = next_statement ();
3363 else if (st == ST_DO)
3366 unexpected_statement (st);
3370 if (gfc_statement_label != NULL
3371 && gfc_state_stack->previous != NULL
3372 && gfc_state_stack->previous->state == COMP_DO
3373 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
3381 there should be no !$OMP END DO. */
3383 return ST_IMPLIED_ENDDO;
3386 check_do_closure ();
3389 st = next_statement ();
3390 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
3392 if (new_st.op == EXEC_OMP_END_NOWAIT)
3393 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
3395 gcc_assert (new_st.op == EXEC_NOP);
3396 gfc_clear_new_st ();
3397 gfc_commit_symbols ();
3398 gfc_warning_check ();
3399 st = next_statement ();
3405 /* Parse the statements of OpenMP atomic directive. */
3408 parse_omp_atomic (void)
3414 accept_statement (ST_OMP_ATOMIC);
3416 cp = gfc_state_stack->tail;
3417 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3418 np = new_level (cp);
3424 st = next_statement ();
3427 else if (st == ST_ASSIGNMENT)
3430 unexpected_statement (st);
3433 accept_statement (st);
3439 /* Parse the statements of an OpenMP structured block. */
3442 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
3444 gfc_statement st, omp_end_st;
3448 accept_statement (omp_st);
3450 cp = gfc_state_stack->tail;
3451 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3452 np = new_level (cp);
3458 case ST_OMP_PARALLEL:
3459 omp_end_st = ST_OMP_END_PARALLEL;
3461 case ST_OMP_PARALLEL_SECTIONS:
3462 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
3464 case ST_OMP_SECTIONS:
3465 omp_end_st = ST_OMP_END_SECTIONS;
3467 case ST_OMP_ORDERED:
3468 omp_end_st = ST_OMP_END_ORDERED;
3470 case ST_OMP_CRITICAL:
3471 omp_end_st = ST_OMP_END_CRITICAL;
3474 omp_end_st = ST_OMP_END_MASTER;
3477 omp_end_st = ST_OMP_END_SINGLE;
3480 omp_end_st = ST_OMP_END_TASK;
3482 case ST_OMP_WORKSHARE:
3483 omp_end_st = ST_OMP_END_WORKSHARE;
3485 case ST_OMP_PARALLEL_WORKSHARE:
3486 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
3494 if (workshare_stmts_only)
3496 /* Inside of !$omp workshare, only
3499 where statements and constructs
3500 forall statements and constructs
3504 are allowed. For !$omp critical these
3505 restrictions apply recursively. */
3508 st = next_statement ();
3519 accept_statement (st);
3522 case ST_WHERE_BLOCK:
3523 parse_where_block ();
3526 case ST_FORALL_BLOCK:
3527 parse_forall_block ();
3530 case ST_OMP_PARALLEL:
3531 case ST_OMP_PARALLEL_SECTIONS:
3532 parse_omp_structured_block (st, false);
3535 case ST_OMP_PARALLEL_WORKSHARE:
3536 case ST_OMP_CRITICAL:
3537 parse_omp_structured_block (st, true);
3540 case ST_OMP_PARALLEL_DO:
3541 st = parse_omp_do (st);
3545 parse_omp_atomic ();
3556 st = next_statement ();
3560 st = parse_executable (ST_NONE);
3563 else if (st == ST_OMP_SECTION
3564 && (omp_st == ST_OMP_SECTIONS
3565 || omp_st == ST_OMP_PARALLEL_SECTIONS))
3567 np = new_level (np);
3571 else if (st != omp_end_st)
3572 unexpected_statement (st);
3574 while (st != omp_end_st);
3578 case EXEC_OMP_END_NOWAIT:
3579 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
3581 case EXEC_OMP_CRITICAL:
3582 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
3583 || (new_st.ext.omp_name != NULL
3584 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
3585 gfc_error ("Name after !$omp critical and !$omp end critical does "
3587 gfc_free (CONST_CAST (char *, new_st.ext.omp_name));
3589 case EXEC_OMP_END_SINGLE:
3590 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
3591 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
3592 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
3593 gfc_free_omp_clauses (new_st.ext.omp_clauses);
3601 gfc_clear_new_st ();
3602 gfc_commit_symbols ();
3603 gfc_warning_check ();
3608 /* Accept a series of executable statements. We return the first
3609 statement that doesn't fit to the caller. Any block statements are
3610 passed on to the correct handler, which usually passes the buck
3613 static gfc_statement
3614 parse_executable (gfc_statement st)
3619 st = next_statement ();
3623 close_flag = check_do_closure ();
3628 case ST_END_PROGRAM:
3631 case ST_END_FUNCTION:
3636 case ST_END_SUBROUTINE:
3641 case ST_SELECT_CASE:
3642 gfc_error ("%s statement at %C cannot terminate a non-block "
3643 "DO loop", gfc_ascii_statement (st));
3659 accept_statement (st);
3660 if (close_flag == 1)
3661 return ST_IMPLIED_ENDDO;
3665 parse_block_construct ();
3676 case ST_SELECT_CASE:
3677 parse_select_block ();
3680 case ST_SELECT_TYPE:
3681 parse_select_type_block();
3686 if (check_do_closure () == 1)
3687 return ST_IMPLIED_ENDDO;
3691 parse_critical_block ();
3694 case ST_WHERE_BLOCK:
3695 parse_where_block ();
3698 case ST_FORALL_BLOCK:
3699 parse_forall_block ();
3702 case ST_OMP_PARALLEL:
3703 case ST_OMP_PARALLEL_SECTIONS:
3704 case ST_OMP_SECTIONS:
3705 case ST_OMP_ORDERED:
3706 case ST_OMP_CRITICAL:
3710 parse_omp_structured_block (st, false);
3713 case ST_OMP_WORKSHARE:
3714 case ST_OMP_PARALLEL_WORKSHARE:
3715 parse_omp_structured_block (st, true);
3719 case ST_OMP_PARALLEL_DO:
3720 st = parse_omp_do (st);
3721 if (st == ST_IMPLIED_ENDDO)
3726 parse_omp_atomic ();
3733 st = next_statement ();
3738 /* Fix the symbols for sibling functions. These are incorrectly added to
3739 the child namespace as the parser didn't know about this procedure. */
3742 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
3746 gfc_symbol *old_sym;
3748 sym->attr.referenced = 1;
3749 for (ns = siblings; ns; ns = ns->sibling)
3751 st = gfc_find_symtree (ns->sym_root, sym->name);
3753 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
3754 goto fixup_contained;
3756 old_sym = st->n.sym;
3757 if (old_sym->ns == ns
3758 && !old_sym->attr.contained
3760 /* By 14.6.1.3, host association should be excluded
3761 for the following. */
3762 && !(old_sym->attr.external
3763 || (old_sym->ts.type != BT_UNKNOWN
3764 && !old_sym->attr.implicit_type)
3765 || old_sym->attr.flavor == FL_PARAMETER
3766 || old_sym->attr.use_assoc
3767 || old_sym->attr.in_common
3768 || old_sym->attr.in_equivalence
3769 || old_sym->attr.data
3770 || old_sym->attr.dummy
3771 || old_sym->attr.result
3772 || old_sym->attr.dimension
3773 || old_sym->attr.allocatable
3774 || old_sym->attr.intrinsic
3775 || old_sym->attr.generic
3776 || old_sym->attr.flavor == FL_NAMELIST
3777 || old_sym->attr.proc == PROC_ST_FUNCTION))
3779 /* Replace it with the symbol from the parent namespace. */
3783 /* Free the old (local) symbol. */
3785 if (old_sym->refs == 0)
3786 gfc_free_symbol (old_sym);
3790 /* Do the same for any contained procedures. */
3791 gfc_fixup_sibling_symbols (sym, ns->contained);
3796 parse_contained (int module)
3798 gfc_namespace *ns, *parent_ns, *tmp;
3799 gfc_state_data s1, s2;
3803 int contains_statements = 0;
3806 push_state (&s1, COMP_CONTAINS, NULL);
3807 parent_ns = gfc_current_ns;
3811 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
3813 gfc_current_ns->sibling = parent_ns->contained;
3814 parent_ns->contained = gfc_current_ns;
3817 /* Process the next available statement. We come here if we got an error
3818 and rejected the last statement. */
3819 st = next_statement ();
3828 contains_statements = 1;
3829 accept_statement (st);
3832 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
3835 /* For internal procedures, create/update the symbol in the
3836 parent namespace. */
3840 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
3841 gfc_error ("Contained procedure '%s' at %C is already "
3842 "ambiguous", gfc_new_block->name);
3845 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
3846 &gfc_new_block->declared_at) ==
3849 if (st == ST_FUNCTION)
3850 gfc_add_function (&sym->attr, sym->name,
3851 &gfc_new_block->declared_at);
3853 gfc_add_subroutine (&sym->attr, sym->name,
3854 &gfc_new_block->declared_at);
3858 gfc_commit_symbols ();
3861 sym = gfc_new_block;
3863 /* Mark this as a contained function, so it isn't replaced
3864 by other module functions. */
3865 sym->attr.contained = 1;
3866 sym->attr.referenced = 1;
3868 parse_progunit (ST_NONE);
3870 /* Fix up any sibling functions that refer to this one. */
3871 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
3872 /* Or refer to any of its alternate entry points. */
3873 for (el = gfc_current_ns->entries; el; el = el->next)
3874 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
3876 gfc_current_ns->code = s2.head;
3877 gfc_current_ns = parent_ns;
3882 /* These statements are associated with the end of the host unit. */
3883 case ST_END_FUNCTION:
3885 case ST_END_PROGRAM:
3886 case ST_END_SUBROUTINE:
3887 accept_statement (st);
3891 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
3892 gfc_ascii_statement (st));
3893 reject_statement ();
3899 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
3900 && st != ST_END_MODULE && st != ST_END_PROGRAM);
3902 /* The first namespace in the list is guaranteed to not have
3903 anything (worthwhile) in it. */
3904 tmp = gfc_current_ns;
3905 gfc_current_ns = parent_ns;
3906 if (seen_error && tmp->refs > 1)
3907 gfc_free_namespace (tmp);
3909 ns = gfc_current_ns->contained;
3910 gfc_current_ns->contained = ns->sibling;
3911 gfc_free_namespace (ns);
3914 if (!contains_statements)
3915 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTAINS statement without "
3916 "FUNCTION or SUBROUTINE statement at %C");
3920 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
3923 parse_progunit (gfc_statement st)
3928 st = parse_spec (st);
3935 /* This is not allowed within BLOCK! */
3936 if (gfc_current_state () != COMP_BLOCK)
3941 accept_statement (st);
3948 if (gfc_current_state () == COMP_FUNCTION)
3949 gfc_check_function_type (gfc_current_ns);
3954 st = parse_executable (st);
3962 /* This is not allowed within BLOCK! */
3963 if (gfc_current_state () != COMP_BLOCK)
3968 accept_statement (st);
3975 unexpected_statement (st);
3976 reject_statement ();
3977 st = next_statement ();
3983 for (p = gfc_state_stack; p; p = p->previous)
3984 if (p->state == COMP_CONTAINS)
3987 if (gfc_find_state (COMP_MODULE) == SUCCESS)
3992 gfc_error ("CONTAINS statement at %C is already in a contained "
3994 st = next_statement ();
3998 parse_contained (0);
4001 gfc_current_ns->code = gfc_state_stack->head;
4005 /* Come here to complain about a global symbol already in use as
4009 gfc_global_used (gfc_gsymbol *sym, locus *where)
4014 where = &gfc_current_locus;
4024 case GSYM_SUBROUTINE:
4025 name = "SUBROUTINE";
4030 case GSYM_BLOCK_DATA:
4031 name = "BLOCK DATA";
4037 gfc_internal_error ("gfc_global_used(): Bad type");
4041 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
4042 sym->name, where, name, &sym->where);
4046 /* Parse a block data program unit. */
4049 parse_block_data (void)
4052 static locus blank_locus;
4053 static int blank_block=0;
4056 gfc_current_ns->proc_name = gfc_new_block;
4057 gfc_current_ns->is_block_data = 1;
4059 if (gfc_new_block == NULL)
4062 gfc_error ("Blank BLOCK DATA at %C conflicts with "
4063 "prior BLOCK DATA at %L", &blank_locus);
4067 blank_locus = gfc_current_locus;
4072 s = gfc_get_gsymbol (gfc_new_block->name);
4074 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
4075 gfc_global_used(s, NULL);
4078 s->type = GSYM_BLOCK_DATA;
4079 s->where = gfc_current_locus;
4084 st = parse_spec (ST_NONE);
4086 while (st != ST_END_BLOCK_DATA)
4088 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
4089 gfc_ascii_statement (st));
4090 reject_statement ();
4091 st = next_statement ();
4096 /* Parse a module subprogram. */
4104 s = gfc_get_gsymbol (gfc_new_block->name);
4105 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
4106 gfc_global_used(s, NULL);
4109 s->type = GSYM_MODULE;
4110 s->where = gfc_current_locus;
4114 st = parse_spec (ST_NONE);
4123 parse_contained (1);
4127 accept_statement (st);
4131 gfc_error ("Unexpected %s statement in MODULE at %C",
4132 gfc_ascii_statement (st));
4134 reject_statement ();
4135 st = next_statement ();
4139 s->ns = gfc_current_ns;
4143 /* Add a procedure name to the global symbol table. */
4146 add_global_procedure (int sub)
4150 s = gfc_get_gsymbol(gfc_new_block->name);
4153 || (s->type != GSYM_UNKNOWN
4154 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
4155 gfc_global_used(s, NULL);
4158 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4159 s->where = gfc_current_locus;
4161 s->ns = gfc_current_ns;
4166 /* Add a program to the global symbol table. */
4169 add_global_program (void)
4173 if (gfc_new_block == NULL)
4175 s = gfc_get_gsymbol (gfc_new_block->name);
4177 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
4178 gfc_global_used(s, NULL);
4181 s->type = GSYM_PROGRAM;
4182 s->where = gfc_current_locus;
4184 s->ns = gfc_current_ns;
4189 /* Resolve all the program units when whole file scope option
4192 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
4194 gfc_free_dt_list ();
4195 gfc_current_ns = gfc_global_ns_list;
4196 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4198 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4199 gfc_resolve (gfc_current_ns);
4200 gfc_current_ns->derived_types = gfc_derived_types;
4201 gfc_derived_types = NULL;
4207 clean_up_modules (gfc_gsymbol *gsym)
4212 clean_up_modules (gsym->left);
4213 clean_up_modules (gsym->right);
4215 if (gsym->type != GSYM_MODULE || !gsym->ns)
4218 gfc_current_ns = gsym->ns;
4219 gfc_derived_types = gfc_current_ns->derived_types;
4226 /* Translate all the program units when whole file scope option
4227 is active. This could be in a different order to resolution if
4228 there are forward references in the file. */
4230 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
4234 gfc_current_ns = gfc_global_ns_list;
4235 gfc_get_errors (NULL, &errors);
4237 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4239 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4240 gfc_derived_types = gfc_current_ns->derived_types;
4241 gfc_generate_code (gfc_current_ns);
4242 gfc_current_ns->translated = 1;
4245 /* Clean up all the namespaces after translation. */
4246 gfc_current_ns = gfc_global_ns_list;
4247 for (;gfc_current_ns;)
4249 gfc_namespace *ns = gfc_current_ns->sibling;
4250 gfc_derived_types = gfc_current_ns->derived_types;
4252 gfc_current_ns = ns;
4255 clean_up_modules (gfc_gsym_root);
4259 /* Top level parser. */
4262 gfc_parse_file (void)
4264 int seen_program, errors_before, errors;
4265 gfc_state_data top, s;
4268 gfc_namespace *next;
4270 gfc_start_source_files ();
4272 top.state = COMP_NONE;
4274 top.previous = NULL;
4275 top.head = top.tail = NULL;
4276 top.do_variable = NULL;
4278 gfc_state_stack = ⊤
4280 gfc_clear_new_st ();
4282 gfc_statement_label = NULL;
4284 if (setjmp (eof_buf))
4285 return FAILURE; /* Come here on unexpected EOF */
4287 /* Prepare the global namespace that will contain the
4289 gfc_global_ns_list = next = NULL;
4293 /* Exit early for empty files. */
4299 st = next_statement ();
4308 goto duplicate_main;
4310 prog_locus = gfc_current_locus;
4312 push_state (&s, COMP_PROGRAM, gfc_new_block);
4313 main_program_symbol(gfc_current_ns, gfc_new_block->name);
4314 accept_statement (st);
4315 add_global_program ();
4316 parse_progunit (ST_NONE);
4317 if (gfc_option.flag_whole_file)
4322 add_global_procedure (1);
4323 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
4324 accept_statement (st);
4325 parse_progunit (ST_NONE);
4326 if (gfc_option.flag_whole_file)
4331 add_global_procedure (0);
4332 push_state (&s, COMP_FUNCTION, gfc_new_block);
4333 accept_statement (st);
4334 parse_progunit (ST_NONE);
4335 if (gfc_option.flag_whole_file)
4340 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
4341 accept_statement (st);
4342 parse_block_data ();
4346 push_state (&s, COMP_MODULE, gfc_new_block);
4347 accept_statement (st);
4349 gfc_get_errors (NULL, &errors_before);
4353 /* Anything else starts a nameless main program block. */
4356 goto duplicate_main;
4358 prog_locus = gfc_current_locus;
4360 push_state (&s, COMP_PROGRAM, gfc_new_block);
4361 main_program_symbol (gfc_current_ns, "MAIN__");
4362 parse_progunit (st);
4363 if (gfc_option.flag_whole_file)
4368 /* Handle the non-program units. */
4369 gfc_current_ns->code = s.head;
4371 gfc_resolve (gfc_current_ns);
4373 /* Dump the parse tree if requested. */
4374 if (gfc_option.dump_parse_tree)
4375 gfc_dump_parse_tree (gfc_current_ns, stdout);
4377 gfc_get_errors (NULL, &errors);
4378 if (s.state == COMP_MODULE)
4380 gfc_dump_module (s.sym->name, errors_before == errors);
4382 gfc_generate_module_code (gfc_current_ns);
4384 if (!gfc_option.flag_whole_file)
4388 gfc_current_ns->derived_types = gfc_derived_types;
4389 gfc_derived_types = NULL;
4390 gfc_current_ns = NULL;
4396 gfc_generate_code (gfc_current_ns);
4404 /* The main program and non-contained procedures are put
4405 in the global namespace list, so that they can be processed
4406 later and all their interfaces resolved. */
4407 gfc_current_ns->code = s.head;
4409 next->sibling = gfc_current_ns;
4411 gfc_global_ns_list = gfc_current_ns;
4413 next = gfc_current_ns;
4420 if (!gfc_option.flag_whole_file)
4423 /* Do the resolution. */
4424 resolve_all_program_units (gfc_global_ns_list);
4426 /* Do the parse tree dump. */
4428 = gfc_option.dump_parse_tree ? gfc_global_ns_list : NULL;
4430 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4432 gfc_dump_parse_tree (gfc_current_ns, stdout);
4433 fputs ("------------------------------------------\n\n", stdout);
4436 /* Do the translation. */
4437 translate_all_program_units (gfc_global_ns_list);
4441 gfc_end_source_files ();
4445 /* If we see a duplicate main program, shut down. If the second
4446 instance is an implied main program, i.e. data decls or executable
4447 statements, we're in for lots of errors. */
4448 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
4449 reject_statement ();