* configure.ac (LD_AS_NEEDED_OPTION, LD_NO_AS_NEEDED_OPTION): Use
[official-gcc.git] / gcc / fortran / parse.c
bloba3693a192f0e3085b839ce2ffa65276d27254aeb
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 ("change team", gfc_match_change_team, ST_CHANGE_TEAM);
455 match ("close", gfc_match_close, ST_CLOSE);
456 match ("continue", gfc_match_continue, ST_CONTINUE);
457 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
458 match ("cycle", gfc_match_cycle, ST_CYCLE);
459 match ("case", gfc_match_case, ST_CASE);
460 match ("common", gfc_match_common, ST_COMMON);
461 match ("contains", gfc_match_eos, ST_CONTAINS);
462 match ("class", gfc_match_class_is, ST_CLASS_IS);
463 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
464 break;
466 case 'd':
467 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
468 match ("data", gfc_match_data, ST_DATA);
469 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
470 break;
472 case 'e':
473 match ("end file", gfc_match_endfile, ST_END_FILE);
474 match ("end team", gfc_match_end_team, ST_END_TEAM);
475 match ("exit", gfc_match_exit, ST_EXIT);
476 match ("else", gfc_match_else, ST_ELSE);
477 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
478 match ("else if", gfc_match_elseif, ST_ELSEIF);
479 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
480 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
482 if (gfc_match_end (&st) == MATCH_YES)
483 return st;
485 match ("entry% ", gfc_match_entry, ST_ENTRY);
486 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
487 match ("external", gfc_match_external, ST_ATTR_DECL);
488 match ("event post", gfc_match_event_post, ST_EVENT_POST);
489 match ("event wait", gfc_match_event_wait, ST_EVENT_WAIT);
490 break;
492 case 'f':
493 match ("fail image", gfc_match_fail_image, ST_FAIL_IMAGE);
494 match ("final", gfc_match_final_decl, ST_FINAL);
495 match ("flush", gfc_match_flush, ST_FLUSH);
496 match ("form team", gfc_match_form_team, ST_FORM_TEAM);
497 match ("format", gfc_match_format, ST_FORMAT);
498 break;
500 case 'g':
501 match ("generic", gfc_match_generic, ST_GENERIC);
502 match ("go to", gfc_match_goto, ST_GOTO);
503 break;
505 case 'i':
506 match ("inquire", gfc_match_inquire, ST_INQUIRE);
507 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
508 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
509 match ("import", gfc_match_import, ST_IMPORT);
510 match ("interface", gfc_match_interface, ST_INTERFACE);
511 match ("intent", gfc_match_intent, ST_ATTR_DECL);
512 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
513 break;
515 case 'l':
516 match ("lock", gfc_match_lock, ST_LOCK);
517 break;
519 case 'm':
520 match ("map", gfc_match_map, ST_MAP);
521 match ("module% procedure", gfc_match_modproc, ST_MODULE_PROC);
522 match ("module", gfc_match_module, ST_MODULE);
523 break;
525 case 'n':
526 match ("nullify", gfc_match_nullify, ST_NULLIFY);
527 match ("namelist", gfc_match_namelist, ST_NAMELIST);
528 break;
530 case 'o':
531 match ("open", gfc_match_open, ST_OPEN);
532 match ("optional", gfc_match_optional, ST_ATTR_DECL);
533 break;
535 case 'p':
536 match ("print", gfc_match_print, ST_WRITE);
537 match ("pause", gfc_match_pause, ST_PAUSE);
538 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
539 if (gfc_match_private (&st) == MATCH_YES)
540 return st;
541 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
542 match ("program", gfc_match_program, ST_PROGRAM);
543 if (gfc_match_public (&st) == MATCH_YES)
544 return st;
545 match ("protected", gfc_match_protected, ST_ATTR_DECL);
546 break;
548 case 'r':
549 match ("read", gfc_match_read, ST_READ);
550 match ("return", gfc_match_return, ST_RETURN);
551 match ("rewind", gfc_match_rewind, ST_REWIND);
552 break;
554 case 's':
555 match ("structure", gfc_match_structure_decl, ST_STRUCTURE_DECL);
556 match ("sequence", gfc_match_eos, ST_SEQUENCE);
557 match ("stop", gfc_match_stop, ST_STOP);
558 match ("save", gfc_match_save, ST_ATTR_DECL);
559 match ("static", gfc_match_static, ST_ATTR_DECL);
560 match ("submodule", gfc_match_submodule, ST_SUBMODULE);
561 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
562 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
563 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
564 match ("sync team", gfc_match_sync_team, ST_SYNC_TEAM);
565 break;
567 case 't':
568 match ("target", gfc_match_target, ST_ATTR_DECL);
569 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
570 match ("type is", gfc_match_type_is, ST_TYPE_IS);
571 break;
573 case 'u':
574 match ("union", gfc_match_union, ST_UNION);
575 match ("unlock", gfc_match_unlock, ST_UNLOCK);
576 break;
578 case 'v':
579 match ("value", gfc_match_value, ST_ATTR_DECL);
580 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
581 break;
583 case 'w':
584 match ("wait", gfc_match_wait, ST_WAIT);
585 match ("write", gfc_match_write, ST_WRITE);
586 break;
589 /* All else has failed, so give up. See if any of the matchers has
590 stored an error message of some sort. */
592 if (!gfc_error_check ())
593 gfc_error_now ("Unclassifiable statement at %C");
595 reject_statement ();
597 gfc_error_recovery ();
599 return ST_NONE;
602 /* Like match and if spec_only, goto do_spec_only without actually
603 matching. */
604 #define matcha(keyword, subr, st) \
605 do { \
606 if (spec_only && gfc_match (keyword) == MATCH_YES) \
607 goto do_spec_only; \
608 else if (match_word (keyword, subr, &old_locus) \
609 == MATCH_YES) \
610 return st; \
611 else \
612 undo_new_statement (); \
613 } while (0)
615 static gfc_statement
616 decode_oacc_directive (void)
618 locus old_locus;
619 char c;
620 bool spec_only = false;
622 gfc_enforce_clean_symbol_state ();
624 gfc_clear_error (); /* Clear any pending errors. */
625 gfc_clear_warning (); /* Clear any pending warnings. */
627 if (gfc_pure (NULL))
629 gfc_error_now ("OpenACC directives at %C may not appear in PURE "
630 "procedures");
631 gfc_error_recovery ();
632 return ST_NONE;
635 if (gfc_current_state () == COMP_FUNCTION
636 && gfc_current_block ()->result->ts.kind == -1)
637 spec_only = true;
639 gfc_unset_implicit_pure (NULL);
641 old_locus = gfc_current_locus;
643 /* General OpenACC directive matching: Instead of testing every possible
644 statement, we eliminate most possibilities by peeking at the
645 first character. */
647 c = gfc_peek_ascii_char ();
649 switch (c)
651 case 'a':
652 matcha ("atomic", gfc_match_oacc_atomic, ST_OACC_ATOMIC);
653 break;
654 case 'c':
655 matcha ("cache", gfc_match_oacc_cache, ST_OACC_CACHE);
656 break;
657 case 'd':
658 matcha ("data", gfc_match_oacc_data, ST_OACC_DATA);
659 match ("declare", gfc_match_oacc_declare, ST_OACC_DECLARE);
660 break;
661 case 'e':
662 matcha ("end atomic", gfc_match_omp_eos, ST_OACC_END_ATOMIC);
663 matcha ("end data", gfc_match_omp_eos, ST_OACC_END_DATA);
664 matcha ("end host_data", gfc_match_omp_eos, ST_OACC_END_HOST_DATA);
665 matcha ("end kernels loop", gfc_match_omp_eos, ST_OACC_END_KERNELS_LOOP);
666 matcha ("end kernels", gfc_match_omp_eos, ST_OACC_END_KERNELS);
667 matcha ("end loop", gfc_match_omp_eos, ST_OACC_END_LOOP);
668 matcha ("end parallel loop", gfc_match_omp_eos,
669 ST_OACC_END_PARALLEL_LOOP);
670 matcha ("end parallel", gfc_match_omp_eos, ST_OACC_END_PARALLEL);
671 matcha ("enter data", gfc_match_oacc_enter_data, ST_OACC_ENTER_DATA);
672 matcha ("exit data", gfc_match_oacc_exit_data, ST_OACC_EXIT_DATA);
673 break;
674 case 'h':
675 matcha ("host_data", gfc_match_oacc_host_data, ST_OACC_HOST_DATA);
676 break;
677 case 'p':
678 matcha ("parallel loop", gfc_match_oacc_parallel_loop,
679 ST_OACC_PARALLEL_LOOP);
680 matcha ("parallel", gfc_match_oacc_parallel, ST_OACC_PARALLEL);
681 break;
682 case 'k':
683 matcha ("kernels loop", gfc_match_oacc_kernels_loop,
684 ST_OACC_KERNELS_LOOP);
685 matcha ("kernels", gfc_match_oacc_kernels, ST_OACC_KERNELS);
686 break;
687 case 'l':
688 matcha ("loop", gfc_match_oacc_loop, ST_OACC_LOOP);
689 break;
690 case 'r':
691 match ("routine", gfc_match_oacc_routine, ST_OACC_ROUTINE);
692 break;
693 case 'u':
694 matcha ("update", gfc_match_oacc_update, ST_OACC_UPDATE);
695 break;
696 case 'w':
697 matcha ("wait", gfc_match_oacc_wait, ST_OACC_WAIT);
698 break;
701 /* Directive not found or stored an error message.
702 Check and give up. */
704 if (gfc_error_check () == 0)
705 gfc_error_now ("Unclassifiable OpenACC directive at %C");
707 reject_statement ();
709 gfc_error_recovery ();
711 return ST_NONE;
713 do_spec_only:
714 reject_statement ();
715 gfc_clear_error ();
716 gfc_buffer_error (false);
717 gfc_current_locus = old_locus;
718 return ST_GET_FCN_CHARACTERISTICS;
721 /* Like match, but set a flag simd_matched if keyword matched
722 and if spec_only, goto do_spec_only without actually matching. */
723 #define matchs(keyword, subr, st) \
724 do { \
725 if (spec_only && gfc_match (keyword) == MATCH_YES) \
726 goto do_spec_only; \
727 if (match_word_omp_simd (keyword, subr, &old_locus, \
728 &simd_matched) == MATCH_YES) \
730 ret = st; \
731 goto finish; \
733 else \
734 undo_new_statement (); \
735 } while (0)
737 /* Like match, but don't match anything if not -fopenmp
738 and if spec_only, goto do_spec_only without actually matching. */
739 #define matcho(keyword, subr, st) \
740 do { \
741 if (!flag_openmp) \
743 else if (spec_only && gfc_match (keyword) == MATCH_YES) \
744 goto do_spec_only; \
745 else if (match_word (keyword, subr, &old_locus) \
746 == MATCH_YES) \
748 ret = st; \
749 goto finish; \
751 else \
752 undo_new_statement (); \
753 } while (0)
755 /* Like match, but set a flag simd_matched if keyword matched. */
756 #define matchds(keyword, subr, st) \
757 do { \
758 if (match_word_omp_simd (keyword, subr, &old_locus, \
759 &simd_matched) == MATCH_YES) \
761 ret = st; \
762 goto finish; \
764 else \
765 undo_new_statement (); \
766 } while (0)
768 /* Like match, but don't match anything if not -fopenmp. */
769 #define matchdo(keyword, subr, st) \
770 do { \
771 if (!flag_openmp) \
773 else if (match_word (keyword, subr, &old_locus) \
774 == MATCH_YES) \
776 ret = st; \
777 goto finish; \
779 else \
780 undo_new_statement (); \
781 } while (0)
783 static gfc_statement
784 decode_omp_directive (void)
786 locus old_locus;
787 char c;
788 bool simd_matched = false;
789 bool spec_only = false;
790 gfc_statement ret = ST_NONE;
791 bool pure_ok = true;
793 gfc_enforce_clean_symbol_state ();
795 gfc_clear_error (); /* Clear any pending errors. */
796 gfc_clear_warning (); /* Clear any pending warnings. */
798 if (gfc_current_state () == COMP_FUNCTION
799 && gfc_current_block ()->result->ts.kind == -1)
800 spec_only = true;
802 old_locus = gfc_current_locus;
804 /* General OpenMP directive matching: Instead of testing every possible
805 statement, we eliminate most possibilities by peeking at the
806 first character. */
808 c = gfc_peek_ascii_char ();
810 /* match is for directives that should be recognized only if
811 -fopenmp, matchs for directives that should be recognized
812 if either -fopenmp or -fopenmp-simd.
813 Handle only the directives allowed in PURE/ELEMENTAL procedures
814 first (those also shall not turn off implicit pure). */
815 switch (c)
817 case 'd':
818 matchds ("declare simd", gfc_match_omp_declare_simd,
819 ST_OMP_DECLARE_SIMD);
820 matchdo ("declare target", gfc_match_omp_declare_target,
821 ST_OMP_DECLARE_TARGET);
822 break;
823 case 's':
824 matchs ("simd", gfc_match_omp_simd, ST_OMP_SIMD);
825 break;
828 pure_ok = false;
829 if (flag_openmp && gfc_pure (NULL))
831 gfc_error_now ("OpenMP directives other than SIMD or DECLARE TARGET "
832 "at %C may not appear in PURE or ELEMENTAL procedures");
833 gfc_error_recovery ();
834 return ST_NONE;
837 /* match is for directives that should be recognized only if
838 -fopenmp, matchs for directives that should be recognized
839 if either -fopenmp or -fopenmp-simd. */
840 switch (c)
842 case 'a':
843 matcho ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
844 break;
845 case 'b':
846 matcho ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
847 break;
848 case 'c':
849 matcho ("cancellation% point", gfc_match_omp_cancellation_point,
850 ST_OMP_CANCELLATION_POINT);
851 matcho ("cancel", gfc_match_omp_cancel, ST_OMP_CANCEL);
852 matcho ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
853 break;
854 case 'd':
855 matchds ("declare reduction", gfc_match_omp_declare_reduction,
856 ST_OMP_DECLARE_REDUCTION);
857 matchs ("distribute parallel do simd",
858 gfc_match_omp_distribute_parallel_do_simd,
859 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD);
860 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do,
861 ST_OMP_DISTRIBUTE_PARALLEL_DO);
862 matchs ("distribute simd", gfc_match_omp_distribute_simd,
863 ST_OMP_DISTRIBUTE_SIMD);
864 matcho ("distribute", gfc_match_omp_distribute, ST_OMP_DISTRIBUTE);
865 matchs ("do simd", gfc_match_omp_do_simd, ST_OMP_DO_SIMD);
866 matcho ("do", gfc_match_omp_do, ST_OMP_DO);
867 break;
868 case 'e':
869 matcho ("end atomic", gfc_match_omp_eos, ST_OMP_END_ATOMIC);
870 matcho ("end critical", gfc_match_omp_end_critical, ST_OMP_END_CRITICAL);
871 matchs ("end distribute parallel do simd", gfc_match_omp_eos,
872 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD);
873 matcho ("end distribute parallel do", gfc_match_omp_eos,
874 ST_OMP_END_DISTRIBUTE_PARALLEL_DO);
875 matchs ("end distribute simd", gfc_match_omp_eos,
876 ST_OMP_END_DISTRIBUTE_SIMD);
877 matcho ("end distribute", gfc_match_omp_eos, ST_OMP_END_DISTRIBUTE);
878 matchs ("end do simd", gfc_match_omp_end_nowait, ST_OMP_END_DO_SIMD);
879 matcho ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
880 matchs ("end simd", gfc_match_omp_eos, ST_OMP_END_SIMD);
881 matcho ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
882 matchs ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
883 matchs ("end parallel do simd", gfc_match_omp_eos,
884 ST_OMP_END_PARALLEL_DO_SIMD);
885 matcho ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
886 matcho ("end parallel sections", gfc_match_omp_eos,
887 ST_OMP_END_PARALLEL_SECTIONS);
888 matcho ("end parallel workshare", gfc_match_omp_eos,
889 ST_OMP_END_PARALLEL_WORKSHARE);
890 matcho ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
891 matcho ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
892 matcho ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
893 matcho ("end target data", gfc_match_omp_eos, ST_OMP_END_TARGET_DATA);
894 matchs ("end target parallel do simd", gfc_match_omp_eos,
895 ST_OMP_END_TARGET_PARALLEL_DO_SIMD);
896 matcho ("end target parallel do", gfc_match_omp_eos,
897 ST_OMP_END_TARGET_PARALLEL_DO);
898 matcho ("end target parallel", gfc_match_omp_eos,
899 ST_OMP_END_TARGET_PARALLEL);
900 matchs ("end target simd", gfc_match_omp_eos, ST_OMP_END_TARGET_SIMD);
901 matchs ("end target teams distribute parallel do simd",
902 gfc_match_omp_eos,
903 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
904 matcho ("end target teams distribute parallel do", gfc_match_omp_eos,
905 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
906 matchs ("end target teams distribute simd", gfc_match_omp_eos,
907 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD);
908 matcho ("end target teams distribute", gfc_match_omp_eos,
909 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE);
910 matcho ("end target teams", gfc_match_omp_eos, ST_OMP_END_TARGET_TEAMS);
911 matcho ("end target", gfc_match_omp_eos, ST_OMP_END_TARGET);
912 matcho ("end taskgroup", gfc_match_omp_eos, ST_OMP_END_TASKGROUP);
913 matchs ("end taskloop simd", gfc_match_omp_eos,
914 ST_OMP_END_TASKLOOP_SIMD);
915 matcho ("end taskloop", gfc_match_omp_eos, ST_OMP_END_TASKLOOP);
916 matcho ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
917 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos,
918 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
919 matcho ("end teams distribute parallel do", gfc_match_omp_eos,
920 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO);
921 matchs ("end teams distribute simd", gfc_match_omp_eos,
922 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD);
923 matcho ("end teams distribute", gfc_match_omp_eos,
924 ST_OMP_END_TEAMS_DISTRIBUTE);
925 matcho ("end teams", gfc_match_omp_eos, ST_OMP_END_TEAMS);
926 matcho ("end workshare", gfc_match_omp_end_nowait,
927 ST_OMP_END_WORKSHARE);
928 break;
929 case 'f':
930 matcho ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
931 break;
932 case 'm':
933 matcho ("master", gfc_match_omp_master, ST_OMP_MASTER);
934 break;
935 case 'o':
936 if (gfc_match ("ordered depend (") == MATCH_YES)
938 gfc_current_locus = old_locus;
939 if (!flag_openmp)
940 break;
941 matcho ("ordered", gfc_match_omp_ordered_depend,
942 ST_OMP_ORDERED_DEPEND);
944 else
945 matchs ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
946 break;
947 case 'p':
948 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
949 ST_OMP_PARALLEL_DO_SIMD);
950 matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
951 matcho ("parallel sections", gfc_match_omp_parallel_sections,
952 ST_OMP_PARALLEL_SECTIONS);
953 matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
954 ST_OMP_PARALLEL_WORKSHARE);
955 matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
956 break;
957 case 's':
958 matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
959 matcho ("section", gfc_match_omp_eos, ST_OMP_SECTION);
960 matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
961 break;
962 case 't':
963 matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
964 matcho ("target enter data", gfc_match_omp_target_enter_data,
965 ST_OMP_TARGET_ENTER_DATA);
966 matcho ("target exit data", gfc_match_omp_target_exit_data,
967 ST_OMP_TARGET_EXIT_DATA);
968 matchs ("target parallel do simd", gfc_match_omp_target_parallel_do_simd,
969 ST_OMP_TARGET_PARALLEL_DO_SIMD);
970 matcho ("target parallel do", gfc_match_omp_target_parallel_do,
971 ST_OMP_TARGET_PARALLEL_DO);
972 matcho ("target parallel", gfc_match_omp_target_parallel,
973 ST_OMP_TARGET_PARALLEL);
974 matchs ("target simd", gfc_match_omp_target_simd, ST_OMP_TARGET_SIMD);
975 matchs ("target teams distribute parallel do simd",
976 gfc_match_omp_target_teams_distribute_parallel_do_simd,
977 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
978 matcho ("target teams distribute parallel do",
979 gfc_match_omp_target_teams_distribute_parallel_do,
980 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
981 matchs ("target teams distribute simd",
982 gfc_match_omp_target_teams_distribute_simd,
983 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
984 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
985 ST_OMP_TARGET_TEAMS_DISTRIBUTE);
986 matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
987 matcho ("target update", gfc_match_omp_target_update,
988 ST_OMP_TARGET_UPDATE);
989 matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
990 matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
991 matchs ("taskloop simd", gfc_match_omp_taskloop_simd,
992 ST_OMP_TASKLOOP_SIMD);
993 matcho ("taskloop", gfc_match_omp_taskloop, ST_OMP_TASKLOOP);
994 matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
995 matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
996 matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
997 matchs ("teams distribute parallel do simd",
998 gfc_match_omp_teams_distribute_parallel_do_simd,
999 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
1000 matcho ("teams distribute parallel do",
1001 gfc_match_omp_teams_distribute_parallel_do,
1002 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
1003 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
1004 ST_OMP_TEAMS_DISTRIBUTE_SIMD);
1005 matcho ("teams distribute", gfc_match_omp_teams_distribute,
1006 ST_OMP_TEAMS_DISTRIBUTE);
1007 matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
1008 matchdo ("threadprivate", gfc_match_omp_threadprivate,
1009 ST_OMP_THREADPRIVATE);
1010 break;
1011 case 'w':
1012 matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
1013 break;
1016 /* All else has failed, so give up. See if any of the matchers has
1017 stored an error message of some sort. Don't error out if
1018 not -fopenmp and simd_matched is false, i.e. if a directive other
1019 than one marked with match has been seen. */
1021 if (flag_openmp || simd_matched)
1023 if (!gfc_error_check ())
1024 gfc_error_now ("Unclassifiable OpenMP directive at %C");
1027 reject_statement ();
1029 gfc_error_recovery ();
1031 return ST_NONE;
1033 finish:
1034 if (!pure_ok)
1036 gfc_unset_implicit_pure (NULL);
1038 if (!flag_openmp && gfc_pure (NULL))
1040 gfc_error_now ("OpenMP directives other than SIMD or DECLARE TARGET "
1041 "at %C may not appear in PURE or ELEMENTAL "
1042 "procedures");
1043 reject_statement ();
1044 gfc_error_recovery ();
1045 return ST_NONE;
1048 return ret;
1050 do_spec_only:
1051 reject_statement ();
1052 gfc_clear_error ();
1053 gfc_buffer_error (false);
1054 gfc_current_locus = old_locus;
1055 return ST_GET_FCN_CHARACTERISTICS;
1058 static gfc_statement
1059 decode_gcc_attribute (void)
1061 locus old_locus;
1063 gfc_enforce_clean_symbol_state ();
1065 gfc_clear_error (); /* Clear any pending errors. */
1066 gfc_clear_warning (); /* Clear any pending warnings. */
1067 old_locus = gfc_current_locus;
1069 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
1070 match ("unroll", gfc_match_gcc_unroll, ST_NONE);
1072 /* All else has failed, so give up. See if any of the matchers has
1073 stored an error message of some sort. */
1075 if (!gfc_error_check ())
1076 gfc_error_now ("Unclassifiable GCC directive at %C");
1078 reject_statement ();
1080 gfc_error_recovery ();
1082 return ST_NONE;
1085 #undef match
1087 /* Assert next length characters to be equal to token in free form. */
1089 static void
1090 verify_token_free (const char* token, int length, bool last_was_use_stmt)
1092 int i;
1093 char c;
1095 c = gfc_next_ascii_char ();
1096 for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
1097 gcc_assert (c == token[i]);
1099 gcc_assert (gfc_is_whitespace(c));
1100 gfc_gobble_whitespace ();
1101 if (last_was_use_stmt)
1102 use_modules ();
1105 /* Get the next statement in free form source. */
1107 static gfc_statement
1108 next_free (void)
1110 match m;
1111 int i, cnt, at_bol;
1112 char c;
1114 at_bol = gfc_at_bol ();
1115 gfc_gobble_whitespace ();
1117 c = gfc_peek_ascii_char ();
1119 if (ISDIGIT (c))
1121 char d;
1123 /* Found a statement label? */
1124 m = gfc_match_st_label (&gfc_statement_label);
1126 d = gfc_peek_ascii_char ();
1127 if (m != MATCH_YES || !gfc_is_whitespace (d))
1129 gfc_match_small_literal_int (&i, &cnt);
1131 if (cnt > 5)
1132 gfc_error_now ("Too many digits in statement label at %C");
1134 if (i == 0)
1135 gfc_error_now ("Zero is not a valid statement label at %C");
1138 c = gfc_next_ascii_char ();
1139 while (ISDIGIT(c));
1141 if (!gfc_is_whitespace (c))
1142 gfc_error_now ("Non-numeric character in statement label at %C");
1144 return ST_NONE;
1146 else
1148 label_locus = gfc_current_locus;
1150 gfc_gobble_whitespace ();
1152 if (at_bol && gfc_peek_ascii_char () == ';')
1154 gfc_error_now ("Semicolon at %C needs to be preceded by "
1155 "statement");
1156 gfc_next_ascii_char (); /* Eat up the semicolon. */
1157 return ST_NONE;
1160 if (gfc_match_eos () == MATCH_YES)
1161 gfc_error_now ("Statement label without statement at %L",
1162 &label_locus);
1165 else if (c == '!')
1167 /* Comments have already been skipped by the time we get here,
1168 except for GCC attributes and OpenMP/OpenACC directives. */
1170 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
1171 c = gfc_peek_ascii_char ();
1173 if (c == 'g')
1175 int i;
1177 c = gfc_next_ascii_char ();
1178 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
1179 gcc_assert (c == "gcc$"[i]);
1181 gfc_gobble_whitespace ();
1182 return decode_gcc_attribute ();
1185 else if (c == '$')
1187 /* Since both OpenMP and OpenACC directives starts with
1188 !$ character sequence, we must check all flags combinations */
1189 if ((flag_openmp || flag_openmp_simd)
1190 && !flag_openacc)
1192 verify_token_free ("$omp", 4, last_was_use_stmt);
1193 return decode_omp_directive ();
1195 else if ((flag_openmp || flag_openmp_simd)
1196 && flag_openacc)
1198 gfc_next_ascii_char (); /* Eat up dollar character */
1199 c = gfc_peek_ascii_char ();
1201 if (c == 'o')
1203 verify_token_free ("omp", 3, last_was_use_stmt);
1204 return decode_omp_directive ();
1206 else if (c == 'a')
1208 verify_token_free ("acc", 3, last_was_use_stmt);
1209 return decode_oacc_directive ();
1212 else if (flag_openacc)
1214 verify_token_free ("$acc", 4, last_was_use_stmt);
1215 return decode_oacc_directive ();
1218 gcc_unreachable ();
1221 if (at_bol && c == ';')
1223 if (!(gfc_option.allow_std & GFC_STD_F2008))
1224 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1225 "statement");
1226 gfc_next_ascii_char (); /* Eat up the semicolon. */
1227 return ST_NONE;
1230 return decode_statement ();
1233 /* Assert next length characters to be equal to token in fixed form. */
1235 static bool
1236 verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
1238 int i;
1239 char c = gfc_next_char_literal (NONSTRING);
1241 for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
1242 gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
1244 if (c != ' ' && c != '0')
1246 gfc_buffer_error (false);
1247 gfc_error ("Bad continuation line at %C");
1248 return false;
1250 if (last_was_use_stmt)
1251 use_modules ();
1253 return true;
1256 /* Get the next statement in fixed-form source. */
1258 static gfc_statement
1259 next_fixed (void)
1261 int label, digit_flag, i;
1262 locus loc;
1263 gfc_char_t c;
1265 if (!gfc_at_bol ())
1266 return decode_statement ();
1268 /* Skip past the current label field, parsing a statement label if
1269 one is there. This is a weird number parser, since the number is
1270 contained within five columns and can have any kind of embedded
1271 spaces. We also check for characters that make the rest of the
1272 line a comment. */
1274 label = 0;
1275 digit_flag = 0;
1277 for (i = 0; i < 5; i++)
1279 c = gfc_next_char_literal (NONSTRING);
1281 switch (c)
1283 case ' ':
1284 break;
1286 case '0':
1287 case '1':
1288 case '2':
1289 case '3':
1290 case '4':
1291 case '5':
1292 case '6':
1293 case '7':
1294 case '8':
1295 case '9':
1296 label = label * 10 + ((unsigned char) c - '0');
1297 label_locus = gfc_current_locus;
1298 digit_flag = 1;
1299 break;
1301 /* Comments have already been skipped by the time we get
1302 here, except for GCC attributes and OpenMP directives. */
1304 case '*':
1305 c = gfc_next_char_literal (NONSTRING);
1307 if (TOLOWER (c) == 'g')
1309 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
1310 gcc_assert (TOLOWER (c) == "gcc$"[i]);
1312 return decode_gcc_attribute ();
1314 else if (c == '$')
1316 if ((flag_openmp || flag_openmp_simd)
1317 && !flag_openacc)
1319 if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
1320 return ST_NONE;
1321 return decode_omp_directive ();
1323 else if ((flag_openmp || flag_openmp_simd)
1324 && flag_openacc)
1326 c = gfc_next_char_literal(NONSTRING);
1327 if (c == 'o' || c == 'O')
1329 if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
1330 return ST_NONE;
1331 return decode_omp_directive ();
1333 else if (c == 'a' || c == 'A')
1335 if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
1336 return ST_NONE;
1337 return decode_oacc_directive ();
1340 else if (flag_openacc)
1342 if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
1343 return ST_NONE;
1344 return decode_oacc_directive ();
1347 gcc_fallthrough ();
1349 /* Comments have already been skipped by the time we get
1350 here so don't bother checking for them. */
1352 default:
1353 gfc_buffer_error (false);
1354 gfc_error ("Non-numeric character in statement label at %C");
1355 return ST_NONE;
1359 if (digit_flag)
1361 if (label == 0)
1362 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1363 else
1365 /* We've found a valid statement label. */
1366 gfc_statement_label = gfc_get_st_label (label);
1370 /* Since this line starts a statement, it cannot be a continuation
1371 of a previous statement. If we see something here besides a
1372 space or zero, it must be a bad continuation line. */
1374 c = gfc_next_char_literal (NONSTRING);
1375 if (c == '\n')
1376 goto blank_line;
1378 if (c != ' ' && c != '0')
1380 gfc_buffer_error (false);
1381 gfc_error ("Bad continuation line at %C");
1382 return ST_NONE;
1385 /* Now that we've taken care of the statement label columns, we have
1386 to make sure that the first nonblank character is not a '!'. If
1387 it is, the rest of the line is a comment. */
1391 loc = gfc_current_locus;
1392 c = gfc_next_char_literal (NONSTRING);
1394 while (gfc_is_whitespace (c));
1396 if (c == '!')
1397 goto blank_line;
1398 gfc_current_locus = loc;
1400 if (c == ';')
1402 if (digit_flag)
1403 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1404 else if (!(gfc_option.allow_std & GFC_STD_F2008))
1405 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1406 "statement");
1407 return ST_NONE;
1410 if (gfc_match_eos () == MATCH_YES)
1411 goto blank_line;
1413 /* At this point, we've got a nonblank statement to parse. */
1414 return decode_statement ();
1416 blank_line:
1417 if (digit_flag)
1418 gfc_error_now ("Statement label without statement at %L", &label_locus);
1420 gfc_current_locus.lb->truncated = 0;
1421 gfc_advance_line ();
1422 return ST_NONE;
1426 /* Return the next non-ST_NONE statement to the caller. We also worry
1427 about including files and the ends of include files at this stage. */
1429 static gfc_statement
1430 next_statement (void)
1432 gfc_statement st;
1433 locus old_locus;
1435 gfc_enforce_clean_symbol_state ();
1437 gfc_new_block = NULL;
1439 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
1440 gfc_current_ns->old_data = gfc_current_ns->data;
1441 for (;;)
1443 gfc_statement_label = NULL;
1444 gfc_buffer_error (true);
1446 if (gfc_at_eol ())
1447 gfc_advance_line ();
1449 gfc_skip_comments ();
1451 if (gfc_at_end ())
1453 st = ST_NONE;
1454 break;
1457 if (gfc_define_undef_line ())
1458 continue;
1460 old_locus = gfc_current_locus;
1462 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
1464 if (st != ST_NONE)
1465 break;
1468 gfc_buffer_error (false);
1470 if (st == ST_GET_FCN_CHARACTERISTICS)
1472 if (gfc_statement_label != NULL)
1474 gfc_free_st_label (gfc_statement_label);
1475 gfc_statement_label = NULL;
1477 gfc_current_locus = old_locus;
1480 if (st != ST_NONE)
1481 check_statement_label (st);
1483 return st;
1487 /****************************** Parser ***********************************/
1489 /* The parser subroutines are of type 'try' that fail if the file ends
1490 unexpectedly. */
1492 /* Macros that expand to case-labels for various classes of
1493 statements. Start with executable statements that directly do
1494 things. */
1496 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1497 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1498 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1499 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1500 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1501 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1502 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1503 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1504 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1505 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1506 case ST_OMP_TARGET_UPDATE: case ST_OMP_TARGET_ENTER_DATA: \
1507 case ST_OMP_TARGET_EXIT_DATA: case ST_OMP_ORDERED_DEPEND: \
1508 case ST_ERROR_STOP: case ST_SYNC_ALL: \
1509 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1510 case ST_FORM_TEAM: case ST_CHANGE_TEAM: \
1511 case ST_END_TEAM: case ST_SYNC_TEAM: \
1512 case ST_EVENT_POST: case ST_EVENT_WAIT: case ST_FAIL_IMAGE: \
1513 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1514 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1516 /* Statements that mark other executable statements. */
1518 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1519 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1520 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1521 case ST_OMP_PARALLEL: \
1522 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1523 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1524 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1525 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1526 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1527 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1528 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1529 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1530 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1531 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1532 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1533 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1534 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1535 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1536 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1537 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1538 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_TARGET_PARALLEL: \
1539 case ST_OMP_TARGET_PARALLEL_DO: case ST_OMP_TARGET_PARALLEL_DO_SIMD: \
1540 case ST_OMP_TARGET_SIMD: case ST_OMP_TASKLOOP: case ST_OMP_TASKLOOP_SIMD: \
1541 case ST_CRITICAL: \
1542 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1543 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: \
1544 case ST_OACC_KERNELS_LOOP: case ST_OACC_ATOMIC
1546 /* Declaration statements */
1548 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1549 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1550 case ST_TYPE: case ST_INTERFACE: case ST_PROCEDURE: case ST_OACC_ROUTINE: \
1551 case ST_OACC_DECLARE
1553 /* OpenMP declaration statements. */
1555 #define case_omp_decl case ST_OMP_THREADPRIVATE: case ST_OMP_DECLARE_SIMD: \
1556 case ST_OMP_DECLARE_TARGET: case ST_OMP_DECLARE_REDUCTION
1558 /* Block end statements. Errors associated with interchanging these
1559 are detected in gfc_match_end(). */
1561 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1562 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1563 case ST_END_BLOCK: case ST_END_ASSOCIATE
1566 /* Push a new state onto the stack. */
1568 static void
1569 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
1571 p->state = new_state;
1572 p->previous = gfc_state_stack;
1573 p->sym = sym;
1574 p->head = p->tail = NULL;
1575 p->do_variable = NULL;
1576 if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
1577 p->ext.oacc_declare_clauses = NULL;
1579 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1580 construct statement was accepted right before pushing the state. Thus,
1581 the construct's gfc_code is available as tail of the parent state. */
1582 gcc_assert (gfc_state_stack);
1583 p->construct = gfc_state_stack->tail;
1585 gfc_state_stack = p;
1589 /* Pop the current state. */
1590 static void
1591 pop_state (void)
1593 gfc_state_stack = gfc_state_stack->previous;
1597 /* Try to find the given state in the state stack. */
1599 bool
1600 gfc_find_state (gfc_compile_state state)
1602 gfc_state_data *p;
1604 for (p = gfc_state_stack; p; p = p->previous)
1605 if (p->state == state)
1606 break;
1608 return (p == NULL) ? false : true;
1612 /* Starts a new level in the statement list. */
1614 static gfc_code *
1615 new_level (gfc_code *q)
1617 gfc_code *p;
1619 p = q->block = gfc_get_code (EXEC_NOP);
1621 gfc_state_stack->head = gfc_state_stack->tail = p;
1623 return p;
1627 /* Add the current new_st code structure and adds it to the current
1628 program unit. As a side-effect, it zeroes the new_st. */
1630 static gfc_code *
1631 add_statement (void)
1633 gfc_code *p;
1635 p = XCNEW (gfc_code);
1636 *p = new_st;
1638 p->loc = gfc_current_locus;
1640 if (gfc_state_stack->head == NULL)
1641 gfc_state_stack->head = p;
1642 else
1643 gfc_state_stack->tail->next = p;
1645 while (p->next != NULL)
1646 p = p->next;
1648 gfc_state_stack->tail = p;
1650 gfc_clear_new_st ();
1652 return p;
1656 /* Frees everything associated with the current statement. */
1658 static void
1659 undo_new_statement (void)
1661 gfc_free_statements (new_st.block);
1662 gfc_free_statements (new_st.next);
1663 gfc_free_statement (&new_st);
1664 gfc_clear_new_st ();
1668 /* If the current statement has a statement label, make sure that it
1669 is allowed to, or should have one. */
1671 static void
1672 check_statement_label (gfc_statement st)
1674 gfc_sl_type type;
1676 if (gfc_statement_label == NULL)
1678 if (st == ST_FORMAT)
1679 gfc_error ("FORMAT statement at %L does not have a statement label",
1680 &new_st.loc);
1681 return;
1684 switch (st)
1686 case ST_END_PROGRAM:
1687 case ST_END_FUNCTION:
1688 case ST_END_SUBROUTINE:
1689 case ST_ENDDO:
1690 case ST_ENDIF:
1691 case ST_END_SELECT:
1692 case ST_END_CRITICAL:
1693 case ST_END_BLOCK:
1694 case ST_END_ASSOCIATE:
1695 case_executable:
1696 case_exec_markers:
1697 if (st == ST_ENDDO || st == ST_CONTINUE)
1698 type = ST_LABEL_DO_TARGET;
1699 else
1700 type = ST_LABEL_TARGET;
1701 break;
1703 case ST_FORMAT:
1704 type = ST_LABEL_FORMAT;
1705 break;
1707 /* Statement labels are not restricted from appearing on a
1708 particular line. However, there are plenty of situations
1709 where the resulting label can't be referenced. */
1711 default:
1712 type = ST_LABEL_BAD_TARGET;
1713 break;
1716 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1718 new_st.here = gfc_statement_label;
1722 /* Figures out what the enclosing program unit is. This will be a
1723 function, subroutine, program, block data or module. */
1725 gfc_state_data *
1726 gfc_enclosing_unit (gfc_compile_state * result)
1728 gfc_state_data *p;
1730 for (p = gfc_state_stack; p; p = p->previous)
1731 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1732 || p->state == COMP_MODULE || p->state == COMP_SUBMODULE
1733 || p->state == COMP_BLOCK_DATA || p->state == COMP_PROGRAM)
1736 if (result != NULL)
1737 *result = p->state;
1738 return p;
1741 if (result != NULL)
1742 *result = COMP_PROGRAM;
1743 return NULL;
1747 /* Translate a statement enum to a string. */
1749 const char *
1750 gfc_ascii_statement (gfc_statement st)
1752 const char *p;
1754 switch (st)
1756 case ST_ARITHMETIC_IF:
1757 p = _("arithmetic IF");
1758 break;
1759 case ST_ALLOCATE:
1760 p = "ALLOCATE";
1761 break;
1762 case ST_ASSOCIATE:
1763 p = "ASSOCIATE";
1764 break;
1765 case ST_ATTR_DECL:
1766 p = _("attribute declaration");
1767 break;
1768 case ST_BACKSPACE:
1769 p = "BACKSPACE";
1770 break;
1771 case ST_BLOCK:
1772 p = "BLOCK";
1773 break;
1774 case ST_BLOCK_DATA:
1775 p = "BLOCK DATA";
1776 break;
1777 case ST_CALL:
1778 p = "CALL";
1779 break;
1780 case ST_CASE:
1781 p = "CASE";
1782 break;
1783 case ST_CLOSE:
1784 p = "CLOSE";
1785 break;
1786 case ST_COMMON:
1787 p = "COMMON";
1788 break;
1789 case ST_CONTINUE:
1790 p = "CONTINUE";
1791 break;
1792 case ST_CONTAINS:
1793 p = "CONTAINS";
1794 break;
1795 case ST_CRITICAL:
1796 p = "CRITICAL";
1797 break;
1798 case ST_CYCLE:
1799 p = "CYCLE";
1800 break;
1801 case ST_DATA_DECL:
1802 p = _("data declaration");
1803 break;
1804 case ST_DATA:
1805 p = "DATA";
1806 break;
1807 case ST_DEALLOCATE:
1808 p = "DEALLOCATE";
1809 break;
1810 case ST_MAP:
1811 p = "MAP";
1812 break;
1813 case ST_UNION:
1814 p = "UNION";
1815 break;
1816 case ST_STRUCTURE_DECL:
1817 p = "STRUCTURE";
1818 break;
1819 case ST_DERIVED_DECL:
1820 p = _("derived type declaration");
1821 break;
1822 case ST_DO:
1823 p = "DO";
1824 break;
1825 case ST_ELSE:
1826 p = "ELSE";
1827 break;
1828 case ST_ELSEIF:
1829 p = "ELSE IF";
1830 break;
1831 case ST_ELSEWHERE:
1832 p = "ELSEWHERE";
1833 break;
1834 case ST_EVENT_POST:
1835 p = "EVENT POST";
1836 break;
1837 case ST_EVENT_WAIT:
1838 p = "EVENT WAIT";
1839 break;
1840 case ST_FAIL_IMAGE:
1841 p = "FAIL IMAGE";
1842 break;
1843 case ST_CHANGE_TEAM:
1844 p = "CHANGE TEAM";
1845 break;
1846 case ST_END_TEAM:
1847 p = "END TEAM";
1848 break;
1849 case ST_FORM_TEAM:
1850 p = "FORM TEAM";
1851 break;
1852 case ST_SYNC_TEAM:
1853 p = "SYNC TEAM";
1854 break;
1855 case ST_END_ASSOCIATE:
1856 p = "END ASSOCIATE";
1857 break;
1858 case ST_END_BLOCK:
1859 p = "END BLOCK";
1860 break;
1861 case ST_END_BLOCK_DATA:
1862 p = "END BLOCK DATA";
1863 break;
1864 case ST_END_CRITICAL:
1865 p = "END CRITICAL";
1866 break;
1867 case ST_ENDDO:
1868 p = "END DO";
1869 break;
1870 case ST_END_FILE:
1871 p = "END FILE";
1872 break;
1873 case ST_END_FORALL:
1874 p = "END FORALL";
1875 break;
1876 case ST_END_FUNCTION:
1877 p = "END FUNCTION";
1878 break;
1879 case ST_ENDIF:
1880 p = "END IF";
1881 break;
1882 case ST_END_INTERFACE:
1883 p = "END INTERFACE";
1884 break;
1885 case ST_END_MODULE:
1886 p = "END MODULE";
1887 break;
1888 case ST_END_SUBMODULE:
1889 p = "END SUBMODULE";
1890 break;
1891 case ST_END_PROGRAM:
1892 p = "END PROGRAM";
1893 break;
1894 case ST_END_SELECT:
1895 p = "END SELECT";
1896 break;
1897 case ST_END_SUBROUTINE:
1898 p = "END SUBROUTINE";
1899 break;
1900 case ST_END_WHERE:
1901 p = "END WHERE";
1902 break;
1903 case ST_END_STRUCTURE:
1904 p = "END STRUCTURE";
1905 break;
1906 case ST_END_UNION:
1907 p = "END UNION";
1908 break;
1909 case ST_END_MAP:
1910 p = "END MAP";
1911 break;
1912 case ST_END_TYPE:
1913 p = "END TYPE";
1914 break;
1915 case ST_ENTRY:
1916 p = "ENTRY";
1917 break;
1918 case ST_EQUIVALENCE:
1919 p = "EQUIVALENCE";
1920 break;
1921 case ST_ERROR_STOP:
1922 p = "ERROR STOP";
1923 break;
1924 case ST_EXIT:
1925 p = "EXIT";
1926 break;
1927 case ST_FLUSH:
1928 p = "FLUSH";
1929 break;
1930 case ST_FORALL_BLOCK: /* Fall through */
1931 case ST_FORALL:
1932 p = "FORALL";
1933 break;
1934 case ST_FORMAT:
1935 p = "FORMAT";
1936 break;
1937 case ST_FUNCTION:
1938 p = "FUNCTION";
1939 break;
1940 case ST_GENERIC:
1941 p = "GENERIC";
1942 break;
1943 case ST_GOTO:
1944 p = "GOTO";
1945 break;
1946 case ST_IF_BLOCK:
1947 p = _("block IF");
1948 break;
1949 case ST_IMPLICIT:
1950 p = "IMPLICIT";
1951 break;
1952 case ST_IMPLICIT_NONE:
1953 p = "IMPLICIT NONE";
1954 break;
1955 case ST_IMPLIED_ENDDO:
1956 p = _("implied END DO");
1957 break;
1958 case ST_IMPORT:
1959 p = "IMPORT";
1960 break;
1961 case ST_INQUIRE:
1962 p = "INQUIRE";
1963 break;
1964 case ST_INTERFACE:
1965 p = "INTERFACE";
1966 break;
1967 case ST_LOCK:
1968 p = "LOCK";
1969 break;
1970 case ST_PARAMETER:
1971 p = "PARAMETER";
1972 break;
1973 case ST_PRIVATE:
1974 p = "PRIVATE";
1975 break;
1976 case ST_PUBLIC:
1977 p = "PUBLIC";
1978 break;
1979 case ST_MODULE:
1980 p = "MODULE";
1981 break;
1982 case ST_SUBMODULE:
1983 p = "SUBMODULE";
1984 break;
1985 case ST_PAUSE:
1986 p = "PAUSE";
1987 break;
1988 case ST_MODULE_PROC:
1989 p = "MODULE PROCEDURE";
1990 break;
1991 case ST_NAMELIST:
1992 p = "NAMELIST";
1993 break;
1994 case ST_NULLIFY:
1995 p = "NULLIFY";
1996 break;
1997 case ST_OPEN:
1998 p = "OPEN";
1999 break;
2000 case ST_PROGRAM:
2001 p = "PROGRAM";
2002 break;
2003 case ST_PROCEDURE:
2004 p = "PROCEDURE";
2005 break;
2006 case ST_READ:
2007 p = "READ";
2008 break;
2009 case ST_RETURN:
2010 p = "RETURN";
2011 break;
2012 case ST_REWIND:
2013 p = "REWIND";
2014 break;
2015 case ST_STOP:
2016 p = "STOP";
2017 break;
2018 case ST_SYNC_ALL:
2019 p = "SYNC ALL";
2020 break;
2021 case ST_SYNC_IMAGES:
2022 p = "SYNC IMAGES";
2023 break;
2024 case ST_SYNC_MEMORY:
2025 p = "SYNC MEMORY";
2026 break;
2027 case ST_SUBROUTINE:
2028 p = "SUBROUTINE";
2029 break;
2030 case ST_TYPE:
2031 p = "TYPE";
2032 break;
2033 case ST_UNLOCK:
2034 p = "UNLOCK";
2035 break;
2036 case ST_USE:
2037 p = "USE";
2038 break;
2039 case ST_WHERE_BLOCK: /* Fall through */
2040 case ST_WHERE:
2041 p = "WHERE";
2042 break;
2043 case ST_WAIT:
2044 p = "WAIT";
2045 break;
2046 case ST_WRITE:
2047 p = "WRITE";
2048 break;
2049 case ST_ASSIGNMENT:
2050 p = _("assignment");
2051 break;
2052 case ST_POINTER_ASSIGNMENT:
2053 p = _("pointer assignment");
2054 break;
2055 case ST_SELECT_CASE:
2056 p = "SELECT CASE";
2057 break;
2058 case ST_SELECT_TYPE:
2059 p = "SELECT TYPE";
2060 break;
2061 case ST_TYPE_IS:
2062 p = "TYPE IS";
2063 break;
2064 case ST_CLASS_IS:
2065 p = "CLASS IS";
2066 break;
2067 case ST_SEQUENCE:
2068 p = "SEQUENCE";
2069 break;
2070 case ST_SIMPLE_IF:
2071 p = _("simple IF");
2072 break;
2073 case ST_STATEMENT_FUNCTION:
2074 p = "STATEMENT FUNCTION";
2075 break;
2076 case ST_LABEL_ASSIGNMENT:
2077 p = "LABEL ASSIGNMENT";
2078 break;
2079 case ST_ENUM:
2080 p = "ENUM DEFINITION";
2081 break;
2082 case ST_ENUMERATOR:
2083 p = "ENUMERATOR DEFINITION";
2084 break;
2085 case ST_END_ENUM:
2086 p = "END ENUM";
2087 break;
2088 case ST_OACC_PARALLEL_LOOP:
2089 p = "!$ACC PARALLEL LOOP";
2090 break;
2091 case ST_OACC_END_PARALLEL_LOOP:
2092 p = "!$ACC END PARALLEL LOOP";
2093 break;
2094 case ST_OACC_PARALLEL:
2095 p = "!$ACC PARALLEL";
2096 break;
2097 case ST_OACC_END_PARALLEL:
2098 p = "!$ACC END PARALLEL";
2099 break;
2100 case ST_OACC_KERNELS:
2101 p = "!$ACC KERNELS";
2102 break;
2103 case ST_OACC_END_KERNELS:
2104 p = "!$ACC END KERNELS";
2105 break;
2106 case ST_OACC_KERNELS_LOOP:
2107 p = "!$ACC KERNELS LOOP";
2108 break;
2109 case ST_OACC_END_KERNELS_LOOP:
2110 p = "!$ACC END KERNELS LOOP";
2111 break;
2112 case ST_OACC_DATA:
2113 p = "!$ACC DATA";
2114 break;
2115 case ST_OACC_END_DATA:
2116 p = "!$ACC END DATA";
2117 break;
2118 case ST_OACC_HOST_DATA:
2119 p = "!$ACC HOST_DATA";
2120 break;
2121 case ST_OACC_END_HOST_DATA:
2122 p = "!$ACC END HOST_DATA";
2123 break;
2124 case ST_OACC_LOOP:
2125 p = "!$ACC LOOP";
2126 break;
2127 case ST_OACC_END_LOOP:
2128 p = "!$ACC END LOOP";
2129 break;
2130 case ST_OACC_DECLARE:
2131 p = "!$ACC DECLARE";
2132 break;
2133 case ST_OACC_UPDATE:
2134 p = "!$ACC UPDATE";
2135 break;
2136 case ST_OACC_WAIT:
2137 p = "!$ACC WAIT";
2138 break;
2139 case ST_OACC_CACHE:
2140 p = "!$ACC CACHE";
2141 break;
2142 case ST_OACC_ENTER_DATA:
2143 p = "!$ACC ENTER DATA";
2144 break;
2145 case ST_OACC_EXIT_DATA:
2146 p = "!$ACC EXIT DATA";
2147 break;
2148 case ST_OACC_ROUTINE:
2149 p = "!$ACC ROUTINE";
2150 break;
2151 case ST_OACC_ATOMIC:
2152 p = "!$ACC ATOMIC";
2153 break;
2154 case ST_OACC_END_ATOMIC:
2155 p = "!$ACC END ATOMIC";
2156 break;
2157 case ST_OMP_ATOMIC:
2158 p = "!$OMP ATOMIC";
2159 break;
2160 case ST_OMP_BARRIER:
2161 p = "!$OMP BARRIER";
2162 break;
2163 case ST_OMP_CANCEL:
2164 p = "!$OMP CANCEL";
2165 break;
2166 case ST_OMP_CANCELLATION_POINT:
2167 p = "!$OMP CANCELLATION POINT";
2168 break;
2169 case ST_OMP_CRITICAL:
2170 p = "!$OMP CRITICAL";
2171 break;
2172 case ST_OMP_DECLARE_REDUCTION:
2173 p = "!$OMP DECLARE REDUCTION";
2174 break;
2175 case ST_OMP_DECLARE_SIMD:
2176 p = "!$OMP DECLARE SIMD";
2177 break;
2178 case ST_OMP_DECLARE_TARGET:
2179 p = "!$OMP DECLARE TARGET";
2180 break;
2181 case ST_OMP_DISTRIBUTE:
2182 p = "!$OMP DISTRIBUTE";
2183 break;
2184 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
2185 p = "!$OMP DISTRIBUTE PARALLEL DO";
2186 break;
2187 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
2188 p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
2189 break;
2190 case ST_OMP_DISTRIBUTE_SIMD:
2191 p = "!$OMP DISTRIBUTE SIMD";
2192 break;
2193 case ST_OMP_DO:
2194 p = "!$OMP DO";
2195 break;
2196 case ST_OMP_DO_SIMD:
2197 p = "!$OMP DO SIMD";
2198 break;
2199 case ST_OMP_END_ATOMIC:
2200 p = "!$OMP END ATOMIC";
2201 break;
2202 case ST_OMP_END_CRITICAL:
2203 p = "!$OMP END CRITICAL";
2204 break;
2205 case ST_OMP_END_DISTRIBUTE:
2206 p = "!$OMP END DISTRIBUTE";
2207 break;
2208 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
2209 p = "!$OMP END DISTRIBUTE PARALLEL DO";
2210 break;
2211 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
2212 p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
2213 break;
2214 case ST_OMP_END_DISTRIBUTE_SIMD:
2215 p = "!$OMP END DISTRIBUTE SIMD";
2216 break;
2217 case ST_OMP_END_DO:
2218 p = "!$OMP END DO";
2219 break;
2220 case ST_OMP_END_DO_SIMD:
2221 p = "!$OMP END DO SIMD";
2222 break;
2223 case ST_OMP_END_SIMD:
2224 p = "!$OMP END SIMD";
2225 break;
2226 case ST_OMP_END_MASTER:
2227 p = "!$OMP END MASTER";
2228 break;
2229 case ST_OMP_END_ORDERED:
2230 p = "!$OMP END ORDERED";
2231 break;
2232 case ST_OMP_END_PARALLEL:
2233 p = "!$OMP END PARALLEL";
2234 break;
2235 case ST_OMP_END_PARALLEL_DO:
2236 p = "!$OMP END PARALLEL DO";
2237 break;
2238 case ST_OMP_END_PARALLEL_DO_SIMD:
2239 p = "!$OMP END PARALLEL DO SIMD";
2240 break;
2241 case ST_OMP_END_PARALLEL_SECTIONS:
2242 p = "!$OMP END PARALLEL SECTIONS";
2243 break;
2244 case ST_OMP_END_PARALLEL_WORKSHARE:
2245 p = "!$OMP END PARALLEL WORKSHARE";
2246 break;
2247 case ST_OMP_END_SECTIONS:
2248 p = "!$OMP END SECTIONS";
2249 break;
2250 case ST_OMP_END_SINGLE:
2251 p = "!$OMP END SINGLE";
2252 break;
2253 case ST_OMP_END_TASK:
2254 p = "!$OMP END TASK";
2255 break;
2256 case ST_OMP_END_TARGET:
2257 p = "!$OMP END TARGET";
2258 break;
2259 case ST_OMP_END_TARGET_DATA:
2260 p = "!$OMP END TARGET DATA";
2261 break;
2262 case ST_OMP_END_TARGET_PARALLEL:
2263 p = "!$OMP END TARGET PARALLEL";
2264 break;
2265 case ST_OMP_END_TARGET_PARALLEL_DO:
2266 p = "!$OMP END TARGET PARALLEL DO";
2267 break;
2268 case ST_OMP_END_TARGET_PARALLEL_DO_SIMD:
2269 p = "!$OMP END TARGET PARALLEL DO SIMD";
2270 break;
2271 case ST_OMP_END_TARGET_SIMD:
2272 p = "!$OMP END TARGET SIMD";
2273 break;
2274 case ST_OMP_END_TARGET_TEAMS:
2275 p = "!$OMP END TARGET TEAMS";
2276 break;
2277 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
2278 p = "!$OMP END TARGET TEAMS DISTRIBUTE";
2279 break;
2280 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2281 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2282 break;
2283 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2284 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2285 break;
2286 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
2287 p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2288 break;
2289 case ST_OMP_END_TASKGROUP:
2290 p = "!$OMP END TASKGROUP";
2291 break;
2292 case ST_OMP_END_TASKLOOP:
2293 p = "!$OMP END TASKLOOP";
2294 break;
2295 case ST_OMP_END_TASKLOOP_SIMD:
2296 p = "!$OMP END TASKLOOP SIMD";
2297 break;
2298 case ST_OMP_END_TEAMS:
2299 p = "!$OMP END TEAMS";
2300 break;
2301 case ST_OMP_END_TEAMS_DISTRIBUTE:
2302 p = "!$OMP END TEAMS DISTRIBUTE";
2303 break;
2304 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
2305 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2306 break;
2307 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2308 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2309 break;
2310 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
2311 p = "!$OMP END TEAMS DISTRIBUTE SIMD";
2312 break;
2313 case ST_OMP_END_WORKSHARE:
2314 p = "!$OMP END WORKSHARE";
2315 break;
2316 case ST_OMP_FLUSH:
2317 p = "!$OMP FLUSH";
2318 break;
2319 case ST_OMP_MASTER:
2320 p = "!$OMP MASTER";
2321 break;
2322 case ST_OMP_ORDERED:
2323 case ST_OMP_ORDERED_DEPEND:
2324 p = "!$OMP ORDERED";
2325 break;
2326 case ST_OMP_PARALLEL:
2327 p = "!$OMP PARALLEL";
2328 break;
2329 case ST_OMP_PARALLEL_DO:
2330 p = "!$OMP PARALLEL DO";
2331 break;
2332 case ST_OMP_PARALLEL_DO_SIMD:
2333 p = "!$OMP PARALLEL DO SIMD";
2334 break;
2335 case ST_OMP_PARALLEL_SECTIONS:
2336 p = "!$OMP PARALLEL SECTIONS";
2337 break;
2338 case ST_OMP_PARALLEL_WORKSHARE:
2339 p = "!$OMP PARALLEL WORKSHARE";
2340 break;
2341 case ST_OMP_SECTIONS:
2342 p = "!$OMP SECTIONS";
2343 break;
2344 case ST_OMP_SECTION:
2345 p = "!$OMP SECTION";
2346 break;
2347 case ST_OMP_SIMD:
2348 p = "!$OMP SIMD";
2349 break;
2350 case ST_OMP_SINGLE:
2351 p = "!$OMP SINGLE";
2352 break;
2353 case ST_OMP_TARGET:
2354 p = "!$OMP TARGET";
2355 break;
2356 case ST_OMP_TARGET_DATA:
2357 p = "!$OMP TARGET DATA";
2358 break;
2359 case ST_OMP_TARGET_ENTER_DATA:
2360 p = "!$OMP TARGET ENTER DATA";
2361 break;
2362 case ST_OMP_TARGET_EXIT_DATA:
2363 p = "!$OMP TARGET EXIT DATA";
2364 break;
2365 case ST_OMP_TARGET_PARALLEL:
2366 p = "!$OMP TARGET PARALLEL";
2367 break;
2368 case ST_OMP_TARGET_PARALLEL_DO:
2369 p = "!$OMP TARGET PARALLEL DO";
2370 break;
2371 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
2372 p = "!$OMP TARGET PARALLEL DO SIMD";
2373 break;
2374 case ST_OMP_TARGET_SIMD:
2375 p = "!$OMP TARGET SIMD";
2376 break;
2377 case ST_OMP_TARGET_TEAMS:
2378 p = "!$OMP TARGET TEAMS";
2379 break;
2380 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
2381 p = "!$OMP TARGET TEAMS DISTRIBUTE";
2382 break;
2383 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2384 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2385 break;
2386 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2387 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2388 break;
2389 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2390 p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2391 break;
2392 case ST_OMP_TARGET_UPDATE:
2393 p = "!$OMP TARGET UPDATE";
2394 break;
2395 case ST_OMP_TASK:
2396 p = "!$OMP TASK";
2397 break;
2398 case ST_OMP_TASKGROUP:
2399 p = "!$OMP TASKGROUP";
2400 break;
2401 case ST_OMP_TASKLOOP:
2402 p = "!$OMP TASKLOOP";
2403 break;
2404 case ST_OMP_TASKLOOP_SIMD:
2405 p = "!$OMP TASKLOOP SIMD";
2406 break;
2407 case ST_OMP_TASKWAIT:
2408 p = "!$OMP TASKWAIT";
2409 break;
2410 case ST_OMP_TASKYIELD:
2411 p = "!$OMP TASKYIELD";
2412 break;
2413 case ST_OMP_TEAMS:
2414 p = "!$OMP TEAMS";
2415 break;
2416 case ST_OMP_TEAMS_DISTRIBUTE:
2417 p = "!$OMP TEAMS DISTRIBUTE";
2418 break;
2419 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2420 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2421 break;
2422 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2423 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2424 break;
2425 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
2426 p = "!$OMP TEAMS DISTRIBUTE SIMD";
2427 break;
2428 case ST_OMP_THREADPRIVATE:
2429 p = "!$OMP THREADPRIVATE";
2430 break;
2431 case ST_OMP_WORKSHARE:
2432 p = "!$OMP WORKSHARE";
2433 break;
2434 default:
2435 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2438 return p;
2442 /* Create a symbol for the main program and assign it to ns->proc_name. */
2444 static void
2445 main_program_symbol (gfc_namespace *ns, const char *name)
2447 gfc_symbol *main_program;
2448 symbol_attribute attr;
2450 gfc_get_symbol (name, ns, &main_program);
2451 gfc_clear_attr (&attr);
2452 attr.flavor = FL_PROGRAM;
2453 attr.proc = PROC_UNKNOWN;
2454 attr.subroutine = 1;
2455 attr.access = ACCESS_PUBLIC;
2456 attr.is_main_program = 1;
2457 main_program->attr = attr;
2458 main_program->declared_at = gfc_current_locus;
2459 ns->proc_name = main_program;
2460 gfc_commit_symbols ();
2464 /* Do whatever is necessary to accept the last statement. */
2466 static void
2467 accept_statement (gfc_statement st)
2469 switch (st)
2471 case ST_IMPLICIT_NONE:
2472 case ST_IMPLICIT:
2473 break;
2475 case ST_FUNCTION:
2476 case ST_SUBROUTINE:
2477 case ST_MODULE:
2478 case ST_SUBMODULE:
2479 gfc_current_ns->proc_name = gfc_new_block;
2480 break;
2482 /* If the statement is the end of a block, lay down a special code
2483 that allows a branch to the end of the block from within the
2484 construct. IF and SELECT are treated differently from DO
2485 (where EXEC_NOP is added inside the loop) for two
2486 reasons:
2487 1. END DO has a meaning in the sense that after a GOTO to
2488 it, the loop counter must be increased.
2489 2. IF blocks and SELECT blocks can consist of multiple
2490 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2491 Putting the label before the END IF would make the jump
2492 from, say, the ELSE IF block to the END IF illegal. */
2494 case ST_ENDIF:
2495 case ST_END_SELECT:
2496 case ST_END_CRITICAL:
2497 if (gfc_statement_label != NULL)
2499 new_st.op = EXEC_END_NESTED_BLOCK;
2500 add_statement ();
2502 break;
2504 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2505 one parallel block. Thus, we add the special code to the nested block
2506 itself, instead of the parent one. */
2507 case ST_END_BLOCK:
2508 case ST_END_ASSOCIATE:
2509 if (gfc_statement_label != NULL)
2511 new_st.op = EXEC_END_BLOCK;
2512 add_statement ();
2514 break;
2516 /* The end-of-program unit statements do not get the special
2517 marker and require a statement of some sort if they are a
2518 branch target. */
2520 case ST_END_PROGRAM:
2521 case ST_END_FUNCTION:
2522 case ST_END_SUBROUTINE:
2523 if (gfc_statement_label != NULL)
2525 new_st.op = EXEC_RETURN;
2526 add_statement ();
2528 else
2530 new_st.op = EXEC_END_PROCEDURE;
2531 add_statement ();
2534 break;
2536 case ST_ENTRY:
2537 case_executable:
2538 case_exec_markers:
2539 add_statement ();
2540 break;
2542 default:
2543 break;
2546 gfc_commit_symbols ();
2547 gfc_warning_check ();
2548 gfc_clear_new_st ();
2552 /* Undo anything tentative that has been built for the current statement,
2553 except if a gfc_charlen structure has been added to current namespace's
2554 list of gfc_charlen structure. */
2556 static void
2557 reject_statement (void)
2559 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
2560 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
2562 gfc_reject_data (gfc_current_ns);
2564 gfc_new_block = NULL;
2565 gfc_undo_symbols ();
2566 gfc_clear_warning ();
2567 undo_new_statement ();
2571 /* Generic complaint about an out of order statement. We also do
2572 whatever is necessary to clean up. */
2574 static void
2575 unexpected_statement (gfc_statement st)
2577 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
2579 reject_statement ();
2583 /* Given the next statement seen by the matcher, make sure that it is
2584 in proper order with the last. This subroutine is initialized by
2585 calling it with an argument of ST_NONE. If there is a problem, we
2586 issue an error and return false. Otherwise we return true.
2588 Individual parsers need to verify that the statements seen are
2589 valid before calling here, i.e., ENTRY statements are not allowed in
2590 INTERFACE blocks. The following diagram is taken from the standard:
2592 +---------------------------------------+
2593 | program subroutine function module |
2594 +---------------------------------------+
2595 | use |
2596 +---------------------------------------+
2597 | import |
2598 +---------------------------------------+
2599 | | implicit none |
2600 | +-----------+------------------+
2601 | | parameter | implicit |
2602 | +-----------+------------------+
2603 | format | | derived type |
2604 | entry | parameter | interface |
2605 | | data | specification |
2606 | | | statement func |
2607 | +-----------+------------------+
2608 | | data | executable |
2609 +--------+-----------+------------------+
2610 | contains |
2611 +---------------------------------------+
2612 | internal module/subprogram |
2613 +---------------------------------------+
2614 | end |
2615 +---------------------------------------+
2619 enum state_order
2621 ORDER_START,
2622 ORDER_USE,
2623 ORDER_IMPORT,
2624 ORDER_IMPLICIT_NONE,
2625 ORDER_IMPLICIT,
2626 ORDER_SPEC,
2627 ORDER_EXEC
2630 typedef struct
2632 enum state_order state;
2633 gfc_statement last_statement;
2634 locus where;
2636 st_state;
2638 static bool
2639 verify_st_order (st_state *p, gfc_statement st, bool silent)
2642 switch (st)
2644 case ST_NONE:
2645 p->state = ORDER_START;
2646 break;
2648 case ST_USE:
2649 if (p->state > ORDER_USE)
2650 goto order;
2651 p->state = ORDER_USE;
2652 break;
2654 case ST_IMPORT:
2655 if (p->state > ORDER_IMPORT)
2656 goto order;
2657 p->state = ORDER_IMPORT;
2658 break;
2660 case ST_IMPLICIT_NONE:
2661 if (p->state > ORDER_IMPLICIT)
2662 goto order;
2664 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2665 statement disqualifies a USE but not an IMPLICIT NONE.
2666 Duplicate IMPLICIT NONEs are caught when the implicit types
2667 are set. */
2669 p->state = ORDER_IMPLICIT_NONE;
2670 break;
2672 case ST_IMPLICIT:
2673 if (p->state > ORDER_IMPLICIT)
2674 goto order;
2675 p->state = ORDER_IMPLICIT;
2676 break;
2678 case ST_FORMAT:
2679 case ST_ENTRY:
2680 if (p->state < ORDER_IMPLICIT_NONE)
2681 p->state = ORDER_IMPLICIT_NONE;
2682 break;
2684 case ST_PARAMETER:
2685 if (p->state >= ORDER_EXEC)
2686 goto order;
2687 if (p->state < ORDER_IMPLICIT)
2688 p->state = ORDER_IMPLICIT;
2689 break;
2691 case ST_DATA:
2692 if (p->state < ORDER_SPEC)
2693 p->state = ORDER_SPEC;
2694 break;
2696 case ST_PUBLIC:
2697 case ST_PRIVATE:
2698 case ST_STRUCTURE_DECL:
2699 case ST_DERIVED_DECL:
2700 case_decl:
2701 if (p->state >= ORDER_EXEC)
2702 goto order;
2703 if (p->state < ORDER_SPEC)
2704 p->state = ORDER_SPEC;
2705 break;
2707 case_omp_decl:
2708 /* The OpenMP directives have to be somewhere in the specification
2709 part, but there are no further requirements on their ordering.
2710 Thus don't adjust p->state, just ignore them. */
2711 if (p->state >= ORDER_EXEC)
2712 goto order;
2713 break;
2715 case_executable:
2716 case_exec_markers:
2717 if (p->state < ORDER_EXEC)
2718 p->state = ORDER_EXEC;
2719 break;
2721 default:
2722 return false;
2725 /* All is well, record the statement in case we need it next time. */
2726 p->where = gfc_current_locus;
2727 p->last_statement = st;
2728 return true;
2730 order:
2731 if (!silent)
2732 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2733 gfc_ascii_statement (st),
2734 gfc_ascii_statement (p->last_statement), &p->where);
2736 return false;
2740 /* Handle an unexpected end of file. This is a show-stopper... */
2742 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
2744 static void
2745 unexpected_eof (void)
2747 gfc_state_data *p;
2749 gfc_error ("Unexpected end of file in %qs", gfc_source_file);
2751 /* Memory cleanup. Move to "second to last". */
2752 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
2753 p = p->previous);
2755 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
2756 gfc_done_2 ();
2758 longjmp (eof_buf, 1);
2760 /* Avoids build error on systems where longjmp is not declared noreturn. */
2761 gcc_unreachable ();
2765 /* Parse the CONTAINS section of a derived type definition. */
2767 gfc_access gfc_typebound_default_access;
2769 static bool
2770 parse_derived_contains (void)
2772 gfc_state_data s;
2773 bool seen_private = false;
2774 bool seen_comps = false;
2775 bool error_flag = false;
2776 bool to_finish;
2778 gcc_assert (gfc_current_state () == COMP_DERIVED);
2779 gcc_assert (gfc_current_block ());
2781 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2782 section. */
2783 if (gfc_current_block ()->attr.sequence)
2784 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2785 " section at %C", gfc_current_block ()->name);
2786 if (gfc_current_block ()->attr.is_bind_c)
2787 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2788 " section at %C", gfc_current_block ()->name);
2790 accept_statement (ST_CONTAINS);
2791 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
2793 gfc_typebound_default_access = ACCESS_PUBLIC;
2795 to_finish = false;
2796 while (!to_finish)
2798 gfc_statement st;
2799 st = next_statement ();
2800 switch (st)
2802 case ST_NONE:
2803 unexpected_eof ();
2804 break;
2806 case ST_DATA_DECL:
2807 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2808 goto error;
2810 case ST_PROCEDURE:
2811 if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
2812 goto error;
2814 accept_statement (ST_PROCEDURE);
2815 seen_comps = true;
2816 break;
2818 case ST_GENERIC:
2819 if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
2820 goto error;
2822 accept_statement (ST_GENERIC);
2823 seen_comps = true;
2824 break;
2826 case ST_FINAL:
2827 if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
2828 " at %C"))
2829 goto error;
2831 accept_statement (ST_FINAL);
2832 seen_comps = true;
2833 break;
2835 case ST_END_TYPE:
2836 to_finish = true;
2838 if (!seen_comps
2839 && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
2840 "at %C with empty CONTAINS section")))
2841 goto error;
2843 /* ST_END_TYPE is accepted by parse_derived after return. */
2844 break;
2846 case ST_PRIVATE:
2847 if (!gfc_find_state (COMP_MODULE))
2849 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2850 "a MODULE");
2851 goto error;
2854 if (seen_comps)
2856 gfc_error ("PRIVATE statement at %C must precede procedure"
2857 " bindings");
2858 goto error;
2861 if (seen_private)
2863 gfc_error ("Duplicate PRIVATE statement at %C");
2864 goto error;
2867 accept_statement (ST_PRIVATE);
2868 gfc_typebound_default_access = ACCESS_PRIVATE;
2869 seen_private = true;
2870 break;
2872 case ST_SEQUENCE:
2873 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2874 goto error;
2876 case ST_CONTAINS:
2877 gfc_error ("Already inside a CONTAINS block at %C");
2878 goto error;
2880 default:
2881 unexpected_statement (st);
2882 break;
2885 continue;
2887 error:
2888 error_flag = true;
2889 reject_statement ();
2892 pop_state ();
2893 gcc_assert (gfc_current_state () == COMP_DERIVED);
2895 return error_flag;
2899 /* Set attributes for the parent symbol based on the attributes of a component
2900 and raise errors if conflicting attributes are found for the component. */
2902 static void
2903 check_component (gfc_symbol *sym, gfc_component *c, gfc_component **lockp,
2904 gfc_component **eventp)
2906 bool coarray, lock_type, event_type, allocatable, pointer;
2907 coarray = lock_type = event_type = allocatable = pointer = false;
2908 gfc_component *lock_comp = NULL, *event_comp = NULL;
2910 if (lockp) lock_comp = *lockp;
2911 if (eventp) event_comp = *eventp;
2913 /* Look for allocatable components. */
2914 if (c->attr.allocatable
2915 || (c->ts.type == BT_CLASS && c->attr.class_ok
2916 && CLASS_DATA (c)->attr.allocatable)
2917 || (c->ts.type == BT_DERIVED && !c->attr.pointer
2918 && c->ts.u.derived->attr.alloc_comp))
2920 allocatable = true;
2921 sym->attr.alloc_comp = 1;
2924 /* Look for pointer components. */
2925 if (c->attr.pointer
2926 || (c->ts.type == BT_CLASS && c->attr.class_ok
2927 && CLASS_DATA (c)->attr.class_pointer)
2928 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2930 pointer = true;
2931 sym->attr.pointer_comp = 1;
2934 /* Look for procedure pointer components. */
2935 if (c->attr.proc_pointer
2936 || (c->ts.type == BT_DERIVED
2937 && c->ts.u.derived->attr.proc_pointer_comp))
2938 sym->attr.proc_pointer_comp = 1;
2940 /* Looking for coarray components. */
2941 if (c->attr.codimension
2942 || (c->ts.type == BT_CLASS && c->attr.class_ok
2943 && CLASS_DATA (c)->attr.codimension))
2945 coarray = true;
2946 sym->attr.coarray_comp = 1;
2949 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
2950 && !c->attr.pointer)
2952 coarray = true;
2953 sym->attr.coarray_comp = 1;
2956 /* Looking for lock_type components. */
2957 if ((c->ts.type == BT_DERIVED
2958 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2959 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2960 || (c->ts.type == BT_CLASS && c->attr.class_ok
2961 && CLASS_DATA (c)->ts.u.derived->from_intmod
2962 == INTMOD_ISO_FORTRAN_ENV
2963 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2964 == ISOFORTRAN_LOCK_TYPE)
2965 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
2966 && !allocatable && !pointer))
2968 lock_type = 1;
2969 lock_comp = c;
2970 sym->attr.lock_comp = 1;
2973 /* Looking for event_type components. */
2974 if ((c->ts.type == BT_DERIVED
2975 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2976 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2977 || (c->ts.type == BT_CLASS && c->attr.class_ok
2978 && CLASS_DATA (c)->ts.u.derived->from_intmod
2979 == INTMOD_ISO_FORTRAN_ENV
2980 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2981 == ISOFORTRAN_EVENT_TYPE)
2982 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.event_comp
2983 && !allocatable && !pointer))
2985 event_type = 1;
2986 event_comp = c;
2987 sym->attr.event_comp = 1;
2990 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2991 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2992 unless there are nondirect [allocatable or pointer] components
2993 involved (cf. 1.3.33.1 and 1.3.33.3). */
2995 if (pointer && !coarray && lock_type)
2996 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2997 "codimension or be a subcomponent of a coarray, "
2998 "which is not possible as the component has the "
2999 "pointer attribute", c->name, &c->loc);
3000 else if (pointer && !coarray && c->ts.type == BT_DERIVED
3001 && c->ts.u.derived->attr.lock_comp)
3002 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
3003 "of type LOCK_TYPE, which must have a codimension or be a "
3004 "subcomponent of a coarray", c->name, &c->loc);
3006 if (lock_type && allocatable && !coarray)
3007 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
3008 "a codimension", c->name, &c->loc);
3009 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
3010 && c->ts.u.derived->attr.lock_comp)
3011 gfc_error ("Allocatable component %s at %L must have a codimension as "
3012 "it has a noncoarray subcomponent of type LOCK_TYPE",
3013 c->name, &c->loc);
3015 if (sym->attr.coarray_comp && !coarray && lock_type)
3016 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
3017 "subcomponent of type LOCK_TYPE must have a codimension or "
3018 "be a subcomponent of a coarray. (Variables of type %s may "
3019 "not have a codimension as already a coarray "
3020 "subcomponent exists)", c->name, &c->loc, sym->name);
3022 if (sym->attr.lock_comp && coarray && !lock_type)
3023 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
3024 "subcomponent of type LOCK_TYPE must have a codimension or "
3025 "be a subcomponent of a coarray. (Variables of type %s may "
3026 "not have a codimension as %s at %L has a codimension or a "
3027 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
3028 sym->name, c->name, &c->loc);
3030 /* Similarly for EVENT TYPE. */
3032 if (pointer && !coarray && event_type)
3033 gfc_error ("Component %s at %L of type EVENT_TYPE must have a "
3034 "codimension or be a subcomponent of a coarray, "
3035 "which is not possible as the component has the "
3036 "pointer attribute", c->name, &c->loc);
3037 else if (pointer && !coarray && c->ts.type == BT_DERIVED
3038 && c->ts.u.derived->attr.event_comp)
3039 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
3040 "of type EVENT_TYPE, which must have a codimension or be a "
3041 "subcomponent of a coarray", c->name, &c->loc);
3043 if (event_type && allocatable && !coarray)
3044 gfc_error ("Allocatable component %s at %L of type EVENT_TYPE must have "
3045 "a codimension", c->name, &c->loc);
3046 else if (event_type && allocatable && c->ts.type == BT_DERIVED
3047 && c->ts.u.derived->attr.event_comp)
3048 gfc_error ("Allocatable component %s at %L must have a codimension as "
3049 "it has a noncoarray subcomponent of type EVENT_TYPE",
3050 c->name, &c->loc);
3052 if (sym->attr.coarray_comp && !coarray && event_type)
3053 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
3054 "subcomponent of type EVENT_TYPE must have a codimension or "
3055 "be a subcomponent of a coarray. (Variables of type %s may "
3056 "not have a codimension as already a coarray "
3057 "subcomponent exists)", c->name, &c->loc, sym->name);
3059 if (sym->attr.event_comp && coarray && !event_type)
3060 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
3061 "subcomponent of type EVENT_TYPE must have a codimension or "
3062 "be a subcomponent of a coarray. (Variables of type %s may "
3063 "not have a codimension as %s at %L has a codimension or a "
3064 "coarray subcomponent)", event_comp->name, &event_comp->loc,
3065 sym->name, c->name, &c->loc);
3067 /* Look for private components. */
3068 if (sym->component_access == ACCESS_PRIVATE
3069 || c->attr.access == ACCESS_PRIVATE
3070 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
3071 sym->attr.private_comp = 1;
3073 if (lockp) *lockp = lock_comp;
3074 if (eventp) *eventp = event_comp;
3078 static void parse_struct_map (gfc_statement);
3080 /* Parse a union component definition within a structure definition. */
3082 static void
3083 parse_union (void)
3085 int compiling;
3086 gfc_statement st;
3087 gfc_state_data s;
3088 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3089 gfc_symbol *un;
3091 accept_statement(ST_UNION);
3092 push_state (&s, COMP_UNION, gfc_new_block);
3093 un = gfc_new_block;
3095 compiling = 1;
3097 while (compiling)
3099 st = next_statement ();
3100 /* Only MAP declarations valid within a union. */
3101 switch (st)
3103 case ST_NONE:
3104 unexpected_eof ();
3106 case ST_MAP:
3107 accept_statement (ST_MAP);
3108 parse_struct_map (ST_MAP);
3109 /* Add a component to the union for each map. */
3110 if (!gfc_add_component (un, gfc_new_block->name, &c))
3112 gfc_internal_error ("failed to create map component '%s'",
3113 gfc_new_block->name);
3114 reject_statement ();
3115 return;
3117 c->ts.type = BT_DERIVED;
3118 c->ts.u.derived = gfc_new_block;
3119 /* Normally components get their initialization expressions when they
3120 are created in decl.c (build_struct) so we can look through the
3121 flat component list for initializers during resolution. Unions and
3122 maps create components along with their type definitions so we
3123 have to generate initializers here. */
3124 c->initializer = gfc_default_initializer (&c->ts);
3125 break;
3127 case ST_END_UNION:
3128 compiling = 0;
3129 accept_statement (ST_END_UNION);
3130 break;
3132 default:
3133 unexpected_statement (st);
3134 break;
3138 for (c = un->components; c; c = c->next)
3139 check_component (un, c, &lock_comp, &event_comp);
3141 /* Add the union as a component in its parent structure. */
3142 pop_state ();
3143 if (!gfc_add_component (gfc_current_block (), un->name, &c))
3145 gfc_internal_error ("failed to create union component '%s'", un->name);
3146 reject_statement ();
3147 return;
3149 c->ts.type = BT_UNION;
3150 c->ts.u.derived = un;
3151 c->initializer = gfc_default_initializer (&c->ts);
3153 un->attr.zero_comp = un->components == NULL;
3157 /* Parse a STRUCTURE or MAP. */
3159 static void
3160 parse_struct_map (gfc_statement block)
3162 int compiling_type;
3163 gfc_statement st;
3164 gfc_state_data s;
3165 gfc_symbol *sym;
3166 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3167 gfc_compile_state comp;
3168 gfc_statement ends;
3170 if (block == ST_STRUCTURE_DECL)
3172 comp = COMP_STRUCTURE;
3173 ends = ST_END_STRUCTURE;
3175 else
3177 gcc_assert (block == ST_MAP);
3178 comp = COMP_MAP;
3179 ends = ST_END_MAP;
3182 accept_statement(block);
3183 push_state (&s, comp, gfc_new_block);
3185 gfc_new_block->component_access = ACCESS_PUBLIC;
3186 compiling_type = 1;
3188 while (compiling_type)
3190 st = next_statement ();
3191 switch (st)
3193 case ST_NONE:
3194 unexpected_eof ();
3196 /* Nested structure declarations will be captured as ST_DATA_DECL. */
3197 case ST_STRUCTURE_DECL:
3198 /* Let a more specific error make it to decode_statement(). */
3199 if (gfc_error_check () == 0)
3200 gfc_error ("Syntax error in nested structure declaration at %C");
3201 reject_statement ();
3202 /* Skip the rest of this statement. */
3203 gfc_error_recovery ();
3204 break;
3206 case ST_UNION:
3207 accept_statement (ST_UNION);
3208 parse_union ();
3209 break;
3211 case ST_DATA_DECL:
3212 /* The data declaration was a nested/ad-hoc STRUCTURE field. */
3213 accept_statement (ST_DATA_DECL);
3214 if (gfc_new_block && gfc_new_block != gfc_current_block ()
3215 && gfc_new_block->attr.flavor == FL_STRUCT)
3216 parse_struct_map (ST_STRUCTURE_DECL);
3217 break;
3219 case ST_END_STRUCTURE:
3220 case ST_END_MAP:
3221 if (st == ends)
3223 accept_statement (st);
3224 compiling_type = 0;
3226 else
3227 unexpected_statement (st);
3228 break;
3230 default:
3231 unexpected_statement (st);
3232 break;
3236 /* Validate each component. */
3237 sym = gfc_current_block ();
3238 for (c = sym->components; c; c = c->next)
3239 check_component (sym, c, &lock_comp, &event_comp);
3241 sym->attr.zero_comp = (sym->components == NULL);
3243 /* Allow parse_union to find this structure to add to its list of maps. */
3244 if (block == ST_MAP)
3245 gfc_new_block = gfc_current_block ();
3247 pop_state ();
3251 /* Parse a derived type. */
3253 static void
3254 parse_derived (void)
3256 int compiling_type, seen_private, seen_sequence, seen_component;
3257 gfc_statement st;
3258 gfc_state_data s;
3259 gfc_symbol *sym;
3260 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3262 accept_statement (ST_DERIVED_DECL);
3263 push_state (&s, COMP_DERIVED, gfc_new_block);
3265 gfc_new_block->component_access = ACCESS_PUBLIC;
3266 seen_private = 0;
3267 seen_sequence = 0;
3268 seen_component = 0;
3270 compiling_type = 1;
3272 while (compiling_type)
3274 st = next_statement ();
3275 switch (st)
3277 case ST_NONE:
3278 unexpected_eof ();
3280 case ST_DATA_DECL:
3281 case ST_PROCEDURE:
3282 accept_statement (st);
3283 seen_component = 1;
3284 break;
3286 case ST_FINAL:
3287 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
3288 break;
3290 case ST_END_TYPE:
3291 endType:
3292 compiling_type = 0;
3294 if (!seen_component)
3295 gfc_notify_std (GFC_STD_F2003, "Derived type "
3296 "definition at %C without components");
3298 accept_statement (ST_END_TYPE);
3299 break;
3301 case ST_PRIVATE:
3302 if (!gfc_find_state (COMP_MODULE))
3304 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
3305 "a MODULE");
3306 break;
3309 if (seen_component)
3311 gfc_error ("PRIVATE statement at %C must precede "
3312 "structure components");
3313 break;
3316 if (seen_private)
3317 gfc_error ("Duplicate PRIVATE statement at %C");
3319 s.sym->component_access = ACCESS_PRIVATE;
3321 accept_statement (ST_PRIVATE);
3322 seen_private = 1;
3323 break;
3325 case ST_SEQUENCE:
3326 if (seen_component)
3328 gfc_error ("SEQUENCE statement at %C must precede "
3329 "structure components");
3330 break;
3333 if (gfc_current_block ()->attr.sequence)
3334 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
3335 "TYPE statement");
3337 if (seen_sequence)
3339 gfc_error ("Duplicate SEQUENCE statement at %C");
3342 seen_sequence = 1;
3343 gfc_add_sequence (&gfc_current_block ()->attr,
3344 gfc_current_block ()->name, NULL);
3345 break;
3347 case ST_CONTAINS:
3348 gfc_notify_std (GFC_STD_F2003,
3349 "CONTAINS block in derived type"
3350 " definition at %C");
3352 accept_statement (ST_CONTAINS);
3353 parse_derived_contains ();
3354 goto endType;
3356 default:
3357 unexpected_statement (st);
3358 break;
3362 /* need to verify that all fields of the derived type are
3363 * interoperable with C if the type is declared to be bind(c)
3365 sym = gfc_current_block ();
3366 for (c = sym->components; c; c = c->next)
3367 check_component (sym, c, &lock_comp, &event_comp);
3369 if (!seen_component)
3370 sym->attr.zero_comp = 1;
3372 pop_state ();
3376 /* Parse an ENUM. */
3378 static void
3379 parse_enum (void)
3381 gfc_statement st;
3382 int compiling_enum;
3383 gfc_state_data s;
3384 int seen_enumerator = 0;
3386 push_state (&s, COMP_ENUM, gfc_new_block);
3388 compiling_enum = 1;
3390 while (compiling_enum)
3392 st = next_statement ();
3393 switch (st)
3395 case ST_NONE:
3396 unexpected_eof ();
3397 break;
3399 case ST_ENUMERATOR:
3400 seen_enumerator = 1;
3401 accept_statement (st);
3402 break;
3404 case ST_END_ENUM:
3405 compiling_enum = 0;
3406 if (!seen_enumerator)
3407 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
3408 accept_statement (st);
3409 break;
3411 default:
3412 gfc_free_enum_history ();
3413 unexpected_statement (st);
3414 break;
3417 pop_state ();
3421 /* Parse an interface. We must be able to deal with the possibility
3422 of recursive interfaces. The parse_spec() subroutine is mutually
3423 recursive with parse_interface(). */
3425 static gfc_statement parse_spec (gfc_statement);
3427 static void
3428 parse_interface (void)
3430 gfc_compile_state new_state = COMP_NONE, current_state;
3431 gfc_symbol *prog_unit, *sym;
3432 gfc_interface_info save;
3433 gfc_state_data s1, s2;
3434 gfc_statement st;
3436 accept_statement (ST_INTERFACE);
3438 current_interface.ns = gfc_current_ns;
3439 save = current_interface;
3441 sym = (current_interface.type == INTERFACE_GENERIC
3442 || current_interface.type == INTERFACE_USER_OP)
3443 ? gfc_new_block : NULL;
3445 push_state (&s1, COMP_INTERFACE, sym);
3446 current_state = COMP_NONE;
3448 loop:
3449 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
3451 st = next_statement ();
3452 switch (st)
3454 case ST_NONE:
3455 unexpected_eof ();
3457 case ST_SUBROUTINE:
3458 case ST_FUNCTION:
3459 if (st == ST_SUBROUTINE)
3460 new_state = COMP_SUBROUTINE;
3461 else if (st == ST_FUNCTION)
3462 new_state = COMP_FUNCTION;
3463 if (gfc_new_block->attr.pointer)
3465 gfc_new_block->attr.pointer = 0;
3466 gfc_new_block->attr.proc_pointer = 1;
3468 if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
3469 gfc_new_block->formal, NULL))
3471 reject_statement ();
3472 gfc_free_namespace (gfc_current_ns);
3473 goto loop;
3475 /* F2008 C1210 forbids the IMPORT statement in module procedure
3476 interface bodies and the flag is set to import symbols. */
3477 if (gfc_new_block->attr.module_procedure)
3478 gfc_current_ns->has_import_set = 1;
3479 break;
3481 case ST_PROCEDURE:
3482 case ST_MODULE_PROC: /* The module procedure matcher makes
3483 sure the context is correct. */
3484 accept_statement (st);
3485 gfc_free_namespace (gfc_current_ns);
3486 goto loop;
3488 case ST_END_INTERFACE:
3489 gfc_free_namespace (gfc_current_ns);
3490 gfc_current_ns = current_interface.ns;
3491 goto done;
3493 default:
3494 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
3495 gfc_ascii_statement (st));
3496 reject_statement ();
3497 gfc_free_namespace (gfc_current_ns);
3498 goto loop;
3502 /* Make sure that the generic name has the right attribute. */
3503 if (current_interface.type == INTERFACE_GENERIC
3504 && current_state == COMP_NONE)
3506 if (new_state == COMP_FUNCTION && sym)
3507 gfc_add_function (&sym->attr, sym->name, NULL);
3508 else if (new_state == COMP_SUBROUTINE && sym)
3509 gfc_add_subroutine (&sym->attr, sym->name, NULL);
3511 current_state = new_state;
3514 if (current_interface.type == INTERFACE_ABSTRACT)
3516 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
3517 if (gfc_is_intrinsic_typename (gfc_new_block->name))
3518 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
3519 "cannot be the same as an intrinsic type",
3520 gfc_new_block->name);
3523 push_state (&s2, new_state, gfc_new_block);
3524 accept_statement (st);
3525 prog_unit = gfc_new_block;
3526 prog_unit->formal_ns = gfc_current_ns;
3527 if (prog_unit == prog_unit->formal_ns->proc_name
3528 && prog_unit->ns != prog_unit->formal_ns)
3529 prog_unit->refs++;
3531 decl:
3532 /* Read data declaration statements. */
3533 st = parse_spec (ST_NONE);
3534 in_specification_block = true;
3536 /* Since the interface block does not permit an IMPLICIT statement,
3537 the default type for the function or the result must be taken
3538 from the formal namespace. */
3539 if (new_state == COMP_FUNCTION)
3541 if (prog_unit->result == prog_unit
3542 && prog_unit->ts.type == BT_UNKNOWN)
3543 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
3544 else if (prog_unit->result != prog_unit
3545 && prog_unit->result->ts.type == BT_UNKNOWN)
3546 gfc_set_default_type (prog_unit->result, 1,
3547 prog_unit->formal_ns);
3550 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
3552 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3553 gfc_ascii_statement (st));
3554 reject_statement ();
3555 goto decl;
3558 /* Add EXTERNAL attribute to function or subroutine. */
3559 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
3560 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
3562 current_interface = save;
3563 gfc_add_interface (prog_unit);
3564 pop_state ();
3566 if (current_interface.ns
3567 && current_interface.ns->proc_name
3568 && strcmp (current_interface.ns->proc_name->name,
3569 prog_unit->name) == 0)
3570 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3571 "enclosing procedure", prog_unit->name,
3572 &current_interface.ns->proc_name->declared_at);
3574 goto loop;
3576 done:
3577 pop_state ();
3581 /* Associate function characteristics by going back to the function
3582 declaration and rematching the prefix. */
3584 static match
3585 match_deferred_characteristics (gfc_typespec * ts)
3587 locus loc;
3588 match m = MATCH_ERROR;
3589 char name[GFC_MAX_SYMBOL_LEN + 1];
3591 loc = gfc_current_locus;
3593 gfc_current_locus = gfc_current_block ()->declared_at;
3595 gfc_clear_error ();
3596 gfc_buffer_error (true);
3597 m = gfc_match_prefix (ts);
3598 gfc_buffer_error (false);
3600 if (ts->type == BT_DERIVED)
3602 ts->kind = 0;
3604 if (!ts->u.derived)
3605 m = MATCH_ERROR;
3608 /* Only permit one go at the characteristic association. */
3609 if (ts->kind == -1)
3610 ts->kind = 0;
3612 /* Set the function locus correctly. If we have not found the
3613 function name, there is an error. */
3614 if (m == MATCH_YES
3615 && gfc_match ("function% %n", name) == MATCH_YES
3616 && strcmp (name, gfc_current_block ()->name) == 0)
3618 gfc_current_block ()->declared_at = gfc_current_locus;
3619 gfc_commit_symbols ();
3621 else
3623 gfc_error_check ();
3624 gfc_undo_symbols ();
3627 gfc_current_locus =loc;
3628 return m;
3632 /* Check specification-expressions in the function result of the currently
3633 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3634 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3635 scope are not yet parsed so this has to be delayed up to parse_spec. */
3637 static void
3638 check_function_result_typed (void)
3640 gfc_typespec ts;
3642 gcc_assert (gfc_current_state () == COMP_FUNCTION);
3644 if (!gfc_current_ns->proc_name->result) return;
3646 ts = gfc_current_ns->proc_name->result->ts;
3648 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3649 /* TODO: Extend when KIND type parameters are implemented. */
3650 if (ts.type == BT_CHARACTER && ts.u.cl && ts.u.cl->length)
3651 gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true);
3655 /* Parse a set of specification statements. Returns the statement
3656 that doesn't fit. */
3658 static gfc_statement
3659 parse_spec (gfc_statement st)
3661 st_state ss;
3662 bool function_result_typed = false;
3663 bool bad_characteristic = false;
3664 gfc_typespec *ts;
3666 in_specification_block = true;
3668 verify_st_order (&ss, ST_NONE, false);
3669 if (st == ST_NONE)
3670 st = next_statement ();
3672 /* If we are not inside a function or don't have a result specified so far,
3673 do nothing special about it. */
3674 if (gfc_current_state () != COMP_FUNCTION)
3675 function_result_typed = true;
3676 else
3678 gfc_symbol* proc = gfc_current_ns->proc_name;
3679 gcc_assert (proc);
3681 if (proc->result->ts.type == BT_UNKNOWN)
3682 function_result_typed = true;
3685 loop:
3687 /* If we're inside a BLOCK construct, some statements are disallowed.
3688 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3689 or VALUE are also disallowed, but they don't have a particular ST_*
3690 key so we have to check for them individually in their matcher routine. */
3691 if (gfc_current_state () == COMP_BLOCK)
3692 switch (st)
3694 case ST_IMPLICIT:
3695 case ST_IMPLICIT_NONE:
3696 case ST_NAMELIST:
3697 case ST_COMMON:
3698 case ST_EQUIVALENCE:
3699 case ST_STATEMENT_FUNCTION:
3700 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3701 gfc_ascii_statement (st));
3702 reject_statement ();
3703 break;
3705 default:
3706 break;
3708 else if (gfc_current_state () == COMP_BLOCK_DATA)
3709 /* Fortran 2008, C1116. */
3710 switch (st)
3712 case ST_ATTR_DECL:
3713 case ST_COMMON:
3714 case ST_DATA:
3715 case ST_DATA_DECL:
3716 case ST_DERIVED_DECL:
3717 case ST_END_BLOCK_DATA:
3718 case ST_EQUIVALENCE:
3719 case ST_IMPLICIT:
3720 case ST_IMPLICIT_NONE:
3721 case ST_OMP_THREADPRIVATE:
3722 case ST_PARAMETER:
3723 case ST_STRUCTURE_DECL:
3724 case ST_TYPE:
3725 case ST_USE:
3726 break;
3728 case ST_NONE:
3729 break;
3731 default:
3732 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3733 gfc_ascii_statement (st));
3734 reject_statement ();
3735 break;
3738 /* If we find a statement that can not be followed by an IMPLICIT statement
3739 (and thus we can expect to see none any further), type the function result
3740 if it has not yet been typed. Be careful not to give the END statement
3741 to verify_st_order! */
3742 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
3744 bool verify_now = false;
3746 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
3747 verify_now = true;
3748 else
3750 st_state dummyss;
3751 verify_st_order (&dummyss, ST_NONE, false);
3752 verify_st_order (&dummyss, st, false);
3754 if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
3755 verify_now = true;
3758 if (verify_now)
3760 check_function_result_typed ();
3761 function_result_typed = true;
3765 switch (st)
3767 case ST_NONE:
3768 unexpected_eof ();
3770 case ST_IMPLICIT_NONE:
3771 case ST_IMPLICIT:
3772 if (!function_result_typed)
3774 check_function_result_typed ();
3775 function_result_typed = true;
3777 goto declSt;
3779 case ST_FORMAT:
3780 case ST_ENTRY:
3781 case ST_DATA: /* Not allowed in interfaces */
3782 if (gfc_current_state () == COMP_INTERFACE)
3783 break;
3785 /* Fall through */
3787 case ST_USE:
3788 case ST_IMPORT:
3789 case ST_PARAMETER:
3790 case ST_PUBLIC:
3791 case ST_PRIVATE:
3792 case ST_STRUCTURE_DECL:
3793 case ST_DERIVED_DECL:
3794 case_decl:
3795 case_omp_decl:
3796 declSt:
3797 if (!verify_st_order (&ss, st, false))
3799 reject_statement ();
3800 st = next_statement ();
3801 goto loop;
3804 switch (st)
3806 case ST_INTERFACE:
3807 parse_interface ();
3808 break;
3810 case ST_STRUCTURE_DECL:
3811 parse_struct_map (ST_STRUCTURE_DECL);
3812 break;
3814 case ST_DERIVED_DECL:
3815 parse_derived ();
3816 break;
3818 case ST_PUBLIC:
3819 case ST_PRIVATE:
3820 if (gfc_current_state () != COMP_MODULE)
3822 gfc_error ("%s statement must appear in a MODULE",
3823 gfc_ascii_statement (st));
3824 reject_statement ();
3825 break;
3828 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
3830 gfc_error ("%s statement at %C follows another accessibility "
3831 "specification", gfc_ascii_statement (st));
3832 reject_statement ();
3833 break;
3836 gfc_current_ns->default_access = (st == ST_PUBLIC)
3837 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3839 break;
3841 case ST_STATEMENT_FUNCTION:
3842 if (gfc_current_state () == COMP_MODULE
3843 || gfc_current_state () == COMP_SUBMODULE)
3845 unexpected_statement (st);
3846 break;
3849 default:
3850 break;
3853 accept_statement (st);
3854 st = next_statement ();
3855 goto loop;
3857 case ST_ENUM:
3858 accept_statement (st);
3859 parse_enum();
3860 st = next_statement ();
3861 goto loop;
3863 case ST_GET_FCN_CHARACTERISTICS:
3864 /* This statement triggers the association of a function's result
3865 characteristics. */
3866 ts = &gfc_current_block ()->result->ts;
3867 if (match_deferred_characteristics (ts) != MATCH_YES)
3868 bad_characteristic = true;
3870 st = next_statement ();
3871 goto loop;
3873 default:
3874 break;
3877 /* If match_deferred_characteristics failed, then there is an error. */
3878 if (bad_characteristic)
3880 ts = &gfc_current_block ()->result->ts;
3881 if (ts->type != BT_DERIVED)
3882 gfc_error ("Bad kind expression for function %qs at %L",
3883 gfc_current_block ()->name,
3884 &gfc_current_block ()->declared_at);
3885 else
3886 gfc_error ("The type for function %qs at %L is not accessible",
3887 gfc_current_block ()->name,
3888 &gfc_current_block ()->declared_at);
3890 gfc_current_block ()->ts.kind = 0;
3891 /* Keep the derived type; if it's bad, it will be discovered later. */
3892 if (!(ts->type == BT_DERIVED && ts->u.derived))
3893 ts->type = BT_UNKNOWN;
3896 in_specification_block = false;
3898 return st;
3902 /* Parse a WHERE block, (not a simple WHERE statement). */
3904 static void
3905 parse_where_block (void)
3907 int seen_empty_else;
3908 gfc_code *top, *d;
3909 gfc_state_data s;
3910 gfc_statement st;
3912 accept_statement (ST_WHERE_BLOCK);
3913 top = gfc_state_stack->tail;
3915 push_state (&s, COMP_WHERE, gfc_new_block);
3917 d = add_statement ();
3918 d->expr1 = top->expr1;
3919 d->op = EXEC_WHERE;
3921 top->expr1 = NULL;
3922 top->block = d;
3924 seen_empty_else = 0;
3928 st = next_statement ();
3929 switch (st)
3931 case ST_NONE:
3932 unexpected_eof ();
3934 case ST_WHERE_BLOCK:
3935 parse_where_block ();
3936 break;
3938 case ST_ASSIGNMENT:
3939 case ST_WHERE:
3940 accept_statement (st);
3941 break;
3943 case ST_ELSEWHERE:
3944 if (seen_empty_else)
3946 gfc_error ("ELSEWHERE statement at %C follows previous "
3947 "unmasked ELSEWHERE");
3948 reject_statement ();
3949 break;
3952 if (new_st.expr1 == NULL)
3953 seen_empty_else = 1;
3955 d = new_level (gfc_state_stack->head);
3956 d->op = EXEC_WHERE;
3957 d->expr1 = new_st.expr1;
3959 accept_statement (st);
3961 break;
3963 case ST_END_WHERE:
3964 accept_statement (st);
3965 break;
3967 default:
3968 gfc_error ("Unexpected %s statement in WHERE block at %C",
3969 gfc_ascii_statement (st));
3970 reject_statement ();
3971 break;
3974 while (st != ST_END_WHERE);
3976 pop_state ();
3980 /* Parse a FORALL block (not a simple FORALL statement). */
3982 static void
3983 parse_forall_block (void)
3985 gfc_code *top, *d;
3986 gfc_state_data s;
3987 gfc_statement st;
3989 accept_statement (ST_FORALL_BLOCK);
3990 top = gfc_state_stack->tail;
3992 push_state (&s, COMP_FORALL, gfc_new_block);
3994 d = add_statement ();
3995 d->op = EXEC_FORALL;
3996 top->block = d;
4000 st = next_statement ();
4001 switch (st)
4004 case ST_ASSIGNMENT:
4005 case ST_POINTER_ASSIGNMENT:
4006 case ST_WHERE:
4007 case ST_FORALL:
4008 accept_statement (st);
4009 break;
4011 case ST_WHERE_BLOCK:
4012 parse_where_block ();
4013 break;
4015 case ST_FORALL_BLOCK:
4016 parse_forall_block ();
4017 break;
4019 case ST_END_FORALL:
4020 accept_statement (st);
4021 break;
4023 case ST_NONE:
4024 unexpected_eof ();
4026 default:
4027 gfc_error ("Unexpected %s statement in FORALL block at %C",
4028 gfc_ascii_statement (st));
4030 reject_statement ();
4031 break;
4034 while (st != ST_END_FORALL);
4036 pop_state ();
4040 static gfc_statement parse_executable (gfc_statement);
4042 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
4044 static void
4045 parse_if_block (void)
4047 gfc_code *top, *d;
4048 gfc_statement st;
4049 locus else_locus;
4050 gfc_state_data s;
4051 int seen_else;
4053 seen_else = 0;
4054 accept_statement (ST_IF_BLOCK);
4056 top = gfc_state_stack->tail;
4057 push_state (&s, COMP_IF, gfc_new_block);
4059 new_st.op = EXEC_IF;
4060 d = add_statement ();
4062 d->expr1 = top->expr1;
4063 top->expr1 = NULL;
4064 top->block = d;
4068 st = parse_executable (ST_NONE);
4070 switch (st)
4072 case ST_NONE:
4073 unexpected_eof ();
4075 case ST_ELSEIF:
4076 if (seen_else)
4078 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
4079 "statement at %L", &else_locus);
4081 reject_statement ();
4082 break;
4085 d = new_level (gfc_state_stack->head);
4086 d->op = EXEC_IF;
4087 d->expr1 = new_st.expr1;
4089 accept_statement (st);
4091 break;
4093 case ST_ELSE:
4094 if (seen_else)
4096 gfc_error ("Duplicate ELSE statements at %L and %C",
4097 &else_locus);
4098 reject_statement ();
4099 break;
4102 seen_else = 1;
4103 else_locus = gfc_current_locus;
4105 d = new_level (gfc_state_stack->head);
4106 d->op = EXEC_IF;
4108 accept_statement (st);
4110 break;
4112 case ST_ENDIF:
4113 break;
4115 default:
4116 unexpected_statement (st);
4117 break;
4120 while (st != ST_ENDIF);
4122 pop_state ();
4123 accept_statement (st);
4127 /* Parse a SELECT block. */
4129 static void
4130 parse_select_block (void)
4132 gfc_statement st;
4133 gfc_code *cp;
4134 gfc_state_data s;
4136 accept_statement (ST_SELECT_CASE);
4138 cp = gfc_state_stack->tail;
4139 push_state (&s, COMP_SELECT, gfc_new_block);
4141 /* Make sure that the next statement is a CASE or END SELECT. */
4142 for (;;)
4144 st = next_statement ();
4145 if (st == ST_NONE)
4146 unexpected_eof ();
4147 if (st == ST_END_SELECT)
4149 /* Empty SELECT CASE is OK. */
4150 accept_statement (st);
4151 pop_state ();
4152 return;
4154 if (st == ST_CASE)
4155 break;
4157 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
4158 "CASE at %C");
4160 reject_statement ();
4163 /* At this point, we're got a nonempty select block. */
4164 cp = new_level (cp);
4165 *cp = new_st;
4167 accept_statement (st);
4171 st = parse_executable (ST_NONE);
4172 switch (st)
4174 case ST_NONE:
4175 unexpected_eof ();
4177 case ST_CASE:
4178 cp = new_level (gfc_state_stack->head);
4179 *cp = new_st;
4180 gfc_clear_new_st ();
4182 accept_statement (st);
4183 /* Fall through */
4185 case ST_END_SELECT:
4186 break;
4188 /* Can't have an executable statement because of
4189 parse_executable(). */
4190 default:
4191 unexpected_statement (st);
4192 break;
4195 while (st != ST_END_SELECT);
4197 pop_state ();
4198 accept_statement (st);
4202 /* Pop the current selector from the SELECT TYPE stack. */
4204 static void
4205 select_type_pop (void)
4207 gfc_select_type_stack *old = select_type_stack;
4208 select_type_stack = old->prev;
4209 free (old);
4213 /* Parse a SELECT TYPE construct (F03:R821). */
4215 static void
4216 parse_select_type_block (void)
4218 gfc_statement st;
4219 gfc_code *cp;
4220 gfc_state_data s;
4222 gfc_current_ns = new_st.ext.block.ns;
4223 accept_statement (ST_SELECT_TYPE);
4225 cp = gfc_state_stack->tail;
4226 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
4228 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
4229 or END SELECT. */
4230 for (;;)
4232 st = next_statement ();
4233 if (st == ST_NONE)
4234 unexpected_eof ();
4235 if (st == ST_END_SELECT)
4236 /* Empty SELECT CASE is OK. */
4237 goto done;
4238 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
4239 break;
4241 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
4242 "following SELECT TYPE at %C");
4244 reject_statement ();
4247 /* At this point, we're got a nonempty select block. */
4248 cp = new_level (cp);
4249 *cp = new_st;
4251 accept_statement (st);
4255 st = parse_executable (ST_NONE);
4256 switch (st)
4258 case ST_NONE:
4259 unexpected_eof ();
4261 case ST_TYPE_IS:
4262 case ST_CLASS_IS:
4263 cp = new_level (gfc_state_stack->head);
4264 *cp = new_st;
4265 gfc_clear_new_st ();
4267 accept_statement (st);
4268 /* Fall through */
4270 case ST_END_SELECT:
4271 break;
4273 /* Can't have an executable statement because of
4274 parse_executable(). */
4275 default:
4276 unexpected_statement (st);
4277 break;
4280 while (st != ST_END_SELECT);
4282 done:
4283 pop_state ();
4284 accept_statement (st);
4285 gfc_current_ns = gfc_current_ns->parent;
4286 select_type_pop ();
4290 /* Given a symbol, make sure it is not an iteration variable for a DO
4291 statement. This subroutine is called when the symbol is seen in a
4292 context that causes it to become redefined. If the symbol is an
4293 iterator, we generate an error message and return nonzero. */
4296 gfc_check_do_variable (gfc_symtree *st)
4298 gfc_state_data *s;
4300 for (s=gfc_state_stack; s; s = s->previous)
4301 if (s->do_variable == st)
4303 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
4304 "loop beginning at %L", st->name, &s->head->loc);
4305 return 1;
4308 return 0;
4312 /* Checks to see if the current statement label closes an enddo.
4313 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
4314 an error) if it incorrectly closes an ENDDO. */
4316 static int
4317 check_do_closure (void)
4319 gfc_state_data *p;
4321 if (gfc_statement_label == NULL)
4322 return 0;
4324 for (p = gfc_state_stack; p; p = p->previous)
4325 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
4326 break;
4328 if (p == NULL)
4329 return 0; /* No loops to close */
4331 if (p->ext.end_do_label == gfc_statement_label)
4333 if (p == gfc_state_stack)
4334 return 1;
4336 gfc_error ("End of nonblock DO statement at %C is within another block");
4337 return 2;
4340 /* At this point, the label doesn't terminate the innermost loop.
4341 Make sure it doesn't terminate another one. */
4342 for (; p; p = p->previous)
4343 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
4344 && p->ext.end_do_label == gfc_statement_label)
4346 gfc_error ("End of nonblock DO statement at %C is interwoven "
4347 "with another DO loop");
4348 return 2;
4351 return 0;
4355 /* Parse a series of contained program units. */
4357 static void parse_progunit (gfc_statement);
4360 /* Parse a CRITICAL block. */
4362 static void
4363 parse_critical_block (void)
4365 gfc_code *top, *d;
4366 gfc_state_data s, *sd;
4367 gfc_statement st;
4369 for (sd = gfc_state_stack; sd; sd = sd->previous)
4370 if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
4371 gfc_error_now (is_oacc (sd)
4372 ? G_("CRITICAL block inside of OpenACC region at %C")
4373 : G_("CRITICAL block inside of OpenMP region at %C"));
4375 s.ext.end_do_label = new_st.label1;
4377 accept_statement (ST_CRITICAL);
4378 top = gfc_state_stack->tail;
4380 push_state (&s, COMP_CRITICAL, gfc_new_block);
4382 d = add_statement ();
4383 d->op = EXEC_CRITICAL;
4384 top->block = d;
4388 st = parse_executable (ST_NONE);
4390 switch (st)
4392 case ST_NONE:
4393 unexpected_eof ();
4394 break;
4396 case ST_END_CRITICAL:
4397 if (s.ext.end_do_label != NULL
4398 && s.ext.end_do_label != gfc_statement_label)
4399 gfc_error_now ("Statement label in END CRITICAL at %C does not "
4400 "match CRITICAL label");
4402 if (gfc_statement_label != NULL)
4404 new_st.op = EXEC_NOP;
4405 add_statement ();
4407 break;
4409 default:
4410 unexpected_statement (st);
4411 break;
4414 while (st != ST_END_CRITICAL);
4416 pop_state ();
4417 accept_statement (st);
4421 /* Set up the local namespace for a BLOCK construct. */
4423 gfc_namespace*
4424 gfc_build_block_ns (gfc_namespace *parent_ns)
4426 gfc_namespace* my_ns;
4427 static int numblock = 1;
4429 my_ns = gfc_get_namespace (parent_ns, 1);
4430 my_ns->construct_entities = 1;
4432 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
4433 code generation (so it must not be NULL).
4434 We set its recursive argument if our container procedure is recursive, so
4435 that local variables are accordingly placed on the stack when it
4436 will be necessary. */
4437 if (gfc_new_block)
4438 my_ns->proc_name = gfc_new_block;
4439 else
4441 bool t;
4442 char buffer[20]; /* Enough to hold "block@2147483648\n". */
4444 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
4445 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
4446 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
4447 my_ns->proc_name->name, NULL);
4448 gcc_assert (t);
4449 gfc_commit_symbol (my_ns->proc_name);
4452 if (parent_ns->proc_name)
4453 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
4455 return my_ns;
4459 /* Parse a BLOCK construct. */
4461 static void
4462 parse_block_construct (void)
4464 gfc_namespace* my_ns;
4465 gfc_namespace* my_parent;
4466 gfc_state_data s;
4468 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
4470 my_ns = gfc_build_block_ns (gfc_current_ns);
4472 new_st.op = EXEC_BLOCK;
4473 new_st.ext.block.ns = my_ns;
4474 new_st.ext.block.assoc = NULL;
4475 accept_statement (ST_BLOCK);
4477 push_state (&s, COMP_BLOCK, my_ns->proc_name);
4478 gfc_current_ns = my_ns;
4479 my_parent = my_ns->parent;
4481 parse_progunit (ST_NONE);
4483 /* Don't depend on the value of gfc_current_ns; it might have been
4484 reset if the block had errors and was cleaned up. */
4485 gfc_current_ns = my_parent;
4487 pop_state ();
4491 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
4492 behind the scenes with compiler-generated variables. */
4494 static void
4495 parse_associate (void)
4497 gfc_namespace* my_ns;
4498 gfc_state_data s;
4499 gfc_statement st;
4500 gfc_association_list* a;
4502 gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
4504 my_ns = gfc_build_block_ns (gfc_current_ns);
4506 new_st.op = EXEC_BLOCK;
4507 new_st.ext.block.ns = my_ns;
4508 gcc_assert (new_st.ext.block.assoc);
4510 /* Add all associate-names as BLOCK variables. Creating them is enough
4511 for now, they'll get their values during trans-* phase. */
4512 gfc_current_ns = my_ns;
4513 for (a = new_st.ext.block.assoc; a; a = a->next)
4515 gfc_symbol* sym;
4516 gfc_ref *ref;
4517 gfc_array_ref *array_ref;
4519 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
4520 gcc_unreachable ();
4522 sym = a->st->n.sym;
4523 sym->attr.flavor = FL_VARIABLE;
4524 sym->assoc = a;
4525 sym->declared_at = a->where;
4526 gfc_set_sym_referenced (sym);
4528 /* Initialize the typespec. It is not available in all cases,
4529 however, as it may only be set on the target during resolution.
4530 Still, sometimes it helps to have it right now -- especially
4531 for parsing component references on the associate-name
4532 in case of association to a derived-type. */
4533 sym->ts = a->target->ts;
4535 /* Check if the target expression is array valued. This can not always
4536 be done by looking at target.rank, because that might not have been
4537 set yet. Therefore traverse the chain of refs, looking for the last
4538 array ref and evaluate that. */
4539 array_ref = NULL;
4540 for (ref = a->target->ref; ref; ref = ref->next)
4541 if (ref->type == REF_ARRAY)
4542 array_ref = &ref->u.ar;
4543 if (array_ref || a->target->rank)
4545 gfc_array_spec *as;
4546 int dim, rank = 0;
4547 if (array_ref)
4549 a->rankguessed = 1;
4550 /* Count the dimension, that have a non-scalar extend. */
4551 for (dim = 0; dim < array_ref->dimen; ++dim)
4552 if (array_ref->dimen_type[dim] != DIMEN_ELEMENT
4553 && !(array_ref->dimen_type[dim] == DIMEN_UNKNOWN
4554 && array_ref->end[dim] == NULL
4555 && array_ref->start[dim] != NULL))
4556 ++rank;
4558 else
4559 rank = a->target->rank;
4560 /* When the rank is greater than zero then sym will be an array. */
4561 if (sym->ts.type == BT_CLASS)
4563 if ((!CLASS_DATA (sym)->as && rank != 0)
4564 || (CLASS_DATA (sym)->as
4565 && CLASS_DATA (sym)->as->rank != rank))
4567 /* Don't just (re-)set the attr and as in the sym.ts,
4568 because this modifies the target's attr and as. Copy the
4569 data and do a build_class_symbol. */
4570 symbol_attribute attr = CLASS_DATA (a->target)->attr;
4571 int corank = gfc_get_corank (a->target);
4572 gfc_typespec type;
4574 if (rank || corank)
4576 as = gfc_get_array_spec ();
4577 as->type = AS_DEFERRED;
4578 as->rank = rank;
4579 as->corank = corank;
4580 attr.dimension = rank ? 1 : 0;
4581 attr.codimension = corank ? 1 : 0;
4583 else
4585 as = NULL;
4586 attr.dimension = attr.codimension = 0;
4588 attr.class_ok = 0;
4589 type = CLASS_DATA (sym)->ts;
4590 if (!gfc_build_class_symbol (&type,
4591 &attr, &as))
4592 gcc_unreachable ();
4593 sym->ts = type;
4594 sym->ts.type = BT_CLASS;
4595 sym->attr.class_ok = 1;
4597 else
4598 sym->attr.class_ok = 1;
4600 else if ((!sym->as && rank != 0)
4601 || (sym->as && sym->as->rank != rank))
4603 as = gfc_get_array_spec ();
4604 as->type = AS_DEFERRED;
4605 as->rank = rank;
4606 as->corank = gfc_get_corank (a->target);
4607 sym->as = as;
4608 sym->attr.dimension = 1;
4609 if (as->corank)
4610 sym->attr.codimension = 1;
4615 accept_statement (ST_ASSOCIATE);
4616 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
4618 loop:
4619 st = parse_executable (ST_NONE);
4620 switch (st)
4622 case ST_NONE:
4623 unexpected_eof ();
4625 case_end:
4626 accept_statement (st);
4627 my_ns->code = gfc_state_stack->head;
4628 break;
4630 default:
4631 unexpected_statement (st);
4632 goto loop;
4635 gfc_current_ns = gfc_current_ns->parent;
4636 pop_state ();
4640 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4641 handled inside of parse_executable(), because they aren't really
4642 loop statements. */
4644 static void
4645 parse_do_block (void)
4647 gfc_statement st;
4648 gfc_code *top;
4649 gfc_state_data s;
4650 gfc_symtree *stree;
4651 gfc_exec_op do_op;
4653 do_op = new_st.op;
4654 s.ext.end_do_label = new_st.label1;
4656 if (new_st.ext.iterator != NULL)
4658 stree = new_st.ext.iterator->var->symtree;
4659 if (directive_unroll != -1)
4661 new_st.ext.iterator->unroll = directive_unroll;
4662 directive_unroll = -1;
4665 else
4666 stree = NULL;
4668 accept_statement (ST_DO);
4670 top = gfc_state_stack->tail;
4671 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
4672 gfc_new_block);
4674 s.do_variable = stree;
4676 top->block = new_level (top);
4677 top->block->op = EXEC_DO;
4679 loop:
4680 st = parse_executable (ST_NONE);
4682 switch (st)
4684 case ST_NONE:
4685 unexpected_eof ();
4687 case ST_ENDDO:
4688 if (s.ext.end_do_label != NULL
4689 && s.ext.end_do_label != gfc_statement_label)
4690 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4691 "DO label");
4693 if (gfc_statement_label != NULL)
4695 new_st.op = EXEC_NOP;
4696 add_statement ();
4698 break;
4700 case ST_IMPLIED_ENDDO:
4701 /* If the do-stmt of this DO construct has a do-construct-name,
4702 the corresponding end-do must be an end-do-stmt (with a matching
4703 name, but in that case we must have seen ST_ENDDO first).
4704 We only complain about this in pedantic mode. */
4705 if (gfc_current_block () != NULL)
4706 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4707 &gfc_current_block()->declared_at);
4709 break;
4711 default:
4712 unexpected_statement (st);
4713 goto loop;
4716 pop_state ();
4717 accept_statement (st);
4721 /* Parse the statements of OpenMP do/parallel do. */
4723 static gfc_statement
4724 parse_omp_do (gfc_statement omp_st)
4726 gfc_statement st;
4727 gfc_code *cp, *np;
4728 gfc_state_data s;
4730 accept_statement (omp_st);
4732 cp = gfc_state_stack->tail;
4733 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4734 np = new_level (cp);
4735 np->op = cp->op;
4736 np->block = NULL;
4738 for (;;)
4740 st = next_statement ();
4741 if (st == ST_NONE)
4742 unexpected_eof ();
4743 else if (st == ST_DO)
4744 break;
4745 else
4746 unexpected_statement (st);
4749 parse_do_block ();
4750 if (gfc_statement_label != NULL
4751 && gfc_state_stack->previous != NULL
4752 && gfc_state_stack->previous->state == COMP_DO
4753 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4755 /* In
4756 DO 100 I=1,10
4757 !$OMP DO
4758 DO J=1,10
4760 100 CONTINUE
4761 there should be no !$OMP END DO. */
4762 pop_state ();
4763 return ST_IMPLIED_ENDDO;
4766 check_do_closure ();
4767 pop_state ();
4769 st = next_statement ();
4770 gfc_statement omp_end_st = ST_OMP_END_DO;
4771 switch (omp_st)
4773 case ST_OMP_DISTRIBUTE: omp_end_st = ST_OMP_END_DISTRIBUTE; break;
4774 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4775 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4776 break;
4777 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4778 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4779 break;
4780 case ST_OMP_DISTRIBUTE_SIMD:
4781 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4782 break;
4783 case ST_OMP_DO: omp_end_st = ST_OMP_END_DO; break;
4784 case ST_OMP_DO_SIMD: omp_end_st = ST_OMP_END_DO_SIMD; break;
4785 case ST_OMP_PARALLEL_DO: omp_end_st = ST_OMP_END_PARALLEL_DO; break;
4786 case ST_OMP_PARALLEL_DO_SIMD:
4787 omp_end_st = ST_OMP_END_PARALLEL_DO_SIMD;
4788 break;
4789 case ST_OMP_SIMD: omp_end_st = ST_OMP_END_SIMD; break;
4790 case ST_OMP_TARGET_PARALLEL_DO:
4791 omp_end_st = ST_OMP_END_TARGET_PARALLEL_DO;
4792 break;
4793 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
4794 omp_end_st = ST_OMP_END_TARGET_PARALLEL_DO_SIMD;
4795 break;
4796 case ST_OMP_TARGET_SIMD: omp_end_st = ST_OMP_END_TARGET_SIMD; break;
4797 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4798 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4799 break;
4800 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4801 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4802 break;
4803 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4804 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4805 break;
4806 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4807 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4808 break;
4809 case ST_OMP_TASKLOOP: omp_end_st = ST_OMP_END_TASKLOOP; break;
4810 case ST_OMP_TASKLOOP_SIMD: omp_end_st = ST_OMP_END_TASKLOOP_SIMD; break;
4811 case ST_OMP_TEAMS_DISTRIBUTE:
4812 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4813 break;
4814 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4815 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4816 break;
4817 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4818 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4819 break;
4820 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4821 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4822 break;
4823 default: gcc_unreachable ();
4825 if (st == omp_end_st)
4827 if (new_st.op == EXEC_OMP_END_NOWAIT)
4828 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4829 else
4830 gcc_assert (new_st.op == EXEC_NOP);
4831 gfc_clear_new_st ();
4832 gfc_commit_symbols ();
4833 gfc_warning_check ();
4834 st = next_statement ();
4836 return st;
4840 /* Parse the statements of OpenMP atomic directive. */
4842 static gfc_statement
4843 parse_omp_oacc_atomic (bool omp_p)
4845 gfc_statement st, st_atomic, st_end_atomic;
4846 gfc_code *cp, *np;
4847 gfc_state_data s;
4848 int count;
4850 if (omp_p)
4852 st_atomic = ST_OMP_ATOMIC;
4853 st_end_atomic = ST_OMP_END_ATOMIC;
4855 else
4857 st_atomic = ST_OACC_ATOMIC;
4858 st_end_atomic = ST_OACC_END_ATOMIC;
4860 accept_statement (st_atomic);
4862 cp = gfc_state_stack->tail;
4863 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4864 np = new_level (cp);
4865 np->op = cp->op;
4866 np->block = NULL;
4867 np->ext.omp_atomic = cp->ext.omp_atomic;
4868 count = 1 + ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4869 == GFC_OMP_ATOMIC_CAPTURE);
4871 while (count)
4873 st = next_statement ();
4874 if (st == ST_NONE)
4875 unexpected_eof ();
4876 else if (st == ST_ASSIGNMENT)
4878 accept_statement (st);
4879 count--;
4881 else
4882 unexpected_statement (st);
4885 pop_state ();
4887 st = next_statement ();
4888 if (st == st_end_atomic)
4890 gfc_clear_new_st ();
4891 gfc_commit_symbols ();
4892 gfc_warning_check ();
4893 st = next_statement ();
4895 else if ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4896 == GFC_OMP_ATOMIC_CAPTURE)
4897 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4898 return st;
4902 /* Parse the statements of an OpenACC structured block. */
4904 static void
4905 parse_oacc_structured_block (gfc_statement acc_st)
4907 gfc_statement st, acc_end_st;
4908 gfc_code *cp, *np;
4909 gfc_state_data s, *sd;
4911 for (sd = gfc_state_stack; sd; sd = sd->previous)
4912 if (sd->state == COMP_CRITICAL)
4913 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4915 accept_statement (acc_st);
4917 cp = gfc_state_stack->tail;
4918 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4919 np = new_level (cp);
4920 np->op = cp->op;
4921 np->block = NULL;
4922 switch (acc_st)
4924 case ST_OACC_PARALLEL:
4925 acc_end_st = ST_OACC_END_PARALLEL;
4926 break;
4927 case ST_OACC_KERNELS:
4928 acc_end_st = ST_OACC_END_KERNELS;
4929 break;
4930 case ST_OACC_DATA:
4931 acc_end_st = ST_OACC_END_DATA;
4932 break;
4933 case ST_OACC_HOST_DATA:
4934 acc_end_st = ST_OACC_END_HOST_DATA;
4935 break;
4936 default:
4937 gcc_unreachable ();
4942 st = parse_executable (ST_NONE);
4943 if (st == ST_NONE)
4944 unexpected_eof ();
4945 else if (st != acc_end_st)
4947 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
4948 reject_statement ();
4951 while (st != acc_end_st);
4953 gcc_assert (new_st.op == EXEC_NOP);
4955 gfc_clear_new_st ();
4956 gfc_commit_symbols ();
4957 gfc_warning_check ();
4958 pop_state ();
4961 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4963 static gfc_statement
4964 parse_oacc_loop (gfc_statement acc_st)
4966 gfc_statement st;
4967 gfc_code *cp, *np;
4968 gfc_state_data s, *sd;
4970 for (sd = gfc_state_stack; sd; sd = sd->previous)
4971 if (sd->state == COMP_CRITICAL)
4972 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4974 accept_statement (acc_st);
4976 cp = gfc_state_stack->tail;
4977 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4978 np = new_level (cp);
4979 np->op = cp->op;
4980 np->block = NULL;
4982 for (;;)
4984 st = next_statement ();
4985 if (st == ST_NONE)
4986 unexpected_eof ();
4987 else if (st == ST_DO)
4988 break;
4989 else
4991 gfc_error ("Expected DO loop at %C");
4992 reject_statement ();
4996 parse_do_block ();
4997 if (gfc_statement_label != NULL
4998 && gfc_state_stack->previous != NULL
4999 && gfc_state_stack->previous->state == COMP_DO
5000 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
5002 pop_state ();
5003 return ST_IMPLIED_ENDDO;
5006 check_do_closure ();
5007 pop_state ();
5009 st = next_statement ();
5010 if (st == ST_OACC_END_LOOP)
5011 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
5012 if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
5013 (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
5014 (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
5016 gcc_assert (new_st.op == EXEC_NOP);
5017 gfc_clear_new_st ();
5018 gfc_commit_symbols ();
5019 gfc_warning_check ();
5020 st = next_statement ();
5022 return st;
5026 /* Parse the statements of an OpenMP structured block. */
5028 static void
5029 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
5031 gfc_statement st, omp_end_st;
5032 gfc_code *cp, *np;
5033 gfc_state_data s;
5035 accept_statement (omp_st);
5037 cp = gfc_state_stack->tail;
5038 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
5039 np = new_level (cp);
5040 np->op = cp->op;
5041 np->block = NULL;
5043 switch (omp_st)
5045 case ST_OMP_PARALLEL:
5046 omp_end_st = ST_OMP_END_PARALLEL;
5047 break;
5048 case ST_OMP_PARALLEL_SECTIONS:
5049 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
5050 break;
5051 case ST_OMP_SECTIONS:
5052 omp_end_st = ST_OMP_END_SECTIONS;
5053 break;
5054 case ST_OMP_ORDERED:
5055 omp_end_st = ST_OMP_END_ORDERED;
5056 break;
5057 case ST_OMP_CRITICAL:
5058 omp_end_st = ST_OMP_END_CRITICAL;
5059 break;
5060 case ST_OMP_MASTER:
5061 omp_end_st = ST_OMP_END_MASTER;
5062 break;
5063 case ST_OMP_SINGLE:
5064 omp_end_st = ST_OMP_END_SINGLE;
5065 break;
5066 case ST_OMP_TARGET:
5067 omp_end_st = ST_OMP_END_TARGET;
5068 break;
5069 case ST_OMP_TARGET_DATA:
5070 omp_end_st = ST_OMP_END_TARGET_DATA;
5071 break;
5072 case ST_OMP_TARGET_TEAMS:
5073 omp_end_st = ST_OMP_END_TARGET_TEAMS;
5074 break;
5075 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
5076 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
5077 break;
5078 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
5079 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
5080 break;
5081 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5082 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
5083 break;
5084 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
5085 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
5086 break;
5087 case ST_OMP_TASK:
5088 omp_end_st = ST_OMP_END_TASK;
5089 break;
5090 case ST_OMP_TASKGROUP:
5091 omp_end_st = ST_OMP_END_TASKGROUP;
5092 break;
5093 case ST_OMP_TEAMS:
5094 omp_end_st = ST_OMP_END_TEAMS;
5095 break;
5096 case ST_OMP_TEAMS_DISTRIBUTE:
5097 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
5098 break;
5099 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
5100 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
5101 break;
5102 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5103 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
5104 break;
5105 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
5106 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
5107 break;
5108 case ST_OMP_DISTRIBUTE:
5109 omp_end_st = ST_OMP_END_DISTRIBUTE;
5110 break;
5111 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
5112 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
5113 break;
5114 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
5115 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
5116 break;
5117 case ST_OMP_DISTRIBUTE_SIMD:
5118 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
5119 break;
5120 case ST_OMP_WORKSHARE:
5121 omp_end_st = ST_OMP_END_WORKSHARE;
5122 break;
5123 case ST_OMP_PARALLEL_WORKSHARE:
5124 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
5125 break;
5126 default:
5127 gcc_unreachable ();
5132 if (workshare_stmts_only)
5134 /* Inside of !$omp workshare, only
5135 scalar assignments
5136 array assignments
5137 where statements and constructs
5138 forall statements and constructs
5139 !$omp atomic
5140 !$omp critical
5141 !$omp parallel
5142 are allowed. For !$omp critical these
5143 restrictions apply recursively. */
5144 bool cycle = true;
5146 st = next_statement ();
5147 for (;;)
5149 switch (st)
5151 case ST_NONE:
5152 unexpected_eof ();
5154 case ST_ASSIGNMENT:
5155 case ST_WHERE:
5156 case ST_FORALL:
5157 accept_statement (st);
5158 break;
5160 case ST_WHERE_BLOCK:
5161 parse_where_block ();
5162 break;
5164 case ST_FORALL_BLOCK:
5165 parse_forall_block ();
5166 break;
5168 case ST_OMP_PARALLEL:
5169 case ST_OMP_PARALLEL_SECTIONS:
5170 parse_omp_structured_block (st, false);
5171 break;
5173 case ST_OMP_PARALLEL_WORKSHARE:
5174 case ST_OMP_CRITICAL:
5175 parse_omp_structured_block (st, true);
5176 break;
5178 case ST_OMP_PARALLEL_DO:
5179 case ST_OMP_PARALLEL_DO_SIMD:
5180 st = parse_omp_do (st);
5181 continue;
5183 case ST_OMP_ATOMIC:
5184 st = parse_omp_oacc_atomic (true);
5185 continue;
5187 default:
5188 cycle = false;
5189 break;
5192 if (!cycle)
5193 break;
5195 st = next_statement ();
5198 else
5199 st = parse_executable (ST_NONE);
5200 if (st == ST_NONE)
5201 unexpected_eof ();
5202 else if (st == ST_OMP_SECTION
5203 && (omp_st == ST_OMP_SECTIONS
5204 || omp_st == ST_OMP_PARALLEL_SECTIONS))
5206 np = new_level (np);
5207 np->op = cp->op;
5208 np->block = NULL;
5210 else if (st != omp_end_st)
5211 unexpected_statement (st);
5213 while (st != omp_end_st);
5215 switch (new_st.op)
5217 case EXEC_OMP_END_NOWAIT:
5218 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
5219 break;
5220 case EXEC_OMP_END_CRITICAL:
5221 if (((cp->ext.omp_clauses == NULL) ^ (new_st.ext.omp_name == NULL))
5222 || (new_st.ext.omp_name != NULL
5223 && strcmp (cp->ext.omp_clauses->critical_name,
5224 new_st.ext.omp_name) != 0))
5225 gfc_error ("Name after !$omp critical and !$omp end critical does "
5226 "not match at %C");
5227 free (CONST_CAST (char *, new_st.ext.omp_name));
5228 new_st.ext.omp_name = NULL;
5229 break;
5230 case EXEC_OMP_END_SINGLE:
5231 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
5232 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
5233 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
5234 gfc_free_omp_clauses (new_st.ext.omp_clauses);
5235 break;
5236 case EXEC_NOP:
5237 break;
5238 default:
5239 gcc_unreachable ();
5242 gfc_clear_new_st ();
5243 gfc_commit_symbols ();
5244 gfc_warning_check ();
5245 pop_state ();
5249 /* Accept a series of executable statements. We return the first
5250 statement that doesn't fit to the caller. Any block statements are
5251 passed on to the correct handler, which usually passes the buck
5252 right back here. */
5254 static gfc_statement
5255 parse_executable (gfc_statement st)
5257 int close_flag;
5259 if (st == ST_NONE)
5260 st = next_statement ();
5262 for (;;)
5264 close_flag = check_do_closure ();
5265 if (close_flag)
5266 switch (st)
5268 case ST_GOTO:
5269 case ST_END_PROGRAM:
5270 case ST_RETURN:
5271 case ST_EXIT:
5272 case ST_END_FUNCTION:
5273 case ST_CYCLE:
5274 case ST_PAUSE:
5275 case ST_STOP:
5276 case ST_ERROR_STOP:
5277 case ST_END_SUBROUTINE:
5279 case ST_DO:
5280 case ST_FORALL:
5281 case ST_WHERE:
5282 case ST_SELECT_CASE:
5283 gfc_error ("%s statement at %C cannot terminate a non-block "
5284 "DO loop", gfc_ascii_statement (st));
5285 break;
5287 default:
5288 break;
5291 switch (st)
5293 case ST_NONE:
5294 unexpected_eof ();
5296 case ST_DATA:
5297 gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
5298 "first executable statement");
5299 /* Fall through. */
5301 case ST_FORMAT:
5302 case ST_ENTRY:
5303 case_executable:
5304 accept_statement (st);
5305 if (close_flag == 1)
5306 return ST_IMPLIED_ENDDO;
5307 break;
5309 case ST_BLOCK:
5310 parse_block_construct ();
5311 break;
5313 case ST_ASSOCIATE:
5314 parse_associate ();
5315 break;
5317 case ST_IF_BLOCK:
5318 parse_if_block ();
5319 break;
5321 case ST_SELECT_CASE:
5322 parse_select_block ();
5323 break;
5325 case ST_SELECT_TYPE:
5326 parse_select_type_block ();
5327 break;
5329 case ST_DO:
5330 parse_do_block ();
5331 if (check_do_closure () == 1)
5332 return ST_IMPLIED_ENDDO;
5333 break;
5335 case ST_CRITICAL:
5336 parse_critical_block ();
5337 break;
5339 case ST_WHERE_BLOCK:
5340 parse_where_block ();
5341 break;
5343 case ST_FORALL_BLOCK:
5344 parse_forall_block ();
5345 break;
5347 case ST_OACC_PARALLEL_LOOP:
5348 case ST_OACC_KERNELS_LOOP:
5349 case ST_OACC_LOOP:
5350 st = parse_oacc_loop (st);
5351 if (st == ST_IMPLIED_ENDDO)
5352 return st;
5353 continue;
5355 case ST_OACC_PARALLEL:
5356 case ST_OACC_KERNELS:
5357 case ST_OACC_DATA:
5358 case ST_OACC_HOST_DATA:
5359 parse_oacc_structured_block (st);
5360 break;
5362 case ST_OMP_PARALLEL:
5363 case ST_OMP_PARALLEL_SECTIONS:
5364 case ST_OMP_SECTIONS:
5365 case ST_OMP_ORDERED:
5366 case ST_OMP_CRITICAL:
5367 case ST_OMP_MASTER:
5368 case ST_OMP_SINGLE:
5369 case ST_OMP_TARGET:
5370 case ST_OMP_TARGET_DATA:
5371 case ST_OMP_TARGET_PARALLEL:
5372 case ST_OMP_TARGET_TEAMS:
5373 case ST_OMP_TEAMS:
5374 case ST_OMP_TASK:
5375 case ST_OMP_TASKGROUP:
5376 parse_omp_structured_block (st, false);
5377 break;
5379 case ST_OMP_WORKSHARE:
5380 case ST_OMP_PARALLEL_WORKSHARE:
5381 parse_omp_structured_block (st, true);
5382 break;
5384 case ST_OMP_DISTRIBUTE:
5385 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
5386 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
5387 case ST_OMP_DISTRIBUTE_SIMD:
5388 case ST_OMP_DO:
5389 case ST_OMP_DO_SIMD:
5390 case ST_OMP_PARALLEL_DO:
5391 case ST_OMP_PARALLEL_DO_SIMD:
5392 case ST_OMP_SIMD:
5393 case ST_OMP_TARGET_PARALLEL_DO:
5394 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
5395 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
5396 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
5397 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5398 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
5399 case ST_OMP_TASKLOOP:
5400 case ST_OMP_TASKLOOP_SIMD:
5401 case ST_OMP_TEAMS_DISTRIBUTE:
5402 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
5403 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5404 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
5405 st = parse_omp_do (st);
5406 if (st == ST_IMPLIED_ENDDO)
5407 return st;
5408 continue;
5410 case ST_OACC_ATOMIC:
5411 st = parse_omp_oacc_atomic (false);
5412 continue;
5414 case ST_OMP_ATOMIC:
5415 st = parse_omp_oacc_atomic (true);
5416 continue;
5418 default:
5419 return st;
5422 if (directive_unroll != -1)
5423 gfc_error ("%<GCC unroll%> directive does not commence a loop at %C");
5425 st = next_statement ();
5430 /* Fix the symbols for sibling functions. These are incorrectly added to
5431 the child namespace as the parser didn't know about this procedure. */
5433 static void
5434 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
5436 gfc_namespace *ns;
5437 gfc_symtree *st;
5438 gfc_symbol *old_sym;
5440 for (ns = siblings; ns; ns = ns->sibling)
5442 st = gfc_find_symtree (ns->sym_root, sym->name);
5444 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
5445 goto fixup_contained;
5447 if ((st->n.sym->attr.flavor == FL_DERIVED
5448 && sym->attr.generic && sym->attr.function)
5449 ||(sym->attr.flavor == FL_DERIVED
5450 && st->n.sym->attr.generic && st->n.sym->attr.function))
5451 goto fixup_contained;
5453 old_sym = st->n.sym;
5454 if (old_sym->ns == ns
5455 && !old_sym->attr.contained
5457 /* By 14.6.1.3, host association should be excluded
5458 for the following. */
5459 && !(old_sym->attr.external
5460 || (old_sym->ts.type != BT_UNKNOWN
5461 && !old_sym->attr.implicit_type)
5462 || old_sym->attr.flavor == FL_PARAMETER
5463 || old_sym->attr.use_assoc
5464 || old_sym->attr.in_common
5465 || old_sym->attr.in_equivalence
5466 || old_sym->attr.data
5467 || old_sym->attr.dummy
5468 || old_sym->attr.result
5469 || old_sym->attr.dimension
5470 || old_sym->attr.allocatable
5471 || old_sym->attr.intrinsic
5472 || old_sym->attr.generic
5473 || old_sym->attr.flavor == FL_NAMELIST
5474 || old_sym->attr.flavor == FL_LABEL
5475 || old_sym->attr.proc == PROC_ST_FUNCTION))
5477 /* Replace it with the symbol from the parent namespace. */
5478 st->n.sym = sym;
5479 sym->refs++;
5481 gfc_release_symbol (old_sym);
5484 fixup_contained:
5485 /* Do the same for any contained procedures. */
5486 gfc_fixup_sibling_symbols (sym, ns->contained);
5490 static void
5491 parse_contained (int module)
5493 gfc_namespace *ns, *parent_ns, *tmp;
5494 gfc_state_data s1, s2;
5495 gfc_statement st;
5496 gfc_symbol *sym;
5497 gfc_entry_list *el;
5498 locus old_loc;
5499 int contains_statements = 0;
5500 int seen_error = 0;
5502 push_state (&s1, COMP_CONTAINS, NULL);
5503 parent_ns = gfc_current_ns;
5507 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
5509 gfc_current_ns->sibling = parent_ns->contained;
5510 parent_ns->contained = gfc_current_ns;
5512 next:
5513 /* Process the next available statement. We come here if we got an error
5514 and rejected the last statement. */
5515 old_loc = gfc_current_locus;
5516 st = next_statement ();
5518 switch (st)
5520 case ST_NONE:
5521 unexpected_eof ();
5523 case ST_FUNCTION:
5524 case ST_SUBROUTINE:
5525 contains_statements = 1;
5526 accept_statement (st);
5528 push_state (&s2,
5529 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
5530 gfc_new_block);
5532 /* For internal procedures, create/update the symbol in the
5533 parent namespace. */
5535 if (!module)
5537 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
5538 gfc_error ("Contained procedure %qs at %C is already "
5539 "ambiguous", gfc_new_block->name);
5540 else
5542 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
5543 sym->name,
5544 &gfc_new_block->declared_at))
5546 if (st == ST_FUNCTION)
5547 gfc_add_function (&sym->attr, sym->name,
5548 &gfc_new_block->declared_at);
5549 else
5550 gfc_add_subroutine (&sym->attr, sym->name,
5551 &gfc_new_block->declared_at);
5555 gfc_commit_symbols ();
5557 else
5558 sym = gfc_new_block;
5560 /* Mark this as a contained function, so it isn't replaced
5561 by other module functions. */
5562 sym->attr.contained = 1;
5564 /* Set implicit_pure so that it can be reset if any of the
5565 tests for purity fail. This is used for some optimisation
5566 during translation. */
5567 if (!sym->attr.pure)
5568 sym->attr.implicit_pure = 1;
5570 parse_progunit (ST_NONE);
5572 /* Fix up any sibling functions that refer to this one. */
5573 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
5574 /* Or refer to any of its alternate entry points. */
5575 for (el = gfc_current_ns->entries; el; el = el->next)
5576 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
5578 gfc_current_ns->code = s2.head;
5579 gfc_current_ns = parent_ns;
5581 pop_state ();
5582 break;
5584 /* These statements are associated with the end of the host unit. */
5585 case ST_END_FUNCTION:
5586 case ST_END_MODULE:
5587 case ST_END_SUBMODULE:
5588 case ST_END_PROGRAM:
5589 case ST_END_SUBROUTINE:
5590 accept_statement (st);
5591 gfc_current_ns->code = s1.head;
5592 break;
5594 default:
5595 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
5596 gfc_ascii_statement (st));
5597 reject_statement ();
5598 seen_error = 1;
5599 goto next;
5600 break;
5603 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
5604 && st != ST_END_MODULE && st != ST_END_SUBMODULE
5605 && st != ST_END_PROGRAM);
5607 /* The first namespace in the list is guaranteed to not have
5608 anything (worthwhile) in it. */
5609 tmp = gfc_current_ns;
5610 gfc_current_ns = parent_ns;
5611 if (seen_error && tmp->refs > 1)
5612 gfc_free_namespace (tmp);
5614 ns = gfc_current_ns->contained;
5615 gfc_current_ns->contained = ns->sibling;
5616 gfc_free_namespace (ns);
5618 pop_state ();
5619 if (!contains_statements)
5620 gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
5621 "FUNCTION or SUBROUTINE statement at %L", &old_loc);
5625 /* The result variable in a MODULE PROCEDURE needs to be created and
5626 its characteristics copied from the interface since it is neither
5627 declared in the procedure declaration nor in the specification
5628 part. */
5630 static void
5631 get_modproc_result (void)
5633 gfc_symbol *proc;
5634 if (gfc_state_stack->previous
5635 && gfc_state_stack->previous->state == COMP_CONTAINS
5636 && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
5638 proc = gfc_current_ns->proc_name ? gfc_current_ns->proc_name : NULL;
5639 if (proc != NULL
5640 && proc->attr.function
5641 && proc->tlink
5642 && proc->tlink->result
5643 && proc->tlink->result != proc->tlink)
5645 gfc_copy_dummy_sym (&proc->result, proc->tlink->result, 1);
5646 gfc_set_sym_referenced (proc->result);
5647 proc->result->attr.if_source = IFSRC_DECL;
5648 gfc_commit_symbol (proc->result);
5654 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
5656 static void
5657 parse_progunit (gfc_statement st)
5659 gfc_state_data *p;
5660 int n;
5662 if (gfc_new_block
5663 && gfc_new_block->abr_modproc_decl
5664 && gfc_new_block->attr.function)
5665 get_modproc_result ();
5667 st = parse_spec (st);
5668 switch (st)
5670 case ST_NONE:
5671 unexpected_eof ();
5673 case ST_CONTAINS:
5674 /* This is not allowed within BLOCK! */
5675 if (gfc_current_state () != COMP_BLOCK)
5676 goto contains;
5677 break;
5679 case_end:
5680 accept_statement (st);
5681 goto done;
5683 default:
5684 break;
5687 if (gfc_current_state () == COMP_FUNCTION)
5688 gfc_check_function_type (gfc_current_ns);
5690 loop:
5691 for (;;)
5693 st = parse_executable (st);
5695 switch (st)
5697 case ST_NONE:
5698 unexpected_eof ();
5700 case ST_CONTAINS:
5701 /* This is not allowed within BLOCK! */
5702 if (gfc_current_state () != COMP_BLOCK)
5703 goto contains;
5704 break;
5706 case_end:
5707 accept_statement (st);
5708 goto done;
5710 default:
5711 break;
5714 unexpected_statement (st);
5715 reject_statement ();
5716 st = next_statement ();
5719 contains:
5720 n = 0;
5722 for (p = gfc_state_stack; p; p = p->previous)
5723 if (p->state == COMP_CONTAINS)
5724 n++;
5726 if (gfc_find_state (COMP_MODULE) == true
5727 || gfc_find_state (COMP_SUBMODULE) == true)
5728 n--;
5730 if (n > 0)
5732 gfc_error ("CONTAINS statement at %C is already in a contained "
5733 "program unit");
5734 reject_statement ();
5735 st = next_statement ();
5736 goto loop;
5739 parse_contained (0);
5741 done:
5742 gfc_current_ns->code = gfc_state_stack->head;
5746 /* Come here to complain about a global symbol already in use as
5747 something else. */
5749 void
5750 gfc_global_used (gfc_gsymbol *sym, locus *where)
5752 const char *name;
5754 if (where == NULL)
5755 where = &gfc_current_locus;
5757 switch(sym->type)
5759 case GSYM_PROGRAM:
5760 name = "PROGRAM";
5761 break;
5762 case GSYM_FUNCTION:
5763 name = "FUNCTION";
5764 break;
5765 case GSYM_SUBROUTINE:
5766 name = "SUBROUTINE";
5767 break;
5768 case GSYM_COMMON:
5769 name = "COMMON";
5770 break;
5771 case GSYM_BLOCK_DATA:
5772 name = "BLOCK DATA";
5773 break;
5774 case GSYM_MODULE:
5775 name = "MODULE";
5776 break;
5777 default:
5778 name = NULL;
5781 if (name)
5783 if (sym->binding_label)
5784 gfc_error ("Global binding name %qs at %L is already being used "
5785 "as a %s at %L", sym->binding_label, where, name,
5786 &sym->where);
5787 else
5788 gfc_error ("Global name %qs at %L is already being used as "
5789 "a %s at %L", sym->name, where, name, &sym->where);
5791 else
5793 if (sym->binding_label)
5794 gfc_error ("Global binding name %qs at %L is already being used "
5795 "at %L", sym->binding_label, where, &sym->where);
5796 else
5797 gfc_error ("Global name %qs at %L is already being used at %L",
5798 sym->name, where, &sym->where);
5803 /* Parse a block data program unit. */
5805 static void
5806 parse_block_data (void)
5808 gfc_statement st;
5809 static locus blank_locus;
5810 static int blank_block=0;
5811 gfc_gsymbol *s;
5813 gfc_current_ns->proc_name = gfc_new_block;
5814 gfc_current_ns->is_block_data = 1;
5816 if (gfc_new_block == NULL)
5818 if (blank_block)
5819 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5820 "prior BLOCK DATA at %L", &blank_locus);
5821 else
5823 blank_block = 1;
5824 blank_locus = gfc_current_locus;
5827 else
5829 s = gfc_get_gsymbol (gfc_new_block->name);
5830 if (s->defined
5831 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
5832 gfc_global_used (s, &gfc_new_block->declared_at);
5833 else
5835 s->type = GSYM_BLOCK_DATA;
5836 s->where = gfc_new_block->declared_at;
5837 s->defined = 1;
5841 st = parse_spec (ST_NONE);
5843 while (st != ST_END_BLOCK_DATA)
5845 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5846 gfc_ascii_statement (st));
5847 reject_statement ();
5848 st = next_statement ();
5853 /* Following the association of the ancestor (sub)module symbols, they
5854 must be set host rather than use associated and all must be public.
5855 They are flagged up by 'used_in_submodule' so that they can be set
5856 DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl). Otherwise the
5857 linker chokes on multiple symbol definitions. */
5859 static void
5860 set_syms_host_assoc (gfc_symbol *sym)
5862 gfc_component *c;
5863 const char dot[2] = ".";
5864 char parent1[GFC_MAX_SYMBOL_LEN + 1];
5865 char parent2[GFC_MAX_SYMBOL_LEN + 1];
5867 if (sym == NULL)
5868 return;
5870 if (sym->attr.module_procedure)
5871 sym->attr.external = 0;
5873 sym->attr.use_assoc = 0;
5874 sym->attr.host_assoc = 1;
5875 sym->attr.used_in_submodule =1;
5877 if (sym->attr.flavor == FL_DERIVED)
5879 /* Derived types with PRIVATE components that are declared in
5880 modules other than the parent module must not be changed to be
5881 PUBLIC. The 'use-assoc' attribute must be reset so that the
5882 test in symbol.c(gfc_find_component) works correctly. This is
5883 not necessary for PRIVATE symbols since they are not read from
5884 the module. */
5885 memset(parent1, '\0', sizeof(parent1));
5886 memset(parent2, '\0', sizeof(parent2));
5887 strcpy (parent1, gfc_new_block->name);
5888 strcpy (parent2, sym->module);
5889 if (strcmp (strtok (parent1, dot), strtok (parent2, dot)) == 0)
5891 for (c = sym->components; c; c = c->next)
5892 c->attr.access = ACCESS_PUBLIC;
5894 else
5896 sym->attr.use_assoc = 1;
5897 sym->attr.host_assoc = 0;
5902 /* Parse a module subprogram. */
5904 static void
5905 parse_module (void)
5907 gfc_statement st;
5908 gfc_gsymbol *s;
5909 bool error;
5911 s = gfc_get_gsymbol (gfc_new_block->name);
5912 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
5913 gfc_global_used (s, &gfc_new_block->declared_at);
5914 else
5916 s->type = GSYM_MODULE;
5917 s->where = gfc_new_block->declared_at;
5918 s->defined = 1;
5921 /* Something is nulling the module_list after this point. This is good
5922 since it allows us to 'USE' the parent modules that the submodule
5923 inherits and to set (most) of the symbols as host associated. */
5924 if (gfc_current_state () == COMP_SUBMODULE)
5926 use_modules ();
5927 gfc_traverse_ns (gfc_current_ns, set_syms_host_assoc);
5930 st = parse_spec (ST_NONE);
5932 error = false;
5933 loop:
5934 switch (st)
5936 case ST_NONE:
5937 unexpected_eof ();
5939 case ST_CONTAINS:
5940 parse_contained (1);
5941 break;
5943 case ST_END_MODULE:
5944 case ST_END_SUBMODULE:
5945 accept_statement (st);
5946 break;
5948 default:
5949 gfc_error ("Unexpected %s statement in MODULE at %C",
5950 gfc_ascii_statement (st));
5952 error = true;
5953 reject_statement ();
5954 st = next_statement ();
5955 goto loop;
5958 /* Make sure not to free the namespace twice on error. */
5959 if (!error)
5960 s->ns = gfc_current_ns;
5964 /* Add a procedure name to the global symbol table. */
5966 static void
5967 add_global_procedure (bool sub)
5969 gfc_gsymbol *s;
5971 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5972 name is a global identifier. */
5973 if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
5975 s = gfc_get_gsymbol (gfc_new_block->name);
5977 if (s->defined
5978 || (s->type != GSYM_UNKNOWN
5979 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5981 gfc_global_used (s, &gfc_new_block->declared_at);
5982 /* Silence follow-up errors. */
5983 gfc_new_block->binding_label = NULL;
5985 else
5987 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5988 s->sym_name = gfc_new_block->name;
5989 s->where = gfc_new_block->declared_at;
5990 s->defined = 1;
5991 s->ns = gfc_current_ns;
5995 /* Don't add the symbol multiple times. */
5996 if (gfc_new_block->binding_label
5997 && (!gfc_notification_std (GFC_STD_F2008)
5998 || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
6000 s = gfc_get_gsymbol (gfc_new_block->binding_label);
6002 if (s->defined
6003 || (s->type != GSYM_UNKNOWN
6004 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
6006 gfc_global_used (s, &gfc_new_block->declared_at);
6007 /* Silence follow-up errors. */
6008 gfc_new_block->binding_label = NULL;
6010 else
6012 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
6013 s->sym_name = gfc_new_block->name;
6014 s->binding_label = gfc_new_block->binding_label;
6015 s->where = gfc_new_block->declared_at;
6016 s->defined = 1;
6017 s->ns = gfc_current_ns;
6023 /* Add a program to the global symbol table. */
6025 static void
6026 add_global_program (void)
6028 gfc_gsymbol *s;
6030 if (gfc_new_block == NULL)
6031 return;
6032 s = gfc_get_gsymbol (gfc_new_block->name);
6034 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
6035 gfc_global_used (s, &gfc_new_block->declared_at);
6036 else
6038 s->type = GSYM_PROGRAM;
6039 s->where = gfc_new_block->declared_at;
6040 s->defined = 1;
6041 s->ns = gfc_current_ns;
6046 /* Resolve all the program units. */
6047 static void
6048 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
6050 gfc_free_dt_list ();
6051 gfc_current_ns = gfc_global_ns_list;
6052 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6054 if (gfc_current_ns->proc_name
6055 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
6056 continue; /* Already resolved. */
6058 if (gfc_current_ns->proc_name)
6059 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
6060 gfc_resolve (gfc_current_ns);
6061 gfc_current_ns->derived_types = gfc_derived_types;
6062 gfc_derived_types = NULL;
6067 static void
6068 clean_up_modules (gfc_gsymbol *gsym)
6070 if (gsym == NULL)
6071 return;
6073 clean_up_modules (gsym->left);
6074 clean_up_modules (gsym->right);
6076 if (gsym->type != GSYM_MODULE || !gsym->ns)
6077 return;
6079 gfc_current_ns = gsym->ns;
6080 gfc_derived_types = gfc_current_ns->derived_types;
6081 gfc_done_2 ();
6082 gsym->ns = NULL;
6083 return;
6087 /* Translate all the program units. This could be in a different order
6088 to resolution if there are forward references in the file. */
6089 static void
6090 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
6092 int errors;
6094 gfc_current_ns = gfc_global_ns_list;
6095 gfc_get_errors (NULL, &errors);
6097 /* We first translate all modules to make sure that later parts
6098 of the program can use the decl. Then we translate the nonmodules. */
6100 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6102 if (!gfc_current_ns->proc_name
6103 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6104 continue;
6106 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
6107 gfc_derived_types = gfc_current_ns->derived_types;
6108 gfc_generate_module_code (gfc_current_ns);
6109 gfc_current_ns->translated = 1;
6112 gfc_current_ns = gfc_global_ns_list;
6113 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6115 if (gfc_current_ns->proc_name
6116 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
6117 continue;
6119 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
6120 gfc_derived_types = gfc_current_ns->derived_types;
6121 gfc_generate_code (gfc_current_ns);
6122 gfc_current_ns->translated = 1;
6125 /* Clean up all the namespaces after translation. */
6126 gfc_current_ns = gfc_global_ns_list;
6127 for (;gfc_current_ns;)
6129 gfc_namespace *ns;
6131 if (gfc_current_ns->proc_name
6132 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
6134 gfc_current_ns = gfc_current_ns->sibling;
6135 continue;
6138 ns = gfc_current_ns->sibling;
6139 gfc_derived_types = gfc_current_ns->derived_types;
6140 gfc_done_2 ();
6141 gfc_current_ns = ns;
6144 clean_up_modules (gfc_gsym_root);
6148 /* Top level parser. */
6150 bool
6151 gfc_parse_file (void)
6153 int seen_program, errors_before, errors;
6154 gfc_state_data top, s;
6155 gfc_statement st;
6156 locus prog_locus;
6157 gfc_namespace *next;
6159 gfc_start_source_files ();
6161 top.state = COMP_NONE;
6162 top.sym = NULL;
6163 top.previous = NULL;
6164 top.head = top.tail = NULL;
6165 top.do_variable = NULL;
6167 gfc_state_stack = &top;
6169 gfc_clear_new_st ();
6171 gfc_statement_label = NULL;
6173 if (setjmp (eof_buf))
6174 return false; /* Come here on unexpected EOF */
6176 /* Prepare the global namespace that will contain the
6177 program units. */
6178 gfc_global_ns_list = next = NULL;
6180 seen_program = 0;
6181 errors_before = 0;
6183 /* Exit early for empty files. */
6184 if (gfc_at_eof ())
6185 goto done;
6187 in_specification_block = true;
6188 loop:
6189 gfc_init_2 ();
6190 st = next_statement ();
6191 switch (st)
6193 case ST_NONE:
6194 gfc_done_2 ();
6195 goto done;
6197 case ST_PROGRAM:
6198 if (seen_program)
6199 goto duplicate_main;
6200 seen_program = 1;
6201 prog_locus = gfc_current_locus;
6203 push_state (&s, COMP_PROGRAM, gfc_new_block);
6204 main_program_symbol (gfc_current_ns, gfc_new_block->name);
6205 accept_statement (st);
6206 add_global_program ();
6207 parse_progunit (ST_NONE);
6208 goto prog_units;
6210 case ST_SUBROUTINE:
6211 add_global_procedure (true);
6212 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
6213 accept_statement (st);
6214 parse_progunit (ST_NONE);
6215 goto prog_units;
6217 case ST_FUNCTION:
6218 add_global_procedure (false);
6219 push_state (&s, COMP_FUNCTION, gfc_new_block);
6220 accept_statement (st);
6221 parse_progunit (ST_NONE);
6222 goto prog_units;
6224 case ST_BLOCK_DATA:
6225 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
6226 accept_statement (st);
6227 parse_block_data ();
6228 break;
6230 case ST_MODULE:
6231 push_state (&s, COMP_MODULE, gfc_new_block);
6232 accept_statement (st);
6234 gfc_get_errors (NULL, &errors_before);
6235 parse_module ();
6236 break;
6238 case ST_SUBMODULE:
6239 push_state (&s, COMP_SUBMODULE, gfc_new_block);
6240 accept_statement (st);
6242 gfc_get_errors (NULL, &errors_before);
6243 parse_module ();
6244 break;
6246 /* Anything else starts a nameless main program block. */
6247 default:
6248 if (seen_program)
6249 goto duplicate_main;
6250 seen_program = 1;
6251 prog_locus = gfc_current_locus;
6253 push_state (&s, COMP_PROGRAM, gfc_new_block);
6254 main_program_symbol (gfc_current_ns, "MAIN__");
6255 parse_progunit (st);
6256 goto prog_units;
6259 /* Handle the non-program units. */
6260 gfc_current_ns->code = s.head;
6262 gfc_resolve (gfc_current_ns);
6264 /* Dump the parse tree if requested. */
6265 if (flag_dump_fortran_original)
6266 gfc_dump_parse_tree (gfc_current_ns, stdout);
6268 if (flag_c_prototypes)
6269 gfc_dump_c_prototypes (gfc_current_ns, stdout);
6271 gfc_get_errors (NULL, &errors);
6272 if (s.state == COMP_MODULE || s.state == COMP_SUBMODULE)
6274 gfc_dump_module (s.sym->name, errors_before == errors);
6275 gfc_current_ns->derived_types = gfc_derived_types;
6276 gfc_derived_types = NULL;
6277 goto prog_units;
6279 else
6281 if (errors == 0)
6282 gfc_generate_code (gfc_current_ns);
6283 pop_state ();
6284 gfc_done_2 ();
6287 goto loop;
6289 prog_units:
6290 /* The main program and non-contained procedures are put
6291 in the global namespace list, so that they can be processed
6292 later and all their interfaces resolved. */
6293 gfc_current_ns->code = s.head;
6294 if (next)
6296 for (; next->sibling; next = next->sibling)
6298 next->sibling = gfc_current_ns;
6300 else
6301 gfc_global_ns_list = gfc_current_ns;
6303 next = gfc_current_ns;
6305 pop_state ();
6306 goto loop;
6308 done:
6309 /* Do the resolution. */
6310 resolve_all_program_units (gfc_global_ns_list);
6312 /* Do the parse tree dump. */
6313 gfc_current_ns = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
6315 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6316 if (!gfc_current_ns->proc_name
6317 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6319 gfc_dump_parse_tree (gfc_current_ns, stdout);
6320 fputs ("------------------------------------------\n\n", stdout);
6323 /* Do the translation. */
6324 translate_all_program_units (gfc_global_ns_list);
6326 gfc_end_source_files ();
6327 return true;
6329 duplicate_main:
6330 /* If we see a duplicate main program, shut down. If the second
6331 instance is an implied main program, i.e. data decls or executable
6332 statements, we're in for lots of errors. */
6333 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
6334 reject_statement ();
6335 gfc_done_2 ();
6336 return true;
6339 /* Return true if this state data represents an OpenACC region. */
6340 bool
6341 is_oacc (gfc_state_data *sd)
6343 switch (sd->construct->op)
6345 case EXEC_OACC_PARALLEL_LOOP:
6346 case EXEC_OACC_PARALLEL:
6347 case EXEC_OACC_KERNELS_LOOP:
6348 case EXEC_OACC_KERNELS:
6349 case EXEC_OACC_DATA:
6350 case EXEC_OACC_HOST_DATA:
6351 case EXEC_OACC_LOOP:
6352 case EXEC_OACC_UPDATE:
6353 case EXEC_OACC_WAIT:
6354 case EXEC_OACC_CACHE:
6355 case EXEC_OACC_ENTER_DATA:
6356 case EXEC_OACC_EXIT_DATA:
6357 case EXEC_OACC_ATOMIC:
6358 case EXEC_OACC_ROUTINE:
6359 return true;
6361 default:
6362 return false;