2015-09-25 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / parse.c
blobf8d84de306a2481b6ca0657e5b202728c67ad041
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 "options.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_buffer old_error;
113 gfc_push_error (&old_error);
114 gfc_buffer_error (false);
115 gfc_use_modules ();
116 gfc_buffer_error (true);
117 gfc_pop_error (&old_error);
118 gfc_commit_symbols ();
119 gfc_warning_check ();
120 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
121 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
122 gfc_current_ns->old_data = gfc_current_ns->data;
123 last_was_use_stmt = false;
127 /* Figure out what the next statement is, (mostly) regardless of
128 proper ordering. The do...while(0) is there to prevent if/else
129 ambiguity. */
131 #define match(keyword, subr, st) \
132 do { \
133 if (match_word (keyword, subr, &old_locus) == MATCH_YES) \
134 return st; \
135 else \
136 undo_new_statement (); \
137 } while (0);
140 /* This is a specialist version of decode_statement that is used
141 for the specification statements in a function, whose
142 characteristics are deferred into the specification statements.
143 eg.: INTEGER (king = mykind) foo ()
144 USE mymodule, ONLY mykind.....
145 The KIND parameter needs a return after USE or IMPORT, whereas
146 derived type declarations can occur anywhere, up the executable
147 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
148 out of the correct kind of specification statements. */
149 static gfc_statement
150 decode_specification_statement (void)
152 gfc_statement st;
153 locus old_locus;
154 char c;
156 if (gfc_match_eos () == MATCH_YES)
157 return ST_NONE;
159 old_locus = gfc_current_locus;
161 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
163 last_was_use_stmt = true;
164 return ST_USE;
166 else
168 undo_new_statement ();
169 if (last_was_use_stmt)
170 use_modules ();
173 match ("import", gfc_match_import, ST_IMPORT);
175 if (gfc_current_block ()->result->ts.type != BT_DERIVED)
176 goto end_of_block;
178 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
179 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
180 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
182 /* General statement matching: Instead of testing every possible
183 statement, we eliminate most possibilities by peeking at the
184 first character. */
186 c = gfc_peek_ascii_char ();
188 switch (c)
190 case 'a':
191 match ("abstract% interface", gfc_match_abstract_interface,
192 ST_INTERFACE);
193 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
194 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
195 break;
197 case 'b':
198 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
199 break;
201 case 'c':
202 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
203 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
204 break;
206 case 'd':
207 match ("data", gfc_match_data, ST_DATA);
208 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
209 break;
211 case 'e':
212 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
213 match ("entry% ", gfc_match_entry, ST_ENTRY);
214 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
215 match ("external", gfc_match_external, ST_ATTR_DECL);
216 break;
218 case 'f':
219 match ("format", gfc_match_format, ST_FORMAT);
220 break;
222 case 'g':
223 break;
225 case 'i':
226 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
227 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
228 match ("interface", gfc_match_interface, ST_INTERFACE);
229 match ("intent", gfc_match_intent, ST_ATTR_DECL);
230 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
231 break;
233 case 'm':
234 break;
236 case 'n':
237 match ("namelist", gfc_match_namelist, ST_NAMELIST);
238 break;
240 case 'o':
241 match ("optional", gfc_match_optional, ST_ATTR_DECL);
242 break;
244 case 'p':
245 match ("parameter", gfc_match_parameter, ST_PARAMETER);
246 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
247 if (gfc_match_private (&st) == MATCH_YES)
248 return st;
249 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
250 if (gfc_match_public (&st) == MATCH_YES)
251 return st;
252 match ("protected", gfc_match_protected, ST_ATTR_DECL);
253 break;
255 case 'r':
256 break;
258 case 's':
259 match ("save", gfc_match_save, ST_ATTR_DECL);
260 break;
262 case 't':
263 match ("target", gfc_match_target, ST_ATTR_DECL);
264 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
265 break;
267 case 'u':
268 break;
270 case 'v':
271 match ("value", gfc_match_value, ST_ATTR_DECL);
272 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
273 break;
275 case 'w':
276 break;
279 /* This is not a specification statement. See if any of the matchers
280 has stored an error message of some sort. */
282 end_of_block:
283 gfc_clear_error ();
284 gfc_buffer_error (false);
285 gfc_current_locus = old_locus;
287 return ST_GET_FCN_CHARACTERISTICS;
291 /* This is the primary 'decode_statement'. */
292 static gfc_statement
293 decode_statement (void)
295 gfc_namespace *ns;
296 gfc_statement st;
297 locus old_locus;
298 match m;
299 char c;
301 gfc_enforce_clean_symbol_state ();
303 gfc_clear_error (); /* Clear any pending errors. */
304 gfc_clear_warning (); /* Clear any pending warnings. */
306 gfc_matching_function = false;
308 if (gfc_match_eos () == MATCH_YES)
309 return ST_NONE;
311 if (gfc_current_state () == COMP_FUNCTION
312 && gfc_current_block ()->result->ts.kind == -1)
313 return decode_specification_statement ();
315 old_locus = gfc_current_locus;
317 c = gfc_peek_ascii_char ();
319 if (c == 'u')
321 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
323 last_was_use_stmt = true;
324 return ST_USE;
326 else
327 undo_new_statement ();
330 if (last_was_use_stmt)
331 use_modules ();
333 /* Try matching a data declaration or function declaration. The
334 input "REALFUNCTIONA(N)" can mean several things in different
335 contexts, so it (and its relatives) get special treatment. */
337 if (gfc_current_state () == COMP_NONE
338 || gfc_current_state () == COMP_INTERFACE
339 || gfc_current_state () == COMP_CONTAINS)
341 gfc_matching_function = true;
342 m = gfc_match_function_decl ();
343 if (m == MATCH_YES)
344 return ST_FUNCTION;
345 else if (m == MATCH_ERROR)
346 reject_statement ();
347 else
348 gfc_undo_symbols ();
349 gfc_current_locus = old_locus;
351 gfc_matching_function = false;
354 /* Match statements whose error messages are meant to be overwritten
355 by something better. */
357 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
358 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
359 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
361 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
362 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
364 /* Try to match a subroutine statement, which has the same optional
365 prefixes that functions can have. */
367 if (gfc_match_subroutine () == MATCH_YES)
368 return ST_SUBROUTINE;
369 gfc_undo_symbols ();
370 gfc_current_locus = old_locus;
372 if (gfc_match_submod_proc () == MATCH_YES)
374 if (gfc_new_block->attr.subroutine)
375 return ST_SUBROUTINE;
376 else if (gfc_new_block->attr.function)
377 return ST_FUNCTION;
379 gfc_undo_symbols ();
380 gfc_current_locus = old_locus;
382 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
383 statements, which might begin with a block label. The match functions for
384 these statements are unusual in that their keyword is not seen before
385 the matcher is called. */
387 if (gfc_match_if (&st) == MATCH_YES)
388 return st;
389 gfc_undo_symbols ();
390 gfc_current_locus = old_locus;
392 if (gfc_match_where (&st) == MATCH_YES)
393 return st;
394 gfc_undo_symbols ();
395 gfc_current_locus = old_locus;
397 if (gfc_match_forall (&st) == MATCH_YES)
398 return st;
399 gfc_undo_symbols ();
400 gfc_current_locus = old_locus;
402 match (NULL, gfc_match_do, ST_DO);
403 match (NULL, gfc_match_block, ST_BLOCK);
404 match (NULL, gfc_match_associate, ST_ASSOCIATE);
405 match (NULL, gfc_match_critical, ST_CRITICAL);
406 match (NULL, gfc_match_select, ST_SELECT_CASE);
408 gfc_current_ns = gfc_build_block_ns (gfc_current_ns);
409 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
410 ns = gfc_current_ns;
411 gfc_current_ns = gfc_current_ns->parent;
412 gfc_free_namespace (ns);
414 /* General statement matching: Instead of testing every possible
415 statement, we eliminate most possibilities by peeking at the
416 first character. */
418 switch (c)
420 case 'a':
421 match ("abstract% interface", gfc_match_abstract_interface,
422 ST_INTERFACE);
423 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
424 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
425 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
426 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
427 break;
429 case 'b':
430 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
431 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
432 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
433 break;
435 case 'c':
436 match ("call", gfc_match_call, ST_CALL);
437 match ("close", gfc_match_close, ST_CLOSE);
438 match ("continue", gfc_match_continue, ST_CONTINUE);
439 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
440 match ("cycle", gfc_match_cycle, ST_CYCLE);
441 match ("case", gfc_match_case, ST_CASE);
442 match ("common", gfc_match_common, ST_COMMON);
443 match ("contains", gfc_match_eos, ST_CONTAINS);
444 match ("class", gfc_match_class_is, ST_CLASS_IS);
445 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
446 break;
448 case 'd':
449 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
450 match ("data", gfc_match_data, ST_DATA);
451 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
452 break;
454 case 'e':
455 match ("end file", gfc_match_endfile, ST_END_FILE);
456 match ("exit", gfc_match_exit, ST_EXIT);
457 match ("else", gfc_match_else, ST_ELSE);
458 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
459 match ("else if", gfc_match_elseif, ST_ELSEIF);
460 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
461 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
463 if (gfc_match_end (&st) == MATCH_YES)
464 return st;
466 match ("entry% ", gfc_match_entry, ST_ENTRY);
467 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
468 match ("external", gfc_match_external, ST_ATTR_DECL);
469 break;
471 case 'f':
472 match ("final", gfc_match_final_decl, ST_FINAL);
473 match ("flush", gfc_match_flush, ST_FLUSH);
474 match ("format", gfc_match_format, ST_FORMAT);
475 break;
477 case 'g':
478 match ("generic", gfc_match_generic, ST_GENERIC);
479 match ("go to", gfc_match_goto, ST_GOTO);
480 break;
482 case 'i':
483 match ("inquire", gfc_match_inquire, ST_INQUIRE);
484 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
485 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
486 match ("import", gfc_match_import, ST_IMPORT);
487 match ("interface", gfc_match_interface, ST_INTERFACE);
488 match ("intent", gfc_match_intent, ST_ATTR_DECL);
489 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
490 break;
492 case 'l':
493 match ("lock", gfc_match_lock, ST_LOCK);
494 break;
496 case 'm':
497 match ("module% procedure", gfc_match_modproc, ST_MODULE_PROC);
498 match ("module", gfc_match_module, ST_MODULE);
499 break;
501 case 'n':
502 match ("nullify", gfc_match_nullify, ST_NULLIFY);
503 match ("namelist", gfc_match_namelist, ST_NAMELIST);
504 break;
506 case 'o':
507 match ("open", gfc_match_open, ST_OPEN);
508 match ("optional", gfc_match_optional, ST_ATTR_DECL);
509 break;
511 case 'p':
512 match ("print", gfc_match_print, ST_WRITE);
513 match ("parameter", gfc_match_parameter, ST_PARAMETER);
514 match ("pause", gfc_match_pause, ST_PAUSE);
515 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
516 if (gfc_match_private (&st) == MATCH_YES)
517 return st;
518 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
519 match ("program", gfc_match_program, ST_PROGRAM);
520 if (gfc_match_public (&st) == MATCH_YES)
521 return st;
522 match ("protected", gfc_match_protected, ST_ATTR_DECL);
523 break;
525 case 'r':
526 match ("read", gfc_match_read, ST_READ);
527 match ("return", gfc_match_return, ST_RETURN);
528 match ("rewind", gfc_match_rewind, ST_REWIND);
529 break;
531 case 's':
532 match ("sequence", gfc_match_eos, ST_SEQUENCE);
533 match ("stop", gfc_match_stop, ST_STOP);
534 match ("save", gfc_match_save, ST_ATTR_DECL);
535 match ("submodule", gfc_match_submodule, ST_SUBMODULE);
536 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
537 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
538 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
539 break;
541 case 't':
542 match ("target", gfc_match_target, ST_ATTR_DECL);
543 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
544 match ("type is", gfc_match_type_is, ST_TYPE_IS);
545 break;
547 case 'u':
548 match ("unlock", gfc_match_unlock, ST_UNLOCK);
549 break;
551 case 'v':
552 match ("value", gfc_match_value, ST_ATTR_DECL);
553 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
554 break;
556 case 'w':
557 match ("wait", gfc_match_wait, ST_WAIT);
558 match ("write", gfc_match_write, ST_WRITE);
559 break;
562 /* All else has failed, so give up. See if any of the matchers has
563 stored an error message of some sort. */
565 if (!gfc_error_check ())
566 gfc_error_now ("Unclassifiable statement at %C");
568 reject_statement ();
570 gfc_error_recovery ();
572 return ST_NONE;
575 /* Like match, but set a flag simd_matched if keyword matched. */
576 #define matchs(keyword, subr, st) \
577 do { \
578 if (match_word_omp_simd (keyword, subr, &old_locus, \
579 &simd_matched) == MATCH_YES) \
580 return st; \
581 else \
582 undo_new_statement (); \
583 } while (0);
585 /* Like match, but don't match anything if not -fopenmp. */
586 #define matcho(keyword, subr, st) \
587 do { \
588 if (!flag_openmp) \
590 else if (match_word (keyword, subr, &old_locus) \
591 == MATCH_YES) \
592 return st; \
593 else \
594 undo_new_statement (); \
595 } while (0);
597 static gfc_statement
598 decode_oacc_directive (void)
600 locus old_locus;
601 char c;
603 gfc_enforce_clean_symbol_state ();
605 gfc_clear_error (); /* Clear any pending errors. */
606 gfc_clear_warning (); /* Clear any pending warnings. */
608 if (gfc_pure (NULL))
610 gfc_error_now ("OpenACC directives at %C may not appear in PURE "
611 "procedures");
612 gfc_error_recovery ();
613 return ST_NONE;
616 gfc_unset_implicit_pure (NULL);
618 old_locus = gfc_current_locus;
620 /* General OpenACC directive matching: Instead of testing every possible
621 statement, we eliminate most possibilities by peeking at the
622 first character. */
624 c = gfc_peek_ascii_char ();
626 switch (c)
628 case 'c':
629 match ("cache", gfc_match_oacc_cache, ST_OACC_CACHE);
630 break;
631 case 'd':
632 match ("data", gfc_match_oacc_data, ST_OACC_DATA);
633 match ("declare", gfc_match_oacc_declare, ST_OACC_DECLARE);
634 break;
635 case 'e':
636 match ("end data", gfc_match_omp_eos, ST_OACC_END_DATA);
637 match ("end host_data", gfc_match_omp_eos, ST_OACC_END_HOST_DATA);
638 match ("end kernels loop", gfc_match_omp_eos, ST_OACC_END_KERNELS_LOOP);
639 match ("end kernels", gfc_match_omp_eos, ST_OACC_END_KERNELS);
640 match ("end loop", gfc_match_omp_eos, ST_OACC_END_LOOP);
641 match ("end parallel loop", gfc_match_omp_eos, ST_OACC_END_PARALLEL_LOOP);
642 match ("end parallel", gfc_match_omp_eos, ST_OACC_END_PARALLEL);
643 match ("enter data", gfc_match_oacc_enter_data, ST_OACC_ENTER_DATA);
644 match ("exit data", gfc_match_oacc_exit_data, ST_OACC_EXIT_DATA);
645 break;
646 case 'h':
647 match ("host_data", gfc_match_oacc_host_data, ST_OACC_HOST_DATA);
648 break;
649 case 'p':
650 match ("parallel loop", gfc_match_oacc_parallel_loop, ST_OACC_PARALLEL_LOOP);
651 match ("parallel", gfc_match_oacc_parallel, ST_OACC_PARALLEL);
652 break;
653 case 'k':
654 match ("kernels loop", gfc_match_oacc_kernels_loop, ST_OACC_KERNELS_LOOP);
655 match ("kernels", gfc_match_oacc_kernels, ST_OACC_KERNELS);
656 break;
657 case 'l':
658 match ("loop", gfc_match_oacc_loop, ST_OACC_LOOP);
659 break;
660 case 'r':
661 match ("routine", gfc_match_oacc_routine, ST_OACC_ROUTINE);
662 break;
663 case 'u':
664 match ("update", gfc_match_oacc_update, ST_OACC_UPDATE);
665 break;
666 case 'w':
667 match ("wait", gfc_match_oacc_wait, ST_OACC_WAIT);
668 break;
671 /* Directive not found or stored an error message.
672 Check and give up. */
674 if (gfc_error_check () == 0)
675 gfc_error_now ("Unclassifiable OpenACC directive at %C");
677 reject_statement ();
679 gfc_error_recovery ();
681 return ST_NONE;
684 static gfc_statement
685 decode_omp_directive (void)
687 locus old_locus;
688 char c;
689 bool simd_matched = false;
691 gfc_enforce_clean_symbol_state ();
693 gfc_clear_error (); /* Clear any pending errors. */
694 gfc_clear_warning (); /* Clear any pending warnings. */
696 if (gfc_pure (NULL))
698 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
699 "or ELEMENTAL procedures");
700 gfc_error_recovery ();
701 return ST_NONE;
704 gfc_unset_implicit_pure (NULL);
706 old_locus = gfc_current_locus;
708 /* General OpenMP directive matching: Instead of testing every possible
709 statement, we eliminate most possibilities by peeking at the
710 first character. */
712 c = gfc_peek_ascii_char ();
714 /* match is for directives that should be recognized only if
715 -fopenmp, matchs for directives that should be recognized
716 if either -fopenmp or -fopenmp-simd. */
717 switch (c)
719 case 'a':
720 matcho ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
721 break;
722 case 'b':
723 matcho ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
724 break;
725 case 'c':
726 matcho ("cancellation% point", gfc_match_omp_cancellation_point,
727 ST_OMP_CANCELLATION_POINT);
728 matcho ("cancel", gfc_match_omp_cancel, ST_OMP_CANCEL);
729 matcho ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
730 break;
731 case 'd':
732 matchs ("declare reduction", gfc_match_omp_declare_reduction,
733 ST_OMP_DECLARE_REDUCTION);
734 matchs ("declare simd", gfc_match_omp_declare_simd,
735 ST_OMP_DECLARE_SIMD);
736 matcho ("declare target", gfc_match_omp_declare_target,
737 ST_OMP_DECLARE_TARGET);
738 matchs ("distribute parallel do simd",
739 gfc_match_omp_distribute_parallel_do_simd,
740 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD);
741 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do,
742 ST_OMP_DISTRIBUTE_PARALLEL_DO);
743 matchs ("distribute simd", gfc_match_omp_distribute_simd,
744 ST_OMP_DISTRIBUTE_SIMD);
745 matcho ("distribute", gfc_match_omp_distribute, ST_OMP_DISTRIBUTE);
746 matchs ("do simd", gfc_match_omp_do_simd, ST_OMP_DO_SIMD);
747 matcho ("do", gfc_match_omp_do, ST_OMP_DO);
748 break;
749 case 'e':
750 matcho ("end atomic", gfc_match_omp_eos, ST_OMP_END_ATOMIC);
751 matcho ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
752 matchs ("end distribute parallel do simd", gfc_match_omp_eos,
753 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD);
754 matcho ("end distribute parallel do", gfc_match_omp_eos,
755 ST_OMP_END_DISTRIBUTE_PARALLEL_DO);
756 matchs ("end distribute simd", gfc_match_omp_eos,
757 ST_OMP_END_DISTRIBUTE_SIMD);
758 matcho ("end distribute", gfc_match_omp_eos, ST_OMP_END_DISTRIBUTE);
759 matchs ("end do simd", gfc_match_omp_end_nowait, ST_OMP_END_DO_SIMD);
760 matcho ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
761 matchs ("end simd", gfc_match_omp_eos, ST_OMP_END_SIMD);
762 matcho ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
763 matcho ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
764 matchs ("end parallel do simd", gfc_match_omp_eos,
765 ST_OMP_END_PARALLEL_DO_SIMD);
766 matcho ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
767 matcho ("end parallel sections", gfc_match_omp_eos,
768 ST_OMP_END_PARALLEL_SECTIONS);
769 matcho ("end parallel workshare", gfc_match_omp_eos,
770 ST_OMP_END_PARALLEL_WORKSHARE);
771 matcho ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
772 matcho ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
773 matcho ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
774 matcho ("end target data", gfc_match_omp_eos, ST_OMP_END_TARGET_DATA);
775 matchs ("end target teams distribute parallel do simd",
776 gfc_match_omp_eos,
777 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
778 matcho ("end target teams distribute parallel do", gfc_match_omp_eos,
779 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
780 matchs ("end target teams distribute simd", gfc_match_omp_eos,
781 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD);
782 matcho ("end target teams distribute", gfc_match_omp_eos,
783 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE);
784 matcho ("end target teams", gfc_match_omp_eos, ST_OMP_END_TARGET_TEAMS);
785 matcho ("end target", gfc_match_omp_eos, ST_OMP_END_TARGET);
786 matcho ("end taskgroup", gfc_match_omp_eos, ST_OMP_END_TASKGROUP);
787 matcho ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
788 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos,
789 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
790 matcho ("end teams distribute parallel do", gfc_match_omp_eos,
791 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO);
792 matchs ("end teams distribute simd", gfc_match_omp_eos,
793 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD);
794 matcho ("end teams distribute", gfc_match_omp_eos,
795 ST_OMP_END_TEAMS_DISTRIBUTE);
796 matcho ("end teams", gfc_match_omp_eos, ST_OMP_END_TEAMS);
797 matcho ("end workshare", gfc_match_omp_end_nowait,
798 ST_OMP_END_WORKSHARE);
799 break;
800 case 'f':
801 matcho ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
802 break;
803 case 'm':
804 matcho ("master", gfc_match_omp_master, ST_OMP_MASTER);
805 break;
806 case 'o':
807 matcho ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
808 break;
809 case 'p':
810 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
811 ST_OMP_PARALLEL_DO_SIMD);
812 matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
813 matcho ("parallel sections", gfc_match_omp_parallel_sections,
814 ST_OMP_PARALLEL_SECTIONS);
815 matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
816 ST_OMP_PARALLEL_WORKSHARE);
817 matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
818 break;
819 case 's':
820 matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
821 matcho ("section", gfc_match_omp_eos, ST_OMP_SECTION);
822 matchs ("simd", gfc_match_omp_simd, ST_OMP_SIMD);
823 matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
824 break;
825 case 't':
826 matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
827 matchs ("target teams distribute parallel do simd",
828 gfc_match_omp_target_teams_distribute_parallel_do_simd,
829 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
830 matcho ("target teams distribute parallel do",
831 gfc_match_omp_target_teams_distribute_parallel_do,
832 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
833 matchs ("target teams distribute simd",
834 gfc_match_omp_target_teams_distribute_simd,
835 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
836 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
837 ST_OMP_TARGET_TEAMS_DISTRIBUTE);
838 matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
839 matcho ("target update", gfc_match_omp_target_update,
840 ST_OMP_TARGET_UPDATE);
841 matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
842 matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
843 matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
844 matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
845 matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
846 matchs ("teams distribute parallel do simd",
847 gfc_match_omp_teams_distribute_parallel_do_simd,
848 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
849 matcho ("teams distribute parallel do",
850 gfc_match_omp_teams_distribute_parallel_do,
851 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
852 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
853 ST_OMP_TEAMS_DISTRIBUTE_SIMD);
854 matcho ("teams distribute", gfc_match_omp_teams_distribute,
855 ST_OMP_TEAMS_DISTRIBUTE);
856 matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
857 matcho ("threadprivate", gfc_match_omp_threadprivate,
858 ST_OMP_THREADPRIVATE);
859 break;
860 case 'w':
861 matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
862 break;
865 /* All else has failed, so give up. See if any of the matchers has
866 stored an error message of some sort. Don't error out if
867 not -fopenmp and simd_matched is false, i.e. if a directive other
868 than one marked with match has been seen. */
870 if (flag_openmp || simd_matched)
872 if (!gfc_error_check ())
873 gfc_error_now ("Unclassifiable OpenMP directive at %C");
876 reject_statement ();
878 gfc_error_recovery ();
880 return ST_NONE;
883 static gfc_statement
884 decode_gcc_attribute (void)
886 locus old_locus;
888 gfc_enforce_clean_symbol_state ();
890 gfc_clear_error (); /* Clear any pending errors. */
891 gfc_clear_warning (); /* Clear any pending warnings. */
892 old_locus = gfc_current_locus;
894 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
896 /* All else has failed, so give up. See if any of the matchers has
897 stored an error message of some sort. */
899 if (!gfc_error_check ())
900 gfc_error_now ("Unclassifiable GCC directive at %C");
902 reject_statement ();
904 gfc_error_recovery ();
906 return ST_NONE;
909 #undef match
911 /* Assert next length characters to be equal to token in free form. */
913 static void
914 verify_token_free (const char* token, int length, bool last_was_use_stmt)
916 int i;
917 char c;
919 c = gfc_next_ascii_char ();
920 for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
921 gcc_assert (c == token[i]);
923 gcc_assert (gfc_is_whitespace(c));
924 gfc_gobble_whitespace ();
925 if (last_was_use_stmt)
926 use_modules ();
929 /* Get the next statement in free form source. */
931 static gfc_statement
932 next_free (void)
934 match m;
935 int i, cnt, at_bol;
936 char c;
938 at_bol = gfc_at_bol ();
939 gfc_gobble_whitespace ();
941 c = gfc_peek_ascii_char ();
943 if (ISDIGIT (c))
945 char d;
947 /* Found a statement label? */
948 m = gfc_match_st_label (&gfc_statement_label);
950 d = gfc_peek_ascii_char ();
951 if (m != MATCH_YES || !gfc_is_whitespace (d))
953 gfc_match_small_literal_int (&i, &cnt);
955 if (cnt > 5)
956 gfc_error_now ("Too many digits in statement label at %C");
958 if (i == 0)
959 gfc_error_now ("Zero is not a valid statement label at %C");
962 c = gfc_next_ascii_char ();
963 while (ISDIGIT(c));
965 if (!gfc_is_whitespace (c))
966 gfc_error_now ("Non-numeric character in statement label at %C");
968 return ST_NONE;
970 else
972 label_locus = gfc_current_locus;
974 gfc_gobble_whitespace ();
976 if (at_bol && gfc_peek_ascii_char () == ';')
978 gfc_error_now ("Semicolon at %C needs to be preceded by "
979 "statement");
980 gfc_next_ascii_char (); /* Eat up the semicolon. */
981 return ST_NONE;
984 if (gfc_match_eos () == MATCH_YES)
986 gfc_warning_now (0, "Ignoring statement label in empty statement "
987 "at %L", &label_locus);
988 gfc_free_st_label (gfc_statement_label);
989 gfc_statement_label = NULL;
990 return ST_NONE;
994 else if (c == '!')
996 /* Comments have already been skipped by the time we get here,
997 except for GCC attributes and OpenMP/OpenACC directives. */
999 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
1000 c = gfc_peek_ascii_char ();
1002 if (c == 'g')
1004 int i;
1006 c = gfc_next_ascii_char ();
1007 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
1008 gcc_assert (c == "gcc$"[i]);
1010 gfc_gobble_whitespace ();
1011 return decode_gcc_attribute ();
1014 else if (c == '$')
1016 /* Since both OpenMP and OpenACC directives starts with
1017 !$ character sequence, we must check all flags combinations */
1018 if ((flag_openmp || flag_openmp_simd)
1019 && !flag_openacc)
1021 verify_token_free ("$omp", 4, last_was_use_stmt);
1022 return decode_omp_directive ();
1024 else if ((flag_openmp || flag_openmp_simd)
1025 && flag_openacc)
1027 gfc_next_ascii_char (); /* Eat up dollar character */
1028 c = gfc_peek_ascii_char ();
1030 if (c == 'o')
1032 verify_token_free ("omp", 3, last_was_use_stmt);
1033 return decode_omp_directive ();
1035 else if (c == 'a')
1037 verify_token_free ("acc", 3, last_was_use_stmt);
1038 return decode_oacc_directive ();
1041 else if (flag_openacc)
1043 verify_token_free ("$acc", 4, last_was_use_stmt);
1044 return decode_oacc_directive ();
1047 gcc_unreachable ();
1050 if (at_bol && c == ';')
1052 if (!(gfc_option.allow_std & GFC_STD_F2008))
1053 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1054 "statement");
1055 gfc_next_ascii_char (); /* Eat up the semicolon. */
1056 return ST_NONE;
1059 return decode_statement ();
1062 /* Assert next length characters to be equal to token in fixed form. */
1064 static bool
1065 verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
1067 int i;
1068 char c = gfc_next_char_literal (NONSTRING);
1070 for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
1071 gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
1073 if (c != ' ' && c != '0')
1075 gfc_buffer_error (false);
1076 gfc_error ("Bad continuation line at %C");
1077 return false;
1079 if (last_was_use_stmt)
1080 use_modules ();
1082 return true;
1085 /* Get the next statement in fixed-form source. */
1087 static gfc_statement
1088 next_fixed (void)
1090 int label, digit_flag, i;
1091 locus loc;
1092 gfc_char_t c;
1094 if (!gfc_at_bol ())
1095 return decode_statement ();
1097 /* Skip past the current label field, parsing a statement label if
1098 one is there. This is a weird number parser, since the number is
1099 contained within five columns and can have any kind of embedded
1100 spaces. We also check for characters that make the rest of the
1101 line a comment. */
1103 label = 0;
1104 digit_flag = 0;
1106 for (i = 0; i < 5; i++)
1108 c = gfc_next_char_literal (NONSTRING);
1110 switch (c)
1112 case ' ':
1113 break;
1115 case '0':
1116 case '1':
1117 case '2':
1118 case '3':
1119 case '4':
1120 case '5':
1121 case '6':
1122 case '7':
1123 case '8':
1124 case '9':
1125 label = label * 10 + ((unsigned char) c - '0');
1126 label_locus = gfc_current_locus;
1127 digit_flag = 1;
1128 break;
1130 /* Comments have already been skipped by the time we get
1131 here, except for GCC attributes and OpenMP directives. */
1133 case '*':
1134 c = gfc_next_char_literal (NONSTRING);
1136 if (TOLOWER (c) == 'g')
1138 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
1139 gcc_assert (TOLOWER (c) == "gcc$"[i]);
1141 return decode_gcc_attribute ();
1143 else if (c == '$')
1145 if ((flag_openmp || flag_openmp_simd)
1146 && !flag_openacc)
1148 if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
1149 return ST_NONE;
1150 return decode_omp_directive ();
1152 else if ((flag_openmp || flag_openmp_simd)
1153 && flag_openacc)
1155 c = gfc_next_char_literal(NONSTRING);
1156 if (c == 'o' || c == 'O')
1158 if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
1159 return ST_NONE;
1160 return decode_omp_directive ();
1162 else if (c == 'a' || c == 'A')
1164 if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
1165 return ST_NONE;
1166 return decode_oacc_directive ();
1169 else if (flag_openacc)
1171 if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
1172 return ST_NONE;
1173 return decode_oacc_directive ();
1176 /* FALLTHROUGH */
1178 /* Comments have already been skipped by the time we get
1179 here so don't bother checking for them. */
1181 default:
1182 gfc_buffer_error (false);
1183 gfc_error ("Non-numeric character in statement label at %C");
1184 return ST_NONE;
1188 if (digit_flag)
1190 if (label == 0)
1191 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1192 else
1194 /* We've found a valid statement label. */
1195 gfc_statement_label = gfc_get_st_label (label);
1199 /* Since this line starts a statement, it cannot be a continuation
1200 of a previous statement. If we see something here besides a
1201 space or zero, it must be a bad continuation line. */
1203 c = gfc_next_char_literal (NONSTRING);
1204 if (c == '\n')
1205 goto blank_line;
1207 if (c != ' ' && c != '0')
1209 gfc_buffer_error (false);
1210 gfc_error ("Bad continuation line at %C");
1211 return ST_NONE;
1214 /* Now that we've taken care of the statement label columns, we have
1215 to make sure that the first nonblank character is not a '!'. If
1216 it is, the rest of the line is a comment. */
1220 loc = gfc_current_locus;
1221 c = gfc_next_char_literal (NONSTRING);
1223 while (gfc_is_whitespace (c));
1225 if (c == '!')
1226 goto blank_line;
1227 gfc_current_locus = loc;
1229 if (c == ';')
1231 if (digit_flag)
1232 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1233 else if (!(gfc_option.allow_std & GFC_STD_F2008))
1234 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1235 "statement");
1236 return ST_NONE;
1239 if (gfc_match_eos () == MATCH_YES)
1240 goto blank_line;
1242 /* At this point, we've got a nonblank statement to parse. */
1243 return decode_statement ();
1245 blank_line:
1246 if (digit_flag)
1247 gfc_warning_now (0, "Ignoring statement label in empty statement at %L",
1248 &label_locus);
1250 gfc_current_locus.lb->truncated = 0;
1251 gfc_advance_line ();
1252 return ST_NONE;
1256 /* Return the next non-ST_NONE statement to the caller. We also worry
1257 about including files and the ends of include files at this stage. */
1259 static gfc_statement
1260 next_statement (void)
1262 gfc_statement st;
1263 locus old_locus;
1265 gfc_enforce_clean_symbol_state ();
1267 gfc_new_block = NULL;
1269 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
1270 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
1271 gfc_current_ns->old_data = gfc_current_ns->data;
1272 for (;;)
1274 gfc_statement_label = NULL;
1275 gfc_buffer_error (true);
1277 if (gfc_at_eol ())
1278 gfc_advance_line ();
1280 gfc_skip_comments ();
1282 if (gfc_at_end ())
1284 st = ST_NONE;
1285 break;
1288 if (gfc_define_undef_line ())
1289 continue;
1291 old_locus = gfc_current_locus;
1293 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
1295 if (st != ST_NONE)
1296 break;
1299 gfc_buffer_error (false);
1301 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
1303 gfc_free_st_label (gfc_statement_label);
1304 gfc_statement_label = NULL;
1305 gfc_current_locus = old_locus;
1308 if (st != ST_NONE)
1309 check_statement_label (st);
1311 return st;
1315 /****************************** Parser ***********************************/
1317 /* The parser subroutines are of type 'try' that fail if the file ends
1318 unexpectedly. */
1320 /* Macros that expand to case-labels for various classes of
1321 statements. Start with executable statements that directly do
1322 things. */
1324 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1325 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1326 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1327 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1328 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1329 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1330 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1331 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1332 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1333 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1334 case ST_OMP_TARGET_UPDATE: case ST_ERROR_STOP: case ST_SYNC_ALL: \
1335 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1336 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1337 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1339 /* Statements that mark other executable statements. */
1341 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1342 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1343 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1344 case ST_OMP_PARALLEL: \
1345 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1346 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1347 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1348 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1349 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1350 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1351 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1352 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1353 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1354 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1355 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1356 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1357 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1358 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1359 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1360 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1361 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: \
1362 case ST_CRITICAL: \
1363 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1364 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: case ST_OACC_KERNELS_LOOP
1366 /* Declaration statements */
1368 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1369 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1370 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
1371 case ST_PROCEDURE: case ST_OMP_DECLARE_SIMD: case ST_OMP_DECLARE_REDUCTION: \
1372 case ST_OMP_DECLARE_TARGET: case ST_OACC_ROUTINE
1374 /* Block end statements. Errors associated with interchanging these
1375 are detected in gfc_match_end(). */
1377 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1378 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1379 case ST_END_BLOCK: case ST_END_ASSOCIATE
1382 /* Push a new state onto the stack. */
1384 static void
1385 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
1387 p->state = new_state;
1388 p->previous = gfc_state_stack;
1389 p->sym = sym;
1390 p->head = p->tail = NULL;
1391 p->do_variable = NULL;
1392 if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
1393 p->ext.oacc_declare_clauses = NULL;
1395 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1396 construct statement was accepted right before pushing the state. Thus,
1397 the construct's gfc_code is available as tail of the parent state. */
1398 gcc_assert (gfc_state_stack);
1399 p->construct = gfc_state_stack->tail;
1401 gfc_state_stack = p;
1405 /* Pop the current state. */
1406 static void
1407 pop_state (void)
1409 gfc_state_stack = gfc_state_stack->previous;
1413 /* Try to find the given state in the state stack. */
1415 bool
1416 gfc_find_state (gfc_compile_state state)
1418 gfc_state_data *p;
1420 for (p = gfc_state_stack; p; p = p->previous)
1421 if (p->state == state)
1422 break;
1424 return (p == NULL) ? false : true;
1428 /* Starts a new level in the statement list. */
1430 static gfc_code *
1431 new_level (gfc_code *q)
1433 gfc_code *p;
1435 p = q->block = gfc_get_code (EXEC_NOP);
1437 gfc_state_stack->head = gfc_state_stack->tail = p;
1439 return p;
1443 /* Add the current new_st code structure and adds it to the current
1444 program unit. As a side-effect, it zeroes the new_st. */
1446 static gfc_code *
1447 add_statement (void)
1449 gfc_code *p;
1451 p = XCNEW (gfc_code);
1452 *p = new_st;
1454 p->loc = gfc_current_locus;
1456 if (gfc_state_stack->head == NULL)
1457 gfc_state_stack->head = p;
1458 else
1459 gfc_state_stack->tail->next = p;
1461 while (p->next != NULL)
1462 p = p->next;
1464 gfc_state_stack->tail = p;
1466 gfc_clear_new_st ();
1468 return p;
1472 /* Frees everything associated with the current statement. */
1474 static void
1475 undo_new_statement (void)
1477 gfc_free_statements (new_st.block);
1478 gfc_free_statements (new_st.next);
1479 gfc_free_statement (&new_st);
1480 gfc_clear_new_st ();
1484 /* If the current statement has a statement label, make sure that it
1485 is allowed to, or should have one. */
1487 static void
1488 check_statement_label (gfc_statement st)
1490 gfc_sl_type type;
1492 if (gfc_statement_label == NULL)
1494 if (st == ST_FORMAT)
1495 gfc_error ("FORMAT statement at %L does not have a statement label",
1496 &new_st.loc);
1497 return;
1500 switch (st)
1502 case ST_END_PROGRAM:
1503 case ST_END_FUNCTION:
1504 case ST_END_SUBROUTINE:
1505 case ST_ENDDO:
1506 case ST_ENDIF:
1507 case ST_END_SELECT:
1508 case ST_END_CRITICAL:
1509 case ST_END_BLOCK:
1510 case ST_END_ASSOCIATE:
1511 case_executable:
1512 case_exec_markers:
1513 if (st == ST_ENDDO || st == ST_CONTINUE)
1514 type = ST_LABEL_DO_TARGET;
1515 else
1516 type = ST_LABEL_TARGET;
1517 break;
1519 case ST_FORMAT:
1520 type = ST_LABEL_FORMAT;
1521 break;
1523 /* Statement labels are not restricted from appearing on a
1524 particular line. However, there are plenty of situations
1525 where the resulting label can't be referenced. */
1527 default:
1528 type = ST_LABEL_BAD_TARGET;
1529 break;
1532 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1534 new_st.here = gfc_statement_label;
1538 /* Figures out what the enclosing program unit is. This will be a
1539 function, subroutine, program, block data or module. */
1541 gfc_state_data *
1542 gfc_enclosing_unit (gfc_compile_state * result)
1544 gfc_state_data *p;
1546 for (p = gfc_state_stack; p; p = p->previous)
1547 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1548 || p->state == COMP_MODULE || p->state == COMP_SUBMODULE
1549 || p->state == COMP_BLOCK_DATA || p->state == COMP_PROGRAM)
1552 if (result != NULL)
1553 *result = p->state;
1554 return p;
1557 if (result != NULL)
1558 *result = COMP_PROGRAM;
1559 return NULL;
1563 /* Translate a statement enum to a string. */
1565 const char *
1566 gfc_ascii_statement (gfc_statement st)
1568 const char *p;
1570 switch (st)
1572 case ST_ARITHMETIC_IF:
1573 p = _("arithmetic IF");
1574 break;
1575 case ST_ALLOCATE:
1576 p = "ALLOCATE";
1577 break;
1578 case ST_ASSOCIATE:
1579 p = "ASSOCIATE";
1580 break;
1581 case ST_ATTR_DECL:
1582 p = _("attribute declaration");
1583 break;
1584 case ST_BACKSPACE:
1585 p = "BACKSPACE";
1586 break;
1587 case ST_BLOCK:
1588 p = "BLOCK";
1589 break;
1590 case ST_BLOCK_DATA:
1591 p = "BLOCK DATA";
1592 break;
1593 case ST_CALL:
1594 p = "CALL";
1595 break;
1596 case ST_CASE:
1597 p = "CASE";
1598 break;
1599 case ST_CLOSE:
1600 p = "CLOSE";
1601 break;
1602 case ST_COMMON:
1603 p = "COMMON";
1604 break;
1605 case ST_CONTINUE:
1606 p = "CONTINUE";
1607 break;
1608 case ST_CONTAINS:
1609 p = "CONTAINS";
1610 break;
1611 case ST_CRITICAL:
1612 p = "CRITICAL";
1613 break;
1614 case ST_CYCLE:
1615 p = "CYCLE";
1616 break;
1617 case ST_DATA_DECL:
1618 p = _("data declaration");
1619 break;
1620 case ST_DATA:
1621 p = "DATA";
1622 break;
1623 case ST_DEALLOCATE:
1624 p = "DEALLOCATE";
1625 break;
1626 case ST_DERIVED_DECL:
1627 p = _("derived type declaration");
1628 break;
1629 case ST_DO:
1630 p = "DO";
1631 break;
1632 case ST_ELSE:
1633 p = "ELSE";
1634 break;
1635 case ST_ELSEIF:
1636 p = "ELSE IF";
1637 break;
1638 case ST_ELSEWHERE:
1639 p = "ELSEWHERE";
1640 break;
1641 case ST_END_ASSOCIATE:
1642 p = "END ASSOCIATE";
1643 break;
1644 case ST_END_BLOCK:
1645 p = "END BLOCK";
1646 break;
1647 case ST_END_BLOCK_DATA:
1648 p = "END BLOCK DATA";
1649 break;
1650 case ST_END_CRITICAL:
1651 p = "END CRITICAL";
1652 break;
1653 case ST_ENDDO:
1654 p = "END DO";
1655 break;
1656 case ST_END_FILE:
1657 p = "END FILE";
1658 break;
1659 case ST_END_FORALL:
1660 p = "END FORALL";
1661 break;
1662 case ST_END_FUNCTION:
1663 p = "END FUNCTION";
1664 break;
1665 case ST_ENDIF:
1666 p = "END IF";
1667 break;
1668 case ST_END_INTERFACE:
1669 p = "END INTERFACE";
1670 break;
1671 case ST_END_MODULE:
1672 p = "END MODULE";
1673 break;
1674 case ST_END_SUBMODULE:
1675 p = "END SUBMODULE";
1676 break;
1677 case ST_END_PROGRAM:
1678 p = "END PROGRAM";
1679 break;
1680 case ST_END_SELECT:
1681 p = "END SELECT";
1682 break;
1683 case ST_END_SUBROUTINE:
1684 p = "END SUBROUTINE";
1685 break;
1686 case ST_END_WHERE:
1687 p = "END WHERE";
1688 break;
1689 case ST_END_TYPE:
1690 p = "END TYPE";
1691 break;
1692 case ST_ENTRY:
1693 p = "ENTRY";
1694 break;
1695 case ST_EQUIVALENCE:
1696 p = "EQUIVALENCE";
1697 break;
1698 case ST_ERROR_STOP:
1699 p = "ERROR STOP";
1700 break;
1701 case ST_EXIT:
1702 p = "EXIT";
1703 break;
1704 case ST_FLUSH:
1705 p = "FLUSH";
1706 break;
1707 case ST_FORALL_BLOCK: /* Fall through */
1708 case ST_FORALL:
1709 p = "FORALL";
1710 break;
1711 case ST_FORMAT:
1712 p = "FORMAT";
1713 break;
1714 case ST_FUNCTION:
1715 p = "FUNCTION";
1716 break;
1717 case ST_GENERIC:
1718 p = "GENERIC";
1719 break;
1720 case ST_GOTO:
1721 p = "GOTO";
1722 break;
1723 case ST_IF_BLOCK:
1724 p = _("block IF");
1725 break;
1726 case ST_IMPLICIT:
1727 p = "IMPLICIT";
1728 break;
1729 case ST_IMPLICIT_NONE:
1730 p = "IMPLICIT NONE";
1731 break;
1732 case ST_IMPLIED_ENDDO:
1733 p = _("implied END DO");
1734 break;
1735 case ST_IMPORT:
1736 p = "IMPORT";
1737 break;
1738 case ST_INQUIRE:
1739 p = "INQUIRE";
1740 break;
1741 case ST_INTERFACE:
1742 p = "INTERFACE";
1743 break;
1744 case ST_LOCK:
1745 p = "LOCK";
1746 break;
1747 case ST_PARAMETER:
1748 p = "PARAMETER";
1749 break;
1750 case ST_PRIVATE:
1751 p = "PRIVATE";
1752 break;
1753 case ST_PUBLIC:
1754 p = "PUBLIC";
1755 break;
1756 case ST_MODULE:
1757 p = "MODULE";
1758 break;
1759 case ST_SUBMODULE:
1760 p = "SUBMODULE";
1761 break;
1762 case ST_PAUSE:
1763 p = "PAUSE";
1764 break;
1765 case ST_MODULE_PROC:
1766 p = "MODULE PROCEDURE";
1767 break;
1768 case ST_NAMELIST:
1769 p = "NAMELIST";
1770 break;
1771 case ST_NULLIFY:
1772 p = "NULLIFY";
1773 break;
1774 case ST_OPEN:
1775 p = "OPEN";
1776 break;
1777 case ST_PROGRAM:
1778 p = "PROGRAM";
1779 break;
1780 case ST_PROCEDURE:
1781 p = "PROCEDURE";
1782 break;
1783 case ST_READ:
1784 p = "READ";
1785 break;
1786 case ST_RETURN:
1787 p = "RETURN";
1788 break;
1789 case ST_REWIND:
1790 p = "REWIND";
1791 break;
1792 case ST_STOP:
1793 p = "STOP";
1794 break;
1795 case ST_SYNC_ALL:
1796 p = "SYNC ALL";
1797 break;
1798 case ST_SYNC_IMAGES:
1799 p = "SYNC IMAGES";
1800 break;
1801 case ST_SYNC_MEMORY:
1802 p = "SYNC MEMORY";
1803 break;
1804 case ST_SUBROUTINE:
1805 p = "SUBROUTINE";
1806 break;
1807 case ST_TYPE:
1808 p = "TYPE";
1809 break;
1810 case ST_UNLOCK:
1811 p = "UNLOCK";
1812 break;
1813 case ST_USE:
1814 p = "USE";
1815 break;
1816 case ST_WHERE_BLOCK: /* Fall through */
1817 case ST_WHERE:
1818 p = "WHERE";
1819 break;
1820 case ST_WAIT:
1821 p = "WAIT";
1822 break;
1823 case ST_WRITE:
1824 p = "WRITE";
1825 break;
1826 case ST_ASSIGNMENT:
1827 p = _("assignment");
1828 break;
1829 case ST_POINTER_ASSIGNMENT:
1830 p = _("pointer assignment");
1831 break;
1832 case ST_SELECT_CASE:
1833 p = "SELECT CASE";
1834 break;
1835 case ST_SELECT_TYPE:
1836 p = "SELECT TYPE";
1837 break;
1838 case ST_TYPE_IS:
1839 p = "TYPE IS";
1840 break;
1841 case ST_CLASS_IS:
1842 p = "CLASS IS";
1843 break;
1844 case ST_SEQUENCE:
1845 p = "SEQUENCE";
1846 break;
1847 case ST_SIMPLE_IF:
1848 p = _("simple IF");
1849 break;
1850 case ST_STATEMENT_FUNCTION:
1851 p = "STATEMENT FUNCTION";
1852 break;
1853 case ST_LABEL_ASSIGNMENT:
1854 p = "LABEL ASSIGNMENT";
1855 break;
1856 case ST_ENUM:
1857 p = "ENUM DEFINITION";
1858 break;
1859 case ST_ENUMERATOR:
1860 p = "ENUMERATOR DEFINITION";
1861 break;
1862 case ST_END_ENUM:
1863 p = "END ENUM";
1864 break;
1865 case ST_OACC_PARALLEL_LOOP:
1866 p = "!$ACC PARALLEL LOOP";
1867 break;
1868 case ST_OACC_END_PARALLEL_LOOP:
1869 p = "!$ACC END PARALLEL LOOP";
1870 break;
1871 case ST_OACC_PARALLEL:
1872 p = "!$ACC PARALLEL";
1873 break;
1874 case ST_OACC_END_PARALLEL:
1875 p = "!$ACC END PARALLEL";
1876 break;
1877 case ST_OACC_KERNELS:
1878 p = "!$ACC KERNELS";
1879 break;
1880 case ST_OACC_END_KERNELS:
1881 p = "!$ACC END KERNELS";
1882 break;
1883 case ST_OACC_KERNELS_LOOP:
1884 p = "!$ACC KERNELS LOOP";
1885 break;
1886 case ST_OACC_END_KERNELS_LOOP:
1887 p = "!$ACC END KERNELS LOOP";
1888 break;
1889 case ST_OACC_DATA:
1890 p = "!$ACC DATA";
1891 break;
1892 case ST_OACC_END_DATA:
1893 p = "!$ACC END DATA";
1894 break;
1895 case ST_OACC_HOST_DATA:
1896 p = "!$ACC HOST_DATA";
1897 break;
1898 case ST_OACC_END_HOST_DATA:
1899 p = "!$ACC END HOST_DATA";
1900 break;
1901 case ST_OACC_LOOP:
1902 p = "!$ACC LOOP";
1903 break;
1904 case ST_OACC_END_LOOP:
1905 p = "!$ACC END LOOP";
1906 break;
1907 case ST_OACC_DECLARE:
1908 p = "!$ACC DECLARE";
1909 break;
1910 case ST_OACC_UPDATE:
1911 p = "!$ACC UPDATE";
1912 break;
1913 case ST_OACC_WAIT:
1914 p = "!$ACC WAIT";
1915 break;
1916 case ST_OACC_CACHE:
1917 p = "!$ACC CACHE";
1918 break;
1919 case ST_OACC_ENTER_DATA:
1920 p = "!$ACC ENTER DATA";
1921 break;
1922 case ST_OACC_EXIT_DATA:
1923 p = "!$ACC EXIT DATA";
1924 break;
1925 case ST_OACC_ROUTINE:
1926 p = "!$ACC ROUTINE";
1927 break;
1928 case ST_OMP_ATOMIC:
1929 p = "!$OMP ATOMIC";
1930 break;
1931 case ST_OMP_BARRIER:
1932 p = "!$OMP BARRIER";
1933 break;
1934 case ST_OMP_CANCEL:
1935 p = "!$OMP CANCEL";
1936 break;
1937 case ST_OMP_CANCELLATION_POINT:
1938 p = "!$OMP CANCELLATION POINT";
1939 break;
1940 case ST_OMP_CRITICAL:
1941 p = "!$OMP CRITICAL";
1942 break;
1943 case ST_OMP_DECLARE_REDUCTION:
1944 p = "!$OMP DECLARE REDUCTION";
1945 break;
1946 case ST_OMP_DECLARE_SIMD:
1947 p = "!$OMP DECLARE SIMD";
1948 break;
1949 case ST_OMP_DECLARE_TARGET:
1950 p = "!$OMP DECLARE TARGET";
1951 break;
1952 case ST_OMP_DISTRIBUTE:
1953 p = "!$OMP DISTRIBUTE";
1954 break;
1955 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
1956 p = "!$OMP DISTRIBUTE PARALLEL DO";
1957 break;
1958 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
1959 p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
1960 break;
1961 case ST_OMP_DISTRIBUTE_SIMD:
1962 p = "!$OMP DISTRIBUTE SIMD";
1963 break;
1964 case ST_OMP_DO:
1965 p = "!$OMP DO";
1966 break;
1967 case ST_OMP_DO_SIMD:
1968 p = "!$OMP DO SIMD";
1969 break;
1970 case ST_OMP_END_ATOMIC:
1971 p = "!$OMP END ATOMIC";
1972 break;
1973 case ST_OMP_END_CRITICAL:
1974 p = "!$OMP END CRITICAL";
1975 break;
1976 case ST_OMP_END_DISTRIBUTE:
1977 p = "!$OMP END DISTRIBUTE";
1978 break;
1979 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
1980 p = "!$OMP END DISTRIBUTE PARALLEL DO";
1981 break;
1982 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
1983 p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
1984 break;
1985 case ST_OMP_END_DISTRIBUTE_SIMD:
1986 p = "!$OMP END DISTRIBUTE SIMD";
1987 break;
1988 case ST_OMP_END_DO:
1989 p = "!$OMP END DO";
1990 break;
1991 case ST_OMP_END_DO_SIMD:
1992 p = "!$OMP END DO SIMD";
1993 break;
1994 case ST_OMP_END_SIMD:
1995 p = "!$OMP END SIMD";
1996 break;
1997 case ST_OMP_END_MASTER:
1998 p = "!$OMP END MASTER";
1999 break;
2000 case ST_OMP_END_ORDERED:
2001 p = "!$OMP END ORDERED";
2002 break;
2003 case ST_OMP_END_PARALLEL:
2004 p = "!$OMP END PARALLEL";
2005 break;
2006 case ST_OMP_END_PARALLEL_DO:
2007 p = "!$OMP END PARALLEL DO";
2008 break;
2009 case ST_OMP_END_PARALLEL_DO_SIMD:
2010 p = "!$OMP END PARALLEL DO SIMD";
2011 break;
2012 case ST_OMP_END_PARALLEL_SECTIONS:
2013 p = "!$OMP END PARALLEL SECTIONS";
2014 break;
2015 case ST_OMP_END_PARALLEL_WORKSHARE:
2016 p = "!$OMP END PARALLEL WORKSHARE";
2017 break;
2018 case ST_OMP_END_SECTIONS:
2019 p = "!$OMP END SECTIONS";
2020 break;
2021 case ST_OMP_END_SINGLE:
2022 p = "!$OMP END SINGLE";
2023 break;
2024 case ST_OMP_END_TASK:
2025 p = "!$OMP END TASK";
2026 break;
2027 case ST_OMP_END_TARGET:
2028 p = "!$OMP END TARGET";
2029 break;
2030 case ST_OMP_END_TARGET_DATA:
2031 p = "!$OMP END TARGET DATA";
2032 break;
2033 case ST_OMP_END_TARGET_TEAMS:
2034 p = "!$OMP END TARGET TEAMS";
2035 break;
2036 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
2037 p = "!$OMP END TARGET TEAMS DISTRIBUTE";
2038 break;
2039 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2040 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2041 break;
2042 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2043 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2044 break;
2045 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
2046 p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2047 break;
2048 case ST_OMP_END_TASKGROUP:
2049 p = "!$OMP END TASKGROUP";
2050 break;
2051 case ST_OMP_END_TEAMS:
2052 p = "!$OMP END TEAMS";
2053 break;
2054 case ST_OMP_END_TEAMS_DISTRIBUTE:
2055 p = "!$OMP END TEAMS DISTRIBUTE";
2056 break;
2057 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
2058 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2059 break;
2060 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2061 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2062 break;
2063 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
2064 p = "!$OMP END TEAMS DISTRIBUTE SIMD";
2065 break;
2066 case ST_OMP_END_WORKSHARE:
2067 p = "!$OMP END WORKSHARE";
2068 break;
2069 case ST_OMP_FLUSH:
2070 p = "!$OMP FLUSH";
2071 break;
2072 case ST_OMP_MASTER:
2073 p = "!$OMP MASTER";
2074 break;
2075 case ST_OMP_ORDERED:
2076 p = "!$OMP ORDERED";
2077 break;
2078 case ST_OMP_PARALLEL:
2079 p = "!$OMP PARALLEL";
2080 break;
2081 case ST_OMP_PARALLEL_DO:
2082 p = "!$OMP PARALLEL DO";
2083 break;
2084 case ST_OMP_PARALLEL_DO_SIMD:
2085 p = "!$OMP PARALLEL DO SIMD";
2086 break;
2087 case ST_OMP_PARALLEL_SECTIONS:
2088 p = "!$OMP PARALLEL SECTIONS";
2089 break;
2090 case ST_OMP_PARALLEL_WORKSHARE:
2091 p = "!$OMP PARALLEL WORKSHARE";
2092 break;
2093 case ST_OMP_SECTIONS:
2094 p = "!$OMP SECTIONS";
2095 break;
2096 case ST_OMP_SECTION:
2097 p = "!$OMP SECTION";
2098 break;
2099 case ST_OMP_SIMD:
2100 p = "!$OMP SIMD";
2101 break;
2102 case ST_OMP_SINGLE:
2103 p = "!$OMP SINGLE";
2104 break;
2105 case ST_OMP_TARGET:
2106 p = "!$OMP TARGET";
2107 break;
2108 case ST_OMP_TARGET_DATA:
2109 p = "!$OMP TARGET DATA";
2110 break;
2111 case ST_OMP_TARGET_TEAMS:
2112 p = "!$OMP TARGET TEAMS";
2113 break;
2114 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
2115 p = "!$OMP TARGET TEAMS DISTRIBUTE";
2116 break;
2117 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2118 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2119 break;
2120 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2121 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2122 break;
2123 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2124 p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2125 break;
2126 case ST_OMP_TARGET_UPDATE:
2127 p = "!$OMP TARGET UPDATE";
2128 break;
2129 case ST_OMP_TASK:
2130 p = "!$OMP TASK";
2131 break;
2132 case ST_OMP_TASKGROUP:
2133 p = "!$OMP TASKGROUP";
2134 break;
2135 case ST_OMP_TASKWAIT:
2136 p = "!$OMP TASKWAIT";
2137 break;
2138 case ST_OMP_TASKYIELD:
2139 p = "!$OMP TASKYIELD";
2140 break;
2141 case ST_OMP_TEAMS:
2142 p = "!$OMP TEAMS";
2143 break;
2144 case ST_OMP_TEAMS_DISTRIBUTE:
2145 p = "!$OMP TEAMS DISTRIBUTE";
2146 break;
2147 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2148 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2149 break;
2150 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2151 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2152 break;
2153 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
2154 p = "!$OMP TEAMS DISTRIBUTE SIMD";
2155 break;
2156 case ST_OMP_THREADPRIVATE:
2157 p = "!$OMP THREADPRIVATE";
2158 break;
2159 case ST_OMP_WORKSHARE:
2160 p = "!$OMP WORKSHARE";
2161 break;
2162 default:
2163 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2166 return p;
2170 /* Create a symbol for the main program and assign it to ns->proc_name. */
2172 static void
2173 main_program_symbol (gfc_namespace *ns, const char *name)
2175 gfc_symbol *main_program;
2176 symbol_attribute attr;
2178 gfc_get_symbol (name, ns, &main_program);
2179 gfc_clear_attr (&attr);
2180 attr.flavor = FL_PROGRAM;
2181 attr.proc = PROC_UNKNOWN;
2182 attr.subroutine = 1;
2183 attr.access = ACCESS_PUBLIC;
2184 attr.is_main_program = 1;
2185 main_program->attr = attr;
2186 main_program->declared_at = gfc_current_locus;
2187 ns->proc_name = main_program;
2188 gfc_commit_symbols ();
2192 /* Do whatever is necessary to accept the last statement. */
2194 static void
2195 accept_statement (gfc_statement st)
2197 switch (st)
2199 case ST_IMPLICIT_NONE:
2200 case ST_IMPLICIT:
2201 break;
2203 case ST_FUNCTION:
2204 case ST_SUBROUTINE:
2205 case ST_MODULE:
2206 case ST_SUBMODULE:
2207 gfc_current_ns->proc_name = gfc_new_block;
2208 break;
2210 /* If the statement is the end of a block, lay down a special code
2211 that allows a branch to the end of the block from within the
2212 construct. IF and SELECT are treated differently from DO
2213 (where EXEC_NOP is added inside the loop) for two
2214 reasons:
2215 1. END DO has a meaning in the sense that after a GOTO to
2216 it, the loop counter must be increased.
2217 2. IF blocks and SELECT blocks can consist of multiple
2218 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2219 Putting the label before the END IF would make the jump
2220 from, say, the ELSE IF block to the END IF illegal. */
2222 case ST_ENDIF:
2223 case ST_END_SELECT:
2224 case ST_END_CRITICAL:
2225 if (gfc_statement_label != NULL)
2227 new_st.op = EXEC_END_NESTED_BLOCK;
2228 add_statement ();
2230 break;
2232 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2233 one parallel block. Thus, we add the special code to the nested block
2234 itself, instead of the parent one. */
2235 case ST_END_BLOCK:
2236 case ST_END_ASSOCIATE:
2237 if (gfc_statement_label != NULL)
2239 new_st.op = EXEC_END_BLOCK;
2240 add_statement ();
2242 break;
2244 /* The end-of-program unit statements do not get the special
2245 marker and require a statement of some sort if they are a
2246 branch target. */
2248 case ST_END_PROGRAM:
2249 case ST_END_FUNCTION:
2250 case ST_END_SUBROUTINE:
2251 if (gfc_statement_label != NULL)
2253 new_st.op = EXEC_RETURN;
2254 add_statement ();
2256 else
2258 new_st.op = EXEC_END_PROCEDURE;
2259 add_statement ();
2262 break;
2264 case ST_ENTRY:
2265 case_executable:
2266 case_exec_markers:
2267 add_statement ();
2268 break;
2270 default:
2271 break;
2274 gfc_commit_symbols ();
2275 gfc_warning_check ();
2276 gfc_clear_new_st ();
2280 /* Undo anything tentative that has been built for the current
2281 statement. */
2283 static void
2284 reject_statement (void)
2286 /* Revert to the previous charlen chain. */
2287 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
2288 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
2290 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
2291 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
2293 gfc_reject_data (gfc_current_ns);
2295 gfc_new_block = NULL;
2296 gfc_undo_symbols ();
2297 gfc_clear_warning ();
2298 undo_new_statement ();
2302 /* Generic complaint about an out of order statement. We also do
2303 whatever is necessary to clean up. */
2305 static void
2306 unexpected_statement (gfc_statement st)
2308 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
2310 reject_statement ();
2314 /* Given the next statement seen by the matcher, make sure that it is
2315 in proper order with the last. This subroutine is initialized by
2316 calling it with an argument of ST_NONE. If there is a problem, we
2317 issue an error and return false. Otherwise we return true.
2319 Individual parsers need to verify that the statements seen are
2320 valid before calling here, i.e., ENTRY statements are not allowed in
2321 INTERFACE blocks. The following diagram is taken from the standard:
2323 +---------------------------------------+
2324 | program subroutine function module |
2325 +---------------------------------------+
2326 | use |
2327 +---------------------------------------+
2328 | import |
2329 +---------------------------------------+
2330 | | implicit none |
2331 | +-----------+------------------+
2332 | | parameter | implicit |
2333 | +-----------+------------------+
2334 | format | | derived type |
2335 | entry | parameter | interface |
2336 | | data | specification |
2337 | | | statement func |
2338 | +-----------+------------------+
2339 | | data | executable |
2340 +--------+-----------+------------------+
2341 | contains |
2342 +---------------------------------------+
2343 | internal module/subprogram |
2344 +---------------------------------------+
2345 | end |
2346 +---------------------------------------+
2350 enum state_order
2352 ORDER_START,
2353 ORDER_USE,
2354 ORDER_IMPORT,
2355 ORDER_IMPLICIT_NONE,
2356 ORDER_IMPLICIT,
2357 ORDER_SPEC,
2358 ORDER_EXEC
2361 typedef struct
2363 enum state_order state;
2364 gfc_statement last_statement;
2365 locus where;
2367 st_state;
2369 static bool
2370 verify_st_order (st_state *p, gfc_statement st, bool silent)
2373 switch (st)
2375 case ST_NONE:
2376 p->state = ORDER_START;
2377 break;
2379 case ST_USE:
2380 if (p->state > ORDER_USE)
2381 goto order;
2382 p->state = ORDER_USE;
2383 break;
2385 case ST_IMPORT:
2386 if (p->state > ORDER_IMPORT)
2387 goto order;
2388 p->state = ORDER_IMPORT;
2389 break;
2391 case ST_IMPLICIT_NONE:
2392 if (p->state > ORDER_IMPLICIT)
2393 goto order;
2395 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2396 statement disqualifies a USE but not an IMPLICIT NONE.
2397 Duplicate IMPLICIT NONEs are caught when the implicit types
2398 are set. */
2400 p->state = ORDER_IMPLICIT_NONE;
2401 break;
2403 case ST_IMPLICIT:
2404 if (p->state > ORDER_IMPLICIT)
2405 goto order;
2406 p->state = ORDER_IMPLICIT;
2407 break;
2409 case ST_FORMAT:
2410 case ST_ENTRY:
2411 if (p->state < ORDER_IMPLICIT_NONE)
2412 p->state = ORDER_IMPLICIT_NONE;
2413 break;
2415 case ST_PARAMETER:
2416 if (p->state >= ORDER_EXEC)
2417 goto order;
2418 if (p->state < ORDER_IMPLICIT)
2419 p->state = ORDER_IMPLICIT;
2420 break;
2422 case ST_DATA:
2423 if (p->state < ORDER_SPEC)
2424 p->state = ORDER_SPEC;
2425 break;
2427 case ST_PUBLIC:
2428 case ST_PRIVATE:
2429 case ST_DERIVED_DECL:
2430 case ST_OACC_DECLARE:
2431 case_decl:
2432 if (p->state >= ORDER_EXEC)
2433 goto order;
2434 if (p->state < ORDER_SPEC)
2435 p->state = ORDER_SPEC;
2436 break;
2438 case_executable:
2439 case_exec_markers:
2440 if (p->state < ORDER_EXEC)
2441 p->state = ORDER_EXEC;
2442 break;
2444 default:
2445 return false;
2448 /* All is well, record the statement in case we need it next time. */
2449 p->where = gfc_current_locus;
2450 p->last_statement = st;
2451 return true;
2453 order:
2454 if (!silent)
2455 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2456 gfc_ascii_statement (st),
2457 gfc_ascii_statement (p->last_statement), &p->where);
2459 return false;
2463 /* Handle an unexpected end of file. This is a show-stopper... */
2465 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
2467 static void
2468 unexpected_eof (void)
2470 gfc_state_data *p;
2472 gfc_error ("Unexpected end of file in %qs", gfc_source_file);
2474 /* Memory cleanup. Move to "second to last". */
2475 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
2476 p = p->previous);
2478 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
2479 gfc_done_2 ();
2481 longjmp (eof_buf, 1);
2485 /* Parse the CONTAINS section of a derived type definition. */
2487 gfc_access gfc_typebound_default_access;
2489 static bool
2490 parse_derived_contains (void)
2492 gfc_state_data s;
2493 bool seen_private = false;
2494 bool seen_comps = false;
2495 bool error_flag = false;
2496 bool to_finish;
2498 gcc_assert (gfc_current_state () == COMP_DERIVED);
2499 gcc_assert (gfc_current_block ());
2501 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2502 section. */
2503 if (gfc_current_block ()->attr.sequence)
2504 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2505 " section at %C", gfc_current_block ()->name);
2506 if (gfc_current_block ()->attr.is_bind_c)
2507 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2508 " section at %C", gfc_current_block ()->name);
2510 accept_statement (ST_CONTAINS);
2511 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
2513 gfc_typebound_default_access = ACCESS_PUBLIC;
2515 to_finish = false;
2516 while (!to_finish)
2518 gfc_statement st;
2519 st = next_statement ();
2520 switch (st)
2522 case ST_NONE:
2523 unexpected_eof ();
2524 break;
2526 case ST_DATA_DECL:
2527 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2528 goto error;
2530 case ST_PROCEDURE:
2531 if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
2532 goto error;
2534 accept_statement (ST_PROCEDURE);
2535 seen_comps = true;
2536 break;
2538 case ST_GENERIC:
2539 if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
2540 goto error;
2542 accept_statement (ST_GENERIC);
2543 seen_comps = true;
2544 break;
2546 case ST_FINAL:
2547 if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
2548 " at %C"))
2549 goto error;
2551 accept_statement (ST_FINAL);
2552 seen_comps = true;
2553 break;
2555 case ST_END_TYPE:
2556 to_finish = true;
2558 if (!seen_comps
2559 && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
2560 "at %C with empty CONTAINS section")))
2561 goto error;
2563 /* ST_END_TYPE is accepted by parse_derived after return. */
2564 break;
2566 case ST_PRIVATE:
2567 if (!gfc_find_state (COMP_MODULE))
2569 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2570 "a MODULE");
2571 goto error;
2574 if (seen_comps)
2576 gfc_error ("PRIVATE statement at %C must precede procedure"
2577 " bindings");
2578 goto error;
2581 if (seen_private)
2583 gfc_error ("Duplicate PRIVATE statement at %C");
2584 goto error;
2587 accept_statement (ST_PRIVATE);
2588 gfc_typebound_default_access = ACCESS_PRIVATE;
2589 seen_private = true;
2590 break;
2592 case ST_SEQUENCE:
2593 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2594 goto error;
2596 case ST_CONTAINS:
2597 gfc_error ("Already inside a CONTAINS block at %C");
2598 goto error;
2600 default:
2601 unexpected_statement (st);
2602 break;
2605 continue;
2607 error:
2608 error_flag = true;
2609 reject_statement ();
2612 pop_state ();
2613 gcc_assert (gfc_current_state () == COMP_DERIVED);
2615 return error_flag;
2619 /* Parse a derived type. */
2621 static void
2622 parse_derived (void)
2624 int compiling_type, seen_private, seen_sequence, seen_component;
2625 gfc_statement st;
2626 gfc_state_data s;
2627 gfc_symbol *sym;
2628 gfc_component *c, *lock_comp = NULL;
2630 accept_statement (ST_DERIVED_DECL);
2631 push_state (&s, COMP_DERIVED, gfc_new_block);
2633 gfc_new_block->component_access = ACCESS_PUBLIC;
2634 seen_private = 0;
2635 seen_sequence = 0;
2636 seen_component = 0;
2638 compiling_type = 1;
2640 while (compiling_type)
2642 st = next_statement ();
2643 switch (st)
2645 case ST_NONE:
2646 unexpected_eof ();
2648 case ST_DATA_DECL:
2649 case ST_PROCEDURE:
2650 accept_statement (st);
2651 seen_component = 1;
2652 break;
2654 case ST_FINAL:
2655 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2656 break;
2658 case ST_END_TYPE:
2659 endType:
2660 compiling_type = 0;
2662 if (!seen_component)
2663 gfc_notify_std (GFC_STD_F2003, "Derived type "
2664 "definition at %C without components");
2666 accept_statement (ST_END_TYPE);
2667 break;
2669 case ST_PRIVATE:
2670 if (!gfc_find_state (COMP_MODULE))
2672 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2673 "a MODULE");
2674 break;
2677 if (seen_component)
2679 gfc_error ("PRIVATE statement at %C must precede "
2680 "structure components");
2681 break;
2684 if (seen_private)
2685 gfc_error ("Duplicate PRIVATE statement at %C");
2687 s.sym->component_access = ACCESS_PRIVATE;
2689 accept_statement (ST_PRIVATE);
2690 seen_private = 1;
2691 break;
2693 case ST_SEQUENCE:
2694 if (seen_component)
2696 gfc_error ("SEQUENCE statement at %C must precede "
2697 "structure components");
2698 break;
2701 if (gfc_current_block ()->attr.sequence)
2702 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
2703 "TYPE statement");
2705 if (seen_sequence)
2707 gfc_error ("Duplicate SEQUENCE statement at %C");
2710 seen_sequence = 1;
2711 gfc_add_sequence (&gfc_current_block ()->attr,
2712 gfc_current_block ()->name, NULL);
2713 break;
2715 case ST_CONTAINS:
2716 gfc_notify_std (GFC_STD_F2003,
2717 "CONTAINS block in derived type"
2718 " definition at %C");
2720 accept_statement (ST_CONTAINS);
2721 parse_derived_contains ();
2722 goto endType;
2724 default:
2725 unexpected_statement (st);
2726 break;
2730 /* need to verify that all fields of the derived type are
2731 * interoperable with C if the type is declared to be bind(c)
2733 sym = gfc_current_block ();
2734 for (c = sym->components; c; c = c->next)
2736 bool coarray, lock_type, allocatable, pointer;
2737 coarray = lock_type = allocatable = pointer = false;
2739 /* Look for allocatable components. */
2740 if (c->attr.allocatable
2741 || (c->ts.type == BT_CLASS && c->attr.class_ok
2742 && CLASS_DATA (c)->attr.allocatable)
2743 || (c->ts.type == BT_DERIVED && !c->attr.pointer
2744 && c->ts.u.derived->attr.alloc_comp))
2746 allocatable = true;
2747 sym->attr.alloc_comp = 1;
2750 /* Look for pointer components. */
2751 if (c->attr.pointer
2752 || (c->ts.type == BT_CLASS && c->attr.class_ok
2753 && CLASS_DATA (c)->attr.class_pointer)
2754 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2756 pointer = true;
2757 sym->attr.pointer_comp = 1;
2760 /* Look for procedure pointer components. */
2761 if (c->attr.proc_pointer
2762 || (c->ts.type == BT_DERIVED
2763 && c->ts.u.derived->attr.proc_pointer_comp))
2764 sym->attr.proc_pointer_comp = 1;
2766 /* Looking for coarray components. */
2767 if (c->attr.codimension
2768 || (c->ts.type == BT_CLASS && c->attr.class_ok
2769 && CLASS_DATA (c)->attr.codimension))
2771 coarray = true;
2772 sym->attr.coarray_comp = 1;
2775 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
2776 && !c->attr.pointer)
2778 coarray = true;
2779 sym->attr.coarray_comp = 1;
2782 /* Looking for lock_type components. */
2783 if ((c->ts.type == BT_DERIVED
2784 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2785 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2786 || (c->ts.type == BT_CLASS && c->attr.class_ok
2787 && CLASS_DATA (c)->ts.u.derived->from_intmod
2788 == INTMOD_ISO_FORTRAN_ENV
2789 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2790 == ISOFORTRAN_LOCK_TYPE)
2791 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
2792 && !allocatable && !pointer))
2794 lock_type = 1;
2795 lock_comp = c;
2796 sym->attr.lock_comp = 1;
2799 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2800 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2801 unless there are nondirect [allocatable or pointer] components
2802 involved (cf. 1.3.33.1 and 1.3.33.3). */
2804 if (pointer && !coarray && lock_type)
2805 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2806 "codimension or be a subcomponent of a coarray, "
2807 "which is not possible as the component has the "
2808 "pointer attribute", c->name, &c->loc);
2809 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2810 && c->ts.u.derived->attr.lock_comp)
2811 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2812 "of type LOCK_TYPE, which must have a codimension or be a "
2813 "subcomponent of a coarray", c->name, &c->loc);
2815 if (lock_type && allocatable && !coarray)
2816 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2817 "a codimension", c->name, &c->loc);
2818 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
2819 && c->ts.u.derived->attr.lock_comp)
2820 gfc_error ("Allocatable component %s at %L must have a codimension as "
2821 "it has a noncoarray subcomponent of type LOCK_TYPE",
2822 c->name, &c->loc);
2824 if (sym->attr.coarray_comp && !coarray && lock_type)
2825 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2826 "subcomponent of type LOCK_TYPE must have a codimension or "
2827 "be a subcomponent of a coarray. (Variables of type %s may "
2828 "not have a codimension as already a coarray "
2829 "subcomponent exists)", c->name, &c->loc, sym->name);
2831 if (sym->attr.lock_comp && coarray && !lock_type)
2832 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2833 "subcomponent of type LOCK_TYPE must have a codimension or "
2834 "be a subcomponent of a coarray. (Variables of type %s may "
2835 "not have a codimension as %s at %L has a codimension or a "
2836 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
2837 sym->name, c->name, &c->loc);
2839 /* Look for private components. */
2840 if (sym->component_access == ACCESS_PRIVATE
2841 || c->attr.access == ACCESS_PRIVATE
2842 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
2843 sym->attr.private_comp = 1;
2846 if (!seen_component)
2847 sym->attr.zero_comp = 1;
2849 pop_state ();
2853 /* Parse an ENUM. */
2855 static void
2856 parse_enum (void)
2858 gfc_statement st;
2859 int compiling_enum;
2860 gfc_state_data s;
2861 int seen_enumerator = 0;
2863 push_state (&s, COMP_ENUM, gfc_new_block);
2865 compiling_enum = 1;
2867 while (compiling_enum)
2869 st = next_statement ();
2870 switch (st)
2872 case ST_NONE:
2873 unexpected_eof ();
2874 break;
2876 case ST_ENUMERATOR:
2877 seen_enumerator = 1;
2878 accept_statement (st);
2879 break;
2881 case ST_END_ENUM:
2882 compiling_enum = 0;
2883 if (!seen_enumerator)
2884 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2885 accept_statement (st);
2886 break;
2888 default:
2889 gfc_free_enum_history ();
2890 unexpected_statement (st);
2891 break;
2894 pop_state ();
2898 /* Parse an interface. We must be able to deal with the possibility
2899 of recursive interfaces. The parse_spec() subroutine is mutually
2900 recursive with parse_interface(). */
2902 static gfc_statement parse_spec (gfc_statement);
2904 static void
2905 parse_interface (void)
2907 gfc_compile_state new_state = COMP_NONE, current_state;
2908 gfc_symbol *prog_unit, *sym;
2909 gfc_interface_info save;
2910 gfc_state_data s1, s2;
2911 gfc_statement st;
2913 accept_statement (ST_INTERFACE);
2915 current_interface.ns = gfc_current_ns;
2916 save = current_interface;
2918 sym = (current_interface.type == INTERFACE_GENERIC
2919 || current_interface.type == INTERFACE_USER_OP)
2920 ? gfc_new_block : NULL;
2922 push_state (&s1, COMP_INTERFACE, sym);
2923 current_state = COMP_NONE;
2925 loop:
2926 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
2928 st = next_statement ();
2929 switch (st)
2931 case ST_NONE:
2932 unexpected_eof ();
2934 case ST_SUBROUTINE:
2935 case ST_FUNCTION:
2936 if (st == ST_SUBROUTINE)
2937 new_state = COMP_SUBROUTINE;
2938 else if (st == ST_FUNCTION)
2939 new_state = COMP_FUNCTION;
2940 if (gfc_new_block->attr.pointer)
2942 gfc_new_block->attr.pointer = 0;
2943 gfc_new_block->attr.proc_pointer = 1;
2945 if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
2946 gfc_new_block->formal, NULL))
2948 reject_statement ();
2949 gfc_free_namespace (gfc_current_ns);
2950 goto loop;
2952 /* F2008 C1210 forbids the IMPORT statement in module procedure
2953 interface bodies and the flag is set to import symbols. */
2954 if (gfc_new_block->attr.module_procedure)
2955 gfc_current_ns->has_import_set = 1;
2956 break;
2958 case ST_PROCEDURE:
2959 case ST_MODULE_PROC: /* The module procedure matcher makes
2960 sure the context is correct. */
2961 accept_statement (st);
2962 gfc_free_namespace (gfc_current_ns);
2963 goto loop;
2965 case ST_END_INTERFACE:
2966 gfc_free_namespace (gfc_current_ns);
2967 gfc_current_ns = current_interface.ns;
2968 goto done;
2970 default:
2971 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2972 gfc_ascii_statement (st));
2973 reject_statement ();
2974 gfc_free_namespace (gfc_current_ns);
2975 goto loop;
2979 /* Make sure that the generic name has the right attribute. */
2980 if (current_interface.type == INTERFACE_GENERIC
2981 && current_state == COMP_NONE)
2983 if (new_state == COMP_FUNCTION && sym)
2984 gfc_add_function (&sym->attr, sym->name, NULL);
2985 else if (new_state == COMP_SUBROUTINE && sym)
2986 gfc_add_subroutine (&sym->attr, sym->name, NULL);
2988 current_state = new_state;
2991 if (current_interface.type == INTERFACE_ABSTRACT)
2993 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
2994 if (gfc_is_intrinsic_typename (gfc_new_block->name))
2995 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
2996 "cannot be the same as an intrinsic type",
2997 gfc_new_block->name);
3000 push_state (&s2, new_state, gfc_new_block);
3001 accept_statement (st);
3002 prog_unit = gfc_new_block;
3003 prog_unit->formal_ns = gfc_current_ns;
3004 if (prog_unit == prog_unit->formal_ns->proc_name
3005 && prog_unit->ns != prog_unit->formal_ns)
3006 prog_unit->refs++;
3008 decl:
3009 /* Read data declaration statements. */
3010 st = parse_spec (ST_NONE);
3012 /* Since the interface block does not permit an IMPLICIT statement,
3013 the default type for the function or the result must be taken
3014 from the formal namespace. */
3015 if (new_state == COMP_FUNCTION)
3017 if (prog_unit->result == prog_unit
3018 && prog_unit->ts.type == BT_UNKNOWN)
3019 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
3020 else if (prog_unit->result != prog_unit
3021 && prog_unit->result->ts.type == BT_UNKNOWN)
3022 gfc_set_default_type (prog_unit->result, 1,
3023 prog_unit->formal_ns);
3026 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
3028 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3029 gfc_ascii_statement (st));
3030 reject_statement ();
3031 goto decl;
3034 /* Add EXTERNAL attribute to function or subroutine. */
3035 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
3036 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
3038 current_interface = save;
3039 gfc_add_interface (prog_unit);
3040 pop_state ();
3042 if (current_interface.ns
3043 && current_interface.ns->proc_name
3044 && strcmp (current_interface.ns->proc_name->name,
3045 prog_unit->name) == 0)
3046 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3047 "enclosing procedure", prog_unit->name,
3048 &current_interface.ns->proc_name->declared_at);
3050 goto loop;
3052 done:
3053 pop_state ();
3057 /* Associate function characteristics by going back to the function
3058 declaration and rematching the prefix. */
3060 static match
3061 match_deferred_characteristics (gfc_typespec * ts)
3063 locus loc;
3064 match m = MATCH_ERROR;
3065 char name[GFC_MAX_SYMBOL_LEN + 1];
3067 loc = gfc_current_locus;
3069 gfc_current_locus = gfc_current_block ()->declared_at;
3071 gfc_clear_error ();
3072 gfc_buffer_error (true);
3073 m = gfc_match_prefix (ts);
3074 gfc_buffer_error (false);
3076 if (ts->type == BT_DERIVED)
3078 ts->kind = 0;
3080 if (!ts->u.derived)
3081 m = MATCH_ERROR;
3084 /* Only permit one go at the characteristic association. */
3085 if (ts->kind == -1)
3086 ts->kind = 0;
3088 /* Set the function locus correctly. If we have not found the
3089 function name, there is an error. */
3090 if (m == MATCH_YES
3091 && gfc_match ("function% %n", name) == MATCH_YES
3092 && strcmp (name, gfc_current_block ()->name) == 0)
3094 gfc_current_block ()->declared_at = gfc_current_locus;
3095 gfc_commit_symbols ();
3097 else
3099 gfc_error_check ();
3100 gfc_undo_symbols ();
3103 gfc_current_locus =loc;
3104 return m;
3108 /* Check specification-expressions in the function result of the currently
3109 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3110 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3111 scope are not yet parsed so this has to be delayed up to parse_spec. */
3113 static void
3114 check_function_result_typed (void)
3116 gfc_typespec ts;
3118 gcc_assert (gfc_current_state () == COMP_FUNCTION);
3120 if (!gfc_current_ns->proc_name->result) return;
3122 ts = gfc_current_ns->proc_name->result->ts;
3124 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3125 /* TODO: Extend when KIND type parameters are implemented. */
3126 if (ts.type == BT_CHARACTER && ts.u.cl && ts.u.cl->length)
3127 gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true);
3131 /* Parse a set of specification statements. Returns the statement
3132 that doesn't fit. */
3134 static gfc_statement
3135 parse_spec (gfc_statement st)
3137 st_state ss;
3138 bool function_result_typed = false;
3139 bool bad_characteristic = false;
3140 gfc_typespec *ts;
3142 verify_st_order (&ss, ST_NONE, false);
3143 if (st == ST_NONE)
3144 st = next_statement ();
3146 /* If we are not inside a function or don't have a result specified so far,
3147 do nothing special about it. */
3148 if (gfc_current_state () != COMP_FUNCTION)
3149 function_result_typed = true;
3150 else
3152 gfc_symbol* proc = gfc_current_ns->proc_name;
3153 gcc_assert (proc);
3155 if (proc->result->ts.type == BT_UNKNOWN)
3156 function_result_typed = true;
3159 loop:
3161 /* If we're inside a BLOCK construct, some statements are disallowed.
3162 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3163 or VALUE are also disallowed, but they don't have a particular ST_*
3164 key so we have to check for them individually in their matcher routine. */
3165 if (gfc_current_state () == COMP_BLOCK)
3166 switch (st)
3168 case ST_IMPLICIT:
3169 case ST_IMPLICIT_NONE:
3170 case ST_NAMELIST:
3171 case ST_COMMON:
3172 case ST_EQUIVALENCE:
3173 case ST_STATEMENT_FUNCTION:
3174 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3175 gfc_ascii_statement (st));
3176 reject_statement ();
3177 break;
3179 default:
3180 break;
3182 else if (gfc_current_state () == COMP_BLOCK_DATA)
3183 /* Fortran 2008, C1116. */
3184 switch (st)
3186 case ST_DATA_DECL:
3187 case ST_COMMON:
3188 case ST_DATA:
3189 case ST_TYPE:
3190 case ST_END_BLOCK_DATA:
3191 case ST_ATTR_DECL:
3192 case ST_EQUIVALENCE:
3193 case ST_PARAMETER:
3194 case ST_IMPLICIT:
3195 case ST_IMPLICIT_NONE:
3196 case ST_DERIVED_DECL:
3197 case ST_USE:
3198 break;
3200 case ST_NONE:
3201 break;
3203 default:
3204 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3205 gfc_ascii_statement (st));
3206 reject_statement ();
3207 break;
3210 /* If we find a statement that can not be followed by an IMPLICIT statement
3211 (and thus we can expect to see none any further), type the function result
3212 if it has not yet been typed. Be careful not to give the END statement
3213 to verify_st_order! */
3214 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
3216 bool verify_now = false;
3218 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
3219 verify_now = true;
3220 else
3222 st_state dummyss;
3223 verify_st_order (&dummyss, ST_NONE, false);
3224 verify_st_order (&dummyss, st, false);
3226 if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
3227 verify_now = true;
3230 if (verify_now)
3232 check_function_result_typed ();
3233 function_result_typed = true;
3237 switch (st)
3239 case ST_NONE:
3240 unexpected_eof ();
3242 case ST_IMPLICIT_NONE:
3243 case ST_IMPLICIT:
3244 if (!function_result_typed)
3246 check_function_result_typed ();
3247 function_result_typed = true;
3249 goto declSt;
3251 case ST_FORMAT:
3252 case ST_ENTRY:
3253 case ST_DATA: /* Not allowed in interfaces */
3254 if (gfc_current_state () == COMP_INTERFACE)
3255 break;
3257 /* Fall through */
3259 case ST_USE:
3260 case ST_IMPORT:
3261 case ST_PARAMETER:
3262 case ST_PUBLIC:
3263 case ST_PRIVATE:
3264 case ST_DERIVED_DECL:
3265 case_decl:
3266 declSt:
3267 if (!verify_st_order (&ss, st, false))
3269 reject_statement ();
3270 st = next_statement ();
3271 goto loop;
3274 switch (st)
3276 case ST_INTERFACE:
3277 parse_interface ();
3278 break;
3280 case ST_DERIVED_DECL:
3281 parse_derived ();
3282 break;
3284 case ST_PUBLIC:
3285 case ST_PRIVATE:
3286 if (gfc_current_state () != COMP_MODULE)
3288 gfc_error ("%s statement must appear in a MODULE",
3289 gfc_ascii_statement (st));
3290 reject_statement ();
3291 break;
3294 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
3296 gfc_error ("%s statement at %C follows another accessibility "
3297 "specification", gfc_ascii_statement (st));
3298 reject_statement ();
3299 break;
3302 gfc_current_ns->default_access = (st == ST_PUBLIC)
3303 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3305 break;
3307 case ST_STATEMENT_FUNCTION:
3308 if (gfc_current_state () == COMP_MODULE
3309 || gfc_current_state () == COMP_SUBMODULE)
3311 unexpected_statement (st);
3312 break;
3315 default:
3316 break;
3319 accept_statement (st);
3320 st = next_statement ();
3321 goto loop;
3323 case ST_ENUM:
3324 accept_statement (st);
3325 parse_enum();
3326 st = next_statement ();
3327 goto loop;
3329 case ST_GET_FCN_CHARACTERISTICS:
3330 /* This statement triggers the association of a function's result
3331 characteristics. */
3332 ts = &gfc_current_block ()->result->ts;
3333 if (match_deferred_characteristics (ts) != MATCH_YES)
3334 bad_characteristic = true;
3336 st = next_statement ();
3337 goto loop;
3339 case ST_OACC_DECLARE:
3340 if (!verify_st_order(&ss, st, false))
3342 reject_statement ();
3343 st = next_statement ();
3344 goto loop;
3346 if (gfc_state_stack->ext.oacc_declare_clauses == NULL)
3347 gfc_state_stack->ext.oacc_declare_clauses = new_st.ext.omp_clauses;
3348 accept_statement (st);
3349 st = next_statement ();
3350 goto loop;
3352 default:
3353 break;
3356 /* If match_deferred_characteristics failed, then there is an error. */
3357 if (bad_characteristic)
3359 ts = &gfc_current_block ()->result->ts;
3360 if (ts->type != BT_DERIVED)
3361 gfc_error ("Bad kind expression for function %qs at %L",
3362 gfc_current_block ()->name,
3363 &gfc_current_block ()->declared_at);
3364 else
3365 gfc_error ("The type for function %qs at %L is not accessible",
3366 gfc_current_block ()->name,
3367 &gfc_current_block ()->declared_at);
3369 gfc_current_block ()->ts.kind = 0;
3370 /* Keep the derived type; if it's bad, it will be discovered later. */
3371 if (!(ts->type == BT_DERIVED && ts->u.derived))
3372 ts->type = BT_UNKNOWN;
3375 return st;
3379 /* Parse a WHERE block, (not a simple WHERE statement). */
3381 static void
3382 parse_where_block (void)
3384 int seen_empty_else;
3385 gfc_code *top, *d;
3386 gfc_state_data s;
3387 gfc_statement st;
3389 accept_statement (ST_WHERE_BLOCK);
3390 top = gfc_state_stack->tail;
3392 push_state (&s, COMP_WHERE, gfc_new_block);
3394 d = add_statement ();
3395 d->expr1 = top->expr1;
3396 d->op = EXEC_WHERE;
3398 top->expr1 = NULL;
3399 top->block = d;
3401 seen_empty_else = 0;
3405 st = next_statement ();
3406 switch (st)
3408 case ST_NONE:
3409 unexpected_eof ();
3411 case ST_WHERE_BLOCK:
3412 parse_where_block ();
3413 break;
3415 case ST_ASSIGNMENT:
3416 case ST_WHERE:
3417 accept_statement (st);
3418 break;
3420 case ST_ELSEWHERE:
3421 if (seen_empty_else)
3423 gfc_error ("ELSEWHERE statement at %C follows previous "
3424 "unmasked ELSEWHERE");
3425 reject_statement ();
3426 break;
3429 if (new_st.expr1 == NULL)
3430 seen_empty_else = 1;
3432 d = new_level (gfc_state_stack->head);
3433 d->op = EXEC_WHERE;
3434 d->expr1 = new_st.expr1;
3436 accept_statement (st);
3438 break;
3440 case ST_END_WHERE:
3441 accept_statement (st);
3442 break;
3444 default:
3445 gfc_error ("Unexpected %s statement in WHERE block at %C",
3446 gfc_ascii_statement (st));
3447 reject_statement ();
3448 break;
3451 while (st != ST_END_WHERE);
3453 pop_state ();
3457 /* Parse a FORALL block (not a simple FORALL statement). */
3459 static void
3460 parse_forall_block (void)
3462 gfc_code *top, *d;
3463 gfc_state_data s;
3464 gfc_statement st;
3466 accept_statement (ST_FORALL_BLOCK);
3467 top = gfc_state_stack->tail;
3469 push_state (&s, COMP_FORALL, gfc_new_block);
3471 d = add_statement ();
3472 d->op = EXEC_FORALL;
3473 top->block = d;
3477 st = next_statement ();
3478 switch (st)
3481 case ST_ASSIGNMENT:
3482 case ST_POINTER_ASSIGNMENT:
3483 case ST_WHERE:
3484 case ST_FORALL:
3485 accept_statement (st);
3486 break;
3488 case ST_WHERE_BLOCK:
3489 parse_where_block ();
3490 break;
3492 case ST_FORALL_BLOCK:
3493 parse_forall_block ();
3494 break;
3496 case ST_END_FORALL:
3497 accept_statement (st);
3498 break;
3500 case ST_NONE:
3501 unexpected_eof ();
3503 default:
3504 gfc_error ("Unexpected %s statement in FORALL block at %C",
3505 gfc_ascii_statement (st));
3507 reject_statement ();
3508 break;
3511 while (st != ST_END_FORALL);
3513 pop_state ();
3517 static gfc_statement parse_executable (gfc_statement);
3519 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
3521 static void
3522 parse_if_block (void)
3524 gfc_code *top, *d;
3525 gfc_statement st;
3526 locus else_locus;
3527 gfc_state_data s;
3528 int seen_else;
3530 seen_else = 0;
3531 accept_statement (ST_IF_BLOCK);
3533 top = gfc_state_stack->tail;
3534 push_state (&s, COMP_IF, gfc_new_block);
3536 new_st.op = EXEC_IF;
3537 d = add_statement ();
3539 d->expr1 = top->expr1;
3540 top->expr1 = NULL;
3541 top->block = d;
3545 st = parse_executable (ST_NONE);
3547 switch (st)
3549 case ST_NONE:
3550 unexpected_eof ();
3552 case ST_ELSEIF:
3553 if (seen_else)
3555 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
3556 "statement at %L", &else_locus);
3558 reject_statement ();
3559 break;
3562 d = new_level (gfc_state_stack->head);
3563 d->op = EXEC_IF;
3564 d->expr1 = new_st.expr1;
3566 accept_statement (st);
3568 break;
3570 case ST_ELSE:
3571 if (seen_else)
3573 gfc_error ("Duplicate ELSE statements at %L and %C",
3574 &else_locus);
3575 reject_statement ();
3576 break;
3579 seen_else = 1;
3580 else_locus = gfc_current_locus;
3582 d = new_level (gfc_state_stack->head);
3583 d->op = EXEC_IF;
3585 accept_statement (st);
3587 break;
3589 case ST_ENDIF:
3590 break;
3592 default:
3593 unexpected_statement (st);
3594 break;
3597 while (st != ST_ENDIF);
3599 pop_state ();
3600 accept_statement (st);
3604 /* Parse a SELECT block. */
3606 static void
3607 parse_select_block (void)
3609 gfc_statement st;
3610 gfc_code *cp;
3611 gfc_state_data s;
3613 accept_statement (ST_SELECT_CASE);
3615 cp = gfc_state_stack->tail;
3616 push_state (&s, COMP_SELECT, gfc_new_block);
3618 /* Make sure that the next statement is a CASE or END SELECT. */
3619 for (;;)
3621 st = next_statement ();
3622 if (st == ST_NONE)
3623 unexpected_eof ();
3624 if (st == ST_END_SELECT)
3626 /* Empty SELECT CASE is OK. */
3627 accept_statement (st);
3628 pop_state ();
3629 return;
3631 if (st == ST_CASE)
3632 break;
3634 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
3635 "CASE at %C");
3637 reject_statement ();
3640 /* At this point, we're got a nonempty select block. */
3641 cp = new_level (cp);
3642 *cp = new_st;
3644 accept_statement (st);
3648 st = parse_executable (ST_NONE);
3649 switch (st)
3651 case ST_NONE:
3652 unexpected_eof ();
3654 case ST_CASE:
3655 cp = new_level (gfc_state_stack->head);
3656 *cp = new_st;
3657 gfc_clear_new_st ();
3659 accept_statement (st);
3660 /* Fall through */
3662 case ST_END_SELECT:
3663 break;
3665 /* Can't have an executable statement because of
3666 parse_executable(). */
3667 default:
3668 unexpected_statement (st);
3669 break;
3672 while (st != ST_END_SELECT);
3674 pop_state ();
3675 accept_statement (st);
3679 /* Pop the current selector from the SELECT TYPE stack. */
3681 static void
3682 select_type_pop (void)
3684 gfc_select_type_stack *old = select_type_stack;
3685 select_type_stack = old->prev;
3686 free (old);
3690 /* Parse a SELECT TYPE construct (F03:R821). */
3692 static void
3693 parse_select_type_block (void)
3695 gfc_statement st;
3696 gfc_code *cp;
3697 gfc_state_data s;
3699 accept_statement (ST_SELECT_TYPE);
3701 cp = gfc_state_stack->tail;
3702 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
3704 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
3705 or END SELECT. */
3706 for (;;)
3708 st = next_statement ();
3709 if (st == ST_NONE)
3710 unexpected_eof ();
3711 if (st == ST_END_SELECT)
3712 /* Empty SELECT CASE is OK. */
3713 goto done;
3714 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
3715 break;
3717 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
3718 "following SELECT TYPE at %C");
3720 reject_statement ();
3723 /* At this point, we're got a nonempty select block. */
3724 cp = new_level (cp);
3725 *cp = new_st;
3727 accept_statement (st);
3731 st = parse_executable (ST_NONE);
3732 switch (st)
3734 case ST_NONE:
3735 unexpected_eof ();
3737 case ST_TYPE_IS:
3738 case ST_CLASS_IS:
3739 cp = new_level (gfc_state_stack->head);
3740 *cp = new_st;
3741 gfc_clear_new_st ();
3743 accept_statement (st);
3744 /* Fall through */
3746 case ST_END_SELECT:
3747 break;
3749 /* Can't have an executable statement because of
3750 parse_executable(). */
3751 default:
3752 unexpected_statement (st);
3753 break;
3756 while (st != ST_END_SELECT);
3758 done:
3759 pop_state ();
3760 accept_statement (st);
3761 gfc_current_ns = gfc_current_ns->parent;
3762 select_type_pop ();
3766 /* Given a symbol, make sure it is not an iteration variable for a DO
3767 statement. This subroutine is called when the symbol is seen in a
3768 context that causes it to become redefined. If the symbol is an
3769 iterator, we generate an error message and return nonzero. */
3771 int
3772 gfc_check_do_variable (gfc_symtree *st)
3774 gfc_state_data *s;
3776 for (s=gfc_state_stack; s; s = s->previous)
3777 if (s->do_variable == st)
3779 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
3780 "loop beginning at %L", st->name, &s->head->loc);
3781 return 1;
3784 return 0;
3788 /* Checks to see if the current statement label closes an enddo.
3789 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3790 an error) if it incorrectly closes an ENDDO. */
3792 static int
3793 check_do_closure (void)
3795 gfc_state_data *p;
3797 if (gfc_statement_label == NULL)
3798 return 0;
3800 for (p = gfc_state_stack; p; p = p->previous)
3801 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
3802 break;
3804 if (p == NULL)
3805 return 0; /* No loops to close */
3807 if (p->ext.end_do_label == gfc_statement_label)
3809 if (p == gfc_state_stack)
3810 return 1;
3812 gfc_error ("End of nonblock DO statement at %C is within another block");
3813 return 2;
3816 /* At this point, the label doesn't terminate the innermost loop.
3817 Make sure it doesn't terminate another one. */
3818 for (; p; p = p->previous)
3819 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
3820 && p->ext.end_do_label == gfc_statement_label)
3822 gfc_error ("End of nonblock DO statement at %C is interwoven "
3823 "with another DO loop");
3824 return 2;
3827 return 0;
3831 /* Parse a series of contained program units. */
3833 static void parse_progunit (gfc_statement);
3836 /* Parse a CRITICAL block. */
3838 static void
3839 parse_critical_block (void)
3841 gfc_code *top, *d;
3842 gfc_state_data s, *sd;
3843 gfc_statement st;
3845 for (sd = gfc_state_stack; sd; sd = sd->previous)
3846 if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
3847 gfc_error_now (is_oacc (sd)
3848 ? "CRITICAL block inside of OpenACC region at %C"
3849 : "CRITICAL block inside of OpenMP region at %C");
3851 s.ext.end_do_label = new_st.label1;
3853 accept_statement (ST_CRITICAL);
3854 top = gfc_state_stack->tail;
3856 push_state (&s, COMP_CRITICAL, gfc_new_block);
3858 d = add_statement ();
3859 d->op = EXEC_CRITICAL;
3860 top->block = d;
3864 st = parse_executable (ST_NONE);
3866 switch (st)
3868 case ST_NONE:
3869 unexpected_eof ();
3870 break;
3872 case ST_END_CRITICAL:
3873 if (s.ext.end_do_label != NULL
3874 && s.ext.end_do_label != gfc_statement_label)
3875 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3876 "match CRITICAL label");
3878 if (gfc_statement_label != NULL)
3880 new_st.op = EXEC_NOP;
3881 add_statement ();
3883 break;
3885 default:
3886 unexpected_statement (st);
3887 break;
3890 while (st != ST_END_CRITICAL);
3892 pop_state ();
3893 accept_statement (st);
3897 /* Set up the local namespace for a BLOCK construct. */
3899 gfc_namespace*
3900 gfc_build_block_ns (gfc_namespace *parent_ns)
3902 gfc_namespace* my_ns;
3903 static int numblock = 1;
3905 my_ns = gfc_get_namespace (parent_ns, 1);
3906 my_ns->construct_entities = 1;
3908 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3909 code generation (so it must not be NULL).
3910 We set its recursive argument if our container procedure is recursive, so
3911 that local variables are accordingly placed on the stack when it
3912 will be necessary. */
3913 if (gfc_new_block)
3914 my_ns->proc_name = gfc_new_block;
3915 else
3917 bool t;
3918 char buffer[20]; /* Enough to hold "block@2147483648\n". */
3920 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
3921 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
3922 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
3923 my_ns->proc_name->name, NULL);
3924 gcc_assert (t);
3925 gfc_commit_symbol (my_ns->proc_name);
3928 if (parent_ns->proc_name)
3929 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
3931 return my_ns;
3935 /* Parse a BLOCK construct. */
3937 static void
3938 parse_block_construct (void)
3940 gfc_namespace* my_ns;
3941 gfc_namespace* my_parent;
3942 gfc_state_data s;
3944 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
3946 my_ns = gfc_build_block_ns (gfc_current_ns);
3948 new_st.op = EXEC_BLOCK;
3949 new_st.ext.block.ns = my_ns;
3950 new_st.ext.block.assoc = NULL;
3951 accept_statement (ST_BLOCK);
3953 push_state (&s, COMP_BLOCK, my_ns->proc_name);
3954 gfc_current_ns = my_ns;
3955 my_parent = my_ns->parent;
3957 parse_progunit (ST_NONE);
3959 /* Don't depend on the value of gfc_current_ns; it might have been
3960 reset if the block had errors and was cleaned up. */
3961 gfc_current_ns = my_parent;
3963 pop_state ();
3967 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3968 behind the scenes with compiler-generated variables. */
3970 static void
3971 parse_associate (void)
3973 gfc_namespace* my_ns;
3974 gfc_state_data s;
3975 gfc_statement st;
3976 gfc_association_list* a;
3978 gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
3980 my_ns = gfc_build_block_ns (gfc_current_ns);
3982 new_st.op = EXEC_BLOCK;
3983 new_st.ext.block.ns = my_ns;
3984 gcc_assert (new_st.ext.block.assoc);
3986 /* Add all associate-names as BLOCK variables. Creating them is enough
3987 for now, they'll get their values during trans-* phase. */
3988 gfc_current_ns = my_ns;
3989 for (a = new_st.ext.block.assoc; a; a = a->next)
3991 gfc_symbol* sym;
3992 gfc_ref *ref;
3993 gfc_array_ref *array_ref;
3995 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
3996 gcc_unreachable ();
3998 sym = a->st->n.sym;
3999 sym->attr.flavor = FL_VARIABLE;
4000 sym->assoc = a;
4001 sym->declared_at = a->where;
4002 gfc_set_sym_referenced (sym);
4004 /* Initialize the typespec. It is not available in all cases,
4005 however, as it may only be set on the target during resolution.
4006 Still, sometimes it helps to have it right now -- especially
4007 for parsing component references on the associate-name
4008 in case of association to a derived-type. */
4009 sym->ts = a->target->ts;
4011 /* Check if the target expression is array valued. This can not always
4012 be done by looking at target.rank, because that might not have been
4013 set yet. Therefore traverse the chain of refs, looking for the last
4014 array ref and evaluate that. */
4015 array_ref = NULL;
4016 for (ref = a->target->ref; ref; ref = ref->next)
4017 if (ref->type == REF_ARRAY)
4018 array_ref = &ref->u.ar;
4019 if (array_ref || a->target->rank)
4021 gfc_array_spec *as;
4022 int dim, rank = 0;
4023 if (array_ref)
4025 /* Count the dimension, that have a non-scalar extend. */
4026 for (dim = 0; dim < array_ref->dimen; ++dim)
4027 if (array_ref->dimen_type[dim] != DIMEN_ELEMENT
4028 && !(array_ref->dimen_type[dim] == DIMEN_UNKNOWN
4029 && array_ref->end[dim] == NULL
4030 && array_ref->start[dim] != NULL))
4031 ++rank;
4033 else
4034 rank = a->target->rank;
4035 /* When the rank is greater than zero then sym will be an array. */
4036 if (sym->ts.type == BT_CLASS)
4038 if ((!CLASS_DATA (sym)->as && rank != 0)
4039 || (CLASS_DATA (sym)->as
4040 && CLASS_DATA (sym)->as->rank != rank))
4042 /* Don't just (re-)set the attr and as in the sym.ts,
4043 because this modifies the target's attr and as. Copy the
4044 data and do a build_class_symbol. */
4045 symbol_attribute attr = CLASS_DATA (a->target)->attr;
4046 int corank = gfc_get_corank (a->target);
4047 gfc_typespec type;
4049 if (rank || corank)
4051 as = gfc_get_array_spec ();
4052 as->type = AS_DEFERRED;
4053 as->rank = rank;
4054 as->corank = corank;
4055 attr.dimension = rank ? 1 : 0;
4056 attr.codimension = corank ? 1 : 0;
4058 else
4060 as = NULL;
4061 attr.dimension = attr.codimension = 0;
4063 attr.class_ok = 0;
4064 type = CLASS_DATA (sym)->ts;
4065 if (!gfc_build_class_symbol (&type,
4066 &attr, &as))
4067 gcc_unreachable ();
4068 sym->ts = type;
4069 sym->ts.type = BT_CLASS;
4070 sym->attr.class_ok = 1;
4072 else
4073 sym->attr.class_ok = 1;
4075 else if ((!sym->as && rank != 0)
4076 || (sym->as && sym->as->rank != rank))
4078 as = gfc_get_array_spec ();
4079 as->type = AS_DEFERRED;
4080 as->rank = rank;
4081 as->corank = gfc_get_corank (a->target);
4082 sym->as = as;
4083 sym->attr.dimension = 1;
4084 if (as->corank)
4085 sym->attr.codimension = 1;
4090 accept_statement (ST_ASSOCIATE);
4091 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
4093 loop:
4094 st = parse_executable (ST_NONE);
4095 switch (st)
4097 case ST_NONE:
4098 unexpected_eof ();
4100 case_end:
4101 accept_statement (st);
4102 my_ns->code = gfc_state_stack->head;
4103 break;
4105 default:
4106 unexpected_statement (st);
4107 goto loop;
4110 gfc_current_ns = gfc_current_ns->parent;
4111 pop_state ();
4115 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4116 handled inside of parse_executable(), because they aren't really
4117 loop statements. */
4119 static void
4120 parse_do_block (void)
4122 gfc_statement st;
4123 gfc_code *top;
4124 gfc_state_data s;
4125 gfc_symtree *stree;
4126 gfc_exec_op do_op;
4128 do_op = new_st.op;
4129 s.ext.end_do_label = new_st.label1;
4131 if (new_st.ext.iterator != NULL)
4132 stree = new_st.ext.iterator->var->symtree;
4133 else
4134 stree = NULL;
4136 accept_statement (ST_DO);
4138 top = gfc_state_stack->tail;
4139 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
4140 gfc_new_block);
4142 s.do_variable = stree;
4144 top->block = new_level (top);
4145 top->block->op = EXEC_DO;
4147 loop:
4148 st = parse_executable (ST_NONE);
4150 switch (st)
4152 case ST_NONE:
4153 unexpected_eof ();
4155 case ST_ENDDO:
4156 if (s.ext.end_do_label != NULL
4157 && s.ext.end_do_label != gfc_statement_label)
4158 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4159 "DO label");
4161 if (gfc_statement_label != NULL)
4163 new_st.op = EXEC_NOP;
4164 add_statement ();
4166 break;
4168 case ST_IMPLIED_ENDDO:
4169 /* If the do-stmt of this DO construct has a do-construct-name,
4170 the corresponding end-do must be an end-do-stmt (with a matching
4171 name, but in that case we must have seen ST_ENDDO first).
4172 We only complain about this in pedantic mode. */
4173 if (gfc_current_block () != NULL)
4174 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4175 &gfc_current_block()->declared_at);
4177 break;
4179 default:
4180 unexpected_statement (st);
4181 goto loop;
4184 pop_state ();
4185 accept_statement (st);
4189 /* Parse the statements of OpenMP do/parallel do. */
4191 static gfc_statement
4192 parse_omp_do (gfc_statement omp_st)
4194 gfc_statement st;
4195 gfc_code *cp, *np;
4196 gfc_state_data s;
4198 accept_statement (omp_st);
4200 cp = gfc_state_stack->tail;
4201 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4202 np = new_level (cp);
4203 np->op = cp->op;
4204 np->block = NULL;
4206 for (;;)
4208 st = next_statement ();
4209 if (st == ST_NONE)
4210 unexpected_eof ();
4211 else if (st == ST_DO)
4212 break;
4213 else
4214 unexpected_statement (st);
4217 parse_do_block ();
4218 if (gfc_statement_label != NULL
4219 && gfc_state_stack->previous != NULL
4220 && gfc_state_stack->previous->state == COMP_DO
4221 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4223 /* In
4224 DO 100 I=1,10
4225 !$OMP DO
4226 DO J=1,10
4228 100 CONTINUE
4229 there should be no !$OMP END DO. */
4230 pop_state ();
4231 return ST_IMPLIED_ENDDO;
4234 check_do_closure ();
4235 pop_state ();
4237 st = next_statement ();
4238 gfc_statement omp_end_st = ST_OMP_END_DO;
4239 switch (omp_st)
4241 case ST_OMP_DISTRIBUTE: omp_end_st = ST_OMP_END_DISTRIBUTE; break;
4242 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4243 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4244 break;
4245 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4246 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4247 break;
4248 case ST_OMP_DISTRIBUTE_SIMD:
4249 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4250 break;
4251 case ST_OMP_DO: omp_end_st = ST_OMP_END_DO; break;
4252 case ST_OMP_DO_SIMD: omp_end_st = ST_OMP_END_DO_SIMD; break;
4253 case ST_OMP_PARALLEL_DO: omp_end_st = ST_OMP_END_PARALLEL_DO; break;
4254 case ST_OMP_PARALLEL_DO_SIMD:
4255 omp_end_st = ST_OMP_END_PARALLEL_DO_SIMD;
4256 break;
4257 case ST_OMP_SIMD: omp_end_st = ST_OMP_END_SIMD; break;
4258 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4259 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4260 break;
4261 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4262 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4263 break;
4264 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4265 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4266 break;
4267 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4268 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4269 break;
4270 case ST_OMP_TEAMS_DISTRIBUTE:
4271 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4272 break;
4273 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4274 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4275 break;
4276 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4277 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4278 break;
4279 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4280 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4281 break;
4282 default: gcc_unreachable ();
4284 if (st == omp_end_st)
4286 if (new_st.op == EXEC_OMP_END_NOWAIT)
4287 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4288 else
4289 gcc_assert (new_st.op == EXEC_NOP);
4290 gfc_clear_new_st ();
4291 gfc_commit_symbols ();
4292 gfc_warning_check ();
4293 st = next_statement ();
4295 return st;
4299 /* Parse the statements of OpenMP atomic directive. */
4301 static gfc_statement
4302 parse_omp_atomic (void)
4304 gfc_statement st;
4305 gfc_code *cp, *np;
4306 gfc_state_data s;
4307 int count;
4309 accept_statement (ST_OMP_ATOMIC);
4311 cp = gfc_state_stack->tail;
4312 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4313 np = new_level (cp);
4314 np->op = cp->op;
4315 np->block = NULL;
4316 count = 1 + ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4317 == GFC_OMP_ATOMIC_CAPTURE);
4319 while (count)
4321 st = next_statement ();
4322 if (st == ST_NONE)
4323 unexpected_eof ();
4324 else if (st == ST_ASSIGNMENT)
4326 accept_statement (st);
4327 count--;
4329 else
4330 unexpected_statement (st);
4333 pop_state ();
4335 st = next_statement ();
4336 if (st == ST_OMP_END_ATOMIC)
4338 gfc_clear_new_st ();
4339 gfc_commit_symbols ();
4340 gfc_warning_check ();
4341 st = next_statement ();
4343 else if ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4344 == GFC_OMP_ATOMIC_CAPTURE)
4345 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4346 return st;
4350 /* Parse the statements of an OpenACC structured block. */
4352 static void
4353 parse_oacc_structured_block (gfc_statement acc_st)
4355 gfc_statement st, acc_end_st;
4356 gfc_code *cp, *np;
4357 gfc_state_data s, *sd;
4359 for (sd = gfc_state_stack; sd; sd = sd->previous)
4360 if (sd->state == COMP_CRITICAL)
4361 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4363 accept_statement (acc_st);
4365 cp = gfc_state_stack->tail;
4366 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4367 np = new_level (cp);
4368 np->op = cp->op;
4369 np->block = NULL;
4370 switch (acc_st)
4372 case ST_OACC_PARALLEL:
4373 acc_end_st = ST_OACC_END_PARALLEL;
4374 break;
4375 case ST_OACC_KERNELS:
4376 acc_end_st = ST_OACC_END_KERNELS;
4377 break;
4378 case ST_OACC_DATA:
4379 acc_end_st = ST_OACC_END_DATA;
4380 break;
4381 case ST_OACC_HOST_DATA:
4382 acc_end_st = ST_OACC_END_HOST_DATA;
4383 break;
4384 default:
4385 gcc_unreachable ();
4390 st = parse_executable (ST_NONE);
4391 if (st == ST_NONE)
4392 unexpected_eof ();
4393 else if (st != acc_end_st)
4395 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
4396 reject_statement ();
4399 while (st != acc_end_st);
4401 gcc_assert (new_st.op == EXEC_NOP);
4403 gfc_clear_new_st ();
4404 gfc_commit_symbols ();
4405 gfc_warning_check ();
4406 pop_state ();
4409 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4411 static gfc_statement
4412 parse_oacc_loop (gfc_statement acc_st)
4414 gfc_statement st;
4415 gfc_code *cp, *np;
4416 gfc_state_data s, *sd;
4418 for (sd = gfc_state_stack; sd; sd = sd->previous)
4419 if (sd->state == COMP_CRITICAL)
4420 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4422 accept_statement (acc_st);
4424 cp = gfc_state_stack->tail;
4425 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4426 np = new_level (cp);
4427 np->op = cp->op;
4428 np->block = NULL;
4430 for (;;)
4432 st = next_statement ();
4433 if (st == ST_NONE)
4434 unexpected_eof ();
4435 else if (st == ST_DO)
4436 break;
4437 else
4439 gfc_error ("Expected DO loop at %C");
4440 reject_statement ();
4444 parse_do_block ();
4445 if (gfc_statement_label != NULL
4446 && gfc_state_stack->previous != NULL
4447 && gfc_state_stack->previous->state == COMP_DO
4448 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4450 pop_state ();
4451 return ST_IMPLIED_ENDDO;
4454 check_do_closure ();
4455 pop_state ();
4457 st = next_statement ();
4458 if (st == ST_OACC_END_LOOP)
4459 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
4460 if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
4461 (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
4462 (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
4464 gcc_assert (new_st.op == EXEC_NOP);
4465 gfc_clear_new_st ();
4466 gfc_commit_symbols ();
4467 gfc_warning_check ();
4468 st = next_statement ();
4470 return st;
4474 /* Parse the statements of an OpenMP structured block. */
4476 static void
4477 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
4479 gfc_statement st, omp_end_st;
4480 gfc_code *cp, *np;
4481 gfc_state_data s;
4483 accept_statement (omp_st);
4485 cp = gfc_state_stack->tail;
4486 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4487 np = new_level (cp);
4488 np->op = cp->op;
4489 np->block = NULL;
4491 switch (omp_st)
4493 case ST_OMP_PARALLEL:
4494 omp_end_st = ST_OMP_END_PARALLEL;
4495 break;
4496 case ST_OMP_PARALLEL_SECTIONS:
4497 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
4498 break;
4499 case ST_OMP_SECTIONS:
4500 omp_end_st = ST_OMP_END_SECTIONS;
4501 break;
4502 case ST_OMP_ORDERED:
4503 omp_end_st = ST_OMP_END_ORDERED;
4504 break;
4505 case ST_OMP_CRITICAL:
4506 omp_end_st = ST_OMP_END_CRITICAL;
4507 break;
4508 case ST_OMP_MASTER:
4509 omp_end_st = ST_OMP_END_MASTER;
4510 break;
4511 case ST_OMP_SINGLE:
4512 omp_end_st = ST_OMP_END_SINGLE;
4513 break;
4514 case ST_OMP_TARGET:
4515 omp_end_st = ST_OMP_END_TARGET;
4516 break;
4517 case ST_OMP_TARGET_DATA:
4518 omp_end_st = ST_OMP_END_TARGET_DATA;
4519 break;
4520 case ST_OMP_TARGET_TEAMS:
4521 omp_end_st = ST_OMP_END_TARGET_TEAMS;
4522 break;
4523 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4524 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4525 break;
4526 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4527 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4528 break;
4529 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4530 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4531 break;
4532 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4533 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4534 break;
4535 case ST_OMP_TASK:
4536 omp_end_st = ST_OMP_END_TASK;
4537 break;
4538 case ST_OMP_TASKGROUP:
4539 omp_end_st = ST_OMP_END_TASKGROUP;
4540 break;
4541 case ST_OMP_TEAMS:
4542 omp_end_st = ST_OMP_END_TEAMS;
4543 break;
4544 case ST_OMP_TEAMS_DISTRIBUTE:
4545 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4546 break;
4547 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4548 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4549 break;
4550 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4551 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4552 break;
4553 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4554 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4555 break;
4556 case ST_OMP_DISTRIBUTE:
4557 omp_end_st = ST_OMP_END_DISTRIBUTE;
4558 break;
4559 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4560 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4561 break;
4562 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4563 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4564 break;
4565 case ST_OMP_DISTRIBUTE_SIMD:
4566 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4567 break;
4568 case ST_OMP_WORKSHARE:
4569 omp_end_st = ST_OMP_END_WORKSHARE;
4570 break;
4571 case ST_OMP_PARALLEL_WORKSHARE:
4572 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
4573 break;
4574 default:
4575 gcc_unreachable ();
4580 if (workshare_stmts_only)
4582 /* Inside of !$omp workshare, only
4583 scalar assignments
4584 array assignments
4585 where statements and constructs
4586 forall statements and constructs
4587 !$omp atomic
4588 !$omp critical
4589 !$omp parallel
4590 are allowed. For !$omp critical these
4591 restrictions apply recursively. */
4592 bool cycle = true;
4594 st = next_statement ();
4595 for (;;)
4597 switch (st)
4599 case ST_NONE:
4600 unexpected_eof ();
4602 case ST_ASSIGNMENT:
4603 case ST_WHERE:
4604 case ST_FORALL:
4605 accept_statement (st);
4606 break;
4608 case ST_WHERE_BLOCK:
4609 parse_where_block ();
4610 break;
4612 case ST_FORALL_BLOCK:
4613 parse_forall_block ();
4614 break;
4616 case ST_OMP_PARALLEL:
4617 case ST_OMP_PARALLEL_SECTIONS:
4618 parse_omp_structured_block (st, false);
4619 break;
4621 case ST_OMP_PARALLEL_WORKSHARE:
4622 case ST_OMP_CRITICAL:
4623 parse_omp_structured_block (st, true);
4624 break;
4626 case ST_OMP_PARALLEL_DO:
4627 case ST_OMP_PARALLEL_DO_SIMD:
4628 st = parse_omp_do (st);
4629 continue;
4631 case ST_OMP_ATOMIC:
4632 st = parse_omp_atomic ();
4633 continue;
4635 default:
4636 cycle = false;
4637 break;
4640 if (!cycle)
4641 break;
4643 st = next_statement ();
4646 else
4647 st = parse_executable (ST_NONE);
4648 if (st == ST_NONE)
4649 unexpected_eof ();
4650 else if (st == ST_OMP_SECTION
4651 && (omp_st == ST_OMP_SECTIONS
4652 || omp_st == ST_OMP_PARALLEL_SECTIONS))
4654 np = new_level (np);
4655 np->op = cp->op;
4656 np->block = NULL;
4658 else if (st != omp_end_st)
4659 unexpected_statement (st);
4661 while (st != omp_end_st);
4663 switch (new_st.op)
4665 case EXEC_OMP_END_NOWAIT:
4666 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4667 break;
4668 case EXEC_OMP_CRITICAL:
4669 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
4670 || (new_st.ext.omp_name != NULL
4671 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
4672 gfc_error ("Name after !$omp critical and !$omp end critical does "
4673 "not match at %C");
4674 free (CONST_CAST (char *, new_st.ext.omp_name));
4675 break;
4676 case EXEC_OMP_END_SINGLE:
4677 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
4678 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
4679 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
4680 gfc_free_omp_clauses (new_st.ext.omp_clauses);
4681 break;
4682 case EXEC_NOP:
4683 break;
4684 default:
4685 gcc_unreachable ();
4688 gfc_clear_new_st ();
4689 gfc_commit_symbols ();
4690 gfc_warning_check ();
4691 pop_state ();
4695 /* Accept a series of executable statements. We return the first
4696 statement that doesn't fit to the caller. Any block statements are
4697 passed on to the correct handler, which usually passes the buck
4698 right back here. */
4700 static gfc_statement
4701 parse_executable (gfc_statement st)
4703 int close_flag;
4705 if (st == ST_NONE)
4706 st = next_statement ();
4708 for (;;)
4710 close_flag = check_do_closure ();
4711 if (close_flag)
4712 switch (st)
4714 case ST_GOTO:
4715 case ST_END_PROGRAM:
4716 case ST_RETURN:
4717 case ST_EXIT:
4718 case ST_END_FUNCTION:
4719 case ST_CYCLE:
4720 case ST_PAUSE:
4721 case ST_STOP:
4722 case ST_ERROR_STOP:
4723 case ST_END_SUBROUTINE:
4725 case ST_DO:
4726 case ST_FORALL:
4727 case ST_WHERE:
4728 case ST_SELECT_CASE:
4729 gfc_error ("%s statement at %C cannot terminate a non-block "
4730 "DO loop", gfc_ascii_statement (st));
4731 break;
4733 default:
4734 break;
4737 switch (st)
4739 case ST_NONE:
4740 unexpected_eof ();
4742 case ST_DATA:
4743 gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
4744 "first executable statement");
4745 /* Fall through. */
4747 case ST_FORMAT:
4748 case ST_ENTRY:
4749 case_executable:
4750 accept_statement (st);
4751 if (close_flag == 1)
4752 return ST_IMPLIED_ENDDO;
4753 break;
4755 case ST_BLOCK:
4756 parse_block_construct ();
4757 break;
4759 case ST_ASSOCIATE:
4760 parse_associate ();
4761 break;
4763 case ST_IF_BLOCK:
4764 parse_if_block ();
4765 break;
4767 case ST_SELECT_CASE:
4768 parse_select_block ();
4769 break;
4771 case ST_SELECT_TYPE:
4772 parse_select_type_block();
4773 break;
4775 case ST_DO:
4776 parse_do_block ();
4777 if (check_do_closure () == 1)
4778 return ST_IMPLIED_ENDDO;
4779 break;
4781 case ST_CRITICAL:
4782 parse_critical_block ();
4783 break;
4785 case ST_WHERE_BLOCK:
4786 parse_where_block ();
4787 break;
4789 case ST_FORALL_BLOCK:
4790 parse_forall_block ();
4791 break;
4793 case ST_OACC_PARALLEL_LOOP:
4794 case ST_OACC_KERNELS_LOOP:
4795 case ST_OACC_LOOP:
4796 st = parse_oacc_loop (st);
4797 if (st == ST_IMPLIED_ENDDO)
4798 return st;
4799 continue;
4801 case ST_OACC_PARALLEL:
4802 case ST_OACC_KERNELS:
4803 case ST_OACC_DATA:
4804 case ST_OACC_HOST_DATA:
4805 parse_oacc_structured_block (st);
4806 break;
4808 case ST_OMP_PARALLEL:
4809 case ST_OMP_PARALLEL_SECTIONS:
4810 case ST_OMP_SECTIONS:
4811 case ST_OMP_ORDERED:
4812 case ST_OMP_CRITICAL:
4813 case ST_OMP_MASTER:
4814 case ST_OMP_SINGLE:
4815 case ST_OMP_TARGET:
4816 case ST_OMP_TARGET_DATA:
4817 case ST_OMP_TARGET_TEAMS:
4818 case ST_OMP_TEAMS:
4819 case ST_OMP_TASK:
4820 case ST_OMP_TASKGROUP:
4821 parse_omp_structured_block (st, false);
4822 break;
4824 case ST_OMP_WORKSHARE:
4825 case ST_OMP_PARALLEL_WORKSHARE:
4826 parse_omp_structured_block (st, true);
4827 break;
4829 case ST_OMP_DISTRIBUTE:
4830 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4831 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4832 case ST_OMP_DISTRIBUTE_SIMD:
4833 case ST_OMP_DO:
4834 case ST_OMP_DO_SIMD:
4835 case ST_OMP_PARALLEL_DO:
4836 case ST_OMP_PARALLEL_DO_SIMD:
4837 case ST_OMP_SIMD:
4838 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4839 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4840 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4841 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4842 case ST_OMP_TEAMS_DISTRIBUTE:
4843 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4844 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4845 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4846 st = parse_omp_do (st);
4847 if (st == ST_IMPLIED_ENDDO)
4848 return st;
4849 continue;
4851 case ST_OMP_ATOMIC:
4852 st = parse_omp_atomic ();
4853 continue;
4855 default:
4856 return st;
4859 st = next_statement ();
4864 /* Fix the symbols for sibling functions. These are incorrectly added to
4865 the child namespace as the parser didn't know about this procedure. */
4867 static void
4868 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
4870 gfc_namespace *ns;
4871 gfc_symtree *st;
4872 gfc_symbol *old_sym;
4874 for (ns = siblings; ns; ns = ns->sibling)
4876 st = gfc_find_symtree (ns->sym_root, sym->name);
4878 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
4879 goto fixup_contained;
4881 if ((st->n.sym->attr.flavor == FL_DERIVED
4882 && sym->attr.generic && sym->attr.function)
4883 ||(sym->attr.flavor == FL_DERIVED
4884 && st->n.sym->attr.generic && st->n.sym->attr.function))
4885 goto fixup_contained;
4887 old_sym = st->n.sym;
4888 if (old_sym->ns == ns
4889 && !old_sym->attr.contained
4891 /* By 14.6.1.3, host association should be excluded
4892 for the following. */
4893 && !(old_sym->attr.external
4894 || (old_sym->ts.type != BT_UNKNOWN
4895 && !old_sym->attr.implicit_type)
4896 || old_sym->attr.flavor == FL_PARAMETER
4897 || old_sym->attr.use_assoc
4898 || old_sym->attr.in_common
4899 || old_sym->attr.in_equivalence
4900 || old_sym->attr.data
4901 || old_sym->attr.dummy
4902 || old_sym->attr.result
4903 || old_sym->attr.dimension
4904 || old_sym->attr.allocatable
4905 || old_sym->attr.intrinsic
4906 || old_sym->attr.generic
4907 || old_sym->attr.flavor == FL_NAMELIST
4908 || old_sym->attr.flavor == FL_LABEL
4909 || old_sym->attr.proc == PROC_ST_FUNCTION))
4911 /* Replace it with the symbol from the parent namespace. */
4912 st->n.sym = sym;
4913 sym->refs++;
4915 gfc_release_symbol (old_sym);
4918 fixup_contained:
4919 /* Do the same for any contained procedures. */
4920 gfc_fixup_sibling_symbols (sym, ns->contained);
4924 static void
4925 parse_contained (int module)
4927 gfc_namespace *ns, *parent_ns, *tmp;
4928 gfc_state_data s1, s2;
4929 gfc_statement st;
4930 gfc_symbol *sym;
4931 gfc_entry_list *el;
4932 int contains_statements = 0;
4933 int seen_error = 0;
4935 push_state (&s1, COMP_CONTAINS, NULL);
4936 parent_ns = gfc_current_ns;
4940 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
4942 gfc_current_ns->sibling = parent_ns->contained;
4943 parent_ns->contained = gfc_current_ns;
4945 next:
4946 /* Process the next available statement. We come here if we got an error
4947 and rejected the last statement. */
4948 st = next_statement ();
4950 switch (st)
4952 case ST_NONE:
4953 unexpected_eof ();
4955 case ST_FUNCTION:
4956 case ST_SUBROUTINE:
4957 contains_statements = 1;
4958 accept_statement (st);
4960 push_state (&s2,
4961 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
4962 gfc_new_block);
4964 /* For internal procedures, create/update the symbol in the
4965 parent namespace. */
4967 if (!module)
4969 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
4970 gfc_error ("Contained procedure %qs at %C is already "
4971 "ambiguous", gfc_new_block->name);
4972 else
4974 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
4975 sym->name,
4976 &gfc_new_block->declared_at))
4978 if (st == ST_FUNCTION)
4979 gfc_add_function (&sym->attr, sym->name,
4980 &gfc_new_block->declared_at);
4981 else
4982 gfc_add_subroutine (&sym->attr, sym->name,
4983 &gfc_new_block->declared_at);
4987 gfc_commit_symbols ();
4989 else
4990 sym = gfc_new_block;
4992 /* Mark this as a contained function, so it isn't replaced
4993 by other module functions. */
4994 sym->attr.contained = 1;
4996 /* Set implicit_pure so that it can be reset if any of the
4997 tests for purity fail. This is used for some optimisation
4998 during translation. */
4999 if (!sym->attr.pure)
5000 sym->attr.implicit_pure = 1;
5002 parse_progunit (ST_NONE);
5004 /* Fix up any sibling functions that refer to this one. */
5005 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
5006 /* Or refer to any of its alternate entry points. */
5007 for (el = gfc_current_ns->entries; el; el = el->next)
5008 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
5010 gfc_current_ns->code = s2.head;
5011 gfc_current_ns = parent_ns;
5013 pop_state ();
5014 break;
5016 /* These statements are associated with the end of the host unit. */
5017 case ST_END_FUNCTION:
5018 case ST_END_MODULE:
5019 case ST_END_SUBMODULE:
5020 case ST_END_PROGRAM:
5021 case ST_END_SUBROUTINE:
5022 accept_statement (st);
5023 gfc_current_ns->code = s1.head;
5024 break;
5026 default:
5027 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
5028 gfc_ascii_statement (st));
5029 reject_statement ();
5030 seen_error = 1;
5031 goto next;
5032 break;
5035 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
5036 && st != ST_END_MODULE && st != ST_END_SUBMODULE
5037 && st != ST_END_PROGRAM);
5039 /* The first namespace in the list is guaranteed to not have
5040 anything (worthwhile) in it. */
5041 tmp = gfc_current_ns;
5042 gfc_current_ns = parent_ns;
5043 if (seen_error && tmp->refs > 1)
5044 gfc_free_namespace (tmp);
5046 ns = gfc_current_ns->contained;
5047 gfc_current_ns->contained = ns->sibling;
5048 gfc_free_namespace (ns);
5050 pop_state ();
5051 if (!contains_statements)
5052 gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
5053 "FUNCTION or SUBROUTINE statement at %C");
5057 /* The result variable in a MODULE PROCEDURE needs to be created and
5058 its characteristics copied from the interface since it is neither
5059 declared in the procedure declaration nor in the specification
5060 part. */
5062 static void
5063 get_modproc_result (void)
5065 gfc_symbol *proc;
5066 if (gfc_state_stack->previous
5067 && gfc_state_stack->previous->state == COMP_CONTAINS
5068 && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
5070 proc = gfc_current_ns->proc_name ? gfc_current_ns->proc_name : NULL;
5071 if (proc != NULL
5072 && proc->attr.function
5073 && proc->ts.interface
5074 && proc->ts.interface->result
5075 && proc->ts.interface->result != proc->ts.interface)
5077 gfc_copy_dummy_sym (&proc->result, proc->ts.interface->result, 1);
5078 gfc_set_sym_referenced (proc->result);
5079 proc->result->attr.if_source = IFSRC_DECL;
5080 gfc_commit_symbol (proc->result);
5086 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
5088 static void
5089 parse_progunit (gfc_statement st)
5091 gfc_state_data *p;
5092 int n;
5094 if (gfc_new_block
5095 && gfc_new_block->abr_modproc_decl
5096 && gfc_new_block->attr.function)
5097 get_modproc_result ();
5099 st = parse_spec (st);
5100 switch (st)
5102 case ST_NONE:
5103 unexpected_eof ();
5105 case ST_CONTAINS:
5106 /* This is not allowed within BLOCK! */
5107 if (gfc_current_state () != COMP_BLOCK)
5108 goto contains;
5109 break;
5111 case_end:
5112 accept_statement (st);
5113 goto done;
5115 default:
5116 break;
5119 if (gfc_current_state () == COMP_FUNCTION)
5120 gfc_check_function_type (gfc_current_ns);
5122 loop:
5123 for (;;)
5125 st = parse_executable (st);
5127 switch (st)
5129 case ST_NONE:
5130 unexpected_eof ();
5132 case ST_CONTAINS:
5133 /* This is not allowed within BLOCK! */
5134 if (gfc_current_state () != COMP_BLOCK)
5135 goto contains;
5136 break;
5138 case_end:
5139 accept_statement (st);
5140 goto done;
5142 default:
5143 break;
5146 unexpected_statement (st);
5147 reject_statement ();
5148 st = next_statement ();
5151 contains:
5152 n = 0;
5154 for (p = gfc_state_stack; p; p = p->previous)
5155 if (p->state == COMP_CONTAINS)
5156 n++;
5158 if (gfc_find_state (COMP_MODULE) == true
5159 || gfc_find_state (COMP_SUBMODULE) == true)
5160 n--;
5162 if (n > 0)
5164 gfc_error ("CONTAINS statement at %C is already in a contained "
5165 "program unit");
5166 reject_statement ();
5167 st = next_statement ();
5168 goto loop;
5171 parse_contained (0);
5173 done:
5174 gfc_current_ns->code = gfc_state_stack->head;
5175 if (gfc_state_stack->state == COMP_PROGRAM
5176 || gfc_state_stack->state == COMP_MODULE
5177 || gfc_state_stack->state == COMP_SUBROUTINE
5178 || gfc_state_stack->state == COMP_FUNCTION
5179 || gfc_state_stack->state == COMP_BLOCK)
5180 gfc_current_ns->oacc_declare_clauses
5181 = gfc_state_stack->ext.oacc_declare_clauses;
5185 /* Come here to complain about a global symbol already in use as
5186 something else. */
5188 void
5189 gfc_global_used (gfc_gsymbol *sym, locus *where)
5191 const char *name;
5193 if (where == NULL)
5194 where = &gfc_current_locus;
5196 switch(sym->type)
5198 case GSYM_PROGRAM:
5199 name = "PROGRAM";
5200 break;
5201 case GSYM_FUNCTION:
5202 name = "FUNCTION";
5203 break;
5204 case GSYM_SUBROUTINE:
5205 name = "SUBROUTINE";
5206 break;
5207 case GSYM_COMMON:
5208 name = "COMMON";
5209 break;
5210 case GSYM_BLOCK_DATA:
5211 name = "BLOCK DATA";
5212 break;
5213 case GSYM_MODULE:
5214 name = "MODULE";
5215 break;
5216 default:
5217 gfc_internal_error ("gfc_global_used(): Bad type");
5218 name = NULL;
5221 if (sym->binding_label)
5222 gfc_error ("Global binding name %qs at %L is already being used as a %s "
5223 "at %L", sym->binding_label, where, name, &sym->where);
5224 else
5225 gfc_error ("Global name %qs at %L is already being used as a %s at %L",
5226 sym->name, where, name, &sym->where);
5230 /* Parse a block data program unit. */
5232 static void
5233 parse_block_data (void)
5235 gfc_statement st;
5236 static locus blank_locus;
5237 static int blank_block=0;
5238 gfc_gsymbol *s;
5240 gfc_current_ns->proc_name = gfc_new_block;
5241 gfc_current_ns->is_block_data = 1;
5243 if (gfc_new_block == NULL)
5245 if (blank_block)
5246 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5247 "prior BLOCK DATA at %L", &blank_locus);
5248 else
5250 blank_block = 1;
5251 blank_locus = gfc_current_locus;
5254 else
5256 s = gfc_get_gsymbol (gfc_new_block->name);
5257 if (s->defined
5258 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
5259 gfc_global_used (s, &gfc_new_block->declared_at);
5260 else
5262 s->type = GSYM_BLOCK_DATA;
5263 s->where = gfc_new_block->declared_at;
5264 s->defined = 1;
5268 st = parse_spec (ST_NONE);
5270 while (st != ST_END_BLOCK_DATA)
5272 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5273 gfc_ascii_statement (st));
5274 reject_statement ();
5275 st = next_statement ();
5280 /* Following the association of the ancestor (sub)module symbols, they
5281 must be set host rather than use associated and all must be public.
5282 They are flagged up by 'used_in_submodule' so that they can be set
5283 DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl). Otherwise the
5284 linker chokes on multiple symbol definitions. */
5286 static void
5287 set_syms_host_assoc (gfc_symbol *sym)
5289 gfc_component *c;
5291 if (sym == NULL)
5292 return;
5294 if (sym->attr.module_procedure)
5295 sym->attr.external = 0;
5297 /* sym->attr.access = ACCESS_PUBLIC; */
5299 sym->attr.use_assoc = 0;
5300 sym->attr.host_assoc = 1;
5301 sym->attr.used_in_submodule =1;
5303 if (sym->attr.flavor == FL_DERIVED)
5305 for (c = sym->components; c; c = c->next)
5306 c->attr.access = ACCESS_PUBLIC;
5310 /* Parse a module subprogram. */
5312 static void
5313 parse_module (void)
5315 gfc_statement st;
5316 gfc_gsymbol *s;
5317 bool error;
5319 s = gfc_get_gsymbol (gfc_new_block->name);
5320 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
5321 gfc_global_used (s, &gfc_new_block->declared_at);
5322 else
5324 s->type = GSYM_MODULE;
5325 s->where = gfc_new_block->declared_at;
5326 s->defined = 1;
5329 /* Something is nulling the module_list after this point. This is good
5330 since it allows us to 'USE' the parent modules that the submodule
5331 inherits and to set (most) of the symbols as host associated. */
5332 if (gfc_current_state () == COMP_SUBMODULE)
5334 use_modules ();
5335 gfc_traverse_ns (gfc_current_ns, set_syms_host_assoc);
5338 st = parse_spec (ST_NONE);
5340 error = false;
5341 loop:
5342 switch (st)
5344 case ST_NONE:
5345 unexpected_eof ();
5347 case ST_CONTAINS:
5348 parse_contained (1);
5349 break;
5351 case ST_END_MODULE:
5352 case ST_END_SUBMODULE:
5353 accept_statement (st);
5354 break;
5356 default:
5357 gfc_error ("Unexpected %s statement in MODULE at %C",
5358 gfc_ascii_statement (st));
5360 error = true;
5361 reject_statement ();
5362 st = next_statement ();
5363 goto loop;
5366 /* Make sure not to free the namespace twice on error. */
5367 if (!error)
5368 s->ns = gfc_current_ns;
5372 /* Add a procedure name to the global symbol table. */
5374 static void
5375 add_global_procedure (bool sub)
5377 gfc_gsymbol *s;
5379 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5380 name is a global identifier. */
5381 if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
5383 s = gfc_get_gsymbol (gfc_new_block->name);
5385 if (s->defined
5386 || (s->type != GSYM_UNKNOWN
5387 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5389 gfc_global_used (s, &gfc_new_block->declared_at);
5390 /* Silence follow-up errors. */
5391 gfc_new_block->binding_label = NULL;
5393 else
5395 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5396 s->sym_name = gfc_new_block->name;
5397 s->where = gfc_new_block->declared_at;
5398 s->defined = 1;
5399 s->ns = gfc_current_ns;
5403 /* Don't add the symbol multiple times. */
5404 if (gfc_new_block->binding_label
5405 && (!gfc_notification_std (GFC_STD_F2008)
5406 || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
5408 s = gfc_get_gsymbol (gfc_new_block->binding_label);
5410 if (s->defined
5411 || (s->type != GSYM_UNKNOWN
5412 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5414 gfc_global_used (s, &gfc_new_block->declared_at);
5415 /* Silence follow-up errors. */
5416 gfc_new_block->binding_label = NULL;
5418 else
5420 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5421 s->sym_name = gfc_new_block->name;
5422 s->binding_label = gfc_new_block->binding_label;
5423 s->where = gfc_new_block->declared_at;
5424 s->defined = 1;
5425 s->ns = gfc_current_ns;
5431 /* Add a program to the global symbol table. */
5433 static void
5434 add_global_program (void)
5436 gfc_gsymbol *s;
5438 if (gfc_new_block == NULL)
5439 return;
5440 s = gfc_get_gsymbol (gfc_new_block->name);
5442 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
5443 gfc_global_used (s, &gfc_new_block->declared_at);
5444 else
5446 s->type = GSYM_PROGRAM;
5447 s->where = gfc_new_block->declared_at;
5448 s->defined = 1;
5449 s->ns = gfc_current_ns;
5454 /* Resolve all the program units. */
5455 static void
5456 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
5458 gfc_free_dt_list ();
5459 gfc_current_ns = gfc_global_ns_list;
5460 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5462 if (gfc_current_ns->proc_name
5463 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5464 continue; /* Already resolved. */
5466 if (gfc_current_ns->proc_name)
5467 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5468 gfc_resolve (gfc_current_ns);
5469 gfc_current_ns->derived_types = gfc_derived_types;
5470 gfc_derived_types = NULL;
5475 static void
5476 clean_up_modules (gfc_gsymbol *gsym)
5478 if (gsym == NULL)
5479 return;
5481 clean_up_modules (gsym->left);
5482 clean_up_modules (gsym->right);
5484 if (gsym->type != GSYM_MODULE || !gsym->ns)
5485 return;
5487 gfc_current_ns = gsym->ns;
5488 gfc_derived_types = gfc_current_ns->derived_types;
5489 gfc_done_2 ();
5490 gsym->ns = NULL;
5491 return;
5495 /* Translate all the program units. This could be in a different order
5496 to resolution if there are forward references in the file. */
5497 static void
5498 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
5500 int errors;
5502 gfc_current_ns = gfc_global_ns_list;
5503 gfc_get_errors (NULL, &errors);
5505 /* We first translate all modules to make sure that later parts
5506 of the program can use the decl. Then we translate the nonmodules. */
5508 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5510 if (!gfc_current_ns->proc_name
5511 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5512 continue;
5514 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5515 gfc_derived_types = gfc_current_ns->derived_types;
5516 gfc_generate_module_code (gfc_current_ns);
5517 gfc_current_ns->translated = 1;
5520 gfc_current_ns = gfc_global_ns_list;
5521 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5523 if (gfc_current_ns->proc_name
5524 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5525 continue;
5527 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5528 gfc_derived_types = gfc_current_ns->derived_types;
5529 gfc_generate_code (gfc_current_ns);
5530 gfc_current_ns->translated = 1;
5533 /* Clean up all the namespaces after translation. */
5534 gfc_current_ns = gfc_global_ns_list;
5535 for (;gfc_current_ns;)
5537 gfc_namespace *ns;
5539 if (gfc_current_ns->proc_name
5540 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5542 gfc_current_ns = gfc_current_ns->sibling;
5543 continue;
5546 ns = gfc_current_ns->sibling;
5547 gfc_derived_types = gfc_current_ns->derived_types;
5548 gfc_done_2 ();
5549 gfc_current_ns = ns;
5552 clean_up_modules (gfc_gsym_root);
5556 /* Top level parser. */
5558 bool
5559 gfc_parse_file (void)
5561 int seen_program, errors_before, errors;
5562 gfc_state_data top, s;
5563 gfc_statement st;
5564 locus prog_locus;
5565 gfc_namespace *next;
5567 gfc_start_source_files ();
5569 top.state = COMP_NONE;
5570 top.sym = NULL;
5571 top.previous = NULL;
5572 top.head = top.tail = NULL;
5573 top.do_variable = NULL;
5575 gfc_state_stack = &top;
5577 gfc_clear_new_st ();
5579 gfc_statement_label = NULL;
5581 if (setjmp (eof_buf))
5582 return false; /* Come here on unexpected EOF */
5584 /* Prepare the global namespace that will contain the
5585 program units. */
5586 gfc_global_ns_list = next = NULL;
5588 seen_program = 0;
5589 errors_before = 0;
5591 /* Exit early for empty files. */
5592 if (gfc_at_eof ())
5593 goto done;
5595 loop:
5596 gfc_init_2 ();
5597 st = next_statement ();
5598 switch (st)
5600 case ST_NONE:
5601 gfc_done_2 ();
5602 goto done;
5604 case ST_PROGRAM:
5605 if (seen_program)
5606 goto duplicate_main;
5607 seen_program = 1;
5608 prog_locus = gfc_current_locus;
5610 push_state (&s, COMP_PROGRAM, gfc_new_block);
5611 main_program_symbol(gfc_current_ns, gfc_new_block->name);
5612 accept_statement (st);
5613 add_global_program ();
5614 parse_progunit (ST_NONE);
5615 goto prog_units;
5616 break;
5618 case ST_SUBROUTINE:
5619 add_global_procedure (true);
5620 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
5621 accept_statement (st);
5622 parse_progunit (ST_NONE);
5623 goto prog_units;
5624 break;
5626 case ST_FUNCTION:
5627 add_global_procedure (false);
5628 push_state (&s, COMP_FUNCTION, gfc_new_block);
5629 accept_statement (st);
5630 parse_progunit (ST_NONE);
5631 goto prog_units;
5632 break;
5634 case ST_BLOCK_DATA:
5635 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
5636 accept_statement (st);
5637 parse_block_data ();
5638 break;
5640 case ST_MODULE:
5641 push_state (&s, COMP_MODULE, gfc_new_block);
5642 accept_statement (st);
5644 gfc_get_errors (NULL, &errors_before);
5645 parse_module ();
5646 break;
5648 case ST_SUBMODULE:
5649 push_state (&s, COMP_SUBMODULE, gfc_new_block);
5650 accept_statement (st);
5652 gfc_get_errors (NULL, &errors_before);
5653 parse_module ();
5654 break;
5656 /* Anything else starts a nameless main program block. */
5657 default:
5658 if (seen_program)
5659 goto duplicate_main;
5660 seen_program = 1;
5661 prog_locus = gfc_current_locus;
5663 push_state (&s, COMP_PROGRAM, gfc_new_block);
5664 main_program_symbol (gfc_current_ns, "MAIN__");
5665 parse_progunit (st);
5666 goto prog_units;
5667 break;
5670 /* Handle the non-program units. */
5671 gfc_current_ns->code = s.head;
5673 gfc_resolve (gfc_current_ns);
5675 /* Dump the parse tree if requested. */
5676 if (flag_dump_fortran_original)
5677 gfc_dump_parse_tree (gfc_current_ns, stdout);
5679 gfc_get_errors (NULL, &errors);
5680 if (s.state == COMP_MODULE || s.state == COMP_SUBMODULE)
5682 gfc_dump_module (s.sym->name, errors_before == errors);
5683 gfc_current_ns->derived_types = gfc_derived_types;
5684 gfc_derived_types = NULL;
5685 goto prog_units;
5687 else
5689 if (errors == 0)
5690 gfc_generate_code (gfc_current_ns);
5691 pop_state ();
5692 gfc_done_2 ();
5695 goto loop;
5697 prog_units:
5698 /* The main program and non-contained procedures are put
5699 in the global namespace list, so that they can be processed
5700 later and all their interfaces resolved. */
5701 gfc_current_ns->code = s.head;
5702 if (next)
5704 for (; next->sibling; next = next->sibling)
5706 next->sibling = gfc_current_ns;
5708 else
5709 gfc_global_ns_list = gfc_current_ns;
5711 next = gfc_current_ns;
5713 pop_state ();
5714 goto loop;
5716 done:
5718 /* Do the resolution. */
5719 resolve_all_program_units (gfc_global_ns_list);
5721 /* Do the parse tree dump. */
5722 gfc_current_ns
5723 = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
5725 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5726 if (!gfc_current_ns->proc_name
5727 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5729 gfc_dump_parse_tree (gfc_current_ns, stdout);
5730 fputs ("------------------------------------------\n\n", stdout);
5733 /* Do the translation. */
5734 translate_all_program_units (gfc_global_ns_list);
5736 gfc_end_source_files ();
5737 return true;
5739 duplicate_main:
5740 /* If we see a duplicate main program, shut down. If the second
5741 instance is an implied main program, i.e. data decls or executable
5742 statements, we're in for lots of errors. */
5743 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
5744 reject_statement ();
5745 gfc_done_2 ();
5746 return true;
5749 /* Return true if this state data represents an OpenACC region. */
5750 bool
5751 is_oacc (gfc_state_data *sd)
5753 switch (sd->construct->op)
5755 case EXEC_OACC_PARALLEL_LOOP:
5756 case EXEC_OACC_PARALLEL:
5757 case EXEC_OACC_KERNELS_LOOP:
5758 case EXEC_OACC_KERNELS:
5759 case EXEC_OACC_DATA:
5760 case EXEC_OACC_HOST_DATA:
5761 case EXEC_OACC_LOOP:
5762 case EXEC_OACC_UPDATE:
5763 case EXEC_OACC_WAIT:
5764 case EXEC_OACC_CACHE:
5765 case EXEC_OACC_ENTER_DATA:
5766 case EXEC_OACC_EXIT_DATA:
5767 return true;
5769 default:
5770 return false;