2017-03-06 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / fortran / parse.c
blob28fa218b2fc17739010ea06286a38654024bba3f
1 /* Main parser.
2 Copyright (C) 2000-2017 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_equiv = gfc_current_ns->equiv;
120 gfc_current_ns->old_data = gfc_current_ns->data;
121 last_was_use_stmt = false;
125 /* Figure out what the next statement is, (mostly) regardless of
126 proper ordering. The do...while(0) is there to prevent if/else
127 ambiguity. */
129 #define match(keyword, subr, st) \
130 do { \
131 if (match_word (keyword, subr, &old_locus) == MATCH_YES) \
132 return st; \
133 else \
134 undo_new_statement (); \
135 } while (0);
138 /* This is a specialist version of decode_statement that is used
139 for the specification statements in a function, whose
140 characteristics are deferred into the specification statements.
141 eg.: INTEGER (king = mykind) foo ()
142 USE mymodule, ONLY mykind.....
143 The KIND parameter needs a return after USE or IMPORT, whereas
144 derived type declarations can occur anywhere, up the executable
145 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
146 out of the correct kind of specification statements. */
147 static gfc_statement
148 decode_specification_statement (void)
150 gfc_statement st;
151 locus old_locus;
152 char c;
154 if (gfc_match_eos () == MATCH_YES)
155 return ST_NONE;
157 old_locus = gfc_current_locus;
159 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
161 last_was_use_stmt = true;
162 return ST_USE;
164 else
166 undo_new_statement ();
167 if (last_was_use_stmt)
168 use_modules ();
171 match ("import", gfc_match_import, ST_IMPORT);
173 if (gfc_current_block ()->result->ts.type != BT_DERIVED)
174 goto end_of_block;
176 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
177 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
178 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
180 /* General statement matching: Instead of testing every possible
181 statement, we eliminate most possibilities by peeking at the
182 first character. */
184 c = gfc_peek_ascii_char ();
186 switch (c)
188 case 'a':
189 match ("abstract% interface", gfc_match_abstract_interface,
190 ST_INTERFACE);
191 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
192 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
193 match ("automatic", gfc_match_automatic, 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 ("static", gfc_match_static, ST_ATTR_DECL);
260 match ("structure", gfc_match_structure_decl, ST_STRUCTURE_DECL);
261 break;
263 case 't':
264 match ("target", gfc_match_target, ST_ATTR_DECL);
265 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
266 break;
268 case 'u':
269 break;
271 case 'v':
272 match ("value", gfc_match_value, ST_ATTR_DECL);
273 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
274 break;
276 case 'w':
277 break;
280 /* This is not a specification statement. See if any of the matchers
281 has stored an error message of some sort. */
283 end_of_block:
284 gfc_clear_error ();
285 gfc_buffer_error (false);
286 gfc_current_locus = old_locus;
288 return ST_GET_FCN_CHARACTERISTICS;
291 static bool in_specification_block;
293 /* This is the primary 'decode_statement'. */
294 static gfc_statement
295 decode_statement (void)
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;
354 /* Legacy parameter statements are ambiguous with assignments so try parameter
355 first. */
356 match ("parameter", gfc_match_parameter, ST_PARAMETER);
358 /* Match statements whose error messages are meant to be overwritten
359 by something better. */
361 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
362 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
364 if (in_specification_block)
366 m = match_word (NULL, gfc_match_st_function, &old_locus);
367 if (m == MATCH_YES)
368 return ST_STATEMENT_FUNCTION;
371 if (!(in_specification_block && m == MATCH_ERROR))
373 match (NULL, gfc_match_ptr_fcn_assign, ST_ASSIGNMENT);
376 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
377 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
379 /* Try to match a subroutine statement, which has the same optional
380 prefixes that functions can have. */
382 if (gfc_match_subroutine () == MATCH_YES)
383 return ST_SUBROUTINE;
384 gfc_undo_symbols ();
385 gfc_current_locus = old_locus;
387 if (gfc_match_submod_proc () == MATCH_YES)
389 if (gfc_new_block->attr.subroutine)
390 return ST_SUBROUTINE;
391 else if (gfc_new_block->attr.function)
392 return ST_FUNCTION;
394 gfc_undo_symbols ();
395 gfc_current_locus = old_locus;
397 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
398 statements, which might begin with a block label. The match functions for
399 these statements are unusual in that their keyword is not seen before
400 the matcher is called. */
402 if (gfc_match_if (&st) == MATCH_YES)
403 return st;
404 gfc_undo_symbols ();
405 gfc_current_locus = old_locus;
407 if (gfc_match_where (&st) == MATCH_YES)
408 return st;
409 gfc_undo_symbols ();
410 gfc_current_locus = old_locus;
412 if (gfc_match_forall (&st) == MATCH_YES)
413 return st;
414 gfc_undo_symbols ();
415 gfc_current_locus = old_locus;
417 /* Try to match TYPE as an alias for PRINT. */
418 if (gfc_match_type (&st) == MATCH_YES)
419 return st;
420 gfc_undo_symbols ();
421 gfc_current_locus = old_locus;
423 match (NULL, gfc_match_do, ST_DO);
424 match (NULL, gfc_match_block, ST_BLOCK);
425 match (NULL, gfc_match_associate, ST_ASSOCIATE);
426 match (NULL, gfc_match_critical, ST_CRITICAL);
427 match (NULL, gfc_match_select, ST_SELECT_CASE);
428 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
430 /* General statement matching: Instead of testing every possible
431 statement, we eliminate most possibilities by peeking at the
432 first character. */
434 switch (c)
436 case 'a':
437 match ("abstract% interface", gfc_match_abstract_interface,
438 ST_INTERFACE);
439 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
440 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
441 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
442 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
443 match ("automatic", gfc_match_automatic, ST_ATTR_DECL);
444 break;
446 case 'b':
447 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
448 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
449 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
450 break;
452 case 'c':
453 match ("call", gfc_match_call, ST_CALL);
454 match ("close", gfc_match_close, ST_CLOSE);
455 match ("continue", gfc_match_continue, ST_CONTINUE);
456 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
457 match ("cycle", gfc_match_cycle, ST_CYCLE);
458 match ("case", gfc_match_case, ST_CASE);
459 match ("common", gfc_match_common, ST_COMMON);
460 match ("contains", gfc_match_eos, ST_CONTAINS);
461 match ("class", gfc_match_class_is, ST_CLASS_IS);
462 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
463 break;
465 case 'd':
466 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
467 match ("data", gfc_match_data, ST_DATA);
468 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
469 break;
471 case 'e':
472 match ("end file", gfc_match_endfile, ST_END_FILE);
473 match ("exit", gfc_match_exit, ST_EXIT);
474 match ("else", gfc_match_else, ST_ELSE);
475 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
476 match ("else if", gfc_match_elseif, ST_ELSEIF);
477 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
478 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
480 if (gfc_match_end (&st) == MATCH_YES)
481 return st;
483 match ("entry% ", gfc_match_entry, ST_ENTRY);
484 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
485 match ("external", gfc_match_external, ST_ATTR_DECL);
486 match ("event post", gfc_match_event_post, ST_EVENT_POST);
487 match ("event wait", gfc_match_event_wait, ST_EVENT_WAIT);
488 break;
490 case 'f':
491 match ("fail image", gfc_match_fail_image, ST_FAIL_IMAGE);
492 match ("final", gfc_match_final_decl, ST_FINAL);
493 match ("flush", gfc_match_flush, ST_FLUSH);
494 match ("format", gfc_match_format, ST_FORMAT);
495 break;
497 case 'g':
498 match ("generic", gfc_match_generic, ST_GENERIC);
499 match ("go to", gfc_match_goto, ST_GOTO);
500 break;
502 case 'i':
503 match ("inquire", gfc_match_inquire, ST_INQUIRE);
504 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
505 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
506 match ("import", gfc_match_import, ST_IMPORT);
507 match ("interface", gfc_match_interface, ST_INTERFACE);
508 match ("intent", gfc_match_intent, ST_ATTR_DECL);
509 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
510 break;
512 case 'l':
513 match ("lock", gfc_match_lock, ST_LOCK);
514 break;
516 case 'm':
517 match ("map", gfc_match_map, ST_MAP);
518 match ("module% procedure", gfc_match_modproc, ST_MODULE_PROC);
519 match ("module", gfc_match_module, ST_MODULE);
520 break;
522 case 'n':
523 match ("nullify", gfc_match_nullify, ST_NULLIFY);
524 match ("namelist", gfc_match_namelist, ST_NAMELIST);
525 break;
527 case 'o':
528 match ("open", gfc_match_open, ST_OPEN);
529 match ("optional", gfc_match_optional, ST_ATTR_DECL);
530 break;
532 case 'p':
533 match ("print", gfc_match_print, ST_WRITE);
534 match ("pause", gfc_match_pause, ST_PAUSE);
535 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
536 if (gfc_match_private (&st) == MATCH_YES)
537 return st;
538 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
539 match ("program", gfc_match_program, ST_PROGRAM);
540 if (gfc_match_public (&st) == MATCH_YES)
541 return st;
542 match ("protected", gfc_match_protected, ST_ATTR_DECL);
543 break;
545 case 'r':
546 match ("read", gfc_match_read, ST_READ);
547 match ("return", gfc_match_return, ST_RETURN);
548 match ("rewind", gfc_match_rewind, ST_REWIND);
549 break;
551 case 's':
552 match ("structure", gfc_match_structure_decl, ST_STRUCTURE_DECL);
553 match ("sequence", gfc_match_eos, ST_SEQUENCE);
554 match ("stop", gfc_match_stop, ST_STOP);
555 match ("save", gfc_match_save, ST_ATTR_DECL);
556 match ("static", gfc_match_static, ST_ATTR_DECL);
557 match ("submodule", gfc_match_submodule, ST_SUBMODULE);
558 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
559 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
560 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
561 break;
563 case 't':
564 match ("target", gfc_match_target, ST_ATTR_DECL);
565 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
566 match ("type is", gfc_match_type_is, ST_TYPE_IS);
567 break;
569 case 'u':
570 match ("union", gfc_match_union, ST_UNION);
571 match ("unlock", gfc_match_unlock, ST_UNLOCK);
572 break;
574 case 'v':
575 match ("value", gfc_match_value, ST_ATTR_DECL);
576 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
577 break;
579 case 'w':
580 match ("wait", gfc_match_wait, ST_WAIT);
581 match ("write", gfc_match_write, ST_WRITE);
582 break;
585 /* All else has failed, so give up. See if any of the matchers has
586 stored an error message of some sort. */
588 if (!gfc_error_check ())
589 gfc_error_now ("Unclassifiable statement at %C");
591 reject_statement ();
593 gfc_error_recovery ();
595 return ST_NONE;
598 /* Like match and if spec_only, goto do_spec_only without actually
599 matching. */
600 #define matcha(keyword, subr, st) \
601 do { \
602 if (spec_only && gfc_match (keyword) == MATCH_YES) \
603 goto do_spec_only; \
604 else if (match_word (keyword, subr, &old_locus) \
605 == MATCH_YES) \
606 return st; \
607 else \
608 undo_new_statement (); \
609 } while (0);
611 static gfc_statement
612 decode_oacc_directive (void)
614 locus old_locus;
615 char c;
616 bool spec_only = false;
618 gfc_enforce_clean_symbol_state ();
620 gfc_clear_error (); /* Clear any pending errors. */
621 gfc_clear_warning (); /* Clear any pending warnings. */
623 if (gfc_pure (NULL))
625 gfc_error_now ("OpenACC directives at %C may not appear in PURE "
626 "procedures");
627 gfc_error_recovery ();
628 return ST_NONE;
631 if (gfc_current_state () == COMP_FUNCTION
632 && gfc_current_block ()->result->ts.kind == -1)
633 spec_only = true;
635 gfc_unset_implicit_pure (NULL);
637 old_locus = gfc_current_locus;
639 /* General OpenACC directive matching: Instead of testing every possible
640 statement, we eliminate most possibilities by peeking at the
641 first character. */
643 c = gfc_peek_ascii_char ();
645 switch (c)
647 case 'a':
648 matcha ("atomic", gfc_match_oacc_atomic, ST_OACC_ATOMIC);
649 break;
650 case 'c':
651 matcha ("cache", gfc_match_oacc_cache, ST_OACC_CACHE);
652 break;
653 case 'd':
654 matcha ("data", gfc_match_oacc_data, ST_OACC_DATA);
655 match ("declare", gfc_match_oacc_declare, ST_OACC_DECLARE);
656 break;
657 case 'e':
658 matcha ("end atomic", gfc_match_omp_eos, ST_OACC_END_ATOMIC);
659 matcha ("end data", gfc_match_omp_eos, ST_OACC_END_DATA);
660 matcha ("end host_data", gfc_match_omp_eos, ST_OACC_END_HOST_DATA);
661 matcha ("end kernels loop", gfc_match_omp_eos, ST_OACC_END_KERNELS_LOOP);
662 matcha ("end kernels", gfc_match_omp_eos, ST_OACC_END_KERNELS);
663 matcha ("end loop", gfc_match_omp_eos, ST_OACC_END_LOOP);
664 matcha ("end parallel loop", gfc_match_omp_eos,
665 ST_OACC_END_PARALLEL_LOOP);
666 matcha ("end parallel", gfc_match_omp_eos, ST_OACC_END_PARALLEL);
667 matcha ("enter data", gfc_match_oacc_enter_data, ST_OACC_ENTER_DATA);
668 matcha ("exit data", gfc_match_oacc_exit_data, ST_OACC_EXIT_DATA);
669 break;
670 case 'h':
671 matcha ("host_data", gfc_match_oacc_host_data, ST_OACC_HOST_DATA);
672 break;
673 case 'p':
674 matcha ("parallel loop", gfc_match_oacc_parallel_loop,
675 ST_OACC_PARALLEL_LOOP);
676 matcha ("parallel", gfc_match_oacc_parallel, ST_OACC_PARALLEL);
677 break;
678 case 'k':
679 matcha ("kernels loop", gfc_match_oacc_kernels_loop,
680 ST_OACC_KERNELS_LOOP);
681 matcha ("kernels", gfc_match_oacc_kernels, ST_OACC_KERNELS);
682 break;
683 case 'l':
684 matcha ("loop", gfc_match_oacc_loop, ST_OACC_LOOP);
685 break;
686 case 'r':
687 match ("routine", gfc_match_oacc_routine, ST_OACC_ROUTINE);
688 break;
689 case 'u':
690 matcha ("update", gfc_match_oacc_update, ST_OACC_UPDATE);
691 break;
692 case 'w':
693 matcha ("wait", gfc_match_oacc_wait, ST_OACC_WAIT);
694 break;
697 /* Directive not found or stored an error message.
698 Check and give up. */
700 if (gfc_error_check () == 0)
701 gfc_error_now ("Unclassifiable OpenACC directive at %C");
703 reject_statement ();
705 gfc_error_recovery ();
707 return ST_NONE;
709 do_spec_only:
710 reject_statement ();
711 gfc_clear_error ();
712 gfc_buffer_error (false);
713 gfc_current_locus = old_locus;
714 return ST_GET_FCN_CHARACTERISTICS;
717 /* Like match, but set a flag simd_matched if keyword matched
718 and if spec_only, goto do_spec_only without actually matching. */
719 #define matchs(keyword, subr, st) \
720 do { \
721 if (spec_only && gfc_match (keyword) == MATCH_YES) \
722 goto do_spec_only; \
723 if (match_word_omp_simd (keyword, subr, &old_locus, \
724 &simd_matched) == MATCH_YES) \
726 ret = st; \
727 goto finish; \
729 else \
730 undo_new_statement (); \
731 } while (0);
733 /* Like match, but don't match anything if not -fopenmp
734 and if spec_only, goto do_spec_only without actually matching. */
735 #define matcho(keyword, subr, st) \
736 do { \
737 if (!flag_openmp) \
739 else if (spec_only && gfc_match (keyword) == MATCH_YES) \
740 goto do_spec_only; \
741 else if (match_word (keyword, subr, &old_locus) \
742 == MATCH_YES) \
744 ret = st; \
745 goto finish; \
747 else \
748 undo_new_statement (); \
749 } while (0);
751 /* Like match, but set a flag simd_matched if keyword matched. */
752 #define matchds(keyword, subr, st) \
753 do { \
754 if (match_word_omp_simd (keyword, subr, &old_locus, \
755 &simd_matched) == MATCH_YES) \
757 ret = st; \
758 goto finish; \
760 else \
761 undo_new_statement (); \
762 } while (0);
764 /* Like match, but don't match anything if not -fopenmp. */
765 #define matchdo(keyword, subr, st) \
766 do { \
767 if (!flag_openmp) \
769 else if (match_word (keyword, subr, &old_locus) \
770 == MATCH_YES) \
772 ret = st; \
773 goto finish; \
775 else \
776 undo_new_statement (); \
777 } while (0);
779 static gfc_statement
780 decode_omp_directive (void)
782 locus old_locus;
783 char c;
784 bool simd_matched = false;
785 bool spec_only = false;
786 gfc_statement ret = ST_NONE;
787 bool pure_ok = true;
789 gfc_enforce_clean_symbol_state ();
791 gfc_clear_error (); /* Clear any pending errors. */
792 gfc_clear_warning (); /* Clear any pending warnings. */
794 if (gfc_current_state () == COMP_FUNCTION
795 && gfc_current_block ()->result->ts.kind == -1)
796 spec_only = true;
798 old_locus = gfc_current_locus;
800 /* General OpenMP directive matching: Instead of testing every possible
801 statement, we eliminate most possibilities by peeking at the
802 first character. */
804 c = gfc_peek_ascii_char ();
806 /* match is for directives that should be recognized only if
807 -fopenmp, matchs for directives that should be recognized
808 if either -fopenmp or -fopenmp-simd.
809 Handle only the directives allowed in PURE/ELEMENTAL procedures
810 first (those also shall not turn off implicit pure). */
811 switch (c)
813 case 'd':
814 matchds ("declare simd", gfc_match_omp_declare_simd,
815 ST_OMP_DECLARE_SIMD);
816 matchdo ("declare target", gfc_match_omp_declare_target,
817 ST_OMP_DECLARE_TARGET);
818 break;
819 case 's':
820 matchs ("simd", gfc_match_omp_simd, ST_OMP_SIMD);
821 break;
824 pure_ok = false;
825 if (flag_openmp && gfc_pure (NULL))
827 gfc_error_now ("OpenMP directives other than SIMD or DECLARE TARGET "
828 "at %C may not appear in PURE or ELEMENTAL procedures");
829 gfc_error_recovery ();
830 return ST_NONE;
833 /* match is for directives that should be recognized only if
834 -fopenmp, matchs for directives that should be recognized
835 if either -fopenmp or -fopenmp-simd. */
836 switch (c)
838 case 'a':
839 matcho ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
840 break;
841 case 'b':
842 matcho ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
843 break;
844 case 'c':
845 matcho ("cancellation% point", gfc_match_omp_cancellation_point,
846 ST_OMP_CANCELLATION_POINT);
847 matcho ("cancel", gfc_match_omp_cancel, ST_OMP_CANCEL);
848 matcho ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
849 break;
850 case 'd':
851 matchds ("declare reduction", gfc_match_omp_declare_reduction,
852 ST_OMP_DECLARE_REDUCTION);
853 matchs ("distribute parallel do simd",
854 gfc_match_omp_distribute_parallel_do_simd,
855 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD);
856 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do,
857 ST_OMP_DISTRIBUTE_PARALLEL_DO);
858 matchs ("distribute simd", gfc_match_omp_distribute_simd,
859 ST_OMP_DISTRIBUTE_SIMD);
860 matcho ("distribute", gfc_match_omp_distribute, ST_OMP_DISTRIBUTE);
861 matchs ("do simd", gfc_match_omp_do_simd, ST_OMP_DO_SIMD);
862 matcho ("do", gfc_match_omp_do, ST_OMP_DO);
863 break;
864 case 'e':
865 matcho ("end atomic", gfc_match_omp_eos, ST_OMP_END_ATOMIC);
866 matcho ("end critical", gfc_match_omp_end_critical, ST_OMP_END_CRITICAL);
867 matchs ("end distribute parallel do simd", gfc_match_omp_eos,
868 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD);
869 matcho ("end distribute parallel do", gfc_match_omp_eos,
870 ST_OMP_END_DISTRIBUTE_PARALLEL_DO);
871 matchs ("end distribute simd", gfc_match_omp_eos,
872 ST_OMP_END_DISTRIBUTE_SIMD);
873 matcho ("end distribute", gfc_match_omp_eos, ST_OMP_END_DISTRIBUTE);
874 matchs ("end do simd", gfc_match_omp_end_nowait, ST_OMP_END_DO_SIMD);
875 matcho ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
876 matchs ("end simd", gfc_match_omp_eos, ST_OMP_END_SIMD);
877 matcho ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
878 matcho ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
879 matchs ("end parallel do simd", gfc_match_omp_eos,
880 ST_OMP_END_PARALLEL_DO_SIMD);
881 matcho ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
882 matcho ("end parallel sections", gfc_match_omp_eos,
883 ST_OMP_END_PARALLEL_SECTIONS);
884 matcho ("end parallel workshare", gfc_match_omp_eos,
885 ST_OMP_END_PARALLEL_WORKSHARE);
886 matcho ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
887 matcho ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
888 matcho ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
889 matcho ("end target data", gfc_match_omp_eos, ST_OMP_END_TARGET_DATA);
890 matchs ("end target parallel do simd", gfc_match_omp_eos,
891 ST_OMP_END_TARGET_PARALLEL_DO_SIMD);
892 matcho ("end target parallel do", gfc_match_omp_eos,
893 ST_OMP_END_TARGET_PARALLEL_DO);
894 matcho ("end target parallel", gfc_match_omp_eos,
895 ST_OMP_END_TARGET_PARALLEL);
896 matchs ("end target simd", gfc_match_omp_eos, ST_OMP_END_TARGET_SIMD);
897 matchs ("end target teams distribute parallel do simd",
898 gfc_match_omp_eos,
899 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
900 matcho ("end target teams distribute parallel do", gfc_match_omp_eos,
901 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
902 matchs ("end target teams distribute simd", gfc_match_omp_eos,
903 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD);
904 matcho ("end target teams distribute", gfc_match_omp_eos,
905 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE);
906 matcho ("end target teams", gfc_match_omp_eos, ST_OMP_END_TARGET_TEAMS);
907 matcho ("end target", gfc_match_omp_eos, ST_OMP_END_TARGET);
908 matcho ("end taskgroup", gfc_match_omp_eos, ST_OMP_END_TASKGROUP);
909 matchs ("end taskloop simd", gfc_match_omp_eos,
910 ST_OMP_END_TASKLOOP_SIMD);
911 matcho ("end taskloop", gfc_match_omp_eos, ST_OMP_END_TASKLOOP);
912 matcho ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
913 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos,
914 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
915 matcho ("end teams distribute parallel do", gfc_match_omp_eos,
916 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO);
917 matchs ("end teams distribute simd", gfc_match_omp_eos,
918 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD);
919 matcho ("end teams distribute", gfc_match_omp_eos,
920 ST_OMP_END_TEAMS_DISTRIBUTE);
921 matcho ("end teams", gfc_match_omp_eos, ST_OMP_END_TEAMS);
922 matcho ("end workshare", gfc_match_omp_end_nowait,
923 ST_OMP_END_WORKSHARE);
924 break;
925 case 'f':
926 matcho ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
927 break;
928 case 'm':
929 matcho ("master", gfc_match_omp_master, ST_OMP_MASTER);
930 break;
931 case 'o':
932 if (flag_openmp && gfc_match ("ordered depend (") == MATCH_YES)
934 gfc_current_locus = old_locus;
935 matcho ("ordered", gfc_match_omp_ordered_depend,
936 ST_OMP_ORDERED_DEPEND);
938 else
939 matcho ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
940 break;
941 case 'p':
942 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
943 ST_OMP_PARALLEL_DO_SIMD);
944 matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
945 matcho ("parallel sections", gfc_match_omp_parallel_sections,
946 ST_OMP_PARALLEL_SECTIONS);
947 matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
948 ST_OMP_PARALLEL_WORKSHARE);
949 matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
950 break;
951 case 's':
952 matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
953 matcho ("section", gfc_match_omp_eos, ST_OMP_SECTION);
954 matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
955 break;
956 case 't':
957 matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
958 matcho ("target enter data", gfc_match_omp_target_enter_data,
959 ST_OMP_TARGET_ENTER_DATA);
960 matcho ("target exit data", gfc_match_omp_target_exit_data,
961 ST_OMP_TARGET_EXIT_DATA);
962 matchs ("target parallel do simd", gfc_match_omp_target_parallel_do_simd,
963 ST_OMP_TARGET_PARALLEL_DO_SIMD);
964 matcho ("target parallel do", gfc_match_omp_target_parallel_do,
965 ST_OMP_TARGET_PARALLEL_DO);
966 matcho ("target parallel", gfc_match_omp_target_parallel,
967 ST_OMP_TARGET_PARALLEL);
968 matchs ("target simd", gfc_match_omp_target_simd, ST_OMP_TARGET_SIMD);
969 matchs ("target teams distribute parallel do simd",
970 gfc_match_omp_target_teams_distribute_parallel_do_simd,
971 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
972 matcho ("target teams distribute parallel do",
973 gfc_match_omp_target_teams_distribute_parallel_do,
974 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
975 matchs ("target teams distribute simd",
976 gfc_match_omp_target_teams_distribute_simd,
977 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
978 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
979 ST_OMP_TARGET_TEAMS_DISTRIBUTE);
980 matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
981 matcho ("target update", gfc_match_omp_target_update,
982 ST_OMP_TARGET_UPDATE);
983 matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
984 matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
985 matchs ("taskloop simd", gfc_match_omp_taskloop_simd,
986 ST_OMP_TASKLOOP_SIMD);
987 matcho ("taskloop", gfc_match_omp_taskloop, ST_OMP_TASKLOOP);
988 matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
989 matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
990 matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
991 matchs ("teams distribute parallel do simd",
992 gfc_match_omp_teams_distribute_parallel_do_simd,
993 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
994 matcho ("teams distribute parallel do",
995 gfc_match_omp_teams_distribute_parallel_do,
996 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
997 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
998 ST_OMP_TEAMS_DISTRIBUTE_SIMD);
999 matcho ("teams distribute", gfc_match_omp_teams_distribute,
1000 ST_OMP_TEAMS_DISTRIBUTE);
1001 matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
1002 matchdo ("threadprivate", gfc_match_omp_threadprivate,
1003 ST_OMP_THREADPRIVATE);
1004 break;
1005 case 'w':
1006 matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
1007 break;
1010 /* All else has failed, so give up. See if any of the matchers has
1011 stored an error message of some sort. Don't error out if
1012 not -fopenmp and simd_matched is false, i.e. if a directive other
1013 than one marked with match has been seen. */
1015 if (flag_openmp || simd_matched)
1017 if (!gfc_error_check ())
1018 gfc_error_now ("Unclassifiable OpenMP directive at %C");
1021 reject_statement ();
1023 gfc_error_recovery ();
1025 return ST_NONE;
1027 finish:
1028 if (!pure_ok)
1030 gfc_unset_implicit_pure (NULL);
1032 if (!flag_openmp && gfc_pure (NULL))
1034 gfc_error_now ("OpenMP directives other than SIMD or DECLARE TARGET "
1035 "at %C may not appear in PURE or ELEMENTAL "
1036 "procedures");
1037 reject_statement ();
1038 gfc_error_recovery ();
1039 return ST_NONE;
1042 return ret;
1044 do_spec_only:
1045 reject_statement ();
1046 gfc_clear_error ();
1047 gfc_buffer_error (false);
1048 gfc_current_locus = old_locus;
1049 return ST_GET_FCN_CHARACTERISTICS;
1052 static gfc_statement
1053 decode_gcc_attribute (void)
1055 locus old_locus;
1057 gfc_enforce_clean_symbol_state ();
1059 gfc_clear_error (); /* Clear any pending errors. */
1060 gfc_clear_warning (); /* Clear any pending warnings. */
1061 old_locus = gfc_current_locus;
1063 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
1065 /* All else has failed, so give up. See if any of the matchers has
1066 stored an error message of some sort. */
1068 if (!gfc_error_check ())
1069 gfc_error_now ("Unclassifiable GCC directive at %C");
1071 reject_statement ();
1073 gfc_error_recovery ();
1075 return ST_NONE;
1078 #undef match
1080 /* Assert next length characters to be equal to token in free form. */
1082 static void
1083 verify_token_free (const char* token, int length, bool last_was_use_stmt)
1085 int i;
1086 char c;
1088 c = gfc_next_ascii_char ();
1089 for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
1090 gcc_assert (c == token[i]);
1092 gcc_assert (gfc_is_whitespace(c));
1093 gfc_gobble_whitespace ();
1094 if (last_was_use_stmt)
1095 use_modules ();
1098 /* Get the next statement in free form source. */
1100 static gfc_statement
1101 next_free (void)
1103 match m;
1104 int i, cnt, at_bol;
1105 char c;
1107 at_bol = gfc_at_bol ();
1108 gfc_gobble_whitespace ();
1110 c = gfc_peek_ascii_char ();
1112 if (ISDIGIT (c))
1114 char d;
1116 /* Found a statement label? */
1117 m = gfc_match_st_label (&gfc_statement_label);
1119 d = gfc_peek_ascii_char ();
1120 if (m != MATCH_YES || !gfc_is_whitespace (d))
1122 gfc_match_small_literal_int (&i, &cnt);
1124 if (cnt > 5)
1125 gfc_error_now ("Too many digits in statement label at %C");
1127 if (i == 0)
1128 gfc_error_now ("Zero is not a valid statement label at %C");
1131 c = gfc_next_ascii_char ();
1132 while (ISDIGIT(c));
1134 if (!gfc_is_whitespace (c))
1135 gfc_error_now ("Non-numeric character in statement label at %C");
1137 return ST_NONE;
1139 else
1141 label_locus = gfc_current_locus;
1143 gfc_gobble_whitespace ();
1145 if (at_bol && gfc_peek_ascii_char () == ';')
1147 gfc_error_now ("Semicolon at %C needs to be preceded by "
1148 "statement");
1149 gfc_next_ascii_char (); /* Eat up the semicolon. */
1150 return ST_NONE;
1153 if (gfc_match_eos () == MATCH_YES)
1154 gfc_error_now ("Statement label without statement at %L",
1155 &label_locus);
1158 else if (c == '!')
1160 /* Comments have already been skipped by the time we get here,
1161 except for GCC attributes and OpenMP/OpenACC directives. */
1163 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
1164 c = gfc_peek_ascii_char ();
1166 if (c == 'g')
1168 int i;
1170 c = gfc_next_ascii_char ();
1171 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
1172 gcc_assert (c == "gcc$"[i]);
1174 gfc_gobble_whitespace ();
1175 return decode_gcc_attribute ();
1178 else if (c == '$')
1180 /* Since both OpenMP and OpenACC directives starts with
1181 !$ character sequence, we must check all flags combinations */
1182 if ((flag_openmp || flag_openmp_simd)
1183 && !flag_openacc)
1185 verify_token_free ("$omp", 4, last_was_use_stmt);
1186 return decode_omp_directive ();
1188 else if ((flag_openmp || flag_openmp_simd)
1189 && flag_openacc)
1191 gfc_next_ascii_char (); /* Eat up dollar character */
1192 c = gfc_peek_ascii_char ();
1194 if (c == 'o')
1196 verify_token_free ("omp", 3, last_was_use_stmt);
1197 return decode_omp_directive ();
1199 else if (c == 'a')
1201 verify_token_free ("acc", 3, last_was_use_stmt);
1202 return decode_oacc_directive ();
1205 else if (flag_openacc)
1207 verify_token_free ("$acc", 4, last_was_use_stmt);
1208 return decode_oacc_directive ();
1211 gcc_unreachable ();
1214 if (at_bol && c == ';')
1216 if (!(gfc_option.allow_std & GFC_STD_F2008))
1217 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1218 "statement");
1219 gfc_next_ascii_char (); /* Eat up the semicolon. */
1220 return ST_NONE;
1223 return decode_statement ();
1226 /* Assert next length characters to be equal to token in fixed form. */
1228 static bool
1229 verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
1231 int i;
1232 char c = gfc_next_char_literal (NONSTRING);
1234 for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
1235 gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
1237 if (c != ' ' && c != '0')
1239 gfc_buffer_error (false);
1240 gfc_error ("Bad continuation line at %C");
1241 return false;
1243 if (last_was_use_stmt)
1244 use_modules ();
1246 return true;
1249 /* Get the next statement in fixed-form source. */
1251 static gfc_statement
1252 next_fixed (void)
1254 int label, digit_flag, i;
1255 locus loc;
1256 gfc_char_t c;
1258 if (!gfc_at_bol ())
1259 return decode_statement ();
1261 /* Skip past the current label field, parsing a statement label if
1262 one is there. This is a weird number parser, since the number is
1263 contained within five columns and can have any kind of embedded
1264 spaces. We also check for characters that make the rest of the
1265 line a comment. */
1267 label = 0;
1268 digit_flag = 0;
1270 for (i = 0; i < 5; i++)
1272 c = gfc_next_char_literal (NONSTRING);
1274 switch (c)
1276 case ' ':
1277 break;
1279 case '0':
1280 case '1':
1281 case '2':
1282 case '3':
1283 case '4':
1284 case '5':
1285 case '6':
1286 case '7':
1287 case '8':
1288 case '9':
1289 label = label * 10 + ((unsigned char) c - '0');
1290 label_locus = gfc_current_locus;
1291 digit_flag = 1;
1292 break;
1294 /* Comments have already been skipped by the time we get
1295 here, except for GCC attributes and OpenMP directives. */
1297 case '*':
1298 c = gfc_next_char_literal (NONSTRING);
1300 if (TOLOWER (c) == 'g')
1302 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
1303 gcc_assert (TOLOWER (c) == "gcc$"[i]);
1305 return decode_gcc_attribute ();
1307 else if (c == '$')
1309 if ((flag_openmp || flag_openmp_simd)
1310 && !flag_openacc)
1312 if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
1313 return ST_NONE;
1314 return decode_omp_directive ();
1316 else if ((flag_openmp || flag_openmp_simd)
1317 && flag_openacc)
1319 c = gfc_next_char_literal(NONSTRING);
1320 if (c == 'o' || c == 'O')
1322 if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
1323 return ST_NONE;
1324 return decode_omp_directive ();
1326 else if (c == 'a' || c == 'A')
1328 if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
1329 return ST_NONE;
1330 return decode_oacc_directive ();
1333 else if (flag_openacc)
1335 if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
1336 return ST_NONE;
1337 return decode_oacc_directive ();
1340 gcc_fallthrough ();
1342 /* Comments have already been skipped by the time we get
1343 here so don't bother checking for them. */
1345 default:
1346 gfc_buffer_error (false);
1347 gfc_error ("Non-numeric character in statement label at %C");
1348 return ST_NONE;
1352 if (digit_flag)
1354 if (label == 0)
1355 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1356 else
1358 /* We've found a valid statement label. */
1359 gfc_statement_label = gfc_get_st_label (label);
1363 /* Since this line starts a statement, it cannot be a continuation
1364 of a previous statement. If we see something here besides a
1365 space or zero, it must be a bad continuation line. */
1367 c = gfc_next_char_literal (NONSTRING);
1368 if (c == '\n')
1369 goto blank_line;
1371 if (c != ' ' && c != '0')
1373 gfc_buffer_error (false);
1374 gfc_error ("Bad continuation line at %C");
1375 return ST_NONE;
1378 /* Now that we've taken care of the statement label columns, we have
1379 to make sure that the first nonblank character is not a '!'. If
1380 it is, the rest of the line is a comment. */
1384 loc = gfc_current_locus;
1385 c = gfc_next_char_literal (NONSTRING);
1387 while (gfc_is_whitespace (c));
1389 if (c == '!')
1390 goto blank_line;
1391 gfc_current_locus = loc;
1393 if (c == ';')
1395 if (digit_flag)
1396 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1397 else if (!(gfc_option.allow_std & GFC_STD_F2008))
1398 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1399 "statement");
1400 return ST_NONE;
1403 if (gfc_match_eos () == MATCH_YES)
1404 goto blank_line;
1406 /* At this point, we've got a nonblank statement to parse. */
1407 return decode_statement ();
1409 blank_line:
1410 if (digit_flag)
1411 gfc_error_now ("Statement label without statement at %L", &label_locus);
1413 gfc_current_locus.lb->truncated = 0;
1414 gfc_advance_line ();
1415 return ST_NONE;
1419 /* Return the next non-ST_NONE statement to the caller. We also worry
1420 about including files and the ends of include files at this stage. */
1422 static gfc_statement
1423 next_statement (void)
1425 gfc_statement st;
1426 locus old_locus;
1428 gfc_enforce_clean_symbol_state ();
1430 gfc_new_block = NULL;
1432 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
1433 gfc_current_ns->old_data = gfc_current_ns->data;
1434 for (;;)
1436 gfc_statement_label = NULL;
1437 gfc_buffer_error (true);
1439 if (gfc_at_eol ())
1440 gfc_advance_line ();
1442 gfc_skip_comments ();
1444 if (gfc_at_end ())
1446 st = ST_NONE;
1447 break;
1450 if (gfc_define_undef_line ())
1451 continue;
1453 old_locus = gfc_current_locus;
1455 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
1457 if (st != ST_NONE)
1458 break;
1461 gfc_buffer_error (false);
1463 if (st == ST_GET_FCN_CHARACTERISTICS)
1465 if (gfc_statement_label != NULL)
1467 gfc_free_st_label (gfc_statement_label);
1468 gfc_statement_label = NULL;
1470 gfc_current_locus = old_locus;
1473 if (st != ST_NONE)
1474 check_statement_label (st);
1476 return st;
1480 /****************************** Parser ***********************************/
1482 /* The parser subroutines are of type 'try' that fail if the file ends
1483 unexpectedly. */
1485 /* Macros that expand to case-labels for various classes of
1486 statements. Start with executable statements that directly do
1487 things. */
1489 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1490 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1491 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1492 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1493 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1494 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1495 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1496 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1497 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1498 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1499 case ST_OMP_TARGET_UPDATE: case ST_OMP_TARGET_ENTER_DATA: \
1500 case ST_OMP_TARGET_EXIT_DATA: case ST_OMP_ORDERED_DEPEND: \
1501 case ST_ERROR_STOP: case ST_SYNC_ALL: \
1502 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1503 case ST_EVENT_POST: case ST_EVENT_WAIT: case ST_FAIL_IMAGE: \
1504 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1505 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1507 /* Statements that mark other executable statements. */
1509 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1510 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1511 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1512 case ST_OMP_PARALLEL: \
1513 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1514 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1515 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1516 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1517 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1518 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1519 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1520 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1521 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1522 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1523 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1524 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1525 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1526 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1527 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1528 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1529 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_TARGET_PARALLEL: \
1530 case ST_OMP_TARGET_PARALLEL_DO: case ST_OMP_TARGET_PARALLEL_DO_SIMD: \
1531 case ST_OMP_TARGET_SIMD: case ST_OMP_TASKLOOP: case ST_OMP_TASKLOOP_SIMD: \
1532 case ST_CRITICAL: \
1533 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1534 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: \
1535 case ST_OACC_KERNELS_LOOP: case ST_OACC_ATOMIC
1537 /* Declaration statements */
1539 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1540 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1541 case ST_TYPE: case ST_INTERFACE: case ST_PROCEDURE: case ST_OACC_ROUTINE: \
1542 case ST_OACC_DECLARE
1544 /* OpenMP declaration statements. */
1546 #define case_omp_decl case ST_OMP_THREADPRIVATE: case ST_OMP_DECLARE_SIMD: \
1547 case ST_OMP_DECLARE_TARGET: case ST_OMP_DECLARE_REDUCTION
1549 /* Block end statements. Errors associated with interchanging these
1550 are detected in gfc_match_end(). */
1552 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1553 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1554 case ST_END_BLOCK: case ST_END_ASSOCIATE
1557 /* Push a new state onto the stack. */
1559 static void
1560 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
1562 p->state = new_state;
1563 p->previous = gfc_state_stack;
1564 p->sym = sym;
1565 p->head = p->tail = NULL;
1566 p->do_variable = NULL;
1567 if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
1568 p->ext.oacc_declare_clauses = NULL;
1570 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1571 construct statement was accepted right before pushing the state. Thus,
1572 the construct's gfc_code is available as tail of the parent state. */
1573 gcc_assert (gfc_state_stack);
1574 p->construct = gfc_state_stack->tail;
1576 gfc_state_stack = p;
1580 /* Pop the current state. */
1581 static void
1582 pop_state (void)
1584 gfc_state_stack = gfc_state_stack->previous;
1588 /* Try to find the given state in the state stack. */
1590 bool
1591 gfc_find_state (gfc_compile_state state)
1593 gfc_state_data *p;
1595 for (p = gfc_state_stack; p; p = p->previous)
1596 if (p->state == state)
1597 break;
1599 return (p == NULL) ? false : true;
1603 /* Starts a new level in the statement list. */
1605 static gfc_code *
1606 new_level (gfc_code *q)
1608 gfc_code *p;
1610 p = q->block = gfc_get_code (EXEC_NOP);
1612 gfc_state_stack->head = gfc_state_stack->tail = p;
1614 return p;
1618 /* Add the current new_st code structure and adds it to the current
1619 program unit. As a side-effect, it zeroes the new_st. */
1621 static gfc_code *
1622 add_statement (void)
1624 gfc_code *p;
1626 p = XCNEW (gfc_code);
1627 *p = new_st;
1629 p->loc = gfc_current_locus;
1631 if (gfc_state_stack->head == NULL)
1632 gfc_state_stack->head = p;
1633 else
1634 gfc_state_stack->tail->next = p;
1636 while (p->next != NULL)
1637 p = p->next;
1639 gfc_state_stack->tail = p;
1641 gfc_clear_new_st ();
1643 return p;
1647 /* Frees everything associated with the current statement. */
1649 static void
1650 undo_new_statement (void)
1652 gfc_free_statements (new_st.block);
1653 gfc_free_statements (new_st.next);
1654 gfc_free_statement (&new_st);
1655 gfc_clear_new_st ();
1659 /* If the current statement has a statement label, make sure that it
1660 is allowed to, or should have one. */
1662 static void
1663 check_statement_label (gfc_statement st)
1665 gfc_sl_type type;
1667 if (gfc_statement_label == NULL)
1669 if (st == ST_FORMAT)
1670 gfc_error ("FORMAT statement at %L does not have a statement label",
1671 &new_st.loc);
1672 return;
1675 switch (st)
1677 case ST_END_PROGRAM:
1678 case ST_END_FUNCTION:
1679 case ST_END_SUBROUTINE:
1680 case ST_ENDDO:
1681 case ST_ENDIF:
1682 case ST_END_SELECT:
1683 case ST_END_CRITICAL:
1684 case ST_END_BLOCK:
1685 case ST_END_ASSOCIATE:
1686 case_executable:
1687 case_exec_markers:
1688 if (st == ST_ENDDO || st == ST_CONTINUE)
1689 type = ST_LABEL_DO_TARGET;
1690 else
1691 type = ST_LABEL_TARGET;
1692 break;
1694 case ST_FORMAT:
1695 type = ST_LABEL_FORMAT;
1696 break;
1698 /* Statement labels are not restricted from appearing on a
1699 particular line. However, there are plenty of situations
1700 where the resulting label can't be referenced. */
1702 default:
1703 type = ST_LABEL_BAD_TARGET;
1704 break;
1707 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1709 new_st.here = gfc_statement_label;
1713 /* Figures out what the enclosing program unit is. This will be a
1714 function, subroutine, program, block data or module. */
1716 gfc_state_data *
1717 gfc_enclosing_unit (gfc_compile_state * result)
1719 gfc_state_data *p;
1721 for (p = gfc_state_stack; p; p = p->previous)
1722 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1723 || p->state == COMP_MODULE || p->state == COMP_SUBMODULE
1724 || p->state == COMP_BLOCK_DATA || p->state == COMP_PROGRAM)
1727 if (result != NULL)
1728 *result = p->state;
1729 return p;
1732 if (result != NULL)
1733 *result = COMP_PROGRAM;
1734 return NULL;
1738 /* Translate a statement enum to a string. */
1740 const char *
1741 gfc_ascii_statement (gfc_statement st)
1743 const char *p;
1745 switch (st)
1747 case ST_ARITHMETIC_IF:
1748 p = _("arithmetic IF");
1749 break;
1750 case ST_ALLOCATE:
1751 p = "ALLOCATE";
1752 break;
1753 case ST_ASSOCIATE:
1754 p = "ASSOCIATE";
1755 break;
1756 case ST_ATTR_DECL:
1757 p = _("attribute declaration");
1758 break;
1759 case ST_BACKSPACE:
1760 p = "BACKSPACE";
1761 break;
1762 case ST_BLOCK:
1763 p = "BLOCK";
1764 break;
1765 case ST_BLOCK_DATA:
1766 p = "BLOCK DATA";
1767 break;
1768 case ST_CALL:
1769 p = "CALL";
1770 break;
1771 case ST_CASE:
1772 p = "CASE";
1773 break;
1774 case ST_CLOSE:
1775 p = "CLOSE";
1776 break;
1777 case ST_COMMON:
1778 p = "COMMON";
1779 break;
1780 case ST_CONTINUE:
1781 p = "CONTINUE";
1782 break;
1783 case ST_CONTAINS:
1784 p = "CONTAINS";
1785 break;
1786 case ST_CRITICAL:
1787 p = "CRITICAL";
1788 break;
1789 case ST_CYCLE:
1790 p = "CYCLE";
1791 break;
1792 case ST_DATA_DECL:
1793 p = _("data declaration");
1794 break;
1795 case ST_DATA:
1796 p = "DATA";
1797 break;
1798 case ST_DEALLOCATE:
1799 p = "DEALLOCATE";
1800 break;
1801 case ST_MAP:
1802 p = "MAP";
1803 break;
1804 case ST_UNION:
1805 p = "UNION";
1806 break;
1807 case ST_STRUCTURE_DECL:
1808 p = "STRUCTURE";
1809 break;
1810 case ST_DERIVED_DECL:
1811 p = _("derived type declaration");
1812 break;
1813 case ST_DO:
1814 p = "DO";
1815 break;
1816 case ST_ELSE:
1817 p = "ELSE";
1818 break;
1819 case ST_ELSEIF:
1820 p = "ELSE IF";
1821 break;
1822 case ST_ELSEWHERE:
1823 p = "ELSEWHERE";
1824 break;
1825 case ST_EVENT_POST:
1826 p = "EVENT POST";
1827 break;
1828 case ST_EVENT_WAIT:
1829 p = "EVENT WAIT";
1830 break;
1831 case ST_FAIL_IMAGE:
1832 p = "FAIL IMAGE";
1833 break;
1834 case ST_END_ASSOCIATE:
1835 p = "END ASSOCIATE";
1836 break;
1837 case ST_END_BLOCK:
1838 p = "END BLOCK";
1839 break;
1840 case ST_END_BLOCK_DATA:
1841 p = "END BLOCK DATA";
1842 break;
1843 case ST_END_CRITICAL:
1844 p = "END CRITICAL";
1845 break;
1846 case ST_ENDDO:
1847 p = "END DO";
1848 break;
1849 case ST_END_FILE:
1850 p = "END FILE";
1851 break;
1852 case ST_END_FORALL:
1853 p = "END FORALL";
1854 break;
1855 case ST_END_FUNCTION:
1856 p = "END FUNCTION";
1857 break;
1858 case ST_ENDIF:
1859 p = "END IF";
1860 break;
1861 case ST_END_INTERFACE:
1862 p = "END INTERFACE";
1863 break;
1864 case ST_END_MODULE:
1865 p = "END MODULE";
1866 break;
1867 case ST_END_SUBMODULE:
1868 p = "END SUBMODULE";
1869 break;
1870 case ST_END_PROGRAM:
1871 p = "END PROGRAM";
1872 break;
1873 case ST_END_SELECT:
1874 p = "END SELECT";
1875 break;
1876 case ST_END_SUBROUTINE:
1877 p = "END SUBROUTINE";
1878 break;
1879 case ST_END_WHERE:
1880 p = "END WHERE";
1881 break;
1882 case ST_END_STRUCTURE:
1883 p = "END STRUCTURE";
1884 break;
1885 case ST_END_UNION:
1886 p = "END UNION";
1887 break;
1888 case ST_END_MAP:
1889 p = "END MAP";
1890 break;
1891 case ST_END_TYPE:
1892 p = "END TYPE";
1893 break;
1894 case ST_ENTRY:
1895 p = "ENTRY";
1896 break;
1897 case ST_EQUIVALENCE:
1898 p = "EQUIVALENCE";
1899 break;
1900 case ST_ERROR_STOP:
1901 p = "ERROR STOP";
1902 break;
1903 case ST_EXIT:
1904 p = "EXIT";
1905 break;
1906 case ST_FLUSH:
1907 p = "FLUSH";
1908 break;
1909 case ST_FORALL_BLOCK: /* Fall through */
1910 case ST_FORALL:
1911 p = "FORALL";
1912 break;
1913 case ST_FORMAT:
1914 p = "FORMAT";
1915 break;
1916 case ST_FUNCTION:
1917 p = "FUNCTION";
1918 break;
1919 case ST_GENERIC:
1920 p = "GENERIC";
1921 break;
1922 case ST_GOTO:
1923 p = "GOTO";
1924 break;
1925 case ST_IF_BLOCK:
1926 p = _("block IF");
1927 break;
1928 case ST_IMPLICIT:
1929 p = "IMPLICIT";
1930 break;
1931 case ST_IMPLICIT_NONE:
1932 p = "IMPLICIT NONE";
1933 break;
1934 case ST_IMPLIED_ENDDO:
1935 p = _("implied END DO");
1936 break;
1937 case ST_IMPORT:
1938 p = "IMPORT";
1939 break;
1940 case ST_INQUIRE:
1941 p = "INQUIRE";
1942 break;
1943 case ST_INTERFACE:
1944 p = "INTERFACE";
1945 break;
1946 case ST_LOCK:
1947 p = "LOCK";
1948 break;
1949 case ST_PARAMETER:
1950 p = "PARAMETER";
1951 break;
1952 case ST_PRIVATE:
1953 p = "PRIVATE";
1954 break;
1955 case ST_PUBLIC:
1956 p = "PUBLIC";
1957 break;
1958 case ST_MODULE:
1959 p = "MODULE";
1960 break;
1961 case ST_SUBMODULE:
1962 p = "SUBMODULE";
1963 break;
1964 case ST_PAUSE:
1965 p = "PAUSE";
1966 break;
1967 case ST_MODULE_PROC:
1968 p = "MODULE PROCEDURE";
1969 break;
1970 case ST_NAMELIST:
1971 p = "NAMELIST";
1972 break;
1973 case ST_NULLIFY:
1974 p = "NULLIFY";
1975 break;
1976 case ST_OPEN:
1977 p = "OPEN";
1978 break;
1979 case ST_PROGRAM:
1980 p = "PROGRAM";
1981 break;
1982 case ST_PROCEDURE:
1983 p = "PROCEDURE";
1984 break;
1985 case ST_READ:
1986 p = "READ";
1987 break;
1988 case ST_RETURN:
1989 p = "RETURN";
1990 break;
1991 case ST_REWIND:
1992 p = "REWIND";
1993 break;
1994 case ST_STOP:
1995 p = "STOP";
1996 break;
1997 case ST_SYNC_ALL:
1998 p = "SYNC ALL";
1999 break;
2000 case ST_SYNC_IMAGES:
2001 p = "SYNC IMAGES";
2002 break;
2003 case ST_SYNC_MEMORY:
2004 p = "SYNC MEMORY";
2005 break;
2006 case ST_SUBROUTINE:
2007 p = "SUBROUTINE";
2008 break;
2009 case ST_TYPE:
2010 p = "TYPE";
2011 break;
2012 case ST_UNLOCK:
2013 p = "UNLOCK";
2014 break;
2015 case ST_USE:
2016 p = "USE";
2017 break;
2018 case ST_WHERE_BLOCK: /* Fall through */
2019 case ST_WHERE:
2020 p = "WHERE";
2021 break;
2022 case ST_WAIT:
2023 p = "WAIT";
2024 break;
2025 case ST_WRITE:
2026 p = "WRITE";
2027 break;
2028 case ST_ASSIGNMENT:
2029 p = _("assignment");
2030 break;
2031 case ST_POINTER_ASSIGNMENT:
2032 p = _("pointer assignment");
2033 break;
2034 case ST_SELECT_CASE:
2035 p = "SELECT CASE";
2036 break;
2037 case ST_SELECT_TYPE:
2038 p = "SELECT TYPE";
2039 break;
2040 case ST_TYPE_IS:
2041 p = "TYPE IS";
2042 break;
2043 case ST_CLASS_IS:
2044 p = "CLASS IS";
2045 break;
2046 case ST_SEQUENCE:
2047 p = "SEQUENCE";
2048 break;
2049 case ST_SIMPLE_IF:
2050 p = _("simple IF");
2051 break;
2052 case ST_STATEMENT_FUNCTION:
2053 p = "STATEMENT FUNCTION";
2054 break;
2055 case ST_LABEL_ASSIGNMENT:
2056 p = "LABEL ASSIGNMENT";
2057 break;
2058 case ST_ENUM:
2059 p = "ENUM DEFINITION";
2060 break;
2061 case ST_ENUMERATOR:
2062 p = "ENUMERATOR DEFINITION";
2063 break;
2064 case ST_END_ENUM:
2065 p = "END ENUM";
2066 break;
2067 case ST_OACC_PARALLEL_LOOP:
2068 p = "!$ACC PARALLEL LOOP";
2069 break;
2070 case ST_OACC_END_PARALLEL_LOOP:
2071 p = "!$ACC END PARALLEL LOOP";
2072 break;
2073 case ST_OACC_PARALLEL:
2074 p = "!$ACC PARALLEL";
2075 break;
2076 case ST_OACC_END_PARALLEL:
2077 p = "!$ACC END PARALLEL";
2078 break;
2079 case ST_OACC_KERNELS:
2080 p = "!$ACC KERNELS";
2081 break;
2082 case ST_OACC_END_KERNELS:
2083 p = "!$ACC END KERNELS";
2084 break;
2085 case ST_OACC_KERNELS_LOOP:
2086 p = "!$ACC KERNELS LOOP";
2087 break;
2088 case ST_OACC_END_KERNELS_LOOP:
2089 p = "!$ACC END KERNELS LOOP";
2090 break;
2091 case ST_OACC_DATA:
2092 p = "!$ACC DATA";
2093 break;
2094 case ST_OACC_END_DATA:
2095 p = "!$ACC END DATA";
2096 break;
2097 case ST_OACC_HOST_DATA:
2098 p = "!$ACC HOST_DATA";
2099 break;
2100 case ST_OACC_END_HOST_DATA:
2101 p = "!$ACC END HOST_DATA";
2102 break;
2103 case ST_OACC_LOOP:
2104 p = "!$ACC LOOP";
2105 break;
2106 case ST_OACC_END_LOOP:
2107 p = "!$ACC END LOOP";
2108 break;
2109 case ST_OACC_DECLARE:
2110 p = "!$ACC DECLARE";
2111 break;
2112 case ST_OACC_UPDATE:
2113 p = "!$ACC UPDATE";
2114 break;
2115 case ST_OACC_WAIT:
2116 p = "!$ACC WAIT";
2117 break;
2118 case ST_OACC_CACHE:
2119 p = "!$ACC CACHE";
2120 break;
2121 case ST_OACC_ENTER_DATA:
2122 p = "!$ACC ENTER DATA";
2123 break;
2124 case ST_OACC_EXIT_DATA:
2125 p = "!$ACC EXIT DATA";
2126 break;
2127 case ST_OACC_ROUTINE:
2128 p = "!$ACC ROUTINE";
2129 break;
2130 case ST_OACC_ATOMIC:
2131 p = "!ACC ATOMIC";
2132 break;
2133 case ST_OACC_END_ATOMIC:
2134 p = "!ACC END ATOMIC";
2135 break;
2136 case ST_OMP_ATOMIC:
2137 p = "!$OMP ATOMIC";
2138 break;
2139 case ST_OMP_BARRIER:
2140 p = "!$OMP BARRIER";
2141 break;
2142 case ST_OMP_CANCEL:
2143 p = "!$OMP CANCEL";
2144 break;
2145 case ST_OMP_CANCELLATION_POINT:
2146 p = "!$OMP CANCELLATION POINT";
2147 break;
2148 case ST_OMP_CRITICAL:
2149 p = "!$OMP CRITICAL";
2150 break;
2151 case ST_OMP_DECLARE_REDUCTION:
2152 p = "!$OMP DECLARE REDUCTION";
2153 break;
2154 case ST_OMP_DECLARE_SIMD:
2155 p = "!$OMP DECLARE SIMD";
2156 break;
2157 case ST_OMP_DECLARE_TARGET:
2158 p = "!$OMP DECLARE TARGET";
2159 break;
2160 case ST_OMP_DISTRIBUTE:
2161 p = "!$OMP DISTRIBUTE";
2162 break;
2163 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
2164 p = "!$OMP DISTRIBUTE PARALLEL DO";
2165 break;
2166 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
2167 p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
2168 break;
2169 case ST_OMP_DISTRIBUTE_SIMD:
2170 p = "!$OMP DISTRIBUTE SIMD";
2171 break;
2172 case ST_OMP_DO:
2173 p = "!$OMP DO";
2174 break;
2175 case ST_OMP_DO_SIMD:
2176 p = "!$OMP DO SIMD";
2177 break;
2178 case ST_OMP_END_ATOMIC:
2179 p = "!$OMP END ATOMIC";
2180 break;
2181 case ST_OMP_END_CRITICAL:
2182 p = "!$OMP END CRITICAL";
2183 break;
2184 case ST_OMP_END_DISTRIBUTE:
2185 p = "!$OMP END DISTRIBUTE";
2186 break;
2187 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
2188 p = "!$OMP END DISTRIBUTE PARALLEL DO";
2189 break;
2190 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
2191 p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
2192 break;
2193 case ST_OMP_END_DISTRIBUTE_SIMD:
2194 p = "!$OMP END DISTRIBUTE SIMD";
2195 break;
2196 case ST_OMP_END_DO:
2197 p = "!$OMP END DO";
2198 break;
2199 case ST_OMP_END_DO_SIMD:
2200 p = "!$OMP END DO SIMD";
2201 break;
2202 case ST_OMP_END_SIMD:
2203 p = "!$OMP END SIMD";
2204 break;
2205 case ST_OMP_END_MASTER:
2206 p = "!$OMP END MASTER";
2207 break;
2208 case ST_OMP_END_ORDERED:
2209 p = "!$OMP END ORDERED";
2210 break;
2211 case ST_OMP_END_PARALLEL:
2212 p = "!$OMP END PARALLEL";
2213 break;
2214 case ST_OMP_END_PARALLEL_DO:
2215 p = "!$OMP END PARALLEL DO";
2216 break;
2217 case ST_OMP_END_PARALLEL_DO_SIMD:
2218 p = "!$OMP END PARALLEL DO SIMD";
2219 break;
2220 case ST_OMP_END_PARALLEL_SECTIONS:
2221 p = "!$OMP END PARALLEL SECTIONS";
2222 break;
2223 case ST_OMP_END_PARALLEL_WORKSHARE:
2224 p = "!$OMP END PARALLEL WORKSHARE";
2225 break;
2226 case ST_OMP_END_SECTIONS:
2227 p = "!$OMP END SECTIONS";
2228 break;
2229 case ST_OMP_END_SINGLE:
2230 p = "!$OMP END SINGLE";
2231 break;
2232 case ST_OMP_END_TASK:
2233 p = "!$OMP END TASK";
2234 break;
2235 case ST_OMP_END_TARGET:
2236 p = "!$OMP END TARGET";
2237 break;
2238 case ST_OMP_END_TARGET_DATA:
2239 p = "!$OMP END TARGET DATA";
2240 break;
2241 case ST_OMP_END_TARGET_PARALLEL:
2242 p = "!$OMP END TARGET PARALLEL";
2243 break;
2244 case ST_OMP_END_TARGET_PARALLEL_DO:
2245 p = "!$OMP END TARGET PARALLEL DO";
2246 break;
2247 case ST_OMP_END_TARGET_PARALLEL_DO_SIMD:
2248 p = "!$OMP END TARGET PARALLEL DO SIMD";
2249 break;
2250 case ST_OMP_END_TARGET_SIMD:
2251 p = "!$OMP END TARGET SIMD";
2252 break;
2253 case ST_OMP_END_TARGET_TEAMS:
2254 p = "!$OMP END TARGET TEAMS";
2255 break;
2256 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
2257 p = "!$OMP END TARGET TEAMS DISTRIBUTE";
2258 break;
2259 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2260 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2261 break;
2262 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2263 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2264 break;
2265 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
2266 p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2267 break;
2268 case ST_OMP_END_TASKGROUP:
2269 p = "!$OMP END TASKGROUP";
2270 break;
2271 case ST_OMP_END_TASKLOOP:
2272 p = "!$OMP END TASKLOOP";
2273 break;
2274 case ST_OMP_END_TASKLOOP_SIMD:
2275 p = "!$OMP END TASKLOOP SIMD";
2276 break;
2277 case ST_OMP_END_TEAMS:
2278 p = "!$OMP END TEAMS";
2279 break;
2280 case ST_OMP_END_TEAMS_DISTRIBUTE:
2281 p = "!$OMP END TEAMS DISTRIBUTE";
2282 break;
2283 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
2284 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2285 break;
2286 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2287 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2288 break;
2289 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
2290 p = "!$OMP END TEAMS DISTRIBUTE SIMD";
2291 break;
2292 case ST_OMP_END_WORKSHARE:
2293 p = "!$OMP END WORKSHARE";
2294 break;
2295 case ST_OMP_FLUSH:
2296 p = "!$OMP FLUSH";
2297 break;
2298 case ST_OMP_MASTER:
2299 p = "!$OMP MASTER";
2300 break;
2301 case ST_OMP_ORDERED:
2302 case ST_OMP_ORDERED_DEPEND:
2303 p = "!$OMP ORDERED";
2304 break;
2305 case ST_OMP_PARALLEL:
2306 p = "!$OMP PARALLEL";
2307 break;
2308 case ST_OMP_PARALLEL_DO:
2309 p = "!$OMP PARALLEL DO";
2310 break;
2311 case ST_OMP_PARALLEL_DO_SIMD:
2312 p = "!$OMP PARALLEL DO SIMD";
2313 break;
2314 case ST_OMP_PARALLEL_SECTIONS:
2315 p = "!$OMP PARALLEL SECTIONS";
2316 break;
2317 case ST_OMP_PARALLEL_WORKSHARE:
2318 p = "!$OMP PARALLEL WORKSHARE";
2319 break;
2320 case ST_OMP_SECTIONS:
2321 p = "!$OMP SECTIONS";
2322 break;
2323 case ST_OMP_SECTION:
2324 p = "!$OMP SECTION";
2325 break;
2326 case ST_OMP_SIMD:
2327 p = "!$OMP SIMD";
2328 break;
2329 case ST_OMP_SINGLE:
2330 p = "!$OMP SINGLE";
2331 break;
2332 case ST_OMP_TARGET:
2333 p = "!$OMP TARGET";
2334 break;
2335 case ST_OMP_TARGET_DATA:
2336 p = "!$OMP TARGET DATA";
2337 break;
2338 case ST_OMP_TARGET_ENTER_DATA:
2339 p = "!$OMP TARGET ENTER DATA";
2340 break;
2341 case ST_OMP_TARGET_EXIT_DATA:
2342 p = "!$OMP TARGET EXIT DATA";
2343 break;
2344 case ST_OMP_TARGET_PARALLEL:
2345 p = "!$OMP TARGET PARALLEL";
2346 break;
2347 case ST_OMP_TARGET_PARALLEL_DO:
2348 p = "!$OMP TARGET PARALLEL DO";
2349 break;
2350 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
2351 p = "!$OMP TARGET PARALLEL DO SIMD";
2352 break;
2353 case ST_OMP_TARGET_SIMD:
2354 p = "!$OMP TARGET SIMD";
2355 break;
2356 case ST_OMP_TARGET_TEAMS:
2357 p = "!$OMP TARGET TEAMS";
2358 break;
2359 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
2360 p = "!$OMP TARGET TEAMS DISTRIBUTE";
2361 break;
2362 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2363 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2364 break;
2365 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2366 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2367 break;
2368 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2369 p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2370 break;
2371 case ST_OMP_TARGET_UPDATE:
2372 p = "!$OMP TARGET UPDATE";
2373 break;
2374 case ST_OMP_TASK:
2375 p = "!$OMP TASK";
2376 break;
2377 case ST_OMP_TASKGROUP:
2378 p = "!$OMP TASKGROUP";
2379 break;
2380 case ST_OMP_TASKLOOP:
2381 p = "!$OMP TASKLOOP";
2382 break;
2383 case ST_OMP_TASKLOOP_SIMD:
2384 p = "!$OMP TASKLOOP SIMD";
2385 break;
2386 case ST_OMP_TASKWAIT:
2387 p = "!$OMP TASKWAIT";
2388 break;
2389 case ST_OMP_TASKYIELD:
2390 p = "!$OMP TASKYIELD";
2391 break;
2392 case ST_OMP_TEAMS:
2393 p = "!$OMP TEAMS";
2394 break;
2395 case ST_OMP_TEAMS_DISTRIBUTE:
2396 p = "!$OMP TEAMS DISTRIBUTE";
2397 break;
2398 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2399 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2400 break;
2401 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2402 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2403 break;
2404 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
2405 p = "!$OMP TEAMS DISTRIBUTE SIMD";
2406 break;
2407 case ST_OMP_THREADPRIVATE:
2408 p = "!$OMP THREADPRIVATE";
2409 break;
2410 case ST_OMP_WORKSHARE:
2411 p = "!$OMP WORKSHARE";
2412 break;
2413 default:
2414 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2417 return p;
2421 /* Create a symbol for the main program and assign it to ns->proc_name. */
2423 static void
2424 main_program_symbol (gfc_namespace *ns, const char *name)
2426 gfc_symbol *main_program;
2427 symbol_attribute attr;
2429 gfc_get_symbol (name, ns, &main_program);
2430 gfc_clear_attr (&attr);
2431 attr.flavor = FL_PROGRAM;
2432 attr.proc = PROC_UNKNOWN;
2433 attr.subroutine = 1;
2434 attr.access = ACCESS_PUBLIC;
2435 attr.is_main_program = 1;
2436 main_program->attr = attr;
2437 main_program->declared_at = gfc_current_locus;
2438 ns->proc_name = main_program;
2439 gfc_commit_symbols ();
2443 /* Do whatever is necessary to accept the last statement. */
2445 static void
2446 accept_statement (gfc_statement st)
2448 switch (st)
2450 case ST_IMPLICIT_NONE:
2451 case ST_IMPLICIT:
2452 break;
2454 case ST_FUNCTION:
2455 case ST_SUBROUTINE:
2456 case ST_MODULE:
2457 case ST_SUBMODULE:
2458 gfc_current_ns->proc_name = gfc_new_block;
2459 break;
2461 /* If the statement is the end of a block, lay down a special code
2462 that allows a branch to the end of the block from within the
2463 construct. IF and SELECT are treated differently from DO
2464 (where EXEC_NOP is added inside the loop) for two
2465 reasons:
2466 1. END DO has a meaning in the sense that after a GOTO to
2467 it, the loop counter must be increased.
2468 2. IF blocks and SELECT blocks can consist of multiple
2469 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2470 Putting the label before the END IF would make the jump
2471 from, say, the ELSE IF block to the END IF illegal. */
2473 case ST_ENDIF:
2474 case ST_END_SELECT:
2475 case ST_END_CRITICAL:
2476 if (gfc_statement_label != NULL)
2478 new_st.op = EXEC_END_NESTED_BLOCK;
2479 add_statement ();
2481 break;
2483 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2484 one parallel block. Thus, we add the special code to the nested block
2485 itself, instead of the parent one. */
2486 case ST_END_BLOCK:
2487 case ST_END_ASSOCIATE:
2488 if (gfc_statement_label != NULL)
2490 new_st.op = EXEC_END_BLOCK;
2491 add_statement ();
2493 break;
2495 /* The end-of-program unit statements do not get the special
2496 marker and require a statement of some sort if they are a
2497 branch target. */
2499 case ST_END_PROGRAM:
2500 case ST_END_FUNCTION:
2501 case ST_END_SUBROUTINE:
2502 if (gfc_statement_label != NULL)
2504 new_st.op = EXEC_RETURN;
2505 add_statement ();
2507 else
2509 new_st.op = EXEC_END_PROCEDURE;
2510 add_statement ();
2513 break;
2515 case ST_ENTRY:
2516 case_executable:
2517 case_exec_markers:
2518 add_statement ();
2519 break;
2521 default:
2522 break;
2525 gfc_commit_symbols ();
2526 gfc_warning_check ();
2527 gfc_clear_new_st ();
2531 /* Undo anything tentative that has been built for the current statement,
2532 except if a gfc_charlen structure has been added to current namespace's
2533 list of gfc_charlen structure. */
2535 static void
2536 reject_statement (void)
2538 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
2539 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
2541 gfc_reject_data (gfc_current_ns);
2543 gfc_new_block = NULL;
2544 gfc_undo_symbols ();
2545 gfc_clear_warning ();
2546 undo_new_statement ();
2550 /* Generic complaint about an out of order statement. We also do
2551 whatever is necessary to clean up. */
2553 static void
2554 unexpected_statement (gfc_statement st)
2556 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
2558 reject_statement ();
2562 /* Given the next statement seen by the matcher, make sure that it is
2563 in proper order with the last. This subroutine is initialized by
2564 calling it with an argument of ST_NONE. If there is a problem, we
2565 issue an error and return false. Otherwise we return true.
2567 Individual parsers need to verify that the statements seen are
2568 valid before calling here, i.e., ENTRY statements are not allowed in
2569 INTERFACE blocks. The following diagram is taken from the standard:
2571 +---------------------------------------+
2572 | program subroutine function module |
2573 +---------------------------------------+
2574 | use |
2575 +---------------------------------------+
2576 | import |
2577 +---------------------------------------+
2578 | | implicit none |
2579 | +-----------+------------------+
2580 | | parameter | implicit |
2581 | +-----------+------------------+
2582 | format | | derived type |
2583 | entry | parameter | interface |
2584 | | data | specification |
2585 | | | statement func |
2586 | +-----------+------------------+
2587 | | data | executable |
2588 +--------+-----------+------------------+
2589 | contains |
2590 +---------------------------------------+
2591 | internal module/subprogram |
2592 +---------------------------------------+
2593 | end |
2594 +---------------------------------------+
2598 enum state_order
2600 ORDER_START,
2601 ORDER_USE,
2602 ORDER_IMPORT,
2603 ORDER_IMPLICIT_NONE,
2604 ORDER_IMPLICIT,
2605 ORDER_SPEC,
2606 ORDER_EXEC
2609 typedef struct
2611 enum state_order state;
2612 gfc_statement last_statement;
2613 locus where;
2615 st_state;
2617 static bool
2618 verify_st_order (st_state *p, gfc_statement st, bool silent)
2621 switch (st)
2623 case ST_NONE:
2624 p->state = ORDER_START;
2625 break;
2627 case ST_USE:
2628 if (p->state > ORDER_USE)
2629 goto order;
2630 p->state = ORDER_USE;
2631 break;
2633 case ST_IMPORT:
2634 if (p->state > ORDER_IMPORT)
2635 goto order;
2636 p->state = ORDER_IMPORT;
2637 break;
2639 case ST_IMPLICIT_NONE:
2640 if (p->state > ORDER_IMPLICIT)
2641 goto order;
2643 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2644 statement disqualifies a USE but not an IMPLICIT NONE.
2645 Duplicate IMPLICIT NONEs are caught when the implicit types
2646 are set. */
2648 p->state = ORDER_IMPLICIT_NONE;
2649 break;
2651 case ST_IMPLICIT:
2652 if (p->state > ORDER_IMPLICIT)
2653 goto order;
2654 p->state = ORDER_IMPLICIT;
2655 break;
2657 case ST_FORMAT:
2658 case ST_ENTRY:
2659 if (p->state < ORDER_IMPLICIT_NONE)
2660 p->state = ORDER_IMPLICIT_NONE;
2661 break;
2663 case ST_PARAMETER:
2664 if (p->state >= ORDER_EXEC)
2665 goto order;
2666 if (p->state < ORDER_IMPLICIT)
2667 p->state = ORDER_IMPLICIT;
2668 break;
2670 case ST_DATA:
2671 if (p->state < ORDER_SPEC)
2672 p->state = ORDER_SPEC;
2673 break;
2675 case ST_PUBLIC:
2676 case ST_PRIVATE:
2677 case ST_STRUCTURE_DECL:
2678 case ST_DERIVED_DECL:
2679 case_decl:
2680 if (p->state >= ORDER_EXEC)
2681 goto order;
2682 if (p->state < ORDER_SPEC)
2683 p->state = ORDER_SPEC;
2684 break;
2686 case_omp_decl:
2687 /* The OpenMP directives have to be somewhere in the specification
2688 part, but there are no further requirements on their ordering.
2689 Thus don't adjust p->state, just ignore them. */
2690 if (p->state >= ORDER_EXEC)
2691 goto order;
2692 break;
2694 case_executable:
2695 case_exec_markers:
2696 if (p->state < ORDER_EXEC)
2697 p->state = ORDER_EXEC;
2698 break;
2700 default:
2701 return false;
2704 /* All is well, record the statement in case we need it next time. */
2705 p->where = gfc_current_locus;
2706 p->last_statement = st;
2707 return true;
2709 order:
2710 if (!silent)
2711 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2712 gfc_ascii_statement (st),
2713 gfc_ascii_statement (p->last_statement), &p->where);
2715 return false;
2719 /* Handle an unexpected end of file. This is a show-stopper... */
2721 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
2723 static void
2724 unexpected_eof (void)
2726 gfc_state_data *p;
2728 gfc_error ("Unexpected end of file in %qs", gfc_source_file);
2730 /* Memory cleanup. Move to "second to last". */
2731 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
2732 p = p->previous);
2734 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
2735 gfc_done_2 ();
2737 longjmp (eof_buf, 1);
2741 /* Parse the CONTAINS section of a derived type definition. */
2743 gfc_access gfc_typebound_default_access;
2745 static bool
2746 parse_derived_contains (void)
2748 gfc_state_data s;
2749 bool seen_private = false;
2750 bool seen_comps = false;
2751 bool error_flag = false;
2752 bool to_finish;
2754 gcc_assert (gfc_current_state () == COMP_DERIVED);
2755 gcc_assert (gfc_current_block ());
2757 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2758 section. */
2759 if (gfc_current_block ()->attr.sequence)
2760 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2761 " section at %C", gfc_current_block ()->name);
2762 if (gfc_current_block ()->attr.is_bind_c)
2763 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2764 " section at %C", gfc_current_block ()->name);
2766 accept_statement (ST_CONTAINS);
2767 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
2769 gfc_typebound_default_access = ACCESS_PUBLIC;
2771 to_finish = false;
2772 while (!to_finish)
2774 gfc_statement st;
2775 st = next_statement ();
2776 switch (st)
2778 case ST_NONE:
2779 unexpected_eof ();
2780 break;
2782 case ST_DATA_DECL:
2783 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2784 goto error;
2786 case ST_PROCEDURE:
2787 if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
2788 goto error;
2790 accept_statement (ST_PROCEDURE);
2791 seen_comps = true;
2792 break;
2794 case ST_GENERIC:
2795 if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
2796 goto error;
2798 accept_statement (ST_GENERIC);
2799 seen_comps = true;
2800 break;
2802 case ST_FINAL:
2803 if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
2804 " at %C"))
2805 goto error;
2807 accept_statement (ST_FINAL);
2808 seen_comps = true;
2809 break;
2811 case ST_END_TYPE:
2812 to_finish = true;
2814 if (!seen_comps
2815 && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
2816 "at %C with empty CONTAINS section")))
2817 goto error;
2819 /* ST_END_TYPE is accepted by parse_derived after return. */
2820 break;
2822 case ST_PRIVATE:
2823 if (!gfc_find_state (COMP_MODULE))
2825 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2826 "a MODULE");
2827 goto error;
2830 if (seen_comps)
2832 gfc_error ("PRIVATE statement at %C must precede procedure"
2833 " bindings");
2834 goto error;
2837 if (seen_private)
2839 gfc_error ("Duplicate PRIVATE statement at %C");
2840 goto error;
2843 accept_statement (ST_PRIVATE);
2844 gfc_typebound_default_access = ACCESS_PRIVATE;
2845 seen_private = true;
2846 break;
2848 case ST_SEQUENCE:
2849 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2850 goto error;
2852 case ST_CONTAINS:
2853 gfc_error ("Already inside a CONTAINS block at %C");
2854 goto error;
2856 default:
2857 unexpected_statement (st);
2858 break;
2861 continue;
2863 error:
2864 error_flag = true;
2865 reject_statement ();
2868 pop_state ();
2869 gcc_assert (gfc_current_state () == COMP_DERIVED);
2871 return error_flag;
2875 /* Set attributes for the parent symbol based on the attributes of a component
2876 and raise errors if conflicting attributes are found for the component. */
2878 static void
2879 check_component (gfc_symbol *sym, gfc_component *c, gfc_component **lockp,
2880 gfc_component **eventp)
2882 bool coarray, lock_type, event_type, allocatable, pointer;
2883 coarray = lock_type = event_type = allocatable = pointer = false;
2884 gfc_component *lock_comp = NULL, *event_comp = NULL;
2886 if (lockp) lock_comp = *lockp;
2887 if (eventp) event_comp = *eventp;
2889 /* Look for allocatable components. */
2890 if (c->attr.allocatable
2891 || (c->ts.type == BT_CLASS && c->attr.class_ok
2892 && CLASS_DATA (c)->attr.allocatable)
2893 || (c->ts.type == BT_DERIVED && !c->attr.pointer
2894 && c->ts.u.derived->attr.alloc_comp))
2896 allocatable = true;
2897 sym->attr.alloc_comp = 1;
2900 /* Look for pointer components. */
2901 if (c->attr.pointer
2902 || (c->ts.type == BT_CLASS && c->attr.class_ok
2903 && CLASS_DATA (c)->attr.class_pointer)
2904 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2906 pointer = true;
2907 sym->attr.pointer_comp = 1;
2910 /* Look for procedure pointer components. */
2911 if (c->attr.proc_pointer
2912 || (c->ts.type == BT_DERIVED
2913 && c->ts.u.derived->attr.proc_pointer_comp))
2914 sym->attr.proc_pointer_comp = 1;
2916 /* Looking for coarray components. */
2917 if (c->attr.codimension
2918 || (c->ts.type == BT_CLASS && c->attr.class_ok
2919 && CLASS_DATA (c)->attr.codimension))
2921 coarray = true;
2922 sym->attr.coarray_comp = 1;
2925 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
2926 && !c->attr.pointer)
2928 coarray = true;
2929 sym->attr.coarray_comp = 1;
2932 /* Looking for lock_type components. */
2933 if ((c->ts.type == BT_DERIVED
2934 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2935 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2936 || (c->ts.type == BT_CLASS && c->attr.class_ok
2937 && CLASS_DATA (c)->ts.u.derived->from_intmod
2938 == INTMOD_ISO_FORTRAN_ENV
2939 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2940 == ISOFORTRAN_LOCK_TYPE)
2941 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
2942 && !allocatable && !pointer))
2944 lock_type = 1;
2945 lock_comp = c;
2946 sym->attr.lock_comp = 1;
2949 /* Looking for event_type components. */
2950 if ((c->ts.type == BT_DERIVED
2951 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2952 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2953 || (c->ts.type == BT_CLASS && c->attr.class_ok
2954 && CLASS_DATA (c)->ts.u.derived->from_intmod
2955 == INTMOD_ISO_FORTRAN_ENV
2956 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2957 == ISOFORTRAN_EVENT_TYPE)
2958 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.event_comp
2959 && !allocatable && !pointer))
2961 event_type = 1;
2962 event_comp = c;
2963 sym->attr.event_comp = 1;
2966 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2967 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2968 unless there are nondirect [allocatable or pointer] components
2969 involved (cf. 1.3.33.1 and 1.3.33.3). */
2971 if (pointer && !coarray && lock_type)
2972 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2973 "codimension or be a subcomponent of a coarray, "
2974 "which is not possible as the component has the "
2975 "pointer attribute", c->name, &c->loc);
2976 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2977 && c->ts.u.derived->attr.lock_comp)
2978 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2979 "of type LOCK_TYPE, which must have a codimension or be a "
2980 "subcomponent of a coarray", c->name, &c->loc);
2982 if (lock_type && allocatable && !coarray)
2983 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2984 "a codimension", c->name, &c->loc);
2985 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
2986 && c->ts.u.derived->attr.lock_comp)
2987 gfc_error ("Allocatable component %s at %L must have a codimension as "
2988 "it has a noncoarray subcomponent of type LOCK_TYPE",
2989 c->name, &c->loc);
2991 if (sym->attr.coarray_comp && !coarray && lock_type)
2992 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2993 "subcomponent of type LOCK_TYPE must have a codimension or "
2994 "be a subcomponent of a coarray. (Variables of type %s may "
2995 "not have a codimension as already a coarray "
2996 "subcomponent exists)", c->name, &c->loc, sym->name);
2998 if (sym->attr.lock_comp && coarray && !lock_type)
2999 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
3000 "subcomponent of type LOCK_TYPE must have a codimension or "
3001 "be a subcomponent of a coarray. (Variables of type %s may "
3002 "not have a codimension as %s at %L has a codimension or a "
3003 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
3004 sym->name, c->name, &c->loc);
3006 /* Similarly for EVENT TYPE. */
3008 if (pointer && !coarray && event_type)
3009 gfc_error ("Component %s at %L of type EVENT_TYPE must have a "
3010 "codimension or be a subcomponent of a coarray, "
3011 "which is not possible as the component has the "
3012 "pointer attribute", c->name, &c->loc);
3013 else if (pointer && !coarray && c->ts.type == BT_DERIVED
3014 && c->ts.u.derived->attr.event_comp)
3015 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
3016 "of type EVENT_TYPE, which must have a codimension or be a "
3017 "subcomponent of a coarray", c->name, &c->loc);
3019 if (event_type && allocatable && !coarray)
3020 gfc_error ("Allocatable component %s at %L of type EVENT_TYPE must have "
3021 "a codimension", c->name, &c->loc);
3022 else if (event_type && allocatable && c->ts.type == BT_DERIVED
3023 && c->ts.u.derived->attr.event_comp)
3024 gfc_error ("Allocatable component %s at %L must have a codimension as "
3025 "it has a noncoarray subcomponent of type EVENT_TYPE",
3026 c->name, &c->loc);
3028 if (sym->attr.coarray_comp && !coarray && event_type)
3029 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
3030 "subcomponent of type EVENT_TYPE must have a codimension or "
3031 "be a subcomponent of a coarray. (Variables of type %s may "
3032 "not have a codimension as already a coarray "
3033 "subcomponent exists)", c->name, &c->loc, sym->name);
3035 if (sym->attr.event_comp && coarray && !event_type)
3036 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
3037 "subcomponent of type EVENT_TYPE must have a codimension or "
3038 "be a subcomponent of a coarray. (Variables of type %s may "
3039 "not have a codimension as %s at %L has a codimension or a "
3040 "coarray subcomponent)", event_comp->name, &event_comp->loc,
3041 sym->name, c->name, &c->loc);
3043 /* Look for private components. */
3044 if (sym->component_access == ACCESS_PRIVATE
3045 || c->attr.access == ACCESS_PRIVATE
3046 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
3047 sym->attr.private_comp = 1;
3049 if (lockp) *lockp = lock_comp;
3050 if (eventp) *eventp = event_comp;
3054 static void parse_struct_map (gfc_statement);
3056 /* Parse a union component definition within a structure definition. */
3058 static void
3059 parse_union (void)
3061 int compiling;
3062 gfc_statement st;
3063 gfc_state_data s;
3064 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3065 gfc_symbol *un;
3067 accept_statement(ST_UNION);
3068 push_state (&s, COMP_UNION, gfc_new_block);
3069 un = gfc_new_block;
3071 compiling = 1;
3073 while (compiling)
3075 st = next_statement ();
3076 /* Only MAP declarations valid within a union. */
3077 switch (st)
3079 case ST_NONE:
3080 unexpected_eof ();
3082 case ST_MAP:
3083 accept_statement (ST_MAP);
3084 parse_struct_map (ST_MAP);
3085 /* Add a component to the union for each map. */
3086 if (!gfc_add_component (un, gfc_new_block->name, &c))
3088 gfc_internal_error ("failed to create map component '%s'",
3089 gfc_new_block->name);
3090 reject_statement ();
3091 return;
3093 c->ts.type = BT_DERIVED;
3094 c->ts.u.derived = gfc_new_block;
3095 /* Normally components get their initialization expressions when they
3096 are created in decl.c (build_struct) so we can look through the
3097 flat component list for initializers during resolution. Unions and
3098 maps create components along with their type definitions so we
3099 have to generate initializers here. */
3100 c->initializer = gfc_default_initializer (&c->ts);
3101 break;
3103 case ST_END_UNION:
3104 compiling = 0;
3105 accept_statement (ST_END_UNION);
3106 break;
3108 default:
3109 unexpected_statement (st);
3110 break;
3114 for (c = un->components; c; c = c->next)
3115 check_component (un, c, &lock_comp, &event_comp);
3117 /* Add the union as a component in its parent structure. */
3118 pop_state ();
3119 if (!gfc_add_component (gfc_current_block (), un->name, &c))
3121 gfc_internal_error ("failed to create union component '%s'", un->name);
3122 reject_statement ();
3123 return;
3125 c->ts.type = BT_UNION;
3126 c->ts.u.derived = un;
3127 c->initializer = gfc_default_initializer (&c->ts);
3129 un->attr.zero_comp = un->components == NULL;
3133 /* Parse a STRUCTURE or MAP. */
3135 static void
3136 parse_struct_map (gfc_statement block)
3138 int compiling_type;
3139 gfc_statement st;
3140 gfc_state_data s;
3141 gfc_symbol *sym;
3142 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3143 gfc_compile_state comp;
3144 gfc_statement ends;
3146 if (block == ST_STRUCTURE_DECL)
3148 comp = COMP_STRUCTURE;
3149 ends = ST_END_STRUCTURE;
3151 else
3153 gcc_assert (block == ST_MAP);
3154 comp = COMP_MAP;
3155 ends = ST_END_MAP;
3158 accept_statement(block);
3159 push_state (&s, comp, gfc_new_block);
3161 gfc_new_block->component_access = ACCESS_PUBLIC;
3162 compiling_type = 1;
3164 while (compiling_type)
3166 st = next_statement ();
3167 switch (st)
3169 case ST_NONE:
3170 unexpected_eof ();
3172 /* Nested structure declarations will be captured as ST_DATA_DECL. */
3173 case ST_STRUCTURE_DECL:
3174 /* Let a more specific error make it to decode_statement(). */
3175 if (gfc_error_check () == 0)
3176 gfc_error ("Syntax error in nested structure declaration at %C");
3177 reject_statement ();
3178 /* Skip the rest of this statement. */
3179 gfc_error_recovery ();
3180 break;
3182 case ST_UNION:
3183 accept_statement (ST_UNION);
3184 parse_union ();
3185 break;
3187 case ST_DATA_DECL:
3188 /* The data declaration was a nested/ad-hoc STRUCTURE field. */
3189 accept_statement (ST_DATA_DECL);
3190 if (gfc_new_block && gfc_new_block != gfc_current_block ()
3191 && gfc_new_block->attr.flavor == FL_STRUCT)
3192 parse_struct_map (ST_STRUCTURE_DECL);
3193 break;
3195 case ST_END_STRUCTURE:
3196 case ST_END_MAP:
3197 if (st == ends)
3199 accept_statement (st);
3200 compiling_type = 0;
3202 else
3203 unexpected_statement (st);
3204 break;
3206 default:
3207 unexpected_statement (st);
3208 break;
3212 /* Validate each component. */
3213 sym = gfc_current_block ();
3214 for (c = sym->components; c; c = c->next)
3215 check_component (sym, c, &lock_comp, &event_comp);
3217 sym->attr.zero_comp = (sym->components == NULL);
3219 /* Allow parse_union to find this structure to add to its list of maps. */
3220 if (block == ST_MAP)
3221 gfc_new_block = gfc_current_block ();
3223 pop_state ();
3227 /* Parse a derived type. */
3229 static void
3230 parse_derived (void)
3232 int compiling_type, seen_private, seen_sequence, seen_component;
3233 gfc_statement st;
3234 gfc_state_data s;
3235 gfc_symbol *sym;
3236 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3238 accept_statement (ST_DERIVED_DECL);
3239 push_state (&s, COMP_DERIVED, gfc_new_block);
3241 gfc_new_block->component_access = ACCESS_PUBLIC;
3242 seen_private = 0;
3243 seen_sequence = 0;
3244 seen_component = 0;
3246 compiling_type = 1;
3248 while (compiling_type)
3250 st = next_statement ();
3251 switch (st)
3253 case ST_NONE:
3254 unexpected_eof ();
3256 case ST_DATA_DECL:
3257 case ST_PROCEDURE:
3258 accept_statement (st);
3259 seen_component = 1;
3260 break;
3262 case ST_FINAL:
3263 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
3264 break;
3266 case ST_END_TYPE:
3267 endType:
3268 compiling_type = 0;
3270 if (!seen_component)
3271 gfc_notify_std (GFC_STD_F2003, "Derived type "
3272 "definition at %C without components");
3274 accept_statement (ST_END_TYPE);
3275 break;
3277 case ST_PRIVATE:
3278 if (!gfc_find_state (COMP_MODULE))
3280 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
3281 "a MODULE");
3282 break;
3285 if (seen_component)
3287 gfc_error ("PRIVATE statement at %C must precede "
3288 "structure components");
3289 break;
3292 if (seen_private)
3293 gfc_error ("Duplicate PRIVATE statement at %C");
3295 s.sym->component_access = ACCESS_PRIVATE;
3297 accept_statement (ST_PRIVATE);
3298 seen_private = 1;
3299 break;
3301 case ST_SEQUENCE:
3302 if (seen_component)
3304 gfc_error ("SEQUENCE statement at %C must precede "
3305 "structure components");
3306 break;
3309 if (gfc_current_block ()->attr.sequence)
3310 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
3311 "TYPE statement");
3313 if (seen_sequence)
3315 gfc_error ("Duplicate SEQUENCE statement at %C");
3318 seen_sequence = 1;
3319 gfc_add_sequence (&gfc_current_block ()->attr,
3320 gfc_current_block ()->name, NULL);
3321 break;
3323 case ST_CONTAINS:
3324 gfc_notify_std (GFC_STD_F2003,
3325 "CONTAINS block in derived type"
3326 " definition at %C");
3328 accept_statement (ST_CONTAINS);
3329 parse_derived_contains ();
3330 goto endType;
3332 default:
3333 unexpected_statement (st);
3334 break;
3338 /* need to verify that all fields of the derived type are
3339 * interoperable with C if the type is declared to be bind(c)
3341 sym = gfc_current_block ();
3342 for (c = sym->components; c; c = c->next)
3343 check_component (sym, c, &lock_comp, &event_comp);
3345 if (!seen_component)
3346 sym->attr.zero_comp = 1;
3348 pop_state ();
3352 /* Parse an ENUM. */
3354 static void
3355 parse_enum (void)
3357 gfc_statement st;
3358 int compiling_enum;
3359 gfc_state_data s;
3360 int seen_enumerator = 0;
3362 push_state (&s, COMP_ENUM, gfc_new_block);
3364 compiling_enum = 1;
3366 while (compiling_enum)
3368 st = next_statement ();
3369 switch (st)
3371 case ST_NONE:
3372 unexpected_eof ();
3373 break;
3375 case ST_ENUMERATOR:
3376 seen_enumerator = 1;
3377 accept_statement (st);
3378 break;
3380 case ST_END_ENUM:
3381 compiling_enum = 0;
3382 if (!seen_enumerator)
3383 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
3384 accept_statement (st);
3385 break;
3387 default:
3388 gfc_free_enum_history ();
3389 unexpected_statement (st);
3390 break;
3393 pop_state ();
3397 /* Parse an interface. We must be able to deal with the possibility
3398 of recursive interfaces. The parse_spec() subroutine is mutually
3399 recursive with parse_interface(). */
3401 static gfc_statement parse_spec (gfc_statement);
3403 static void
3404 parse_interface (void)
3406 gfc_compile_state new_state = COMP_NONE, current_state;
3407 gfc_symbol *prog_unit, *sym;
3408 gfc_interface_info save;
3409 gfc_state_data s1, s2;
3410 gfc_statement st;
3412 accept_statement (ST_INTERFACE);
3414 current_interface.ns = gfc_current_ns;
3415 save = current_interface;
3417 sym = (current_interface.type == INTERFACE_GENERIC
3418 || current_interface.type == INTERFACE_USER_OP)
3419 ? gfc_new_block : NULL;
3421 push_state (&s1, COMP_INTERFACE, sym);
3422 current_state = COMP_NONE;
3424 loop:
3425 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
3427 st = next_statement ();
3428 switch (st)
3430 case ST_NONE:
3431 unexpected_eof ();
3433 case ST_SUBROUTINE:
3434 case ST_FUNCTION:
3435 if (st == ST_SUBROUTINE)
3436 new_state = COMP_SUBROUTINE;
3437 else if (st == ST_FUNCTION)
3438 new_state = COMP_FUNCTION;
3439 if (gfc_new_block->attr.pointer)
3441 gfc_new_block->attr.pointer = 0;
3442 gfc_new_block->attr.proc_pointer = 1;
3444 if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
3445 gfc_new_block->formal, NULL))
3447 reject_statement ();
3448 gfc_free_namespace (gfc_current_ns);
3449 goto loop;
3451 /* F2008 C1210 forbids the IMPORT statement in module procedure
3452 interface bodies and the flag is set to import symbols. */
3453 if (gfc_new_block->attr.module_procedure)
3454 gfc_current_ns->has_import_set = 1;
3455 break;
3457 case ST_PROCEDURE:
3458 case ST_MODULE_PROC: /* The module procedure matcher makes
3459 sure the context is correct. */
3460 accept_statement (st);
3461 gfc_free_namespace (gfc_current_ns);
3462 goto loop;
3464 case ST_END_INTERFACE:
3465 gfc_free_namespace (gfc_current_ns);
3466 gfc_current_ns = current_interface.ns;
3467 goto done;
3469 default:
3470 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
3471 gfc_ascii_statement (st));
3472 reject_statement ();
3473 gfc_free_namespace (gfc_current_ns);
3474 goto loop;
3478 /* Make sure that the generic name has the right attribute. */
3479 if (current_interface.type == INTERFACE_GENERIC
3480 && current_state == COMP_NONE)
3482 if (new_state == COMP_FUNCTION && sym)
3483 gfc_add_function (&sym->attr, sym->name, NULL);
3484 else if (new_state == COMP_SUBROUTINE && sym)
3485 gfc_add_subroutine (&sym->attr, sym->name, NULL);
3487 current_state = new_state;
3490 if (current_interface.type == INTERFACE_ABSTRACT)
3492 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
3493 if (gfc_is_intrinsic_typename (gfc_new_block->name))
3494 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
3495 "cannot be the same as an intrinsic type",
3496 gfc_new_block->name);
3499 push_state (&s2, new_state, gfc_new_block);
3500 accept_statement (st);
3501 prog_unit = gfc_new_block;
3502 prog_unit->formal_ns = gfc_current_ns;
3503 if (prog_unit == prog_unit->formal_ns->proc_name
3504 && prog_unit->ns != prog_unit->formal_ns)
3505 prog_unit->refs++;
3507 decl:
3508 /* Read data declaration statements. */
3509 st = parse_spec (ST_NONE);
3510 in_specification_block = true;
3512 /* Since the interface block does not permit an IMPLICIT statement,
3513 the default type for the function or the result must be taken
3514 from the formal namespace. */
3515 if (new_state == COMP_FUNCTION)
3517 if (prog_unit->result == prog_unit
3518 && prog_unit->ts.type == BT_UNKNOWN)
3519 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
3520 else if (prog_unit->result != prog_unit
3521 && prog_unit->result->ts.type == BT_UNKNOWN)
3522 gfc_set_default_type (prog_unit->result, 1,
3523 prog_unit->formal_ns);
3526 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
3528 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3529 gfc_ascii_statement (st));
3530 reject_statement ();
3531 goto decl;
3534 /* Add EXTERNAL attribute to function or subroutine. */
3535 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
3536 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
3538 current_interface = save;
3539 gfc_add_interface (prog_unit);
3540 pop_state ();
3542 if (current_interface.ns
3543 && current_interface.ns->proc_name
3544 && strcmp (current_interface.ns->proc_name->name,
3545 prog_unit->name) == 0)
3546 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3547 "enclosing procedure", prog_unit->name,
3548 &current_interface.ns->proc_name->declared_at);
3550 goto loop;
3552 done:
3553 pop_state ();
3557 /* Associate function characteristics by going back to the function
3558 declaration and rematching the prefix. */
3560 static match
3561 match_deferred_characteristics (gfc_typespec * ts)
3563 locus loc;
3564 match m = MATCH_ERROR;
3565 char name[GFC_MAX_SYMBOL_LEN + 1];
3567 loc = gfc_current_locus;
3569 gfc_current_locus = gfc_current_block ()->declared_at;
3571 gfc_clear_error ();
3572 gfc_buffer_error (true);
3573 m = gfc_match_prefix (ts);
3574 gfc_buffer_error (false);
3576 if (ts->type == BT_DERIVED)
3578 ts->kind = 0;
3580 if (!ts->u.derived)
3581 m = MATCH_ERROR;
3584 /* Only permit one go at the characteristic association. */
3585 if (ts->kind == -1)
3586 ts->kind = 0;
3588 /* Set the function locus correctly. If we have not found the
3589 function name, there is an error. */
3590 if (m == MATCH_YES
3591 && gfc_match ("function% %n", name) == MATCH_YES
3592 && strcmp (name, gfc_current_block ()->name) == 0)
3594 gfc_current_block ()->declared_at = gfc_current_locus;
3595 gfc_commit_symbols ();
3597 else
3599 gfc_error_check ();
3600 gfc_undo_symbols ();
3603 gfc_current_locus =loc;
3604 return m;
3608 /* Check specification-expressions in the function result of the currently
3609 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3610 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3611 scope are not yet parsed so this has to be delayed up to parse_spec. */
3613 static void
3614 check_function_result_typed (void)
3616 gfc_typespec ts;
3618 gcc_assert (gfc_current_state () == COMP_FUNCTION);
3620 if (!gfc_current_ns->proc_name->result) return;
3622 ts = gfc_current_ns->proc_name->result->ts;
3624 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3625 /* TODO: Extend when KIND type parameters are implemented. */
3626 if (ts.type == BT_CHARACTER && ts.u.cl && ts.u.cl->length)
3627 gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true);
3631 /* Parse a set of specification statements. Returns the statement
3632 that doesn't fit. */
3634 static gfc_statement
3635 parse_spec (gfc_statement st)
3637 st_state ss;
3638 bool function_result_typed = false;
3639 bool bad_characteristic = false;
3640 gfc_typespec *ts;
3642 in_specification_block = true;
3644 verify_st_order (&ss, ST_NONE, false);
3645 if (st == ST_NONE)
3646 st = next_statement ();
3648 /* If we are not inside a function or don't have a result specified so far,
3649 do nothing special about it. */
3650 if (gfc_current_state () != COMP_FUNCTION)
3651 function_result_typed = true;
3652 else
3654 gfc_symbol* proc = gfc_current_ns->proc_name;
3655 gcc_assert (proc);
3657 if (proc->result->ts.type == BT_UNKNOWN)
3658 function_result_typed = true;
3661 loop:
3663 /* If we're inside a BLOCK construct, some statements are disallowed.
3664 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3665 or VALUE are also disallowed, but they don't have a particular ST_*
3666 key so we have to check for them individually in their matcher routine. */
3667 if (gfc_current_state () == COMP_BLOCK)
3668 switch (st)
3670 case ST_IMPLICIT:
3671 case ST_IMPLICIT_NONE:
3672 case ST_NAMELIST:
3673 case ST_COMMON:
3674 case ST_EQUIVALENCE:
3675 case ST_STATEMENT_FUNCTION:
3676 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3677 gfc_ascii_statement (st));
3678 reject_statement ();
3679 break;
3681 default:
3682 break;
3684 else if (gfc_current_state () == COMP_BLOCK_DATA)
3685 /* Fortran 2008, C1116. */
3686 switch (st)
3688 case ST_ATTR_DECL:
3689 case ST_COMMON:
3690 case ST_DATA:
3691 case ST_DATA_DECL:
3692 case ST_DERIVED_DECL:
3693 case ST_END_BLOCK_DATA:
3694 case ST_EQUIVALENCE:
3695 case ST_IMPLICIT:
3696 case ST_IMPLICIT_NONE:
3697 case ST_PARAMETER:
3698 case ST_STRUCTURE_DECL:
3699 case ST_TYPE:
3700 case ST_USE:
3701 break;
3703 case ST_NONE:
3704 break;
3706 default:
3707 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3708 gfc_ascii_statement (st));
3709 reject_statement ();
3710 break;
3713 /* If we find a statement that can not be followed by an IMPLICIT statement
3714 (and thus we can expect to see none any further), type the function result
3715 if it has not yet been typed. Be careful not to give the END statement
3716 to verify_st_order! */
3717 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
3719 bool verify_now = false;
3721 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
3722 verify_now = true;
3723 else
3725 st_state dummyss;
3726 verify_st_order (&dummyss, ST_NONE, false);
3727 verify_st_order (&dummyss, st, false);
3729 if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
3730 verify_now = true;
3733 if (verify_now)
3735 check_function_result_typed ();
3736 function_result_typed = true;
3740 switch (st)
3742 case ST_NONE:
3743 unexpected_eof ();
3745 case ST_IMPLICIT_NONE:
3746 case ST_IMPLICIT:
3747 if (!function_result_typed)
3749 check_function_result_typed ();
3750 function_result_typed = true;
3752 goto declSt;
3754 case ST_FORMAT:
3755 case ST_ENTRY:
3756 case ST_DATA: /* Not allowed in interfaces */
3757 if (gfc_current_state () == COMP_INTERFACE)
3758 break;
3760 /* Fall through */
3762 case ST_USE:
3763 case ST_IMPORT:
3764 case ST_PARAMETER:
3765 case ST_PUBLIC:
3766 case ST_PRIVATE:
3767 case ST_STRUCTURE_DECL:
3768 case ST_DERIVED_DECL:
3769 case_decl:
3770 case_omp_decl:
3771 declSt:
3772 if (!verify_st_order (&ss, st, false))
3774 reject_statement ();
3775 st = next_statement ();
3776 goto loop;
3779 switch (st)
3781 case ST_INTERFACE:
3782 parse_interface ();
3783 break;
3785 case ST_STRUCTURE_DECL:
3786 parse_struct_map (ST_STRUCTURE_DECL);
3787 break;
3789 case ST_DERIVED_DECL:
3790 parse_derived ();
3791 break;
3793 case ST_PUBLIC:
3794 case ST_PRIVATE:
3795 if (gfc_current_state () != COMP_MODULE)
3797 gfc_error ("%s statement must appear in a MODULE",
3798 gfc_ascii_statement (st));
3799 reject_statement ();
3800 break;
3803 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
3805 gfc_error ("%s statement at %C follows another accessibility "
3806 "specification", gfc_ascii_statement (st));
3807 reject_statement ();
3808 break;
3811 gfc_current_ns->default_access = (st == ST_PUBLIC)
3812 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3814 break;
3816 case ST_STATEMENT_FUNCTION:
3817 if (gfc_current_state () == COMP_MODULE
3818 || gfc_current_state () == COMP_SUBMODULE)
3820 unexpected_statement (st);
3821 break;
3824 default:
3825 break;
3828 accept_statement (st);
3829 st = next_statement ();
3830 goto loop;
3832 case ST_ENUM:
3833 accept_statement (st);
3834 parse_enum();
3835 st = next_statement ();
3836 goto loop;
3838 case ST_GET_FCN_CHARACTERISTICS:
3839 /* This statement triggers the association of a function's result
3840 characteristics. */
3841 ts = &gfc_current_block ()->result->ts;
3842 if (match_deferred_characteristics (ts) != MATCH_YES)
3843 bad_characteristic = true;
3845 st = next_statement ();
3846 goto loop;
3848 default:
3849 break;
3852 /* If match_deferred_characteristics failed, then there is an error. */
3853 if (bad_characteristic)
3855 ts = &gfc_current_block ()->result->ts;
3856 if (ts->type != BT_DERIVED)
3857 gfc_error ("Bad kind expression for function %qs at %L",
3858 gfc_current_block ()->name,
3859 &gfc_current_block ()->declared_at);
3860 else
3861 gfc_error ("The type for function %qs at %L is not accessible",
3862 gfc_current_block ()->name,
3863 &gfc_current_block ()->declared_at);
3865 gfc_current_block ()->ts.kind = 0;
3866 /* Keep the derived type; if it's bad, it will be discovered later. */
3867 if (!(ts->type == BT_DERIVED && ts->u.derived))
3868 ts->type = BT_UNKNOWN;
3871 in_specification_block = false;
3873 return st;
3877 /* Parse a WHERE block, (not a simple WHERE statement). */
3879 static void
3880 parse_where_block (void)
3882 int seen_empty_else;
3883 gfc_code *top, *d;
3884 gfc_state_data s;
3885 gfc_statement st;
3887 accept_statement (ST_WHERE_BLOCK);
3888 top = gfc_state_stack->tail;
3890 push_state (&s, COMP_WHERE, gfc_new_block);
3892 d = add_statement ();
3893 d->expr1 = top->expr1;
3894 d->op = EXEC_WHERE;
3896 top->expr1 = NULL;
3897 top->block = d;
3899 seen_empty_else = 0;
3903 st = next_statement ();
3904 switch (st)
3906 case ST_NONE:
3907 unexpected_eof ();
3909 case ST_WHERE_BLOCK:
3910 parse_where_block ();
3911 break;
3913 case ST_ASSIGNMENT:
3914 case ST_WHERE:
3915 accept_statement (st);
3916 break;
3918 case ST_ELSEWHERE:
3919 if (seen_empty_else)
3921 gfc_error ("ELSEWHERE statement at %C follows previous "
3922 "unmasked ELSEWHERE");
3923 reject_statement ();
3924 break;
3927 if (new_st.expr1 == NULL)
3928 seen_empty_else = 1;
3930 d = new_level (gfc_state_stack->head);
3931 d->op = EXEC_WHERE;
3932 d->expr1 = new_st.expr1;
3934 accept_statement (st);
3936 break;
3938 case ST_END_WHERE:
3939 accept_statement (st);
3940 break;
3942 default:
3943 gfc_error ("Unexpected %s statement in WHERE block at %C",
3944 gfc_ascii_statement (st));
3945 reject_statement ();
3946 break;
3949 while (st != ST_END_WHERE);
3951 pop_state ();
3955 /* Parse a FORALL block (not a simple FORALL statement). */
3957 static void
3958 parse_forall_block (void)
3960 gfc_code *top, *d;
3961 gfc_state_data s;
3962 gfc_statement st;
3964 accept_statement (ST_FORALL_BLOCK);
3965 top = gfc_state_stack->tail;
3967 push_state (&s, COMP_FORALL, gfc_new_block);
3969 d = add_statement ();
3970 d->op = EXEC_FORALL;
3971 top->block = d;
3975 st = next_statement ();
3976 switch (st)
3979 case ST_ASSIGNMENT:
3980 case ST_POINTER_ASSIGNMENT:
3981 case ST_WHERE:
3982 case ST_FORALL:
3983 accept_statement (st);
3984 break;
3986 case ST_WHERE_BLOCK:
3987 parse_where_block ();
3988 break;
3990 case ST_FORALL_BLOCK:
3991 parse_forall_block ();
3992 break;
3994 case ST_END_FORALL:
3995 accept_statement (st);
3996 break;
3998 case ST_NONE:
3999 unexpected_eof ();
4001 default:
4002 gfc_error ("Unexpected %s statement in FORALL block at %C",
4003 gfc_ascii_statement (st));
4005 reject_statement ();
4006 break;
4009 while (st != ST_END_FORALL);
4011 pop_state ();
4015 static gfc_statement parse_executable (gfc_statement);
4017 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
4019 static void
4020 parse_if_block (void)
4022 gfc_code *top, *d;
4023 gfc_statement st;
4024 locus else_locus;
4025 gfc_state_data s;
4026 int seen_else;
4028 seen_else = 0;
4029 accept_statement (ST_IF_BLOCK);
4031 top = gfc_state_stack->tail;
4032 push_state (&s, COMP_IF, gfc_new_block);
4034 new_st.op = EXEC_IF;
4035 d = add_statement ();
4037 d->expr1 = top->expr1;
4038 top->expr1 = NULL;
4039 top->block = d;
4043 st = parse_executable (ST_NONE);
4045 switch (st)
4047 case ST_NONE:
4048 unexpected_eof ();
4050 case ST_ELSEIF:
4051 if (seen_else)
4053 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
4054 "statement at %L", &else_locus);
4056 reject_statement ();
4057 break;
4060 d = new_level (gfc_state_stack->head);
4061 d->op = EXEC_IF;
4062 d->expr1 = new_st.expr1;
4064 accept_statement (st);
4066 break;
4068 case ST_ELSE:
4069 if (seen_else)
4071 gfc_error ("Duplicate ELSE statements at %L and %C",
4072 &else_locus);
4073 reject_statement ();
4074 break;
4077 seen_else = 1;
4078 else_locus = gfc_current_locus;
4080 d = new_level (gfc_state_stack->head);
4081 d->op = EXEC_IF;
4083 accept_statement (st);
4085 break;
4087 case ST_ENDIF:
4088 break;
4090 default:
4091 unexpected_statement (st);
4092 break;
4095 while (st != ST_ENDIF);
4097 pop_state ();
4098 accept_statement (st);
4102 /* Parse a SELECT block. */
4104 static void
4105 parse_select_block (void)
4107 gfc_statement st;
4108 gfc_code *cp;
4109 gfc_state_data s;
4111 accept_statement (ST_SELECT_CASE);
4113 cp = gfc_state_stack->tail;
4114 push_state (&s, COMP_SELECT, gfc_new_block);
4116 /* Make sure that the next statement is a CASE or END SELECT. */
4117 for (;;)
4119 st = next_statement ();
4120 if (st == ST_NONE)
4121 unexpected_eof ();
4122 if (st == ST_END_SELECT)
4124 /* Empty SELECT CASE is OK. */
4125 accept_statement (st);
4126 pop_state ();
4127 return;
4129 if (st == ST_CASE)
4130 break;
4132 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
4133 "CASE at %C");
4135 reject_statement ();
4138 /* At this point, we're got a nonempty select block. */
4139 cp = new_level (cp);
4140 *cp = new_st;
4142 accept_statement (st);
4146 st = parse_executable (ST_NONE);
4147 switch (st)
4149 case ST_NONE:
4150 unexpected_eof ();
4152 case ST_CASE:
4153 cp = new_level (gfc_state_stack->head);
4154 *cp = new_st;
4155 gfc_clear_new_st ();
4157 accept_statement (st);
4158 /* Fall through */
4160 case ST_END_SELECT:
4161 break;
4163 /* Can't have an executable statement because of
4164 parse_executable(). */
4165 default:
4166 unexpected_statement (st);
4167 break;
4170 while (st != ST_END_SELECT);
4172 pop_state ();
4173 accept_statement (st);
4177 /* Pop the current selector from the SELECT TYPE stack. */
4179 static void
4180 select_type_pop (void)
4182 gfc_select_type_stack *old = select_type_stack;
4183 select_type_stack = old->prev;
4184 free (old);
4188 /* Parse a SELECT TYPE construct (F03:R821). */
4190 static void
4191 parse_select_type_block (void)
4193 gfc_statement st;
4194 gfc_code *cp;
4195 gfc_state_data s;
4197 gfc_current_ns = new_st.ext.block.ns;
4198 accept_statement (ST_SELECT_TYPE);
4200 cp = gfc_state_stack->tail;
4201 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
4203 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
4204 or END SELECT. */
4205 for (;;)
4207 st = next_statement ();
4208 if (st == ST_NONE)
4209 unexpected_eof ();
4210 if (st == ST_END_SELECT)
4211 /* Empty SELECT CASE is OK. */
4212 goto done;
4213 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
4214 break;
4216 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
4217 "following SELECT TYPE at %C");
4219 reject_statement ();
4222 /* At this point, we're got a nonempty select block. */
4223 cp = new_level (cp);
4224 *cp = new_st;
4226 accept_statement (st);
4230 st = parse_executable (ST_NONE);
4231 switch (st)
4233 case ST_NONE:
4234 unexpected_eof ();
4236 case ST_TYPE_IS:
4237 case ST_CLASS_IS:
4238 cp = new_level (gfc_state_stack->head);
4239 *cp = new_st;
4240 gfc_clear_new_st ();
4242 accept_statement (st);
4243 /* Fall through */
4245 case ST_END_SELECT:
4246 break;
4248 /* Can't have an executable statement because of
4249 parse_executable(). */
4250 default:
4251 unexpected_statement (st);
4252 break;
4255 while (st != ST_END_SELECT);
4257 done:
4258 pop_state ();
4259 accept_statement (st);
4260 gfc_current_ns = gfc_current_ns->parent;
4261 select_type_pop ();
4265 /* Given a symbol, make sure it is not an iteration variable for a DO
4266 statement. This subroutine is called when the symbol is seen in a
4267 context that causes it to become redefined. If the symbol is an
4268 iterator, we generate an error message and return nonzero. */
4271 gfc_check_do_variable (gfc_symtree *st)
4273 gfc_state_data *s;
4275 for (s=gfc_state_stack; s; s = s->previous)
4276 if (s->do_variable == st)
4278 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
4279 "loop beginning at %L", st->name, &s->head->loc);
4280 return 1;
4283 return 0;
4287 /* Checks to see if the current statement label closes an enddo.
4288 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
4289 an error) if it incorrectly closes an ENDDO. */
4291 static int
4292 check_do_closure (void)
4294 gfc_state_data *p;
4296 if (gfc_statement_label == NULL)
4297 return 0;
4299 for (p = gfc_state_stack; p; p = p->previous)
4300 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
4301 break;
4303 if (p == NULL)
4304 return 0; /* No loops to close */
4306 if (p->ext.end_do_label == gfc_statement_label)
4308 if (p == gfc_state_stack)
4309 return 1;
4311 gfc_error ("End of nonblock DO statement at %C is within another block");
4312 return 2;
4315 /* At this point, the label doesn't terminate the innermost loop.
4316 Make sure it doesn't terminate another one. */
4317 for (; p; p = p->previous)
4318 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
4319 && p->ext.end_do_label == gfc_statement_label)
4321 gfc_error ("End of nonblock DO statement at %C is interwoven "
4322 "with another DO loop");
4323 return 2;
4326 return 0;
4330 /* Parse a series of contained program units. */
4332 static void parse_progunit (gfc_statement);
4335 /* Parse a CRITICAL block. */
4337 static void
4338 parse_critical_block (void)
4340 gfc_code *top, *d;
4341 gfc_state_data s, *sd;
4342 gfc_statement st;
4344 for (sd = gfc_state_stack; sd; sd = sd->previous)
4345 if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
4346 gfc_error_now (is_oacc (sd)
4347 ? G_("CRITICAL block inside of OpenACC region at %C")
4348 : G_("CRITICAL block inside of OpenMP region at %C"));
4350 s.ext.end_do_label = new_st.label1;
4352 accept_statement (ST_CRITICAL);
4353 top = gfc_state_stack->tail;
4355 push_state (&s, COMP_CRITICAL, gfc_new_block);
4357 d = add_statement ();
4358 d->op = EXEC_CRITICAL;
4359 top->block = d;
4363 st = parse_executable (ST_NONE);
4365 switch (st)
4367 case ST_NONE:
4368 unexpected_eof ();
4369 break;
4371 case ST_END_CRITICAL:
4372 if (s.ext.end_do_label != NULL
4373 && s.ext.end_do_label != gfc_statement_label)
4374 gfc_error_now ("Statement label in END CRITICAL at %C does not "
4375 "match CRITICAL label");
4377 if (gfc_statement_label != NULL)
4379 new_st.op = EXEC_NOP;
4380 add_statement ();
4382 break;
4384 default:
4385 unexpected_statement (st);
4386 break;
4389 while (st != ST_END_CRITICAL);
4391 pop_state ();
4392 accept_statement (st);
4396 /* Set up the local namespace for a BLOCK construct. */
4398 gfc_namespace*
4399 gfc_build_block_ns (gfc_namespace *parent_ns)
4401 gfc_namespace* my_ns;
4402 static int numblock = 1;
4404 my_ns = gfc_get_namespace (parent_ns, 1);
4405 my_ns->construct_entities = 1;
4407 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
4408 code generation (so it must not be NULL).
4409 We set its recursive argument if our container procedure is recursive, so
4410 that local variables are accordingly placed on the stack when it
4411 will be necessary. */
4412 if (gfc_new_block)
4413 my_ns->proc_name = gfc_new_block;
4414 else
4416 bool t;
4417 char buffer[20]; /* Enough to hold "block@2147483648\n". */
4419 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
4420 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
4421 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
4422 my_ns->proc_name->name, NULL);
4423 gcc_assert (t);
4424 gfc_commit_symbol (my_ns->proc_name);
4427 if (parent_ns->proc_name)
4428 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
4430 return my_ns;
4434 /* Parse a BLOCK construct. */
4436 static void
4437 parse_block_construct (void)
4439 gfc_namespace* my_ns;
4440 gfc_namespace* my_parent;
4441 gfc_state_data s;
4443 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
4445 my_ns = gfc_build_block_ns (gfc_current_ns);
4447 new_st.op = EXEC_BLOCK;
4448 new_st.ext.block.ns = my_ns;
4449 new_st.ext.block.assoc = NULL;
4450 accept_statement (ST_BLOCK);
4452 push_state (&s, COMP_BLOCK, my_ns->proc_name);
4453 gfc_current_ns = my_ns;
4454 my_parent = my_ns->parent;
4456 parse_progunit (ST_NONE);
4458 /* Don't depend on the value of gfc_current_ns; it might have been
4459 reset if the block had errors and was cleaned up. */
4460 gfc_current_ns = my_parent;
4462 pop_state ();
4466 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
4467 behind the scenes with compiler-generated variables. */
4469 static void
4470 parse_associate (void)
4472 gfc_namespace* my_ns;
4473 gfc_state_data s;
4474 gfc_statement st;
4475 gfc_association_list* a;
4477 gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
4479 my_ns = gfc_build_block_ns (gfc_current_ns);
4481 new_st.op = EXEC_BLOCK;
4482 new_st.ext.block.ns = my_ns;
4483 gcc_assert (new_st.ext.block.assoc);
4485 /* Add all associate-names as BLOCK variables. Creating them is enough
4486 for now, they'll get their values during trans-* phase. */
4487 gfc_current_ns = my_ns;
4488 for (a = new_st.ext.block.assoc; a; a = a->next)
4490 gfc_symbol* sym;
4491 gfc_ref *ref;
4492 gfc_array_ref *array_ref;
4494 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
4495 gcc_unreachable ();
4497 sym = a->st->n.sym;
4498 sym->attr.flavor = FL_VARIABLE;
4499 sym->assoc = a;
4500 sym->declared_at = a->where;
4501 gfc_set_sym_referenced (sym);
4503 /* Initialize the typespec. It is not available in all cases,
4504 however, as it may only be set on the target during resolution.
4505 Still, sometimes it helps to have it right now -- especially
4506 for parsing component references on the associate-name
4507 in case of association to a derived-type. */
4508 sym->ts = a->target->ts;
4510 /* Check if the target expression is array valued. This can not always
4511 be done by looking at target.rank, because that might not have been
4512 set yet. Therefore traverse the chain of refs, looking for the last
4513 array ref and evaluate that. */
4514 array_ref = NULL;
4515 for (ref = a->target->ref; ref; ref = ref->next)
4516 if (ref->type == REF_ARRAY)
4517 array_ref = &ref->u.ar;
4518 if (array_ref || a->target->rank)
4520 gfc_array_spec *as;
4521 int dim, rank = 0;
4522 if (array_ref)
4524 a->rankguessed = 1;
4525 /* Count the dimension, that have a non-scalar extend. */
4526 for (dim = 0; dim < array_ref->dimen; ++dim)
4527 if (array_ref->dimen_type[dim] != DIMEN_ELEMENT
4528 && !(array_ref->dimen_type[dim] == DIMEN_UNKNOWN
4529 && array_ref->end[dim] == NULL
4530 && array_ref->start[dim] != NULL))
4531 ++rank;
4533 else
4534 rank = a->target->rank;
4535 /* When the rank is greater than zero then sym will be an array. */
4536 if (sym->ts.type == BT_CLASS)
4538 if ((!CLASS_DATA (sym)->as && rank != 0)
4539 || (CLASS_DATA (sym)->as
4540 && CLASS_DATA (sym)->as->rank != rank))
4542 /* Don't just (re-)set the attr and as in the sym.ts,
4543 because this modifies the target's attr and as. Copy the
4544 data and do a build_class_symbol. */
4545 symbol_attribute attr = CLASS_DATA (a->target)->attr;
4546 int corank = gfc_get_corank (a->target);
4547 gfc_typespec type;
4549 if (rank || corank)
4551 as = gfc_get_array_spec ();
4552 as->type = AS_DEFERRED;
4553 as->rank = rank;
4554 as->corank = corank;
4555 attr.dimension = rank ? 1 : 0;
4556 attr.codimension = corank ? 1 : 0;
4558 else
4560 as = NULL;
4561 attr.dimension = attr.codimension = 0;
4563 attr.class_ok = 0;
4564 type = CLASS_DATA (sym)->ts;
4565 if (!gfc_build_class_symbol (&type,
4566 &attr, &as))
4567 gcc_unreachable ();
4568 sym->ts = type;
4569 sym->ts.type = BT_CLASS;
4570 sym->attr.class_ok = 1;
4572 else
4573 sym->attr.class_ok = 1;
4575 else if ((!sym->as && rank != 0)
4576 || (sym->as && sym->as->rank != rank))
4578 as = gfc_get_array_spec ();
4579 as->type = AS_DEFERRED;
4580 as->rank = rank;
4581 as->corank = gfc_get_corank (a->target);
4582 sym->as = as;
4583 sym->attr.dimension = 1;
4584 if (as->corank)
4585 sym->attr.codimension = 1;
4590 accept_statement (ST_ASSOCIATE);
4591 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
4593 loop:
4594 st = parse_executable (ST_NONE);
4595 switch (st)
4597 case ST_NONE:
4598 unexpected_eof ();
4600 case_end:
4601 accept_statement (st);
4602 my_ns->code = gfc_state_stack->head;
4603 break;
4605 default:
4606 unexpected_statement (st);
4607 goto loop;
4610 gfc_current_ns = gfc_current_ns->parent;
4611 pop_state ();
4615 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4616 handled inside of parse_executable(), because they aren't really
4617 loop statements. */
4619 static void
4620 parse_do_block (void)
4622 gfc_statement st;
4623 gfc_code *top;
4624 gfc_state_data s;
4625 gfc_symtree *stree;
4626 gfc_exec_op do_op;
4628 do_op = new_st.op;
4629 s.ext.end_do_label = new_st.label1;
4631 if (new_st.ext.iterator != NULL)
4632 stree = new_st.ext.iterator->var->symtree;
4633 else
4634 stree = NULL;
4636 accept_statement (ST_DO);
4638 top = gfc_state_stack->tail;
4639 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
4640 gfc_new_block);
4642 s.do_variable = stree;
4644 top->block = new_level (top);
4645 top->block->op = EXEC_DO;
4647 loop:
4648 st = parse_executable (ST_NONE);
4650 switch (st)
4652 case ST_NONE:
4653 unexpected_eof ();
4655 case ST_ENDDO:
4656 if (s.ext.end_do_label != NULL
4657 && s.ext.end_do_label != gfc_statement_label)
4658 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4659 "DO label");
4661 if (gfc_statement_label != NULL)
4663 new_st.op = EXEC_NOP;
4664 add_statement ();
4666 break;
4668 case ST_IMPLIED_ENDDO:
4669 /* If the do-stmt of this DO construct has a do-construct-name,
4670 the corresponding end-do must be an end-do-stmt (with a matching
4671 name, but in that case we must have seen ST_ENDDO first).
4672 We only complain about this in pedantic mode. */
4673 if (gfc_current_block () != NULL)
4674 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4675 &gfc_current_block()->declared_at);
4677 break;
4679 default:
4680 unexpected_statement (st);
4681 goto loop;
4684 pop_state ();
4685 accept_statement (st);
4689 /* Parse the statements of OpenMP do/parallel do. */
4691 static gfc_statement
4692 parse_omp_do (gfc_statement omp_st)
4694 gfc_statement st;
4695 gfc_code *cp, *np;
4696 gfc_state_data s;
4698 accept_statement (omp_st);
4700 cp = gfc_state_stack->tail;
4701 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4702 np = new_level (cp);
4703 np->op = cp->op;
4704 np->block = NULL;
4706 for (;;)
4708 st = next_statement ();
4709 if (st == ST_NONE)
4710 unexpected_eof ();
4711 else if (st == ST_DO)
4712 break;
4713 else
4714 unexpected_statement (st);
4717 parse_do_block ();
4718 if (gfc_statement_label != NULL
4719 && gfc_state_stack->previous != NULL
4720 && gfc_state_stack->previous->state == COMP_DO
4721 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4723 /* In
4724 DO 100 I=1,10
4725 !$OMP DO
4726 DO J=1,10
4728 100 CONTINUE
4729 there should be no !$OMP END DO. */
4730 pop_state ();
4731 return ST_IMPLIED_ENDDO;
4734 check_do_closure ();
4735 pop_state ();
4737 st = next_statement ();
4738 gfc_statement omp_end_st = ST_OMP_END_DO;
4739 switch (omp_st)
4741 case ST_OMP_DISTRIBUTE: omp_end_st = ST_OMP_END_DISTRIBUTE; break;
4742 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4743 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4744 break;
4745 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4746 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4747 break;
4748 case ST_OMP_DISTRIBUTE_SIMD:
4749 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4750 break;
4751 case ST_OMP_DO: omp_end_st = ST_OMP_END_DO; break;
4752 case ST_OMP_DO_SIMD: omp_end_st = ST_OMP_END_DO_SIMD; break;
4753 case ST_OMP_PARALLEL_DO: omp_end_st = ST_OMP_END_PARALLEL_DO; break;
4754 case ST_OMP_PARALLEL_DO_SIMD:
4755 omp_end_st = ST_OMP_END_PARALLEL_DO_SIMD;
4756 break;
4757 case ST_OMP_SIMD: omp_end_st = ST_OMP_END_SIMD; break;
4758 case ST_OMP_TARGET_PARALLEL_DO:
4759 omp_end_st = ST_OMP_END_TARGET_PARALLEL_DO;
4760 break;
4761 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
4762 omp_end_st = ST_OMP_END_TARGET_PARALLEL_DO_SIMD;
4763 break;
4764 case ST_OMP_TARGET_SIMD: omp_end_st = ST_OMP_END_TARGET_SIMD; break;
4765 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4766 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4767 break;
4768 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4769 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4770 break;
4771 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4772 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4773 break;
4774 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4775 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4776 break;
4777 case ST_OMP_TASKLOOP: omp_end_st = ST_OMP_END_TASKLOOP; break;
4778 case ST_OMP_TASKLOOP_SIMD: omp_end_st = ST_OMP_END_TASKLOOP_SIMD; break;
4779 case ST_OMP_TEAMS_DISTRIBUTE:
4780 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4781 break;
4782 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4783 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4784 break;
4785 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4786 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4787 break;
4788 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4789 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4790 break;
4791 default: gcc_unreachable ();
4793 if (st == omp_end_st)
4795 if (new_st.op == EXEC_OMP_END_NOWAIT)
4796 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4797 else
4798 gcc_assert (new_st.op == EXEC_NOP);
4799 gfc_clear_new_st ();
4800 gfc_commit_symbols ();
4801 gfc_warning_check ();
4802 st = next_statement ();
4804 return st;
4808 /* Parse the statements of OpenMP atomic directive. */
4810 static gfc_statement
4811 parse_omp_oacc_atomic (bool omp_p)
4813 gfc_statement st, st_atomic, st_end_atomic;
4814 gfc_code *cp, *np;
4815 gfc_state_data s;
4816 int count;
4818 if (omp_p)
4820 st_atomic = ST_OMP_ATOMIC;
4821 st_end_atomic = ST_OMP_END_ATOMIC;
4823 else
4825 st_atomic = ST_OACC_ATOMIC;
4826 st_end_atomic = ST_OACC_END_ATOMIC;
4828 accept_statement (st_atomic);
4830 cp = gfc_state_stack->tail;
4831 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4832 np = new_level (cp);
4833 np->op = cp->op;
4834 np->block = NULL;
4835 np->ext.omp_atomic = cp->ext.omp_atomic;
4836 count = 1 + ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4837 == GFC_OMP_ATOMIC_CAPTURE);
4839 while (count)
4841 st = next_statement ();
4842 if (st == ST_NONE)
4843 unexpected_eof ();
4844 else if (st == ST_ASSIGNMENT)
4846 accept_statement (st);
4847 count--;
4849 else
4850 unexpected_statement (st);
4853 pop_state ();
4855 st = next_statement ();
4856 if (st == st_end_atomic)
4858 gfc_clear_new_st ();
4859 gfc_commit_symbols ();
4860 gfc_warning_check ();
4861 st = next_statement ();
4863 else if ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4864 == GFC_OMP_ATOMIC_CAPTURE)
4865 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4866 return st;
4870 /* Parse the statements of an OpenACC structured block. */
4872 static void
4873 parse_oacc_structured_block (gfc_statement acc_st)
4875 gfc_statement st, acc_end_st;
4876 gfc_code *cp, *np;
4877 gfc_state_data s, *sd;
4879 for (sd = gfc_state_stack; sd; sd = sd->previous)
4880 if (sd->state == COMP_CRITICAL)
4881 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4883 accept_statement (acc_st);
4885 cp = gfc_state_stack->tail;
4886 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4887 np = new_level (cp);
4888 np->op = cp->op;
4889 np->block = NULL;
4890 switch (acc_st)
4892 case ST_OACC_PARALLEL:
4893 acc_end_st = ST_OACC_END_PARALLEL;
4894 break;
4895 case ST_OACC_KERNELS:
4896 acc_end_st = ST_OACC_END_KERNELS;
4897 break;
4898 case ST_OACC_DATA:
4899 acc_end_st = ST_OACC_END_DATA;
4900 break;
4901 case ST_OACC_HOST_DATA:
4902 acc_end_st = ST_OACC_END_HOST_DATA;
4903 break;
4904 default:
4905 gcc_unreachable ();
4910 st = parse_executable (ST_NONE);
4911 if (st == ST_NONE)
4912 unexpected_eof ();
4913 else if (st != acc_end_st)
4915 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
4916 reject_statement ();
4919 while (st != acc_end_st);
4921 gcc_assert (new_st.op == EXEC_NOP);
4923 gfc_clear_new_st ();
4924 gfc_commit_symbols ();
4925 gfc_warning_check ();
4926 pop_state ();
4929 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4931 static gfc_statement
4932 parse_oacc_loop (gfc_statement acc_st)
4934 gfc_statement st;
4935 gfc_code *cp, *np;
4936 gfc_state_data s, *sd;
4938 for (sd = gfc_state_stack; sd; sd = sd->previous)
4939 if (sd->state == COMP_CRITICAL)
4940 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4942 accept_statement (acc_st);
4944 cp = gfc_state_stack->tail;
4945 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4946 np = new_level (cp);
4947 np->op = cp->op;
4948 np->block = NULL;
4950 for (;;)
4952 st = next_statement ();
4953 if (st == ST_NONE)
4954 unexpected_eof ();
4955 else if (st == ST_DO)
4956 break;
4957 else
4959 gfc_error ("Expected DO loop at %C");
4960 reject_statement ();
4964 parse_do_block ();
4965 if (gfc_statement_label != NULL
4966 && gfc_state_stack->previous != NULL
4967 && gfc_state_stack->previous->state == COMP_DO
4968 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4970 pop_state ();
4971 return ST_IMPLIED_ENDDO;
4974 check_do_closure ();
4975 pop_state ();
4977 st = next_statement ();
4978 if (st == ST_OACC_END_LOOP)
4979 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
4980 if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
4981 (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
4982 (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
4984 gcc_assert (new_st.op == EXEC_NOP);
4985 gfc_clear_new_st ();
4986 gfc_commit_symbols ();
4987 gfc_warning_check ();
4988 st = next_statement ();
4990 return st;
4994 /* Parse the statements of an OpenMP structured block. */
4996 static void
4997 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
4999 gfc_statement st, omp_end_st;
5000 gfc_code *cp, *np;
5001 gfc_state_data s;
5003 accept_statement (omp_st);
5005 cp = gfc_state_stack->tail;
5006 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
5007 np = new_level (cp);
5008 np->op = cp->op;
5009 np->block = NULL;
5011 switch (omp_st)
5013 case ST_OMP_PARALLEL:
5014 omp_end_st = ST_OMP_END_PARALLEL;
5015 break;
5016 case ST_OMP_PARALLEL_SECTIONS:
5017 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
5018 break;
5019 case ST_OMP_SECTIONS:
5020 omp_end_st = ST_OMP_END_SECTIONS;
5021 break;
5022 case ST_OMP_ORDERED:
5023 omp_end_st = ST_OMP_END_ORDERED;
5024 break;
5025 case ST_OMP_CRITICAL:
5026 omp_end_st = ST_OMP_END_CRITICAL;
5027 break;
5028 case ST_OMP_MASTER:
5029 omp_end_st = ST_OMP_END_MASTER;
5030 break;
5031 case ST_OMP_SINGLE:
5032 omp_end_st = ST_OMP_END_SINGLE;
5033 break;
5034 case ST_OMP_TARGET:
5035 omp_end_st = ST_OMP_END_TARGET;
5036 break;
5037 case ST_OMP_TARGET_DATA:
5038 omp_end_st = ST_OMP_END_TARGET_DATA;
5039 break;
5040 case ST_OMP_TARGET_TEAMS:
5041 omp_end_st = ST_OMP_END_TARGET_TEAMS;
5042 break;
5043 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
5044 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
5045 break;
5046 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
5047 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
5048 break;
5049 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5050 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
5051 break;
5052 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
5053 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
5054 break;
5055 case ST_OMP_TASK:
5056 omp_end_st = ST_OMP_END_TASK;
5057 break;
5058 case ST_OMP_TASKGROUP:
5059 omp_end_st = ST_OMP_END_TASKGROUP;
5060 break;
5061 case ST_OMP_TEAMS:
5062 omp_end_st = ST_OMP_END_TEAMS;
5063 break;
5064 case ST_OMP_TEAMS_DISTRIBUTE:
5065 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
5066 break;
5067 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
5068 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
5069 break;
5070 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5071 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
5072 break;
5073 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
5074 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
5075 break;
5076 case ST_OMP_DISTRIBUTE:
5077 omp_end_st = ST_OMP_END_DISTRIBUTE;
5078 break;
5079 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
5080 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
5081 break;
5082 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
5083 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
5084 break;
5085 case ST_OMP_DISTRIBUTE_SIMD:
5086 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
5087 break;
5088 case ST_OMP_WORKSHARE:
5089 omp_end_st = ST_OMP_END_WORKSHARE;
5090 break;
5091 case ST_OMP_PARALLEL_WORKSHARE:
5092 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
5093 break;
5094 default:
5095 gcc_unreachable ();
5100 if (workshare_stmts_only)
5102 /* Inside of !$omp workshare, only
5103 scalar assignments
5104 array assignments
5105 where statements and constructs
5106 forall statements and constructs
5107 !$omp atomic
5108 !$omp critical
5109 !$omp parallel
5110 are allowed. For !$omp critical these
5111 restrictions apply recursively. */
5112 bool cycle = true;
5114 st = next_statement ();
5115 for (;;)
5117 switch (st)
5119 case ST_NONE:
5120 unexpected_eof ();
5122 case ST_ASSIGNMENT:
5123 case ST_WHERE:
5124 case ST_FORALL:
5125 accept_statement (st);
5126 break;
5128 case ST_WHERE_BLOCK:
5129 parse_where_block ();
5130 break;
5132 case ST_FORALL_BLOCK:
5133 parse_forall_block ();
5134 break;
5136 case ST_OMP_PARALLEL:
5137 case ST_OMP_PARALLEL_SECTIONS:
5138 parse_omp_structured_block (st, false);
5139 break;
5141 case ST_OMP_PARALLEL_WORKSHARE:
5142 case ST_OMP_CRITICAL:
5143 parse_omp_structured_block (st, true);
5144 break;
5146 case ST_OMP_PARALLEL_DO:
5147 case ST_OMP_PARALLEL_DO_SIMD:
5148 st = parse_omp_do (st);
5149 continue;
5151 case ST_OMP_ATOMIC:
5152 st = parse_omp_oacc_atomic (true);
5153 continue;
5155 default:
5156 cycle = false;
5157 break;
5160 if (!cycle)
5161 break;
5163 st = next_statement ();
5166 else
5167 st = parse_executable (ST_NONE);
5168 if (st == ST_NONE)
5169 unexpected_eof ();
5170 else if (st == ST_OMP_SECTION
5171 && (omp_st == ST_OMP_SECTIONS
5172 || omp_st == ST_OMP_PARALLEL_SECTIONS))
5174 np = new_level (np);
5175 np->op = cp->op;
5176 np->block = NULL;
5178 else if (st != omp_end_st)
5179 unexpected_statement (st);
5181 while (st != omp_end_st);
5183 switch (new_st.op)
5185 case EXEC_OMP_END_NOWAIT:
5186 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
5187 break;
5188 case EXEC_OMP_END_CRITICAL:
5189 if (((cp->ext.omp_clauses == NULL) ^ (new_st.ext.omp_name == NULL))
5190 || (new_st.ext.omp_name != NULL
5191 && strcmp (cp->ext.omp_clauses->critical_name,
5192 new_st.ext.omp_name) != 0))
5193 gfc_error ("Name after !$omp critical and !$omp end critical does "
5194 "not match at %C");
5195 free (CONST_CAST (char *, new_st.ext.omp_name));
5196 new_st.ext.omp_name = NULL;
5197 break;
5198 case EXEC_OMP_END_SINGLE:
5199 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
5200 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
5201 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
5202 gfc_free_omp_clauses (new_st.ext.omp_clauses);
5203 break;
5204 case EXEC_NOP:
5205 break;
5206 default:
5207 gcc_unreachable ();
5210 gfc_clear_new_st ();
5211 gfc_commit_symbols ();
5212 gfc_warning_check ();
5213 pop_state ();
5217 /* Accept a series of executable statements. We return the first
5218 statement that doesn't fit to the caller. Any block statements are
5219 passed on to the correct handler, which usually passes the buck
5220 right back here. */
5222 static gfc_statement
5223 parse_executable (gfc_statement st)
5225 int close_flag;
5227 if (st == ST_NONE)
5228 st = next_statement ();
5230 for (;;)
5232 close_flag = check_do_closure ();
5233 if (close_flag)
5234 switch (st)
5236 case ST_GOTO:
5237 case ST_END_PROGRAM:
5238 case ST_RETURN:
5239 case ST_EXIT:
5240 case ST_END_FUNCTION:
5241 case ST_CYCLE:
5242 case ST_PAUSE:
5243 case ST_STOP:
5244 case ST_ERROR_STOP:
5245 case ST_END_SUBROUTINE:
5247 case ST_DO:
5248 case ST_FORALL:
5249 case ST_WHERE:
5250 case ST_SELECT_CASE:
5251 gfc_error ("%s statement at %C cannot terminate a non-block "
5252 "DO loop", gfc_ascii_statement (st));
5253 break;
5255 default:
5256 break;
5259 switch (st)
5261 case ST_NONE:
5262 unexpected_eof ();
5264 case ST_DATA:
5265 gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
5266 "first executable statement");
5267 /* Fall through. */
5269 case ST_FORMAT:
5270 case ST_ENTRY:
5271 case_executable:
5272 accept_statement (st);
5273 if (close_flag == 1)
5274 return ST_IMPLIED_ENDDO;
5275 break;
5277 case ST_BLOCK:
5278 parse_block_construct ();
5279 break;
5281 case ST_ASSOCIATE:
5282 parse_associate ();
5283 break;
5285 case ST_IF_BLOCK:
5286 parse_if_block ();
5287 break;
5289 case ST_SELECT_CASE:
5290 parse_select_block ();
5291 break;
5293 case ST_SELECT_TYPE:
5294 parse_select_type_block ();
5295 break;
5297 case ST_DO:
5298 parse_do_block ();
5299 if (check_do_closure () == 1)
5300 return ST_IMPLIED_ENDDO;
5301 break;
5303 case ST_CRITICAL:
5304 parse_critical_block ();
5305 break;
5307 case ST_WHERE_BLOCK:
5308 parse_where_block ();
5309 break;
5311 case ST_FORALL_BLOCK:
5312 parse_forall_block ();
5313 break;
5315 case ST_OACC_PARALLEL_LOOP:
5316 case ST_OACC_KERNELS_LOOP:
5317 case ST_OACC_LOOP:
5318 st = parse_oacc_loop (st);
5319 if (st == ST_IMPLIED_ENDDO)
5320 return st;
5321 continue;
5323 case ST_OACC_PARALLEL:
5324 case ST_OACC_KERNELS:
5325 case ST_OACC_DATA:
5326 case ST_OACC_HOST_DATA:
5327 parse_oacc_structured_block (st);
5328 break;
5330 case ST_OMP_PARALLEL:
5331 case ST_OMP_PARALLEL_SECTIONS:
5332 case ST_OMP_SECTIONS:
5333 case ST_OMP_ORDERED:
5334 case ST_OMP_CRITICAL:
5335 case ST_OMP_MASTER:
5336 case ST_OMP_SINGLE:
5337 case ST_OMP_TARGET:
5338 case ST_OMP_TARGET_DATA:
5339 case ST_OMP_TARGET_PARALLEL:
5340 case ST_OMP_TARGET_TEAMS:
5341 case ST_OMP_TEAMS:
5342 case ST_OMP_TASK:
5343 case ST_OMP_TASKGROUP:
5344 parse_omp_structured_block (st, false);
5345 break;
5347 case ST_OMP_WORKSHARE:
5348 case ST_OMP_PARALLEL_WORKSHARE:
5349 parse_omp_structured_block (st, true);
5350 break;
5352 case ST_OMP_DISTRIBUTE:
5353 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
5354 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
5355 case ST_OMP_DISTRIBUTE_SIMD:
5356 case ST_OMP_DO:
5357 case ST_OMP_DO_SIMD:
5358 case ST_OMP_PARALLEL_DO:
5359 case ST_OMP_PARALLEL_DO_SIMD:
5360 case ST_OMP_SIMD:
5361 case ST_OMP_TARGET_PARALLEL_DO:
5362 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
5363 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
5364 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
5365 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5366 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
5367 case ST_OMP_TASKLOOP:
5368 case ST_OMP_TASKLOOP_SIMD:
5369 case ST_OMP_TEAMS_DISTRIBUTE:
5370 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
5371 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5372 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
5373 st = parse_omp_do (st);
5374 if (st == ST_IMPLIED_ENDDO)
5375 return st;
5376 continue;
5378 case ST_OACC_ATOMIC:
5379 st = parse_omp_oacc_atomic (false);
5380 continue;
5382 case ST_OMP_ATOMIC:
5383 st = parse_omp_oacc_atomic (true);
5384 continue;
5386 default:
5387 return st;
5390 st = next_statement ();
5395 /* Fix the symbols for sibling functions. These are incorrectly added to
5396 the child namespace as the parser didn't know about this procedure. */
5398 static void
5399 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
5401 gfc_namespace *ns;
5402 gfc_symtree *st;
5403 gfc_symbol *old_sym;
5405 for (ns = siblings; ns; ns = ns->sibling)
5407 st = gfc_find_symtree (ns->sym_root, sym->name);
5409 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
5410 goto fixup_contained;
5412 if ((st->n.sym->attr.flavor == FL_DERIVED
5413 && sym->attr.generic && sym->attr.function)
5414 ||(sym->attr.flavor == FL_DERIVED
5415 && st->n.sym->attr.generic && st->n.sym->attr.function))
5416 goto fixup_contained;
5418 old_sym = st->n.sym;
5419 if (old_sym->ns == ns
5420 && !old_sym->attr.contained
5422 /* By 14.6.1.3, host association should be excluded
5423 for the following. */
5424 && !(old_sym->attr.external
5425 || (old_sym->ts.type != BT_UNKNOWN
5426 && !old_sym->attr.implicit_type)
5427 || old_sym->attr.flavor == FL_PARAMETER
5428 || old_sym->attr.use_assoc
5429 || old_sym->attr.in_common
5430 || old_sym->attr.in_equivalence
5431 || old_sym->attr.data
5432 || old_sym->attr.dummy
5433 || old_sym->attr.result
5434 || old_sym->attr.dimension
5435 || old_sym->attr.allocatable
5436 || old_sym->attr.intrinsic
5437 || old_sym->attr.generic
5438 || old_sym->attr.flavor == FL_NAMELIST
5439 || old_sym->attr.flavor == FL_LABEL
5440 || old_sym->attr.proc == PROC_ST_FUNCTION))
5442 /* Replace it with the symbol from the parent namespace. */
5443 st->n.sym = sym;
5444 sym->refs++;
5446 gfc_release_symbol (old_sym);
5449 fixup_contained:
5450 /* Do the same for any contained procedures. */
5451 gfc_fixup_sibling_symbols (sym, ns->contained);
5455 static void
5456 parse_contained (int module)
5458 gfc_namespace *ns, *parent_ns, *tmp;
5459 gfc_state_data s1, s2;
5460 gfc_statement st;
5461 gfc_symbol *sym;
5462 gfc_entry_list *el;
5463 locus old_loc;
5464 int contains_statements = 0;
5465 int seen_error = 0;
5467 push_state (&s1, COMP_CONTAINS, NULL);
5468 parent_ns = gfc_current_ns;
5472 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
5474 gfc_current_ns->sibling = parent_ns->contained;
5475 parent_ns->contained = gfc_current_ns;
5477 next:
5478 /* Process the next available statement. We come here if we got an error
5479 and rejected the last statement. */
5480 old_loc = gfc_current_locus;
5481 st = next_statement ();
5483 switch (st)
5485 case ST_NONE:
5486 unexpected_eof ();
5488 case ST_FUNCTION:
5489 case ST_SUBROUTINE:
5490 contains_statements = 1;
5491 accept_statement (st);
5493 push_state (&s2,
5494 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
5495 gfc_new_block);
5497 /* For internal procedures, create/update the symbol in the
5498 parent namespace. */
5500 if (!module)
5502 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
5503 gfc_error ("Contained procedure %qs at %C is already "
5504 "ambiguous", gfc_new_block->name);
5505 else
5507 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
5508 sym->name,
5509 &gfc_new_block->declared_at))
5511 if (st == ST_FUNCTION)
5512 gfc_add_function (&sym->attr, sym->name,
5513 &gfc_new_block->declared_at);
5514 else
5515 gfc_add_subroutine (&sym->attr, sym->name,
5516 &gfc_new_block->declared_at);
5520 gfc_commit_symbols ();
5522 else
5523 sym = gfc_new_block;
5525 /* Mark this as a contained function, so it isn't replaced
5526 by other module functions. */
5527 sym->attr.contained = 1;
5529 /* Set implicit_pure so that it can be reset if any of the
5530 tests for purity fail. This is used for some optimisation
5531 during translation. */
5532 if (!sym->attr.pure)
5533 sym->attr.implicit_pure = 1;
5535 parse_progunit (ST_NONE);
5537 /* Fix up any sibling functions that refer to this one. */
5538 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
5539 /* Or refer to any of its alternate entry points. */
5540 for (el = gfc_current_ns->entries; el; el = el->next)
5541 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
5543 gfc_current_ns->code = s2.head;
5544 gfc_current_ns = parent_ns;
5546 pop_state ();
5547 break;
5549 /* These statements are associated with the end of the host unit. */
5550 case ST_END_FUNCTION:
5551 case ST_END_MODULE:
5552 case ST_END_SUBMODULE:
5553 case ST_END_PROGRAM:
5554 case ST_END_SUBROUTINE:
5555 accept_statement (st);
5556 gfc_current_ns->code = s1.head;
5557 break;
5559 default:
5560 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
5561 gfc_ascii_statement (st));
5562 reject_statement ();
5563 seen_error = 1;
5564 goto next;
5565 break;
5568 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
5569 && st != ST_END_MODULE && st != ST_END_SUBMODULE
5570 && st != ST_END_PROGRAM);
5572 /* The first namespace in the list is guaranteed to not have
5573 anything (worthwhile) in it. */
5574 tmp = gfc_current_ns;
5575 gfc_current_ns = parent_ns;
5576 if (seen_error && tmp->refs > 1)
5577 gfc_free_namespace (tmp);
5579 ns = gfc_current_ns->contained;
5580 gfc_current_ns->contained = ns->sibling;
5581 gfc_free_namespace (ns);
5583 pop_state ();
5584 if (!contains_statements)
5585 gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
5586 "FUNCTION or SUBROUTINE statement at %L", &old_loc);
5590 /* The result variable in a MODULE PROCEDURE needs to be created and
5591 its characteristics copied from the interface since it is neither
5592 declared in the procedure declaration nor in the specification
5593 part. */
5595 static void
5596 get_modproc_result (void)
5598 gfc_symbol *proc;
5599 if (gfc_state_stack->previous
5600 && gfc_state_stack->previous->state == COMP_CONTAINS
5601 && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
5603 proc = gfc_current_ns->proc_name ? gfc_current_ns->proc_name : NULL;
5604 if (proc != NULL
5605 && proc->attr.function
5606 && proc->tlink
5607 && proc->tlink->result
5608 && proc->tlink->result != proc->tlink)
5610 gfc_copy_dummy_sym (&proc->result, proc->tlink->result, 1);
5611 gfc_set_sym_referenced (proc->result);
5612 proc->result->attr.if_source = IFSRC_DECL;
5613 gfc_commit_symbol (proc->result);
5619 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
5621 static void
5622 parse_progunit (gfc_statement st)
5624 gfc_state_data *p;
5625 int n;
5627 if (gfc_new_block
5628 && gfc_new_block->abr_modproc_decl
5629 && gfc_new_block->attr.function)
5630 get_modproc_result ();
5632 st = parse_spec (st);
5633 switch (st)
5635 case ST_NONE:
5636 unexpected_eof ();
5638 case ST_CONTAINS:
5639 /* This is not allowed within BLOCK! */
5640 if (gfc_current_state () != COMP_BLOCK)
5641 goto contains;
5642 break;
5644 case_end:
5645 accept_statement (st);
5646 goto done;
5648 default:
5649 break;
5652 if (gfc_current_state () == COMP_FUNCTION)
5653 gfc_check_function_type (gfc_current_ns);
5655 loop:
5656 for (;;)
5658 st = parse_executable (st);
5660 switch (st)
5662 case ST_NONE:
5663 unexpected_eof ();
5665 case ST_CONTAINS:
5666 /* This is not allowed within BLOCK! */
5667 if (gfc_current_state () != COMP_BLOCK)
5668 goto contains;
5669 break;
5671 case_end:
5672 accept_statement (st);
5673 goto done;
5675 default:
5676 break;
5679 unexpected_statement (st);
5680 reject_statement ();
5681 st = next_statement ();
5684 contains:
5685 n = 0;
5687 for (p = gfc_state_stack; p; p = p->previous)
5688 if (p->state == COMP_CONTAINS)
5689 n++;
5691 if (gfc_find_state (COMP_MODULE) == true
5692 || gfc_find_state (COMP_SUBMODULE) == true)
5693 n--;
5695 if (n > 0)
5697 gfc_error ("CONTAINS statement at %C is already in a contained "
5698 "program unit");
5699 reject_statement ();
5700 st = next_statement ();
5701 goto loop;
5704 parse_contained (0);
5706 done:
5707 gfc_current_ns->code = gfc_state_stack->head;
5711 /* Come here to complain about a global symbol already in use as
5712 something else. */
5714 void
5715 gfc_global_used (gfc_gsymbol *sym, locus *where)
5717 const char *name;
5719 if (where == NULL)
5720 where = &gfc_current_locus;
5722 switch(sym->type)
5724 case GSYM_PROGRAM:
5725 name = "PROGRAM";
5726 break;
5727 case GSYM_FUNCTION:
5728 name = "FUNCTION";
5729 break;
5730 case GSYM_SUBROUTINE:
5731 name = "SUBROUTINE";
5732 break;
5733 case GSYM_COMMON:
5734 name = "COMMON";
5735 break;
5736 case GSYM_BLOCK_DATA:
5737 name = "BLOCK DATA";
5738 break;
5739 case GSYM_MODULE:
5740 name = "MODULE";
5741 break;
5742 default:
5743 gfc_internal_error ("gfc_global_used(): Bad type");
5744 name = NULL;
5747 if (sym->binding_label)
5748 gfc_error ("Global binding name %qs at %L is already being used as a %s "
5749 "at %L", sym->binding_label, where, name, &sym->where);
5750 else
5751 gfc_error ("Global name %qs at %L is already being used as a %s at %L",
5752 sym->name, where, name, &sym->where);
5756 /* Parse a block data program unit. */
5758 static void
5759 parse_block_data (void)
5761 gfc_statement st;
5762 static locus blank_locus;
5763 static int blank_block=0;
5764 gfc_gsymbol *s;
5766 gfc_current_ns->proc_name = gfc_new_block;
5767 gfc_current_ns->is_block_data = 1;
5769 if (gfc_new_block == NULL)
5771 if (blank_block)
5772 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5773 "prior BLOCK DATA at %L", &blank_locus);
5774 else
5776 blank_block = 1;
5777 blank_locus = gfc_current_locus;
5780 else
5782 s = gfc_get_gsymbol (gfc_new_block->name);
5783 if (s->defined
5784 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
5785 gfc_global_used (s, &gfc_new_block->declared_at);
5786 else
5788 s->type = GSYM_BLOCK_DATA;
5789 s->where = gfc_new_block->declared_at;
5790 s->defined = 1;
5794 st = parse_spec (ST_NONE);
5796 while (st != ST_END_BLOCK_DATA)
5798 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5799 gfc_ascii_statement (st));
5800 reject_statement ();
5801 st = next_statement ();
5806 /* Following the association of the ancestor (sub)module symbols, they
5807 must be set host rather than use associated and all must be public.
5808 They are flagged up by 'used_in_submodule' so that they can be set
5809 DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl). Otherwise the
5810 linker chokes on multiple symbol definitions. */
5812 static void
5813 set_syms_host_assoc (gfc_symbol *sym)
5815 gfc_component *c;
5816 const char dot[2] = ".";
5817 char parent1[GFC_MAX_SYMBOL_LEN + 1];
5818 char parent2[GFC_MAX_SYMBOL_LEN + 1];
5820 if (sym == NULL)
5821 return;
5823 if (sym->attr.module_procedure)
5824 sym->attr.external = 0;
5826 sym->attr.use_assoc = 0;
5827 sym->attr.host_assoc = 1;
5828 sym->attr.used_in_submodule =1;
5830 if (sym->attr.flavor == FL_DERIVED)
5832 /* Derived types with PRIVATE components that are declared in
5833 modules other than the parent module must not be changed to be
5834 PUBLIC. The 'use-assoc' attribute must be reset so that the
5835 test in symbol.c(gfc_find_component) works correctly. This is
5836 not necessary for PRIVATE symbols since they are not read from
5837 the module. */
5838 memset(parent1, '\0', sizeof(parent1));
5839 memset(parent2, '\0', sizeof(parent2));
5840 strcpy (parent1, gfc_new_block->name);
5841 strcpy (parent2, sym->module);
5842 if (strcmp (strtok (parent1, dot), strtok (parent2, dot)) == 0)
5844 for (c = sym->components; c; c = c->next)
5845 c->attr.access = ACCESS_PUBLIC;
5847 else
5849 sym->attr.use_assoc = 1;
5850 sym->attr.host_assoc = 0;
5855 /* Parse a module subprogram. */
5857 static void
5858 parse_module (void)
5860 gfc_statement st;
5861 gfc_gsymbol *s;
5862 bool error;
5864 s = gfc_get_gsymbol (gfc_new_block->name);
5865 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
5866 gfc_global_used (s, &gfc_new_block->declared_at);
5867 else
5869 s->type = GSYM_MODULE;
5870 s->where = gfc_new_block->declared_at;
5871 s->defined = 1;
5874 /* Something is nulling the module_list after this point. This is good
5875 since it allows us to 'USE' the parent modules that the submodule
5876 inherits and to set (most) of the symbols as host associated. */
5877 if (gfc_current_state () == COMP_SUBMODULE)
5879 use_modules ();
5880 gfc_traverse_ns (gfc_current_ns, set_syms_host_assoc);
5883 st = parse_spec (ST_NONE);
5885 error = false;
5886 loop:
5887 switch (st)
5889 case ST_NONE:
5890 unexpected_eof ();
5892 case ST_CONTAINS:
5893 parse_contained (1);
5894 break;
5896 case ST_END_MODULE:
5897 case ST_END_SUBMODULE:
5898 accept_statement (st);
5899 break;
5901 default:
5902 gfc_error ("Unexpected %s statement in MODULE at %C",
5903 gfc_ascii_statement (st));
5905 error = true;
5906 reject_statement ();
5907 st = next_statement ();
5908 goto loop;
5911 /* Make sure not to free the namespace twice on error. */
5912 if (!error)
5913 s->ns = gfc_current_ns;
5917 /* Add a procedure name to the global symbol table. */
5919 static void
5920 add_global_procedure (bool sub)
5922 gfc_gsymbol *s;
5924 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5925 name is a global identifier. */
5926 if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
5928 s = gfc_get_gsymbol (gfc_new_block->name);
5930 if (s->defined
5931 || (s->type != GSYM_UNKNOWN
5932 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5934 gfc_global_used (s, &gfc_new_block->declared_at);
5935 /* Silence follow-up errors. */
5936 gfc_new_block->binding_label = NULL;
5938 else
5940 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5941 s->sym_name = gfc_new_block->name;
5942 s->where = gfc_new_block->declared_at;
5943 s->defined = 1;
5944 s->ns = gfc_current_ns;
5948 /* Don't add the symbol multiple times. */
5949 if (gfc_new_block->binding_label
5950 && (!gfc_notification_std (GFC_STD_F2008)
5951 || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
5953 s = gfc_get_gsymbol (gfc_new_block->binding_label);
5955 if (s->defined
5956 || (s->type != GSYM_UNKNOWN
5957 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5959 gfc_global_used (s, &gfc_new_block->declared_at);
5960 /* Silence follow-up errors. */
5961 gfc_new_block->binding_label = NULL;
5963 else
5965 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5966 s->sym_name = gfc_new_block->name;
5967 s->binding_label = gfc_new_block->binding_label;
5968 s->where = gfc_new_block->declared_at;
5969 s->defined = 1;
5970 s->ns = gfc_current_ns;
5976 /* Add a program to the global symbol table. */
5978 static void
5979 add_global_program (void)
5981 gfc_gsymbol *s;
5983 if (gfc_new_block == NULL)
5984 return;
5985 s = gfc_get_gsymbol (gfc_new_block->name);
5987 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
5988 gfc_global_used (s, &gfc_new_block->declared_at);
5989 else
5991 s->type = GSYM_PROGRAM;
5992 s->where = gfc_new_block->declared_at;
5993 s->defined = 1;
5994 s->ns = gfc_current_ns;
5999 /* Resolve all the program units. */
6000 static void
6001 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
6003 gfc_free_dt_list ();
6004 gfc_current_ns = gfc_global_ns_list;
6005 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6007 if (gfc_current_ns->proc_name
6008 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
6009 continue; /* Already resolved. */
6011 if (gfc_current_ns->proc_name)
6012 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
6013 gfc_resolve (gfc_current_ns);
6014 gfc_current_ns->derived_types = gfc_derived_types;
6015 gfc_derived_types = NULL;
6020 static void
6021 clean_up_modules (gfc_gsymbol *gsym)
6023 if (gsym == NULL)
6024 return;
6026 clean_up_modules (gsym->left);
6027 clean_up_modules (gsym->right);
6029 if (gsym->type != GSYM_MODULE || !gsym->ns)
6030 return;
6032 gfc_current_ns = gsym->ns;
6033 gfc_derived_types = gfc_current_ns->derived_types;
6034 gfc_done_2 ();
6035 gsym->ns = NULL;
6036 return;
6040 /* Translate all the program units. This could be in a different order
6041 to resolution if there are forward references in the file. */
6042 static void
6043 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
6045 int errors;
6047 gfc_current_ns = gfc_global_ns_list;
6048 gfc_get_errors (NULL, &errors);
6050 /* We first translate all modules to make sure that later parts
6051 of the program can use the decl. Then we translate the nonmodules. */
6053 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6055 if (!gfc_current_ns->proc_name
6056 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6057 continue;
6059 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
6060 gfc_derived_types = gfc_current_ns->derived_types;
6061 gfc_generate_module_code (gfc_current_ns);
6062 gfc_current_ns->translated = 1;
6065 gfc_current_ns = gfc_global_ns_list;
6066 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6068 if (gfc_current_ns->proc_name
6069 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
6070 continue;
6072 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
6073 gfc_derived_types = gfc_current_ns->derived_types;
6074 gfc_generate_code (gfc_current_ns);
6075 gfc_current_ns->translated = 1;
6078 /* Clean up all the namespaces after translation. */
6079 gfc_current_ns = gfc_global_ns_list;
6080 for (;gfc_current_ns;)
6082 gfc_namespace *ns;
6084 if (gfc_current_ns->proc_name
6085 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
6087 gfc_current_ns = gfc_current_ns->sibling;
6088 continue;
6091 ns = gfc_current_ns->sibling;
6092 gfc_derived_types = gfc_current_ns->derived_types;
6093 gfc_done_2 ();
6094 gfc_current_ns = ns;
6097 clean_up_modules (gfc_gsym_root);
6101 /* Top level parser. */
6103 bool
6104 gfc_parse_file (void)
6106 int seen_program, errors_before, errors;
6107 gfc_state_data top, s;
6108 gfc_statement st;
6109 locus prog_locus;
6110 gfc_namespace *next;
6112 gfc_start_source_files ();
6114 top.state = COMP_NONE;
6115 top.sym = NULL;
6116 top.previous = NULL;
6117 top.head = top.tail = NULL;
6118 top.do_variable = NULL;
6120 gfc_state_stack = &top;
6122 gfc_clear_new_st ();
6124 gfc_statement_label = NULL;
6126 if (setjmp (eof_buf))
6127 return false; /* Come here on unexpected EOF */
6129 /* Prepare the global namespace that will contain the
6130 program units. */
6131 gfc_global_ns_list = next = NULL;
6133 seen_program = 0;
6134 errors_before = 0;
6136 /* Exit early for empty files. */
6137 if (gfc_at_eof ())
6138 goto done;
6140 in_specification_block = true;
6141 loop:
6142 gfc_init_2 ();
6143 st = next_statement ();
6144 switch (st)
6146 case ST_NONE:
6147 gfc_done_2 ();
6148 goto done;
6150 case ST_PROGRAM:
6151 if (seen_program)
6152 goto duplicate_main;
6153 seen_program = 1;
6154 prog_locus = gfc_current_locus;
6156 push_state (&s, COMP_PROGRAM, gfc_new_block);
6157 main_program_symbol (gfc_current_ns, gfc_new_block->name);
6158 accept_statement (st);
6159 add_global_program ();
6160 parse_progunit (ST_NONE);
6161 goto prog_units;
6163 case ST_SUBROUTINE:
6164 add_global_procedure (true);
6165 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
6166 accept_statement (st);
6167 parse_progunit (ST_NONE);
6168 goto prog_units;
6170 case ST_FUNCTION:
6171 add_global_procedure (false);
6172 push_state (&s, COMP_FUNCTION, gfc_new_block);
6173 accept_statement (st);
6174 parse_progunit (ST_NONE);
6175 goto prog_units;
6177 case ST_BLOCK_DATA:
6178 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
6179 accept_statement (st);
6180 parse_block_data ();
6181 break;
6183 case ST_MODULE:
6184 push_state (&s, COMP_MODULE, gfc_new_block);
6185 accept_statement (st);
6187 gfc_get_errors (NULL, &errors_before);
6188 parse_module ();
6189 break;
6191 case ST_SUBMODULE:
6192 push_state (&s, COMP_SUBMODULE, gfc_new_block);
6193 accept_statement (st);
6195 gfc_get_errors (NULL, &errors_before);
6196 parse_module ();
6197 break;
6199 /* Anything else starts a nameless main program block. */
6200 default:
6201 if (seen_program)
6202 goto duplicate_main;
6203 seen_program = 1;
6204 prog_locus = gfc_current_locus;
6206 push_state (&s, COMP_PROGRAM, gfc_new_block);
6207 main_program_symbol (gfc_current_ns, "MAIN__");
6208 parse_progunit (st);
6209 goto prog_units;
6212 /* Handle the non-program units. */
6213 gfc_current_ns->code = s.head;
6215 gfc_resolve (gfc_current_ns);
6217 /* Dump the parse tree if requested. */
6218 if (flag_dump_fortran_original)
6219 gfc_dump_parse_tree (gfc_current_ns, stdout);
6221 gfc_get_errors (NULL, &errors);
6222 if (s.state == COMP_MODULE || s.state == COMP_SUBMODULE)
6224 gfc_dump_module (s.sym->name, errors_before == errors);
6225 gfc_current_ns->derived_types = gfc_derived_types;
6226 gfc_derived_types = NULL;
6227 goto prog_units;
6229 else
6231 if (errors == 0)
6232 gfc_generate_code (gfc_current_ns);
6233 pop_state ();
6234 gfc_done_2 ();
6237 goto loop;
6239 prog_units:
6240 /* The main program and non-contained procedures are put
6241 in the global namespace list, so that they can be processed
6242 later and all their interfaces resolved. */
6243 gfc_current_ns->code = s.head;
6244 if (next)
6246 for (; next->sibling; next = next->sibling)
6248 next->sibling = gfc_current_ns;
6250 else
6251 gfc_global_ns_list = gfc_current_ns;
6253 next = gfc_current_ns;
6255 pop_state ();
6256 goto loop;
6258 done:
6259 /* Do the resolution. */
6260 resolve_all_program_units (gfc_global_ns_list);
6262 /* Do the parse tree dump. */
6263 gfc_current_ns = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
6265 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6266 if (!gfc_current_ns->proc_name
6267 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6269 gfc_dump_parse_tree (gfc_current_ns, stdout);
6270 fputs ("------------------------------------------\n\n", stdout);
6273 /* Do the translation. */
6274 translate_all_program_units (gfc_global_ns_list);
6276 gfc_end_source_files ();
6277 return true;
6279 duplicate_main:
6280 /* If we see a duplicate main program, shut down. If the second
6281 instance is an implied main program, i.e. data decls or executable
6282 statements, we're in for lots of errors. */
6283 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
6284 reject_statement ();
6285 gfc_done_2 ();
6286 return true;
6289 /* Return true if this state data represents an OpenACC region. */
6290 bool
6291 is_oacc (gfc_state_data *sd)
6293 switch (sd->construct->op)
6295 case EXEC_OACC_PARALLEL_LOOP:
6296 case EXEC_OACC_PARALLEL:
6297 case EXEC_OACC_KERNELS_LOOP:
6298 case EXEC_OACC_KERNELS:
6299 case EXEC_OACC_DATA:
6300 case EXEC_OACC_HOST_DATA:
6301 case EXEC_OACC_LOOP:
6302 case EXEC_OACC_UPDATE:
6303 case EXEC_OACC_WAIT:
6304 case EXEC_OACC_CACHE:
6305 case EXEC_OACC_ENTER_DATA:
6306 case EXEC_OACC_EXIT_DATA:
6307 case EXEC_OACC_ATOMIC:
6308 case EXEC_OACC_ROUTINE:
6309 return true;
6311 default:
6312 return false;