2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
31 /* Current statement label. Zero means no statement label. Because new_st
32 can get wiped during statement matching, we have to keep it separate. */
34 gfc_st_label *gfc_statement_label;
36 static locus label_locus;
37 static jmp_buf eof_buf;
39 gfc_state_data *gfc_state_stack;
41 /* TODO: Re-order functions to kill these forward decls. */
42 static void check_statement_label (gfc_statement);
43 static void undo_new_statement (void);
44 static void reject_statement (void);
47 /* A sort of half-matching function. We try to match the word on the
48 input with the passed string. If this succeeds, we call the
49 keyword-dependent matching function that will match the rest of the
50 statement. For single keywords, the matching subroutine is
54 match_word (const char *str, match (*subr) (void), locus *old_locus)
69 gfc_current_locus = *old_locus;
77 /* Figure out what the next statement is, (mostly) regardless of
78 proper ordering. The do...while(0) is there to prevent if/else
81 #define match(keyword, subr, st) \
83 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
86 undo_new_statement (); \
90 /* This is a specialist version of decode_statement that is used
91 for the specification statements in a function, whose
92 characteristics are deferred into the specification statements.
93 eg.: INTEGER (king = mykind) foo ()
94 USE mymodule, ONLY mykind.....
95 The KIND parameter needs a return after USE or IMPORT, whereas
96 derived type declarations can occur anywhere, up the executable
97 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
98 out of the correct kind of specification statements. */
100 decode_specification_statement (void)
106 if (gfc_match_eos () == MATCH_YES)
109 old_locus = gfc_current_locus;
111 match ("import", gfc_match_import, ST_IMPORT);
112 match ("use", gfc_match_use, ST_USE);
114 if (gfc_current_block ()->result->ts.type != BT_DERIVED)
117 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
118 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
119 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
121 /* General statement matching: Instead of testing every possible
122 statement, we eliminate most possibilities by peeking at the
125 c = gfc_peek_ascii_char ();
130 match ("abstract% interface", gfc_match_abstract_interface,
132 match ("allocatable", gfc_match_asynchronous, ST_ATTR_DECL);
133 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
137 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
141 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
142 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
146 match ("data", gfc_match_data, ST_DATA);
147 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
151 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
152 match ("entry% ", gfc_match_entry, ST_ENTRY);
153 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
154 match ("external", gfc_match_external, ST_ATTR_DECL);
158 match ("format", gfc_match_format, ST_FORMAT);
165 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
166 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
167 match ("interface", gfc_match_interface, ST_INTERFACE);
168 match ("intent", gfc_match_intent, ST_ATTR_DECL);
169 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
176 match ("namelist", gfc_match_namelist, ST_NAMELIST);
180 match ("optional", gfc_match_optional, ST_ATTR_DECL);
184 match ("parameter", gfc_match_parameter, ST_PARAMETER);
185 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
186 if (gfc_match_private (&st) == MATCH_YES)
188 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
189 if (gfc_match_public (&st) == MATCH_YES)
191 match ("protected", gfc_match_protected, ST_ATTR_DECL);
198 match ("save", gfc_match_save, ST_ATTR_DECL);
202 match ("target", gfc_match_target, ST_ATTR_DECL);
203 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
210 match ("value", gfc_match_value, ST_ATTR_DECL);
211 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
218 /* This is not a specification statement. See if any of the matchers
219 has stored an error message of some sort. */
223 gfc_buffer_error (0);
224 gfc_current_locus = old_locus;
226 return ST_GET_FCN_CHARACTERISTICS;
230 /* This is the primary 'decode_statement'. */
232 decode_statement (void)
243 gfc_clear_error (); /* Clear any pending errors. */
244 gfc_clear_warning (); /* Clear any pending warnings. */
246 gfc_matching_function = false;
248 if (gfc_match_eos () == MATCH_YES)
251 if (gfc_current_state () == COMP_FUNCTION
252 && gfc_current_block ()->result->ts.kind == -1)
253 return decode_specification_statement ();
255 old_locus = gfc_current_locus;
257 /* Try matching a data declaration or function declaration. The
258 input "REALFUNCTIONA(N)" can mean several things in different
259 contexts, so it (and its relatives) get special treatment. */
261 if (gfc_current_state () == COMP_NONE
262 || gfc_current_state () == COMP_INTERFACE
263 || gfc_current_state () == COMP_CONTAINS)
265 gfc_matching_function = true;
266 m = gfc_match_function_decl ();
269 else if (m == MATCH_ERROR)
273 gfc_current_locus = old_locus;
275 gfc_matching_function = false;
278 /* Match statements whose error messages are meant to be overwritten
279 by something better. */
281 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
282 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
283 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
285 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
286 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
288 /* Try to match a subroutine statement, which has the same optional
289 prefixes that functions can have. */
291 if (gfc_match_subroutine () == MATCH_YES)
292 return ST_SUBROUTINE;
294 gfc_current_locus = old_locus;
296 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
297 statements, which might begin with a block label. The match functions for
298 these statements are unusual in that their keyword is not seen before
299 the matcher is called. */
301 if (gfc_match_if (&st) == MATCH_YES)
304 gfc_current_locus = old_locus;
306 if (gfc_match_where (&st) == MATCH_YES)
309 gfc_current_locus = old_locus;
311 if (gfc_match_forall (&st) == MATCH_YES)
314 gfc_current_locus = old_locus;
316 match (NULL, gfc_match_do, ST_DO);
317 match (NULL, gfc_match_block, ST_BLOCK);
318 match (NULL, gfc_match_associate, ST_ASSOCIATE);
319 match (NULL, gfc_match_critical, ST_CRITICAL);
320 match (NULL, gfc_match_select, ST_SELECT_CASE);
321 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
323 /* General statement matching: Instead of testing every possible
324 statement, we eliminate most possibilities by peeking at the
327 c = gfc_peek_ascii_char ();
332 match ("abstract% interface", gfc_match_abstract_interface,
334 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
335 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
336 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
337 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
341 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
342 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
343 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
347 match ("call", gfc_match_call, ST_CALL);
348 match ("close", gfc_match_close, ST_CLOSE);
349 match ("continue", gfc_match_continue, ST_CONTINUE);
350 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
351 match ("cycle", gfc_match_cycle, ST_CYCLE);
352 match ("case", gfc_match_case, ST_CASE);
353 match ("common", gfc_match_common, ST_COMMON);
354 match ("contains", gfc_match_eos, ST_CONTAINS);
355 match ("class", gfc_match_class_is, ST_CLASS_IS);
356 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
360 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
361 match ("data", gfc_match_data, ST_DATA);
362 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
366 match ("end file", gfc_match_endfile, ST_END_FILE);
367 match ("exit", gfc_match_exit, ST_EXIT);
368 match ("else", gfc_match_else, ST_ELSE);
369 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
370 match ("else if", gfc_match_elseif, ST_ELSEIF);
371 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
372 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
374 if (gfc_match_end (&st) == MATCH_YES)
377 match ("entry% ", gfc_match_entry, ST_ENTRY);
378 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
379 match ("external", gfc_match_external, ST_ATTR_DECL);
383 match ("final", gfc_match_final_decl, ST_FINAL);
384 match ("flush", gfc_match_flush, ST_FLUSH);
385 match ("format", gfc_match_format, ST_FORMAT);
389 match ("generic", gfc_match_generic, ST_GENERIC);
390 match ("go to", gfc_match_goto, ST_GOTO);
394 match ("inquire", gfc_match_inquire, ST_INQUIRE);
395 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
396 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
397 match ("import", gfc_match_import, ST_IMPORT);
398 match ("interface", gfc_match_interface, ST_INTERFACE);
399 match ("intent", gfc_match_intent, ST_ATTR_DECL);
400 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
404 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
405 match ("module", gfc_match_module, ST_MODULE);
409 match ("nullify", gfc_match_nullify, ST_NULLIFY);
410 match ("namelist", gfc_match_namelist, ST_NAMELIST);
414 match ("open", gfc_match_open, ST_OPEN);
415 match ("optional", gfc_match_optional, ST_ATTR_DECL);
419 match ("print", gfc_match_print, ST_WRITE);
420 match ("parameter", gfc_match_parameter, ST_PARAMETER);
421 match ("pause", gfc_match_pause, ST_PAUSE);
422 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
423 if (gfc_match_private (&st) == MATCH_YES)
425 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
426 match ("program", gfc_match_program, ST_PROGRAM);
427 if (gfc_match_public (&st) == MATCH_YES)
429 match ("protected", gfc_match_protected, ST_ATTR_DECL);
433 match ("read", gfc_match_read, ST_READ);
434 match ("return", gfc_match_return, ST_RETURN);
435 match ("rewind", gfc_match_rewind, ST_REWIND);
439 match ("sequence", gfc_match_eos, ST_SEQUENCE);
440 match ("stop", gfc_match_stop, ST_STOP);
441 match ("save", gfc_match_save, ST_ATTR_DECL);
442 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
443 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
444 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
448 match ("target", gfc_match_target, ST_ATTR_DECL);
449 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
450 match ("type is", gfc_match_type_is, ST_TYPE_IS);
454 match ("use", gfc_match_use, ST_USE);
458 match ("value", gfc_match_value, ST_ATTR_DECL);
459 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
463 match ("wait", gfc_match_wait, ST_WAIT);
464 match ("write", gfc_match_write, ST_WRITE);
468 /* All else has failed, so give up. See if any of the matchers has
469 stored an error message of some sort. */
471 if (gfc_error_check () == 0)
472 gfc_error_now ("Unclassifiable statement at %C");
476 gfc_error_recovery ();
482 decode_omp_directive (void)
491 gfc_clear_error (); /* Clear any pending errors. */
492 gfc_clear_warning (); /* Clear any pending warnings. */
496 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
497 "or ELEMENTAL procedures");
498 gfc_error_recovery ();
502 old_locus = gfc_current_locus;
504 /* General OpenMP directive matching: Instead of testing every possible
505 statement, we eliminate most possibilities by peeking at the
508 c = gfc_peek_ascii_char ();
513 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
516 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
519 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
522 match ("do", gfc_match_omp_do, ST_OMP_DO);
525 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
526 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
527 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
528 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
529 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
530 match ("end parallel sections", gfc_match_omp_eos,
531 ST_OMP_END_PARALLEL_SECTIONS);
532 match ("end parallel workshare", gfc_match_omp_eos,
533 ST_OMP_END_PARALLEL_WORKSHARE);
534 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
535 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
536 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
537 match ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
538 match ("end workshare", gfc_match_omp_end_nowait,
539 ST_OMP_END_WORKSHARE);
542 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
545 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
548 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
551 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
552 match ("parallel sections", gfc_match_omp_parallel_sections,
553 ST_OMP_PARALLEL_SECTIONS);
554 match ("parallel workshare", gfc_match_omp_parallel_workshare,
555 ST_OMP_PARALLEL_WORKSHARE);
556 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
559 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
560 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
561 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
564 match ("task", gfc_match_omp_task, ST_OMP_TASK);
565 match ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
566 match ("threadprivate", gfc_match_omp_threadprivate,
567 ST_OMP_THREADPRIVATE);
569 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
573 /* All else has failed, so give up. See if any of the matchers has
574 stored an error message of some sort. */
576 if (gfc_error_check () == 0)
577 gfc_error_now ("Unclassifiable OpenMP directive at %C");
581 gfc_error_recovery ();
587 decode_gcc_attribute (void)
595 gfc_clear_error (); /* Clear any pending errors. */
596 gfc_clear_warning (); /* Clear any pending warnings. */
597 old_locus = gfc_current_locus;
599 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
601 /* All else has failed, so give up. See if any of the matchers has
602 stored an error message of some sort. */
604 if (gfc_error_check () == 0)
605 gfc_error_now ("Unclassifiable GCC directive at %C");
609 gfc_error_recovery ();
617 /* Get the next statement in free form source. */
626 at_bol = gfc_at_bol ();
627 gfc_gobble_whitespace ();
629 c = gfc_peek_ascii_char ();
635 /* Found a statement label? */
636 m = gfc_match_st_label (&gfc_statement_label);
638 d = gfc_peek_ascii_char ();
639 if (m != MATCH_YES || !gfc_is_whitespace (d))
641 gfc_match_small_literal_int (&i, &cnt);
644 gfc_error_now ("Too many digits in statement label at %C");
647 gfc_error_now ("Zero is not a valid statement label at %C");
650 c = gfc_next_ascii_char ();
653 if (!gfc_is_whitespace (c))
654 gfc_error_now ("Non-numeric character in statement label at %C");
660 label_locus = gfc_current_locus;
662 gfc_gobble_whitespace ();
664 if (at_bol && gfc_peek_ascii_char () == ';')
666 gfc_error_now ("Semicolon at %C needs to be preceded by "
668 gfc_next_ascii_char (); /* Eat up the semicolon. */
672 if (gfc_match_eos () == MATCH_YES)
674 gfc_warning_now ("Ignoring statement label in empty statement "
675 "at %L", &label_locus);
676 gfc_free_st_label (gfc_statement_label);
677 gfc_statement_label = NULL;
684 /* Comments have already been skipped by the time we get here,
685 except for GCC attributes and OpenMP directives. */
687 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
688 c = gfc_peek_ascii_char ();
694 c = gfc_next_ascii_char ();
695 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
696 gcc_assert (c == "gcc$"[i]);
698 gfc_gobble_whitespace ();
699 return decode_gcc_attribute ();
702 else if (c == '$' && gfc_option.flag_openmp)
706 c = gfc_next_ascii_char ();
707 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
708 gcc_assert (c == "$omp"[i]);
710 gcc_assert (c == ' ' || c == '\t');
711 gfc_gobble_whitespace ();
712 return decode_omp_directive ();
718 if (at_bol && c == ';')
720 if (!(gfc_option.allow_std & GFC_STD_F2008))
721 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
723 gfc_next_ascii_char (); /* Eat up the semicolon. */
727 return decode_statement ();
731 /* Get the next statement in fixed-form source. */
736 int label, digit_flag, i;
741 return decode_statement ();
743 /* Skip past the current label field, parsing a statement label if
744 one is there. This is a weird number parser, since the number is
745 contained within five columns and can have any kind of embedded
746 spaces. We also check for characters that make the rest of the
752 for (i = 0; i < 5; i++)
754 c = gfc_next_char_literal (0);
771 label = label * 10 + ((unsigned char) c - '0');
772 label_locus = gfc_current_locus;
776 /* Comments have already been skipped by the time we get
777 here, except for GCC attributes and OpenMP directives. */
780 c = gfc_next_char_literal (0);
782 if (TOLOWER (c) == 'g')
784 for (i = 0; i < 4; i++, c = gfc_next_char_literal (0))
785 gcc_assert (TOLOWER (c) == "gcc$"[i]);
787 return decode_gcc_attribute ();
789 else if (c == '$' && gfc_option.flag_openmp)
791 for (i = 0; i < 4; i++, c = gfc_next_char_literal (0))
792 gcc_assert ((char) gfc_wide_tolower (c) == "$omp"[i]);
794 if (c != ' ' && c != '0')
796 gfc_buffer_error (0);
797 gfc_error ("Bad continuation line at %C");
801 return decode_omp_directive ();
805 /* Comments have already been skipped by the time we get
806 here so don't bother checking for them. */
809 gfc_buffer_error (0);
810 gfc_error ("Non-numeric character in statement label at %C");
818 gfc_warning_now ("Zero is not a valid statement label at %C");
821 /* We've found a valid statement label. */
822 gfc_statement_label = gfc_get_st_label (label);
826 /* Since this line starts a statement, it cannot be a continuation
827 of a previous statement. If we see something here besides a
828 space or zero, it must be a bad continuation line. */
830 c = gfc_next_char_literal (0);
834 if (c != ' ' && c != '0')
836 gfc_buffer_error (0);
837 gfc_error ("Bad continuation line at %C");
841 /* Now that we've taken care of the statement label columns, we have
842 to make sure that the first nonblank character is not a '!'. If
843 it is, the rest of the line is a comment. */
847 loc = gfc_current_locus;
848 c = gfc_next_char_literal (0);
850 while (gfc_is_whitespace (c));
854 gfc_current_locus = loc;
859 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
860 else if (!(gfc_option.allow_std & GFC_STD_F2008))
861 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
866 if (gfc_match_eos () == MATCH_YES)
869 /* At this point, we've got a nonblank statement to parse. */
870 return decode_statement ();
874 gfc_warning_now ("Ignoring statement label in empty statement at %L",
877 gfc_current_locus.lb->truncated = 0;
883 /* Return the next non-ST_NONE statement to the caller. We also worry
884 about including files and the ends of include files at this stage. */
887 next_statement (void)
892 gfc_new_block = NULL;
894 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
897 gfc_statement_label = NULL;
898 gfc_buffer_error (1);
903 gfc_skip_comments ();
911 if (gfc_define_undef_line ())
914 old_locus = gfc_current_locus;
916 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
922 gfc_buffer_error (0);
924 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
926 gfc_free_st_label (gfc_statement_label);
927 gfc_statement_label = NULL;
928 gfc_current_locus = old_locus;
932 check_statement_label (st);
938 /****************************** Parser ***********************************/
940 /* The parser subroutines are of type 'try' that fail if the file ends
943 /* Macros that expand to case-labels for various classes of
944 statements. Start with executable statements that directly do
947 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
948 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
949 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
950 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
951 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
952 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
953 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
954 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
955 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_ERROR_STOP: \
956 case ST_SYNC_ALL: case ST_SYNC_IMAGES: case ST_SYNC_MEMORY
958 /* Statements that mark other executable statements. */
960 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
961 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
962 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
963 case ST_OMP_PARALLEL: \
964 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
965 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
966 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
967 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
968 case ST_OMP_TASK: case ST_CRITICAL
970 /* Declaration statements */
972 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
973 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
974 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
977 /* Block end statements. Errors associated with interchanging these
978 are detected in gfc_match_end(). */
980 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
981 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
982 case ST_END_BLOCK: case ST_END_ASSOCIATE
985 /* Push a new state onto the stack. */
988 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
990 p->state = new_state;
991 p->previous = gfc_state_stack;
993 p->head = p->tail = NULL;
994 p->do_variable = NULL;
999 /* Pop the current state. */
1003 gfc_state_stack = gfc_state_stack->previous;
1007 /* Try to find the given state in the state stack. */
1010 gfc_find_state (gfc_compile_state state)
1014 for (p = gfc_state_stack; p; p = p->previous)
1015 if (p->state == state)
1018 return (p == NULL) ? FAILURE : SUCCESS;
1022 /* Starts a new level in the statement list. */
1025 new_level (gfc_code *q)
1029 p = q->block = gfc_get_code ();
1031 gfc_state_stack->head = gfc_state_stack->tail = p;
1037 /* Add the current new_st code structure and adds it to the current
1038 program unit. As a side-effect, it zeroes the new_st. */
1041 add_statement (void)
1045 p = gfc_get_code ();
1048 p->loc = gfc_current_locus;
1050 if (gfc_state_stack->head == NULL)
1051 gfc_state_stack->head = p;
1053 gfc_state_stack->tail->next = p;
1055 while (p->next != NULL)
1058 gfc_state_stack->tail = p;
1060 gfc_clear_new_st ();
1066 /* Frees everything associated with the current statement. */
1069 undo_new_statement (void)
1071 gfc_free_statements (new_st.block);
1072 gfc_free_statements (new_st.next);
1073 gfc_free_statement (&new_st);
1074 gfc_clear_new_st ();
1078 /* If the current statement has a statement label, make sure that it
1079 is allowed to, or should have one. */
1082 check_statement_label (gfc_statement st)
1086 if (gfc_statement_label == NULL)
1088 if (st == ST_FORMAT)
1089 gfc_error ("FORMAT statement at %L does not have a statement label",
1096 case ST_END_PROGRAM:
1097 case ST_END_FUNCTION:
1098 case ST_END_SUBROUTINE:
1102 case ST_END_CRITICAL:
1105 type = ST_LABEL_TARGET;
1109 type = ST_LABEL_FORMAT;
1112 /* Statement labels are not restricted from appearing on a
1113 particular line. However, there are plenty of situations
1114 where the resulting label can't be referenced. */
1117 type = ST_LABEL_BAD_TARGET;
1121 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1123 new_st.here = gfc_statement_label;
1127 /* Figures out what the enclosing program unit is. This will be a
1128 function, subroutine, program, block data or module. */
1131 gfc_enclosing_unit (gfc_compile_state * result)
1135 for (p = gfc_state_stack; p; p = p->previous)
1136 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1137 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
1138 || p->state == COMP_PROGRAM)
1147 *result = COMP_PROGRAM;
1152 /* Translate a statement enum to a string. */
1155 gfc_ascii_statement (gfc_statement st)
1161 case ST_ARITHMETIC_IF:
1162 p = _("arithmetic IF");
1171 p = _("attribute declaration");
1207 p = _("data declaration");
1215 case ST_DERIVED_DECL:
1216 p = _("derived type declaration");
1230 case ST_END_ASSOCIATE:
1231 p = "END ASSOCIATE";
1236 case ST_END_BLOCK_DATA:
1237 p = "END BLOCK DATA";
1239 case ST_END_CRITICAL:
1251 case ST_END_FUNCTION:
1257 case ST_END_INTERFACE:
1258 p = "END INTERFACE";
1263 case ST_END_PROGRAM:
1269 case ST_END_SUBROUTINE:
1270 p = "END SUBROUTINE";
1281 case ST_EQUIVALENCE:
1293 case ST_FORALL_BLOCK: /* Fall through */
1315 case ST_IMPLICIT_NONE:
1316 p = "IMPLICIT NONE";
1318 case ST_IMPLIED_ENDDO:
1319 p = _("implied END DO");
1345 case ST_MODULE_PROC:
1346 p = "MODULE PROCEDURE";
1378 case ST_SYNC_IMAGES:
1381 case ST_SYNC_MEMORY:
1393 case ST_WHERE_BLOCK: /* Fall through */
1404 p = _("assignment");
1406 case ST_POINTER_ASSIGNMENT:
1407 p = _("pointer assignment");
1409 case ST_SELECT_CASE:
1412 case ST_SELECT_TYPE:
1427 case ST_STATEMENT_FUNCTION:
1428 p = "STATEMENT FUNCTION";
1430 case ST_LABEL_ASSIGNMENT:
1431 p = "LABEL ASSIGNMENT";
1434 p = "ENUM DEFINITION";
1437 p = "ENUMERATOR DEFINITION";
1445 case ST_OMP_BARRIER:
1446 p = "!$OMP BARRIER";
1448 case ST_OMP_CRITICAL:
1449 p = "!$OMP CRITICAL";
1454 case ST_OMP_END_CRITICAL:
1455 p = "!$OMP END CRITICAL";
1460 case ST_OMP_END_MASTER:
1461 p = "!$OMP END MASTER";
1463 case ST_OMP_END_ORDERED:
1464 p = "!$OMP END ORDERED";
1466 case ST_OMP_END_PARALLEL:
1467 p = "!$OMP END PARALLEL";
1469 case ST_OMP_END_PARALLEL_DO:
1470 p = "!$OMP END PARALLEL DO";
1472 case ST_OMP_END_PARALLEL_SECTIONS:
1473 p = "!$OMP END PARALLEL SECTIONS";
1475 case ST_OMP_END_PARALLEL_WORKSHARE:
1476 p = "!$OMP END PARALLEL WORKSHARE";
1478 case ST_OMP_END_SECTIONS:
1479 p = "!$OMP END SECTIONS";
1481 case ST_OMP_END_SINGLE:
1482 p = "!$OMP END SINGLE";
1484 case ST_OMP_END_TASK:
1485 p = "!$OMP END TASK";
1487 case ST_OMP_END_WORKSHARE:
1488 p = "!$OMP END WORKSHARE";
1496 case ST_OMP_ORDERED:
1497 p = "!$OMP ORDERED";
1499 case ST_OMP_PARALLEL:
1500 p = "!$OMP PARALLEL";
1502 case ST_OMP_PARALLEL_DO:
1503 p = "!$OMP PARALLEL DO";
1505 case ST_OMP_PARALLEL_SECTIONS:
1506 p = "!$OMP PARALLEL SECTIONS";
1508 case ST_OMP_PARALLEL_WORKSHARE:
1509 p = "!$OMP PARALLEL WORKSHARE";
1511 case ST_OMP_SECTIONS:
1512 p = "!$OMP SECTIONS";
1514 case ST_OMP_SECTION:
1515 p = "!$OMP SECTION";
1523 case ST_OMP_TASKWAIT:
1524 p = "!$OMP TASKWAIT";
1526 case ST_OMP_THREADPRIVATE:
1527 p = "!$OMP THREADPRIVATE";
1529 case ST_OMP_WORKSHARE:
1530 p = "!$OMP WORKSHARE";
1533 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1540 /* Create a symbol for the main program and assign it to ns->proc_name. */
1543 main_program_symbol (gfc_namespace *ns, const char *name)
1545 gfc_symbol *main_program;
1546 symbol_attribute attr;
1548 gfc_get_symbol (name, ns, &main_program);
1549 gfc_clear_attr (&attr);
1550 attr.flavor = FL_PROGRAM;
1551 attr.proc = PROC_UNKNOWN;
1552 attr.subroutine = 1;
1553 attr.access = ACCESS_PUBLIC;
1554 attr.is_main_program = 1;
1555 main_program->attr = attr;
1556 main_program->declared_at = gfc_current_locus;
1557 ns->proc_name = main_program;
1558 gfc_commit_symbols ();
1562 /* Do whatever is necessary to accept the last statement. */
1565 accept_statement (gfc_statement st)
1573 case ST_IMPLICIT_NONE:
1574 gfc_set_implicit_none ();
1583 gfc_current_ns->proc_name = gfc_new_block;
1586 /* If the statement is the end of a block, lay down a special code
1587 that allows a branch to the end of the block from within the
1588 construct. IF and SELECT are treated differently from DO
1589 (where EXEC_NOP is added inside the loop) for two
1591 1. END DO has a meaning in the sense that after a GOTO to
1592 it, the loop counter must be increased.
1593 2. IF blocks and SELECT blocks can consist of multiple
1594 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
1595 Putting the label before the END IF would make the jump
1596 from, say, the ELSE IF block to the END IF illegal. */
1600 case ST_END_CRITICAL:
1601 if (gfc_statement_label != NULL)
1603 new_st.op = EXEC_END_BLOCK;
1608 /* The end-of-program unit statements do not get the special
1609 marker and require a statement of some sort if they are a
1612 case ST_END_PROGRAM:
1613 case ST_END_FUNCTION:
1614 case ST_END_SUBROUTINE:
1615 if (gfc_statement_label != NULL)
1617 new_st.op = EXEC_RETURN;
1622 new_st.op = EXEC_END_PROCEDURE;
1638 gfc_commit_symbols ();
1639 gfc_warning_check ();
1640 gfc_clear_new_st ();
1644 /* Undo anything tentative that has been built for the current
1648 reject_statement (void)
1650 /* Revert to the previous charlen chain. */
1651 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
1652 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
1654 gfc_new_block = NULL;
1655 gfc_undo_symbols ();
1656 gfc_clear_warning ();
1657 undo_new_statement ();
1661 /* Generic complaint about an out of order statement. We also do
1662 whatever is necessary to clean up. */
1665 unexpected_statement (gfc_statement st)
1667 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1669 reject_statement ();
1673 /* Given the next statement seen by the matcher, make sure that it is
1674 in proper order with the last. This subroutine is initialized by
1675 calling it with an argument of ST_NONE. If there is a problem, we
1676 issue an error and return FAILURE. Otherwise we return SUCCESS.
1678 Individual parsers need to verify that the statements seen are
1679 valid before calling here, i.e., ENTRY statements are not allowed in
1680 INTERFACE blocks. The following diagram is taken from the standard:
1682 +---------------------------------------+
1683 | program subroutine function module |
1684 +---------------------------------------+
1686 +---------------------------------------+
1688 +---------------------------------------+
1690 | +-----------+------------------+
1691 | | parameter | implicit |
1692 | +-----------+------------------+
1693 | format | | derived type |
1694 | entry | parameter | interface |
1695 | | data | specification |
1696 | | | statement func |
1697 | +-----------+------------------+
1698 | | data | executable |
1699 +--------+-----------+------------------+
1701 +---------------------------------------+
1702 | internal module/subprogram |
1703 +---------------------------------------+
1705 +---------------------------------------+
1714 ORDER_IMPLICIT_NONE,
1722 enum state_order state;
1723 gfc_statement last_statement;
1729 verify_st_order (st_state *p, gfc_statement st, bool silent)
1735 p->state = ORDER_START;
1739 if (p->state > ORDER_USE)
1741 p->state = ORDER_USE;
1745 if (p->state > ORDER_IMPORT)
1747 p->state = ORDER_IMPORT;
1750 case ST_IMPLICIT_NONE:
1751 if (p->state > ORDER_IMPLICIT_NONE)
1754 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1755 statement disqualifies a USE but not an IMPLICIT NONE.
1756 Duplicate IMPLICIT NONEs are caught when the implicit types
1759 p->state = ORDER_IMPLICIT_NONE;
1763 if (p->state > ORDER_IMPLICIT)
1765 p->state = ORDER_IMPLICIT;
1770 if (p->state < ORDER_IMPLICIT_NONE)
1771 p->state = ORDER_IMPLICIT_NONE;
1775 if (p->state >= ORDER_EXEC)
1777 if (p->state < ORDER_IMPLICIT)
1778 p->state = ORDER_IMPLICIT;
1782 if (p->state < ORDER_SPEC)
1783 p->state = ORDER_SPEC;
1788 case ST_DERIVED_DECL:
1790 if (p->state >= ORDER_EXEC)
1792 if (p->state < ORDER_SPEC)
1793 p->state = ORDER_SPEC;
1798 if (p->state < ORDER_EXEC)
1799 p->state = ORDER_EXEC;
1803 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1804 gfc_ascii_statement (st));
1807 /* All is well, record the statement in case we need it next time. */
1808 p->where = gfc_current_locus;
1809 p->last_statement = st;
1814 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1815 gfc_ascii_statement (st),
1816 gfc_ascii_statement (p->last_statement), &p->where);
1822 /* Handle an unexpected end of file. This is a show-stopper... */
1824 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1827 unexpected_eof (void)
1831 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1833 /* Memory cleanup. Move to "second to last". */
1834 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1837 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1840 longjmp (eof_buf, 1);
1844 /* Parse the CONTAINS section of a derived type definition. */
1846 gfc_access gfc_typebound_default_access;
1849 parse_derived_contains (void)
1852 bool seen_private = false;
1853 bool seen_comps = false;
1854 bool error_flag = false;
1857 gcc_assert (gfc_current_state () == COMP_DERIVED);
1858 gcc_assert (gfc_current_block ());
1860 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
1862 if (gfc_current_block ()->attr.sequence)
1863 gfc_error ("Derived-type '%s' with SEQUENCE must not have a CONTAINS"
1864 " section at %C", gfc_current_block ()->name);
1865 if (gfc_current_block ()->attr.is_bind_c)
1866 gfc_error ("Derived-type '%s' with BIND(C) must not have a CONTAINS"
1867 " section at %C", gfc_current_block ()->name);
1869 accept_statement (ST_CONTAINS);
1870 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
1872 gfc_typebound_default_access = ACCESS_PUBLIC;
1878 st = next_statement ();
1886 gfc_error ("Components in TYPE at %C must precede CONTAINS");
1891 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Type-bound"
1892 " procedure at %C") == FAILURE)
1895 accept_statement (ST_PROCEDURE);
1900 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: GENERIC binding"
1901 " at %C") == FAILURE)
1904 accept_statement (ST_GENERIC);
1909 if (gfc_notify_std (GFC_STD_F2003,
1910 "Fortran 2003: FINAL procedure declaration"
1911 " at %C") == FAILURE)
1914 accept_statement (ST_FINAL);
1922 && (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Derived type "
1923 "definition at %C with empty CONTAINS "
1924 "section") == FAILURE))
1927 /* ST_END_TYPE is accepted by parse_derived after return. */
1931 if (gfc_find_state (COMP_MODULE) == FAILURE)
1933 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1941 gfc_error ("PRIVATE statement at %C must precede procedure"
1949 gfc_error ("Duplicate PRIVATE statement at %C");
1953 accept_statement (ST_PRIVATE);
1954 gfc_typebound_default_access = ACCESS_PRIVATE;
1955 seen_private = true;
1959 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
1964 gfc_error ("Already inside a CONTAINS block at %C");
1969 unexpected_statement (st);
1975 gcc_assert (gfc_current_state () == COMP_DERIVED);
1981 /* Parse a derived type. */
1984 parse_derived (void)
1986 int compiling_type, seen_private, seen_sequence, seen_component;
1992 accept_statement (ST_DERIVED_DECL);
1993 push_state (&s, COMP_DERIVED, gfc_new_block);
1995 gfc_new_block->component_access = ACCESS_PUBLIC;
2002 while (compiling_type)
2004 st = next_statement ();
2012 accept_statement (st);
2017 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2024 if (!seen_component)
2025 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Derived type "
2026 "definition at %C without components");
2028 accept_statement (ST_END_TYPE);
2032 if (gfc_find_state (COMP_MODULE) == FAILURE)
2034 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2041 gfc_error ("PRIVATE statement at %C must precede "
2042 "structure components");
2047 gfc_error ("Duplicate PRIVATE statement at %C");
2049 s.sym->component_access = ACCESS_PRIVATE;
2051 accept_statement (ST_PRIVATE);
2058 gfc_error ("SEQUENCE statement at %C must precede "
2059 "structure components");
2063 if (gfc_current_block ()->attr.sequence)
2064 gfc_warning ("SEQUENCE attribute at %C already specified in "
2069 gfc_error ("Duplicate SEQUENCE statement at %C");
2073 gfc_add_sequence (&gfc_current_block ()->attr,
2074 gfc_current_block ()->name, NULL);
2078 gfc_notify_std (GFC_STD_F2003,
2079 "Fortran 2003: CONTAINS block in derived type"
2080 " definition at %C");
2082 accept_statement (ST_CONTAINS);
2083 parse_derived_contains ();
2087 unexpected_statement (st);
2092 /* need to verify that all fields of the derived type are
2093 * interoperable with C if the type is declared to be bind(c)
2095 sym = gfc_current_block ();
2096 for (c = sym->components; c; c = c->next)
2098 /* Look for allocatable components. */
2099 if (c->attr.allocatable
2100 || (c->ts.type == BT_CLASS && CLASS_DATA (c)->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 && CLASS_DATA (c)->attr.class_pointer)
2107 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2108 sym->attr.pointer_comp = 1;
2110 /* Look for procedure pointer components. */
2111 if (c->attr.proc_pointer
2112 || (c->ts.type == BT_DERIVED
2113 && c->ts.u.derived->attr.proc_pointer_comp))
2114 sym->attr.proc_pointer_comp = 1;
2116 /* Looking for coarray components. */
2117 if (c->attr.codimension
2118 || (c->attr.coarray_comp && !c->attr.pointer && !c->attr.allocatable))
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. */
2143 int seen_enumerator = 0;
2145 push_state (&s, COMP_ENUM, gfc_new_block);
2149 while (compiling_enum)
2151 st = next_statement ();
2159 seen_enumerator = 1;
2160 accept_statement (st);
2165 if (!seen_enumerator)
2166 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2167 accept_statement (st);
2171 gfc_free_enum_history ();
2172 unexpected_statement (st);
2180 /* Parse an interface. We must be able to deal with the possibility
2181 of recursive interfaces. The parse_spec() subroutine is mutually
2182 recursive with parse_interface(). */
2184 static gfc_statement parse_spec (gfc_statement);
2187 parse_interface (void)
2189 gfc_compile_state new_state = COMP_NONE, current_state;
2190 gfc_symbol *prog_unit, *sym;
2191 gfc_interface_info save;
2192 gfc_state_data s1, s2;
2196 accept_statement (ST_INTERFACE);
2198 current_interface.ns = gfc_current_ns;
2199 save = current_interface;
2201 sym = (current_interface.type == INTERFACE_GENERIC
2202 || current_interface.type == INTERFACE_USER_OP)
2203 ? gfc_new_block : NULL;
2205 push_state (&s1, COMP_INTERFACE, sym);
2206 current_state = COMP_NONE;
2209 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
2211 st = next_statement ();
2219 if (st == ST_SUBROUTINE)
2220 new_state = COMP_SUBROUTINE;
2221 else if (st == ST_FUNCTION)
2222 new_state = COMP_FUNCTION;
2223 if (gfc_new_block->attr.pointer)
2225 gfc_new_block->attr.pointer = 0;
2226 gfc_new_block->attr.proc_pointer = 1;
2228 if (gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
2229 gfc_new_block->formal, NULL) == FAILURE)
2231 reject_statement ();
2232 gfc_free_namespace (gfc_current_ns);
2238 case ST_MODULE_PROC: /* The module procedure matcher makes
2239 sure the context is correct. */
2240 accept_statement (st);
2241 gfc_free_namespace (gfc_current_ns);
2244 case ST_END_INTERFACE:
2245 gfc_free_namespace (gfc_current_ns);
2246 gfc_current_ns = current_interface.ns;
2250 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2251 gfc_ascii_statement (st));
2252 reject_statement ();
2253 gfc_free_namespace (gfc_current_ns);
2258 /* Make sure that a generic interface has only subroutines or
2259 functions and that the generic name has the right attribute. */
2260 if (current_interface.type == INTERFACE_GENERIC)
2262 if (current_state == COMP_NONE)
2264 if (new_state == COMP_FUNCTION && sym)
2265 gfc_add_function (&sym->attr, sym->name, NULL);
2266 else if (new_state == COMP_SUBROUTINE && sym)
2267 gfc_add_subroutine (&sym->attr, sym->name, NULL);
2269 current_state = new_state;
2273 if (new_state != current_state)
2275 if (new_state == COMP_SUBROUTINE)
2276 gfc_error ("SUBROUTINE at %C does not belong in a "
2277 "generic function interface");
2279 if (new_state == COMP_FUNCTION)
2280 gfc_error ("FUNCTION at %C does not belong in a "
2281 "generic subroutine interface");
2286 if (current_interface.type == INTERFACE_ABSTRACT)
2288 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
2289 if (gfc_is_intrinsic_typename (gfc_new_block->name))
2290 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
2291 "cannot be the same as an intrinsic type",
2292 gfc_new_block->name);
2295 push_state (&s2, new_state, gfc_new_block);
2296 accept_statement (st);
2297 prog_unit = gfc_new_block;
2298 prog_unit->formal_ns = gfc_current_ns;
2299 proc_locus = gfc_current_locus;
2302 /* Read data declaration statements. */
2303 st = parse_spec (ST_NONE);
2305 /* Since the interface block does not permit an IMPLICIT statement,
2306 the default type for the function or the result must be taken
2307 from the formal namespace. */
2308 if (new_state == COMP_FUNCTION)
2310 if (prog_unit->result == prog_unit
2311 && prog_unit->ts.type == BT_UNKNOWN)
2312 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
2313 else if (prog_unit->result != prog_unit
2314 && prog_unit->result->ts.type == BT_UNKNOWN)
2315 gfc_set_default_type (prog_unit->result, 1,
2316 prog_unit->formal_ns);
2319 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
2321 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2322 gfc_ascii_statement (st));
2323 reject_statement ();
2327 /* Add EXTERNAL attribute to function or subroutine. */
2328 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
2329 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
2331 current_interface = save;
2332 gfc_add_interface (prog_unit);
2335 if (current_interface.ns
2336 && current_interface.ns->proc_name
2337 && strcmp (current_interface.ns->proc_name->name,
2338 prog_unit->name) == 0)
2339 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2340 "enclosing procedure", prog_unit->name, &proc_locus);
2349 /* Associate function characteristics by going back to the function
2350 declaration and rematching the prefix. */
2353 match_deferred_characteristics (gfc_typespec * ts)
2356 match m = MATCH_ERROR;
2357 char name[GFC_MAX_SYMBOL_LEN + 1];
2359 loc = gfc_current_locus;
2361 gfc_current_locus = gfc_current_block ()->declared_at;
2364 gfc_buffer_error (1);
2365 m = gfc_match_prefix (ts);
2366 gfc_buffer_error (0);
2368 if (ts->type == BT_DERIVED)
2376 /* Only permit one go at the characteristic association. */
2380 /* Set the function locus correctly. If we have not found the
2381 function name, there is an error. */
2383 && gfc_match ("function% %n", name) == MATCH_YES
2384 && strcmp (name, gfc_current_block ()->name) == 0)
2386 gfc_current_block ()->declared_at = gfc_current_locus;
2387 gfc_commit_symbols ();
2392 gfc_current_locus =loc;
2397 /* Check specification-expressions in the function result of the currently
2398 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2399 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2400 scope are not yet parsed so this has to be delayed up to parse_spec. */
2403 check_function_result_typed (void)
2405 gfc_typespec* ts = &gfc_current_ns->proc_name->result->ts;
2407 gcc_assert (gfc_current_state () == COMP_FUNCTION);
2408 gcc_assert (ts->type != BT_UNKNOWN);
2410 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2411 /* TODO: Extend when KIND type parameters are implemented. */
2412 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length)
2413 gfc_expr_check_typed (ts->u.cl->length, gfc_current_ns, true);
2417 /* Parse a set of specification statements. Returns the statement
2418 that doesn't fit. */
2420 static gfc_statement
2421 parse_spec (gfc_statement st)
2424 bool function_result_typed = false;
2425 bool bad_characteristic = false;
2428 verify_st_order (&ss, ST_NONE, false);
2430 st = next_statement ();
2432 /* If we are not inside a function or don't have a result specified so far,
2433 do nothing special about it. */
2434 if (gfc_current_state () != COMP_FUNCTION)
2435 function_result_typed = true;
2438 gfc_symbol* proc = gfc_current_ns->proc_name;
2441 if (proc->result->ts.type == BT_UNKNOWN)
2442 function_result_typed = true;
2447 /* If we're inside a BLOCK construct, some statements are disallowed.
2448 Check this here. Attribute declaration statements like INTENT, OPTIONAL
2449 or VALUE are also disallowed, but they don't have a particular ST_*
2450 key so we have to check for them individually in their matcher routine. */
2451 if (gfc_current_state () == COMP_BLOCK)
2455 case ST_IMPLICIT_NONE:
2458 case ST_EQUIVALENCE:
2459 case ST_STATEMENT_FUNCTION:
2460 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
2461 gfc_ascii_statement (st));
2468 /* If we find a statement that can not be followed by an IMPLICIT statement
2469 (and thus we can expect to see none any further), type the function result
2470 if it has not yet been typed. Be careful not to give the END statement
2471 to verify_st_order! */
2472 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
2474 bool verify_now = false;
2476 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
2481 verify_st_order (&dummyss, ST_NONE, false);
2482 verify_st_order (&dummyss, st, false);
2484 if (verify_st_order (&dummyss, ST_IMPLICIT, true) == FAILURE)
2490 check_function_result_typed ();
2491 function_result_typed = true;
2500 case ST_IMPLICIT_NONE:
2502 if (!function_result_typed)
2504 check_function_result_typed ();
2505 function_result_typed = true;
2511 case ST_DATA: /* Not allowed in interfaces */
2512 if (gfc_current_state () == COMP_INTERFACE)
2522 case ST_DERIVED_DECL:
2525 if (verify_st_order (&ss, st, false) == FAILURE)
2527 reject_statement ();
2528 st = next_statement ();
2538 case ST_DERIVED_DECL:
2544 if (gfc_current_state () != COMP_MODULE)
2546 gfc_error ("%s statement must appear in a MODULE",
2547 gfc_ascii_statement (st));
2551 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
2553 gfc_error ("%s statement at %C follows another accessibility "
2554 "specification", gfc_ascii_statement (st));
2558 gfc_current_ns->default_access = (st == ST_PUBLIC)
2559 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2563 case ST_STATEMENT_FUNCTION:
2564 if (gfc_current_state () == COMP_MODULE)
2566 unexpected_statement (st);
2574 accept_statement (st);
2575 st = next_statement ();
2579 accept_statement (st);
2581 st = next_statement ();
2584 case ST_GET_FCN_CHARACTERISTICS:
2585 /* This statement triggers the association of a function's result
2587 ts = &gfc_current_block ()->result->ts;
2588 if (match_deferred_characteristics (ts) != MATCH_YES)
2589 bad_characteristic = true;
2591 st = next_statement ();
2598 /* If match_deferred_characteristics failed, then there is an error. */
2599 if (bad_characteristic)
2601 ts = &gfc_current_block ()->result->ts;
2602 if (ts->type != BT_DERIVED)
2603 gfc_error ("Bad kind expression for function '%s' at %L",
2604 gfc_current_block ()->name,
2605 &gfc_current_block ()->declared_at);
2607 gfc_error ("The type for function '%s' at %L is not accessible",
2608 gfc_current_block ()->name,
2609 &gfc_current_block ()->declared_at);
2611 gfc_current_block ()->ts.kind = 0;
2612 /* Keep the derived type; if it's bad, it will be discovered later. */
2613 if (!(ts->type == BT_DERIVED && ts->u.derived))
2614 ts->type = BT_UNKNOWN;
2621 /* Parse a WHERE block, (not a simple WHERE statement). */
2624 parse_where_block (void)
2626 int seen_empty_else;
2631 accept_statement (ST_WHERE_BLOCK);
2632 top = gfc_state_stack->tail;
2634 push_state (&s, COMP_WHERE, gfc_new_block);
2636 d = add_statement ();
2637 d->expr1 = top->expr1;
2643 seen_empty_else = 0;
2647 st = next_statement ();
2653 case ST_WHERE_BLOCK:
2654 parse_where_block ();
2659 accept_statement (st);
2663 if (seen_empty_else)
2665 gfc_error ("ELSEWHERE statement at %C follows previous "
2666 "unmasked ELSEWHERE");
2670 if (new_st.expr1 == NULL)
2671 seen_empty_else = 1;
2673 d = new_level (gfc_state_stack->head);
2675 d->expr1 = new_st.expr1;
2677 accept_statement (st);
2682 accept_statement (st);
2686 gfc_error ("Unexpected %s statement in WHERE block at %C",
2687 gfc_ascii_statement (st));
2688 reject_statement ();
2692 while (st != ST_END_WHERE);
2698 /* Parse a FORALL block (not a simple FORALL statement). */
2701 parse_forall_block (void)
2707 accept_statement (ST_FORALL_BLOCK);
2708 top = gfc_state_stack->tail;
2710 push_state (&s, COMP_FORALL, gfc_new_block);
2712 d = add_statement ();
2713 d->op = EXEC_FORALL;
2718 st = next_statement ();
2723 case ST_POINTER_ASSIGNMENT:
2726 accept_statement (st);
2729 case ST_WHERE_BLOCK:
2730 parse_where_block ();
2733 case ST_FORALL_BLOCK:
2734 parse_forall_block ();
2738 accept_statement (st);
2745 gfc_error ("Unexpected %s statement in FORALL block at %C",
2746 gfc_ascii_statement (st));
2748 reject_statement ();
2752 while (st != ST_END_FORALL);
2758 static gfc_statement parse_executable (gfc_statement);
2760 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2763 parse_if_block (void)
2772 accept_statement (ST_IF_BLOCK);
2774 top = gfc_state_stack->tail;
2775 push_state (&s, COMP_IF, gfc_new_block);
2777 new_st.op = EXEC_IF;
2778 d = add_statement ();
2780 d->expr1 = top->expr1;
2786 st = parse_executable (ST_NONE);
2796 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2797 "statement at %L", &else_locus);
2799 reject_statement ();
2803 d = new_level (gfc_state_stack->head);
2805 d->expr1 = new_st.expr1;
2807 accept_statement (st);
2814 gfc_error ("Duplicate ELSE statements at %L and %C",
2816 reject_statement ();
2821 else_locus = gfc_current_locus;
2823 d = new_level (gfc_state_stack->head);
2826 accept_statement (st);
2834 unexpected_statement (st);
2838 while (st != ST_ENDIF);
2841 accept_statement (st);
2845 /* Parse a SELECT block. */
2848 parse_select_block (void)
2854 accept_statement (ST_SELECT_CASE);
2856 cp = gfc_state_stack->tail;
2857 push_state (&s, COMP_SELECT, gfc_new_block);
2859 /* Make sure that the next statement is a CASE or END SELECT. */
2862 st = next_statement ();
2865 if (st == ST_END_SELECT)
2867 /* Empty SELECT CASE is OK. */
2868 accept_statement (st);
2875 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2878 reject_statement ();
2881 /* At this point, we're got a nonempty select block. */
2882 cp = new_level (cp);
2885 accept_statement (st);
2889 st = parse_executable (ST_NONE);
2896 cp = new_level (gfc_state_stack->head);
2898 gfc_clear_new_st ();
2900 accept_statement (st);
2906 /* Can't have an executable statement because of
2907 parse_executable(). */
2909 unexpected_statement (st);
2913 while (st != ST_END_SELECT);
2916 accept_statement (st);
2920 /* Pop the current selector from the SELECT TYPE stack. */
2923 select_type_pop (void)
2925 gfc_select_type_stack *old = select_type_stack;
2926 select_type_stack = old->prev;
2931 /* Parse a SELECT TYPE construct (F03:R821). */
2934 parse_select_type_block (void)
2940 accept_statement (ST_SELECT_TYPE);
2942 cp = gfc_state_stack->tail;
2943 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
2945 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
2949 st = next_statement ();
2952 if (st == ST_END_SELECT)
2953 /* Empty SELECT CASE is OK. */
2955 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
2958 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
2959 "following SELECT TYPE at %C");
2961 reject_statement ();
2964 /* At this point, we're got a nonempty select block. */
2965 cp = new_level (cp);
2968 accept_statement (st);
2972 st = parse_executable (ST_NONE);
2980 cp = new_level (gfc_state_stack->head);
2982 gfc_clear_new_st ();
2984 accept_statement (st);
2990 /* Can't have an executable statement because of
2991 parse_executable(). */
2993 unexpected_statement (st);
2997 while (st != ST_END_SELECT);
3001 accept_statement (st);
3002 gfc_current_ns = gfc_current_ns->parent;
3007 /* Given a symbol, make sure it is not an iteration variable for a DO
3008 statement. This subroutine is called when the symbol is seen in a
3009 context that causes it to become redefined. If the symbol is an
3010 iterator, we generate an error message and return nonzero. */
3013 gfc_check_do_variable (gfc_symtree *st)
3017 for (s=gfc_state_stack; s; s = s->previous)
3018 if (s->do_variable == st)
3020 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
3021 "loop beginning at %L", st->name, &s->head->loc);
3029 /* Checks to see if the current statement label closes an enddo.
3030 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3031 an error) if it incorrectly closes an ENDDO. */
3034 check_do_closure (void)
3038 if (gfc_statement_label == NULL)
3041 for (p = gfc_state_stack; p; p = p->previous)
3042 if (p->state == COMP_DO)
3046 return 0; /* No loops to close */
3048 if (p->ext.end_do_label == gfc_statement_label)
3050 if (p == gfc_state_stack)
3053 gfc_error ("End of nonblock DO statement at %C is within another block");
3057 /* At this point, the label doesn't terminate the innermost loop.
3058 Make sure it doesn't terminate another one. */
3059 for (; p; p = p->previous)
3060 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
3062 gfc_error ("End of nonblock DO statement at %C is interwoven "
3063 "with another DO loop");
3071 /* Parse a series of contained program units. */
3073 static void parse_progunit (gfc_statement);
3076 /* Parse a CRITICAL block. */
3079 parse_critical_block (void)
3085 s.ext.end_do_label = new_st.label1;
3087 accept_statement (ST_CRITICAL);
3088 top = gfc_state_stack->tail;
3090 push_state (&s, COMP_CRITICAL, gfc_new_block);
3092 d = add_statement ();
3093 d->op = EXEC_CRITICAL;
3098 st = parse_executable (ST_NONE);
3106 case ST_END_CRITICAL:
3107 if (s.ext.end_do_label != NULL
3108 && s.ext.end_do_label != gfc_statement_label)
3109 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3110 "match CRITIAL label");
3112 if (gfc_statement_label != NULL)
3114 new_st.op = EXEC_NOP;
3120 unexpected_statement (st);
3124 while (st != ST_END_CRITICAL);
3127 accept_statement (st);
3131 /* Set up the local namespace for a BLOCK construct. */
3134 gfc_build_block_ns (gfc_namespace *parent_ns)
3136 gfc_namespace* my_ns;
3138 my_ns = gfc_get_namespace (parent_ns, 1);
3139 my_ns->construct_entities = 1;
3141 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3142 code generation (so it must not be NULL).
3143 We set its recursive argument if our container procedure is recursive, so
3144 that local variables are accordingly placed on the stack when it
3145 will be necessary. */
3147 my_ns->proc_name = gfc_new_block;
3152 gfc_get_symbol ("block@", my_ns, &my_ns->proc_name);
3153 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
3154 my_ns->proc_name->name, NULL);
3155 gcc_assert (t == SUCCESS);
3158 if (parent_ns->proc_name)
3159 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
3165 /* Parse a BLOCK construct. */
3168 parse_block_construct (void)
3170 gfc_namespace* my_ns;
3173 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BLOCK construct at %C");
3175 my_ns = gfc_build_block_ns (gfc_current_ns);
3177 new_st.op = EXEC_BLOCK;
3178 new_st.ext.block.ns = my_ns;
3179 new_st.ext.block.assoc = NULL;
3180 accept_statement (ST_BLOCK);
3182 push_state (&s, COMP_BLOCK, my_ns->proc_name);
3183 gfc_current_ns = my_ns;
3185 parse_progunit (ST_NONE);
3187 gfc_current_ns = gfc_current_ns->parent;
3192 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3193 behind the scenes with compiler-generated variables. */
3196 parse_associate (void)
3198 gfc_namespace* my_ns;
3201 gfc_association_list* a;
3202 gfc_code* assignTail;
3204 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASSOCIATE construct at %C");
3206 my_ns = gfc_build_block_ns (gfc_current_ns);
3208 new_st.op = EXEC_BLOCK;
3209 new_st.ext.block.ns = my_ns;
3210 gcc_assert (new_st.ext.block.assoc);
3212 /* Add all associations to expressions as BLOCK variables, and create
3213 assignments to them giving their values. */
3214 gfc_current_ns = my_ns;
3216 for (a = new_st.ext.block.assoc; a; a = a->next)
3219 gfc_code* newAssign;
3221 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
3224 /* Note that in certain cases, the target-expression's type is not yet
3225 known and so we have to adapt the symbol's ts also during resolution
3227 a->st->n.sym->ts = a->target->ts;
3228 a->st->n.sym->attr.flavor = FL_VARIABLE;
3229 a->st->n.sym->assoc = a;
3230 gfc_set_sym_referenced (a->st->n.sym);
3232 /* Create the assignment to calculate the expression and set it. */
3233 newAssign = gfc_get_code ();
3234 newAssign->op = EXEC_ASSIGN;
3235 newAssign->loc = gfc_current_locus;
3236 newAssign->expr1 = gfc_get_variable_expr (a->st);
3237 newAssign->expr2 = a->target;
3241 assignTail->next = newAssign;
3243 gfc_current_ns->code = newAssign;
3244 assignTail = newAssign;
3248 gfc_error ("Association to variables is not yet supported at %C");
3251 gcc_assert (assignTail);
3253 accept_statement (ST_ASSOCIATE);
3254 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
3257 st = parse_executable (ST_NONE);
3264 accept_statement (st);
3265 assignTail->next = gfc_state_stack->head;
3269 unexpected_statement (st);
3273 gfc_current_ns = gfc_current_ns->parent;
3278 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
3279 handled inside of parse_executable(), because they aren't really
3283 parse_do_block (void)
3290 s.ext.end_do_label = new_st.label1;
3292 if (new_st.ext.iterator != NULL)
3293 stree = new_st.ext.iterator->var->symtree;
3297 accept_statement (ST_DO);
3299 top = gfc_state_stack->tail;
3300 push_state (&s, COMP_DO, gfc_new_block);
3302 s.do_variable = stree;
3304 top->block = new_level (top);
3305 top->block->op = EXEC_DO;
3308 st = parse_executable (ST_NONE);
3316 if (s.ext.end_do_label != NULL
3317 && s.ext.end_do_label != gfc_statement_label)
3318 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
3321 if (gfc_statement_label != NULL)
3323 new_st.op = EXEC_NOP;
3328 case ST_IMPLIED_ENDDO:
3329 /* If the do-stmt of this DO construct has a do-construct-name,
3330 the corresponding end-do must be an end-do-stmt (with a matching
3331 name, but in that case we must have seen ST_ENDDO first).
3332 We only complain about this in pedantic mode. */
3333 if (gfc_current_block () != NULL)
3334 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
3335 &gfc_current_block()->declared_at);
3340 unexpected_statement (st);
3345 accept_statement (st);
3349 /* Parse the statements of OpenMP do/parallel do. */
3351 static gfc_statement
3352 parse_omp_do (gfc_statement omp_st)
3358 accept_statement (omp_st);
3360 cp = gfc_state_stack->tail;
3361 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3362 np = new_level (cp);
3368 st = next_statement ();
3371 else if (st == ST_DO)
3374 unexpected_statement (st);
3378 if (gfc_statement_label != NULL
3379 && gfc_state_stack->previous != NULL
3380 && gfc_state_stack->previous->state == COMP_DO
3381 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
3389 there should be no !$OMP END DO. */
3391 return ST_IMPLIED_ENDDO;
3394 check_do_closure ();
3397 st = next_statement ();
3398 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
3400 if (new_st.op == EXEC_OMP_END_NOWAIT)
3401 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
3403 gcc_assert (new_st.op == EXEC_NOP);
3404 gfc_clear_new_st ();
3405 gfc_commit_symbols ();
3406 gfc_warning_check ();
3407 st = next_statement ();
3413 /* Parse the statements of OpenMP atomic directive. */
3416 parse_omp_atomic (void)
3422 accept_statement (ST_OMP_ATOMIC);
3424 cp = gfc_state_stack->tail;
3425 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3426 np = new_level (cp);
3432 st = next_statement ();
3435 else if (st == ST_ASSIGNMENT)
3438 unexpected_statement (st);
3441 accept_statement (st);
3447 /* Parse the statements of an OpenMP structured block. */
3450 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
3452 gfc_statement st, omp_end_st;
3456 accept_statement (omp_st);
3458 cp = gfc_state_stack->tail;
3459 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3460 np = new_level (cp);
3466 case ST_OMP_PARALLEL:
3467 omp_end_st = ST_OMP_END_PARALLEL;
3469 case ST_OMP_PARALLEL_SECTIONS:
3470 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
3472 case ST_OMP_SECTIONS:
3473 omp_end_st = ST_OMP_END_SECTIONS;
3475 case ST_OMP_ORDERED:
3476 omp_end_st = ST_OMP_END_ORDERED;
3478 case ST_OMP_CRITICAL:
3479 omp_end_st = ST_OMP_END_CRITICAL;
3482 omp_end_st = ST_OMP_END_MASTER;
3485 omp_end_st = ST_OMP_END_SINGLE;
3488 omp_end_st = ST_OMP_END_TASK;
3490 case ST_OMP_WORKSHARE:
3491 omp_end_st = ST_OMP_END_WORKSHARE;
3493 case ST_OMP_PARALLEL_WORKSHARE:
3494 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
3502 if (workshare_stmts_only)
3504 /* Inside of !$omp workshare, only
3507 where statements and constructs
3508 forall statements and constructs
3512 are allowed. For !$omp critical these
3513 restrictions apply recursively. */
3516 st = next_statement ();
3527 accept_statement (st);
3530 case ST_WHERE_BLOCK:
3531 parse_where_block ();
3534 case ST_FORALL_BLOCK:
3535 parse_forall_block ();
3538 case ST_OMP_PARALLEL:
3539 case ST_OMP_PARALLEL_SECTIONS:
3540 parse_omp_structured_block (st, false);
3543 case ST_OMP_PARALLEL_WORKSHARE:
3544 case ST_OMP_CRITICAL:
3545 parse_omp_structured_block (st, true);
3548 case ST_OMP_PARALLEL_DO:
3549 st = parse_omp_do (st);
3553 parse_omp_atomic ();
3564 st = next_statement ();
3568 st = parse_executable (ST_NONE);
3571 else if (st == ST_OMP_SECTION
3572 && (omp_st == ST_OMP_SECTIONS
3573 || omp_st == ST_OMP_PARALLEL_SECTIONS))
3575 np = new_level (np);
3579 else if (st != omp_end_st)
3580 unexpected_statement (st);
3582 while (st != omp_end_st);
3586 case EXEC_OMP_END_NOWAIT:
3587 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
3589 case EXEC_OMP_CRITICAL:
3590 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
3591 || (new_st.ext.omp_name != NULL
3592 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
3593 gfc_error ("Name after !$omp critical and !$omp end critical does "
3595 gfc_free (CONST_CAST (char *, new_st.ext.omp_name));
3597 case EXEC_OMP_END_SINGLE:
3598 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
3599 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
3600 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
3601 gfc_free_omp_clauses (new_st.ext.omp_clauses);
3609 gfc_clear_new_st ();
3610 gfc_commit_symbols ();
3611 gfc_warning_check ();
3616 /* Accept a series of executable statements. We return the first
3617 statement that doesn't fit to the caller. Any block statements are
3618 passed on to the correct handler, which usually passes the buck
3621 static gfc_statement
3622 parse_executable (gfc_statement st)
3627 st = next_statement ();
3631 close_flag = check_do_closure ();
3636 case ST_END_PROGRAM:
3639 case ST_END_FUNCTION:
3644 case ST_END_SUBROUTINE:
3649 case ST_SELECT_CASE:
3650 gfc_error ("%s statement at %C cannot terminate a non-block "
3651 "DO loop", gfc_ascii_statement (st));
3667 accept_statement (st);
3668 if (close_flag == 1)
3669 return ST_IMPLIED_ENDDO;
3673 parse_block_construct ();
3684 case ST_SELECT_CASE:
3685 parse_select_block ();
3688 case ST_SELECT_TYPE:
3689 parse_select_type_block();
3694 if (check_do_closure () == 1)
3695 return ST_IMPLIED_ENDDO;
3699 parse_critical_block ();
3702 case ST_WHERE_BLOCK:
3703 parse_where_block ();
3706 case ST_FORALL_BLOCK:
3707 parse_forall_block ();
3710 case ST_OMP_PARALLEL:
3711 case ST_OMP_PARALLEL_SECTIONS:
3712 case ST_OMP_SECTIONS:
3713 case ST_OMP_ORDERED:
3714 case ST_OMP_CRITICAL:
3718 parse_omp_structured_block (st, false);
3721 case ST_OMP_WORKSHARE:
3722 case ST_OMP_PARALLEL_WORKSHARE:
3723 parse_omp_structured_block (st, true);
3727 case ST_OMP_PARALLEL_DO:
3728 st = parse_omp_do (st);
3729 if (st == ST_IMPLIED_ENDDO)
3734 parse_omp_atomic ();
3741 st = next_statement ();
3746 /* Fix the symbols for sibling functions. These are incorrectly added to
3747 the child namespace as the parser didn't know about this procedure. */
3750 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
3754 gfc_symbol *old_sym;
3756 sym->attr.referenced = 1;
3757 for (ns = siblings; ns; ns = ns->sibling)
3759 st = gfc_find_symtree (ns->sym_root, sym->name);
3761 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
3762 goto fixup_contained;
3764 old_sym = st->n.sym;
3765 if (old_sym->ns == ns
3766 && !old_sym->attr.contained
3768 /* By 14.6.1.3, host association should be excluded
3769 for the following. */
3770 && !(old_sym->attr.external
3771 || (old_sym->ts.type != BT_UNKNOWN
3772 && !old_sym->attr.implicit_type)
3773 || old_sym->attr.flavor == FL_PARAMETER
3774 || old_sym->attr.use_assoc
3775 || old_sym->attr.in_common
3776 || old_sym->attr.in_equivalence
3777 || old_sym->attr.data
3778 || old_sym->attr.dummy
3779 || old_sym->attr.result
3780 || old_sym->attr.dimension
3781 || old_sym->attr.allocatable
3782 || old_sym->attr.intrinsic
3783 || old_sym->attr.generic
3784 || old_sym->attr.flavor == FL_NAMELIST
3785 || old_sym->attr.proc == PROC_ST_FUNCTION))
3787 /* Replace it with the symbol from the parent namespace. */
3791 /* Free the old (local) symbol. */
3793 if (old_sym->refs == 0)
3794 gfc_free_symbol (old_sym);
3798 /* Do the same for any contained procedures. */
3799 gfc_fixup_sibling_symbols (sym, ns->contained);
3804 parse_contained (int module)
3806 gfc_namespace *ns, *parent_ns, *tmp;
3807 gfc_state_data s1, s2;
3811 int contains_statements = 0;
3814 push_state (&s1, COMP_CONTAINS, NULL);
3815 parent_ns = gfc_current_ns;
3819 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
3821 gfc_current_ns->sibling = parent_ns->contained;
3822 parent_ns->contained = gfc_current_ns;
3825 /* Process the next available statement. We come here if we got an error
3826 and rejected the last statement. */
3827 st = next_statement ();
3836 contains_statements = 1;
3837 accept_statement (st);
3840 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
3843 /* For internal procedures, create/update the symbol in the
3844 parent namespace. */
3848 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
3849 gfc_error ("Contained procedure '%s' at %C is already "
3850 "ambiguous", gfc_new_block->name);
3853 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
3854 &gfc_new_block->declared_at) ==
3857 if (st == ST_FUNCTION)
3858 gfc_add_function (&sym->attr, sym->name,
3859 &gfc_new_block->declared_at);
3861 gfc_add_subroutine (&sym->attr, sym->name,
3862 &gfc_new_block->declared_at);
3866 gfc_commit_symbols ();
3869 sym = gfc_new_block;
3871 /* Mark this as a contained function, so it isn't replaced
3872 by other module functions. */
3873 sym->attr.contained = 1;
3874 sym->attr.referenced = 1;
3876 parse_progunit (ST_NONE);
3878 /* Fix up any sibling functions that refer to this one. */
3879 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
3880 /* Or refer to any of its alternate entry points. */
3881 for (el = gfc_current_ns->entries; el; el = el->next)
3882 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
3884 gfc_current_ns->code = s2.head;
3885 gfc_current_ns = parent_ns;
3890 /* These statements are associated with the end of the host unit. */
3891 case ST_END_FUNCTION:
3893 case ST_END_PROGRAM:
3894 case ST_END_SUBROUTINE:
3895 accept_statement (st);
3899 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
3900 gfc_ascii_statement (st));
3901 reject_statement ();
3907 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
3908 && st != ST_END_MODULE && st != ST_END_PROGRAM);
3910 /* The first namespace in the list is guaranteed to not have
3911 anything (worthwhile) in it. */
3912 tmp = gfc_current_ns;
3913 gfc_current_ns = parent_ns;
3914 if (seen_error && tmp->refs > 1)
3915 gfc_free_namespace (tmp);
3917 ns = gfc_current_ns->contained;
3918 gfc_current_ns->contained = ns->sibling;
3919 gfc_free_namespace (ns);
3922 if (!contains_statements)
3923 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTAINS statement without "
3924 "FUNCTION or SUBROUTINE statement at %C");
3928 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
3931 parse_progunit (gfc_statement st)
3936 st = parse_spec (st);
3943 /* This is not allowed within BLOCK! */
3944 if (gfc_current_state () != COMP_BLOCK)
3949 accept_statement (st);
3956 if (gfc_current_state () == COMP_FUNCTION)
3957 gfc_check_function_type (gfc_current_ns);
3962 st = parse_executable (st);
3970 /* This is not allowed within BLOCK! */
3971 if (gfc_current_state () != COMP_BLOCK)
3976 accept_statement (st);
3983 unexpected_statement (st);
3984 reject_statement ();
3985 st = next_statement ();
3991 for (p = gfc_state_stack; p; p = p->previous)
3992 if (p->state == COMP_CONTAINS)
3995 if (gfc_find_state (COMP_MODULE) == SUCCESS)
4000 gfc_error ("CONTAINS statement at %C is already in a contained "
4002 st = next_statement ();
4006 parse_contained (0);
4009 gfc_current_ns->code = gfc_state_stack->head;
4013 /* Come here to complain about a global symbol already in use as
4017 gfc_global_used (gfc_gsymbol *sym, locus *where)
4022 where = &gfc_current_locus;
4032 case GSYM_SUBROUTINE:
4033 name = "SUBROUTINE";
4038 case GSYM_BLOCK_DATA:
4039 name = "BLOCK DATA";
4045 gfc_internal_error ("gfc_global_used(): Bad type");
4049 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
4050 sym->name, where, name, &sym->where);
4054 /* Parse a block data program unit. */
4057 parse_block_data (void)
4060 static locus blank_locus;
4061 static int blank_block=0;
4064 gfc_current_ns->proc_name = gfc_new_block;
4065 gfc_current_ns->is_block_data = 1;
4067 if (gfc_new_block == NULL)
4070 gfc_error ("Blank BLOCK DATA at %C conflicts with "
4071 "prior BLOCK DATA at %L", &blank_locus);
4075 blank_locus = gfc_current_locus;
4080 s = gfc_get_gsymbol (gfc_new_block->name);
4082 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
4083 gfc_global_used(s, NULL);
4086 s->type = GSYM_BLOCK_DATA;
4087 s->where = gfc_current_locus;
4092 st = parse_spec (ST_NONE);
4094 while (st != ST_END_BLOCK_DATA)
4096 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
4097 gfc_ascii_statement (st));
4098 reject_statement ();
4099 st = next_statement ();
4104 /* Parse a module subprogram. */
4112 s = gfc_get_gsymbol (gfc_new_block->name);
4113 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
4114 gfc_global_used(s, NULL);
4117 s->type = GSYM_MODULE;
4118 s->where = gfc_current_locus;
4122 st = parse_spec (ST_NONE);
4131 parse_contained (1);
4135 accept_statement (st);
4139 gfc_error ("Unexpected %s statement in MODULE at %C",
4140 gfc_ascii_statement (st));
4142 reject_statement ();
4143 st = next_statement ();
4147 s->ns = gfc_current_ns;
4151 /* Add a procedure name to the global symbol table. */
4154 add_global_procedure (int sub)
4158 s = gfc_get_gsymbol(gfc_new_block->name);
4161 || (s->type != GSYM_UNKNOWN
4162 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
4163 gfc_global_used(s, NULL);
4166 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4167 s->where = gfc_current_locus;
4169 s->ns = gfc_current_ns;
4174 /* Add a program to the global symbol table. */
4177 add_global_program (void)
4181 if (gfc_new_block == NULL)
4183 s = gfc_get_gsymbol (gfc_new_block->name);
4185 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
4186 gfc_global_used(s, NULL);
4189 s->type = GSYM_PROGRAM;
4190 s->where = gfc_current_locus;
4192 s->ns = gfc_current_ns;
4197 /* Resolve all the program units when whole file scope option
4200 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
4202 gfc_free_dt_list ();
4203 gfc_current_ns = gfc_global_ns_list;
4204 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4206 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4207 gfc_resolve (gfc_current_ns);
4208 gfc_current_ns->derived_types = gfc_derived_types;
4209 gfc_derived_types = NULL;
4215 clean_up_modules (gfc_gsymbol *gsym)
4220 clean_up_modules (gsym->left);
4221 clean_up_modules (gsym->right);
4223 if (gsym->type != GSYM_MODULE || !gsym->ns)
4226 gfc_current_ns = gsym->ns;
4227 gfc_derived_types = gfc_current_ns->derived_types;
4234 /* Translate all the program units when whole file scope option
4235 is active. This could be in a different order to resolution if
4236 there are forward references in the file. */
4238 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
4242 gfc_current_ns = gfc_global_ns_list;
4243 gfc_get_errors (NULL, &errors);
4245 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4247 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4248 gfc_derived_types = gfc_current_ns->derived_types;
4249 gfc_generate_code (gfc_current_ns);
4250 gfc_current_ns->translated = 1;
4253 /* Clean up all the namespaces after translation. */
4254 gfc_current_ns = gfc_global_ns_list;
4255 for (;gfc_current_ns;)
4257 gfc_namespace *ns = gfc_current_ns->sibling;
4258 gfc_derived_types = gfc_current_ns->derived_types;
4260 gfc_current_ns = ns;
4263 clean_up_modules (gfc_gsym_root);
4267 /* Top level parser. */
4270 gfc_parse_file (void)
4272 int seen_program, errors_before, errors;
4273 gfc_state_data top, s;
4276 gfc_namespace *next;
4278 gfc_start_source_files ();
4280 top.state = COMP_NONE;
4282 top.previous = NULL;
4283 top.head = top.tail = NULL;
4284 top.do_variable = NULL;
4286 gfc_state_stack = ⊤
4288 gfc_clear_new_st ();
4290 gfc_statement_label = NULL;
4292 if (setjmp (eof_buf))
4293 return FAILURE; /* Come here on unexpected EOF */
4295 /* Prepare the global namespace that will contain the
4297 gfc_global_ns_list = next = NULL;
4301 /* Exit early for empty files. */
4307 st = next_statement ();
4316 goto duplicate_main;
4318 prog_locus = gfc_current_locus;
4320 push_state (&s, COMP_PROGRAM, gfc_new_block);
4321 main_program_symbol(gfc_current_ns, gfc_new_block->name);
4322 accept_statement (st);
4323 add_global_program ();
4324 parse_progunit (ST_NONE);
4325 if (gfc_option.flag_whole_file)
4330 add_global_procedure (1);
4331 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
4332 accept_statement (st);
4333 parse_progunit (ST_NONE);
4334 if (gfc_option.flag_whole_file)
4339 add_global_procedure (0);
4340 push_state (&s, COMP_FUNCTION, gfc_new_block);
4341 accept_statement (st);
4342 parse_progunit (ST_NONE);
4343 if (gfc_option.flag_whole_file)
4348 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
4349 accept_statement (st);
4350 parse_block_data ();
4354 push_state (&s, COMP_MODULE, gfc_new_block);
4355 accept_statement (st);
4357 gfc_get_errors (NULL, &errors_before);