* target.h (struct gcc_target): Add new field to struct cxx: import_export_class.
[official-gcc.git] / gcc / fortran / parse.c
blob3f9ca813c65d1bad6f0edf8792d84ba6300073cb
1 /* Main parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation,
3 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 2, or (at your option) any later
11 version.
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
16 for more details.
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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
24 #include "config.h"
25 #include <string.h>
26 #include <setjmp.h>
28 #include "gfortran.h"
29 #include "match.h"
30 #include "parse.h"
32 /* Current statement label. Zero means no statement label. Because
33 new_st can get wiped during statement matching, we have to keep it
34 separate. */
36 gfc_st_label *gfc_statement_label;
38 static locus label_locus;
39 static jmp_buf eof;
41 gfc_state_data *gfc_state_stack;
43 /* TODO: Re-order functions to kill these forward decls. */
44 static void check_statement_label (gfc_statement);
45 static void undo_new_statement (void);
46 static void reject_statement (void);
48 /* A sort of half-matching function. We try to match the word on the
49 input with the passed string. If this succeeds, we call the
50 keyword-dependent matching function that will match the rest of the
51 statement. For single keywords, the matching subroutine is
52 gfc_match_eos(). */
54 static match
55 match_word (const char *str, match (*subr) (void), locus * old_locus)
57 match m;
59 if (str != NULL)
61 m = gfc_match (str);
62 if (m != MATCH_YES)
63 return m;
66 m = (*subr) ();
68 if (m != MATCH_YES)
70 gfc_current_locus = *old_locus;
71 reject_statement ();
74 return m;
78 /* Figure out what the next statement is, (mostly) regardless of
79 proper ordering. */
81 #define match(keyword, subr, st) \
82 if (match_word(keyword, subr, &old_locus) == MATCH_YES) \
83 return st; \
84 else \
85 undo_new_statement ();
87 static gfc_statement
88 decode_statement (void)
90 gfc_statement st;
91 locus old_locus;
92 match m;
93 int c;
95 #ifdef GFC_DEBUG
96 gfc_symbol_state ();
97 #endif
99 gfc_clear_error (); /* Clear any pending errors. */
100 gfc_clear_warning (); /* Clear any pending warnings. */
102 if (gfc_match_eos () == MATCH_YES)
103 return ST_NONE;
105 old_locus = gfc_current_locus;
107 /* Try matching a data declaration or function declaration. The
108 input "REALFUNCTIONA(N)" can mean several things in different
109 contexts, so it (and its relatives) get special treatment. */
111 if (gfc_current_state () == COMP_NONE
112 || gfc_current_state () == COMP_INTERFACE
113 || gfc_current_state () == COMP_CONTAINS)
115 m = gfc_match_function_decl ();
116 if (m == MATCH_YES)
117 return ST_FUNCTION;
118 else if (m == MATCH_ERROR)
119 reject_statement ();
121 gfc_undo_symbols ();
122 gfc_current_locus = old_locus;
125 /* Match statements whose error messages are meant to be overwritten
126 by something better. */
128 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
129 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
130 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
132 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
134 /* Try to match a subroutine statement, which has the same optional
135 prefixes that functions can have. */
137 if (gfc_match_subroutine () == MATCH_YES)
138 return ST_SUBROUTINE;
139 gfc_undo_symbols ();
140 gfc_current_locus = old_locus;
142 /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
143 might begin with a block label. The match functions for these
144 statements are unusual in that their keyword is not seen before
145 the matcher is called. */
147 if (gfc_match_if (&st) == MATCH_YES)
148 return st;
149 gfc_undo_symbols ();
150 gfc_current_locus = old_locus;
152 if (gfc_match_where (&st) == MATCH_YES)
153 return st;
154 gfc_undo_symbols ();
155 gfc_current_locus = old_locus;
157 if (gfc_match_forall (&st) == MATCH_YES)
158 return st;
159 gfc_undo_symbols ();
160 gfc_current_locus = old_locus;
162 match (NULL, gfc_match_do, ST_DO);
163 match (NULL, gfc_match_select, ST_SELECT_CASE);
165 /* General statement matching: Instead of testing every possible
166 statement, we eliminate most possibilities by peeking at the
167 first character. */
169 c = gfc_peek_char ();
171 switch (c)
173 case 'a':
174 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
175 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
176 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
177 break;
179 case 'b':
180 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
181 match ("block data% ", gfc_match_block_data, ST_BLOCK_DATA);
182 break;
184 case 'c':
185 match ("call", gfc_match_call, ST_CALL);
186 match ("close", gfc_match_close, ST_CLOSE);
187 match ("continue", gfc_match_continue, ST_CONTINUE);
188 match ("cycle", gfc_match_cycle, ST_CYCLE);
189 match ("case", gfc_match_case, ST_CASE);
190 match ("common", gfc_match_common, ST_COMMON);
191 match ("contains", gfc_match_eos, ST_CONTAINS);
192 break;
194 case 'd':
195 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
196 match ("data", gfc_match_data, ST_DATA);
197 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
198 break;
200 case 'e':
201 match ("end file", gfc_match_endfile, ST_END_FILE);
202 match ("exit", gfc_match_exit, ST_EXIT);
203 match ("else", gfc_match_else, ST_ELSE);
204 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
205 match ("else if", gfc_match_elseif, ST_ELSEIF);
207 if (gfc_match_end (&st) == MATCH_YES)
208 return st;
210 match ("entry% ", gfc_match_entry, ST_ENTRY);
211 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
212 match ("external", gfc_match_external, ST_ATTR_DECL);
213 break;
215 case 'f':
216 match ("format", gfc_match_format, ST_FORMAT);
217 break;
219 case 'g':
220 match ("go to", gfc_match_goto, ST_GOTO);
221 break;
223 case 'i':
224 match ("inquire", gfc_match_inquire, ST_INQUIRE);
225 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
226 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
227 match ("interface", gfc_match_interface, ST_INTERFACE);
228 match ("intent", gfc_match_intent, ST_ATTR_DECL);
229 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
230 break;
232 case 'm':
233 match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
234 match ("module", gfc_match_module, ST_MODULE);
235 break;
237 case 'n':
238 match ("nullify", gfc_match_nullify, ST_NULLIFY);
239 match ("namelist", gfc_match_namelist, ST_NAMELIST);
240 break;
242 case 'o':
243 match ("open", gfc_match_open, ST_OPEN);
244 match ("optional", gfc_match_optional, ST_ATTR_DECL);
245 break;
247 case 'p':
248 match ("print", gfc_match_print, ST_WRITE);
249 match ("parameter", gfc_match_parameter, ST_PARAMETER);
250 match ("pause", gfc_match_pause, ST_PAUSE);
251 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
252 if (gfc_match_private (&st) == MATCH_YES)
253 return st;
254 match ("program", gfc_match_program, ST_PROGRAM);
255 if (gfc_match_public (&st) == MATCH_YES)
256 return st;
257 break;
259 case 'r':
260 match ("read", gfc_match_read, ST_READ);
261 match ("return", gfc_match_return, ST_RETURN);
262 match ("rewind", gfc_match_rewind, ST_REWIND);
263 break;
265 case 's':
266 match ("sequence", gfc_match_eos, ST_SEQUENCE);
267 match ("stop", gfc_match_stop, ST_STOP);
268 match ("save", gfc_match_save, ST_ATTR_DECL);
269 break;
271 case 't':
272 match ("target", gfc_match_target, ST_ATTR_DECL);
273 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
274 break;
276 case 'u':
277 match ("use% ", gfc_match_use, ST_USE);
278 break;
280 case 'w':
281 match ("write", gfc_match_write, ST_WRITE);
282 break;
285 /* All else has failed, so give up. See if any of the matchers has
286 stored an error message of some sort. */
288 if (gfc_error_check () == 0)
289 gfc_error_now ("Unclassifiable statement at %C");
291 reject_statement ();
293 gfc_error_recovery ();
295 return ST_NONE;
298 #undef match
301 /* Get the next statement in free form source. */
303 static gfc_statement
304 next_free (void)
306 match m;
307 int c, d;
309 gfc_gobble_whitespace ();
311 c = gfc_peek_char ();
313 if (ISDIGIT (c))
315 /* Found a statement label? */
316 m = gfc_match_st_label (&gfc_statement_label, 0);
318 d = gfc_peek_char ();
319 if (m != MATCH_YES || !gfc_is_whitespace (d))
323 /* Skip the bad statement label. */
324 gfc_warning_now ("Ignoring bad statement label at %C");
325 c = gfc_next_char ();
327 while (ISDIGIT (c));
329 else
331 label_locus = gfc_current_locus;
333 if (gfc_statement_label->value == 0)
335 gfc_warning_now ("Ignoring statement label of zero at %C");
336 gfc_free_st_label (gfc_statement_label);
337 gfc_statement_label = NULL;
340 gfc_gobble_whitespace ();
342 if (gfc_match_eos () == MATCH_YES)
344 gfc_warning_now
345 ("Ignoring statement label in empty statement at %C");
346 gfc_free_st_label (gfc_statement_label);
347 gfc_statement_label = NULL;
348 return ST_NONE;
353 return decode_statement ();
357 /* Get the next statement in fixed-form source. */
359 static gfc_statement
360 next_fixed (void)
362 int label, digit_flag, i;
363 locus loc;
364 char c;
366 if (!gfc_at_bol ())
367 return decode_statement ();
369 /* Skip past the current label field, parsing a statement label if
370 one is there. This is a weird number parser, since the number is
371 contained within five columns and can have any kind of embedded
372 spaces. We also check for characters that make the rest of the
373 line a comment. */
375 label = 0;
376 digit_flag = 0;
378 for (i = 0; i < 5; i++)
380 c = gfc_next_char_literal (0);
382 switch (c)
384 case ' ':
385 break;
387 case '0':
388 case '1':
389 case '2':
390 case '3':
391 case '4':
392 case '5':
393 case '6':
394 case '7':
395 case '8':
396 case '9':
397 label = label * 10 + c - '0';
398 label_locus = gfc_current_locus;
399 digit_flag = 1;
400 break;
402 /* Comments have already been skipped by the time we get
403 here so don't bother checking for them. */
405 default:
406 gfc_buffer_error (0);
407 gfc_error ("Non-numeric character in statement label at %C");
408 return ST_NONE;
412 if (digit_flag)
414 if (label == 0)
415 gfc_warning_now ("Zero is not a valid statement label at %C");
416 else
418 /* We've found a valid statement label. */
419 gfc_statement_label = gfc_get_st_label (label);
423 /* Since this line starts a statement, it cannot be a continuation
424 of a previous statement. If we see something here besides a
425 space or zero, it must be a bad continuation line. */
427 c = gfc_next_char_literal (0);
428 if (c == '\n')
429 goto blank_line;
431 if (c != ' ' && c!= '0')
433 gfc_buffer_error (0);
434 gfc_error ("Bad continuation line at %C");
435 return ST_NONE;
438 /* Now that we've taken care of the statement label columns, we have
439 to make sure that the first nonblank character is not a '!'. If
440 it is, the rest of the line is a comment. */
444 loc = gfc_current_locus;
445 c = gfc_next_char_literal (0);
447 while (gfc_is_whitespace (c));
449 if (c == '!')
450 goto blank_line;
451 gfc_current_locus = loc;
453 if (gfc_match_eos () == MATCH_YES)
454 goto blank_line;
456 /* At this point, we've got a nonblank statement to parse. */
457 return decode_statement ();
459 blank_line:
460 if (digit_flag)
461 gfc_warning ("Statement label in blank line will be " "ignored at %C");
462 gfc_advance_line ();
463 return ST_NONE;
467 /* Return the next non-ST_NONE statement to the caller. We also worry
468 about including files and the ends of include files at this stage. */
470 static gfc_statement
471 next_statement (void)
473 gfc_statement st;
475 gfc_new_block = NULL;
477 for (;;)
479 gfc_statement_label = NULL;
480 gfc_buffer_error (1);
482 if (gfc_at_eol ())
483 gfc_advance_line ();
485 gfc_skip_comments ();
487 if (gfc_at_end ())
489 st = ST_NONE;
490 break;
493 st =
494 (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
496 if (st != ST_NONE)
497 break;
500 gfc_buffer_error (0);
502 if (st != ST_NONE)
503 check_statement_label (st);
505 return st;
509 /****************************** Parser ***********************************/
511 /* The parser subroutines are of type 'try' that fail if the file ends
512 unexpectedly. */
514 /* Macros that expand to case-labels for various classes of
515 statements. Start with executable statements that directly do
516 things. */
518 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
519 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
520 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
521 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
522 case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
523 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
524 case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: case ST_LABEL_ASSIGNMENT
526 /* Statements that mark other executable statements. */
528 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
529 case ST_WHERE_BLOCK: case ST_SELECT_CASE
531 /* Declaration statements */
533 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
534 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
535 case ST_TYPE: case ST_INTERFACE
537 /* Block end statements. Errors associated with interchanging these
538 are detected in gfc_match_end(). */
540 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
541 case ST_END_PROGRAM: case ST_END_SUBROUTINE
544 /* Push a new state onto the stack. */
546 static void
547 push_state (gfc_state_data * p, gfc_compile_state new_state, gfc_symbol * sym)
550 p->state = new_state;
551 p->previous = gfc_state_stack;
552 p->sym = sym;
553 p->head = p->tail = NULL;
555 gfc_state_stack = p;
559 /* Pop the current state. */
561 static void
562 pop_state (void)
565 gfc_state_stack = gfc_state_stack->previous;
569 /* Try to find the given state in the state stack. */
572 gfc_find_state (gfc_compile_state state)
574 gfc_state_data *p;
576 for (p = gfc_state_stack; p; p = p->previous)
577 if (p->state == state)
578 break;
580 return (p == NULL) ? FAILURE : SUCCESS;
584 /* Starts a new level in the statement list. */
586 static gfc_code *
587 new_level (gfc_code * q)
589 gfc_code *p;
591 p = q->block = gfc_get_code ();
593 gfc_state_stack->head = gfc_state_stack->tail = p;
595 return p;
599 /* Add the current new_st code structure and adds it to the current
600 program unit. As a side-effect, it zeroes the new_st. */
602 static gfc_code *
603 add_statement (void)
605 gfc_code *p;
607 p = gfc_get_code ();
608 *p = new_st;
610 p->loc = gfc_current_locus;
612 if (gfc_state_stack->head == NULL)
613 gfc_state_stack->head = p;
614 else
615 gfc_state_stack->tail->next = p;
617 while (p->next != NULL)
618 p = p->next;
620 gfc_state_stack->tail = p;
622 gfc_clear_new_st ();
624 return p;
628 /* Frees everything associated with the current statement. */
630 static void
631 undo_new_statement (void)
633 gfc_free_statements (new_st.block);
634 gfc_free_statements (new_st.next);
635 gfc_free_statement (&new_st);
636 gfc_clear_new_st ();
640 /* If the current statement has a statement label, make sure that it
641 is allowed to, or should have one. */
643 static void
644 check_statement_label (gfc_statement st)
646 gfc_sl_type type;
648 if (gfc_statement_label == NULL)
650 if (st == ST_FORMAT)
651 gfc_error ("FORMAT statement at %L does not have a statement label",
652 &new_st.loc);
653 return;
656 switch (st)
658 case ST_END_PROGRAM:
659 case ST_END_FUNCTION:
660 case ST_END_SUBROUTINE:
661 case ST_ENDDO:
662 case ST_ENDIF:
663 case ST_END_SELECT:
664 case_executable:
665 case_exec_markers:
666 type = ST_LABEL_TARGET;
667 break;
669 case ST_FORMAT:
670 type = ST_LABEL_FORMAT;
671 break;
673 /* Statement labels are not restricted from appearing on a
674 particular line. However, there are plenty of situations
675 where the resulting label can't be referenced. */
677 default:
678 type = ST_LABEL_BAD_TARGET;
679 break;
682 gfc_define_st_label (gfc_statement_label, type, &label_locus);
684 new_st.here = gfc_statement_label;
688 /* Figures out what the enclosing program unit is. This will be a
689 function, subroutine, program, block data or module. */
691 gfc_state_data *
692 gfc_enclosing_unit (gfc_compile_state * result)
694 gfc_state_data *p;
696 for (p = gfc_state_stack; p; p = p->previous)
697 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
698 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
699 || p->state == COMP_PROGRAM)
702 if (result != NULL)
703 *result = p->state;
704 return p;
707 if (result != NULL)
708 *result = COMP_PROGRAM;
709 return NULL;
713 /* Translate a statement enum to a string. */
715 const char *
716 gfc_ascii_statement (gfc_statement st)
718 const char *p;
720 switch (st)
722 case ST_ARITHMETIC_IF:
723 p = "arithmetic IF";
724 break;
725 case ST_ALLOCATE:
726 p = "ALLOCATE";
727 break;
728 case ST_ATTR_DECL:
729 p = "attribute declaration";
730 break;
731 case ST_BACKSPACE:
732 p = "BACKSPACE";
733 break;
734 case ST_BLOCK_DATA:
735 p = "BLOCK DATA";
736 break;
737 case ST_CALL:
738 p = "CALL";
739 break;
740 case ST_CASE:
741 p = "CASE";
742 break;
743 case ST_CLOSE:
744 p = "CLOSE";
745 break;
746 case ST_COMMON:
747 p = "COMMON";
748 break;
749 case ST_CONTINUE:
750 p = "CONTINUE";
751 break;
752 case ST_CONTAINS:
753 p = "CONTAINS";
754 break;
755 case ST_CYCLE:
756 p = "CYCLE";
757 break;
758 case ST_DATA_DECL:
759 p = "data declaration";
760 break;
761 case ST_DATA:
762 p = "DATA";
763 break;
764 case ST_DEALLOCATE:
765 p = "DEALLOCATE";
766 break;
767 case ST_DERIVED_DECL:
768 p = "Derived type declaration";
769 break;
770 case ST_DO:
771 p = "DO";
772 break;
773 case ST_ELSE:
774 p = "ELSE";
775 break;
776 case ST_ELSEIF:
777 p = "ELSE IF";
778 break;
779 case ST_ELSEWHERE:
780 p = "ELSEWHERE";
781 break;
782 case ST_END_BLOCK_DATA:
783 p = "END BLOCK DATA";
784 break;
785 case ST_ENDDO:
786 p = "END DO";
787 break;
788 case ST_END_FILE:
789 p = "END FILE";
790 break;
791 case ST_END_FORALL:
792 p = "END FORALL";
793 break;
794 case ST_END_FUNCTION:
795 p = "END FUNCTION";
796 break;
797 case ST_ENDIF:
798 p = "END IF";
799 break;
800 case ST_END_INTERFACE:
801 p = "END INTERFACE";
802 break;
803 case ST_END_MODULE:
804 p = "END MODULE";
805 break;
806 case ST_END_PROGRAM:
807 p = "END PROGRAM";
808 break;
809 case ST_END_SELECT:
810 p = "END SELECT";
811 break;
812 case ST_END_SUBROUTINE:
813 p = "END SUBROUTINE";
814 break;
815 case ST_END_WHERE:
816 p = "END WHERE";
817 break;
818 case ST_END_TYPE:
819 p = "END TYPE";
820 break;
821 case ST_ENTRY:
822 p = "ENTRY";
823 break;
824 case ST_EQUIVALENCE:
825 p = "EQUIVALENCE";
826 break;
827 case ST_EXIT:
828 p = "EXIT";
829 break;
830 case ST_FORALL_BLOCK: /* Fall through */
831 case ST_FORALL:
832 p = "FORALL";
833 break;
834 case ST_FORMAT:
835 p = "FORMAT";
836 break;
837 case ST_FUNCTION:
838 p = "FUNCTION";
839 break;
840 case ST_GOTO:
841 p = "GOTO";
842 break;
843 case ST_IF_BLOCK:
844 p = "block IF";
845 break;
846 case ST_IMPLICIT:
847 p = "IMPLICIT";
848 break;
849 case ST_IMPLICIT_NONE:
850 p = "IMPLICIT NONE";
851 break;
852 case ST_IMPLIED_ENDDO:
853 p = "implied END DO";
854 break;
855 case ST_INQUIRE:
856 p = "INQUIRE";
857 break;
858 case ST_INTERFACE:
859 p = "INTERFACE";
860 break;
861 case ST_PARAMETER:
862 p = "PARAMETER";
863 break;
864 case ST_PRIVATE:
865 p = "PRIVATE";
866 break;
867 case ST_PUBLIC:
868 p = "PUBLIC";
869 break;
870 case ST_MODULE:
871 p = "MODULE";
872 break;
873 case ST_PAUSE:
874 p = "PAUSE";
875 break;
876 case ST_MODULE_PROC:
877 p = "MODULE PROCEDURE";
878 break;
879 case ST_NAMELIST:
880 p = "NAMELIST";
881 break;
882 case ST_NULLIFY:
883 p = "NULLIFY";
884 break;
885 case ST_OPEN:
886 p = "OPEN";
887 break;
888 case ST_PROGRAM:
889 p = "PROGRAM";
890 break;
891 case ST_READ:
892 p = "READ";
893 break;
894 case ST_RETURN:
895 p = "RETURN";
896 break;
897 case ST_REWIND:
898 p = "REWIND";
899 break;
900 case ST_STOP:
901 p = "STOP";
902 break;
903 case ST_SUBROUTINE:
904 p = "SUBROUTINE";
905 break;
906 case ST_TYPE:
907 p = "TYPE";
908 break;
909 case ST_USE:
910 p = "USE";
911 break;
912 case ST_WHERE_BLOCK: /* Fall through */
913 case ST_WHERE:
914 p = "WHERE";
915 break;
916 case ST_WRITE:
917 p = "WRITE";
918 break;
919 case ST_ASSIGNMENT:
920 p = "assignment";
921 break;
922 case ST_POINTER_ASSIGNMENT:
923 p = "pointer assignment";
924 break;
925 case ST_SELECT_CASE:
926 p = "SELECT CASE";
927 break;
928 case ST_SEQUENCE:
929 p = "SEQUENCE";
930 break;
931 case ST_SIMPLE_IF:
932 p = "Simple IF";
933 break;
934 case ST_STATEMENT_FUNCTION:
935 p = "STATEMENT FUNCTION";
936 break;
937 case ST_LABEL_ASSIGNMENT:
938 p = "LABEL ASSIGNMENT";
939 break;
940 default:
941 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
944 return p;
948 /* Return the name of a compile state. */
950 const char *
951 gfc_state_name (gfc_compile_state state)
953 const char *p;
955 switch (state)
957 case COMP_PROGRAM:
958 p = "a PROGRAM";
959 break;
960 case COMP_MODULE:
961 p = "a MODULE";
962 break;
963 case COMP_SUBROUTINE:
964 p = "a SUBROUTINE";
965 break;
966 case COMP_FUNCTION:
967 p = "a FUNCTION";
968 break;
969 case COMP_BLOCK_DATA:
970 p = "a BLOCK DATA";
971 break;
972 case COMP_INTERFACE:
973 p = "an INTERFACE";
974 break;
975 case COMP_DERIVED:
976 p = "a DERIVED TYPE block";
977 break;
978 case COMP_IF:
979 p = "an IF-THEN block";
980 break;
981 case COMP_DO:
982 p = "a DO block";
983 break;
984 case COMP_SELECT:
985 p = "a SELECT block";
986 break;
987 case COMP_FORALL:
988 p = "a FORALL block";
989 break;
990 case COMP_WHERE:
991 p = "a WHERE block";
992 break;
993 case COMP_CONTAINS:
994 p = "a contained subprogram";
995 break;
997 default:
998 gfc_internal_error ("gfc_state_name(): Bad state");
1001 return p;
1005 /* Do whatever is necessary to accept the last statement. */
1007 static void
1008 accept_statement (gfc_statement st)
1011 switch (st)
1013 case ST_USE:
1014 gfc_use_module ();
1015 break;
1017 case ST_IMPLICIT_NONE:
1018 gfc_set_implicit_none ();
1019 break;
1021 case ST_IMPLICIT:
1022 gfc_set_implicit ();
1023 break;
1025 case ST_FUNCTION:
1026 case ST_SUBROUTINE:
1027 case ST_MODULE:
1028 gfc_current_ns->proc_name = gfc_new_block;
1029 break;
1031 /* If the statement is the end of a block, lay down a special code
1032 that allows a branch to the end of the block from within the
1033 construct. */
1035 case ST_ENDIF:
1036 case ST_ENDDO:
1037 case ST_END_SELECT:
1038 if (gfc_statement_label != NULL)
1040 new_st.op = EXEC_NOP;
1041 add_statement ();
1044 break;
1046 /* The end-of-program unit statements do not get the special
1047 marker and require a statement of some sort if they are a
1048 branch target. */
1050 case ST_END_PROGRAM:
1051 case ST_END_FUNCTION:
1052 case ST_END_SUBROUTINE:
1053 if (gfc_statement_label != NULL)
1055 new_st.op = EXEC_RETURN;
1056 add_statement ();
1059 break;
1061 case ST_BLOCK_DATA:
1063 gfc_symbol *block_data = NULL;
1064 symbol_attribute attr;
1066 gfc_get_symbol ("_BLOCK_DATA__", gfc_current_ns, &block_data);
1067 gfc_clear_attr (&attr);
1068 attr.flavor = FL_PROCEDURE;
1069 attr.proc = PROC_UNKNOWN;
1070 attr.subroutine = 1;
1071 attr.access = ACCESS_PUBLIC;
1072 block_data->attr = attr;
1073 gfc_current_ns->proc_name = block_data;
1074 gfc_commit_symbols ();
1077 break;
1079 case_executable:
1080 case_exec_markers:
1081 add_statement ();
1082 break;
1084 default:
1085 break;
1088 gfc_commit_symbols ();
1089 gfc_warning_check ();
1090 gfc_clear_new_st ();
1094 /* Undo anything tentative that has been built for the current
1095 statement. */
1097 static void
1098 reject_statement (void)
1101 gfc_undo_symbols ();
1102 gfc_clear_warning ();
1103 undo_new_statement ();
1107 /* Generic complaint about an out of order statement. We also do
1108 whatever is necessary to clean up. */
1110 static void
1111 unexpected_statement (gfc_statement st)
1114 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1116 reject_statement ();
1120 /* Given the next statement seen by the matcher, make sure that it is
1121 in proper order with the last. This subroutine is initialized by
1122 calling it with an argument of ST_NONE. If there is a problem, we
1123 issue an error and return FAILURE. Otherwise we return SUCCESS.
1125 Individual parsers need to verify that the statements seen are
1126 valid before calling here, ie ENTRY statements are not allowed in
1127 INTERFACE blocks. The following diagram is taken from the standard:
1129 +---------------------------------------+
1130 | program subroutine function module |
1131 +---------------------------------------+
1132 | use |
1133 |---------------------------------------+
1134 | | implicit none |
1135 | +-----------+------------------+
1136 | | parameter | implicit |
1137 | +-----------+------------------+
1138 | format | | derived type |
1139 | entry | parameter | interface |
1140 | | data | specification |
1141 | | | statement func |
1142 | +-----------+------------------+
1143 | | data | executable |
1144 +--------+-----------+------------------+
1145 | contains |
1146 +---------------------------------------+
1147 | internal module/subprogram |
1148 +---------------------------------------+
1149 | end |
1150 +---------------------------------------+
1154 typedef struct
1156 enum
1157 { ORDER_START, ORDER_USE, ORDER_IMPLICIT_NONE, ORDER_IMPLICIT,
1158 ORDER_SPEC, ORDER_EXEC
1160 state;
1161 gfc_statement last_statement;
1162 locus where;
1164 st_state;
1166 static try
1167 verify_st_order (st_state * p, gfc_statement st)
1170 switch (st)
1172 case ST_NONE:
1173 p->state = ORDER_START;
1174 break;
1176 case ST_USE:
1177 if (p->state > ORDER_USE)
1178 goto order;
1179 p->state = ORDER_USE;
1180 break;
1182 case ST_IMPLICIT_NONE:
1183 if (p->state > ORDER_IMPLICIT_NONE)
1184 goto order;
1186 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1187 statement disqualifies a USE but not an IMPLICIT NONE.
1188 Duplicate IMPLICIT NONEs are caught when the implicit types
1189 are set. */
1191 p->state = ORDER_IMPLICIT_NONE;
1192 break;
1194 case ST_IMPLICIT:
1195 if (p->state > ORDER_IMPLICIT)
1196 goto order;
1197 p->state = ORDER_IMPLICIT;
1198 break;
1200 case ST_FORMAT:
1201 case ST_ENTRY:
1202 if (p->state < ORDER_IMPLICIT_NONE)
1203 p->state = ORDER_IMPLICIT_NONE;
1204 break;
1206 case ST_PARAMETER:
1207 if (p->state >= ORDER_EXEC)
1208 goto order;
1209 if (p->state < ORDER_IMPLICIT)
1210 p->state = ORDER_IMPLICIT;
1211 break;
1213 case ST_DATA:
1214 if (p->state < ORDER_SPEC)
1215 p->state = ORDER_SPEC;
1216 break;
1218 case ST_PUBLIC:
1219 case ST_PRIVATE:
1220 case ST_DERIVED_DECL:
1221 case_decl:
1222 if (p->state >= ORDER_EXEC)
1223 goto order;
1224 if (p->state < ORDER_SPEC)
1225 p->state = ORDER_SPEC;
1226 break;
1228 case_executable:
1229 case_exec_markers:
1230 if (p->state < ORDER_EXEC)
1231 p->state = ORDER_EXEC;
1232 break;
1234 default:
1235 gfc_internal_error
1236 ("Unexpected %s statement in verify_st_order() at %C",
1237 gfc_ascii_statement (st));
1240 /* All is well, record the statement in case we need it next time. */
1241 p->where = gfc_current_locus;
1242 p->last_statement = st;
1243 return SUCCESS;
1245 order:
1246 gfc_error ("%s statement at %C cannot follow %s statement at %L",
1247 gfc_ascii_statement (st),
1248 gfc_ascii_statement (p->last_statement), &p->where);
1250 return FAILURE;
1254 /* Handle an unexpected end of file. This is a show-stopper... */
1256 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1258 static void
1259 unexpected_eof (void)
1261 gfc_state_data *p;
1263 gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1265 /* Memory cleanup. Move to "second to last". */
1266 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1267 p = p->previous);
1269 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1270 gfc_done_2 ();
1272 longjmp (eof, 1);
1276 /* Parse a derived type. */
1278 static void
1279 parse_derived (void)
1281 int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1282 gfc_statement st;
1283 gfc_component *c;
1284 gfc_state_data s;
1286 error_flag = 0;
1288 accept_statement (ST_DERIVED_DECL);
1289 push_state (&s, COMP_DERIVED, gfc_new_block);
1291 gfc_new_block->component_access = ACCESS_PUBLIC;
1292 seen_private = 0;
1293 seen_sequence = 0;
1294 seen_component = 0;
1296 compiling_type = 1;
1298 while (compiling_type)
1300 st = next_statement ();
1301 switch (st)
1303 case ST_NONE:
1304 unexpected_eof ();
1306 case ST_DATA_DECL:
1307 accept_statement (st);
1308 seen_component = 1;
1309 break;
1311 case ST_END_TYPE:
1312 compiling_type = 0;
1314 if (!seen_component)
1316 gfc_error ("Derived type definition at %C has no components");
1317 error_flag = 1;
1320 accept_statement (ST_END_TYPE);
1321 break;
1323 case ST_PRIVATE:
1324 if (gfc_find_state (COMP_MODULE) == FAILURE)
1326 gfc_error
1327 ("PRIVATE statement in TYPE at %C must be inside a MODULE");
1328 error_flag = 1;
1329 break;
1332 if (seen_component)
1334 gfc_error ("PRIVATE statement at %C must precede "
1335 "structure components");
1336 error_flag = 1;
1337 break;
1340 if (seen_private)
1342 gfc_error ("Duplicate PRIVATE statement at %C");
1343 error_flag = 1;
1346 s.sym->component_access = ACCESS_PRIVATE;
1347 accept_statement (ST_PRIVATE);
1348 seen_private = 1;
1349 break;
1351 case ST_SEQUENCE:
1352 if (seen_component)
1354 gfc_error ("SEQUENCE statement at %C must precede "
1355 "structure components");
1356 error_flag = 1;
1357 break;
1360 if (gfc_current_block ()->attr.sequence)
1361 gfc_warning ("SEQUENCE attribute at %C already specified in "
1362 "TYPE statement");
1364 if (seen_sequence)
1366 gfc_error ("Duplicate SEQUENCE statement at %C");
1367 error_flag = 1;
1370 seen_sequence = 1;
1371 gfc_add_sequence (&gfc_current_block ()->attr, NULL);
1372 break;
1374 default:
1375 unexpected_statement (st);
1376 break;
1380 /* Sanity checks on the structure. If the structure has the
1381 SEQUENCE attribute, then all component structures must also have
1382 SEQUENCE. */
1383 if (error_flag == 0 && gfc_current_block ()->attr.sequence)
1384 for (c = gfc_current_block ()->components; c; c = c->next)
1386 if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
1388 gfc_error
1389 ("Component %s of SEQUENCE type declared at %C does not "
1390 "have the SEQUENCE attribute", c->ts.derived->name);
1394 pop_state ();
1399 /* Parse an interface. We must be able to deal with the possibility
1400 of recursive interfaces. The parse_spec() subroutine is mutually
1401 recursive with parse_interface(). */
1403 static gfc_statement parse_spec (gfc_statement);
1405 static void
1406 parse_interface (void)
1408 gfc_compile_state new_state, current_state;
1409 gfc_symbol *prog_unit, *sym;
1410 gfc_interface_info save;
1411 gfc_state_data s1, s2;
1412 gfc_statement st;
1414 accept_statement (ST_INTERFACE);
1416 current_interface.ns = gfc_current_ns;
1417 save = current_interface;
1419 sym = (current_interface.type == INTERFACE_GENERIC
1420 || current_interface.type == INTERFACE_USER_OP) ? gfc_new_block : NULL;
1422 push_state (&s1, COMP_INTERFACE, sym);
1423 current_state = COMP_NONE;
1425 loop:
1426 gfc_current_ns = gfc_get_namespace (current_interface.ns);
1428 st = next_statement ();
1429 switch (st)
1431 case ST_NONE:
1432 unexpected_eof ();
1434 case ST_SUBROUTINE:
1435 new_state = COMP_SUBROUTINE;
1436 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1437 gfc_new_block->formal, NULL);
1438 break;
1440 case ST_FUNCTION:
1441 new_state = COMP_FUNCTION;
1442 gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1443 gfc_new_block->formal, NULL);
1444 break;
1446 case ST_MODULE_PROC: /* The module procedure matcher makes
1447 sure the context is correct. */
1448 accept_statement (st);
1449 gfc_free_namespace (gfc_current_ns);
1450 goto loop;
1452 case ST_END_INTERFACE:
1453 gfc_free_namespace (gfc_current_ns);
1454 gfc_current_ns = current_interface.ns;
1455 goto done;
1457 default:
1458 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1459 gfc_ascii_statement (st));
1460 reject_statement ();
1461 gfc_free_namespace (gfc_current_ns);
1462 goto loop;
1466 /* Make sure that a generic interface has only subroutines or
1467 functions and that the generic name has the right attribute. */
1468 if (current_interface.type == INTERFACE_GENERIC)
1470 if (current_state == COMP_NONE)
1472 if (new_state == COMP_FUNCTION)
1473 gfc_add_function (&sym->attr, NULL);
1474 if (new_state == COMP_SUBROUTINE)
1475 gfc_add_subroutine (&sym->attr, NULL);
1477 current_state = new_state;
1479 else
1481 if (new_state != current_state)
1483 if (new_state == COMP_SUBROUTINE)
1484 gfc_error
1485 ("SUBROUTINE at %C does not belong in a generic function "
1486 "interface");
1488 if (new_state == COMP_FUNCTION)
1489 gfc_error
1490 ("FUNCTION at %C does not belong in a generic subroutine "
1491 "interface");
1496 push_state (&s2, new_state, gfc_new_block);
1497 accept_statement (st);
1498 prog_unit = gfc_new_block;
1499 prog_unit->formal_ns = gfc_current_ns;
1501 decl:
1502 /* Read data declaration statements. */
1503 st = parse_spec (ST_NONE);
1505 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1507 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1508 gfc_ascii_statement (st));
1509 reject_statement ();
1510 goto decl;
1513 current_interface = save;
1514 gfc_add_interface (prog_unit);
1516 pop_state ();
1517 goto loop;
1519 done:
1520 pop_state ();
1524 /* Parse a set of specification statements. Returns the statement
1525 that doesn't fit. */
1527 static gfc_statement
1528 parse_spec (gfc_statement st)
1530 st_state ss;
1532 verify_st_order (&ss, ST_NONE);
1533 if (st == ST_NONE)
1534 st = next_statement ();
1536 loop:
1537 switch (st)
1539 case ST_NONE:
1540 unexpected_eof ();
1542 case ST_FORMAT:
1543 case ST_ENTRY:
1544 case ST_DATA: /* Not allowed in interfaces */
1545 if (gfc_current_state () == COMP_INTERFACE)
1546 break;
1548 /* Fall through */
1550 case ST_USE:
1551 case ST_IMPLICIT_NONE:
1552 case ST_IMPLICIT:
1553 case ST_PARAMETER:
1554 case ST_PUBLIC:
1555 case ST_PRIVATE:
1556 case ST_DERIVED_DECL:
1557 case_decl:
1558 if (verify_st_order (&ss, st) == FAILURE)
1560 reject_statement ();
1561 st = next_statement ();
1562 goto loop;
1565 switch (st)
1567 case ST_INTERFACE:
1568 parse_interface ();
1569 break;
1571 case ST_DERIVED_DECL:
1572 parse_derived ();
1573 break;
1575 case ST_PUBLIC:
1576 case ST_PRIVATE:
1577 if (gfc_current_state () != COMP_MODULE)
1579 gfc_error ("%s statement must appear in a MODULE",
1580 gfc_ascii_statement (st));
1581 break;
1584 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1586 gfc_error ("%s statement at %C follows another accessibility "
1587 "specification", gfc_ascii_statement (st));
1588 break;
1591 gfc_current_ns->default_access = (st == ST_PUBLIC)
1592 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1594 break;
1596 default:
1597 break;
1600 accept_statement (st);
1601 st = next_statement ();
1602 goto loop;
1604 default:
1605 break;
1608 return st;
1612 /* Parse a WHERE block, (not a simple WHERE statement). */
1614 static void
1615 parse_where_block (void)
1617 int seen_empty_else;
1618 gfc_code *top, *d;
1619 gfc_state_data s;
1620 gfc_statement st;
1622 accept_statement (ST_WHERE_BLOCK);
1623 top = gfc_state_stack->tail;
1625 push_state (&s, COMP_WHERE, gfc_new_block);
1627 d = add_statement ();
1628 d->expr = top->expr;
1629 d->op = EXEC_WHERE;
1631 top->expr = NULL;
1632 top->block = d;
1634 seen_empty_else = 0;
1638 st = next_statement ();
1639 switch (st)
1641 case ST_NONE:
1642 unexpected_eof ();
1644 case ST_WHERE_BLOCK:
1645 parse_where_block ();
1646 /* Fall through */
1648 case ST_ASSIGNMENT:
1649 case ST_WHERE:
1650 accept_statement (st);
1651 break;
1653 case ST_ELSEWHERE:
1654 if (seen_empty_else)
1656 gfc_error
1657 ("ELSEWHERE statement at %C follows previous unmasked "
1658 "ELSEWHERE");
1659 break;
1662 if (new_st.expr == NULL)
1663 seen_empty_else = 1;
1665 d = new_level (gfc_state_stack->head);
1666 d->op = EXEC_WHERE;
1667 d->expr = new_st.expr;
1669 accept_statement (st);
1671 break;
1673 case ST_END_WHERE:
1674 accept_statement (st);
1675 break;
1677 default:
1678 gfc_error ("Unexpected %s statement in WHERE block at %C",
1679 gfc_ascii_statement (st));
1680 reject_statement ();
1681 break;
1685 while (st != ST_END_WHERE);
1687 pop_state ();
1691 /* Parse a FORALL block (not a simple FORALL statement). */
1693 static void
1694 parse_forall_block (void)
1696 gfc_code *top, *d;
1697 gfc_state_data s;
1698 gfc_statement st;
1700 accept_statement (ST_FORALL_BLOCK);
1701 top = gfc_state_stack->tail;
1703 push_state (&s, COMP_FORALL, gfc_new_block);
1705 d = add_statement ();
1706 d->op = EXEC_FORALL;
1707 top->block = d;
1711 st = next_statement ();
1712 switch (st)
1715 case ST_ASSIGNMENT:
1716 case ST_POINTER_ASSIGNMENT:
1717 case ST_WHERE:
1718 case ST_FORALL:
1719 accept_statement (st);
1720 break;
1722 case ST_WHERE_BLOCK:
1723 parse_where_block ();
1724 break;
1726 case ST_FORALL_BLOCK:
1727 parse_forall_block ();
1728 break;
1730 case ST_END_FORALL:
1731 accept_statement (st);
1732 break;
1734 case ST_NONE:
1735 unexpected_eof ();
1737 default:
1738 gfc_error ("Unexpected %s statement in FORALL block at %C",
1739 gfc_ascii_statement (st));
1741 reject_statement ();
1742 break;
1745 while (st != ST_END_FORALL);
1747 pop_state ();
1751 static gfc_statement parse_executable (gfc_statement);
1753 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
1755 static void
1756 parse_if_block (void)
1758 gfc_code *top, *d;
1759 gfc_statement st;
1760 locus else_locus;
1761 gfc_state_data s;
1762 int seen_else;
1764 seen_else = 0;
1765 accept_statement (ST_IF_BLOCK);
1767 top = gfc_state_stack->tail;
1768 push_state (&s, COMP_IF, gfc_new_block);
1770 new_st.op = EXEC_IF;
1771 d = add_statement ();
1773 d->expr = top->expr;
1774 top->expr = NULL;
1775 top->block = d;
1779 st = parse_executable (ST_NONE);
1781 switch (st)
1783 case ST_NONE:
1784 unexpected_eof ();
1786 case ST_ELSEIF:
1787 if (seen_else)
1789 gfc_error
1790 ("ELSE IF statement at %C cannot follow ELSE statement at %L",
1791 &else_locus);
1793 reject_statement ();
1794 break;
1797 d = new_level (gfc_state_stack->head);
1798 d->op = EXEC_IF;
1799 d->expr = new_st.expr;
1801 accept_statement (st);
1803 break;
1805 case ST_ELSE:
1806 if (seen_else)
1808 gfc_error ("Duplicate ELSE statements at %L and %C",
1809 &else_locus);
1810 reject_statement ();
1811 break;
1814 seen_else = 1;
1815 else_locus = gfc_current_locus;
1817 d = new_level (gfc_state_stack->head);
1818 d->op = EXEC_IF;
1820 accept_statement (st);
1822 break;
1824 case ST_ENDIF:
1825 break;
1827 default:
1828 unexpected_statement (st);
1829 break;
1832 while (st != ST_ENDIF);
1834 pop_state ();
1835 accept_statement (st);
1839 /* Parse a SELECT block. */
1841 static void
1842 parse_select_block (void)
1844 gfc_statement st;
1845 gfc_code *cp;
1846 gfc_state_data s;
1848 accept_statement (ST_SELECT_CASE);
1850 cp = gfc_state_stack->tail;
1851 push_state (&s, COMP_SELECT, gfc_new_block);
1853 /* Make sure that the next statement is a CASE or END SELECT. */
1854 for (;;)
1856 st = next_statement ();
1857 if (st == ST_NONE)
1858 unexpected_eof ();
1859 if (st == ST_END_SELECT)
1861 /* Empty SELECT CASE is OK. */
1862 accept_statement (st);
1863 pop_state ();
1864 return;
1866 if (st == ST_CASE)
1867 break;
1869 gfc_error
1870 ("Expected a CASE or END SELECT statement following SELECT CASE "
1871 "at %C");
1873 reject_statement ();
1876 /* At this point, we're got a nonempty select block. */
1877 cp = new_level (cp);
1878 *cp = new_st;
1880 accept_statement (st);
1884 st = parse_executable (ST_NONE);
1885 switch (st)
1887 case ST_NONE:
1888 unexpected_eof ();
1890 case ST_CASE:
1891 cp = new_level (gfc_state_stack->head);
1892 *cp = new_st;
1893 gfc_clear_new_st ();
1895 accept_statement (st);
1896 /* Fall through */
1898 case ST_END_SELECT:
1899 break;
1901 /* Can't have an executable statement because of
1902 parse_executable(). */
1903 default:
1904 unexpected_statement (st);
1905 break;
1908 while (st != ST_END_SELECT);
1910 pop_state ();
1911 accept_statement (st);
1915 /* Checks to see if the current statement label closes an enddo.
1916 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
1917 an error) if it incorrectly closes an ENDDO. */
1919 static int
1920 check_do_closure (void)
1922 gfc_state_data *p;
1924 if (gfc_statement_label == NULL)
1925 return 0;
1927 for (p = gfc_state_stack; p; p = p->previous)
1928 if (p->state == COMP_DO)
1929 break;
1931 if (p == NULL)
1932 return 0; /* No loops to close */
1934 if (p->ext.end_do_label == gfc_statement_label)
1937 if (p == gfc_state_stack)
1938 return 1;
1940 gfc_error
1941 ("End of nonblock DO statement at %C is within another block");
1942 return 2;
1945 /* At this point, the label doesn't terminate the innermost loop.
1946 Make sure it doesn't terminate another one. */
1947 for (; p; p = p->previous)
1948 if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
1950 gfc_error ("End of nonblock DO statement at %C is interwoven "
1951 "with another DO loop");
1952 return 2;
1955 return 0;
1959 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
1960 handled inside of parse_executable(), because they aren't really
1961 loop statements. */
1963 static void
1964 parse_do_block (void)
1966 gfc_statement st;
1967 gfc_code *top;
1968 gfc_state_data s;
1970 s.ext.end_do_label = new_st.label;
1972 accept_statement (ST_DO);
1974 top = gfc_state_stack->tail;
1975 push_state (&s, COMP_DO, gfc_new_block);
1977 top->block = new_level (top);
1978 top->block->op = EXEC_DO;
1980 loop:
1981 st = parse_executable (ST_NONE);
1983 switch (st)
1985 case ST_NONE:
1986 unexpected_eof ();
1988 case ST_ENDDO:
1989 if (s.ext.end_do_label != NULL
1990 && s.ext.end_do_label != gfc_statement_label)
1991 gfc_error_now
1992 ("Statement label in ENDDO at %C doesn't match DO label");
1993 /* Fall through */
1995 case ST_IMPLIED_ENDDO:
1996 break;
1998 default:
1999 unexpected_statement (st);
2000 goto loop;
2003 pop_state ();
2004 accept_statement (st);
2008 /* Accept a series of executable statements. We return the first
2009 statement that doesn't fit to the caller. Any block statements are
2010 passed on to the correct handler, which usually passes the buck
2011 right back here. */
2013 static gfc_statement
2014 parse_executable (gfc_statement st)
2016 int close_flag;
2018 if (st == ST_NONE)
2019 st = next_statement ();
2021 for (;; st = next_statement ())
2024 close_flag = check_do_closure ();
2025 if (close_flag)
2026 switch (st)
2028 case ST_GOTO:
2029 case ST_END_PROGRAM:
2030 case ST_RETURN:
2031 case ST_EXIT:
2032 case ST_END_FUNCTION:
2033 case ST_CYCLE:
2034 case ST_PAUSE:
2035 case ST_STOP:
2036 case ST_END_SUBROUTINE:
2038 case ST_DO:
2039 case ST_FORALL:
2040 case ST_WHERE:
2041 case ST_SELECT_CASE:
2042 gfc_error
2043 ("%s statement at %C cannot terminate a non-block DO loop",
2044 gfc_ascii_statement (st));
2045 break;
2047 default:
2048 break;
2051 switch (st)
2053 case ST_NONE:
2054 unexpected_eof ();
2056 case ST_FORMAT:
2057 case ST_DATA:
2058 case ST_ENTRY:
2059 case_executable:
2060 accept_statement (st);
2061 if (close_flag == 1)
2062 return ST_IMPLIED_ENDDO;
2063 continue;
2065 case ST_IF_BLOCK:
2066 parse_if_block ();
2067 continue;
2069 case ST_SELECT_CASE:
2070 parse_select_block ();
2071 continue;
2073 case ST_DO:
2074 parse_do_block ();
2075 if (check_do_closure () == 1)
2076 return ST_IMPLIED_ENDDO;
2077 continue;
2079 case ST_WHERE_BLOCK:
2080 parse_where_block ();
2081 continue;
2083 case ST_FORALL_BLOCK:
2084 parse_forall_block ();
2085 continue;
2087 default:
2088 break;
2091 break;
2094 return st;
2098 /* Parse a series of contained program units. */
2100 static void parse_progunit (gfc_statement);
2103 /* Fix the symbols for sibling functions. These are incorrectly added to
2104 the child namespace as the parser didn't know about this procedure. */
2106 static void
2107 gfc_fixup_sibling_symbols (gfc_symbol * sym, gfc_namespace * siblings)
2109 gfc_namespace *ns;
2110 gfc_symtree *st;
2111 gfc_symbol *old_sym;
2113 for (ns = siblings; ns; ns = ns->sibling)
2115 gfc_find_sym_tree (sym->name, ns, 0, &st);
2116 if (!st)
2117 continue;
2119 old_sym = st->n.sym;
2120 if (old_sym->attr.flavor == FL_PROCEDURE && old_sym->ns == ns
2121 && ! old_sym->attr.contained)
2123 /* Replace it with the symbol from the parent namespace. */
2124 st->n.sym = sym;
2125 sym->refs++;
2127 /* Free the old (local) symbol. */
2128 old_sym->refs--;
2129 if (old_sym->refs == 0)
2130 gfc_free_symbol (old_sym);
2133 /* Do the same for any contined procedures. */
2134 gfc_fixup_sibling_symbols (sym, ns->contained);
2138 static void
2139 parse_contained (int module)
2141 gfc_namespace *ns, *parent_ns;
2142 gfc_state_data s1, s2;
2143 gfc_statement st;
2144 gfc_symbol *sym;
2146 push_state (&s1, COMP_CONTAINS, NULL);
2147 parent_ns = gfc_current_ns;
2151 gfc_current_ns = gfc_get_namespace (parent_ns);
2153 gfc_current_ns->sibling = parent_ns->contained;
2154 parent_ns->contained = gfc_current_ns;
2156 st = next_statement ();
2158 switch (st)
2160 case ST_NONE:
2161 unexpected_eof ();
2163 case ST_FUNCTION:
2164 case ST_SUBROUTINE:
2165 accept_statement (st);
2167 push_state (&s2,
2168 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2169 gfc_new_block);
2171 /* For internal procedures, create/update the symbol in the
2172 * parent namespace */
2174 if (!module)
2176 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2177 gfc_error
2178 ("Contained procedure '%s' at %C is already ambiguous",
2179 gfc_new_block->name);
2180 else
2182 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
2183 &gfc_new_block->declared_at) ==
2184 SUCCESS)
2186 if (st == ST_FUNCTION)
2187 gfc_add_function (&sym->attr,
2188 &gfc_new_block->declared_at);
2189 else
2190 gfc_add_subroutine (&sym->attr,
2191 &gfc_new_block->declared_at);
2195 gfc_commit_symbols ();
2197 else
2198 sym = gfc_new_block;
2200 /* Mark this as a contained function, so it isn't replaced
2201 by other module functions. */
2202 sym->attr.contained = 1;
2204 /* Fix up any sibling functions that refer to this one. */
2205 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2207 parse_progunit (ST_NONE);
2209 gfc_current_ns->code = s2.head;
2210 gfc_current_ns = parent_ns;
2212 pop_state ();
2213 break;
2215 /* These statements are associated with the end of the host
2216 unit. */
2217 case ST_END_FUNCTION:
2218 case ST_END_MODULE:
2219 case ST_END_PROGRAM:
2220 case ST_END_SUBROUTINE:
2221 accept_statement (st);
2222 break;
2224 default:
2225 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2226 gfc_ascii_statement (st));
2227 reject_statement ();
2228 break;
2231 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2232 && st != ST_END_MODULE && st != ST_END_PROGRAM);
2234 /* The first namespace in the list is guaranteed to not have
2235 anything (worthwhile) in it. */
2237 gfc_current_ns = parent_ns;
2239 ns = gfc_current_ns->contained;
2240 gfc_current_ns->contained = ns->sibling;
2241 gfc_free_namespace (ns);
2243 pop_state ();
2247 /* Parse a PROGRAM, SUBROUTINE or FUNCTION unit. */
2249 static void
2250 parse_progunit (gfc_statement st)
2252 gfc_state_data *p;
2253 int n;
2255 st = parse_spec (st);
2256 switch (st)
2258 case ST_NONE:
2259 unexpected_eof ();
2261 case ST_CONTAINS:
2262 goto contains;
2264 case_end:
2265 accept_statement (st);
2266 goto done;
2268 default:
2269 break;
2272 loop:
2273 for (;;)
2275 st = parse_executable (st);
2277 switch (st)
2279 case ST_NONE:
2280 unexpected_eof ();
2282 case ST_CONTAINS:
2283 goto contains;
2285 case_end:
2286 accept_statement (st);
2287 goto done;
2289 default:
2290 break;
2293 unexpected_statement (st);
2294 reject_statement ();
2295 st = next_statement ();
2298 contains:
2299 n = 0;
2301 for (p = gfc_state_stack; p; p = p->previous)
2302 if (p->state == COMP_CONTAINS)
2303 n++;
2305 if (gfc_find_state (COMP_MODULE) == SUCCESS)
2306 n--;
2308 if (n > 0)
2310 gfc_error ("CONTAINS statement at %C is already in a contained "
2311 "program unit");
2312 st = next_statement ();
2313 goto loop;
2316 parse_contained (0);
2318 done:
2319 gfc_current_ns->code = gfc_state_stack->head;
2323 /* Come here to complain about a global symbol already in use as
2324 something else. */
2326 static void
2327 global_used (gfc_gsymbol *sym, locus *where)
2329 const char *name;
2331 if (where == NULL)
2332 where = &gfc_current_locus;
2334 switch(sym->type)
2336 case GSYM_PROGRAM:
2337 name = "PROGRAM";
2338 break;
2339 case GSYM_FUNCTION:
2340 name = "FUNCTION";
2341 break;
2342 case GSYM_SUBROUTINE:
2343 name = "SUBROUTINE";
2344 break;
2345 case GSYM_COMMON:
2346 name = "COMMON";
2347 break;
2348 case GSYM_BLOCK_DATA:
2349 name = "BLOCK DATA";
2350 break;
2351 case GSYM_MODULE:
2352 name = "MODULE";
2353 break;
2354 default:
2355 gfc_internal_error ("gfc_gsymbol_type(): Bad type");
2356 name = NULL;
2359 gfc_error("Global name '%s' at %L is already being used as a %s at %L",
2360 gfc_new_block->name, where, name, &sym->where);
2364 /* Parse a block data program unit. */
2366 static void
2367 parse_block_data (void)
2369 gfc_statement st;
2370 static locus blank_locus;
2371 static int blank_block=0;
2372 gfc_gsymbol *s;
2374 if (gfc_new_block == NULL)
2376 if (blank_block)
2377 gfc_error ("Blank BLOCK DATA at %C conflicts with "
2378 "prior BLOCK DATA at %L", &blank_locus);
2379 else
2381 blank_block = 1;
2382 blank_locus = gfc_current_locus;
2385 else
2387 s = gfc_get_gsymbol (gfc_new_block->name);
2388 if (s->type != GSYM_UNKNOWN)
2389 global_used(s, NULL);
2390 else
2392 s->type = GSYM_BLOCK_DATA;
2393 s->where = gfc_current_locus;
2397 st = parse_spec (ST_NONE);
2399 while (st != ST_END_BLOCK_DATA)
2401 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
2402 gfc_ascii_statement (st));
2403 reject_statement ();
2404 st = next_statement ();
2409 /* Parse a module subprogram. */
2411 static void
2412 parse_module (void)
2414 gfc_statement st;
2415 gfc_gsymbol *s;
2417 s = gfc_get_gsymbol (gfc_new_block->name);
2418 if (s->type != GSYM_UNKNOWN)
2419 global_used(s, NULL);
2420 else
2422 s->type = GSYM_MODULE;
2423 s->where = gfc_current_locus;
2426 st = parse_spec (ST_NONE);
2428 loop:
2429 switch (st)
2431 case ST_NONE:
2432 unexpected_eof ();
2434 case ST_CONTAINS:
2435 parse_contained (1);
2436 break;
2438 case ST_END_MODULE:
2439 accept_statement (st);
2440 break;
2442 default:
2443 gfc_error ("Unexpected %s statement in MODULE at %C",
2444 gfc_ascii_statement (st));
2446 reject_statement ();
2447 st = next_statement ();
2448 goto loop;
2453 /* Add a procedure name to the global symbol table. */
2455 static void
2456 add_global_procedure (int sub)
2458 gfc_gsymbol *s;
2460 s = gfc_get_gsymbol(gfc_new_block->name);
2462 if (s->type != GSYM_UNKNOWN)
2463 global_used(s, NULL);
2464 else
2466 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2467 s->where = gfc_current_locus;
2472 /* Add a program to the global symbol table. */
2474 static void
2475 add_global_program (void)
2477 gfc_gsymbol *s;
2479 if (gfc_new_block == NULL)
2480 return;
2481 s = gfc_get_gsymbol (gfc_new_block->name);
2483 if (s->type != GSYM_UNKNOWN)
2484 global_used(s, NULL);
2485 else
2487 s->type = GSYM_PROGRAM;
2488 s->where = gfc_current_locus;
2493 /* Top level parser. */
2496 gfc_parse_file (void)
2498 int seen_program, errors_before, errors;
2499 gfc_state_data top, s;
2500 gfc_statement st;
2501 locus prog_locus;
2503 top.state = COMP_NONE;
2504 top.sym = NULL;
2505 top.previous = NULL;
2506 top.head = top.tail = NULL;
2508 gfc_state_stack = &top;
2510 gfc_clear_new_st ();
2512 gfc_statement_label = NULL;
2514 if (setjmp (eof))
2515 return FAILURE; /* Come here on unexpected EOF */
2517 seen_program = 0;
2519 loop:
2520 gfc_init_2 ();
2521 st = next_statement ();
2522 switch (st)
2524 case ST_NONE:
2525 gfc_done_2 ();
2526 goto done;
2528 case ST_PROGRAM:
2529 if (seen_program)
2530 goto duplicate_main;
2531 seen_program = 1;
2532 prog_locus = gfc_current_locus;
2534 push_state (&s, COMP_PROGRAM, gfc_new_block);
2535 accept_statement (st);
2536 add_global_program ();
2537 parse_progunit (ST_NONE);
2538 break;
2540 case ST_SUBROUTINE:
2541 add_global_procedure (1);
2542 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
2543 accept_statement (st);
2544 parse_progunit (ST_NONE);
2545 break;
2547 case ST_FUNCTION:
2548 add_global_procedure (0);
2549 push_state (&s, COMP_FUNCTION, gfc_new_block);
2550 accept_statement (st);
2551 parse_progunit (ST_NONE);
2552 break;
2554 case ST_BLOCK_DATA:
2555 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
2556 accept_statement (st);
2557 parse_block_data ();
2558 break;
2560 case ST_MODULE:
2561 push_state (&s, COMP_MODULE, gfc_new_block);
2562 accept_statement (st);
2564 gfc_get_errors (NULL, &errors_before);
2565 parse_module ();
2566 break;
2568 /* Anything else starts a nameless main program block. */
2569 default:
2570 if (seen_program)
2571 goto duplicate_main;
2572 seen_program = 1;
2573 prog_locus = gfc_current_locus;
2575 push_state (&s, COMP_PROGRAM, gfc_new_block);
2576 parse_progunit (st);
2577 break;
2580 gfc_current_ns->code = s.head;
2582 gfc_resolve (gfc_current_ns);
2584 /* Dump the parse tree if requested. */
2585 if (gfc_option.verbose)
2586 gfc_show_namespace (gfc_current_ns);
2588 gfc_get_errors (NULL, &errors);
2589 if (s.state == COMP_MODULE)
2591 gfc_dump_module (s.sym->name, errors_before == errors);
2592 if (errors == 0 && ! gfc_option.flag_no_backend)
2593 gfc_generate_module_code (gfc_current_ns);
2595 else
2597 if (errors == 0 && ! gfc_option.flag_no_backend)
2598 gfc_generate_code (gfc_current_ns);
2601 pop_state ();
2602 gfc_done_2 ();
2603 goto loop;
2605 done:
2606 return SUCCESS;
2608 duplicate_main:
2609 /* If we see a duplicate main program, shut down. If the second
2610 instance is an implied main program, ie data decls or executable
2611 statements, we're in for lots of errors. */
2612 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
2613 reject_statement ();
2614 gfc_done_2 ();
2615 return SUCCESS;