PR middle-end/66867
[official-gcc.git] / gcc / fortran / parse.c
blob1081b2e605e35ff064ad1150cc021439b7646afd
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, but set a flag simd_matched if keyword matched. */
593 #define matchs(keyword, subr, st) \
594 do { \
595 if (match_word_omp_simd (keyword, subr, &old_locus, \
596 &simd_matched) == MATCH_YES) \
597 return st; \
598 else \
599 undo_new_statement (); \
600 } while (0);
602 /* Like match, but don't match anything if not -fopenmp. */
603 #define matcho(keyword, subr, st) \
604 do { \
605 if (!flag_openmp) \
607 else if (match_word (keyword, subr, &old_locus) \
608 == MATCH_YES) \
609 return st; \
610 else \
611 undo_new_statement (); \
612 } while (0);
614 static gfc_statement
615 decode_oacc_directive (void)
617 locus old_locus;
618 char c;
620 gfc_enforce_clean_symbol_state ();
622 gfc_clear_error (); /* Clear any pending errors. */
623 gfc_clear_warning (); /* Clear any pending warnings. */
625 if (gfc_pure (NULL))
627 gfc_error_now ("OpenACC directives at %C may not appear in PURE "
628 "procedures");
629 gfc_error_recovery ();
630 return ST_NONE;
633 gfc_unset_implicit_pure (NULL);
635 old_locus = gfc_current_locus;
637 /* General OpenACC directive matching: Instead of testing every possible
638 statement, we eliminate most possibilities by peeking at the
639 first character. */
641 c = gfc_peek_ascii_char ();
643 switch (c)
645 case 'a':
646 match ("atomic", gfc_match_oacc_atomic, ST_OACC_ATOMIC);
647 break;
648 case 'c':
649 match ("cache", gfc_match_oacc_cache, ST_OACC_CACHE);
650 break;
651 case 'd':
652 match ("data", gfc_match_oacc_data, ST_OACC_DATA);
653 match ("declare", gfc_match_oacc_declare, ST_OACC_DECLARE);
654 break;
655 case 'e':
656 match ("end atomic", gfc_match_omp_eos, ST_OACC_END_ATOMIC);
657 match ("end data", gfc_match_omp_eos, ST_OACC_END_DATA);
658 match ("end host_data", gfc_match_omp_eos, ST_OACC_END_HOST_DATA);
659 match ("end kernels loop", gfc_match_omp_eos, ST_OACC_END_KERNELS_LOOP);
660 match ("end kernels", gfc_match_omp_eos, ST_OACC_END_KERNELS);
661 match ("end loop", gfc_match_omp_eos, ST_OACC_END_LOOP);
662 match ("end parallel loop", gfc_match_omp_eos, ST_OACC_END_PARALLEL_LOOP);
663 match ("end parallel", gfc_match_omp_eos, ST_OACC_END_PARALLEL);
664 match ("enter data", gfc_match_oacc_enter_data, ST_OACC_ENTER_DATA);
665 match ("exit data", gfc_match_oacc_exit_data, ST_OACC_EXIT_DATA);
666 break;
667 case 'h':
668 match ("host_data", gfc_match_oacc_host_data, ST_OACC_HOST_DATA);
669 break;
670 case 'p':
671 match ("parallel loop", gfc_match_oacc_parallel_loop, ST_OACC_PARALLEL_LOOP);
672 match ("parallel", gfc_match_oacc_parallel, ST_OACC_PARALLEL);
673 break;
674 case 'k':
675 match ("kernels loop", gfc_match_oacc_kernels_loop, ST_OACC_KERNELS_LOOP);
676 match ("kernels", gfc_match_oacc_kernels, ST_OACC_KERNELS);
677 break;
678 case 'l':
679 match ("loop", gfc_match_oacc_loop, ST_OACC_LOOP);
680 break;
681 case 'r':
682 match ("routine", gfc_match_oacc_routine, ST_OACC_ROUTINE);
683 break;
684 case 'u':
685 match ("update", gfc_match_oacc_update, ST_OACC_UPDATE);
686 break;
687 case 'w':
688 match ("wait", gfc_match_oacc_wait, ST_OACC_WAIT);
689 break;
692 /* Directive not found or stored an error message.
693 Check and give up. */
695 if (gfc_error_check () == 0)
696 gfc_error_now ("Unclassifiable OpenACC directive at %C");
698 reject_statement ();
700 gfc_error_recovery ();
702 return ST_NONE;
705 static gfc_statement
706 decode_omp_directive (void)
708 locus old_locus;
709 char c;
710 bool simd_matched = false;
712 gfc_enforce_clean_symbol_state ();
714 gfc_clear_error (); /* Clear any pending errors. */
715 gfc_clear_warning (); /* Clear any pending warnings. */
717 if (gfc_pure (NULL))
719 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
720 "or ELEMENTAL procedures");
721 gfc_error_recovery ();
722 return ST_NONE;
725 gfc_unset_implicit_pure (NULL);
727 old_locus = gfc_current_locus;
729 /* General OpenMP directive matching: Instead of testing every possible
730 statement, we eliminate most possibilities by peeking at the
731 first character. */
733 c = gfc_peek_ascii_char ();
735 /* match is for directives that should be recognized only if
736 -fopenmp, matchs for directives that should be recognized
737 if either -fopenmp or -fopenmp-simd. */
738 switch (c)
740 case 'a':
741 matcho ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
742 break;
743 case 'b':
744 matcho ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
745 break;
746 case 'c':
747 matcho ("cancellation% point", gfc_match_omp_cancellation_point,
748 ST_OMP_CANCELLATION_POINT);
749 matcho ("cancel", gfc_match_omp_cancel, ST_OMP_CANCEL);
750 matcho ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
751 break;
752 case 'd':
753 matchs ("declare reduction", gfc_match_omp_declare_reduction,
754 ST_OMP_DECLARE_REDUCTION);
755 matchs ("declare simd", gfc_match_omp_declare_simd,
756 ST_OMP_DECLARE_SIMD);
757 matcho ("declare target", gfc_match_omp_declare_target,
758 ST_OMP_DECLARE_TARGET);
759 matchs ("distribute parallel do simd",
760 gfc_match_omp_distribute_parallel_do_simd,
761 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD);
762 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do,
763 ST_OMP_DISTRIBUTE_PARALLEL_DO);
764 matchs ("distribute simd", gfc_match_omp_distribute_simd,
765 ST_OMP_DISTRIBUTE_SIMD);
766 matcho ("distribute", gfc_match_omp_distribute, ST_OMP_DISTRIBUTE);
767 matchs ("do simd", gfc_match_omp_do_simd, ST_OMP_DO_SIMD);
768 matcho ("do", gfc_match_omp_do, ST_OMP_DO);
769 break;
770 case 'e':
771 matcho ("end atomic", gfc_match_omp_eos, ST_OMP_END_ATOMIC);
772 matcho ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
773 matchs ("end distribute parallel do simd", gfc_match_omp_eos,
774 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD);
775 matcho ("end distribute parallel do", gfc_match_omp_eos,
776 ST_OMP_END_DISTRIBUTE_PARALLEL_DO);
777 matchs ("end distribute simd", gfc_match_omp_eos,
778 ST_OMP_END_DISTRIBUTE_SIMD);
779 matcho ("end distribute", gfc_match_omp_eos, ST_OMP_END_DISTRIBUTE);
780 matchs ("end do simd", gfc_match_omp_end_nowait, ST_OMP_END_DO_SIMD);
781 matcho ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
782 matchs ("end simd", gfc_match_omp_eos, ST_OMP_END_SIMD);
783 matcho ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
784 matcho ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
785 matchs ("end parallel do simd", gfc_match_omp_eos,
786 ST_OMP_END_PARALLEL_DO_SIMD);
787 matcho ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
788 matcho ("end parallel sections", gfc_match_omp_eos,
789 ST_OMP_END_PARALLEL_SECTIONS);
790 matcho ("end parallel workshare", gfc_match_omp_eos,
791 ST_OMP_END_PARALLEL_WORKSHARE);
792 matcho ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
793 matcho ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
794 matcho ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
795 matcho ("end target data", gfc_match_omp_eos, ST_OMP_END_TARGET_DATA);
796 matchs ("end target teams distribute parallel do simd",
797 gfc_match_omp_eos,
798 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
799 matcho ("end target teams distribute parallel do", gfc_match_omp_eos,
800 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
801 matchs ("end target teams distribute simd", gfc_match_omp_eos,
802 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD);
803 matcho ("end target teams distribute", gfc_match_omp_eos,
804 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE);
805 matcho ("end target teams", gfc_match_omp_eos, ST_OMP_END_TARGET_TEAMS);
806 matcho ("end target", gfc_match_omp_eos, ST_OMP_END_TARGET);
807 matcho ("end taskgroup", gfc_match_omp_eos, ST_OMP_END_TASKGROUP);
808 matcho ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
809 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos,
810 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
811 matcho ("end teams distribute parallel do", gfc_match_omp_eos,
812 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO);
813 matchs ("end teams distribute simd", gfc_match_omp_eos,
814 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD);
815 matcho ("end teams distribute", gfc_match_omp_eos,
816 ST_OMP_END_TEAMS_DISTRIBUTE);
817 matcho ("end teams", gfc_match_omp_eos, ST_OMP_END_TEAMS);
818 matcho ("end workshare", gfc_match_omp_end_nowait,
819 ST_OMP_END_WORKSHARE);
820 break;
821 case 'f':
822 matcho ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
823 break;
824 case 'm':
825 matcho ("master", gfc_match_omp_master, ST_OMP_MASTER);
826 break;
827 case 'o':
828 matcho ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
829 break;
830 case 'p':
831 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
832 ST_OMP_PARALLEL_DO_SIMD);
833 matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
834 matcho ("parallel sections", gfc_match_omp_parallel_sections,
835 ST_OMP_PARALLEL_SECTIONS);
836 matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
837 ST_OMP_PARALLEL_WORKSHARE);
838 matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
839 break;
840 case 's':
841 matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
842 matcho ("section", gfc_match_omp_eos, ST_OMP_SECTION);
843 matchs ("simd", gfc_match_omp_simd, ST_OMP_SIMD);
844 matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
845 break;
846 case 't':
847 matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
848 matchs ("target teams distribute parallel do simd",
849 gfc_match_omp_target_teams_distribute_parallel_do_simd,
850 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
851 matcho ("target teams distribute parallel do",
852 gfc_match_omp_target_teams_distribute_parallel_do,
853 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
854 matchs ("target teams distribute simd",
855 gfc_match_omp_target_teams_distribute_simd,
856 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
857 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
858 ST_OMP_TARGET_TEAMS_DISTRIBUTE);
859 matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
860 matcho ("target update", gfc_match_omp_target_update,
861 ST_OMP_TARGET_UPDATE);
862 matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
863 matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
864 matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
865 matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
866 matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
867 matchs ("teams distribute parallel do simd",
868 gfc_match_omp_teams_distribute_parallel_do_simd,
869 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
870 matcho ("teams distribute parallel do",
871 gfc_match_omp_teams_distribute_parallel_do,
872 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
873 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
874 ST_OMP_TEAMS_DISTRIBUTE_SIMD);
875 matcho ("teams distribute", gfc_match_omp_teams_distribute,
876 ST_OMP_TEAMS_DISTRIBUTE);
877 matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
878 matcho ("threadprivate", gfc_match_omp_threadprivate,
879 ST_OMP_THREADPRIVATE);
880 break;
881 case 'w':
882 matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
883 break;
886 /* All else has failed, so give up. See if any of the matchers has
887 stored an error message of some sort. Don't error out if
888 not -fopenmp and simd_matched is false, i.e. if a directive other
889 than one marked with match has been seen. */
891 if (flag_openmp || simd_matched)
893 if (!gfc_error_check ())
894 gfc_error_now ("Unclassifiable OpenMP directive at %C");
897 reject_statement ();
899 gfc_error_recovery ();
901 return ST_NONE;
904 static gfc_statement
905 decode_gcc_attribute (void)
907 locus old_locus;
909 gfc_enforce_clean_symbol_state ();
911 gfc_clear_error (); /* Clear any pending errors. */
912 gfc_clear_warning (); /* Clear any pending warnings. */
913 old_locus = gfc_current_locus;
915 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
917 /* All else has failed, so give up. See if any of the matchers has
918 stored an error message of some sort. */
920 if (!gfc_error_check ())
921 gfc_error_now ("Unclassifiable GCC directive at %C");
923 reject_statement ();
925 gfc_error_recovery ();
927 return ST_NONE;
930 #undef match
932 /* Assert next length characters to be equal to token in free form. */
934 static void
935 verify_token_free (const char* token, int length, bool last_was_use_stmt)
937 int i;
938 char c;
940 c = gfc_next_ascii_char ();
941 for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
942 gcc_assert (c == token[i]);
944 gcc_assert (gfc_is_whitespace(c));
945 gfc_gobble_whitespace ();
946 if (last_was_use_stmt)
947 use_modules ();
950 /* Get the next statement in free form source. */
952 static gfc_statement
953 next_free (void)
955 match m;
956 int i, cnt, at_bol;
957 char c;
959 at_bol = gfc_at_bol ();
960 gfc_gobble_whitespace ();
962 c = gfc_peek_ascii_char ();
964 if (ISDIGIT (c))
966 char d;
968 /* Found a statement label? */
969 m = gfc_match_st_label (&gfc_statement_label);
971 d = gfc_peek_ascii_char ();
972 if (m != MATCH_YES || !gfc_is_whitespace (d))
974 gfc_match_small_literal_int (&i, &cnt);
976 if (cnt > 5)
977 gfc_error_now ("Too many digits in statement label at %C");
979 if (i == 0)
980 gfc_error_now ("Zero is not a valid statement label at %C");
983 c = gfc_next_ascii_char ();
984 while (ISDIGIT(c));
986 if (!gfc_is_whitespace (c))
987 gfc_error_now ("Non-numeric character in statement label at %C");
989 return ST_NONE;
991 else
993 label_locus = gfc_current_locus;
995 gfc_gobble_whitespace ();
997 if (at_bol && gfc_peek_ascii_char () == ';')
999 gfc_error_now ("Semicolon at %C needs to be preceded by "
1000 "statement");
1001 gfc_next_ascii_char (); /* Eat up the semicolon. */
1002 return ST_NONE;
1005 if (gfc_match_eos () == MATCH_YES)
1007 gfc_warning_now (0, "Ignoring statement label in empty statement "
1008 "at %L", &label_locus);
1009 gfc_free_st_label (gfc_statement_label);
1010 gfc_statement_label = NULL;
1011 return ST_NONE;
1015 else if (c == '!')
1017 /* Comments have already been skipped by the time we get here,
1018 except for GCC attributes and OpenMP/OpenACC directives. */
1020 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
1021 c = gfc_peek_ascii_char ();
1023 if (c == 'g')
1025 int i;
1027 c = gfc_next_ascii_char ();
1028 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
1029 gcc_assert (c == "gcc$"[i]);
1031 gfc_gobble_whitespace ();
1032 return decode_gcc_attribute ();
1035 else if (c == '$')
1037 /* Since both OpenMP and OpenACC directives starts with
1038 !$ character sequence, we must check all flags combinations */
1039 if ((flag_openmp || flag_openmp_simd)
1040 && !flag_openacc)
1042 verify_token_free ("$omp", 4, last_was_use_stmt);
1043 return decode_omp_directive ();
1045 else if ((flag_openmp || flag_openmp_simd)
1046 && flag_openacc)
1048 gfc_next_ascii_char (); /* Eat up dollar character */
1049 c = gfc_peek_ascii_char ();
1051 if (c == 'o')
1053 verify_token_free ("omp", 3, last_was_use_stmt);
1054 return decode_omp_directive ();
1056 else if (c == 'a')
1058 verify_token_free ("acc", 3, last_was_use_stmt);
1059 return decode_oacc_directive ();
1062 else if (flag_openacc)
1064 verify_token_free ("$acc", 4, last_was_use_stmt);
1065 return decode_oacc_directive ();
1068 gcc_unreachable ();
1071 if (at_bol && c == ';')
1073 if (!(gfc_option.allow_std & GFC_STD_F2008))
1074 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1075 "statement");
1076 gfc_next_ascii_char (); /* Eat up the semicolon. */
1077 return ST_NONE;
1080 return decode_statement ();
1083 /* Assert next length characters to be equal to token in fixed form. */
1085 static bool
1086 verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
1088 int i;
1089 char c = gfc_next_char_literal (NONSTRING);
1091 for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
1092 gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
1094 if (c != ' ' && c != '0')
1096 gfc_buffer_error (false);
1097 gfc_error ("Bad continuation line at %C");
1098 return false;
1100 if (last_was_use_stmt)
1101 use_modules ();
1103 return true;
1106 /* Get the next statement in fixed-form source. */
1108 static gfc_statement
1109 next_fixed (void)
1111 int label, digit_flag, i;
1112 locus loc;
1113 gfc_char_t c;
1115 if (!gfc_at_bol ())
1116 return decode_statement ();
1118 /* Skip past the current label field, parsing a statement label if
1119 one is there. This is a weird number parser, since the number is
1120 contained within five columns and can have any kind of embedded
1121 spaces. We also check for characters that make the rest of the
1122 line a comment. */
1124 label = 0;
1125 digit_flag = 0;
1127 for (i = 0; i < 5; i++)
1129 c = gfc_next_char_literal (NONSTRING);
1131 switch (c)
1133 case ' ':
1134 break;
1136 case '0':
1137 case '1':
1138 case '2':
1139 case '3':
1140 case '4':
1141 case '5':
1142 case '6':
1143 case '7':
1144 case '8':
1145 case '9':
1146 label = label * 10 + ((unsigned char) c - '0');
1147 label_locus = gfc_current_locus;
1148 digit_flag = 1;
1149 break;
1151 /* Comments have already been skipped by the time we get
1152 here, except for GCC attributes and OpenMP directives. */
1154 case '*':
1155 c = gfc_next_char_literal (NONSTRING);
1157 if (TOLOWER (c) == 'g')
1159 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
1160 gcc_assert (TOLOWER (c) == "gcc$"[i]);
1162 return decode_gcc_attribute ();
1164 else if (c == '$')
1166 if ((flag_openmp || flag_openmp_simd)
1167 && !flag_openacc)
1169 if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
1170 return ST_NONE;
1171 return decode_omp_directive ();
1173 else if ((flag_openmp || flag_openmp_simd)
1174 && flag_openacc)
1176 c = gfc_next_char_literal(NONSTRING);
1177 if (c == 'o' || c == 'O')
1179 if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
1180 return ST_NONE;
1181 return decode_omp_directive ();
1183 else if (c == 'a' || c == 'A')
1185 if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
1186 return ST_NONE;
1187 return decode_oacc_directive ();
1190 else if (flag_openacc)
1192 if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
1193 return ST_NONE;
1194 return decode_oacc_directive ();
1197 /* FALLTHROUGH */
1199 /* Comments have already been skipped by the time we get
1200 here so don't bother checking for them. */
1202 default:
1203 gfc_buffer_error (false);
1204 gfc_error ("Non-numeric character in statement label at %C");
1205 return ST_NONE;
1209 if (digit_flag)
1211 if (label == 0)
1212 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1213 else
1215 /* We've found a valid statement label. */
1216 gfc_statement_label = gfc_get_st_label (label);
1220 /* Since this line starts a statement, it cannot be a continuation
1221 of a previous statement. If we see something here besides a
1222 space or zero, it must be a bad continuation line. */
1224 c = gfc_next_char_literal (NONSTRING);
1225 if (c == '\n')
1226 goto blank_line;
1228 if (c != ' ' && c != '0')
1230 gfc_buffer_error (false);
1231 gfc_error ("Bad continuation line at %C");
1232 return ST_NONE;
1235 /* Now that we've taken care of the statement label columns, we have
1236 to make sure that the first nonblank character is not a '!'. If
1237 it is, the rest of the line is a comment. */
1241 loc = gfc_current_locus;
1242 c = gfc_next_char_literal (NONSTRING);
1244 while (gfc_is_whitespace (c));
1246 if (c == '!')
1247 goto blank_line;
1248 gfc_current_locus = loc;
1250 if (c == ';')
1252 if (digit_flag)
1253 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1254 else if (!(gfc_option.allow_std & GFC_STD_F2008))
1255 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1256 "statement");
1257 return ST_NONE;
1260 if (gfc_match_eos () == MATCH_YES)
1261 goto blank_line;
1263 /* At this point, we've got a nonblank statement to parse. */
1264 return decode_statement ();
1266 blank_line:
1267 if (digit_flag)
1268 gfc_warning_now (0, "Ignoring statement label in empty statement at %L",
1269 &label_locus);
1271 gfc_current_locus.lb->truncated = 0;
1272 gfc_advance_line ();
1273 return ST_NONE;
1277 /* Return the next non-ST_NONE statement to the caller. We also worry
1278 about including files and the ends of include files at this stage. */
1280 static gfc_statement
1281 next_statement (void)
1283 gfc_statement st;
1284 locus old_locus;
1286 gfc_enforce_clean_symbol_state ();
1288 gfc_new_block = NULL;
1290 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
1291 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
1292 gfc_current_ns->old_data = gfc_current_ns->data;
1293 for (;;)
1295 gfc_statement_label = NULL;
1296 gfc_buffer_error (true);
1298 if (gfc_at_eol ())
1299 gfc_advance_line ();
1301 gfc_skip_comments ();
1303 if (gfc_at_end ())
1305 st = ST_NONE;
1306 break;
1309 if (gfc_define_undef_line ())
1310 continue;
1312 old_locus = gfc_current_locus;
1314 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
1316 if (st != ST_NONE)
1317 break;
1320 gfc_buffer_error (false);
1322 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
1324 gfc_free_st_label (gfc_statement_label);
1325 gfc_statement_label = NULL;
1326 gfc_current_locus = old_locus;
1329 if (st != ST_NONE)
1330 check_statement_label (st);
1332 return st;
1336 /****************************** Parser ***********************************/
1338 /* The parser subroutines are of type 'try' that fail if the file ends
1339 unexpectedly. */
1341 /* Macros that expand to case-labels for various classes of
1342 statements. Start with executable statements that directly do
1343 things. */
1345 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1346 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1347 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1348 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1349 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1350 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1351 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1352 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1353 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1354 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1355 case ST_OMP_TARGET_UPDATE: case ST_ERROR_STOP: case ST_SYNC_ALL: \
1356 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1357 case ST_EVENT_POST: case ST_EVENT_WAIT: \
1358 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1359 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1361 /* Statements that mark other executable statements. */
1363 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1364 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1365 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1366 case ST_OMP_PARALLEL: \
1367 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1368 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1369 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1370 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1371 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1372 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1373 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1374 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1375 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1376 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1377 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1378 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1379 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1380 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1381 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1382 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1383 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: \
1384 case ST_CRITICAL: \
1385 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1386 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: \
1387 case ST_OACC_KERNELS_LOOP: case ST_OACC_ATOMIC
1389 /* Declaration statements */
1391 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1392 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1393 case ST_TYPE: case ST_INTERFACE: case ST_PROCEDURE: case ST_OACC_ROUTINE: \
1394 case ST_OACC_DECLARE
1396 /* OpenMP declaration statements. */
1398 #define case_omp_decl case ST_OMP_THREADPRIVATE: case ST_OMP_DECLARE_SIMD: \
1399 case ST_OMP_DECLARE_TARGET: case ST_OMP_DECLARE_REDUCTION
1401 /* Block end statements. Errors associated with interchanging these
1402 are detected in gfc_match_end(). */
1404 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1405 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1406 case ST_END_BLOCK: case ST_END_ASSOCIATE
1409 /* Push a new state onto the stack. */
1411 static void
1412 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
1414 p->state = new_state;
1415 p->previous = gfc_state_stack;
1416 p->sym = sym;
1417 p->head = p->tail = NULL;
1418 p->do_variable = NULL;
1419 if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
1420 p->ext.oacc_declare_clauses = NULL;
1422 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1423 construct statement was accepted right before pushing the state. Thus,
1424 the construct's gfc_code is available as tail of the parent state. */
1425 gcc_assert (gfc_state_stack);
1426 p->construct = gfc_state_stack->tail;
1428 gfc_state_stack = p;
1432 /* Pop the current state. */
1433 static void
1434 pop_state (void)
1436 gfc_state_stack = gfc_state_stack->previous;
1440 /* Try to find the given state in the state stack. */
1442 bool
1443 gfc_find_state (gfc_compile_state state)
1445 gfc_state_data *p;
1447 for (p = gfc_state_stack; p; p = p->previous)
1448 if (p->state == state)
1449 break;
1451 return (p == NULL) ? false : true;
1455 /* Starts a new level in the statement list. */
1457 static gfc_code *
1458 new_level (gfc_code *q)
1460 gfc_code *p;
1462 p = q->block = gfc_get_code (EXEC_NOP);
1464 gfc_state_stack->head = gfc_state_stack->tail = p;
1466 return p;
1470 /* Add the current new_st code structure and adds it to the current
1471 program unit. As a side-effect, it zeroes the new_st. */
1473 static gfc_code *
1474 add_statement (void)
1476 gfc_code *p;
1478 p = XCNEW (gfc_code);
1479 *p = new_st;
1481 p->loc = gfc_current_locus;
1483 if (gfc_state_stack->head == NULL)
1484 gfc_state_stack->head = p;
1485 else
1486 gfc_state_stack->tail->next = p;
1488 while (p->next != NULL)
1489 p = p->next;
1491 gfc_state_stack->tail = p;
1493 gfc_clear_new_st ();
1495 return p;
1499 /* Frees everything associated with the current statement. */
1501 static void
1502 undo_new_statement (void)
1504 gfc_free_statements (new_st.block);
1505 gfc_free_statements (new_st.next);
1506 gfc_free_statement (&new_st);
1507 gfc_clear_new_st ();
1511 /* If the current statement has a statement label, make sure that it
1512 is allowed to, or should have one. */
1514 static void
1515 check_statement_label (gfc_statement st)
1517 gfc_sl_type type;
1519 if (gfc_statement_label == NULL)
1521 if (st == ST_FORMAT)
1522 gfc_error ("FORMAT statement at %L does not have a statement label",
1523 &new_st.loc);
1524 return;
1527 switch (st)
1529 case ST_END_PROGRAM:
1530 case ST_END_FUNCTION:
1531 case ST_END_SUBROUTINE:
1532 case ST_ENDDO:
1533 case ST_ENDIF:
1534 case ST_END_SELECT:
1535 case ST_END_CRITICAL:
1536 case ST_END_BLOCK:
1537 case ST_END_ASSOCIATE:
1538 case_executable:
1539 case_exec_markers:
1540 if (st == ST_ENDDO || st == ST_CONTINUE)
1541 type = ST_LABEL_DO_TARGET;
1542 else
1543 type = ST_LABEL_TARGET;
1544 break;
1546 case ST_FORMAT:
1547 type = ST_LABEL_FORMAT;
1548 break;
1550 /* Statement labels are not restricted from appearing on a
1551 particular line. However, there are plenty of situations
1552 where the resulting label can't be referenced. */
1554 default:
1555 type = ST_LABEL_BAD_TARGET;
1556 break;
1559 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1561 new_st.here = gfc_statement_label;
1565 /* Figures out what the enclosing program unit is. This will be a
1566 function, subroutine, program, block data or module. */
1568 gfc_state_data *
1569 gfc_enclosing_unit (gfc_compile_state * result)
1571 gfc_state_data *p;
1573 for (p = gfc_state_stack; p; p = p->previous)
1574 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1575 || p->state == COMP_MODULE || p->state == COMP_SUBMODULE
1576 || p->state == COMP_BLOCK_DATA || p->state == COMP_PROGRAM)
1579 if (result != NULL)
1580 *result = p->state;
1581 return p;
1584 if (result != NULL)
1585 *result = COMP_PROGRAM;
1586 return NULL;
1590 /* Translate a statement enum to a string. */
1592 const char *
1593 gfc_ascii_statement (gfc_statement st)
1595 const char *p;
1597 switch (st)
1599 case ST_ARITHMETIC_IF:
1600 p = _("arithmetic IF");
1601 break;
1602 case ST_ALLOCATE:
1603 p = "ALLOCATE";
1604 break;
1605 case ST_ASSOCIATE:
1606 p = "ASSOCIATE";
1607 break;
1608 case ST_ATTR_DECL:
1609 p = _("attribute declaration");
1610 break;
1611 case ST_BACKSPACE:
1612 p = "BACKSPACE";
1613 break;
1614 case ST_BLOCK:
1615 p = "BLOCK";
1616 break;
1617 case ST_BLOCK_DATA:
1618 p = "BLOCK DATA";
1619 break;
1620 case ST_CALL:
1621 p = "CALL";
1622 break;
1623 case ST_CASE:
1624 p = "CASE";
1625 break;
1626 case ST_CLOSE:
1627 p = "CLOSE";
1628 break;
1629 case ST_COMMON:
1630 p = "COMMON";
1631 break;
1632 case ST_CONTINUE:
1633 p = "CONTINUE";
1634 break;
1635 case ST_CONTAINS:
1636 p = "CONTAINS";
1637 break;
1638 case ST_CRITICAL:
1639 p = "CRITICAL";
1640 break;
1641 case ST_CYCLE:
1642 p = "CYCLE";
1643 break;
1644 case ST_DATA_DECL:
1645 p = _("data declaration");
1646 break;
1647 case ST_DATA:
1648 p = "DATA";
1649 break;
1650 case ST_DEALLOCATE:
1651 p = "DEALLOCATE";
1652 break;
1653 case ST_MAP:
1654 p = "MAP";
1655 break;
1656 case ST_UNION:
1657 p = "UNION";
1658 break;
1659 case ST_STRUCTURE_DECL:
1660 p = "STRUCTURE";
1661 break;
1662 case ST_DERIVED_DECL:
1663 p = _("derived type declaration");
1664 break;
1665 case ST_DO:
1666 p = "DO";
1667 break;
1668 case ST_ELSE:
1669 p = "ELSE";
1670 break;
1671 case ST_ELSEIF:
1672 p = "ELSE IF";
1673 break;
1674 case ST_ELSEWHERE:
1675 p = "ELSEWHERE";
1676 break;
1677 case ST_EVENT_POST:
1678 p = "EVENT POST";
1679 break;
1680 case ST_EVENT_WAIT:
1681 p = "EVENT WAIT";
1682 break;
1683 case ST_END_ASSOCIATE:
1684 p = "END ASSOCIATE";
1685 break;
1686 case ST_END_BLOCK:
1687 p = "END BLOCK";
1688 break;
1689 case ST_END_BLOCK_DATA:
1690 p = "END BLOCK DATA";
1691 break;
1692 case ST_END_CRITICAL:
1693 p = "END CRITICAL";
1694 break;
1695 case ST_ENDDO:
1696 p = "END DO";
1697 break;
1698 case ST_END_FILE:
1699 p = "END FILE";
1700 break;
1701 case ST_END_FORALL:
1702 p = "END FORALL";
1703 break;
1704 case ST_END_FUNCTION:
1705 p = "END FUNCTION";
1706 break;
1707 case ST_ENDIF:
1708 p = "END IF";
1709 break;
1710 case ST_END_INTERFACE:
1711 p = "END INTERFACE";
1712 break;
1713 case ST_END_MODULE:
1714 p = "END MODULE";
1715 break;
1716 case ST_END_SUBMODULE:
1717 p = "END SUBMODULE";
1718 break;
1719 case ST_END_PROGRAM:
1720 p = "END PROGRAM";
1721 break;
1722 case ST_END_SELECT:
1723 p = "END SELECT";
1724 break;
1725 case ST_END_SUBROUTINE:
1726 p = "END SUBROUTINE";
1727 break;
1728 case ST_END_WHERE:
1729 p = "END WHERE";
1730 break;
1731 case ST_END_STRUCTURE:
1732 p = "END STRUCTURE";
1733 break;
1734 case ST_END_UNION:
1735 p = "END UNION";
1736 break;
1737 case ST_END_MAP:
1738 p = "END MAP";
1739 break;
1740 case ST_END_TYPE:
1741 p = "END TYPE";
1742 break;
1743 case ST_ENTRY:
1744 p = "ENTRY";
1745 break;
1746 case ST_EQUIVALENCE:
1747 p = "EQUIVALENCE";
1748 break;
1749 case ST_ERROR_STOP:
1750 p = "ERROR STOP";
1751 break;
1752 case ST_EXIT:
1753 p = "EXIT";
1754 break;
1755 case ST_FLUSH:
1756 p = "FLUSH";
1757 break;
1758 case ST_FORALL_BLOCK: /* Fall through */
1759 case ST_FORALL:
1760 p = "FORALL";
1761 break;
1762 case ST_FORMAT:
1763 p = "FORMAT";
1764 break;
1765 case ST_FUNCTION:
1766 p = "FUNCTION";
1767 break;
1768 case ST_GENERIC:
1769 p = "GENERIC";
1770 break;
1771 case ST_GOTO:
1772 p = "GOTO";
1773 break;
1774 case ST_IF_BLOCK:
1775 p = _("block IF");
1776 break;
1777 case ST_IMPLICIT:
1778 p = "IMPLICIT";
1779 break;
1780 case ST_IMPLICIT_NONE:
1781 p = "IMPLICIT NONE";
1782 break;
1783 case ST_IMPLIED_ENDDO:
1784 p = _("implied END DO");
1785 break;
1786 case ST_IMPORT:
1787 p = "IMPORT";
1788 break;
1789 case ST_INQUIRE:
1790 p = "INQUIRE";
1791 break;
1792 case ST_INTERFACE:
1793 p = "INTERFACE";
1794 break;
1795 case ST_LOCK:
1796 p = "LOCK";
1797 break;
1798 case ST_PARAMETER:
1799 p = "PARAMETER";
1800 break;
1801 case ST_PRIVATE:
1802 p = "PRIVATE";
1803 break;
1804 case ST_PUBLIC:
1805 p = "PUBLIC";
1806 break;
1807 case ST_MODULE:
1808 p = "MODULE";
1809 break;
1810 case ST_SUBMODULE:
1811 p = "SUBMODULE";
1812 break;
1813 case ST_PAUSE:
1814 p = "PAUSE";
1815 break;
1816 case ST_MODULE_PROC:
1817 p = "MODULE PROCEDURE";
1818 break;
1819 case ST_NAMELIST:
1820 p = "NAMELIST";
1821 break;
1822 case ST_NULLIFY:
1823 p = "NULLIFY";
1824 break;
1825 case ST_OPEN:
1826 p = "OPEN";
1827 break;
1828 case ST_PROGRAM:
1829 p = "PROGRAM";
1830 break;
1831 case ST_PROCEDURE:
1832 p = "PROCEDURE";
1833 break;
1834 case ST_READ:
1835 p = "READ";
1836 break;
1837 case ST_RETURN:
1838 p = "RETURN";
1839 break;
1840 case ST_REWIND:
1841 p = "REWIND";
1842 break;
1843 case ST_STOP:
1844 p = "STOP";
1845 break;
1846 case ST_SYNC_ALL:
1847 p = "SYNC ALL";
1848 break;
1849 case ST_SYNC_IMAGES:
1850 p = "SYNC IMAGES";
1851 break;
1852 case ST_SYNC_MEMORY:
1853 p = "SYNC MEMORY";
1854 break;
1855 case ST_SUBROUTINE:
1856 p = "SUBROUTINE";
1857 break;
1858 case ST_TYPE:
1859 p = "TYPE";
1860 break;
1861 case ST_UNLOCK:
1862 p = "UNLOCK";
1863 break;
1864 case ST_USE:
1865 p = "USE";
1866 break;
1867 case ST_WHERE_BLOCK: /* Fall through */
1868 case ST_WHERE:
1869 p = "WHERE";
1870 break;
1871 case ST_WAIT:
1872 p = "WAIT";
1873 break;
1874 case ST_WRITE:
1875 p = "WRITE";
1876 break;
1877 case ST_ASSIGNMENT:
1878 p = _("assignment");
1879 break;
1880 case ST_POINTER_ASSIGNMENT:
1881 p = _("pointer assignment");
1882 break;
1883 case ST_SELECT_CASE:
1884 p = "SELECT CASE";
1885 break;
1886 case ST_SELECT_TYPE:
1887 p = "SELECT TYPE";
1888 break;
1889 case ST_TYPE_IS:
1890 p = "TYPE IS";
1891 break;
1892 case ST_CLASS_IS:
1893 p = "CLASS IS";
1894 break;
1895 case ST_SEQUENCE:
1896 p = "SEQUENCE";
1897 break;
1898 case ST_SIMPLE_IF:
1899 p = _("simple IF");
1900 break;
1901 case ST_STATEMENT_FUNCTION:
1902 p = "STATEMENT FUNCTION";
1903 break;
1904 case ST_LABEL_ASSIGNMENT:
1905 p = "LABEL ASSIGNMENT";
1906 break;
1907 case ST_ENUM:
1908 p = "ENUM DEFINITION";
1909 break;
1910 case ST_ENUMERATOR:
1911 p = "ENUMERATOR DEFINITION";
1912 break;
1913 case ST_END_ENUM:
1914 p = "END ENUM";
1915 break;
1916 case ST_OACC_PARALLEL_LOOP:
1917 p = "!$ACC PARALLEL LOOP";
1918 break;
1919 case ST_OACC_END_PARALLEL_LOOP:
1920 p = "!$ACC END PARALLEL LOOP";
1921 break;
1922 case ST_OACC_PARALLEL:
1923 p = "!$ACC PARALLEL";
1924 break;
1925 case ST_OACC_END_PARALLEL:
1926 p = "!$ACC END PARALLEL";
1927 break;
1928 case ST_OACC_KERNELS:
1929 p = "!$ACC KERNELS";
1930 break;
1931 case ST_OACC_END_KERNELS:
1932 p = "!$ACC END KERNELS";
1933 break;
1934 case ST_OACC_KERNELS_LOOP:
1935 p = "!$ACC KERNELS LOOP";
1936 break;
1937 case ST_OACC_END_KERNELS_LOOP:
1938 p = "!$ACC END KERNELS LOOP";
1939 break;
1940 case ST_OACC_DATA:
1941 p = "!$ACC DATA";
1942 break;
1943 case ST_OACC_END_DATA:
1944 p = "!$ACC END DATA";
1945 break;
1946 case ST_OACC_HOST_DATA:
1947 p = "!$ACC HOST_DATA";
1948 break;
1949 case ST_OACC_END_HOST_DATA:
1950 p = "!$ACC END HOST_DATA";
1951 break;
1952 case ST_OACC_LOOP:
1953 p = "!$ACC LOOP";
1954 break;
1955 case ST_OACC_END_LOOP:
1956 p = "!$ACC END LOOP";
1957 break;
1958 case ST_OACC_DECLARE:
1959 p = "!$ACC DECLARE";
1960 break;
1961 case ST_OACC_UPDATE:
1962 p = "!$ACC UPDATE";
1963 break;
1964 case ST_OACC_WAIT:
1965 p = "!$ACC WAIT";
1966 break;
1967 case ST_OACC_CACHE:
1968 p = "!$ACC CACHE";
1969 break;
1970 case ST_OACC_ENTER_DATA:
1971 p = "!$ACC ENTER DATA";
1972 break;
1973 case ST_OACC_EXIT_DATA:
1974 p = "!$ACC EXIT DATA";
1975 break;
1976 case ST_OACC_ROUTINE:
1977 p = "!$ACC ROUTINE";
1978 break;
1979 case ST_OACC_ATOMIC:
1980 p = "!ACC ATOMIC";
1981 break;
1982 case ST_OACC_END_ATOMIC:
1983 p = "!ACC END ATOMIC";
1984 break;
1985 case ST_OMP_ATOMIC:
1986 p = "!$OMP ATOMIC";
1987 break;
1988 case ST_OMP_BARRIER:
1989 p = "!$OMP BARRIER";
1990 break;
1991 case ST_OMP_CANCEL:
1992 p = "!$OMP CANCEL";
1993 break;
1994 case ST_OMP_CANCELLATION_POINT:
1995 p = "!$OMP CANCELLATION POINT";
1996 break;
1997 case ST_OMP_CRITICAL:
1998 p = "!$OMP CRITICAL";
1999 break;
2000 case ST_OMP_DECLARE_REDUCTION:
2001 p = "!$OMP DECLARE REDUCTION";
2002 break;
2003 case ST_OMP_DECLARE_SIMD:
2004 p = "!$OMP DECLARE SIMD";
2005 break;
2006 case ST_OMP_DECLARE_TARGET:
2007 p = "!$OMP DECLARE TARGET";
2008 break;
2009 case ST_OMP_DISTRIBUTE:
2010 p = "!$OMP DISTRIBUTE";
2011 break;
2012 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
2013 p = "!$OMP DISTRIBUTE PARALLEL DO";
2014 break;
2015 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
2016 p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
2017 break;
2018 case ST_OMP_DISTRIBUTE_SIMD:
2019 p = "!$OMP DISTRIBUTE SIMD";
2020 break;
2021 case ST_OMP_DO:
2022 p = "!$OMP DO";
2023 break;
2024 case ST_OMP_DO_SIMD:
2025 p = "!$OMP DO SIMD";
2026 break;
2027 case ST_OMP_END_ATOMIC:
2028 p = "!$OMP END ATOMIC";
2029 break;
2030 case ST_OMP_END_CRITICAL:
2031 p = "!$OMP END CRITICAL";
2032 break;
2033 case ST_OMP_END_DISTRIBUTE:
2034 p = "!$OMP END DISTRIBUTE";
2035 break;
2036 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
2037 p = "!$OMP END DISTRIBUTE PARALLEL DO";
2038 break;
2039 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
2040 p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
2041 break;
2042 case ST_OMP_END_DISTRIBUTE_SIMD:
2043 p = "!$OMP END DISTRIBUTE SIMD";
2044 break;
2045 case ST_OMP_END_DO:
2046 p = "!$OMP END DO";
2047 break;
2048 case ST_OMP_END_DO_SIMD:
2049 p = "!$OMP END DO SIMD";
2050 break;
2051 case ST_OMP_END_SIMD:
2052 p = "!$OMP END SIMD";
2053 break;
2054 case ST_OMP_END_MASTER:
2055 p = "!$OMP END MASTER";
2056 break;
2057 case ST_OMP_END_ORDERED:
2058 p = "!$OMP END ORDERED";
2059 break;
2060 case ST_OMP_END_PARALLEL:
2061 p = "!$OMP END PARALLEL";
2062 break;
2063 case ST_OMP_END_PARALLEL_DO:
2064 p = "!$OMP END PARALLEL DO";
2065 break;
2066 case ST_OMP_END_PARALLEL_DO_SIMD:
2067 p = "!$OMP END PARALLEL DO SIMD";
2068 break;
2069 case ST_OMP_END_PARALLEL_SECTIONS:
2070 p = "!$OMP END PARALLEL SECTIONS";
2071 break;
2072 case ST_OMP_END_PARALLEL_WORKSHARE:
2073 p = "!$OMP END PARALLEL WORKSHARE";
2074 break;
2075 case ST_OMP_END_SECTIONS:
2076 p = "!$OMP END SECTIONS";
2077 break;
2078 case ST_OMP_END_SINGLE:
2079 p = "!$OMP END SINGLE";
2080 break;
2081 case ST_OMP_END_TASK:
2082 p = "!$OMP END TASK";
2083 break;
2084 case ST_OMP_END_TARGET:
2085 p = "!$OMP END TARGET";
2086 break;
2087 case ST_OMP_END_TARGET_DATA:
2088 p = "!$OMP END TARGET DATA";
2089 break;
2090 case ST_OMP_END_TARGET_TEAMS:
2091 p = "!$OMP END TARGET TEAMS";
2092 break;
2093 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
2094 p = "!$OMP END TARGET TEAMS DISTRIBUTE";
2095 break;
2096 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2097 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2098 break;
2099 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2100 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2101 break;
2102 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
2103 p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2104 break;
2105 case ST_OMP_END_TASKGROUP:
2106 p = "!$OMP END TASKGROUP";
2107 break;
2108 case ST_OMP_END_TEAMS:
2109 p = "!$OMP END TEAMS";
2110 break;
2111 case ST_OMP_END_TEAMS_DISTRIBUTE:
2112 p = "!$OMP END TEAMS DISTRIBUTE";
2113 break;
2114 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
2115 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2116 break;
2117 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2118 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2119 break;
2120 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
2121 p = "!$OMP END TEAMS DISTRIBUTE SIMD";
2122 break;
2123 case ST_OMP_END_WORKSHARE:
2124 p = "!$OMP END WORKSHARE";
2125 break;
2126 case ST_OMP_FLUSH:
2127 p = "!$OMP FLUSH";
2128 break;
2129 case ST_OMP_MASTER:
2130 p = "!$OMP MASTER";
2131 break;
2132 case ST_OMP_ORDERED:
2133 p = "!$OMP ORDERED";
2134 break;
2135 case ST_OMP_PARALLEL:
2136 p = "!$OMP PARALLEL";
2137 break;
2138 case ST_OMP_PARALLEL_DO:
2139 p = "!$OMP PARALLEL DO";
2140 break;
2141 case ST_OMP_PARALLEL_DO_SIMD:
2142 p = "!$OMP PARALLEL DO SIMD";
2143 break;
2144 case ST_OMP_PARALLEL_SECTIONS:
2145 p = "!$OMP PARALLEL SECTIONS";
2146 break;
2147 case ST_OMP_PARALLEL_WORKSHARE:
2148 p = "!$OMP PARALLEL WORKSHARE";
2149 break;
2150 case ST_OMP_SECTIONS:
2151 p = "!$OMP SECTIONS";
2152 break;
2153 case ST_OMP_SECTION:
2154 p = "!$OMP SECTION";
2155 break;
2156 case ST_OMP_SIMD:
2157 p = "!$OMP SIMD";
2158 break;
2159 case ST_OMP_SINGLE:
2160 p = "!$OMP SINGLE";
2161 break;
2162 case ST_OMP_TARGET:
2163 p = "!$OMP TARGET";
2164 break;
2165 case ST_OMP_TARGET_DATA:
2166 p = "!$OMP TARGET DATA";
2167 break;
2168 case ST_OMP_TARGET_TEAMS:
2169 p = "!$OMP TARGET TEAMS";
2170 break;
2171 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
2172 p = "!$OMP TARGET TEAMS DISTRIBUTE";
2173 break;
2174 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2175 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2176 break;
2177 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2178 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2179 break;
2180 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2181 p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2182 break;
2183 case ST_OMP_TARGET_UPDATE:
2184 p = "!$OMP TARGET UPDATE";
2185 break;
2186 case ST_OMP_TASK:
2187 p = "!$OMP TASK";
2188 break;
2189 case ST_OMP_TASKGROUP:
2190 p = "!$OMP TASKGROUP";
2191 break;
2192 case ST_OMP_TASKWAIT:
2193 p = "!$OMP TASKWAIT";
2194 break;
2195 case ST_OMP_TASKYIELD:
2196 p = "!$OMP TASKYIELD";
2197 break;
2198 case ST_OMP_TEAMS:
2199 p = "!$OMP TEAMS";
2200 break;
2201 case ST_OMP_TEAMS_DISTRIBUTE:
2202 p = "!$OMP TEAMS DISTRIBUTE";
2203 break;
2204 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2205 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2206 break;
2207 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2208 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2209 break;
2210 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
2211 p = "!$OMP TEAMS DISTRIBUTE SIMD";
2212 break;
2213 case ST_OMP_THREADPRIVATE:
2214 p = "!$OMP THREADPRIVATE";
2215 break;
2216 case ST_OMP_WORKSHARE:
2217 p = "!$OMP WORKSHARE";
2218 break;
2219 default:
2220 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2223 return p;
2227 /* Create a symbol for the main program and assign it to ns->proc_name. */
2229 static void
2230 main_program_symbol (gfc_namespace *ns, const char *name)
2232 gfc_symbol *main_program;
2233 symbol_attribute attr;
2235 gfc_get_symbol (name, ns, &main_program);
2236 gfc_clear_attr (&attr);
2237 attr.flavor = FL_PROGRAM;
2238 attr.proc = PROC_UNKNOWN;
2239 attr.subroutine = 1;
2240 attr.access = ACCESS_PUBLIC;
2241 attr.is_main_program = 1;
2242 main_program->attr = attr;
2243 main_program->declared_at = gfc_current_locus;
2244 ns->proc_name = main_program;
2245 gfc_commit_symbols ();
2249 /* Do whatever is necessary to accept the last statement. */
2251 static void
2252 accept_statement (gfc_statement st)
2254 switch (st)
2256 case ST_IMPLICIT_NONE:
2257 case ST_IMPLICIT:
2258 break;
2260 case ST_FUNCTION:
2261 case ST_SUBROUTINE:
2262 case ST_MODULE:
2263 case ST_SUBMODULE:
2264 gfc_current_ns->proc_name = gfc_new_block;
2265 break;
2267 /* If the statement is the end of a block, lay down a special code
2268 that allows a branch to the end of the block from within the
2269 construct. IF and SELECT are treated differently from DO
2270 (where EXEC_NOP is added inside the loop) for two
2271 reasons:
2272 1. END DO has a meaning in the sense that after a GOTO to
2273 it, the loop counter must be increased.
2274 2. IF blocks and SELECT blocks can consist of multiple
2275 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2276 Putting the label before the END IF would make the jump
2277 from, say, the ELSE IF block to the END IF illegal. */
2279 case ST_ENDIF:
2280 case ST_END_SELECT:
2281 case ST_END_CRITICAL:
2282 if (gfc_statement_label != NULL)
2284 new_st.op = EXEC_END_NESTED_BLOCK;
2285 add_statement ();
2287 break;
2289 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2290 one parallel block. Thus, we add the special code to the nested block
2291 itself, instead of the parent one. */
2292 case ST_END_BLOCK:
2293 case ST_END_ASSOCIATE:
2294 if (gfc_statement_label != NULL)
2296 new_st.op = EXEC_END_BLOCK;
2297 add_statement ();
2299 break;
2301 /* The end-of-program unit statements do not get the special
2302 marker and require a statement of some sort if they are a
2303 branch target. */
2305 case ST_END_PROGRAM:
2306 case ST_END_FUNCTION:
2307 case ST_END_SUBROUTINE:
2308 if (gfc_statement_label != NULL)
2310 new_st.op = EXEC_RETURN;
2311 add_statement ();
2313 else
2315 new_st.op = EXEC_END_PROCEDURE;
2316 add_statement ();
2319 break;
2321 case ST_ENTRY:
2322 case_executable:
2323 case_exec_markers:
2324 add_statement ();
2325 break;
2327 default:
2328 break;
2331 gfc_commit_symbols ();
2332 gfc_warning_check ();
2333 gfc_clear_new_st ();
2337 /* Undo anything tentative that has been built for the current
2338 statement. */
2340 static void
2341 reject_statement (void)
2343 /* Revert to the previous charlen chain. */
2344 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
2345 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
2347 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
2348 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
2350 gfc_reject_data (gfc_current_ns);
2352 gfc_new_block = NULL;
2353 gfc_undo_symbols ();
2354 gfc_clear_warning ();
2355 undo_new_statement ();
2359 /* Generic complaint about an out of order statement. We also do
2360 whatever is necessary to clean up. */
2362 static void
2363 unexpected_statement (gfc_statement st)
2365 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
2367 reject_statement ();
2371 /* Given the next statement seen by the matcher, make sure that it is
2372 in proper order with the last. This subroutine is initialized by
2373 calling it with an argument of ST_NONE. If there is a problem, we
2374 issue an error and return false. Otherwise we return true.
2376 Individual parsers need to verify that the statements seen are
2377 valid before calling here, i.e., ENTRY statements are not allowed in
2378 INTERFACE blocks. The following diagram is taken from the standard:
2380 +---------------------------------------+
2381 | program subroutine function module |
2382 +---------------------------------------+
2383 | use |
2384 +---------------------------------------+
2385 | import |
2386 +---------------------------------------+
2387 | | implicit none |
2388 | +-----------+------------------+
2389 | | parameter | implicit |
2390 | +-----------+------------------+
2391 | format | | derived type |
2392 | entry | parameter | interface |
2393 | | data | specification |
2394 | | | statement func |
2395 | +-----------+------------------+
2396 | | data | executable |
2397 +--------+-----------+------------------+
2398 | contains |
2399 +---------------------------------------+
2400 | internal module/subprogram |
2401 +---------------------------------------+
2402 | end |
2403 +---------------------------------------+
2407 enum state_order
2409 ORDER_START,
2410 ORDER_USE,
2411 ORDER_IMPORT,
2412 ORDER_IMPLICIT_NONE,
2413 ORDER_IMPLICIT,
2414 ORDER_SPEC,
2415 ORDER_EXEC
2418 typedef struct
2420 enum state_order state;
2421 gfc_statement last_statement;
2422 locus where;
2424 st_state;
2426 static bool
2427 verify_st_order (st_state *p, gfc_statement st, bool silent)
2430 switch (st)
2432 case ST_NONE:
2433 p->state = ORDER_START;
2434 break;
2436 case ST_USE:
2437 if (p->state > ORDER_USE)
2438 goto order;
2439 p->state = ORDER_USE;
2440 break;
2442 case ST_IMPORT:
2443 if (p->state > ORDER_IMPORT)
2444 goto order;
2445 p->state = ORDER_IMPORT;
2446 break;
2448 case ST_IMPLICIT_NONE:
2449 if (p->state > ORDER_IMPLICIT)
2450 goto order;
2452 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2453 statement disqualifies a USE but not an IMPLICIT NONE.
2454 Duplicate IMPLICIT NONEs are caught when the implicit types
2455 are set. */
2457 p->state = ORDER_IMPLICIT_NONE;
2458 break;
2460 case ST_IMPLICIT:
2461 if (p->state > ORDER_IMPLICIT)
2462 goto order;
2463 p->state = ORDER_IMPLICIT;
2464 break;
2466 case ST_FORMAT:
2467 case ST_ENTRY:
2468 if (p->state < ORDER_IMPLICIT_NONE)
2469 p->state = ORDER_IMPLICIT_NONE;
2470 break;
2472 case ST_PARAMETER:
2473 if (p->state >= ORDER_EXEC)
2474 goto order;
2475 if (p->state < ORDER_IMPLICIT)
2476 p->state = ORDER_IMPLICIT;
2477 break;
2479 case ST_DATA:
2480 if (p->state < ORDER_SPEC)
2481 p->state = ORDER_SPEC;
2482 break;
2484 case ST_PUBLIC:
2485 case ST_PRIVATE:
2486 case ST_STRUCTURE_DECL:
2487 case ST_DERIVED_DECL:
2488 case_decl:
2489 if (p->state >= ORDER_EXEC)
2490 goto order;
2491 if (p->state < ORDER_SPEC)
2492 p->state = ORDER_SPEC;
2493 break;
2495 case_omp_decl:
2496 /* The OpenMP directives have to be somewhere in the specification
2497 part, but there are no further requirements on their ordering.
2498 Thus don't adjust p->state, just ignore them. */
2499 if (p->state >= ORDER_EXEC)
2500 goto order;
2501 break;
2503 case_executable:
2504 case_exec_markers:
2505 if (p->state < ORDER_EXEC)
2506 p->state = ORDER_EXEC;
2507 break;
2509 default:
2510 return false;
2513 /* All is well, record the statement in case we need it next time. */
2514 p->where = gfc_current_locus;
2515 p->last_statement = st;
2516 return true;
2518 order:
2519 if (!silent)
2520 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2521 gfc_ascii_statement (st),
2522 gfc_ascii_statement (p->last_statement), &p->where);
2524 return false;
2528 /* Handle an unexpected end of file. This is a show-stopper... */
2530 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
2532 static void
2533 unexpected_eof (void)
2535 gfc_state_data *p;
2537 gfc_error ("Unexpected end of file in %qs", gfc_source_file);
2539 /* Memory cleanup. Move to "second to last". */
2540 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
2541 p = p->previous);
2543 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
2544 gfc_done_2 ();
2546 longjmp (eof_buf, 1);
2550 /* Parse the CONTAINS section of a derived type definition. */
2552 gfc_access gfc_typebound_default_access;
2554 static bool
2555 parse_derived_contains (void)
2557 gfc_state_data s;
2558 bool seen_private = false;
2559 bool seen_comps = false;
2560 bool error_flag = false;
2561 bool to_finish;
2563 gcc_assert (gfc_current_state () == COMP_DERIVED);
2564 gcc_assert (gfc_current_block ());
2566 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2567 section. */
2568 if (gfc_current_block ()->attr.sequence)
2569 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2570 " section at %C", gfc_current_block ()->name);
2571 if (gfc_current_block ()->attr.is_bind_c)
2572 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2573 " section at %C", gfc_current_block ()->name);
2575 accept_statement (ST_CONTAINS);
2576 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
2578 gfc_typebound_default_access = ACCESS_PUBLIC;
2580 to_finish = false;
2581 while (!to_finish)
2583 gfc_statement st;
2584 st = next_statement ();
2585 switch (st)
2587 case ST_NONE:
2588 unexpected_eof ();
2589 break;
2591 case ST_DATA_DECL:
2592 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2593 goto error;
2595 case ST_PROCEDURE:
2596 if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
2597 goto error;
2599 accept_statement (ST_PROCEDURE);
2600 seen_comps = true;
2601 break;
2603 case ST_GENERIC:
2604 if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
2605 goto error;
2607 accept_statement (ST_GENERIC);
2608 seen_comps = true;
2609 break;
2611 case ST_FINAL:
2612 if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
2613 " at %C"))
2614 goto error;
2616 accept_statement (ST_FINAL);
2617 seen_comps = true;
2618 break;
2620 case ST_END_TYPE:
2621 to_finish = true;
2623 if (!seen_comps
2624 && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
2625 "at %C with empty CONTAINS section")))
2626 goto error;
2628 /* ST_END_TYPE is accepted by parse_derived after return. */
2629 break;
2631 case ST_PRIVATE:
2632 if (!gfc_find_state (COMP_MODULE))
2634 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2635 "a MODULE");
2636 goto error;
2639 if (seen_comps)
2641 gfc_error ("PRIVATE statement at %C must precede procedure"
2642 " bindings");
2643 goto error;
2646 if (seen_private)
2648 gfc_error ("Duplicate PRIVATE statement at %C");
2649 goto error;
2652 accept_statement (ST_PRIVATE);
2653 gfc_typebound_default_access = ACCESS_PRIVATE;
2654 seen_private = true;
2655 break;
2657 case ST_SEQUENCE:
2658 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2659 goto error;
2661 case ST_CONTAINS:
2662 gfc_error ("Already inside a CONTAINS block at %C");
2663 goto error;
2665 default:
2666 unexpected_statement (st);
2667 break;
2670 continue;
2672 error:
2673 error_flag = true;
2674 reject_statement ();
2677 pop_state ();
2678 gcc_assert (gfc_current_state () == COMP_DERIVED);
2680 return error_flag;
2684 /* Set attributes for the parent symbol based on the attributes of a component
2685 and raise errors if conflicting attributes are found for the component. */
2687 static void
2688 check_component (gfc_symbol *sym, gfc_component *c, gfc_component **lockp,
2689 gfc_component **eventp)
2691 bool coarray, lock_type, event_type, allocatable, pointer;
2692 coarray = lock_type = event_type = allocatable = pointer = false;
2693 gfc_component *lock_comp = NULL, *event_comp = NULL;
2695 if (lockp) lock_comp = *lockp;
2696 if (eventp) event_comp = *eventp;
2698 /* Look for allocatable components. */
2699 if (c->attr.allocatable
2700 || (c->ts.type == BT_CLASS && c->attr.class_ok
2701 && CLASS_DATA (c)->attr.allocatable)
2702 || (c->ts.type == BT_DERIVED && !c->attr.pointer
2703 && c->ts.u.derived->attr.alloc_comp))
2705 allocatable = true;
2706 sym->attr.alloc_comp = 1;
2709 /* Look for pointer components. */
2710 if (c->attr.pointer
2711 || (c->ts.type == BT_CLASS && c->attr.class_ok
2712 && CLASS_DATA (c)->attr.class_pointer)
2713 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2715 pointer = true;
2716 sym->attr.pointer_comp = 1;
2719 /* Look for procedure pointer components. */
2720 if (c->attr.proc_pointer
2721 || (c->ts.type == BT_DERIVED
2722 && c->ts.u.derived->attr.proc_pointer_comp))
2723 sym->attr.proc_pointer_comp = 1;
2725 /* Looking for coarray components. */
2726 if (c->attr.codimension
2727 || (c->ts.type == BT_CLASS && c->attr.class_ok
2728 && CLASS_DATA (c)->attr.codimension))
2730 coarray = true;
2731 sym->attr.coarray_comp = 1;
2734 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
2735 && !c->attr.pointer)
2737 coarray = true;
2738 sym->attr.coarray_comp = 1;
2741 /* Looking for lock_type components. */
2742 if ((c->ts.type == BT_DERIVED
2743 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2744 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2745 || (c->ts.type == BT_CLASS && c->attr.class_ok
2746 && CLASS_DATA (c)->ts.u.derived->from_intmod
2747 == INTMOD_ISO_FORTRAN_ENV
2748 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2749 == ISOFORTRAN_LOCK_TYPE)
2750 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
2751 && !allocatable && !pointer))
2753 lock_type = 1;
2754 lock_comp = c;
2755 sym->attr.lock_comp = 1;
2758 /* Looking for event_type components. */
2759 if ((c->ts.type == BT_DERIVED
2760 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2761 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2762 || (c->ts.type == BT_CLASS && c->attr.class_ok
2763 && CLASS_DATA (c)->ts.u.derived->from_intmod
2764 == INTMOD_ISO_FORTRAN_ENV
2765 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2766 == ISOFORTRAN_EVENT_TYPE)
2767 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.event_comp
2768 && !allocatable && !pointer))
2770 event_type = 1;
2771 event_comp = c;
2772 sym->attr.event_comp = 1;
2775 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2776 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2777 unless there are nondirect [allocatable or pointer] components
2778 involved (cf. 1.3.33.1 and 1.3.33.3). */
2780 if (pointer && !coarray && lock_type)
2781 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2782 "codimension or be a subcomponent of a coarray, "
2783 "which is not possible as the component has the "
2784 "pointer attribute", c->name, &c->loc);
2785 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2786 && c->ts.u.derived->attr.lock_comp)
2787 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2788 "of type LOCK_TYPE, which must have a codimension or be a "
2789 "subcomponent of a coarray", c->name, &c->loc);
2791 if (lock_type && allocatable && !coarray)
2792 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2793 "a codimension", c->name, &c->loc);
2794 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
2795 && c->ts.u.derived->attr.lock_comp)
2796 gfc_error ("Allocatable component %s at %L must have a codimension as "
2797 "it has a noncoarray subcomponent of type LOCK_TYPE",
2798 c->name, &c->loc);
2800 if (sym->attr.coarray_comp && !coarray && lock_type)
2801 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2802 "subcomponent of type LOCK_TYPE must have a codimension or "
2803 "be a subcomponent of a coarray. (Variables of type %s may "
2804 "not have a codimension as already a coarray "
2805 "subcomponent exists)", c->name, &c->loc, sym->name);
2807 if (sym->attr.lock_comp && coarray && !lock_type)
2808 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2809 "subcomponent of type LOCK_TYPE must have a codimension or "
2810 "be a subcomponent of a coarray. (Variables of type %s may "
2811 "not have a codimension as %s at %L has a codimension or a "
2812 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
2813 sym->name, c->name, &c->loc);
2815 /* Similarly for EVENT TYPE. */
2817 if (pointer && !coarray && event_type)
2818 gfc_error ("Component %s at %L of type EVENT_TYPE must have a "
2819 "codimension or be a subcomponent of a coarray, "
2820 "which is not possible as the component has the "
2821 "pointer attribute", c->name, &c->loc);
2822 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2823 && c->ts.u.derived->attr.event_comp)
2824 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2825 "of type EVENT_TYPE, which must have a codimension or be a "
2826 "subcomponent of a coarray", c->name, &c->loc);
2828 if (event_type && allocatable && !coarray)
2829 gfc_error ("Allocatable component %s at %L of type EVENT_TYPE must have "
2830 "a codimension", c->name, &c->loc);
2831 else if (event_type && allocatable && c->ts.type == BT_DERIVED
2832 && c->ts.u.derived->attr.event_comp)
2833 gfc_error ("Allocatable component %s at %L must have a codimension as "
2834 "it has a noncoarray subcomponent of type EVENT_TYPE",
2835 c->name, &c->loc);
2837 if (sym->attr.coarray_comp && !coarray && event_type)
2838 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
2839 "subcomponent of type EVENT_TYPE must have a codimension or "
2840 "be a subcomponent of a coarray. (Variables of type %s may "
2841 "not have a codimension as already a coarray "
2842 "subcomponent exists)", c->name, &c->loc, sym->name);
2844 if (sym->attr.event_comp && coarray && !event_type)
2845 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
2846 "subcomponent of type EVENT_TYPE must have a codimension or "
2847 "be a subcomponent of a coarray. (Variables of type %s may "
2848 "not have a codimension as %s at %L has a codimension or a "
2849 "coarray subcomponent)", event_comp->name, &event_comp->loc,
2850 sym->name, c->name, &c->loc);
2852 /* Look for private components. */
2853 if (sym->component_access == ACCESS_PRIVATE
2854 || c->attr.access == ACCESS_PRIVATE
2855 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
2856 sym->attr.private_comp = 1;
2858 if (lockp) *lockp = lock_comp;
2859 if (eventp) *eventp = event_comp;
2863 static void parse_struct_map (gfc_statement);
2865 /* Parse a union component definition within a structure definition. */
2867 static void
2868 parse_union (void)
2870 int compiling;
2871 gfc_statement st;
2872 gfc_state_data s;
2873 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
2874 gfc_symbol *un;
2876 accept_statement(ST_UNION);
2877 push_state (&s, COMP_UNION, gfc_new_block);
2878 un = gfc_new_block;
2880 compiling = 1;
2882 while (compiling)
2884 st = next_statement ();
2885 /* Only MAP declarations valid within a union. */
2886 switch (st)
2888 case ST_NONE:
2889 unexpected_eof ();
2891 case ST_MAP:
2892 accept_statement (ST_MAP);
2893 parse_struct_map (ST_MAP);
2894 /* Add a component to the union for each map. */
2895 if (!gfc_add_component (un, gfc_new_block->name, &c))
2897 gfc_internal_error ("failed to create map component '%s'",
2898 gfc_new_block->name);
2899 reject_statement ();
2900 return;
2902 c->ts.type = BT_DERIVED;
2903 c->ts.u.derived = gfc_new_block;
2904 /* Normally components get their initialization expressions when they
2905 are created in decl.c (build_struct) so we can look through the
2906 flat component list for initializers during resolution. Unions and
2907 maps create components along with their type definitions so we
2908 have to generate initializers here. */
2909 c->initializer = gfc_default_initializer (&c->ts);
2910 break;
2912 case ST_END_UNION:
2913 compiling = 0;
2914 accept_statement (ST_END_UNION);
2915 break;
2917 default:
2918 unexpected_statement (st);
2919 break;
2923 for (c = un->components; c; c = c->next)
2924 check_component (un, c, &lock_comp, &event_comp);
2926 /* Add the union as a component in its parent structure. */
2927 pop_state ();
2928 if (!gfc_add_component (gfc_current_block (), un->name, &c))
2930 gfc_internal_error ("failed to create union component '%s'", un->name);
2931 reject_statement ();
2932 return;
2934 c->ts.type = BT_UNION;
2935 c->ts.u.derived = un;
2936 c->initializer = gfc_default_initializer (&c->ts);
2938 un->attr.zero_comp = un->components == NULL;
2942 /* Parse a STRUCTURE or MAP. */
2944 static void
2945 parse_struct_map (gfc_statement block)
2947 int compiling_type;
2948 gfc_statement st;
2949 gfc_state_data s;
2950 gfc_symbol *sym;
2951 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
2952 gfc_compile_state comp;
2953 gfc_statement ends;
2955 if (block == ST_STRUCTURE_DECL)
2957 comp = COMP_STRUCTURE;
2958 ends = ST_END_STRUCTURE;
2960 else
2962 gcc_assert (block == ST_MAP);
2963 comp = COMP_MAP;
2964 ends = ST_END_MAP;
2967 accept_statement(block);
2968 push_state (&s, comp, gfc_new_block);
2970 gfc_new_block->component_access = ACCESS_PUBLIC;
2971 compiling_type = 1;
2973 while (compiling_type)
2975 st = next_statement ();
2976 switch (st)
2978 case ST_NONE:
2979 unexpected_eof ();
2981 /* Nested structure declarations will be captured as ST_DATA_DECL. */
2982 case ST_STRUCTURE_DECL:
2983 /* Let a more specific error make it to decode_statement(). */
2984 if (gfc_error_check () == 0)
2985 gfc_error ("Syntax error in nested structure declaration at %C");
2986 reject_statement ();
2987 /* Skip the rest of this statement. */
2988 gfc_error_recovery ();
2989 break;
2991 case ST_UNION:
2992 accept_statement (ST_UNION);
2993 parse_union ();
2994 break;
2996 case ST_DATA_DECL:
2997 /* The data declaration was a nested/ad-hoc STRUCTURE field. */
2998 accept_statement (ST_DATA_DECL);
2999 if (gfc_new_block && gfc_new_block != gfc_current_block ()
3000 && gfc_new_block->attr.flavor == FL_STRUCT)
3001 parse_struct_map (ST_STRUCTURE_DECL);
3002 break;
3004 case ST_END_STRUCTURE:
3005 case ST_END_MAP:
3006 if (st == ends)
3008 accept_statement (st);
3009 compiling_type = 0;
3011 else
3012 unexpected_statement (st);
3013 break;
3015 default:
3016 unexpected_statement (st);
3017 break;
3021 /* Validate each component. */
3022 sym = gfc_current_block ();
3023 for (c = sym->components; c; c = c->next)
3024 check_component (sym, c, &lock_comp, &event_comp);
3026 sym->attr.zero_comp = (sym->components == NULL);
3028 /* Allow parse_union to find this structure to add to its list of maps. */
3029 if (block == ST_MAP)
3030 gfc_new_block = gfc_current_block ();
3032 pop_state ();
3036 /* Parse a derived type. */
3038 static void
3039 parse_derived (void)
3041 int compiling_type, seen_private, seen_sequence, seen_component;
3042 gfc_statement st;
3043 gfc_state_data s;
3044 gfc_symbol *sym;
3045 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3047 accept_statement (ST_DERIVED_DECL);
3048 push_state (&s, COMP_DERIVED, gfc_new_block);
3050 gfc_new_block->component_access = ACCESS_PUBLIC;
3051 seen_private = 0;
3052 seen_sequence = 0;
3053 seen_component = 0;
3055 compiling_type = 1;
3057 while (compiling_type)
3059 st = next_statement ();
3060 switch (st)
3062 case ST_NONE:
3063 unexpected_eof ();
3065 case ST_DATA_DECL:
3066 case ST_PROCEDURE:
3067 accept_statement (st);
3068 seen_component = 1;
3069 break;
3071 case ST_FINAL:
3072 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
3073 break;
3075 case ST_END_TYPE:
3076 endType:
3077 compiling_type = 0;
3079 if (!seen_component)
3080 gfc_notify_std (GFC_STD_F2003, "Derived type "
3081 "definition at %C without components");
3083 accept_statement (ST_END_TYPE);
3084 break;
3086 case ST_PRIVATE:
3087 if (!gfc_find_state (COMP_MODULE))
3089 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
3090 "a MODULE");
3091 break;
3094 if (seen_component)
3096 gfc_error ("PRIVATE statement at %C must precede "
3097 "structure components");
3098 break;
3101 if (seen_private)
3102 gfc_error ("Duplicate PRIVATE statement at %C");
3104 s.sym->component_access = ACCESS_PRIVATE;
3106 accept_statement (ST_PRIVATE);
3107 seen_private = 1;
3108 break;
3110 case ST_SEQUENCE:
3111 if (seen_component)
3113 gfc_error ("SEQUENCE statement at %C must precede "
3114 "structure components");
3115 break;
3118 if (gfc_current_block ()->attr.sequence)
3119 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
3120 "TYPE statement");
3122 if (seen_sequence)
3124 gfc_error ("Duplicate SEQUENCE statement at %C");
3127 seen_sequence = 1;
3128 gfc_add_sequence (&gfc_current_block ()->attr,
3129 gfc_current_block ()->name, NULL);
3130 break;
3132 case ST_CONTAINS:
3133 gfc_notify_std (GFC_STD_F2003,
3134 "CONTAINS block in derived type"
3135 " definition at %C");
3137 accept_statement (ST_CONTAINS);
3138 parse_derived_contains ();
3139 goto endType;
3141 default:
3142 unexpected_statement (st);
3143 break;
3147 /* need to verify that all fields of the derived type are
3148 * interoperable with C if the type is declared to be bind(c)
3150 sym = gfc_current_block ();
3151 for (c = sym->components; c; c = c->next)
3152 check_component (sym, c, &lock_comp, &event_comp);
3154 if (!seen_component)
3155 sym->attr.zero_comp = 1;
3157 pop_state ();
3161 /* Parse an ENUM. */
3163 static void
3164 parse_enum (void)
3166 gfc_statement st;
3167 int compiling_enum;
3168 gfc_state_data s;
3169 int seen_enumerator = 0;
3171 push_state (&s, COMP_ENUM, gfc_new_block);
3173 compiling_enum = 1;
3175 while (compiling_enum)
3177 st = next_statement ();
3178 switch (st)
3180 case ST_NONE:
3181 unexpected_eof ();
3182 break;
3184 case ST_ENUMERATOR:
3185 seen_enumerator = 1;
3186 accept_statement (st);
3187 break;
3189 case ST_END_ENUM:
3190 compiling_enum = 0;
3191 if (!seen_enumerator)
3192 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
3193 accept_statement (st);
3194 break;
3196 default:
3197 gfc_free_enum_history ();
3198 unexpected_statement (st);
3199 break;
3202 pop_state ();
3206 /* Parse an interface. We must be able to deal with the possibility
3207 of recursive interfaces. The parse_spec() subroutine is mutually
3208 recursive with parse_interface(). */
3210 static gfc_statement parse_spec (gfc_statement);
3212 static void
3213 parse_interface (void)
3215 gfc_compile_state new_state = COMP_NONE, current_state;
3216 gfc_symbol *prog_unit, *sym;
3217 gfc_interface_info save;
3218 gfc_state_data s1, s2;
3219 gfc_statement st;
3221 accept_statement (ST_INTERFACE);
3223 current_interface.ns = gfc_current_ns;
3224 save = current_interface;
3226 sym = (current_interface.type == INTERFACE_GENERIC
3227 || current_interface.type == INTERFACE_USER_OP)
3228 ? gfc_new_block : NULL;
3230 push_state (&s1, COMP_INTERFACE, sym);
3231 current_state = COMP_NONE;
3233 loop:
3234 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
3236 st = next_statement ();
3237 switch (st)
3239 case ST_NONE:
3240 unexpected_eof ();
3242 case ST_SUBROUTINE:
3243 case ST_FUNCTION:
3244 if (st == ST_SUBROUTINE)
3245 new_state = COMP_SUBROUTINE;
3246 else if (st == ST_FUNCTION)
3247 new_state = COMP_FUNCTION;
3248 if (gfc_new_block->attr.pointer)
3250 gfc_new_block->attr.pointer = 0;
3251 gfc_new_block->attr.proc_pointer = 1;
3253 if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
3254 gfc_new_block->formal, NULL))
3256 reject_statement ();
3257 gfc_free_namespace (gfc_current_ns);
3258 goto loop;
3260 /* F2008 C1210 forbids the IMPORT statement in module procedure
3261 interface bodies and the flag is set to import symbols. */
3262 if (gfc_new_block->attr.module_procedure)
3263 gfc_current_ns->has_import_set = 1;
3264 break;
3266 case ST_PROCEDURE:
3267 case ST_MODULE_PROC: /* The module procedure matcher makes
3268 sure the context is correct. */
3269 accept_statement (st);
3270 gfc_free_namespace (gfc_current_ns);
3271 goto loop;
3273 case ST_END_INTERFACE:
3274 gfc_free_namespace (gfc_current_ns);
3275 gfc_current_ns = current_interface.ns;
3276 goto done;
3278 default:
3279 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
3280 gfc_ascii_statement (st));
3281 reject_statement ();
3282 gfc_free_namespace (gfc_current_ns);
3283 goto loop;
3287 /* Make sure that the generic name has the right attribute. */
3288 if (current_interface.type == INTERFACE_GENERIC
3289 && current_state == COMP_NONE)
3291 if (new_state == COMP_FUNCTION && sym)
3292 gfc_add_function (&sym->attr, sym->name, NULL);
3293 else if (new_state == COMP_SUBROUTINE && sym)
3294 gfc_add_subroutine (&sym->attr, sym->name, NULL);
3296 current_state = new_state;
3299 if (current_interface.type == INTERFACE_ABSTRACT)
3301 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
3302 if (gfc_is_intrinsic_typename (gfc_new_block->name))
3303 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
3304 "cannot be the same as an intrinsic type",
3305 gfc_new_block->name);
3308 push_state (&s2, new_state, gfc_new_block);
3309 accept_statement (st);
3310 prog_unit = gfc_new_block;
3311 prog_unit->formal_ns = gfc_current_ns;
3312 if (prog_unit == prog_unit->formal_ns->proc_name
3313 && prog_unit->ns != prog_unit->formal_ns)
3314 prog_unit->refs++;
3316 decl:
3317 /* Read data declaration statements. */
3318 st = parse_spec (ST_NONE);
3319 in_specification_block = true;
3321 /* Since the interface block does not permit an IMPLICIT statement,
3322 the default type for the function or the result must be taken
3323 from the formal namespace. */
3324 if (new_state == COMP_FUNCTION)
3326 if (prog_unit->result == prog_unit
3327 && prog_unit->ts.type == BT_UNKNOWN)
3328 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
3329 else if (prog_unit->result != prog_unit
3330 && prog_unit->result->ts.type == BT_UNKNOWN)
3331 gfc_set_default_type (prog_unit->result, 1,
3332 prog_unit->formal_ns);
3335 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
3337 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3338 gfc_ascii_statement (st));
3339 reject_statement ();
3340 goto decl;
3343 /* Add EXTERNAL attribute to function or subroutine. */
3344 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
3345 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
3347 current_interface = save;
3348 gfc_add_interface (prog_unit);
3349 pop_state ();
3351 if (current_interface.ns
3352 && current_interface.ns->proc_name
3353 && strcmp (current_interface.ns->proc_name->name,
3354 prog_unit->name) == 0)
3355 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3356 "enclosing procedure", prog_unit->name,
3357 &current_interface.ns->proc_name->declared_at);
3359 goto loop;
3361 done:
3362 pop_state ();
3366 /* Associate function characteristics by going back to the function
3367 declaration and rematching the prefix. */
3369 static match
3370 match_deferred_characteristics (gfc_typespec * ts)
3372 locus loc;
3373 match m = MATCH_ERROR;
3374 char name[GFC_MAX_SYMBOL_LEN + 1];
3376 loc = gfc_current_locus;
3378 gfc_current_locus = gfc_current_block ()->declared_at;
3380 gfc_clear_error ();
3381 gfc_buffer_error (true);
3382 m = gfc_match_prefix (ts);
3383 gfc_buffer_error (false);
3385 if (ts->type == BT_DERIVED)
3387 ts->kind = 0;
3389 if (!ts->u.derived)
3390 m = MATCH_ERROR;
3393 /* Only permit one go at the characteristic association. */
3394 if (ts->kind == -1)
3395 ts->kind = 0;
3397 /* Set the function locus correctly. If we have not found the
3398 function name, there is an error. */
3399 if (m == MATCH_YES
3400 && gfc_match ("function% %n", name) == MATCH_YES
3401 && strcmp (name, gfc_current_block ()->name) == 0)
3403 gfc_current_block ()->declared_at = gfc_current_locus;
3404 gfc_commit_symbols ();
3406 else
3408 gfc_error_check ();
3409 gfc_undo_symbols ();
3412 gfc_current_locus =loc;
3413 return m;
3417 /* Check specification-expressions in the function result of the currently
3418 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3419 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3420 scope are not yet parsed so this has to be delayed up to parse_spec. */
3422 static void
3423 check_function_result_typed (void)
3425 gfc_typespec ts;
3427 gcc_assert (gfc_current_state () == COMP_FUNCTION);
3429 if (!gfc_current_ns->proc_name->result) return;
3431 ts = gfc_current_ns->proc_name->result->ts;
3433 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3434 /* TODO: Extend when KIND type parameters are implemented. */
3435 if (ts.type == BT_CHARACTER && ts.u.cl && ts.u.cl->length)
3436 gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true);
3440 /* Parse a set of specification statements. Returns the statement
3441 that doesn't fit. */
3443 static gfc_statement
3444 parse_spec (gfc_statement st)
3446 st_state ss;
3447 bool function_result_typed = false;
3448 bool bad_characteristic = false;
3449 gfc_typespec *ts;
3451 in_specification_block = true;
3453 verify_st_order (&ss, ST_NONE, false);
3454 if (st == ST_NONE)
3455 st = next_statement ();
3457 /* If we are not inside a function or don't have a result specified so far,
3458 do nothing special about it. */
3459 if (gfc_current_state () != COMP_FUNCTION)
3460 function_result_typed = true;
3461 else
3463 gfc_symbol* proc = gfc_current_ns->proc_name;
3464 gcc_assert (proc);
3466 if (proc->result->ts.type == BT_UNKNOWN)
3467 function_result_typed = true;
3470 loop:
3472 /* If we're inside a BLOCK construct, some statements are disallowed.
3473 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3474 or VALUE are also disallowed, but they don't have a particular ST_*
3475 key so we have to check for them individually in their matcher routine. */
3476 if (gfc_current_state () == COMP_BLOCK)
3477 switch (st)
3479 case ST_IMPLICIT:
3480 case ST_IMPLICIT_NONE:
3481 case ST_NAMELIST:
3482 case ST_COMMON:
3483 case ST_EQUIVALENCE:
3484 case ST_STATEMENT_FUNCTION:
3485 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3486 gfc_ascii_statement (st));
3487 reject_statement ();
3488 break;
3490 default:
3491 break;
3493 else if (gfc_current_state () == COMP_BLOCK_DATA)
3494 /* Fortran 2008, C1116. */
3495 switch (st)
3497 case ST_DATA_DECL:
3498 case ST_COMMON:
3499 case ST_DATA:
3500 case ST_TYPE:
3501 case ST_END_BLOCK_DATA:
3502 case ST_ATTR_DECL:
3503 case ST_EQUIVALENCE:
3504 case ST_PARAMETER:
3505 case ST_IMPLICIT:
3506 case ST_IMPLICIT_NONE:
3507 case ST_DERIVED_DECL:
3508 case ST_USE:
3509 break;
3511 case ST_NONE:
3512 break;
3514 default:
3515 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3516 gfc_ascii_statement (st));
3517 reject_statement ();
3518 break;
3521 /* If we find a statement that can not be followed by an IMPLICIT statement
3522 (and thus we can expect to see none any further), type the function result
3523 if it has not yet been typed. Be careful not to give the END statement
3524 to verify_st_order! */
3525 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
3527 bool verify_now = false;
3529 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
3530 verify_now = true;
3531 else
3533 st_state dummyss;
3534 verify_st_order (&dummyss, ST_NONE, false);
3535 verify_st_order (&dummyss, st, false);
3537 if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
3538 verify_now = true;
3541 if (verify_now)
3543 check_function_result_typed ();
3544 function_result_typed = true;
3548 switch (st)
3550 case ST_NONE:
3551 unexpected_eof ();
3553 case ST_IMPLICIT_NONE:
3554 case ST_IMPLICIT:
3555 if (!function_result_typed)
3557 check_function_result_typed ();
3558 function_result_typed = true;
3560 goto declSt;
3562 case ST_FORMAT:
3563 case ST_ENTRY:
3564 case ST_DATA: /* Not allowed in interfaces */
3565 if (gfc_current_state () == COMP_INTERFACE)
3566 break;
3568 /* Fall through */
3570 case ST_USE:
3571 case ST_IMPORT:
3572 case ST_PARAMETER:
3573 case ST_PUBLIC:
3574 case ST_PRIVATE:
3575 case ST_STRUCTURE_DECL:
3576 case ST_DERIVED_DECL:
3577 case_decl:
3578 case_omp_decl:
3579 declSt:
3580 if (!verify_st_order (&ss, st, false))
3582 reject_statement ();
3583 st = next_statement ();
3584 goto loop;
3587 switch (st)
3589 case ST_INTERFACE:
3590 parse_interface ();
3591 break;
3593 case ST_STRUCTURE_DECL:
3594 parse_struct_map (ST_STRUCTURE_DECL);
3595 break;
3597 case ST_DERIVED_DECL:
3598 parse_derived ();
3599 break;
3601 case ST_PUBLIC:
3602 case ST_PRIVATE:
3603 if (gfc_current_state () != COMP_MODULE)
3605 gfc_error ("%s statement must appear in a MODULE",
3606 gfc_ascii_statement (st));
3607 reject_statement ();
3608 break;
3611 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
3613 gfc_error ("%s statement at %C follows another accessibility "
3614 "specification", gfc_ascii_statement (st));
3615 reject_statement ();
3616 break;
3619 gfc_current_ns->default_access = (st == ST_PUBLIC)
3620 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3622 break;
3624 case ST_STATEMENT_FUNCTION:
3625 if (gfc_current_state () == COMP_MODULE
3626 || gfc_current_state () == COMP_SUBMODULE)
3628 unexpected_statement (st);
3629 break;
3632 default:
3633 break;
3636 accept_statement (st);
3637 st = next_statement ();
3638 goto loop;
3640 case ST_ENUM:
3641 accept_statement (st);
3642 parse_enum();
3643 st = next_statement ();
3644 goto loop;
3646 case ST_GET_FCN_CHARACTERISTICS:
3647 /* This statement triggers the association of a function's result
3648 characteristics. */
3649 ts = &gfc_current_block ()->result->ts;
3650 if (match_deferred_characteristics (ts) != MATCH_YES)
3651 bad_characteristic = true;
3653 st = next_statement ();
3654 goto loop;
3656 default:
3657 break;
3660 /* If match_deferred_characteristics failed, then there is an error. */
3661 if (bad_characteristic)
3663 ts = &gfc_current_block ()->result->ts;
3664 if (ts->type != BT_DERIVED)
3665 gfc_error ("Bad kind expression for function %qs at %L",
3666 gfc_current_block ()->name,
3667 &gfc_current_block ()->declared_at);
3668 else
3669 gfc_error ("The type for function %qs at %L is not accessible",
3670 gfc_current_block ()->name,
3671 &gfc_current_block ()->declared_at);
3673 gfc_current_block ()->ts.kind = 0;
3674 /* Keep the derived type; if it's bad, it will be discovered later. */
3675 if (!(ts->type == BT_DERIVED && ts->u.derived))
3676 ts->type = BT_UNKNOWN;
3679 in_specification_block = false;
3681 return st;
3685 /* Parse a WHERE block, (not a simple WHERE statement). */
3687 static void
3688 parse_where_block (void)
3690 int seen_empty_else;
3691 gfc_code *top, *d;
3692 gfc_state_data s;
3693 gfc_statement st;
3695 accept_statement (ST_WHERE_BLOCK);
3696 top = gfc_state_stack->tail;
3698 push_state (&s, COMP_WHERE, gfc_new_block);
3700 d = add_statement ();
3701 d->expr1 = top->expr1;
3702 d->op = EXEC_WHERE;
3704 top->expr1 = NULL;
3705 top->block = d;
3707 seen_empty_else = 0;
3711 st = next_statement ();
3712 switch (st)
3714 case ST_NONE:
3715 unexpected_eof ();
3717 case ST_WHERE_BLOCK:
3718 parse_where_block ();
3719 break;
3721 case ST_ASSIGNMENT:
3722 case ST_WHERE:
3723 accept_statement (st);
3724 break;
3726 case ST_ELSEWHERE:
3727 if (seen_empty_else)
3729 gfc_error ("ELSEWHERE statement at %C follows previous "
3730 "unmasked ELSEWHERE");
3731 reject_statement ();
3732 break;
3735 if (new_st.expr1 == NULL)
3736 seen_empty_else = 1;
3738 d = new_level (gfc_state_stack->head);
3739 d->op = EXEC_WHERE;
3740 d->expr1 = new_st.expr1;
3742 accept_statement (st);
3744 break;
3746 case ST_END_WHERE:
3747 accept_statement (st);
3748 break;
3750 default:
3751 gfc_error ("Unexpected %s statement in WHERE block at %C",
3752 gfc_ascii_statement (st));
3753 reject_statement ();
3754 break;
3757 while (st != ST_END_WHERE);
3759 pop_state ();
3763 /* Parse a FORALL block (not a simple FORALL statement). */
3765 static void
3766 parse_forall_block (void)
3768 gfc_code *top, *d;
3769 gfc_state_data s;
3770 gfc_statement st;
3772 accept_statement (ST_FORALL_BLOCK);
3773 top = gfc_state_stack->tail;
3775 push_state (&s, COMP_FORALL, gfc_new_block);
3777 d = add_statement ();
3778 d->op = EXEC_FORALL;
3779 top->block = d;
3783 st = next_statement ();
3784 switch (st)
3787 case ST_ASSIGNMENT:
3788 case ST_POINTER_ASSIGNMENT:
3789 case ST_WHERE:
3790 case ST_FORALL:
3791 accept_statement (st);
3792 break;
3794 case ST_WHERE_BLOCK:
3795 parse_where_block ();
3796 break;
3798 case ST_FORALL_BLOCK:
3799 parse_forall_block ();
3800 break;
3802 case ST_END_FORALL:
3803 accept_statement (st);
3804 break;
3806 case ST_NONE:
3807 unexpected_eof ();
3809 default:
3810 gfc_error ("Unexpected %s statement in FORALL block at %C",
3811 gfc_ascii_statement (st));
3813 reject_statement ();
3814 break;
3817 while (st != ST_END_FORALL);
3819 pop_state ();
3823 static gfc_statement parse_executable (gfc_statement);
3825 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
3827 static void
3828 parse_if_block (void)
3830 gfc_code *top, *d;
3831 gfc_statement st;
3832 locus else_locus;
3833 gfc_state_data s;
3834 int seen_else;
3836 seen_else = 0;
3837 accept_statement (ST_IF_BLOCK);
3839 top = gfc_state_stack->tail;
3840 push_state (&s, COMP_IF, gfc_new_block);
3842 new_st.op = EXEC_IF;
3843 d = add_statement ();
3845 d->expr1 = top->expr1;
3846 top->expr1 = NULL;
3847 top->block = d;
3851 st = parse_executable (ST_NONE);
3853 switch (st)
3855 case ST_NONE:
3856 unexpected_eof ();
3858 case ST_ELSEIF:
3859 if (seen_else)
3861 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
3862 "statement at %L", &else_locus);
3864 reject_statement ();
3865 break;
3868 d = new_level (gfc_state_stack->head);
3869 d->op = EXEC_IF;
3870 d->expr1 = new_st.expr1;
3872 accept_statement (st);
3874 break;
3876 case ST_ELSE:
3877 if (seen_else)
3879 gfc_error ("Duplicate ELSE statements at %L and %C",
3880 &else_locus);
3881 reject_statement ();
3882 break;
3885 seen_else = 1;
3886 else_locus = gfc_current_locus;
3888 d = new_level (gfc_state_stack->head);
3889 d->op = EXEC_IF;
3891 accept_statement (st);
3893 break;
3895 case ST_ENDIF:
3896 break;
3898 default:
3899 unexpected_statement (st);
3900 break;
3903 while (st != ST_ENDIF);
3905 pop_state ();
3906 accept_statement (st);
3910 /* Parse a SELECT block. */
3912 static void
3913 parse_select_block (void)
3915 gfc_statement st;
3916 gfc_code *cp;
3917 gfc_state_data s;
3919 accept_statement (ST_SELECT_CASE);
3921 cp = gfc_state_stack->tail;
3922 push_state (&s, COMP_SELECT, gfc_new_block);
3924 /* Make sure that the next statement is a CASE or END SELECT. */
3925 for (;;)
3927 st = next_statement ();
3928 if (st == ST_NONE)
3929 unexpected_eof ();
3930 if (st == ST_END_SELECT)
3932 /* Empty SELECT CASE is OK. */
3933 accept_statement (st);
3934 pop_state ();
3935 return;
3937 if (st == ST_CASE)
3938 break;
3940 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
3941 "CASE at %C");
3943 reject_statement ();
3946 /* At this point, we're got a nonempty select block. */
3947 cp = new_level (cp);
3948 *cp = new_st;
3950 accept_statement (st);
3954 st = parse_executable (ST_NONE);
3955 switch (st)
3957 case ST_NONE:
3958 unexpected_eof ();
3960 case ST_CASE:
3961 cp = new_level (gfc_state_stack->head);
3962 *cp = new_st;
3963 gfc_clear_new_st ();
3965 accept_statement (st);
3966 /* Fall through */
3968 case ST_END_SELECT:
3969 break;
3971 /* Can't have an executable statement because of
3972 parse_executable(). */
3973 default:
3974 unexpected_statement (st);
3975 break;
3978 while (st != ST_END_SELECT);
3980 pop_state ();
3981 accept_statement (st);
3985 /* Pop the current selector from the SELECT TYPE stack. */
3987 static void
3988 select_type_pop (void)
3990 gfc_select_type_stack *old = select_type_stack;
3991 select_type_stack = old->prev;
3992 free (old);
3996 /* Parse a SELECT TYPE construct (F03:R821). */
3998 static void
3999 parse_select_type_block (void)
4001 gfc_statement st;
4002 gfc_code *cp;
4003 gfc_state_data s;
4005 accept_statement (ST_SELECT_TYPE);
4007 cp = gfc_state_stack->tail;
4008 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
4010 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
4011 or END SELECT. */
4012 for (;;)
4014 st = next_statement ();
4015 if (st == ST_NONE)
4016 unexpected_eof ();
4017 if (st == ST_END_SELECT)
4018 /* Empty SELECT CASE is OK. */
4019 goto done;
4020 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
4021 break;
4023 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
4024 "following SELECT TYPE at %C");
4026 reject_statement ();
4029 /* At this point, we're got a nonempty select block. */
4030 cp = new_level (cp);
4031 *cp = new_st;
4033 accept_statement (st);
4037 st = parse_executable (ST_NONE);
4038 switch (st)
4040 case ST_NONE:
4041 unexpected_eof ();
4043 case ST_TYPE_IS:
4044 case ST_CLASS_IS:
4045 cp = new_level (gfc_state_stack->head);
4046 *cp = new_st;
4047 gfc_clear_new_st ();
4049 accept_statement (st);
4050 /* Fall through */
4052 case ST_END_SELECT:
4053 break;
4055 /* Can't have an executable statement because of
4056 parse_executable(). */
4057 default:
4058 unexpected_statement (st);
4059 break;
4062 while (st != ST_END_SELECT);
4064 done:
4065 pop_state ();
4066 accept_statement (st);
4067 gfc_current_ns = gfc_current_ns->parent;
4068 select_type_pop ();
4072 /* Given a symbol, make sure it is not an iteration variable for a DO
4073 statement. This subroutine is called when the symbol is seen in a
4074 context that causes it to become redefined. If the symbol is an
4075 iterator, we generate an error message and return nonzero. */
4078 gfc_check_do_variable (gfc_symtree *st)
4080 gfc_state_data *s;
4082 for (s=gfc_state_stack; s; s = s->previous)
4083 if (s->do_variable == st)
4085 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
4086 "loop beginning at %L", st->name, &s->head->loc);
4087 return 1;
4090 return 0;
4094 /* Checks to see if the current statement label closes an enddo.
4095 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
4096 an error) if it incorrectly closes an ENDDO. */
4098 static int
4099 check_do_closure (void)
4101 gfc_state_data *p;
4103 if (gfc_statement_label == NULL)
4104 return 0;
4106 for (p = gfc_state_stack; p; p = p->previous)
4107 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
4108 break;
4110 if (p == NULL)
4111 return 0; /* No loops to close */
4113 if (p->ext.end_do_label == gfc_statement_label)
4115 if (p == gfc_state_stack)
4116 return 1;
4118 gfc_error ("End of nonblock DO statement at %C is within another block");
4119 return 2;
4122 /* At this point, the label doesn't terminate the innermost loop.
4123 Make sure it doesn't terminate another one. */
4124 for (; p; p = p->previous)
4125 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
4126 && p->ext.end_do_label == gfc_statement_label)
4128 gfc_error ("End of nonblock DO statement at %C is interwoven "
4129 "with another DO loop");
4130 return 2;
4133 return 0;
4137 /* Parse a series of contained program units. */
4139 static void parse_progunit (gfc_statement);
4142 /* Parse a CRITICAL block. */
4144 static void
4145 parse_critical_block (void)
4147 gfc_code *top, *d;
4148 gfc_state_data s, *sd;
4149 gfc_statement st;
4151 for (sd = gfc_state_stack; sd; sd = sd->previous)
4152 if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
4153 gfc_error_now (is_oacc (sd)
4154 ? "CRITICAL block inside of OpenACC region at %C"
4155 : "CRITICAL block inside of OpenMP region at %C");
4157 s.ext.end_do_label = new_st.label1;
4159 accept_statement (ST_CRITICAL);
4160 top = gfc_state_stack->tail;
4162 push_state (&s, COMP_CRITICAL, gfc_new_block);
4164 d = add_statement ();
4165 d->op = EXEC_CRITICAL;
4166 top->block = d;
4170 st = parse_executable (ST_NONE);
4172 switch (st)
4174 case ST_NONE:
4175 unexpected_eof ();
4176 break;
4178 case ST_END_CRITICAL:
4179 if (s.ext.end_do_label != NULL
4180 && s.ext.end_do_label != gfc_statement_label)
4181 gfc_error_now ("Statement label in END CRITICAL at %C does not "
4182 "match CRITICAL label");
4184 if (gfc_statement_label != NULL)
4186 new_st.op = EXEC_NOP;
4187 add_statement ();
4189 break;
4191 default:
4192 unexpected_statement (st);
4193 break;
4196 while (st != ST_END_CRITICAL);
4198 pop_state ();
4199 accept_statement (st);
4203 /* Set up the local namespace for a BLOCK construct. */
4205 gfc_namespace*
4206 gfc_build_block_ns (gfc_namespace *parent_ns)
4208 gfc_namespace* my_ns;
4209 static int numblock = 1;
4211 my_ns = gfc_get_namespace (parent_ns, 1);
4212 my_ns->construct_entities = 1;
4214 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
4215 code generation (so it must not be NULL).
4216 We set its recursive argument if our container procedure is recursive, so
4217 that local variables are accordingly placed on the stack when it
4218 will be necessary. */
4219 if (gfc_new_block)
4220 my_ns->proc_name = gfc_new_block;
4221 else
4223 bool t;
4224 char buffer[20]; /* Enough to hold "block@2147483648\n". */
4226 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
4227 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
4228 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
4229 my_ns->proc_name->name, NULL);
4230 gcc_assert (t);
4231 gfc_commit_symbol (my_ns->proc_name);
4234 if (parent_ns->proc_name)
4235 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
4237 return my_ns;
4241 /* Parse a BLOCK construct. */
4243 static void
4244 parse_block_construct (void)
4246 gfc_namespace* my_ns;
4247 gfc_namespace* my_parent;
4248 gfc_state_data s;
4250 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
4252 my_ns = gfc_build_block_ns (gfc_current_ns);
4254 new_st.op = EXEC_BLOCK;
4255 new_st.ext.block.ns = my_ns;
4256 new_st.ext.block.assoc = NULL;
4257 accept_statement (ST_BLOCK);
4259 push_state (&s, COMP_BLOCK, my_ns->proc_name);
4260 gfc_current_ns = my_ns;
4261 my_parent = my_ns->parent;
4263 parse_progunit (ST_NONE);
4265 /* Don't depend on the value of gfc_current_ns; it might have been
4266 reset if the block had errors and was cleaned up. */
4267 gfc_current_ns = my_parent;
4269 pop_state ();
4273 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
4274 behind the scenes with compiler-generated variables. */
4276 static void
4277 parse_associate (void)
4279 gfc_namespace* my_ns;
4280 gfc_state_data s;
4281 gfc_statement st;
4282 gfc_association_list* a;
4284 gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
4286 my_ns = gfc_build_block_ns (gfc_current_ns);
4288 new_st.op = EXEC_BLOCK;
4289 new_st.ext.block.ns = my_ns;
4290 gcc_assert (new_st.ext.block.assoc);
4292 /* Add all associate-names as BLOCK variables. Creating them is enough
4293 for now, they'll get their values during trans-* phase. */
4294 gfc_current_ns = my_ns;
4295 for (a = new_st.ext.block.assoc; a; a = a->next)
4297 gfc_symbol* sym;
4298 gfc_ref *ref;
4299 gfc_array_ref *array_ref;
4301 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
4302 gcc_unreachable ();
4304 sym = a->st->n.sym;
4305 sym->attr.flavor = FL_VARIABLE;
4306 sym->assoc = a;
4307 sym->declared_at = a->where;
4308 gfc_set_sym_referenced (sym);
4310 /* Initialize the typespec. It is not available in all cases,
4311 however, as it may only be set on the target during resolution.
4312 Still, sometimes it helps to have it right now -- especially
4313 for parsing component references on the associate-name
4314 in case of association to a derived-type. */
4315 sym->ts = a->target->ts;
4317 /* Check if the target expression is array valued. This can not always
4318 be done by looking at target.rank, because that might not have been
4319 set yet. Therefore traverse the chain of refs, looking for the last
4320 array ref and evaluate that. */
4321 array_ref = NULL;
4322 for (ref = a->target->ref; ref; ref = ref->next)
4323 if (ref->type == REF_ARRAY)
4324 array_ref = &ref->u.ar;
4325 if (array_ref || a->target->rank)
4327 gfc_array_spec *as;
4328 int dim, rank = 0;
4329 if (array_ref)
4331 a->rankguessed = 1;
4332 /* Count the dimension, that have a non-scalar extend. */
4333 for (dim = 0; dim < array_ref->dimen; ++dim)
4334 if (array_ref->dimen_type[dim] != DIMEN_ELEMENT
4335 && !(array_ref->dimen_type[dim] == DIMEN_UNKNOWN
4336 && array_ref->end[dim] == NULL
4337 && array_ref->start[dim] != NULL))
4338 ++rank;
4340 else
4341 rank = a->target->rank;
4342 /* When the rank is greater than zero then sym will be an array. */
4343 if (sym->ts.type == BT_CLASS)
4345 if ((!CLASS_DATA (sym)->as && rank != 0)
4346 || (CLASS_DATA (sym)->as
4347 && CLASS_DATA (sym)->as->rank != rank))
4349 /* Don't just (re-)set the attr and as in the sym.ts,
4350 because this modifies the target's attr and as. Copy the
4351 data and do a build_class_symbol. */
4352 symbol_attribute attr = CLASS_DATA (a->target)->attr;
4353 int corank = gfc_get_corank (a->target);
4354 gfc_typespec type;
4356 if (rank || corank)
4358 as = gfc_get_array_spec ();
4359 as->type = AS_DEFERRED;
4360 as->rank = rank;
4361 as->corank = corank;
4362 attr.dimension = rank ? 1 : 0;
4363 attr.codimension = corank ? 1 : 0;
4365 else
4367 as = NULL;
4368 attr.dimension = attr.codimension = 0;
4370 attr.class_ok = 0;
4371 type = CLASS_DATA (sym)->ts;
4372 if (!gfc_build_class_symbol (&type,
4373 &attr, &as))
4374 gcc_unreachable ();
4375 sym->ts = type;
4376 sym->ts.type = BT_CLASS;
4377 sym->attr.class_ok = 1;
4379 else
4380 sym->attr.class_ok = 1;
4382 else if ((!sym->as && rank != 0)
4383 || (sym->as && sym->as->rank != rank))
4385 as = gfc_get_array_spec ();
4386 as->type = AS_DEFERRED;
4387 as->rank = rank;
4388 as->corank = gfc_get_corank (a->target);
4389 sym->as = as;
4390 sym->attr.dimension = 1;
4391 if (as->corank)
4392 sym->attr.codimension = 1;
4397 accept_statement (ST_ASSOCIATE);
4398 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
4400 loop:
4401 st = parse_executable (ST_NONE);
4402 switch (st)
4404 case ST_NONE:
4405 unexpected_eof ();
4407 case_end:
4408 accept_statement (st);
4409 my_ns->code = gfc_state_stack->head;
4410 break;
4412 default:
4413 unexpected_statement (st);
4414 goto loop;
4417 gfc_current_ns = gfc_current_ns->parent;
4418 pop_state ();
4422 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4423 handled inside of parse_executable(), because they aren't really
4424 loop statements. */
4426 static void
4427 parse_do_block (void)
4429 gfc_statement st;
4430 gfc_code *top;
4431 gfc_state_data s;
4432 gfc_symtree *stree;
4433 gfc_exec_op do_op;
4435 do_op = new_st.op;
4436 s.ext.end_do_label = new_st.label1;
4438 if (new_st.ext.iterator != NULL)
4439 stree = new_st.ext.iterator->var->symtree;
4440 else
4441 stree = NULL;
4443 accept_statement (ST_DO);
4445 top = gfc_state_stack->tail;
4446 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
4447 gfc_new_block);
4449 s.do_variable = stree;
4451 top->block = new_level (top);
4452 top->block->op = EXEC_DO;
4454 loop:
4455 st = parse_executable (ST_NONE);
4457 switch (st)
4459 case ST_NONE:
4460 unexpected_eof ();
4462 case ST_ENDDO:
4463 if (s.ext.end_do_label != NULL
4464 && s.ext.end_do_label != gfc_statement_label)
4465 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4466 "DO label");
4468 if (gfc_statement_label != NULL)
4470 new_st.op = EXEC_NOP;
4471 add_statement ();
4473 break;
4475 case ST_IMPLIED_ENDDO:
4476 /* If the do-stmt of this DO construct has a do-construct-name,
4477 the corresponding end-do must be an end-do-stmt (with a matching
4478 name, but in that case we must have seen ST_ENDDO first).
4479 We only complain about this in pedantic mode. */
4480 if (gfc_current_block () != NULL)
4481 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4482 &gfc_current_block()->declared_at);
4484 break;
4486 default:
4487 unexpected_statement (st);
4488 goto loop;
4491 pop_state ();
4492 accept_statement (st);
4496 /* Parse the statements of OpenMP do/parallel do. */
4498 static gfc_statement
4499 parse_omp_do (gfc_statement omp_st)
4501 gfc_statement st;
4502 gfc_code *cp, *np;
4503 gfc_state_data s;
4505 accept_statement (omp_st);
4507 cp = gfc_state_stack->tail;
4508 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4509 np = new_level (cp);
4510 np->op = cp->op;
4511 np->block = NULL;
4513 for (;;)
4515 st = next_statement ();
4516 if (st == ST_NONE)
4517 unexpected_eof ();
4518 else if (st == ST_DO)
4519 break;
4520 else
4521 unexpected_statement (st);
4524 parse_do_block ();
4525 if (gfc_statement_label != NULL
4526 && gfc_state_stack->previous != NULL
4527 && gfc_state_stack->previous->state == COMP_DO
4528 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4530 /* In
4531 DO 100 I=1,10
4532 !$OMP DO
4533 DO J=1,10
4535 100 CONTINUE
4536 there should be no !$OMP END DO. */
4537 pop_state ();
4538 return ST_IMPLIED_ENDDO;
4541 check_do_closure ();
4542 pop_state ();
4544 st = next_statement ();
4545 gfc_statement omp_end_st = ST_OMP_END_DO;
4546 switch (omp_st)
4548 case ST_OMP_DISTRIBUTE: omp_end_st = ST_OMP_END_DISTRIBUTE; break;
4549 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4550 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4551 break;
4552 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4553 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4554 break;
4555 case ST_OMP_DISTRIBUTE_SIMD:
4556 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4557 break;
4558 case ST_OMP_DO: omp_end_st = ST_OMP_END_DO; break;
4559 case ST_OMP_DO_SIMD: omp_end_st = ST_OMP_END_DO_SIMD; break;
4560 case ST_OMP_PARALLEL_DO: omp_end_st = ST_OMP_END_PARALLEL_DO; break;
4561 case ST_OMP_PARALLEL_DO_SIMD:
4562 omp_end_st = ST_OMP_END_PARALLEL_DO_SIMD;
4563 break;
4564 case ST_OMP_SIMD: omp_end_st = ST_OMP_END_SIMD; break;
4565 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4566 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4567 break;
4568 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4569 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4570 break;
4571 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4572 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4573 break;
4574 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4575 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4576 break;
4577 case ST_OMP_TEAMS_DISTRIBUTE:
4578 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4579 break;
4580 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4581 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4582 break;
4583 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4584 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4585 break;
4586 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4587 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4588 break;
4589 default: gcc_unreachable ();
4591 if (st == omp_end_st)
4593 if (new_st.op == EXEC_OMP_END_NOWAIT)
4594 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4595 else
4596 gcc_assert (new_st.op == EXEC_NOP);
4597 gfc_clear_new_st ();
4598 gfc_commit_symbols ();
4599 gfc_warning_check ();
4600 st = next_statement ();
4602 return st;
4606 /* Parse the statements of OpenMP atomic directive. */
4608 static gfc_statement
4609 parse_omp_oacc_atomic (bool omp_p)
4611 gfc_statement st, st_atomic, st_end_atomic;
4612 gfc_code *cp, *np;
4613 gfc_state_data s;
4614 int count;
4616 if (omp_p)
4618 st_atomic = ST_OMP_ATOMIC;
4619 st_end_atomic = ST_OMP_END_ATOMIC;
4621 else
4623 st_atomic = ST_OACC_ATOMIC;
4624 st_end_atomic = ST_OACC_END_ATOMIC;
4626 accept_statement (st_atomic);
4628 cp = gfc_state_stack->tail;
4629 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4630 np = new_level (cp);
4631 np->op = cp->op;
4632 np->block = NULL;
4633 count = 1 + ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4634 == GFC_OMP_ATOMIC_CAPTURE);
4636 while (count)
4638 st = next_statement ();
4639 if (st == ST_NONE)
4640 unexpected_eof ();
4641 else if (st == ST_ASSIGNMENT)
4643 accept_statement (st);
4644 count--;
4646 else
4647 unexpected_statement (st);
4650 pop_state ();
4652 st = next_statement ();
4653 if (st == st_end_atomic)
4655 gfc_clear_new_st ();
4656 gfc_commit_symbols ();
4657 gfc_warning_check ();
4658 st = next_statement ();
4660 else if ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4661 == GFC_OMP_ATOMIC_CAPTURE)
4662 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4663 return st;
4667 /* Parse the statements of an OpenACC structured block. */
4669 static void
4670 parse_oacc_structured_block (gfc_statement acc_st)
4672 gfc_statement st, acc_end_st;
4673 gfc_code *cp, *np;
4674 gfc_state_data s, *sd;
4676 for (sd = gfc_state_stack; sd; sd = sd->previous)
4677 if (sd->state == COMP_CRITICAL)
4678 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4680 accept_statement (acc_st);
4682 cp = gfc_state_stack->tail;
4683 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4684 np = new_level (cp);
4685 np->op = cp->op;
4686 np->block = NULL;
4687 switch (acc_st)
4689 case ST_OACC_PARALLEL:
4690 acc_end_st = ST_OACC_END_PARALLEL;
4691 break;
4692 case ST_OACC_KERNELS:
4693 acc_end_st = ST_OACC_END_KERNELS;
4694 break;
4695 case ST_OACC_DATA:
4696 acc_end_st = ST_OACC_END_DATA;
4697 break;
4698 case ST_OACC_HOST_DATA:
4699 acc_end_st = ST_OACC_END_HOST_DATA;
4700 break;
4701 default:
4702 gcc_unreachable ();
4707 st = parse_executable (ST_NONE);
4708 if (st == ST_NONE)
4709 unexpected_eof ();
4710 else if (st != acc_end_st)
4712 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
4713 reject_statement ();
4716 while (st != acc_end_st);
4718 gcc_assert (new_st.op == EXEC_NOP);
4720 gfc_clear_new_st ();
4721 gfc_commit_symbols ();
4722 gfc_warning_check ();
4723 pop_state ();
4726 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4728 static gfc_statement
4729 parse_oacc_loop (gfc_statement acc_st)
4731 gfc_statement st;
4732 gfc_code *cp, *np;
4733 gfc_state_data s, *sd;
4735 for (sd = gfc_state_stack; sd; sd = sd->previous)
4736 if (sd->state == COMP_CRITICAL)
4737 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4739 accept_statement (acc_st);
4741 cp = gfc_state_stack->tail;
4742 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4743 np = new_level (cp);
4744 np->op = cp->op;
4745 np->block = NULL;
4747 for (;;)
4749 st = next_statement ();
4750 if (st == ST_NONE)
4751 unexpected_eof ();
4752 else if (st == ST_DO)
4753 break;
4754 else
4756 gfc_error ("Expected DO loop at %C");
4757 reject_statement ();
4761 parse_do_block ();
4762 if (gfc_statement_label != NULL
4763 && gfc_state_stack->previous != NULL
4764 && gfc_state_stack->previous->state == COMP_DO
4765 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4767 pop_state ();
4768 return ST_IMPLIED_ENDDO;
4771 check_do_closure ();
4772 pop_state ();
4774 st = next_statement ();
4775 if (st == ST_OACC_END_LOOP)
4776 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
4777 if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
4778 (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
4779 (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
4781 gcc_assert (new_st.op == EXEC_NOP);
4782 gfc_clear_new_st ();
4783 gfc_commit_symbols ();
4784 gfc_warning_check ();
4785 st = next_statement ();
4787 return st;
4791 /* Parse the statements of an OpenMP structured block. */
4793 static void
4794 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
4796 gfc_statement st, omp_end_st;
4797 gfc_code *cp, *np;
4798 gfc_state_data s;
4800 accept_statement (omp_st);
4802 cp = gfc_state_stack->tail;
4803 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4804 np = new_level (cp);
4805 np->op = cp->op;
4806 np->block = NULL;
4808 switch (omp_st)
4810 case ST_OMP_PARALLEL:
4811 omp_end_st = ST_OMP_END_PARALLEL;
4812 break;
4813 case ST_OMP_PARALLEL_SECTIONS:
4814 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
4815 break;
4816 case ST_OMP_SECTIONS:
4817 omp_end_st = ST_OMP_END_SECTIONS;
4818 break;
4819 case ST_OMP_ORDERED:
4820 omp_end_st = ST_OMP_END_ORDERED;
4821 break;
4822 case ST_OMP_CRITICAL:
4823 omp_end_st = ST_OMP_END_CRITICAL;
4824 break;
4825 case ST_OMP_MASTER:
4826 omp_end_st = ST_OMP_END_MASTER;
4827 break;
4828 case ST_OMP_SINGLE:
4829 omp_end_st = ST_OMP_END_SINGLE;
4830 break;
4831 case ST_OMP_TARGET:
4832 omp_end_st = ST_OMP_END_TARGET;
4833 break;
4834 case ST_OMP_TARGET_DATA:
4835 omp_end_st = ST_OMP_END_TARGET_DATA;
4836 break;
4837 case ST_OMP_TARGET_TEAMS:
4838 omp_end_st = ST_OMP_END_TARGET_TEAMS;
4839 break;
4840 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4841 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4842 break;
4843 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4844 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4845 break;
4846 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4847 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4848 break;
4849 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4850 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4851 break;
4852 case ST_OMP_TASK:
4853 omp_end_st = ST_OMP_END_TASK;
4854 break;
4855 case ST_OMP_TASKGROUP:
4856 omp_end_st = ST_OMP_END_TASKGROUP;
4857 break;
4858 case ST_OMP_TEAMS:
4859 omp_end_st = ST_OMP_END_TEAMS;
4860 break;
4861 case ST_OMP_TEAMS_DISTRIBUTE:
4862 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4863 break;
4864 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4865 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4866 break;
4867 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4868 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4869 break;
4870 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4871 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4872 break;
4873 case ST_OMP_DISTRIBUTE:
4874 omp_end_st = ST_OMP_END_DISTRIBUTE;
4875 break;
4876 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4877 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4878 break;
4879 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4880 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4881 break;
4882 case ST_OMP_DISTRIBUTE_SIMD:
4883 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4884 break;
4885 case ST_OMP_WORKSHARE:
4886 omp_end_st = ST_OMP_END_WORKSHARE;
4887 break;
4888 case ST_OMP_PARALLEL_WORKSHARE:
4889 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
4890 break;
4891 default:
4892 gcc_unreachable ();
4897 if (workshare_stmts_only)
4899 /* Inside of !$omp workshare, only
4900 scalar assignments
4901 array assignments
4902 where statements and constructs
4903 forall statements and constructs
4904 !$omp atomic
4905 !$omp critical
4906 !$omp parallel
4907 are allowed. For !$omp critical these
4908 restrictions apply recursively. */
4909 bool cycle = true;
4911 st = next_statement ();
4912 for (;;)
4914 switch (st)
4916 case ST_NONE:
4917 unexpected_eof ();
4919 case ST_ASSIGNMENT:
4920 case ST_WHERE:
4921 case ST_FORALL:
4922 accept_statement (st);
4923 break;
4925 case ST_WHERE_BLOCK:
4926 parse_where_block ();
4927 break;
4929 case ST_FORALL_BLOCK:
4930 parse_forall_block ();
4931 break;
4933 case ST_OMP_PARALLEL:
4934 case ST_OMP_PARALLEL_SECTIONS:
4935 parse_omp_structured_block (st, false);
4936 break;
4938 case ST_OMP_PARALLEL_WORKSHARE:
4939 case ST_OMP_CRITICAL:
4940 parse_omp_structured_block (st, true);
4941 break;
4943 case ST_OMP_PARALLEL_DO:
4944 case ST_OMP_PARALLEL_DO_SIMD:
4945 st = parse_omp_do (st);
4946 continue;
4948 case ST_OMP_ATOMIC:
4949 st = parse_omp_oacc_atomic (true);
4950 continue;
4952 default:
4953 cycle = false;
4954 break;
4957 if (!cycle)
4958 break;
4960 st = next_statement ();
4963 else
4964 st = parse_executable (ST_NONE);
4965 if (st == ST_NONE)
4966 unexpected_eof ();
4967 else if (st == ST_OMP_SECTION
4968 && (omp_st == ST_OMP_SECTIONS
4969 || omp_st == ST_OMP_PARALLEL_SECTIONS))
4971 np = new_level (np);
4972 np->op = cp->op;
4973 np->block = NULL;
4975 else if (st != omp_end_st)
4976 unexpected_statement (st);
4978 while (st != omp_end_st);
4980 switch (new_st.op)
4982 case EXEC_OMP_END_NOWAIT:
4983 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4984 break;
4985 case EXEC_OMP_CRITICAL:
4986 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
4987 || (new_st.ext.omp_name != NULL
4988 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
4989 gfc_error ("Name after !$omp critical and !$omp end critical does "
4990 "not match at %C");
4991 free (CONST_CAST (char *, new_st.ext.omp_name));
4992 break;
4993 case EXEC_OMP_END_SINGLE:
4994 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
4995 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
4996 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
4997 gfc_free_omp_clauses (new_st.ext.omp_clauses);
4998 break;
4999 case EXEC_NOP:
5000 break;
5001 default:
5002 gcc_unreachable ();
5005 gfc_clear_new_st ();
5006 gfc_commit_symbols ();
5007 gfc_warning_check ();
5008 pop_state ();
5012 /* Accept a series of executable statements. We return the first
5013 statement that doesn't fit to the caller. Any block statements are
5014 passed on to the correct handler, which usually passes the buck
5015 right back here. */
5017 static gfc_statement
5018 parse_executable (gfc_statement st)
5020 int close_flag;
5022 if (st == ST_NONE)
5023 st = next_statement ();
5025 for (;;)
5027 close_flag = check_do_closure ();
5028 if (close_flag)
5029 switch (st)
5031 case ST_GOTO:
5032 case ST_END_PROGRAM:
5033 case ST_RETURN:
5034 case ST_EXIT:
5035 case ST_END_FUNCTION:
5036 case ST_CYCLE:
5037 case ST_PAUSE:
5038 case ST_STOP:
5039 case ST_ERROR_STOP:
5040 case ST_END_SUBROUTINE:
5042 case ST_DO:
5043 case ST_FORALL:
5044 case ST_WHERE:
5045 case ST_SELECT_CASE:
5046 gfc_error ("%s statement at %C cannot terminate a non-block "
5047 "DO loop", gfc_ascii_statement (st));
5048 break;
5050 default:
5051 break;
5054 switch (st)
5056 case ST_NONE:
5057 unexpected_eof ();
5059 case ST_DATA:
5060 gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
5061 "first executable statement");
5062 /* Fall through. */
5064 case ST_FORMAT:
5065 case ST_ENTRY:
5066 case_executable:
5067 accept_statement (st);
5068 if (close_flag == 1)
5069 return ST_IMPLIED_ENDDO;
5070 break;
5072 case ST_BLOCK:
5073 parse_block_construct ();
5074 break;
5076 case ST_ASSOCIATE:
5077 parse_associate ();
5078 break;
5080 case ST_IF_BLOCK:
5081 parse_if_block ();
5082 break;
5084 case ST_SELECT_CASE:
5085 parse_select_block ();
5086 break;
5088 case ST_SELECT_TYPE:
5089 parse_select_type_block();
5090 break;
5092 case ST_DO:
5093 parse_do_block ();
5094 if (check_do_closure () == 1)
5095 return ST_IMPLIED_ENDDO;
5096 break;
5098 case ST_CRITICAL:
5099 parse_critical_block ();
5100 break;
5102 case ST_WHERE_BLOCK:
5103 parse_where_block ();
5104 break;
5106 case ST_FORALL_BLOCK:
5107 parse_forall_block ();
5108 break;
5110 case ST_OACC_PARALLEL_LOOP:
5111 case ST_OACC_KERNELS_LOOP:
5112 case ST_OACC_LOOP:
5113 st = parse_oacc_loop (st);
5114 if (st == ST_IMPLIED_ENDDO)
5115 return st;
5116 continue;
5118 case ST_OACC_PARALLEL:
5119 case ST_OACC_KERNELS:
5120 case ST_OACC_DATA:
5121 case ST_OACC_HOST_DATA:
5122 parse_oacc_structured_block (st);
5123 break;
5125 case ST_OMP_PARALLEL:
5126 case ST_OMP_PARALLEL_SECTIONS:
5127 case ST_OMP_SECTIONS:
5128 case ST_OMP_ORDERED:
5129 case ST_OMP_CRITICAL:
5130 case ST_OMP_MASTER:
5131 case ST_OMP_SINGLE:
5132 case ST_OMP_TARGET:
5133 case ST_OMP_TARGET_DATA:
5134 case ST_OMP_TARGET_TEAMS:
5135 case ST_OMP_TEAMS:
5136 case ST_OMP_TASK:
5137 case ST_OMP_TASKGROUP:
5138 parse_omp_structured_block (st, false);
5139 break;
5141 case ST_OMP_WORKSHARE:
5142 case ST_OMP_PARALLEL_WORKSHARE:
5143 parse_omp_structured_block (st, true);
5144 break;
5146 case ST_OMP_DISTRIBUTE:
5147 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
5148 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
5149 case ST_OMP_DISTRIBUTE_SIMD:
5150 case ST_OMP_DO:
5151 case ST_OMP_DO_SIMD:
5152 case ST_OMP_PARALLEL_DO:
5153 case ST_OMP_PARALLEL_DO_SIMD:
5154 case ST_OMP_SIMD:
5155 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
5156 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
5157 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5158 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
5159 case ST_OMP_TEAMS_DISTRIBUTE:
5160 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
5161 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5162 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
5163 st = parse_omp_do (st);
5164 if (st == ST_IMPLIED_ENDDO)
5165 return st;
5166 continue;
5168 case ST_OACC_ATOMIC:
5169 st = parse_omp_oacc_atomic (false);
5170 continue;
5172 case ST_OMP_ATOMIC:
5173 st = parse_omp_oacc_atomic (true);
5174 continue;
5176 default:
5177 return st;
5180 st = next_statement ();
5185 /* Fix the symbols for sibling functions. These are incorrectly added to
5186 the child namespace as the parser didn't know about this procedure. */
5188 static void
5189 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
5191 gfc_namespace *ns;
5192 gfc_symtree *st;
5193 gfc_symbol *old_sym;
5195 for (ns = siblings; ns; ns = ns->sibling)
5197 st = gfc_find_symtree (ns->sym_root, sym->name);
5199 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
5200 goto fixup_contained;
5202 if ((st->n.sym->attr.flavor == FL_DERIVED
5203 && sym->attr.generic && sym->attr.function)
5204 ||(sym->attr.flavor == FL_DERIVED
5205 && st->n.sym->attr.generic && st->n.sym->attr.function))
5206 goto fixup_contained;
5208 old_sym = st->n.sym;
5209 if (old_sym->ns == ns
5210 && !old_sym->attr.contained
5212 /* By 14.6.1.3, host association should be excluded
5213 for the following. */
5214 && !(old_sym->attr.external
5215 || (old_sym->ts.type != BT_UNKNOWN
5216 && !old_sym->attr.implicit_type)
5217 || old_sym->attr.flavor == FL_PARAMETER
5218 || old_sym->attr.use_assoc
5219 || old_sym->attr.in_common
5220 || old_sym->attr.in_equivalence
5221 || old_sym->attr.data
5222 || old_sym->attr.dummy
5223 || old_sym->attr.result
5224 || old_sym->attr.dimension
5225 || old_sym->attr.allocatable
5226 || old_sym->attr.intrinsic
5227 || old_sym->attr.generic
5228 || old_sym->attr.flavor == FL_NAMELIST
5229 || old_sym->attr.flavor == FL_LABEL
5230 || old_sym->attr.proc == PROC_ST_FUNCTION))
5232 /* Replace it with the symbol from the parent namespace. */
5233 st->n.sym = sym;
5234 sym->refs++;
5236 gfc_release_symbol (old_sym);
5239 fixup_contained:
5240 /* Do the same for any contained procedures. */
5241 gfc_fixup_sibling_symbols (sym, ns->contained);
5245 static void
5246 parse_contained (int module)
5248 gfc_namespace *ns, *parent_ns, *tmp;
5249 gfc_state_data s1, s2;
5250 gfc_statement st;
5251 gfc_symbol *sym;
5252 gfc_entry_list *el;
5253 int contains_statements = 0;
5254 int seen_error = 0;
5256 push_state (&s1, COMP_CONTAINS, NULL);
5257 parent_ns = gfc_current_ns;
5261 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
5263 gfc_current_ns->sibling = parent_ns->contained;
5264 parent_ns->contained = gfc_current_ns;
5266 next:
5267 /* Process the next available statement. We come here if we got an error
5268 and rejected the last statement. */
5269 st = next_statement ();
5271 switch (st)
5273 case ST_NONE:
5274 unexpected_eof ();
5276 case ST_FUNCTION:
5277 case ST_SUBROUTINE:
5278 contains_statements = 1;
5279 accept_statement (st);
5281 push_state (&s2,
5282 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
5283 gfc_new_block);
5285 /* For internal procedures, create/update the symbol in the
5286 parent namespace. */
5288 if (!module)
5290 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
5291 gfc_error ("Contained procedure %qs at %C is already "
5292 "ambiguous", gfc_new_block->name);
5293 else
5295 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
5296 sym->name,
5297 &gfc_new_block->declared_at))
5299 if (st == ST_FUNCTION)
5300 gfc_add_function (&sym->attr, sym->name,
5301 &gfc_new_block->declared_at);
5302 else
5303 gfc_add_subroutine (&sym->attr, sym->name,
5304 &gfc_new_block->declared_at);
5308 gfc_commit_symbols ();
5310 else
5311 sym = gfc_new_block;
5313 /* Mark this as a contained function, so it isn't replaced
5314 by other module functions. */
5315 sym->attr.contained = 1;
5317 /* Set implicit_pure so that it can be reset if any of the
5318 tests for purity fail. This is used for some optimisation
5319 during translation. */
5320 if (!sym->attr.pure)
5321 sym->attr.implicit_pure = 1;
5323 parse_progunit (ST_NONE);
5325 /* Fix up any sibling functions that refer to this one. */
5326 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
5327 /* Or refer to any of its alternate entry points. */
5328 for (el = gfc_current_ns->entries; el; el = el->next)
5329 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
5331 gfc_current_ns->code = s2.head;
5332 gfc_current_ns = parent_ns;
5334 pop_state ();
5335 break;
5337 /* These statements are associated with the end of the host unit. */
5338 case ST_END_FUNCTION:
5339 case ST_END_MODULE:
5340 case ST_END_SUBMODULE:
5341 case ST_END_PROGRAM:
5342 case ST_END_SUBROUTINE:
5343 accept_statement (st);
5344 gfc_current_ns->code = s1.head;
5345 break;
5347 default:
5348 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
5349 gfc_ascii_statement (st));
5350 reject_statement ();
5351 seen_error = 1;
5352 goto next;
5353 break;
5356 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
5357 && st != ST_END_MODULE && st != ST_END_SUBMODULE
5358 && st != ST_END_PROGRAM);
5360 /* The first namespace in the list is guaranteed to not have
5361 anything (worthwhile) in it. */
5362 tmp = gfc_current_ns;
5363 gfc_current_ns = parent_ns;
5364 if (seen_error && tmp->refs > 1)
5365 gfc_free_namespace (tmp);
5367 ns = gfc_current_ns->contained;
5368 gfc_current_ns->contained = ns->sibling;
5369 gfc_free_namespace (ns);
5371 pop_state ();
5372 if (!contains_statements)
5373 gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
5374 "FUNCTION or SUBROUTINE statement at %C");
5378 /* The result variable in a MODULE PROCEDURE needs to be created and
5379 its characteristics copied from the interface since it is neither
5380 declared in the procedure declaration nor in the specification
5381 part. */
5383 static void
5384 get_modproc_result (void)
5386 gfc_symbol *proc;
5387 if (gfc_state_stack->previous
5388 && gfc_state_stack->previous->state == COMP_CONTAINS
5389 && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
5391 proc = gfc_current_ns->proc_name ? gfc_current_ns->proc_name : NULL;
5392 if (proc != NULL
5393 && proc->attr.function
5394 && proc->ts.interface
5395 && proc->ts.interface->result
5396 && proc->ts.interface->result != proc->ts.interface)
5398 gfc_copy_dummy_sym (&proc->result, proc->ts.interface->result, 1);
5399 gfc_set_sym_referenced (proc->result);
5400 proc->result->attr.if_source = IFSRC_DECL;
5401 gfc_commit_symbol (proc->result);
5407 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
5409 static void
5410 parse_progunit (gfc_statement st)
5412 gfc_state_data *p;
5413 int n;
5415 if (gfc_new_block
5416 && gfc_new_block->abr_modproc_decl
5417 && gfc_new_block->attr.function)
5418 get_modproc_result ();
5420 st = parse_spec (st);
5421 switch (st)
5423 case ST_NONE:
5424 unexpected_eof ();
5426 case ST_CONTAINS:
5427 /* This is not allowed within BLOCK! */
5428 if (gfc_current_state () != COMP_BLOCK)
5429 goto contains;
5430 break;
5432 case_end:
5433 accept_statement (st);
5434 goto done;
5436 default:
5437 break;
5440 if (gfc_current_state () == COMP_FUNCTION)
5441 gfc_check_function_type (gfc_current_ns);
5443 loop:
5444 for (;;)
5446 st = parse_executable (st);
5448 switch (st)
5450 case ST_NONE:
5451 unexpected_eof ();
5453 case ST_CONTAINS:
5454 /* This is not allowed within BLOCK! */
5455 if (gfc_current_state () != COMP_BLOCK)
5456 goto contains;
5457 break;
5459 case_end:
5460 accept_statement (st);
5461 goto done;
5463 default:
5464 break;
5467 unexpected_statement (st);
5468 reject_statement ();
5469 st = next_statement ();
5472 contains:
5473 n = 0;
5475 for (p = gfc_state_stack; p; p = p->previous)
5476 if (p->state == COMP_CONTAINS)
5477 n++;
5479 if (gfc_find_state (COMP_MODULE) == true
5480 || gfc_find_state (COMP_SUBMODULE) == true)
5481 n--;
5483 if (n > 0)
5485 gfc_error ("CONTAINS statement at %C is already in a contained "
5486 "program unit");
5487 reject_statement ();
5488 st = next_statement ();
5489 goto loop;
5492 parse_contained (0);
5494 done:
5495 gfc_current_ns->code = gfc_state_stack->head;
5499 /* Come here to complain about a global symbol already in use as
5500 something else. */
5502 void
5503 gfc_global_used (gfc_gsymbol *sym, locus *where)
5505 const char *name;
5507 if (where == NULL)
5508 where = &gfc_current_locus;
5510 switch(sym->type)
5512 case GSYM_PROGRAM:
5513 name = "PROGRAM";
5514 break;
5515 case GSYM_FUNCTION:
5516 name = "FUNCTION";
5517 break;
5518 case GSYM_SUBROUTINE:
5519 name = "SUBROUTINE";
5520 break;
5521 case GSYM_COMMON:
5522 name = "COMMON";
5523 break;
5524 case GSYM_BLOCK_DATA:
5525 name = "BLOCK DATA";
5526 break;
5527 case GSYM_MODULE:
5528 name = "MODULE";
5529 break;
5530 default:
5531 gfc_internal_error ("gfc_global_used(): Bad type");
5532 name = NULL;
5535 if (sym->binding_label)
5536 gfc_error ("Global binding name %qs at %L is already being used as a %s "
5537 "at %L", sym->binding_label, where, name, &sym->where);
5538 else
5539 gfc_error ("Global name %qs at %L is already being used as a %s at %L",
5540 sym->name, where, name, &sym->where);
5544 /* Parse a block data program unit. */
5546 static void
5547 parse_block_data (void)
5549 gfc_statement st;
5550 static locus blank_locus;
5551 static int blank_block=0;
5552 gfc_gsymbol *s;
5554 gfc_current_ns->proc_name = gfc_new_block;
5555 gfc_current_ns->is_block_data = 1;
5557 if (gfc_new_block == NULL)
5559 if (blank_block)
5560 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5561 "prior BLOCK DATA at %L", &blank_locus);
5562 else
5564 blank_block = 1;
5565 blank_locus = gfc_current_locus;
5568 else
5570 s = gfc_get_gsymbol (gfc_new_block->name);
5571 if (s->defined
5572 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
5573 gfc_global_used (s, &gfc_new_block->declared_at);
5574 else
5576 s->type = GSYM_BLOCK_DATA;
5577 s->where = gfc_new_block->declared_at;
5578 s->defined = 1;
5582 st = parse_spec (ST_NONE);
5584 while (st != ST_END_BLOCK_DATA)
5586 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5587 gfc_ascii_statement (st));
5588 reject_statement ();
5589 st = next_statement ();
5594 /* Following the association of the ancestor (sub)module symbols, they
5595 must be set host rather than use associated and all must be public.
5596 They are flagged up by 'used_in_submodule' so that they can be set
5597 DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl). Otherwise the
5598 linker chokes on multiple symbol definitions. */
5600 static void
5601 set_syms_host_assoc (gfc_symbol *sym)
5603 gfc_component *c;
5605 if (sym == NULL)
5606 return;
5608 if (sym->attr.module_procedure)
5609 sym->attr.external = 0;
5611 /* sym->attr.access = ACCESS_PUBLIC; */
5613 sym->attr.use_assoc = 0;
5614 sym->attr.host_assoc = 1;
5615 sym->attr.used_in_submodule =1;
5617 if (sym->attr.flavor == FL_DERIVED)
5619 for (c = sym->components; c; c = c->next)
5620 c->attr.access = ACCESS_PUBLIC;
5624 /* Parse a module subprogram. */
5626 static void
5627 parse_module (void)
5629 gfc_statement st;
5630 gfc_gsymbol *s;
5631 bool error;
5633 s = gfc_get_gsymbol (gfc_new_block->name);
5634 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
5635 gfc_global_used (s, &gfc_new_block->declared_at);
5636 else
5638 s->type = GSYM_MODULE;
5639 s->where = gfc_new_block->declared_at;
5640 s->defined = 1;
5643 /* Something is nulling the module_list after this point. This is good
5644 since it allows us to 'USE' the parent modules that the submodule
5645 inherits and to set (most) of the symbols as host associated. */
5646 if (gfc_current_state () == COMP_SUBMODULE)
5648 use_modules ();
5649 gfc_traverse_ns (gfc_current_ns, set_syms_host_assoc);
5652 st = parse_spec (ST_NONE);
5654 error = false;
5655 loop:
5656 switch (st)
5658 case ST_NONE:
5659 unexpected_eof ();
5661 case ST_CONTAINS:
5662 parse_contained (1);
5663 break;
5665 case ST_END_MODULE:
5666 case ST_END_SUBMODULE:
5667 accept_statement (st);
5668 break;
5670 default:
5671 gfc_error ("Unexpected %s statement in MODULE at %C",
5672 gfc_ascii_statement (st));
5674 error = true;
5675 reject_statement ();
5676 st = next_statement ();
5677 goto loop;
5680 /* Make sure not to free the namespace twice on error. */
5681 if (!error)
5682 s->ns = gfc_current_ns;
5686 /* Add a procedure name to the global symbol table. */
5688 static void
5689 add_global_procedure (bool sub)
5691 gfc_gsymbol *s;
5693 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5694 name is a global identifier. */
5695 if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
5697 s = gfc_get_gsymbol (gfc_new_block->name);
5699 if (s->defined
5700 || (s->type != GSYM_UNKNOWN
5701 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5703 gfc_global_used (s, &gfc_new_block->declared_at);
5704 /* Silence follow-up errors. */
5705 gfc_new_block->binding_label = NULL;
5707 else
5709 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5710 s->sym_name = gfc_new_block->name;
5711 s->where = gfc_new_block->declared_at;
5712 s->defined = 1;
5713 s->ns = gfc_current_ns;
5717 /* Don't add the symbol multiple times. */
5718 if (gfc_new_block->binding_label
5719 && (!gfc_notification_std (GFC_STD_F2008)
5720 || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
5722 s = gfc_get_gsymbol (gfc_new_block->binding_label);
5724 if (s->defined
5725 || (s->type != GSYM_UNKNOWN
5726 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5728 gfc_global_used (s, &gfc_new_block->declared_at);
5729 /* Silence follow-up errors. */
5730 gfc_new_block->binding_label = NULL;
5732 else
5734 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5735 s->sym_name = gfc_new_block->name;
5736 s->binding_label = gfc_new_block->binding_label;
5737 s->where = gfc_new_block->declared_at;
5738 s->defined = 1;
5739 s->ns = gfc_current_ns;
5745 /* Add a program to the global symbol table. */
5747 static void
5748 add_global_program (void)
5750 gfc_gsymbol *s;
5752 if (gfc_new_block == NULL)
5753 return;
5754 s = gfc_get_gsymbol (gfc_new_block->name);
5756 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
5757 gfc_global_used (s, &gfc_new_block->declared_at);
5758 else
5760 s->type = GSYM_PROGRAM;
5761 s->where = gfc_new_block->declared_at;
5762 s->defined = 1;
5763 s->ns = gfc_current_ns;
5768 /* Resolve all the program units. */
5769 static void
5770 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
5772 gfc_free_dt_list ();
5773 gfc_current_ns = gfc_global_ns_list;
5774 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5776 if (gfc_current_ns->proc_name
5777 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5778 continue; /* Already resolved. */
5780 if (gfc_current_ns->proc_name)
5781 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5782 gfc_resolve (gfc_current_ns);
5783 gfc_current_ns->derived_types = gfc_derived_types;
5784 gfc_derived_types = NULL;
5789 static void
5790 clean_up_modules (gfc_gsymbol *gsym)
5792 if (gsym == NULL)
5793 return;
5795 clean_up_modules (gsym->left);
5796 clean_up_modules (gsym->right);
5798 if (gsym->type != GSYM_MODULE || !gsym->ns)
5799 return;
5801 gfc_current_ns = gsym->ns;
5802 gfc_derived_types = gfc_current_ns->derived_types;
5803 gfc_done_2 ();
5804 gsym->ns = NULL;
5805 return;
5809 /* Translate all the program units. This could be in a different order
5810 to resolution if there are forward references in the file. */
5811 static void
5812 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
5814 int errors;
5816 gfc_current_ns = gfc_global_ns_list;
5817 gfc_get_errors (NULL, &errors);
5819 /* We first translate all modules to make sure that later parts
5820 of the program can use the decl. Then we translate the nonmodules. */
5822 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5824 if (!gfc_current_ns->proc_name
5825 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5826 continue;
5828 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5829 gfc_derived_types = gfc_current_ns->derived_types;
5830 gfc_generate_module_code (gfc_current_ns);
5831 gfc_current_ns->translated = 1;
5834 gfc_current_ns = gfc_global_ns_list;
5835 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5837 if (gfc_current_ns->proc_name
5838 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5839 continue;
5841 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5842 gfc_derived_types = gfc_current_ns->derived_types;
5843 gfc_generate_code (gfc_current_ns);
5844 gfc_current_ns->translated = 1;
5847 /* Clean up all the namespaces after translation. */
5848 gfc_current_ns = gfc_global_ns_list;
5849 for (;gfc_current_ns;)
5851 gfc_namespace *ns;
5853 if (gfc_current_ns->proc_name
5854 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5856 gfc_current_ns = gfc_current_ns->sibling;
5857 continue;
5860 ns = gfc_current_ns->sibling;
5861 gfc_derived_types = gfc_current_ns->derived_types;
5862 gfc_done_2 ();
5863 gfc_current_ns = ns;
5866 clean_up_modules (gfc_gsym_root);
5870 /* Top level parser. */
5872 bool
5873 gfc_parse_file (void)
5875 int seen_program, errors_before, errors;
5876 gfc_state_data top, s;
5877 gfc_statement st;
5878 locus prog_locus;
5879 gfc_namespace *next;
5881 gfc_start_source_files ();
5883 top.state = COMP_NONE;
5884 top.sym = NULL;
5885 top.previous = NULL;
5886 top.head = top.tail = NULL;
5887 top.do_variable = NULL;
5889 gfc_state_stack = &top;
5891 gfc_clear_new_st ();
5893 gfc_statement_label = NULL;
5895 if (setjmp (eof_buf))
5896 return false; /* Come here on unexpected EOF */
5898 /* Prepare the global namespace that will contain the
5899 program units. */
5900 gfc_global_ns_list = next = NULL;
5902 seen_program = 0;
5903 errors_before = 0;
5905 /* Exit early for empty files. */
5906 if (gfc_at_eof ())
5907 goto done;
5909 in_specification_block = true;
5910 loop:
5911 gfc_init_2 ();
5912 st = next_statement ();
5913 switch (st)
5915 case ST_NONE:
5916 gfc_done_2 ();
5917 goto done;
5919 case ST_PROGRAM:
5920 if (seen_program)
5921 goto duplicate_main;
5922 seen_program = 1;
5923 prog_locus = gfc_current_locus;
5925 push_state (&s, COMP_PROGRAM, gfc_new_block);
5926 main_program_symbol(gfc_current_ns, gfc_new_block->name);
5927 accept_statement (st);
5928 add_global_program ();
5929 parse_progunit (ST_NONE);
5930 goto prog_units;
5931 break;
5933 case ST_SUBROUTINE:
5934 add_global_procedure (true);
5935 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
5936 accept_statement (st);
5937 parse_progunit (ST_NONE);
5938 goto prog_units;
5939 break;
5941 case ST_FUNCTION:
5942 add_global_procedure (false);
5943 push_state (&s, COMP_FUNCTION, gfc_new_block);
5944 accept_statement (st);
5945 parse_progunit (ST_NONE);
5946 goto prog_units;
5947 break;
5949 case ST_BLOCK_DATA:
5950 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
5951 accept_statement (st);
5952 parse_block_data ();
5953 break;
5955 case ST_MODULE:
5956 push_state (&s, COMP_MODULE, gfc_new_block);
5957 accept_statement (st);
5959 gfc_get_errors (NULL, &errors_before);
5960 parse_module ();
5961 break;
5963 case ST_SUBMODULE:
5964 push_state (&s, COMP_SUBMODULE, gfc_new_block);
5965 accept_statement (st);
5967 gfc_get_errors (NULL, &errors_before);
5968 parse_module ();
5969 break;
5971 /* Anything else starts a nameless main program block. */
5972 default:
5973 if (seen_program)
5974 goto duplicate_main;
5975 seen_program = 1;
5976 prog_locus = gfc_current_locus;
5978 push_state (&s, COMP_PROGRAM, gfc_new_block);
5979 main_program_symbol (gfc_current_ns, "MAIN__");
5980 parse_progunit (st);
5981 goto prog_units;
5982 break;
5985 /* Handle the non-program units. */
5986 gfc_current_ns->code = s.head;
5988 gfc_resolve (gfc_current_ns);
5990 /* Dump the parse tree if requested. */
5991 if (flag_dump_fortran_original)
5992 gfc_dump_parse_tree (gfc_current_ns, stdout);
5994 gfc_get_errors (NULL, &errors);
5995 if (s.state == COMP_MODULE || s.state == COMP_SUBMODULE)
5997 gfc_dump_module (s.sym->name, errors_before == errors);
5998 gfc_current_ns->derived_types = gfc_derived_types;
5999 gfc_derived_types = NULL;
6000 goto prog_units;
6002 else
6004 if (errors == 0)
6005 gfc_generate_code (gfc_current_ns);
6006 pop_state ();
6007 gfc_done_2 ();
6010 goto loop;
6012 prog_units:
6013 /* The main program and non-contained procedures are put
6014 in the global namespace list, so that they can be processed
6015 later and all their interfaces resolved. */
6016 gfc_current_ns->code = s.head;
6017 if (next)
6019 for (; next->sibling; next = next->sibling)
6021 next->sibling = gfc_current_ns;
6023 else
6024 gfc_global_ns_list = gfc_current_ns;
6026 next = gfc_current_ns;
6028 pop_state ();
6029 goto loop;
6031 done:
6033 /* Do the resolution. */
6034 resolve_all_program_units (gfc_global_ns_list);
6036 /* Do the parse tree dump. */
6037 gfc_current_ns
6038 = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
6040 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6041 if (!gfc_current_ns->proc_name
6042 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6044 gfc_dump_parse_tree (gfc_current_ns, stdout);
6045 fputs ("------------------------------------------\n\n", stdout);
6048 /* Do the translation. */
6049 translate_all_program_units (gfc_global_ns_list);
6051 gfc_end_source_files ();
6052 return true;
6054 duplicate_main:
6055 /* If we see a duplicate main program, shut down. If the second
6056 instance is an implied main program, i.e. data decls or executable
6057 statements, we're in for lots of errors. */
6058 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
6059 reject_statement ();
6060 gfc_done_2 ();
6061 return true;
6064 /* Return true if this state data represents an OpenACC region. */
6065 bool
6066 is_oacc (gfc_state_data *sd)
6068 switch (sd->construct->op)
6070 case EXEC_OACC_PARALLEL_LOOP:
6071 case EXEC_OACC_PARALLEL:
6072 case EXEC_OACC_KERNELS_LOOP:
6073 case EXEC_OACC_KERNELS:
6074 case EXEC_OACC_DATA:
6075 case EXEC_OACC_HOST_DATA:
6076 case EXEC_OACC_LOOP:
6077 case EXEC_OACC_UPDATE:
6078 case EXEC_OACC_WAIT:
6079 case EXEC_OACC_CACHE:
6080 case EXEC_OACC_ENTER_DATA:
6081 case EXEC_OACC_EXIT_DATA:
6082 case EXEC_OACC_ATOMIC:
6083 case EXEC_OACC_ROUTINE:
6084 return true;
6086 default:
6087 return false;