2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
30 /* Current statement label. Zero means no statement label. Because new_st
31 can get wiped during statement matching, we have to keep it separate. */
33 gfc_st_label *gfc_statement_label;
35 static locus label_locus;
36 static jmp_buf eof_buf;
38 gfc_state_data *gfc_state_stack;
40 /* TODO: Re-order functions to kill these forward decls. */
41 static void check_statement_label (gfc_statement);
42 static void undo_new_statement (void);
43 static void reject_statement (void);
46 /* A sort of half-matching function. We try to match the word on the
47 input with the passed string. If this succeeds, we call the
48 keyword-dependent matching function that will match the rest of the
49 statement. For single keywords, the matching subroutine is
53 match_word (const char *str, match (*subr) (void), locus *old_locus)
68 gfc_current_locus = *old_locus;
76 /* Figure out what the next statement is, (mostly) regardless of
77 proper ordering. The do...while(0) is there to prevent if/else
80 #define match(keyword, subr, st) \
82 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
85 undo_new_statement (); \
89 /* This is a specialist version of decode_statement that is used
90 for the specification statements in a function, whose
91 characteristics are deferred into the specification statements.
92 eg.: INTEGER (king = mykind) foo ()
93 USE mymodule, ONLY mykind.....
94 The KIND parameter needs a return after USE or IMPORT, whereas
95 derived type declarations can occur anywhere, up the executable
96 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
97 out of the correct kind of specification statements. */
99 decode_specification_statement (void)
105 if (gfc_match_eos () == MATCH_YES)
108 old_locus = gfc_current_locus;
110 match ("import", gfc_match_import, ST_IMPORT);
111 match ("use", gfc_match_use, ST_USE);
113 if (gfc_numeric_ts (&gfc_current_block ()->ts))
116 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
117 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
118 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
120 /* General statement matching: Instead of testing every possible
121 statement, we eliminate most possibilities by peeking at the
124 c = gfc_peek_char ();
129 match ("abstract% interface", gfc_match_abstract_interface,
134 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
141 match ("data", gfc_match_data, ST_DATA);
142 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
146 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
147 match ("entry% ", gfc_match_entry, ST_ENTRY);
148 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
149 match ("external", gfc_match_external, ST_ATTR_DECL);
153 match ("format", gfc_match_format, ST_FORMAT);
160 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
161 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
162 match ("interface", gfc_match_interface, ST_INTERFACE);
163 match ("intent", gfc_match_intent, ST_ATTR_DECL);
164 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
171 match ("namelist", gfc_match_namelist, ST_NAMELIST);
175 match ("optional", gfc_match_optional, ST_ATTR_DECL);
179 match ("parameter", gfc_match_parameter, ST_PARAMETER);
180 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
181 if (gfc_match_private (&st) == MATCH_YES)
183 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
184 if (gfc_match_public (&st) == MATCH_YES)
186 match ("protected", gfc_match_protected, ST_ATTR_DECL);
193 match ("save", gfc_match_save, ST_ATTR_DECL);
197 match ("target", gfc_match_target, ST_ATTR_DECL);
198 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
205 match ("value", gfc_match_value, ST_ATTR_DECL);
206 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
213 /* This is not a specification statement. See if any of the matchers
214 has stored an error message of some sort. */
218 gfc_buffer_error (0);
219 gfc_current_locus = old_locus;
221 return ST_GET_FCN_CHARACTERISTICS;
225 /* This is the primary 'decode_statement'. */
227 decode_statement (void)
238 gfc_clear_error (); /* Clear any pending errors. */
239 gfc_clear_warning (); /* Clear any pending warnings. */
241 gfc_matching_function = false;
243 if (gfc_match_eos () == MATCH_YES)
246 if (gfc_current_state () == COMP_FUNCTION
247 && gfc_current_block ()->result->ts.kind == -1)
248 return decode_specification_statement ();
250 old_locus = gfc_current_locus;
252 /* Try matching a data declaration or function declaration. The
253 input "REALFUNCTIONA(N)" can mean several things in different
254 contexts, so it (and its relatives) get special treatment. */
256 if (gfc_current_state () == COMP_NONE
257 || gfc_current_state () == COMP_INTERFACE
258 || gfc_current_state () == COMP_CONTAINS)
260 gfc_matching_function = true;
261 m = gfc_match_function_decl ();
264 else if (m == MATCH_ERROR)
268 gfc_current_locus = old_locus;
270 gfc_matching_function = false;
273 /* Match statements whose error messages are meant to be overwritten
274 by something better. */
276 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
277 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
278 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
280 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
281 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
283 /* Try to match a subroutine statement, which has the same optional
284 prefixes that functions can have. */
286 if (gfc_match_subroutine () == MATCH_YES)
287 return ST_SUBROUTINE;
289 gfc_current_locus = old_locus;
291 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
292 might begin with a block label. The match functions for these
293 statements are unusual in that their keyword is not seen before
294 the matcher is called. */
296 if (gfc_match_if (&st) == MATCH_YES)
299 gfc_current_locus = old_locus;
301 if (gfc_match_where (&st) == MATCH_YES)
304 gfc_current_locus = old_locus;
306 if (gfc_match_forall (&st) == MATCH_YES)
309 gfc_current_locus = old_locus;
311 match (NULL, gfc_match_do, ST_DO);
312 match (NULL, gfc_match_select, ST_SELECT_CASE);
314 /* General statement matching: Instead of testing every possible
315 statement, we eliminate most possibilities by peeking at the
318 c = gfc_peek_char ();
323 match ("abstract% interface", gfc_match_abstract_interface,
325 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
326 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
327 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
331 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
332 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
333 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
337 match ("call", gfc_match_call, ST_CALL);
338 match ("close", gfc_match_close, ST_CLOSE);
339 match ("continue", gfc_match_continue, ST_CONTINUE);
340 match ("cycle", gfc_match_cycle, ST_CYCLE);
341 match ("case", gfc_match_case, ST_CASE);
342 match ("common", gfc_match_common, ST_COMMON);
343 match ("contains", gfc_match_eos, ST_CONTAINS);
347 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
348 match ("data", gfc_match_data, ST_DATA);
349 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
353 match ("end file", gfc_match_endfile, ST_END_FILE);
354 match ("exit", gfc_match_exit, ST_EXIT);
355 match ("else", gfc_match_else, ST_ELSE);
356 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
357 match ("else if", gfc_match_elseif, ST_ELSEIF);
358 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
360 if (gfc_match_end (&st) == MATCH_YES)
363 match ("entry% ", gfc_match_entry, ST_ENTRY);
364 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
365 match ("external", gfc_match_external, ST_ATTR_DECL);
369 match ("flush", gfc_match_flush, ST_FLUSH);
370 match ("format", gfc_match_format, ST_FORMAT);
374 match ("go to", gfc_match_goto, ST_GOTO);
378 match ("inquire", gfc_match_inquire, ST_INQUIRE);
379 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
380 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
381 match ("import", gfc_match_import, ST_IMPORT);
382 match ("interface", gfc_match_interface, ST_INTERFACE);
383 match ("intent", gfc_match_intent, ST_ATTR_DECL);
384 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
388 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
389 match ("module", gfc_match_module, ST_MODULE);
393 match ("nullify", gfc_match_nullify, ST_NULLIFY);
394 match ("namelist", gfc_match_namelist, ST_NAMELIST);
398 match ("open", gfc_match_open, ST_OPEN);
399 match ("optional", gfc_match_optional, ST_ATTR_DECL);
403 match ("print", gfc_match_print, ST_WRITE);
404 match ("parameter", gfc_match_parameter, ST_PARAMETER);
405 match ("pause", gfc_match_pause, ST_PAUSE);
406 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
407 if (gfc_match_private (&st) == MATCH_YES)
409 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
410 match ("program", gfc_match_program, ST_PROGRAM);
411 if (gfc_match_public (&st) == MATCH_YES)
413 match ("protected", gfc_match_protected, ST_ATTR_DECL);
417 match ("read", gfc_match_read, ST_READ);
418 match ("return", gfc_match_return, ST_RETURN);
419 match ("rewind", gfc_match_rewind, ST_REWIND);
423 match ("sequence", gfc_match_eos, ST_SEQUENCE);
424 match ("stop", gfc_match_stop, ST_STOP);
425 match ("save", gfc_match_save, ST_ATTR_DECL);
429 match ("target", gfc_match_target, ST_ATTR_DECL);
430 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
434 match ("use", gfc_match_use, ST_USE);
438 match ("value", gfc_match_value, ST_ATTR_DECL);
439 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
443 match ("write", gfc_match_write, ST_WRITE);
447 /* All else has failed, so give up. See if any of the matchers has
448 stored an error message of some sort. */
450 if (gfc_error_check () == 0)
451 gfc_error_now ("Unclassifiable statement at %C");
455 gfc_error_recovery ();
461 decode_omp_directive (void)
470 gfc_clear_error (); /* Clear any pending errors. */
471 gfc_clear_warning (); /* Clear any pending warnings. */
475 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
476 "or ELEMENTAL procedures");
477 gfc_error_recovery ();
481 old_locus = gfc_current_locus;
483 /* General OpenMP directive matching: Instead of testing every possible
484 statement, we eliminate most possibilities by peeking at the
487 c = gfc_peek_char ();
492 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
495 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
498 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
501 match ("do", gfc_match_omp_do, ST_OMP_DO);
504 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
505 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
506 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
507 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
508 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
509 match ("end parallel sections", gfc_match_omp_eos,
510 ST_OMP_END_PARALLEL_SECTIONS);
511 match ("end parallel workshare", gfc_match_omp_eos,
512 ST_OMP_END_PARALLEL_WORKSHARE);
513 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
514 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
515 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
516 match ("end workshare", gfc_match_omp_end_nowait,
517 ST_OMP_END_WORKSHARE);
520 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
523 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
526 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
529 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
530 match ("parallel sections", gfc_match_omp_parallel_sections,
531 ST_OMP_PARALLEL_SECTIONS);
532 match ("parallel workshare", gfc_match_omp_parallel_workshare,
533 ST_OMP_PARALLEL_WORKSHARE);
534 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
537 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
538 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
539 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
542 match ("threadprivate", gfc_match_omp_threadprivate,
543 ST_OMP_THREADPRIVATE);
545 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
549 /* All else has failed, so give up. See if any of the matchers has
550 stored an error message of some sort. */
552 if (gfc_error_check () == 0)
553 gfc_error_now ("Unclassifiable OpenMP directive at %C");
557 gfc_error_recovery ();
565 /* Get the next statement in free form source. */
571 int c, d, cnt, at_bol;
573 at_bol = gfc_at_bol ();
574 gfc_gobble_whitespace ();
576 c = gfc_peek_char ();
580 /* Found a statement label? */
581 m = gfc_match_st_label (&gfc_statement_label);
583 d = gfc_peek_char ();
584 if (m != MATCH_YES || !gfc_is_whitespace (d))
586 gfc_match_small_literal_int (&c, &cnt);
589 gfc_error_now ("Too many digits in statement label at %C");
592 gfc_error_now ("Zero is not a valid statement label at %C");
595 c = gfc_next_char ();
598 if (!gfc_is_whitespace (c))
599 gfc_error_now ("Non-numeric character in statement label at %C");
605 label_locus = gfc_current_locus;
607 gfc_gobble_whitespace ();
609 if (at_bol && gfc_peek_char () == ';')
611 gfc_error_now ("Semicolon at %C needs to be preceded by "
613 gfc_next_char (); /* Eat up the semicolon. */
617 if (gfc_match_eos () == MATCH_YES)
619 gfc_warning_now ("Ignoring statement label in empty statement "
621 gfc_free_st_label (gfc_statement_label);
622 gfc_statement_label = NULL;
629 /* Comments have already been skipped by the time we get here,
630 except for OpenMP directives. */
631 if (gfc_option.flag_openmp)
635 c = gfc_next_char ();
636 for (i = 0; i < 5; i++, c = gfc_next_char ())
637 gcc_assert (c == "!$omp"[i]);
639 gcc_assert (c == ' ');
640 gfc_gobble_whitespace ();
641 return decode_omp_directive ();
645 if (at_bol && c == ';')
647 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
648 gfc_next_char (); /* Eat up the semicolon. */
652 return decode_statement ();
656 /* Get the next statement in fixed-form source. */
661 int label, digit_flag, i;
666 return decode_statement ();
668 /* Skip past the current label field, parsing a statement label if
669 one is there. This is a weird number parser, since the number is
670 contained within five columns and can have any kind of embedded
671 spaces. We also check for characters that make the rest of the
677 for (i = 0; i < 5; i++)
679 c = gfc_next_char_literal (0);
696 label = label * 10 + c - '0';
697 label_locus = gfc_current_locus;
701 /* Comments have already been skipped by the time we get
702 here, except for OpenMP directives. */
704 if (gfc_option.flag_openmp)
706 for (i = 0; i < 5; i++, c = gfc_next_char_literal (0))
707 gcc_assert (TOLOWER (c) == "*$omp"[i]);
709 if (c != ' ' && c != '0')
711 gfc_buffer_error (0);
712 gfc_error ("Bad continuation line at %C");
716 return decode_omp_directive ();
720 /* Comments have already been skipped by the time we get
721 here so don't bother checking for them. */
724 gfc_buffer_error (0);
725 gfc_error ("Non-numeric character in statement label at %C");
733 gfc_warning_now ("Zero is not a valid statement label at %C");
736 /* We've found a valid statement label. */
737 gfc_statement_label = gfc_get_st_label (label);
741 /* Since this line starts a statement, it cannot be a continuation
742 of a previous statement. If we see something here besides a
743 space or zero, it must be a bad continuation line. */
745 c = gfc_next_char_literal (0);
749 if (c != ' ' && c != '0')
751 gfc_buffer_error (0);
752 gfc_error ("Bad continuation line at %C");
756 /* Now that we've taken care of the statement label columns, we have
757 to make sure that the first nonblank character is not a '!'. If
758 it is, the rest of the line is a comment. */
762 loc = gfc_current_locus;
763 c = gfc_next_char_literal (0);
765 while (gfc_is_whitespace (c));
769 gfc_current_locus = loc;
773 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
777 if (gfc_match_eos () == MATCH_YES)
780 /* At this point, we've got a nonblank statement to parse. */
781 return decode_statement ();
785 gfc_warning ("Ignoring statement label in empty statement at %C");
791 /* Return the next non-ST_NONE statement to the caller. We also worry
792 about including files and the ends of include files at this stage. */
795 next_statement (void)
799 gfc_new_block = NULL;
803 gfc_statement_label = NULL;
804 gfc_buffer_error (1);
808 if ((gfc_option.warn_line_truncation || gfc_current_form == FORM_FREE)
809 && gfc_current_locus.lb
810 && gfc_current_locus.lb->truncated)
811 gfc_warning_now ("Line truncated at %C");
816 gfc_skip_comments ();
824 if (gfc_define_undef_line ())
827 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
833 gfc_buffer_error (0);
836 check_statement_label (st);
842 /****************************** Parser ***********************************/
844 /* The parser subroutines are of type 'try' that fail if the file ends
847 /* Macros that expand to case-labels for various classes of
848 statements. Start with executable statements that directly do
851 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
852 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
853 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
854 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
855 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
856 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
857 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
858 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
861 /* Statements that mark other executable statements. */
863 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
864 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
865 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
866 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
867 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
868 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
870 /* Declaration statements */
872 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
873 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
874 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
877 /* Block end statements. Errors associated with interchanging these
878 are detected in gfc_match_end(). */
880 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
881 case ST_END_PROGRAM: case ST_END_SUBROUTINE
884 /* Push a new state onto the stack. */
887 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
889 p->state = new_state;
890 p->previous = gfc_state_stack;
892 p->head = p->tail = NULL;
893 p->do_variable = NULL;
898 /* Pop the current state. */
902 gfc_state_stack = gfc_state_stack->previous;
906 /* Try to find the given state in the state stack. */
909 gfc_find_state (gfc_compile_state state)
913 for (p = gfc_state_stack; p; p = p->previous)
914 if (p->state == state)
917 return (p == NULL) ? FAILURE : SUCCESS;
921 /* Starts a new level in the statement list. */
924 new_level (gfc_code *q)
928 p = q->block = gfc_get_code ();
930 gfc_state_stack->head = gfc_state_stack->tail = p;
936 /* Add the current new_st code structure and adds it to the current
937 program unit. As a side-effect, it zeroes the new_st. */
947 p->loc = gfc_current_locus;
949 if (gfc_state_stack->head == NULL)
950 gfc_state_stack->head = p;
952 gfc_state_stack->tail->next = p;
954 while (p->next != NULL)
957 gfc_state_stack->tail = p;
965 /* Frees everything associated with the current statement. */
968 undo_new_statement (void)
970 gfc_free_statements (new_st.block);
971 gfc_free_statements (new_st.next);
972 gfc_free_statement (&new_st);
977 /* If the current statement has a statement label, make sure that it
978 is allowed to, or should have one. */
981 check_statement_label (gfc_statement st)
985 if (gfc_statement_label == NULL)
988 gfc_error ("FORMAT statement at %L does not have a statement label",
996 case ST_END_FUNCTION:
997 case ST_END_SUBROUTINE:
1003 type = ST_LABEL_TARGET;
1007 type = ST_LABEL_FORMAT;
1010 /* Statement labels are not restricted from appearing on a
1011 particular line. However, there are plenty of situations
1012 where the resulting label can't be referenced. */
1015 type = ST_LABEL_BAD_TARGET;
1019 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1021 new_st.here = gfc_statement_label;
1025 /* Figures out what the enclosing program unit is. This will be a
1026 function, subroutine, program, block data or module. */
1029 gfc_enclosing_unit (gfc_compile_state * result)
1033 for (p = gfc_state_stack; p; p = p->previous)
1034 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1035 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
1036 || p->state == COMP_PROGRAM)
1045 *result = COMP_PROGRAM;
1050 /* Translate a statement enum to a string. */
1053 gfc_ascii_statement (gfc_statement st)
1059 case ST_ARITHMETIC_IF:
1060 p = _("arithmetic IF");
1066 p = _("attribute declaration");
1096 p = _("data declaration");
1104 case ST_DERIVED_DECL:
1105 p = _("derived type declaration");
1119 case ST_END_BLOCK_DATA:
1120 p = "END BLOCK DATA";
1131 case ST_END_FUNCTION:
1137 case ST_END_INTERFACE:
1138 p = "END INTERFACE";
1143 case ST_END_PROGRAM:
1149 case ST_END_SUBROUTINE:
1150 p = "END SUBROUTINE";
1161 case ST_EQUIVALENCE:
1170 case ST_FORALL_BLOCK: /* Fall through */
1189 case ST_IMPLICIT_NONE:
1190 p = "IMPLICIT NONE";
1192 case ST_IMPLIED_ENDDO:
1193 p = _("implied END DO");
1219 case ST_MODULE_PROC:
1220 p = "MODULE PROCEDURE";
1258 case ST_WHERE_BLOCK: /* Fall through */
1266 p = _("assignment");
1268 case ST_POINTER_ASSIGNMENT:
1269 p = _("pointer assignment");
1271 case ST_SELECT_CASE:
1280 case ST_STATEMENT_FUNCTION:
1281 p = "STATEMENT FUNCTION";
1283 case ST_LABEL_ASSIGNMENT:
1284 p = "LABEL ASSIGNMENT";
1287 p = "ENUM DEFINITION";
1290 p = "ENUMERATOR DEFINITION";
1298 case ST_OMP_BARRIER:
1299 p = "!$OMP BARRIER";
1301 case ST_OMP_CRITICAL:
1302 p = "!$OMP CRITICAL";
1307 case ST_OMP_END_CRITICAL:
1308 p = "!$OMP END CRITICAL";
1313 case ST_OMP_END_MASTER:
1314 p = "!$OMP END MASTER";
1316 case ST_OMP_END_ORDERED:
1317 p = "!$OMP END ORDERED";
1319 case ST_OMP_END_PARALLEL:
1320 p = "!$OMP END PARALLEL";
1322 case ST_OMP_END_PARALLEL_DO:
1323 p = "!$OMP END PARALLEL DO";
1325 case ST_OMP_END_PARALLEL_SECTIONS:
1326 p = "!$OMP END PARALLEL SECTIONS";
1328 case ST_OMP_END_PARALLEL_WORKSHARE:
1329 p = "!$OMP END PARALLEL WORKSHARE";
1331 case ST_OMP_END_SECTIONS:
1332 p = "!$OMP END SECTIONS";
1334 case ST_OMP_END_SINGLE:
1335 p = "!$OMP END SINGLE";
1337 case ST_OMP_END_WORKSHARE:
1338 p = "!$OMP END WORKSHARE";
1346 case ST_OMP_ORDERED:
1347 p = "!$OMP ORDERED";
1349 case ST_OMP_PARALLEL:
1350 p = "!$OMP PARALLEL";
1352 case ST_OMP_PARALLEL_DO:
1353 p = "!$OMP PARALLEL DO";
1355 case ST_OMP_PARALLEL_SECTIONS:
1356 p = "!$OMP PARALLEL SECTIONS";
1358 case ST_OMP_PARALLEL_WORKSHARE:
1359 p = "!$OMP PARALLEL WORKSHARE";
1361 case ST_OMP_SECTIONS:
1362 p = "!$OMP SECTIONS";
1364 case ST_OMP_SECTION:
1365 p = "!$OMP SECTION";
1370 case ST_OMP_THREADPRIVATE:
1371 p = "!$OMP THREADPRIVATE";
1373 case ST_OMP_WORKSHARE:
1374 p = "!$OMP WORKSHARE";
1377 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1384 /* Create a symbol for the main program and assign it to ns->proc_name. */
1387 main_program_symbol (gfc_namespace *ns, const char *name)
1389 gfc_symbol *main_program;
1390 symbol_attribute attr;
1392 gfc_get_symbol (name, ns, &main_program);
1393 gfc_clear_attr (&attr);
1394 attr.flavor = FL_PROGRAM;
1395 attr.proc = PROC_UNKNOWN;
1396 attr.subroutine = 1;
1397 attr.access = ACCESS_PUBLIC;
1398 attr.is_main_program = 1;
1399 main_program->attr = attr;
1400 main_program->declared_at = gfc_current_locus;
1401 ns->proc_name = main_program;
1402 gfc_commit_symbols ();
1406 /* Do whatever is necessary to accept the last statement. */
1409 accept_statement (gfc_statement st)
1417 case ST_IMPLICIT_NONE:
1418 gfc_set_implicit_none ();
1427 gfc_current_ns->proc_name = gfc_new_block;
1430 /* If the statement is the end of a block, lay down a special code
1431 that allows a branch to the end of the block from within the
1436 if (gfc_statement_label != NULL)
1438 new_st.op = EXEC_NOP;
1444 /* The end-of-program unit statements do not get the special
1445 marker and require a statement of some sort if they are a
1448 case ST_END_PROGRAM:
1449 case ST_END_FUNCTION:
1450 case ST_END_SUBROUTINE:
1451 if (gfc_statement_label != NULL)
1453 new_st.op = EXEC_RETURN;
1469 gfc_commit_symbols ();
1470 gfc_warning_check ();
1471 gfc_clear_new_st ();
1475 /* Undo anything tentative that has been built for the current
1479 reject_statement (void)
1481 gfc_new_block = NULL;
1482 gfc_undo_symbols ();
1483 gfc_clear_warning ();
1484 undo_new_statement ();
1488 /* Generic complaint about an out of order statement. We also do
1489 whatever is necessary to clean up. */
1492 unexpected_statement (gfc_statement st)
1494 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1496 reject_statement ();
1500 /* Given the next statement seen by the matcher, make sure that it is
1501 in proper order with the last. This subroutine is initialized by
1502 calling it with an argument of ST_NONE. If there is a problem, we
1503 issue an error and return FAILURE. Otherwise we return SUCCESS.
1505 Individual parsers need to verify that the statements seen are
1506 valid before calling here, ie ENTRY statements are not allowed in
1507 INTERFACE blocks. The following diagram is taken from the standard:
1509 +---------------------------------------+
1510 | program subroutine function module |
1511 +---------------------------------------+
1513 +---------------------------------------+
1515 +---------------------------------------+
1517 | +-----------+------------------+
1518 | | parameter | implicit |
1519 | +-----------+------------------+
1520 | format | | derived type |
1521 | entry | parameter | interface |
1522 | | data | specification |
1523 | | | statement func |
1524 | +-----------+------------------+
1525 | | data | executable |
1526 +--------+-----------+------------------+
1528 +---------------------------------------+
1529 | internal module/subprogram |
1530 +---------------------------------------+
1532 +---------------------------------------+
1539 { ORDER_START, ORDER_USE, ORDER_IMPORT, ORDER_IMPLICIT_NONE,
1540 ORDER_IMPLICIT, ORDER_SPEC, ORDER_EXEC
1543 gfc_statement last_statement;
1549 verify_st_order (st_state *p, gfc_statement st)
1555 p->state = ORDER_START;
1559 if (p->state > ORDER_USE)
1561 p->state = ORDER_USE;
1565 if (p->state > ORDER_IMPORT)
1567 p->state = ORDER_IMPORT;
1570 case ST_IMPLICIT_NONE:
1571 if (p->state > ORDER_IMPLICIT_NONE)
1574 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1575 statement disqualifies a USE but not an IMPLICIT NONE.
1576 Duplicate IMPLICIT NONEs are caught when the implicit types
1579 p->state = ORDER_IMPLICIT_NONE;
1583 if (p->state > ORDER_IMPLICIT)
1585 p->state = ORDER_IMPLICIT;
1590 if (p->state < ORDER_IMPLICIT_NONE)
1591 p->state = ORDER_IMPLICIT_NONE;
1595 if (p->state >= ORDER_EXEC)
1597 if (p->state < ORDER_IMPLICIT)
1598 p->state = ORDER_IMPLICIT;
1602 if (p->state < ORDER_SPEC)
1603 p->state = ORDER_SPEC;
1608 case ST_DERIVED_DECL:
1610 if (p->state >= ORDER_EXEC)
1612 if (p->state < ORDER_SPEC)
1613 p->state = ORDER_SPEC;
1618 if (p->state < ORDER_EXEC)
1619 p->state = ORDER_EXEC;
1623 gfc_internal_error ("Unexpected %s statement in verify_st_order() at %C",
1624 gfc_ascii_statement (st));
1627 /* All is well, record the statement in case we need it next time. */
1628 p->where = gfc_current_locus;
1629 p->last_statement = st;
1633 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1634 gfc_ascii_statement (st),
1635 gfc_ascii_statement (p->last_statement), &p->where);
1641 /* Handle an unexpected end of file. This is a show-stopper... */
1643 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1646 unexpected_eof (void)
1650 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1652 /* Memory cleanup. Move to "second to last". */
1653 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1656 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1659 longjmp (eof_buf, 1);
1663 /* Parse a derived type. */
1666 parse_derived (void)
1668 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1671 gfc_symbol *derived_sym = NULL;
1677 accept_statement (ST_DERIVED_DECL);
1678 push_state (&s, COMP_DERIVED, gfc_new_block);
1680 gfc_new_block->component_access = ACCESS_PUBLIC;
1687 while (compiling_type)
1689 st = next_statement ();
1697 accept_statement (st);
1705 && (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Derived type "
1706 "definition at %C without components")
1710 accept_statement (ST_END_TYPE);
1714 if (gfc_find_state (COMP_MODULE) == FAILURE)
1716 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
1724 gfc_error ("PRIVATE statement at %C must precede "
1725 "structure components");
1732 gfc_error ("Duplicate PRIVATE statement at %C");
1736 s.sym->component_access = ACCESS_PRIVATE;
1737 accept_statement (ST_PRIVATE);
1744 gfc_error ("SEQUENCE statement at %C must precede "
1745 "structure components");
1750 if (gfc_current_block ()->attr.sequence)
1751 gfc_warning ("SEQUENCE attribute at %C already specified in "
1756 gfc_error ("Duplicate SEQUENCE statement at %C");
1761 gfc_add_sequence (&gfc_current_block ()->attr,
1762 gfc_current_block ()->name, NULL);
1766 unexpected_statement (st);
1771 /* need to verify that all fields of the derived type are
1772 * interoperable with C if the type is declared to be bind(c)
1774 derived_sym = gfc_current_block();
1776 sym = gfc_current_block ();
1777 for (c = sym->components; c; c = c->next)
1779 /* Look for allocatable components. */
1781 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.alloc_comp))
1783 sym->attr.alloc_comp = 1;
1787 /* Look for pointer components. */
1789 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.pointer_comp))
1791 sym->attr.pointer_comp = 1;
1795 /* Look for private components. */
1796 if (sym->component_access == ACCESS_PRIVATE
1797 || c->access == ACCESS_PRIVATE
1798 || (c->ts.type == BT_DERIVED && c->ts.derived->attr.private_comp))
1800 sym->attr.private_comp = 1;
1805 if (!seen_component)
1806 sym->attr.zero_comp = 1;
1812 /* Parse an ENUM. */
1821 int seen_enumerator = 0;
1825 push_state (&s, COMP_ENUM, gfc_new_block);
1829 while (compiling_enum)
1831 st = next_statement ();
1839 seen_enumerator = 1;
1840 accept_statement (st);
1845 if (!seen_enumerator)
1847 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1850 accept_statement (st);
1854 gfc_free_enum_history ();
1855 unexpected_statement (st);
1863 /* Parse an interface. We must be able to deal with the possibility
1864 of recursive interfaces. The parse_spec() subroutine is mutually
1865 recursive with parse_interface(). */
1867 static gfc_statement parse_spec (gfc_statement);
1870 parse_interface (void)
1872 gfc_compile_state new_state, current_state;
1873 gfc_symbol *prog_unit, *sym;
1874 gfc_interface_info save;
1875 gfc_state_data s1, s2;
1879 accept_statement (ST_INTERFACE);
1881 current_interface.ns = gfc_current_ns;
1882 save = current_interface;
1884 sym = (current_interface.type == INTERFACE_GENERIC
1885 || current_interface.type == INTERFACE_USER_OP)
1886 ? gfc_new_block : NULL;
1888 push_state (&s1, COMP_INTERFACE, sym);
1889 current_state = COMP_NONE;
1892 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1894 st = next_statement ();
1901 new_state = COMP_SUBROUTINE;
1902 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1903 gfc_new_block->formal, NULL);
1907 new_state = COMP_FUNCTION;
1908 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1909 gfc_new_block->formal, NULL);
1913 case ST_MODULE_PROC: /* The module procedure matcher makes
1914 sure the context is correct. */
1915 accept_statement (st);
1916 gfc_free_namespace (gfc_current_ns);
1919 case ST_END_INTERFACE:
1920 gfc_free_namespace (gfc_current_ns);
1921 gfc_current_ns = current_interface.ns;
1925 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1926 gfc_ascii_statement (st));
1927 reject_statement ();
1928 gfc_free_namespace (gfc_current_ns);
1933 /* Make sure that a generic interface has only subroutines or
1934 functions and that the generic name has the right attribute. */
1935 if (current_interface.type == INTERFACE_GENERIC)
1937 if (current_state == COMP_NONE)
1939 if (new_state == COMP_FUNCTION)
1940 gfc_add_function (&sym->attr, sym->name, NULL);
1941 else if (new_state == COMP_SUBROUTINE)
1942 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1944 current_state = new_state;
1948 if (new_state != current_state)
1950 if (new_state == COMP_SUBROUTINE)
1951 gfc_error ("SUBROUTINE at %C does not belong in a "
1952 "generic function interface");
1954 if (new_state == COMP_FUNCTION)
1955 gfc_error ("FUNCTION at %C does not belong in a "
1956 "generic subroutine interface");
1961 if (current_interface.type == INTERFACE_ABSTRACT)
1963 gfc_new_block->attr.abstract = 1;
1964 if (gfc_is_intrinsic_typename (gfc_new_block->name))
1965 gfc_error ("Name '%s' of ABSTRACT INTERFACE at %C "
1966 "cannot be the same as an intrinsic type",
1967 gfc_new_block->name);
1970 push_state (&s2, new_state, gfc_new_block);
1971 accept_statement (st);
1972 prog_unit = gfc_new_block;
1973 prog_unit->formal_ns = gfc_current_ns;
1974 proc_locus = gfc_current_locus;
1977 /* Read data declaration statements. */
1978 st = parse_spec (ST_NONE);
1980 /* Since the interface block does not permit an IMPLICIT statement,
1981 the default type for the function or the result must be taken
1982 from the formal namespace. */
1983 if (new_state == COMP_FUNCTION)
1985 if (prog_unit->result == prog_unit
1986 && prog_unit->ts.type == BT_UNKNOWN)
1987 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
1988 else if (prog_unit->result != prog_unit
1989 && prog_unit->result->ts.type == BT_UNKNOWN)
1990 gfc_set_default_type (prog_unit->result, 1,
1991 prog_unit->formal_ns);
1994 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1996 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1997 gfc_ascii_statement (st));
1998 reject_statement ();
2002 current_interface = save;
2003 gfc_add_interface (prog_unit);
2006 if (current_interface.ns
2007 && current_interface.ns->proc_name
2008 && strcmp (current_interface.ns->proc_name->name,
2009 prog_unit->name) == 0)
2010 gfc_error ("INTERFACE procedure '%s' at %L has the same name as the "
2011 "enclosing procedure", prog_unit->name, &proc_locus);
2020 /* Associate function characteristics by going back to the function
2021 declaration and rematching the prefix. */
2024 match_deferred_characteristics (gfc_typespec * ts)
2027 match m = MATCH_ERROR;
2028 char name[GFC_MAX_SYMBOL_LEN + 1];
2030 loc = gfc_current_locus;
2032 gfc_current_locus = gfc_current_block ()->declared_at;
2035 gfc_buffer_error (1);
2036 m = gfc_match_prefix (ts);
2037 gfc_buffer_error (0);
2039 if (ts->type == BT_DERIVED)
2043 if (!ts->derived || !ts->derived->components)
2047 /* Only permit one go at the characteristic association. */
2051 /* Set the function locus correctly. If we have not found the
2052 function name, there is an error. */
2053 gfc_match ("function% %n", name);
2054 if (m == MATCH_YES && strcmp (name, gfc_current_block ()->name) == 0)
2056 gfc_current_block ()->declared_at = gfc_current_locus;
2057 gfc_commit_symbols ();
2062 gfc_current_locus =loc;
2067 /* Parse a set of specification statements. Returns the statement
2068 that doesn't fit. */
2070 static gfc_statement
2071 parse_spec (gfc_statement st)
2074 bool bad_characteristic = false;
2077 verify_st_order (&ss, ST_NONE);
2079 st = next_statement ();
2089 case ST_DATA: /* Not allowed in interfaces */
2090 if (gfc_current_state () == COMP_INTERFACE)
2097 case ST_IMPLICIT_NONE:
2102 case ST_DERIVED_DECL:
2104 if (verify_st_order (&ss, st) == FAILURE)
2106 reject_statement ();
2107 st = next_statement ();
2117 case ST_DERIVED_DECL:
2123 if (gfc_current_state () != COMP_MODULE)
2125 gfc_error ("%s statement must appear in a MODULE",
2126 gfc_ascii_statement (st));
2130 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
2132 gfc_error ("%s statement at %C follows another accessibility "
2133 "specification", gfc_ascii_statement (st));
2137 gfc_current_ns->default_access = (st == ST_PUBLIC)
2138 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2142 case ST_STATEMENT_FUNCTION:
2143 if (gfc_current_state () == COMP_MODULE)
2145 unexpected_statement (st);
2153 accept_statement (st);
2154 st = next_statement ();
2158 accept_statement (st);
2160 st = next_statement ();
2163 case ST_GET_FCN_CHARACTERISTICS:
2164 /* This statement triggers the association of a function's result
2166 ts = &gfc_current_block ()->result->ts;
2167 if (match_deferred_characteristics (ts) != MATCH_YES)
2168 bad_characteristic = true;
2170 st = next_statement ();
2177 /* If match_deferred_characteristics failed, then there is an error. */
2178 if (bad_characteristic)
2180 ts = &gfc_current_block ()->result->ts;
2181 if (ts->type != BT_DERIVED)
2182 gfc_error ("Bad kind expression for function '%s' at %L",
2183 gfc_current_block ()->name,
2184 &gfc_current_block ()->declared_at);
2186 gfc_error ("The type for function '%s' at %L is not accessible",
2187 gfc_current_block ()->name,
2188 &gfc_current_block ()->declared_at);
2190 gfc_current_block ()->ts.kind = 0;
2191 /* Keep the derived type; if it's bad, it will be discovered later. */
2192 if (!(ts->type == BT_DERIVED && ts->derived))
2193 ts->type = BT_UNKNOWN;
2200 /* Parse a WHERE block, (not a simple WHERE statement). */
2203 parse_where_block (void)
2205 int seen_empty_else;
2210 accept_statement (ST_WHERE_BLOCK);
2211 top = gfc_state_stack->tail;
2213 push_state (&s, COMP_WHERE, gfc_new_block);
2215 d = add_statement ();
2216 d->expr = top->expr;
2222 seen_empty_else = 0;
2226 st = next_statement ();
2232 case ST_WHERE_BLOCK:
2233 parse_where_block ();
2238 accept_statement (st);
2242 if (seen_empty_else)
2244 gfc_error ("ELSEWHERE statement at %C follows previous "
2245 "unmasked ELSEWHERE");
2249 if (new_st.expr == NULL)
2250 seen_empty_else = 1;
2252 d = new_level (gfc_state_stack->head);
2254 d->expr = new_st.expr;
2256 accept_statement (st);
2261 accept_statement (st);
2265 gfc_error ("Unexpected %s statement in WHERE block at %C",
2266 gfc_ascii_statement (st));
2267 reject_statement ();
2271 while (st != ST_END_WHERE);
2277 /* Parse a FORALL block (not a simple FORALL statement). */
2280 parse_forall_block (void)
2286 accept_statement (ST_FORALL_BLOCK);
2287 top = gfc_state_stack->tail;
2289 push_state (&s, COMP_FORALL, gfc_new_block);
2291 d = add_statement ();
2292 d->op = EXEC_FORALL;
2297 st = next_statement ();
2302 case ST_POINTER_ASSIGNMENT:
2305 accept_statement (st);
2308 case ST_WHERE_BLOCK:
2309 parse_where_block ();
2312 case ST_FORALL_BLOCK:
2313 parse_forall_block ();
2317 accept_statement (st);
2324 gfc_error ("Unexpected %s statement in FORALL block at %C",
2325 gfc_ascii_statement (st));
2327 reject_statement ();
2331 while (st != ST_END_FORALL);
2337 static gfc_statement parse_executable (gfc_statement);
2339 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2342 parse_if_block (void)
2351 accept_statement (ST_IF_BLOCK);
2353 top = gfc_state_stack->tail;
2354 push_state (&s, COMP_IF, gfc_new_block);
2356 new_st.op = EXEC_IF;
2357 d = add_statement ();
2359 d->expr = top->expr;
2365 st = parse_executable (ST_NONE);
2375 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
2376 "statement at %L", &else_locus);
2378 reject_statement ();
2382 d = new_level (gfc_state_stack->head);
2384 d->expr = new_st.expr;
2386 accept_statement (st);
2393 gfc_error ("Duplicate ELSE statements at %L and %C",
2395 reject_statement ();
2400 else_locus = gfc_current_locus;
2402 d = new_level (gfc_state_stack->head);
2405 accept_statement (st);
2413 unexpected_statement (st);
2417 while (st != ST_ENDIF);
2420 accept_statement (st);
2424 /* Parse a SELECT block. */
2427 parse_select_block (void)
2433 accept_statement (ST_SELECT_CASE);
2435 cp = gfc_state_stack->tail;
2436 push_state (&s, COMP_SELECT, gfc_new_block);
2438 /* Make sure that the next statement is a CASE or END SELECT. */
2441 st = next_statement ();
2444 if (st == ST_END_SELECT)
2446 /* Empty SELECT CASE is OK. */
2447 accept_statement (st);
2454 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
2457 reject_statement ();
2460 /* At this point, we're got a nonempty select block. */
2461 cp = new_level (cp);
2464 accept_statement (st);
2468 st = parse_executable (ST_NONE);
2475 cp = new_level (gfc_state_stack->head);
2477 gfc_clear_new_st ();
2479 accept_statement (st);
2485 /* Can't have an executable statement because of
2486 parse_executable(). */
2488 unexpected_statement (st);
2492 while (st != ST_END_SELECT);
2495 accept_statement (st);
2499 /* Given a symbol, make sure it is not an iteration variable for a DO
2500 statement. This subroutine is called when the symbol is seen in a
2501 context that causes it to become redefined. If the symbol is an
2502 iterator, we generate an error message and return nonzero. */
2505 gfc_check_do_variable (gfc_symtree *st)
2509 for (s=gfc_state_stack; s; s = s->previous)
2510 if (s->do_variable == st)
2512 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2513 "loop beginning at %L", st->name, &s->head->loc);
2521 /* Checks to see if the current statement label closes an enddo.
2522 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2523 an error) if it incorrectly closes an ENDDO. */
2526 check_do_closure (void)
2530 if (gfc_statement_label == NULL)
2533 for (p = gfc_state_stack; p; p = p->previous)
2534 if (p->state == COMP_DO)
2538 return 0; /* No loops to close */
2540 if (p->ext.end_do_label == gfc_statement_label)
2543 if (p == gfc_state_stack)
2546 gfc_error ("End of nonblock DO statement at %C is within another block");
2550 /* At this point, the label doesn't terminate the innermost loop.
2551 Make sure it doesn't terminate another one. */
2552 for (; p; p = p->previous)
2553 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
2555 gfc_error ("End of nonblock DO statement at %C is interwoven "
2556 "with another DO loop");
2564 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2565 handled inside of parse_executable(), because they aren't really
2569 parse_do_block (void)
2576 s.ext.end_do_label = new_st.label;
2578 if (new_st.ext.iterator != NULL)
2579 stree = new_st.ext.iterator->var->symtree;
2583 accept_statement (ST_DO);
2585 top = gfc_state_stack->tail;
2586 push_state (&s, COMP_DO, gfc_new_block);
2588 s.do_variable = stree;
2590 top->block = new_level (top);
2591 top->block->op = EXEC_DO;
2594 st = parse_executable (ST_NONE);
2602 if (s.ext.end_do_label != NULL
2603 && s.ext.end_do_label != gfc_statement_label)
2604 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
2607 if (gfc_statement_label != NULL)
2609 new_st.op = EXEC_NOP;
2614 case ST_IMPLIED_ENDDO:
2615 /* If the do-stmt of this DO construct has a do-construct-name,
2616 the corresponding end-do must be an end-do-stmt (with a matching
2617 name, but in that case we must have seen ST_ENDDO first).
2618 We only complain about this in pedantic mode. */
2619 if (gfc_current_block () != NULL)
2620 gfc_error_now ("named block DO at %L requires matching ENDDO name",
2621 &gfc_current_block()->declared_at);
2626 unexpected_statement (st);
2631 accept_statement (st);
2635 /* Parse the statements of OpenMP do/parallel do. */
2637 static gfc_statement
2638 parse_omp_do (gfc_statement omp_st)
2644 accept_statement (omp_st);
2646 cp = gfc_state_stack->tail;
2647 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2648 np = new_level (cp);
2654 st = next_statement ();
2657 else if (st == ST_DO)
2660 unexpected_statement (st);
2664 if (gfc_statement_label != NULL
2665 && gfc_state_stack->previous != NULL
2666 && gfc_state_stack->previous->state == COMP_DO
2667 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
2675 there should be no !$OMP END DO. */
2677 return ST_IMPLIED_ENDDO;
2680 check_do_closure ();
2683 st = next_statement ();
2684 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
2686 if (new_st.op == EXEC_OMP_END_NOWAIT)
2687 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2689 gcc_assert (new_st.op == EXEC_NOP);
2690 gfc_clear_new_st ();
2691 gfc_commit_symbols ();
2692 gfc_warning_check ();
2693 st = next_statement ();
2699 /* Parse the statements of OpenMP atomic directive. */
2702 parse_omp_atomic (void)
2708 accept_statement (ST_OMP_ATOMIC);
2710 cp = gfc_state_stack->tail;
2711 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2712 np = new_level (cp);
2718 st = next_statement ();
2721 else if (st == ST_ASSIGNMENT)
2724 unexpected_statement (st);
2727 accept_statement (st);
2733 /* Parse the statements of an OpenMP structured block. */
2736 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
2738 gfc_statement st, omp_end_st;
2742 accept_statement (omp_st);
2744 cp = gfc_state_stack->tail;
2745 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2746 np = new_level (cp);
2752 case ST_OMP_PARALLEL:
2753 omp_end_st = ST_OMP_END_PARALLEL;
2755 case ST_OMP_PARALLEL_SECTIONS:
2756 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
2758 case ST_OMP_SECTIONS:
2759 omp_end_st = ST_OMP_END_SECTIONS;
2761 case ST_OMP_ORDERED:
2762 omp_end_st = ST_OMP_END_ORDERED;
2764 case ST_OMP_CRITICAL:
2765 omp_end_st = ST_OMP_END_CRITICAL;
2768 omp_end_st = ST_OMP_END_MASTER;
2771 omp_end_st = ST_OMP_END_SINGLE;
2773 case ST_OMP_WORKSHARE:
2774 omp_end_st = ST_OMP_END_WORKSHARE;
2776 case ST_OMP_PARALLEL_WORKSHARE:
2777 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
2785 if (workshare_stmts_only)
2787 /* Inside of !$omp workshare, only
2790 where statements and constructs
2791 forall statements and constructs
2795 are allowed. For !$omp critical these
2796 restrictions apply recursively. */
2799 st = next_statement ();
2810 accept_statement (st);
2813 case ST_WHERE_BLOCK:
2814 parse_where_block ();
2817 case ST_FORALL_BLOCK:
2818 parse_forall_block ();
2821 case ST_OMP_PARALLEL:
2822 case ST_OMP_PARALLEL_SECTIONS:
2823 parse_omp_structured_block (st, false);
2826 case ST_OMP_PARALLEL_WORKSHARE:
2827 case ST_OMP_CRITICAL:
2828 parse_omp_structured_block (st, true);
2831 case ST_OMP_PARALLEL_DO:
2832 st = parse_omp_do (st);
2836 parse_omp_atomic ();
2847 st = next_statement ();
2851 st = parse_executable (ST_NONE);
2854 else if (st == ST_OMP_SECTION
2855 && (omp_st == ST_OMP_SECTIONS
2856 || omp_st == ST_OMP_PARALLEL_SECTIONS))
2858 np = new_level (np);
2862 else if (st != omp_end_st)
2863 unexpected_statement (st);
2865 while (st != omp_end_st);
2869 case EXEC_OMP_END_NOWAIT:
2870 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2872 case EXEC_OMP_CRITICAL:
2873 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
2874 || (new_st.ext.omp_name != NULL
2875 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
2876 gfc_error ("Name after !$omp critical and !$omp end critical does "
2878 gfc_free (CONST_CAST (char *, new_st.ext.omp_name));
2880 case EXEC_OMP_END_SINGLE:
2881 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
2882 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
2883 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
2884 gfc_free_omp_clauses (new_st.ext.omp_clauses);
2892 gfc_clear_new_st ();
2893 gfc_commit_symbols ();
2894 gfc_warning_check ();
2899 /* Accept a series of executable statements. We return the first
2900 statement that doesn't fit to the caller. Any block statements are
2901 passed on to the correct handler, which usually passes the buck
2904 static gfc_statement
2905 parse_executable (gfc_statement st)
2910 st = next_statement ();
2914 close_flag = check_do_closure ();
2919 case ST_END_PROGRAM:
2922 case ST_END_FUNCTION:
2926 case ST_END_SUBROUTINE:
2931 case ST_SELECT_CASE:
2932 gfc_error ("%s statement at %C cannot terminate a non-block "
2933 "DO loop", gfc_ascii_statement (st));
2949 accept_statement (st);
2950 if (close_flag == 1)
2951 return ST_IMPLIED_ENDDO;
2958 case ST_SELECT_CASE:
2959 parse_select_block ();
2964 if (check_do_closure () == 1)
2965 return ST_IMPLIED_ENDDO;
2968 case ST_WHERE_BLOCK:
2969 parse_where_block ();
2972 case ST_FORALL_BLOCK:
2973 parse_forall_block ();
2976 case ST_OMP_PARALLEL:
2977 case ST_OMP_PARALLEL_SECTIONS:
2978 case ST_OMP_SECTIONS:
2979 case ST_OMP_ORDERED:
2980 case ST_OMP_CRITICAL:
2983 parse_omp_structured_block (st, false);
2986 case ST_OMP_WORKSHARE:
2987 case ST_OMP_PARALLEL_WORKSHARE:
2988 parse_omp_structured_block (st, true);
2992 case ST_OMP_PARALLEL_DO:
2993 st = parse_omp_do (st);
2994 if (st == ST_IMPLIED_ENDDO)
2999 parse_omp_atomic ();
3006 st = next_statement ();
3011 /* Parse a series of contained program units. */
3013 static void parse_progunit (gfc_statement);
3016 /* Fix the symbols for sibling functions. These are incorrectly added to
3017 the child namespace as the parser didn't know about this procedure. */
3020 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
3024 gfc_symbol *old_sym;
3026 sym->attr.referenced = 1;
3027 for (ns = siblings; ns; ns = ns->sibling)
3029 gfc_find_sym_tree (sym->name, ns, 0, &st);
3031 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
3034 old_sym = st->n.sym;
3035 if (old_sym->ns == ns
3036 && !old_sym->attr.contained
3038 /* By 14.6.1.3, host association should be excluded
3039 for the following. */
3040 && !(old_sym->attr.external
3041 || (old_sym->ts.type != BT_UNKNOWN
3042 && !old_sym->attr.implicit_type)
3043 || old_sym->attr.flavor == FL_PARAMETER
3044 || old_sym->attr.in_common
3045 || old_sym->attr.in_equivalence
3046 || old_sym->attr.data
3047 || old_sym->attr.dummy
3048 || old_sym->attr.result
3049 || old_sym->attr.dimension
3050 || old_sym->attr.allocatable
3051 || old_sym->attr.intrinsic
3052 || old_sym->attr.generic
3053 || old_sym->attr.flavor == FL_NAMELIST
3054 || old_sym->attr.proc == PROC_ST_FUNCTION))
3056 /* Replace it with the symbol from the parent namespace. */
3060 /* Free the old (local) symbol. */
3062 if (old_sym->refs == 0)
3063 gfc_free_symbol (old_sym);
3066 /* Do the same for any contained procedures. */
3067 gfc_fixup_sibling_symbols (sym, ns->contained);
3072 parse_contained (int module)
3074 gfc_namespace *ns, *parent_ns, *tmp;
3075 gfc_state_data s1, s2;
3079 int contains_statements = 0;
3082 push_state (&s1, COMP_CONTAINS, NULL);
3083 parent_ns = gfc_current_ns;
3087 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
3089 gfc_current_ns->sibling = parent_ns->contained;
3090 parent_ns->contained = gfc_current_ns;
3093 /* Process the next available statement. We come here if we got an error
3094 and rejected the last statement. */
3095 st = next_statement ();
3104 contains_statements = 1;
3105 accept_statement (st);
3108 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
3111 /* For internal procedures, create/update the symbol in the
3112 parent namespace. */
3116 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
3117 gfc_error ("Contained procedure '%s' at %C is already "
3118 "ambiguous", gfc_new_block->name);
3121 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
3122 &gfc_new_block->declared_at) ==
3125 if (st == ST_FUNCTION)
3126 gfc_add_function (&sym->attr, sym->name,
3127 &gfc_new_block->declared_at);
3129 gfc_add_subroutine (&sym->attr, sym->name,
3130 &gfc_new_block->declared_at);
3134 gfc_commit_symbols ();
3137 sym = gfc_new_block;
3139 /* Mark this as a contained function, so it isn't replaced
3140 by other module functions. */
3141 sym->attr.contained = 1;
3142 sym->attr.referenced = 1;
3144 parse_progunit (ST_NONE);
3146 /* Fix up any sibling functions that refer to this one. */
3147 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
3148 /* Or refer to any of its alternate entry points. */
3149 for (el = gfc_current_ns->entries; el; el = el->next)
3150 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
3152 gfc_current_ns->code = s2.head;
3153 gfc_current_ns = parent_ns;
3158 /* These statements are associated with the end of the host unit. */
3159 case ST_END_FUNCTION:
3161 case ST_END_PROGRAM:
3162 case ST_END_SUBROUTINE:
3163 accept_statement (st);
3167 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
3168 gfc_ascii_statement (st));
3169 reject_statement ();
3175 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
3176 && st != ST_END_MODULE && st != ST_END_PROGRAM);
3178 /* The first namespace in the list is guaranteed to not have
3179 anything (worthwhile) in it. */
3180 tmp = gfc_current_ns;
3181 gfc_current_ns = parent_ns;
3182 if (seen_error && tmp->refs > 1)
3183 gfc_free_namespace (tmp);
3185 ns = gfc_current_ns->contained;
3186 gfc_current_ns->contained = ns->sibling;
3187 gfc_free_namespace (ns);
3190 if (!contains_statements)
3191 /* This is valid in Fortran 2008. */
3192 gfc_notify_std (GFC_STD_GNU, "Extension: CONTAINS statement without "
3193 "FUNCTION or SUBROUTINE statement at %C");
3197 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
3200 parse_progunit (gfc_statement st)
3205 st = parse_spec (st);
3215 accept_statement (st);
3222 if (gfc_current_state () == COMP_FUNCTION)
3223 gfc_check_function_type (gfc_current_ns);
3228 st = parse_executable (st);
3239 accept_statement (st);
3246 unexpected_statement (st);
3247 reject_statement ();
3248 st = next_statement ();
3254 for (p = gfc_state_stack; p; p = p->previous)
3255 if (p->state == COMP_CONTAINS)
3258 if (gfc_find_state (COMP_MODULE) == SUCCESS)
3263 gfc_error ("CONTAINS statement at %C is already in a contained "
3265 st = next_statement ();
3269 parse_contained (0);
3272 gfc_current_ns->code = gfc_state_stack->head;
3276 /* Come here to complain about a global symbol already in use as
3280 gfc_global_used (gfc_gsymbol *sym, locus *where)
3285 where = &gfc_current_locus;
3295 case GSYM_SUBROUTINE:
3296 name = "SUBROUTINE";
3301 case GSYM_BLOCK_DATA:
3302 name = "BLOCK DATA";
3308 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
3312 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
3313 sym->name, where, name, &sym->where);
3317 /* Parse a block data program unit. */
3320 parse_block_data (void)
3323 static locus blank_locus;
3324 static int blank_block=0;
3327 gfc_current_ns->proc_name = gfc_new_block;
3328 gfc_current_ns->is_block_data = 1;
3330 if (gfc_new_block == NULL)
3333 gfc_error ("Blank BLOCK DATA at %C conflicts with "
3334 "prior BLOCK DATA at %L", &blank_locus);
3338 blank_locus = gfc_current_locus;
3343 s = gfc_get_gsymbol (gfc_new_block->name);
3345 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
3346 gfc_global_used(s, NULL);
3349 s->type = GSYM_BLOCK_DATA;
3350 s->where = gfc_current_locus;
3355 st = parse_spec (ST_NONE);
3357 while (st != ST_END_BLOCK_DATA)
3359 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
3360 gfc_ascii_statement (st));
3361 reject_statement ();
3362 st = next_statement ();
3367 /* Parse a module subprogram. */
3375 s = gfc_get_gsymbol (gfc_new_block->name);
3376 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
3377 gfc_global_used(s, NULL);
3380 s->type = GSYM_MODULE;
3381 s->where = gfc_current_locus;
3385 st = parse_spec (ST_NONE);
3394 parse_contained (1);
3398 accept_statement (st);
3402 gfc_error ("Unexpected %s statement in MODULE at %C",
3403 gfc_ascii_statement (st));
3405 reject_statement ();
3406 st = next_statement ();
3412 /* Add a procedure name to the global symbol table. */
3415 add_global_procedure (int sub)
3419 s = gfc_get_gsymbol(gfc_new_block->name);
3422 || (s->type != GSYM_UNKNOWN
3423 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3424 gfc_global_used(s, NULL);
3427 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3428 s->where = gfc_current_locus;
3434 /* Add a program to the global symbol table. */
3437 add_global_program (void)
3441 if (gfc_new_block == NULL)
3443 s = gfc_get_gsymbol (gfc_new_block->name);
3445 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
3446 gfc_global_used(s, NULL);
3449 s->type = GSYM_PROGRAM;
3450 s->where = gfc_current_locus;
3456 /* Top level parser. */
3459 gfc_parse_file (void)
3461 int seen_program, errors_before, errors;
3462 gfc_state_data top, s;
3466 gfc_start_source_files ();
3468 top.state = COMP_NONE;
3470 top.previous = NULL;
3471 top.head = top.tail = NULL;
3472 top.do_variable = NULL;
3474 gfc_state_stack = ⊤
3476 gfc_clear_new_st ();
3478 gfc_statement_label = NULL;
3480 if (setjmp (eof_buf))
3481 return FAILURE; /* Come here on unexpected EOF */
3485 /* Exit early for empty files. */
3491 st = next_statement ();
3500 goto duplicate_main;
3502 prog_locus = gfc_current_locus;
3504 push_state (&s, COMP_PROGRAM, gfc_new_block);
3505 main_program_symbol(gfc_current_ns, gfc_new_block->name);
3506 accept_statement (st);
3507 add_global_program ();
3508 parse_progunit (ST_NONE);
3512 add_global_procedure (1);
3513 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
3514 accept_statement (st);
3515 parse_progunit (ST_NONE);
3519 add_global_procedure (0);
3520 push_state (&s, COMP_FUNCTION, gfc_new_block);
3521 accept_statement (st);
3522 parse_progunit (ST_NONE);
3526 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
3527 accept_statement (st);
3528 parse_block_data ();
3532 push_state (&s, COMP_MODULE, gfc_new_block);
3533 accept_statement (st);
3535 gfc_get_errors (NULL, &errors_before);
3539 /* Anything else starts a nameless main program block. */
3542 goto duplicate_main;
3544 prog_locus = gfc_current_locus;
3546 push_state (&s, COMP_PROGRAM, gfc_new_block);
3547 main_program_symbol (gfc_current_ns, "MAIN__");
3548 parse_progunit (st);
3552 gfc_current_ns->code = s.head;
3554 gfc_resolve (gfc_current_ns);
3556 /* Dump the parse tree if requested. */
3557 if (gfc_option.verbose)
3558 gfc_show_namespace (gfc_current_ns);
3560 gfc_get_errors (NULL, &errors);
3561 if (s.state == COMP_MODULE)
3563 gfc_dump_module (s.sym->name, errors_before == errors);
3565 gfc_generate_module_code (gfc_current_ns);
3570 gfc_generate_code (gfc_current_ns);
3578 gfc_end_source_files ();
3582 /* If we see a duplicate main program, shut down. If the second
3583 instance is an implied main program, ie data decls or executable
3584 statements, we're in for lots of errors. */
3585 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
3586 reject_statement ();