libbacktrace: add casts to avoid undefined shifts
[gcc.git] / gcc / fortran / parse.cc
blob00cd23d77299d4dd0e3a60b0a5c8a895f428595e
1 /* Main parser.
2 Copyright (C) 2000-2025 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"
29 #include "tree-core.h"
30 #include "omp-general.h"
32 /* Current statement label. Zero means no statement label. Because new_st
33 can get wiped during statement matching, we have to keep it separate. */
35 gfc_st_label *gfc_statement_label;
37 static locus label_locus;
38 static jmp_buf eof_buf;
40 /* Respectively pointer and content of the current interface body being parsed
41 as they were at the beginning of decode_statement. Used to restore the
42 interface to its previous state in case a parsed statement is rejected after
43 some symbols have been added to the interface. */
44 static gfc_interface **current_interface_ptr = nullptr;
45 static gfc_interface *previous_interface_head = nullptr;
47 gfc_state_data *gfc_state_stack;
48 static bool last_was_use_stmt = false;
49 bool in_exec_part;
51 /* True when matching an OpenMP context selector. */
52 bool gfc_matching_omp_context_selector;
54 /* True when parsing the body of an OpenMP metadirective. */
55 bool gfc_in_omp_metadirective_body;
57 /* Each metadirective body in the translation unit is given a unique
58 number, used to ensure that labels in the body have unique names. */
59 int gfc_omp_metadirective_region_count;
61 /* TODO: Re-order functions to kill these forward decls. */
62 static void check_statement_label (gfc_statement);
63 static void undo_new_statement (void);
64 static void reject_statement (void);
67 /* A sort of half-matching function. We try to match the word on the
68 input with the passed string. If this succeeds, we call the
69 keyword-dependent matching function that will match the rest of the
70 statement. For single keywords, the matching subroutine is
71 gfc_match_eos(). */
73 static match
74 match_word (const char *str, match (*subr) (void), locus *old_locus)
76 match m;
78 if (str != NULL)
80 m = gfc_match (str);
81 if (m != MATCH_YES)
82 return m;
85 m = (*subr) ();
87 if (m != MATCH_YES)
89 gfc_current_locus = *old_locus;
90 reject_statement ();
93 return m;
97 /* Like match_word, but if str is matched, set a flag that it
98 was matched. */
99 static match
100 match_word_omp_simd (const char *str, match (*subr) (void), locus *old_locus,
101 bool *simd_matched)
103 match m;
105 if (str != NULL)
107 m = gfc_match (str);
108 if (m != MATCH_YES)
109 return m;
110 *simd_matched = true;
113 m = (*subr) ();
115 if (m != MATCH_YES)
117 gfc_current_locus = *old_locus;
118 reject_statement ();
121 return m;
125 /* Load symbols from all USE statements encountered in this scoping unit. */
127 static void
128 use_modules (void)
130 gfc_error_buffer old_error;
132 gfc_push_error (&old_error);
133 gfc_buffer_error (false);
134 gfc_use_modules ();
135 gfc_buffer_error (true);
136 gfc_pop_error (&old_error);
137 gfc_commit_symbols ();
138 gfc_warning_check ();
139 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
140 gfc_current_ns->old_data = gfc_current_ns->data;
141 last_was_use_stmt = false;
145 /* Figure out what the next statement is, (mostly) regardless of
146 proper ordering. The do...while(0) is there to prevent if/else
147 ambiguity. */
149 #define match(keyword, subr, st) \
150 do { \
151 if (match_word (keyword, subr, &old_locus) == MATCH_YES) \
152 return st; \
153 else \
154 undo_new_statement (); \
155 } while (0)
158 /* This is a specialist version of decode_statement that is used
159 for the specification statements in a function, whose
160 characteristics are deferred into the specification statements.
161 eg.: INTEGER (king = mykind) foo ()
162 USE mymodule, ONLY mykind.....
163 The KIND parameter needs a return after USE or IMPORT, whereas
164 derived type declarations can occur anywhere, up the executable
165 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
166 out of the correct kind of specification statements. */
167 static gfc_statement
168 decode_specification_statement (void)
170 gfc_statement st;
171 locus old_locus;
172 char c;
174 if (gfc_match_eos () == MATCH_YES)
175 return ST_NONE;
177 old_locus = gfc_current_locus;
179 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
181 last_was_use_stmt = true;
182 return ST_USE;
184 else
186 undo_new_statement ();
187 if (last_was_use_stmt)
188 use_modules ();
191 match ("import", gfc_match_import, ST_IMPORT);
193 if (gfc_current_block ()->result->ts.type != BT_DERIVED)
194 goto end_of_block;
196 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
197 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
198 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
200 /* General statement matching: Instead of testing every possible
201 statement, we eliminate most possibilities by peeking at the
202 first character. */
204 c = gfc_peek_ascii_char ();
206 switch (c)
208 case 'a':
209 match ("abstract% interface", gfc_match_abstract_interface,
210 ST_INTERFACE);
211 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
212 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
213 match ("automatic", gfc_match_automatic, ST_ATTR_DECL);
214 break;
216 case 'b':
217 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
218 break;
220 case 'c':
221 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
222 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
223 break;
225 case 'd':
226 match ("data", gfc_match_data, ST_DATA);
227 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
228 break;
230 case 'e':
231 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
232 match ("entry% ", gfc_match_entry, ST_ENTRY);
233 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
234 match ("external", gfc_match_external, ST_ATTR_DECL);
235 break;
237 case 'f':
238 match ("format", gfc_match_format, ST_FORMAT);
239 break;
241 case 'g':
242 break;
244 case 'i':
245 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
246 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
247 match ("interface", gfc_match_interface, ST_INTERFACE);
248 match ("intent", gfc_match_intent, ST_ATTR_DECL);
249 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
250 break;
252 case 'm':
253 break;
255 case 'n':
256 match ("namelist", gfc_match_namelist, ST_NAMELIST);
257 break;
259 case 'o':
260 match ("optional", gfc_match_optional, ST_ATTR_DECL);
261 break;
263 case 'p':
264 match ("parameter", gfc_match_parameter, ST_PARAMETER);
265 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
266 if (gfc_match_private (&st) == MATCH_YES)
267 return st;
268 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
269 if (gfc_match_public (&st) == MATCH_YES)
270 return st;
271 match ("protected", gfc_match_protected, ST_ATTR_DECL);
272 break;
274 case 'r':
275 break;
277 case 's':
278 match ("save", gfc_match_save, ST_ATTR_DECL);
279 match ("static", gfc_match_static, ST_ATTR_DECL);
280 match ("structure", gfc_match_structure_decl, ST_STRUCTURE_DECL);
281 break;
283 case 't':
284 match ("target", gfc_match_target, ST_ATTR_DECL);
285 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
286 break;
288 case 'u':
289 break;
291 case 'v':
292 match ("value", gfc_match_value, ST_ATTR_DECL);
293 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
294 break;
296 case 'w':
297 break;
300 /* This is not a specification statement. See if any of the matchers
301 has stored an error message of some sort. */
303 end_of_block:
304 gfc_clear_error ();
305 gfc_buffer_error (false);
306 gfc_current_locus = old_locus;
308 return ST_GET_FCN_CHARACTERISTICS;
312 /* Tells whether gfc_get_current_interface_head can be used safely. */
314 static bool
315 current_interface_valid_p ()
317 switch (current_interface.type)
319 case INTERFACE_INTRINSIC_OP:
320 return current_interface.ns != nullptr;
322 case INTERFACE_GENERIC:
323 case INTERFACE_DTIO:
324 return current_interface.sym != nullptr;
326 case INTERFACE_USER_OP:
327 return current_interface.uop != nullptr;
329 default:
330 return false;
335 /* Return a pointer to the interface currently being parsed, or nullptr if
336 we are not currently parsing an interface body. */
338 static gfc_interface **
339 get_current_interface_ptr ()
341 if (current_interface_valid_p ())
343 gfc_interface *& ifc_ptr = gfc_current_interface_head ();
344 return &ifc_ptr;
346 else
347 return nullptr;
351 static bool in_specification_block;
353 /* This is the primary 'decode_statement'. */
354 static gfc_statement
355 decode_statement (void)
357 gfc_statement st;
358 locus old_locus;
359 match m = MATCH_NO;
360 char c;
362 gfc_enforce_clean_symbol_state ();
364 gfc_clear_error (); /* Clear any pending errors. */
365 gfc_clear_warning (); /* Clear any pending warnings. */
367 current_interface_ptr = get_current_interface_ptr ();
368 previous_interface_head = current_interface_ptr == nullptr
369 ? nullptr
370 : *current_interface_ptr;
372 gfc_matching_function = false;
374 if (gfc_match_eos () == MATCH_YES)
375 return ST_NONE;
377 if (gfc_current_state () == COMP_FUNCTION
378 && gfc_current_block ()->result->ts.kind == -1)
379 return decode_specification_statement ();
381 old_locus = gfc_current_locus;
383 c = gfc_peek_ascii_char ();
385 if (c == 'u')
387 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
389 last_was_use_stmt = true;
390 return ST_USE;
392 else
393 undo_new_statement ();
396 if (last_was_use_stmt)
397 use_modules ();
399 /* Try matching a data declaration or function declaration. The
400 input "REALFUNCTIONA(N)" can mean several things in different
401 contexts, so it (and its relatives) get special treatment. */
403 if (gfc_current_state () == COMP_NONE
404 || gfc_current_state () == COMP_INTERFACE
405 || gfc_current_state () == COMP_CONTAINS)
407 gfc_matching_function = true;
408 m = gfc_match_function_decl ();
409 if (m == MATCH_YES)
410 return ST_FUNCTION;
411 else if (m == MATCH_ERROR)
412 reject_statement ();
413 else
414 gfc_undo_symbols ();
415 gfc_current_locus = old_locus;
417 gfc_matching_function = false;
419 /* Legacy parameter statements are ambiguous with assignments so try parameter
420 first. */
421 match ("parameter", gfc_match_parameter, ST_PARAMETER);
423 /* Match statements whose error messages are meant to be overwritten
424 by something better. */
426 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
427 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
429 if (in_specification_block)
431 m = match_word (NULL, gfc_match_st_function, &old_locus);
432 if (m == MATCH_YES)
433 return ST_STATEMENT_FUNCTION;
436 if (!(in_specification_block && m == MATCH_ERROR))
438 match (NULL, gfc_match_ptr_fcn_assign, ST_ASSIGNMENT);
441 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
442 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
444 /* Try to match a subroutine statement, which has the same optional
445 prefixes that functions can have. */
447 if (gfc_match_subroutine () == MATCH_YES)
448 return ST_SUBROUTINE;
449 gfc_undo_symbols ();
450 gfc_current_locus = old_locus;
452 if (gfc_match_submod_proc () == MATCH_YES)
454 if (gfc_new_block->attr.subroutine)
455 return ST_SUBROUTINE;
456 else if (gfc_new_block->attr.function)
457 return ST_FUNCTION;
459 gfc_undo_symbols ();
460 gfc_current_locus = old_locus;
462 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
463 statements, which might begin with a block label. The match functions for
464 these statements are unusual in that their keyword is not seen before
465 the matcher is called. */
467 if (gfc_match_if (&st) == MATCH_YES)
468 return st;
469 gfc_undo_symbols ();
470 gfc_current_locus = old_locus;
472 if (gfc_match_where (&st) == MATCH_YES)
473 return st;
474 gfc_undo_symbols ();
475 gfc_current_locus = old_locus;
477 if (gfc_match_forall (&st) == MATCH_YES)
478 return st;
479 gfc_undo_symbols ();
480 gfc_current_locus = old_locus;
482 /* Try to match TYPE as an alias for PRINT. */
483 if (gfc_match_type (&st) == MATCH_YES)
484 return st;
485 gfc_undo_symbols ();
486 gfc_current_locus = old_locus;
488 match (NULL, gfc_match_do, ST_DO);
489 match (NULL, gfc_match_block, ST_BLOCK);
490 match (NULL, gfc_match_associate, ST_ASSOCIATE);
491 match (NULL, gfc_match_critical, ST_CRITICAL);
492 match (NULL, gfc_match_select, ST_SELECT_CASE);
493 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
494 match (NULL, gfc_match_select_rank, ST_SELECT_RANK);
496 /* General statement matching: Instead of testing every possible
497 statement, we eliminate most possibilities by peeking at the
498 first character. */
500 switch (c)
502 case 'a':
503 match ("abstract% interface", gfc_match_abstract_interface,
504 ST_INTERFACE);
505 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
506 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
507 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
508 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
509 match ("automatic", gfc_match_automatic, ST_ATTR_DECL);
510 break;
512 case 'b':
513 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
514 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
515 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
516 break;
518 case 'c':
519 match ("call", gfc_match_call, ST_CALL);
520 match ("change% team", gfc_match_change_team, ST_CHANGE_TEAM);
521 match ("close", gfc_match_close, ST_CLOSE);
522 match ("continue", gfc_match_continue, ST_CONTINUE);
523 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
524 match ("cycle", gfc_match_cycle, ST_CYCLE);
525 match ("case", gfc_match_case, ST_CASE);
526 match ("common", gfc_match_common, ST_COMMON);
527 match ("contains", gfc_match_eos, ST_CONTAINS);
528 match ("class", gfc_match_class_is, ST_CLASS_IS);
529 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
530 break;
532 case 'd':
533 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
534 match ("data", gfc_match_data, ST_DATA);
535 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
536 break;
538 case 'e':
539 match ("end file", gfc_match_endfile, ST_END_FILE);
540 match ("end team", gfc_match_end_team, ST_END_TEAM);
541 match ("exit", gfc_match_exit, ST_EXIT);
542 match ("else", gfc_match_else, ST_ELSE);
543 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
544 match ("else if", gfc_match_elseif, ST_ELSEIF);
545 match ("error% stop", gfc_match_error_stop, ST_ERROR_STOP);
546 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
548 if (gfc_match_end (&st) == MATCH_YES)
549 return st;
551 match ("entry% ", gfc_match_entry, ST_ENTRY);
552 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
553 match ("external", gfc_match_external, ST_ATTR_DECL);
554 match ("event% post", gfc_match_event_post, ST_EVENT_POST);
555 match ("event% wait", gfc_match_event_wait, ST_EVENT_WAIT);
556 break;
558 case 'f':
559 match ("fail% image", gfc_match_fail_image, ST_FAIL_IMAGE);
560 match ("final", gfc_match_final_decl, ST_FINAL);
561 match ("flush", gfc_match_flush, ST_FLUSH);
562 match ("form% team", gfc_match_form_team, ST_FORM_TEAM);
563 match ("format", gfc_match_format, ST_FORMAT);
564 break;
566 case 'g':
567 match ("generic", gfc_match_generic, ST_GENERIC);
568 match ("go to", gfc_match_goto, ST_GOTO);
569 break;
571 case 'i':
572 match ("inquire", gfc_match_inquire, ST_INQUIRE);
573 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
574 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
575 match ("import", gfc_match_import, ST_IMPORT);
576 match ("interface", gfc_match_interface, ST_INTERFACE);
577 match ("intent", gfc_match_intent, ST_ATTR_DECL);
578 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
579 break;
581 case 'l':
582 match ("lock", gfc_match_lock, ST_LOCK);
583 break;
585 case 'm':
586 match ("map", gfc_match_map, ST_MAP);
587 match ("module% procedure", gfc_match_modproc, ST_MODULE_PROC);
588 match ("module", gfc_match_module, ST_MODULE);
589 break;
591 case 'n':
592 match ("nullify", gfc_match_nullify, ST_NULLIFY);
593 match ("namelist", gfc_match_namelist, ST_NAMELIST);
594 break;
596 case 'o':
597 match ("open", gfc_match_open, ST_OPEN);
598 match ("optional", gfc_match_optional, ST_ATTR_DECL);
599 break;
601 case 'p':
602 match ("print", gfc_match_print, ST_WRITE);
603 match ("pause", gfc_match_pause, ST_PAUSE);
604 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
605 if (gfc_match_private (&st) == MATCH_YES)
606 return st;
607 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
608 match ("program", gfc_match_program, ST_PROGRAM);
609 if (gfc_match_public (&st) == MATCH_YES)
610 return st;
611 match ("protected", gfc_match_protected, ST_ATTR_DECL);
612 break;
614 case 'r':
615 match ("rank", gfc_match_rank_is, ST_RANK);
616 match ("read", gfc_match_read, ST_READ);
617 match ("return", gfc_match_return, ST_RETURN);
618 match ("rewind", gfc_match_rewind, ST_REWIND);
619 break;
621 case 's':
622 match ("structure", gfc_match_structure_decl, ST_STRUCTURE_DECL);
623 match ("sequence", gfc_match_eos, ST_SEQUENCE);
624 match ("stop", gfc_match_stop, ST_STOP);
625 match ("save", gfc_match_save, ST_ATTR_DECL);
626 match ("static", gfc_match_static, ST_ATTR_DECL);
627 match ("submodule", gfc_match_submodule, ST_SUBMODULE);
628 match ("sync% all", gfc_match_sync_all, ST_SYNC_ALL);
629 match ("sync% images", gfc_match_sync_images, ST_SYNC_IMAGES);
630 match ("sync% memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
631 match ("sync% team", gfc_match_sync_team, ST_SYNC_TEAM);
632 break;
634 case 't':
635 match ("target", gfc_match_target, ST_ATTR_DECL);
636 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
637 match ("type% is", gfc_match_type_is, ST_TYPE_IS);
638 break;
640 case 'u':
641 match ("union", gfc_match_union, ST_UNION);
642 match ("unlock", gfc_match_unlock, ST_UNLOCK);
643 break;
645 case 'v':
646 match ("value", gfc_match_value, ST_ATTR_DECL);
647 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
648 break;
650 case 'w':
651 match ("wait", gfc_match_wait, ST_WAIT);
652 match ("write", gfc_match_write, ST_WRITE);
653 break;
656 /* All else has failed, so give up. See if any of the matchers has
657 stored an error message of some sort. Suppress the "Unclassifiable
658 statement" if a previous error message was emitted, e.g., by
659 gfc_error_now (). */
660 if (!gfc_error_check ())
662 int ecnt;
663 gfc_get_errors (NULL, &ecnt);
664 if (ecnt <= 0)
665 gfc_error_now ("Unclassifiable statement at %C");
668 reject_statement ();
670 gfc_error_recovery ();
672 return ST_NONE;
675 /* Like match and if spec_only, goto do_spec_only without actually
676 matching. */
677 /* If the directive matched but the clauses failed, do not start
678 matching the next directive in the same switch statement. */
679 #define matcha(keyword, subr, st) \
680 do { \
681 match m2; \
682 if (spec_only && gfc_match (keyword) == MATCH_YES) \
683 goto do_spec_only; \
684 else if ((m2 = match_word (keyword, subr, &old_locus)) \
685 == MATCH_YES) \
686 return st; \
687 else if (m2 == MATCH_ERROR) \
688 goto error_handling; \
689 else \
690 undo_new_statement (); \
691 } while (0)
693 static gfc_statement
694 decode_oacc_directive (void)
696 locus old_locus;
697 char c;
698 bool spec_only = false;
700 gfc_enforce_clean_symbol_state ();
702 gfc_clear_error (); /* Clear any pending errors. */
703 gfc_clear_warning (); /* Clear any pending warnings. */
705 gfc_matching_function = false;
707 if (gfc_current_state () == COMP_FUNCTION
708 && gfc_current_block ()->result->ts.kind == -1)
709 spec_only = true;
711 old_locus = gfc_current_locus;
713 /* General OpenACC directive matching: Instead of testing every possible
714 statement, we eliminate most possibilities by peeking at the
715 first character. */
717 c = gfc_peek_ascii_char ();
719 switch (c)
721 case 'r':
722 matcha ("routine", gfc_match_oacc_routine, ST_OACC_ROUTINE);
723 break;
726 gfc_unset_implicit_pure (NULL);
727 if (gfc_pure (NULL))
729 gfc_error_now ("OpenACC directives other than ROUTINE may not appear in PURE "
730 "procedures at %C");
731 goto error_handling;
734 switch (c)
736 case 'a':
737 matcha ("atomic", gfc_match_oacc_atomic, ST_OACC_ATOMIC);
738 break;
739 case 'c':
740 matcha ("cache", gfc_match_oacc_cache, ST_OACC_CACHE);
741 break;
742 case 'd':
743 matcha ("data", gfc_match_oacc_data, ST_OACC_DATA);
744 match ("declare", gfc_match_oacc_declare, ST_OACC_DECLARE);
745 break;
746 case 'e':
747 matcha ("end atomic", gfc_match_omp_eos_error, ST_OACC_END_ATOMIC);
748 matcha ("end data", gfc_match_omp_eos_error, ST_OACC_END_DATA);
749 matcha ("end host_data", gfc_match_omp_eos_error, ST_OACC_END_HOST_DATA);
750 matcha ("end kernels loop", gfc_match_omp_eos_error, ST_OACC_END_KERNELS_LOOP);
751 matcha ("end kernels", gfc_match_omp_eos_error, ST_OACC_END_KERNELS);
752 matcha ("end loop", gfc_match_omp_eos_error, ST_OACC_END_LOOP);
753 matcha ("end parallel loop", gfc_match_omp_eos_error,
754 ST_OACC_END_PARALLEL_LOOP);
755 matcha ("end parallel", gfc_match_omp_eos_error, ST_OACC_END_PARALLEL);
756 matcha ("end serial loop", gfc_match_omp_eos_error,
757 ST_OACC_END_SERIAL_LOOP);
758 matcha ("end serial", gfc_match_omp_eos_error, ST_OACC_END_SERIAL);
759 matcha ("enter data", gfc_match_oacc_enter_data, ST_OACC_ENTER_DATA);
760 matcha ("exit data", gfc_match_oacc_exit_data, ST_OACC_EXIT_DATA);
761 break;
762 case 'h':
763 matcha ("host_data", gfc_match_oacc_host_data, ST_OACC_HOST_DATA);
764 break;
765 case 'p':
766 matcha ("parallel loop", gfc_match_oacc_parallel_loop,
767 ST_OACC_PARALLEL_LOOP);
768 matcha ("parallel", gfc_match_oacc_parallel, ST_OACC_PARALLEL);
769 break;
770 case 'k':
771 matcha ("kernels loop", gfc_match_oacc_kernels_loop,
772 ST_OACC_KERNELS_LOOP);
773 matcha ("kernels", gfc_match_oacc_kernels, ST_OACC_KERNELS);
774 break;
775 case 'l':
776 matcha ("loop", gfc_match_oacc_loop, ST_OACC_LOOP);
777 break;
778 case 's':
779 matcha ("serial loop", gfc_match_oacc_serial_loop, ST_OACC_SERIAL_LOOP);
780 matcha ("serial", gfc_match_oacc_serial, ST_OACC_SERIAL);
781 break;
782 case 'u':
783 matcha ("update", gfc_match_oacc_update, ST_OACC_UPDATE);
784 break;
785 case 'w':
786 matcha ("wait", gfc_match_oacc_wait, ST_OACC_WAIT);
787 break;
790 /* Directive not found or stored an error message.
791 Check and give up. */
793 error_handling:
794 if (gfc_error_check () == 0)
795 gfc_error_now ("Unclassifiable OpenACC directive at %C");
797 reject_statement ();
799 gfc_error_recovery ();
801 return ST_NONE;
803 do_spec_only:
804 reject_statement ();
805 gfc_clear_error ();
806 gfc_buffer_error (false);
807 gfc_current_locus = old_locus;
808 return ST_GET_FCN_CHARACTERISTICS;
811 /* Checks for the ST_OMP_ALLOCATE. First, check whether all list items
812 are allocatables/pointers - and if so, assume it is associated with a Fortran
813 ALLOCATE stmt. If not, do some initial parsing-related checks and append
814 namelist to namespace.
815 The check follows OpenMP 5.1 by requiring an executable stmt or OpenMP
816 construct before a directive associated with an allocate statement
817 (-> ST_OMP_ALLOCATE_EXEC); instead of showing an error, conversion of
818 ST_OMP_ALLOCATE -> ST_OMP_ALLOCATE_EXEC would be an alternative. */
820 bool
821 check_omp_allocate_stmt (locus *loc)
823 gfc_omp_namelist *n;
825 if (new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE]->sym == NULL)
827 gfc_error ("%qs directive at %L must either have a variable argument or, "
828 "if associated with an ALLOCATE stmt, must be preceded by an "
829 "executable statement or OpenMP construct",
830 gfc_ascii_statement (ST_OMP_ALLOCATE), loc);
831 return false;
833 bool has_allocatable = false;
834 bool has_non_allocatable = false;
835 for (n = new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE]; n; n = n->next)
837 if (n->expr)
839 gfc_error ("Structure-component expression at %L in %qs directive not"
840 " permitted in declarative directive; as directive "
841 "associated with an ALLOCATE stmt it must be preceded by "
842 "an executable statement or OpenMP construct",
843 &n->expr->where, gfc_ascii_statement (ST_OMP_ALLOCATE));
844 return false;
846 /* Procedure pointers are not allocatable; hence, we do not regard them as
847 pointers here - and reject them later in gfc_resolve_omp_allocate. */
848 bool alloc_ptr;
849 if (n->sym->ts.type == BT_CLASS && n->sym->attr.class_ok)
850 alloc_ptr = (CLASS_DATA (n->sym)->attr.allocatable
851 || CLASS_DATA (n->sym)->attr.class_pointer);
852 else
853 alloc_ptr = n->sym->attr.allocatable || n->sym->attr.pointer;
854 if (alloc_ptr
855 || (n->sym->ns && n->sym->ns->proc_name
856 && (n->sym->ns->proc_name->attr.allocatable
857 || n->sym->ns->proc_name->attr.pointer)))
858 has_allocatable = true;
859 else
860 has_non_allocatable = true;
862 /* All allocatables - assume it is allocated with an ALLOCATE stmt. */
863 if (has_allocatable && !has_non_allocatable)
865 gfc_error ("%qs directive at %L associated with an ALLOCATE stmt must be "
866 "preceded by an executable statement or OpenMP construct; "
867 "note the variables in the list all have the allocatable or "
868 "pointer attribute", gfc_ascii_statement (ST_OMP_ALLOCATE),
869 loc);
870 return false;
872 if (!gfc_current_ns->omp_allocate)
873 gfc_current_ns->omp_allocate
874 = new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE];
875 else
877 for (n = gfc_current_ns->omp_allocate; n->next; n = n->next)
879 n->next = new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE];
881 new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE] = NULL;
882 gfc_free_omp_clauses (new_st.ext.omp_clauses);
883 return true;
887 /* Like match, but set a flag simd_matched if keyword matched
888 and if spec_only, goto do_spec_only without actually matching. */
889 #define matchs(keyword, subr, st) \
890 do { \
891 match m2; \
892 if (spec_only && gfc_match (keyword) == MATCH_YES) \
893 goto do_spec_only; \
894 if ((m2 = match_word_omp_simd (keyword, subr, &old_locus, \
895 &simd_matched)) == MATCH_YES) \
897 ret = st; \
898 goto finish; \
900 else if (m2 == MATCH_ERROR) \
901 goto error_handling; \
902 else \
903 undo_new_statement (); \
904 } while (0)
906 /* Like match, but don't match anything if not -fopenmp
907 and if spec_only, goto do_spec_only without actually matching. */
908 /* If the directive matched but the clauses failed, do not start
909 matching the next directive in the same switch statement. */
910 #define matcho(keyword, subr, st) \
911 do { \
912 match m2; \
913 if (!flag_openmp) \
915 else if (spec_only && gfc_match (keyword) == MATCH_YES) \
916 goto do_spec_only; \
917 else if ((m2 = match_word (keyword, subr, &old_locus)) \
918 == MATCH_YES) \
920 ret = st; \
921 goto finish; \
923 else if (m2 == MATCH_ERROR) \
924 goto error_handling; \
925 else \
926 undo_new_statement (); \
927 } while (0)
929 /* Like match, but set a flag simd_matched if keyword matched. */
930 #define matchds(keyword, subr, st) \
931 do { \
932 match m2; \
933 if ((m2 = match_word_omp_simd (keyword, subr, &old_locus, \
934 &simd_matched)) == MATCH_YES) \
936 ret = st; \
937 goto finish; \
939 else if (m2 == MATCH_ERROR) \
940 goto error_handling; \
941 else \
942 undo_new_statement (); \
943 } while (0)
945 /* Like match, but don't match anything if not -fopenmp. */
946 #define matchdo(keyword, subr, st) \
947 do { \
948 match m2; \
949 if (!flag_openmp) \
951 else if ((m2 = match_word (keyword, subr, &old_locus)) \
952 == MATCH_YES) \
954 ret = st; \
955 goto finish; \
957 else if (m2 == MATCH_ERROR) \
958 goto error_handling; \
959 else \
960 undo_new_statement (); \
961 } while (0)
963 static gfc_statement
964 decode_omp_directive (void)
966 locus old_locus;
967 char c;
968 bool simd_matched = false;
969 bool spec_only = false;
970 gfc_statement ret = ST_NONE;
971 bool pure_ok = true;
973 gfc_enforce_clean_symbol_state ();
975 gfc_clear_error (); /* Clear any pending errors. */
976 gfc_clear_warning (); /* Clear any pending warnings. */
978 gfc_matching_function = false;
980 if (gfc_current_state () == COMP_FUNCTION
981 && gfc_current_block ()->result->ts.kind == -1)
982 spec_only = true;
984 old_locus = gfc_current_locus;
986 /* General OpenMP directive matching: Instead of testing every possible
987 statement, we eliminate most possibilities by peeking at the
988 first character. */
990 c = gfc_peek_ascii_char ();
992 /* match is for directives that should be recognized only if
993 -fopenmp, matchs for directives that should be recognized
994 if either -fopenmp or -fopenmp-simd.
995 Handle only the directives allowed in PURE procedures
996 first (those also shall not turn off implicit pure). */
997 switch (c)
999 case 'a':
1000 /* For -fopenmp-simd, ignore 'assumes'; note no clause starts with 's'. */
1001 if (!flag_openmp && gfc_match ("assumes") == MATCH_YES)
1002 break;
1003 matcho ("assumes", gfc_match_omp_assumes, ST_OMP_ASSUMES);
1004 matchs ("assume", gfc_match_omp_assume, ST_OMP_ASSUME);
1005 break;
1007 case 'b':
1008 matcho ("begin metadirective", gfc_match_omp_begin_metadirective,
1009 ST_OMP_BEGIN_METADIRECTIVE);
1010 break;
1012 case 'd':
1013 matchds ("declare reduction", gfc_match_omp_declare_reduction,
1014 ST_OMP_DECLARE_REDUCTION);
1015 matchds ("declare simd", gfc_match_omp_declare_simd,
1016 ST_OMP_DECLARE_SIMD);
1017 matchdo ("declare target", gfc_match_omp_declare_target,
1018 ST_OMP_DECLARE_TARGET);
1019 matchdo ("declare variant", gfc_match_omp_declare_variant,
1020 ST_OMP_DECLARE_VARIANT);
1021 break;
1022 case 'e':
1023 matchs ("end assume", gfc_match_omp_eos_error, ST_OMP_END_ASSUME);
1024 matcho ("end metadirective", gfc_match_omp_eos_error,
1025 ST_OMP_END_METADIRECTIVE);
1026 matchs ("end simd", gfc_match_omp_eos_error, ST_OMP_END_SIMD);
1027 matchs ("end tile", gfc_match_omp_eos_error, ST_OMP_END_TILE);
1028 matchs ("end unroll", gfc_match_omp_eos_error, ST_OMP_END_UNROLL);
1029 matcho ("error", gfc_match_omp_error, ST_OMP_ERROR);
1030 break;
1032 case 'm':
1033 matcho ("metadirective", gfc_match_omp_metadirective,
1034 ST_OMP_METADIRECTIVE);
1035 break;
1037 case 'n':
1038 matcho ("nothing", gfc_match_omp_nothing, ST_NONE);
1039 break;
1040 case 's':
1041 matchs ("scan", gfc_match_omp_scan, ST_OMP_SCAN);
1042 matchs ("simd", gfc_match_omp_simd, ST_OMP_SIMD);
1043 break;
1044 case 't':
1045 matchs ("tile", gfc_match_omp_tile, ST_OMP_TILE);
1046 break;
1047 case 'u':
1048 matchs ("unroll", gfc_match_omp_unroll, ST_OMP_UNROLL);
1049 break;
1052 pure_ok = false;
1053 if (flag_openmp && gfc_pure (NULL))
1055 gfc_error_now ("OpenMP directive at %C is not pure and thus may not "
1056 "appear in a PURE procedure");
1057 gfc_error_recovery ();
1058 return ST_NONE;
1061 /* match is for directives that should be recognized only if
1062 -fopenmp, matchs for directives that should be recognized
1063 if either -fopenmp or -fopenmp-simd. */
1064 switch (c)
1066 case 'a':
1067 if (in_exec_part)
1068 matcho ("allocate", gfc_match_omp_allocate, ST_OMP_ALLOCATE_EXEC);
1069 else
1070 matcho ("allocate", gfc_match_omp_allocate, ST_OMP_ALLOCATE);
1071 matcho ("allocators", gfc_match_omp_allocators, ST_OMP_ALLOCATORS);
1072 matcho ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
1073 break;
1074 case 'b':
1075 matcho ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
1076 break;
1077 case 'c':
1078 matcho ("cancellation% point", gfc_match_omp_cancellation_point,
1079 ST_OMP_CANCELLATION_POINT);
1080 matcho ("cancel", gfc_match_omp_cancel, ST_OMP_CANCEL);
1081 matcho ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
1082 break;
1083 case 'd':
1084 matcho ("depobj", gfc_match_omp_depobj, ST_OMP_DEPOBJ);
1085 matcho ("dispatch", gfc_match_omp_dispatch, ST_OMP_DISPATCH);
1086 matchs ("distribute parallel do simd",
1087 gfc_match_omp_distribute_parallel_do_simd,
1088 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD);
1089 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do,
1090 ST_OMP_DISTRIBUTE_PARALLEL_DO);
1091 matchs ("distribute simd", gfc_match_omp_distribute_simd,
1092 ST_OMP_DISTRIBUTE_SIMD);
1093 matcho ("distribute", gfc_match_omp_distribute, ST_OMP_DISTRIBUTE);
1094 matchs ("do simd", gfc_match_omp_do_simd, ST_OMP_DO_SIMD);
1095 matcho ("do", gfc_match_omp_do, ST_OMP_DO);
1096 break;
1097 case 'e':
1098 matcho ("end allocators", gfc_match_omp_eos_error, ST_OMP_END_ALLOCATORS);
1099 matcho ("end atomic", gfc_match_omp_eos_error, ST_OMP_END_ATOMIC);
1100 matcho ("end critical", gfc_match_omp_end_critical, ST_OMP_END_CRITICAL);
1101 matcho ("end dispatch", gfc_match_omp_end_nowait, ST_OMP_END_DISPATCH);
1102 matchs ("end distribute parallel do simd", gfc_match_omp_eos_error,
1103 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD);
1104 matcho ("end distribute parallel do", gfc_match_omp_eos_error,
1105 ST_OMP_END_DISTRIBUTE_PARALLEL_DO);
1106 matchs ("end distribute simd", gfc_match_omp_eos_error,
1107 ST_OMP_END_DISTRIBUTE_SIMD);
1108 matcho ("end distribute", gfc_match_omp_eos_error, ST_OMP_END_DISTRIBUTE);
1109 matchs ("end do simd", gfc_match_omp_end_nowait, ST_OMP_END_DO_SIMD);
1110 matcho ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
1111 matchs ("end loop", gfc_match_omp_eos_error, ST_OMP_END_LOOP);
1112 matcho ("end masked taskloop simd", gfc_match_omp_eos_error,
1113 ST_OMP_END_MASKED_TASKLOOP_SIMD);
1114 matcho ("end masked taskloop", gfc_match_omp_eos_error,
1115 ST_OMP_END_MASKED_TASKLOOP);
1116 matcho ("end masked", gfc_match_omp_eos_error, ST_OMP_END_MASKED);
1117 matcho ("end master taskloop simd", gfc_match_omp_eos_error,
1118 ST_OMP_END_MASTER_TASKLOOP_SIMD);
1119 matcho ("end master taskloop", gfc_match_omp_eos_error,
1120 ST_OMP_END_MASTER_TASKLOOP);
1121 matcho ("end master", gfc_match_omp_eos_error, ST_OMP_END_MASTER);
1122 matchs ("end ordered", gfc_match_omp_eos_error, ST_OMP_END_ORDERED);
1123 matchs ("end parallel do simd", gfc_match_omp_eos_error,
1124 ST_OMP_END_PARALLEL_DO_SIMD);
1125 matcho ("end parallel do", gfc_match_omp_eos_error,
1126 ST_OMP_END_PARALLEL_DO);
1127 matcho ("end parallel loop", gfc_match_omp_eos_error,
1128 ST_OMP_END_PARALLEL_LOOP);
1129 matcho ("end parallel masked taskloop simd", gfc_match_omp_eos_error,
1130 ST_OMP_END_PARALLEL_MASKED_TASKLOOP_SIMD);
1131 matcho ("end parallel masked taskloop", gfc_match_omp_eos_error,
1132 ST_OMP_END_PARALLEL_MASKED_TASKLOOP);
1133 matcho ("end parallel masked", gfc_match_omp_eos_error,
1134 ST_OMP_END_PARALLEL_MASKED);
1135 matcho ("end parallel master taskloop simd", gfc_match_omp_eos_error,
1136 ST_OMP_END_PARALLEL_MASTER_TASKLOOP_SIMD);
1137 matcho ("end parallel master taskloop", gfc_match_omp_eos_error,
1138 ST_OMP_END_PARALLEL_MASTER_TASKLOOP);
1139 matcho ("end parallel master", gfc_match_omp_eos_error,
1140 ST_OMP_END_PARALLEL_MASTER);
1141 matcho ("end parallel sections", gfc_match_omp_eos_error,
1142 ST_OMP_END_PARALLEL_SECTIONS);
1143 matcho ("end parallel workshare", gfc_match_omp_eos_error,
1144 ST_OMP_END_PARALLEL_WORKSHARE);
1145 matcho ("end parallel", gfc_match_omp_eos_error, ST_OMP_END_PARALLEL);
1146 matcho ("end scope", gfc_match_omp_end_nowait, ST_OMP_END_SCOPE);
1147 matcho ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
1148 matcho ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
1149 matcho ("end target data", gfc_match_omp_eos_error, ST_OMP_END_TARGET_DATA);
1150 matchs ("end target parallel do simd", gfc_match_omp_end_nowait,
1151 ST_OMP_END_TARGET_PARALLEL_DO_SIMD);
1152 matcho ("end target parallel do", gfc_match_omp_end_nowait,
1153 ST_OMP_END_TARGET_PARALLEL_DO);
1154 matcho ("end target parallel loop", gfc_match_omp_end_nowait,
1155 ST_OMP_END_TARGET_PARALLEL_LOOP);
1156 matcho ("end target parallel", gfc_match_omp_end_nowait,
1157 ST_OMP_END_TARGET_PARALLEL);
1158 matchs ("end target simd", gfc_match_omp_end_nowait, ST_OMP_END_TARGET_SIMD);
1159 matchs ("end target teams distribute parallel do simd",
1160 gfc_match_omp_end_nowait,
1161 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
1162 matcho ("end target teams distribute parallel do", gfc_match_omp_end_nowait,
1163 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
1164 matchs ("end target teams distribute simd", gfc_match_omp_end_nowait,
1165 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD);
1166 matcho ("end target teams distribute", gfc_match_omp_end_nowait,
1167 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE);
1168 matcho ("end target teams loop", gfc_match_omp_end_nowait,
1169 ST_OMP_END_TARGET_TEAMS_LOOP);
1170 matcho ("end target teams", gfc_match_omp_end_nowait,
1171 ST_OMP_END_TARGET_TEAMS);
1172 matcho ("end target", gfc_match_omp_end_nowait, ST_OMP_END_TARGET);
1173 matcho ("end taskgroup", gfc_match_omp_eos_error, ST_OMP_END_TASKGROUP);
1174 matchs ("end taskloop simd", gfc_match_omp_eos_error,
1175 ST_OMP_END_TASKLOOP_SIMD);
1176 matcho ("end taskloop", gfc_match_omp_eos_error, ST_OMP_END_TASKLOOP);
1177 matcho ("end task", gfc_match_omp_eos_error, ST_OMP_END_TASK);
1178 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos_error,
1179 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
1180 matcho ("end teams distribute parallel do", gfc_match_omp_eos_error,
1181 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO);
1182 matchs ("end teams distribute simd", gfc_match_omp_eos_error,
1183 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD);
1184 matcho ("end teams distribute", gfc_match_omp_eos_error,
1185 ST_OMP_END_TEAMS_DISTRIBUTE);
1186 matcho ("end teams loop", gfc_match_omp_eos_error, ST_OMP_END_TEAMS_LOOP);
1187 matcho ("end teams", gfc_match_omp_eos_error, ST_OMP_END_TEAMS);
1188 matcho ("end workshare", gfc_match_omp_end_nowait,
1189 ST_OMP_END_WORKSHARE);
1190 break;
1191 case 'f':
1192 matcho ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
1193 break;
1194 case 'i':
1195 matcho ("interop", gfc_match_omp_interop, ST_OMP_INTEROP);
1196 break;
1197 case 'm':
1198 matcho ("masked taskloop simd", gfc_match_omp_masked_taskloop_simd,
1199 ST_OMP_MASKED_TASKLOOP_SIMD);
1200 matcho ("masked taskloop", gfc_match_omp_masked_taskloop,
1201 ST_OMP_MASKED_TASKLOOP);
1202 matcho ("masked", gfc_match_omp_masked, ST_OMP_MASKED);
1203 matcho ("master taskloop simd", gfc_match_omp_master_taskloop_simd,
1204 ST_OMP_MASTER_TASKLOOP_SIMD);
1205 matcho ("master taskloop", gfc_match_omp_master_taskloop,
1206 ST_OMP_MASTER_TASKLOOP);
1207 matcho ("master", gfc_match_omp_master, ST_OMP_MASTER);
1208 break;
1209 case 'n':
1210 matcho ("nothing", gfc_match_omp_nothing, ST_NONE);
1211 break;
1212 case 'l':
1213 matchs ("loop", gfc_match_omp_loop, ST_OMP_LOOP);
1214 break;
1215 case 'o':
1216 if (gfc_match ("ordered depend (") == MATCH_YES
1217 || gfc_match ("ordered doacross (") == MATCH_YES)
1219 gfc_current_locus = old_locus;
1220 if (!flag_openmp)
1221 break;
1222 matcho ("ordered", gfc_match_omp_ordered_depend,
1223 ST_OMP_ORDERED_DEPEND);
1225 else
1226 matchs ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
1227 break;
1228 case 'p':
1229 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
1230 ST_OMP_PARALLEL_DO_SIMD);
1231 matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
1232 matcho ("parallel loop", gfc_match_omp_parallel_loop,
1233 ST_OMP_PARALLEL_LOOP);
1234 matcho ("parallel masked taskloop simd",
1235 gfc_match_omp_parallel_masked_taskloop_simd,
1236 ST_OMP_PARALLEL_MASKED_TASKLOOP_SIMD);
1237 matcho ("parallel masked taskloop",
1238 gfc_match_omp_parallel_masked_taskloop,
1239 ST_OMP_PARALLEL_MASKED_TASKLOOP);
1240 matcho ("parallel masked", gfc_match_omp_parallel_masked,
1241 ST_OMP_PARALLEL_MASKED);
1242 matcho ("parallel master taskloop simd",
1243 gfc_match_omp_parallel_master_taskloop_simd,
1244 ST_OMP_PARALLEL_MASTER_TASKLOOP_SIMD);
1245 matcho ("parallel master taskloop",
1246 gfc_match_omp_parallel_master_taskloop,
1247 ST_OMP_PARALLEL_MASTER_TASKLOOP);
1248 matcho ("parallel master", gfc_match_omp_parallel_master,
1249 ST_OMP_PARALLEL_MASTER);
1250 matcho ("parallel sections", gfc_match_omp_parallel_sections,
1251 ST_OMP_PARALLEL_SECTIONS);
1252 matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
1253 ST_OMP_PARALLEL_WORKSHARE);
1254 matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
1255 break;
1256 case 'r':
1257 matcho ("requires", gfc_match_omp_requires, ST_OMP_REQUIRES);
1258 break;
1259 case 's':
1260 matcho ("scope", gfc_match_omp_scope, ST_OMP_SCOPE);
1261 matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
1262 matcho ("section", gfc_match_omp_eos_error, ST_OMP_SECTION);
1263 matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
1264 break;
1265 case 't':
1266 matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
1267 matcho ("target enter data", gfc_match_omp_target_enter_data,
1268 ST_OMP_TARGET_ENTER_DATA);
1269 matcho ("target exit data", gfc_match_omp_target_exit_data,
1270 ST_OMP_TARGET_EXIT_DATA);
1271 matchs ("target parallel do simd", gfc_match_omp_target_parallel_do_simd,
1272 ST_OMP_TARGET_PARALLEL_DO_SIMD);
1273 matcho ("target parallel do", gfc_match_omp_target_parallel_do,
1274 ST_OMP_TARGET_PARALLEL_DO);
1275 matcho ("target parallel loop", gfc_match_omp_target_parallel_loop,
1276 ST_OMP_TARGET_PARALLEL_LOOP);
1277 matcho ("target parallel", gfc_match_omp_target_parallel,
1278 ST_OMP_TARGET_PARALLEL);
1279 matchs ("target simd", gfc_match_omp_target_simd, ST_OMP_TARGET_SIMD);
1280 matchs ("target teams distribute parallel do simd",
1281 gfc_match_omp_target_teams_distribute_parallel_do_simd,
1282 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
1283 matcho ("target teams distribute parallel do",
1284 gfc_match_omp_target_teams_distribute_parallel_do,
1285 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
1286 matchs ("target teams distribute simd",
1287 gfc_match_omp_target_teams_distribute_simd,
1288 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
1289 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
1290 ST_OMP_TARGET_TEAMS_DISTRIBUTE);
1291 matcho ("target teams loop", gfc_match_omp_target_teams_loop,
1292 ST_OMP_TARGET_TEAMS_LOOP);
1293 matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
1294 matcho ("target update", gfc_match_omp_target_update,
1295 ST_OMP_TARGET_UPDATE);
1296 matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
1297 matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
1298 matchs ("taskloop simd", gfc_match_omp_taskloop_simd,
1299 ST_OMP_TASKLOOP_SIMD);
1300 matcho ("taskloop", gfc_match_omp_taskloop, ST_OMP_TASKLOOP);
1301 matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
1302 matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
1303 matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
1304 matchs ("teams distribute parallel do simd",
1305 gfc_match_omp_teams_distribute_parallel_do_simd,
1306 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
1307 matcho ("teams distribute parallel do",
1308 gfc_match_omp_teams_distribute_parallel_do,
1309 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
1310 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
1311 ST_OMP_TEAMS_DISTRIBUTE_SIMD);
1312 matcho ("teams distribute", gfc_match_omp_teams_distribute,
1313 ST_OMP_TEAMS_DISTRIBUTE);
1314 matcho ("teams loop", gfc_match_omp_teams_loop, ST_OMP_TEAMS_LOOP);
1315 matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
1316 matchdo ("threadprivate", gfc_match_omp_threadprivate,
1317 ST_OMP_THREADPRIVATE);
1318 break;
1319 case 'w':
1320 matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
1321 break;
1324 /* All else has failed, so give up. See if any of the matchers has
1325 stored an error message of some sort. Don't error out if
1326 not -fopenmp and simd_matched is false, i.e. if a directive other
1327 than one marked with match has been seen. */
1329 error_handling:
1330 if (flag_openmp || simd_matched)
1332 if (!gfc_error_check ())
1333 gfc_error_now ("Unclassifiable OpenMP directive at %C");
1336 /* If parsing a metadirective, let the caller deal with the cleanup. */
1337 if (gfc_matching_omp_context_selector)
1338 return ST_NONE;
1340 reject_statement ();
1342 gfc_error_recovery ();
1344 return ST_NONE;
1346 finish:
1347 if (ret == ST_OMP_ERROR && new_st.ext.omp_clauses->at == OMP_AT_EXECUTION)
1349 gfc_unset_implicit_pure (NULL);
1351 if (gfc_pure (NULL))
1353 gfc_error_now ("OpenMP ERROR directive at %L with %<at(execution)%> "
1354 "clause in a PURE procedure", &old_locus);
1355 reject_statement ();
1356 gfc_error_recovery ();
1357 return ST_NONE;
1360 if (!pure_ok)
1362 gfc_unset_implicit_pure (NULL);
1364 if (!flag_openmp && gfc_pure (NULL))
1366 gfc_error_now ("OpenMP directive at %C is not pure and thus may not "
1367 "appear in a PURE procedure");
1368 reject_statement ();
1369 gfc_error_recovery ();
1370 return ST_NONE;
1373 if (ret == ST_OMP_ALLOCATE && !check_omp_allocate_stmt (&old_locus))
1374 goto error_handling;
1376 switch (ret)
1378 /* For the constraints on clauses with the global requirement property,
1379 we set omp_target_seen. This included all clauses that take the
1380 DEVICE clause, (BEGIN) DECLARE_TARGET and procedures run the device
1381 (which effectively is implied by the former). */
1382 case ST_OMP_DECLARE_TARGET:
1383 case ST_OMP_INTEROP:
1384 case ST_OMP_TARGET:
1385 case ST_OMP_TARGET_DATA:
1386 case ST_OMP_TARGET_ENTER_DATA:
1387 case ST_OMP_TARGET_EXIT_DATA:
1388 case ST_OMP_TARGET_TEAMS:
1389 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
1390 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
1391 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
1392 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
1393 case ST_OMP_TARGET_TEAMS_LOOP:
1394 case ST_OMP_TARGET_PARALLEL:
1395 case ST_OMP_TARGET_PARALLEL_DO:
1396 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
1397 case ST_OMP_TARGET_PARALLEL_LOOP:
1398 case ST_OMP_TARGET_SIMD:
1399 case ST_OMP_TARGET_UPDATE:
1401 gfc_namespace *prog_unit = gfc_current_ns;
1402 while (prog_unit->parent)
1404 if (gfc_state_stack->previous
1405 && gfc_state_stack->previous->state == COMP_INTERFACE)
1406 break;
1407 prog_unit = prog_unit->parent;
1409 prog_unit->omp_target_seen = true;
1410 break;
1412 case ST_OMP_ALLOCATE_EXEC:
1413 case ST_OMP_ALLOCATORS:
1414 case ST_OMP_TEAMS:
1415 case ST_OMP_TEAMS_DISTRIBUTE:
1416 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
1417 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
1418 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
1419 case ST_OMP_TEAMS_LOOP:
1420 for (gfc_state_data *stk = gfc_state_stack->previous; stk;
1421 stk = stk->previous)
1422 if (stk && stk->tail)
1423 switch (stk->tail->op)
1425 case EXEC_OMP_TARGET:
1426 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
1427 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
1428 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
1429 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
1430 case EXEC_OMP_TARGET_TEAMS_LOOP:
1431 case EXEC_OMP_TARGET_PARALLEL:
1432 case EXEC_OMP_TARGET_PARALLEL_DO:
1433 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
1434 case EXEC_OMP_TARGET_PARALLEL_LOOP:
1435 case EXEC_OMP_TARGET_SIMD:
1436 if (ret == ST_OMP_ALLOCATE_EXEC || ret == ST_OMP_ALLOCATORS)
1437 new_st.ext.omp_clauses->contained_in_target_construct = 1;
1438 else
1439 stk->tail->ext.omp_clauses->contains_teams_construct = 1;
1440 break;
1441 default:
1442 break;
1444 break;
1445 case ST_OMP_ERROR:
1446 if (new_st.ext.omp_clauses->at != OMP_AT_EXECUTION)
1447 return ST_NONE;
1448 default:
1449 break;
1451 return ret;
1453 do_spec_only:
1454 reject_statement ();
1455 gfc_clear_error ();
1456 gfc_buffer_error (false);
1457 gfc_current_locus = old_locus;
1458 return ST_GET_FCN_CHARACTERISTICS;
1461 gfc_statement
1462 match_omp_directive (void)
1464 return decode_omp_directive ();
1467 static gfc_statement
1468 decode_gcc_attribute (void)
1470 locus old_locus;
1472 gfc_enforce_clean_symbol_state ();
1474 gfc_clear_error (); /* Clear any pending errors. */
1475 gfc_clear_warning (); /* Clear any pending warnings. */
1476 old_locus = gfc_current_locus;
1478 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
1479 match ("unroll", gfc_match_gcc_unroll, ST_NONE);
1480 match ("builtin", gfc_match_gcc_builtin, ST_NONE);
1481 match ("ivdep", gfc_match_gcc_ivdep, ST_NONE);
1482 match ("vector", gfc_match_gcc_vector, ST_NONE);
1483 match ("novector", gfc_match_gcc_novector, ST_NONE);
1485 /* All else has failed, so give up. See if any of the matchers has
1486 stored an error message of some sort. */
1488 if (!gfc_error_check ())
1490 if (pedantic)
1491 gfc_error_now ("Unclassifiable GCC directive at %C");
1492 else
1493 gfc_warning_now (0, "Unclassifiable GCC directive at %C, ignored");
1496 reject_statement ();
1498 gfc_error_recovery ();
1500 return ST_NONE;
1503 #undef match
1505 /* Assert next length characters to be equal to token in free form. */
1507 static void
1508 verify_token_free (const char* token, int length, bool last_was_use_stmt)
1510 int i;
1511 char c;
1513 c = gfc_next_ascii_char ();
1514 for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
1515 gcc_assert (c == token[i]);
1517 gcc_assert (gfc_is_whitespace(c));
1518 gfc_gobble_whitespace ();
1519 if (last_was_use_stmt)
1520 use_modules ();
1523 /* Get the next statement in free form source. */
1525 static gfc_statement
1526 next_free (void)
1528 match m;
1529 int i, cnt, at_bol;
1530 char c;
1532 at_bol = gfc_at_bol ();
1533 gfc_gobble_whitespace ();
1535 c = gfc_peek_ascii_char ();
1537 if (ISDIGIT (c))
1539 char d;
1541 /* Found a statement label? */
1542 m = gfc_match_st_label (&gfc_statement_label);
1544 d = gfc_peek_ascii_char ();
1545 if (m != MATCH_YES || !gfc_is_whitespace (d))
1547 gfc_match_small_literal_int (&i, &cnt);
1549 if (cnt > 5)
1550 gfc_error_now ("Too many digits in statement label at %C");
1552 if (i == 0)
1553 gfc_error_now ("Zero is not a valid statement label at %C");
1556 c = gfc_next_ascii_char ();
1557 while (ISDIGIT(c));
1559 if (!gfc_is_whitespace (c))
1560 gfc_error_now ("Non-numeric character in statement label at %C");
1562 return ST_NONE;
1564 else
1566 label_locus = gfc_current_locus;
1568 gfc_gobble_whitespace ();
1570 if (at_bol && gfc_peek_ascii_char () == ';')
1572 gfc_error_now ("Semicolon at %C needs to be preceded by "
1573 "statement");
1574 gfc_next_ascii_char (); /* Eat up the semicolon. */
1575 return ST_NONE;
1578 if (gfc_match_eos () == MATCH_YES)
1579 gfc_error_now ("Statement label without statement at %L",
1580 &label_locus);
1583 else if (c == '!')
1585 /* Comments have already been skipped by the time we get here,
1586 except for GCC attributes and OpenMP/OpenACC directives. */
1588 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
1589 c = gfc_peek_ascii_char ();
1591 if (c == 'g')
1593 int i;
1595 c = gfc_next_ascii_char ();
1596 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
1597 gcc_assert (c == "gcc$"[i]);
1599 gfc_gobble_whitespace ();
1600 return decode_gcc_attribute ();
1603 else if (c == '$')
1605 /* Since both OpenMP and OpenACC directives starts with
1606 !$ character sequence, we must check all flags combinations */
1607 if ((flag_openmp || flag_openmp_simd)
1608 && !flag_openacc)
1610 verify_token_free ("$omp", 4, last_was_use_stmt);
1611 return decode_omp_directive ();
1613 else if ((flag_openmp || flag_openmp_simd)
1614 && flag_openacc)
1616 gfc_next_ascii_char (); /* Eat up dollar character */
1617 c = gfc_peek_ascii_char ();
1619 if (c == 'o')
1621 verify_token_free ("omp", 3, last_was_use_stmt);
1622 return decode_omp_directive ();
1624 else if (c == 'a')
1626 verify_token_free ("acc", 3, last_was_use_stmt);
1627 return decode_oacc_directive ();
1630 else if (flag_openacc)
1632 verify_token_free ("$acc", 4, last_was_use_stmt);
1633 return decode_oacc_directive ();
1636 gcc_unreachable ();
1639 if (at_bol && c == ';')
1641 if (!(gfc_option.allow_std & GFC_STD_F2008))
1642 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1643 "statement");
1644 gfc_next_ascii_char (); /* Eat up the semicolon. */
1645 return ST_NONE;
1648 return decode_statement ();
1651 /* Assert next length characters to be equal to token in fixed form. */
1653 static bool
1654 verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
1656 int i;
1657 char c = gfc_next_char_literal (NONSTRING);
1659 for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
1660 gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
1662 if (c != ' ' && c != '0')
1664 gfc_buffer_error (false);
1665 gfc_error ("Bad continuation line at %C");
1666 return false;
1668 if (last_was_use_stmt)
1669 use_modules ();
1671 return true;
1674 /* Get the next statement in fixed-form source. */
1676 static gfc_statement
1677 next_fixed (void)
1679 int label, digit_flag, i;
1680 locus loc;
1681 gfc_char_t c;
1683 if (!gfc_at_bol ())
1684 return decode_statement ();
1686 /* Skip past the current label field, parsing a statement label if
1687 one is there. This is a weird number parser, since the number is
1688 contained within five columns and can have any kind of embedded
1689 spaces. We also check for characters that make the rest of the
1690 line a comment. */
1692 label = 0;
1693 digit_flag = 0;
1695 for (i = 0; i < 5; i++)
1697 c = gfc_next_char_literal (NONSTRING);
1699 switch (c)
1701 case ' ':
1702 break;
1704 case '0':
1705 case '1':
1706 case '2':
1707 case '3':
1708 case '4':
1709 case '5':
1710 case '6':
1711 case '7':
1712 case '8':
1713 case '9':
1714 label = label * 10 + ((unsigned char) c - '0');
1715 label_locus = gfc_current_locus;
1716 digit_flag = 1;
1717 break;
1719 /* Comments have already been skipped by the time we get
1720 here, except for GCC attributes and OpenMP directives. */
1722 case '*':
1723 c = gfc_next_char_literal (NONSTRING);
1725 if (TOLOWER (c) == 'g')
1727 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
1728 gcc_assert (TOLOWER (c) == "gcc$"[i]);
1730 return decode_gcc_attribute ();
1732 else if (c == '$')
1734 if ((flag_openmp || flag_openmp_simd)
1735 && !flag_openacc)
1737 if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
1738 return ST_NONE;
1739 return decode_omp_directive ();
1741 else if ((flag_openmp || flag_openmp_simd)
1742 && flag_openacc)
1744 c = gfc_next_char_literal(NONSTRING);
1745 if (c == 'o' || c == 'O')
1747 if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
1748 return ST_NONE;
1749 return decode_omp_directive ();
1751 else if (c == 'a' || c == 'A')
1753 if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
1754 return ST_NONE;
1755 return decode_oacc_directive ();
1758 else if (flag_openacc)
1760 if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
1761 return ST_NONE;
1762 return decode_oacc_directive ();
1765 gcc_fallthrough ();
1767 /* Comments have already been skipped by the time we get
1768 here so don't bother checking for them. */
1770 default:
1771 gfc_buffer_error (false);
1772 gfc_error ("Non-numeric character in statement label at %C");
1773 return ST_NONE;
1777 if (digit_flag)
1779 if (label == 0)
1780 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1781 else
1783 /* We've found a valid statement label. */
1784 gfc_statement_label = gfc_get_st_label (label);
1788 /* Since this line starts a statement, it cannot be a continuation
1789 of a previous statement. If we see something here besides a
1790 space or zero, it must be a bad continuation line. */
1792 c = gfc_next_char_literal (NONSTRING);
1793 if (c == '\n')
1794 goto blank_line;
1796 if (c != ' ' && c != '0')
1798 gfc_buffer_error (false);
1799 gfc_error ("Bad continuation line at %C");
1800 return ST_NONE;
1803 /* Now that we've taken care of the statement label columns, we have
1804 to make sure that the first nonblank character is not a '!'. If
1805 it is, the rest of the line is a comment. */
1809 loc = gfc_current_locus;
1810 c = gfc_next_char_literal (NONSTRING);
1812 while (gfc_is_whitespace (c));
1814 if (c == '!')
1815 goto blank_line;
1816 gfc_current_locus = loc;
1818 if (c == ';')
1820 if (digit_flag)
1821 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1822 else if (!(gfc_option.allow_std & GFC_STD_F2008))
1823 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1824 "statement");
1825 return ST_NONE;
1828 if (gfc_match_eos () == MATCH_YES)
1829 goto blank_line;
1831 /* At this point, we've got a nonblank statement to parse. */
1832 return decode_statement ();
1834 blank_line:
1835 if (digit_flag)
1836 gfc_error_now ("Statement label without statement at %L", &label_locus);
1838 gfc_current_locus.u.lb->truncated = 0;
1839 gfc_advance_line ();
1840 return ST_NONE;
1844 /* Return the next non-ST_NONE statement to the caller. We also worry
1845 about including files and the ends of include files at this stage. */
1847 static gfc_statement
1848 next_statement (void)
1850 gfc_statement st;
1851 locus old_locus;
1853 gfc_enforce_clean_symbol_state ();
1854 gfc_save_module_list ();
1856 gfc_new_block = NULL;
1858 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
1859 gfc_current_ns->old_data = gfc_current_ns->data;
1860 for (;;)
1862 gfc_statement_label = NULL;
1863 gfc_buffer_error (true);
1865 if (gfc_at_eol ())
1866 gfc_advance_line ();
1868 gfc_skip_comments ();
1870 if (gfc_at_end ())
1872 st = ST_NONE;
1873 break;
1876 if (gfc_define_undef_line ())
1877 continue;
1879 old_locus = gfc_current_locus;
1881 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
1883 if (st != ST_NONE)
1884 break;
1887 gfc_buffer_error (false);
1889 if (st == ST_GET_FCN_CHARACTERISTICS)
1891 if (gfc_statement_label != NULL)
1893 gfc_free_st_label (gfc_statement_label);
1894 gfc_statement_label = NULL;
1896 gfc_current_locus = old_locus;
1899 if (st != ST_NONE)
1900 check_statement_label (st);
1902 return st;
1906 /****************************** Parser ***********************************/
1908 /* The parser subroutines are of type 'try' that fail if the file ends
1909 unexpectedly. */
1911 /* Macros that expand to case-labels for various classes of
1912 statements. Start with executable statements that directly do
1913 things. */
1915 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1916 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1917 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1918 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1919 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1920 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1921 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1922 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1923 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1924 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: case ST_OMP_DEPOBJ: \
1925 case ST_OMP_TARGET_UPDATE: case ST_OMP_TARGET_ENTER_DATA: \
1926 case ST_OMP_TARGET_EXIT_DATA: case ST_OMP_ORDERED_DEPEND: case ST_OMP_ERROR: \
1927 case ST_OMP_INTEROP: \
1928 case ST_ERROR_STOP: case ST_OMP_SCAN: case ST_SYNC_ALL: \
1929 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1930 case ST_FORM_TEAM: case ST_CHANGE_TEAM: \
1931 case ST_END_TEAM: case ST_SYNC_TEAM: \
1932 case ST_EVENT_POST: case ST_EVENT_WAIT: case ST_FAIL_IMAGE: \
1933 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1934 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1936 /* Statements that mark other executable statements. */
1938 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1939 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1940 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1941 case ST_SELECT_RANK: case ST_OMP_PARALLEL: case ST_OMP_PARALLEL_MASKED: \
1942 case ST_OMP_PARALLEL_MASKED_TASKLOOP: \
1943 case ST_OMP_PARALLEL_MASKED_TASKLOOP_SIMD: case ST_OMP_PARALLEL_MASTER: \
1944 case ST_OMP_PARALLEL_MASTER_TASKLOOP: \
1945 case ST_OMP_PARALLEL_MASTER_TASKLOOP_SIMD: \
1946 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1947 case ST_OMP_CRITICAL: case ST_OMP_MASKED: case ST_OMP_MASKED_TASKLOOP: \
1948 case ST_OMP_MASKED_TASKLOOP_SIMD: \
1949 case ST_OMP_MASTER: case ST_OMP_MASTER_TASKLOOP: \
1950 case ST_OMP_MASTER_TASKLOOP_SIMD: case ST_OMP_SCOPE: case ST_OMP_SINGLE: \
1951 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1952 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1953 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1954 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1955 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1956 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1957 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1958 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1959 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1960 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1961 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1962 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1963 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1964 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1965 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_TARGET_PARALLEL: \
1966 case ST_OMP_TARGET_PARALLEL_DO: case ST_OMP_TARGET_PARALLEL_DO_SIMD: \
1967 case ST_OMP_TARGET_SIMD: case ST_OMP_TASKLOOP: case ST_OMP_TASKLOOP_SIMD: \
1968 case ST_OMP_LOOP: case ST_OMP_PARALLEL_LOOP: case ST_OMP_TEAMS_LOOP: \
1969 case ST_OMP_TARGET_PARALLEL_LOOP: case ST_OMP_TARGET_TEAMS_LOOP: \
1970 case ST_OMP_ALLOCATE_EXEC: case ST_OMP_ALLOCATORS: case ST_OMP_ASSUME: \
1971 case ST_OMP_TILE: case ST_OMP_UNROLL: case ST_OMP_DISPATCH: \
1972 case ST_CRITICAL: \
1973 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1974 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: \
1975 case ST_OACC_KERNELS_LOOP: case ST_OACC_SERIAL_LOOP: case ST_OACC_SERIAL: \
1976 case ST_OACC_ATOMIC
1978 /* Declaration statements */
1980 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1981 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1982 case ST_TYPE: case ST_INTERFACE: case ST_PROCEDURE
1984 /* OpenMP and OpenACC declaration statements, which may appear anywhere in
1985 the specification part. */
1987 #define case_omp_decl case ST_OMP_THREADPRIVATE: case ST_OMP_DECLARE_SIMD: \
1988 case ST_OMP_DECLARE_TARGET: case ST_OMP_DECLARE_REDUCTION: \
1989 case ST_OMP_DECLARE_VARIANT: case ST_OMP_ALLOCATE: case ST_OMP_ASSUMES: \
1990 case ST_OMP_REQUIRES: case ST_OACC_ROUTINE: case ST_OACC_DECLARE
1992 /* OpenMP statements that are followed by a structured block. */
1994 #define case_omp_structured_block case ST_OMP_ASSUME: case ST_OMP_PARALLEL: \
1995 case ST_OMP_PARALLEL_MASKED: case ST_OMP_PARALLEL_MASTER: \
1996 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_ORDERED: \
1997 case ST_OMP_CRITICAL: case ST_OMP_MASKED: case ST_OMP_MASTER: \
1998 case ST_OMP_SCOPE: case ST_OMP_SECTIONS: case ST_OMP_SINGLE: \
1999 case ST_OMP_TARGET: case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_PARALLEL: \
2000 case ST_OMP_TARGET_TEAMS: case ST_OMP_TEAMS: case ST_OMP_TASK: \
2001 case ST_OMP_TASKGROUP: \
2002 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE
2004 /* OpenMP statements that are followed by a do loop. */
2006 #define case_omp_do case ST_OMP_DISTRIBUTE: \
2007 case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
2008 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE_SIMD: \
2009 case ST_OMP_DO: case ST_OMP_DO_SIMD: case ST_OMP_LOOP: \
2010 case ST_OMP_PARALLEL_DO: case ST_OMP_PARALLEL_DO_SIMD: \
2011 case ST_OMP_PARALLEL_LOOP: case ST_OMP_PARALLEL_MASKED_TASKLOOP: \
2012 case ST_OMP_PARALLEL_MASKED_TASKLOOP_SIMD: \
2013 case ST_OMP_PARALLEL_MASTER_TASKLOOP: \
2014 case ST_OMP_PARALLEL_MASTER_TASKLOOP_SIMD: \
2015 case ST_OMP_MASKED_TASKLOOP: case ST_OMP_MASKED_TASKLOOP_SIMD: \
2016 case ST_OMP_MASTER_TASKLOOP: case ST_OMP_MASTER_TASKLOOP_SIMD: \
2017 case ST_OMP_SIMD: \
2018 case ST_OMP_TARGET_PARALLEL_DO: case ST_OMP_TARGET_PARALLEL_DO_SIMD: \
2019 case ST_OMP_TARGET_PARALLEL_LOOP: case ST_OMP_TARGET_SIMD: \
2020 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
2021 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
2022 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
2023 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: case ST_OMP_TARGET_TEAMS_LOOP: \
2024 case ST_OMP_TASKLOOP: case ST_OMP_TASKLOOP_SIMD: \
2025 case ST_OMP_TEAMS_DISTRIBUTE: case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
2026 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
2027 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: case ST_OMP_TEAMS_LOOP: \
2028 case ST_OMP_TILE: case ST_OMP_UNROLL
2030 /* Block end statements. Errors associated with interchanging these
2031 are detected in gfc_match_end(). */
2033 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
2034 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
2035 case ST_END_BLOCK: case ST_END_ASSOCIATE
2038 /* Push a new state onto the stack. */
2040 static void
2041 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
2043 p->state = new_state;
2044 p->previous = gfc_state_stack;
2045 p->sym = sym;
2046 p->head = p->tail = NULL;
2047 p->do_variable = NULL;
2048 if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
2049 p->ext.oacc_declare_clauses = NULL;
2051 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
2052 construct statement was accepted right before pushing the state. Thus,
2053 the construct's gfc_code is available as tail of the parent state. */
2054 gcc_assert (gfc_state_stack);
2055 p->construct = gfc_state_stack->tail;
2057 gfc_state_stack = p;
2061 /* Pop the current state. */
2062 static void
2063 pop_state (void)
2065 gfc_state_stack = gfc_state_stack->previous;
2069 /* Try to find the given state in the state stack. */
2071 bool
2072 gfc_find_state (gfc_compile_state state)
2074 gfc_state_data *p;
2076 for (p = gfc_state_stack; p; p = p->previous)
2077 if (p->state == state)
2078 break;
2080 return (p == NULL) ? false : true;
2084 /* Starts a new level in the statement list. */
2086 static gfc_code *
2087 new_level (gfc_code *q)
2089 gfc_code *p;
2091 p = q->block = gfc_get_code (EXEC_NOP);
2093 gfc_state_stack->head = gfc_state_stack->tail = p;
2095 return p;
2099 /* Add the current new_st code structure and adds it to the current
2100 program unit. As a side-effect, it zeroes the new_st. */
2102 static gfc_code *
2103 add_statement (void)
2105 gfc_code *p;
2107 p = XCNEW (gfc_code);
2108 *p = new_st;
2110 p->loc = gfc_current_locus;
2112 if (gfc_state_stack->head == NULL)
2113 gfc_state_stack->head = p;
2114 else
2115 gfc_state_stack->tail->next = p;
2117 while (p->next != NULL)
2118 p = p->next;
2120 gfc_state_stack->tail = p;
2122 gfc_clear_new_st ();
2124 return p;
2128 /* Frees everything associated with the current statement. */
2130 static void
2131 undo_new_statement (void)
2133 gfc_free_statements (new_st.block);
2134 gfc_free_statements (new_st.next);
2135 gfc_free_statement (&new_st);
2136 gfc_clear_new_st ();
2140 /* If the current statement has a statement label, make sure that it
2141 is allowed to, or should have one. */
2143 static void
2144 check_statement_label (gfc_statement st)
2146 gfc_sl_type type;
2148 if (gfc_statement_label == NULL)
2150 if (st == ST_FORMAT)
2151 gfc_error ("FORMAT statement at %L does not have a statement label",
2152 &new_st.loc);
2153 return;
2156 switch (st)
2158 case ST_END_PROGRAM:
2159 case ST_END_FUNCTION:
2160 case ST_END_SUBROUTINE:
2161 case ST_ENDDO:
2162 case ST_ENDIF:
2163 case ST_END_SELECT:
2164 case ST_END_CRITICAL:
2165 case ST_END_BLOCK:
2166 case ST_END_ASSOCIATE:
2167 case_executable:
2168 case_exec_markers:
2169 if (st == ST_ENDDO || st == ST_CONTINUE)
2170 type = ST_LABEL_DO_TARGET;
2171 else
2172 type = ST_LABEL_TARGET;
2173 break;
2175 case ST_FORMAT:
2176 type = ST_LABEL_FORMAT;
2177 break;
2179 /* Statement labels are not restricted from appearing on a
2180 particular line. However, there are plenty of situations
2181 where the resulting label can't be referenced. */
2183 default:
2184 type = ST_LABEL_BAD_TARGET;
2185 break;
2188 gfc_define_st_label (gfc_statement_label, type, &label_locus);
2190 new_st.here = gfc_statement_label;
2194 /* Figures out what the enclosing program unit is. This will be a
2195 function, subroutine, program, block data or module. */
2197 gfc_state_data *
2198 gfc_enclosing_unit (gfc_compile_state * result)
2200 gfc_state_data *p;
2202 for (p = gfc_state_stack; p; p = p->previous)
2203 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
2204 || p->state == COMP_MODULE || p->state == COMP_SUBMODULE
2205 || p->state == COMP_BLOCK_DATA || p->state == COMP_PROGRAM)
2208 if (result != NULL)
2209 *result = p->state;
2210 return p;
2213 if (result != NULL)
2214 *result = COMP_PROGRAM;
2215 return NULL;
2219 /* Translate a statement enum to a string. If strip_sentinel is true,
2220 the !$OMP/!$ACC sentinel is excluded. */
2222 const char *
2223 gfc_ascii_statement (gfc_statement st, bool strip_sentinel)
2225 const char *p;
2227 switch (st)
2229 case ST_ARITHMETIC_IF:
2230 p = _("arithmetic IF");
2231 break;
2232 case ST_ALLOCATE:
2233 p = "ALLOCATE";
2234 break;
2235 case ST_ASSOCIATE:
2236 p = "ASSOCIATE";
2237 break;
2238 case ST_ATTR_DECL:
2239 p = _("attribute declaration");
2240 break;
2241 case ST_BACKSPACE:
2242 p = "BACKSPACE";
2243 break;
2244 case ST_BLOCK:
2245 p = "BLOCK";
2246 break;
2247 case ST_BLOCK_DATA:
2248 p = "BLOCK DATA";
2249 break;
2250 case ST_CALL:
2251 p = "CALL";
2252 break;
2253 case ST_CASE:
2254 p = "CASE";
2255 break;
2256 case ST_CLOSE:
2257 p = "CLOSE";
2258 break;
2259 case ST_COMMON:
2260 p = "COMMON";
2261 break;
2262 case ST_CONTINUE:
2263 p = "CONTINUE";
2264 break;
2265 case ST_CONTAINS:
2266 p = "CONTAINS";
2267 break;
2268 case ST_CRITICAL:
2269 p = "CRITICAL";
2270 break;
2271 case ST_CYCLE:
2272 p = "CYCLE";
2273 break;
2274 case ST_DATA_DECL:
2275 p = _("data declaration");
2276 break;
2277 case ST_DATA:
2278 p = "DATA";
2279 break;
2280 case ST_DEALLOCATE:
2281 p = "DEALLOCATE";
2282 break;
2283 case ST_MAP:
2284 p = "MAP";
2285 break;
2286 case ST_UNION:
2287 p = "UNION";
2288 break;
2289 case ST_STRUCTURE_DECL:
2290 p = "STRUCTURE";
2291 break;
2292 case ST_DERIVED_DECL:
2293 p = _("derived type declaration");
2294 break;
2295 case ST_DO:
2296 p = "DO";
2297 break;
2298 case ST_ELSE:
2299 p = "ELSE";
2300 break;
2301 case ST_ELSEIF:
2302 p = "ELSE IF";
2303 break;
2304 case ST_ELSEWHERE:
2305 p = "ELSEWHERE";
2306 break;
2307 case ST_EVENT_POST:
2308 p = "EVENT POST";
2309 break;
2310 case ST_EVENT_WAIT:
2311 p = "EVENT WAIT";
2312 break;
2313 case ST_FAIL_IMAGE:
2314 p = "FAIL IMAGE";
2315 break;
2316 case ST_CHANGE_TEAM:
2317 p = "CHANGE TEAM";
2318 break;
2319 case ST_END_TEAM:
2320 p = "END TEAM";
2321 break;
2322 case ST_FORM_TEAM:
2323 p = "FORM TEAM";
2324 break;
2325 case ST_SYNC_TEAM:
2326 p = "SYNC TEAM";
2327 break;
2328 case ST_END_ASSOCIATE:
2329 p = "END ASSOCIATE";
2330 break;
2331 case ST_END_BLOCK:
2332 p = "END BLOCK";
2333 break;
2334 case ST_END_BLOCK_DATA:
2335 p = "END BLOCK DATA";
2336 break;
2337 case ST_END_CRITICAL:
2338 p = "END CRITICAL";
2339 break;
2340 case ST_ENDDO:
2341 p = "END DO";
2342 break;
2343 case ST_END_FILE:
2344 p = "END FILE";
2345 break;
2346 case ST_END_FORALL:
2347 p = "END FORALL";
2348 break;
2349 case ST_END_FUNCTION:
2350 p = "END FUNCTION";
2351 break;
2352 case ST_ENDIF:
2353 p = "END IF";
2354 break;
2355 case ST_END_INTERFACE:
2356 p = "END INTERFACE";
2357 break;
2358 case ST_END_MODULE:
2359 p = "END MODULE";
2360 break;
2361 case ST_END_SUBMODULE:
2362 p = "END SUBMODULE";
2363 break;
2364 case ST_END_PROGRAM:
2365 p = "END PROGRAM";
2366 break;
2367 case ST_END_SELECT:
2368 p = "END SELECT";
2369 break;
2370 case ST_END_SUBROUTINE:
2371 p = "END SUBROUTINE";
2372 break;
2373 case ST_END_WHERE:
2374 p = "END WHERE";
2375 break;
2376 case ST_END_STRUCTURE:
2377 p = "END STRUCTURE";
2378 break;
2379 case ST_END_UNION:
2380 p = "END UNION";
2381 break;
2382 case ST_END_MAP:
2383 p = "END MAP";
2384 break;
2385 case ST_END_TYPE:
2386 p = "END TYPE";
2387 break;
2388 case ST_ENTRY:
2389 p = "ENTRY";
2390 break;
2391 case ST_EQUIVALENCE:
2392 p = "EQUIVALENCE";
2393 break;
2394 case ST_ERROR_STOP:
2395 p = "ERROR STOP";
2396 break;
2397 case ST_EXIT:
2398 p = "EXIT";
2399 break;
2400 case ST_FLUSH:
2401 p = "FLUSH";
2402 break;
2403 case ST_FORALL_BLOCK: /* Fall through */
2404 case ST_FORALL:
2405 p = "FORALL";
2406 break;
2407 case ST_FORMAT:
2408 p = "FORMAT";
2409 break;
2410 case ST_FUNCTION:
2411 p = "FUNCTION";
2412 break;
2413 case ST_GENERIC:
2414 p = "GENERIC";
2415 break;
2416 case ST_GOTO:
2417 p = "GOTO";
2418 break;
2419 case ST_IF_BLOCK:
2420 p = _("block IF");
2421 break;
2422 case ST_IMPLICIT:
2423 p = "IMPLICIT";
2424 break;
2425 case ST_IMPLICIT_NONE:
2426 p = "IMPLICIT NONE";
2427 break;
2428 case ST_IMPLIED_ENDDO:
2429 p = _("implied END DO");
2430 break;
2431 case ST_IMPORT:
2432 p = "IMPORT";
2433 break;
2434 case ST_INQUIRE:
2435 p = "INQUIRE";
2436 break;
2437 case ST_INTERFACE:
2438 p = "INTERFACE";
2439 break;
2440 case ST_LOCK:
2441 p = "LOCK";
2442 break;
2443 case ST_PARAMETER:
2444 p = "PARAMETER";
2445 break;
2446 case ST_PRIVATE:
2447 p = "PRIVATE";
2448 break;
2449 case ST_PUBLIC:
2450 p = "PUBLIC";
2451 break;
2452 case ST_MODULE:
2453 p = "MODULE";
2454 break;
2455 case ST_SUBMODULE:
2456 p = "SUBMODULE";
2457 break;
2458 case ST_PAUSE:
2459 p = "PAUSE";
2460 break;
2461 case ST_MODULE_PROC:
2462 p = "MODULE PROCEDURE";
2463 break;
2464 case ST_NAMELIST:
2465 p = "NAMELIST";
2466 break;
2467 case ST_NULLIFY:
2468 p = "NULLIFY";
2469 break;
2470 case ST_OPEN:
2471 p = "OPEN";
2472 break;
2473 case ST_PROGRAM:
2474 p = "PROGRAM";
2475 break;
2476 case ST_PROCEDURE:
2477 p = "PROCEDURE";
2478 break;
2479 case ST_READ:
2480 p = "READ";
2481 break;
2482 case ST_RETURN:
2483 p = "RETURN";
2484 break;
2485 case ST_REWIND:
2486 p = "REWIND";
2487 break;
2488 case ST_STOP:
2489 p = "STOP";
2490 break;
2491 case ST_SYNC_ALL:
2492 p = "SYNC ALL";
2493 break;
2494 case ST_SYNC_IMAGES:
2495 p = "SYNC IMAGES";
2496 break;
2497 case ST_SYNC_MEMORY:
2498 p = "SYNC MEMORY";
2499 break;
2500 case ST_SUBROUTINE:
2501 p = "SUBROUTINE";
2502 break;
2503 case ST_TYPE:
2504 p = "TYPE";
2505 break;
2506 case ST_UNLOCK:
2507 p = "UNLOCK";
2508 break;
2509 case ST_USE:
2510 p = "USE";
2511 break;
2512 case ST_WHERE_BLOCK: /* Fall through */
2513 case ST_WHERE:
2514 p = "WHERE";
2515 break;
2516 case ST_WAIT:
2517 p = "WAIT";
2518 break;
2519 case ST_WRITE:
2520 p = "WRITE";
2521 break;
2522 case ST_ASSIGNMENT:
2523 p = _("assignment");
2524 break;
2525 case ST_POINTER_ASSIGNMENT:
2526 p = _("pointer assignment");
2527 break;
2528 case ST_SELECT_CASE:
2529 p = "SELECT CASE";
2530 break;
2531 case ST_SELECT_TYPE:
2532 p = "SELECT TYPE";
2533 break;
2534 case ST_SELECT_RANK:
2535 p = "SELECT RANK";
2536 break;
2537 case ST_TYPE_IS:
2538 p = "TYPE IS";
2539 break;
2540 case ST_CLASS_IS:
2541 p = "CLASS IS";
2542 break;
2543 case ST_RANK:
2544 p = "RANK";
2545 break;
2546 case ST_SEQUENCE:
2547 p = "SEQUENCE";
2548 break;
2549 case ST_SIMPLE_IF:
2550 p = _("simple IF");
2551 break;
2552 case ST_STATEMENT_FUNCTION:
2553 p = "STATEMENT FUNCTION";
2554 break;
2555 case ST_LABEL_ASSIGNMENT:
2556 p = "LABEL ASSIGNMENT";
2557 break;
2558 case ST_ENUM:
2559 p = "ENUM DEFINITION";
2560 break;
2561 case ST_ENUMERATOR:
2562 p = "ENUMERATOR DEFINITION";
2563 break;
2564 case ST_END_ENUM:
2565 p = "END ENUM";
2566 break;
2567 case ST_OACC_PARALLEL_LOOP:
2568 p = "!$ACC PARALLEL LOOP";
2569 break;
2570 case ST_OACC_END_PARALLEL_LOOP:
2571 p = "!$ACC END PARALLEL LOOP";
2572 break;
2573 case ST_OACC_PARALLEL:
2574 p = "!$ACC PARALLEL";
2575 break;
2576 case ST_OACC_END_PARALLEL:
2577 p = "!$ACC END PARALLEL";
2578 break;
2579 case ST_OACC_KERNELS:
2580 p = "!$ACC KERNELS";
2581 break;
2582 case ST_OACC_END_KERNELS:
2583 p = "!$ACC END KERNELS";
2584 break;
2585 case ST_OACC_KERNELS_LOOP:
2586 p = "!$ACC KERNELS LOOP";
2587 break;
2588 case ST_OACC_END_KERNELS_LOOP:
2589 p = "!$ACC END KERNELS LOOP";
2590 break;
2591 case ST_OACC_SERIAL_LOOP:
2592 p = "!$ACC SERIAL LOOP";
2593 break;
2594 case ST_OACC_END_SERIAL_LOOP:
2595 p = "!$ACC END SERIAL LOOP";
2596 break;
2597 case ST_OACC_SERIAL:
2598 p = "!$ACC SERIAL";
2599 break;
2600 case ST_OACC_END_SERIAL:
2601 p = "!$ACC END SERIAL";
2602 break;
2603 case ST_OACC_DATA:
2604 p = "!$ACC DATA";
2605 break;
2606 case ST_OACC_END_DATA:
2607 p = "!$ACC END DATA";
2608 break;
2609 case ST_OACC_HOST_DATA:
2610 p = "!$ACC HOST_DATA";
2611 break;
2612 case ST_OACC_END_HOST_DATA:
2613 p = "!$ACC END HOST_DATA";
2614 break;
2615 case ST_OACC_LOOP:
2616 p = "!$ACC LOOP";
2617 break;
2618 case ST_OACC_END_LOOP:
2619 p = "!$ACC END LOOP";
2620 break;
2621 case ST_OACC_DECLARE:
2622 p = "!$ACC DECLARE";
2623 break;
2624 case ST_OACC_UPDATE:
2625 p = "!$ACC UPDATE";
2626 break;
2627 case ST_OACC_WAIT:
2628 p = "!$ACC WAIT";
2629 break;
2630 case ST_OACC_CACHE:
2631 p = "!$ACC CACHE";
2632 break;
2633 case ST_OACC_ENTER_DATA:
2634 p = "!$ACC ENTER DATA";
2635 break;
2636 case ST_OACC_EXIT_DATA:
2637 p = "!$ACC EXIT DATA";
2638 break;
2639 case ST_OACC_ROUTINE:
2640 p = "!$ACC ROUTINE";
2641 break;
2642 case ST_OACC_ATOMIC:
2643 p = "!$ACC ATOMIC";
2644 break;
2645 case ST_OACC_END_ATOMIC:
2646 p = "!$ACC END ATOMIC";
2647 break;
2648 case ST_OMP_ALLOCATE:
2649 case ST_OMP_ALLOCATE_EXEC:
2650 p = "!$OMP ALLOCATE";
2651 break;
2652 case ST_OMP_ALLOCATORS:
2653 p = "!$OMP ALLOCATORS";
2654 break;
2655 case ST_OMP_ASSUME:
2656 p = "!$OMP ASSUME";
2657 break;
2658 case ST_OMP_ASSUMES:
2659 p = "!$OMP ASSUMES";
2660 break;
2661 case ST_OMP_ATOMIC:
2662 p = "!$OMP ATOMIC";
2663 break;
2664 case ST_OMP_BARRIER:
2665 p = "!$OMP BARRIER";
2666 break;
2667 case ST_OMP_BEGIN_METADIRECTIVE:
2668 p = "!$OMP BEGIN METADIRECTIVE";
2669 break;
2670 case ST_OMP_CANCEL:
2671 p = "!$OMP CANCEL";
2672 break;
2673 case ST_OMP_CANCELLATION_POINT:
2674 p = "!$OMP CANCELLATION POINT";
2675 break;
2676 case ST_OMP_CRITICAL:
2677 p = "!$OMP CRITICAL";
2678 break;
2679 case ST_OMP_DECLARE_REDUCTION:
2680 p = "!$OMP DECLARE REDUCTION";
2681 break;
2682 case ST_OMP_DECLARE_SIMD:
2683 p = "!$OMP DECLARE SIMD";
2684 break;
2685 case ST_OMP_DECLARE_TARGET:
2686 p = "!$OMP DECLARE TARGET";
2687 break;
2688 case ST_OMP_DECLARE_VARIANT:
2689 p = "!$OMP DECLARE VARIANT";
2690 break;
2691 case ST_OMP_DEPOBJ:
2692 p = "!$OMP DEPOBJ";
2693 break;
2694 case ST_OMP_DISPATCH:
2695 p = "!$OMP DISPATCH";
2696 break;
2697 case ST_OMP_DISTRIBUTE:
2698 p = "!$OMP DISTRIBUTE";
2699 break;
2700 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
2701 p = "!$OMP DISTRIBUTE PARALLEL DO";
2702 break;
2703 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
2704 p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
2705 break;
2706 case ST_OMP_DISTRIBUTE_SIMD:
2707 p = "!$OMP DISTRIBUTE SIMD";
2708 break;
2709 case ST_OMP_DO:
2710 p = "!$OMP DO";
2711 break;
2712 case ST_OMP_DO_SIMD:
2713 p = "!$OMP DO SIMD";
2714 break;
2715 case ST_OMP_END_ALLOCATORS:
2716 p = "!$OMP END ALLOCATORS";
2717 break;
2718 case ST_OMP_END_ASSUME:
2719 p = "!$OMP END ASSUME";
2720 break;
2721 case ST_OMP_END_ATOMIC:
2722 p = "!$OMP END ATOMIC";
2723 break;
2724 case ST_OMP_END_CRITICAL:
2725 p = "!$OMP END CRITICAL";
2726 break;
2727 case ST_OMP_END_DISPATCH:
2728 p = "!$OMP END DISPATCH";
2729 break;
2730 case ST_OMP_END_DISTRIBUTE:
2731 p = "!$OMP END DISTRIBUTE";
2732 break;
2733 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
2734 p = "!$OMP END DISTRIBUTE PARALLEL DO";
2735 break;
2736 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
2737 p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
2738 break;
2739 case ST_OMP_END_DISTRIBUTE_SIMD:
2740 p = "!$OMP END DISTRIBUTE SIMD";
2741 break;
2742 case ST_OMP_END_DO:
2743 p = "!$OMP END DO";
2744 break;
2745 case ST_OMP_END_DO_SIMD:
2746 p = "!$OMP END DO SIMD";
2747 break;
2748 case ST_OMP_END_SCOPE:
2749 p = "!$OMP END SCOPE";
2750 break;
2751 case ST_OMP_END_SIMD:
2752 p = "!$OMP END SIMD";
2753 break;
2754 case ST_OMP_END_LOOP:
2755 p = "!$OMP END LOOP";
2756 break;
2757 case ST_OMP_END_MASKED:
2758 p = "!$OMP END MASKED";
2759 break;
2760 case ST_OMP_END_MASKED_TASKLOOP:
2761 p = "!$OMP END MASKED TASKLOOP";
2762 break;
2763 case ST_OMP_END_MASKED_TASKLOOP_SIMD:
2764 p = "!$OMP END MASKED TASKLOOP SIMD";
2765 break;
2766 case ST_OMP_END_MASTER:
2767 p = "!$OMP END MASTER";
2768 break;
2769 case ST_OMP_END_MASTER_TASKLOOP:
2770 p = "!$OMP END MASTER TASKLOOP";
2771 break;
2772 case ST_OMP_END_MASTER_TASKLOOP_SIMD:
2773 p = "!$OMP END MASTER TASKLOOP SIMD";
2774 break;
2775 case ST_OMP_END_METADIRECTIVE:
2776 p = "!$OMP END METADIRECTIVE";
2777 break;
2778 case ST_OMP_END_ORDERED:
2779 p = "!$OMP END ORDERED";
2780 break;
2781 case ST_OMP_END_PARALLEL:
2782 p = "!$OMP END PARALLEL";
2783 break;
2784 case ST_OMP_END_PARALLEL_DO:
2785 p = "!$OMP END PARALLEL DO";
2786 break;
2787 case ST_OMP_END_PARALLEL_DO_SIMD:
2788 p = "!$OMP END PARALLEL DO SIMD";
2789 break;
2790 case ST_OMP_END_PARALLEL_LOOP:
2791 p = "!$OMP END PARALLEL LOOP";
2792 break;
2793 case ST_OMP_END_PARALLEL_MASKED:
2794 p = "!$OMP END PARALLEL MASKED";
2795 break;
2796 case ST_OMP_END_PARALLEL_MASKED_TASKLOOP:
2797 p = "!$OMP END PARALLEL MASKED TASKLOOP";
2798 break;
2799 case ST_OMP_END_PARALLEL_MASKED_TASKLOOP_SIMD:
2800 p = "!$OMP END PARALLEL MASKED TASKLOOP SIMD";
2801 break;
2802 case ST_OMP_END_PARALLEL_MASTER:
2803 p = "!$OMP END PARALLEL MASTER";
2804 break;
2805 case ST_OMP_END_PARALLEL_MASTER_TASKLOOP:
2806 p = "!$OMP END PARALLEL MASTER TASKLOOP";
2807 break;
2808 case ST_OMP_END_PARALLEL_MASTER_TASKLOOP_SIMD:
2809 p = "!$OMP END PARALLEL MASTER TASKLOOP SIMD";
2810 break;
2811 case ST_OMP_END_PARALLEL_SECTIONS:
2812 p = "!$OMP END PARALLEL SECTIONS";
2813 break;
2814 case ST_OMP_END_PARALLEL_WORKSHARE:
2815 p = "!$OMP END PARALLEL WORKSHARE";
2816 break;
2817 case ST_OMP_END_SECTIONS:
2818 p = "!$OMP END SECTIONS";
2819 break;
2820 case ST_OMP_END_SINGLE:
2821 p = "!$OMP END SINGLE";
2822 break;
2823 case ST_OMP_END_TASK:
2824 p = "!$OMP END TASK";
2825 break;
2826 case ST_OMP_END_TARGET:
2827 p = "!$OMP END TARGET";
2828 break;
2829 case ST_OMP_END_TARGET_DATA:
2830 p = "!$OMP END TARGET DATA";
2831 break;
2832 case ST_OMP_END_TARGET_PARALLEL:
2833 p = "!$OMP END TARGET PARALLEL";
2834 break;
2835 case ST_OMP_END_TARGET_PARALLEL_DO:
2836 p = "!$OMP END TARGET PARALLEL DO";
2837 break;
2838 case ST_OMP_END_TARGET_PARALLEL_DO_SIMD:
2839 p = "!$OMP END TARGET PARALLEL DO SIMD";
2840 break;
2841 case ST_OMP_END_TARGET_PARALLEL_LOOP:
2842 p = "!$OMP END TARGET PARALLEL LOOP";
2843 break;
2844 case ST_OMP_END_TARGET_SIMD:
2845 p = "!$OMP END TARGET SIMD";
2846 break;
2847 case ST_OMP_END_TARGET_TEAMS:
2848 p = "!$OMP END TARGET TEAMS";
2849 break;
2850 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
2851 p = "!$OMP END TARGET TEAMS DISTRIBUTE";
2852 break;
2853 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2854 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2855 break;
2856 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2857 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2858 break;
2859 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
2860 p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2861 break;
2862 case ST_OMP_END_TARGET_TEAMS_LOOP:
2863 p = "!$OMP END TARGET TEAMS LOOP";
2864 break;
2865 case ST_OMP_END_TASKGROUP:
2866 p = "!$OMP END TASKGROUP";
2867 break;
2868 case ST_OMP_END_TASKLOOP:
2869 p = "!$OMP END TASKLOOP";
2870 break;
2871 case ST_OMP_END_TASKLOOP_SIMD:
2872 p = "!$OMP END TASKLOOP SIMD";
2873 break;
2874 case ST_OMP_END_TEAMS:
2875 p = "!$OMP END TEAMS";
2876 break;
2877 case ST_OMP_END_TEAMS_DISTRIBUTE:
2878 p = "!$OMP END TEAMS DISTRIBUTE";
2879 break;
2880 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
2881 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2882 break;
2883 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2884 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2885 break;
2886 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
2887 p = "!$OMP END TEAMS DISTRIBUTE SIMD";
2888 break;
2889 case ST_OMP_END_TEAMS_LOOP:
2890 p = "!$OMP END TEAMS LOOP";
2891 break;
2892 case ST_OMP_END_TILE:
2893 p = "!$OMP END TILE";
2894 break;
2895 case ST_OMP_END_UNROLL:
2896 p = "!$OMP END UNROLL";
2897 break;
2898 case ST_OMP_END_WORKSHARE:
2899 p = "!$OMP END WORKSHARE";
2900 break;
2901 case ST_OMP_ERROR:
2902 p = "!$OMP ERROR";
2903 break;
2904 case ST_OMP_FLUSH:
2905 p = "!$OMP FLUSH";
2906 break;
2907 case ST_OMP_INTEROP:
2908 p = "!$OMP INTEROP";
2909 break;
2910 case ST_OMP_LOOP:
2911 p = "!$OMP LOOP";
2912 break;
2913 case ST_OMP_MASKED:
2914 p = "!$OMP MASKED";
2915 break;
2916 case ST_OMP_MASKED_TASKLOOP:
2917 p = "!$OMP MASKED TASKLOOP";
2918 break;
2919 case ST_OMP_MASKED_TASKLOOP_SIMD:
2920 p = "!$OMP MASKED TASKLOOP SIMD";
2921 break;
2922 case ST_OMP_MASTER:
2923 p = "!$OMP MASTER";
2924 break;
2925 case ST_OMP_MASTER_TASKLOOP:
2926 p = "!$OMP MASTER TASKLOOP";
2927 break;
2928 case ST_OMP_MASTER_TASKLOOP_SIMD:
2929 p = "!$OMP MASTER TASKLOOP SIMD";
2930 break;
2931 case ST_OMP_METADIRECTIVE:
2932 p = "!$OMP METADIRECTIVE";
2933 break;
2934 case ST_OMP_ORDERED:
2935 case ST_OMP_ORDERED_DEPEND:
2936 p = "!$OMP ORDERED";
2937 break;
2938 case ST_OMP_NOTHING:
2939 /* Note: gfc_match_omp_nothing returns ST_NONE. */
2940 p = "!$OMP NOTHING";
2941 break;
2942 case ST_OMP_PARALLEL:
2943 p = "!$OMP PARALLEL";
2944 break;
2945 case ST_OMP_PARALLEL_DO:
2946 p = "!$OMP PARALLEL DO";
2947 break;
2948 case ST_OMP_PARALLEL_LOOP:
2949 p = "!$OMP PARALLEL LOOP";
2950 break;
2951 case ST_OMP_PARALLEL_DO_SIMD:
2952 p = "!$OMP PARALLEL DO SIMD";
2953 break;
2954 case ST_OMP_PARALLEL_MASKED:
2955 p = "!$OMP PARALLEL MASKED";
2956 break;
2957 case ST_OMP_PARALLEL_MASKED_TASKLOOP:
2958 p = "!$OMP PARALLEL MASKED TASKLOOP";
2959 break;
2960 case ST_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
2961 p = "!$OMP PARALLEL MASKED TASKLOOP SIMD";
2962 break;
2963 case ST_OMP_PARALLEL_MASTER:
2964 p = "!$OMP PARALLEL MASTER";
2965 break;
2966 case ST_OMP_PARALLEL_MASTER_TASKLOOP:
2967 p = "!$OMP PARALLEL MASTER TASKLOOP";
2968 break;
2969 case ST_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
2970 p = "!$OMP PARALLEL MASTER TASKLOOP SIMD";
2971 break;
2972 case ST_OMP_PARALLEL_SECTIONS:
2973 p = "!$OMP PARALLEL SECTIONS";
2974 break;
2975 case ST_OMP_PARALLEL_WORKSHARE:
2976 p = "!$OMP PARALLEL WORKSHARE";
2977 break;
2978 case ST_OMP_REQUIRES:
2979 p = "!$OMP REQUIRES";
2980 break;
2981 case ST_OMP_SCAN:
2982 p = "!$OMP SCAN";
2983 break;
2984 case ST_OMP_SCOPE:
2985 p = "!$OMP SCOPE";
2986 break;
2987 case ST_OMP_SECTIONS:
2988 p = "!$OMP SECTIONS";
2989 break;
2990 case ST_OMP_SECTION:
2991 p = "!$OMP SECTION";
2992 break;
2993 case ST_OMP_SIMD:
2994 p = "!$OMP SIMD";
2995 break;
2996 case ST_OMP_SINGLE:
2997 p = "!$OMP SINGLE";
2998 break;
2999 case ST_OMP_TARGET:
3000 p = "!$OMP TARGET";
3001 break;
3002 case ST_OMP_TARGET_DATA:
3003 p = "!$OMP TARGET DATA";
3004 break;
3005 case ST_OMP_TARGET_ENTER_DATA:
3006 p = "!$OMP TARGET ENTER DATA";
3007 break;
3008 case ST_OMP_TARGET_EXIT_DATA:
3009 p = "!$OMP TARGET EXIT DATA";
3010 break;
3011 case ST_OMP_TARGET_PARALLEL:
3012 p = "!$OMP TARGET PARALLEL";
3013 break;
3014 case ST_OMP_TARGET_PARALLEL_DO:
3015 p = "!$OMP TARGET PARALLEL DO";
3016 break;
3017 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
3018 p = "!$OMP TARGET PARALLEL DO SIMD";
3019 break;
3020 case ST_OMP_TARGET_PARALLEL_LOOP:
3021 p = "!$OMP TARGET PARALLEL LOOP";
3022 break;
3023 case ST_OMP_TARGET_SIMD:
3024 p = "!$OMP TARGET SIMD";
3025 break;
3026 case ST_OMP_TARGET_TEAMS:
3027 p = "!$OMP TARGET TEAMS";
3028 break;
3029 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
3030 p = "!$OMP TARGET TEAMS DISTRIBUTE";
3031 break;
3032 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
3033 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
3034 break;
3035 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
3036 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
3037 break;
3038 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
3039 p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
3040 break;
3041 case ST_OMP_TARGET_TEAMS_LOOP:
3042 p = "!$OMP TARGET TEAMS LOOP";
3043 break;
3044 case ST_OMP_TARGET_UPDATE:
3045 p = "!$OMP TARGET UPDATE";
3046 break;
3047 case ST_OMP_TASK:
3048 p = "!$OMP TASK";
3049 break;
3050 case ST_OMP_TASKGROUP:
3051 p = "!$OMP TASKGROUP";
3052 break;
3053 case ST_OMP_TASKLOOP:
3054 p = "!$OMP TASKLOOP";
3055 break;
3056 case ST_OMP_TASKLOOP_SIMD:
3057 p = "!$OMP TASKLOOP SIMD";
3058 break;
3059 case ST_OMP_TASKWAIT:
3060 p = "!$OMP TASKWAIT";
3061 break;
3062 case ST_OMP_TASKYIELD:
3063 p = "!$OMP TASKYIELD";
3064 break;
3065 case ST_OMP_TEAMS:
3066 p = "!$OMP TEAMS";
3067 break;
3068 case ST_OMP_TEAMS_DISTRIBUTE:
3069 p = "!$OMP TEAMS DISTRIBUTE";
3070 break;
3071 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
3072 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
3073 break;
3074 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
3075 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
3076 break;
3077 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
3078 p = "!$OMP TEAMS DISTRIBUTE SIMD";
3079 break;
3080 case ST_OMP_TEAMS_LOOP:
3081 p = "!$OMP TEAMS LOOP";
3082 break;
3083 case ST_OMP_THREADPRIVATE:
3084 p = "!$OMP THREADPRIVATE";
3085 break;
3086 case ST_OMP_TILE:
3087 p = "!$OMP TILE";
3088 break;
3089 case ST_OMP_UNROLL:
3090 p = "!$OMP UNROLL";
3091 break;
3092 case ST_OMP_WORKSHARE:
3093 p = "!$OMP WORKSHARE";
3094 break;
3095 default:
3096 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
3099 if (strip_sentinel && p[0] == '!')
3100 return p + strlen ("!$OMP ");
3101 return p;
3105 /* Create a symbol for the main program and assign it to ns->proc_name. */
3107 static void
3108 main_program_symbol (gfc_namespace *ns, const char *name)
3110 gfc_symbol *main_program;
3111 symbol_attribute attr;
3113 gfc_get_symbol (name, ns, &main_program);
3114 gfc_clear_attr (&attr);
3115 attr.flavor = FL_PROGRAM;
3116 attr.proc = PROC_UNKNOWN;
3117 attr.subroutine = 1;
3118 attr.access = ACCESS_PUBLIC;
3119 attr.is_main_program = 1;
3120 main_program->attr = attr;
3121 main_program->declared_at = gfc_current_locus;
3122 ns->proc_name = main_program;
3123 gfc_commit_symbols ();
3127 /* Do whatever is necessary to accept the last statement. */
3129 static void
3130 accept_statement (gfc_statement st)
3132 switch (st)
3134 case ST_IMPLICIT_NONE:
3135 case ST_IMPLICIT:
3136 break;
3138 case ST_FUNCTION:
3139 case ST_SUBROUTINE:
3140 case ST_MODULE:
3141 case ST_SUBMODULE:
3142 gfc_current_ns->proc_name = gfc_new_block;
3143 break;
3145 /* If the statement is the end of a block, lay down a special code
3146 that allows a branch to the end of the block from within the
3147 construct. IF and SELECT are treated differently from DO
3148 (where EXEC_NOP is added inside the loop) for two
3149 reasons:
3150 1. END DO has a meaning in the sense that after a GOTO to
3151 it, the loop counter must be increased.
3152 2. IF blocks and SELECT blocks can consist of multiple
3153 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
3154 Putting the label before the END IF would make the jump
3155 from, say, the ELSE IF block to the END IF illegal. */
3157 case ST_ENDIF:
3158 case ST_END_SELECT:
3159 case ST_END_CRITICAL:
3160 if (gfc_statement_label != NULL)
3162 new_st.op = EXEC_END_NESTED_BLOCK;
3163 add_statement ();
3165 break;
3167 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
3168 one parallel block. Thus, we add the special code to the nested block
3169 itself, instead of the parent one. */
3170 case ST_END_BLOCK:
3171 case ST_END_ASSOCIATE:
3172 if (gfc_statement_label != NULL)
3174 new_st.op = EXEC_END_BLOCK;
3175 add_statement ();
3177 break;
3179 /* The end-of-program unit statements do not get the special
3180 marker and require a statement of some sort if they are a
3181 branch target. */
3183 case ST_END_PROGRAM:
3184 case ST_END_FUNCTION:
3185 case ST_END_SUBROUTINE:
3186 if (gfc_statement_label != NULL)
3188 new_st.op = EXEC_RETURN;
3189 add_statement ();
3191 else
3193 new_st.op = EXEC_END_PROCEDURE;
3194 add_statement ();
3197 break;
3199 case ST_ENTRY:
3200 case ST_OMP_METADIRECTIVE:
3201 case ST_OMP_BEGIN_METADIRECTIVE:
3202 case_executable:
3203 case_exec_markers:
3204 add_statement ();
3205 break;
3207 default:
3208 break;
3211 gfc_commit_symbols ();
3212 gfc_warning_check ();
3213 gfc_clear_new_st ();
3217 /* Undo anything tentative that has been built for the current statement,
3218 except if a gfc_charlen structure has been added to current namespace's
3219 list of gfc_charlen structure. */
3221 static void
3222 reject_statement (void)
3224 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
3225 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
3226 gfc_drop_interface_elements_before (current_interface_ptr,
3227 previous_interface_head);
3229 gfc_reject_data (gfc_current_ns);
3231 /* Don't queue use-association of a module if we reject the use statement. */
3232 gfc_restore_old_module_list ();
3234 gfc_new_block = NULL;
3235 gfc_undo_symbols ();
3236 gfc_clear_warning ();
3237 undo_new_statement ();
3241 /* Generic complaint about an out of order statement. We also do
3242 whatever is necessary to clean up. */
3244 static void
3245 unexpected_statement (gfc_statement st)
3247 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
3249 reject_statement ();
3253 /* Given the next statement seen by the matcher, make sure that it is
3254 in proper order with the last. This subroutine is initialized by
3255 calling it with an argument of ST_NONE. If there is a problem, we
3256 issue an error and return false. Otherwise we return true.
3258 Individual parsers need to verify that the statements seen are
3259 valid before calling here, i.e., ENTRY statements are not allowed in
3260 INTERFACE blocks. The following diagram is taken from the standard:
3262 +---------------------------------------+
3263 | program subroutine function module |
3264 +---------------------------------------+
3265 | use |
3266 +---------------------------------------+
3267 | import |
3268 +---------------------------------------+
3269 | | implicit none |
3270 | +-----------+------------------+
3271 | | parameter | implicit |
3272 | +-----------+------------------+
3273 | format | | derived type |
3274 | entry | parameter | interface |
3275 | | data | specification |
3276 | | | statement func |
3277 | +-----------+------------------+
3278 | | data | executable |
3279 +--------+-----------+------------------+
3280 | contains |
3281 +---------------------------------------+
3282 | internal module/subprogram |
3283 +---------------------------------------+
3284 | end |
3285 +---------------------------------------+
3289 enum state_order
3291 ORDER_START,
3292 ORDER_USE,
3293 ORDER_IMPORT,
3294 ORDER_IMPLICIT_NONE,
3295 ORDER_IMPLICIT,
3296 ORDER_SPEC,
3297 ORDER_EXEC
3300 typedef struct
3302 enum state_order state;
3303 gfc_statement last_statement;
3304 locus where;
3306 st_state;
3308 static bool
3309 verify_st_order (st_state *p, gfc_statement st, bool silent)
3312 switch (st)
3314 case ST_NONE:
3315 p->state = ORDER_START;
3316 in_exec_part = false;
3317 break;
3319 case ST_USE:
3320 if (p->state > ORDER_USE)
3321 goto order;
3322 p->state = ORDER_USE;
3323 break;
3325 case ST_IMPORT:
3326 if (p->state > ORDER_IMPORT)
3327 goto order;
3328 p->state = ORDER_IMPORT;
3329 break;
3331 case ST_IMPLICIT_NONE:
3332 if (p->state > ORDER_IMPLICIT)
3333 goto order;
3335 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
3336 statement disqualifies a USE but not an IMPLICIT NONE.
3337 Duplicate IMPLICIT NONEs are caught when the implicit types
3338 are set. */
3340 p->state = ORDER_IMPLICIT_NONE;
3341 break;
3343 case ST_IMPLICIT:
3344 if (p->state > ORDER_IMPLICIT)
3345 goto order;
3346 p->state = ORDER_IMPLICIT;
3347 break;
3349 case ST_FORMAT:
3350 case ST_ENTRY:
3351 if (p->state < ORDER_IMPLICIT_NONE)
3352 p->state = ORDER_IMPLICIT_NONE;
3353 break;
3355 case ST_PARAMETER:
3356 if (p->state >= ORDER_EXEC)
3357 goto order;
3358 if (p->state < ORDER_IMPLICIT)
3359 p->state = ORDER_IMPLICIT;
3360 break;
3362 case ST_DATA:
3363 if (p->state < ORDER_SPEC)
3364 p->state = ORDER_SPEC;
3365 break;
3367 case ST_PUBLIC:
3368 case ST_PRIVATE:
3369 case ST_STRUCTURE_DECL:
3370 case ST_DERIVED_DECL:
3371 case_decl:
3372 if (p->state >= ORDER_EXEC)
3373 goto order;
3374 if (p->state < ORDER_SPEC)
3375 p->state = ORDER_SPEC;
3376 break;
3378 case_omp_decl:
3379 /* The OpenMP/OpenACC directives have to be somewhere in the specification
3380 part, but there are no further requirements on their ordering.
3381 Thus don't adjust p->state, just ignore them. */
3382 if (p->state >= ORDER_EXEC)
3383 goto order;
3384 break;
3386 case_executable:
3387 case_exec_markers:
3388 if (p->state < ORDER_EXEC)
3389 p->state = ORDER_EXEC;
3390 in_exec_part = true;
3391 break;
3393 default:
3394 return false;
3397 /* All is well, record the statement in case we need it next time. */
3398 p->where = gfc_current_locus;
3399 p->last_statement = st;
3400 return true;
3402 order:
3403 if (!silent)
3404 gfc_error ("%s statement at %C cannot follow %s statement at %L",
3405 gfc_ascii_statement (st),
3406 gfc_ascii_statement (p->last_statement), &p->where);
3408 return false;
3412 /* Handle an unexpected end of file. This is a show-stopper... */
3414 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
3416 static void
3417 unexpected_eof (void)
3419 gfc_state_data *p;
3421 gfc_error ("Unexpected end of file in %qs", gfc_source_file);
3423 /* Memory cleanup. Move to "second to last". */
3424 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
3425 p = p->previous);
3427 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
3428 gfc_done_2 ();
3430 longjmp (eof_buf, 1);
3432 /* Avoids build error on systems where longjmp is not declared noreturn. */
3433 gcc_unreachable ();
3437 /* Parse the CONTAINS section of a derived type definition. */
3439 gfc_access gfc_typebound_default_access;
3441 static bool
3442 parse_derived_contains (void)
3444 gfc_state_data s;
3445 bool seen_private = false;
3446 bool seen_comps = false;
3447 bool error_flag = false;
3448 bool to_finish;
3450 gcc_assert (gfc_current_state () == COMP_DERIVED);
3451 gcc_assert (gfc_current_block ());
3453 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
3454 section. */
3455 if (gfc_current_block ()->attr.sequence)
3456 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
3457 " section at %C", gfc_current_block ()->name);
3458 if (gfc_current_block ()->attr.is_bind_c)
3459 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
3460 " section at %C", gfc_current_block ()->name);
3462 accept_statement (ST_CONTAINS);
3463 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
3465 gfc_typebound_default_access = ACCESS_PUBLIC;
3467 to_finish = false;
3468 while (!to_finish)
3470 gfc_statement st;
3471 st = next_statement ();
3472 switch (st)
3474 case ST_NONE:
3475 unexpected_eof ();
3476 break;
3478 case ST_DATA_DECL:
3479 gfc_error ("Components in TYPE at %C must precede CONTAINS");
3480 goto error;
3482 case ST_PROCEDURE:
3483 if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
3484 goto error;
3486 accept_statement (ST_PROCEDURE);
3487 seen_comps = true;
3488 break;
3490 case ST_GENERIC:
3491 if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
3492 goto error;
3494 accept_statement (ST_GENERIC);
3495 seen_comps = true;
3496 break;
3498 case ST_FINAL:
3499 if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
3500 " at %C"))
3501 goto error;
3503 accept_statement (ST_FINAL);
3504 seen_comps = true;
3505 break;
3507 case ST_END_TYPE:
3508 to_finish = true;
3510 if (!seen_comps
3511 && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
3512 "at %C with empty CONTAINS section")))
3513 goto error;
3515 /* ST_END_TYPE is accepted by parse_derived after return. */
3516 break;
3518 case ST_PRIVATE:
3519 if (!gfc_find_state (COMP_MODULE))
3521 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
3522 "a MODULE");
3523 goto error;
3526 if (seen_comps)
3528 gfc_error ("PRIVATE statement at %C must precede procedure"
3529 " bindings");
3530 goto error;
3533 if (seen_private)
3535 gfc_error ("Duplicate PRIVATE statement at %C");
3536 goto error;
3539 accept_statement (ST_PRIVATE);
3540 gfc_typebound_default_access = ACCESS_PRIVATE;
3541 seen_private = true;
3542 break;
3544 case ST_SEQUENCE:
3545 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
3546 goto error;
3548 case ST_CONTAINS:
3549 gfc_error ("Already inside a CONTAINS block at %C");
3550 goto error;
3552 default:
3553 unexpected_statement (st);
3554 break;
3557 continue;
3559 error:
3560 error_flag = true;
3561 reject_statement ();
3564 pop_state ();
3565 gcc_assert (gfc_current_state () == COMP_DERIVED);
3567 return error_flag;
3571 /* Set attributes for the parent symbol based on the attributes of a component
3572 and raise errors if conflicting attributes are found for the component. */
3574 static void
3575 check_component (gfc_symbol *sym, gfc_component *c, gfc_component **lockp,
3576 gfc_component **eventp)
3578 bool coarray, lock_type, event_type, allocatable, pointer;
3579 coarray = lock_type = event_type = allocatable = pointer = false;
3580 gfc_component *lock_comp = NULL, *event_comp = NULL;
3582 if (lockp) lock_comp = *lockp;
3583 if (eventp) event_comp = *eventp;
3585 /* Look for allocatable components. */
3586 if (c->attr.allocatable
3587 || (c->ts.type == BT_CLASS && c->attr.class_ok
3588 && CLASS_DATA (c)->attr.allocatable)
3589 || (c->ts.type == BT_DERIVED && !c->attr.pointer
3590 && c->ts.u.derived->attr.alloc_comp))
3592 allocatable = true;
3593 sym->attr.alloc_comp = 1;
3596 /* Look for pointer components. */
3597 if (c->attr.pointer
3598 || (c->ts.type == BT_CLASS && c->attr.class_ok
3599 && CLASS_DATA (c)->attr.class_pointer)
3600 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
3602 pointer = true;
3603 sym->attr.pointer_comp = 1;
3606 /* Look for procedure pointer components. */
3607 if (c->attr.proc_pointer
3608 || (c->ts.type == BT_DERIVED
3609 && c->ts.u.derived->attr.proc_pointer_comp))
3610 sym->attr.proc_pointer_comp = 1;
3612 /* Looking for coarray components. */
3613 if (c->attr.codimension
3614 || (c->ts.type == BT_CLASS && c->attr.class_ok
3615 && CLASS_DATA (c)->attr.codimension))
3617 coarray = true;
3618 sym->attr.coarray_comp = 1;
3621 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
3622 && !c->attr.pointer)
3624 coarray = true;
3625 sym->attr.coarray_comp = 1;
3628 /* Looking for lock_type components. */
3629 if ((c->ts.type == BT_DERIVED
3630 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
3631 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
3632 || (c->ts.type == BT_CLASS && c->attr.class_ok
3633 && CLASS_DATA (c)->ts.u.derived->from_intmod
3634 == INTMOD_ISO_FORTRAN_ENV
3635 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
3636 == ISOFORTRAN_LOCK_TYPE)
3637 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
3638 && !allocatable && !pointer))
3640 lock_type = 1;
3641 lock_comp = c;
3642 sym->attr.lock_comp = 1;
3645 /* Looking for event_type components. */
3646 if ((c->ts.type == BT_DERIVED
3647 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
3648 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
3649 || (c->ts.type == BT_CLASS && c->attr.class_ok
3650 && CLASS_DATA (c)->ts.u.derived->from_intmod
3651 == INTMOD_ISO_FORTRAN_ENV
3652 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
3653 == ISOFORTRAN_EVENT_TYPE)
3654 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.event_comp
3655 && !allocatable && !pointer))
3657 event_type = 1;
3658 event_comp = c;
3659 sym->attr.event_comp = 1;
3662 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
3663 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
3664 unless there are nondirect [allocatable or pointer] components
3665 involved (cf. 1.3.33.1 and 1.3.33.3). */
3667 if (pointer && !coarray && lock_type)
3668 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
3669 "codimension or be a subcomponent of a coarray, "
3670 "which is not possible as the component has the "
3671 "pointer attribute", c->name, &c->loc);
3672 else if (pointer && !coarray && c->ts.type == BT_DERIVED
3673 && c->ts.u.derived->attr.lock_comp)
3674 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
3675 "of type LOCK_TYPE, which must have a codimension or be a "
3676 "subcomponent of a coarray", c->name, &c->loc);
3678 if (lock_type && allocatable && !coarray)
3679 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
3680 "a codimension", c->name, &c->loc);
3681 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
3682 && c->ts.u.derived->attr.lock_comp)
3683 gfc_error ("Allocatable component %s at %L must have a codimension as "
3684 "it has a noncoarray subcomponent of type LOCK_TYPE",
3685 c->name, &c->loc);
3687 if (sym->attr.coarray_comp && !coarray && lock_type)
3688 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
3689 "subcomponent of type LOCK_TYPE must have a codimension or "
3690 "be a subcomponent of a coarray. (Variables of type %s may "
3691 "not have a codimension as already a coarray "
3692 "subcomponent exists)", c->name, &c->loc, sym->name);
3694 if (sym->attr.lock_comp && coarray && !lock_type)
3695 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
3696 "subcomponent of type LOCK_TYPE must have a codimension or "
3697 "be a subcomponent of a coarray. (Variables of type %s may "
3698 "not have a codimension as %s at %L has a codimension or a "
3699 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
3700 sym->name, c->name, &c->loc);
3702 /* Similarly for EVENT TYPE. */
3704 if (pointer && !coarray && event_type)
3705 gfc_error ("Component %s at %L of type EVENT_TYPE must have a "
3706 "codimension or be a subcomponent of a coarray, "
3707 "which is not possible as the component has the "
3708 "pointer attribute", c->name, &c->loc);
3709 else if (pointer && !coarray && c->ts.type == BT_DERIVED
3710 && c->ts.u.derived->attr.event_comp)
3711 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
3712 "of type EVENT_TYPE, which must have a codimension or be a "
3713 "subcomponent of a coarray", c->name, &c->loc);
3715 if (event_type && allocatable && !coarray)
3716 gfc_error ("Allocatable component %s at %L of type EVENT_TYPE must have "
3717 "a codimension", c->name, &c->loc);
3718 else if (event_type && allocatable && c->ts.type == BT_DERIVED
3719 && c->ts.u.derived->attr.event_comp)
3720 gfc_error ("Allocatable component %s at %L must have a codimension as "
3721 "it has a noncoarray subcomponent of type EVENT_TYPE",
3722 c->name, &c->loc);
3724 if (sym->attr.coarray_comp && !coarray && event_type)
3725 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
3726 "subcomponent of type EVENT_TYPE must have a codimension or "
3727 "be a subcomponent of a coarray. (Variables of type %s may "
3728 "not have a codimension as already a coarray "
3729 "subcomponent exists)", c->name, &c->loc, sym->name);
3731 if (sym->attr.event_comp && coarray && !event_type)
3732 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
3733 "subcomponent of type EVENT_TYPE must have a codimension or "
3734 "be a subcomponent of a coarray. (Variables of type %s may "
3735 "not have a codimension as %s at %L has a codimension or a "
3736 "coarray subcomponent)", event_comp->name, &event_comp->loc,
3737 sym->name, c->name, &c->loc);
3739 /* Look for private components. */
3740 if (sym->component_access == ACCESS_PRIVATE
3741 || c->attr.access == ACCESS_PRIVATE
3742 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
3743 sym->attr.private_comp = 1;
3745 if (lockp) *lockp = lock_comp;
3746 if (eventp) *eventp = event_comp;
3750 static void parse_struct_map (gfc_statement);
3752 /* Parse a union component definition within a structure definition. */
3754 static void
3755 parse_union (void)
3757 int compiling;
3758 gfc_statement st;
3759 gfc_state_data s;
3760 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3761 gfc_symbol *un;
3763 accept_statement(ST_UNION);
3764 push_state (&s, COMP_UNION, gfc_new_block);
3765 un = gfc_new_block;
3767 compiling = 1;
3769 while (compiling)
3771 st = next_statement ();
3772 /* Only MAP declarations valid within a union. */
3773 switch (st)
3775 case ST_NONE:
3776 unexpected_eof ();
3778 case ST_MAP:
3779 accept_statement (ST_MAP);
3780 parse_struct_map (ST_MAP);
3781 /* Add a component to the union for each map. */
3782 if (!gfc_add_component (un, gfc_new_block->name, &c))
3784 gfc_internal_error ("failed to create map component '%s'",
3785 gfc_new_block->name);
3786 reject_statement ();
3787 return;
3789 c->ts.type = BT_DERIVED;
3790 c->ts.u.derived = gfc_new_block;
3791 /* Normally components get their initialization expressions when they
3792 are created in decl.cc (build_struct) so we can look through the
3793 flat component list for initializers during resolution. Unions and
3794 maps create components along with their type definitions so we
3795 have to generate initializers here. */
3796 c->initializer = gfc_default_initializer (&c->ts);
3797 break;
3799 case ST_END_UNION:
3800 compiling = 0;
3801 accept_statement (ST_END_UNION);
3802 break;
3804 default:
3805 unexpected_statement (st);
3806 break;
3810 for (c = un->components; c; c = c->next)
3811 check_component (un, c, &lock_comp, &event_comp);
3813 /* Add the union as a component in its parent structure. */
3814 pop_state ();
3815 if (!gfc_add_component (gfc_current_block (), un->name, &c))
3817 gfc_internal_error ("failed to create union component '%s'", un->name);
3818 reject_statement ();
3819 return;
3821 c->ts.type = BT_UNION;
3822 c->ts.u.derived = un;
3823 c->initializer = gfc_default_initializer (&c->ts);
3825 un->attr.zero_comp = un->components == NULL;
3829 /* Parse a STRUCTURE or MAP. */
3831 static void
3832 parse_struct_map (gfc_statement block)
3834 int compiling_type;
3835 gfc_statement st;
3836 gfc_state_data s;
3837 gfc_symbol *sym;
3838 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3839 gfc_compile_state comp;
3840 gfc_statement ends;
3842 if (block == ST_STRUCTURE_DECL)
3844 comp = COMP_STRUCTURE;
3845 ends = ST_END_STRUCTURE;
3847 else
3849 gcc_assert (block == ST_MAP);
3850 comp = COMP_MAP;
3851 ends = ST_END_MAP;
3854 accept_statement(block);
3855 push_state (&s, comp, gfc_new_block);
3857 gfc_new_block->component_access = ACCESS_PUBLIC;
3858 compiling_type = 1;
3860 while (compiling_type)
3862 st = next_statement ();
3863 switch (st)
3865 case ST_NONE:
3866 unexpected_eof ();
3868 /* Nested structure declarations will be captured as ST_DATA_DECL. */
3869 case ST_STRUCTURE_DECL:
3870 /* Let a more specific error make it to decode_statement(). */
3871 if (gfc_error_check () == 0)
3872 gfc_error ("Syntax error in nested structure declaration at %C");
3873 reject_statement ();
3874 /* Skip the rest of this statement. */
3875 gfc_error_recovery ();
3876 break;
3878 case ST_UNION:
3879 accept_statement (ST_UNION);
3880 parse_union ();
3881 break;
3883 case ST_DATA_DECL:
3884 /* The data declaration was a nested/ad-hoc STRUCTURE field. */
3885 accept_statement (ST_DATA_DECL);
3886 if (gfc_new_block && gfc_new_block != gfc_current_block ()
3887 && gfc_new_block->attr.flavor == FL_STRUCT)
3888 parse_struct_map (ST_STRUCTURE_DECL);
3889 break;
3891 case ST_END_STRUCTURE:
3892 case ST_END_MAP:
3893 if (st == ends)
3895 accept_statement (st);
3896 compiling_type = 0;
3898 else
3899 unexpected_statement (st);
3900 break;
3902 default:
3903 unexpected_statement (st);
3904 break;
3908 /* Validate each component. */
3909 sym = gfc_current_block ();
3910 for (c = sym->components; c; c = c->next)
3911 check_component (sym, c, &lock_comp, &event_comp);
3913 sym->attr.zero_comp = (sym->components == NULL);
3915 /* Allow parse_union to find this structure to add to its list of maps. */
3916 if (block == ST_MAP)
3917 gfc_new_block = gfc_current_block ();
3919 pop_state ();
3923 /* Parse a derived type. */
3925 static void
3926 parse_derived (void)
3928 int compiling_type, seen_private, seen_sequence, seen_component;
3929 gfc_statement st;
3930 gfc_state_data s;
3931 gfc_symbol *sym;
3932 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3934 accept_statement (ST_DERIVED_DECL);
3935 push_state (&s, COMP_DERIVED, gfc_new_block);
3937 gfc_new_block->component_access = ACCESS_PUBLIC;
3938 seen_private = 0;
3939 seen_sequence = 0;
3940 seen_component = 0;
3942 compiling_type = 1;
3944 while (compiling_type)
3946 st = next_statement ();
3947 switch (st)
3949 case ST_NONE:
3950 unexpected_eof ();
3952 case ST_DATA_DECL:
3953 case ST_PROCEDURE:
3954 accept_statement (st);
3955 seen_component = 1;
3956 break;
3958 case ST_FINAL:
3959 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
3960 break;
3962 case ST_END_TYPE:
3963 endType:
3964 compiling_type = 0;
3966 if (!seen_component)
3967 gfc_notify_std (GFC_STD_F2003, "Derived type "
3968 "definition at %C without components");
3970 accept_statement (ST_END_TYPE);
3971 break;
3973 case ST_PRIVATE:
3974 if (!gfc_find_state (COMP_MODULE))
3976 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
3977 "a MODULE");
3978 break;
3981 if (seen_component)
3983 gfc_error ("PRIVATE statement at %C must precede "
3984 "structure components");
3985 break;
3988 if (seen_private)
3989 gfc_error ("Duplicate PRIVATE statement at %C");
3991 s.sym->component_access = ACCESS_PRIVATE;
3993 accept_statement (ST_PRIVATE);
3994 seen_private = 1;
3995 break;
3997 case ST_SEQUENCE:
3998 if (seen_component)
4000 gfc_error ("SEQUENCE statement at %C must precede "
4001 "structure components");
4002 break;
4005 if (gfc_current_block ()->attr.sequence)
4006 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
4007 "TYPE statement");
4009 if (seen_sequence)
4011 gfc_error ("Duplicate SEQUENCE statement at %C");
4014 seen_sequence = 1;
4015 gfc_add_sequence (&gfc_current_block ()->attr,
4016 gfc_current_block ()->name, NULL);
4017 break;
4019 case ST_CONTAINS:
4020 gfc_notify_std (GFC_STD_F2003,
4021 "CONTAINS block in derived type"
4022 " definition at %C");
4024 accept_statement (ST_CONTAINS);
4025 parse_derived_contains ();
4026 goto endType;
4028 default:
4029 unexpected_statement (st);
4030 break;
4034 /* need to verify that all fields of the derived type are
4035 * interoperable with C if the type is declared to be bind(c)
4037 sym = gfc_current_block ();
4038 for (c = sym->components; c; c = c->next)
4039 check_component (sym, c, &lock_comp, &event_comp);
4041 if (!seen_component)
4042 sym->attr.zero_comp = 1;
4044 pop_state ();
4048 /* Parse an ENUM. */
4050 static void
4051 parse_enum (void)
4053 gfc_statement st;
4054 int compiling_enum;
4055 gfc_state_data s;
4056 int seen_enumerator = 0;
4058 push_state (&s, COMP_ENUM, gfc_new_block);
4060 compiling_enum = 1;
4062 while (compiling_enum)
4064 st = next_statement ();
4065 switch (st)
4067 case ST_NONE:
4068 unexpected_eof ();
4069 break;
4071 case ST_ENUMERATOR:
4072 seen_enumerator = 1;
4073 accept_statement (st);
4074 break;
4076 case ST_END_ENUM:
4077 compiling_enum = 0;
4078 if (!seen_enumerator)
4079 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
4080 accept_statement (st);
4081 break;
4083 default:
4084 gfc_free_enum_history ();
4085 unexpected_statement (st);
4086 break;
4089 pop_state ();
4093 /* Parse an interface. We must be able to deal with the possibility
4094 of recursive interfaces. The parse_spec() subroutine is mutually
4095 recursive with parse_interface(). */
4097 static gfc_statement parse_spec (gfc_statement);
4099 static void
4100 parse_interface (void)
4102 gfc_compile_state new_state = COMP_NONE, current_state;
4103 gfc_symbol *prog_unit, *sym;
4104 gfc_interface_info save;
4105 gfc_state_data s1, s2;
4106 gfc_statement st;
4108 accept_statement (ST_INTERFACE);
4110 current_interface.ns = gfc_current_ns;
4111 save = current_interface;
4113 sym = (current_interface.type == INTERFACE_GENERIC
4114 || current_interface.type == INTERFACE_USER_OP)
4115 ? gfc_new_block : NULL;
4117 push_state (&s1, COMP_INTERFACE, sym);
4118 current_state = COMP_NONE;
4120 loop:
4121 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
4123 st = next_statement ();
4124 switch (st)
4126 case ST_NONE:
4127 unexpected_eof ();
4129 case ST_SUBROUTINE:
4130 case ST_FUNCTION:
4131 if (st == ST_SUBROUTINE)
4132 new_state = COMP_SUBROUTINE;
4133 else if (st == ST_FUNCTION)
4134 new_state = COMP_FUNCTION;
4135 if (gfc_new_block->attr.pointer)
4137 gfc_new_block->attr.pointer = 0;
4138 gfc_new_block->attr.proc_pointer = 1;
4140 if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
4141 gfc_new_block->formal, NULL))
4143 reject_statement ();
4144 gfc_free_namespace (gfc_current_ns);
4145 goto loop;
4147 /* F2008 C1210 forbids the IMPORT statement in module procedure
4148 interface bodies and the flag is set to import symbols. */
4149 if (gfc_new_block->attr.module_procedure)
4150 gfc_current_ns->has_import_set = 1;
4151 break;
4153 case ST_PROCEDURE:
4154 case ST_MODULE_PROC: /* The module procedure matcher makes
4155 sure the context is correct. */
4156 accept_statement (st);
4157 gfc_free_namespace (gfc_current_ns);
4158 goto loop;
4160 case ST_END_INTERFACE:
4161 gfc_free_namespace (gfc_current_ns);
4162 gfc_current_ns = current_interface.ns;
4163 goto done;
4165 default:
4166 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
4167 gfc_ascii_statement (st));
4168 current_interface = save;
4169 reject_statement ();
4170 gfc_free_namespace (gfc_current_ns);
4171 goto loop;
4175 /* Make sure that the generic name has the right attribute. */
4176 if (current_interface.type == INTERFACE_GENERIC
4177 && current_state == COMP_NONE)
4179 if (new_state == COMP_FUNCTION && sym)
4180 gfc_add_function (&sym->attr, sym->name, NULL);
4181 else if (new_state == COMP_SUBROUTINE && sym)
4182 gfc_add_subroutine (&sym->attr, sym->name, NULL);
4184 current_state = new_state;
4187 if (current_interface.type == INTERFACE_ABSTRACT)
4189 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
4190 if (gfc_is_intrinsic_typename (gfc_new_block->name))
4191 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
4192 "cannot be the same as an intrinsic type",
4193 gfc_new_block->name);
4196 push_state (&s2, new_state, gfc_new_block);
4197 accept_statement (st);
4198 prog_unit = gfc_new_block;
4199 prog_unit->formal_ns = gfc_current_ns;
4201 decl:
4202 /* Read data declaration statements. */
4203 st = parse_spec (ST_NONE);
4204 in_specification_block = true;
4206 /* Since the interface block does not permit an IMPLICIT statement,
4207 the default type for the function or the result must be taken
4208 from the formal namespace. */
4209 if (new_state == COMP_FUNCTION)
4211 if (prog_unit->result == prog_unit
4212 && prog_unit->ts.type == BT_UNKNOWN)
4213 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
4214 else if (prog_unit->result != prog_unit
4215 && prog_unit->result->ts.type == BT_UNKNOWN)
4216 gfc_set_default_type (prog_unit->result, 1,
4217 prog_unit->formal_ns);
4220 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
4222 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
4223 gfc_ascii_statement (st));
4224 reject_statement ();
4225 goto decl;
4228 /* Add EXTERNAL attribute to function or subroutine. */
4229 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
4230 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
4232 current_interface = save;
4233 gfc_add_interface (prog_unit);
4234 pop_state ();
4236 if (current_interface.ns
4237 && current_interface.ns->proc_name
4238 && strcmp (current_interface.ns->proc_name->name,
4239 prog_unit->name) == 0)
4240 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
4241 "enclosing procedure", prog_unit->name,
4242 &current_interface.ns->proc_name->declared_at);
4244 goto loop;
4246 done:
4247 pop_state ();
4251 /* Associate function characteristics by going back to the function
4252 declaration and rematching the prefix. */
4254 static match
4255 match_deferred_characteristics (gfc_typespec * ts)
4257 locus loc;
4258 match m = MATCH_ERROR;
4259 char name[GFC_MAX_SYMBOL_LEN + 1];
4261 loc = gfc_current_locus;
4263 gfc_current_locus = gfc_current_block ()->declared_at;
4265 gfc_clear_error ();
4266 gfc_buffer_error (true);
4267 m = gfc_match_prefix (ts);
4268 gfc_buffer_error (false);
4270 if (ts->type == BT_DERIVED || ts->type == BT_CLASS)
4272 ts->kind = 0;
4274 if (!ts->u.derived)
4275 m = MATCH_ERROR;
4278 /* Only permit one go at the characteristic association. */
4279 if (ts->kind == -1)
4280 ts->kind = 0;
4282 /* Set the function locus correctly. If we have not found the
4283 function name, there is an error. */
4284 if (m == MATCH_YES
4285 && gfc_match ("function% %n", name) == MATCH_YES
4286 && strcmp (name, gfc_current_block ()->name) == 0)
4288 gfc_current_block ()->declared_at = gfc_current_locus;
4289 gfc_commit_symbols ();
4291 else
4293 gfc_error_check ();
4294 gfc_undo_symbols ();
4297 gfc_current_locus =loc;
4298 return m;
4302 /* Check specification-expressions in the function result of the currently
4303 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
4304 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
4305 scope are not yet parsed so this has to be delayed up to parse_spec. */
4307 static bool
4308 check_function_result_typed (void)
4310 gfc_typespec ts;
4312 gcc_assert (gfc_current_state () == COMP_FUNCTION);
4314 if (!gfc_current_ns->proc_name->result)
4315 return true;
4317 ts = gfc_current_ns->proc_name->result->ts;
4319 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
4320 /* TODO: Extend when KIND type parameters are implemented. */
4321 if (ts.type == BT_CHARACTER && ts.u.cl && ts.u.cl->length)
4323 /* Reject invalid type of specification expression for length. */
4324 if (ts.u.cl->length->ts.type != BT_INTEGER)
4325 return false;
4327 gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true);
4330 return true;
4334 /* Parse a set of specification statements. Returns the statement
4335 that doesn't fit. */
4337 static gfc_statement
4338 parse_spec (gfc_statement st)
4340 st_state ss;
4341 bool function_result_typed = false;
4342 bool bad_characteristic = false;
4343 gfc_typespec *ts;
4345 in_specification_block = true;
4347 verify_st_order (&ss, ST_NONE, false);
4348 if (st == ST_NONE)
4349 st = next_statement ();
4351 /* If we are not inside a function or don't have a result specified so far,
4352 do nothing special about it. */
4353 if (gfc_current_state () != COMP_FUNCTION)
4354 function_result_typed = true;
4355 else
4357 gfc_symbol* proc = gfc_current_ns->proc_name;
4358 gcc_assert (proc);
4360 if (proc->result && proc->result->ts.type == BT_UNKNOWN)
4361 function_result_typed = true;
4364 loop:
4366 /* If we're inside a BLOCK construct, some statements are disallowed.
4367 Check this here. Attribute declaration statements like INTENT, OPTIONAL
4368 or VALUE are also disallowed, but they don't have a particular ST_*
4369 key so we have to check for them individually in their matcher routine. */
4370 if (gfc_current_state () == COMP_BLOCK)
4371 switch (st)
4373 case ST_IMPLICIT:
4374 case ST_IMPLICIT_NONE:
4375 case ST_NAMELIST:
4376 case ST_COMMON:
4377 case ST_EQUIVALENCE:
4378 case ST_STATEMENT_FUNCTION:
4379 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
4380 gfc_ascii_statement (st));
4381 reject_statement ();
4382 break;
4384 default:
4385 break;
4387 else if (gfc_current_state () == COMP_BLOCK_DATA)
4388 /* Fortran 2008, C1116. */
4389 switch (st)
4391 case ST_ATTR_DECL:
4392 case ST_COMMON:
4393 case ST_DATA:
4394 case ST_DATA_DECL:
4395 case ST_DERIVED_DECL:
4396 case ST_END_BLOCK_DATA:
4397 case ST_EQUIVALENCE:
4398 case ST_IMPLICIT:
4399 case ST_IMPLICIT_NONE:
4400 case ST_OMP_THREADPRIVATE:
4401 case ST_PARAMETER:
4402 case ST_STRUCTURE_DECL:
4403 case ST_TYPE:
4404 case ST_USE:
4405 break;
4407 case ST_NONE:
4408 break;
4410 default:
4411 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
4412 gfc_ascii_statement (st));
4413 reject_statement ();
4414 break;
4417 /* If we find a statement that cannot be followed by an IMPLICIT statement
4418 (and thus we can expect to see none any further), type the function result
4419 if it has not yet been typed. Be careful not to give the END statement
4420 to verify_st_order! */
4421 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
4423 bool verify_now = false;
4425 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
4426 verify_now = true;
4427 else
4429 st_state dummyss;
4430 verify_st_order (&dummyss, ST_NONE, false);
4431 verify_st_order (&dummyss, st, false);
4433 if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
4434 verify_now = true;
4437 if (verify_now)
4438 function_result_typed = check_function_result_typed ();
4441 switch (st)
4443 case ST_NONE:
4444 unexpected_eof ();
4446 case ST_IMPLICIT_NONE:
4447 case ST_IMPLICIT:
4448 if (!function_result_typed)
4449 function_result_typed = check_function_result_typed ();
4450 goto declSt;
4452 case ST_FORMAT:
4453 case ST_ENTRY:
4454 case ST_DATA: /* Not allowed in interfaces */
4455 if (gfc_current_state () == COMP_INTERFACE)
4456 break;
4458 /* Fall through */
4460 case ST_USE:
4461 case ST_IMPORT:
4462 case ST_PARAMETER:
4463 case ST_PUBLIC:
4464 case ST_PRIVATE:
4465 case ST_STRUCTURE_DECL:
4466 case ST_DERIVED_DECL:
4467 case_decl:
4468 case_omp_decl:
4469 declSt:
4470 if (!verify_st_order (&ss, st, false))
4472 reject_statement ();
4473 st = next_statement ();
4474 goto loop;
4477 switch (st)
4479 case ST_INTERFACE:
4480 parse_interface ();
4481 break;
4483 case ST_STRUCTURE_DECL:
4484 parse_struct_map (ST_STRUCTURE_DECL);
4485 break;
4487 case ST_DERIVED_DECL:
4488 parse_derived ();
4489 break;
4491 case ST_PUBLIC:
4492 case ST_PRIVATE:
4493 if (gfc_current_state () != COMP_MODULE)
4495 gfc_error ("%s statement must appear in a MODULE",
4496 gfc_ascii_statement (st));
4497 reject_statement ();
4498 break;
4501 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
4503 gfc_error ("%s statement at %C follows another accessibility "
4504 "specification", gfc_ascii_statement (st));
4505 reject_statement ();
4506 break;
4509 gfc_current_ns->default_access = (st == ST_PUBLIC)
4510 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
4512 break;
4514 case ST_STATEMENT_FUNCTION:
4515 if (gfc_current_state () == COMP_MODULE
4516 || gfc_current_state () == COMP_SUBMODULE)
4518 unexpected_statement (st);
4519 break;
4522 default:
4523 break;
4526 accept_statement (st);
4527 st = next_statement ();
4528 goto loop;
4530 case ST_ENUM:
4531 accept_statement (st);
4532 parse_enum();
4533 st = next_statement ();
4534 goto loop;
4536 case ST_GET_FCN_CHARACTERISTICS:
4537 /* This statement triggers the association of a function's result
4538 characteristics. */
4539 ts = &gfc_current_block ()->result->ts;
4540 if (match_deferred_characteristics (ts) != MATCH_YES)
4541 bad_characteristic = true;
4543 st = next_statement ();
4544 goto loop;
4546 default:
4547 break;
4550 /* If match_deferred_characteristics failed, then there is an error. */
4551 if (bad_characteristic)
4553 ts = &gfc_current_block ()->result->ts;
4554 if (ts->type != BT_DERIVED && ts->type != BT_CLASS)
4555 gfc_error ("Bad kind expression for function %qs at %L",
4556 gfc_current_block ()->name,
4557 &gfc_current_block ()->declared_at);
4558 else
4559 gfc_error ("The type for function %qs at %L is not accessible",
4560 gfc_current_block ()->name,
4561 &gfc_current_block ()->declared_at);
4563 gfc_current_block ()->ts.kind = 0;
4564 /* Keep the derived type; if it's bad, it will be discovered later. */
4565 if (!(ts->type == BT_DERIVED && ts->u.derived))
4566 ts->type = BT_UNKNOWN;
4569 in_specification_block = false;
4571 return st;
4575 /* Parse a WHERE block, (not a simple WHERE statement). */
4577 static void
4578 parse_where_block (void)
4580 int seen_empty_else;
4581 gfc_code *top, *d;
4582 gfc_state_data s;
4583 gfc_statement st;
4585 accept_statement (ST_WHERE_BLOCK);
4586 top = gfc_state_stack->tail;
4588 push_state (&s, COMP_WHERE, gfc_new_block);
4590 d = add_statement ();
4591 d->expr1 = top->expr1;
4592 d->op = EXEC_WHERE;
4594 top->expr1 = NULL;
4595 top->block = d;
4597 seen_empty_else = 0;
4601 st = next_statement ();
4602 switch (st)
4604 case ST_NONE:
4605 unexpected_eof ();
4607 case ST_WHERE_BLOCK:
4608 parse_where_block ();
4609 break;
4611 case ST_ASSIGNMENT:
4612 case ST_WHERE:
4613 accept_statement (st);
4614 break;
4616 case ST_ELSEWHERE:
4617 if (seen_empty_else)
4619 gfc_error ("ELSEWHERE statement at %C follows previous "
4620 "unmasked ELSEWHERE");
4621 reject_statement ();
4622 break;
4625 if (new_st.expr1 == NULL)
4626 seen_empty_else = 1;
4628 d = new_level (gfc_state_stack->head);
4629 d->op = EXEC_WHERE;
4630 d->expr1 = new_st.expr1;
4632 accept_statement (st);
4634 break;
4636 case ST_END_WHERE:
4637 accept_statement (st);
4638 break;
4640 default:
4641 gfc_error ("Unexpected %s statement in WHERE block at %C",
4642 gfc_ascii_statement (st));
4643 reject_statement ();
4644 break;
4647 while (st != ST_END_WHERE);
4649 pop_state ();
4653 /* Parse a FORALL block (not a simple FORALL statement). */
4655 static void
4656 parse_forall_block (void)
4658 gfc_code *top, *d;
4659 gfc_state_data s;
4660 gfc_statement st;
4662 accept_statement (ST_FORALL_BLOCK);
4663 top = gfc_state_stack->tail;
4665 push_state (&s, COMP_FORALL, gfc_new_block);
4667 d = add_statement ();
4668 d->op = EXEC_FORALL;
4669 top->block = d;
4673 st = next_statement ();
4674 switch (st)
4677 case ST_ASSIGNMENT:
4678 case ST_POINTER_ASSIGNMENT:
4679 case ST_WHERE:
4680 case ST_FORALL:
4681 accept_statement (st);
4682 break;
4684 case ST_WHERE_BLOCK:
4685 parse_where_block ();
4686 break;
4688 case ST_FORALL_BLOCK:
4689 parse_forall_block ();
4690 break;
4692 case ST_END_FORALL:
4693 accept_statement (st);
4694 break;
4696 case ST_NONE:
4697 unexpected_eof ();
4699 default:
4700 gfc_error ("Unexpected %s statement in FORALL block at %C",
4701 gfc_ascii_statement (st));
4703 reject_statement ();
4704 break;
4707 while (st != ST_END_FORALL);
4709 pop_state ();
4713 static gfc_statement parse_executable (gfc_statement);
4715 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
4717 static void
4718 parse_if_block (void)
4720 gfc_code *top, *d;
4721 gfc_statement st;
4722 locus else_locus;
4723 gfc_state_data s;
4724 int seen_else;
4726 seen_else = 0;
4727 accept_statement (ST_IF_BLOCK);
4729 top = gfc_state_stack->tail;
4730 push_state (&s, COMP_IF, gfc_new_block);
4732 new_st.op = EXEC_IF;
4733 d = add_statement ();
4735 d->expr1 = top->expr1;
4736 top->expr1 = NULL;
4737 top->block = d;
4741 st = parse_executable (ST_NONE);
4743 switch (st)
4745 case ST_NONE:
4746 unexpected_eof ();
4748 case ST_ELSEIF:
4749 if (seen_else)
4751 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
4752 "statement at %L", &else_locus);
4754 reject_statement ();
4755 break;
4758 d = new_level (gfc_state_stack->head);
4759 d->op = EXEC_IF;
4760 d->expr1 = new_st.expr1;
4762 accept_statement (st);
4764 break;
4766 case ST_ELSE:
4767 if (seen_else)
4769 gfc_error ("Duplicate ELSE statements at %L and %C",
4770 &else_locus);
4771 reject_statement ();
4772 break;
4775 seen_else = 1;
4776 else_locus = gfc_current_locus;
4778 d = new_level (gfc_state_stack->head);
4779 d->op = EXEC_IF;
4781 accept_statement (st);
4783 break;
4785 case ST_ENDIF:
4786 break;
4788 default:
4789 unexpected_statement (st);
4790 break;
4793 while (st != ST_ENDIF);
4795 pop_state ();
4796 accept_statement (st);
4800 /* Parse a SELECT block. */
4802 static void
4803 parse_select_block (void)
4805 gfc_statement st;
4806 gfc_code *cp;
4807 gfc_state_data s;
4809 accept_statement (ST_SELECT_CASE);
4811 cp = gfc_state_stack->tail;
4812 push_state (&s, COMP_SELECT, gfc_new_block);
4814 /* Make sure that the next statement is a CASE or END SELECT. */
4815 for (;;)
4817 st = next_statement ();
4818 if (st == ST_NONE)
4819 unexpected_eof ();
4820 if (st == ST_END_SELECT)
4822 /* Empty SELECT CASE is OK. */
4823 accept_statement (st);
4824 pop_state ();
4825 return;
4827 if (st == ST_CASE)
4828 break;
4830 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
4831 "CASE at %C");
4833 reject_statement ();
4836 /* At this point, we've got a nonempty select block. */
4837 cp = new_level (cp);
4838 *cp = new_st;
4840 accept_statement (st);
4844 st = parse_executable (ST_NONE);
4845 switch (st)
4847 case ST_NONE:
4848 unexpected_eof ();
4850 case ST_CASE:
4851 cp = new_level (gfc_state_stack->head);
4852 *cp = new_st;
4853 gfc_clear_new_st ();
4855 accept_statement (st);
4856 /* Fall through */
4858 case ST_END_SELECT:
4859 break;
4861 /* Can't have an executable statement because of
4862 parse_executable(). */
4863 default:
4864 unexpected_statement (st);
4865 break;
4868 while (st != ST_END_SELECT);
4870 pop_state ();
4871 accept_statement (st);
4875 /* Pop the current selector from the SELECT TYPE stack. */
4877 static void
4878 select_type_pop (void)
4880 gfc_select_type_stack *old = select_type_stack;
4881 select_type_stack = old->prev;
4882 free (old);
4886 /* Parse a SELECT TYPE construct (F03:R821). */
4888 static void
4889 parse_select_type_block (void)
4891 gfc_statement st;
4892 gfc_code *cp;
4893 gfc_state_data s;
4895 gfc_current_ns = new_st.ext.block.ns;
4896 accept_statement (ST_SELECT_TYPE);
4898 cp = gfc_state_stack->tail;
4899 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
4901 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
4902 or END SELECT. */
4903 for (;;)
4905 st = next_statement ();
4906 if (st == ST_NONE)
4907 unexpected_eof ();
4908 if (st == ST_END_SELECT)
4909 /* Empty SELECT CASE is OK. */
4910 goto done;
4911 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
4912 break;
4914 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
4915 "following SELECT TYPE at %C");
4917 reject_statement ();
4920 /* At this point, we've got a nonempty select block. */
4921 cp = new_level (cp);
4922 *cp = new_st;
4924 accept_statement (st);
4928 st = parse_executable (ST_NONE);
4929 switch (st)
4931 case ST_NONE:
4932 unexpected_eof ();
4934 case ST_TYPE_IS:
4935 case ST_CLASS_IS:
4936 cp = new_level (gfc_state_stack->head);
4937 *cp = new_st;
4938 gfc_clear_new_st ();
4940 accept_statement (st);
4941 /* Fall through */
4943 case ST_END_SELECT:
4944 break;
4946 /* Can't have an executable statement because of
4947 parse_executable(). */
4948 default:
4949 unexpected_statement (st);
4950 break;
4953 while (st != ST_END_SELECT);
4955 done:
4956 pop_state ();
4957 accept_statement (st);
4958 gfc_current_ns = gfc_current_ns->parent;
4959 select_type_pop ();
4963 /* Parse a SELECT RANK construct. */
4965 static void
4966 parse_select_rank_block (void)
4968 gfc_statement st;
4969 gfc_code *cp;
4970 gfc_state_data s;
4972 gfc_current_ns = new_st.ext.block.ns;
4973 accept_statement (ST_SELECT_RANK);
4975 cp = gfc_state_stack->tail;
4976 push_state (&s, COMP_SELECT_RANK, gfc_new_block);
4978 /* Make sure that the next statement is a RANK IS or RANK DEFAULT. */
4979 for (;;)
4981 st = next_statement ();
4982 if (st == ST_NONE)
4983 unexpected_eof ();
4984 if (st == ST_END_SELECT)
4985 /* Empty SELECT CASE is OK. */
4986 goto done;
4987 if (st == ST_RANK)
4988 break;
4990 gfc_error ("Expected RANK or RANK DEFAULT "
4991 "following SELECT RANK at %C");
4993 reject_statement ();
4996 /* At this point, we've got a nonempty select block. */
4997 cp = new_level (cp);
4998 *cp = new_st;
5000 accept_statement (st);
5004 st = parse_executable (ST_NONE);
5005 switch (st)
5007 case ST_NONE:
5008 unexpected_eof ();
5010 case ST_RANK:
5011 cp = new_level (gfc_state_stack->head);
5012 *cp = new_st;
5013 gfc_clear_new_st ();
5015 accept_statement (st);
5016 /* Fall through */
5018 case ST_END_SELECT:
5019 break;
5021 /* Can't have an executable statement because of
5022 parse_executable(). */
5023 default:
5024 unexpected_statement (st);
5025 break;
5028 while (st != ST_END_SELECT);
5030 done:
5031 pop_state ();
5032 accept_statement (st);
5033 gfc_current_ns = gfc_current_ns->parent;
5034 select_type_pop ();
5038 /* Given a symbol, make sure it is not an iteration variable for a DO
5039 statement. This subroutine is called when the symbol is seen in a
5040 context that causes it to become redefined. If the symbol is an
5041 iterator, we generate an error message and return nonzero. */
5043 bool
5044 gfc_check_do_variable (gfc_symtree *st)
5046 gfc_state_data *s;
5048 if (!st)
5049 return 0;
5051 for (s=gfc_state_stack; s; s = s->previous)
5052 if (s->do_variable == st)
5054 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
5055 "loop beginning at %L", st->name, &s->head->loc);
5056 return 1;
5059 return 0;
5063 /* Checks to see if the current statement label closes an enddo.
5064 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
5065 an error) if it incorrectly closes an ENDDO. */
5067 static int
5068 check_do_closure (void)
5070 gfc_state_data *p;
5072 if (gfc_statement_label == NULL)
5073 return 0;
5075 for (p = gfc_state_stack; p; p = p->previous)
5076 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
5077 break;
5079 if (p == NULL)
5080 return 0; /* No loops to close */
5082 if (p->ext.end_do_label == gfc_statement_label)
5084 if (p == gfc_state_stack)
5085 return 1;
5087 gfc_error ("End of nonblock DO statement at %C is within another block");
5088 return 2;
5091 /* At this point, the label doesn't terminate the innermost loop.
5092 Make sure it doesn't terminate another one. */
5093 for (; p; p = p->previous)
5094 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
5095 && p->ext.end_do_label == gfc_statement_label)
5097 gfc_error ("End of nonblock DO statement at %C is interwoven "
5098 "with another DO loop");
5099 return 2;
5102 return 0;
5106 /* Parse a series of contained program units. */
5108 static void parse_progunit (gfc_statement);
5111 /* Parse a CRITICAL block. */
5113 static void
5114 parse_critical_block (void)
5116 gfc_code *top, *d;
5117 gfc_state_data s, *sd;
5118 gfc_statement st;
5120 for (sd = gfc_state_stack; sd; sd = sd->previous)
5121 if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
5122 gfc_error_now (is_oacc (sd)
5123 ? G_("CRITICAL block inside of OpenACC region at %C")
5124 : G_("CRITICAL block inside of OpenMP region at %C"));
5126 s.ext.end_do_label = new_st.label1;
5128 accept_statement (ST_CRITICAL);
5129 top = gfc_state_stack->tail;
5131 push_state (&s, COMP_CRITICAL, gfc_new_block);
5133 d = add_statement ();
5134 d->op = EXEC_CRITICAL;
5135 top->block = d;
5139 st = parse_executable (ST_NONE);
5141 switch (st)
5143 case ST_NONE:
5144 unexpected_eof ();
5145 break;
5147 case ST_END_CRITICAL:
5148 if (s.ext.end_do_label != NULL
5149 && s.ext.end_do_label != gfc_statement_label)
5150 gfc_error_now ("Statement label in END CRITICAL at %C does not "
5151 "match CRITICAL label");
5153 if (gfc_statement_label != NULL)
5155 new_st.op = EXEC_NOP;
5156 add_statement ();
5158 break;
5160 default:
5161 unexpected_statement (st);
5162 break;
5165 while (st != ST_END_CRITICAL);
5167 pop_state ();
5168 accept_statement (st);
5172 /* Set up the local namespace for a BLOCK construct. */
5174 gfc_namespace*
5175 gfc_build_block_ns (gfc_namespace *parent_ns)
5177 gfc_namespace* my_ns;
5178 static int numblock = 1;
5180 my_ns = gfc_get_namespace (parent_ns, 1);
5181 my_ns->construct_entities = 1;
5183 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
5184 code generation (so it must not be NULL).
5185 We set its recursive argument if our container procedure is recursive, so
5186 that local variables are accordingly placed on the stack when it
5187 will be necessary. */
5188 if (gfc_new_block)
5189 my_ns->proc_name = gfc_new_block;
5190 else
5192 bool t;
5193 char buffer[20]; /* Enough to hold "block@2147483648\n". */
5195 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
5196 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
5197 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
5198 my_ns->proc_name->name, NULL);
5199 gcc_assert (t);
5200 gfc_commit_symbol (my_ns->proc_name);
5203 if (parent_ns->proc_name)
5204 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
5206 return my_ns;
5210 /* Parse a BLOCK construct. */
5212 static void
5213 parse_block_construct (void)
5215 gfc_namespace* my_ns;
5216 gfc_namespace* my_parent;
5217 gfc_state_data s;
5219 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
5221 my_ns = gfc_build_block_ns (gfc_current_ns);
5223 new_st.op = EXEC_BLOCK;
5224 new_st.ext.block.ns = my_ns;
5225 new_st.ext.block.assoc = NULL;
5226 accept_statement (ST_BLOCK);
5228 push_state (&s, COMP_BLOCK, my_ns->proc_name);
5229 gfc_current_ns = my_ns;
5230 my_parent = my_ns->parent;
5232 parse_progunit (ST_NONE);
5234 /* Don't depend on the value of gfc_current_ns; it might have been
5235 reset if the block had errors and was cleaned up. */
5236 gfc_current_ns = my_parent;
5238 pop_state ();
5242 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
5243 behind the scenes with compiler-generated variables. */
5245 static void
5246 parse_associate (void)
5248 gfc_namespace* my_ns;
5249 gfc_state_data s;
5250 gfc_statement st;
5251 gfc_association_list* a;
5252 gfc_array_spec *as;
5254 gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
5256 my_ns = gfc_build_block_ns (gfc_current_ns);
5258 new_st.op = EXEC_BLOCK;
5259 new_st.ext.block.ns = my_ns;
5260 gcc_assert (new_st.ext.block.assoc);
5262 /* Add all associate-names as BLOCK variables. Creating them is enough
5263 for now, they'll get their values during trans-* phase. */
5264 gfc_current_ns = my_ns;
5265 for (a = new_st.ext.block.assoc; a; a = a->next)
5267 gfc_symbol *sym, *tsym;
5268 gfc_expr *target;
5269 int rank, corank;
5271 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
5272 gcc_unreachable ();
5274 sym = a->st->n.sym;
5275 sym->attr.flavor = FL_VARIABLE;
5276 sym->assoc = a;
5277 sym->declared_at = a->where;
5278 gfc_set_sym_referenced (sym);
5280 /* If the selector is a inferred type then the associate_name had better
5281 be as well. Use array references, if present, to identify it as an
5282 array. */
5283 if (IS_INFERRED_TYPE (a->target))
5285 sym->assoc->inferred_type = 1;
5286 for (gfc_ref *r = a->target->ref; r; r = r->next)
5287 if (r->type == REF_ARRAY)
5288 sym->attr.dimension = 1;
5291 /* Initialize the typespec. It is not available in all cases,
5292 however, as it may only be set on the target during resolution.
5293 Still, sometimes it helps to have it right now -- especially
5294 for parsing component references on the associate-name
5295 in case of association to a derived-type. */
5296 sym->ts = a->target->ts;
5297 target = a->target;
5299 /* Don’t share the character length information between associate
5300 variable and target if the length is not a compile-time constant,
5301 as we don’t want to touch some other character length variable when
5302 we try to initialize the associate variable’s character length
5303 variable.
5304 We do it here rather than later so that expressions referencing the
5305 associate variable will automatically have the correctly setup length
5306 information. If we did it at resolution stage the expressions would
5307 use the original length information, and the variable a new different
5308 one, but only the latter one would be correctly initialized at
5309 translation stage, and the former one would need some additional setup
5310 there. */
5311 if (sym->ts.type == BT_CHARACTER
5312 && sym->ts.u.cl
5313 && !(sym->ts.u.cl->length
5314 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT))
5315 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5317 /* If the function has been parsed, go straight to the result to
5318 obtain the expression rank. */
5319 if (target->expr_type == EXPR_FUNCTION
5320 && target->symtree
5321 && target->symtree->n.sym)
5323 tsym = target->symtree->n.sym;
5324 if (!tsym->result)
5325 tsym->result = tsym;
5326 sym->ts = tsym->result->ts;
5327 if (sym->ts.type == BT_CLASS)
5329 if (CLASS_DATA (sym)->as)
5331 target->rank = CLASS_DATA (sym)->as->rank;
5332 target->corank = CLASS_DATA (sym)->as->corank;
5334 sym->attr.class_ok = 1;
5336 else
5338 target->rank = tsym->result->as ? tsym->result->as->rank : 0;
5339 target->corank = tsym->result->as ? tsym->result->as->corank : 0;
5343 /* Check if the target expression is array valued. This cannot be done
5344 by calling gfc_resolve_expr because the context is unavailable.
5345 However, the references can be resolved and the rank of the target
5346 expression set. */
5347 if (!sym->assoc->inferred_type
5348 && target->ref && gfc_resolve_ref (target)
5349 && target->expr_type != EXPR_ARRAY
5350 && target->expr_type != EXPR_COMPCALL)
5351 gfc_expression_rank (target);
5353 /* Determine whether or not function expressions with unknown type are
5354 structure constructors. If so, the function result can be converted
5355 to be a derived type. */
5356 if (target->expr_type == EXPR_FUNCTION
5357 && target->ts.type == BT_UNKNOWN)
5359 gfc_symbol *derived;
5360 /* The derived type has a leading uppercase character. */
5361 gfc_find_symbol (gfc_dt_upper_string (target->symtree->name),
5362 my_ns->parent, 1, &derived);
5363 if (derived && derived->attr.flavor == FL_DERIVED)
5365 sym->ts.type = BT_DERIVED;
5366 sym->ts.u.derived = derived;
5367 sym->assoc->inferred_type = 0;
5371 rank = target->rank;
5372 corank = target->corank;
5373 /* Fixup cases where the ranks are mismatched. */
5374 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
5376 if ((!CLASS_DATA (sym)->as && (rank != 0 || corank != 0))
5377 || (CLASS_DATA (sym)->as
5378 && (CLASS_DATA (sym)->as->rank != rank
5379 || CLASS_DATA (sym)->as->corank != corank))
5380 || rank == -1)
5382 /* Don't just (re-)set the attr and as in the sym.ts,
5383 because this modifies the target's attr and as. Copy the
5384 data and do a build_class_symbol. */
5385 symbol_attribute attr = CLASS_DATA (target)->attr;
5386 gfc_typespec type;
5387 if (rank == -1 && a->ar)
5389 as = gfc_get_array_spec ();
5390 as->rank = a->ar->dimen;
5391 as->corank = 0;
5392 as->type = AS_DEFERRED;
5393 attr.dimension = rank ? 1 : 0;
5394 attr.codimension = as->corank ? 1 : 0;
5395 sym->assoc->variable = true;
5397 else if (rank || corank)
5399 as = gfc_get_array_spec ();
5400 as->type = AS_DEFERRED;
5401 as->rank = rank;
5402 as->corank = corank;
5403 attr.dimension = rank ? 1 : 0;
5404 attr.codimension = corank ? 1 : 0;
5406 else
5408 as = NULL;
5409 attr.dimension = attr.codimension = 0;
5411 attr.class_ok = 0;
5412 attr.associate_var = 1;
5413 type = CLASS_DATA (sym)->ts;
5414 if (!gfc_build_class_symbol (&type, &attr, &as))
5415 gcc_unreachable ();
5416 sym->ts = type;
5417 sym->ts.type = BT_CLASS;
5418 sym->attr.class_ok = 1;
5420 else
5421 sym->attr.class_ok = 1;
5423 else if (rank == -1 && a->ar)
5425 sym->as = gfc_get_array_spec ();
5426 sym->as->rank = a->ar->dimen;
5427 sym->as->corank = a->ar->codimen;
5428 sym->as->type = AS_DEFERRED;
5429 sym->attr.dimension = 1;
5430 sym->attr.codimension = sym->as->corank ? 1 : 0;
5431 sym->attr.pointer = 1;
5433 else if ((!sym->as && (rank != 0 || corank != 0))
5434 || (sym->as
5435 && (sym->as->rank != rank || sym->as->corank != corank)))
5437 as = gfc_get_array_spec ();
5438 as->type = AS_DEFERRED;
5439 as->rank = rank;
5440 as->corank = corank;
5441 sym->as = as;
5442 if (rank)
5443 sym->attr.dimension = 1;
5444 if (corank)
5446 as->cotype = AS_ASSUMED_SHAPE;
5447 sym->attr.codimension = 1;
5450 gfc_commit_symbols ();
5453 accept_statement (ST_ASSOCIATE);
5454 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
5456 loop:
5457 st = parse_executable (ST_NONE);
5458 switch (st)
5460 case ST_NONE:
5461 unexpected_eof ();
5463 case_end:
5464 accept_statement (st);
5465 my_ns->code = gfc_state_stack->head;
5466 break;
5468 default:
5469 unexpected_statement (st);
5470 goto loop;
5473 gfc_current_ns = gfc_current_ns->parent;
5474 pop_state ();
5478 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
5479 handled inside of parse_executable(), because they aren't really
5480 loop statements. */
5482 static void
5483 parse_do_block (void)
5485 gfc_statement st;
5486 gfc_code *top;
5487 gfc_state_data s;
5488 gfc_symtree *stree;
5489 gfc_exec_op do_op;
5491 do_op = new_st.op;
5492 s.ext.end_do_label = new_st.label1;
5494 if (do_op == EXEC_DO_CONCURRENT)
5496 gfc_forall_iterator *fa;
5497 for (fa = new_st.ext.concur.forall_iterator; fa; fa = fa->next)
5499 /* Apply unroll only to innermost loop (first control
5500 variable). */
5501 if (directive_unroll != -1)
5503 fa->annot.unroll = directive_unroll;
5504 directive_unroll = -1;
5506 if (directive_ivdep)
5507 fa->annot.ivdep = directive_ivdep;
5508 if (directive_vector)
5509 fa->annot.vector = directive_vector;
5510 if (directive_novector)
5511 fa->annot.novector = directive_novector;
5513 directive_ivdep = false;
5514 directive_vector = false;
5515 directive_novector = false;
5516 stree = NULL;
5518 else if (new_st.ext.iterator != NULL)
5520 stree = new_st.ext.iterator->var->symtree;
5521 if (directive_unroll != -1)
5523 new_st.ext.iterator->annot.unroll = directive_unroll;
5524 directive_unroll = -1;
5526 if (directive_ivdep)
5528 new_st.ext.iterator->annot.ivdep = directive_ivdep;
5529 directive_ivdep = false;
5531 if (directive_vector)
5533 new_st.ext.iterator->annot.vector = directive_vector;
5534 directive_vector = false;
5536 if (directive_novector)
5538 new_st.ext.iterator->annot.novector = directive_novector;
5539 directive_novector = false;
5542 else
5543 stree = NULL;
5545 accept_statement (ST_DO);
5547 top = gfc_state_stack->tail;
5548 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
5549 gfc_new_block);
5551 s.do_variable = stree;
5553 top->block = new_level (top);
5554 top->block->op = EXEC_DO;
5556 loop:
5557 st = parse_executable (ST_NONE);
5559 switch (st)
5561 case ST_NONE:
5562 unexpected_eof ();
5564 case ST_ENDDO:
5565 if (s.ext.end_do_label != NULL
5566 && s.ext.end_do_label != gfc_statement_label)
5567 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
5568 "DO label");
5570 if (gfc_statement_label != NULL)
5572 new_st.op = EXEC_NOP;
5573 add_statement ();
5575 break;
5577 case ST_IMPLIED_ENDDO:
5578 /* If the do-stmt of this DO construct has a do-construct-name,
5579 the corresponding end-do must be an end-do-stmt (with a matching
5580 name, but in that case we must have seen ST_ENDDO first).
5581 We only complain about this in pedantic mode. */
5582 if (gfc_current_block () != NULL)
5583 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
5584 &gfc_current_block()->declared_at);
5586 break;
5588 default:
5589 unexpected_statement (st);
5590 goto loop;
5593 pop_state ();
5594 accept_statement (st);
5597 /* Get the corresponding ending statement type for the OpenMP directive
5598 OMP_ST. If it does not have one, return ST_NONE. */
5600 gfc_statement
5601 gfc_omp_end_stmt (gfc_statement omp_st,
5602 bool omp_do_p, bool omp_structured_p)
5604 if (omp_do_p)
5606 switch (omp_st)
5608 case ST_OMP_DISTRIBUTE: return ST_OMP_END_DISTRIBUTE;
5609 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
5610 return ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
5611 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
5612 return ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
5613 case ST_OMP_DISTRIBUTE_SIMD:
5614 return ST_OMP_END_DISTRIBUTE_SIMD;
5615 case ST_OMP_DO: return ST_OMP_END_DO;
5616 case ST_OMP_DO_SIMD: return ST_OMP_END_DO_SIMD;
5617 case ST_OMP_LOOP: return ST_OMP_END_LOOP;
5618 case ST_OMP_PARALLEL_DO: return ST_OMP_END_PARALLEL_DO;
5619 case ST_OMP_PARALLEL_DO_SIMD:
5620 return ST_OMP_END_PARALLEL_DO_SIMD;
5621 case ST_OMP_PARALLEL_LOOP:
5622 return ST_OMP_END_PARALLEL_LOOP;
5623 case ST_OMP_SIMD: return ST_OMP_END_SIMD;
5624 case ST_OMP_TARGET_PARALLEL_DO:
5625 return ST_OMP_END_TARGET_PARALLEL_DO;
5626 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
5627 return ST_OMP_END_TARGET_PARALLEL_DO_SIMD;
5628 case ST_OMP_TARGET_PARALLEL_LOOP:
5629 return ST_OMP_END_TARGET_PARALLEL_LOOP;
5630 case ST_OMP_TARGET_SIMD: return ST_OMP_END_TARGET_SIMD;
5631 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
5632 return ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
5633 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
5634 return ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
5635 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5636 return ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
5637 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
5638 return ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
5639 case ST_OMP_TARGET_TEAMS_LOOP:
5640 return ST_OMP_END_TARGET_TEAMS_LOOP;
5641 case ST_OMP_TASKLOOP: return ST_OMP_END_TASKLOOP;
5642 case ST_OMP_TASKLOOP_SIMD: return ST_OMP_END_TASKLOOP_SIMD;
5643 case ST_OMP_MASKED_TASKLOOP: return ST_OMP_END_MASKED_TASKLOOP;
5644 case ST_OMP_MASKED_TASKLOOP_SIMD:
5645 return ST_OMP_END_MASKED_TASKLOOP_SIMD;
5646 case ST_OMP_MASTER_TASKLOOP: return ST_OMP_END_MASTER_TASKLOOP;
5647 case ST_OMP_MASTER_TASKLOOP_SIMD:
5648 return ST_OMP_END_MASTER_TASKLOOP_SIMD;
5649 case ST_OMP_PARALLEL_MASKED_TASKLOOP:
5650 return ST_OMP_END_PARALLEL_MASKED_TASKLOOP;
5651 case ST_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
5652 return ST_OMP_END_PARALLEL_MASKED_TASKLOOP_SIMD;
5653 case ST_OMP_PARALLEL_MASTER_TASKLOOP:
5654 return ST_OMP_END_PARALLEL_MASTER_TASKLOOP;
5655 case ST_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
5656 return ST_OMP_END_PARALLEL_MASTER_TASKLOOP_SIMD;
5657 case ST_OMP_TEAMS_DISTRIBUTE:
5658 return ST_OMP_END_TEAMS_DISTRIBUTE;
5659 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
5660 return ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
5661 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5662 return ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
5663 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
5664 return ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
5665 case ST_OMP_TEAMS_LOOP:
5666 return ST_OMP_END_TEAMS_LOOP;
5667 case ST_OMP_TILE:
5668 return ST_OMP_END_TILE;
5669 case ST_OMP_UNROLL:
5670 return ST_OMP_END_UNROLL;
5671 default:
5672 break;
5676 if (omp_structured_p)
5678 switch (omp_st)
5680 case ST_OMP_ALLOCATORS:
5681 return ST_OMP_END_ALLOCATORS;
5682 case ST_OMP_ASSUME:
5683 return ST_OMP_END_ASSUME;
5684 case ST_OMP_ATOMIC:
5685 return ST_OMP_END_ATOMIC;
5686 case ST_OMP_DISPATCH:
5687 return ST_OMP_END_DISPATCH;
5688 case ST_OMP_PARALLEL:
5689 return ST_OMP_END_PARALLEL;
5690 case ST_OMP_PARALLEL_MASKED:
5691 return ST_OMP_END_PARALLEL_MASKED;
5692 case ST_OMP_PARALLEL_MASTER:
5693 return ST_OMP_END_PARALLEL_MASTER;
5694 case ST_OMP_PARALLEL_SECTIONS:
5695 return ST_OMP_END_PARALLEL_SECTIONS;
5696 case ST_OMP_SCOPE:
5697 return ST_OMP_END_SCOPE;
5698 case ST_OMP_SECTIONS:
5699 return ST_OMP_END_SECTIONS;
5700 case ST_OMP_ORDERED:
5701 return ST_OMP_END_ORDERED;
5702 case ST_OMP_CRITICAL:
5703 return ST_OMP_END_CRITICAL;
5704 case ST_OMP_MASKED:
5705 return ST_OMP_END_MASKED;
5706 case ST_OMP_MASTER:
5707 return ST_OMP_END_MASTER;
5708 case ST_OMP_SINGLE:
5709 return ST_OMP_END_SINGLE;
5710 case ST_OMP_TARGET:
5711 return ST_OMP_END_TARGET;
5712 case ST_OMP_TARGET_DATA:
5713 return ST_OMP_END_TARGET_DATA;
5714 case ST_OMP_TARGET_PARALLEL:
5715 return ST_OMP_END_TARGET_PARALLEL;
5716 case ST_OMP_TARGET_TEAMS:
5717 return ST_OMP_END_TARGET_TEAMS;
5718 case ST_OMP_TASK:
5719 return ST_OMP_END_TASK;
5720 case ST_OMP_TASKGROUP:
5721 return ST_OMP_END_TASKGROUP;
5722 case ST_OMP_TEAMS:
5723 return ST_OMP_END_TEAMS;
5724 case ST_OMP_TEAMS_DISTRIBUTE:
5725 return ST_OMP_END_TEAMS_DISTRIBUTE;
5726 case ST_OMP_DISTRIBUTE:
5727 return ST_OMP_END_DISTRIBUTE;
5728 case ST_OMP_WORKSHARE:
5729 return ST_OMP_END_WORKSHARE;
5730 case ST_OMP_PARALLEL_WORKSHARE:
5731 return ST_OMP_END_PARALLEL_WORKSHARE;
5732 case ST_OMP_BEGIN_METADIRECTIVE:
5733 return ST_OMP_END_METADIRECTIVE;
5734 default:
5735 break;
5739 return ST_NONE;
5742 /* Parse the statements of OpenMP do/parallel do. */
5744 static gfc_statement
5745 parse_omp_do (gfc_statement omp_st, int nested)
5747 gfc_statement st;
5748 gfc_code *cp, *np;
5749 gfc_state_data s;
5751 accept_statement (omp_st);
5753 cp = gfc_state_stack->tail;
5754 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
5755 np = new_level (cp);
5756 np->op = cp->op;
5757 np->block = NULL;
5759 for (;;)
5761 st = next_statement ();
5762 if (st == ST_NONE)
5763 unexpected_eof ();
5764 else if (st == ST_DO)
5765 break;
5766 else if (st == ST_OMP_UNROLL || st == ST_OMP_TILE)
5768 st = parse_omp_do (st, nested + 1);
5769 if (st == ST_IMPLIED_ENDDO)
5770 return st;
5771 goto do_end;
5773 else
5774 unexpected_statement (st);
5777 parse_do_block ();
5778 for (; nested; --nested)
5779 pop_state ();
5780 if (gfc_statement_label != NULL
5781 && gfc_state_stack->previous != NULL
5782 && gfc_state_stack->previous->state == COMP_DO
5783 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
5785 /* In
5786 DO 100 I=1,10
5787 !$OMP DO
5788 DO J=1,10
5790 100 CONTINUE
5791 there should be no !$OMP END DO. */
5792 pop_state ();
5793 return ST_IMPLIED_ENDDO;
5796 check_do_closure ();
5797 pop_state ();
5799 st = next_statement ();
5800 do_end:
5801 gfc_statement omp_end_st = gfc_omp_end_stmt (omp_st, true, false);
5802 if (omp_st == ST_NONE)
5803 gcc_unreachable ();
5805 /* If handling a metadirective variant, treat 'omp end metadirective'
5806 as the expected end statement for the current construct. */
5807 if (st == ST_OMP_END_METADIRECTIVE
5808 && gfc_state_stack->state == COMP_OMP_BEGIN_METADIRECTIVE)
5809 st = omp_end_st;
5811 if (st == omp_end_st)
5813 if (new_st.op == EXEC_OMP_END_NOWAIT)
5815 if (cp->ext.omp_clauses->nowait && new_st.ext.omp_bool)
5816 gfc_error_now ("Duplicated NOWAIT clause on %s and %s at %C",
5817 gfc_ascii_statement (omp_st),
5818 gfc_ascii_statement (omp_end_st));
5819 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
5821 else
5822 gcc_assert (new_st.op == EXEC_NOP);
5823 gfc_clear_new_st ();
5824 gfc_commit_symbols ();
5825 gfc_warning_check ();
5826 st = next_statement ();
5828 return st;
5832 /* Parse the statements of OpenMP atomic directive. */
5834 static gfc_statement
5835 parse_omp_oacc_atomic (bool omp_p)
5837 gfc_statement st, st_atomic, st_end_atomic;
5838 gfc_code *cp, *np;
5839 gfc_state_data s;
5840 int count;
5842 if (omp_p)
5844 st_atomic = ST_OMP_ATOMIC;
5845 if (gfc_state_stack->state == COMP_OMP_BEGIN_METADIRECTIVE)
5846 st_end_atomic = ST_OMP_END_METADIRECTIVE;
5847 else
5848 st_end_atomic = ST_OMP_END_ATOMIC;
5850 else
5852 st_atomic = ST_OACC_ATOMIC;
5853 st_end_atomic = ST_OACC_END_ATOMIC;
5855 accept_statement (st_atomic);
5857 cp = gfc_state_stack->tail;
5858 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
5859 np = new_level (cp);
5860 np->op = cp->op;
5861 np->block = NULL;
5862 np->ext.omp_clauses = cp->ext.omp_clauses;
5863 cp->ext.omp_clauses = NULL;
5864 count = 1 + np->ext.omp_clauses->capture;
5866 while (count)
5868 st = next_statement ();
5869 if (st == ST_NONE)
5870 unexpected_eof ();
5871 else if (np->ext.omp_clauses->compare
5872 && (st == ST_SIMPLE_IF || st == ST_IF_BLOCK))
5874 count--;
5875 if (st == ST_IF_BLOCK)
5877 parse_if_block ();
5878 /* With else (or elseif). */
5879 if (gfc_state_stack->tail->block->block)
5880 count--;
5882 accept_statement (st);
5884 else if (st == ST_ASSIGNMENT
5885 && (!np->ext.omp_clauses->compare
5886 || np->ext.omp_clauses->capture))
5888 accept_statement (st);
5889 count--;
5891 else
5892 unexpected_statement (st);
5895 pop_state ();
5897 st = next_statement ();
5898 if (st == st_end_atomic)
5900 gfc_clear_new_st ();
5901 gfc_commit_symbols ();
5902 gfc_warning_check ();
5903 st = next_statement ();
5905 return st;
5909 /* Parse the statements of an OpenACC structured block. */
5911 static void
5912 parse_oacc_structured_block (gfc_statement acc_st)
5914 gfc_statement st, acc_end_st;
5915 gfc_code *cp, *np;
5916 gfc_state_data s, *sd;
5918 for (sd = gfc_state_stack; sd; sd = sd->previous)
5919 if (sd->state == COMP_CRITICAL)
5920 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
5922 accept_statement (acc_st);
5924 cp = gfc_state_stack->tail;
5925 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
5926 np = new_level (cp);
5927 np->op = cp->op;
5928 np->block = NULL;
5929 switch (acc_st)
5931 case ST_OACC_PARALLEL:
5932 acc_end_st = ST_OACC_END_PARALLEL;
5933 break;
5934 case ST_OACC_KERNELS:
5935 acc_end_st = ST_OACC_END_KERNELS;
5936 break;
5937 case ST_OACC_SERIAL:
5938 acc_end_st = ST_OACC_END_SERIAL;
5939 break;
5940 case ST_OACC_DATA:
5941 acc_end_st = ST_OACC_END_DATA;
5942 break;
5943 case ST_OACC_HOST_DATA:
5944 acc_end_st = ST_OACC_END_HOST_DATA;
5945 break;
5946 default:
5947 gcc_unreachable ();
5952 st = parse_executable (ST_NONE);
5953 if (st == ST_NONE)
5954 unexpected_eof ();
5955 else if (st != acc_end_st)
5957 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
5958 reject_statement ();
5961 while (st != acc_end_st);
5963 gcc_assert (new_st.op == EXEC_NOP);
5965 gfc_clear_new_st ();
5966 gfc_commit_symbols ();
5967 gfc_warning_check ();
5968 pop_state ();
5971 /* Parse the statements of OpenACC 'loop', or combined compute 'loop'. */
5973 static gfc_statement
5974 parse_oacc_loop (gfc_statement acc_st)
5976 gfc_statement st;
5977 gfc_code *cp, *np;
5978 gfc_state_data s, *sd;
5980 for (sd = gfc_state_stack; sd; sd = sd->previous)
5981 if (sd->state == COMP_CRITICAL)
5982 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
5984 accept_statement (acc_st);
5986 cp = gfc_state_stack->tail;
5987 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
5988 np = new_level (cp);
5989 np->op = cp->op;
5990 np->block = NULL;
5992 for (;;)
5994 st = next_statement ();
5995 if (st == ST_NONE)
5996 unexpected_eof ();
5997 else if (st == ST_DO)
5998 break;
5999 else
6001 gfc_error ("Expected DO loop at %C");
6002 reject_statement ();
6006 parse_do_block ();
6007 if (gfc_statement_label != NULL
6008 && gfc_state_stack->previous != NULL
6009 && gfc_state_stack->previous->state == COMP_DO
6010 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
6012 pop_state ();
6013 return ST_IMPLIED_ENDDO;
6016 check_do_closure ();
6017 pop_state ();
6019 st = next_statement ();
6020 if (st == ST_OACC_END_LOOP)
6021 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
6022 if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
6023 (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
6024 (acc_st == ST_OACC_SERIAL_LOOP && st == ST_OACC_END_SERIAL_LOOP) ||
6025 (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
6027 gcc_assert (new_st.op == EXEC_NOP);
6028 gfc_clear_new_st ();
6029 gfc_commit_symbols ();
6030 gfc_warning_check ();
6031 st = next_statement ();
6033 return st;
6037 /* Parse an OpenMP allocate block, including optional ALLOCATORS
6038 end directive. */
6040 static gfc_statement
6041 parse_openmp_allocate_block (gfc_statement omp_st)
6043 gfc_statement st;
6044 gfc_code *cp, *np;
6045 gfc_state_data s;
6046 bool empty_list = false;
6047 locus empty_list_loc;
6048 gfc_omp_namelist *n_first = new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE];
6050 if (omp_st == ST_OMP_ALLOCATE_EXEC
6051 && new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE]->sym == NULL)
6053 empty_list = true;
6054 empty_list_loc = new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE]->where;
6057 accept_statement (omp_st);
6059 cp = gfc_state_stack->tail;
6060 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
6061 np = new_level (cp);
6062 np->op = cp->op;
6063 np->block = NULL;
6065 st = next_statement ();
6066 while (omp_st == ST_OMP_ALLOCATE_EXEC && st == ST_OMP_ALLOCATE_EXEC)
6068 if (empty_list && !new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE]->sym)
6070 locus *loc = &new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE]->where;
6071 gfc_error_now ("%s statements at %L and %L have both no list item but"
6072 " only one may", gfc_ascii_statement (st),
6073 &empty_list_loc, loc);
6074 empty_list = false;
6076 if (!new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE]->sym)
6078 empty_list = true;
6079 empty_list_loc = new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE]->where;
6081 for ( ; n_first->next; n_first = n_first->next)
6083 n_first->next = new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE];
6084 new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE] = NULL;
6085 gfc_free_omp_clauses (new_st.ext.omp_clauses);
6087 accept_statement (ST_NONE);
6088 st = next_statement ();
6090 if (st != ST_ALLOCATE && omp_st == ST_OMP_ALLOCATE_EXEC)
6091 gfc_error_now ("Unexpected %s at %C; expected ALLOCATE or %s statement",
6092 gfc_ascii_statement (st), gfc_ascii_statement (omp_st));
6093 else if (st != ST_ALLOCATE)
6094 gfc_error_now ("Unexpected %s at %C; expected ALLOCATE statement after %s",
6095 gfc_ascii_statement (st), gfc_ascii_statement (omp_st));
6096 accept_statement (st);
6097 pop_state ();
6098 st = next_statement ();
6099 if (omp_st == ST_OMP_ALLOCATORS
6100 && (st == ST_OMP_END_ALLOCATORS
6101 || (st == ST_OMP_END_METADIRECTIVE
6102 && gfc_state_stack->state == COMP_OMP_BEGIN_METADIRECTIVE)))
6104 accept_statement (st);
6105 st = next_statement ();
6107 return st;
6111 /* Parse the statements of an OpenMP structured block. */
6113 static gfc_statement
6114 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
6116 gfc_statement st, omp_end_st, first_st;
6117 gfc_code *cp, *np;
6118 gfc_state_data s, s2;
6120 accept_statement (omp_st);
6122 cp = gfc_state_stack->tail;
6123 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
6124 np = new_level (cp);
6125 np->op = cp->op;
6126 np->block = NULL;
6128 omp_end_st = gfc_omp_end_stmt (omp_st, false, true);
6129 if (omp_end_st == ST_NONE)
6130 gcc_unreachable ();
6132 /* If handling a metadirective variant, treat 'omp end metadirective'
6133 as the expected end statement for the current construct. */
6134 if (gfc_state_stack->previous != NULL
6135 && gfc_state_stack->previous->state == COMP_OMP_BEGIN_METADIRECTIVE)
6136 omp_end_st = ST_OMP_END_METADIRECTIVE;
6138 bool block_construct = false;
6139 gfc_namespace *my_ns = NULL;
6140 gfc_namespace *my_parent = NULL;
6142 first_st = st = next_statement ();
6144 if (st == ST_BLOCK)
6146 /* Adjust state to a strictly-structured block, now that we found that
6147 the body starts with a BLOCK construct. */
6148 s.state = COMP_OMP_STRICTLY_STRUCTURED_BLOCK;
6150 block_construct = true;
6151 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
6153 my_ns = gfc_build_block_ns (gfc_current_ns);
6154 new_st.op = EXEC_BLOCK;
6155 new_st.ext.block.ns = my_ns;
6156 new_st.ext.block.assoc = NULL;
6157 accept_statement (ST_BLOCK);
6159 push_state (&s2, COMP_BLOCK, my_ns->proc_name);
6160 gfc_current_ns = my_ns;
6161 my_parent = my_ns->parent;
6162 if (omp_st == ST_OMP_SECTIONS
6163 || omp_st == ST_OMP_PARALLEL_SECTIONS)
6165 np = new_level (cp);
6166 np->op = cp->op;
6169 first_st = next_statement ();
6170 st = parse_spec (first_st);
6173 if (omp_end_st == ST_OMP_END_TARGET)
6174 switch (first_st)
6176 case ST_OMP_TEAMS:
6177 case ST_OMP_TEAMS_DISTRIBUTE:
6178 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
6179 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
6180 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
6181 case ST_OMP_TEAMS_LOOP:
6182 case ST_OMP_METADIRECTIVE:
6183 case ST_OMP_BEGIN_METADIRECTIVE:
6185 gfc_state_data *stk = gfc_state_stack->previous;
6186 if (stk->state == COMP_OMP_STRICTLY_STRUCTURED_BLOCK)
6187 stk = stk->previous;
6188 stk->tail->ext.omp_clauses->target_first_st_is_teams_or_meta = true;
6189 break;
6191 default:
6192 break;
6197 if (workshare_stmts_only)
6199 /* Inside of !$omp workshare, only
6200 scalar assignments
6201 array assignments
6202 where statements and constructs
6203 forall statements and constructs
6204 !$omp atomic
6205 !$omp critical
6206 !$omp parallel
6207 are allowed. For !$omp critical these
6208 restrictions apply recursively. */
6209 bool cycle = true;
6211 for (;;)
6213 switch (st)
6215 case ST_NONE:
6216 unexpected_eof ();
6218 case ST_ASSIGNMENT:
6219 case ST_WHERE:
6220 case ST_FORALL:
6221 accept_statement (st);
6222 break;
6224 case ST_WHERE_BLOCK:
6225 parse_where_block ();
6226 break;
6228 case ST_FORALL_BLOCK:
6229 parse_forall_block ();
6230 break;
6232 case ST_OMP_ALLOCATE_EXEC:
6233 case ST_OMP_ALLOCATORS:
6234 st = parse_openmp_allocate_block (st);
6235 continue;
6237 case ST_OMP_ASSUME:
6238 case ST_OMP_PARALLEL:
6239 case ST_OMP_PARALLEL_MASKED:
6240 case ST_OMP_PARALLEL_MASTER:
6241 case ST_OMP_PARALLEL_SECTIONS:
6242 st = parse_omp_structured_block (st, false);
6243 continue;
6245 case ST_OMP_PARALLEL_WORKSHARE:
6246 case ST_OMP_CRITICAL:
6247 st = parse_omp_structured_block (st, true);
6248 continue;
6250 case ST_OMP_PARALLEL_DO:
6251 case ST_OMP_PARALLEL_DO_SIMD:
6252 st = parse_omp_do (st, 0);
6253 continue;
6255 case ST_OMP_ATOMIC:
6256 st = parse_omp_oacc_atomic (true);
6257 continue;
6259 default:
6260 cycle = false;
6261 break;
6264 if (!cycle)
6265 break;
6267 st = next_statement ();
6270 else
6271 st = parse_executable (st);
6272 if (st == ST_NONE)
6273 unexpected_eof ();
6274 else if (st == ST_OMP_SECTION
6275 && (omp_st == ST_OMP_SECTIONS
6276 || omp_st == ST_OMP_PARALLEL_SECTIONS))
6278 np = new_level (np);
6279 np->op = cp->op;
6280 np->block = NULL;
6281 st = next_statement ();
6283 else if (block_construct && st == ST_END_BLOCK)
6285 accept_statement (st);
6286 gfc_current_ns->code = gfc_state_stack->head;
6287 gfc_current_ns = my_parent;
6288 pop_state (); /* Inner BLOCK */
6289 pop_state (); /* Outer COMP_OMP_STRICTLY_STRUCTURED_BLOCK */
6291 st = next_statement ();
6292 if (st == omp_end_st)
6294 accept_statement (st);
6295 st = next_statement ();
6297 return st;
6299 else if (st != omp_end_st || block_construct)
6301 unexpected_statement (st);
6302 st = next_statement ();
6305 while (st != omp_end_st);
6307 switch (new_st.op)
6309 case EXEC_OMP_END_NOWAIT:
6310 if (cp->ext.omp_clauses->nowait && new_st.ext.omp_bool)
6311 gfc_error_now ("Duplicated NOWAIT clause on %s and %s at %C",
6312 gfc_ascii_statement (omp_st),
6313 gfc_ascii_statement (omp_end_st));
6314 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
6315 break;
6316 case EXEC_OMP_END_CRITICAL:
6317 if (((cp->ext.omp_clauses->critical_name == NULL)
6318 ^ (new_st.ext.omp_name == NULL))
6319 || (new_st.ext.omp_name != NULL
6320 && strcmp (cp->ext.omp_clauses->critical_name,
6321 new_st.ext.omp_name) != 0))
6322 gfc_error ("Name after !$omp critical and !$omp end critical does "
6323 "not match at %C");
6324 free (CONST_CAST (char *, new_st.ext.omp_name));
6325 new_st.ext.omp_name = NULL;
6326 break;
6327 case EXEC_OMP_END_SINGLE:
6328 if (cp->ext.omp_clauses->nowait && new_st.ext.omp_clauses->nowait)
6329 gfc_error_now ("Duplicated NOWAIT clause on %s and %s at %C",
6330 gfc_ascii_statement (omp_st),
6331 gfc_ascii_statement (omp_end_st));
6332 cp->ext.omp_clauses->nowait |= new_st.ext.omp_clauses->nowait;
6333 if (cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE])
6335 gfc_omp_namelist *nl;
6336 for (nl = cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
6337 nl->next; nl = nl->next)
6339 nl->next = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
6341 else
6342 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
6343 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
6344 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
6345 gfc_free_omp_clauses (new_st.ext.omp_clauses);
6346 break;
6347 case EXEC_NOP:
6348 break;
6349 default:
6350 gcc_unreachable ();
6353 gfc_clear_new_st ();
6354 gfc_commit_symbols ();
6355 gfc_warning_check ();
6356 pop_state ();
6357 st = next_statement ();
6358 return st;
6361 static gfc_statement
6362 parse_omp_dispatch (void)
6364 gfc_statement st;
6365 gfc_code *cp, *np;
6366 gfc_state_data s;
6368 accept_statement (ST_OMP_DISPATCH);
6370 cp = gfc_state_stack->tail;
6371 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
6372 np = new_level (cp);
6373 np->op = cp->op;
6374 np->block = NULL;
6376 st = next_statement ();
6377 if (st == ST_NONE)
6378 return st;
6379 if (st == ST_CALL || st == ST_ASSIGNMENT)
6380 accept_statement (st);
6381 else
6383 gfc_error ("%<OMP DISPATCH%> directive must be followed by a procedure "
6384 "call with optional assignment at %C");
6385 reject_statement ();
6387 pop_state ();
6388 st = next_statement ();
6389 if (st == ST_OMP_END_DISPATCH
6390 || (st == ST_OMP_END_METADIRECTIVE
6391 && gfc_state_stack->state == COMP_OMP_BEGIN_METADIRECTIVE))
6393 if (cp->ext.omp_clauses->nowait && new_st.ext.omp_bool)
6394 gfc_error_now ("Duplicated NOWAIT clause on !$OMP DISPATCH and !$OMP "
6395 "END DISPATCH at %C");
6396 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
6397 accept_statement (st);
6398 st = next_statement ();
6400 return st;
6403 static gfc_statement
6404 parse_omp_metadirective_body (gfc_statement omp_st)
6406 gfc_omp_variant *variant
6407 = new_st.ext.omp_variants;
6408 locus body_locus = gfc_current_locus;
6410 accept_statement (omp_st);
6412 gfc_statement next_st = ST_NONE;
6414 while (variant)
6416 gfc_current_locus = body_locus;
6417 gfc_state_data s;
6418 bool workshare_p
6419 = (variant->stmt == ST_OMP_WORKSHARE
6420 || variant->stmt == ST_OMP_PARALLEL_WORKSHARE);
6421 enum gfc_compile_state new_state
6422 = (omp_st == ST_OMP_METADIRECTIVE
6423 ? COMP_OMP_METADIRECTIVE : COMP_OMP_BEGIN_METADIRECTIVE);
6425 new_st = *variant->code;
6426 push_state (&s, new_state, NULL);
6428 gfc_statement st;
6429 bool old_in_metadirective_body = gfc_in_omp_metadirective_body;
6430 gfc_in_omp_metadirective_body = true;
6432 gfc_omp_metadirective_region_count++;
6433 switch (variant->stmt)
6435 case_omp_structured_block:
6436 st = parse_omp_structured_block (variant->stmt, workshare_p);
6437 break;
6438 case_omp_do:
6439 st = parse_omp_do (variant->stmt, 0);
6440 /* TODO: Does st == ST_IMPLIED_ENDDO need special handling? */
6441 break;
6442 case ST_OMP_ALLOCATORS:
6443 st = parse_openmp_allocate_block (variant->stmt);
6444 break;
6445 case ST_OMP_ATOMIC:
6446 st = parse_omp_oacc_atomic (true);
6447 break;
6448 case ST_OMP_DISPATCH:
6449 st = parse_omp_dispatch ();
6450 break;
6451 default:
6452 accept_statement (variant->stmt);
6453 st = parse_executable (next_statement ());
6454 break;
6457 if (gfc_state_stack->state == COMP_OMP_METADIRECTIVE
6458 && startswith (gfc_ascii_statement (st), "!$OMP END "))
6460 for (gfc_state_data *p = gfc_state_stack; p; p = p->previous)
6461 if (p->state == COMP_OMP_STRUCTURED_BLOCK
6462 || p->state == COMP_OMP_BEGIN_METADIRECTIVE)
6463 goto finish;
6464 gfc_error ("Unexpected %s statement in OMP METADIRECTIVE "
6465 "block at %C",
6466 gfc_ascii_statement (st));
6467 reject_statement ();
6468 st = next_statement ();
6470 finish:
6472 gfc_in_omp_metadirective_body = old_in_metadirective_body;
6474 if (gfc_state_stack->head)
6475 *variant->code = *gfc_state_stack->head;
6476 pop_state ();
6478 gfc_commit_symbols ();
6479 gfc_warning_check ();
6480 if (variant->next)
6481 gfc_clear_new_st ();
6483 /* Sanity-check that each variant finishes parsing at the same place. */
6484 if (next_st == ST_NONE)
6485 next_st = st;
6486 else
6487 gcc_assert (st == next_st);
6489 variant = variant->next;
6492 return next_st;
6495 /* Accept a series of executable statements. We return the first
6496 statement that doesn't fit to the caller. Any block statements are
6497 passed on to the correct handler, which usually passes the buck
6498 right back here. */
6500 static gfc_statement
6501 parse_executable (gfc_statement st)
6503 int close_flag;
6504 bool one_stmt_p = false;
6505 in_exec_part = true;
6507 if (st == ST_NONE)
6508 st = next_statement ();
6510 for (;;)
6512 /* Only parse one statement for the form of metadirective without
6513 an explicit begin..end. */
6514 if (gfc_state_stack->state == COMP_OMP_METADIRECTIVE && one_stmt_p)
6515 return st;
6516 one_stmt_p = true;
6518 close_flag = check_do_closure ();
6519 if (close_flag)
6520 switch (st)
6522 case ST_GOTO:
6523 case ST_END_PROGRAM:
6524 case ST_RETURN:
6525 case ST_EXIT:
6526 case ST_END_FUNCTION:
6527 case ST_CYCLE:
6528 case ST_PAUSE:
6529 case ST_STOP:
6530 case ST_ERROR_STOP:
6531 case ST_END_SUBROUTINE:
6533 case ST_DO:
6534 case ST_FORALL:
6535 case ST_WHERE:
6536 case ST_SELECT_CASE:
6537 gfc_error ("%s statement at %C cannot terminate a non-block "
6538 "DO loop", gfc_ascii_statement (st));
6539 break;
6541 default:
6542 break;
6545 switch (st)
6547 case ST_NONE:
6548 unexpected_eof ();
6550 case ST_DATA:
6551 gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
6552 "first executable statement");
6553 /* Fall through. */
6555 case ST_FORMAT:
6556 case ST_ENTRY:
6557 case_executable:
6558 accept_statement (st);
6559 if (close_flag == 1)
6560 return ST_IMPLIED_ENDDO;
6561 break;
6563 case ST_BLOCK:
6564 parse_block_construct ();
6565 break;
6567 case ST_ASSOCIATE:
6568 parse_associate ();
6569 break;
6571 case ST_IF_BLOCK:
6572 parse_if_block ();
6573 break;
6575 case ST_SELECT_CASE:
6576 parse_select_block ();
6577 break;
6579 case ST_SELECT_TYPE:
6580 parse_select_type_block ();
6581 break;
6583 case ST_SELECT_RANK:
6584 parse_select_rank_block ();
6585 break;
6587 case ST_DO:
6588 parse_do_block ();
6589 if (check_do_closure () == 1)
6590 return ST_IMPLIED_ENDDO;
6591 break;
6593 case ST_CRITICAL:
6594 parse_critical_block ();
6595 break;
6597 case ST_WHERE_BLOCK:
6598 parse_where_block ();
6599 break;
6601 case ST_FORALL_BLOCK:
6602 parse_forall_block ();
6603 break;
6605 case ST_OACC_PARALLEL_LOOP:
6606 case ST_OACC_KERNELS_LOOP:
6607 case ST_OACC_SERIAL_LOOP:
6608 case ST_OACC_LOOP:
6609 st = parse_oacc_loop (st);
6610 if (st == ST_IMPLIED_ENDDO)
6611 return st;
6612 continue;
6614 case ST_OACC_PARALLEL:
6615 case ST_OACC_KERNELS:
6616 case ST_OACC_SERIAL:
6617 case ST_OACC_DATA:
6618 case ST_OACC_HOST_DATA:
6619 parse_oacc_structured_block (st);
6620 break;
6622 case ST_OMP_ALLOCATE_EXEC:
6623 case ST_OMP_ALLOCATORS:
6624 st = parse_openmp_allocate_block (st);
6625 continue;
6627 case_omp_structured_block:
6628 st = parse_omp_structured_block (st,
6629 st == ST_OMP_WORKSHARE
6630 || st == ST_OMP_PARALLEL_WORKSHARE);
6631 continue;
6633 case_omp_do:
6634 st = parse_omp_do (st, 0);
6635 if (st == ST_IMPLIED_ENDDO)
6636 return st;
6637 continue;
6639 case ST_OACC_ATOMIC:
6640 st = parse_omp_oacc_atomic (false);
6641 continue;
6643 case ST_OMP_ATOMIC:
6644 st = parse_omp_oacc_atomic (true);
6645 continue;
6647 case ST_OMP_DISPATCH:
6648 st = parse_omp_dispatch ();
6649 continue;
6651 case ST_OMP_METADIRECTIVE:
6652 case ST_OMP_BEGIN_METADIRECTIVE:
6653 st = parse_omp_metadirective_body (st);
6654 continue;
6656 case ST_OMP_END_METADIRECTIVE:
6657 if (gfc_state_stack->state == COMP_OMP_BEGIN_METADIRECTIVE)
6658 return next_statement ();
6659 else
6660 return st;
6662 default:
6663 return st;
6666 if (directive_unroll != -1)
6667 gfc_error ("%<GCC unroll%> directive not at the start of a loop at %C");
6669 if (directive_ivdep)
6670 gfc_error ("%<GCC ivdep%> directive not at the start of a loop at %C");
6672 if (directive_vector)
6673 gfc_error ("%<GCC vector%> directive not at the start of a loop at %C");
6675 if (directive_novector)
6676 gfc_error ("%<GCC novector%> "
6677 "directive not at the start of a loop at %C");
6679 st = next_statement ();
6684 /* Fix the symbols for sibling functions. These are incorrectly added to
6685 the child namespace as the parser didn't know about this procedure. */
6687 static void
6688 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
6690 gfc_namespace *ns;
6691 gfc_symtree *st;
6692 gfc_symbol *old_sym;
6694 for (ns = siblings; ns; ns = ns->sibling)
6696 st = gfc_find_symtree (ns->sym_root, sym->name);
6698 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
6699 goto fixup_contained;
6701 if ((st->n.sym->attr.flavor == FL_DERIVED
6702 && sym->attr.generic && sym->attr.function)
6703 ||(sym->attr.flavor == FL_DERIVED
6704 && st->n.sym->attr.generic && st->n.sym->attr.function))
6705 goto fixup_contained;
6707 old_sym = st->n.sym;
6708 if (old_sym->ns == ns
6709 && !old_sym->attr.contained
6711 /* By 14.6.1.3, host association should be excluded
6712 for the following. */
6713 && !(old_sym->attr.external
6714 || (old_sym->ts.type != BT_UNKNOWN
6715 && !old_sym->attr.implicit_type)
6716 || old_sym->attr.flavor == FL_PARAMETER
6717 || old_sym->attr.use_assoc
6718 || old_sym->attr.in_common
6719 || old_sym->attr.in_equivalence
6720 || old_sym->attr.data
6721 || old_sym->attr.dummy
6722 || old_sym->attr.result
6723 || old_sym->attr.dimension
6724 || old_sym->attr.allocatable
6725 || old_sym->attr.intrinsic
6726 || old_sym->attr.generic
6727 || old_sym->attr.flavor == FL_NAMELIST
6728 || old_sym->attr.flavor == FL_LABEL
6729 || old_sym->attr.proc == PROC_ST_FUNCTION))
6731 /* Replace it with the symbol from the parent namespace. */
6732 st->n.sym = sym;
6733 sym->refs++;
6735 gfc_release_symbol (old_sym);
6738 fixup_contained:
6739 /* Do the same for any contained procedures. */
6740 gfc_fixup_sibling_symbols (sym, ns->contained);
6744 static void
6745 parse_contained (int module)
6747 gfc_namespace *ns, *parent_ns, *tmp;
6748 gfc_state_data s1, s2;
6749 gfc_statement st;
6750 gfc_symbol *sym;
6751 gfc_entry_list *el;
6752 locus old_loc;
6753 int contains_statements = 0;
6754 int seen_error = 0;
6756 push_state (&s1, COMP_CONTAINS, NULL);
6757 parent_ns = gfc_current_ns;
6761 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
6763 gfc_current_ns->sibling = parent_ns->contained;
6764 parent_ns->contained = gfc_current_ns;
6766 next:
6767 /* Process the next available statement. We come here if we got an error
6768 and rejected the last statement. */
6769 old_loc = gfc_current_locus;
6770 st = next_statement ();
6772 switch (st)
6774 case ST_NONE:
6775 unexpected_eof ();
6777 case ST_FUNCTION:
6778 case ST_SUBROUTINE:
6779 contains_statements = 1;
6780 accept_statement (st);
6782 push_state (&s2,
6783 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
6784 gfc_new_block);
6786 /* For internal procedures, create/update the symbol in the
6787 parent namespace. */
6789 if (!module)
6791 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
6792 gfc_error ("Contained procedure %qs at %C is already "
6793 "ambiguous", gfc_new_block->name);
6794 else
6796 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
6797 sym->name,
6798 &gfc_new_block->declared_at))
6800 if (st == ST_FUNCTION)
6801 gfc_add_function (&sym->attr, sym->name,
6802 &gfc_new_block->declared_at);
6803 else
6804 gfc_add_subroutine (&sym->attr, sym->name,
6805 &gfc_new_block->declared_at);
6809 gfc_commit_symbols ();
6811 else
6812 sym = gfc_new_block;
6814 /* Mark this as a contained function, so it isn't replaced
6815 by other module functions. */
6816 sym->attr.contained = 1;
6818 /* Set implicit_pure so that it can be reset if any of the
6819 tests for purity fail. This is used for some optimisation
6820 during translation. */
6821 if (!sym->attr.pure)
6822 sym->attr.implicit_pure = 1;
6824 parse_progunit (ST_NONE);
6826 /* Fix up any sibling functions that refer to this one. */
6827 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
6828 /* Or refer to any of its alternate entry points. */
6829 for (el = gfc_current_ns->entries; el; el = el->next)
6830 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
6832 gfc_current_ns->code = s2.head;
6833 gfc_current_ns = parent_ns;
6835 pop_state ();
6836 break;
6838 /* These statements are associated with the end of the host unit. */
6839 case ST_END_FUNCTION:
6840 case ST_END_MODULE:
6841 case ST_END_SUBMODULE:
6842 case ST_END_PROGRAM:
6843 case ST_END_SUBROUTINE:
6844 accept_statement (st);
6845 gfc_current_ns->code = s1.head;
6846 break;
6848 default:
6849 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
6850 gfc_ascii_statement (st));
6851 reject_statement ();
6852 seen_error = 1;
6853 goto next;
6854 break;
6857 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
6858 && st != ST_END_MODULE && st != ST_END_SUBMODULE
6859 && st != ST_END_PROGRAM);
6861 /* The first namespace in the list is guaranteed to not have
6862 anything (worthwhile) in it. */
6863 tmp = gfc_current_ns;
6864 gfc_current_ns = parent_ns;
6865 if (seen_error && tmp->refs > 1)
6866 gfc_free_namespace (tmp);
6868 ns = gfc_current_ns->contained;
6869 gfc_current_ns->contained = ns->sibling;
6870 gfc_free_namespace (ns);
6872 pop_state ();
6873 if (!contains_statements)
6874 gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
6875 "FUNCTION or SUBROUTINE statement at %L", &old_loc);
6879 /* The result variable in a MODULE PROCEDURE needs to be created and
6880 its characteristics copied from the interface since it is neither
6881 declared in the procedure declaration nor in the specification
6882 part. */
6884 static void
6885 get_modproc_result (void)
6887 gfc_symbol *proc;
6888 if (gfc_state_stack->previous
6889 && gfc_state_stack->previous->state == COMP_CONTAINS
6890 && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
6892 proc = gfc_current_ns->proc_name ? gfc_current_ns->proc_name : NULL;
6893 if (proc != NULL
6894 && proc->attr.function
6895 && proc->tlink
6896 && proc->tlink->result
6897 && proc->tlink->result != proc->tlink)
6899 gfc_copy_dummy_sym (&proc->result, proc->tlink->result, 1);
6900 gfc_set_sym_referenced (proc->result);
6901 proc->result->attr.if_source = IFSRC_DECL;
6902 gfc_commit_symbol (proc->result);
6908 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
6910 static void
6911 parse_progunit (gfc_statement st)
6913 gfc_state_data *p;
6914 int n;
6916 gfc_adjust_builtins ();
6918 if (gfc_new_block
6919 && gfc_new_block->abr_modproc_decl
6920 && gfc_new_block->attr.function)
6921 get_modproc_result ();
6923 st = parse_spec (st);
6924 switch (st)
6926 case ST_NONE:
6927 unexpected_eof ();
6929 case ST_CONTAINS:
6930 /* This is not allowed within BLOCK! */
6931 if (gfc_current_state () != COMP_BLOCK)
6932 goto contains;
6933 break;
6935 case_end:
6936 accept_statement (st);
6937 goto done;
6939 default:
6940 break;
6943 if (gfc_current_state () == COMP_FUNCTION)
6944 gfc_check_function_type (gfc_current_ns);
6946 loop:
6947 for (;;)
6949 st = parse_executable (st);
6951 switch (st)
6953 case ST_NONE:
6954 unexpected_eof ();
6956 case ST_CONTAINS:
6957 /* This is not allowed within BLOCK! */
6958 if (gfc_current_state () != COMP_BLOCK)
6959 goto contains;
6960 break;
6962 case_end:
6963 accept_statement (st);
6964 goto done;
6966 default:
6967 break;
6970 unexpected_statement (st);
6971 reject_statement ();
6972 st = next_statement ();
6975 contains:
6976 n = 0;
6978 for (p = gfc_state_stack; p; p = p->previous)
6979 if (p->state == COMP_CONTAINS)
6980 n++;
6982 if (gfc_find_state (COMP_MODULE) == true
6983 || gfc_find_state (COMP_SUBMODULE) == true)
6984 n--;
6986 if (n > 0)
6988 gfc_error ("CONTAINS statement at %C is already in a contained "
6989 "program unit");
6990 reject_statement ();
6991 st = next_statement ();
6992 goto loop;
6995 parse_contained (0);
6997 done:
6998 gfc_current_ns->code = gfc_state_stack->head;
7002 /* Come here to complain about a global symbol already in use as
7003 something else. */
7005 void
7006 gfc_global_used (gfc_gsymbol *sym, locus *where)
7008 const char *name;
7010 if (where == NULL)
7011 where = &gfc_current_locus;
7013 switch(sym->type)
7015 case GSYM_PROGRAM:
7016 name = "PROGRAM";
7017 break;
7018 case GSYM_FUNCTION:
7019 name = "FUNCTION";
7020 break;
7021 case GSYM_SUBROUTINE:
7022 name = "SUBROUTINE";
7023 break;
7024 case GSYM_COMMON:
7025 name = "COMMON";
7026 break;
7027 case GSYM_BLOCK_DATA:
7028 name = "BLOCK DATA";
7029 break;
7030 case GSYM_MODULE:
7031 name = "MODULE";
7032 break;
7033 default:
7034 name = NULL;
7037 if (name)
7039 if (sym->binding_label)
7040 gfc_error ("Global binding name %qs at %L is already being used "
7041 "as a %s at %L", sym->binding_label, where, name,
7042 &sym->where);
7043 else
7044 gfc_error ("Global name %qs at %L is already being used as "
7045 "a %s at %L", sym->name, where, name, &sym->where);
7047 else
7049 if (sym->binding_label)
7050 gfc_error ("Global binding name %qs at %L is already being used "
7051 "at %L", sym->binding_label, where, &sym->where);
7052 else
7053 gfc_error ("Global name %qs at %L is already being used at %L",
7054 sym->name, where, &sym->where);
7059 /* Parse a block data program unit. */
7061 static void
7062 parse_block_data (void)
7064 gfc_statement st;
7065 static locus blank_locus;
7066 static int blank_block=0;
7067 gfc_gsymbol *s;
7069 gfc_current_ns->proc_name = gfc_new_block;
7070 gfc_current_ns->is_block_data = 1;
7072 if (gfc_new_block == NULL)
7074 if (blank_block)
7075 gfc_error ("Blank BLOCK DATA at %C conflicts with "
7076 "prior BLOCK DATA at %L", &blank_locus);
7077 else
7079 blank_block = 1;
7080 blank_locus = gfc_current_locus;
7083 else
7085 s = gfc_get_gsymbol (gfc_new_block->name, false);
7086 if (s->defined
7087 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
7088 gfc_global_used (s, &gfc_new_block->declared_at);
7089 else
7091 s->type = GSYM_BLOCK_DATA;
7092 s->where = gfc_new_block->declared_at;
7093 s->defined = 1;
7097 st = parse_spec (ST_NONE);
7099 while (st != ST_END_BLOCK_DATA)
7101 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
7102 gfc_ascii_statement (st));
7103 reject_statement ();
7104 st = next_statement ();
7109 /* Following the association of the ancestor (sub)module symbols, they
7110 must be set host rather than use associated and all must be public.
7111 They are flagged up by 'used_in_submodule' so that they can be set
7112 DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl). Otherwise the
7113 linker chokes on multiple symbol definitions. */
7115 static void
7116 set_syms_host_assoc (gfc_symbol *sym)
7118 gfc_component *c;
7119 const char dot[2] = ".";
7120 /* Symbols take the form module.submodule_ or module.name_. */
7121 char parent1[2 * GFC_MAX_SYMBOL_LEN + 2];
7122 char parent2[2 * GFC_MAX_SYMBOL_LEN + 2];
7124 if (sym == NULL)
7125 return;
7127 if (sym->attr.module_procedure)
7128 sym->attr.external = 0;
7130 sym->attr.use_assoc = 0;
7131 sym->attr.host_assoc = 1;
7132 sym->attr.used_in_submodule =1;
7134 if (sym->attr.flavor == FL_DERIVED)
7136 /* Derived types with PRIVATE components that are declared in
7137 modules other than the parent module must not be changed to be
7138 PUBLIC. The 'use-assoc' attribute must be reset so that the
7139 test in symbol.cc(gfc_find_component) works correctly. This is
7140 not necessary for PRIVATE symbols since they are not read from
7141 the module. */
7142 memset(parent1, '\0', sizeof(parent1));
7143 memset(parent2, '\0', sizeof(parent2));
7144 strcpy (parent1, gfc_new_block->name);
7145 strcpy (parent2, sym->module);
7146 if (strcmp (strtok (parent1, dot), strtok (parent2, dot)) == 0)
7148 for (c = sym->components; c; c = c->next)
7149 c->attr.access = ACCESS_PUBLIC;
7151 else
7153 sym->attr.use_assoc = 1;
7154 sym->attr.host_assoc = 0;
7159 /* Parse a module subprogram. */
7161 static void
7162 parse_module (void)
7164 gfc_statement st;
7165 gfc_gsymbol *s;
7167 s = gfc_get_gsymbol (gfc_new_block->name, false);
7168 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
7169 gfc_global_used (s, &gfc_new_block->declared_at);
7170 else
7172 s->type = GSYM_MODULE;
7173 s->where = gfc_new_block->declared_at;
7174 s->defined = 1;
7177 /* Something is nulling the module_list after this point. This is good
7178 since it allows us to 'USE' the parent modules that the submodule
7179 inherits and to set (most) of the symbols as host associated. */
7180 if (gfc_current_state () == COMP_SUBMODULE)
7182 use_modules ();
7183 gfc_traverse_ns (gfc_current_ns, set_syms_host_assoc);
7186 st = parse_spec (ST_NONE);
7188 loop:
7189 switch (st)
7191 case ST_NONE:
7192 unexpected_eof ();
7194 case ST_CONTAINS:
7195 parse_contained (1);
7196 break;
7198 case ST_END_MODULE:
7199 case ST_END_SUBMODULE:
7200 accept_statement (st);
7201 break;
7203 default:
7204 gfc_error ("Unexpected %s statement in MODULE at %C",
7205 gfc_ascii_statement (st));
7206 reject_statement ();
7207 st = next_statement ();
7208 goto loop;
7210 s->ns = gfc_current_ns;
7214 /* Add a procedure name to the global symbol table. */
7216 static void
7217 add_global_procedure (bool sub)
7219 gfc_gsymbol *s;
7221 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
7222 name is a global identifier. */
7223 if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
7225 s = gfc_get_gsymbol (gfc_new_block->name, false);
7227 if (s->defined
7228 || (s->type != GSYM_UNKNOWN
7229 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
7231 gfc_global_used (s, &gfc_new_block->declared_at);
7232 /* Silence follow-up errors. */
7233 gfc_new_block->binding_label = NULL;
7235 else
7237 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
7238 s->sym_name = gfc_new_block->name;
7239 s->where = gfc_new_block->declared_at;
7240 s->defined = 1;
7241 s->ns = gfc_current_ns;
7245 /* Don't add the symbol multiple times. */
7246 if (gfc_new_block->binding_label
7247 && (!gfc_notification_std (GFC_STD_F2008)
7248 || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
7250 s = gfc_get_gsymbol (gfc_new_block->binding_label, true);
7252 if (s->defined
7253 || (s->type != GSYM_UNKNOWN
7254 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
7256 gfc_global_used (s, &gfc_new_block->declared_at);
7257 /* Silence follow-up errors. */
7258 gfc_new_block->binding_label = NULL;
7260 else
7262 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
7263 s->sym_name = gfc_new_block->name;
7264 s->binding_label = gfc_new_block->binding_label;
7265 s->where = gfc_new_block->declared_at;
7266 s->defined = 1;
7267 s->ns = gfc_current_ns;
7273 /* Add a program to the global symbol table. */
7275 static void
7276 add_global_program (void)
7278 gfc_gsymbol *s;
7280 if (gfc_new_block == NULL)
7281 return;
7282 s = gfc_get_gsymbol (gfc_new_block->name, false);
7284 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
7285 gfc_global_used (s, &gfc_new_block->declared_at);
7286 else
7288 s->type = GSYM_PROGRAM;
7289 s->where = gfc_new_block->declared_at;
7290 s->defined = 1;
7291 s->ns = gfc_current_ns;
7296 /* Resolve all the program units. */
7297 static void
7298 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
7300 gfc_derived_types = NULL;
7301 gfc_current_ns = gfc_global_ns_list;
7302 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
7304 if (gfc_current_ns->proc_name
7305 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
7306 continue; /* Already resolved. */
7308 if (gfc_current_ns->proc_name)
7309 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
7310 gfc_resolve (gfc_current_ns);
7311 gfc_current_ns->derived_types = gfc_derived_types;
7312 gfc_derived_types = NULL;
7317 static void
7318 clean_up_modules (gfc_gsymbol *&gsym)
7320 if (gsym == NULL)
7321 return;
7323 clean_up_modules (gsym->left);
7324 clean_up_modules (gsym->right);
7326 if (gsym->type != GSYM_MODULE)
7327 return;
7329 if (gsym->ns)
7331 gfc_current_ns = gsym->ns;
7332 gfc_derived_types = gfc_current_ns->derived_types;
7333 gfc_done_2 ();
7334 gsym->ns = NULL;
7336 free (gsym);
7337 gsym = NULL;
7341 /* Translate all the program units. This could be in a different order
7342 to resolution if there are forward references in the file. */
7343 static void
7344 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
7346 int errors;
7348 gfc_current_ns = gfc_global_ns_list;
7349 gfc_get_errors (NULL, &errors);
7351 /* We first translate all modules to make sure that later parts
7352 of the program can use the decl. Then we translate the nonmodules. */
7354 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
7356 if (!gfc_current_ns->proc_name
7357 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
7358 continue;
7360 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
7361 gfc_derived_types = gfc_current_ns->derived_types;
7362 gfc_generate_module_code (gfc_current_ns);
7363 gfc_current_ns->translated = 1;
7366 gfc_current_ns = gfc_global_ns_list;
7367 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
7369 if (gfc_current_ns->proc_name
7370 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
7371 continue;
7373 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
7374 gfc_derived_types = gfc_current_ns->derived_types;
7375 gfc_generate_code (gfc_current_ns);
7376 gfc_current_ns->translated = 1;
7379 /* Clean up all the namespaces after translation. */
7380 gfc_current_ns = gfc_global_ns_list;
7381 for (;gfc_current_ns;)
7383 gfc_namespace *ns;
7385 if (gfc_current_ns->proc_name
7386 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
7388 gfc_current_ns = gfc_current_ns->sibling;
7389 continue;
7392 ns = gfc_current_ns->sibling;
7393 gfc_derived_types = gfc_current_ns->derived_types;
7394 gfc_done_2 ();
7395 gfc_current_ns = ns;
7398 clean_up_modules (gfc_gsym_root);
7402 /* Top level parser. */
7404 bool
7405 gfc_parse_file (void)
7407 int seen_program, errors_before, errors;
7408 gfc_state_data top, s;
7409 gfc_statement st;
7410 locus prog_locus;
7411 gfc_namespace *next;
7413 gfc_start_source_files ();
7415 top.state = COMP_NONE;
7416 top.sym = NULL;
7417 top.previous = NULL;
7418 top.head = top.tail = NULL;
7419 top.do_variable = NULL;
7421 gfc_state_stack = &top;
7423 gfc_clear_new_st ();
7425 gfc_statement_label = NULL;
7427 gfc_omp_metadirective_region_count = 0;
7428 gfc_in_omp_metadirective_body = false;
7429 gfc_matching_omp_context_selector = false;
7431 if (setjmp (eof_buf))
7432 return false; /* Come here on unexpected EOF */
7434 /* Prepare the global namespace that will contain the
7435 program units. */
7436 gfc_global_ns_list = next = NULL;
7438 seen_program = 0;
7439 errors_before = 0;
7441 /* Exit early for empty files. */
7442 if (gfc_at_eof ())
7443 goto done;
7445 in_specification_block = true;
7446 loop:
7447 gfc_init_2 ();
7448 st = next_statement ();
7449 switch (st)
7451 case ST_NONE:
7452 gfc_done_2 ();
7453 goto done;
7455 case ST_PROGRAM:
7456 if (seen_program)
7457 goto duplicate_main;
7458 seen_program = 1;
7459 prog_locus = gfc_current_locus;
7461 push_state (&s, COMP_PROGRAM, gfc_new_block);
7462 main_program_symbol (gfc_current_ns, gfc_new_block->name);
7463 accept_statement (st);
7464 add_global_program ();
7465 parse_progunit (ST_NONE);
7466 goto prog_units;
7468 case ST_SUBROUTINE:
7469 add_global_procedure (true);
7470 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
7471 accept_statement (st);
7472 parse_progunit (ST_NONE);
7473 goto prog_units;
7475 case ST_FUNCTION:
7476 add_global_procedure (false);
7477 push_state (&s, COMP_FUNCTION, gfc_new_block);
7478 accept_statement (st);
7479 parse_progunit (ST_NONE);
7480 goto prog_units;
7482 case ST_BLOCK_DATA:
7483 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
7484 accept_statement (st);
7485 parse_block_data ();
7486 break;
7488 case ST_MODULE:
7489 push_state (&s, COMP_MODULE, gfc_new_block);
7490 accept_statement (st);
7492 gfc_get_errors (NULL, &errors_before);
7493 parse_module ();
7494 break;
7496 case ST_SUBMODULE:
7497 push_state (&s, COMP_SUBMODULE, gfc_new_block);
7498 accept_statement (st);
7500 gfc_get_errors (NULL, &errors_before);
7501 parse_module ();
7502 break;
7504 /* Anything else starts a nameless main program block. */
7505 default:
7506 if (seen_program)
7507 goto duplicate_main;
7508 seen_program = 1;
7509 prog_locus = gfc_current_locus;
7511 push_state (&s, COMP_PROGRAM, gfc_new_block);
7512 main_program_symbol (gfc_current_ns, "MAIN__");
7513 parse_progunit (st);
7514 goto prog_units;
7517 /* Handle the non-program units. */
7518 gfc_current_ns->code = s.head;
7520 gfc_resolve (gfc_current_ns);
7522 /* Fix the implicit_pure attribute for those procedures who should
7523 not have it. */
7524 while (gfc_fix_implicit_pure (gfc_current_ns))
7527 /* Dump the parse tree if requested. */
7528 if (flag_dump_fortran_original)
7529 gfc_dump_parse_tree (gfc_current_ns, stdout);
7531 gfc_get_errors (NULL, &errors);
7532 if (s.state == COMP_MODULE || s.state == COMP_SUBMODULE)
7534 gfc_dump_module (s.sym->name, errors_before == errors);
7535 gfc_current_ns->derived_types = gfc_derived_types;
7536 gfc_derived_types = NULL;
7537 goto prog_units;
7539 else
7541 if (errors == 0)
7542 gfc_generate_code (gfc_current_ns);
7543 pop_state ();
7544 gfc_done_2 ();
7547 goto loop;
7549 prog_units:
7550 /* The main program and non-contained procedures are put
7551 in the global namespace list, so that they can be processed
7552 later and all their interfaces resolved. */
7553 gfc_current_ns->code = s.head;
7554 if (next)
7556 for (; next->sibling; next = next->sibling)
7558 next->sibling = gfc_current_ns;
7560 else
7561 gfc_global_ns_list = gfc_current_ns;
7563 next = gfc_current_ns;
7565 pop_state ();
7566 goto loop;
7568 done:
7569 /* Do the resolution. */
7570 resolve_all_program_units (gfc_global_ns_list);
7572 /* Go through all top-level namespaces and unset the implicit_pure
7573 attribute for any procedures that call something not pure or
7574 implicit_pure. Because the a procedure marked as not implicit_pure
7575 in one sweep may be called by another routine, we repeat this
7576 process until there are no more changes. */
7577 bool changed;
7580 changed = false;
7581 for (gfc_current_ns = gfc_global_ns_list; gfc_current_ns;
7582 gfc_current_ns = gfc_current_ns->sibling)
7584 if (gfc_fix_implicit_pure (gfc_current_ns))
7585 changed = true;
7588 while (changed);
7590 /* Fixup for external procedures and resolve 'omp requires'. */
7591 int omp_requires;
7592 bool omp_target_seen;
7593 omp_requires = 0;
7594 omp_target_seen = false;
7595 for (gfc_current_ns = gfc_global_ns_list; gfc_current_ns;
7596 gfc_current_ns = gfc_current_ns->sibling)
7598 omp_requires |= gfc_current_ns->omp_requires;
7599 omp_target_seen |= gfc_current_ns->omp_target_seen;
7600 gfc_check_externals (gfc_current_ns);
7602 for (gfc_current_ns = gfc_global_ns_list; gfc_current_ns;
7603 gfc_current_ns = gfc_current_ns->sibling)
7604 gfc_check_omp_requires (gfc_current_ns, omp_requires);
7606 /* Populate omp_requires_mask (needed for resolving OpenMP
7607 metadirectives and declare variant). */
7608 switch (omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
7610 case OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST:
7611 omp_requires_mask
7612 = (enum omp_requires) (omp_requires_mask | OMP_MEMORY_ORDER_SEQ_CST);
7613 break;
7614 case OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL:
7615 omp_requires_mask
7616 = (enum omp_requires) (omp_requires_mask | OMP_MEMORY_ORDER_ACQ_REL);
7617 break;
7618 case OMP_REQ_ATOMIC_MEM_ORDER_ACQUIRE:
7619 omp_requires_mask
7620 = (enum omp_requires) (omp_requires_mask | OMP_MEMORY_ORDER_ACQUIRE);
7621 break;
7622 case OMP_REQ_ATOMIC_MEM_ORDER_RELAXED:
7623 omp_requires_mask
7624 = (enum omp_requires) (omp_requires_mask | OMP_MEMORY_ORDER_RELAXED);
7625 break;
7626 case OMP_REQ_ATOMIC_MEM_ORDER_RELEASE:
7627 omp_requires_mask
7628 = (enum omp_requires) (omp_requires_mask | OMP_MEMORY_ORDER_RELEASE);
7629 break;
7632 if (omp_target_seen)
7633 omp_requires_mask = (enum omp_requires) (omp_requires_mask
7634 | OMP_REQUIRES_TARGET_USED);
7635 if (omp_requires & OMP_REQ_REVERSE_OFFLOAD)
7636 omp_requires_mask = (enum omp_requires) (omp_requires_mask
7637 | OMP_REQUIRES_REVERSE_OFFLOAD);
7638 if (omp_requires & OMP_REQ_UNIFIED_ADDRESS)
7639 omp_requires_mask = (enum omp_requires) (omp_requires_mask
7640 | OMP_REQUIRES_UNIFIED_ADDRESS);
7641 if (omp_requires & OMP_REQ_UNIFIED_SHARED_MEMORY)
7642 omp_requires_mask
7643 = (enum omp_requires) (omp_requires_mask
7644 | OMP_REQUIRES_UNIFIED_SHARED_MEMORY);
7645 if (omp_requires & OMP_REQ_SELF_MAPS)
7646 omp_requires_mask
7647 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_SELF_MAPS);
7648 if (omp_requires & OMP_REQ_DYNAMIC_ALLOCATORS)
7649 omp_requires_mask = (enum omp_requires) (omp_requires_mask
7650 | OMP_REQUIRES_DYNAMIC_ALLOCATORS);
7651 /* Do the parse tree dump. */
7652 gfc_current_ns = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
7654 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
7655 if (!gfc_current_ns->proc_name
7656 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
7658 gfc_dump_parse_tree (gfc_current_ns, stdout);
7659 fputs ("------------------------------------------\n\n", stdout);
7662 /* Dump C prototypes. */
7663 if (flag_c_prototypes || flag_c_prototypes_external)
7665 fprintf (stdout,
7666 "#include <stddef.h>\n"
7667 "#ifdef __cplusplus\n"
7668 "#include <complex>\n"
7669 "#define __GFORTRAN_FLOAT_COMPLEX std::complex<float>\n"
7670 "#define __GFORTRAN_DOUBLE_COMPLEX std::complex<double>\n"
7671 "#define __GFORTRAN_LONG_DOUBLE_COMPLEX std::complex<long double>\n"
7672 "extern \"C\" {\n"
7673 "#else\n"
7674 "#define __GFORTRAN_FLOAT_COMPLEX float _Complex\n"
7675 "#define __GFORTRAN_DOUBLE_COMPLEX double _Complex\n"
7676 "#define __GFORTRAN_LONG_DOUBLE_COMPLEX long double _Complex\n"
7677 "#endif\n\n");
7680 /* First dump BIND(C) prototypes. */
7681 if (flag_c_prototypes)
7682 gfc_dump_c_prototypes (stdout);
7684 /* Dump external prototypes. */
7685 if (flag_c_prototypes_external)
7686 gfc_dump_external_c_prototypes (stdout);
7688 if (flag_c_prototypes || flag_c_prototypes_external)
7689 fprintf (stdout, "\n#ifdef __cplusplus\n}\n#endif\n");
7691 /* Do the translation. */
7692 translate_all_program_units (gfc_global_ns_list);
7694 /* Dump the global symbol ist. We only do this here because part
7695 of it is generated after mangling the identifiers in
7696 trans-decl.cc. */
7698 if (flag_dump_fortran_global)
7699 gfc_dump_global_symbols (stdout);
7701 gfc_end_source_files ();
7702 return true;
7704 duplicate_main:
7705 /* If we see a duplicate main program, shut down. If the second
7706 instance is an implied main program, i.e. data decls or executable
7707 statements, we're in for lots of errors. */
7708 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
7709 reject_statement ();
7710 gfc_done_2 ();
7711 return true;
7714 /* Return true if this state data represents an OpenACC region. */
7715 bool
7716 is_oacc (gfc_state_data *sd)
7718 switch (sd->construct->op)
7720 case EXEC_OACC_PARALLEL_LOOP:
7721 case EXEC_OACC_PARALLEL:
7722 case EXEC_OACC_KERNELS_LOOP:
7723 case EXEC_OACC_KERNELS:
7724 case EXEC_OACC_SERIAL_LOOP:
7725 case EXEC_OACC_SERIAL:
7726 case EXEC_OACC_DATA:
7727 case EXEC_OACC_HOST_DATA:
7728 case EXEC_OACC_LOOP:
7729 case EXEC_OACC_UPDATE:
7730 case EXEC_OACC_WAIT:
7731 case EXEC_OACC_CACHE:
7732 case EXEC_OACC_ENTER_DATA:
7733 case EXEC_OACC_EXIT_DATA:
7734 case EXEC_OACC_ATOMIC:
7735 case EXEC_OACC_ROUTINE:
7736 return true;
7738 default:
7739 return false;
7743 /* Return true if ST is a declarative OpenMP statement. */
7744 bool
7745 is_omp_declarative_stmt (gfc_statement st)
7747 switch (st)
7749 case_omp_decl:
7750 return true;
7751 default:
7752 return false;