2016-07-28 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / parse.c
blobbd7b1384a0c81d839f9f08fa9a143cbd9dc6151f
1 /* Main parser.
2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "gfortran.h"
26 #include <setjmp.h>
27 #include "match.h"
28 #include "parse.h"
30 /* Current statement label. Zero means no statement label. Because new_st
31 can get wiped during statement matching, we have to keep it separate. */
33 gfc_st_label *gfc_statement_label;
35 static locus label_locus;
36 static jmp_buf eof_buf;
38 gfc_state_data *gfc_state_stack;
39 static bool last_was_use_stmt = false;
41 /* TODO: Re-order functions to kill these forward decls. */
42 static void check_statement_label (gfc_statement);
43 static void undo_new_statement (void);
44 static void reject_statement (void);
47 /* A sort of half-matching function. We try to match the word on the
48 input with the passed string. If this succeeds, we call the
49 keyword-dependent matching function that will match the rest of the
50 statement. For single keywords, the matching subroutine is
51 gfc_match_eos(). */
53 static match
54 match_word (const char *str, match (*subr) (void), locus *old_locus)
56 match m;
58 if (str != NULL)
60 m = gfc_match (str);
61 if (m != MATCH_YES)
62 return m;
65 m = (*subr) ();
67 if (m != MATCH_YES)
69 gfc_current_locus = *old_locus;
70 reject_statement ();
73 return m;
77 /* Like match_word, but if str is matched, set a flag that it
78 was matched. */
79 static match
80 match_word_omp_simd (const char *str, match (*subr) (void), locus *old_locus,
81 bool *simd_matched)
83 match m;
85 if (str != NULL)
87 m = gfc_match (str);
88 if (m != MATCH_YES)
89 return m;
90 *simd_matched = true;
93 m = (*subr) ();
95 if (m != MATCH_YES)
97 gfc_current_locus = *old_locus;
98 reject_statement ();
101 return m;
105 /* Load symbols from all USE statements encountered in this scoping unit. */
107 static void
108 use_modules (void)
110 gfc_error_buffer old_error;
112 gfc_push_error (&old_error);
113 gfc_buffer_error (false);
114 gfc_use_modules ();
115 gfc_buffer_error (true);
116 gfc_pop_error (&old_error);
117 gfc_commit_symbols ();
118 gfc_warning_check ();
119 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
120 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
121 gfc_current_ns->old_data = gfc_current_ns->data;
122 last_was_use_stmt = false;
126 /* Figure out what the next statement is, (mostly) regardless of
127 proper ordering. The do...while(0) is there to prevent if/else
128 ambiguity. */
130 #define match(keyword, subr, st) \
131 do { \
132 if (match_word (keyword, subr, &old_locus) == MATCH_YES) \
133 return st; \
134 else \
135 undo_new_statement (); \
136 } while (0);
139 /* This is a specialist version of decode_statement that is used
140 for the specification statements in a function, whose
141 characteristics are deferred into the specification statements.
142 eg.: INTEGER (king = mykind) foo ()
143 USE mymodule, ONLY mykind.....
144 The KIND parameter needs a return after USE or IMPORT, whereas
145 derived type declarations can occur anywhere, up the executable
146 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
147 out of the correct kind of specification statements. */
148 static gfc_statement
149 decode_specification_statement (void)
151 gfc_statement st;
152 locus old_locus;
153 char c;
155 if (gfc_match_eos () == MATCH_YES)
156 return ST_NONE;
158 old_locus = gfc_current_locus;
160 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
162 last_was_use_stmt = true;
163 return ST_USE;
165 else
167 undo_new_statement ();
168 if (last_was_use_stmt)
169 use_modules ();
172 match ("import", gfc_match_import, ST_IMPORT);
174 if (gfc_current_block ()->result->ts.type != BT_DERIVED)
175 goto end_of_block;
177 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
178 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
179 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
181 /* General statement matching: Instead of testing every possible
182 statement, we eliminate most possibilities by peeking at the
183 first character. */
185 c = gfc_peek_ascii_char ();
187 switch (c)
189 case 'a':
190 match ("abstract% interface", gfc_match_abstract_interface,
191 ST_INTERFACE);
192 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
193 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
194 break;
196 case 'b':
197 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
198 break;
200 case 'c':
201 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
202 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
203 break;
205 case 'd':
206 match ("data", gfc_match_data, ST_DATA);
207 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
208 break;
210 case 'e':
211 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
212 match ("entry% ", gfc_match_entry, ST_ENTRY);
213 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
214 match ("external", gfc_match_external, ST_ATTR_DECL);
215 break;
217 case 'f':
218 match ("format", gfc_match_format, ST_FORMAT);
219 break;
221 case 'g':
222 break;
224 case 'i':
225 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
226 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
227 match ("interface", gfc_match_interface, ST_INTERFACE);
228 match ("intent", gfc_match_intent, ST_ATTR_DECL);
229 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
230 break;
232 case 'm':
233 break;
235 case 'n':
236 match ("namelist", gfc_match_namelist, ST_NAMELIST);
237 break;
239 case 'o':
240 match ("optional", gfc_match_optional, ST_ATTR_DECL);
241 break;
243 case 'p':
244 match ("parameter", gfc_match_parameter, ST_PARAMETER);
245 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
246 if (gfc_match_private (&st) == MATCH_YES)
247 return st;
248 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
249 if (gfc_match_public (&st) == MATCH_YES)
250 return st;
251 match ("protected", gfc_match_protected, ST_ATTR_DECL);
252 break;
254 case 'r':
255 break;
257 case 's':
258 match ("save", gfc_match_save, ST_ATTR_DECL);
259 match ("structure", gfc_match_structure_decl, ST_STRUCTURE_DECL);
260 break;
262 case 't':
263 match ("target", gfc_match_target, ST_ATTR_DECL);
264 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
265 break;
267 case 'u':
268 break;
270 case 'v':
271 match ("value", gfc_match_value, ST_ATTR_DECL);
272 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
273 break;
275 case 'w':
276 break;
279 /* This is not a specification statement. See if any of the matchers
280 has stored an error message of some sort. */
282 end_of_block:
283 gfc_clear_error ();
284 gfc_buffer_error (false);
285 gfc_current_locus = old_locus;
287 return ST_GET_FCN_CHARACTERISTICS;
290 static bool in_specification_block;
292 /* This is the primary 'decode_statement'. */
293 static gfc_statement
294 decode_statement (void)
296 gfc_namespace *ns;
297 gfc_statement st;
298 locus old_locus;
299 match m = MATCH_NO;
300 char c;
302 gfc_enforce_clean_symbol_state ();
304 gfc_clear_error (); /* Clear any pending errors. */
305 gfc_clear_warning (); /* Clear any pending warnings. */
307 gfc_matching_function = false;
309 if (gfc_match_eos () == MATCH_YES)
310 return ST_NONE;
312 if (gfc_current_state () == COMP_FUNCTION
313 && gfc_current_block ()->result->ts.kind == -1)
314 return decode_specification_statement ();
316 old_locus = gfc_current_locus;
318 c = gfc_peek_ascii_char ();
320 if (c == 'u')
322 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
324 last_was_use_stmt = true;
325 return ST_USE;
327 else
328 undo_new_statement ();
331 if (last_was_use_stmt)
332 use_modules ();
334 /* Try matching a data declaration or function declaration. The
335 input "REALFUNCTIONA(N)" can mean several things in different
336 contexts, so it (and its relatives) get special treatment. */
338 if (gfc_current_state () == COMP_NONE
339 || gfc_current_state () == COMP_INTERFACE
340 || gfc_current_state () == COMP_CONTAINS)
342 gfc_matching_function = true;
343 m = gfc_match_function_decl ();
344 if (m == MATCH_YES)
345 return ST_FUNCTION;
346 else if (m == MATCH_ERROR)
347 reject_statement ();
348 else
349 gfc_undo_symbols ();
350 gfc_current_locus = old_locus;
352 gfc_matching_function = false;
355 /* Match statements whose error messages are meant to be overwritten
356 by something better. */
358 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
359 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
361 if (in_specification_block)
363 m = match_word (NULL, gfc_match_st_function, &old_locus);
364 if (m == MATCH_YES)
365 return ST_STATEMENT_FUNCTION;
368 if (!(in_specification_block && m == MATCH_ERROR))
370 match (NULL, gfc_match_ptr_fcn_assign, ST_ASSIGNMENT);
373 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
374 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
376 /* Try to match a subroutine statement, which has the same optional
377 prefixes that functions can have. */
379 if (gfc_match_subroutine () == MATCH_YES)
380 return ST_SUBROUTINE;
381 gfc_undo_symbols ();
382 gfc_current_locus = old_locus;
384 if (gfc_match_submod_proc () == MATCH_YES)
386 if (gfc_new_block->attr.subroutine)
387 return ST_SUBROUTINE;
388 else if (gfc_new_block->attr.function)
389 return ST_FUNCTION;
391 gfc_undo_symbols ();
392 gfc_current_locus = old_locus;
394 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
395 statements, which might begin with a block label. The match functions for
396 these statements are unusual in that their keyword is not seen before
397 the matcher is called. */
399 if (gfc_match_if (&st) == MATCH_YES)
400 return st;
401 gfc_undo_symbols ();
402 gfc_current_locus = old_locus;
404 if (gfc_match_where (&st) == MATCH_YES)
405 return st;
406 gfc_undo_symbols ();
407 gfc_current_locus = old_locus;
409 if (gfc_match_forall (&st) == MATCH_YES)
410 return st;
411 gfc_undo_symbols ();
412 gfc_current_locus = old_locus;
414 match (NULL, gfc_match_do, ST_DO);
415 match (NULL, gfc_match_block, ST_BLOCK);
416 match (NULL, gfc_match_associate, ST_ASSOCIATE);
417 match (NULL, gfc_match_critical, ST_CRITICAL);
418 match (NULL, gfc_match_select, ST_SELECT_CASE);
420 gfc_current_ns = gfc_build_block_ns (gfc_current_ns);
421 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
422 ns = gfc_current_ns;
423 gfc_current_ns = gfc_current_ns->parent;
424 gfc_free_namespace (ns);
426 /* General statement matching: Instead of testing every possible
427 statement, we eliminate most possibilities by peeking at the
428 first character. */
430 switch (c)
432 case 'a':
433 match ("abstract% interface", gfc_match_abstract_interface,
434 ST_INTERFACE);
435 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
436 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
437 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
438 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
439 break;
441 case 'b':
442 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
443 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
444 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
445 break;
447 case 'c':
448 match ("call", gfc_match_call, ST_CALL);
449 match ("close", gfc_match_close, ST_CLOSE);
450 match ("continue", gfc_match_continue, ST_CONTINUE);
451 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
452 match ("cycle", gfc_match_cycle, ST_CYCLE);
453 match ("case", gfc_match_case, ST_CASE);
454 match ("common", gfc_match_common, ST_COMMON);
455 match ("contains", gfc_match_eos, ST_CONTAINS);
456 match ("class", gfc_match_class_is, ST_CLASS_IS);
457 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
458 break;
460 case 'd':
461 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
462 match ("data", gfc_match_data, ST_DATA);
463 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
464 break;
466 case 'e':
467 match ("end file", gfc_match_endfile, ST_END_FILE);
468 match ("exit", gfc_match_exit, ST_EXIT);
469 match ("else", gfc_match_else, ST_ELSE);
470 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
471 match ("else if", gfc_match_elseif, ST_ELSEIF);
472 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
473 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
475 if (gfc_match_end (&st) == MATCH_YES)
476 return st;
478 match ("entry% ", gfc_match_entry, ST_ENTRY);
479 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
480 match ("external", gfc_match_external, ST_ATTR_DECL);
481 match ("event post", gfc_match_event_post, ST_EVENT_POST);
482 match ("event wait", gfc_match_event_wait, ST_EVENT_WAIT);
483 break;
485 case 'f':
486 match ("final", gfc_match_final_decl, ST_FINAL);
487 match ("flush", gfc_match_flush, ST_FLUSH);
488 match ("format", gfc_match_format, ST_FORMAT);
489 break;
491 case 'g':
492 match ("generic", gfc_match_generic, ST_GENERIC);
493 match ("go to", gfc_match_goto, ST_GOTO);
494 break;
496 case 'i':
497 match ("inquire", gfc_match_inquire, ST_INQUIRE);
498 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
499 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
500 match ("import", gfc_match_import, ST_IMPORT);
501 match ("interface", gfc_match_interface, ST_INTERFACE);
502 match ("intent", gfc_match_intent, ST_ATTR_DECL);
503 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
504 break;
506 case 'l':
507 match ("lock", gfc_match_lock, ST_LOCK);
508 break;
510 case 'm':
511 match ("map", gfc_match_map, ST_MAP);
512 match ("module% procedure", gfc_match_modproc, ST_MODULE_PROC);
513 match ("module", gfc_match_module, ST_MODULE);
514 break;
516 case 'n':
517 match ("nullify", gfc_match_nullify, ST_NULLIFY);
518 match ("namelist", gfc_match_namelist, ST_NAMELIST);
519 break;
521 case 'o':
522 match ("open", gfc_match_open, ST_OPEN);
523 match ("optional", gfc_match_optional, ST_ATTR_DECL);
524 break;
526 case 'p':
527 match ("print", gfc_match_print, ST_WRITE);
528 match ("parameter", gfc_match_parameter, ST_PARAMETER);
529 match ("pause", gfc_match_pause, ST_PAUSE);
530 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
531 if (gfc_match_private (&st) == MATCH_YES)
532 return st;
533 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
534 match ("program", gfc_match_program, ST_PROGRAM);
535 if (gfc_match_public (&st) == MATCH_YES)
536 return st;
537 match ("protected", gfc_match_protected, ST_ATTR_DECL);
538 break;
540 case 'r':
541 match ("read", gfc_match_read, ST_READ);
542 match ("return", gfc_match_return, ST_RETURN);
543 match ("rewind", gfc_match_rewind, ST_REWIND);
544 break;
546 case 's':
547 match ("structure", gfc_match_structure_decl, ST_STRUCTURE_DECL);
548 match ("sequence", gfc_match_eos, ST_SEQUENCE);
549 match ("stop", gfc_match_stop, ST_STOP);
550 match ("save", gfc_match_save, ST_ATTR_DECL);
551 match ("submodule", gfc_match_submodule, ST_SUBMODULE);
552 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
553 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
554 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
555 break;
557 case 't':
558 match ("target", gfc_match_target, ST_ATTR_DECL);
559 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
560 match ("type is", gfc_match_type_is, ST_TYPE_IS);
561 break;
563 case 'u':
564 match ("union", gfc_match_union, ST_UNION);
565 match ("unlock", gfc_match_unlock, ST_UNLOCK);
566 break;
568 case 'v':
569 match ("value", gfc_match_value, ST_ATTR_DECL);
570 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
571 break;
573 case 'w':
574 match ("wait", gfc_match_wait, ST_WAIT);
575 match ("write", gfc_match_write, ST_WRITE);
576 break;
579 /* All else has failed, so give up. See if any of the matchers has
580 stored an error message of some sort. */
582 if (!gfc_error_check ())
583 gfc_error_now ("Unclassifiable statement at %C");
585 reject_statement ();
587 gfc_error_recovery ();
589 return ST_NONE;
592 /* Like match and if spec_only, goto do_spec_only without actually
593 matching. */
594 #define matcha(keyword, subr, st) \
595 do { \
596 if (spec_only && gfc_match (keyword) == MATCH_YES) \
597 goto do_spec_only; \
598 else if (match_word (keyword, subr, &old_locus) \
599 == MATCH_YES) \
600 return st; \
601 else \
602 undo_new_statement (); \
603 } while (0);
605 static gfc_statement
606 decode_oacc_directive (void)
608 locus old_locus;
609 char c;
610 bool spec_only = false;
612 gfc_enforce_clean_symbol_state ();
614 gfc_clear_error (); /* Clear any pending errors. */
615 gfc_clear_warning (); /* Clear any pending warnings. */
617 if (gfc_pure (NULL))
619 gfc_error_now ("OpenACC directives at %C may not appear in PURE "
620 "procedures");
621 gfc_error_recovery ();
622 return ST_NONE;
625 if (gfc_current_state () == COMP_FUNCTION
626 && gfc_current_block ()->result->ts.kind == -1)
627 spec_only = true;
629 gfc_unset_implicit_pure (NULL);
631 old_locus = gfc_current_locus;
633 /* General OpenACC directive matching: Instead of testing every possible
634 statement, we eliminate most possibilities by peeking at the
635 first character. */
637 c = gfc_peek_ascii_char ();
639 switch (c)
641 case 'a':
642 matcha ("atomic", gfc_match_oacc_atomic, ST_OACC_ATOMIC);
643 break;
644 case 'c':
645 matcha ("cache", gfc_match_oacc_cache, ST_OACC_CACHE);
646 break;
647 case 'd':
648 matcha ("data", gfc_match_oacc_data, ST_OACC_DATA);
649 match ("declare", gfc_match_oacc_declare, ST_OACC_DECLARE);
650 break;
651 case 'e':
652 matcha ("end atomic", gfc_match_omp_eos, ST_OACC_END_ATOMIC);
653 matcha ("end data", gfc_match_omp_eos, ST_OACC_END_DATA);
654 matcha ("end host_data", gfc_match_omp_eos, ST_OACC_END_HOST_DATA);
655 matcha ("end kernels loop", gfc_match_omp_eos, ST_OACC_END_KERNELS_LOOP);
656 matcha ("end kernels", gfc_match_omp_eos, ST_OACC_END_KERNELS);
657 matcha ("end loop", gfc_match_omp_eos, ST_OACC_END_LOOP);
658 matcha ("end parallel loop", gfc_match_omp_eos,
659 ST_OACC_END_PARALLEL_LOOP);
660 matcha ("end parallel", gfc_match_omp_eos, ST_OACC_END_PARALLEL);
661 matcha ("enter data", gfc_match_oacc_enter_data, ST_OACC_ENTER_DATA);
662 matcha ("exit data", gfc_match_oacc_exit_data, ST_OACC_EXIT_DATA);
663 break;
664 case 'h':
665 matcha ("host_data", gfc_match_oacc_host_data, ST_OACC_HOST_DATA);
666 break;
667 case 'p':
668 matcha ("parallel loop", gfc_match_oacc_parallel_loop,
669 ST_OACC_PARALLEL_LOOP);
670 matcha ("parallel", gfc_match_oacc_parallel, ST_OACC_PARALLEL);
671 break;
672 case 'k':
673 matcha ("kernels loop", gfc_match_oacc_kernels_loop,
674 ST_OACC_KERNELS_LOOP);
675 matcha ("kernels", gfc_match_oacc_kernels, ST_OACC_KERNELS);
676 break;
677 case 'l':
678 matcha ("loop", gfc_match_oacc_loop, ST_OACC_LOOP);
679 break;
680 case 'r':
681 match ("routine", gfc_match_oacc_routine, ST_OACC_ROUTINE);
682 break;
683 case 'u':
684 matcha ("update", gfc_match_oacc_update, ST_OACC_UPDATE);
685 break;
686 case 'w':
687 matcha ("wait", gfc_match_oacc_wait, ST_OACC_WAIT);
688 break;
691 /* Directive not found or stored an error message.
692 Check and give up. */
694 if (gfc_error_check () == 0)
695 gfc_error_now ("Unclassifiable OpenACC directive at %C");
697 reject_statement ();
699 gfc_error_recovery ();
701 return ST_NONE;
703 do_spec_only:
704 reject_statement ();
705 gfc_clear_error ();
706 gfc_buffer_error (false);
707 gfc_current_locus = old_locus;
708 return ST_GET_FCN_CHARACTERISTICS;
711 /* Like match, but set a flag simd_matched if keyword matched
712 and if spec_only, goto do_spec_only without actually matching. */
713 #define matchs(keyword, subr, st) \
714 do { \
715 if (spec_only && gfc_match (keyword) == MATCH_YES) \
716 goto do_spec_only; \
717 if (match_word_omp_simd (keyword, subr, &old_locus, \
718 &simd_matched) == MATCH_YES) \
719 return st; \
720 else \
721 undo_new_statement (); \
722 } while (0);
724 /* Like match, but don't match anything if not -fopenmp
725 and if spec_only, goto do_spec_only without actually matching. */
726 #define matcho(keyword, subr, st) \
727 do { \
728 if (!flag_openmp) \
730 else if (spec_only && gfc_match (keyword) == MATCH_YES) \
731 goto do_spec_only; \
732 else if (match_word (keyword, subr, &old_locus) \
733 == MATCH_YES) \
734 return st; \
735 else \
736 undo_new_statement (); \
737 } while (0);
739 /* Like match, but set a flag simd_matched if keyword matched. */
740 #define matchds(keyword, subr, st) \
741 do { \
742 if (match_word_omp_simd (keyword, subr, &old_locus, \
743 &simd_matched) == MATCH_YES) \
744 return st; \
745 else \
746 undo_new_statement (); \
747 } while (0);
749 /* Like match, but don't match anything if not -fopenmp. */
750 #define matchdo(keyword, subr, st) \
751 do { \
752 if (!flag_openmp) \
754 else if (match_word (keyword, subr, &old_locus) \
755 == MATCH_YES) \
756 return st; \
757 else \
758 undo_new_statement (); \
759 } while (0);
761 static gfc_statement
762 decode_omp_directive (void)
764 locus old_locus;
765 char c;
766 bool simd_matched = false;
767 bool spec_only = false;
769 gfc_enforce_clean_symbol_state ();
771 gfc_clear_error (); /* Clear any pending errors. */
772 gfc_clear_warning (); /* Clear any pending warnings. */
774 if (gfc_pure (NULL))
776 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
777 "or ELEMENTAL procedures");
778 gfc_error_recovery ();
779 return ST_NONE;
782 if (gfc_current_state () == COMP_FUNCTION
783 && gfc_current_block ()->result->ts.kind == -1)
784 spec_only = true;
786 gfc_unset_implicit_pure (NULL);
788 old_locus = gfc_current_locus;
790 /* General OpenMP directive matching: Instead of testing every possible
791 statement, we eliminate most possibilities by peeking at the
792 first character. */
794 c = gfc_peek_ascii_char ();
796 /* match is for directives that should be recognized only if
797 -fopenmp, matchs for directives that should be recognized
798 if either -fopenmp or -fopenmp-simd. */
799 switch (c)
801 case 'a':
802 matcho ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
803 break;
804 case 'b':
805 matcho ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
806 break;
807 case 'c':
808 matcho ("cancellation% point", gfc_match_omp_cancellation_point,
809 ST_OMP_CANCELLATION_POINT);
810 matcho ("cancel", gfc_match_omp_cancel, ST_OMP_CANCEL);
811 matcho ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
812 break;
813 case 'd':
814 matchds ("declare reduction", gfc_match_omp_declare_reduction,
815 ST_OMP_DECLARE_REDUCTION);
816 matchds ("declare simd", gfc_match_omp_declare_simd,
817 ST_OMP_DECLARE_SIMD);
818 matchdo ("declare target", gfc_match_omp_declare_target,
819 ST_OMP_DECLARE_TARGET);
820 matchs ("distribute parallel do simd",
821 gfc_match_omp_distribute_parallel_do_simd,
822 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD);
823 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do,
824 ST_OMP_DISTRIBUTE_PARALLEL_DO);
825 matchs ("distribute simd", gfc_match_omp_distribute_simd,
826 ST_OMP_DISTRIBUTE_SIMD);
827 matcho ("distribute", gfc_match_omp_distribute, ST_OMP_DISTRIBUTE);
828 matchs ("do simd", gfc_match_omp_do_simd, ST_OMP_DO_SIMD);
829 matcho ("do", gfc_match_omp_do, ST_OMP_DO);
830 break;
831 case 'e':
832 matcho ("end atomic", gfc_match_omp_eos, ST_OMP_END_ATOMIC);
833 matcho ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
834 matchs ("end distribute parallel do simd", gfc_match_omp_eos,
835 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD);
836 matcho ("end distribute parallel do", gfc_match_omp_eos,
837 ST_OMP_END_DISTRIBUTE_PARALLEL_DO);
838 matchs ("end distribute simd", gfc_match_omp_eos,
839 ST_OMP_END_DISTRIBUTE_SIMD);
840 matcho ("end distribute", gfc_match_omp_eos, ST_OMP_END_DISTRIBUTE);
841 matchs ("end do simd", gfc_match_omp_end_nowait, ST_OMP_END_DO_SIMD);
842 matcho ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
843 matchs ("end simd", gfc_match_omp_eos, ST_OMP_END_SIMD);
844 matcho ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
845 matcho ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
846 matchs ("end parallel do simd", gfc_match_omp_eos,
847 ST_OMP_END_PARALLEL_DO_SIMD);
848 matcho ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
849 matcho ("end parallel sections", gfc_match_omp_eos,
850 ST_OMP_END_PARALLEL_SECTIONS);
851 matcho ("end parallel workshare", gfc_match_omp_eos,
852 ST_OMP_END_PARALLEL_WORKSHARE);
853 matcho ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
854 matcho ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
855 matcho ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
856 matcho ("end target data", gfc_match_omp_eos, ST_OMP_END_TARGET_DATA);
857 matchs ("end target teams distribute parallel do simd",
858 gfc_match_omp_eos,
859 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
860 matcho ("end target teams distribute parallel do", gfc_match_omp_eos,
861 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
862 matchs ("end target teams distribute simd", gfc_match_omp_eos,
863 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD);
864 matcho ("end target teams distribute", gfc_match_omp_eos,
865 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE);
866 matcho ("end target teams", gfc_match_omp_eos, ST_OMP_END_TARGET_TEAMS);
867 matcho ("end target", gfc_match_omp_eos, ST_OMP_END_TARGET);
868 matcho ("end taskgroup", gfc_match_omp_eos, ST_OMP_END_TASKGROUP);
869 matcho ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
870 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos,
871 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
872 matcho ("end teams distribute parallel do", gfc_match_omp_eos,
873 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO);
874 matchs ("end teams distribute simd", gfc_match_omp_eos,
875 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD);
876 matcho ("end teams distribute", gfc_match_omp_eos,
877 ST_OMP_END_TEAMS_DISTRIBUTE);
878 matcho ("end teams", gfc_match_omp_eos, ST_OMP_END_TEAMS);
879 matcho ("end workshare", gfc_match_omp_end_nowait,
880 ST_OMP_END_WORKSHARE);
881 break;
882 case 'f':
883 matcho ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
884 break;
885 case 'm':
886 matcho ("master", gfc_match_omp_master, ST_OMP_MASTER);
887 break;
888 case 'o':
889 matcho ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
890 break;
891 case 'p':
892 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
893 ST_OMP_PARALLEL_DO_SIMD);
894 matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
895 matcho ("parallel sections", gfc_match_omp_parallel_sections,
896 ST_OMP_PARALLEL_SECTIONS);
897 matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
898 ST_OMP_PARALLEL_WORKSHARE);
899 matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
900 break;
901 case 's':
902 matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
903 matcho ("section", gfc_match_omp_eos, ST_OMP_SECTION);
904 matchs ("simd", gfc_match_omp_simd, ST_OMP_SIMD);
905 matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
906 break;
907 case 't':
908 matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
909 matchs ("target teams distribute parallel do simd",
910 gfc_match_omp_target_teams_distribute_parallel_do_simd,
911 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
912 matcho ("target teams distribute parallel do",
913 gfc_match_omp_target_teams_distribute_parallel_do,
914 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
915 matchs ("target teams distribute simd",
916 gfc_match_omp_target_teams_distribute_simd,
917 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
918 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
919 ST_OMP_TARGET_TEAMS_DISTRIBUTE);
920 matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
921 matcho ("target update", gfc_match_omp_target_update,
922 ST_OMP_TARGET_UPDATE);
923 matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
924 matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
925 matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
926 matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
927 matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
928 matchs ("teams distribute parallel do simd",
929 gfc_match_omp_teams_distribute_parallel_do_simd,
930 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
931 matcho ("teams distribute parallel do",
932 gfc_match_omp_teams_distribute_parallel_do,
933 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
934 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
935 ST_OMP_TEAMS_DISTRIBUTE_SIMD);
936 matcho ("teams distribute", gfc_match_omp_teams_distribute,
937 ST_OMP_TEAMS_DISTRIBUTE);
938 matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
939 matchdo ("threadprivate", gfc_match_omp_threadprivate,
940 ST_OMP_THREADPRIVATE);
941 break;
942 case 'w':
943 matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
944 break;
947 /* All else has failed, so give up. See if any of the matchers has
948 stored an error message of some sort. Don't error out if
949 not -fopenmp and simd_matched is false, i.e. if a directive other
950 than one marked with match has been seen. */
952 if (flag_openmp || simd_matched)
954 if (!gfc_error_check ())
955 gfc_error_now ("Unclassifiable OpenMP directive at %C");
958 reject_statement ();
960 gfc_error_recovery ();
962 return ST_NONE;
964 do_spec_only:
965 reject_statement ();
966 gfc_clear_error ();
967 gfc_buffer_error (false);
968 gfc_current_locus = old_locus;
969 return ST_GET_FCN_CHARACTERISTICS;
972 static gfc_statement
973 decode_gcc_attribute (void)
975 locus old_locus;
977 gfc_enforce_clean_symbol_state ();
979 gfc_clear_error (); /* Clear any pending errors. */
980 gfc_clear_warning (); /* Clear any pending warnings. */
981 old_locus = gfc_current_locus;
983 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
985 /* All else has failed, so give up. See if any of the matchers has
986 stored an error message of some sort. */
988 if (!gfc_error_check ())
989 gfc_error_now ("Unclassifiable GCC directive at %C");
991 reject_statement ();
993 gfc_error_recovery ();
995 return ST_NONE;
998 #undef match
1000 /* Assert next length characters to be equal to token in free form. */
1002 static void
1003 verify_token_free (const char* token, int length, bool last_was_use_stmt)
1005 int i;
1006 char c;
1008 c = gfc_next_ascii_char ();
1009 for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
1010 gcc_assert (c == token[i]);
1012 gcc_assert (gfc_is_whitespace(c));
1013 gfc_gobble_whitespace ();
1014 if (last_was_use_stmt)
1015 use_modules ();
1018 /* Get the next statement in free form source. */
1020 static gfc_statement
1021 next_free (void)
1023 match m;
1024 int i, cnt, at_bol;
1025 char c;
1027 at_bol = gfc_at_bol ();
1028 gfc_gobble_whitespace ();
1030 c = gfc_peek_ascii_char ();
1032 if (ISDIGIT (c))
1034 char d;
1036 /* Found a statement label? */
1037 m = gfc_match_st_label (&gfc_statement_label);
1039 d = gfc_peek_ascii_char ();
1040 if (m != MATCH_YES || !gfc_is_whitespace (d))
1042 gfc_match_small_literal_int (&i, &cnt);
1044 if (cnt > 5)
1045 gfc_error_now ("Too many digits in statement label at %C");
1047 if (i == 0)
1048 gfc_error_now ("Zero is not a valid statement label at %C");
1051 c = gfc_next_ascii_char ();
1052 while (ISDIGIT(c));
1054 if (!gfc_is_whitespace (c))
1055 gfc_error_now ("Non-numeric character in statement label at %C");
1057 return ST_NONE;
1059 else
1061 label_locus = gfc_current_locus;
1063 gfc_gobble_whitespace ();
1065 if (at_bol && gfc_peek_ascii_char () == ';')
1067 gfc_error_now ("Semicolon at %C needs to be preceded by "
1068 "statement");
1069 gfc_next_ascii_char (); /* Eat up the semicolon. */
1070 return ST_NONE;
1073 if (gfc_match_eos () == MATCH_YES)
1075 gfc_warning_now (0, "Ignoring statement label in empty statement "
1076 "at %L", &label_locus);
1077 gfc_free_st_label (gfc_statement_label);
1078 gfc_statement_label = NULL;
1079 return ST_NONE;
1083 else if (c == '!')
1085 /* Comments have already been skipped by the time we get here,
1086 except for GCC attributes and OpenMP/OpenACC directives. */
1088 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
1089 c = gfc_peek_ascii_char ();
1091 if (c == 'g')
1093 int i;
1095 c = gfc_next_ascii_char ();
1096 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
1097 gcc_assert (c == "gcc$"[i]);
1099 gfc_gobble_whitespace ();
1100 return decode_gcc_attribute ();
1103 else if (c == '$')
1105 /* Since both OpenMP and OpenACC directives starts with
1106 !$ character sequence, we must check all flags combinations */
1107 if ((flag_openmp || flag_openmp_simd)
1108 && !flag_openacc)
1110 verify_token_free ("$omp", 4, last_was_use_stmt);
1111 return decode_omp_directive ();
1113 else if ((flag_openmp || flag_openmp_simd)
1114 && flag_openacc)
1116 gfc_next_ascii_char (); /* Eat up dollar character */
1117 c = gfc_peek_ascii_char ();
1119 if (c == 'o')
1121 verify_token_free ("omp", 3, last_was_use_stmt);
1122 return decode_omp_directive ();
1124 else if (c == 'a')
1126 verify_token_free ("acc", 3, last_was_use_stmt);
1127 return decode_oacc_directive ();
1130 else if (flag_openacc)
1132 verify_token_free ("$acc", 4, last_was_use_stmt);
1133 return decode_oacc_directive ();
1136 gcc_unreachable ();
1139 if (at_bol && c == ';')
1141 if (!(gfc_option.allow_std & GFC_STD_F2008))
1142 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1143 "statement");
1144 gfc_next_ascii_char (); /* Eat up the semicolon. */
1145 return ST_NONE;
1148 return decode_statement ();
1151 /* Assert next length characters to be equal to token in fixed form. */
1153 static bool
1154 verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
1156 int i;
1157 char c = gfc_next_char_literal (NONSTRING);
1159 for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
1160 gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
1162 if (c != ' ' && c != '0')
1164 gfc_buffer_error (false);
1165 gfc_error ("Bad continuation line at %C");
1166 return false;
1168 if (last_was_use_stmt)
1169 use_modules ();
1171 return true;
1174 /* Get the next statement in fixed-form source. */
1176 static gfc_statement
1177 next_fixed (void)
1179 int label, digit_flag, i;
1180 locus loc;
1181 gfc_char_t c;
1183 if (!gfc_at_bol ())
1184 return decode_statement ();
1186 /* Skip past the current label field, parsing a statement label if
1187 one is there. This is a weird number parser, since the number is
1188 contained within five columns and can have any kind of embedded
1189 spaces. We also check for characters that make the rest of the
1190 line a comment. */
1192 label = 0;
1193 digit_flag = 0;
1195 for (i = 0; i < 5; i++)
1197 c = gfc_next_char_literal (NONSTRING);
1199 switch (c)
1201 case ' ':
1202 break;
1204 case '0':
1205 case '1':
1206 case '2':
1207 case '3':
1208 case '4':
1209 case '5':
1210 case '6':
1211 case '7':
1212 case '8':
1213 case '9':
1214 label = label * 10 + ((unsigned char) c - '0');
1215 label_locus = gfc_current_locus;
1216 digit_flag = 1;
1217 break;
1219 /* Comments have already been skipped by the time we get
1220 here, except for GCC attributes and OpenMP directives. */
1222 case '*':
1223 c = gfc_next_char_literal (NONSTRING);
1225 if (TOLOWER (c) == 'g')
1227 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
1228 gcc_assert (TOLOWER (c) == "gcc$"[i]);
1230 return decode_gcc_attribute ();
1232 else if (c == '$')
1234 if ((flag_openmp || flag_openmp_simd)
1235 && !flag_openacc)
1237 if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
1238 return ST_NONE;
1239 return decode_omp_directive ();
1241 else if ((flag_openmp || flag_openmp_simd)
1242 && flag_openacc)
1244 c = gfc_next_char_literal(NONSTRING);
1245 if (c == 'o' || c == 'O')
1247 if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
1248 return ST_NONE;
1249 return decode_omp_directive ();
1251 else if (c == 'a' || c == 'A')
1253 if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
1254 return ST_NONE;
1255 return decode_oacc_directive ();
1258 else if (flag_openacc)
1260 if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
1261 return ST_NONE;
1262 return decode_oacc_directive ();
1265 /* FALLTHROUGH */
1267 /* Comments have already been skipped by the time we get
1268 here so don't bother checking for them. */
1270 default:
1271 gfc_buffer_error (false);
1272 gfc_error ("Non-numeric character in statement label at %C");
1273 return ST_NONE;
1277 if (digit_flag)
1279 if (label == 0)
1280 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1281 else
1283 /* We've found a valid statement label. */
1284 gfc_statement_label = gfc_get_st_label (label);
1288 /* Since this line starts a statement, it cannot be a continuation
1289 of a previous statement. If we see something here besides a
1290 space or zero, it must be a bad continuation line. */
1292 c = gfc_next_char_literal (NONSTRING);
1293 if (c == '\n')
1294 goto blank_line;
1296 if (c != ' ' && c != '0')
1298 gfc_buffer_error (false);
1299 gfc_error ("Bad continuation line at %C");
1300 return ST_NONE;
1303 /* Now that we've taken care of the statement label columns, we have
1304 to make sure that the first nonblank character is not a '!'. If
1305 it is, the rest of the line is a comment. */
1309 loc = gfc_current_locus;
1310 c = gfc_next_char_literal (NONSTRING);
1312 while (gfc_is_whitespace (c));
1314 if (c == '!')
1315 goto blank_line;
1316 gfc_current_locus = loc;
1318 if (c == ';')
1320 if (digit_flag)
1321 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1322 else if (!(gfc_option.allow_std & GFC_STD_F2008))
1323 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1324 "statement");
1325 return ST_NONE;
1328 if (gfc_match_eos () == MATCH_YES)
1329 goto blank_line;
1331 /* At this point, we've got a nonblank statement to parse. */
1332 return decode_statement ();
1334 blank_line:
1335 if (digit_flag)
1336 gfc_warning_now (0, "Ignoring statement label in empty statement at %L",
1337 &label_locus);
1339 gfc_current_locus.lb->truncated = 0;
1340 gfc_advance_line ();
1341 return ST_NONE;
1345 /* Return the next non-ST_NONE statement to the caller. We also worry
1346 about including files and the ends of include files at this stage. */
1348 static gfc_statement
1349 next_statement (void)
1351 gfc_statement st;
1352 locus old_locus;
1354 gfc_enforce_clean_symbol_state ();
1356 gfc_new_block = NULL;
1358 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
1359 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
1360 gfc_current_ns->old_data = gfc_current_ns->data;
1361 for (;;)
1363 gfc_statement_label = NULL;
1364 gfc_buffer_error (true);
1366 if (gfc_at_eol ())
1367 gfc_advance_line ();
1369 gfc_skip_comments ();
1371 if (gfc_at_end ())
1373 st = ST_NONE;
1374 break;
1377 if (gfc_define_undef_line ())
1378 continue;
1380 old_locus = gfc_current_locus;
1382 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
1384 if (st != ST_NONE)
1385 break;
1388 gfc_buffer_error (false);
1390 if (st == ST_GET_FCN_CHARACTERISTICS)
1392 if (gfc_statement_label != NULL)
1394 gfc_free_st_label (gfc_statement_label);
1395 gfc_statement_label = NULL;
1397 gfc_current_locus = old_locus;
1400 if (st != ST_NONE)
1401 check_statement_label (st);
1403 return st;
1407 /****************************** Parser ***********************************/
1409 /* The parser subroutines are of type 'try' that fail if the file ends
1410 unexpectedly. */
1412 /* Macros that expand to case-labels for various classes of
1413 statements. Start with executable statements that directly do
1414 things. */
1416 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1417 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1418 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1419 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1420 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1421 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1422 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1423 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1424 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1425 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1426 case ST_OMP_TARGET_UPDATE: case ST_ERROR_STOP: case ST_SYNC_ALL: \
1427 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1428 case ST_EVENT_POST: case ST_EVENT_WAIT: \
1429 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1430 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1432 /* Statements that mark other executable statements. */
1434 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1435 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1436 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1437 case ST_OMP_PARALLEL: \
1438 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1439 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1440 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1441 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1442 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1443 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1444 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1445 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1446 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1447 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1448 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1449 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1450 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1451 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1452 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1453 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1454 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: \
1455 case ST_CRITICAL: \
1456 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1457 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: \
1458 case ST_OACC_KERNELS_LOOP: case ST_OACC_ATOMIC
1460 /* Declaration statements */
1462 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1463 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1464 case ST_TYPE: case ST_INTERFACE: case ST_PROCEDURE: case ST_OACC_ROUTINE: \
1465 case ST_OACC_DECLARE
1467 /* OpenMP declaration statements. */
1469 #define case_omp_decl case ST_OMP_THREADPRIVATE: case ST_OMP_DECLARE_SIMD: \
1470 case ST_OMP_DECLARE_TARGET: case ST_OMP_DECLARE_REDUCTION
1472 /* Block end statements. Errors associated with interchanging these
1473 are detected in gfc_match_end(). */
1475 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1476 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1477 case ST_END_BLOCK: case ST_END_ASSOCIATE
1480 /* Push a new state onto the stack. */
1482 static void
1483 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
1485 p->state = new_state;
1486 p->previous = gfc_state_stack;
1487 p->sym = sym;
1488 p->head = p->tail = NULL;
1489 p->do_variable = NULL;
1490 if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
1491 p->ext.oacc_declare_clauses = NULL;
1493 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1494 construct statement was accepted right before pushing the state. Thus,
1495 the construct's gfc_code is available as tail of the parent state. */
1496 gcc_assert (gfc_state_stack);
1497 p->construct = gfc_state_stack->tail;
1499 gfc_state_stack = p;
1503 /* Pop the current state. */
1504 static void
1505 pop_state (void)
1507 gfc_state_stack = gfc_state_stack->previous;
1511 /* Try to find the given state in the state stack. */
1513 bool
1514 gfc_find_state (gfc_compile_state state)
1516 gfc_state_data *p;
1518 for (p = gfc_state_stack; p; p = p->previous)
1519 if (p->state == state)
1520 break;
1522 return (p == NULL) ? false : true;
1526 /* Starts a new level in the statement list. */
1528 static gfc_code *
1529 new_level (gfc_code *q)
1531 gfc_code *p;
1533 p = q->block = gfc_get_code (EXEC_NOP);
1535 gfc_state_stack->head = gfc_state_stack->tail = p;
1537 return p;
1541 /* Add the current new_st code structure and adds it to the current
1542 program unit. As a side-effect, it zeroes the new_st. */
1544 static gfc_code *
1545 add_statement (void)
1547 gfc_code *p;
1549 p = XCNEW (gfc_code);
1550 *p = new_st;
1552 p->loc = gfc_current_locus;
1554 if (gfc_state_stack->head == NULL)
1555 gfc_state_stack->head = p;
1556 else
1557 gfc_state_stack->tail->next = p;
1559 while (p->next != NULL)
1560 p = p->next;
1562 gfc_state_stack->tail = p;
1564 gfc_clear_new_st ();
1566 return p;
1570 /* Frees everything associated with the current statement. */
1572 static void
1573 undo_new_statement (void)
1575 gfc_free_statements (new_st.block);
1576 gfc_free_statements (new_st.next);
1577 gfc_free_statement (&new_st);
1578 gfc_clear_new_st ();
1582 /* If the current statement has a statement label, make sure that it
1583 is allowed to, or should have one. */
1585 static void
1586 check_statement_label (gfc_statement st)
1588 gfc_sl_type type;
1590 if (gfc_statement_label == NULL)
1592 if (st == ST_FORMAT)
1593 gfc_error ("FORMAT statement at %L does not have a statement label",
1594 &new_st.loc);
1595 return;
1598 switch (st)
1600 case ST_END_PROGRAM:
1601 case ST_END_FUNCTION:
1602 case ST_END_SUBROUTINE:
1603 case ST_ENDDO:
1604 case ST_ENDIF:
1605 case ST_END_SELECT:
1606 case ST_END_CRITICAL:
1607 case ST_END_BLOCK:
1608 case ST_END_ASSOCIATE:
1609 case_executable:
1610 case_exec_markers:
1611 if (st == ST_ENDDO || st == ST_CONTINUE)
1612 type = ST_LABEL_DO_TARGET;
1613 else
1614 type = ST_LABEL_TARGET;
1615 break;
1617 case ST_FORMAT:
1618 type = ST_LABEL_FORMAT;
1619 break;
1621 /* Statement labels are not restricted from appearing on a
1622 particular line. However, there are plenty of situations
1623 where the resulting label can't be referenced. */
1625 default:
1626 type = ST_LABEL_BAD_TARGET;
1627 break;
1630 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1632 new_st.here = gfc_statement_label;
1636 /* Figures out what the enclosing program unit is. This will be a
1637 function, subroutine, program, block data or module. */
1639 gfc_state_data *
1640 gfc_enclosing_unit (gfc_compile_state * result)
1642 gfc_state_data *p;
1644 for (p = gfc_state_stack; p; p = p->previous)
1645 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1646 || p->state == COMP_MODULE || p->state == COMP_SUBMODULE
1647 || p->state == COMP_BLOCK_DATA || p->state == COMP_PROGRAM)
1650 if (result != NULL)
1651 *result = p->state;
1652 return p;
1655 if (result != NULL)
1656 *result = COMP_PROGRAM;
1657 return NULL;
1661 /* Translate a statement enum to a string. */
1663 const char *
1664 gfc_ascii_statement (gfc_statement st)
1666 const char *p;
1668 switch (st)
1670 case ST_ARITHMETIC_IF:
1671 p = _("arithmetic IF");
1672 break;
1673 case ST_ALLOCATE:
1674 p = "ALLOCATE";
1675 break;
1676 case ST_ASSOCIATE:
1677 p = "ASSOCIATE";
1678 break;
1679 case ST_ATTR_DECL:
1680 p = _("attribute declaration");
1681 break;
1682 case ST_BACKSPACE:
1683 p = "BACKSPACE";
1684 break;
1685 case ST_BLOCK:
1686 p = "BLOCK";
1687 break;
1688 case ST_BLOCK_DATA:
1689 p = "BLOCK DATA";
1690 break;
1691 case ST_CALL:
1692 p = "CALL";
1693 break;
1694 case ST_CASE:
1695 p = "CASE";
1696 break;
1697 case ST_CLOSE:
1698 p = "CLOSE";
1699 break;
1700 case ST_COMMON:
1701 p = "COMMON";
1702 break;
1703 case ST_CONTINUE:
1704 p = "CONTINUE";
1705 break;
1706 case ST_CONTAINS:
1707 p = "CONTAINS";
1708 break;
1709 case ST_CRITICAL:
1710 p = "CRITICAL";
1711 break;
1712 case ST_CYCLE:
1713 p = "CYCLE";
1714 break;
1715 case ST_DATA_DECL:
1716 p = _("data declaration");
1717 break;
1718 case ST_DATA:
1719 p = "DATA";
1720 break;
1721 case ST_DEALLOCATE:
1722 p = "DEALLOCATE";
1723 break;
1724 case ST_MAP:
1725 p = "MAP";
1726 break;
1727 case ST_UNION:
1728 p = "UNION";
1729 break;
1730 case ST_STRUCTURE_DECL:
1731 p = "STRUCTURE";
1732 break;
1733 case ST_DERIVED_DECL:
1734 p = _("derived type declaration");
1735 break;
1736 case ST_DO:
1737 p = "DO";
1738 break;
1739 case ST_ELSE:
1740 p = "ELSE";
1741 break;
1742 case ST_ELSEIF:
1743 p = "ELSE IF";
1744 break;
1745 case ST_ELSEWHERE:
1746 p = "ELSEWHERE";
1747 break;
1748 case ST_EVENT_POST:
1749 p = "EVENT POST";
1750 break;
1751 case ST_EVENT_WAIT:
1752 p = "EVENT WAIT";
1753 break;
1754 case ST_END_ASSOCIATE:
1755 p = "END ASSOCIATE";
1756 break;
1757 case ST_END_BLOCK:
1758 p = "END BLOCK";
1759 break;
1760 case ST_END_BLOCK_DATA:
1761 p = "END BLOCK DATA";
1762 break;
1763 case ST_END_CRITICAL:
1764 p = "END CRITICAL";
1765 break;
1766 case ST_ENDDO:
1767 p = "END DO";
1768 break;
1769 case ST_END_FILE:
1770 p = "END FILE";
1771 break;
1772 case ST_END_FORALL:
1773 p = "END FORALL";
1774 break;
1775 case ST_END_FUNCTION:
1776 p = "END FUNCTION";
1777 break;
1778 case ST_ENDIF:
1779 p = "END IF";
1780 break;
1781 case ST_END_INTERFACE:
1782 p = "END INTERFACE";
1783 break;
1784 case ST_END_MODULE:
1785 p = "END MODULE";
1786 break;
1787 case ST_END_SUBMODULE:
1788 p = "END SUBMODULE";
1789 break;
1790 case ST_END_PROGRAM:
1791 p = "END PROGRAM";
1792 break;
1793 case ST_END_SELECT:
1794 p = "END SELECT";
1795 break;
1796 case ST_END_SUBROUTINE:
1797 p = "END SUBROUTINE";
1798 break;
1799 case ST_END_WHERE:
1800 p = "END WHERE";
1801 break;
1802 case ST_END_STRUCTURE:
1803 p = "END STRUCTURE";
1804 break;
1805 case ST_END_UNION:
1806 p = "END UNION";
1807 break;
1808 case ST_END_MAP:
1809 p = "END MAP";
1810 break;
1811 case ST_END_TYPE:
1812 p = "END TYPE";
1813 break;
1814 case ST_ENTRY:
1815 p = "ENTRY";
1816 break;
1817 case ST_EQUIVALENCE:
1818 p = "EQUIVALENCE";
1819 break;
1820 case ST_ERROR_STOP:
1821 p = "ERROR STOP";
1822 break;
1823 case ST_EXIT:
1824 p = "EXIT";
1825 break;
1826 case ST_FLUSH:
1827 p = "FLUSH";
1828 break;
1829 case ST_FORALL_BLOCK: /* Fall through */
1830 case ST_FORALL:
1831 p = "FORALL";
1832 break;
1833 case ST_FORMAT:
1834 p = "FORMAT";
1835 break;
1836 case ST_FUNCTION:
1837 p = "FUNCTION";
1838 break;
1839 case ST_GENERIC:
1840 p = "GENERIC";
1841 break;
1842 case ST_GOTO:
1843 p = "GOTO";
1844 break;
1845 case ST_IF_BLOCK:
1846 p = _("block IF");
1847 break;
1848 case ST_IMPLICIT:
1849 p = "IMPLICIT";
1850 break;
1851 case ST_IMPLICIT_NONE:
1852 p = "IMPLICIT NONE";
1853 break;
1854 case ST_IMPLIED_ENDDO:
1855 p = _("implied END DO");
1856 break;
1857 case ST_IMPORT:
1858 p = "IMPORT";
1859 break;
1860 case ST_INQUIRE:
1861 p = "INQUIRE";
1862 break;
1863 case ST_INTERFACE:
1864 p = "INTERFACE";
1865 break;
1866 case ST_LOCK:
1867 p = "LOCK";
1868 break;
1869 case ST_PARAMETER:
1870 p = "PARAMETER";
1871 break;
1872 case ST_PRIVATE:
1873 p = "PRIVATE";
1874 break;
1875 case ST_PUBLIC:
1876 p = "PUBLIC";
1877 break;
1878 case ST_MODULE:
1879 p = "MODULE";
1880 break;
1881 case ST_SUBMODULE:
1882 p = "SUBMODULE";
1883 break;
1884 case ST_PAUSE:
1885 p = "PAUSE";
1886 break;
1887 case ST_MODULE_PROC:
1888 p = "MODULE PROCEDURE";
1889 break;
1890 case ST_NAMELIST:
1891 p = "NAMELIST";
1892 break;
1893 case ST_NULLIFY:
1894 p = "NULLIFY";
1895 break;
1896 case ST_OPEN:
1897 p = "OPEN";
1898 break;
1899 case ST_PROGRAM:
1900 p = "PROGRAM";
1901 break;
1902 case ST_PROCEDURE:
1903 p = "PROCEDURE";
1904 break;
1905 case ST_READ:
1906 p = "READ";
1907 break;
1908 case ST_RETURN:
1909 p = "RETURN";
1910 break;
1911 case ST_REWIND:
1912 p = "REWIND";
1913 break;
1914 case ST_STOP:
1915 p = "STOP";
1916 break;
1917 case ST_SYNC_ALL:
1918 p = "SYNC ALL";
1919 break;
1920 case ST_SYNC_IMAGES:
1921 p = "SYNC IMAGES";
1922 break;
1923 case ST_SYNC_MEMORY:
1924 p = "SYNC MEMORY";
1925 break;
1926 case ST_SUBROUTINE:
1927 p = "SUBROUTINE";
1928 break;
1929 case ST_TYPE:
1930 p = "TYPE";
1931 break;
1932 case ST_UNLOCK:
1933 p = "UNLOCK";
1934 break;
1935 case ST_USE:
1936 p = "USE";
1937 break;
1938 case ST_WHERE_BLOCK: /* Fall through */
1939 case ST_WHERE:
1940 p = "WHERE";
1941 break;
1942 case ST_WAIT:
1943 p = "WAIT";
1944 break;
1945 case ST_WRITE:
1946 p = "WRITE";
1947 break;
1948 case ST_ASSIGNMENT:
1949 p = _("assignment");
1950 break;
1951 case ST_POINTER_ASSIGNMENT:
1952 p = _("pointer assignment");
1953 break;
1954 case ST_SELECT_CASE:
1955 p = "SELECT CASE";
1956 break;
1957 case ST_SELECT_TYPE:
1958 p = "SELECT TYPE";
1959 break;
1960 case ST_TYPE_IS:
1961 p = "TYPE IS";
1962 break;
1963 case ST_CLASS_IS:
1964 p = "CLASS IS";
1965 break;
1966 case ST_SEQUENCE:
1967 p = "SEQUENCE";
1968 break;
1969 case ST_SIMPLE_IF:
1970 p = _("simple IF");
1971 break;
1972 case ST_STATEMENT_FUNCTION:
1973 p = "STATEMENT FUNCTION";
1974 break;
1975 case ST_LABEL_ASSIGNMENT:
1976 p = "LABEL ASSIGNMENT";
1977 break;
1978 case ST_ENUM:
1979 p = "ENUM DEFINITION";
1980 break;
1981 case ST_ENUMERATOR:
1982 p = "ENUMERATOR DEFINITION";
1983 break;
1984 case ST_END_ENUM:
1985 p = "END ENUM";
1986 break;
1987 case ST_OACC_PARALLEL_LOOP:
1988 p = "!$ACC PARALLEL LOOP";
1989 break;
1990 case ST_OACC_END_PARALLEL_LOOP:
1991 p = "!$ACC END PARALLEL LOOP";
1992 break;
1993 case ST_OACC_PARALLEL:
1994 p = "!$ACC PARALLEL";
1995 break;
1996 case ST_OACC_END_PARALLEL:
1997 p = "!$ACC END PARALLEL";
1998 break;
1999 case ST_OACC_KERNELS:
2000 p = "!$ACC KERNELS";
2001 break;
2002 case ST_OACC_END_KERNELS:
2003 p = "!$ACC END KERNELS";
2004 break;
2005 case ST_OACC_KERNELS_LOOP:
2006 p = "!$ACC KERNELS LOOP";
2007 break;
2008 case ST_OACC_END_KERNELS_LOOP:
2009 p = "!$ACC END KERNELS LOOP";
2010 break;
2011 case ST_OACC_DATA:
2012 p = "!$ACC DATA";
2013 break;
2014 case ST_OACC_END_DATA:
2015 p = "!$ACC END DATA";
2016 break;
2017 case ST_OACC_HOST_DATA:
2018 p = "!$ACC HOST_DATA";
2019 break;
2020 case ST_OACC_END_HOST_DATA:
2021 p = "!$ACC END HOST_DATA";
2022 break;
2023 case ST_OACC_LOOP:
2024 p = "!$ACC LOOP";
2025 break;
2026 case ST_OACC_END_LOOP:
2027 p = "!$ACC END LOOP";
2028 break;
2029 case ST_OACC_DECLARE:
2030 p = "!$ACC DECLARE";
2031 break;
2032 case ST_OACC_UPDATE:
2033 p = "!$ACC UPDATE";
2034 break;
2035 case ST_OACC_WAIT:
2036 p = "!$ACC WAIT";
2037 break;
2038 case ST_OACC_CACHE:
2039 p = "!$ACC CACHE";
2040 break;
2041 case ST_OACC_ENTER_DATA:
2042 p = "!$ACC ENTER DATA";
2043 break;
2044 case ST_OACC_EXIT_DATA:
2045 p = "!$ACC EXIT DATA";
2046 break;
2047 case ST_OACC_ROUTINE:
2048 p = "!$ACC ROUTINE";
2049 break;
2050 case ST_OACC_ATOMIC:
2051 p = "!ACC ATOMIC";
2052 break;
2053 case ST_OACC_END_ATOMIC:
2054 p = "!ACC END ATOMIC";
2055 break;
2056 case ST_OMP_ATOMIC:
2057 p = "!$OMP ATOMIC";
2058 break;
2059 case ST_OMP_BARRIER:
2060 p = "!$OMP BARRIER";
2061 break;
2062 case ST_OMP_CANCEL:
2063 p = "!$OMP CANCEL";
2064 break;
2065 case ST_OMP_CANCELLATION_POINT:
2066 p = "!$OMP CANCELLATION POINT";
2067 break;
2068 case ST_OMP_CRITICAL:
2069 p = "!$OMP CRITICAL";
2070 break;
2071 case ST_OMP_DECLARE_REDUCTION:
2072 p = "!$OMP DECLARE REDUCTION";
2073 break;
2074 case ST_OMP_DECLARE_SIMD:
2075 p = "!$OMP DECLARE SIMD";
2076 break;
2077 case ST_OMP_DECLARE_TARGET:
2078 p = "!$OMP DECLARE TARGET";
2079 break;
2080 case ST_OMP_DISTRIBUTE:
2081 p = "!$OMP DISTRIBUTE";
2082 break;
2083 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
2084 p = "!$OMP DISTRIBUTE PARALLEL DO";
2085 break;
2086 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
2087 p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
2088 break;
2089 case ST_OMP_DISTRIBUTE_SIMD:
2090 p = "!$OMP DISTRIBUTE SIMD";
2091 break;
2092 case ST_OMP_DO:
2093 p = "!$OMP DO";
2094 break;
2095 case ST_OMP_DO_SIMD:
2096 p = "!$OMP DO SIMD";
2097 break;
2098 case ST_OMP_END_ATOMIC:
2099 p = "!$OMP END ATOMIC";
2100 break;
2101 case ST_OMP_END_CRITICAL:
2102 p = "!$OMP END CRITICAL";
2103 break;
2104 case ST_OMP_END_DISTRIBUTE:
2105 p = "!$OMP END DISTRIBUTE";
2106 break;
2107 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
2108 p = "!$OMP END DISTRIBUTE PARALLEL DO";
2109 break;
2110 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
2111 p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
2112 break;
2113 case ST_OMP_END_DISTRIBUTE_SIMD:
2114 p = "!$OMP END DISTRIBUTE SIMD";
2115 break;
2116 case ST_OMP_END_DO:
2117 p = "!$OMP END DO";
2118 break;
2119 case ST_OMP_END_DO_SIMD:
2120 p = "!$OMP END DO SIMD";
2121 break;
2122 case ST_OMP_END_SIMD:
2123 p = "!$OMP END SIMD";
2124 break;
2125 case ST_OMP_END_MASTER:
2126 p = "!$OMP END MASTER";
2127 break;
2128 case ST_OMP_END_ORDERED:
2129 p = "!$OMP END ORDERED";
2130 break;
2131 case ST_OMP_END_PARALLEL:
2132 p = "!$OMP END PARALLEL";
2133 break;
2134 case ST_OMP_END_PARALLEL_DO:
2135 p = "!$OMP END PARALLEL DO";
2136 break;
2137 case ST_OMP_END_PARALLEL_DO_SIMD:
2138 p = "!$OMP END PARALLEL DO SIMD";
2139 break;
2140 case ST_OMP_END_PARALLEL_SECTIONS:
2141 p = "!$OMP END PARALLEL SECTIONS";
2142 break;
2143 case ST_OMP_END_PARALLEL_WORKSHARE:
2144 p = "!$OMP END PARALLEL WORKSHARE";
2145 break;
2146 case ST_OMP_END_SECTIONS:
2147 p = "!$OMP END SECTIONS";
2148 break;
2149 case ST_OMP_END_SINGLE:
2150 p = "!$OMP END SINGLE";
2151 break;
2152 case ST_OMP_END_TASK:
2153 p = "!$OMP END TASK";
2154 break;
2155 case ST_OMP_END_TARGET:
2156 p = "!$OMP END TARGET";
2157 break;
2158 case ST_OMP_END_TARGET_DATA:
2159 p = "!$OMP END TARGET DATA";
2160 break;
2161 case ST_OMP_END_TARGET_TEAMS:
2162 p = "!$OMP END TARGET TEAMS";
2163 break;
2164 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
2165 p = "!$OMP END TARGET TEAMS DISTRIBUTE";
2166 break;
2167 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2168 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2169 break;
2170 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2171 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2172 break;
2173 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
2174 p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2175 break;
2176 case ST_OMP_END_TASKGROUP:
2177 p = "!$OMP END TASKGROUP";
2178 break;
2179 case ST_OMP_END_TEAMS:
2180 p = "!$OMP END TEAMS";
2181 break;
2182 case ST_OMP_END_TEAMS_DISTRIBUTE:
2183 p = "!$OMP END TEAMS DISTRIBUTE";
2184 break;
2185 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
2186 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2187 break;
2188 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2189 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2190 break;
2191 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
2192 p = "!$OMP END TEAMS DISTRIBUTE SIMD";
2193 break;
2194 case ST_OMP_END_WORKSHARE:
2195 p = "!$OMP END WORKSHARE";
2196 break;
2197 case ST_OMP_FLUSH:
2198 p = "!$OMP FLUSH";
2199 break;
2200 case ST_OMP_MASTER:
2201 p = "!$OMP MASTER";
2202 break;
2203 case ST_OMP_ORDERED:
2204 p = "!$OMP ORDERED";
2205 break;
2206 case ST_OMP_PARALLEL:
2207 p = "!$OMP PARALLEL";
2208 break;
2209 case ST_OMP_PARALLEL_DO:
2210 p = "!$OMP PARALLEL DO";
2211 break;
2212 case ST_OMP_PARALLEL_DO_SIMD:
2213 p = "!$OMP PARALLEL DO SIMD";
2214 break;
2215 case ST_OMP_PARALLEL_SECTIONS:
2216 p = "!$OMP PARALLEL SECTIONS";
2217 break;
2218 case ST_OMP_PARALLEL_WORKSHARE:
2219 p = "!$OMP PARALLEL WORKSHARE";
2220 break;
2221 case ST_OMP_SECTIONS:
2222 p = "!$OMP SECTIONS";
2223 break;
2224 case ST_OMP_SECTION:
2225 p = "!$OMP SECTION";
2226 break;
2227 case ST_OMP_SIMD:
2228 p = "!$OMP SIMD";
2229 break;
2230 case ST_OMP_SINGLE:
2231 p = "!$OMP SINGLE";
2232 break;
2233 case ST_OMP_TARGET:
2234 p = "!$OMP TARGET";
2235 break;
2236 case ST_OMP_TARGET_DATA:
2237 p = "!$OMP TARGET DATA";
2238 break;
2239 case ST_OMP_TARGET_TEAMS:
2240 p = "!$OMP TARGET TEAMS";
2241 break;
2242 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
2243 p = "!$OMP TARGET TEAMS DISTRIBUTE";
2244 break;
2245 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2246 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2247 break;
2248 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2249 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2250 break;
2251 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2252 p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2253 break;
2254 case ST_OMP_TARGET_UPDATE:
2255 p = "!$OMP TARGET UPDATE";
2256 break;
2257 case ST_OMP_TASK:
2258 p = "!$OMP TASK";
2259 break;
2260 case ST_OMP_TASKGROUP:
2261 p = "!$OMP TASKGROUP";
2262 break;
2263 case ST_OMP_TASKWAIT:
2264 p = "!$OMP TASKWAIT";
2265 break;
2266 case ST_OMP_TASKYIELD:
2267 p = "!$OMP TASKYIELD";
2268 break;
2269 case ST_OMP_TEAMS:
2270 p = "!$OMP TEAMS";
2271 break;
2272 case ST_OMP_TEAMS_DISTRIBUTE:
2273 p = "!$OMP TEAMS DISTRIBUTE";
2274 break;
2275 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2276 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2277 break;
2278 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2279 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2280 break;
2281 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
2282 p = "!$OMP TEAMS DISTRIBUTE SIMD";
2283 break;
2284 case ST_OMP_THREADPRIVATE:
2285 p = "!$OMP THREADPRIVATE";
2286 break;
2287 case ST_OMP_WORKSHARE:
2288 p = "!$OMP WORKSHARE";
2289 break;
2290 default:
2291 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2294 return p;
2298 /* Create a symbol for the main program and assign it to ns->proc_name. */
2300 static void
2301 main_program_symbol (gfc_namespace *ns, const char *name)
2303 gfc_symbol *main_program;
2304 symbol_attribute attr;
2306 gfc_get_symbol (name, ns, &main_program);
2307 gfc_clear_attr (&attr);
2308 attr.flavor = FL_PROGRAM;
2309 attr.proc = PROC_UNKNOWN;
2310 attr.subroutine = 1;
2311 attr.access = ACCESS_PUBLIC;
2312 attr.is_main_program = 1;
2313 main_program->attr = attr;
2314 main_program->declared_at = gfc_current_locus;
2315 ns->proc_name = main_program;
2316 gfc_commit_symbols ();
2320 /* Do whatever is necessary to accept the last statement. */
2322 static void
2323 accept_statement (gfc_statement st)
2325 switch (st)
2327 case ST_IMPLICIT_NONE:
2328 case ST_IMPLICIT:
2329 break;
2331 case ST_FUNCTION:
2332 case ST_SUBROUTINE:
2333 case ST_MODULE:
2334 case ST_SUBMODULE:
2335 gfc_current_ns->proc_name = gfc_new_block;
2336 break;
2338 /* If the statement is the end of a block, lay down a special code
2339 that allows a branch to the end of the block from within the
2340 construct. IF and SELECT are treated differently from DO
2341 (where EXEC_NOP is added inside the loop) for two
2342 reasons:
2343 1. END DO has a meaning in the sense that after a GOTO to
2344 it, the loop counter must be increased.
2345 2. IF blocks and SELECT blocks can consist of multiple
2346 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2347 Putting the label before the END IF would make the jump
2348 from, say, the ELSE IF block to the END IF illegal. */
2350 case ST_ENDIF:
2351 case ST_END_SELECT:
2352 case ST_END_CRITICAL:
2353 if (gfc_statement_label != NULL)
2355 new_st.op = EXEC_END_NESTED_BLOCK;
2356 add_statement ();
2358 break;
2360 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2361 one parallel block. Thus, we add the special code to the nested block
2362 itself, instead of the parent one. */
2363 case ST_END_BLOCK:
2364 case ST_END_ASSOCIATE:
2365 if (gfc_statement_label != NULL)
2367 new_st.op = EXEC_END_BLOCK;
2368 add_statement ();
2370 break;
2372 /* The end-of-program unit statements do not get the special
2373 marker and require a statement of some sort if they are a
2374 branch target. */
2376 case ST_END_PROGRAM:
2377 case ST_END_FUNCTION:
2378 case ST_END_SUBROUTINE:
2379 if (gfc_statement_label != NULL)
2381 new_st.op = EXEC_RETURN;
2382 add_statement ();
2384 else
2386 new_st.op = EXEC_END_PROCEDURE;
2387 add_statement ();
2390 break;
2392 case ST_ENTRY:
2393 case_executable:
2394 case_exec_markers:
2395 add_statement ();
2396 break;
2398 default:
2399 break;
2402 gfc_commit_symbols ();
2403 gfc_warning_check ();
2404 gfc_clear_new_st ();
2408 /* Undo anything tentative that has been built for the current
2409 statement. */
2411 static void
2412 reject_statement (void)
2414 /* Revert to the previous charlen chain. */
2415 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
2416 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
2418 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
2419 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
2421 gfc_reject_data (gfc_current_ns);
2423 gfc_new_block = NULL;
2424 gfc_undo_symbols ();
2425 gfc_clear_warning ();
2426 undo_new_statement ();
2430 /* Generic complaint about an out of order statement. We also do
2431 whatever is necessary to clean up. */
2433 static void
2434 unexpected_statement (gfc_statement st)
2436 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
2438 reject_statement ();
2442 /* Given the next statement seen by the matcher, make sure that it is
2443 in proper order with the last. This subroutine is initialized by
2444 calling it with an argument of ST_NONE. If there is a problem, we
2445 issue an error and return false. Otherwise we return true.
2447 Individual parsers need to verify that the statements seen are
2448 valid before calling here, i.e., ENTRY statements are not allowed in
2449 INTERFACE blocks. The following diagram is taken from the standard:
2451 +---------------------------------------+
2452 | program subroutine function module |
2453 +---------------------------------------+
2454 | use |
2455 +---------------------------------------+
2456 | import |
2457 +---------------------------------------+
2458 | | implicit none |
2459 | +-----------+------------------+
2460 | | parameter | implicit |
2461 | +-----------+------------------+
2462 | format | | derived type |
2463 | entry | parameter | interface |
2464 | | data | specification |
2465 | | | statement func |
2466 | +-----------+------------------+
2467 | | data | executable |
2468 +--------+-----------+------------------+
2469 | contains |
2470 +---------------------------------------+
2471 | internal module/subprogram |
2472 +---------------------------------------+
2473 | end |
2474 +---------------------------------------+
2478 enum state_order
2480 ORDER_START,
2481 ORDER_USE,
2482 ORDER_IMPORT,
2483 ORDER_IMPLICIT_NONE,
2484 ORDER_IMPLICIT,
2485 ORDER_SPEC,
2486 ORDER_EXEC
2489 typedef struct
2491 enum state_order state;
2492 gfc_statement last_statement;
2493 locus where;
2495 st_state;
2497 static bool
2498 verify_st_order (st_state *p, gfc_statement st, bool silent)
2501 switch (st)
2503 case ST_NONE:
2504 p->state = ORDER_START;
2505 break;
2507 case ST_USE:
2508 if (p->state > ORDER_USE)
2509 goto order;
2510 p->state = ORDER_USE;
2511 break;
2513 case ST_IMPORT:
2514 if (p->state > ORDER_IMPORT)
2515 goto order;
2516 p->state = ORDER_IMPORT;
2517 break;
2519 case ST_IMPLICIT_NONE:
2520 if (p->state > ORDER_IMPLICIT)
2521 goto order;
2523 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2524 statement disqualifies a USE but not an IMPLICIT NONE.
2525 Duplicate IMPLICIT NONEs are caught when the implicit types
2526 are set. */
2528 p->state = ORDER_IMPLICIT_NONE;
2529 break;
2531 case ST_IMPLICIT:
2532 if (p->state > ORDER_IMPLICIT)
2533 goto order;
2534 p->state = ORDER_IMPLICIT;
2535 break;
2537 case ST_FORMAT:
2538 case ST_ENTRY:
2539 if (p->state < ORDER_IMPLICIT_NONE)
2540 p->state = ORDER_IMPLICIT_NONE;
2541 break;
2543 case ST_PARAMETER:
2544 if (p->state >= ORDER_EXEC)
2545 goto order;
2546 if (p->state < ORDER_IMPLICIT)
2547 p->state = ORDER_IMPLICIT;
2548 break;
2550 case ST_DATA:
2551 if (p->state < ORDER_SPEC)
2552 p->state = ORDER_SPEC;
2553 break;
2555 case ST_PUBLIC:
2556 case ST_PRIVATE:
2557 case ST_STRUCTURE_DECL:
2558 case ST_DERIVED_DECL:
2559 case_decl:
2560 if (p->state >= ORDER_EXEC)
2561 goto order;
2562 if (p->state < ORDER_SPEC)
2563 p->state = ORDER_SPEC;
2564 break;
2566 case_omp_decl:
2567 /* The OpenMP directives have to be somewhere in the specification
2568 part, but there are no further requirements on their ordering.
2569 Thus don't adjust p->state, just ignore them. */
2570 if (p->state >= ORDER_EXEC)
2571 goto order;
2572 break;
2574 case_executable:
2575 case_exec_markers:
2576 if (p->state < ORDER_EXEC)
2577 p->state = ORDER_EXEC;
2578 break;
2580 default:
2581 return false;
2584 /* All is well, record the statement in case we need it next time. */
2585 p->where = gfc_current_locus;
2586 p->last_statement = st;
2587 return true;
2589 order:
2590 if (!silent)
2591 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2592 gfc_ascii_statement (st),
2593 gfc_ascii_statement (p->last_statement), &p->where);
2595 return false;
2599 /* Handle an unexpected end of file. This is a show-stopper... */
2601 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
2603 static void
2604 unexpected_eof (void)
2606 gfc_state_data *p;
2608 gfc_error ("Unexpected end of file in %qs", gfc_source_file);
2610 /* Memory cleanup. Move to "second to last". */
2611 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
2612 p = p->previous);
2614 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
2615 gfc_done_2 ();
2617 longjmp (eof_buf, 1);
2621 /* Parse the CONTAINS section of a derived type definition. */
2623 gfc_access gfc_typebound_default_access;
2625 static bool
2626 parse_derived_contains (void)
2628 gfc_state_data s;
2629 bool seen_private = false;
2630 bool seen_comps = false;
2631 bool error_flag = false;
2632 bool to_finish;
2634 gcc_assert (gfc_current_state () == COMP_DERIVED);
2635 gcc_assert (gfc_current_block ());
2637 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2638 section. */
2639 if (gfc_current_block ()->attr.sequence)
2640 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2641 " section at %C", gfc_current_block ()->name);
2642 if (gfc_current_block ()->attr.is_bind_c)
2643 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2644 " section at %C", gfc_current_block ()->name);
2646 accept_statement (ST_CONTAINS);
2647 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
2649 gfc_typebound_default_access = ACCESS_PUBLIC;
2651 to_finish = false;
2652 while (!to_finish)
2654 gfc_statement st;
2655 st = next_statement ();
2656 switch (st)
2658 case ST_NONE:
2659 unexpected_eof ();
2660 break;
2662 case ST_DATA_DECL:
2663 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2664 goto error;
2666 case ST_PROCEDURE:
2667 if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
2668 goto error;
2670 accept_statement (ST_PROCEDURE);
2671 seen_comps = true;
2672 break;
2674 case ST_GENERIC:
2675 if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
2676 goto error;
2678 accept_statement (ST_GENERIC);
2679 seen_comps = true;
2680 break;
2682 case ST_FINAL:
2683 if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
2684 " at %C"))
2685 goto error;
2687 accept_statement (ST_FINAL);
2688 seen_comps = true;
2689 break;
2691 case ST_END_TYPE:
2692 to_finish = true;
2694 if (!seen_comps
2695 && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
2696 "at %C with empty CONTAINS section")))
2697 goto error;
2699 /* ST_END_TYPE is accepted by parse_derived after return. */
2700 break;
2702 case ST_PRIVATE:
2703 if (!gfc_find_state (COMP_MODULE))
2705 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2706 "a MODULE");
2707 goto error;
2710 if (seen_comps)
2712 gfc_error ("PRIVATE statement at %C must precede procedure"
2713 " bindings");
2714 goto error;
2717 if (seen_private)
2719 gfc_error ("Duplicate PRIVATE statement at %C");
2720 goto error;
2723 accept_statement (ST_PRIVATE);
2724 gfc_typebound_default_access = ACCESS_PRIVATE;
2725 seen_private = true;
2726 break;
2728 case ST_SEQUENCE:
2729 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2730 goto error;
2732 case ST_CONTAINS:
2733 gfc_error ("Already inside a CONTAINS block at %C");
2734 goto error;
2736 default:
2737 unexpected_statement (st);
2738 break;
2741 continue;
2743 error:
2744 error_flag = true;
2745 reject_statement ();
2748 pop_state ();
2749 gcc_assert (gfc_current_state () == COMP_DERIVED);
2751 return error_flag;
2755 /* Set attributes for the parent symbol based on the attributes of a component
2756 and raise errors if conflicting attributes are found for the component. */
2758 static void
2759 check_component (gfc_symbol *sym, gfc_component *c, gfc_component **lockp,
2760 gfc_component **eventp)
2762 bool coarray, lock_type, event_type, allocatable, pointer;
2763 coarray = lock_type = event_type = allocatable = pointer = false;
2764 gfc_component *lock_comp = NULL, *event_comp = NULL;
2766 if (lockp) lock_comp = *lockp;
2767 if (eventp) event_comp = *eventp;
2769 /* Look for allocatable components. */
2770 if (c->attr.allocatable
2771 || (c->ts.type == BT_CLASS && c->attr.class_ok
2772 && CLASS_DATA (c)->attr.allocatable)
2773 || (c->ts.type == BT_DERIVED && !c->attr.pointer
2774 && c->ts.u.derived->attr.alloc_comp))
2776 allocatable = true;
2777 sym->attr.alloc_comp = 1;
2780 /* Look for pointer components. */
2781 if (c->attr.pointer
2782 || (c->ts.type == BT_CLASS && c->attr.class_ok
2783 && CLASS_DATA (c)->attr.class_pointer)
2784 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2786 pointer = true;
2787 sym->attr.pointer_comp = 1;
2790 /* Look for procedure pointer components. */
2791 if (c->attr.proc_pointer
2792 || (c->ts.type == BT_DERIVED
2793 && c->ts.u.derived->attr.proc_pointer_comp))
2794 sym->attr.proc_pointer_comp = 1;
2796 /* Looking for coarray components. */
2797 if (c->attr.codimension
2798 || (c->ts.type == BT_CLASS && c->attr.class_ok
2799 && CLASS_DATA (c)->attr.codimension))
2801 coarray = true;
2802 sym->attr.coarray_comp = 1;
2805 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
2806 && !c->attr.pointer)
2808 coarray = true;
2809 sym->attr.coarray_comp = 1;
2812 /* Looking for lock_type components. */
2813 if ((c->ts.type == BT_DERIVED
2814 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2815 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2816 || (c->ts.type == BT_CLASS && c->attr.class_ok
2817 && CLASS_DATA (c)->ts.u.derived->from_intmod
2818 == INTMOD_ISO_FORTRAN_ENV
2819 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2820 == ISOFORTRAN_LOCK_TYPE)
2821 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
2822 && !allocatable && !pointer))
2824 lock_type = 1;
2825 lock_comp = c;
2826 sym->attr.lock_comp = 1;
2829 /* Looking for event_type components. */
2830 if ((c->ts.type == BT_DERIVED
2831 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2832 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2833 || (c->ts.type == BT_CLASS && c->attr.class_ok
2834 && CLASS_DATA (c)->ts.u.derived->from_intmod
2835 == INTMOD_ISO_FORTRAN_ENV
2836 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2837 == ISOFORTRAN_EVENT_TYPE)
2838 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.event_comp
2839 && !allocatable && !pointer))
2841 event_type = 1;
2842 event_comp = c;
2843 sym->attr.event_comp = 1;
2846 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2847 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2848 unless there are nondirect [allocatable or pointer] components
2849 involved (cf. 1.3.33.1 and 1.3.33.3). */
2851 if (pointer && !coarray && lock_type)
2852 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2853 "codimension or be a subcomponent of a coarray, "
2854 "which is not possible as the component has the "
2855 "pointer attribute", c->name, &c->loc);
2856 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2857 && c->ts.u.derived->attr.lock_comp)
2858 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2859 "of type LOCK_TYPE, which must have a codimension or be a "
2860 "subcomponent of a coarray", c->name, &c->loc);
2862 if (lock_type && allocatable && !coarray)
2863 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2864 "a codimension", c->name, &c->loc);
2865 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
2866 && c->ts.u.derived->attr.lock_comp)
2867 gfc_error ("Allocatable component %s at %L must have a codimension as "
2868 "it has a noncoarray subcomponent of type LOCK_TYPE",
2869 c->name, &c->loc);
2871 if (sym->attr.coarray_comp && !coarray && lock_type)
2872 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2873 "subcomponent of type LOCK_TYPE must have a codimension or "
2874 "be a subcomponent of a coarray. (Variables of type %s may "
2875 "not have a codimension as already a coarray "
2876 "subcomponent exists)", c->name, &c->loc, sym->name);
2878 if (sym->attr.lock_comp && coarray && !lock_type)
2879 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2880 "subcomponent of type LOCK_TYPE must have a codimension or "
2881 "be a subcomponent of a coarray. (Variables of type %s may "
2882 "not have a codimension as %s at %L has a codimension or a "
2883 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
2884 sym->name, c->name, &c->loc);
2886 /* Similarly for EVENT TYPE. */
2888 if (pointer && !coarray && event_type)
2889 gfc_error ("Component %s at %L of type EVENT_TYPE must have a "
2890 "codimension or be a subcomponent of a coarray, "
2891 "which is not possible as the component has the "
2892 "pointer attribute", c->name, &c->loc);
2893 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2894 && c->ts.u.derived->attr.event_comp)
2895 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2896 "of type EVENT_TYPE, which must have a codimension or be a "
2897 "subcomponent of a coarray", c->name, &c->loc);
2899 if (event_type && allocatable && !coarray)
2900 gfc_error ("Allocatable component %s at %L of type EVENT_TYPE must have "
2901 "a codimension", c->name, &c->loc);
2902 else if (event_type && allocatable && c->ts.type == BT_DERIVED
2903 && c->ts.u.derived->attr.event_comp)
2904 gfc_error ("Allocatable component %s at %L must have a codimension as "
2905 "it has a noncoarray subcomponent of type EVENT_TYPE",
2906 c->name, &c->loc);
2908 if (sym->attr.coarray_comp && !coarray && event_type)
2909 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
2910 "subcomponent of type EVENT_TYPE must have a codimension or "
2911 "be a subcomponent of a coarray. (Variables of type %s may "
2912 "not have a codimension as already a coarray "
2913 "subcomponent exists)", c->name, &c->loc, sym->name);
2915 if (sym->attr.event_comp && coarray && !event_type)
2916 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
2917 "subcomponent of type EVENT_TYPE must have a codimension or "
2918 "be a subcomponent of a coarray. (Variables of type %s may "
2919 "not have a codimension as %s at %L has a codimension or a "
2920 "coarray subcomponent)", event_comp->name, &event_comp->loc,
2921 sym->name, c->name, &c->loc);
2923 /* Look for private components. */
2924 if (sym->component_access == ACCESS_PRIVATE
2925 || c->attr.access == ACCESS_PRIVATE
2926 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
2927 sym->attr.private_comp = 1;
2929 if (lockp) *lockp = lock_comp;
2930 if (eventp) *eventp = event_comp;
2934 static void parse_struct_map (gfc_statement);
2936 /* Parse a union component definition within a structure definition. */
2938 static void
2939 parse_union (void)
2941 int compiling;
2942 gfc_statement st;
2943 gfc_state_data s;
2944 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
2945 gfc_symbol *un;
2947 accept_statement(ST_UNION);
2948 push_state (&s, COMP_UNION, gfc_new_block);
2949 un = gfc_new_block;
2951 compiling = 1;
2953 while (compiling)
2955 st = next_statement ();
2956 /* Only MAP declarations valid within a union. */
2957 switch (st)
2959 case ST_NONE:
2960 unexpected_eof ();
2962 case ST_MAP:
2963 accept_statement (ST_MAP);
2964 parse_struct_map (ST_MAP);
2965 /* Add a component to the union for each map. */
2966 if (!gfc_add_component (un, gfc_new_block->name, &c))
2968 gfc_internal_error ("failed to create map component '%s'",
2969 gfc_new_block->name);
2970 reject_statement ();
2971 return;
2973 c->ts.type = BT_DERIVED;
2974 c->ts.u.derived = gfc_new_block;
2975 /* Normally components get their initialization expressions when they
2976 are created in decl.c (build_struct) so we can look through the
2977 flat component list for initializers during resolution. Unions and
2978 maps create components along with their type definitions so we
2979 have to generate initializers here. */
2980 c->initializer = gfc_default_initializer (&c->ts);
2981 break;
2983 case ST_END_UNION:
2984 compiling = 0;
2985 accept_statement (ST_END_UNION);
2986 break;
2988 default:
2989 unexpected_statement (st);
2990 break;
2994 for (c = un->components; c; c = c->next)
2995 check_component (un, c, &lock_comp, &event_comp);
2997 /* Add the union as a component in its parent structure. */
2998 pop_state ();
2999 if (!gfc_add_component (gfc_current_block (), un->name, &c))
3001 gfc_internal_error ("failed to create union component '%s'", un->name);
3002 reject_statement ();
3003 return;
3005 c->ts.type = BT_UNION;
3006 c->ts.u.derived = un;
3007 c->initializer = gfc_default_initializer (&c->ts);
3009 un->attr.zero_comp = un->components == NULL;
3013 /* Parse a STRUCTURE or MAP. */
3015 static void
3016 parse_struct_map (gfc_statement block)
3018 int compiling_type;
3019 gfc_statement st;
3020 gfc_state_data s;
3021 gfc_symbol *sym;
3022 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3023 gfc_compile_state comp;
3024 gfc_statement ends;
3026 if (block == ST_STRUCTURE_DECL)
3028 comp = COMP_STRUCTURE;
3029 ends = ST_END_STRUCTURE;
3031 else
3033 gcc_assert (block == ST_MAP);
3034 comp = COMP_MAP;
3035 ends = ST_END_MAP;
3038 accept_statement(block);
3039 push_state (&s, comp, gfc_new_block);
3041 gfc_new_block->component_access = ACCESS_PUBLIC;
3042 compiling_type = 1;
3044 while (compiling_type)
3046 st = next_statement ();
3047 switch (st)
3049 case ST_NONE:
3050 unexpected_eof ();
3052 /* Nested structure declarations will be captured as ST_DATA_DECL. */
3053 case ST_STRUCTURE_DECL:
3054 /* Let a more specific error make it to decode_statement(). */
3055 if (gfc_error_check () == 0)
3056 gfc_error ("Syntax error in nested structure declaration at %C");
3057 reject_statement ();
3058 /* Skip the rest of this statement. */
3059 gfc_error_recovery ();
3060 break;
3062 case ST_UNION:
3063 accept_statement (ST_UNION);
3064 parse_union ();
3065 break;
3067 case ST_DATA_DECL:
3068 /* The data declaration was a nested/ad-hoc STRUCTURE field. */
3069 accept_statement (ST_DATA_DECL);
3070 if (gfc_new_block && gfc_new_block != gfc_current_block ()
3071 && gfc_new_block->attr.flavor == FL_STRUCT)
3072 parse_struct_map (ST_STRUCTURE_DECL);
3073 break;
3075 case ST_END_STRUCTURE:
3076 case ST_END_MAP:
3077 if (st == ends)
3079 accept_statement (st);
3080 compiling_type = 0;
3082 else
3083 unexpected_statement (st);
3084 break;
3086 default:
3087 unexpected_statement (st);
3088 break;
3092 /* Validate each component. */
3093 sym = gfc_current_block ();
3094 for (c = sym->components; c; c = c->next)
3095 check_component (sym, c, &lock_comp, &event_comp);
3097 sym->attr.zero_comp = (sym->components == NULL);
3099 /* Allow parse_union to find this structure to add to its list of maps. */
3100 if (block == ST_MAP)
3101 gfc_new_block = gfc_current_block ();
3103 pop_state ();
3107 /* Parse a derived type. */
3109 static void
3110 parse_derived (void)
3112 int compiling_type, seen_private, seen_sequence, seen_component;
3113 gfc_statement st;
3114 gfc_state_data s;
3115 gfc_symbol *sym;
3116 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3118 accept_statement (ST_DERIVED_DECL);
3119 push_state (&s, COMP_DERIVED, gfc_new_block);
3121 gfc_new_block->component_access = ACCESS_PUBLIC;
3122 seen_private = 0;
3123 seen_sequence = 0;
3124 seen_component = 0;
3126 compiling_type = 1;
3128 while (compiling_type)
3130 st = next_statement ();
3131 switch (st)
3133 case ST_NONE:
3134 unexpected_eof ();
3136 case ST_DATA_DECL:
3137 case ST_PROCEDURE:
3138 accept_statement (st);
3139 seen_component = 1;
3140 break;
3142 case ST_FINAL:
3143 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
3144 break;
3146 case ST_END_TYPE:
3147 endType:
3148 compiling_type = 0;
3150 if (!seen_component)
3151 gfc_notify_std (GFC_STD_F2003, "Derived type "
3152 "definition at %C without components");
3154 accept_statement (ST_END_TYPE);
3155 break;
3157 case ST_PRIVATE:
3158 if (!gfc_find_state (COMP_MODULE))
3160 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
3161 "a MODULE");
3162 break;
3165 if (seen_component)
3167 gfc_error ("PRIVATE statement at %C must precede "
3168 "structure components");
3169 break;
3172 if (seen_private)
3173 gfc_error ("Duplicate PRIVATE statement at %C");
3175 s.sym->component_access = ACCESS_PRIVATE;
3177 accept_statement (ST_PRIVATE);
3178 seen_private = 1;
3179 break;
3181 case ST_SEQUENCE:
3182 if (seen_component)
3184 gfc_error ("SEQUENCE statement at %C must precede "
3185 "structure components");
3186 break;
3189 if (gfc_current_block ()->attr.sequence)
3190 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
3191 "TYPE statement");
3193 if (seen_sequence)
3195 gfc_error ("Duplicate SEQUENCE statement at %C");
3198 seen_sequence = 1;
3199 gfc_add_sequence (&gfc_current_block ()->attr,
3200 gfc_current_block ()->name, NULL);
3201 break;
3203 case ST_CONTAINS:
3204 gfc_notify_std (GFC_STD_F2003,
3205 "CONTAINS block in derived type"
3206 " definition at %C");
3208 accept_statement (ST_CONTAINS);
3209 parse_derived_contains ();
3210 goto endType;
3212 default:
3213 unexpected_statement (st);
3214 break;
3218 /* need to verify that all fields of the derived type are
3219 * interoperable with C if the type is declared to be bind(c)
3221 sym = gfc_current_block ();
3222 for (c = sym->components; c; c = c->next)
3223 check_component (sym, c, &lock_comp, &event_comp);
3225 if (!seen_component)
3226 sym->attr.zero_comp = 1;
3228 pop_state ();
3232 /* Parse an ENUM. */
3234 static void
3235 parse_enum (void)
3237 gfc_statement st;
3238 int compiling_enum;
3239 gfc_state_data s;
3240 int seen_enumerator = 0;
3242 push_state (&s, COMP_ENUM, gfc_new_block);
3244 compiling_enum = 1;
3246 while (compiling_enum)
3248 st = next_statement ();
3249 switch (st)
3251 case ST_NONE:
3252 unexpected_eof ();
3253 break;
3255 case ST_ENUMERATOR:
3256 seen_enumerator = 1;
3257 accept_statement (st);
3258 break;
3260 case ST_END_ENUM:
3261 compiling_enum = 0;
3262 if (!seen_enumerator)
3263 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
3264 accept_statement (st);
3265 break;
3267 default:
3268 gfc_free_enum_history ();
3269 unexpected_statement (st);
3270 break;
3273 pop_state ();
3277 /* Parse an interface. We must be able to deal with the possibility
3278 of recursive interfaces. The parse_spec() subroutine is mutually
3279 recursive with parse_interface(). */
3281 static gfc_statement parse_spec (gfc_statement);
3283 static void
3284 parse_interface (void)
3286 gfc_compile_state new_state = COMP_NONE, current_state;
3287 gfc_symbol *prog_unit, *sym;
3288 gfc_interface_info save;
3289 gfc_state_data s1, s2;
3290 gfc_statement st;
3292 accept_statement (ST_INTERFACE);
3294 current_interface.ns = gfc_current_ns;
3295 save = current_interface;
3297 sym = (current_interface.type == INTERFACE_GENERIC
3298 || current_interface.type == INTERFACE_USER_OP)
3299 ? gfc_new_block : NULL;
3301 push_state (&s1, COMP_INTERFACE, sym);
3302 current_state = COMP_NONE;
3304 loop:
3305 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
3307 st = next_statement ();
3308 switch (st)
3310 case ST_NONE:
3311 unexpected_eof ();
3313 case ST_SUBROUTINE:
3314 case ST_FUNCTION:
3315 if (st == ST_SUBROUTINE)
3316 new_state = COMP_SUBROUTINE;
3317 else if (st == ST_FUNCTION)
3318 new_state = COMP_FUNCTION;
3319 if (gfc_new_block->attr.pointer)
3321 gfc_new_block->attr.pointer = 0;
3322 gfc_new_block->attr.proc_pointer = 1;
3324 if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
3325 gfc_new_block->formal, NULL))
3327 reject_statement ();
3328 gfc_free_namespace (gfc_current_ns);
3329 goto loop;
3331 /* F2008 C1210 forbids the IMPORT statement in module procedure
3332 interface bodies and the flag is set to import symbols. */
3333 if (gfc_new_block->attr.module_procedure)
3334 gfc_current_ns->has_import_set = 1;
3335 break;
3337 case ST_PROCEDURE:
3338 case ST_MODULE_PROC: /* The module procedure matcher makes
3339 sure the context is correct. */
3340 accept_statement (st);
3341 gfc_free_namespace (gfc_current_ns);
3342 goto loop;
3344 case ST_END_INTERFACE:
3345 gfc_free_namespace (gfc_current_ns);
3346 gfc_current_ns = current_interface.ns;
3347 goto done;
3349 default:
3350 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
3351 gfc_ascii_statement (st));
3352 reject_statement ();
3353 gfc_free_namespace (gfc_current_ns);
3354 goto loop;
3358 /* Make sure that the generic name has the right attribute. */
3359 if (current_interface.type == INTERFACE_GENERIC
3360 && current_state == COMP_NONE)
3362 if (new_state == COMP_FUNCTION && sym)
3363 gfc_add_function (&sym->attr, sym->name, NULL);
3364 else if (new_state == COMP_SUBROUTINE && sym)
3365 gfc_add_subroutine (&sym->attr, sym->name, NULL);
3367 current_state = new_state;
3370 if (current_interface.type == INTERFACE_ABSTRACT)
3372 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
3373 if (gfc_is_intrinsic_typename (gfc_new_block->name))
3374 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
3375 "cannot be the same as an intrinsic type",
3376 gfc_new_block->name);
3379 push_state (&s2, new_state, gfc_new_block);
3380 accept_statement (st);
3381 prog_unit = gfc_new_block;
3382 prog_unit->formal_ns = gfc_current_ns;
3383 if (prog_unit == prog_unit->formal_ns->proc_name
3384 && prog_unit->ns != prog_unit->formal_ns)
3385 prog_unit->refs++;
3387 decl:
3388 /* Read data declaration statements. */
3389 st = parse_spec (ST_NONE);
3390 in_specification_block = true;
3392 /* Since the interface block does not permit an IMPLICIT statement,
3393 the default type for the function or the result must be taken
3394 from the formal namespace. */
3395 if (new_state == COMP_FUNCTION)
3397 if (prog_unit->result == prog_unit
3398 && prog_unit->ts.type == BT_UNKNOWN)
3399 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
3400 else if (prog_unit->result != prog_unit
3401 && prog_unit->result->ts.type == BT_UNKNOWN)
3402 gfc_set_default_type (prog_unit->result, 1,
3403 prog_unit->formal_ns);
3406 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
3408 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3409 gfc_ascii_statement (st));
3410 reject_statement ();
3411 goto decl;
3414 /* Add EXTERNAL attribute to function or subroutine. */
3415 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
3416 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
3418 current_interface = save;
3419 gfc_add_interface (prog_unit);
3420 pop_state ();
3422 if (current_interface.ns
3423 && current_interface.ns->proc_name
3424 && strcmp (current_interface.ns->proc_name->name,
3425 prog_unit->name) == 0)
3426 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3427 "enclosing procedure", prog_unit->name,
3428 &current_interface.ns->proc_name->declared_at);
3430 goto loop;
3432 done:
3433 pop_state ();
3437 /* Associate function characteristics by going back to the function
3438 declaration and rematching the prefix. */
3440 static match
3441 match_deferred_characteristics (gfc_typespec * ts)
3443 locus loc;
3444 match m = MATCH_ERROR;
3445 char name[GFC_MAX_SYMBOL_LEN + 1];
3447 loc = gfc_current_locus;
3449 gfc_current_locus = gfc_current_block ()->declared_at;
3451 gfc_clear_error ();
3452 gfc_buffer_error (true);
3453 m = gfc_match_prefix (ts);
3454 gfc_buffer_error (false);
3456 if (ts->type == BT_DERIVED)
3458 ts->kind = 0;
3460 if (!ts->u.derived)
3461 m = MATCH_ERROR;
3464 /* Only permit one go at the characteristic association. */
3465 if (ts->kind == -1)
3466 ts->kind = 0;
3468 /* Set the function locus correctly. If we have not found the
3469 function name, there is an error. */
3470 if (m == MATCH_YES
3471 && gfc_match ("function% %n", name) == MATCH_YES
3472 && strcmp (name, gfc_current_block ()->name) == 0)
3474 gfc_current_block ()->declared_at = gfc_current_locus;
3475 gfc_commit_symbols ();
3477 else
3479 gfc_error_check ();
3480 gfc_undo_symbols ();
3483 gfc_current_locus =loc;
3484 return m;
3488 /* Check specification-expressions in the function result of the currently
3489 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3490 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3491 scope are not yet parsed so this has to be delayed up to parse_spec. */
3493 static void
3494 check_function_result_typed (void)
3496 gfc_typespec ts;
3498 gcc_assert (gfc_current_state () == COMP_FUNCTION);
3500 if (!gfc_current_ns->proc_name->result) return;
3502 ts = gfc_current_ns->proc_name->result->ts;
3504 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3505 /* TODO: Extend when KIND type parameters are implemented. */
3506 if (ts.type == BT_CHARACTER && ts.u.cl && ts.u.cl->length)
3507 gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true);
3511 /* Parse a set of specification statements. Returns the statement
3512 that doesn't fit. */
3514 static gfc_statement
3515 parse_spec (gfc_statement st)
3517 st_state ss;
3518 bool function_result_typed = false;
3519 bool bad_characteristic = false;
3520 gfc_typespec *ts;
3522 in_specification_block = true;
3524 verify_st_order (&ss, ST_NONE, false);
3525 if (st == ST_NONE)
3526 st = next_statement ();
3528 /* If we are not inside a function or don't have a result specified so far,
3529 do nothing special about it. */
3530 if (gfc_current_state () != COMP_FUNCTION)
3531 function_result_typed = true;
3532 else
3534 gfc_symbol* proc = gfc_current_ns->proc_name;
3535 gcc_assert (proc);
3537 if (proc->result->ts.type == BT_UNKNOWN)
3538 function_result_typed = true;
3541 loop:
3543 /* If we're inside a BLOCK construct, some statements are disallowed.
3544 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3545 or VALUE are also disallowed, but they don't have a particular ST_*
3546 key so we have to check for them individually in their matcher routine. */
3547 if (gfc_current_state () == COMP_BLOCK)
3548 switch (st)
3550 case ST_IMPLICIT:
3551 case ST_IMPLICIT_NONE:
3552 case ST_NAMELIST:
3553 case ST_COMMON:
3554 case ST_EQUIVALENCE:
3555 case ST_STATEMENT_FUNCTION:
3556 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3557 gfc_ascii_statement (st));
3558 reject_statement ();
3559 break;
3561 default:
3562 break;
3564 else if (gfc_current_state () == COMP_BLOCK_DATA)
3565 /* Fortran 2008, C1116. */
3566 switch (st)
3568 case ST_DATA_DECL:
3569 case ST_COMMON:
3570 case ST_DATA:
3571 case ST_TYPE:
3572 case ST_END_BLOCK_DATA:
3573 case ST_ATTR_DECL:
3574 case ST_EQUIVALENCE:
3575 case ST_PARAMETER:
3576 case ST_IMPLICIT:
3577 case ST_IMPLICIT_NONE:
3578 case ST_DERIVED_DECL:
3579 case ST_USE:
3580 break;
3582 case ST_NONE:
3583 break;
3585 default:
3586 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3587 gfc_ascii_statement (st));
3588 reject_statement ();
3589 break;
3592 /* If we find a statement that can not be followed by an IMPLICIT statement
3593 (and thus we can expect to see none any further), type the function result
3594 if it has not yet been typed. Be careful not to give the END statement
3595 to verify_st_order! */
3596 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
3598 bool verify_now = false;
3600 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
3601 verify_now = true;
3602 else
3604 st_state dummyss;
3605 verify_st_order (&dummyss, ST_NONE, false);
3606 verify_st_order (&dummyss, st, false);
3608 if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
3609 verify_now = true;
3612 if (verify_now)
3614 check_function_result_typed ();
3615 function_result_typed = true;
3619 switch (st)
3621 case ST_NONE:
3622 unexpected_eof ();
3624 case ST_IMPLICIT_NONE:
3625 case ST_IMPLICIT:
3626 if (!function_result_typed)
3628 check_function_result_typed ();
3629 function_result_typed = true;
3631 goto declSt;
3633 case ST_FORMAT:
3634 case ST_ENTRY:
3635 case ST_DATA: /* Not allowed in interfaces */
3636 if (gfc_current_state () == COMP_INTERFACE)
3637 break;
3639 /* Fall through */
3641 case ST_USE:
3642 case ST_IMPORT:
3643 case ST_PARAMETER:
3644 case ST_PUBLIC:
3645 case ST_PRIVATE:
3646 case ST_STRUCTURE_DECL:
3647 case ST_DERIVED_DECL:
3648 case_decl:
3649 case_omp_decl:
3650 declSt:
3651 if (!verify_st_order (&ss, st, false))
3653 reject_statement ();
3654 st = next_statement ();
3655 goto loop;
3658 switch (st)
3660 case ST_INTERFACE:
3661 parse_interface ();
3662 break;
3664 case ST_STRUCTURE_DECL:
3665 parse_struct_map (ST_STRUCTURE_DECL);
3666 break;
3668 case ST_DERIVED_DECL:
3669 parse_derived ();
3670 break;
3672 case ST_PUBLIC:
3673 case ST_PRIVATE:
3674 if (gfc_current_state () != COMP_MODULE)
3676 gfc_error ("%s statement must appear in a MODULE",
3677 gfc_ascii_statement (st));
3678 reject_statement ();
3679 break;
3682 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
3684 gfc_error ("%s statement at %C follows another accessibility "
3685 "specification", gfc_ascii_statement (st));
3686 reject_statement ();
3687 break;
3690 gfc_current_ns->default_access = (st == ST_PUBLIC)
3691 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3693 break;
3695 case ST_STATEMENT_FUNCTION:
3696 if (gfc_current_state () == COMP_MODULE
3697 || gfc_current_state () == COMP_SUBMODULE)
3699 unexpected_statement (st);
3700 break;
3703 default:
3704 break;
3707 accept_statement (st);
3708 st = next_statement ();
3709 goto loop;
3711 case ST_ENUM:
3712 accept_statement (st);
3713 parse_enum();
3714 st = next_statement ();
3715 goto loop;
3717 case ST_GET_FCN_CHARACTERISTICS:
3718 /* This statement triggers the association of a function's result
3719 characteristics. */
3720 ts = &gfc_current_block ()->result->ts;
3721 if (match_deferred_characteristics (ts) != MATCH_YES)
3722 bad_characteristic = true;
3724 st = next_statement ();
3725 goto loop;
3727 default:
3728 break;
3731 /* If match_deferred_characteristics failed, then there is an error. */
3732 if (bad_characteristic)
3734 ts = &gfc_current_block ()->result->ts;
3735 if (ts->type != BT_DERIVED)
3736 gfc_error ("Bad kind expression for function %qs at %L",
3737 gfc_current_block ()->name,
3738 &gfc_current_block ()->declared_at);
3739 else
3740 gfc_error ("The type for function %qs at %L is not accessible",
3741 gfc_current_block ()->name,
3742 &gfc_current_block ()->declared_at);
3744 gfc_current_block ()->ts.kind = 0;
3745 /* Keep the derived type; if it's bad, it will be discovered later. */
3746 if (!(ts->type == BT_DERIVED && ts->u.derived))
3747 ts->type = BT_UNKNOWN;
3750 in_specification_block = false;
3752 return st;
3756 /* Parse a WHERE block, (not a simple WHERE statement). */
3758 static void
3759 parse_where_block (void)
3761 int seen_empty_else;
3762 gfc_code *top, *d;
3763 gfc_state_data s;
3764 gfc_statement st;
3766 accept_statement (ST_WHERE_BLOCK);
3767 top = gfc_state_stack->tail;
3769 push_state (&s, COMP_WHERE, gfc_new_block);
3771 d = add_statement ();
3772 d->expr1 = top->expr1;
3773 d->op = EXEC_WHERE;
3775 top->expr1 = NULL;
3776 top->block = d;
3778 seen_empty_else = 0;
3782 st = next_statement ();
3783 switch (st)
3785 case ST_NONE:
3786 unexpected_eof ();
3788 case ST_WHERE_BLOCK:
3789 parse_where_block ();
3790 break;
3792 case ST_ASSIGNMENT:
3793 case ST_WHERE:
3794 accept_statement (st);
3795 break;
3797 case ST_ELSEWHERE:
3798 if (seen_empty_else)
3800 gfc_error ("ELSEWHERE statement at %C follows previous "
3801 "unmasked ELSEWHERE");
3802 reject_statement ();
3803 break;
3806 if (new_st.expr1 == NULL)
3807 seen_empty_else = 1;
3809 d = new_level (gfc_state_stack->head);
3810 d->op = EXEC_WHERE;
3811 d->expr1 = new_st.expr1;
3813 accept_statement (st);
3815 break;
3817 case ST_END_WHERE:
3818 accept_statement (st);
3819 break;
3821 default:
3822 gfc_error ("Unexpected %s statement in WHERE block at %C",
3823 gfc_ascii_statement (st));
3824 reject_statement ();
3825 break;
3828 while (st != ST_END_WHERE);
3830 pop_state ();
3834 /* Parse a FORALL block (not a simple FORALL statement). */
3836 static void
3837 parse_forall_block (void)
3839 gfc_code *top, *d;
3840 gfc_state_data s;
3841 gfc_statement st;
3843 accept_statement (ST_FORALL_BLOCK);
3844 top = gfc_state_stack->tail;
3846 push_state (&s, COMP_FORALL, gfc_new_block);
3848 d = add_statement ();
3849 d->op = EXEC_FORALL;
3850 top->block = d;
3854 st = next_statement ();
3855 switch (st)
3858 case ST_ASSIGNMENT:
3859 case ST_POINTER_ASSIGNMENT:
3860 case ST_WHERE:
3861 case ST_FORALL:
3862 accept_statement (st);
3863 break;
3865 case ST_WHERE_BLOCK:
3866 parse_where_block ();
3867 break;
3869 case ST_FORALL_BLOCK:
3870 parse_forall_block ();
3871 break;
3873 case ST_END_FORALL:
3874 accept_statement (st);
3875 break;
3877 case ST_NONE:
3878 unexpected_eof ();
3880 default:
3881 gfc_error ("Unexpected %s statement in FORALL block at %C",
3882 gfc_ascii_statement (st));
3884 reject_statement ();
3885 break;
3888 while (st != ST_END_FORALL);
3890 pop_state ();
3894 static gfc_statement parse_executable (gfc_statement);
3896 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
3898 static void
3899 parse_if_block (void)
3901 gfc_code *top, *d;
3902 gfc_statement st;
3903 locus else_locus;
3904 gfc_state_data s;
3905 int seen_else;
3907 seen_else = 0;
3908 accept_statement (ST_IF_BLOCK);
3910 top = gfc_state_stack->tail;
3911 push_state (&s, COMP_IF, gfc_new_block);
3913 new_st.op = EXEC_IF;
3914 d = add_statement ();
3916 d->expr1 = top->expr1;
3917 top->expr1 = NULL;
3918 top->block = d;
3922 st = parse_executable (ST_NONE);
3924 switch (st)
3926 case ST_NONE:
3927 unexpected_eof ();
3929 case ST_ELSEIF:
3930 if (seen_else)
3932 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
3933 "statement at %L", &else_locus);
3935 reject_statement ();
3936 break;
3939 d = new_level (gfc_state_stack->head);
3940 d->op = EXEC_IF;
3941 d->expr1 = new_st.expr1;
3943 accept_statement (st);
3945 break;
3947 case ST_ELSE:
3948 if (seen_else)
3950 gfc_error ("Duplicate ELSE statements at %L and %C",
3951 &else_locus);
3952 reject_statement ();
3953 break;
3956 seen_else = 1;
3957 else_locus = gfc_current_locus;
3959 d = new_level (gfc_state_stack->head);
3960 d->op = EXEC_IF;
3962 accept_statement (st);
3964 break;
3966 case ST_ENDIF:
3967 break;
3969 default:
3970 unexpected_statement (st);
3971 break;
3974 while (st != ST_ENDIF);
3976 pop_state ();
3977 accept_statement (st);
3981 /* Parse a SELECT block. */
3983 static void
3984 parse_select_block (void)
3986 gfc_statement st;
3987 gfc_code *cp;
3988 gfc_state_data s;
3990 accept_statement (ST_SELECT_CASE);
3992 cp = gfc_state_stack->tail;
3993 push_state (&s, COMP_SELECT, gfc_new_block);
3995 /* Make sure that the next statement is a CASE or END SELECT. */
3996 for (;;)
3998 st = next_statement ();
3999 if (st == ST_NONE)
4000 unexpected_eof ();
4001 if (st == ST_END_SELECT)
4003 /* Empty SELECT CASE is OK. */
4004 accept_statement (st);
4005 pop_state ();
4006 return;
4008 if (st == ST_CASE)
4009 break;
4011 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
4012 "CASE at %C");
4014 reject_statement ();
4017 /* At this point, we're got a nonempty select block. */
4018 cp = new_level (cp);
4019 *cp = new_st;
4021 accept_statement (st);
4025 st = parse_executable (ST_NONE);
4026 switch (st)
4028 case ST_NONE:
4029 unexpected_eof ();
4031 case ST_CASE:
4032 cp = new_level (gfc_state_stack->head);
4033 *cp = new_st;
4034 gfc_clear_new_st ();
4036 accept_statement (st);
4037 /* Fall through */
4039 case ST_END_SELECT:
4040 break;
4042 /* Can't have an executable statement because of
4043 parse_executable(). */
4044 default:
4045 unexpected_statement (st);
4046 break;
4049 while (st != ST_END_SELECT);
4051 pop_state ();
4052 accept_statement (st);
4056 /* Pop the current selector from the SELECT TYPE stack. */
4058 static void
4059 select_type_pop (void)
4061 gfc_select_type_stack *old = select_type_stack;
4062 select_type_stack = old->prev;
4063 free (old);
4067 /* Parse a SELECT TYPE construct (F03:R821). */
4069 static void
4070 parse_select_type_block (void)
4072 gfc_statement st;
4073 gfc_code *cp;
4074 gfc_state_data s;
4076 accept_statement (ST_SELECT_TYPE);
4078 cp = gfc_state_stack->tail;
4079 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
4081 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
4082 or END SELECT. */
4083 for (;;)
4085 st = next_statement ();
4086 if (st == ST_NONE)
4087 unexpected_eof ();
4088 if (st == ST_END_SELECT)
4089 /* Empty SELECT CASE is OK. */
4090 goto done;
4091 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
4092 break;
4094 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
4095 "following SELECT TYPE at %C");
4097 reject_statement ();
4100 /* At this point, we're got a nonempty select block. */
4101 cp = new_level (cp);
4102 *cp = new_st;
4104 accept_statement (st);
4108 st = parse_executable (ST_NONE);
4109 switch (st)
4111 case ST_NONE:
4112 unexpected_eof ();
4114 case ST_TYPE_IS:
4115 case ST_CLASS_IS:
4116 cp = new_level (gfc_state_stack->head);
4117 *cp = new_st;
4118 gfc_clear_new_st ();
4120 accept_statement (st);
4121 /* Fall through */
4123 case ST_END_SELECT:
4124 break;
4126 /* Can't have an executable statement because of
4127 parse_executable(). */
4128 default:
4129 unexpected_statement (st);
4130 break;
4133 while (st != ST_END_SELECT);
4135 done:
4136 pop_state ();
4137 accept_statement (st);
4138 gfc_current_ns = gfc_current_ns->parent;
4139 select_type_pop ();
4143 /* Given a symbol, make sure it is not an iteration variable for a DO
4144 statement. This subroutine is called when the symbol is seen in a
4145 context that causes it to become redefined. If the symbol is an
4146 iterator, we generate an error message and return nonzero. */
4149 gfc_check_do_variable (gfc_symtree *st)
4151 gfc_state_data *s;
4153 for (s=gfc_state_stack; s; s = s->previous)
4154 if (s->do_variable == st)
4156 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
4157 "loop beginning at %L", st->name, &s->head->loc);
4158 return 1;
4161 return 0;
4165 /* Checks to see if the current statement label closes an enddo.
4166 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
4167 an error) if it incorrectly closes an ENDDO. */
4169 static int
4170 check_do_closure (void)
4172 gfc_state_data *p;
4174 if (gfc_statement_label == NULL)
4175 return 0;
4177 for (p = gfc_state_stack; p; p = p->previous)
4178 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
4179 break;
4181 if (p == NULL)
4182 return 0; /* No loops to close */
4184 if (p->ext.end_do_label == gfc_statement_label)
4186 if (p == gfc_state_stack)
4187 return 1;
4189 gfc_error ("End of nonblock DO statement at %C is within another block");
4190 return 2;
4193 /* At this point, the label doesn't terminate the innermost loop.
4194 Make sure it doesn't terminate another one. */
4195 for (; p; p = p->previous)
4196 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
4197 && p->ext.end_do_label == gfc_statement_label)
4199 gfc_error ("End of nonblock DO statement at %C is interwoven "
4200 "with another DO loop");
4201 return 2;
4204 return 0;
4208 /* Parse a series of contained program units. */
4210 static void parse_progunit (gfc_statement);
4213 /* Parse a CRITICAL block. */
4215 static void
4216 parse_critical_block (void)
4218 gfc_code *top, *d;
4219 gfc_state_data s, *sd;
4220 gfc_statement st;
4222 for (sd = gfc_state_stack; sd; sd = sd->previous)
4223 if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
4224 gfc_error_now (is_oacc (sd)
4225 ? "CRITICAL block inside of OpenACC region at %C"
4226 : "CRITICAL block inside of OpenMP region at %C");
4228 s.ext.end_do_label = new_st.label1;
4230 accept_statement (ST_CRITICAL);
4231 top = gfc_state_stack->tail;
4233 push_state (&s, COMP_CRITICAL, gfc_new_block);
4235 d = add_statement ();
4236 d->op = EXEC_CRITICAL;
4237 top->block = d;
4241 st = parse_executable (ST_NONE);
4243 switch (st)
4245 case ST_NONE:
4246 unexpected_eof ();
4247 break;
4249 case ST_END_CRITICAL:
4250 if (s.ext.end_do_label != NULL
4251 && s.ext.end_do_label != gfc_statement_label)
4252 gfc_error_now ("Statement label in END CRITICAL at %C does not "
4253 "match CRITICAL label");
4255 if (gfc_statement_label != NULL)
4257 new_st.op = EXEC_NOP;
4258 add_statement ();
4260 break;
4262 default:
4263 unexpected_statement (st);
4264 break;
4267 while (st != ST_END_CRITICAL);
4269 pop_state ();
4270 accept_statement (st);
4274 /* Set up the local namespace for a BLOCK construct. */
4276 gfc_namespace*
4277 gfc_build_block_ns (gfc_namespace *parent_ns)
4279 gfc_namespace* my_ns;
4280 static int numblock = 1;
4282 my_ns = gfc_get_namespace (parent_ns, 1);
4283 my_ns->construct_entities = 1;
4285 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
4286 code generation (so it must not be NULL).
4287 We set its recursive argument if our container procedure is recursive, so
4288 that local variables are accordingly placed on the stack when it
4289 will be necessary. */
4290 if (gfc_new_block)
4291 my_ns->proc_name = gfc_new_block;
4292 else
4294 bool t;
4295 char buffer[20]; /* Enough to hold "block@2147483648\n". */
4297 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
4298 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
4299 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
4300 my_ns->proc_name->name, NULL);
4301 gcc_assert (t);
4302 gfc_commit_symbol (my_ns->proc_name);
4305 if (parent_ns->proc_name)
4306 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
4308 return my_ns;
4312 /* Parse a BLOCK construct. */
4314 static void
4315 parse_block_construct (void)
4317 gfc_namespace* my_ns;
4318 gfc_namespace* my_parent;
4319 gfc_state_data s;
4321 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
4323 my_ns = gfc_build_block_ns (gfc_current_ns);
4325 new_st.op = EXEC_BLOCK;
4326 new_st.ext.block.ns = my_ns;
4327 new_st.ext.block.assoc = NULL;
4328 accept_statement (ST_BLOCK);
4330 push_state (&s, COMP_BLOCK, my_ns->proc_name);
4331 gfc_current_ns = my_ns;
4332 my_parent = my_ns->parent;
4334 parse_progunit (ST_NONE);
4336 /* Don't depend on the value of gfc_current_ns; it might have been
4337 reset if the block had errors and was cleaned up. */
4338 gfc_current_ns = my_parent;
4340 pop_state ();
4344 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
4345 behind the scenes with compiler-generated variables. */
4347 static void
4348 parse_associate (void)
4350 gfc_namespace* my_ns;
4351 gfc_state_data s;
4352 gfc_statement st;
4353 gfc_association_list* a;
4355 gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
4357 my_ns = gfc_build_block_ns (gfc_current_ns);
4359 new_st.op = EXEC_BLOCK;
4360 new_st.ext.block.ns = my_ns;
4361 gcc_assert (new_st.ext.block.assoc);
4363 /* Add all associate-names as BLOCK variables. Creating them is enough
4364 for now, they'll get their values during trans-* phase. */
4365 gfc_current_ns = my_ns;
4366 for (a = new_st.ext.block.assoc; a; a = a->next)
4368 gfc_symbol* sym;
4369 gfc_ref *ref;
4370 gfc_array_ref *array_ref;
4372 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
4373 gcc_unreachable ();
4375 sym = a->st->n.sym;
4376 sym->attr.flavor = FL_VARIABLE;
4377 sym->assoc = a;
4378 sym->declared_at = a->where;
4379 gfc_set_sym_referenced (sym);
4381 /* Initialize the typespec. It is not available in all cases,
4382 however, as it may only be set on the target during resolution.
4383 Still, sometimes it helps to have it right now -- especially
4384 for parsing component references on the associate-name
4385 in case of association to a derived-type. */
4386 sym->ts = a->target->ts;
4388 /* Check if the target expression is array valued. This can not always
4389 be done by looking at target.rank, because that might not have been
4390 set yet. Therefore traverse the chain of refs, looking for the last
4391 array ref and evaluate that. */
4392 array_ref = NULL;
4393 for (ref = a->target->ref; ref; ref = ref->next)
4394 if (ref->type == REF_ARRAY)
4395 array_ref = &ref->u.ar;
4396 if (array_ref || a->target->rank)
4398 gfc_array_spec *as;
4399 int dim, rank = 0;
4400 if (array_ref)
4402 a->rankguessed = 1;
4403 /* Count the dimension, that have a non-scalar extend. */
4404 for (dim = 0; dim < array_ref->dimen; ++dim)
4405 if (array_ref->dimen_type[dim] != DIMEN_ELEMENT
4406 && !(array_ref->dimen_type[dim] == DIMEN_UNKNOWN
4407 && array_ref->end[dim] == NULL
4408 && array_ref->start[dim] != NULL))
4409 ++rank;
4411 else
4412 rank = a->target->rank;
4413 /* When the rank is greater than zero then sym will be an array. */
4414 if (sym->ts.type == BT_CLASS)
4416 if ((!CLASS_DATA (sym)->as && rank != 0)
4417 || (CLASS_DATA (sym)->as
4418 && CLASS_DATA (sym)->as->rank != rank))
4420 /* Don't just (re-)set the attr and as in the sym.ts,
4421 because this modifies the target's attr and as. Copy the
4422 data and do a build_class_symbol. */
4423 symbol_attribute attr = CLASS_DATA (a->target)->attr;
4424 int corank = gfc_get_corank (a->target);
4425 gfc_typespec type;
4427 if (rank || corank)
4429 as = gfc_get_array_spec ();
4430 as->type = AS_DEFERRED;
4431 as->rank = rank;
4432 as->corank = corank;
4433 attr.dimension = rank ? 1 : 0;
4434 attr.codimension = corank ? 1 : 0;
4436 else
4438 as = NULL;
4439 attr.dimension = attr.codimension = 0;
4441 attr.class_ok = 0;
4442 type = CLASS_DATA (sym)->ts;
4443 if (!gfc_build_class_symbol (&type,
4444 &attr, &as))
4445 gcc_unreachable ();
4446 sym->ts = type;
4447 sym->ts.type = BT_CLASS;
4448 sym->attr.class_ok = 1;
4450 else
4451 sym->attr.class_ok = 1;
4453 else if ((!sym->as && rank != 0)
4454 || (sym->as && sym->as->rank != rank))
4456 as = gfc_get_array_spec ();
4457 as->type = AS_DEFERRED;
4458 as->rank = rank;
4459 as->corank = gfc_get_corank (a->target);
4460 sym->as = as;
4461 sym->attr.dimension = 1;
4462 if (as->corank)
4463 sym->attr.codimension = 1;
4468 accept_statement (ST_ASSOCIATE);
4469 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
4471 loop:
4472 st = parse_executable (ST_NONE);
4473 switch (st)
4475 case ST_NONE:
4476 unexpected_eof ();
4478 case_end:
4479 accept_statement (st);
4480 my_ns->code = gfc_state_stack->head;
4481 break;
4483 default:
4484 unexpected_statement (st);
4485 goto loop;
4488 gfc_current_ns = gfc_current_ns->parent;
4489 pop_state ();
4493 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4494 handled inside of parse_executable(), because they aren't really
4495 loop statements. */
4497 static void
4498 parse_do_block (void)
4500 gfc_statement st;
4501 gfc_code *top;
4502 gfc_state_data s;
4503 gfc_symtree *stree;
4504 gfc_exec_op do_op;
4506 do_op = new_st.op;
4507 s.ext.end_do_label = new_st.label1;
4509 if (new_st.ext.iterator != NULL)
4510 stree = new_st.ext.iterator->var->symtree;
4511 else
4512 stree = NULL;
4514 accept_statement (ST_DO);
4516 top = gfc_state_stack->tail;
4517 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
4518 gfc_new_block);
4520 s.do_variable = stree;
4522 top->block = new_level (top);
4523 top->block->op = EXEC_DO;
4525 loop:
4526 st = parse_executable (ST_NONE);
4528 switch (st)
4530 case ST_NONE:
4531 unexpected_eof ();
4533 case ST_ENDDO:
4534 if (s.ext.end_do_label != NULL
4535 && s.ext.end_do_label != gfc_statement_label)
4536 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4537 "DO label");
4539 if (gfc_statement_label != NULL)
4541 new_st.op = EXEC_NOP;
4542 add_statement ();
4544 break;
4546 case ST_IMPLIED_ENDDO:
4547 /* If the do-stmt of this DO construct has a do-construct-name,
4548 the corresponding end-do must be an end-do-stmt (with a matching
4549 name, but in that case we must have seen ST_ENDDO first).
4550 We only complain about this in pedantic mode. */
4551 if (gfc_current_block () != NULL)
4552 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4553 &gfc_current_block()->declared_at);
4555 break;
4557 default:
4558 unexpected_statement (st);
4559 goto loop;
4562 pop_state ();
4563 accept_statement (st);
4567 /* Parse the statements of OpenMP do/parallel do. */
4569 static gfc_statement
4570 parse_omp_do (gfc_statement omp_st)
4572 gfc_statement st;
4573 gfc_code *cp, *np;
4574 gfc_state_data s;
4576 accept_statement (omp_st);
4578 cp = gfc_state_stack->tail;
4579 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4580 np = new_level (cp);
4581 np->op = cp->op;
4582 np->block = NULL;
4584 for (;;)
4586 st = next_statement ();
4587 if (st == ST_NONE)
4588 unexpected_eof ();
4589 else if (st == ST_DO)
4590 break;
4591 else
4592 unexpected_statement (st);
4595 parse_do_block ();
4596 if (gfc_statement_label != NULL
4597 && gfc_state_stack->previous != NULL
4598 && gfc_state_stack->previous->state == COMP_DO
4599 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4601 /* In
4602 DO 100 I=1,10
4603 !$OMP DO
4604 DO J=1,10
4606 100 CONTINUE
4607 there should be no !$OMP END DO. */
4608 pop_state ();
4609 return ST_IMPLIED_ENDDO;
4612 check_do_closure ();
4613 pop_state ();
4615 st = next_statement ();
4616 gfc_statement omp_end_st = ST_OMP_END_DO;
4617 switch (omp_st)
4619 case ST_OMP_DISTRIBUTE: omp_end_st = ST_OMP_END_DISTRIBUTE; break;
4620 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4621 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4622 break;
4623 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4624 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4625 break;
4626 case ST_OMP_DISTRIBUTE_SIMD:
4627 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4628 break;
4629 case ST_OMP_DO: omp_end_st = ST_OMP_END_DO; break;
4630 case ST_OMP_DO_SIMD: omp_end_st = ST_OMP_END_DO_SIMD; break;
4631 case ST_OMP_PARALLEL_DO: omp_end_st = ST_OMP_END_PARALLEL_DO; break;
4632 case ST_OMP_PARALLEL_DO_SIMD:
4633 omp_end_st = ST_OMP_END_PARALLEL_DO_SIMD;
4634 break;
4635 case ST_OMP_SIMD: omp_end_st = ST_OMP_END_SIMD; break;
4636 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4637 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4638 break;
4639 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4640 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4641 break;
4642 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4643 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4644 break;
4645 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4646 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4647 break;
4648 case ST_OMP_TEAMS_DISTRIBUTE:
4649 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4650 break;
4651 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4652 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4653 break;
4654 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4655 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4656 break;
4657 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4658 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4659 break;
4660 default: gcc_unreachable ();
4662 if (st == omp_end_st)
4664 if (new_st.op == EXEC_OMP_END_NOWAIT)
4665 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4666 else
4667 gcc_assert (new_st.op == EXEC_NOP);
4668 gfc_clear_new_st ();
4669 gfc_commit_symbols ();
4670 gfc_warning_check ();
4671 st = next_statement ();
4673 return st;
4677 /* Parse the statements of OpenMP atomic directive. */
4679 static gfc_statement
4680 parse_omp_oacc_atomic (bool omp_p)
4682 gfc_statement st, st_atomic, st_end_atomic;
4683 gfc_code *cp, *np;
4684 gfc_state_data s;
4685 int count;
4687 if (omp_p)
4689 st_atomic = ST_OMP_ATOMIC;
4690 st_end_atomic = ST_OMP_END_ATOMIC;
4692 else
4694 st_atomic = ST_OACC_ATOMIC;
4695 st_end_atomic = ST_OACC_END_ATOMIC;
4697 accept_statement (st_atomic);
4699 cp = gfc_state_stack->tail;
4700 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4701 np = new_level (cp);
4702 np->op = cp->op;
4703 np->block = NULL;
4704 count = 1 + ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4705 == GFC_OMP_ATOMIC_CAPTURE);
4707 while (count)
4709 st = next_statement ();
4710 if (st == ST_NONE)
4711 unexpected_eof ();
4712 else if (st == ST_ASSIGNMENT)
4714 accept_statement (st);
4715 count--;
4717 else
4718 unexpected_statement (st);
4721 pop_state ();
4723 st = next_statement ();
4724 if (st == st_end_atomic)
4726 gfc_clear_new_st ();
4727 gfc_commit_symbols ();
4728 gfc_warning_check ();
4729 st = next_statement ();
4731 else if ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4732 == GFC_OMP_ATOMIC_CAPTURE)
4733 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4734 return st;
4738 /* Parse the statements of an OpenACC structured block. */
4740 static void
4741 parse_oacc_structured_block (gfc_statement acc_st)
4743 gfc_statement st, acc_end_st;
4744 gfc_code *cp, *np;
4745 gfc_state_data s, *sd;
4747 for (sd = gfc_state_stack; sd; sd = sd->previous)
4748 if (sd->state == COMP_CRITICAL)
4749 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4751 accept_statement (acc_st);
4753 cp = gfc_state_stack->tail;
4754 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4755 np = new_level (cp);
4756 np->op = cp->op;
4757 np->block = NULL;
4758 switch (acc_st)
4760 case ST_OACC_PARALLEL:
4761 acc_end_st = ST_OACC_END_PARALLEL;
4762 break;
4763 case ST_OACC_KERNELS:
4764 acc_end_st = ST_OACC_END_KERNELS;
4765 break;
4766 case ST_OACC_DATA:
4767 acc_end_st = ST_OACC_END_DATA;
4768 break;
4769 case ST_OACC_HOST_DATA:
4770 acc_end_st = ST_OACC_END_HOST_DATA;
4771 break;
4772 default:
4773 gcc_unreachable ();
4778 st = parse_executable (ST_NONE);
4779 if (st == ST_NONE)
4780 unexpected_eof ();
4781 else if (st != acc_end_st)
4783 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
4784 reject_statement ();
4787 while (st != acc_end_st);
4789 gcc_assert (new_st.op == EXEC_NOP);
4791 gfc_clear_new_st ();
4792 gfc_commit_symbols ();
4793 gfc_warning_check ();
4794 pop_state ();
4797 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4799 static gfc_statement
4800 parse_oacc_loop (gfc_statement acc_st)
4802 gfc_statement st;
4803 gfc_code *cp, *np;
4804 gfc_state_data s, *sd;
4806 for (sd = gfc_state_stack; sd; sd = sd->previous)
4807 if (sd->state == COMP_CRITICAL)
4808 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4810 accept_statement (acc_st);
4812 cp = gfc_state_stack->tail;
4813 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4814 np = new_level (cp);
4815 np->op = cp->op;
4816 np->block = NULL;
4818 for (;;)
4820 st = next_statement ();
4821 if (st == ST_NONE)
4822 unexpected_eof ();
4823 else if (st == ST_DO)
4824 break;
4825 else
4827 gfc_error ("Expected DO loop at %C");
4828 reject_statement ();
4832 parse_do_block ();
4833 if (gfc_statement_label != NULL
4834 && gfc_state_stack->previous != NULL
4835 && gfc_state_stack->previous->state == COMP_DO
4836 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4838 pop_state ();
4839 return ST_IMPLIED_ENDDO;
4842 check_do_closure ();
4843 pop_state ();
4845 st = next_statement ();
4846 if (st == ST_OACC_END_LOOP)
4847 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
4848 if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
4849 (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
4850 (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
4852 gcc_assert (new_st.op == EXEC_NOP);
4853 gfc_clear_new_st ();
4854 gfc_commit_symbols ();
4855 gfc_warning_check ();
4856 st = next_statement ();
4858 return st;
4862 /* Parse the statements of an OpenMP structured block. */
4864 static void
4865 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
4867 gfc_statement st, omp_end_st;
4868 gfc_code *cp, *np;
4869 gfc_state_data s;
4871 accept_statement (omp_st);
4873 cp = gfc_state_stack->tail;
4874 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4875 np = new_level (cp);
4876 np->op = cp->op;
4877 np->block = NULL;
4879 switch (omp_st)
4881 case ST_OMP_PARALLEL:
4882 omp_end_st = ST_OMP_END_PARALLEL;
4883 break;
4884 case ST_OMP_PARALLEL_SECTIONS:
4885 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
4886 break;
4887 case ST_OMP_SECTIONS:
4888 omp_end_st = ST_OMP_END_SECTIONS;
4889 break;
4890 case ST_OMP_ORDERED:
4891 omp_end_st = ST_OMP_END_ORDERED;
4892 break;
4893 case ST_OMP_CRITICAL:
4894 omp_end_st = ST_OMP_END_CRITICAL;
4895 break;
4896 case ST_OMP_MASTER:
4897 omp_end_st = ST_OMP_END_MASTER;
4898 break;
4899 case ST_OMP_SINGLE:
4900 omp_end_st = ST_OMP_END_SINGLE;
4901 break;
4902 case ST_OMP_TARGET:
4903 omp_end_st = ST_OMP_END_TARGET;
4904 break;
4905 case ST_OMP_TARGET_DATA:
4906 omp_end_st = ST_OMP_END_TARGET_DATA;
4907 break;
4908 case ST_OMP_TARGET_TEAMS:
4909 omp_end_st = ST_OMP_END_TARGET_TEAMS;
4910 break;
4911 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4912 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4913 break;
4914 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4915 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4916 break;
4917 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4918 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4919 break;
4920 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4921 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4922 break;
4923 case ST_OMP_TASK:
4924 omp_end_st = ST_OMP_END_TASK;
4925 break;
4926 case ST_OMP_TASKGROUP:
4927 omp_end_st = ST_OMP_END_TASKGROUP;
4928 break;
4929 case ST_OMP_TEAMS:
4930 omp_end_st = ST_OMP_END_TEAMS;
4931 break;
4932 case ST_OMP_TEAMS_DISTRIBUTE:
4933 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4934 break;
4935 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4936 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4937 break;
4938 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4939 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4940 break;
4941 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4942 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4943 break;
4944 case ST_OMP_DISTRIBUTE:
4945 omp_end_st = ST_OMP_END_DISTRIBUTE;
4946 break;
4947 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4948 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4949 break;
4950 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4951 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4952 break;
4953 case ST_OMP_DISTRIBUTE_SIMD:
4954 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4955 break;
4956 case ST_OMP_WORKSHARE:
4957 omp_end_st = ST_OMP_END_WORKSHARE;
4958 break;
4959 case ST_OMP_PARALLEL_WORKSHARE:
4960 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
4961 break;
4962 default:
4963 gcc_unreachable ();
4968 if (workshare_stmts_only)
4970 /* Inside of !$omp workshare, only
4971 scalar assignments
4972 array assignments
4973 where statements and constructs
4974 forall statements and constructs
4975 !$omp atomic
4976 !$omp critical
4977 !$omp parallel
4978 are allowed. For !$omp critical these
4979 restrictions apply recursively. */
4980 bool cycle = true;
4982 st = next_statement ();
4983 for (;;)
4985 switch (st)
4987 case ST_NONE:
4988 unexpected_eof ();
4990 case ST_ASSIGNMENT:
4991 case ST_WHERE:
4992 case ST_FORALL:
4993 accept_statement (st);
4994 break;
4996 case ST_WHERE_BLOCK:
4997 parse_where_block ();
4998 break;
5000 case ST_FORALL_BLOCK:
5001 parse_forall_block ();
5002 break;
5004 case ST_OMP_PARALLEL:
5005 case ST_OMP_PARALLEL_SECTIONS:
5006 parse_omp_structured_block (st, false);
5007 break;
5009 case ST_OMP_PARALLEL_WORKSHARE:
5010 case ST_OMP_CRITICAL:
5011 parse_omp_structured_block (st, true);
5012 break;
5014 case ST_OMP_PARALLEL_DO:
5015 case ST_OMP_PARALLEL_DO_SIMD:
5016 st = parse_omp_do (st);
5017 continue;
5019 case ST_OMP_ATOMIC:
5020 st = parse_omp_oacc_atomic (true);
5021 continue;
5023 default:
5024 cycle = false;
5025 break;
5028 if (!cycle)
5029 break;
5031 st = next_statement ();
5034 else
5035 st = parse_executable (ST_NONE);
5036 if (st == ST_NONE)
5037 unexpected_eof ();
5038 else if (st == ST_OMP_SECTION
5039 && (omp_st == ST_OMP_SECTIONS
5040 || omp_st == ST_OMP_PARALLEL_SECTIONS))
5042 np = new_level (np);
5043 np->op = cp->op;
5044 np->block = NULL;
5046 else if (st != omp_end_st)
5047 unexpected_statement (st);
5049 while (st != omp_end_st);
5051 switch (new_st.op)
5053 case EXEC_OMP_END_NOWAIT:
5054 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
5055 break;
5056 case EXEC_OMP_CRITICAL:
5057 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
5058 || (new_st.ext.omp_name != NULL
5059 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
5060 gfc_error ("Name after !$omp critical and !$omp end critical does "
5061 "not match at %C");
5062 free (CONST_CAST (char *, new_st.ext.omp_name));
5063 break;
5064 case EXEC_OMP_END_SINGLE:
5065 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
5066 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
5067 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
5068 gfc_free_omp_clauses (new_st.ext.omp_clauses);
5069 break;
5070 case EXEC_NOP:
5071 break;
5072 default:
5073 gcc_unreachable ();
5076 gfc_clear_new_st ();
5077 gfc_commit_symbols ();
5078 gfc_warning_check ();
5079 pop_state ();
5083 /* Accept a series of executable statements. We return the first
5084 statement that doesn't fit to the caller. Any block statements are
5085 passed on to the correct handler, which usually passes the buck
5086 right back here. */
5088 static gfc_statement
5089 parse_executable (gfc_statement st)
5091 int close_flag;
5093 if (st == ST_NONE)
5094 st = next_statement ();
5096 for (;;)
5098 close_flag = check_do_closure ();
5099 if (close_flag)
5100 switch (st)
5102 case ST_GOTO:
5103 case ST_END_PROGRAM:
5104 case ST_RETURN:
5105 case ST_EXIT:
5106 case ST_END_FUNCTION:
5107 case ST_CYCLE:
5108 case ST_PAUSE:
5109 case ST_STOP:
5110 case ST_ERROR_STOP:
5111 case ST_END_SUBROUTINE:
5113 case ST_DO:
5114 case ST_FORALL:
5115 case ST_WHERE:
5116 case ST_SELECT_CASE:
5117 gfc_error ("%s statement at %C cannot terminate a non-block "
5118 "DO loop", gfc_ascii_statement (st));
5119 break;
5121 default:
5122 break;
5125 switch (st)
5127 case ST_NONE:
5128 unexpected_eof ();
5130 case ST_DATA:
5131 gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
5132 "first executable statement");
5133 /* Fall through. */
5135 case ST_FORMAT:
5136 case ST_ENTRY:
5137 case_executable:
5138 accept_statement (st);
5139 if (close_flag == 1)
5140 return ST_IMPLIED_ENDDO;
5141 break;
5143 case ST_BLOCK:
5144 parse_block_construct ();
5145 break;
5147 case ST_ASSOCIATE:
5148 parse_associate ();
5149 break;
5151 case ST_IF_BLOCK:
5152 parse_if_block ();
5153 break;
5155 case ST_SELECT_CASE:
5156 parse_select_block ();
5157 break;
5159 case ST_SELECT_TYPE:
5160 parse_select_type_block();
5161 break;
5163 case ST_DO:
5164 parse_do_block ();
5165 if (check_do_closure () == 1)
5166 return ST_IMPLIED_ENDDO;
5167 break;
5169 case ST_CRITICAL:
5170 parse_critical_block ();
5171 break;
5173 case ST_WHERE_BLOCK:
5174 parse_where_block ();
5175 break;
5177 case ST_FORALL_BLOCK:
5178 parse_forall_block ();
5179 break;
5181 case ST_OACC_PARALLEL_LOOP:
5182 case ST_OACC_KERNELS_LOOP:
5183 case ST_OACC_LOOP:
5184 st = parse_oacc_loop (st);
5185 if (st == ST_IMPLIED_ENDDO)
5186 return st;
5187 continue;
5189 case ST_OACC_PARALLEL:
5190 case ST_OACC_KERNELS:
5191 case ST_OACC_DATA:
5192 case ST_OACC_HOST_DATA:
5193 parse_oacc_structured_block (st);
5194 break;
5196 case ST_OMP_PARALLEL:
5197 case ST_OMP_PARALLEL_SECTIONS:
5198 case ST_OMP_SECTIONS:
5199 case ST_OMP_ORDERED:
5200 case ST_OMP_CRITICAL:
5201 case ST_OMP_MASTER:
5202 case ST_OMP_SINGLE:
5203 case ST_OMP_TARGET:
5204 case ST_OMP_TARGET_DATA:
5205 case ST_OMP_TARGET_TEAMS:
5206 case ST_OMP_TEAMS:
5207 case ST_OMP_TASK:
5208 case ST_OMP_TASKGROUP:
5209 parse_omp_structured_block (st, false);
5210 break;
5212 case ST_OMP_WORKSHARE:
5213 case ST_OMP_PARALLEL_WORKSHARE:
5214 parse_omp_structured_block (st, true);
5215 break;
5217 case ST_OMP_DISTRIBUTE:
5218 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
5219 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
5220 case ST_OMP_DISTRIBUTE_SIMD:
5221 case ST_OMP_DO:
5222 case ST_OMP_DO_SIMD:
5223 case ST_OMP_PARALLEL_DO:
5224 case ST_OMP_PARALLEL_DO_SIMD:
5225 case ST_OMP_SIMD:
5226 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
5227 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
5228 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5229 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
5230 case ST_OMP_TEAMS_DISTRIBUTE:
5231 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
5232 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5233 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
5234 st = parse_omp_do (st);
5235 if (st == ST_IMPLIED_ENDDO)
5236 return st;
5237 continue;
5239 case ST_OACC_ATOMIC:
5240 st = parse_omp_oacc_atomic (false);
5241 continue;
5243 case ST_OMP_ATOMIC:
5244 st = parse_omp_oacc_atomic (true);
5245 continue;
5247 default:
5248 return st;
5251 st = next_statement ();
5256 /* Fix the symbols for sibling functions. These are incorrectly added to
5257 the child namespace as the parser didn't know about this procedure. */
5259 static void
5260 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
5262 gfc_namespace *ns;
5263 gfc_symtree *st;
5264 gfc_symbol *old_sym;
5266 for (ns = siblings; ns; ns = ns->sibling)
5268 st = gfc_find_symtree (ns->sym_root, sym->name);
5270 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
5271 goto fixup_contained;
5273 if ((st->n.sym->attr.flavor == FL_DERIVED
5274 && sym->attr.generic && sym->attr.function)
5275 ||(sym->attr.flavor == FL_DERIVED
5276 && st->n.sym->attr.generic && st->n.sym->attr.function))
5277 goto fixup_contained;
5279 old_sym = st->n.sym;
5280 if (old_sym->ns == ns
5281 && !old_sym->attr.contained
5283 /* By 14.6.1.3, host association should be excluded
5284 for the following. */
5285 && !(old_sym->attr.external
5286 || (old_sym->ts.type != BT_UNKNOWN
5287 && !old_sym->attr.implicit_type)
5288 || old_sym->attr.flavor == FL_PARAMETER
5289 || old_sym->attr.use_assoc
5290 || old_sym->attr.in_common
5291 || old_sym->attr.in_equivalence
5292 || old_sym->attr.data
5293 || old_sym->attr.dummy
5294 || old_sym->attr.result
5295 || old_sym->attr.dimension
5296 || old_sym->attr.allocatable
5297 || old_sym->attr.intrinsic
5298 || old_sym->attr.generic
5299 || old_sym->attr.flavor == FL_NAMELIST
5300 || old_sym->attr.flavor == FL_LABEL
5301 || old_sym->attr.proc == PROC_ST_FUNCTION))
5303 /* Replace it with the symbol from the parent namespace. */
5304 st->n.sym = sym;
5305 sym->refs++;
5307 gfc_release_symbol (old_sym);
5310 fixup_contained:
5311 /* Do the same for any contained procedures. */
5312 gfc_fixup_sibling_symbols (sym, ns->contained);
5316 static void
5317 parse_contained (int module)
5319 gfc_namespace *ns, *parent_ns, *tmp;
5320 gfc_state_data s1, s2;
5321 gfc_statement st;
5322 gfc_symbol *sym;
5323 gfc_entry_list *el;
5324 locus old_loc;
5325 int contains_statements = 0;
5326 int seen_error = 0;
5328 push_state (&s1, COMP_CONTAINS, NULL);
5329 parent_ns = gfc_current_ns;
5333 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
5335 gfc_current_ns->sibling = parent_ns->contained;
5336 parent_ns->contained = gfc_current_ns;
5338 next:
5339 /* Process the next available statement. We come here if we got an error
5340 and rejected the last statement. */
5341 old_loc = gfc_current_locus;
5342 st = next_statement ();
5344 switch (st)
5346 case ST_NONE:
5347 unexpected_eof ();
5349 case ST_FUNCTION:
5350 case ST_SUBROUTINE:
5351 contains_statements = 1;
5352 accept_statement (st);
5354 push_state (&s2,
5355 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
5356 gfc_new_block);
5358 /* For internal procedures, create/update the symbol in the
5359 parent namespace. */
5361 if (!module)
5363 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
5364 gfc_error ("Contained procedure %qs at %C is already "
5365 "ambiguous", gfc_new_block->name);
5366 else
5368 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
5369 sym->name,
5370 &gfc_new_block->declared_at))
5372 if (st == ST_FUNCTION)
5373 gfc_add_function (&sym->attr, sym->name,
5374 &gfc_new_block->declared_at);
5375 else
5376 gfc_add_subroutine (&sym->attr, sym->name,
5377 &gfc_new_block->declared_at);
5381 gfc_commit_symbols ();
5383 else
5384 sym = gfc_new_block;
5386 /* Mark this as a contained function, so it isn't replaced
5387 by other module functions. */
5388 sym->attr.contained = 1;
5390 /* Set implicit_pure so that it can be reset if any of the
5391 tests for purity fail. This is used for some optimisation
5392 during translation. */
5393 if (!sym->attr.pure)
5394 sym->attr.implicit_pure = 1;
5396 parse_progunit (ST_NONE);
5398 /* Fix up any sibling functions that refer to this one. */
5399 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
5400 /* Or refer to any of its alternate entry points. */
5401 for (el = gfc_current_ns->entries; el; el = el->next)
5402 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
5404 gfc_current_ns->code = s2.head;
5405 gfc_current_ns = parent_ns;
5407 pop_state ();
5408 break;
5410 /* These statements are associated with the end of the host unit. */
5411 case ST_END_FUNCTION:
5412 case ST_END_MODULE:
5413 case ST_END_SUBMODULE:
5414 case ST_END_PROGRAM:
5415 case ST_END_SUBROUTINE:
5416 accept_statement (st);
5417 gfc_current_ns->code = s1.head;
5418 break;
5420 default:
5421 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
5422 gfc_ascii_statement (st));
5423 reject_statement ();
5424 seen_error = 1;
5425 goto next;
5426 break;
5429 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
5430 && st != ST_END_MODULE && st != ST_END_SUBMODULE
5431 && st != ST_END_PROGRAM);
5433 /* The first namespace in the list is guaranteed to not have
5434 anything (worthwhile) in it. */
5435 tmp = gfc_current_ns;
5436 gfc_current_ns = parent_ns;
5437 if (seen_error && tmp->refs > 1)
5438 gfc_free_namespace (tmp);
5440 ns = gfc_current_ns->contained;
5441 gfc_current_ns->contained = ns->sibling;
5442 gfc_free_namespace (ns);
5444 pop_state ();
5445 if (!contains_statements)
5446 gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
5447 "FUNCTION or SUBROUTINE statement at %L", &old_loc);
5451 /* The result variable in a MODULE PROCEDURE needs to be created and
5452 its characteristics copied from the interface since it is neither
5453 declared in the procedure declaration nor in the specification
5454 part. */
5456 static void
5457 get_modproc_result (void)
5459 gfc_symbol *proc;
5460 if (gfc_state_stack->previous
5461 && gfc_state_stack->previous->state == COMP_CONTAINS
5462 && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
5464 proc = gfc_current_ns->proc_name ? gfc_current_ns->proc_name : NULL;
5465 if (proc != NULL
5466 && proc->attr.function
5467 && proc->ts.interface
5468 && proc->ts.interface->result
5469 && proc->ts.interface->result != proc->ts.interface)
5471 gfc_copy_dummy_sym (&proc->result, proc->ts.interface->result, 1);
5472 gfc_set_sym_referenced (proc->result);
5473 proc->result->attr.if_source = IFSRC_DECL;
5474 gfc_commit_symbol (proc->result);
5480 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
5482 static void
5483 parse_progunit (gfc_statement st)
5485 gfc_state_data *p;
5486 int n;
5488 if (gfc_new_block
5489 && gfc_new_block->abr_modproc_decl
5490 && gfc_new_block->attr.function)
5491 get_modproc_result ();
5493 st = parse_spec (st);
5494 switch (st)
5496 case ST_NONE:
5497 unexpected_eof ();
5499 case ST_CONTAINS:
5500 /* This is not allowed within BLOCK! */
5501 if (gfc_current_state () != COMP_BLOCK)
5502 goto contains;
5503 break;
5505 case_end:
5506 accept_statement (st);
5507 goto done;
5509 default:
5510 break;
5513 if (gfc_current_state () == COMP_FUNCTION)
5514 gfc_check_function_type (gfc_current_ns);
5516 loop:
5517 for (;;)
5519 st = parse_executable (st);
5521 switch (st)
5523 case ST_NONE:
5524 unexpected_eof ();
5526 case ST_CONTAINS:
5527 /* This is not allowed within BLOCK! */
5528 if (gfc_current_state () != COMP_BLOCK)
5529 goto contains;
5530 break;
5532 case_end:
5533 accept_statement (st);
5534 goto done;
5536 default:
5537 break;
5540 unexpected_statement (st);
5541 reject_statement ();
5542 st = next_statement ();
5545 contains:
5546 n = 0;
5548 for (p = gfc_state_stack; p; p = p->previous)
5549 if (p->state == COMP_CONTAINS)
5550 n++;
5552 if (gfc_find_state (COMP_MODULE) == true
5553 || gfc_find_state (COMP_SUBMODULE) == true)
5554 n--;
5556 if (n > 0)
5558 gfc_error ("CONTAINS statement at %C is already in a contained "
5559 "program unit");
5560 reject_statement ();
5561 st = next_statement ();
5562 goto loop;
5565 parse_contained (0);
5567 done:
5568 gfc_current_ns->code = gfc_state_stack->head;
5572 /* Come here to complain about a global symbol already in use as
5573 something else. */
5575 void
5576 gfc_global_used (gfc_gsymbol *sym, locus *where)
5578 const char *name;
5580 if (where == NULL)
5581 where = &gfc_current_locus;
5583 switch(sym->type)
5585 case GSYM_PROGRAM:
5586 name = "PROGRAM";
5587 break;
5588 case GSYM_FUNCTION:
5589 name = "FUNCTION";
5590 break;
5591 case GSYM_SUBROUTINE:
5592 name = "SUBROUTINE";
5593 break;
5594 case GSYM_COMMON:
5595 name = "COMMON";
5596 break;
5597 case GSYM_BLOCK_DATA:
5598 name = "BLOCK DATA";
5599 break;
5600 case GSYM_MODULE:
5601 name = "MODULE";
5602 break;
5603 default:
5604 gfc_internal_error ("gfc_global_used(): Bad type");
5605 name = NULL;
5608 if (sym->binding_label)
5609 gfc_error ("Global binding name %qs at %L is already being used as a %s "
5610 "at %L", sym->binding_label, where, name, &sym->where);
5611 else
5612 gfc_error ("Global name %qs at %L is already being used as a %s at %L",
5613 sym->name, where, name, &sym->where);
5617 /* Parse a block data program unit. */
5619 static void
5620 parse_block_data (void)
5622 gfc_statement st;
5623 static locus blank_locus;
5624 static int blank_block=0;
5625 gfc_gsymbol *s;
5627 gfc_current_ns->proc_name = gfc_new_block;
5628 gfc_current_ns->is_block_data = 1;
5630 if (gfc_new_block == NULL)
5632 if (blank_block)
5633 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5634 "prior BLOCK DATA at %L", &blank_locus);
5635 else
5637 blank_block = 1;
5638 blank_locus = gfc_current_locus;
5641 else
5643 s = gfc_get_gsymbol (gfc_new_block->name);
5644 if (s->defined
5645 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
5646 gfc_global_used (s, &gfc_new_block->declared_at);
5647 else
5649 s->type = GSYM_BLOCK_DATA;
5650 s->where = gfc_new_block->declared_at;
5651 s->defined = 1;
5655 st = parse_spec (ST_NONE);
5657 while (st != ST_END_BLOCK_DATA)
5659 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5660 gfc_ascii_statement (st));
5661 reject_statement ();
5662 st = next_statement ();
5667 /* Following the association of the ancestor (sub)module symbols, they
5668 must be set host rather than use associated and all must be public.
5669 They are flagged up by 'used_in_submodule' so that they can be set
5670 DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl). Otherwise the
5671 linker chokes on multiple symbol definitions. */
5673 static void
5674 set_syms_host_assoc (gfc_symbol *sym)
5676 gfc_component *c;
5678 if (sym == NULL)
5679 return;
5681 if (sym->attr.module_procedure)
5682 sym->attr.external = 0;
5684 /* sym->attr.access = ACCESS_PUBLIC; */
5686 sym->attr.use_assoc = 0;
5687 sym->attr.host_assoc = 1;
5688 sym->attr.used_in_submodule =1;
5690 if (sym->attr.flavor == FL_DERIVED)
5692 for (c = sym->components; c; c = c->next)
5693 c->attr.access = ACCESS_PUBLIC;
5697 /* Parse a module subprogram. */
5699 static void
5700 parse_module (void)
5702 gfc_statement st;
5703 gfc_gsymbol *s;
5704 bool error;
5706 s = gfc_get_gsymbol (gfc_new_block->name);
5707 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
5708 gfc_global_used (s, &gfc_new_block->declared_at);
5709 else
5711 s->type = GSYM_MODULE;
5712 s->where = gfc_new_block->declared_at;
5713 s->defined = 1;
5716 /* Something is nulling the module_list after this point. This is good
5717 since it allows us to 'USE' the parent modules that the submodule
5718 inherits and to set (most) of the symbols as host associated. */
5719 if (gfc_current_state () == COMP_SUBMODULE)
5721 use_modules ();
5722 gfc_traverse_ns (gfc_current_ns, set_syms_host_assoc);
5725 st = parse_spec (ST_NONE);
5727 error = false;
5728 loop:
5729 switch (st)
5731 case ST_NONE:
5732 unexpected_eof ();
5734 case ST_CONTAINS:
5735 parse_contained (1);
5736 break;
5738 case ST_END_MODULE:
5739 case ST_END_SUBMODULE:
5740 accept_statement (st);
5741 break;
5743 default:
5744 gfc_error ("Unexpected %s statement in MODULE at %C",
5745 gfc_ascii_statement (st));
5747 error = true;
5748 reject_statement ();
5749 st = next_statement ();
5750 goto loop;
5753 /* Make sure not to free the namespace twice on error. */
5754 if (!error)
5755 s->ns = gfc_current_ns;
5759 /* Add a procedure name to the global symbol table. */
5761 static void
5762 add_global_procedure (bool sub)
5764 gfc_gsymbol *s;
5766 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5767 name is a global identifier. */
5768 if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
5770 s = gfc_get_gsymbol (gfc_new_block->name);
5772 if (s->defined
5773 || (s->type != GSYM_UNKNOWN
5774 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5776 gfc_global_used (s, &gfc_new_block->declared_at);
5777 /* Silence follow-up errors. */
5778 gfc_new_block->binding_label = NULL;
5780 else
5782 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5783 s->sym_name = gfc_new_block->name;
5784 s->where = gfc_new_block->declared_at;
5785 s->defined = 1;
5786 s->ns = gfc_current_ns;
5790 /* Don't add the symbol multiple times. */
5791 if (gfc_new_block->binding_label
5792 && (!gfc_notification_std (GFC_STD_F2008)
5793 || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
5795 s = gfc_get_gsymbol (gfc_new_block->binding_label);
5797 if (s->defined
5798 || (s->type != GSYM_UNKNOWN
5799 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5801 gfc_global_used (s, &gfc_new_block->declared_at);
5802 /* Silence follow-up errors. */
5803 gfc_new_block->binding_label = NULL;
5805 else
5807 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5808 s->sym_name = gfc_new_block->name;
5809 s->binding_label = gfc_new_block->binding_label;
5810 s->where = gfc_new_block->declared_at;
5811 s->defined = 1;
5812 s->ns = gfc_current_ns;
5818 /* Add a program to the global symbol table. */
5820 static void
5821 add_global_program (void)
5823 gfc_gsymbol *s;
5825 if (gfc_new_block == NULL)
5826 return;
5827 s = gfc_get_gsymbol (gfc_new_block->name);
5829 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
5830 gfc_global_used (s, &gfc_new_block->declared_at);
5831 else
5833 s->type = GSYM_PROGRAM;
5834 s->where = gfc_new_block->declared_at;
5835 s->defined = 1;
5836 s->ns = gfc_current_ns;
5841 /* Resolve all the program units. */
5842 static void
5843 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
5845 gfc_free_dt_list ();
5846 gfc_current_ns = gfc_global_ns_list;
5847 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5849 if (gfc_current_ns->proc_name
5850 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5851 continue; /* Already resolved. */
5853 if (gfc_current_ns->proc_name)
5854 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5855 gfc_resolve (gfc_current_ns);
5856 gfc_current_ns->derived_types = gfc_derived_types;
5857 gfc_derived_types = NULL;
5862 static void
5863 clean_up_modules (gfc_gsymbol *gsym)
5865 if (gsym == NULL)
5866 return;
5868 clean_up_modules (gsym->left);
5869 clean_up_modules (gsym->right);
5871 if (gsym->type != GSYM_MODULE || !gsym->ns)
5872 return;
5874 gfc_current_ns = gsym->ns;
5875 gfc_derived_types = gfc_current_ns->derived_types;
5876 gfc_done_2 ();
5877 gsym->ns = NULL;
5878 return;
5882 /* Translate all the program units. This could be in a different order
5883 to resolution if there are forward references in the file. */
5884 static void
5885 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
5887 int errors;
5889 gfc_current_ns = gfc_global_ns_list;
5890 gfc_get_errors (NULL, &errors);
5892 /* We first translate all modules to make sure that later parts
5893 of the program can use the decl. Then we translate the nonmodules. */
5895 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5897 if (!gfc_current_ns->proc_name
5898 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5899 continue;
5901 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5902 gfc_derived_types = gfc_current_ns->derived_types;
5903 gfc_generate_module_code (gfc_current_ns);
5904 gfc_current_ns->translated = 1;
5907 gfc_current_ns = gfc_global_ns_list;
5908 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5910 if (gfc_current_ns->proc_name
5911 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5912 continue;
5914 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5915 gfc_derived_types = gfc_current_ns->derived_types;
5916 gfc_generate_code (gfc_current_ns);
5917 gfc_current_ns->translated = 1;
5920 /* Clean up all the namespaces after translation. */
5921 gfc_current_ns = gfc_global_ns_list;
5922 for (;gfc_current_ns;)
5924 gfc_namespace *ns;
5926 if (gfc_current_ns->proc_name
5927 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5929 gfc_current_ns = gfc_current_ns->sibling;
5930 continue;
5933 ns = gfc_current_ns->sibling;
5934 gfc_derived_types = gfc_current_ns->derived_types;
5935 gfc_done_2 ();
5936 gfc_current_ns = ns;
5939 clean_up_modules (gfc_gsym_root);
5943 /* Top level parser. */
5945 bool
5946 gfc_parse_file (void)
5948 int seen_program, errors_before, errors;
5949 gfc_state_data top, s;
5950 gfc_statement st;
5951 locus prog_locus;
5952 gfc_namespace *next;
5954 gfc_start_source_files ();
5956 top.state = COMP_NONE;
5957 top.sym = NULL;
5958 top.previous = NULL;
5959 top.head = top.tail = NULL;
5960 top.do_variable = NULL;
5962 gfc_state_stack = &top;
5964 gfc_clear_new_st ();
5966 gfc_statement_label = NULL;
5968 if (setjmp (eof_buf))
5969 return false; /* Come here on unexpected EOF */
5971 /* Prepare the global namespace that will contain the
5972 program units. */
5973 gfc_global_ns_list = next = NULL;
5975 seen_program = 0;
5976 errors_before = 0;
5978 /* Exit early for empty files. */
5979 if (gfc_at_eof ())
5980 goto done;
5982 in_specification_block = true;
5983 loop:
5984 gfc_init_2 ();
5985 st = next_statement ();
5986 switch (st)
5988 case ST_NONE:
5989 gfc_done_2 ();
5990 goto done;
5992 case ST_PROGRAM:
5993 if (seen_program)
5994 goto duplicate_main;
5995 seen_program = 1;
5996 prog_locus = gfc_current_locus;
5998 push_state (&s, COMP_PROGRAM, gfc_new_block);
5999 main_program_symbol(gfc_current_ns, gfc_new_block->name);
6000 accept_statement (st);
6001 add_global_program ();
6002 parse_progunit (ST_NONE);
6003 goto prog_units;
6004 break;
6006 case ST_SUBROUTINE:
6007 add_global_procedure (true);
6008 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
6009 accept_statement (st);
6010 parse_progunit (ST_NONE);
6011 goto prog_units;
6012 break;
6014 case ST_FUNCTION:
6015 add_global_procedure (false);
6016 push_state (&s, COMP_FUNCTION, gfc_new_block);
6017 accept_statement (st);
6018 parse_progunit (ST_NONE);
6019 goto prog_units;
6020 break;
6022 case ST_BLOCK_DATA:
6023 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
6024 accept_statement (st);
6025 parse_block_data ();
6026 break;
6028 case ST_MODULE:
6029 push_state (&s, COMP_MODULE, gfc_new_block);
6030 accept_statement (st);
6032 gfc_get_errors (NULL, &errors_before);
6033 parse_module ();
6034 break;
6036 case ST_SUBMODULE:
6037 push_state (&s, COMP_SUBMODULE, gfc_new_block);
6038 accept_statement (st);
6040 gfc_get_errors (NULL, &errors_before);
6041 parse_module ();
6042 break;
6044 /* Anything else starts a nameless main program block. */
6045 default:
6046 if (seen_program)
6047 goto duplicate_main;
6048 seen_program = 1;
6049 prog_locus = gfc_current_locus;
6051 push_state (&s, COMP_PROGRAM, gfc_new_block);
6052 main_program_symbol (gfc_current_ns, "MAIN__");
6053 parse_progunit (st);
6054 goto prog_units;
6055 break;
6058 /* Handle the non-program units. */
6059 gfc_current_ns->code = s.head;
6061 gfc_resolve (gfc_current_ns);
6063 /* Dump the parse tree if requested. */
6064 if (flag_dump_fortran_original)
6065 gfc_dump_parse_tree (gfc_current_ns, stdout);
6067 gfc_get_errors (NULL, &errors);
6068 if (s.state == COMP_MODULE || s.state == COMP_SUBMODULE)
6070 gfc_dump_module (s.sym->name, errors_before == errors);
6071 gfc_current_ns->derived_types = gfc_derived_types;
6072 gfc_derived_types = NULL;
6073 goto prog_units;
6075 else
6077 if (errors == 0)
6078 gfc_generate_code (gfc_current_ns);
6079 pop_state ();
6080 gfc_done_2 ();
6083 goto loop;
6085 prog_units:
6086 /* The main program and non-contained procedures are put
6087 in the global namespace list, so that they can be processed
6088 later and all their interfaces resolved. */
6089 gfc_current_ns->code = s.head;
6090 if (next)
6092 for (; next->sibling; next = next->sibling)
6094 next->sibling = gfc_current_ns;
6096 else
6097 gfc_global_ns_list = gfc_current_ns;
6099 next = gfc_current_ns;
6101 pop_state ();
6102 goto loop;
6104 done:
6106 /* Do the resolution. */
6107 resolve_all_program_units (gfc_global_ns_list);
6109 /* Do the parse tree dump. */
6110 gfc_current_ns
6111 = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
6113 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6114 if (!gfc_current_ns->proc_name
6115 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6117 gfc_dump_parse_tree (gfc_current_ns, stdout);
6118 fputs ("------------------------------------------\n\n", stdout);
6121 /* Do the translation. */
6122 translate_all_program_units (gfc_global_ns_list);
6124 gfc_end_source_files ();
6125 return true;
6127 duplicate_main:
6128 /* If we see a duplicate main program, shut down. If the second
6129 instance is an implied main program, i.e. data decls or executable
6130 statements, we're in for lots of errors. */
6131 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
6132 reject_statement ();
6133 gfc_done_2 ();
6134 return true;
6137 /* Return true if this state data represents an OpenACC region. */
6138 bool
6139 is_oacc (gfc_state_data *sd)
6141 switch (sd->construct->op)
6143 case EXEC_OACC_PARALLEL_LOOP:
6144 case EXEC_OACC_PARALLEL:
6145 case EXEC_OACC_KERNELS_LOOP:
6146 case EXEC_OACC_KERNELS:
6147 case EXEC_OACC_DATA:
6148 case EXEC_OACC_HOST_DATA:
6149 case EXEC_OACC_LOOP:
6150 case EXEC_OACC_UPDATE:
6151 case EXEC_OACC_WAIT:
6152 case EXEC_OACC_CACHE:
6153 case EXEC_OACC_ENTER_DATA:
6154 case EXEC_OACC_EXIT_DATA:
6155 case EXEC_OACC_ATOMIC:
6156 case EXEC_OACC_ROUTINE:
6157 return true;
6159 default:
6160 return false;