Use gather loads for strided accesses
[official-gcc.git] / gcc / fortran / parse.c
blobd63d7bc989fa11ec2c1f632cd6b3e7a3673a50a1
1 /* Main parser.
2 Copyright (C) 2000-2018 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 matchs ("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 (gfc_match ("ordered depend (") == MATCH_YES)
934 gfc_current_locus = old_locus;
935 if (!flag_openmp)
936 break;
937 matcho ("ordered", gfc_match_omp_ordered_depend,
938 ST_OMP_ORDERED_DEPEND);
940 else
941 matchs ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
942 break;
943 case 'p':
944 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
945 ST_OMP_PARALLEL_DO_SIMD);
946 matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
947 matcho ("parallel sections", gfc_match_omp_parallel_sections,
948 ST_OMP_PARALLEL_SECTIONS);
949 matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
950 ST_OMP_PARALLEL_WORKSHARE);
951 matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
952 break;
953 case 's':
954 matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
955 matcho ("section", gfc_match_omp_eos, ST_OMP_SECTION);
956 matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
957 break;
958 case 't':
959 matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
960 matcho ("target enter data", gfc_match_omp_target_enter_data,
961 ST_OMP_TARGET_ENTER_DATA);
962 matcho ("target exit data", gfc_match_omp_target_exit_data,
963 ST_OMP_TARGET_EXIT_DATA);
964 matchs ("target parallel do simd", gfc_match_omp_target_parallel_do_simd,
965 ST_OMP_TARGET_PARALLEL_DO_SIMD);
966 matcho ("target parallel do", gfc_match_omp_target_parallel_do,
967 ST_OMP_TARGET_PARALLEL_DO);
968 matcho ("target parallel", gfc_match_omp_target_parallel,
969 ST_OMP_TARGET_PARALLEL);
970 matchs ("target simd", gfc_match_omp_target_simd, ST_OMP_TARGET_SIMD);
971 matchs ("target teams distribute parallel do simd",
972 gfc_match_omp_target_teams_distribute_parallel_do_simd,
973 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
974 matcho ("target teams distribute parallel do",
975 gfc_match_omp_target_teams_distribute_parallel_do,
976 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
977 matchs ("target teams distribute simd",
978 gfc_match_omp_target_teams_distribute_simd,
979 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
980 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
981 ST_OMP_TARGET_TEAMS_DISTRIBUTE);
982 matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
983 matcho ("target update", gfc_match_omp_target_update,
984 ST_OMP_TARGET_UPDATE);
985 matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
986 matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
987 matchs ("taskloop simd", gfc_match_omp_taskloop_simd,
988 ST_OMP_TASKLOOP_SIMD);
989 matcho ("taskloop", gfc_match_omp_taskloop, ST_OMP_TASKLOOP);
990 matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
991 matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
992 matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
993 matchs ("teams distribute parallel do simd",
994 gfc_match_omp_teams_distribute_parallel_do_simd,
995 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
996 matcho ("teams distribute parallel do",
997 gfc_match_omp_teams_distribute_parallel_do,
998 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
999 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
1000 ST_OMP_TEAMS_DISTRIBUTE_SIMD);
1001 matcho ("teams distribute", gfc_match_omp_teams_distribute,
1002 ST_OMP_TEAMS_DISTRIBUTE);
1003 matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
1004 matchdo ("threadprivate", gfc_match_omp_threadprivate,
1005 ST_OMP_THREADPRIVATE);
1006 break;
1007 case 'w':
1008 matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
1009 break;
1012 /* All else has failed, so give up. See if any of the matchers has
1013 stored an error message of some sort. Don't error out if
1014 not -fopenmp and simd_matched is false, i.e. if a directive other
1015 than one marked with match has been seen. */
1017 if (flag_openmp || simd_matched)
1019 if (!gfc_error_check ())
1020 gfc_error_now ("Unclassifiable OpenMP directive at %C");
1023 reject_statement ();
1025 gfc_error_recovery ();
1027 return ST_NONE;
1029 finish:
1030 if (!pure_ok)
1032 gfc_unset_implicit_pure (NULL);
1034 if (!flag_openmp && gfc_pure (NULL))
1036 gfc_error_now ("OpenMP directives other than SIMD or DECLARE TARGET "
1037 "at %C may not appear in PURE or ELEMENTAL "
1038 "procedures");
1039 reject_statement ();
1040 gfc_error_recovery ();
1041 return ST_NONE;
1044 return ret;
1046 do_spec_only:
1047 reject_statement ();
1048 gfc_clear_error ();
1049 gfc_buffer_error (false);
1050 gfc_current_locus = old_locus;
1051 return ST_GET_FCN_CHARACTERISTICS;
1054 static gfc_statement
1055 decode_gcc_attribute (void)
1057 locus old_locus;
1059 gfc_enforce_clean_symbol_state ();
1061 gfc_clear_error (); /* Clear any pending errors. */
1062 gfc_clear_warning (); /* Clear any pending warnings. */
1063 old_locus = gfc_current_locus;
1065 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
1066 match ("unroll", gfc_match_gcc_unroll, ST_NONE);
1068 /* All else has failed, so give up. See if any of the matchers has
1069 stored an error message of some sort. */
1071 if (!gfc_error_check ())
1072 gfc_error_now ("Unclassifiable GCC directive at %C");
1074 reject_statement ();
1076 gfc_error_recovery ();
1078 return ST_NONE;
1081 #undef match
1083 /* Assert next length characters to be equal to token in free form. */
1085 static void
1086 verify_token_free (const char* token, int length, bool last_was_use_stmt)
1088 int i;
1089 char c;
1091 c = gfc_next_ascii_char ();
1092 for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
1093 gcc_assert (c == token[i]);
1095 gcc_assert (gfc_is_whitespace(c));
1096 gfc_gobble_whitespace ();
1097 if (last_was_use_stmt)
1098 use_modules ();
1101 /* Get the next statement in free form source. */
1103 static gfc_statement
1104 next_free (void)
1106 match m;
1107 int i, cnt, at_bol;
1108 char c;
1110 at_bol = gfc_at_bol ();
1111 gfc_gobble_whitespace ();
1113 c = gfc_peek_ascii_char ();
1115 if (ISDIGIT (c))
1117 char d;
1119 /* Found a statement label? */
1120 m = gfc_match_st_label (&gfc_statement_label);
1122 d = gfc_peek_ascii_char ();
1123 if (m != MATCH_YES || !gfc_is_whitespace (d))
1125 gfc_match_small_literal_int (&i, &cnt);
1127 if (cnt > 5)
1128 gfc_error_now ("Too many digits in statement label at %C");
1130 if (i == 0)
1131 gfc_error_now ("Zero is not a valid statement label at %C");
1134 c = gfc_next_ascii_char ();
1135 while (ISDIGIT(c));
1137 if (!gfc_is_whitespace (c))
1138 gfc_error_now ("Non-numeric character in statement label at %C");
1140 return ST_NONE;
1142 else
1144 label_locus = gfc_current_locus;
1146 gfc_gobble_whitespace ();
1148 if (at_bol && gfc_peek_ascii_char () == ';')
1150 gfc_error_now ("Semicolon at %C needs to be preceded by "
1151 "statement");
1152 gfc_next_ascii_char (); /* Eat up the semicolon. */
1153 return ST_NONE;
1156 if (gfc_match_eos () == MATCH_YES)
1157 gfc_error_now ("Statement label without statement at %L",
1158 &label_locus);
1161 else if (c == '!')
1163 /* Comments have already been skipped by the time we get here,
1164 except for GCC attributes and OpenMP/OpenACC directives. */
1166 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
1167 c = gfc_peek_ascii_char ();
1169 if (c == 'g')
1171 int i;
1173 c = gfc_next_ascii_char ();
1174 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
1175 gcc_assert (c == "gcc$"[i]);
1177 gfc_gobble_whitespace ();
1178 return decode_gcc_attribute ();
1181 else if (c == '$')
1183 /* Since both OpenMP and OpenACC directives starts with
1184 !$ character sequence, we must check all flags combinations */
1185 if ((flag_openmp || flag_openmp_simd)
1186 && !flag_openacc)
1188 verify_token_free ("$omp", 4, last_was_use_stmt);
1189 return decode_omp_directive ();
1191 else if ((flag_openmp || flag_openmp_simd)
1192 && flag_openacc)
1194 gfc_next_ascii_char (); /* Eat up dollar character */
1195 c = gfc_peek_ascii_char ();
1197 if (c == 'o')
1199 verify_token_free ("omp", 3, last_was_use_stmt);
1200 return decode_omp_directive ();
1202 else if (c == 'a')
1204 verify_token_free ("acc", 3, last_was_use_stmt);
1205 return decode_oacc_directive ();
1208 else if (flag_openacc)
1210 verify_token_free ("$acc", 4, last_was_use_stmt);
1211 return decode_oacc_directive ();
1214 gcc_unreachable ();
1217 if (at_bol && c == ';')
1219 if (!(gfc_option.allow_std & GFC_STD_F2008))
1220 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1221 "statement");
1222 gfc_next_ascii_char (); /* Eat up the semicolon. */
1223 return ST_NONE;
1226 return decode_statement ();
1229 /* Assert next length characters to be equal to token in fixed form. */
1231 static bool
1232 verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
1234 int i;
1235 char c = gfc_next_char_literal (NONSTRING);
1237 for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
1238 gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
1240 if (c != ' ' && c != '0')
1242 gfc_buffer_error (false);
1243 gfc_error ("Bad continuation line at %C");
1244 return false;
1246 if (last_was_use_stmt)
1247 use_modules ();
1249 return true;
1252 /* Get the next statement in fixed-form source. */
1254 static gfc_statement
1255 next_fixed (void)
1257 int label, digit_flag, i;
1258 locus loc;
1259 gfc_char_t c;
1261 if (!gfc_at_bol ())
1262 return decode_statement ();
1264 /* Skip past the current label field, parsing a statement label if
1265 one is there. This is a weird number parser, since the number is
1266 contained within five columns and can have any kind of embedded
1267 spaces. We also check for characters that make the rest of the
1268 line a comment. */
1270 label = 0;
1271 digit_flag = 0;
1273 for (i = 0; i < 5; i++)
1275 c = gfc_next_char_literal (NONSTRING);
1277 switch (c)
1279 case ' ':
1280 break;
1282 case '0':
1283 case '1':
1284 case '2':
1285 case '3':
1286 case '4':
1287 case '5':
1288 case '6':
1289 case '7':
1290 case '8':
1291 case '9':
1292 label = label * 10 + ((unsigned char) c - '0');
1293 label_locus = gfc_current_locus;
1294 digit_flag = 1;
1295 break;
1297 /* Comments have already been skipped by the time we get
1298 here, except for GCC attributes and OpenMP directives. */
1300 case '*':
1301 c = gfc_next_char_literal (NONSTRING);
1303 if (TOLOWER (c) == 'g')
1305 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
1306 gcc_assert (TOLOWER (c) == "gcc$"[i]);
1308 return decode_gcc_attribute ();
1310 else if (c == '$')
1312 if ((flag_openmp || flag_openmp_simd)
1313 && !flag_openacc)
1315 if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
1316 return ST_NONE;
1317 return decode_omp_directive ();
1319 else if ((flag_openmp || flag_openmp_simd)
1320 && flag_openacc)
1322 c = gfc_next_char_literal(NONSTRING);
1323 if (c == 'o' || c == 'O')
1325 if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
1326 return ST_NONE;
1327 return decode_omp_directive ();
1329 else if (c == 'a' || c == 'A')
1331 if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
1332 return ST_NONE;
1333 return decode_oacc_directive ();
1336 else if (flag_openacc)
1338 if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
1339 return ST_NONE;
1340 return decode_oacc_directive ();
1343 gcc_fallthrough ();
1345 /* Comments have already been skipped by the time we get
1346 here so don't bother checking for them. */
1348 default:
1349 gfc_buffer_error (false);
1350 gfc_error ("Non-numeric character in statement label at %C");
1351 return ST_NONE;
1355 if (digit_flag)
1357 if (label == 0)
1358 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1359 else
1361 /* We've found a valid statement label. */
1362 gfc_statement_label = gfc_get_st_label (label);
1366 /* Since this line starts a statement, it cannot be a continuation
1367 of a previous statement. If we see something here besides a
1368 space or zero, it must be a bad continuation line. */
1370 c = gfc_next_char_literal (NONSTRING);
1371 if (c == '\n')
1372 goto blank_line;
1374 if (c != ' ' && c != '0')
1376 gfc_buffer_error (false);
1377 gfc_error ("Bad continuation line at %C");
1378 return ST_NONE;
1381 /* Now that we've taken care of the statement label columns, we have
1382 to make sure that the first nonblank character is not a '!'. If
1383 it is, the rest of the line is a comment. */
1387 loc = gfc_current_locus;
1388 c = gfc_next_char_literal (NONSTRING);
1390 while (gfc_is_whitespace (c));
1392 if (c == '!')
1393 goto blank_line;
1394 gfc_current_locus = loc;
1396 if (c == ';')
1398 if (digit_flag)
1399 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1400 else if (!(gfc_option.allow_std & GFC_STD_F2008))
1401 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1402 "statement");
1403 return ST_NONE;
1406 if (gfc_match_eos () == MATCH_YES)
1407 goto blank_line;
1409 /* At this point, we've got a nonblank statement to parse. */
1410 return decode_statement ();
1412 blank_line:
1413 if (digit_flag)
1414 gfc_error_now ("Statement label without statement at %L", &label_locus);
1416 gfc_current_locus.lb->truncated = 0;
1417 gfc_advance_line ();
1418 return ST_NONE;
1422 /* Return the next non-ST_NONE statement to the caller. We also worry
1423 about including files and the ends of include files at this stage. */
1425 static gfc_statement
1426 next_statement (void)
1428 gfc_statement st;
1429 locus old_locus;
1431 gfc_enforce_clean_symbol_state ();
1433 gfc_new_block = NULL;
1435 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
1436 gfc_current_ns->old_data = gfc_current_ns->data;
1437 for (;;)
1439 gfc_statement_label = NULL;
1440 gfc_buffer_error (true);
1442 if (gfc_at_eol ())
1443 gfc_advance_line ();
1445 gfc_skip_comments ();
1447 if (gfc_at_end ())
1449 st = ST_NONE;
1450 break;
1453 if (gfc_define_undef_line ())
1454 continue;
1456 old_locus = gfc_current_locus;
1458 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
1460 if (st != ST_NONE)
1461 break;
1464 gfc_buffer_error (false);
1466 if (st == ST_GET_FCN_CHARACTERISTICS)
1468 if (gfc_statement_label != NULL)
1470 gfc_free_st_label (gfc_statement_label);
1471 gfc_statement_label = NULL;
1473 gfc_current_locus = old_locus;
1476 if (st != ST_NONE)
1477 check_statement_label (st);
1479 return st;
1483 /****************************** Parser ***********************************/
1485 /* The parser subroutines are of type 'try' that fail if the file ends
1486 unexpectedly. */
1488 /* Macros that expand to case-labels for various classes of
1489 statements. Start with executable statements that directly do
1490 things. */
1492 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1493 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1494 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1495 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1496 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1497 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1498 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1499 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1500 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1501 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1502 case ST_OMP_TARGET_UPDATE: case ST_OMP_TARGET_ENTER_DATA: \
1503 case ST_OMP_TARGET_EXIT_DATA: case ST_OMP_ORDERED_DEPEND: \
1504 case ST_ERROR_STOP: case ST_SYNC_ALL: \
1505 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1506 case ST_EVENT_POST: case ST_EVENT_WAIT: case ST_FAIL_IMAGE: \
1507 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1508 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1510 /* Statements that mark other executable statements. */
1512 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1513 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1514 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1515 case ST_OMP_PARALLEL: \
1516 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1517 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1518 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1519 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1520 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1521 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1522 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1523 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1524 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1525 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1526 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1527 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1528 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1529 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1530 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1531 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1532 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_TARGET_PARALLEL: \
1533 case ST_OMP_TARGET_PARALLEL_DO: case ST_OMP_TARGET_PARALLEL_DO_SIMD: \
1534 case ST_OMP_TARGET_SIMD: case ST_OMP_TASKLOOP: case ST_OMP_TASKLOOP_SIMD: \
1535 case ST_CRITICAL: \
1536 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1537 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: \
1538 case ST_OACC_KERNELS_LOOP: case ST_OACC_ATOMIC
1540 /* Declaration statements */
1542 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1543 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1544 case ST_TYPE: case ST_INTERFACE: case ST_PROCEDURE: case ST_OACC_ROUTINE: \
1545 case ST_OACC_DECLARE
1547 /* OpenMP declaration statements. */
1549 #define case_omp_decl case ST_OMP_THREADPRIVATE: case ST_OMP_DECLARE_SIMD: \
1550 case ST_OMP_DECLARE_TARGET: case ST_OMP_DECLARE_REDUCTION
1552 /* Block end statements. Errors associated with interchanging these
1553 are detected in gfc_match_end(). */
1555 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1556 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1557 case ST_END_BLOCK: case ST_END_ASSOCIATE
1560 /* Push a new state onto the stack. */
1562 static void
1563 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
1565 p->state = new_state;
1566 p->previous = gfc_state_stack;
1567 p->sym = sym;
1568 p->head = p->tail = NULL;
1569 p->do_variable = NULL;
1570 if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
1571 p->ext.oacc_declare_clauses = NULL;
1573 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1574 construct statement was accepted right before pushing the state. Thus,
1575 the construct's gfc_code is available as tail of the parent state. */
1576 gcc_assert (gfc_state_stack);
1577 p->construct = gfc_state_stack->tail;
1579 gfc_state_stack = p;
1583 /* Pop the current state. */
1584 static void
1585 pop_state (void)
1587 gfc_state_stack = gfc_state_stack->previous;
1591 /* Try to find the given state in the state stack. */
1593 bool
1594 gfc_find_state (gfc_compile_state state)
1596 gfc_state_data *p;
1598 for (p = gfc_state_stack; p; p = p->previous)
1599 if (p->state == state)
1600 break;
1602 return (p == NULL) ? false : true;
1606 /* Starts a new level in the statement list. */
1608 static gfc_code *
1609 new_level (gfc_code *q)
1611 gfc_code *p;
1613 p = q->block = gfc_get_code (EXEC_NOP);
1615 gfc_state_stack->head = gfc_state_stack->tail = p;
1617 return p;
1621 /* Add the current new_st code structure and adds it to the current
1622 program unit. As a side-effect, it zeroes the new_st. */
1624 static gfc_code *
1625 add_statement (void)
1627 gfc_code *p;
1629 p = XCNEW (gfc_code);
1630 *p = new_st;
1632 p->loc = gfc_current_locus;
1634 if (gfc_state_stack->head == NULL)
1635 gfc_state_stack->head = p;
1636 else
1637 gfc_state_stack->tail->next = p;
1639 while (p->next != NULL)
1640 p = p->next;
1642 gfc_state_stack->tail = p;
1644 gfc_clear_new_st ();
1646 return p;
1650 /* Frees everything associated with the current statement. */
1652 static void
1653 undo_new_statement (void)
1655 gfc_free_statements (new_st.block);
1656 gfc_free_statements (new_st.next);
1657 gfc_free_statement (&new_st);
1658 gfc_clear_new_st ();
1662 /* If the current statement has a statement label, make sure that it
1663 is allowed to, or should have one. */
1665 static void
1666 check_statement_label (gfc_statement st)
1668 gfc_sl_type type;
1670 if (gfc_statement_label == NULL)
1672 if (st == ST_FORMAT)
1673 gfc_error ("FORMAT statement at %L does not have a statement label",
1674 &new_st.loc);
1675 return;
1678 switch (st)
1680 case ST_END_PROGRAM:
1681 case ST_END_FUNCTION:
1682 case ST_END_SUBROUTINE:
1683 case ST_ENDDO:
1684 case ST_ENDIF:
1685 case ST_END_SELECT:
1686 case ST_END_CRITICAL:
1687 case ST_END_BLOCK:
1688 case ST_END_ASSOCIATE:
1689 case_executable:
1690 case_exec_markers:
1691 if (st == ST_ENDDO || st == ST_CONTINUE)
1692 type = ST_LABEL_DO_TARGET;
1693 else
1694 type = ST_LABEL_TARGET;
1695 break;
1697 case ST_FORMAT:
1698 type = ST_LABEL_FORMAT;
1699 break;
1701 /* Statement labels are not restricted from appearing on a
1702 particular line. However, there are plenty of situations
1703 where the resulting label can't be referenced. */
1705 default:
1706 type = ST_LABEL_BAD_TARGET;
1707 break;
1710 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1712 new_st.here = gfc_statement_label;
1716 /* Figures out what the enclosing program unit is. This will be a
1717 function, subroutine, program, block data or module. */
1719 gfc_state_data *
1720 gfc_enclosing_unit (gfc_compile_state * result)
1722 gfc_state_data *p;
1724 for (p = gfc_state_stack; p; p = p->previous)
1725 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1726 || p->state == COMP_MODULE || p->state == COMP_SUBMODULE
1727 || p->state == COMP_BLOCK_DATA || p->state == COMP_PROGRAM)
1730 if (result != NULL)
1731 *result = p->state;
1732 return p;
1735 if (result != NULL)
1736 *result = COMP_PROGRAM;
1737 return NULL;
1741 /* Translate a statement enum to a string. */
1743 const char *
1744 gfc_ascii_statement (gfc_statement st)
1746 const char *p;
1748 switch (st)
1750 case ST_ARITHMETIC_IF:
1751 p = _("arithmetic IF");
1752 break;
1753 case ST_ALLOCATE:
1754 p = "ALLOCATE";
1755 break;
1756 case ST_ASSOCIATE:
1757 p = "ASSOCIATE";
1758 break;
1759 case ST_ATTR_DECL:
1760 p = _("attribute declaration");
1761 break;
1762 case ST_BACKSPACE:
1763 p = "BACKSPACE";
1764 break;
1765 case ST_BLOCK:
1766 p = "BLOCK";
1767 break;
1768 case ST_BLOCK_DATA:
1769 p = "BLOCK DATA";
1770 break;
1771 case ST_CALL:
1772 p = "CALL";
1773 break;
1774 case ST_CASE:
1775 p = "CASE";
1776 break;
1777 case ST_CLOSE:
1778 p = "CLOSE";
1779 break;
1780 case ST_COMMON:
1781 p = "COMMON";
1782 break;
1783 case ST_CONTINUE:
1784 p = "CONTINUE";
1785 break;
1786 case ST_CONTAINS:
1787 p = "CONTAINS";
1788 break;
1789 case ST_CRITICAL:
1790 p = "CRITICAL";
1791 break;
1792 case ST_CYCLE:
1793 p = "CYCLE";
1794 break;
1795 case ST_DATA_DECL:
1796 p = _("data declaration");
1797 break;
1798 case ST_DATA:
1799 p = "DATA";
1800 break;
1801 case ST_DEALLOCATE:
1802 p = "DEALLOCATE";
1803 break;
1804 case ST_MAP:
1805 p = "MAP";
1806 break;
1807 case ST_UNION:
1808 p = "UNION";
1809 break;
1810 case ST_STRUCTURE_DECL:
1811 p = "STRUCTURE";
1812 break;
1813 case ST_DERIVED_DECL:
1814 p = _("derived type declaration");
1815 break;
1816 case ST_DO:
1817 p = "DO";
1818 break;
1819 case ST_ELSE:
1820 p = "ELSE";
1821 break;
1822 case ST_ELSEIF:
1823 p = "ELSE IF";
1824 break;
1825 case ST_ELSEWHERE:
1826 p = "ELSEWHERE";
1827 break;
1828 case ST_EVENT_POST:
1829 p = "EVENT POST";
1830 break;
1831 case ST_EVENT_WAIT:
1832 p = "EVENT WAIT";
1833 break;
1834 case ST_FAIL_IMAGE:
1835 p = "FAIL IMAGE";
1836 break;
1837 case ST_END_ASSOCIATE:
1838 p = "END ASSOCIATE";
1839 break;
1840 case ST_END_BLOCK:
1841 p = "END BLOCK";
1842 break;
1843 case ST_END_BLOCK_DATA:
1844 p = "END BLOCK DATA";
1845 break;
1846 case ST_END_CRITICAL:
1847 p = "END CRITICAL";
1848 break;
1849 case ST_ENDDO:
1850 p = "END DO";
1851 break;
1852 case ST_END_FILE:
1853 p = "END FILE";
1854 break;
1855 case ST_END_FORALL:
1856 p = "END FORALL";
1857 break;
1858 case ST_END_FUNCTION:
1859 p = "END FUNCTION";
1860 break;
1861 case ST_ENDIF:
1862 p = "END IF";
1863 break;
1864 case ST_END_INTERFACE:
1865 p = "END INTERFACE";
1866 break;
1867 case ST_END_MODULE:
1868 p = "END MODULE";
1869 break;
1870 case ST_END_SUBMODULE:
1871 p = "END SUBMODULE";
1872 break;
1873 case ST_END_PROGRAM:
1874 p = "END PROGRAM";
1875 break;
1876 case ST_END_SELECT:
1877 p = "END SELECT";
1878 break;
1879 case ST_END_SUBROUTINE:
1880 p = "END SUBROUTINE";
1881 break;
1882 case ST_END_WHERE:
1883 p = "END WHERE";
1884 break;
1885 case ST_END_STRUCTURE:
1886 p = "END STRUCTURE";
1887 break;
1888 case ST_END_UNION:
1889 p = "END UNION";
1890 break;
1891 case ST_END_MAP:
1892 p = "END MAP";
1893 break;
1894 case ST_END_TYPE:
1895 p = "END TYPE";
1896 break;
1897 case ST_ENTRY:
1898 p = "ENTRY";
1899 break;
1900 case ST_EQUIVALENCE:
1901 p = "EQUIVALENCE";
1902 break;
1903 case ST_ERROR_STOP:
1904 p = "ERROR STOP";
1905 break;
1906 case ST_EXIT:
1907 p = "EXIT";
1908 break;
1909 case ST_FLUSH:
1910 p = "FLUSH";
1911 break;
1912 case ST_FORALL_BLOCK: /* Fall through */
1913 case ST_FORALL:
1914 p = "FORALL";
1915 break;
1916 case ST_FORMAT:
1917 p = "FORMAT";
1918 break;
1919 case ST_FUNCTION:
1920 p = "FUNCTION";
1921 break;
1922 case ST_GENERIC:
1923 p = "GENERIC";
1924 break;
1925 case ST_GOTO:
1926 p = "GOTO";
1927 break;
1928 case ST_IF_BLOCK:
1929 p = _("block IF");
1930 break;
1931 case ST_IMPLICIT:
1932 p = "IMPLICIT";
1933 break;
1934 case ST_IMPLICIT_NONE:
1935 p = "IMPLICIT NONE";
1936 break;
1937 case ST_IMPLIED_ENDDO:
1938 p = _("implied END DO");
1939 break;
1940 case ST_IMPORT:
1941 p = "IMPORT";
1942 break;
1943 case ST_INQUIRE:
1944 p = "INQUIRE";
1945 break;
1946 case ST_INTERFACE:
1947 p = "INTERFACE";
1948 break;
1949 case ST_LOCK:
1950 p = "LOCK";
1951 break;
1952 case ST_PARAMETER:
1953 p = "PARAMETER";
1954 break;
1955 case ST_PRIVATE:
1956 p = "PRIVATE";
1957 break;
1958 case ST_PUBLIC:
1959 p = "PUBLIC";
1960 break;
1961 case ST_MODULE:
1962 p = "MODULE";
1963 break;
1964 case ST_SUBMODULE:
1965 p = "SUBMODULE";
1966 break;
1967 case ST_PAUSE:
1968 p = "PAUSE";
1969 break;
1970 case ST_MODULE_PROC:
1971 p = "MODULE PROCEDURE";
1972 break;
1973 case ST_NAMELIST:
1974 p = "NAMELIST";
1975 break;
1976 case ST_NULLIFY:
1977 p = "NULLIFY";
1978 break;
1979 case ST_OPEN:
1980 p = "OPEN";
1981 break;
1982 case ST_PROGRAM:
1983 p = "PROGRAM";
1984 break;
1985 case ST_PROCEDURE:
1986 p = "PROCEDURE";
1987 break;
1988 case ST_READ:
1989 p = "READ";
1990 break;
1991 case ST_RETURN:
1992 p = "RETURN";
1993 break;
1994 case ST_REWIND:
1995 p = "REWIND";
1996 break;
1997 case ST_STOP:
1998 p = "STOP";
1999 break;
2000 case ST_SYNC_ALL:
2001 p = "SYNC ALL";
2002 break;
2003 case ST_SYNC_IMAGES:
2004 p = "SYNC IMAGES";
2005 break;
2006 case ST_SYNC_MEMORY:
2007 p = "SYNC MEMORY";
2008 break;
2009 case ST_SUBROUTINE:
2010 p = "SUBROUTINE";
2011 break;
2012 case ST_TYPE:
2013 p = "TYPE";
2014 break;
2015 case ST_UNLOCK:
2016 p = "UNLOCK";
2017 break;
2018 case ST_USE:
2019 p = "USE";
2020 break;
2021 case ST_WHERE_BLOCK: /* Fall through */
2022 case ST_WHERE:
2023 p = "WHERE";
2024 break;
2025 case ST_WAIT:
2026 p = "WAIT";
2027 break;
2028 case ST_WRITE:
2029 p = "WRITE";
2030 break;
2031 case ST_ASSIGNMENT:
2032 p = _("assignment");
2033 break;
2034 case ST_POINTER_ASSIGNMENT:
2035 p = _("pointer assignment");
2036 break;
2037 case ST_SELECT_CASE:
2038 p = "SELECT CASE";
2039 break;
2040 case ST_SELECT_TYPE:
2041 p = "SELECT TYPE";
2042 break;
2043 case ST_TYPE_IS:
2044 p = "TYPE IS";
2045 break;
2046 case ST_CLASS_IS:
2047 p = "CLASS IS";
2048 break;
2049 case ST_SEQUENCE:
2050 p = "SEQUENCE";
2051 break;
2052 case ST_SIMPLE_IF:
2053 p = _("simple IF");
2054 break;
2055 case ST_STATEMENT_FUNCTION:
2056 p = "STATEMENT FUNCTION";
2057 break;
2058 case ST_LABEL_ASSIGNMENT:
2059 p = "LABEL ASSIGNMENT";
2060 break;
2061 case ST_ENUM:
2062 p = "ENUM DEFINITION";
2063 break;
2064 case ST_ENUMERATOR:
2065 p = "ENUMERATOR DEFINITION";
2066 break;
2067 case ST_END_ENUM:
2068 p = "END ENUM";
2069 break;
2070 case ST_OACC_PARALLEL_LOOP:
2071 p = "!$ACC PARALLEL LOOP";
2072 break;
2073 case ST_OACC_END_PARALLEL_LOOP:
2074 p = "!$ACC END PARALLEL LOOP";
2075 break;
2076 case ST_OACC_PARALLEL:
2077 p = "!$ACC PARALLEL";
2078 break;
2079 case ST_OACC_END_PARALLEL:
2080 p = "!$ACC END PARALLEL";
2081 break;
2082 case ST_OACC_KERNELS:
2083 p = "!$ACC KERNELS";
2084 break;
2085 case ST_OACC_END_KERNELS:
2086 p = "!$ACC END KERNELS";
2087 break;
2088 case ST_OACC_KERNELS_LOOP:
2089 p = "!$ACC KERNELS LOOP";
2090 break;
2091 case ST_OACC_END_KERNELS_LOOP:
2092 p = "!$ACC END KERNELS LOOP";
2093 break;
2094 case ST_OACC_DATA:
2095 p = "!$ACC DATA";
2096 break;
2097 case ST_OACC_END_DATA:
2098 p = "!$ACC END DATA";
2099 break;
2100 case ST_OACC_HOST_DATA:
2101 p = "!$ACC HOST_DATA";
2102 break;
2103 case ST_OACC_END_HOST_DATA:
2104 p = "!$ACC END HOST_DATA";
2105 break;
2106 case ST_OACC_LOOP:
2107 p = "!$ACC LOOP";
2108 break;
2109 case ST_OACC_END_LOOP:
2110 p = "!$ACC END LOOP";
2111 break;
2112 case ST_OACC_DECLARE:
2113 p = "!$ACC DECLARE";
2114 break;
2115 case ST_OACC_UPDATE:
2116 p = "!$ACC UPDATE";
2117 break;
2118 case ST_OACC_WAIT:
2119 p = "!$ACC WAIT";
2120 break;
2121 case ST_OACC_CACHE:
2122 p = "!$ACC CACHE";
2123 break;
2124 case ST_OACC_ENTER_DATA:
2125 p = "!$ACC ENTER DATA";
2126 break;
2127 case ST_OACC_EXIT_DATA:
2128 p = "!$ACC EXIT DATA";
2129 break;
2130 case ST_OACC_ROUTINE:
2131 p = "!$ACC ROUTINE";
2132 break;
2133 case ST_OACC_ATOMIC:
2134 p = "!$ACC ATOMIC";
2135 break;
2136 case ST_OACC_END_ATOMIC:
2137 p = "!$ACC END ATOMIC";
2138 break;
2139 case ST_OMP_ATOMIC:
2140 p = "!$OMP ATOMIC";
2141 break;
2142 case ST_OMP_BARRIER:
2143 p = "!$OMP BARRIER";
2144 break;
2145 case ST_OMP_CANCEL:
2146 p = "!$OMP CANCEL";
2147 break;
2148 case ST_OMP_CANCELLATION_POINT:
2149 p = "!$OMP CANCELLATION POINT";
2150 break;
2151 case ST_OMP_CRITICAL:
2152 p = "!$OMP CRITICAL";
2153 break;
2154 case ST_OMP_DECLARE_REDUCTION:
2155 p = "!$OMP DECLARE REDUCTION";
2156 break;
2157 case ST_OMP_DECLARE_SIMD:
2158 p = "!$OMP DECLARE SIMD";
2159 break;
2160 case ST_OMP_DECLARE_TARGET:
2161 p = "!$OMP DECLARE TARGET";
2162 break;
2163 case ST_OMP_DISTRIBUTE:
2164 p = "!$OMP DISTRIBUTE";
2165 break;
2166 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
2167 p = "!$OMP DISTRIBUTE PARALLEL DO";
2168 break;
2169 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
2170 p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
2171 break;
2172 case ST_OMP_DISTRIBUTE_SIMD:
2173 p = "!$OMP DISTRIBUTE SIMD";
2174 break;
2175 case ST_OMP_DO:
2176 p = "!$OMP DO";
2177 break;
2178 case ST_OMP_DO_SIMD:
2179 p = "!$OMP DO SIMD";
2180 break;
2181 case ST_OMP_END_ATOMIC:
2182 p = "!$OMP END ATOMIC";
2183 break;
2184 case ST_OMP_END_CRITICAL:
2185 p = "!$OMP END CRITICAL";
2186 break;
2187 case ST_OMP_END_DISTRIBUTE:
2188 p = "!$OMP END DISTRIBUTE";
2189 break;
2190 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
2191 p = "!$OMP END DISTRIBUTE PARALLEL DO";
2192 break;
2193 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
2194 p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
2195 break;
2196 case ST_OMP_END_DISTRIBUTE_SIMD:
2197 p = "!$OMP END DISTRIBUTE SIMD";
2198 break;
2199 case ST_OMP_END_DO:
2200 p = "!$OMP END DO";
2201 break;
2202 case ST_OMP_END_DO_SIMD:
2203 p = "!$OMP END DO SIMD";
2204 break;
2205 case ST_OMP_END_SIMD:
2206 p = "!$OMP END SIMD";
2207 break;
2208 case ST_OMP_END_MASTER:
2209 p = "!$OMP END MASTER";
2210 break;
2211 case ST_OMP_END_ORDERED:
2212 p = "!$OMP END ORDERED";
2213 break;
2214 case ST_OMP_END_PARALLEL:
2215 p = "!$OMP END PARALLEL";
2216 break;
2217 case ST_OMP_END_PARALLEL_DO:
2218 p = "!$OMP END PARALLEL DO";
2219 break;
2220 case ST_OMP_END_PARALLEL_DO_SIMD:
2221 p = "!$OMP END PARALLEL DO SIMD";
2222 break;
2223 case ST_OMP_END_PARALLEL_SECTIONS:
2224 p = "!$OMP END PARALLEL SECTIONS";
2225 break;
2226 case ST_OMP_END_PARALLEL_WORKSHARE:
2227 p = "!$OMP END PARALLEL WORKSHARE";
2228 break;
2229 case ST_OMP_END_SECTIONS:
2230 p = "!$OMP END SECTIONS";
2231 break;
2232 case ST_OMP_END_SINGLE:
2233 p = "!$OMP END SINGLE";
2234 break;
2235 case ST_OMP_END_TASK:
2236 p = "!$OMP END TASK";
2237 break;
2238 case ST_OMP_END_TARGET:
2239 p = "!$OMP END TARGET";
2240 break;
2241 case ST_OMP_END_TARGET_DATA:
2242 p = "!$OMP END TARGET DATA";
2243 break;
2244 case ST_OMP_END_TARGET_PARALLEL:
2245 p = "!$OMP END TARGET PARALLEL";
2246 break;
2247 case ST_OMP_END_TARGET_PARALLEL_DO:
2248 p = "!$OMP END TARGET PARALLEL DO";
2249 break;
2250 case ST_OMP_END_TARGET_PARALLEL_DO_SIMD:
2251 p = "!$OMP END TARGET PARALLEL DO SIMD";
2252 break;
2253 case ST_OMP_END_TARGET_SIMD:
2254 p = "!$OMP END TARGET SIMD";
2255 break;
2256 case ST_OMP_END_TARGET_TEAMS:
2257 p = "!$OMP END TARGET TEAMS";
2258 break;
2259 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
2260 p = "!$OMP END TARGET TEAMS DISTRIBUTE";
2261 break;
2262 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2263 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2264 break;
2265 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2266 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2267 break;
2268 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
2269 p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2270 break;
2271 case ST_OMP_END_TASKGROUP:
2272 p = "!$OMP END TASKGROUP";
2273 break;
2274 case ST_OMP_END_TASKLOOP:
2275 p = "!$OMP END TASKLOOP";
2276 break;
2277 case ST_OMP_END_TASKLOOP_SIMD:
2278 p = "!$OMP END TASKLOOP SIMD";
2279 break;
2280 case ST_OMP_END_TEAMS:
2281 p = "!$OMP END TEAMS";
2282 break;
2283 case ST_OMP_END_TEAMS_DISTRIBUTE:
2284 p = "!$OMP END TEAMS DISTRIBUTE";
2285 break;
2286 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
2287 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2288 break;
2289 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2290 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2291 break;
2292 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
2293 p = "!$OMP END TEAMS DISTRIBUTE SIMD";
2294 break;
2295 case ST_OMP_END_WORKSHARE:
2296 p = "!$OMP END WORKSHARE";
2297 break;
2298 case ST_OMP_FLUSH:
2299 p = "!$OMP FLUSH";
2300 break;
2301 case ST_OMP_MASTER:
2302 p = "!$OMP MASTER";
2303 break;
2304 case ST_OMP_ORDERED:
2305 case ST_OMP_ORDERED_DEPEND:
2306 p = "!$OMP ORDERED";
2307 break;
2308 case ST_OMP_PARALLEL:
2309 p = "!$OMP PARALLEL";
2310 break;
2311 case ST_OMP_PARALLEL_DO:
2312 p = "!$OMP PARALLEL DO";
2313 break;
2314 case ST_OMP_PARALLEL_DO_SIMD:
2315 p = "!$OMP PARALLEL DO SIMD";
2316 break;
2317 case ST_OMP_PARALLEL_SECTIONS:
2318 p = "!$OMP PARALLEL SECTIONS";
2319 break;
2320 case ST_OMP_PARALLEL_WORKSHARE:
2321 p = "!$OMP PARALLEL WORKSHARE";
2322 break;
2323 case ST_OMP_SECTIONS:
2324 p = "!$OMP SECTIONS";
2325 break;
2326 case ST_OMP_SECTION:
2327 p = "!$OMP SECTION";
2328 break;
2329 case ST_OMP_SIMD:
2330 p = "!$OMP SIMD";
2331 break;
2332 case ST_OMP_SINGLE:
2333 p = "!$OMP SINGLE";
2334 break;
2335 case ST_OMP_TARGET:
2336 p = "!$OMP TARGET";
2337 break;
2338 case ST_OMP_TARGET_DATA:
2339 p = "!$OMP TARGET DATA";
2340 break;
2341 case ST_OMP_TARGET_ENTER_DATA:
2342 p = "!$OMP TARGET ENTER DATA";
2343 break;
2344 case ST_OMP_TARGET_EXIT_DATA:
2345 p = "!$OMP TARGET EXIT DATA";
2346 break;
2347 case ST_OMP_TARGET_PARALLEL:
2348 p = "!$OMP TARGET PARALLEL";
2349 break;
2350 case ST_OMP_TARGET_PARALLEL_DO:
2351 p = "!$OMP TARGET PARALLEL DO";
2352 break;
2353 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
2354 p = "!$OMP TARGET PARALLEL DO SIMD";
2355 break;
2356 case ST_OMP_TARGET_SIMD:
2357 p = "!$OMP TARGET SIMD";
2358 break;
2359 case ST_OMP_TARGET_TEAMS:
2360 p = "!$OMP TARGET TEAMS";
2361 break;
2362 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
2363 p = "!$OMP TARGET TEAMS DISTRIBUTE";
2364 break;
2365 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2366 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2367 break;
2368 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2369 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2370 break;
2371 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2372 p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2373 break;
2374 case ST_OMP_TARGET_UPDATE:
2375 p = "!$OMP TARGET UPDATE";
2376 break;
2377 case ST_OMP_TASK:
2378 p = "!$OMP TASK";
2379 break;
2380 case ST_OMP_TASKGROUP:
2381 p = "!$OMP TASKGROUP";
2382 break;
2383 case ST_OMP_TASKLOOP:
2384 p = "!$OMP TASKLOOP";
2385 break;
2386 case ST_OMP_TASKLOOP_SIMD:
2387 p = "!$OMP TASKLOOP SIMD";
2388 break;
2389 case ST_OMP_TASKWAIT:
2390 p = "!$OMP TASKWAIT";
2391 break;
2392 case ST_OMP_TASKYIELD:
2393 p = "!$OMP TASKYIELD";
2394 break;
2395 case ST_OMP_TEAMS:
2396 p = "!$OMP TEAMS";
2397 break;
2398 case ST_OMP_TEAMS_DISTRIBUTE:
2399 p = "!$OMP TEAMS DISTRIBUTE";
2400 break;
2401 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2402 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2403 break;
2404 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2405 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2406 break;
2407 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
2408 p = "!$OMP TEAMS DISTRIBUTE SIMD";
2409 break;
2410 case ST_OMP_THREADPRIVATE:
2411 p = "!$OMP THREADPRIVATE";
2412 break;
2413 case ST_OMP_WORKSHARE:
2414 p = "!$OMP WORKSHARE";
2415 break;
2416 default:
2417 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2420 return p;
2424 /* Create a symbol for the main program and assign it to ns->proc_name. */
2426 static void
2427 main_program_symbol (gfc_namespace *ns, const char *name)
2429 gfc_symbol *main_program;
2430 symbol_attribute attr;
2432 gfc_get_symbol (name, ns, &main_program);
2433 gfc_clear_attr (&attr);
2434 attr.flavor = FL_PROGRAM;
2435 attr.proc = PROC_UNKNOWN;
2436 attr.subroutine = 1;
2437 attr.access = ACCESS_PUBLIC;
2438 attr.is_main_program = 1;
2439 main_program->attr = attr;
2440 main_program->declared_at = gfc_current_locus;
2441 ns->proc_name = main_program;
2442 gfc_commit_symbols ();
2446 /* Do whatever is necessary to accept the last statement. */
2448 static void
2449 accept_statement (gfc_statement st)
2451 switch (st)
2453 case ST_IMPLICIT_NONE:
2454 case ST_IMPLICIT:
2455 break;
2457 case ST_FUNCTION:
2458 case ST_SUBROUTINE:
2459 case ST_MODULE:
2460 case ST_SUBMODULE:
2461 gfc_current_ns->proc_name = gfc_new_block;
2462 break;
2464 /* If the statement is the end of a block, lay down a special code
2465 that allows a branch to the end of the block from within the
2466 construct. IF and SELECT are treated differently from DO
2467 (where EXEC_NOP is added inside the loop) for two
2468 reasons:
2469 1. END DO has a meaning in the sense that after a GOTO to
2470 it, the loop counter must be increased.
2471 2. IF blocks and SELECT blocks can consist of multiple
2472 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2473 Putting the label before the END IF would make the jump
2474 from, say, the ELSE IF block to the END IF illegal. */
2476 case ST_ENDIF:
2477 case ST_END_SELECT:
2478 case ST_END_CRITICAL:
2479 if (gfc_statement_label != NULL)
2481 new_st.op = EXEC_END_NESTED_BLOCK;
2482 add_statement ();
2484 break;
2486 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2487 one parallel block. Thus, we add the special code to the nested block
2488 itself, instead of the parent one. */
2489 case ST_END_BLOCK:
2490 case ST_END_ASSOCIATE:
2491 if (gfc_statement_label != NULL)
2493 new_st.op = EXEC_END_BLOCK;
2494 add_statement ();
2496 break;
2498 /* The end-of-program unit statements do not get the special
2499 marker and require a statement of some sort if they are a
2500 branch target. */
2502 case ST_END_PROGRAM:
2503 case ST_END_FUNCTION:
2504 case ST_END_SUBROUTINE:
2505 if (gfc_statement_label != NULL)
2507 new_st.op = EXEC_RETURN;
2508 add_statement ();
2510 else
2512 new_st.op = EXEC_END_PROCEDURE;
2513 add_statement ();
2516 break;
2518 case ST_ENTRY:
2519 case_executable:
2520 case_exec_markers:
2521 add_statement ();
2522 break;
2524 default:
2525 break;
2528 gfc_commit_symbols ();
2529 gfc_warning_check ();
2530 gfc_clear_new_st ();
2534 /* Undo anything tentative that has been built for the current statement,
2535 except if a gfc_charlen structure has been added to current namespace's
2536 list of gfc_charlen structure. */
2538 static void
2539 reject_statement (void)
2541 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
2542 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
2544 gfc_reject_data (gfc_current_ns);
2546 gfc_new_block = NULL;
2547 gfc_undo_symbols ();
2548 gfc_clear_warning ();
2549 undo_new_statement ();
2553 /* Generic complaint about an out of order statement. We also do
2554 whatever is necessary to clean up. */
2556 static void
2557 unexpected_statement (gfc_statement st)
2559 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
2561 reject_statement ();
2565 /* Given the next statement seen by the matcher, make sure that it is
2566 in proper order with the last. This subroutine is initialized by
2567 calling it with an argument of ST_NONE. If there is a problem, we
2568 issue an error and return false. Otherwise we return true.
2570 Individual parsers need to verify that the statements seen are
2571 valid before calling here, i.e., ENTRY statements are not allowed in
2572 INTERFACE blocks. The following diagram is taken from the standard:
2574 +---------------------------------------+
2575 | program subroutine function module |
2576 +---------------------------------------+
2577 | use |
2578 +---------------------------------------+
2579 | import |
2580 +---------------------------------------+
2581 | | implicit none |
2582 | +-----------+------------------+
2583 | | parameter | implicit |
2584 | +-----------+------------------+
2585 | format | | derived type |
2586 | entry | parameter | interface |
2587 | | data | specification |
2588 | | | statement func |
2589 | +-----------+------------------+
2590 | | data | executable |
2591 +--------+-----------+------------------+
2592 | contains |
2593 +---------------------------------------+
2594 | internal module/subprogram |
2595 +---------------------------------------+
2596 | end |
2597 +---------------------------------------+
2601 enum state_order
2603 ORDER_START,
2604 ORDER_USE,
2605 ORDER_IMPORT,
2606 ORDER_IMPLICIT_NONE,
2607 ORDER_IMPLICIT,
2608 ORDER_SPEC,
2609 ORDER_EXEC
2612 typedef struct
2614 enum state_order state;
2615 gfc_statement last_statement;
2616 locus where;
2618 st_state;
2620 static bool
2621 verify_st_order (st_state *p, gfc_statement st, bool silent)
2624 switch (st)
2626 case ST_NONE:
2627 p->state = ORDER_START;
2628 break;
2630 case ST_USE:
2631 if (p->state > ORDER_USE)
2632 goto order;
2633 p->state = ORDER_USE;
2634 break;
2636 case ST_IMPORT:
2637 if (p->state > ORDER_IMPORT)
2638 goto order;
2639 p->state = ORDER_IMPORT;
2640 break;
2642 case ST_IMPLICIT_NONE:
2643 if (p->state > ORDER_IMPLICIT)
2644 goto order;
2646 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2647 statement disqualifies a USE but not an IMPLICIT NONE.
2648 Duplicate IMPLICIT NONEs are caught when the implicit types
2649 are set. */
2651 p->state = ORDER_IMPLICIT_NONE;
2652 break;
2654 case ST_IMPLICIT:
2655 if (p->state > ORDER_IMPLICIT)
2656 goto order;
2657 p->state = ORDER_IMPLICIT;
2658 break;
2660 case ST_FORMAT:
2661 case ST_ENTRY:
2662 if (p->state < ORDER_IMPLICIT_NONE)
2663 p->state = ORDER_IMPLICIT_NONE;
2664 break;
2666 case ST_PARAMETER:
2667 if (p->state >= ORDER_EXEC)
2668 goto order;
2669 if (p->state < ORDER_IMPLICIT)
2670 p->state = ORDER_IMPLICIT;
2671 break;
2673 case ST_DATA:
2674 if (p->state < ORDER_SPEC)
2675 p->state = ORDER_SPEC;
2676 break;
2678 case ST_PUBLIC:
2679 case ST_PRIVATE:
2680 case ST_STRUCTURE_DECL:
2681 case ST_DERIVED_DECL:
2682 case_decl:
2683 if (p->state >= ORDER_EXEC)
2684 goto order;
2685 if (p->state < ORDER_SPEC)
2686 p->state = ORDER_SPEC;
2687 break;
2689 case_omp_decl:
2690 /* The OpenMP directives have to be somewhere in the specification
2691 part, but there are no further requirements on their ordering.
2692 Thus don't adjust p->state, just ignore them. */
2693 if (p->state >= ORDER_EXEC)
2694 goto order;
2695 break;
2697 case_executable:
2698 case_exec_markers:
2699 if (p->state < ORDER_EXEC)
2700 p->state = ORDER_EXEC;
2701 break;
2703 default:
2704 return false;
2707 /* All is well, record the statement in case we need it next time. */
2708 p->where = gfc_current_locus;
2709 p->last_statement = st;
2710 return true;
2712 order:
2713 if (!silent)
2714 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2715 gfc_ascii_statement (st),
2716 gfc_ascii_statement (p->last_statement), &p->where);
2718 return false;
2722 /* Handle an unexpected end of file. This is a show-stopper... */
2724 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
2726 static void
2727 unexpected_eof (void)
2729 gfc_state_data *p;
2731 gfc_error ("Unexpected end of file in %qs", gfc_source_file);
2733 /* Memory cleanup. Move to "second to last". */
2734 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
2735 p = p->previous);
2737 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
2738 gfc_done_2 ();
2740 longjmp (eof_buf, 1);
2742 /* Avoids build error on systems where longjmp is not declared noreturn. */
2743 gcc_unreachable ();
2747 /* Parse the CONTAINS section of a derived type definition. */
2749 gfc_access gfc_typebound_default_access;
2751 static bool
2752 parse_derived_contains (void)
2754 gfc_state_data s;
2755 bool seen_private = false;
2756 bool seen_comps = false;
2757 bool error_flag = false;
2758 bool to_finish;
2760 gcc_assert (gfc_current_state () == COMP_DERIVED);
2761 gcc_assert (gfc_current_block ());
2763 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2764 section. */
2765 if (gfc_current_block ()->attr.sequence)
2766 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2767 " section at %C", gfc_current_block ()->name);
2768 if (gfc_current_block ()->attr.is_bind_c)
2769 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2770 " section at %C", gfc_current_block ()->name);
2772 accept_statement (ST_CONTAINS);
2773 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
2775 gfc_typebound_default_access = ACCESS_PUBLIC;
2777 to_finish = false;
2778 while (!to_finish)
2780 gfc_statement st;
2781 st = next_statement ();
2782 switch (st)
2784 case ST_NONE:
2785 unexpected_eof ();
2786 break;
2788 case ST_DATA_DECL:
2789 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2790 goto error;
2792 case ST_PROCEDURE:
2793 if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
2794 goto error;
2796 accept_statement (ST_PROCEDURE);
2797 seen_comps = true;
2798 break;
2800 case ST_GENERIC:
2801 if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
2802 goto error;
2804 accept_statement (ST_GENERIC);
2805 seen_comps = true;
2806 break;
2808 case ST_FINAL:
2809 if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
2810 " at %C"))
2811 goto error;
2813 accept_statement (ST_FINAL);
2814 seen_comps = true;
2815 break;
2817 case ST_END_TYPE:
2818 to_finish = true;
2820 if (!seen_comps
2821 && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
2822 "at %C with empty CONTAINS section")))
2823 goto error;
2825 /* ST_END_TYPE is accepted by parse_derived after return. */
2826 break;
2828 case ST_PRIVATE:
2829 if (!gfc_find_state (COMP_MODULE))
2831 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2832 "a MODULE");
2833 goto error;
2836 if (seen_comps)
2838 gfc_error ("PRIVATE statement at %C must precede procedure"
2839 " bindings");
2840 goto error;
2843 if (seen_private)
2845 gfc_error ("Duplicate PRIVATE statement at %C");
2846 goto error;
2849 accept_statement (ST_PRIVATE);
2850 gfc_typebound_default_access = ACCESS_PRIVATE;
2851 seen_private = true;
2852 break;
2854 case ST_SEQUENCE:
2855 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2856 goto error;
2858 case ST_CONTAINS:
2859 gfc_error ("Already inside a CONTAINS block at %C");
2860 goto error;
2862 default:
2863 unexpected_statement (st);
2864 break;
2867 continue;
2869 error:
2870 error_flag = true;
2871 reject_statement ();
2874 pop_state ();
2875 gcc_assert (gfc_current_state () == COMP_DERIVED);
2877 return error_flag;
2881 /* Set attributes for the parent symbol based on the attributes of a component
2882 and raise errors if conflicting attributes are found for the component. */
2884 static void
2885 check_component (gfc_symbol *sym, gfc_component *c, gfc_component **lockp,
2886 gfc_component **eventp)
2888 bool coarray, lock_type, event_type, allocatable, pointer;
2889 coarray = lock_type = event_type = allocatable = pointer = false;
2890 gfc_component *lock_comp = NULL, *event_comp = NULL;
2892 if (lockp) lock_comp = *lockp;
2893 if (eventp) event_comp = *eventp;
2895 /* Look for allocatable components. */
2896 if (c->attr.allocatable
2897 || (c->ts.type == BT_CLASS && c->attr.class_ok
2898 && CLASS_DATA (c)->attr.allocatable)
2899 || (c->ts.type == BT_DERIVED && !c->attr.pointer
2900 && c->ts.u.derived->attr.alloc_comp))
2902 allocatable = true;
2903 sym->attr.alloc_comp = 1;
2906 /* Look for pointer components. */
2907 if (c->attr.pointer
2908 || (c->ts.type == BT_CLASS && c->attr.class_ok
2909 && CLASS_DATA (c)->attr.class_pointer)
2910 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2912 pointer = true;
2913 sym->attr.pointer_comp = 1;
2916 /* Look for procedure pointer components. */
2917 if (c->attr.proc_pointer
2918 || (c->ts.type == BT_DERIVED
2919 && c->ts.u.derived->attr.proc_pointer_comp))
2920 sym->attr.proc_pointer_comp = 1;
2922 /* Looking for coarray components. */
2923 if (c->attr.codimension
2924 || (c->ts.type == BT_CLASS && c->attr.class_ok
2925 && CLASS_DATA (c)->attr.codimension))
2927 coarray = true;
2928 sym->attr.coarray_comp = 1;
2931 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
2932 && !c->attr.pointer)
2934 coarray = true;
2935 sym->attr.coarray_comp = 1;
2938 /* Looking for lock_type components. */
2939 if ((c->ts.type == BT_DERIVED
2940 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2941 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2942 || (c->ts.type == BT_CLASS && c->attr.class_ok
2943 && CLASS_DATA (c)->ts.u.derived->from_intmod
2944 == INTMOD_ISO_FORTRAN_ENV
2945 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2946 == ISOFORTRAN_LOCK_TYPE)
2947 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
2948 && !allocatable && !pointer))
2950 lock_type = 1;
2951 lock_comp = c;
2952 sym->attr.lock_comp = 1;
2955 /* Looking for event_type components. */
2956 if ((c->ts.type == BT_DERIVED
2957 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2958 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2959 || (c->ts.type == BT_CLASS && c->attr.class_ok
2960 && CLASS_DATA (c)->ts.u.derived->from_intmod
2961 == INTMOD_ISO_FORTRAN_ENV
2962 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2963 == ISOFORTRAN_EVENT_TYPE)
2964 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.event_comp
2965 && !allocatable && !pointer))
2967 event_type = 1;
2968 event_comp = c;
2969 sym->attr.event_comp = 1;
2972 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2973 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2974 unless there are nondirect [allocatable or pointer] components
2975 involved (cf. 1.3.33.1 and 1.3.33.3). */
2977 if (pointer && !coarray && lock_type)
2978 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2979 "codimension or be a subcomponent of a coarray, "
2980 "which is not possible as the component has the "
2981 "pointer attribute", c->name, &c->loc);
2982 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2983 && c->ts.u.derived->attr.lock_comp)
2984 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2985 "of type LOCK_TYPE, which must have a codimension or be a "
2986 "subcomponent of a coarray", c->name, &c->loc);
2988 if (lock_type && allocatable && !coarray)
2989 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2990 "a codimension", c->name, &c->loc);
2991 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
2992 && c->ts.u.derived->attr.lock_comp)
2993 gfc_error ("Allocatable component %s at %L must have a codimension as "
2994 "it has a noncoarray subcomponent of type LOCK_TYPE",
2995 c->name, &c->loc);
2997 if (sym->attr.coarray_comp && !coarray && lock_type)
2998 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2999 "subcomponent of type LOCK_TYPE must have a codimension or "
3000 "be a subcomponent of a coarray. (Variables of type %s may "
3001 "not have a codimension as already a coarray "
3002 "subcomponent exists)", c->name, &c->loc, sym->name);
3004 if (sym->attr.lock_comp && coarray && !lock_type)
3005 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
3006 "subcomponent of type LOCK_TYPE must have a codimension or "
3007 "be a subcomponent of a coarray. (Variables of type %s may "
3008 "not have a codimension as %s at %L has a codimension or a "
3009 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
3010 sym->name, c->name, &c->loc);
3012 /* Similarly for EVENT TYPE. */
3014 if (pointer && !coarray && event_type)
3015 gfc_error ("Component %s at %L of type EVENT_TYPE must have a "
3016 "codimension or be a subcomponent of a coarray, "
3017 "which is not possible as the component has the "
3018 "pointer attribute", c->name, &c->loc);
3019 else if (pointer && !coarray && c->ts.type == BT_DERIVED
3020 && c->ts.u.derived->attr.event_comp)
3021 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
3022 "of type EVENT_TYPE, which must have a codimension or be a "
3023 "subcomponent of a coarray", c->name, &c->loc);
3025 if (event_type && allocatable && !coarray)
3026 gfc_error ("Allocatable component %s at %L of type EVENT_TYPE must have "
3027 "a codimension", c->name, &c->loc);
3028 else if (event_type && allocatable && c->ts.type == BT_DERIVED
3029 && c->ts.u.derived->attr.event_comp)
3030 gfc_error ("Allocatable component %s at %L must have a codimension as "
3031 "it has a noncoarray subcomponent of type EVENT_TYPE",
3032 c->name, &c->loc);
3034 if (sym->attr.coarray_comp && !coarray && event_type)
3035 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
3036 "subcomponent of type EVENT_TYPE must have a codimension or "
3037 "be a subcomponent of a coarray. (Variables of type %s may "
3038 "not have a codimension as already a coarray "
3039 "subcomponent exists)", c->name, &c->loc, sym->name);
3041 if (sym->attr.event_comp && coarray && !event_type)
3042 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
3043 "subcomponent of type EVENT_TYPE must have a codimension or "
3044 "be a subcomponent of a coarray. (Variables of type %s may "
3045 "not have a codimension as %s at %L has a codimension or a "
3046 "coarray subcomponent)", event_comp->name, &event_comp->loc,
3047 sym->name, c->name, &c->loc);
3049 /* Look for private components. */
3050 if (sym->component_access == ACCESS_PRIVATE
3051 || c->attr.access == ACCESS_PRIVATE
3052 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
3053 sym->attr.private_comp = 1;
3055 if (lockp) *lockp = lock_comp;
3056 if (eventp) *eventp = event_comp;
3060 static void parse_struct_map (gfc_statement);
3062 /* Parse a union component definition within a structure definition. */
3064 static void
3065 parse_union (void)
3067 int compiling;
3068 gfc_statement st;
3069 gfc_state_data s;
3070 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3071 gfc_symbol *un;
3073 accept_statement(ST_UNION);
3074 push_state (&s, COMP_UNION, gfc_new_block);
3075 un = gfc_new_block;
3077 compiling = 1;
3079 while (compiling)
3081 st = next_statement ();
3082 /* Only MAP declarations valid within a union. */
3083 switch (st)
3085 case ST_NONE:
3086 unexpected_eof ();
3088 case ST_MAP:
3089 accept_statement (ST_MAP);
3090 parse_struct_map (ST_MAP);
3091 /* Add a component to the union for each map. */
3092 if (!gfc_add_component (un, gfc_new_block->name, &c))
3094 gfc_internal_error ("failed to create map component '%s'",
3095 gfc_new_block->name);
3096 reject_statement ();
3097 return;
3099 c->ts.type = BT_DERIVED;
3100 c->ts.u.derived = gfc_new_block;
3101 /* Normally components get their initialization expressions when they
3102 are created in decl.c (build_struct) so we can look through the
3103 flat component list for initializers during resolution. Unions and
3104 maps create components along with their type definitions so we
3105 have to generate initializers here. */
3106 c->initializer = gfc_default_initializer (&c->ts);
3107 break;
3109 case ST_END_UNION:
3110 compiling = 0;
3111 accept_statement (ST_END_UNION);
3112 break;
3114 default:
3115 unexpected_statement (st);
3116 break;
3120 for (c = un->components; c; c = c->next)
3121 check_component (un, c, &lock_comp, &event_comp);
3123 /* Add the union as a component in its parent structure. */
3124 pop_state ();
3125 if (!gfc_add_component (gfc_current_block (), un->name, &c))
3127 gfc_internal_error ("failed to create union component '%s'", un->name);
3128 reject_statement ();
3129 return;
3131 c->ts.type = BT_UNION;
3132 c->ts.u.derived = un;
3133 c->initializer = gfc_default_initializer (&c->ts);
3135 un->attr.zero_comp = un->components == NULL;
3139 /* Parse a STRUCTURE or MAP. */
3141 static void
3142 parse_struct_map (gfc_statement block)
3144 int compiling_type;
3145 gfc_statement st;
3146 gfc_state_data s;
3147 gfc_symbol *sym;
3148 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3149 gfc_compile_state comp;
3150 gfc_statement ends;
3152 if (block == ST_STRUCTURE_DECL)
3154 comp = COMP_STRUCTURE;
3155 ends = ST_END_STRUCTURE;
3157 else
3159 gcc_assert (block == ST_MAP);
3160 comp = COMP_MAP;
3161 ends = ST_END_MAP;
3164 accept_statement(block);
3165 push_state (&s, comp, gfc_new_block);
3167 gfc_new_block->component_access = ACCESS_PUBLIC;
3168 compiling_type = 1;
3170 while (compiling_type)
3172 st = next_statement ();
3173 switch (st)
3175 case ST_NONE:
3176 unexpected_eof ();
3178 /* Nested structure declarations will be captured as ST_DATA_DECL. */
3179 case ST_STRUCTURE_DECL:
3180 /* Let a more specific error make it to decode_statement(). */
3181 if (gfc_error_check () == 0)
3182 gfc_error ("Syntax error in nested structure declaration at %C");
3183 reject_statement ();
3184 /* Skip the rest of this statement. */
3185 gfc_error_recovery ();
3186 break;
3188 case ST_UNION:
3189 accept_statement (ST_UNION);
3190 parse_union ();
3191 break;
3193 case ST_DATA_DECL:
3194 /* The data declaration was a nested/ad-hoc STRUCTURE field. */
3195 accept_statement (ST_DATA_DECL);
3196 if (gfc_new_block && gfc_new_block != gfc_current_block ()
3197 && gfc_new_block->attr.flavor == FL_STRUCT)
3198 parse_struct_map (ST_STRUCTURE_DECL);
3199 break;
3201 case ST_END_STRUCTURE:
3202 case ST_END_MAP:
3203 if (st == ends)
3205 accept_statement (st);
3206 compiling_type = 0;
3208 else
3209 unexpected_statement (st);
3210 break;
3212 default:
3213 unexpected_statement (st);
3214 break;
3218 /* Validate each component. */
3219 sym = gfc_current_block ();
3220 for (c = sym->components; c; c = c->next)
3221 check_component (sym, c, &lock_comp, &event_comp);
3223 sym->attr.zero_comp = (sym->components == NULL);
3225 /* Allow parse_union to find this structure to add to its list of maps. */
3226 if (block == ST_MAP)
3227 gfc_new_block = gfc_current_block ();
3229 pop_state ();
3233 /* Parse a derived type. */
3235 static void
3236 parse_derived (void)
3238 int compiling_type, seen_private, seen_sequence, seen_component;
3239 gfc_statement st;
3240 gfc_state_data s;
3241 gfc_symbol *sym;
3242 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3244 accept_statement (ST_DERIVED_DECL);
3245 push_state (&s, COMP_DERIVED, gfc_new_block);
3247 gfc_new_block->component_access = ACCESS_PUBLIC;
3248 seen_private = 0;
3249 seen_sequence = 0;
3250 seen_component = 0;
3252 compiling_type = 1;
3254 while (compiling_type)
3256 st = next_statement ();
3257 switch (st)
3259 case ST_NONE:
3260 unexpected_eof ();
3262 case ST_DATA_DECL:
3263 case ST_PROCEDURE:
3264 accept_statement (st);
3265 seen_component = 1;
3266 break;
3268 case ST_FINAL:
3269 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
3270 break;
3272 case ST_END_TYPE:
3273 endType:
3274 compiling_type = 0;
3276 if (!seen_component)
3277 gfc_notify_std (GFC_STD_F2003, "Derived type "
3278 "definition at %C without components");
3280 accept_statement (ST_END_TYPE);
3281 break;
3283 case ST_PRIVATE:
3284 if (!gfc_find_state (COMP_MODULE))
3286 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
3287 "a MODULE");
3288 break;
3291 if (seen_component)
3293 gfc_error ("PRIVATE statement at %C must precede "
3294 "structure components");
3295 break;
3298 if (seen_private)
3299 gfc_error ("Duplicate PRIVATE statement at %C");
3301 s.sym->component_access = ACCESS_PRIVATE;
3303 accept_statement (ST_PRIVATE);
3304 seen_private = 1;
3305 break;
3307 case ST_SEQUENCE:
3308 if (seen_component)
3310 gfc_error ("SEQUENCE statement at %C must precede "
3311 "structure components");
3312 break;
3315 if (gfc_current_block ()->attr.sequence)
3316 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
3317 "TYPE statement");
3319 if (seen_sequence)
3321 gfc_error ("Duplicate SEQUENCE statement at %C");
3324 seen_sequence = 1;
3325 gfc_add_sequence (&gfc_current_block ()->attr,
3326 gfc_current_block ()->name, NULL);
3327 break;
3329 case ST_CONTAINS:
3330 gfc_notify_std (GFC_STD_F2003,
3331 "CONTAINS block in derived type"
3332 " definition at %C");
3334 accept_statement (ST_CONTAINS);
3335 parse_derived_contains ();
3336 goto endType;
3338 default:
3339 unexpected_statement (st);
3340 break;
3344 /* need to verify that all fields of the derived type are
3345 * interoperable with C if the type is declared to be bind(c)
3347 sym = gfc_current_block ();
3348 for (c = sym->components; c; c = c->next)
3349 check_component (sym, c, &lock_comp, &event_comp);
3351 if (!seen_component)
3352 sym->attr.zero_comp = 1;
3354 pop_state ();
3358 /* Parse an ENUM. */
3360 static void
3361 parse_enum (void)
3363 gfc_statement st;
3364 int compiling_enum;
3365 gfc_state_data s;
3366 int seen_enumerator = 0;
3368 push_state (&s, COMP_ENUM, gfc_new_block);
3370 compiling_enum = 1;
3372 while (compiling_enum)
3374 st = next_statement ();
3375 switch (st)
3377 case ST_NONE:
3378 unexpected_eof ();
3379 break;
3381 case ST_ENUMERATOR:
3382 seen_enumerator = 1;
3383 accept_statement (st);
3384 break;
3386 case ST_END_ENUM:
3387 compiling_enum = 0;
3388 if (!seen_enumerator)
3389 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
3390 accept_statement (st);
3391 break;
3393 default:
3394 gfc_free_enum_history ();
3395 unexpected_statement (st);
3396 break;
3399 pop_state ();
3403 /* Parse an interface. We must be able to deal with the possibility
3404 of recursive interfaces. The parse_spec() subroutine is mutually
3405 recursive with parse_interface(). */
3407 static gfc_statement parse_spec (gfc_statement);
3409 static void
3410 parse_interface (void)
3412 gfc_compile_state new_state = COMP_NONE, current_state;
3413 gfc_symbol *prog_unit, *sym;
3414 gfc_interface_info save;
3415 gfc_state_data s1, s2;
3416 gfc_statement st;
3418 accept_statement (ST_INTERFACE);
3420 current_interface.ns = gfc_current_ns;
3421 save = current_interface;
3423 sym = (current_interface.type == INTERFACE_GENERIC
3424 || current_interface.type == INTERFACE_USER_OP)
3425 ? gfc_new_block : NULL;
3427 push_state (&s1, COMP_INTERFACE, sym);
3428 current_state = COMP_NONE;
3430 loop:
3431 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
3433 st = next_statement ();
3434 switch (st)
3436 case ST_NONE:
3437 unexpected_eof ();
3439 case ST_SUBROUTINE:
3440 case ST_FUNCTION:
3441 if (st == ST_SUBROUTINE)
3442 new_state = COMP_SUBROUTINE;
3443 else if (st == ST_FUNCTION)
3444 new_state = COMP_FUNCTION;
3445 if (gfc_new_block->attr.pointer)
3447 gfc_new_block->attr.pointer = 0;
3448 gfc_new_block->attr.proc_pointer = 1;
3450 if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
3451 gfc_new_block->formal, NULL))
3453 reject_statement ();
3454 gfc_free_namespace (gfc_current_ns);
3455 goto loop;
3457 /* F2008 C1210 forbids the IMPORT statement in module procedure
3458 interface bodies and the flag is set to import symbols. */
3459 if (gfc_new_block->attr.module_procedure)
3460 gfc_current_ns->has_import_set = 1;
3461 break;
3463 case ST_PROCEDURE:
3464 case ST_MODULE_PROC: /* The module procedure matcher makes
3465 sure the context is correct. */
3466 accept_statement (st);
3467 gfc_free_namespace (gfc_current_ns);
3468 goto loop;
3470 case ST_END_INTERFACE:
3471 gfc_free_namespace (gfc_current_ns);
3472 gfc_current_ns = current_interface.ns;
3473 goto done;
3475 default:
3476 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
3477 gfc_ascii_statement (st));
3478 reject_statement ();
3479 gfc_free_namespace (gfc_current_ns);
3480 goto loop;
3484 /* Make sure that the generic name has the right attribute. */
3485 if (current_interface.type == INTERFACE_GENERIC
3486 && current_state == COMP_NONE)
3488 if (new_state == COMP_FUNCTION && sym)
3489 gfc_add_function (&sym->attr, sym->name, NULL);
3490 else if (new_state == COMP_SUBROUTINE && sym)
3491 gfc_add_subroutine (&sym->attr, sym->name, NULL);
3493 current_state = new_state;
3496 if (current_interface.type == INTERFACE_ABSTRACT)
3498 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
3499 if (gfc_is_intrinsic_typename (gfc_new_block->name))
3500 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
3501 "cannot be the same as an intrinsic type",
3502 gfc_new_block->name);
3505 push_state (&s2, new_state, gfc_new_block);
3506 accept_statement (st);
3507 prog_unit = gfc_new_block;
3508 prog_unit->formal_ns = gfc_current_ns;
3509 if (prog_unit == prog_unit->formal_ns->proc_name
3510 && prog_unit->ns != prog_unit->formal_ns)
3511 prog_unit->refs++;
3513 decl:
3514 /* Read data declaration statements. */
3515 st = parse_spec (ST_NONE);
3516 in_specification_block = true;
3518 /* Since the interface block does not permit an IMPLICIT statement,
3519 the default type for the function or the result must be taken
3520 from the formal namespace. */
3521 if (new_state == COMP_FUNCTION)
3523 if (prog_unit->result == prog_unit
3524 && prog_unit->ts.type == BT_UNKNOWN)
3525 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
3526 else if (prog_unit->result != prog_unit
3527 && prog_unit->result->ts.type == BT_UNKNOWN)
3528 gfc_set_default_type (prog_unit->result, 1,
3529 prog_unit->formal_ns);
3532 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
3534 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3535 gfc_ascii_statement (st));
3536 reject_statement ();
3537 goto decl;
3540 /* Add EXTERNAL attribute to function or subroutine. */
3541 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
3542 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
3544 current_interface = save;
3545 gfc_add_interface (prog_unit);
3546 pop_state ();
3548 if (current_interface.ns
3549 && current_interface.ns->proc_name
3550 && strcmp (current_interface.ns->proc_name->name,
3551 prog_unit->name) == 0)
3552 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3553 "enclosing procedure", prog_unit->name,
3554 &current_interface.ns->proc_name->declared_at);
3556 goto loop;
3558 done:
3559 pop_state ();
3563 /* Associate function characteristics by going back to the function
3564 declaration and rematching the prefix. */
3566 static match
3567 match_deferred_characteristics (gfc_typespec * ts)
3569 locus loc;
3570 match m = MATCH_ERROR;
3571 char name[GFC_MAX_SYMBOL_LEN + 1];
3573 loc = gfc_current_locus;
3575 gfc_current_locus = gfc_current_block ()->declared_at;
3577 gfc_clear_error ();
3578 gfc_buffer_error (true);
3579 m = gfc_match_prefix (ts);
3580 gfc_buffer_error (false);
3582 if (ts->type == BT_DERIVED)
3584 ts->kind = 0;
3586 if (!ts->u.derived)
3587 m = MATCH_ERROR;
3590 /* Only permit one go at the characteristic association. */
3591 if (ts->kind == -1)
3592 ts->kind = 0;
3594 /* Set the function locus correctly. If we have not found the
3595 function name, there is an error. */
3596 if (m == MATCH_YES
3597 && gfc_match ("function% %n", name) == MATCH_YES
3598 && strcmp (name, gfc_current_block ()->name) == 0)
3600 gfc_current_block ()->declared_at = gfc_current_locus;
3601 gfc_commit_symbols ();
3603 else
3605 gfc_error_check ();
3606 gfc_undo_symbols ();
3609 gfc_current_locus =loc;
3610 return m;
3614 /* Check specification-expressions in the function result of the currently
3615 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3616 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3617 scope are not yet parsed so this has to be delayed up to parse_spec. */
3619 static void
3620 check_function_result_typed (void)
3622 gfc_typespec ts;
3624 gcc_assert (gfc_current_state () == COMP_FUNCTION);
3626 if (!gfc_current_ns->proc_name->result) return;
3628 ts = gfc_current_ns->proc_name->result->ts;
3630 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3631 /* TODO: Extend when KIND type parameters are implemented. */
3632 if (ts.type == BT_CHARACTER && ts.u.cl && ts.u.cl->length)
3633 gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true);
3637 /* Parse a set of specification statements. Returns the statement
3638 that doesn't fit. */
3640 static gfc_statement
3641 parse_spec (gfc_statement st)
3643 st_state ss;
3644 bool function_result_typed = false;
3645 bool bad_characteristic = false;
3646 gfc_typespec *ts;
3648 in_specification_block = true;
3650 verify_st_order (&ss, ST_NONE, false);
3651 if (st == ST_NONE)
3652 st = next_statement ();
3654 /* If we are not inside a function or don't have a result specified so far,
3655 do nothing special about it. */
3656 if (gfc_current_state () != COMP_FUNCTION)
3657 function_result_typed = true;
3658 else
3660 gfc_symbol* proc = gfc_current_ns->proc_name;
3661 gcc_assert (proc);
3663 if (proc->result->ts.type == BT_UNKNOWN)
3664 function_result_typed = true;
3667 loop:
3669 /* If we're inside a BLOCK construct, some statements are disallowed.
3670 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3671 or VALUE are also disallowed, but they don't have a particular ST_*
3672 key so we have to check for them individually in their matcher routine. */
3673 if (gfc_current_state () == COMP_BLOCK)
3674 switch (st)
3676 case ST_IMPLICIT:
3677 case ST_IMPLICIT_NONE:
3678 case ST_NAMELIST:
3679 case ST_COMMON:
3680 case ST_EQUIVALENCE:
3681 case ST_STATEMENT_FUNCTION:
3682 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3683 gfc_ascii_statement (st));
3684 reject_statement ();
3685 break;
3687 default:
3688 break;
3690 else if (gfc_current_state () == COMP_BLOCK_DATA)
3691 /* Fortran 2008, C1116. */
3692 switch (st)
3694 case ST_ATTR_DECL:
3695 case ST_COMMON:
3696 case ST_DATA:
3697 case ST_DATA_DECL:
3698 case ST_DERIVED_DECL:
3699 case ST_END_BLOCK_DATA:
3700 case ST_EQUIVALENCE:
3701 case ST_IMPLICIT:
3702 case ST_IMPLICIT_NONE:
3703 case ST_OMP_THREADPRIVATE:
3704 case ST_PARAMETER:
3705 case ST_STRUCTURE_DECL:
3706 case ST_TYPE:
3707 case ST_USE:
3708 break;
3710 case ST_NONE:
3711 break;
3713 default:
3714 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3715 gfc_ascii_statement (st));
3716 reject_statement ();
3717 break;
3720 /* If we find a statement that can not be followed by an IMPLICIT statement
3721 (and thus we can expect to see none any further), type the function result
3722 if it has not yet been typed. Be careful not to give the END statement
3723 to verify_st_order! */
3724 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
3726 bool verify_now = false;
3728 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
3729 verify_now = true;
3730 else
3732 st_state dummyss;
3733 verify_st_order (&dummyss, ST_NONE, false);
3734 verify_st_order (&dummyss, st, false);
3736 if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
3737 verify_now = true;
3740 if (verify_now)
3742 check_function_result_typed ();
3743 function_result_typed = true;
3747 switch (st)
3749 case ST_NONE:
3750 unexpected_eof ();
3752 case ST_IMPLICIT_NONE:
3753 case ST_IMPLICIT:
3754 if (!function_result_typed)
3756 check_function_result_typed ();
3757 function_result_typed = true;
3759 goto declSt;
3761 case ST_FORMAT:
3762 case ST_ENTRY:
3763 case ST_DATA: /* Not allowed in interfaces */
3764 if (gfc_current_state () == COMP_INTERFACE)
3765 break;
3767 /* Fall through */
3769 case ST_USE:
3770 case ST_IMPORT:
3771 case ST_PARAMETER:
3772 case ST_PUBLIC:
3773 case ST_PRIVATE:
3774 case ST_STRUCTURE_DECL:
3775 case ST_DERIVED_DECL:
3776 case_decl:
3777 case_omp_decl:
3778 declSt:
3779 if (!verify_st_order (&ss, st, false))
3781 reject_statement ();
3782 st = next_statement ();
3783 goto loop;
3786 switch (st)
3788 case ST_INTERFACE:
3789 parse_interface ();
3790 break;
3792 case ST_STRUCTURE_DECL:
3793 parse_struct_map (ST_STRUCTURE_DECL);
3794 break;
3796 case ST_DERIVED_DECL:
3797 parse_derived ();
3798 break;
3800 case ST_PUBLIC:
3801 case ST_PRIVATE:
3802 if (gfc_current_state () != COMP_MODULE)
3804 gfc_error ("%s statement must appear in a MODULE",
3805 gfc_ascii_statement (st));
3806 reject_statement ();
3807 break;
3810 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
3812 gfc_error ("%s statement at %C follows another accessibility "
3813 "specification", gfc_ascii_statement (st));
3814 reject_statement ();
3815 break;
3818 gfc_current_ns->default_access = (st == ST_PUBLIC)
3819 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3821 break;
3823 case ST_STATEMENT_FUNCTION:
3824 if (gfc_current_state () == COMP_MODULE
3825 || gfc_current_state () == COMP_SUBMODULE)
3827 unexpected_statement (st);
3828 break;
3831 default:
3832 break;
3835 accept_statement (st);
3836 st = next_statement ();
3837 goto loop;
3839 case ST_ENUM:
3840 accept_statement (st);
3841 parse_enum();
3842 st = next_statement ();
3843 goto loop;
3845 case ST_GET_FCN_CHARACTERISTICS:
3846 /* This statement triggers the association of a function's result
3847 characteristics. */
3848 ts = &gfc_current_block ()->result->ts;
3849 if (match_deferred_characteristics (ts) != MATCH_YES)
3850 bad_characteristic = true;
3852 st = next_statement ();
3853 goto loop;
3855 default:
3856 break;
3859 /* If match_deferred_characteristics failed, then there is an error. */
3860 if (bad_characteristic)
3862 ts = &gfc_current_block ()->result->ts;
3863 if (ts->type != BT_DERIVED)
3864 gfc_error ("Bad kind expression for function %qs at %L",
3865 gfc_current_block ()->name,
3866 &gfc_current_block ()->declared_at);
3867 else
3868 gfc_error ("The type for function %qs at %L is not accessible",
3869 gfc_current_block ()->name,
3870 &gfc_current_block ()->declared_at);
3872 gfc_current_block ()->ts.kind = 0;
3873 /* Keep the derived type; if it's bad, it will be discovered later. */
3874 if (!(ts->type == BT_DERIVED && ts->u.derived))
3875 ts->type = BT_UNKNOWN;
3878 in_specification_block = false;
3880 return st;
3884 /* Parse a WHERE block, (not a simple WHERE statement). */
3886 static void
3887 parse_where_block (void)
3889 int seen_empty_else;
3890 gfc_code *top, *d;
3891 gfc_state_data s;
3892 gfc_statement st;
3894 accept_statement (ST_WHERE_BLOCK);
3895 top = gfc_state_stack->tail;
3897 push_state (&s, COMP_WHERE, gfc_new_block);
3899 d = add_statement ();
3900 d->expr1 = top->expr1;
3901 d->op = EXEC_WHERE;
3903 top->expr1 = NULL;
3904 top->block = d;
3906 seen_empty_else = 0;
3910 st = next_statement ();
3911 switch (st)
3913 case ST_NONE:
3914 unexpected_eof ();
3916 case ST_WHERE_BLOCK:
3917 parse_where_block ();
3918 break;
3920 case ST_ASSIGNMENT:
3921 case ST_WHERE:
3922 accept_statement (st);
3923 break;
3925 case ST_ELSEWHERE:
3926 if (seen_empty_else)
3928 gfc_error ("ELSEWHERE statement at %C follows previous "
3929 "unmasked ELSEWHERE");
3930 reject_statement ();
3931 break;
3934 if (new_st.expr1 == NULL)
3935 seen_empty_else = 1;
3937 d = new_level (gfc_state_stack->head);
3938 d->op = EXEC_WHERE;
3939 d->expr1 = new_st.expr1;
3941 accept_statement (st);
3943 break;
3945 case ST_END_WHERE:
3946 accept_statement (st);
3947 break;
3949 default:
3950 gfc_error ("Unexpected %s statement in WHERE block at %C",
3951 gfc_ascii_statement (st));
3952 reject_statement ();
3953 break;
3956 while (st != ST_END_WHERE);
3958 pop_state ();
3962 /* Parse a FORALL block (not a simple FORALL statement). */
3964 static void
3965 parse_forall_block (void)
3967 gfc_code *top, *d;
3968 gfc_state_data s;
3969 gfc_statement st;
3971 accept_statement (ST_FORALL_BLOCK);
3972 top = gfc_state_stack->tail;
3974 push_state (&s, COMP_FORALL, gfc_new_block);
3976 d = add_statement ();
3977 d->op = EXEC_FORALL;
3978 top->block = d;
3982 st = next_statement ();
3983 switch (st)
3986 case ST_ASSIGNMENT:
3987 case ST_POINTER_ASSIGNMENT:
3988 case ST_WHERE:
3989 case ST_FORALL:
3990 accept_statement (st);
3991 break;
3993 case ST_WHERE_BLOCK:
3994 parse_where_block ();
3995 break;
3997 case ST_FORALL_BLOCK:
3998 parse_forall_block ();
3999 break;
4001 case ST_END_FORALL:
4002 accept_statement (st);
4003 break;
4005 case ST_NONE:
4006 unexpected_eof ();
4008 default:
4009 gfc_error ("Unexpected %s statement in FORALL block at %C",
4010 gfc_ascii_statement (st));
4012 reject_statement ();
4013 break;
4016 while (st != ST_END_FORALL);
4018 pop_state ();
4022 static gfc_statement parse_executable (gfc_statement);
4024 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
4026 static void
4027 parse_if_block (void)
4029 gfc_code *top, *d;
4030 gfc_statement st;
4031 locus else_locus;
4032 gfc_state_data s;
4033 int seen_else;
4035 seen_else = 0;
4036 accept_statement (ST_IF_BLOCK);
4038 top = gfc_state_stack->tail;
4039 push_state (&s, COMP_IF, gfc_new_block);
4041 new_st.op = EXEC_IF;
4042 d = add_statement ();
4044 d->expr1 = top->expr1;
4045 top->expr1 = NULL;
4046 top->block = d;
4050 st = parse_executable (ST_NONE);
4052 switch (st)
4054 case ST_NONE:
4055 unexpected_eof ();
4057 case ST_ELSEIF:
4058 if (seen_else)
4060 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
4061 "statement at %L", &else_locus);
4063 reject_statement ();
4064 break;
4067 d = new_level (gfc_state_stack->head);
4068 d->op = EXEC_IF;
4069 d->expr1 = new_st.expr1;
4071 accept_statement (st);
4073 break;
4075 case ST_ELSE:
4076 if (seen_else)
4078 gfc_error ("Duplicate ELSE statements at %L and %C",
4079 &else_locus);
4080 reject_statement ();
4081 break;
4084 seen_else = 1;
4085 else_locus = gfc_current_locus;
4087 d = new_level (gfc_state_stack->head);
4088 d->op = EXEC_IF;
4090 accept_statement (st);
4092 break;
4094 case ST_ENDIF:
4095 break;
4097 default:
4098 unexpected_statement (st);
4099 break;
4102 while (st != ST_ENDIF);
4104 pop_state ();
4105 accept_statement (st);
4109 /* Parse a SELECT block. */
4111 static void
4112 parse_select_block (void)
4114 gfc_statement st;
4115 gfc_code *cp;
4116 gfc_state_data s;
4118 accept_statement (ST_SELECT_CASE);
4120 cp = gfc_state_stack->tail;
4121 push_state (&s, COMP_SELECT, gfc_new_block);
4123 /* Make sure that the next statement is a CASE or END SELECT. */
4124 for (;;)
4126 st = next_statement ();
4127 if (st == ST_NONE)
4128 unexpected_eof ();
4129 if (st == ST_END_SELECT)
4131 /* Empty SELECT CASE is OK. */
4132 accept_statement (st);
4133 pop_state ();
4134 return;
4136 if (st == ST_CASE)
4137 break;
4139 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
4140 "CASE at %C");
4142 reject_statement ();
4145 /* At this point, we're got a nonempty select block. */
4146 cp = new_level (cp);
4147 *cp = new_st;
4149 accept_statement (st);
4153 st = parse_executable (ST_NONE);
4154 switch (st)
4156 case ST_NONE:
4157 unexpected_eof ();
4159 case ST_CASE:
4160 cp = new_level (gfc_state_stack->head);
4161 *cp = new_st;
4162 gfc_clear_new_st ();
4164 accept_statement (st);
4165 /* Fall through */
4167 case ST_END_SELECT:
4168 break;
4170 /* Can't have an executable statement because of
4171 parse_executable(). */
4172 default:
4173 unexpected_statement (st);
4174 break;
4177 while (st != ST_END_SELECT);
4179 pop_state ();
4180 accept_statement (st);
4184 /* Pop the current selector from the SELECT TYPE stack. */
4186 static void
4187 select_type_pop (void)
4189 gfc_select_type_stack *old = select_type_stack;
4190 select_type_stack = old->prev;
4191 free (old);
4195 /* Parse a SELECT TYPE construct (F03:R821). */
4197 static void
4198 parse_select_type_block (void)
4200 gfc_statement st;
4201 gfc_code *cp;
4202 gfc_state_data s;
4204 gfc_current_ns = new_st.ext.block.ns;
4205 accept_statement (ST_SELECT_TYPE);
4207 cp = gfc_state_stack->tail;
4208 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
4210 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
4211 or END SELECT. */
4212 for (;;)
4214 st = next_statement ();
4215 if (st == ST_NONE)
4216 unexpected_eof ();
4217 if (st == ST_END_SELECT)
4218 /* Empty SELECT CASE is OK. */
4219 goto done;
4220 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
4221 break;
4223 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
4224 "following SELECT TYPE at %C");
4226 reject_statement ();
4229 /* At this point, we're got a nonempty select block. */
4230 cp = new_level (cp);
4231 *cp = new_st;
4233 accept_statement (st);
4237 st = parse_executable (ST_NONE);
4238 switch (st)
4240 case ST_NONE:
4241 unexpected_eof ();
4243 case ST_TYPE_IS:
4244 case ST_CLASS_IS:
4245 cp = new_level (gfc_state_stack->head);
4246 *cp = new_st;
4247 gfc_clear_new_st ();
4249 accept_statement (st);
4250 /* Fall through */
4252 case ST_END_SELECT:
4253 break;
4255 /* Can't have an executable statement because of
4256 parse_executable(). */
4257 default:
4258 unexpected_statement (st);
4259 break;
4262 while (st != ST_END_SELECT);
4264 done:
4265 pop_state ();
4266 accept_statement (st);
4267 gfc_current_ns = gfc_current_ns->parent;
4268 select_type_pop ();
4272 /* Given a symbol, make sure it is not an iteration variable for a DO
4273 statement. This subroutine is called when the symbol is seen in a
4274 context that causes it to become redefined. If the symbol is an
4275 iterator, we generate an error message and return nonzero. */
4278 gfc_check_do_variable (gfc_symtree *st)
4280 gfc_state_data *s;
4282 for (s=gfc_state_stack; s; s = s->previous)
4283 if (s->do_variable == st)
4285 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
4286 "loop beginning at %L", st->name, &s->head->loc);
4287 return 1;
4290 return 0;
4294 /* Checks to see if the current statement label closes an enddo.
4295 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
4296 an error) if it incorrectly closes an ENDDO. */
4298 static int
4299 check_do_closure (void)
4301 gfc_state_data *p;
4303 if (gfc_statement_label == NULL)
4304 return 0;
4306 for (p = gfc_state_stack; p; p = p->previous)
4307 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
4308 break;
4310 if (p == NULL)
4311 return 0; /* No loops to close */
4313 if (p->ext.end_do_label == gfc_statement_label)
4315 if (p == gfc_state_stack)
4316 return 1;
4318 gfc_error ("End of nonblock DO statement at %C is within another block");
4319 return 2;
4322 /* At this point, the label doesn't terminate the innermost loop.
4323 Make sure it doesn't terminate another one. */
4324 for (; p; p = p->previous)
4325 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
4326 && p->ext.end_do_label == gfc_statement_label)
4328 gfc_error ("End of nonblock DO statement at %C is interwoven "
4329 "with another DO loop");
4330 return 2;
4333 return 0;
4337 /* Parse a series of contained program units. */
4339 static void parse_progunit (gfc_statement);
4342 /* Parse a CRITICAL block. */
4344 static void
4345 parse_critical_block (void)
4347 gfc_code *top, *d;
4348 gfc_state_data s, *sd;
4349 gfc_statement st;
4351 for (sd = gfc_state_stack; sd; sd = sd->previous)
4352 if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
4353 gfc_error_now (is_oacc (sd)
4354 ? G_("CRITICAL block inside of OpenACC region at %C")
4355 : G_("CRITICAL block inside of OpenMP region at %C"));
4357 s.ext.end_do_label = new_st.label1;
4359 accept_statement (ST_CRITICAL);
4360 top = gfc_state_stack->tail;
4362 push_state (&s, COMP_CRITICAL, gfc_new_block);
4364 d = add_statement ();
4365 d->op = EXEC_CRITICAL;
4366 top->block = d;
4370 st = parse_executable (ST_NONE);
4372 switch (st)
4374 case ST_NONE:
4375 unexpected_eof ();
4376 break;
4378 case ST_END_CRITICAL:
4379 if (s.ext.end_do_label != NULL
4380 && s.ext.end_do_label != gfc_statement_label)
4381 gfc_error_now ("Statement label in END CRITICAL at %C does not "
4382 "match CRITICAL label");
4384 if (gfc_statement_label != NULL)
4386 new_st.op = EXEC_NOP;
4387 add_statement ();
4389 break;
4391 default:
4392 unexpected_statement (st);
4393 break;
4396 while (st != ST_END_CRITICAL);
4398 pop_state ();
4399 accept_statement (st);
4403 /* Set up the local namespace for a BLOCK construct. */
4405 gfc_namespace*
4406 gfc_build_block_ns (gfc_namespace *parent_ns)
4408 gfc_namespace* my_ns;
4409 static int numblock = 1;
4411 my_ns = gfc_get_namespace (parent_ns, 1);
4412 my_ns->construct_entities = 1;
4414 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
4415 code generation (so it must not be NULL).
4416 We set its recursive argument if our container procedure is recursive, so
4417 that local variables are accordingly placed on the stack when it
4418 will be necessary. */
4419 if (gfc_new_block)
4420 my_ns->proc_name = gfc_new_block;
4421 else
4423 bool t;
4424 char buffer[20]; /* Enough to hold "block@2147483648\n". */
4426 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
4427 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
4428 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
4429 my_ns->proc_name->name, NULL);
4430 gcc_assert (t);
4431 gfc_commit_symbol (my_ns->proc_name);
4434 if (parent_ns->proc_name)
4435 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
4437 return my_ns;
4441 /* Parse a BLOCK construct. */
4443 static void
4444 parse_block_construct (void)
4446 gfc_namespace* my_ns;
4447 gfc_namespace* my_parent;
4448 gfc_state_data s;
4450 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
4452 my_ns = gfc_build_block_ns (gfc_current_ns);
4454 new_st.op = EXEC_BLOCK;
4455 new_st.ext.block.ns = my_ns;
4456 new_st.ext.block.assoc = NULL;
4457 accept_statement (ST_BLOCK);
4459 push_state (&s, COMP_BLOCK, my_ns->proc_name);
4460 gfc_current_ns = my_ns;
4461 my_parent = my_ns->parent;
4463 parse_progunit (ST_NONE);
4465 /* Don't depend on the value of gfc_current_ns; it might have been
4466 reset if the block had errors and was cleaned up. */
4467 gfc_current_ns = my_parent;
4469 pop_state ();
4473 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
4474 behind the scenes with compiler-generated variables. */
4476 static void
4477 parse_associate (void)
4479 gfc_namespace* my_ns;
4480 gfc_state_data s;
4481 gfc_statement st;
4482 gfc_association_list* a;
4484 gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
4486 my_ns = gfc_build_block_ns (gfc_current_ns);
4488 new_st.op = EXEC_BLOCK;
4489 new_st.ext.block.ns = my_ns;
4490 gcc_assert (new_st.ext.block.assoc);
4492 /* Add all associate-names as BLOCK variables. Creating them is enough
4493 for now, they'll get their values during trans-* phase. */
4494 gfc_current_ns = my_ns;
4495 for (a = new_st.ext.block.assoc; a; a = a->next)
4497 gfc_symbol* sym;
4498 gfc_ref *ref;
4499 gfc_array_ref *array_ref;
4501 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
4502 gcc_unreachable ();
4504 sym = a->st->n.sym;
4505 sym->attr.flavor = FL_VARIABLE;
4506 sym->assoc = a;
4507 sym->declared_at = a->where;
4508 gfc_set_sym_referenced (sym);
4510 /* Initialize the typespec. It is not available in all cases,
4511 however, as it may only be set on the target during resolution.
4512 Still, sometimes it helps to have it right now -- especially
4513 for parsing component references on the associate-name
4514 in case of association to a derived-type. */
4515 sym->ts = a->target->ts;
4517 /* Check if the target expression is array valued. This can not always
4518 be done by looking at target.rank, because that might not have been
4519 set yet. Therefore traverse the chain of refs, looking for the last
4520 array ref and evaluate that. */
4521 array_ref = NULL;
4522 for (ref = a->target->ref; ref; ref = ref->next)
4523 if (ref->type == REF_ARRAY)
4524 array_ref = &ref->u.ar;
4525 if (array_ref || a->target->rank)
4527 gfc_array_spec *as;
4528 int dim, rank = 0;
4529 if (array_ref)
4531 a->rankguessed = 1;
4532 /* Count the dimension, that have a non-scalar extend. */
4533 for (dim = 0; dim < array_ref->dimen; ++dim)
4534 if (array_ref->dimen_type[dim] != DIMEN_ELEMENT
4535 && !(array_ref->dimen_type[dim] == DIMEN_UNKNOWN
4536 && array_ref->end[dim] == NULL
4537 && array_ref->start[dim] != NULL))
4538 ++rank;
4540 else
4541 rank = a->target->rank;
4542 /* When the rank is greater than zero then sym will be an array. */
4543 if (sym->ts.type == BT_CLASS)
4545 if ((!CLASS_DATA (sym)->as && rank != 0)
4546 || (CLASS_DATA (sym)->as
4547 && CLASS_DATA (sym)->as->rank != rank))
4549 /* Don't just (re-)set the attr and as in the sym.ts,
4550 because this modifies the target's attr and as. Copy the
4551 data and do a build_class_symbol. */
4552 symbol_attribute attr = CLASS_DATA (a->target)->attr;
4553 int corank = gfc_get_corank (a->target);
4554 gfc_typespec type;
4556 if (rank || corank)
4558 as = gfc_get_array_spec ();
4559 as->type = AS_DEFERRED;
4560 as->rank = rank;
4561 as->corank = corank;
4562 attr.dimension = rank ? 1 : 0;
4563 attr.codimension = corank ? 1 : 0;
4565 else
4567 as = NULL;
4568 attr.dimension = attr.codimension = 0;
4570 attr.class_ok = 0;
4571 type = CLASS_DATA (sym)->ts;
4572 if (!gfc_build_class_symbol (&type,
4573 &attr, &as))
4574 gcc_unreachable ();
4575 sym->ts = type;
4576 sym->ts.type = BT_CLASS;
4577 sym->attr.class_ok = 1;
4579 else
4580 sym->attr.class_ok = 1;
4582 else if ((!sym->as && rank != 0)
4583 || (sym->as && sym->as->rank != rank))
4585 as = gfc_get_array_spec ();
4586 as->type = AS_DEFERRED;
4587 as->rank = rank;
4588 as->corank = gfc_get_corank (a->target);
4589 sym->as = as;
4590 sym->attr.dimension = 1;
4591 if (as->corank)
4592 sym->attr.codimension = 1;
4597 accept_statement (ST_ASSOCIATE);
4598 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
4600 loop:
4601 st = parse_executable (ST_NONE);
4602 switch (st)
4604 case ST_NONE:
4605 unexpected_eof ();
4607 case_end:
4608 accept_statement (st);
4609 my_ns->code = gfc_state_stack->head;
4610 break;
4612 default:
4613 unexpected_statement (st);
4614 goto loop;
4617 gfc_current_ns = gfc_current_ns->parent;
4618 pop_state ();
4622 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4623 handled inside of parse_executable(), because they aren't really
4624 loop statements. */
4626 static void
4627 parse_do_block (void)
4629 gfc_statement st;
4630 gfc_code *top;
4631 gfc_state_data s;
4632 gfc_symtree *stree;
4633 gfc_exec_op do_op;
4635 do_op = new_st.op;
4636 s.ext.end_do_label = new_st.label1;
4638 if (new_st.ext.iterator != NULL)
4640 stree = new_st.ext.iterator->var->symtree;
4641 if (directive_unroll != -1)
4643 new_st.ext.iterator->unroll = directive_unroll;
4644 directive_unroll = -1;
4647 else
4648 stree = NULL;
4650 accept_statement (ST_DO);
4652 top = gfc_state_stack->tail;
4653 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
4654 gfc_new_block);
4656 s.do_variable = stree;
4658 top->block = new_level (top);
4659 top->block->op = EXEC_DO;
4661 loop:
4662 st = parse_executable (ST_NONE);
4664 switch (st)
4666 case ST_NONE:
4667 unexpected_eof ();
4669 case ST_ENDDO:
4670 if (s.ext.end_do_label != NULL
4671 && s.ext.end_do_label != gfc_statement_label)
4672 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4673 "DO label");
4675 if (gfc_statement_label != NULL)
4677 new_st.op = EXEC_NOP;
4678 add_statement ();
4680 break;
4682 case ST_IMPLIED_ENDDO:
4683 /* If the do-stmt of this DO construct has a do-construct-name,
4684 the corresponding end-do must be an end-do-stmt (with a matching
4685 name, but in that case we must have seen ST_ENDDO first).
4686 We only complain about this in pedantic mode. */
4687 if (gfc_current_block () != NULL)
4688 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4689 &gfc_current_block()->declared_at);
4691 break;
4693 default:
4694 unexpected_statement (st);
4695 goto loop;
4698 pop_state ();
4699 accept_statement (st);
4703 /* Parse the statements of OpenMP do/parallel do. */
4705 static gfc_statement
4706 parse_omp_do (gfc_statement omp_st)
4708 gfc_statement st;
4709 gfc_code *cp, *np;
4710 gfc_state_data s;
4712 accept_statement (omp_st);
4714 cp = gfc_state_stack->tail;
4715 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4716 np = new_level (cp);
4717 np->op = cp->op;
4718 np->block = NULL;
4720 for (;;)
4722 st = next_statement ();
4723 if (st == ST_NONE)
4724 unexpected_eof ();
4725 else if (st == ST_DO)
4726 break;
4727 else
4728 unexpected_statement (st);
4731 parse_do_block ();
4732 if (gfc_statement_label != NULL
4733 && gfc_state_stack->previous != NULL
4734 && gfc_state_stack->previous->state == COMP_DO
4735 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4737 /* In
4738 DO 100 I=1,10
4739 !$OMP DO
4740 DO J=1,10
4742 100 CONTINUE
4743 there should be no !$OMP END DO. */
4744 pop_state ();
4745 return ST_IMPLIED_ENDDO;
4748 check_do_closure ();
4749 pop_state ();
4751 st = next_statement ();
4752 gfc_statement omp_end_st = ST_OMP_END_DO;
4753 switch (omp_st)
4755 case ST_OMP_DISTRIBUTE: omp_end_st = ST_OMP_END_DISTRIBUTE; break;
4756 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4757 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4758 break;
4759 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4760 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4761 break;
4762 case ST_OMP_DISTRIBUTE_SIMD:
4763 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4764 break;
4765 case ST_OMP_DO: omp_end_st = ST_OMP_END_DO; break;
4766 case ST_OMP_DO_SIMD: omp_end_st = ST_OMP_END_DO_SIMD; break;
4767 case ST_OMP_PARALLEL_DO: omp_end_st = ST_OMP_END_PARALLEL_DO; break;
4768 case ST_OMP_PARALLEL_DO_SIMD:
4769 omp_end_st = ST_OMP_END_PARALLEL_DO_SIMD;
4770 break;
4771 case ST_OMP_SIMD: omp_end_st = ST_OMP_END_SIMD; break;
4772 case ST_OMP_TARGET_PARALLEL_DO:
4773 omp_end_st = ST_OMP_END_TARGET_PARALLEL_DO;
4774 break;
4775 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
4776 omp_end_st = ST_OMP_END_TARGET_PARALLEL_DO_SIMD;
4777 break;
4778 case ST_OMP_TARGET_SIMD: omp_end_st = ST_OMP_END_TARGET_SIMD; break;
4779 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4780 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4781 break;
4782 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4783 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4784 break;
4785 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4786 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4787 break;
4788 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4789 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4790 break;
4791 case ST_OMP_TASKLOOP: omp_end_st = ST_OMP_END_TASKLOOP; break;
4792 case ST_OMP_TASKLOOP_SIMD: omp_end_st = ST_OMP_END_TASKLOOP_SIMD; break;
4793 case ST_OMP_TEAMS_DISTRIBUTE:
4794 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4795 break;
4796 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4797 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4798 break;
4799 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4800 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4801 break;
4802 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4803 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4804 break;
4805 default: gcc_unreachable ();
4807 if (st == omp_end_st)
4809 if (new_st.op == EXEC_OMP_END_NOWAIT)
4810 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4811 else
4812 gcc_assert (new_st.op == EXEC_NOP);
4813 gfc_clear_new_st ();
4814 gfc_commit_symbols ();
4815 gfc_warning_check ();
4816 st = next_statement ();
4818 return st;
4822 /* Parse the statements of OpenMP atomic directive. */
4824 static gfc_statement
4825 parse_omp_oacc_atomic (bool omp_p)
4827 gfc_statement st, st_atomic, st_end_atomic;
4828 gfc_code *cp, *np;
4829 gfc_state_data s;
4830 int count;
4832 if (omp_p)
4834 st_atomic = ST_OMP_ATOMIC;
4835 st_end_atomic = ST_OMP_END_ATOMIC;
4837 else
4839 st_atomic = ST_OACC_ATOMIC;
4840 st_end_atomic = ST_OACC_END_ATOMIC;
4842 accept_statement (st_atomic);
4844 cp = gfc_state_stack->tail;
4845 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4846 np = new_level (cp);
4847 np->op = cp->op;
4848 np->block = NULL;
4849 np->ext.omp_atomic = cp->ext.omp_atomic;
4850 count = 1 + ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4851 == GFC_OMP_ATOMIC_CAPTURE);
4853 while (count)
4855 st = next_statement ();
4856 if (st == ST_NONE)
4857 unexpected_eof ();
4858 else if (st == ST_ASSIGNMENT)
4860 accept_statement (st);
4861 count--;
4863 else
4864 unexpected_statement (st);
4867 pop_state ();
4869 st = next_statement ();
4870 if (st == st_end_atomic)
4872 gfc_clear_new_st ();
4873 gfc_commit_symbols ();
4874 gfc_warning_check ();
4875 st = next_statement ();
4877 else if ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4878 == GFC_OMP_ATOMIC_CAPTURE)
4879 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4880 return st;
4884 /* Parse the statements of an OpenACC structured block. */
4886 static void
4887 parse_oacc_structured_block (gfc_statement acc_st)
4889 gfc_statement st, acc_end_st;
4890 gfc_code *cp, *np;
4891 gfc_state_data s, *sd;
4893 for (sd = gfc_state_stack; sd; sd = sd->previous)
4894 if (sd->state == COMP_CRITICAL)
4895 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4897 accept_statement (acc_st);
4899 cp = gfc_state_stack->tail;
4900 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4901 np = new_level (cp);
4902 np->op = cp->op;
4903 np->block = NULL;
4904 switch (acc_st)
4906 case ST_OACC_PARALLEL:
4907 acc_end_st = ST_OACC_END_PARALLEL;
4908 break;
4909 case ST_OACC_KERNELS:
4910 acc_end_st = ST_OACC_END_KERNELS;
4911 break;
4912 case ST_OACC_DATA:
4913 acc_end_st = ST_OACC_END_DATA;
4914 break;
4915 case ST_OACC_HOST_DATA:
4916 acc_end_st = ST_OACC_END_HOST_DATA;
4917 break;
4918 default:
4919 gcc_unreachable ();
4924 st = parse_executable (ST_NONE);
4925 if (st == ST_NONE)
4926 unexpected_eof ();
4927 else if (st != acc_end_st)
4929 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
4930 reject_statement ();
4933 while (st != acc_end_st);
4935 gcc_assert (new_st.op == EXEC_NOP);
4937 gfc_clear_new_st ();
4938 gfc_commit_symbols ();
4939 gfc_warning_check ();
4940 pop_state ();
4943 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4945 static gfc_statement
4946 parse_oacc_loop (gfc_statement acc_st)
4948 gfc_statement st;
4949 gfc_code *cp, *np;
4950 gfc_state_data s, *sd;
4952 for (sd = gfc_state_stack; sd; sd = sd->previous)
4953 if (sd->state == COMP_CRITICAL)
4954 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4956 accept_statement (acc_st);
4958 cp = gfc_state_stack->tail;
4959 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4960 np = new_level (cp);
4961 np->op = cp->op;
4962 np->block = NULL;
4964 for (;;)
4966 st = next_statement ();
4967 if (st == ST_NONE)
4968 unexpected_eof ();
4969 else if (st == ST_DO)
4970 break;
4971 else
4973 gfc_error ("Expected DO loop at %C");
4974 reject_statement ();
4978 parse_do_block ();
4979 if (gfc_statement_label != NULL
4980 && gfc_state_stack->previous != NULL
4981 && gfc_state_stack->previous->state == COMP_DO
4982 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4984 pop_state ();
4985 return ST_IMPLIED_ENDDO;
4988 check_do_closure ();
4989 pop_state ();
4991 st = next_statement ();
4992 if (st == ST_OACC_END_LOOP)
4993 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
4994 if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
4995 (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
4996 (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
4998 gcc_assert (new_st.op == EXEC_NOP);
4999 gfc_clear_new_st ();
5000 gfc_commit_symbols ();
5001 gfc_warning_check ();
5002 st = next_statement ();
5004 return st;
5008 /* Parse the statements of an OpenMP structured block. */
5010 static void
5011 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
5013 gfc_statement st, omp_end_st;
5014 gfc_code *cp, *np;
5015 gfc_state_data s;
5017 accept_statement (omp_st);
5019 cp = gfc_state_stack->tail;
5020 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
5021 np = new_level (cp);
5022 np->op = cp->op;
5023 np->block = NULL;
5025 switch (omp_st)
5027 case ST_OMP_PARALLEL:
5028 omp_end_st = ST_OMP_END_PARALLEL;
5029 break;
5030 case ST_OMP_PARALLEL_SECTIONS:
5031 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
5032 break;
5033 case ST_OMP_SECTIONS:
5034 omp_end_st = ST_OMP_END_SECTIONS;
5035 break;
5036 case ST_OMP_ORDERED:
5037 omp_end_st = ST_OMP_END_ORDERED;
5038 break;
5039 case ST_OMP_CRITICAL:
5040 omp_end_st = ST_OMP_END_CRITICAL;
5041 break;
5042 case ST_OMP_MASTER:
5043 omp_end_st = ST_OMP_END_MASTER;
5044 break;
5045 case ST_OMP_SINGLE:
5046 omp_end_st = ST_OMP_END_SINGLE;
5047 break;
5048 case ST_OMP_TARGET:
5049 omp_end_st = ST_OMP_END_TARGET;
5050 break;
5051 case ST_OMP_TARGET_DATA:
5052 omp_end_st = ST_OMP_END_TARGET_DATA;
5053 break;
5054 case ST_OMP_TARGET_TEAMS:
5055 omp_end_st = ST_OMP_END_TARGET_TEAMS;
5056 break;
5057 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
5058 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
5059 break;
5060 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
5061 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
5062 break;
5063 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5064 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
5065 break;
5066 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
5067 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
5068 break;
5069 case ST_OMP_TASK:
5070 omp_end_st = ST_OMP_END_TASK;
5071 break;
5072 case ST_OMP_TASKGROUP:
5073 omp_end_st = ST_OMP_END_TASKGROUP;
5074 break;
5075 case ST_OMP_TEAMS:
5076 omp_end_st = ST_OMP_END_TEAMS;
5077 break;
5078 case ST_OMP_TEAMS_DISTRIBUTE:
5079 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
5080 break;
5081 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
5082 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
5083 break;
5084 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5085 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
5086 break;
5087 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
5088 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
5089 break;
5090 case ST_OMP_DISTRIBUTE:
5091 omp_end_st = ST_OMP_END_DISTRIBUTE;
5092 break;
5093 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
5094 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
5095 break;
5096 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
5097 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
5098 break;
5099 case ST_OMP_DISTRIBUTE_SIMD:
5100 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
5101 break;
5102 case ST_OMP_WORKSHARE:
5103 omp_end_st = ST_OMP_END_WORKSHARE;
5104 break;
5105 case ST_OMP_PARALLEL_WORKSHARE:
5106 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
5107 break;
5108 default:
5109 gcc_unreachable ();
5114 if (workshare_stmts_only)
5116 /* Inside of !$omp workshare, only
5117 scalar assignments
5118 array assignments
5119 where statements and constructs
5120 forall statements and constructs
5121 !$omp atomic
5122 !$omp critical
5123 !$omp parallel
5124 are allowed. For !$omp critical these
5125 restrictions apply recursively. */
5126 bool cycle = true;
5128 st = next_statement ();
5129 for (;;)
5131 switch (st)
5133 case ST_NONE:
5134 unexpected_eof ();
5136 case ST_ASSIGNMENT:
5137 case ST_WHERE:
5138 case ST_FORALL:
5139 accept_statement (st);
5140 break;
5142 case ST_WHERE_BLOCK:
5143 parse_where_block ();
5144 break;
5146 case ST_FORALL_BLOCK:
5147 parse_forall_block ();
5148 break;
5150 case ST_OMP_PARALLEL:
5151 case ST_OMP_PARALLEL_SECTIONS:
5152 parse_omp_structured_block (st, false);
5153 break;
5155 case ST_OMP_PARALLEL_WORKSHARE:
5156 case ST_OMP_CRITICAL:
5157 parse_omp_structured_block (st, true);
5158 break;
5160 case ST_OMP_PARALLEL_DO:
5161 case ST_OMP_PARALLEL_DO_SIMD:
5162 st = parse_omp_do (st);
5163 continue;
5165 case ST_OMP_ATOMIC:
5166 st = parse_omp_oacc_atomic (true);
5167 continue;
5169 default:
5170 cycle = false;
5171 break;
5174 if (!cycle)
5175 break;
5177 st = next_statement ();
5180 else
5181 st = parse_executable (ST_NONE);
5182 if (st == ST_NONE)
5183 unexpected_eof ();
5184 else if (st == ST_OMP_SECTION
5185 && (omp_st == ST_OMP_SECTIONS
5186 || omp_st == ST_OMP_PARALLEL_SECTIONS))
5188 np = new_level (np);
5189 np->op = cp->op;
5190 np->block = NULL;
5192 else if (st != omp_end_st)
5193 unexpected_statement (st);
5195 while (st != omp_end_st);
5197 switch (new_st.op)
5199 case EXEC_OMP_END_NOWAIT:
5200 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
5201 break;
5202 case EXEC_OMP_END_CRITICAL:
5203 if (((cp->ext.omp_clauses == NULL) ^ (new_st.ext.omp_name == NULL))
5204 || (new_st.ext.omp_name != NULL
5205 && strcmp (cp->ext.omp_clauses->critical_name,
5206 new_st.ext.omp_name) != 0))
5207 gfc_error ("Name after !$omp critical and !$omp end critical does "
5208 "not match at %C");
5209 free (CONST_CAST (char *, new_st.ext.omp_name));
5210 new_st.ext.omp_name = NULL;
5211 break;
5212 case EXEC_OMP_END_SINGLE:
5213 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
5214 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
5215 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
5216 gfc_free_omp_clauses (new_st.ext.omp_clauses);
5217 break;
5218 case EXEC_NOP:
5219 break;
5220 default:
5221 gcc_unreachable ();
5224 gfc_clear_new_st ();
5225 gfc_commit_symbols ();
5226 gfc_warning_check ();
5227 pop_state ();
5231 /* Accept a series of executable statements. We return the first
5232 statement that doesn't fit to the caller. Any block statements are
5233 passed on to the correct handler, which usually passes the buck
5234 right back here. */
5236 static gfc_statement
5237 parse_executable (gfc_statement st)
5239 int close_flag;
5241 if (st == ST_NONE)
5242 st = next_statement ();
5244 for (;;)
5246 close_flag = check_do_closure ();
5247 if (close_flag)
5248 switch (st)
5250 case ST_GOTO:
5251 case ST_END_PROGRAM:
5252 case ST_RETURN:
5253 case ST_EXIT:
5254 case ST_END_FUNCTION:
5255 case ST_CYCLE:
5256 case ST_PAUSE:
5257 case ST_STOP:
5258 case ST_ERROR_STOP:
5259 case ST_END_SUBROUTINE:
5261 case ST_DO:
5262 case ST_FORALL:
5263 case ST_WHERE:
5264 case ST_SELECT_CASE:
5265 gfc_error ("%s statement at %C cannot terminate a non-block "
5266 "DO loop", gfc_ascii_statement (st));
5267 break;
5269 default:
5270 break;
5273 switch (st)
5275 case ST_NONE:
5276 unexpected_eof ();
5278 case ST_DATA:
5279 gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
5280 "first executable statement");
5281 /* Fall through. */
5283 case ST_FORMAT:
5284 case ST_ENTRY:
5285 case_executable:
5286 accept_statement (st);
5287 if (close_flag == 1)
5288 return ST_IMPLIED_ENDDO;
5289 break;
5291 case ST_BLOCK:
5292 parse_block_construct ();
5293 break;
5295 case ST_ASSOCIATE:
5296 parse_associate ();
5297 break;
5299 case ST_IF_BLOCK:
5300 parse_if_block ();
5301 break;
5303 case ST_SELECT_CASE:
5304 parse_select_block ();
5305 break;
5307 case ST_SELECT_TYPE:
5308 parse_select_type_block ();
5309 break;
5311 case ST_DO:
5312 parse_do_block ();
5313 if (check_do_closure () == 1)
5314 return ST_IMPLIED_ENDDO;
5315 break;
5317 case ST_CRITICAL:
5318 parse_critical_block ();
5319 break;
5321 case ST_WHERE_BLOCK:
5322 parse_where_block ();
5323 break;
5325 case ST_FORALL_BLOCK:
5326 parse_forall_block ();
5327 break;
5329 case ST_OACC_PARALLEL_LOOP:
5330 case ST_OACC_KERNELS_LOOP:
5331 case ST_OACC_LOOP:
5332 st = parse_oacc_loop (st);
5333 if (st == ST_IMPLIED_ENDDO)
5334 return st;
5335 continue;
5337 case ST_OACC_PARALLEL:
5338 case ST_OACC_KERNELS:
5339 case ST_OACC_DATA:
5340 case ST_OACC_HOST_DATA:
5341 parse_oacc_structured_block (st);
5342 break;
5344 case ST_OMP_PARALLEL:
5345 case ST_OMP_PARALLEL_SECTIONS:
5346 case ST_OMP_SECTIONS:
5347 case ST_OMP_ORDERED:
5348 case ST_OMP_CRITICAL:
5349 case ST_OMP_MASTER:
5350 case ST_OMP_SINGLE:
5351 case ST_OMP_TARGET:
5352 case ST_OMP_TARGET_DATA:
5353 case ST_OMP_TARGET_PARALLEL:
5354 case ST_OMP_TARGET_TEAMS:
5355 case ST_OMP_TEAMS:
5356 case ST_OMP_TASK:
5357 case ST_OMP_TASKGROUP:
5358 parse_omp_structured_block (st, false);
5359 break;
5361 case ST_OMP_WORKSHARE:
5362 case ST_OMP_PARALLEL_WORKSHARE:
5363 parse_omp_structured_block (st, true);
5364 break;
5366 case ST_OMP_DISTRIBUTE:
5367 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
5368 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
5369 case ST_OMP_DISTRIBUTE_SIMD:
5370 case ST_OMP_DO:
5371 case ST_OMP_DO_SIMD:
5372 case ST_OMP_PARALLEL_DO:
5373 case ST_OMP_PARALLEL_DO_SIMD:
5374 case ST_OMP_SIMD:
5375 case ST_OMP_TARGET_PARALLEL_DO:
5376 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
5377 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
5378 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
5379 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5380 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
5381 case ST_OMP_TASKLOOP:
5382 case ST_OMP_TASKLOOP_SIMD:
5383 case ST_OMP_TEAMS_DISTRIBUTE:
5384 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
5385 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5386 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
5387 st = parse_omp_do (st);
5388 if (st == ST_IMPLIED_ENDDO)
5389 return st;
5390 continue;
5392 case ST_OACC_ATOMIC:
5393 st = parse_omp_oacc_atomic (false);
5394 continue;
5396 case ST_OMP_ATOMIC:
5397 st = parse_omp_oacc_atomic (true);
5398 continue;
5400 default:
5401 return st;
5404 if (directive_unroll != -1)
5405 gfc_error ("%<GCC unroll%> directive does not commence a loop at %C");
5407 st = next_statement ();
5412 /* Fix the symbols for sibling functions. These are incorrectly added to
5413 the child namespace as the parser didn't know about this procedure. */
5415 static void
5416 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
5418 gfc_namespace *ns;
5419 gfc_symtree *st;
5420 gfc_symbol *old_sym;
5422 for (ns = siblings; ns; ns = ns->sibling)
5424 st = gfc_find_symtree (ns->sym_root, sym->name);
5426 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
5427 goto fixup_contained;
5429 if ((st->n.sym->attr.flavor == FL_DERIVED
5430 && sym->attr.generic && sym->attr.function)
5431 ||(sym->attr.flavor == FL_DERIVED
5432 && st->n.sym->attr.generic && st->n.sym->attr.function))
5433 goto fixup_contained;
5435 old_sym = st->n.sym;
5436 if (old_sym->ns == ns
5437 && !old_sym->attr.contained
5439 /* By 14.6.1.3, host association should be excluded
5440 for the following. */
5441 && !(old_sym->attr.external
5442 || (old_sym->ts.type != BT_UNKNOWN
5443 && !old_sym->attr.implicit_type)
5444 || old_sym->attr.flavor == FL_PARAMETER
5445 || old_sym->attr.use_assoc
5446 || old_sym->attr.in_common
5447 || old_sym->attr.in_equivalence
5448 || old_sym->attr.data
5449 || old_sym->attr.dummy
5450 || old_sym->attr.result
5451 || old_sym->attr.dimension
5452 || old_sym->attr.allocatable
5453 || old_sym->attr.intrinsic
5454 || old_sym->attr.generic
5455 || old_sym->attr.flavor == FL_NAMELIST
5456 || old_sym->attr.flavor == FL_LABEL
5457 || old_sym->attr.proc == PROC_ST_FUNCTION))
5459 /* Replace it with the symbol from the parent namespace. */
5460 st->n.sym = sym;
5461 sym->refs++;
5463 gfc_release_symbol (old_sym);
5466 fixup_contained:
5467 /* Do the same for any contained procedures. */
5468 gfc_fixup_sibling_symbols (sym, ns->contained);
5472 static void
5473 parse_contained (int module)
5475 gfc_namespace *ns, *parent_ns, *tmp;
5476 gfc_state_data s1, s2;
5477 gfc_statement st;
5478 gfc_symbol *sym;
5479 gfc_entry_list *el;
5480 locus old_loc;
5481 int contains_statements = 0;
5482 int seen_error = 0;
5484 push_state (&s1, COMP_CONTAINS, NULL);
5485 parent_ns = gfc_current_ns;
5489 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
5491 gfc_current_ns->sibling = parent_ns->contained;
5492 parent_ns->contained = gfc_current_ns;
5494 next:
5495 /* Process the next available statement. We come here if we got an error
5496 and rejected the last statement. */
5497 old_loc = gfc_current_locus;
5498 st = next_statement ();
5500 switch (st)
5502 case ST_NONE:
5503 unexpected_eof ();
5505 case ST_FUNCTION:
5506 case ST_SUBROUTINE:
5507 contains_statements = 1;
5508 accept_statement (st);
5510 push_state (&s2,
5511 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
5512 gfc_new_block);
5514 /* For internal procedures, create/update the symbol in the
5515 parent namespace. */
5517 if (!module)
5519 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
5520 gfc_error ("Contained procedure %qs at %C is already "
5521 "ambiguous", gfc_new_block->name);
5522 else
5524 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
5525 sym->name,
5526 &gfc_new_block->declared_at))
5528 if (st == ST_FUNCTION)
5529 gfc_add_function (&sym->attr, sym->name,
5530 &gfc_new_block->declared_at);
5531 else
5532 gfc_add_subroutine (&sym->attr, sym->name,
5533 &gfc_new_block->declared_at);
5537 gfc_commit_symbols ();
5539 else
5540 sym = gfc_new_block;
5542 /* Mark this as a contained function, so it isn't replaced
5543 by other module functions. */
5544 sym->attr.contained = 1;
5546 /* Set implicit_pure so that it can be reset if any of the
5547 tests for purity fail. This is used for some optimisation
5548 during translation. */
5549 if (!sym->attr.pure)
5550 sym->attr.implicit_pure = 1;
5552 parse_progunit (ST_NONE);
5554 /* Fix up any sibling functions that refer to this one. */
5555 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
5556 /* Or refer to any of its alternate entry points. */
5557 for (el = gfc_current_ns->entries; el; el = el->next)
5558 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
5560 gfc_current_ns->code = s2.head;
5561 gfc_current_ns = parent_ns;
5563 pop_state ();
5564 break;
5566 /* These statements are associated with the end of the host unit. */
5567 case ST_END_FUNCTION:
5568 case ST_END_MODULE:
5569 case ST_END_SUBMODULE:
5570 case ST_END_PROGRAM:
5571 case ST_END_SUBROUTINE:
5572 accept_statement (st);
5573 gfc_current_ns->code = s1.head;
5574 break;
5576 default:
5577 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
5578 gfc_ascii_statement (st));
5579 reject_statement ();
5580 seen_error = 1;
5581 goto next;
5582 break;
5585 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
5586 && st != ST_END_MODULE && st != ST_END_SUBMODULE
5587 && st != ST_END_PROGRAM);
5589 /* The first namespace in the list is guaranteed to not have
5590 anything (worthwhile) in it. */
5591 tmp = gfc_current_ns;
5592 gfc_current_ns = parent_ns;
5593 if (seen_error && tmp->refs > 1)
5594 gfc_free_namespace (tmp);
5596 ns = gfc_current_ns->contained;
5597 gfc_current_ns->contained = ns->sibling;
5598 gfc_free_namespace (ns);
5600 pop_state ();
5601 if (!contains_statements)
5602 gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
5603 "FUNCTION or SUBROUTINE statement at %L", &old_loc);
5607 /* The result variable in a MODULE PROCEDURE needs to be created and
5608 its characteristics copied from the interface since it is neither
5609 declared in the procedure declaration nor in the specification
5610 part. */
5612 static void
5613 get_modproc_result (void)
5615 gfc_symbol *proc;
5616 if (gfc_state_stack->previous
5617 && gfc_state_stack->previous->state == COMP_CONTAINS
5618 && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
5620 proc = gfc_current_ns->proc_name ? gfc_current_ns->proc_name : NULL;
5621 if (proc != NULL
5622 && proc->attr.function
5623 && proc->tlink
5624 && proc->tlink->result
5625 && proc->tlink->result != proc->tlink)
5627 gfc_copy_dummy_sym (&proc->result, proc->tlink->result, 1);
5628 gfc_set_sym_referenced (proc->result);
5629 proc->result->attr.if_source = IFSRC_DECL;
5630 gfc_commit_symbol (proc->result);
5636 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
5638 static void
5639 parse_progunit (gfc_statement st)
5641 gfc_state_data *p;
5642 int n;
5644 if (gfc_new_block
5645 && gfc_new_block->abr_modproc_decl
5646 && gfc_new_block->attr.function)
5647 get_modproc_result ();
5649 st = parse_spec (st);
5650 switch (st)
5652 case ST_NONE:
5653 unexpected_eof ();
5655 case ST_CONTAINS:
5656 /* This is not allowed within BLOCK! */
5657 if (gfc_current_state () != COMP_BLOCK)
5658 goto contains;
5659 break;
5661 case_end:
5662 accept_statement (st);
5663 goto done;
5665 default:
5666 break;
5669 if (gfc_current_state () == COMP_FUNCTION)
5670 gfc_check_function_type (gfc_current_ns);
5672 loop:
5673 for (;;)
5675 st = parse_executable (st);
5677 switch (st)
5679 case ST_NONE:
5680 unexpected_eof ();
5682 case ST_CONTAINS:
5683 /* This is not allowed within BLOCK! */
5684 if (gfc_current_state () != COMP_BLOCK)
5685 goto contains;
5686 break;
5688 case_end:
5689 accept_statement (st);
5690 goto done;
5692 default:
5693 break;
5696 unexpected_statement (st);
5697 reject_statement ();
5698 st = next_statement ();
5701 contains:
5702 n = 0;
5704 for (p = gfc_state_stack; p; p = p->previous)
5705 if (p->state == COMP_CONTAINS)
5706 n++;
5708 if (gfc_find_state (COMP_MODULE) == true
5709 || gfc_find_state (COMP_SUBMODULE) == true)
5710 n--;
5712 if (n > 0)
5714 gfc_error ("CONTAINS statement at %C is already in a contained "
5715 "program unit");
5716 reject_statement ();
5717 st = next_statement ();
5718 goto loop;
5721 parse_contained (0);
5723 done:
5724 gfc_current_ns->code = gfc_state_stack->head;
5728 /* Come here to complain about a global symbol already in use as
5729 something else. */
5731 void
5732 gfc_global_used (gfc_gsymbol *sym, locus *where)
5734 const char *name;
5736 if (where == NULL)
5737 where = &gfc_current_locus;
5739 switch(sym->type)
5741 case GSYM_PROGRAM:
5742 name = "PROGRAM";
5743 break;
5744 case GSYM_FUNCTION:
5745 name = "FUNCTION";
5746 break;
5747 case GSYM_SUBROUTINE:
5748 name = "SUBROUTINE";
5749 break;
5750 case GSYM_COMMON:
5751 name = "COMMON";
5752 break;
5753 case GSYM_BLOCK_DATA:
5754 name = "BLOCK DATA";
5755 break;
5756 case GSYM_MODULE:
5757 name = "MODULE";
5758 break;
5759 default:
5760 name = NULL;
5763 if (name)
5765 if (sym->binding_label)
5766 gfc_error ("Global binding name %qs at %L is already being used "
5767 "as a %s at %L", sym->binding_label, where, name,
5768 &sym->where);
5769 else
5770 gfc_error ("Global name %qs at %L is already being used as "
5771 "a %s at %L", sym->name, where, name, &sym->where);
5773 else
5775 if (sym->binding_label)
5776 gfc_error ("Global binding name %qs at %L is already being used "
5777 "at %L", sym->binding_label, where, &sym->where);
5778 else
5779 gfc_error ("Global name %qs at %L is already being used at %L",
5780 sym->name, where, &sym->where);
5785 /* Parse a block data program unit. */
5787 static void
5788 parse_block_data (void)
5790 gfc_statement st;
5791 static locus blank_locus;
5792 static int blank_block=0;
5793 gfc_gsymbol *s;
5795 gfc_current_ns->proc_name = gfc_new_block;
5796 gfc_current_ns->is_block_data = 1;
5798 if (gfc_new_block == NULL)
5800 if (blank_block)
5801 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5802 "prior BLOCK DATA at %L", &blank_locus);
5803 else
5805 blank_block = 1;
5806 blank_locus = gfc_current_locus;
5809 else
5811 s = gfc_get_gsymbol (gfc_new_block->name);
5812 if (s->defined
5813 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
5814 gfc_global_used (s, &gfc_new_block->declared_at);
5815 else
5817 s->type = GSYM_BLOCK_DATA;
5818 s->where = gfc_new_block->declared_at;
5819 s->defined = 1;
5823 st = parse_spec (ST_NONE);
5825 while (st != ST_END_BLOCK_DATA)
5827 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5828 gfc_ascii_statement (st));
5829 reject_statement ();
5830 st = next_statement ();
5835 /* Following the association of the ancestor (sub)module symbols, they
5836 must be set host rather than use associated and all must be public.
5837 They are flagged up by 'used_in_submodule' so that they can be set
5838 DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl). Otherwise the
5839 linker chokes on multiple symbol definitions. */
5841 static void
5842 set_syms_host_assoc (gfc_symbol *sym)
5844 gfc_component *c;
5845 const char dot[2] = ".";
5846 char parent1[GFC_MAX_SYMBOL_LEN + 1];
5847 char parent2[GFC_MAX_SYMBOL_LEN + 1];
5849 if (sym == NULL)
5850 return;
5852 if (sym->attr.module_procedure)
5853 sym->attr.external = 0;
5855 sym->attr.use_assoc = 0;
5856 sym->attr.host_assoc = 1;
5857 sym->attr.used_in_submodule =1;
5859 if (sym->attr.flavor == FL_DERIVED)
5861 /* Derived types with PRIVATE components that are declared in
5862 modules other than the parent module must not be changed to be
5863 PUBLIC. The 'use-assoc' attribute must be reset so that the
5864 test in symbol.c(gfc_find_component) works correctly. This is
5865 not necessary for PRIVATE symbols since they are not read from
5866 the module. */
5867 memset(parent1, '\0', sizeof(parent1));
5868 memset(parent2, '\0', sizeof(parent2));
5869 strcpy (parent1, gfc_new_block->name);
5870 strcpy (parent2, sym->module);
5871 if (strcmp (strtok (parent1, dot), strtok (parent2, dot)) == 0)
5873 for (c = sym->components; c; c = c->next)
5874 c->attr.access = ACCESS_PUBLIC;
5876 else
5878 sym->attr.use_assoc = 1;
5879 sym->attr.host_assoc = 0;
5884 /* Parse a module subprogram. */
5886 static void
5887 parse_module (void)
5889 gfc_statement st;
5890 gfc_gsymbol *s;
5891 bool error;
5893 s = gfc_get_gsymbol (gfc_new_block->name);
5894 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
5895 gfc_global_used (s, &gfc_new_block->declared_at);
5896 else
5898 s->type = GSYM_MODULE;
5899 s->where = gfc_new_block->declared_at;
5900 s->defined = 1;
5903 /* Something is nulling the module_list after this point. This is good
5904 since it allows us to 'USE' the parent modules that the submodule
5905 inherits and to set (most) of the symbols as host associated. */
5906 if (gfc_current_state () == COMP_SUBMODULE)
5908 use_modules ();
5909 gfc_traverse_ns (gfc_current_ns, set_syms_host_assoc);
5912 st = parse_spec (ST_NONE);
5914 error = false;
5915 loop:
5916 switch (st)
5918 case ST_NONE:
5919 unexpected_eof ();
5921 case ST_CONTAINS:
5922 parse_contained (1);
5923 break;
5925 case ST_END_MODULE:
5926 case ST_END_SUBMODULE:
5927 accept_statement (st);
5928 break;
5930 default:
5931 gfc_error ("Unexpected %s statement in MODULE at %C",
5932 gfc_ascii_statement (st));
5934 error = true;
5935 reject_statement ();
5936 st = next_statement ();
5937 goto loop;
5940 /* Make sure not to free the namespace twice on error. */
5941 if (!error)
5942 s->ns = gfc_current_ns;
5946 /* Add a procedure name to the global symbol table. */
5948 static void
5949 add_global_procedure (bool sub)
5951 gfc_gsymbol *s;
5953 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5954 name is a global identifier. */
5955 if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
5957 s = gfc_get_gsymbol (gfc_new_block->name);
5959 if (s->defined
5960 || (s->type != GSYM_UNKNOWN
5961 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5963 gfc_global_used (s, &gfc_new_block->declared_at);
5964 /* Silence follow-up errors. */
5965 gfc_new_block->binding_label = NULL;
5967 else
5969 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5970 s->sym_name = gfc_new_block->name;
5971 s->where = gfc_new_block->declared_at;
5972 s->defined = 1;
5973 s->ns = gfc_current_ns;
5977 /* Don't add the symbol multiple times. */
5978 if (gfc_new_block->binding_label
5979 && (!gfc_notification_std (GFC_STD_F2008)
5980 || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
5982 s = gfc_get_gsymbol (gfc_new_block->binding_label);
5984 if (s->defined
5985 || (s->type != GSYM_UNKNOWN
5986 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5988 gfc_global_used (s, &gfc_new_block->declared_at);
5989 /* Silence follow-up errors. */
5990 gfc_new_block->binding_label = NULL;
5992 else
5994 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5995 s->sym_name = gfc_new_block->name;
5996 s->binding_label = gfc_new_block->binding_label;
5997 s->where = gfc_new_block->declared_at;
5998 s->defined = 1;
5999 s->ns = gfc_current_ns;
6005 /* Add a program to the global symbol table. */
6007 static void
6008 add_global_program (void)
6010 gfc_gsymbol *s;
6012 if (gfc_new_block == NULL)
6013 return;
6014 s = gfc_get_gsymbol (gfc_new_block->name);
6016 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
6017 gfc_global_used (s, &gfc_new_block->declared_at);
6018 else
6020 s->type = GSYM_PROGRAM;
6021 s->where = gfc_new_block->declared_at;
6022 s->defined = 1;
6023 s->ns = gfc_current_ns;
6028 /* Resolve all the program units. */
6029 static void
6030 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
6032 gfc_free_dt_list ();
6033 gfc_current_ns = gfc_global_ns_list;
6034 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6036 if (gfc_current_ns->proc_name
6037 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
6038 continue; /* Already resolved. */
6040 if (gfc_current_ns->proc_name)
6041 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
6042 gfc_resolve (gfc_current_ns);
6043 gfc_current_ns->derived_types = gfc_derived_types;
6044 gfc_derived_types = NULL;
6049 static void
6050 clean_up_modules (gfc_gsymbol *gsym)
6052 if (gsym == NULL)
6053 return;
6055 clean_up_modules (gsym->left);
6056 clean_up_modules (gsym->right);
6058 if (gsym->type != GSYM_MODULE || !gsym->ns)
6059 return;
6061 gfc_current_ns = gsym->ns;
6062 gfc_derived_types = gfc_current_ns->derived_types;
6063 gfc_done_2 ();
6064 gsym->ns = NULL;
6065 return;
6069 /* Translate all the program units. This could be in a different order
6070 to resolution if there are forward references in the file. */
6071 static void
6072 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
6074 int errors;
6076 gfc_current_ns = gfc_global_ns_list;
6077 gfc_get_errors (NULL, &errors);
6079 /* We first translate all modules to make sure that later parts
6080 of the program can use the decl. Then we translate the nonmodules. */
6082 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6084 if (!gfc_current_ns->proc_name
6085 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6086 continue;
6088 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
6089 gfc_derived_types = gfc_current_ns->derived_types;
6090 gfc_generate_module_code (gfc_current_ns);
6091 gfc_current_ns->translated = 1;
6094 gfc_current_ns = gfc_global_ns_list;
6095 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6097 if (gfc_current_ns->proc_name
6098 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
6099 continue;
6101 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
6102 gfc_derived_types = gfc_current_ns->derived_types;
6103 gfc_generate_code (gfc_current_ns);
6104 gfc_current_ns->translated = 1;
6107 /* Clean up all the namespaces after translation. */
6108 gfc_current_ns = gfc_global_ns_list;
6109 for (;gfc_current_ns;)
6111 gfc_namespace *ns;
6113 if (gfc_current_ns->proc_name
6114 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
6116 gfc_current_ns = gfc_current_ns->sibling;
6117 continue;
6120 ns = gfc_current_ns->sibling;
6121 gfc_derived_types = gfc_current_ns->derived_types;
6122 gfc_done_2 ();
6123 gfc_current_ns = ns;
6126 clean_up_modules (gfc_gsym_root);
6130 /* Top level parser. */
6132 bool
6133 gfc_parse_file (void)
6135 int seen_program, errors_before, errors;
6136 gfc_state_data top, s;
6137 gfc_statement st;
6138 locus prog_locus;
6139 gfc_namespace *next;
6141 gfc_start_source_files ();
6143 top.state = COMP_NONE;
6144 top.sym = NULL;
6145 top.previous = NULL;
6146 top.head = top.tail = NULL;
6147 top.do_variable = NULL;
6149 gfc_state_stack = &top;
6151 gfc_clear_new_st ();
6153 gfc_statement_label = NULL;
6155 if (setjmp (eof_buf))
6156 return false; /* Come here on unexpected EOF */
6158 /* Prepare the global namespace that will contain the
6159 program units. */
6160 gfc_global_ns_list = next = NULL;
6162 seen_program = 0;
6163 errors_before = 0;
6165 /* Exit early for empty files. */
6166 if (gfc_at_eof ())
6167 goto done;
6169 in_specification_block = true;
6170 loop:
6171 gfc_init_2 ();
6172 st = next_statement ();
6173 switch (st)
6175 case ST_NONE:
6176 gfc_done_2 ();
6177 goto done;
6179 case ST_PROGRAM:
6180 if (seen_program)
6181 goto duplicate_main;
6182 seen_program = 1;
6183 prog_locus = gfc_current_locus;
6185 push_state (&s, COMP_PROGRAM, gfc_new_block);
6186 main_program_symbol (gfc_current_ns, gfc_new_block->name);
6187 accept_statement (st);
6188 add_global_program ();
6189 parse_progunit (ST_NONE);
6190 goto prog_units;
6192 case ST_SUBROUTINE:
6193 add_global_procedure (true);
6194 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
6195 accept_statement (st);
6196 parse_progunit (ST_NONE);
6197 goto prog_units;
6199 case ST_FUNCTION:
6200 add_global_procedure (false);
6201 push_state (&s, COMP_FUNCTION, gfc_new_block);
6202 accept_statement (st);
6203 parse_progunit (ST_NONE);
6204 goto prog_units;
6206 case ST_BLOCK_DATA:
6207 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
6208 accept_statement (st);
6209 parse_block_data ();
6210 break;
6212 case ST_MODULE:
6213 push_state (&s, COMP_MODULE, gfc_new_block);
6214 accept_statement (st);
6216 gfc_get_errors (NULL, &errors_before);
6217 parse_module ();
6218 break;
6220 case ST_SUBMODULE:
6221 push_state (&s, COMP_SUBMODULE, gfc_new_block);
6222 accept_statement (st);
6224 gfc_get_errors (NULL, &errors_before);
6225 parse_module ();
6226 break;
6228 /* Anything else starts a nameless main program block. */
6229 default:
6230 if (seen_program)
6231 goto duplicate_main;
6232 seen_program = 1;
6233 prog_locus = gfc_current_locus;
6235 push_state (&s, COMP_PROGRAM, gfc_new_block);
6236 main_program_symbol (gfc_current_ns, "MAIN__");
6237 parse_progunit (st);
6238 goto prog_units;
6241 /* Handle the non-program units. */
6242 gfc_current_ns->code = s.head;
6244 gfc_resolve (gfc_current_ns);
6246 /* Dump the parse tree if requested. */
6247 if (flag_dump_fortran_original)
6248 gfc_dump_parse_tree (gfc_current_ns, stdout);
6250 if (flag_c_prototypes)
6251 gfc_dump_c_prototypes (gfc_current_ns, stdout);
6253 gfc_get_errors (NULL, &errors);
6254 if (s.state == COMP_MODULE || s.state == COMP_SUBMODULE)
6256 gfc_dump_module (s.sym->name, errors_before == errors);
6257 gfc_current_ns->derived_types = gfc_derived_types;
6258 gfc_derived_types = NULL;
6259 goto prog_units;
6261 else
6263 if (errors == 0)
6264 gfc_generate_code (gfc_current_ns);
6265 pop_state ();
6266 gfc_done_2 ();
6269 goto loop;
6271 prog_units:
6272 /* The main program and non-contained procedures are put
6273 in the global namespace list, so that they can be processed
6274 later and all their interfaces resolved. */
6275 gfc_current_ns->code = s.head;
6276 if (next)
6278 for (; next->sibling; next = next->sibling)
6280 next->sibling = gfc_current_ns;
6282 else
6283 gfc_global_ns_list = gfc_current_ns;
6285 next = gfc_current_ns;
6287 pop_state ();
6288 goto loop;
6290 done:
6291 /* Do the resolution. */
6292 resolve_all_program_units (gfc_global_ns_list);
6294 /* Do the parse tree dump. */
6295 gfc_current_ns = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
6297 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6298 if (!gfc_current_ns->proc_name
6299 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6301 gfc_dump_parse_tree (gfc_current_ns, stdout);
6302 fputs ("------------------------------------------\n\n", stdout);
6305 /* Do the translation. */
6306 translate_all_program_units (gfc_global_ns_list);
6308 gfc_end_source_files ();
6309 return true;
6311 duplicate_main:
6312 /* If we see a duplicate main program, shut down. If the second
6313 instance is an implied main program, i.e. data decls or executable
6314 statements, we're in for lots of errors. */
6315 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
6316 reject_statement ();
6317 gfc_done_2 ();
6318 return true;
6321 /* Return true if this state data represents an OpenACC region. */
6322 bool
6323 is_oacc (gfc_state_data *sd)
6325 switch (sd->construct->op)
6327 case EXEC_OACC_PARALLEL_LOOP:
6328 case EXEC_OACC_PARALLEL:
6329 case EXEC_OACC_KERNELS_LOOP:
6330 case EXEC_OACC_KERNELS:
6331 case EXEC_OACC_DATA:
6332 case EXEC_OACC_HOST_DATA:
6333 case EXEC_OACC_LOOP:
6334 case EXEC_OACC_UPDATE:
6335 case EXEC_OACC_WAIT:
6336 case EXEC_OACC_CACHE:
6337 case EXEC_OACC_ENTER_DATA:
6338 case EXEC_OACC_EXIT_DATA:
6339 case EXEC_OACC_ATOMIC:
6340 case EXEC_OACC_ROUTINE:
6341 return true;
6343 default:
6344 return false;