2017-02-17 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / fortran / parse.c
blobc9f8da46ed3d04fee0481b44c668761d07ad5ae1
1 /* Main parser.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "gfortran.h"
26 #include <setjmp.h>
27 #include "match.h"
28 #include "parse.h"
30 /* Current statement label. Zero means no statement label. Because new_st
31 can get wiped during statement matching, we have to keep it separate. */
33 gfc_st_label *gfc_statement_label;
35 static locus label_locus;
36 static jmp_buf eof_buf;
38 gfc_state_data *gfc_state_stack;
39 static bool last_was_use_stmt = false;
41 /* TODO: Re-order functions to kill these forward decls. */
42 static void check_statement_label (gfc_statement);
43 static void undo_new_statement (void);
44 static void reject_statement (void);
47 /* A sort of half-matching function. We try to match the word on the
48 input with the passed string. If this succeeds, we call the
49 keyword-dependent matching function that will match the rest of the
50 statement. For single keywords, the matching subroutine is
51 gfc_match_eos(). */
53 static match
54 match_word (const char *str, match (*subr) (void), locus *old_locus)
56 match m;
58 if (str != NULL)
60 m = gfc_match (str);
61 if (m != MATCH_YES)
62 return m;
65 m = (*subr) ();
67 if (m != MATCH_YES)
69 gfc_current_locus = *old_locus;
70 reject_statement ();
73 return m;
77 /* Like match_word, but if str is matched, set a flag that it
78 was matched. */
79 static match
80 match_word_omp_simd (const char *str, match (*subr) (void), locus *old_locus,
81 bool *simd_matched)
83 match m;
85 if (str != NULL)
87 m = gfc_match (str);
88 if (m != MATCH_YES)
89 return m;
90 *simd_matched = true;
93 m = (*subr) ();
95 if (m != MATCH_YES)
97 gfc_current_locus = *old_locus;
98 reject_statement ();
101 return m;
105 /* Load symbols from all USE statements encountered in this scoping unit. */
107 static void
108 use_modules (void)
110 gfc_error_buffer old_error;
112 gfc_push_error (&old_error);
113 gfc_buffer_error (false);
114 gfc_use_modules ();
115 gfc_buffer_error (true);
116 gfc_pop_error (&old_error);
117 gfc_commit_symbols ();
118 gfc_warning_check ();
119 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
120 gfc_current_ns->old_data = gfc_current_ns->data;
121 last_was_use_stmt = false;
125 /* Figure out what the next statement is, (mostly) regardless of
126 proper ordering. The do...while(0) is there to prevent if/else
127 ambiguity. */
129 #define match(keyword, subr, st) \
130 do { \
131 if (match_word (keyword, subr, &old_locus) == MATCH_YES) \
132 return st; \
133 else \
134 undo_new_statement (); \
135 } while (0);
138 /* This is a specialist version of decode_statement that is used
139 for the specification statements in a function, whose
140 characteristics are deferred into the specification statements.
141 eg.: INTEGER (king = mykind) foo ()
142 USE mymodule, ONLY mykind.....
143 The KIND parameter needs a return after USE or IMPORT, whereas
144 derived type declarations can occur anywhere, up the executable
145 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
146 out of the correct kind of specification statements. */
147 static gfc_statement
148 decode_specification_statement (void)
150 gfc_statement st;
151 locus old_locus;
152 char c;
154 if (gfc_match_eos () == MATCH_YES)
155 return ST_NONE;
157 old_locus = gfc_current_locus;
159 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
161 last_was_use_stmt = true;
162 return ST_USE;
164 else
166 undo_new_statement ();
167 if (last_was_use_stmt)
168 use_modules ();
171 match ("import", gfc_match_import, ST_IMPORT);
173 if (gfc_current_block ()->result->ts.type != BT_DERIVED)
174 goto end_of_block;
176 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
177 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
178 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
180 /* General statement matching: Instead of testing every possible
181 statement, we eliminate most possibilities by peeking at the
182 first character. */
184 c = gfc_peek_ascii_char ();
186 switch (c)
188 case 'a':
189 match ("abstract% interface", gfc_match_abstract_interface,
190 ST_INTERFACE);
191 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
192 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
193 match ("automatic", gfc_match_automatic, ST_ATTR_DECL);
194 break;
196 case 'b':
197 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
198 break;
200 case 'c':
201 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
202 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
203 break;
205 case 'd':
206 match ("data", gfc_match_data, ST_DATA);
207 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
208 break;
210 case 'e':
211 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
212 match ("entry% ", gfc_match_entry, ST_ENTRY);
213 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
214 match ("external", gfc_match_external, ST_ATTR_DECL);
215 break;
217 case 'f':
218 match ("format", gfc_match_format, ST_FORMAT);
219 break;
221 case 'g':
222 break;
224 case 'i':
225 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
226 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
227 match ("interface", gfc_match_interface, ST_INTERFACE);
228 match ("intent", gfc_match_intent, ST_ATTR_DECL);
229 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
230 break;
232 case 'm':
233 break;
235 case 'n':
236 match ("namelist", gfc_match_namelist, ST_NAMELIST);
237 break;
239 case 'o':
240 match ("optional", gfc_match_optional, ST_ATTR_DECL);
241 break;
243 case 'p':
244 match ("parameter", gfc_match_parameter, ST_PARAMETER);
245 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
246 if (gfc_match_private (&st) == MATCH_YES)
247 return st;
248 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
249 if (gfc_match_public (&st) == MATCH_YES)
250 return st;
251 match ("protected", gfc_match_protected, ST_ATTR_DECL);
252 break;
254 case 'r':
255 break;
257 case 's':
258 match ("save", gfc_match_save, ST_ATTR_DECL);
259 match ("static", gfc_match_static, ST_ATTR_DECL);
260 match ("structure", gfc_match_structure_decl, ST_STRUCTURE_DECL);
261 break;
263 case 't':
264 match ("target", gfc_match_target, ST_ATTR_DECL);
265 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
266 break;
268 case 'u':
269 break;
271 case 'v':
272 match ("value", gfc_match_value, ST_ATTR_DECL);
273 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
274 break;
276 case 'w':
277 break;
280 /* This is not a specification statement. See if any of the matchers
281 has stored an error message of some sort. */
283 end_of_block:
284 gfc_clear_error ();
285 gfc_buffer_error (false);
286 gfc_current_locus = old_locus;
288 return ST_GET_FCN_CHARACTERISTICS;
291 static bool in_specification_block;
293 /* This is the primary 'decode_statement'. */
294 static gfc_statement
295 decode_statement (void)
297 gfc_statement st;
298 locus old_locus;
299 match m = MATCH_NO;
300 char c;
302 gfc_enforce_clean_symbol_state ();
304 gfc_clear_error (); /* Clear any pending errors. */
305 gfc_clear_warning (); /* Clear any pending warnings. */
307 gfc_matching_function = false;
309 if (gfc_match_eos () == MATCH_YES)
310 return ST_NONE;
312 if (gfc_current_state () == COMP_FUNCTION
313 && gfc_current_block ()->result->ts.kind == -1)
314 return decode_specification_statement ();
316 old_locus = gfc_current_locus;
318 c = gfc_peek_ascii_char ();
320 if (c == 'u')
322 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
324 last_was_use_stmt = true;
325 return ST_USE;
327 else
328 undo_new_statement ();
331 if (last_was_use_stmt)
332 use_modules ();
334 /* Try matching a data declaration or function declaration. The
335 input "REALFUNCTIONA(N)" can mean several things in different
336 contexts, so it (and its relatives) get special treatment. */
338 if (gfc_current_state () == COMP_NONE
339 || gfc_current_state () == COMP_INTERFACE
340 || gfc_current_state () == COMP_CONTAINS)
342 gfc_matching_function = true;
343 m = gfc_match_function_decl ();
344 if (m == MATCH_YES)
345 return ST_FUNCTION;
346 else if (m == MATCH_ERROR)
347 reject_statement ();
348 else
349 gfc_undo_symbols ();
350 gfc_current_locus = old_locus;
352 gfc_matching_function = false;
354 /* Legacy parameter statements are ambiguous with assignments so try parameter
355 first. */
356 match ("parameter", gfc_match_parameter, ST_PARAMETER);
358 /* Match statements whose error messages are meant to be overwritten
359 by something better. */
361 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
362 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
364 if (in_specification_block)
366 m = match_word (NULL, gfc_match_st_function, &old_locus);
367 if (m == MATCH_YES)
368 return ST_STATEMENT_FUNCTION;
371 if (!(in_specification_block && m == MATCH_ERROR))
373 match (NULL, gfc_match_ptr_fcn_assign, ST_ASSIGNMENT);
376 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
377 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
379 /* Try to match a subroutine statement, which has the same optional
380 prefixes that functions can have. */
382 if (gfc_match_subroutine () == MATCH_YES)
383 return ST_SUBROUTINE;
384 gfc_undo_symbols ();
385 gfc_current_locus = old_locus;
387 if (gfc_match_submod_proc () == MATCH_YES)
389 if (gfc_new_block->attr.subroutine)
390 return ST_SUBROUTINE;
391 else if (gfc_new_block->attr.function)
392 return ST_FUNCTION;
394 gfc_undo_symbols ();
395 gfc_current_locus = old_locus;
397 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
398 statements, which might begin with a block label. The match functions for
399 these statements are unusual in that their keyword is not seen before
400 the matcher is called. */
402 if (gfc_match_if (&st) == MATCH_YES)
403 return st;
404 gfc_undo_symbols ();
405 gfc_current_locus = old_locus;
407 if (gfc_match_where (&st) == MATCH_YES)
408 return st;
409 gfc_undo_symbols ();
410 gfc_current_locus = old_locus;
412 if (gfc_match_forall (&st) == MATCH_YES)
413 return st;
414 gfc_undo_symbols ();
415 gfc_current_locus = old_locus;
417 /* Try to match TYPE as an alias for PRINT. */
418 if (gfc_match_type (&st) == MATCH_YES)
419 return st;
420 gfc_undo_symbols ();
421 gfc_current_locus = old_locus;
423 match (NULL, gfc_match_do, ST_DO);
424 match (NULL, gfc_match_block, ST_BLOCK);
425 match (NULL, gfc_match_associate, ST_ASSOCIATE);
426 match (NULL, gfc_match_critical, ST_CRITICAL);
427 match (NULL, gfc_match_select, ST_SELECT_CASE);
428 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
430 /* General statement matching: Instead of testing every possible
431 statement, we eliminate most possibilities by peeking at the
432 first character. */
434 switch (c)
436 case 'a':
437 match ("abstract% interface", gfc_match_abstract_interface,
438 ST_INTERFACE);
439 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
440 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
441 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
442 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
443 match ("automatic", gfc_match_automatic, ST_ATTR_DECL);
444 break;
446 case 'b':
447 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
448 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
449 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
450 break;
452 case 'c':
453 match ("call", gfc_match_call, ST_CALL);
454 match ("close", gfc_match_close, ST_CLOSE);
455 match ("continue", gfc_match_continue, ST_CONTINUE);
456 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
457 match ("cycle", gfc_match_cycle, ST_CYCLE);
458 match ("case", gfc_match_case, ST_CASE);
459 match ("common", gfc_match_common, ST_COMMON);
460 match ("contains", gfc_match_eos, ST_CONTAINS);
461 match ("class", gfc_match_class_is, ST_CLASS_IS);
462 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
463 break;
465 case 'd':
466 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
467 match ("data", gfc_match_data, ST_DATA);
468 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
469 break;
471 case 'e':
472 match ("end file", gfc_match_endfile, ST_END_FILE);
473 match ("exit", gfc_match_exit, ST_EXIT);
474 match ("else", gfc_match_else, ST_ELSE);
475 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
476 match ("else if", gfc_match_elseif, ST_ELSEIF);
477 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
478 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
480 if (gfc_match_end (&st) == MATCH_YES)
481 return st;
483 match ("entry% ", gfc_match_entry, ST_ENTRY);
484 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
485 match ("external", gfc_match_external, ST_ATTR_DECL);
486 match ("event post", gfc_match_event_post, ST_EVENT_POST);
487 match ("event wait", gfc_match_event_wait, ST_EVENT_WAIT);
488 break;
490 case 'f':
491 match ("final", gfc_match_final_decl, ST_FINAL);
492 match ("flush", gfc_match_flush, ST_FLUSH);
493 match ("format", gfc_match_format, ST_FORMAT);
494 break;
496 case 'g':
497 match ("generic", gfc_match_generic, ST_GENERIC);
498 match ("go to", gfc_match_goto, ST_GOTO);
499 break;
501 case 'i':
502 match ("inquire", gfc_match_inquire, ST_INQUIRE);
503 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
504 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
505 match ("import", gfc_match_import, ST_IMPORT);
506 match ("interface", gfc_match_interface, ST_INTERFACE);
507 match ("intent", gfc_match_intent, ST_ATTR_DECL);
508 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
509 break;
511 case 'l':
512 match ("lock", gfc_match_lock, ST_LOCK);
513 break;
515 case 'm':
516 match ("map", gfc_match_map, ST_MAP);
517 match ("module% procedure", gfc_match_modproc, ST_MODULE_PROC);
518 match ("module", gfc_match_module, ST_MODULE);
519 break;
521 case 'n':
522 match ("nullify", gfc_match_nullify, ST_NULLIFY);
523 match ("namelist", gfc_match_namelist, ST_NAMELIST);
524 break;
526 case 'o':
527 match ("open", gfc_match_open, ST_OPEN);
528 match ("optional", gfc_match_optional, ST_ATTR_DECL);
529 break;
531 case 'p':
532 match ("print", gfc_match_print, ST_WRITE);
533 match ("pause", gfc_match_pause, ST_PAUSE);
534 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
535 if (gfc_match_private (&st) == MATCH_YES)
536 return st;
537 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
538 match ("program", gfc_match_program, ST_PROGRAM);
539 if (gfc_match_public (&st) == MATCH_YES)
540 return st;
541 match ("protected", gfc_match_protected, ST_ATTR_DECL);
542 break;
544 case 'r':
545 match ("read", gfc_match_read, ST_READ);
546 match ("return", gfc_match_return, ST_RETURN);
547 match ("rewind", gfc_match_rewind, ST_REWIND);
548 break;
550 case 's':
551 match ("structure", gfc_match_structure_decl, ST_STRUCTURE_DECL);
552 match ("sequence", gfc_match_eos, ST_SEQUENCE);
553 match ("stop", gfc_match_stop, ST_STOP);
554 match ("save", gfc_match_save, ST_ATTR_DECL);
555 match ("static", gfc_match_static, ST_ATTR_DECL);
556 match ("submodule", gfc_match_submodule, ST_SUBMODULE);
557 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
558 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
559 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
560 break;
562 case 't':
563 match ("target", gfc_match_target, ST_ATTR_DECL);
564 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
565 match ("type is", gfc_match_type_is, ST_TYPE_IS);
566 break;
568 case 'u':
569 match ("union", gfc_match_union, ST_UNION);
570 match ("unlock", gfc_match_unlock, ST_UNLOCK);
571 break;
573 case 'v':
574 match ("value", gfc_match_value, ST_ATTR_DECL);
575 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
576 break;
578 case 'w':
579 match ("wait", gfc_match_wait, ST_WAIT);
580 match ("write", gfc_match_write, ST_WRITE);
581 break;
584 /* All else has failed, so give up. See if any of the matchers has
585 stored an error message of some sort. */
587 if (!gfc_error_check ())
588 gfc_error_now ("Unclassifiable statement at %C");
590 reject_statement ();
592 gfc_error_recovery ();
594 return ST_NONE;
597 /* Like match and if spec_only, goto do_spec_only without actually
598 matching. */
599 #define matcha(keyword, subr, st) \
600 do { \
601 if (spec_only && gfc_match (keyword) == MATCH_YES) \
602 goto do_spec_only; \
603 else if (match_word (keyword, subr, &old_locus) \
604 == MATCH_YES) \
605 return st; \
606 else \
607 undo_new_statement (); \
608 } while (0);
610 static gfc_statement
611 decode_oacc_directive (void)
613 locus old_locus;
614 char c;
615 bool spec_only = false;
617 gfc_enforce_clean_symbol_state ();
619 gfc_clear_error (); /* Clear any pending errors. */
620 gfc_clear_warning (); /* Clear any pending warnings. */
622 if (gfc_pure (NULL))
624 gfc_error_now ("OpenACC directives at %C may not appear in PURE "
625 "procedures");
626 gfc_error_recovery ();
627 return ST_NONE;
630 if (gfc_current_state () == COMP_FUNCTION
631 && gfc_current_block ()->result->ts.kind == -1)
632 spec_only = true;
634 gfc_unset_implicit_pure (NULL);
636 old_locus = gfc_current_locus;
638 /* General OpenACC directive matching: Instead of testing every possible
639 statement, we eliminate most possibilities by peeking at the
640 first character. */
642 c = gfc_peek_ascii_char ();
644 switch (c)
646 case 'a':
647 matcha ("atomic", gfc_match_oacc_atomic, ST_OACC_ATOMIC);
648 break;
649 case 'c':
650 matcha ("cache", gfc_match_oacc_cache, ST_OACC_CACHE);
651 break;
652 case 'd':
653 matcha ("data", gfc_match_oacc_data, ST_OACC_DATA);
654 match ("declare", gfc_match_oacc_declare, ST_OACC_DECLARE);
655 break;
656 case 'e':
657 matcha ("end atomic", gfc_match_omp_eos, ST_OACC_END_ATOMIC);
658 matcha ("end data", gfc_match_omp_eos, ST_OACC_END_DATA);
659 matcha ("end host_data", gfc_match_omp_eos, ST_OACC_END_HOST_DATA);
660 matcha ("end kernels loop", gfc_match_omp_eos, ST_OACC_END_KERNELS_LOOP);
661 matcha ("end kernels", gfc_match_omp_eos, ST_OACC_END_KERNELS);
662 matcha ("end loop", gfc_match_omp_eos, ST_OACC_END_LOOP);
663 matcha ("end parallel loop", gfc_match_omp_eos,
664 ST_OACC_END_PARALLEL_LOOP);
665 matcha ("end parallel", gfc_match_omp_eos, ST_OACC_END_PARALLEL);
666 matcha ("enter data", gfc_match_oacc_enter_data, ST_OACC_ENTER_DATA);
667 matcha ("exit data", gfc_match_oacc_exit_data, ST_OACC_EXIT_DATA);
668 break;
669 case 'h':
670 matcha ("host_data", gfc_match_oacc_host_data, ST_OACC_HOST_DATA);
671 break;
672 case 'p':
673 matcha ("parallel loop", gfc_match_oacc_parallel_loop,
674 ST_OACC_PARALLEL_LOOP);
675 matcha ("parallel", gfc_match_oacc_parallel, ST_OACC_PARALLEL);
676 break;
677 case 'k':
678 matcha ("kernels loop", gfc_match_oacc_kernels_loop,
679 ST_OACC_KERNELS_LOOP);
680 matcha ("kernels", gfc_match_oacc_kernels, ST_OACC_KERNELS);
681 break;
682 case 'l':
683 matcha ("loop", gfc_match_oacc_loop, ST_OACC_LOOP);
684 break;
685 case 'r':
686 match ("routine", gfc_match_oacc_routine, ST_OACC_ROUTINE);
687 break;
688 case 'u':
689 matcha ("update", gfc_match_oacc_update, ST_OACC_UPDATE);
690 break;
691 case 'w':
692 matcha ("wait", gfc_match_oacc_wait, ST_OACC_WAIT);
693 break;
696 /* Directive not found or stored an error message.
697 Check and give up. */
699 if (gfc_error_check () == 0)
700 gfc_error_now ("Unclassifiable OpenACC directive at %C");
702 reject_statement ();
704 gfc_error_recovery ();
706 return ST_NONE;
708 do_spec_only:
709 reject_statement ();
710 gfc_clear_error ();
711 gfc_buffer_error (false);
712 gfc_current_locus = old_locus;
713 return ST_GET_FCN_CHARACTERISTICS;
716 /* Like match, but set a flag simd_matched if keyword matched
717 and if spec_only, goto do_spec_only without actually matching. */
718 #define matchs(keyword, subr, st) \
719 do { \
720 if (spec_only && gfc_match (keyword) == MATCH_YES) \
721 goto do_spec_only; \
722 if (match_word_omp_simd (keyword, subr, &old_locus, \
723 &simd_matched) == MATCH_YES) \
725 ret = st; \
726 goto finish; \
728 else \
729 undo_new_statement (); \
730 } while (0);
732 /* Like match, but don't match anything if not -fopenmp
733 and if spec_only, goto do_spec_only without actually matching. */
734 #define matcho(keyword, subr, st) \
735 do { \
736 if (!flag_openmp) \
738 else if (spec_only && gfc_match (keyword) == MATCH_YES) \
739 goto do_spec_only; \
740 else if (match_word (keyword, subr, &old_locus) \
741 == MATCH_YES) \
743 ret = st; \
744 goto finish; \
746 else \
747 undo_new_statement (); \
748 } while (0);
750 /* Like match, but set a flag simd_matched if keyword matched. */
751 #define matchds(keyword, subr, st) \
752 do { \
753 if (match_word_omp_simd (keyword, subr, &old_locus, \
754 &simd_matched) == MATCH_YES) \
756 ret = st; \
757 goto finish; \
759 else \
760 undo_new_statement (); \
761 } while (0);
763 /* Like match, but don't match anything if not -fopenmp. */
764 #define matchdo(keyword, subr, st) \
765 do { \
766 if (!flag_openmp) \
768 else if (match_word (keyword, subr, &old_locus) \
769 == MATCH_YES) \
771 ret = st; \
772 goto finish; \
774 else \
775 undo_new_statement (); \
776 } while (0);
778 static gfc_statement
779 decode_omp_directive (void)
781 locus old_locus;
782 char c;
783 bool simd_matched = false;
784 bool spec_only = false;
785 gfc_statement ret = ST_NONE;
786 bool pure_ok = true;
788 gfc_enforce_clean_symbol_state ();
790 gfc_clear_error (); /* Clear any pending errors. */
791 gfc_clear_warning (); /* Clear any pending warnings. */
793 if (gfc_current_state () == COMP_FUNCTION
794 && gfc_current_block ()->result->ts.kind == -1)
795 spec_only = true;
797 old_locus = gfc_current_locus;
799 /* General OpenMP directive matching: Instead of testing every possible
800 statement, we eliminate most possibilities by peeking at the
801 first character. */
803 c = gfc_peek_ascii_char ();
805 /* match is for directives that should be recognized only if
806 -fopenmp, matchs for directives that should be recognized
807 if either -fopenmp or -fopenmp-simd.
808 Handle only the directives allowed in PURE/ELEMENTAL procedures
809 first (those also shall not turn off implicit pure). */
810 switch (c)
812 case 'd':
813 matchds ("declare simd", gfc_match_omp_declare_simd,
814 ST_OMP_DECLARE_SIMD);
815 matchdo ("declare target", gfc_match_omp_declare_target,
816 ST_OMP_DECLARE_TARGET);
817 break;
818 case 's':
819 matchs ("simd", gfc_match_omp_simd, ST_OMP_SIMD);
820 break;
823 pure_ok = false;
824 if (flag_openmp && gfc_pure (NULL))
826 gfc_error_now ("OpenMP directives other than SIMD or DECLARE TARGET "
827 "at %C may not appear in PURE or ELEMENTAL procedures");
828 gfc_error_recovery ();
829 return ST_NONE;
832 /* match is for directives that should be recognized only if
833 -fopenmp, matchs for directives that should be recognized
834 if either -fopenmp or -fopenmp-simd. */
835 switch (c)
837 case 'a':
838 matcho ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
839 break;
840 case 'b':
841 matcho ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
842 break;
843 case 'c':
844 matcho ("cancellation% point", gfc_match_omp_cancellation_point,
845 ST_OMP_CANCELLATION_POINT);
846 matcho ("cancel", gfc_match_omp_cancel, ST_OMP_CANCEL);
847 matcho ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
848 break;
849 case 'd':
850 matchds ("declare reduction", gfc_match_omp_declare_reduction,
851 ST_OMP_DECLARE_REDUCTION);
852 matchs ("distribute parallel do simd",
853 gfc_match_omp_distribute_parallel_do_simd,
854 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD);
855 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do,
856 ST_OMP_DISTRIBUTE_PARALLEL_DO);
857 matchs ("distribute simd", gfc_match_omp_distribute_simd,
858 ST_OMP_DISTRIBUTE_SIMD);
859 matcho ("distribute", gfc_match_omp_distribute, ST_OMP_DISTRIBUTE);
860 matchs ("do simd", gfc_match_omp_do_simd, ST_OMP_DO_SIMD);
861 matcho ("do", gfc_match_omp_do, ST_OMP_DO);
862 break;
863 case 'e':
864 matcho ("end atomic", gfc_match_omp_eos, ST_OMP_END_ATOMIC);
865 matcho ("end critical", gfc_match_omp_end_critical, ST_OMP_END_CRITICAL);
866 matchs ("end distribute parallel do simd", gfc_match_omp_eos,
867 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD);
868 matcho ("end distribute parallel do", gfc_match_omp_eos,
869 ST_OMP_END_DISTRIBUTE_PARALLEL_DO);
870 matchs ("end distribute simd", gfc_match_omp_eos,
871 ST_OMP_END_DISTRIBUTE_SIMD);
872 matcho ("end distribute", gfc_match_omp_eos, ST_OMP_END_DISTRIBUTE);
873 matchs ("end do simd", gfc_match_omp_end_nowait, ST_OMP_END_DO_SIMD);
874 matcho ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
875 matchs ("end simd", gfc_match_omp_eos, ST_OMP_END_SIMD);
876 matcho ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
877 matcho ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
878 matchs ("end parallel do simd", gfc_match_omp_eos,
879 ST_OMP_END_PARALLEL_DO_SIMD);
880 matcho ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
881 matcho ("end parallel sections", gfc_match_omp_eos,
882 ST_OMP_END_PARALLEL_SECTIONS);
883 matcho ("end parallel workshare", gfc_match_omp_eos,
884 ST_OMP_END_PARALLEL_WORKSHARE);
885 matcho ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
886 matcho ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
887 matcho ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
888 matcho ("end target data", gfc_match_omp_eos, ST_OMP_END_TARGET_DATA);
889 matchs ("end target parallel do simd", gfc_match_omp_eos,
890 ST_OMP_END_TARGET_PARALLEL_DO_SIMD);
891 matcho ("end target parallel do", gfc_match_omp_eos,
892 ST_OMP_END_TARGET_PARALLEL_DO);
893 matcho ("end target parallel", gfc_match_omp_eos,
894 ST_OMP_END_TARGET_PARALLEL);
895 matchs ("end target simd", gfc_match_omp_eos, ST_OMP_END_TARGET_SIMD);
896 matchs ("end target teams distribute parallel do simd",
897 gfc_match_omp_eos,
898 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
899 matcho ("end target teams distribute parallel do", gfc_match_omp_eos,
900 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
901 matchs ("end target teams distribute simd", gfc_match_omp_eos,
902 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD);
903 matcho ("end target teams distribute", gfc_match_omp_eos,
904 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE);
905 matcho ("end target teams", gfc_match_omp_eos, ST_OMP_END_TARGET_TEAMS);
906 matcho ("end target", gfc_match_omp_eos, ST_OMP_END_TARGET);
907 matcho ("end taskgroup", gfc_match_omp_eos, ST_OMP_END_TASKGROUP);
908 matchs ("end taskloop simd", gfc_match_omp_eos,
909 ST_OMP_END_TASKLOOP_SIMD);
910 matcho ("end taskloop", gfc_match_omp_eos, ST_OMP_END_TASKLOOP);
911 matcho ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
912 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos,
913 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
914 matcho ("end teams distribute parallel do", gfc_match_omp_eos,
915 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO);
916 matchs ("end teams distribute simd", gfc_match_omp_eos,
917 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD);
918 matcho ("end teams distribute", gfc_match_omp_eos,
919 ST_OMP_END_TEAMS_DISTRIBUTE);
920 matcho ("end teams", gfc_match_omp_eos, ST_OMP_END_TEAMS);
921 matcho ("end workshare", gfc_match_omp_end_nowait,
922 ST_OMP_END_WORKSHARE);
923 break;
924 case 'f':
925 matcho ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
926 break;
927 case 'm':
928 matcho ("master", gfc_match_omp_master, ST_OMP_MASTER);
929 break;
930 case 'o':
931 if (flag_openmp && gfc_match ("ordered depend (") == MATCH_YES)
933 gfc_current_locus = old_locus;
934 matcho ("ordered", gfc_match_omp_ordered_depend,
935 ST_OMP_ORDERED_DEPEND);
937 else
938 matcho ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
939 break;
940 case 'p':
941 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
942 ST_OMP_PARALLEL_DO_SIMD);
943 matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
944 matcho ("parallel sections", gfc_match_omp_parallel_sections,
945 ST_OMP_PARALLEL_SECTIONS);
946 matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
947 ST_OMP_PARALLEL_WORKSHARE);
948 matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
949 break;
950 case 's':
951 matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
952 matcho ("section", gfc_match_omp_eos, ST_OMP_SECTION);
953 matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
954 break;
955 case 't':
956 matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
957 matcho ("target enter data", gfc_match_omp_target_enter_data,
958 ST_OMP_TARGET_ENTER_DATA);
959 matcho ("target exit data", gfc_match_omp_target_exit_data,
960 ST_OMP_TARGET_EXIT_DATA);
961 matchs ("target parallel do simd", gfc_match_omp_target_parallel_do_simd,
962 ST_OMP_TARGET_PARALLEL_DO_SIMD);
963 matcho ("target parallel do", gfc_match_omp_target_parallel_do,
964 ST_OMP_TARGET_PARALLEL_DO);
965 matcho ("target parallel", gfc_match_omp_target_parallel,
966 ST_OMP_TARGET_PARALLEL);
967 matchs ("target simd", gfc_match_omp_target_simd, ST_OMP_TARGET_SIMD);
968 matchs ("target teams distribute parallel do simd",
969 gfc_match_omp_target_teams_distribute_parallel_do_simd,
970 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
971 matcho ("target teams distribute parallel do",
972 gfc_match_omp_target_teams_distribute_parallel_do,
973 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
974 matchs ("target teams distribute simd",
975 gfc_match_omp_target_teams_distribute_simd,
976 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
977 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
978 ST_OMP_TARGET_TEAMS_DISTRIBUTE);
979 matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
980 matcho ("target update", gfc_match_omp_target_update,
981 ST_OMP_TARGET_UPDATE);
982 matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
983 matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
984 matchs ("taskloop simd", gfc_match_omp_taskloop_simd,
985 ST_OMP_TASKLOOP_SIMD);
986 matcho ("taskloop", gfc_match_omp_taskloop, ST_OMP_TASKLOOP);
987 matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
988 matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
989 matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
990 matchs ("teams distribute parallel do simd",
991 gfc_match_omp_teams_distribute_parallel_do_simd,
992 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
993 matcho ("teams distribute parallel do",
994 gfc_match_omp_teams_distribute_parallel_do,
995 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
996 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
997 ST_OMP_TEAMS_DISTRIBUTE_SIMD);
998 matcho ("teams distribute", gfc_match_omp_teams_distribute,
999 ST_OMP_TEAMS_DISTRIBUTE);
1000 matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
1001 matchdo ("threadprivate", gfc_match_omp_threadprivate,
1002 ST_OMP_THREADPRIVATE);
1003 break;
1004 case 'w':
1005 matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
1006 break;
1009 /* All else has failed, so give up. See if any of the matchers has
1010 stored an error message of some sort. Don't error out if
1011 not -fopenmp and simd_matched is false, i.e. if a directive other
1012 than one marked with match has been seen. */
1014 if (flag_openmp || simd_matched)
1016 if (!gfc_error_check ())
1017 gfc_error_now ("Unclassifiable OpenMP directive at %C");
1020 reject_statement ();
1022 gfc_error_recovery ();
1024 return ST_NONE;
1026 finish:
1027 if (!pure_ok)
1029 gfc_unset_implicit_pure (NULL);
1031 if (!flag_openmp && gfc_pure (NULL))
1033 gfc_error_now ("OpenMP directives other than SIMD or DECLARE TARGET "
1034 "at %C may not appear in PURE or ELEMENTAL "
1035 "procedures");
1036 reject_statement ();
1037 gfc_error_recovery ();
1038 return ST_NONE;
1041 return ret;
1043 do_spec_only:
1044 reject_statement ();
1045 gfc_clear_error ();
1046 gfc_buffer_error (false);
1047 gfc_current_locus = old_locus;
1048 return ST_GET_FCN_CHARACTERISTICS;
1051 static gfc_statement
1052 decode_gcc_attribute (void)
1054 locus old_locus;
1056 gfc_enforce_clean_symbol_state ();
1058 gfc_clear_error (); /* Clear any pending errors. */
1059 gfc_clear_warning (); /* Clear any pending warnings. */
1060 old_locus = gfc_current_locus;
1062 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
1064 /* All else has failed, so give up. See if any of the matchers has
1065 stored an error message of some sort. */
1067 if (!gfc_error_check ())
1068 gfc_error_now ("Unclassifiable GCC directive at %C");
1070 reject_statement ();
1072 gfc_error_recovery ();
1074 return ST_NONE;
1077 #undef match
1079 /* Assert next length characters to be equal to token in free form. */
1081 static void
1082 verify_token_free (const char* token, int length, bool last_was_use_stmt)
1084 int i;
1085 char c;
1087 c = gfc_next_ascii_char ();
1088 for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
1089 gcc_assert (c == token[i]);
1091 gcc_assert (gfc_is_whitespace(c));
1092 gfc_gobble_whitespace ();
1093 if (last_was_use_stmt)
1094 use_modules ();
1097 /* Get the next statement in free form source. */
1099 static gfc_statement
1100 next_free (void)
1102 match m;
1103 int i, cnt, at_bol;
1104 char c;
1106 at_bol = gfc_at_bol ();
1107 gfc_gobble_whitespace ();
1109 c = gfc_peek_ascii_char ();
1111 if (ISDIGIT (c))
1113 char d;
1115 /* Found a statement label? */
1116 m = gfc_match_st_label (&gfc_statement_label);
1118 d = gfc_peek_ascii_char ();
1119 if (m != MATCH_YES || !gfc_is_whitespace (d))
1121 gfc_match_small_literal_int (&i, &cnt);
1123 if (cnt > 5)
1124 gfc_error_now ("Too many digits in statement label at %C");
1126 if (i == 0)
1127 gfc_error_now ("Zero is not a valid statement label at %C");
1130 c = gfc_next_ascii_char ();
1131 while (ISDIGIT(c));
1133 if (!gfc_is_whitespace (c))
1134 gfc_error_now ("Non-numeric character in statement label at %C");
1136 return ST_NONE;
1138 else
1140 label_locus = gfc_current_locus;
1142 gfc_gobble_whitespace ();
1144 if (at_bol && gfc_peek_ascii_char () == ';')
1146 gfc_error_now ("Semicolon at %C needs to be preceded by "
1147 "statement");
1148 gfc_next_ascii_char (); /* Eat up the semicolon. */
1149 return ST_NONE;
1152 if (gfc_match_eos () == MATCH_YES)
1153 gfc_error_now ("Statement label without statement at %L",
1154 &label_locus);
1157 else if (c == '!')
1159 /* Comments have already been skipped by the time we get here,
1160 except for GCC attributes and OpenMP/OpenACC directives. */
1162 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
1163 c = gfc_peek_ascii_char ();
1165 if (c == 'g')
1167 int i;
1169 c = gfc_next_ascii_char ();
1170 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
1171 gcc_assert (c == "gcc$"[i]);
1173 gfc_gobble_whitespace ();
1174 return decode_gcc_attribute ();
1177 else if (c == '$')
1179 /* Since both OpenMP and OpenACC directives starts with
1180 !$ character sequence, we must check all flags combinations */
1181 if ((flag_openmp || flag_openmp_simd)
1182 && !flag_openacc)
1184 verify_token_free ("$omp", 4, last_was_use_stmt);
1185 return decode_omp_directive ();
1187 else if ((flag_openmp || flag_openmp_simd)
1188 && flag_openacc)
1190 gfc_next_ascii_char (); /* Eat up dollar character */
1191 c = gfc_peek_ascii_char ();
1193 if (c == 'o')
1195 verify_token_free ("omp", 3, last_was_use_stmt);
1196 return decode_omp_directive ();
1198 else if (c == 'a')
1200 verify_token_free ("acc", 3, last_was_use_stmt);
1201 return decode_oacc_directive ();
1204 else if (flag_openacc)
1206 verify_token_free ("$acc", 4, last_was_use_stmt);
1207 return decode_oacc_directive ();
1210 gcc_unreachable ();
1213 if (at_bol && c == ';')
1215 if (!(gfc_option.allow_std & GFC_STD_F2008))
1216 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1217 "statement");
1218 gfc_next_ascii_char (); /* Eat up the semicolon. */
1219 return ST_NONE;
1222 return decode_statement ();
1225 /* Assert next length characters to be equal to token in fixed form. */
1227 static bool
1228 verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
1230 int i;
1231 char c = gfc_next_char_literal (NONSTRING);
1233 for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
1234 gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
1236 if (c != ' ' && c != '0')
1238 gfc_buffer_error (false);
1239 gfc_error ("Bad continuation line at %C");
1240 return false;
1242 if (last_was_use_stmt)
1243 use_modules ();
1245 return true;
1248 /* Get the next statement in fixed-form source. */
1250 static gfc_statement
1251 next_fixed (void)
1253 int label, digit_flag, i;
1254 locus loc;
1255 gfc_char_t c;
1257 if (!gfc_at_bol ())
1258 return decode_statement ();
1260 /* Skip past the current label field, parsing a statement label if
1261 one is there. This is a weird number parser, since the number is
1262 contained within five columns and can have any kind of embedded
1263 spaces. We also check for characters that make the rest of the
1264 line a comment. */
1266 label = 0;
1267 digit_flag = 0;
1269 for (i = 0; i < 5; i++)
1271 c = gfc_next_char_literal (NONSTRING);
1273 switch (c)
1275 case ' ':
1276 break;
1278 case '0':
1279 case '1':
1280 case '2':
1281 case '3':
1282 case '4':
1283 case '5':
1284 case '6':
1285 case '7':
1286 case '8':
1287 case '9':
1288 label = label * 10 + ((unsigned char) c - '0');
1289 label_locus = gfc_current_locus;
1290 digit_flag = 1;
1291 break;
1293 /* Comments have already been skipped by the time we get
1294 here, except for GCC attributes and OpenMP directives. */
1296 case '*':
1297 c = gfc_next_char_literal (NONSTRING);
1299 if (TOLOWER (c) == 'g')
1301 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
1302 gcc_assert (TOLOWER (c) == "gcc$"[i]);
1304 return decode_gcc_attribute ();
1306 else if (c == '$')
1308 if ((flag_openmp || flag_openmp_simd)
1309 && !flag_openacc)
1311 if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
1312 return ST_NONE;
1313 return decode_omp_directive ();
1315 else if ((flag_openmp || flag_openmp_simd)
1316 && flag_openacc)
1318 c = gfc_next_char_literal(NONSTRING);
1319 if (c == 'o' || c == 'O')
1321 if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
1322 return ST_NONE;
1323 return decode_omp_directive ();
1325 else if (c == 'a' || c == 'A')
1327 if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
1328 return ST_NONE;
1329 return decode_oacc_directive ();
1332 else if (flag_openacc)
1334 if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
1335 return ST_NONE;
1336 return decode_oacc_directive ();
1339 gcc_fallthrough ();
1341 /* Comments have already been skipped by the time we get
1342 here so don't bother checking for them. */
1344 default:
1345 gfc_buffer_error (false);
1346 gfc_error ("Non-numeric character in statement label at %C");
1347 return ST_NONE;
1351 if (digit_flag)
1353 if (label == 0)
1354 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1355 else
1357 /* We've found a valid statement label. */
1358 gfc_statement_label = gfc_get_st_label (label);
1362 /* Since this line starts a statement, it cannot be a continuation
1363 of a previous statement. If we see something here besides a
1364 space or zero, it must be a bad continuation line. */
1366 c = gfc_next_char_literal (NONSTRING);
1367 if (c == '\n')
1368 goto blank_line;
1370 if (c != ' ' && c != '0')
1372 gfc_buffer_error (false);
1373 gfc_error ("Bad continuation line at %C");
1374 return ST_NONE;
1377 /* Now that we've taken care of the statement label columns, we have
1378 to make sure that the first nonblank character is not a '!'. If
1379 it is, the rest of the line is a comment. */
1383 loc = gfc_current_locus;
1384 c = gfc_next_char_literal (NONSTRING);
1386 while (gfc_is_whitespace (c));
1388 if (c == '!')
1389 goto blank_line;
1390 gfc_current_locus = loc;
1392 if (c == ';')
1394 if (digit_flag)
1395 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1396 else if (!(gfc_option.allow_std & GFC_STD_F2008))
1397 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1398 "statement");
1399 return ST_NONE;
1402 if (gfc_match_eos () == MATCH_YES)
1403 goto blank_line;
1405 /* At this point, we've got a nonblank statement to parse. */
1406 return decode_statement ();
1408 blank_line:
1409 if (digit_flag)
1410 gfc_error_now ("Statement label without statement at %L", &label_locus);
1412 gfc_current_locus.lb->truncated = 0;
1413 gfc_advance_line ();
1414 return ST_NONE;
1418 /* Return the next non-ST_NONE statement to the caller. We also worry
1419 about including files and the ends of include files at this stage. */
1421 static gfc_statement
1422 next_statement (void)
1424 gfc_statement st;
1425 locus old_locus;
1427 gfc_enforce_clean_symbol_state ();
1429 gfc_new_block = NULL;
1431 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
1432 gfc_current_ns->old_data = gfc_current_ns->data;
1433 for (;;)
1435 gfc_statement_label = NULL;
1436 gfc_buffer_error (true);
1438 if (gfc_at_eol ())
1439 gfc_advance_line ();
1441 gfc_skip_comments ();
1443 if (gfc_at_end ())
1445 st = ST_NONE;
1446 break;
1449 if (gfc_define_undef_line ())
1450 continue;
1452 old_locus = gfc_current_locus;
1454 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
1456 if (st != ST_NONE)
1457 break;
1460 gfc_buffer_error (false);
1462 if (st == ST_GET_FCN_CHARACTERISTICS)
1464 if (gfc_statement_label != NULL)
1466 gfc_free_st_label (gfc_statement_label);
1467 gfc_statement_label = NULL;
1469 gfc_current_locus = old_locus;
1472 if (st != ST_NONE)
1473 check_statement_label (st);
1475 return st;
1479 /****************************** Parser ***********************************/
1481 /* The parser subroutines are of type 'try' that fail if the file ends
1482 unexpectedly. */
1484 /* Macros that expand to case-labels for various classes of
1485 statements. Start with executable statements that directly do
1486 things. */
1488 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1489 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1490 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1491 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1492 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1493 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1494 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1495 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1496 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1497 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1498 case ST_OMP_TARGET_UPDATE: case ST_OMP_TARGET_ENTER_DATA: \
1499 case ST_OMP_TARGET_EXIT_DATA: case ST_OMP_ORDERED_DEPEND: \
1500 case ST_ERROR_STOP: case ST_SYNC_ALL: \
1501 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1502 case ST_EVENT_POST: case ST_EVENT_WAIT: \
1503 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1504 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1506 /* Statements that mark other executable statements. */
1508 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1509 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1510 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1511 case ST_OMP_PARALLEL: \
1512 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1513 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1514 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1515 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1516 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1517 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1518 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1519 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1520 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1521 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1522 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1523 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1524 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1525 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1526 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1527 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1528 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_TARGET_PARALLEL: \
1529 case ST_OMP_TARGET_PARALLEL_DO: case ST_OMP_TARGET_PARALLEL_DO_SIMD: \
1530 case ST_OMP_TARGET_SIMD: case ST_OMP_TASKLOOP: case ST_OMP_TASKLOOP_SIMD: \
1531 case ST_CRITICAL: \
1532 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1533 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: \
1534 case ST_OACC_KERNELS_LOOP: case ST_OACC_ATOMIC
1536 /* Declaration statements */
1538 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1539 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1540 case ST_TYPE: case ST_INTERFACE: case ST_PROCEDURE: case ST_OACC_ROUTINE: \
1541 case ST_OACC_DECLARE
1543 /* OpenMP declaration statements. */
1545 #define case_omp_decl case ST_OMP_THREADPRIVATE: case ST_OMP_DECLARE_SIMD: \
1546 case ST_OMP_DECLARE_TARGET: case ST_OMP_DECLARE_REDUCTION
1548 /* Block end statements. Errors associated with interchanging these
1549 are detected in gfc_match_end(). */
1551 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1552 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1553 case ST_END_BLOCK: case ST_END_ASSOCIATE
1556 /* Push a new state onto the stack. */
1558 static void
1559 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
1561 p->state = new_state;
1562 p->previous = gfc_state_stack;
1563 p->sym = sym;
1564 p->head = p->tail = NULL;
1565 p->do_variable = NULL;
1566 if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
1567 p->ext.oacc_declare_clauses = NULL;
1569 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1570 construct statement was accepted right before pushing the state. Thus,
1571 the construct's gfc_code is available as tail of the parent state. */
1572 gcc_assert (gfc_state_stack);
1573 p->construct = gfc_state_stack->tail;
1575 gfc_state_stack = p;
1579 /* Pop the current state. */
1580 static void
1581 pop_state (void)
1583 gfc_state_stack = gfc_state_stack->previous;
1587 /* Try to find the given state in the state stack. */
1589 bool
1590 gfc_find_state (gfc_compile_state state)
1592 gfc_state_data *p;
1594 for (p = gfc_state_stack; p; p = p->previous)
1595 if (p->state == state)
1596 break;
1598 return (p == NULL) ? false : true;
1602 /* Starts a new level in the statement list. */
1604 static gfc_code *
1605 new_level (gfc_code *q)
1607 gfc_code *p;
1609 p = q->block = gfc_get_code (EXEC_NOP);
1611 gfc_state_stack->head = gfc_state_stack->tail = p;
1613 return p;
1617 /* Add the current new_st code structure and adds it to the current
1618 program unit. As a side-effect, it zeroes the new_st. */
1620 static gfc_code *
1621 add_statement (void)
1623 gfc_code *p;
1625 p = XCNEW (gfc_code);
1626 *p = new_st;
1628 p->loc = gfc_current_locus;
1630 if (gfc_state_stack->head == NULL)
1631 gfc_state_stack->head = p;
1632 else
1633 gfc_state_stack->tail->next = p;
1635 while (p->next != NULL)
1636 p = p->next;
1638 gfc_state_stack->tail = p;
1640 gfc_clear_new_st ();
1642 return p;
1646 /* Frees everything associated with the current statement. */
1648 static void
1649 undo_new_statement (void)
1651 gfc_free_statements (new_st.block);
1652 gfc_free_statements (new_st.next);
1653 gfc_free_statement (&new_st);
1654 gfc_clear_new_st ();
1658 /* If the current statement has a statement label, make sure that it
1659 is allowed to, or should have one. */
1661 static void
1662 check_statement_label (gfc_statement st)
1664 gfc_sl_type type;
1666 if (gfc_statement_label == NULL)
1668 if (st == ST_FORMAT)
1669 gfc_error ("FORMAT statement at %L does not have a statement label",
1670 &new_st.loc);
1671 return;
1674 switch (st)
1676 case ST_END_PROGRAM:
1677 case ST_END_FUNCTION:
1678 case ST_END_SUBROUTINE:
1679 case ST_ENDDO:
1680 case ST_ENDIF:
1681 case ST_END_SELECT:
1682 case ST_END_CRITICAL:
1683 case ST_END_BLOCK:
1684 case ST_END_ASSOCIATE:
1685 case_executable:
1686 case_exec_markers:
1687 if (st == ST_ENDDO || st == ST_CONTINUE)
1688 type = ST_LABEL_DO_TARGET;
1689 else
1690 type = ST_LABEL_TARGET;
1691 break;
1693 case ST_FORMAT:
1694 type = ST_LABEL_FORMAT;
1695 break;
1697 /* Statement labels are not restricted from appearing on a
1698 particular line. However, there are plenty of situations
1699 where the resulting label can't be referenced. */
1701 default:
1702 type = ST_LABEL_BAD_TARGET;
1703 break;
1706 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1708 new_st.here = gfc_statement_label;
1712 /* Figures out what the enclosing program unit is. This will be a
1713 function, subroutine, program, block data or module. */
1715 gfc_state_data *
1716 gfc_enclosing_unit (gfc_compile_state * result)
1718 gfc_state_data *p;
1720 for (p = gfc_state_stack; p; p = p->previous)
1721 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1722 || p->state == COMP_MODULE || p->state == COMP_SUBMODULE
1723 || p->state == COMP_BLOCK_DATA || p->state == COMP_PROGRAM)
1726 if (result != NULL)
1727 *result = p->state;
1728 return p;
1731 if (result != NULL)
1732 *result = COMP_PROGRAM;
1733 return NULL;
1737 /* Translate a statement enum to a string. */
1739 const char *
1740 gfc_ascii_statement (gfc_statement st)
1742 const char *p;
1744 switch (st)
1746 case ST_ARITHMETIC_IF:
1747 p = _("arithmetic IF");
1748 break;
1749 case ST_ALLOCATE:
1750 p = "ALLOCATE";
1751 break;
1752 case ST_ASSOCIATE:
1753 p = "ASSOCIATE";
1754 break;
1755 case ST_ATTR_DECL:
1756 p = _("attribute declaration");
1757 break;
1758 case ST_BACKSPACE:
1759 p = "BACKSPACE";
1760 break;
1761 case ST_BLOCK:
1762 p = "BLOCK";
1763 break;
1764 case ST_BLOCK_DATA:
1765 p = "BLOCK DATA";
1766 break;
1767 case ST_CALL:
1768 p = "CALL";
1769 break;
1770 case ST_CASE:
1771 p = "CASE";
1772 break;
1773 case ST_CLOSE:
1774 p = "CLOSE";
1775 break;
1776 case ST_COMMON:
1777 p = "COMMON";
1778 break;
1779 case ST_CONTINUE:
1780 p = "CONTINUE";
1781 break;
1782 case ST_CONTAINS:
1783 p = "CONTAINS";
1784 break;
1785 case ST_CRITICAL:
1786 p = "CRITICAL";
1787 break;
1788 case ST_CYCLE:
1789 p = "CYCLE";
1790 break;
1791 case ST_DATA_DECL:
1792 p = _("data declaration");
1793 break;
1794 case ST_DATA:
1795 p = "DATA";
1796 break;
1797 case ST_DEALLOCATE:
1798 p = "DEALLOCATE";
1799 break;
1800 case ST_MAP:
1801 p = "MAP";
1802 break;
1803 case ST_UNION:
1804 p = "UNION";
1805 break;
1806 case ST_STRUCTURE_DECL:
1807 p = "STRUCTURE";
1808 break;
1809 case ST_DERIVED_DECL:
1810 p = _("derived type declaration");
1811 break;
1812 case ST_DO:
1813 p = "DO";
1814 break;
1815 case ST_ELSE:
1816 p = "ELSE";
1817 break;
1818 case ST_ELSEIF:
1819 p = "ELSE IF";
1820 break;
1821 case ST_ELSEWHERE:
1822 p = "ELSEWHERE";
1823 break;
1824 case ST_EVENT_POST:
1825 p = "EVENT POST";
1826 break;
1827 case ST_EVENT_WAIT:
1828 p = "EVENT WAIT";
1829 break;
1830 case ST_END_ASSOCIATE:
1831 p = "END ASSOCIATE";
1832 break;
1833 case ST_END_BLOCK:
1834 p = "END BLOCK";
1835 break;
1836 case ST_END_BLOCK_DATA:
1837 p = "END BLOCK DATA";
1838 break;
1839 case ST_END_CRITICAL:
1840 p = "END CRITICAL";
1841 break;
1842 case ST_ENDDO:
1843 p = "END DO";
1844 break;
1845 case ST_END_FILE:
1846 p = "END FILE";
1847 break;
1848 case ST_END_FORALL:
1849 p = "END FORALL";
1850 break;
1851 case ST_END_FUNCTION:
1852 p = "END FUNCTION";
1853 break;
1854 case ST_ENDIF:
1855 p = "END IF";
1856 break;
1857 case ST_END_INTERFACE:
1858 p = "END INTERFACE";
1859 break;
1860 case ST_END_MODULE:
1861 p = "END MODULE";
1862 break;
1863 case ST_END_SUBMODULE:
1864 p = "END SUBMODULE";
1865 break;
1866 case ST_END_PROGRAM:
1867 p = "END PROGRAM";
1868 break;
1869 case ST_END_SELECT:
1870 p = "END SELECT";
1871 break;
1872 case ST_END_SUBROUTINE:
1873 p = "END SUBROUTINE";
1874 break;
1875 case ST_END_WHERE:
1876 p = "END WHERE";
1877 break;
1878 case ST_END_STRUCTURE:
1879 p = "END STRUCTURE";
1880 break;
1881 case ST_END_UNION:
1882 p = "END UNION";
1883 break;
1884 case ST_END_MAP:
1885 p = "END MAP";
1886 break;
1887 case ST_END_TYPE:
1888 p = "END TYPE";
1889 break;
1890 case ST_ENTRY:
1891 p = "ENTRY";
1892 break;
1893 case ST_EQUIVALENCE:
1894 p = "EQUIVALENCE";
1895 break;
1896 case ST_ERROR_STOP:
1897 p = "ERROR STOP";
1898 break;
1899 case ST_EXIT:
1900 p = "EXIT";
1901 break;
1902 case ST_FLUSH:
1903 p = "FLUSH";
1904 break;
1905 case ST_FORALL_BLOCK: /* Fall through */
1906 case ST_FORALL:
1907 p = "FORALL";
1908 break;
1909 case ST_FORMAT:
1910 p = "FORMAT";
1911 break;
1912 case ST_FUNCTION:
1913 p = "FUNCTION";
1914 break;
1915 case ST_GENERIC:
1916 p = "GENERIC";
1917 break;
1918 case ST_GOTO:
1919 p = "GOTO";
1920 break;
1921 case ST_IF_BLOCK:
1922 p = _("block IF");
1923 break;
1924 case ST_IMPLICIT:
1925 p = "IMPLICIT";
1926 break;
1927 case ST_IMPLICIT_NONE:
1928 p = "IMPLICIT NONE";
1929 break;
1930 case ST_IMPLIED_ENDDO:
1931 p = _("implied END DO");
1932 break;
1933 case ST_IMPORT:
1934 p = "IMPORT";
1935 break;
1936 case ST_INQUIRE:
1937 p = "INQUIRE";
1938 break;
1939 case ST_INTERFACE:
1940 p = "INTERFACE";
1941 break;
1942 case ST_LOCK:
1943 p = "LOCK";
1944 break;
1945 case ST_PARAMETER:
1946 p = "PARAMETER";
1947 break;
1948 case ST_PRIVATE:
1949 p = "PRIVATE";
1950 break;
1951 case ST_PUBLIC:
1952 p = "PUBLIC";
1953 break;
1954 case ST_MODULE:
1955 p = "MODULE";
1956 break;
1957 case ST_SUBMODULE:
1958 p = "SUBMODULE";
1959 break;
1960 case ST_PAUSE:
1961 p = "PAUSE";
1962 break;
1963 case ST_MODULE_PROC:
1964 p = "MODULE PROCEDURE";
1965 break;
1966 case ST_NAMELIST:
1967 p = "NAMELIST";
1968 break;
1969 case ST_NULLIFY:
1970 p = "NULLIFY";
1971 break;
1972 case ST_OPEN:
1973 p = "OPEN";
1974 break;
1975 case ST_PROGRAM:
1976 p = "PROGRAM";
1977 break;
1978 case ST_PROCEDURE:
1979 p = "PROCEDURE";
1980 break;
1981 case ST_READ:
1982 p = "READ";
1983 break;
1984 case ST_RETURN:
1985 p = "RETURN";
1986 break;
1987 case ST_REWIND:
1988 p = "REWIND";
1989 break;
1990 case ST_STOP:
1991 p = "STOP";
1992 break;
1993 case ST_SYNC_ALL:
1994 p = "SYNC ALL";
1995 break;
1996 case ST_SYNC_IMAGES:
1997 p = "SYNC IMAGES";
1998 break;
1999 case ST_SYNC_MEMORY:
2000 p = "SYNC MEMORY";
2001 break;
2002 case ST_SUBROUTINE:
2003 p = "SUBROUTINE";
2004 break;
2005 case ST_TYPE:
2006 p = "TYPE";
2007 break;
2008 case ST_UNLOCK:
2009 p = "UNLOCK";
2010 break;
2011 case ST_USE:
2012 p = "USE";
2013 break;
2014 case ST_WHERE_BLOCK: /* Fall through */
2015 case ST_WHERE:
2016 p = "WHERE";
2017 break;
2018 case ST_WAIT:
2019 p = "WAIT";
2020 break;
2021 case ST_WRITE:
2022 p = "WRITE";
2023 break;
2024 case ST_ASSIGNMENT:
2025 p = _("assignment");
2026 break;
2027 case ST_POINTER_ASSIGNMENT:
2028 p = _("pointer assignment");
2029 break;
2030 case ST_SELECT_CASE:
2031 p = "SELECT CASE";
2032 break;
2033 case ST_SELECT_TYPE:
2034 p = "SELECT TYPE";
2035 break;
2036 case ST_TYPE_IS:
2037 p = "TYPE IS";
2038 break;
2039 case ST_CLASS_IS:
2040 p = "CLASS IS";
2041 break;
2042 case ST_SEQUENCE:
2043 p = "SEQUENCE";
2044 break;
2045 case ST_SIMPLE_IF:
2046 p = _("simple IF");
2047 break;
2048 case ST_STATEMENT_FUNCTION:
2049 p = "STATEMENT FUNCTION";
2050 break;
2051 case ST_LABEL_ASSIGNMENT:
2052 p = "LABEL ASSIGNMENT";
2053 break;
2054 case ST_ENUM:
2055 p = "ENUM DEFINITION";
2056 break;
2057 case ST_ENUMERATOR:
2058 p = "ENUMERATOR DEFINITION";
2059 break;
2060 case ST_END_ENUM:
2061 p = "END ENUM";
2062 break;
2063 case ST_OACC_PARALLEL_LOOP:
2064 p = "!$ACC PARALLEL LOOP";
2065 break;
2066 case ST_OACC_END_PARALLEL_LOOP:
2067 p = "!$ACC END PARALLEL LOOP";
2068 break;
2069 case ST_OACC_PARALLEL:
2070 p = "!$ACC PARALLEL";
2071 break;
2072 case ST_OACC_END_PARALLEL:
2073 p = "!$ACC END PARALLEL";
2074 break;
2075 case ST_OACC_KERNELS:
2076 p = "!$ACC KERNELS";
2077 break;
2078 case ST_OACC_END_KERNELS:
2079 p = "!$ACC END KERNELS";
2080 break;
2081 case ST_OACC_KERNELS_LOOP:
2082 p = "!$ACC KERNELS LOOP";
2083 break;
2084 case ST_OACC_END_KERNELS_LOOP:
2085 p = "!$ACC END KERNELS LOOP";
2086 break;
2087 case ST_OACC_DATA:
2088 p = "!$ACC DATA";
2089 break;
2090 case ST_OACC_END_DATA:
2091 p = "!$ACC END DATA";
2092 break;
2093 case ST_OACC_HOST_DATA:
2094 p = "!$ACC HOST_DATA";
2095 break;
2096 case ST_OACC_END_HOST_DATA:
2097 p = "!$ACC END HOST_DATA";
2098 break;
2099 case ST_OACC_LOOP:
2100 p = "!$ACC LOOP";
2101 break;
2102 case ST_OACC_END_LOOP:
2103 p = "!$ACC END LOOP";
2104 break;
2105 case ST_OACC_DECLARE:
2106 p = "!$ACC DECLARE";
2107 break;
2108 case ST_OACC_UPDATE:
2109 p = "!$ACC UPDATE";
2110 break;
2111 case ST_OACC_WAIT:
2112 p = "!$ACC WAIT";
2113 break;
2114 case ST_OACC_CACHE:
2115 p = "!$ACC CACHE";
2116 break;
2117 case ST_OACC_ENTER_DATA:
2118 p = "!$ACC ENTER DATA";
2119 break;
2120 case ST_OACC_EXIT_DATA:
2121 p = "!$ACC EXIT DATA";
2122 break;
2123 case ST_OACC_ROUTINE:
2124 p = "!$ACC ROUTINE";
2125 break;
2126 case ST_OACC_ATOMIC:
2127 p = "!ACC ATOMIC";
2128 break;
2129 case ST_OACC_END_ATOMIC:
2130 p = "!ACC END ATOMIC";
2131 break;
2132 case ST_OMP_ATOMIC:
2133 p = "!$OMP ATOMIC";
2134 break;
2135 case ST_OMP_BARRIER:
2136 p = "!$OMP BARRIER";
2137 break;
2138 case ST_OMP_CANCEL:
2139 p = "!$OMP CANCEL";
2140 break;
2141 case ST_OMP_CANCELLATION_POINT:
2142 p = "!$OMP CANCELLATION POINT";
2143 break;
2144 case ST_OMP_CRITICAL:
2145 p = "!$OMP CRITICAL";
2146 break;
2147 case ST_OMP_DECLARE_REDUCTION:
2148 p = "!$OMP DECLARE REDUCTION";
2149 break;
2150 case ST_OMP_DECLARE_SIMD:
2151 p = "!$OMP DECLARE SIMD";
2152 break;
2153 case ST_OMP_DECLARE_TARGET:
2154 p = "!$OMP DECLARE TARGET";
2155 break;
2156 case ST_OMP_DISTRIBUTE:
2157 p = "!$OMP DISTRIBUTE";
2158 break;
2159 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
2160 p = "!$OMP DISTRIBUTE PARALLEL DO";
2161 break;
2162 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
2163 p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
2164 break;
2165 case ST_OMP_DISTRIBUTE_SIMD:
2166 p = "!$OMP DISTRIBUTE SIMD";
2167 break;
2168 case ST_OMP_DO:
2169 p = "!$OMP DO";
2170 break;
2171 case ST_OMP_DO_SIMD:
2172 p = "!$OMP DO SIMD";
2173 break;
2174 case ST_OMP_END_ATOMIC:
2175 p = "!$OMP END ATOMIC";
2176 break;
2177 case ST_OMP_END_CRITICAL:
2178 p = "!$OMP END CRITICAL";
2179 break;
2180 case ST_OMP_END_DISTRIBUTE:
2181 p = "!$OMP END DISTRIBUTE";
2182 break;
2183 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
2184 p = "!$OMP END DISTRIBUTE PARALLEL DO";
2185 break;
2186 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
2187 p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
2188 break;
2189 case ST_OMP_END_DISTRIBUTE_SIMD:
2190 p = "!$OMP END DISTRIBUTE SIMD";
2191 break;
2192 case ST_OMP_END_DO:
2193 p = "!$OMP END DO";
2194 break;
2195 case ST_OMP_END_DO_SIMD:
2196 p = "!$OMP END DO SIMD";
2197 break;
2198 case ST_OMP_END_SIMD:
2199 p = "!$OMP END SIMD";
2200 break;
2201 case ST_OMP_END_MASTER:
2202 p = "!$OMP END MASTER";
2203 break;
2204 case ST_OMP_END_ORDERED:
2205 p = "!$OMP END ORDERED";
2206 break;
2207 case ST_OMP_END_PARALLEL:
2208 p = "!$OMP END PARALLEL";
2209 break;
2210 case ST_OMP_END_PARALLEL_DO:
2211 p = "!$OMP END PARALLEL DO";
2212 break;
2213 case ST_OMP_END_PARALLEL_DO_SIMD:
2214 p = "!$OMP END PARALLEL DO SIMD";
2215 break;
2216 case ST_OMP_END_PARALLEL_SECTIONS:
2217 p = "!$OMP END PARALLEL SECTIONS";
2218 break;
2219 case ST_OMP_END_PARALLEL_WORKSHARE:
2220 p = "!$OMP END PARALLEL WORKSHARE";
2221 break;
2222 case ST_OMP_END_SECTIONS:
2223 p = "!$OMP END SECTIONS";
2224 break;
2225 case ST_OMP_END_SINGLE:
2226 p = "!$OMP END SINGLE";
2227 break;
2228 case ST_OMP_END_TASK:
2229 p = "!$OMP END TASK";
2230 break;
2231 case ST_OMP_END_TARGET:
2232 p = "!$OMP END TARGET";
2233 break;
2234 case ST_OMP_END_TARGET_DATA:
2235 p = "!$OMP END TARGET DATA";
2236 break;
2237 case ST_OMP_END_TARGET_PARALLEL:
2238 p = "!$OMP END TARGET PARALLEL";
2239 break;
2240 case ST_OMP_END_TARGET_PARALLEL_DO:
2241 p = "!$OMP END TARGET PARALLEL DO";
2242 break;
2243 case ST_OMP_END_TARGET_PARALLEL_DO_SIMD:
2244 p = "!$OMP END TARGET PARALLEL DO SIMD";
2245 break;
2246 case ST_OMP_END_TARGET_SIMD:
2247 p = "!$OMP END TARGET SIMD";
2248 break;
2249 case ST_OMP_END_TARGET_TEAMS:
2250 p = "!$OMP END TARGET TEAMS";
2251 break;
2252 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
2253 p = "!$OMP END TARGET TEAMS DISTRIBUTE";
2254 break;
2255 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2256 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2257 break;
2258 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2259 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2260 break;
2261 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
2262 p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2263 break;
2264 case ST_OMP_END_TASKGROUP:
2265 p = "!$OMP END TASKGROUP";
2266 break;
2267 case ST_OMP_END_TASKLOOP:
2268 p = "!$OMP END TASKLOOP";
2269 break;
2270 case ST_OMP_END_TASKLOOP_SIMD:
2271 p = "!$OMP END TASKLOOP SIMD";
2272 break;
2273 case ST_OMP_END_TEAMS:
2274 p = "!$OMP END TEAMS";
2275 break;
2276 case ST_OMP_END_TEAMS_DISTRIBUTE:
2277 p = "!$OMP END TEAMS DISTRIBUTE";
2278 break;
2279 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
2280 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2281 break;
2282 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2283 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2284 break;
2285 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
2286 p = "!$OMP END TEAMS DISTRIBUTE SIMD";
2287 break;
2288 case ST_OMP_END_WORKSHARE:
2289 p = "!$OMP END WORKSHARE";
2290 break;
2291 case ST_OMP_FLUSH:
2292 p = "!$OMP FLUSH";
2293 break;
2294 case ST_OMP_MASTER:
2295 p = "!$OMP MASTER";
2296 break;
2297 case ST_OMP_ORDERED:
2298 case ST_OMP_ORDERED_DEPEND:
2299 p = "!$OMP ORDERED";
2300 break;
2301 case ST_OMP_PARALLEL:
2302 p = "!$OMP PARALLEL";
2303 break;
2304 case ST_OMP_PARALLEL_DO:
2305 p = "!$OMP PARALLEL DO";
2306 break;
2307 case ST_OMP_PARALLEL_DO_SIMD:
2308 p = "!$OMP PARALLEL DO SIMD";
2309 break;
2310 case ST_OMP_PARALLEL_SECTIONS:
2311 p = "!$OMP PARALLEL SECTIONS";
2312 break;
2313 case ST_OMP_PARALLEL_WORKSHARE:
2314 p = "!$OMP PARALLEL WORKSHARE";
2315 break;
2316 case ST_OMP_SECTIONS:
2317 p = "!$OMP SECTIONS";
2318 break;
2319 case ST_OMP_SECTION:
2320 p = "!$OMP SECTION";
2321 break;
2322 case ST_OMP_SIMD:
2323 p = "!$OMP SIMD";
2324 break;
2325 case ST_OMP_SINGLE:
2326 p = "!$OMP SINGLE";
2327 break;
2328 case ST_OMP_TARGET:
2329 p = "!$OMP TARGET";
2330 break;
2331 case ST_OMP_TARGET_DATA:
2332 p = "!$OMP TARGET DATA";
2333 break;
2334 case ST_OMP_TARGET_ENTER_DATA:
2335 p = "!$OMP TARGET ENTER DATA";
2336 break;
2337 case ST_OMP_TARGET_EXIT_DATA:
2338 p = "!$OMP TARGET EXIT DATA";
2339 break;
2340 case ST_OMP_TARGET_PARALLEL:
2341 p = "!$OMP TARGET PARALLEL";
2342 break;
2343 case ST_OMP_TARGET_PARALLEL_DO:
2344 p = "!$OMP TARGET PARALLEL DO";
2345 break;
2346 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
2347 p = "!$OMP TARGET PARALLEL DO SIMD";
2348 break;
2349 case ST_OMP_TARGET_SIMD:
2350 p = "!$OMP TARGET SIMD";
2351 break;
2352 case ST_OMP_TARGET_TEAMS:
2353 p = "!$OMP TARGET TEAMS";
2354 break;
2355 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
2356 p = "!$OMP TARGET TEAMS DISTRIBUTE";
2357 break;
2358 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2359 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2360 break;
2361 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2362 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2363 break;
2364 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2365 p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2366 break;
2367 case ST_OMP_TARGET_UPDATE:
2368 p = "!$OMP TARGET UPDATE";
2369 break;
2370 case ST_OMP_TASK:
2371 p = "!$OMP TASK";
2372 break;
2373 case ST_OMP_TASKGROUP:
2374 p = "!$OMP TASKGROUP";
2375 break;
2376 case ST_OMP_TASKLOOP:
2377 p = "!$OMP TASKLOOP";
2378 break;
2379 case ST_OMP_TASKLOOP_SIMD:
2380 p = "!$OMP TASKLOOP SIMD";
2381 break;
2382 case ST_OMP_TASKWAIT:
2383 p = "!$OMP TASKWAIT";
2384 break;
2385 case ST_OMP_TASKYIELD:
2386 p = "!$OMP TASKYIELD";
2387 break;
2388 case ST_OMP_TEAMS:
2389 p = "!$OMP TEAMS";
2390 break;
2391 case ST_OMP_TEAMS_DISTRIBUTE:
2392 p = "!$OMP TEAMS DISTRIBUTE";
2393 break;
2394 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2395 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2396 break;
2397 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2398 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2399 break;
2400 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
2401 p = "!$OMP TEAMS DISTRIBUTE SIMD";
2402 break;
2403 case ST_OMP_THREADPRIVATE:
2404 p = "!$OMP THREADPRIVATE";
2405 break;
2406 case ST_OMP_WORKSHARE:
2407 p = "!$OMP WORKSHARE";
2408 break;
2409 default:
2410 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2413 return p;
2417 /* Create a symbol for the main program and assign it to ns->proc_name. */
2419 static void
2420 main_program_symbol (gfc_namespace *ns, const char *name)
2422 gfc_symbol *main_program;
2423 symbol_attribute attr;
2425 gfc_get_symbol (name, ns, &main_program);
2426 gfc_clear_attr (&attr);
2427 attr.flavor = FL_PROGRAM;
2428 attr.proc = PROC_UNKNOWN;
2429 attr.subroutine = 1;
2430 attr.access = ACCESS_PUBLIC;
2431 attr.is_main_program = 1;
2432 main_program->attr = attr;
2433 main_program->declared_at = gfc_current_locus;
2434 ns->proc_name = main_program;
2435 gfc_commit_symbols ();
2439 /* Do whatever is necessary to accept the last statement. */
2441 static void
2442 accept_statement (gfc_statement st)
2444 switch (st)
2446 case ST_IMPLICIT_NONE:
2447 case ST_IMPLICIT:
2448 break;
2450 case ST_FUNCTION:
2451 case ST_SUBROUTINE:
2452 case ST_MODULE:
2453 case ST_SUBMODULE:
2454 gfc_current_ns->proc_name = gfc_new_block;
2455 break;
2457 /* If the statement is the end of a block, lay down a special code
2458 that allows a branch to the end of the block from within the
2459 construct. IF and SELECT are treated differently from DO
2460 (where EXEC_NOP is added inside the loop) for two
2461 reasons:
2462 1. END DO has a meaning in the sense that after a GOTO to
2463 it, the loop counter must be increased.
2464 2. IF blocks and SELECT blocks can consist of multiple
2465 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2466 Putting the label before the END IF would make the jump
2467 from, say, the ELSE IF block to the END IF illegal. */
2469 case ST_ENDIF:
2470 case ST_END_SELECT:
2471 case ST_END_CRITICAL:
2472 if (gfc_statement_label != NULL)
2474 new_st.op = EXEC_END_NESTED_BLOCK;
2475 add_statement ();
2477 break;
2479 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2480 one parallel block. Thus, we add the special code to the nested block
2481 itself, instead of the parent one. */
2482 case ST_END_BLOCK:
2483 case ST_END_ASSOCIATE:
2484 if (gfc_statement_label != NULL)
2486 new_st.op = EXEC_END_BLOCK;
2487 add_statement ();
2489 break;
2491 /* The end-of-program unit statements do not get the special
2492 marker and require a statement of some sort if they are a
2493 branch target. */
2495 case ST_END_PROGRAM:
2496 case ST_END_FUNCTION:
2497 case ST_END_SUBROUTINE:
2498 if (gfc_statement_label != NULL)
2500 new_st.op = EXEC_RETURN;
2501 add_statement ();
2503 else
2505 new_st.op = EXEC_END_PROCEDURE;
2506 add_statement ();
2509 break;
2511 case ST_ENTRY:
2512 case_executable:
2513 case_exec_markers:
2514 add_statement ();
2515 break;
2517 default:
2518 break;
2521 gfc_commit_symbols ();
2522 gfc_warning_check ();
2523 gfc_clear_new_st ();
2527 /* Undo anything tentative that has been built for the current statement,
2528 except if a gfc_charlen structure has been added to current namespace's
2529 list of gfc_charlen structure. */
2531 static void
2532 reject_statement (void)
2534 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
2535 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
2537 gfc_reject_data (gfc_current_ns);
2539 gfc_new_block = NULL;
2540 gfc_undo_symbols ();
2541 gfc_clear_warning ();
2542 undo_new_statement ();
2546 /* Generic complaint about an out of order statement. We also do
2547 whatever is necessary to clean up. */
2549 static void
2550 unexpected_statement (gfc_statement st)
2552 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
2554 reject_statement ();
2558 /* Given the next statement seen by the matcher, make sure that it is
2559 in proper order with the last. This subroutine is initialized by
2560 calling it with an argument of ST_NONE. If there is a problem, we
2561 issue an error and return false. Otherwise we return true.
2563 Individual parsers need to verify that the statements seen are
2564 valid before calling here, i.e., ENTRY statements are not allowed in
2565 INTERFACE blocks. The following diagram is taken from the standard:
2567 +---------------------------------------+
2568 | program subroutine function module |
2569 +---------------------------------------+
2570 | use |
2571 +---------------------------------------+
2572 | import |
2573 +---------------------------------------+
2574 | | implicit none |
2575 | +-----------+------------------+
2576 | | parameter | implicit |
2577 | +-----------+------------------+
2578 | format | | derived type |
2579 | entry | parameter | interface |
2580 | | data | specification |
2581 | | | statement func |
2582 | +-----------+------------------+
2583 | | data | executable |
2584 +--------+-----------+------------------+
2585 | contains |
2586 +---------------------------------------+
2587 | internal module/subprogram |
2588 +---------------------------------------+
2589 | end |
2590 +---------------------------------------+
2594 enum state_order
2596 ORDER_START,
2597 ORDER_USE,
2598 ORDER_IMPORT,
2599 ORDER_IMPLICIT_NONE,
2600 ORDER_IMPLICIT,
2601 ORDER_SPEC,
2602 ORDER_EXEC
2605 typedef struct
2607 enum state_order state;
2608 gfc_statement last_statement;
2609 locus where;
2611 st_state;
2613 static bool
2614 verify_st_order (st_state *p, gfc_statement st, bool silent)
2617 switch (st)
2619 case ST_NONE:
2620 p->state = ORDER_START;
2621 break;
2623 case ST_USE:
2624 if (p->state > ORDER_USE)
2625 goto order;
2626 p->state = ORDER_USE;
2627 break;
2629 case ST_IMPORT:
2630 if (p->state > ORDER_IMPORT)
2631 goto order;
2632 p->state = ORDER_IMPORT;
2633 break;
2635 case ST_IMPLICIT_NONE:
2636 if (p->state > ORDER_IMPLICIT)
2637 goto order;
2639 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2640 statement disqualifies a USE but not an IMPLICIT NONE.
2641 Duplicate IMPLICIT NONEs are caught when the implicit types
2642 are set. */
2644 p->state = ORDER_IMPLICIT_NONE;
2645 break;
2647 case ST_IMPLICIT:
2648 if (p->state > ORDER_IMPLICIT)
2649 goto order;
2650 p->state = ORDER_IMPLICIT;
2651 break;
2653 case ST_FORMAT:
2654 case ST_ENTRY:
2655 if (p->state < ORDER_IMPLICIT_NONE)
2656 p->state = ORDER_IMPLICIT_NONE;
2657 break;
2659 case ST_PARAMETER:
2660 if (p->state >= ORDER_EXEC)
2661 goto order;
2662 if (p->state < ORDER_IMPLICIT)
2663 p->state = ORDER_IMPLICIT;
2664 break;
2666 case ST_DATA:
2667 if (p->state < ORDER_SPEC)
2668 p->state = ORDER_SPEC;
2669 break;
2671 case ST_PUBLIC:
2672 case ST_PRIVATE:
2673 case ST_STRUCTURE_DECL:
2674 case ST_DERIVED_DECL:
2675 case_decl:
2676 if (p->state >= ORDER_EXEC)
2677 goto order;
2678 if (p->state < ORDER_SPEC)
2679 p->state = ORDER_SPEC;
2680 break;
2682 case_omp_decl:
2683 /* The OpenMP directives have to be somewhere in the specification
2684 part, but there are no further requirements on their ordering.
2685 Thus don't adjust p->state, just ignore them. */
2686 if (p->state >= ORDER_EXEC)
2687 goto order;
2688 break;
2690 case_executable:
2691 case_exec_markers:
2692 if (p->state < ORDER_EXEC)
2693 p->state = ORDER_EXEC;
2694 break;
2696 default:
2697 return false;
2700 /* All is well, record the statement in case we need it next time. */
2701 p->where = gfc_current_locus;
2702 p->last_statement = st;
2703 return true;
2705 order:
2706 if (!silent)
2707 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2708 gfc_ascii_statement (st),
2709 gfc_ascii_statement (p->last_statement), &p->where);
2711 return false;
2715 /* Handle an unexpected end of file. This is a show-stopper... */
2717 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
2719 static void
2720 unexpected_eof (void)
2722 gfc_state_data *p;
2724 gfc_error ("Unexpected end of file in %qs", gfc_source_file);
2726 /* Memory cleanup. Move to "second to last". */
2727 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
2728 p = p->previous);
2730 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
2731 gfc_done_2 ();
2733 longjmp (eof_buf, 1);
2737 /* Parse the CONTAINS section of a derived type definition. */
2739 gfc_access gfc_typebound_default_access;
2741 static bool
2742 parse_derived_contains (void)
2744 gfc_state_data s;
2745 bool seen_private = false;
2746 bool seen_comps = false;
2747 bool error_flag = false;
2748 bool to_finish;
2750 gcc_assert (gfc_current_state () == COMP_DERIVED);
2751 gcc_assert (gfc_current_block ());
2753 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2754 section. */
2755 if (gfc_current_block ()->attr.sequence)
2756 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2757 " section at %C", gfc_current_block ()->name);
2758 if (gfc_current_block ()->attr.is_bind_c)
2759 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2760 " section at %C", gfc_current_block ()->name);
2762 accept_statement (ST_CONTAINS);
2763 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
2765 gfc_typebound_default_access = ACCESS_PUBLIC;
2767 to_finish = false;
2768 while (!to_finish)
2770 gfc_statement st;
2771 st = next_statement ();
2772 switch (st)
2774 case ST_NONE:
2775 unexpected_eof ();
2776 break;
2778 case ST_DATA_DECL:
2779 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2780 goto error;
2782 case ST_PROCEDURE:
2783 if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
2784 goto error;
2786 accept_statement (ST_PROCEDURE);
2787 seen_comps = true;
2788 break;
2790 case ST_GENERIC:
2791 if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
2792 goto error;
2794 accept_statement (ST_GENERIC);
2795 seen_comps = true;
2796 break;
2798 case ST_FINAL:
2799 if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
2800 " at %C"))
2801 goto error;
2803 accept_statement (ST_FINAL);
2804 seen_comps = true;
2805 break;
2807 case ST_END_TYPE:
2808 to_finish = true;
2810 if (!seen_comps
2811 && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
2812 "at %C with empty CONTAINS section")))
2813 goto error;
2815 /* ST_END_TYPE is accepted by parse_derived after return. */
2816 break;
2818 case ST_PRIVATE:
2819 if (!gfc_find_state (COMP_MODULE))
2821 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2822 "a MODULE");
2823 goto error;
2826 if (seen_comps)
2828 gfc_error ("PRIVATE statement at %C must precede procedure"
2829 " bindings");
2830 goto error;
2833 if (seen_private)
2835 gfc_error ("Duplicate PRIVATE statement at %C");
2836 goto error;
2839 accept_statement (ST_PRIVATE);
2840 gfc_typebound_default_access = ACCESS_PRIVATE;
2841 seen_private = true;
2842 break;
2844 case ST_SEQUENCE:
2845 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2846 goto error;
2848 case ST_CONTAINS:
2849 gfc_error ("Already inside a CONTAINS block at %C");
2850 goto error;
2852 default:
2853 unexpected_statement (st);
2854 break;
2857 continue;
2859 error:
2860 error_flag = true;
2861 reject_statement ();
2864 pop_state ();
2865 gcc_assert (gfc_current_state () == COMP_DERIVED);
2867 return error_flag;
2871 /* Set attributes for the parent symbol based on the attributes of a component
2872 and raise errors if conflicting attributes are found for the component. */
2874 static void
2875 check_component (gfc_symbol *sym, gfc_component *c, gfc_component **lockp,
2876 gfc_component **eventp)
2878 bool coarray, lock_type, event_type, allocatable, pointer;
2879 coarray = lock_type = event_type = allocatable = pointer = false;
2880 gfc_component *lock_comp = NULL, *event_comp = NULL;
2882 if (lockp) lock_comp = *lockp;
2883 if (eventp) event_comp = *eventp;
2885 /* Look for allocatable components. */
2886 if (c->attr.allocatable
2887 || (c->ts.type == BT_CLASS && c->attr.class_ok
2888 && CLASS_DATA (c)->attr.allocatable)
2889 || (c->ts.type == BT_DERIVED && !c->attr.pointer
2890 && c->ts.u.derived->attr.alloc_comp))
2892 allocatable = true;
2893 sym->attr.alloc_comp = 1;
2896 /* Look for pointer components. */
2897 if (c->attr.pointer
2898 || (c->ts.type == BT_CLASS && c->attr.class_ok
2899 && CLASS_DATA (c)->attr.class_pointer)
2900 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2902 pointer = true;
2903 sym->attr.pointer_comp = 1;
2906 /* Look for procedure pointer components. */
2907 if (c->attr.proc_pointer
2908 || (c->ts.type == BT_DERIVED
2909 && c->ts.u.derived->attr.proc_pointer_comp))
2910 sym->attr.proc_pointer_comp = 1;
2912 /* Looking for coarray components. */
2913 if (c->attr.codimension
2914 || (c->ts.type == BT_CLASS && c->attr.class_ok
2915 && CLASS_DATA (c)->attr.codimension))
2917 coarray = true;
2918 sym->attr.coarray_comp = 1;
2921 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
2922 && !c->attr.pointer)
2924 coarray = true;
2925 sym->attr.coarray_comp = 1;
2928 /* Looking for lock_type components. */
2929 if ((c->ts.type == BT_DERIVED
2930 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2931 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2932 || (c->ts.type == BT_CLASS && c->attr.class_ok
2933 && CLASS_DATA (c)->ts.u.derived->from_intmod
2934 == INTMOD_ISO_FORTRAN_ENV
2935 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2936 == ISOFORTRAN_LOCK_TYPE)
2937 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
2938 && !allocatable && !pointer))
2940 lock_type = 1;
2941 lock_comp = c;
2942 sym->attr.lock_comp = 1;
2945 /* Looking for event_type components. */
2946 if ((c->ts.type == BT_DERIVED
2947 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2948 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2949 || (c->ts.type == BT_CLASS && c->attr.class_ok
2950 && CLASS_DATA (c)->ts.u.derived->from_intmod
2951 == INTMOD_ISO_FORTRAN_ENV
2952 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2953 == ISOFORTRAN_EVENT_TYPE)
2954 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.event_comp
2955 && !allocatable && !pointer))
2957 event_type = 1;
2958 event_comp = c;
2959 sym->attr.event_comp = 1;
2962 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2963 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2964 unless there are nondirect [allocatable or pointer] components
2965 involved (cf. 1.3.33.1 and 1.3.33.3). */
2967 if (pointer && !coarray && lock_type)
2968 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2969 "codimension or be a subcomponent of a coarray, "
2970 "which is not possible as the component has the "
2971 "pointer attribute", c->name, &c->loc);
2972 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2973 && c->ts.u.derived->attr.lock_comp)
2974 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2975 "of type LOCK_TYPE, which must have a codimension or be a "
2976 "subcomponent of a coarray", c->name, &c->loc);
2978 if (lock_type && allocatable && !coarray)
2979 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2980 "a codimension", c->name, &c->loc);
2981 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
2982 && c->ts.u.derived->attr.lock_comp)
2983 gfc_error ("Allocatable component %s at %L must have a codimension as "
2984 "it has a noncoarray subcomponent of type LOCK_TYPE",
2985 c->name, &c->loc);
2987 if (sym->attr.coarray_comp && !coarray && lock_type)
2988 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2989 "subcomponent of type LOCK_TYPE must have a codimension or "
2990 "be a subcomponent of a coarray. (Variables of type %s may "
2991 "not have a codimension as already a coarray "
2992 "subcomponent exists)", c->name, &c->loc, sym->name);
2994 if (sym->attr.lock_comp && coarray && !lock_type)
2995 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2996 "subcomponent of type LOCK_TYPE must have a codimension or "
2997 "be a subcomponent of a coarray. (Variables of type %s may "
2998 "not have a codimension as %s at %L has a codimension or a "
2999 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
3000 sym->name, c->name, &c->loc);
3002 /* Similarly for EVENT TYPE. */
3004 if (pointer && !coarray && event_type)
3005 gfc_error ("Component %s at %L of type EVENT_TYPE must have a "
3006 "codimension or be a subcomponent of a coarray, "
3007 "which is not possible as the component has the "
3008 "pointer attribute", c->name, &c->loc);
3009 else if (pointer && !coarray && c->ts.type == BT_DERIVED
3010 && c->ts.u.derived->attr.event_comp)
3011 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
3012 "of type EVENT_TYPE, which must have a codimension or be a "
3013 "subcomponent of a coarray", c->name, &c->loc);
3015 if (event_type && allocatable && !coarray)
3016 gfc_error ("Allocatable component %s at %L of type EVENT_TYPE must have "
3017 "a codimension", c->name, &c->loc);
3018 else if (event_type && allocatable && c->ts.type == BT_DERIVED
3019 && c->ts.u.derived->attr.event_comp)
3020 gfc_error ("Allocatable component %s at %L must have a codimension as "
3021 "it has a noncoarray subcomponent of type EVENT_TYPE",
3022 c->name, &c->loc);
3024 if (sym->attr.coarray_comp && !coarray && event_type)
3025 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
3026 "subcomponent of type EVENT_TYPE must have a codimension or "
3027 "be a subcomponent of a coarray. (Variables of type %s may "
3028 "not have a codimension as already a coarray "
3029 "subcomponent exists)", c->name, &c->loc, sym->name);
3031 if (sym->attr.event_comp && coarray && !event_type)
3032 gfc_error ("Noncoarray component %s at %L of type EVENT_TYPE or with "
3033 "subcomponent of type EVENT_TYPE must have a codimension or "
3034 "be a subcomponent of a coarray. (Variables of type %s may "
3035 "not have a codimension as %s at %L has a codimension or a "
3036 "coarray subcomponent)", event_comp->name, &event_comp->loc,
3037 sym->name, c->name, &c->loc);
3039 /* Look for private components. */
3040 if (sym->component_access == ACCESS_PRIVATE
3041 || c->attr.access == ACCESS_PRIVATE
3042 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
3043 sym->attr.private_comp = 1;
3045 if (lockp) *lockp = lock_comp;
3046 if (eventp) *eventp = event_comp;
3050 static void parse_struct_map (gfc_statement);
3052 /* Parse a union component definition within a structure definition. */
3054 static void
3055 parse_union (void)
3057 int compiling;
3058 gfc_statement st;
3059 gfc_state_data s;
3060 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3061 gfc_symbol *un;
3063 accept_statement(ST_UNION);
3064 push_state (&s, COMP_UNION, gfc_new_block);
3065 un = gfc_new_block;
3067 compiling = 1;
3069 while (compiling)
3071 st = next_statement ();
3072 /* Only MAP declarations valid within a union. */
3073 switch (st)
3075 case ST_NONE:
3076 unexpected_eof ();
3078 case ST_MAP:
3079 accept_statement (ST_MAP);
3080 parse_struct_map (ST_MAP);
3081 /* Add a component to the union for each map. */
3082 if (!gfc_add_component (un, gfc_new_block->name, &c))
3084 gfc_internal_error ("failed to create map component '%s'",
3085 gfc_new_block->name);
3086 reject_statement ();
3087 return;
3089 c->ts.type = BT_DERIVED;
3090 c->ts.u.derived = gfc_new_block;
3091 /* Normally components get their initialization expressions when they
3092 are created in decl.c (build_struct) so we can look through the
3093 flat component list for initializers during resolution. Unions and
3094 maps create components along with their type definitions so we
3095 have to generate initializers here. */
3096 c->initializer = gfc_default_initializer (&c->ts);
3097 break;
3099 case ST_END_UNION:
3100 compiling = 0;
3101 accept_statement (ST_END_UNION);
3102 break;
3104 default:
3105 unexpected_statement (st);
3106 break;
3110 for (c = un->components; c; c = c->next)
3111 check_component (un, c, &lock_comp, &event_comp);
3113 /* Add the union as a component in its parent structure. */
3114 pop_state ();
3115 if (!gfc_add_component (gfc_current_block (), un->name, &c))
3117 gfc_internal_error ("failed to create union component '%s'", un->name);
3118 reject_statement ();
3119 return;
3121 c->ts.type = BT_UNION;
3122 c->ts.u.derived = un;
3123 c->initializer = gfc_default_initializer (&c->ts);
3125 un->attr.zero_comp = un->components == NULL;
3129 /* Parse a STRUCTURE or MAP. */
3131 static void
3132 parse_struct_map (gfc_statement block)
3134 int compiling_type;
3135 gfc_statement st;
3136 gfc_state_data s;
3137 gfc_symbol *sym;
3138 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3139 gfc_compile_state comp;
3140 gfc_statement ends;
3142 if (block == ST_STRUCTURE_DECL)
3144 comp = COMP_STRUCTURE;
3145 ends = ST_END_STRUCTURE;
3147 else
3149 gcc_assert (block == ST_MAP);
3150 comp = COMP_MAP;
3151 ends = ST_END_MAP;
3154 accept_statement(block);
3155 push_state (&s, comp, gfc_new_block);
3157 gfc_new_block->component_access = ACCESS_PUBLIC;
3158 compiling_type = 1;
3160 while (compiling_type)
3162 st = next_statement ();
3163 switch (st)
3165 case ST_NONE:
3166 unexpected_eof ();
3168 /* Nested structure declarations will be captured as ST_DATA_DECL. */
3169 case ST_STRUCTURE_DECL:
3170 /* Let a more specific error make it to decode_statement(). */
3171 if (gfc_error_check () == 0)
3172 gfc_error ("Syntax error in nested structure declaration at %C");
3173 reject_statement ();
3174 /* Skip the rest of this statement. */
3175 gfc_error_recovery ();
3176 break;
3178 case ST_UNION:
3179 accept_statement (ST_UNION);
3180 parse_union ();
3181 break;
3183 case ST_DATA_DECL:
3184 /* The data declaration was a nested/ad-hoc STRUCTURE field. */
3185 accept_statement (ST_DATA_DECL);
3186 if (gfc_new_block && gfc_new_block != gfc_current_block ()
3187 && gfc_new_block->attr.flavor == FL_STRUCT)
3188 parse_struct_map (ST_STRUCTURE_DECL);
3189 break;
3191 case ST_END_STRUCTURE:
3192 case ST_END_MAP:
3193 if (st == ends)
3195 accept_statement (st);
3196 compiling_type = 0;
3198 else
3199 unexpected_statement (st);
3200 break;
3202 default:
3203 unexpected_statement (st);
3204 break;
3208 /* Validate each component. */
3209 sym = gfc_current_block ();
3210 for (c = sym->components; c; c = c->next)
3211 check_component (sym, c, &lock_comp, &event_comp);
3213 sym->attr.zero_comp = (sym->components == NULL);
3215 /* Allow parse_union to find this structure to add to its list of maps. */
3216 if (block == ST_MAP)
3217 gfc_new_block = gfc_current_block ();
3219 pop_state ();
3223 /* Parse a derived type. */
3225 static void
3226 parse_derived (void)
3228 int compiling_type, seen_private, seen_sequence, seen_component;
3229 gfc_statement st;
3230 gfc_state_data s;
3231 gfc_symbol *sym;
3232 gfc_component *c, *lock_comp = NULL, *event_comp = NULL;
3234 accept_statement (ST_DERIVED_DECL);
3235 push_state (&s, COMP_DERIVED, gfc_new_block);
3237 gfc_new_block->component_access = ACCESS_PUBLIC;
3238 seen_private = 0;
3239 seen_sequence = 0;
3240 seen_component = 0;
3242 compiling_type = 1;
3244 while (compiling_type)
3246 st = next_statement ();
3247 switch (st)
3249 case ST_NONE:
3250 unexpected_eof ();
3252 case ST_DATA_DECL:
3253 case ST_PROCEDURE:
3254 accept_statement (st);
3255 seen_component = 1;
3256 break;
3258 case ST_FINAL:
3259 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
3260 break;
3262 case ST_END_TYPE:
3263 endType:
3264 compiling_type = 0;
3266 if (!seen_component)
3267 gfc_notify_std (GFC_STD_F2003, "Derived type "
3268 "definition at %C without components");
3270 accept_statement (ST_END_TYPE);
3271 break;
3273 case ST_PRIVATE:
3274 if (!gfc_find_state (COMP_MODULE))
3276 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
3277 "a MODULE");
3278 break;
3281 if (seen_component)
3283 gfc_error ("PRIVATE statement at %C must precede "
3284 "structure components");
3285 break;
3288 if (seen_private)
3289 gfc_error ("Duplicate PRIVATE statement at %C");
3291 s.sym->component_access = ACCESS_PRIVATE;
3293 accept_statement (ST_PRIVATE);
3294 seen_private = 1;
3295 break;
3297 case ST_SEQUENCE:
3298 if (seen_component)
3300 gfc_error ("SEQUENCE statement at %C must precede "
3301 "structure components");
3302 break;
3305 if (gfc_current_block ()->attr.sequence)
3306 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
3307 "TYPE statement");
3309 if (seen_sequence)
3311 gfc_error ("Duplicate SEQUENCE statement at %C");
3314 seen_sequence = 1;
3315 gfc_add_sequence (&gfc_current_block ()->attr,
3316 gfc_current_block ()->name, NULL);
3317 break;
3319 case ST_CONTAINS:
3320 gfc_notify_std (GFC_STD_F2003,
3321 "CONTAINS block in derived type"
3322 " definition at %C");
3324 accept_statement (ST_CONTAINS);
3325 parse_derived_contains ();
3326 goto endType;
3328 default:
3329 unexpected_statement (st);
3330 break;
3334 /* need to verify that all fields of the derived type are
3335 * interoperable with C if the type is declared to be bind(c)
3337 sym = gfc_current_block ();
3338 for (c = sym->components; c; c = c->next)
3339 check_component (sym, c, &lock_comp, &event_comp);
3341 if (!seen_component)
3342 sym->attr.zero_comp = 1;
3344 pop_state ();
3348 /* Parse an ENUM. */
3350 static void
3351 parse_enum (void)
3353 gfc_statement st;
3354 int compiling_enum;
3355 gfc_state_data s;
3356 int seen_enumerator = 0;
3358 push_state (&s, COMP_ENUM, gfc_new_block);
3360 compiling_enum = 1;
3362 while (compiling_enum)
3364 st = next_statement ();
3365 switch (st)
3367 case ST_NONE:
3368 unexpected_eof ();
3369 break;
3371 case ST_ENUMERATOR:
3372 seen_enumerator = 1;
3373 accept_statement (st);
3374 break;
3376 case ST_END_ENUM:
3377 compiling_enum = 0;
3378 if (!seen_enumerator)
3379 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
3380 accept_statement (st);
3381 break;
3383 default:
3384 gfc_free_enum_history ();
3385 unexpected_statement (st);
3386 break;
3389 pop_state ();
3393 /* Parse an interface. We must be able to deal with the possibility
3394 of recursive interfaces. The parse_spec() subroutine is mutually
3395 recursive with parse_interface(). */
3397 static gfc_statement parse_spec (gfc_statement);
3399 static void
3400 parse_interface (void)
3402 gfc_compile_state new_state = COMP_NONE, current_state;
3403 gfc_symbol *prog_unit, *sym;
3404 gfc_interface_info save;
3405 gfc_state_data s1, s2;
3406 gfc_statement st;
3408 accept_statement (ST_INTERFACE);
3410 current_interface.ns = gfc_current_ns;
3411 save = current_interface;
3413 sym = (current_interface.type == INTERFACE_GENERIC
3414 || current_interface.type == INTERFACE_USER_OP)
3415 ? gfc_new_block : NULL;
3417 push_state (&s1, COMP_INTERFACE, sym);
3418 current_state = COMP_NONE;
3420 loop:
3421 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
3423 st = next_statement ();
3424 switch (st)
3426 case ST_NONE:
3427 unexpected_eof ();
3429 case ST_SUBROUTINE:
3430 case ST_FUNCTION:
3431 if (st == ST_SUBROUTINE)
3432 new_state = COMP_SUBROUTINE;
3433 else if (st == ST_FUNCTION)
3434 new_state = COMP_FUNCTION;
3435 if (gfc_new_block->attr.pointer)
3437 gfc_new_block->attr.pointer = 0;
3438 gfc_new_block->attr.proc_pointer = 1;
3440 if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
3441 gfc_new_block->formal, NULL))
3443 reject_statement ();
3444 gfc_free_namespace (gfc_current_ns);
3445 goto loop;
3447 /* F2008 C1210 forbids the IMPORT statement in module procedure
3448 interface bodies and the flag is set to import symbols. */
3449 if (gfc_new_block->attr.module_procedure)
3450 gfc_current_ns->has_import_set = 1;
3451 break;
3453 case ST_PROCEDURE:
3454 case ST_MODULE_PROC: /* The module procedure matcher makes
3455 sure the context is correct. */
3456 accept_statement (st);
3457 gfc_free_namespace (gfc_current_ns);
3458 goto loop;
3460 case ST_END_INTERFACE:
3461 gfc_free_namespace (gfc_current_ns);
3462 gfc_current_ns = current_interface.ns;
3463 goto done;
3465 default:
3466 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
3467 gfc_ascii_statement (st));
3468 reject_statement ();
3469 gfc_free_namespace (gfc_current_ns);
3470 goto loop;
3474 /* Make sure that the generic name has the right attribute. */
3475 if (current_interface.type == INTERFACE_GENERIC
3476 && current_state == COMP_NONE)
3478 if (new_state == COMP_FUNCTION && sym)
3479 gfc_add_function (&sym->attr, sym->name, NULL);
3480 else if (new_state == COMP_SUBROUTINE && sym)
3481 gfc_add_subroutine (&sym->attr, sym->name, NULL);
3483 current_state = new_state;
3486 if (current_interface.type == INTERFACE_ABSTRACT)
3488 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
3489 if (gfc_is_intrinsic_typename (gfc_new_block->name))
3490 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
3491 "cannot be the same as an intrinsic type",
3492 gfc_new_block->name);
3495 push_state (&s2, new_state, gfc_new_block);
3496 accept_statement (st);
3497 prog_unit = gfc_new_block;
3498 prog_unit->formal_ns = gfc_current_ns;
3499 if (prog_unit == prog_unit->formal_ns->proc_name
3500 && prog_unit->ns != prog_unit->formal_ns)
3501 prog_unit->refs++;
3503 decl:
3504 /* Read data declaration statements. */
3505 st = parse_spec (ST_NONE);
3506 in_specification_block = true;
3508 /* Since the interface block does not permit an IMPLICIT statement,
3509 the default type for the function or the result must be taken
3510 from the formal namespace. */
3511 if (new_state == COMP_FUNCTION)
3513 if (prog_unit->result == prog_unit
3514 && prog_unit->ts.type == BT_UNKNOWN)
3515 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
3516 else if (prog_unit->result != prog_unit
3517 && prog_unit->result->ts.type == BT_UNKNOWN)
3518 gfc_set_default_type (prog_unit->result, 1,
3519 prog_unit->formal_ns);
3522 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
3524 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3525 gfc_ascii_statement (st));
3526 reject_statement ();
3527 goto decl;
3530 /* Add EXTERNAL attribute to function or subroutine. */
3531 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
3532 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
3534 current_interface = save;
3535 gfc_add_interface (prog_unit);
3536 pop_state ();
3538 if (current_interface.ns
3539 && current_interface.ns->proc_name
3540 && strcmp (current_interface.ns->proc_name->name,
3541 prog_unit->name) == 0)
3542 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3543 "enclosing procedure", prog_unit->name,
3544 &current_interface.ns->proc_name->declared_at);
3546 goto loop;
3548 done:
3549 pop_state ();
3553 /* Associate function characteristics by going back to the function
3554 declaration and rematching the prefix. */
3556 static match
3557 match_deferred_characteristics (gfc_typespec * ts)
3559 locus loc;
3560 match m = MATCH_ERROR;
3561 char name[GFC_MAX_SYMBOL_LEN + 1];
3563 loc = gfc_current_locus;
3565 gfc_current_locus = gfc_current_block ()->declared_at;
3567 gfc_clear_error ();
3568 gfc_buffer_error (true);
3569 m = gfc_match_prefix (ts);
3570 gfc_buffer_error (false);
3572 if (ts->type == BT_DERIVED)
3574 ts->kind = 0;
3576 if (!ts->u.derived)
3577 m = MATCH_ERROR;
3580 /* Only permit one go at the characteristic association. */
3581 if (ts->kind == -1)
3582 ts->kind = 0;
3584 /* Set the function locus correctly. If we have not found the
3585 function name, there is an error. */
3586 if (m == MATCH_YES
3587 && gfc_match ("function% %n", name) == MATCH_YES
3588 && strcmp (name, gfc_current_block ()->name) == 0)
3590 gfc_current_block ()->declared_at = gfc_current_locus;
3591 gfc_commit_symbols ();
3593 else
3595 gfc_error_check ();
3596 gfc_undo_symbols ();
3599 gfc_current_locus =loc;
3600 return m;
3604 /* Check specification-expressions in the function result of the currently
3605 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3606 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3607 scope are not yet parsed so this has to be delayed up to parse_spec. */
3609 static void
3610 check_function_result_typed (void)
3612 gfc_typespec ts;
3614 gcc_assert (gfc_current_state () == COMP_FUNCTION);
3616 if (!gfc_current_ns->proc_name->result) return;
3618 ts = gfc_current_ns->proc_name->result->ts;
3620 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3621 /* TODO: Extend when KIND type parameters are implemented. */
3622 if (ts.type == BT_CHARACTER && ts.u.cl && ts.u.cl->length)
3623 gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true);
3627 /* Parse a set of specification statements. Returns the statement
3628 that doesn't fit. */
3630 static gfc_statement
3631 parse_spec (gfc_statement st)
3633 st_state ss;
3634 bool function_result_typed = false;
3635 bool bad_characteristic = false;
3636 gfc_typespec *ts;
3638 in_specification_block = true;
3640 verify_st_order (&ss, ST_NONE, false);
3641 if (st == ST_NONE)
3642 st = next_statement ();
3644 /* If we are not inside a function or don't have a result specified so far,
3645 do nothing special about it. */
3646 if (gfc_current_state () != COMP_FUNCTION)
3647 function_result_typed = true;
3648 else
3650 gfc_symbol* proc = gfc_current_ns->proc_name;
3651 gcc_assert (proc);
3653 if (proc->result->ts.type == BT_UNKNOWN)
3654 function_result_typed = true;
3657 loop:
3659 /* If we're inside a BLOCK construct, some statements are disallowed.
3660 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3661 or VALUE are also disallowed, but they don't have a particular ST_*
3662 key so we have to check for them individually in their matcher routine. */
3663 if (gfc_current_state () == COMP_BLOCK)
3664 switch (st)
3666 case ST_IMPLICIT:
3667 case ST_IMPLICIT_NONE:
3668 case ST_NAMELIST:
3669 case ST_COMMON:
3670 case ST_EQUIVALENCE:
3671 case ST_STATEMENT_FUNCTION:
3672 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3673 gfc_ascii_statement (st));
3674 reject_statement ();
3675 break;
3677 default:
3678 break;
3680 else if (gfc_current_state () == COMP_BLOCK_DATA)
3681 /* Fortran 2008, C1116. */
3682 switch (st)
3684 case ST_ATTR_DECL:
3685 case ST_COMMON:
3686 case ST_DATA:
3687 case ST_DATA_DECL:
3688 case ST_DERIVED_DECL:
3689 case ST_END_BLOCK_DATA:
3690 case ST_EQUIVALENCE:
3691 case ST_IMPLICIT:
3692 case ST_IMPLICIT_NONE:
3693 case ST_PARAMETER:
3694 case ST_STRUCTURE_DECL:
3695 case ST_TYPE:
3696 case ST_USE:
3697 break;
3699 case ST_NONE:
3700 break;
3702 default:
3703 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3704 gfc_ascii_statement (st));
3705 reject_statement ();
3706 break;
3709 /* If we find a statement that can not be followed by an IMPLICIT statement
3710 (and thus we can expect to see none any further), type the function result
3711 if it has not yet been typed. Be careful not to give the END statement
3712 to verify_st_order! */
3713 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
3715 bool verify_now = false;
3717 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
3718 verify_now = true;
3719 else
3721 st_state dummyss;
3722 verify_st_order (&dummyss, ST_NONE, false);
3723 verify_st_order (&dummyss, st, false);
3725 if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
3726 verify_now = true;
3729 if (verify_now)
3731 check_function_result_typed ();
3732 function_result_typed = true;
3736 switch (st)
3738 case ST_NONE:
3739 unexpected_eof ();
3741 case ST_IMPLICIT_NONE:
3742 case ST_IMPLICIT:
3743 if (!function_result_typed)
3745 check_function_result_typed ();
3746 function_result_typed = true;
3748 goto declSt;
3750 case ST_FORMAT:
3751 case ST_ENTRY:
3752 case ST_DATA: /* Not allowed in interfaces */
3753 if (gfc_current_state () == COMP_INTERFACE)
3754 break;
3756 /* Fall through */
3758 case ST_USE:
3759 case ST_IMPORT:
3760 case ST_PARAMETER:
3761 case ST_PUBLIC:
3762 case ST_PRIVATE:
3763 case ST_STRUCTURE_DECL:
3764 case ST_DERIVED_DECL:
3765 case_decl:
3766 case_omp_decl:
3767 declSt:
3768 if (!verify_st_order (&ss, st, false))
3770 reject_statement ();
3771 st = next_statement ();
3772 goto loop;
3775 switch (st)
3777 case ST_INTERFACE:
3778 parse_interface ();
3779 break;
3781 case ST_STRUCTURE_DECL:
3782 parse_struct_map (ST_STRUCTURE_DECL);
3783 break;
3785 case ST_DERIVED_DECL:
3786 parse_derived ();
3787 break;
3789 case ST_PUBLIC:
3790 case ST_PRIVATE:
3791 if (gfc_current_state () != COMP_MODULE)
3793 gfc_error ("%s statement must appear in a MODULE",
3794 gfc_ascii_statement (st));
3795 reject_statement ();
3796 break;
3799 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
3801 gfc_error ("%s statement at %C follows another accessibility "
3802 "specification", gfc_ascii_statement (st));
3803 reject_statement ();
3804 break;
3807 gfc_current_ns->default_access = (st == ST_PUBLIC)
3808 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3810 break;
3812 case ST_STATEMENT_FUNCTION:
3813 if (gfc_current_state () == COMP_MODULE
3814 || gfc_current_state () == COMP_SUBMODULE)
3816 unexpected_statement (st);
3817 break;
3820 default:
3821 break;
3824 accept_statement (st);
3825 st = next_statement ();
3826 goto loop;
3828 case ST_ENUM:
3829 accept_statement (st);
3830 parse_enum();
3831 st = next_statement ();
3832 goto loop;
3834 case ST_GET_FCN_CHARACTERISTICS:
3835 /* This statement triggers the association of a function's result
3836 characteristics. */
3837 ts = &gfc_current_block ()->result->ts;
3838 if (match_deferred_characteristics (ts) != MATCH_YES)
3839 bad_characteristic = true;
3841 st = next_statement ();
3842 goto loop;
3844 default:
3845 break;
3848 /* If match_deferred_characteristics failed, then there is an error. */
3849 if (bad_characteristic)
3851 ts = &gfc_current_block ()->result->ts;
3852 if (ts->type != BT_DERIVED)
3853 gfc_error ("Bad kind expression for function %qs at %L",
3854 gfc_current_block ()->name,
3855 &gfc_current_block ()->declared_at);
3856 else
3857 gfc_error ("The type for function %qs at %L is not accessible",
3858 gfc_current_block ()->name,
3859 &gfc_current_block ()->declared_at);
3861 gfc_current_block ()->ts.kind = 0;
3862 /* Keep the derived type; if it's bad, it will be discovered later. */
3863 if (!(ts->type == BT_DERIVED && ts->u.derived))
3864 ts->type = BT_UNKNOWN;
3867 in_specification_block = false;
3869 return st;
3873 /* Parse a WHERE block, (not a simple WHERE statement). */
3875 static void
3876 parse_where_block (void)
3878 int seen_empty_else;
3879 gfc_code *top, *d;
3880 gfc_state_data s;
3881 gfc_statement st;
3883 accept_statement (ST_WHERE_BLOCK);
3884 top = gfc_state_stack->tail;
3886 push_state (&s, COMP_WHERE, gfc_new_block);
3888 d = add_statement ();
3889 d->expr1 = top->expr1;
3890 d->op = EXEC_WHERE;
3892 top->expr1 = NULL;
3893 top->block = d;
3895 seen_empty_else = 0;
3899 st = next_statement ();
3900 switch (st)
3902 case ST_NONE:
3903 unexpected_eof ();
3905 case ST_WHERE_BLOCK:
3906 parse_where_block ();
3907 break;
3909 case ST_ASSIGNMENT:
3910 case ST_WHERE:
3911 accept_statement (st);
3912 break;
3914 case ST_ELSEWHERE:
3915 if (seen_empty_else)
3917 gfc_error ("ELSEWHERE statement at %C follows previous "
3918 "unmasked ELSEWHERE");
3919 reject_statement ();
3920 break;
3923 if (new_st.expr1 == NULL)
3924 seen_empty_else = 1;
3926 d = new_level (gfc_state_stack->head);
3927 d->op = EXEC_WHERE;
3928 d->expr1 = new_st.expr1;
3930 accept_statement (st);
3932 break;
3934 case ST_END_WHERE:
3935 accept_statement (st);
3936 break;
3938 default:
3939 gfc_error ("Unexpected %s statement in WHERE block at %C",
3940 gfc_ascii_statement (st));
3941 reject_statement ();
3942 break;
3945 while (st != ST_END_WHERE);
3947 pop_state ();
3951 /* Parse a FORALL block (not a simple FORALL statement). */
3953 static void
3954 parse_forall_block (void)
3956 gfc_code *top, *d;
3957 gfc_state_data s;
3958 gfc_statement st;
3960 accept_statement (ST_FORALL_BLOCK);
3961 top = gfc_state_stack->tail;
3963 push_state (&s, COMP_FORALL, gfc_new_block);
3965 d = add_statement ();
3966 d->op = EXEC_FORALL;
3967 top->block = d;
3971 st = next_statement ();
3972 switch (st)
3975 case ST_ASSIGNMENT:
3976 case ST_POINTER_ASSIGNMENT:
3977 case ST_WHERE:
3978 case ST_FORALL:
3979 accept_statement (st);
3980 break;
3982 case ST_WHERE_BLOCK:
3983 parse_where_block ();
3984 break;
3986 case ST_FORALL_BLOCK:
3987 parse_forall_block ();
3988 break;
3990 case ST_END_FORALL:
3991 accept_statement (st);
3992 break;
3994 case ST_NONE:
3995 unexpected_eof ();
3997 default:
3998 gfc_error ("Unexpected %s statement in FORALL block at %C",
3999 gfc_ascii_statement (st));
4001 reject_statement ();
4002 break;
4005 while (st != ST_END_FORALL);
4007 pop_state ();
4011 static gfc_statement parse_executable (gfc_statement);
4013 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
4015 static void
4016 parse_if_block (void)
4018 gfc_code *top, *d;
4019 gfc_statement st;
4020 locus else_locus;
4021 gfc_state_data s;
4022 int seen_else;
4024 seen_else = 0;
4025 accept_statement (ST_IF_BLOCK);
4027 top = gfc_state_stack->tail;
4028 push_state (&s, COMP_IF, gfc_new_block);
4030 new_st.op = EXEC_IF;
4031 d = add_statement ();
4033 d->expr1 = top->expr1;
4034 top->expr1 = NULL;
4035 top->block = d;
4039 st = parse_executable (ST_NONE);
4041 switch (st)
4043 case ST_NONE:
4044 unexpected_eof ();
4046 case ST_ELSEIF:
4047 if (seen_else)
4049 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
4050 "statement at %L", &else_locus);
4052 reject_statement ();
4053 break;
4056 d = new_level (gfc_state_stack->head);
4057 d->op = EXEC_IF;
4058 d->expr1 = new_st.expr1;
4060 accept_statement (st);
4062 break;
4064 case ST_ELSE:
4065 if (seen_else)
4067 gfc_error ("Duplicate ELSE statements at %L and %C",
4068 &else_locus);
4069 reject_statement ();
4070 break;
4073 seen_else = 1;
4074 else_locus = gfc_current_locus;
4076 d = new_level (gfc_state_stack->head);
4077 d->op = EXEC_IF;
4079 accept_statement (st);
4081 break;
4083 case ST_ENDIF:
4084 break;
4086 default:
4087 unexpected_statement (st);
4088 break;
4091 while (st != ST_ENDIF);
4093 pop_state ();
4094 accept_statement (st);
4098 /* Parse a SELECT block. */
4100 static void
4101 parse_select_block (void)
4103 gfc_statement st;
4104 gfc_code *cp;
4105 gfc_state_data s;
4107 accept_statement (ST_SELECT_CASE);
4109 cp = gfc_state_stack->tail;
4110 push_state (&s, COMP_SELECT, gfc_new_block);
4112 /* Make sure that the next statement is a CASE or END SELECT. */
4113 for (;;)
4115 st = next_statement ();
4116 if (st == ST_NONE)
4117 unexpected_eof ();
4118 if (st == ST_END_SELECT)
4120 /* Empty SELECT CASE is OK. */
4121 accept_statement (st);
4122 pop_state ();
4123 return;
4125 if (st == ST_CASE)
4126 break;
4128 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
4129 "CASE at %C");
4131 reject_statement ();
4134 /* At this point, we're got a nonempty select block. */
4135 cp = new_level (cp);
4136 *cp = new_st;
4138 accept_statement (st);
4142 st = parse_executable (ST_NONE);
4143 switch (st)
4145 case ST_NONE:
4146 unexpected_eof ();
4148 case ST_CASE:
4149 cp = new_level (gfc_state_stack->head);
4150 *cp = new_st;
4151 gfc_clear_new_st ();
4153 accept_statement (st);
4154 /* Fall through */
4156 case ST_END_SELECT:
4157 break;
4159 /* Can't have an executable statement because of
4160 parse_executable(). */
4161 default:
4162 unexpected_statement (st);
4163 break;
4166 while (st != ST_END_SELECT);
4168 pop_state ();
4169 accept_statement (st);
4173 /* Pop the current selector from the SELECT TYPE stack. */
4175 static void
4176 select_type_pop (void)
4178 gfc_select_type_stack *old = select_type_stack;
4179 select_type_stack = old->prev;
4180 free (old);
4184 /* Parse a SELECT TYPE construct (F03:R821). */
4186 static void
4187 parse_select_type_block (void)
4189 gfc_statement st;
4190 gfc_code *cp;
4191 gfc_state_data s;
4193 gfc_current_ns = new_st.ext.block.ns;
4194 accept_statement (ST_SELECT_TYPE);
4196 cp = gfc_state_stack->tail;
4197 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
4199 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
4200 or END SELECT. */
4201 for (;;)
4203 st = next_statement ();
4204 if (st == ST_NONE)
4205 unexpected_eof ();
4206 if (st == ST_END_SELECT)
4207 /* Empty SELECT CASE is OK. */
4208 goto done;
4209 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
4210 break;
4212 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
4213 "following SELECT TYPE at %C");
4215 reject_statement ();
4218 /* At this point, we're got a nonempty select block. */
4219 cp = new_level (cp);
4220 *cp = new_st;
4222 accept_statement (st);
4226 st = parse_executable (ST_NONE);
4227 switch (st)
4229 case ST_NONE:
4230 unexpected_eof ();
4232 case ST_TYPE_IS:
4233 case ST_CLASS_IS:
4234 cp = new_level (gfc_state_stack->head);
4235 *cp = new_st;
4236 gfc_clear_new_st ();
4238 accept_statement (st);
4239 /* Fall through */
4241 case ST_END_SELECT:
4242 break;
4244 /* Can't have an executable statement because of
4245 parse_executable(). */
4246 default:
4247 unexpected_statement (st);
4248 break;
4251 while (st != ST_END_SELECT);
4253 done:
4254 pop_state ();
4255 accept_statement (st);
4256 gfc_current_ns = gfc_current_ns->parent;
4257 select_type_pop ();
4261 /* Given a symbol, make sure it is not an iteration variable for a DO
4262 statement. This subroutine is called when the symbol is seen in a
4263 context that causes it to become redefined. If the symbol is an
4264 iterator, we generate an error message and return nonzero. */
4267 gfc_check_do_variable (gfc_symtree *st)
4269 gfc_state_data *s;
4271 for (s=gfc_state_stack; s; s = s->previous)
4272 if (s->do_variable == st)
4274 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
4275 "loop beginning at %L", st->name, &s->head->loc);
4276 return 1;
4279 return 0;
4283 /* Checks to see if the current statement label closes an enddo.
4284 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
4285 an error) if it incorrectly closes an ENDDO. */
4287 static int
4288 check_do_closure (void)
4290 gfc_state_data *p;
4292 if (gfc_statement_label == NULL)
4293 return 0;
4295 for (p = gfc_state_stack; p; p = p->previous)
4296 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
4297 break;
4299 if (p == NULL)
4300 return 0; /* No loops to close */
4302 if (p->ext.end_do_label == gfc_statement_label)
4304 if (p == gfc_state_stack)
4305 return 1;
4307 gfc_error ("End of nonblock DO statement at %C is within another block");
4308 return 2;
4311 /* At this point, the label doesn't terminate the innermost loop.
4312 Make sure it doesn't terminate another one. */
4313 for (; p; p = p->previous)
4314 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
4315 && p->ext.end_do_label == gfc_statement_label)
4317 gfc_error ("End of nonblock DO statement at %C is interwoven "
4318 "with another DO loop");
4319 return 2;
4322 return 0;
4326 /* Parse a series of contained program units. */
4328 static void parse_progunit (gfc_statement);
4331 /* Parse a CRITICAL block. */
4333 static void
4334 parse_critical_block (void)
4336 gfc_code *top, *d;
4337 gfc_state_data s, *sd;
4338 gfc_statement st;
4340 for (sd = gfc_state_stack; sd; sd = sd->previous)
4341 if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
4342 gfc_error_now (is_oacc (sd)
4343 ? "CRITICAL block inside of OpenACC region at %C"
4344 : "CRITICAL block inside of OpenMP region at %C");
4346 s.ext.end_do_label = new_st.label1;
4348 accept_statement (ST_CRITICAL);
4349 top = gfc_state_stack->tail;
4351 push_state (&s, COMP_CRITICAL, gfc_new_block);
4353 d = add_statement ();
4354 d->op = EXEC_CRITICAL;
4355 top->block = d;
4359 st = parse_executable (ST_NONE);
4361 switch (st)
4363 case ST_NONE:
4364 unexpected_eof ();
4365 break;
4367 case ST_END_CRITICAL:
4368 if (s.ext.end_do_label != NULL
4369 && s.ext.end_do_label != gfc_statement_label)
4370 gfc_error_now ("Statement label in END CRITICAL at %C does not "
4371 "match CRITICAL label");
4373 if (gfc_statement_label != NULL)
4375 new_st.op = EXEC_NOP;
4376 add_statement ();
4378 break;
4380 default:
4381 unexpected_statement (st);
4382 break;
4385 while (st != ST_END_CRITICAL);
4387 pop_state ();
4388 accept_statement (st);
4392 /* Set up the local namespace for a BLOCK construct. */
4394 gfc_namespace*
4395 gfc_build_block_ns (gfc_namespace *parent_ns)
4397 gfc_namespace* my_ns;
4398 static int numblock = 1;
4400 my_ns = gfc_get_namespace (parent_ns, 1);
4401 my_ns->construct_entities = 1;
4403 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
4404 code generation (so it must not be NULL).
4405 We set its recursive argument if our container procedure is recursive, so
4406 that local variables are accordingly placed on the stack when it
4407 will be necessary. */
4408 if (gfc_new_block)
4409 my_ns->proc_name = gfc_new_block;
4410 else
4412 bool t;
4413 char buffer[20]; /* Enough to hold "block@2147483648\n". */
4415 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
4416 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
4417 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
4418 my_ns->proc_name->name, NULL);
4419 gcc_assert (t);
4420 gfc_commit_symbol (my_ns->proc_name);
4423 if (parent_ns->proc_name)
4424 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
4426 return my_ns;
4430 /* Parse a BLOCK construct. */
4432 static void
4433 parse_block_construct (void)
4435 gfc_namespace* my_ns;
4436 gfc_namespace* my_parent;
4437 gfc_state_data s;
4439 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
4441 my_ns = gfc_build_block_ns (gfc_current_ns);
4443 new_st.op = EXEC_BLOCK;
4444 new_st.ext.block.ns = my_ns;
4445 new_st.ext.block.assoc = NULL;
4446 accept_statement (ST_BLOCK);
4448 push_state (&s, COMP_BLOCK, my_ns->proc_name);
4449 gfc_current_ns = my_ns;
4450 my_parent = my_ns->parent;
4452 parse_progunit (ST_NONE);
4454 /* Don't depend on the value of gfc_current_ns; it might have been
4455 reset if the block had errors and was cleaned up. */
4456 gfc_current_ns = my_parent;
4458 pop_state ();
4462 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
4463 behind the scenes with compiler-generated variables. */
4465 static void
4466 parse_associate (void)
4468 gfc_namespace* my_ns;
4469 gfc_state_data s;
4470 gfc_statement st;
4471 gfc_association_list* a;
4473 gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
4475 my_ns = gfc_build_block_ns (gfc_current_ns);
4477 new_st.op = EXEC_BLOCK;
4478 new_st.ext.block.ns = my_ns;
4479 gcc_assert (new_st.ext.block.assoc);
4481 /* Add all associate-names as BLOCK variables. Creating them is enough
4482 for now, they'll get their values during trans-* phase. */
4483 gfc_current_ns = my_ns;
4484 for (a = new_st.ext.block.assoc; a; a = a->next)
4486 gfc_symbol* sym;
4487 gfc_ref *ref;
4488 gfc_array_ref *array_ref;
4490 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
4491 gcc_unreachable ();
4493 sym = a->st->n.sym;
4494 sym->attr.flavor = FL_VARIABLE;
4495 sym->assoc = a;
4496 sym->declared_at = a->where;
4497 gfc_set_sym_referenced (sym);
4499 /* Initialize the typespec. It is not available in all cases,
4500 however, as it may only be set on the target during resolution.
4501 Still, sometimes it helps to have it right now -- especially
4502 for parsing component references on the associate-name
4503 in case of association to a derived-type. */
4504 sym->ts = a->target->ts;
4506 /* Check if the target expression is array valued. This can not always
4507 be done by looking at target.rank, because that might not have been
4508 set yet. Therefore traverse the chain of refs, looking for the last
4509 array ref and evaluate that. */
4510 array_ref = NULL;
4511 for (ref = a->target->ref; ref; ref = ref->next)
4512 if (ref->type == REF_ARRAY)
4513 array_ref = &ref->u.ar;
4514 if (array_ref || a->target->rank)
4516 gfc_array_spec *as;
4517 int dim, rank = 0;
4518 if (array_ref)
4520 a->rankguessed = 1;
4521 /* Count the dimension, that have a non-scalar extend. */
4522 for (dim = 0; dim < array_ref->dimen; ++dim)
4523 if (array_ref->dimen_type[dim] != DIMEN_ELEMENT
4524 && !(array_ref->dimen_type[dim] == DIMEN_UNKNOWN
4525 && array_ref->end[dim] == NULL
4526 && array_ref->start[dim] != NULL))
4527 ++rank;
4529 else
4530 rank = a->target->rank;
4531 /* When the rank is greater than zero then sym will be an array. */
4532 if (sym->ts.type == BT_CLASS)
4534 if ((!CLASS_DATA (sym)->as && rank != 0)
4535 || (CLASS_DATA (sym)->as
4536 && CLASS_DATA (sym)->as->rank != rank))
4538 /* Don't just (re-)set the attr and as in the sym.ts,
4539 because this modifies the target's attr and as. Copy the
4540 data and do a build_class_symbol. */
4541 symbol_attribute attr = CLASS_DATA (a->target)->attr;
4542 int corank = gfc_get_corank (a->target);
4543 gfc_typespec type;
4545 if (rank || corank)
4547 as = gfc_get_array_spec ();
4548 as->type = AS_DEFERRED;
4549 as->rank = rank;
4550 as->corank = corank;
4551 attr.dimension = rank ? 1 : 0;
4552 attr.codimension = corank ? 1 : 0;
4554 else
4556 as = NULL;
4557 attr.dimension = attr.codimension = 0;
4559 attr.class_ok = 0;
4560 type = CLASS_DATA (sym)->ts;
4561 if (!gfc_build_class_symbol (&type,
4562 &attr, &as))
4563 gcc_unreachable ();
4564 sym->ts = type;
4565 sym->ts.type = BT_CLASS;
4566 sym->attr.class_ok = 1;
4568 else
4569 sym->attr.class_ok = 1;
4571 else if ((!sym->as && rank != 0)
4572 || (sym->as && sym->as->rank != rank))
4574 as = gfc_get_array_spec ();
4575 as->type = AS_DEFERRED;
4576 as->rank = rank;
4577 as->corank = gfc_get_corank (a->target);
4578 sym->as = as;
4579 sym->attr.dimension = 1;
4580 if (as->corank)
4581 sym->attr.codimension = 1;
4586 accept_statement (ST_ASSOCIATE);
4587 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
4589 loop:
4590 st = parse_executable (ST_NONE);
4591 switch (st)
4593 case ST_NONE:
4594 unexpected_eof ();
4596 case_end:
4597 accept_statement (st);
4598 my_ns->code = gfc_state_stack->head;
4599 break;
4601 default:
4602 unexpected_statement (st);
4603 goto loop;
4606 gfc_current_ns = gfc_current_ns->parent;
4607 pop_state ();
4611 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4612 handled inside of parse_executable(), because they aren't really
4613 loop statements. */
4615 static void
4616 parse_do_block (void)
4618 gfc_statement st;
4619 gfc_code *top;
4620 gfc_state_data s;
4621 gfc_symtree *stree;
4622 gfc_exec_op do_op;
4624 do_op = new_st.op;
4625 s.ext.end_do_label = new_st.label1;
4627 if (new_st.ext.iterator != NULL)
4628 stree = new_st.ext.iterator->var->symtree;
4629 else
4630 stree = NULL;
4632 accept_statement (ST_DO);
4634 top = gfc_state_stack->tail;
4635 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
4636 gfc_new_block);
4638 s.do_variable = stree;
4640 top->block = new_level (top);
4641 top->block->op = EXEC_DO;
4643 loop:
4644 st = parse_executable (ST_NONE);
4646 switch (st)
4648 case ST_NONE:
4649 unexpected_eof ();
4651 case ST_ENDDO:
4652 if (s.ext.end_do_label != NULL
4653 && s.ext.end_do_label != gfc_statement_label)
4654 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4655 "DO label");
4657 if (gfc_statement_label != NULL)
4659 new_st.op = EXEC_NOP;
4660 add_statement ();
4662 break;
4664 case ST_IMPLIED_ENDDO:
4665 /* If the do-stmt of this DO construct has a do-construct-name,
4666 the corresponding end-do must be an end-do-stmt (with a matching
4667 name, but in that case we must have seen ST_ENDDO first).
4668 We only complain about this in pedantic mode. */
4669 if (gfc_current_block () != NULL)
4670 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4671 &gfc_current_block()->declared_at);
4673 break;
4675 default:
4676 unexpected_statement (st);
4677 goto loop;
4680 pop_state ();
4681 accept_statement (st);
4685 /* Parse the statements of OpenMP do/parallel do. */
4687 static gfc_statement
4688 parse_omp_do (gfc_statement omp_st)
4690 gfc_statement st;
4691 gfc_code *cp, *np;
4692 gfc_state_data s;
4694 accept_statement (omp_st);
4696 cp = gfc_state_stack->tail;
4697 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4698 np = new_level (cp);
4699 np->op = cp->op;
4700 np->block = NULL;
4702 for (;;)
4704 st = next_statement ();
4705 if (st == ST_NONE)
4706 unexpected_eof ();
4707 else if (st == ST_DO)
4708 break;
4709 else
4710 unexpected_statement (st);
4713 parse_do_block ();
4714 if (gfc_statement_label != NULL
4715 && gfc_state_stack->previous != NULL
4716 && gfc_state_stack->previous->state == COMP_DO
4717 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4719 /* In
4720 DO 100 I=1,10
4721 !$OMP DO
4722 DO J=1,10
4724 100 CONTINUE
4725 there should be no !$OMP END DO. */
4726 pop_state ();
4727 return ST_IMPLIED_ENDDO;
4730 check_do_closure ();
4731 pop_state ();
4733 st = next_statement ();
4734 gfc_statement omp_end_st = ST_OMP_END_DO;
4735 switch (omp_st)
4737 case ST_OMP_DISTRIBUTE: omp_end_st = ST_OMP_END_DISTRIBUTE; break;
4738 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4739 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4740 break;
4741 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4742 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4743 break;
4744 case ST_OMP_DISTRIBUTE_SIMD:
4745 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4746 break;
4747 case ST_OMP_DO: omp_end_st = ST_OMP_END_DO; break;
4748 case ST_OMP_DO_SIMD: omp_end_st = ST_OMP_END_DO_SIMD; break;
4749 case ST_OMP_PARALLEL_DO: omp_end_st = ST_OMP_END_PARALLEL_DO; break;
4750 case ST_OMP_PARALLEL_DO_SIMD:
4751 omp_end_st = ST_OMP_END_PARALLEL_DO_SIMD;
4752 break;
4753 case ST_OMP_SIMD: omp_end_st = ST_OMP_END_SIMD; break;
4754 case ST_OMP_TARGET_PARALLEL_DO:
4755 omp_end_st = ST_OMP_END_TARGET_PARALLEL_DO;
4756 break;
4757 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
4758 omp_end_st = ST_OMP_END_TARGET_PARALLEL_DO_SIMD;
4759 break;
4760 case ST_OMP_TARGET_SIMD: omp_end_st = ST_OMP_END_TARGET_SIMD; break;
4761 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4762 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4763 break;
4764 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4765 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4766 break;
4767 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4768 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4769 break;
4770 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4771 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4772 break;
4773 case ST_OMP_TASKLOOP: omp_end_st = ST_OMP_END_TASKLOOP; break;
4774 case ST_OMP_TASKLOOP_SIMD: omp_end_st = ST_OMP_END_TASKLOOP_SIMD; break;
4775 case ST_OMP_TEAMS_DISTRIBUTE:
4776 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4777 break;
4778 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4779 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4780 break;
4781 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4782 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4783 break;
4784 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4785 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4786 break;
4787 default: gcc_unreachable ();
4789 if (st == omp_end_st)
4791 if (new_st.op == EXEC_OMP_END_NOWAIT)
4792 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4793 else
4794 gcc_assert (new_st.op == EXEC_NOP);
4795 gfc_clear_new_st ();
4796 gfc_commit_symbols ();
4797 gfc_warning_check ();
4798 st = next_statement ();
4800 return st;
4804 /* Parse the statements of OpenMP atomic directive. */
4806 static gfc_statement
4807 parse_omp_oacc_atomic (bool omp_p)
4809 gfc_statement st, st_atomic, st_end_atomic;
4810 gfc_code *cp, *np;
4811 gfc_state_data s;
4812 int count;
4814 if (omp_p)
4816 st_atomic = ST_OMP_ATOMIC;
4817 st_end_atomic = ST_OMP_END_ATOMIC;
4819 else
4821 st_atomic = ST_OACC_ATOMIC;
4822 st_end_atomic = ST_OACC_END_ATOMIC;
4824 accept_statement (st_atomic);
4826 cp = gfc_state_stack->tail;
4827 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4828 np = new_level (cp);
4829 np->op = cp->op;
4830 np->block = NULL;
4831 np->ext.omp_atomic = cp->ext.omp_atomic;
4832 count = 1 + ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4833 == GFC_OMP_ATOMIC_CAPTURE);
4835 while (count)
4837 st = next_statement ();
4838 if (st == ST_NONE)
4839 unexpected_eof ();
4840 else if (st == ST_ASSIGNMENT)
4842 accept_statement (st);
4843 count--;
4845 else
4846 unexpected_statement (st);
4849 pop_state ();
4851 st = next_statement ();
4852 if (st == st_end_atomic)
4854 gfc_clear_new_st ();
4855 gfc_commit_symbols ();
4856 gfc_warning_check ();
4857 st = next_statement ();
4859 else if ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4860 == GFC_OMP_ATOMIC_CAPTURE)
4861 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4862 return st;
4866 /* Parse the statements of an OpenACC structured block. */
4868 static void
4869 parse_oacc_structured_block (gfc_statement acc_st)
4871 gfc_statement st, acc_end_st;
4872 gfc_code *cp, *np;
4873 gfc_state_data s, *sd;
4875 for (sd = gfc_state_stack; sd; sd = sd->previous)
4876 if (sd->state == COMP_CRITICAL)
4877 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4879 accept_statement (acc_st);
4881 cp = gfc_state_stack->tail;
4882 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4883 np = new_level (cp);
4884 np->op = cp->op;
4885 np->block = NULL;
4886 switch (acc_st)
4888 case ST_OACC_PARALLEL:
4889 acc_end_st = ST_OACC_END_PARALLEL;
4890 break;
4891 case ST_OACC_KERNELS:
4892 acc_end_st = ST_OACC_END_KERNELS;
4893 break;
4894 case ST_OACC_DATA:
4895 acc_end_st = ST_OACC_END_DATA;
4896 break;
4897 case ST_OACC_HOST_DATA:
4898 acc_end_st = ST_OACC_END_HOST_DATA;
4899 break;
4900 default:
4901 gcc_unreachable ();
4906 st = parse_executable (ST_NONE);
4907 if (st == ST_NONE)
4908 unexpected_eof ();
4909 else if (st != acc_end_st)
4911 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
4912 reject_statement ();
4915 while (st != acc_end_st);
4917 gcc_assert (new_st.op == EXEC_NOP);
4919 gfc_clear_new_st ();
4920 gfc_commit_symbols ();
4921 gfc_warning_check ();
4922 pop_state ();
4925 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4927 static gfc_statement
4928 parse_oacc_loop (gfc_statement acc_st)
4930 gfc_statement st;
4931 gfc_code *cp, *np;
4932 gfc_state_data s, *sd;
4934 for (sd = gfc_state_stack; sd; sd = sd->previous)
4935 if (sd->state == COMP_CRITICAL)
4936 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4938 accept_statement (acc_st);
4940 cp = gfc_state_stack->tail;
4941 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4942 np = new_level (cp);
4943 np->op = cp->op;
4944 np->block = NULL;
4946 for (;;)
4948 st = next_statement ();
4949 if (st == ST_NONE)
4950 unexpected_eof ();
4951 else if (st == ST_DO)
4952 break;
4953 else
4955 gfc_error ("Expected DO loop at %C");
4956 reject_statement ();
4960 parse_do_block ();
4961 if (gfc_statement_label != NULL
4962 && gfc_state_stack->previous != NULL
4963 && gfc_state_stack->previous->state == COMP_DO
4964 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4966 pop_state ();
4967 return ST_IMPLIED_ENDDO;
4970 check_do_closure ();
4971 pop_state ();
4973 st = next_statement ();
4974 if (st == ST_OACC_END_LOOP)
4975 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
4976 if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
4977 (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
4978 (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
4980 gcc_assert (new_st.op == EXEC_NOP);
4981 gfc_clear_new_st ();
4982 gfc_commit_symbols ();
4983 gfc_warning_check ();
4984 st = next_statement ();
4986 return st;
4990 /* Parse the statements of an OpenMP structured block. */
4992 static void
4993 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
4995 gfc_statement st, omp_end_st;
4996 gfc_code *cp, *np;
4997 gfc_state_data s;
4999 accept_statement (omp_st);
5001 cp = gfc_state_stack->tail;
5002 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
5003 np = new_level (cp);
5004 np->op = cp->op;
5005 np->block = NULL;
5007 switch (omp_st)
5009 case ST_OMP_PARALLEL:
5010 omp_end_st = ST_OMP_END_PARALLEL;
5011 break;
5012 case ST_OMP_PARALLEL_SECTIONS:
5013 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
5014 break;
5015 case ST_OMP_SECTIONS:
5016 omp_end_st = ST_OMP_END_SECTIONS;
5017 break;
5018 case ST_OMP_ORDERED:
5019 omp_end_st = ST_OMP_END_ORDERED;
5020 break;
5021 case ST_OMP_CRITICAL:
5022 omp_end_st = ST_OMP_END_CRITICAL;
5023 break;
5024 case ST_OMP_MASTER:
5025 omp_end_st = ST_OMP_END_MASTER;
5026 break;
5027 case ST_OMP_SINGLE:
5028 omp_end_st = ST_OMP_END_SINGLE;
5029 break;
5030 case ST_OMP_TARGET:
5031 omp_end_st = ST_OMP_END_TARGET;
5032 break;
5033 case ST_OMP_TARGET_DATA:
5034 omp_end_st = ST_OMP_END_TARGET_DATA;
5035 break;
5036 case ST_OMP_TARGET_TEAMS:
5037 omp_end_st = ST_OMP_END_TARGET_TEAMS;
5038 break;
5039 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
5040 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
5041 break;
5042 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
5043 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
5044 break;
5045 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5046 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
5047 break;
5048 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
5049 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
5050 break;
5051 case ST_OMP_TASK:
5052 omp_end_st = ST_OMP_END_TASK;
5053 break;
5054 case ST_OMP_TASKGROUP:
5055 omp_end_st = ST_OMP_END_TASKGROUP;
5056 break;
5057 case ST_OMP_TEAMS:
5058 omp_end_st = ST_OMP_END_TEAMS;
5059 break;
5060 case ST_OMP_TEAMS_DISTRIBUTE:
5061 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
5062 break;
5063 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
5064 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
5065 break;
5066 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5067 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
5068 break;
5069 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
5070 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
5071 break;
5072 case ST_OMP_DISTRIBUTE:
5073 omp_end_st = ST_OMP_END_DISTRIBUTE;
5074 break;
5075 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
5076 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
5077 break;
5078 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
5079 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
5080 break;
5081 case ST_OMP_DISTRIBUTE_SIMD:
5082 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
5083 break;
5084 case ST_OMP_WORKSHARE:
5085 omp_end_st = ST_OMP_END_WORKSHARE;
5086 break;
5087 case ST_OMP_PARALLEL_WORKSHARE:
5088 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
5089 break;
5090 default:
5091 gcc_unreachable ();
5096 if (workshare_stmts_only)
5098 /* Inside of !$omp workshare, only
5099 scalar assignments
5100 array assignments
5101 where statements and constructs
5102 forall statements and constructs
5103 !$omp atomic
5104 !$omp critical
5105 !$omp parallel
5106 are allowed. For !$omp critical these
5107 restrictions apply recursively. */
5108 bool cycle = true;
5110 st = next_statement ();
5111 for (;;)
5113 switch (st)
5115 case ST_NONE:
5116 unexpected_eof ();
5118 case ST_ASSIGNMENT:
5119 case ST_WHERE:
5120 case ST_FORALL:
5121 accept_statement (st);
5122 break;
5124 case ST_WHERE_BLOCK:
5125 parse_where_block ();
5126 break;
5128 case ST_FORALL_BLOCK:
5129 parse_forall_block ();
5130 break;
5132 case ST_OMP_PARALLEL:
5133 case ST_OMP_PARALLEL_SECTIONS:
5134 parse_omp_structured_block (st, false);
5135 break;
5137 case ST_OMP_PARALLEL_WORKSHARE:
5138 case ST_OMP_CRITICAL:
5139 parse_omp_structured_block (st, true);
5140 break;
5142 case ST_OMP_PARALLEL_DO:
5143 case ST_OMP_PARALLEL_DO_SIMD:
5144 st = parse_omp_do (st);
5145 continue;
5147 case ST_OMP_ATOMIC:
5148 st = parse_omp_oacc_atomic (true);
5149 continue;
5151 default:
5152 cycle = false;
5153 break;
5156 if (!cycle)
5157 break;
5159 st = next_statement ();
5162 else
5163 st = parse_executable (ST_NONE);
5164 if (st == ST_NONE)
5165 unexpected_eof ();
5166 else if (st == ST_OMP_SECTION
5167 && (omp_st == ST_OMP_SECTIONS
5168 || omp_st == ST_OMP_PARALLEL_SECTIONS))
5170 np = new_level (np);
5171 np->op = cp->op;
5172 np->block = NULL;
5174 else if (st != omp_end_st)
5175 unexpected_statement (st);
5177 while (st != omp_end_st);
5179 switch (new_st.op)
5181 case EXEC_OMP_END_NOWAIT:
5182 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
5183 break;
5184 case EXEC_OMP_END_CRITICAL:
5185 if (((cp->ext.omp_clauses == NULL) ^ (new_st.ext.omp_name == NULL))
5186 || (new_st.ext.omp_name != NULL
5187 && strcmp (cp->ext.omp_clauses->critical_name,
5188 new_st.ext.omp_name) != 0))
5189 gfc_error ("Name after !$omp critical and !$omp end critical does "
5190 "not match at %C");
5191 free (CONST_CAST (char *, new_st.ext.omp_name));
5192 new_st.ext.omp_name = NULL;
5193 break;
5194 case EXEC_OMP_END_SINGLE:
5195 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
5196 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
5197 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
5198 gfc_free_omp_clauses (new_st.ext.omp_clauses);
5199 break;
5200 case EXEC_NOP:
5201 break;
5202 default:
5203 gcc_unreachable ();
5206 gfc_clear_new_st ();
5207 gfc_commit_symbols ();
5208 gfc_warning_check ();
5209 pop_state ();
5213 /* Accept a series of executable statements. We return the first
5214 statement that doesn't fit to the caller. Any block statements are
5215 passed on to the correct handler, which usually passes the buck
5216 right back here. */
5218 static gfc_statement
5219 parse_executable (gfc_statement st)
5221 int close_flag;
5223 if (st == ST_NONE)
5224 st = next_statement ();
5226 for (;;)
5228 close_flag = check_do_closure ();
5229 if (close_flag)
5230 switch (st)
5232 case ST_GOTO:
5233 case ST_END_PROGRAM:
5234 case ST_RETURN:
5235 case ST_EXIT:
5236 case ST_END_FUNCTION:
5237 case ST_CYCLE:
5238 case ST_PAUSE:
5239 case ST_STOP:
5240 case ST_ERROR_STOP:
5241 case ST_END_SUBROUTINE:
5243 case ST_DO:
5244 case ST_FORALL:
5245 case ST_WHERE:
5246 case ST_SELECT_CASE:
5247 gfc_error ("%s statement at %C cannot terminate a non-block "
5248 "DO loop", gfc_ascii_statement (st));
5249 break;
5251 default:
5252 break;
5255 switch (st)
5257 case ST_NONE:
5258 unexpected_eof ();
5260 case ST_DATA:
5261 gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
5262 "first executable statement");
5263 /* Fall through. */
5265 case ST_FORMAT:
5266 case ST_ENTRY:
5267 case_executable:
5268 accept_statement (st);
5269 if (close_flag == 1)
5270 return ST_IMPLIED_ENDDO;
5271 break;
5273 case ST_BLOCK:
5274 parse_block_construct ();
5275 break;
5277 case ST_ASSOCIATE:
5278 parse_associate ();
5279 break;
5281 case ST_IF_BLOCK:
5282 parse_if_block ();
5283 break;
5285 case ST_SELECT_CASE:
5286 parse_select_block ();
5287 break;
5289 case ST_SELECT_TYPE:
5290 parse_select_type_block ();
5291 break;
5293 case ST_DO:
5294 parse_do_block ();
5295 if (check_do_closure () == 1)
5296 return ST_IMPLIED_ENDDO;
5297 break;
5299 case ST_CRITICAL:
5300 parse_critical_block ();
5301 break;
5303 case ST_WHERE_BLOCK:
5304 parse_where_block ();
5305 break;
5307 case ST_FORALL_BLOCK:
5308 parse_forall_block ();
5309 break;
5311 case ST_OACC_PARALLEL_LOOP:
5312 case ST_OACC_KERNELS_LOOP:
5313 case ST_OACC_LOOP:
5314 st = parse_oacc_loop (st);
5315 if (st == ST_IMPLIED_ENDDO)
5316 return st;
5317 continue;
5319 case ST_OACC_PARALLEL:
5320 case ST_OACC_KERNELS:
5321 case ST_OACC_DATA:
5322 case ST_OACC_HOST_DATA:
5323 parse_oacc_structured_block (st);
5324 break;
5326 case ST_OMP_PARALLEL:
5327 case ST_OMP_PARALLEL_SECTIONS:
5328 case ST_OMP_SECTIONS:
5329 case ST_OMP_ORDERED:
5330 case ST_OMP_CRITICAL:
5331 case ST_OMP_MASTER:
5332 case ST_OMP_SINGLE:
5333 case ST_OMP_TARGET:
5334 case ST_OMP_TARGET_DATA:
5335 case ST_OMP_TARGET_PARALLEL:
5336 case ST_OMP_TARGET_TEAMS:
5337 case ST_OMP_TEAMS:
5338 case ST_OMP_TASK:
5339 case ST_OMP_TASKGROUP:
5340 parse_omp_structured_block (st, false);
5341 break;
5343 case ST_OMP_WORKSHARE:
5344 case ST_OMP_PARALLEL_WORKSHARE:
5345 parse_omp_structured_block (st, true);
5346 break;
5348 case ST_OMP_DISTRIBUTE:
5349 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
5350 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
5351 case ST_OMP_DISTRIBUTE_SIMD:
5352 case ST_OMP_DO:
5353 case ST_OMP_DO_SIMD:
5354 case ST_OMP_PARALLEL_DO:
5355 case ST_OMP_PARALLEL_DO_SIMD:
5356 case ST_OMP_SIMD:
5357 case ST_OMP_TARGET_PARALLEL_DO:
5358 case ST_OMP_TARGET_PARALLEL_DO_SIMD:
5359 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
5360 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
5361 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5362 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
5363 case ST_OMP_TASKLOOP:
5364 case ST_OMP_TASKLOOP_SIMD:
5365 case ST_OMP_TEAMS_DISTRIBUTE:
5366 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
5367 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
5368 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
5369 st = parse_omp_do (st);
5370 if (st == ST_IMPLIED_ENDDO)
5371 return st;
5372 continue;
5374 case ST_OACC_ATOMIC:
5375 st = parse_omp_oacc_atomic (false);
5376 continue;
5378 case ST_OMP_ATOMIC:
5379 st = parse_omp_oacc_atomic (true);
5380 continue;
5382 default:
5383 return st;
5386 st = next_statement ();
5391 /* Fix the symbols for sibling functions. These are incorrectly added to
5392 the child namespace as the parser didn't know about this procedure. */
5394 static void
5395 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
5397 gfc_namespace *ns;
5398 gfc_symtree *st;
5399 gfc_symbol *old_sym;
5401 for (ns = siblings; ns; ns = ns->sibling)
5403 st = gfc_find_symtree (ns->sym_root, sym->name);
5405 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
5406 goto fixup_contained;
5408 if ((st->n.sym->attr.flavor == FL_DERIVED
5409 && sym->attr.generic && sym->attr.function)
5410 ||(sym->attr.flavor == FL_DERIVED
5411 && st->n.sym->attr.generic && st->n.sym->attr.function))
5412 goto fixup_contained;
5414 old_sym = st->n.sym;
5415 if (old_sym->ns == ns
5416 && !old_sym->attr.contained
5418 /* By 14.6.1.3, host association should be excluded
5419 for the following. */
5420 && !(old_sym->attr.external
5421 || (old_sym->ts.type != BT_UNKNOWN
5422 && !old_sym->attr.implicit_type)
5423 || old_sym->attr.flavor == FL_PARAMETER
5424 || old_sym->attr.use_assoc
5425 || old_sym->attr.in_common
5426 || old_sym->attr.in_equivalence
5427 || old_sym->attr.data
5428 || old_sym->attr.dummy
5429 || old_sym->attr.result
5430 || old_sym->attr.dimension
5431 || old_sym->attr.allocatable
5432 || old_sym->attr.intrinsic
5433 || old_sym->attr.generic
5434 || old_sym->attr.flavor == FL_NAMELIST
5435 || old_sym->attr.flavor == FL_LABEL
5436 || old_sym->attr.proc == PROC_ST_FUNCTION))
5438 /* Replace it with the symbol from the parent namespace. */
5439 st->n.sym = sym;
5440 sym->refs++;
5442 gfc_release_symbol (old_sym);
5445 fixup_contained:
5446 /* Do the same for any contained procedures. */
5447 gfc_fixup_sibling_symbols (sym, ns->contained);
5451 static void
5452 parse_contained (int module)
5454 gfc_namespace *ns, *parent_ns, *tmp;
5455 gfc_state_data s1, s2;
5456 gfc_statement st;
5457 gfc_symbol *sym;
5458 gfc_entry_list *el;
5459 locus old_loc;
5460 int contains_statements = 0;
5461 int seen_error = 0;
5463 push_state (&s1, COMP_CONTAINS, NULL);
5464 parent_ns = gfc_current_ns;
5468 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
5470 gfc_current_ns->sibling = parent_ns->contained;
5471 parent_ns->contained = gfc_current_ns;
5473 next:
5474 /* Process the next available statement. We come here if we got an error
5475 and rejected the last statement. */
5476 old_loc = gfc_current_locus;
5477 st = next_statement ();
5479 switch (st)
5481 case ST_NONE:
5482 unexpected_eof ();
5484 case ST_FUNCTION:
5485 case ST_SUBROUTINE:
5486 contains_statements = 1;
5487 accept_statement (st);
5489 push_state (&s2,
5490 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
5491 gfc_new_block);
5493 /* For internal procedures, create/update the symbol in the
5494 parent namespace. */
5496 if (!module)
5498 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
5499 gfc_error ("Contained procedure %qs at %C is already "
5500 "ambiguous", gfc_new_block->name);
5501 else
5503 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
5504 sym->name,
5505 &gfc_new_block->declared_at))
5507 if (st == ST_FUNCTION)
5508 gfc_add_function (&sym->attr, sym->name,
5509 &gfc_new_block->declared_at);
5510 else
5511 gfc_add_subroutine (&sym->attr, sym->name,
5512 &gfc_new_block->declared_at);
5516 gfc_commit_symbols ();
5518 else
5519 sym = gfc_new_block;
5521 /* Mark this as a contained function, so it isn't replaced
5522 by other module functions. */
5523 sym->attr.contained = 1;
5525 /* Set implicit_pure so that it can be reset if any of the
5526 tests for purity fail. This is used for some optimisation
5527 during translation. */
5528 if (!sym->attr.pure)
5529 sym->attr.implicit_pure = 1;
5531 parse_progunit (ST_NONE);
5533 /* Fix up any sibling functions that refer to this one. */
5534 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
5535 /* Or refer to any of its alternate entry points. */
5536 for (el = gfc_current_ns->entries; el; el = el->next)
5537 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
5539 gfc_current_ns->code = s2.head;
5540 gfc_current_ns = parent_ns;
5542 pop_state ();
5543 break;
5545 /* These statements are associated with the end of the host unit. */
5546 case ST_END_FUNCTION:
5547 case ST_END_MODULE:
5548 case ST_END_SUBMODULE:
5549 case ST_END_PROGRAM:
5550 case ST_END_SUBROUTINE:
5551 accept_statement (st);
5552 gfc_current_ns->code = s1.head;
5553 break;
5555 default:
5556 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
5557 gfc_ascii_statement (st));
5558 reject_statement ();
5559 seen_error = 1;
5560 goto next;
5561 break;
5564 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
5565 && st != ST_END_MODULE && st != ST_END_SUBMODULE
5566 && st != ST_END_PROGRAM);
5568 /* The first namespace in the list is guaranteed to not have
5569 anything (worthwhile) in it. */
5570 tmp = gfc_current_ns;
5571 gfc_current_ns = parent_ns;
5572 if (seen_error && tmp->refs > 1)
5573 gfc_free_namespace (tmp);
5575 ns = gfc_current_ns->contained;
5576 gfc_current_ns->contained = ns->sibling;
5577 gfc_free_namespace (ns);
5579 pop_state ();
5580 if (!contains_statements)
5581 gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
5582 "FUNCTION or SUBROUTINE statement at %L", &old_loc);
5586 /* The result variable in a MODULE PROCEDURE needs to be created and
5587 its characteristics copied from the interface since it is neither
5588 declared in the procedure declaration nor in the specification
5589 part. */
5591 static void
5592 get_modproc_result (void)
5594 gfc_symbol *proc;
5595 if (gfc_state_stack->previous
5596 && gfc_state_stack->previous->state == COMP_CONTAINS
5597 && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
5599 proc = gfc_current_ns->proc_name ? gfc_current_ns->proc_name : NULL;
5600 if (proc != NULL
5601 && proc->attr.function
5602 && proc->tlink
5603 && proc->tlink->result
5604 && proc->tlink->result != proc->tlink)
5606 gfc_copy_dummy_sym (&proc->result, proc->tlink->result, 1);
5607 gfc_set_sym_referenced (proc->result);
5608 proc->result->attr.if_source = IFSRC_DECL;
5609 gfc_commit_symbol (proc->result);
5615 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
5617 static void
5618 parse_progunit (gfc_statement st)
5620 gfc_state_data *p;
5621 int n;
5623 if (gfc_new_block
5624 && gfc_new_block->abr_modproc_decl
5625 && gfc_new_block->attr.function)
5626 get_modproc_result ();
5628 st = parse_spec (st);
5629 switch (st)
5631 case ST_NONE:
5632 unexpected_eof ();
5634 case ST_CONTAINS:
5635 /* This is not allowed within BLOCK! */
5636 if (gfc_current_state () != COMP_BLOCK)
5637 goto contains;
5638 break;
5640 case_end:
5641 accept_statement (st);
5642 goto done;
5644 default:
5645 break;
5648 if (gfc_current_state () == COMP_FUNCTION)
5649 gfc_check_function_type (gfc_current_ns);
5651 loop:
5652 for (;;)
5654 st = parse_executable (st);
5656 switch (st)
5658 case ST_NONE:
5659 unexpected_eof ();
5661 case ST_CONTAINS:
5662 /* This is not allowed within BLOCK! */
5663 if (gfc_current_state () != COMP_BLOCK)
5664 goto contains;
5665 break;
5667 case_end:
5668 accept_statement (st);
5669 goto done;
5671 default:
5672 break;
5675 unexpected_statement (st);
5676 reject_statement ();
5677 st = next_statement ();
5680 contains:
5681 n = 0;
5683 for (p = gfc_state_stack; p; p = p->previous)
5684 if (p->state == COMP_CONTAINS)
5685 n++;
5687 if (gfc_find_state (COMP_MODULE) == true
5688 || gfc_find_state (COMP_SUBMODULE) == true)
5689 n--;
5691 if (n > 0)
5693 gfc_error ("CONTAINS statement at %C is already in a contained "
5694 "program unit");
5695 reject_statement ();
5696 st = next_statement ();
5697 goto loop;
5700 parse_contained (0);
5702 done:
5703 gfc_current_ns->code = gfc_state_stack->head;
5707 /* Come here to complain about a global symbol already in use as
5708 something else. */
5710 void
5711 gfc_global_used (gfc_gsymbol *sym, locus *where)
5713 const char *name;
5715 if (where == NULL)
5716 where = &gfc_current_locus;
5718 switch(sym->type)
5720 case GSYM_PROGRAM:
5721 name = "PROGRAM";
5722 break;
5723 case GSYM_FUNCTION:
5724 name = "FUNCTION";
5725 break;
5726 case GSYM_SUBROUTINE:
5727 name = "SUBROUTINE";
5728 break;
5729 case GSYM_COMMON:
5730 name = "COMMON";
5731 break;
5732 case GSYM_BLOCK_DATA:
5733 name = "BLOCK DATA";
5734 break;
5735 case GSYM_MODULE:
5736 name = "MODULE";
5737 break;
5738 default:
5739 gfc_internal_error ("gfc_global_used(): Bad type");
5740 name = NULL;
5743 if (sym->binding_label)
5744 gfc_error ("Global binding name %qs at %L is already being used as a %s "
5745 "at %L", sym->binding_label, where, name, &sym->where);
5746 else
5747 gfc_error ("Global name %qs at %L is already being used as a %s at %L",
5748 sym->name, where, name, &sym->where);
5752 /* Parse a block data program unit. */
5754 static void
5755 parse_block_data (void)
5757 gfc_statement st;
5758 static locus blank_locus;
5759 static int blank_block=0;
5760 gfc_gsymbol *s;
5762 gfc_current_ns->proc_name = gfc_new_block;
5763 gfc_current_ns->is_block_data = 1;
5765 if (gfc_new_block == NULL)
5767 if (blank_block)
5768 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5769 "prior BLOCK DATA at %L", &blank_locus);
5770 else
5772 blank_block = 1;
5773 blank_locus = gfc_current_locus;
5776 else
5778 s = gfc_get_gsymbol (gfc_new_block->name);
5779 if (s->defined
5780 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
5781 gfc_global_used (s, &gfc_new_block->declared_at);
5782 else
5784 s->type = GSYM_BLOCK_DATA;
5785 s->where = gfc_new_block->declared_at;
5786 s->defined = 1;
5790 st = parse_spec (ST_NONE);
5792 while (st != ST_END_BLOCK_DATA)
5794 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5795 gfc_ascii_statement (st));
5796 reject_statement ();
5797 st = next_statement ();
5802 /* Following the association of the ancestor (sub)module symbols, they
5803 must be set host rather than use associated and all must be public.
5804 They are flagged up by 'used_in_submodule' so that they can be set
5805 DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl). Otherwise the
5806 linker chokes on multiple symbol definitions. */
5808 static void
5809 set_syms_host_assoc (gfc_symbol *sym)
5811 gfc_component *c;
5813 if (sym == NULL)
5814 return;
5816 if (sym->attr.module_procedure)
5817 sym->attr.external = 0;
5819 /* sym->attr.access = ACCESS_PUBLIC; */
5821 sym->attr.use_assoc = 0;
5822 sym->attr.host_assoc = 1;
5823 sym->attr.used_in_submodule =1;
5825 if (sym->attr.flavor == FL_DERIVED)
5827 for (c = sym->components; c; c = c->next)
5828 c->attr.access = ACCESS_PUBLIC;
5832 /* Parse a module subprogram. */
5834 static void
5835 parse_module (void)
5837 gfc_statement st;
5838 gfc_gsymbol *s;
5839 bool error;
5841 s = gfc_get_gsymbol (gfc_new_block->name);
5842 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
5843 gfc_global_used (s, &gfc_new_block->declared_at);
5844 else
5846 s->type = GSYM_MODULE;
5847 s->where = gfc_new_block->declared_at;
5848 s->defined = 1;
5851 /* Something is nulling the module_list after this point. This is good
5852 since it allows us to 'USE' the parent modules that the submodule
5853 inherits and to set (most) of the symbols as host associated. */
5854 if (gfc_current_state () == COMP_SUBMODULE)
5856 use_modules ();
5857 gfc_traverse_ns (gfc_current_ns, set_syms_host_assoc);
5860 st = parse_spec (ST_NONE);
5862 error = false;
5863 loop:
5864 switch (st)
5866 case ST_NONE:
5867 unexpected_eof ();
5869 case ST_CONTAINS:
5870 parse_contained (1);
5871 break;
5873 case ST_END_MODULE:
5874 case ST_END_SUBMODULE:
5875 accept_statement (st);
5876 break;
5878 default:
5879 gfc_error ("Unexpected %s statement in MODULE at %C",
5880 gfc_ascii_statement (st));
5882 error = true;
5883 reject_statement ();
5884 st = next_statement ();
5885 goto loop;
5888 /* Make sure not to free the namespace twice on error. */
5889 if (!error)
5890 s->ns = gfc_current_ns;
5894 /* Add a procedure name to the global symbol table. */
5896 static void
5897 add_global_procedure (bool sub)
5899 gfc_gsymbol *s;
5901 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5902 name is a global identifier. */
5903 if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
5905 s = gfc_get_gsymbol (gfc_new_block->name);
5907 if (s->defined
5908 || (s->type != GSYM_UNKNOWN
5909 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5911 gfc_global_used (s, &gfc_new_block->declared_at);
5912 /* Silence follow-up errors. */
5913 gfc_new_block->binding_label = NULL;
5915 else
5917 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5918 s->sym_name = gfc_new_block->name;
5919 s->where = gfc_new_block->declared_at;
5920 s->defined = 1;
5921 s->ns = gfc_current_ns;
5925 /* Don't add the symbol multiple times. */
5926 if (gfc_new_block->binding_label
5927 && (!gfc_notification_std (GFC_STD_F2008)
5928 || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
5930 s = gfc_get_gsymbol (gfc_new_block->binding_label);
5932 if (s->defined
5933 || (s->type != GSYM_UNKNOWN
5934 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5936 gfc_global_used (s, &gfc_new_block->declared_at);
5937 /* Silence follow-up errors. */
5938 gfc_new_block->binding_label = NULL;
5940 else
5942 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5943 s->sym_name = gfc_new_block->name;
5944 s->binding_label = gfc_new_block->binding_label;
5945 s->where = gfc_new_block->declared_at;
5946 s->defined = 1;
5947 s->ns = gfc_current_ns;
5953 /* Add a program to the global symbol table. */
5955 static void
5956 add_global_program (void)
5958 gfc_gsymbol *s;
5960 if (gfc_new_block == NULL)
5961 return;
5962 s = gfc_get_gsymbol (gfc_new_block->name);
5964 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
5965 gfc_global_used (s, &gfc_new_block->declared_at);
5966 else
5968 s->type = GSYM_PROGRAM;
5969 s->where = gfc_new_block->declared_at;
5970 s->defined = 1;
5971 s->ns = gfc_current_ns;
5976 /* Resolve all the program units. */
5977 static void
5978 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
5980 gfc_free_dt_list ();
5981 gfc_current_ns = gfc_global_ns_list;
5982 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5984 if (gfc_current_ns->proc_name
5985 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5986 continue; /* Already resolved. */
5988 if (gfc_current_ns->proc_name)
5989 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5990 gfc_resolve (gfc_current_ns);
5991 gfc_current_ns->derived_types = gfc_derived_types;
5992 gfc_derived_types = NULL;
5997 static void
5998 clean_up_modules (gfc_gsymbol *gsym)
6000 if (gsym == NULL)
6001 return;
6003 clean_up_modules (gsym->left);
6004 clean_up_modules (gsym->right);
6006 if (gsym->type != GSYM_MODULE || !gsym->ns)
6007 return;
6009 gfc_current_ns = gsym->ns;
6010 gfc_derived_types = gfc_current_ns->derived_types;
6011 gfc_done_2 ();
6012 gsym->ns = NULL;
6013 return;
6017 /* Translate all the program units. This could be in a different order
6018 to resolution if there are forward references in the file. */
6019 static void
6020 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
6022 int errors;
6024 gfc_current_ns = gfc_global_ns_list;
6025 gfc_get_errors (NULL, &errors);
6027 /* We first translate all modules to make sure that later parts
6028 of the program can use the decl. Then we translate the nonmodules. */
6030 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6032 if (!gfc_current_ns->proc_name
6033 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6034 continue;
6036 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
6037 gfc_derived_types = gfc_current_ns->derived_types;
6038 gfc_generate_module_code (gfc_current_ns);
6039 gfc_current_ns->translated = 1;
6042 gfc_current_ns = gfc_global_ns_list;
6043 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6045 if (gfc_current_ns->proc_name
6046 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
6047 continue;
6049 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
6050 gfc_derived_types = gfc_current_ns->derived_types;
6051 gfc_generate_code (gfc_current_ns);
6052 gfc_current_ns->translated = 1;
6055 /* Clean up all the namespaces after translation. */
6056 gfc_current_ns = gfc_global_ns_list;
6057 for (;gfc_current_ns;)
6059 gfc_namespace *ns;
6061 if (gfc_current_ns->proc_name
6062 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
6064 gfc_current_ns = gfc_current_ns->sibling;
6065 continue;
6068 ns = gfc_current_ns->sibling;
6069 gfc_derived_types = gfc_current_ns->derived_types;
6070 gfc_done_2 ();
6071 gfc_current_ns = ns;
6074 clean_up_modules (gfc_gsym_root);
6078 /* Top level parser. */
6080 bool
6081 gfc_parse_file (void)
6083 int seen_program, errors_before, errors;
6084 gfc_state_data top, s;
6085 gfc_statement st;
6086 locus prog_locus;
6087 gfc_namespace *next;
6089 gfc_start_source_files ();
6091 top.state = COMP_NONE;
6092 top.sym = NULL;
6093 top.previous = NULL;
6094 top.head = top.tail = NULL;
6095 top.do_variable = NULL;
6097 gfc_state_stack = &top;
6099 gfc_clear_new_st ();
6101 gfc_statement_label = NULL;
6103 if (setjmp (eof_buf))
6104 return false; /* Come here on unexpected EOF */
6106 /* Prepare the global namespace that will contain the
6107 program units. */
6108 gfc_global_ns_list = next = NULL;
6110 seen_program = 0;
6111 errors_before = 0;
6113 /* Exit early for empty files. */
6114 if (gfc_at_eof ())
6115 goto done;
6117 in_specification_block = true;
6118 loop:
6119 gfc_init_2 ();
6120 st = next_statement ();
6121 switch (st)
6123 case ST_NONE:
6124 gfc_done_2 ();
6125 goto done;
6127 case ST_PROGRAM:
6128 if (seen_program)
6129 goto duplicate_main;
6130 seen_program = 1;
6131 prog_locus = gfc_current_locus;
6133 push_state (&s, COMP_PROGRAM, gfc_new_block);
6134 main_program_symbol (gfc_current_ns, gfc_new_block->name);
6135 accept_statement (st);
6136 add_global_program ();
6137 parse_progunit (ST_NONE);
6138 goto prog_units;
6140 case ST_SUBROUTINE:
6141 add_global_procedure (true);
6142 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
6143 accept_statement (st);
6144 parse_progunit (ST_NONE);
6145 goto prog_units;
6147 case ST_FUNCTION:
6148 add_global_procedure (false);
6149 push_state (&s, COMP_FUNCTION, gfc_new_block);
6150 accept_statement (st);
6151 parse_progunit (ST_NONE);
6152 goto prog_units;
6154 case ST_BLOCK_DATA:
6155 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
6156 accept_statement (st);
6157 parse_block_data ();
6158 break;
6160 case ST_MODULE:
6161 push_state (&s, COMP_MODULE, gfc_new_block);
6162 accept_statement (st);
6164 gfc_get_errors (NULL, &errors_before);
6165 parse_module ();
6166 break;
6168 case ST_SUBMODULE:
6169 push_state (&s, COMP_SUBMODULE, gfc_new_block);
6170 accept_statement (st);
6172 gfc_get_errors (NULL, &errors_before);
6173 parse_module ();
6174 break;
6176 /* Anything else starts a nameless main program block. */
6177 default:
6178 if (seen_program)
6179 goto duplicate_main;
6180 seen_program = 1;
6181 prog_locus = gfc_current_locus;
6183 push_state (&s, COMP_PROGRAM, gfc_new_block);
6184 main_program_symbol (gfc_current_ns, "MAIN__");
6185 parse_progunit (st);
6186 goto prog_units;
6189 /* Handle the non-program units. */
6190 gfc_current_ns->code = s.head;
6192 gfc_resolve (gfc_current_ns);
6194 /* Dump the parse tree if requested. */
6195 if (flag_dump_fortran_original)
6196 gfc_dump_parse_tree (gfc_current_ns, stdout);
6198 gfc_get_errors (NULL, &errors);
6199 if (s.state == COMP_MODULE || s.state == COMP_SUBMODULE)
6201 gfc_dump_module (s.sym->name, errors_before == errors);
6202 gfc_current_ns->derived_types = gfc_derived_types;
6203 gfc_derived_types = NULL;
6204 goto prog_units;
6206 else
6208 if (errors == 0)
6209 gfc_generate_code (gfc_current_ns);
6210 pop_state ();
6211 gfc_done_2 ();
6214 goto loop;
6216 prog_units:
6217 /* The main program and non-contained procedures are put
6218 in the global namespace list, so that they can be processed
6219 later and all their interfaces resolved. */
6220 gfc_current_ns->code = s.head;
6221 if (next)
6223 for (; next->sibling; next = next->sibling)
6225 next->sibling = gfc_current_ns;
6227 else
6228 gfc_global_ns_list = gfc_current_ns;
6230 next = gfc_current_ns;
6232 pop_state ();
6233 goto loop;
6235 done:
6236 /* Do the resolution. */
6237 resolve_all_program_units (gfc_global_ns_list);
6239 /* Do the parse tree dump. */
6240 gfc_current_ns = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
6242 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
6243 if (!gfc_current_ns->proc_name
6244 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6246 gfc_dump_parse_tree (gfc_current_ns, stdout);
6247 fputs ("------------------------------------------\n\n", stdout);
6250 /* Do the translation. */
6251 translate_all_program_units (gfc_global_ns_list);
6253 gfc_end_source_files ();
6254 return true;
6256 duplicate_main:
6257 /* If we see a duplicate main program, shut down. If the second
6258 instance is an implied main program, i.e. data decls or executable
6259 statements, we're in for lots of errors. */
6260 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
6261 reject_statement ();
6262 gfc_done_2 ();
6263 return true;
6266 /* Return true if this state data represents an OpenACC region. */
6267 bool
6268 is_oacc (gfc_state_data *sd)
6270 switch (sd->construct->op)
6272 case EXEC_OACC_PARALLEL_LOOP:
6273 case EXEC_OACC_PARALLEL:
6274 case EXEC_OACC_KERNELS_LOOP:
6275 case EXEC_OACC_KERNELS:
6276 case EXEC_OACC_DATA:
6277 case EXEC_OACC_HOST_DATA:
6278 case EXEC_OACC_LOOP:
6279 case EXEC_OACC_UPDATE:
6280 case EXEC_OACC_WAIT:
6281 case EXEC_OACC_CACHE:
6282 case EXEC_OACC_ENTER_DATA:
6283 case EXEC_OACC_EXIT_DATA:
6284 case EXEC_OACC_ATOMIC:
6285 case EXEC_OACC_ROUTINE:
6286 return true;
6288 default:
6289 return false;