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 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
721 gfc_next_ascii_char (); /* Eat up the semicolon. */
725 return decode_statement ();
729 /* Get the next statement in fixed-form source. */
734 int label, digit_flag, i;
739 return decode_statement ();
741 /* Skip past the current label field, parsing a statement label if
742 one is there. This is a weird number parser, since the number is
743 contained within five columns and can have any kind of embedded
744 spaces. We also check for characters that make the rest of the
750 for (i = 0; i < 5; i++)
752 c = gfc_next_char_literal (0);
769 label = label * 10 + ((unsigned char) c - '0');
770 label_locus = gfc_current_locus;
774 /* Comments have already been skipped by the time we get
775 here, except for GCC attributes and OpenMP directives. */
778 c = gfc_next_char_literal (0);
780 if (TOLOWER (c) == 'g')
782 for (i = 0; i < 4; i++, c = gfc_next_char_literal (0))
783 gcc_assert (TOLOWER (c) == "gcc$"[i]);
785 return decode_gcc_attribute ();
787 else if (c == '$' && gfc_option.flag_openmp)
789 for (i = 0; i < 4; i++, c = gfc_next_char_literal (0))
790 gcc_assert ((char) gfc_wide_tolower (c) == "$omp"[i]);
792 if (c != ' ' && c != '0')
794 gfc_buffer_error (0);
795 gfc_error ("Bad continuation line at %C");
799 return decode_omp_directive ();
803 /* Comments have already been skipped by the time we get
804 here so don't bother checking for them. */
807 gfc_buffer_error (0);
808 gfc_error ("Non-numeric character in statement label at %C");
816 gfc_warning_now ("Zero is not a valid statement label at %C");
819 /* We've found a valid statement label. */
820 gfc_statement_label = gfc_get_st_label (label);
824 /* Since this line starts a statement, it cannot be a continuation
825 of a previous statement. If we see something here besides a
826 space or zero, it must be a bad continuation line. */
828 c = gfc_next_char_literal (0);
832 if (c != ' ' && c != '0')
834 gfc_buffer_error (0);
835 gfc_error ("Bad continuation line at %C");
839 /* Now that we've taken care of the statement label columns, we have
840 to make sure that the first nonblank character is not a '!'. If
841 it is, the rest of the line is a comment. */
845 loc = gfc_current_locus;
846 c = gfc_next_char_literal (0);
848 while (gfc_is_whitespace (c));
852 gfc_current_locus = loc;
856 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
860 if (gfc_match_eos () == MATCH_YES)
863 /* At this point, we've got a nonblank statement to parse. */
864 return decode_statement ();
868 gfc_warning_now ("Ignoring statement label in empty statement at %L",
871 gfc_current_locus.lb->truncated = 0;
877 /* Return the next non-ST_NONE statement to the caller. We also worry
878 about including files and the ends of include files at this stage. */
881 next_statement (void)
886 gfc_new_block = NULL;
888 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
891 gfc_statement_label = NULL;
892 gfc_buffer_error (1);
897 gfc_skip_comments ();
905 if (gfc_define_undef_line ())
908 old_locus = gfc_current_locus;
910 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
916 gfc_buffer_error (0);
918 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
920 gfc_free_st_label (gfc_statement_label);
921 gfc_statement_label = NULL;
922 gfc_current_locus = old_locus;
926 check_statement_label (st);
932 /****************************** Parser ***********************************/
934 /* The parser subroutines are of type 'try' that fail if the file ends
937 /* Macros that expand to case-labels for various classes of
938 statements. Start with executable statements that directly do
941 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
942 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
943 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
944 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
945 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
946 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
947 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
948 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
949 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_ERROR_STOP: \
950 case ST_SYNC_ALL: case ST_SYNC_IMAGES: case ST_SYNC_MEMORY
952 /* Statements that mark other executable statements. */
954 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
955 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
956 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
957 case ST_OMP_PARALLEL: \
958 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
959 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
960 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
961 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
962 case ST_OMP_TASK: case ST_CRITICAL
964 /* Declaration statements */
966 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
967 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
968 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
971 /* Block end statements. Errors associated with interchanging these
972 are detected in gfc_match_end(). */
974 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
975 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
976 case ST_END_BLOCK: case ST_END_ASSOCIATE
979 /* Push a new state onto the stack. */
982 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
984 p->state = new_state;
985 p->previous = gfc_state_stack;
987 p->head = p->tail = NULL;
988 p->do_variable = NULL;
993 /* Pop the current state. */
997 gfc_state_stack = gfc_state_stack->previous;
1001 /* Try to find the given state in the state stack. */
1004 gfc_find_state (gfc_compile_state state)
1008 for (p = gfc_state_stack; p; p = p->previous)
1009 if (p->state == state)
1012 return (p == NULL) ? FAILURE : SUCCESS;
1016 /* Starts a new level in the statement list. */
1019 new_level (gfc_code *q)
1023 p = q->block = gfc_get_code ();
1025 gfc_state_stack->head = gfc_state_stack->tail = p;
1031 /* Add the current new_st code structure and adds it to the current
1032 program unit. As a side-effect, it zeroes the new_st. */
1035 add_statement (void)
1039 p = gfc_get_code ();
1042 p->loc = gfc_current_locus;
1044 if (gfc_state_stack->head == NULL)
1045 gfc_state_stack->head = p;
1047 gfc_state_stack->tail->next = p;
1049 while (p->next != NULL)
1052 gfc_state_stack->tail = p;
1054 gfc_clear_new_st ();
1060 /* Frees everything associated with the current statement. */
1063 undo_new_statement (void)
1065 gfc_free_statements (new_st.block);
1066 gfc_free_statements (new_st.next);
1067 gfc_free_statement (&new_st);
1068 gfc_clear_new_st ();
1072 /* If the current statement has a statement label, make sure that it
1073 is allowed to, or should have one. */
1076 check_statement_label (gfc_statement st)
1080 if (gfc_statement_label == NULL)
1082 if (st == ST_FORMAT)
1083 gfc_error ("FORMAT statement at %L does not have a statement label",
1090 case ST_END_PROGRAM:
1091 case ST_END_FUNCTION:
1092 case ST_END_SUBROUTINE:
1096 case ST_END_CRITICAL:
1099 type = ST_LABEL_TARGET;
1103 type = ST_LABEL_FORMAT;
1106 /* Statement labels are not restricted from appearing on a
1107 particular line. However, there are plenty of situations
1108 where the resulting label can't be referenced. */
1111 type = ST_LABEL_BAD_TARGET;
1115 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1117 new_st.here = gfc_statement_label;
1121 /* Figures out what the enclosing program unit is. This will be a
1122 function, subroutine, program, block data or module. */
1125 gfc_enclosing_unit (gfc_compile_state * result)
1129 for (p = gfc_state_stack; p; p = p->previous)
1130 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1131 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
1132 || p->state == COMP_PROGRAM)
1141 *result = COMP_PROGRAM;
1146 /* Translate a statement enum to a string. */
1149 gfc_ascii_statement (gfc_statement st)
1155 case ST_ARITHMETIC_IF:
1156 p = _("arithmetic IF");
1165 p = _("attribute declaration");
1201 p = _("data declaration");
1209 case ST_DERIVED_DECL:
1210 p = _("derived type declaration");
1224 case ST_END_ASSOCIATE:
1225 p = "END ASSOCIATE";
1230 case ST_END_BLOCK_DATA:
1231 p = "END BLOCK DATA";
1233 case ST_END_CRITICAL:
1245 case ST_END_FUNCTION:
1251 case ST_END_INTERFACE:
1252 p = "END INTERFACE";
1257 case ST_END_PROGRAM:
1263 case ST_END_SUBROUTINE:
1264 p = "END SUBROUTINE";
1275 case ST_EQUIVALENCE:
1287 case ST_FORALL_BLOCK: /* Fall through */
1309 case ST_IMPLICIT_NONE:
1310 p = "IMPLICIT NONE";
1312 case ST_IMPLIED_ENDDO:
1313 p = _("implied END DO");
1339 case ST_MODULE_PROC:
1340 p = "MODULE PROCEDURE";
1372 case ST_SYNC_IMAGES:
1375 case ST_SYNC_MEMORY:
1387 case ST_WHERE_BLOCK: /* Fall through */
1398 p = _("assignment");
1400 case ST_POINTER_ASSIGNMENT:
1401 p = _("pointer assignment");
1403 case ST_SELECT_CASE:
1406 case ST_SELECT_TYPE:
1421 case ST_STATEMENT_FUNCTION:
1422 p = "STATEMENT FUNCTION";
1424 case ST_LABEL_ASSIGNMENT:
1425 p = "LABEL ASSIGNMENT";
1428 p = "ENUM DEFINITION";
1431 p = "ENUMERATOR DEFINITION";
1439 case ST_OMP_BARRIER:
1440 p = "!$OMP BARRIER";
1442 case ST_OMP_CRITICAL:
1443 p = "!$OMP CRITICAL";
1448 case ST_OMP_END_CRITICAL:
1449 p = "!$OMP END CRITICAL";
1454 case ST_OMP_END_MASTER:
1455 p = "!$OMP END MASTER";
1457 case ST_OMP_END_ORDERED:
1458 p = "!$OMP END ORDERED";
1460 case ST_OMP_END_PARALLEL:
1461 p = "!$OMP END PARALLEL";
1463 case ST_OMP_END_PARALLEL_DO:
1464 p = "!$OMP END PARALLEL DO";
1466 case ST_OMP_END_PARALLEL_SECTIONS:
1467 p = "!$OMP END PARALLEL SECTIONS";
1469 case ST_OMP_END_PARALLEL_WORKSHARE:
1470 p = "!$OMP END PARALLEL WORKSHARE";
1472 case ST_OMP_END_SECTIONS:
1473 p = "!$OMP END SECTIONS";
1475 case ST_OMP_END_SINGLE:
1476 p = "!$OMP END SINGLE";
1478 case ST_OMP_END_TASK:
1479 p = "!$OMP END TASK";
1481 case ST_OMP_END_WORKSHARE:
1482 p = "!$OMP END WORKSHARE";
1490 case ST_OMP_ORDERED:
1491 p = "!$OMP ORDERED";
1493 case ST_OMP_PARALLEL:
1494 p = "!$OMP PARALLEL";
1496 case ST_OMP_PARALLEL_DO:
1497 p = "!$OMP PARALLEL DO";
1499 case ST_OMP_PARALLEL_SECTIONS:
1500 p = "!$OMP PARALLEL SECTIONS";
1502 case ST_OMP_PARALLEL_WORKSHARE:
1503 p = "!$OMP PARALLEL WORKSHARE";
1505 case ST_OMP_SECTIONS:
1506 p = "!$OMP SECTIONS";
1508 case ST_OMP_SECTION:
1509 p = "!$OMP SECTION";
1517 case ST_OMP_TASKWAIT:
1518 p = "!$OMP TASKWAIT";
1520 case ST_OMP_THREADPRIVATE:
1521 p = "!$OMP THREADPRIVATE";
1523 case ST_OMP_WORKSHARE:
1524 p = "!$OMP WORKSHARE";
1527 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1534 /* Create a symbol for the main program and assign it to ns->proc_name. */
1537 main_program_symbol (gfc_namespace *ns, const char *name)
1539 gfc_symbol *main_program;
1540 symbol_attribute attr;
1542 gfc_get_symbol (name, ns, &main_program);
1543 gfc_clear_attr (&attr);
1544 attr.flavor = FL_PROGRAM;
1545 attr.proc = PROC_UNKNOWN;
1546 attr.subroutine = 1;
1547 attr.access = ACCESS_PUBLIC;
1548 attr.is_main_program = 1;
1549 main_program->attr = attr;
1550 main_program->declared_at = gfc_current_locus;
1551 ns->proc_name = main_program;
1552 gfc_commit_symbols ();
1556 /* Do whatever is necessary to accept the last statement. */
1559 accept_statement (gfc_statement st)
1567 case ST_IMPLICIT_NONE:
1568 gfc_set_implicit_none ();
1577 gfc_current_ns->proc_name = gfc_new_block;
1580 /* If the statement is the end of a block, lay down a special code
1581 that allows a branch to the end of the block from within the
1582 construct. IF and SELECT are treated differently from DO
1583 (where EXEC_NOP is added inside the loop) for two
1585 1. END DO has a meaning in the sense that after a GOTO to
1586 it, the loop counter must be increased.
1587 2. IF blocks and SELECT blocks can consist of multiple
1588 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
1589 Putting the label before the END IF would make the jump
1590 from, say, the ELSE IF block to the END IF illegal. */
1594 case ST_END_CRITICAL:
1595 if (gfc_statement_label != NULL)
1597 new_st.op = EXEC_END_BLOCK;
1602 /* The end-of-program unit statements do not get the special
1603 marker and require a statement of some sort if they are a
1606 case ST_END_PROGRAM:
1607 case ST_END_FUNCTION:
1608 case ST_END_SUBROUTINE:
1609 if (gfc_statement_label != NULL)
1611 new_st.op = EXEC_RETURN;
1616 new_st.op = EXEC_END_PROCEDURE;
1632 gfc_commit_symbols ();
1633 gfc_warning_check ();
1634 gfc_clear_new_st ();
1638 /* Undo anything tentative that has been built for the current
1642 reject_statement (void)
1644 /* Revert to the previous charlen chain. */
1645 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
1646 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
1648 gfc_new_block = NULL;
1649 gfc_undo_symbols ();
1650 gfc_clear_warning ();
1651 undo_new_statement ();
1655 /* Generic complaint about an out of order statement. We also do
1656 whatever is necessary to clean up. */
1659 unexpected_statement (gfc_statement st)
1661 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1663 reject_statement ();
1667 /* Given the next statement seen by the matcher, make sure that it is
1668 in proper order with the last. This subroutine is initialized by
1669 calling it with an argument of ST_NONE. If there is a problem, we
1670 issue an error and return FAILURE. Otherwise we return SUCCESS.
1672 Individual parsers need to verify that the statements seen are
1673 valid before calling here, i.e., ENTRY statements are not allowed in
1674 INTERFACE blocks. The following diagram is taken from the standard:
1676 +---------------------------------------+
1677 | program subroutine function module |
1678 +---------------------------------------+
1680 +---------------------------------------+
1682 +---------------------------------------+
1684 | +-----------+------------------+
1685 | | parameter | implicit |
1686 | +-----------+------------------+
1687 | format | | derived type |
1688 | entry | parameter | interface |
1689 | | data | specification |
1690 | | | statement func |
1691 | +-----------+------------------+
1692 | | data | executable |
1693 +--------+-----------+------------------+
1695 +---------------------------------------+
1696 | internal module/subprogram |
1697 +---------------------------------------+
1699 +---------------------------------------+
1708 ORDER_IMPLICIT_NONE,
1716 enum state_order state;
1717 gfc_statement last_statement;
1723 verify_st_order (st_state *p, gfc_statement st, bool silent)
1729 p->state = ORDER_START;
1733 if (p->state > ORDER_USE)
1735 p->state = ORDER_USE;
1739 if (p->state > ORDER_IMPORT)
1741 p->state = ORDER_IMPORT;
1744 case ST_IMPLICIT_NONE:
1745 if (p->state > ORDER_IMPLICIT_NONE)
1748 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1749 statement disqualifies a USE but not an IMPLICIT NONE.
1750 Duplicate IMPLICIT NONEs are caught when the implicit types
1753 p->state = ORDER_IMPLICIT_NONE;
1757 if (p->state > ORDER_IMPLICIT)
1759 p->state = ORDER_IMPLICIT;
1764 if (p->state < ORDER_IMPLICIT_NONE)
1765 p->state = ORDER_IMPLICIT_NONE;
1769 if (p->state >= ORDER_EXEC)
1771 if (p->state < ORDER_IMPLICIT)
1772 p->state = ORDER_IMPLICIT;
1776 if (p->state < ORDER_SPEC)
1777 p->state = ORDER_SPEC;
1782 case ST_DERIVED_DECL:
1784 if (p->state >= ORDER_EXEC)
1786 if (p->state < ORDER_SPEC)
1787 p->state = ORDER_SPEC;
1792 if (p->state < ORDER_EXEC)
1793 p->state = ORDER_EXEC;
1797 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1798 gfc_ascii_statement (st));
1801 /* All is well, record the statement in case we need it next time. */
1802 p->where = gfc_current_locus;
1803 p->last_statement = st;
1808 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1809 gfc_ascii_statement (st),
1810 gfc_ascii_statement (p->last_statement), &p->where);
1816 /* Handle an unexpected end of file. This is a show-stopper... */
1818 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1821 unexpected_eof (void)
1825 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1827 /* Memory cleanup. Move to "second to last". */
1828 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1831 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1834 longjmp (eof_buf, 1);
1838 /* Parse the CONTAINS section of a derived type definition. */
1840 gfc_access gfc_typebound_default_access;
1843 parse_derived_contains (void)
1846 bool seen_private = false;
1847 bool seen_comps = false;
1848 bool error_flag = false;
1851 gcc_assert (gfc_current_state () == COMP_DERIVED);
1852 gcc_assert (gfc_current_block ());
1854 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
1856 if (gfc_current_block ()->attr.sequence)
1857 gfc_error ("Derived-type '%s' with SEQUENCE must not have a CONTAINS"
1858 " section at %C", gfc_current_block ()->name);
1859 if (gfc_current_block ()->attr.is_bind_c)
1860 gfc_error ("Derived-type '%s' with BIND(C) must not have a CONTAINS"
1861 " section at %C", gfc_current_block ()->name);
1863 accept_statement (ST_CONTAINS);
1864 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
1866 gfc_typebound_default_access = ACCESS_PUBLIC;
1872 st = next_statement ();
1880 gfc_error ("Components in TYPE at %C must precede CONTAINS");
1885 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Type-bound"
1886 " procedure at %C") == FAILURE)
1889 accept_statement (ST_PROCEDURE);
1894 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: GENERIC binding"
1895 " at %C") == FAILURE)
1898 accept_statement (ST_GENERIC);
1903 if (gfc_notify_std (GFC_STD_F2003,
1904 "Fortran 2003: FINAL procedure declaration"
1905 " at %C") == FAILURE)
1908 accept_statement (ST_FINAL);
1916 && (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Derived type "
1917 "definition at %C with empty CONTAINS "
1918 "section") == FAILURE))
1921 /* ST_END_TYPE is accepted by parse_derived after return. */
1925 if (gfc_find_state (COMP_MODULE) == FAILURE)
1927 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1935 gfc_error ("PRIVATE statement at %C must precede procedure"
1943 gfc_error ("Duplicate PRIVATE statement at %C");
1947 accept_statement (ST_PRIVATE);
1948 gfc_typebound_default_access = ACCESS_PRIVATE;
1949 seen_private = true;
1953 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
1958 gfc_error ("Already inside a CONTAINS block at %C");
1963 unexpected_statement (st);
1969 gcc_assert (gfc_current_state () == COMP_DERIVED);
1975 /* Parse a derived type. */
1978 parse_derived (void)
1980 int compiling_type, seen_private, seen_sequence, seen_component;
1986 accept_statement (ST_DERIVED_DECL);
1987 push_state (&s, COMP_DERIVED, gfc_new_block);
1989 gfc_new_block->component_access = ACCESS_PUBLIC;
1996 while (compiling_type)
1998 st = next_statement ();
2006 accept_statement (st);
2011 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2018 if (!seen_component)
2019 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Derived type "
2020 "definition at %C without components");
2022 accept_statement (ST_END_TYPE);
2026 if (gfc_find_state (COMP_MODULE) == FAILURE)
2028 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2035 gfc_error ("PRIVATE statement at %C must precede "
2036 "structure components");
2041 gfc_error ("Duplicate PRIVATE statement at %C");
2043 s.sym->component_access = ACCESS_PRIVATE;
2045 accept_statement (ST_PRIVATE);
2052 gfc_error ("SEQUENCE statement at %C must precede "
2053 "structure components");
2057 if (gfc_current_block ()->attr.sequence)
2058 gfc_warning ("SEQUENCE attribute at %C already specified in "
2063 gfc_error ("Duplicate SEQUENCE statement at %C");
2067 gfc_add_sequence (&gfc_current_block ()->attr,
2068 gfc_current_block ()->name, NULL);
2072 gfc_notify_std (GFC_STD_F2003,
2073 "Fortran 2003: CONTAINS block in derived type"
2074 " definition at %C");
2076 accept_statement (ST_CONTAINS);
2077 parse_derived_contains ();
2081 unexpected_statement (st);
2086 /* need to verify that all fields of the derived type are
2087 * interoperable with C if the type is declared to be bind(c)
2089 sym = gfc_current_block ();
2090 for (c = sym->components; c; c = c->next)
2092 /* Look for allocatable components. */
2093 if (c->attr.allocatable
2094 || (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.allocatable)
2095 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.alloc_comp))
2096 sym->attr.alloc_comp = 1;
2098 /* Look for pointer components. */
2100 || (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.pointer)
2101 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2102 sym->attr.pointer_comp = 1;
2104 /* Look for procedure pointer components. */
2105 if (c->attr.proc_pointer
2106 || (c->ts.type == BT_DERIVED
2107 && c->ts.u.derived->attr.proc_pointer_comp))
2108 sym->attr.proc_pointer_comp = 1;
2110 /* Looking for coarray components. */
2111 if (c->attr.codimension
2112 || (c->attr.coarray_comp && !c->attr.pointer && !c->attr.allocatable))
2113 sym->attr.coarray_comp = 1;
2115 /* Look for private components. */
2116 if (sym->component_access == ACCESS_PRIVATE
2117 || c->attr.access == ACCESS_PRIVATE
2118 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
2119 sym->attr.private_comp = 1;
2122 if (!seen_component)
2123 sym->attr.zero_comp = 1;
2129 /* Parse an ENUM. */
2137 int seen_enumerator = 0;
2139 push_state (&s, COMP_ENUM, gfc_new_block);
2143 while (compiling_enum)
2145 st = next_statement ();
2153 seen_enumerator = 1;
2154 accept_statement (st);
2159 if (!seen_enumerator)
2160 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2161 accept_statement (st);
2165 gfc_free_enum_history ();
2166 unexpected_statement (st);
2174 /* Parse an interface. We must be able to deal with the possibility
2175 of recursive interfaces. The parse_spec() subroutine is mutually
2176 recursive with parse_interface(). */
2178 static gfc_statement parse_spec (gfc_statement);
2181 parse_interface (void)
2183 gfc_compile_state new_state = COMP_NONE, current_state;
2184 gfc_symbol *prog_unit, *sym;
2185 gfc_interface_info save;
2186 gfc_state_data s1, s2;
2190 accept_statement (ST_INTERFACE);
2192 current_interface.ns = gfc_current_ns;
2193 save = current_interface;
2195 sym = (current_interface.type == INTERFACE_GENERIC
2196 || current_interface.type == INTERFACE_USER_OP)
2197 ? gfc_new_block : NULL;
2199 push_state (&s1, COMP_INTERFACE, sym);
2200 current_state = COMP_NONE;
2203 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
2205 st = next_statement ();
2213 if (st == ST_SUBROUTINE)
2214 new_state = COMP_SUBROUTINE;
2215 else if (st == ST_FUNCTION)
2216 new_state = COMP_FUNCTION;
2217 if (gfc_new_block->attr.pointer)
2219 gfc_new_block->attr.pointer = 0;
2220 gfc_new_block->attr.proc_pointer = 1;
2222 if (gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
2223 gfc_new_block->formal, NULL) == FAILURE)
2225 reject_statement ();
2226 gfc_free_namespace (gfc_current_ns);
2232 case ST_MODULE_PROC: /* The module procedure matcher makes
2233 sure the context is correct. */
2234 accept_statement (st);
2235 gfc_free_namespace (gfc_current_ns);
2238 case ST_END_INTERFACE:
2239 gfc_free_namespace (gfc_current_ns);
2240 gfc_current_ns = current_interface.ns;
2244 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2245 gfc_ascii_statement (st));
2246 reject_statement ();
2247 gfc_free_namespace (gfc_current_ns);
2252 /* Make sure that a generic interface has only subroutines or
2253 functions and that the generic name has the right attribute. */
2254 if (current_interface.type == INTERFACE_GENERIC)
2256 if (current_state == COMP_NONE)
2258 if (new_state == COMP_FUNCTION && sym)
2259 gfc_add_function (&sym->attr, sym->name, NULL);
2260 else if (new_state == COMP_SUBROUTINE && sym)
2261 gfc_add_subroutine (&sym->attr, sym->name, NULL);
2263 current_state = new_state;
2267 if (new_state != current_state)
2269 if (new_state == COMP_SUBROUTINE)
2270 gfc_error ("SUBROUTINE at %C does not belong in a "
2271 "generic function interface");
2273 if (new_state == COMP_FUNCTION)
2274 gfc_error ("FUNCTION at %C does not belong in a "
2275 "generic subroutine interface");
2280 if (current_interface.type == INTERFACE_ABSTRACT)
2282 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
2283 if (gfc_is_intrinsic_typename (gfc_new_block->name))
2284 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
2285 "cannot be the same as an intrinsic type",
2286 gfc_new_block->name);
2289 push_state (&s2, new_state, gfc_new_block);
2290 accept_statement (st);
2291 prog_unit = gfc_new_block;
2292 prog_unit->formal_ns = gfc_current_ns;
2293 proc_locus = gfc_current_locus;
2296 /* Read data declaration statements. */
2297 st = parse_spec (ST_NONE);
2299 /* Since the interface block does not permit an IMPLICIT statement,
2300 the default type for the function or the result must be taken
2301 from the formal namespace. */
2302 if (new_state == COMP_FUNCTION)
2304 if (prog_unit->result == prog_unit
2305 && prog_unit->ts.type == BT_UNKNOWN)
2306 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
2307 else if (prog_unit->result != prog_unit
2308 && prog_unit->result->ts.type == BT_UNKNOWN)
2309 gfc_set_default_type (prog_unit->result, 1,
2310 prog_unit->formal_ns);
2313 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
2315 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
2316 gfc_ascii_statement (st));
2317 reject_statement ();
2321 /* Add EXTERNAL attribute to function or subroutine. */
2322 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
2323 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
2325 current_interface = save;
2326 gfc_add_interface (prog_unit);
2329 if (current_interface.ns
2330 && current_interface.ns->proc_name
2331 && strcmp (current_interface.ns->proc_name->name,
2332 prog_unit->name) == 0)
2333 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2334 "enclosing procedure", prog_unit->name, &proc_locus);
2343 /* Associate function characteristics by going back to the function
2344 declaration and rematching the prefix. */
2347 match_deferred_characteristics (gfc_typespec * ts)
2350 match m = MATCH_ERROR;
2351 char name[GFC_MAX_SYMBOL_LEN + 1];
2353 loc = gfc_current_locus;
2355 gfc_current_locus = gfc_current_block ()->declared_at;
2358 gfc_buffer_error (1);
2359 m = gfc_match_prefix (ts);
2360 gfc_buffer_error (0);
2362 if (ts->type == BT_DERIVED)
2370 /* Only permit one go at the characteristic association. */
2374 /* Set the function locus correctly. If we have not found the
2375 function name, there is an error. */
2377 && gfc_match ("function% %n", name) == MATCH_YES
2378 && strcmp (name, gfc_current_block ()->name) == 0)
2380 gfc_current_block ()->declared_at = gfc_current_locus;
2381 gfc_commit_symbols ();
2386 gfc_current_locus =loc;
2391 /* Check specification-expressions in the function result of the currently
2392 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
2393 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
2394 scope are not yet parsed so this has to be delayed up to parse_spec. */
2397 check_function_result_typed (void)
2399 gfc_typespec* ts = &gfc_current_ns->proc_name->result->ts;
2401 gcc_assert (gfc_current_state () == COMP_FUNCTION);
2402 gcc_assert (ts->type != BT_UNKNOWN);
2404 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
2405 /* TODO: Extend when KIND type parameters are implemented. */
2406 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length)
2407 gfc_expr_check_typed (ts->u.cl->length, gfc_current_ns, true);
2411 /* Parse a set of specification statements. Returns the statement
2412 that doesn't fit. */
2414 static gfc_statement
2415 parse_spec (gfc_statement st)
2418 bool function_result_typed = false;
2419 bool bad_characteristic = false;
2422 verify_st_order (&ss, ST_NONE, false);
2424 st = next_statement ();
2426 /* If we are not inside a function or don't have a result specified so far,
2427 do nothing special about it. */
2428 if (gfc_current_state () != COMP_FUNCTION)
2429 function_result_typed = true;
2432 gfc_symbol* proc = gfc_current_ns->proc_name;
2435 if (proc->result->ts.type == BT_UNKNOWN)
2436 function_result_typed = true;
2441 /* If we're inside a BLOCK construct, some statements are disallowed.
2442 Check this here. Attribute declaration statements like INTENT, OPTIONAL
2443 or VALUE are also disallowed, but they don't have a particular ST_*
2444 key so we have to check for them individually in their matcher routine. */
2445 if (gfc_current_state () == COMP_BLOCK)
2449 case ST_IMPLICIT_NONE:
2452 case ST_EQUIVALENCE:
2453 case ST_STATEMENT_FUNCTION:
2454 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
2455 gfc_ascii_statement (st));
2462 /* If we find a statement that can not be followed by an IMPLICIT statement
2463 (and thus we can expect to see none any further), type the function result
2464 if it has not yet been typed. Be careful not to give the END statement
2465 to verify_st_order! */
2466 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
2468 bool verify_now = false;
2470 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
2475 verify_st_order (&dummyss, ST_NONE, false);
2476 verify_st_order (&dummyss, st, false);
2478 if (verify_st_order (&dummyss, ST_IMPLICIT, true) == FAILURE)
2484 check_function_result_typed ();
2485 function_result_typed = true;
2494 case ST_IMPLICIT_NONE:
2496 if (!function_result_typed)
2498 check_function_result_typed ();
2499 function_result_typed = true;
2505 case ST_DATA: /* Not allowed in interfaces */
2506 if (gfc_current_state () == COMP_INTERFACE)
2516 case ST_DERIVED_DECL:
2519 if (verify_st_order (&ss, st, false) == FAILURE)
2521 reject_statement ();
2522 st = next_statement ();
2532 case ST_DERIVED_DECL:
2538 if (gfc_current_state () != COMP_MODULE)
2540 gfc_error ("%s statement must appear in a MODULE",
2541 gfc_ascii_statement (st));
2545 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
2547 gfc_error ("%s statement at %C follows another accessibility "
2548 "specification", gfc_ascii_statement (st));
2552 gfc_current_ns->default_access = (st == ST_PUBLIC)
2553 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2557 case ST_STATEMENT_FUNCTION:
2558 if (gfc_current_state () == COMP_MODULE)
2560 unexpected_statement (st);
2568 accept_statement (st);
2569 st = next_statement ();
2573 accept_statement (st);
2575 st = next_statement ();
2578 case ST_GET_FCN_CHARACTERISTICS:
2579 /* This statement triggers the association of a function's result
2581 ts = &gfc_current_block ()->result->ts;
2582 if (match_deferred_characteristics (ts) != MATCH_YES)
2583 bad_characteristic = true;
2585 st = next_statement ();
2592 /* If match_deferred_characteristics failed, then there is an error. */
2593 if (bad_characteristic)
2595 ts = &gfc_current_block ()->result->ts;
2596 if (ts->type != BT_DERIVED)
2597 gfc_error ("Bad kind expression for function '%s' at %L",
2598 gfc_current_block ()->name,
2599 &gfc_current_block ()->declared_at);
2601 gfc_error ("The type for function '%s' at %L is not accessible",
2602 gfc_current_block ()->name,
2603 &gfc_current_block ()->declared_at);
2605 gfc_current_block ()->ts.kind = 0;
2606 /* Keep the derived type; if it's bad, it will be discovered later. */
2607 if (!(ts->type == BT_DERIVED && ts->u.derived))
2608 ts->type = BT_UNKNOWN;
2615 /* Parse a WHERE block, (not a simple WHERE statement). */
2618 parse_where_block (void)
2620 int seen_empty_else;
2625 accept_statement (ST_WHERE_BLOCK);
2626 top = gfc_state_stack->tail;
2628 push_state (&s, COMP_WHERE, gfc_new_block);
2630 d = add_statement ();
2631 d->expr1 = top->expr1;
2637 seen_empty_else = 0;
2641 st = next_statement ();
2647 case ST_WHERE_BLOCK:
2648 parse_where_block ();
2653 accept_statement (st);
2657 if (seen_empty_else)
2659 gfc_error ("ELSEWHERE statement at %C follows previous "
2660 "unmasked ELSEWHERE");
2664 if (new_st.expr1 == NULL)
2665 seen_empty_else = 1;
2667 d = new_level (gfc_state_stack->head);
2669 d->expr1 = new_st.expr1;
2671 accept_statement (st);
2676 accept_statement (st);
2680 gfc_error ("Unexpected %s statement in WHERE block at %C",
2681 gfc_ascii_statement (st));
2682 reject_statement ();
2686 while (st != ST_END_WHERE);
2692 /* Parse a FORALL block (not a simple FORALL statement). */
2695 parse_forall_block (void)
2701 accept_statement (ST_FORALL_BLOCK);
2702 top = gfc_state_stack->tail;
2704 push_state (&s, COMP_FORALL, gfc_new_block);
2706 d = add_statement ();
2707 d->op = EXEC_FORALL;
2712 st = next_statement ();
2717 case ST_POINTER_ASSIGNMENT:
2720 accept_statement (st);
2723 case ST_WHERE_BLOCK:
2724 parse_where_block ();
2727 case ST_FORALL_BLOCK:
2728 parse_forall_block ();
2732 accept_statement (st);
2739 gfc_error ("Unexpected %s statement in FORALL block at %C",
2740 gfc_ascii_statement (st));
2742 reject_statement ();
2746 while (st != ST_END_FORALL);
2752 static gfc_statement parse_executable (gfc_statement);
2754 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2757 parse_if_block (void)
2766 accept_statement (ST_IF_BLOCK);
2768 top = gfc_state_stack->tail;
2769 push_state (&s, COMP_IF, gfc_new_block);
2771 new_st.op = EXEC_IF;
2772 d = add_statement ();
2774 d->expr1 = top->expr1;
2780 st = parse_executable (ST_NONE);
2790 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2791 "statement at %L", &else_locus);
2793 reject_statement ();
2797 d = new_level (gfc_state_stack->head);
2799 d->expr1 = new_st.expr1;
2801 accept_statement (st);
2808 gfc_error ("Duplicate ELSE statements at %L and %C",
2810 reject_statement ();
2815 else_locus = gfc_current_locus;
2817 d = new_level (gfc_state_stack->head);
2820 accept_statement (st);
2828 unexpected_statement (st);
2832 while (st != ST_ENDIF);
2835 accept_statement (st);
2839 /* Parse a SELECT block. */
2842 parse_select_block (void)
2848 accept_statement (ST_SELECT_CASE);
2850 cp = gfc_state_stack->tail;
2851 push_state (&s, COMP_SELECT, gfc_new_block);
2853 /* Make sure that the next statement is a CASE or END SELECT. */
2856 st = next_statement ();
2859 if (st == ST_END_SELECT)
2861 /* Empty SELECT CASE is OK. */
2862 accept_statement (st);
2869 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2872 reject_statement ();
2875 /* At this point, we're got a nonempty select block. */
2876 cp = new_level (cp);
2879 accept_statement (st);
2883 st = parse_executable (ST_NONE);
2890 cp = new_level (gfc_state_stack->head);
2892 gfc_clear_new_st ();
2894 accept_statement (st);
2900 /* Can't have an executable statement because of
2901 parse_executable(). */
2903 unexpected_statement (st);
2907 while (st != ST_END_SELECT);
2910 accept_statement (st);
2914 /* Pop the current selector from the SELECT TYPE stack. */
2917 select_type_pop (void)
2919 gfc_select_type_stack *old = select_type_stack;
2920 select_type_stack = old->prev;
2925 /* Parse a SELECT TYPE construct (F03:R821). */
2928 parse_select_type_block (void)
2934 accept_statement (ST_SELECT_TYPE);
2936 cp = gfc_state_stack->tail;
2937 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
2939 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
2943 st = next_statement ();
2946 if (st == ST_END_SELECT)
2947 /* Empty SELECT CASE is OK. */
2949 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
2952 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
2953 "following SELECT TYPE at %C");
2955 reject_statement ();
2958 /* At this point, we're got a nonempty select block. */
2959 cp = new_level (cp);
2962 accept_statement (st);
2966 st = parse_executable (ST_NONE);
2974 cp = new_level (gfc_state_stack->head);
2976 gfc_clear_new_st ();
2978 accept_statement (st);
2984 /* Can't have an executable statement because of
2985 parse_executable(). */
2987 unexpected_statement (st);
2991 while (st != ST_END_SELECT);
2995 accept_statement (st);
2996 gfc_current_ns = gfc_current_ns->parent;
3001 /* Given a symbol, make sure it is not an iteration variable for a DO
3002 statement. This subroutine is called when the symbol is seen in a
3003 context that causes it to become redefined. If the symbol is an
3004 iterator, we generate an error message and return nonzero. */
3007 gfc_check_do_variable (gfc_symtree *st)
3011 for (s=gfc_state_stack; s; s = s->previous)
3012 if (s->do_variable == st)
3014 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
3015 "loop beginning at %L", st->name, &s->head->loc);
3023 /* Checks to see if the current statement label closes an enddo.
3024 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3025 an error) if it incorrectly closes an ENDDO. */
3028 check_do_closure (void)
3032 if (gfc_statement_label == NULL)
3035 for (p = gfc_state_stack; p; p = p->previous)
3036 if (p->state == COMP_DO)
3040 return 0; /* No loops to close */
3042 if (p->ext.end_do_label == gfc_statement_label)
3044 if (p == gfc_state_stack)
3047 gfc_error ("End of nonblock DO statement at %C is within another block");
3051 /* At this point, the label doesn't terminate the innermost loop.
3052 Make sure it doesn't terminate another one. */
3053 for (; p; p = p->previous)
3054 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
3056 gfc_error ("End of nonblock DO statement at %C is interwoven "
3057 "with another DO loop");
3065 /* Parse a series of contained program units. */
3067 static void parse_progunit (gfc_statement);
3070 /* Parse a CRITICAL block. */
3073 parse_critical_block (void)
3079 s.ext.end_do_label = new_st.label1;
3081 accept_statement (ST_CRITICAL);
3082 top = gfc_state_stack->tail;
3084 push_state (&s, COMP_CRITICAL, gfc_new_block);
3086 d = add_statement ();
3087 d->op = EXEC_CRITICAL;
3092 st = parse_executable (ST_NONE);
3100 case ST_END_CRITICAL:
3101 if (s.ext.end_do_label != NULL
3102 && s.ext.end_do_label != gfc_statement_label)
3103 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3104 "match CRITIAL label");
3106 if (gfc_statement_label != NULL)
3108 new_st.op = EXEC_NOP;
3114 unexpected_statement (st);
3118 while (st != ST_END_CRITICAL);
3121 accept_statement (st);
3125 /* Set up the local namespace for a BLOCK construct. */
3128 gfc_build_block_ns (gfc_namespace *parent_ns)
3130 gfc_namespace* my_ns;
3132 my_ns = gfc_get_namespace (parent_ns, 1);
3133 my_ns->construct_entities = 1;
3135 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3136 code generation (so it must not be NULL).
3137 We set its recursive argument if our container procedure is recursive, so
3138 that local variables are accordingly placed on the stack when it
3139 will be necessary. */
3141 my_ns->proc_name = gfc_new_block;
3146 gfc_get_symbol ("block@", my_ns, &my_ns->proc_name);
3147 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
3148 my_ns->proc_name->name, NULL);
3149 gcc_assert (t == SUCCESS);
3152 if (parent_ns->proc_name)
3153 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
3159 /* Parse a BLOCK construct. */
3162 parse_block_construct (void)
3164 gfc_namespace* my_ns;
3167 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BLOCK construct at %C");
3169 my_ns = gfc_build_block_ns (gfc_current_ns);
3171 new_st.op = EXEC_BLOCK;
3172 new_st.ext.block.ns = my_ns;
3173 new_st.ext.block.assoc = NULL;
3174 accept_statement (ST_BLOCK);
3176 push_state (&s, COMP_BLOCK, my_ns->proc_name);
3177 gfc_current_ns = my_ns;
3179 parse_progunit (ST_NONE);
3181 gfc_current_ns = gfc_current_ns->parent;
3186 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3187 behind the scenes with compiler-generated variables. */
3190 parse_associate (void)
3192 gfc_namespace* my_ns;
3195 gfc_association_list* a;
3196 gfc_code* assignTail;
3198 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASSOCIATE construct at %C");
3200 my_ns = gfc_build_block_ns (gfc_current_ns);
3202 new_st.op = EXEC_BLOCK;
3203 new_st.ext.block.ns = my_ns;
3204 gcc_assert (new_st.ext.block.assoc);
3206 /* Add all associations to expressions as BLOCK variables, and create
3207 assignments to them giving their values. */
3208 gfc_current_ns = my_ns;
3210 for (a = new_st.ext.block.assoc; a; a = a->next)
3213 gfc_code* newAssign;
3215 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
3218 /* Note that in certain cases, the target-expression's type is not yet
3219 known and so we have to adapt the symbol's ts also during resolution
3221 a->st->n.sym->ts = a->target->ts;
3222 a->st->n.sym->attr.flavor = FL_VARIABLE;
3223 a->st->n.sym->assoc = a;
3224 gfc_set_sym_referenced (a->st->n.sym);
3226 /* Create the assignment to calculate the expression and set it. */
3227 newAssign = gfc_get_code ();
3228 newAssign->op = EXEC_ASSIGN;
3229 newAssign->loc = gfc_current_locus;
3230 newAssign->expr1 = gfc_get_variable_expr (a->st);
3231 newAssign->expr2 = a->target;
3235 assignTail->next = newAssign;
3237 gfc_current_ns->code = newAssign;
3238 assignTail = newAssign;
3242 gfc_error ("Association to variables is not yet supported at %C");
3245 gcc_assert (assignTail);
3247 accept_statement (ST_ASSOCIATE);
3248 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
3251 st = parse_executable (ST_NONE);
3258 accept_statement (st);
3259 assignTail->next = gfc_state_stack->head;
3263 unexpected_statement (st);
3267 gfc_current_ns = gfc_current_ns->parent;
3272 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
3273 handled inside of parse_executable(), because they aren't really
3277 parse_do_block (void)
3284 s.ext.end_do_label = new_st.label1;
3286 if (new_st.ext.iterator != NULL)
3287 stree = new_st.ext.iterator->var->symtree;
3291 accept_statement (ST_DO);
3293 top = gfc_state_stack->tail;
3294 push_state (&s, COMP_DO, gfc_new_block);
3296 s.do_variable = stree;
3298 top->block = new_level (top);
3299 top->block->op = EXEC_DO;
3302 st = parse_executable (ST_NONE);
3310 if (s.ext.end_do_label != NULL
3311 && s.ext.end_do_label != gfc_statement_label)
3312 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
3315 if (gfc_statement_label != NULL)
3317 new_st.op = EXEC_NOP;
3322 case ST_IMPLIED_ENDDO:
3323 /* If the do-stmt of this DO construct has a do-construct-name,
3324 the corresponding end-do must be an end-do-stmt (with a matching
3325 name, but in that case we must have seen ST_ENDDO first).
3326 We only complain about this in pedantic mode. */
3327 if (gfc_current_block () != NULL)
3328 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
3329 &gfc_current_block()->declared_at);
3334 unexpected_statement (st);
3339 accept_statement (st);
3343 /* Parse the statements of OpenMP do/parallel do. */
3345 static gfc_statement
3346 parse_omp_do (gfc_statement omp_st)
3352 accept_statement (omp_st);
3354 cp = gfc_state_stack->tail;
3355 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3356 np = new_level (cp);
3362 st = next_statement ();
3365 else if (st == ST_DO)
3368 unexpected_statement (st);
3372 if (gfc_statement_label != NULL
3373 && gfc_state_stack->previous != NULL
3374 && gfc_state_stack->previous->state == COMP_DO
3375 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
3383 there should be no !$OMP END DO. */
3385 return ST_IMPLIED_ENDDO;
3388 check_do_closure ();
3391 st = next_statement ();
3392 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
3394 if (new_st.op == EXEC_OMP_END_NOWAIT)
3395 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
3397 gcc_assert (new_st.op == EXEC_NOP);
3398 gfc_clear_new_st ();
3399 gfc_commit_symbols ();
3400 gfc_warning_check ();
3401 st = next_statement ();
3407 /* Parse the statements of OpenMP atomic directive. */
3410 parse_omp_atomic (void)
3416 accept_statement (ST_OMP_ATOMIC);
3418 cp = gfc_state_stack->tail;
3419 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3420 np = new_level (cp);
3426 st = next_statement ();
3429 else if (st == ST_ASSIGNMENT)
3432 unexpected_statement (st);
3435 accept_statement (st);
3441 /* Parse the statements of an OpenMP structured block. */
3444 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
3446 gfc_statement st, omp_end_st;
3450 accept_statement (omp_st);
3452 cp = gfc_state_stack->tail;
3453 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
3454 np = new_level (cp);
3460 case ST_OMP_PARALLEL:
3461 omp_end_st = ST_OMP_END_PARALLEL;
3463 case ST_OMP_PARALLEL_SECTIONS:
3464 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
3466 case ST_OMP_SECTIONS:
3467 omp_end_st = ST_OMP_END_SECTIONS;
3469 case ST_OMP_ORDERED:
3470 omp_end_st = ST_OMP_END_ORDERED;
3472 case ST_OMP_CRITICAL:
3473 omp_end_st = ST_OMP_END_CRITICAL;
3476 omp_end_st = ST_OMP_END_MASTER;
3479 omp_end_st = ST_OMP_END_SINGLE;
3482 omp_end_st = ST_OMP_END_TASK;
3484 case ST_OMP_WORKSHARE:
3485 omp_end_st = ST_OMP_END_WORKSHARE;
3487 case ST_OMP_PARALLEL_WORKSHARE:
3488 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
3496 if (workshare_stmts_only)
3498 /* Inside of !$omp workshare, only
3501 where statements and constructs
3502 forall statements and constructs
3506 are allowed. For !$omp critical these
3507 restrictions apply recursively. */
3510 st = next_statement ();
3521 accept_statement (st);
3524 case ST_WHERE_BLOCK:
3525 parse_where_block ();
3528 case ST_FORALL_BLOCK:
3529 parse_forall_block ();
3532 case ST_OMP_PARALLEL:
3533 case ST_OMP_PARALLEL_SECTIONS:
3534 parse_omp_structured_block (st, false);
3537 case ST_OMP_PARALLEL_WORKSHARE:
3538 case ST_OMP_CRITICAL:
3539 parse_omp_structured_block (st, true);
3542 case ST_OMP_PARALLEL_DO:
3543 st = parse_omp_do (st);
3547 parse_omp_atomic ();
3558 st = next_statement ();
3562 st = parse_executable (ST_NONE);
3565 else if (st == ST_OMP_SECTION
3566 && (omp_st == ST_OMP_SECTIONS
3567 || omp_st == ST_OMP_PARALLEL_SECTIONS))
3569 np = new_level (np);
3573 else if (st != omp_end_st)
3574 unexpected_statement (st);
3576 while (st != omp_end_st);
3580 case EXEC_OMP_END_NOWAIT:
3581 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
3583 case EXEC_OMP_CRITICAL:
3584 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
3585 || (new_st.ext.omp_name != NULL
3586 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
3587 gfc_error ("Name after !$omp critical and !$omp end critical does "
3589 gfc_free (CONST_CAST (char *, new_st.ext.omp_name));
3591 case EXEC_OMP_END_SINGLE:
3592 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
3593 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
3594 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
3595 gfc_free_omp_clauses (new_st.ext.omp_clauses);
3603 gfc_clear_new_st ();
3604 gfc_commit_symbols ();
3605 gfc_warning_check ();
3610 /* Accept a series of executable statements. We return the first
3611 statement that doesn't fit to the caller. Any block statements are
3612 passed on to the correct handler, which usually passes the buck
3615 static gfc_statement
3616 parse_executable (gfc_statement st)
3621 st = next_statement ();
3625 close_flag = check_do_closure ();
3630 case ST_END_PROGRAM:
3633 case ST_END_FUNCTION:
3638 case ST_END_SUBROUTINE:
3643 case ST_SELECT_CASE:
3644 gfc_error ("%s statement at %C cannot terminate a non-block "
3645 "DO loop", gfc_ascii_statement (st));
3661 accept_statement (st);
3662 if (close_flag == 1)
3663 return ST_IMPLIED_ENDDO;
3667 parse_block_construct ();
3678 case ST_SELECT_CASE:
3679 parse_select_block ();
3682 case ST_SELECT_TYPE:
3683 parse_select_type_block();
3688 if (check_do_closure () == 1)
3689 return ST_IMPLIED_ENDDO;
3693 parse_critical_block ();
3696 case ST_WHERE_BLOCK:
3697 parse_where_block ();
3700 case ST_FORALL_BLOCK:
3701 parse_forall_block ();
3704 case ST_OMP_PARALLEL:
3705 case ST_OMP_PARALLEL_SECTIONS:
3706 case ST_OMP_SECTIONS:
3707 case ST_OMP_ORDERED:
3708 case ST_OMP_CRITICAL:
3712 parse_omp_structured_block (st, false);
3715 case ST_OMP_WORKSHARE:
3716 case ST_OMP_PARALLEL_WORKSHARE:
3717 parse_omp_structured_block (st, true);
3721 case ST_OMP_PARALLEL_DO:
3722 st = parse_omp_do (st);
3723 if (st == ST_IMPLIED_ENDDO)
3728 parse_omp_atomic ();
3735 st = next_statement ();
3740 /* Fix the symbols for sibling functions. These are incorrectly added to
3741 the child namespace as the parser didn't know about this procedure. */
3744 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
3748 gfc_symbol *old_sym;
3750 sym->attr.referenced = 1;
3751 for (ns = siblings; ns; ns = ns->sibling)
3753 st = gfc_find_symtree (ns->sym_root, sym->name);
3755 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
3756 goto fixup_contained;
3758 old_sym = st->n.sym;
3759 if (old_sym->ns == ns
3760 && !old_sym->attr.contained
3762 /* By 14.6.1.3, host association should be excluded
3763 for the following. */
3764 && !(old_sym->attr.external
3765 || (old_sym->ts.type != BT_UNKNOWN
3766 && !old_sym->attr.implicit_type)
3767 || old_sym->attr.flavor == FL_PARAMETER
3768 || old_sym->attr.use_assoc
3769 || old_sym->attr.in_common
3770 || old_sym->attr.in_equivalence
3771 || old_sym->attr.data
3772 || old_sym->attr.dummy
3773 || old_sym->attr.result
3774 || old_sym->attr.dimension
3775 || old_sym->attr.allocatable
3776 || old_sym->attr.intrinsic
3777 || old_sym->attr.generic
3778 || old_sym->attr.flavor == FL_NAMELIST
3779 || old_sym->attr.proc == PROC_ST_FUNCTION))
3781 /* Replace it with the symbol from the parent namespace. */
3785 /* Free the old (local) symbol. */
3787 if (old_sym->refs == 0)
3788 gfc_free_symbol (old_sym);
3792 /* Do the same for any contained procedures. */
3793 gfc_fixup_sibling_symbols (sym, ns->contained);
3798 parse_contained (int module)
3800 gfc_namespace *ns, *parent_ns, *tmp;
3801 gfc_state_data s1, s2;
3805 int contains_statements = 0;
3808 push_state (&s1, COMP_CONTAINS, NULL);
3809 parent_ns = gfc_current_ns;
3813 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
3815 gfc_current_ns->sibling = parent_ns->contained;
3816 parent_ns->contained = gfc_current_ns;
3819 /* Process the next available statement. We come here if we got an error
3820 and rejected the last statement. */
3821 st = next_statement ();
3830 contains_statements = 1;
3831 accept_statement (st);
3834 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
3837 /* For internal procedures, create/update the symbol in the
3838 parent namespace. */
3842 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
3843 gfc_error ("Contained procedure '%s' at %C is already "
3844 "ambiguous", gfc_new_block->name);
3847 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
3848 &gfc_new_block->declared_at) ==
3851 if (st == ST_FUNCTION)
3852 gfc_add_function (&sym->attr, sym->name,
3853 &gfc_new_block->declared_at);
3855 gfc_add_subroutine (&sym->attr, sym->name,
3856 &gfc_new_block->declared_at);
3860 gfc_commit_symbols ();
3863 sym = gfc_new_block;
3865 /* Mark this as a contained function, so it isn't replaced
3866 by other module functions. */
3867 sym->attr.contained = 1;
3868 sym->attr.referenced = 1;
3870 parse_progunit (ST_NONE);
3872 /* Fix up any sibling functions that refer to this one. */
3873 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
3874 /* Or refer to any of its alternate entry points. */
3875 for (el = gfc_current_ns->entries; el; el = el->next)
3876 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
3878 gfc_current_ns->code = s2.head;
3879 gfc_current_ns = parent_ns;
3884 /* These statements are associated with the end of the host unit. */
3885 case ST_END_FUNCTION:
3887 case ST_END_PROGRAM:
3888 case ST_END_SUBROUTINE:
3889 accept_statement (st);
3893 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
3894 gfc_ascii_statement (st));
3895 reject_statement ();
3901 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
3902 && st != ST_END_MODULE && st != ST_END_PROGRAM);
3904 /* The first namespace in the list is guaranteed to not have
3905 anything (worthwhile) in it. */
3906 tmp = gfc_current_ns;
3907 gfc_current_ns = parent_ns;
3908 if (seen_error && tmp->refs > 1)
3909 gfc_free_namespace (tmp);
3911 ns = gfc_current_ns->contained;
3912 gfc_current_ns->contained = ns->sibling;
3913 gfc_free_namespace (ns);
3916 if (!contains_statements)
3917 gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTAINS statement without "
3918 "FUNCTION or SUBROUTINE statement at %C");
3922 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
3925 parse_progunit (gfc_statement st)
3930 st = parse_spec (st);
3937 /* This is not allowed within BLOCK! */
3938 if (gfc_current_state () != COMP_BLOCK)
3943 accept_statement (st);
3950 if (gfc_current_state () == COMP_FUNCTION)
3951 gfc_check_function_type (gfc_current_ns);
3956 st = parse_executable (st);
3964 /* This is not allowed within BLOCK! */
3965 if (gfc_current_state () != COMP_BLOCK)
3970 accept_statement (st);
3977 unexpected_statement (st);
3978 reject_statement ();
3979 st = next_statement ();
3985 for (p = gfc_state_stack; p; p = p->previous)
3986 if (p->state == COMP_CONTAINS)
3989 if (gfc_find_state (COMP_MODULE) == SUCCESS)
3994 gfc_error ("CONTAINS statement at %C is already in a contained "
3996 st = next_statement ();
4000 parse_contained (0);
4003 gfc_current_ns->code = gfc_state_stack->head;
4007 /* Come here to complain about a global symbol already in use as
4011 gfc_global_used (gfc_gsymbol *sym, locus *where)
4016 where = &gfc_current_locus;
4026 case GSYM_SUBROUTINE:
4027 name = "SUBROUTINE";
4032 case GSYM_BLOCK_DATA:
4033 name = "BLOCK DATA";
4039 gfc_internal_error ("gfc_global_used(): Bad type");
4043 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
4044 sym->name, where, name, &sym->where);
4048 /* Parse a block data program unit. */
4051 parse_block_data (void)
4054 static locus blank_locus;
4055 static int blank_block=0;
4058 gfc_current_ns->proc_name = gfc_new_block;
4059 gfc_current_ns->is_block_data = 1;
4061 if (gfc_new_block == NULL)
4064 gfc_error ("Blank BLOCK DATA at %C conflicts with "
4065 "prior BLOCK DATA at %L", &blank_locus);
4069 blank_locus = gfc_current_locus;
4074 s = gfc_get_gsymbol (gfc_new_block->name);
4076 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
4077 gfc_global_used(s, NULL);
4080 s->type = GSYM_BLOCK_DATA;
4081 s->where = gfc_current_locus;
4086 st = parse_spec (ST_NONE);
4088 while (st != ST_END_BLOCK_DATA)
4090 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
4091 gfc_ascii_statement (st));
4092 reject_statement ();
4093 st = next_statement ();
4098 /* Parse a module subprogram. */
4106 s = gfc_get_gsymbol (gfc_new_block->name);
4107 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
4108 gfc_global_used(s, NULL);
4111 s->type = GSYM_MODULE;
4112 s->where = gfc_current_locus;
4116 st = parse_spec (ST_NONE);
4125 parse_contained (1);
4129 accept_statement (st);
4133 gfc_error ("Unexpected %s statement in MODULE at %C",
4134 gfc_ascii_statement (st));
4136 reject_statement ();
4137 st = next_statement ();
4141 s->ns = gfc_current_ns;
4145 /* Add a procedure name to the global symbol table. */
4148 add_global_procedure (int sub)
4152 s = gfc_get_gsymbol(gfc_new_block->name);
4155 || (s->type != GSYM_UNKNOWN
4156 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
4157 gfc_global_used(s, NULL);
4160 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4161 s->where = gfc_current_locus;
4163 s->ns = gfc_current_ns;
4168 /* Add a program to the global symbol table. */
4171 add_global_program (void)
4175 if (gfc_new_block == NULL)
4177 s = gfc_get_gsymbol (gfc_new_block->name);
4179 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
4180 gfc_global_used(s, NULL);
4183 s->type = GSYM_PROGRAM;
4184 s->where = gfc_current_locus;
4186 s->ns = gfc_current_ns;
4191 /* Resolve all the program units when whole file scope option
4194 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
4196 gfc_free_dt_list ();
4197 gfc_current_ns = gfc_global_ns_list;
4198 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4200 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4201 gfc_resolve (gfc_current_ns);
4202 gfc_current_ns->derived_types = gfc_derived_types;
4203 gfc_derived_types = NULL;
4209 clean_up_modules (gfc_gsymbol *gsym)
4214 clean_up_modules (gsym->left);
4215 clean_up_modules (gsym->right);
4217 if (gsym->type != GSYM_MODULE || !gsym->ns)
4220 gfc_current_ns = gsym->ns;
4221 gfc_derived_types = gfc_current_ns->derived_types;
4228 /* Translate all the program units when whole file scope option
4229 is active. This could be in a different order to resolution if
4230 there are forward references in the file. */
4232 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
4236 gfc_current_ns = gfc_global_ns_list;
4237 gfc_get_errors (NULL, &errors);
4239 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4241 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
4242 gfc_derived_types = gfc_current_ns->derived_types;
4243 gfc_generate_code (gfc_current_ns);
4244 gfc_current_ns->translated = 1;
4247 /* Clean up all the namespaces after translation. */
4248 gfc_current_ns = gfc_global_ns_list;
4249 for (;gfc_current_ns;)
4251 gfc_namespace *ns = gfc_current_ns->sibling;
4252 gfc_derived_types = gfc_current_ns->derived_types;
4254 gfc_current_ns = ns;
4257 clean_up_modules (gfc_gsym_root);
4261 /* Top level parser. */
4264 gfc_parse_file (void)
4266 int seen_program, errors_before, errors;
4267 gfc_state_data top, s;
4270 gfc_namespace *next;
4272 gfc_start_source_files ();
4274 top.state = COMP_NONE;
4276 top.previous = NULL;
4277 top.head = top.tail = NULL;
4278 top.do_variable = NULL;
4280 gfc_state_stack = ⊤
4282 gfc_clear_new_st ();
4284 gfc_statement_label = NULL;
4286 if (setjmp (eof_buf))
4287 return FAILURE; /* Come here on unexpected EOF */
4289 /* Prepare the global namespace that will contain the
4291 gfc_global_ns_list = next = NULL;
4295 /* Exit early for empty files. */
4301 st = next_statement ();
4310 goto duplicate_main;
4312 prog_locus = gfc_current_locus;
4314 push_state (&s, COMP_PROGRAM, gfc_new_block);
4315 main_program_symbol(gfc_current_ns, gfc_new_block->name);
4316 accept_statement (st);
4317 add_global_program ();
4318 parse_progunit (ST_NONE);
4319 if (gfc_option.flag_whole_file)
4324 add_global_procedure (1);
4325 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
4326 accept_statement (st);
4327 parse_progunit (ST_NONE);
4328 if (gfc_option.flag_whole_file)
4333 add_global_procedure (0);
4334 push_state (&s, COMP_FUNCTION, gfc_new_block);
4335 accept_statement (st);
4336 parse_progunit (ST_NONE);
4337 if (gfc_option.flag_whole_file)
4342 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
4343 accept_statement (st);
4344 parse_block_data ();
4348 push_state (&s, COMP_MODULE, gfc_new_block);
4349 accept_statement (st);
4351 gfc_get_errors (NULL, &errors_before);
4355 /* Anything else starts a nameless main program block. */
4358 goto duplicate_main;
4360 prog_locus = gfc_current_locus;
4362 push_state (&s, COMP_PROGRAM, gfc_new_block);
4363 main_program_symbol (gfc_current_ns, "MAIN__");
4364 parse_progunit (st);
4365 if (gfc_option.flag_whole_file)
4370 /* Handle the non-program units. */
4371 gfc_current_ns->code = s.head;
4373 gfc_resolve (gfc_current_ns);
4375 /* Dump the parse tree if requested. */
4376 if (gfc_option.dump_parse_tree)
4377 gfc_dump_parse_tree (gfc_current_ns, stdout);
4379 gfc_get_errors (NULL, &errors);
4380 if (s.state == COMP_MODULE)
4382 gfc_dump_module (s.sym->name, errors_before == errors);
4384 gfc_generate_module_code (gfc_current_ns);
4386 if (!gfc_option.flag_whole_file)
4390 gfc_current_ns->derived_types = gfc_derived_types;
4391 gfc_derived_types = NULL;
4392 gfc_current_ns = NULL;
4398 gfc_generate_code (gfc_current_ns);
4406 /* The main program and non-contained procedures are put
4407 in the global namespace list, so that they can be processed
4408 later and all their interfaces resolved. */
4409 gfc_current_ns->code = s.head;
4411 next->sibling = gfc_current_ns;
4413 gfc_global_ns_list = gfc_current_ns;
4415 next = gfc_current_ns;
4422 if (!gfc_option.flag_whole_file)
4425 /* Do the resolution. */
4426 resolve_all_program_units (gfc_global_ns_list);
4428 /* Do the parse tree dump. */
4430 = gfc_option.dump_parse_tree ? gfc_global_ns_list : NULL;
4432 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
4434 gfc_dump_parse_tree (gfc_current_ns, stdout);
4435 fputs ("------------------------------------------\n\n", stdout);
4438 /* Do the translation. */
4439 translate_all_program_units (gfc_global_ns_list);
4443 gfc_end_source_files ();
4447 /* If we see a duplicate main program, shut down. If the second
4448 instance is an implied main program, i.e. data decls or executable
4449 statements, we're in for lots of errors. */
4450 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
4451 reject_statement ();