2015-05-22 Pascal Obry <obry@adacore.com>
[official-gcc.git] / gcc / fortran / parse.c
blob3135d9af7976bfb479e411179f3b6dc6465caf20
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 <setjmp.h>
24 #include "coretypes.h"
25 #include "flags.h"
26 #include "gfortran.h"
27 #include "match.h"
28 #include "parse.h"
29 #include "debug.h"
31 /* Current statement label. Zero means no statement label. Because new_st
32 can get wiped during statement matching, we have to keep it separate. */
34 gfc_st_label *gfc_statement_label;
36 static locus label_locus;
37 static jmp_buf eof_buf;
39 gfc_state_data *gfc_state_stack;
40 static bool last_was_use_stmt = false;
42 /* TODO: Re-order functions to kill these forward decls. */
43 static void check_statement_label (gfc_statement);
44 static void undo_new_statement (void);
45 static void reject_statement (void);
48 /* A sort of half-matching function. We try to match the word on the
49 input with the passed string. If this succeeds, we call the
50 keyword-dependent matching function that will match the rest of the
51 statement. For single keywords, the matching subroutine is
52 gfc_match_eos(). */
54 static match
55 match_word (const char *str, match (*subr) (void), locus *old_locus)
57 match m;
59 if (str != NULL)
61 m = gfc_match (str);
62 if (m != MATCH_YES)
63 return m;
66 m = (*subr) ();
68 if (m != MATCH_YES)
70 gfc_current_locus = *old_locus;
71 reject_statement ();
74 return m;
78 /* Like match_word, but if str is matched, set a flag that it
79 was matched. */
80 static match
81 match_word_omp_simd (const char *str, match (*subr) (void), locus *old_locus,
82 bool *simd_matched)
84 match m;
86 if (str != NULL)
88 m = gfc_match (str);
89 if (m != MATCH_YES)
90 return m;
91 *simd_matched = true;
94 m = (*subr) ();
96 if (m != MATCH_YES)
98 gfc_current_locus = *old_locus;
99 reject_statement ();
102 return m;
106 /* Load symbols from all USE statements encountered in this scoping unit. */
108 static void
109 use_modules (void)
111 gfc_error_buf old_error_1;
112 output_buffer old_error;
114 gfc_push_error (&old_error, &old_error_1);
115 gfc_buffer_error (false);
116 gfc_use_modules ();
117 gfc_buffer_error (true);
118 gfc_pop_error (&old_error, &old_error_1);
119 gfc_commit_symbols ();
120 gfc_warning_check ();
121 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
122 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
123 gfc_current_ns->old_data = gfc_current_ns->data;
124 last_was_use_stmt = false;
128 /* Figure out what the next statement is, (mostly) regardless of
129 proper ordering. The do...while(0) is there to prevent if/else
130 ambiguity. */
132 #define match(keyword, subr, st) \
133 do { \
134 if (match_word (keyword, subr, &old_locus) == MATCH_YES) \
135 return st; \
136 else \
137 undo_new_statement (); \
138 } while (0);
141 /* This is a specialist version of decode_statement that is used
142 for the specification statements in a function, whose
143 characteristics are deferred into the specification statements.
144 eg.: INTEGER (king = mykind) foo ()
145 USE mymodule, ONLY mykind.....
146 The KIND parameter needs a return after USE or IMPORT, whereas
147 derived type declarations can occur anywhere, up the executable
148 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
149 out of the correct kind of specification statements. */
150 static gfc_statement
151 decode_specification_statement (void)
153 gfc_statement st;
154 locus old_locus;
155 char c;
157 if (gfc_match_eos () == MATCH_YES)
158 return ST_NONE;
160 old_locus = gfc_current_locus;
162 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
164 last_was_use_stmt = true;
165 return ST_USE;
167 else
169 undo_new_statement ();
170 if (last_was_use_stmt)
171 use_modules ();
174 match ("import", gfc_match_import, ST_IMPORT);
176 if (gfc_current_block ()->result->ts.type != BT_DERIVED)
177 goto end_of_block;
179 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
180 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
181 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
183 /* General statement matching: Instead of testing every possible
184 statement, we eliminate most possibilities by peeking at the
185 first character. */
187 c = gfc_peek_ascii_char ();
189 switch (c)
191 case 'a':
192 match ("abstract% interface", gfc_match_abstract_interface,
193 ST_INTERFACE);
194 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
195 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
196 break;
198 case 'b':
199 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
200 break;
202 case 'c':
203 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
204 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
205 break;
207 case 'd':
208 match ("data", gfc_match_data, ST_DATA);
209 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
210 break;
212 case 'e':
213 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
214 match ("entry% ", gfc_match_entry, ST_ENTRY);
215 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
216 match ("external", gfc_match_external, ST_ATTR_DECL);
217 break;
219 case 'f':
220 match ("format", gfc_match_format, ST_FORMAT);
221 break;
223 case 'g':
224 break;
226 case 'i':
227 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
228 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
229 match ("interface", gfc_match_interface, ST_INTERFACE);
230 match ("intent", gfc_match_intent, ST_ATTR_DECL);
231 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
232 break;
234 case 'm':
235 break;
237 case 'n':
238 match ("namelist", gfc_match_namelist, ST_NAMELIST);
239 break;
241 case 'o':
242 match ("optional", gfc_match_optional, ST_ATTR_DECL);
243 break;
245 case 'p':
246 match ("parameter", gfc_match_parameter, ST_PARAMETER);
247 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
248 if (gfc_match_private (&st) == MATCH_YES)
249 return st;
250 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
251 if (gfc_match_public (&st) == MATCH_YES)
252 return st;
253 match ("protected", gfc_match_protected, ST_ATTR_DECL);
254 break;
256 case 'r':
257 break;
259 case 's':
260 match ("save", gfc_match_save, ST_ATTR_DECL);
261 break;
263 case 't':
264 match ("target", gfc_match_target, ST_ATTR_DECL);
265 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
266 break;
268 case 'u':
269 break;
271 case 'v':
272 match ("value", gfc_match_value, ST_ATTR_DECL);
273 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
274 break;
276 case 'w':
277 break;
280 /* This is not a specification statement. See if any of the matchers
281 has stored an error message of some sort. */
283 end_of_block:
284 gfc_clear_error ();
285 gfc_buffer_error (false);
286 gfc_current_locus = old_locus;
288 return ST_GET_FCN_CHARACTERISTICS;
292 /* This is the primary 'decode_statement'. */
293 static gfc_statement
294 decode_statement (void)
296 gfc_namespace *ns;
297 gfc_statement st;
298 locus old_locus;
299 match m;
300 char c;
302 gfc_enforce_clean_symbol_state ();
304 gfc_clear_error (); /* Clear any pending errors. */
305 gfc_clear_warning (); /* Clear any pending warnings. */
307 gfc_matching_function = false;
309 if (gfc_match_eos () == MATCH_YES)
310 return ST_NONE;
312 if (gfc_current_state () == COMP_FUNCTION
313 && gfc_current_block ()->result->ts.kind == -1)
314 return decode_specification_statement ();
316 old_locus = gfc_current_locus;
318 c = gfc_peek_ascii_char ();
320 if (c == 'u')
322 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
324 last_was_use_stmt = true;
325 return ST_USE;
327 else
328 undo_new_statement ();
331 if (last_was_use_stmt)
332 use_modules ();
334 /* Try matching a data declaration or function declaration. The
335 input "REALFUNCTIONA(N)" can mean several things in different
336 contexts, so it (and its relatives) get special treatment. */
338 if (gfc_current_state () == COMP_NONE
339 || gfc_current_state () == COMP_INTERFACE
340 || gfc_current_state () == COMP_CONTAINS)
342 gfc_matching_function = true;
343 m = gfc_match_function_decl ();
344 if (m == MATCH_YES)
345 return ST_FUNCTION;
346 else if (m == MATCH_ERROR)
347 reject_statement ();
348 else
349 gfc_undo_symbols ();
350 gfc_current_locus = old_locus;
352 gfc_matching_function = false;
355 /* Match statements whose error messages are meant to be overwritten
356 by something better. */
358 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
359 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
360 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
362 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
363 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
365 /* Try to match a subroutine statement, which has the same optional
366 prefixes that functions can have. */
368 if (gfc_match_subroutine () == MATCH_YES)
369 return ST_SUBROUTINE;
370 gfc_undo_symbols ();
371 gfc_current_locus = old_locus;
373 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
374 statements, which might begin with a block label. The match functions for
375 these statements are unusual in that their keyword is not seen before
376 the matcher is called. */
378 if (gfc_match_if (&st) == MATCH_YES)
379 return st;
380 gfc_undo_symbols ();
381 gfc_current_locus = old_locus;
383 if (gfc_match_where (&st) == MATCH_YES)
384 return st;
385 gfc_undo_symbols ();
386 gfc_current_locus = old_locus;
388 if (gfc_match_forall (&st) == MATCH_YES)
389 return st;
390 gfc_undo_symbols ();
391 gfc_current_locus = old_locus;
393 match (NULL, gfc_match_do, ST_DO);
394 match (NULL, gfc_match_block, ST_BLOCK);
395 match (NULL, gfc_match_associate, ST_ASSOCIATE);
396 match (NULL, gfc_match_critical, ST_CRITICAL);
397 match (NULL, gfc_match_select, ST_SELECT_CASE);
399 gfc_current_ns = gfc_build_block_ns (gfc_current_ns);
400 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
401 ns = gfc_current_ns;
402 gfc_current_ns = gfc_current_ns->parent;
403 gfc_free_namespace (ns);
405 /* General statement matching: Instead of testing every possible
406 statement, we eliminate most possibilities by peeking at the
407 first character. */
409 switch (c)
411 case 'a':
412 match ("abstract% interface", gfc_match_abstract_interface,
413 ST_INTERFACE);
414 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
415 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
416 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
417 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
418 break;
420 case 'b':
421 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
422 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
423 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
424 break;
426 case 'c':
427 match ("call", gfc_match_call, ST_CALL);
428 match ("close", gfc_match_close, ST_CLOSE);
429 match ("continue", gfc_match_continue, ST_CONTINUE);
430 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
431 match ("cycle", gfc_match_cycle, ST_CYCLE);
432 match ("case", gfc_match_case, ST_CASE);
433 match ("common", gfc_match_common, ST_COMMON);
434 match ("contains", gfc_match_eos, ST_CONTAINS);
435 match ("class", gfc_match_class_is, ST_CLASS_IS);
436 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
437 break;
439 case 'd':
440 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
441 match ("data", gfc_match_data, ST_DATA);
442 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
443 break;
445 case 'e':
446 match ("end file", gfc_match_endfile, ST_END_FILE);
447 match ("exit", gfc_match_exit, ST_EXIT);
448 match ("else", gfc_match_else, ST_ELSE);
449 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
450 match ("else if", gfc_match_elseif, ST_ELSEIF);
451 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
452 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
454 if (gfc_match_end (&st) == MATCH_YES)
455 return st;
457 match ("entry% ", gfc_match_entry, ST_ENTRY);
458 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
459 match ("external", gfc_match_external, ST_ATTR_DECL);
460 break;
462 case 'f':
463 match ("final", gfc_match_final_decl, ST_FINAL);
464 match ("flush", gfc_match_flush, ST_FLUSH);
465 match ("format", gfc_match_format, ST_FORMAT);
466 break;
468 case 'g':
469 match ("generic", gfc_match_generic, ST_GENERIC);
470 match ("go to", gfc_match_goto, ST_GOTO);
471 break;
473 case 'i':
474 match ("inquire", gfc_match_inquire, ST_INQUIRE);
475 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
476 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
477 match ("import", gfc_match_import, ST_IMPORT);
478 match ("interface", gfc_match_interface, ST_INTERFACE);
479 match ("intent", gfc_match_intent, ST_ATTR_DECL);
480 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
481 break;
483 case 'l':
484 match ("lock", gfc_match_lock, ST_LOCK);
485 break;
487 case 'm':
488 match ("module% procedure", gfc_match_modproc, ST_MODULE_PROC);
489 match ("module", gfc_match_module, ST_MODULE);
490 break;
492 case 'n':
493 match ("nullify", gfc_match_nullify, ST_NULLIFY);
494 match ("namelist", gfc_match_namelist, ST_NAMELIST);
495 break;
497 case 'o':
498 match ("open", gfc_match_open, ST_OPEN);
499 match ("optional", gfc_match_optional, ST_ATTR_DECL);
500 break;
502 case 'p':
503 match ("print", gfc_match_print, ST_WRITE);
504 match ("parameter", gfc_match_parameter, ST_PARAMETER);
505 match ("pause", gfc_match_pause, ST_PAUSE);
506 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
507 if (gfc_match_private (&st) == MATCH_YES)
508 return st;
509 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
510 match ("program", gfc_match_program, ST_PROGRAM);
511 if (gfc_match_public (&st) == MATCH_YES)
512 return st;
513 match ("protected", gfc_match_protected, ST_ATTR_DECL);
514 break;
516 case 'r':
517 match ("read", gfc_match_read, ST_READ);
518 match ("return", gfc_match_return, ST_RETURN);
519 match ("rewind", gfc_match_rewind, ST_REWIND);
520 break;
522 case 's':
523 match ("sequence", gfc_match_eos, ST_SEQUENCE);
524 match ("stop", gfc_match_stop, ST_STOP);
525 match ("save", gfc_match_save, ST_ATTR_DECL);
526 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
527 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
528 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
529 break;
531 case 't':
532 match ("target", gfc_match_target, ST_ATTR_DECL);
533 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
534 match ("type is", gfc_match_type_is, ST_TYPE_IS);
535 break;
537 case 'u':
538 match ("unlock", gfc_match_unlock, ST_UNLOCK);
539 break;
541 case 'v':
542 match ("value", gfc_match_value, ST_ATTR_DECL);
543 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
544 break;
546 case 'w':
547 match ("wait", gfc_match_wait, ST_WAIT);
548 match ("write", gfc_match_write, ST_WRITE);
549 break;
552 /* All else has failed, so give up. See if any of the matchers has
553 stored an error message of some sort. */
555 if (!gfc_error_check ())
556 gfc_error_now ("Unclassifiable statement at %C");
558 reject_statement ();
560 gfc_error_recovery ();
562 return ST_NONE;
565 /* Like match, but set a flag simd_matched if keyword matched. */
566 #define matchs(keyword, subr, st) \
567 do { \
568 if (match_word_omp_simd (keyword, subr, &old_locus, \
569 &simd_matched) == MATCH_YES) \
570 return st; \
571 else \
572 undo_new_statement (); \
573 } while (0);
575 /* Like match, but don't match anything if not -fopenmp. */
576 #define matcho(keyword, subr, st) \
577 do { \
578 if (!flag_openmp) \
580 else if (match_word (keyword, subr, &old_locus) \
581 == MATCH_YES) \
582 return st; \
583 else \
584 undo_new_statement (); \
585 } while (0);
587 static gfc_statement
588 decode_oacc_directive (void)
590 locus old_locus;
591 char c;
593 gfc_enforce_clean_symbol_state ();
595 gfc_clear_error (); /* Clear any pending errors. */
596 gfc_clear_warning (); /* Clear any pending warnings. */
598 if (gfc_pure (NULL))
600 gfc_error_now ("OpenACC directives at %C may not appear in PURE "
601 "procedures");
602 gfc_error_recovery ();
603 return ST_NONE;
606 gfc_unset_implicit_pure (NULL);
608 old_locus = gfc_current_locus;
610 /* General OpenACC directive matching: Instead of testing every possible
611 statement, we eliminate most possibilities by peeking at the
612 first character. */
614 c = gfc_peek_ascii_char ();
616 switch (c)
618 case 'c':
619 match ("cache", gfc_match_oacc_cache, ST_OACC_CACHE);
620 break;
621 case 'd':
622 match ("data", gfc_match_oacc_data, ST_OACC_DATA);
623 match ("declare", gfc_match_oacc_declare, ST_OACC_DECLARE);
624 break;
625 case 'e':
626 match ("end data", gfc_match_omp_eos, ST_OACC_END_DATA);
627 match ("end host_data", gfc_match_omp_eos, ST_OACC_END_HOST_DATA);
628 match ("end kernels loop", gfc_match_omp_eos, ST_OACC_END_KERNELS_LOOP);
629 match ("end kernels", gfc_match_omp_eos, ST_OACC_END_KERNELS);
630 match ("end loop", gfc_match_omp_eos, ST_OACC_END_LOOP);
631 match ("end parallel loop", gfc_match_omp_eos, ST_OACC_END_PARALLEL_LOOP);
632 match ("end parallel", gfc_match_omp_eos, ST_OACC_END_PARALLEL);
633 match ("enter data", gfc_match_oacc_enter_data, ST_OACC_ENTER_DATA);
634 match ("exit data", gfc_match_oacc_exit_data, ST_OACC_EXIT_DATA);
635 break;
636 case 'h':
637 match ("host_data", gfc_match_oacc_host_data, ST_OACC_HOST_DATA);
638 break;
639 case 'p':
640 match ("parallel loop", gfc_match_oacc_parallel_loop, ST_OACC_PARALLEL_LOOP);
641 match ("parallel", gfc_match_oacc_parallel, ST_OACC_PARALLEL);
642 break;
643 case 'k':
644 match ("kernels loop", gfc_match_oacc_kernels_loop, ST_OACC_KERNELS_LOOP);
645 match ("kernels", gfc_match_oacc_kernels, ST_OACC_KERNELS);
646 break;
647 case 'l':
648 match ("loop", gfc_match_oacc_loop, ST_OACC_LOOP);
649 break;
650 case 'r':
651 match ("routine", gfc_match_oacc_routine, ST_OACC_ROUTINE);
652 break;
653 case 'u':
654 match ("update", gfc_match_oacc_update, ST_OACC_UPDATE);
655 break;
656 case 'w':
657 match ("wait", gfc_match_oacc_wait, ST_OACC_WAIT);
658 break;
661 /* Directive not found or stored an error message.
662 Check and give up. */
664 if (gfc_error_check () == 0)
665 gfc_error_now ("Unclassifiable OpenACC directive at %C");
667 reject_statement ();
669 gfc_error_recovery ();
671 return ST_NONE;
674 static gfc_statement
675 decode_omp_directive (void)
677 locus old_locus;
678 char c;
679 bool simd_matched = false;
681 gfc_enforce_clean_symbol_state ();
683 gfc_clear_error (); /* Clear any pending errors. */
684 gfc_clear_warning (); /* Clear any pending warnings. */
686 if (gfc_pure (NULL))
688 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
689 "or ELEMENTAL procedures");
690 gfc_error_recovery ();
691 return ST_NONE;
694 gfc_unset_implicit_pure (NULL);
696 old_locus = gfc_current_locus;
698 /* General OpenMP directive matching: Instead of testing every possible
699 statement, we eliminate most possibilities by peeking at the
700 first character. */
702 c = gfc_peek_ascii_char ();
704 /* match is for directives that should be recognized only if
705 -fopenmp, matchs for directives that should be recognized
706 if either -fopenmp or -fopenmp-simd. */
707 switch (c)
709 case 'a':
710 matcho ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
711 break;
712 case 'b':
713 matcho ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
714 break;
715 case 'c':
716 matcho ("cancellation% point", gfc_match_omp_cancellation_point,
717 ST_OMP_CANCELLATION_POINT);
718 matcho ("cancel", gfc_match_omp_cancel, ST_OMP_CANCEL);
719 matcho ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
720 break;
721 case 'd':
722 matchs ("declare reduction", gfc_match_omp_declare_reduction,
723 ST_OMP_DECLARE_REDUCTION);
724 matchs ("declare simd", gfc_match_omp_declare_simd,
725 ST_OMP_DECLARE_SIMD);
726 matcho ("declare target", gfc_match_omp_declare_target,
727 ST_OMP_DECLARE_TARGET);
728 matchs ("distribute parallel do simd",
729 gfc_match_omp_distribute_parallel_do_simd,
730 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD);
731 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do,
732 ST_OMP_DISTRIBUTE_PARALLEL_DO);
733 matchs ("distribute simd", gfc_match_omp_distribute_simd,
734 ST_OMP_DISTRIBUTE_SIMD);
735 matcho ("distribute", gfc_match_omp_distribute, ST_OMP_DISTRIBUTE);
736 matchs ("do simd", gfc_match_omp_do_simd, ST_OMP_DO_SIMD);
737 matcho ("do", gfc_match_omp_do, ST_OMP_DO);
738 break;
739 case 'e':
740 matcho ("end atomic", gfc_match_omp_eos, ST_OMP_END_ATOMIC);
741 matcho ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
742 matchs ("end distribute parallel do simd", gfc_match_omp_eos,
743 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD);
744 matcho ("end distribute parallel do", gfc_match_omp_eos,
745 ST_OMP_END_DISTRIBUTE_PARALLEL_DO);
746 matchs ("end distribute simd", gfc_match_omp_eos,
747 ST_OMP_END_DISTRIBUTE_SIMD);
748 matcho ("end distribute", gfc_match_omp_eos, ST_OMP_END_DISTRIBUTE);
749 matchs ("end do simd", gfc_match_omp_end_nowait, ST_OMP_END_DO_SIMD);
750 matcho ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
751 matchs ("end simd", gfc_match_omp_eos, ST_OMP_END_SIMD);
752 matcho ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
753 matcho ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
754 matchs ("end parallel do simd", gfc_match_omp_eos,
755 ST_OMP_END_PARALLEL_DO_SIMD);
756 matcho ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
757 matcho ("end parallel sections", gfc_match_omp_eos,
758 ST_OMP_END_PARALLEL_SECTIONS);
759 matcho ("end parallel workshare", gfc_match_omp_eos,
760 ST_OMP_END_PARALLEL_WORKSHARE);
761 matcho ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
762 matcho ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
763 matcho ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
764 matcho ("end target data", gfc_match_omp_eos, ST_OMP_END_TARGET_DATA);
765 matchs ("end target teams distribute parallel do simd",
766 gfc_match_omp_eos,
767 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
768 matcho ("end target teams distribute parallel do", gfc_match_omp_eos,
769 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
770 matchs ("end target teams distribute simd", gfc_match_omp_eos,
771 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD);
772 matcho ("end target teams distribute", gfc_match_omp_eos,
773 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE);
774 matcho ("end target teams", gfc_match_omp_eos, ST_OMP_END_TARGET_TEAMS);
775 matcho ("end target", gfc_match_omp_eos, ST_OMP_END_TARGET);
776 matcho ("end taskgroup", gfc_match_omp_eos, ST_OMP_END_TASKGROUP);
777 matcho ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
778 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos,
779 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
780 matcho ("end teams distribute parallel do", gfc_match_omp_eos,
781 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO);
782 matchs ("end teams distribute simd", gfc_match_omp_eos,
783 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD);
784 matcho ("end teams distribute", gfc_match_omp_eos,
785 ST_OMP_END_TEAMS_DISTRIBUTE);
786 matcho ("end teams", gfc_match_omp_eos, ST_OMP_END_TEAMS);
787 matcho ("end workshare", gfc_match_omp_end_nowait,
788 ST_OMP_END_WORKSHARE);
789 break;
790 case 'f':
791 matcho ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
792 break;
793 case 'm':
794 matcho ("master", gfc_match_omp_master, ST_OMP_MASTER);
795 break;
796 case 'o':
797 matcho ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
798 break;
799 case 'p':
800 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
801 ST_OMP_PARALLEL_DO_SIMD);
802 matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
803 matcho ("parallel sections", gfc_match_omp_parallel_sections,
804 ST_OMP_PARALLEL_SECTIONS);
805 matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
806 ST_OMP_PARALLEL_WORKSHARE);
807 matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
808 break;
809 case 's':
810 matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
811 matcho ("section", gfc_match_omp_eos, ST_OMP_SECTION);
812 matchs ("simd", gfc_match_omp_simd, ST_OMP_SIMD);
813 matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
814 break;
815 case 't':
816 matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
817 matchs ("target teams distribute parallel do simd",
818 gfc_match_omp_target_teams_distribute_parallel_do_simd,
819 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
820 matcho ("target teams distribute parallel do",
821 gfc_match_omp_target_teams_distribute_parallel_do,
822 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
823 matchs ("target teams distribute simd",
824 gfc_match_omp_target_teams_distribute_simd,
825 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
826 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
827 ST_OMP_TARGET_TEAMS_DISTRIBUTE);
828 matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
829 matcho ("target update", gfc_match_omp_target_update,
830 ST_OMP_TARGET_UPDATE);
831 matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
832 matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
833 matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
834 matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
835 matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
836 matchs ("teams distribute parallel do simd",
837 gfc_match_omp_teams_distribute_parallel_do_simd,
838 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
839 matcho ("teams distribute parallel do",
840 gfc_match_omp_teams_distribute_parallel_do,
841 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
842 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
843 ST_OMP_TEAMS_DISTRIBUTE_SIMD);
844 matcho ("teams distribute", gfc_match_omp_teams_distribute,
845 ST_OMP_TEAMS_DISTRIBUTE);
846 matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
847 matcho ("threadprivate", gfc_match_omp_threadprivate,
848 ST_OMP_THREADPRIVATE);
849 break;
850 case 'w':
851 matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
852 break;
855 /* All else has failed, so give up. See if any of the matchers has
856 stored an error message of some sort. Don't error out if
857 not -fopenmp and simd_matched is false, i.e. if a directive other
858 than one marked with match has been seen. */
860 if (flag_openmp || simd_matched)
862 if (!gfc_error_check ())
863 gfc_error_now ("Unclassifiable OpenMP directive at %C");
866 reject_statement ();
868 gfc_error_recovery ();
870 return ST_NONE;
873 static gfc_statement
874 decode_gcc_attribute (void)
876 locus old_locus;
878 gfc_enforce_clean_symbol_state ();
880 gfc_clear_error (); /* Clear any pending errors. */
881 gfc_clear_warning (); /* Clear any pending warnings. */
882 old_locus = gfc_current_locus;
884 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
886 /* All else has failed, so give up. See if any of the matchers has
887 stored an error message of some sort. */
889 if (!gfc_error_check ())
890 gfc_error_now ("Unclassifiable GCC directive at %C");
892 reject_statement ();
894 gfc_error_recovery ();
896 return ST_NONE;
899 #undef match
901 /* Assert next length characters to be equal to token in free form. */
903 static void
904 verify_token_free (const char* token, int length, bool last_was_use_stmt)
906 int i;
907 char c;
909 c = gfc_next_ascii_char ();
910 for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
911 gcc_assert (c == token[i]);
913 gcc_assert (gfc_is_whitespace(c));
914 gfc_gobble_whitespace ();
915 if (last_was_use_stmt)
916 use_modules ();
919 /* Get the next statement in free form source. */
921 static gfc_statement
922 next_free (void)
924 match m;
925 int i, cnt, at_bol;
926 char c;
928 at_bol = gfc_at_bol ();
929 gfc_gobble_whitespace ();
931 c = gfc_peek_ascii_char ();
933 if (ISDIGIT (c))
935 char d;
937 /* Found a statement label? */
938 m = gfc_match_st_label (&gfc_statement_label);
940 d = gfc_peek_ascii_char ();
941 if (m != MATCH_YES || !gfc_is_whitespace (d))
943 gfc_match_small_literal_int (&i, &cnt);
945 if (cnt > 5)
946 gfc_error_now ("Too many digits in statement label at %C");
948 if (i == 0)
949 gfc_error_now ("Zero is not a valid statement label at %C");
952 c = gfc_next_ascii_char ();
953 while (ISDIGIT(c));
955 if (!gfc_is_whitespace (c))
956 gfc_error_now ("Non-numeric character in statement label at %C");
958 return ST_NONE;
960 else
962 label_locus = gfc_current_locus;
964 gfc_gobble_whitespace ();
966 if (at_bol && gfc_peek_ascii_char () == ';')
968 gfc_error_now ("Semicolon at %C needs to be preceded by "
969 "statement");
970 gfc_next_ascii_char (); /* Eat up the semicolon. */
971 return ST_NONE;
974 if (gfc_match_eos () == MATCH_YES)
976 gfc_warning_now (0, "Ignoring statement label in empty statement "
977 "at %L", &label_locus);
978 gfc_free_st_label (gfc_statement_label);
979 gfc_statement_label = NULL;
980 return ST_NONE;
984 else if (c == '!')
986 /* Comments have already been skipped by the time we get here,
987 except for GCC attributes and OpenMP/OpenACC directives. */
989 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
990 c = gfc_peek_ascii_char ();
992 if (c == 'g')
994 int i;
996 c = gfc_next_ascii_char ();
997 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
998 gcc_assert (c == "gcc$"[i]);
1000 gfc_gobble_whitespace ();
1001 return decode_gcc_attribute ();
1004 else if (c == '$')
1006 /* Since both OpenMP and OpenACC directives starts with
1007 !$ character sequence, we must check all flags combinations */
1008 if ((flag_openmp || flag_openmp_simd)
1009 && !flag_openacc)
1011 verify_token_free ("$omp", 4, last_was_use_stmt);
1012 return decode_omp_directive ();
1014 else if ((flag_openmp || flag_openmp_simd)
1015 && flag_openacc)
1017 gfc_next_ascii_char (); /* Eat up dollar character */
1018 c = gfc_peek_ascii_char ();
1020 if (c == 'o')
1022 verify_token_free ("omp", 3, last_was_use_stmt);
1023 return decode_omp_directive ();
1025 else if (c == 'a')
1027 verify_token_free ("acc", 3, last_was_use_stmt);
1028 return decode_oacc_directive ();
1031 else if (flag_openacc)
1033 verify_token_free ("$acc", 4, last_was_use_stmt);
1034 return decode_oacc_directive ();
1037 gcc_unreachable ();
1040 if (at_bol && c == ';')
1042 if (!(gfc_option.allow_std & GFC_STD_F2008))
1043 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1044 "statement");
1045 gfc_next_ascii_char (); /* Eat up the semicolon. */
1046 return ST_NONE;
1049 return decode_statement ();
1052 /* Assert next length characters to be equal to token in fixed form. */
1054 static bool
1055 verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
1057 int i;
1058 char c = gfc_next_char_literal (NONSTRING);
1060 for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
1061 gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
1063 if (c != ' ' && c != '0')
1065 gfc_buffer_error (false);
1066 gfc_error ("Bad continuation line at %C");
1067 return false;
1069 if (last_was_use_stmt)
1070 use_modules ();
1072 return true;
1075 /* Get the next statement in fixed-form source. */
1077 static gfc_statement
1078 next_fixed (void)
1080 int label, digit_flag, i;
1081 locus loc;
1082 gfc_char_t c;
1084 if (!gfc_at_bol ())
1085 return decode_statement ();
1087 /* Skip past the current label field, parsing a statement label if
1088 one is there. This is a weird number parser, since the number is
1089 contained within five columns and can have any kind of embedded
1090 spaces. We also check for characters that make the rest of the
1091 line a comment. */
1093 label = 0;
1094 digit_flag = 0;
1096 for (i = 0; i < 5; i++)
1098 c = gfc_next_char_literal (NONSTRING);
1100 switch (c)
1102 case ' ':
1103 break;
1105 case '0':
1106 case '1':
1107 case '2':
1108 case '3':
1109 case '4':
1110 case '5':
1111 case '6':
1112 case '7':
1113 case '8':
1114 case '9':
1115 label = label * 10 + ((unsigned char) c - '0');
1116 label_locus = gfc_current_locus;
1117 digit_flag = 1;
1118 break;
1120 /* Comments have already been skipped by the time we get
1121 here, except for GCC attributes and OpenMP directives. */
1123 case '*':
1124 c = gfc_next_char_literal (NONSTRING);
1126 if (TOLOWER (c) == 'g')
1128 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
1129 gcc_assert (TOLOWER (c) == "gcc$"[i]);
1131 return decode_gcc_attribute ();
1133 else if (c == '$')
1135 if ((flag_openmp || flag_openmp_simd)
1136 && !flag_openacc)
1138 if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
1139 return ST_NONE;
1140 return decode_omp_directive ();
1142 else if ((flag_openmp || flag_openmp_simd)
1143 && flag_openacc)
1145 c = gfc_next_char_literal(NONSTRING);
1146 if (c == 'o' || c == 'O')
1148 if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
1149 return ST_NONE;
1150 return decode_omp_directive ();
1152 else if (c == 'a' || c == 'A')
1154 if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
1155 return ST_NONE;
1156 return decode_oacc_directive ();
1159 else if (flag_openacc)
1161 if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
1162 return ST_NONE;
1163 return decode_oacc_directive ();
1166 /* FALLTHROUGH */
1168 /* Comments have already been skipped by the time we get
1169 here so don't bother checking for them. */
1171 default:
1172 gfc_buffer_error (false);
1173 gfc_error ("Non-numeric character in statement label at %C");
1174 return ST_NONE;
1178 if (digit_flag)
1180 if (label == 0)
1181 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1182 else
1184 /* We've found a valid statement label. */
1185 gfc_statement_label = gfc_get_st_label (label);
1189 /* Since this line starts a statement, it cannot be a continuation
1190 of a previous statement. If we see something here besides a
1191 space or zero, it must be a bad continuation line. */
1193 c = gfc_next_char_literal (NONSTRING);
1194 if (c == '\n')
1195 goto blank_line;
1197 if (c != ' ' && c != '0')
1199 gfc_buffer_error (false);
1200 gfc_error ("Bad continuation line at %C");
1201 return ST_NONE;
1204 /* Now that we've taken care of the statement label columns, we have
1205 to make sure that the first nonblank character is not a '!'. If
1206 it is, the rest of the line is a comment. */
1210 loc = gfc_current_locus;
1211 c = gfc_next_char_literal (NONSTRING);
1213 while (gfc_is_whitespace (c));
1215 if (c == '!')
1216 goto blank_line;
1217 gfc_current_locus = loc;
1219 if (c == ';')
1221 if (digit_flag)
1222 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1223 else if (!(gfc_option.allow_std & GFC_STD_F2008))
1224 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1225 "statement");
1226 return ST_NONE;
1229 if (gfc_match_eos () == MATCH_YES)
1230 goto blank_line;
1232 /* At this point, we've got a nonblank statement to parse. */
1233 return decode_statement ();
1235 blank_line:
1236 if (digit_flag)
1237 gfc_warning_now (0, "Ignoring statement label in empty statement at %L",
1238 &label_locus);
1240 gfc_current_locus.lb->truncated = 0;
1241 gfc_advance_line ();
1242 return ST_NONE;
1246 /* Return the next non-ST_NONE statement to the caller. We also worry
1247 about including files and the ends of include files at this stage. */
1249 static gfc_statement
1250 next_statement (void)
1252 gfc_statement st;
1253 locus old_locus;
1255 gfc_enforce_clean_symbol_state ();
1257 gfc_new_block = NULL;
1259 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
1260 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
1261 gfc_current_ns->old_data = gfc_current_ns->data;
1262 for (;;)
1264 gfc_statement_label = NULL;
1265 gfc_buffer_error (true);
1267 if (gfc_at_eol ())
1268 gfc_advance_line ();
1270 gfc_skip_comments ();
1272 if (gfc_at_end ())
1274 st = ST_NONE;
1275 break;
1278 if (gfc_define_undef_line ())
1279 continue;
1281 old_locus = gfc_current_locus;
1283 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
1285 if (st != ST_NONE)
1286 break;
1289 gfc_buffer_error (false);
1291 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
1293 gfc_free_st_label (gfc_statement_label);
1294 gfc_statement_label = NULL;
1295 gfc_current_locus = old_locus;
1298 if (st != ST_NONE)
1299 check_statement_label (st);
1301 return st;
1305 /****************************** Parser ***********************************/
1307 /* The parser subroutines are of type 'try' that fail if the file ends
1308 unexpectedly. */
1310 /* Macros that expand to case-labels for various classes of
1311 statements. Start with executable statements that directly do
1312 things. */
1314 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1315 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1316 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1317 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1318 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1319 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1320 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1321 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1322 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1323 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1324 case ST_OMP_TARGET_UPDATE: case ST_ERROR_STOP: case ST_SYNC_ALL: \
1325 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1326 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1327 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1329 /* Statements that mark other executable statements. */
1331 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1332 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1333 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1334 case ST_OMP_PARALLEL: \
1335 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1336 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1337 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1338 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1339 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1340 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1341 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1342 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1343 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1344 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1345 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1346 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1347 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1348 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1349 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1350 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1351 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: \
1352 case ST_CRITICAL: \
1353 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1354 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: case ST_OACC_KERNELS_LOOP
1356 /* Declaration statements */
1358 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1359 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1360 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
1361 case ST_PROCEDURE: case ST_OMP_DECLARE_SIMD: case ST_OMP_DECLARE_REDUCTION: \
1362 case ST_OMP_DECLARE_TARGET: case ST_OACC_ROUTINE
1364 /* Block end statements. Errors associated with interchanging these
1365 are detected in gfc_match_end(). */
1367 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1368 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1369 case ST_END_BLOCK: case ST_END_ASSOCIATE
1372 /* Push a new state onto the stack. */
1374 static void
1375 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
1377 p->state = new_state;
1378 p->previous = gfc_state_stack;
1379 p->sym = sym;
1380 p->head = p->tail = NULL;
1381 p->do_variable = NULL;
1382 if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
1383 p->ext.oacc_declare_clauses = NULL;
1385 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1386 construct statement was accepted right before pushing the state. Thus,
1387 the construct's gfc_code is available as tail of the parent state. */
1388 gcc_assert (gfc_state_stack);
1389 p->construct = gfc_state_stack->tail;
1391 gfc_state_stack = p;
1395 /* Pop the current state. */
1396 static void
1397 pop_state (void)
1399 gfc_state_stack = gfc_state_stack->previous;
1403 /* Try to find the given state in the state stack. */
1405 bool
1406 gfc_find_state (gfc_compile_state state)
1408 gfc_state_data *p;
1410 for (p = gfc_state_stack; p; p = p->previous)
1411 if (p->state == state)
1412 break;
1414 return (p == NULL) ? false : true;
1418 /* Starts a new level in the statement list. */
1420 static gfc_code *
1421 new_level (gfc_code *q)
1423 gfc_code *p;
1425 p = q->block = gfc_get_code (EXEC_NOP);
1427 gfc_state_stack->head = gfc_state_stack->tail = p;
1429 return p;
1433 /* Add the current new_st code structure and adds it to the current
1434 program unit. As a side-effect, it zeroes the new_st. */
1436 static gfc_code *
1437 add_statement (void)
1439 gfc_code *p;
1441 p = XCNEW (gfc_code);
1442 *p = new_st;
1444 p->loc = gfc_current_locus;
1446 if (gfc_state_stack->head == NULL)
1447 gfc_state_stack->head = p;
1448 else
1449 gfc_state_stack->tail->next = p;
1451 while (p->next != NULL)
1452 p = p->next;
1454 gfc_state_stack->tail = p;
1456 gfc_clear_new_st ();
1458 return p;
1462 /* Frees everything associated with the current statement. */
1464 static void
1465 undo_new_statement (void)
1467 gfc_free_statements (new_st.block);
1468 gfc_free_statements (new_st.next);
1469 gfc_free_statement (&new_st);
1470 gfc_clear_new_st ();
1474 /* If the current statement has a statement label, make sure that it
1475 is allowed to, or should have one. */
1477 static void
1478 check_statement_label (gfc_statement st)
1480 gfc_sl_type type;
1482 if (gfc_statement_label == NULL)
1484 if (st == ST_FORMAT)
1485 gfc_error ("FORMAT statement at %L does not have a statement label",
1486 &new_st.loc);
1487 return;
1490 switch (st)
1492 case ST_END_PROGRAM:
1493 case ST_END_FUNCTION:
1494 case ST_END_SUBROUTINE:
1495 case ST_ENDDO:
1496 case ST_ENDIF:
1497 case ST_END_SELECT:
1498 case ST_END_CRITICAL:
1499 case ST_END_BLOCK:
1500 case ST_END_ASSOCIATE:
1501 case_executable:
1502 case_exec_markers:
1503 if (st == ST_ENDDO || st == ST_CONTINUE)
1504 type = ST_LABEL_DO_TARGET;
1505 else
1506 type = ST_LABEL_TARGET;
1507 break;
1509 case ST_FORMAT:
1510 type = ST_LABEL_FORMAT;
1511 break;
1513 /* Statement labels are not restricted from appearing on a
1514 particular line. However, there are plenty of situations
1515 where the resulting label can't be referenced. */
1517 default:
1518 type = ST_LABEL_BAD_TARGET;
1519 break;
1522 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1524 new_st.here = gfc_statement_label;
1528 /* Figures out what the enclosing program unit is. This will be a
1529 function, subroutine, program, block data or module. */
1531 gfc_state_data *
1532 gfc_enclosing_unit (gfc_compile_state * result)
1534 gfc_state_data *p;
1536 for (p = gfc_state_stack; p; p = p->previous)
1537 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1538 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
1539 || p->state == COMP_PROGRAM)
1542 if (result != NULL)
1543 *result = p->state;
1544 return p;
1547 if (result != NULL)
1548 *result = COMP_PROGRAM;
1549 return NULL;
1553 /* Translate a statement enum to a string. */
1555 const char *
1556 gfc_ascii_statement (gfc_statement st)
1558 const char *p;
1560 switch (st)
1562 case ST_ARITHMETIC_IF:
1563 p = _("arithmetic IF");
1564 break;
1565 case ST_ALLOCATE:
1566 p = "ALLOCATE";
1567 break;
1568 case ST_ASSOCIATE:
1569 p = "ASSOCIATE";
1570 break;
1571 case ST_ATTR_DECL:
1572 p = _("attribute declaration");
1573 break;
1574 case ST_BACKSPACE:
1575 p = "BACKSPACE";
1576 break;
1577 case ST_BLOCK:
1578 p = "BLOCK";
1579 break;
1580 case ST_BLOCK_DATA:
1581 p = "BLOCK DATA";
1582 break;
1583 case ST_CALL:
1584 p = "CALL";
1585 break;
1586 case ST_CASE:
1587 p = "CASE";
1588 break;
1589 case ST_CLOSE:
1590 p = "CLOSE";
1591 break;
1592 case ST_COMMON:
1593 p = "COMMON";
1594 break;
1595 case ST_CONTINUE:
1596 p = "CONTINUE";
1597 break;
1598 case ST_CONTAINS:
1599 p = "CONTAINS";
1600 break;
1601 case ST_CRITICAL:
1602 p = "CRITICAL";
1603 break;
1604 case ST_CYCLE:
1605 p = "CYCLE";
1606 break;
1607 case ST_DATA_DECL:
1608 p = _("data declaration");
1609 break;
1610 case ST_DATA:
1611 p = "DATA";
1612 break;
1613 case ST_DEALLOCATE:
1614 p = "DEALLOCATE";
1615 break;
1616 case ST_DERIVED_DECL:
1617 p = _("derived type declaration");
1618 break;
1619 case ST_DO:
1620 p = "DO";
1621 break;
1622 case ST_ELSE:
1623 p = "ELSE";
1624 break;
1625 case ST_ELSEIF:
1626 p = "ELSE IF";
1627 break;
1628 case ST_ELSEWHERE:
1629 p = "ELSEWHERE";
1630 break;
1631 case ST_END_ASSOCIATE:
1632 p = "END ASSOCIATE";
1633 break;
1634 case ST_END_BLOCK:
1635 p = "END BLOCK";
1636 break;
1637 case ST_END_BLOCK_DATA:
1638 p = "END BLOCK DATA";
1639 break;
1640 case ST_END_CRITICAL:
1641 p = "END CRITICAL";
1642 break;
1643 case ST_ENDDO:
1644 p = "END DO";
1645 break;
1646 case ST_END_FILE:
1647 p = "END FILE";
1648 break;
1649 case ST_END_FORALL:
1650 p = "END FORALL";
1651 break;
1652 case ST_END_FUNCTION:
1653 p = "END FUNCTION";
1654 break;
1655 case ST_ENDIF:
1656 p = "END IF";
1657 break;
1658 case ST_END_INTERFACE:
1659 p = "END INTERFACE";
1660 break;
1661 case ST_END_MODULE:
1662 p = "END MODULE";
1663 break;
1664 case ST_END_PROGRAM:
1665 p = "END PROGRAM";
1666 break;
1667 case ST_END_SELECT:
1668 p = "END SELECT";
1669 break;
1670 case ST_END_SUBROUTINE:
1671 p = "END SUBROUTINE";
1672 break;
1673 case ST_END_WHERE:
1674 p = "END WHERE";
1675 break;
1676 case ST_END_TYPE:
1677 p = "END TYPE";
1678 break;
1679 case ST_ENTRY:
1680 p = "ENTRY";
1681 break;
1682 case ST_EQUIVALENCE:
1683 p = "EQUIVALENCE";
1684 break;
1685 case ST_ERROR_STOP:
1686 p = "ERROR STOP";
1687 break;
1688 case ST_EXIT:
1689 p = "EXIT";
1690 break;
1691 case ST_FLUSH:
1692 p = "FLUSH";
1693 break;
1694 case ST_FORALL_BLOCK: /* Fall through */
1695 case ST_FORALL:
1696 p = "FORALL";
1697 break;
1698 case ST_FORMAT:
1699 p = "FORMAT";
1700 break;
1701 case ST_FUNCTION:
1702 p = "FUNCTION";
1703 break;
1704 case ST_GENERIC:
1705 p = "GENERIC";
1706 break;
1707 case ST_GOTO:
1708 p = "GOTO";
1709 break;
1710 case ST_IF_BLOCK:
1711 p = _("block IF");
1712 break;
1713 case ST_IMPLICIT:
1714 p = "IMPLICIT";
1715 break;
1716 case ST_IMPLICIT_NONE:
1717 p = "IMPLICIT NONE";
1718 break;
1719 case ST_IMPLIED_ENDDO:
1720 p = _("implied END DO");
1721 break;
1722 case ST_IMPORT:
1723 p = "IMPORT";
1724 break;
1725 case ST_INQUIRE:
1726 p = "INQUIRE";
1727 break;
1728 case ST_INTERFACE:
1729 p = "INTERFACE";
1730 break;
1731 case ST_LOCK:
1732 p = "LOCK";
1733 break;
1734 case ST_PARAMETER:
1735 p = "PARAMETER";
1736 break;
1737 case ST_PRIVATE:
1738 p = "PRIVATE";
1739 break;
1740 case ST_PUBLIC:
1741 p = "PUBLIC";
1742 break;
1743 case ST_MODULE:
1744 p = "MODULE";
1745 break;
1746 case ST_PAUSE:
1747 p = "PAUSE";
1748 break;
1749 case ST_MODULE_PROC:
1750 p = "MODULE PROCEDURE";
1751 break;
1752 case ST_NAMELIST:
1753 p = "NAMELIST";
1754 break;
1755 case ST_NULLIFY:
1756 p = "NULLIFY";
1757 break;
1758 case ST_OPEN:
1759 p = "OPEN";
1760 break;
1761 case ST_PROGRAM:
1762 p = "PROGRAM";
1763 break;
1764 case ST_PROCEDURE:
1765 p = "PROCEDURE";
1766 break;
1767 case ST_READ:
1768 p = "READ";
1769 break;
1770 case ST_RETURN:
1771 p = "RETURN";
1772 break;
1773 case ST_REWIND:
1774 p = "REWIND";
1775 break;
1776 case ST_STOP:
1777 p = "STOP";
1778 break;
1779 case ST_SYNC_ALL:
1780 p = "SYNC ALL";
1781 break;
1782 case ST_SYNC_IMAGES:
1783 p = "SYNC IMAGES";
1784 break;
1785 case ST_SYNC_MEMORY:
1786 p = "SYNC MEMORY";
1787 break;
1788 case ST_SUBROUTINE:
1789 p = "SUBROUTINE";
1790 break;
1791 case ST_TYPE:
1792 p = "TYPE";
1793 break;
1794 case ST_UNLOCK:
1795 p = "UNLOCK";
1796 break;
1797 case ST_USE:
1798 p = "USE";
1799 break;
1800 case ST_WHERE_BLOCK: /* Fall through */
1801 case ST_WHERE:
1802 p = "WHERE";
1803 break;
1804 case ST_WAIT:
1805 p = "WAIT";
1806 break;
1807 case ST_WRITE:
1808 p = "WRITE";
1809 break;
1810 case ST_ASSIGNMENT:
1811 p = _("assignment");
1812 break;
1813 case ST_POINTER_ASSIGNMENT:
1814 p = _("pointer assignment");
1815 break;
1816 case ST_SELECT_CASE:
1817 p = "SELECT CASE";
1818 break;
1819 case ST_SELECT_TYPE:
1820 p = "SELECT TYPE";
1821 break;
1822 case ST_TYPE_IS:
1823 p = "TYPE IS";
1824 break;
1825 case ST_CLASS_IS:
1826 p = "CLASS IS";
1827 break;
1828 case ST_SEQUENCE:
1829 p = "SEQUENCE";
1830 break;
1831 case ST_SIMPLE_IF:
1832 p = _("simple IF");
1833 break;
1834 case ST_STATEMENT_FUNCTION:
1835 p = "STATEMENT FUNCTION";
1836 break;
1837 case ST_LABEL_ASSIGNMENT:
1838 p = "LABEL ASSIGNMENT";
1839 break;
1840 case ST_ENUM:
1841 p = "ENUM DEFINITION";
1842 break;
1843 case ST_ENUMERATOR:
1844 p = "ENUMERATOR DEFINITION";
1845 break;
1846 case ST_END_ENUM:
1847 p = "END ENUM";
1848 break;
1849 case ST_OACC_PARALLEL_LOOP:
1850 p = "!$ACC PARALLEL LOOP";
1851 break;
1852 case ST_OACC_END_PARALLEL_LOOP:
1853 p = "!$ACC END PARALLEL LOOP";
1854 break;
1855 case ST_OACC_PARALLEL:
1856 p = "!$ACC PARALLEL";
1857 break;
1858 case ST_OACC_END_PARALLEL:
1859 p = "!$ACC END PARALLEL";
1860 break;
1861 case ST_OACC_KERNELS:
1862 p = "!$ACC KERNELS";
1863 break;
1864 case ST_OACC_END_KERNELS:
1865 p = "!$ACC END KERNELS";
1866 break;
1867 case ST_OACC_KERNELS_LOOP:
1868 p = "!$ACC KERNELS LOOP";
1869 break;
1870 case ST_OACC_END_KERNELS_LOOP:
1871 p = "!$ACC END KERNELS LOOP";
1872 break;
1873 case ST_OACC_DATA:
1874 p = "!$ACC DATA";
1875 break;
1876 case ST_OACC_END_DATA:
1877 p = "!$ACC END DATA";
1878 break;
1879 case ST_OACC_HOST_DATA:
1880 p = "!$ACC HOST_DATA";
1881 break;
1882 case ST_OACC_END_HOST_DATA:
1883 p = "!$ACC END HOST_DATA";
1884 break;
1885 case ST_OACC_LOOP:
1886 p = "!$ACC LOOP";
1887 break;
1888 case ST_OACC_END_LOOP:
1889 p = "!$ACC END LOOP";
1890 break;
1891 case ST_OACC_DECLARE:
1892 p = "!$ACC DECLARE";
1893 break;
1894 case ST_OACC_UPDATE:
1895 p = "!$ACC UPDATE";
1896 break;
1897 case ST_OACC_WAIT:
1898 p = "!$ACC WAIT";
1899 break;
1900 case ST_OACC_CACHE:
1901 p = "!$ACC CACHE";
1902 break;
1903 case ST_OACC_ENTER_DATA:
1904 p = "!$ACC ENTER DATA";
1905 break;
1906 case ST_OACC_EXIT_DATA:
1907 p = "!$ACC EXIT DATA";
1908 break;
1909 case ST_OACC_ROUTINE:
1910 p = "!$ACC ROUTINE";
1911 break;
1912 case ST_OMP_ATOMIC:
1913 p = "!$OMP ATOMIC";
1914 break;
1915 case ST_OMP_BARRIER:
1916 p = "!$OMP BARRIER";
1917 break;
1918 case ST_OMP_CANCEL:
1919 p = "!$OMP CANCEL";
1920 break;
1921 case ST_OMP_CANCELLATION_POINT:
1922 p = "!$OMP CANCELLATION POINT";
1923 break;
1924 case ST_OMP_CRITICAL:
1925 p = "!$OMP CRITICAL";
1926 break;
1927 case ST_OMP_DECLARE_REDUCTION:
1928 p = "!$OMP DECLARE REDUCTION";
1929 break;
1930 case ST_OMP_DECLARE_SIMD:
1931 p = "!$OMP DECLARE SIMD";
1932 break;
1933 case ST_OMP_DECLARE_TARGET:
1934 p = "!$OMP DECLARE TARGET";
1935 break;
1936 case ST_OMP_DISTRIBUTE:
1937 p = "!$OMP DISTRIBUTE";
1938 break;
1939 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
1940 p = "!$OMP DISTRIBUTE PARALLEL DO";
1941 break;
1942 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
1943 p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
1944 break;
1945 case ST_OMP_DISTRIBUTE_SIMD:
1946 p = "!$OMP DISTRIBUTE SIMD";
1947 break;
1948 case ST_OMP_DO:
1949 p = "!$OMP DO";
1950 break;
1951 case ST_OMP_DO_SIMD:
1952 p = "!$OMP DO SIMD";
1953 break;
1954 case ST_OMP_END_ATOMIC:
1955 p = "!$OMP END ATOMIC";
1956 break;
1957 case ST_OMP_END_CRITICAL:
1958 p = "!$OMP END CRITICAL";
1959 break;
1960 case ST_OMP_END_DISTRIBUTE:
1961 p = "!$OMP END DISTRIBUTE";
1962 break;
1963 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
1964 p = "!$OMP END DISTRIBUTE PARALLEL DO";
1965 break;
1966 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
1967 p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
1968 break;
1969 case ST_OMP_END_DISTRIBUTE_SIMD:
1970 p = "!$OMP END DISTRIBUTE SIMD";
1971 break;
1972 case ST_OMP_END_DO:
1973 p = "!$OMP END DO";
1974 break;
1975 case ST_OMP_END_DO_SIMD:
1976 p = "!$OMP END DO SIMD";
1977 break;
1978 case ST_OMP_END_SIMD:
1979 p = "!$OMP END SIMD";
1980 break;
1981 case ST_OMP_END_MASTER:
1982 p = "!$OMP END MASTER";
1983 break;
1984 case ST_OMP_END_ORDERED:
1985 p = "!$OMP END ORDERED";
1986 break;
1987 case ST_OMP_END_PARALLEL:
1988 p = "!$OMP END PARALLEL";
1989 break;
1990 case ST_OMP_END_PARALLEL_DO:
1991 p = "!$OMP END PARALLEL DO";
1992 break;
1993 case ST_OMP_END_PARALLEL_DO_SIMD:
1994 p = "!$OMP END PARALLEL DO SIMD";
1995 break;
1996 case ST_OMP_END_PARALLEL_SECTIONS:
1997 p = "!$OMP END PARALLEL SECTIONS";
1998 break;
1999 case ST_OMP_END_PARALLEL_WORKSHARE:
2000 p = "!$OMP END PARALLEL WORKSHARE";
2001 break;
2002 case ST_OMP_END_SECTIONS:
2003 p = "!$OMP END SECTIONS";
2004 break;
2005 case ST_OMP_END_SINGLE:
2006 p = "!$OMP END SINGLE";
2007 break;
2008 case ST_OMP_END_TASK:
2009 p = "!$OMP END TASK";
2010 break;
2011 case ST_OMP_END_TARGET:
2012 p = "!$OMP END TARGET";
2013 break;
2014 case ST_OMP_END_TARGET_DATA:
2015 p = "!$OMP END TARGET DATA";
2016 break;
2017 case ST_OMP_END_TARGET_TEAMS:
2018 p = "!$OMP END TARGET TEAMS";
2019 break;
2020 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
2021 p = "!$OMP END TARGET TEAMS DISTRIBUTE";
2022 break;
2023 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2024 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2025 break;
2026 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2027 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2028 break;
2029 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
2030 p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2031 break;
2032 case ST_OMP_END_TASKGROUP:
2033 p = "!$OMP END TASKGROUP";
2034 break;
2035 case ST_OMP_END_TEAMS:
2036 p = "!$OMP END TEAMS";
2037 break;
2038 case ST_OMP_END_TEAMS_DISTRIBUTE:
2039 p = "!$OMP END TEAMS DISTRIBUTE";
2040 break;
2041 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
2042 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2043 break;
2044 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2045 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2046 break;
2047 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
2048 p = "!$OMP END TEAMS DISTRIBUTE SIMD";
2049 break;
2050 case ST_OMP_END_WORKSHARE:
2051 p = "!$OMP END WORKSHARE";
2052 break;
2053 case ST_OMP_FLUSH:
2054 p = "!$OMP FLUSH";
2055 break;
2056 case ST_OMP_MASTER:
2057 p = "!$OMP MASTER";
2058 break;
2059 case ST_OMP_ORDERED:
2060 p = "!$OMP ORDERED";
2061 break;
2062 case ST_OMP_PARALLEL:
2063 p = "!$OMP PARALLEL";
2064 break;
2065 case ST_OMP_PARALLEL_DO:
2066 p = "!$OMP PARALLEL DO";
2067 break;
2068 case ST_OMP_PARALLEL_DO_SIMD:
2069 p = "!$OMP PARALLEL DO SIMD";
2070 break;
2071 case ST_OMP_PARALLEL_SECTIONS:
2072 p = "!$OMP PARALLEL SECTIONS";
2073 break;
2074 case ST_OMP_PARALLEL_WORKSHARE:
2075 p = "!$OMP PARALLEL WORKSHARE";
2076 break;
2077 case ST_OMP_SECTIONS:
2078 p = "!$OMP SECTIONS";
2079 break;
2080 case ST_OMP_SECTION:
2081 p = "!$OMP SECTION";
2082 break;
2083 case ST_OMP_SIMD:
2084 p = "!$OMP SIMD";
2085 break;
2086 case ST_OMP_SINGLE:
2087 p = "!$OMP SINGLE";
2088 break;
2089 case ST_OMP_TARGET:
2090 p = "!$OMP TARGET";
2091 break;
2092 case ST_OMP_TARGET_DATA:
2093 p = "!$OMP TARGET DATA";
2094 break;
2095 case ST_OMP_TARGET_TEAMS:
2096 p = "!$OMP TARGET TEAMS";
2097 break;
2098 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
2099 p = "!$OMP TARGET TEAMS DISTRIBUTE";
2100 break;
2101 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2102 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2103 break;
2104 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2105 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2106 break;
2107 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2108 p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2109 break;
2110 case ST_OMP_TARGET_UPDATE:
2111 p = "!$OMP TARGET UPDATE";
2112 break;
2113 case ST_OMP_TASK:
2114 p = "!$OMP TASK";
2115 break;
2116 case ST_OMP_TASKGROUP:
2117 p = "!$OMP TASKGROUP";
2118 break;
2119 case ST_OMP_TASKWAIT:
2120 p = "!$OMP TASKWAIT";
2121 break;
2122 case ST_OMP_TASKYIELD:
2123 p = "!$OMP TASKYIELD";
2124 break;
2125 case ST_OMP_TEAMS:
2126 p = "!$OMP TEAMS";
2127 break;
2128 case ST_OMP_TEAMS_DISTRIBUTE:
2129 p = "!$OMP TEAMS DISTRIBUTE";
2130 break;
2131 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2132 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2133 break;
2134 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2135 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2136 break;
2137 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
2138 p = "!$OMP TEAMS DISTRIBUTE SIMD";
2139 break;
2140 case ST_OMP_THREADPRIVATE:
2141 p = "!$OMP THREADPRIVATE";
2142 break;
2143 case ST_OMP_WORKSHARE:
2144 p = "!$OMP WORKSHARE";
2145 break;
2146 default:
2147 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2150 return p;
2154 /* Create a symbol for the main program and assign it to ns->proc_name. */
2156 static void
2157 main_program_symbol (gfc_namespace *ns, const char *name)
2159 gfc_symbol *main_program;
2160 symbol_attribute attr;
2162 gfc_get_symbol (name, ns, &main_program);
2163 gfc_clear_attr (&attr);
2164 attr.flavor = FL_PROGRAM;
2165 attr.proc = PROC_UNKNOWN;
2166 attr.subroutine = 1;
2167 attr.access = ACCESS_PUBLIC;
2168 attr.is_main_program = 1;
2169 main_program->attr = attr;
2170 main_program->declared_at = gfc_current_locus;
2171 ns->proc_name = main_program;
2172 gfc_commit_symbols ();
2176 /* Do whatever is necessary to accept the last statement. */
2178 static void
2179 accept_statement (gfc_statement st)
2181 switch (st)
2183 case ST_IMPLICIT_NONE:
2184 case ST_IMPLICIT:
2185 break;
2187 case ST_FUNCTION:
2188 case ST_SUBROUTINE:
2189 case ST_MODULE:
2190 gfc_current_ns->proc_name = gfc_new_block;
2191 break;
2193 /* If the statement is the end of a block, lay down a special code
2194 that allows a branch to the end of the block from within the
2195 construct. IF and SELECT are treated differently from DO
2196 (where EXEC_NOP is added inside the loop) for two
2197 reasons:
2198 1. END DO has a meaning in the sense that after a GOTO to
2199 it, the loop counter must be increased.
2200 2. IF blocks and SELECT blocks can consist of multiple
2201 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2202 Putting the label before the END IF would make the jump
2203 from, say, the ELSE IF block to the END IF illegal. */
2205 case ST_ENDIF:
2206 case ST_END_SELECT:
2207 case ST_END_CRITICAL:
2208 if (gfc_statement_label != NULL)
2210 new_st.op = EXEC_END_NESTED_BLOCK;
2211 add_statement ();
2213 break;
2215 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2216 one parallel block. Thus, we add the special code to the nested block
2217 itself, instead of the parent one. */
2218 case ST_END_BLOCK:
2219 case ST_END_ASSOCIATE:
2220 if (gfc_statement_label != NULL)
2222 new_st.op = EXEC_END_BLOCK;
2223 add_statement ();
2225 break;
2227 /* The end-of-program unit statements do not get the special
2228 marker and require a statement of some sort if they are a
2229 branch target. */
2231 case ST_END_PROGRAM:
2232 case ST_END_FUNCTION:
2233 case ST_END_SUBROUTINE:
2234 if (gfc_statement_label != NULL)
2236 new_st.op = EXEC_RETURN;
2237 add_statement ();
2239 else
2241 new_st.op = EXEC_END_PROCEDURE;
2242 add_statement ();
2245 break;
2247 case ST_ENTRY:
2248 case_executable:
2249 case_exec_markers:
2250 add_statement ();
2251 break;
2253 default:
2254 break;
2257 gfc_commit_symbols ();
2258 gfc_warning_check ();
2259 gfc_clear_new_st ();
2263 /* Undo anything tentative that has been built for the current
2264 statement. */
2266 static void
2267 reject_statement (void)
2269 /* Revert to the previous charlen chain. */
2270 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
2271 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
2273 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
2274 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
2276 gfc_reject_data (gfc_current_ns);
2278 gfc_new_block = NULL;
2279 gfc_undo_symbols ();
2280 gfc_clear_warning ();
2281 undo_new_statement ();
2285 /* Generic complaint about an out of order statement. We also do
2286 whatever is necessary to clean up. */
2288 static void
2289 unexpected_statement (gfc_statement st)
2291 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
2293 reject_statement ();
2297 /* Given the next statement seen by the matcher, make sure that it is
2298 in proper order with the last. This subroutine is initialized by
2299 calling it with an argument of ST_NONE. If there is a problem, we
2300 issue an error and return false. Otherwise we return true.
2302 Individual parsers need to verify that the statements seen are
2303 valid before calling here, i.e., ENTRY statements are not allowed in
2304 INTERFACE blocks. The following diagram is taken from the standard:
2306 +---------------------------------------+
2307 | program subroutine function module |
2308 +---------------------------------------+
2309 | use |
2310 +---------------------------------------+
2311 | import |
2312 +---------------------------------------+
2313 | | implicit none |
2314 | +-----------+------------------+
2315 | | parameter | implicit |
2316 | +-----------+------------------+
2317 | format | | derived type |
2318 | entry | parameter | interface |
2319 | | data | specification |
2320 | | | statement func |
2321 | +-----------+------------------+
2322 | | data | executable |
2323 +--------+-----------+------------------+
2324 | contains |
2325 +---------------------------------------+
2326 | internal module/subprogram |
2327 +---------------------------------------+
2328 | end |
2329 +---------------------------------------+
2333 enum state_order
2335 ORDER_START,
2336 ORDER_USE,
2337 ORDER_IMPORT,
2338 ORDER_IMPLICIT_NONE,
2339 ORDER_IMPLICIT,
2340 ORDER_SPEC,
2341 ORDER_EXEC
2344 typedef struct
2346 enum state_order state;
2347 gfc_statement last_statement;
2348 locus where;
2350 st_state;
2352 static bool
2353 verify_st_order (st_state *p, gfc_statement st, bool silent)
2356 switch (st)
2358 case ST_NONE:
2359 p->state = ORDER_START;
2360 break;
2362 case ST_USE:
2363 if (p->state > ORDER_USE)
2364 goto order;
2365 p->state = ORDER_USE;
2366 break;
2368 case ST_IMPORT:
2369 if (p->state > ORDER_IMPORT)
2370 goto order;
2371 p->state = ORDER_IMPORT;
2372 break;
2374 case ST_IMPLICIT_NONE:
2375 if (p->state > ORDER_IMPLICIT)
2376 goto order;
2378 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2379 statement disqualifies a USE but not an IMPLICIT NONE.
2380 Duplicate IMPLICIT NONEs are caught when the implicit types
2381 are set. */
2383 p->state = ORDER_IMPLICIT_NONE;
2384 break;
2386 case ST_IMPLICIT:
2387 if (p->state > ORDER_IMPLICIT)
2388 goto order;
2389 p->state = ORDER_IMPLICIT;
2390 break;
2392 case ST_FORMAT:
2393 case ST_ENTRY:
2394 if (p->state < ORDER_IMPLICIT_NONE)
2395 p->state = ORDER_IMPLICIT_NONE;
2396 break;
2398 case ST_PARAMETER:
2399 if (p->state >= ORDER_EXEC)
2400 goto order;
2401 if (p->state < ORDER_IMPLICIT)
2402 p->state = ORDER_IMPLICIT;
2403 break;
2405 case ST_DATA:
2406 if (p->state < ORDER_SPEC)
2407 p->state = ORDER_SPEC;
2408 break;
2410 case ST_PUBLIC:
2411 case ST_PRIVATE:
2412 case ST_DERIVED_DECL:
2413 case ST_OACC_DECLARE:
2414 case_decl:
2415 if (p->state >= ORDER_EXEC)
2416 goto order;
2417 if (p->state < ORDER_SPEC)
2418 p->state = ORDER_SPEC;
2419 break;
2421 case_executable:
2422 case_exec_markers:
2423 if (p->state < ORDER_EXEC)
2424 p->state = ORDER_EXEC;
2425 break;
2427 default:
2428 return false;
2431 /* All is well, record the statement in case we need it next time. */
2432 p->where = gfc_current_locus;
2433 p->last_statement = st;
2434 return true;
2436 order:
2437 if (!silent)
2438 gfc_error_1 ("%s statement at %C cannot follow %s statement at %L",
2439 gfc_ascii_statement (st),
2440 gfc_ascii_statement (p->last_statement), &p->where);
2442 return false;
2446 /* Handle an unexpected end of file. This is a show-stopper... */
2448 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
2450 static void
2451 unexpected_eof (void)
2453 gfc_state_data *p;
2455 gfc_error ("Unexpected end of file in %qs", gfc_source_file);
2457 /* Memory cleanup. Move to "second to last". */
2458 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
2459 p = p->previous);
2461 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
2462 gfc_done_2 ();
2464 longjmp (eof_buf, 1);
2468 /* Parse the CONTAINS section of a derived type definition. */
2470 gfc_access gfc_typebound_default_access;
2472 static bool
2473 parse_derived_contains (void)
2475 gfc_state_data s;
2476 bool seen_private = false;
2477 bool seen_comps = false;
2478 bool error_flag = false;
2479 bool to_finish;
2481 gcc_assert (gfc_current_state () == COMP_DERIVED);
2482 gcc_assert (gfc_current_block ());
2484 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2485 section. */
2486 if (gfc_current_block ()->attr.sequence)
2487 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2488 " section at %C", gfc_current_block ()->name);
2489 if (gfc_current_block ()->attr.is_bind_c)
2490 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2491 " section at %C", gfc_current_block ()->name);
2493 accept_statement (ST_CONTAINS);
2494 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
2496 gfc_typebound_default_access = ACCESS_PUBLIC;
2498 to_finish = false;
2499 while (!to_finish)
2501 gfc_statement st;
2502 st = next_statement ();
2503 switch (st)
2505 case ST_NONE:
2506 unexpected_eof ();
2507 break;
2509 case ST_DATA_DECL:
2510 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2511 goto error;
2513 case ST_PROCEDURE:
2514 if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
2515 goto error;
2517 accept_statement (ST_PROCEDURE);
2518 seen_comps = true;
2519 break;
2521 case ST_GENERIC:
2522 if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
2523 goto error;
2525 accept_statement (ST_GENERIC);
2526 seen_comps = true;
2527 break;
2529 case ST_FINAL:
2530 if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
2531 " at %C"))
2532 goto error;
2534 accept_statement (ST_FINAL);
2535 seen_comps = true;
2536 break;
2538 case ST_END_TYPE:
2539 to_finish = true;
2541 if (!seen_comps
2542 && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
2543 "at %C with empty CONTAINS section")))
2544 goto error;
2546 /* ST_END_TYPE is accepted by parse_derived after return. */
2547 break;
2549 case ST_PRIVATE:
2550 if (!gfc_find_state (COMP_MODULE))
2552 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2553 "a MODULE");
2554 goto error;
2557 if (seen_comps)
2559 gfc_error ("PRIVATE statement at %C must precede procedure"
2560 " bindings");
2561 goto error;
2564 if (seen_private)
2566 gfc_error ("Duplicate PRIVATE statement at %C");
2567 goto error;
2570 accept_statement (ST_PRIVATE);
2571 gfc_typebound_default_access = ACCESS_PRIVATE;
2572 seen_private = true;
2573 break;
2575 case ST_SEQUENCE:
2576 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2577 goto error;
2579 case ST_CONTAINS:
2580 gfc_error ("Already inside a CONTAINS block at %C");
2581 goto error;
2583 default:
2584 unexpected_statement (st);
2585 break;
2588 continue;
2590 error:
2591 error_flag = true;
2592 reject_statement ();
2595 pop_state ();
2596 gcc_assert (gfc_current_state () == COMP_DERIVED);
2598 return error_flag;
2602 /* Parse a derived type. */
2604 static void
2605 parse_derived (void)
2607 int compiling_type, seen_private, seen_sequence, seen_component;
2608 gfc_statement st;
2609 gfc_state_data s;
2610 gfc_symbol *sym;
2611 gfc_component *c, *lock_comp = NULL;
2613 accept_statement (ST_DERIVED_DECL);
2614 push_state (&s, COMP_DERIVED, gfc_new_block);
2616 gfc_new_block->component_access = ACCESS_PUBLIC;
2617 seen_private = 0;
2618 seen_sequence = 0;
2619 seen_component = 0;
2621 compiling_type = 1;
2623 while (compiling_type)
2625 st = next_statement ();
2626 switch (st)
2628 case ST_NONE:
2629 unexpected_eof ();
2631 case ST_DATA_DECL:
2632 case ST_PROCEDURE:
2633 accept_statement (st);
2634 seen_component = 1;
2635 break;
2637 case ST_FINAL:
2638 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2639 break;
2641 case ST_END_TYPE:
2642 endType:
2643 compiling_type = 0;
2645 if (!seen_component)
2646 gfc_notify_std (GFC_STD_F2003, "Derived type "
2647 "definition at %C without components");
2649 accept_statement (ST_END_TYPE);
2650 break;
2652 case ST_PRIVATE:
2653 if (!gfc_find_state (COMP_MODULE))
2655 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2656 "a MODULE");
2657 break;
2660 if (seen_component)
2662 gfc_error ("PRIVATE statement at %C must precede "
2663 "structure components");
2664 break;
2667 if (seen_private)
2668 gfc_error ("Duplicate PRIVATE statement at %C");
2670 s.sym->component_access = ACCESS_PRIVATE;
2672 accept_statement (ST_PRIVATE);
2673 seen_private = 1;
2674 break;
2676 case ST_SEQUENCE:
2677 if (seen_component)
2679 gfc_error ("SEQUENCE statement at %C must precede "
2680 "structure components");
2681 break;
2684 if (gfc_current_block ()->attr.sequence)
2685 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
2686 "TYPE statement");
2688 if (seen_sequence)
2690 gfc_error ("Duplicate SEQUENCE statement at %C");
2693 seen_sequence = 1;
2694 gfc_add_sequence (&gfc_current_block ()->attr,
2695 gfc_current_block ()->name, NULL);
2696 break;
2698 case ST_CONTAINS:
2699 gfc_notify_std (GFC_STD_F2003,
2700 "CONTAINS block in derived type"
2701 " definition at %C");
2703 accept_statement (ST_CONTAINS);
2704 parse_derived_contains ();
2705 goto endType;
2707 default:
2708 unexpected_statement (st);
2709 break;
2713 /* need to verify that all fields of the derived type are
2714 * interoperable with C if the type is declared to be bind(c)
2716 sym = gfc_current_block ();
2717 for (c = sym->components; c; c = c->next)
2719 bool coarray, lock_type, allocatable, pointer;
2720 coarray = lock_type = allocatable = pointer = false;
2722 /* Look for allocatable components. */
2723 if (c->attr.allocatable
2724 || (c->ts.type == BT_CLASS && c->attr.class_ok
2725 && CLASS_DATA (c)->attr.allocatable)
2726 || (c->ts.type == BT_DERIVED && !c->attr.pointer
2727 && c->ts.u.derived->attr.alloc_comp))
2729 allocatable = true;
2730 sym->attr.alloc_comp = 1;
2733 /* Look for pointer components. */
2734 if (c->attr.pointer
2735 || (c->ts.type == BT_CLASS && c->attr.class_ok
2736 && CLASS_DATA (c)->attr.class_pointer)
2737 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2739 pointer = true;
2740 sym->attr.pointer_comp = 1;
2743 /* Look for procedure pointer components. */
2744 if (c->attr.proc_pointer
2745 || (c->ts.type == BT_DERIVED
2746 && c->ts.u.derived->attr.proc_pointer_comp))
2747 sym->attr.proc_pointer_comp = 1;
2749 /* Looking for coarray components. */
2750 if (c->attr.codimension
2751 || (c->ts.type == BT_CLASS && c->attr.class_ok
2752 && CLASS_DATA (c)->attr.codimension))
2754 coarray = true;
2755 sym->attr.coarray_comp = 1;
2758 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
2759 && !c->attr.pointer)
2761 coarray = true;
2762 sym->attr.coarray_comp = 1;
2765 /* Looking for lock_type components. */
2766 if ((c->ts.type == BT_DERIVED
2767 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2768 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2769 || (c->ts.type == BT_CLASS && c->attr.class_ok
2770 && CLASS_DATA (c)->ts.u.derived->from_intmod
2771 == INTMOD_ISO_FORTRAN_ENV
2772 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2773 == ISOFORTRAN_LOCK_TYPE)
2774 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
2775 && !allocatable && !pointer))
2777 lock_type = 1;
2778 lock_comp = c;
2779 sym->attr.lock_comp = 1;
2782 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2783 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2784 unless there are nondirect [allocatable or pointer] components
2785 involved (cf. 1.3.33.1 and 1.3.33.3). */
2787 if (pointer && !coarray && lock_type)
2788 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2789 "codimension or be a subcomponent of a coarray, "
2790 "which is not possible as the component has the "
2791 "pointer attribute", c->name, &c->loc);
2792 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2793 && c->ts.u.derived->attr.lock_comp)
2794 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2795 "of type LOCK_TYPE, which must have a codimension or be a "
2796 "subcomponent of a coarray", c->name, &c->loc);
2798 if (lock_type && allocatable && !coarray)
2799 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2800 "a codimension", c->name, &c->loc);
2801 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
2802 && c->ts.u.derived->attr.lock_comp)
2803 gfc_error ("Allocatable component %s at %L must have a codimension as "
2804 "it has a noncoarray subcomponent of type LOCK_TYPE",
2805 c->name, &c->loc);
2807 if (sym->attr.coarray_comp && !coarray && lock_type)
2808 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2809 "subcomponent of type LOCK_TYPE must have a codimension or "
2810 "be a subcomponent of a coarray. (Variables of type %s may "
2811 "not have a codimension as already a coarray "
2812 "subcomponent exists)", c->name, &c->loc, sym->name);
2814 if (sym->attr.lock_comp && coarray && !lock_type)
2815 gfc_error_1 ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2816 "subcomponent of type LOCK_TYPE must have a codimension or "
2817 "be a subcomponent of a coarray. (Variables of type %s may "
2818 "not have a codimension as %s at %L has a codimension or a "
2819 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
2820 sym->name, c->name, &c->loc);
2822 /* Look for private components. */
2823 if (sym->component_access == ACCESS_PRIVATE
2824 || c->attr.access == ACCESS_PRIVATE
2825 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
2826 sym->attr.private_comp = 1;
2829 if (!seen_component)
2830 sym->attr.zero_comp = 1;
2832 pop_state ();
2836 /* Parse an ENUM. */
2838 static void
2839 parse_enum (void)
2841 gfc_statement st;
2842 int compiling_enum;
2843 gfc_state_data s;
2844 int seen_enumerator = 0;
2846 push_state (&s, COMP_ENUM, gfc_new_block);
2848 compiling_enum = 1;
2850 while (compiling_enum)
2852 st = next_statement ();
2853 switch (st)
2855 case ST_NONE:
2856 unexpected_eof ();
2857 break;
2859 case ST_ENUMERATOR:
2860 seen_enumerator = 1;
2861 accept_statement (st);
2862 break;
2864 case ST_END_ENUM:
2865 compiling_enum = 0;
2866 if (!seen_enumerator)
2867 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2868 accept_statement (st);
2869 break;
2871 default:
2872 gfc_free_enum_history ();
2873 unexpected_statement (st);
2874 break;
2877 pop_state ();
2881 /* Parse an interface. We must be able to deal with the possibility
2882 of recursive interfaces. The parse_spec() subroutine is mutually
2883 recursive with parse_interface(). */
2885 static gfc_statement parse_spec (gfc_statement);
2887 static void
2888 parse_interface (void)
2890 gfc_compile_state new_state = COMP_NONE, current_state;
2891 gfc_symbol *prog_unit, *sym;
2892 gfc_interface_info save;
2893 gfc_state_data s1, s2;
2894 gfc_statement st;
2896 accept_statement (ST_INTERFACE);
2898 current_interface.ns = gfc_current_ns;
2899 save = current_interface;
2901 sym = (current_interface.type == INTERFACE_GENERIC
2902 || current_interface.type == INTERFACE_USER_OP)
2903 ? gfc_new_block : NULL;
2905 push_state (&s1, COMP_INTERFACE, sym);
2906 current_state = COMP_NONE;
2908 loop:
2909 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
2911 st = next_statement ();
2912 switch (st)
2914 case ST_NONE:
2915 unexpected_eof ();
2917 case ST_SUBROUTINE:
2918 case ST_FUNCTION:
2919 if (st == ST_SUBROUTINE)
2920 new_state = COMP_SUBROUTINE;
2921 else if (st == ST_FUNCTION)
2922 new_state = COMP_FUNCTION;
2923 if (gfc_new_block->attr.pointer)
2925 gfc_new_block->attr.pointer = 0;
2926 gfc_new_block->attr.proc_pointer = 1;
2928 if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
2929 gfc_new_block->formal, NULL))
2931 reject_statement ();
2932 gfc_free_namespace (gfc_current_ns);
2933 goto loop;
2935 break;
2937 case ST_PROCEDURE:
2938 case ST_MODULE_PROC: /* The module procedure matcher makes
2939 sure the context is correct. */
2940 accept_statement (st);
2941 gfc_free_namespace (gfc_current_ns);
2942 goto loop;
2944 case ST_END_INTERFACE:
2945 gfc_free_namespace (gfc_current_ns);
2946 gfc_current_ns = current_interface.ns;
2947 goto done;
2949 default:
2950 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2951 gfc_ascii_statement (st));
2952 reject_statement ();
2953 gfc_free_namespace (gfc_current_ns);
2954 goto loop;
2958 /* Make sure that the generic name has the right attribute. */
2959 if (current_interface.type == INTERFACE_GENERIC
2960 && current_state == COMP_NONE)
2962 if (new_state == COMP_FUNCTION && sym)
2963 gfc_add_function (&sym->attr, sym->name, NULL);
2964 else if (new_state == COMP_SUBROUTINE && sym)
2965 gfc_add_subroutine (&sym->attr, sym->name, NULL);
2967 current_state = new_state;
2970 if (current_interface.type == INTERFACE_ABSTRACT)
2972 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
2973 if (gfc_is_intrinsic_typename (gfc_new_block->name))
2974 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
2975 "cannot be the same as an intrinsic type",
2976 gfc_new_block->name);
2979 push_state (&s2, new_state, gfc_new_block);
2980 accept_statement (st);
2981 prog_unit = gfc_new_block;
2982 prog_unit->formal_ns = gfc_current_ns;
2983 if (prog_unit == prog_unit->formal_ns->proc_name
2984 && prog_unit->ns != prog_unit->formal_ns)
2985 prog_unit->refs++;
2987 decl:
2988 /* Read data declaration statements. */
2989 st = parse_spec (ST_NONE);
2991 /* Since the interface block does not permit an IMPLICIT statement,
2992 the default type for the function or the result must be taken
2993 from the formal namespace. */
2994 if (new_state == COMP_FUNCTION)
2996 if (prog_unit->result == prog_unit
2997 && prog_unit->ts.type == BT_UNKNOWN)
2998 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
2999 else if (prog_unit->result != prog_unit
3000 && prog_unit->result->ts.type == BT_UNKNOWN)
3001 gfc_set_default_type (prog_unit->result, 1,
3002 prog_unit->formal_ns);
3005 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
3007 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3008 gfc_ascii_statement (st));
3009 reject_statement ();
3010 goto decl;
3013 /* Add EXTERNAL attribute to function or subroutine. */
3014 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
3015 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
3017 current_interface = save;
3018 gfc_add_interface (prog_unit);
3019 pop_state ();
3021 if (current_interface.ns
3022 && current_interface.ns->proc_name
3023 && strcmp (current_interface.ns->proc_name->name,
3024 prog_unit->name) == 0)
3025 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3026 "enclosing procedure", prog_unit->name,
3027 &current_interface.ns->proc_name->declared_at);
3029 goto loop;
3031 done:
3032 pop_state ();
3036 /* Associate function characteristics by going back to the function
3037 declaration and rematching the prefix. */
3039 static match
3040 match_deferred_characteristics (gfc_typespec * ts)
3042 locus loc;
3043 match m = MATCH_ERROR;
3044 char name[GFC_MAX_SYMBOL_LEN + 1];
3046 loc = gfc_current_locus;
3048 gfc_current_locus = gfc_current_block ()->declared_at;
3050 gfc_clear_error ();
3051 gfc_buffer_error (true);
3052 m = gfc_match_prefix (ts);
3053 gfc_buffer_error (false);
3055 if (ts->type == BT_DERIVED)
3057 ts->kind = 0;
3059 if (!ts->u.derived)
3060 m = MATCH_ERROR;
3063 /* Only permit one go at the characteristic association. */
3064 if (ts->kind == -1)
3065 ts->kind = 0;
3067 /* Set the function locus correctly. If we have not found the
3068 function name, there is an error. */
3069 if (m == MATCH_YES
3070 && gfc_match ("function% %n", name) == MATCH_YES
3071 && strcmp (name, gfc_current_block ()->name) == 0)
3073 gfc_current_block ()->declared_at = gfc_current_locus;
3074 gfc_commit_symbols ();
3076 else
3078 gfc_error_check ();
3079 gfc_undo_symbols ();
3082 gfc_current_locus =loc;
3083 return m;
3087 /* Check specification-expressions in the function result of the currently
3088 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3089 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3090 scope are not yet parsed so this has to be delayed up to parse_spec. */
3092 static void
3093 check_function_result_typed (void)
3095 gfc_typespec* ts = &gfc_current_ns->proc_name->result->ts;
3097 gcc_assert (gfc_current_state () == COMP_FUNCTION);
3098 gcc_assert (ts->type != BT_UNKNOWN);
3100 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3101 /* TODO: Extend when KIND type parameters are implemented. */
3102 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length)
3103 gfc_expr_check_typed (ts->u.cl->length, gfc_current_ns, true);
3107 /* Parse a set of specification statements. Returns the statement
3108 that doesn't fit. */
3110 static gfc_statement
3111 parse_spec (gfc_statement st)
3113 st_state ss;
3114 bool function_result_typed = false;
3115 bool bad_characteristic = false;
3116 gfc_typespec *ts;
3118 verify_st_order (&ss, ST_NONE, false);
3119 if (st == ST_NONE)
3120 st = next_statement ();
3122 /* If we are not inside a function or don't have a result specified so far,
3123 do nothing special about it. */
3124 if (gfc_current_state () != COMP_FUNCTION)
3125 function_result_typed = true;
3126 else
3128 gfc_symbol* proc = gfc_current_ns->proc_name;
3129 gcc_assert (proc);
3131 if (proc->result->ts.type == BT_UNKNOWN)
3132 function_result_typed = true;
3135 loop:
3137 /* If we're inside a BLOCK construct, some statements are disallowed.
3138 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3139 or VALUE are also disallowed, but they don't have a particular ST_*
3140 key so we have to check for them individually in their matcher routine. */
3141 if (gfc_current_state () == COMP_BLOCK)
3142 switch (st)
3144 case ST_IMPLICIT:
3145 case ST_IMPLICIT_NONE:
3146 case ST_NAMELIST:
3147 case ST_COMMON:
3148 case ST_EQUIVALENCE:
3149 case ST_STATEMENT_FUNCTION:
3150 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3151 gfc_ascii_statement (st));
3152 reject_statement ();
3153 break;
3155 default:
3156 break;
3158 else if (gfc_current_state () == COMP_BLOCK_DATA)
3159 /* Fortran 2008, C1116. */
3160 switch (st)
3162 case ST_DATA_DECL:
3163 case ST_COMMON:
3164 case ST_DATA:
3165 case ST_TYPE:
3166 case ST_END_BLOCK_DATA:
3167 case ST_ATTR_DECL:
3168 case ST_EQUIVALENCE:
3169 case ST_PARAMETER:
3170 case ST_IMPLICIT:
3171 case ST_IMPLICIT_NONE:
3172 case ST_DERIVED_DECL:
3173 case ST_USE:
3174 break;
3176 case ST_NONE:
3177 break;
3179 default:
3180 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3181 gfc_ascii_statement (st));
3182 reject_statement ();
3183 break;
3186 /* If we find a statement that can not be followed by an IMPLICIT statement
3187 (and thus we can expect to see none any further), type the function result
3188 if it has not yet been typed. Be careful not to give the END statement
3189 to verify_st_order! */
3190 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
3192 bool verify_now = false;
3194 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
3195 verify_now = true;
3196 else
3198 st_state dummyss;
3199 verify_st_order (&dummyss, ST_NONE, false);
3200 verify_st_order (&dummyss, st, false);
3202 if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
3203 verify_now = true;
3206 if (verify_now)
3208 check_function_result_typed ();
3209 function_result_typed = true;
3213 switch (st)
3215 case ST_NONE:
3216 unexpected_eof ();
3218 case ST_IMPLICIT_NONE:
3219 case ST_IMPLICIT:
3220 if (!function_result_typed)
3222 check_function_result_typed ();
3223 function_result_typed = true;
3225 goto declSt;
3227 case ST_FORMAT:
3228 case ST_ENTRY:
3229 case ST_DATA: /* Not allowed in interfaces */
3230 if (gfc_current_state () == COMP_INTERFACE)
3231 break;
3233 /* Fall through */
3235 case ST_USE:
3236 case ST_IMPORT:
3237 case ST_PARAMETER:
3238 case ST_PUBLIC:
3239 case ST_PRIVATE:
3240 case ST_DERIVED_DECL:
3241 case_decl:
3242 declSt:
3243 if (!verify_st_order (&ss, st, false))
3245 reject_statement ();
3246 st = next_statement ();
3247 goto loop;
3250 switch (st)
3252 case ST_INTERFACE:
3253 parse_interface ();
3254 break;
3256 case ST_DERIVED_DECL:
3257 parse_derived ();
3258 break;
3260 case ST_PUBLIC:
3261 case ST_PRIVATE:
3262 if (gfc_current_state () != COMP_MODULE)
3264 gfc_error ("%s statement must appear in a MODULE",
3265 gfc_ascii_statement (st));
3266 reject_statement ();
3267 break;
3270 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
3272 gfc_error ("%s statement at %C follows another accessibility "
3273 "specification", gfc_ascii_statement (st));
3274 reject_statement ();
3275 break;
3278 gfc_current_ns->default_access = (st == ST_PUBLIC)
3279 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3281 break;
3283 case ST_STATEMENT_FUNCTION:
3284 if (gfc_current_state () == COMP_MODULE)
3286 unexpected_statement (st);
3287 break;
3290 default:
3291 break;
3294 accept_statement (st);
3295 st = next_statement ();
3296 goto loop;
3298 case ST_ENUM:
3299 accept_statement (st);
3300 parse_enum();
3301 st = next_statement ();
3302 goto loop;
3304 case ST_GET_FCN_CHARACTERISTICS:
3305 /* This statement triggers the association of a function's result
3306 characteristics. */
3307 ts = &gfc_current_block ()->result->ts;
3308 if (match_deferred_characteristics (ts) != MATCH_YES)
3309 bad_characteristic = true;
3311 st = next_statement ();
3312 goto loop;
3314 case ST_OACC_DECLARE:
3315 if (!verify_st_order(&ss, st, false))
3317 reject_statement ();
3318 st = next_statement ();
3319 goto loop;
3321 if (gfc_state_stack->ext.oacc_declare_clauses == NULL)
3322 gfc_state_stack->ext.oacc_declare_clauses = new_st.ext.omp_clauses;
3323 accept_statement (st);
3324 st = next_statement ();
3325 goto loop;
3327 default:
3328 break;
3331 /* If match_deferred_characteristics failed, then there is an error. */
3332 if (bad_characteristic)
3334 ts = &gfc_current_block ()->result->ts;
3335 if (ts->type != BT_DERIVED)
3336 gfc_error ("Bad kind expression for function %qs at %L",
3337 gfc_current_block ()->name,
3338 &gfc_current_block ()->declared_at);
3339 else
3340 gfc_error ("The type for function %qs at %L is not accessible",
3341 gfc_current_block ()->name,
3342 &gfc_current_block ()->declared_at);
3344 gfc_current_block ()->ts.kind = 0;
3345 /* Keep the derived type; if it's bad, it will be discovered later. */
3346 if (!(ts->type == BT_DERIVED && ts->u.derived))
3347 ts->type = BT_UNKNOWN;
3350 return st;
3354 /* Parse a WHERE block, (not a simple WHERE statement). */
3356 static void
3357 parse_where_block (void)
3359 int seen_empty_else;
3360 gfc_code *top, *d;
3361 gfc_state_data s;
3362 gfc_statement st;
3364 accept_statement (ST_WHERE_BLOCK);
3365 top = gfc_state_stack->tail;
3367 push_state (&s, COMP_WHERE, gfc_new_block);
3369 d = add_statement ();
3370 d->expr1 = top->expr1;
3371 d->op = EXEC_WHERE;
3373 top->expr1 = NULL;
3374 top->block = d;
3376 seen_empty_else = 0;
3380 st = next_statement ();
3381 switch (st)
3383 case ST_NONE:
3384 unexpected_eof ();
3386 case ST_WHERE_BLOCK:
3387 parse_where_block ();
3388 break;
3390 case ST_ASSIGNMENT:
3391 case ST_WHERE:
3392 accept_statement (st);
3393 break;
3395 case ST_ELSEWHERE:
3396 if (seen_empty_else)
3398 gfc_error ("ELSEWHERE statement at %C follows previous "
3399 "unmasked ELSEWHERE");
3400 reject_statement ();
3401 break;
3404 if (new_st.expr1 == NULL)
3405 seen_empty_else = 1;
3407 d = new_level (gfc_state_stack->head);
3408 d->op = EXEC_WHERE;
3409 d->expr1 = new_st.expr1;
3411 accept_statement (st);
3413 break;
3415 case ST_END_WHERE:
3416 accept_statement (st);
3417 break;
3419 default:
3420 gfc_error ("Unexpected %s statement in WHERE block at %C",
3421 gfc_ascii_statement (st));
3422 reject_statement ();
3423 break;
3426 while (st != ST_END_WHERE);
3428 pop_state ();
3432 /* Parse a FORALL block (not a simple FORALL statement). */
3434 static void
3435 parse_forall_block (void)
3437 gfc_code *top, *d;
3438 gfc_state_data s;
3439 gfc_statement st;
3441 accept_statement (ST_FORALL_BLOCK);
3442 top = gfc_state_stack->tail;
3444 push_state (&s, COMP_FORALL, gfc_new_block);
3446 d = add_statement ();
3447 d->op = EXEC_FORALL;
3448 top->block = d;
3452 st = next_statement ();
3453 switch (st)
3456 case ST_ASSIGNMENT:
3457 case ST_POINTER_ASSIGNMENT:
3458 case ST_WHERE:
3459 case ST_FORALL:
3460 accept_statement (st);
3461 break;
3463 case ST_WHERE_BLOCK:
3464 parse_where_block ();
3465 break;
3467 case ST_FORALL_BLOCK:
3468 parse_forall_block ();
3469 break;
3471 case ST_END_FORALL:
3472 accept_statement (st);
3473 break;
3475 case ST_NONE:
3476 unexpected_eof ();
3478 default:
3479 gfc_error ("Unexpected %s statement in FORALL block at %C",
3480 gfc_ascii_statement (st));
3482 reject_statement ();
3483 break;
3486 while (st != ST_END_FORALL);
3488 pop_state ();
3492 static gfc_statement parse_executable (gfc_statement);
3494 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
3496 static void
3497 parse_if_block (void)
3499 gfc_code *top, *d;
3500 gfc_statement st;
3501 locus else_locus;
3502 gfc_state_data s;
3503 int seen_else;
3505 seen_else = 0;
3506 accept_statement (ST_IF_BLOCK);
3508 top = gfc_state_stack->tail;
3509 push_state (&s, COMP_IF, gfc_new_block);
3511 new_st.op = EXEC_IF;
3512 d = add_statement ();
3514 d->expr1 = top->expr1;
3515 top->expr1 = NULL;
3516 top->block = d;
3520 st = parse_executable (ST_NONE);
3522 switch (st)
3524 case ST_NONE:
3525 unexpected_eof ();
3527 case ST_ELSEIF:
3528 if (seen_else)
3530 gfc_error_1 ("ELSE IF statement at %C cannot follow ELSE "
3531 "statement at %L", &else_locus);
3533 reject_statement ();
3534 break;
3537 d = new_level (gfc_state_stack->head);
3538 d->op = EXEC_IF;
3539 d->expr1 = new_st.expr1;
3541 accept_statement (st);
3543 break;
3545 case ST_ELSE:
3546 if (seen_else)
3548 gfc_error ("Duplicate ELSE statements at %L and %C",
3549 &else_locus);
3550 reject_statement ();
3551 break;
3554 seen_else = 1;
3555 else_locus = gfc_current_locus;
3557 d = new_level (gfc_state_stack->head);
3558 d->op = EXEC_IF;
3560 accept_statement (st);
3562 break;
3564 case ST_ENDIF:
3565 break;
3567 default:
3568 unexpected_statement (st);
3569 break;
3572 while (st != ST_ENDIF);
3574 pop_state ();
3575 accept_statement (st);
3579 /* Parse a SELECT block. */
3581 static void
3582 parse_select_block (void)
3584 gfc_statement st;
3585 gfc_code *cp;
3586 gfc_state_data s;
3588 accept_statement (ST_SELECT_CASE);
3590 cp = gfc_state_stack->tail;
3591 push_state (&s, COMP_SELECT, gfc_new_block);
3593 /* Make sure that the next statement is a CASE or END SELECT. */
3594 for (;;)
3596 st = next_statement ();
3597 if (st == ST_NONE)
3598 unexpected_eof ();
3599 if (st == ST_END_SELECT)
3601 /* Empty SELECT CASE is OK. */
3602 accept_statement (st);
3603 pop_state ();
3604 return;
3606 if (st == ST_CASE)
3607 break;
3609 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
3610 "CASE at %C");
3612 reject_statement ();
3615 /* At this point, we're got a nonempty select block. */
3616 cp = new_level (cp);
3617 *cp = new_st;
3619 accept_statement (st);
3623 st = parse_executable (ST_NONE);
3624 switch (st)
3626 case ST_NONE:
3627 unexpected_eof ();
3629 case ST_CASE:
3630 cp = new_level (gfc_state_stack->head);
3631 *cp = new_st;
3632 gfc_clear_new_st ();
3634 accept_statement (st);
3635 /* Fall through */
3637 case ST_END_SELECT:
3638 break;
3640 /* Can't have an executable statement because of
3641 parse_executable(). */
3642 default:
3643 unexpected_statement (st);
3644 break;
3647 while (st != ST_END_SELECT);
3649 pop_state ();
3650 accept_statement (st);
3654 /* Pop the current selector from the SELECT TYPE stack. */
3656 static void
3657 select_type_pop (void)
3659 gfc_select_type_stack *old = select_type_stack;
3660 select_type_stack = old->prev;
3661 free (old);
3665 /* Parse a SELECT TYPE construct (F03:R821). */
3667 static void
3668 parse_select_type_block (void)
3670 gfc_statement st;
3671 gfc_code *cp;
3672 gfc_state_data s;
3674 accept_statement (ST_SELECT_TYPE);
3676 cp = gfc_state_stack->tail;
3677 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
3679 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
3680 or END SELECT. */
3681 for (;;)
3683 st = next_statement ();
3684 if (st == ST_NONE)
3685 unexpected_eof ();
3686 if (st == ST_END_SELECT)
3687 /* Empty SELECT CASE is OK. */
3688 goto done;
3689 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
3690 break;
3692 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
3693 "following SELECT TYPE at %C");
3695 reject_statement ();
3698 /* At this point, we're got a nonempty select block. */
3699 cp = new_level (cp);
3700 *cp = new_st;
3702 accept_statement (st);
3706 st = parse_executable (ST_NONE);
3707 switch (st)
3709 case ST_NONE:
3710 unexpected_eof ();
3712 case ST_TYPE_IS:
3713 case ST_CLASS_IS:
3714 cp = new_level (gfc_state_stack->head);
3715 *cp = new_st;
3716 gfc_clear_new_st ();
3718 accept_statement (st);
3719 /* Fall through */
3721 case ST_END_SELECT:
3722 break;
3724 /* Can't have an executable statement because of
3725 parse_executable(). */
3726 default:
3727 unexpected_statement (st);
3728 break;
3731 while (st != ST_END_SELECT);
3733 done:
3734 pop_state ();
3735 accept_statement (st);
3736 gfc_current_ns = gfc_current_ns->parent;
3737 select_type_pop ();
3741 /* Given a symbol, make sure it is not an iteration variable for a DO
3742 statement. This subroutine is called when the symbol is seen in a
3743 context that causes it to become redefined. If the symbol is an
3744 iterator, we generate an error message and return nonzero. */
3746 int
3747 gfc_check_do_variable (gfc_symtree *st)
3749 gfc_state_data *s;
3751 for (s=gfc_state_stack; s; s = s->previous)
3752 if (s->do_variable == st)
3754 gfc_error_now_1 ("Variable '%s' at %C cannot be redefined inside "
3755 "loop beginning at %L", st->name, &s->head->loc);
3756 return 1;
3759 return 0;
3763 /* Checks to see if the current statement label closes an enddo.
3764 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3765 an error) if it incorrectly closes an ENDDO. */
3767 static int
3768 check_do_closure (void)
3770 gfc_state_data *p;
3772 if (gfc_statement_label == NULL)
3773 return 0;
3775 for (p = gfc_state_stack; p; p = p->previous)
3776 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
3777 break;
3779 if (p == NULL)
3780 return 0; /* No loops to close */
3782 if (p->ext.end_do_label == gfc_statement_label)
3784 if (p == gfc_state_stack)
3785 return 1;
3787 gfc_error ("End of nonblock DO statement at %C is within another block");
3788 return 2;
3791 /* At this point, the label doesn't terminate the innermost loop.
3792 Make sure it doesn't terminate another one. */
3793 for (; p; p = p->previous)
3794 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
3795 && p->ext.end_do_label == gfc_statement_label)
3797 gfc_error ("End of nonblock DO statement at %C is interwoven "
3798 "with another DO loop");
3799 return 2;
3802 return 0;
3806 /* Parse a series of contained program units. */
3808 static void parse_progunit (gfc_statement);
3811 /* Parse a CRITICAL block. */
3813 static void
3814 parse_critical_block (void)
3816 gfc_code *top, *d;
3817 gfc_state_data s, *sd;
3818 gfc_statement st;
3820 for (sd = gfc_state_stack; sd; sd = sd->previous)
3821 if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
3822 gfc_error_now (is_oacc (sd)
3823 ? "CRITICAL block inside of OpenACC region at %C"
3824 : "CRITICAL block inside of OpenMP region at %C");
3826 s.ext.end_do_label = new_st.label1;
3828 accept_statement (ST_CRITICAL);
3829 top = gfc_state_stack->tail;
3831 push_state (&s, COMP_CRITICAL, gfc_new_block);
3833 d = add_statement ();
3834 d->op = EXEC_CRITICAL;
3835 top->block = d;
3839 st = parse_executable (ST_NONE);
3841 switch (st)
3843 case ST_NONE:
3844 unexpected_eof ();
3845 break;
3847 case ST_END_CRITICAL:
3848 if (s.ext.end_do_label != NULL
3849 && s.ext.end_do_label != gfc_statement_label)
3850 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3851 "match CRITICAL label");
3853 if (gfc_statement_label != NULL)
3855 new_st.op = EXEC_NOP;
3856 add_statement ();
3858 break;
3860 default:
3861 unexpected_statement (st);
3862 break;
3865 while (st != ST_END_CRITICAL);
3867 pop_state ();
3868 accept_statement (st);
3872 /* Set up the local namespace for a BLOCK construct. */
3874 gfc_namespace*
3875 gfc_build_block_ns (gfc_namespace *parent_ns)
3877 gfc_namespace* my_ns;
3878 static int numblock = 1;
3880 my_ns = gfc_get_namespace (parent_ns, 1);
3881 my_ns->construct_entities = 1;
3883 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3884 code generation (so it must not be NULL).
3885 We set its recursive argument if our container procedure is recursive, so
3886 that local variables are accordingly placed on the stack when it
3887 will be necessary. */
3888 if (gfc_new_block)
3889 my_ns->proc_name = gfc_new_block;
3890 else
3892 bool t;
3893 char buffer[20]; /* Enough to hold "block@2147483648\n". */
3895 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
3896 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
3897 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
3898 my_ns->proc_name->name, NULL);
3899 gcc_assert (t);
3900 gfc_commit_symbol (my_ns->proc_name);
3903 if (parent_ns->proc_name)
3904 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
3906 return my_ns;
3910 /* Parse a BLOCK construct. */
3912 static void
3913 parse_block_construct (void)
3915 gfc_namespace* my_ns;
3916 gfc_state_data s;
3918 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
3920 my_ns = gfc_build_block_ns (gfc_current_ns);
3922 new_st.op = EXEC_BLOCK;
3923 new_st.ext.block.ns = my_ns;
3924 new_st.ext.block.assoc = NULL;
3925 accept_statement (ST_BLOCK);
3927 push_state (&s, COMP_BLOCK, my_ns->proc_name);
3928 gfc_current_ns = my_ns;
3930 parse_progunit (ST_NONE);
3932 gfc_current_ns = gfc_current_ns->parent;
3933 pop_state ();
3937 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3938 behind the scenes with compiler-generated variables. */
3940 static void
3941 parse_associate (void)
3943 gfc_namespace* my_ns;
3944 gfc_state_data s;
3945 gfc_statement st;
3946 gfc_association_list* a;
3948 gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
3950 my_ns = gfc_build_block_ns (gfc_current_ns);
3952 new_st.op = EXEC_BLOCK;
3953 new_st.ext.block.ns = my_ns;
3954 gcc_assert (new_st.ext.block.assoc);
3956 /* Add all associate-names as BLOCK variables. Creating them is enough
3957 for now, they'll get their values during trans-* phase. */
3958 gfc_current_ns = my_ns;
3959 for (a = new_st.ext.block.assoc; a; a = a->next)
3961 gfc_symbol* sym;
3963 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
3964 gcc_unreachable ();
3966 sym = a->st->n.sym;
3967 sym->attr.flavor = FL_VARIABLE;
3968 sym->assoc = a;
3969 sym->declared_at = a->where;
3970 gfc_set_sym_referenced (sym);
3972 /* Initialize the typespec. It is not available in all cases,
3973 however, as it may only be set on the target during resolution.
3974 Still, sometimes it helps to have it right now -- especially
3975 for parsing component references on the associate-name
3976 in case of association to a derived-type. */
3977 sym->ts = a->target->ts;
3980 accept_statement (ST_ASSOCIATE);
3981 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
3983 loop:
3984 st = parse_executable (ST_NONE);
3985 switch (st)
3987 case ST_NONE:
3988 unexpected_eof ();
3990 case_end:
3991 accept_statement (st);
3992 my_ns->code = gfc_state_stack->head;
3993 break;
3995 default:
3996 unexpected_statement (st);
3997 goto loop;
4000 gfc_current_ns = gfc_current_ns->parent;
4001 pop_state ();
4005 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4006 handled inside of parse_executable(), because they aren't really
4007 loop statements. */
4009 static void
4010 parse_do_block (void)
4012 gfc_statement st;
4013 gfc_code *top;
4014 gfc_state_data s;
4015 gfc_symtree *stree;
4016 gfc_exec_op do_op;
4018 do_op = new_st.op;
4019 s.ext.end_do_label = new_st.label1;
4021 if (new_st.ext.iterator != NULL)
4022 stree = new_st.ext.iterator->var->symtree;
4023 else
4024 stree = NULL;
4026 accept_statement (ST_DO);
4028 top = gfc_state_stack->tail;
4029 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
4030 gfc_new_block);
4032 s.do_variable = stree;
4034 top->block = new_level (top);
4035 top->block->op = EXEC_DO;
4037 loop:
4038 st = parse_executable (ST_NONE);
4040 switch (st)
4042 case ST_NONE:
4043 unexpected_eof ();
4045 case ST_ENDDO:
4046 if (s.ext.end_do_label != NULL
4047 && s.ext.end_do_label != gfc_statement_label)
4048 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4049 "DO label");
4051 if (gfc_statement_label != NULL)
4053 new_st.op = EXEC_NOP;
4054 add_statement ();
4056 break;
4058 case ST_IMPLIED_ENDDO:
4059 /* If the do-stmt of this DO construct has a do-construct-name,
4060 the corresponding end-do must be an end-do-stmt (with a matching
4061 name, but in that case we must have seen ST_ENDDO first).
4062 We only complain about this in pedantic mode. */
4063 if (gfc_current_block () != NULL)
4064 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4065 &gfc_current_block()->declared_at);
4067 break;
4069 default:
4070 unexpected_statement (st);
4071 goto loop;
4074 pop_state ();
4075 accept_statement (st);
4079 /* Parse the statements of OpenMP do/parallel do. */
4081 static gfc_statement
4082 parse_omp_do (gfc_statement omp_st)
4084 gfc_statement st;
4085 gfc_code *cp, *np;
4086 gfc_state_data s;
4088 accept_statement (omp_st);
4090 cp = gfc_state_stack->tail;
4091 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4092 np = new_level (cp);
4093 np->op = cp->op;
4094 np->block = NULL;
4096 for (;;)
4098 st = next_statement ();
4099 if (st == ST_NONE)
4100 unexpected_eof ();
4101 else if (st == ST_DO)
4102 break;
4103 else
4104 unexpected_statement (st);
4107 parse_do_block ();
4108 if (gfc_statement_label != NULL
4109 && gfc_state_stack->previous != NULL
4110 && gfc_state_stack->previous->state == COMP_DO
4111 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4113 /* In
4114 DO 100 I=1,10
4115 !$OMP DO
4116 DO J=1,10
4118 100 CONTINUE
4119 there should be no !$OMP END DO. */
4120 pop_state ();
4121 return ST_IMPLIED_ENDDO;
4124 check_do_closure ();
4125 pop_state ();
4127 st = next_statement ();
4128 gfc_statement omp_end_st = ST_OMP_END_DO;
4129 switch (omp_st)
4131 case ST_OMP_DISTRIBUTE: omp_end_st = ST_OMP_END_DISTRIBUTE; break;
4132 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4133 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4134 break;
4135 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4136 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4137 break;
4138 case ST_OMP_DISTRIBUTE_SIMD:
4139 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4140 break;
4141 case ST_OMP_DO: omp_end_st = ST_OMP_END_DO; break;
4142 case ST_OMP_DO_SIMD: omp_end_st = ST_OMP_END_DO_SIMD; break;
4143 case ST_OMP_PARALLEL_DO: omp_end_st = ST_OMP_END_PARALLEL_DO; break;
4144 case ST_OMP_PARALLEL_DO_SIMD:
4145 omp_end_st = ST_OMP_END_PARALLEL_DO_SIMD;
4146 break;
4147 case ST_OMP_SIMD: omp_end_st = ST_OMP_END_SIMD; break;
4148 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4149 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4150 break;
4151 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4152 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4153 break;
4154 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4155 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4156 break;
4157 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4158 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4159 break;
4160 case ST_OMP_TEAMS_DISTRIBUTE:
4161 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4162 break;
4163 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4164 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4165 break;
4166 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4167 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4168 break;
4169 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4170 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4171 break;
4172 default: gcc_unreachable ();
4174 if (st == omp_end_st)
4176 if (new_st.op == EXEC_OMP_END_NOWAIT)
4177 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4178 else
4179 gcc_assert (new_st.op == EXEC_NOP);
4180 gfc_clear_new_st ();
4181 gfc_commit_symbols ();
4182 gfc_warning_check ();
4183 st = next_statement ();
4185 return st;
4189 /* Parse the statements of OpenMP atomic directive. */
4191 static gfc_statement
4192 parse_omp_atomic (void)
4194 gfc_statement st;
4195 gfc_code *cp, *np;
4196 gfc_state_data s;
4197 int count;
4199 accept_statement (ST_OMP_ATOMIC);
4201 cp = gfc_state_stack->tail;
4202 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4203 np = new_level (cp);
4204 np->op = cp->op;
4205 np->block = NULL;
4206 count = 1 + ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4207 == GFC_OMP_ATOMIC_CAPTURE);
4209 while (count)
4211 st = next_statement ();
4212 if (st == ST_NONE)
4213 unexpected_eof ();
4214 else if (st == ST_ASSIGNMENT)
4216 accept_statement (st);
4217 count--;
4219 else
4220 unexpected_statement (st);
4223 pop_state ();
4225 st = next_statement ();
4226 if (st == ST_OMP_END_ATOMIC)
4228 gfc_clear_new_st ();
4229 gfc_commit_symbols ();
4230 gfc_warning_check ();
4231 st = next_statement ();
4233 else if ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4234 == GFC_OMP_ATOMIC_CAPTURE)
4235 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4236 return st;
4240 /* Parse the statements of an OpenACC structured block. */
4242 static void
4243 parse_oacc_structured_block (gfc_statement acc_st)
4245 gfc_statement st, acc_end_st;
4246 gfc_code *cp, *np;
4247 gfc_state_data s, *sd;
4249 for (sd = gfc_state_stack; sd; sd = sd->previous)
4250 if (sd->state == COMP_CRITICAL)
4251 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4253 accept_statement (acc_st);
4255 cp = gfc_state_stack->tail;
4256 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4257 np = new_level (cp);
4258 np->op = cp->op;
4259 np->block = NULL;
4260 switch (acc_st)
4262 case ST_OACC_PARALLEL:
4263 acc_end_st = ST_OACC_END_PARALLEL;
4264 break;
4265 case ST_OACC_KERNELS:
4266 acc_end_st = ST_OACC_END_KERNELS;
4267 break;
4268 case ST_OACC_DATA:
4269 acc_end_st = ST_OACC_END_DATA;
4270 break;
4271 case ST_OACC_HOST_DATA:
4272 acc_end_st = ST_OACC_END_HOST_DATA;
4273 break;
4274 default:
4275 gcc_unreachable ();
4280 st = parse_executable (ST_NONE);
4281 if (st == ST_NONE)
4282 unexpected_eof ();
4283 else if (st != acc_end_st)
4284 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
4285 reject_statement ();
4287 while (st != acc_end_st);
4289 gcc_assert (new_st.op == EXEC_NOP);
4291 gfc_clear_new_st ();
4292 gfc_commit_symbols ();
4293 gfc_warning_check ();
4294 pop_state ();
4297 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4299 static gfc_statement
4300 parse_oacc_loop (gfc_statement acc_st)
4302 gfc_statement st;
4303 gfc_code *cp, *np;
4304 gfc_state_data s, *sd;
4306 for (sd = gfc_state_stack; sd; sd = sd->previous)
4307 if (sd->state == COMP_CRITICAL)
4308 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4310 accept_statement (acc_st);
4312 cp = gfc_state_stack->tail;
4313 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4314 np = new_level (cp);
4315 np->op = cp->op;
4316 np->block = NULL;
4318 for (;;)
4320 st = next_statement ();
4321 if (st == ST_NONE)
4322 unexpected_eof ();
4323 else if (st == ST_DO)
4324 break;
4325 else
4327 gfc_error ("Expected DO loop at %C");
4328 reject_statement ();
4332 parse_do_block ();
4333 if (gfc_statement_label != NULL
4334 && gfc_state_stack->previous != NULL
4335 && gfc_state_stack->previous->state == COMP_DO
4336 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4338 pop_state ();
4339 return ST_IMPLIED_ENDDO;
4342 check_do_closure ();
4343 pop_state ();
4345 st = next_statement ();
4346 if (st == ST_OACC_END_LOOP)
4347 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
4348 if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
4349 (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
4350 (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
4352 gcc_assert (new_st.op == EXEC_NOP);
4353 gfc_clear_new_st ();
4354 gfc_commit_symbols ();
4355 gfc_warning_check ();
4356 st = next_statement ();
4358 return st;
4362 /* Parse the statements of an OpenMP structured block. */
4364 static void
4365 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
4367 gfc_statement st, omp_end_st;
4368 gfc_code *cp, *np;
4369 gfc_state_data s;
4371 accept_statement (omp_st);
4373 cp = gfc_state_stack->tail;
4374 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4375 np = new_level (cp);
4376 np->op = cp->op;
4377 np->block = NULL;
4379 switch (omp_st)
4381 case ST_OMP_PARALLEL:
4382 omp_end_st = ST_OMP_END_PARALLEL;
4383 break;
4384 case ST_OMP_PARALLEL_SECTIONS:
4385 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
4386 break;
4387 case ST_OMP_SECTIONS:
4388 omp_end_st = ST_OMP_END_SECTIONS;
4389 break;
4390 case ST_OMP_ORDERED:
4391 omp_end_st = ST_OMP_END_ORDERED;
4392 break;
4393 case ST_OMP_CRITICAL:
4394 omp_end_st = ST_OMP_END_CRITICAL;
4395 break;
4396 case ST_OMP_MASTER:
4397 omp_end_st = ST_OMP_END_MASTER;
4398 break;
4399 case ST_OMP_SINGLE:
4400 omp_end_st = ST_OMP_END_SINGLE;
4401 break;
4402 case ST_OMP_TARGET:
4403 omp_end_st = ST_OMP_END_TARGET;
4404 break;
4405 case ST_OMP_TARGET_DATA:
4406 omp_end_st = ST_OMP_END_TARGET_DATA;
4407 break;
4408 case ST_OMP_TARGET_TEAMS:
4409 omp_end_st = ST_OMP_END_TARGET_TEAMS;
4410 break;
4411 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4412 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4413 break;
4414 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4415 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4416 break;
4417 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4418 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4419 break;
4420 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4421 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4422 break;
4423 case ST_OMP_TASK:
4424 omp_end_st = ST_OMP_END_TASK;
4425 break;
4426 case ST_OMP_TASKGROUP:
4427 omp_end_st = ST_OMP_END_TASKGROUP;
4428 break;
4429 case ST_OMP_TEAMS:
4430 omp_end_st = ST_OMP_END_TEAMS;
4431 break;
4432 case ST_OMP_TEAMS_DISTRIBUTE:
4433 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4434 break;
4435 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4436 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4437 break;
4438 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4439 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4440 break;
4441 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4442 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4443 break;
4444 case ST_OMP_DISTRIBUTE:
4445 omp_end_st = ST_OMP_END_DISTRIBUTE;
4446 break;
4447 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4448 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4449 break;
4450 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4451 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4452 break;
4453 case ST_OMP_DISTRIBUTE_SIMD:
4454 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4455 break;
4456 case ST_OMP_WORKSHARE:
4457 omp_end_st = ST_OMP_END_WORKSHARE;
4458 break;
4459 case ST_OMP_PARALLEL_WORKSHARE:
4460 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
4461 break;
4462 default:
4463 gcc_unreachable ();
4468 if (workshare_stmts_only)
4470 /* Inside of !$omp workshare, only
4471 scalar assignments
4472 array assignments
4473 where statements and constructs
4474 forall statements and constructs
4475 !$omp atomic
4476 !$omp critical
4477 !$omp parallel
4478 are allowed. For !$omp critical these
4479 restrictions apply recursively. */
4480 bool cycle = true;
4482 st = next_statement ();
4483 for (;;)
4485 switch (st)
4487 case ST_NONE:
4488 unexpected_eof ();
4490 case ST_ASSIGNMENT:
4491 case ST_WHERE:
4492 case ST_FORALL:
4493 accept_statement (st);
4494 break;
4496 case ST_WHERE_BLOCK:
4497 parse_where_block ();
4498 break;
4500 case ST_FORALL_BLOCK:
4501 parse_forall_block ();
4502 break;
4504 case ST_OMP_PARALLEL:
4505 case ST_OMP_PARALLEL_SECTIONS:
4506 parse_omp_structured_block (st, false);
4507 break;
4509 case ST_OMP_PARALLEL_WORKSHARE:
4510 case ST_OMP_CRITICAL:
4511 parse_omp_structured_block (st, true);
4512 break;
4514 case ST_OMP_PARALLEL_DO:
4515 case ST_OMP_PARALLEL_DO_SIMD:
4516 st = parse_omp_do (st);
4517 continue;
4519 case ST_OMP_ATOMIC:
4520 st = parse_omp_atomic ();
4521 continue;
4523 default:
4524 cycle = false;
4525 break;
4528 if (!cycle)
4529 break;
4531 st = next_statement ();
4534 else
4535 st = parse_executable (ST_NONE);
4536 if (st == ST_NONE)
4537 unexpected_eof ();
4538 else if (st == ST_OMP_SECTION
4539 && (omp_st == ST_OMP_SECTIONS
4540 || omp_st == ST_OMP_PARALLEL_SECTIONS))
4542 np = new_level (np);
4543 np->op = cp->op;
4544 np->block = NULL;
4546 else if (st != omp_end_st)
4547 unexpected_statement (st);
4549 while (st != omp_end_st);
4551 switch (new_st.op)
4553 case EXEC_OMP_END_NOWAIT:
4554 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4555 break;
4556 case EXEC_OMP_CRITICAL:
4557 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
4558 || (new_st.ext.omp_name != NULL
4559 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
4560 gfc_error ("Name after !$omp critical and !$omp end critical does "
4561 "not match at %C");
4562 free (CONST_CAST (char *, new_st.ext.omp_name));
4563 break;
4564 case EXEC_OMP_END_SINGLE:
4565 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
4566 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
4567 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
4568 gfc_free_omp_clauses (new_st.ext.omp_clauses);
4569 break;
4570 case EXEC_NOP:
4571 break;
4572 default:
4573 gcc_unreachable ();
4576 gfc_clear_new_st ();
4577 gfc_commit_symbols ();
4578 gfc_warning_check ();
4579 pop_state ();
4583 /* Accept a series of executable statements. We return the first
4584 statement that doesn't fit to the caller. Any block statements are
4585 passed on to the correct handler, which usually passes the buck
4586 right back here. */
4588 static gfc_statement
4589 parse_executable (gfc_statement st)
4591 int close_flag;
4593 if (st == ST_NONE)
4594 st = next_statement ();
4596 for (;;)
4598 close_flag = check_do_closure ();
4599 if (close_flag)
4600 switch (st)
4602 case ST_GOTO:
4603 case ST_END_PROGRAM:
4604 case ST_RETURN:
4605 case ST_EXIT:
4606 case ST_END_FUNCTION:
4607 case ST_CYCLE:
4608 case ST_PAUSE:
4609 case ST_STOP:
4610 case ST_ERROR_STOP:
4611 case ST_END_SUBROUTINE:
4613 case ST_DO:
4614 case ST_FORALL:
4615 case ST_WHERE:
4616 case ST_SELECT_CASE:
4617 gfc_error ("%s statement at %C cannot terminate a non-block "
4618 "DO loop", gfc_ascii_statement (st));
4619 break;
4621 default:
4622 break;
4625 switch (st)
4627 case ST_NONE:
4628 unexpected_eof ();
4630 case ST_DATA:
4631 gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
4632 "first executable statement");
4633 /* Fall through. */
4635 case ST_FORMAT:
4636 case ST_ENTRY:
4637 case_executable:
4638 accept_statement (st);
4639 if (close_flag == 1)
4640 return ST_IMPLIED_ENDDO;
4641 break;
4643 case ST_BLOCK:
4644 parse_block_construct ();
4645 break;
4647 case ST_ASSOCIATE:
4648 parse_associate ();
4649 break;
4651 case ST_IF_BLOCK:
4652 parse_if_block ();
4653 break;
4655 case ST_SELECT_CASE:
4656 parse_select_block ();
4657 break;
4659 case ST_SELECT_TYPE:
4660 parse_select_type_block();
4661 break;
4663 case ST_DO:
4664 parse_do_block ();
4665 if (check_do_closure () == 1)
4666 return ST_IMPLIED_ENDDO;
4667 break;
4669 case ST_CRITICAL:
4670 parse_critical_block ();
4671 break;
4673 case ST_WHERE_BLOCK:
4674 parse_where_block ();
4675 break;
4677 case ST_FORALL_BLOCK:
4678 parse_forall_block ();
4679 break;
4681 case ST_OACC_PARALLEL_LOOP:
4682 case ST_OACC_KERNELS_LOOP:
4683 case ST_OACC_LOOP:
4684 st = parse_oacc_loop (st);
4685 if (st == ST_IMPLIED_ENDDO)
4686 return st;
4687 continue;
4689 case ST_OACC_PARALLEL:
4690 case ST_OACC_KERNELS:
4691 case ST_OACC_DATA:
4692 case ST_OACC_HOST_DATA:
4693 parse_oacc_structured_block (st);
4694 break;
4696 case ST_OMP_PARALLEL:
4697 case ST_OMP_PARALLEL_SECTIONS:
4698 case ST_OMP_SECTIONS:
4699 case ST_OMP_ORDERED:
4700 case ST_OMP_CRITICAL:
4701 case ST_OMP_MASTER:
4702 case ST_OMP_SINGLE:
4703 case ST_OMP_TARGET:
4704 case ST_OMP_TARGET_DATA:
4705 case ST_OMP_TARGET_TEAMS:
4706 case ST_OMP_TEAMS:
4707 case ST_OMP_TASK:
4708 case ST_OMP_TASKGROUP:
4709 parse_omp_structured_block (st, false);
4710 break;
4712 case ST_OMP_WORKSHARE:
4713 case ST_OMP_PARALLEL_WORKSHARE:
4714 parse_omp_structured_block (st, true);
4715 break;
4717 case ST_OMP_DISTRIBUTE:
4718 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4719 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4720 case ST_OMP_DISTRIBUTE_SIMD:
4721 case ST_OMP_DO:
4722 case ST_OMP_DO_SIMD:
4723 case ST_OMP_PARALLEL_DO:
4724 case ST_OMP_PARALLEL_DO_SIMD:
4725 case ST_OMP_SIMD:
4726 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4727 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4728 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4729 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4730 case ST_OMP_TEAMS_DISTRIBUTE:
4731 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4732 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4733 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4734 st = parse_omp_do (st);
4735 if (st == ST_IMPLIED_ENDDO)
4736 return st;
4737 continue;
4739 case ST_OMP_ATOMIC:
4740 st = parse_omp_atomic ();
4741 continue;
4743 default:
4744 return st;
4747 st = next_statement ();
4752 /* Fix the symbols for sibling functions. These are incorrectly added to
4753 the child namespace as the parser didn't know about this procedure. */
4755 static void
4756 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
4758 gfc_namespace *ns;
4759 gfc_symtree *st;
4760 gfc_symbol *old_sym;
4762 for (ns = siblings; ns; ns = ns->sibling)
4764 st = gfc_find_symtree (ns->sym_root, sym->name);
4766 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
4767 goto fixup_contained;
4769 if ((st->n.sym->attr.flavor == FL_DERIVED
4770 && sym->attr.generic && sym->attr.function)
4771 ||(sym->attr.flavor == FL_DERIVED
4772 && st->n.sym->attr.generic && st->n.sym->attr.function))
4773 goto fixup_contained;
4775 old_sym = st->n.sym;
4776 if (old_sym->ns == ns
4777 && !old_sym->attr.contained
4779 /* By 14.6.1.3, host association should be excluded
4780 for the following. */
4781 && !(old_sym->attr.external
4782 || (old_sym->ts.type != BT_UNKNOWN
4783 && !old_sym->attr.implicit_type)
4784 || old_sym->attr.flavor == FL_PARAMETER
4785 || old_sym->attr.use_assoc
4786 || old_sym->attr.in_common
4787 || old_sym->attr.in_equivalence
4788 || old_sym->attr.data
4789 || old_sym->attr.dummy
4790 || old_sym->attr.result
4791 || old_sym->attr.dimension
4792 || old_sym->attr.allocatable
4793 || old_sym->attr.intrinsic
4794 || old_sym->attr.generic
4795 || old_sym->attr.flavor == FL_NAMELIST
4796 || old_sym->attr.flavor == FL_LABEL
4797 || old_sym->attr.proc == PROC_ST_FUNCTION))
4799 /* Replace it with the symbol from the parent namespace. */
4800 st->n.sym = sym;
4801 sym->refs++;
4803 gfc_release_symbol (old_sym);
4806 fixup_contained:
4807 /* Do the same for any contained procedures. */
4808 gfc_fixup_sibling_symbols (sym, ns->contained);
4812 static void
4813 parse_contained (int module)
4815 gfc_namespace *ns, *parent_ns, *tmp;
4816 gfc_state_data s1, s2;
4817 gfc_statement st;
4818 gfc_symbol *sym;
4819 gfc_entry_list *el;
4820 int contains_statements = 0;
4821 int seen_error = 0;
4823 push_state (&s1, COMP_CONTAINS, NULL);
4824 parent_ns = gfc_current_ns;
4828 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
4830 gfc_current_ns->sibling = parent_ns->contained;
4831 parent_ns->contained = gfc_current_ns;
4833 next:
4834 /* Process the next available statement. We come here if we got an error
4835 and rejected the last statement. */
4836 st = next_statement ();
4838 switch (st)
4840 case ST_NONE:
4841 unexpected_eof ();
4843 case ST_FUNCTION:
4844 case ST_SUBROUTINE:
4845 contains_statements = 1;
4846 accept_statement (st);
4848 push_state (&s2,
4849 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
4850 gfc_new_block);
4852 /* For internal procedures, create/update the symbol in the
4853 parent namespace. */
4855 if (!module)
4857 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
4858 gfc_error ("Contained procedure %qs at %C is already "
4859 "ambiguous", gfc_new_block->name);
4860 else
4862 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
4863 sym->name,
4864 &gfc_new_block->declared_at))
4866 if (st == ST_FUNCTION)
4867 gfc_add_function (&sym->attr, sym->name,
4868 &gfc_new_block->declared_at);
4869 else
4870 gfc_add_subroutine (&sym->attr, sym->name,
4871 &gfc_new_block->declared_at);
4875 gfc_commit_symbols ();
4877 else
4878 sym = gfc_new_block;
4880 /* Mark this as a contained function, so it isn't replaced
4881 by other module functions. */
4882 sym->attr.contained = 1;
4884 /* Set implicit_pure so that it can be reset if any of the
4885 tests for purity fail. This is used for some optimisation
4886 during translation. */
4887 if (!sym->attr.pure)
4888 sym->attr.implicit_pure = 1;
4890 parse_progunit (ST_NONE);
4892 /* Fix up any sibling functions that refer to this one. */
4893 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
4894 /* Or refer to any of its alternate entry points. */
4895 for (el = gfc_current_ns->entries; el; el = el->next)
4896 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
4898 gfc_current_ns->code = s2.head;
4899 gfc_current_ns = parent_ns;
4901 pop_state ();
4902 break;
4904 /* These statements are associated with the end of the host unit. */
4905 case ST_END_FUNCTION:
4906 case ST_END_MODULE:
4907 case ST_END_PROGRAM:
4908 case ST_END_SUBROUTINE:
4909 accept_statement (st);
4910 gfc_current_ns->code = s1.head;
4911 break;
4913 default:
4914 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
4915 gfc_ascii_statement (st));
4916 reject_statement ();
4917 seen_error = 1;
4918 goto next;
4919 break;
4922 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
4923 && st != ST_END_MODULE && st != ST_END_PROGRAM);
4925 /* The first namespace in the list is guaranteed to not have
4926 anything (worthwhile) in it. */
4927 tmp = gfc_current_ns;
4928 gfc_current_ns = parent_ns;
4929 if (seen_error && tmp->refs > 1)
4930 gfc_free_namespace (tmp);
4932 ns = gfc_current_ns->contained;
4933 gfc_current_ns->contained = ns->sibling;
4934 gfc_free_namespace (ns);
4936 pop_state ();
4937 if (!contains_statements)
4938 gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
4939 "FUNCTION or SUBROUTINE statement at %C");
4943 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
4945 static void
4946 parse_progunit (gfc_statement st)
4948 gfc_state_data *p;
4949 int n;
4951 st = parse_spec (st);
4952 switch (st)
4954 case ST_NONE:
4955 unexpected_eof ();
4957 case ST_CONTAINS:
4958 /* This is not allowed within BLOCK! */
4959 if (gfc_current_state () != COMP_BLOCK)
4960 goto contains;
4961 break;
4963 case_end:
4964 accept_statement (st);
4965 goto done;
4967 default:
4968 break;
4971 if (gfc_current_state () == COMP_FUNCTION)
4972 gfc_check_function_type (gfc_current_ns);
4974 loop:
4975 for (;;)
4977 st = parse_executable (st);
4979 switch (st)
4981 case ST_NONE:
4982 unexpected_eof ();
4984 case ST_CONTAINS:
4985 /* This is not allowed within BLOCK! */
4986 if (gfc_current_state () != COMP_BLOCK)
4987 goto contains;
4988 break;
4990 case_end:
4991 accept_statement (st);
4992 goto done;
4994 default:
4995 break;
4998 unexpected_statement (st);
4999 reject_statement ();
5000 st = next_statement ();
5003 contains:
5004 n = 0;
5006 for (p = gfc_state_stack; p; p = p->previous)
5007 if (p->state == COMP_CONTAINS)
5008 n++;
5010 if (gfc_find_state (COMP_MODULE) == true)
5011 n--;
5013 if (n > 0)
5015 gfc_error ("CONTAINS statement at %C is already in a contained "
5016 "program unit");
5017 reject_statement ();
5018 st = next_statement ();
5019 goto loop;
5022 parse_contained (0);
5024 done:
5025 gfc_current_ns->code = gfc_state_stack->head;
5026 if (gfc_state_stack->state == COMP_PROGRAM
5027 || gfc_state_stack->state == COMP_MODULE
5028 || gfc_state_stack->state == COMP_SUBROUTINE
5029 || gfc_state_stack->state == COMP_FUNCTION
5030 || gfc_state_stack->state == COMP_BLOCK)
5031 gfc_current_ns->oacc_declare_clauses
5032 = gfc_state_stack->ext.oacc_declare_clauses;
5036 /* Come here to complain about a global symbol already in use as
5037 something else. */
5039 void
5040 gfc_global_used (gfc_gsymbol *sym, locus *where)
5042 const char *name;
5044 if (where == NULL)
5045 where = &gfc_current_locus;
5047 switch(sym->type)
5049 case GSYM_PROGRAM:
5050 name = "PROGRAM";
5051 break;
5052 case GSYM_FUNCTION:
5053 name = "FUNCTION";
5054 break;
5055 case GSYM_SUBROUTINE:
5056 name = "SUBROUTINE";
5057 break;
5058 case GSYM_COMMON:
5059 name = "COMMON";
5060 break;
5061 case GSYM_BLOCK_DATA:
5062 name = "BLOCK DATA";
5063 break;
5064 case GSYM_MODULE:
5065 name = "MODULE";
5066 break;
5067 default:
5068 gfc_internal_error ("gfc_global_used(): Bad type");
5069 name = NULL;
5072 if (sym->binding_label)
5073 gfc_error_1 ("Global binding name '%s' at %L is already being used as a %s "
5074 "at %L", sym->binding_label, where, name, &sym->where);
5075 else
5076 gfc_error_1 ("Global name '%s' at %L is already being used as a %s at %L",
5077 sym->name, where, name, &sym->where);
5081 /* Parse a block data program unit. */
5083 static void
5084 parse_block_data (void)
5086 gfc_statement st;
5087 static locus blank_locus;
5088 static int blank_block=0;
5089 gfc_gsymbol *s;
5091 gfc_current_ns->proc_name = gfc_new_block;
5092 gfc_current_ns->is_block_data = 1;
5094 if (gfc_new_block == NULL)
5096 if (blank_block)
5097 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5098 "prior BLOCK DATA at %L", &blank_locus);
5099 else
5101 blank_block = 1;
5102 blank_locus = gfc_current_locus;
5105 else
5107 s = gfc_get_gsymbol (gfc_new_block->name);
5108 if (s->defined
5109 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
5110 gfc_global_used (s, &gfc_new_block->declared_at);
5111 else
5113 s->type = GSYM_BLOCK_DATA;
5114 s->where = gfc_new_block->declared_at;
5115 s->defined = 1;
5119 st = parse_spec (ST_NONE);
5121 while (st != ST_END_BLOCK_DATA)
5123 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5124 gfc_ascii_statement (st));
5125 reject_statement ();
5126 st = next_statement ();
5131 /* Parse a module subprogram. */
5133 static void
5134 parse_module (void)
5136 gfc_statement st;
5137 gfc_gsymbol *s;
5138 bool error;
5140 s = gfc_get_gsymbol (gfc_new_block->name);
5141 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
5142 gfc_global_used (s, &gfc_new_block->declared_at);
5143 else
5145 s->type = GSYM_MODULE;
5146 s->where = gfc_new_block->declared_at;
5147 s->defined = 1;
5150 st = parse_spec (ST_NONE);
5152 error = false;
5153 loop:
5154 switch (st)
5156 case ST_NONE:
5157 unexpected_eof ();
5159 case ST_CONTAINS:
5160 parse_contained (1);
5161 break;
5163 case ST_END_MODULE:
5164 accept_statement (st);
5165 break;
5167 default:
5168 gfc_error ("Unexpected %s statement in MODULE at %C",
5169 gfc_ascii_statement (st));
5171 error = true;
5172 reject_statement ();
5173 st = next_statement ();
5174 goto loop;
5177 /* Make sure not to free the namespace twice on error. */
5178 if (!error)
5179 s->ns = gfc_current_ns;
5183 /* Add a procedure name to the global symbol table. */
5185 static void
5186 add_global_procedure (bool sub)
5188 gfc_gsymbol *s;
5190 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5191 name is a global identifier. */
5192 if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
5194 s = gfc_get_gsymbol (gfc_new_block->name);
5196 if (s->defined
5197 || (s->type != GSYM_UNKNOWN
5198 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5200 gfc_global_used (s, &gfc_new_block->declared_at);
5201 /* Silence follow-up errors. */
5202 gfc_new_block->binding_label = NULL;
5204 else
5206 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5207 s->sym_name = gfc_new_block->name;
5208 s->where = gfc_new_block->declared_at;
5209 s->defined = 1;
5210 s->ns = gfc_current_ns;
5214 /* Don't add the symbol multiple times. */
5215 if (gfc_new_block->binding_label
5216 && (!gfc_notification_std (GFC_STD_F2008)
5217 || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
5219 s = gfc_get_gsymbol (gfc_new_block->binding_label);
5221 if (s->defined
5222 || (s->type != GSYM_UNKNOWN
5223 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5225 gfc_global_used (s, &gfc_new_block->declared_at);
5226 /* Silence follow-up errors. */
5227 gfc_new_block->binding_label = NULL;
5229 else
5231 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5232 s->sym_name = gfc_new_block->name;
5233 s->binding_label = gfc_new_block->binding_label;
5234 s->where = gfc_new_block->declared_at;
5235 s->defined = 1;
5236 s->ns = gfc_current_ns;
5242 /* Add a program to the global symbol table. */
5244 static void
5245 add_global_program (void)
5247 gfc_gsymbol *s;
5249 if (gfc_new_block == NULL)
5250 return;
5251 s = gfc_get_gsymbol (gfc_new_block->name);
5253 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
5254 gfc_global_used (s, &gfc_new_block->declared_at);
5255 else
5257 s->type = GSYM_PROGRAM;
5258 s->where = gfc_new_block->declared_at;
5259 s->defined = 1;
5260 s->ns = gfc_current_ns;
5265 /* Resolve all the program units. */
5266 static void
5267 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
5269 gfc_free_dt_list ();
5270 gfc_current_ns = gfc_global_ns_list;
5271 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5273 if (gfc_current_ns->proc_name
5274 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5275 continue; /* Already resolved. */
5277 if (gfc_current_ns->proc_name)
5278 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5279 gfc_resolve (gfc_current_ns);
5280 gfc_current_ns->derived_types = gfc_derived_types;
5281 gfc_derived_types = NULL;
5286 static void
5287 clean_up_modules (gfc_gsymbol *gsym)
5289 if (gsym == NULL)
5290 return;
5292 clean_up_modules (gsym->left);
5293 clean_up_modules (gsym->right);
5295 if (gsym->type != GSYM_MODULE || !gsym->ns)
5296 return;
5298 gfc_current_ns = gsym->ns;
5299 gfc_derived_types = gfc_current_ns->derived_types;
5300 gfc_done_2 ();
5301 gsym->ns = NULL;
5302 return;
5306 /* Translate all the program units. This could be in a different order
5307 to resolution if there are forward references in the file. */
5308 static void
5309 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
5311 int errors;
5313 gfc_current_ns = gfc_global_ns_list;
5314 gfc_get_errors (NULL, &errors);
5316 /* We first translate all modules to make sure that later parts
5317 of the program can use the decl. Then we translate the nonmodules. */
5319 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5321 if (!gfc_current_ns->proc_name
5322 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5323 continue;
5325 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5326 gfc_derived_types = gfc_current_ns->derived_types;
5327 gfc_generate_module_code (gfc_current_ns);
5328 gfc_current_ns->translated = 1;
5331 gfc_current_ns = gfc_global_ns_list;
5332 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5334 if (gfc_current_ns->proc_name
5335 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5336 continue;
5338 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5339 gfc_derived_types = gfc_current_ns->derived_types;
5340 gfc_generate_code (gfc_current_ns);
5341 gfc_current_ns->translated = 1;
5344 /* Clean up all the namespaces after translation. */
5345 gfc_current_ns = gfc_global_ns_list;
5346 for (;gfc_current_ns;)
5348 gfc_namespace *ns;
5350 if (gfc_current_ns->proc_name
5351 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5353 gfc_current_ns = gfc_current_ns->sibling;
5354 continue;
5357 ns = gfc_current_ns->sibling;
5358 gfc_derived_types = gfc_current_ns->derived_types;
5359 gfc_done_2 ();
5360 gfc_current_ns = ns;
5363 clean_up_modules (gfc_gsym_root);
5367 /* Top level parser. */
5369 bool
5370 gfc_parse_file (void)
5372 int seen_program, errors_before, errors;
5373 gfc_state_data top, s;
5374 gfc_statement st;
5375 locus prog_locus;
5376 gfc_namespace *next;
5378 gfc_start_source_files ();
5380 top.state = COMP_NONE;
5381 top.sym = NULL;
5382 top.previous = NULL;
5383 top.head = top.tail = NULL;
5384 top.do_variable = NULL;
5386 gfc_state_stack = &top;
5388 gfc_clear_new_st ();
5390 gfc_statement_label = NULL;
5392 if (setjmp (eof_buf))
5393 return false; /* Come here on unexpected EOF */
5395 /* Prepare the global namespace that will contain the
5396 program units. */
5397 gfc_global_ns_list = next = NULL;
5399 seen_program = 0;
5400 errors_before = 0;
5402 /* Exit early for empty files. */
5403 if (gfc_at_eof ())
5404 goto done;
5406 loop:
5407 gfc_init_2 ();
5408 st = next_statement ();
5409 switch (st)
5411 case ST_NONE:
5412 gfc_done_2 ();
5413 goto done;
5415 case ST_PROGRAM:
5416 if (seen_program)
5417 goto duplicate_main;
5418 seen_program = 1;
5419 prog_locus = gfc_current_locus;
5421 push_state (&s, COMP_PROGRAM, gfc_new_block);
5422 main_program_symbol(gfc_current_ns, gfc_new_block->name);
5423 accept_statement (st);
5424 add_global_program ();
5425 parse_progunit (ST_NONE);
5426 goto prog_units;
5427 break;
5429 case ST_SUBROUTINE:
5430 add_global_procedure (true);
5431 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
5432 accept_statement (st);
5433 parse_progunit (ST_NONE);
5434 goto prog_units;
5435 break;
5437 case ST_FUNCTION:
5438 add_global_procedure (false);
5439 push_state (&s, COMP_FUNCTION, gfc_new_block);
5440 accept_statement (st);
5441 parse_progunit (ST_NONE);
5442 goto prog_units;
5443 break;
5445 case ST_BLOCK_DATA:
5446 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
5447 accept_statement (st);
5448 parse_block_data ();
5449 break;
5451 case ST_MODULE:
5452 push_state (&s, COMP_MODULE, gfc_new_block);
5453 accept_statement (st);
5455 gfc_get_errors (NULL, &errors_before);
5456 parse_module ();
5457 break;
5459 /* Anything else starts a nameless main program block. */
5460 default:
5461 if (seen_program)
5462 goto duplicate_main;
5463 seen_program = 1;
5464 prog_locus = gfc_current_locus;
5466 push_state (&s, COMP_PROGRAM, gfc_new_block);
5467 main_program_symbol (gfc_current_ns, "MAIN__");
5468 parse_progunit (st);
5469 goto prog_units;
5470 break;
5473 /* Handle the non-program units. */
5474 gfc_current_ns->code = s.head;
5476 gfc_resolve (gfc_current_ns);
5478 /* Dump the parse tree if requested. */
5479 if (flag_dump_fortran_original)
5480 gfc_dump_parse_tree (gfc_current_ns, stdout);
5482 gfc_get_errors (NULL, &errors);
5483 if (s.state == COMP_MODULE)
5485 gfc_dump_module (s.sym->name, errors_before == errors);
5486 gfc_current_ns->derived_types = gfc_derived_types;
5487 gfc_derived_types = NULL;
5488 goto prog_units;
5490 else
5492 if (errors == 0)
5493 gfc_generate_code (gfc_current_ns);
5494 pop_state ();
5495 gfc_done_2 ();
5498 goto loop;
5500 prog_units:
5501 /* The main program and non-contained procedures are put
5502 in the global namespace list, so that they can be processed
5503 later and all their interfaces resolved. */
5504 gfc_current_ns->code = s.head;
5505 if (next)
5507 for (; next->sibling; next = next->sibling)
5509 next->sibling = gfc_current_ns;
5511 else
5512 gfc_global_ns_list = gfc_current_ns;
5514 next = gfc_current_ns;
5516 pop_state ();
5517 goto loop;
5519 done:
5521 /* Do the resolution. */
5522 resolve_all_program_units (gfc_global_ns_list);
5524 /* Do the parse tree dump. */
5525 gfc_current_ns
5526 = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
5528 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5529 if (!gfc_current_ns->proc_name
5530 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5532 gfc_dump_parse_tree (gfc_current_ns, stdout);
5533 fputs ("------------------------------------------\n\n", stdout);
5536 /* Do the translation. */
5537 translate_all_program_units (gfc_global_ns_list);
5539 gfc_end_source_files ();
5540 return true;
5542 duplicate_main:
5543 /* If we see a duplicate main program, shut down. If the second
5544 instance is an implied main program, i.e. data decls or executable
5545 statements, we're in for lots of errors. */
5546 gfc_error_1 ("Two main PROGRAMs at %L and %C", &prog_locus);
5547 reject_statement ();
5548 gfc_done_2 ();
5549 return true;
5552 /* Return true if this state data represents an OpenACC region. */
5553 bool
5554 is_oacc (gfc_state_data *sd)
5556 switch (sd->construct->op)
5558 case EXEC_OACC_PARALLEL_LOOP:
5559 case EXEC_OACC_PARALLEL:
5560 case EXEC_OACC_KERNELS_LOOP:
5561 case EXEC_OACC_KERNELS:
5562 case EXEC_OACC_DATA:
5563 case EXEC_OACC_HOST_DATA:
5564 case EXEC_OACC_LOOP:
5565 case EXEC_OACC_UPDATE:
5566 case EXEC_OACC_WAIT:
5567 case EXEC_OACC_CACHE:
5568 case EXEC_OACC_ENTER_DATA:
5569 case EXEC_OACC_EXIT_DATA:
5570 return true;
5572 default:
5573 return false;