2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation,
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
31 /* Current statement label. Zero means no statement label. Because
32 new_st can get wiped during statement matching, we have to keep it
35 gfc_st_label *gfc_statement_label;
37 static locus label_locus;
38 static jmp_buf eof_buf;
40 gfc_state_data *gfc_state_stack;
42 /* TODO: Re-order functions to kill these forward decls. */
43 static void check_statement_label (gfc_statement);
44 static void undo_new_statement (void);
45 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 decode_statement (void)
101 gfc_clear_error (); /* Clear any pending errors. */
102 gfc_clear_warning (); /* Clear any pending warnings. */
104 if (gfc_match_eos () == MATCH_YES)
107 old_locus = gfc_current_locus;
109 /* Try matching a data declaration or function declaration. The
110 input "REALFUNCTIONA(N)" can mean several things in different
111 contexts, so it (and its relatives) get special treatment. */
113 if (gfc_current_state () == COMP_NONE
114 || gfc_current_state () == COMP_INTERFACE
115 || gfc_current_state () == COMP_CONTAINS)
117 m = gfc_match_function_decl ();
120 else if (m == MATCH_ERROR)
124 gfc_current_locus = old_locus;
127 /* Match statements whose error messages are meant to be overwritten
128 by something better. */
130 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
131 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
132 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
134 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
135 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
137 /* Try to match a subroutine statement, which has the same optional
138 prefixes that functions can have. */
140 if (gfc_match_subroutine () == MATCH_YES)
141 return ST_SUBROUTINE;
143 gfc_current_locus = old_locus;
145 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
146 might begin with a block label. The match functions for these
147 statements are unusual in that their keyword is not seen before
148 the matcher is called. */
150 if (gfc_match_if (&st) == MATCH_YES)
153 gfc_current_locus = old_locus;
155 if (gfc_match_where (&st) == MATCH_YES)
158 gfc_current_locus = old_locus;
160 if (gfc_match_forall (&st) == MATCH_YES)
163 gfc_current_locus = old_locus;
165 match (NULL, gfc_match_do, ST_DO);
166 match (NULL, gfc_match_select, ST_SELECT_CASE);
168 /* General statement matching: Instead of testing every possible
169 statement, we eliminate most possibilities by peeking at the
172 c = gfc_peek_char ();
177 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
178 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
179 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
183 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
184 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
188 match ("call", gfc_match_call, ST_CALL);
189 match ("close", gfc_match_close, ST_CLOSE);
190 match ("continue", gfc_match_continue, ST_CONTINUE);
191 match ("cycle", gfc_match_cycle, ST_CYCLE);
192 match ("case", gfc_match_case, ST_CASE);
193 match ("common", gfc_match_common, ST_COMMON);
194 match ("contains", gfc_match_eos, ST_CONTAINS);
198 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
199 match ("data", gfc_match_data, ST_DATA);
200 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
204 match ("end file", gfc_match_endfile, ST_END_FILE);
205 match ("exit", gfc_match_exit, ST_EXIT);
206 match ("else", gfc_match_else, ST_ELSE);
207 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
208 match ("else if", gfc_match_elseif, ST_ELSEIF);
209 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
211 if (gfc_match_end (&st) == MATCH_YES)
214 match ("entry% ", gfc_match_entry, ST_ENTRY);
215 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
216 match ("external", gfc_match_external, ST_ATTR_DECL);
220 match ("flush", gfc_match_flush, ST_FLUSH);
221 match ("format", gfc_match_format, ST_FORMAT);
225 match ("go to", gfc_match_goto, ST_GOTO);
229 match ("inquire", gfc_match_inquire, ST_INQUIRE);
230 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
231 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
232 match ("interface", gfc_match_interface, ST_INTERFACE);
233 match ("intent", gfc_match_intent, ST_ATTR_DECL);
234 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
238 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
239 match ("module", gfc_match_module, ST_MODULE);
243 match ("nullify", gfc_match_nullify, ST_NULLIFY);
244 match ("namelist", gfc_match_namelist, ST_NAMELIST);
248 match ("open", gfc_match_open, ST_OPEN);
249 match ("optional", gfc_match_optional, ST_ATTR_DECL);
253 match ("print", gfc_match_print, ST_WRITE);
254 match ("parameter", gfc_match_parameter, ST_PARAMETER);
255 match ("pause", gfc_match_pause, ST_PAUSE);
256 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
257 if (gfc_match_private (&st) == MATCH_YES)
259 match ("program", gfc_match_program, ST_PROGRAM);
260 if (gfc_match_public (&st) == MATCH_YES)
265 match ("read", gfc_match_read, ST_READ);
266 match ("return", gfc_match_return, ST_RETURN);
267 match ("rewind", gfc_match_rewind, ST_REWIND);
271 match ("sequence", gfc_match_eos, ST_SEQUENCE);
272 match ("stop", gfc_match_stop, ST_STOP);
273 match ("save", gfc_match_save, ST_ATTR_DECL);
277 match ("target", gfc_match_target, ST_ATTR_DECL);
278 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
282 match ("use% ", gfc_match_use, ST_USE);
286 match ("write", gfc_match_write, ST_WRITE);
290 /* All else has failed, so give up. See if any of the matchers has
291 stored an error message of some sort. */
293 if (gfc_error_check () == 0)
294 gfc_error_now ("Unclassifiable statement at %C");
298 gfc_error_recovery ();
304 decode_omp_directive (void)
313 gfc_clear_error (); /* Clear any pending errors. */
314 gfc_clear_warning (); /* Clear any pending warnings. */
318 gfc_error_now ("OpenMP directives at %C may not appear in PURE or ELEMENTAL procedures");
319 gfc_error_recovery ();
323 old_locus = gfc_current_locus;
325 /* General OpenMP directive matching: Instead of testing every possible
326 statement, we eliminate most possibilities by peeking at the
329 c = gfc_peek_char ();
334 match ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
337 match ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
340 match ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
343 match ("do", gfc_match_omp_do, ST_OMP_DO);
346 match ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
347 match ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
348 match ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
349 match ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
350 match ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
351 match ("end parallel sections", gfc_match_omp_eos,
352 ST_OMP_END_PARALLEL_SECTIONS);
353 match ("end parallel workshare", gfc_match_omp_eos,
354 ST_OMP_END_PARALLEL_WORKSHARE);
355 match ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
356 match ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
357 match ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
358 match ("end workshare", gfc_match_omp_end_nowait,
359 ST_OMP_END_WORKSHARE);
362 match ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
365 match ("master", gfc_match_omp_master, ST_OMP_MASTER);
368 match ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
371 match ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
372 match ("parallel sections", gfc_match_omp_parallel_sections,
373 ST_OMP_PARALLEL_SECTIONS);
374 match ("parallel workshare", gfc_match_omp_parallel_workshare,
375 ST_OMP_PARALLEL_WORKSHARE);
376 match ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
379 match ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
380 match ("section", gfc_match_omp_eos, ST_OMP_SECTION);
381 match ("single", gfc_match_omp_single, ST_OMP_SINGLE);
384 match ("threadprivate", gfc_match_omp_threadprivate,
385 ST_OMP_THREADPRIVATE);
387 match ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
391 /* All else has failed, so give up. See if any of the matchers has
392 stored an error message of some sort. */
394 if (gfc_error_check () == 0)
395 gfc_error_now ("Unclassifiable OpenMP directive at %C");
399 gfc_error_recovery ();
407 /* Get the next statement in free form source. */
415 gfc_gobble_whitespace ();
417 c = gfc_peek_char ();
421 /* Found a statement label? */
422 m = gfc_match_st_label (&gfc_statement_label);
424 d = gfc_peek_char ();
425 if (m != MATCH_YES || !gfc_is_whitespace (d))
427 gfc_match_small_literal_int (&c, &cnt);
430 gfc_error_now ("Too many digits in statement label at %C");
433 gfc_error_now ("Zero is not a valid statement label at %C");
436 c = gfc_next_char ();
439 if (!gfc_is_whitespace (c))
440 gfc_error_now ("Non-numeric character in statement label at %C");
446 label_locus = gfc_current_locus;
448 gfc_gobble_whitespace ();
450 if (gfc_match_eos () == MATCH_YES)
453 ("Ignoring statement label in empty statement at %C");
454 gfc_free_st_label (gfc_statement_label);
455 gfc_statement_label = NULL;
462 /* Comments have already been skipped by the time we get here,
463 except for OpenMP directives. */
464 if (gfc_option.flag_openmp)
468 c = gfc_next_char ();
469 for (i = 0; i < 5; i++, c = gfc_next_char ())
470 gcc_assert (c == "!$omp"[i]);
472 gcc_assert (c == ' ');
473 return decode_omp_directive ();
477 return decode_statement ();
481 /* Get the next statement in fixed-form source. */
486 int label, digit_flag, i;
491 return decode_statement ();
493 /* Skip past the current label field, parsing a statement label if
494 one is there. This is a weird number parser, since the number is
495 contained within five columns and can have any kind of embedded
496 spaces. We also check for characters that make the rest of the
502 for (i = 0; i < 5; i++)
504 c = gfc_next_char_literal (0);
521 label = label * 10 + c - '0';
522 label_locus = gfc_current_locus;
526 /* Comments have already been skipped by the time we get
527 here, except for OpenMP directives. */
529 if (gfc_option.flag_openmp)
531 for (i = 0; i < 5; i++, c = gfc_next_char_literal (0))
532 gcc_assert (TOLOWER (c) == "*$omp"[i]);
534 if (c != ' ' && c != '0')
536 gfc_buffer_error (0);
537 gfc_error ("Bad continuation line at %C");
541 return decode_omp_directive ();
545 /* Comments have already been skipped by the time we get
546 here so don't bother checking for them. */
549 gfc_buffer_error (0);
550 gfc_error ("Non-numeric character in statement label at %C");
558 gfc_warning_now ("Zero is not a valid statement label at %C");
561 /* We've found a valid statement label. */
562 gfc_statement_label = gfc_get_st_label (label);
566 /* Since this line starts a statement, it cannot be a continuation
567 of a previous statement. If we see something here besides a
568 space or zero, it must be a bad continuation line. */
570 c = gfc_next_char_literal (0);
574 if (c != ' ' && c!= '0')
576 gfc_buffer_error (0);
577 gfc_error ("Bad continuation line at %C");
581 /* Now that we've taken care of the statement label columns, we have
582 to make sure that the first nonblank character is not a '!'. If
583 it is, the rest of the line is a comment. */
587 loc = gfc_current_locus;
588 c = gfc_next_char_literal (0);
590 while (gfc_is_whitespace (c));
594 gfc_current_locus = loc;
596 if (gfc_match_eos () == MATCH_YES)
599 /* At this point, we've got a nonblank statement to parse. */
600 return decode_statement ();
604 gfc_warning ("Ignoring statement label in empty statement at %C");
610 /* Return the next non-ST_NONE statement to the caller. We also worry
611 about including files and the ends of include files at this stage. */
614 next_statement (void)
618 gfc_new_block = NULL;
622 gfc_statement_label = NULL;
623 gfc_buffer_error (1);
627 if (gfc_option.warn_line_truncation
628 && gfc_current_locus.lb
629 && gfc_current_locus.lb->truncated)
630 gfc_warning_now ("Line truncated at %C");
635 gfc_skip_comments ();
644 (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
650 gfc_buffer_error (0);
653 check_statement_label (st);
659 /****************************** Parser ***********************************/
661 /* The parser subroutines are of type 'try' that fail if the file ends
664 /* Macros that expand to case-labels for various classes of
665 statements. Start with executable statements that directly do
668 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
669 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
670 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
671 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
672 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
673 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
674 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
675 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
678 /* Statements that mark other executable statements. */
680 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
681 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_OMP_PARALLEL: \
682 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
683 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
684 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
685 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
687 /* Declaration statements */
689 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
690 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
691 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE
693 /* Block end statements. Errors associated with interchanging these
694 are detected in gfc_match_end(). */
696 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
697 case ST_END_PROGRAM: case ST_END_SUBROUTINE
700 /* Push a new state onto the stack. */
703 push_state (gfc_state_data * p, gfc_compile_state new_state, gfc_symbol * sym)
706 p->state = new_state;
707 p->previous = gfc_state_stack;
709 p->head = p->tail = NULL;
710 p->do_variable = NULL;
716 /* Pop the current state. */
722 gfc_state_stack = gfc_state_stack->previous;
726 /* Try to find the given state in the state stack. */
729 gfc_find_state (gfc_compile_state state)
733 for (p = gfc_state_stack; p; p = p->previous)
734 if (p->state == state)
737 return (p == NULL) ? FAILURE : SUCCESS;
741 /* Starts a new level in the statement list. */
744 new_level (gfc_code * q)
748 p = q->block = gfc_get_code ();
750 gfc_state_stack->head = gfc_state_stack->tail = p;
756 /* Add the current new_st code structure and adds it to the current
757 program unit. As a side-effect, it zeroes the new_st. */
767 p->loc = gfc_current_locus;
769 if (gfc_state_stack->head == NULL)
770 gfc_state_stack->head = p;
772 gfc_state_stack->tail->next = p;
774 while (p->next != NULL)
777 gfc_state_stack->tail = p;
785 /* Frees everything associated with the current statement. */
788 undo_new_statement (void)
790 gfc_free_statements (new_st.block);
791 gfc_free_statements (new_st.next);
792 gfc_free_statement (&new_st);
797 /* If the current statement has a statement label, make sure that it
798 is allowed to, or should have one. */
801 check_statement_label (gfc_statement st)
805 if (gfc_statement_label == NULL)
808 gfc_error ("FORMAT statement at %L does not have a statement label",
816 case ST_END_FUNCTION:
817 case ST_END_SUBROUTINE:
823 type = ST_LABEL_TARGET;
827 type = ST_LABEL_FORMAT;
830 /* Statement labels are not restricted from appearing on a
831 particular line. However, there are plenty of situations
832 where the resulting label can't be referenced. */
835 type = ST_LABEL_BAD_TARGET;
839 gfc_define_st_label (gfc_statement_label, type, &label_locus);
841 new_st.here = gfc_statement_label;
845 /* Figures out what the enclosing program unit is. This will be a
846 function, subroutine, program, block data or module. */
849 gfc_enclosing_unit (gfc_compile_state * result)
853 for (p = gfc_state_stack; p; p = p->previous)
854 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
855 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
856 || p->state == COMP_PROGRAM)
865 *result = COMP_PROGRAM;
870 /* Translate a statement enum to a string. */
873 gfc_ascii_statement (gfc_statement st)
879 case ST_ARITHMETIC_IF:
880 p = _("arithmetic IF");
886 p = _("attribute declaration");
916 p = _("data declaration");
924 case ST_DERIVED_DECL:
925 p = _("derived type declaration");
939 case ST_END_BLOCK_DATA:
940 p = "END BLOCK DATA";
951 case ST_END_FUNCTION:
957 case ST_END_INTERFACE:
969 case ST_END_SUBROUTINE:
970 p = "END SUBROUTINE";
990 case ST_FORALL_BLOCK: /* Fall through */
1009 case ST_IMPLICIT_NONE:
1010 p = "IMPLICIT NONE";
1012 case ST_IMPLIED_ENDDO:
1013 p = _("implied END DO");
1036 case ST_MODULE_PROC:
1037 p = "MODULE PROCEDURE";
1072 case ST_WHERE_BLOCK: /* Fall through */
1080 p = _("assignment");
1082 case ST_POINTER_ASSIGNMENT:
1083 p = _("pointer assignment");
1085 case ST_SELECT_CASE:
1094 case ST_STATEMENT_FUNCTION:
1095 p = "STATEMENT FUNCTION";
1097 case ST_LABEL_ASSIGNMENT:
1098 p = "LABEL ASSIGNMENT";
1101 p = "ENUM DEFINITION";
1104 p = "ENUMERATOR DEFINITION";
1112 case ST_OMP_BARRIER:
1113 p = "!$OMP BARRIER";
1115 case ST_OMP_CRITICAL:
1116 p = "!$OMP CRITICAL";
1121 case ST_OMP_END_CRITICAL:
1122 p = "!$OMP END CRITICAL";
1127 case ST_OMP_END_MASTER:
1128 p = "!$OMP END MASTER";
1130 case ST_OMP_END_ORDERED:
1131 p = "!$OMP END ORDERED";
1133 case ST_OMP_END_PARALLEL:
1134 p = "!$OMP END PARALLEL";
1136 case ST_OMP_END_PARALLEL_DO:
1137 p = "!$OMP END PARALLEL DO";
1139 case ST_OMP_END_PARALLEL_SECTIONS:
1140 p = "!$OMP END PARALLEL SECTIONS";
1142 case ST_OMP_END_PARALLEL_WORKSHARE:
1143 p = "!$OMP END PARALLEL WORKSHARE";
1145 case ST_OMP_END_SECTIONS:
1146 p = "!$OMP END SECTIONS";
1148 case ST_OMP_END_SINGLE:
1149 p = "!$OMP END SINGLE";
1151 case ST_OMP_END_WORKSHARE:
1152 p = "!$OMP END WORKSHARE";
1160 case ST_OMP_ORDERED:
1161 p = "!$OMP ORDERED";
1163 case ST_OMP_PARALLEL:
1164 p = "!$OMP PARALLEL";
1166 case ST_OMP_PARALLEL_DO:
1167 p = "!$OMP PARALLEL DO";
1169 case ST_OMP_PARALLEL_SECTIONS:
1170 p = "!$OMP PARALLEL SECTIONS";
1172 case ST_OMP_PARALLEL_WORKSHARE:
1173 p = "!$OMP PARALLEL WORKSHARE";
1175 case ST_OMP_SECTIONS:
1176 p = "!$OMP SECTIONS";
1178 case ST_OMP_SECTION:
1179 p = "!$OMP SECTION";
1184 case ST_OMP_THREADPRIVATE:
1185 p = "!$OMP THREADPRIVATE";
1187 case ST_OMP_WORKSHARE:
1188 p = "!$OMP WORKSHARE";
1191 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
1198 /* Create a symbol for the main program and assign it to ns->proc_name. */
1201 main_program_symbol (gfc_namespace * ns)
1203 gfc_symbol *main_program;
1204 symbol_attribute attr;
1206 gfc_get_symbol ("MAIN__", ns, &main_program);
1207 gfc_clear_attr (&attr);
1208 attr.flavor = FL_PROCEDURE;
1209 attr.proc = PROC_UNKNOWN;
1210 attr.subroutine = 1;
1211 attr.access = ACCESS_PUBLIC;
1212 attr.is_main_program = 1;
1213 main_program->attr = attr;
1214 main_program->declared_at = gfc_current_locus;
1215 ns->proc_name = main_program;
1216 gfc_commit_symbols ();
1220 /* Do whatever is necessary to accept the last statement. */
1223 accept_statement (gfc_statement st)
1232 case ST_IMPLICIT_NONE:
1233 gfc_set_implicit_none ();
1242 gfc_current_ns->proc_name = gfc_new_block;
1245 /* If the statement is the end of a block, lay down a special code
1246 that allows a branch to the end of the block from within the
1251 if (gfc_statement_label != NULL)
1253 new_st.op = EXEC_NOP;
1259 /* The end-of-program unit statements do not get the special
1260 marker and require a statement of some sort if they are a
1263 case ST_END_PROGRAM:
1264 case ST_END_FUNCTION:
1265 case ST_END_SUBROUTINE:
1266 if (gfc_statement_label != NULL)
1268 new_st.op = EXEC_RETURN;
1284 gfc_commit_symbols ();
1285 gfc_warning_check ();
1286 gfc_clear_new_st ();
1290 /* Undo anything tentative that has been built for the current
1294 reject_statement (void)
1296 gfc_new_block = NULL;
1297 gfc_undo_symbols ();
1298 gfc_clear_warning ();
1299 undo_new_statement ();
1303 /* Generic complaint about an out of order statement. We also do
1304 whatever is necessary to clean up. */
1307 unexpected_statement (gfc_statement st)
1310 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1312 reject_statement ();
1316 /* Given the next statement seen by the matcher, make sure that it is
1317 in proper order with the last. This subroutine is initialized by
1318 calling it with an argument of ST_NONE. If there is a problem, we
1319 issue an error and return FAILURE. Otherwise we return SUCCESS.
1321 Individual parsers need to verify that the statements seen are
1322 valid before calling here, ie ENTRY statements are not allowed in
1323 INTERFACE blocks. The following diagram is taken from the standard:
1325 +---------------------------------------+
1326 | program subroutine function module |
1327 +---------------------------------------+
1329 |---------------------------------------+
1331 | +-----------+------------------+
1332 | | parameter | implicit |
1333 | +-----------+------------------+
1334 | format | | derived type |
1335 | entry | parameter | interface |
1336 | | data | specification |
1337 | | | statement func |
1338 | +-----------+------------------+
1339 | | data | executable |
1340 +--------+-----------+------------------+
1342 +---------------------------------------+
1343 | internal module/subprogram |
1344 +---------------------------------------+
1346 +---------------------------------------+
1353 { ORDER_START, ORDER_USE, ORDER_IMPLICIT_NONE, ORDER_IMPLICIT,
1354 ORDER_SPEC, ORDER_EXEC
1357 gfc_statement last_statement;
1363 verify_st_order (st_state * p, gfc_statement st)
1369 p->state = ORDER_START;
1373 if (p->state > ORDER_USE)
1375 p->state = ORDER_USE;
1378 case ST_IMPLICIT_NONE:
1379 if (p->state > ORDER_IMPLICIT_NONE)
1382 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1383 statement disqualifies a USE but not an IMPLICIT NONE.
1384 Duplicate IMPLICIT NONEs are caught when the implicit types
1387 p->state = ORDER_IMPLICIT_NONE;
1391 if (p->state > ORDER_IMPLICIT)
1393 p->state = ORDER_IMPLICIT;
1398 if (p->state < ORDER_IMPLICIT_NONE)
1399 p->state = ORDER_IMPLICIT_NONE;
1403 if (p->state >= ORDER_EXEC)
1405 if (p->state < ORDER_IMPLICIT)
1406 p->state = ORDER_IMPLICIT;
1410 if (p->state < ORDER_SPEC)
1411 p->state = ORDER_SPEC;
1416 case ST_DERIVED_DECL:
1418 if (p->state >= ORDER_EXEC)
1420 if (p->state < ORDER_SPEC)
1421 p->state = ORDER_SPEC;
1426 if (p->state < ORDER_EXEC)
1427 p->state = ORDER_EXEC;
1432 ("Unexpected %s statement in verify_st_order() at %C",
1433 gfc_ascii_statement (st));
1436 /* All is well, record the statement in case we need it next time. */
1437 p->where = gfc_current_locus;
1438 p->last_statement = st;
1442 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1443 gfc_ascii_statement (st),
1444 gfc_ascii_statement (p->last_statement), &p->where);
1450 /* Handle an unexpected end of file. This is a show-stopper... */
1452 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1455 unexpected_eof (void)
1459 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1461 /* Memory cleanup. Move to "second to last". */
1462 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1465 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1468 longjmp (eof_buf, 1);
1472 /* Parse a derived type. */
1475 parse_derived (void)
1477 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1484 accept_statement (ST_DERIVED_DECL);
1485 push_state (&s, COMP_DERIVED, gfc_new_block);
1487 gfc_new_block->component_access = ACCESS_PUBLIC;
1494 while (compiling_type)
1496 st = next_statement ();
1503 accept_statement (st);
1510 if (!seen_component)
1512 gfc_error ("Derived type definition at %C has no components");
1516 accept_statement (ST_END_TYPE);
1520 if (gfc_find_state (COMP_MODULE) == FAILURE)
1523 ("PRIVATE statement in TYPE at %C must be inside a MODULE");
1530 gfc_error ("PRIVATE statement at %C must precede "
1531 "structure components");
1538 gfc_error ("Duplicate PRIVATE statement at %C");
1542 s.sym->component_access = ACCESS_PRIVATE;
1543 accept_statement (ST_PRIVATE);
1550 gfc_error ("SEQUENCE statement at %C must precede "
1551 "structure components");
1556 if (gfc_current_block ()->attr.sequence)
1557 gfc_warning ("SEQUENCE attribute at %C already specified in "
1562 gfc_error ("Duplicate SEQUENCE statement at %C");
1567 gfc_add_sequence (&gfc_current_block ()->attr,
1568 gfc_current_block ()->name, NULL);
1572 unexpected_statement (st);
1577 /* Sanity checks on the structure. If the structure has the
1578 SEQUENCE attribute, then all component structures must also have
1580 if (error_flag == 0 && gfc_current_block ()->attr.sequence)
1581 for (c = gfc_current_block ()->components; c; c = c->next)
1583 if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
1586 ("Component %s of SEQUENCE type declared at %C does not "
1587 "have the SEQUENCE attribute", c->ts.derived->name);
1596 /* Parse an ENUM. */
1605 int seen_enumerator = 0;
1609 push_state (&s, COMP_ENUM, gfc_new_block);
1613 while (compiling_enum)
1615 st = next_statement ();
1623 seen_enumerator = 1;
1624 accept_statement (st);
1629 if (!seen_enumerator)
1631 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1634 accept_statement (st);
1638 gfc_free_enum_history ();
1639 unexpected_statement (st);
1646 /* Parse an interface. We must be able to deal with the possibility
1647 of recursive interfaces. The parse_spec() subroutine is mutually
1648 recursive with parse_interface(). */
1650 static gfc_statement parse_spec (gfc_statement);
1653 parse_interface (void)
1655 gfc_compile_state new_state, current_state;
1656 gfc_symbol *prog_unit, *sym;
1657 gfc_interface_info save;
1658 gfc_state_data s1, s2;
1661 accept_statement (ST_INTERFACE);
1663 current_interface.ns = gfc_current_ns;
1664 save = current_interface;
1666 sym = (current_interface.type == INTERFACE_GENERIC
1667 || current_interface.type == INTERFACE_USER_OP) ? gfc_new_block : NULL;
1669 push_state (&s1, COMP_INTERFACE, sym);
1670 current_state = COMP_NONE;
1673 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1675 st = next_statement ();
1682 new_state = COMP_SUBROUTINE;
1683 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1684 gfc_new_block->formal, NULL);
1688 new_state = COMP_FUNCTION;
1689 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1690 gfc_new_block->formal, NULL);
1693 case ST_MODULE_PROC: /* The module procedure matcher makes
1694 sure the context is correct. */
1695 accept_statement (st);
1696 gfc_free_namespace (gfc_current_ns);
1699 case ST_END_INTERFACE:
1700 gfc_free_namespace (gfc_current_ns);
1701 gfc_current_ns = current_interface.ns;
1705 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1706 gfc_ascii_statement (st));
1707 reject_statement ();
1708 gfc_free_namespace (gfc_current_ns);
1713 /* Make sure that a generic interface has only subroutines or
1714 functions and that the generic name has the right attribute. */
1715 if (current_interface.type == INTERFACE_GENERIC)
1717 if (current_state == COMP_NONE)
1719 if (new_state == COMP_FUNCTION)
1720 gfc_add_function (&sym->attr, sym->name, NULL);
1721 else if (new_state == COMP_SUBROUTINE)
1722 gfc_add_subroutine (&sym->attr, sym->name, NULL);
1724 current_state = new_state;
1728 if (new_state != current_state)
1730 if (new_state == COMP_SUBROUTINE)
1732 ("SUBROUTINE at %C does not belong in a generic function "
1735 if (new_state == COMP_FUNCTION)
1737 ("FUNCTION at %C does not belong in a generic subroutine "
1743 push_state (&s2, new_state, gfc_new_block);
1744 accept_statement (st);
1745 prog_unit = gfc_new_block;
1746 prog_unit->formal_ns = gfc_current_ns;
1749 /* Read data declaration statements. */
1750 st = parse_spec (ST_NONE);
1752 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1754 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1755 gfc_ascii_statement (st));
1756 reject_statement ();
1760 current_interface = save;
1761 gfc_add_interface (prog_unit);
1771 /* Parse a set of specification statements. Returns the statement
1772 that doesn't fit. */
1774 static gfc_statement
1775 parse_spec (gfc_statement st)
1779 verify_st_order (&ss, ST_NONE);
1781 st = next_statement ();
1791 case ST_DATA: /* Not allowed in interfaces */
1792 if (gfc_current_state () == COMP_INTERFACE)
1798 case ST_IMPLICIT_NONE:
1803 case ST_DERIVED_DECL:
1805 if (verify_st_order (&ss, st) == FAILURE)
1807 reject_statement ();
1808 st = next_statement ();
1818 case ST_DERIVED_DECL:
1824 if (gfc_current_state () != COMP_MODULE)
1826 gfc_error ("%s statement must appear in a MODULE",
1827 gfc_ascii_statement (st));
1831 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1833 gfc_error ("%s statement at %C follows another accessibility "
1834 "specification", gfc_ascii_statement (st));
1838 gfc_current_ns->default_access = (st == ST_PUBLIC)
1839 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1847 accept_statement (st);
1848 st = next_statement ();
1852 accept_statement (st);
1854 st = next_statement ();
1865 /* Parse a WHERE block, (not a simple WHERE statement). */
1868 parse_where_block (void)
1870 int seen_empty_else;
1875 accept_statement (ST_WHERE_BLOCK);
1876 top = gfc_state_stack->tail;
1878 push_state (&s, COMP_WHERE, gfc_new_block);
1880 d = add_statement ();
1881 d->expr = top->expr;
1887 seen_empty_else = 0;
1891 st = next_statement ();
1897 case ST_WHERE_BLOCK:
1898 parse_where_block ();
1903 accept_statement (st);
1907 if (seen_empty_else)
1910 ("ELSEWHERE statement at %C follows previous unmasked "
1915 if (new_st.expr == NULL)
1916 seen_empty_else = 1;
1918 d = new_level (gfc_state_stack->head);
1920 d->expr = new_st.expr;
1922 accept_statement (st);
1927 accept_statement (st);
1931 gfc_error ("Unexpected %s statement in WHERE block at %C",
1932 gfc_ascii_statement (st));
1933 reject_statement ();
1938 while (st != ST_END_WHERE);
1944 /* Parse a FORALL block (not a simple FORALL statement). */
1947 parse_forall_block (void)
1953 accept_statement (ST_FORALL_BLOCK);
1954 top = gfc_state_stack->tail;
1956 push_state (&s, COMP_FORALL, gfc_new_block);
1958 d = add_statement ();
1959 d->op = EXEC_FORALL;
1964 st = next_statement ();
1969 case ST_POINTER_ASSIGNMENT:
1972 accept_statement (st);
1975 case ST_WHERE_BLOCK:
1976 parse_where_block ();
1979 case ST_FORALL_BLOCK:
1980 parse_forall_block ();
1984 accept_statement (st);
1991 gfc_error ("Unexpected %s statement in FORALL block at %C",
1992 gfc_ascii_statement (st));
1994 reject_statement ();
1998 while (st != ST_END_FORALL);
2004 static gfc_statement parse_executable (gfc_statement);
2006 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
2009 parse_if_block (void)
2018 accept_statement (ST_IF_BLOCK);
2020 top = gfc_state_stack->tail;
2021 push_state (&s, COMP_IF, gfc_new_block);
2023 new_st.op = EXEC_IF;
2024 d = add_statement ();
2026 d->expr = top->expr;
2032 st = parse_executable (ST_NONE);
2043 ("ELSE IF statement at %C cannot follow ELSE statement at %L",
2046 reject_statement ();
2050 d = new_level (gfc_state_stack->head);
2052 d->expr = new_st.expr;
2054 accept_statement (st);
2061 gfc_error ("Duplicate ELSE statements at %L and %C",
2063 reject_statement ();
2068 else_locus = gfc_current_locus;
2070 d = new_level (gfc_state_stack->head);
2073 accept_statement (st);
2081 unexpected_statement (st);
2085 while (st != ST_ENDIF);
2088 accept_statement (st);
2092 /* Parse a SELECT block. */
2095 parse_select_block (void)
2101 accept_statement (ST_SELECT_CASE);
2103 cp = gfc_state_stack->tail;
2104 push_state (&s, COMP_SELECT, gfc_new_block);
2106 /* Make sure that the next statement is a CASE or END SELECT. */
2109 st = next_statement ();
2112 if (st == ST_END_SELECT)
2114 /* Empty SELECT CASE is OK. */
2115 accept_statement (st);
2123 ("Expected a CASE or END SELECT statement following SELECT CASE "
2126 reject_statement ();
2129 /* At this point, we're got a nonempty select block. */
2130 cp = new_level (cp);
2133 accept_statement (st);
2137 st = parse_executable (ST_NONE);
2144 cp = new_level (gfc_state_stack->head);
2146 gfc_clear_new_st ();
2148 accept_statement (st);
2154 /* Can't have an executable statement because of
2155 parse_executable(). */
2157 unexpected_statement (st);
2161 while (st != ST_END_SELECT);
2164 accept_statement (st);
2168 /* Given a symbol, make sure it is not an iteration variable for a DO
2169 statement. This subroutine is called when the symbol is seen in a
2170 context that causes it to become redefined. If the symbol is an
2171 iterator, we generate an error message and return nonzero. */
2174 gfc_check_do_variable (gfc_symtree *st)
2178 for (s=gfc_state_stack; s; s = s->previous)
2179 if (s->do_variable == st)
2181 gfc_error_now("Variable '%s' at %C cannot be redefined inside "
2182 "loop beginning at %L", st->name, &s->head->loc);
2190 /* Checks to see if the current statement label closes an enddo.
2191 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
2192 an error) if it incorrectly closes an ENDDO. */
2195 check_do_closure (void)
2199 if (gfc_statement_label == NULL)
2202 for (p = gfc_state_stack; p; p = p->previous)
2203 if (p->state == COMP_DO)
2207 return 0; /* No loops to close */
2209 if (p->ext.end_do_label == gfc_statement_label)
2212 if (p == gfc_state_stack)
2216 ("End of nonblock DO statement at %C is within another block");
2220 /* At this point, the label doesn't terminate the innermost loop.
2221 Make sure it doesn't terminate another one. */
2222 for (; p; p = p->previous)
2223 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
2225 gfc_error ("End of nonblock DO statement at %C is interwoven "
2226 "with another DO loop");
2234 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
2235 handled inside of parse_executable(), because they aren't really
2239 parse_do_block (void)
2246 s.ext.end_do_label = new_st.label;
2248 if (new_st.ext.iterator != NULL)
2249 stree = new_st.ext.iterator->var->symtree;
2253 accept_statement (ST_DO);
2255 top = gfc_state_stack->tail;
2256 push_state (&s, COMP_DO, gfc_new_block);
2258 s.do_variable = stree;
2260 top->block = new_level (top);
2261 top->block->op = EXEC_DO;
2264 st = parse_executable (ST_NONE);
2272 if (s.ext.end_do_label != NULL
2273 && s.ext.end_do_label != gfc_statement_label)
2275 ("Statement label in ENDDO at %C doesn't match DO label");
2277 if (gfc_statement_label != NULL)
2279 new_st.op = EXEC_NOP;
2284 case ST_IMPLIED_ENDDO:
2288 unexpected_statement (st);
2293 accept_statement (st);
2297 /* Parse the statements of OpenMP do/parallel do. */
2299 static gfc_statement
2300 parse_omp_do (gfc_statement omp_st)
2306 accept_statement (omp_st);
2308 cp = gfc_state_stack->tail;
2309 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2310 np = new_level (cp);
2316 st = next_statement ();
2319 else if (st == ST_DO)
2322 unexpected_statement (st);
2326 if (gfc_statement_label != NULL
2327 && gfc_state_stack->previous != NULL
2328 && gfc_state_stack->previous->state == COMP_DO
2329 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
2337 there should be no !$OMP END DO. */
2339 return ST_IMPLIED_ENDDO;
2342 check_do_closure ();
2345 st = next_statement ();
2346 if (st == (omp_st == ST_OMP_DO ? ST_OMP_END_DO : ST_OMP_END_PARALLEL_DO))
2348 if (new_st.op == EXEC_OMP_END_NOWAIT)
2349 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2351 gcc_assert (new_st.op == EXEC_NOP);
2352 gfc_clear_new_st ();
2353 gfc_commit_symbols ();
2354 gfc_warning_check ();
2355 st = next_statement ();
2361 /* Parse the statements of OpenMP atomic directive. */
2364 parse_omp_atomic (void)
2370 accept_statement (ST_OMP_ATOMIC);
2372 cp = gfc_state_stack->tail;
2373 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2374 np = new_level (cp);
2380 st = next_statement ();
2383 else if (st == ST_ASSIGNMENT)
2386 unexpected_statement (st);
2389 accept_statement (st);
2395 /* Parse the statements of an OpenMP structured block. */
2398 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
2400 gfc_statement st, omp_end_st;
2404 accept_statement (omp_st);
2406 cp = gfc_state_stack->tail;
2407 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
2408 np = new_level (cp);
2414 case ST_OMP_PARALLEL:
2415 omp_end_st = ST_OMP_END_PARALLEL;
2417 case ST_OMP_PARALLEL_SECTIONS:
2418 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
2420 case ST_OMP_SECTIONS:
2421 omp_end_st = ST_OMP_END_SECTIONS;
2423 case ST_OMP_ORDERED:
2424 omp_end_st = ST_OMP_END_ORDERED;
2426 case ST_OMP_CRITICAL:
2427 omp_end_st = ST_OMP_END_CRITICAL;
2430 omp_end_st = ST_OMP_END_MASTER;
2433 omp_end_st = ST_OMP_END_SINGLE;
2435 case ST_OMP_WORKSHARE:
2436 omp_end_st = ST_OMP_END_WORKSHARE;
2438 case ST_OMP_PARALLEL_WORKSHARE:
2439 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
2447 if (workshare_stmts_only)
2449 /* Inside of !$omp workshare, only
2452 where statements and constructs
2453 forall statements and constructs
2457 are allowed. For !$omp critical these
2458 restrictions apply recursively. */
2461 st = next_statement ();
2472 accept_statement (st);
2475 case ST_WHERE_BLOCK:
2476 parse_where_block ();
2479 case ST_FORALL_BLOCK:
2480 parse_forall_block ();
2483 case ST_OMP_PARALLEL:
2484 case ST_OMP_PARALLEL_SECTIONS:
2485 parse_omp_structured_block (st, false);
2488 case ST_OMP_PARALLEL_WORKSHARE:
2489 case ST_OMP_CRITICAL:
2490 parse_omp_structured_block (st, true);
2493 case ST_OMP_PARALLEL_DO:
2494 st = parse_omp_do (st);
2498 parse_omp_atomic ();
2509 st = next_statement ();
2513 st = parse_executable (ST_NONE);
2516 else if (st == ST_OMP_SECTION
2517 && (omp_st == ST_OMP_SECTIONS
2518 || omp_st == ST_OMP_PARALLEL_SECTIONS))
2520 np = new_level (np);
2524 else if (st != omp_end_st)
2525 unexpected_statement (st);
2527 while (st != omp_end_st);
2531 case EXEC_OMP_END_NOWAIT:
2532 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
2534 case EXEC_OMP_CRITICAL:
2535 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
2536 || (new_st.ext.omp_name != NULL
2537 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
2538 gfc_error ("Name after !$omp critical and !$omp end critical does"
2539 " not match at %C");
2540 gfc_free ((char *) new_st.ext.omp_name);
2542 case EXEC_OMP_END_SINGLE:
2543 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
2544 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
2545 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
2546 gfc_free_omp_clauses (new_st.ext.omp_clauses);
2554 gfc_clear_new_st ();
2555 gfc_commit_symbols ();
2556 gfc_warning_check ();
2561 /* Accept a series of executable statements. We return the first
2562 statement that doesn't fit to the caller. Any block statements are
2563 passed on to the correct handler, which usually passes the buck
2566 static gfc_statement
2567 parse_executable (gfc_statement st)
2572 st = next_statement ();
2576 close_flag = check_do_closure ();
2581 case ST_END_PROGRAM:
2584 case ST_END_FUNCTION:
2588 case ST_END_SUBROUTINE:
2593 case ST_SELECT_CASE:
2595 ("%s statement at %C cannot terminate a non-block DO loop",
2596 gfc_ascii_statement (st));
2612 accept_statement (st);
2613 if (close_flag == 1)
2614 return ST_IMPLIED_ENDDO;
2621 case ST_SELECT_CASE:
2622 parse_select_block ();
2627 if (check_do_closure () == 1)
2628 return ST_IMPLIED_ENDDO;
2631 case ST_WHERE_BLOCK:
2632 parse_where_block ();
2635 case ST_FORALL_BLOCK:
2636 parse_forall_block ();
2639 case ST_OMP_PARALLEL:
2640 case ST_OMP_PARALLEL_SECTIONS:
2641 case ST_OMP_SECTIONS:
2642 case ST_OMP_ORDERED:
2643 case ST_OMP_CRITICAL:
2646 parse_omp_structured_block (st, false);
2649 case ST_OMP_WORKSHARE:
2650 case ST_OMP_PARALLEL_WORKSHARE:
2651 parse_omp_structured_block (st, true);
2655 case ST_OMP_PARALLEL_DO:
2656 st = parse_omp_do (st);
2657 if (st == ST_IMPLIED_ENDDO)
2662 parse_omp_atomic ();
2669 st = next_statement ();
2674 /* Parse a series of contained program units. */
2676 static void parse_progunit (gfc_statement);
2679 /* Fix the symbols for sibling functions. These are incorrectly added to
2680 the child namespace as the parser didn't know about this procedure. */
2683 gfc_fixup_sibling_symbols (gfc_symbol * sym, gfc_namespace * siblings)
2687 gfc_symbol *old_sym;
2689 sym->attr.referenced = 1;
2690 for (ns = siblings; ns; ns = ns->sibling)
2692 gfc_find_sym_tree (sym->name, ns, 0, &st);
2696 old_sym = st->n.sym;
2697 if ((old_sym->attr.flavor == FL_PROCEDURE
2698 || old_sym->ts.type == BT_UNKNOWN)
2699 && old_sym->ns == ns
2700 && ! old_sym->attr.contained)
2702 /* Replace it with the symbol from the parent namespace. */
2706 /* Free the old (local) symbol. */
2708 if (old_sym->refs == 0)
2709 gfc_free_symbol (old_sym);
2712 /* Do the same for any contained procedures. */
2713 gfc_fixup_sibling_symbols (sym, ns->contained);
2718 parse_contained (int module)
2720 gfc_namespace *ns, *parent_ns;
2721 gfc_state_data s1, s2;
2726 push_state (&s1, COMP_CONTAINS, NULL);
2727 parent_ns = gfc_current_ns;
2731 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2733 gfc_current_ns->sibling = parent_ns->contained;
2734 parent_ns->contained = gfc_current_ns;
2736 st = next_statement ();
2745 accept_statement (st);
2748 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2751 /* For internal procedures, create/update the symbol in the
2752 parent namespace. */
2756 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2758 ("Contained procedure '%s' at %C is already ambiguous",
2759 gfc_new_block->name);
2762 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2763 &gfc_new_block->declared_at) ==
2766 if (st == ST_FUNCTION)
2767 gfc_add_function (&sym->attr, sym->name,
2768 &gfc_new_block->declared_at);
2770 gfc_add_subroutine (&sym->attr, sym->name,
2771 &gfc_new_block->declared_at);
2775 gfc_commit_symbols ();
2778 sym = gfc_new_block;
2780 /* Mark this as a contained function, so it isn't replaced
2781 by other module functions. */
2782 sym->attr.contained = 1;
2783 sym->attr.referenced = 1;
2785 parse_progunit (ST_NONE);
2787 /* Fix up any sibling functions that refer to this one. */
2788 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2789 /* Or refer to any of its alternate entry points. */
2790 for (el = gfc_current_ns->entries; el; el = el->next)
2791 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2793 gfc_current_ns->code = s2.head;
2794 gfc_current_ns = parent_ns;
2799 /* These statements are associated with the end of the host
2801 case ST_END_FUNCTION:
2803 case ST_END_PROGRAM:
2804 case ST_END_SUBROUTINE:
2805 accept_statement (st);
2809 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2810 gfc_ascii_statement (st));
2811 reject_statement ();
2815 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2816 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2818 /* The first namespace in the list is guaranteed to not have
2819 anything (worthwhile) in it. */
2821 gfc_current_ns = parent_ns;
2823 ns = gfc_current_ns->contained;
2824 gfc_current_ns->contained = ns->sibling;
2825 gfc_free_namespace (ns);
2831 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2834 parse_progunit (gfc_statement st)
2839 st = parse_spec (st);
2849 accept_statement (st);
2859 st = parse_executable (st);
2870 accept_statement (st);
2877 unexpected_statement (st);
2878 reject_statement ();
2879 st = next_statement ();
2885 for (p = gfc_state_stack; p; p = p->previous)
2886 if (p->state == COMP_CONTAINS)
2889 if (gfc_find_state (COMP_MODULE) == SUCCESS)
2894 gfc_error ("CONTAINS statement at %C is already in a contained "
2896 st = next_statement ();
2900 parse_contained (0);
2903 gfc_current_ns->code = gfc_state_stack->head;
2907 /* Come here to complain about a global symbol already in use as
2911 global_used (gfc_gsymbol *sym, locus *where)
2916 where = &gfc_current_locus;
2926 case GSYM_SUBROUTINE:
2927 name = "SUBROUTINE";
2932 case GSYM_BLOCK_DATA:
2933 name = "BLOCK DATA";
2939 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
2943 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
2944 sym->name, where, name, &sym->where);
2948 /* Parse a block data program unit. */
2951 parse_block_data (void)
2954 static locus blank_locus;
2955 static int blank_block=0;
2958 gfc_current_ns->proc_name = gfc_new_block;
2959 gfc_current_ns->is_block_data = 1;
2961 if (gfc_new_block == NULL)
2964 gfc_error ("Blank BLOCK DATA at %C conflicts with "
2965 "prior BLOCK DATA at %L", &blank_locus);
2969 blank_locus = gfc_current_locus;
2974 s = gfc_get_gsymbol (gfc_new_block->name);
2975 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
2976 global_used(s, NULL);
2979 s->type = GSYM_BLOCK_DATA;
2980 s->where = gfc_current_locus;
2985 st = parse_spec (ST_NONE);
2987 while (st != ST_END_BLOCK_DATA)
2989 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
2990 gfc_ascii_statement (st));
2991 reject_statement ();
2992 st = next_statement ();
2997 /* Parse a module subprogram. */
3005 s = gfc_get_gsymbol (gfc_new_block->name);
3006 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
3007 global_used(s, NULL);
3010 s->type = GSYM_MODULE;
3011 s->where = gfc_current_locus;
3015 st = parse_spec (ST_NONE);
3024 parse_contained (1);
3028 accept_statement (st);
3032 gfc_error ("Unexpected %s statement in MODULE at %C",
3033 gfc_ascii_statement (st));
3035 reject_statement ();
3036 st = next_statement ();
3042 /* Add a procedure name to the global symbol table. */
3045 add_global_procedure (int sub)
3049 s = gfc_get_gsymbol(gfc_new_block->name);
3052 || (s->type != GSYM_UNKNOWN && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3053 global_used(s, NULL);
3056 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3057 s->where = gfc_current_locus;
3063 /* Add a program to the global symbol table. */
3066 add_global_program (void)
3070 if (gfc_new_block == NULL)
3072 s = gfc_get_gsymbol (gfc_new_block->name);
3074 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
3075 global_used(s, NULL);
3078 s->type = GSYM_PROGRAM;
3079 s->where = gfc_current_locus;
3085 /* Top level parser. */
3088 gfc_parse_file (void)
3090 int seen_program, errors_before, errors;
3091 gfc_state_data top, s;
3095 top.state = COMP_NONE;
3097 top.previous = NULL;
3098 top.head = top.tail = NULL;
3099 top.do_variable = NULL;
3101 gfc_state_stack = ⊤
3103 gfc_clear_new_st ();
3105 gfc_statement_label = NULL;
3107 if (setjmp (eof_buf))
3108 return FAILURE; /* Come here on unexpected EOF */
3112 /* Exit early for empty files. */
3118 st = next_statement ();
3127 goto duplicate_main;
3129 prog_locus = gfc_current_locus;
3131 push_state (&s, COMP_PROGRAM, gfc_new_block);
3132 main_program_symbol(gfc_current_ns);
3133 accept_statement (st);
3134 add_global_program ();
3135 parse_progunit (ST_NONE);
3139 add_global_procedure (1);
3140 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
3141 accept_statement (st);
3142 parse_progunit (ST_NONE);
3146 add_global_procedure (0);
3147 push_state (&s, COMP_FUNCTION, gfc_new_block);
3148 accept_statement (st);
3149 parse_progunit (ST_NONE);
3153 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
3154 accept_statement (st);
3155 parse_block_data ();
3159 push_state (&s, COMP_MODULE, gfc_new_block);
3160 accept_statement (st);
3162 gfc_get_errors (NULL, &errors_before);
3166 /* Anything else starts a nameless main program block. */
3169 goto duplicate_main;
3171 prog_locus = gfc_current_locus;
3173 push_state (&s, COMP_PROGRAM, gfc_new_block);
3174 main_program_symbol(gfc_current_ns);
3175 parse_progunit (st);
3179 gfc_current_ns->code = s.head;
3181 gfc_resolve (gfc_current_ns);
3183 /* Dump the parse tree if requested. */
3184 if (gfc_option.verbose)
3185 gfc_show_namespace (gfc_current_ns);
3187 gfc_get_errors (NULL, &errors);
3188 if (s.state == COMP_MODULE)
3190 gfc_dump_module (s.sym->name, errors_before == errors);
3191 if (errors == 0 && ! gfc_option.flag_no_backend)
3192 gfc_generate_module_code (gfc_current_ns);
3196 if (errors == 0 && ! gfc_option.flag_no_backend)
3197 gfc_generate_code (gfc_current_ns);
3208 /* If we see a duplicate main program, shut down. If the second
3209 instance is an implied main program, ie data decls or executable
3210 statements, we're in for lots of errors. */
3211 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
3212 reject_statement ();