Fix missing ChangeLog entry for Graphite head files fix.
[official-gcc.git] / gcc / fortran / parse.c
blobb2806214e1a638ffc1091aebe466abde9fce508e
1 /* Main parser.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "gfortran.h"
26 #include <setjmp.h>
27 #include "match.h"
28 #include "parse.h"
30 /* Current statement label. Zero means no statement label. Because new_st
31 can get wiped during statement matching, we have to keep it separate. */
33 gfc_st_label *gfc_statement_label;
35 static locus label_locus;
36 static jmp_buf eof_buf;
38 gfc_state_data *gfc_state_stack;
39 static bool last_was_use_stmt = false;
41 /* TODO: Re-order functions to kill these forward decls. */
42 static void check_statement_label (gfc_statement);
43 static void undo_new_statement (void);
44 static void reject_statement (void);
47 /* A sort of half-matching function. We try to match the word on the
48 input with the passed string. If this succeeds, we call the
49 keyword-dependent matching function that will match the rest of the
50 statement. For single keywords, the matching subroutine is
51 gfc_match_eos(). */
53 static match
54 match_word (const char *str, match (*subr) (void), locus *old_locus)
56 match m;
58 if (str != NULL)
60 m = gfc_match (str);
61 if (m != MATCH_YES)
62 return m;
65 m = (*subr) ();
67 if (m != MATCH_YES)
69 gfc_current_locus = *old_locus;
70 reject_statement ();
73 return m;
77 /* Like match_word, but if str is matched, set a flag that it
78 was matched. */
79 static match
80 match_word_omp_simd (const char *str, match (*subr) (void), locus *old_locus,
81 bool *simd_matched)
83 match m;
85 if (str != NULL)
87 m = gfc_match (str);
88 if (m != MATCH_YES)
89 return m;
90 *simd_matched = true;
93 m = (*subr) ();
95 if (m != MATCH_YES)
97 gfc_current_locus = *old_locus;
98 reject_statement ();
101 return m;
105 /* Load symbols from all USE statements encountered in this scoping unit. */
107 static void
108 use_modules (void)
110 gfc_error_buffer old_error;
112 gfc_push_error (&old_error);
113 gfc_buffer_error (false);
114 gfc_use_modules ();
115 gfc_buffer_error (true);
116 gfc_pop_error (&old_error);
117 gfc_commit_symbols ();
118 gfc_warning_check ();
119 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
120 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
121 gfc_current_ns->old_data = gfc_current_ns->data;
122 last_was_use_stmt = false;
126 /* Figure out what the next statement is, (mostly) regardless of
127 proper ordering. The do...while(0) is there to prevent if/else
128 ambiguity. */
130 #define match(keyword, subr, st) \
131 do { \
132 if (match_word (keyword, subr, &old_locus) == MATCH_YES) \
133 return st; \
134 else \
135 undo_new_statement (); \
136 } while (0);
139 /* This is a specialist version of decode_statement that is used
140 for the specification statements in a function, whose
141 characteristics are deferred into the specification statements.
142 eg.: INTEGER (king = mykind) foo ()
143 USE mymodule, ONLY mykind.....
144 The KIND parameter needs a return after USE or IMPORT, whereas
145 derived type declarations can occur anywhere, up the executable
146 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
147 out of the correct kind of specification statements. */
148 static gfc_statement
149 decode_specification_statement (void)
151 gfc_statement st;
152 locus old_locus;
153 char c;
155 if (gfc_match_eos () == MATCH_YES)
156 return ST_NONE;
158 old_locus = gfc_current_locus;
160 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
162 last_was_use_stmt = true;
163 return ST_USE;
165 else
167 undo_new_statement ();
168 if (last_was_use_stmt)
169 use_modules ();
172 match ("import", gfc_match_import, ST_IMPORT);
174 if (gfc_current_block ()->result->ts.type != BT_DERIVED)
175 goto end_of_block;
177 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
178 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
179 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
181 /* General statement matching: Instead of testing every possible
182 statement, we eliminate most possibilities by peeking at the
183 first character. */
185 c = gfc_peek_ascii_char ();
187 switch (c)
189 case 'a':
190 match ("abstract% interface", gfc_match_abstract_interface,
191 ST_INTERFACE);
192 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
193 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
194 break;
196 case 'b':
197 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
198 break;
200 case 'c':
201 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
202 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
203 break;
205 case 'd':
206 match ("data", gfc_match_data, ST_DATA);
207 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
208 break;
210 case 'e':
211 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
212 match ("entry% ", gfc_match_entry, ST_ENTRY);
213 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
214 match ("external", gfc_match_external, ST_ATTR_DECL);
215 break;
217 case 'f':
218 match ("format", gfc_match_format, ST_FORMAT);
219 break;
221 case 'g':
222 break;
224 case 'i':
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 break;
235 case 'n':
236 match ("namelist", gfc_match_namelist, ST_NAMELIST);
237 break;
239 case 'o':
240 match ("optional", gfc_match_optional, ST_ATTR_DECL);
241 break;
243 case 'p':
244 match ("parameter", gfc_match_parameter, ST_PARAMETER);
245 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
246 if (gfc_match_private (&st) == MATCH_YES)
247 return st;
248 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
249 if (gfc_match_public (&st) == MATCH_YES)
250 return st;
251 match ("protected", gfc_match_protected, ST_ATTR_DECL);
252 break;
254 case 'r':
255 break;
257 case 's':
258 match ("save", gfc_match_save, ST_ATTR_DECL);
259 break;
261 case 't':
262 match ("target", gfc_match_target, ST_ATTR_DECL);
263 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
264 break;
266 case 'u':
267 break;
269 case 'v':
270 match ("value", gfc_match_value, ST_ATTR_DECL);
271 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
272 break;
274 case 'w':
275 break;
278 /* This is not a specification statement. See if any of the matchers
279 has stored an error message of some sort. */
281 end_of_block:
282 gfc_clear_error ();
283 gfc_buffer_error (false);
284 gfc_current_locus = old_locus;
286 return ST_GET_FCN_CHARACTERISTICS;
289 static bool in_specification_block;
291 /* This is the primary 'decode_statement'. */
292 static gfc_statement
293 decode_statement (void)
295 gfc_namespace *ns;
296 gfc_statement st;
297 locus old_locus;
298 match m = MATCH_NO;
299 char c;
301 gfc_enforce_clean_symbol_state ();
303 gfc_clear_error (); /* Clear any pending errors. */
304 gfc_clear_warning (); /* Clear any pending warnings. */
306 gfc_matching_function = false;
308 if (gfc_match_eos () == MATCH_YES)
309 return ST_NONE;
311 if (gfc_current_state () == COMP_FUNCTION
312 && gfc_current_block ()->result->ts.kind == -1)
313 return decode_specification_statement ();
315 old_locus = gfc_current_locus;
317 c = gfc_peek_ascii_char ();
319 if (c == 'u')
321 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
323 last_was_use_stmt = true;
324 return ST_USE;
326 else
327 undo_new_statement ();
330 if (last_was_use_stmt)
331 use_modules ();
333 /* Try matching a data declaration or function declaration. The
334 input "REALFUNCTIONA(N)" can mean several things in different
335 contexts, so it (and its relatives) get special treatment. */
337 if (gfc_current_state () == COMP_NONE
338 || gfc_current_state () == COMP_INTERFACE
339 || gfc_current_state () == COMP_CONTAINS)
341 gfc_matching_function = true;
342 m = gfc_match_function_decl ();
343 if (m == MATCH_YES)
344 return ST_FUNCTION;
345 else if (m == MATCH_ERROR)
346 reject_statement ();
347 else
348 gfc_undo_symbols ();
349 gfc_current_locus = old_locus;
351 gfc_matching_function = false;
354 /* Match statements whose error messages are meant to be overwritten
355 by something better. */
357 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
358 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
360 if (in_specification_block)
362 m = match_word (NULL, gfc_match_st_function, &old_locus);
363 if (m == MATCH_YES)
364 return ST_STATEMENT_FUNCTION;
367 if (!(in_specification_block && m == MATCH_ERROR))
369 match (NULL, gfc_match_ptr_fcn_assign, ST_ASSIGNMENT);
372 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
373 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
375 /* Try to match a subroutine statement, which has the same optional
376 prefixes that functions can have. */
378 if (gfc_match_subroutine () == MATCH_YES)
379 return ST_SUBROUTINE;
380 gfc_undo_symbols ();
381 gfc_current_locus = old_locus;
383 if (gfc_match_submod_proc () == MATCH_YES)
385 if (gfc_new_block->attr.subroutine)
386 return ST_SUBROUTINE;
387 else if (gfc_new_block->attr.function)
388 return ST_FUNCTION;
390 gfc_undo_symbols ();
391 gfc_current_locus = old_locus;
393 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
394 statements, which might begin with a block label. The match functions for
395 these statements are unusual in that their keyword is not seen before
396 the matcher is called. */
398 if (gfc_match_if (&st) == MATCH_YES)
399 return st;
400 gfc_undo_symbols ();
401 gfc_current_locus = old_locus;
403 if (gfc_match_where (&st) == MATCH_YES)
404 return st;
405 gfc_undo_symbols ();
406 gfc_current_locus = old_locus;
408 if (gfc_match_forall (&st) == MATCH_YES)
409 return st;
410 gfc_undo_symbols ();
411 gfc_current_locus = old_locus;
413 match (NULL, gfc_match_do, ST_DO);
414 match (NULL, gfc_match_block, ST_BLOCK);
415 match (NULL, gfc_match_associate, ST_ASSOCIATE);
416 match (NULL, gfc_match_critical, ST_CRITICAL);
417 match (NULL, gfc_match_select, ST_SELECT_CASE);
419 gfc_current_ns = gfc_build_block_ns (gfc_current_ns);
420 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
421 ns = gfc_current_ns;
422 gfc_current_ns = gfc_current_ns->parent;
423 gfc_free_namespace (ns);
425 /* General statement matching: Instead of testing every possible
426 statement, we eliminate most possibilities by peeking at the
427 first character. */
429 switch (c)
431 case 'a':
432 match ("abstract% interface", gfc_match_abstract_interface,
433 ST_INTERFACE);
434 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
435 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
436 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
437 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
438 break;
440 case 'b':
441 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
442 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
443 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
444 break;
446 case 'c':
447 match ("call", gfc_match_call, ST_CALL);
448 match ("close", gfc_match_close, ST_CLOSE);
449 match ("continue", gfc_match_continue, ST_CONTINUE);
450 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
451 match ("cycle", gfc_match_cycle, ST_CYCLE);
452 match ("case", gfc_match_case, ST_CASE);
453 match ("common", gfc_match_common, ST_COMMON);
454 match ("contains", gfc_match_eos, ST_CONTAINS);
455 match ("class", gfc_match_class_is, ST_CLASS_IS);
456 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
457 break;
459 case 'd':
460 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
461 match ("data", gfc_match_data, ST_DATA);
462 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
463 break;
465 case 'e':
466 match ("end file", gfc_match_endfile, ST_END_FILE);
467 match ("exit", gfc_match_exit, ST_EXIT);
468 match ("else", gfc_match_else, ST_ELSE);
469 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
470 match ("else if", gfc_match_elseif, ST_ELSEIF);
471 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
472 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
474 if (gfc_match_end (&st) == MATCH_YES)
475 return st;
477 match ("entry% ", gfc_match_entry, ST_ENTRY);
478 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
479 match ("external", gfc_match_external, ST_ATTR_DECL);
480 break;
482 case 'f':
483 match ("final", gfc_match_final_decl, ST_FINAL);
484 match ("flush", gfc_match_flush, ST_FLUSH);
485 match ("format", gfc_match_format, ST_FORMAT);
486 break;
488 case 'g':
489 match ("generic", gfc_match_generic, ST_GENERIC);
490 match ("go to", gfc_match_goto, ST_GOTO);
491 break;
493 case 'i':
494 match ("inquire", gfc_match_inquire, ST_INQUIRE);
495 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
496 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
497 match ("import", gfc_match_import, ST_IMPORT);
498 match ("interface", gfc_match_interface, ST_INTERFACE);
499 match ("intent", gfc_match_intent, ST_ATTR_DECL);
500 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
501 break;
503 case 'l':
504 match ("lock", gfc_match_lock, ST_LOCK);
505 break;
507 case 'm':
508 match ("module% procedure", gfc_match_modproc, ST_MODULE_PROC);
509 match ("module", gfc_match_module, ST_MODULE);
510 break;
512 case 'n':
513 match ("nullify", gfc_match_nullify, ST_NULLIFY);
514 match ("namelist", gfc_match_namelist, ST_NAMELIST);
515 break;
517 case 'o':
518 match ("open", gfc_match_open, ST_OPEN);
519 match ("optional", gfc_match_optional, ST_ATTR_DECL);
520 break;
522 case 'p':
523 match ("print", gfc_match_print, ST_WRITE);
524 match ("parameter", gfc_match_parameter, ST_PARAMETER);
525 match ("pause", gfc_match_pause, ST_PAUSE);
526 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
527 if (gfc_match_private (&st) == MATCH_YES)
528 return st;
529 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
530 match ("program", gfc_match_program, ST_PROGRAM);
531 if (gfc_match_public (&st) == MATCH_YES)
532 return st;
533 match ("protected", gfc_match_protected, ST_ATTR_DECL);
534 break;
536 case 'r':
537 match ("read", gfc_match_read, ST_READ);
538 match ("return", gfc_match_return, ST_RETURN);
539 match ("rewind", gfc_match_rewind, ST_REWIND);
540 break;
542 case 's':
543 match ("sequence", gfc_match_eos, ST_SEQUENCE);
544 match ("stop", gfc_match_stop, ST_STOP);
545 match ("save", gfc_match_save, ST_ATTR_DECL);
546 match ("submodule", gfc_match_submodule, ST_SUBMODULE);
547 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
548 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
549 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
550 break;
552 case 't':
553 match ("target", gfc_match_target, ST_ATTR_DECL);
554 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
555 match ("type is", gfc_match_type_is, ST_TYPE_IS);
556 break;
558 case 'u':
559 match ("unlock", gfc_match_unlock, ST_UNLOCK);
560 break;
562 case 'v':
563 match ("value", gfc_match_value, ST_ATTR_DECL);
564 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
565 break;
567 case 'w':
568 match ("wait", gfc_match_wait, ST_WAIT);
569 match ("write", gfc_match_write, ST_WRITE);
570 break;
573 /* All else has failed, so give up. See if any of the matchers has
574 stored an error message of some sort. */
576 if (!gfc_error_check ())
577 gfc_error_now ("Unclassifiable statement at %C");
579 reject_statement ();
581 gfc_error_recovery ();
583 return ST_NONE;
586 /* Like match, but set a flag simd_matched if keyword matched. */
587 #define matchs(keyword, subr, st) \
588 do { \
589 if (match_word_omp_simd (keyword, subr, &old_locus, \
590 &simd_matched) == MATCH_YES) \
591 return st; \
592 else \
593 undo_new_statement (); \
594 } while (0);
596 /* Like match, but don't match anything if not -fopenmp. */
597 #define matcho(keyword, subr, st) \
598 do { \
599 if (!flag_openmp) \
601 else if (match_word (keyword, subr, &old_locus) \
602 == MATCH_YES) \
603 return st; \
604 else \
605 undo_new_statement (); \
606 } while (0);
608 static gfc_statement
609 decode_oacc_directive (void)
611 locus old_locus;
612 char c;
614 gfc_enforce_clean_symbol_state ();
616 gfc_clear_error (); /* Clear any pending errors. */
617 gfc_clear_warning (); /* Clear any pending warnings. */
619 if (gfc_pure (NULL))
621 gfc_error_now ("OpenACC directives at %C may not appear in PURE "
622 "procedures");
623 gfc_error_recovery ();
624 return ST_NONE;
627 gfc_unset_implicit_pure (NULL);
629 old_locus = gfc_current_locus;
631 /* General OpenACC directive matching: Instead of testing every possible
632 statement, we eliminate most possibilities by peeking at the
633 first character. */
635 c = gfc_peek_ascii_char ();
637 switch (c)
639 case 'a':
640 match ("atomic", gfc_match_oacc_atomic, ST_OACC_ATOMIC);
641 break;
642 case 'c':
643 match ("cache", gfc_match_oacc_cache, ST_OACC_CACHE);
644 break;
645 case 'd':
646 match ("data", gfc_match_oacc_data, ST_OACC_DATA);
647 match ("declare", gfc_match_oacc_declare, ST_OACC_DECLARE);
648 break;
649 case 'e':
650 match ("end atomic", gfc_match_omp_eos, ST_OACC_END_ATOMIC);
651 match ("end data", gfc_match_omp_eos, ST_OACC_END_DATA);
652 match ("end host_data", gfc_match_omp_eos, ST_OACC_END_HOST_DATA);
653 match ("end kernels loop", gfc_match_omp_eos, ST_OACC_END_KERNELS_LOOP);
654 match ("end kernels", gfc_match_omp_eos, ST_OACC_END_KERNELS);
655 match ("end loop", gfc_match_omp_eos, ST_OACC_END_LOOP);
656 match ("end parallel loop", gfc_match_omp_eos, ST_OACC_END_PARALLEL_LOOP);
657 match ("end parallel", gfc_match_omp_eos, ST_OACC_END_PARALLEL);
658 match ("enter data", gfc_match_oacc_enter_data, ST_OACC_ENTER_DATA);
659 match ("exit data", gfc_match_oacc_exit_data, ST_OACC_EXIT_DATA);
660 break;
661 case 'h':
662 match ("host_data", gfc_match_oacc_host_data, ST_OACC_HOST_DATA);
663 break;
664 case 'p':
665 match ("parallel loop", gfc_match_oacc_parallel_loop, ST_OACC_PARALLEL_LOOP);
666 match ("parallel", gfc_match_oacc_parallel, ST_OACC_PARALLEL);
667 break;
668 case 'k':
669 match ("kernels loop", gfc_match_oacc_kernels_loop, ST_OACC_KERNELS_LOOP);
670 match ("kernels", gfc_match_oacc_kernels, ST_OACC_KERNELS);
671 break;
672 case 'l':
673 match ("loop", gfc_match_oacc_loop, ST_OACC_LOOP);
674 break;
675 case 'r':
676 match ("routine", gfc_match_oacc_routine, ST_OACC_ROUTINE);
677 break;
678 case 'u':
679 match ("update", gfc_match_oacc_update, ST_OACC_UPDATE);
680 break;
681 case 'w':
682 match ("wait", gfc_match_oacc_wait, ST_OACC_WAIT);
683 break;
686 /* Directive not found or stored an error message.
687 Check and give up. */
689 if (gfc_error_check () == 0)
690 gfc_error_now ("Unclassifiable OpenACC directive at %C");
692 reject_statement ();
694 gfc_error_recovery ();
696 return ST_NONE;
699 static gfc_statement
700 decode_omp_directive (void)
702 locus old_locus;
703 char c;
704 bool simd_matched = false;
706 gfc_enforce_clean_symbol_state ();
708 gfc_clear_error (); /* Clear any pending errors. */
709 gfc_clear_warning (); /* Clear any pending warnings. */
711 if (gfc_pure (NULL))
713 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
714 "or ELEMENTAL procedures");
715 gfc_error_recovery ();
716 return ST_NONE;
719 gfc_unset_implicit_pure (NULL);
721 old_locus = gfc_current_locus;
723 /* General OpenMP directive matching: Instead of testing every possible
724 statement, we eliminate most possibilities by peeking at the
725 first character. */
727 c = gfc_peek_ascii_char ();
729 /* match is for directives that should be recognized only if
730 -fopenmp, matchs for directives that should be recognized
731 if either -fopenmp or -fopenmp-simd. */
732 switch (c)
734 case 'a':
735 matcho ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
736 break;
737 case 'b':
738 matcho ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
739 break;
740 case 'c':
741 matcho ("cancellation% point", gfc_match_omp_cancellation_point,
742 ST_OMP_CANCELLATION_POINT);
743 matcho ("cancel", gfc_match_omp_cancel, ST_OMP_CANCEL);
744 matcho ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
745 break;
746 case 'd':
747 matchs ("declare reduction", gfc_match_omp_declare_reduction,
748 ST_OMP_DECLARE_REDUCTION);
749 matchs ("declare simd", gfc_match_omp_declare_simd,
750 ST_OMP_DECLARE_SIMD);
751 matcho ("declare target", gfc_match_omp_declare_target,
752 ST_OMP_DECLARE_TARGET);
753 matchs ("distribute parallel do simd",
754 gfc_match_omp_distribute_parallel_do_simd,
755 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD);
756 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do,
757 ST_OMP_DISTRIBUTE_PARALLEL_DO);
758 matchs ("distribute simd", gfc_match_omp_distribute_simd,
759 ST_OMP_DISTRIBUTE_SIMD);
760 matcho ("distribute", gfc_match_omp_distribute, ST_OMP_DISTRIBUTE);
761 matchs ("do simd", gfc_match_omp_do_simd, ST_OMP_DO_SIMD);
762 matcho ("do", gfc_match_omp_do, ST_OMP_DO);
763 break;
764 case 'e':
765 matcho ("end atomic", gfc_match_omp_eos, ST_OMP_END_ATOMIC);
766 matcho ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
767 matchs ("end distribute parallel do simd", gfc_match_omp_eos,
768 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD);
769 matcho ("end distribute parallel do", gfc_match_omp_eos,
770 ST_OMP_END_DISTRIBUTE_PARALLEL_DO);
771 matchs ("end distribute simd", gfc_match_omp_eos,
772 ST_OMP_END_DISTRIBUTE_SIMD);
773 matcho ("end distribute", gfc_match_omp_eos, ST_OMP_END_DISTRIBUTE);
774 matchs ("end do simd", gfc_match_omp_end_nowait, ST_OMP_END_DO_SIMD);
775 matcho ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
776 matchs ("end simd", gfc_match_omp_eos, ST_OMP_END_SIMD);
777 matcho ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
778 matcho ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
779 matchs ("end parallel do simd", gfc_match_omp_eos,
780 ST_OMP_END_PARALLEL_DO_SIMD);
781 matcho ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
782 matcho ("end parallel sections", gfc_match_omp_eos,
783 ST_OMP_END_PARALLEL_SECTIONS);
784 matcho ("end parallel workshare", gfc_match_omp_eos,
785 ST_OMP_END_PARALLEL_WORKSHARE);
786 matcho ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
787 matcho ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
788 matcho ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
789 matcho ("end target data", gfc_match_omp_eos, ST_OMP_END_TARGET_DATA);
790 matchs ("end target teams distribute parallel do simd",
791 gfc_match_omp_eos,
792 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
793 matcho ("end target teams distribute parallel do", gfc_match_omp_eos,
794 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
795 matchs ("end target teams distribute simd", gfc_match_omp_eos,
796 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD);
797 matcho ("end target teams distribute", gfc_match_omp_eos,
798 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE);
799 matcho ("end target teams", gfc_match_omp_eos, ST_OMP_END_TARGET_TEAMS);
800 matcho ("end target", gfc_match_omp_eos, ST_OMP_END_TARGET);
801 matcho ("end taskgroup", gfc_match_omp_eos, ST_OMP_END_TASKGROUP);
802 matcho ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
803 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos,
804 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
805 matcho ("end teams distribute parallel do", gfc_match_omp_eos,
806 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO);
807 matchs ("end teams distribute simd", gfc_match_omp_eos,
808 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD);
809 matcho ("end teams distribute", gfc_match_omp_eos,
810 ST_OMP_END_TEAMS_DISTRIBUTE);
811 matcho ("end teams", gfc_match_omp_eos, ST_OMP_END_TEAMS);
812 matcho ("end workshare", gfc_match_omp_end_nowait,
813 ST_OMP_END_WORKSHARE);
814 break;
815 case 'f':
816 matcho ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
817 break;
818 case 'm':
819 matcho ("master", gfc_match_omp_master, ST_OMP_MASTER);
820 break;
821 case 'o':
822 matcho ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
823 break;
824 case 'p':
825 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
826 ST_OMP_PARALLEL_DO_SIMD);
827 matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
828 matcho ("parallel sections", gfc_match_omp_parallel_sections,
829 ST_OMP_PARALLEL_SECTIONS);
830 matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
831 ST_OMP_PARALLEL_WORKSHARE);
832 matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
833 break;
834 case 's':
835 matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
836 matcho ("section", gfc_match_omp_eos, ST_OMP_SECTION);
837 matchs ("simd", gfc_match_omp_simd, ST_OMP_SIMD);
838 matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
839 break;
840 case 't':
841 matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
842 matchs ("target teams distribute parallel do simd",
843 gfc_match_omp_target_teams_distribute_parallel_do_simd,
844 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
845 matcho ("target teams distribute parallel do",
846 gfc_match_omp_target_teams_distribute_parallel_do,
847 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
848 matchs ("target teams distribute simd",
849 gfc_match_omp_target_teams_distribute_simd,
850 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
851 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
852 ST_OMP_TARGET_TEAMS_DISTRIBUTE);
853 matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
854 matcho ("target update", gfc_match_omp_target_update,
855 ST_OMP_TARGET_UPDATE);
856 matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
857 matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
858 matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
859 matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
860 matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
861 matchs ("teams distribute parallel do simd",
862 gfc_match_omp_teams_distribute_parallel_do_simd,
863 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
864 matcho ("teams distribute parallel do",
865 gfc_match_omp_teams_distribute_parallel_do,
866 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
867 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
868 ST_OMP_TEAMS_DISTRIBUTE_SIMD);
869 matcho ("teams distribute", gfc_match_omp_teams_distribute,
870 ST_OMP_TEAMS_DISTRIBUTE);
871 matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
872 matcho ("threadprivate", gfc_match_omp_threadprivate,
873 ST_OMP_THREADPRIVATE);
874 break;
875 case 'w':
876 matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
877 break;
880 /* All else has failed, so give up. See if any of the matchers has
881 stored an error message of some sort. Don't error out if
882 not -fopenmp and simd_matched is false, i.e. if a directive other
883 than one marked with match has been seen. */
885 if (flag_openmp || simd_matched)
887 if (!gfc_error_check ())
888 gfc_error_now ("Unclassifiable OpenMP directive at %C");
891 reject_statement ();
893 gfc_error_recovery ();
895 return ST_NONE;
898 static gfc_statement
899 decode_gcc_attribute (void)
901 locus old_locus;
903 gfc_enforce_clean_symbol_state ();
905 gfc_clear_error (); /* Clear any pending errors. */
906 gfc_clear_warning (); /* Clear any pending warnings. */
907 old_locus = gfc_current_locus;
909 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
911 /* All else has failed, so give up. See if any of the matchers has
912 stored an error message of some sort. */
914 if (!gfc_error_check ())
915 gfc_error_now ("Unclassifiable GCC directive at %C");
917 reject_statement ();
919 gfc_error_recovery ();
921 return ST_NONE;
924 #undef match
926 /* Assert next length characters to be equal to token in free form. */
928 static void
929 verify_token_free (const char* token, int length, bool last_was_use_stmt)
931 int i;
932 char c;
934 c = gfc_next_ascii_char ();
935 for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
936 gcc_assert (c == token[i]);
938 gcc_assert (gfc_is_whitespace(c));
939 gfc_gobble_whitespace ();
940 if (last_was_use_stmt)
941 use_modules ();
944 /* Get the next statement in free form source. */
946 static gfc_statement
947 next_free (void)
949 match m;
950 int i, cnt, at_bol;
951 char c;
953 at_bol = gfc_at_bol ();
954 gfc_gobble_whitespace ();
956 c = gfc_peek_ascii_char ();
958 if (ISDIGIT (c))
960 char d;
962 /* Found a statement label? */
963 m = gfc_match_st_label (&gfc_statement_label);
965 d = gfc_peek_ascii_char ();
966 if (m != MATCH_YES || !gfc_is_whitespace (d))
968 gfc_match_small_literal_int (&i, &cnt);
970 if (cnt > 5)
971 gfc_error_now ("Too many digits in statement label at %C");
973 if (i == 0)
974 gfc_error_now ("Zero is not a valid statement label at %C");
977 c = gfc_next_ascii_char ();
978 while (ISDIGIT(c));
980 if (!gfc_is_whitespace (c))
981 gfc_error_now ("Non-numeric character in statement label at %C");
983 return ST_NONE;
985 else
987 label_locus = gfc_current_locus;
989 gfc_gobble_whitespace ();
991 if (at_bol && gfc_peek_ascii_char () == ';')
993 gfc_error_now ("Semicolon at %C needs to be preceded by "
994 "statement");
995 gfc_next_ascii_char (); /* Eat up the semicolon. */
996 return ST_NONE;
999 if (gfc_match_eos () == MATCH_YES)
1001 gfc_warning_now (0, "Ignoring statement label in empty statement "
1002 "at %L", &label_locus);
1003 gfc_free_st_label (gfc_statement_label);
1004 gfc_statement_label = NULL;
1005 return ST_NONE;
1009 else if (c == '!')
1011 /* Comments have already been skipped by the time we get here,
1012 except for GCC attributes and OpenMP/OpenACC directives. */
1014 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
1015 c = gfc_peek_ascii_char ();
1017 if (c == 'g')
1019 int i;
1021 c = gfc_next_ascii_char ();
1022 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
1023 gcc_assert (c == "gcc$"[i]);
1025 gfc_gobble_whitespace ();
1026 return decode_gcc_attribute ();
1029 else if (c == '$')
1031 /* Since both OpenMP and OpenACC directives starts with
1032 !$ character sequence, we must check all flags combinations */
1033 if ((flag_openmp || flag_openmp_simd)
1034 && !flag_openacc)
1036 verify_token_free ("$omp", 4, last_was_use_stmt);
1037 return decode_omp_directive ();
1039 else if ((flag_openmp || flag_openmp_simd)
1040 && flag_openacc)
1042 gfc_next_ascii_char (); /* Eat up dollar character */
1043 c = gfc_peek_ascii_char ();
1045 if (c == 'o')
1047 verify_token_free ("omp", 3, last_was_use_stmt);
1048 return decode_omp_directive ();
1050 else if (c == 'a')
1052 verify_token_free ("acc", 3, last_was_use_stmt);
1053 return decode_oacc_directive ();
1056 else if (flag_openacc)
1058 verify_token_free ("$acc", 4, last_was_use_stmt);
1059 return decode_oacc_directive ();
1062 gcc_unreachable ();
1065 if (at_bol && c == ';')
1067 if (!(gfc_option.allow_std & GFC_STD_F2008))
1068 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1069 "statement");
1070 gfc_next_ascii_char (); /* Eat up the semicolon. */
1071 return ST_NONE;
1074 return decode_statement ();
1077 /* Assert next length characters to be equal to token in fixed form. */
1079 static bool
1080 verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
1082 int i;
1083 char c = gfc_next_char_literal (NONSTRING);
1085 for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
1086 gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
1088 if (c != ' ' && c != '0')
1090 gfc_buffer_error (false);
1091 gfc_error ("Bad continuation line at %C");
1092 return false;
1094 if (last_was_use_stmt)
1095 use_modules ();
1097 return true;
1100 /* Get the next statement in fixed-form source. */
1102 static gfc_statement
1103 next_fixed (void)
1105 int label, digit_flag, i;
1106 locus loc;
1107 gfc_char_t c;
1109 if (!gfc_at_bol ())
1110 return decode_statement ();
1112 /* Skip past the current label field, parsing a statement label if
1113 one is there. This is a weird number parser, since the number is
1114 contained within five columns and can have any kind of embedded
1115 spaces. We also check for characters that make the rest of the
1116 line a comment. */
1118 label = 0;
1119 digit_flag = 0;
1121 for (i = 0; i < 5; i++)
1123 c = gfc_next_char_literal (NONSTRING);
1125 switch (c)
1127 case ' ':
1128 break;
1130 case '0':
1131 case '1':
1132 case '2':
1133 case '3':
1134 case '4':
1135 case '5':
1136 case '6':
1137 case '7':
1138 case '8':
1139 case '9':
1140 label = label * 10 + ((unsigned char) c - '0');
1141 label_locus = gfc_current_locus;
1142 digit_flag = 1;
1143 break;
1145 /* Comments have already been skipped by the time we get
1146 here, except for GCC attributes and OpenMP directives. */
1148 case '*':
1149 c = gfc_next_char_literal (NONSTRING);
1151 if (TOLOWER (c) == 'g')
1153 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
1154 gcc_assert (TOLOWER (c) == "gcc$"[i]);
1156 return decode_gcc_attribute ();
1158 else if (c == '$')
1160 if ((flag_openmp || flag_openmp_simd)
1161 && !flag_openacc)
1163 if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
1164 return ST_NONE;
1165 return decode_omp_directive ();
1167 else if ((flag_openmp || flag_openmp_simd)
1168 && flag_openacc)
1170 c = gfc_next_char_literal(NONSTRING);
1171 if (c == 'o' || c == 'O')
1173 if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
1174 return ST_NONE;
1175 return decode_omp_directive ();
1177 else if (c == 'a' || c == 'A')
1179 if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
1180 return ST_NONE;
1181 return decode_oacc_directive ();
1184 else if (flag_openacc)
1186 if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
1187 return ST_NONE;
1188 return decode_oacc_directive ();
1191 /* FALLTHROUGH */
1193 /* Comments have already been skipped by the time we get
1194 here so don't bother checking for them. */
1196 default:
1197 gfc_buffer_error (false);
1198 gfc_error ("Non-numeric character in statement label at %C");
1199 return ST_NONE;
1203 if (digit_flag)
1205 if (label == 0)
1206 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1207 else
1209 /* We've found a valid statement label. */
1210 gfc_statement_label = gfc_get_st_label (label);
1214 /* Since this line starts a statement, it cannot be a continuation
1215 of a previous statement. If we see something here besides a
1216 space or zero, it must be a bad continuation line. */
1218 c = gfc_next_char_literal (NONSTRING);
1219 if (c == '\n')
1220 goto blank_line;
1222 if (c != ' ' && c != '0')
1224 gfc_buffer_error (false);
1225 gfc_error ("Bad continuation line at %C");
1226 return ST_NONE;
1229 /* Now that we've taken care of the statement label columns, we have
1230 to make sure that the first nonblank character is not a '!'. If
1231 it is, the rest of the line is a comment. */
1235 loc = gfc_current_locus;
1236 c = gfc_next_char_literal (NONSTRING);
1238 while (gfc_is_whitespace (c));
1240 if (c == '!')
1241 goto blank_line;
1242 gfc_current_locus = loc;
1244 if (c == ';')
1246 if (digit_flag)
1247 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1248 else if (!(gfc_option.allow_std & GFC_STD_F2008))
1249 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1250 "statement");
1251 return ST_NONE;
1254 if (gfc_match_eos () == MATCH_YES)
1255 goto blank_line;
1257 /* At this point, we've got a nonblank statement to parse. */
1258 return decode_statement ();
1260 blank_line:
1261 if (digit_flag)
1262 gfc_warning_now (0, "Ignoring statement label in empty statement at %L",
1263 &label_locus);
1265 gfc_current_locus.lb->truncated = 0;
1266 gfc_advance_line ();
1267 return ST_NONE;
1271 /* Return the next non-ST_NONE statement to the caller. We also worry
1272 about including files and the ends of include files at this stage. */
1274 static gfc_statement
1275 next_statement (void)
1277 gfc_statement st;
1278 locus old_locus;
1280 gfc_enforce_clean_symbol_state ();
1282 gfc_new_block = NULL;
1284 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
1285 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
1286 gfc_current_ns->old_data = gfc_current_ns->data;
1287 for (;;)
1289 gfc_statement_label = NULL;
1290 gfc_buffer_error (true);
1292 if (gfc_at_eol ())
1293 gfc_advance_line ();
1295 gfc_skip_comments ();
1297 if (gfc_at_end ())
1299 st = ST_NONE;
1300 break;
1303 if (gfc_define_undef_line ())
1304 continue;
1306 old_locus = gfc_current_locus;
1308 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
1310 if (st != ST_NONE)
1311 break;
1314 gfc_buffer_error (false);
1316 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
1318 gfc_free_st_label (gfc_statement_label);
1319 gfc_statement_label = NULL;
1320 gfc_current_locus = old_locus;
1323 if (st != ST_NONE)
1324 check_statement_label (st);
1326 return st;
1330 /****************************** Parser ***********************************/
1332 /* The parser subroutines are of type 'try' that fail if the file ends
1333 unexpectedly. */
1335 /* Macros that expand to case-labels for various classes of
1336 statements. Start with executable statements that directly do
1337 things. */
1339 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1340 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1341 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1342 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1343 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1344 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1345 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1346 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1347 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1348 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1349 case ST_OMP_TARGET_UPDATE: case ST_ERROR_STOP: case ST_SYNC_ALL: \
1350 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1351 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1352 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1354 /* Statements that mark other executable statements. */
1356 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1357 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1358 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1359 case ST_OMP_PARALLEL: \
1360 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1361 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1362 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1363 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1364 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1365 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1366 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1367 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1368 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1369 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1370 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1371 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1372 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1373 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1374 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1375 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1376 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: \
1377 case ST_CRITICAL: \
1378 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1379 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: \
1380 case ST_OACC_KERNELS_LOOP: case ST_OACC_ATOMIC
1382 /* Declaration statements */
1384 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1385 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1386 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
1387 case ST_PROCEDURE: case ST_OMP_DECLARE_SIMD: case ST_OMP_DECLARE_REDUCTION: \
1388 case ST_OMP_DECLARE_TARGET: case ST_OACC_ROUTINE: case ST_OACC_DECLARE
1390 /* Block end statements. Errors associated with interchanging these
1391 are detected in gfc_match_end(). */
1393 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1394 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1395 case ST_END_BLOCK: case ST_END_ASSOCIATE
1398 /* Push a new state onto the stack. */
1400 static void
1401 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
1403 p->state = new_state;
1404 p->previous = gfc_state_stack;
1405 p->sym = sym;
1406 p->head = p->tail = NULL;
1407 p->do_variable = NULL;
1408 if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
1409 p->ext.oacc_declare_clauses = NULL;
1411 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1412 construct statement was accepted right before pushing the state. Thus,
1413 the construct's gfc_code is available as tail of the parent state. */
1414 gcc_assert (gfc_state_stack);
1415 p->construct = gfc_state_stack->tail;
1417 gfc_state_stack = p;
1421 /* Pop the current state. */
1422 static void
1423 pop_state (void)
1425 gfc_state_stack = gfc_state_stack->previous;
1429 /* Try to find the given state in the state stack. */
1431 bool
1432 gfc_find_state (gfc_compile_state state)
1434 gfc_state_data *p;
1436 for (p = gfc_state_stack; p; p = p->previous)
1437 if (p->state == state)
1438 break;
1440 return (p == NULL) ? false : true;
1444 /* Starts a new level in the statement list. */
1446 static gfc_code *
1447 new_level (gfc_code *q)
1449 gfc_code *p;
1451 p = q->block = gfc_get_code (EXEC_NOP);
1453 gfc_state_stack->head = gfc_state_stack->tail = p;
1455 return p;
1459 /* Add the current new_st code structure and adds it to the current
1460 program unit. As a side-effect, it zeroes the new_st. */
1462 static gfc_code *
1463 add_statement (void)
1465 gfc_code *p;
1467 p = XCNEW (gfc_code);
1468 *p = new_st;
1470 p->loc = gfc_current_locus;
1472 if (gfc_state_stack->head == NULL)
1473 gfc_state_stack->head = p;
1474 else
1475 gfc_state_stack->tail->next = p;
1477 while (p->next != NULL)
1478 p = p->next;
1480 gfc_state_stack->tail = p;
1482 gfc_clear_new_st ();
1484 return p;
1488 /* Frees everything associated with the current statement. */
1490 static void
1491 undo_new_statement (void)
1493 gfc_free_statements (new_st.block);
1494 gfc_free_statements (new_st.next);
1495 gfc_free_statement (&new_st);
1496 gfc_clear_new_st ();
1500 /* If the current statement has a statement label, make sure that it
1501 is allowed to, or should have one. */
1503 static void
1504 check_statement_label (gfc_statement st)
1506 gfc_sl_type type;
1508 if (gfc_statement_label == NULL)
1510 if (st == ST_FORMAT)
1511 gfc_error ("FORMAT statement at %L does not have a statement label",
1512 &new_st.loc);
1513 return;
1516 switch (st)
1518 case ST_END_PROGRAM:
1519 case ST_END_FUNCTION:
1520 case ST_END_SUBROUTINE:
1521 case ST_ENDDO:
1522 case ST_ENDIF:
1523 case ST_END_SELECT:
1524 case ST_END_CRITICAL:
1525 case ST_END_BLOCK:
1526 case ST_END_ASSOCIATE:
1527 case_executable:
1528 case_exec_markers:
1529 if (st == ST_ENDDO || st == ST_CONTINUE)
1530 type = ST_LABEL_DO_TARGET;
1531 else
1532 type = ST_LABEL_TARGET;
1533 break;
1535 case ST_FORMAT:
1536 type = ST_LABEL_FORMAT;
1537 break;
1539 /* Statement labels are not restricted from appearing on a
1540 particular line. However, there are plenty of situations
1541 where the resulting label can't be referenced. */
1543 default:
1544 type = ST_LABEL_BAD_TARGET;
1545 break;
1548 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1550 new_st.here = gfc_statement_label;
1554 /* Figures out what the enclosing program unit is. This will be a
1555 function, subroutine, program, block data or module. */
1557 gfc_state_data *
1558 gfc_enclosing_unit (gfc_compile_state * result)
1560 gfc_state_data *p;
1562 for (p = gfc_state_stack; p; p = p->previous)
1563 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1564 || p->state == COMP_MODULE || p->state == COMP_SUBMODULE
1565 || p->state == COMP_BLOCK_DATA || p->state == COMP_PROGRAM)
1568 if (result != NULL)
1569 *result = p->state;
1570 return p;
1573 if (result != NULL)
1574 *result = COMP_PROGRAM;
1575 return NULL;
1579 /* Translate a statement enum to a string. */
1581 const char *
1582 gfc_ascii_statement (gfc_statement st)
1584 const char *p;
1586 switch (st)
1588 case ST_ARITHMETIC_IF:
1589 p = _("arithmetic IF");
1590 break;
1591 case ST_ALLOCATE:
1592 p = "ALLOCATE";
1593 break;
1594 case ST_ASSOCIATE:
1595 p = "ASSOCIATE";
1596 break;
1597 case ST_ATTR_DECL:
1598 p = _("attribute declaration");
1599 break;
1600 case ST_BACKSPACE:
1601 p = "BACKSPACE";
1602 break;
1603 case ST_BLOCK:
1604 p = "BLOCK";
1605 break;
1606 case ST_BLOCK_DATA:
1607 p = "BLOCK DATA";
1608 break;
1609 case ST_CALL:
1610 p = "CALL";
1611 break;
1612 case ST_CASE:
1613 p = "CASE";
1614 break;
1615 case ST_CLOSE:
1616 p = "CLOSE";
1617 break;
1618 case ST_COMMON:
1619 p = "COMMON";
1620 break;
1621 case ST_CONTINUE:
1622 p = "CONTINUE";
1623 break;
1624 case ST_CONTAINS:
1625 p = "CONTAINS";
1626 break;
1627 case ST_CRITICAL:
1628 p = "CRITICAL";
1629 break;
1630 case ST_CYCLE:
1631 p = "CYCLE";
1632 break;
1633 case ST_DATA_DECL:
1634 p = _("data declaration");
1635 break;
1636 case ST_DATA:
1637 p = "DATA";
1638 break;
1639 case ST_DEALLOCATE:
1640 p = "DEALLOCATE";
1641 break;
1642 case ST_DERIVED_DECL:
1643 p = _("derived type declaration");
1644 break;
1645 case ST_DO:
1646 p = "DO";
1647 break;
1648 case ST_ELSE:
1649 p = "ELSE";
1650 break;
1651 case ST_ELSEIF:
1652 p = "ELSE IF";
1653 break;
1654 case ST_ELSEWHERE:
1655 p = "ELSEWHERE";
1656 break;
1657 case ST_END_ASSOCIATE:
1658 p = "END ASSOCIATE";
1659 break;
1660 case ST_END_BLOCK:
1661 p = "END BLOCK";
1662 break;
1663 case ST_END_BLOCK_DATA:
1664 p = "END BLOCK DATA";
1665 break;
1666 case ST_END_CRITICAL:
1667 p = "END CRITICAL";
1668 break;
1669 case ST_ENDDO:
1670 p = "END DO";
1671 break;
1672 case ST_END_FILE:
1673 p = "END FILE";
1674 break;
1675 case ST_END_FORALL:
1676 p = "END FORALL";
1677 break;
1678 case ST_END_FUNCTION:
1679 p = "END FUNCTION";
1680 break;
1681 case ST_ENDIF:
1682 p = "END IF";
1683 break;
1684 case ST_END_INTERFACE:
1685 p = "END INTERFACE";
1686 break;
1687 case ST_END_MODULE:
1688 p = "END MODULE";
1689 break;
1690 case ST_END_SUBMODULE:
1691 p = "END SUBMODULE";
1692 break;
1693 case ST_END_PROGRAM:
1694 p = "END PROGRAM";
1695 break;
1696 case ST_END_SELECT:
1697 p = "END SELECT";
1698 break;
1699 case ST_END_SUBROUTINE:
1700 p = "END SUBROUTINE";
1701 break;
1702 case ST_END_WHERE:
1703 p = "END WHERE";
1704 break;
1705 case ST_END_TYPE:
1706 p = "END TYPE";
1707 break;
1708 case ST_ENTRY:
1709 p = "ENTRY";
1710 break;
1711 case ST_EQUIVALENCE:
1712 p = "EQUIVALENCE";
1713 break;
1714 case ST_ERROR_STOP:
1715 p = "ERROR STOP";
1716 break;
1717 case ST_EXIT:
1718 p = "EXIT";
1719 break;
1720 case ST_FLUSH:
1721 p = "FLUSH";
1722 break;
1723 case ST_FORALL_BLOCK: /* Fall through */
1724 case ST_FORALL:
1725 p = "FORALL";
1726 break;
1727 case ST_FORMAT:
1728 p = "FORMAT";
1729 break;
1730 case ST_FUNCTION:
1731 p = "FUNCTION";
1732 break;
1733 case ST_GENERIC:
1734 p = "GENERIC";
1735 break;
1736 case ST_GOTO:
1737 p = "GOTO";
1738 break;
1739 case ST_IF_BLOCK:
1740 p = _("block IF");
1741 break;
1742 case ST_IMPLICIT:
1743 p = "IMPLICIT";
1744 break;
1745 case ST_IMPLICIT_NONE:
1746 p = "IMPLICIT NONE";
1747 break;
1748 case ST_IMPLIED_ENDDO:
1749 p = _("implied END DO");
1750 break;
1751 case ST_IMPORT:
1752 p = "IMPORT";
1753 break;
1754 case ST_INQUIRE:
1755 p = "INQUIRE";
1756 break;
1757 case ST_INTERFACE:
1758 p = "INTERFACE";
1759 break;
1760 case ST_LOCK:
1761 p = "LOCK";
1762 break;
1763 case ST_PARAMETER:
1764 p = "PARAMETER";
1765 break;
1766 case ST_PRIVATE:
1767 p = "PRIVATE";
1768 break;
1769 case ST_PUBLIC:
1770 p = "PUBLIC";
1771 break;
1772 case ST_MODULE:
1773 p = "MODULE";
1774 break;
1775 case ST_SUBMODULE:
1776 p = "SUBMODULE";
1777 break;
1778 case ST_PAUSE:
1779 p = "PAUSE";
1780 break;
1781 case ST_MODULE_PROC:
1782 p = "MODULE PROCEDURE";
1783 break;
1784 case ST_NAMELIST:
1785 p = "NAMELIST";
1786 break;
1787 case ST_NULLIFY:
1788 p = "NULLIFY";
1789 break;
1790 case ST_OPEN:
1791 p = "OPEN";
1792 break;
1793 case ST_PROGRAM:
1794 p = "PROGRAM";
1795 break;
1796 case ST_PROCEDURE:
1797 p = "PROCEDURE";
1798 break;
1799 case ST_READ:
1800 p = "READ";
1801 break;
1802 case ST_RETURN:
1803 p = "RETURN";
1804 break;
1805 case ST_REWIND:
1806 p = "REWIND";
1807 break;
1808 case ST_STOP:
1809 p = "STOP";
1810 break;
1811 case ST_SYNC_ALL:
1812 p = "SYNC ALL";
1813 break;
1814 case ST_SYNC_IMAGES:
1815 p = "SYNC IMAGES";
1816 break;
1817 case ST_SYNC_MEMORY:
1818 p = "SYNC MEMORY";
1819 break;
1820 case ST_SUBROUTINE:
1821 p = "SUBROUTINE";
1822 break;
1823 case ST_TYPE:
1824 p = "TYPE";
1825 break;
1826 case ST_UNLOCK:
1827 p = "UNLOCK";
1828 break;
1829 case ST_USE:
1830 p = "USE";
1831 break;
1832 case ST_WHERE_BLOCK: /* Fall through */
1833 case ST_WHERE:
1834 p = "WHERE";
1835 break;
1836 case ST_WAIT:
1837 p = "WAIT";
1838 break;
1839 case ST_WRITE:
1840 p = "WRITE";
1841 break;
1842 case ST_ASSIGNMENT:
1843 p = _("assignment");
1844 break;
1845 case ST_POINTER_ASSIGNMENT:
1846 p = _("pointer assignment");
1847 break;
1848 case ST_SELECT_CASE:
1849 p = "SELECT CASE";
1850 break;
1851 case ST_SELECT_TYPE:
1852 p = "SELECT TYPE";
1853 break;
1854 case ST_TYPE_IS:
1855 p = "TYPE IS";
1856 break;
1857 case ST_CLASS_IS:
1858 p = "CLASS IS";
1859 break;
1860 case ST_SEQUENCE:
1861 p = "SEQUENCE";
1862 break;
1863 case ST_SIMPLE_IF:
1864 p = _("simple IF");
1865 break;
1866 case ST_STATEMENT_FUNCTION:
1867 p = "STATEMENT FUNCTION";
1868 break;
1869 case ST_LABEL_ASSIGNMENT:
1870 p = "LABEL ASSIGNMENT";
1871 break;
1872 case ST_ENUM:
1873 p = "ENUM DEFINITION";
1874 break;
1875 case ST_ENUMERATOR:
1876 p = "ENUMERATOR DEFINITION";
1877 break;
1878 case ST_END_ENUM:
1879 p = "END ENUM";
1880 break;
1881 case ST_OACC_PARALLEL_LOOP:
1882 p = "!$ACC PARALLEL LOOP";
1883 break;
1884 case ST_OACC_END_PARALLEL_LOOP:
1885 p = "!$ACC END PARALLEL LOOP";
1886 break;
1887 case ST_OACC_PARALLEL:
1888 p = "!$ACC PARALLEL";
1889 break;
1890 case ST_OACC_END_PARALLEL:
1891 p = "!$ACC END PARALLEL";
1892 break;
1893 case ST_OACC_KERNELS:
1894 p = "!$ACC KERNELS";
1895 break;
1896 case ST_OACC_END_KERNELS:
1897 p = "!$ACC END KERNELS";
1898 break;
1899 case ST_OACC_KERNELS_LOOP:
1900 p = "!$ACC KERNELS LOOP";
1901 break;
1902 case ST_OACC_END_KERNELS_LOOP:
1903 p = "!$ACC END KERNELS LOOP";
1904 break;
1905 case ST_OACC_DATA:
1906 p = "!$ACC DATA";
1907 break;
1908 case ST_OACC_END_DATA:
1909 p = "!$ACC END DATA";
1910 break;
1911 case ST_OACC_HOST_DATA:
1912 p = "!$ACC HOST_DATA";
1913 break;
1914 case ST_OACC_END_HOST_DATA:
1915 p = "!$ACC END HOST_DATA";
1916 break;
1917 case ST_OACC_LOOP:
1918 p = "!$ACC LOOP";
1919 break;
1920 case ST_OACC_END_LOOP:
1921 p = "!$ACC END LOOP";
1922 break;
1923 case ST_OACC_DECLARE:
1924 p = "!$ACC DECLARE";
1925 break;
1926 case ST_OACC_UPDATE:
1927 p = "!$ACC UPDATE";
1928 break;
1929 case ST_OACC_WAIT:
1930 p = "!$ACC WAIT";
1931 break;
1932 case ST_OACC_CACHE:
1933 p = "!$ACC CACHE";
1934 break;
1935 case ST_OACC_ENTER_DATA:
1936 p = "!$ACC ENTER DATA";
1937 break;
1938 case ST_OACC_EXIT_DATA:
1939 p = "!$ACC EXIT DATA";
1940 break;
1941 case ST_OACC_ROUTINE:
1942 p = "!$ACC ROUTINE";
1943 break;
1944 case ST_OACC_ATOMIC:
1945 p = "!ACC ATOMIC";
1946 break;
1947 case ST_OACC_END_ATOMIC:
1948 p = "!ACC END ATOMIC";
1949 break;
1950 case ST_OMP_ATOMIC:
1951 p = "!$OMP ATOMIC";
1952 break;
1953 case ST_OMP_BARRIER:
1954 p = "!$OMP BARRIER";
1955 break;
1956 case ST_OMP_CANCEL:
1957 p = "!$OMP CANCEL";
1958 break;
1959 case ST_OMP_CANCELLATION_POINT:
1960 p = "!$OMP CANCELLATION POINT";
1961 break;
1962 case ST_OMP_CRITICAL:
1963 p = "!$OMP CRITICAL";
1964 break;
1965 case ST_OMP_DECLARE_REDUCTION:
1966 p = "!$OMP DECLARE REDUCTION";
1967 break;
1968 case ST_OMP_DECLARE_SIMD:
1969 p = "!$OMP DECLARE SIMD";
1970 break;
1971 case ST_OMP_DECLARE_TARGET:
1972 p = "!$OMP DECLARE TARGET";
1973 break;
1974 case ST_OMP_DISTRIBUTE:
1975 p = "!$OMP DISTRIBUTE";
1976 break;
1977 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
1978 p = "!$OMP DISTRIBUTE PARALLEL DO";
1979 break;
1980 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
1981 p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
1982 break;
1983 case ST_OMP_DISTRIBUTE_SIMD:
1984 p = "!$OMP DISTRIBUTE SIMD";
1985 break;
1986 case ST_OMP_DO:
1987 p = "!$OMP DO";
1988 break;
1989 case ST_OMP_DO_SIMD:
1990 p = "!$OMP DO SIMD";
1991 break;
1992 case ST_OMP_END_ATOMIC:
1993 p = "!$OMP END ATOMIC";
1994 break;
1995 case ST_OMP_END_CRITICAL:
1996 p = "!$OMP END CRITICAL";
1997 break;
1998 case ST_OMP_END_DISTRIBUTE:
1999 p = "!$OMP END DISTRIBUTE";
2000 break;
2001 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
2002 p = "!$OMP END DISTRIBUTE PARALLEL DO";
2003 break;
2004 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
2005 p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
2006 break;
2007 case ST_OMP_END_DISTRIBUTE_SIMD:
2008 p = "!$OMP END DISTRIBUTE SIMD";
2009 break;
2010 case ST_OMP_END_DO:
2011 p = "!$OMP END DO";
2012 break;
2013 case ST_OMP_END_DO_SIMD:
2014 p = "!$OMP END DO SIMD";
2015 break;
2016 case ST_OMP_END_SIMD:
2017 p = "!$OMP END SIMD";
2018 break;
2019 case ST_OMP_END_MASTER:
2020 p = "!$OMP END MASTER";
2021 break;
2022 case ST_OMP_END_ORDERED:
2023 p = "!$OMP END ORDERED";
2024 break;
2025 case ST_OMP_END_PARALLEL:
2026 p = "!$OMP END PARALLEL";
2027 break;
2028 case ST_OMP_END_PARALLEL_DO:
2029 p = "!$OMP END PARALLEL DO";
2030 break;
2031 case ST_OMP_END_PARALLEL_DO_SIMD:
2032 p = "!$OMP END PARALLEL DO SIMD";
2033 break;
2034 case ST_OMP_END_PARALLEL_SECTIONS:
2035 p = "!$OMP END PARALLEL SECTIONS";
2036 break;
2037 case ST_OMP_END_PARALLEL_WORKSHARE:
2038 p = "!$OMP END PARALLEL WORKSHARE";
2039 break;
2040 case ST_OMP_END_SECTIONS:
2041 p = "!$OMP END SECTIONS";
2042 break;
2043 case ST_OMP_END_SINGLE:
2044 p = "!$OMP END SINGLE";
2045 break;
2046 case ST_OMP_END_TASK:
2047 p = "!$OMP END TASK";
2048 break;
2049 case ST_OMP_END_TARGET:
2050 p = "!$OMP END TARGET";
2051 break;
2052 case ST_OMP_END_TARGET_DATA:
2053 p = "!$OMP END TARGET DATA";
2054 break;
2055 case ST_OMP_END_TARGET_TEAMS:
2056 p = "!$OMP END TARGET TEAMS";
2057 break;
2058 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
2059 p = "!$OMP END TARGET TEAMS DISTRIBUTE";
2060 break;
2061 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2062 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2063 break;
2064 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2065 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2066 break;
2067 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
2068 p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2069 break;
2070 case ST_OMP_END_TASKGROUP:
2071 p = "!$OMP END TASKGROUP";
2072 break;
2073 case ST_OMP_END_TEAMS:
2074 p = "!$OMP END TEAMS";
2075 break;
2076 case ST_OMP_END_TEAMS_DISTRIBUTE:
2077 p = "!$OMP END TEAMS DISTRIBUTE";
2078 break;
2079 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
2080 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2081 break;
2082 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2083 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2084 break;
2085 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
2086 p = "!$OMP END TEAMS DISTRIBUTE SIMD";
2087 break;
2088 case ST_OMP_END_WORKSHARE:
2089 p = "!$OMP END WORKSHARE";
2090 break;
2091 case ST_OMP_FLUSH:
2092 p = "!$OMP FLUSH";
2093 break;
2094 case ST_OMP_MASTER:
2095 p = "!$OMP MASTER";
2096 break;
2097 case ST_OMP_ORDERED:
2098 p = "!$OMP ORDERED";
2099 break;
2100 case ST_OMP_PARALLEL:
2101 p = "!$OMP PARALLEL";
2102 break;
2103 case ST_OMP_PARALLEL_DO:
2104 p = "!$OMP PARALLEL DO";
2105 break;
2106 case ST_OMP_PARALLEL_DO_SIMD:
2107 p = "!$OMP PARALLEL DO SIMD";
2108 break;
2109 case ST_OMP_PARALLEL_SECTIONS:
2110 p = "!$OMP PARALLEL SECTIONS";
2111 break;
2112 case ST_OMP_PARALLEL_WORKSHARE:
2113 p = "!$OMP PARALLEL WORKSHARE";
2114 break;
2115 case ST_OMP_SECTIONS:
2116 p = "!$OMP SECTIONS";
2117 break;
2118 case ST_OMP_SECTION:
2119 p = "!$OMP SECTION";
2120 break;
2121 case ST_OMP_SIMD:
2122 p = "!$OMP SIMD";
2123 break;
2124 case ST_OMP_SINGLE:
2125 p = "!$OMP SINGLE";
2126 break;
2127 case ST_OMP_TARGET:
2128 p = "!$OMP TARGET";
2129 break;
2130 case ST_OMP_TARGET_DATA:
2131 p = "!$OMP TARGET DATA";
2132 break;
2133 case ST_OMP_TARGET_TEAMS:
2134 p = "!$OMP TARGET TEAMS";
2135 break;
2136 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
2137 p = "!$OMP TARGET TEAMS DISTRIBUTE";
2138 break;
2139 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2140 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2141 break;
2142 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2143 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2144 break;
2145 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2146 p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2147 break;
2148 case ST_OMP_TARGET_UPDATE:
2149 p = "!$OMP TARGET UPDATE";
2150 break;
2151 case ST_OMP_TASK:
2152 p = "!$OMP TASK";
2153 break;
2154 case ST_OMP_TASKGROUP:
2155 p = "!$OMP TASKGROUP";
2156 break;
2157 case ST_OMP_TASKWAIT:
2158 p = "!$OMP TASKWAIT";
2159 break;
2160 case ST_OMP_TASKYIELD:
2161 p = "!$OMP TASKYIELD";
2162 break;
2163 case ST_OMP_TEAMS:
2164 p = "!$OMP TEAMS";
2165 break;
2166 case ST_OMP_TEAMS_DISTRIBUTE:
2167 p = "!$OMP TEAMS DISTRIBUTE";
2168 break;
2169 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2170 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2171 break;
2172 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2173 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2174 break;
2175 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
2176 p = "!$OMP TEAMS DISTRIBUTE SIMD";
2177 break;
2178 case ST_OMP_THREADPRIVATE:
2179 p = "!$OMP THREADPRIVATE";
2180 break;
2181 case ST_OMP_WORKSHARE:
2182 p = "!$OMP WORKSHARE";
2183 break;
2184 default:
2185 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2188 return p;
2192 /* Create a symbol for the main program and assign it to ns->proc_name. */
2194 static void
2195 main_program_symbol (gfc_namespace *ns, const char *name)
2197 gfc_symbol *main_program;
2198 symbol_attribute attr;
2200 gfc_get_symbol (name, ns, &main_program);
2201 gfc_clear_attr (&attr);
2202 attr.flavor = FL_PROGRAM;
2203 attr.proc = PROC_UNKNOWN;
2204 attr.subroutine = 1;
2205 attr.access = ACCESS_PUBLIC;
2206 attr.is_main_program = 1;
2207 main_program->attr = attr;
2208 main_program->declared_at = gfc_current_locus;
2209 ns->proc_name = main_program;
2210 gfc_commit_symbols ();
2214 /* Do whatever is necessary to accept the last statement. */
2216 static void
2217 accept_statement (gfc_statement st)
2219 switch (st)
2221 case ST_IMPLICIT_NONE:
2222 case ST_IMPLICIT:
2223 break;
2225 case ST_FUNCTION:
2226 case ST_SUBROUTINE:
2227 case ST_MODULE:
2228 case ST_SUBMODULE:
2229 gfc_current_ns->proc_name = gfc_new_block;
2230 break;
2232 /* If the statement is the end of a block, lay down a special code
2233 that allows a branch to the end of the block from within the
2234 construct. IF and SELECT are treated differently from DO
2235 (where EXEC_NOP is added inside the loop) for two
2236 reasons:
2237 1. END DO has a meaning in the sense that after a GOTO to
2238 it, the loop counter must be increased.
2239 2. IF blocks and SELECT blocks can consist of multiple
2240 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2241 Putting the label before the END IF would make the jump
2242 from, say, the ELSE IF block to the END IF illegal. */
2244 case ST_ENDIF:
2245 case ST_END_SELECT:
2246 case ST_END_CRITICAL:
2247 if (gfc_statement_label != NULL)
2249 new_st.op = EXEC_END_NESTED_BLOCK;
2250 add_statement ();
2252 break;
2254 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2255 one parallel block. Thus, we add the special code to the nested block
2256 itself, instead of the parent one. */
2257 case ST_END_BLOCK:
2258 case ST_END_ASSOCIATE:
2259 if (gfc_statement_label != NULL)
2261 new_st.op = EXEC_END_BLOCK;
2262 add_statement ();
2264 break;
2266 /* The end-of-program unit statements do not get the special
2267 marker and require a statement of some sort if they are a
2268 branch target. */
2270 case ST_END_PROGRAM:
2271 case ST_END_FUNCTION:
2272 case ST_END_SUBROUTINE:
2273 if (gfc_statement_label != NULL)
2275 new_st.op = EXEC_RETURN;
2276 add_statement ();
2278 else
2280 new_st.op = EXEC_END_PROCEDURE;
2281 add_statement ();
2284 break;
2286 case ST_ENTRY:
2287 case_executable:
2288 case_exec_markers:
2289 add_statement ();
2290 break;
2292 default:
2293 break;
2296 gfc_commit_symbols ();
2297 gfc_warning_check ();
2298 gfc_clear_new_st ();
2302 /* Undo anything tentative that has been built for the current
2303 statement. */
2305 static void
2306 reject_statement (void)
2308 /* Revert to the previous charlen chain. */
2309 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
2310 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
2312 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
2313 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
2315 gfc_reject_data (gfc_current_ns);
2317 gfc_new_block = NULL;
2318 gfc_undo_symbols ();
2319 gfc_clear_warning ();
2320 undo_new_statement ();
2324 /* Generic complaint about an out of order statement. We also do
2325 whatever is necessary to clean up. */
2327 static void
2328 unexpected_statement (gfc_statement st)
2330 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
2332 reject_statement ();
2336 /* Given the next statement seen by the matcher, make sure that it is
2337 in proper order with the last. This subroutine is initialized by
2338 calling it with an argument of ST_NONE. If there is a problem, we
2339 issue an error and return false. Otherwise we return true.
2341 Individual parsers need to verify that the statements seen are
2342 valid before calling here, i.e., ENTRY statements are not allowed in
2343 INTERFACE blocks. The following diagram is taken from the standard:
2345 +---------------------------------------+
2346 | program subroutine function module |
2347 +---------------------------------------+
2348 | use |
2349 +---------------------------------------+
2350 | import |
2351 +---------------------------------------+
2352 | | implicit none |
2353 | +-----------+------------------+
2354 | | parameter | implicit |
2355 | +-----------+------------------+
2356 | format | | derived type |
2357 | entry | parameter | interface |
2358 | | data | specification |
2359 | | | statement func |
2360 | +-----------+------------------+
2361 | | data | executable |
2362 +--------+-----------+------------------+
2363 | contains |
2364 +---------------------------------------+
2365 | internal module/subprogram |
2366 +---------------------------------------+
2367 | end |
2368 +---------------------------------------+
2372 enum state_order
2374 ORDER_START,
2375 ORDER_USE,
2376 ORDER_IMPORT,
2377 ORDER_IMPLICIT_NONE,
2378 ORDER_IMPLICIT,
2379 ORDER_SPEC,
2380 ORDER_EXEC
2383 typedef struct
2385 enum state_order state;
2386 gfc_statement last_statement;
2387 locus where;
2389 st_state;
2391 static bool
2392 verify_st_order (st_state *p, gfc_statement st, bool silent)
2395 switch (st)
2397 case ST_NONE:
2398 p->state = ORDER_START;
2399 break;
2401 case ST_USE:
2402 if (p->state > ORDER_USE)
2403 goto order;
2404 p->state = ORDER_USE;
2405 break;
2407 case ST_IMPORT:
2408 if (p->state > ORDER_IMPORT)
2409 goto order;
2410 p->state = ORDER_IMPORT;
2411 break;
2413 case ST_IMPLICIT_NONE:
2414 if (p->state > ORDER_IMPLICIT)
2415 goto order;
2417 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2418 statement disqualifies a USE but not an IMPLICIT NONE.
2419 Duplicate IMPLICIT NONEs are caught when the implicit types
2420 are set. */
2422 p->state = ORDER_IMPLICIT_NONE;
2423 break;
2425 case ST_IMPLICIT:
2426 if (p->state > ORDER_IMPLICIT)
2427 goto order;
2428 p->state = ORDER_IMPLICIT;
2429 break;
2431 case ST_FORMAT:
2432 case ST_ENTRY:
2433 if (p->state < ORDER_IMPLICIT_NONE)
2434 p->state = ORDER_IMPLICIT_NONE;
2435 break;
2437 case ST_PARAMETER:
2438 if (p->state >= ORDER_EXEC)
2439 goto order;
2440 if (p->state < ORDER_IMPLICIT)
2441 p->state = ORDER_IMPLICIT;
2442 break;
2444 case ST_DATA:
2445 if (p->state < ORDER_SPEC)
2446 p->state = ORDER_SPEC;
2447 break;
2449 case ST_PUBLIC:
2450 case ST_PRIVATE:
2451 case ST_DERIVED_DECL:
2452 case_decl:
2453 if (p->state >= ORDER_EXEC)
2454 goto order;
2455 if (p->state < ORDER_SPEC)
2456 p->state = ORDER_SPEC;
2457 break;
2459 case_executable:
2460 case_exec_markers:
2461 if (p->state < ORDER_EXEC)
2462 p->state = ORDER_EXEC;
2463 break;
2465 default:
2466 return false;
2469 /* All is well, record the statement in case we need it next time. */
2470 p->where = gfc_current_locus;
2471 p->last_statement = st;
2472 return true;
2474 order:
2475 if (!silent)
2476 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2477 gfc_ascii_statement (st),
2478 gfc_ascii_statement (p->last_statement), &p->where);
2480 return false;
2484 /* Handle an unexpected end of file. This is a show-stopper... */
2486 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
2488 static void
2489 unexpected_eof (void)
2491 gfc_state_data *p;
2493 gfc_error ("Unexpected end of file in %qs", gfc_source_file);
2495 /* Memory cleanup. Move to "second to last". */
2496 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
2497 p = p->previous);
2499 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
2500 gfc_done_2 ();
2502 longjmp (eof_buf, 1);
2506 /* Parse the CONTAINS section of a derived type definition. */
2508 gfc_access gfc_typebound_default_access;
2510 static bool
2511 parse_derived_contains (void)
2513 gfc_state_data s;
2514 bool seen_private = false;
2515 bool seen_comps = false;
2516 bool error_flag = false;
2517 bool to_finish;
2519 gcc_assert (gfc_current_state () == COMP_DERIVED);
2520 gcc_assert (gfc_current_block ());
2522 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2523 section. */
2524 if (gfc_current_block ()->attr.sequence)
2525 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2526 " section at %C", gfc_current_block ()->name);
2527 if (gfc_current_block ()->attr.is_bind_c)
2528 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2529 " section at %C", gfc_current_block ()->name);
2531 accept_statement (ST_CONTAINS);
2532 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
2534 gfc_typebound_default_access = ACCESS_PUBLIC;
2536 to_finish = false;
2537 while (!to_finish)
2539 gfc_statement st;
2540 st = next_statement ();
2541 switch (st)
2543 case ST_NONE:
2544 unexpected_eof ();
2545 break;
2547 case ST_DATA_DECL:
2548 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2549 goto error;
2551 case ST_PROCEDURE:
2552 if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
2553 goto error;
2555 accept_statement (ST_PROCEDURE);
2556 seen_comps = true;
2557 break;
2559 case ST_GENERIC:
2560 if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
2561 goto error;
2563 accept_statement (ST_GENERIC);
2564 seen_comps = true;
2565 break;
2567 case ST_FINAL:
2568 if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
2569 " at %C"))
2570 goto error;
2572 accept_statement (ST_FINAL);
2573 seen_comps = true;
2574 break;
2576 case ST_END_TYPE:
2577 to_finish = true;
2579 if (!seen_comps
2580 && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
2581 "at %C with empty CONTAINS section")))
2582 goto error;
2584 /* ST_END_TYPE is accepted by parse_derived after return. */
2585 break;
2587 case ST_PRIVATE:
2588 if (!gfc_find_state (COMP_MODULE))
2590 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2591 "a MODULE");
2592 goto error;
2595 if (seen_comps)
2597 gfc_error ("PRIVATE statement at %C must precede procedure"
2598 " bindings");
2599 goto error;
2602 if (seen_private)
2604 gfc_error ("Duplicate PRIVATE statement at %C");
2605 goto error;
2608 accept_statement (ST_PRIVATE);
2609 gfc_typebound_default_access = ACCESS_PRIVATE;
2610 seen_private = true;
2611 break;
2613 case ST_SEQUENCE:
2614 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2615 goto error;
2617 case ST_CONTAINS:
2618 gfc_error ("Already inside a CONTAINS block at %C");
2619 goto error;
2621 default:
2622 unexpected_statement (st);
2623 break;
2626 continue;
2628 error:
2629 error_flag = true;
2630 reject_statement ();
2633 pop_state ();
2634 gcc_assert (gfc_current_state () == COMP_DERIVED);
2636 return error_flag;
2640 /* Parse a derived type. */
2642 static void
2643 parse_derived (void)
2645 int compiling_type, seen_private, seen_sequence, seen_component;
2646 gfc_statement st;
2647 gfc_state_data s;
2648 gfc_symbol *sym;
2649 gfc_component *c, *lock_comp = NULL;
2651 accept_statement (ST_DERIVED_DECL);
2652 push_state (&s, COMP_DERIVED, gfc_new_block);
2654 gfc_new_block->component_access = ACCESS_PUBLIC;
2655 seen_private = 0;
2656 seen_sequence = 0;
2657 seen_component = 0;
2659 compiling_type = 1;
2661 while (compiling_type)
2663 st = next_statement ();
2664 switch (st)
2666 case ST_NONE:
2667 unexpected_eof ();
2669 case ST_DATA_DECL:
2670 case ST_PROCEDURE:
2671 accept_statement (st);
2672 seen_component = 1;
2673 break;
2675 case ST_FINAL:
2676 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2677 break;
2679 case ST_END_TYPE:
2680 endType:
2681 compiling_type = 0;
2683 if (!seen_component)
2684 gfc_notify_std (GFC_STD_F2003, "Derived type "
2685 "definition at %C without components");
2687 accept_statement (ST_END_TYPE);
2688 break;
2690 case ST_PRIVATE:
2691 if (!gfc_find_state (COMP_MODULE))
2693 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2694 "a MODULE");
2695 break;
2698 if (seen_component)
2700 gfc_error ("PRIVATE statement at %C must precede "
2701 "structure components");
2702 break;
2705 if (seen_private)
2706 gfc_error ("Duplicate PRIVATE statement at %C");
2708 s.sym->component_access = ACCESS_PRIVATE;
2710 accept_statement (ST_PRIVATE);
2711 seen_private = 1;
2712 break;
2714 case ST_SEQUENCE:
2715 if (seen_component)
2717 gfc_error ("SEQUENCE statement at %C must precede "
2718 "structure components");
2719 break;
2722 if (gfc_current_block ()->attr.sequence)
2723 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
2724 "TYPE statement");
2726 if (seen_sequence)
2728 gfc_error ("Duplicate SEQUENCE statement at %C");
2731 seen_sequence = 1;
2732 gfc_add_sequence (&gfc_current_block ()->attr,
2733 gfc_current_block ()->name, NULL);
2734 break;
2736 case ST_CONTAINS:
2737 gfc_notify_std (GFC_STD_F2003,
2738 "CONTAINS block in derived type"
2739 " definition at %C");
2741 accept_statement (ST_CONTAINS);
2742 parse_derived_contains ();
2743 goto endType;
2745 default:
2746 unexpected_statement (st);
2747 break;
2751 /* need to verify that all fields of the derived type are
2752 * interoperable with C if the type is declared to be bind(c)
2754 sym = gfc_current_block ();
2755 for (c = sym->components; c; c = c->next)
2757 bool coarray, lock_type, allocatable, pointer;
2758 coarray = lock_type = allocatable = pointer = false;
2760 /* Look for allocatable components. */
2761 if (c->attr.allocatable
2762 || (c->ts.type == BT_CLASS && c->attr.class_ok
2763 && CLASS_DATA (c)->attr.allocatable)
2764 || (c->ts.type == BT_DERIVED && !c->attr.pointer
2765 && c->ts.u.derived->attr.alloc_comp))
2767 allocatable = true;
2768 sym->attr.alloc_comp = 1;
2771 /* Look for pointer components. */
2772 if (c->attr.pointer
2773 || (c->ts.type == BT_CLASS && c->attr.class_ok
2774 && CLASS_DATA (c)->attr.class_pointer)
2775 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2777 pointer = true;
2778 sym->attr.pointer_comp = 1;
2781 /* Look for procedure pointer components. */
2782 if (c->attr.proc_pointer
2783 || (c->ts.type == BT_DERIVED
2784 && c->ts.u.derived->attr.proc_pointer_comp))
2785 sym->attr.proc_pointer_comp = 1;
2787 /* Looking for coarray components. */
2788 if (c->attr.codimension
2789 || (c->ts.type == BT_CLASS && c->attr.class_ok
2790 && CLASS_DATA (c)->attr.codimension))
2792 coarray = true;
2793 sym->attr.coarray_comp = 1;
2796 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
2797 && !c->attr.pointer)
2799 coarray = true;
2800 sym->attr.coarray_comp = 1;
2803 /* Looking for lock_type components. */
2804 if ((c->ts.type == BT_DERIVED
2805 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2806 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2807 || (c->ts.type == BT_CLASS && c->attr.class_ok
2808 && CLASS_DATA (c)->ts.u.derived->from_intmod
2809 == INTMOD_ISO_FORTRAN_ENV
2810 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2811 == ISOFORTRAN_LOCK_TYPE)
2812 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
2813 && !allocatable && !pointer))
2815 lock_type = 1;
2816 lock_comp = c;
2817 sym->attr.lock_comp = 1;
2820 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2821 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2822 unless there are nondirect [allocatable or pointer] components
2823 involved (cf. 1.3.33.1 and 1.3.33.3). */
2825 if (pointer && !coarray && lock_type)
2826 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2827 "codimension or be a subcomponent of a coarray, "
2828 "which is not possible as the component has the "
2829 "pointer attribute", c->name, &c->loc);
2830 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2831 && c->ts.u.derived->attr.lock_comp)
2832 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2833 "of type LOCK_TYPE, which must have a codimension or be a "
2834 "subcomponent of a coarray", c->name, &c->loc);
2836 if (lock_type && allocatable && !coarray)
2837 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2838 "a codimension", c->name, &c->loc);
2839 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
2840 && c->ts.u.derived->attr.lock_comp)
2841 gfc_error ("Allocatable component %s at %L must have a codimension as "
2842 "it has a noncoarray subcomponent of type LOCK_TYPE",
2843 c->name, &c->loc);
2845 if (sym->attr.coarray_comp && !coarray && lock_type)
2846 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2847 "subcomponent of type LOCK_TYPE must have a codimension or "
2848 "be a subcomponent of a coarray. (Variables of type %s may "
2849 "not have a codimension as already a coarray "
2850 "subcomponent exists)", c->name, &c->loc, sym->name);
2852 if (sym->attr.lock_comp && coarray && !lock_type)
2853 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2854 "subcomponent of type LOCK_TYPE must have a codimension or "
2855 "be a subcomponent of a coarray. (Variables of type %s may "
2856 "not have a codimension as %s at %L has a codimension or a "
2857 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
2858 sym->name, c->name, &c->loc);
2860 /* Look for private components. */
2861 if (sym->component_access == ACCESS_PRIVATE
2862 || c->attr.access == ACCESS_PRIVATE
2863 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
2864 sym->attr.private_comp = 1;
2867 if (!seen_component)
2868 sym->attr.zero_comp = 1;
2870 pop_state ();
2874 /* Parse an ENUM. */
2876 static void
2877 parse_enum (void)
2879 gfc_statement st;
2880 int compiling_enum;
2881 gfc_state_data s;
2882 int seen_enumerator = 0;
2884 push_state (&s, COMP_ENUM, gfc_new_block);
2886 compiling_enum = 1;
2888 while (compiling_enum)
2890 st = next_statement ();
2891 switch (st)
2893 case ST_NONE:
2894 unexpected_eof ();
2895 break;
2897 case ST_ENUMERATOR:
2898 seen_enumerator = 1;
2899 accept_statement (st);
2900 break;
2902 case ST_END_ENUM:
2903 compiling_enum = 0;
2904 if (!seen_enumerator)
2905 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2906 accept_statement (st);
2907 break;
2909 default:
2910 gfc_free_enum_history ();
2911 unexpected_statement (st);
2912 break;
2915 pop_state ();
2919 /* Parse an interface. We must be able to deal with the possibility
2920 of recursive interfaces. The parse_spec() subroutine is mutually
2921 recursive with parse_interface(). */
2923 static gfc_statement parse_spec (gfc_statement);
2925 static void
2926 parse_interface (void)
2928 gfc_compile_state new_state = COMP_NONE, current_state;
2929 gfc_symbol *prog_unit, *sym;
2930 gfc_interface_info save;
2931 gfc_state_data s1, s2;
2932 gfc_statement st;
2934 accept_statement (ST_INTERFACE);
2936 current_interface.ns = gfc_current_ns;
2937 save = current_interface;
2939 sym = (current_interface.type == INTERFACE_GENERIC
2940 || current_interface.type == INTERFACE_USER_OP)
2941 ? gfc_new_block : NULL;
2943 push_state (&s1, COMP_INTERFACE, sym);
2944 current_state = COMP_NONE;
2946 loop:
2947 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
2949 st = next_statement ();
2950 switch (st)
2952 case ST_NONE:
2953 unexpected_eof ();
2955 case ST_SUBROUTINE:
2956 case ST_FUNCTION:
2957 if (st == ST_SUBROUTINE)
2958 new_state = COMP_SUBROUTINE;
2959 else if (st == ST_FUNCTION)
2960 new_state = COMP_FUNCTION;
2961 if (gfc_new_block->attr.pointer)
2963 gfc_new_block->attr.pointer = 0;
2964 gfc_new_block->attr.proc_pointer = 1;
2966 if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
2967 gfc_new_block->formal, NULL))
2969 reject_statement ();
2970 gfc_free_namespace (gfc_current_ns);
2971 goto loop;
2973 /* F2008 C1210 forbids the IMPORT statement in module procedure
2974 interface bodies and the flag is set to import symbols. */
2975 if (gfc_new_block->attr.module_procedure)
2976 gfc_current_ns->has_import_set = 1;
2977 break;
2979 case ST_PROCEDURE:
2980 case ST_MODULE_PROC: /* The module procedure matcher makes
2981 sure the context is correct. */
2982 accept_statement (st);
2983 gfc_free_namespace (gfc_current_ns);
2984 goto loop;
2986 case ST_END_INTERFACE:
2987 gfc_free_namespace (gfc_current_ns);
2988 gfc_current_ns = current_interface.ns;
2989 goto done;
2991 default:
2992 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2993 gfc_ascii_statement (st));
2994 reject_statement ();
2995 gfc_free_namespace (gfc_current_ns);
2996 goto loop;
3000 /* Make sure that the generic name has the right attribute. */
3001 if (current_interface.type == INTERFACE_GENERIC
3002 && current_state == COMP_NONE)
3004 if (new_state == COMP_FUNCTION && sym)
3005 gfc_add_function (&sym->attr, sym->name, NULL);
3006 else if (new_state == COMP_SUBROUTINE && sym)
3007 gfc_add_subroutine (&sym->attr, sym->name, NULL);
3009 current_state = new_state;
3012 if (current_interface.type == INTERFACE_ABSTRACT)
3014 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
3015 if (gfc_is_intrinsic_typename (gfc_new_block->name))
3016 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
3017 "cannot be the same as an intrinsic type",
3018 gfc_new_block->name);
3021 push_state (&s2, new_state, gfc_new_block);
3022 accept_statement (st);
3023 prog_unit = gfc_new_block;
3024 prog_unit->formal_ns = gfc_current_ns;
3025 if (prog_unit == prog_unit->formal_ns->proc_name
3026 && prog_unit->ns != prog_unit->formal_ns)
3027 prog_unit->refs++;
3029 decl:
3030 /* Read data declaration statements. */
3031 st = parse_spec (ST_NONE);
3032 in_specification_block = true;
3034 /* Since the interface block does not permit an IMPLICIT statement,
3035 the default type for the function or the result must be taken
3036 from the formal namespace. */
3037 if (new_state == COMP_FUNCTION)
3039 if (prog_unit->result == prog_unit
3040 && prog_unit->ts.type == BT_UNKNOWN)
3041 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
3042 else if (prog_unit->result != prog_unit
3043 && prog_unit->result->ts.type == BT_UNKNOWN)
3044 gfc_set_default_type (prog_unit->result, 1,
3045 prog_unit->formal_ns);
3048 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
3050 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3051 gfc_ascii_statement (st));
3052 reject_statement ();
3053 goto decl;
3056 /* Add EXTERNAL attribute to function or subroutine. */
3057 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
3058 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
3060 current_interface = save;
3061 gfc_add_interface (prog_unit);
3062 pop_state ();
3064 if (current_interface.ns
3065 && current_interface.ns->proc_name
3066 && strcmp (current_interface.ns->proc_name->name,
3067 prog_unit->name) == 0)
3068 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3069 "enclosing procedure", prog_unit->name,
3070 &current_interface.ns->proc_name->declared_at);
3072 goto loop;
3074 done:
3075 pop_state ();
3079 /* Associate function characteristics by going back to the function
3080 declaration and rematching the prefix. */
3082 static match
3083 match_deferred_characteristics (gfc_typespec * ts)
3085 locus loc;
3086 match m = MATCH_ERROR;
3087 char name[GFC_MAX_SYMBOL_LEN + 1];
3089 loc = gfc_current_locus;
3091 gfc_current_locus = gfc_current_block ()->declared_at;
3093 gfc_clear_error ();
3094 gfc_buffer_error (true);
3095 m = gfc_match_prefix (ts);
3096 gfc_buffer_error (false);
3098 if (ts->type == BT_DERIVED)
3100 ts->kind = 0;
3102 if (!ts->u.derived)
3103 m = MATCH_ERROR;
3106 /* Only permit one go at the characteristic association. */
3107 if (ts->kind == -1)
3108 ts->kind = 0;
3110 /* Set the function locus correctly. If we have not found the
3111 function name, there is an error. */
3112 if (m == MATCH_YES
3113 && gfc_match ("function% %n", name) == MATCH_YES
3114 && strcmp (name, gfc_current_block ()->name) == 0)
3116 gfc_current_block ()->declared_at = gfc_current_locus;
3117 gfc_commit_symbols ();
3119 else
3121 gfc_error_check ();
3122 gfc_undo_symbols ();
3125 gfc_current_locus =loc;
3126 return m;
3130 /* Check specification-expressions in the function result of the currently
3131 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3132 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3133 scope are not yet parsed so this has to be delayed up to parse_spec. */
3135 static void
3136 check_function_result_typed (void)
3138 gfc_typespec ts;
3140 gcc_assert (gfc_current_state () == COMP_FUNCTION);
3142 if (!gfc_current_ns->proc_name->result) return;
3144 ts = gfc_current_ns->proc_name->result->ts;
3146 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3147 /* TODO: Extend when KIND type parameters are implemented. */
3148 if (ts.type == BT_CHARACTER && ts.u.cl && ts.u.cl->length)
3149 gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true);
3153 /* Parse a set of specification statements. Returns the statement
3154 that doesn't fit. */
3156 static gfc_statement
3157 parse_spec (gfc_statement st)
3159 st_state ss;
3160 bool function_result_typed = false;
3161 bool bad_characteristic = false;
3162 gfc_typespec *ts;
3164 in_specification_block = true;
3166 verify_st_order (&ss, ST_NONE, false);
3167 if (st == ST_NONE)
3168 st = next_statement ();
3170 /* If we are not inside a function or don't have a result specified so far,
3171 do nothing special about it. */
3172 if (gfc_current_state () != COMP_FUNCTION)
3173 function_result_typed = true;
3174 else
3176 gfc_symbol* proc = gfc_current_ns->proc_name;
3177 gcc_assert (proc);
3179 if (proc->result->ts.type == BT_UNKNOWN)
3180 function_result_typed = true;
3183 loop:
3185 /* If we're inside a BLOCK construct, some statements are disallowed.
3186 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3187 or VALUE are also disallowed, but they don't have a particular ST_*
3188 key so we have to check for them individually in their matcher routine. */
3189 if (gfc_current_state () == COMP_BLOCK)
3190 switch (st)
3192 case ST_IMPLICIT:
3193 case ST_IMPLICIT_NONE:
3194 case ST_NAMELIST:
3195 case ST_COMMON:
3196 case ST_EQUIVALENCE:
3197 case ST_STATEMENT_FUNCTION:
3198 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3199 gfc_ascii_statement (st));
3200 reject_statement ();
3201 break;
3203 default:
3204 break;
3206 else if (gfc_current_state () == COMP_BLOCK_DATA)
3207 /* Fortran 2008, C1116. */
3208 switch (st)
3210 case ST_DATA_DECL:
3211 case ST_COMMON:
3212 case ST_DATA:
3213 case ST_TYPE:
3214 case ST_END_BLOCK_DATA:
3215 case ST_ATTR_DECL:
3216 case ST_EQUIVALENCE:
3217 case ST_PARAMETER:
3218 case ST_IMPLICIT:
3219 case ST_IMPLICIT_NONE:
3220 case ST_DERIVED_DECL:
3221 case ST_USE:
3222 break;
3224 case ST_NONE:
3225 break;
3227 default:
3228 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3229 gfc_ascii_statement (st));
3230 reject_statement ();
3231 break;
3234 /* If we find a statement that can not be followed by an IMPLICIT statement
3235 (and thus we can expect to see none any further), type the function result
3236 if it has not yet been typed. Be careful not to give the END statement
3237 to verify_st_order! */
3238 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
3240 bool verify_now = false;
3242 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
3243 verify_now = true;
3244 else
3246 st_state dummyss;
3247 verify_st_order (&dummyss, ST_NONE, false);
3248 verify_st_order (&dummyss, st, false);
3250 if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
3251 verify_now = true;
3254 if (verify_now)
3256 check_function_result_typed ();
3257 function_result_typed = true;
3261 switch (st)
3263 case ST_NONE:
3264 unexpected_eof ();
3266 case ST_IMPLICIT_NONE:
3267 case ST_IMPLICIT:
3268 if (!function_result_typed)
3270 check_function_result_typed ();
3271 function_result_typed = true;
3273 goto declSt;
3275 case ST_FORMAT:
3276 case ST_ENTRY:
3277 case ST_DATA: /* Not allowed in interfaces */
3278 if (gfc_current_state () == COMP_INTERFACE)
3279 break;
3281 /* Fall through */
3283 case ST_USE:
3284 case ST_IMPORT:
3285 case ST_PARAMETER:
3286 case ST_PUBLIC:
3287 case ST_PRIVATE:
3288 case ST_DERIVED_DECL:
3289 case_decl:
3290 declSt:
3291 if (!verify_st_order (&ss, st, false))
3293 reject_statement ();
3294 st = next_statement ();
3295 goto loop;
3298 switch (st)
3300 case ST_INTERFACE:
3301 parse_interface ();
3302 break;
3304 case ST_DERIVED_DECL:
3305 parse_derived ();
3306 break;
3308 case ST_PUBLIC:
3309 case ST_PRIVATE:
3310 if (gfc_current_state () != COMP_MODULE)
3312 gfc_error ("%s statement must appear in a MODULE",
3313 gfc_ascii_statement (st));
3314 reject_statement ();
3315 break;
3318 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
3320 gfc_error ("%s statement at %C follows another accessibility "
3321 "specification", gfc_ascii_statement (st));
3322 reject_statement ();
3323 break;
3326 gfc_current_ns->default_access = (st == ST_PUBLIC)
3327 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3329 break;
3331 case ST_STATEMENT_FUNCTION:
3332 if (gfc_current_state () == COMP_MODULE
3333 || gfc_current_state () == COMP_SUBMODULE)
3335 unexpected_statement (st);
3336 break;
3339 default:
3340 break;
3343 accept_statement (st);
3344 st = next_statement ();
3345 goto loop;
3347 case ST_ENUM:
3348 accept_statement (st);
3349 parse_enum();
3350 st = next_statement ();
3351 goto loop;
3353 case ST_GET_FCN_CHARACTERISTICS:
3354 /* This statement triggers the association of a function's result
3355 characteristics. */
3356 ts = &gfc_current_block ()->result->ts;
3357 if (match_deferred_characteristics (ts) != MATCH_YES)
3358 bad_characteristic = true;
3360 st = next_statement ();
3361 goto loop;
3363 default:
3364 break;
3367 /* If match_deferred_characteristics failed, then there is an error. */
3368 if (bad_characteristic)
3370 ts = &gfc_current_block ()->result->ts;
3371 if (ts->type != BT_DERIVED)
3372 gfc_error ("Bad kind expression for function %qs at %L",
3373 gfc_current_block ()->name,
3374 &gfc_current_block ()->declared_at);
3375 else
3376 gfc_error ("The type for function %qs at %L is not accessible",
3377 gfc_current_block ()->name,
3378 &gfc_current_block ()->declared_at);
3380 gfc_current_block ()->ts.kind = 0;
3381 /* Keep the derived type; if it's bad, it will be discovered later. */
3382 if (!(ts->type == BT_DERIVED && ts->u.derived))
3383 ts->type = BT_UNKNOWN;
3386 in_specification_block = false;
3388 return st;
3392 /* Parse a WHERE block, (not a simple WHERE statement). */
3394 static void
3395 parse_where_block (void)
3397 int seen_empty_else;
3398 gfc_code *top, *d;
3399 gfc_state_data s;
3400 gfc_statement st;
3402 accept_statement (ST_WHERE_BLOCK);
3403 top = gfc_state_stack->tail;
3405 push_state (&s, COMP_WHERE, gfc_new_block);
3407 d = add_statement ();
3408 d->expr1 = top->expr1;
3409 d->op = EXEC_WHERE;
3411 top->expr1 = NULL;
3412 top->block = d;
3414 seen_empty_else = 0;
3418 st = next_statement ();
3419 switch (st)
3421 case ST_NONE:
3422 unexpected_eof ();
3424 case ST_WHERE_BLOCK:
3425 parse_where_block ();
3426 break;
3428 case ST_ASSIGNMENT:
3429 case ST_WHERE:
3430 accept_statement (st);
3431 break;
3433 case ST_ELSEWHERE:
3434 if (seen_empty_else)
3436 gfc_error ("ELSEWHERE statement at %C follows previous "
3437 "unmasked ELSEWHERE");
3438 reject_statement ();
3439 break;
3442 if (new_st.expr1 == NULL)
3443 seen_empty_else = 1;
3445 d = new_level (gfc_state_stack->head);
3446 d->op = EXEC_WHERE;
3447 d->expr1 = new_st.expr1;
3449 accept_statement (st);
3451 break;
3453 case ST_END_WHERE:
3454 accept_statement (st);
3455 break;
3457 default:
3458 gfc_error ("Unexpected %s statement in WHERE block at %C",
3459 gfc_ascii_statement (st));
3460 reject_statement ();
3461 break;
3464 while (st != ST_END_WHERE);
3466 pop_state ();
3470 /* Parse a FORALL block (not a simple FORALL statement). */
3472 static void
3473 parse_forall_block (void)
3475 gfc_code *top, *d;
3476 gfc_state_data s;
3477 gfc_statement st;
3479 accept_statement (ST_FORALL_BLOCK);
3480 top = gfc_state_stack->tail;
3482 push_state (&s, COMP_FORALL, gfc_new_block);
3484 d = add_statement ();
3485 d->op = EXEC_FORALL;
3486 top->block = d;
3490 st = next_statement ();
3491 switch (st)
3494 case ST_ASSIGNMENT:
3495 case ST_POINTER_ASSIGNMENT:
3496 case ST_WHERE:
3497 case ST_FORALL:
3498 accept_statement (st);
3499 break;
3501 case ST_WHERE_BLOCK:
3502 parse_where_block ();
3503 break;
3505 case ST_FORALL_BLOCK:
3506 parse_forall_block ();
3507 break;
3509 case ST_END_FORALL:
3510 accept_statement (st);
3511 break;
3513 case ST_NONE:
3514 unexpected_eof ();
3516 default:
3517 gfc_error ("Unexpected %s statement in FORALL block at %C",
3518 gfc_ascii_statement (st));
3520 reject_statement ();
3521 break;
3524 while (st != ST_END_FORALL);
3526 pop_state ();
3530 static gfc_statement parse_executable (gfc_statement);
3532 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
3534 static void
3535 parse_if_block (void)
3537 gfc_code *top, *d;
3538 gfc_statement st;
3539 locus else_locus;
3540 gfc_state_data s;
3541 int seen_else;
3543 seen_else = 0;
3544 accept_statement (ST_IF_BLOCK);
3546 top = gfc_state_stack->tail;
3547 push_state (&s, COMP_IF, gfc_new_block);
3549 new_st.op = EXEC_IF;
3550 d = add_statement ();
3552 d->expr1 = top->expr1;
3553 top->expr1 = NULL;
3554 top->block = d;
3558 st = parse_executable (ST_NONE);
3560 switch (st)
3562 case ST_NONE:
3563 unexpected_eof ();
3565 case ST_ELSEIF:
3566 if (seen_else)
3568 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
3569 "statement at %L", &else_locus);
3571 reject_statement ();
3572 break;
3575 d = new_level (gfc_state_stack->head);
3576 d->op = EXEC_IF;
3577 d->expr1 = new_st.expr1;
3579 accept_statement (st);
3581 break;
3583 case ST_ELSE:
3584 if (seen_else)
3586 gfc_error ("Duplicate ELSE statements at %L and %C",
3587 &else_locus);
3588 reject_statement ();
3589 break;
3592 seen_else = 1;
3593 else_locus = gfc_current_locus;
3595 d = new_level (gfc_state_stack->head);
3596 d->op = EXEC_IF;
3598 accept_statement (st);
3600 break;
3602 case ST_ENDIF:
3603 break;
3605 default:
3606 unexpected_statement (st);
3607 break;
3610 while (st != ST_ENDIF);
3612 pop_state ();
3613 accept_statement (st);
3617 /* Parse a SELECT block. */
3619 static void
3620 parse_select_block (void)
3622 gfc_statement st;
3623 gfc_code *cp;
3624 gfc_state_data s;
3626 accept_statement (ST_SELECT_CASE);
3628 cp = gfc_state_stack->tail;
3629 push_state (&s, COMP_SELECT, gfc_new_block);
3631 /* Make sure that the next statement is a CASE or END SELECT. */
3632 for (;;)
3634 st = next_statement ();
3635 if (st == ST_NONE)
3636 unexpected_eof ();
3637 if (st == ST_END_SELECT)
3639 /* Empty SELECT CASE is OK. */
3640 accept_statement (st);
3641 pop_state ();
3642 return;
3644 if (st == ST_CASE)
3645 break;
3647 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
3648 "CASE at %C");
3650 reject_statement ();
3653 /* At this point, we're got a nonempty select block. */
3654 cp = new_level (cp);
3655 *cp = new_st;
3657 accept_statement (st);
3661 st = parse_executable (ST_NONE);
3662 switch (st)
3664 case ST_NONE:
3665 unexpected_eof ();
3667 case ST_CASE:
3668 cp = new_level (gfc_state_stack->head);
3669 *cp = new_st;
3670 gfc_clear_new_st ();
3672 accept_statement (st);
3673 /* Fall through */
3675 case ST_END_SELECT:
3676 break;
3678 /* Can't have an executable statement because of
3679 parse_executable(). */
3680 default:
3681 unexpected_statement (st);
3682 break;
3685 while (st != ST_END_SELECT);
3687 pop_state ();
3688 accept_statement (st);
3692 /* Pop the current selector from the SELECT TYPE stack. */
3694 static void
3695 select_type_pop (void)
3697 gfc_select_type_stack *old = select_type_stack;
3698 select_type_stack = old->prev;
3699 free (old);
3703 /* Parse a SELECT TYPE construct (F03:R821). */
3705 static void
3706 parse_select_type_block (void)
3708 gfc_statement st;
3709 gfc_code *cp;
3710 gfc_state_data s;
3712 accept_statement (ST_SELECT_TYPE);
3714 cp = gfc_state_stack->tail;
3715 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
3717 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
3718 or END SELECT. */
3719 for (;;)
3721 st = next_statement ();
3722 if (st == ST_NONE)
3723 unexpected_eof ();
3724 if (st == ST_END_SELECT)
3725 /* Empty SELECT CASE is OK. */
3726 goto done;
3727 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
3728 break;
3730 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
3731 "following SELECT TYPE at %C");
3733 reject_statement ();
3736 /* At this point, we're got a nonempty select block. */
3737 cp = new_level (cp);
3738 *cp = new_st;
3740 accept_statement (st);
3744 st = parse_executable (ST_NONE);
3745 switch (st)
3747 case ST_NONE:
3748 unexpected_eof ();
3750 case ST_TYPE_IS:
3751 case ST_CLASS_IS:
3752 cp = new_level (gfc_state_stack->head);
3753 *cp = new_st;
3754 gfc_clear_new_st ();
3756 accept_statement (st);
3757 /* Fall through */
3759 case ST_END_SELECT:
3760 break;
3762 /* Can't have an executable statement because of
3763 parse_executable(). */
3764 default:
3765 unexpected_statement (st);
3766 break;
3769 while (st != ST_END_SELECT);
3771 done:
3772 pop_state ();
3773 accept_statement (st);
3774 gfc_current_ns = gfc_current_ns->parent;
3775 select_type_pop ();
3779 /* Given a symbol, make sure it is not an iteration variable for a DO
3780 statement. This subroutine is called when the symbol is seen in a
3781 context that causes it to become redefined. If the symbol is an
3782 iterator, we generate an error message and return nonzero. */
3785 gfc_check_do_variable (gfc_symtree *st)
3787 gfc_state_data *s;
3789 for (s=gfc_state_stack; s; s = s->previous)
3790 if (s->do_variable == st)
3792 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
3793 "loop beginning at %L", st->name, &s->head->loc);
3794 return 1;
3797 return 0;
3801 /* Checks to see if the current statement label closes an enddo.
3802 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3803 an error) if it incorrectly closes an ENDDO. */
3805 static int
3806 check_do_closure (void)
3808 gfc_state_data *p;
3810 if (gfc_statement_label == NULL)
3811 return 0;
3813 for (p = gfc_state_stack; p; p = p->previous)
3814 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
3815 break;
3817 if (p == NULL)
3818 return 0; /* No loops to close */
3820 if (p->ext.end_do_label == gfc_statement_label)
3822 if (p == gfc_state_stack)
3823 return 1;
3825 gfc_error ("End of nonblock DO statement at %C is within another block");
3826 return 2;
3829 /* At this point, the label doesn't terminate the innermost loop.
3830 Make sure it doesn't terminate another one. */
3831 for (; p; p = p->previous)
3832 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
3833 && p->ext.end_do_label == gfc_statement_label)
3835 gfc_error ("End of nonblock DO statement at %C is interwoven "
3836 "with another DO loop");
3837 return 2;
3840 return 0;
3844 /* Parse a series of contained program units. */
3846 static void parse_progunit (gfc_statement);
3849 /* Parse a CRITICAL block. */
3851 static void
3852 parse_critical_block (void)
3854 gfc_code *top, *d;
3855 gfc_state_data s, *sd;
3856 gfc_statement st;
3858 for (sd = gfc_state_stack; sd; sd = sd->previous)
3859 if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
3860 gfc_error_now (is_oacc (sd)
3861 ? "CRITICAL block inside of OpenACC region at %C"
3862 : "CRITICAL block inside of OpenMP region at %C");
3864 s.ext.end_do_label = new_st.label1;
3866 accept_statement (ST_CRITICAL);
3867 top = gfc_state_stack->tail;
3869 push_state (&s, COMP_CRITICAL, gfc_new_block);
3871 d = add_statement ();
3872 d->op = EXEC_CRITICAL;
3873 top->block = d;
3877 st = parse_executable (ST_NONE);
3879 switch (st)
3881 case ST_NONE:
3882 unexpected_eof ();
3883 break;
3885 case ST_END_CRITICAL:
3886 if (s.ext.end_do_label != NULL
3887 && s.ext.end_do_label != gfc_statement_label)
3888 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3889 "match CRITICAL label");
3891 if (gfc_statement_label != NULL)
3893 new_st.op = EXEC_NOP;
3894 add_statement ();
3896 break;
3898 default:
3899 unexpected_statement (st);
3900 break;
3903 while (st != ST_END_CRITICAL);
3905 pop_state ();
3906 accept_statement (st);
3910 /* Set up the local namespace for a BLOCK construct. */
3912 gfc_namespace*
3913 gfc_build_block_ns (gfc_namespace *parent_ns)
3915 gfc_namespace* my_ns;
3916 static int numblock = 1;
3918 my_ns = gfc_get_namespace (parent_ns, 1);
3919 my_ns->construct_entities = 1;
3921 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3922 code generation (so it must not be NULL).
3923 We set its recursive argument if our container procedure is recursive, so
3924 that local variables are accordingly placed on the stack when it
3925 will be necessary. */
3926 if (gfc_new_block)
3927 my_ns->proc_name = gfc_new_block;
3928 else
3930 bool t;
3931 char buffer[20]; /* Enough to hold "block@2147483648\n". */
3933 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
3934 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
3935 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
3936 my_ns->proc_name->name, NULL);
3937 gcc_assert (t);
3938 gfc_commit_symbol (my_ns->proc_name);
3941 if (parent_ns->proc_name)
3942 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
3944 return my_ns;
3948 /* Parse a BLOCK construct. */
3950 static void
3951 parse_block_construct (void)
3953 gfc_namespace* my_ns;
3954 gfc_namespace* my_parent;
3955 gfc_state_data s;
3957 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
3959 my_ns = gfc_build_block_ns (gfc_current_ns);
3961 new_st.op = EXEC_BLOCK;
3962 new_st.ext.block.ns = my_ns;
3963 new_st.ext.block.assoc = NULL;
3964 accept_statement (ST_BLOCK);
3966 push_state (&s, COMP_BLOCK, my_ns->proc_name);
3967 gfc_current_ns = my_ns;
3968 my_parent = my_ns->parent;
3970 parse_progunit (ST_NONE);
3972 /* Don't depend on the value of gfc_current_ns; it might have been
3973 reset if the block had errors and was cleaned up. */
3974 gfc_current_ns = my_parent;
3976 pop_state ();
3980 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3981 behind the scenes with compiler-generated variables. */
3983 static void
3984 parse_associate (void)
3986 gfc_namespace* my_ns;
3987 gfc_state_data s;
3988 gfc_statement st;
3989 gfc_association_list* a;
3991 gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
3993 my_ns = gfc_build_block_ns (gfc_current_ns);
3995 new_st.op = EXEC_BLOCK;
3996 new_st.ext.block.ns = my_ns;
3997 gcc_assert (new_st.ext.block.assoc);
3999 /* Add all associate-names as BLOCK variables. Creating them is enough
4000 for now, they'll get their values during trans-* phase. */
4001 gfc_current_ns = my_ns;
4002 for (a = new_st.ext.block.assoc; a; a = a->next)
4004 gfc_symbol* sym;
4005 gfc_ref *ref;
4006 gfc_array_ref *array_ref;
4008 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
4009 gcc_unreachable ();
4011 sym = a->st->n.sym;
4012 sym->attr.flavor = FL_VARIABLE;
4013 sym->assoc = a;
4014 sym->declared_at = a->where;
4015 gfc_set_sym_referenced (sym);
4017 /* Initialize the typespec. It is not available in all cases,
4018 however, as it may only be set on the target during resolution.
4019 Still, sometimes it helps to have it right now -- especially
4020 for parsing component references on the associate-name
4021 in case of association to a derived-type. */
4022 sym->ts = a->target->ts;
4024 /* Check if the target expression is array valued. This can not always
4025 be done by looking at target.rank, because that might not have been
4026 set yet. Therefore traverse the chain of refs, looking for the last
4027 array ref and evaluate that. */
4028 array_ref = NULL;
4029 for (ref = a->target->ref; ref; ref = ref->next)
4030 if (ref->type == REF_ARRAY)
4031 array_ref = &ref->u.ar;
4032 if (array_ref || a->target->rank)
4034 gfc_array_spec *as;
4035 int dim, rank = 0;
4036 if (array_ref)
4038 /* Count the dimension, that have a non-scalar extend. */
4039 for (dim = 0; dim < array_ref->dimen; ++dim)
4040 if (array_ref->dimen_type[dim] != DIMEN_ELEMENT
4041 && !(array_ref->dimen_type[dim] == DIMEN_UNKNOWN
4042 && array_ref->end[dim] == NULL
4043 && array_ref->start[dim] != NULL))
4044 ++rank;
4046 else
4047 rank = a->target->rank;
4048 /* When the rank is greater than zero then sym will be an array. */
4049 if (sym->ts.type == BT_CLASS)
4051 if ((!CLASS_DATA (sym)->as && rank != 0)
4052 || (CLASS_DATA (sym)->as
4053 && CLASS_DATA (sym)->as->rank != rank))
4055 /* Don't just (re-)set the attr and as in the sym.ts,
4056 because this modifies the target's attr and as. Copy the
4057 data and do a build_class_symbol. */
4058 symbol_attribute attr = CLASS_DATA (a->target)->attr;
4059 int corank = gfc_get_corank (a->target);
4060 gfc_typespec type;
4062 if (rank || corank)
4064 as = gfc_get_array_spec ();
4065 as->type = AS_DEFERRED;
4066 as->rank = rank;
4067 as->corank = corank;
4068 attr.dimension = rank ? 1 : 0;
4069 attr.codimension = corank ? 1 : 0;
4071 else
4073 as = NULL;
4074 attr.dimension = attr.codimension = 0;
4076 attr.class_ok = 0;
4077 type = CLASS_DATA (sym)->ts;
4078 if (!gfc_build_class_symbol (&type,
4079 &attr, &as))
4080 gcc_unreachable ();
4081 sym->ts = type;
4082 sym->ts.type = BT_CLASS;
4083 sym->attr.class_ok = 1;
4085 else
4086 sym->attr.class_ok = 1;
4088 else if ((!sym->as && rank != 0)
4089 || (sym->as && sym->as->rank != rank))
4091 as = gfc_get_array_spec ();
4092 as->type = AS_DEFERRED;
4093 as->rank = rank;
4094 as->corank = gfc_get_corank (a->target);
4095 sym->as = as;
4096 sym->attr.dimension = 1;
4097 if (as->corank)
4098 sym->attr.codimension = 1;
4103 accept_statement (ST_ASSOCIATE);
4104 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
4106 loop:
4107 st = parse_executable (ST_NONE);
4108 switch (st)
4110 case ST_NONE:
4111 unexpected_eof ();
4113 case_end:
4114 accept_statement (st);
4115 my_ns->code = gfc_state_stack->head;
4116 break;
4118 default:
4119 unexpected_statement (st);
4120 goto loop;
4123 gfc_current_ns = gfc_current_ns->parent;
4124 pop_state ();
4128 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4129 handled inside of parse_executable(), because they aren't really
4130 loop statements. */
4132 static void
4133 parse_do_block (void)
4135 gfc_statement st;
4136 gfc_code *top;
4137 gfc_state_data s;
4138 gfc_symtree *stree;
4139 gfc_exec_op do_op;
4141 do_op = new_st.op;
4142 s.ext.end_do_label = new_st.label1;
4144 if (new_st.ext.iterator != NULL)
4145 stree = new_st.ext.iterator->var->symtree;
4146 else
4147 stree = NULL;
4149 accept_statement (ST_DO);
4151 top = gfc_state_stack->tail;
4152 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
4153 gfc_new_block);
4155 s.do_variable = stree;
4157 top->block = new_level (top);
4158 top->block->op = EXEC_DO;
4160 loop:
4161 st = parse_executable (ST_NONE);
4163 switch (st)
4165 case ST_NONE:
4166 unexpected_eof ();
4168 case ST_ENDDO:
4169 if (s.ext.end_do_label != NULL
4170 && s.ext.end_do_label != gfc_statement_label)
4171 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4172 "DO label");
4174 if (gfc_statement_label != NULL)
4176 new_st.op = EXEC_NOP;
4177 add_statement ();
4179 break;
4181 case ST_IMPLIED_ENDDO:
4182 /* If the do-stmt of this DO construct has a do-construct-name,
4183 the corresponding end-do must be an end-do-stmt (with a matching
4184 name, but in that case we must have seen ST_ENDDO first).
4185 We only complain about this in pedantic mode. */
4186 if (gfc_current_block () != NULL)
4187 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4188 &gfc_current_block()->declared_at);
4190 break;
4192 default:
4193 unexpected_statement (st);
4194 goto loop;
4197 pop_state ();
4198 accept_statement (st);
4202 /* Parse the statements of OpenMP do/parallel do. */
4204 static gfc_statement
4205 parse_omp_do (gfc_statement omp_st)
4207 gfc_statement st;
4208 gfc_code *cp, *np;
4209 gfc_state_data s;
4211 accept_statement (omp_st);
4213 cp = gfc_state_stack->tail;
4214 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4215 np = new_level (cp);
4216 np->op = cp->op;
4217 np->block = NULL;
4219 for (;;)
4221 st = next_statement ();
4222 if (st == ST_NONE)
4223 unexpected_eof ();
4224 else if (st == ST_DO)
4225 break;
4226 else
4227 unexpected_statement (st);
4230 parse_do_block ();
4231 if (gfc_statement_label != NULL
4232 && gfc_state_stack->previous != NULL
4233 && gfc_state_stack->previous->state == COMP_DO
4234 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4236 /* In
4237 DO 100 I=1,10
4238 !$OMP DO
4239 DO J=1,10
4241 100 CONTINUE
4242 there should be no !$OMP END DO. */
4243 pop_state ();
4244 return ST_IMPLIED_ENDDO;
4247 check_do_closure ();
4248 pop_state ();
4250 st = next_statement ();
4251 gfc_statement omp_end_st = ST_OMP_END_DO;
4252 switch (omp_st)
4254 case ST_OMP_DISTRIBUTE: omp_end_st = ST_OMP_END_DISTRIBUTE; break;
4255 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4256 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4257 break;
4258 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4259 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4260 break;
4261 case ST_OMP_DISTRIBUTE_SIMD:
4262 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4263 break;
4264 case ST_OMP_DO: omp_end_st = ST_OMP_END_DO; break;
4265 case ST_OMP_DO_SIMD: omp_end_st = ST_OMP_END_DO_SIMD; break;
4266 case ST_OMP_PARALLEL_DO: omp_end_st = ST_OMP_END_PARALLEL_DO; break;
4267 case ST_OMP_PARALLEL_DO_SIMD:
4268 omp_end_st = ST_OMP_END_PARALLEL_DO_SIMD;
4269 break;
4270 case ST_OMP_SIMD: omp_end_st = ST_OMP_END_SIMD; break;
4271 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4272 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4273 break;
4274 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4275 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4276 break;
4277 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4278 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4279 break;
4280 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4281 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4282 break;
4283 case ST_OMP_TEAMS_DISTRIBUTE:
4284 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4285 break;
4286 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4287 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4288 break;
4289 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4290 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4291 break;
4292 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4293 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4294 break;
4295 default: gcc_unreachable ();
4297 if (st == omp_end_st)
4299 if (new_st.op == EXEC_OMP_END_NOWAIT)
4300 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4301 else
4302 gcc_assert (new_st.op == EXEC_NOP);
4303 gfc_clear_new_st ();
4304 gfc_commit_symbols ();
4305 gfc_warning_check ();
4306 st = next_statement ();
4308 return st;
4312 /* Parse the statements of OpenMP atomic directive. */
4314 static gfc_statement
4315 parse_omp_oacc_atomic (bool omp_p)
4317 gfc_statement st, st_atomic, st_end_atomic;
4318 gfc_code *cp, *np;
4319 gfc_state_data s;
4320 int count;
4322 if (omp_p)
4324 st_atomic = ST_OMP_ATOMIC;
4325 st_end_atomic = ST_OMP_END_ATOMIC;
4327 else
4329 st_atomic = ST_OACC_ATOMIC;
4330 st_end_atomic = ST_OACC_END_ATOMIC;
4332 accept_statement (st_atomic);
4334 cp = gfc_state_stack->tail;
4335 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4336 np = new_level (cp);
4337 np->op = cp->op;
4338 np->block = NULL;
4339 count = 1 + ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4340 == GFC_OMP_ATOMIC_CAPTURE);
4342 while (count)
4344 st = next_statement ();
4345 if (st == ST_NONE)
4346 unexpected_eof ();
4347 else if (st == ST_ASSIGNMENT)
4349 accept_statement (st);
4350 count--;
4352 else
4353 unexpected_statement (st);
4356 pop_state ();
4358 st = next_statement ();
4359 if (st == st_end_atomic)
4361 gfc_clear_new_st ();
4362 gfc_commit_symbols ();
4363 gfc_warning_check ();
4364 st = next_statement ();
4366 else if ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4367 == GFC_OMP_ATOMIC_CAPTURE)
4368 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4369 return st;
4373 /* Parse the statements of an OpenACC structured block. */
4375 static void
4376 parse_oacc_structured_block (gfc_statement acc_st)
4378 gfc_statement st, acc_end_st;
4379 gfc_code *cp, *np;
4380 gfc_state_data s, *sd;
4382 for (sd = gfc_state_stack; sd; sd = sd->previous)
4383 if (sd->state == COMP_CRITICAL)
4384 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4386 accept_statement (acc_st);
4388 cp = gfc_state_stack->tail;
4389 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4390 np = new_level (cp);
4391 np->op = cp->op;
4392 np->block = NULL;
4393 switch (acc_st)
4395 case ST_OACC_PARALLEL:
4396 acc_end_st = ST_OACC_END_PARALLEL;
4397 break;
4398 case ST_OACC_KERNELS:
4399 acc_end_st = ST_OACC_END_KERNELS;
4400 break;
4401 case ST_OACC_DATA:
4402 acc_end_st = ST_OACC_END_DATA;
4403 break;
4404 case ST_OACC_HOST_DATA:
4405 acc_end_st = ST_OACC_END_HOST_DATA;
4406 break;
4407 default:
4408 gcc_unreachable ();
4413 st = parse_executable (ST_NONE);
4414 if (st == ST_NONE)
4415 unexpected_eof ();
4416 else if (st != acc_end_st)
4418 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
4419 reject_statement ();
4422 while (st != acc_end_st);
4424 gcc_assert (new_st.op == EXEC_NOP);
4426 gfc_clear_new_st ();
4427 gfc_commit_symbols ();
4428 gfc_warning_check ();
4429 pop_state ();
4432 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4434 static gfc_statement
4435 parse_oacc_loop (gfc_statement acc_st)
4437 gfc_statement st;
4438 gfc_code *cp, *np;
4439 gfc_state_data s, *sd;
4441 for (sd = gfc_state_stack; sd; sd = sd->previous)
4442 if (sd->state == COMP_CRITICAL)
4443 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4445 accept_statement (acc_st);
4447 cp = gfc_state_stack->tail;
4448 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4449 np = new_level (cp);
4450 np->op = cp->op;
4451 np->block = NULL;
4453 for (;;)
4455 st = next_statement ();
4456 if (st == ST_NONE)
4457 unexpected_eof ();
4458 else if (st == ST_DO)
4459 break;
4460 else
4462 gfc_error ("Expected DO loop at %C");
4463 reject_statement ();
4467 parse_do_block ();
4468 if (gfc_statement_label != NULL
4469 && gfc_state_stack->previous != NULL
4470 && gfc_state_stack->previous->state == COMP_DO
4471 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4473 pop_state ();
4474 return ST_IMPLIED_ENDDO;
4477 check_do_closure ();
4478 pop_state ();
4480 st = next_statement ();
4481 if (st == ST_OACC_END_LOOP)
4482 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
4483 if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
4484 (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
4485 (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
4487 gcc_assert (new_st.op == EXEC_NOP);
4488 gfc_clear_new_st ();
4489 gfc_commit_symbols ();
4490 gfc_warning_check ();
4491 st = next_statement ();
4493 return st;
4497 /* Parse the statements of an OpenMP structured block. */
4499 static void
4500 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
4502 gfc_statement st, omp_end_st;
4503 gfc_code *cp, *np;
4504 gfc_state_data s;
4506 accept_statement (omp_st);
4508 cp = gfc_state_stack->tail;
4509 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4510 np = new_level (cp);
4511 np->op = cp->op;
4512 np->block = NULL;
4514 switch (omp_st)
4516 case ST_OMP_PARALLEL:
4517 omp_end_st = ST_OMP_END_PARALLEL;
4518 break;
4519 case ST_OMP_PARALLEL_SECTIONS:
4520 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
4521 break;
4522 case ST_OMP_SECTIONS:
4523 omp_end_st = ST_OMP_END_SECTIONS;
4524 break;
4525 case ST_OMP_ORDERED:
4526 omp_end_st = ST_OMP_END_ORDERED;
4527 break;
4528 case ST_OMP_CRITICAL:
4529 omp_end_st = ST_OMP_END_CRITICAL;
4530 break;
4531 case ST_OMP_MASTER:
4532 omp_end_st = ST_OMP_END_MASTER;
4533 break;
4534 case ST_OMP_SINGLE:
4535 omp_end_st = ST_OMP_END_SINGLE;
4536 break;
4537 case ST_OMP_TARGET:
4538 omp_end_st = ST_OMP_END_TARGET;
4539 break;
4540 case ST_OMP_TARGET_DATA:
4541 omp_end_st = ST_OMP_END_TARGET_DATA;
4542 break;
4543 case ST_OMP_TARGET_TEAMS:
4544 omp_end_st = ST_OMP_END_TARGET_TEAMS;
4545 break;
4546 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4547 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4548 break;
4549 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4550 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4551 break;
4552 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4553 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4554 break;
4555 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4556 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4557 break;
4558 case ST_OMP_TASK:
4559 omp_end_st = ST_OMP_END_TASK;
4560 break;
4561 case ST_OMP_TASKGROUP:
4562 omp_end_st = ST_OMP_END_TASKGROUP;
4563 break;
4564 case ST_OMP_TEAMS:
4565 omp_end_st = ST_OMP_END_TEAMS;
4566 break;
4567 case ST_OMP_TEAMS_DISTRIBUTE:
4568 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4569 break;
4570 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4571 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4572 break;
4573 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4574 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4575 break;
4576 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4577 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4578 break;
4579 case ST_OMP_DISTRIBUTE:
4580 omp_end_st = ST_OMP_END_DISTRIBUTE;
4581 break;
4582 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4583 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4584 break;
4585 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4586 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4587 break;
4588 case ST_OMP_DISTRIBUTE_SIMD:
4589 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4590 break;
4591 case ST_OMP_WORKSHARE:
4592 omp_end_st = ST_OMP_END_WORKSHARE;
4593 break;
4594 case ST_OMP_PARALLEL_WORKSHARE:
4595 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
4596 break;
4597 default:
4598 gcc_unreachable ();
4603 if (workshare_stmts_only)
4605 /* Inside of !$omp workshare, only
4606 scalar assignments
4607 array assignments
4608 where statements and constructs
4609 forall statements and constructs
4610 !$omp atomic
4611 !$omp critical
4612 !$omp parallel
4613 are allowed. For !$omp critical these
4614 restrictions apply recursively. */
4615 bool cycle = true;
4617 st = next_statement ();
4618 for (;;)
4620 switch (st)
4622 case ST_NONE:
4623 unexpected_eof ();
4625 case ST_ASSIGNMENT:
4626 case ST_WHERE:
4627 case ST_FORALL:
4628 accept_statement (st);
4629 break;
4631 case ST_WHERE_BLOCK:
4632 parse_where_block ();
4633 break;
4635 case ST_FORALL_BLOCK:
4636 parse_forall_block ();
4637 break;
4639 case ST_OMP_PARALLEL:
4640 case ST_OMP_PARALLEL_SECTIONS:
4641 parse_omp_structured_block (st, false);
4642 break;
4644 case ST_OMP_PARALLEL_WORKSHARE:
4645 case ST_OMP_CRITICAL:
4646 parse_omp_structured_block (st, true);
4647 break;
4649 case ST_OMP_PARALLEL_DO:
4650 case ST_OMP_PARALLEL_DO_SIMD:
4651 st = parse_omp_do (st);
4652 continue;
4654 case ST_OMP_ATOMIC:
4655 st = parse_omp_oacc_atomic (true);
4656 continue;
4658 default:
4659 cycle = false;
4660 break;
4663 if (!cycle)
4664 break;
4666 st = next_statement ();
4669 else
4670 st = parse_executable (ST_NONE);
4671 if (st == ST_NONE)
4672 unexpected_eof ();
4673 else if (st == ST_OMP_SECTION
4674 && (omp_st == ST_OMP_SECTIONS
4675 || omp_st == ST_OMP_PARALLEL_SECTIONS))
4677 np = new_level (np);
4678 np->op = cp->op;
4679 np->block = NULL;
4681 else if (st != omp_end_st)
4682 unexpected_statement (st);
4684 while (st != omp_end_st);
4686 switch (new_st.op)
4688 case EXEC_OMP_END_NOWAIT:
4689 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4690 break;
4691 case EXEC_OMP_CRITICAL:
4692 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
4693 || (new_st.ext.omp_name != NULL
4694 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
4695 gfc_error ("Name after !$omp critical and !$omp end critical does "
4696 "not match at %C");
4697 free (CONST_CAST (char *, new_st.ext.omp_name));
4698 break;
4699 case EXEC_OMP_END_SINGLE:
4700 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
4701 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
4702 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
4703 gfc_free_omp_clauses (new_st.ext.omp_clauses);
4704 break;
4705 case EXEC_NOP:
4706 break;
4707 default:
4708 gcc_unreachable ();
4711 gfc_clear_new_st ();
4712 gfc_commit_symbols ();
4713 gfc_warning_check ();
4714 pop_state ();
4718 /* Accept a series of executable statements. We return the first
4719 statement that doesn't fit to the caller. Any block statements are
4720 passed on to the correct handler, which usually passes the buck
4721 right back here. */
4723 static gfc_statement
4724 parse_executable (gfc_statement st)
4726 int close_flag;
4728 if (st == ST_NONE)
4729 st = next_statement ();
4731 for (;;)
4733 close_flag = check_do_closure ();
4734 if (close_flag)
4735 switch (st)
4737 case ST_GOTO:
4738 case ST_END_PROGRAM:
4739 case ST_RETURN:
4740 case ST_EXIT:
4741 case ST_END_FUNCTION:
4742 case ST_CYCLE:
4743 case ST_PAUSE:
4744 case ST_STOP:
4745 case ST_ERROR_STOP:
4746 case ST_END_SUBROUTINE:
4748 case ST_DO:
4749 case ST_FORALL:
4750 case ST_WHERE:
4751 case ST_SELECT_CASE:
4752 gfc_error ("%s statement at %C cannot terminate a non-block "
4753 "DO loop", gfc_ascii_statement (st));
4754 break;
4756 default:
4757 break;
4760 switch (st)
4762 case ST_NONE:
4763 unexpected_eof ();
4765 case ST_DATA:
4766 gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
4767 "first executable statement");
4768 /* Fall through. */
4770 case ST_FORMAT:
4771 case ST_ENTRY:
4772 case_executable:
4773 accept_statement (st);
4774 if (close_flag == 1)
4775 return ST_IMPLIED_ENDDO;
4776 break;
4778 case ST_BLOCK:
4779 parse_block_construct ();
4780 break;
4782 case ST_ASSOCIATE:
4783 parse_associate ();
4784 break;
4786 case ST_IF_BLOCK:
4787 parse_if_block ();
4788 break;
4790 case ST_SELECT_CASE:
4791 parse_select_block ();
4792 break;
4794 case ST_SELECT_TYPE:
4795 parse_select_type_block();
4796 break;
4798 case ST_DO:
4799 parse_do_block ();
4800 if (check_do_closure () == 1)
4801 return ST_IMPLIED_ENDDO;
4802 break;
4804 case ST_CRITICAL:
4805 parse_critical_block ();
4806 break;
4808 case ST_WHERE_BLOCK:
4809 parse_where_block ();
4810 break;
4812 case ST_FORALL_BLOCK:
4813 parse_forall_block ();
4814 break;
4816 case ST_OACC_PARALLEL_LOOP:
4817 case ST_OACC_KERNELS_LOOP:
4818 case ST_OACC_LOOP:
4819 st = parse_oacc_loop (st);
4820 if (st == ST_IMPLIED_ENDDO)
4821 return st;
4822 continue;
4824 case ST_OACC_PARALLEL:
4825 case ST_OACC_KERNELS:
4826 case ST_OACC_DATA:
4827 case ST_OACC_HOST_DATA:
4828 parse_oacc_structured_block (st);
4829 break;
4831 case ST_OMP_PARALLEL:
4832 case ST_OMP_PARALLEL_SECTIONS:
4833 case ST_OMP_SECTIONS:
4834 case ST_OMP_ORDERED:
4835 case ST_OMP_CRITICAL:
4836 case ST_OMP_MASTER:
4837 case ST_OMP_SINGLE:
4838 case ST_OMP_TARGET:
4839 case ST_OMP_TARGET_DATA:
4840 case ST_OMP_TARGET_TEAMS:
4841 case ST_OMP_TEAMS:
4842 case ST_OMP_TASK:
4843 case ST_OMP_TASKGROUP:
4844 parse_omp_structured_block (st, false);
4845 break;
4847 case ST_OMP_WORKSHARE:
4848 case ST_OMP_PARALLEL_WORKSHARE:
4849 parse_omp_structured_block (st, true);
4850 break;
4852 case ST_OMP_DISTRIBUTE:
4853 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4854 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4855 case ST_OMP_DISTRIBUTE_SIMD:
4856 case ST_OMP_DO:
4857 case ST_OMP_DO_SIMD:
4858 case ST_OMP_PARALLEL_DO:
4859 case ST_OMP_PARALLEL_DO_SIMD:
4860 case ST_OMP_SIMD:
4861 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4862 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4863 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4864 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4865 case ST_OMP_TEAMS_DISTRIBUTE:
4866 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4867 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4868 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4869 st = parse_omp_do (st);
4870 if (st == ST_IMPLIED_ENDDO)
4871 return st;
4872 continue;
4874 case ST_OACC_ATOMIC:
4875 st = parse_omp_oacc_atomic (false);
4876 continue;
4878 case ST_OMP_ATOMIC:
4879 st = parse_omp_oacc_atomic (true);
4880 continue;
4882 default:
4883 return st;
4886 st = next_statement ();
4891 /* Fix the symbols for sibling functions. These are incorrectly added to
4892 the child namespace as the parser didn't know about this procedure. */
4894 static void
4895 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
4897 gfc_namespace *ns;
4898 gfc_symtree *st;
4899 gfc_symbol *old_sym;
4901 for (ns = siblings; ns; ns = ns->sibling)
4903 st = gfc_find_symtree (ns->sym_root, sym->name);
4905 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
4906 goto fixup_contained;
4908 if ((st->n.sym->attr.flavor == FL_DERIVED
4909 && sym->attr.generic && sym->attr.function)
4910 ||(sym->attr.flavor == FL_DERIVED
4911 && st->n.sym->attr.generic && st->n.sym->attr.function))
4912 goto fixup_contained;
4914 old_sym = st->n.sym;
4915 if (old_sym->ns == ns
4916 && !old_sym->attr.contained
4918 /* By 14.6.1.3, host association should be excluded
4919 for the following. */
4920 && !(old_sym->attr.external
4921 || (old_sym->ts.type != BT_UNKNOWN
4922 && !old_sym->attr.implicit_type)
4923 || old_sym->attr.flavor == FL_PARAMETER
4924 || old_sym->attr.use_assoc
4925 || old_sym->attr.in_common
4926 || old_sym->attr.in_equivalence
4927 || old_sym->attr.data
4928 || old_sym->attr.dummy
4929 || old_sym->attr.result
4930 || old_sym->attr.dimension
4931 || old_sym->attr.allocatable
4932 || old_sym->attr.intrinsic
4933 || old_sym->attr.generic
4934 || old_sym->attr.flavor == FL_NAMELIST
4935 || old_sym->attr.flavor == FL_LABEL
4936 || old_sym->attr.proc == PROC_ST_FUNCTION))
4938 /* Replace it with the symbol from the parent namespace. */
4939 st->n.sym = sym;
4940 sym->refs++;
4942 gfc_release_symbol (old_sym);
4945 fixup_contained:
4946 /* Do the same for any contained procedures. */
4947 gfc_fixup_sibling_symbols (sym, ns->contained);
4951 static void
4952 parse_contained (int module)
4954 gfc_namespace *ns, *parent_ns, *tmp;
4955 gfc_state_data s1, s2;
4956 gfc_statement st;
4957 gfc_symbol *sym;
4958 gfc_entry_list *el;
4959 int contains_statements = 0;
4960 int seen_error = 0;
4962 push_state (&s1, COMP_CONTAINS, NULL);
4963 parent_ns = gfc_current_ns;
4967 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
4969 gfc_current_ns->sibling = parent_ns->contained;
4970 parent_ns->contained = gfc_current_ns;
4972 next:
4973 /* Process the next available statement. We come here if we got an error
4974 and rejected the last statement. */
4975 st = next_statement ();
4977 switch (st)
4979 case ST_NONE:
4980 unexpected_eof ();
4982 case ST_FUNCTION:
4983 case ST_SUBROUTINE:
4984 contains_statements = 1;
4985 accept_statement (st);
4987 push_state (&s2,
4988 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
4989 gfc_new_block);
4991 /* For internal procedures, create/update the symbol in the
4992 parent namespace. */
4994 if (!module)
4996 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
4997 gfc_error ("Contained procedure %qs at %C is already "
4998 "ambiguous", gfc_new_block->name);
4999 else
5001 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
5002 sym->name,
5003 &gfc_new_block->declared_at))
5005 if (st == ST_FUNCTION)
5006 gfc_add_function (&sym->attr, sym->name,
5007 &gfc_new_block->declared_at);
5008 else
5009 gfc_add_subroutine (&sym->attr, sym->name,
5010 &gfc_new_block->declared_at);
5014 gfc_commit_symbols ();
5016 else
5017 sym = gfc_new_block;
5019 /* Mark this as a contained function, so it isn't replaced
5020 by other module functions. */
5021 sym->attr.contained = 1;
5023 /* Set implicit_pure so that it can be reset if any of the
5024 tests for purity fail. This is used for some optimisation
5025 during translation. */
5026 if (!sym->attr.pure)
5027 sym->attr.implicit_pure = 1;
5029 parse_progunit (ST_NONE);
5031 /* Fix up any sibling functions that refer to this one. */
5032 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
5033 /* Or refer to any of its alternate entry points. */
5034 for (el = gfc_current_ns->entries; el; el = el->next)
5035 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
5037 gfc_current_ns->code = s2.head;
5038 gfc_current_ns = parent_ns;
5040 pop_state ();
5041 break;
5043 /* These statements are associated with the end of the host unit. */
5044 case ST_END_FUNCTION:
5045 case ST_END_MODULE:
5046 case ST_END_SUBMODULE:
5047 case ST_END_PROGRAM:
5048 case ST_END_SUBROUTINE:
5049 accept_statement (st);
5050 gfc_current_ns->code = s1.head;
5051 break;
5053 default:
5054 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
5055 gfc_ascii_statement (st));
5056 reject_statement ();
5057 seen_error = 1;
5058 goto next;
5059 break;
5062 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
5063 && st != ST_END_MODULE && st != ST_END_SUBMODULE
5064 && st != ST_END_PROGRAM);
5066 /* The first namespace in the list is guaranteed to not have
5067 anything (worthwhile) in it. */
5068 tmp = gfc_current_ns;
5069 gfc_current_ns = parent_ns;
5070 if (seen_error && tmp->refs > 1)
5071 gfc_free_namespace (tmp);
5073 ns = gfc_current_ns->contained;
5074 gfc_current_ns->contained = ns->sibling;
5075 gfc_free_namespace (ns);
5077 pop_state ();
5078 if (!contains_statements)
5079 gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
5080 "FUNCTION or SUBROUTINE statement at %C");
5084 /* The result variable in a MODULE PROCEDURE needs to be created and
5085 its characteristics copied from the interface since it is neither
5086 declared in the procedure declaration nor in the specification
5087 part. */
5089 static void
5090 get_modproc_result (void)
5092 gfc_symbol *proc;
5093 if (gfc_state_stack->previous
5094 && gfc_state_stack->previous->state == COMP_CONTAINS
5095 && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
5097 proc = gfc_current_ns->proc_name ? gfc_current_ns->proc_name : NULL;
5098 if (proc != NULL
5099 && proc->attr.function
5100 && proc->ts.interface
5101 && proc->ts.interface->result
5102 && proc->ts.interface->result != proc->ts.interface)
5104 gfc_copy_dummy_sym (&proc->result, proc->ts.interface->result, 1);
5105 gfc_set_sym_referenced (proc->result);
5106 proc->result->attr.if_source = IFSRC_DECL;
5107 gfc_commit_symbol (proc->result);
5113 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
5115 static void
5116 parse_progunit (gfc_statement st)
5118 gfc_state_data *p;
5119 int n;
5121 if (gfc_new_block
5122 && gfc_new_block->abr_modproc_decl
5123 && gfc_new_block->attr.function)
5124 get_modproc_result ();
5126 st = parse_spec (st);
5127 switch (st)
5129 case ST_NONE:
5130 unexpected_eof ();
5132 case ST_CONTAINS:
5133 /* This is not allowed within BLOCK! */
5134 if (gfc_current_state () != COMP_BLOCK)
5135 goto contains;
5136 break;
5138 case_end:
5139 accept_statement (st);
5140 goto done;
5142 default:
5143 break;
5146 if (gfc_current_state () == COMP_FUNCTION)
5147 gfc_check_function_type (gfc_current_ns);
5149 loop:
5150 for (;;)
5152 st = parse_executable (st);
5154 switch (st)
5156 case ST_NONE:
5157 unexpected_eof ();
5159 case ST_CONTAINS:
5160 /* This is not allowed within BLOCK! */
5161 if (gfc_current_state () != COMP_BLOCK)
5162 goto contains;
5163 break;
5165 case_end:
5166 accept_statement (st);
5167 goto done;
5169 default:
5170 break;
5173 unexpected_statement (st);
5174 reject_statement ();
5175 st = next_statement ();
5178 contains:
5179 n = 0;
5181 for (p = gfc_state_stack; p; p = p->previous)
5182 if (p->state == COMP_CONTAINS)
5183 n++;
5185 if (gfc_find_state (COMP_MODULE) == true
5186 || gfc_find_state (COMP_SUBMODULE) == true)
5187 n--;
5189 if (n > 0)
5191 gfc_error ("CONTAINS statement at %C is already in a contained "
5192 "program unit");
5193 reject_statement ();
5194 st = next_statement ();
5195 goto loop;
5198 parse_contained (0);
5200 done:
5201 gfc_current_ns->code = gfc_state_stack->head;
5205 /* Come here to complain about a global symbol already in use as
5206 something else. */
5208 void
5209 gfc_global_used (gfc_gsymbol *sym, locus *where)
5211 const char *name;
5213 if (where == NULL)
5214 where = &gfc_current_locus;
5216 switch(sym->type)
5218 case GSYM_PROGRAM:
5219 name = "PROGRAM";
5220 break;
5221 case GSYM_FUNCTION:
5222 name = "FUNCTION";
5223 break;
5224 case GSYM_SUBROUTINE:
5225 name = "SUBROUTINE";
5226 break;
5227 case GSYM_COMMON:
5228 name = "COMMON";
5229 break;
5230 case GSYM_BLOCK_DATA:
5231 name = "BLOCK DATA";
5232 break;
5233 case GSYM_MODULE:
5234 name = "MODULE";
5235 break;
5236 default:
5237 gfc_internal_error ("gfc_global_used(): Bad type");
5238 name = NULL;
5241 if (sym->binding_label)
5242 gfc_error ("Global binding name %qs at %L is already being used as a %s "
5243 "at %L", sym->binding_label, where, name, &sym->where);
5244 else
5245 gfc_error ("Global name %qs at %L is already being used as a %s at %L",
5246 sym->name, where, name, &sym->where);
5250 /* Parse a block data program unit. */
5252 static void
5253 parse_block_data (void)
5255 gfc_statement st;
5256 static locus blank_locus;
5257 static int blank_block=0;
5258 gfc_gsymbol *s;
5260 gfc_current_ns->proc_name = gfc_new_block;
5261 gfc_current_ns->is_block_data = 1;
5263 if (gfc_new_block == NULL)
5265 if (blank_block)
5266 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5267 "prior BLOCK DATA at %L", &blank_locus);
5268 else
5270 blank_block = 1;
5271 blank_locus = gfc_current_locus;
5274 else
5276 s = gfc_get_gsymbol (gfc_new_block->name);
5277 if (s->defined
5278 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
5279 gfc_global_used (s, &gfc_new_block->declared_at);
5280 else
5282 s->type = GSYM_BLOCK_DATA;
5283 s->where = gfc_new_block->declared_at;
5284 s->defined = 1;
5288 st = parse_spec (ST_NONE);
5290 while (st != ST_END_BLOCK_DATA)
5292 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5293 gfc_ascii_statement (st));
5294 reject_statement ();
5295 st = next_statement ();
5300 /* Following the association of the ancestor (sub)module symbols, they
5301 must be set host rather than use associated and all must be public.
5302 They are flagged up by 'used_in_submodule' so that they can be set
5303 DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl). Otherwise the
5304 linker chokes on multiple symbol definitions. */
5306 static void
5307 set_syms_host_assoc (gfc_symbol *sym)
5309 gfc_component *c;
5311 if (sym == NULL)
5312 return;
5314 if (sym->attr.module_procedure)
5315 sym->attr.external = 0;
5317 /* sym->attr.access = ACCESS_PUBLIC; */
5319 sym->attr.use_assoc = 0;
5320 sym->attr.host_assoc = 1;
5321 sym->attr.used_in_submodule =1;
5323 if (sym->attr.flavor == FL_DERIVED)
5325 for (c = sym->components; c; c = c->next)
5326 c->attr.access = ACCESS_PUBLIC;
5330 /* Parse a module subprogram. */
5332 static void
5333 parse_module (void)
5335 gfc_statement st;
5336 gfc_gsymbol *s;
5337 bool error;
5339 s = gfc_get_gsymbol (gfc_new_block->name);
5340 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
5341 gfc_global_used (s, &gfc_new_block->declared_at);
5342 else
5344 s->type = GSYM_MODULE;
5345 s->where = gfc_new_block->declared_at;
5346 s->defined = 1;
5349 /* Something is nulling the module_list after this point. This is good
5350 since it allows us to 'USE' the parent modules that the submodule
5351 inherits and to set (most) of the symbols as host associated. */
5352 if (gfc_current_state () == COMP_SUBMODULE)
5354 use_modules ();
5355 gfc_traverse_ns (gfc_current_ns, set_syms_host_assoc);
5358 st = parse_spec (ST_NONE);
5360 error = false;
5361 loop:
5362 switch (st)
5364 case ST_NONE:
5365 unexpected_eof ();
5367 case ST_CONTAINS:
5368 parse_contained (1);
5369 break;
5371 case ST_END_MODULE:
5372 case ST_END_SUBMODULE:
5373 accept_statement (st);
5374 break;
5376 default:
5377 gfc_error ("Unexpected %s statement in MODULE at %C",
5378 gfc_ascii_statement (st));
5380 error = true;
5381 reject_statement ();
5382 st = next_statement ();
5383 goto loop;
5386 /* Make sure not to free the namespace twice on error. */
5387 if (!error)
5388 s->ns = gfc_current_ns;
5392 /* Add a procedure name to the global symbol table. */
5394 static void
5395 add_global_procedure (bool sub)
5397 gfc_gsymbol *s;
5399 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5400 name is a global identifier. */
5401 if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
5403 s = gfc_get_gsymbol (gfc_new_block->name);
5405 if (s->defined
5406 || (s->type != GSYM_UNKNOWN
5407 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5409 gfc_global_used (s, &gfc_new_block->declared_at);
5410 /* Silence follow-up errors. */
5411 gfc_new_block->binding_label = NULL;
5413 else
5415 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5416 s->sym_name = gfc_new_block->name;
5417 s->where = gfc_new_block->declared_at;
5418 s->defined = 1;
5419 s->ns = gfc_current_ns;
5423 /* Don't add the symbol multiple times. */
5424 if (gfc_new_block->binding_label
5425 && (!gfc_notification_std (GFC_STD_F2008)
5426 || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
5428 s = gfc_get_gsymbol (gfc_new_block->binding_label);
5430 if (s->defined
5431 || (s->type != GSYM_UNKNOWN
5432 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5434 gfc_global_used (s, &gfc_new_block->declared_at);
5435 /* Silence follow-up errors. */
5436 gfc_new_block->binding_label = NULL;
5438 else
5440 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5441 s->sym_name = gfc_new_block->name;
5442 s->binding_label = gfc_new_block->binding_label;
5443 s->where = gfc_new_block->declared_at;
5444 s->defined = 1;
5445 s->ns = gfc_current_ns;
5451 /* Add a program to the global symbol table. */
5453 static void
5454 add_global_program (void)
5456 gfc_gsymbol *s;
5458 if (gfc_new_block == NULL)
5459 return;
5460 s = gfc_get_gsymbol (gfc_new_block->name);
5462 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
5463 gfc_global_used (s, &gfc_new_block->declared_at);
5464 else
5466 s->type = GSYM_PROGRAM;
5467 s->where = gfc_new_block->declared_at;
5468 s->defined = 1;
5469 s->ns = gfc_current_ns;
5474 /* Resolve all the program units. */
5475 static void
5476 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
5478 gfc_free_dt_list ();
5479 gfc_current_ns = gfc_global_ns_list;
5480 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5482 if (gfc_current_ns->proc_name
5483 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5484 continue; /* Already resolved. */
5486 if (gfc_current_ns->proc_name)
5487 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5488 gfc_resolve (gfc_current_ns);
5489 gfc_current_ns->derived_types = gfc_derived_types;
5490 gfc_derived_types = NULL;
5495 static void
5496 clean_up_modules (gfc_gsymbol *gsym)
5498 if (gsym == NULL)
5499 return;
5501 clean_up_modules (gsym->left);
5502 clean_up_modules (gsym->right);
5504 if (gsym->type != GSYM_MODULE || !gsym->ns)
5505 return;
5507 gfc_current_ns = gsym->ns;
5508 gfc_derived_types = gfc_current_ns->derived_types;
5509 gfc_done_2 ();
5510 gsym->ns = NULL;
5511 return;
5515 /* Translate all the program units. This could be in a different order
5516 to resolution if there are forward references in the file. */
5517 static void
5518 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
5520 int errors;
5522 gfc_current_ns = gfc_global_ns_list;
5523 gfc_get_errors (NULL, &errors);
5525 /* We first translate all modules to make sure that later parts
5526 of the program can use the decl. Then we translate the nonmodules. */
5528 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5530 if (!gfc_current_ns->proc_name
5531 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5532 continue;
5534 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5535 gfc_derived_types = gfc_current_ns->derived_types;
5536 gfc_generate_module_code (gfc_current_ns);
5537 gfc_current_ns->translated = 1;
5540 gfc_current_ns = gfc_global_ns_list;
5541 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5543 if (gfc_current_ns->proc_name
5544 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5545 continue;
5547 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5548 gfc_derived_types = gfc_current_ns->derived_types;
5549 gfc_generate_code (gfc_current_ns);
5550 gfc_current_ns->translated = 1;
5553 /* Clean up all the namespaces after translation. */
5554 gfc_current_ns = gfc_global_ns_list;
5555 for (;gfc_current_ns;)
5557 gfc_namespace *ns;
5559 if (gfc_current_ns->proc_name
5560 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5562 gfc_current_ns = gfc_current_ns->sibling;
5563 continue;
5566 ns = gfc_current_ns->sibling;
5567 gfc_derived_types = gfc_current_ns->derived_types;
5568 gfc_done_2 ();
5569 gfc_current_ns = ns;
5572 clean_up_modules (gfc_gsym_root);
5576 /* Top level parser. */
5578 bool
5579 gfc_parse_file (void)
5581 int seen_program, errors_before, errors;
5582 gfc_state_data top, s;
5583 gfc_statement st;
5584 locus prog_locus;
5585 gfc_namespace *next;
5587 gfc_start_source_files ();
5589 top.state = COMP_NONE;
5590 top.sym = NULL;
5591 top.previous = NULL;
5592 top.head = top.tail = NULL;
5593 top.do_variable = NULL;
5595 gfc_state_stack = &top;
5597 gfc_clear_new_st ();
5599 gfc_statement_label = NULL;
5601 if (setjmp (eof_buf))
5602 return false; /* Come here on unexpected EOF */
5604 /* Prepare the global namespace that will contain the
5605 program units. */
5606 gfc_global_ns_list = next = NULL;
5608 seen_program = 0;
5609 errors_before = 0;
5611 /* Exit early for empty files. */
5612 if (gfc_at_eof ())
5613 goto done;
5615 in_specification_block = true;
5616 loop:
5617 gfc_init_2 ();
5618 st = next_statement ();
5619 switch (st)
5621 case ST_NONE:
5622 gfc_done_2 ();
5623 goto done;
5625 case ST_PROGRAM:
5626 if (seen_program)
5627 goto duplicate_main;
5628 seen_program = 1;
5629 prog_locus = gfc_current_locus;
5631 push_state (&s, COMP_PROGRAM, gfc_new_block);
5632 main_program_symbol(gfc_current_ns, gfc_new_block->name);
5633 accept_statement (st);
5634 add_global_program ();
5635 parse_progunit (ST_NONE);
5636 goto prog_units;
5637 break;
5639 case ST_SUBROUTINE:
5640 add_global_procedure (true);
5641 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
5642 accept_statement (st);
5643 parse_progunit (ST_NONE);
5644 goto prog_units;
5645 break;
5647 case ST_FUNCTION:
5648 add_global_procedure (false);
5649 push_state (&s, COMP_FUNCTION, gfc_new_block);
5650 accept_statement (st);
5651 parse_progunit (ST_NONE);
5652 goto prog_units;
5653 break;
5655 case ST_BLOCK_DATA:
5656 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
5657 accept_statement (st);
5658 parse_block_data ();
5659 break;
5661 case ST_MODULE:
5662 push_state (&s, COMP_MODULE, gfc_new_block);
5663 accept_statement (st);
5665 gfc_get_errors (NULL, &errors_before);
5666 parse_module ();
5667 break;
5669 case ST_SUBMODULE:
5670 push_state (&s, COMP_SUBMODULE, gfc_new_block);
5671 accept_statement (st);
5673 gfc_get_errors (NULL, &errors_before);
5674 parse_module ();
5675 break;
5677 /* Anything else starts a nameless main program block. */
5678 default:
5679 if (seen_program)
5680 goto duplicate_main;
5681 seen_program = 1;
5682 prog_locus = gfc_current_locus;
5684 push_state (&s, COMP_PROGRAM, gfc_new_block);
5685 main_program_symbol (gfc_current_ns, "MAIN__");
5686 parse_progunit (st);
5687 goto prog_units;
5688 break;
5691 /* Handle the non-program units. */
5692 gfc_current_ns->code = s.head;
5694 gfc_resolve (gfc_current_ns);
5696 /* Dump the parse tree if requested. */
5697 if (flag_dump_fortran_original)
5698 gfc_dump_parse_tree (gfc_current_ns, stdout);
5700 gfc_get_errors (NULL, &errors);
5701 if (s.state == COMP_MODULE || s.state == COMP_SUBMODULE)
5703 gfc_dump_module (s.sym->name, errors_before == errors);
5704 gfc_current_ns->derived_types = gfc_derived_types;
5705 gfc_derived_types = NULL;
5706 goto prog_units;
5708 else
5710 if (errors == 0)
5711 gfc_generate_code (gfc_current_ns);
5712 pop_state ();
5713 gfc_done_2 ();
5716 goto loop;
5718 prog_units:
5719 /* The main program and non-contained procedures are put
5720 in the global namespace list, so that they can be processed
5721 later and all their interfaces resolved. */
5722 gfc_current_ns->code = s.head;
5723 if (next)
5725 for (; next->sibling; next = next->sibling)
5727 next->sibling = gfc_current_ns;
5729 else
5730 gfc_global_ns_list = gfc_current_ns;
5732 next = gfc_current_ns;
5734 pop_state ();
5735 goto loop;
5737 done:
5739 /* Do the resolution. */
5740 resolve_all_program_units (gfc_global_ns_list);
5742 /* Do the parse tree dump. */
5743 gfc_current_ns
5744 = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
5746 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5747 if (!gfc_current_ns->proc_name
5748 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5750 gfc_dump_parse_tree (gfc_current_ns, stdout);
5751 fputs ("------------------------------------------\n\n", stdout);
5754 /* Do the translation. */
5755 translate_all_program_units (gfc_global_ns_list);
5757 gfc_end_source_files ();
5758 return true;
5760 duplicate_main:
5761 /* If we see a duplicate main program, shut down. If the second
5762 instance is an implied main program, i.e. data decls or executable
5763 statements, we're in for lots of errors. */
5764 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
5765 reject_statement ();
5766 gfc_done_2 ();
5767 return true;
5770 /* Return true if this state data represents an OpenACC region. */
5771 bool
5772 is_oacc (gfc_state_data *sd)
5774 switch (sd->construct->op)
5776 case EXEC_OACC_PARALLEL_LOOP:
5777 case EXEC_OACC_PARALLEL:
5778 case EXEC_OACC_KERNELS_LOOP:
5779 case EXEC_OACC_KERNELS:
5780 case EXEC_OACC_DATA:
5781 case EXEC_OACC_HOST_DATA:
5782 case EXEC_OACC_LOOP:
5783 case EXEC_OACC_UPDATE:
5784 case EXEC_OACC_WAIT:
5785 case EXEC_OACC_CACHE:
5786 case EXEC_OACC_ENTER_DATA:
5787 case EXEC_OACC_EXIT_DATA:
5788 case EXEC_OACC_ATOMIC:
5789 return true;
5791 default:
5792 return false;