PR ipa/70283
[official-gcc.git] / gcc / fortran / parse.c
blob7bce47fef0ae0d7b134234424b52e68ff04ac0a2
1 /* Main parser.
2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "gfortran.h"
26 #include <setjmp.h>
27 #include "match.h"
28 #include "parse.h"
30 /* Current statement label. Zero means no statement label. Because new_st
31 can get wiped during statement matching, we have to keep it separate. */
33 gfc_st_label *gfc_statement_label;
35 static locus label_locus;
36 static jmp_buf eof_buf;
38 gfc_state_data *gfc_state_stack;
39 static bool last_was_use_stmt = false;
41 /* TODO: Re-order functions to kill these forward decls. */
42 static void check_statement_label (gfc_statement);
43 static void undo_new_statement (void);
44 static void reject_statement (void);
47 /* A sort of half-matching function. We try to match the word on the
48 input with the passed string. If this succeeds, we call the
49 keyword-dependent matching function that will match the rest of the
50 statement. For single keywords, the matching subroutine is
51 gfc_match_eos(). */
53 static match
54 match_word (const char *str, match (*subr) (void), locus *old_locus)
56 match m;
58 if (str != NULL)
60 m = gfc_match (str);
61 if (m != MATCH_YES)
62 return m;
65 m = (*subr) ();
67 if (m != MATCH_YES)
69 gfc_current_locus = *old_locus;
70 reject_statement ();
73 return m;
77 /* Like match_word, but if str is matched, set a flag that it
78 was matched. */
79 static match
80 match_word_omp_simd (const char *str, match (*subr) (void), locus *old_locus,
81 bool *simd_matched)
83 match m;
85 if (str != NULL)
87 m = gfc_match (str);
88 if (m != MATCH_YES)
89 return m;
90 *simd_matched = true;
93 m = (*subr) ();
95 if (m != MATCH_YES)
97 gfc_current_locus = *old_locus;
98 reject_statement ();
101 return m;
105 /* Load symbols from all USE statements encountered in this scoping unit. */
107 static void
108 use_modules (void)
110 gfc_error_buffer old_error;
112 gfc_push_error (&old_error);
113 gfc_buffer_error (false);
114 gfc_use_modules ();
115 gfc_buffer_error (true);
116 gfc_pop_error (&old_error);
117 gfc_commit_symbols ();
118 gfc_warning_check ();
119 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
120 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
121 gfc_current_ns->old_data = gfc_current_ns->data;
122 last_was_use_stmt = false;
126 /* Figure out what the next statement is, (mostly) regardless of
127 proper ordering. The do...while(0) is there to prevent if/else
128 ambiguity. */
130 #define match(keyword, subr, st) \
131 do { \
132 if (match_word (keyword, subr, &old_locus) == MATCH_YES) \
133 return st; \
134 else \
135 undo_new_statement (); \
136 } while (0);
139 /* This is a specialist version of decode_statement that is used
140 for the specification statements in a function, whose
141 characteristics are deferred into the specification statements.
142 eg.: INTEGER (king = mykind) foo ()
143 USE mymodule, ONLY mykind.....
144 The KIND parameter needs a return after USE or IMPORT, whereas
145 derived type declarations can occur anywhere, up the executable
146 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
147 out of the correct kind of specification statements. */
148 static gfc_statement
149 decode_specification_statement (void)
151 gfc_statement st;
152 locus old_locus;
153 char c;
155 if (gfc_match_eos () == MATCH_YES)
156 return ST_NONE;
158 old_locus = gfc_current_locus;
160 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
162 last_was_use_stmt = true;
163 return ST_USE;
165 else
167 undo_new_statement ();
168 if (last_was_use_stmt)
169 use_modules ();
172 match ("import", gfc_match_import, ST_IMPORT);
174 if (gfc_current_block ()->result->ts.type != BT_DERIVED)
175 goto end_of_block;
177 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
178 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
179 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
181 /* General statement matching: Instead of testing every possible
182 statement, we eliminate most possibilities by peeking at the
183 first character. */
185 c = gfc_peek_ascii_char ();
187 switch (c)
189 case 'a':
190 match ("abstract% interface", gfc_match_abstract_interface,
191 ST_INTERFACE);
192 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
193 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
194 break;
196 case 'b':
197 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
198 break;
200 case 'c':
201 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
202 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
203 break;
205 case 'd':
206 match ("data", gfc_match_data, ST_DATA);
207 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
208 break;
210 case 'e':
211 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
212 match ("entry% ", gfc_match_entry, ST_ENTRY);
213 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
214 match ("external", gfc_match_external, ST_ATTR_DECL);
215 break;
217 case 'f':
218 match ("format", gfc_match_format, ST_FORMAT);
219 break;
221 case 'g':
222 break;
224 case 'i':
225 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
226 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
227 match ("interface", gfc_match_interface, ST_INTERFACE);
228 match ("intent", gfc_match_intent, ST_ATTR_DECL);
229 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
230 break;
232 case 'm':
233 break;
235 case 'n':
236 match ("namelist", gfc_match_namelist, ST_NAMELIST);
237 break;
239 case 'o':
240 match ("optional", gfc_match_optional, ST_ATTR_DECL);
241 break;
243 case 'p':
244 match ("parameter", gfc_match_parameter, ST_PARAMETER);
245 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
246 if (gfc_match_private (&st) == MATCH_YES)
247 return st;
248 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
249 if (gfc_match_public (&st) == MATCH_YES)
250 return st;
251 match ("protected", gfc_match_protected, ST_ATTR_DECL);
252 break;
254 case 'r':
255 break;
257 case 's':
258 match ("save", gfc_match_save, ST_ATTR_DECL);
259 break;
261 case 't':
262 match ("target", gfc_match_target, ST_ATTR_DECL);
263 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
264 break;
266 case 'u':
267 break;
269 case 'v':
270 match ("value", gfc_match_value, ST_ATTR_DECL);
271 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
272 break;
274 case 'w':
275 break;
278 /* This is not a specification statement. See if any of the matchers
279 has stored an error message of some sort. */
281 end_of_block:
282 gfc_clear_error ();
283 gfc_buffer_error (false);
284 gfc_current_locus = old_locus;
286 return ST_GET_FCN_CHARACTERISTICS;
289 static bool in_specification_block;
291 /* This is the primary 'decode_statement'. */
292 static gfc_statement
293 decode_statement (void)
295 gfc_namespace *ns;
296 gfc_statement st;
297 locus old_locus;
298 match m = MATCH_NO;
299 char c;
301 gfc_enforce_clean_symbol_state ();
303 gfc_clear_error (); /* Clear any pending errors. */
304 gfc_clear_warning (); /* Clear any pending warnings. */
306 gfc_matching_function = false;
308 if (gfc_match_eos () == MATCH_YES)
309 return ST_NONE;
311 if (gfc_current_state () == COMP_FUNCTION
312 && gfc_current_block ()->result->ts.kind == -1)
313 return decode_specification_statement ();
315 old_locus = gfc_current_locus;
317 c = gfc_peek_ascii_char ();
319 if (c == 'u')
321 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
323 last_was_use_stmt = true;
324 return ST_USE;
326 else
327 undo_new_statement ();
330 if (last_was_use_stmt)
331 use_modules ();
333 /* Try matching a data declaration or function declaration. The
334 input "REALFUNCTIONA(N)" can mean several things in different
335 contexts, so it (and its relatives) get special treatment. */
337 if (gfc_current_state () == COMP_NONE
338 || gfc_current_state () == COMP_INTERFACE
339 || gfc_current_state () == COMP_CONTAINS)
341 gfc_matching_function = true;
342 m = gfc_match_function_decl ();
343 if (m == MATCH_YES)
344 return ST_FUNCTION;
345 else if (m == MATCH_ERROR)
346 reject_statement ();
347 else
348 gfc_undo_symbols ();
349 gfc_current_locus = old_locus;
351 gfc_matching_function = false;
354 /* Match statements whose error messages are meant to be overwritten
355 by something better. */
357 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
358 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
360 if (in_specification_block)
362 m = match_word (NULL, gfc_match_st_function, &old_locus);
363 if (m == MATCH_YES)
364 return ST_STATEMENT_FUNCTION;
367 if (!(in_specification_block && m == MATCH_ERROR))
369 match (NULL, gfc_match_ptr_fcn_assign, ST_ASSIGNMENT);
372 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
373 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
375 /* Try to match a subroutine statement, which has the same optional
376 prefixes that functions can have. */
378 if (gfc_match_subroutine () == MATCH_YES)
379 return ST_SUBROUTINE;
380 gfc_undo_symbols ();
381 gfc_current_locus = old_locus;
383 if (gfc_match_submod_proc () == MATCH_YES)
385 if (gfc_new_block->attr.subroutine)
386 return ST_SUBROUTINE;
387 else if (gfc_new_block->attr.function)
388 return ST_FUNCTION;
390 gfc_undo_symbols ();
391 gfc_current_locus = old_locus;
393 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
394 statements, which might begin with a block label. The match functions for
395 these statements are unusual in that their keyword is not seen before
396 the matcher is called. */
398 if (gfc_match_if (&st) == MATCH_YES)
399 return st;
400 gfc_undo_symbols ();
401 gfc_current_locus = old_locus;
403 if (gfc_match_where (&st) == MATCH_YES)
404 return st;
405 gfc_undo_symbols ();
406 gfc_current_locus = old_locus;
408 if (gfc_match_forall (&st) == MATCH_YES)
409 return st;
410 gfc_undo_symbols ();
411 gfc_current_locus = old_locus;
413 match (NULL, gfc_match_do, ST_DO);
414 match (NULL, gfc_match_block, ST_BLOCK);
415 match (NULL, gfc_match_associate, ST_ASSOCIATE);
416 match (NULL, gfc_match_critical, ST_CRITICAL);
417 match (NULL, gfc_match_select, ST_SELECT_CASE);
419 gfc_current_ns = gfc_build_block_ns (gfc_current_ns);
420 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
421 ns = gfc_current_ns;
422 gfc_current_ns = gfc_current_ns->parent;
423 gfc_free_namespace (ns);
425 /* General statement matching: Instead of testing every possible
426 statement, we eliminate most possibilities by peeking at the
427 first character. */
429 switch (c)
431 case 'a':
432 match ("abstract% interface", gfc_match_abstract_interface,
433 ST_INTERFACE);
434 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
435 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
436 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
437 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
438 break;
440 case 'b':
441 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
442 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
443 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
444 break;
446 case 'c':
447 match ("call", gfc_match_call, ST_CALL);
448 match ("close", gfc_match_close, ST_CLOSE);
449 match ("continue", gfc_match_continue, ST_CONTINUE);
450 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
451 match ("cycle", gfc_match_cycle, ST_CYCLE);
452 match ("case", gfc_match_case, ST_CASE);
453 match ("common", gfc_match_common, ST_COMMON);
454 match ("contains", gfc_match_eos, ST_CONTAINS);
455 match ("class", gfc_match_class_is, ST_CLASS_IS);
456 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
457 break;
459 case 'd':
460 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
461 match ("data", gfc_match_data, ST_DATA);
462 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
463 break;
465 case 'e':
466 match ("end file", gfc_match_endfile, ST_END_FILE);
467 match ("exit", gfc_match_exit, ST_EXIT);
468 match ("else", gfc_match_else, ST_ELSE);
469 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
470 match ("else if", gfc_match_elseif, ST_ELSEIF);
471 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
472 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
474 if (gfc_match_end (&st) == MATCH_YES)
475 return st;
477 match ("entry% ", gfc_match_entry, ST_ENTRY);
478 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
479 match ("external", gfc_match_external, ST_ATTR_DECL);
480 match ("event post", gfc_match_event_post, ST_EVENT_POST);
481 match ("event wait", gfc_match_event_wait, ST_EVENT_WAIT);
482 break;
484 case 'f':
485 match ("final", gfc_match_final_decl, ST_FINAL);
486 match ("flush", gfc_match_flush, ST_FLUSH);
487 match ("format", gfc_match_format, ST_FORMAT);
488 break;
490 case 'g':
491 match ("generic", gfc_match_generic, ST_GENERIC);
492 match ("go to", gfc_match_goto, ST_GOTO);
493 break;
495 case 'i':
496 match ("inquire", gfc_match_inquire, ST_INQUIRE);
497 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
498 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
499 match ("import", gfc_match_import, ST_IMPORT);
500 match ("interface", gfc_match_interface, ST_INTERFACE);
501 match ("intent", gfc_match_intent, ST_ATTR_DECL);
502 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
503 break;
505 case 'l':
506 match ("lock", gfc_match_lock, ST_LOCK);
507 break;
509 case 'm':
510 match ("module% procedure", gfc_match_modproc, ST_MODULE_PROC);
511 match ("module", gfc_match_module, ST_MODULE);
512 break;
514 case 'n':
515 match ("nullify", gfc_match_nullify, ST_NULLIFY);
516 match ("namelist", gfc_match_namelist, ST_NAMELIST);
517 break;
519 case 'o':
520 match ("open", gfc_match_open, ST_OPEN);
521 match ("optional", gfc_match_optional, ST_ATTR_DECL);
522 break;
524 case 'p':
525 match ("print", gfc_match_print, ST_WRITE);
526 match ("parameter", gfc_match_parameter, ST_PARAMETER);
527 match ("pause", gfc_match_pause, ST_PAUSE);
528 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
529 if (gfc_match_private (&st) == MATCH_YES)
530 return st;
531 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
532 match ("program", gfc_match_program, ST_PROGRAM);
533 if (gfc_match_public (&st) == MATCH_YES)
534 return st;
535 match ("protected", gfc_match_protected, ST_ATTR_DECL);
536 break;
538 case 'r':
539 match ("read", gfc_match_read, ST_READ);
540 match ("return", gfc_match_return, ST_RETURN);
541 match ("rewind", gfc_match_rewind, ST_REWIND);
542 break;
544 case 's':
545 match ("sequence", gfc_match_eos, ST_SEQUENCE);
546 match ("stop", gfc_match_stop, ST_STOP);
547 match ("save", gfc_match_save, ST_ATTR_DECL);
548 match ("submodule", gfc_match_submodule, ST_SUBMODULE);
549 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
550 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
551 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
552 break;
554 case 't':
555 match ("target", gfc_match_target, ST_ATTR_DECL);
556 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
557 match ("type is", gfc_match_type_is, ST_TYPE_IS);
558 break;
560 case 'u':
561 match ("unlock", gfc_match_unlock, ST_UNLOCK);
562 break;
564 case 'v':
565 match ("value", gfc_match_value, ST_ATTR_DECL);
566 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
567 break;
569 case 'w':
570 match ("wait", gfc_match_wait, ST_WAIT);
571 match ("write", gfc_match_write, ST_WRITE);
572 break;
575 /* All else has failed, so give up. See if any of the matchers has
576 stored an error message of some sort. */
578 if (!gfc_error_check ())
579 gfc_error_now ("Unclassifiable statement at %C");
581 reject_statement ();
583 gfc_error_recovery ();
585 return ST_NONE;
588 /* Like match, but set a flag simd_matched if keyword matched. */
589 #define matchs(keyword, subr, st) \
590 do { \
591 if (match_word_omp_simd (keyword, subr, &old_locus, \
592 &simd_matched) == MATCH_YES) \
593 return st; \
594 else \
595 undo_new_statement (); \
596 } while (0);
598 /* Like match, but don't match anything if not -fopenmp. */
599 #define matcho(keyword, subr, st) \
600 do { \
601 if (!flag_openmp) \
603 else if (match_word (keyword, subr, &old_locus) \
604 == MATCH_YES) \
605 return st; \
606 else \
607 undo_new_statement (); \
608 } while (0);
610 static gfc_statement
611 decode_oacc_directive (void)
613 locus old_locus;
614 char c;
616 gfc_enforce_clean_symbol_state ();
618 gfc_clear_error (); /* Clear any pending errors. */
619 gfc_clear_warning (); /* Clear any pending warnings. */
621 if (gfc_pure (NULL))
623 gfc_error_now ("OpenACC directives at %C may not appear in PURE "
624 "procedures");
625 gfc_error_recovery ();
626 return ST_NONE;
629 gfc_unset_implicit_pure (NULL);
631 old_locus = gfc_current_locus;
633 /* General OpenACC directive matching: Instead of testing every possible
634 statement, we eliminate most possibilities by peeking at the
635 first character. */
637 c = gfc_peek_ascii_char ();
639 switch (c)
641 case 'a':
642 match ("atomic", gfc_match_oacc_atomic, ST_OACC_ATOMIC);
643 break;
644 case 'c':
645 match ("cache", gfc_match_oacc_cache, ST_OACC_CACHE);
646 break;
647 case 'd':
648 match ("data", gfc_match_oacc_data, ST_OACC_DATA);
649 match ("declare", gfc_match_oacc_declare, ST_OACC_DECLARE);
650 break;
651 case 'e':
652 match ("end atomic", gfc_match_omp_eos, ST_OACC_END_ATOMIC);
653 match ("end data", gfc_match_omp_eos, ST_OACC_END_DATA);
654 match ("end host_data", gfc_match_omp_eos, ST_OACC_END_HOST_DATA);
655 match ("end kernels loop", gfc_match_omp_eos, ST_OACC_END_KERNELS_LOOP);
656 match ("end kernels", gfc_match_omp_eos, ST_OACC_END_KERNELS);
657 match ("end loop", gfc_match_omp_eos, ST_OACC_END_LOOP);
658 match ("end parallel loop", gfc_match_omp_eos, ST_OACC_END_PARALLEL_LOOP);
659 match ("end parallel", gfc_match_omp_eos, ST_OACC_END_PARALLEL);
660 match ("enter data", gfc_match_oacc_enter_data, ST_OACC_ENTER_DATA);
661 match ("exit data", gfc_match_oacc_exit_data, ST_OACC_EXIT_DATA);
662 break;
663 case 'h':
664 match ("host_data", gfc_match_oacc_host_data, ST_OACC_HOST_DATA);
665 break;
666 case 'p':
667 match ("parallel loop", gfc_match_oacc_parallel_loop, ST_OACC_PARALLEL_LOOP);
668 match ("parallel", gfc_match_oacc_parallel, ST_OACC_PARALLEL);
669 break;
670 case 'k':
671 match ("kernels loop", gfc_match_oacc_kernels_loop, ST_OACC_KERNELS_LOOP);
672 match ("kernels", gfc_match_oacc_kernels, ST_OACC_KERNELS);
673 break;
674 case 'l':
675 match ("loop", gfc_match_oacc_loop, ST_OACC_LOOP);
676 break;
677 case 'r':
678 match ("routine", gfc_match_oacc_routine, ST_OACC_ROUTINE);
679 break;
680 case 'u':
681 match ("update", gfc_match_oacc_update, ST_OACC_UPDATE);
682 break;
683 case 'w':
684 match ("wait", gfc_match_oacc_wait, ST_OACC_WAIT);
685 break;
688 /* Directive not found or stored an error message.
689 Check and give up. */
691 if (gfc_error_check () == 0)
692 gfc_error_now ("Unclassifiable OpenACC directive at %C");
694 reject_statement ();
696 gfc_error_recovery ();
698 return ST_NONE;
701 static gfc_statement
702 decode_omp_directive (void)
704 locus old_locus;
705 char c;
706 bool simd_matched = false;
708 gfc_enforce_clean_symbol_state ();
710 gfc_clear_error (); /* Clear any pending errors. */
711 gfc_clear_warning (); /* Clear any pending warnings. */
713 if (gfc_pure (NULL))
715 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
716 "or ELEMENTAL procedures");
717 gfc_error_recovery ();
718 return ST_NONE;
721 gfc_unset_implicit_pure (NULL);
723 old_locus = gfc_current_locus;
725 /* General OpenMP directive matching: Instead of testing every possible
726 statement, we eliminate most possibilities by peeking at the
727 first character. */
729 c = gfc_peek_ascii_char ();
731 /* match is for directives that should be recognized only if
732 -fopenmp, matchs for directives that should be recognized
733 if either -fopenmp or -fopenmp-simd. */
734 switch (c)
736 case 'a':
737 matcho ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
738 break;
739 case 'b':
740 matcho ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
741 break;
742 case 'c':
743 matcho ("cancellation% point", gfc_match_omp_cancellation_point,
744 ST_OMP_CANCELLATION_POINT);
745 matcho ("cancel", gfc_match_omp_cancel, ST_OMP_CANCEL);
746 matcho ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
747 break;
748 case 'd':
749 matchs ("declare reduction", gfc_match_omp_declare_reduction,
750 ST_OMP_DECLARE_REDUCTION);
751 matchs ("declare simd", gfc_match_omp_declare_simd,
752 ST_OMP_DECLARE_SIMD);
753 matcho ("declare target", gfc_match_omp_declare_target,
754 ST_OMP_DECLARE_TARGET);
755 matchs ("distribute parallel do simd",
756 gfc_match_omp_distribute_parallel_do_simd,
757 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD);
758 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do,
759 ST_OMP_DISTRIBUTE_PARALLEL_DO);
760 matchs ("distribute simd", gfc_match_omp_distribute_simd,
761 ST_OMP_DISTRIBUTE_SIMD);
762 matcho ("distribute", gfc_match_omp_distribute, ST_OMP_DISTRIBUTE);
763 matchs ("do simd", gfc_match_omp_do_simd, ST_OMP_DO_SIMD);
764 matcho ("do", gfc_match_omp_do, ST_OMP_DO);
765 break;
766 case 'e':
767 matcho ("end atomic", gfc_match_omp_eos, ST_OMP_END_ATOMIC);
768 matcho ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
769 matchs ("end distribute parallel do simd", gfc_match_omp_eos,
770 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD);
771 matcho ("end distribute parallel do", gfc_match_omp_eos,
772 ST_OMP_END_DISTRIBUTE_PARALLEL_DO);
773 matchs ("end distribute simd", gfc_match_omp_eos,
774 ST_OMP_END_DISTRIBUTE_SIMD);
775 matcho ("end distribute", gfc_match_omp_eos, ST_OMP_END_DISTRIBUTE);
776 matchs ("end do simd", gfc_match_omp_end_nowait, ST_OMP_END_DO_SIMD);
777 matcho ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
778 matchs ("end simd", gfc_match_omp_eos, ST_OMP_END_SIMD);
779 matcho ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
780 matcho ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
781 matchs ("end parallel do simd", gfc_match_omp_eos,
782 ST_OMP_END_PARALLEL_DO_SIMD);
783 matcho ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
784 matcho ("end parallel sections", gfc_match_omp_eos,
785 ST_OMP_END_PARALLEL_SECTIONS);
786 matcho ("end parallel workshare", gfc_match_omp_eos,
787 ST_OMP_END_PARALLEL_WORKSHARE);
788 matcho ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
789 matcho ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
790 matcho ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
791 matcho ("end target data", gfc_match_omp_eos, ST_OMP_END_TARGET_DATA);
792 matchs ("end target teams distribute parallel do simd",
793 gfc_match_omp_eos,
794 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
795 matcho ("end target teams distribute parallel do", gfc_match_omp_eos,
796 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
797 matchs ("end target teams distribute simd", gfc_match_omp_eos,
798 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD);
799 matcho ("end target teams distribute", gfc_match_omp_eos,
800 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE);
801 matcho ("end target teams", gfc_match_omp_eos, ST_OMP_END_TARGET_TEAMS);
802 matcho ("end target", gfc_match_omp_eos, ST_OMP_END_TARGET);
803 matcho ("end taskgroup", gfc_match_omp_eos, ST_OMP_END_TASKGROUP);
804 matcho ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
805 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos,
806 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
807 matcho ("end teams distribute parallel do", gfc_match_omp_eos,
808 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO);
809 matchs ("end teams distribute simd", gfc_match_omp_eos,
810 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD);
811 matcho ("end teams distribute", gfc_match_omp_eos,
812 ST_OMP_END_TEAMS_DISTRIBUTE);
813 matcho ("end teams", gfc_match_omp_eos, ST_OMP_END_TEAMS);
814 matcho ("end workshare", gfc_match_omp_end_nowait,
815 ST_OMP_END_WORKSHARE);
816 break;
817 case 'f':
818 matcho ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
819 break;
820 case 'm':
821 matcho ("master", gfc_match_omp_master, ST_OMP_MASTER);
822 break;
823 case 'o':
824 matcho ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
825 break;
826 case 'p':
827 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
828 ST_OMP_PARALLEL_DO_SIMD);
829 matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
830 matcho ("parallel sections", gfc_match_omp_parallel_sections,
831 ST_OMP_PARALLEL_SECTIONS);
832 matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
833 ST_OMP_PARALLEL_WORKSHARE);
834 matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
835 break;
836 case 's':
837 matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
838 matcho ("section", gfc_match_omp_eos, ST_OMP_SECTION);
839 matchs ("simd", gfc_match_omp_simd, ST_OMP_SIMD);
840 matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
841 break;
842 case 't':
843 matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
844 matchs ("target teams distribute parallel do simd",
845 gfc_match_omp_target_teams_distribute_parallel_do_simd,
846 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
847 matcho ("target teams distribute parallel do",
848 gfc_match_omp_target_teams_distribute_parallel_do,
849 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
850 matchs ("target teams distribute simd",
851 gfc_match_omp_target_teams_distribute_simd,
852 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
853 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
854 ST_OMP_TARGET_TEAMS_DISTRIBUTE);
855 matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
856 matcho ("target update", gfc_match_omp_target_update,
857 ST_OMP_TARGET_UPDATE);
858 matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
859 matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
860 matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
861 matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
862 matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
863 matchs ("teams distribute parallel do simd",
864 gfc_match_omp_teams_distribute_parallel_do_simd,
865 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
866 matcho ("teams distribute parallel do",
867 gfc_match_omp_teams_distribute_parallel_do,
868 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
869 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
870 ST_OMP_TEAMS_DISTRIBUTE_SIMD);
871 matcho ("teams distribute", gfc_match_omp_teams_distribute,
872 ST_OMP_TEAMS_DISTRIBUTE);
873 matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
874 matcho ("threadprivate", gfc_match_omp_threadprivate,
875 ST_OMP_THREADPRIVATE);
876 break;
877 case 'w':
878 matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
879 break;
882 /* All else has failed, so give up. See if any of the matchers has
883 stored an error message of some sort. Don't error out if
884 not -fopenmp and simd_matched is false, i.e. if a directive other
885 than one marked with match has been seen. */
887 if (flag_openmp || simd_matched)
889 if (!gfc_error_check ())
890 gfc_error_now ("Unclassifiable OpenMP directive at %C");
893 reject_statement ();
895 gfc_error_recovery ();
897 return ST_NONE;
900 static gfc_statement
901 decode_gcc_attribute (void)
903 locus old_locus;
905 gfc_enforce_clean_symbol_state ();
907 gfc_clear_error (); /* Clear any pending errors. */
908 gfc_clear_warning (); /* Clear any pending warnings. */
909 old_locus = gfc_current_locus;
911 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
913 /* All else has failed, so give up. See if any of the matchers has
914 stored an error message of some sort. */
916 if (!gfc_error_check ())
917 gfc_error_now ("Unclassifiable GCC directive at %C");
919 reject_statement ();
921 gfc_error_recovery ();
923 return ST_NONE;
926 #undef match
928 /* Assert next length characters to be equal to token in free form. */
930 static void
931 verify_token_free (const char* token, int length, bool last_was_use_stmt)
933 int i;
934 char c;
936 c = gfc_next_ascii_char ();
937 for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
938 gcc_assert (c == token[i]);
940 gcc_assert (gfc_is_whitespace(c));
941 gfc_gobble_whitespace ();
942 if (last_was_use_stmt)
943 use_modules ();
946 /* Get the next statement in free form source. */
948 static gfc_statement
949 next_free (void)
951 match m;
952 int i, cnt, at_bol;
953 char c;
955 at_bol = gfc_at_bol ();
956 gfc_gobble_whitespace ();
958 c = gfc_peek_ascii_char ();
960 if (ISDIGIT (c))
962 char d;
964 /* Found a statement label? */
965 m = gfc_match_st_label (&gfc_statement_label);
967 d = gfc_peek_ascii_char ();
968 if (m != MATCH_YES || !gfc_is_whitespace (d))
970 gfc_match_small_literal_int (&i, &cnt);
972 if (cnt > 5)
973 gfc_error_now ("Too many digits in statement label at %C");
975 if (i == 0)
976 gfc_error_now ("Zero is not a valid statement label at %C");
979 c = gfc_next_ascii_char ();
980 while (ISDIGIT(c));
982 if (!gfc_is_whitespace (c))
983 gfc_error_now ("Non-numeric character in statement label at %C");
985 return ST_NONE;
987 else
989 label_locus = gfc_current_locus;
991 gfc_gobble_whitespace ();
993 if (at_bol && gfc_peek_ascii_char () == ';')
995 gfc_error_now ("Semicolon at %C needs to be preceded by "
996 "statement");
997 gfc_next_ascii_char (); /* Eat up the semicolon. */
998 return ST_NONE;
1001 if (gfc_match_eos () == MATCH_YES)
1003 gfc_warning_now (0, "Ignoring statement label in empty statement "
1004 "at %L", &label_locus);
1005 gfc_free_st_label (gfc_statement_label);
1006 gfc_statement_label = NULL;
1007 return ST_NONE;
1011 else if (c == '!')
1013 /* Comments have already been skipped by the time we get here,
1014 except for GCC attributes and OpenMP/OpenACC directives. */
1016 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
1017 c = gfc_peek_ascii_char ();
1019 if (c == 'g')
1021 int i;
1023 c = gfc_next_ascii_char ();
1024 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
1025 gcc_assert (c == "gcc$"[i]);
1027 gfc_gobble_whitespace ();
1028 return decode_gcc_attribute ();
1031 else if (c == '$')
1033 /* Since both OpenMP and OpenACC directives starts with
1034 !$ character sequence, we must check all flags combinations */
1035 if ((flag_openmp || flag_openmp_simd)
1036 && !flag_openacc)
1038 verify_token_free ("$omp", 4, last_was_use_stmt);
1039 return decode_omp_directive ();
1041 else if ((flag_openmp || flag_openmp_simd)
1042 && flag_openacc)
1044 gfc_next_ascii_char (); /* Eat up dollar character */
1045 c = gfc_peek_ascii_char ();
1047 if (c == 'o')
1049 verify_token_free ("omp", 3, last_was_use_stmt);
1050 return decode_omp_directive ();
1052 else if (c == 'a')
1054 verify_token_free ("acc", 3, last_was_use_stmt);
1055 return decode_oacc_directive ();
1058 else if (flag_openacc)
1060 verify_token_free ("$acc", 4, last_was_use_stmt);
1061 return decode_oacc_directive ();
1064 gcc_unreachable ();
1067 if (at_bol && c == ';')
1069 if (!(gfc_option.allow_std & GFC_STD_F2008))
1070 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1071 "statement");
1072 gfc_next_ascii_char (); /* Eat up the semicolon. */
1073 return ST_NONE;
1076 return decode_statement ();
1079 /* Assert next length characters to be equal to token in fixed form. */
1081 static bool
1082 verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
1084 int i;
1085 char c = gfc_next_char_literal (NONSTRING);
1087 for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
1088 gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
1090 if (c != ' ' && c != '0')
1092 gfc_buffer_error (false);
1093 gfc_error ("Bad continuation line at %C");
1094 return false;
1096 if (last_was_use_stmt)
1097 use_modules ();
1099 return true;
1102 /* Get the next statement in fixed-form source. */
1104 static gfc_statement
1105 next_fixed (void)
1107 int label, digit_flag, i;
1108 locus loc;
1109 gfc_char_t c;
1111 if (!gfc_at_bol ())
1112 return decode_statement ();
1114 /* Skip past the current label field, parsing a statement label if
1115 one is there. This is a weird number parser, since the number is
1116 contained within five columns and can have any kind of embedded
1117 spaces. We also check for characters that make the rest of the
1118 line a comment. */
1120 label = 0;
1121 digit_flag = 0;
1123 for (i = 0; i < 5; i++)
1125 c = gfc_next_char_literal (NONSTRING);
1127 switch (c)
1129 case ' ':
1130 break;
1132 case '0':
1133 case '1':
1134 case '2':
1135 case '3':
1136 case '4':
1137 case '5':
1138 case '6':
1139 case '7':
1140 case '8':
1141 case '9':
1142 label = label * 10 + ((unsigned char) c - '0');
1143 label_locus = gfc_current_locus;
1144 digit_flag = 1;
1145 break;
1147 /* Comments have already been skipped by the time we get
1148 here, except for GCC attributes and OpenMP directives. */
1150 case '*':
1151 c = gfc_next_char_literal (NONSTRING);
1153 if (TOLOWER (c) == 'g')
1155 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
1156 gcc_assert (TOLOWER (c) == "gcc$"[i]);
1158 return decode_gcc_attribute ();
1160 else if (c == '$')
1162 if ((flag_openmp || flag_openmp_simd)
1163 && !flag_openacc)
1165 if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
1166 return ST_NONE;
1167 return decode_omp_directive ();
1169 else if ((flag_openmp || flag_openmp_simd)
1170 && flag_openacc)
1172 c = gfc_next_char_literal(NONSTRING);
1173 if (c == 'o' || c == 'O')
1175 if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
1176 return ST_NONE;
1177 return decode_omp_directive ();
1179 else if (c == 'a' || c == 'A')
1181 if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
1182 return ST_NONE;
1183 return decode_oacc_directive ();
1186 else if (flag_openacc)
1188 if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
1189 return ST_NONE;
1190 return decode_oacc_directive ();
1193 /* FALLTHROUGH */
1195 /* Comments have already been skipped by the time we get
1196 here so don't bother checking for them. */
1198 default:
1199 gfc_buffer_error (false);
1200 gfc_error ("Non-numeric character in statement label at %C");
1201 return ST_NONE;
1205 if (digit_flag)
1207 if (label == 0)
1208 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1209 else
1211 /* We've found a valid statement label. */
1212 gfc_statement_label = gfc_get_st_label (label);
1216 /* Since this line starts a statement, it cannot be a continuation
1217 of a previous statement. If we see something here besides a
1218 space or zero, it must be a bad continuation line. */
1220 c = gfc_next_char_literal (NONSTRING);
1221 if (c == '\n')
1222 goto blank_line;
1224 if (c != ' ' && c != '0')
1226 gfc_buffer_error (false);
1227 gfc_error ("Bad continuation line at %C");
1228 return ST_NONE;
1231 /* Now that we've taken care of the statement label columns, we have
1232 to make sure that the first nonblank character is not a '!'. If
1233 it is, the rest of the line is a comment. */
1237 loc = gfc_current_locus;
1238 c = gfc_next_char_literal (NONSTRING);
1240 while (gfc_is_whitespace (c));
1242 if (c == '!')
1243 goto blank_line;
1244 gfc_current_locus = loc;
1246 if (c == ';')
1248 if (digit_flag)
1249 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1250 else if (!(gfc_option.allow_std & GFC_STD_F2008))
1251 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1252 "statement");
1253 return ST_NONE;
1256 if (gfc_match_eos () == MATCH_YES)
1257 goto blank_line;
1259 /* At this point, we've got a nonblank statement to parse. */
1260 return decode_statement ();
1262 blank_line:
1263 if (digit_flag)
1264 gfc_warning_now (0, "Ignoring statement label in empty statement at %L",
1265 &label_locus);
1267 gfc_current_locus.lb->truncated = 0;
1268 gfc_advance_line ();
1269 return ST_NONE;
1273 /* Return the next non-ST_NONE statement to the caller. We also worry
1274 about including files and the ends of include files at this stage. */
1276 static gfc_statement
1277 next_statement (void)
1279 gfc_statement st;
1280 locus old_locus;
1282 gfc_enforce_clean_symbol_state ();
1284 gfc_new_block = NULL;
1286 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
1287 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
1288 gfc_current_ns->old_data = gfc_current_ns->data;
1289 for (;;)
1291 gfc_statement_label = NULL;
1292 gfc_buffer_error (true);
1294 if (gfc_at_eol ())
1295 gfc_advance_line ();
1297 gfc_skip_comments ();
1299 if (gfc_at_end ())
1301 st = ST_NONE;
1302 break;
1305 if (gfc_define_undef_line ())
1306 continue;
1308 old_locus = gfc_current_locus;
1310 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
1312 if (st != ST_NONE)
1313 break;
1316 gfc_buffer_error (false);
1318 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
1320 gfc_free_st_label (gfc_statement_label);
1321 gfc_statement_label = NULL;
1322 gfc_current_locus = old_locus;
1325 if (st != ST_NONE)
1326 check_statement_label (st);
1328 return st;
1332 /****************************** Parser ***********************************/
1334 /* The parser subroutines are of type 'try' that fail if the file ends
1335 unexpectedly. */
1337 /* Macros that expand to case-labels for various classes of
1338 statements. Start with executable statements that directly do
1339 things. */
1341 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1342 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1343 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1344 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1345 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1346 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1347 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1348 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1349 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1350 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1351 case ST_OMP_TARGET_UPDATE: case ST_ERROR_STOP: case ST_SYNC_ALL: \
1352 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1353 case ST_EVENT_POST: case ST_EVENT_WAIT: \
1354 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1355 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1357 /* Statements that mark other executable statements. */
1359 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1360 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1361 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1362 case ST_OMP_PARALLEL: \
1363 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1364 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1365 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1366 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1367 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1368 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1369 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1370 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1371 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1372 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1373 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1374 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1375 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1376 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1377 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1378 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1379 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: \
1380 case ST_CRITICAL: \
1381 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1382 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: \
1383 case ST_OACC_KERNELS_LOOP: case ST_OACC_ATOMIC
1385 /* Declaration statements */
1387 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1388 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1389 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
1390 case ST_PROCEDURE: case ST_OMP_DECLARE_SIMD: case ST_OMP_DECLARE_REDUCTION: \
1391 case ST_OMP_DECLARE_TARGET: case ST_OACC_ROUTINE: case ST_OACC_DECLARE
1393 /* Block end statements. Errors associated with interchanging these
1394 are detected in gfc_match_end(). */
1396 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1397 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1398 case ST_END_BLOCK: case ST_END_ASSOCIATE
1401 /* Push a new state onto the stack. */
1403 static void
1404 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
1406 p->state = new_state;
1407 p->previous = gfc_state_stack;
1408 p->sym = sym;
1409 p->head = p->tail = NULL;
1410 p->do_variable = NULL;
1411 if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
1412 p->ext.oacc_declare_clauses = NULL;
1414 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1415 construct statement was accepted right before pushing the state. Thus,
1416 the construct's gfc_code is available as tail of the parent state. */
1417 gcc_assert (gfc_state_stack);
1418 p->construct = gfc_state_stack->tail;
1420 gfc_state_stack = p;
1424 /* Pop the current state. */
1425 static void
1426 pop_state (void)
1428 gfc_state_stack = gfc_state_stack->previous;
1432 /* Try to find the given state in the state stack. */
1434 bool
1435 gfc_find_state (gfc_compile_state state)
1437 gfc_state_data *p;
1439 for (p = gfc_state_stack; p; p = p->previous)
1440 if (p->state == state)
1441 break;
1443 return (p == NULL) ? false : true;
1447 /* Starts a new level in the statement list. */
1449 static gfc_code *
1450 new_level (gfc_code *q)
1452 gfc_code *p;
1454 p = q->block = gfc_get_code (EXEC_NOP);
1456 gfc_state_stack->head = gfc_state_stack->tail = p;
1458 return p;
1462 /* Add the current new_st code structure and adds it to the current
1463 program unit. As a side-effect, it zeroes the new_st. */
1465 static gfc_code *
1466 add_statement (void)
1468 gfc_code *p;
1470 p = XCNEW (gfc_code);
1471 *p = new_st;
1473 p->loc = gfc_current_locus;
1475 if (gfc_state_stack->head == NULL)
1476 gfc_state_stack->head = p;
1477 else
1478 gfc_state_stack->tail->next = p;
1480 while (p->next != NULL)
1481 p = p->next;
1483 gfc_state_stack->tail = p;
1485 gfc_clear_new_st ();
1487 return p;
1491 /* Frees everything associated with the current statement. */
1493 static void
1494 undo_new_statement (void)
1496 gfc_free_statements (new_st.block);
1497 gfc_free_statements (new_st.next);
1498 gfc_free_statement (&new_st);
1499 gfc_clear_new_st ();
1503 /* If the current statement has a statement label, make sure that it
1504 is allowed to, or should have one. */
1506 static void
1507 check_statement_label (gfc_statement st)
1509 gfc_sl_type type;
1511 if (gfc_statement_label == NULL)
1513 if (st == ST_FORMAT)
1514 gfc_error ("FORMAT statement at %L does not have a statement label",
1515 &new_st.loc);
1516 return;
1519 switch (st)
1521 case ST_END_PROGRAM:
1522 case ST_END_FUNCTION:
1523 case ST_END_SUBROUTINE:
1524 case ST_ENDDO:
1525 case ST_ENDIF:
1526 case ST_END_SELECT:
1527 case ST_END_CRITICAL:
1528 case ST_END_BLOCK:
1529 case ST_END_ASSOCIATE:
1530 case_executable:
1531 case_exec_markers:
1532 if (st == ST_ENDDO || st == ST_CONTINUE)
1533 type = ST_LABEL_DO_TARGET;
1534 else
1535 type = ST_LABEL_TARGET;
1536 break;
1538 case ST_FORMAT:
1539 type = ST_LABEL_FORMAT;
1540 break;
1542 /* Statement labels are not restricted from appearing on a
1543 particular line. However, there are plenty of situations
1544 where the resulting label can't be referenced. */
1546 default:
1547 type = ST_LABEL_BAD_TARGET;
1548 break;
1551 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1553 new_st.here = gfc_statement_label;
1557 /* Figures out what the enclosing program unit is. This will be a
1558 function, subroutine, program, block data or module. */
1560 gfc_state_data *
1561 gfc_enclosing_unit (gfc_compile_state * result)
1563 gfc_state_data *p;
1565 for (p = gfc_state_stack; p; p = p->previous)
1566 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1567 || p->state == COMP_MODULE || p->state == COMP_SUBMODULE
1568 || p->state == COMP_BLOCK_DATA || p->state == COMP_PROGRAM)
1571 if (result != NULL)
1572 *result = p->state;
1573 return p;
1576 if (result != NULL)
1577 *result = COMP_PROGRAM;
1578 return NULL;
1582 /* Translate a statement enum to a string. */
1584 const char *
1585 gfc_ascii_statement (gfc_statement st)
1587 const char *p;
1589 switch (st)
1591 case ST_ARITHMETIC_IF:
1592 p = _("arithmetic IF");
1593 break;
1594 case ST_ALLOCATE:
1595 p = "ALLOCATE";
1596 break;
1597 case ST_ASSOCIATE:
1598 p = "ASSOCIATE";
1599 break;
1600 case ST_ATTR_DECL:
1601 p = _("attribute declaration");
1602 break;
1603 case ST_BACKSPACE:
1604 p = "BACKSPACE";
1605 break;
1606 case ST_BLOCK:
1607 p = "BLOCK";
1608 break;
1609 case ST_BLOCK_DATA:
1610 p = "BLOCK DATA";
1611 break;
1612 case ST_CALL:
1613 p = "CALL";
1614 break;
1615 case ST_CASE:
1616 p = "CASE";
1617 break;
1618 case ST_CLOSE:
1619 p = "CLOSE";
1620 break;
1621 case ST_COMMON:
1622 p = "COMMON";
1623 break;
1624 case ST_CONTINUE:
1625 p = "CONTINUE";
1626 break;
1627 case ST_CONTAINS:
1628 p = "CONTAINS";
1629 break;
1630 case ST_CRITICAL:
1631 p = "CRITICAL";
1632 break;
1633 case ST_CYCLE:
1634 p = "CYCLE";
1635 break;
1636 case ST_DATA_DECL:
1637 p = _("data declaration");
1638 break;
1639 case ST_DATA:
1640 p = "DATA";
1641 break;
1642 case ST_DEALLOCATE:
1643 p = "DEALLOCATE";
1644 break;
1645 case ST_DERIVED_DECL:
1646 p = _("derived type declaration");
1647 break;
1648 case ST_DO:
1649 p = "DO";
1650 break;
1651 case ST_ELSE:
1652 p = "ELSE";
1653 break;
1654 case ST_ELSEIF:
1655 p = "ELSE IF";
1656 break;
1657 case ST_ELSEWHERE:
1658 p = "ELSEWHERE";
1659 break;
1660 case ST_EVENT_POST:
1661 p = "EVENT POST";
1662 break;
1663 case ST_EVENT_WAIT:
1664 p = "EVENT WAIT";
1665 break;
1666 case ST_END_ASSOCIATE:
1667 p = "END ASSOCIATE";
1668 break;
1669 case ST_END_BLOCK:
1670 p = "END BLOCK";
1671 break;
1672 case ST_END_BLOCK_DATA:
1673 p = "END BLOCK DATA";
1674 break;
1675 case ST_END_CRITICAL:
1676 p = "END CRITICAL";
1677 break;
1678 case ST_ENDDO:
1679 p = "END DO";
1680 break;
1681 case ST_END_FILE:
1682 p = "END FILE";
1683 break;
1684 case ST_END_FORALL:
1685 p = "END FORALL";
1686 break;
1687 case ST_END_FUNCTION:
1688 p = "END FUNCTION";
1689 break;
1690 case ST_ENDIF:
1691 p = "END IF";
1692 break;
1693 case ST_END_INTERFACE:
1694 p = "END INTERFACE";
1695 break;
1696 case ST_END_MODULE:
1697 p = "END MODULE";
1698 break;
1699 case ST_END_SUBMODULE:
1700 p = "END SUBMODULE";
1701 break;
1702 case ST_END_PROGRAM:
1703 p = "END PROGRAM";
1704 break;
1705 case ST_END_SELECT:
1706 p = "END SELECT";
1707 break;
1708 case ST_END_SUBROUTINE:
1709 p = "END SUBROUTINE";
1710 break;
1711 case ST_END_WHERE:
1712 p = "END WHERE";
1713 break;
1714 case ST_END_TYPE:
1715 p = "END TYPE";
1716 break;
1717 case ST_ENTRY:
1718 p = "ENTRY";
1719 break;
1720 case ST_EQUIVALENCE:
1721 p = "EQUIVALENCE";
1722 break;
1723 case ST_ERROR_STOP:
1724 p = "ERROR STOP";
1725 break;
1726 case ST_EXIT:
1727 p = "EXIT";
1728 break;
1729 case ST_FLUSH:
1730 p = "FLUSH";
1731 break;
1732 case ST_FORALL_BLOCK: /* Fall through */
1733 case ST_FORALL:
1734 p = "FORALL";
1735 break;
1736 case ST_FORMAT:
1737 p = "FORMAT";
1738 break;
1739 case ST_FUNCTION:
1740 p = "FUNCTION";
1741 break;
1742 case ST_GENERIC:
1743 p = "GENERIC";
1744 break;
1745 case ST_GOTO:
1746 p = "GOTO";
1747 break;
1748 case ST_IF_BLOCK:
1749 p = _("block IF");
1750 break;
1751 case ST_IMPLICIT:
1752 p = "IMPLICIT";
1753 break;
1754 case ST_IMPLICIT_NONE:
1755 p = "IMPLICIT NONE";
1756 break;
1757 case ST_IMPLIED_ENDDO:
1758 p = _("implied END DO");
1759 break;
1760 case ST_IMPORT:
1761 p = "IMPORT";
1762 break;
1763 case ST_INQUIRE:
1764 p = "INQUIRE";
1765 break;
1766 case ST_INTERFACE:
1767 p = "INTERFACE";
1768 break;
1769 case ST_LOCK:
1770 p = "LOCK";
1771 break;
1772 case ST_PARAMETER:
1773 p = "PARAMETER";
1774 break;
1775 case ST_PRIVATE:
1776 p = "PRIVATE";
1777 break;
1778 case ST_PUBLIC:
1779 p = "PUBLIC";
1780 break;
1781 case ST_MODULE:
1782 p = "MODULE";
1783 break;
1784 case ST_SUBMODULE:
1785 p = "SUBMODULE";
1786 break;
1787 case ST_PAUSE:
1788 p = "PAUSE";
1789 break;
1790 case ST_MODULE_PROC:
1791 p = "MODULE PROCEDURE";
1792 break;
1793 case ST_NAMELIST:
1794 p = "NAMELIST";
1795 break;
1796 case ST_NULLIFY:
1797 p = "NULLIFY";
1798 break;
1799 case ST_OPEN:
1800 p = "OPEN";
1801 break;
1802 case ST_PROGRAM:
1803 p = "PROGRAM";
1804 break;
1805 case ST_PROCEDURE:
1806 p = "PROCEDURE";
1807 break;
1808 case ST_READ:
1809 p = "READ";
1810 break;
1811 case ST_RETURN:
1812 p = "RETURN";
1813 break;
1814 case ST_REWIND:
1815 p = "REWIND";
1816 break;
1817 case ST_STOP:
1818 p = "STOP";
1819 break;
1820 case ST_SYNC_ALL:
1821 p = "SYNC ALL";
1822 break;
1823 case ST_SYNC_IMAGES:
1824 p = "SYNC IMAGES";
1825 break;
1826 case ST_SYNC_MEMORY:
1827 p = "SYNC MEMORY";
1828 break;
1829 case ST_SUBROUTINE:
1830 p = "SUBROUTINE";
1831 break;
1832 case ST_TYPE:
1833 p = "TYPE";
1834 break;
1835 case ST_UNLOCK:
1836 p = "UNLOCK";
1837 break;
1838 case ST_USE:
1839 p = "USE";
1840 break;
1841 case ST_WHERE_BLOCK: /* Fall through */
1842 case ST_WHERE:
1843 p = "WHERE";
1844 break;
1845 case ST_WAIT:
1846 p = "WAIT";
1847 break;
1848 case ST_WRITE:
1849 p = "WRITE";
1850 break;
1851 case ST_ASSIGNMENT:
1852 p = _("assignment");
1853 break;
1854 case ST_POINTER_ASSIGNMENT:
1855 p = _("pointer assignment");
1856 break;
1857 case ST_SELECT_CASE:
1858 p = "SELECT CASE";
1859 break;
1860 case ST_SELECT_TYPE:
1861 p = "SELECT TYPE";
1862 break;
1863 case ST_TYPE_IS:
1864 p = "TYPE IS";
1865 break;
1866 case ST_CLASS_IS:
1867 p = "CLASS IS";
1868 break;
1869 case ST_SEQUENCE:
1870 p = "SEQUENCE";
1871 break;
1872 case ST_SIMPLE_IF:
1873 p = _("simple IF");
1874 break;
1875 case ST_STATEMENT_FUNCTION:
1876 p = "STATEMENT FUNCTION";
1877 break;
1878 case ST_LABEL_ASSIGNMENT:
1879 p = "LABEL ASSIGNMENT";
1880 break;
1881 case ST_ENUM:
1882 p = "ENUM DEFINITION";
1883 break;
1884 case ST_ENUMERATOR:
1885 p = "ENUMERATOR DEFINITION";
1886 break;
1887 case ST_END_ENUM:
1888 p = "END ENUM";
1889 break;
1890 case ST_OACC_PARALLEL_LOOP:
1891 p = "!$ACC PARALLEL LOOP";
1892 break;
1893 case ST_OACC_END_PARALLEL_LOOP:
1894 p = "!$ACC END PARALLEL LOOP";
1895 break;
1896 case ST_OACC_PARALLEL:
1897 p = "!$ACC PARALLEL";
1898 break;
1899 case ST_OACC_END_PARALLEL:
1900 p = "!$ACC END PARALLEL";
1901 break;
1902 case ST_OACC_KERNELS:
1903 p = "!$ACC KERNELS";
1904 break;
1905 case ST_OACC_END_KERNELS:
1906 p = "!$ACC END KERNELS";
1907 break;
1908 case ST_OACC_KERNELS_LOOP:
1909 p = "!$ACC KERNELS LOOP";
1910 break;
1911 case ST_OACC_END_KERNELS_LOOP:
1912 p = "!$ACC END KERNELS LOOP";
1913 break;
1914 case ST_OACC_DATA:
1915 p = "!$ACC DATA";
1916 break;
1917 case ST_OACC_END_DATA:
1918 p = "!$ACC END DATA";
1919 break;
1920 case ST_OACC_HOST_DATA:
1921 p = "!$ACC HOST_DATA";
1922 break;
1923 case ST_OACC_END_HOST_DATA:
1924 p = "!$ACC END HOST_DATA";
1925 break;
1926 case ST_OACC_LOOP:
1927 p = "!$ACC LOOP";
1928 break;
1929 case ST_OACC_END_LOOP:
1930 p = "!$ACC END LOOP";
1931 break;
1932 case ST_OACC_DECLARE:
1933 p = "!$ACC DECLARE";
1934 break;
1935 case ST_OACC_UPDATE:
1936 p = "!$ACC UPDATE";
1937 break;
1938 case ST_OACC_WAIT:
1939 p = "!$ACC WAIT";
1940 break;
1941 case ST_OACC_CACHE:
1942 p = "!$ACC CACHE";
1943 break;
1944 case ST_OACC_ENTER_DATA:
1945 p = "!$ACC ENTER DATA";
1946 break;
1947 case ST_OACC_EXIT_DATA:
1948 p = "!$ACC EXIT DATA";
1949 break;
1950 case ST_OACC_ROUTINE:
1951 p = "!$ACC ROUTINE";
1952 break;
1953 case ST_OACC_ATOMIC:
1954 p = "!ACC ATOMIC";
1955 break;
1956 case ST_OACC_END_ATOMIC:
1957 p = "!ACC END ATOMIC";
1958 break;
1959 case ST_OMP_ATOMIC:
1960 p = "!$OMP ATOMIC";
1961 break;
1962 case ST_OMP_BARRIER:
1963 p = "!$OMP BARRIER";
1964 break;
1965 case ST_OMP_CANCEL:
1966 p = "!$OMP CANCEL";
1967 break;
1968 case ST_OMP_CANCELLATION_POINT:
1969 p = "!$OMP CANCELLATION POINT";
1970 break;
1971 case ST_OMP_CRITICAL:
1972 p = "!$OMP CRITICAL";
1973 break;
1974 case ST_OMP_DECLARE_REDUCTION:
1975 p = "!$OMP DECLARE REDUCTION";
1976 break;
1977 case ST_OMP_DECLARE_SIMD:
1978 p = "!$OMP DECLARE SIMD";
1979 break;
1980 case ST_OMP_DECLARE_TARGET:
1981 p = "!$OMP DECLARE TARGET";
1982 break;
1983 case ST_OMP_DISTRIBUTE:
1984 p = "!$OMP DISTRIBUTE";
1985 break;
1986 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
1987 p = "!$OMP DISTRIBUTE PARALLEL DO";
1988 break;
1989 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
1990 p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
1991 break;
1992 case ST_OMP_DISTRIBUTE_SIMD:
1993 p = "!$OMP DISTRIBUTE SIMD";
1994 break;
1995 case ST_OMP_DO:
1996 p = "!$OMP DO";
1997 break;
1998 case ST_OMP_DO_SIMD:
1999 p = "!$OMP DO SIMD";
2000 break;
2001 case ST_OMP_END_ATOMIC:
2002 p = "!$OMP END ATOMIC";
2003 break;
2004 case ST_OMP_END_CRITICAL:
2005 p = "!$OMP END CRITICAL";
2006 break;
2007 case ST_OMP_END_DISTRIBUTE:
2008 p = "!$OMP END DISTRIBUTE";
2009 break;
2010 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
2011 p = "!$OMP END DISTRIBUTE PARALLEL DO";
2012 break;
2013 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
2014 p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
2015 break;
2016 case ST_OMP_END_DISTRIBUTE_SIMD:
2017 p = "!$OMP END DISTRIBUTE SIMD";
2018 break;
2019 case ST_OMP_END_DO:
2020 p = "!$OMP END DO";
2021 break;
2022 case ST_OMP_END_DO_SIMD:
2023 p = "!$OMP END DO SIMD";
2024 break;
2025 case ST_OMP_END_SIMD:
2026 p = "!$OMP END SIMD";
2027 break;
2028 case ST_OMP_END_MASTER:
2029 p = "!$OMP END MASTER";
2030 break;
2031 case ST_OMP_END_ORDERED:
2032 p = "!$OMP END ORDERED";
2033 break;
2034 case ST_OMP_END_PARALLEL:
2035 p = "!$OMP END PARALLEL";
2036 break;
2037 case ST_OMP_END_PARALLEL_DO:
2038 p = "!$OMP END PARALLEL DO";
2039 break;
2040 case ST_OMP_END_PARALLEL_DO_SIMD:
2041 p = "!$OMP END PARALLEL DO SIMD";
2042 break;
2043 case ST_OMP_END_PARALLEL_SECTIONS:
2044 p = "!$OMP END PARALLEL SECTIONS";
2045 break;
2046 case ST_OMP_END_PARALLEL_WORKSHARE:
2047 p = "!$OMP END PARALLEL WORKSHARE";
2048 break;
2049 case ST_OMP_END_SECTIONS:
2050 p = "!$OMP END SECTIONS";
2051 break;
2052 case ST_OMP_END_SINGLE:
2053 p = "!$OMP END SINGLE";
2054 break;
2055 case ST_OMP_END_TASK:
2056 p = "!$OMP END TASK";
2057 break;
2058 case ST_OMP_END_TARGET:
2059 p = "!$OMP END TARGET";
2060 break;
2061 case ST_OMP_END_TARGET_DATA:
2062 p = "!$OMP END TARGET DATA";
2063 break;
2064 case ST_OMP_END_TARGET_TEAMS:
2065 p = "!$OMP END TARGET TEAMS";
2066 break;
2067 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
2068 p = "!$OMP END TARGET TEAMS DISTRIBUTE";
2069 break;
2070 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2071 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2072 break;
2073 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2074 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2075 break;
2076 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
2077 p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2078 break;
2079 case ST_OMP_END_TASKGROUP:
2080 p = "!$OMP END TASKGROUP";
2081 break;
2082 case ST_OMP_END_TEAMS:
2083 p = "!$OMP END TEAMS";
2084 break;
2085 case ST_OMP_END_TEAMS_DISTRIBUTE:
2086 p = "!$OMP END TEAMS DISTRIBUTE";
2087 break;
2088 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
2089 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2090 break;
2091 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2092 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2093 break;
2094 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
2095 p = "!$OMP END TEAMS DISTRIBUTE SIMD";
2096 break;
2097 case ST_OMP_END_WORKSHARE:
2098 p = "!$OMP END WORKSHARE";
2099 break;
2100 case ST_OMP_FLUSH:
2101 p = "!$OMP FLUSH";
2102 break;
2103 case ST_OMP_MASTER:
2104 p = "!$OMP MASTER";
2105 break;
2106 case ST_OMP_ORDERED:
2107 p = "!$OMP ORDERED";
2108 break;
2109 case ST_OMP_PARALLEL:
2110 p = "!$OMP PARALLEL";
2111 break;
2112 case ST_OMP_PARALLEL_DO:
2113 p = "!$OMP PARALLEL DO";
2114 break;
2115 case ST_OMP_PARALLEL_DO_SIMD:
2116 p = "!$OMP PARALLEL DO SIMD";
2117 break;
2118 case ST_OMP_PARALLEL_SECTIONS:
2119 p = "!$OMP PARALLEL SECTIONS";
2120 break;
2121 case ST_OMP_PARALLEL_WORKSHARE:
2122 p = "!$OMP PARALLEL WORKSHARE";
2123 break;
2124 case ST_OMP_SECTIONS:
2125 p = "!$OMP SECTIONS";
2126 break;
2127 case ST_OMP_SECTION:
2128 p = "!$OMP SECTION";
2129 break;
2130 case ST_OMP_SIMD:
2131 p = "!$OMP SIMD";
2132 break;
2133 case ST_OMP_SINGLE:
2134 p = "!$OMP SINGLE";
2135 break;
2136 case ST_OMP_TARGET:
2137 p = "!$OMP TARGET";
2138 break;
2139 case ST_OMP_TARGET_DATA:
2140 p = "!$OMP TARGET DATA";
2141 break;
2142 case ST_OMP_TARGET_TEAMS:
2143 p = "!$OMP TARGET TEAMS";
2144 break;
2145 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
2146 p = "!$OMP TARGET TEAMS DISTRIBUTE";
2147 break;
2148 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2149 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2150 break;
2151 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2152 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2153 break;
2154 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2155 p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2156 break;
2157 case ST_OMP_TARGET_UPDATE:
2158 p = "!$OMP TARGET UPDATE";
2159 break;
2160 case ST_OMP_TASK:
2161 p = "!$OMP TASK";
2162 break;
2163 case ST_OMP_TASKGROUP:
2164 p = "!$OMP TASKGROUP";
2165 break;
2166 case ST_OMP_TASKWAIT:
2167 p = "!$OMP TASKWAIT";
2168 break;
2169 case ST_OMP_TASKYIELD:
2170 p = "!$OMP TASKYIELD";
2171 break;
2172 case ST_OMP_TEAMS:
2173 p = "!$OMP TEAMS";
2174 break;
2175 case ST_OMP_TEAMS_DISTRIBUTE:
2176 p = "!$OMP TEAMS DISTRIBUTE";
2177 break;
2178 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2179 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2180 break;
2181 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2182 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2183 break;
2184 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
2185 p = "!$OMP TEAMS DISTRIBUTE SIMD";
2186 break;
2187 case ST_OMP_THREADPRIVATE:
2188 p = "!$OMP THREADPRIVATE";
2189 break;
2190 case ST_OMP_WORKSHARE:
2191 p = "!$OMP WORKSHARE";
2192 break;
2193 default:
2194 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2197 return p;
2201 /* Create a symbol for the main program and assign it to ns->proc_name. */
2203 static void
2204 main_program_symbol (gfc_namespace *ns, const char *name)
2206 gfc_symbol *main_program;
2207 symbol_attribute attr;
2209 gfc_get_symbol (name, ns, &main_program);
2210 gfc_clear_attr (&attr);
2211 attr.flavor = FL_PROGRAM;
2212 attr.proc = PROC_UNKNOWN;
2213 attr.subroutine = 1;
2214 attr.access = ACCESS_PUBLIC;
2215 attr.is_main_program = 1;
2216 main_program->attr = attr;
2217 main_program->declared_at = gfc_current_locus;
2218 ns->proc_name = main_program;
2219 gfc_commit_symbols ();
2223 /* Do whatever is necessary to accept the last statement. */
2225 static void
2226 accept_statement (gfc_statement st)
2228 switch (st)
2230 case ST_IMPLICIT_NONE:
2231 case ST_IMPLICIT:
2232 break;
2234 case ST_FUNCTION:
2235 case ST_SUBROUTINE:
2236 case ST_MODULE:
2237 case ST_SUBMODULE:
2238 gfc_current_ns->proc_name = gfc_new_block;
2239 break;
2241 /* If the statement is the end of a block, lay down a special code
2242 that allows a branch to the end of the block from within the
2243 construct. IF and SELECT are treated differently from DO
2244 (where EXEC_NOP is added inside the loop) for two
2245 reasons:
2246 1. END DO has a meaning in the sense that after a GOTO to
2247 it, the loop counter must be increased.
2248 2. IF blocks and SELECT blocks can consist of multiple
2249 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2250 Putting the label before the END IF would make the jump
2251 from, say, the ELSE IF block to the END IF illegal. */
2253 case ST_ENDIF:
2254 case ST_END_SELECT:
2255 case ST_END_CRITICAL:
2256 if (gfc_statement_label != NULL)
2258 new_st.op = EXEC_END_NESTED_BLOCK;
2259 add_statement ();
2261 break;
2263 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2264 one parallel block. Thus, we add the special code to the nested block
2265 itself, instead of the parent one. */
2266 case ST_END_BLOCK:
2267 case ST_END_ASSOCIATE:
2268 if (gfc_statement_label != NULL)
2270 new_st.op = EXEC_END_BLOCK;
2271 add_statement ();
2273 break;
2275 /* The end-of-program unit statements do not get the special
2276 marker and require a statement of some sort if they are a
2277 branch target. */
2279 case ST_END_PROGRAM:
2280 case ST_END_FUNCTION:
2281 case ST_END_SUBROUTINE:
2282 if (gfc_statement_label != NULL)
2284 new_st.op = EXEC_RETURN;
2285 add_statement ();
2287 else
2289 new_st.op = EXEC_END_PROCEDURE;
2290 add_statement ();
2293 break;
2295 case ST_ENTRY:
2296 case_executable:
2297 case_exec_markers:
2298 add_statement ();
2299 break;
2301 default:
2302 break;
2305 gfc_commit_symbols ();
2306 gfc_warning_check ();
2307 gfc_clear_new_st ();
2311 /* Undo anything tentative that has been built for the current
2312 statement. */
2314 static void
2315 reject_statement (void)
2317 /* Revert to the previous charlen chain. */
2318 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
2319 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
2321 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
2322 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
2324 gfc_reject_data (gfc_current_ns);
2326 gfc_new_block = NULL;
2327 gfc_undo_symbols ();
2328 gfc_clear_warning ();
2329 undo_new_statement ();
2333 /* Generic complaint about an out of order statement. We also do
2334 whatever is necessary to clean up. */
2336 static void
2337 unexpected_statement (gfc_statement st)
2339 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
2341 reject_statement ();
2345 /* Given the next statement seen by the matcher, make sure that it is
2346 in proper order with the last. This subroutine is initialized by
2347 calling it with an argument of ST_NONE. If there is a problem, we
2348 issue an error and return false. Otherwise we return true.
2350 Individual parsers need to verify that the statements seen are
2351 valid before calling here, i.e., ENTRY statements are not allowed in
2352 INTERFACE blocks. The following diagram is taken from the standard:
2354 +---------------------------------------+
2355 | program subroutine function module |
2356 +---------------------------------------+
2357 | use |
2358 +---------------------------------------+
2359 | import |
2360 +---------------------------------------+
2361 | | implicit none |
2362 | +-----------+------------------+
2363 | | parameter | implicit |
2364 | +-----------+------------------+
2365 | format | | derived type |
2366 | entry | parameter | interface |
2367 | | data | specification |
2368 | | | statement func |
2369 | +-----------+------------------+
2370 | | data | executable |
2371 +--------+-----------+------------------+
2372 | contains |
2373 +---------------------------------------+
2374 | internal module/subprogram |
2375 +---------------------------------------+
2376 | end |
2377 +---------------------------------------+
2381 enum state_order
2383 ORDER_START,
2384 ORDER_USE,
2385 ORDER_IMPORT,
2386 ORDER_IMPLICIT_NONE,
2387 ORDER_IMPLICIT,
2388 ORDER_SPEC,
2389 ORDER_EXEC
2392 typedef struct
2394 enum state_order state;
2395 gfc_statement last_statement;
2396 locus where;
2398 st_state;
2400 static bool
2401 verify_st_order (st_state *p, gfc_statement st, bool silent)
2404 switch (st)
2406 case ST_NONE:
2407 p->state = ORDER_START;
2408 break;
2410 case ST_USE:
2411 if (p->state > ORDER_USE)
2412 goto order;
2413 p->state = ORDER_USE;
2414 break;
2416 case ST_IMPORT:
2417 if (p->state > ORDER_IMPORT)
2418 goto order;
2419 p->state = ORDER_IMPORT;
2420 break;
2422 case ST_IMPLICIT_NONE:
2423 if (p->state > ORDER_IMPLICIT)
2424 goto order;
2426 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2427 statement disqualifies a USE but not an IMPLICIT NONE.
2428 Duplicate IMPLICIT NONEs are caught when the implicit types
2429 are set. */
2431 p->state = ORDER_IMPLICIT_NONE;
2432 break;
2434 case ST_IMPLICIT:
2435 if (p->state > ORDER_IMPLICIT)
2436 goto order;
2437 p->state = ORDER_IMPLICIT;
2438 break;
2440 case ST_FORMAT:
2441 case ST_ENTRY:
2442 if (p->state < ORDER_IMPLICIT_NONE)
2443 p->state = ORDER_IMPLICIT_NONE;
2444 break;
2446 case ST_PARAMETER:
2447 if (p->state >= ORDER_EXEC)
2448 goto order;
2449 if (p->state < ORDER_IMPLICIT)
2450 p->state = ORDER_IMPLICIT;
2451 break;
2453 case ST_DATA:
2454 if (p->state < ORDER_SPEC)
2455 p->state = ORDER_SPEC;
2456 break;
2458 case ST_PUBLIC:
2459 case ST_PRIVATE:
2460 case ST_DERIVED_DECL:
2461 case_decl:
2462 if (p->state >= ORDER_EXEC)
2463 goto order;
2464 if (p->state < ORDER_SPEC)
2465 p->state = ORDER_SPEC;
2466 break;
2468 case_executable:
2469 case_exec_markers:
2470 if (p->state < ORDER_EXEC)
2471 p->state = ORDER_EXEC;
2472 break;
2474 default:
2475 return false;
2478 /* All is well, record the statement in case we need it next time. */
2479 p->where = gfc_current_locus;
2480 p->last_statement = st;
2481 return true;
2483 order:
2484 if (!silent)
2485 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2486 gfc_ascii_statement (st),
2487 gfc_ascii_statement (p->last_statement), &p->where);
2489 return false;
2493 /* Handle an unexpected end of file. This is a show-stopper... */
2495 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
2497 static void
2498 unexpected_eof (void)
2500 gfc_state_data *p;
2502 gfc_error ("Unexpected end of file in %qs", gfc_source_file);
2504 /* Memory cleanup. Move to "second to last". */
2505 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
2506 p = p->previous);
2508 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
2509 gfc_done_2 ();
2511 longjmp (eof_buf, 1);
2515 /* Parse the CONTAINS section of a derived type definition. */
2517 gfc_access gfc_typebound_default_access;
2519 static bool
2520 parse_derived_contains (void)
2522 gfc_state_data s;
2523 bool seen_private = false;
2524 bool seen_comps = false;
2525 bool error_flag = false;
2526 bool to_finish;
2528 gcc_assert (gfc_current_state () == COMP_DERIVED);
2529 gcc_assert (gfc_current_block ());
2531 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2532 section. */
2533 if (gfc_current_block ()->attr.sequence)
2534 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2535 " section at %C", gfc_current_block ()->name);
2536 if (gfc_current_block ()->attr.is_bind_c)
2537 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2538 " section at %C", gfc_current_block ()->name);
2540 accept_statement (ST_CONTAINS);
2541 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
2543 gfc_typebound_default_access = ACCESS_PUBLIC;
2545 to_finish = false;
2546 while (!to_finish)
2548 gfc_statement st;
2549 st = next_statement ();
2550 switch (st)
2552 case ST_NONE:
2553 unexpected_eof ();
2554 break;
2556 case ST_DATA_DECL:
2557 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2558 goto error;
2560 case ST_PROCEDURE:
2561 if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
2562 goto error;
2564 accept_statement (ST_PROCEDURE);
2565 seen_comps = true;
2566 break;
2568 case ST_GENERIC:
2569 if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
2570 goto error;
2572 accept_statement (ST_GENERIC);
2573 seen_comps = true;
2574 break;
2576 case ST_FINAL:
2577 if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
2578 " at %C"))
2579 goto error;
2581 accept_statement (ST_FINAL);
2582 seen_comps = true;
2583 break;
2585 case ST_END_TYPE:
2586 to_finish = true;
2588 if (!seen_comps
2589 && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
2590 "at %C with empty CONTAINS section")))
2591 goto error;
2593 /* ST_END_TYPE is accepted by parse_derived after return. */
2594 break;
2596 case ST_PRIVATE:
2597 if (!gfc_find_state (COMP_MODULE))
2599 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2600 "a MODULE");
2601 goto error;
2604 if (seen_comps)
2606 gfc_error ("PRIVATE statement at %C must precede procedure"
2607 " bindings");
2608 goto error;
2611 if (seen_private)
2613 gfc_error ("Duplicate PRIVATE statement at %C");
2614 goto error;
2617 accept_statement (ST_PRIVATE);
2618 gfc_typebound_default_access = ACCESS_PRIVATE;
2619 seen_private = true;
2620 break;
2622 case ST_SEQUENCE:
2623 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2624 goto error;
2626 case ST_CONTAINS:
2627 gfc_error ("Already inside a CONTAINS block at %C");
2628 goto error;
2630 default:
2631 unexpected_statement (st);
2632 break;
2635 continue;
2637 error:
2638 error_flag = true;
2639 reject_statement ();
2642 pop_state ();
2643 gcc_assert (gfc_current_state () == COMP_DERIVED);
2645 return error_flag;
2649 /* Parse a derived type. */
2651 static void
2652 parse_derived (void)
2654 int compiling_type, seen_private, seen_sequence, seen_component;
2655 gfc_statement st;
2656 gfc_state_data s;
2657 gfc_symbol *sym;
2658 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
2660 accept_statement (ST_DERIVED_DECL);
2661 push_state (&s, COMP_DERIVED, gfc_new_block);
2663 gfc_new_block->component_access = ACCESS_PUBLIC;
2664 seen_private = 0;
2665 seen_sequence = 0;
2666 seen_component = 0;
2668 compiling_type = 1;
2670 while (compiling_type)
2672 st = next_statement ();
2673 switch (st)
2675 case ST_NONE:
2676 unexpected_eof ();
2678 case ST_DATA_DECL:
2679 case ST_PROCEDURE:
2680 accept_statement (st);
2681 seen_component = 1;
2682 break;
2684 case ST_FINAL:
2685 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2686 break;
2688 case ST_END_TYPE:
2689 endType:
2690 compiling_type = 0;
2692 if (!seen_component)
2693 gfc_notify_std (GFC_STD_F2003, "Derived type "
2694 "definition at %C without components");
2696 accept_statement (ST_END_TYPE);
2697 break;
2699 case ST_PRIVATE:
2700 if (!gfc_find_state (COMP_MODULE))
2702 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2703 "a MODULE");
2704 break;
2707 if (seen_component)
2709 gfc_error ("PRIVATE statement at %C must precede "
2710 "structure components");
2711 break;
2714 if (seen_private)
2715 gfc_error ("Duplicate PRIVATE statement at %C");
2717 s.sym->component_access = ACCESS_PRIVATE;
2719 accept_statement (ST_PRIVATE);
2720 seen_private = 1;
2721 break;
2723 case ST_SEQUENCE:
2724 if (seen_component)
2726 gfc_error ("SEQUENCE statement at %C must precede "
2727 "structure components");
2728 break;
2731 if (gfc_current_block ()->attr.sequence)
2732 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
2733 "TYPE statement");
2735 if (seen_sequence)
2737 gfc_error ("Duplicate SEQUENCE statement at %C");
2740 seen_sequence = 1;
2741 gfc_add_sequence (&gfc_current_block ()->attr,
2742 gfc_current_block ()->name, NULL);
2743 break;
2745 case ST_CONTAINS:
2746 gfc_notify_std (GFC_STD_F2003,
2747 "CONTAINS block in derived type"
2748 " definition at %C");
2750 accept_statement (ST_CONTAINS);
2751 parse_derived_contains ();
2752 goto endType;
2754 default:
2755 unexpected_statement (st);
2756 break;
2760 /* need to verify that all fields of the derived type are
2761 * interoperable with C if the type is declared to be bind(c)
2763 sym = gfc_current_block ();
2764 for (c = sym->components; c; c = c->next)
2766 bool coarray, lock_type, event_type, allocatable, pointer;
2767 coarray = lock_type = event_type = allocatable = pointer = false;
2769 /* Look for allocatable components. */
2770 if (c->attr.allocatable
2771 || (c->ts.type == BT_CLASS && c->attr.class_ok
2772 && CLASS_DATA (c)->attr.allocatable)
2773 || (c->ts.type == BT_DERIVED && !c->attr.pointer
2774 && c->ts.u.derived->attr.alloc_comp))
2776 allocatable = true;
2777 sym->attr.alloc_comp = 1;
2780 /* Look for pointer components. */
2781 if (c->attr.pointer
2782 || (c->ts.type == BT_CLASS && c->attr.class_ok
2783 && CLASS_DATA (c)->attr.class_pointer)
2784 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2786 pointer = true;
2787 sym->attr.pointer_comp = 1;
2790 /* Look for procedure pointer components. */
2791 if (c->attr.proc_pointer
2792 || (c->ts.type == BT_DERIVED
2793 && c->ts.u.derived->attr.proc_pointer_comp))
2794 sym->attr.proc_pointer_comp = 1;
2796 /* Looking for coarray components. */
2797 if (c->attr.codimension
2798 || (c->ts.type == BT_CLASS && c->attr.class_ok
2799 && CLASS_DATA (c)->attr.codimension))
2801 coarray = true;
2802 sym->attr.coarray_comp = 1;
2805 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
2806 && !c->attr.pointer)
2808 coarray = true;
2809 sym->attr.coarray_comp = 1;
2812 /* Looking for lock_type components. */
2813 if ((c->ts.type == BT_DERIVED
2814 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2815 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2816 || (c->ts.type == BT_CLASS && c->attr.class_ok
2817 && CLASS_DATA (c)->ts.u.derived->from_intmod
2818 == INTMOD_ISO_FORTRAN_ENV
2819 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2820 == ISOFORTRAN_LOCK_TYPE)
2821 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
2822 && !allocatable && !pointer))
2824 lock_type = 1;
2825 lock_comp = c;
2826 sym->attr.lock_comp = 1;
2829 /* Looking for event_type components. */
2830 if ((c->ts.type == BT_DERIVED
2831 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2832 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2833 || (c->ts.type == BT_CLASS && c->attr.class_ok
2834 && CLASS_DATA (c)->ts.u.derived->from_intmod
2835 == INTMOD_ISO_FORTRAN_ENV
2836 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2837 == ISOFORTRAN_EVENT_TYPE)
2838 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.event_comp
2839 && !allocatable && !pointer))
2841 event_type = 1;
2842 event_comp = c;
2843 sym->attr.event_comp = 1;
2846 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2847 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2848 unless there are nondirect [allocatable or pointer] components
2849 involved (cf. 1.3.33.1 and 1.3.33.3). */
2851 if (pointer && !coarray && lock_type)
2852 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2853 "codimension or be a subcomponent of a coarray, "
2854 "which is not possible as the component has the "
2855 "pointer attribute", c->name, &c->loc);
2856 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2857 && c->ts.u.derived->attr.lock_comp)
2858 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2859 "of type LOCK_TYPE, which must have a codimension or be a "
2860 "subcomponent of a coarray", c->name, &c->loc);
2862 if (lock_type && allocatable && !coarray)
2863 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2864 "a codimension", c->name, &c->loc);
2865 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
2866 && c->ts.u.derived->attr.lock_comp)
2867 gfc_error ("Allocatable component %s at %L must have a codimension as "
2868 "it has a noncoarray subcomponent of type LOCK_TYPE",
2869 c->name, &c->loc);
2871 if (sym->attr.coarray_comp && !coarray && lock_type)
2872 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2873 "subcomponent of type LOCK_TYPE must have a codimension or "
2874 "be a subcomponent of a coarray. (Variables of type %s may "
2875 "not have a codimension as already a coarray "
2876 "subcomponent exists)", c->name, &c->loc, sym->name);
2878 if (sym->attr.lock_comp && coarray && !lock_type)
2879 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2880 "subcomponent of type LOCK_TYPE must have a codimension or "
2881 "be a subcomponent of a coarray. (Variables of type %s may "
2882 "not have a codimension as %s at %L has a codimension or a "
2883 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
2884 sym->name, c->name, &c->loc);
2886 /* Similarly for EVENT TYPE. */
2888 if (pointer && !coarray && event_type)
2889 gfc_error ("Component %s at %L of type EVENT_TYPE must have a "
2890 "codimension or be a subcomponent of a coarray, "
2891 "which is not possible as the component has the "
2892 "pointer attribute", c->name, &c->loc);
2893 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2894 && c->ts.u.derived->attr.event_comp)
2895 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2896 "of type EVENT_TYPE, which must have a codimension or be a "
2897 "subcomponent of a coarray", c->name, &c->loc);
2899 if (event_type && allocatable && !coarray)
2900 gfc_error ("Allocatable component %s at %L of type EVENT_TYPE must have "
2901 "a codimension", c->name, &c->loc);
2902 else if (event_type && allocatable && c->ts.type == BT_DERIVED
2903 && c->ts.u.derived->attr.event_comp)
2904 gfc_error ("Allocatable component %s at %L must have a codimension as "
2905 "it has a noncoarray subcomponent of type EVENT_TYPE",
2906 c->name, &c->loc);
2908 if (sym->attr.coarray_comp && !coarray && event_type)
2909 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
2910 "subcomponent of type EVENT_TYPE must have a codimension or "
2911 "be a subcomponent of a coarray. (Variables of type %s may "
2912 "not have a codimension as already a coarray "
2913 "subcomponent exists)", c->name, &c->loc, sym->name);
2915 if (sym->attr.event_comp && coarray && !event_type)
2916 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
2917 "subcomponent of type EVENT_TYPE must have a codimension or "
2918 "be a subcomponent of a coarray. (Variables of type %s may "
2919 "not have a codimension as %s at %L has a codimension or a "
2920 "coarray subcomponent)", event_comp->name, &event_comp->loc,
2921 sym->name, c->name, &c->loc);
2923 /* Look for private components. */
2924 if (sym->component_access == ACCESS_PRIVATE
2925 || c->attr.access == ACCESS_PRIVATE
2926 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
2927 sym->attr.private_comp = 1;
2930 if (!seen_component)
2931 sym->attr.zero_comp = 1;
2933 pop_state ();
2937 /* Parse an ENUM. */
2939 static void
2940 parse_enum (void)
2942 gfc_statement st;
2943 int compiling_enum;
2944 gfc_state_data s;
2945 int seen_enumerator = 0;
2947 push_state (&s, COMP_ENUM, gfc_new_block);
2949 compiling_enum = 1;
2951 while (compiling_enum)
2953 st = next_statement ();
2954 switch (st)
2956 case ST_NONE:
2957 unexpected_eof ();
2958 break;
2960 case ST_ENUMERATOR:
2961 seen_enumerator = 1;
2962 accept_statement (st);
2963 break;
2965 case ST_END_ENUM:
2966 compiling_enum = 0;
2967 if (!seen_enumerator)
2968 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2969 accept_statement (st);
2970 break;
2972 default:
2973 gfc_free_enum_history ();
2974 unexpected_statement (st);
2975 break;
2978 pop_state ();
2982 /* Parse an interface. We must be able to deal with the possibility
2983 of recursive interfaces. The parse_spec() subroutine is mutually
2984 recursive with parse_interface(). */
2986 static gfc_statement parse_spec (gfc_statement);
2988 static void
2989 parse_interface (void)
2991 gfc_compile_state new_state = COMP_NONE, current_state;
2992 gfc_symbol *prog_unit, *sym;
2993 gfc_interface_info save;
2994 gfc_state_data s1, s2;
2995 gfc_statement st;
2997 accept_statement (ST_INTERFACE);
2999 current_interface.ns = gfc_current_ns;
3000 save = current_interface;
3002 sym = (current_interface.type == INTERFACE_GENERIC
3003 || current_interface.type == INTERFACE_USER_OP)
3004 ? gfc_new_block : NULL;
3006 push_state (&s1, COMP_INTERFACE, sym);
3007 current_state = COMP_NONE;
3009 loop:
3010 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
3012 st = next_statement ();
3013 switch (st)
3015 case ST_NONE:
3016 unexpected_eof ();
3018 case ST_SUBROUTINE:
3019 case ST_FUNCTION:
3020 if (st == ST_SUBROUTINE)
3021 new_state = COMP_SUBROUTINE;
3022 else if (st == ST_FUNCTION)
3023 new_state = COMP_FUNCTION;
3024 if (gfc_new_block->attr.pointer)
3026 gfc_new_block->attr.pointer = 0;
3027 gfc_new_block->attr.proc_pointer = 1;
3029 if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
3030 gfc_new_block->formal, NULL))
3032 reject_statement ();
3033 gfc_free_namespace (gfc_current_ns);
3034 goto loop;
3036 /* F2008 C1210 forbids the IMPORT statement in module procedure
3037 interface bodies and the flag is set to import symbols. */
3038 if (gfc_new_block->attr.module_procedure)
3039 gfc_current_ns->has_import_set = 1;
3040 break;
3042 case ST_PROCEDURE:
3043 case ST_MODULE_PROC: /* The module procedure matcher makes
3044 sure the context is correct. */
3045 accept_statement (st);
3046 gfc_free_namespace (gfc_current_ns);
3047 goto loop;
3049 case ST_END_INTERFACE:
3050 gfc_free_namespace (gfc_current_ns);
3051 gfc_current_ns = current_interface.ns;
3052 goto done;
3054 default:
3055 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
3056 gfc_ascii_statement (st));
3057 reject_statement ();
3058 gfc_free_namespace (gfc_current_ns);
3059 goto loop;
3063 /* Make sure that the generic name has the right attribute. */
3064 if (current_interface.type == INTERFACE_GENERIC
3065 && current_state == COMP_NONE)
3067 if (new_state == COMP_FUNCTION && sym)
3068 gfc_add_function (&sym->attr, sym->name, NULL);
3069 else if (new_state == COMP_SUBROUTINE && sym)
3070 gfc_add_subroutine (&sym->attr, sym->name, NULL);
3072 current_state = new_state;
3075 if (current_interface.type == INTERFACE_ABSTRACT)
3077 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
3078 if (gfc_is_intrinsic_typename (gfc_new_block->name))
3079 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
3080 "cannot be the same as an intrinsic type",
3081 gfc_new_block->name);
3084 push_state (&s2, new_state, gfc_new_block);
3085 accept_statement (st);
3086 prog_unit = gfc_new_block;
3087 prog_unit->formal_ns = gfc_current_ns;
3088 if (prog_unit == prog_unit->formal_ns->proc_name
3089 && prog_unit->ns != prog_unit->formal_ns)
3090 prog_unit->refs++;
3092 decl:
3093 /* Read data declaration statements. */
3094 st = parse_spec (ST_NONE);
3095 in_specification_block = true;
3097 /* Since the interface block does not permit an IMPLICIT statement,
3098 the default type for the function or the result must be taken
3099 from the formal namespace. */
3100 if (new_state == COMP_FUNCTION)
3102 if (prog_unit->result == prog_unit
3103 && prog_unit->ts.type == BT_UNKNOWN)
3104 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
3105 else if (prog_unit->result != prog_unit
3106 && prog_unit->result->ts.type == BT_UNKNOWN)
3107 gfc_set_default_type (prog_unit->result, 1,
3108 prog_unit->formal_ns);
3111 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
3113 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3114 gfc_ascii_statement (st));
3115 reject_statement ();
3116 goto decl;
3119 /* Add EXTERNAL attribute to function or subroutine. */
3120 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
3121 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
3123 current_interface = save;
3124 gfc_add_interface (prog_unit);
3125 pop_state ();
3127 if (current_interface.ns
3128 && current_interface.ns->proc_name
3129 && strcmp (current_interface.ns->proc_name->name,
3130 prog_unit->name) == 0)
3131 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3132 "enclosing procedure", prog_unit->name,
3133 &current_interface.ns->proc_name->declared_at);
3135 goto loop;
3137 done:
3138 pop_state ();
3142 /* Associate function characteristics by going back to the function
3143 declaration and rematching the prefix. */
3145 static match
3146 match_deferred_characteristics (gfc_typespec * ts)
3148 locus loc;
3149 match m = MATCH_ERROR;
3150 char name[GFC_MAX_SYMBOL_LEN + 1];
3152 loc = gfc_current_locus;
3154 gfc_current_locus = gfc_current_block ()->declared_at;
3156 gfc_clear_error ();
3157 gfc_buffer_error (true);
3158 m = gfc_match_prefix (ts);
3159 gfc_buffer_error (false);
3161 if (ts->type == BT_DERIVED)
3163 ts->kind = 0;
3165 if (!ts->u.derived)
3166 m = MATCH_ERROR;
3169 /* Only permit one go at the characteristic association. */
3170 if (ts->kind == -1)
3171 ts->kind = 0;
3173 /* Set the function locus correctly. If we have not found the
3174 function name, there is an error. */
3175 if (m == MATCH_YES
3176 && gfc_match ("function% %n", name) == MATCH_YES
3177 && strcmp (name, gfc_current_block ()->name) == 0)
3179 gfc_current_block ()->declared_at = gfc_current_locus;
3180 gfc_commit_symbols ();
3182 else
3184 gfc_error_check ();
3185 gfc_undo_symbols ();
3188 gfc_current_locus =loc;
3189 return m;
3193 /* Check specification-expressions in the function result of the currently
3194 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3195 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3196 scope are not yet parsed so this has to be delayed up to parse_spec. */
3198 static void
3199 check_function_result_typed (void)
3201 gfc_typespec ts;
3203 gcc_assert (gfc_current_state () == COMP_FUNCTION);
3205 if (!gfc_current_ns->proc_name->result) return;
3207 ts = gfc_current_ns->proc_name->result->ts;
3209 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3210 /* TODO: Extend when KIND type parameters are implemented. */
3211 if (ts.type == BT_CHARACTER && ts.u.cl && ts.u.cl->length)
3212 gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true);
3216 /* Parse a set of specification statements. Returns the statement
3217 that doesn't fit. */
3219 static gfc_statement
3220 parse_spec (gfc_statement st)
3222 st_state ss;
3223 bool function_result_typed = false;
3224 bool bad_characteristic = false;
3225 gfc_typespec *ts;
3227 in_specification_block = true;
3229 verify_st_order (&ss, ST_NONE, false);
3230 if (st == ST_NONE)
3231 st = next_statement ();
3233 /* If we are not inside a function or don't have a result specified so far,
3234 do nothing special about it. */
3235 if (gfc_current_state () != COMP_FUNCTION)
3236 function_result_typed = true;
3237 else
3239 gfc_symbol* proc = gfc_current_ns->proc_name;
3240 gcc_assert (proc);
3242 if (proc->result->ts.type == BT_UNKNOWN)
3243 function_result_typed = true;
3246 loop:
3248 /* If we're inside a BLOCK construct, some statements are disallowed.
3249 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3250 or VALUE are also disallowed, but they don't have a particular ST_*
3251 key so we have to check for them individually in their matcher routine. */
3252 if (gfc_current_state () == COMP_BLOCK)
3253 switch (st)
3255 case ST_IMPLICIT:
3256 case ST_IMPLICIT_NONE:
3257 case ST_NAMELIST:
3258 case ST_COMMON:
3259 case ST_EQUIVALENCE:
3260 case ST_STATEMENT_FUNCTION:
3261 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3262 gfc_ascii_statement (st));
3263 reject_statement ();
3264 break;
3266 default:
3267 break;
3269 else if (gfc_current_state () == COMP_BLOCK_DATA)
3270 /* Fortran 2008, C1116. */
3271 switch (st)
3273 case ST_DATA_DECL:
3274 case ST_COMMON:
3275 case ST_DATA:
3276 case ST_TYPE:
3277 case ST_END_BLOCK_DATA:
3278 case ST_ATTR_DECL:
3279 case ST_EQUIVALENCE:
3280 case ST_PARAMETER:
3281 case ST_IMPLICIT:
3282 case ST_IMPLICIT_NONE:
3283 case ST_DERIVED_DECL:
3284 case ST_USE:
3285 break;
3287 case ST_NONE:
3288 break;
3290 default:
3291 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3292 gfc_ascii_statement (st));
3293 reject_statement ();
3294 break;
3297 /* If we find a statement that can not be followed by an IMPLICIT statement
3298 (and thus we can expect to see none any further), type the function result
3299 if it has not yet been typed. Be careful not to give the END statement
3300 to verify_st_order! */
3301 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
3303 bool verify_now = false;
3305 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
3306 verify_now = true;
3307 else
3309 st_state dummyss;
3310 verify_st_order (&dummyss, ST_NONE, false);
3311 verify_st_order (&dummyss, st, false);
3313 if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
3314 verify_now = true;
3317 if (verify_now)
3319 check_function_result_typed ();
3320 function_result_typed = true;
3324 switch (st)
3326 case ST_NONE:
3327 unexpected_eof ();
3329 case ST_IMPLICIT_NONE:
3330 case ST_IMPLICIT:
3331 if (!function_result_typed)
3333 check_function_result_typed ();
3334 function_result_typed = true;
3336 goto declSt;
3338 case ST_FORMAT:
3339 case ST_ENTRY:
3340 case ST_DATA: /* Not allowed in interfaces */
3341 if (gfc_current_state () == COMP_INTERFACE)
3342 break;
3344 /* Fall through */
3346 case ST_USE:
3347 case ST_IMPORT:
3348 case ST_PARAMETER:
3349 case ST_PUBLIC:
3350 case ST_PRIVATE:
3351 case ST_DERIVED_DECL:
3352 case_decl:
3353 declSt:
3354 if (!verify_st_order (&ss, st, false))
3356 reject_statement ();
3357 st = next_statement ();
3358 goto loop;
3361 switch (st)
3363 case ST_INTERFACE:
3364 parse_interface ();
3365 break;
3367 case ST_DERIVED_DECL:
3368 parse_derived ();
3369 break;
3371 case ST_PUBLIC:
3372 case ST_PRIVATE:
3373 if (gfc_current_state () != COMP_MODULE)
3375 gfc_error ("%s statement must appear in a MODULE",
3376 gfc_ascii_statement (st));
3377 reject_statement ();
3378 break;
3381 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
3383 gfc_error ("%s statement at %C follows another accessibility "
3384 "specification", gfc_ascii_statement (st));
3385 reject_statement ();
3386 break;
3389 gfc_current_ns->default_access = (st == ST_PUBLIC)
3390 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3392 break;
3394 case ST_STATEMENT_FUNCTION:
3395 if (gfc_current_state () == COMP_MODULE
3396 || gfc_current_state () == COMP_SUBMODULE)
3398 unexpected_statement (st);
3399 break;
3402 default:
3403 break;
3406 accept_statement (st);
3407 st = next_statement ();
3408 goto loop;
3410 case ST_ENUM:
3411 accept_statement (st);
3412 parse_enum();
3413 st = next_statement ();
3414 goto loop;
3416 case ST_GET_FCN_CHARACTERISTICS:
3417 /* This statement triggers the association of a function's result
3418 characteristics. */
3419 ts = &gfc_current_block ()->result->ts;
3420 if (match_deferred_characteristics (ts) != MATCH_YES)
3421 bad_characteristic = true;
3423 st = next_statement ();
3424 goto loop;
3426 default:
3427 break;
3430 /* If match_deferred_characteristics failed, then there is an error. */
3431 if (bad_characteristic)
3433 ts = &gfc_current_block ()->result->ts;
3434 if (ts->type != BT_DERIVED)
3435 gfc_error ("Bad kind expression for function %qs at %L",
3436 gfc_current_block ()->name,
3437 &gfc_current_block ()->declared_at);
3438 else
3439 gfc_error ("The type for function %qs at %L is not accessible",
3440 gfc_current_block ()->name,
3441 &gfc_current_block ()->declared_at);
3443 gfc_current_block ()->ts.kind = 0;
3444 /* Keep the derived type; if it's bad, it will be discovered later. */
3445 if (!(ts->type == BT_DERIVED && ts->u.derived))
3446 ts->type = BT_UNKNOWN;
3449 in_specification_block = false;
3451 return st;
3455 /* Parse a WHERE block, (not a simple WHERE statement). */
3457 static void
3458 parse_where_block (void)
3460 int seen_empty_else;
3461 gfc_code *top, *d;
3462 gfc_state_data s;
3463 gfc_statement st;
3465 accept_statement (ST_WHERE_BLOCK);
3466 top = gfc_state_stack->tail;
3468 push_state (&s, COMP_WHERE, gfc_new_block);
3470 d = add_statement ();
3471 d->expr1 = top->expr1;
3472 d->op = EXEC_WHERE;
3474 top->expr1 = NULL;
3475 top->block = d;
3477 seen_empty_else = 0;
3481 st = next_statement ();
3482 switch (st)
3484 case ST_NONE:
3485 unexpected_eof ();
3487 case ST_WHERE_BLOCK:
3488 parse_where_block ();
3489 break;
3491 case ST_ASSIGNMENT:
3492 case ST_WHERE:
3493 accept_statement (st);
3494 break;
3496 case ST_ELSEWHERE:
3497 if (seen_empty_else)
3499 gfc_error ("ELSEWHERE statement at %C follows previous "
3500 "unmasked ELSEWHERE");
3501 reject_statement ();
3502 break;
3505 if (new_st.expr1 == NULL)
3506 seen_empty_else = 1;
3508 d = new_level (gfc_state_stack->head);
3509 d->op = EXEC_WHERE;
3510 d->expr1 = new_st.expr1;
3512 accept_statement (st);
3514 break;
3516 case ST_END_WHERE:
3517 accept_statement (st);
3518 break;
3520 default:
3521 gfc_error ("Unexpected %s statement in WHERE block at %C",
3522 gfc_ascii_statement (st));
3523 reject_statement ();
3524 break;
3527 while (st != ST_END_WHERE);
3529 pop_state ();
3533 /* Parse a FORALL block (not a simple FORALL statement). */
3535 static void
3536 parse_forall_block (void)
3538 gfc_code *top, *d;
3539 gfc_state_data s;
3540 gfc_statement st;
3542 accept_statement (ST_FORALL_BLOCK);
3543 top = gfc_state_stack->tail;
3545 push_state (&s, COMP_FORALL, gfc_new_block);
3547 d = add_statement ();
3548 d->op = EXEC_FORALL;
3549 top->block = d;
3553 st = next_statement ();
3554 switch (st)
3557 case ST_ASSIGNMENT:
3558 case ST_POINTER_ASSIGNMENT:
3559 case ST_WHERE:
3560 case ST_FORALL:
3561 accept_statement (st);
3562 break;
3564 case ST_WHERE_BLOCK:
3565 parse_where_block ();
3566 break;
3568 case ST_FORALL_BLOCK:
3569 parse_forall_block ();
3570 break;
3572 case ST_END_FORALL:
3573 accept_statement (st);
3574 break;
3576 case ST_NONE:
3577 unexpected_eof ();
3579 default:
3580 gfc_error ("Unexpected %s statement in FORALL block at %C",
3581 gfc_ascii_statement (st));
3583 reject_statement ();
3584 break;
3587 while (st != ST_END_FORALL);
3589 pop_state ();
3593 static gfc_statement parse_executable (gfc_statement);
3595 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
3597 static void
3598 parse_if_block (void)
3600 gfc_code *top, *d;
3601 gfc_statement st;
3602 locus else_locus;
3603 gfc_state_data s;
3604 int seen_else;
3606 seen_else = 0;
3607 accept_statement (ST_IF_BLOCK);
3609 top = gfc_state_stack->tail;
3610 push_state (&s, COMP_IF, gfc_new_block);
3612 new_st.op = EXEC_IF;
3613 d = add_statement ();
3615 d->expr1 = top->expr1;
3616 top->expr1 = NULL;
3617 top->block = d;
3621 st = parse_executable (ST_NONE);
3623 switch (st)
3625 case ST_NONE:
3626 unexpected_eof ();
3628 case ST_ELSEIF:
3629 if (seen_else)
3631 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
3632 "statement at %L", &else_locus);
3634 reject_statement ();
3635 break;
3638 d = new_level (gfc_state_stack->head);
3639 d->op = EXEC_IF;
3640 d->expr1 = new_st.expr1;
3642 accept_statement (st);
3644 break;
3646 case ST_ELSE:
3647 if (seen_else)
3649 gfc_error ("Duplicate ELSE statements at %L and %C",
3650 &else_locus);
3651 reject_statement ();
3652 break;
3655 seen_else = 1;
3656 else_locus = gfc_current_locus;
3658 d = new_level (gfc_state_stack->head);
3659 d->op = EXEC_IF;
3661 accept_statement (st);
3663 break;
3665 case ST_ENDIF:
3666 break;
3668 default:
3669 unexpected_statement (st);
3670 break;
3673 while (st != ST_ENDIF);
3675 pop_state ();
3676 accept_statement (st);
3680 /* Parse a SELECT block. */
3682 static void
3683 parse_select_block (void)
3685 gfc_statement st;
3686 gfc_code *cp;
3687 gfc_state_data s;
3689 accept_statement (ST_SELECT_CASE);
3691 cp = gfc_state_stack->tail;
3692 push_state (&s, COMP_SELECT, gfc_new_block);
3694 /* Make sure that the next statement is a CASE or END SELECT. */
3695 for (;;)
3697 st = next_statement ();
3698 if (st == ST_NONE)
3699 unexpected_eof ();
3700 if (st == ST_END_SELECT)
3702 /* Empty SELECT CASE is OK. */
3703 accept_statement (st);
3704 pop_state ();
3705 return;
3707 if (st == ST_CASE)
3708 break;
3710 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
3711 "CASE at %C");
3713 reject_statement ();
3716 /* At this point, we're got a nonempty select block. */
3717 cp = new_level (cp);
3718 *cp = new_st;
3720 accept_statement (st);
3724 st = parse_executable (ST_NONE);
3725 switch (st)
3727 case ST_NONE:
3728 unexpected_eof ();
3730 case ST_CASE:
3731 cp = new_level (gfc_state_stack->head);
3732 *cp = new_st;
3733 gfc_clear_new_st ();
3735 accept_statement (st);
3736 /* Fall through */
3738 case ST_END_SELECT:
3739 break;
3741 /* Can't have an executable statement because of
3742 parse_executable(). */
3743 default:
3744 unexpected_statement (st);
3745 break;
3748 while (st != ST_END_SELECT);
3750 pop_state ();
3751 accept_statement (st);
3755 /* Pop the current selector from the SELECT TYPE stack. */
3757 static void
3758 select_type_pop (void)
3760 gfc_select_type_stack *old = select_type_stack;
3761 select_type_stack = old->prev;
3762 free (old);
3766 /* Parse a SELECT TYPE construct (F03:R821). */
3768 static void
3769 parse_select_type_block (void)
3771 gfc_statement st;
3772 gfc_code *cp;
3773 gfc_state_data s;
3775 accept_statement (ST_SELECT_TYPE);
3777 cp = gfc_state_stack->tail;
3778 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
3780 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
3781 or END SELECT. */
3782 for (;;)
3784 st = next_statement ();
3785 if (st == ST_NONE)
3786 unexpected_eof ();
3787 if (st == ST_END_SELECT)
3788 /* Empty SELECT CASE is OK. */
3789 goto done;
3790 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
3791 break;
3793 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
3794 "following SELECT TYPE at %C");
3796 reject_statement ();
3799 /* At this point, we're got a nonempty select block. */
3800 cp = new_level (cp);
3801 *cp = new_st;
3803 accept_statement (st);
3807 st = parse_executable (ST_NONE);
3808 switch (st)
3810 case ST_NONE:
3811 unexpected_eof ();
3813 case ST_TYPE_IS:
3814 case ST_CLASS_IS:
3815 cp = new_level (gfc_state_stack->head);
3816 *cp = new_st;
3817 gfc_clear_new_st ();
3819 accept_statement (st);
3820 /* Fall through */
3822 case ST_END_SELECT:
3823 break;
3825 /* Can't have an executable statement because of
3826 parse_executable(). */
3827 default:
3828 unexpected_statement (st);
3829 break;
3832 while (st != ST_END_SELECT);
3834 done:
3835 pop_state ();
3836 accept_statement (st);
3837 gfc_current_ns = gfc_current_ns->parent;
3838 select_type_pop ();
3842 /* Given a symbol, make sure it is not an iteration variable for a DO
3843 statement. This subroutine is called when the symbol is seen in a
3844 context that causes it to become redefined. If the symbol is an
3845 iterator, we generate an error message and return nonzero. */
3848 gfc_check_do_variable (gfc_symtree *st)
3850 gfc_state_data *s;
3852 for (s=gfc_state_stack; s; s = s->previous)
3853 if (s->do_variable == st)
3855 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
3856 "loop beginning at %L", st->name, &s->head->loc);
3857 return 1;
3860 return 0;
3864 /* Checks to see if the current statement label closes an enddo.
3865 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3866 an error) if it incorrectly closes an ENDDO. */
3868 static int
3869 check_do_closure (void)
3871 gfc_state_data *p;
3873 if (gfc_statement_label == NULL)
3874 return 0;
3876 for (p = gfc_state_stack; p; p = p->previous)
3877 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
3878 break;
3880 if (p == NULL)
3881 return 0; /* No loops to close */
3883 if (p->ext.end_do_label == gfc_statement_label)
3885 if (p == gfc_state_stack)
3886 return 1;
3888 gfc_error ("End of nonblock DO statement at %C is within another block");
3889 return 2;
3892 /* At this point, the label doesn't terminate the innermost loop.
3893 Make sure it doesn't terminate another one. */
3894 for (; p; p = p->previous)
3895 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
3896 && p->ext.end_do_label == gfc_statement_label)
3898 gfc_error ("End of nonblock DO statement at %C is interwoven "
3899 "with another DO loop");
3900 return 2;
3903 return 0;
3907 /* Parse a series of contained program units. */
3909 static void parse_progunit (gfc_statement);
3912 /* Parse a CRITICAL block. */
3914 static void
3915 parse_critical_block (void)
3917 gfc_code *top, *d;
3918 gfc_state_data s, *sd;
3919 gfc_statement st;
3921 for (sd = gfc_state_stack; sd; sd = sd->previous)
3922 if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
3923 gfc_error_now (is_oacc (sd)
3924 ? "CRITICAL block inside of OpenACC region at %C"
3925 : "CRITICAL block inside of OpenMP region at %C");
3927 s.ext.end_do_label = new_st.label1;
3929 accept_statement (ST_CRITICAL);
3930 top = gfc_state_stack->tail;
3932 push_state (&s, COMP_CRITICAL, gfc_new_block);
3934 d = add_statement ();
3935 d->op = EXEC_CRITICAL;
3936 top->block = d;
3940 st = parse_executable (ST_NONE);
3942 switch (st)
3944 case ST_NONE:
3945 unexpected_eof ();
3946 break;
3948 case ST_END_CRITICAL:
3949 if (s.ext.end_do_label != NULL
3950 && s.ext.end_do_label != gfc_statement_label)
3951 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3952 "match CRITICAL label");
3954 if (gfc_statement_label != NULL)
3956 new_st.op = EXEC_NOP;
3957 add_statement ();
3959 break;
3961 default:
3962 unexpected_statement (st);
3963 break;
3966 while (st != ST_END_CRITICAL);
3968 pop_state ();
3969 accept_statement (st);
3973 /* Set up the local namespace for a BLOCK construct. */
3975 gfc_namespace*
3976 gfc_build_block_ns (gfc_namespace *parent_ns)
3978 gfc_namespace* my_ns;
3979 static int numblock = 1;
3981 my_ns = gfc_get_namespace (parent_ns, 1);
3982 my_ns->construct_entities = 1;
3984 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3985 code generation (so it must not be NULL).
3986 We set its recursive argument if our container procedure is recursive, so
3987 that local variables are accordingly placed on the stack when it
3988 will be necessary. */
3989 if (gfc_new_block)
3990 my_ns->proc_name = gfc_new_block;
3991 else
3993 bool t;
3994 char buffer[20]; /* Enough to hold "block@2147483648\n". */
3996 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
3997 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
3998 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
3999 my_ns->proc_name->name, NULL);
4000 gcc_assert (t);
4001 gfc_commit_symbol (my_ns->proc_name);
4004 if (parent_ns->proc_name)
4005 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
4007 return my_ns;
4011 /* Parse a BLOCK construct. */
4013 static void
4014 parse_block_construct (void)
4016 gfc_namespace* my_ns;
4017 gfc_namespace* my_parent;
4018 gfc_state_data s;
4020 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
4022 my_ns = gfc_build_block_ns (gfc_current_ns);
4024 new_st.op = EXEC_BLOCK;
4025 new_st.ext.block.ns = my_ns;
4026 new_st.ext.block.assoc = NULL;
4027 accept_statement (ST_BLOCK);
4029 push_state (&s, COMP_BLOCK, my_ns->proc_name);
4030 gfc_current_ns = my_ns;
4031 my_parent = my_ns->parent;
4033 parse_progunit (ST_NONE);
4035 /* Don't depend on the value of gfc_current_ns; it might have been
4036 reset if the block had errors and was cleaned up. */
4037 gfc_current_ns = my_parent;
4039 pop_state ();
4043 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
4044 behind the scenes with compiler-generated variables. */
4046 static void
4047 parse_associate (void)
4049 gfc_namespace* my_ns;
4050 gfc_state_data s;
4051 gfc_statement st;
4052 gfc_association_list* a;
4054 gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
4056 my_ns = gfc_build_block_ns (gfc_current_ns);
4058 new_st.op = EXEC_BLOCK;
4059 new_st.ext.block.ns = my_ns;
4060 gcc_assert (new_st.ext.block.assoc);
4062 /* Add all associate-names as BLOCK variables. Creating them is enough
4063 for now, they'll get their values during trans-* phase. */
4064 gfc_current_ns = my_ns;
4065 for (a = new_st.ext.block.assoc; a; a = a->next)
4067 gfc_symbol* sym;
4068 gfc_ref *ref;
4069 gfc_array_ref *array_ref;
4071 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
4072 gcc_unreachable ();
4074 sym = a->st->n.sym;
4075 sym->attr.flavor = FL_VARIABLE;
4076 sym->assoc = a;
4077 sym->declared_at = a->where;
4078 gfc_set_sym_referenced (sym);
4080 /* Initialize the typespec. It is not available in all cases,
4081 however, as it may only be set on the target during resolution.
4082 Still, sometimes it helps to have it right now -- especially
4083 for parsing component references on the associate-name
4084 in case of association to a derived-type. */
4085 sym->ts = a->target->ts;
4087 /* Check if the target expression is array valued. This can not always
4088 be done by looking at target.rank, because that might not have been
4089 set yet. Therefore traverse the chain of refs, looking for the last
4090 array ref and evaluate that. */
4091 array_ref = NULL;
4092 for (ref = a->target->ref; ref; ref = ref->next)
4093 if (ref->type == REF_ARRAY)
4094 array_ref = &ref->u.ar;
4095 if (array_ref || a->target->rank)
4097 gfc_array_spec *as;
4098 int dim, rank = 0;
4099 if (array_ref)
4101 a->rankguessed = 1;
4102 /* Count the dimension, that have a non-scalar extend. */
4103 for (dim = 0; dim < array_ref->dimen; ++dim)
4104 if (array_ref->dimen_type[dim] != DIMEN_ELEMENT
4105 && !(array_ref->dimen_type[dim] == DIMEN_UNKNOWN
4106 && array_ref->end[dim] == NULL
4107 && array_ref->start[dim] != NULL))
4108 ++rank;
4110 else
4111 rank = a->target->rank;
4112 /* When the rank is greater than zero then sym will be an array. */
4113 if (sym->ts.type == BT_CLASS)
4115 if ((!CLASS_DATA (sym)->as && rank != 0)
4116 || (CLASS_DATA (sym)->as
4117 && CLASS_DATA (sym)->as->rank != rank))
4119 /* Don't just (re-)set the attr and as in the sym.ts,
4120 because this modifies the target's attr and as. Copy the
4121 data and do a build_class_symbol. */
4122 symbol_attribute attr = CLASS_DATA (a->target)->attr;
4123 int corank = gfc_get_corank (a->target);
4124 gfc_typespec type;
4126 if (rank || corank)
4128 as = gfc_get_array_spec ();
4129 as->type = AS_DEFERRED;
4130 as->rank = rank;
4131 as->corank = corank;
4132 attr.dimension = rank ? 1 : 0;
4133 attr.codimension = corank ? 1 : 0;
4135 else
4137 as = NULL;
4138 attr.dimension = attr.codimension = 0;
4140 attr.class_ok = 0;
4141 type = CLASS_DATA (sym)->ts;
4142 if (!gfc_build_class_symbol (&type,
4143 &attr, &as))
4144 gcc_unreachable ();
4145 sym->ts = type;
4146 sym->ts.type = BT_CLASS;
4147 sym->attr.class_ok = 1;
4149 else
4150 sym->attr.class_ok = 1;
4152 else if ((!sym->as && rank != 0)
4153 || (sym->as && sym->as->rank != rank))
4155 as = gfc_get_array_spec ();
4156 as->type = AS_DEFERRED;
4157 as->rank = rank;
4158 as->corank = gfc_get_corank (a->target);
4159 sym->as = as;
4160 sym->attr.dimension = 1;
4161 if (as->corank)
4162 sym->attr.codimension = 1;
4167 accept_statement (ST_ASSOCIATE);
4168 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
4170 loop:
4171 st = parse_executable (ST_NONE);
4172 switch (st)
4174 case ST_NONE:
4175 unexpected_eof ();
4177 case_end:
4178 accept_statement (st);
4179 my_ns->code = gfc_state_stack->head;
4180 break;
4182 default:
4183 unexpected_statement (st);
4184 goto loop;
4187 gfc_current_ns = gfc_current_ns->parent;
4188 pop_state ();
4192 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4193 handled inside of parse_executable(), because they aren't really
4194 loop statements. */
4196 static void
4197 parse_do_block (void)
4199 gfc_statement st;
4200 gfc_code *top;
4201 gfc_state_data s;
4202 gfc_symtree *stree;
4203 gfc_exec_op do_op;
4205 do_op = new_st.op;
4206 s.ext.end_do_label = new_st.label1;
4208 if (new_st.ext.iterator != NULL)
4209 stree = new_st.ext.iterator->var->symtree;
4210 else
4211 stree = NULL;
4213 accept_statement (ST_DO);
4215 top = gfc_state_stack->tail;
4216 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
4217 gfc_new_block);
4219 s.do_variable = stree;
4221 top->block = new_level (top);
4222 top->block->op = EXEC_DO;
4224 loop:
4225 st = parse_executable (ST_NONE);
4227 switch (st)
4229 case ST_NONE:
4230 unexpected_eof ();
4232 case ST_ENDDO:
4233 if (s.ext.end_do_label != NULL
4234 && s.ext.end_do_label != gfc_statement_label)
4235 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4236 "DO label");
4238 if (gfc_statement_label != NULL)
4240 new_st.op = EXEC_NOP;
4241 add_statement ();
4243 break;
4245 case ST_IMPLIED_ENDDO:
4246 /* If the do-stmt of this DO construct has a do-construct-name,
4247 the corresponding end-do must be an end-do-stmt (with a matching
4248 name, but in that case we must have seen ST_ENDDO first).
4249 We only complain about this in pedantic mode. */
4250 if (gfc_current_block () != NULL)
4251 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4252 &gfc_current_block()->declared_at);
4254 break;
4256 default:
4257 unexpected_statement (st);
4258 goto loop;
4261 pop_state ();
4262 accept_statement (st);
4266 /* Parse the statements of OpenMP do/parallel do. */
4268 static gfc_statement
4269 parse_omp_do (gfc_statement omp_st)
4271 gfc_statement st;
4272 gfc_code *cp, *np;
4273 gfc_state_data s;
4275 accept_statement (omp_st);
4277 cp = gfc_state_stack->tail;
4278 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4279 np = new_level (cp);
4280 np->op = cp->op;
4281 np->block = NULL;
4283 for (;;)
4285 st = next_statement ();
4286 if (st == ST_NONE)
4287 unexpected_eof ();
4288 else if (st == ST_DO)
4289 break;
4290 else
4291 unexpected_statement (st);
4294 parse_do_block ();
4295 if (gfc_statement_label != NULL
4296 && gfc_state_stack->previous != NULL
4297 && gfc_state_stack->previous->state == COMP_DO
4298 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4300 /* In
4301 DO 100 I=1,10
4302 !$OMP DO
4303 DO J=1,10
4305 100 CONTINUE
4306 there should be no !$OMP END DO. */
4307 pop_state ();
4308 return ST_IMPLIED_ENDDO;
4311 check_do_closure ();
4312 pop_state ();
4314 st = next_statement ();
4315 gfc_statement omp_end_st = ST_OMP_END_DO;
4316 switch (omp_st)
4318 case ST_OMP_DISTRIBUTE: omp_end_st = ST_OMP_END_DISTRIBUTE; break;
4319 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4320 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4321 break;
4322 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4323 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4324 break;
4325 case ST_OMP_DISTRIBUTE_SIMD:
4326 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4327 break;
4328 case ST_OMP_DO: omp_end_st = ST_OMP_END_DO; break;
4329 case ST_OMP_DO_SIMD: omp_end_st = ST_OMP_END_DO_SIMD; break;
4330 case ST_OMP_PARALLEL_DO: omp_end_st = ST_OMP_END_PARALLEL_DO; break;
4331 case ST_OMP_PARALLEL_DO_SIMD:
4332 omp_end_st = ST_OMP_END_PARALLEL_DO_SIMD;
4333 break;
4334 case ST_OMP_SIMD: omp_end_st = ST_OMP_END_SIMD; break;
4335 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4336 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4337 break;
4338 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4339 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4340 break;
4341 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4342 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4343 break;
4344 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4345 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4346 break;
4347 case ST_OMP_TEAMS_DISTRIBUTE:
4348 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4349 break;
4350 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4351 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4352 break;
4353 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4354 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4355 break;
4356 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4357 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4358 break;
4359 default: gcc_unreachable ();
4361 if (st == omp_end_st)
4363 if (new_st.op == EXEC_OMP_END_NOWAIT)
4364 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4365 else
4366 gcc_assert (new_st.op == EXEC_NOP);
4367 gfc_clear_new_st ();
4368 gfc_commit_symbols ();
4369 gfc_warning_check ();
4370 st = next_statement ();
4372 return st;
4376 /* Parse the statements of OpenMP atomic directive. */
4378 static gfc_statement
4379 parse_omp_oacc_atomic (bool omp_p)
4381 gfc_statement st, st_atomic, st_end_atomic;
4382 gfc_code *cp, *np;
4383 gfc_state_data s;
4384 int count;
4386 if (omp_p)
4388 st_atomic = ST_OMP_ATOMIC;
4389 st_end_atomic = ST_OMP_END_ATOMIC;
4391 else
4393 st_atomic = ST_OACC_ATOMIC;
4394 st_end_atomic = ST_OACC_END_ATOMIC;
4396 accept_statement (st_atomic);
4398 cp = gfc_state_stack->tail;
4399 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4400 np = new_level (cp);
4401 np->op = cp->op;
4402 np->block = NULL;
4403 count = 1 + ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4404 == GFC_OMP_ATOMIC_CAPTURE);
4406 while (count)
4408 st = next_statement ();
4409 if (st == ST_NONE)
4410 unexpected_eof ();
4411 else if (st == ST_ASSIGNMENT)
4413 accept_statement (st);
4414 count--;
4416 else
4417 unexpected_statement (st);
4420 pop_state ();
4422 st = next_statement ();
4423 if (st == st_end_atomic)
4425 gfc_clear_new_st ();
4426 gfc_commit_symbols ();
4427 gfc_warning_check ();
4428 st = next_statement ();
4430 else if ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4431 == GFC_OMP_ATOMIC_CAPTURE)
4432 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4433 return st;
4437 /* Parse the statements of an OpenACC structured block. */
4439 static void
4440 parse_oacc_structured_block (gfc_statement acc_st)
4442 gfc_statement st, acc_end_st;
4443 gfc_code *cp, *np;
4444 gfc_state_data s, *sd;
4446 for (sd = gfc_state_stack; sd; sd = sd->previous)
4447 if (sd->state == COMP_CRITICAL)
4448 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4450 accept_statement (acc_st);
4452 cp = gfc_state_stack->tail;
4453 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4454 np = new_level (cp);
4455 np->op = cp->op;
4456 np->block = NULL;
4457 switch (acc_st)
4459 case ST_OACC_PARALLEL:
4460 acc_end_st = ST_OACC_END_PARALLEL;
4461 break;
4462 case ST_OACC_KERNELS:
4463 acc_end_st = ST_OACC_END_KERNELS;
4464 break;
4465 case ST_OACC_DATA:
4466 acc_end_st = ST_OACC_END_DATA;
4467 break;
4468 case ST_OACC_HOST_DATA:
4469 acc_end_st = ST_OACC_END_HOST_DATA;
4470 break;
4471 default:
4472 gcc_unreachable ();
4477 st = parse_executable (ST_NONE);
4478 if (st == ST_NONE)
4479 unexpected_eof ();
4480 else if (st != acc_end_st)
4482 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
4483 reject_statement ();
4486 while (st != acc_end_st);
4488 gcc_assert (new_st.op == EXEC_NOP);
4490 gfc_clear_new_st ();
4491 gfc_commit_symbols ();
4492 gfc_warning_check ();
4493 pop_state ();
4496 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4498 static gfc_statement
4499 parse_oacc_loop (gfc_statement acc_st)
4501 gfc_statement st;
4502 gfc_code *cp, *np;
4503 gfc_state_data s, *sd;
4505 for (sd = gfc_state_stack; sd; sd = sd->previous)
4506 if (sd->state == COMP_CRITICAL)
4507 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4509 accept_statement (acc_st);
4511 cp = gfc_state_stack->tail;
4512 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4513 np = new_level (cp);
4514 np->op = cp->op;
4515 np->block = NULL;
4517 for (;;)
4519 st = next_statement ();
4520 if (st == ST_NONE)
4521 unexpected_eof ();
4522 else if (st == ST_DO)
4523 break;
4524 else
4526 gfc_error ("Expected DO loop at %C");
4527 reject_statement ();
4531 parse_do_block ();
4532 if (gfc_statement_label != NULL
4533 && gfc_state_stack->previous != NULL
4534 && gfc_state_stack->previous->state == COMP_DO
4535 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4537 pop_state ();
4538 return ST_IMPLIED_ENDDO;
4541 check_do_closure ();
4542 pop_state ();
4544 st = next_statement ();
4545 if (st == ST_OACC_END_LOOP)
4546 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
4547 if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
4548 (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
4549 (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
4551 gcc_assert (new_st.op == EXEC_NOP);
4552 gfc_clear_new_st ();
4553 gfc_commit_symbols ();
4554 gfc_warning_check ();
4555 st = next_statement ();
4557 return st;
4561 /* Parse the statements of an OpenMP structured block. */
4563 static void
4564 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
4566 gfc_statement st, omp_end_st;
4567 gfc_code *cp, *np;
4568 gfc_state_data s;
4570 accept_statement (omp_st);
4572 cp = gfc_state_stack->tail;
4573 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4574 np = new_level (cp);
4575 np->op = cp->op;
4576 np->block = NULL;
4578 switch (omp_st)
4580 case ST_OMP_PARALLEL:
4581 omp_end_st = ST_OMP_END_PARALLEL;
4582 break;
4583 case ST_OMP_PARALLEL_SECTIONS:
4584 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
4585 break;
4586 case ST_OMP_SECTIONS:
4587 omp_end_st = ST_OMP_END_SECTIONS;
4588 break;
4589 case ST_OMP_ORDERED:
4590 omp_end_st = ST_OMP_END_ORDERED;
4591 break;
4592 case ST_OMP_CRITICAL:
4593 omp_end_st = ST_OMP_END_CRITICAL;
4594 break;
4595 case ST_OMP_MASTER:
4596 omp_end_st = ST_OMP_END_MASTER;
4597 break;
4598 case ST_OMP_SINGLE:
4599 omp_end_st = ST_OMP_END_SINGLE;
4600 break;
4601 case ST_OMP_TARGET:
4602 omp_end_st = ST_OMP_END_TARGET;
4603 break;
4604 case ST_OMP_TARGET_DATA:
4605 omp_end_st = ST_OMP_END_TARGET_DATA;
4606 break;
4607 case ST_OMP_TARGET_TEAMS:
4608 omp_end_st = ST_OMP_END_TARGET_TEAMS;
4609 break;
4610 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4611 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4612 break;
4613 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4614 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4615 break;
4616 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4617 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4618 break;
4619 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4620 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4621 break;
4622 case ST_OMP_TASK:
4623 omp_end_st = ST_OMP_END_TASK;
4624 break;
4625 case ST_OMP_TASKGROUP:
4626 omp_end_st = ST_OMP_END_TASKGROUP;
4627 break;
4628 case ST_OMP_TEAMS:
4629 omp_end_st = ST_OMP_END_TEAMS;
4630 break;
4631 case ST_OMP_TEAMS_DISTRIBUTE:
4632 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4633 break;
4634 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4635 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4636 break;
4637 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4638 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4639 break;
4640 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4641 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4642 break;
4643 case ST_OMP_DISTRIBUTE:
4644 omp_end_st = ST_OMP_END_DISTRIBUTE;
4645 break;
4646 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4647 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4648 break;
4649 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4650 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4651 break;
4652 case ST_OMP_DISTRIBUTE_SIMD:
4653 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4654 break;
4655 case ST_OMP_WORKSHARE:
4656 omp_end_st = ST_OMP_END_WORKSHARE;
4657 break;
4658 case ST_OMP_PARALLEL_WORKSHARE:
4659 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
4660 break;
4661 default:
4662 gcc_unreachable ();
4667 if (workshare_stmts_only)
4669 /* Inside of !$omp workshare, only
4670 scalar assignments
4671 array assignments
4672 where statements and constructs
4673 forall statements and constructs
4674 !$omp atomic
4675 !$omp critical
4676 !$omp parallel
4677 are allowed. For !$omp critical these
4678 restrictions apply recursively. */
4679 bool cycle = true;
4681 st = next_statement ();
4682 for (;;)
4684 switch (st)
4686 case ST_NONE:
4687 unexpected_eof ();
4689 case ST_ASSIGNMENT:
4690 case ST_WHERE:
4691 case ST_FORALL:
4692 accept_statement (st);
4693 break;
4695 case ST_WHERE_BLOCK:
4696 parse_where_block ();
4697 break;
4699 case ST_FORALL_BLOCK:
4700 parse_forall_block ();
4701 break;
4703 case ST_OMP_PARALLEL:
4704 case ST_OMP_PARALLEL_SECTIONS:
4705 parse_omp_structured_block (st, false);
4706 break;
4708 case ST_OMP_PARALLEL_WORKSHARE:
4709 case ST_OMP_CRITICAL:
4710 parse_omp_structured_block (st, true);
4711 break;
4713 case ST_OMP_PARALLEL_DO:
4714 case ST_OMP_PARALLEL_DO_SIMD:
4715 st = parse_omp_do (st);
4716 continue;
4718 case ST_OMP_ATOMIC:
4719 st = parse_omp_oacc_atomic (true);
4720 continue;
4722 default:
4723 cycle = false;
4724 break;
4727 if (!cycle)
4728 break;
4730 st = next_statement ();
4733 else
4734 st = parse_executable (ST_NONE);
4735 if (st == ST_NONE)
4736 unexpected_eof ();
4737 else if (st == ST_OMP_SECTION
4738 && (omp_st == ST_OMP_SECTIONS
4739 || omp_st == ST_OMP_PARALLEL_SECTIONS))
4741 np = new_level (np);
4742 np->op = cp->op;
4743 np->block = NULL;
4745 else if (st != omp_end_st)
4746 unexpected_statement (st);
4748 while (st != omp_end_st);
4750 switch (new_st.op)
4752 case EXEC_OMP_END_NOWAIT:
4753 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4754 break;
4755 case EXEC_OMP_CRITICAL:
4756 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
4757 || (new_st.ext.omp_name != NULL
4758 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
4759 gfc_error ("Name after !$omp critical and !$omp end critical does "
4760 "not match at %C");
4761 free (CONST_CAST (char *, new_st.ext.omp_name));
4762 break;
4763 case EXEC_OMP_END_SINGLE:
4764 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
4765 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
4766 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
4767 gfc_free_omp_clauses (new_st.ext.omp_clauses);
4768 break;
4769 case EXEC_NOP:
4770 break;
4771 default:
4772 gcc_unreachable ();
4775 gfc_clear_new_st ();
4776 gfc_commit_symbols ();
4777 gfc_warning_check ();
4778 pop_state ();
4782 /* Accept a series of executable statements. We return the first
4783 statement that doesn't fit to the caller. Any block statements are
4784 passed on to the correct handler, which usually passes the buck
4785 right back here. */
4787 static gfc_statement
4788 parse_executable (gfc_statement st)
4790 int close_flag;
4792 if (st == ST_NONE)
4793 st = next_statement ();
4795 for (;;)
4797 close_flag = check_do_closure ();
4798 if (close_flag)
4799 switch (st)
4801 case ST_GOTO:
4802 case ST_END_PROGRAM:
4803 case ST_RETURN:
4804 case ST_EXIT:
4805 case ST_END_FUNCTION:
4806 case ST_CYCLE:
4807 case ST_PAUSE:
4808 case ST_STOP:
4809 case ST_ERROR_STOP:
4810 case ST_END_SUBROUTINE:
4812 case ST_DO:
4813 case ST_FORALL:
4814 case ST_WHERE:
4815 case ST_SELECT_CASE:
4816 gfc_error ("%s statement at %C cannot terminate a non-block "
4817 "DO loop", gfc_ascii_statement (st));
4818 break;
4820 default:
4821 break;
4824 switch (st)
4826 case ST_NONE:
4827 unexpected_eof ();
4829 case ST_DATA:
4830 gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
4831 "first executable statement");
4832 /* Fall through. */
4834 case ST_FORMAT:
4835 case ST_ENTRY:
4836 case_executable:
4837 accept_statement (st);
4838 if (close_flag == 1)
4839 return ST_IMPLIED_ENDDO;
4840 break;
4842 case ST_BLOCK:
4843 parse_block_construct ();
4844 break;
4846 case ST_ASSOCIATE:
4847 parse_associate ();
4848 break;
4850 case ST_IF_BLOCK:
4851 parse_if_block ();
4852 break;
4854 case ST_SELECT_CASE:
4855 parse_select_block ();
4856 break;
4858 case ST_SELECT_TYPE:
4859 parse_select_type_block();
4860 break;
4862 case ST_DO:
4863 parse_do_block ();
4864 if (check_do_closure () == 1)
4865 return ST_IMPLIED_ENDDO;
4866 break;
4868 case ST_CRITICAL:
4869 parse_critical_block ();
4870 break;
4872 case ST_WHERE_BLOCK:
4873 parse_where_block ();
4874 break;
4876 case ST_FORALL_BLOCK:
4877 parse_forall_block ();
4878 break;
4880 case ST_OACC_PARALLEL_LOOP:
4881 case ST_OACC_KERNELS_LOOP:
4882 case ST_OACC_LOOP:
4883 st = parse_oacc_loop (st);
4884 if (st == ST_IMPLIED_ENDDO)
4885 return st;
4886 continue;
4888 case ST_OACC_PARALLEL:
4889 case ST_OACC_KERNELS:
4890 case ST_OACC_DATA:
4891 case ST_OACC_HOST_DATA:
4892 parse_oacc_structured_block (st);
4893 break;
4895 case ST_OMP_PARALLEL:
4896 case ST_OMP_PARALLEL_SECTIONS:
4897 case ST_OMP_SECTIONS:
4898 case ST_OMP_ORDERED:
4899 case ST_OMP_CRITICAL:
4900 case ST_OMP_MASTER:
4901 case ST_OMP_SINGLE:
4902 case ST_OMP_TARGET:
4903 case ST_OMP_TARGET_DATA:
4904 case ST_OMP_TARGET_TEAMS:
4905 case ST_OMP_TEAMS:
4906 case ST_OMP_TASK:
4907 case ST_OMP_TASKGROUP:
4908 parse_omp_structured_block (st, false);
4909 break;
4911 case ST_OMP_WORKSHARE:
4912 case ST_OMP_PARALLEL_WORKSHARE:
4913 parse_omp_structured_block (st, true);
4914 break;
4916 case ST_OMP_DISTRIBUTE:
4917 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4918 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4919 case ST_OMP_DISTRIBUTE_SIMD:
4920 case ST_OMP_DO:
4921 case ST_OMP_DO_SIMD:
4922 case ST_OMP_PARALLEL_DO:
4923 case ST_OMP_PARALLEL_DO_SIMD:
4924 case ST_OMP_SIMD:
4925 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4926 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4927 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4928 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4929 case ST_OMP_TEAMS_DISTRIBUTE:
4930 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4931 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4932 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4933 st = parse_omp_do (st);
4934 if (st == ST_IMPLIED_ENDDO)
4935 return st;
4936 continue;
4938 case ST_OACC_ATOMIC:
4939 st = parse_omp_oacc_atomic (false);
4940 continue;
4942 case ST_OMP_ATOMIC:
4943 st = parse_omp_oacc_atomic (true);
4944 continue;
4946 default:
4947 return st;
4950 st = next_statement ();
4955 /* Fix the symbols for sibling functions. These are incorrectly added to
4956 the child namespace as the parser didn't know about this procedure. */
4958 static void
4959 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
4961 gfc_namespace *ns;
4962 gfc_symtree *st;
4963 gfc_symbol *old_sym;
4965 for (ns = siblings; ns; ns = ns->sibling)
4967 st = gfc_find_symtree (ns->sym_root, sym->name);
4969 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
4970 goto fixup_contained;
4972 if ((st->n.sym->attr.flavor == FL_DERIVED
4973 && sym->attr.generic && sym->attr.function)
4974 ||(sym->attr.flavor == FL_DERIVED
4975 && st->n.sym->attr.generic && st->n.sym->attr.function))
4976 goto fixup_contained;
4978 old_sym = st->n.sym;
4979 if (old_sym->ns == ns
4980 && !old_sym->attr.contained
4982 /* By 14.6.1.3, host association should be excluded
4983 for the following. */
4984 && !(old_sym->attr.external
4985 || (old_sym->ts.type != BT_UNKNOWN
4986 && !old_sym->attr.implicit_type)
4987 || old_sym->attr.flavor == FL_PARAMETER
4988 || old_sym->attr.use_assoc
4989 || old_sym->attr.in_common
4990 || old_sym->attr.in_equivalence
4991 || old_sym->attr.data
4992 || old_sym->attr.dummy
4993 || old_sym->attr.result
4994 || old_sym->attr.dimension
4995 || old_sym->attr.allocatable
4996 || old_sym->attr.intrinsic
4997 || old_sym->attr.generic
4998 || old_sym->attr.flavor == FL_NAMELIST
4999 || old_sym->attr.flavor == FL_LABEL
5000 || old_sym->attr.proc == PROC_ST_FUNCTION))
5002 /* Replace it with the symbol from the parent namespace. */
5003 st->n.sym = sym;
5004 sym->refs++;
5006 gfc_release_symbol (old_sym);
5009 fixup_contained:
5010 /* Do the same for any contained procedures. */
5011 gfc_fixup_sibling_symbols (sym, ns->contained);
5015 static void
5016 parse_contained (int module)
5018 gfc_namespace *ns, *parent_ns, *tmp;
5019 gfc_state_data s1, s2;
5020 gfc_statement st;
5021 gfc_symbol *sym;
5022 gfc_entry_list *el;
5023 int contains_statements = 0;
5024 int seen_error = 0;
5026 push_state (&s1, COMP_CONTAINS, NULL);
5027 parent_ns = gfc_current_ns;
5031 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
5033 gfc_current_ns->sibling = parent_ns->contained;
5034 parent_ns->contained = gfc_current_ns;
5036 next:
5037 /* Process the next available statement. We come here if we got an error
5038 and rejected the last statement. */
5039 st = next_statement ();
5041 switch (st)
5043 case ST_NONE:
5044 unexpected_eof ();
5046 case ST_FUNCTION:
5047 case ST_SUBROUTINE:
5048 contains_statements = 1;
5049 accept_statement (st);
5051 push_state (&s2,
5052 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
5053 gfc_new_block);
5055 /* For internal procedures, create/update the symbol in the
5056 parent namespace. */
5058 if (!module)
5060 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
5061 gfc_error ("Contained procedure %qs at %C is already "
5062 "ambiguous", gfc_new_block->name);
5063 else
5065 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
5066 sym->name,
5067 &gfc_new_block->declared_at))
5069 if (st == ST_FUNCTION)
5070 gfc_add_function (&sym->attr, sym->name,
5071 &gfc_new_block->declared_at);
5072 else
5073 gfc_add_subroutine (&sym->attr, sym->name,
5074 &gfc_new_block->declared_at);
5078 gfc_commit_symbols ();
5080 else
5081 sym = gfc_new_block;
5083 /* Mark this as a contained function, so it isn't replaced
5084 by other module functions. */
5085 sym->attr.contained = 1;
5087 /* Set implicit_pure so that it can be reset if any of the
5088 tests for purity fail. This is used for some optimisation
5089 during translation. */
5090 if (!sym->attr.pure)
5091 sym->attr.implicit_pure = 1;
5093 parse_progunit (ST_NONE);
5095 /* Fix up any sibling functions that refer to this one. */
5096 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
5097 /* Or refer to any of its alternate entry points. */
5098 for (el = gfc_current_ns->entries; el; el = el->next)
5099 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
5101 gfc_current_ns->code = s2.head;
5102 gfc_current_ns = parent_ns;
5104 pop_state ();
5105 break;
5107 /* These statements are associated with the end of the host unit. */
5108 case ST_END_FUNCTION:
5109 case ST_END_MODULE:
5110 case ST_END_SUBMODULE:
5111 case ST_END_PROGRAM:
5112 case ST_END_SUBROUTINE:
5113 accept_statement (st);
5114 gfc_current_ns->code = s1.head;
5115 break;
5117 default:
5118 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
5119 gfc_ascii_statement (st));
5120 reject_statement ();
5121 seen_error = 1;
5122 goto next;
5123 break;
5126 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
5127 && st != ST_END_MODULE && st != ST_END_SUBMODULE
5128 && st != ST_END_PROGRAM);
5130 /* The first namespace in the list is guaranteed to not have
5131 anything (worthwhile) in it. */
5132 tmp = gfc_current_ns;
5133 gfc_current_ns = parent_ns;
5134 if (seen_error && tmp->refs > 1)
5135 gfc_free_namespace (tmp);
5137 ns = gfc_current_ns->contained;
5138 gfc_current_ns->contained = ns->sibling;
5139 gfc_free_namespace (ns);
5141 pop_state ();
5142 if (!contains_statements)
5143 gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
5144 "FUNCTION or SUBROUTINE statement at %C");
5148 /* The result variable in a MODULE PROCEDURE needs to be created and
5149 its characteristics copied from the interface since it is neither
5150 declared in the procedure declaration nor in the specification
5151 part. */
5153 static void
5154 get_modproc_result (void)
5156 gfc_symbol *proc;
5157 if (gfc_state_stack->previous
5158 && gfc_state_stack->previous->state == COMP_CONTAINS
5159 && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
5161 proc = gfc_current_ns->proc_name ? gfc_current_ns->proc_name : NULL;
5162 if (proc != NULL
5163 && proc->attr.function
5164 && proc->ts.interface
5165 && proc->ts.interface->result
5166 && proc->ts.interface->result != proc->ts.interface)
5168 gfc_copy_dummy_sym (&proc->result, proc->ts.interface->result, 1);
5169 gfc_set_sym_referenced (proc->result);
5170 proc->result->attr.if_source = IFSRC_DECL;
5171 gfc_commit_symbol (proc->result);
5177 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
5179 static void
5180 parse_progunit (gfc_statement st)
5182 gfc_state_data *p;
5183 int n;
5185 if (gfc_new_block
5186 && gfc_new_block->abr_modproc_decl
5187 && gfc_new_block->attr.function)
5188 get_modproc_result ();
5190 st = parse_spec (st);
5191 switch (st)
5193 case ST_NONE:
5194 unexpected_eof ();
5196 case ST_CONTAINS:
5197 /* This is not allowed within BLOCK! */
5198 if (gfc_current_state () != COMP_BLOCK)
5199 goto contains;
5200 break;
5202 case_end:
5203 accept_statement (st);
5204 goto done;
5206 default:
5207 break;
5210 if (gfc_current_state () == COMP_FUNCTION)
5211 gfc_check_function_type (gfc_current_ns);
5213 loop:
5214 for (;;)
5216 st = parse_executable (st);
5218 switch (st)
5220 case ST_NONE:
5221 unexpected_eof ();
5223 case ST_CONTAINS:
5224 /* This is not allowed within BLOCK! */
5225 if (gfc_current_state () != COMP_BLOCK)
5226 goto contains;
5227 break;
5229 case_end:
5230 accept_statement (st);
5231 goto done;
5233 default:
5234 break;
5237 unexpected_statement (st);
5238 reject_statement ();
5239 st = next_statement ();
5242 contains:
5243 n = 0;
5245 for (p = gfc_state_stack; p; p = p->previous)
5246 if (p->state == COMP_CONTAINS)
5247 n++;
5249 if (gfc_find_state (COMP_MODULE) == true
5250 || gfc_find_state (COMP_SUBMODULE) == true)
5251 n--;
5253 if (n > 0)
5255 gfc_error ("CONTAINS statement at %C is already in a contained "
5256 "program unit");
5257 reject_statement ();
5258 st = next_statement ();
5259 goto loop;
5262 parse_contained (0);
5264 done:
5265 gfc_current_ns->code = gfc_state_stack->head;
5269 /* Come here to complain about a global symbol already in use as
5270 something else. */
5272 void
5273 gfc_global_used (gfc_gsymbol *sym, locus *where)
5275 const char *name;
5277 if (where == NULL)
5278 where = &gfc_current_locus;
5280 switch(sym->type)
5282 case GSYM_PROGRAM:
5283 name = "PROGRAM";
5284 break;
5285 case GSYM_FUNCTION:
5286 name = "FUNCTION";
5287 break;
5288 case GSYM_SUBROUTINE:
5289 name = "SUBROUTINE";
5290 break;
5291 case GSYM_COMMON:
5292 name = "COMMON";
5293 break;
5294 case GSYM_BLOCK_DATA:
5295 name = "BLOCK DATA";
5296 break;
5297 case GSYM_MODULE:
5298 name = "MODULE";
5299 break;
5300 default:
5301 gfc_internal_error ("gfc_global_used(): Bad type");
5302 name = NULL;
5305 if (sym->binding_label)
5306 gfc_error ("Global binding name %qs at %L is already being used as a %s "
5307 "at %L", sym->binding_label, where, name, &sym->where);
5308 else
5309 gfc_error ("Global name %qs at %L is already being used as a %s at %L",
5310 sym->name, where, name, &sym->where);
5314 /* Parse a block data program unit. */
5316 static void
5317 parse_block_data (void)
5319 gfc_statement st;
5320 static locus blank_locus;
5321 static int blank_block=0;
5322 gfc_gsymbol *s;
5324 gfc_current_ns->proc_name = gfc_new_block;
5325 gfc_current_ns->is_block_data = 1;
5327 if (gfc_new_block == NULL)
5329 if (blank_block)
5330 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5331 "prior BLOCK DATA at %L", &blank_locus);
5332 else
5334 blank_block = 1;
5335 blank_locus = gfc_current_locus;
5338 else
5340 s = gfc_get_gsymbol (gfc_new_block->name);
5341 if (s->defined
5342 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
5343 gfc_global_used (s, &gfc_new_block->declared_at);
5344 else
5346 s->type = GSYM_BLOCK_DATA;
5347 s->where = gfc_new_block->declared_at;
5348 s->defined = 1;
5352 st = parse_spec (ST_NONE);
5354 while (st != ST_END_BLOCK_DATA)
5356 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5357 gfc_ascii_statement (st));
5358 reject_statement ();
5359 st = next_statement ();
5364 /* Following the association of the ancestor (sub)module symbols, they
5365 must be set host rather than use associated and all must be public.
5366 They are flagged up by 'used_in_submodule' so that they can be set
5367 DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl). Otherwise the
5368 linker chokes on multiple symbol definitions. */
5370 static void
5371 set_syms_host_assoc (gfc_symbol *sym)
5373 gfc_component *c;
5375 if (sym == NULL)
5376 return;
5378 if (sym->attr.module_procedure)
5379 sym->attr.external = 0;
5381 /* sym->attr.access = ACCESS_PUBLIC; */
5383 sym->attr.use_assoc = 0;
5384 sym->attr.host_assoc = 1;
5385 sym->attr.used_in_submodule =1;
5387 if (sym->attr.flavor == FL_DERIVED)
5389 for (c = sym->components; c; c = c->next)
5390 c->attr.access = ACCESS_PUBLIC;
5394 /* Parse a module subprogram. */
5396 static void
5397 parse_module (void)
5399 gfc_statement st;
5400 gfc_gsymbol *s;
5401 bool error;
5403 s = gfc_get_gsymbol (gfc_new_block->name);
5404 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
5405 gfc_global_used (s, &gfc_new_block->declared_at);
5406 else
5408 s->type = GSYM_MODULE;
5409 s->where = gfc_new_block->declared_at;
5410 s->defined = 1;
5413 /* Something is nulling the module_list after this point. This is good
5414 since it allows us to 'USE' the parent modules that the submodule
5415 inherits and to set (most) of the symbols as host associated. */
5416 if (gfc_current_state () == COMP_SUBMODULE)
5418 use_modules ();
5419 gfc_traverse_ns (gfc_current_ns, set_syms_host_assoc);
5422 st = parse_spec (ST_NONE);
5424 error = false;
5425 loop:
5426 switch (st)
5428 case ST_NONE:
5429 unexpected_eof ();
5431 case ST_CONTAINS:
5432 parse_contained (1);
5433 break;
5435 case ST_END_MODULE:
5436 case ST_END_SUBMODULE:
5437 accept_statement (st);
5438 break;
5440 default:
5441 gfc_error ("Unexpected %s statement in MODULE at %C",
5442 gfc_ascii_statement (st));
5444 error = true;
5445 reject_statement ();
5446 st = next_statement ();
5447 goto loop;
5450 /* Make sure not to free the namespace twice on error. */
5451 if (!error)
5452 s->ns = gfc_current_ns;
5456 /* Add a procedure name to the global symbol table. */
5458 static void
5459 add_global_procedure (bool sub)
5461 gfc_gsymbol *s;
5463 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5464 name is a global identifier. */
5465 if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
5467 s = gfc_get_gsymbol (gfc_new_block->name);
5469 if (s->defined
5470 || (s->type != GSYM_UNKNOWN
5471 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5473 gfc_global_used (s, &gfc_new_block->declared_at);
5474 /* Silence follow-up errors. */
5475 gfc_new_block->binding_label = NULL;
5477 else
5479 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5480 s->sym_name = gfc_new_block->name;
5481 s->where = gfc_new_block->declared_at;
5482 s->defined = 1;
5483 s->ns = gfc_current_ns;
5487 /* Don't add the symbol multiple times. */
5488 if (gfc_new_block->binding_label
5489 && (!gfc_notification_std (GFC_STD_F2008)
5490 || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
5492 s = gfc_get_gsymbol (gfc_new_block->binding_label);
5494 if (s->defined
5495 || (s->type != GSYM_UNKNOWN
5496 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5498 gfc_global_used (s, &gfc_new_block->declared_at);
5499 /* Silence follow-up errors. */
5500 gfc_new_block->binding_label = NULL;
5502 else
5504 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5505 s->sym_name = gfc_new_block->name;
5506 s->binding_label = gfc_new_block->binding_label;
5507 s->where = gfc_new_block->declared_at;
5508 s->defined = 1;
5509 s->ns = gfc_current_ns;
5515 /* Add a program to the global symbol table. */
5517 static void
5518 add_global_program (void)
5520 gfc_gsymbol *s;
5522 if (gfc_new_block == NULL)
5523 return;
5524 s = gfc_get_gsymbol (gfc_new_block->name);
5526 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
5527 gfc_global_used (s, &gfc_new_block->declared_at);
5528 else
5530 s->type = GSYM_PROGRAM;
5531 s->where = gfc_new_block->declared_at;
5532 s->defined = 1;
5533 s->ns = gfc_current_ns;
5538 /* Resolve all the program units. */
5539 static void
5540 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
5542 gfc_free_dt_list ();
5543 gfc_current_ns = gfc_global_ns_list;
5544 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5546 if (gfc_current_ns->proc_name
5547 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5548 continue; /* Already resolved. */
5550 if (gfc_current_ns->proc_name)
5551 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5552 gfc_resolve (gfc_current_ns);
5553 gfc_current_ns->derived_types = gfc_derived_types;
5554 gfc_derived_types = NULL;
5559 static void
5560 clean_up_modules (gfc_gsymbol *gsym)
5562 if (gsym == NULL)
5563 return;
5565 clean_up_modules (gsym->left);
5566 clean_up_modules (gsym->right);
5568 if (gsym->type != GSYM_MODULE || !gsym->ns)
5569 return;
5571 gfc_current_ns = gsym->ns;
5572 gfc_derived_types = gfc_current_ns->derived_types;
5573 gfc_done_2 ();
5574 gsym->ns = NULL;
5575 return;
5579 /* Translate all the program units. This could be in a different order
5580 to resolution if there are forward references in the file. */
5581 static void
5582 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
5584 int errors;
5586 gfc_current_ns = gfc_global_ns_list;
5587 gfc_get_errors (NULL, &errors);
5589 /* We first translate all modules to make sure that later parts
5590 of the program can use the decl. Then we translate the nonmodules. */
5592 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5594 if (!gfc_current_ns->proc_name
5595 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5596 continue;
5598 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5599 gfc_derived_types = gfc_current_ns->derived_types;
5600 gfc_generate_module_code (gfc_current_ns);
5601 gfc_current_ns->translated = 1;
5604 gfc_current_ns = gfc_global_ns_list;
5605 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5607 if (gfc_current_ns->proc_name
5608 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5609 continue;
5611 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5612 gfc_derived_types = gfc_current_ns->derived_types;
5613 gfc_generate_code (gfc_current_ns);
5614 gfc_current_ns->translated = 1;
5617 /* Clean up all the namespaces after translation. */
5618 gfc_current_ns = gfc_global_ns_list;
5619 for (;gfc_current_ns;)
5621 gfc_namespace *ns;
5623 if (gfc_current_ns->proc_name
5624 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5626 gfc_current_ns = gfc_current_ns->sibling;
5627 continue;
5630 ns = gfc_current_ns->sibling;
5631 gfc_derived_types = gfc_current_ns->derived_types;
5632 gfc_done_2 ();
5633 gfc_current_ns = ns;
5636 clean_up_modules (gfc_gsym_root);
5640 /* Top level parser. */
5642 bool
5643 gfc_parse_file (void)
5645 int seen_program, errors_before, errors;
5646 gfc_state_data top, s;
5647 gfc_statement st;
5648 locus prog_locus;
5649 gfc_namespace *next;
5651 gfc_start_source_files ();
5653 top.state = COMP_NONE;
5654 top.sym = NULL;
5655 top.previous = NULL;
5656 top.head = top.tail = NULL;
5657 top.do_variable = NULL;
5659 gfc_state_stack = &top;
5661 gfc_clear_new_st ();
5663 gfc_statement_label = NULL;
5665 if (setjmp (eof_buf))
5666 return false; /* Come here on unexpected EOF */
5668 /* Prepare the global namespace that will contain the
5669 program units. */
5670 gfc_global_ns_list = next = NULL;
5672 seen_program = 0;
5673 errors_before = 0;
5675 /* Exit early for empty files. */
5676 if (gfc_at_eof ())
5677 goto done;
5679 in_specification_block = true;
5680 loop:
5681 gfc_init_2 ();
5682 st = next_statement ();
5683 switch (st)
5685 case ST_NONE:
5686 gfc_done_2 ();
5687 goto done;
5689 case ST_PROGRAM:
5690 if (seen_program)
5691 goto duplicate_main;
5692 seen_program = 1;
5693 prog_locus = gfc_current_locus;
5695 push_state (&s, COMP_PROGRAM, gfc_new_block);
5696 main_program_symbol(gfc_current_ns, gfc_new_block->name);
5697 accept_statement (st);
5698 add_global_program ();
5699 parse_progunit (ST_NONE);
5700 goto prog_units;
5701 break;
5703 case ST_SUBROUTINE:
5704 add_global_procedure (true);
5705 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
5706 accept_statement (st);
5707 parse_progunit (ST_NONE);
5708 goto prog_units;
5709 break;
5711 case ST_FUNCTION:
5712 add_global_procedure (false);
5713 push_state (&s, COMP_FUNCTION, gfc_new_block);
5714 accept_statement (st);
5715 parse_progunit (ST_NONE);
5716 goto prog_units;
5717 break;
5719 case ST_BLOCK_DATA:
5720 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
5721 accept_statement (st);
5722 parse_block_data ();
5723 break;
5725 case ST_MODULE:
5726 push_state (&s, COMP_MODULE, gfc_new_block);
5727 accept_statement (st);
5729 gfc_get_errors (NULL, &errors_before);
5730 parse_module ();
5731 break;
5733 case ST_SUBMODULE:
5734 push_state (&s, COMP_SUBMODULE, gfc_new_block);
5735 accept_statement (st);
5737 gfc_get_errors (NULL, &errors_before);
5738 parse_module ();
5739 break;
5741 /* Anything else starts a nameless main program block. */
5742 default:
5743 if (seen_program)
5744 goto duplicate_main;
5745 seen_program = 1;
5746 prog_locus = gfc_current_locus;
5748 push_state (&s, COMP_PROGRAM, gfc_new_block);
5749 main_program_symbol (gfc_current_ns, "MAIN__");
5750 parse_progunit (st);
5751 goto prog_units;
5752 break;
5755 /* Handle the non-program units. */
5756 gfc_current_ns->code = s.head;
5758 gfc_resolve (gfc_current_ns);
5760 /* Dump the parse tree if requested. */
5761 if (flag_dump_fortran_original)
5762 gfc_dump_parse_tree (gfc_current_ns, stdout);
5764 gfc_get_errors (NULL, &errors);
5765 if (s.state == COMP_MODULE || s.state == COMP_SUBMODULE)
5767 gfc_dump_module (s.sym->name, errors_before == errors);
5768 gfc_current_ns->derived_types = gfc_derived_types;
5769 gfc_derived_types = NULL;
5770 goto prog_units;
5772 else
5774 if (errors == 0)
5775 gfc_generate_code (gfc_current_ns);
5776 pop_state ();
5777 gfc_done_2 ();
5780 goto loop;
5782 prog_units:
5783 /* The main program and non-contained procedures are put
5784 in the global namespace list, so that they can be processed
5785 later and all their interfaces resolved. */
5786 gfc_current_ns->code = s.head;
5787 if (next)
5789 for (; next->sibling; next = next->sibling)
5791 next->sibling = gfc_current_ns;
5793 else
5794 gfc_global_ns_list = gfc_current_ns;
5796 next = gfc_current_ns;
5798 pop_state ();
5799 goto loop;
5801 done:
5803 /* Do the resolution. */
5804 resolve_all_program_units (gfc_global_ns_list);
5806 /* Do the parse tree dump. */
5807 gfc_current_ns
5808 = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
5810 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5811 if (!gfc_current_ns->proc_name
5812 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5814 gfc_dump_parse_tree (gfc_current_ns, stdout);
5815 fputs ("------------------------------------------\n\n", stdout);
5818 /* Do the translation. */
5819 translate_all_program_units (gfc_global_ns_list);
5821 gfc_end_source_files ();
5822 return true;
5824 duplicate_main:
5825 /* If we see a duplicate main program, shut down. If the second
5826 instance is an implied main program, i.e. data decls or executable
5827 statements, we're in for lots of errors. */
5828 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
5829 reject_statement ();
5830 gfc_done_2 ();
5831 return true;
5834 /* Return true if this state data represents an OpenACC region. */
5835 bool
5836 is_oacc (gfc_state_data *sd)
5838 switch (sd->construct->op)
5840 case EXEC_OACC_PARALLEL_LOOP:
5841 case EXEC_OACC_PARALLEL:
5842 case EXEC_OACC_KERNELS_LOOP:
5843 case EXEC_OACC_KERNELS:
5844 case EXEC_OACC_DATA:
5845 case EXEC_OACC_HOST_DATA:
5846 case EXEC_OACC_LOOP:
5847 case EXEC_OACC_UPDATE:
5848 case EXEC_OACC_WAIT:
5849 case EXEC_OACC_CACHE:
5850 case EXEC_OACC_ENTER_DATA:
5851 case EXEC_OACC_EXIT_DATA:
5852 case EXEC_OACC_ATOMIC:
5853 case EXEC_OACC_ROUTINE:
5854 return true;
5856 default:
5857 return false;