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