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