re PR fortran/44054 (Handle -Werror, -Werror=, -fdiagnostics-show-option, !GCC$ diagn...
[gcc.git] / gcc / fortran / parse.c
1 /* Main parser.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include <setjmp.h>
24 #include "coretypes.h"
25 #include "flags.h"
26 #include "gfortran.h"
27 #include "match.h"
28 #include "parse.h"
29 #include "debug.h"
30
31 /* Current statement label. Zero means no statement label. Because new_st
32 can get wiped during statement matching, we have to keep it separate. */
33
34 gfc_st_label *gfc_statement_label;
35
36 static locus label_locus;
37 static jmp_buf eof_buf;
38
39 gfc_state_data *gfc_state_stack;
40 static bool last_was_use_stmt = false;
41
42 /* TODO: Re-order functions to kill these forward decls. */
43 static void check_statement_label (gfc_statement);
44 static void undo_new_statement (void);
45 static void reject_statement (void);
46
47
48 /* A sort of half-matching function. We try to match the word on the
49 input with the passed string. If this succeeds, we call the
50 keyword-dependent matching function that will match the rest of the
51 statement. For single keywords, the matching subroutine is
52 gfc_match_eos(). */
53
54 static match
55 match_word (const char *str, match (*subr) (void), locus *old_locus)
56 {
57 match m;
58
59 if (str != NULL)
60 {
61 m = gfc_match (str);
62 if (m != MATCH_YES)
63 return m;
64 }
65
66 m = (*subr) ();
67
68 if (m != MATCH_YES)
69 {
70 gfc_current_locus = *old_locus;
71 reject_statement ();
72 }
73
74 return m;
75 }
76
77
78 /* Like match_word, but if str is matched, set a flag that it
79 was matched. */
80 static match
81 match_word_omp_simd (const char *str, match (*subr) (void), locus *old_locus,
82 bool *simd_matched)
83 {
84 match m;
85
86 if (str != NULL)
87 {
88 m = gfc_match (str);
89 if (m != MATCH_YES)
90 return m;
91 *simd_matched = true;
92 }
93
94 m = (*subr) ();
95
96 if (m != MATCH_YES)
97 {
98 gfc_current_locus = *old_locus;
99 reject_statement ();
100 }
101
102 return m;
103 }
104
105
106 /* Load symbols from all USE statements encountered in this scoping unit. */
107
108 static void
109 use_modules (void)
110 {
111 gfc_error_buffer old_error;
112
113 gfc_push_error (&old_error);
114 gfc_buffer_error (false);
115 gfc_use_modules ();
116 gfc_buffer_error (true);
117 gfc_pop_error (&old_error);
118 gfc_commit_symbols ();
119 gfc_warning_check ();
120 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
121 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
122 gfc_current_ns->old_data = gfc_current_ns->data;
123 last_was_use_stmt = false;
124 }
125
126
127 /* Figure out what the next statement is, (mostly) regardless of
128 proper ordering. The do...while(0) is there to prevent if/else
129 ambiguity. */
130
131 #define match(keyword, subr, st) \
132 do { \
133 if (match_word (keyword, subr, &old_locus) == MATCH_YES) \
134 return st; \
135 else \
136 undo_new_statement (); \
137 } while (0);
138
139
140 /* This is a specialist version of decode_statement that is used
141 for the specification statements in a function, whose
142 characteristics are deferred into the specification statements.
143 eg.: INTEGER (king = mykind) foo ()
144 USE mymodule, ONLY mykind.....
145 The KIND parameter needs a return after USE or IMPORT, whereas
146 derived type declarations can occur anywhere, up the executable
147 block. ST_GET_FCN_CHARACTERISTICS is returned when we have run
148 out of the correct kind of specification statements. */
149 static gfc_statement
150 decode_specification_statement (void)
151 {
152 gfc_statement st;
153 locus old_locus;
154 char c;
155
156 if (gfc_match_eos () == MATCH_YES)
157 return ST_NONE;
158
159 old_locus = gfc_current_locus;
160
161 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
162 {
163 last_was_use_stmt = true;
164 return ST_USE;
165 }
166 else
167 {
168 undo_new_statement ();
169 if (last_was_use_stmt)
170 use_modules ();
171 }
172
173 match ("import", gfc_match_import, ST_IMPORT);
174
175 if (gfc_current_block ()->result->ts.type != BT_DERIVED)
176 goto end_of_block;
177
178 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
179 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
180 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
181
182 /* General statement matching: Instead of testing every possible
183 statement, we eliminate most possibilities by peeking at the
184 first character. */
185
186 c = gfc_peek_ascii_char ();
187
188 switch (c)
189 {
190 case 'a':
191 match ("abstract% interface", gfc_match_abstract_interface,
192 ST_INTERFACE);
193 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
194 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
195 break;
196
197 case 'b':
198 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
199 break;
200
201 case 'c':
202 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
203 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
204 break;
205
206 case 'd':
207 match ("data", gfc_match_data, ST_DATA);
208 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
209 break;
210
211 case 'e':
212 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
213 match ("entry% ", gfc_match_entry, ST_ENTRY);
214 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
215 match ("external", gfc_match_external, ST_ATTR_DECL);
216 break;
217
218 case 'f':
219 match ("format", gfc_match_format, ST_FORMAT);
220 break;
221
222 case 'g':
223 break;
224
225 case 'i':
226 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
227 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
228 match ("interface", gfc_match_interface, ST_INTERFACE);
229 match ("intent", gfc_match_intent, ST_ATTR_DECL);
230 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
231 break;
232
233 case 'm':
234 break;
235
236 case 'n':
237 match ("namelist", gfc_match_namelist, ST_NAMELIST);
238 break;
239
240 case 'o':
241 match ("optional", gfc_match_optional, ST_ATTR_DECL);
242 break;
243
244 case 'p':
245 match ("parameter", gfc_match_parameter, ST_PARAMETER);
246 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
247 if (gfc_match_private (&st) == MATCH_YES)
248 return st;
249 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
250 if (gfc_match_public (&st) == MATCH_YES)
251 return st;
252 match ("protected", gfc_match_protected, ST_ATTR_DECL);
253 break;
254
255 case 'r':
256 break;
257
258 case 's':
259 match ("save", gfc_match_save, ST_ATTR_DECL);
260 break;
261
262 case 't':
263 match ("target", gfc_match_target, ST_ATTR_DECL);
264 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
265 break;
266
267 case 'u':
268 break;
269
270 case 'v':
271 match ("value", gfc_match_value, ST_ATTR_DECL);
272 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
273 break;
274
275 case 'w':
276 break;
277 }
278
279 /* This is not a specification statement. See if any of the matchers
280 has stored an error message of some sort. */
281
282 end_of_block:
283 gfc_clear_error ();
284 gfc_buffer_error (false);
285 gfc_current_locus = old_locus;
286
287 return ST_GET_FCN_CHARACTERISTICS;
288 }
289
290
291 /* This is the primary 'decode_statement'. */
292 static gfc_statement
293 decode_statement (void)
294 {
295 gfc_namespace *ns;
296 gfc_statement st;
297 locus old_locus;
298 match m;
299 char c;
300
301 gfc_enforce_clean_symbol_state ();
302
303 gfc_clear_error (); /* Clear any pending errors. */
304 gfc_clear_warning (); /* Clear any pending warnings. */
305
306 gfc_matching_function = false;
307
308 if (gfc_match_eos () == MATCH_YES)
309 return ST_NONE;
310
311 if (gfc_current_state () == COMP_FUNCTION
312 && gfc_current_block ()->result->ts.kind == -1)
313 return decode_specification_statement ();
314
315 old_locus = gfc_current_locus;
316
317 c = gfc_peek_ascii_char ();
318
319 if (c == 'u')
320 {
321 if (match_word ("use", gfc_match_use, &old_locus) == MATCH_YES)
322 {
323 last_was_use_stmt = true;
324 return ST_USE;
325 }
326 else
327 undo_new_statement ();
328 }
329
330 if (last_was_use_stmt)
331 use_modules ();
332
333 /* Try matching a data declaration or function declaration. The
334 input "REALFUNCTIONA(N)" can mean several things in different
335 contexts, so it (and its relatives) get special treatment. */
336
337 if (gfc_current_state () == COMP_NONE
338 || gfc_current_state () == COMP_INTERFACE
339 || gfc_current_state () == COMP_CONTAINS)
340 {
341 gfc_matching_function = true;
342 m = gfc_match_function_decl ();
343 if (m == MATCH_YES)
344 return ST_FUNCTION;
345 else if (m == MATCH_ERROR)
346 reject_statement ();
347 else
348 gfc_undo_symbols ();
349 gfc_current_locus = old_locus;
350 }
351 gfc_matching_function = false;
352
353
354 /* Match statements whose error messages are meant to be overwritten
355 by something better. */
356
357 match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
358 match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
359 match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
360
361 match (NULL, gfc_match_data_decl, ST_DATA_DECL);
362 match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
363
364 /* Try to match a subroutine statement, which has the same optional
365 prefixes that functions can have. */
366
367 if (gfc_match_subroutine () == MATCH_YES)
368 return ST_SUBROUTINE;
369 gfc_undo_symbols ();
370 gfc_current_locus = old_locus;
371
372 /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
373 statements, which might begin with a block label. The match functions for
374 these statements are unusual in that their keyword is not seen before
375 the matcher is called. */
376
377 if (gfc_match_if (&st) == MATCH_YES)
378 return st;
379 gfc_undo_symbols ();
380 gfc_current_locus = old_locus;
381
382 if (gfc_match_where (&st) == MATCH_YES)
383 return st;
384 gfc_undo_symbols ();
385 gfc_current_locus = old_locus;
386
387 if (gfc_match_forall (&st) == MATCH_YES)
388 return st;
389 gfc_undo_symbols ();
390 gfc_current_locus = old_locus;
391
392 match (NULL, gfc_match_do, ST_DO);
393 match (NULL, gfc_match_block, ST_BLOCK);
394 match (NULL, gfc_match_associate, ST_ASSOCIATE);
395 match (NULL, gfc_match_critical, ST_CRITICAL);
396 match (NULL, gfc_match_select, ST_SELECT_CASE);
397
398 gfc_current_ns = gfc_build_block_ns (gfc_current_ns);
399 match (NULL, gfc_match_select_type, ST_SELECT_TYPE);
400 ns = gfc_current_ns;
401 gfc_current_ns = gfc_current_ns->parent;
402 gfc_free_namespace (ns);
403
404 /* General statement matching: Instead of testing every possible
405 statement, we eliminate most possibilities by peeking at the
406 first character. */
407
408 switch (c)
409 {
410 case 'a':
411 match ("abstract% interface", gfc_match_abstract_interface,
412 ST_INTERFACE);
413 match ("allocate", gfc_match_allocate, ST_ALLOCATE);
414 match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
415 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
416 match ("asynchronous", gfc_match_asynchronous, ST_ATTR_DECL);
417 break;
418
419 case 'b':
420 match ("backspace", gfc_match_backspace, ST_BACKSPACE);
421 match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
422 match (NULL, gfc_match_bind_c_stmt, ST_ATTR_DECL);
423 break;
424
425 case 'c':
426 match ("call", gfc_match_call, ST_CALL);
427 match ("close", gfc_match_close, ST_CLOSE);
428 match ("continue", gfc_match_continue, ST_CONTINUE);
429 match ("contiguous", gfc_match_contiguous, ST_ATTR_DECL);
430 match ("cycle", gfc_match_cycle, ST_CYCLE);
431 match ("case", gfc_match_case, ST_CASE);
432 match ("common", gfc_match_common, ST_COMMON);
433 match ("contains", gfc_match_eos, ST_CONTAINS);
434 match ("class", gfc_match_class_is, ST_CLASS_IS);
435 match ("codimension", gfc_match_codimension, ST_ATTR_DECL);
436 break;
437
438 case 'd':
439 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
440 match ("data", gfc_match_data, ST_DATA);
441 match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
442 break;
443
444 case 'e':
445 match ("end file", gfc_match_endfile, ST_END_FILE);
446 match ("exit", gfc_match_exit, ST_EXIT);
447 match ("else", gfc_match_else, ST_ELSE);
448 match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
449 match ("else if", gfc_match_elseif, ST_ELSEIF);
450 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP);
451 match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
452
453 if (gfc_match_end (&st) == MATCH_YES)
454 return st;
455
456 match ("entry% ", gfc_match_entry, ST_ENTRY);
457 match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
458 match ("external", gfc_match_external, ST_ATTR_DECL);
459 break;
460
461 case 'f':
462 match ("final", gfc_match_final_decl, ST_FINAL);
463 match ("flush", gfc_match_flush, ST_FLUSH);
464 match ("format", gfc_match_format, ST_FORMAT);
465 break;
466
467 case 'g':
468 match ("generic", gfc_match_generic, ST_GENERIC);
469 match ("go to", gfc_match_goto, ST_GOTO);
470 break;
471
472 case 'i':
473 match ("inquire", gfc_match_inquire, ST_INQUIRE);
474 match ("implicit", gfc_match_implicit, ST_IMPLICIT);
475 match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
476 match ("import", gfc_match_import, ST_IMPORT);
477 match ("interface", gfc_match_interface, ST_INTERFACE);
478 match ("intent", gfc_match_intent, ST_ATTR_DECL);
479 match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
480 break;
481
482 case 'l':
483 match ("lock", gfc_match_lock, ST_LOCK);
484 break;
485
486 case 'm':
487 match ("module% procedure", gfc_match_modproc, ST_MODULE_PROC);
488 match ("module", gfc_match_module, ST_MODULE);
489 break;
490
491 case 'n':
492 match ("nullify", gfc_match_nullify, ST_NULLIFY);
493 match ("namelist", gfc_match_namelist, ST_NAMELIST);
494 break;
495
496 case 'o':
497 match ("open", gfc_match_open, ST_OPEN);
498 match ("optional", gfc_match_optional, ST_ATTR_DECL);
499 break;
500
501 case 'p':
502 match ("print", gfc_match_print, ST_WRITE);
503 match ("parameter", gfc_match_parameter, ST_PARAMETER);
504 match ("pause", gfc_match_pause, ST_PAUSE);
505 match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
506 if (gfc_match_private (&st) == MATCH_YES)
507 return st;
508 match ("procedure", gfc_match_procedure, ST_PROCEDURE);
509 match ("program", gfc_match_program, ST_PROGRAM);
510 if (gfc_match_public (&st) == MATCH_YES)
511 return st;
512 match ("protected", gfc_match_protected, ST_ATTR_DECL);
513 break;
514
515 case 'r':
516 match ("read", gfc_match_read, ST_READ);
517 match ("return", gfc_match_return, ST_RETURN);
518 match ("rewind", gfc_match_rewind, ST_REWIND);
519 break;
520
521 case 's':
522 match ("sequence", gfc_match_eos, ST_SEQUENCE);
523 match ("stop", gfc_match_stop, ST_STOP);
524 match ("save", gfc_match_save, ST_ATTR_DECL);
525 match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
526 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
527 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
528 break;
529
530 case 't':
531 match ("target", gfc_match_target, ST_ATTR_DECL);
532 match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
533 match ("type is", gfc_match_type_is, ST_TYPE_IS);
534 break;
535
536 case 'u':
537 match ("unlock", gfc_match_unlock, ST_UNLOCK);
538 break;
539
540 case 'v':
541 match ("value", gfc_match_value, ST_ATTR_DECL);
542 match ("volatile", gfc_match_volatile, ST_ATTR_DECL);
543 break;
544
545 case 'w':
546 match ("wait", gfc_match_wait, ST_WAIT);
547 match ("write", gfc_match_write, ST_WRITE);
548 break;
549 }
550
551 /* All else has failed, so give up. See if any of the matchers has
552 stored an error message of some sort. */
553
554 if (!gfc_error_check ())
555 gfc_error_now ("Unclassifiable statement at %C");
556
557 reject_statement ();
558
559 gfc_error_recovery ();
560
561 return ST_NONE;
562 }
563
564 /* Like match, but set a flag simd_matched if keyword matched. */
565 #define matchs(keyword, subr, st) \
566 do { \
567 if (match_word_omp_simd (keyword, subr, &old_locus, \
568 &simd_matched) == MATCH_YES) \
569 return st; \
570 else \
571 undo_new_statement (); \
572 } while (0);
573
574 /* Like match, but don't match anything if not -fopenmp. */
575 #define matcho(keyword, subr, st) \
576 do { \
577 if (!flag_openmp) \
578 ; \
579 else if (match_word (keyword, subr, &old_locus) \
580 == MATCH_YES) \
581 return st; \
582 else \
583 undo_new_statement (); \
584 } while (0);
585
586 static gfc_statement
587 decode_oacc_directive (void)
588 {
589 locus old_locus;
590 char c;
591
592 gfc_enforce_clean_symbol_state ();
593
594 gfc_clear_error (); /* Clear any pending errors. */
595 gfc_clear_warning (); /* Clear any pending warnings. */
596
597 if (gfc_pure (NULL))
598 {
599 gfc_error_now ("OpenACC directives at %C may not appear in PURE "
600 "procedures");
601 gfc_error_recovery ();
602 return ST_NONE;
603 }
604
605 gfc_unset_implicit_pure (NULL);
606
607 old_locus = gfc_current_locus;
608
609 /* General OpenACC directive matching: Instead of testing every possible
610 statement, we eliminate most possibilities by peeking at the
611 first character. */
612
613 c = gfc_peek_ascii_char ();
614
615 switch (c)
616 {
617 case 'c':
618 match ("cache", gfc_match_oacc_cache, ST_OACC_CACHE);
619 break;
620 case 'd':
621 match ("data", gfc_match_oacc_data, ST_OACC_DATA);
622 match ("declare", gfc_match_oacc_declare, ST_OACC_DECLARE);
623 break;
624 case 'e':
625 match ("end data", gfc_match_omp_eos, ST_OACC_END_DATA);
626 match ("end host_data", gfc_match_omp_eos, ST_OACC_END_HOST_DATA);
627 match ("end kernels loop", gfc_match_omp_eos, ST_OACC_END_KERNELS_LOOP);
628 match ("end kernels", gfc_match_omp_eos, ST_OACC_END_KERNELS);
629 match ("end loop", gfc_match_omp_eos, ST_OACC_END_LOOP);
630 match ("end parallel loop", gfc_match_omp_eos, ST_OACC_END_PARALLEL_LOOP);
631 match ("end parallel", gfc_match_omp_eos, ST_OACC_END_PARALLEL);
632 match ("enter data", gfc_match_oacc_enter_data, ST_OACC_ENTER_DATA);
633 match ("exit data", gfc_match_oacc_exit_data, ST_OACC_EXIT_DATA);
634 break;
635 case 'h':
636 match ("host_data", gfc_match_oacc_host_data, ST_OACC_HOST_DATA);
637 break;
638 case 'p':
639 match ("parallel loop", gfc_match_oacc_parallel_loop, ST_OACC_PARALLEL_LOOP);
640 match ("parallel", gfc_match_oacc_parallel, ST_OACC_PARALLEL);
641 break;
642 case 'k':
643 match ("kernels loop", gfc_match_oacc_kernels_loop, ST_OACC_KERNELS_LOOP);
644 match ("kernels", gfc_match_oacc_kernels, ST_OACC_KERNELS);
645 break;
646 case 'l':
647 match ("loop", gfc_match_oacc_loop, ST_OACC_LOOP);
648 break;
649 case 'r':
650 match ("routine", gfc_match_oacc_routine, ST_OACC_ROUTINE);
651 break;
652 case 'u':
653 match ("update", gfc_match_oacc_update, ST_OACC_UPDATE);
654 break;
655 case 'w':
656 match ("wait", gfc_match_oacc_wait, ST_OACC_WAIT);
657 break;
658 }
659
660 /* Directive not found or stored an error message.
661 Check and give up. */
662
663 if (gfc_error_check () == 0)
664 gfc_error_now ("Unclassifiable OpenACC directive at %C");
665
666 reject_statement ();
667
668 gfc_error_recovery ();
669
670 return ST_NONE;
671 }
672
673 static gfc_statement
674 decode_omp_directive (void)
675 {
676 locus old_locus;
677 char c;
678 bool simd_matched = false;
679
680 gfc_enforce_clean_symbol_state ();
681
682 gfc_clear_error (); /* Clear any pending errors. */
683 gfc_clear_warning (); /* Clear any pending warnings. */
684
685 if (gfc_pure (NULL))
686 {
687 gfc_error_now ("OpenMP directives at %C may not appear in PURE "
688 "or ELEMENTAL procedures");
689 gfc_error_recovery ();
690 return ST_NONE;
691 }
692
693 gfc_unset_implicit_pure (NULL);
694
695 old_locus = gfc_current_locus;
696
697 /* General OpenMP directive matching: Instead of testing every possible
698 statement, we eliminate most possibilities by peeking at the
699 first character. */
700
701 c = gfc_peek_ascii_char ();
702
703 /* match is for directives that should be recognized only if
704 -fopenmp, matchs for directives that should be recognized
705 if either -fopenmp or -fopenmp-simd. */
706 switch (c)
707 {
708 case 'a':
709 matcho ("atomic", gfc_match_omp_atomic, ST_OMP_ATOMIC);
710 break;
711 case 'b':
712 matcho ("barrier", gfc_match_omp_barrier, ST_OMP_BARRIER);
713 break;
714 case 'c':
715 matcho ("cancellation% point", gfc_match_omp_cancellation_point,
716 ST_OMP_CANCELLATION_POINT);
717 matcho ("cancel", gfc_match_omp_cancel, ST_OMP_CANCEL);
718 matcho ("critical", gfc_match_omp_critical, ST_OMP_CRITICAL);
719 break;
720 case 'd':
721 matchs ("declare reduction", gfc_match_omp_declare_reduction,
722 ST_OMP_DECLARE_REDUCTION);
723 matchs ("declare simd", gfc_match_omp_declare_simd,
724 ST_OMP_DECLARE_SIMD);
725 matcho ("declare target", gfc_match_omp_declare_target,
726 ST_OMP_DECLARE_TARGET);
727 matchs ("distribute parallel do simd",
728 gfc_match_omp_distribute_parallel_do_simd,
729 ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD);
730 matcho ("distribute parallel do", gfc_match_omp_distribute_parallel_do,
731 ST_OMP_DISTRIBUTE_PARALLEL_DO);
732 matchs ("distribute simd", gfc_match_omp_distribute_simd,
733 ST_OMP_DISTRIBUTE_SIMD);
734 matcho ("distribute", gfc_match_omp_distribute, ST_OMP_DISTRIBUTE);
735 matchs ("do simd", gfc_match_omp_do_simd, ST_OMP_DO_SIMD);
736 matcho ("do", gfc_match_omp_do, ST_OMP_DO);
737 break;
738 case 'e':
739 matcho ("end atomic", gfc_match_omp_eos, ST_OMP_END_ATOMIC);
740 matcho ("end critical", gfc_match_omp_critical, ST_OMP_END_CRITICAL);
741 matchs ("end distribute parallel do simd", gfc_match_omp_eos,
742 ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD);
743 matcho ("end distribute parallel do", gfc_match_omp_eos,
744 ST_OMP_END_DISTRIBUTE_PARALLEL_DO);
745 matchs ("end distribute simd", gfc_match_omp_eos,
746 ST_OMP_END_DISTRIBUTE_SIMD);
747 matcho ("end distribute", gfc_match_omp_eos, ST_OMP_END_DISTRIBUTE);
748 matchs ("end do simd", gfc_match_omp_end_nowait, ST_OMP_END_DO_SIMD);
749 matcho ("end do", gfc_match_omp_end_nowait, ST_OMP_END_DO);
750 matchs ("end simd", gfc_match_omp_eos, ST_OMP_END_SIMD);
751 matcho ("end master", gfc_match_omp_eos, ST_OMP_END_MASTER);
752 matcho ("end ordered", gfc_match_omp_eos, ST_OMP_END_ORDERED);
753 matchs ("end parallel do simd", gfc_match_omp_eos,
754 ST_OMP_END_PARALLEL_DO_SIMD);
755 matcho ("end parallel do", gfc_match_omp_eos, ST_OMP_END_PARALLEL_DO);
756 matcho ("end parallel sections", gfc_match_omp_eos,
757 ST_OMP_END_PARALLEL_SECTIONS);
758 matcho ("end parallel workshare", gfc_match_omp_eos,
759 ST_OMP_END_PARALLEL_WORKSHARE);
760 matcho ("end parallel", gfc_match_omp_eos, ST_OMP_END_PARALLEL);
761 matcho ("end sections", gfc_match_omp_end_nowait, ST_OMP_END_SECTIONS);
762 matcho ("end single", gfc_match_omp_end_single, ST_OMP_END_SINGLE);
763 matcho ("end target data", gfc_match_omp_eos, ST_OMP_END_TARGET_DATA);
764 matchs ("end target teams distribute parallel do simd",
765 gfc_match_omp_eos,
766 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
767 matcho ("end target teams distribute parallel do", gfc_match_omp_eos,
768 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
769 matchs ("end target teams distribute simd", gfc_match_omp_eos,
770 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD);
771 matcho ("end target teams distribute", gfc_match_omp_eos,
772 ST_OMP_END_TARGET_TEAMS_DISTRIBUTE);
773 matcho ("end target teams", gfc_match_omp_eos, ST_OMP_END_TARGET_TEAMS);
774 matcho ("end target", gfc_match_omp_eos, ST_OMP_END_TARGET);
775 matcho ("end taskgroup", gfc_match_omp_eos, ST_OMP_END_TASKGROUP);
776 matcho ("end task", gfc_match_omp_eos, ST_OMP_END_TASK);
777 matchs ("end teams distribute parallel do simd", gfc_match_omp_eos,
778 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
779 matcho ("end teams distribute parallel do", gfc_match_omp_eos,
780 ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO);
781 matchs ("end teams distribute simd", gfc_match_omp_eos,
782 ST_OMP_END_TEAMS_DISTRIBUTE_SIMD);
783 matcho ("end teams distribute", gfc_match_omp_eos,
784 ST_OMP_END_TEAMS_DISTRIBUTE);
785 matcho ("end teams", gfc_match_omp_eos, ST_OMP_END_TEAMS);
786 matcho ("end workshare", gfc_match_omp_end_nowait,
787 ST_OMP_END_WORKSHARE);
788 break;
789 case 'f':
790 matcho ("flush", gfc_match_omp_flush, ST_OMP_FLUSH);
791 break;
792 case 'm':
793 matcho ("master", gfc_match_omp_master, ST_OMP_MASTER);
794 break;
795 case 'o':
796 matcho ("ordered", gfc_match_omp_ordered, ST_OMP_ORDERED);
797 break;
798 case 'p':
799 matchs ("parallel do simd", gfc_match_omp_parallel_do_simd,
800 ST_OMP_PARALLEL_DO_SIMD);
801 matcho ("parallel do", gfc_match_omp_parallel_do, ST_OMP_PARALLEL_DO);
802 matcho ("parallel sections", gfc_match_omp_parallel_sections,
803 ST_OMP_PARALLEL_SECTIONS);
804 matcho ("parallel workshare", gfc_match_omp_parallel_workshare,
805 ST_OMP_PARALLEL_WORKSHARE);
806 matcho ("parallel", gfc_match_omp_parallel, ST_OMP_PARALLEL);
807 break;
808 case 's':
809 matcho ("sections", gfc_match_omp_sections, ST_OMP_SECTIONS);
810 matcho ("section", gfc_match_omp_eos, ST_OMP_SECTION);
811 matchs ("simd", gfc_match_omp_simd, ST_OMP_SIMD);
812 matcho ("single", gfc_match_omp_single, ST_OMP_SINGLE);
813 break;
814 case 't':
815 matcho ("target data", gfc_match_omp_target_data, ST_OMP_TARGET_DATA);
816 matchs ("target teams distribute parallel do simd",
817 gfc_match_omp_target_teams_distribute_parallel_do_simd,
818 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
819 matcho ("target teams distribute parallel do",
820 gfc_match_omp_target_teams_distribute_parallel_do,
821 ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
822 matchs ("target teams distribute simd",
823 gfc_match_omp_target_teams_distribute_simd,
824 ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD);
825 matcho ("target teams distribute", gfc_match_omp_target_teams_distribute,
826 ST_OMP_TARGET_TEAMS_DISTRIBUTE);
827 matcho ("target teams", gfc_match_omp_target_teams, ST_OMP_TARGET_TEAMS);
828 matcho ("target update", gfc_match_omp_target_update,
829 ST_OMP_TARGET_UPDATE);
830 matcho ("target", gfc_match_omp_target, ST_OMP_TARGET);
831 matcho ("taskgroup", gfc_match_omp_taskgroup, ST_OMP_TASKGROUP);
832 matcho ("taskwait", gfc_match_omp_taskwait, ST_OMP_TASKWAIT);
833 matcho ("taskyield", gfc_match_omp_taskyield, ST_OMP_TASKYIELD);
834 matcho ("task", gfc_match_omp_task, ST_OMP_TASK);
835 matchs ("teams distribute parallel do simd",
836 gfc_match_omp_teams_distribute_parallel_do_simd,
837 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
838 matcho ("teams distribute parallel do",
839 gfc_match_omp_teams_distribute_parallel_do,
840 ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO);
841 matchs ("teams distribute simd", gfc_match_omp_teams_distribute_simd,
842 ST_OMP_TEAMS_DISTRIBUTE_SIMD);
843 matcho ("teams distribute", gfc_match_omp_teams_distribute,
844 ST_OMP_TEAMS_DISTRIBUTE);
845 matcho ("teams", gfc_match_omp_teams, ST_OMP_TEAMS);
846 matcho ("threadprivate", gfc_match_omp_threadprivate,
847 ST_OMP_THREADPRIVATE);
848 break;
849 case 'w':
850 matcho ("workshare", gfc_match_omp_workshare, ST_OMP_WORKSHARE);
851 break;
852 }
853
854 /* All else has failed, so give up. See if any of the matchers has
855 stored an error message of some sort. Don't error out if
856 not -fopenmp and simd_matched is false, i.e. if a directive other
857 than one marked with match has been seen. */
858
859 if (flag_openmp || simd_matched)
860 {
861 if (!gfc_error_check ())
862 gfc_error_now ("Unclassifiable OpenMP directive at %C");
863 }
864
865 reject_statement ();
866
867 gfc_error_recovery ();
868
869 return ST_NONE;
870 }
871
872 static gfc_statement
873 decode_gcc_attribute (void)
874 {
875 locus old_locus;
876
877 gfc_enforce_clean_symbol_state ();
878
879 gfc_clear_error (); /* Clear any pending errors. */
880 gfc_clear_warning (); /* Clear any pending warnings. */
881 old_locus = gfc_current_locus;
882
883 match ("attributes", gfc_match_gcc_attributes, ST_ATTR_DECL);
884
885 /* All else has failed, so give up. See if any of the matchers has
886 stored an error message of some sort. */
887
888 if (!gfc_error_check ())
889 gfc_error_now ("Unclassifiable GCC directive at %C");
890
891 reject_statement ();
892
893 gfc_error_recovery ();
894
895 return ST_NONE;
896 }
897
898 #undef match
899
900 /* Assert next length characters to be equal to token in free form. */
901
902 static void
903 verify_token_free (const char* token, int length, bool last_was_use_stmt)
904 {
905 int i;
906 char c;
907
908 c = gfc_next_ascii_char ();
909 for (i = 0; i < length; i++, c = gfc_next_ascii_char ())
910 gcc_assert (c == token[i]);
911
912 gcc_assert (gfc_is_whitespace(c));
913 gfc_gobble_whitespace ();
914 if (last_was_use_stmt)
915 use_modules ();
916 }
917
918 /* Get the next statement in free form source. */
919
920 static gfc_statement
921 next_free (void)
922 {
923 match m;
924 int i, cnt, at_bol;
925 char c;
926
927 at_bol = gfc_at_bol ();
928 gfc_gobble_whitespace ();
929
930 c = gfc_peek_ascii_char ();
931
932 if (ISDIGIT (c))
933 {
934 char d;
935
936 /* Found a statement label? */
937 m = gfc_match_st_label (&gfc_statement_label);
938
939 d = gfc_peek_ascii_char ();
940 if (m != MATCH_YES || !gfc_is_whitespace (d))
941 {
942 gfc_match_small_literal_int (&i, &cnt);
943
944 if (cnt > 5)
945 gfc_error_now ("Too many digits in statement label at %C");
946
947 if (i == 0)
948 gfc_error_now ("Zero is not a valid statement label at %C");
949
950 do
951 c = gfc_next_ascii_char ();
952 while (ISDIGIT(c));
953
954 if (!gfc_is_whitespace (c))
955 gfc_error_now ("Non-numeric character in statement label at %C");
956
957 return ST_NONE;
958 }
959 else
960 {
961 label_locus = gfc_current_locus;
962
963 gfc_gobble_whitespace ();
964
965 if (at_bol && gfc_peek_ascii_char () == ';')
966 {
967 gfc_error_now ("Semicolon at %C needs to be preceded by "
968 "statement");
969 gfc_next_ascii_char (); /* Eat up the semicolon. */
970 return ST_NONE;
971 }
972
973 if (gfc_match_eos () == MATCH_YES)
974 {
975 gfc_warning_now (0, "Ignoring statement label in empty statement "
976 "at %L", &label_locus);
977 gfc_free_st_label (gfc_statement_label);
978 gfc_statement_label = NULL;
979 return ST_NONE;
980 }
981 }
982 }
983 else if (c == '!')
984 {
985 /* Comments have already been skipped by the time we get here,
986 except for GCC attributes and OpenMP/OpenACC directives. */
987
988 gfc_next_ascii_char (); /* Eat up the exclamation sign. */
989 c = gfc_peek_ascii_char ();
990
991 if (c == 'g')
992 {
993 int i;
994
995 c = gfc_next_ascii_char ();
996 for (i = 0; i < 4; i++, c = gfc_next_ascii_char ())
997 gcc_assert (c == "gcc$"[i]);
998
999 gfc_gobble_whitespace ();
1000 return decode_gcc_attribute ();
1001
1002 }
1003 else if (c == '$')
1004 {
1005 /* Since both OpenMP and OpenACC directives starts with
1006 !$ character sequence, we must check all flags combinations */
1007 if ((flag_openmp || flag_openmp_simd)
1008 && !flag_openacc)
1009 {
1010 verify_token_free ("$omp", 4, last_was_use_stmt);
1011 return decode_omp_directive ();
1012 }
1013 else if ((flag_openmp || flag_openmp_simd)
1014 && flag_openacc)
1015 {
1016 gfc_next_ascii_char (); /* Eat up dollar character */
1017 c = gfc_peek_ascii_char ();
1018
1019 if (c == 'o')
1020 {
1021 verify_token_free ("omp", 3, last_was_use_stmt);
1022 return decode_omp_directive ();
1023 }
1024 else if (c == 'a')
1025 {
1026 verify_token_free ("acc", 3, last_was_use_stmt);
1027 return decode_oacc_directive ();
1028 }
1029 }
1030 else if (flag_openacc)
1031 {
1032 verify_token_free ("$acc", 4, last_was_use_stmt);
1033 return decode_oacc_directive ();
1034 }
1035 }
1036 gcc_unreachable ();
1037 }
1038
1039 if (at_bol && c == ';')
1040 {
1041 if (!(gfc_option.allow_std & GFC_STD_F2008))
1042 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1043 "statement");
1044 gfc_next_ascii_char (); /* Eat up the semicolon. */
1045 return ST_NONE;
1046 }
1047
1048 return decode_statement ();
1049 }
1050
1051 /* Assert next length characters to be equal to token in fixed form. */
1052
1053 static bool
1054 verify_token_fixed (const char *token, int length, bool last_was_use_stmt)
1055 {
1056 int i;
1057 char c = gfc_next_char_literal (NONSTRING);
1058
1059 for (i = 0; i < length; i++, c = gfc_next_char_literal (NONSTRING))
1060 gcc_assert ((char) gfc_wide_tolower (c) == token[i]);
1061
1062 if (c != ' ' && c != '0')
1063 {
1064 gfc_buffer_error (false);
1065 gfc_error ("Bad continuation line at %C");
1066 return false;
1067 }
1068 if (last_was_use_stmt)
1069 use_modules ();
1070
1071 return true;
1072 }
1073
1074 /* Get the next statement in fixed-form source. */
1075
1076 static gfc_statement
1077 next_fixed (void)
1078 {
1079 int label, digit_flag, i;
1080 locus loc;
1081 gfc_char_t c;
1082
1083 if (!gfc_at_bol ())
1084 return decode_statement ();
1085
1086 /* Skip past the current label field, parsing a statement label if
1087 one is there. This is a weird number parser, since the number is
1088 contained within five columns and can have any kind of embedded
1089 spaces. We also check for characters that make the rest of the
1090 line a comment. */
1091
1092 label = 0;
1093 digit_flag = 0;
1094
1095 for (i = 0; i < 5; i++)
1096 {
1097 c = gfc_next_char_literal (NONSTRING);
1098
1099 switch (c)
1100 {
1101 case ' ':
1102 break;
1103
1104 case '0':
1105 case '1':
1106 case '2':
1107 case '3':
1108 case '4':
1109 case '5':
1110 case '6':
1111 case '7':
1112 case '8':
1113 case '9':
1114 label = label * 10 + ((unsigned char) c - '0');
1115 label_locus = gfc_current_locus;
1116 digit_flag = 1;
1117 break;
1118
1119 /* Comments have already been skipped by the time we get
1120 here, except for GCC attributes and OpenMP directives. */
1121
1122 case '*':
1123 c = gfc_next_char_literal (NONSTRING);
1124
1125 if (TOLOWER (c) == 'g')
1126 {
1127 for (i = 0; i < 4; i++, c = gfc_next_char_literal (NONSTRING))
1128 gcc_assert (TOLOWER (c) == "gcc$"[i]);
1129
1130 return decode_gcc_attribute ();
1131 }
1132 else if (c == '$')
1133 {
1134 if ((flag_openmp || flag_openmp_simd)
1135 && !flag_openacc)
1136 {
1137 if (!verify_token_fixed ("omp", 3, last_was_use_stmt))
1138 return ST_NONE;
1139 return decode_omp_directive ();
1140 }
1141 else if ((flag_openmp || flag_openmp_simd)
1142 && flag_openacc)
1143 {
1144 c = gfc_next_char_literal(NONSTRING);
1145 if (c == 'o' || c == 'O')
1146 {
1147 if (!verify_token_fixed ("mp", 2, last_was_use_stmt))
1148 return ST_NONE;
1149 return decode_omp_directive ();
1150 }
1151 else if (c == 'a' || c == 'A')
1152 {
1153 if (!verify_token_fixed ("cc", 2, last_was_use_stmt))
1154 return ST_NONE;
1155 return decode_oacc_directive ();
1156 }
1157 }
1158 else if (flag_openacc)
1159 {
1160 if (!verify_token_fixed ("acc", 3, last_was_use_stmt))
1161 return ST_NONE;
1162 return decode_oacc_directive ();
1163 }
1164 }
1165 /* FALLTHROUGH */
1166
1167 /* Comments have already been skipped by the time we get
1168 here so don't bother checking for them. */
1169
1170 default:
1171 gfc_buffer_error (false);
1172 gfc_error ("Non-numeric character in statement label at %C");
1173 return ST_NONE;
1174 }
1175 }
1176
1177 if (digit_flag)
1178 {
1179 if (label == 0)
1180 gfc_warning_now (0, "Zero is not a valid statement label at %C");
1181 else
1182 {
1183 /* We've found a valid statement label. */
1184 gfc_statement_label = gfc_get_st_label (label);
1185 }
1186 }
1187
1188 /* Since this line starts a statement, it cannot be a continuation
1189 of a previous statement. If we see something here besides a
1190 space or zero, it must be a bad continuation line. */
1191
1192 c = gfc_next_char_literal (NONSTRING);
1193 if (c == '\n')
1194 goto blank_line;
1195
1196 if (c != ' ' && c != '0')
1197 {
1198 gfc_buffer_error (false);
1199 gfc_error ("Bad continuation line at %C");
1200 return ST_NONE;
1201 }
1202
1203 /* Now that we've taken care of the statement label columns, we have
1204 to make sure that the first nonblank character is not a '!'. If
1205 it is, the rest of the line is a comment. */
1206
1207 do
1208 {
1209 loc = gfc_current_locus;
1210 c = gfc_next_char_literal (NONSTRING);
1211 }
1212 while (gfc_is_whitespace (c));
1213
1214 if (c == '!')
1215 goto blank_line;
1216 gfc_current_locus = loc;
1217
1218 if (c == ';')
1219 {
1220 if (digit_flag)
1221 gfc_error_now ("Semicolon at %C needs to be preceded by statement");
1222 else if (!(gfc_option.allow_std & GFC_STD_F2008))
1223 gfc_error_now ("Fortran 2008: Semicolon at %C without preceding "
1224 "statement");
1225 return ST_NONE;
1226 }
1227
1228 if (gfc_match_eos () == MATCH_YES)
1229 goto blank_line;
1230
1231 /* At this point, we've got a nonblank statement to parse. */
1232 return decode_statement ();
1233
1234 blank_line:
1235 if (digit_flag)
1236 gfc_warning_now (0, "Ignoring statement label in empty statement at %L",
1237 &label_locus);
1238
1239 gfc_current_locus.lb->truncated = 0;
1240 gfc_advance_line ();
1241 return ST_NONE;
1242 }
1243
1244
1245 /* Return the next non-ST_NONE statement to the caller. We also worry
1246 about including files and the ends of include files at this stage. */
1247
1248 static gfc_statement
1249 next_statement (void)
1250 {
1251 gfc_statement st;
1252 locus old_locus;
1253
1254 gfc_enforce_clean_symbol_state ();
1255
1256 gfc_new_block = NULL;
1257
1258 gfc_current_ns->old_cl_list = gfc_current_ns->cl_list;
1259 gfc_current_ns->old_equiv = gfc_current_ns->equiv;
1260 gfc_current_ns->old_data = gfc_current_ns->data;
1261 for (;;)
1262 {
1263 gfc_statement_label = NULL;
1264 gfc_buffer_error (true);
1265
1266 if (gfc_at_eol ())
1267 gfc_advance_line ();
1268
1269 gfc_skip_comments ();
1270
1271 if (gfc_at_end ())
1272 {
1273 st = ST_NONE;
1274 break;
1275 }
1276
1277 if (gfc_define_undef_line ())
1278 continue;
1279
1280 old_locus = gfc_current_locus;
1281
1282 st = (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
1283
1284 if (st != ST_NONE)
1285 break;
1286 }
1287
1288 gfc_buffer_error (false);
1289
1290 if (st == ST_GET_FCN_CHARACTERISTICS && gfc_statement_label != NULL)
1291 {
1292 gfc_free_st_label (gfc_statement_label);
1293 gfc_statement_label = NULL;
1294 gfc_current_locus = old_locus;
1295 }
1296
1297 if (st != ST_NONE)
1298 check_statement_label (st);
1299
1300 return st;
1301 }
1302
1303
1304 /****************************** Parser ***********************************/
1305
1306 /* The parser subroutines are of type 'try' that fail if the file ends
1307 unexpectedly. */
1308
1309 /* Macros that expand to case-labels for various classes of
1310 statements. Start with executable statements that directly do
1311 things. */
1312
1313 #define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
1314 case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
1315 case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
1316 case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
1317 case ST_PAUSE: case ST_STOP: case ST_WAIT: case ST_WRITE: \
1318 case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
1319 case ST_ASSIGNMENT: case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
1320 case ST_LABEL_ASSIGNMENT: case ST_FLUSH: case ST_OMP_FLUSH: \
1321 case ST_OMP_BARRIER: case ST_OMP_TASKWAIT: case ST_OMP_TASKYIELD: \
1322 case ST_OMP_CANCEL: case ST_OMP_CANCELLATION_POINT: \
1323 case ST_OMP_TARGET_UPDATE: case ST_ERROR_STOP: case ST_SYNC_ALL: \
1324 case ST_SYNC_IMAGES: case ST_SYNC_MEMORY: case ST_LOCK: case ST_UNLOCK: \
1325 case ST_OACC_UPDATE: case ST_OACC_WAIT: case ST_OACC_CACHE: \
1326 case ST_OACC_ENTER_DATA: case ST_OACC_EXIT_DATA
1327
1328 /* Statements that mark other executable statements. */
1329
1330 #define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: \
1331 case ST_IF_BLOCK: case ST_BLOCK: case ST_ASSOCIATE: \
1332 case ST_WHERE_BLOCK: case ST_SELECT_CASE: case ST_SELECT_TYPE: \
1333 case ST_OMP_PARALLEL: \
1334 case ST_OMP_PARALLEL_SECTIONS: case ST_OMP_SECTIONS: case ST_OMP_ORDERED: \
1335 case ST_OMP_CRITICAL: case ST_OMP_MASTER: case ST_OMP_SINGLE: \
1336 case ST_OMP_DO: case ST_OMP_PARALLEL_DO: case ST_OMP_ATOMIC: \
1337 case ST_OMP_WORKSHARE: case ST_OMP_PARALLEL_WORKSHARE: \
1338 case ST_OMP_TASK: case ST_OMP_TASKGROUP: case ST_OMP_SIMD: \
1339 case ST_OMP_DO_SIMD: case ST_OMP_PARALLEL_DO_SIMD: case ST_OMP_TARGET: \
1340 case ST_OMP_TARGET_DATA: case ST_OMP_TARGET_TEAMS: \
1341 case ST_OMP_TARGET_TEAMS_DISTRIBUTE: \
1342 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: \
1343 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1344 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: \
1345 case ST_OMP_TEAMS: case ST_OMP_TEAMS_DISTRIBUTE: \
1346 case ST_OMP_TEAMS_DISTRIBUTE_SIMD: \
1347 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: \
1348 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: case ST_OMP_DISTRIBUTE: \
1349 case ST_OMP_DISTRIBUTE_SIMD: case ST_OMP_DISTRIBUTE_PARALLEL_DO: \
1350 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: \
1351 case ST_CRITICAL: \
1352 case ST_OACC_PARALLEL_LOOP: case ST_OACC_PARALLEL: case ST_OACC_KERNELS: \
1353 case ST_OACC_DATA: case ST_OACC_HOST_DATA: case ST_OACC_LOOP: case ST_OACC_KERNELS_LOOP
1354
1355 /* Declaration statements */
1356
1357 #define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
1358 case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
1359 case ST_TYPE: case ST_INTERFACE: case ST_OMP_THREADPRIVATE: \
1360 case ST_PROCEDURE: case ST_OMP_DECLARE_SIMD: case ST_OMP_DECLARE_REDUCTION: \
1361 case ST_OMP_DECLARE_TARGET: case ST_OACC_ROUTINE
1362
1363 /* Block end statements. Errors associated with interchanging these
1364 are detected in gfc_match_end(). */
1365
1366 #define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
1367 case ST_END_PROGRAM: case ST_END_SUBROUTINE: \
1368 case ST_END_BLOCK: case ST_END_ASSOCIATE
1369
1370
1371 /* Push a new state onto the stack. */
1372
1373 static void
1374 push_state (gfc_state_data *p, gfc_compile_state new_state, gfc_symbol *sym)
1375 {
1376 p->state = new_state;
1377 p->previous = gfc_state_stack;
1378 p->sym = sym;
1379 p->head = p->tail = NULL;
1380 p->do_variable = NULL;
1381 if (p->state != COMP_DO && p->state != COMP_DO_CONCURRENT)
1382 p->ext.oacc_declare_clauses = NULL;
1383
1384 /* If this the state of a construct like BLOCK, DO or IF, the corresponding
1385 construct statement was accepted right before pushing the state. Thus,
1386 the construct's gfc_code is available as tail of the parent state. */
1387 gcc_assert (gfc_state_stack);
1388 p->construct = gfc_state_stack->tail;
1389
1390 gfc_state_stack = p;
1391 }
1392
1393
1394 /* Pop the current state. */
1395 static void
1396 pop_state (void)
1397 {
1398 gfc_state_stack = gfc_state_stack->previous;
1399 }
1400
1401
1402 /* Try to find the given state in the state stack. */
1403
1404 bool
1405 gfc_find_state (gfc_compile_state state)
1406 {
1407 gfc_state_data *p;
1408
1409 for (p = gfc_state_stack; p; p = p->previous)
1410 if (p->state == state)
1411 break;
1412
1413 return (p == NULL) ? false : true;
1414 }
1415
1416
1417 /* Starts a new level in the statement list. */
1418
1419 static gfc_code *
1420 new_level (gfc_code *q)
1421 {
1422 gfc_code *p;
1423
1424 p = q->block = gfc_get_code (EXEC_NOP);
1425
1426 gfc_state_stack->head = gfc_state_stack->tail = p;
1427
1428 return p;
1429 }
1430
1431
1432 /* Add the current new_st code structure and adds it to the current
1433 program unit. As a side-effect, it zeroes the new_st. */
1434
1435 static gfc_code *
1436 add_statement (void)
1437 {
1438 gfc_code *p;
1439
1440 p = XCNEW (gfc_code);
1441 *p = new_st;
1442
1443 p->loc = gfc_current_locus;
1444
1445 if (gfc_state_stack->head == NULL)
1446 gfc_state_stack->head = p;
1447 else
1448 gfc_state_stack->tail->next = p;
1449
1450 while (p->next != NULL)
1451 p = p->next;
1452
1453 gfc_state_stack->tail = p;
1454
1455 gfc_clear_new_st ();
1456
1457 return p;
1458 }
1459
1460
1461 /* Frees everything associated with the current statement. */
1462
1463 static void
1464 undo_new_statement (void)
1465 {
1466 gfc_free_statements (new_st.block);
1467 gfc_free_statements (new_st.next);
1468 gfc_free_statement (&new_st);
1469 gfc_clear_new_st ();
1470 }
1471
1472
1473 /* If the current statement has a statement label, make sure that it
1474 is allowed to, or should have one. */
1475
1476 static void
1477 check_statement_label (gfc_statement st)
1478 {
1479 gfc_sl_type type;
1480
1481 if (gfc_statement_label == NULL)
1482 {
1483 if (st == ST_FORMAT)
1484 gfc_error ("FORMAT statement at %L does not have a statement label",
1485 &new_st.loc);
1486 return;
1487 }
1488
1489 switch (st)
1490 {
1491 case ST_END_PROGRAM:
1492 case ST_END_FUNCTION:
1493 case ST_END_SUBROUTINE:
1494 case ST_ENDDO:
1495 case ST_ENDIF:
1496 case ST_END_SELECT:
1497 case ST_END_CRITICAL:
1498 case ST_END_BLOCK:
1499 case ST_END_ASSOCIATE:
1500 case_executable:
1501 case_exec_markers:
1502 if (st == ST_ENDDO || st == ST_CONTINUE)
1503 type = ST_LABEL_DO_TARGET;
1504 else
1505 type = ST_LABEL_TARGET;
1506 break;
1507
1508 case ST_FORMAT:
1509 type = ST_LABEL_FORMAT;
1510 break;
1511
1512 /* Statement labels are not restricted from appearing on a
1513 particular line. However, there are plenty of situations
1514 where the resulting label can't be referenced. */
1515
1516 default:
1517 type = ST_LABEL_BAD_TARGET;
1518 break;
1519 }
1520
1521 gfc_define_st_label (gfc_statement_label, type, &label_locus);
1522
1523 new_st.here = gfc_statement_label;
1524 }
1525
1526
1527 /* Figures out what the enclosing program unit is. This will be a
1528 function, subroutine, program, block data or module. */
1529
1530 gfc_state_data *
1531 gfc_enclosing_unit (gfc_compile_state * result)
1532 {
1533 gfc_state_data *p;
1534
1535 for (p = gfc_state_stack; p; p = p->previous)
1536 if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
1537 || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
1538 || p->state == COMP_PROGRAM)
1539 {
1540
1541 if (result != NULL)
1542 *result = p->state;
1543 return p;
1544 }
1545
1546 if (result != NULL)
1547 *result = COMP_PROGRAM;
1548 return NULL;
1549 }
1550
1551
1552 /* Translate a statement enum to a string. */
1553
1554 const char *
1555 gfc_ascii_statement (gfc_statement st)
1556 {
1557 const char *p;
1558
1559 switch (st)
1560 {
1561 case ST_ARITHMETIC_IF:
1562 p = _("arithmetic IF");
1563 break;
1564 case ST_ALLOCATE:
1565 p = "ALLOCATE";
1566 break;
1567 case ST_ASSOCIATE:
1568 p = "ASSOCIATE";
1569 break;
1570 case ST_ATTR_DECL:
1571 p = _("attribute declaration");
1572 break;
1573 case ST_BACKSPACE:
1574 p = "BACKSPACE";
1575 break;
1576 case ST_BLOCK:
1577 p = "BLOCK";
1578 break;
1579 case ST_BLOCK_DATA:
1580 p = "BLOCK DATA";
1581 break;
1582 case ST_CALL:
1583 p = "CALL";
1584 break;
1585 case ST_CASE:
1586 p = "CASE";
1587 break;
1588 case ST_CLOSE:
1589 p = "CLOSE";
1590 break;
1591 case ST_COMMON:
1592 p = "COMMON";
1593 break;
1594 case ST_CONTINUE:
1595 p = "CONTINUE";
1596 break;
1597 case ST_CONTAINS:
1598 p = "CONTAINS";
1599 break;
1600 case ST_CRITICAL:
1601 p = "CRITICAL";
1602 break;
1603 case ST_CYCLE:
1604 p = "CYCLE";
1605 break;
1606 case ST_DATA_DECL:
1607 p = _("data declaration");
1608 break;
1609 case ST_DATA:
1610 p = "DATA";
1611 break;
1612 case ST_DEALLOCATE:
1613 p = "DEALLOCATE";
1614 break;
1615 case ST_DERIVED_DECL:
1616 p = _("derived type declaration");
1617 break;
1618 case ST_DO:
1619 p = "DO";
1620 break;
1621 case ST_ELSE:
1622 p = "ELSE";
1623 break;
1624 case ST_ELSEIF:
1625 p = "ELSE IF";
1626 break;
1627 case ST_ELSEWHERE:
1628 p = "ELSEWHERE";
1629 break;
1630 case ST_END_ASSOCIATE:
1631 p = "END ASSOCIATE";
1632 break;
1633 case ST_END_BLOCK:
1634 p = "END BLOCK";
1635 break;
1636 case ST_END_BLOCK_DATA:
1637 p = "END BLOCK DATA";
1638 break;
1639 case ST_END_CRITICAL:
1640 p = "END CRITICAL";
1641 break;
1642 case ST_ENDDO:
1643 p = "END DO";
1644 break;
1645 case ST_END_FILE:
1646 p = "END FILE";
1647 break;
1648 case ST_END_FORALL:
1649 p = "END FORALL";
1650 break;
1651 case ST_END_FUNCTION:
1652 p = "END FUNCTION";
1653 break;
1654 case ST_ENDIF:
1655 p = "END IF";
1656 break;
1657 case ST_END_INTERFACE:
1658 p = "END INTERFACE";
1659 break;
1660 case ST_END_MODULE:
1661 p = "END MODULE";
1662 break;
1663 case ST_END_PROGRAM:
1664 p = "END PROGRAM";
1665 break;
1666 case ST_END_SELECT:
1667 p = "END SELECT";
1668 break;
1669 case ST_END_SUBROUTINE:
1670 p = "END SUBROUTINE";
1671 break;
1672 case ST_END_WHERE:
1673 p = "END WHERE";
1674 break;
1675 case ST_END_TYPE:
1676 p = "END TYPE";
1677 break;
1678 case ST_ENTRY:
1679 p = "ENTRY";
1680 break;
1681 case ST_EQUIVALENCE:
1682 p = "EQUIVALENCE";
1683 break;
1684 case ST_ERROR_STOP:
1685 p = "ERROR STOP";
1686 break;
1687 case ST_EXIT:
1688 p = "EXIT";
1689 break;
1690 case ST_FLUSH:
1691 p = "FLUSH";
1692 break;
1693 case ST_FORALL_BLOCK: /* Fall through */
1694 case ST_FORALL:
1695 p = "FORALL";
1696 break;
1697 case ST_FORMAT:
1698 p = "FORMAT";
1699 break;
1700 case ST_FUNCTION:
1701 p = "FUNCTION";
1702 break;
1703 case ST_GENERIC:
1704 p = "GENERIC";
1705 break;
1706 case ST_GOTO:
1707 p = "GOTO";
1708 break;
1709 case ST_IF_BLOCK:
1710 p = _("block IF");
1711 break;
1712 case ST_IMPLICIT:
1713 p = "IMPLICIT";
1714 break;
1715 case ST_IMPLICIT_NONE:
1716 p = "IMPLICIT NONE";
1717 break;
1718 case ST_IMPLIED_ENDDO:
1719 p = _("implied END DO");
1720 break;
1721 case ST_IMPORT:
1722 p = "IMPORT";
1723 break;
1724 case ST_INQUIRE:
1725 p = "INQUIRE";
1726 break;
1727 case ST_INTERFACE:
1728 p = "INTERFACE";
1729 break;
1730 case ST_LOCK:
1731 p = "LOCK";
1732 break;
1733 case ST_PARAMETER:
1734 p = "PARAMETER";
1735 break;
1736 case ST_PRIVATE:
1737 p = "PRIVATE";
1738 break;
1739 case ST_PUBLIC:
1740 p = "PUBLIC";
1741 break;
1742 case ST_MODULE:
1743 p = "MODULE";
1744 break;
1745 case ST_PAUSE:
1746 p = "PAUSE";
1747 break;
1748 case ST_MODULE_PROC:
1749 p = "MODULE PROCEDURE";
1750 break;
1751 case ST_NAMELIST:
1752 p = "NAMELIST";
1753 break;
1754 case ST_NULLIFY:
1755 p = "NULLIFY";
1756 break;
1757 case ST_OPEN:
1758 p = "OPEN";
1759 break;
1760 case ST_PROGRAM:
1761 p = "PROGRAM";
1762 break;
1763 case ST_PROCEDURE:
1764 p = "PROCEDURE";
1765 break;
1766 case ST_READ:
1767 p = "READ";
1768 break;
1769 case ST_RETURN:
1770 p = "RETURN";
1771 break;
1772 case ST_REWIND:
1773 p = "REWIND";
1774 break;
1775 case ST_STOP:
1776 p = "STOP";
1777 break;
1778 case ST_SYNC_ALL:
1779 p = "SYNC ALL";
1780 break;
1781 case ST_SYNC_IMAGES:
1782 p = "SYNC IMAGES";
1783 break;
1784 case ST_SYNC_MEMORY:
1785 p = "SYNC MEMORY";
1786 break;
1787 case ST_SUBROUTINE:
1788 p = "SUBROUTINE";
1789 break;
1790 case ST_TYPE:
1791 p = "TYPE";
1792 break;
1793 case ST_UNLOCK:
1794 p = "UNLOCK";
1795 break;
1796 case ST_USE:
1797 p = "USE";
1798 break;
1799 case ST_WHERE_BLOCK: /* Fall through */
1800 case ST_WHERE:
1801 p = "WHERE";
1802 break;
1803 case ST_WAIT:
1804 p = "WAIT";
1805 break;
1806 case ST_WRITE:
1807 p = "WRITE";
1808 break;
1809 case ST_ASSIGNMENT:
1810 p = _("assignment");
1811 break;
1812 case ST_POINTER_ASSIGNMENT:
1813 p = _("pointer assignment");
1814 break;
1815 case ST_SELECT_CASE:
1816 p = "SELECT CASE";
1817 break;
1818 case ST_SELECT_TYPE:
1819 p = "SELECT TYPE";
1820 break;
1821 case ST_TYPE_IS:
1822 p = "TYPE IS";
1823 break;
1824 case ST_CLASS_IS:
1825 p = "CLASS IS";
1826 break;
1827 case ST_SEQUENCE:
1828 p = "SEQUENCE";
1829 break;
1830 case ST_SIMPLE_IF:
1831 p = _("simple IF");
1832 break;
1833 case ST_STATEMENT_FUNCTION:
1834 p = "STATEMENT FUNCTION";
1835 break;
1836 case ST_LABEL_ASSIGNMENT:
1837 p = "LABEL ASSIGNMENT";
1838 break;
1839 case ST_ENUM:
1840 p = "ENUM DEFINITION";
1841 break;
1842 case ST_ENUMERATOR:
1843 p = "ENUMERATOR DEFINITION";
1844 break;
1845 case ST_END_ENUM:
1846 p = "END ENUM";
1847 break;
1848 case ST_OACC_PARALLEL_LOOP:
1849 p = "!$ACC PARALLEL LOOP";
1850 break;
1851 case ST_OACC_END_PARALLEL_LOOP:
1852 p = "!$ACC END PARALLEL LOOP";
1853 break;
1854 case ST_OACC_PARALLEL:
1855 p = "!$ACC PARALLEL";
1856 break;
1857 case ST_OACC_END_PARALLEL:
1858 p = "!$ACC END PARALLEL";
1859 break;
1860 case ST_OACC_KERNELS:
1861 p = "!$ACC KERNELS";
1862 break;
1863 case ST_OACC_END_KERNELS:
1864 p = "!$ACC END KERNELS";
1865 break;
1866 case ST_OACC_KERNELS_LOOP:
1867 p = "!$ACC KERNELS LOOP";
1868 break;
1869 case ST_OACC_END_KERNELS_LOOP:
1870 p = "!$ACC END KERNELS LOOP";
1871 break;
1872 case ST_OACC_DATA:
1873 p = "!$ACC DATA";
1874 break;
1875 case ST_OACC_END_DATA:
1876 p = "!$ACC END DATA";
1877 break;
1878 case ST_OACC_HOST_DATA:
1879 p = "!$ACC HOST_DATA";
1880 break;
1881 case ST_OACC_END_HOST_DATA:
1882 p = "!$ACC END HOST_DATA";
1883 break;
1884 case ST_OACC_LOOP:
1885 p = "!$ACC LOOP";
1886 break;
1887 case ST_OACC_END_LOOP:
1888 p = "!$ACC END LOOP";
1889 break;
1890 case ST_OACC_DECLARE:
1891 p = "!$ACC DECLARE";
1892 break;
1893 case ST_OACC_UPDATE:
1894 p = "!$ACC UPDATE";
1895 break;
1896 case ST_OACC_WAIT:
1897 p = "!$ACC WAIT";
1898 break;
1899 case ST_OACC_CACHE:
1900 p = "!$ACC CACHE";
1901 break;
1902 case ST_OACC_ENTER_DATA:
1903 p = "!$ACC ENTER DATA";
1904 break;
1905 case ST_OACC_EXIT_DATA:
1906 p = "!$ACC EXIT DATA";
1907 break;
1908 case ST_OACC_ROUTINE:
1909 p = "!$ACC ROUTINE";
1910 break;
1911 case ST_OMP_ATOMIC:
1912 p = "!$OMP ATOMIC";
1913 break;
1914 case ST_OMP_BARRIER:
1915 p = "!$OMP BARRIER";
1916 break;
1917 case ST_OMP_CANCEL:
1918 p = "!$OMP CANCEL";
1919 break;
1920 case ST_OMP_CANCELLATION_POINT:
1921 p = "!$OMP CANCELLATION POINT";
1922 break;
1923 case ST_OMP_CRITICAL:
1924 p = "!$OMP CRITICAL";
1925 break;
1926 case ST_OMP_DECLARE_REDUCTION:
1927 p = "!$OMP DECLARE REDUCTION";
1928 break;
1929 case ST_OMP_DECLARE_SIMD:
1930 p = "!$OMP DECLARE SIMD";
1931 break;
1932 case ST_OMP_DECLARE_TARGET:
1933 p = "!$OMP DECLARE TARGET";
1934 break;
1935 case ST_OMP_DISTRIBUTE:
1936 p = "!$OMP DISTRIBUTE";
1937 break;
1938 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
1939 p = "!$OMP DISTRIBUTE PARALLEL DO";
1940 break;
1941 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
1942 p = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
1943 break;
1944 case ST_OMP_DISTRIBUTE_SIMD:
1945 p = "!$OMP DISTRIBUTE SIMD";
1946 break;
1947 case ST_OMP_DO:
1948 p = "!$OMP DO";
1949 break;
1950 case ST_OMP_DO_SIMD:
1951 p = "!$OMP DO SIMD";
1952 break;
1953 case ST_OMP_END_ATOMIC:
1954 p = "!$OMP END ATOMIC";
1955 break;
1956 case ST_OMP_END_CRITICAL:
1957 p = "!$OMP END CRITICAL";
1958 break;
1959 case ST_OMP_END_DISTRIBUTE:
1960 p = "!$OMP END DISTRIBUTE";
1961 break;
1962 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO:
1963 p = "!$OMP END DISTRIBUTE PARALLEL DO";
1964 break;
1965 case ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD:
1966 p = "!$OMP END DISTRIBUTE PARALLEL DO SIMD";
1967 break;
1968 case ST_OMP_END_DISTRIBUTE_SIMD:
1969 p = "!$OMP END DISTRIBUTE SIMD";
1970 break;
1971 case ST_OMP_END_DO:
1972 p = "!$OMP END DO";
1973 break;
1974 case ST_OMP_END_DO_SIMD:
1975 p = "!$OMP END DO SIMD";
1976 break;
1977 case ST_OMP_END_SIMD:
1978 p = "!$OMP END SIMD";
1979 break;
1980 case ST_OMP_END_MASTER:
1981 p = "!$OMP END MASTER";
1982 break;
1983 case ST_OMP_END_ORDERED:
1984 p = "!$OMP END ORDERED";
1985 break;
1986 case ST_OMP_END_PARALLEL:
1987 p = "!$OMP END PARALLEL";
1988 break;
1989 case ST_OMP_END_PARALLEL_DO:
1990 p = "!$OMP END PARALLEL DO";
1991 break;
1992 case ST_OMP_END_PARALLEL_DO_SIMD:
1993 p = "!$OMP END PARALLEL DO SIMD";
1994 break;
1995 case ST_OMP_END_PARALLEL_SECTIONS:
1996 p = "!$OMP END PARALLEL SECTIONS";
1997 break;
1998 case ST_OMP_END_PARALLEL_WORKSHARE:
1999 p = "!$OMP END PARALLEL WORKSHARE";
2000 break;
2001 case ST_OMP_END_SECTIONS:
2002 p = "!$OMP END SECTIONS";
2003 break;
2004 case ST_OMP_END_SINGLE:
2005 p = "!$OMP END SINGLE";
2006 break;
2007 case ST_OMP_END_TASK:
2008 p = "!$OMP END TASK";
2009 break;
2010 case ST_OMP_END_TARGET:
2011 p = "!$OMP END TARGET";
2012 break;
2013 case ST_OMP_END_TARGET_DATA:
2014 p = "!$OMP END TARGET DATA";
2015 break;
2016 case ST_OMP_END_TARGET_TEAMS:
2017 p = "!$OMP END TARGET TEAMS";
2018 break;
2019 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE:
2020 p = "!$OMP END TARGET TEAMS DISTRIBUTE";
2021 break;
2022 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2023 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO";
2024 break;
2025 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2026 p = "!$OMP END TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2027 break;
2028 case ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD:
2029 p = "!$OMP END TARGET TEAMS DISTRIBUTE SIMD";
2030 break;
2031 case ST_OMP_END_TASKGROUP:
2032 p = "!$OMP END TASKGROUP";
2033 break;
2034 case ST_OMP_END_TEAMS:
2035 p = "!$OMP END TEAMS";
2036 break;
2037 case ST_OMP_END_TEAMS_DISTRIBUTE:
2038 p = "!$OMP END TEAMS DISTRIBUTE";
2039 break;
2040 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO:
2041 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO";
2042 break;
2043 case ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2044 p = "!$OMP END TEAMS DISTRIBUTE PARALLEL DO SIMD";
2045 break;
2046 case ST_OMP_END_TEAMS_DISTRIBUTE_SIMD:
2047 p = "!$OMP END TEAMS DISTRIBUTE SIMD";
2048 break;
2049 case ST_OMP_END_WORKSHARE:
2050 p = "!$OMP END WORKSHARE";
2051 break;
2052 case ST_OMP_FLUSH:
2053 p = "!$OMP FLUSH";
2054 break;
2055 case ST_OMP_MASTER:
2056 p = "!$OMP MASTER";
2057 break;
2058 case ST_OMP_ORDERED:
2059 p = "!$OMP ORDERED";
2060 break;
2061 case ST_OMP_PARALLEL:
2062 p = "!$OMP PARALLEL";
2063 break;
2064 case ST_OMP_PARALLEL_DO:
2065 p = "!$OMP PARALLEL DO";
2066 break;
2067 case ST_OMP_PARALLEL_DO_SIMD:
2068 p = "!$OMP PARALLEL DO SIMD";
2069 break;
2070 case ST_OMP_PARALLEL_SECTIONS:
2071 p = "!$OMP PARALLEL SECTIONS";
2072 break;
2073 case ST_OMP_PARALLEL_WORKSHARE:
2074 p = "!$OMP PARALLEL WORKSHARE";
2075 break;
2076 case ST_OMP_SECTIONS:
2077 p = "!$OMP SECTIONS";
2078 break;
2079 case ST_OMP_SECTION:
2080 p = "!$OMP SECTION";
2081 break;
2082 case ST_OMP_SIMD:
2083 p = "!$OMP SIMD";
2084 break;
2085 case ST_OMP_SINGLE:
2086 p = "!$OMP SINGLE";
2087 break;
2088 case ST_OMP_TARGET:
2089 p = "!$OMP TARGET";
2090 break;
2091 case ST_OMP_TARGET_DATA:
2092 p = "!$OMP TARGET DATA";
2093 break;
2094 case ST_OMP_TARGET_TEAMS:
2095 p = "!$OMP TARGET TEAMS";
2096 break;
2097 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
2098 p = "!$OMP TARGET TEAMS DISTRIBUTE";
2099 break;
2100 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2101 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
2102 break;
2103 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2104 p = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
2105 break;
2106 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2107 p = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
2108 break;
2109 case ST_OMP_TARGET_UPDATE:
2110 p = "!$OMP TARGET UPDATE";
2111 break;
2112 case ST_OMP_TASK:
2113 p = "!$OMP TASK";
2114 break;
2115 case ST_OMP_TASKGROUP:
2116 p = "!$OMP TASKGROUP";
2117 break;
2118 case ST_OMP_TASKWAIT:
2119 p = "!$OMP TASKWAIT";
2120 break;
2121 case ST_OMP_TASKYIELD:
2122 p = "!$OMP TASKYIELD";
2123 break;
2124 case ST_OMP_TEAMS:
2125 p = "!$OMP TEAMS";
2126 break;
2127 case ST_OMP_TEAMS_DISTRIBUTE:
2128 p = "!$OMP TEAMS DISTRIBUTE";
2129 break;
2130 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2131 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
2132 break;
2133 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2134 p = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
2135 break;
2136 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
2137 p = "!$OMP TEAMS DISTRIBUTE SIMD";
2138 break;
2139 case ST_OMP_THREADPRIVATE:
2140 p = "!$OMP THREADPRIVATE";
2141 break;
2142 case ST_OMP_WORKSHARE:
2143 p = "!$OMP WORKSHARE";
2144 break;
2145 default:
2146 gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
2147 }
2148
2149 return p;
2150 }
2151
2152
2153 /* Create a symbol for the main program and assign it to ns->proc_name. */
2154
2155 static void
2156 main_program_symbol (gfc_namespace *ns, const char *name)
2157 {
2158 gfc_symbol *main_program;
2159 symbol_attribute attr;
2160
2161 gfc_get_symbol (name, ns, &main_program);
2162 gfc_clear_attr (&attr);
2163 attr.flavor = FL_PROGRAM;
2164 attr.proc = PROC_UNKNOWN;
2165 attr.subroutine = 1;
2166 attr.access = ACCESS_PUBLIC;
2167 attr.is_main_program = 1;
2168 main_program->attr = attr;
2169 main_program->declared_at = gfc_current_locus;
2170 ns->proc_name = main_program;
2171 gfc_commit_symbols ();
2172 }
2173
2174
2175 /* Do whatever is necessary to accept the last statement. */
2176
2177 static void
2178 accept_statement (gfc_statement st)
2179 {
2180 switch (st)
2181 {
2182 case ST_IMPLICIT_NONE:
2183 case ST_IMPLICIT:
2184 break;
2185
2186 case ST_FUNCTION:
2187 case ST_SUBROUTINE:
2188 case ST_MODULE:
2189 gfc_current_ns->proc_name = gfc_new_block;
2190 break;
2191
2192 /* If the statement is the end of a block, lay down a special code
2193 that allows a branch to the end of the block from within the
2194 construct. IF and SELECT are treated differently from DO
2195 (where EXEC_NOP is added inside the loop) for two
2196 reasons:
2197 1. END DO has a meaning in the sense that after a GOTO to
2198 it, the loop counter must be increased.
2199 2. IF blocks and SELECT blocks can consist of multiple
2200 parallel blocks (IF ... ELSE IF ... ELSE ... END IF).
2201 Putting the label before the END IF would make the jump
2202 from, say, the ELSE IF block to the END IF illegal. */
2203
2204 case ST_ENDIF:
2205 case ST_END_SELECT:
2206 case ST_END_CRITICAL:
2207 if (gfc_statement_label != NULL)
2208 {
2209 new_st.op = EXEC_END_NESTED_BLOCK;
2210 add_statement ();
2211 }
2212 break;
2213
2214 /* In the case of BLOCK and ASSOCIATE blocks, there cannot be more than
2215 one parallel block. Thus, we add the special code to the nested block
2216 itself, instead of the parent one. */
2217 case ST_END_BLOCK:
2218 case ST_END_ASSOCIATE:
2219 if (gfc_statement_label != NULL)
2220 {
2221 new_st.op = EXEC_END_BLOCK;
2222 add_statement ();
2223 }
2224 break;
2225
2226 /* The end-of-program unit statements do not get the special
2227 marker and require a statement of some sort if they are a
2228 branch target. */
2229
2230 case ST_END_PROGRAM:
2231 case ST_END_FUNCTION:
2232 case ST_END_SUBROUTINE:
2233 if (gfc_statement_label != NULL)
2234 {
2235 new_st.op = EXEC_RETURN;
2236 add_statement ();
2237 }
2238 else
2239 {
2240 new_st.op = EXEC_END_PROCEDURE;
2241 add_statement ();
2242 }
2243
2244 break;
2245
2246 case ST_ENTRY:
2247 case_executable:
2248 case_exec_markers:
2249 add_statement ();
2250 break;
2251
2252 default:
2253 break;
2254 }
2255
2256 gfc_commit_symbols ();
2257 gfc_warning_check ();
2258 gfc_clear_new_st ();
2259 }
2260
2261
2262 /* Undo anything tentative that has been built for the current
2263 statement. */
2264
2265 static void
2266 reject_statement (void)
2267 {
2268 /* Revert to the previous charlen chain. */
2269 gfc_free_charlen (gfc_current_ns->cl_list, gfc_current_ns->old_cl_list);
2270 gfc_current_ns->cl_list = gfc_current_ns->old_cl_list;
2271
2272 gfc_free_equiv_until (gfc_current_ns->equiv, gfc_current_ns->old_equiv);
2273 gfc_current_ns->equiv = gfc_current_ns->old_equiv;
2274
2275 gfc_reject_data (gfc_current_ns);
2276
2277 gfc_new_block = NULL;
2278 gfc_undo_symbols ();
2279 gfc_clear_warning ();
2280 undo_new_statement ();
2281 }
2282
2283
2284 /* Generic complaint about an out of order statement. We also do
2285 whatever is necessary to clean up. */
2286
2287 static void
2288 unexpected_statement (gfc_statement st)
2289 {
2290 gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
2291
2292 reject_statement ();
2293 }
2294
2295
2296 /* Given the next statement seen by the matcher, make sure that it is
2297 in proper order with the last. This subroutine is initialized by
2298 calling it with an argument of ST_NONE. If there is a problem, we
2299 issue an error and return false. Otherwise we return true.
2300
2301 Individual parsers need to verify that the statements seen are
2302 valid before calling here, i.e., ENTRY statements are not allowed in
2303 INTERFACE blocks. The following diagram is taken from the standard:
2304
2305 +---------------------------------------+
2306 | program subroutine function module |
2307 +---------------------------------------+
2308 | use |
2309 +---------------------------------------+
2310 | import |
2311 +---------------------------------------+
2312 | | implicit none |
2313 | +-----------+------------------+
2314 | | parameter | implicit |
2315 | +-----------+------------------+
2316 | format | | derived type |
2317 | entry | parameter | interface |
2318 | | data | specification |
2319 | | | statement func |
2320 | +-----------+------------------+
2321 | | data | executable |
2322 +--------+-----------+------------------+
2323 | contains |
2324 +---------------------------------------+
2325 | internal module/subprogram |
2326 +---------------------------------------+
2327 | end |
2328 +---------------------------------------+
2329
2330 */
2331
2332 enum state_order
2333 {
2334 ORDER_START,
2335 ORDER_USE,
2336 ORDER_IMPORT,
2337 ORDER_IMPLICIT_NONE,
2338 ORDER_IMPLICIT,
2339 ORDER_SPEC,
2340 ORDER_EXEC
2341 };
2342
2343 typedef struct
2344 {
2345 enum state_order state;
2346 gfc_statement last_statement;
2347 locus where;
2348 }
2349 st_state;
2350
2351 static bool
2352 verify_st_order (st_state *p, gfc_statement st, bool silent)
2353 {
2354
2355 switch (st)
2356 {
2357 case ST_NONE:
2358 p->state = ORDER_START;
2359 break;
2360
2361 case ST_USE:
2362 if (p->state > ORDER_USE)
2363 goto order;
2364 p->state = ORDER_USE;
2365 break;
2366
2367 case ST_IMPORT:
2368 if (p->state > ORDER_IMPORT)
2369 goto order;
2370 p->state = ORDER_IMPORT;
2371 break;
2372
2373 case ST_IMPLICIT_NONE:
2374 if (p->state > ORDER_IMPLICIT)
2375 goto order;
2376
2377 /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
2378 statement disqualifies a USE but not an IMPLICIT NONE.
2379 Duplicate IMPLICIT NONEs are caught when the implicit types
2380 are set. */
2381
2382 p->state = ORDER_IMPLICIT_NONE;
2383 break;
2384
2385 case ST_IMPLICIT:
2386 if (p->state > ORDER_IMPLICIT)
2387 goto order;
2388 p->state = ORDER_IMPLICIT;
2389 break;
2390
2391 case ST_FORMAT:
2392 case ST_ENTRY:
2393 if (p->state < ORDER_IMPLICIT_NONE)
2394 p->state = ORDER_IMPLICIT_NONE;
2395 break;
2396
2397 case ST_PARAMETER:
2398 if (p->state >= ORDER_EXEC)
2399 goto order;
2400 if (p->state < ORDER_IMPLICIT)
2401 p->state = ORDER_IMPLICIT;
2402 break;
2403
2404 case ST_DATA:
2405 if (p->state < ORDER_SPEC)
2406 p->state = ORDER_SPEC;
2407 break;
2408
2409 case ST_PUBLIC:
2410 case ST_PRIVATE:
2411 case ST_DERIVED_DECL:
2412 case ST_OACC_DECLARE:
2413 case_decl:
2414 if (p->state >= ORDER_EXEC)
2415 goto order;
2416 if (p->state < ORDER_SPEC)
2417 p->state = ORDER_SPEC;
2418 break;
2419
2420 case_executable:
2421 case_exec_markers:
2422 if (p->state < ORDER_EXEC)
2423 p->state = ORDER_EXEC;
2424 break;
2425
2426 default:
2427 return false;
2428 }
2429
2430 /* All is well, record the statement in case we need it next time. */
2431 p->where = gfc_current_locus;
2432 p->last_statement = st;
2433 return true;
2434
2435 order:
2436 if (!silent)
2437 gfc_error ("%s statement at %C cannot follow %s statement at %L",
2438 gfc_ascii_statement (st),
2439 gfc_ascii_statement (p->last_statement), &p->where);
2440
2441 return false;
2442 }
2443
2444
2445 /* Handle an unexpected end of file. This is a show-stopper... */
2446
2447 static void unexpected_eof (void) ATTRIBUTE_NORETURN;
2448
2449 static void
2450 unexpected_eof (void)
2451 {
2452 gfc_state_data *p;
2453
2454 gfc_error ("Unexpected end of file in %qs", gfc_source_file);
2455
2456 /* Memory cleanup. Move to "second to last". */
2457 for (p = gfc_state_stack; p && p->previous && p->previous->previous;
2458 p = p->previous);
2459
2460 gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
2461 gfc_done_2 ();
2462
2463 longjmp (eof_buf, 1);
2464 }
2465
2466
2467 /* Parse the CONTAINS section of a derived type definition. */
2468
2469 gfc_access gfc_typebound_default_access;
2470
2471 static bool
2472 parse_derived_contains (void)
2473 {
2474 gfc_state_data s;
2475 bool seen_private = false;
2476 bool seen_comps = false;
2477 bool error_flag = false;
2478 bool to_finish;
2479
2480 gcc_assert (gfc_current_state () == COMP_DERIVED);
2481 gcc_assert (gfc_current_block ());
2482
2483 /* Derived-types with SEQUENCE and/or BIND(C) must not have a CONTAINS
2484 section. */
2485 if (gfc_current_block ()->attr.sequence)
2486 gfc_error ("Derived-type %qs with SEQUENCE must not have a CONTAINS"
2487 " section at %C", gfc_current_block ()->name);
2488 if (gfc_current_block ()->attr.is_bind_c)
2489 gfc_error ("Derived-type %qs with BIND(C) must not have a CONTAINS"
2490 " section at %C", gfc_current_block ()->name);
2491
2492 accept_statement (ST_CONTAINS);
2493 push_state (&s, COMP_DERIVED_CONTAINS, NULL);
2494
2495 gfc_typebound_default_access = ACCESS_PUBLIC;
2496
2497 to_finish = false;
2498 while (!to_finish)
2499 {
2500 gfc_statement st;
2501 st = next_statement ();
2502 switch (st)
2503 {
2504 case ST_NONE:
2505 unexpected_eof ();
2506 break;
2507
2508 case ST_DATA_DECL:
2509 gfc_error ("Components in TYPE at %C must precede CONTAINS");
2510 goto error;
2511
2512 case ST_PROCEDURE:
2513 if (!gfc_notify_std (GFC_STD_F2003, "Type-bound procedure at %C"))
2514 goto error;
2515
2516 accept_statement (ST_PROCEDURE);
2517 seen_comps = true;
2518 break;
2519
2520 case ST_GENERIC:
2521 if (!gfc_notify_std (GFC_STD_F2003, "GENERIC binding at %C"))
2522 goto error;
2523
2524 accept_statement (ST_GENERIC);
2525 seen_comps = true;
2526 break;
2527
2528 case ST_FINAL:
2529 if (!gfc_notify_std (GFC_STD_F2003, "FINAL procedure declaration"
2530 " at %C"))
2531 goto error;
2532
2533 accept_statement (ST_FINAL);
2534 seen_comps = true;
2535 break;
2536
2537 case ST_END_TYPE:
2538 to_finish = true;
2539
2540 if (!seen_comps
2541 && (!gfc_notify_std(GFC_STD_F2008, "Derived type definition "
2542 "at %C with empty CONTAINS section")))
2543 goto error;
2544
2545 /* ST_END_TYPE is accepted by parse_derived after return. */
2546 break;
2547
2548 case ST_PRIVATE:
2549 if (!gfc_find_state (COMP_MODULE))
2550 {
2551 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2552 "a MODULE");
2553 goto error;
2554 }
2555
2556 if (seen_comps)
2557 {
2558 gfc_error ("PRIVATE statement at %C must precede procedure"
2559 " bindings");
2560 goto error;
2561 }
2562
2563 if (seen_private)
2564 {
2565 gfc_error ("Duplicate PRIVATE statement at %C");
2566 goto error;
2567 }
2568
2569 accept_statement (ST_PRIVATE);
2570 gfc_typebound_default_access = ACCESS_PRIVATE;
2571 seen_private = true;
2572 break;
2573
2574 case ST_SEQUENCE:
2575 gfc_error ("SEQUENCE statement at %C must precede CONTAINS");
2576 goto error;
2577
2578 case ST_CONTAINS:
2579 gfc_error ("Already inside a CONTAINS block at %C");
2580 goto error;
2581
2582 default:
2583 unexpected_statement (st);
2584 break;
2585 }
2586
2587 continue;
2588
2589 error:
2590 error_flag = true;
2591 reject_statement ();
2592 }
2593
2594 pop_state ();
2595 gcc_assert (gfc_current_state () == COMP_DERIVED);
2596
2597 return error_flag;
2598 }
2599
2600
2601 /* Parse a derived type. */
2602
2603 static void
2604 parse_derived (void)
2605 {
2606 int compiling_type, seen_private, seen_sequence, seen_component;
2607 gfc_statement st;
2608 gfc_state_data s;
2609 gfc_symbol *sym;
2610 gfc_component *c, *lock_comp = NULL;
2611
2612 accept_statement (ST_DERIVED_DECL);
2613 push_state (&s, COMP_DERIVED, gfc_new_block);
2614
2615 gfc_new_block->component_access = ACCESS_PUBLIC;
2616 seen_private = 0;
2617 seen_sequence = 0;
2618 seen_component = 0;
2619
2620 compiling_type = 1;
2621
2622 while (compiling_type)
2623 {
2624 st = next_statement ();
2625 switch (st)
2626 {
2627 case ST_NONE:
2628 unexpected_eof ();
2629
2630 case ST_DATA_DECL:
2631 case ST_PROCEDURE:
2632 accept_statement (st);
2633 seen_component = 1;
2634 break;
2635
2636 case ST_FINAL:
2637 gfc_error ("FINAL declaration at %C must be inside CONTAINS");
2638 break;
2639
2640 case ST_END_TYPE:
2641 endType:
2642 compiling_type = 0;
2643
2644 if (!seen_component)
2645 gfc_notify_std (GFC_STD_F2003, "Derived type "
2646 "definition at %C without components");
2647
2648 accept_statement (ST_END_TYPE);
2649 break;
2650
2651 case ST_PRIVATE:
2652 if (!gfc_find_state (COMP_MODULE))
2653 {
2654 gfc_error ("PRIVATE statement in TYPE at %C must be inside "
2655 "a MODULE");
2656 break;
2657 }
2658
2659 if (seen_component)
2660 {
2661 gfc_error ("PRIVATE statement at %C must precede "
2662 "structure components");
2663 break;
2664 }
2665
2666 if (seen_private)
2667 gfc_error ("Duplicate PRIVATE statement at %C");
2668
2669 s.sym->component_access = ACCESS_PRIVATE;
2670
2671 accept_statement (ST_PRIVATE);
2672 seen_private = 1;
2673 break;
2674
2675 case ST_SEQUENCE:
2676 if (seen_component)
2677 {
2678 gfc_error ("SEQUENCE statement at %C must precede "
2679 "structure components");
2680 break;
2681 }
2682
2683 if (gfc_current_block ()->attr.sequence)
2684 gfc_warning (0, "SEQUENCE attribute at %C already specified in "
2685 "TYPE statement");
2686
2687 if (seen_sequence)
2688 {
2689 gfc_error ("Duplicate SEQUENCE statement at %C");
2690 }
2691
2692 seen_sequence = 1;
2693 gfc_add_sequence (&gfc_current_block ()->attr,
2694 gfc_current_block ()->name, NULL);
2695 break;
2696
2697 case ST_CONTAINS:
2698 gfc_notify_std (GFC_STD_F2003,
2699 "CONTAINS block in derived type"
2700 " definition at %C");
2701
2702 accept_statement (ST_CONTAINS);
2703 parse_derived_contains ();
2704 goto endType;
2705
2706 default:
2707 unexpected_statement (st);
2708 break;
2709 }
2710 }
2711
2712 /* need to verify that all fields of the derived type are
2713 * interoperable with C if the type is declared to be bind(c)
2714 */
2715 sym = gfc_current_block ();
2716 for (c = sym->components; c; c = c->next)
2717 {
2718 bool coarray, lock_type, allocatable, pointer;
2719 coarray = lock_type = allocatable = pointer = false;
2720
2721 /* Look for allocatable components. */
2722 if (c->attr.allocatable
2723 || (c->ts.type == BT_CLASS && c->attr.class_ok
2724 && CLASS_DATA (c)->attr.allocatable)
2725 || (c->ts.type == BT_DERIVED && !c->attr.pointer
2726 && c->ts.u.derived->attr.alloc_comp))
2727 {
2728 allocatable = true;
2729 sym->attr.alloc_comp = 1;
2730 }
2731
2732 /* Look for pointer components. */
2733 if (c->attr.pointer
2734 || (c->ts.type == BT_CLASS && c->attr.class_ok
2735 && CLASS_DATA (c)->attr.class_pointer)
2736 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pointer_comp))
2737 {
2738 pointer = true;
2739 sym->attr.pointer_comp = 1;
2740 }
2741
2742 /* Look for procedure pointer components. */
2743 if (c->attr.proc_pointer
2744 || (c->ts.type == BT_DERIVED
2745 && c->ts.u.derived->attr.proc_pointer_comp))
2746 sym->attr.proc_pointer_comp = 1;
2747
2748 /* Looking for coarray components. */
2749 if (c->attr.codimension
2750 || (c->ts.type == BT_CLASS && c->attr.class_ok
2751 && CLASS_DATA (c)->attr.codimension))
2752 {
2753 coarray = true;
2754 sym->attr.coarray_comp = 1;
2755 }
2756
2757 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
2758 && !c->attr.pointer)
2759 {
2760 coarray = true;
2761 sym->attr.coarray_comp = 1;
2762 }
2763
2764 /* Looking for lock_type components. */
2765 if ((c->ts.type == BT_DERIVED
2766 && c->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2767 && c->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2768 || (c->ts.type == BT_CLASS && c->attr.class_ok
2769 && CLASS_DATA (c)->ts.u.derived->from_intmod
2770 == INTMOD_ISO_FORTRAN_ENV
2771 && CLASS_DATA (c)->ts.u.derived->intmod_sym_id
2772 == ISOFORTRAN_LOCK_TYPE)
2773 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.lock_comp
2774 && !allocatable && !pointer))
2775 {
2776 lock_type = 1;
2777 lock_comp = c;
2778 sym->attr.lock_comp = 1;
2779 }
2780
2781 /* Check for F2008, C1302 - and recall that pointers may not be coarrays
2782 (5.3.14) and that subobjects of coarray are coarray themselves (2.4.7),
2783 unless there are nondirect [allocatable or pointer] components
2784 involved (cf. 1.3.33.1 and 1.3.33.3). */
2785
2786 if (pointer && !coarray && lock_type)
2787 gfc_error ("Component %s at %L of type LOCK_TYPE must have a "
2788 "codimension or be a subcomponent of a coarray, "
2789 "which is not possible as the component has the "
2790 "pointer attribute", c->name, &c->loc);
2791 else if (pointer && !coarray && c->ts.type == BT_DERIVED
2792 && c->ts.u.derived->attr.lock_comp)
2793 gfc_error ("Pointer component %s at %L has a noncoarray subcomponent "
2794 "of type LOCK_TYPE, which must have a codimension or be a "
2795 "subcomponent of a coarray", c->name, &c->loc);
2796
2797 if (lock_type && allocatable && !coarray)
2798 gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
2799 "a codimension", c->name, &c->loc);
2800 else if (lock_type && allocatable && c->ts.type == BT_DERIVED
2801 && c->ts.u.derived->attr.lock_comp)
2802 gfc_error ("Allocatable component %s at %L must have a codimension as "
2803 "it has a noncoarray subcomponent of type LOCK_TYPE",
2804 c->name, &c->loc);
2805
2806 if (sym->attr.coarray_comp && !coarray && lock_type)
2807 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2808 "subcomponent of type LOCK_TYPE must have a codimension or "
2809 "be a subcomponent of a coarray. (Variables of type %s may "
2810 "not have a codimension as already a coarray "
2811 "subcomponent exists)", c->name, &c->loc, sym->name);
2812
2813 if (sym->attr.lock_comp && coarray && !lock_type)
2814 gfc_error ("Noncoarray component %s at %L of type LOCK_TYPE or with "
2815 "subcomponent of type LOCK_TYPE must have a codimension or "
2816 "be a subcomponent of a coarray. (Variables of type %s may "
2817 "not have a codimension as %s at %L has a codimension or a "
2818 "coarray subcomponent)", lock_comp->name, &lock_comp->loc,
2819 sym->name, c->name, &c->loc);
2820
2821 /* Look for private components. */
2822 if (sym->component_access == ACCESS_PRIVATE
2823 || c->attr.access == ACCESS_PRIVATE
2824 || (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.private_comp))
2825 sym->attr.private_comp = 1;
2826 }
2827
2828 if (!seen_component)
2829 sym->attr.zero_comp = 1;
2830
2831 pop_state ();
2832 }
2833
2834
2835 /* Parse an ENUM. */
2836
2837 static void
2838 parse_enum (void)
2839 {
2840 gfc_statement st;
2841 int compiling_enum;
2842 gfc_state_data s;
2843 int seen_enumerator = 0;
2844
2845 push_state (&s, COMP_ENUM, gfc_new_block);
2846
2847 compiling_enum = 1;
2848
2849 while (compiling_enum)
2850 {
2851 st = next_statement ();
2852 switch (st)
2853 {
2854 case ST_NONE:
2855 unexpected_eof ();
2856 break;
2857
2858 case ST_ENUMERATOR:
2859 seen_enumerator = 1;
2860 accept_statement (st);
2861 break;
2862
2863 case ST_END_ENUM:
2864 compiling_enum = 0;
2865 if (!seen_enumerator)
2866 gfc_error ("ENUM declaration at %C has no ENUMERATORS");
2867 accept_statement (st);
2868 break;
2869
2870 default:
2871 gfc_free_enum_history ();
2872 unexpected_statement (st);
2873 break;
2874 }
2875 }
2876 pop_state ();
2877 }
2878
2879
2880 /* Parse an interface. We must be able to deal with the possibility
2881 of recursive interfaces. The parse_spec() subroutine is mutually
2882 recursive with parse_interface(). */
2883
2884 static gfc_statement parse_spec (gfc_statement);
2885
2886 static void
2887 parse_interface (void)
2888 {
2889 gfc_compile_state new_state = COMP_NONE, current_state;
2890 gfc_symbol *prog_unit, *sym;
2891 gfc_interface_info save;
2892 gfc_state_data s1, s2;
2893 gfc_statement st;
2894
2895 accept_statement (ST_INTERFACE);
2896
2897 current_interface.ns = gfc_current_ns;
2898 save = current_interface;
2899
2900 sym = (current_interface.type == INTERFACE_GENERIC
2901 || current_interface.type == INTERFACE_USER_OP)
2902 ? gfc_new_block : NULL;
2903
2904 push_state (&s1, COMP_INTERFACE, sym);
2905 current_state = COMP_NONE;
2906
2907 loop:
2908 gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
2909
2910 st = next_statement ();
2911 switch (st)
2912 {
2913 case ST_NONE:
2914 unexpected_eof ();
2915
2916 case ST_SUBROUTINE:
2917 case ST_FUNCTION:
2918 if (st == ST_SUBROUTINE)
2919 new_state = COMP_SUBROUTINE;
2920 else if (st == ST_FUNCTION)
2921 new_state = COMP_FUNCTION;
2922 if (gfc_new_block->attr.pointer)
2923 {
2924 gfc_new_block->attr.pointer = 0;
2925 gfc_new_block->attr.proc_pointer = 1;
2926 }
2927 if (!gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
2928 gfc_new_block->formal, NULL))
2929 {
2930 reject_statement ();
2931 gfc_free_namespace (gfc_current_ns);
2932 goto loop;
2933 }
2934 break;
2935
2936 case ST_PROCEDURE:
2937 case ST_MODULE_PROC: /* The module procedure matcher makes
2938 sure the context is correct. */
2939 accept_statement (st);
2940 gfc_free_namespace (gfc_current_ns);
2941 goto loop;
2942
2943 case ST_END_INTERFACE:
2944 gfc_free_namespace (gfc_current_ns);
2945 gfc_current_ns = current_interface.ns;
2946 goto done;
2947
2948 default:
2949 gfc_error ("Unexpected %s statement in INTERFACE block at %C",
2950 gfc_ascii_statement (st));
2951 reject_statement ();
2952 gfc_free_namespace (gfc_current_ns);
2953 goto loop;
2954 }
2955
2956
2957 /* Make sure that the generic name has the right attribute. */
2958 if (current_interface.type == INTERFACE_GENERIC
2959 && current_state == COMP_NONE)
2960 {
2961 if (new_state == COMP_FUNCTION && sym)
2962 gfc_add_function (&sym->attr, sym->name, NULL);
2963 else if (new_state == COMP_SUBROUTINE && sym)
2964 gfc_add_subroutine (&sym->attr, sym->name, NULL);
2965
2966 current_state = new_state;
2967 }
2968
2969 if (current_interface.type == INTERFACE_ABSTRACT)
2970 {
2971 gfc_add_abstract (&gfc_new_block->attr, &gfc_current_locus);
2972 if (gfc_is_intrinsic_typename (gfc_new_block->name))
2973 gfc_error ("Name %qs of ABSTRACT INTERFACE at %C "
2974 "cannot be the same as an intrinsic type",
2975 gfc_new_block->name);
2976 }
2977
2978 push_state (&s2, new_state, gfc_new_block);
2979 accept_statement (st);
2980 prog_unit = gfc_new_block;
2981 prog_unit->formal_ns = gfc_current_ns;
2982 if (prog_unit == prog_unit->formal_ns->proc_name
2983 && prog_unit->ns != prog_unit->formal_ns)
2984 prog_unit->refs++;
2985
2986 decl:
2987 /* Read data declaration statements. */
2988 st = parse_spec (ST_NONE);
2989
2990 /* Since the interface block does not permit an IMPLICIT statement,
2991 the default type for the function or the result must be taken
2992 from the formal namespace. */
2993 if (new_state == COMP_FUNCTION)
2994 {
2995 if (prog_unit->result == prog_unit
2996 && prog_unit->ts.type == BT_UNKNOWN)
2997 gfc_set_default_type (prog_unit, 1, prog_unit->formal_ns);
2998 else if (prog_unit->result != prog_unit
2999 && prog_unit->result->ts.type == BT_UNKNOWN)
3000 gfc_set_default_type (prog_unit->result, 1,
3001 prog_unit->formal_ns);
3002 }
3003
3004 if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
3005 {
3006 gfc_error ("Unexpected %s statement at %C in INTERFACE body",
3007 gfc_ascii_statement (st));
3008 reject_statement ();
3009 goto decl;
3010 }
3011
3012 /* Add EXTERNAL attribute to function or subroutine. */
3013 if (current_interface.type != INTERFACE_ABSTRACT && !prog_unit->attr.dummy)
3014 gfc_add_external (&prog_unit->attr, &gfc_current_locus);
3015
3016 current_interface = save;
3017 gfc_add_interface (prog_unit);
3018 pop_state ();
3019
3020 if (current_interface.ns
3021 && current_interface.ns->proc_name
3022 && strcmp (current_interface.ns->proc_name->name,
3023 prog_unit->name) == 0)
3024 gfc_error ("INTERFACE procedure %qs at %L has the same name as the "
3025 "enclosing procedure", prog_unit->name,
3026 &current_interface.ns->proc_name->declared_at);
3027
3028 goto loop;
3029
3030 done:
3031 pop_state ();
3032 }
3033
3034
3035 /* Associate function characteristics by going back to the function
3036 declaration and rematching the prefix. */
3037
3038 static match
3039 match_deferred_characteristics (gfc_typespec * ts)
3040 {
3041 locus loc;
3042 match m = MATCH_ERROR;
3043 char name[GFC_MAX_SYMBOL_LEN + 1];
3044
3045 loc = gfc_current_locus;
3046
3047 gfc_current_locus = gfc_current_block ()->declared_at;
3048
3049 gfc_clear_error ();
3050 gfc_buffer_error (true);
3051 m = gfc_match_prefix (ts);
3052 gfc_buffer_error (false);
3053
3054 if (ts->type == BT_DERIVED)
3055 {
3056 ts->kind = 0;
3057
3058 if (!ts->u.derived)
3059 m = MATCH_ERROR;
3060 }
3061
3062 /* Only permit one go at the characteristic association. */
3063 if (ts->kind == -1)
3064 ts->kind = 0;
3065
3066 /* Set the function locus correctly. If we have not found the
3067 function name, there is an error. */
3068 if (m == MATCH_YES
3069 && gfc_match ("function% %n", name) == MATCH_YES
3070 && strcmp (name, gfc_current_block ()->name) == 0)
3071 {
3072 gfc_current_block ()->declared_at = gfc_current_locus;
3073 gfc_commit_symbols ();
3074 }
3075 else
3076 {
3077 gfc_error_check ();
3078 gfc_undo_symbols ();
3079 }
3080
3081 gfc_current_locus =loc;
3082 return m;
3083 }
3084
3085
3086 /* Check specification-expressions in the function result of the currently
3087 parsed block and ensure they are typed (give an IMPLICIT type if necessary).
3088 For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
3089 scope are not yet parsed so this has to be delayed up to parse_spec. */
3090
3091 static void
3092 check_function_result_typed (void)
3093 {
3094 gfc_typespec* ts = &gfc_current_ns->proc_name->result->ts;
3095
3096 gcc_assert (gfc_current_state () == COMP_FUNCTION);
3097 gcc_assert (ts->type != BT_UNKNOWN);
3098
3099 /* Check type-parameters, at the moment only CHARACTER lengths possible. */
3100 /* TODO: Extend when KIND type parameters are implemented. */
3101 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length)
3102 gfc_expr_check_typed (ts->u.cl->length, gfc_current_ns, true);
3103 }
3104
3105
3106 /* Parse a set of specification statements. Returns the statement
3107 that doesn't fit. */
3108
3109 static gfc_statement
3110 parse_spec (gfc_statement st)
3111 {
3112 st_state ss;
3113 bool function_result_typed = false;
3114 bool bad_characteristic = false;
3115 gfc_typespec *ts;
3116
3117 verify_st_order (&ss, ST_NONE, false);
3118 if (st == ST_NONE)
3119 st = next_statement ();
3120
3121 /* If we are not inside a function or don't have a result specified so far,
3122 do nothing special about it. */
3123 if (gfc_current_state () != COMP_FUNCTION)
3124 function_result_typed = true;
3125 else
3126 {
3127 gfc_symbol* proc = gfc_current_ns->proc_name;
3128 gcc_assert (proc);
3129
3130 if (proc->result->ts.type == BT_UNKNOWN)
3131 function_result_typed = true;
3132 }
3133
3134 loop:
3135
3136 /* If we're inside a BLOCK construct, some statements are disallowed.
3137 Check this here. Attribute declaration statements like INTENT, OPTIONAL
3138 or VALUE are also disallowed, but they don't have a particular ST_*
3139 key so we have to check for them individually in their matcher routine. */
3140 if (gfc_current_state () == COMP_BLOCK)
3141 switch (st)
3142 {
3143 case ST_IMPLICIT:
3144 case ST_IMPLICIT_NONE:
3145 case ST_NAMELIST:
3146 case ST_COMMON:
3147 case ST_EQUIVALENCE:
3148 case ST_STATEMENT_FUNCTION:
3149 gfc_error ("%s statement is not allowed inside of BLOCK at %C",
3150 gfc_ascii_statement (st));
3151 reject_statement ();
3152 break;
3153
3154 default:
3155 break;
3156 }
3157 else if (gfc_current_state () == COMP_BLOCK_DATA)
3158 /* Fortran 2008, C1116. */
3159 switch (st)
3160 {
3161 case ST_DATA_DECL:
3162 case ST_COMMON:
3163 case ST_DATA:
3164 case ST_TYPE:
3165 case ST_END_BLOCK_DATA:
3166 case ST_ATTR_DECL:
3167 case ST_EQUIVALENCE:
3168 case ST_PARAMETER:
3169 case ST_IMPLICIT:
3170 case ST_IMPLICIT_NONE:
3171 case ST_DERIVED_DECL:
3172 case ST_USE:
3173 break;
3174
3175 case ST_NONE:
3176 break;
3177
3178 default:
3179 gfc_error ("%s statement is not allowed inside of BLOCK DATA at %C",
3180 gfc_ascii_statement (st));
3181 reject_statement ();
3182 break;
3183 }
3184
3185 /* If we find a statement that can not be followed by an IMPLICIT statement
3186 (and thus we can expect to see none any further), type the function result
3187 if it has not yet been typed. Be careful not to give the END statement
3188 to verify_st_order! */
3189 if (!function_result_typed && st != ST_GET_FCN_CHARACTERISTICS)
3190 {
3191 bool verify_now = false;
3192
3193 if (st == ST_END_FUNCTION || st == ST_CONTAINS)
3194 verify_now = true;
3195 else
3196 {
3197 st_state dummyss;
3198 verify_st_order (&dummyss, ST_NONE, false);
3199 verify_st_order (&dummyss, st, false);
3200
3201 if (!verify_st_order (&dummyss, ST_IMPLICIT, true))
3202 verify_now = true;
3203 }
3204
3205 if (verify_now)
3206 {
3207 check_function_result_typed ();
3208 function_result_typed = true;
3209 }
3210 }
3211
3212 switch (st)
3213 {
3214 case ST_NONE:
3215 unexpected_eof ();
3216
3217 case ST_IMPLICIT_NONE:
3218 case ST_IMPLICIT:
3219 if (!function_result_typed)
3220 {
3221 check_function_result_typed ();
3222 function_result_typed = true;
3223 }
3224 goto declSt;
3225
3226 case ST_FORMAT:
3227 case ST_ENTRY:
3228 case ST_DATA: /* Not allowed in interfaces */
3229 if (gfc_current_state () == COMP_INTERFACE)
3230 break;
3231
3232 /* Fall through */
3233
3234 case ST_USE:
3235 case ST_IMPORT:
3236 case ST_PARAMETER:
3237 case ST_PUBLIC:
3238 case ST_PRIVATE:
3239 case ST_DERIVED_DECL:
3240 case_decl:
3241 declSt:
3242 if (!verify_st_order (&ss, st, false))
3243 {
3244 reject_statement ();
3245 st = next_statement ();
3246 goto loop;
3247 }
3248
3249 switch (st)
3250 {
3251 case ST_INTERFACE:
3252 parse_interface ();
3253 break;
3254
3255 case ST_DERIVED_DECL:
3256 parse_derived ();
3257 break;
3258
3259 case ST_PUBLIC:
3260 case ST_PRIVATE:
3261 if (gfc_current_state () != COMP_MODULE)
3262 {
3263 gfc_error ("%s statement must appear in a MODULE",
3264 gfc_ascii_statement (st));
3265 reject_statement ();
3266 break;
3267 }
3268
3269 if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
3270 {
3271 gfc_error ("%s statement at %C follows another accessibility "
3272 "specification", gfc_ascii_statement (st));
3273 reject_statement ();
3274 break;
3275 }
3276
3277 gfc_current_ns->default_access = (st == ST_PUBLIC)
3278 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3279
3280 break;
3281
3282 case ST_STATEMENT_FUNCTION:
3283 if (gfc_current_state () == COMP_MODULE)
3284 {
3285 unexpected_statement (st);
3286 break;
3287 }
3288
3289 default:
3290 break;
3291 }
3292
3293 accept_statement (st);
3294 st = next_statement ();
3295 goto loop;
3296
3297 case ST_ENUM:
3298 accept_statement (st);
3299 parse_enum();
3300 st = next_statement ();
3301 goto loop;
3302
3303 case ST_GET_FCN_CHARACTERISTICS:
3304 /* This statement triggers the association of a function's result
3305 characteristics. */
3306 ts = &gfc_current_block ()->result->ts;
3307 if (match_deferred_characteristics (ts) != MATCH_YES)
3308 bad_characteristic = true;
3309
3310 st = next_statement ();
3311 goto loop;
3312
3313 case ST_OACC_DECLARE:
3314 if (!verify_st_order(&ss, st, false))
3315 {
3316 reject_statement ();
3317 st = next_statement ();
3318 goto loop;
3319 }
3320 if (gfc_state_stack->ext.oacc_declare_clauses == NULL)
3321 gfc_state_stack->ext.oacc_declare_clauses = new_st.ext.omp_clauses;
3322 accept_statement (st);
3323 st = next_statement ();
3324 goto loop;
3325
3326 default:
3327 break;
3328 }
3329
3330 /* If match_deferred_characteristics failed, then there is an error. */
3331 if (bad_characteristic)
3332 {
3333 ts = &gfc_current_block ()->result->ts;
3334 if (ts->type != BT_DERIVED)
3335 gfc_error ("Bad kind expression for function %qs at %L",
3336 gfc_current_block ()->name,
3337 &gfc_current_block ()->declared_at);
3338 else
3339 gfc_error ("The type for function %qs at %L is not accessible",
3340 gfc_current_block ()->name,
3341 &gfc_current_block ()->declared_at);
3342
3343 gfc_current_block ()->ts.kind = 0;
3344 /* Keep the derived type; if it's bad, it will be discovered later. */
3345 if (!(ts->type == BT_DERIVED && ts->u.derived))
3346 ts->type = BT_UNKNOWN;
3347 }
3348
3349 return st;
3350 }
3351
3352
3353 /* Parse a WHERE block, (not a simple WHERE statement). */
3354
3355 static void
3356 parse_where_block (void)
3357 {
3358 int seen_empty_else;
3359 gfc_code *top, *d;
3360 gfc_state_data s;
3361 gfc_statement st;
3362
3363 accept_statement (ST_WHERE_BLOCK);
3364 top = gfc_state_stack->tail;
3365
3366 push_state (&s, COMP_WHERE, gfc_new_block);
3367
3368 d = add_statement ();
3369 d->expr1 = top->expr1;
3370 d->op = EXEC_WHERE;
3371
3372 top->expr1 = NULL;
3373 top->block = d;
3374
3375 seen_empty_else = 0;
3376
3377 do
3378 {
3379 st = next_statement ();
3380 switch (st)
3381 {
3382 case ST_NONE:
3383 unexpected_eof ();
3384
3385 case ST_WHERE_BLOCK:
3386 parse_where_block ();
3387 break;
3388
3389 case ST_ASSIGNMENT:
3390 case ST_WHERE:
3391 accept_statement (st);
3392 break;
3393
3394 case ST_ELSEWHERE:
3395 if (seen_empty_else)
3396 {
3397 gfc_error ("ELSEWHERE statement at %C follows previous "
3398 "unmasked ELSEWHERE");
3399 reject_statement ();
3400 break;
3401 }
3402
3403 if (new_st.expr1 == NULL)
3404 seen_empty_else = 1;
3405
3406 d = new_level (gfc_state_stack->head);
3407 d->op = EXEC_WHERE;
3408 d->expr1 = new_st.expr1;
3409
3410 accept_statement (st);
3411
3412 break;
3413
3414 case ST_END_WHERE:
3415 accept_statement (st);
3416 break;
3417
3418 default:
3419 gfc_error ("Unexpected %s statement in WHERE block at %C",
3420 gfc_ascii_statement (st));
3421 reject_statement ();
3422 break;
3423 }
3424 }
3425 while (st != ST_END_WHERE);
3426
3427 pop_state ();
3428 }
3429
3430
3431 /* Parse a FORALL block (not a simple FORALL statement). */
3432
3433 static void
3434 parse_forall_block (void)
3435 {
3436 gfc_code *top, *d;
3437 gfc_state_data s;
3438 gfc_statement st;
3439
3440 accept_statement (ST_FORALL_BLOCK);
3441 top = gfc_state_stack->tail;
3442
3443 push_state (&s, COMP_FORALL, gfc_new_block);
3444
3445 d = add_statement ();
3446 d->op = EXEC_FORALL;
3447 top->block = d;
3448
3449 do
3450 {
3451 st = next_statement ();
3452 switch (st)
3453 {
3454
3455 case ST_ASSIGNMENT:
3456 case ST_POINTER_ASSIGNMENT:
3457 case ST_WHERE:
3458 case ST_FORALL:
3459 accept_statement (st);
3460 break;
3461
3462 case ST_WHERE_BLOCK:
3463 parse_where_block ();
3464 break;
3465
3466 case ST_FORALL_BLOCK:
3467 parse_forall_block ();
3468 break;
3469
3470 case ST_END_FORALL:
3471 accept_statement (st);
3472 break;
3473
3474 case ST_NONE:
3475 unexpected_eof ();
3476
3477 default:
3478 gfc_error ("Unexpected %s statement in FORALL block at %C",
3479 gfc_ascii_statement (st));
3480
3481 reject_statement ();
3482 break;
3483 }
3484 }
3485 while (st != ST_END_FORALL);
3486
3487 pop_state ();
3488 }
3489
3490
3491 static gfc_statement parse_executable (gfc_statement);
3492
3493 /* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block. */
3494
3495 static void
3496 parse_if_block (void)
3497 {
3498 gfc_code *top, *d;
3499 gfc_statement st;
3500 locus else_locus;
3501 gfc_state_data s;
3502 int seen_else;
3503
3504 seen_else = 0;
3505 accept_statement (ST_IF_BLOCK);
3506
3507 top = gfc_state_stack->tail;
3508 push_state (&s, COMP_IF, gfc_new_block);
3509
3510 new_st.op = EXEC_IF;
3511 d = add_statement ();
3512
3513 d->expr1 = top->expr1;
3514 top->expr1 = NULL;
3515 top->block = d;
3516
3517 do
3518 {
3519 st = parse_executable (ST_NONE);
3520
3521 switch (st)
3522 {
3523 case ST_NONE:
3524 unexpected_eof ();
3525
3526 case ST_ELSEIF:
3527 if (seen_else)
3528 {
3529 gfc_error ("ELSE IF statement at %C cannot follow ELSE "
3530 "statement at %L", &else_locus);
3531
3532 reject_statement ();
3533 break;
3534 }
3535
3536 d = new_level (gfc_state_stack->head);
3537 d->op = EXEC_IF;
3538 d->expr1 = new_st.expr1;
3539
3540 accept_statement (st);
3541
3542 break;
3543
3544 case ST_ELSE:
3545 if (seen_else)
3546 {
3547 gfc_error ("Duplicate ELSE statements at %L and %C",
3548 &else_locus);
3549 reject_statement ();
3550 break;
3551 }
3552
3553 seen_else = 1;
3554 else_locus = gfc_current_locus;
3555
3556 d = new_level (gfc_state_stack->head);
3557 d->op = EXEC_IF;
3558
3559 accept_statement (st);
3560
3561 break;
3562
3563 case ST_ENDIF:
3564 break;
3565
3566 default:
3567 unexpected_statement (st);
3568 break;
3569 }
3570 }
3571 while (st != ST_ENDIF);
3572
3573 pop_state ();
3574 accept_statement (st);
3575 }
3576
3577
3578 /* Parse a SELECT block. */
3579
3580 static void
3581 parse_select_block (void)
3582 {
3583 gfc_statement st;
3584 gfc_code *cp;
3585 gfc_state_data s;
3586
3587 accept_statement (ST_SELECT_CASE);
3588
3589 cp = gfc_state_stack->tail;
3590 push_state (&s, COMP_SELECT, gfc_new_block);
3591
3592 /* Make sure that the next statement is a CASE or END SELECT. */
3593 for (;;)
3594 {
3595 st = next_statement ();
3596 if (st == ST_NONE)
3597 unexpected_eof ();
3598 if (st == ST_END_SELECT)
3599 {
3600 /* Empty SELECT CASE is OK. */
3601 accept_statement (st);
3602 pop_state ();
3603 return;
3604 }
3605 if (st == ST_CASE)
3606 break;
3607
3608 gfc_error ("Expected a CASE or END SELECT statement following SELECT "
3609 "CASE at %C");
3610
3611 reject_statement ();
3612 }
3613
3614 /* At this point, we're got a nonempty select block. */
3615 cp = new_level (cp);
3616 *cp = new_st;
3617
3618 accept_statement (st);
3619
3620 do
3621 {
3622 st = parse_executable (ST_NONE);
3623 switch (st)
3624 {
3625 case ST_NONE:
3626 unexpected_eof ();
3627
3628 case ST_CASE:
3629 cp = new_level (gfc_state_stack->head);
3630 *cp = new_st;
3631 gfc_clear_new_st ();
3632
3633 accept_statement (st);
3634 /* Fall through */
3635
3636 case ST_END_SELECT:
3637 break;
3638
3639 /* Can't have an executable statement because of
3640 parse_executable(). */
3641 default:
3642 unexpected_statement (st);
3643 break;
3644 }
3645 }
3646 while (st != ST_END_SELECT);
3647
3648 pop_state ();
3649 accept_statement (st);
3650 }
3651
3652
3653 /* Pop the current selector from the SELECT TYPE stack. */
3654
3655 static void
3656 select_type_pop (void)
3657 {
3658 gfc_select_type_stack *old = select_type_stack;
3659 select_type_stack = old->prev;
3660 free (old);
3661 }
3662
3663
3664 /* Parse a SELECT TYPE construct (F03:R821). */
3665
3666 static void
3667 parse_select_type_block (void)
3668 {
3669 gfc_statement st;
3670 gfc_code *cp;
3671 gfc_state_data s;
3672
3673 accept_statement (ST_SELECT_TYPE);
3674
3675 cp = gfc_state_stack->tail;
3676 push_state (&s, COMP_SELECT_TYPE, gfc_new_block);
3677
3678 /* Make sure that the next statement is a TYPE IS, CLASS IS, CLASS DEFAULT
3679 or END SELECT. */
3680 for (;;)
3681 {
3682 st = next_statement ();
3683 if (st == ST_NONE)
3684 unexpected_eof ();
3685 if (st == ST_END_SELECT)
3686 /* Empty SELECT CASE is OK. */
3687 goto done;
3688 if (st == ST_TYPE_IS || st == ST_CLASS_IS)
3689 break;
3690
3691 gfc_error ("Expected TYPE IS, CLASS IS or END SELECT statement "
3692 "following SELECT TYPE at %C");
3693
3694 reject_statement ();
3695 }
3696
3697 /* At this point, we're got a nonempty select block. */
3698 cp = new_level (cp);
3699 *cp = new_st;
3700
3701 accept_statement (st);
3702
3703 do
3704 {
3705 st = parse_executable (ST_NONE);
3706 switch (st)
3707 {
3708 case ST_NONE:
3709 unexpected_eof ();
3710
3711 case ST_TYPE_IS:
3712 case ST_CLASS_IS:
3713 cp = new_level (gfc_state_stack->head);
3714 *cp = new_st;
3715 gfc_clear_new_st ();
3716
3717 accept_statement (st);
3718 /* Fall through */
3719
3720 case ST_END_SELECT:
3721 break;
3722
3723 /* Can't have an executable statement because of
3724 parse_executable(). */
3725 default:
3726 unexpected_statement (st);
3727 break;
3728 }
3729 }
3730 while (st != ST_END_SELECT);
3731
3732 done:
3733 pop_state ();
3734 accept_statement (st);
3735 gfc_current_ns = gfc_current_ns->parent;
3736 select_type_pop ();
3737 }
3738
3739
3740 /* Given a symbol, make sure it is not an iteration variable for a DO
3741 statement. This subroutine is called when the symbol is seen in a
3742 context that causes it to become redefined. If the symbol is an
3743 iterator, we generate an error message and return nonzero. */
3744
3745 int
3746 gfc_check_do_variable (gfc_symtree *st)
3747 {
3748 gfc_state_data *s;
3749
3750 for (s=gfc_state_stack; s; s = s->previous)
3751 if (s->do_variable == st)
3752 {
3753 gfc_error_now ("Variable %qs at %C cannot be redefined inside "
3754 "loop beginning at %L", st->name, &s->head->loc);
3755 return 1;
3756 }
3757
3758 return 0;
3759 }
3760
3761
3762 /* Checks to see if the current statement label closes an enddo.
3763 Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
3764 an error) if it incorrectly closes an ENDDO. */
3765
3766 static int
3767 check_do_closure (void)
3768 {
3769 gfc_state_data *p;
3770
3771 if (gfc_statement_label == NULL)
3772 return 0;
3773
3774 for (p = gfc_state_stack; p; p = p->previous)
3775 if (p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
3776 break;
3777
3778 if (p == NULL)
3779 return 0; /* No loops to close */
3780
3781 if (p->ext.end_do_label == gfc_statement_label)
3782 {
3783 if (p == gfc_state_stack)
3784 return 1;
3785
3786 gfc_error ("End of nonblock DO statement at %C is within another block");
3787 return 2;
3788 }
3789
3790 /* At this point, the label doesn't terminate the innermost loop.
3791 Make sure it doesn't terminate another one. */
3792 for (; p; p = p->previous)
3793 if ((p->state == COMP_DO || p->state == COMP_DO_CONCURRENT)
3794 && p->ext.end_do_label == gfc_statement_label)
3795 {
3796 gfc_error ("End of nonblock DO statement at %C is interwoven "
3797 "with another DO loop");
3798 return 2;
3799 }
3800
3801 return 0;
3802 }
3803
3804
3805 /* Parse a series of contained program units. */
3806
3807 static void parse_progunit (gfc_statement);
3808
3809
3810 /* Parse a CRITICAL block. */
3811
3812 static void
3813 parse_critical_block (void)
3814 {
3815 gfc_code *top, *d;
3816 gfc_state_data s, *sd;
3817 gfc_statement st;
3818
3819 for (sd = gfc_state_stack; sd; sd = sd->previous)
3820 if (sd->state == COMP_OMP_STRUCTURED_BLOCK)
3821 gfc_error_now (is_oacc (sd)
3822 ? "CRITICAL block inside of OpenACC region at %C"
3823 : "CRITICAL block inside of OpenMP region at %C");
3824
3825 s.ext.end_do_label = new_st.label1;
3826
3827 accept_statement (ST_CRITICAL);
3828 top = gfc_state_stack->tail;
3829
3830 push_state (&s, COMP_CRITICAL, gfc_new_block);
3831
3832 d = add_statement ();
3833 d->op = EXEC_CRITICAL;
3834 top->block = d;
3835
3836 do
3837 {
3838 st = parse_executable (ST_NONE);
3839
3840 switch (st)
3841 {
3842 case ST_NONE:
3843 unexpected_eof ();
3844 break;
3845
3846 case ST_END_CRITICAL:
3847 if (s.ext.end_do_label != NULL
3848 && s.ext.end_do_label != gfc_statement_label)
3849 gfc_error_now ("Statement label in END CRITICAL at %C does not "
3850 "match CRITICAL label");
3851
3852 if (gfc_statement_label != NULL)
3853 {
3854 new_st.op = EXEC_NOP;
3855 add_statement ();
3856 }
3857 break;
3858
3859 default:
3860 unexpected_statement (st);
3861 break;
3862 }
3863 }
3864 while (st != ST_END_CRITICAL);
3865
3866 pop_state ();
3867 accept_statement (st);
3868 }
3869
3870
3871 /* Set up the local namespace for a BLOCK construct. */
3872
3873 gfc_namespace*
3874 gfc_build_block_ns (gfc_namespace *parent_ns)
3875 {
3876 gfc_namespace* my_ns;
3877 static int numblock = 1;
3878
3879 my_ns = gfc_get_namespace (parent_ns, 1);
3880 my_ns->construct_entities = 1;
3881
3882 /* Give the BLOCK a symbol of flavor LABEL; this is later needed for correct
3883 code generation (so it must not be NULL).
3884 We set its recursive argument if our container procedure is recursive, so
3885 that local variables are accordingly placed on the stack when it
3886 will be necessary. */
3887 if (gfc_new_block)
3888 my_ns->proc_name = gfc_new_block;
3889 else
3890 {
3891 bool t;
3892 char buffer[20]; /* Enough to hold "block@2147483648\n". */
3893
3894 snprintf(buffer, sizeof(buffer), "block@%d", numblock++);
3895 gfc_get_symbol (buffer, my_ns, &my_ns->proc_name);
3896 t = gfc_add_flavor (&my_ns->proc_name->attr, FL_LABEL,
3897 my_ns->proc_name->name, NULL);
3898 gcc_assert (t);
3899 gfc_commit_symbol (my_ns->proc_name);
3900 }
3901
3902 if (parent_ns->proc_name)
3903 my_ns->proc_name->attr.recursive = parent_ns->proc_name->attr.recursive;
3904
3905 return my_ns;
3906 }
3907
3908
3909 /* Parse a BLOCK construct. */
3910
3911 static void
3912 parse_block_construct (void)
3913 {
3914 gfc_namespace* my_ns;
3915 gfc_state_data s;
3916
3917 gfc_notify_std (GFC_STD_F2008, "BLOCK construct at %C");
3918
3919 my_ns = gfc_build_block_ns (gfc_current_ns);
3920
3921 new_st.op = EXEC_BLOCK;
3922 new_st.ext.block.ns = my_ns;
3923 new_st.ext.block.assoc = NULL;
3924 accept_statement (ST_BLOCK);
3925
3926 push_state (&s, COMP_BLOCK, my_ns->proc_name);
3927 gfc_current_ns = my_ns;
3928
3929 parse_progunit (ST_NONE);
3930
3931 gfc_current_ns = gfc_current_ns->parent;
3932 pop_state ();
3933 }
3934
3935
3936 /* Parse an ASSOCIATE construct. This is essentially a BLOCK construct
3937 behind the scenes with compiler-generated variables. */
3938
3939 static void
3940 parse_associate (void)
3941 {
3942 gfc_namespace* my_ns;
3943 gfc_state_data s;
3944 gfc_statement st;
3945 gfc_association_list* a;
3946
3947 gfc_notify_std (GFC_STD_F2003, "ASSOCIATE construct at %C");
3948
3949 my_ns = gfc_build_block_ns (gfc_current_ns);
3950
3951 new_st.op = EXEC_BLOCK;
3952 new_st.ext.block.ns = my_ns;
3953 gcc_assert (new_st.ext.block.assoc);
3954
3955 /* Add all associate-names as BLOCK variables. Creating them is enough
3956 for now, they'll get their values during trans-* phase. */
3957 gfc_current_ns = my_ns;
3958 for (a = new_st.ext.block.assoc; a; a = a->next)
3959 {
3960 gfc_symbol* sym;
3961
3962 if (gfc_get_sym_tree (a->name, NULL, &a->st, false))
3963 gcc_unreachable ();
3964
3965 sym = a->st->n.sym;
3966 sym->attr.flavor = FL_VARIABLE;
3967 sym->assoc = a;
3968 sym->declared_at = a->where;
3969 gfc_set_sym_referenced (sym);
3970
3971 /* Initialize the typespec. It is not available in all cases,
3972 however, as it may only be set on the target during resolution.
3973 Still, sometimes it helps to have it right now -- especially
3974 for parsing component references on the associate-name
3975 in case of association to a derived-type. */
3976 sym->ts = a->target->ts;
3977 }
3978
3979 accept_statement (ST_ASSOCIATE);
3980 push_state (&s, COMP_ASSOCIATE, my_ns->proc_name);
3981
3982 loop:
3983 st = parse_executable (ST_NONE);
3984 switch (st)
3985 {
3986 case ST_NONE:
3987 unexpected_eof ();
3988
3989 case_end:
3990 accept_statement (st);
3991 my_ns->code = gfc_state_stack->head;
3992 break;
3993
3994 default:
3995 unexpected_statement (st);
3996 goto loop;
3997 }
3998
3999 gfc_current_ns = gfc_current_ns->parent;
4000 pop_state ();
4001 }
4002
4003
4004 /* Parse a DO loop. Note that the ST_CYCLE and ST_EXIT statements are
4005 handled inside of parse_executable(), because they aren't really
4006 loop statements. */
4007
4008 static void
4009 parse_do_block (void)
4010 {
4011 gfc_statement st;
4012 gfc_code *top;
4013 gfc_state_data s;
4014 gfc_symtree *stree;
4015 gfc_exec_op do_op;
4016
4017 do_op = new_st.op;
4018 s.ext.end_do_label = new_st.label1;
4019
4020 if (new_st.ext.iterator != NULL)
4021 stree = new_st.ext.iterator->var->symtree;
4022 else
4023 stree = NULL;
4024
4025 accept_statement (ST_DO);
4026
4027 top = gfc_state_stack->tail;
4028 push_state (&s, do_op == EXEC_DO_CONCURRENT ? COMP_DO_CONCURRENT : COMP_DO,
4029 gfc_new_block);
4030
4031 s.do_variable = stree;
4032
4033 top->block = new_level (top);
4034 top->block->op = EXEC_DO;
4035
4036 loop:
4037 st = parse_executable (ST_NONE);
4038
4039 switch (st)
4040 {
4041 case ST_NONE:
4042 unexpected_eof ();
4043
4044 case ST_ENDDO:
4045 if (s.ext.end_do_label != NULL
4046 && s.ext.end_do_label != gfc_statement_label)
4047 gfc_error_now ("Statement label in ENDDO at %C doesn't match "
4048 "DO label");
4049
4050 if (gfc_statement_label != NULL)
4051 {
4052 new_st.op = EXEC_NOP;
4053 add_statement ();
4054 }
4055 break;
4056
4057 case ST_IMPLIED_ENDDO:
4058 /* If the do-stmt of this DO construct has a do-construct-name,
4059 the corresponding end-do must be an end-do-stmt (with a matching
4060 name, but in that case we must have seen ST_ENDDO first).
4061 We only complain about this in pedantic mode. */
4062 if (gfc_current_block () != NULL)
4063 gfc_error_now ("Named block DO at %L requires matching ENDDO name",
4064 &gfc_current_block()->declared_at);
4065
4066 break;
4067
4068 default:
4069 unexpected_statement (st);
4070 goto loop;
4071 }
4072
4073 pop_state ();
4074 accept_statement (st);
4075 }
4076
4077
4078 /* Parse the statements of OpenMP do/parallel do. */
4079
4080 static gfc_statement
4081 parse_omp_do (gfc_statement omp_st)
4082 {
4083 gfc_statement st;
4084 gfc_code *cp, *np;
4085 gfc_state_data s;
4086
4087 accept_statement (omp_st);
4088
4089 cp = gfc_state_stack->tail;
4090 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4091 np = new_level (cp);
4092 np->op = cp->op;
4093 np->block = NULL;
4094
4095 for (;;)
4096 {
4097 st = next_statement ();
4098 if (st == ST_NONE)
4099 unexpected_eof ();
4100 else if (st == ST_DO)
4101 break;
4102 else
4103 unexpected_statement (st);
4104 }
4105
4106 parse_do_block ();
4107 if (gfc_statement_label != NULL
4108 && gfc_state_stack->previous != NULL
4109 && gfc_state_stack->previous->state == COMP_DO
4110 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4111 {
4112 /* In
4113 DO 100 I=1,10
4114 !$OMP DO
4115 DO J=1,10
4116 ...
4117 100 CONTINUE
4118 there should be no !$OMP END DO. */
4119 pop_state ();
4120 return ST_IMPLIED_ENDDO;
4121 }
4122
4123 check_do_closure ();
4124 pop_state ();
4125
4126 st = next_statement ();
4127 gfc_statement omp_end_st = ST_OMP_END_DO;
4128 switch (omp_st)
4129 {
4130 case ST_OMP_DISTRIBUTE: omp_end_st = ST_OMP_END_DISTRIBUTE; break;
4131 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4132 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4133 break;
4134 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4135 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4136 break;
4137 case ST_OMP_DISTRIBUTE_SIMD:
4138 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4139 break;
4140 case ST_OMP_DO: omp_end_st = ST_OMP_END_DO; break;
4141 case ST_OMP_DO_SIMD: omp_end_st = ST_OMP_END_DO_SIMD; break;
4142 case ST_OMP_PARALLEL_DO: omp_end_st = ST_OMP_END_PARALLEL_DO; break;
4143 case ST_OMP_PARALLEL_DO_SIMD:
4144 omp_end_st = ST_OMP_END_PARALLEL_DO_SIMD;
4145 break;
4146 case ST_OMP_SIMD: omp_end_st = ST_OMP_END_SIMD; break;
4147 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4148 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4149 break;
4150 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4151 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4152 break;
4153 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4154 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4155 break;
4156 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4157 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4158 break;
4159 case ST_OMP_TEAMS_DISTRIBUTE:
4160 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4161 break;
4162 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4163 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4164 break;
4165 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4166 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4167 break;
4168 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4169 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4170 break;
4171 default: gcc_unreachable ();
4172 }
4173 if (st == omp_end_st)
4174 {
4175 if (new_st.op == EXEC_OMP_END_NOWAIT)
4176 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4177 else
4178 gcc_assert (new_st.op == EXEC_NOP);
4179 gfc_clear_new_st ();
4180 gfc_commit_symbols ();
4181 gfc_warning_check ();
4182 st = next_statement ();
4183 }
4184 return st;
4185 }
4186
4187
4188 /* Parse the statements of OpenMP atomic directive. */
4189
4190 static gfc_statement
4191 parse_omp_atomic (void)
4192 {
4193 gfc_statement st;
4194 gfc_code *cp, *np;
4195 gfc_state_data s;
4196 int count;
4197
4198 accept_statement (ST_OMP_ATOMIC);
4199
4200 cp = gfc_state_stack->tail;
4201 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4202 np = new_level (cp);
4203 np->op = cp->op;
4204 np->block = NULL;
4205 count = 1 + ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4206 == GFC_OMP_ATOMIC_CAPTURE);
4207
4208 while (count)
4209 {
4210 st = next_statement ();
4211 if (st == ST_NONE)
4212 unexpected_eof ();
4213 else if (st == ST_ASSIGNMENT)
4214 {
4215 accept_statement (st);
4216 count--;
4217 }
4218 else
4219 unexpected_statement (st);
4220 }
4221
4222 pop_state ();
4223
4224 st = next_statement ();
4225 if (st == ST_OMP_END_ATOMIC)
4226 {
4227 gfc_clear_new_st ();
4228 gfc_commit_symbols ();
4229 gfc_warning_check ();
4230 st = next_statement ();
4231 }
4232 else if ((cp->ext.omp_atomic & GFC_OMP_ATOMIC_MASK)
4233 == GFC_OMP_ATOMIC_CAPTURE)
4234 gfc_error ("Missing !$OMP END ATOMIC after !$OMP ATOMIC CAPTURE at %C");
4235 return st;
4236 }
4237
4238
4239 /* Parse the statements of an OpenACC structured block. */
4240
4241 static void
4242 parse_oacc_structured_block (gfc_statement acc_st)
4243 {
4244 gfc_statement st, acc_end_st;
4245 gfc_code *cp, *np;
4246 gfc_state_data s, *sd;
4247
4248 for (sd = gfc_state_stack; sd; sd = sd->previous)
4249 if (sd->state == COMP_CRITICAL)
4250 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4251
4252 accept_statement (acc_st);
4253
4254 cp = gfc_state_stack->tail;
4255 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4256 np = new_level (cp);
4257 np->op = cp->op;
4258 np->block = NULL;
4259 switch (acc_st)
4260 {
4261 case ST_OACC_PARALLEL:
4262 acc_end_st = ST_OACC_END_PARALLEL;
4263 break;
4264 case ST_OACC_KERNELS:
4265 acc_end_st = ST_OACC_END_KERNELS;
4266 break;
4267 case ST_OACC_DATA:
4268 acc_end_st = ST_OACC_END_DATA;
4269 break;
4270 case ST_OACC_HOST_DATA:
4271 acc_end_st = ST_OACC_END_HOST_DATA;
4272 break;
4273 default:
4274 gcc_unreachable ();
4275 }
4276
4277 do
4278 {
4279 st = parse_executable (ST_NONE);
4280 if (st == ST_NONE)
4281 unexpected_eof ();
4282 else if (st != acc_end_st)
4283 gfc_error ("Expecting %s at %C", gfc_ascii_statement (acc_end_st));
4284 reject_statement ();
4285 }
4286 while (st != acc_end_st);
4287
4288 gcc_assert (new_st.op == EXEC_NOP);
4289
4290 gfc_clear_new_st ();
4291 gfc_commit_symbols ();
4292 gfc_warning_check ();
4293 pop_state ();
4294 }
4295
4296 /* Parse the statements of OpenACC loop/parallel loop/kernels loop. */
4297
4298 static gfc_statement
4299 parse_oacc_loop (gfc_statement acc_st)
4300 {
4301 gfc_statement st;
4302 gfc_code *cp, *np;
4303 gfc_state_data s, *sd;
4304
4305 for (sd = gfc_state_stack; sd; sd = sd->previous)
4306 if (sd->state == COMP_CRITICAL)
4307 gfc_error_now ("OpenACC directive inside of CRITICAL block at %C");
4308
4309 accept_statement (acc_st);
4310
4311 cp = gfc_state_stack->tail;
4312 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4313 np = new_level (cp);
4314 np->op = cp->op;
4315 np->block = NULL;
4316
4317 for (;;)
4318 {
4319 st = next_statement ();
4320 if (st == ST_NONE)
4321 unexpected_eof ();
4322 else if (st == ST_DO)
4323 break;
4324 else
4325 {
4326 gfc_error ("Expected DO loop at %C");
4327 reject_statement ();
4328 }
4329 }
4330
4331 parse_do_block ();
4332 if (gfc_statement_label != NULL
4333 && gfc_state_stack->previous != NULL
4334 && gfc_state_stack->previous->state == COMP_DO
4335 && gfc_state_stack->previous->ext.end_do_label == gfc_statement_label)
4336 {
4337 pop_state ();
4338 return ST_IMPLIED_ENDDO;
4339 }
4340
4341 check_do_closure ();
4342 pop_state ();
4343
4344 st = next_statement ();
4345 if (st == ST_OACC_END_LOOP)
4346 gfc_warning (0, "Redundant !$ACC END LOOP at %C");
4347 if ((acc_st == ST_OACC_PARALLEL_LOOP && st == ST_OACC_END_PARALLEL_LOOP) ||
4348 (acc_st == ST_OACC_KERNELS_LOOP && st == ST_OACC_END_KERNELS_LOOP) ||
4349 (acc_st == ST_OACC_LOOP && st == ST_OACC_END_LOOP))
4350 {
4351 gcc_assert (new_st.op == EXEC_NOP);
4352 gfc_clear_new_st ();
4353 gfc_commit_symbols ();
4354 gfc_warning_check ();
4355 st = next_statement ();
4356 }
4357 return st;
4358 }
4359
4360
4361 /* Parse the statements of an OpenMP structured block. */
4362
4363 static void
4364 parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
4365 {
4366 gfc_statement st, omp_end_st;
4367 gfc_code *cp, *np;
4368 gfc_state_data s;
4369
4370 accept_statement (omp_st);
4371
4372 cp = gfc_state_stack->tail;
4373 push_state (&s, COMP_OMP_STRUCTURED_BLOCK, NULL);
4374 np = new_level (cp);
4375 np->op = cp->op;
4376 np->block = NULL;
4377
4378 switch (omp_st)
4379 {
4380 case ST_OMP_PARALLEL:
4381 omp_end_st = ST_OMP_END_PARALLEL;
4382 break;
4383 case ST_OMP_PARALLEL_SECTIONS:
4384 omp_end_st = ST_OMP_END_PARALLEL_SECTIONS;
4385 break;
4386 case ST_OMP_SECTIONS:
4387 omp_end_st = ST_OMP_END_SECTIONS;
4388 break;
4389 case ST_OMP_ORDERED:
4390 omp_end_st = ST_OMP_END_ORDERED;
4391 break;
4392 case ST_OMP_CRITICAL:
4393 omp_end_st = ST_OMP_END_CRITICAL;
4394 break;
4395 case ST_OMP_MASTER:
4396 omp_end_st = ST_OMP_END_MASTER;
4397 break;
4398 case ST_OMP_SINGLE:
4399 omp_end_st = ST_OMP_END_SINGLE;
4400 break;
4401 case ST_OMP_TARGET:
4402 omp_end_st = ST_OMP_END_TARGET;
4403 break;
4404 case ST_OMP_TARGET_DATA:
4405 omp_end_st = ST_OMP_END_TARGET_DATA;
4406 break;
4407 case ST_OMP_TARGET_TEAMS:
4408 omp_end_st = ST_OMP_END_TARGET_TEAMS;
4409 break;
4410 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4411 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE;
4412 break;
4413 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4414 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
4415 break;
4416 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4417 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4418 break;
4419 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4420 omp_end_st = ST_OMP_END_TARGET_TEAMS_DISTRIBUTE_SIMD;
4421 break;
4422 case ST_OMP_TASK:
4423 omp_end_st = ST_OMP_END_TASK;
4424 break;
4425 case ST_OMP_TASKGROUP:
4426 omp_end_st = ST_OMP_END_TASKGROUP;
4427 break;
4428 case ST_OMP_TEAMS:
4429 omp_end_st = ST_OMP_END_TEAMS;
4430 break;
4431 case ST_OMP_TEAMS_DISTRIBUTE:
4432 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE;
4433 break;
4434 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4435 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO;
4436 break;
4437 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4438 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
4439 break;
4440 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4441 omp_end_st = ST_OMP_END_TEAMS_DISTRIBUTE_SIMD;
4442 break;
4443 case ST_OMP_DISTRIBUTE:
4444 omp_end_st = ST_OMP_END_DISTRIBUTE;
4445 break;
4446 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4447 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO;
4448 break;
4449 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4450 omp_end_st = ST_OMP_END_DISTRIBUTE_PARALLEL_DO_SIMD;
4451 break;
4452 case ST_OMP_DISTRIBUTE_SIMD:
4453 omp_end_st = ST_OMP_END_DISTRIBUTE_SIMD;
4454 break;
4455 case ST_OMP_WORKSHARE:
4456 omp_end_st = ST_OMP_END_WORKSHARE;
4457 break;
4458 case ST_OMP_PARALLEL_WORKSHARE:
4459 omp_end_st = ST_OMP_END_PARALLEL_WORKSHARE;
4460 break;
4461 default:
4462 gcc_unreachable ();
4463 }
4464
4465 do
4466 {
4467 if (workshare_stmts_only)
4468 {
4469 /* Inside of !$omp workshare, only
4470 scalar assignments
4471 array assignments
4472 where statements and constructs
4473 forall statements and constructs
4474 !$omp atomic
4475 !$omp critical
4476 !$omp parallel
4477 are allowed. For !$omp critical these
4478 restrictions apply recursively. */
4479 bool cycle = true;
4480
4481 st = next_statement ();
4482 for (;;)
4483 {
4484 switch (st)
4485 {
4486 case ST_NONE:
4487 unexpected_eof ();
4488
4489 case ST_ASSIGNMENT:
4490 case ST_WHERE:
4491 case ST_FORALL:
4492 accept_statement (st);
4493 break;
4494
4495 case ST_WHERE_BLOCK:
4496 parse_where_block ();
4497 break;
4498
4499 case ST_FORALL_BLOCK:
4500 parse_forall_block ();
4501 break;
4502
4503 case ST_OMP_PARALLEL:
4504 case ST_OMP_PARALLEL_SECTIONS:
4505 parse_omp_structured_block (st, false);
4506 break;
4507
4508 case ST_OMP_PARALLEL_WORKSHARE:
4509 case ST_OMP_CRITICAL:
4510 parse_omp_structured_block (st, true);
4511 break;
4512
4513 case ST_OMP_PARALLEL_DO:
4514 case ST_OMP_PARALLEL_DO_SIMD:
4515 st = parse_omp_do (st);
4516 continue;
4517
4518 case ST_OMP_ATOMIC:
4519 st = parse_omp_atomic ();
4520 continue;
4521
4522 default:
4523 cycle = false;
4524 break;
4525 }
4526
4527 if (!cycle)
4528 break;
4529
4530 st = next_statement ();
4531 }
4532 }
4533 else
4534 st = parse_executable (ST_NONE);
4535 if (st == ST_NONE)
4536 unexpected_eof ();
4537 else if (st == ST_OMP_SECTION
4538 && (omp_st == ST_OMP_SECTIONS
4539 || omp_st == ST_OMP_PARALLEL_SECTIONS))
4540 {
4541 np = new_level (np);
4542 np->op = cp->op;
4543 np->block = NULL;
4544 }
4545 else if (st != omp_end_st)
4546 unexpected_statement (st);
4547 }
4548 while (st != omp_end_st);
4549
4550 switch (new_st.op)
4551 {
4552 case EXEC_OMP_END_NOWAIT:
4553 cp->ext.omp_clauses->nowait |= new_st.ext.omp_bool;
4554 break;
4555 case EXEC_OMP_CRITICAL:
4556 if (((cp->ext.omp_name == NULL) ^ (new_st.ext.omp_name == NULL))
4557 || (new_st.ext.omp_name != NULL
4558 && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
4559 gfc_error ("Name after !$omp critical and !$omp end critical does "
4560 "not match at %C");
4561 free (CONST_CAST (char *, new_st.ext.omp_name));
4562 break;
4563 case EXEC_OMP_END_SINGLE:
4564 cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
4565 = new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE];
4566 new_st.ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE] = NULL;
4567 gfc_free_omp_clauses (new_st.ext.omp_clauses);
4568 break;
4569 case EXEC_NOP:
4570 break;
4571 default:
4572 gcc_unreachable ();
4573 }
4574
4575 gfc_clear_new_st ();
4576 gfc_commit_symbols ();
4577 gfc_warning_check ();
4578 pop_state ();
4579 }
4580
4581
4582 /* Accept a series of executable statements. We return the first
4583 statement that doesn't fit to the caller. Any block statements are
4584 passed on to the correct handler, which usually passes the buck
4585 right back here. */
4586
4587 static gfc_statement
4588 parse_executable (gfc_statement st)
4589 {
4590 int close_flag;
4591
4592 if (st == ST_NONE)
4593 st = next_statement ();
4594
4595 for (;;)
4596 {
4597 close_flag = check_do_closure ();
4598 if (close_flag)
4599 switch (st)
4600 {
4601 case ST_GOTO:
4602 case ST_END_PROGRAM:
4603 case ST_RETURN:
4604 case ST_EXIT:
4605 case ST_END_FUNCTION:
4606 case ST_CYCLE:
4607 case ST_PAUSE:
4608 case ST_STOP:
4609 case ST_ERROR_STOP:
4610 case ST_END_SUBROUTINE:
4611
4612 case ST_DO:
4613 case ST_FORALL:
4614 case ST_WHERE:
4615 case ST_SELECT_CASE:
4616 gfc_error ("%s statement at %C cannot terminate a non-block "
4617 "DO loop", gfc_ascii_statement (st));
4618 break;
4619
4620 default:
4621 break;
4622 }
4623
4624 switch (st)
4625 {
4626 case ST_NONE:
4627 unexpected_eof ();
4628
4629 case ST_DATA:
4630 gfc_notify_std (GFC_STD_F95_OBS, "DATA statement at %C after the "
4631 "first executable statement");
4632 /* Fall through. */
4633
4634 case ST_FORMAT:
4635 case ST_ENTRY:
4636 case_executable:
4637 accept_statement (st);
4638 if (close_flag == 1)
4639 return ST_IMPLIED_ENDDO;
4640 break;
4641
4642 case ST_BLOCK:
4643 parse_block_construct ();
4644 break;
4645
4646 case ST_ASSOCIATE:
4647 parse_associate ();
4648 break;
4649
4650 case ST_IF_BLOCK:
4651 parse_if_block ();
4652 break;
4653
4654 case ST_SELECT_CASE:
4655 parse_select_block ();
4656 break;
4657
4658 case ST_SELECT_TYPE:
4659 parse_select_type_block();
4660 break;
4661
4662 case ST_DO:
4663 parse_do_block ();
4664 if (check_do_closure () == 1)
4665 return ST_IMPLIED_ENDDO;
4666 break;
4667
4668 case ST_CRITICAL:
4669 parse_critical_block ();
4670 break;
4671
4672 case ST_WHERE_BLOCK:
4673 parse_where_block ();
4674 break;
4675
4676 case ST_FORALL_BLOCK:
4677 parse_forall_block ();
4678 break;
4679
4680 case ST_OACC_PARALLEL_LOOP:
4681 case ST_OACC_KERNELS_LOOP:
4682 case ST_OACC_LOOP:
4683 st = parse_oacc_loop (st);
4684 if (st == ST_IMPLIED_ENDDO)
4685 return st;
4686 continue;
4687
4688 case ST_OACC_PARALLEL:
4689 case ST_OACC_KERNELS:
4690 case ST_OACC_DATA:
4691 case ST_OACC_HOST_DATA:
4692 parse_oacc_structured_block (st);
4693 break;
4694
4695 case ST_OMP_PARALLEL:
4696 case ST_OMP_PARALLEL_SECTIONS:
4697 case ST_OMP_SECTIONS:
4698 case ST_OMP_ORDERED:
4699 case ST_OMP_CRITICAL:
4700 case ST_OMP_MASTER:
4701 case ST_OMP_SINGLE:
4702 case ST_OMP_TARGET:
4703 case ST_OMP_TARGET_DATA:
4704 case ST_OMP_TARGET_TEAMS:
4705 case ST_OMP_TEAMS:
4706 case ST_OMP_TASK:
4707 case ST_OMP_TASKGROUP:
4708 parse_omp_structured_block (st, false);
4709 break;
4710
4711 case ST_OMP_WORKSHARE:
4712 case ST_OMP_PARALLEL_WORKSHARE:
4713 parse_omp_structured_block (st, true);
4714 break;
4715
4716 case ST_OMP_DISTRIBUTE:
4717 case ST_OMP_DISTRIBUTE_PARALLEL_DO:
4718 case ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
4719 case ST_OMP_DISTRIBUTE_SIMD:
4720 case ST_OMP_DO:
4721 case ST_OMP_DO_SIMD:
4722 case ST_OMP_PARALLEL_DO:
4723 case ST_OMP_PARALLEL_DO_SIMD:
4724 case ST_OMP_SIMD:
4725 case ST_OMP_TARGET_TEAMS_DISTRIBUTE:
4726 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
4727 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4728 case ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
4729 case ST_OMP_TEAMS_DISTRIBUTE:
4730 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
4731 case ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
4732 case ST_OMP_TEAMS_DISTRIBUTE_SIMD:
4733 st = parse_omp_do (st);
4734 if (st == ST_IMPLIED_ENDDO)
4735 return st;
4736 continue;
4737
4738 case ST_OMP_ATOMIC:
4739 st = parse_omp_atomic ();
4740 continue;
4741
4742 default:
4743 return st;
4744 }
4745
4746 st = next_statement ();
4747 }
4748 }
4749
4750
4751 /* Fix the symbols for sibling functions. These are incorrectly added to
4752 the child namespace as the parser didn't know about this procedure. */
4753
4754 static void
4755 gfc_fixup_sibling_symbols (gfc_symbol *sym, gfc_namespace *siblings)
4756 {
4757 gfc_namespace *ns;
4758 gfc_symtree *st;
4759 gfc_symbol *old_sym;
4760
4761 for (ns = siblings; ns; ns = ns->sibling)
4762 {
4763 st = gfc_find_symtree (ns->sym_root, sym->name);
4764
4765 if (!st || (st->n.sym->attr.dummy && ns == st->n.sym->ns))
4766 goto fixup_contained;
4767
4768 if ((st->n.sym->attr.flavor == FL_DERIVED
4769 && sym->attr.generic && sym->attr.function)
4770 ||(sym->attr.flavor == FL_DERIVED
4771 && st->n.sym->attr.generic && st->n.sym->attr.function))
4772 goto fixup_contained;
4773
4774 old_sym = st->n.sym;
4775 if (old_sym->ns == ns
4776 && !old_sym->attr.contained
4777
4778 /* By 14.6.1.3, host association should be excluded
4779 for the following. */
4780 && !(old_sym->attr.external
4781 || (old_sym->ts.type != BT_UNKNOWN
4782 && !old_sym->attr.implicit_type)
4783 || old_sym->attr.flavor == FL_PARAMETER
4784 || old_sym->attr.use_assoc
4785 || old_sym->attr.in_common
4786 || old_sym->attr.in_equivalence
4787 || old_sym->attr.data
4788 || old_sym->attr.dummy
4789 || old_sym->attr.result
4790 || old_sym->attr.dimension
4791 || old_sym->attr.allocatable
4792 || old_sym->attr.intrinsic
4793 || old_sym->attr.generic
4794 || old_sym->attr.flavor == FL_NAMELIST
4795 || old_sym->attr.flavor == FL_LABEL
4796 || old_sym->attr.proc == PROC_ST_FUNCTION))
4797 {
4798 /* Replace it with the symbol from the parent namespace. */
4799 st->n.sym = sym;
4800 sym->refs++;
4801
4802 gfc_release_symbol (old_sym);
4803 }
4804
4805 fixup_contained:
4806 /* Do the same for any contained procedures. */
4807 gfc_fixup_sibling_symbols (sym, ns->contained);
4808 }
4809 }
4810
4811 static void
4812 parse_contained (int module)
4813 {
4814 gfc_namespace *ns, *parent_ns, *tmp;
4815 gfc_state_data s1, s2;
4816 gfc_statement st;
4817 gfc_symbol *sym;
4818 gfc_entry_list *el;
4819 int contains_statements = 0;
4820 int seen_error = 0;
4821
4822 push_state (&s1, COMP_CONTAINS, NULL);
4823 parent_ns = gfc_current_ns;
4824
4825 do
4826 {
4827 gfc_current_ns = gfc_get_namespace (parent_ns, 1);
4828
4829 gfc_current_ns->sibling = parent_ns->contained;
4830 parent_ns->contained = gfc_current_ns;
4831
4832 next:
4833 /* Process the next available statement. We come here if we got an error
4834 and rejected the last statement. */
4835 st = next_statement ();
4836
4837 switch (st)
4838 {
4839 case ST_NONE:
4840 unexpected_eof ();
4841
4842 case ST_FUNCTION:
4843 case ST_SUBROUTINE:
4844 contains_statements = 1;
4845 accept_statement (st);
4846
4847 push_state (&s2,
4848 (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
4849 gfc_new_block);
4850
4851 /* For internal procedures, create/update the symbol in the
4852 parent namespace. */
4853
4854 if (!module)
4855 {
4856 if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
4857 gfc_error ("Contained procedure %qs at %C is already "
4858 "ambiguous", gfc_new_block->name);
4859 else
4860 {
4861 if (gfc_add_procedure (&sym->attr, PROC_INTERNAL,
4862 sym->name,
4863 &gfc_new_block->declared_at))
4864 {
4865 if (st == ST_FUNCTION)
4866 gfc_add_function (&sym->attr, sym->name,
4867 &gfc_new_block->declared_at);
4868 else
4869 gfc_add_subroutine (&sym->attr, sym->name,
4870 &gfc_new_block->declared_at);
4871 }
4872 }
4873
4874 gfc_commit_symbols ();
4875 }
4876 else
4877 sym = gfc_new_block;
4878
4879 /* Mark this as a contained function, so it isn't replaced
4880 by other module functions. */
4881 sym->attr.contained = 1;
4882
4883 /* Set implicit_pure so that it can be reset if any of the
4884 tests for purity fail. This is used for some optimisation
4885 during translation. */
4886 if (!sym->attr.pure)
4887 sym->attr.implicit_pure = 1;
4888
4889 parse_progunit (ST_NONE);
4890
4891 /* Fix up any sibling functions that refer to this one. */
4892 gfc_fixup_sibling_symbols (sym, gfc_current_ns);
4893 /* Or refer to any of its alternate entry points. */
4894 for (el = gfc_current_ns->entries; el; el = el->next)
4895 gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
4896
4897 gfc_current_ns->code = s2.head;
4898 gfc_current_ns = parent_ns;
4899
4900 pop_state ();
4901 break;
4902
4903 /* These statements are associated with the end of the host unit. */
4904 case ST_END_FUNCTION:
4905 case ST_END_MODULE:
4906 case ST_END_PROGRAM:
4907 case ST_END_SUBROUTINE:
4908 accept_statement (st);
4909 gfc_current_ns->code = s1.head;
4910 break;
4911
4912 default:
4913 gfc_error ("Unexpected %s statement in CONTAINS section at %C",
4914 gfc_ascii_statement (st));
4915 reject_statement ();
4916 seen_error = 1;
4917 goto next;
4918 break;
4919 }
4920 }
4921 while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
4922 && st != ST_END_MODULE && st != ST_END_PROGRAM);
4923
4924 /* The first namespace in the list is guaranteed to not have
4925 anything (worthwhile) in it. */
4926 tmp = gfc_current_ns;
4927 gfc_current_ns = parent_ns;
4928 if (seen_error && tmp->refs > 1)
4929 gfc_free_namespace (tmp);
4930
4931 ns = gfc_current_ns->contained;
4932 gfc_current_ns->contained = ns->sibling;
4933 gfc_free_namespace (ns);
4934
4935 pop_state ();
4936 if (!contains_statements)
4937 gfc_notify_std (GFC_STD_F2008, "CONTAINS statement without "
4938 "FUNCTION or SUBROUTINE statement at %C");
4939 }
4940
4941
4942 /* Parse a PROGRAM, SUBROUTINE, FUNCTION unit or BLOCK construct. */
4943
4944 static void
4945 parse_progunit (gfc_statement st)
4946 {
4947 gfc_state_data *p;
4948 int n;
4949
4950 st = parse_spec (st);
4951 switch (st)
4952 {
4953 case ST_NONE:
4954 unexpected_eof ();
4955
4956 case ST_CONTAINS:
4957 /* This is not allowed within BLOCK! */
4958 if (gfc_current_state () != COMP_BLOCK)
4959 goto contains;
4960 break;
4961
4962 case_end:
4963 accept_statement (st);
4964 goto done;
4965
4966 default:
4967 break;
4968 }
4969
4970 if (gfc_current_state () == COMP_FUNCTION)
4971 gfc_check_function_type (gfc_current_ns);
4972
4973 loop:
4974 for (;;)
4975 {
4976 st = parse_executable (st);
4977
4978 switch (st)
4979 {
4980 case ST_NONE:
4981 unexpected_eof ();
4982
4983 case ST_CONTAINS:
4984 /* This is not allowed within BLOCK! */
4985 if (gfc_current_state () != COMP_BLOCK)
4986 goto contains;
4987 break;
4988
4989 case_end:
4990 accept_statement (st);
4991 goto done;
4992
4993 default:
4994 break;
4995 }
4996
4997 unexpected_statement (st);
4998 reject_statement ();
4999 st = next_statement ();
5000 }
5001
5002 contains:
5003 n = 0;
5004
5005 for (p = gfc_state_stack; p; p = p->previous)
5006 if (p->state == COMP_CONTAINS)
5007 n++;
5008
5009 if (gfc_find_state (COMP_MODULE) == true)
5010 n--;
5011
5012 if (n > 0)
5013 {
5014 gfc_error ("CONTAINS statement at %C is already in a contained "
5015 "program unit");
5016 reject_statement ();
5017 st = next_statement ();
5018 goto loop;
5019 }
5020
5021 parse_contained (0);
5022
5023 done:
5024 gfc_current_ns->code = gfc_state_stack->head;
5025 if (gfc_state_stack->state == COMP_PROGRAM
5026 || gfc_state_stack->state == COMP_MODULE
5027 || gfc_state_stack->state == COMP_SUBROUTINE
5028 || gfc_state_stack->state == COMP_FUNCTION
5029 || gfc_state_stack->state == COMP_BLOCK)
5030 gfc_current_ns->oacc_declare_clauses
5031 = gfc_state_stack->ext.oacc_declare_clauses;
5032 }
5033
5034
5035 /* Come here to complain about a global symbol already in use as
5036 something else. */
5037
5038 void
5039 gfc_global_used (gfc_gsymbol *sym, locus *where)
5040 {
5041 const char *name;
5042
5043 if (where == NULL)
5044 where = &gfc_current_locus;
5045
5046 switch(sym->type)
5047 {
5048 case GSYM_PROGRAM:
5049 name = "PROGRAM";
5050 break;
5051 case GSYM_FUNCTION:
5052 name = "FUNCTION";
5053 break;
5054 case GSYM_SUBROUTINE:
5055 name = "SUBROUTINE";
5056 break;
5057 case GSYM_COMMON:
5058 name = "COMMON";
5059 break;
5060 case GSYM_BLOCK_DATA:
5061 name = "BLOCK DATA";
5062 break;
5063 case GSYM_MODULE:
5064 name = "MODULE";
5065 break;
5066 default:
5067 gfc_internal_error ("gfc_global_used(): Bad type");
5068 name = NULL;
5069 }
5070
5071 if (sym->binding_label)
5072 gfc_error ("Global binding name %qs at %L is already being used as a %s "
5073 "at %L", sym->binding_label, where, name, &sym->where);
5074 else
5075 gfc_error ("Global name %qs at %L is already being used as a %s at %L",
5076 sym->name, where, name, &sym->where);
5077 }
5078
5079
5080 /* Parse a block data program unit. */
5081
5082 static void
5083 parse_block_data (void)
5084 {
5085 gfc_statement st;
5086 static locus blank_locus;
5087 static int blank_block=0;
5088 gfc_gsymbol *s;
5089
5090 gfc_current_ns->proc_name = gfc_new_block;
5091 gfc_current_ns->is_block_data = 1;
5092
5093 if (gfc_new_block == NULL)
5094 {
5095 if (blank_block)
5096 gfc_error ("Blank BLOCK DATA at %C conflicts with "
5097 "prior BLOCK DATA at %L", &blank_locus);
5098 else
5099 {
5100 blank_block = 1;
5101 blank_locus = gfc_current_locus;
5102 }
5103 }
5104 else
5105 {
5106 s = gfc_get_gsymbol (gfc_new_block->name);
5107 if (s->defined
5108 || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
5109 gfc_global_used (s, &gfc_new_block->declared_at);
5110 else
5111 {
5112 s->type = GSYM_BLOCK_DATA;
5113 s->where = gfc_new_block->declared_at;
5114 s->defined = 1;
5115 }
5116 }
5117
5118 st = parse_spec (ST_NONE);
5119
5120 while (st != ST_END_BLOCK_DATA)
5121 {
5122 gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
5123 gfc_ascii_statement (st));
5124 reject_statement ();
5125 st = next_statement ();
5126 }
5127 }
5128
5129
5130 /* Parse a module subprogram. */
5131
5132 static void
5133 parse_module (void)
5134 {
5135 gfc_statement st;
5136 gfc_gsymbol *s;
5137 bool error;
5138
5139 s = gfc_get_gsymbol (gfc_new_block->name);
5140 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
5141 gfc_global_used (s, &gfc_new_block->declared_at);
5142 else
5143 {
5144 s->type = GSYM_MODULE;
5145 s->where = gfc_new_block->declared_at;
5146 s->defined = 1;
5147 }
5148
5149 st = parse_spec (ST_NONE);
5150
5151 error = false;
5152 loop:
5153 switch (st)
5154 {
5155 case ST_NONE:
5156 unexpected_eof ();
5157
5158 case ST_CONTAINS:
5159 parse_contained (1);
5160 break;
5161
5162 case ST_END_MODULE:
5163 accept_statement (st);
5164 break;
5165
5166 default:
5167 gfc_error ("Unexpected %s statement in MODULE at %C",
5168 gfc_ascii_statement (st));
5169
5170 error = true;
5171 reject_statement ();
5172 st = next_statement ();
5173 goto loop;
5174 }
5175
5176 /* Make sure not to free the namespace twice on error. */
5177 if (!error)
5178 s->ns = gfc_current_ns;
5179 }
5180
5181
5182 /* Add a procedure name to the global symbol table. */
5183
5184 static void
5185 add_global_procedure (bool sub)
5186 {
5187 gfc_gsymbol *s;
5188
5189 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5190 name is a global identifier. */
5191 if (!gfc_new_block->binding_label || gfc_notification_std (GFC_STD_F2008))
5192 {
5193 s = gfc_get_gsymbol (gfc_new_block->name);
5194
5195 if (s->defined
5196 || (s->type != GSYM_UNKNOWN
5197 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5198 {
5199 gfc_global_used (s, &gfc_new_block->declared_at);
5200 /* Silence follow-up errors. */
5201 gfc_new_block->binding_label = NULL;
5202 }
5203 else
5204 {
5205 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5206 s->sym_name = gfc_new_block->name;
5207 s->where = gfc_new_block->declared_at;
5208 s->defined = 1;
5209 s->ns = gfc_current_ns;
5210 }
5211 }
5212
5213 /* Don't add the symbol multiple times. */
5214 if (gfc_new_block->binding_label
5215 && (!gfc_notification_std (GFC_STD_F2008)
5216 || strcmp (gfc_new_block->name, gfc_new_block->binding_label) != 0))
5217 {
5218 s = gfc_get_gsymbol (gfc_new_block->binding_label);
5219
5220 if (s->defined
5221 || (s->type != GSYM_UNKNOWN
5222 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
5223 {
5224 gfc_global_used (s, &gfc_new_block->declared_at);
5225 /* Silence follow-up errors. */
5226 gfc_new_block->binding_label = NULL;
5227 }
5228 else
5229 {
5230 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5231 s->sym_name = gfc_new_block->name;
5232 s->binding_label = gfc_new_block->binding_label;
5233 s->where = gfc_new_block->declared_at;
5234 s->defined = 1;
5235 s->ns = gfc_current_ns;
5236 }
5237 }
5238 }
5239
5240
5241 /* Add a program to the global symbol table. */
5242
5243 static void
5244 add_global_program (void)
5245 {
5246 gfc_gsymbol *s;
5247
5248 if (gfc_new_block == NULL)
5249 return;
5250 s = gfc_get_gsymbol (gfc_new_block->name);
5251
5252 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
5253 gfc_global_used (s, &gfc_new_block->declared_at);
5254 else
5255 {
5256 s->type = GSYM_PROGRAM;
5257 s->where = gfc_new_block->declared_at;
5258 s->defined = 1;
5259 s->ns = gfc_current_ns;
5260 }
5261 }
5262
5263
5264 /* Resolve all the program units. */
5265 static void
5266 resolve_all_program_units (gfc_namespace *gfc_global_ns_list)
5267 {
5268 gfc_free_dt_list ();
5269 gfc_current_ns = gfc_global_ns_list;
5270 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5271 {
5272 if (gfc_current_ns->proc_name
5273 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5274 continue; /* Already resolved. */
5275
5276 if (gfc_current_ns->proc_name)
5277 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5278 gfc_resolve (gfc_current_ns);
5279 gfc_current_ns->derived_types = gfc_derived_types;
5280 gfc_derived_types = NULL;
5281 }
5282 }
5283
5284
5285 static void
5286 clean_up_modules (gfc_gsymbol *gsym)
5287 {
5288 if (gsym == NULL)
5289 return;
5290
5291 clean_up_modules (gsym->left);
5292 clean_up_modules (gsym->right);
5293
5294 if (gsym->type != GSYM_MODULE || !gsym->ns)
5295 return;
5296
5297 gfc_current_ns = gsym->ns;
5298 gfc_derived_types = gfc_current_ns->derived_types;
5299 gfc_done_2 ();
5300 gsym->ns = NULL;
5301 return;
5302 }
5303
5304
5305 /* Translate all the program units. This could be in a different order
5306 to resolution if there are forward references in the file. */
5307 static void
5308 translate_all_program_units (gfc_namespace *gfc_global_ns_list)
5309 {
5310 int errors;
5311
5312 gfc_current_ns = gfc_global_ns_list;
5313 gfc_get_errors (NULL, &errors);
5314
5315 /* We first translate all modules to make sure that later parts
5316 of the program can use the decl. Then we translate the nonmodules. */
5317
5318 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5319 {
5320 if (!gfc_current_ns->proc_name
5321 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5322 continue;
5323
5324 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5325 gfc_derived_types = gfc_current_ns->derived_types;
5326 gfc_generate_module_code (gfc_current_ns);
5327 gfc_current_ns->translated = 1;
5328 }
5329
5330 gfc_current_ns = gfc_global_ns_list;
5331 for (; !errors && gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5332 {
5333 if (gfc_current_ns->proc_name
5334 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5335 continue;
5336
5337 gfc_current_locus = gfc_current_ns->proc_name->declared_at;
5338 gfc_derived_types = gfc_current_ns->derived_types;
5339 gfc_generate_code (gfc_current_ns);
5340 gfc_current_ns->translated = 1;
5341 }
5342
5343 /* Clean up all the namespaces after translation. */
5344 gfc_current_ns = gfc_global_ns_list;
5345 for (;gfc_current_ns;)
5346 {
5347 gfc_namespace *ns;
5348
5349 if (gfc_current_ns->proc_name
5350 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
5351 {
5352 gfc_current_ns = gfc_current_ns->sibling;
5353 continue;
5354 }
5355
5356 ns = gfc_current_ns->sibling;
5357 gfc_derived_types = gfc_current_ns->derived_types;
5358 gfc_done_2 ();
5359 gfc_current_ns = ns;
5360 }
5361
5362 clean_up_modules (gfc_gsym_root);
5363 }
5364
5365
5366 /* Top level parser. */
5367
5368 bool
5369 gfc_parse_file (void)
5370 {
5371 int seen_program, errors_before, errors;
5372 gfc_state_data top, s;
5373 gfc_statement st;
5374 locus prog_locus;
5375 gfc_namespace *next;
5376
5377 gfc_start_source_files ();
5378
5379 top.state = COMP_NONE;
5380 top.sym = NULL;
5381 top.previous = NULL;
5382 top.head = top.tail = NULL;
5383 top.do_variable = NULL;
5384
5385 gfc_state_stack = &top;
5386
5387 gfc_clear_new_st ();
5388
5389 gfc_statement_label = NULL;
5390
5391 if (setjmp (eof_buf))
5392 return false; /* Come here on unexpected EOF */
5393
5394 /* Prepare the global namespace that will contain the
5395 program units. */
5396 gfc_global_ns_list = next = NULL;
5397
5398 seen_program = 0;
5399 errors_before = 0;
5400
5401 /* Exit early for empty files. */
5402 if (gfc_at_eof ())
5403 goto done;
5404
5405 loop:
5406 gfc_init_2 ();
5407 st = next_statement ();
5408 switch (st)
5409 {
5410 case ST_NONE:
5411 gfc_done_2 ();
5412 goto done;
5413
5414 case ST_PROGRAM:
5415 if (seen_program)
5416 goto duplicate_main;
5417 seen_program = 1;
5418 prog_locus = gfc_current_locus;
5419
5420 push_state (&s, COMP_PROGRAM, gfc_new_block);
5421 main_program_symbol(gfc_current_ns, gfc_new_block->name);
5422 accept_statement (st);
5423 add_global_program ();
5424 parse_progunit (ST_NONE);
5425 goto prog_units;
5426 break;
5427
5428 case ST_SUBROUTINE:
5429 add_global_procedure (true);
5430 push_state (&s, COMP_SUBROUTINE, gfc_new_block);
5431 accept_statement (st);
5432 parse_progunit (ST_NONE);
5433 goto prog_units;
5434 break;
5435
5436 case ST_FUNCTION:
5437 add_global_procedure (false);
5438 push_state (&s, COMP_FUNCTION, gfc_new_block);
5439 accept_statement (st);
5440 parse_progunit (ST_NONE);
5441 goto prog_units;
5442 break;
5443
5444 case ST_BLOCK_DATA:
5445 push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
5446 accept_statement (st);
5447 parse_block_data ();
5448 break;
5449
5450 case ST_MODULE:
5451 push_state (&s, COMP_MODULE, gfc_new_block);
5452 accept_statement (st);
5453
5454 gfc_get_errors (NULL, &errors_before);
5455 parse_module ();
5456 break;
5457
5458 /* Anything else starts a nameless main program block. */
5459 default:
5460 if (seen_program)
5461 goto duplicate_main;
5462 seen_program = 1;
5463 prog_locus = gfc_current_locus;
5464
5465 push_state (&s, COMP_PROGRAM, gfc_new_block);
5466 main_program_symbol (gfc_current_ns, "MAIN__");
5467 parse_progunit (st);
5468 goto prog_units;
5469 break;
5470 }
5471
5472 /* Handle the non-program units. */
5473 gfc_current_ns->code = s.head;
5474
5475 gfc_resolve (gfc_current_ns);
5476
5477 /* Dump the parse tree if requested. */
5478 if (flag_dump_fortran_original)
5479 gfc_dump_parse_tree (gfc_current_ns, stdout);
5480
5481 gfc_get_errors (NULL, &errors);
5482 if (s.state == COMP_MODULE)
5483 {
5484 gfc_dump_module (s.sym->name, errors_before == errors);
5485 gfc_current_ns->derived_types = gfc_derived_types;
5486 gfc_derived_types = NULL;
5487 goto prog_units;
5488 }
5489 else
5490 {
5491 if (errors == 0)
5492 gfc_generate_code (gfc_current_ns);
5493 pop_state ();
5494 gfc_done_2 ();
5495 }
5496
5497 goto loop;
5498
5499 prog_units:
5500 /* The main program and non-contained procedures are put
5501 in the global namespace list, so that they can be processed
5502 later and all their interfaces resolved. */
5503 gfc_current_ns->code = s.head;
5504 if (next)
5505 {
5506 for (; next->sibling; next = next->sibling)
5507 ;
5508 next->sibling = gfc_current_ns;
5509 }
5510 else
5511 gfc_global_ns_list = gfc_current_ns;
5512
5513 next = gfc_current_ns;
5514
5515 pop_state ();
5516 goto loop;
5517
5518 done:
5519
5520 /* Do the resolution. */
5521 resolve_all_program_units (gfc_global_ns_list);
5522
5523 /* Do the parse tree dump. */
5524 gfc_current_ns
5525 = flag_dump_fortran_original ? gfc_global_ns_list : NULL;
5526
5527 for (; gfc_current_ns; gfc_current_ns = gfc_current_ns->sibling)
5528 if (!gfc_current_ns->proc_name
5529 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5530 {
5531 gfc_dump_parse_tree (gfc_current_ns, stdout);
5532 fputs ("------------------------------------------\n\n", stdout);
5533 }
5534
5535 /* Do the translation. */
5536 translate_all_program_units (gfc_global_ns_list);
5537
5538 gfc_end_source_files ();
5539 return true;
5540
5541 duplicate_main:
5542 /* If we see a duplicate main program, shut down. If the second
5543 instance is an implied main program, i.e. data decls or executable
5544 statements, we're in for lots of errors. */
5545 gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
5546 reject_statement ();
5547 gfc_done_2 ();
5548 return true;
5549 }
5550
5551 /* Return true if this state data represents an OpenACC region. */
5552 bool
5553 is_oacc (gfc_state_data *sd)
5554 {
5555 switch (sd->construct->op)
5556 {
5557 case EXEC_OACC_PARALLEL_LOOP:
5558 case EXEC_OACC_PARALLEL:
5559 case EXEC_OACC_KERNELS_LOOP:
5560 case EXEC_OACC_KERNELS:
5561 case EXEC_OACC_DATA:
5562 case EXEC_OACC_HOST_DATA:
5563 case EXEC_OACC_LOOP:
5564 case EXEC_OACC_UPDATE:
5565 case EXEC_OACC_WAIT:
5566 case EXEC_OACC_CACHE:
5567 case EXEC_OACC_ENTER_DATA:
5568 case EXEC_OACC_EXIT_DATA:
5569 return true;
5570
5571 default:
5572 return false;
5573 }
5574 }