Debian Codegraph @

gcc/fortran/openmp.cc

raw
1 /* OpenMP directive matching and resolving.
2 Copyright (C) 2005-2025 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #define INCLUDE_VECTOR
22 #define INCLUDE_STRING
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "options.h"
27 #include "gfortran.h"
28 #include "arith.h"
29 #include "match.h"
30 #include "parse.h"
31 #include "constructor.h"
32 #include "diagnostic.h"
33 #include "gomp-constants.h"
34 #include "target-memory.h" /* For gfc_encode_character. */
35 #include "bitmap.h"
36 #include "omp-api.h" /* For omp_runtime_api_procname. */
37
38 location_t gfc_get_location (locus *);
39
40 static gfc_statement omp_code_to_statement (gfc_code *);
41
42 enum gfc_omp_directive_kind {
43 GFC_OMP_DIR_DECLARATIVE,
44 GFC_OMP_DIR_EXECUTABLE,
45 GFC_OMP_DIR_INFORMATIONAL,
46 GFC_OMP_DIR_META,
47 GFC_OMP_DIR_SUBSIDIARY,
48 GFC_OMP_DIR_UTILITY
49 };
50
51 struct gfc_omp_directive {
52 const char *name;
53 enum gfc_omp_directive_kind kind;
54 gfc_statement st;
55 };
56
57 /* Alphabetically sorted OpenMP clauses, except that longer strings are before
58 substrings; excludes combined/composite directives. See note for "ordered"
59 and "nothing". */
60
61 static const struct gfc_omp_directive gfc_omp_directives[] = {
62 {"allocate", GFC_OMP_DIR_DECLARATIVE, ST_OMP_ALLOCATE},
63 {"allocators", GFC_OMP_DIR_EXECUTABLE, ST_OMP_ALLOCATORS},
64 {"assumes", GFC_OMP_DIR_INFORMATIONAL, ST_OMP_ASSUMES},
65 {"assume", GFC_OMP_DIR_INFORMATIONAL, ST_OMP_ASSUME},
66 {"atomic", GFC_OMP_DIR_EXECUTABLE, ST_OMP_ATOMIC},
67 {"barrier", GFC_OMP_DIR_EXECUTABLE, ST_OMP_BARRIER},
68 {"cancellation point", GFC_OMP_DIR_EXECUTABLE, ST_OMP_CANCELLATION_POINT},
69 {"cancel", GFC_OMP_DIR_EXECUTABLE, ST_OMP_CANCEL},
70 {"critical", GFC_OMP_DIR_EXECUTABLE, ST_OMP_CRITICAL},
71 /* {"declare mapper", GFC_OMP_DIR_DECLARATIVE, ST_OMP_DECLARE_MAPPER}, */
72 {"declare reduction", GFC_OMP_DIR_DECLARATIVE, ST_OMP_DECLARE_REDUCTION},
73 {"declare simd", GFC_OMP_DIR_DECLARATIVE, ST_OMP_DECLARE_SIMD},
74 {"declare target", GFC_OMP_DIR_DECLARATIVE, ST_OMP_DECLARE_TARGET},
75 {"declare variant", GFC_OMP_DIR_DECLARATIVE, ST_OMP_DECLARE_VARIANT},
76 {"depobj", GFC_OMP_DIR_EXECUTABLE, ST_OMP_DEPOBJ},
77 {"dispatch", GFC_OMP_DIR_EXECUTABLE, ST_OMP_DISPATCH},
78 {"distribute", GFC_OMP_DIR_EXECUTABLE, ST_OMP_DISTRIBUTE},
79 {"do", GFC_OMP_DIR_EXECUTABLE, ST_OMP_DO},
80 /* "error" becomes GFC_OMP_DIR_EXECUTABLE with at(execution) */
81 {"error", GFC_OMP_DIR_UTILITY, ST_OMP_ERROR},
82 {"flush", GFC_OMP_DIR_EXECUTABLE, ST_OMP_FLUSH},
83 {"interop", GFC_OMP_DIR_EXECUTABLE, ST_OMP_INTEROP},
84 {"loop", GFC_OMP_DIR_EXECUTABLE, ST_OMP_LOOP},
85 {"masked", GFC_OMP_DIR_EXECUTABLE, ST_OMP_MASKED},
86 {"metadirective", GFC_OMP_DIR_META, ST_OMP_METADIRECTIVE},
87 /* Note: gfc_match_omp_nothing returns ST_NONE. */
88 {"nothing", GFC_OMP_DIR_UTILITY, ST_OMP_NOTHING},
89 /* Special case; for now map to the first one.
90 ordered-blockassoc = ST_OMP_ORDERED
91 ordered-standalone = ST_OMP_ORDERED_DEPEND + depend/doacross. */
92 {"ordered", GFC_OMP_DIR_EXECUTABLE, ST_OMP_ORDERED},
93 {"parallel", GFC_OMP_DIR_EXECUTABLE, ST_OMP_PARALLEL},
94 {"requires", GFC_OMP_DIR_INFORMATIONAL, ST_OMP_REQUIRES},
95 {"scan", GFC_OMP_DIR_SUBSIDIARY, ST_OMP_SCAN},
96 {"scope", GFC_OMP_DIR_EXECUTABLE, ST_OMP_SCOPE},
97 {"sections", GFC_OMP_DIR_EXECUTABLE, ST_OMP_SECTIONS},
98 {"section", GFC_OMP_DIR_SUBSIDIARY, ST_OMP_SECTION},
99 {"simd", GFC_OMP_DIR_EXECUTABLE, ST_OMP_SIMD},
100 {"single", GFC_OMP_DIR_EXECUTABLE, ST_OMP_SINGLE},
101 {"target data", GFC_OMP_DIR_EXECUTABLE, ST_OMP_TARGET_DATA},
102 {"target enter data", GFC_OMP_DIR_EXECUTABLE, ST_OMP_TARGET_ENTER_DATA},
103 {"target exit data", GFC_OMP_DIR_EXECUTABLE, ST_OMP_TARGET_EXIT_DATA},
104 {"target update", GFC_OMP_DIR_EXECUTABLE, ST_OMP_TARGET_UPDATE},
105 {"target", GFC_OMP_DIR_EXECUTABLE, ST_OMP_TARGET},
106 {"taskloop", GFC_OMP_DIR_EXECUTABLE, ST_OMP_TASKLOOP},
107 {"taskwait", GFC_OMP_DIR_EXECUTABLE, ST_OMP_TASKWAIT},
108 {"taskyield", GFC_OMP_DIR_EXECUTABLE, ST_OMP_TASKYIELD},
109 {"task", GFC_OMP_DIR_EXECUTABLE, ST_OMP_TASK},
110 {"teams", GFC_OMP_DIR_EXECUTABLE, ST_OMP_TEAMS},
111 {"threadprivate", GFC_OMP_DIR_DECLARATIVE, ST_OMP_THREADPRIVATE},
112 {"tile", GFC_OMP_DIR_EXECUTABLE, ST_OMP_TILE},
113 {"unroll", GFC_OMP_DIR_EXECUTABLE, ST_OMP_UNROLL},
114 {"workshare", GFC_OMP_DIR_EXECUTABLE, ST_OMP_WORKSHARE},
115 };
116
117
118 /* Match an end of OpenMP directive. End of OpenMP directive is optional
119 whitespace, followed by '\n' or comment '!'. In the special case where a
120 context selector is being matched, match against ')' instead. */
121
122 static match
123 gfc_match_omp_eos (void)
124 {
125 locus old_loc;
126 char c;
127
128 old_loc = gfc_current_locus;
129 gfc_gobble_whitespace ();
130
131 if (gfc_matching_omp_context_selector)
132 {
133 if (gfc_peek_ascii_char () == ')')
134 return MATCH_YES;
135 }
136 else
137 {
138 c = gfc_next_ascii_char ();
139 switch (c)
140 {
141 case '!':
142 do
143 c = gfc_next_ascii_char ();
144 while (c != '\n');
145 /* Fall through */
146
147 case '\n':
148 return MATCH_YES;
149 }
150 }
151
152 gfc_current_locus = old_loc;
153 return MATCH_NO;
154 }
155
156 match
157 gfc_match_omp_eos_error (void)
158 {
159 if (gfc_match_omp_eos() == MATCH_YES)
160 return MATCH_YES;
161
162 gfc_error ("Unexpected junk at %C");
163 return MATCH_ERROR;
164 }
165
166
167 /* Free an omp_clauses structure. */
168
169 void
170 gfc_free_omp_clauses (gfc_omp_clauses *c)
171 {
172 int i;
173 if (c == NULL)
174 return;
175
176 gfc_free_expr (c->if_expr);
177 for (i = 0; i < OMP_IF_LAST; i++)
178 gfc_free_expr (c->if_exprs[i]);
179 gfc_free_expr (c->self_expr);
180 gfc_free_expr (c->final_expr);
181 gfc_free_expr (c->num_threads);
182 gfc_free_expr (c->chunk_size);
183 gfc_free_expr (c->safelen_expr);
184 gfc_free_expr (c->simdlen_expr);
185 gfc_free_expr (c->num_teams_lower);
186 gfc_free_expr (c->num_teams_upper);
187 gfc_free_expr (c->device);
188 gfc_free_expr (c->thread_limit);
189 gfc_free_expr (c->dist_chunk_size);
190 gfc_free_expr (c->grainsize);
191 gfc_free_expr (c->hint);
192 gfc_free_expr (c->num_tasks);
193 gfc_free_expr (c->priority);
194 gfc_free_expr (c->detach);
195 gfc_free_expr (c->novariants);
196 gfc_free_expr (c->nocontext);
197 gfc_free_expr (c->async_expr);
198 gfc_free_expr (c->gang_num_expr);
199 gfc_free_expr (c->gang_static_expr);
200 gfc_free_expr (c->worker_expr);
201 gfc_free_expr (c->vector_expr);
202 gfc_free_expr (c->num_gangs_expr);
203 gfc_free_expr (c->num_workers_expr);
204 gfc_free_expr (c->vector_length_expr);
205 for (i = 0; i < OMP_LIST_NUM; i++)
206 gfc_free_omp_namelist (c->lists[i],
207 i == OMP_LIST_AFFINITY || i == OMP_LIST_DEPEND,
208 i == OMP_LIST_ALLOCATE,
209 i == OMP_LIST_USES_ALLOCATORS,
210 i == OMP_LIST_INIT);
211 gfc_free_expr_list (c->wait_list);
212 gfc_free_expr_list (c->tile_list);
213 gfc_free_expr_list (c->sizes_list);
214 free (CONST_CAST (char *, c->critical_name));
215 if (c->assume)
216 {
217 free (c->assume->absent);
218 free (c->assume->contains);
219 gfc_free_expr_list (c->assume->holds);
220 free (c->assume);
221 }
222 free (c);
223 }
224
225 /* Free oacc_declare structures. */
226
227 void
228 gfc_free_oacc_declare_clauses (struct gfc_oacc_declare *oc)
229 {
230 struct gfc_oacc_declare *decl = oc;
231
232 do
233 {
234 struct gfc_oacc_declare *next;
235
236 next = decl->next;
237 gfc_free_omp_clauses (decl->clauses);
238 free (decl);
239 decl = next;
240 }
241 while (decl);
242 }
243
244 /* Free expression list. */
245 void
246 gfc_free_expr_list (gfc_expr_list *list)
247 {
248 gfc_expr_list *n;
249
250 for (; list; list = n)
251 {
252 n = list->next;
253 free (list);
254 }
255 }
256
257 /* Free an !$omp declare simd construct list. */
258
259 void
260 gfc_free_omp_declare_simd (gfc_omp_declare_simd *ods)
261 {
262 if (ods)
263 {
264 gfc_free_omp_clauses (ods->clauses);
265 free (ods);
266 }
267 }
268
269 void
270 gfc_free_omp_declare_simd_list (gfc_omp_declare_simd *list)
271 {
272 while (list)
273 {
274 gfc_omp_declare_simd *current = list;
275 list = list->next;
276 gfc_free_omp_declare_simd (current);
277 }
278 }
279
280 static void
281 gfc_free_omp_trait_property_list (gfc_omp_trait_property *list)
282 {
283 while (list)
284 {
285 gfc_omp_trait_property *current = list;
286 list = list->next;
287 switch (current->property_kind)
288 {
289 case OMP_TRAIT_PROPERTY_ID:
290 free (current->name);
291 break;
292 case OMP_TRAIT_PROPERTY_NAME_LIST:
293 if (current->is_name)
294 free (current->name);
295 break;
296 case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
297 gfc_free_omp_clauses (current->clauses);
298 break;
299 default:
300 break;
301 }
302 free (current);
303 }
304 }
305
306 static void
307 gfc_free_omp_selector_list (gfc_omp_selector *list)
308 {
309 while (list)
310 {
311 gfc_omp_selector *current = list;
312 list = list->next;
313 gfc_free_omp_trait_property_list (current->properties);
314 free (current);
315 }
316 }
317
318 static void
319 gfc_free_omp_set_selector_list (gfc_omp_set_selector *list)
320 {
321 while (list)
322 {
323 gfc_omp_set_selector *current = list;
324 list = list->next;
325 gfc_free_omp_selector_list (current->trait_selectors);
326 free (current);
327 }
328 }
329
330 /* Free an !$omp declare variant construct list. */
331
332 void
333 gfc_free_omp_declare_variant_list (gfc_omp_declare_variant *list)
334 {
335 while (list)
336 {
337 gfc_omp_declare_variant *current = list;
338 list = list->next;
339 gfc_free_omp_set_selector_list (current->set_selectors);
340 gfc_free_omp_namelist (current->adjust_args_list, false, false, false,
341 false);
342 free (current);
343 }
344 }
345
346 /* Free an !$omp declare reduction. */
347
348 void
349 gfc_free_omp_udr (gfc_omp_udr *omp_udr)
350 {
351 if (omp_udr)
352 {
353 gfc_free_omp_udr (omp_udr->next);
354 gfc_free_namespace (omp_udr->combiner_ns);
355 if (omp_udr->initializer_ns)
356 gfc_free_namespace (omp_udr->initializer_ns);
357 free (omp_udr);
358 }
359 }
360
361 /* Free variants of an !$omp metadirective construct. */
362
363 void
364 gfc_free_omp_variants (gfc_omp_variant *variant)
365 {
366 while (variant)
367 {
368 gfc_omp_variant *next_variant = variant->next;
369 gfc_free_omp_set_selector_list (variant->selectors);
370 free (variant);
371 variant = next_variant;
372 }
373 }
374
375 static gfc_omp_udr *
376 gfc_find_omp_udr (gfc_namespace *ns, const char *name, gfc_typespec *ts)
377 {
378 gfc_symtree *st;
379
380 if (ns == NULL)
381 ns = gfc_current_ns;
382 do
383 {
384 gfc_omp_udr *omp_udr;
385
386 st = gfc_find_symtree (ns->omp_udr_root, name);
387 if (st != NULL)
388 {
389 for (omp_udr = st->n.omp_udr; omp_udr; omp_udr = omp_udr->next)
390 if (ts == NULL)
391 return omp_udr;
392 else if (gfc_compare_types (&omp_udr->ts, ts))
393 {
394 if (ts->type == BT_CHARACTER)
395 {
396 if (omp_udr->ts.u.cl->length == NULL)
397 return omp_udr;
398 if (ts->u.cl->length == NULL)
399 continue;
400 if (gfc_compare_expr (omp_udr->ts.u.cl->length,
401 ts->u.cl->length,
402 INTRINSIC_EQ) != 0)
403 continue;
404 }
405 return omp_udr;
406 }
407 }
408
409 /* Don't escape an interface block. */
410 if (ns && !ns->has_import_set
411 && ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY)
412 break;
413
414 ns = ns->parent;
415 }
416 while (ns != NULL);
417
418 return NULL;
419 }
420
421
422 /* Match a variable/common block list and construct a namelist from it;
423 if has_all_memory != NULL, *has_all_memory is set and omp_all_memory
424 yields a list->sym NULL entry. */
425
426 static match
427 gfc_match_omp_variable_list (const char *str, gfc_omp_namelist **list,
428 bool allow_common, bool *end_colon = NULL,
429 gfc_omp_namelist ***headp = NULL,
430 bool allow_sections = false,
431 bool allow_derived = false,
432 bool *has_all_memory = NULL,
433 bool reject_common_vars = false,
434 bool reverse_order = false)
435 {
436 gfc_omp_namelist *head, *tail, *p;
437 locus old_loc, cur_loc;
438 char n[GFC_MAX_SYMBOL_LEN+1];
439 gfc_symbol *sym;
440 match m;
441 gfc_symtree *st;
442
443 head = tail = NULL;
444
445 old_loc = gfc_current_locus;
446 if (has_all_memory)
447 *has_all_memory = false;
448 m = gfc_match (str);
449 if (m != MATCH_YES)
450 return m;
451
452 for (;;)
453 {
454 gfc_gobble_whitespace ();
455 cur_loc = gfc_current_locus;
456
457 m = gfc_match_name (n);
458 if (m == MATCH_YES && strcmp (n, "omp_all_memory") == 0)
459 {
460 locus loc = gfc_get_location_range (NULL, 0, &cur_loc, 1,
461 &gfc_current_locus);
462 if (!has_all_memory)
463 {
464 gfc_error ("%<omp_all_memory%> at %L not permitted in this "
465 "clause", &loc);
466 goto cleanup;
467 }
468 *has_all_memory = true;
469 p = gfc_get_omp_namelist ();
470 if (head == NULL)
471 head = tail = p;
472 else
473 {
474 tail->next = p;
475 tail = tail->next;
476 }
477 tail->where = loc;
478 goto next_item;
479 }
480 if (m == MATCH_YES)
481 {
482 gfc_symtree *st;
483 if ((m = gfc_get_ha_sym_tree (n, &st) ? MATCH_ERROR : MATCH_YES)
484 == MATCH_YES)
485 sym = st->n.sym;
486 }
487 switch (m)
488 {
489 case MATCH_YES:
490 gfc_expr *expr;
491 expr = NULL;
492 gfc_gobble_whitespace ();
493 if ((allow_sections && gfc_peek_ascii_char () == '(')
494 || (allow_derived && gfc_peek_ascii_char () == '%'))
495 {
496 gfc_current_locus = cur_loc;
497 m = gfc_match_variable (&expr, 0);
498 switch (m)
499 {
500 case MATCH_ERROR:
501 goto cleanup;
502 case MATCH_NO:
503 goto syntax;
504 default:
505 break;
506 }
507 if (gfc_is_coindexed (expr))
508 {
509 gfc_error ("List item shall not be coindexed at %L",
510 &expr->where);
511 goto cleanup;
512 }
513 }
514 gfc_set_sym_referenced (sym);
515 p = gfc_get_omp_namelist ();
516 if (head == NULL)
517 head = tail = p;
518 else if (reverse_order)
519 {
520 p->next = head;
521 head = p;
522 }
523 else
524 {
525 tail->next = p;
526 tail = tail->next;
527 }
528 p->sym = sym;
529 p->expr = expr;
530 p->where = gfc_get_location_range (NULL, 0, &cur_loc, 1,
531 &gfc_current_locus);
532 if (reject_common_vars && sym->attr.in_common)
533 {
534 gcc_assert (allow_common);
535 gfc_error ("%qs at %L is part of the common block %</%s/%> and "
536 "may only be specificed implicitly via the named "
537 "common block", sym->name, &cur_loc,
538 sym->common_head->name);
539 goto cleanup;
540 }
541 goto next_item;
542 case MATCH_NO:
543 break;
544 case MATCH_ERROR:
545 goto cleanup;
546 }
547
548 if (!allow_common)
549 goto syntax;
550
551 m = gfc_match ("/ %n /", n);
552 if (m == MATCH_ERROR)
553 goto cleanup;
554 if (m == MATCH_NO)
555 goto syntax;
556
557 cur_loc = gfc_get_location_range (NULL, 0, &cur_loc, 1,
558 &gfc_current_locus);
559 st = gfc_find_symtree (gfc_current_ns->common_root, n);
560 if (st == NULL)
561 {
562 gfc_error ("COMMON block %</%s/%> not found at %L", n, &cur_loc);
563 goto cleanup;
564 }
565 for (sym = st->n.common->head; sym; sym = sym->common_next)
566 {
567 gfc_set_sym_referenced (sym);
568 p = gfc_get_omp_namelist ();
569 if (head == NULL)
570 head = tail = p;
571 else if (reverse_order)
572 {
573 p->next = head;
574 head = p;
575 }
576 else
577 {
578 tail->next = p;
579 tail = tail->next;
580 }
581 p->sym = sym;
582 p->where = cur_loc;
583 }
584
585 next_item:
586 if (end_colon && gfc_match_char (':') == MATCH_YES)
587 {
588 *end_colon = true;
589 break;
590 }
591 if (gfc_match_char (')') == MATCH_YES)
592 break;
593 if (gfc_match_char (',') != MATCH_YES)
594 goto syntax;
595 }
596
597 while (*list)
598 list = &(*list)->next;
599
600 *list = head;
601 if (headp)
602 *headp = list;
603 return MATCH_YES;
604
605 syntax:
606 gfc_error ("Syntax error in OpenMP variable list at %C");
607
608 cleanup:
609 gfc_free_omp_namelist (head, false, false, false, false);
610 gfc_current_locus = old_loc;
611 return MATCH_ERROR;
612 }
613
614 /* Match a variable/procedure/common block list and construct a namelist
615 from it. */
616
617 static match
618 gfc_match_omp_to_link (const char *str, gfc_omp_namelist **list)
619 {
620 gfc_omp_namelist *head, *tail, *p;
621 locus old_loc, cur_loc;
622 char n[GFC_MAX_SYMBOL_LEN+1];
623 gfc_symbol *sym;
624 match m;
625 gfc_symtree *st;
626
627 head = tail = NULL;
628
629 old_loc = gfc_current_locus;
630
631 m = gfc_match (str);
632 if (m != MATCH_YES)
633 return m;
634
635 for (;;)
636 {
637 cur_loc = gfc_current_locus;
638 m = gfc_match_symbol (&sym, 1);
639 switch (m)
640 {
641 case MATCH_YES:
642 p = gfc_get_omp_namelist ();
643 if (head == NULL)
644 head = tail = p;
645 else
646 {
647 tail->next = p;
648 tail = tail->next;
649 }
650 tail->sym = sym;
651 tail->where = cur_loc;
652 goto next_item;
653 case MATCH_NO:
654 break;
655 case MATCH_ERROR:
656 goto cleanup;
657 }
658
659 m = gfc_match (" / %n /", n);
660 if (m == MATCH_ERROR)
661 goto cleanup;
662 if (m == MATCH_NO)
663 goto syntax;
664
665 st = gfc_find_symtree (gfc_current_ns->common_root, n);
666 if (st == NULL)
667 {
668 gfc_error ("COMMON block /%s/ not found at %C", n);
669 goto cleanup;
670 }
671 p = gfc_get_omp_namelist ();
672 if (head == NULL)
673 head = tail = p;
674 else
675 {
676 tail->next = p;
677 tail = tail->next;
678 }
679 tail->u.common = st->n.common;
680 tail->where = cur_loc;
681
682 next_item:
683 if (gfc_match_char (')') == MATCH_YES)
684 break;
685 if (gfc_match_char (',') != MATCH_YES)
686 goto syntax;
687 }
688
689 while (*list)
690 list = &(*list)->next;
691
692 *list = head;
693 return MATCH_YES;
694
695 syntax:
696 gfc_error ("Syntax error in OpenMP variable list at %C");
697
698 cleanup:
699 gfc_free_omp_namelist (head, false, false, false, false);
700 gfc_current_locus = old_loc;
701 return MATCH_ERROR;
702 }
703
704 /* Match detach(event-handle). */
705
706 static match
707 gfc_match_omp_detach (gfc_expr **expr)
708 {
709 locus old_loc = gfc_current_locus;
710
711 if (gfc_match ("detach ( ") != MATCH_YES)
712 goto syntax_error;
713
714 if (gfc_match_variable (expr, 0) != MATCH_YES)
715 goto syntax_error;
716
717 if (gfc_match_char (')') != MATCH_YES)
718 goto syntax_error;
719
720 return MATCH_YES;
721
722 syntax_error:
723 gfc_error ("Syntax error in OpenMP detach clause at %C");
724 gfc_current_locus = old_loc;
725 return MATCH_ERROR;
726
727 }
728
729 /* Match doacross(sink : ...) construct a namelist from it;
730 if depend is true, match legacy 'depend(sink : ...)'. */
731
732 static match
733 gfc_match_omp_doacross_sink (gfc_omp_namelist **list, bool depend)
734 {
735 char n[GFC_MAX_SYMBOL_LEN+1];
736 gfc_omp_namelist *head, *tail, *p;
737 locus old_loc, cur_loc;
738 gfc_symbol *sym;
739
740 head = tail = NULL;
741
742 old_loc = gfc_current_locus;
743
744 for (;;)
745 {
746 gfc_gobble_whitespace ();
747 cur_loc = gfc_current_locus;
748
749 if (gfc_match_name (n) != MATCH_YES)
750 goto syntax;
751 locus loc = gfc_get_location_range (NULL, 0, &cur_loc, 1,
752 &gfc_current_locus);
753 if (UNLIKELY (strcmp (n, "omp_all_memory") == 0))
754 {
755 gfc_error ("%<omp_all_memory%> used with dependence-type "
756 "other than OUT or INOUT at %L", &loc);
757 goto cleanup;
758 }
759 sym = NULL;
760 if (!(strcmp (n, "omp_cur_iteration") == 0))
761 {
762 gfc_symtree *st;
763 if (gfc_get_ha_sym_tree (n, &st))
764 goto syntax;
765 sym = st->n.sym;
766 gfc_set_sym_referenced (sym);
767 }
768 p = gfc_get_omp_namelist ();
769 if (head == NULL)
770 {
771 head = tail = p;
772 head->u.depend_doacross_op = (depend ? OMP_DEPEND_SINK_FIRST
773 : OMP_DOACROSS_SINK_FIRST);
774 }
775 else
776 {
777 tail->next = p;
778 tail = tail->next;
779 tail->u.depend_doacross_op = OMP_DOACROSS_SINK;
780 }
781 tail->sym = sym;
782 tail->expr = NULL;
783 tail->where = loc;
784 if (gfc_match_char ('+') == MATCH_YES)
785 {
786 if (gfc_match_literal_constant (&tail->expr, 0) != MATCH_YES)
787 goto syntax;
788 }
789 else if (gfc_match_char ('-') == MATCH_YES)
790 {
791 if (gfc_match_literal_constant (&tail->expr, 0) != MATCH_YES)
792 goto syntax;
793 tail->expr = gfc_uminus (tail->expr);
794 }
795 if (gfc_match_char (')') == MATCH_YES)
796 break;
797 if (gfc_match_char (',') != MATCH_YES)
798 goto syntax;
799 }
800
801 while (*list)
802 list = &(*list)->next;
803
804 *list = head;
805 return MATCH_YES;
806
807 syntax:
808 gfc_error ("Syntax error in OpenMP SINK dependence-type list at %C");
809
810 cleanup:
811 gfc_free_omp_namelist (head, false, false, false, false);
812 gfc_current_locus = old_loc;
813 return MATCH_ERROR;
814 }
815
816 static match
817 match_omp_oacc_expr_list (const char *str, gfc_expr_list **list,
818 bool allow_asterisk, bool is_omp)
819 {
820 gfc_expr_list *head, *tail, *p;
821 locus old_loc;
822 gfc_expr *expr;
823 match m;
824
825 head = tail = NULL;
826
827 old_loc = gfc_current_locus;
828
829 m = gfc_match (str);
830 if (m != MATCH_YES)
831 return m;
832
833 for (;;)
834 {
835 m = gfc_match_expr (&expr);
836 if (m == MATCH_YES || allow_asterisk)
837 {
838 p = gfc_get_expr_list ();
839 if (head == NULL)
840 head = tail = p;
841 else
842 {
843 tail->next = p;
844 tail = tail->next;
845 }
846 if (m == MATCH_YES)
847 tail->expr = expr;
848 else if (gfc_match (" *") != MATCH_YES)
849 goto syntax;
850 goto next_item;
851 }
852 if (m == MATCH_ERROR)
853 goto cleanup;
854 goto syntax;
855
856 next_item:
857 if (gfc_match_char (')') == MATCH_YES)
858 break;
859 if (gfc_match_char (',') != MATCH_YES)
860 goto syntax;
861 }
862
863 while (*list)
864 list = &(*list)->next;
865
866 *list = head;
867 return MATCH_YES;
868
869 syntax:
870 if (is_omp)
871 gfc_error ("Syntax error in OpenMP expression list at %C");
872 else
873 gfc_error ("Syntax error in OpenACC expression list at %C");
874
875 cleanup:
876 gfc_free_expr_list (head);
877 gfc_current_locus = old_loc;
878 return MATCH_ERROR;
879 }
880
881 static match
882 match_oacc_clause_gwv (gfc_omp_clauses *cp, unsigned gwv)
883 {
884 match ret = MATCH_YES;
885
886 if (gfc_match (" ( ") != MATCH_YES)
887 return MATCH_NO;
888
889 if (gwv == GOMP_DIM_GANG)
890 {
891 /* The gang clause accepts two optional arguments, num and static.
892 The num argument may either be explicit (num: <val>) or
893 implicit without (<val> without num:). */
894
895 while (ret == MATCH_YES)
896 {
897 if (gfc_match (" static :") == MATCH_YES)
898 {
899 if (cp->gang_static)
900 return MATCH_ERROR;
901 else
902 cp->gang_static = true;
903 if (gfc_match_char ('*') == MATCH_YES)
904 cp->gang_static_expr = NULL;
905 else if (gfc_match (" %e ", &cp->gang_static_expr) != MATCH_YES)
906 return MATCH_ERROR;
907 }
908 else
909 {
910 if (cp->gang_num_expr)
911 return MATCH_ERROR;
912
913 /* The 'num' argument is optional. */
914 gfc_match (" num :");
915
916 if (gfc_match (" %e ", &cp->gang_num_expr) != MATCH_YES)
917 return MATCH_ERROR;
918 }
919
920 ret = gfc_match (" , ");
921 }
922 }
923 else if (gwv == GOMP_DIM_WORKER)
924 {
925 /* The 'num' argument is optional. */
926 gfc_match (" num :");
927
928 if (gfc_match (" %e ", &cp->worker_expr) != MATCH_YES)
929 return MATCH_ERROR;
930 }
931 else if (gwv == GOMP_DIM_VECTOR)
932 {
933 /* The 'length' argument is optional. */
934 gfc_match (" length :");
935
936 if (gfc_match (" %e ", &cp->vector_expr) != MATCH_YES)
937 return MATCH_ERROR;
938 }
939 else
940 gfc_fatal_error ("Unexpected OpenACC parallelism.");
941
942 return gfc_match (" )");
943 }
944
945 static match
946 gfc_match_oacc_clause_link (const char *str, gfc_omp_namelist **list)
947 {
948 gfc_omp_namelist *head = NULL;
949 gfc_omp_namelist *tail, *p;
950 locus old_loc;
951 char n[GFC_MAX_SYMBOL_LEN+1];
952 gfc_symbol *sym;
953 match m;
954 gfc_symtree *st;
955
956 old_loc = gfc_current_locus;
957
958 m = gfc_match (str);
959 if (m != MATCH_YES)
960 return m;
961
962 m = gfc_match (" (");
963
964 for (;;)
965 {
966 m = gfc_match_symbol (&sym, 0);
967 switch (m)
968 {
969 case MATCH_YES:
970 if (sym->attr.in_common)
971 {
972 gfc_error_now ("Variable at %C is an element of a COMMON block");
973 goto cleanup;
974 }
975 gfc_set_sym_referenced (sym);
976 p = gfc_get_omp_namelist ();
977 if (head == NULL)
978 head = tail = p;
979 else
980 {
981 tail->next = p;
982 tail = tail->next;
983 }
984 tail->sym = sym;
985 tail->expr = NULL;
986 tail->where = gfc_current_locus;
987 goto next_item;
988 case MATCH_NO:
989 break;
990
991 case MATCH_ERROR:
992 goto cleanup;
993 }
994
995 m = gfc_match (" / %n /", n);
996 if (m == MATCH_ERROR)
997 goto cleanup;
998 if (m == MATCH_NO || n[0] == '\0')
999 goto syntax;
1000
1001 st = gfc_find_symtree (gfc_current_ns->common_root, n);
1002 if (st == NULL)
1003 {
1004 gfc_error ("COMMON block /%s/ not found at %C", n);
1005 goto cleanup;
1006 }
1007
1008 for (sym = st->n.common->head; sym; sym = sym->common_next)
1009 {
1010 gfc_set_sym_referenced (sym);
1011 p = gfc_get_omp_namelist ();
1012 if (head == NULL)
1013 head = tail = p;
1014 else
1015 {
1016 tail->next = p;
1017 tail = tail->next;
1018 }
1019 tail->sym = sym;
1020 tail->where = gfc_current_locus;
1021 }
1022
1023 next_item:
1024 if (gfc_match_char (')') == MATCH_YES)
1025 break;
1026 if (gfc_match_char (',') != MATCH_YES)
1027 goto syntax;
1028 }
1029
1030 if (gfc_match_omp_eos () != MATCH_YES)
1031 {
1032 gfc_error ("Unexpected junk after !$ACC DECLARE at %C");
1033 goto cleanup;
1034 }
1035
1036 while (*list)
1037 list = &(*list)->next;
1038 *list = head;
1039 return MATCH_YES;
1040
1041 syntax:
1042 gfc_error ("Syntax error in !$ACC DECLARE list at %C");
1043
1044 cleanup:
1045 gfc_current_locus = old_loc;
1046 return MATCH_ERROR;
1047 }
1048
1049 /* OpenMP clauses. */
1050 enum omp_mask1
1051 {
1052 OMP_CLAUSE_PRIVATE,
1053 OMP_CLAUSE_FIRSTPRIVATE,
1054 OMP_CLAUSE_LASTPRIVATE,
1055 OMP_CLAUSE_COPYPRIVATE,
1056 OMP_CLAUSE_SHARED,
1057 OMP_CLAUSE_COPYIN,
1058 OMP_CLAUSE_REDUCTION,
1059 OMP_CLAUSE_IN_REDUCTION,
1060 OMP_CLAUSE_TASK_REDUCTION,
1061 OMP_CLAUSE_IF,
1062 OMP_CLAUSE_NUM_THREADS,
1063 OMP_CLAUSE_SCHEDULE,
1064 OMP_CLAUSE_DEFAULT,
1065 OMP_CLAUSE_ORDER,
1066 OMP_CLAUSE_ORDERED,
1067 OMP_CLAUSE_COLLAPSE,
1068 OMP_CLAUSE_UNTIED,
1069 OMP_CLAUSE_FINAL,
1070 OMP_CLAUSE_MERGEABLE,
1071 OMP_CLAUSE_ALIGNED,
1072 OMP_CLAUSE_DEPEND,
1073 OMP_CLAUSE_INBRANCH,
1074 OMP_CLAUSE_LINEAR,
1075 OMP_CLAUSE_NOTINBRANCH,
1076 OMP_CLAUSE_PROC_BIND,
1077 OMP_CLAUSE_SAFELEN,
1078 OMP_CLAUSE_SIMDLEN,
1079 OMP_CLAUSE_UNIFORM,
1080 OMP_CLAUSE_DEVICE,
1081 OMP_CLAUSE_MAP,
1082 OMP_CLAUSE_TO,
1083 OMP_CLAUSE_FROM,
1084 OMP_CLAUSE_NUM_TEAMS,
1085 OMP_CLAUSE_THREAD_LIMIT,
1086 OMP_CLAUSE_DIST_SCHEDULE,
1087 OMP_CLAUSE_DEFAULTMAP,
1088 OMP_CLAUSE_GRAINSIZE,
1089 OMP_CLAUSE_HINT,
1090 OMP_CLAUSE_IS_DEVICE_PTR,
1091 OMP_CLAUSE_LINK,
1092 OMP_CLAUSE_NOGROUP,
1093 OMP_CLAUSE_NOTEMPORAL,
1094 OMP_CLAUSE_NUM_TASKS,
1095 OMP_CLAUSE_PRIORITY,
1096 OMP_CLAUSE_SIMD,
1097 OMP_CLAUSE_THREADS,
1098 OMP_CLAUSE_USE_DEVICE_PTR,
1099 OMP_CLAUSE_USE_DEVICE_ADDR, /* OpenMP 5.0. */
1100 OMP_CLAUSE_DEVICE_TYPE, /* OpenMP 5.0. */
1101 OMP_CLAUSE_ATOMIC, /* OpenMP 5.0. */
1102 OMP_CLAUSE_CAPTURE, /* OpenMP 5.0. */
1103 OMP_CLAUSE_MEMORDER, /* OpenMP 5.0. */
1104 OMP_CLAUSE_DETACH, /* OpenMP 5.0. */
1105 OMP_CLAUSE_AFFINITY, /* OpenMP 5.0. */
1106 OMP_CLAUSE_ALLOCATE, /* OpenMP 5.0. */
1107 OMP_CLAUSE_BIND, /* OpenMP 5.0. */
1108 OMP_CLAUSE_FILTER, /* OpenMP 5.1. */
1109 OMP_CLAUSE_AT, /* OpenMP 5.1. */
1110 OMP_CLAUSE_MESSAGE, /* OpenMP 5.1. */
1111 OMP_CLAUSE_SEVERITY, /* OpenMP 5.1. */
1112 OMP_CLAUSE_COMPARE, /* OpenMP 5.1. */
1113 OMP_CLAUSE_FAIL, /* OpenMP 5.1. */
1114 OMP_CLAUSE_WEAK, /* OpenMP 5.1. */
1115 OMP_CLAUSE_NOWAIT,
1116 /* This must come last. */
1117 OMP_MASK1_LAST
1118 };
1119
1120 /* More OpenMP clauses and OpenACC 2.0+ specific clauses. */
1121 enum omp_mask2
1122 {
1123 OMP_CLAUSE_ASYNC,
1124 OMP_CLAUSE_NUM_GANGS,
1125 OMP_CLAUSE_NUM_WORKERS,
1126 OMP_CLAUSE_VECTOR_LENGTH,
1127 OMP_CLAUSE_COPY,
1128 OMP_CLAUSE_COPYOUT,
1129 OMP_CLAUSE_CREATE,
1130 OMP_CLAUSE_NO_CREATE,
1131 OMP_CLAUSE_PRESENT,
1132 OMP_CLAUSE_DEVICEPTR,
1133 OMP_CLAUSE_GANG,
1134 OMP_CLAUSE_WORKER,
1135 OMP_CLAUSE_VECTOR,
1136 OMP_CLAUSE_SEQ,
1137 OMP_CLAUSE_INDEPENDENT,
1138 OMP_CLAUSE_USE_DEVICE,
1139 OMP_CLAUSE_DEVICE_RESIDENT,
1140 OMP_CLAUSE_SELF,
1141 OMP_CLAUSE_HOST,
1142 OMP_CLAUSE_WAIT,
1143 OMP_CLAUSE_DELETE,
1144 OMP_CLAUSE_AUTO,
1145 OMP_CLAUSE_TILE,
1146 OMP_CLAUSE_IF_PRESENT,
1147 OMP_CLAUSE_FINALIZE,
1148 OMP_CLAUSE_ATTACH,
1149 OMP_CLAUSE_NOHOST,
1150 OMP_CLAUSE_HAS_DEVICE_ADDR, /* OpenMP 5.1 */
1151 OMP_CLAUSE_ENTER, /* OpenMP 5.2 */
1152 OMP_CLAUSE_DOACROSS, /* OpenMP 5.2 */
1153 OMP_CLAUSE_ASSUMPTIONS, /* OpenMP 5.1. */
1154 OMP_CLAUSE_USES_ALLOCATORS, /* OpenMP 5.0 */
1155 OMP_CLAUSE_INDIRECT, /* OpenMP 5.1 */
1156 OMP_CLAUSE_FULL, /* OpenMP 5.1. */
1157 OMP_CLAUSE_PARTIAL, /* OpenMP 5.1. */
1158 OMP_CLAUSE_SIZES, /* OpenMP 5.1. */
1159 OMP_CLAUSE_INIT, /* OpenMP 5.1. */
1160 OMP_CLAUSE_DESTROY, /* OpenMP 5.1. */
1161 OMP_CLAUSE_USE, /* OpenMP 5.1. */
1162 OMP_CLAUSE_NOVARIANTS, /* OpenMP 5.1 */
1163 OMP_CLAUSE_NOCONTEXT, /* OpenMP 5.1 */
1164 OMP_CLAUSE_INTEROP, /* OpenMP 5.1 */
1165 /* This must come last. */
1166 OMP_MASK2_LAST
1167 };
1168
1169 struct omp_inv_mask;
1170
1171 /* Customized bitset for up to 128-bits.
1172 The two enums above provide bit numbers to use, and which of the
1173 two enums it is determines which of the two mask fields is used.
1174 Supported operations are defining a mask, like:
1175 #define XXX_CLAUSES \
1176 (omp_mask (OMP_CLAUSE_XXX) | OMP_CLAUSE_YYY | OMP_CLAUSE_ZZZ)
1177 oring such bitsets together or removing selected bits:
1178 (XXX_CLAUSES | YYY_CLAUSES) & ~(omp_mask (OMP_CLAUSE_VVV))
1179 and testing individual bits:
1180 if (mask & OMP_CLAUSE_UUU) */
1181
1182 struct omp_mask {
1183 const uint64_t mask1;
1184 const uint64_t mask2;
1185 inline omp_mask ();
1186 inline omp_mask (omp_mask1);
1187 inline omp_mask (omp_mask2);
1188 inline omp_mask (uint64_t, uint64_t);
1189 inline omp_mask operator| (omp_mask1) const;
1190 inline omp_mask operator| (omp_mask2) const;
1191 inline omp_mask operator| (omp_mask) const;
1192 inline omp_mask operator& (const omp_inv_mask &) const;
1193 inline bool operator& (omp_mask1) const;
1194 inline bool operator& (omp_mask2) const;
1195 inline omp_inv_mask operator~ () const;
1196 };
1197
1198 struct omp_inv_mask : public omp_mask {
1199 inline omp_inv_mask (const omp_mask &);
1200 };
1201
1202 omp_mask::omp_mask () : mask1 (0), mask2 (0)
1203 {
1204 }
1205
1206 omp_mask::omp_mask (omp_mask1 m) : mask1 (((uint64_t) 1) << m), mask2 (0)
1207 {
1208 }
1209
1210 omp_mask::omp_mask (omp_mask2 m) : mask1 (0), mask2 (((uint64_t) 1) << m)
1211 {
1212 }
1213
1214 omp_mask::omp_mask (uint64_t m1, uint64_t m2) : mask1 (m1), mask2 (m2)
1215 {
1216 }
1217
1218 omp_mask
1219 omp_mask::operator| (omp_mask1 m) const
1220 {
1221 return omp_mask (mask1 | (((uint64_t) 1) << m), mask2);
1222 }
1223
1224 omp_mask
1225 omp_mask::operator| (omp_mask2 m) const
1226 {
1227 return omp_mask (mask1, mask2 | (((uint64_t) 1) << m));
1228 }
1229
1230 omp_mask
1231 omp_mask::operator| (omp_mask m) const
1232 {
1233 return omp_mask (mask1 | m.mask1, mask2 | m.mask2);
1234 }
1235
1236 omp_mask
1237 omp_mask::operator& (const omp_inv_mask &m) const
1238 {
1239 return omp_mask (mask1 & ~m.mask1, mask2 & ~m.mask2);
1240 }
1241
1242 bool
1243 omp_mask::operator& (omp_mask1 m) const
1244 {
1245 return (mask1 & (((uint64_t) 1) << m)) != 0;
1246 }
1247
1248 bool
1249 omp_mask::operator& (omp_mask2 m) const
1250 {
1251 return (mask2 & (((uint64_t) 1) << m)) != 0;
1252 }
1253
1254 omp_inv_mask
1255 omp_mask::operator~ () const
1256 {
1257 return omp_inv_mask (*this);
1258 }
1259
1260 omp_inv_mask::omp_inv_mask (const omp_mask &m) : omp_mask (m)
1261 {
1262 }
1263
1264 /* Helper function for OpenACC and OpenMP clauses involving memory
1265 mapping. */
1266
1267 static bool
1268 gfc_match_omp_map_clause (gfc_omp_namelist **list, gfc_omp_map_op map_op,
1269 bool allow_common, bool allow_derived)
1270 {
1271 gfc_omp_namelist **head = NULL;
1272 if (gfc_match_omp_variable_list ("", list, allow_common, NULL, &head, true,
1273 allow_derived)
1274 == MATCH_YES)
1275 {
1276 gfc_omp_namelist *n;
1277 for (n = *head; n; n = n->next)
1278 n->u.map.op = map_op;
1279 return true;
1280 }
1281
1282 return false;
1283 }
1284
1285 static match
1286 gfc_match_iterator (gfc_namespace **ns, bool permit_var)
1287 {
1288 locus old_loc = gfc_current_locus;
1289
1290 if (gfc_match ("iterator ( ") != MATCH_YES)
1291 return MATCH_NO;
1292
1293 gfc_typespec ts;
1294 gfc_symbol *last = NULL;
1295 gfc_expr *begin, *end, *step;
1296 *ns = gfc_build_block_ns (gfc_current_ns);
1297 char name[GFC_MAX_SYMBOL_LEN + 1];
1298 while (true)
1299 {
1300 locus prev_loc = gfc_current_locus;
1301 if (gfc_match_type_spec (&ts) == MATCH_YES
1302 && gfc_match (" :: ") == MATCH_YES)
1303 {
1304 if (ts.type != BT_INTEGER)
1305 {
1306 gfc_error ("Expected INTEGER type at %L", &prev_loc);
1307 return MATCH_ERROR;
1308 }
1309 permit_var = false;
1310 }
1311 else
1312 {
1313 ts.type = BT_INTEGER;
1314 ts.kind = gfc_default_integer_kind;
1315 gfc_current_locus = prev_loc;
1316 }
1317 prev_loc = gfc_current_locus;
1318 if (gfc_match_name (name) != MATCH_YES)
1319 {
1320 gfc_error ("Expected identifier at %C");
1321 goto failed;
1322 }
1323 if (gfc_find_symtree ((*ns)->sym_root, name))
1324 {
1325 gfc_error ("Same identifier %qs specified again at %C", name);
1326 goto failed;
1327 }
1328
1329 gfc_symbol *sym = gfc_new_symbol (name, *ns);
1330 if (last)
1331 last->tlink = sym;
1332 else
1333 (*ns)->omp_affinity_iterators = sym;
1334 last = sym;
1335 sym->declared_at = prev_loc;
1336 sym->ts = ts;
1337 sym->attr.flavor = FL_VARIABLE;
1338 sym->attr.artificial = 1;
1339 sym->attr.referenced = 1;
1340 sym->refs++;
1341 gfc_symtree *st = gfc_new_symtree (&(*ns)->sym_root, name);
1342 st->n.sym = sym;
1343
1344 prev_loc = gfc_current_locus;
1345 if (gfc_match (" = ") != MATCH_YES)
1346 goto failed;
1347 permit_var = false;
1348 begin = end = step = NULL;
1349 if (gfc_match ("%e : ", &begin) != MATCH_YES
1350 || gfc_match ("%e ", &end) != MATCH_YES)
1351 {
1352 gfc_error ("Expected range-specification at %C");
1353 gfc_free_expr (begin);
1354 gfc_free_expr (end);
1355 return MATCH_ERROR;
1356 }
1357 if (':' == gfc_peek_ascii_char ())
1358 {
1359 if (gfc_match (": %e ", &step) != MATCH_YES)
1360 {
1361 gfc_free_expr (begin);
1362 gfc_free_expr (end);
1363 gfc_free_expr (step);
1364 goto failed;
1365 }
1366 }
1367
1368 gfc_expr *e = gfc_get_expr ();
1369 e->where = prev_loc;
1370 e->expr_type = EXPR_ARRAY;
1371 e->ts = ts;
1372 e->rank = 1;
1373 e->shape = gfc_get_shape (1);
1374 mpz_init_set_ui (e->shape[0], step ? 3 : 2);
1375 gfc_constructor_append_expr (&e->value.constructor, begin, &begin->where);
1376 gfc_constructor_append_expr (&e->value.constructor, end, &end->where);
1377 if (step)
1378 gfc_constructor_append_expr (&e->value.constructor, step, &step->where);
1379 sym->value = e;
1380
1381 if (gfc_match (") ") == MATCH_YES)
1382 break;
1383 if (gfc_match (", ") != MATCH_YES)
1384 goto failed;
1385 }
1386 return MATCH_YES;
1387
1388 failed:
1389 gfc_namespace *prev_ns = NULL;
1390 for (gfc_namespace *it = gfc_current_ns->contained; it; it = it->sibling)
1391 {
1392 if (it == *ns)
1393 {
1394 if (prev_ns)
1395 prev_ns->sibling = it->sibling;
1396 else
1397 gfc_current_ns->contained = it->sibling;
1398 gfc_free_namespace (it);
1399 break;
1400 }
1401 prev_ns = it;
1402 }
1403 *ns = NULL;
1404 if (!permit_var)
1405 return MATCH_ERROR;
1406 gfc_current_locus = old_loc;
1407 return MATCH_NO;
1408 }
1409
1410 /* Match target update's to/from( [present:] var-list). */
1411
1412 static match
1413 gfc_match_motion_var_list (const char *str, gfc_omp_namelist **list,
1414 gfc_omp_namelist ***headp)
1415 {
1416 match m = gfc_match (str);
1417 if (m != MATCH_YES)
1418 return m;
1419
1420 match m_present = gfc_match (" present : ");
1421
1422 m = gfc_match_omp_variable_list ("", list, false, NULL, headp, true, true);
1423 if (m != MATCH_YES)
1424 return m;
1425 if (m_present == MATCH_YES)
1426 {
1427 gfc_omp_namelist *n;
1428 for (n = **headp; n; n = n->next)
1429 n->u.present_modifier = true;
1430 }
1431 return MATCH_YES;
1432 }
1433
1434 /* reduction ( reduction-modifier, reduction-operator : variable-list )
1435 in_reduction ( reduction-operator : variable-list )
1436 task_reduction ( reduction-operator : variable-list ) */
1437
1438 static match
1439 gfc_match_omp_clause_reduction (char pc, gfc_omp_clauses *c, bool openacc,
1440 bool allow_derived, bool openmp_target = false)
1441 {
1442 if (pc == 'r' && gfc_match ("reduction ( ") != MATCH_YES)
1443 return MATCH_NO;
1444 else if (pc == 'i' && gfc_match ("in_reduction ( ") != MATCH_YES)
1445 return MATCH_NO;
1446 else if (pc == 't' && gfc_match ("task_reduction ( ") != MATCH_YES)
1447 return MATCH_NO;
1448
1449 locus old_loc = gfc_current_locus;
1450 int list_idx = 0;
1451
1452 if (pc == 'r' && !openacc)
1453 {
1454 if (gfc_match ("inscan") == MATCH_YES)
1455 list_idx = OMP_LIST_REDUCTION_INSCAN;
1456 else if (gfc_match ("task") == MATCH_YES)
1457 list_idx = OMP_LIST_REDUCTION_TASK;
1458 else if (gfc_match ("default") == MATCH_YES)
1459 list_idx = OMP_LIST_REDUCTION;
1460 if (list_idx != 0 && gfc_match (", ") != MATCH_YES)
1461 {
1462 gfc_error ("Comma expected at %C");
1463 gfc_current_locus = old_loc;
1464 return MATCH_NO;
1465 }
1466 if (list_idx == 0)
1467 list_idx = OMP_LIST_REDUCTION;
1468 }
1469 else if (pc == 'i')
1470 list_idx = OMP_LIST_IN_REDUCTION;
1471 else if (pc == 't')
1472 list_idx = OMP_LIST_TASK_REDUCTION;
1473 else
1474 list_idx = OMP_LIST_REDUCTION;
1475
1476 gfc_omp_reduction_op rop = OMP_REDUCTION_NONE;
1477 char buffer[GFC_MAX_SYMBOL_LEN + 3];
1478 if (gfc_match_char ('+') == MATCH_YES)
1479 rop = OMP_REDUCTION_PLUS;
1480 else if (gfc_match_char ('*') == MATCH_YES)
1481 rop = OMP_REDUCTION_TIMES;
1482 else if (gfc_match_char ('-') == MATCH_YES)
1483 rop = OMP_REDUCTION_MINUS;
1484 else if (gfc_match (".and.") == MATCH_YES)
1485 rop = OMP_REDUCTION_AND;
1486 else if (gfc_match (".or.") == MATCH_YES)
1487 rop = OMP_REDUCTION_OR;
1488 else if (gfc_match (".eqv.") == MATCH_YES)
1489 rop = OMP_REDUCTION_EQV;
1490 else if (gfc_match (".neqv.") == MATCH_YES)
1491 rop = OMP_REDUCTION_NEQV;
1492 if (rop != OMP_REDUCTION_NONE)
1493 snprintf (buffer, sizeof buffer, "operator %s",
1494 gfc_op2string ((gfc_intrinsic_op) rop));
1495 else if (gfc_match_defined_op_name (buffer + 1, 1) == MATCH_YES)
1496 {
1497 buffer[0] = '.';
1498 strcat (buffer, ".");
1499 }
1500 else if (gfc_match_name (buffer) == MATCH_YES)
1501 {
1502 gfc_symbol *sym;
1503 const char *n = buffer;
1504
1505 gfc_find_symbol (buffer, NULL, 1, &sym);
1506 if (sym != NULL)
1507 {
1508 if (sym->attr.intrinsic)
1509 n = sym->name;
1510 else if ((sym->attr.flavor != FL_UNKNOWN
1511 && sym->attr.flavor != FL_PROCEDURE)
1512 || sym->attr.external
1513 || sym->attr.generic
1514 || sym->attr.entry
1515 || sym->attr.result
1516 || sym->attr.dummy
1517 || sym->attr.subroutine
1518 || sym->attr.pointer
1519 || sym->attr.target
1520 || sym->attr.cray_pointer
1521 || sym->attr.cray_pointee
1522 || (sym->attr.proc != PROC_UNKNOWN
1523 && sym->attr.proc != PROC_INTRINSIC)
1524 || sym->attr.if_source != IFSRC_UNKNOWN
1525 || sym == sym->ns->proc_name)
1526 {
1527 sym = NULL;
1528 n = NULL;
1529 }
1530 else
1531 n = sym->name;
1532 }
1533 if (n == NULL)
1534 rop = OMP_REDUCTION_NONE;
1535 else if (strcmp (n, "max") == 0)
1536 rop = OMP_REDUCTION_MAX;
1537 else if (strcmp (n, "min") == 0)
1538 rop = OMP_REDUCTION_MIN;
1539 else if (strcmp (n, "iand") == 0)
1540 rop = OMP_REDUCTION_IAND;
1541 else if (strcmp (n, "ior") == 0)
1542 rop = OMP_REDUCTION_IOR;
1543 else if (strcmp (n, "ieor") == 0)
1544 rop = OMP_REDUCTION_IEOR;
1545 if (rop != OMP_REDUCTION_NONE
1546 && sym != NULL
1547 && ! sym->attr.intrinsic
1548 && ! sym->attr.use_assoc
1549 && ((sym->attr.flavor == FL_UNKNOWN
1550 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE,
1551 sym->name, NULL))
1552 || !gfc_add_intrinsic (&sym->attr, NULL)))
1553 rop = OMP_REDUCTION_NONE;
1554 }
1555 else
1556 buffer[0] = '\0';
1557 gfc_omp_udr *udr = (buffer[0] ? gfc_find_omp_udr (gfc_current_ns, buffer, NULL)
1558 : NULL);
1559 gfc_omp_namelist **head = NULL;
1560 if (rop == OMP_REDUCTION_NONE && udr)
1561 rop = OMP_REDUCTION_USER;
1562
1563 if (gfc_match_omp_variable_list (" :", &c->lists[list_idx], false, NULL,
1564 &head, openacc, allow_derived) != MATCH_YES)
1565 {
1566 gfc_current_locus = old_loc;
1567 return MATCH_NO;
1568 }
1569 gfc_omp_namelist *n;
1570 if (rop == OMP_REDUCTION_NONE)
1571 {
1572 n = *head;
1573 *head = NULL;
1574 gfc_error_now ("!$OMP DECLARE REDUCTION %s not found at %L",
1575 buffer, &old_loc);
1576 gfc_free_omp_namelist (n, false, false, false, false);
1577 }
1578 else
1579 for (n = *head; n; n = n->next)
1580 {
1581 n->u.reduction_op = rop;
1582 if (udr)
1583 {
1584 n->u2.udr = gfc_get_omp_namelist_udr ();
1585 n->u2.udr->udr = udr;
1586 }
1587 if (openmp_target && list_idx == OMP_LIST_IN_REDUCTION)
1588 {
1589 gfc_omp_namelist *p = gfc_get_omp_namelist (), **tl;
1590 p->sym = n->sym;
1591 p->where = n->where;
1592 p->u.map.op = OMP_MAP_ALWAYS_TOFROM;
1593
1594 tl = &c->lists[OMP_LIST_MAP];
1595 while (*tl)
1596 tl = &((*tl)->next);
1597 *tl = p;
1598 p->next = NULL;
1599 }
1600 }
1601 return MATCH_YES;
1602 }
1603
1604 static match
1605 gfc_omp_absent_contains_clause (gfc_omp_assumptions **assume, bool is_absent)
1606 {
1607 if (*assume == NULL)
1608 *assume = gfc_get_omp_assumptions ();
1609 do
1610 {
1611 gfc_statement st = ST_NONE;
1612 gfc_gobble_whitespace ();
1613 locus old_loc = gfc_current_locus;
1614 char c = gfc_peek_ascii_char ();
1615 enum gfc_omp_directive_kind kind
1616 = GFC_OMP_DIR_DECLARATIVE; /* Silence warning. */
1617 for (size_t i = 0; i < ARRAY_SIZE (gfc_omp_directives); i++)
1618 {
1619 if (gfc_omp_directives[i].name[0] > c)
1620 break;
1621 if (gfc_omp_directives[i].name[0] != c)
1622 continue;
1623 if (gfc_match (gfc_omp_directives[i].name) == MATCH_YES)
1624 {
1625 st = gfc_omp_directives[i].st;
1626 kind = gfc_omp_directives[i].kind;
1627 }
1628 }
1629 gfc_gobble_whitespace ();
1630 c = gfc_peek_ascii_char ();
1631 if (st == ST_NONE || (c != ',' && c != ')'))
1632 {
1633 if (st == ST_NONE)
1634 gfc_error ("Unknown directive at %L", &old_loc);
1635 else
1636 gfc_error ("Invalid combined or composite directive at %L",
1637 &old_loc);
1638 return MATCH_ERROR;
1639 }
1640 if (kind == GFC_OMP_DIR_DECLARATIVE
1641 || kind == GFC_OMP_DIR_INFORMATIONAL
1642 || kind == GFC_OMP_DIR_META)
1643 {
1644 gfc_error ("Invalid %qs directive at %L in %s clause: declarative, "
1645 "informational, and meta directives not permitted",
1646 gfc_ascii_statement (st, true), &old_loc,
1647 is_absent ? "ABSENT" : "CONTAINS");
1648 return MATCH_ERROR;
1649 }
1650 if (is_absent)
1651 {
1652 /* Use exponential allocation; equivalent to pow2p(x). */
1653 int i = (*assume)->n_absent;
1654 int size = ((i == 0) ? 4
1655 : pow2p_hwi (i) == 1 ? i*2 : 0);
1656 if (size != 0)
1657 (*assume)->absent = XRESIZEVEC (gfc_statement,
1658 (*assume)->absent, size);
1659 (*assume)->absent[(*assume)->n_absent++] = st;
1660 }
1661 else
1662 {
1663 int i = (*assume)->n_contains;
1664 int size = ((i == 0) ? 4
1665 : pow2p_hwi (i) == 1 ? i*2 : 0);
1666 if (size != 0)
1667 (*assume)->contains = XRESIZEVEC (gfc_statement,
1668 (*assume)->contains, size);
1669 (*assume)->contains[(*assume)->n_contains++] = st;
1670 }
1671 gfc_gobble_whitespace ();
1672 if (gfc_match(",") == MATCH_YES)
1673 continue;
1674 if (gfc_match(")") == MATCH_YES)
1675 break;
1676 gfc_error ("Expected %<,%> or %<)%> at %C");
1677 return MATCH_ERROR;
1678 }
1679 while (true);
1680
1681 return MATCH_YES;
1682 }
1683
1684 /* Check 'check' argument for duplicated statements in absent and/or contains
1685 clauses. If 'merge', merge them from check to 'merge'. */
1686
1687 static match
1688 omp_verify_merge_absent_contains (gfc_statement st, gfc_omp_assumptions *check,
1689 gfc_omp_assumptions *merge, locus *loc)
1690 {
1691 if (check == NULL)
1692 return MATCH_YES;
1693 bitmap_head absent_head, contains_head;
1694 bitmap_obstack_initialize (NULL);
1695 bitmap_initialize (&absent_head, &bitmap_default_obstack);
1696 bitmap_initialize (&contains_head, &bitmap_default_obstack);
1697
1698 match m = MATCH_YES;
1699 for (int i = 0; i < check->n_absent; i++)
1700 if (!bitmap_set_bit (&absent_head, check->absent[i]))
1701 {
1702 gfc_error ("%qs directive mentioned multiple times in %s clause in %s "
1703 "directive at %L",
1704 gfc_ascii_statement (check->absent[i], true),
1705 "ABSENT", gfc_ascii_statement (st), loc);
1706 m = MATCH_ERROR;
1707 }
1708 for (int i = 0; i < check->n_contains; i++)
1709 {
1710 if (!bitmap_set_bit (&contains_head, check->contains[i]))
1711 {
1712 gfc_error ("%qs directive mentioned multiple times in %s clause in %s "
1713 "directive at %L",
1714 gfc_ascii_statement (check->contains[i], true),
1715 "CONTAINS", gfc_ascii_statement (st), loc);
1716 m = MATCH_ERROR;
1717 }
1718 if (bitmap_bit_p (&absent_head, check->contains[i]))
1719 {
1720 gfc_error ("%qs directive mentioned both times in ABSENT and CONTAINS "
1721 "clauses in %s directive at %L",
1722 gfc_ascii_statement (check->absent[i], true),
1723 gfc_ascii_statement (st), loc);
1724 m = MATCH_ERROR;
1725 }
1726 }
1727
1728 if (m == MATCH_ERROR)
1729 return MATCH_ERROR;
1730 if (merge == NULL)
1731 return MATCH_YES;
1732 if (merge->absent == NULL && check->absent)
1733 {
1734 merge->n_absent = check->n_absent;
1735 merge->absent = check->absent;
1736 check->absent = NULL;
1737 }
1738 else if (merge->absent && check->absent)
1739 {
1740 check->absent = XRESIZEVEC (gfc_statement, check->absent,
1741 merge->n_absent + check->n_absent);
1742 for (int i = 0; i < merge->n_absent; i++)
1743 if (!bitmap_bit_p (&absent_head, merge->absent[i]))
1744 check->absent[check->n_absent++] = merge->absent[i];
1745 free (merge->absent);
1746 merge->absent = check->absent;
1747 merge->n_absent = check->n_absent;
1748 check->absent = NULL;
1749 }
1750 if (merge->contains == NULL && check->contains)
1751 {
1752 merge->n_contains = check->n_contains;
1753 merge->contains = check->contains;
1754 check->contains = NULL;
1755 }
1756 else if (merge->contains && check->contains)
1757 {
1758 check->contains = XRESIZEVEC (gfc_statement, check->contains,
1759 merge->n_contains + check->n_contains);
1760 for (int i = 0; i < merge->n_contains; i++)
1761 if (!bitmap_bit_p (&contains_head, merge->contains[i]))
1762 check->contains[check->n_contains++] = merge->contains[i];
1763 free (merge->contains);
1764 merge->contains = check->contains;
1765 merge->n_contains = check->n_contains;
1766 check->contains = NULL;
1767 }
1768 return MATCH_YES;
1769 }
1770
1771 /* OpenMP 5.0
1772 uses_allocators ( allocator-list )
1773
1774 allocator:
1775 predefined-allocator
1776 variable ( traits-array )
1777
1778 OpenMP 5.2:
1779 uses_allocators ( [modifier-list :] allocator-list )
1780
1781 allocator:
1782 variable or predefined-allocator
1783 modifier:
1784 traits ( traits-array )
1785 memspace ( mem-space-handle ) */
1786
1787 static match
1788 gfc_match_omp_clause_uses_allocators (gfc_omp_clauses *c)
1789 {
1790 gfc_symbol *memspace_sym = NULL;
1791 gfc_symbol *traits_sym = NULL;
1792 gfc_omp_namelist *head = NULL;
1793 gfc_omp_namelist *p, *tail, **list;
1794 int ntraits, nmemspace;
1795 bool has_modifiers;
1796 locus old_loc, cur_loc;
1797
1798 gfc_gobble_whitespace ();
1799 old_loc = gfc_current_locus;
1800 ntraits = nmemspace = 0;
1801 do
1802 {
1803 cur_loc = gfc_current_locus;
1804 if (gfc_match ("traits ( %S ) ", &traits_sym) == MATCH_YES)
1805 ntraits++;
1806 else if (gfc_match ("memspace ( %S ) ", &memspace_sym) == MATCH_YES)
1807 nmemspace++;
1808 if (ntraits > 1 || nmemspace > 1)
1809 {
1810 gfc_error ("Duplicate %s modifier at %L in USES_ALLOCATORS clause",
1811 ntraits > 1 ? "TRAITS" : "MEMSPACE", &cur_loc);
1812 return MATCH_ERROR;
1813 }
1814 if (gfc_match (", ") == MATCH_YES)
1815 continue;
1816 if (gfc_match (": ") != MATCH_YES)
1817 {
1818 /* Assume no modifier. */
1819 memspace_sym = traits_sym = NULL;
1820 gfc_current_locus = old_loc;
1821 break;
1822 }
1823 break;
1824 } while (true);
1825
1826 has_modifiers = traits_sym != NULL || memspace_sym != NULL;
1827 do
1828 {
1829 p = gfc_get_omp_namelist ();
1830 p->where = gfc_current_locus;
1831 if (head == NULL)
1832 head = tail = p;
1833 else
1834 {
1835 tail->next = p;
1836 tail = tail->next;
1837 }
1838 if (gfc_match ("%S ", &p->sym) != MATCH_YES)
1839 goto error;
1840 if (!has_modifiers)
1841 gfc_match ("( %S ) ", &p->u2.traits_sym);
1842 else if (gfc_peek_ascii_char () == '(')
1843 {
1844 gfc_error ("Unexpected %<(%> at %C");
1845 goto error;
1846 }
1847 else
1848 {
1849 p->u.memspace_sym = memspace_sym;
1850 p->u2.traits_sym = traits_sym;
1851 }
1852 if (gfc_match (", ") == MATCH_YES)
1853 continue;
1854 if (gfc_match (") ") == MATCH_YES)
1855 break;
1856 goto error;
1857 } while (true);
1858
1859 list = &c->lists[OMP_LIST_USES_ALLOCATORS];
1860 while (*list)
1861 list = &(*list)->next;
1862 *list = head;
1863
1864 return MATCH_YES;
1865
1866 error:
1867 gfc_free_omp_namelist (head, false, false, true, false);
1868 return MATCH_ERROR;
1869 }
1870
1871
1872 /* Match the 'prefer_type' modifier of the interop 'init' clause:
1873 with either OpenMP 5.1's
1874 prefer_type ( <const-int-expr|string literal> [, ...]
1875 or
1876 prefer_type ( '{' <fr(...) | attr (...)>, ...] '}' [, '{' ... '}' ] )
1877 where 'fr' takes a constant expression or a string literal
1878 and 'attr takes a list of string literals, starting with 'ompx_')
1879
1880 For the foreign runtime identifiers, string values are converted to
1881 their integer value; unknown string or integer values are set to
1882 GOMP_INTEROP_IFR_KNOWN.
1883
1884 Data format:
1885 For the foreign runtime identifiers, string values are converted to
1886 their integer value; unknown string or integer values are set to 0.
1887
1888 Each item (a) GOMP_INTEROP_IFR_SEPARATOR
1889 (b) for any 'fr', its integer value.
1890 Note: Spec only permits 1 'fr' entry (6.0; changed after TR13)
1891 (c) GOMP_INTEROP_IFR_SEPARATOR
1892 (d) list of \0-terminated non-empty strings for 'attr'
1893 (e) '\0'
1894 Tailing '\0'. */
1895
1896 static match
1897 gfc_match_omp_prefer_type (char **type_str, int *type_str_len)
1898 {
1899 gfc_expr *e;
1900 std::string type_string, attr_string;
1901 /* New syntax. */
1902 if (gfc_peek_ascii_char () == '{')
1903 do
1904 {
1905 attr_string.clear ();
1906 type_string += (char) GOMP_INTEROP_IFR_SEPARATOR;
1907 if (gfc_match ("{ ") != MATCH_YES)
1908 {
1909 gfc_error ("Expected %<{%> at %C");
1910 return MATCH_ERROR;
1911 }
1912 bool fr_found = false;
1913 do
1914 {
1915 if (gfc_match ("fr ( ") == MATCH_YES)
1916 {
1917 if (fr_found)
1918 {
1919 gfc_error ("Duplicated %<fr%> preference-selector-name "
1920 "at %C");
1921 return MATCH_ERROR;
1922 }
1923 fr_found = true;
1924 do
1925 {
1926 bool found_literal = false;
1927 match m = MATCH_YES;
1928 if (gfc_match_literal_constant (&e, false) == MATCH_YES)
1929 found_literal = true;
1930 else
1931 m = gfc_match_expr (&e);
1932 if (m != MATCH_YES
1933 || !gfc_resolve_expr (e)
1934 || e->rank != 0
1935 || e->expr_type != EXPR_CONSTANT
1936 || (e->ts.type != BT_INTEGER
1937 && (!found_literal || e->ts.type != BT_CHARACTER))
1938 || (e->ts.type == BT_INTEGER
1939 && !mpz_fits_sint_p (e->value.integer))
1940 || (e->ts.type == BT_CHARACTER
1941 && (e->ts.kind != gfc_default_character_kind
1942 || e->value.character.length == 0)))
1943 {
1944 gfc_error ("Expected constant scalar integer expression"
1945 " or non-empty default-kind character "
1946 "literal at %L", &e->where);
1947 gfc_free_expr (e);
1948 return MATCH_ERROR;
1949 }
1950 gfc_gobble_whitespace ();
1951 int val;
1952 if (e->ts.type == BT_INTEGER)
1953 {
1954 val = mpz_get_si (e->value.integer);
1955 if (val < 1 || val > GOMP_INTEROP_IFR_LAST)
1956 {
1957 gfc_warning_now (OPT_Wopenmp,
1958 "Unknown foreign runtime "
1959 "identifier %qd at %L",
1960 val, &e->where);
1961 val = GOMP_INTEROP_IFR_UNKNOWN;
1962 }
1963 }
1964 else
1965 {
1966 char *str = XALLOCAVEC (char,
1967 e->value.character.length+1);
1968 for (int i = 0; i < e->value.character.length + 1; i++)
1969 str[i] = e->value.character.string[i];
1970 if (memchr (str, '\0', e->value.character.length) != 0)
1971 {
1972 gfc_error ("Unexpected null character in character "
1973 "literal at %L", &e->where);
1974 return MATCH_ERROR;
1975 }
1976 val = omp_get_fr_id_from_name (str);
1977 if (val == GOMP_INTEROP_IFR_UNKNOWN)
1978 gfc_warning_now (OPT_Wopenmp,
1979 "Unknown foreign runtime identifier "
1980 "%qs at %L", str, &e->where);
1981 }
1982
1983 type_string += (char) val;
1984 if (gfc_match (") ") == MATCH_YES)
1985 break;
1986 gfc_error ("Expected %<)%> at %C");
1987 return MATCH_ERROR;
1988 }
1989 while (true);
1990 }
1991 else if (gfc_match ("attr ( ") == MATCH_YES)
1992 {
1993 do
1994 {
1995 if (gfc_match_literal_constant (&e, false) != MATCH_YES
1996 || !gfc_resolve_expr (e)
1997 || e->expr_type != EXPR_CONSTANT
1998 || e->rank != 0
1999 || e->ts.type != BT_CHARACTER
2000 || e->ts.kind != gfc_default_character_kind)
2001 {
2002 gfc_error ("Expected default-kind character literal "
2003 "at %L", &e->where);
2004 gfc_free_expr (e);
2005 return MATCH_ERROR;
2006 }
2007 gfc_gobble_whitespace ();
2008 char *str = XALLOCAVEC (char, e->value.character.length+1);
2009 for (int i = 0; i < e->value.character.length + 1; i++)
2010 str[i] = e->value.character.string[i];
2011 if (!startswith (str, "ompx_"))
2012 {
2013 gfc_error ("Character literal at %L must start with "
2014 "%<ompx_%>", &e->where);
2015 gfc_free_expr (e);
2016 return MATCH_ERROR;
2017 }
2018 if (memchr (str, '\0', e->value.character.length) != 0
2019 || memchr (str, ',', e->value.character.length) != 0)
2020 {
2021 gfc_error ("Unexpected null or %<,%> character in "
2022 "character literal at %L", &e->where);
2023 return MATCH_ERROR;
2024 }
2025 attr_string += str;
2026 attr_string += '\0';
2027 if (gfc_match (", ") == MATCH_YES)
2028 continue;
2029 if (gfc_match (") ") == MATCH_YES)
2030 break;
2031 gfc_error ("Expected %<,%> or %<)%> at %C");
2032 return MATCH_ERROR;
2033 }
2034 while (true);
2035 }
2036 else
2037 {
2038 gfc_error ("Expected %<fr(%> or %<attr(%> at %C");
2039 return MATCH_ERROR;
2040 }
2041 if (gfc_match (", ") == MATCH_YES)
2042 continue;
2043 if (gfc_match ("} ") == MATCH_YES)
2044 break;
2045 gfc_error ("Expected %<,%> or %<}%> at %C");
2046 return MATCH_ERROR;
2047 }
2048 while (true);
2049 type_string += (char) GOMP_INTEROP_IFR_SEPARATOR;
2050 type_string += attr_string;
2051 type_string += '\0';
2052 if (gfc_match (", ") == MATCH_YES)
2053 continue;
2054 if (gfc_match (") ") == MATCH_YES)
2055 break;
2056 gfc_error ("Expected %<,%> or %<)%> at %C");
2057 return MATCH_ERROR;
2058 }
2059 while (true);
2060 else
2061 do
2062 {
2063 type_string += (char) GOMP_INTEROP_IFR_SEPARATOR;
2064 bool found_literal = false;
2065 match m = MATCH_YES;
2066 if (gfc_match_literal_constant (&e, false) == MATCH_YES)
2067 found_literal = true;
2068 else
2069 m = gfc_match_expr (&e);
2070 if (m != MATCH_YES
2071 || !gfc_resolve_expr (e)
2072 || e->rank != 0
2073 || e->expr_type != EXPR_CONSTANT
2074 || (e->ts.type != BT_INTEGER
2075 && (!found_literal || e->ts.type != BT_CHARACTER))
2076 || (e->ts.type == BT_INTEGER
2077 && !mpz_fits_sint_p (e->value.integer))
2078 || (e->ts.type == BT_CHARACTER
2079 && (e->ts.kind != gfc_default_character_kind
2080 || e->value.character.length == 0)))
2081 {
2082 gfc_error ("Expected constant scalar integer expression or "
2083 "non-empty default-kind character literal at %L", &e->where);
2084 gfc_free_expr (e);
2085 return MATCH_ERROR;
2086 }
2087 gfc_gobble_whitespace ();
2088 int val;
2089 if (e->ts.type == BT_INTEGER)
2090 {
2091 val = mpz_get_si (e->value.integer);
2092 if (val < 1 || val > GOMP_INTEROP_IFR_LAST)
2093 {
2094 gfc_warning_now (OPT_Wopenmp,
2095 "Unknown foreign runtime identifier %qd at %L",
2096 val, &e->where);
2097 val = 0;
2098 }
2099 }
2100 else
2101 {
2102 char *str = XALLOCAVEC (char, e->value.character.length+1);
2103 for (int i = 0; i < e->value.character.length + 1; i++)
2104 str[i] = e->value.character.string[i];
2105 if (memchr (str, '\0', e->value.character.length) != 0)
2106 {
2107 gfc_error ("Unexpected null character in character "
2108 "literal at %L", &e->where);
2109 return MATCH_ERROR;
2110 }
2111 val = omp_get_fr_id_from_name (str);
2112 if (val == GOMP_INTEROP_IFR_UNKNOWN)
2113 gfc_warning_now (OPT_Wopenmp,
2114 "Unknown foreign runtime identifier %qs at %L",
2115 str, &e->where);
2116 }
2117 type_string += (char) val;
2118 type_string += (char) GOMP_INTEROP_IFR_SEPARATOR;
2119 type_string += '\0';
2120 gfc_free_expr (e);
2121 if (gfc_match (", ") == MATCH_YES)
2122 continue;
2123 if (gfc_match (") ") == MATCH_YES)
2124 break;
2125 gfc_error ("Expected %<,%> or %<)%> at %C");
2126 return MATCH_ERROR;
2127 }
2128 while (true);
2129 type_string += '\0';
2130 *type_str_len = type_string.length();
2131 *type_str = XNEWVEC (char, type_string.length ());
2132 memcpy (*type_str, type_string.data (), type_string.length ());
2133 return MATCH_YES;
2134 }
2135
2136
2137 /* Match OpenMP 5.1's 'init'-clause modifiers, used by the 'init' clause of
2138 the 'interop' directive and the 'append_args' directive of 'declare variant'.
2139 [prefer_type(...)][,][<target|targetsync>, ...])
2140
2141 If is_init_clause, the modifier parsing ends with a ':'.
2142 If not is_init_clause (i.e. append_args), the parsing ends with ')'. */
2143
2144 static match
2145 gfc_parser_omp_clause_init_modifiers (bool &target, bool &targetsync,
2146 char **type_str, int &type_str_len,
2147 bool is_init_clause)
2148 {
2149 target = false;
2150 targetsync = false;
2151 *type_str = NULL;
2152 type_str_len = 0;
2153 match m;
2154
2155 do
2156 {
2157 if (gfc_match ("prefer_type ( ") == MATCH_YES)
2158 {
2159 if (*type_str)
2160 {
2161 gfc_error ("Duplicate %<prefer_type%> modifier at %C");
2162 return MATCH_ERROR;
2163 }
2164 m = gfc_match_omp_prefer_type (type_str, &type_str_len);
2165 if (m != MATCH_YES)
2166 return m;
2167 if (gfc_match (", ") == MATCH_YES)
2168 continue;
2169 if (is_init_clause)
2170 {
2171 if (gfc_match (": ") == MATCH_YES)
2172 break;
2173 gfc_error ("Expected %<,%> or %<:%> at %C");
2174 }
2175 else
2176 {
2177 if (gfc_match (") ") == MATCH_YES)
2178 break;
2179 gfc_error ("Expected %<,%> or %<)%> at %C");
2180 }
2181 return MATCH_ERROR;
2182 }
2183
2184 if (gfc_match ("prefer_type ") == MATCH_YES)
2185 {
2186 gfc_error ("Expected %<(%> after %<prefer_type%> at %C");
2187 return MATCH_ERROR;
2188 }
2189
2190 if (gfc_match ("targetsync ") == MATCH_YES)
2191 {
2192 if (targetsync)
2193 {
2194 gfc_error ("Duplicate %<targetsync%> at %C");
2195 return MATCH_ERROR;
2196 }
2197 targetsync = true;
2198 if (gfc_match (", ") == MATCH_YES)
2199 continue;
2200 if (!is_init_clause)
2201 {
2202 if (gfc_match (") ") == MATCH_YES)
2203 break;
2204 gfc_error ("Expected %<,%> or %<)%> at %C");
2205 return MATCH_ERROR;
2206 }
2207 if (gfc_match (": ") == MATCH_YES)
2208 break;
2209 gfc_error ("Expected %<,%> or %<:%> at %C");
2210 return MATCH_ERROR;
2211 }
2212 if (gfc_match ("target ") == MATCH_YES)
2213 {
2214 if (target)
2215 {
2216 gfc_error ("Duplicate %<target%> at %C");
2217 return MATCH_ERROR;
2218 }
2219 target = true;
2220 if (gfc_match (", ") == MATCH_YES)
2221 continue;
2222 if (!is_init_clause)
2223 {
2224 if (gfc_match (") ") == MATCH_YES)
2225 break;
2226 gfc_error ("Expected %<,%> or %<)%> at %C");
2227 return MATCH_ERROR;
2228 }
2229 if (gfc_match (": ") == MATCH_YES)
2230 break;
2231 gfc_error ("Expected %<,%> or %<:%> at %C");
2232 return MATCH_ERROR;
2233 }
2234 gfc_error ("Expected %<prefer_type%>, %<target%>, or %<targetsync%> "
2235 "at %C");
2236 return MATCH_ERROR;
2237 }
2238 while (true);
2239
2240 if (!target && !targetsync)
2241 {
2242 gfc_error ("Missing required %<target%> and/or %<targetsync%> "
2243 "modifier at %C");
2244 return MATCH_ERROR;
2245 }
2246 return MATCH_YES;
2247 }
2248
2249 /* Match OpenMP 5.1's 'init' clause for 'interop' objects:
2250 init([prefer_type(...)][,][<target|targetsync>, ...] :] interop-obj-list) */
2251
2252 static match
2253 gfc_match_omp_init (gfc_omp_namelist **list)
2254 {
2255 bool target, targetsync;
2256 char *type_str = NULL;
2257 int type_str_len;
2258 if (gfc_parser_omp_clause_init_modifiers (target, targetsync, &type_str,
2259 type_str_len, true) == MATCH_ERROR)
2260 return MATCH_ERROR;
2261
2262 gfc_omp_namelist **head = NULL;
2263 if (gfc_match_omp_variable_list ("", list, false, NULL, &head) != MATCH_YES)
2264 return MATCH_ERROR;
2265 for (gfc_omp_namelist *n = *head; n; n = n->next)
2266 {
2267 n->u.init.target = target;
2268 n->u.init.targetsync = targetsync;
2269 n->u.init.len = type_str_len;
2270 n->u2.init_interop = type_str;
2271 }
2272 return MATCH_YES;
2273 }
2274
2275
2276 /* Match with duplicate check. Matches 'name'. If expr != NULL, it
2277 then matches '(expr)', otherwise, if open_parens is true,
2278 it matches a ' ( ' after 'name'.
2279 dupl_message requires '%qs %L' - and is used by
2280 gfc_match_dupl_memorder and gfc_match_dupl_atomic. */
2281
2282 static match
2283 gfc_match_dupl_check (bool not_dupl, const char *name, bool open_parens = false,
2284 gfc_expr **expr = NULL, const char *dupl_msg = NULL)
2285 {
2286 match m;
2287 locus old_loc = gfc_current_locus;
2288 if ((m = gfc_match (name)) != MATCH_YES)
2289 return m;
2290 if (!not_dupl)
2291 {
2292 if (dupl_msg)
2293 gfc_error (dupl_msg, name, &old_loc);
2294 else
2295 gfc_error ("Duplicated %qs clause at %L", name, &old_loc);
2296 return MATCH_ERROR;
2297 }
2298 if (open_parens || expr)
2299 {
2300 if (gfc_match (" ( ") != MATCH_YES)
2301 {
2302 gfc_error ("Expected %<(%> after %qs at %C", name);
2303 return MATCH_ERROR;
2304 }
2305 if (expr)
2306 {
2307 if (gfc_match ("%e )", expr) != MATCH_YES)
2308 {
2309 gfc_error ("Invalid expression after %<%s(%> at %C", name);
2310 return MATCH_ERROR;
2311 }
2312 }
2313 }
2314 return MATCH_YES;
2315 }
2316
2317 static match
2318 gfc_match_dupl_memorder (bool not_dupl, const char *name)
2319 {
2320 return gfc_match_dupl_check (not_dupl, name, false, NULL,
2321 "Duplicated memory-order clause: unexpected %s "
2322 "clause at %L");
2323 }
2324
2325 static match
2326 gfc_match_dupl_atomic (bool not_dupl, const char *name)
2327 {
2328 return gfc_match_dupl_check (not_dupl, name, false, NULL,
2329 "Duplicated atomic clause: unexpected %s "
2330 "clause at %L");
2331 }
2332
2333 /* Match OpenMP and OpenACC directive clauses. MASK is a bitmask of
2334 clauses that are allowed for a particular directive. */
2335
2336 static match
2337 gfc_match_omp_clauses (gfc_omp_clauses **cp, const omp_mask mask,
2338 bool first = true, bool needs_space = true,
2339 bool openacc = false, bool openmp_target = false)
2340 {
2341 bool error = false;
2342 gfc_omp_clauses *c = gfc_get_omp_clauses ();
2343 locus old_loc;
2344 /* Determine whether we're dealing with an OpenACC directive that permits
2345 derived type member accesses. This in particular disallows
2346 "!$acc declare" from using such accesses, because it's not clear if/how
2347 that should work. */
2348 bool allow_derived = (openacc
2349 && ((mask & OMP_CLAUSE_ATTACH)
2350 || (mask & OMP_CLAUSE_DETACH)));
2351
2352 gcc_checking_assert (OMP_MASK1_LAST <= 64 && OMP_MASK2_LAST <= 64);
2353 *cp = NULL;
2354 while (1)
2355 {
2356 match m = MATCH_NO;
2357 if ((first || (m = gfc_match_char (',')) != MATCH_YES)
2358 && (needs_space && gfc_match_space () != MATCH_YES))
2359 break;
2360 needs_space = false;
2361 first = false;
2362 gfc_gobble_whitespace ();
2363 bool end_colon;
2364 gfc_omp_namelist **head;
2365 old_loc = gfc_current_locus;
2366 char pc = gfc_peek_ascii_char ();
2367 if (pc == '\n' && m == MATCH_YES)
2368 {
2369 gfc_error ("Clause expected at %C after trailing comma");
2370 goto error;
2371 }
2372 switch (pc)
2373 {
2374 case 'a':
2375 end_colon = false;
2376 head = NULL;
2377 if ((mask & OMP_CLAUSE_ASSUMPTIONS)
2378 && gfc_match ("absent ( ") == MATCH_YES)
2379 {
2380 if (gfc_omp_absent_contains_clause (&c->assume, true)
2381 != MATCH_YES)
2382 goto error;
2383 continue;
2384 }
2385 if ((mask & OMP_CLAUSE_ALIGNED)
2386 && gfc_match_omp_variable_list ("aligned (",
2387 &c->lists[OMP_LIST_ALIGNED],
2388 false, &end_colon,
2389 &head) == MATCH_YES)
2390 {
2391 gfc_expr *alignment = NULL;
2392 gfc_omp_namelist *n;
2393
2394 if (end_colon && gfc_match (" %e )", &alignment) != MATCH_YES)
2395 {
2396 gfc_free_omp_namelist (*head, false, false, false, false);
2397 gfc_current_locus = old_loc;
2398 *head = NULL;
2399 break;
2400 }
2401 for (n = *head; n; n = n->next)
2402 if (n->next && alignment)
2403 n->expr = gfc_copy_expr (alignment);
2404 else
2405 n->expr = alignment;
2406 continue;
2407 }
2408 if ((mask & OMP_CLAUSE_MEMORDER)
2409 && (m = gfc_match_dupl_memorder ((c->memorder
2410 == OMP_MEMORDER_UNSET),
2411 "acq_rel")) != MATCH_NO)
2412 {
2413 if (m == MATCH_ERROR)
2414 goto error;
2415 c->memorder = OMP_MEMORDER_ACQ_REL;
2416 needs_space = true;
2417 continue;
2418 }
2419 if ((mask & OMP_CLAUSE_MEMORDER)
2420 && (m = gfc_match_dupl_memorder ((c->memorder
2421 == OMP_MEMORDER_UNSET),
2422 "acquire")) != MATCH_NO)
2423 {
2424 if (m == MATCH_ERROR)
2425 goto error;
2426 c->memorder = OMP_MEMORDER_ACQUIRE;
2427 needs_space = true;
2428 continue;
2429 }
2430 if ((mask & OMP_CLAUSE_AFFINITY)
2431 && gfc_match ("affinity ( ") == MATCH_YES)
2432 {
2433 gfc_namespace *ns_iter = NULL, *ns_curr = gfc_current_ns;
2434 m = gfc_match_iterator (&ns_iter, true);
2435 if (m == MATCH_ERROR)
2436 break;
2437 if (m == MATCH_YES && gfc_match (" : ") != MATCH_YES)
2438 {
2439 gfc_error ("Expected %<:%> at %C");
2440 break;
2441 }
2442 if (ns_iter)
2443 gfc_current_ns = ns_iter;
2444 head = NULL;
2445 m = gfc_match_omp_variable_list ("", &c->lists[OMP_LIST_AFFINITY],
2446 false, NULL, &head, true);
2447 gfc_current_ns = ns_curr;
2448 if (m == MATCH_ERROR)
2449 break;
2450 if (ns_iter)
2451 {
2452 for (gfc_omp_namelist *n = *head; n; n = n->next)
2453 {
2454 n->u2.ns = ns_iter;
2455 ns_iter->refs++;
2456 }
2457 }
2458 continue;
2459 }
2460 if ((mask & OMP_CLAUSE_ALLOCATE)
2461 && gfc_match ("allocate ( ") == MATCH_YES)
2462 {
2463 gfc_expr *allocator = NULL;
2464 gfc_expr *align = NULL;
2465 old_loc = gfc_current_locus;
2466 if ((m = gfc_match ("allocator ( %e )", &allocator)) == MATCH_YES)
2467 gfc_match (" , align ( %e )", &align);
2468 else if ((m = gfc_match ("align ( %e )", &align)) == MATCH_YES)
2469 gfc_match (" , allocator ( %e )", &allocator);
2470
2471 if (m == MATCH_YES)
2472 {
2473 if (gfc_match (" : ") != MATCH_YES)
2474 {
2475 gfc_error ("Expected %<:%> at %C");
2476 goto error;
2477 }
2478 }
2479 else
2480 {
2481 m = gfc_match_expr (&allocator);
2482 if (m == MATCH_YES && gfc_match (" : ") != MATCH_YES)
2483 {
2484 /* If no ":" then there is no allocator, we backtrack
2485 and read the variable list. */
2486 gfc_free_expr (allocator);
2487 allocator = NULL;
2488 gfc_current_locus = old_loc;
2489 }
2490 }
2491 gfc_omp_namelist **head = NULL;
2492 m = gfc_match_omp_variable_list ("", &c->lists[OMP_LIST_ALLOCATE],
2493 true, NULL, &head);
2494
2495 if (m != MATCH_YES)
2496 {
2497 gfc_free_expr (allocator);
2498 gfc_free_expr (align);
2499 gfc_error ("Expected variable list at %C");
2500 goto error;
2501 }
2502
2503 for (gfc_omp_namelist *n = *head; n; n = n->next)
2504 {
2505 n->u2.allocator = allocator;
2506 n->u.align = (align) ? gfc_copy_expr (align) : NULL;
2507 }
2508 gfc_free_expr (align);
2509 continue;
2510 }
2511 if ((mask & OMP_CLAUSE_AT)
2512 && (m = gfc_match_dupl_check (c->at == OMP_AT_UNSET, "at", true))
2513 != MATCH_NO)
2514 {
2515 if (m == MATCH_ERROR)
2516 goto error;
2517 if (gfc_match ("compilation )") == MATCH_YES)
2518 c->at = OMP_AT_COMPILATION;
2519 else if (gfc_match ("execution )") == MATCH_YES)
2520 c->at = OMP_AT_EXECUTION;
2521 else
2522 {
2523 gfc_error ("Expected COMPILATION or EXECUTION in AT clause "
2524 "at %C");
2525 goto error;
2526 }
2527 continue;
2528 }
2529 if ((mask & OMP_CLAUSE_ASYNC)
2530 && (m = gfc_match_dupl_check (!c->async, "async")) != MATCH_NO)
2531 {
2532 if (m == MATCH_ERROR)
2533 goto error;
2534 c->async = true;
2535 m = gfc_match (" ( %e )", &c->async_expr);
2536 if (m == MATCH_ERROR)
2537 {
2538 gfc_current_locus = old_loc;
2539 break;
2540 }
2541 else if (m == MATCH_NO)
2542 {
2543 c->async_expr
2544 = gfc_get_constant_expr (BT_INTEGER,
2545 gfc_default_integer_kind,
2546 &gfc_current_locus);
2547 mpz_set_si (c->async_expr->value.integer, GOMP_ASYNC_NOVAL);
2548 needs_space = true;
2549 }
2550 continue;
2551 }
2552 if ((mask & OMP_CLAUSE_AUTO)
2553 && (m = gfc_match_dupl_check (!c->par_auto, "auto"))
2554 != MATCH_NO)
2555 {
2556 if (m == MATCH_ERROR)
2557 goto error;
2558 c->par_auto = true;
2559 needs_space = true;
2560 continue;
2561 }
2562 if ((mask & OMP_CLAUSE_ATTACH)
2563 && gfc_match ("attach ( ") == MATCH_YES
2564 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
2565 OMP_MAP_ATTACH, false,
2566 allow_derived))
2567 continue;
2568 break;
2569 case 'b':
2570 if ((mask & OMP_CLAUSE_BIND)
2571 && (m = gfc_match_dupl_check (c->bind == OMP_BIND_UNSET, "bind",
2572 true)) != MATCH_NO)
2573 {
2574 if (m == MATCH_ERROR)
2575 goto error;
2576 if (gfc_match ("teams )") == MATCH_YES)
2577 c->bind = OMP_BIND_TEAMS;
2578 else if (gfc_match ("parallel )") == MATCH_YES)
2579 c->bind = OMP_BIND_PARALLEL;
2580 else if (gfc_match ("thread )") == MATCH_YES)
2581 c->bind = OMP_BIND_THREAD;
2582 else
2583 {
2584 gfc_error ("Expected TEAMS, PARALLEL or THREAD as binding in "
2585 "BIND at %C");
2586 break;
2587 }
2588 continue;
2589 }
2590 break;
2591 case 'c':
2592 if ((mask & OMP_CLAUSE_CAPTURE)
2593 && (m = gfc_match_dupl_check (!c->capture, "capture"))
2594 != MATCH_NO)
2595 {
2596 if (m == MATCH_ERROR)
2597 goto error;
2598 c->capture = true;
2599 needs_space = true;
2600 continue;
2601 }
2602 if (mask & OMP_CLAUSE_COLLAPSE)
2603 {
2604 gfc_expr *cexpr = NULL;
2605 if ((m = gfc_match_dupl_check (!c->collapse, "collapse", true,
2606 &cexpr)) != MATCH_NO)
2607 {
2608 int collapse;
2609 if (m == MATCH_ERROR)
2610 goto error;
2611 if (gfc_extract_int (cexpr, &collapse, -1))
2612 collapse = 1;
2613 else if (collapse <= 0)
2614 {
2615 gfc_error_now ("COLLAPSE clause argument not constant "
2616 "positive integer at %C");
2617 collapse = 1;
2618 }
2619 gfc_free_expr (cexpr);
2620 c->collapse = collapse;
2621 continue;
2622 }
2623 }
2624 if ((mask & OMP_CLAUSE_COMPARE)
2625 && (m = gfc_match_dupl_check (!c->compare, "compare"))
2626 != MATCH_NO)
2627 {
2628 if (m == MATCH_ERROR)
2629 goto error;
2630 c->compare = true;
2631 needs_space = true;
2632 continue;
2633 }
2634 if ((mask & OMP_CLAUSE_ASSUMPTIONS)
2635 && gfc_match ("contains ( ") == MATCH_YES)
2636 {
2637 if (gfc_omp_absent_contains_clause (&c->assume, false)
2638 != MATCH_YES)
2639 goto error;
2640 continue;
2641 }
2642 if ((mask & OMP_CLAUSE_COPY)
2643 && gfc_match ("copy ( ") == MATCH_YES
2644 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
2645 OMP_MAP_TOFROM, true,
2646 allow_derived))
2647 continue;
2648 if (mask & OMP_CLAUSE_COPYIN)
2649 {
2650 if (openacc)
2651 {
2652 if (gfc_match ("copyin ( ") == MATCH_YES)
2653 {
2654 bool readonly = gfc_match ("readonly : ") == MATCH_YES;
2655 head = NULL;
2656 if (gfc_match_omp_variable_list ("",
2657 &c->lists[OMP_LIST_MAP],
2658 true, NULL, &head, true,
2659 allow_derived)
2660 == MATCH_YES)
2661 {
2662 gfc_omp_namelist *n;
2663 for (n = *head; n; n = n->next)
2664 {
2665 n->u.map.op = OMP_MAP_TO;
2666 n->u.map.readonly = readonly;
2667 }
2668 continue;
2669 }
2670 }
2671 }
2672 else if (gfc_match_omp_variable_list ("copyin (",
2673 &c->lists[OMP_LIST_COPYIN],
2674 true) == MATCH_YES)
2675 continue;
2676 }
2677 if ((mask & OMP_CLAUSE_COPYOUT)
2678 && gfc_match ("copyout ( ") == MATCH_YES
2679 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
2680 OMP_MAP_FROM, true, allow_derived))
2681 continue;
2682 if ((mask & OMP_CLAUSE_COPYPRIVATE)
2683 && gfc_match_omp_variable_list ("copyprivate (",
2684 &c->lists[OMP_LIST_COPYPRIVATE],
2685 true) == MATCH_YES)
2686 continue;
2687 if ((mask & OMP_CLAUSE_CREATE)
2688 && gfc_match ("create ( ") == MATCH_YES
2689 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
2690 OMP_MAP_ALLOC, true, allow_derived))
2691 continue;
2692 break;
2693 case 'd':
2694 if ((mask & OMP_CLAUSE_DEFAULTMAP)
2695 && gfc_match ("defaultmap ( ") == MATCH_YES)
2696 {
2697 enum gfc_omp_defaultmap behavior;
2698 gfc_omp_defaultmap_category category
2699 = OMP_DEFAULTMAP_CAT_UNCATEGORIZED;
2700 if (gfc_match ("alloc ") == MATCH_YES)
2701 behavior = OMP_DEFAULTMAP_ALLOC;
2702 else if (gfc_match ("tofrom ") == MATCH_YES)
2703 behavior = OMP_DEFAULTMAP_TOFROM;
2704 else if (gfc_match ("to ") == MATCH_YES)
2705 behavior = OMP_DEFAULTMAP_TO;
2706 else if (gfc_match ("from ") == MATCH_YES)
2707 behavior = OMP_DEFAULTMAP_FROM;
2708 else if (gfc_match ("firstprivate ") == MATCH_YES)
2709 behavior = OMP_DEFAULTMAP_FIRSTPRIVATE;
2710 else if (gfc_match ("present ") == MATCH_YES)
2711 behavior = OMP_DEFAULTMAP_PRESENT;
2712 else if (gfc_match ("none ") == MATCH_YES)
2713 behavior = OMP_DEFAULTMAP_NONE;
2714 else if (gfc_match ("default ") == MATCH_YES)
2715 behavior = OMP_DEFAULTMAP_DEFAULT;
2716 else
2717 {
2718 gfc_error ("Expected ALLOC, TO, FROM, TOFROM, FIRSTPRIVATE, "
2719 "PRESENT, NONE or DEFAULT at %C");
2720 break;
2721 }
2722 if (')' == gfc_peek_ascii_char ())
2723 ;
2724 else if (gfc_match (": ") != MATCH_YES)
2725 break;
2726 else
2727 {
2728 if (gfc_match ("scalar ") == MATCH_YES)
2729 category = OMP_DEFAULTMAP_CAT_SCALAR;
2730 else if (gfc_match ("aggregate ") == MATCH_YES)
2731 category = OMP_DEFAULTMAP_CAT_AGGREGATE;
2732 else if (gfc_match ("allocatable ") == MATCH_YES)
2733 category = OMP_DEFAULTMAP_CAT_ALLOCATABLE;
2734 else if (gfc_match ("pointer ") == MATCH_YES)
2735 category = OMP_DEFAULTMAP_CAT_POINTER;
2736 else if (gfc_match ("all ") == MATCH_YES)
2737 category = OMP_DEFAULTMAP_CAT_ALL;
2738 else
2739 {
2740 gfc_error ("Expected SCALAR, AGGREGATE, ALLOCATABLE, "
2741 "POINTER or ALL at %C");
2742 break;
2743 }
2744 }
2745 for (int i = 0; i < OMP_DEFAULTMAP_CAT_NUM; ++i)
2746 {
2747 if (i != category
2748 && category != OMP_DEFAULTMAP_CAT_UNCATEGORIZED
2749 && category != OMP_DEFAULTMAP_CAT_ALL
2750 && i != OMP_DEFAULTMAP_CAT_UNCATEGORIZED
2751 && i != OMP_DEFAULTMAP_CAT_ALL)
2752 continue;
2753 if (c->defaultmap[i] != OMP_DEFAULTMAP_UNSET)
2754 {
2755 const char *pcategory = NULL;
2756 switch (i)
2757 {
2758 case OMP_DEFAULTMAP_CAT_UNCATEGORIZED: break;
2759 case OMP_DEFAULTMAP_CAT_ALL: pcategory = "ALL"; break;
2760 case OMP_DEFAULTMAP_CAT_SCALAR: pcategory = "SCALAR"; break;
2761 case OMP_DEFAULTMAP_CAT_AGGREGATE:
2762 pcategory = "AGGREGATE";
2763 break;
2764 case OMP_DEFAULTMAP_CAT_ALLOCATABLE:
2765 pcategory = "ALLOCATABLE";
2766 break;
2767 case OMP_DEFAULTMAP_CAT_POINTER:
2768 pcategory = "POINTER";
2769 break;
2770 default: gcc_unreachable ();
2771 }
2772 if (i == OMP_DEFAULTMAP_CAT_UNCATEGORIZED)
2773 gfc_error ("DEFAULTMAP at %C but prior DEFAULTMAP with "
2774 "unspecified category");
2775 else
2776 gfc_error ("DEFAULTMAP at %C but prior DEFAULTMAP for "
2777 "category %s", pcategory);
2778 goto error;
2779 }
2780 }
2781 c->defaultmap[category] = behavior;
2782 if (gfc_match (")") != MATCH_YES)
2783 break;
2784 continue;
2785 }
2786 if ((mask & OMP_CLAUSE_DEFAULT)
2787 && (m = gfc_match_dupl_check (c->default_sharing
2788 == OMP_DEFAULT_UNKNOWN, "default",
2789 true)) != MATCH_NO)
2790 {
2791 if (m == MATCH_ERROR)
2792 goto error;
2793 if (gfc_match ("none") == MATCH_YES)
2794 c->default_sharing = OMP_DEFAULT_NONE;
2795 else if (openacc)
2796 {
2797 if (gfc_match ("present") == MATCH_YES)
2798 c->default_sharing = OMP_DEFAULT_PRESENT;
2799 }
2800 else
2801 {
2802 if (gfc_match ("firstprivate") == MATCH_YES)
2803 c->default_sharing = OMP_DEFAULT_FIRSTPRIVATE;
2804 else if (gfc_match ("private") == MATCH_YES)
2805 c->default_sharing = OMP_DEFAULT_PRIVATE;
2806 else if (gfc_match ("shared") == MATCH_YES)
2807 c->default_sharing = OMP_DEFAULT_SHARED;
2808 }
2809 if (c->default_sharing == OMP_DEFAULT_UNKNOWN)
2810 {
2811 if (openacc)
2812 gfc_error ("Expected NONE or PRESENT in DEFAULT clause "
2813 "at %C");
2814 else
2815 gfc_error ("Expected NONE, FIRSTPRIVATE, PRIVATE or SHARED "
2816 "in DEFAULT clause at %C");
2817 goto error;
2818 }
2819 if (gfc_match (" )") != MATCH_YES)
2820 goto error;
2821 continue;
2822 }
2823 if ((mask & OMP_CLAUSE_DELETE)
2824 && gfc_match ("delete ( ") == MATCH_YES
2825 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
2826 OMP_MAP_RELEASE, true,
2827 allow_derived))
2828 continue;
2829 /* DOACROSS: match 'doacross' and 'depend' with sink/source.
2830 DEPEND: match 'depend' but not sink/source. */
2831 m = MATCH_NO;
2832 if (((mask & OMP_CLAUSE_DOACROSS)
2833 && gfc_match ("doacross ( ") == MATCH_YES)
2834 || (((mask & OMP_CLAUSE_DEPEND) || (mask & OMP_CLAUSE_DOACROSS))
2835 && (m = gfc_match ("depend ( ")) == MATCH_YES))
2836 {
2837 bool has_omp_all_memory;
2838 bool is_depend = m == MATCH_YES;
2839 gfc_namespace *ns_iter = NULL, *ns_curr = gfc_current_ns;
2840 match m_it = MATCH_NO;
2841 if (is_depend)
2842 m_it = gfc_match_iterator (&ns_iter, false);
2843 if (m_it == MATCH_ERROR)
2844 break;
2845 if (m_it == MATCH_YES && gfc_match (" , ") != MATCH_YES)
2846 break;
2847 m = MATCH_YES;
2848 gfc_omp_depend_doacross_op depend_op = OMP_DEPEND_OUT;
2849 if (gfc_match ("inoutset") == MATCH_YES)
2850 depend_op = OMP_DEPEND_INOUTSET;
2851 else if (gfc_match ("inout") == MATCH_YES)
2852 depend_op = OMP_DEPEND_INOUT;
2853 else if (gfc_match ("in") == MATCH_YES)
2854 depend_op = OMP_DEPEND_IN;
2855 else if (gfc_match ("out") == MATCH_YES)
2856 depend_op = OMP_DEPEND_OUT;
2857 else if (gfc_match ("mutexinoutset") == MATCH_YES)
2858 depend_op = OMP_DEPEND_MUTEXINOUTSET;
2859 else if (gfc_match ("depobj") == MATCH_YES)
2860 depend_op = OMP_DEPEND_DEPOBJ;
2861 else if (gfc_match ("source") == MATCH_YES)
2862 {
2863 if (m_it == MATCH_YES)
2864 {
2865 gfc_error ("ITERATOR may not be combined with SOURCE "
2866 "at %C");
2867 goto error;
2868 }
2869 if (!(mask & OMP_CLAUSE_DOACROSS))
2870 {
2871 gfc_error ("SOURCE at %C not permitted as dependence-type"
2872 " for this directive");
2873 goto error;
2874 }
2875 if (c->doacross_source)
2876 {
2877 gfc_error ("Duplicated clause with SOURCE dependence-type"
2878 " at %C");
2879 goto error;
2880 }
2881 gfc_gobble_whitespace ();
2882 m = gfc_match (": ");
2883 if (m != MATCH_YES && !is_depend)
2884 {
2885 gfc_error ("Expected %<:%> at %C");
2886 goto error;
2887 }
2888 if (gfc_match (")") != MATCH_YES
2889 && !(m == MATCH_YES
2890 && gfc_match ("omp_cur_iteration )") == MATCH_YES))
2891 {
2892 gfc_error ("Expected %<)%> or %<omp_cur_iteration)%> "
2893 "at %C");
2894 goto error;
2895 }
2896 c->doacross_source = true;
2897 c->depend_source = is_depend;
2898 continue;
2899 }
2900 else if (gfc_match ("sink ") == MATCH_YES)
2901 {
2902 if (!(mask & OMP_CLAUSE_DOACROSS))
2903 {
2904 gfc_error ("SINK at %C not permitted as dependence-type "
2905 "for this directive");
2906 goto error;
2907 }
2908 if (gfc_match (": ") != MATCH_YES)
2909 {
2910 gfc_error ("Expected %<:%> at %C");
2911 goto error;
2912 }
2913 if (m_it == MATCH_YES)
2914 {
2915 gfc_error ("ITERATOR may not be combined with SINK "
2916 "at %C");
2917 goto error;
2918 }
2919 m = gfc_match_omp_doacross_sink (&c->lists[OMP_LIST_DEPEND],
2920 is_depend);
2921 if (m == MATCH_YES)
2922 continue;
2923 goto error;
2924 }
2925 else
2926 m = MATCH_NO;
2927 if (!(mask & OMP_CLAUSE_DEPEND))
2928 {
2929 gfc_error ("Expected dependence-type SINK or SOURCE at %C");
2930 goto error;
2931 }
2932 head = NULL;
2933 if (ns_iter)
2934 gfc_current_ns = ns_iter;
2935 if (m == MATCH_YES)
2936 m = gfc_match_omp_variable_list (" : ",
2937 &c->lists[OMP_LIST_DEPEND],
2938 false, NULL, &head, true,
2939 false, &has_omp_all_memory);
2940 if (m != MATCH_YES)
2941 goto error;
2942 gfc_current_ns = ns_curr;
2943 if (has_omp_all_memory && depend_op != OMP_DEPEND_INOUT
2944 && depend_op != OMP_DEPEND_OUT)
2945 {
2946 gfc_error ("%<omp_all_memory%> used with DEPEND kind "
2947 "other than OUT or INOUT at %C");
2948 goto error;
2949 }
2950 gfc_omp_namelist *n;
2951 for (n = *head; n; n = n->next)
2952 {
2953 n->u.depend_doacross_op = depend_op;
2954 n->u2.ns = ns_iter;
2955 if (ns_iter)
2956 ns_iter->refs++;
2957 }
2958 continue;
2959 }
2960 if ((mask & OMP_CLAUSE_DESTROY)
2961 && gfc_match_omp_variable_list ("destroy (",
2962 &c->lists[OMP_LIST_DESTROY],
2963 true) == MATCH_YES)
2964 continue;
2965 if ((mask & OMP_CLAUSE_DETACH)
2966 && !openacc
2967 && !c->detach
2968 && gfc_match_omp_detach (&c->detach) == MATCH_YES)
2969 continue;
2970 if ((mask & OMP_CLAUSE_DETACH)
2971 && openacc
2972 && gfc_match ("detach ( ") == MATCH_YES
2973 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
2974 OMP_MAP_DETACH, false,
2975 allow_derived))
2976 continue;
2977 if ((mask & OMP_CLAUSE_DEVICE)
2978 && !openacc
2979 && ((m = gfc_match_dupl_check (!c->device, "device", true))
2980 != MATCH_NO))
2981 {
2982 if (m == MATCH_ERROR)
2983 goto error;
2984 c->ancestor = false;
2985 if (gfc_match ("device_num : ") == MATCH_YES)
2986 {
2987 if (gfc_match ("%e )", &c->device) != MATCH_YES)
2988 {
2989 gfc_error ("Expected integer expression at %C");
2990 break;
2991 }
2992 }
2993 else if (gfc_match ("ancestor : ") == MATCH_YES)
2994 {
2995 bool has_requires = false;
2996 c->ancestor = true;
2997 for (gfc_namespace *ns = gfc_current_ns; ns; ns = ns->parent)
2998 if (ns->omp_requires & OMP_REQ_REVERSE_OFFLOAD)
2999 {
3000 has_requires = true;
3001 break;
3002 }
3003 if (!has_requires)
3004 {
3005 gfc_error ("%<ancestor%> device modifier not "
3006 "preceded by %<requires%> directive "
3007 "with %<reverse_offload%> clause at %C");
3008 break;
3009 }
3010 locus old_loc2 = gfc_current_locus;
3011 if (gfc_match ("%e )", &c->device) == MATCH_YES)
3012 {
3013 int device = 0;
3014 if (!gfc_extract_int (c->device, &device) && device != 1)
3015 {
3016 gfc_current_locus = old_loc2;
3017 gfc_error ("the %<device%> clause expression must "
3018 "evaluate to %<1%> at %C");
3019 break;
3020 }
3021 }
3022 else
3023 {
3024 gfc_error ("Expected integer expression at %C");
3025 break;
3026 }
3027 }
3028 else if (gfc_match ("%e )", &c->device) != MATCH_YES)
3029 {
3030 gfc_error ("Expected integer expression or a single device-"
3031 "modifier %<device_num%> or %<ancestor%> at %C");
3032 break;
3033 }
3034 continue;
3035 }
3036 if ((mask & OMP_CLAUSE_DEVICE)
3037 && openacc
3038 && gfc_match ("device ( ") == MATCH_YES
3039 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3040 OMP_MAP_FORCE_TO, true,
3041 /* allow_derived = */ true))
3042 continue;
3043 if ((mask & OMP_CLAUSE_DEVICEPTR)
3044 && gfc_match ("deviceptr ( ") == MATCH_YES
3045 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3046 OMP_MAP_FORCE_DEVICEPTR, false,
3047 allow_derived))
3048 continue;
3049 if ((mask & OMP_CLAUSE_DEVICE_TYPE)
3050 && gfc_match ("device_type ( ") == MATCH_YES)
3051 {
3052 if (gfc_match ("host") == MATCH_YES)
3053 c->device_type = OMP_DEVICE_TYPE_HOST;
3054 else if (gfc_match ("nohost") == MATCH_YES)
3055 c->device_type = OMP_DEVICE_TYPE_NOHOST;
3056 else if (gfc_match ("any") == MATCH_YES)
3057 c->device_type = OMP_DEVICE_TYPE_ANY;
3058 else
3059 {
3060 gfc_error ("Expected HOST, NOHOST or ANY at %C");
3061 break;
3062 }
3063 if (gfc_match (" )") != MATCH_YES)
3064 break;
3065 continue;
3066 }
3067 if ((mask & OMP_CLAUSE_DEVICE_RESIDENT)
3068 && gfc_match_omp_variable_list
3069 ("device_resident (",
3070 &c->lists[OMP_LIST_DEVICE_RESIDENT], true) == MATCH_YES)
3071 continue;
3072 if ((mask & OMP_CLAUSE_DIST_SCHEDULE)
3073 && c->dist_sched_kind == OMP_SCHED_NONE
3074 && gfc_match ("dist_schedule ( static") == MATCH_YES)
3075 {
3076 m = MATCH_NO;
3077 c->dist_sched_kind = OMP_SCHED_STATIC;
3078 m = gfc_match (" , %e )", &c->dist_chunk_size);
3079 if (m != MATCH_YES)
3080 m = gfc_match_char (')');
3081 if (m != MATCH_YES)
3082 {
3083 c->dist_sched_kind = OMP_SCHED_NONE;
3084 gfc_current_locus = old_loc;
3085 }
3086 else
3087 continue;
3088 }
3089 break;
3090 case 'e':
3091 if ((mask & OMP_CLAUSE_ENTER))
3092 {
3093 m = gfc_match_omp_to_link ("enter (", &c->lists[OMP_LIST_ENTER]);
3094 if (m == MATCH_ERROR)
3095 goto error;
3096 if (m == MATCH_YES)
3097 continue;
3098 }
3099 break;
3100 case 'f':
3101 if ((mask & OMP_CLAUSE_FAIL)
3102 && (m = gfc_match_dupl_check (c->fail == OMP_MEMORDER_UNSET,
3103 "fail", true)) != MATCH_NO)
3104 {
3105 if (m == MATCH_ERROR)
3106 goto error;
3107 if (gfc_match ("seq_cst") == MATCH_YES)
3108 c->fail = OMP_MEMORDER_SEQ_CST;
3109 else if (gfc_match ("acquire") == MATCH_YES)
3110 c->fail = OMP_MEMORDER_ACQUIRE;
3111 else if (gfc_match ("relaxed") == MATCH_YES)
3112 c->fail = OMP_MEMORDER_RELAXED;
3113 else
3114 {
3115 gfc_error ("Expected SEQ_CST, ACQUIRE or RELAXED at %C");
3116 break;
3117 }
3118 if (gfc_match (" )") != MATCH_YES)
3119 goto error;
3120 continue;
3121 }
3122 if ((mask & OMP_CLAUSE_FILTER)
3123 && (m = gfc_match_dupl_check (!c->filter, "filter", true,
3124 &c->filter)) != MATCH_NO)
3125 {
3126 if (m == MATCH_ERROR)
3127 goto error;
3128 continue;
3129 }
3130 if ((mask & OMP_CLAUSE_FINAL)
3131 && (m = gfc_match_dupl_check (!c->final_expr, "final", true,
3132 &c->final_expr)) != MATCH_NO)
3133 {
3134 if (m == MATCH_ERROR)
3135 goto error;
3136 continue;
3137 }
3138 if ((mask & OMP_CLAUSE_FINALIZE)
3139 && (m = gfc_match_dupl_check (!c->finalize, "finalize"))
3140 != MATCH_NO)
3141 {
3142 if (m == MATCH_ERROR)
3143 goto error;
3144 c->finalize = true;
3145 needs_space = true;
3146 continue;
3147 }
3148 if ((mask & OMP_CLAUSE_FIRSTPRIVATE)
3149 && gfc_match_omp_variable_list ("firstprivate (",
3150 &c->lists[OMP_LIST_FIRSTPRIVATE],
3151 true) == MATCH_YES)
3152 continue;
3153 if ((mask & OMP_CLAUSE_FROM)
3154 && gfc_match_motion_var_list ("from (", &c->lists[OMP_LIST_FROM],
3155 &head) == MATCH_YES)
3156 continue;
3157 if ((mask & OMP_CLAUSE_FULL)
3158 && (m = gfc_match_dupl_check (!c->full, "full")) != MATCH_NO)
3159 {
3160 if (m == MATCH_ERROR)
3161 goto error;
3162 c->full = needs_space = true;
3163 continue;
3164 }
3165 break;
3166 case 'g':
3167 if ((mask & OMP_CLAUSE_GANG)
3168 && (m = gfc_match_dupl_check (!c->gang, "gang")) != MATCH_NO)
3169 {
3170 if (m == MATCH_ERROR)
3171 goto error;
3172 c->gang = true;
3173 m = match_oacc_clause_gwv (c, GOMP_DIM_GANG);
3174 if (m == MATCH_ERROR)
3175 {
3176 gfc_current_locus = old_loc;
3177 break;
3178 }
3179 else if (m == MATCH_NO)
3180 needs_space = true;
3181 continue;
3182 }
3183 if ((mask & OMP_CLAUSE_GRAINSIZE)
3184 && (m = gfc_match_dupl_check (!c->grainsize, "grainsize", true))
3185 != MATCH_NO)
3186 {
3187 if (m == MATCH_ERROR)
3188 goto error;
3189 if (gfc_match ("strict : ") == MATCH_YES)
3190 c->grainsize_strict = true;
3191 if (gfc_match (" %e )", &c->grainsize) != MATCH_YES)
3192 goto error;
3193 continue;
3194 }
3195 break;
3196 case 'h':
3197 if ((mask & OMP_CLAUSE_HAS_DEVICE_ADDR)
3198 && gfc_match_omp_variable_list
3199 ("has_device_addr (", &c->lists[OMP_LIST_HAS_DEVICE_ADDR],
3200 false, NULL, NULL, true) == MATCH_YES)
3201 continue;
3202 if ((mask & OMP_CLAUSE_HINT)
3203 && (m = gfc_match_dupl_check (!c->hint, "hint", true, &c->hint))
3204 != MATCH_NO)
3205 {
3206 if (m == MATCH_ERROR)
3207 goto error;
3208 continue;
3209 }
3210 if ((mask & OMP_CLAUSE_ASSUMPTIONS)
3211 && gfc_match ("holds ( ") == MATCH_YES)
3212 {
3213 gfc_expr *e;
3214 if (gfc_match ("%e )", &e) != MATCH_YES)
3215 goto error;
3216 if (c->assume == NULL)
3217 c->assume = gfc_get_omp_assumptions ();
3218 gfc_expr_list *el = XCNEW (gfc_expr_list);
3219 el->expr = e;
3220 el->next = c->assume->holds;
3221 c->assume->holds = el;
3222 continue;
3223 }
3224 if ((mask & OMP_CLAUSE_HOST)
3225 && gfc_match ("host ( ") == MATCH_YES
3226 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3227 OMP_MAP_FORCE_FROM, true,
3228 /* allow_derived = */ true))
3229 continue;
3230 break;
3231 case 'i':
3232 if ((mask & OMP_CLAUSE_IF_PRESENT)
3233 && (m = gfc_match_dupl_check (!c->if_present, "if_present"))
3234 != MATCH_NO)
3235 {
3236 if (m == MATCH_ERROR)
3237 goto error;
3238 c->if_present = true;
3239 needs_space = true;
3240 continue;
3241 }
3242 if ((mask & OMP_CLAUSE_IF)
3243 && (m = gfc_match_dupl_check (!c->if_expr, "if", true))
3244 != MATCH_NO)
3245 {
3246 if (m == MATCH_ERROR)
3247 goto error;
3248 if (!openacc)
3249 {
3250 /* This should match the enum gfc_omp_if_kind order. */
3251 static const char *ifs[OMP_IF_LAST] = {
3252 "cancel : %e )",
3253 "parallel : %e )",
3254 "simd : %e )",
3255 "task : %e )",
3256 "taskloop : %e )",
3257 "target : %e )",
3258 "target data : %e )",
3259 "target update : %e )",
3260 "target enter data : %e )",
3261 "target exit data : %e )" };
3262 int i;
3263 for (i = 0; i < OMP_IF_LAST; i++)
3264 if (c->if_exprs[i] == NULL
3265 && gfc_match (ifs[i], &c->if_exprs[i]) == MATCH_YES)
3266 break;
3267 if (i < OMP_IF_LAST)
3268 continue;
3269 }
3270 if (gfc_match (" %e )", &c->if_expr) == MATCH_YES)
3271 continue;
3272 goto error;
3273 }
3274 if ((mask & OMP_CLAUSE_IN_REDUCTION)
3275 && gfc_match_omp_clause_reduction (pc, c, openacc, allow_derived,
3276 openmp_target) == MATCH_YES)
3277 continue;
3278 if ((mask & OMP_CLAUSE_INBRANCH)
3279 && (m = gfc_match_dupl_check (!c->inbranch && !c->notinbranch,
3280 "inbranch")) != MATCH_NO)
3281 {
3282 if (m == MATCH_ERROR)
3283 goto error;
3284 c->inbranch = needs_space = true;
3285 continue;
3286 }
3287 if ((mask & OMP_CLAUSE_INDEPENDENT)
3288 && (m = gfc_match_dupl_check (!c->independent, "independent"))
3289 != MATCH_NO)
3290 {
3291 if (m == MATCH_ERROR)
3292 goto error;
3293 c->independent = true;
3294 needs_space = true;
3295 continue;
3296 }
3297 if ((mask & OMP_CLAUSE_INDIRECT)
3298 && (m = gfc_match_dupl_check (!c->indirect, "indirect"))
3299 != MATCH_NO)
3300 {
3301 if (m == MATCH_ERROR)
3302 goto error;
3303 gfc_expr *indirect_expr = NULL;
3304 m = gfc_match (" ( %e )", &indirect_expr);
3305 if (m == MATCH_YES)
3306 {
3307 if (!gfc_resolve_expr (indirect_expr)
3308 || indirect_expr->ts.type != BT_LOGICAL
3309 || indirect_expr->expr_type != EXPR_CONSTANT)
3310 {
3311 gfc_error ("INDIRECT clause at %C requires a constant "
3312 "logical expression");
3313 gfc_free_expr (indirect_expr);
3314 goto error;
3315 }
3316 c->indirect = indirect_expr->value.logical;
3317 gfc_free_expr (indirect_expr);
3318 }
3319 else
3320 c->indirect = 1;
3321 continue;
3322 }
3323 if ((mask & OMP_CLAUSE_INIT)
3324 && gfc_match ("init ( ") == MATCH_YES)
3325 {
3326 m = gfc_match_omp_init (&c->lists[OMP_LIST_INIT]);
3327 if (m == MATCH_YES)
3328 continue;
3329 goto error;
3330 }
3331 if ((mask & OMP_CLAUSE_INTEROP)
3332 && (m = gfc_match_dupl_check (!c->lists[OMP_LIST_INTEROP],
3333 "interop", true)) != MATCH_NO)
3334 {
3335 /* Note: the interop objects are saved in reverse order to match
3336 the order in C/C++. */
3337 if (m == MATCH_YES
3338 && (gfc_match_omp_variable_list ("",
3339 &c->lists[OMP_LIST_INTEROP],
3340 false, NULL, NULL, false,
3341 false, NULL, false, true)
3342 == MATCH_YES))
3343 continue;
3344 goto error;
3345 }
3346 if ((mask & OMP_CLAUSE_IS_DEVICE_PTR)
3347 && gfc_match_omp_variable_list
3348 ("is_device_ptr (",
3349 &c->lists[OMP_LIST_IS_DEVICE_PTR], false) == MATCH_YES)
3350 continue;
3351 break;
3352 case 'l':
3353 if ((mask & OMP_CLAUSE_LASTPRIVATE)
3354 && gfc_match ("lastprivate ( ") == MATCH_YES)
3355 {
3356 bool conditional = gfc_match ("conditional : ") == MATCH_YES;
3357 head = NULL;
3358 if (gfc_match_omp_variable_list ("",
3359 &c->lists[OMP_LIST_LASTPRIVATE],
3360 false, NULL, &head) == MATCH_YES)
3361 {
3362 gfc_omp_namelist *n;
3363 for (n = *head; n; n = n->next)
3364 n->u.lastprivate_conditional = conditional;
3365 continue;
3366 }
3367 gfc_current_locus = old_loc;
3368 break;
3369 }
3370 end_colon = false;
3371 head = NULL;
3372 if ((mask & OMP_CLAUSE_LINEAR)
3373 && gfc_match ("linear (") == MATCH_YES)
3374 {
3375 bool old_linear_modifier = false;
3376 gfc_omp_linear_op linear_op = OMP_LINEAR_DEFAULT;
3377 gfc_expr *step = NULL;
3378
3379 if (gfc_match_omp_variable_list (" ref (",
3380 &c->lists[OMP_LIST_LINEAR],
3381 false, NULL, &head)
3382 == MATCH_YES)
3383 {
3384 linear_op = OMP_LINEAR_REF;
3385 old_linear_modifier = true;
3386 }
3387 else if (gfc_match_omp_variable_list (" val (",
3388 &c->lists[OMP_LIST_LINEAR],
3389 false, NULL, &head)
3390 == MATCH_YES)
3391 {
3392 linear_op = OMP_LINEAR_VAL;
3393 old_linear_modifier = true;
3394 }
3395 else if (gfc_match_omp_variable_list (" uval (",
3396 &c->lists[OMP_LIST_LINEAR],
3397 false, NULL, &head)
3398 == MATCH_YES)
3399 {
3400 linear_op = OMP_LINEAR_UVAL;
3401 old_linear_modifier = true;
3402 }
3403 else if (gfc_match_omp_variable_list ("",
3404 &c->lists[OMP_LIST_LINEAR],
3405 false, &end_colon, &head)
3406 == MATCH_YES)
3407 linear_op = OMP_LINEAR_DEFAULT;
3408 else
3409 {
3410 gfc_current_locus = old_loc;
3411 break;
3412 }
3413 if (linear_op != OMP_LINEAR_DEFAULT)
3414 {
3415 if (gfc_match (" :") == MATCH_YES)
3416 end_colon = true;
3417 else if (gfc_match (" )") != MATCH_YES)
3418 {
3419 gfc_free_omp_namelist (*head, false, false, false, false);
3420 gfc_current_locus = old_loc;
3421 *head = NULL;
3422 break;
3423 }
3424 }
3425 gfc_gobble_whitespace ();
3426 if (old_linear_modifier && end_colon)
3427 {
3428 if (gfc_match (" %e )", &step) != MATCH_YES)
3429 {
3430 gfc_free_omp_namelist (*head, false, false, false, false);
3431 gfc_current_locus = old_loc;
3432 *head = NULL;
3433 goto error;
3434 }
3435 }
3436 else if (end_colon)
3437 {
3438 bool has_error = false;
3439 bool has_modifiers = false;
3440 bool has_step = false;
3441 bool duplicate_step = false;
3442 bool duplicate_mod = false;
3443 while (true)
3444 {
3445 old_loc = gfc_current_locus;
3446 bool close_paren = gfc_match ("val )") == MATCH_YES;
3447 if (close_paren || gfc_match ("val , ") == MATCH_YES)
3448 {
3449 if (linear_op != OMP_LINEAR_DEFAULT)
3450 {
3451 duplicate_mod = true;
3452 break;
3453 }
3454 linear_op = OMP_LINEAR_VAL;
3455 has_modifiers = true;
3456 if (close_paren)
3457 break;
3458 continue;
3459 }
3460 close_paren = gfc_match ("uval )") == MATCH_YES;
3461 if (close_paren || gfc_match ("uval , ") == MATCH_YES)
3462 {
3463 if (linear_op != OMP_LINEAR_DEFAULT)
3464 {
3465 duplicate_mod = true;
3466 break;
3467 }
3468 linear_op = OMP_LINEAR_UVAL;
3469 has_modifiers = true;
3470 if (close_paren)
3471 break;
3472 continue;
3473 }
3474 close_paren = gfc_match ("ref )") == MATCH_YES;
3475 if (close_paren || gfc_match ("ref , ") == MATCH_YES)
3476 {
3477 if (linear_op != OMP_LINEAR_DEFAULT)
3478 {
3479 duplicate_mod = true;
3480 break;
3481 }
3482 linear_op = OMP_LINEAR_REF;
3483 has_modifiers = true;
3484 if (close_paren)
3485 break;
3486 continue;
3487 }
3488 close_paren = (gfc_match ("step ( %e ) )", &step)
3489 == MATCH_YES);
3490 if (close_paren
3491 || gfc_match ("step ( %e ) , ", &step) == MATCH_YES)
3492 {
3493 if (has_step)
3494 {
3495 duplicate_step = true;
3496 break;
3497 }
3498 has_modifiers = has_step = true;
3499 if (close_paren)
3500 break;
3501 continue;
3502 }
3503 if (!has_modifiers
3504 && gfc_match ("%e )", &step) == MATCH_YES)
3505 {
3506 if ((step->expr_type == EXPR_FUNCTION
3507 || step->expr_type == EXPR_VARIABLE)
3508 && strcmp (step->symtree->name, "step") == 0)
3509 {
3510 gfc_current_locus = old_loc;
3511 gfc_match ("step (");
3512 has_error = true;
3513 }
3514 break;
3515 }
3516 has_error = true;
3517 break;
3518 }
3519 if (duplicate_mod || duplicate_step)
3520 {
3521 gfc_error ("Multiple %qs modifiers specified at %C",
3522 duplicate_mod ? "linear" : "step");
3523 has_error = true;
3524 }
3525 if (has_error)
3526 {
3527 gfc_free_omp_namelist (*head, false, false, false, false);
3528 *head = NULL;
3529 goto error;
3530 }
3531 }
3532 if (step == NULL)
3533 {
3534 step = gfc_get_constant_expr (BT_INTEGER,
3535 gfc_default_integer_kind,
3536 &old_loc);
3537 mpz_set_si (step->value.integer, 1);
3538 }
3539 (*head)->expr = step;
3540 if (linear_op != OMP_LINEAR_DEFAULT || old_linear_modifier)
3541 for (gfc_omp_namelist *n = *head; n; n = n->next)
3542 {
3543 n->u.linear.op = linear_op;
3544 n->u.linear.old_modifier = old_linear_modifier;
3545 }
3546 continue;
3547 }
3548 if ((mask & OMP_CLAUSE_LINK)
3549 && openacc
3550 && (gfc_match_oacc_clause_link ("link (",
3551 &c->lists[OMP_LIST_LINK])
3552 == MATCH_YES))
3553 continue;
3554 else if ((mask & OMP_CLAUSE_LINK)
3555 && !openacc
3556 && (gfc_match_omp_to_link ("link (",
3557 &c->lists[OMP_LIST_LINK])
3558 == MATCH_YES))
3559 continue;
3560 break;
3561 case 'm':
3562 if ((mask & OMP_CLAUSE_MAP)
3563 && gfc_match ("map ( ") == MATCH_YES)
3564 {
3565 locus old_loc2 = gfc_current_locus;
3566 int always_modifier = 0;
3567 int close_modifier = 0;
3568 int present_modifier = 0;
3569 locus second_always_locus = old_loc2;
3570 locus second_close_locus = old_loc2;
3571 locus second_present_locus = old_loc2;
3572
3573 for (;;)
3574 {
3575 locus current_locus = gfc_current_locus;
3576 if (gfc_match ("always ") == MATCH_YES)
3577 {
3578 if (always_modifier++ == 1)
3579 second_always_locus = current_locus;
3580 }
3581 else if (gfc_match ("close ") == MATCH_YES)
3582 {
3583 if (close_modifier++ == 1)
3584 second_close_locus = current_locus;
3585 }
3586 else if (gfc_match ("present ") == MATCH_YES)
3587 {
3588 if (present_modifier++ == 1)
3589 second_present_locus = current_locus;
3590 }
3591 else
3592 break;
3593 gfc_match (", ");
3594 }
3595
3596 gfc_omp_map_op map_op = OMP_MAP_TOFROM;
3597 int always_present_modifier
3598 = always_modifier && present_modifier;
3599
3600 if (gfc_match ("alloc : ") == MATCH_YES)
3601 map_op = (present_modifier ? OMP_MAP_PRESENT_ALLOC
3602 : OMP_MAP_ALLOC);
3603 else if (gfc_match ("tofrom : ") == MATCH_YES)
3604 map_op = (always_present_modifier ? OMP_MAP_ALWAYS_PRESENT_TOFROM
3605 : present_modifier ? OMP_MAP_PRESENT_TOFROM
3606 : always_modifier ? OMP_MAP_ALWAYS_TOFROM
3607 : OMP_MAP_TOFROM);
3608 else if (gfc_match ("to : ") == MATCH_YES)
3609 map_op = (always_present_modifier ? OMP_MAP_ALWAYS_PRESENT_TO
3610 : present_modifier ? OMP_MAP_PRESENT_TO
3611 : always_modifier ? OMP_MAP_ALWAYS_TO
3612 : OMP_MAP_TO);
3613 else if (gfc_match ("from : ") == MATCH_YES)
3614 map_op = (always_present_modifier ? OMP_MAP_ALWAYS_PRESENT_FROM
3615 : present_modifier ? OMP_MAP_PRESENT_FROM
3616 : always_modifier ? OMP_MAP_ALWAYS_FROM
3617 : OMP_MAP_FROM);
3618 else if (gfc_match ("release : ") == MATCH_YES)
3619 map_op = OMP_MAP_RELEASE;
3620 else if (gfc_match ("delete : ") == MATCH_YES)
3621 map_op = OMP_MAP_DELETE;
3622 else
3623 {
3624 gfc_current_locus = old_loc2;
3625 always_modifier = 0;
3626 close_modifier = 0;
3627 }
3628
3629 if (always_modifier > 1)
3630 {
3631 gfc_error ("too many %<always%> modifiers at %L",
3632 &second_always_locus);
3633 break;
3634 }
3635 if (close_modifier > 1)
3636 {
3637 gfc_error ("too many %<close%> modifiers at %L",
3638 &second_close_locus);
3639 break;
3640 }
3641 if (present_modifier > 1)
3642 {
3643 gfc_error ("too many %<present%> modifiers at %L",
3644 &second_present_locus);
3645 break;
3646 }
3647
3648 head = NULL;
3649 if (gfc_match_omp_variable_list ("", &c->lists[OMP_LIST_MAP],
3650 false, NULL, &head,
3651 true, true) == MATCH_YES)
3652 {
3653 gfc_omp_namelist *n;
3654 for (n = *head; n; n = n->next)
3655 n->u.map.op = map_op;
3656 continue;
3657 }
3658 gfc_current_locus = old_loc;
3659 break;
3660 }
3661 if ((mask & OMP_CLAUSE_MERGEABLE)
3662 && (m = gfc_match_dupl_check (!c->mergeable, "mergeable"))
3663 != MATCH_NO)
3664 {
3665 if (m == MATCH_ERROR)
3666 goto error;
3667 c->mergeable = needs_space = true;
3668 continue;
3669 }
3670 if ((mask & OMP_CLAUSE_MESSAGE)
3671 && (m = gfc_match_dupl_check (!c->message, "message", true,
3672 &c->message)) != MATCH_NO)
3673 {
3674 if (m == MATCH_ERROR)
3675 goto error;
3676 continue;
3677 }
3678 break;
3679 case 'n':
3680 if ((mask & OMP_CLAUSE_NO_CREATE)
3681 && gfc_match ("no_create ( ") == MATCH_YES
3682 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3683 OMP_MAP_IF_PRESENT, true,
3684 allow_derived))
3685 continue;
3686 if ((mask & OMP_CLAUSE_ASSUMPTIONS)
3687 && (m = gfc_match_dupl_check (!c->assume
3688 || !c->assume->no_openmp_routines,
3689 "no_openmp_routines")) == MATCH_YES)
3690 {
3691 if (m == MATCH_ERROR)
3692 goto error;
3693 if (c->assume == NULL)
3694 c->assume = gfc_get_omp_assumptions ();
3695 c->assume->no_openmp_routines = needs_space = true;
3696 continue;
3697 }
3698 if ((mask & OMP_CLAUSE_ASSUMPTIONS)
3699 && (m = gfc_match_dupl_check (!c->assume || !c->assume->no_openmp,
3700 "no_openmp")) == MATCH_YES)
3701 {
3702 if (m == MATCH_ERROR)
3703 goto error;
3704 if (c->assume == NULL)
3705 c->assume = gfc_get_omp_assumptions ();
3706 c->assume->no_openmp = needs_space = true;
3707 continue;
3708 }
3709 if ((mask & OMP_CLAUSE_ASSUMPTIONS)
3710 && (m = gfc_match_dupl_check (!c->assume
3711 || !c->assume->no_parallelism,
3712 "no_parallelism")) == MATCH_YES)
3713 {
3714 if (m == MATCH_ERROR)
3715 goto error;
3716 if (c->assume == NULL)
3717 c->assume = gfc_get_omp_assumptions ();
3718 c->assume->no_parallelism = needs_space = true;
3719 continue;
3720 }
3721
3722 if ((mask & OMP_CLAUSE_NOVARIANTS)
3723 && (m = gfc_match_dupl_check (!c->novariants, "novariants", true,
3724 &c->novariants))
3725 != MATCH_NO)
3726 {
3727 if (m == MATCH_ERROR)
3728 goto error;
3729 continue;
3730 }
3731 if ((mask & OMP_CLAUSE_NOCONTEXT)
3732 && (m = gfc_match_dupl_check (!c->nocontext, "nocontext", true,
3733 &c->nocontext))
3734 != MATCH_NO)
3735 {
3736 if (m == MATCH_ERROR)
3737 goto error;
3738 continue;
3739 }
3740 if ((mask & OMP_CLAUSE_NOGROUP)
3741 && (m = gfc_match_dupl_check (!c->nogroup, "nogroup"))
3742 != MATCH_NO)
3743 {
3744 if (m == MATCH_ERROR)
3745 goto error;
3746 c->nogroup = needs_space = true;
3747 continue;
3748 }
3749 if ((mask & OMP_CLAUSE_NOHOST)
3750 && (m = gfc_match_dupl_check (!c->nohost, "nohost")) != MATCH_NO)
3751 {
3752 if (m == MATCH_ERROR)
3753 goto error;
3754 c->nohost = needs_space = true;
3755 continue;
3756 }
3757 if ((mask & OMP_CLAUSE_NOTEMPORAL)
3758 && gfc_match_omp_variable_list ("nontemporal (",
3759 &c->lists[OMP_LIST_NONTEMPORAL],
3760 true) == MATCH_YES)
3761 continue;
3762 if ((mask & OMP_CLAUSE_NOTINBRANCH)
3763 && (m = gfc_match_dupl_check (!c->notinbranch && !c->inbranch,
3764 "notinbranch")) != MATCH_NO)
3765 {
3766 if (m == MATCH_ERROR)
3767 goto error;
3768 c->notinbranch = needs_space = true;
3769 continue;
3770 }
3771 if ((mask & OMP_CLAUSE_NOWAIT)
3772 && (m = gfc_match_dupl_check (!c->nowait, "nowait")) != MATCH_NO)
3773 {
3774 if (m == MATCH_ERROR)
3775 goto error;
3776 c->nowait = needs_space = true;
3777 continue;
3778 }
3779 if ((mask & OMP_CLAUSE_NUM_GANGS)
3780 && (m = gfc_match_dupl_check (!c->num_gangs_expr, "num_gangs",
3781 true)) != MATCH_NO)
3782 {
3783 if (m == MATCH_ERROR)
3784 goto error;
3785 if (gfc_match (" %e )", &c->num_gangs_expr) != MATCH_YES)
3786 goto error;
3787 continue;
3788 }
3789 if ((mask & OMP_CLAUSE_NUM_TASKS)
3790 && (m = gfc_match_dupl_check (!c->num_tasks, "num_tasks", true))
3791 != MATCH_NO)
3792 {
3793 if (m == MATCH_ERROR)
3794 goto error;
3795 if (gfc_match ("strict : ") == MATCH_YES)
3796 c->num_tasks_strict = true;
3797 if (gfc_match (" %e )", &c->num_tasks) != MATCH_YES)
3798 goto error;
3799 continue;
3800 }
3801 if ((mask & OMP_CLAUSE_NUM_TEAMS)
3802 && (m = gfc_match_dupl_check (!c->num_teams_upper, "num_teams",
3803 true)) != MATCH_NO)
3804 {
3805 if (m == MATCH_ERROR)
3806 goto error;
3807 if (gfc_match ("%e ", &c->num_teams_upper) != MATCH_YES)
3808 goto error;
3809 if (gfc_peek_ascii_char () == ':')
3810 {
3811 c->num_teams_lower = c->num_teams_upper;
3812 c->num_teams_upper = NULL;
3813 if (gfc_match (": %e ", &c->num_teams_upper) != MATCH_YES)
3814 goto error;
3815 }
3816 if (gfc_match (") ") != MATCH_YES)
3817 goto error;
3818 continue;
3819 }
3820 if ((mask & OMP_CLAUSE_NUM_THREADS)
3821 && (m = gfc_match_dupl_check (!c->num_threads, "num_threads", true,
3822 &c->num_threads)) != MATCH_NO)
3823 {
3824 if (m == MATCH_ERROR)
3825 goto error;
3826 continue;
3827 }
3828 if ((mask & OMP_CLAUSE_NUM_WORKERS)
3829 && (m = gfc_match_dupl_check (!c->num_workers_expr, "num_workers",
3830 true, &c->num_workers_expr))
3831 != MATCH_NO)
3832 {
3833 if (m == MATCH_ERROR)
3834 goto error;
3835 continue;
3836 }
3837 break;
3838 case 'o':
3839 if ((mask & OMP_CLAUSE_ORDER)
3840 && (m = gfc_match_dupl_check (!c->order_concurrent, "order ("))
3841 != MATCH_NO)
3842 {
3843 if (m == MATCH_ERROR)
3844 goto error;
3845 if (gfc_match (" reproducible : concurrent )") == MATCH_YES)
3846 c->order_reproducible = true;
3847 else if (gfc_match (" concurrent )") == MATCH_YES)
3848 ;
3849 else if (gfc_match (" unconstrained : concurrent )") == MATCH_YES)
3850 c->order_unconstrained = true;
3851 else
3852 {
3853 gfc_error ("Expected ORDER(CONCURRENT) at %C "
3854 "with optional %<reproducible%> or "
3855 "%<unconstrained%> modifier");
3856 goto error;
3857 }
3858 c->order_concurrent = true;
3859 continue;
3860 }
3861 if ((mask & OMP_CLAUSE_ORDERED)
3862 && (m = gfc_match_dupl_check (!c->ordered, "ordered"))
3863 != MATCH_NO)
3864 {
3865 if (m == MATCH_ERROR)
3866 goto error;
3867 gfc_expr *cexpr = NULL;
3868 m = gfc_match (" ( %e )", &cexpr);
3869
3870 c->ordered = true;
3871 if (m == MATCH_YES)
3872 {
3873 int ordered = 0;
3874 if (gfc_extract_int (cexpr, &ordered, -1))
3875 ordered = 0;
3876 else if (ordered <= 0)
3877 {
3878 gfc_error_now ("ORDERED clause argument not"
3879 " constant positive integer at %C");
3880 ordered = 0;
3881 }
3882 c->orderedc = ordered;
3883 gfc_free_expr (cexpr);
3884 continue;
3885 }
3886
3887 needs_space = true;
3888 continue;
3889 }
3890 break;
3891 case 'p':
3892 if (mask & OMP_CLAUSE_PARTIAL)
3893 {
3894 if ((m = gfc_match_dupl_check (!c->partial, "partial"))
3895 != MATCH_NO)
3896 {
3897 int expr;
3898 if (m == MATCH_ERROR)
3899 goto error;
3900
3901 c->partial = -1;
3902
3903 gfc_expr *cexpr = NULL;
3904 m = gfc_match (" ( %e )", &cexpr);
3905 if (m == MATCH_NO)
3906 ;
3907 else if (m == MATCH_YES
3908 && !gfc_extract_int (cexpr, &expr, -1)
3909 && expr > 0)
3910 c->partial = expr;
3911 else
3912 gfc_error_now ("PARTIAL clause argument not constant "
3913 "positive integer at %C");
3914 gfc_free_expr (cexpr);
3915 continue;
3916 }
3917 }
3918 if ((mask & OMP_CLAUSE_COPY)
3919 && gfc_match ("pcopy ( ") == MATCH_YES
3920 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3921 OMP_MAP_TOFROM, true, allow_derived))
3922 continue;
3923 if ((mask & OMP_CLAUSE_COPYIN)
3924 && gfc_match ("pcopyin ( ") == MATCH_YES
3925 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3926 OMP_MAP_TO, true, allow_derived))
3927 continue;
3928 if ((mask & OMP_CLAUSE_COPYOUT)
3929 && gfc_match ("pcopyout ( ") == MATCH_YES
3930 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3931 OMP_MAP_FROM, true, allow_derived))
3932 continue;
3933 if ((mask & OMP_CLAUSE_CREATE)
3934 && gfc_match ("pcreate ( ") == MATCH_YES
3935 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3936 OMP_MAP_ALLOC, true, allow_derived))
3937 continue;
3938 if ((mask & OMP_CLAUSE_PRESENT)
3939 && gfc_match ("present ( ") == MATCH_YES
3940 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3941 OMP_MAP_FORCE_PRESENT, false,
3942 allow_derived))
3943 continue;
3944 if ((mask & OMP_CLAUSE_COPY)
3945 && gfc_match ("present_or_copy ( ") == MATCH_YES
3946 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3947 OMP_MAP_TOFROM, true,
3948 allow_derived))
3949 continue;
3950 if ((mask & OMP_CLAUSE_COPYIN)
3951 && gfc_match ("present_or_copyin ( ") == MATCH_YES
3952 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3953 OMP_MAP_TO, true, allow_derived))
3954 continue;
3955 if ((mask & OMP_CLAUSE_COPYOUT)
3956 && gfc_match ("present_or_copyout ( ") == MATCH_YES
3957 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3958 OMP_MAP_FROM, true, allow_derived))
3959 continue;
3960 if ((mask & OMP_CLAUSE_CREATE)
3961 && gfc_match ("present_or_create ( ") == MATCH_YES
3962 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
3963 OMP_MAP_ALLOC, true, allow_derived))
3964 continue;
3965 if ((mask & OMP_CLAUSE_PRIORITY)
3966 && (m = gfc_match_dupl_check (!c->priority, "priority", true,
3967 &c->priority)) != MATCH_NO)
3968 {
3969 if (m == MATCH_ERROR)
3970 goto error;
3971 continue;
3972 }
3973 if ((mask & OMP_CLAUSE_PRIVATE)
3974 && gfc_match_omp_variable_list ("private (",
3975 &c->lists[OMP_LIST_PRIVATE],
3976 true) == MATCH_YES)
3977 continue;
3978 if ((mask & OMP_CLAUSE_PROC_BIND)
3979 && (m = gfc_match_dupl_check ((c->proc_bind
3980 == OMP_PROC_BIND_UNKNOWN),
3981 "proc_bind", true)) != MATCH_NO)
3982 {
3983 if (m == MATCH_ERROR)
3984 goto error;
3985 if (gfc_match ("primary )") == MATCH_YES)
3986 c->proc_bind = OMP_PROC_BIND_PRIMARY;
3987 else if (gfc_match ("master )") == MATCH_YES)
3988 c->proc_bind = OMP_PROC_BIND_MASTER;
3989 else if (gfc_match ("spread )") == MATCH_YES)
3990 c->proc_bind = OMP_PROC_BIND_SPREAD;
3991 else if (gfc_match ("close )") == MATCH_YES)
3992 c->proc_bind = OMP_PROC_BIND_CLOSE;
3993 else
3994 goto error;
3995 continue;
3996 }
3997 break;
3998 case 'r':
3999 if ((mask & OMP_CLAUSE_ATOMIC)
4000 && (m = gfc_match_dupl_atomic ((c->atomic_op
4001 == GFC_OMP_ATOMIC_UNSET),
4002 "read")) != MATCH_NO)
4003 {
4004 if (m == MATCH_ERROR)
4005 goto error;
4006 c->atomic_op = GFC_OMP_ATOMIC_READ;
4007 needs_space = true;
4008 continue;
4009 }
4010 if ((mask & OMP_CLAUSE_REDUCTION)
4011 && gfc_match_omp_clause_reduction (pc, c, openacc,
4012 allow_derived) == MATCH_YES)
4013 continue;
4014 if ((mask & OMP_CLAUSE_MEMORDER)
4015 && (m = gfc_match_dupl_memorder ((c->memorder
4016 == OMP_MEMORDER_UNSET),
4017 "relaxed")) != MATCH_NO)
4018 {
4019 if (m == MATCH_ERROR)
4020 goto error;
4021 c->memorder = OMP_MEMORDER_RELAXED;
4022 needs_space = true;
4023 continue;
4024 }
4025 if ((mask & OMP_CLAUSE_MEMORDER)
4026 && (m = gfc_match_dupl_memorder ((c->memorder
4027 == OMP_MEMORDER_UNSET),
4028 "release")) != MATCH_NO)
4029 {
4030 if (m == MATCH_ERROR)
4031 goto error;
4032 c->memorder = OMP_MEMORDER_RELEASE;
4033 needs_space = true;
4034 continue;
4035 }
4036 break;
4037 case 's':
4038 if ((mask & OMP_CLAUSE_SAFELEN)
4039 && (m = gfc_match_dupl_check (!c->safelen_expr, "safelen",
4040 true, &c->safelen_expr))
4041 != MATCH_NO)
4042 {
4043 if (m == MATCH_ERROR)
4044 goto error;
4045 continue;
4046 }
4047 if ((mask & OMP_CLAUSE_SCHEDULE)
4048 && (m = gfc_match_dupl_check (c->sched_kind == OMP_SCHED_NONE,
4049 "schedule", true)) != MATCH_NO)
4050 {
4051 if (m == MATCH_ERROR)
4052 goto error;
4053 int nmodifiers = 0;
4054 locus old_loc2 = gfc_current_locus;
4055 do
4056 {
4057 if (gfc_match ("simd") == MATCH_YES)
4058 {
4059 c->sched_simd = true;
4060 nmodifiers++;
4061 }
4062 else if (gfc_match ("monotonic") == MATCH_YES)
4063 {
4064 c->sched_monotonic = true;
4065 nmodifiers++;
4066 }
4067 else if (gfc_match ("nonmonotonic") == MATCH_YES)
4068 {
4069 c->sched_nonmonotonic = true;
4070 nmodifiers++;
4071 }
4072 else
4073 {
4074 if (nmodifiers)
4075 gfc_current_locus = old_loc2;
4076 break;
4077 }
4078 if (nmodifiers == 1
4079 && gfc_match (" , ") == MATCH_YES)
4080 continue;
4081 else if (gfc_match (" : ") == MATCH_YES)
4082 break;
4083 gfc_current_locus = old_loc2;
4084 break;
4085 }
4086 while (1);
4087 if (gfc_match ("static") == MATCH_YES)
4088 c->sched_kind = OMP_SCHED_STATIC;
4089 else if (gfc_match ("dynamic") == MATCH_YES)
4090 c->sched_kind = OMP_SCHED_DYNAMIC;
4091 else if (gfc_match ("guided") == MATCH_YES)
4092 c->sched_kind = OMP_SCHED_GUIDED;
4093 else if (gfc_match ("runtime") == MATCH_YES)
4094 c->sched_kind = OMP_SCHED_RUNTIME;
4095 else if (gfc_match ("auto") == MATCH_YES)
4096 c->sched_kind = OMP_SCHED_AUTO;
4097 if (c->sched_kind != OMP_SCHED_NONE)
4098 {
4099 m = MATCH_NO;
4100 if (c->sched_kind != OMP_SCHED_RUNTIME
4101 && c->sched_kind != OMP_SCHED_AUTO)
4102 m = gfc_match (" , %e )", &c->chunk_size);
4103 if (m != MATCH_YES)
4104 m = gfc_match_char (')');
4105 if (m != MATCH_YES)
4106 c->sched_kind = OMP_SCHED_NONE;
4107 }
4108 if (c->sched_kind != OMP_SCHED_NONE)
4109 continue;
4110 else
4111 gfc_current_locus = old_loc;
4112 }
4113 if ((mask & OMP_CLAUSE_SELF)
4114 && !(mask & OMP_CLAUSE_HOST) /* OpenACC compute construct */
4115 && (m = gfc_match_dupl_check (!c->self_expr, "self"))
4116 != MATCH_NO)
4117 {
4118 if (m == MATCH_ERROR)
4119 goto error;
4120 m = gfc_match (" ( %e )", &c->self_expr);
4121 if (m == MATCH_ERROR)
4122 {
4123 gfc_current_locus = old_loc;
4124 break;
4125 }
4126 else if (m == MATCH_NO)
4127 {
4128 c->self_expr = gfc_get_logical_expr (gfc_default_logical_kind,
4129 NULL, true);
4130 needs_space = true;
4131 }
4132 continue;
4133 }
4134 if ((mask & OMP_CLAUSE_SELF)
4135 && (mask & OMP_CLAUSE_HOST) /* OpenACC 'update' directive */
4136 && gfc_match ("self ( ") == MATCH_YES
4137 && gfc_match_omp_map_clause (&c->lists[OMP_LIST_MAP],
4138 OMP_MAP_FORCE_FROM, true,
4139 /* allow_derived = */ true))
4140 continue;
4141 if ((mask & OMP_CLAUSE_SEQ)
4142 && (m = gfc_match_dupl_check (!c->seq, "seq")) != MATCH_NO)
4143 {
4144 if (m == MATCH_ERROR)
4145 goto error;
4146 c->seq = true;
4147 needs_space = true;
4148 continue;
4149 }
4150 if ((mask & OMP_CLAUSE_MEMORDER)
4151 && (m = gfc_match_dupl_memorder ((c->memorder
4152 == OMP_MEMORDER_UNSET),
4153 "seq_cst")) != MATCH_NO)
4154 {
4155 if (m == MATCH_ERROR)
4156 goto error;
4157 c->memorder = OMP_MEMORDER_SEQ_CST;
4158 needs_space = true;
4159 continue;
4160 }
4161 if ((mask & OMP_CLAUSE_SHARED)
4162 && gfc_match_omp_variable_list ("shared (",
4163 &c->lists[OMP_LIST_SHARED],
4164 true) == MATCH_YES)
4165 continue;
4166 if ((mask & OMP_CLAUSE_SIMDLEN)
4167 && (m = gfc_match_dupl_check (!c->simdlen_expr, "simdlen", true,
4168 &c->simdlen_expr)) != MATCH_NO)
4169 {
4170 if (m == MATCH_ERROR)
4171 goto error;
4172 continue;
4173 }
4174 if ((mask & OMP_CLAUSE_SIMD)
4175 && (m = gfc_match_dupl_check (!c->simd, "simd")) != MATCH_NO)
4176 {
4177 if (m == MATCH_ERROR)
4178 goto error;
4179 c->simd = needs_space = true;
4180 continue;
4181 }
4182 if ((mask & OMP_CLAUSE_SEVERITY)
4183 && (m = gfc_match_dupl_check (!c->severity, "severity", true))
4184 != MATCH_NO)
4185 {
4186 if (m == MATCH_ERROR)
4187 goto error;
4188 if (gfc_match ("fatal )") == MATCH_YES)
4189 c->severity = OMP_SEVERITY_FATAL;
4190 else if (gfc_match ("warning )") == MATCH_YES)
4191 c->severity = OMP_SEVERITY_WARNING;
4192 else
4193 {
4194 gfc_error ("Expected FATAL or WARNING in SEVERITY clause "
4195 "at %C");
4196 goto error;
4197 }
4198 continue;
4199 }
4200 if ((mask & OMP_CLAUSE_SIZES)
4201 && ((m = gfc_match_dupl_check (!c->sizes_list, "sizes"))
4202 != MATCH_NO))
4203 {
4204 if (m == MATCH_ERROR)
4205 goto error;
4206 m = match_omp_oacc_expr_list (" (", &c->sizes_list, false, true);
4207 if (m == MATCH_ERROR)
4208 goto error;
4209 if (m == MATCH_YES)
4210 continue;
4211 gfc_error ("Expected %<(%> after %qs at %C", "sizes");
4212 goto error;
4213 }
4214 break;
4215 case 't':
4216 if ((mask & OMP_CLAUSE_TASK_REDUCTION)
4217 && gfc_match_omp_clause_reduction (pc, c, openacc,
4218 allow_derived) == MATCH_YES)
4219 continue;
4220 if ((mask & OMP_CLAUSE_THREAD_LIMIT)
4221 && (m = gfc_match_dupl_check (!c->thread_limit, "thread_limit",
4222 true, &c->thread_limit))
4223 != MATCH_NO)
4224 {
4225 if (m == MATCH_ERROR)
4226 goto error;
4227 continue;
4228 }
4229 if ((mask & OMP_CLAUSE_THREADS)
4230 && (m = gfc_match_dupl_check (!c->threads, "threads"))
4231 != MATCH_NO)
4232 {
4233 if (m == MATCH_ERROR)
4234 goto error;
4235 c->threads = needs_space = true;
4236 continue;
4237 }
4238 if ((mask & OMP_CLAUSE_TILE)
4239 && !c->tile_list
4240 && match_omp_oacc_expr_list ("tile (", &c->tile_list,
4241 true, false) == MATCH_YES)
4242 continue;
4243 if ((mask & OMP_CLAUSE_TO) && (mask & OMP_CLAUSE_LINK))
4244 {
4245 /* Declare target: 'to' is an alias for 'enter';
4246 'to' is deprecated since 5.2. */
4247 m = gfc_match_omp_to_link ("to (", &c->lists[OMP_LIST_TO]);
4248 if (m == MATCH_ERROR)
4249 goto error;
4250 if (m == MATCH_YES)
4251 continue;
4252 }
4253 else if ((mask & OMP_CLAUSE_TO)
4254 && gfc_match_motion_var_list ("to (", &c->lists[OMP_LIST_TO],
4255 &head) == MATCH_YES)
4256 continue;
4257 break;
4258 case 'u':
4259 if ((mask & OMP_CLAUSE_UNIFORM)
4260 && gfc_match_omp_variable_list ("uniform (",
4261 &c->lists[OMP_LIST_UNIFORM],
4262 false) == MATCH_YES)
4263 continue;
4264 if ((mask & OMP_CLAUSE_UNTIED)
4265 && (m = gfc_match_dupl_check (!c->untied, "untied")) != MATCH_NO)
4266 {
4267 if (m == MATCH_ERROR)
4268 goto error;
4269 c->untied = needs_space = true;
4270 continue;
4271 }
4272 if ((mask & OMP_CLAUSE_ATOMIC)
4273 && (m = gfc_match_dupl_atomic ((c->atomic_op
4274 == GFC_OMP_ATOMIC_UNSET),
4275 "update")) != MATCH_NO)
4276 {
4277 if (m == MATCH_ERROR)
4278 goto error;
4279 c->atomic_op = GFC_OMP_ATOMIC_UPDATE;
4280 needs_space = true;
4281 continue;
4282 }
4283 if ((mask & OMP_CLAUSE_USE)
4284 && gfc_match_omp_variable_list ("use (",
4285 &c->lists[OMP_LIST_USE],
4286 true) == MATCH_YES)
4287 continue;
4288 if ((mask & OMP_CLAUSE_USE_DEVICE)
4289 && gfc_match_omp_variable_list ("use_device (",
4290 &c->lists[OMP_LIST_USE_DEVICE],
4291 true) == MATCH_YES)
4292 continue;
4293 if ((mask & OMP_CLAUSE_USE_DEVICE_PTR)
4294 && gfc_match_omp_variable_list
4295 ("use_device_ptr (",
4296 &c->lists[OMP_LIST_USE_DEVICE_PTR], false) == MATCH_YES)
4297 continue;
4298 if ((mask & OMP_CLAUSE_USE_DEVICE_ADDR)
4299 && gfc_match_omp_variable_list
4300 ("use_device_addr (", &c->lists[OMP_LIST_USE_DEVICE_ADDR],
4301 false, NULL, NULL, true) == MATCH_YES)
4302 continue;
4303 if ((mask & OMP_CLAUSE_USES_ALLOCATORS)
4304 && (gfc_match ("uses_allocators ( ") == MATCH_YES))
4305 {
4306 if (gfc_match_omp_clause_uses_allocators (c) != MATCH_YES)
4307 goto error;
4308 continue;
4309 }
4310 break;
4311 case 'v':
4312 /* VECTOR_LENGTH must be matched before VECTOR, because the latter
4313 doesn't unconditionally match '('. */
4314 if ((mask & OMP_CLAUSE_VECTOR_LENGTH)
4315 && (m = gfc_match_dupl_check (!c->vector_length_expr,
4316 "vector_length", true,
4317 &c->vector_length_expr))
4318 != MATCH_NO)
4319 {
4320 if (m == MATCH_ERROR)
4321 goto error;
4322 continue;
4323 }
4324 if ((mask & OMP_CLAUSE_VECTOR)
4325 && (m = gfc_match_dupl_check (!c->vector, "vector")) != MATCH_NO)
4326 {
4327 if (m == MATCH_ERROR)
4328 goto error;
4329 c->vector = true;
4330 m = match_oacc_clause_gwv (c, GOMP_DIM_VECTOR);
4331 if (m == MATCH_ERROR)
4332 goto error;
4333 if (m == MATCH_NO)
4334 needs_space = true;
4335 continue;
4336 }
4337 break;
4338 case 'w':
4339 if ((mask & OMP_CLAUSE_WAIT)
4340 && gfc_match ("wait") == MATCH_YES)
4341 {
4342 m = match_omp_oacc_expr_list (" (", &c->wait_list, false, false);
4343 if (m == MATCH_ERROR)
4344 goto error;
4345 else if (m == MATCH_NO)
4346 {
4347 gfc_expr *expr
4348 = gfc_get_constant_expr (BT_INTEGER,
4349 gfc_default_integer_kind,
4350 &gfc_current_locus);
4351 mpz_set_si (expr->value.integer, GOMP_ASYNC_NOVAL);
4352 gfc_expr_list **expr_list = &c->wait_list;
4353 while (*expr_list)
4354 expr_list = &(*expr_list)->next;
4355 *expr_list = gfc_get_expr_list ();
4356 (*expr_list)->expr = expr;
4357 needs_space = true;
4358 }
4359 continue;
4360 }
4361 if ((mask & OMP_CLAUSE_WEAK)
4362 && (m = gfc_match_dupl_check (!c->weak, "weak"))
4363 != MATCH_NO)
4364 {
4365 if (m == MATCH_ERROR)
4366 goto error;
4367 c->weak = true;
4368 needs_space = true;
4369 continue;
4370 }
4371 if ((mask & OMP_CLAUSE_WORKER)
4372 && (m = gfc_match_dupl_check (!c->worker, "worker")) != MATCH_NO)
4373 {
4374 if (m == MATCH_ERROR)
4375 goto error;
4376 c->worker = true;
4377 m = match_oacc_clause_gwv (c, GOMP_DIM_WORKER);
4378 if (m == MATCH_ERROR)
4379 goto error;
4380 else if (m == MATCH_NO)
4381 needs_space = true;
4382 continue;
4383 }
4384 if ((mask & OMP_CLAUSE_ATOMIC)
4385 && (m = gfc_match_dupl_atomic ((c->atomic_op
4386 == GFC_OMP_ATOMIC_UNSET),
4387 "write")) != MATCH_NO)
4388 {
4389 if (m == MATCH_ERROR)
4390 goto error;
4391 c->atomic_op = GFC_OMP_ATOMIC_WRITE;
4392 needs_space = true;
4393 continue;
4394 }
4395 break;
4396 }
4397 break;
4398 }
4399
4400 end:
4401 if (error || gfc_match_omp_eos () != MATCH_YES)
4402 {
4403 if (!gfc_error_flag_test ())
4404 gfc_error ("Failed to match clause at %C");
4405 gfc_free_omp_clauses (c);
4406 return MATCH_ERROR;
4407 }
4408
4409 *cp = c;
4410 return MATCH_YES;
4411
4412 error:
4413 error = true;
4414 goto end;
4415 }
4416
4417
4418 #define OACC_PARALLEL_CLAUSES \
4419 (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_ASYNC | OMP_CLAUSE_NUM_GANGS \
4420 | OMP_CLAUSE_NUM_WORKERS | OMP_CLAUSE_VECTOR_LENGTH | OMP_CLAUSE_REDUCTION \
4421 | OMP_CLAUSE_COPY | OMP_CLAUSE_COPYIN | OMP_CLAUSE_COPYOUT \
4422 | OMP_CLAUSE_CREATE | OMP_CLAUSE_NO_CREATE | OMP_CLAUSE_PRESENT \
4423 | OMP_CLAUSE_DEVICEPTR | OMP_CLAUSE_PRIVATE | OMP_CLAUSE_FIRSTPRIVATE \
4424 | OMP_CLAUSE_DEFAULT | OMP_CLAUSE_WAIT | OMP_CLAUSE_ATTACH \
4425 | OMP_CLAUSE_SELF)
4426 #define OACC_KERNELS_CLAUSES \
4427 (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_ASYNC | OMP_CLAUSE_NUM_GANGS \
4428 | OMP_CLAUSE_NUM_WORKERS | OMP_CLAUSE_VECTOR_LENGTH | OMP_CLAUSE_DEVICEPTR \
4429 | OMP_CLAUSE_COPY | OMP_CLAUSE_COPYIN | OMP_CLAUSE_COPYOUT \
4430 | OMP_CLAUSE_CREATE | OMP_CLAUSE_NO_CREATE | OMP_CLAUSE_PRESENT \
4431 | OMP_CLAUSE_DEFAULT | OMP_CLAUSE_WAIT | OMP_CLAUSE_ATTACH \
4432 | OMP_CLAUSE_SELF)
4433 #define OACC_SERIAL_CLAUSES \
4434 (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_ASYNC | OMP_CLAUSE_REDUCTION \
4435 | OMP_CLAUSE_COPY | OMP_CLAUSE_COPYIN | OMP_CLAUSE_COPYOUT \
4436 | OMP_CLAUSE_CREATE | OMP_CLAUSE_NO_CREATE | OMP_CLAUSE_PRESENT \
4437 | OMP_CLAUSE_DEVICEPTR | OMP_CLAUSE_PRIVATE | OMP_CLAUSE_FIRSTPRIVATE \
4438 | OMP_CLAUSE_DEFAULT | OMP_CLAUSE_WAIT | OMP_CLAUSE_ATTACH \
4439 | OMP_CLAUSE_SELF)
4440 #define OACC_DATA_CLAUSES \
4441 (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_DEVICEPTR | OMP_CLAUSE_COPY \
4442 | OMP_CLAUSE_COPYIN | OMP_CLAUSE_COPYOUT | OMP_CLAUSE_CREATE \
4443 | OMP_CLAUSE_NO_CREATE | OMP_CLAUSE_PRESENT | OMP_CLAUSE_ATTACH \
4444 | OMP_CLAUSE_DEFAULT)
4445 #define OACC_LOOP_CLAUSES \
4446 (omp_mask (OMP_CLAUSE_COLLAPSE) | OMP_CLAUSE_GANG | OMP_CLAUSE_WORKER \
4447 | OMP_CLAUSE_VECTOR | OMP_CLAUSE_SEQ | OMP_CLAUSE_INDEPENDENT \
4448 | OMP_CLAUSE_PRIVATE | OMP_CLAUSE_REDUCTION | OMP_CLAUSE_AUTO \
4449 | OMP_CLAUSE_TILE)
4450 #define OACC_PARALLEL_LOOP_CLAUSES \
4451 (OACC_LOOP_CLAUSES | OACC_PARALLEL_CLAUSES)
4452 #define OACC_KERNELS_LOOP_CLAUSES \
4453 (OACC_LOOP_CLAUSES | OACC_KERNELS_CLAUSES)
4454 #define OACC_SERIAL_LOOP_CLAUSES \
4455 (OACC_LOOP_CLAUSES | OACC_SERIAL_CLAUSES)
4456 #define OACC_HOST_DATA_CLAUSES \
4457 (omp_mask (OMP_CLAUSE_USE_DEVICE) \
4458 | OMP_CLAUSE_IF \
4459 | OMP_CLAUSE_IF_PRESENT)
4460 #define OACC_DECLARE_CLAUSES \
4461 (omp_mask (OMP_CLAUSE_COPY) | OMP_CLAUSE_COPYIN | OMP_CLAUSE_COPYOUT \
4462 | OMP_CLAUSE_CREATE | OMP_CLAUSE_DEVICEPTR | OMP_CLAUSE_DEVICE_RESIDENT \
4463 | OMP_CLAUSE_PRESENT \
4464 | OMP_CLAUSE_LINK)
4465 #define OACC_UPDATE_CLAUSES \
4466 (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_ASYNC | OMP_CLAUSE_HOST \
4467 | OMP_CLAUSE_DEVICE | OMP_CLAUSE_WAIT | OMP_CLAUSE_IF_PRESENT \
4468 | OMP_CLAUSE_SELF)
4469 #define OACC_ENTER_DATA_CLAUSES \
4470 (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_ASYNC | OMP_CLAUSE_WAIT \
4471 | OMP_CLAUSE_COPYIN | OMP_CLAUSE_CREATE | OMP_CLAUSE_ATTACH)
4472 #define OACC_EXIT_DATA_CLAUSES \
4473 (omp_mask (OMP_CLAUSE_IF) | OMP_CLAUSE_ASYNC | OMP_CLAUSE_WAIT \
4474 | OMP_CLAUSE_COPYOUT | OMP_CLAUSE_DELETE | OMP_CLAUSE_FINALIZE \
4475 | OMP_CLAUSE_DETACH)
4476 #define OACC_WAIT_CLAUSES \
4477 omp_mask (OMP_CLAUSE_ASYNC)
4478 #define OACC_ROUTINE_CLAUSES \
4479 (omp_mask (OMP_CLAUSE_GANG) | OMP_CLAUSE_WORKER | OMP_CLAUSE_VECTOR \
4480 | OMP_CLAUSE_SEQ \
4481 | OMP_CLAUSE_NOHOST)
4482
4483
4484 static match
4485 match_acc (gfc_exec_op op, const omp_mask mask)
4486 {
4487 gfc_omp_clauses *c;
4488 if (gfc_match_omp_clauses (&c, mask, false, false, true) != MATCH_YES)
4489 return MATCH_ERROR;
4490 new_st.op = op;
4491 new_st.ext.omp_clauses = c;
4492 return MATCH_YES;
4493 }
4494
4495 match
4496 gfc_match_oacc_parallel_loop (void)
4497 {
4498 return match_acc (EXEC_OACC_PARALLEL_LOOP, OACC_PARALLEL_LOOP_CLAUSES);
4499 }
4500
4501
4502 match
4503 gfc_match_oacc_parallel (void)
4504 {
4505 return match_acc (EXEC_OACC_PARALLEL, OACC_PARALLEL_CLAUSES);
4506 }
4507
4508
4509 match
4510 gfc_match_oacc_kernels_loop (void)
4511 {
4512 return match_acc (EXEC_OACC_KERNELS_LOOP, OACC_KERNELS_LOOP_CLAUSES);
4513 }
4514
4515
4516 match
4517 gfc_match_oacc_kernels (void)
4518 {
4519 return match_acc (EXEC_OACC_KERNELS, OACC_KERNELS_CLAUSES);
4520 }
4521
4522
4523 match
4524 gfc_match_oacc_serial_loop (void)
4525 {
4526 return match_acc (EXEC_OACC_SERIAL_LOOP, OACC_SERIAL_LOOP_CLAUSES);
4527 }
4528
4529
4530 match
4531 gfc_match_oacc_serial (void)
4532 {
4533 return match_acc (EXEC_OACC_SERIAL, OACC_SERIAL_CLAUSES);
4534 }
4535
4536
4537 match
4538 gfc_match_oacc_data (void)
4539 {
4540 return match_acc (EXEC_OACC_DATA, OACC_DATA_CLAUSES);
4541 }
4542
4543
4544 match
4545 gfc_match_oacc_host_data (void)
4546 {
4547 return match_acc (EXEC_OACC_HOST_DATA, OACC_HOST_DATA_CLAUSES);
4548 }
4549
4550
4551 match
4552 gfc_match_oacc_loop (void)
4553 {
4554 return match_acc (EXEC_OACC_LOOP, OACC_LOOP_CLAUSES);
4555 }
4556
4557
4558 match
4559 gfc_match_oacc_declare (void)
4560 {
4561 gfc_omp_clauses *c;
4562 gfc_omp_namelist *n;
4563 gfc_namespace *ns = gfc_current_ns;
4564 gfc_oacc_declare *new_oc;
4565 bool module_var = false;
4566 locus where = gfc_current_locus;
4567
4568 if (gfc_match_omp_clauses (&c, OACC_DECLARE_CLAUSES, false, false, true)
4569 != MATCH_YES)
4570 return MATCH_ERROR;
4571
4572 for (n = c->lists[OMP_LIST_DEVICE_RESIDENT]; n != NULL; n = n->next)
4573 n->sym->attr.oacc_declare_device_resident = 1;
4574
4575 for (n = c->lists[OMP_LIST_LINK]; n != NULL; n = n->next)
4576 n->sym->attr.oacc_declare_link = 1;
4577
4578 for (n = c->lists[OMP_LIST_MAP]; n != NULL; n = n->next)
4579 {
4580 gfc_symbol *s = n->sym;
4581
4582 if (gfc_current_ns->proc_name
4583 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
4584 {
4585 if (n->u.map.op != OMP_MAP_ALLOC && n->u.map.op != OMP_MAP_TO)
4586 {
4587 gfc_error ("Invalid clause in module with !$ACC DECLARE at %L",
4588 &where);
4589 return MATCH_ERROR;
4590 }
4591
4592 module_var = true;
4593 }
4594
4595 if (s->attr.use_assoc)
4596 {
4597 gfc_error ("Variable is USE-associated with !$ACC DECLARE at %L",
4598 &where);
4599 return MATCH_ERROR;
4600 }
4601
4602 if ((s->result == s && s->ns->contained != gfc_current_ns)
4603 || ((s->attr.flavor == FL_UNKNOWN || s->attr.flavor == FL_VARIABLE)
4604 && s->ns != gfc_current_ns))
4605 {
4606 gfc_error ("Variable %qs shall be declared in the same scoping unit "
4607 "as !$ACC DECLARE at %L", s->name, &where);
4608 return MATCH_ERROR;
4609 }
4610
4611 if ((s->attr.dimension || s->attr.codimension)
4612 && s->attr.dummy && s->as->type != AS_EXPLICIT)
4613 {
4614 gfc_error ("Assumed-size dummy array with !$ACC DECLARE at %L",
4615 &where);
4616 return MATCH_ERROR;
4617 }
4618
4619 switch (n->u.map.op)
4620 {
4621 case OMP_MAP_FORCE_ALLOC:
4622 case OMP_MAP_ALLOC:
4623 s->attr.oacc_declare_create = 1;
4624 break;
4625
4626 case OMP_MAP_FORCE_TO:
4627 case OMP_MAP_TO:
4628 s->attr.oacc_declare_copyin = 1;
4629 break;
4630
4631 case OMP_MAP_FORCE_DEVICEPTR:
4632 s->attr.oacc_declare_deviceptr = 1;
4633 break;
4634
4635 default:
4636 break;
4637 }
4638 }
4639
4640 new_oc = gfc_get_oacc_declare ();
4641 new_oc->next = ns->oacc_declare;
4642 new_oc->module_var = module_var;
4643 new_oc->clauses = c;
4644 new_oc->loc = gfc_current_locus;
4645 ns->oacc_declare = new_oc;
4646
4647 return MATCH_YES;
4648 }
4649
4650
4651 match
4652 gfc_match_oacc_update (void)
4653 {
4654 gfc_omp_clauses *c;
4655 locus here = gfc_current_locus;
4656
4657 if (gfc_match_omp_clauses (&c, OACC_UPDATE_CLAUSES, false, false, true)
4658 != MATCH_YES)
4659 return MATCH_ERROR;
4660
4661 if (!c->lists[OMP_LIST_MAP])
4662 {
4663 gfc_error ("%<acc update%> must contain at least one "
4664 "%<device%> or %<host%> or %<self%> clause at %L", &here);
4665 return MATCH_ERROR;
4666 }
4667
4668 new_st.op = EXEC_OACC_UPDATE;
4669 new_st.ext.omp_clauses = c;
4670 return MATCH_YES;
4671 }
4672
4673
4674 match
4675 gfc_match_oacc_enter_data (void)
4676 {
4677 return match_acc (EXEC_OACC_ENTER_DATA, OACC_ENTER_DATA_CLAUSES);
4678 }
4679
4680
4681 match
4682 gfc_match_oacc_exit_data (void)
4683 {
4684 return match_acc (EXEC_OACC_EXIT_DATA, OACC_EXIT_DATA_CLAUSES);
4685 }
4686
4687
4688 match
4689 gfc_match_oacc_wait (void)
4690 {
4691 gfc_omp_clauses *c = gfc_get_omp_clauses ();
4692 gfc_expr_list *wait_list = NULL, *el;
4693 bool space = true;
4694 match m;
4695
4696 m = match_omp_oacc_expr_list (" (", &wait_list, true, false);
4697 if (m == MATCH_ERROR)
4698 return m;
4699 else if (m == MATCH_YES)
4700 space = false;
4701
4702 if (gfc_match_omp_clauses (&c, OACC_WAIT_CLAUSES, space, space, true)
4703 == MATCH_ERROR)
4704 return MATCH_ERROR;
4705
4706 if (wait_list)
4707 for (el = wait_list; el; el = el->next)
4708 {
4709 if (el->expr == NULL)
4710 {
4711 gfc_error ("Invalid argument to !$ACC WAIT at %C");
4712 return MATCH_ERROR;
4713 }
4714
4715 if (!gfc_resolve_expr (el->expr)
4716 || el->expr->ts.type != BT_INTEGER || el->expr->rank != 0)
4717 {
4718 gfc_error ("WAIT clause at %L requires a scalar INTEGER expression",
4719 &el->expr->where);
4720
4721 return MATCH_ERROR;
4722 }
4723 }
4724 c->wait_list = wait_list;
4725 new_st.op = EXEC_OACC_WAIT;
4726 new_st.ext.omp_clauses = c;
4727 return MATCH_YES;
4728 }
4729
4730
4731 match
4732 gfc_match_oacc_cache (void)
4733 {
4734 bool readonly = false;
4735 gfc_omp_clauses *c = gfc_get_omp_clauses ();
4736 /* The OpenACC cache directive explicitly only allows "array elements or
4737 subarrays", which we're currently not checking here. Either check this
4738 after the call of gfc_match_omp_variable_list, or add something like a
4739 only_sections variant next to its allow_sections parameter. */
4740 match m = gfc_match (" ( ");
4741 if (m != MATCH_YES)
4742 {
4743 gfc_free_omp_clauses(c);
4744 return m;
4745 }
4746
4747 if (gfc_match ("readonly : ") == MATCH_YES)
4748 readonly = true;
4749
4750 gfc_omp_namelist **head = NULL;
4751 m = gfc_match_omp_variable_list ("", &c->lists[OMP_LIST_CACHE], true,
4752 NULL, &head, true);
4753 if (m != MATCH_YES)
4754 {
4755 gfc_free_omp_clauses(c);
4756 return m;
4757 }
4758
4759 if (readonly)
4760 for (gfc_omp_namelist *n = *head; n; n = n->next)
4761 n->u.map.readonly = true;
4762
4763 if (gfc_current_state() != COMP_DO
4764 && gfc_current_state() != COMP_DO_CONCURRENT)
4765 {
4766 gfc_error ("ACC CACHE directive must be inside of loop %C");
4767 gfc_free_omp_clauses(c);
4768 return MATCH_ERROR;
4769 }
4770
4771 new_st.op = EXEC_OACC_CACHE;
4772 new_st.ext.omp_clauses = c;
4773 return MATCH_YES;
4774 }
4775
4776 /* Determine the OpenACC 'routine' directive's level of parallelism. */
4777
4778 static oacc_routine_lop
4779 gfc_oacc_routine_lop (gfc_omp_clauses *clauses)
4780 {
4781 oacc_routine_lop ret = OACC_ROUTINE_LOP_SEQ;
4782
4783 if (clauses)
4784 {
4785 unsigned n_lop_clauses = 0;
4786
4787 if (clauses->gang)
4788 {
4789 ++n_lop_clauses;
4790 ret = OACC_ROUTINE_LOP_GANG;
4791 }
4792 if (clauses->worker)
4793 {
4794 ++n_lop_clauses;
4795 ret = OACC_ROUTINE_LOP_WORKER;
4796 }
4797 if (clauses->vector)
4798 {
4799 ++n_lop_clauses;
4800 ret = OACC_ROUTINE_LOP_VECTOR;
4801 }
4802 if (clauses->seq)
4803 {
4804 ++n_lop_clauses;
4805 ret = OACC_ROUTINE_LOP_SEQ;
4806 }
4807
4808 if (n_lop_clauses > 1)
4809 ret = OACC_ROUTINE_LOP_ERROR;
4810 }
4811
4812 return ret;
4813 }
4814
4815 match
4816 gfc_match_oacc_routine (void)
4817 {
4818 locus old_loc;
4819 match m;
4820 gfc_intrinsic_sym *isym = NULL;
4821 gfc_symbol *sym = NULL;
4822 gfc_omp_clauses *c = NULL;
4823 gfc_oacc_routine_name *n = NULL;
4824 oacc_routine_lop lop = OACC_ROUTINE_LOP_NONE;
4825 bool nohost;
4826
4827 old_loc = gfc_current_locus;
4828
4829 m = gfc_match (" (");
4830
4831 if (gfc_current_ns->proc_name
4832 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
4833 && m == MATCH_YES)
4834 {
4835 gfc_error ("Only the !$ACC ROUTINE form without "
4836 "list is allowed in interface block at %C");
4837 goto cleanup;
4838 }
4839
4840 if (m == MATCH_YES)
4841 {
4842 char buffer[GFC_MAX_SYMBOL_LEN + 1];
4843
4844 m = gfc_match_name (buffer);
4845 if (m == MATCH_YES)
4846 {
4847 gfc_symtree *st = NULL;
4848
4849 /* First look for an intrinsic symbol. */
4850 isym = gfc_find_function (buffer);
4851 if (!isym)
4852 isym = gfc_find_subroutine (buffer);
4853 /* If no intrinsic symbol found, search the current namespace. */
4854 if (!isym)
4855 st = gfc_find_symtree (gfc_current_ns->sym_root, buffer);
4856 if (st)
4857 {
4858 sym = st->n.sym;
4859 /* If the name in a 'routine' directive refers to the containing
4860 subroutine or function, then make sure that we'll later handle
4861 this accordingly. */
4862 if (gfc_current_ns->proc_name != NULL
4863 && strcmp (sym->name, gfc_current_ns->proc_name->name) == 0)
4864 sym = NULL;
4865 }
4866
4867 if (isym == NULL && st == NULL)
4868 {
4869 gfc_error ("Invalid NAME %qs in !$ACC ROUTINE ( NAME ) at %C",
4870 buffer);
4871 gfc_current_locus = old_loc;
4872 return MATCH_ERROR;
4873 }
4874 }
4875 else
4876 {
4877 gfc_error ("Syntax error in !$ACC ROUTINE ( NAME ) at %C");
4878 gfc_current_locus = old_loc;
4879 return MATCH_ERROR;
4880 }
4881
4882 if (gfc_match_char (')') != MATCH_YES)
4883 {
4884 gfc_error ("Syntax error in !$ACC ROUTINE ( NAME ) at %C, expecting"
4885 " %<)%> after NAME");
4886 gfc_current_locus = old_loc;
4887 return MATCH_ERROR;
4888 }
4889 }
4890
4891 if (gfc_match_omp_eos () != MATCH_YES
4892 && (gfc_match_omp_clauses (&c, OACC_ROUTINE_CLAUSES, false, false, true)
4893 != MATCH_YES))
4894 return MATCH_ERROR;
4895
4896 lop = gfc_oacc_routine_lop (c);
4897 if (lop == OACC_ROUTINE_LOP_ERROR)
4898 {
4899 gfc_error ("Multiple loop axes specified for routine at %C");
4900 goto cleanup;
4901 }
4902 nohost = c ? c->nohost : false;
4903
4904 if (isym != NULL)
4905 {
4906 /* Diagnose any OpenACC 'routine' directive that doesn't match the
4907 (implicit) one with a 'seq' clause. */
4908 if (c && (c->gang || c->worker || c->vector))
4909 {
4910 gfc_error ("Intrinsic symbol specified in !$ACC ROUTINE ( NAME )"
4911 " at %C marked with incompatible GANG, WORKER, or VECTOR"
4912 " clause");
4913 goto cleanup;
4914 }
4915 /* ..., and no 'nohost' clause. */
4916 if (nohost)
4917 {
4918 gfc_error ("Intrinsic symbol specified in !$ACC ROUTINE ( NAME )"
4919 " at %C marked with incompatible NOHOST clause");
4920 goto cleanup;
4921 }
4922 }
4923 else if (sym != NULL)
4924 {
4925 bool add = true;
4926
4927 /* For a repeated OpenACC 'routine' directive, diagnose if it doesn't
4928 match the first one. */
4929 for (gfc_oacc_routine_name *n_p = gfc_current_ns->oacc_routine_names;
4930 n_p;
4931 n_p = n_p->next)
4932 if (n_p->sym == sym)
4933 {
4934 add = false;
4935 bool nohost_p = n_p->clauses ? n_p->clauses->nohost : false;
4936 if (lop != gfc_oacc_routine_lop (n_p->clauses)
4937 || nohost != nohost_p)
4938 {
4939 gfc_error ("!$ACC ROUTINE already applied at %C");
4940 goto cleanup;
4941 }
4942 }
4943
4944 if (add)
4945 {
4946 sym->attr.oacc_routine_lop = lop;
4947 sym->attr.oacc_routine_nohost = nohost;
4948
4949 n = gfc_get_oacc_routine_name ();
4950 n->sym = sym;
4951 n->clauses = c;
4952 n->next = gfc_current_ns->oacc_routine_names;
4953 n->loc = old_loc;
4954 gfc_current_ns->oacc_routine_names = n;
4955 }
4956 }
4957 else if (gfc_current_ns->proc_name)
4958 {
4959 /* For a repeated OpenACC 'routine' directive, diagnose if it doesn't
4960 match the first one. */
4961 oacc_routine_lop lop_p = gfc_current_ns->proc_name->attr.oacc_routine_lop;
4962 bool nohost_p = gfc_current_ns->proc_name->attr.oacc_routine_nohost;
4963 if (lop_p != OACC_ROUTINE_LOP_NONE
4964 && (lop != lop_p
4965 || nohost != nohost_p))
4966 {
4967 gfc_error ("!$ACC ROUTINE already applied at %C");
4968 goto cleanup;
4969 }
4970
4971 if (!gfc_add_omp_declare_target (&gfc_current_ns->proc_name->attr,
4972 gfc_current_ns->proc_name->name,
4973 &old_loc))
4974 goto cleanup;
4975 gfc_current_ns->proc_name->attr.oacc_routine_lop = lop;
4976 gfc_current_ns->proc_name->attr.oacc_routine_nohost = nohost;
4977 }
4978 else
4979 /* Something has gone wrong, possibly a syntax error. */
4980 goto cleanup;
4981
4982 if (gfc_pure (NULL) && c && (c->gang || c->worker || c->vector))
4983 {
4984 gfc_error ("!$ACC ROUTINE with GANG, WORKER, or VECTOR clause is not "
4985 "permitted in PURE procedure at %C");
4986 goto cleanup;
4987 }
4988
4989
4990 if (n)
4991 n->clauses = c;
4992 else if (gfc_current_ns->oacc_routine)
4993 gfc_current_ns->oacc_routine_clauses = c;
4994
4995 new_st.op = EXEC_OACC_ROUTINE;
4996 new_st.ext.omp_clauses = c;
4997 return MATCH_YES;
4998
4999 cleanup:
5000 gfc_current_locus = old_loc;
5001 return MATCH_ERROR;
5002 }
5003
5004
5005 #define OMP_PARALLEL_CLAUSES \
5006 (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE \
5007 | OMP_CLAUSE_SHARED | OMP_CLAUSE_COPYIN | OMP_CLAUSE_REDUCTION \
5008 | OMP_CLAUSE_IF | OMP_CLAUSE_NUM_THREADS | OMP_CLAUSE_DEFAULT \
5009 | OMP_CLAUSE_PROC_BIND | OMP_CLAUSE_ALLOCATE)
5010 #define OMP_DECLARE_SIMD_CLAUSES \
5011 (omp_mask (OMP_CLAUSE_SIMDLEN) | OMP_CLAUSE_LINEAR \
5012 | OMP_CLAUSE_UNIFORM | OMP_CLAUSE_ALIGNED | OMP_CLAUSE_INBRANCH \
5013 | OMP_CLAUSE_NOTINBRANCH)
5014 #define OMP_DO_CLAUSES \
5015 (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE \
5016 | OMP_CLAUSE_LASTPRIVATE | OMP_CLAUSE_REDUCTION \
5017 | OMP_CLAUSE_SCHEDULE | OMP_CLAUSE_ORDERED | OMP_CLAUSE_COLLAPSE \
5018 | OMP_CLAUSE_LINEAR | OMP_CLAUSE_ORDER | OMP_CLAUSE_ALLOCATE \
5019 | OMP_CLAUSE_NOWAIT)
5020 #define OMP_LOOP_CLAUSES \
5021 (omp_mask (OMP_CLAUSE_BIND) | OMP_CLAUSE_COLLAPSE | OMP_CLAUSE_ORDER \
5022 | OMP_CLAUSE_PRIVATE | OMP_CLAUSE_LASTPRIVATE | OMP_CLAUSE_REDUCTION)
5023
5024 #define OMP_SCOPE_CLAUSES \
5025 (omp_mask (OMP_CLAUSE_PRIVATE) |OMP_CLAUSE_FIRSTPRIVATE \
5026 | OMP_CLAUSE_REDUCTION | OMP_CLAUSE_ALLOCATE | OMP_CLAUSE_NOWAIT)
5027 #define OMP_SECTIONS_CLAUSES \
5028 (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE \
5029 | OMP_CLAUSE_LASTPRIVATE | OMP_CLAUSE_REDUCTION \
5030 | OMP_CLAUSE_ALLOCATE | OMP_CLAUSE_NOWAIT)
5031 #define OMP_SIMD_CLAUSES \
5032 (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_LASTPRIVATE \
5033 | OMP_CLAUSE_REDUCTION | OMP_CLAUSE_COLLAPSE | OMP_CLAUSE_SAFELEN \
5034 | OMP_CLAUSE_LINEAR | OMP_CLAUSE_ALIGNED | OMP_CLAUSE_SIMDLEN \
5035 | OMP_CLAUSE_IF | OMP_CLAUSE_ORDER | OMP_CLAUSE_NOTEMPORAL)
5036 #define OMP_TASK_CLAUSES \
5037 (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE \
5038 | OMP_CLAUSE_SHARED | OMP_CLAUSE_IF | OMP_CLAUSE_DEFAULT \
5039 | OMP_CLAUSE_UNTIED | OMP_CLAUSE_FINAL | OMP_CLAUSE_MERGEABLE \
5040 | OMP_CLAUSE_DEPEND | OMP_CLAUSE_PRIORITY | OMP_CLAUSE_IN_REDUCTION \
5041 | OMP_CLAUSE_DETACH | OMP_CLAUSE_AFFINITY | OMP_CLAUSE_ALLOCATE)
5042 #define OMP_TASKLOOP_CLAUSES \
5043 (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE \
5044 | OMP_CLAUSE_LASTPRIVATE | OMP_CLAUSE_SHARED | OMP_CLAUSE_IF \
5045 | OMP_CLAUSE_DEFAULT | OMP_CLAUSE_UNTIED | OMP_CLAUSE_FINAL \
5046 | OMP_CLAUSE_MERGEABLE | OMP_CLAUSE_PRIORITY | OMP_CLAUSE_GRAINSIZE \
5047 | OMP_CLAUSE_NUM_TASKS | OMP_CLAUSE_COLLAPSE | OMP_CLAUSE_NOGROUP \
5048 | OMP_CLAUSE_REDUCTION | OMP_CLAUSE_IN_REDUCTION | OMP_CLAUSE_ALLOCATE)
5049 #define OMP_TASKGROUP_CLAUSES \
5050 (omp_mask (OMP_CLAUSE_TASK_REDUCTION) | OMP_CLAUSE_ALLOCATE)
5051 #define OMP_TARGET_CLAUSES \
5052 (omp_mask (OMP_CLAUSE_DEVICE) | OMP_CLAUSE_MAP | OMP_CLAUSE_IF \
5053 | OMP_CLAUSE_DEPEND | OMP_CLAUSE_NOWAIT | OMP_CLAUSE_PRIVATE \
5054 | OMP_CLAUSE_FIRSTPRIVATE | OMP_CLAUSE_DEFAULTMAP \
5055 | OMP_CLAUSE_IS_DEVICE_PTR | OMP_CLAUSE_IN_REDUCTION \
5056 | OMP_CLAUSE_THREAD_LIMIT | OMP_CLAUSE_ALLOCATE \
5057 | OMP_CLAUSE_HAS_DEVICE_ADDR | OMP_CLAUSE_USES_ALLOCATORS)
5058 #define OMP_TARGET_DATA_CLAUSES \
5059 (omp_mask (OMP_CLAUSE_DEVICE) | OMP_CLAUSE_MAP | OMP_CLAUSE_IF \
5060 | OMP_CLAUSE_USE_DEVICE_PTR | OMP_CLAUSE_USE_DEVICE_ADDR)
5061 #define OMP_TARGET_ENTER_DATA_CLAUSES \
5062 (omp_mask (OMP_CLAUSE_DEVICE) | OMP_CLAUSE_MAP | OMP_CLAUSE_IF \
5063 | OMP_CLAUSE_DEPEND | OMP_CLAUSE_NOWAIT)
5064 #define OMP_TARGET_EXIT_DATA_CLAUSES \
5065 (omp_mask (OMP_CLAUSE_DEVICE) | OMP_CLAUSE_MAP | OMP_CLAUSE_IF \
5066 | OMP_CLAUSE_DEPEND | OMP_CLAUSE_NOWAIT)
5067 #define OMP_TARGET_UPDATE_CLAUSES \
5068 (omp_mask (OMP_CLAUSE_DEVICE) | OMP_CLAUSE_IF | OMP_CLAUSE_TO \
5069 | OMP_CLAUSE_FROM | OMP_CLAUSE_DEPEND | OMP_CLAUSE_NOWAIT)
5070 #define OMP_TEAMS_CLAUSES \
5071 (omp_mask (OMP_CLAUSE_NUM_TEAMS) | OMP_CLAUSE_THREAD_LIMIT \
5072 | OMP_CLAUSE_DEFAULT | OMP_CLAUSE_PRIVATE | OMP_CLAUSE_FIRSTPRIVATE \
5073 | OMP_CLAUSE_SHARED | OMP_CLAUSE_REDUCTION | OMP_CLAUSE_ALLOCATE)
5074 #define OMP_DISTRIBUTE_CLAUSES \
5075 (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE \
5076 | OMP_CLAUSE_LASTPRIVATE | OMP_CLAUSE_COLLAPSE | OMP_CLAUSE_DIST_SCHEDULE \
5077 | OMP_CLAUSE_ORDER | OMP_CLAUSE_ALLOCATE)
5078 #define OMP_SINGLE_CLAUSES \
5079 (omp_mask (OMP_CLAUSE_PRIVATE) | OMP_CLAUSE_FIRSTPRIVATE \
5080 | OMP_CLAUSE_ALLOCATE | OMP_CLAUSE_NOWAIT | OMP_CLAUSE_COPYPRIVATE)
5081 #define OMP_ORDERED_CLAUSES \
5082 (omp_mask (OMP_CLAUSE_THREADS) | OMP_CLAUSE_SIMD)
5083 #define OMP_DECLARE_TARGET_CLAUSES \
5084 (omp_mask (OMP_CLAUSE_ENTER) | OMP_CLAUSE_LINK | OMP_CLAUSE_DEVICE_TYPE \
5085 | OMP_CLAUSE_TO | OMP_CLAUSE_INDIRECT)
5086 #define OMP_ATOMIC_CLAUSES \
5087 (omp_mask (OMP_CLAUSE_ATOMIC) | OMP_CLAUSE_CAPTURE | OMP_CLAUSE_HINT \
5088 | OMP_CLAUSE_MEMORDER | OMP_CLAUSE_COMPARE | OMP_CLAUSE_FAIL \
5089 | OMP_CLAUSE_WEAK)
5090 #define OMP_MASKED_CLAUSES \
5091 (omp_mask (OMP_CLAUSE_FILTER))
5092 #define OMP_ERROR_CLAUSES \
5093 (omp_mask (OMP_CLAUSE_AT) | OMP_CLAUSE_MESSAGE | OMP_CLAUSE_SEVERITY)
5094 #define OMP_WORKSHARE_CLAUSES \
5095 omp_mask (OMP_CLAUSE_NOWAIT)
5096 #define OMP_UNROLL_CLAUSES \
5097 (omp_mask (OMP_CLAUSE_FULL) | OMP_CLAUSE_PARTIAL)
5098 #define OMP_TILE_CLAUSES \
5099 (omp_mask (OMP_CLAUSE_SIZES))
5100 #define OMP_ALLOCATORS_CLAUSES \
5101 omp_mask (OMP_CLAUSE_ALLOCATE)
5102 #define OMP_INTEROP_CLAUSES \
5103 (omp_mask (OMP_CLAUSE_DEPEND) | OMP_CLAUSE_NOWAIT | OMP_CLAUSE_DEVICE \
5104 | OMP_CLAUSE_INIT | OMP_CLAUSE_DESTROY | OMP_CLAUSE_USE)
5105 #define OMP_DISPATCH_CLAUSES \
5106 (omp_mask (OMP_CLAUSE_DEVICE) | OMP_CLAUSE_DEPEND | OMP_CLAUSE_NOVARIANTS \
5107 | OMP_CLAUSE_NOCONTEXT | OMP_CLAUSE_IS_DEVICE_PTR | OMP_CLAUSE_NOWAIT \
5108 | OMP_CLAUSE_HAS_DEVICE_ADDR | OMP_CLAUSE_INTEROP)
5109
5110
5111 static match
5112 match_omp (gfc_exec_op op, const omp_mask mask)
5113 {
5114 gfc_omp_clauses *c;
5115 if (gfc_match_omp_clauses (&c, mask, true, true, false,
5116 op == EXEC_OMP_TARGET) != MATCH_YES)
5117 return MATCH_ERROR;
5118 new_st.op = op;
5119 new_st.ext.omp_clauses = c;
5120 return MATCH_YES;
5121 }
5122
5123 /* Handles both declarative and (deprecated) executable ALLOCATE directive;
5124 accepts optional list (for executable) and common blocks.
5125 If no variables have been provided, the single omp namelist has sym == NULL.
5126
5127 Note that the executable ALLOCATE directive permits structure elements only
5128 in OpenMP 5.0 and 5.1 but not longer in 5.2. See also the comment on the
5129 'omp allocators' directive below. The accidental change was reverted for
5130 OpenMP TR12, permitting them again. See also gfc_match_omp_allocators.
5131
5132 Hence, structure elements are rejected for now, also to make resolving
5133 OMP_LIST_ALLOCATE simpler (check for duplicates, same symbol in
5134 Fortran allocate stmt). TODO: Permit structure elements. */
5135
5136 match
5137 gfc_match_omp_allocate (void)
5138 {
5139 match m;
5140 bool first = true;
5141 gfc_omp_namelist *vars = NULL;
5142 gfc_expr *align = NULL;
5143 gfc_expr *allocator = NULL;
5144 locus loc = gfc_current_locus;
5145
5146 m = gfc_match_omp_variable_list (" (", &vars, true, NULL, NULL, true, true,
5147 NULL, true);
5148
5149 if (m == MATCH_ERROR)
5150 return m;
5151
5152 while (true)
5153 {
5154 gfc_gobble_whitespace ();
5155 if (gfc_match_omp_eos () == MATCH_YES)
5156 break;
5157 if (!first)
5158 gfc_match (", ");
5159 first = false;
5160 if ((m = gfc_match_dupl_check (!align, "align", true, &align))
5161 != MATCH_NO)
5162 {
5163 if (m == MATCH_ERROR)
5164 goto error;
5165 continue;
5166 }
5167 if ((m = gfc_match_dupl_check (!allocator, "allocator",
5168 true, &allocator)) != MATCH_NO)
5169 {
5170 if (m == MATCH_ERROR)
5171 goto error;
5172 continue;
5173 }
5174 gfc_error ("Expected ALIGN or ALLOCATOR clause at %C");
5175 return MATCH_ERROR;
5176 }
5177 for (gfc_omp_namelist *n = vars; n; n = n->next)
5178 if (n->expr)
5179 {
5180 if ((n->expr->ref && n->expr->ref->type == REF_COMPONENT)
5181 || (n->expr->ref->next && n->expr->ref->type == REF_COMPONENT))
5182 gfc_error ("Sorry, structure-element list item at %L in ALLOCATE "
5183 "directive is not yet supported", &n->expr->where);
5184 else
5185 gfc_error ("Unexpected expression as list item at %L in ALLOCATE "
5186 "directive", &n->expr->where);
5187
5188 gfc_free_omp_namelist (vars, false, true, false, false);
5189 goto error;
5190 }
5191
5192 new_st.op = EXEC_OMP_ALLOCATE;
5193 new_st.ext.omp_clauses = gfc_get_omp_clauses ();
5194 if (vars == NULL)
5195 {
5196 vars = gfc_get_omp_namelist ();
5197 vars->where = loc;
5198 vars->u.align = align;
5199 vars->u2.allocator = allocator;
5200 new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE] = vars;
5201 }
5202 else
5203 {
5204 new_st.ext.omp_clauses->lists[OMP_LIST_ALLOCATE] = vars;
5205 for (; vars; vars = vars->next)
5206 {
5207 vars->u.align = (align) ? gfc_copy_expr (align) : NULL;
5208 vars->u2.allocator = allocator;
5209 }
5210 gfc_free_expr (align);
5211 }
5212 return MATCH_YES;
5213
5214 error:
5215 gfc_free_expr (align);
5216 gfc_free_expr (allocator);
5217 return MATCH_ERROR;
5218 }
5219
5220 /* In line with OpenMP 5.2 derived-type components are rejected.
5221 See also comment before gfc_match_omp_allocate. */
5222
5223 match
5224 gfc_match_omp_allocators (void)
5225 {
5226 return match_omp (EXEC_OMP_ALLOCATORS, OMP_ALLOCATORS_CLAUSES);
5227 }
5228
5229
5230 match
5231 gfc_match_omp_assume (void)
5232 {
5233 gfc_omp_clauses *c;
5234 locus loc = gfc_current_locus;
5235 if ((gfc_match_omp_clauses (&c, omp_mask (OMP_CLAUSE_ASSUMPTIONS))
5236 != MATCH_YES)
5237 || (omp_verify_merge_absent_contains (ST_OMP_ASSUME, c->assume, NULL,
5238 &loc) != MATCH_YES))
5239 return MATCH_ERROR;
5240 new_st.op = EXEC_OMP_ASSUME;
5241 new_st.ext.omp_clauses = c;
5242 return MATCH_YES;
5243 }
5244
5245
5246 match
5247 gfc_match_omp_assumes (void)
5248 {
5249 gfc_omp_clauses *c;
5250 locus loc = gfc_current_locus;
5251 if (!gfc_current_ns->proc_name
5252 || (gfc_current_ns->proc_name->attr.flavor != FL_MODULE
5253 && !gfc_current_ns->proc_name->attr.subroutine
5254 && !gfc_current_ns->proc_name->attr.function))
5255 {
5256 gfc_error ("!$OMP ASSUMES at %C must be in the specification part of a "
5257 "subprogram or module");
5258 return MATCH_ERROR;
5259 }
5260 if ((gfc_match_omp_clauses (&c, omp_mask (OMP_CLAUSE_ASSUMPTIONS))
5261 != MATCH_YES)
5262 || (omp_verify_merge_absent_contains (ST_OMP_ASSUMES, c->assume,
5263 gfc_current_ns->omp_assumes, &loc)
5264 != MATCH_YES))
5265 return MATCH_ERROR;
5266 if (gfc_current_ns->omp_assumes == NULL)
5267 {
5268 gfc_current_ns->omp_assumes = c->assume;
5269 c->assume = NULL;
5270 }
5271 else if (gfc_current_ns->omp_assumes && c->assume)
5272 {
5273 gfc_current_ns->omp_assumes->no_openmp |= c->assume->no_openmp;
5274 gfc_current_ns->omp_assumes->no_openmp_routines
5275 |= c->assume->no_openmp_routines;
5276 gfc_current_ns->omp_assumes->no_parallelism |= c->assume->no_parallelism;
5277 if (gfc_current_ns->omp_assumes->holds && c->assume->holds)
5278 {
5279 gfc_expr_list *el = gfc_current_ns->omp_assumes->holds;
5280 for ( ; el->next ; el = el->next)
5281 ;
5282 el->next = c->assume->holds;
5283 }
5284 else if (c->assume->holds)
5285 gfc_current_ns->omp_assumes->holds = c->assume->holds;
5286 c->assume->holds = NULL;
5287 }
5288 gfc_free_omp_clauses (c);
5289 return MATCH_YES;
5290 }
5291
5292
5293 match
5294 gfc_match_omp_critical (void)
5295 {
5296 char n[GFC_MAX_SYMBOL_LEN+1];
5297 gfc_omp_clauses *c = NULL;
5298
5299 if (gfc_match (" ( %n )", n) != MATCH_YES)
5300 n[0] = '\0';
5301
5302 if (gfc_match_omp_clauses (&c, omp_mask (OMP_CLAUSE_HINT),
5303 /* first = */ n[0] == '\0') != MATCH_YES)
5304 return MATCH_ERROR;
5305
5306 new_st.op = EXEC_OMP_CRITICAL;
5307 new_st.ext.omp_clauses = c;
5308 if (n[0])
5309 c->critical_name = xstrdup (n);
5310 return MATCH_YES;
5311 }
5312
5313
5314 match
5315 gfc_match_omp_end_critical (void)
5316 {
5317 char n[GFC_MAX_SYMBOL_LEN+1];
5318
5319 if (gfc_match (" ( %n )", n) != MATCH_YES)
5320 n[0] = '\0';
5321 if (gfc_match_omp_eos () != MATCH_YES)
5322 {
5323 gfc_error ("Unexpected junk after $OMP CRITICAL statement at %C");
5324 return MATCH_ERROR;
5325 }
5326
5327 new_st.op = EXEC_OMP_END_CRITICAL;
5328 new_st.ext.omp_name = n[0] ? xstrdup (n) : NULL;
5329 return MATCH_YES;
5330 }
5331
5332 /* depobj(depobj) depend(dep-type:loc)|destroy|update(dep-type)
5333 dep-type = in/out/inout/mutexinoutset/depobj/source/sink
5334 depend: !source, !sink
5335 update: !source, !sink, !depobj
5336 locator = exactly one list item .*/
5337 match
5338 gfc_match_omp_depobj (void)
5339 {
5340 gfc_omp_clauses *c = NULL;
5341 gfc_expr *depobj;
5342
5343 if (gfc_match (" ( %v ) ", &depobj) != MATCH_YES)
5344 {
5345 gfc_error ("Expected %<( depobj )%> at %C");
5346 return MATCH_ERROR;
5347 }
5348 if (gfc_match ("update ( ") == MATCH_YES)
5349 {
5350 c = gfc_get_omp_clauses ();
5351 if (gfc_match ("inoutset )") == MATCH_YES)
5352 c->depobj_update = OMP_DEPEND_INOUTSET;
5353 else if (gfc_match ("inout )") == MATCH_YES)
5354 c->depobj_update = OMP_DEPEND_INOUT;
5355 else if (gfc_match ("in )") == MATCH_YES)
5356 c->depobj_update = OMP_DEPEND_IN;
5357 else if (gfc_match ("out )") == MATCH_YES)
5358 c->depobj_update = OMP_DEPEND_OUT;
5359 else if (gfc_match ("mutexinoutset )") == MATCH_YES)
5360 c->depobj_update = OMP_DEPEND_MUTEXINOUTSET;
5361 else
5362 {
5363 gfc_error ("Expected IN, OUT, INOUT, INOUTSET or MUTEXINOUTSET "
5364 "followed by %<)%> at %C");
5365 goto error;
5366 }
5367 }
5368 else if (gfc_match ("destroy ") == MATCH_YES)
5369 {
5370 gfc_expr *destroyobj = NULL;
5371 c = gfc_get_omp_clauses ();
5372 c->destroy = true;
5373
5374 if (gfc_match (" ( %v ) ", &destroyobj) == MATCH_YES)
5375 {
5376 if (destroyobj->symtree != depobj->symtree)
5377 gfc_warning (OPT_Wopenmp, "The same depend object should be used as"
5378 " DEPOBJ argument at %L and as DESTROY argument at %L",
5379 &depobj->where, &destroyobj->where);
5380 gfc_free_expr (destroyobj);
5381 }
5382 }
5383 else if (gfc_match_omp_clauses (&c, omp_mask (OMP_CLAUSE_DEPEND), true, false)
5384 != MATCH_YES)
5385 goto error;
5386
5387 if (c->depobj_update == OMP_DEPEND_UNSET && !c->destroy)
5388 {
5389 if (!c->doacross_source && !c->lists[OMP_LIST_DEPEND])
5390 {
5391 gfc_error ("Expected DEPEND, UPDATE, or DESTROY clause at %C");
5392 goto error;
5393 }
5394 if (c->lists[OMP_LIST_DEPEND]->u.depend_doacross_op == OMP_DEPEND_DEPOBJ)
5395 {
5396 gfc_error ("DEPEND clause at %L of OMP DEPOBJ construct shall not "
5397 "have dependence-type DEPOBJ",
5398 c->lists[OMP_LIST_DEPEND]
5399 ? &c->lists[OMP_LIST_DEPEND]->where : &gfc_current_locus);
5400 goto error;
5401 }
5402 if (c->lists[OMP_LIST_DEPEND]->next)
5403 {
5404 gfc_error ("DEPEND clause at %L of OMP DEPOBJ construct shall have "
5405 "only a single locator",
5406 &c->lists[OMP_LIST_DEPEND]->next->where);
5407 goto error;
5408 }
5409 }
5410
5411 c->depobj = depobj;
5412 new_st.op = EXEC_OMP_DEPOBJ;
5413 new_st.ext.omp_clauses = c;
5414 return MATCH_YES;
5415
5416 error:
5417 gfc_free_expr (depobj);
5418 gfc_free_omp_clauses (c);
5419 return MATCH_ERROR;
5420 }
5421
5422 match
5423 gfc_match_omp_dispatch (void)
5424 {
5425 return match_omp (EXEC_OMP_DISPATCH, OMP_DISPATCH_CLAUSES);
5426 }
5427
5428 match
5429 gfc_match_omp_distribute (void)
5430 {
5431 return match_omp (EXEC_OMP_DISTRIBUTE, OMP_DISTRIBUTE_CLAUSES);
5432 }
5433
5434
5435 match
5436 gfc_match_omp_distribute_parallel_do (void)
5437 {
5438 return match_omp (EXEC_OMP_DISTRIBUTE_PARALLEL_DO,
5439 (OMP_DISTRIBUTE_CLAUSES | OMP_PARALLEL_CLAUSES
5440 | OMP_DO_CLAUSES)
5441 & ~(omp_mask (OMP_CLAUSE_ORDERED)
5442 | OMP_CLAUSE_LINEAR | OMP_CLAUSE_NOWAIT));
5443 }
5444
5445
5446 match
5447 gfc_match_omp_distribute_parallel_do_simd (void)
5448 {
5449 return match_omp (EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD,
5450 (OMP_DISTRIBUTE_CLAUSES | OMP_PARALLEL_CLAUSES
5451 | OMP_DO_CLAUSES | OMP_SIMD_CLAUSES)
5452 & ~(omp_mask (OMP_CLAUSE_ORDERED) | OMP_CLAUSE_NOWAIT));
5453 }
5454
5455
5456 match
5457 gfc_match_omp_distribute_simd (void)
5458 {
5459 return match_omp (EXEC_OMP_DISTRIBUTE_SIMD,
5460 OMP_DISTRIBUTE_CLAUSES | OMP_SIMD_CLAUSES);
5461 }
5462
5463
5464 match
5465 gfc_match_omp_do (void)
5466 {
5467 return match_omp (EXEC_OMP_DO, OMP_DO_CLAUSES);
5468 }
5469
5470
5471 match
5472 gfc_match_omp_do_simd (void)
5473 {
5474 return match_omp (EXEC_OMP_DO_SIMD, OMP_DO_CLAUSES | OMP_SIMD_CLAUSES);
5475 }
5476
5477
5478 match
5479 gfc_match_omp_loop (void)
5480 {
5481 return match_omp (EXEC_OMP_LOOP, OMP_LOOP_CLAUSES);
5482 }
5483
5484
5485 match
5486 gfc_match_omp_teams_loop (void)
5487 {
5488 return match_omp (EXEC_OMP_TEAMS_LOOP, OMP_TEAMS_CLAUSES | OMP_LOOP_CLAUSES);
5489 }
5490
5491
5492 match
5493 gfc_match_omp_target_teams_loop (void)
5494 {
5495 return match_omp (EXEC_OMP_TARGET_TEAMS_LOOP,
5496 OMP_TARGET_CLAUSES | OMP_TEAMS_CLAUSES | OMP_LOOP_CLAUSES);
5497 }
5498
5499
5500 match
5501 gfc_match_omp_parallel_loop (void)
5502 {
5503 return match_omp (EXEC_OMP_PARALLEL_LOOP,
5504 OMP_PARALLEL_CLAUSES | OMP_LOOP_CLAUSES);
5505 }
5506
5507
5508 match
5509 gfc_match_omp_target_parallel_loop (void)
5510 {
5511 return match_omp (EXEC_OMP_TARGET_PARALLEL_LOOP,
5512 (OMP_TARGET_CLAUSES | OMP_PARALLEL_CLAUSES
5513 | OMP_LOOP_CLAUSES));
5514 }
5515
5516
5517 match
5518 gfc_match_omp_error (void)
5519 {
5520 locus loc = gfc_current_locus;
5521 match m = match_omp (EXEC_OMP_ERROR, OMP_ERROR_CLAUSES);
5522 if (m != MATCH_YES)
5523 return m;
5524
5525 gfc_omp_clauses *c = new_st.ext.omp_clauses;
5526 if (c->severity == OMP_SEVERITY_UNSET)
5527 c->severity = OMP_SEVERITY_FATAL;
5528 if (new_st.ext.omp_clauses->at == OMP_AT_EXECUTION)
5529 return MATCH_YES;
5530 if (c->message
5531 && (!gfc_resolve_expr (c->message)
5532 || c->message->ts.type != BT_CHARACTER
5533 || c->message->ts.kind != gfc_default_character_kind
5534 || c->message->rank != 0))
5535 {
5536 gfc_error ("MESSAGE clause at %L requires a scalar default-kind "
5537 "CHARACTER expression",
5538 &new_st.ext.omp_clauses->message->where);
5539 return MATCH_ERROR;
5540 }
5541 if (c->message && !gfc_is_constant_expr (c->message))
5542 {
5543 gfc_error ("Constant character expression required in MESSAGE clause "
5544 "at %L", &new_st.ext.omp_clauses->message->where);
5545 return MATCH_ERROR;
5546 }
5547 if (c->message)
5548 {
5549 const char *msg = G_("$OMP ERROR encountered at %L: %s");
5550 gcc_assert (c->message->expr_type == EXPR_CONSTANT);
5551 gfc_charlen_t slen = c->message->value.character.length;
5552 int i = gfc_validate_kind (BT_CHARACTER, gfc_default_character_kind,
5553 false);
5554 size_t size = slen * gfc_character_kinds[i].bit_size / 8;
5555 unsigned char *s = XCNEWVAR (unsigned char, size + 1);
5556 gfc_encode_character (gfc_default_character_kind, slen,
5557 c->message->value.character.string,
5558 (unsigned char *) s, size);
5559 s[size] = '\0';
5560 if (c->severity == OMP_SEVERITY_WARNING)
5561 gfc_warning_now (0, msg, &loc, s);
5562 else
5563 gfc_error_now (msg, &loc, s);
5564 free (s);
5565 }
5566 else
5567 {
5568 const char *msg = G_("$OMP ERROR encountered at %L");
5569 if (c->severity == OMP_SEVERITY_WARNING)
5570 gfc_warning_now (0, msg, &loc);
5571 else
5572 gfc_error_now (msg, &loc);
5573 }
5574 return MATCH_YES;
5575 }
5576
5577 match
5578 gfc_match_omp_flush (void)
5579 {
5580 gfc_omp_namelist *list = NULL;
5581 gfc_omp_clauses *c = NULL;
5582 gfc_gobble_whitespace ();
5583 enum gfc_omp_memorder mo = OMP_MEMORDER_UNSET;
5584 if (gfc_match_omp_eos () == MATCH_NO && gfc_peek_ascii_char () != '(')
5585 {
5586 if (gfc_match ("seq_cst") == MATCH_YES)
5587 mo = OMP_MEMORDER_SEQ_CST;
5588 else if (gfc_match ("acq_rel") == MATCH_YES)
5589 mo = OMP_MEMORDER_ACQ_REL;
5590 else if (gfc_match ("release") == MATCH_YES)
5591 mo = OMP_MEMORDER_RELEASE;
5592 else if (gfc_match ("acquire") == MATCH_YES)
5593 mo = OMP_MEMORDER_ACQUIRE;
5594 else
5595 {
5596 gfc_error ("Expected SEQ_CST, AQC_REL, RELEASE, or ACQUIRE at %C");
5597 return MATCH_ERROR;
5598 }
5599 c = gfc_get_omp_clauses ();
5600 c->memorder = mo;
5601 }
5602 gfc_match_omp_variable_list (" (", &list, true);
5603 if (list && mo != OMP_MEMORDER_UNSET)
5604 {
5605 gfc_error ("List specified together with memory order clause in FLUSH "
5606 "directive at %C");
5607 gfc_free_omp_namelist (list, false, false, false, false);
5608 gfc_free_omp_clauses (c);
5609 return MATCH_ERROR;
5610 }
5611 if (gfc_match_omp_eos () != MATCH_YES)
5612 {
5613 gfc_error ("Unexpected junk after $OMP FLUSH statement at %C");
5614 gfc_free_omp_namelist (list, false, false, false, false);
5615 gfc_free_omp_clauses (c);
5616 return MATCH_ERROR;
5617 }
5618 new_st.op = EXEC_OMP_FLUSH;
5619 new_st.ext.omp_namelist = list;
5620 new_st.ext.omp_clauses = c;
5621 return MATCH_YES;
5622 }
5623
5624
5625 match
5626 gfc_match_omp_declare_simd (void)
5627 {
5628 locus where = gfc_current_locus;
5629 gfc_symbol *proc_name;
5630 gfc_omp_clauses *c;
5631 gfc_omp_declare_simd *ods;
5632 bool needs_space = false;
5633
5634 switch (gfc_match (" ( "))
5635 {
5636 case MATCH_YES:
5637 if (gfc_match_symbol (&proc_name, /* host assoc = */ true) != MATCH_YES
5638 || gfc_match (" ) ") != MATCH_YES)
5639 return MATCH_ERROR;
5640 break;
5641 case MATCH_NO: proc_name = NULL; needs_space = true; break;
5642 case MATCH_ERROR: return MATCH_ERROR;
5643 }
5644
5645 if (gfc_match_omp_clauses (&c, OMP_DECLARE_SIMD_CLAUSES, true,
5646 needs_space) != MATCH_YES)
5647 return MATCH_ERROR;
5648
5649 if (gfc_current_ns->is_block_data)
5650 {
5651 gfc_free_omp_clauses (c);
5652 return MATCH_YES;
5653 }
5654
5655 ods = gfc_get_omp_declare_simd ();
5656 ods->where = where;
5657 ods->proc_name = proc_name;
5658 ods->clauses = c;
5659 ods->next = gfc_current_ns->omp_declare_simd;
5660 gfc_current_ns->omp_declare_simd = ods;
5661 return MATCH_YES;
5662 }
5663
5664
5665 static bool
5666 match_udr_expr (gfc_symtree *omp_sym1, gfc_symtree *omp_sym2)
5667 {
5668 match m;
5669 locus old_loc = gfc_current_locus;
5670 char sname[GFC_MAX_SYMBOL_LEN + 1];
5671 gfc_symbol *sym;
5672 gfc_namespace *ns = gfc_current_ns;
5673 gfc_expr *lvalue = NULL, *rvalue = NULL;
5674 gfc_symtree *st;
5675 gfc_actual_arglist *arglist;
5676
5677 m = gfc_match (" %v =", &lvalue);
5678 if (m != MATCH_YES)
5679 gfc_current_locus = old_loc;
5680 else
5681 {
5682 m = gfc_match (" %e )", &rvalue);
5683 if (m == MATCH_YES)
5684 {
5685 ns->code = gfc_get_code (EXEC_ASSIGN);
5686 ns->code->expr1 = lvalue;
5687 ns->code->expr2 = rvalue;
5688 ns->code->loc = old_loc;
5689 return true;
5690 }
5691
5692 gfc_current_locus = old_loc;
5693 gfc_free_expr (lvalue);
5694 }
5695
5696 m = gfc_match (" %n", sname);
5697 if (m != MATCH_YES)
5698 return false;
5699
5700 if (strcmp (sname, omp_sym1->name) == 0
5701 || strcmp (sname, omp_sym2->name) == 0)
5702 return false;
5703
5704 gfc_current_ns = ns->parent;
5705 if (gfc_get_ha_sym_tree (sname, &st))
5706 return false;
5707
5708 sym = st->n.sym;
5709 if (sym->attr.flavor != FL_PROCEDURE
5710 && sym->attr.flavor != FL_UNKNOWN)
5711 return false;
5712
5713 if (!sym->attr.generic
5714 && !sym->attr.subroutine
5715 && !sym->attr.function)
5716 {
5717 if (!(sym->attr.external && !sym->attr.referenced))
5718 {
5719 /* ...create a symbol in this scope... */
5720 if (sym->ns != gfc_current_ns
5721 && gfc_get_sym_tree (sname, NULL, &st, false) == 1)
5722 return false;
5723
5724 if (sym != st->n.sym)
5725 sym = st->n.sym;
5726 }
5727
5728 /* ...and then to try to make the symbol into a subroutine. */
5729 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
5730 return false;
5731 }
5732
5733 gfc_set_sym_referenced (sym);
5734 gfc_gobble_whitespace ();
5735 if (gfc_peek_ascii_char () != '(')
5736 return false;
5737
5738 gfc_current_ns = ns;
5739 m = gfc_match_actual_arglist (1, &arglist);
5740 if (m != MATCH_YES)
5741 return false;
5742
5743 if (gfc_match_char (')') != MATCH_YES)
5744 return false;
5745
5746 ns->code = gfc_get_code (EXEC_CALL);
5747 ns->code->symtree = st;
5748 ns->code->ext.actual = arglist;
5749 ns->code->loc = old_loc;
5750 return true;
5751 }
5752
5753 static bool
5754 gfc_omp_udr_predef (gfc_omp_reduction_op rop, const char *name,
5755 gfc_typespec *ts, const char **n)
5756 {
5757 if (!gfc_numeric_ts (ts) && ts->type != BT_LOGICAL)
5758 return false;
5759
5760 switch (rop)
5761 {
5762 case OMP_REDUCTION_PLUS:
5763 case OMP_REDUCTION_MINUS:
5764 case OMP_REDUCTION_TIMES:
5765 return ts->type != BT_LOGICAL;
5766 case OMP_REDUCTION_AND:
5767 case OMP_REDUCTION_OR:
5768 case OMP_REDUCTION_EQV:
5769 case OMP_REDUCTION_NEQV:
5770 return ts->type == BT_LOGICAL;
5771 case OMP_REDUCTION_USER:
5772 if (name[0] != '.' && (ts->type == BT_INTEGER || ts->type == BT_REAL))
5773 {
5774 gfc_symbol *sym;
5775
5776 gfc_find_symbol (name, NULL, 1, &sym);
5777 if (sym != NULL)
5778 {
5779 if (sym->attr.intrinsic)
5780 *n = sym->name;
5781 else if ((sym->attr.flavor != FL_UNKNOWN
5782 && sym->attr.flavor != FL_PROCEDURE)
5783 || sym->attr.external
5784 || sym->attr.generic
5785 || sym->attr.entry
5786 || sym->attr.result
5787 || sym->attr.dummy
5788 || sym->attr.subroutine
5789 || sym->attr.pointer
5790 || sym->attr.target
5791 || sym->attr.cray_pointer
5792 || sym->attr.cray_pointee
5793 || (sym->attr.proc != PROC_UNKNOWN
5794 && sym->attr.proc != PROC_INTRINSIC)
5795 || sym->attr.if_source != IFSRC_UNKNOWN
5796 || sym == sym->ns->proc_name)
5797 *n = NULL;
5798 else
5799 *n = sym->name;
5800 }
5801 else
5802 *n = name;
5803 if (*n
5804 && (strcmp (*n, "max") == 0 || strcmp (*n, "min") == 0))
5805 return true;
5806 else if (*n
5807 && ts->type == BT_INTEGER
5808 && (strcmp (*n, "iand") == 0
5809 || strcmp (*n, "ior") == 0
5810 || strcmp (*n, "ieor") == 0))
5811 return true;
5812 }
5813 break;
5814 default:
5815 break;
5816 }
5817 return false;
5818 }
5819
5820 gfc_omp_udr *
5821 gfc_omp_udr_find (gfc_symtree *st, gfc_typespec *ts)
5822 {
5823 gfc_omp_udr *omp_udr;
5824
5825 if (st == NULL)
5826 return NULL;
5827
5828 for (omp_udr = st->n.omp_udr; omp_udr; omp_udr = omp_udr->next)
5829 if (omp_udr->ts.type == ts->type
5830 || ((omp_udr->ts.type == BT_DERIVED || omp_udr->ts.type == BT_CLASS)
5831 && (ts->type == BT_DERIVED || ts->type == BT_CLASS)))
5832 {
5833 if (omp_udr->ts.type == BT_DERIVED || omp_udr->ts.type == BT_CLASS)
5834 {
5835 if (strcmp (omp_udr->ts.u.derived->name, ts->u.derived->name) == 0)
5836 return omp_udr;
5837 }
5838 else if (omp_udr->ts.kind == ts->kind)
5839 {
5840 if (omp_udr->ts.type == BT_CHARACTER)
5841 {
5842 if (omp_udr->ts.u.cl->length == NULL
5843 || ts->u.cl->length == NULL)
5844 return omp_udr;
5845 if (omp_udr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
5846 return omp_udr;
5847 if (ts->u.cl->length->expr_type != EXPR_CONSTANT)
5848 return omp_udr;
5849 if (omp_udr->ts.u.cl->length->ts.type != BT_INTEGER)
5850 return omp_udr;
5851 if (ts->u.cl->length->ts.type != BT_INTEGER)
5852 return omp_udr;
5853 if (gfc_compare_expr (omp_udr->ts.u.cl->length,
5854 ts->u.cl->length, INTRINSIC_EQ) != 0)
5855 continue;
5856 }
5857 return omp_udr;
5858 }
5859 }
5860 return NULL;
5861 }
5862
5863 match
5864 gfc_match_omp_declare_reduction (void)
5865 {
5866 match m;
5867 gfc_intrinsic_op op;
5868 char name[GFC_MAX_SYMBOL_LEN + 3];
5869 auto_vec<gfc_typespec, 5> tss;
5870 gfc_typespec ts;
5871 unsigned int i;
5872 gfc_symtree *st;
5873 locus where = gfc_current_locus;
5874 locus end_loc = gfc_current_locus;
5875 bool end_loc_set = false;
5876 gfc_omp_reduction_op rop = OMP_REDUCTION_NONE;
5877
5878 if (gfc_match_char ('(') != MATCH_YES)
5879 return MATCH_ERROR;
5880
5881 m = gfc_match (" %o : ", &op);
5882 if (m == MATCH_ERROR)
5883 return MATCH_ERROR;
5884 if (m == MATCH_YES)
5885 {
5886 snprintf (name, sizeof name, "operator %s", gfc_op2string (op));
5887 rop = (gfc_omp_reduction_op) op;
5888 }
5889 else
5890 {
5891 m = gfc_match_defined_op_name (name + 1, 1);
5892 if (m == MATCH_ERROR)
5893 return MATCH_ERROR;
5894 if (m == MATCH_YES)
5895 {
5896 name[0] = '.';
5897 strcat (name, ".");
5898 if (gfc_match (" : ") != MATCH_YES)
5899 return MATCH_ERROR;
5900 }
5901 else
5902 {
5903 if (gfc_match (" %n : ", name) != MATCH_YES)
5904 return MATCH_ERROR;
5905 }
5906 rop = OMP_REDUCTION_USER;
5907 }
5908
5909 m = gfc_match_type_spec (&ts);
5910 if (m != MATCH_YES)
5911 return MATCH_ERROR;
5912 /* Treat len=: the same as len=*. */
5913 if (ts.type == BT_CHARACTER)
5914 ts.deferred = false;
5915 tss.safe_push (ts);
5916
5917 while (gfc_match_char (',') == MATCH_YES)
5918 {
5919 m = gfc_match_type_spec (&ts);
5920 if (m != MATCH_YES)
5921 return MATCH_ERROR;
5922 tss.safe_push (ts);
5923 }
5924 if (gfc_match_char (':') != MATCH_YES)
5925 return MATCH_ERROR;
5926
5927 st = gfc_find_symtree (gfc_current_ns->omp_udr_root, name);
5928 for (i = 0; i < tss.length (); i++)
5929 {
5930 gfc_symtree *omp_out, *omp_in;
5931 gfc_symtree *omp_priv = NULL, *omp_orig = NULL;
5932 gfc_namespace *combiner_ns, *initializer_ns = NULL;
5933 gfc_omp_udr *prev_udr, *omp_udr;
5934 const char *predef_name = NULL;
5935
5936 omp_udr = gfc_get_omp_udr ();
5937 omp_udr->name = gfc_get_string ("%s", name);
5938 omp_udr->rop = rop;
5939 omp_udr->ts = tss[i];
5940 omp_udr->where = where;
5941
5942 gfc_current_ns = combiner_ns = gfc_get_namespace (gfc_current_ns, 1);
5943 combiner_ns->proc_name = combiner_ns->parent->proc_name;
5944
5945 gfc_get_sym_tree ("omp_out", combiner_ns, &omp_out, false);
5946 gfc_get_sym_tree ("omp_in", combiner_ns, &omp_in, false);
5947 combiner_ns->omp_udr_ns = 1;
5948 omp_out->n.sym->ts = tss[i];
5949 omp_in->n.sym->ts = tss[i];
5950 omp_out->n.sym->attr.omp_udr_artificial_var = 1;
5951 omp_in->n.sym->attr.omp_udr_artificial_var = 1;
5952 omp_out->n.sym->attr.flavor = FL_VARIABLE;
5953 omp_in->n.sym->attr.flavor = FL_VARIABLE;
5954 gfc_commit_symbols ();
5955 omp_udr->combiner_ns = combiner_ns;
5956 omp_udr->omp_out = omp_out->n.sym;
5957 omp_udr->omp_in = omp_in->n.sym;
5958
5959 locus old_loc = gfc_current_locus;
5960
5961 if (!match_udr_expr (omp_out, omp_in))
5962 {
5963 syntax:
5964 gfc_current_locus = old_loc;
5965 gfc_current_ns = combiner_ns->parent;
5966 gfc_undo_symbols ();
5967 gfc_free_omp_udr (omp_udr);
5968 return MATCH_ERROR;
5969 }
5970
5971 if (gfc_match (" initializer ( ") == MATCH_YES)
5972 {
5973 gfc_current_ns = combiner_ns->parent;
5974 initializer_ns = gfc_get_namespace (gfc_current_ns, 1);
5975 gfc_current_ns = initializer_ns;
5976 initializer_ns->proc_name = initializer_ns->parent->proc_name;
5977
5978 gfc_get_sym_tree ("omp_priv", initializer_ns, &omp_priv, false);
5979 gfc_get_sym_tree ("omp_orig", initializer_ns, &omp_orig, false);
5980 initializer_ns->omp_udr_ns = 1;
5981 omp_priv->n.sym->ts = tss[i];
5982 omp_orig->n.sym->ts = tss[i];
5983 omp_priv->n.sym->attr.omp_udr_artificial_var = 1;
5984 omp_orig->n.sym->attr.omp_udr_artificial_var = 1;
5985 omp_priv->n.sym->attr.flavor = FL_VARIABLE;
5986 omp_orig->n.sym->attr.flavor = FL_VARIABLE;
5987 gfc_commit_symbols ();
5988 omp_udr->initializer_ns = initializer_ns;
5989 omp_udr->omp_priv = omp_priv->n.sym;
5990 omp_udr->omp_orig = omp_orig->n.sym;
5991
5992 if (!match_udr_expr (omp_priv, omp_orig))
5993 goto syntax;
5994 }
5995
5996 gfc_current_ns = combiner_ns->parent;
5997 if (!end_loc_set)
5998 {
5999 end_loc_set = true;
6000 end_loc = gfc_current_locus;
6001 }
6002 gfc_current_locus = old_loc;
6003
6004 prev_udr = gfc_omp_udr_find (st, &tss[i]);
6005 if (gfc_omp_udr_predef (rop, name, &tss[i], &predef_name)
6006 /* Don't error on !$omp declare reduction (min : integer : ...)
6007 just yet, there could be integer :: min afterwards,
6008 making it valid. When the UDR is resolved, we'll get
6009 to it again. */
6010 && (rop != OMP_REDUCTION_USER || name[0] == '.'))
6011 {
6012 if (predef_name)
6013 gfc_error_now ("Redefinition of predefined %s "
6014 "!$OMP DECLARE REDUCTION at %L",
6015 predef_name, &where);
6016 else
6017 gfc_error_now ("Redefinition of predefined "
6018 "!$OMP DECLARE REDUCTION at %L", &where);
6019 }
6020 else if (prev_udr)
6021 {
6022 gfc_error_now ("Redefinition of !$OMP DECLARE REDUCTION at %L",
6023 &where);
6024 gfc_error_now ("Previous !$OMP DECLARE REDUCTION at %L",
6025 &prev_udr->where);
6026 }
6027 else if (st)
6028 {
6029 omp_udr->next = st->n.omp_udr;
6030 st->n.omp_udr = omp_udr;
6031 }
6032 else
6033 {
6034 st = gfc_new_symtree (&gfc_current_ns->omp_udr_root, name);
6035 st->n.omp_udr = omp_udr;
6036 }
6037 }
6038
6039 if (end_loc_set)
6040 {
6041 gfc_current_locus = end_loc;
6042 if (gfc_match_omp_eos () != MATCH_YES)
6043 {
6044 gfc_error ("Unexpected junk after !$OMP DECLARE REDUCTION at %C");
6045 gfc_current_locus = where;
6046 return MATCH_ERROR;
6047 }
6048
6049 return MATCH_YES;
6050 }
6051 gfc_clear_error ();
6052 return MATCH_ERROR;
6053 }
6054
6055
6056 match
6057 gfc_match_omp_declare_target (void)
6058 {
6059 locus old_loc;
6060 match m;
6061 gfc_omp_clauses *c = NULL;
6062 int list;
6063 gfc_omp_namelist *n;
6064 gfc_symbol *s;
6065
6066 old_loc = gfc_current_locus;
6067
6068 if (gfc_current_ns->proc_name
6069 && gfc_match_omp_eos () == MATCH_YES)
6070 {
6071 if (!gfc_add_omp_declare_target (&gfc_current_ns->proc_name->attr,
6072 gfc_current_ns->proc_name->name,
6073 &old_loc))
6074 goto cleanup;
6075 return MATCH_YES;
6076 }
6077
6078 if (gfc_current_ns->proc_name
6079 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY)
6080 {
6081 gfc_error ("Only the !$OMP DECLARE TARGET form without "
6082 "clauses is allowed in interface block at %C");
6083 goto cleanup;
6084 }
6085
6086 m = gfc_match (" (");
6087 if (m == MATCH_YES)
6088 {
6089 c = gfc_get_omp_clauses ();
6090 gfc_current_locus = old_loc;
6091 m = gfc_match_omp_to_link (" (", &c->lists[OMP_LIST_ENTER]);
6092 if (m != MATCH_YES)
6093 goto syntax;
6094 if (gfc_match_omp_eos () != MATCH_YES)
6095 {
6096 gfc_error ("Unexpected junk after !$OMP DECLARE TARGET at %C");
6097 goto cleanup;
6098 }
6099 }
6100 else if (gfc_match_omp_clauses (&c, OMP_DECLARE_TARGET_CLAUSES) != MATCH_YES)
6101 return MATCH_ERROR;
6102
6103 gfc_buffer_error (false);
6104
6105 static const int to_enter_link_lists[]
6106 = { OMP_LIST_TO, OMP_LIST_ENTER, OMP_LIST_LINK };
6107 for (size_t listn = 0; listn < ARRAY_SIZE (to_enter_link_lists)
6108 && (list = to_enter_link_lists[listn], true); ++listn)
6109 for (n = c->lists[list]; n; n = n->next)
6110 if (n->sym)
6111 n->sym->mark = 0;
6112 else if (n->u.common->head)
6113 n->u.common->head->mark = 0;
6114
6115 for (size_t listn = 0; listn < ARRAY_SIZE (to_enter_link_lists)
6116 && (list = to_enter_link_lists[listn], true); ++listn)
6117 for (n = c->lists[list]; n; n = n->next)
6118 if (n->sym)
6119 {
6120 if (n->sym->attr.in_common)
6121 gfc_error_now ("OMP DECLARE TARGET variable at %L is an "
6122 "element of a COMMON block", &n->where);
6123 else if (n->sym->mark)
6124 gfc_error_now ("Variable at %L mentioned multiple times in "
6125 "clauses of the same OMP DECLARE TARGET directive",
6126 &n->where);
6127 else if (n->sym->attr.omp_declare_target
6128 && n->sym->attr.omp_declare_target_link
6129 && list != OMP_LIST_LINK)
6130 gfc_error_now ("OMP DECLARE TARGET variable at %L previously "
6131 "mentioned in LINK clause and later in %s clause",
6132 &n->where, list == OMP_LIST_TO ? "TO" : "ENTER");
6133 else if (n->sym->attr.omp_declare_target
6134 && !n->sym->attr.omp_declare_target_link
6135 && list == OMP_LIST_LINK)
6136 gfc_error_now ("OMP DECLARE TARGET variable at %L previously "
6137 "mentioned in TO or ENTER clause and later in "
6138 "LINK clause", &n->where);
6139 else if (gfc_add_omp_declare_target (&n->sym->attr, n->sym->name,
6140 &n->sym->declared_at))
6141 {
6142 if (list == OMP_LIST_LINK)
6143 gfc_add_omp_declare_target_link (&n->sym->attr, n->sym->name,
6144 &n->sym->declared_at);
6145 }
6146 if (c->device_type != OMP_DEVICE_TYPE_UNSET)
6147 {
6148 if (n->sym->attr.omp_device_type != OMP_DEVICE_TYPE_UNSET
6149 && n->sym->attr.omp_device_type != c->device_type)
6150 gfc_error_now ("List item %qs at %L set in previous OMP DECLARE "
6151 "TARGET directive to a different DEVICE_TYPE",
6152 n->sym->name, &n->where);
6153 n->sym->attr.omp_device_type = c->device_type;
6154 }
6155 if (c->indirect)
6156 {
6157 if (n->sym->attr.omp_device_type != OMP_DEVICE_TYPE_UNSET
6158 && n->sym->attr.omp_device_type != OMP_DEVICE_TYPE_ANY)
6159 gfc_error_now ("DEVICE_TYPE must be ANY when used with "
6160 "INDIRECT at %L", &n->where);
6161 n->sym->attr.omp_declare_target_indirect = c->indirect;
6162 }
6163
6164 n->sym->mark = 1;
6165 }
6166 else if (n->u.common->omp_declare_target
6167 && n->u.common->omp_declare_target_link
6168 && list != OMP_LIST_LINK)
6169 gfc_error_now ("OMP DECLARE TARGET COMMON at %L previously "
6170 "mentioned in LINK clause and later in %s clause",
6171 &n->where, list == OMP_LIST_TO ? "TO" : "ENTER");
6172 else if (n->u.common->omp_declare_target
6173 && !n->u.common->omp_declare_target_link
6174 && list == OMP_LIST_LINK)
6175 gfc_error_now ("OMP DECLARE TARGET COMMON at %L previously "
6176 "mentioned in TO or ENTER clause and later in "
6177 "LINK clause", &n->where);
6178 else if (n->u.common->head && n->u.common->head->mark)
6179 gfc_error_now ("COMMON at %L mentioned multiple times in "
6180 "clauses of the same OMP DECLARE TARGET directive",
6181 &n->where);
6182 else
6183 {
6184 n->u.common->omp_declare_target = 1;
6185 n->u.common->omp_declare_target_link = (list == OMP_LIST_LINK);
6186 if (n->u.common->omp_device_type != OMP_DEVICE_TYPE_UNSET
6187 && n->u.common->omp_device_type != c->device_type)
6188 gfc_error_now ("COMMON at %L set in previous OMP DECLARE "
6189 "TARGET directive to a different DEVICE_TYPE",
6190 &n->where);
6191 n->u.common->omp_device_type = c->device_type;
6192
6193 for (s = n->u.common->head; s; s = s->common_next)
6194 {
6195 s->mark = 1;
6196 if (gfc_add_omp_declare_target (&s->attr, s->name,
6197 &s->declared_at))
6198 {
6199 if (list == OMP_LIST_LINK)
6200 gfc_add_omp_declare_target_link (&s->attr, s->name,
6201 &s->declared_at);
6202 }
6203 if (s->attr.omp_device_type != OMP_DEVICE_TYPE_UNSET
6204 && s->attr.omp_device_type != c->device_type)
6205 gfc_error_now ("List item %qs at %L set in previous OMP DECLARE"
6206 " TARGET directive to a different DEVICE_TYPE",
6207 s->name, &n->where);
6208 s->attr.omp_device_type = c->device_type;
6209
6210 if (c->indirect
6211 && s->attr.omp_device_type != OMP_DEVICE_TYPE_UNSET
6212 && s->attr.omp_device_type != OMP_DEVICE_TYPE_ANY)
6213 gfc_error_now ("DEVICE_TYPE must be ANY when used with "
6214 "INDIRECT at %L", &n->where);
6215 s->attr.omp_declare_target_indirect = c->indirect;
6216 }
6217 }
6218 if ((c->device_type || c->indirect)
6219 && !c->lists[OMP_LIST_ENTER]
6220 && !c->lists[OMP_LIST_TO]
6221 && !c->lists[OMP_LIST_LINK])
6222 gfc_warning_now (OPT_Wopenmp,
6223 "OMP DECLARE TARGET directive at %L with only "
6224 "DEVICE_TYPE or INDIRECT clauses is ignored",
6225 &old_loc);
6226
6227 gfc_buffer_error (true);
6228
6229 if (c)
6230 gfc_free_omp_clauses (c);
6231 return MATCH_YES;
6232
6233 syntax:
6234 gfc_error ("Syntax error in !$OMP DECLARE TARGET list at %C");
6235
6236 cleanup:
6237 gfc_current_locus = old_loc;
6238 if (c)
6239 gfc_free_omp_clauses (c);
6240 return MATCH_ERROR;
6241 }
6242
6243 /* Skip over and ignore trait-property-extensions.
6244
6245 trait-property-extension :
6246 trait-property-name
6247 identifier (trait-property-extension[, trait-property-extension[, ...]])
6248 constant integer expression
6249 */
6250
6251 static match gfc_ignore_trait_property_extension_list (void);
6252
6253 static match
6254 gfc_ignore_trait_property_extension (void)
6255 {
6256 char buf[GFC_MAX_SYMBOL_LEN + 1];
6257 gfc_expr *expr;
6258
6259 /* Identifier form of trait-property name, possibly followed by
6260 a list of (recursive) trait-property-extensions. */
6261 if (gfc_match_name (buf) == MATCH_YES)
6262 {
6263 if (gfc_match (" (") == MATCH_YES)
6264 return gfc_ignore_trait_property_extension_list ();
6265 return MATCH_YES;
6266 }
6267
6268 /* Literal constant. */
6269 if (gfc_match_literal_constant (&expr, 0) == MATCH_YES)
6270 return MATCH_YES;
6271
6272 /* FIXME: constant integer expressions. */
6273 gfc_error ("Expected trait-property-extension at %C");
6274 return MATCH_ERROR;
6275 }
6276
6277 static match
6278 gfc_ignore_trait_property_extension_list (void)
6279 {
6280 while (1)
6281 {
6282 if (gfc_ignore_trait_property_extension () != MATCH_YES)
6283 return MATCH_ERROR;
6284 if (gfc_match (" ,") == MATCH_YES)
6285 continue;
6286 if (gfc_match (" )") == MATCH_YES)
6287 return MATCH_YES;
6288 gfc_error ("expected %<)%> at %C");
6289 return MATCH_ERROR;
6290 }
6291 }
6292
6293
6294 match
6295 gfc_match_omp_interop (void)
6296 {
6297 return match_omp (EXEC_OMP_INTEROP, OMP_INTEROP_CLAUSES);
6298 }
6299
6300
6301 /* OpenMP 5.0:
6302
6303 trait-selector:
6304 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
6305
6306 trait-score:
6307 score(score-expression) */
6308
6309 match
6310 gfc_match_omp_context_selector (gfc_omp_set_selector *oss,
6311 bool metadirective_p)
6312 {
6313 do
6314 {
6315 char selector[GFC_MAX_SYMBOL_LEN + 1];
6316
6317 if (gfc_match_name (selector) != MATCH_YES)
6318 {
6319 gfc_error ("expected trait selector name at %C");
6320 return MATCH_ERROR;
6321 }
6322
6323 gfc_omp_selector *os = gfc_get_omp_selector ();
6324 if (oss->code == OMP_TRAIT_SET_CONSTRUCT
6325 && !strcmp (selector, "do"))
6326 os->code = OMP_TRAIT_CONSTRUCT_FOR;
6327 else if (oss->code == OMP_TRAIT_SET_CONSTRUCT
6328 && !strcmp (selector, "for"))
6329 os->code = OMP_TRAIT_INVALID;
6330 else
6331 os->code = omp_lookup_ts_code (oss->code, selector);
6332 os->next = oss->trait_selectors;
6333 oss->trait_selectors = os;
6334
6335 if (os->code == OMP_TRAIT_INVALID)
6336 {
6337 gfc_warning (OPT_Wopenmp,
6338 "unknown selector %qs for context selector set %qs "
6339 "at %C",
6340 selector, omp_tss_map[oss->code]);
6341 if (gfc_match (" (") == MATCH_YES
6342 && gfc_ignore_trait_property_extension_list () != MATCH_YES)
6343 return MATCH_ERROR;
6344 if (gfc_match (" ,") == MATCH_YES)
6345 continue;
6346 break;
6347 }
6348
6349 enum omp_tp_type property_kind = omp_ts_map[os->code].tp_type;
6350 bool allow_score = omp_ts_map[os->code].allow_score;
6351
6352 if (gfc_match (" (") == MATCH_YES)
6353 {
6354 if (property_kind == OMP_TRAIT_PROPERTY_NONE)
6355 {
6356 gfc_error ("selector %qs does not accept any properties at %C",
6357 selector);
6358 return MATCH_ERROR;
6359 }
6360
6361 if (gfc_match (" score") == MATCH_YES)
6362 {
6363 if (!allow_score)
6364 {
6365 gfc_error ("%<score%> cannot be specified in traits "
6366 "in the %qs trait-selector-set at %C",
6367 omp_tss_map[oss->code]);
6368 return MATCH_ERROR;
6369 }
6370 if (gfc_match (" (") != MATCH_YES)
6371 {
6372 gfc_error ("expected %<(%> at %C");
6373 return MATCH_ERROR;
6374 }
6375 if (gfc_match_expr (&os->score) != MATCH_YES
6376 || !gfc_resolve_expr (os->score)
6377 || os->score->ts.type != BT_INTEGER
6378 || os->score->rank != 0)
6379 {
6380 gfc_error ("%<score%> argument must be constant integer "
6381 "expression at %C");
6382 return MATCH_ERROR;
6383 }
6384
6385 if (os->score->expr_type == EXPR_CONSTANT
6386 && mpz_sgn (os->score->value.integer) < 0)
6387 {
6388 gfc_error ("%<score%> argument must be non-negative at %C");
6389 return MATCH_ERROR;
6390 }
6391
6392 if (gfc_match (" )") != MATCH_YES)
6393 {
6394 gfc_error ("expected %<)%> at %C");
6395 return MATCH_ERROR;
6396 }
6397
6398 if (gfc_match (" :") != MATCH_YES)
6399 {
6400 gfc_error ("expected : at %C");
6401 return MATCH_ERROR;
6402 }
6403 }
6404
6405 gfc_omp_trait_property *otp = gfc_get_omp_trait_property ();
6406 otp->property_kind = property_kind;
6407 otp->next = os->properties;
6408 os->properties = otp;
6409
6410 switch (property_kind)
6411 {
6412 case OMP_TRAIT_PROPERTY_ID:
6413 {
6414 char buf[GFC_MAX_SYMBOL_LEN + 1];
6415 if (gfc_match_name (buf) == MATCH_YES)
6416 {
6417 otp->name = XNEWVEC (char, strlen (buf) + 1);
6418 strcpy (otp->name, buf);
6419 }
6420 else
6421 {
6422 gfc_error ("expected identifier at %C");
6423 return MATCH_ERROR;
6424 }
6425 }
6426 break;
6427 case OMP_TRAIT_PROPERTY_NAME_LIST:
6428 do
6429 {
6430 char buf[GFC_MAX_SYMBOL_LEN + 1];
6431 if (gfc_match_name (buf) == MATCH_YES)
6432 {
6433 otp->name = XNEWVEC (char, strlen (buf) + 1);
6434 strcpy (otp->name, buf);
6435 otp->is_name = true;
6436 }
6437 else if (gfc_match_literal_constant (&otp->expr, 0)
6438 != MATCH_YES
6439 || otp->expr->ts.type != BT_CHARACTER)
6440 {
6441 gfc_error ("expected identifier or string literal "
6442 "at %C");
6443 return MATCH_ERROR;
6444 }
6445
6446 if (gfc_match (" ,") == MATCH_YES)
6447 {
6448 otp = gfc_get_omp_trait_property ();
6449 otp->property_kind = property_kind;
6450 otp->next = os->properties;
6451 os->properties = otp;
6452 }
6453 else
6454 break;
6455 }
6456 while (1);
6457 break;
6458 case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR:
6459 case OMP_TRAIT_PROPERTY_BOOL_EXPR:
6460 if (gfc_match_expr (&otp->expr) != MATCH_YES)
6461 {
6462 gfc_error ("expected expression at %C");
6463 return MATCH_ERROR;
6464 }
6465 if (!gfc_resolve_expr (otp->expr)
6466 || (property_kind == OMP_TRAIT_PROPERTY_BOOL_EXPR
6467 && otp->expr->ts.type != BT_LOGICAL)
6468 || (property_kind == OMP_TRAIT_PROPERTY_DEV_NUM_EXPR
6469 && otp->expr->ts.type != BT_INTEGER)
6470 || otp->expr->rank != 0
6471 || (!metadirective_p
6472 && otp->expr->expr_type != EXPR_CONSTANT))
6473 {
6474 if (metadirective_p)
6475 {
6476 if (property_kind == OMP_TRAIT_PROPERTY_BOOL_EXPR)
6477 gfc_error ("property must be a "
6478 "logical expression at %L",
6479 &otp->expr->where);
6480 else
6481 gfc_error ("property must be an "
6482 "integer expression at %L",
6483 &otp->expr->where);
6484 }
6485 else
6486 {
6487 if (property_kind == OMP_TRAIT_PROPERTY_BOOL_EXPR)
6488 gfc_error ("property must be a constant "
6489 "logical expression at %L",
6490 &otp->expr->where);
6491 else
6492 gfc_error ("property must be a constant "
6493 "integer expression at %L",
6494 &otp->expr->where);
6495 }
6496 return MATCH_ERROR;
6497 }
6498 /* Device number must be conforming, which includes
6499 omp_initial_device (-1) and omp_invalid_device (-4). */
6500 if (property_kind == OMP_TRAIT_PROPERTY_DEV_NUM_EXPR
6501 && otp->expr->expr_type == EXPR_CONSTANT
6502 && mpz_sgn (otp->expr->value.integer) < 0
6503 && mpz_cmp_si (otp->expr->value.integer, -1) != 0
6504 && mpz_cmp_si (otp->expr->value.integer, -4) != 0)
6505 {
6506 gfc_error ("property must be a conforming device number "
6507 "at %C");
6508 return MATCH_ERROR;
6509 }
6510 break;
6511 case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
6512 {
6513 if (os->code == OMP_TRAIT_CONSTRUCT_SIMD)
6514 {
6515 gfc_matching_omp_context_selector = true;
6516 if (gfc_match_omp_clauses (&otp->clauses,
6517 OMP_DECLARE_SIMD_CLAUSES,
6518 true, false, false)
6519 != MATCH_YES)
6520 {
6521 gfc_matching_omp_context_selector = false;
6522 gfc_error ("expected simd clause at %C");
6523 return MATCH_ERROR;
6524 }
6525 gfc_matching_omp_context_selector = false;
6526 }
6527 else if (os->code == OMP_TRAIT_IMPLEMENTATION_REQUIRES)
6528 {
6529 /* FIXME: The "requires" selector was added in OpenMP 5.1.
6530 Currently only the now-deprecated syntax
6531 from OpenMP 5.0 is supported.
6532 TODO: When implementing, update modules.cc as well. */
6533 sorry_at (gfc_get_location (&gfc_current_locus),
6534 "%<requires%> selector is not supported yet");
6535 return MATCH_ERROR;
6536 }
6537 else
6538 gcc_unreachable ();
6539 break;
6540 }
6541 default:
6542 gcc_unreachable ();
6543 }
6544
6545 if (gfc_match (" )") != MATCH_YES)
6546 {
6547 gfc_error ("expected %<)%> at %C");
6548 return MATCH_ERROR;
6549 }
6550 }
6551 else if (property_kind != OMP_TRAIT_PROPERTY_NONE
6552 && property_kind != OMP_TRAIT_PROPERTY_CLAUSE_LIST
6553 && property_kind != OMP_TRAIT_PROPERTY_EXTENSION)
6554 {
6555 if (gfc_match (" (") != MATCH_YES)
6556 {
6557 gfc_error ("expected %<(%> at %C");
6558 return MATCH_ERROR;
6559 }
6560 }
6561
6562 if (gfc_match (" ,") != MATCH_YES)
6563 break;
6564 }
6565 while (1);
6566
6567 return MATCH_YES;
6568 }
6569
6570 /* OpenMP 5.0:
6571
6572 trait-set-selector[,trait-set-selector[,...]]
6573
6574 trait-set-selector:
6575 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
6576
6577 trait-set-selector-name:
6578 constructor
6579 device
6580 implementation
6581 user */
6582
6583 match
6584 gfc_match_omp_context_selector_specification (gfc_omp_set_selector **oss_head,
6585 bool metadirective_p)
6586 {
6587 do
6588 {
6589 match m;
6590 char buf[GFC_MAX_SYMBOL_LEN + 1];
6591 enum omp_tss_code set = OMP_TRAIT_SET_INVALID;
6592
6593 m = gfc_match_name (buf);
6594 if (m == MATCH_YES)
6595 set = omp_lookup_tss_code (buf);
6596
6597 if (set == OMP_TRAIT_SET_INVALID)
6598 {
6599 gfc_error ("expected context selector set name at %C");
6600 return MATCH_ERROR;
6601 }
6602
6603 m = gfc_match (" =");
6604 if (m != MATCH_YES)
6605 {
6606 gfc_error ("expected %<=%> at %C");
6607 return MATCH_ERROR;
6608 }
6609
6610 m = gfc_match (" {");
6611 if (m != MATCH_YES)
6612 {
6613 gfc_error ("expected %<{%> at %C");
6614 return MATCH_ERROR;
6615 }
6616
6617 gfc_omp_set_selector *oss = gfc_get_omp_set_selector ();
6618 oss->next = *oss_head;
6619 oss->code = set;
6620 *oss_head = oss;
6621
6622 if (gfc_match_omp_context_selector (oss, metadirective_p) != MATCH_YES)
6623 return MATCH_ERROR;
6624
6625 m = gfc_match (" }");
6626 if (m != MATCH_YES)
6627 {
6628 gfc_error ("expected %<}%> at %C");
6629 return MATCH_ERROR;
6630 }
6631
6632 m = gfc_match (" ,");
6633 if (m != MATCH_YES)
6634 break;
6635 }
6636 while (1);
6637
6638 return MATCH_YES;
6639 }
6640
6641
6642 match
6643 gfc_match_omp_declare_variant (void)
6644 {
6645 char buf[GFC_MAX_SYMBOL_LEN + 1];
6646
6647 if (gfc_match (" (") != MATCH_YES)
6648 {
6649 gfc_error ("expected %<(%> at %C");
6650 return MATCH_ERROR;
6651 }
6652
6653 gfc_symtree *base_proc_st, *variant_proc_st;
6654 if (gfc_match_name (buf) != MATCH_YES)
6655 {
6656 gfc_error ("expected name at %C");
6657 return MATCH_ERROR;
6658 }
6659
6660 if (gfc_get_ha_sym_tree (buf, &base_proc_st))
6661 return MATCH_ERROR;
6662
6663 if (gfc_match (" :") == MATCH_YES)
6664 {
6665 if (gfc_match_name (buf) != MATCH_YES)
6666 {
6667 gfc_error ("expected variant name at %C");
6668 return MATCH_ERROR;
6669 }
6670
6671 if (gfc_get_ha_sym_tree (buf, &variant_proc_st))
6672 return MATCH_ERROR;
6673 }
6674 else
6675 {
6676 /* Base procedure not specified. */
6677 variant_proc_st = base_proc_st;
6678 base_proc_st = NULL;
6679 }
6680
6681 gfc_omp_declare_variant *odv;
6682 odv = gfc_get_omp_declare_variant ();
6683 odv->where = gfc_current_locus;
6684 odv->variant_proc_symtree = variant_proc_st;
6685 odv->adjust_args_list = NULL;
6686 odv->base_proc_symtree = base_proc_st;
6687 odv->next = NULL;
6688 odv->error_p = false;
6689
6690 /* Add the new declare variant to the end of the list. */
6691 gfc_omp_declare_variant **prev_next = &gfc_current_ns->omp_declare_variant;
6692 while (*prev_next)
6693 prev_next = &((*prev_next)->next);
6694 *prev_next = odv;
6695
6696 if (gfc_match (" )") != MATCH_YES)
6697 {
6698 gfc_error ("expected %<)%> at %C");
6699 return MATCH_ERROR;
6700 }
6701
6702 bool has_match = false, has_adjust_args = false, has_append_args = false;
6703 bool error_p = false;
6704 locus adjust_args_loc;
6705 locus append_args_loc;
6706
6707 gfc_gobble_whitespace ();
6708 gfc_match_char (',');
6709 for (;;)
6710 {
6711 gfc_gobble_whitespace ();
6712
6713 enum clause
6714 {
6715 clause_match,
6716 clause_adjust_args,
6717 clause_append_args
6718 } ccode;
6719
6720 if (gfc_match ("match") == MATCH_YES)
6721 ccode = clause_match;
6722 else if (gfc_match ("adjust_args") == MATCH_YES)
6723 {
6724 ccode = clause_adjust_args;
6725 adjust_args_loc = gfc_current_locus;
6726 }
6727 else if (gfc_match ("append_args") == MATCH_YES)
6728 {
6729 ccode = clause_append_args;
6730 append_args_loc = gfc_current_locus;
6731 }
6732 else
6733 {
6734 error_p = true;
6735 break;
6736 }
6737
6738 if (gfc_match (" ( ") != MATCH_YES)
6739 {
6740 gfc_error ("expected %<(%> at %C");
6741 return MATCH_ERROR;
6742 }
6743
6744 if (ccode == clause_match)
6745 {
6746 if (has_match)
6747 {
6748 gfc_error ("%qs clause at %L specified more than once",
6749 "match", &gfc_current_locus);
6750 return MATCH_ERROR;
6751 }
6752 has_match = true;
6753 if (gfc_match_omp_context_selector_specification (&odv->set_selectors,
6754 false)
6755 != MATCH_YES)
6756 return MATCH_ERROR;
6757 if (gfc_match (" )") != MATCH_YES)
6758 {
6759 gfc_error ("expected %<)%> at %C");
6760 return MATCH_ERROR;
6761 }
6762 }
6763 else if (ccode == clause_adjust_args)
6764 {
6765 has_adjust_args = true;
6766 bool need_device_ptr_p = false;
6767 bool need_device_addr_p = false;
6768 if (gfc_match ("nothing ") == MATCH_YES)
6769 ;
6770 else if (gfc_match ("need_device_ptr ") == MATCH_YES)
6771 need_device_ptr_p = true;
6772 else if (gfc_match ("need_device_addr ") == MATCH_YES)
6773 need_device_addr_p = true;
6774 else
6775 {
6776 gfc_error ("expected %<nothing%>, %<need_device_ptr%> or "
6777 "%<need_device_addr%> at %C");
6778 return MATCH_ERROR;
6779 }
6780 if (gfc_match (": ") != MATCH_YES)
6781 {
6782 gfc_error ("expected %<:%> at %C");
6783 return MATCH_ERROR;
6784 }
6785 gfc_omp_namelist *tail = NULL;
6786 bool need_range = false, have_range = false;
6787 while (true)
6788 {
6789 gfc_omp_namelist *p = gfc_get_omp_namelist ();
6790 p->where = gfc_current_locus;
6791 p->u.adj_args.need_ptr = need_device_ptr_p;
6792 p->u.adj_args.need_addr = need_device_addr_p;
6793 if (tail)
6794 {
6795 tail->next = p;
6796 tail = tail->next;
6797 }
6798 else
6799 {
6800 gfc_omp_namelist **q = &odv->adjust_args_list;
6801 if (*q)
6802 {
6803 for (; (*q)->next; q = &(*q)->next)
6804 ;
6805 (*q)->next = p;
6806 }
6807 else
6808 *q = p;
6809 tail = p;
6810 }
6811 if (gfc_match (": ") == MATCH_YES)
6812 {
6813 if (have_range)
6814 {
6815 gfc_error ("unexpected %<:%> at %C");
6816 return MATCH_ERROR;
6817 }
6818 p->u.adj_args.range_start = have_range = true;
6819 need_range = false;
6820 continue;
6821 }
6822 if (have_range && gfc_match (", ") == MATCH_YES)
6823 {
6824 have_range = false;
6825 continue;
6826 }
6827 if (have_range && gfc_match (") ") == MATCH_YES)
6828 break;
6829 locus saved_loc = gfc_current_locus;
6830
6831 /* Without ranges, only arg names or integer literals permitted;
6832 handle literals here as gfc_match_expr simplifies the expr. */
6833 if (gfc_match_literal_constant (&p->expr, true) == MATCH_YES)
6834 {
6835 gfc_gobble_whitespace ();
6836 char c = gfc_peek_ascii_char ();
6837 if (c != ')' && c != ',' && c != ':')
6838 {
6839 gfc_free_expr (p->expr);
6840 p->expr = NULL;
6841 gfc_current_locus = saved_loc;
6842 }
6843 }
6844 if (!p->expr && gfc_match ("omp_num_args") == MATCH_YES)
6845 {
6846 if (!have_range)
6847 p->u.adj_args.range_start = need_range = true;
6848 else
6849 need_range = false;
6850
6851 locus saved_loc2 = gfc_current_locus;
6852 gfc_gobble_whitespace ();
6853 char c = gfc_peek_ascii_char ();
6854 if (c == '+' || c == '-')
6855 {
6856 if (gfc_match ("+ %e", &p->expr) == MATCH_YES)
6857 p->u.adj_args.omp_num_args_plus = true;
6858 else if (gfc_match ("- %e", &p->expr) == MATCH_YES)
6859 p->u.adj_args.omp_num_args_minus = true;
6860 else if (!gfc_error_check ())
6861 {
6862 gfc_error ("expected constant integer expression "
6863 "at %C");
6864 p->u.adj_args.error_p = true;
6865 return MATCH_ERROR;
6866 }
6867 p->where = gfc_get_location_range (&saved_loc, 1,
6868 &saved_loc, 1,
6869 &gfc_current_locus);
6870 }
6871 else
6872 {
6873 p->where = gfc_get_location_range (&saved_loc, 1,
6874 &saved_loc, 1,
6875 &saved_loc2);
6876 p->u.adj_args.omp_num_args_plus = true;
6877 }
6878 }
6879 else if (!p->expr)
6880 {
6881 match m = gfc_match_expr (&p->expr);
6882 if (m != MATCH_YES)
6883 {
6884 gfc_error ("expected dummy parameter name, "
6885 "%<omp_num_args%> or constant positive integer"
6886 " at %C");
6887 p->u.adj_args.error_p = true;
6888 return MATCH_ERROR;
6889 }
6890 if (p->expr->expr_type == EXPR_CONSTANT && !have_range)
6891 need_range = true; /* Constant expr but not literal. */
6892 p->where = p->expr->where;
6893 }
6894 else
6895 p->where = p->expr->where;
6896 gfc_gobble_whitespace ();
6897 match m = gfc_match (": ");
6898 if (need_range && m != MATCH_YES)
6899 {
6900 gfc_error ("expected %<:%> at %C");
6901 return MATCH_ERROR;
6902 }
6903 if (m == MATCH_YES)
6904 {
6905 p->u.adj_args.range_start = have_range = true;
6906 need_range = false;
6907 continue;
6908 }
6909 need_range = have_range = false;
6910 if (gfc_match (", ") == MATCH_YES)
6911 continue;
6912 if (gfc_match (") ") == MATCH_YES)
6913 break;
6914 }
6915 }
6916 else if (ccode == clause_append_args)
6917 {
6918 if (has_append_args)
6919 {
6920 gfc_error ("%qs clause at %L specified more than once",
6921 "append_args", &gfc_current_locus);
6922 return MATCH_ERROR;
6923 }
6924 has_append_args = true;
6925 gfc_omp_namelist *append_args_last = NULL;
6926 do
6927 {
6928 gfc_gobble_whitespace ();
6929 if (gfc_match ("interop ") != MATCH_YES)
6930 {
6931 gfc_error ("expected %<interop%> at %C");
6932 return MATCH_ERROR;
6933 }
6934 if (gfc_match ("( ") != MATCH_YES)
6935 {
6936 gfc_error ("expected %<(%> at %C");
6937 return MATCH_ERROR;
6938 }
6939
6940 bool target, targetsync;
6941 char *type_str = NULL;
6942 int type_str_len;
6943 locus loc = gfc_current_locus;
6944 if (gfc_parser_omp_clause_init_modifiers (target, targetsync,
6945 &type_str, type_str_len,
6946 false) == MATCH_ERROR)
6947 return MATCH_ERROR;
6948
6949 gfc_omp_namelist *n = gfc_get_omp_namelist();
6950 n->where = loc;
6951 n->u.init.target = target;
6952 n->u.init.targetsync = targetsync;
6953 n->u.init.len = type_str_len;
6954 n->u2.init_interop = type_str;
6955 if (odv->append_args_list)
6956 {
6957 append_args_last->next = n;
6958 append_args_last = n;
6959 }
6960 else
6961 append_args_last = odv->append_args_list = n;
6962
6963 gfc_gobble_whitespace ();
6964 if (gfc_match_char (',') == MATCH_YES)
6965 continue;
6966 if (gfc_match_char (')') == MATCH_YES)
6967 break;
6968 gfc_error ("Expected %<,%> or %<)%> at %C");
6969 return MATCH_ERROR;
6970 }
6971 while (true);
6972 }
6973 gfc_gobble_whitespace ();
6974 if (gfc_match_omp_eos () == MATCH_YES)
6975 break;
6976 gfc_match_char (',');
6977 }
6978
6979 if (error_p || (!has_match && !has_adjust_args && !has_append_args))
6980 {
6981 gfc_error ("expected %<match%>, %<adjust_args%> or %<append_args%> at %C");
6982 return MATCH_ERROR;
6983 }
6984
6985 if ((has_adjust_args || has_append_args) && !has_match)
6986 {
6987 gfc_error ("the %qs clause at %L can only be specified if the "
6988 "%<dispatch%> selector of the construct selector set appears "
6989 "in the %<match%> clause",
6990 has_adjust_args ? "adjust_args" : "append_args",
6991 has_adjust_args ? &adjust_args_loc : &append_args_loc);
6992 return MATCH_ERROR;
6993 }
6994
6995 return MATCH_YES;
6996 }
6997
6998
6999 static match
7000 match_omp_metadirective (bool begin_p)
7001 {
7002 locus old_loc = gfc_current_locus;
7003 gfc_omp_variant *variants_head;
7004 gfc_omp_variant **next_variant = &variants_head;
7005 bool default_seen = false;
7006
7007 /* Parse the context selectors. */
7008 for (;;)
7009 {
7010 bool default_p = false;
7011 gfc_omp_set_selector *selectors = NULL;
7012
7013 gfc_gobble_whitespace ();
7014 if (gfc_match_eos () == MATCH_YES)
7015 break;
7016 gfc_match_char (',');
7017 gfc_gobble_whitespace ();
7018
7019 locus variant_locus = gfc_current_locus;
7020
7021 if (gfc_match (" default ( ") == MATCH_YES)
7022 default_p = true;
7023 else if (gfc_match (" otherwise ( ") == MATCH_YES)
7024 default_p = true;
7025 else if (gfc_match (" when ( ") != MATCH_YES)
7026 {
7027 gfc_error ("expected %<when%>, %<otherwise%>, or %<default%> at %C");
7028 gfc_current_locus = old_loc;
7029 return MATCH_ERROR;
7030 }
7031
7032 if (default_p && default_seen)
7033 {
7034 gfc_error ("too many %<otherwise%> or %<default%> clauses "
7035 "in %<metadirective%> at %C");
7036 gfc_current_locus = old_loc;
7037 return MATCH_ERROR;
7038 }
7039 else if (default_seen)
7040 {
7041 gfc_error ("%<otherwise%> or %<default%> clause "
7042 "must appear last in %<metadirective%> at %C");
7043 gfc_current_locus = old_loc;
7044 return MATCH_ERROR;
7045 }
7046
7047 if (!default_p)
7048 {
7049 if (gfc_match_omp_context_selector_specification (&selectors, true)
7050 != MATCH_YES)
7051 return MATCH_ERROR;
7052
7053 if (gfc_match (" : ") != MATCH_YES)
7054 {
7055 gfc_error ("expected %<:%> at %C");
7056 gfc_current_locus = old_loc;
7057 return MATCH_ERROR;
7058 }
7059
7060 gfc_commit_symbols ();
7061 }
7062
7063 gfc_matching_omp_context_selector = true;
7064 gfc_statement directive = match_omp_directive ();
7065 gfc_matching_omp_context_selector = false;
7066
7067 if (is_omp_declarative_stmt (directive))
7068 sorry_at (gfc_get_location (&gfc_current_locus),
7069 "declarative directive variants are not supported");
7070
7071 if (gfc_error_flag_test ())
7072 {
7073 gfc_current_locus = old_loc;
7074 return MATCH_ERROR;
7075 }
7076
7077 if (gfc_match (" )") != MATCH_YES)
7078 {
7079 gfc_error ("Expected %<)%> at %C");
7080 gfc_current_locus = old_loc;
7081 return MATCH_ERROR;
7082 }
7083
7084 gfc_commit_symbols ();
7085
7086 if (begin_p
7087 && directive != ST_NONE
7088 && gfc_omp_end_stmt (directive) == ST_NONE)
7089 {
7090 gfc_error ("variant directive used in OMP BEGIN METADIRECTIVE "
7091 "at %C must have a corresponding end directive");
7092 gfc_current_locus = old_loc;
7093 return MATCH_ERROR;
7094 }
7095
7096 if (default_p)
7097 default_seen = true;
7098
7099 gfc_omp_variant *omv = gfc_get_omp_variant ();
7100 omv->selectors = selectors;
7101 omv->stmt = directive;
7102 omv->where = variant_locus;
7103
7104 if (directive == ST_NONE)
7105 {
7106 /* The directive was a 'nothing' directive. */
7107 omv->code = gfc_get_code (EXEC_CONTINUE);
7108 omv->code->ext.omp_clauses = NULL;
7109 }
7110 else
7111 {
7112 omv->code = gfc_get_code (new_st.op);
7113 omv->code->ext.omp_clauses = new_st.ext.omp_clauses;
7114 /* Prevent the OpenMP clauses from being freed via NEW_ST. */
7115 new_st.ext.omp_clauses = NULL;
7116 }
7117
7118 *next_variant = omv;
7119 next_variant = &omv->next;
7120 }
7121
7122 if (gfc_match_omp_eos () != MATCH_YES)
7123 {
7124 gfc_error ("Unexpected junk after OMP METADIRECTIVE at %C");
7125 gfc_current_locus = old_loc;
7126 return MATCH_ERROR;
7127 }
7128
7129 /* Add a 'default (nothing)' clause if no default is explicitly given. */
7130 if (!default_seen)
7131 {
7132 gfc_omp_variant *omv = gfc_get_omp_variant ();
7133 omv->stmt = ST_NONE;
7134 omv->code = gfc_get_code (EXEC_CONTINUE);
7135 omv->code->ext.omp_clauses = NULL;
7136 omv->where = old_loc;
7137 omv->selectors = NULL;
7138
7139 *next_variant = omv;
7140 next_variant = &omv->next;
7141 }
7142
7143 new_st.op = EXEC_OMP_METADIRECTIVE;
7144 new_st.ext.omp_variants = variants_head;
7145
7146 return MATCH_YES;
7147 }
7148
7149 match
7150 gfc_match_omp_begin_metadirective (void)
7151 {
7152 return match_omp_metadirective (true);
7153 }
7154
7155 match
7156 gfc_match_omp_metadirective (void)
7157 {
7158 return match_omp_metadirective (false);
7159 }
7160
7161 match
7162 gfc_match_omp_threadprivate (void)
7163 {
7164 locus old_loc;
7165 char n[GFC_MAX_SYMBOL_LEN+1];
7166 gfc_symbol *sym;
7167 match m;
7168 gfc_symtree *st;
7169
7170 old_loc = gfc_current_locus;
7171
7172 m = gfc_match (" (");
7173 if (m != MATCH_YES)
7174 return m;
7175
7176 for (;;)
7177 {
7178 m = gfc_match_symbol (&sym, 0);
7179 switch (m)
7180 {
7181 case MATCH_YES:
7182 if (sym->attr.in_common)
7183 gfc_error_now ("Threadprivate variable at %C is an element of "
7184 "a COMMON block");
7185 else if (!gfc_add_threadprivate (&sym->attr, sym->name, &sym->declared_at))
7186 goto cleanup;
7187 goto next_item;
7188 case MATCH_NO:
7189 break;
7190 case MATCH_ERROR:
7191 goto cleanup;
7192 }
7193
7194 m = gfc_match (" / %n /", n);
7195 if (m == MATCH_ERROR)
7196 goto cleanup;
7197 if (m == MATCH_NO || n[0] == '\0')
7198 goto syntax;
7199
7200 st = gfc_find_symtree (gfc_current_ns->common_root, n);
7201 if (st == NULL)
7202 {
7203 gfc_error ("COMMON block /%s/ not found at %C", n);
7204 goto cleanup;
7205 }
7206 st->n.common->threadprivate = 1;
7207 for (sym = st->n.common->head; sym; sym = sym->common_next)
7208 if (!gfc_add_threadprivate (&sym->attr, sym->name, &sym->declared_at))
7209 goto cleanup;
7210
7211 next_item:
7212 if (gfc_match_char (')') == MATCH_YES)
7213 break;
7214 if (gfc_match_char (',') != MATCH_YES)
7215 goto syntax;
7216 }
7217
7218 if (gfc_match_omp_eos () != MATCH_YES)
7219 {
7220 gfc_error ("Unexpected junk after OMP THREADPRIVATE at %C");
7221 goto cleanup;
7222 }
7223
7224 return MATCH_YES;
7225
7226 syntax:
7227 gfc_error ("Syntax error in !$OMP THREADPRIVATE list at %C");
7228
7229 cleanup:
7230 gfc_current_locus = old_loc;
7231 return MATCH_ERROR;
7232 }
7233
7234
7235 match
7236 gfc_match_omp_parallel (void)
7237 {
7238 return match_omp (EXEC_OMP_PARALLEL, OMP_PARALLEL_CLAUSES);
7239 }
7240
7241
7242 match
7243 gfc_match_omp_parallel_do (void)
7244 {
7245 return match_omp (EXEC_OMP_PARALLEL_DO,
7246 (OMP_PARALLEL_CLAUSES | OMP_DO_CLAUSES)
7247 & ~(omp_mask (OMP_CLAUSE_NOWAIT)));
7248 }
7249
7250
7251 match
7252 gfc_match_omp_parallel_do_simd (void)
7253 {
7254 return match_omp (EXEC_OMP_PARALLEL_DO_SIMD,
7255 (OMP_PARALLEL_CLAUSES | OMP_DO_CLAUSES | OMP_SIMD_CLAUSES)
7256 & ~(omp_mask (OMP_CLAUSE_NOWAIT)));
7257 }
7258
7259
7260 match
7261 gfc_match_omp_parallel_masked (void)
7262 {
7263 return match_omp (EXEC_OMP_PARALLEL_MASKED,
7264 OMP_PARALLEL_CLAUSES | OMP_MASKED_CLAUSES);
7265 }
7266
7267 match
7268 gfc_match_omp_parallel_masked_taskloop (void)
7269 {
7270 return match_omp (EXEC_OMP_PARALLEL_MASKED_TASKLOOP,
7271 (OMP_PARALLEL_CLAUSES | OMP_MASKED_CLAUSES
7272 | OMP_TASKLOOP_CLAUSES)
7273 & ~(omp_mask (OMP_CLAUSE_IN_REDUCTION)));
7274 }
7275
7276 match
7277 gfc_match_omp_parallel_masked_taskloop_simd (void)
7278 {
7279 return match_omp (EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD,
7280 (OMP_PARALLEL_CLAUSES | OMP_MASKED_CLAUSES
7281 | OMP_TASKLOOP_CLAUSES | OMP_SIMD_CLAUSES)
7282 & ~(omp_mask (OMP_CLAUSE_IN_REDUCTION)));
7283 }
7284
7285 match
7286 gfc_match_omp_parallel_master (void)
7287 {
7288 return match_omp (EXEC_OMP_PARALLEL_MASTER, OMP_PARALLEL_CLAUSES);
7289 }
7290
7291 match
7292 gfc_match_omp_parallel_master_taskloop (void)
7293 {
7294 return match_omp (EXEC_OMP_PARALLEL_MASTER_TASKLOOP,
7295 (OMP_PARALLEL_CLAUSES | OMP_TASKLOOP_CLAUSES)
7296 & ~(omp_mask (OMP_CLAUSE_IN_REDUCTION)));
7297 }
7298
7299 match
7300 gfc_match_omp_parallel_master_taskloop_simd (void)
7301 {
7302 return match_omp (EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD,
7303 (OMP_PARALLEL_CLAUSES | OMP_TASKLOOP_CLAUSES
7304 | OMP_SIMD_CLAUSES)
7305 & ~(omp_mask (OMP_CLAUSE_IN_REDUCTION)));
7306 }
7307
7308 match
7309 gfc_match_omp_parallel_sections (void)
7310 {
7311 return match_omp (EXEC_OMP_PARALLEL_SECTIONS,
7312 (OMP_PARALLEL_CLAUSES | OMP_SECTIONS_CLAUSES)
7313 & ~(omp_mask (OMP_CLAUSE_NOWAIT)));
7314 }
7315
7316
7317 match
7318 gfc_match_omp_parallel_workshare (void)
7319 {
7320 return match_omp (EXEC_OMP_PARALLEL_WORKSHARE, OMP_PARALLEL_CLAUSES);
7321 }
7322
7323 void
7324 gfc_check_omp_requires (gfc_namespace *ns, int ref_omp_requires)
7325 {
7326 const char *msg = G_("Program unit at %L has OpenMP device "
7327 "constructs/routines but does not set !$OMP REQUIRES %s "
7328 "but other program units do");
7329 if (ns->omp_target_seen
7330 && (ns->omp_requires & OMP_REQ_TARGET_MASK)
7331 != (ref_omp_requires & OMP_REQ_TARGET_MASK))
7332 {
7333 gcc_assert (ns->proc_name);
7334 if ((ref_omp_requires & OMP_REQ_REVERSE_OFFLOAD)
7335 && !(ns->omp_requires & OMP_REQ_REVERSE_OFFLOAD))
7336 gfc_error (msg, &ns->proc_name->declared_at, "REVERSE_OFFLOAD");
7337 if ((ref_omp_requires & OMP_REQ_UNIFIED_ADDRESS)
7338 && !(ns->omp_requires & OMP_REQ_UNIFIED_ADDRESS))
7339 gfc_error (msg, &ns->proc_name->declared_at, "UNIFIED_ADDRESS");
7340 if ((ref_omp_requires & OMP_REQ_UNIFIED_SHARED_MEMORY)
7341 && !(ns->omp_requires & OMP_REQ_UNIFIED_SHARED_MEMORY))
7342 gfc_error (msg, &ns->proc_name->declared_at, "UNIFIED_SHARED_MEMORY");
7343 if ((ref_omp_requires & OMP_REQ_SELF_MAPS)
7344 && !(ns->omp_requires & OMP_REQ_UNIFIED_SHARED_MEMORY))
7345 gfc_error (msg, &ns->proc_name->declared_at, "SELF_MAPS");
7346 }
7347 }
7348
7349 bool
7350 gfc_omp_requires_add_clause (gfc_omp_requires_kind clause,
7351 const char *clause_name, locus *loc,
7352 const char *module_name)
7353 {
7354 gfc_namespace *prog_unit = gfc_current_ns;
7355 while (prog_unit->parent)
7356 {
7357 if (gfc_state_stack->previous
7358 && gfc_state_stack->previous->state == COMP_INTERFACE)
7359 break;
7360 prog_unit = prog_unit->parent;
7361 }
7362
7363 /* Requires added after use. */
7364 if (prog_unit->omp_target_seen
7365 && (clause & OMP_REQ_TARGET_MASK)
7366 && !(prog_unit->omp_requires & clause))
7367 {
7368 if (module_name)
7369 gfc_error ("!$OMP REQUIRES clause %qs specified via module %qs use "
7370 "at %L comes after using a device construct/routine",
7371 clause_name, module_name, loc);
7372 else
7373 gfc_error ("!$OMP REQUIRES clause %qs specified at %L comes after "
7374 "using a device construct/routine", clause_name, loc);
7375 return false;
7376 }
7377
7378 /* Overriding atomic_default_mem_order clause value. */
7379 if ((clause & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
7380 && (prog_unit->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
7381 && (prog_unit->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
7382 != (int) clause)
7383 {
7384 const char *other;
7385 switch (prog_unit->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
7386 {
7387 case OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST: other = "seq_cst"; break;
7388 case OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL: other = "acq_rel"; break;
7389 case OMP_REQ_ATOMIC_MEM_ORDER_ACQUIRE: other = "acquire"; break;
7390 case OMP_REQ_ATOMIC_MEM_ORDER_RELAXED: other = "relaxed"; break;
7391 case OMP_REQ_ATOMIC_MEM_ORDER_RELEASE: other = "release"; break;
7392 default: gcc_unreachable ();
7393 }
7394
7395 if (module_name)
7396 gfc_error ("!$OMP REQUIRES clause %<atomic_default_mem_order(%s)%> "
7397 "specified via module %qs use at %L overrides a previous "
7398 "%<atomic_default_mem_order(%s)%> (which might be through "
7399 "using a module)", clause_name, module_name, loc, other);
7400 else
7401 gfc_error ("!$OMP REQUIRES clause %<atomic_default_mem_order(%s)%> "
7402 "specified at %L overrides a previous "
7403 "%<atomic_default_mem_order(%s)%> (which might be through "
7404 "using a module)", clause_name, loc, other);
7405 return false;
7406 }
7407
7408 /* Requires via module not at program-unit level and not repeating clause. */
7409 if (prog_unit != gfc_current_ns && !(prog_unit->omp_requires & clause))
7410 {
7411 if (clause & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
7412 gfc_error ("!$OMP REQUIRES clause %<atomic_default_mem_order(%s)%> "
7413 "specified via module %qs use at %L but same clause is "
7414 "not specified for the program unit", clause_name,
7415 module_name, loc);
7416 else
7417 gfc_error ("!$OMP REQUIRES clause %qs specified via module %qs use at "
7418 "%L but same clause is not specified for the program unit",
7419 clause_name, module_name, loc);
7420 return false;
7421 }
7422
7423 if (!gfc_state_stack->previous
7424 || gfc_state_stack->previous->state != COMP_INTERFACE)
7425 prog_unit->omp_requires |= clause;
7426 return true;
7427 }
7428
7429 match
7430 gfc_match_omp_requires (void)
7431 {
7432 static const char *clauses[] = {"reverse_offload",
7433 "unified_address",
7434 "unified_shared_memory",
7435 "self_maps",
7436 "dynamic_allocators",
7437 "atomic_default"};
7438 const char *clause = NULL;
7439 int requires_clauses = 0;
7440 bool first = true;
7441 locus old_loc;
7442
7443 if (gfc_current_ns->parent
7444 && (!gfc_state_stack->previous
7445 || gfc_state_stack->previous->state != COMP_INTERFACE))
7446 {
7447 gfc_error ("!$OMP REQUIRES at %C must appear in the specification part "
7448 "of a program unit");
7449 return MATCH_ERROR;
7450 }
7451
7452 while (true)
7453 {
7454 old_loc = gfc_current_locus;
7455 gfc_omp_requires_kind requires_clause;
7456 if ((first || gfc_match_char (',') != MATCH_YES)
7457 && (first && gfc_match_space () != MATCH_YES))
7458 goto error;
7459 first = false;
7460 gfc_gobble_whitespace ();
7461 old_loc = gfc_current_locus;
7462
7463 if (gfc_match_omp_eos () != MATCH_NO)
7464 break;
7465 if (gfc_match (clauses[0]) == MATCH_YES)
7466 {
7467 clause = clauses[0];
7468 requires_clause = OMP_REQ_REVERSE_OFFLOAD;
7469 if (requires_clauses & OMP_REQ_REVERSE_OFFLOAD)
7470 goto duplicate_clause;
7471 }
7472 else if (gfc_match (clauses[1]) == MATCH_YES)
7473 {
7474 clause = clauses[1];
7475 requires_clause = OMP_REQ_UNIFIED_ADDRESS;
7476 if (requires_clauses & OMP_REQ_UNIFIED_ADDRESS)
7477 goto duplicate_clause;
7478 }
7479 else if (gfc_match (clauses[2]) == MATCH_YES)
7480 {
7481 clause = clauses[2];
7482 requires_clause = OMP_REQ_UNIFIED_SHARED_MEMORY;
7483 if (requires_clauses & OMP_REQ_UNIFIED_SHARED_MEMORY)
7484 goto duplicate_clause;
7485 }
7486 else if (gfc_match (clauses[3]) == MATCH_YES)
7487 {
7488 clause = clauses[3];
7489 requires_clause = OMP_REQ_SELF_MAPS;
7490 if (requires_clauses & OMP_REQ_SELF_MAPS)
7491 goto duplicate_clause;
7492 }
7493 else if (gfc_match (clauses[4]) == MATCH_YES)
7494 {
7495 clause = clauses[4];
7496 requires_clause = OMP_REQ_DYNAMIC_ALLOCATORS;
7497 if (requires_clauses & OMP_REQ_DYNAMIC_ALLOCATORS)
7498 goto duplicate_clause;
7499 }
7500 else if (gfc_match ("atomic_default_mem_order (") == MATCH_YES)
7501 {
7502 clause = clauses[5];
7503 if (requires_clauses & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
7504 goto duplicate_clause;
7505 if (gfc_match (" seq_cst )") == MATCH_YES)
7506 {
7507 clause = "seq_cst";
7508 requires_clause = OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST;
7509 }
7510 else if (gfc_match (" acq_rel )") == MATCH_YES)
7511 {
7512 clause = "acq_rel";
7513 requires_clause = OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL;
7514 }
7515 else if (gfc_match (" acquire )") == MATCH_YES)
7516 {
7517 clause = "acquire";
7518 requires_clause = OMP_REQ_ATOMIC_MEM_ORDER_ACQUIRE;
7519 }
7520 else if (gfc_match (" relaxed )") == MATCH_YES)
7521 {
7522 clause = "relaxed";
7523 requires_clause = OMP_REQ_ATOMIC_MEM_ORDER_RELAXED;
7524 }
7525 else if (gfc_match (" release )") == MATCH_YES)
7526 {
7527 clause = "release";
7528 requires_clause = OMP_REQ_ATOMIC_MEM_ORDER_RELEASE;
7529 }
7530 else
7531 {
7532 gfc_error ("Expected ACQ_REL, ACQUIRE, RELAXED, RELEASE or "
7533 "SEQ_CST for ATOMIC_DEFAULT_MEM_ORDER clause at %C");
7534 goto error;
7535 }
7536 }
7537 else
7538 goto error;
7539
7540 if (!gfc_omp_requires_add_clause (requires_clause, clause, &old_loc, NULL))
7541 goto error;
7542 requires_clauses |= requires_clause;
7543 }
7544
7545 if (requires_clauses == 0)
7546 {
7547 if (!gfc_error_flag_test ())
7548 gfc_error ("Clause expected at %C");
7549 goto error;
7550 }
7551 return MATCH_YES;
7552
7553 duplicate_clause:
7554 gfc_error ("%qs clause at %L specified more than once", clause, &old_loc);
7555 error:
7556 if (!gfc_error_flag_test ())
7557 gfc_error ("Expected UNIFIED_ADDRESS, UNIFIED_SHARED_MEMORY, SELF_MAPS, "
7558 "DYNAMIC_ALLOCATORS, REVERSE_OFFLOAD, or "
7559 "ATOMIC_DEFAULT_MEM_ORDER clause at %L", &old_loc);
7560 return MATCH_ERROR;
7561 }
7562
7563
7564 match
7565 gfc_match_omp_scan (void)
7566 {
7567 bool incl;
7568 gfc_omp_clauses *c = gfc_get_omp_clauses ();
7569 gfc_gobble_whitespace ();
7570 if ((incl = (gfc_match ("inclusive") == MATCH_YES))
7571 || gfc_match ("exclusive") == MATCH_YES)
7572 {
7573 if (gfc_match_omp_variable_list (" (", &c->lists[incl ? OMP_LIST_SCAN_IN
7574 : OMP_LIST_SCAN_EX],
7575 false) != MATCH_YES)
7576 {
7577 gfc_free_omp_clauses (c);
7578 return MATCH_ERROR;
7579 }
7580 }
7581 else
7582 {
7583 gfc_error ("Expected INCLUSIVE or EXCLUSIVE clause at %C");
7584 gfc_free_omp_clauses (c);
7585 return MATCH_ERROR;
7586 }
7587 if (gfc_match_omp_eos () != MATCH_YES)
7588 {
7589 gfc_error ("Unexpected junk after !$OMP SCAN at %C");
7590 gfc_free_omp_clauses (c);
7591 return MATCH_ERROR;
7592 }
7593
7594 new_st.op = EXEC_OMP_SCAN;
7595 new_st.ext.omp_clauses = c;
7596 return MATCH_YES;
7597 }
7598
7599
7600 match
7601 gfc_match_omp_scope (void)
7602 {
7603 return match_omp (EXEC_OMP_SCOPE, OMP_SCOPE_CLAUSES);
7604 }
7605
7606
7607 match
7608 gfc_match_omp_sections (void)
7609 {
7610 return match_omp (EXEC_OMP_SECTIONS, OMP_SECTIONS_CLAUSES);
7611 }
7612
7613
7614 match
7615 gfc_match_omp_simd (void)
7616 {
7617 return match_omp (EXEC_OMP_SIMD, OMP_SIMD_CLAUSES);
7618 }
7619
7620
7621 match
7622 gfc_match_omp_single (void)
7623 {
7624 return match_omp (EXEC_OMP_SINGLE, OMP_SINGLE_CLAUSES);
7625 }
7626
7627
7628 match
7629 gfc_match_omp_target (void)
7630 {
7631 return match_omp (EXEC_OMP_TARGET, OMP_TARGET_CLAUSES);
7632 }
7633
7634
7635 match
7636 gfc_match_omp_target_data (void)
7637 {
7638 return match_omp (EXEC_OMP_TARGET_DATA, OMP_TARGET_DATA_CLAUSES);
7639 }
7640
7641
7642 match
7643 gfc_match_omp_target_enter_data (void)
7644 {
7645 return match_omp (EXEC_OMP_TARGET_ENTER_DATA, OMP_TARGET_ENTER_DATA_CLAUSES);
7646 }
7647
7648
7649 match
7650 gfc_match_omp_target_exit_data (void)
7651 {
7652 return match_omp (EXEC_OMP_TARGET_EXIT_DATA, OMP_TARGET_EXIT_DATA_CLAUSES);
7653 }
7654
7655
7656 match
7657 gfc_match_omp_target_parallel (void)
7658 {
7659 return match_omp (EXEC_OMP_TARGET_PARALLEL,
7660 (OMP_TARGET_CLAUSES | OMP_PARALLEL_CLAUSES)
7661 & ~(omp_mask (OMP_CLAUSE_COPYIN)));
7662 }
7663
7664
7665 match
7666 gfc_match_omp_target_parallel_do (void)
7667 {
7668 return match_omp (EXEC_OMP_TARGET_PARALLEL_DO,
7669 (OMP_TARGET_CLAUSES | OMP_PARALLEL_CLAUSES
7670 | OMP_DO_CLAUSES) & ~(omp_mask (OMP_CLAUSE_COPYIN)));
7671 }
7672
7673
7674 match
7675 gfc_match_omp_target_parallel_do_simd (void)
7676 {
7677 return match_omp (EXEC_OMP_TARGET_PARALLEL_DO_SIMD,
7678 (OMP_TARGET_CLAUSES | OMP_PARALLEL_CLAUSES | OMP_DO_CLAUSES
7679 | OMP_SIMD_CLAUSES) & ~(omp_mask (OMP_CLAUSE_COPYIN)));
7680 }
7681
7682
7683 match
7684 gfc_match_omp_target_simd (void)
7685 {
7686 return match_omp (EXEC_OMP_TARGET_SIMD,
7687 OMP_TARGET_CLAUSES | OMP_SIMD_CLAUSES);
7688 }
7689
7690
7691 match
7692 gfc_match_omp_target_teams (void)
7693 {
7694 return match_omp (EXEC_OMP_TARGET_TEAMS,
7695 OMP_TARGET_CLAUSES | OMP_TEAMS_CLAUSES);
7696 }
7697
7698
7699 match
7700 gfc_match_omp_target_teams_distribute (void)
7701 {
7702 return match_omp (EXEC_OMP_TARGET_TEAMS_DISTRIBUTE,
7703 OMP_TARGET_CLAUSES | OMP_TEAMS_CLAUSES
7704 | OMP_DISTRIBUTE_CLAUSES);
7705 }
7706
7707
7708 match
7709 gfc_match_omp_target_teams_distribute_parallel_do (void)
7710 {
7711 return match_omp (EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO,
7712 (OMP_TARGET_CLAUSES | OMP_TEAMS_CLAUSES
7713 | OMP_DISTRIBUTE_CLAUSES | OMP_PARALLEL_CLAUSES
7714 | OMP_DO_CLAUSES)
7715 & ~(omp_mask (OMP_CLAUSE_ORDERED))
7716 & ~(omp_mask (OMP_CLAUSE_LINEAR)));
7717 }
7718
7719
7720 match
7721 gfc_match_omp_target_teams_distribute_parallel_do_simd (void)
7722 {
7723 return match_omp (EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD,
7724 (OMP_TARGET_CLAUSES | OMP_TEAMS_CLAUSES
7725 | OMP_DISTRIBUTE_CLAUSES | OMP_PARALLEL_CLAUSES
7726 | OMP_DO_CLAUSES | OMP_SIMD_CLAUSES)
7727 & ~(omp_mask (OMP_CLAUSE_ORDERED)));
7728 }
7729
7730
7731 match
7732 gfc_match_omp_target_teams_distribute_simd (void)
7733 {
7734 return match_omp (EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD,
7735 OMP_TARGET_CLAUSES | OMP_TEAMS_CLAUSES
7736 | OMP_DISTRIBUTE_CLAUSES | OMP_SIMD_CLAUSES);
7737 }
7738
7739
7740 match
7741 gfc_match_omp_target_update (void)
7742 {
7743 return match_omp (EXEC_OMP_TARGET_UPDATE, OMP_TARGET_UPDATE_CLAUSES);
7744 }
7745
7746
7747 match
7748 gfc_match_omp_task (void)
7749 {
7750 return match_omp (EXEC_OMP_TASK, OMP_TASK_CLAUSES);
7751 }
7752
7753
7754 match
7755 gfc_match_omp_taskloop (void)
7756 {
7757 return match_omp (EXEC_OMP_TASKLOOP, OMP_TASKLOOP_CLAUSES);
7758 }
7759
7760
7761 match
7762 gfc_match_omp_taskloop_simd (void)
7763 {
7764 return match_omp (EXEC_OMP_TASKLOOP_SIMD,
7765 OMP_TASKLOOP_CLAUSES | OMP_SIMD_CLAUSES);
7766 }
7767
7768
7769 match
7770 gfc_match_omp_taskwait (void)
7771 {
7772 if (gfc_match_omp_eos () == MATCH_YES)
7773 {
7774 new_st.op = EXEC_OMP_TASKWAIT;
7775 new_st.ext.omp_clauses = NULL;
7776 return MATCH_YES;
7777 }
7778 return match_omp (EXEC_OMP_TASKWAIT,
7779 omp_mask (OMP_CLAUSE_DEPEND) | OMP_CLAUSE_NOWAIT);
7780 }
7781
7782
7783 match
7784 gfc_match_omp_taskyield (void)
7785 {
7786 if (gfc_match_omp_eos () != MATCH_YES)
7787 {
7788 gfc_error ("Unexpected junk after TASKYIELD clause at %C");
7789 return MATCH_ERROR;
7790 }
7791 new_st.op = EXEC_OMP_TASKYIELD;
7792 new_st.ext.omp_clauses = NULL;
7793 return MATCH_YES;
7794 }
7795
7796
7797 match
7798 gfc_match_omp_teams (void)
7799 {
7800 return match_omp (EXEC_OMP_TEAMS, OMP_TEAMS_CLAUSES);
7801 }
7802
7803
7804 match
7805 gfc_match_omp_teams_distribute (void)
7806 {
7807 return match_omp (EXEC_OMP_TEAMS_DISTRIBUTE,
7808 OMP_TEAMS_CLAUSES | OMP_DISTRIBUTE_CLAUSES);
7809 }
7810
7811
7812 match
7813 gfc_match_omp_teams_distribute_parallel_do (void)
7814 {
7815 return match_omp (EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO,
7816 (OMP_TEAMS_CLAUSES | OMP_DISTRIBUTE_CLAUSES
7817 | OMP_PARALLEL_CLAUSES | OMP_DO_CLAUSES)
7818 & ~(omp_mask (OMP_CLAUSE_ORDERED)
7819 | OMP_CLAUSE_LINEAR | OMP_CLAUSE_NOWAIT));
7820 }
7821
7822
7823 match
7824 gfc_match_omp_teams_distribute_parallel_do_simd (void)
7825 {
7826 return match_omp (EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD,
7827 (OMP_TEAMS_CLAUSES | OMP_DISTRIBUTE_CLAUSES
7828 | OMP_PARALLEL_CLAUSES | OMP_DO_CLAUSES
7829 | OMP_SIMD_CLAUSES)
7830 & ~(omp_mask (OMP_CLAUSE_ORDERED) | OMP_CLAUSE_NOWAIT));
7831 }
7832
7833
7834 match
7835 gfc_match_omp_teams_distribute_simd (void)
7836 {
7837 return match_omp (EXEC_OMP_TEAMS_DISTRIBUTE_SIMD,
7838 OMP_TEAMS_CLAUSES | OMP_DISTRIBUTE_CLAUSES
7839 | OMP_SIMD_CLAUSES);
7840 }
7841
7842 match
7843 gfc_match_omp_tile (void)
7844 {
7845 return match_omp (EXEC_OMP_TILE, OMP_TILE_CLAUSES);
7846 }
7847
7848 match
7849 gfc_match_omp_unroll (void)
7850 {
7851 return match_omp (EXEC_OMP_UNROLL, OMP_UNROLL_CLAUSES);
7852 }
7853
7854 match
7855 gfc_match_omp_workshare (void)
7856 {
7857 return match_omp (EXEC_OMP_WORKSHARE, OMP_WORKSHARE_CLAUSES);
7858 }
7859
7860
7861 match
7862 gfc_match_omp_masked (void)
7863 {
7864 return match_omp (EXEC_OMP_MASKED, OMP_MASKED_CLAUSES);
7865 }
7866
7867 match
7868 gfc_match_omp_masked_taskloop (void)
7869 {
7870 return match_omp (EXEC_OMP_MASKED_TASKLOOP,
7871 OMP_MASKED_CLAUSES | OMP_TASKLOOP_CLAUSES);
7872 }
7873
7874 match
7875 gfc_match_omp_masked_taskloop_simd (void)
7876 {
7877 return match_omp (EXEC_OMP_MASKED_TASKLOOP_SIMD,
7878 (OMP_MASKED_CLAUSES | OMP_TASKLOOP_CLAUSES
7879 | OMP_SIMD_CLAUSES));
7880 }
7881
7882 match
7883 gfc_match_omp_master (void)
7884 {
7885 if (gfc_match_omp_eos () != MATCH_YES)
7886 {
7887 gfc_error ("Unexpected junk after $OMP MASTER statement at %C");
7888 return MATCH_ERROR;
7889 }
7890 new_st.op = EXEC_OMP_MASTER;
7891 new_st.ext.omp_clauses = NULL;
7892 return MATCH_YES;
7893 }
7894
7895 match
7896 gfc_match_omp_master_taskloop (void)
7897 {
7898 return match_omp (EXEC_OMP_MASTER_TASKLOOP, OMP_TASKLOOP_CLAUSES);
7899 }
7900
7901 match
7902 gfc_match_omp_master_taskloop_simd (void)
7903 {
7904 return match_omp (EXEC_OMP_MASTER_TASKLOOP_SIMD,
7905 OMP_TASKLOOP_CLAUSES | OMP_SIMD_CLAUSES);
7906 }
7907
7908 match
7909 gfc_match_omp_ordered (void)
7910 {
7911 return match_omp (EXEC_OMP_ORDERED, OMP_ORDERED_CLAUSES);
7912 }
7913
7914 match
7915 gfc_match_omp_nothing (void)
7916 {
7917 if (gfc_match_omp_eos () != MATCH_YES)
7918 {
7919 gfc_error ("Unexpected junk after $OMP NOTHING statement at %C");
7920 return MATCH_ERROR;
7921 }
7922 /* Will use ST_NONE; therefore, no EXEC_OMP_ is needed. */
7923 return MATCH_YES;
7924 }
7925
7926 match
7927 gfc_match_omp_ordered_depend (void)
7928 {
7929 return match_omp (EXEC_OMP_ORDERED, omp_mask (OMP_CLAUSE_DOACROSS));
7930 }
7931
7932
7933 /* omp atomic [clause-list]
7934 - atomic-clause: read | write | update
7935 - capture
7936 - memory-order-clause: seq_cst | acq_rel | release | acquire | relaxed
7937 - hint(hint-expr)
7938 - OpenMP 5.1: compare | fail (seq_cst | acquire | relaxed ) | weak
7939 */
7940
7941 match
7942 gfc_match_omp_atomic (void)
7943 {
7944 gfc_omp_clauses *c;
7945 locus loc = gfc_current_locus;
7946
7947 if (gfc_match_omp_clauses (&c, OMP_ATOMIC_CLAUSES, true, true) != MATCH_YES)
7948 return MATCH_ERROR;
7949
7950 if (c->atomic_op == GFC_OMP_ATOMIC_UNSET)
7951 c->atomic_op = GFC_OMP_ATOMIC_UPDATE;
7952
7953 if (c->capture && c->atomic_op != GFC_OMP_ATOMIC_UPDATE)
7954 gfc_error ("!$OMP ATOMIC at %L with %s clause is incompatible with "
7955 "READ or WRITE", &loc, "CAPTURE");
7956 if (c->compare && c->atomic_op != GFC_OMP_ATOMIC_UPDATE)
7957 gfc_error ("!$OMP ATOMIC at %L with %s clause is incompatible with "
7958 "READ or WRITE", &loc, "COMPARE");
7959 if (c->fail != OMP_MEMORDER_UNSET && c->atomic_op != GFC_OMP_ATOMIC_UPDATE)
7960 gfc_error ("!$OMP ATOMIC at %L with %s clause is incompatible with "
7961 "READ or WRITE", &loc, "FAIL");
7962 if (c->weak && !c->compare)
7963 {
7964 gfc_error ("!$OMP ATOMIC at %L with %s clause requires %s clause", &loc,
7965 "WEAK", "COMPARE");
7966 c->weak = false;
7967 }
7968
7969 if (c->memorder == OMP_MEMORDER_UNSET)
7970 {
7971 gfc_namespace *prog_unit = gfc_current_ns;
7972 while (prog_unit->parent)
7973 prog_unit = prog_unit->parent;
7974 switch (prog_unit->omp_requires & OMP_REQ_ATOMIC_MEM_ORDER_MASK)
7975 {
7976 case 0:
7977 case OMP_REQ_ATOMIC_MEM_ORDER_RELAXED:
7978 c->memorder = OMP_MEMORDER_RELAXED;
7979 break;
7980 case OMP_REQ_ATOMIC_MEM_ORDER_SEQ_CST:
7981 c->memorder = OMP_MEMORDER_SEQ_CST;
7982 break;
7983 case OMP_REQ_ATOMIC_MEM_ORDER_ACQ_REL:
7984 if (c->capture)
7985 c->memorder = OMP_MEMORDER_ACQ_REL;
7986 else if (c->atomic_op == GFC_OMP_ATOMIC_READ)
7987 c->memorder = OMP_MEMORDER_ACQUIRE;
7988 else
7989 c->memorder = OMP_MEMORDER_RELEASE;
7990 break;
7991 case OMP_REQ_ATOMIC_MEM_ORDER_ACQUIRE:
7992 if (c->atomic_op == GFC_OMP_ATOMIC_WRITE)
7993 {
7994 gfc_error ("!$OMP ATOMIC WRITE at %L incompatible with "
7995 "ACQUIRES clause implicitly provided by a "
7996 "REQUIRES directive", &loc);
7997 c->memorder = OMP_MEMORDER_SEQ_CST;
7998 }
7999 else
8000 c->memorder = OMP_MEMORDER_ACQUIRE;
8001 break;
8002 case OMP_REQ_ATOMIC_MEM_ORDER_RELEASE:
8003 if (c->atomic_op == GFC_OMP_ATOMIC_READ)
8004 {
8005 gfc_error ("!$OMP ATOMIC READ at %L incompatible with "
8006 "RELEASE clause implicitly provided by a "
8007 "REQUIRES directive", &loc);
8008 c->memorder = OMP_MEMORDER_SEQ_CST;
8009 }
8010 else
8011 c->memorder = OMP_MEMORDER_RELEASE;
8012 break;
8013 default:
8014 gcc_unreachable ();
8015 }
8016 }
8017 else
8018 switch (c->atomic_op)
8019 {
8020 case GFC_OMP_ATOMIC_READ:
8021 if (c->memorder == OMP_MEMORDER_RELEASE)
8022 {
8023 gfc_error ("!$OMP ATOMIC READ at %L incompatible with "
8024 "RELEASE clause", &loc);
8025 c->memorder = OMP_MEMORDER_SEQ_CST;
8026 }
8027 else if (c->memorder == OMP_MEMORDER_ACQ_REL)
8028 c->memorder = OMP_MEMORDER_ACQUIRE;
8029 break;
8030 case GFC_OMP_ATOMIC_WRITE:
8031 if (c->memorder == OMP_MEMORDER_ACQUIRE)
8032 {
8033 gfc_error ("!$OMP ATOMIC WRITE at %L incompatible with "
8034 "ACQUIRE clause", &loc);
8035 c->memorder = OMP_MEMORDER_SEQ_CST;
8036 }
8037 else if (c->memorder == OMP_MEMORDER_ACQ_REL)
8038 c->memorder = OMP_MEMORDER_RELEASE;
8039 break;
8040 default:
8041 break;
8042 }
8043 gfc_error_check ();
8044 new_st.ext.omp_clauses = c;
8045 new_st.op = EXEC_OMP_ATOMIC;
8046 return MATCH_YES;
8047 }
8048
8049
8050 /* acc atomic [ read | write | update | capture] */
8051
8052 match
8053 gfc_match_oacc_atomic (void)
8054 {
8055 gfc_omp_clauses *c = gfc_get_omp_clauses ();
8056 c->atomic_op = GFC_OMP_ATOMIC_UPDATE;
8057 c->memorder = OMP_MEMORDER_RELAXED;
8058 gfc_gobble_whitespace ();
8059 if (gfc_match ("update") == MATCH_YES)
8060 ;
8061 else if (gfc_match ("read") == MATCH_YES)
8062 c->atomic_op = GFC_OMP_ATOMIC_READ;
8063 else if (gfc_match ("write") == MATCH_YES)
8064 c->atomic_op = GFC_OMP_ATOMIC_WRITE;
8065 else if (gfc_match ("capture") == MATCH_YES)
8066 c->capture = true;
8067 gfc_gobble_whitespace ();
8068 if (gfc_match_omp_eos () != MATCH_YES)
8069 {
8070 gfc_error ("Unexpected junk after !$ACC ATOMIC statement at %C");
8071 gfc_free_omp_clauses (c);
8072 return MATCH_ERROR;
8073 }
8074 new_st.ext.omp_clauses = c;
8075 new_st.op = EXEC_OACC_ATOMIC;
8076 return MATCH_YES;
8077 }
8078
8079
8080 match
8081 gfc_match_omp_barrier (void)
8082 {
8083 if (gfc_match_omp_eos () != MATCH_YES)
8084 {
8085 gfc_error ("Unexpected junk after $OMP BARRIER statement at %C");
8086 return MATCH_ERROR;
8087 }
8088 new_st.op = EXEC_OMP_BARRIER;
8089 new_st.ext.omp_clauses = NULL;
8090 return MATCH_YES;
8091 }
8092
8093
8094 match
8095 gfc_match_omp_taskgroup (void)
8096 {
8097 return match_omp (EXEC_OMP_TASKGROUP, OMP_TASKGROUP_CLAUSES);
8098 }
8099
8100
8101 static enum gfc_omp_cancel_kind
8102 gfc_match_omp_cancel_kind (void)
8103 {
8104 if (gfc_match_space () != MATCH_YES)
8105 return OMP_CANCEL_UNKNOWN;
8106 if (gfc_match ("parallel") == MATCH_YES)
8107 return OMP_CANCEL_PARALLEL;
8108 if (gfc_match ("sections") == MATCH_YES)
8109 return OMP_CANCEL_SECTIONS;
8110 if (gfc_match ("do") == MATCH_YES)
8111 return OMP_CANCEL_DO;
8112 if (gfc_match ("taskgroup") == MATCH_YES)
8113 return OMP_CANCEL_TASKGROUP;
8114 return OMP_CANCEL_UNKNOWN;
8115 }
8116
8117
8118 match
8119 gfc_match_omp_cancel (void)
8120 {
8121 gfc_omp_clauses *c;
8122 enum gfc_omp_cancel_kind kind = gfc_match_omp_cancel_kind ();
8123 if (kind == OMP_CANCEL_UNKNOWN)
8124 return MATCH_ERROR;
8125 if (gfc_match_omp_clauses (&c, omp_mask (OMP_CLAUSE_IF), false) != MATCH_YES)
8126 return MATCH_ERROR;
8127 c->cancel = kind;
8128 new_st.op = EXEC_OMP_CANCEL;
8129 new_st.ext.omp_clauses = c;
8130 return MATCH_YES;
8131 }
8132
8133
8134 match
8135 gfc_match_omp_cancellation_point (void)
8136 {
8137 gfc_omp_clauses *c;
8138 enum gfc_omp_cancel_kind kind = gfc_match_omp_cancel_kind ();
8139 if (kind == OMP_CANCEL_UNKNOWN)
8140 {
8141 gfc_error ("Expected construct-type PARALLEL, SECTIONS, DO or TASKGROUP "
8142 "in $OMP CANCELLATION POINT statement at %C");
8143 return MATCH_ERROR;
8144 }
8145 if (gfc_match_omp_eos () != MATCH_YES)
8146 {
8147 gfc_error ("Unexpected junk after $OMP CANCELLATION POINT statement "
8148 "at %C");
8149 return MATCH_ERROR;
8150 }
8151 c = gfc_get_omp_clauses ();
8152 c->cancel = kind;
8153 new_st.op = EXEC_OMP_CANCELLATION_POINT;
8154 new_st.ext.omp_clauses = c;
8155 return MATCH_YES;
8156 }
8157
8158
8159 match
8160 gfc_match_omp_end_nowait (void)
8161 {
8162 bool nowait = false;
8163 if (gfc_match ("% nowait") == MATCH_YES)
8164 nowait = true;
8165 if (gfc_match_omp_eos () != MATCH_YES)
8166 {
8167 if (nowait)
8168 gfc_error ("Unexpected junk after NOWAIT clause at %C");
8169 else
8170 gfc_error ("Unexpected junk at %C");
8171 return MATCH_ERROR;
8172 }
8173 new_st.op = EXEC_OMP_END_NOWAIT;
8174 new_st.ext.omp_bool = nowait;
8175 return MATCH_YES;
8176 }
8177
8178
8179 match
8180 gfc_match_omp_end_single (void)
8181 {
8182 gfc_omp_clauses *c;
8183 if (gfc_match_omp_clauses (&c, omp_mask (OMP_CLAUSE_COPYPRIVATE)
8184 | OMP_CLAUSE_NOWAIT) != MATCH_YES)
8185 return MATCH_ERROR;
8186 new_st.op = EXEC_OMP_END_SINGLE;
8187 new_st.ext.omp_clauses = c;
8188 return MATCH_YES;
8189 }
8190
8191
8192 static bool
8193 oacc_is_loop (gfc_code *code)
8194 {
8195 return code->op == EXEC_OACC_PARALLEL_LOOP
8196 || code->op == EXEC_OACC_KERNELS_LOOP
8197 || code->op == EXEC_OACC_SERIAL_LOOP
8198 || code->op == EXEC_OACC_LOOP;
8199 }
8200
8201 static void
8202 resolve_scalar_int_expr (gfc_expr *expr, const char *clause)
8203 {
8204 if (!gfc_resolve_expr (expr)
8205 || expr->ts.type != BT_INTEGER
8206 || expr->rank != 0)
8207 gfc_error ("%s clause at %L requires a scalar INTEGER expression",
8208 clause, &expr->where);
8209 }
8210
8211 static void
8212 resolve_positive_int_expr (gfc_expr *expr, const char *clause)
8213 {
8214 resolve_scalar_int_expr (expr, clause);
8215 if (expr->expr_type == EXPR_CONSTANT
8216 && expr->ts.type == BT_INTEGER
8217 && mpz_sgn (expr->value.integer) <= 0)
8218 gfc_warning ((flag_openmp || flag_openmp_simd) ? OPT_Wopenmp : 0,
8219 "INTEGER expression of %s clause at %L must be positive",
8220 clause, &expr->where);
8221 }
8222
8223 static void
8224 resolve_nonnegative_int_expr (gfc_expr *expr, const char *clause)
8225 {
8226 resolve_scalar_int_expr (expr, clause);
8227 if (expr->expr_type == EXPR_CONSTANT
8228 && expr->ts.type == BT_INTEGER
8229 && mpz_sgn (expr->value.integer) < 0)
8230 gfc_warning ((flag_openmp || flag_openmp_simd) ? OPT_Wopenmp : 0,
8231 "INTEGER expression of %s clause at %L must be non-negative",
8232 clause, &expr->where);
8233 }
8234
8235 /* Emits error when symbol is pointer, cray pointer or cray pointee
8236 of derived of polymorphic type. */
8237
8238 static void
8239 check_symbol_not_pointer (gfc_symbol *sym, locus loc, const char *name)
8240 {
8241 if (sym->ts.type == BT_DERIVED && sym->attr.cray_pointer)
8242 gfc_error ("Cray pointer object %qs of derived type in %s clause at %L",
8243 sym->name, name, &loc);
8244 if (sym->ts.type == BT_DERIVED && sym->attr.cray_pointee)
8245 gfc_error ("Cray pointee object %qs of derived type in %s clause at %L",
8246 sym->name, name, &loc);
8247
8248 if ((sym->ts.type == BT_ASSUMED && sym->attr.pointer)
8249 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
8250 && CLASS_DATA (sym)->attr.pointer))
8251 gfc_error ("POINTER object %qs of polymorphic type in %s clause at %L",
8252 sym->name, name, &loc);
8253 if ((sym->ts.type == BT_ASSUMED && sym->attr.cray_pointer)
8254 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
8255 && CLASS_DATA (sym)->attr.cray_pointer))
8256 gfc_error ("Cray pointer object %qs of polymorphic type in %s clause at %L",
8257 sym->name, name, &loc);
8258 if ((sym->ts.type == BT_ASSUMED && sym->attr.cray_pointee)
8259 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
8260 && CLASS_DATA (sym)->attr.cray_pointee))
8261 gfc_error ("Cray pointee object %qs of polymorphic type in %s clause at %L",
8262 sym->name, name, &loc);
8263 }
8264
8265 /* Emits error when symbol represents assumed size/rank array. */
8266
8267 static void
8268 check_array_not_assumed (gfc_symbol *sym, locus loc, const char *name)
8269 {
8270 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
8271 gfc_error ("Assumed size array %qs in %s clause at %L",
8272 sym->name, name, &loc);
8273 if (sym->as && sym->as->type == AS_ASSUMED_RANK)
8274 gfc_error ("Assumed rank array %qs in %s clause at %L",
8275 sym->name, name, &loc);
8276 }
8277
8278 static void
8279 resolve_oacc_data_clauses (gfc_symbol *sym, locus loc, const char *name)
8280 {
8281 check_array_not_assumed (sym, loc, name);
8282 }
8283
8284 static void
8285 resolve_oacc_deviceptr_clause (gfc_symbol *sym, locus loc, const char *name)
8286 {
8287 if (sym->attr.pointer
8288 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
8289 && CLASS_DATA (sym)->attr.class_pointer))
8290 gfc_error ("POINTER object %qs in %s clause at %L",
8291 sym->name, name, &loc);
8292 if (sym->attr.cray_pointer
8293 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
8294 && CLASS_DATA (sym)->attr.cray_pointer))
8295 gfc_error ("Cray pointer object %qs in %s clause at %L",
8296 sym->name, name, &loc);
8297 if (sym->attr.cray_pointee
8298 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
8299 && CLASS_DATA (sym)->attr.cray_pointee))
8300 gfc_error ("Cray pointee object %qs in %s clause at %L",
8301 sym->name, name, &loc);
8302 if (sym->attr.allocatable
8303 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
8304 && CLASS_DATA (sym)->attr.allocatable))
8305 gfc_error ("ALLOCATABLE object %qs in %s clause at %L",
8306 sym->name, name, &loc);
8307 if (sym->attr.value)
8308 gfc_error ("VALUE object %qs in %s clause at %L",
8309 sym->name, name, &loc);
8310 check_array_not_assumed (sym, loc, name);
8311 }
8312
8313
8314 struct resolve_omp_udr_callback_data
8315 {
8316 gfc_symbol *sym1, *sym2;
8317 };
8318
8319
8320 static int
8321 resolve_omp_udr_callback (gfc_expr **e, int *, void *data)
8322 {
8323 struct resolve_omp_udr_callback_data *rcd
8324 = (struct resolve_omp_udr_callback_data *) data;
8325 if ((*e)->expr_type == EXPR_VARIABLE
8326 && ((*e)->symtree->n.sym == rcd->sym1
8327 || (*e)->symtree->n.sym == rcd->sym2))
8328 {
8329 gfc_ref *ref = gfc_get_ref ();
8330 ref->type = REF_ARRAY;
8331 ref->u.ar.where = (*e)->where;
8332 ref->u.ar.as = (*e)->symtree->n.sym->as;
8333 ref->u.ar.type = AR_FULL;
8334 ref->u.ar.dimen = 0;
8335 ref->next = (*e)->ref;
8336 (*e)->ref = ref;
8337 }
8338 return 0;
8339 }
8340
8341
8342 static int
8343 resolve_omp_udr_callback2 (gfc_expr **e, int *, void *)
8344 {
8345 if ((*e)->expr_type == EXPR_FUNCTION
8346 && (*e)->value.function.isym == NULL)
8347 {
8348 gfc_symbol *sym = (*e)->symtree->n.sym;
8349 if (!sym->attr.intrinsic
8350 && sym->attr.if_source == IFSRC_UNKNOWN)
8351 gfc_error ("Implicitly declared function %s used in "
8352 "!$OMP DECLARE REDUCTION at %L", sym->name, &(*e)->where);
8353 }
8354 return 0;
8355 }
8356
8357
8358 static gfc_code *
8359 resolve_omp_udr_clause (gfc_omp_namelist *n, gfc_namespace *ns,
8360 gfc_symbol *sym1, gfc_symbol *sym2)
8361 {
8362 gfc_code *copy;
8363 gfc_symbol sym1_copy, sym2_copy;
8364
8365 if (ns->code->op == EXEC_ASSIGN)
8366 {
8367 copy = gfc_get_code (EXEC_ASSIGN);
8368 copy->expr1 = gfc_copy_expr (ns->code->expr1);
8369 copy->expr2 = gfc_copy_expr (ns->code->expr2);
8370 }
8371 else
8372 {
8373 copy = gfc_get_code (EXEC_CALL);
8374 copy->symtree = ns->code->symtree;
8375 copy->ext.actual = gfc_copy_actual_arglist (ns->code->ext.actual);
8376 }
8377 copy->loc = ns->code->loc;
8378 sym1_copy = *sym1;
8379 sym2_copy = *sym2;
8380 *sym1 = *n->sym;
8381 *sym2 = *n->sym;
8382 sym1->name = sym1_copy.name;
8383 sym2->name = sym2_copy.name;
8384 ns->proc_name = ns->parent->proc_name;
8385 if (n->sym->attr.dimension)
8386 {
8387 struct resolve_omp_udr_callback_data rcd;
8388 rcd.sym1 = sym1;
8389 rcd.sym2 = sym2;
8390 gfc_code_walker (&copy, gfc_dummy_code_callback,
8391 resolve_omp_udr_callback, &rcd);
8392 }
8393 gfc_resolve_code (copy, gfc_current_ns);
8394 if (copy->op == EXEC_CALL && copy->resolved_isym == NULL)
8395 {
8396 gfc_symbol *sym = copy->resolved_sym;
8397 if (sym
8398 && !sym->attr.intrinsic
8399 && sym->attr.if_source == IFSRC_UNKNOWN)
8400 gfc_error ("Implicitly declared subroutine %s used in "
8401 "!$OMP DECLARE REDUCTION at %L", sym->name,
8402 &copy->loc);
8403 }
8404 gfc_code_walker (&copy, gfc_dummy_code_callback,
8405 resolve_omp_udr_callback2, NULL);
8406 *sym1 = sym1_copy;
8407 *sym2 = sym2_copy;
8408 return copy;
8409 }
8410
8411 /* Assume that a constant expression in the range 1 (omp_default_mem_alloc)
8412 to 8 (omp_thread_mem_alloc) range, or 200 (ompx_gnu_pinned_mem_alloc) is
8413 fine. The original symbol name is already lost during matching via
8414 gfc_match_expr. */
8415 static bool
8416 is_predefined_allocator (gfc_expr *expr)
8417 {
8418 return (gfc_resolve_expr (expr)
8419 && expr->rank == 0
8420 && expr->ts.type == BT_INTEGER
8421 && expr->ts.kind == gfc_c_intptr_kind
8422 && expr->expr_type == EXPR_CONSTANT
8423 && ((mpz_sgn (expr->value.integer) > 0
8424 && mpz_cmp_si (expr->value.integer,
8425 GOMP_OMP_PREDEF_ALLOC_MAX) <= 0)
8426 || (mpz_cmp_si (expr->value.integer,
8427 GOMP_OMPX_PREDEF_ALLOC_MIN) >= 0
8428 && mpz_cmp_si (expr->value.integer,
8429 GOMP_OMPX_PREDEF_ALLOC_MAX) <= 0)));
8430 }
8431
8432 /* Resolve declarative ALLOCATE statement. Note: Common block vars only appear
8433 as /block/ not individual, which is ensured during parsing. */
8434
8435 void
8436 gfc_resolve_omp_allocate (gfc_namespace *ns, gfc_omp_namelist *list)
8437 {
8438 for (gfc_omp_namelist *n = list; n; n = n->next)
8439 {
8440 if (n->sym->attr.result || n->sym->result == n->sym)
8441 {
8442 gfc_error ("Unexpected function-result variable %qs at %L in "
8443 "declarative !$OMP ALLOCATE", n->sym->name, &n->where);
8444 continue;
8445 }
8446 if (ns->omp_allocate->sym->attr.proc_pointer)
8447 {
8448 gfc_error ("Procedure pointer %qs not supported with !$OMP "
8449 "ALLOCATE at %L", n->sym->name, &n->where);
8450 continue;
8451 }
8452 if (n->sym->attr.flavor != FL_VARIABLE)
8453 {
8454 gfc_error ("Argument %qs at %L to declarative !$OMP ALLOCATE "
8455 "directive must be a variable", n->sym->name,
8456 &n->where);
8457 continue;
8458 }
8459 if (ns != n->sym->ns || n->sym->attr.use_assoc || n->sym->attr.imported)
8460 {
8461 gfc_error ("Argument %qs at %L to declarative !$OMP ALLOCATE shall be"
8462 " in the same scope as the variable declaration",
8463 n->sym->name, &n->where);
8464 continue;
8465 }
8466 if (n->sym->attr.dummy)
8467 {
8468 gfc_error ("Unexpected dummy argument %qs as argument at %L to "
8469 "declarative !$OMP ALLOCATE", n->sym->name, &n->where);
8470 continue;
8471 }
8472 if (n->sym->attr.codimension)
8473 {
8474 gfc_error ("Unexpected coarray argument %qs as argument at %L to "
8475 "declarative !$OMP ALLOCATE", n->sym->name, &n->where);
8476 continue;
8477 }
8478 if (n->sym->attr.omp_allocate)
8479 {
8480 if (n->sym->attr.in_common)
8481 {
8482 gfc_error ("Duplicated common block %</%s/%> in !$OMP ALLOCATE "
8483 "at %L", n->sym->common_head->name, &n->where);
8484 while (n->next && n->next->sym
8485 && n->sym->common_head == n->next->sym->common_head)
8486 n = n->next;
8487 }
8488 else
8489 gfc_error ("Duplicated variable %qs in !$OMP ALLOCATE at %L",
8490 n->sym->name, &n->where);
8491 continue;
8492 }
8493 /* For 'equivalence(a,b)', a 'union_type {<type> a,b} equiv.0' is created
8494 with a value expression for 'a' as 'equiv.0.a' (likewise for b); while
8495 this can be handled, EQUIVALENCE is marked as obsolescent since Fortran
8496 2018 and also not widely used. However, it could be supported,
8497 if needed. */
8498 if (n->sym->attr.in_equivalence)
8499 {
8500 gfc_error ("Sorry, EQUIVALENCE object %qs not supported with !$OMP "
8501 "ALLOCATE at %L", n->sym->name, &n->where);
8502 continue;
8503 }
8504 /* Similar for Cray pointer/pointee - they could be implemented but as
8505 common vendor extension but nowadays rarely used and requiring
8506 -fcray-pointer, there is no need to support them. */
8507 if (n->sym->attr.cray_pointer || n->sym->attr.cray_pointee)
8508 {
8509 gfc_error ("Sorry, Cray pointers and pointees such as %qs are not "
8510 "supported with !$OMP ALLOCATE at %L",
8511 n->sym->name, &n->where);
8512 continue;
8513 }
8514 n->sym->attr.omp_allocate = 1;
8515 if ((n->sym->ts.type == BT_CLASS && n->sym->attr.class_ok
8516 && CLASS_DATA (n->sym)->attr.allocatable)
8517 || (n->sym->ts.type != BT_CLASS && n->sym->attr.allocatable))
8518 gfc_error ("Unexpected allocatable variable %qs at %L in declarative "
8519 "!$OMP ALLOCATE directive", n->sym->name, &n->where);
8520 else if ((n->sym->ts.type == BT_CLASS && n->sym->attr.class_ok
8521 && CLASS_DATA (n->sym)->attr.class_pointer)
8522 || (n->sym->ts.type != BT_CLASS && n->sym->attr.pointer))
8523 gfc_error ("Unexpected pointer variable %qs at %L in declarative "
8524 "!$OMP ALLOCATE directive", n->sym->name, &n->where);
8525 HOST_WIDE_INT alignment = 0;
8526 if (n->u.align
8527 && (!gfc_resolve_expr (n->u.align)
8528 || n->u.align->ts.type != BT_INTEGER
8529 || n->u.align->rank != 0
8530 || n->u.align->expr_type != EXPR_CONSTANT
8531 || gfc_extract_hwi (n->u.align, &alignment)
8532 || !pow2p_hwi (alignment)))
8533 {
8534 gfc_error ("ALIGN requires a scalar positive constant integer "
8535 "alignment expression at %L that is a power of two",
8536 &n->u.align->where);
8537 while (n->sym->attr.in_common && n->next && n->next->sym
8538 && n->sym->common_head == n->next->sym->common_head)
8539 n = n->next;
8540 continue;
8541 }
8542 if (n->sym->attr.in_common || n->sym->attr.save || n->sym->ns->save_all
8543 || (n->sym->ns->proc_name
8544 && (n->sym->ns->proc_name->attr.flavor == FL_PROGRAM
8545 || n->sym->ns->proc_name->attr.flavor == FL_MODULE)))
8546 {
8547 bool com = n->sym->attr.in_common;
8548 if (!n->u2.allocator)
8549 gfc_error ("An ALLOCATOR clause is required as the list item "
8550 "%<%s%s%s%> at %L has the SAVE attribute", com ? "/" : "",
8551 com ? n->sym->common_head->name : n->sym->name,
8552 com ? "/" : "", &n->where);
8553 else if (!is_predefined_allocator (n->u2.allocator))
8554 gfc_error ("Predefined allocator required in ALLOCATOR clause at %L"
8555 " as the list item %<%s%s%s%> at %L has the SAVE attribute",
8556 &n->u2.allocator->where, com ? "/" : "",
8557 com ? n->sym->common_head->name : n->sym->name,
8558 com ? "/" : "", &n->where);
8559 while (n->sym->attr.in_common && n->next && n->next->sym
8560 && n->sym->common_head == n->next->sym->common_head)
8561 n = n->next;
8562 }
8563 else if (n->u2.allocator
8564 && (!gfc_resolve_expr (n->u2.allocator)
8565 || n->u2.allocator->ts.type != BT_INTEGER
8566 || n->u2.allocator->rank != 0
8567 || n->u2.allocator->ts.kind != gfc_c_intptr_kind))
8568 gfc_error ("Expected integer expression of the "
8569 "%<omp_allocator_handle_kind%> kind at %L",
8570 &n->u2.allocator->where);
8571 }
8572 }
8573
8574 /* Resolve ASSUME's and ASSUMES' assumption clauses. Note that absent/contains
8575 is handled during parse time in omp_verify_merge_absent_contains. */
8576
8577 void
8578 gfc_resolve_omp_assumptions (gfc_omp_assumptions *assume)
8579 {
8580 for (gfc_expr_list *el = assume->holds; el; el = el->next)
8581 if (!gfc_resolve_expr (el->expr)
8582 || el->expr->ts.type != BT_LOGICAL
8583 || el->expr->rank != 0)
8584 gfc_error ("HOLDS expression at %L must be a scalar logical expression",
8585 &el->expr->where);
8586 }
8587
8588
8589 /* OpenMP directive resolving routines. */
8590
8591 static void
8592 resolve_omp_clauses (gfc_code *code, gfc_omp_clauses *omp_clauses,
8593 gfc_namespace *ns, bool openacc = false)
8594 {
8595 gfc_omp_namelist *n, *last;
8596 gfc_expr_list *el;
8597 int list;
8598 int ifc;
8599 bool if_without_mod = false;
8600 gfc_omp_linear_op linear_op = OMP_LINEAR_DEFAULT;
8601 static const char *clause_names[]
8602 = { "PRIVATE", "FIRSTPRIVATE", "LASTPRIVATE", "COPYPRIVATE", "SHARED",
8603 "COPYIN", "UNIFORM", "AFFINITY", "ALIGNED", "LINEAR", "DEPEND", "MAP",
8604 "TO", "FROM", "INCLUSIVE", "EXCLUSIVE",
8605 "REDUCTION", "REDUCTION" /*inscan*/, "REDUCTION" /*task*/,
8606 "IN_REDUCTION", "TASK_REDUCTION",
8607 "DEVICE_RESIDENT", "LINK", "USE_DEVICE",
8608 "CACHE", "IS_DEVICE_PTR", "USE_DEVICE_PTR", "USE_DEVICE_ADDR",
8609 "NONTEMPORAL", "ALLOCATE", "HAS_DEVICE_ADDR", "ENTER",
8610 "USES_ALLOCATORS", "INIT", "USE", "DESTROY", "INTEROP", "ADJUST_ARGS" };
8611 STATIC_ASSERT (ARRAY_SIZE (clause_names) == OMP_LIST_NUM);
8612
8613 if (omp_clauses == NULL)
8614 return;
8615
8616 if (ns == NULL)
8617 ns = gfc_current_ns;
8618
8619 if (omp_clauses->orderedc && omp_clauses->orderedc < omp_clauses->collapse)
8620 gfc_error ("ORDERED clause parameter is less than COLLAPSE at %L",
8621 &code->loc);
8622 if (omp_clauses->order_concurrent && omp_clauses->ordered)
8623 gfc_error ("ORDER clause must not be used together with ORDERED at %L",
8624 &code->loc);
8625 if (omp_clauses->if_expr)
8626 {
8627 gfc_expr *expr = omp_clauses->if_expr;
8628 if (!gfc_resolve_expr (expr)
8629 || expr->ts.type != BT_LOGICAL || expr->rank != 0)
8630 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8631 &expr->where);
8632 if_without_mod = true;
8633 }
8634 for (ifc = 0; ifc < OMP_IF_LAST; ifc++)
8635 if (omp_clauses->if_exprs[ifc])
8636 {
8637 gfc_expr *expr = omp_clauses->if_exprs[ifc];
8638 bool ok = true;
8639 if (!gfc_resolve_expr (expr)
8640 || expr->ts.type != BT_LOGICAL || expr->rank != 0)
8641 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8642 &expr->where);
8643 else if (if_without_mod)
8644 {
8645 gfc_error ("IF clause without modifier at %L used together with "
8646 "IF clauses with modifiers",
8647 &omp_clauses->if_expr->where);
8648 if_without_mod = false;
8649 }
8650 else
8651 switch (code->op)
8652 {
8653 case EXEC_OMP_CANCEL:
8654 ok = ifc == OMP_IF_CANCEL;
8655 break;
8656
8657 case EXEC_OMP_PARALLEL:
8658 case EXEC_OMP_PARALLEL_DO:
8659 case EXEC_OMP_PARALLEL_LOOP:
8660 case EXEC_OMP_PARALLEL_MASKED:
8661 case EXEC_OMP_PARALLEL_MASTER:
8662 case EXEC_OMP_PARALLEL_SECTIONS:
8663 case EXEC_OMP_PARALLEL_WORKSHARE:
8664 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
8665 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
8666 ok = ifc == OMP_IF_PARALLEL;
8667 break;
8668
8669 case EXEC_OMP_PARALLEL_DO_SIMD:
8670 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
8671 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
8672 ok = ifc == OMP_IF_PARALLEL || ifc == OMP_IF_SIMD;
8673 break;
8674
8675 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
8676 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
8677 ok = ifc == OMP_IF_PARALLEL || ifc == OMP_IF_TASKLOOP;
8678 break;
8679
8680 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
8681 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
8682 ok = (ifc == OMP_IF_PARALLEL
8683 || ifc == OMP_IF_TASKLOOP
8684 || ifc == OMP_IF_SIMD);
8685 break;
8686
8687 case EXEC_OMP_SIMD:
8688 case EXEC_OMP_DO_SIMD:
8689 case EXEC_OMP_DISTRIBUTE_SIMD:
8690 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
8691 ok = ifc == OMP_IF_SIMD;
8692 break;
8693
8694 case EXEC_OMP_TASK:
8695 ok = ifc == OMP_IF_TASK;
8696 break;
8697
8698 case EXEC_OMP_TASKLOOP:
8699 case EXEC_OMP_MASKED_TASKLOOP:
8700 case EXEC_OMP_MASTER_TASKLOOP:
8701 ok = ifc == OMP_IF_TASKLOOP;
8702 break;
8703
8704 case EXEC_OMP_TASKLOOP_SIMD:
8705 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
8706 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
8707 ok = ifc == OMP_IF_TASKLOOP || ifc == OMP_IF_SIMD;
8708 break;
8709
8710 case EXEC_OMP_TARGET:
8711 case EXEC_OMP_TARGET_TEAMS:
8712 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
8713 case EXEC_OMP_TARGET_TEAMS_LOOP:
8714 ok = ifc == OMP_IF_TARGET;
8715 break;
8716
8717 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
8718 case EXEC_OMP_TARGET_SIMD:
8719 ok = ifc == OMP_IF_TARGET || ifc == OMP_IF_SIMD;
8720 break;
8721
8722 case EXEC_OMP_TARGET_DATA:
8723 ok = ifc == OMP_IF_TARGET_DATA;
8724 break;
8725
8726 case EXEC_OMP_TARGET_UPDATE:
8727 ok = ifc == OMP_IF_TARGET_UPDATE;
8728 break;
8729
8730 case EXEC_OMP_TARGET_ENTER_DATA:
8731 ok = ifc == OMP_IF_TARGET_ENTER_DATA;
8732 break;
8733
8734 case EXEC_OMP_TARGET_EXIT_DATA:
8735 ok = ifc == OMP_IF_TARGET_EXIT_DATA;
8736 break;
8737
8738 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
8739 case EXEC_OMP_TARGET_PARALLEL:
8740 case EXEC_OMP_TARGET_PARALLEL_DO:
8741 case EXEC_OMP_TARGET_PARALLEL_LOOP:
8742 ok = ifc == OMP_IF_TARGET || ifc == OMP_IF_PARALLEL;
8743 break;
8744
8745 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
8746 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
8747 ok = (ifc == OMP_IF_TARGET
8748 || ifc == OMP_IF_PARALLEL
8749 || ifc == OMP_IF_SIMD);
8750 break;
8751
8752 default:
8753 ok = false;
8754 break;
8755 }
8756 if (!ok)
8757 {
8758 static const char *ifs[] = {
8759 "CANCEL",
8760 "PARALLEL",
8761 "SIMD",
8762 "TASK",
8763 "TASKLOOP",
8764 "TARGET",
8765 "TARGET DATA",
8766 "TARGET UPDATE",
8767 "TARGET ENTER DATA",
8768 "TARGET EXIT DATA"
8769 };
8770 gfc_error ("IF clause modifier %s at %L not appropriate for "
8771 "the current OpenMP construct", ifs[ifc], &expr->where);
8772 }
8773 }
8774
8775 if (omp_clauses->self_expr)
8776 {
8777 gfc_expr *expr = omp_clauses->self_expr;
8778 if (!gfc_resolve_expr (expr)
8779 || expr->ts.type != BT_LOGICAL || expr->rank != 0)
8780 gfc_error ("SELF clause at %L requires a scalar LOGICAL expression",
8781 &expr->where);
8782 }
8783
8784 if (omp_clauses->final_expr)
8785 {
8786 gfc_expr *expr = omp_clauses->final_expr;
8787 if (!gfc_resolve_expr (expr)
8788 || expr->ts.type != BT_LOGICAL || expr->rank != 0)
8789 gfc_error ("FINAL clause at %L requires a scalar LOGICAL expression",
8790 &expr->where);
8791 }
8792 if (omp_clauses->novariants)
8793 {
8794 gfc_expr *expr = omp_clauses->novariants;
8795 if (!gfc_resolve_expr (expr) || expr->ts.type != BT_LOGICAL
8796 || expr->rank != 0)
8797 gfc_error (
8798 "NOVARIANTS clause at %L requires a scalar LOGICAL expression",
8799 &expr->where);
8800 if_without_mod = true;
8801 }
8802 if (omp_clauses->nocontext)
8803 {
8804 gfc_expr *expr = omp_clauses->nocontext;
8805 if (!gfc_resolve_expr (expr) || expr->ts.type != BT_LOGICAL
8806 || expr->rank != 0)
8807 gfc_error (
8808 "NOCONTEXT clause at %L requires a scalar LOGICAL expression",
8809 &expr->where);
8810 if_without_mod = true;
8811 }
8812 if (omp_clauses->num_threads)
8813 resolve_positive_int_expr (omp_clauses->num_threads, "NUM_THREADS");
8814 if (omp_clauses->chunk_size)
8815 {
8816 gfc_expr *expr = omp_clauses->chunk_size;
8817 if (!gfc_resolve_expr (expr)
8818 || expr->ts.type != BT_INTEGER || expr->rank != 0)
8819 gfc_error ("SCHEDULE clause's chunk_size at %L requires "
8820 "a scalar INTEGER expression", &expr->where);
8821 else if (expr->expr_type == EXPR_CONSTANT
8822 && expr->ts.type == BT_INTEGER
8823 && mpz_sgn (expr->value.integer) <= 0)
8824 gfc_warning (OPT_Wopenmp, "INTEGER expression of SCHEDULE clause's "
8825 "chunk_size at %L must be positive", &expr->where);
8826 }
8827 if (omp_clauses->sched_kind != OMP_SCHED_NONE
8828 && omp_clauses->sched_nonmonotonic)
8829 {
8830 if (omp_clauses->sched_monotonic)
8831 gfc_error ("Both MONOTONIC and NONMONOTONIC schedule modifiers "
8832 "specified at %L", &code->loc);
8833 else if (omp_clauses->ordered)
8834 gfc_error ("NONMONOTONIC schedule modifier specified with ORDERED "
8835 "clause at %L", &code->loc);
8836 }
8837
8838 if (omp_clauses->depobj
8839 && (!gfc_resolve_expr (omp_clauses->depobj)
8840 || omp_clauses->depobj->ts.type != BT_INTEGER
8841 || omp_clauses->depobj->ts.kind != 2 * gfc_index_integer_kind
8842 || omp_clauses->depobj->rank != 0))
8843 gfc_error ("DEPOBJ in DEPOBJ construct at %L shall be a scalar integer "
8844 "of OMP_DEPEND_KIND kind", &omp_clauses->depobj->where);
8845
8846 /* Check that no symbol appears on multiple clauses, except that
8847 a symbol can appear on both firstprivate and lastprivate. */
8848 for (list = 0; list < OMP_LIST_NUM; list++)
8849 for (n = omp_clauses->lists[list]; n; n = n->next)
8850 {
8851 if (!n->sym) /* omp_all_memory. */
8852 continue;
8853 n->sym->mark = 0;
8854 n->sym->comp_mark = 0;
8855 n->sym->data_mark = 0;
8856 n->sym->dev_mark = 0;
8857 n->sym->gen_mark = 0;
8858 n->sym->reduc_mark = 0;
8859 if (n->sym->attr.flavor == FL_VARIABLE
8860 || n->sym->attr.proc_pointer
8861 || (!code && (!n->sym->attr.dummy || n->sym->ns != ns)))
8862 {
8863 if (!code && (!n->sym->attr.dummy || n->sym->ns != ns))
8864 gfc_error ("Variable %qs is not a dummy argument at %L",
8865 n->sym->name, &n->where);
8866 continue;
8867 }
8868 if (n->sym->attr.flavor == FL_PROCEDURE
8869 && n->sym->result == n->sym
8870 && n->sym->attr.function)
8871 {
8872 if (ns->proc_name == n->sym
8873 || (ns->parent && ns->parent->proc_name == n->sym))
8874 continue;
8875 if (ns->proc_name->attr.entry_master)
8876 {
8877 gfc_entry_list *el = ns->entries;
8878 for (; el; el = el->next)
8879 if (el->sym == n->sym)
8880 break;
8881 if (el)
8882 continue;
8883 }
8884 if (ns->parent
8885 && ns->parent->proc_name->attr.entry_master)
8886 {
8887 gfc_entry_list *el = ns->parent->entries;
8888 for (; el; el = el->next)
8889 if (el->sym == n->sym)
8890 break;
8891 if (el)
8892 continue;
8893 }
8894 }
8895 if (list == OMP_LIST_MAP
8896 && n->sym->attr.flavor == FL_PARAMETER)
8897 {
8898 if (openacc)
8899 gfc_error ("Object %qs is not a variable at %L; parameters"
8900 " cannot be and need not be copied", n->sym->name,
8901 &n->where);
8902 else
8903 gfc_error ("Object %qs is not a variable at %L; parameters"
8904 " cannot be and need not be mapped", n->sym->name,
8905 &n->where);
8906 }
8907 else if (list != OMP_LIST_USES_ALLOCATORS)
8908 gfc_error ("Object %qs is not a variable at %L", n->sym->name,
8909 &n->where);
8910 }
8911 if (omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN])
8912 {
8913 locus *loc = &omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN]->where;
8914 if (code->op != EXEC_OMP_DO
8915 && code->op != EXEC_OMP_SIMD
8916 && code->op != EXEC_OMP_DO_SIMD
8917 && code->op != EXEC_OMP_PARALLEL_DO
8918 && code->op != EXEC_OMP_PARALLEL_DO_SIMD)
8919 gfc_error ("%<inscan%> REDUCTION clause on construct other than DO, "
8920 "SIMD, DO SIMD, PARALLEL DO, PARALLEL DO SIMD at %L",
8921 loc);
8922 if (omp_clauses->ordered)
8923 gfc_error ("ORDERED clause specified together with %<inscan%> "
8924 "REDUCTION clause at %L", loc);
8925 if (omp_clauses->sched_kind != OMP_SCHED_NONE)
8926 gfc_error ("SCHEDULE clause specified together with %<inscan%> "
8927 "REDUCTION clause at %L", loc);
8928 }
8929
8930 for (list = 0; list < OMP_LIST_NUM; list++)
8931 if (list != OMP_LIST_FIRSTPRIVATE
8932 && list != OMP_LIST_LASTPRIVATE
8933 && list != OMP_LIST_ALIGNED
8934 && list != OMP_LIST_DEPEND
8935 && list != OMP_LIST_FROM
8936 && list != OMP_LIST_TO
8937 && list != OMP_LIST_INTEROP
8938 && (list != OMP_LIST_REDUCTION || !openacc)
8939 && list != OMP_LIST_ALLOCATE)
8940 for (n = omp_clauses->lists[list]; n; n = n->next)
8941 {
8942 bool component_ref_p = false;
8943
8944 /* Allow multiple components of the same (e.g. derived-type)
8945 variable here. Duplicate components are detected elsewhere. */
8946 if (n->expr && n->expr->expr_type == EXPR_VARIABLE)
8947 for (gfc_ref *ref = n->expr->ref; ref; ref = ref->next)
8948 if (ref->type == REF_COMPONENT)
8949 component_ref_p = true;
8950 if ((list == OMP_LIST_IS_DEVICE_PTR
8951 || list == OMP_LIST_HAS_DEVICE_ADDR)
8952 && !component_ref_p)
8953 {
8954 if (n->sym->gen_mark
8955 || n->sym->dev_mark
8956 || n->sym->reduc_mark
8957 || n->sym->mark)
8958 gfc_error ("Symbol %qs present on multiple clauses at %L",
8959 n->sym->name, &n->where);
8960 else
8961 n->sym->dev_mark = 1;
8962 }
8963 else if ((list == OMP_LIST_USE_DEVICE_PTR
8964 || list == OMP_LIST_USE_DEVICE_ADDR
8965 || list == OMP_LIST_PRIVATE
8966 || list == OMP_LIST_SHARED)
8967 && !component_ref_p)
8968 {
8969 if (n->sym->gen_mark || n->sym->dev_mark || n->sym->reduc_mark)
8970 gfc_error ("Symbol %qs present on multiple clauses at %L",
8971 n->sym->name, &n->where);
8972 else
8973 {
8974 n->sym->gen_mark = 1;
8975 /* Set both generic and device bits if we have
8976 use_device_*(x) or shared(x). This allows us to diagnose
8977 "map(x) private(x)" below. */
8978 if (list != OMP_LIST_PRIVATE)
8979 n->sym->dev_mark = 1;
8980 }
8981 }
8982 else if ((list == OMP_LIST_REDUCTION
8983 || list == OMP_LIST_REDUCTION_TASK
8984 || list == OMP_LIST_REDUCTION_INSCAN
8985 || list == OMP_LIST_IN_REDUCTION
8986 || list == OMP_LIST_TASK_REDUCTION)
8987 && !component_ref_p)
8988 {
8989 /* Attempts to mix reduction types are diagnosed below. */
8990 if (n->sym->gen_mark || n->sym->dev_mark)
8991 gfc_error ("Symbol %qs present on multiple clauses at %L",
8992 n->sym->name, &n->where);
8993 n->sym->reduc_mark = 1;
8994 }
8995 else if ((!component_ref_p && n->sym->comp_mark)
8996 || (component_ref_p && n->sym->mark))
8997 {
8998 if (openacc)
8999 gfc_error ("Symbol %qs has mixed component and non-component "
9000 "accesses at %L", n->sym->name, &n->where);
9001 }
9002 else if (n->sym->mark)
9003 gfc_error ("Symbol %qs present on multiple clauses at %L",
9004 n->sym->name, &n->where);
9005 else
9006 {
9007 if (component_ref_p)
9008 n->sym->comp_mark = 1;
9009 else
9010 n->sym->mark = 1;
9011 }
9012 }
9013
9014 if (code
9015 && code->op == EXEC_OMP_INTEROP
9016 && omp_clauses->lists[OMP_LIST_DEPEND])
9017 {
9018 if (!omp_clauses->lists[OMP_LIST_INIT]
9019 && !omp_clauses->lists[OMP_LIST_USE]
9020 && !omp_clauses->lists[OMP_LIST_DESTROY])
9021 {
9022 gfc_error ("DEPEND clause at %L requires action clause with "
9023 "%<targetsync%> interop-type",
9024 &omp_clauses->lists[OMP_LIST_DEPEND]->where);
9025 }
9026 for (n = omp_clauses->lists[OMP_LIST_INIT]; n; n = n->next)
9027 if (!n->u.init.targetsync)
9028 {
9029 gfc_error ("DEPEND clause at %L requires %<targetsync%> "
9030 "interop-type, lacking it for %qs at %L",
9031 &omp_clauses->lists[OMP_LIST_DEPEND]->where,
9032 n->sym->name, &n->where);
9033 break;
9034 }
9035 }
9036 if (code && (code->op == EXEC_OMP_INTEROP || code->op == EXEC_OMP_DISPATCH))
9037 for (list = OMP_LIST_INIT; list <= OMP_LIST_INTEROP; list++)
9038 for (n = omp_clauses->lists[list]; n; n = n->next)
9039 {
9040 if (n->sym->ts.type != BT_INTEGER
9041 || n->sym->ts.kind != gfc_index_integer_kind
9042 || n->sym->attr.dimension
9043 || n->sym->attr.flavor != FL_VARIABLE)
9044 gfc_error ("%qs at %L in %qs clause must be a scalar integer "
9045 "variable of %<omp_interop_kind%> kind", n->sym->name,
9046 &n->where, clause_names[list]);
9047 if (list != OMP_LIST_USE && list != OMP_LIST_INTEROP
9048 && n->sym->attr.intent == INTENT_IN)
9049 gfc_error ("%qs at %L in %qs clause must be definable",
9050 n->sym->name, &n->where, clause_names[list]);
9051 }
9052
9053 /* Detect specifically the case where we have "map(x) private(x)" and raise
9054 an error. If we have "...simd" combined directives though, the "private"
9055 applies to the simd part, so this is permitted though. */
9056 for (n = omp_clauses->lists[OMP_LIST_PRIVATE]; n; n = n->next)
9057 if (n->sym->mark
9058 && n->sym->gen_mark
9059 && !n->sym->dev_mark
9060 && !n->sym->reduc_mark
9061 && code->op != EXEC_OMP_TARGET_SIMD
9062 && code->op != EXEC_OMP_TARGET_PARALLEL_DO_SIMD
9063 && code->op != EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
9064 && code->op != EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD)
9065 gfc_error ("Symbol %qs present on multiple clauses at %L",
9066 n->sym->name, &n->where);
9067
9068 gcc_assert (OMP_LIST_LASTPRIVATE == OMP_LIST_FIRSTPRIVATE + 1);
9069 for (list = OMP_LIST_FIRSTPRIVATE; list <= OMP_LIST_LASTPRIVATE; list++)
9070 for (n = omp_clauses->lists[list]; n; n = n->next)
9071 if (n->sym->data_mark || n->sym->gen_mark || n->sym->dev_mark)
9072 {
9073 gfc_error ("Symbol %qs present on multiple clauses at %L",
9074 n->sym->name, &n->where);
9075 n->sym->data_mark = n->sym->gen_mark = n->sym->dev_mark = 0;
9076 }
9077 else if (n->sym->mark
9078 && code->op != EXEC_OMP_TARGET_TEAMS
9079 && code->op != EXEC_OMP_TARGET_TEAMS_DISTRIBUTE
9080 && code->op != EXEC_OMP_TARGET_TEAMS_LOOP
9081 && code->op != EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD
9082 && code->op != EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO
9083 && code->op != EXEC_OMP_TARGET_PARALLEL
9084 && code->op != EXEC_OMP_TARGET_PARALLEL_DO
9085 && code->op != EXEC_OMP_TARGET_PARALLEL_LOOP
9086 && code->op != EXEC_OMP_TARGET_PARALLEL_DO_SIMD
9087 && code->op != EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD)
9088 gfc_error ("Symbol %qs present on both data and map clauses "
9089 "at %L", n->sym->name, &n->where);
9090
9091 for (n = omp_clauses->lists[OMP_LIST_FIRSTPRIVATE]; n; n = n->next)
9092 {
9093 if (n->sym->data_mark || n->sym->gen_mark || n->sym->dev_mark)
9094 gfc_error ("Symbol %qs present on multiple clauses at %L",
9095 n->sym->name, &n->where);
9096 else
9097 n->sym->data_mark = 1;
9098 }
9099 for (n = omp_clauses->lists[OMP_LIST_LASTPRIVATE]; n; n = n->next)
9100 n->sym->data_mark = 0;
9101
9102 for (n = omp_clauses->lists[OMP_LIST_LASTPRIVATE]; n; n = n->next)
9103 {
9104 if (n->sym->data_mark || n->sym->gen_mark || n->sym->dev_mark)
9105 gfc_error ("Symbol %qs present on multiple clauses at %L",
9106 n->sym->name, &n->where);
9107 else
9108 n->sym->data_mark = 1;
9109 }
9110
9111 for (n = omp_clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
9112 n->sym->mark = 0;
9113
9114 for (n = omp_clauses->lists[OMP_LIST_ALIGNED]; n; n = n->next)
9115 {
9116 if (n->sym->mark)
9117 gfc_error ("Symbol %qs present on multiple clauses at %L",
9118 n->sym->name, &n->where);
9119 else
9120 n->sym->mark = 1;
9121 }
9122
9123 if (omp_clauses->lists[OMP_LIST_ALLOCATE])
9124 {
9125 for (n = omp_clauses->lists[OMP_LIST_ALLOCATE]; n; n = n->next)
9126 {
9127 if (n->u2.allocator
9128 && (!gfc_resolve_expr (n->u2.allocator)
9129 || n->u2.allocator->ts.type != BT_INTEGER
9130 || n->u2.allocator->rank != 0
9131 || n->u2.allocator->ts.kind != gfc_c_intptr_kind))
9132 {
9133 gfc_error ("Expected integer expression of the "
9134 "%<omp_allocator_handle_kind%> kind at %L",
9135 &n->u2.allocator->where);
9136 break;
9137 }
9138 if (!n->u.align)
9139 continue;
9140 HOST_WIDE_INT alignment = 0;
9141 if (!gfc_resolve_expr (n->u.align)
9142 || n->u.align->ts.type != BT_INTEGER
9143 || n->u.align->rank != 0
9144 || n->u.align->expr_type != EXPR_CONSTANT
9145 || gfc_extract_hwi (n->u.align, &alignment)
9146 || alignment <= 0
9147 || !pow2p_hwi (alignment))
9148 {
9149 gfc_error ("ALIGN requires a scalar positive constant integer "
9150 "alignment expression at %L that is a power of two",
9151 &n->u.align->where);
9152 break;
9153 }
9154 }
9155
9156 /* Check for 2 things here.
9157 1. There is no duplication of variable in allocate clause.
9158 2. Variable in allocate clause are also present in some
9159 privatization clase (non-composite case). */
9160 for (n = omp_clauses->lists[OMP_LIST_ALLOCATE]; n; n = n->next)
9161 if (n->sym)
9162 n->sym->mark = 0;
9163
9164 gfc_omp_namelist *prev = NULL;
9165 for (n = omp_clauses->lists[OMP_LIST_ALLOCATE]; n; )
9166 {
9167 if (n->sym == NULL)
9168 {
9169 n = n->next;
9170 continue;
9171 }
9172 if (n->sym->mark == 1)
9173 {
9174 gfc_warning (OPT_Wopenmp, "%qs appears more than once in "
9175 "%<allocate%> at %L" , n->sym->name, &n->where);
9176 /* We have already seen this variable so it is a duplicate.
9177 Remove it. */
9178 if (prev != NULL && prev->next == n)
9179 {
9180 prev->next = n->next;
9181 n->next = NULL;
9182 gfc_free_omp_namelist (n, false, true, false, false);
9183 n = prev->next;
9184 }
9185 continue;
9186 }
9187 n->sym->mark = 1;
9188 prev = n;
9189 n = n->next;
9190 }
9191
9192 /* Non-composite constructs. */
9193 if (code && code->op < EXEC_OMP_DO_SIMD)
9194 {
9195 for (list = 0; list < OMP_LIST_NUM; list++)
9196 switch (list)
9197 {
9198 case OMP_LIST_PRIVATE:
9199 case OMP_LIST_FIRSTPRIVATE:
9200 case OMP_LIST_LASTPRIVATE:
9201 case OMP_LIST_REDUCTION:
9202 case OMP_LIST_REDUCTION_INSCAN:
9203 case OMP_LIST_REDUCTION_TASK:
9204 case OMP_LIST_IN_REDUCTION:
9205 case OMP_LIST_TASK_REDUCTION:
9206 case OMP_LIST_LINEAR:
9207 for (n = omp_clauses->lists[list]; n; n = n->next)
9208 n->sym->mark = 0;
9209 break;
9210 default:
9211 break;
9212 }
9213
9214 for (n = omp_clauses->lists[OMP_LIST_ALLOCATE]; n; n = n->next)
9215 if (n->sym->mark == 1)
9216 gfc_error ("%qs specified in %<allocate%> clause at %L but not "
9217 "in an explicit privatization clause",
9218 n->sym->name, &n->where);
9219 }
9220 if (code
9221 && (code->op == EXEC_OMP_ALLOCATORS || code->op == EXEC_OMP_ALLOCATE)
9222 && code->block
9223 && code->block->next
9224 && code->block->next->op == EXEC_ALLOCATE)
9225 {
9226 gfc_alloc *a;
9227 gfc_omp_namelist *n_null = NULL;
9228 bool missing_allocator = false;
9229 gfc_symbol *missing_allocator_sym = NULL;
9230 for (n = omp_clauses->lists[OMP_LIST_ALLOCATE]; n; n = n->next)
9231 {
9232 if (n->u2.allocator == NULL)
9233 {
9234 if (!missing_allocator_sym)
9235 missing_allocator_sym = n->sym;
9236 missing_allocator = true;
9237 }
9238 if (n->sym == NULL)
9239 {
9240 n_null = n;
9241 continue;
9242 }
9243 if (n->sym->attr.codimension)
9244 gfc_error ("Unexpected coarray %qs in %<allocate%> at %L",
9245 n->sym->name, &n->where);
9246 for (a = code->block->next->ext.alloc.list; a; a = a->next)
9247 if (a->expr->expr_type == EXPR_VARIABLE
9248 && a->expr->symtree->n.sym == n->sym)
9249 {
9250 gfc_ref *ref;
9251 for (ref = a->expr->ref; ref; ref = ref->next)
9252 if (ref->type == REF_COMPONENT)
9253 break;
9254 if (ref == NULL)
9255 break;
9256 }
9257 if (a == NULL)
9258 gfc_error ("%qs specified in %<allocate%> at %L but not "
9259 "in the associated ALLOCATE statement",
9260 n->sym->name, &n->where);
9261 }
9262 /* If there is an ALLOCATE directive without list argument, a
9263 namelist with its allocator/align clauses and n->sym = NULL is
9264 created during parsing; here, we add all not otherwise specified
9265 items from the Fortran allocate to that list.
9266 For an ALLOCATORS directive, not listed items use the normal
9267 Fortran way.
9268 The behavior of an ALLOCATE directive that does not list all
9269 arguments but there is no directive without list argument is not
9270 well specified. Thus, we reject such code below. In OpenMP 5.2
9271 the executable ALLOCATE directive is deprecated and in 6.0
9272 deleted such that no spec clarification is to be expected. */
9273 for (a = code->block->next->ext.alloc.list; a; a = a->next)
9274 if (a->expr->expr_type == EXPR_VARIABLE)
9275 {
9276 for (n = omp_clauses->lists[OMP_LIST_ALLOCATE]; n; n = n->next)
9277 if (a->expr->symtree->n.sym == n->sym)
9278 {
9279 gfc_ref *ref;
9280 for (ref = a->expr->ref; ref; ref = ref->next)
9281 if (ref->type == REF_COMPONENT)
9282 break;
9283 if (ref == NULL)
9284 break;
9285 }
9286 if (n == NULL && n_null == NULL)
9287 {
9288 /* OK for ALLOCATORS but for ALLOCATE: Unspecified whether
9289 that should use the default allocator of OpenMP or the
9290 Fortran allocator. Thus, just reject it. */
9291 if (code->op == EXEC_OMP_ALLOCATE)
9292 gfc_error ("%qs listed in %<allocate%> statement at %L "
9293 "but it is neither explicitly in listed in "
9294 "the %<!$OMP ALLOCATE%> directive nor exists"
9295 " a directive without argument list",
9296 a->expr->symtree->n.sym->name,
9297 &a->expr->where);
9298 break;
9299 }
9300 if (n == NULL)
9301 {
9302 if (a->expr->symtree->n.sym->attr.codimension)
9303 gfc_error ("Unexpected coarray %qs in %<allocate%> at "
9304 "%L, implicitly listed in %<!$OMP ALLOCATE%>"
9305 " at %L", a->expr->symtree->n.sym->name,
9306 &a->expr->where, &n_null->where);
9307 break;
9308 }
9309 }
9310 gfc_namespace *prog_unit = ns;
9311 while (prog_unit->parent)
9312 prog_unit = prog_unit->parent;
9313 gfc_namespace *fn_ns = ns;
9314 while (fn_ns)
9315 {
9316 if (ns->proc_name
9317 && (ns->proc_name->attr.subroutine
9318 || ns->proc_name->attr.function))
9319 break;
9320 fn_ns = fn_ns->parent;
9321 }
9322 if (missing_allocator
9323 && !(prog_unit->omp_requires & OMP_REQ_DYNAMIC_ALLOCATORS)
9324 && ((fn_ns && fn_ns->proc_name->attr.omp_declare_target)
9325 || omp_clauses->contained_in_target_construct))
9326 {
9327 if (code->op == EXEC_OMP_ALLOCATORS)
9328 gfc_error ("ALLOCATORS directive at %L inside a target region "
9329 "must specify an ALLOCATOR modifier for %qs",
9330 &code->loc, missing_allocator_sym->name);
9331 else if (missing_allocator_sym)
9332 gfc_error ("ALLOCATE directive at %L inside a target region "
9333 "must specify an ALLOCATOR clause for %qs",
9334 &code->loc, missing_allocator_sym->name);
9335 else
9336 gfc_error ("ALLOCATE directive at %L inside a target region "
9337 "must specify an ALLOCATOR clause", &code->loc);
9338 }
9339
9340 }
9341 }
9342
9343 /* OpenACC reductions. */
9344 if (openacc)
9345 {
9346 for (n = omp_clauses->lists[OMP_LIST_REDUCTION]; n; n = n->next)
9347 n->sym->mark = 0;
9348
9349 for (n = omp_clauses->lists[OMP_LIST_REDUCTION]; n; n = n->next)
9350 {
9351 if (n->sym->mark)
9352 gfc_error ("Symbol %qs present on multiple clauses at %L",
9353 n->sym->name, &n->where);
9354 else
9355 n->sym->mark = 1;
9356
9357 /* OpenACC does not support reductions on arrays. */
9358 if (n->sym->as)
9359 gfc_error ("Array %qs is not permitted in reduction at %L",
9360 n->sym->name, &n->where);
9361 }
9362 }
9363
9364 for (n = omp_clauses->lists[OMP_LIST_TO]; n; n = n->next)
9365 n->sym->mark = 0;
9366 for (n = omp_clauses->lists[OMP_LIST_FROM]; n; n = n->next)
9367 if (n->expr == NULL)
9368 n->sym->mark = 1;
9369 for (n = omp_clauses->lists[OMP_LIST_TO]; n; n = n->next)
9370 {
9371 if (n->expr == NULL && n->sym->mark)
9372 gfc_error ("Symbol %qs present on both FROM and TO clauses at %L",
9373 n->sym->name, &n->where);
9374 else
9375 n->sym->mark = 1;
9376 }
9377
9378 bool has_inscan = false, has_notinscan = false;
9379 for (list = 0; list < OMP_LIST_NUM; list++)
9380 if ((n = omp_clauses->lists[list]) != NULL)
9381 {
9382 const char *name = clause_names[list];
9383
9384 switch (list)
9385 {
9386 case OMP_LIST_COPYIN:
9387 for (; n != NULL; n = n->next)
9388 {
9389 if (!n->sym->attr.threadprivate)
9390 gfc_error ("Non-THREADPRIVATE object %qs in COPYIN clause"
9391 " at %L", n->sym->name, &n->where);
9392 }
9393 break;
9394 case OMP_LIST_COPYPRIVATE:
9395 if (omp_clauses->nowait)
9396 gfc_error ("NOWAIT clause must not be used with COPYPRIVATE "
9397 "clause at %L", &n->where);
9398 for (; n != NULL; n = n->next)
9399 {
9400 if (n->sym->as && n->sym->as->type == AS_ASSUMED_SIZE)
9401 gfc_error ("Assumed size array %qs in COPYPRIVATE clause "
9402 "at %L", n->sym->name, &n->where);
9403 if (n->sym->attr.pointer && n->sym->attr.intent == INTENT_IN)
9404 gfc_error ("INTENT(IN) POINTER %qs in COPYPRIVATE clause "
9405 "at %L", n->sym->name, &n->where);
9406 }
9407 break;
9408 case OMP_LIST_SHARED:
9409 for (; n != NULL; n = n->next)
9410 {
9411 if (n->sym->attr.threadprivate)
9412 gfc_error ("THREADPRIVATE object %qs in SHARED clause at "
9413 "%L", n->sym->name, &n->where);
9414 if (n->sym->attr.cray_pointee)
9415 gfc_error ("Cray pointee %qs in SHARED clause at %L",
9416 n->sym->name, &n->where);
9417 if (n->sym->attr.associate_var)
9418 gfc_error ("Associate name %qs in SHARED clause at %L",
9419 n->sym->attr.select_type_temporary
9420 ? n->sym->assoc->target->symtree->n.sym->name
9421 : n->sym->name, &n->where);
9422 if (omp_clauses->detach
9423 && n->sym == omp_clauses->detach->symtree->n.sym)
9424 gfc_error ("DETACH event handle %qs in SHARED clause at %L",
9425 n->sym->name, &n->where);
9426 }
9427 break;
9428 case OMP_LIST_ALIGNED:
9429 for (; n != NULL; n = n->next)
9430 {
9431 if (!n->sym->attr.pointer
9432 && !n->sym->attr.allocatable
9433 && !n->sym->attr.cray_pointer
9434 && (n->sym->ts.type != BT_DERIVED
9435 || (n->sym->ts.u.derived->from_intmod
9436 != INTMOD_ISO_C_BINDING)
9437 || (n->sym->ts.u.derived->intmod_sym_id
9438 != ISOCBINDING_PTR)))
9439 gfc_error ("%qs in ALIGNED clause must be POINTER, "
9440 "ALLOCATABLE, Cray pointer or C_PTR at %L",
9441 n->sym->name, &n->where);
9442 else if (n->expr)
9443 {
9444 if (!gfc_resolve_expr (n->expr)
9445 || n->expr->ts.type != BT_INTEGER
9446 || n->expr->rank != 0
9447 || n->expr->expr_type != EXPR_CONSTANT
9448 || mpz_sgn (n->expr->value.integer) <= 0)
9449 gfc_error ("%qs in ALIGNED clause at %L requires a scalar"
9450 " positive constant integer alignment "
9451 "expression", n->sym->name, &n->where);
9452 }
9453 }
9454 break;
9455 case OMP_LIST_AFFINITY:
9456 case OMP_LIST_DEPEND:
9457 case OMP_LIST_MAP:
9458 case OMP_LIST_TO:
9459 case OMP_LIST_FROM:
9460 case OMP_LIST_CACHE:
9461 for (; n != NULL; n = n->next)
9462 {
9463 if ((list == OMP_LIST_DEPEND || list == OMP_LIST_AFFINITY)
9464 && n->u2.ns && !n->u2.ns->resolved)
9465 {
9466 n->u2.ns->resolved = 1;
9467 for (gfc_symbol *sym = n->u2.ns->omp_affinity_iterators;
9468 sym; sym = sym->tlink)
9469 {
9470 gfc_constructor *c;
9471 c = gfc_constructor_first (sym->value->value.constructor);
9472 if (!gfc_resolve_expr (c->expr)
9473 || c->expr->ts.type != BT_INTEGER
9474 || c->expr->rank != 0)
9475 gfc_error ("Scalar integer expression for range begin"
9476 " expected at %L", &c->expr->where);
9477 c = gfc_constructor_next (c);
9478 if (!gfc_resolve_expr (c->expr)
9479 || c->expr->ts.type != BT_INTEGER
9480 || c->expr->rank != 0)
9481 gfc_error ("Scalar integer expression for range end "
9482 "expected at %L", &c->expr->where);
9483 c = gfc_constructor_next (c);
9484 if (c && (!gfc_resolve_expr (c->expr)
9485 || c->expr->ts.type != BT_INTEGER
9486 || c->expr->rank != 0))
9487 gfc_error ("Scalar integer expression for range step "
9488 "expected at %L", &c->expr->where);
9489 else if (c
9490 && c->expr->expr_type == EXPR_CONSTANT
9491 && mpz_cmp_si (c->expr->value.integer, 0) == 0)
9492 gfc_error ("Nonzero range step expected at %L",
9493 &c->expr->where);
9494 }
9495 }
9496
9497 if (list == OMP_LIST_DEPEND)
9498 {
9499 if (n->u.depend_doacross_op == OMP_DEPEND_SINK_FIRST
9500 || n->u.depend_doacross_op == OMP_DOACROSS_SINK_FIRST
9501 || n->u.depend_doacross_op == OMP_DOACROSS_SINK)
9502 {
9503 if (omp_clauses->doacross_source)
9504 {
9505 gfc_error ("Dependence-type SINK used together with"
9506 " SOURCE on the same construct at %L",
9507 &n->where);
9508 omp_clauses->doacross_source = false;
9509 }
9510 else if (n->expr)
9511 {
9512 if (!gfc_resolve_expr (n->expr)
9513 || n->expr->ts.type != BT_INTEGER
9514 || n->expr->rank != 0)
9515 gfc_error ("SINK addend not a constant integer "
9516 "at %L", &n->where);
9517 }
9518 if (n->sym == NULL
9519 && (n->expr == NULL
9520 || mpz_cmp_si (n->expr->value.integer, -1) != 0))
9521 gfc_error ("omp_cur_iteration at %L requires %<-1%> "
9522 "as logical offset", &n->where);
9523 continue;
9524 }
9525 else if (n->u.depend_doacross_op == OMP_DEPEND_DEPOBJ
9526 && !n->expr
9527 && (n->sym->ts.type != BT_INTEGER
9528 || n->sym->ts.kind
9529 != 2 * gfc_index_integer_kind
9530 || n->sym->attr.dimension))
9531 gfc_error ("Locator %qs at %L in DEPEND clause of depobj "
9532 "type shall be a scalar integer of "
9533 "OMP_DEPEND_KIND kind", n->sym->name,
9534 &n->where);
9535 else if (n->u.depend_doacross_op == OMP_DEPEND_DEPOBJ
9536 && n->expr
9537 && (!gfc_resolve_expr (n->expr)
9538 || n->expr->ts.type != BT_INTEGER
9539 || n->expr->ts.kind
9540 != 2 * gfc_index_integer_kind
9541 || n->expr->rank != 0))
9542 gfc_error ("Locator at %L in DEPEND clause of depobj "
9543 "type shall be a scalar integer of "
9544 "OMP_DEPEND_KIND kind", &n->expr->where);
9545 }
9546 gfc_ref *lastref = NULL, *lastslice = NULL;
9547 bool resolved = false;
9548 if (n->expr)
9549 {
9550 lastref = n->expr->ref;
9551 resolved = gfc_resolve_expr (n->expr);
9552
9553 /* Look through component refs to find last array
9554 reference. */
9555 if (resolved)
9556 {
9557 for (gfc_ref *ref = n->expr->ref; ref; ref = ref->next)
9558 if (ref->type == REF_COMPONENT
9559 || ref->type == REF_SUBSTRING
9560 || ref->type == REF_INQUIRY)
9561 lastref = ref;
9562 else if (ref->type == REF_ARRAY)
9563 {
9564 for (int i = 0; i < ref->u.ar.dimen; i++)
9565 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE)
9566 lastslice = ref;
9567
9568 lastref = ref;
9569 }
9570
9571 /* The "!$acc cache" directive allows rectangular
9572 subarrays to be specified, with some restrictions
9573 on the form of bounds (not implemented).
9574 Only raise an error here if we're really sure the
9575 array isn't contiguous. An expression such as
9576 arr(-n:n,-n:n) could be contiguous even if it looks
9577 like it may not be. */
9578 if (code->op != EXEC_OACC_UPDATE
9579 && list != OMP_LIST_CACHE
9580 && list != OMP_LIST_DEPEND
9581 && !gfc_is_simply_contiguous (n->expr, false, true)
9582 && gfc_is_not_contiguous (n->expr)
9583 && !(lastslice
9584 && (lastslice->next
9585 || lastslice->type != REF_ARRAY)))
9586 gfc_error ("Array is not contiguous at %L",
9587 &n->where);
9588 }
9589 }
9590 if (openacc
9591 && list == OMP_LIST_MAP
9592 && (n->u.map.op == OMP_MAP_ATTACH
9593 || n->u.map.op == OMP_MAP_DETACH))
9594 {
9595 symbol_attribute attr;
9596 if (n->expr)
9597 attr = gfc_expr_attr (n->expr);
9598 else
9599 attr = n->sym->attr;
9600 if (!attr.pointer && !attr.allocatable)
9601 gfc_error ("%qs clause argument must be ALLOCATABLE or "
9602 "a POINTER at %L",
9603 (n->u.map.op == OMP_MAP_ATTACH) ? "attach"
9604 : "detach", &n->where);
9605 }
9606 if (lastref
9607 || (n->expr
9608 && (!resolved || n->expr->expr_type != EXPR_VARIABLE)))
9609 {
9610 if (!lastslice
9611 && lastref
9612 && lastref->type == REF_SUBSTRING)
9613 gfc_error ("Unexpected substring reference in %s clause "
9614 "at %L", name, &n->where);
9615 else if (!lastslice
9616 && lastref
9617 && lastref->type == REF_INQUIRY)
9618 {
9619 gcc_assert (lastref->u.i == INQUIRY_RE
9620 || lastref->u.i == INQUIRY_IM);
9621 gfc_error ("Unexpected complex-parts designator "
9622 "reference in %s clause at %L",
9623 name, &n->where);
9624 }
9625 else if (!resolved
9626 || n->expr->expr_type != EXPR_VARIABLE
9627 || (lastslice
9628 && (lastslice->next
9629 || lastslice->type != REF_ARRAY)))
9630 gfc_error ("%qs in %s clause at %L is not a proper "
9631 "array section", n->sym->name, name,
9632 &n->where);
9633 else if (lastslice)
9634 {
9635 int i;
9636 gfc_array_ref *ar = &lastslice->u.ar;
9637 for (i = 0; i < ar->dimen; i++)
9638 if (ar->stride[i] && code->op != EXEC_OACC_UPDATE)
9639 {
9640 gfc_error ("Stride should not be specified for "
9641 "array section in %s clause at %L",
9642 name, &n->where);
9643 break;
9644 }
9645 else if (ar->dimen_type[i] != DIMEN_ELEMENT
9646 && ar->dimen_type[i] != DIMEN_RANGE)
9647 {
9648 gfc_error ("%qs in %s clause at %L is not a "
9649 "proper array section",
9650 n->sym->name, name, &n->where);
9651 break;
9652 }
9653 else if ((list == OMP_LIST_DEPEND
9654 || list == OMP_LIST_AFFINITY)
9655 && ar->start[i]
9656 && ar->start[i]->expr_type == EXPR_CONSTANT
9657 && ar->end[i]
9658 && ar->end[i]->expr_type == EXPR_CONSTANT
9659 && mpz_cmp (ar->start[i]->value.integer,
9660 ar->end[i]->value.integer) > 0)
9661 {
9662 gfc_error ("%qs in %s clause at %L is a "
9663 "zero size array section",
9664 n->sym->name,
9665 list == OMP_LIST_DEPEND
9666 ? "DEPEND" : "AFFINITY", &n->where);
9667 break;
9668 }
9669 }
9670 }
9671 else if (openacc)
9672 {
9673 if (list == OMP_LIST_MAP
9674 && n->u.map.op == OMP_MAP_FORCE_DEVICEPTR)
9675 resolve_oacc_deviceptr_clause (n->sym, n->where, name);
9676 else
9677 resolve_oacc_data_clauses (n->sym, n->where, name);
9678 }
9679 else if (list != OMP_LIST_DEPEND
9680 && n->sym->as
9681 && n->sym->as->type == AS_ASSUMED_SIZE)
9682 gfc_error ("Assumed size array %qs in %s clause at %L",
9683 n->sym->name, name, &n->where);
9684 if (list == OMP_LIST_MAP && !openacc)
9685 switch (code->op)
9686 {
9687 case EXEC_OMP_TARGET:
9688 case EXEC_OMP_TARGET_PARALLEL:
9689 case EXEC_OMP_TARGET_PARALLEL_DO:
9690 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
9691 case EXEC_OMP_TARGET_PARALLEL_LOOP:
9692 case EXEC_OMP_TARGET_SIMD:
9693 case EXEC_OMP_TARGET_TEAMS:
9694 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9695 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9696 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9697 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9698 case EXEC_OMP_TARGET_TEAMS_LOOP:
9699 case EXEC_OMP_TARGET_DATA:
9700 switch (n->u.map.op)
9701 {
9702 case OMP_MAP_TO:
9703 case OMP_MAP_ALWAYS_TO:
9704 case OMP_MAP_PRESENT_TO:
9705 case OMP_MAP_ALWAYS_PRESENT_TO:
9706 case OMP_MAP_FROM:
9707 case OMP_MAP_ALWAYS_FROM:
9708 case OMP_MAP_PRESENT_FROM:
9709 case OMP_MAP_ALWAYS_PRESENT_FROM:
9710 case OMP_MAP_TOFROM:
9711 case OMP_MAP_ALWAYS_TOFROM:
9712 case OMP_MAP_PRESENT_TOFROM:
9713 case OMP_MAP_ALWAYS_PRESENT_TOFROM:
9714 case OMP_MAP_ALLOC:
9715 case OMP_MAP_PRESENT_ALLOC:
9716 break;
9717 default:
9718 gfc_error ("TARGET%s with map-type other than TO, "
9719 "FROM, TOFROM, or ALLOC on MAP clause "
9720 "at %L",
9721 code->op == EXEC_OMP_TARGET_DATA
9722 ? " DATA" : "", &n->where);
9723 break;
9724 }
9725 break;
9726 case EXEC_OMP_TARGET_ENTER_DATA:
9727 switch (n->u.map.op)
9728 {
9729 case OMP_MAP_TO:
9730 case OMP_MAP_ALWAYS_TO:
9731 case OMP_MAP_PRESENT_TO:
9732 case OMP_MAP_ALWAYS_PRESENT_TO:
9733 case OMP_MAP_ALLOC:
9734 case OMP_MAP_PRESENT_ALLOC:
9735 break;
9736 case OMP_MAP_TOFROM:
9737 n->u.map.op = OMP_MAP_TO;
9738 break;
9739 case OMP_MAP_ALWAYS_TOFROM:
9740 n->u.map.op = OMP_MAP_ALWAYS_TO;
9741 break;
9742 case OMP_MAP_PRESENT_TOFROM:
9743 n->u.map.op = OMP_MAP_PRESENT_TO;
9744 break;
9745 case OMP_MAP_ALWAYS_PRESENT_TOFROM:
9746 n->u.map.op = OMP_MAP_ALWAYS_PRESENT_TO;
9747 break;
9748 default:
9749 gfc_error ("TARGET ENTER DATA with map-type other "
9750 "than TO, TOFROM or ALLOC on MAP clause "
9751 "at %L", &n->where);
9752 break;
9753 }
9754 break;
9755 case EXEC_OMP_TARGET_EXIT_DATA:
9756 switch (n->u.map.op)
9757 {
9758 case OMP_MAP_FROM:
9759 case OMP_MAP_ALWAYS_FROM:
9760 case OMP_MAP_PRESENT_FROM:
9761 case OMP_MAP_ALWAYS_PRESENT_FROM:
9762 case OMP_MAP_RELEASE:
9763 case OMP_MAP_DELETE:
9764 break;
9765 case OMP_MAP_TOFROM:
9766 n->u.map.op = OMP_MAP_FROM;
9767 break;
9768 case OMP_MAP_ALWAYS_TOFROM:
9769 n->u.map.op = OMP_MAP_ALWAYS_FROM;
9770 break;
9771 case OMP_MAP_PRESENT_TOFROM:
9772 n->u.map.op = OMP_MAP_PRESENT_FROM;
9773 break;
9774 case OMP_MAP_ALWAYS_PRESENT_TOFROM:
9775 n->u.map.op = OMP_MAP_ALWAYS_PRESENT_FROM;
9776 break;
9777 default:
9778 gfc_error ("TARGET EXIT DATA with map-type other "
9779 "than FROM, TOFROM, RELEASE, or DELETE on "
9780 "MAP clause at %L", &n->where);
9781 break;
9782 }
9783 break;
9784 default:
9785 break;
9786 }
9787 }
9788
9789 if (list != OMP_LIST_DEPEND)
9790 for (n = omp_clauses->lists[list]; n != NULL; n = n->next)
9791 {
9792 n->sym->attr.referenced = 1;
9793 if (n->sym->attr.threadprivate)
9794 gfc_error ("THREADPRIVATE object %qs in %s clause at %L",
9795 n->sym->name, name, &n->where);
9796 if (n->sym->attr.cray_pointee)
9797 gfc_error ("Cray pointee %qs in %s clause at %L",
9798 n->sym->name, name, &n->where);
9799 }
9800 break;
9801 case OMP_LIST_IS_DEVICE_PTR:
9802 last = NULL;
9803 for (n = omp_clauses->lists[list]; n != NULL; )
9804 {
9805 if ((n->sym->ts.type != BT_DERIVED
9806 || !n->sym->ts.u.derived->ts.is_iso_c
9807 || (n->sym->ts.u.derived->intmod_sym_id
9808 != ISOCBINDING_PTR))
9809 && code->op == EXEC_OMP_DISPATCH)
9810 /* Non-TARGET (i.e. DISPATCH) requires a C_PTR. */
9811 gfc_error ("List item %qs in %s clause at %L must be of "
9812 "TYPE(C_PTR)", n->sym->name, name, &n->where);
9813 else if (n->sym->ts.type != BT_DERIVED
9814 || !n->sym->ts.u.derived->ts.is_iso_c
9815 || (n->sym->ts.u.derived->intmod_sym_id
9816 != ISOCBINDING_PTR))
9817 {
9818 /* For TARGET, non-C_PTR are deprecated and handled as
9819 has_device_addr. */
9820 gfc_omp_namelist *n2 = n;
9821 n = n->next;
9822 if (last)
9823 last->next = n;
9824 else
9825 omp_clauses->lists[list] = n;
9826 n2->next = omp_clauses->lists[OMP_LIST_HAS_DEVICE_ADDR];
9827 omp_clauses->lists[OMP_LIST_HAS_DEVICE_ADDR] = n2;
9828 continue;
9829 }
9830 last = n;
9831 n = n->next;
9832 }
9833 break;
9834 case OMP_LIST_HAS_DEVICE_ADDR:
9835 case OMP_LIST_USE_DEVICE_ADDR:
9836 break;
9837 case OMP_LIST_USE_DEVICE_PTR:
9838 /* Non-C_PTR are deprecated and handled as use_device_ADDR. */
9839 last = NULL;
9840 for (n = omp_clauses->lists[list]; n != NULL; )
9841 {
9842 gfc_omp_namelist *n2 = n;
9843 if (n->sym->ts.type != BT_DERIVED
9844 || !n->sym->ts.u.derived->ts.is_iso_c)
9845 {
9846 n = n->next;
9847 if (last)
9848 last->next = n;
9849 else
9850 omp_clauses->lists[list] = n;
9851 n2->next = omp_clauses->lists[OMP_LIST_USE_DEVICE_ADDR];
9852 omp_clauses->lists[OMP_LIST_USE_DEVICE_ADDR] = n2;
9853 continue;
9854 }
9855 last = n;
9856 n = n->next;
9857 }
9858 break;
9859 case OMP_LIST_USES_ALLOCATORS:
9860 {
9861 if (n != NULL
9862 && n->u.memspace_sym
9863 && (n->u.memspace_sym->attr.flavor != FL_PARAMETER
9864 || n->u.memspace_sym->ts.type != BT_INTEGER
9865 || n->u.memspace_sym->ts.kind != gfc_c_intptr_kind
9866 || n->u.memspace_sym->attr.dimension
9867 || (!startswith (n->u.memspace_sym->name, "omp_")
9868 && !startswith (n->u.memspace_sym->name, "ompx_"))
9869 || !endswith (n->u.memspace_sym->name, "_mem_space")))
9870 gfc_error ("Memspace %qs at %L in USES_ALLOCATORS must be "
9871 "a predefined memory space",
9872 n->u.memspace_sym->name, &n->where);
9873 for (; n != NULL; n = n->next)
9874 {
9875 if (n->sym->ts.type != BT_INTEGER
9876 || n->sym->ts.kind != gfc_c_intptr_kind
9877 || n->sym->attr.dimension)
9878 gfc_error ("Allocator %qs at %L in USES_ALLOCATORS must "
9879 "be a scalar integer of kind "
9880 "%<omp_allocator_handle_kind%>", n->sym->name,
9881 &n->where);
9882 else if (n->sym->attr.flavor != FL_VARIABLE
9883 && ((!startswith (n->sym->name, "omp_")
9884 && !startswith (n->sym->name, "ompx_"))
9885 || !endswith (n->sym->name, "_mem_alloc")))
9886 gfc_error ("Allocator %qs at %L in USES_ALLOCATORS must "
9887 "either a variable or a predefined allocator",
9888 n->sym->name, &n->where);
9889 else if ((n->u.memspace_sym || n->u2.traits_sym)
9890 && n->sym->attr.flavor != FL_VARIABLE)
9891 gfc_error ("A memory space or traits array may not be "
9892 "specified for predefined allocator %qs at %L",
9893 n->sym->name, &n->where);
9894 if (n->u2.traits_sym
9895 && (n->u2.traits_sym->attr.flavor != FL_PARAMETER
9896 || !n->u2.traits_sym->attr.dimension
9897 || n->u2.traits_sym->as->rank != 1
9898 || n->u2.traits_sym->ts.type != BT_DERIVED
9899 || strcmp (n->u2.traits_sym->ts.u.derived->name,
9900 "omp_alloctrait") != 0))
9901 {
9902 gfc_error ("Traits array %qs in USES_ALLOCATORS %L must "
9903 "be a one-dimensional named constant array of "
9904 "type %<omp_alloctrait%>",
9905 n->u2.traits_sym->name, &n->where);
9906 break;
9907 }
9908 }
9909 break;
9910 }
9911 default:
9912 for (; n != NULL; n = n->next)
9913 {
9914 if (n->sym == NULL)
9915 {
9916 gcc_assert (code->op == EXEC_OMP_ALLOCATORS
9917 || code->op == EXEC_OMP_ALLOCATE);
9918 continue;
9919 }
9920 bool bad = false;
9921 bool is_reduction = (list == OMP_LIST_REDUCTION
9922 || list == OMP_LIST_REDUCTION_INSCAN
9923 || list == OMP_LIST_REDUCTION_TASK
9924 || list == OMP_LIST_IN_REDUCTION
9925 || list == OMP_LIST_TASK_REDUCTION);
9926 if (list == OMP_LIST_REDUCTION_INSCAN)
9927 has_inscan = true;
9928 else if (is_reduction)
9929 has_notinscan = true;
9930 if (has_inscan && has_notinscan && is_reduction)
9931 {
9932 gfc_error ("%<inscan%> and non-%<inscan%> %<reduction%> "
9933 "clauses on the same construct at %L",
9934 &n->where);
9935 break;
9936 }
9937 if (n->sym->attr.threadprivate)
9938 gfc_error ("THREADPRIVATE object %qs in %s clause at %L",
9939 n->sym->name, name, &n->where);
9940 if (n->sym->attr.cray_pointee)
9941 gfc_error ("Cray pointee %qs in %s clause at %L",
9942 n->sym->name, name, &n->where);
9943 if (n->sym->attr.associate_var)
9944 gfc_error ("Associate name %qs in %s clause at %L",
9945 n->sym->attr.select_type_temporary
9946 ? n->sym->assoc->target->symtree->n.sym->name
9947 : n->sym->name, name, &n->where);
9948 if (list != OMP_LIST_PRIVATE && is_reduction)
9949 {
9950 if (n->sym->attr.proc_pointer)
9951 gfc_error ("Procedure pointer %qs in %s clause at %L",
9952 n->sym->name, name, &n->where);
9953 if (n->sym->attr.pointer)
9954 gfc_error ("POINTER object %qs in %s clause at %L",
9955 n->sym->name, name, &n->where);
9956 if (n->sym->attr.cray_pointer)
9957 gfc_error ("Cray pointer %qs in %s clause at %L",
9958 n->sym->name, name, &n->where);
9959 }
9960 if (code
9961 && (oacc_is_loop (code)
9962 || code->op == EXEC_OACC_PARALLEL
9963 || code->op == EXEC_OACC_SERIAL))
9964 check_array_not_assumed (n->sym, n->where, name);
9965 else if (list != OMP_LIST_UNIFORM
9966 && n->sym->as && n->sym->as->type == AS_ASSUMED_SIZE)
9967 gfc_error ("Assumed size array %qs in %s clause at %L",
9968 n->sym->name, name, &n->where);
9969 if (n->sym->attr.in_namelist && !is_reduction)
9970 gfc_error ("Variable %qs in %s clause is used in "
9971 "NAMELIST statement at %L",
9972 n->sym->name, name, &n->where);
9973 if (n->sym->attr.pointer && n->sym->attr.intent == INTENT_IN)
9974 switch (list)
9975 {
9976 case OMP_LIST_PRIVATE:
9977 case OMP_LIST_LASTPRIVATE:
9978 case OMP_LIST_LINEAR:
9979 /* case OMP_LIST_REDUCTION: */
9980 gfc_error ("INTENT(IN) POINTER %qs in %s clause at %L",
9981 n->sym->name, name, &n->where);
9982 break;
9983 default:
9984 break;
9985 }
9986 if (omp_clauses->detach
9987 && (list == OMP_LIST_PRIVATE
9988 || list == OMP_LIST_FIRSTPRIVATE
9989 || list == OMP_LIST_LASTPRIVATE)
9990 && n->sym == omp_clauses->detach->symtree->n.sym)
9991 gfc_error ("DETACH event handle %qs in %s clause at %L",
9992 n->sym->name, name, &n->where);
9993
9994 if (!openacc
9995 && (list == OMP_LIST_PRIVATE
9996 || list == OMP_LIST_FIRSTPRIVATE)
9997 && ((n->sym->ts.type == BT_DERIVED
9998 && n->sym->ts.u.derived->attr.alloc_comp)
9999 || n->sym->ts.type == BT_CLASS))
10000 switch (code->op)
10001 {
10002 case EXEC_OMP_TARGET:
10003 case EXEC_OMP_TARGET_PARALLEL:
10004 case EXEC_OMP_TARGET_PARALLEL_DO:
10005 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
10006 case EXEC_OMP_TARGET_PARALLEL_LOOP:
10007 case EXEC_OMP_TARGET_SIMD:
10008 case EXEC_OMP_TARGET_TEAMS:
10009 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10010 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10011 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10012 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10013 case EXEC_OMP_TARGET_TEAMS_LOOP:
10014 if (n->sym->ts.type == BT_DERIVED
10015 && n->sym->ts.u.derived->attr.alloc_comp)
10016 gfc_error ("Sorry, list item %qs at %L with allocatable"
10017 " components is not yet supported in %s "
10018 "clause", n->sym->name, &n->where,
10019 list == OMP_LIST_PRIVATE ? "PRIVATE"
10020 : "FIRSTPRIVATE");
10021 else
10022 gfc_error ("Polymorphic list item %qs at %L in %s "
10023 "clause has unspecified behavior and "
10024 "unsupported", n->sym->name, &n->where,
10025 list == OMP_LIST_PRIVATE ? "PRIVATE"
10026 : "FIRSTPRIVATE");
10027 break;
10028 default:
10029 break;
10030 }
10031
10032 switch (list)
10033 {
10034 case OMP_LIST_REDUCTION_TASK:
10035 if (code
10036 && (code->op == EXEC_OMP_LOOP
10037 || code->op == EXEC_OMP_TASKLOOP
10038 || code->op == EXEC_OMP_TASKLOOP_SIMD
10039 || code->op == EXEC_OMP_MASKED_TASKLOOP
10040 || code->op == EXEC_OMP_MASKED_TASKLOOP_SIMD
10041 || code->op == EXEC_OMP_MASTER_TASKLOOP
10042 || code->op == EXEC_OMP_MASTER_TASKLOOP_SIMD
10043 || code->op == EXEC_OMP_PARALLEL_LOOP
10044 || code->op == EXEC_OMP_PARALLEL_MASKED_TASKLOOP
10045 || code->op == EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD
10046 || code->op == EXEC_OMP_PARALLEL_MASTER_TASKLOOP
10047 || code->op == EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD
10048 || code->op == EXEC_OMP_TARGET_PARALLEL_LOOP
10049 || code->op == EXEC_OMP_TARGET_TEAMS_LOOP
10050 || code->op == EXEC_OMP_TEAMS
10051 || code->op == EXEC_OMP_TEAMS_DISTRIBUTE
10052 || code->op == EXEC_OMP_TEAMS_LOOP))
10053 {
10054 gfc_error ("Only DEFAULT permitted as reduction-"
10055 "modifier in REDUCTION clause at %L",
10056 &n->where);
10057 break;
10058 }
10059 gcc_fallthrough ();
10060 case OMP_LIST_REDUCTION:
10061 case OMP_LIST_IN_REDUCTION:
10062 case OMP_LIST_TASK_REDUCTION:
10063 case OMP_LIST_REDUCTION_INSCAN:
10064 switch (n->u.reduction_op)
10065 {
10066 case OMP_REDUCTION_PLUS:
10067 case OMP_REDUCTION_TIMES:
10068 case OMP_REDUCTION_MINUS:
10069 if (!gfc_numeric_ts (&n->sym->ts))
10070 bad = true;
10071 break;
10072 case OMP_REDUCTION_AND:
10073 case OMP_REDUCTION_OR:
10074 case OMP_REDUCTION_EQV:
10075 case OMP_REDUCTION_NEQV:
10076 if (n->sym->ts.type != BT_LOGICAL)
10077 bad = true;
10078 break;
10079 case OMP_REDUCTION_MAX:
10080 case OMP_REDUCTION_MIN:
10081 if (n->sym->ts.type != BT_INTEGER
10082 && n->sym->ts.type != BT_REAL)
10083 bad = true;
10084 break;
10085 case OMP_REDUCTION_IAND:
10086 case OMP_REDUCTION_IOR:
10087 case OMP_REDUCTION_IEOR:
10088 if (n->sym->ts.type != BT_INTEGER)
10089 bad = true;
10090 break;
10091 case OMP_REDUCTION_USER:
10092 bad = true;
10093 break;
10094 default:
10095 break;
10096 }
10097 if (!bad)
10098 n->u2.udr = NULL;
10099 else
10100 {
10101 const char *udr_name = NULL;
10102 if (n->u2.udr)
10103 {
10104 udr_name = n->u2.udr->udr->name;
10105 n->u2.udr->udr
10106 = gfc_find_omp_udr (NULL, udr_name,
10107 &n->sym->ts);
10108 if (n->u2.udr->udr == NULL)
10109 {
10110 free (n->u2.udr);
10111 n->u2.udr = NULL;
10112 }
10113 }
10114 if (n->u2.udr == NULL)
10115 {
10116 if (udr_name == NULL)
10117 switch (n->u.reduction_op)
10118 {
10119 case OMP_REDUCTION_PLUS:
10120 case OMP_REDUCTION_TIMES:
10121 case OMP_REDUCTION_MINUS:
10122 case OMP_REDUCTION_AND:
10123 case OMP_REDUCTION_OR:
10124 case OMP_REDUCTION_EQV:
10125 case OMP_REDUCTION_NEQV:
10126 udr_name = gfc_op2string ((gfc_intrinsic_op)
10127 n->u.reduction_op);
10128 break;
10129 case OMP_REDUCTION_MAX:
10130 udr_name = "max";
10131 break;
10132 case OMP_REDUCTION_MIN:
10133 udr_name = "min";
10134 break;
10135 case OMP_REDUCTION_IAND:
10136 udr_name = "iand";
10137 break;
10138 case OMP_REDUCTION_IOR:
10139 udr_name = "ior";
10140 break;
10141 case OMP_REDUCTION_IEOR:
10142 udr_name = "ieor";
10143 break;
10144 default:
10145 gcc_unreachable ();
10146 }
10147 gfc_error ("!$OMP DECLARE REDUCTION %s not found "
10148 "for type %s at %L", udr_name,
10149 gfc_typename (&n->sym->ts), &n->where);
10150 }
10151 else
10152 {
10153 gfc_omp_udr *udr = n->u2.udr->udr;
10154 n->u.reduction_op = OMP_REDUCTION_USER;
10155 n->u2.udr->combiner
10156 = resolve_omp_udr_clause (n, udr->combiner_ns,
10157 udr->omp_out,
10158 udr->omp_in);
10159 if (udr->initializer_ns)
10160 n->u2.udr->initializer
10161 = resolve_omp_udr_clause (n,
10162 udr->initializer_ns,
10163 udr->omp_priv,
10164 udr->omp_orig);
10165 }
10166 }
10167 break;
10168 case OMP_LIST_LINEAR:
10169 if (code)
10170 {
10171 bool is_worksharing_for = false;
10172 switch (code->op)
10173 {
10174 case EXEC_OMP_DO:
10175 case EXEC_OMP_PARALLEL_DO:
10176 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10177 case EXEC_OMP_TARGET_PARALLEL_DO:
10178 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10179 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10180 is_worksharing_for = true;
10181 break;
10182 default:
10183 break;
10184 }
10185
10186 if (is_worksharing_for
10187 && (n->sym->attr.dimension
10188 || n->sym->attr.allocatable))
10189 {
10190 if (n->sym->attr.allocatable)
10191 gfc_error ("Sorry, ALLOCATABLE object %qs in "
10192 "LINEAR clause on worksharing-loop "
10193 "construct at %L is not yet supported",
10194 n->sym->name, &n->where);
10195 else
10196 gfc_error ("Sorry, array %qs in LINEAR clause "
10197 "on worksharing-loop construct at %L "
10198 "is not yet supported",
10199 n->sym->name, &n->where);
10200 break;
10201 }
10202 }
10203
10204 if (code
10205 && n->u.linear.op != OMP_LINEAR_DEFAULT
10206 && n->u.linear.op != linear_op)
10207 {
10208 if (n->u.linear.old_modifier)
10209 {
10210 gfc_error ("LINEAR clause modifier used on DO or "
10211 "SIMD construct at %L", &n->where);
10212 linear_op = n->u.linear.op;
10213 }
10214 else if (n->u.linear.op != OMP_LINEAR_VAL)
10215 {
10216 gfc_error ("LINEAR clause modifier other than VAL "
10217 "used on DO or SIMD construct at %L",
10218 &n->where);
10219 linear_op = n->u.linear.op;
10220 }
10221 }
10222 else if (n->u.linear.op != OMP_LINEAR_REF
10223 && n->sym->ts.type != BT_INTEGER)
10224 gfc_error ("LINEAR variable %qs must be INTEGER "
10225 "at %L", n->sym->name, &n->where);
10226 else if ((n->u.linear.op == OMP_LINEAR_REF
10227 || n->u.linear.op == OMP_LINEAR_UVAL)
10228 && n->sym->attr.value)
10229 gfc_error ("LINEAR dummy argument %qs with VALUE "
10230 "attribute with %s modifier at %L",
10231 n->sym->name,
10232 n->u.linear.op == OMP_LINEAR_REF
10233 ? "REF" : "UVAL", &n->where);
10234 else if (n->expr)
10235 {
10236 gfc_expr *expr = n->expr;
10237 if (!gfc_resolve_expr (expr)
10238 || expr->ts.type != BT_INTEGER
10239 || expr->rank != 0)
10240 gfc_error ("%qs in LINEAR clause at %L requires "
10241 "a scalar integer linear-step expression",
10242 n->sym->name, &n->where);
10243 else if (!code && expr->expr_type != EXPR_CONSTANT)
10244 {
10245 if (expr->expr_type == EXPR_VARIABLE
10246 && expr->symtree->n.sym->attr.dummy
10247 && expr->symtree->n.sym->ns == ns)
10248 {
10249 gfc_omp_namelist *n2;
10250 for (n2 = omp_clauses->lists[OMP_LIST_UNIFORM];
10251 n2; n2 = n2->next)
10252 if (n2->sym == expr->symtree->n.sym)
10253 break;
10254 if (n2)
10255 break;
10256 }
10257 gfc_error ("%qs in LINEAR clause at %L requires "
10258 "a constant integer linear-step "
10259 "expression or dummy argument "
10260 "specified in UNIFORM clause",
10261 n->sym->name, &n->where);
10262 }
10263 }
10264 break;
10265 /* Workaround for PR middle-end/26316, nothing really needs
10266 to be done here for OMP_LIST_PRIVATE. */
10267 case OMP_LIST_PRIVATE:
10268 gcc_assert (code && code->op != EXEC_NOP);
10269 break;
10270 case OMP_LIST_USE_DEVICE:
10271 if (n->sym->attr.allocatable
10272 || (n->sym->ts.type == BT_CLASS && CLASS_DATA (n->sym)
10273 && CLASS_DATA (n->sym)->attr.allocatable))
10274 gfc_error ("ALLOCATABLE object %qs in %s clause at %L",
10275 n->sym->name, name, &n->where);
10276 if (n->sym->ts.type == BT_CLASS
10277 && CLASS_DATA (n->sym)
10278 && CLASS_DATA (n->sym)->attr.class_pointer)
10279 gfc_error ("POINTER object %qs of polymorphic type in "
10280 "%s clause at %L", n->sym->name, name,
10281 &n->where);
10282 if (n->sym->attr.cray_pointer)
10283 gfc_error ("Cray pointer object %qs in %s clause at %L",
10284 n->sym->name, name, &n->where);
10285 else if (n->sym->attr.cray_pointee)
10286 gfc_error ("Cray pointee object %qs in %s clause at %L",
10287 n->sym->name, name, &n->where);
10288 else if (n->sym->attr.flavor == FL_VARIABLE
10289 && !n->sym->as
10290 && !n->sym->attr.pointer)
10291 gfc_error ("%s clause variable %qs at %L is neither "
10292 "a POINTER nor an array", name,
10293 n->sym->name, &n->where);
10294 /* FALLTHRU */
10295 case OMP_LIST_DEVICE_RESIDENT:
10296 check_symbol_not_pointer (n->sym, n->where, name);
10297 check_array_not_assumed (n->sym, n->where, name);
10298 break;
10299 default:
10300 break;
10301 }
10302 }
10303 break;
10304 }
10305 }
10306 /* OpenMP 5.1: use_device_ptr acts like use_device_addr, except for
10307 type(c_ptr). */
10308 if (omp_clauses->lists[OMP_LIST_USE_DEVICE_PTR])
10309 {
10310 gfc_omp_namelist *n_prev, *n_next, *n_addr;
10311 n_addr = omp_clauses->lists[OMP_LIST_USE_DEVICE_ADDR];
10312 for (; n_addr && n_addr->next; n_addr = n_addr->next)
10313 ;
10314 n_prev = NULL;
10315 n = omp_clauses->lists[OMP_LIST_USE_DEVICE_PTR];
10316 while (n)
10317 {
10318 n_next = n->next;
10319 if (n->sym->ts.type != BT_DERIVED
10320 || n->sym->ts.u.derived->ts.f90_type != BT_VOID)
10321 {
10322 n->next = NULL;
10323 if (n_addr)
10324 n_addr->next = n;
10325 else
10326 omp_clauses->lists[OMP_LIST_USE_DEVICE_ADDR] = n;
10327 n_addr = n;
10328 if (n_prev)
10329 n_prev->next = n_next;
10330 else
10331 omp_clauses->lists[OMP_LIST_USE_DEVICE_PTR] = n_next;
10332 }
10333 else
10334 n_prev = n;
10335 n = n_next;
10336 }
10337 }
10338 if (omp_clauses->safelen_expr)
10339 resolve_positive_int_expr (omp_clauses->safelen_expr, "SAFELEN");
10340 if (omp_clauses->simdlen_expr)
10341 resolve_positive_int_expr (omp_clauses->simdlen_expr, "SIMDLEN");
10342 if (omp_clauses->num_teams_lower)
10343 resolve_positive_int_expr (omp_clauses->num_teams_lower, "NUM_TEAMS");
10344 if (omp_clauses->num_teams_upper)
10345 resolve_positive_int_expr (omp_clauses->num_teams_upper, "NUM_TEAMS");
10346 if (omp_clauses->num_teams_lower
10347 && omp_clauses->num_teams_lower->expr_type == EXPR_CONSTANT
10348 && omp_clauses->num_teams_upper->expr_type == EXPR_CONSTANT
10349 && mpz_cmp (omp_clauses->num_teams_lower->value.integer,
10350 omp_clauses->num_teams_upper->value.integer) > 0)
10351 gfc_warning (OPT_Wopenmp, "NUM_TEAMS lower bound at %L larger than upper "
10352 "bound at %L", &omp_clauses->num_teams_lower->where,
10353 &omp_clauses->num_teams_upper->where);
10354 if (omp_clauses->device)
10355 resolve_scalar_int_expr (omp_clauses->device, "DEVICE");
10356 if (omp_clauses->filter)
10357 resolve_nonnegative_int_expr (omp_clauses->filter, "FILTER");
10358 if (omp_clauses->hint)
10359 {
10360 resolve_scalar_int_expr (omp_clauses->hint, "HINT");
10361 if (omp_clauses->hint->ts.type != BT_INTEGER
10362 || omp_clauses->hint->expr_type != EXPR_CONSTANT
10363 || mpz_sgn (omp_clauses->hint->value.integer) < 0)
10364 gfc_error ("Value of HINT clause at %L shall be a valid "
10365 "constant hint expression", &omp_clauses->hint->where);
10366 }
10367 if (omp_clauses->priority)
10368 resolve_nonnegative_int_expr (omp_clauses->priority, "PRIORITY");
10369 if (omp_clauses->dist_chunk_size)
10370 {
10371 gfc_expr *expr = omp_clauses->dist_chunk_size;
10372 if (!gfc_resolve_expr (expr)
10373 || expr->ts.type != BT_INTEGER || expr->rank != 0)
10374 gfc_error ("DIST_SCHEDULE clause's chunk_size at %L requires "
10375 "a scalar INTEGER expression", &expr->where);
10376 }
10377 if (omp_clauses->thread_limit)
10378 resolve_positive_int_expr (omp_clauses->thread_limit, "THREAD_LIMIT");
10379 if (omp_clauses->grainsize)
10380 resolve_positive_int_expr (omp_clauses->grainsize, "GRAINSIZE");
10381 if (omp_clauses->num_tasks)
10382 resolve_positive_int_expr (omp_clauses->num_tasks, "NUM_TASKS");
10383 if (omp_clauses->grainsize && omp_clauses->num_tasks)
10384 gfc_error ("%<GRAINSIZE%> clause at %L must not be used together with "
10385 "%<NUM_TASKS%> clause", &omp_clauses->grainsize->where);
10386 if (omp_clauses->lists[OMP_LIST_REDUCTION] && omp_clauses->nogroup)
10387 gfc_error ("%<REDUCTION%> clause at %L must not be used together with "
10388 "%<NOGROUP%> clause",
10389 &omp_clauses->lists[OMP_LIST_REDUCTION]->where);
10390 if (omp_clauses->full && omp_clauses->partial)
10391 gfc_error ("%<FULL%> clause at %C must not be used together with "
10392 "%<PARTIAL%> clause");
10393 if (omp_clauses->async)
10394 if (omp_clauses->async_expr)
10395 resolve_scalar_int_expr (omp_clauses->async_expr, "ASYNC");
10396 if (omp_clauses->num_gangs_expr)
10397 resolve_positive_int_expr (omp_clauses->num_gangs_expr, "NUM_GANGS");
10398 if (omp_clauses->num_workers_expr)
10399 resolve_positive_int_expr (omp_clauses->num_workers_expr, "NUM_WORKERS");
10400 if (omp_clauses->vector_length_expr)
10401 resolve_positive_int_expr (omp_clauses->vector_length_expr,
10402 "VECTOR_LENGTH");
10403 if (omp_clauses->gang_num_expr)
10404 resolve_positive_int_expr (omp_clauses->gang_num_expr, "GANG");
10405 if (omp_clauses->gang_static_expr)
10406 resolve_positive_int_expr (omp_clauses->gang_static_expr, "GANG");
10407 if (omp_clauses->worker_expr)
10408 resolve_positive_int_expr (omp_clauses->worker_expr, "WORKER");
10409 if (omp_clauses->vector_expr)
10410 resolve_positive_int_expr (omp_clauses->vector_expr, "VECTOR");
10411 for (el = omp_clauses->wait_list; el; el = el->next)
10412 resolve_scalar_int_expr (el->expr, "WAIT");
10413 if (omp_clauses->collapse && omp_clauses->tile_list)
10414 gfc_error ("Incompatible use of TILE and COLLAPSE at %L", &code->loc);
10415 if (omp_clauses->message)
10416 {
10417 gfc_expr *expr = omp_clauses->message;
10418 if (!gfc_resolve_expr (expr)
10419 || expr->ts.kind != gfc_default_character_kind
10420 || expr->ts.type != BT_CHARACTER || expr->rank != 0)
10421 gfc_error ("MESSAGE clause at %L requires a scalar default-kind "
10422 "CHARACTER expression", &expr->where);
10423 }
10424 if (!openacc
10425 && code
10426 && omp_clauses->lists[OMP_LIST_MAP] == NULL
10427 && omp_clauses->lists[OMP_LIST_USE_DEVICE_PTR] == NULL
10428 && omp_clauses->lists[OMP_LIST_USE_DEVICE_ADDR] == NULL)
10429 {
10430 const char *p = NULL;
10431 switch (code->op)
10432 {
10433 case EXEC_OMP_TARGET_ENTER_DATA: p = "TARGET ENTER DATA"; break;
10434 case EXEC_OMP_TARGET_EXIT_DATA: p = "TARGET EXIT DATA"; break;
10435 default: break;
10436 }
10437 if (code->op == EXEC_OMP_TARGET_DATA)
10438 gfc_error ("TARGET DATA must contain at least one MAP, USE_DEVICE_PTR, "
10439 "or USE_DEVICE_ADDR clause at %L", &code->loc);
10440 else if (p)
10441 gfc_error ("%s must contain at least one MAP clause at %L",
10442 p, &code->loc);
10443 }
10444 if (omp_clauses->sizes_list)
10445 {
10446 gfc_expr_list *el;
10447 for (el = omp_clauses->sizes_list; el; el = el->next)
10448 {
10449 resolve_scalar_int_expr (el->expr, "SIZES");
10450 if (el->expr->expr_type != EXPR_CONSTANT)
10451 gfc_error ("SIZES requires constant expression at %L",
10452 &el->expr->where);
10453 else if (el->expr->expr_type == EXPR_CONSTANT
10454 && el->expr->ts.type == BT_INTEGER
10455 && mpz_sgn (el->expr->value.integer) <= 0)
10456 gfc_error ("INTEGER expression of %s clause at %L must be "
10457 "positive", "SIZES", &el->expr->where);
10458 }
10459 }
10460
10461 if (!openacc && omp_clauses->detach)
10462 {
10463 if (!gfc_resolve_expr (omp_clauses->detach)
10464 || omp_clauses->detach->ts.type != BT_INTEGER
10465 || omp_clauses->detach->ts.kind != gfc_c_intptr_kind
10466 || omp_clauses->detach->rank != 0)
10467 gfc_error ("%qs at %L should be a scalar of type "
10468 "integer(kind=omp_event_handle_kind)",
10469 omp_clauses->detach->symtree->n.sym->name,
10470 &omp_clauses->detach->where);
10471 else if (omp_clauses->detach->symtree->n.sym->attr.dimension > 0)
10472 gfc_error ("The event handle at %L must not be an array element",
10473 &omp_clauses->detach->where);
10474 else if (omp_clauses->detach->symtree->n.sym->ts.type == BT_DERIVED
10475 || omp_clauses->detach->symtree->n.sym->ts.type == BT_CLASS)
10476 gfc_error ("The event handle at %L must not be part of "
10477 "a derived type or class", &omp_clauses->detach->where);
10478
10479 if (omp_clauses->mergeable)
10480 gfc_error ("%<DETACH%> clause at %L must not be used together with "
10481 "%<MERGEABLE%> clause", &omp_clauses->detach->where);
10482 }
10483
10484 if (openacc
10485 && code->op == EXEC_OACC_HOST_DATA
10486 && omp_clauses->lists[OMP_LIST_USE_DEVICE] == NULL)
10487 gfc_error ("%<host_data%> construct at %L requires %<use_device%> clause",
10488 &code->loc);
10489
10490 if (omp_clauses->assume)
10491 gfc_resolve_omp_assumptions (omp_clauses->assume);
10492 }
10493
10494
10495 /* Return true if SYM is ever referenced in EXPR except in the SE node. */
10496
10497 static bool
10498 expr_references_sym (gfc_expr *e, gfc_symbol *s, gfc_expr *se)
10499 {
10500 gfc_actual_arglist *arg;
10501 if (e == NULL || e == se)
10502 return false;
10503 switch (e->expr_type)
10504 {
10505 case EXPR_CONSTANT:
10506 case EXPR_NULL:
10507 case EXPR_VARIABLE:
10508 case EXPR_STRUCTURE:
10509 case EXPR_ARRAY:
10510 if (e->symtree != NULL
10511 && e->symtree->n.sym == s)
10512 return true;
10513 return false;
10514 case EXPR_SUBSTRING:
10515 if (e->ref != NULL
10516 && (expr_references_sym (e->ref->u.ss.start, s, se)
10517 || expr_references_sym (e->ref->u.ss.end, s, se)))
10518 return true;
10519 return false;
10520 case EXPR_OP:
10521 if (expr_references_sym (e->value.op.op2, s, se))
10522 return true;
10523 return expr_references_sym (e->value.op.op1, s, se);
10524 case EXPR_FUNCTION:
10525 for (arg = e->value.function.actual; arg; arg = arg->next)
10526 if (expr_references_sym (arg->expr, s, se))
10527 return true;
10528 return false;
10529 default:
10530 gcc_unreachable ();
10531 }
10532 }
10533
10534
10535 /* If EXPR is a conversion function that widens the type
10536 if WIDENING is true or narrows the type if NARROW is true,
10537 return the inner expression, otherwise return NULL. */
10538
10539 static gfc_expr *
10540 is_conversion (gfc_expr *expr, bool narrowing, bool widening)
10541 {
10542 gfc_typespec *ts1, *ts2;
10543
10544 if (expr->expr_type != EXPR_FUNCTION
10545 || expr->value.function.isym == NULL
10546 || expr->value.function.esym != NULL
10547 || expr->value.function.isym->id != GFC_ISYM_CONVERSION
10548 || (!narrowing && !widening))
10549 return NULL;
10550
10551 if (narrowing && widening)
10552 return expr->value.function.actual->expr;
10553
10554 if (widening)
10555 {
10556 ts1 = &expr->ts;
10557 ts2 = &expr->value.function.actual->expr->ts;
10558 }
10559 else
10560 {
10561 ts1 = &expr->value.function.actual->expr->ts;
10562 ts2 = &expr->ts;
10563 }
10564
10565 if (ts1->type > ts2->type
10566 || (ts1->type == ts2->type && ts1->kind > ts2->kind))
10567 return expr->value.function.actual->expr;
10568
10569 return NULL;
10570 }
10571
10572 static bool
10573 is_scalar_intrinsic_expr (gfc_expr *expr, bool must_be_var, bool conv_ok)
10574 {
10575 if (must_be_var
10576 && (expr->expr_type != EXPR_VARIABLE || !expr->symtree))
10577 {
10578 if (!conv_ok)
10579 return false;
10580 gfc_expr *conv = is_conversion (expr, true, true);
10581 if (!conv)
10582 return false;
10583 if (conv->expr_type != EXPR_VARIABLE || !conv->symtree)
10584 return false;
10585 }
10586 return (expr->rank == 0
10587 && !gfc_is_coindexed (expr)
10588 && (expr->ts.type == BT_INTEGER
10589 || expr->ts.type == BT_REAL
10590 || expr->ts.type == BT_COMPLEX
10591 || expr->ts.type == BT_LOGICAL));
10592 }
10593
10594 static void
10595 resolve_omp_atomic (gfc_code *code)
10596 {
10597 gfc_code *atomic_code = code->block;
10598 gfc_symbol *var;
10599 gfc_expr *stmt_expr2, *capt_expr2;
10600 gfc_omp_atomic_op aop
10601 = (gfc_omp_atomic_op) (atomic_code->ext.omp_clauses->atomic_op
10602 & GFC_OMP_ATOMIC_MASK);
10603 gfc_code *stmt = NULL, *capture_stmt = NULL, *tailing_stmt = NULL;
10604 gfc_expr *comp_cond = NULL;
10605 locus *loc = NULL;
10606
10607 code = code->block->next;
10608 /* resolve_blocks asserts this is initially EXEC_ASSIGN or EXEC_IF
10609 If it changed to EXEC_NOP, assume an error has been emitted already. */
10610 if (code->op == EXEC_NOP)
10611 return;
10612
10613 if (atomic_code->ext.omp_clauses->compare
10614 && atomic_code->ext.omp_clauses->capture)
10615 {
10616 /* Must be either "if (x == e) then; x = d; else; v = x; end if"
10617 or "v = expr" followed/preceded by
10618 "if (x == e) then; x = d; end if" or "if (x == e) x = d". */
10619 gfc_code *next = code;
10620 if (code->op == EXEC_ASSIGN)
10621 {
10622 capture_stmt = code;
10623 next = code->next;
10624 }
10625 if (next->op == EXEC_IF
10626 && next->block
10627 && next->block->op == EXEC_IF
10628 && next->block->next
10629 && next->block->next->op == EXEC_ASSIGN)
10630 {
10631 comp_cond = next->block->expr1;
10632 stmt = next->block->next;
10633 if (stmt->next)
10634 {
10635 loc = &stmt->loc;
10636 goto unexpected;
10637 }
10638 }
10639 else if (capture_stmt)
10640 {
10641 gfc_error ("Expected IF at %L in atomic compare capture",
10642 &next->loc);
10643 return;
10644 }
10645 if (stmt && !capture_stmt && next->block->block)
10646 {
10647 if (next->block->block->expr1)
10648 {
10649 gfc_error ("Expected ELSE at %L in atomic compare capture",
10650 &next->block->block->expr1->where);
10651 return;
10652 }
10653 if (!code->block->block->next
10654 || code->block->block->next->op != EXEC_ASSIGN)
10655 {
10656 loc = (code->block->block->next ? &code->block->block->next->loc
10657 : &code->block->block->loc);
10658 goto unexpected;
10659 }
10660 capture_stmt = code->block->block->next;
10661 if (capture_stmt->next)
10662 {
10663 loc = &capture_stmt->next->loc;
10664 goto unexpected;
10665 }
10666 }
10667 if (stmt && !capture_stmt && next->next->op == EXEC_ASSIGN)
10668 capture_stmt = next->next;
10669 else if (!capture_stmt)
10670 {
10671 loc = &code->loc;
10672 goto unexpected;
10673 }
10674 }
10675 else if (atomic_code->ext.omp_clauses->compare)
10676 {
10677 /* Must be: "if (x == e) then; x = d; end if" or "if (x == e) x = d". */
10678 if (code->op == EXEC_IF
10679 && code->block
10680 && code->block->op == EXEC_IF
10681 && code->block->next
10682 && code->block->next->op == EXEC_ASSIGN)
10683 {
10684 comp_cond = code->block->expr1;
10685 stmt = code->block->next;
10686 if (stmt->next || code->block->block)
10687 {
10688 loc = stmt->next ? &stmt->next->loc : &code->block->block->loc;
10689 goto unexpected;
10690 }
10691 }
10692 else
10693 {
10694 loc = &code->loc;
10695 goto unexpected;
10696 }
10697 }
10698 else if (atomic_code->ext.omp_clauses->capture)
10699 {
10700 /* Must be: "v = x" followed/preceded by "x = ...". */
10701 if (code->op != EXEC_ASSIGN)
10702 goto unexpected;
10703 if (code->next->op != EXEC_ASSIGN)
10704 {
10705 loc = &code->next->loc;
10706 goto unexpected;
10707 }
10708 gfc_expr *expr2, *expr2_next;
10709 expr2 = is_conversion (code->expr2, true, true);
10710 if (expr2 == NULL)
10711 expr2 = code->expr2;
10712 expr2_next = is_conversion (code->next->expr2, true, true);
10713 if (expr2_next == NULL)
10714 expr2_next = code->next->expr2;
10715 if (code->expr1->expr_type == EXPR_VARIABLE
10716 && code->next->expr1->expr_type == EXPR_VARIABLE
10717 && expr2->expr_type == EXPR_VARIABLE
10718 && expr2_next->expr_type == EXPR_VARIABLE)
10719 {
10720 if (code->expr1->symtree->n.sym == expr2_next->symtree->n.sym)
10721 {
10722 stmt = code;
10723 capture_stmt = code->next;
10724 }
10725 else
10726 {
10727 capture_stmt = code;
10728 stmt = code->next;
10729 }
10730 }
10731 else if (expr2->expr_type == EXPR_VARIABLE)
10732 {
10733 capture_stmt = code;
10734 stmt = code->next;
10735 }
10736 else
10737 {
10738 stmt = code;
10739 capture_stmt = code->next;
10740 }
10741 /* Shall be NULL but can happen for invalid code. */
10742 tailing_stmt = code->next->next;
10743 }
10744 else
10745 {
10746 /* x = ... */
10747 stmt = code;
10748 if (!atomic_code->ext.omp_clauses->compare && stmt->op != EXEC_ASSIGN)
10749 goto unexpected;
10750 /* Shall be NULL but can happen for invalid code. */
10751 tailing_stmt = code->next;
10752 }
10753
10754 if (comp_cond)
10755 {
10756 if (comp_cond->expr_type != EXPR_OP
10757 || (comp_cond->value.op.op != INTRINSIC_EQ
10758 && comp_cond->value.op.op != INTRINSIC_EQ_OS
10759 && comp_cond->value.op.op != INTRINSIC_EQV))
10760 {
10761 gfc_error ("Expected %<==%>, %<.EQ.%> or %<.EQV.%> atomic comparison "
10762 "expression at %L", &comp_cond->where);
10763 return;
10764 }
10765 if (!is_scalar_intrinsic_expr (comp_cond->value.op.op1, true, true))
10766 {
10767 gfc_error ("Expected scalar intrinsic variable at %L in atomic "
10768 "comparison", &comp_cond->value.op.op1->where);
10769 return;
10770 }
10771 if (!gfc_resolve_expr (comp_cond->value.op.op2))
10772 return;
10773 if (!is_scalar_intrinsic_expr (comp_cond->value.op.op2, false, false))
10774 {
10775 gfc_error ("Expected scalar intrinsic expression at %L in atomic "
10776 "comparison", &comp_cond->value.op.op1->where);
10777 return;
10778 }
10779 }
10780
10781 if (!is_scalar_intrinsic_expr (stmt->expr1, true, false))
10782 {
10783 gfc_error ("!$OMP ATOMIC statement must set a scalar variable of "
10784 "intrinsic type at %L", &stmt->expr1->where);
10785 return;
10786 }
10787
10788 if (!gfc_resolve_expr (stmt->expr2))
10789 return;
10790 if (!is_scalar_intrinsic_expr (stmt->expr2, false, false))
10791 {
10792 gfc_error ("!$OMP ATOMIC statement must assign an expression of "
10793 "intrinsic type at %L", &stmt->expr2->where);
10794 return;
10795 }
10796
10797 if (gfc_expr_attr (stmt->expr1).allocatable)
10798 {
10799 gfc_error ("!$OMP ATOMIC with ALLOCATABLE variable at %L",
10800 &stmt->expr1->where);
10801 return;
10802 }
10803
10804 /* Should be diagnosed above already. */
10805 gcc_assert (tailing_stmt == NULL);
10806
10807 var = stmt->expr1->symtree->n.sym;
10808 stmt_expr2 = is_conversion (stmt->expr2, true, true);
10809 if (stmt_expr2 == NULL)
10810 stmt_expr2 = stmt->expr2;
10811
10812 switch (aop)
10813 {
10814 case GFC_OMP_ATOMIC_READ:
10815 if (stmt_expr2->expr_type != EXPR_VARIABLE)
10816 gfc_error ("!$OMP ATOMIC READ statement must read from a scalar "
10817 "variable of intrinsic type at %L", &stmt_expr2->where);
10818 return;
10819 case GFC_OMP_ATOMIC_WRITE:
10820 if (expr_references_sym (stmt_expr2, var, NULL))
10821 gfc_error ("expr in !$OMP ATOMIC WRITE assignment var = expr "
10822 "must be scalar and cannot reference var at %L",
10823 &stmt_expr2->where);
10824 return;
10825 default:
10826 break;
10827 }
10828
10829 if (atomic_code->ext.omp_clauses->capture)
10830 {
10831 if (!is_scalar_intrinsic_expr (capture_stmt->expr1, true, false))
10832 {
10833 gfc_error ("!$OMP ATOMIC capture-statement must set a scalar "
10834 "variable of intrinsic type at %L",
10835 &capture_stmt->expr1->where);
10836 return;
10837 }
10838
10839 if (!is_scalar_intrinsic_expr (capture_stmt->expr2, true, true))
10840 {
10841 gfc_error ("!$OMP ATOMIC capture-statement requires a scalar variable"
10842 " of intrinsic type at %L", &capture_stmt->expr2->where);
10843 return;
10844 }
10845 capt_expr2 = is_conversion (capture_stmt->expr2, true, true);
10846 if (capt_expr2 == NULL)
10847 capt_expr2 = capture_stmt->expr2;
10848
10849 if (capt_expr2->symtree->n.sym != var)
10850 {
10851 gfc_error ("!$OMP ATOMIC CAPTURE capture statement reads from "
10852 "different variable than update statement writes "
10853 "into at %L", &capture_stmt->expr2->where);
10854 return;
10855 }
10856 }
10857
10858 if (atomic_code->ext.omp_clauses->compare)
10859 {
10860 gfc_expr *var_expr;
10861 if (comp_cond->value.op.op1->expr_type == EXPR_VARIABLE)
10862 var_expr = comp_cond->value.op.op1;
10863 else
10864 var_expr = comp_cond->value.op.op1->value.function.actual->expr;
10865 if (var_expr->symtree->n.sym != var)
10866 {
10867 gfc_error ("For !$OMP ATOMIC COMPARE, the first operand in comparison"
10868 " at %L must be the variable %qs that the update statement"
10869 " writes into at %L", &var_expr->where, var->name,
10870 &stmt->expr1->where);
10871 return;
10872 }
10873 if (stmt_expr2->rank != 0 || expr_references_sym (stmt_expr2, var, NULL))
10874 {
10875 gfc_error ("expr in !$OMP ATOMIC COMPARE assignment var = expr "
10876 "must be scalar and cannot reference var at %L",
10877 &stmt_expr2->where);
10878 return;
10879 }
10880 }
10881 else if (atomic_code->ext.omp_clauses->capture
10882 && !expr_references_sym (stmt_expr2, var, NULL))
10883 atomic_code->ext.omp_clauses->atomic_op
10884 = (gfc_omp_atomic_op) (atomic_code->ext.omp_clauses->atomic_op
10885 | GFC_OMP_ATOMIC_SWAP);
10886 else if (stmt_expr2->expr_type == EXPR_OP)
10887 {
10888 gfc_expr *v = NULL, *e, *c;
10889 gfc_intrinsic_op op = stmt_expr2->value.op.op;
10890 gfc_intrinsic_op alt_op = INTRINSIC_NONE;
10891
10892 if (atomic_code->ext.omp_clauses->fail != OMP_MEMORDER_UNSET)
10893 gfc_error ("!$OMP ATOMIC UPDATE at %L with FAIL clause requires either"
10894 " the COMPARE clause or using the intrinsic MIN/MAX "
10895 "procedure", &atomic_code->loc);
10896 switch (op)
10897 {
10898 case INTRINSIC_PLUS:
10899 alt_op = INTRINSIC_MINUS;
10900 break;
10901 case INTRINSIC_TIMES:
10902 alt_op = INTRINSIC_DIVIDE;
10903 break;
10904 case INTRINSIC_MINUS:
10905 alt_op = INTRINSIC_PLUS;
10906 break;
10907 case INTRINSIC_DIVIDE:
10908 alt_op = INTRINSIC_TIMES;
10909 break;
10910 case INTRINSIC_AND:
10911 case INTRINSIC_OR:
10912 break;
10913 case INTRINSIC_EQV:
10914 alt_op = INTRINSIC_NEQV;
10915 break;
10916 case INTRINSIC_NEQV:
10917 alt_op = INTRINSIC_EQV;
10918 break;
10919 default:
10920 gfc_error ("!$OMP ATOMIC assignment operator must be binary "
10921 "+, *, -, /, .AND., .OR., .EQV. or .NEQV. at %L",
10922 &stmt_expr2->where);
10923 return;
10924 }
10925
10926 /* Check for var = var op expr resp. var = expr op var where
10927 expr doesn't reference var and var op expr is mathematically
10928 equivalent to var op (expr) resp. expr op var equivalent to
10929 (expr) op var. We rely here on the fact that the matcher
10930 for x op1 y op2 z where op1 and op2 have equal precedence
10931 returns (x op1 y) op2 z. */
10932 e = stmt_expr2->value.op.op2;
10933 if (e->expr_type == EXPR_VARIABLE
10934 && e->symtree != NULL
10935 && e->symtree->n.sym == var)
10936 v = e;
10937 else if ((c = is_conversion (e, false, true)) != NULL
10938 && c->expr_type == EXPR_VARIABLE
10939 && c->symtree != NULL
10940 && c->symtree->n.sym == var)
10941 v = c;
10942 else
10943 {
10944 gfc_expr **p = NULL, **q;
10945 for (q = &stmt_expr2->value.op.op1; (e = *q) != NULL; )
10946 if (e->expr_type == EXPR_VARIABLE
10947 && e->symtree != NULL
10948 && e->symtree->n.sym == var)
10949 {
10950 v = e;
10951 break;
10952 }
10953 else if ((c = is_conversion (e, false, true)) != NULL)
10954 q = &e->value.function.actual->expr;
10955 else if (e->expr_type != EXPR_OP
10956 || (e->value.op.op != op
10957 && e->value.op.op != alt_op)
10958 || e->rank != 0)
10959 break;
10960 else
10961 {
10962 p = q;
10963 q = &e->value.op.op1;
10964 }
10965
10966 if (v == NULL)
10967 {
10968 gfc_error ("!$OMP ATOMIC assignment must be var = var op expr "
10969 "or var = expr op var at %L", &stmt_expr2->where);
10970 return;
10971 }
10972
10973 if (p != NULL)
10974 {
10975 e = *p;
10976 switch (e->value.op.op)
10977 {
10978 case INTRINSIC_MINUS:
10979 case INTRINSIC_DIVIDE:
10980 case INTRINSIC_EQV:
10981 case INTRINSIC_NEQV:
10982 gfc_error ("!$OMP ATOMIC var = var op expr not "
10983 "mathematically equivalent to var = var op "
10984 "(expr) at %L", &stmt_expr2->where);
10985 break;
10986 default:
10987 break;
10988 }
10989
10990 /* Canonicalize into var = var op (expr). */
10991 *p = e->value.op.op2;
10992 e->value.op.op2 = stmt_expr2;
10993 e->ts = stmt_expr2->ts;
10994 if (stmt->expr2 == stmt_expr2)
10995 stmt->expr2 = stmt_expr2 = e;
10996 else
10997 stmt->expr2->value.function.actual->expr = stmt_expr2 = e;
10998
10999 if (!gfc_compare_types (&stmt_expr2->value.op.op1->ts,
11000 &stmt_expr2->ts))
11001 {
11002 for (p = &stmt_expr2->value.op.op1; *p != v;
11003 p = &(*p)->value.function.actual->expr)
11004 ;
11005 *p = NULL;
11006 gfc_free_expr (stmt_expr2->value.op.op1);
11007 stmt_expr2->value.op.op1 = v;
11008 gfc_convert_type (v, &stmt_expr2->ts, 2);
11009 }
11010 }
11011 }
11012
11013 if (e->rank != 0 || expr_references_sym (stmt->expr2, var, v))
11014 {
11015 gfc_error ("expr in !$OMP ATOMIC assignment var = var op expr "
11016 "must be scalar and cannot reference var at %L",
11017 &stmt_expr2->where);
11018 return;
11019 }
11020 }
11021 else if (stmt_expr2->expr_type == EXPR_FUNCTION
11022 && stmt_expr2->value.function.isym != NULL
11023 && stmt_expr2->value.function.esym == NULL
11024 && stmt_expr2->value.function.actual != NULL
11025 && stmt_expr2->value.function.actual->next != NULL)
11026 {
11027 gfc_actual_arglist *arg, *var_arg;
11028
11029 switch (stmt_expr2->value.function.isym->id)
11030 {
11031 case GFC_ISYM_MIN:
11032 case GFC_ISYM_MAX:
11033 break;
11034 case GFC_ISYM_IAND:
11035 case GFC_ISYM_IOR:
11036 case GFC_ISYM_IEOR:
11037 if (stmt_expr2->value.function.actual->next->next != NULL)
11038 {
11039 gfc_error ("!$OMP ATOMIC assignment intrinsic IAND, IOR "
11040 "or IEOR must have two arguments at %L",
11041 &stmt_expr2->where);
11042 return;
11043 }
11044 break;
11045 default:
11046 gfc_error ("!$OMP ATOMIC assignment intrinsic must be "
11047 "MIN, MAX, IAND, IOR or IEOR at %L",
11048 &stmt_expr2->where);
11049 return;
11050 }
11051
11052 var_arg = NULL;
11053 for (arg = stmt_expr2->value.function.actual; arg; arg = arg->next)
11054 {
11055 gfc_expr *e = NULL;
11056 if (arg == stmt_expr2->value.function.actual
11057 || (var_arg == NULL && arg->next == NULL))
11058 {
11059 e = is_conversion (arg->expr, false, true);
11060 if (!e)
11061 e = arg->expr;
11062 if (e->expr_type == EXPR_VARIABLE
11063 && e->symtree != NULL
11064 && e->symtree->n.sym == var)
11065 var_arg = arg;
11066 }
11067 if ((!var_arg || !e) && expr_references_sym (arg->expr, var, NULL))
11068 {
11069 gfc_error ("!$OMP ATOMIC intrinsic arguments except one must "
11070 "not reference %qs at %L",
11071 var->name, &arg->expr->where);
11072 return;
11073 }
11074 if (arg->expr->rank != 0)
11075 {
11076 gfc_error ("!$OMP ATOMIC intrinsic arguments must be scalar "
11077 "at %L", &arg->expr->where);
11078 return;
11079 }
11080 }
11081
11082 if (var_arg == NULL)
11083 {
11084 gfc_error ("First or last !$OMP ATOMIC intrinsic argument must "
11085 "be %qs at %L", var->name, &stmt_expr2->where);
11086 return;
11087 }
11088
11089 if (var_arg != stmt_expr2->value.function.actual)
11090 {
11091 /* Canonicalize, so that var comes first. */
11092 gcc_assert (var_arg->next == NULL);
11093 for (arg = stmt_expr2->value.function.actual;
11094 arg->next != var_arg; arg = arg->next)
11095 ;
11096 var_arg->next = stmt_expr2->value.function.actual;
11097 stmt_expr2->value.function.actual = var_arg;
11098 arg->next = NULL;
11099 }
11100 }
11101 else
11102 gfc_error ("!$OMP ATOMIC assignment must have an operator or "
11103 "intrinsic on right hand side at %L", &stmt_expr2->where);
11104 return;
11105
11106 unexpected:
11107 gfc_error ("unexpected !$OMP ATOMIC expression at %L",
11108 loc ? loc : &code->loc);
11109 return;
11110 }
11111
11112
11113 static struct fortran_omp_context
11114 {
11115 gfc_code *code;
11116 hash_set<gfc_symbol *> *sharing_clauses;
11117 hash_set<gfc_symbol *> *private_iterators;
11118 struct fortran_omp_context *previous;
11119 bool is_openmp;
11120 } *omp_current_ctx;
11121 static gfc_code *omp_current_do_code;
11122 static int omp_current_do_collapse;
11123
11124 /* Forward declaration for mutually recursive functions. */
11125 static gfc_code *
11126 find_nested_loop_in_block (gfc_code *block);
11127
11128 /* Return the first nested DO loop in CHAIN, or NULL if there
11129 isn't one. Does no error checking on intervening code. */
11130
11131 static gfc_code *
11132 find_nested_loop_in_chain (gfc_code *chain)
11133 {
11134 gfc_code *code;
11135
11136 if (!chain)
11137 return NULL;
11138
11139 for (code = chain; code; code = code->next)
11140 switch (code->op)
11141 {
11142 case EXEC_DO:
11143 case EXEC_OMP_TILE:
11144 case EXEC_OMP_UNROLL:
11145 return code;
11146 case EXEC_BLOCK:
11147 if (gfc_code *c = find_nested_loop_in_block (code))
11148 return c;
11149 break;
11150 default:
11151 break;
11152 }
11153 return NULL;
11154 }
11155
11156 /* Return the first nested DO loop in BLOCK, or NULL if there
11157 isn't one. Does no error checking on intervening code. */
11158 static gfc_code *
11159 find_nested_loop_in_block (gfc_code *block)
11160 {
11161 gfc_namespace *ns;
11162 gcc_assert (block->op == EXEC_BLOCK);
11163 ns = block->ext.block.ns;
11164 gcc_assert (ns);
11165 return find_nested_loop_in_chain (ns->code);
11166 }
11167
11168 void
11169 gfc_resolve_omp_do_blocks (gfc_code *code, gfc_namespace *ns)
11170 {
11171 if (code->block->next && code->block->next->op == EXEC_DO)
11172 {
11173 int i;
11174
11175 omp_current_do_code = code->block->next;
11176 if (code->ext.omp_clauses->orderedc)
11177 omp_current_do_collapse = code->ext.omp_clauses->orderedc;
11178 else if (code->ext.omp_clauses->collapse)
11179 omp_current_do_collapse = code->ext.omp_clauses->collapse;
11180 else if (code->ext.omp_clauses->sizes_list)
11181 omp_current_do_collapse
11182 = gfc_expr_list_len (code->ext.omp_clauses->sizes_list);
11183 else
11184 omp_current_do_collapse = 1;
11185 if (code->ext.omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN])
11186 {
11187 /* Checking that there is a matching EXEC_OMP_SCAN in the
11188 innermost body cannot be deferred to resolve_omp_do because
11189 we process directives nested in the loop before we get
11190 there. */
11191 locus *loc
11192 = &code->ext.omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN]->where;
11193 gfc_code *c;
11194
11195 for (i = 1, c = omp_current_do_code;
11196 i < omp_current_do_collapse; i++)
11197 {
11198 c = find_nested_loop_in_chain (c->block->next);
11199 if (!c || c->op != EXEC_DO || c->block == NULL)
11200 break;
11201 }
11202
11203 /* Skip this if we don't have enough nested loops. That
11204 problem will be diagnosed elsewhere. */
11205 if (c && c->op == EXEC_DO)
11206 {
11207 gfc_code *block = c->block ? c->block->next : NULL;
11208 if (block && block->op != EXEC_OMP_SCAN)
11209 while (block && block->next
11210 && block->next->op != EXEC_OMP_SCAN)
11211 block = block->next;
11212 if (!block
11213 || (block->op != EXEC_OMP_SCAN
11214 && (!block->next || block->next->op != EXEC_OMP_SCAN)))
11215 gfc_error ("With INSCAN at %L, expected loop body with "
11216 "!$OMP SCAN between two "
11217 "structured block sequences", loc);
11218 else
11219 {
11220 if (block->op == EXEC_OMP_SCAN)
11221 gfc_warning (OPT_Wopenmp,
11222 "!$OMP SCAN at %L with zero executable "
11223 "statements in preceding structured block "
11224 "sequence", &block->loc);
11225 if ((block->op == EXEC_OMP_SCAN && !block->next)
11226 || (block->next && block->next->op == EXEC_OMP_SCAN
11227 && !block->next->next))
11228 gfc_warning (OPT_Wopenmp,
11229 "!$OMP SCAN at %L with zero executable "
11230 "statements in succeeding structured block "
11231 "sequence", block->op == EXEC_OMP_SCAN
11232 ? &block->loc : &block->next->loc);
11233 }
11234 if (block && block->op != EXEC_OMP_SCAN)
11235 block = block->next;
11236 if (block && block->op == EXEC_OMP_SCAN)
11237 /* Mark 'omp scan' as checked; flag will be unset later. */
11238 block->ext.omp_clauses->if_present = true;
11239 }
11240 }
11241 }
11242 gfc_resolve_blocks (code->block, ns);
11243 omp_current_do_collapse = 0;
11244 omp_current_do_code = NULL;
11245 }
11246
11247
11248 void
11249 gfc_resolve_omp_parallel_blocks (gfc_code *code, gfc_namespace *ns)
11250 {
11251 struct fortran_omp_context ctx;
11252 gfc_omp_clauses *omp_clauses = code->ext.omp_clauses;
11253 gfc_omp_namelist *n;
11254 int list;
11255
11256 ctx.code = code;
11257 ctx.sharing_clauses = new hash_set<gfc_symbol *>;
11258 ctx.private_iterators = new hash_set<gfc_symbol *>;
11259 ctx.previous = omp_current_ctx;
11260 ctx.is_openmp = true;
11261 omp_current_ctx = &ctx;
11262
11263 for (list = 0; list < OMP_LIST_NUM; list++)
11264 switch (list)
11265 {
11266 case OMP_LIST_SHARED:
11267 case OMP_LIST_PRIVATE:
11268 case OMP_LIST_FIRSTPRIVATE:
11269 case OMP_LIST_LASTPRIVATE:
11270 case OMP_LIST_REDUCTION:
11271 case OMP_LIST_REDUCTION_INSCAN:
11272 case OMP_LIST_REDUCTION_TASK:
11273 case OMP_LIST_IN_REDUCTION:
11274 case OMP_LIST_TASK_REDUCTION:
11275 case OMP_LIST_LINEAR:
11276 for (n = omp_clauses->lists[list]; n; n = n->next)
11277 ctx.sharing_clauses->add (n->sym);
11278 break;
11279 default:
11280 break;
11281 }
11282
11283 switch (code->op)
11284 {
11285 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
11286 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
11287 case EXEC_OMP_MASKED_TASKLOOP:
11288 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
11289 case EXEC_OMP_MASTER_TASKLOOP:
11290 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
11291 case EXEC_OMP_PARALLEL_DO:
11292 case EXEC_OMP_PARALLEL_DO_SIMD:
11293 case EXEC_OMP_PARALLEL_LOOP:
11294 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
11295 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
11296 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
11297 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
11298 case EXEC_OMP_TARGET_PARALLEL_DO:
11299 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
11300 case EXEC_OMP_TARGET_PARALLEL_LOOP:
11301 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11302 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11303 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11304 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11305 case EXEC_OMP_TARGET_TEAMS_LOOP:
11306 case EXEC_OMP_TASKLOOP:
11307 case EXEC_OMP_TASKLOOP_SIMD:
11308 case EXEC_OMP_TEAMS_DISTRIBUTE:
11309 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11310 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11311 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11312 case EXEC_OMP_TEAMS_LOOP:
11313 gfc_resolve_omp_do_blocks (code, ns);
11314 break;
11315 default:
11316 gfc_resolve_blocks (code->block, ns);
11317 }
11318
11319 omp_current_ctx = ctx.previous;
11320 delete ctx.sharing_clauses;
11321 delete ctx.private_iterators;
11322 }
11323
11324
11325 /* Save and clear openmp.cc private state. */
11326
11327 void
11328 gfc_omp_save_and_clear_state (struct gfc_omp_saved_state *state)
11329 {
11330 state->ptrs[0] = omp_current_ctx;
11331 state->ptrs[1] = omp_current_do_code;
11332 state->ints[0] = omp_current_do_collapse;
11333 omp_current_ctx = NULL;
11334 omp_current_do_code = NULL;
11335 omp_current_do_collapse = 0;
11336 }
11337
11338
11339 /* Restore openmp.cc private state from the saved state. */
11340
11341 void
11342 gfc_omp_restore_state (struct gfc_omp_saved_state *state)
11343 {
11344 omp_current_ctx = (struct fortran_omp_context *) state->ptrs[0];
11345 omp_current_do_code = (gfc_code *) state->ptrs[1];
11346 omp_current_do_collapse = state->ints[0];
11347 }
11348
11349
11350 /* Note a DO iterator variable. This is special in !$omp parallel
11351 construct, where they are predetermined private. */
11352
11353 void
11354 gfc_resolve_do_iterator (gfc_code *code, gfc_symbol *sym, bool add_clause)
11355 {
11356 if (omp_current_ctx == NULL)
11357 return;
11358
11359 int i = omp_current_do_collapse;
11360 gfc_code *c = omp_current_do_code;
11361
11362 if (sym->attr.threadprivate)
11363 return;
11364
11365 /* !$omp do and !$omp parallel do iteration variable is predetermined
11366 private just in the !$omp do resp. !$omp parallel do construct,
11367 with no implications for the outer parallel constructs. */
11368
11369 while (i-- >= 1 && c)
11370 {
11371 if (code == c)
11372 return;
11373 c = find_nested_loop_in_chain (c->block->next);
11374 if (c && (c->op == EXEC_OMP_TILE || c->op == EXEC_OMP_UNROLL))
11375 return;
11376 }
11377
11378 /* An openacc context may represent a data clause. Abort if so. */
11379 if (!omp_current_ctx->is_openmp && !oacc_is_loop (omp_current_ctx->code))
11380 return;
11381
11382 if (omp_current_ctx->sharing_clauses->contains (sym))
11383 return;
11384
11385 if (! omp_current_ctx->private_iterators->add (sym) && add_clause)
11386 {
11387 gfc_omp_clauses *omp_clauses = omp_current_ctx->code->ext.omp_clauses;
11388 gfc_omp_namelist *p;
11389
11390 p = gfc_get_omp_namelist ();
11391 p->sym = sym;
11392 p->where = omp_current_ctx->code->loc;
11393 p->next = omp_clauses->lists[OMP_LIST_PRIVATE];
11394 omp_clauses->lists[OMP_LIST_PRIVATE] = p;
11395 }
11396 }
11397
11398 static void
11399 handle_local_var (gfc_symbol *sym)
11400 {
11401 if (sym->attr.flavor != FL_VARIABLE
11402 || sym->as != NULL
11403 || (sym->ts.type != BT_INTEGER && sym->ts.type != BT_REAL))
11404 return;
11405 gfc_resolve_do_iterator (sym->ns->code, sym, false);
11406 }
11407
11408 void
11409 gfc_resolve_omp_local_vars (gfc_namespace *ns)
11410 {
11411 if (omp_current_ctx)
11412 gfc_traverse_ns (ns, handle_local_var);
11413 }
11414
11415
11416 /* Error checking on intervening code uses a code walker. */
11417
11418 struct icode_error_state
11419 {
11420 const char *name;
11421 bool errorp;
11422 gfc_code *nested;
11423 gfc_code *next;
11424 };
11425
11426 static int
11427 icode_code_error_callback (gfc_code **codep,
11428 int *walk_subtrees ATTRIBUTE_UNUSED, void *opaque)
11429 {
11430 gfc_code *code = *codep;
11431 icode_error_state *state = (icode_error_state *)opaque;
11432
11433 /* gfc_code_walker walks down CODE's next chain as well as
11434 walking things that are actually nested in CODE. We need to
11435 special-case traversal of outer blocks, so stop immediately if we
11436 are heading down such a next chain. */
11437 if (code == state->next)
11438 return 1;
11439
11440 switch (code->op)
11441 {
11442 case EXEC_DO:
11443 case EXEC_DO_WHILE:
11444 case EXEC_DO_CONCURRENT:
11445 gfc_error ("%s cannot contain loop in intervening code at %L",
11446 state->name, &code->loc);
11447 state->errorp = true;
11448 break;
11449 case EXEC_CYCLE:
11450 case EXEC_EXIT:
11451 /* Errors have already been diagnosed in match_exit_cycle. */
11452 state->errorp = true;
11453 break;
11454 case EXEC_OMP_CRITICAL:
11455 case EXEC_OMP_DO:
11456 case EXEC_OMP_FLUSH:
11457 case EXEC_OMP_MASTER:
11458 case EXEC_OMP_ORDERED:
11459 case EXEC_OMP_PARALLEL:
11460 case EXEC_OMP_PARALLEL_DO:
11461 case EXEC_OMP_PARALLEL_SECTIONS:
11462 case EXEC_OMP_PARALLEL_WORKSHARE:
11463 case EXEC_OMP_SECTIONS:
11464 case EXEC_OMP_SINGLE:
11465 case EXEC_OMP_WORKSHARE:
11466 case EXEC_OMP_ATOMIC:
11467 case EXEC_OMP_BARRIER:
11468 case EXEC_OMP_END_NOWAIT:
11469 case EXEC_OMP_END_SINGLE:
11470 case EXEC_OMP_TASK:
11471 case EXEC_OMP_TASKWAIT:
11472 case EXEC_OMP_TASKYIELD:
11473 case EXEC_OMP_CANCEL:
11474 case EXEC_OMP_CANCELLATION_POINT:
11475 case EXEC_OMP_TASKGROUP:
11476 case EXEC_OMP_SIMD:
11477 case EXEC_OMP_DO_SIMD:
11478 case EXEC_OMP_PARALLEL_DO_SIMD:
11479 case EXEC_OMP_TARGET:
11480 case EXEC_OMP_TARGET_DATA:
11481 case EXEC_OMP_TEAMS:
11482 case EXEC_OMP_DISTRIBUTE:
11483 case EXEC_OMP_DISTRIBUTE_SIMD:
11484 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
11485 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
11486 case EXEC_OMP_TARGET_TEAMS:
11487 case EXEC_OMP_TEAMS_DISTRIBUTE:
11488 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11489 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11490 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11491 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11492 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11493 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11494 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11495 case EXEC_OMP_TARGET_UPDATE:
11496 case EXEC_OMP_END_CRITICAL:
11497 case EXEC_OMP_TARGET_ENTER_DATA:
11498 case EXEC_OMP_TARGET_EXIT_DATA:
11499 case EXEC_OMP_TARGET_PARALLEL:
11500 case EXEC_OMP_TARGET_PARALLEL_DO:
11501 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
11502 case EXEC_OMP_TARGET_SIMD:
11503 case EXEC_OMP_TASKLOOP:
11504 case EXEC_OMP_TASKLOOP_SIMD:
11505 case EXEC_OMP_SCAN:
11506 case EXEC_OMP_DEPOBJ:
11507 case EXEC_OMP_PARALLEL_MASTER:
11508 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
11509 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
11510 case EXEC_OMP_MASTER_TASKLOOP:
11511 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
11512 case EXEC_OMP_LOOP:
11513 case EXEC_OMP_PARALLEL_LOOP:
11514 case EXEC_OMP_TEAMS_LOOP:
11515 case EXEC_OMP_TARGET_PARALLEL_LOOP:
11516 case EXEC_OMP_TARGET_TEAMS_LOOP:
11517 case EXEC_OMP_MASKED:
11518 case EXEC_OMP_PARALLEL_MASKED:
11519 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
11520 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
11521 case EXEC_OMP_MASKED_TASKLOOP:
11522 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
11523 case EXEC_OMP_SCOPE:
11524 case EXEC_OMP_ERROR:
11525 case EXEC_OMP_DISPATCH:
11526 gfc_error ("%s cannot contain OpenMP directive in intervening code "
11527 "at %L",
11528 state->name, &code->loc);
11529 state->errorp = true;
11530 break;
11531 case EXEC_CALL:
11532 /* Per OpenMP 5.2, the "omp_" prefix is reserved, so we don't have to
11533 consider the possibility that some locally-bound definition
11534 overrides the runtime routine. */
11535 if (code->resolved_sym
11536 && omp_runtime_api_procname (code->resolved_sym->name))
11537 {
11538 gfc_error ("%s cannot contain OpenMP API call in intervening code "
11539 "at %L",
11540 state->name, &code->loc);
11541 state->errorp = true;
11542 }
11543 break;
11544 default:
11545 break;
11546 }
11547 return 0;
11548 }
11549
11550 static int
11551 icode_expr_error_callback (gfc_expr **expr,
11552 int *walk_subtrees ATTRIBUTE_UNUSED, void *opaque)
11553 {
11554 icode_error_state *state = (icode_error_state *)opaque;
11555
11556 switch ((*expr)->expr_type)
11557 {
11558 /* As for EXPR_CALL with "omp_"-prefixed symbols. */
11559 case EXPR_FUNCTION:
11560 {
11561 gfc_symbol *sym = (*expr)->value.function.esym;
11562 if (sym && omp_runtime_api_procname (sym->name))
11563 {
11564 gfc_error ("%s cannot contain OpenMP API call in intervening code "
11565 "at %L",
11566 state->name, &((*expr)->where));
11567 state->errorp = true;
11568 }
11569 }
11570
11571 break;
11572 default:
11573 break;
11574 }
11575
11576 /* FIXME: The description of canonical loop form in the OpenMP standard
11577 also says "array expressions" are not permitted in intervening code.
11578 That term is not defined in either the OpenMP spec or the Fortran
11579 standard, although the latter uses it informally to refer to any
11580 expression that is not scalar-valued. It is also apparently not the
11581 thing GCC internally calls EXPR_ARRAY. It seems the intent of the
11582 OpenMP restriction is to disallow elemental operations/intrinsics
11583 (including things that are not expressions, like assignment
11584 statements) that generate implicit loops over array operands
11585 (even if the result is a scalar), but even if the spec said
11586 that there is no list of all the cases that would be forbidden.
11587 This is OpenMP issue 3326. */
11588
11589 return 0;
11590 }
11591
11592 static void
11593 diagnose_intervening_code_errors_1 (gfc_code *chain,
11594 struct icode_error_state *state)
11595 {
11596 gfc_code *code;
11597 for (code = chain; code; code = code->next)
11598 {
11599 if (code == state->nested)
11600 /* Do not walk the nested loop or its body, we are only
11601 interested in intervening code. */
11602 ;
11603 else if (code->op == EXEC_BLOCK
11604 && find_nested_loop_in_block (code) == state->nested)
11605 /* This block contains the nested loop, recurse on its
11606 statements. */
11607 {
11608 gfc_namespace* ns = code->ext.block.ns;
11609 diagnose_intervening_code_errors_1 (ns->code, state);
11610 }
11611 else
11612 /* Treat the whole statement as a unit. */
11613 {
11614 gfc_code *temp = state->next;
11615 state->next = code->next;
11616 gfc_code_walker (&code, icode_code_error_callback,
11617 icode_expr_error_callback, state);
11618 state->next = temp;
11619 }
11620 }
11621 }
11622
11623 /* Diagnose intervening code errors in BLOCK with nested loop NESTED.
11624 NAME is the user-friendly name of the OMP directive, used for error
11625 messages. Returns true if any error was found. */
11626 static bool
11627 diagnose_intervening_code_errors (gfc_code *chain, const char *name,
11628 gfc_code *nested)
11629 {
11630 struct icode_error_state state;
11631 state.name = name;
11632 state.errorp = false;
11633 state.nested = nested;
11634 state.next = NULL;
11635 diagnose_intervening_code_errors_1 (chain, &state);
11636 return state.errorp;
11637 }
11638
11639 /* Helper function for restructure_intervening_code: wrap CHAIN in
11640 a marker to indicate that it is a structured block sequence. That
11641 information will be used later on (in omp-low.cc) for error checking. */
11642 static gfc_code *
11643 make_structured_block (gfc_code *chain)
11644 {
11645 gcc_assert (chain);
11646 gfc_namespace *ns = gfc_build_block_ns (gfc_current_ns);
11647 gfc_code *result = gfc_get_code (EXEC_BLOCK);
11648 result->op = EXEC_BLOCK;
11649 result->ext.block.ns = ns;
11650 result->ext.block.assoc = NULL;
11651 result->loc = chain->loc;
11652 ns->omp_structured_block = 1;
11653 ns->code = chain;
11654 return result;
11655 }
11656
11657 /* Push intervening code surrounding a loop, including nested scopes,
11658 into the body of the loop. CHAINP is the pointer to the head of
11659 the next-chain to scan, OUTER_LOOP is the EXEC_DO for the next outer
11660 loop level, and COLLAPSE is the number of nested loops we need to
11661 process.
11662 Note that CHAINP may point at outer_loop->block->next when we
11663 are scanning the body of a loop, but if there is an intervening block
11664 CHAINP points into the block's chain rather than its enclosing outer
11665 loop. This is why OUTER_LOOP is passed separately. */
11666 static gfc_code *
11667 restructure_intervening_code (gfc_code **chainp, gfc_code *outer_loop,
11668 int count)
11669 {
11670 gfc_code *code;
11671 gfc_code *head = *chainp;
11672 gfc_code *tail = NULL;
11673 gfc_code *innermost_loop = NULL;
11674
11675 for (code = *chainp; code; code = code->next, chainp = &(*chainp)->next)
11676 {
11677 if (code->op == EXEC_DO)
11678 {
11679 /* Cut CODE free from its chain, leaving the ends dangling. */
11680 *chainp = NULL;
11681 tail = code->next;
11682 code->next = NULL;
11683
11684 if (count == 1)
11685 innermost_loop = code;
11686 else
11687 innermost_loop
11688 = restructure_intervening_code (&code->block->next,
11689 code, count - 1);
11690 break;
11691 }
11692 else if (code->op == EXEC_BLOCK
11693 && find_nested_loop_in_block (code))
11694 {
11695 gfc_namespace *ns = code->ext.block.ns;
11696
11697 /* Cut CODE free from its chain, leaving the ends dangling. */
11698 *chainp = NULL;
11699 tail = code->next;
11700 code->next = NULL;
11701
11702 innermost_loop
11703 = restructure_intervening_code (&ns->code, outer_loop,
11704 count);
11705
11706 /* At this point we have already pulled out the nested loop and
11707 pointed outer_loop at it, and moved the intervening code that
11708 was previously in the block into the body of innermost_loop.
11709 Now we want to move the BLOCK itself so it wraps the entire
11710 current body of innermost_loop. */
11711 ns->code = innermost_loop->block->next;
11712 innermost_loop->block->next = code;
11713 break;
11714 }
11715 }
11716
11717 gcc_assert (innermost_loop);
11718
11719 /* Now we have split the intervening code into two parts:
11720 head is the start of the part before the loop/block, terminating
11721 at *chainp, and tail is the part after it. Mark each part as
11722 a structured block sequence, and splice the two parts around the
11723 existing body of the innermost loop. */
11724 if (head != code)
11725 {
11726 gfc_code *block = make_structured_block (head);
11727 if (innermost_loop->block->next)
11728 gfc_append_code (block, innermost_loop->block->next);
11729 innermost_loop->block->next = block;
11730 }
11731 if (tail)
11732 {
11733 gfc_code *block = make_structured_block (tail);
11734 if (innermost_loop->block->next)
11735 gfc_append_code (innermost_loop->block->next, block);
11736 else
11737 innermost_loop->block->next = block;
11738 }
11739
11740 /* For loops, finally splice CODE into OUTER_LOOP. We already handled
11741 relinking EXEC_BLOCK above. */
11742 if (code->op == EXEC_DO && outer_loop)
11743 outer_loop->block->next = code;
11744
11745 return innermost_loop;
11746 }
11747
11748 /* CODE is an OMP loop construct. Return true if VAR matches an iteration
11749 variable outer to level DEPTH. */
11750 static bool
11751 is_outer_iteration_variable (gfc_code *code, int depth, gfc_symbol *var)
11752 {
11753 int i;
11754 gfc_code *do_code = code;
11755
11756 for (i = 1; i < depth; i++)
11757 {
11758 do_code = find_nested_loop_in_chain (do_code->block->next);
11759 gcc_assert (do_code);
11760 if (do_code->op == EXEC_OMP_TILE || do_code->op == EXEC_OMP_UNROLL)
11761 {
11762 --i;
11763 continue;
11764 }
11765 gfc_symbol *ivar = do_code->ext.iterator->var->symtree->n.sym;
11766 if (var == ivar)
11767 return true;
11768 }
11769 return false;
11770 }
11771
11772 /* Forward declaration for recursive functions. */
11773 static gfc_code *
11774 check_nested_loop_in_block (gfc_code *block, gfc_expr *expr, gfc_symbol *sym,
11775 bool *bad);
11776
11777 /* Like find_nested_loop_in_chain, but additionally check that EXPR
11778 does not reference any variables bound in intervening EXEC_BLOCKs
11779 and that SYM is not bound in such intervening blocks. Either EXPR or SYM
11780 may be null. Sets *BAD to true if either test fails. */
11781 static gfc_code *
11782 check_nested_loop_in_chain (gfc_code *chain, gfc_expr *expr, gfc_symbol *sym,
11783 bool *bad)
11784 {
11785 for (gfc_code *code = chain; code; code = code->next)
11786 {
11787 if (code->op == EXEC_DO)
11788 return code;
11789 else if (code->op == EXEC_OMP_TILE || code->op == EXEC_OMP_UNROLL)
11790 return check_nested_loop_in_chain (code->block->next, expr, sym, bad);
11791 else if (code->op == EXEC_BLOCK)
11792 {
11793 gfc_code *c = check_nested_loop_in_block (code, expr, sym, bad);
11794 if (c)
11795 return c;
11796 }
11797 }
11798 return NULL;
11799 }
11800
11801 /* Code walker for block symtrees. It doesn't take any kind of state
11802 argument, so use a static variable. */
11803 static struct check_nested_loop_in_block_state_t {
11804 gfc_expr *expr;
11805 gfc_symbol *sym;
11806 bool *bad;
11807 } check_nested_loop_in_block_state;
11808
11809 static void
11810 check_nested_loop_in_block_symbol (gfc_symbol *sym)
11811 {
11812 if (sym == check_nested_loop_in_block_state.sym
11813 || (check_nested_loop_in_block_state.expr
11814 && gfc_find_sym_in_expr (sym,
11815 check_nested_loop_in_block_state.expr)))
11816 *check_nested_loop_in_block_state.bad = true;
11817 }
11818
11819 /* Return the first nested DO loop in BLOCK, or NULL if there
11820 isn't one. Set *BAD to true if EXPR references any variables in BLOCK, or
11821 SYM is bound in BLOCK. Either EXPR or SYM may be null. */
11822 static gfc_code *
11823 check_nested_loop_in_block (gfc_code *block, gfc_expr *expr,
11824 gfc_symbol *sym, bool *bad)
11825 {
11826 gfc_namespace *ns;
11827 gcc_assert (block->op == EXEC_BLOCK);
11828 ns = block->ext.block.ns;
11829 gcc_assert (ns);
11830
11831 /* Skip the check if this block doesn't contain the nested loop, or
11832 if we already know it's bad. */
11833 gfc_code *result = check_nested_loop_in_chain (ns->code, expr, sym, bad);
11834 if (result && !*bad)
11835 {
11836 check_nested_loop_in_block_state.expr = expr;
11837 check_nested_loop_in_block_state.sym = sym;
11838 check_nested_loop_in_block_state.bad = bad;
11839 gfc_traverse_ns (ns, check_nested_loop_in_block_symbol);
11840 check_nested_loop_in_block_state.expr = NULL;
11841 check_nested_loop_in_block_state.sym = NULL;
11842 check_nested_loop_in_block_state.bad = NULL;
11843 }
11844 return result;
11845 }
11846
11847 /* CODE is an OMP loop construct. Return true if EXPR references
11848 any variables bound in intervening code, to level DEPTH. */
11849 static bool
11850 expr_uses_intervening_var (gfc_code *code, int depth, gfc_expr *expr)
11851 {
11852 int i;
11853 gfc_code *do_code = code;
11854
11855 for (i = 0; i < depth; i++)
11856 {
11857 bool bad = false;
11858 do_code = check_nested_loop_in_chain (do_code->block->next,
11859 expr, NULL, &bad);
11860 if (bad)
11861 return true;
11862 }
11863 return false;
11864 }
11865
11866 /* CODE is an OMP loop construct. Return true if SYM is bound in
11867 intervening code, to level DEPTH. */
11868 static bool
11869 is_intervening_var (gfc_code *code, int depth, gfc_symbol *sym)
11870 {
11871 int i;
11872 gfc_code *do_code = code;
11873
11874 for (i = 0; i < depth; i++)
11875 {
11876 bool bad = false;
11877 do_code = check_nested_loop_in_chain (do_code->block->next,
11878 NULL, sym, &bad);
11879 if (bad)
11880 return true;
11881 }
11882 return false;
11883 }
11884
11885 /* CODE is an OMP loop construct. Return true if EXPR does not reference
11886 any iteration variables outer to level DEPTH. */
11887 static bool
11888 expr_is_invariant (gfc_code *code, int depth, gfc_expr *expr)
11889 {
11890 int i;
11891 gfc_code *do_code = code;
11892
11893 for (i = 1; i < depth; i++)
11894 {
11895 do_code = find_nested_loop_in_chain (do_code->block->next);
11896 gcc_assert (do_code);
11897 if (do_code->op == EXEC_OMP_TILE || do_code->op == EXEC_OMP_UNROLL)
11898 {
11899 --i;
11900 continue;
11901 }
11902 gfc_symbol *ivar = do_code->ext.iterator->var->symtree->n.sym;
11903 if (gfc_find_sym_in_expr (ivar, expr))
11904 return false;
11905 }
11906 return true;
11907 }
11908
11909 /* CODE is an OMP loop construct. Return true if EXPR matches one of the
11910 canonical forms for a bound expression. It may include references to
11911 an iteration variable outer to level DEPTH; set OUTER_VARP if so. */
11912 static bool
11913 bound_expr_is_canonical (gfc_code *code, int depth, gfc_expr *expr,
11914 gfc_symbol **outer_varp)
11915 {
11916 gfc_expr *expr2 = NULL;
11917
11918 /* Rectangular case. */
11919 if (depth == 0 || expr_is_invariant (code, depth, expr))
11920 return true;
11921
11922 /* Any simple variable that didn't pass expr_is_invariant must be
11923 an outer_var. */
11924 if (expr->expr_type == EXPR_VARIABLE && expr->rank == 0)
11925 {
11926 *outer_varp = expr->symtree->n.sym;
11927 return true;
11928 }
11929
11930 /* All other permitted forms are binary operators. */
11931 if (expr->expr_type != EXPR_OP)
11932 return false;
11933
11934 /* Check for plus/minus a loop invariant expr. */
11935 if (expr->value.op.op == INTRINSIC_PLUS
11936 || expr->value.op.op == INTRINSIC_MINUS)
11937 {
11938 if (expr_is_invariant (code, depth, expr->value.op.op1))
11939 expr2 = expr->value.op.op2;
11940 else if (expr_is_invariant (code, depth, expr->value.op.op2))
11941 expr2 = expr->value.op.op1;
11942 else
11943 return false;
11944 }
11945 else
11946 expr2 = expr;
11947
11948 /* Check for a product with a loop-invariant expr. */
11949 if (expr2->expr_type == EXPR_OP
11950 && expr2->value.op.op == INTRINSIC_TIMES)
11951 {
11952 if (expr_is_invariant (code, depth, expr2->value.op.op1))
11953 expr2 = expr2->value.op.op2;
11954 else if (expr_is_invariant (code, depth, expr2->value.op.op2))
11955 expr2 = expr2->value.op.op1;
11956 else
11957 return false;
11958 }
11959
11960 /* What's left must be a reference to an outer loop variable. */
11961 if (expr2->expr_type == EXPR_VARIABLE
11962 && expr2->rank == 0
11963 && is_outer_iteration_variable (code, depth, expr2->symtree->n.sym))
11964 {
11965 *outer_varp = expr2->symtree->n.sym;
11966 return true;
11967 }
11968
11969 return false;
11970 }
11971
11972 static void
11973 resolve_omp_do (gfc_code *code)
11974 {
11975 gfc_code *do_code, *next;
11976 int list, i, count, non_generated_count;
11977 gfc_omp_namelist *n;
11978 gfc_symbol *dovar;
11979 const char *name;
11980 bool is_simd = false;
11981 bool errorp = false;
11982 bool perfect_nesting_errorp = false;
11983 bool imperfect = false;
11984
11985 switch (code->op)
11986 {
11987 case EXEC_OMP_DISTRIBUTE: name = "!$OMP DISTRIBUTE"; break;
11988 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
11989 name = "!$OMP DISTRIBUTE PARALLEL DO";
11990 break;
11991 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
11992 name = "!$OMP DISTRIBUTE PARALLEL DO SIMD";
11993 is_simd = true;
11994 break;
11995 case EXEC_OMP_DISTRIBUTE_SIMD:
11996 name = "!$OMP DISTRIBUTE SIMD";
11997 is_simd = true;
11998 break;
11999 case EXEC_OMP_DO: name = "!$OMP DO"; break;
12000 case EXEC_OMP_DO_SIMD: name = "!$OMP DO SIMD"; is_simd = true; break;
12001 case EXEC_OMP_LOOP: name = "!$OMP LOOP"; break;
12002 case EXEC_OMP_PARALLEL_DO: name = "!$OMP PARALLEL DO"; break;
12003 case EXEC_OMP_PARALLEL_DO_SIMD:
12004 name = "!$OMP PARALLEL DO SIMD";
12005 is_simd = true;
12006 break;
12007 case EXEC_OMP_PARALLEL_LOOP: name = "!$OMP PARALLEL LOOP"; break;
12008 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
12009 name = "!$OMP PARALLEL MASKED TASKLOOP";
12010 break;
12011 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
12012 name = "!$OMP PARALLEL MASKED TASKLOOP SIMD";
12013 is_simd = true;
12014 break;
12015 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
12016 name = "!$OMP PARALLEL MASTER TASKLOOP";
12017 break;
12018 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
12019 name = "!$OMP PARALLEL MASTER TASKLOOP SIMD";
12020 is_simd = true;
12021 break;
12022 case EXEC_OMP_MASKED_TASKLOOP: name = "!$OMP MASKED TASKLOOP"; break;
12023 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
12024 name = "!$OMP MASKED TASKLOOP SIMD";
12025 is_simd = true;
12026 break;
12027 case EXEC_OMP_MASTER_TASKLOOP: name = "!$OMP MASTER TASKLOOP"; break;
12028 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
12029 name = "!$OMP MASTER TASKLOOP SIMD";
12030 is_simd = true;
12031 break;
12032 case EXEC_OMP_SIMD: name = "!$OMP SIMD"; is_simd = true; break;
12033 case EXEC_OMP_TARGET_PARALLEL_DO: name = "!$OMP TARGET PARALLEL DO"; break;
12034 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12035 name = "!$OMP TARGET PARALLEL DO SIMD";
12036 is_simd = true;
12037 break;
12038 case EXEC_OMP_TARGET_PARALLEL_LOOP:
12039 name = "!$OMP TARGET PARALLEL LOOP";
12040 break;
12041 case EXEC_OMP_TARGET_SIMD:
12042 name = "!$OMP TARGET SIMD";
12043 is_simd = true;
12044 break;
12045 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12046 name = "!$OMP TARGET TEAMS DISTRIBUTE";
12047 break;
12048 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12049 name = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO";
12050 break;
12051 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12052 name = "!$OMP TARGET TEAMS DISTRIBUTE PARALLEL DO SIMD";
12053 is_simd = true;
12054 break;
12055 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12056 name = "!$OMP TARGET TEAMS DISTRIBUTE SIMD";
12057 is_simd = true;
12058 break;
12059 case EXEC_OMP_TARGET_TEAMS_LOOP: name = "!$OMP TARGET TEAMS LOOP"; break;
12060 case EXEC_OMP_TASKLOOP: name = "!$OMP TASKLOOP"; break;
12061 case EXEC_OMP_TASKLOOP_SIMD:
12062 name = "!$OMP TASKLOOP SIMD";
12063 is_simd = true;
12064 break;
12065 case EXEC_OMP_TEAMS_DISTRIBUTE: name = "!$OMP TEAMS DISTRIBUTE"; break;
12066 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12067 name = "!$OMP TEAMS DISTRIBUTE PARALLEL DO";
12068 break;
12069 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12070 name = "!$OMP TEAMS DISTRIBUTE PARALLEL DO SIMD";
12071 is_simd = true;
12072 break;
12073 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12074 name = "!$OMP TEAMS DISTRIBUTE SIMD";
12075 is_simd = true;
12076 break;
12077 case EXEC_OMP_TEAMS_LOOP: name = "!$OMP TEAMS LOOP"; break;
12078 case EXEC_OMP_TILE: name = "!$OMP TILE"; break;
12079 case EXEC_OMP_UNROLL: name = "!$OMP UNROLL"; break;
12080 default: gcc_unreachable ();
12081 }
12082
12083 if (code->ext.omp_clauses)
12084 resolve_omp_clauses (code, code->ext.omp_clauses, NULL);
12085
12086 if (code->op == EXEC_OMP_TILE && code->ext.omp_clauses->sizes_list == NULL)
12087 gfc_error ("SIZES clause is required on !$OMP TILE construct at %L",
12088 &code->loc);
12089
12090 do_code = code->block->next;
12091 if (code->ext.omp_clauses->orderedc)
12092 count = code->ext.omp_clauses->orderedc;
12093 else if (code->ext.omp_clauses->sizes_list)
12094 count = gfc_expr_list_len (code->ext.omp_clauses->sizes_list);
12095 else
12096 {
12097 count = code->ext.omp_clauses->collapse;
12098 if (count <= 0)
12099 count = 1;
12100 }
12101
12102 non_generated_count = count;
12103 /* While the spec defines the loop nest depth independently of the COLLAPSE
12104 clause, in practice the middle end only pays attention to the COLLAPSE
12105 depth and treats any further inner loops as the final-loop-body. So
12106 here we also check canonical loop nest form only for the number of
12107 outer loops specified by the COLLAPSE clause too. */
12108 for (i = 1; i <= count; i++)
12109 {
12110 gfc_symbol *start_var = NULL, *end_var = NULL;
12111 /* Parse errors are not recoverable. */
12112 if (do_code->op == EXEC_DO_WHILE)
12113 {
12114 gfc_error ("%s cannot be a DO WHILE or DO without loop control "
12115 "at %L", name, &do_code->loc);
12116 goto fail;
12117 }
12118 if (do_code->op == EXEC_DO_CONCURRENT)
12119 {
12120 gfc_error ("%s cannot be a DO CONCURRENT loop at %L", name,
12121 &do_code->loc);
12122 goto fail;
12123 }
12124 if (do_code->op == EXEC_OMP_TILE || do_code->op == EXEC_OMP_UNROLL)
12125 {
12126 if (do_code->op == EXEC_OMP_UNROLL)
12127 {
12128 if (!do_code->ext.omp_clauses->partial)
12129 {
12130 gfc_error ("Generated loop of UNROLL construct at %L "
12131 "without PARTIAL clause does not have "
12132 "canonical form", &do_code->loc);
12133 goto fail;
12134 }
12135 else if (i != count)
12136 {
12137 gfc_error ("UNROLL construct at %L with PARTIAL clause "
12138 "generates just one loop with canonical form "
12139 "but %d loops are needed",
12140 &do_code->loc, count - i + 1);
12141 goto fail;
12142 }
12143 }
12144 else if (do_code->op == EXEC_OMP_TILE)
12145 {
12146 if (do_code->ext.omp_clauses->sizes_list == NULL)
12147 /* This should have been diagnosed earlier already. */
12148 return;
12149 int l = gfc_expr_list_len (do_code->ext.omp_clauses->sizes_list);
12150 if (count - i + 1 > l)
12151 {
12152 gfc_error ("TILE construct at %L generates %d loops "
12153 "with canonical form but %d loops are needed",
12154 &do_code->loc, l, count - i + 1);
12155 goto fail;
12156 }
12157 }
12158 if (do_code->ext.omp_clauses && do_code->ext.omp_clauses->erroneous)
12159 goto fail;
12160 if (imperfect && !perfect_nesting_errorp)
12161 {
12162 sorry_at (gfc_get_location (&do_code->loc),
12163 "Imperfectly nested loop using generated loops");
12164 errorp = true;
12165 }
12166 if (non_generated_count == count)
12167 non_generated_count = i - 1;
12168 --i;
12169 do_code = do_code->block->next;
12170 continue;
12171 }
12172 gcc_assert (do_code->op == EXEC_DO);
12173 if (do_code->ext.iterator->var->ts.type != BT_INTEGER)
12174 {
12175 gfc_error ("%s iteration variable must be of type integer at %L",
12176 name, &do_code->loc);
12177 errorp = true;
12178 }
12179 dovar = do_code->ext.iterator->var->symtree->n.sym;
12180 if (dovar->attr.threadprivate)
12181 {
12182 gfc_error ("%s iteration variable must not be THREADPRIVATE "
12183 "at %L", name, &do_code->loc);
12184 errorp = true;
12185 }
12186 if (code->ext.omp_clauses)
12187 for (list = 0; list < OMP_LIST_NUM; list++)
12188 if (!is_simd || code->ext.omp_clauses->collapse > 1
12189 ? (list != OMP_LIST_PRIVATE && list != OMP_LIST_LASTPRIVATE
12190 && list != OMP_LIST_ALLOCATE)
12191 : (list != OMP_LIST_PRIVATE && list != OMP_LIST_LASTPRIVATE
12192 && list != OMP_LIST_ALLOCATE && list != OMP_LIST_LINEAR))
12193 for (n = code->ext.omp_clauses->lists[list]; n; n = n->next)
12194 if (dovar == n->sym)
12195 {
12196 if (!is_simd || code->ext.omp_clauses->collapse > 1)
12197 gfc_error ("%s iteration variable present on clause "
12198 "other than PRIVATE, LASTPRIVATE or "
12199 "ALLOCATE at %L", name, &do_code->loc);
12200 else
12201 gfc_error ("%s iteration variable present on clause "
12202 "other than PRIVATE, LASTPRIVATE, ALLOCATE or "
12203 "LINEAR at %L", name, &do_code->loc);
12204 errorp = true;
12205 }
12206 if (is_outer_iteration_variable (code, i, dovar))
12207 {
12208 gfc_error ("%s iteration variable used in more than one loop at %L",
12209 name, &do_code->loc);
12210 errorp = true;
12211 }
12212 else if (is_intervening_var (code, i, dovar))
12213 {
12214 gfc_error ("%s iteration variable at %L is bound in "
12215 "intervening code",
12216 name, &do_code->loc);
12217 errorp = true;
12218 }
12219 else if (!bound_expr_is_canonical (code, i,
12220 do_code->ext.iterator->start,
12221 &start_var))
12222 {
12223 gfc_error ("%s loop start expression not in canonical form at %L",
12224 name, &do_code->loc);
12225 errorp = true;
12226 }
12227 else if (expr_uses_intervening_var (code, i,
12228 do_code->ext.iterator->start))
12229 {
12230 gfc_error ("%s loop start expression at %L uses variable bound in "
12231 "intervening code",
12232 name, &do_code->loc);
12233 errorp = true;
12234 }
12235 else if (!bound_expr_is_canonical (code, i,
12236 do_code->ext.iterator->end,
12237 &end_var))
12238 {
12239 gfc_error ("%s loop end expression not in canonical form at %L",
12240 name, &do_code->loc);
12241 errorp = true;
12242 }
12243 else if (expr_uses_intervening_var (code, i,
12244 do_code->ext.iterator->end))
12245 {
12246 gfc_error ("%s loop end expression at %L uses variable bound in "
12247 "intervening code",
12248 name, &do_code->loc);
12249 errorp = true;
12250 }
12251 else if (start_var && end_var && start_var != end_var)
12252 {
12253 gfc_error ("%s loop bounds reference different "
12254 "iteration variables at %L", name, &do_code->loc);
12255 errorp = true;
12256 }
12257 else if (!expr_is_invariant (code, i, do_code->ext.iterator->step))
12258 {
12259 gfc_error ("%s loop increment not in canonical form at %L",
12260 name, &do_code->loc);
12261 errorp = true;
12262 }
12263 else if (expr_uses_intervening_var (code, i,
12264 do_code->ext.iterator->step))
12265 {
12266 gfc_error ("%s loop increment expression at %L uses variable "
12267 "bound in intervening code",
12268 name, &do_code->loc);
12269 errorp = true;
12270 }
12271 if (start_var || end_var)
12272 {
12273 code->ext.omp_clauses->non_rectangular = 1;
12274 if (i > non_generated_count)
12275 {
12276 sorry_at (gfc_get_location (&do_code->loc),
12277 "Non-rectangular loops from generated loops "
12278 "unsupported");
12279 errorp = true;
12280 }
12281 }
12282
12283 /* Only parse loop body into nested loop and intervening code if
12284 there are supposed to be more loops in the nest to collapse. */
12285 if (i == count)
12286 break;
12287
12288 next = find_nested_loop_in_chain (do_code->block->next);
12289
12290 if (!next)
12291 {
12292 /* Parse error, can't recover from this. */
12293 gfc_error ("not enough DO loops for collapsed %s (level %d) at %L",
12294 name, i, &code->loc);
12295 goto fail;
12296 }
12297 else if (next != do_code->block->next || next->next)
12298 /* Imperfectly nested loop found. */
12299 {
12300 /* Only diagnose violation of imperfect nesting constraints once. */
12301 if (!perfect_nesting_errorp)
12302 {
12303 if (code->ext.omp_clauses->orderedc)
12304 {
12305 gfc_error ("%s inner loops must be perfectly nested with "
12306 "ORDERED clause at %L",
12307 name, &code->loc);
12308 perfect_nesting_errorp = true;
12309 }
12310 else if (code->ext.omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN])
12311 {
12312 gfc_error ("%s inner loops must be perfectly nested with "
12313 "REDUCTION INSCAN clause at %L",
12314 name, &code->loc);
12315 perfect_nesting_errorp = true;
12316 }
12317 else if (code->op == EXEC_OMP_TILE)
12318 {
12319 gfc_error ("%s inner loops must be perfectly nested at %L",
12320 name, &code->loc);
12321 perfect_nesting_errorp = true;
12322 }
12323 if (perfect_nesting_errorp)
12324 errorp = true;
12325 }
12326 if (diagnose_intervening_code_errors (do_code->block->next,
12327 name, next))
12328 errorp = true;
12329 imperfect = true;
12330 }
12331 do_code = next;
12332 }
12333
12334 /* Give up now if we found any constraint violations. */
12335 if (errorp)
12336 {
12337 fail:
12338 if (code->ext.omp_clauses)
12339 code->ext.omp_clauses->erroneous = 1;
12340 return;
12341 }
12342
12343 if (non_generated_count)
12344 restructure_intervening_code (&code->block->next, code,
12345 non_generated_count);
12346 }
12347
12348 static void
12349 resolve_omp_metadirective (gfc_code *code, gfc_namespace *ns)
12350 {
12351 gfc_omp_variant *variant = code->ext.omp_variants;
12352
12353 while (variant)
12354 {
12355 gfc_code *variant_code = variant->code;
12356 gfc_resolve_code (variant_code, ns);
12357 variant = variant->next;
12358 }
12359 }
12360
12361
12362 static gfc_statement
12363 omp_code_to_statement (gfc_code *code)
12364 {
12365 switch (code->op)
12366 {
12367 case EXEC_OMP_PARALLEL:
12368 return ST_OMP_PARALLEL;
12369 case EXEC_OMP_PARALLEL_MASKED:
12370 return ST_OMP_PARALLEL_MASKED;
12371 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
12372 return ST_OMP_PARALLEL_MASKED_TASKLOOP;
12373 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
12374 return ST_OMP_PARALLEL_MASKED_TASKLOOP_SIMD;
12375 case EXEC_OMP_PARALLEL_MASTER:
12376 return ST_OMP_PARALLEL_MASTER;
12377 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
12378 return ST_OMP_PARALLEL_MASTER_TASKLOOP;
12379 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
12380 return ST_OMP_PARALLEL_MASTER_TASKLOOP_SIMD;
12381 case EXEC_OMP_PARALLEL_SECTIONS:
12382 return ST_OMP_PARALLEL_SECTIONS;
12383 case EXEC_OMP_SECTIONS:
12384 return ST_OMP_SECTIONS;
12385 case EXEC_OMP_ORDERED:
12386 return ST_OMP_ORDERED;
12387 case EXEC_OMP_CRITICAL:
12388 return ST_OMP_CRITICAL;
12389 case EXEC_OMP_MASKED:
12390 return ST_OMP_MASKED;
12391 case EXEC_OMP_MASKED_TASKLOOP:
12392 return ST_OMP_MASKED_TASKLOOP;
12393 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
12394 return ST_OMP_MASKED_TASKLOOP_SIMD;
12395 case EXEC_OMP_MASTER:
12396 return ST_OMP_MASTER;
12397 case EXEC_OMP_MASTER_TASKLOOP:
12398 return ST_OMP_MASTER_TASKLOOP;
12399 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
12400 return ST_OMP_MASTER_TASKLOOP_SIMD;
12401 case EXEC_OMP_SINGLE:
12402 return ST_OMP_SINGLE;
12403 case EXEC_OMP_TASK:
12404 return ST_OMP_TASK;
12405 case EXEC_OMP_WORKSHARE:
12406 return ST_OMP_WORKSHARE;
12407 case EXEC_OMP_PARALLEL_WORKSHARE:
12408 return ST_OMP_PARALLEL_WORKSHARE;
12409 case EXEC_OMP_DO:
12410 return ST_OMP_DO;
12411 case EXEC_OMP_LOOP:
12412 return ST_OMP_LOOP;
12413 case EXEC_OMP_ALLOCATE:
12414 return ST_OMP_ALLOCATE_EXEC;
12415 case EXEC_OMP_ALLOCATORS:
12416 return ST_OMP_ALLOCATORS;
12417 case EXEC_OMP_ASSUME:
12418 return ST_OMP_ASSUME;
12419 case EXEC_OMP_ATOMIC:
12420 return ST_OMP_ATOMIC;
12421 case EXEC_OMP_BARRIER:
12422 return ST_OMP_BARRIER;
12423 case EXEC_OMP_CANCEL:
12424 return ST_OMP_CANCEL;
12425 case EXEC_OMP_CANCELLATION_POINT:
12426 return ST_OMP_CANCELLATION_POINT;
12427 case EXEC_OMP_ERROR:
12428 return ST_OMP_ERROR;
12429 case EXEC_OMP_FLUSH:
12430 return ST_OMP_FLUSH;
12431 case EXEC_OMP_INTEROP:
12432 return ST_OMP_INTEROP;
12433 case EXEC_OMP_DISTRIBUTE:
12434 return ST_OMP_DISTRIBUTE;
12435 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12436 return ST_OMP_DISTRIBUTE_PARALLEL_DO;
12437 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12438 return ST_OMP_DISTRIBUTE_PARALLEL_DO_SIMD;
12439 case EXEC_OMP_DISTRIBUTE_SIMD:
12440 return ST_OMP_DISTRIBUTE_SIMD;
12441 case EXEC_OMP_DO_SIMD:
12442 return ST_OMP_DO_SIMD;
12443 case EXEC_OMP_SCAN:
12444 return ST_OMP_SCAN;
12445 case EXEC_OMP_SCOPE:
12446 return ST_OMP_SCOPE;
12447 case EXEC_OMP_SIMD:
12448 return ST_OMP_SIMD;
12449 case EXEC_OMP_TARGET:
12450 return ST_OMP_TARGET;
12451 case EXEC_OMP_TARGET_DATA:
12452 return ST_OMP_TARGET_DATA;
12453 case EXEC_OMP_TARGET_ENTER_DATA:
12454 return ST_OMP_TARGET_ENTER_DATA;
12455 case EXEC_OMP_TARGET_EXIT_DATA:
12456 return ST_OMP_TARGET_EXIT_DATA;
12457 case EXEC_OMP_TARGET_PARALLEL:
12458 return ST_OMP_TARGET_PARALLEL;
12459 case EXEC_OMP_TARGET_PARALLEL_DO:
12460 return ST_OMP_TARGET_PARALLEL_DO;
12461 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12462 return ST_OMP_TARGET_PARALLEL_DO_SIMD;
12463 case EXEC_OMP_TARGET_PARALLEL_LOOP:
12464 return ST_OMP_TARGET_PARALLEL_LOOP;
12465 case EXEC_OMP_TARGET_SIMD:
12466 return ST_OMP_TARGET_SIMD;
12467 case EXEC_OMP_TARGET_TEAMS:
12468 return ST_OMP_TARGET_TEAMS;
12469 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12470 return ST_OMP_TARGET_TEAMS_DISTRIBUTE;
12471 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12472 return ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO;
12473 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12474 return ST_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
12475 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12476 return ST_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD;
12477 case EXEC_OMP_TARGET_TEAMS_LOOP:
12478 return ST_OMP_TARGET_TEAMS_LOOP;
12479 case EXEC_OMP_TARGET_UPDATE:
12480 return ST_OMP_TARGET_UPDATE;
12481 case EXEC_OMP_TASKGROUP:
12482 return ST_OMP_TASKGROUP;
12483 case EXEC_OMP_TASKLOOP:
12484 return ST_OMP_TASKLOOP;
12485 case EXEC_OMP_TASKLOOP_SIMD:
12486 return ST_OMP_TASKLOOP_SIMD;
12487 case EXEC_OMP_TASKWAIT:
12488 return ST_OMP_TASKWAIT;
12489 case EXEC_OMP_TASKYIELD:
12490 return ST_OMP_TASKYIELD;
12491 case EXEC_OMP_TEAMS:
12492 return ST_OMP_TEAMS;
12493 case EXEC_OMP_TEAMS_DISTRIBUTE:
12494 return ST_OMP_TEAMS_DISTRIBUTE;
12495 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12496 return ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO;
12497 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12498 return ST_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD;
12499 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12500 return ST_OMP_TEAMS_DISTRIBUTE_SIMD;
12501 case EXEC_OMP_TEAMS_LOOP:
12502 return ST_OMP_TEAMS_LOOP;
12503 case EXEC_OMP_PARALLEL_DO:
12504 return ST_OMP_PARALLEL_DO;
12505 case EXEC_OMP_PARALLEL_DO_SIMD:
12506 return ST_OMP_PARALLEL_DO_SIMD;
12507 case EXEC_OMP_PARALLEL_LOOP:
12508 return ST_OMP_PARALLEL_LOOP;
12509 case EXEC_OMP_DEPOBJ:
12510 return ST_OMP_DEPOBJ;
12511 case EXEC_OMP_TILE:
12512 return ST_OMP_TILE;
12513 case EXEC_OMP_UNROLL:
12514 return ST_OMP_UNROLL;
12515 case EXEC_OMP_DISPATCH:
12516 return ST_OMP_DISPATCH;
12517 default:
12518 gcc_unreachable ();
12519 }
12520 }
12521
12522 static gfc_statement
12523 oacc_code_to_statement (gfc_code *code)
12524 {
12525 switch (code->op)
12526 {
12527 case EXEC_OACC_PARALLEL:
12528 return ST_OACC_PARALLEL;
12529 case EXEC_OACC_KERNELS:
12530 return ST_OACC_KERNELS;
12531 case EXEC_OACC_SERIAL:
12532 return ST_OACC_SERIAL;
12533 case EXEC_OACC_DATA:
12534 return ST_OACC_DATA;
12535 case EXEC_OACC_HOST_DATA:
12536 return ST_OACC_HOST_DATA;
12537 case EXEC_OACC_PARALLEL_LOOP:
12538 return ST_OACC_PARALLEL_LOOP;
12539 case EXEC_OACC_KERNELS_LOOP:
12540 return ST_OACC_KERNELS_LOOP;
12541 case EXEC_OACC_SERIAL_LOOP:
12542 return ST_OACC_SERIAL_LOOP;
12543 case EXEC_OACC_LOOP:
12544 return ST_OACC_LOOP;
12545 case EXEC_OACC_ATOMIC:
12546 return ST_OACC_ATOMIC;
12547 case EXEC_OACC_ROUTINE:
12548 return ST_OACC_ROUTINE;
12549 case EXEC_OACC_UPDATE:
12550 return ST_OACC_UPDATE;
12551 case EXEC_OACC_WAIT:
12552 return ST_OACC_WAIT;
12553 case EXEC_OACC_CACHE:
12554 return ST_OACC_CACHE;
12555 case EXEC_OACC_ENTER_DATA:
12556 return ST_OACC_ENTER_DATA;
12557 case EXEC_OACC_EXIT_DATA:
12558 return ST_OACC_EXIT_DATA;
12559 case EXEC_OACC_DECLARE:
12560 return ST_OACC_DECLARE;
12561 default:
12562 gcc_unreachable ();
12563 }
12564 }
12565
12566 static void
12567 resolve_oacc_directive_inside_omp_region (gfc_code *code)
12568 {
12569 if (omp_current_ctx != NULL && omp_current_ctx->is_openmp)
12570 {
12571 gfc_statement st = omp_code_to_statement (omp_current_ctx->code);
12572 gfc_statement oacc_st = oacc_code_to_statement (code);
12573 gfc_error ("The %s directive cannot be specified within "
12574 "a %s region at %L", gfc_ascii_statement (oacc_st),
12575 gfc_ascii_statement (st), &code->loc);
12576 }
12577 }
12578
12579 static void
12580 resolve_omp_directive_inside_oacc_region (gfc_code *code)
12581 {
12582 if (omp_current_ctx != NULL && !omp_current_ctx->is_openmp)
12583 {
12584 gfc_statement st = oacc_code_to_statement (omp_current_ctx->code);
12585 gfc_statement omp_st = omp_code_to_statement (code);
12586 gfc_error ("The %s directive cannot be specified within "
12587 "a %s region at %L", gfc_ascii_statement (omp_st),
12588 gfc_ascii_statement (st), &code->loc);
12589 }
12590 }
12591
12592
12593 static void
12594 resolve_oacc_nested_loops (gfc_code *code, gfc_code* do_code, int collapse,
12595 const char *clause)
12596 {
12597 gfc_symbol *dovar;
12598 gfc_code *c;
12599 int i;
12600
12601 for (i = 1; i <= collapse; i++)
12602 {
12603 if (do_code->op == EXEC_DO_WHILE)
12604 {
12605 gfc_error ("!$ACC LOOP cannot be a DO WHILE or DO without loop control "
12606 "at %L", &do_code->loc);
12607 break;
12608 }
12609 if (do_code->op == EXEC_DO_CONCURRENT)
12610 {
12611 gfc_error ("!$ACC LOOP cannot be a DO CONCURRENT loop at %L",
12612 &do_code->loc);
12613 break;
12614 }
12615 gcc_assert (do_code->op == EXEC_DO);
12616 if (do_code->ext.iterator->var->ts.type != BT_INTEGER)
12617 gfc_error ("!$ACC LOOP iteration variable must be of type integer at %L",
12618 &do_code->loc);
12619 dovar = do_code->ext.iterator->var->symtree->n.sym;
12620 if (i > 1)
12621 {
12622 gfc_code *do_code2 = code->block->next;
12623 int j;
12624
12625 for (j = 1; j < i; j++)
12626 {
12627 gfc_symbol *ivar = do_code2->ext.iterator->var->symtree->n.sym;
12628 if (dovar == ivar
12629 || gfc_find_sym_in_expr (ivar, do_code->ext.iterator->start)
12630 || gfc_find_sym_in_expr (ivar, do_code->ext.iterator->end)
12631 || gfc_find_sym_in_expr (ivar, do_code->ext.iterator->step))
12632 {
12633 gfc_error ("!$ACC LOOP %s loops don't form rectangular "
12634 "iteration space at %L", clause, &do_code->loc);
12635 break;
12636 }
12637 do_code2 = do_code2->block->next;
12638 }
12639 }
12640 if (i == collapse)
12641 break;
12642 for (c = do_code->next; c; c = c->next)
12643 if (c->op != EXEC_NOP && c->op != EXEC_CONTINUE)
12644 {
12645 gfc_error ("%s !$ACC LOOP loops not perfectly nested at %L",
12646 clause, &c->loc);
12647 break;
12648 }
12649 if (c)
12650 break;
12651 do_code = do_code->block;
12652 if (do_code->op != EXEC_DO && do_code->op != EXEC_DO_WHILE
12653 && do_code->op != EXEC_DO_CONCURRENT)
12654 {
12655 gfc_error ("not enough DO loops for %s !$ACC LOOP at %L",
12656 clause, &code->loc);
12657 break;
12658 }
12659 do_code = do_code->next;
12660 if (do_code == NULL
12661 || (do_code->op != EXEC_DO && do_code->op != EXEC_DO_WHILE
12662 && do_code->op != EXEC_DO_CONCURRENT))
12663 {
12664 gfc_error ("not enough DO loops for %s !$ACC LOOP at %L",
12665 clause, &code->loc);
12666 break;
12667 }
12668 }
12669 }
12670
12671
12672 static void
12673 resolve_oacc_loop_blocks (gfc_code *code)
12674 {
12675 if (!oacc_is_loop (code))
12676 return;
12677
12678 if (code->ext.omp_clauses->tile_list && code->ext.omp_clauses->gang
12679 && code->ext.omp_clauses->worker && code->ext.omp_clauses->vector)
12680 gfc_error ("Tiled loop cannot be parallelized across gangs, workers and "
12681 "vectors at the same time at %L", &code->loc);
12682
12683 if (code->ext.omp_clauses->tile_list)
12684 {
12685 gfc_expr_list *el;
12686 for (el = code->ext.omp_clauses->tile_list; el; el = el->next)
12687 {
12688 if (el->expr == NULL)
12689 {
12690 /* NULL expressions are used to represent '*' arguments.
12691 Convert those to a 0 expressions. */
12692 el->expr = gfc_get_constant_expr (BT_INTEGER,
12693 gfc_default_integer_kind,
12694 &code->loc);
12695 mpz_set_si (el->expr->value.integer, 0);
12696 }
12697 else
12698 {
12699 resolve_positive_int_expr (el->expr, "TILE");
12700 if (el->expr->expr_type != EXPR_CONSTANT)
12701 gfc_error ("TILE requires constant expression at %L",
12702 &code->loc);
12703 }
12704 }
12705 }
12706 }
12707
12708
12709 void
12710 gfc_resolve_oacc_blocks (gfc_code *code, gfc_namespace *ns)
12711 {
12712 fortran_omp_context ctx;
12713 gfc_omp_clauses *omp_clauses = code->ext.omp_clauses;
12714 gfc_omp_namelist *n;
12715 int list;
12716
12717 resolve_oacc_loop_blocks (code);
12718
12719 ctx.code = code;
12720 ctx.sharing_clauses = new hash_set<gfc_symbol *>;
12721 ctx.private_iterators = new hash_set<gfc_symbol *>;
12722 ctx.previous = omp_current_ctx;
12723 ctx.is_openmp = false;
12724 omp_current_ctx = &ctx;
12725
12726 for (list = 0; list < OMP_LIST_NUM; list++)
12727 switch (list)
12728 {
12729 case OMP_LIST_PRIVATE:
12730 for (n = omp_clauses->lists[list]; n; n = n->next)
12731 ctx.sharing_clauses->add (n->sym);
12732 break;
12733 default:
12734 break;
12735 }
12736
12737 gfc_resolve_blocks (code->block, ns);
12738
12739 omp_current_ctx = ctx.previous;
12740 delete ctx.sharing_clauses;
12741 delete ctx.private_iterators;
12742 }
12743
12744
12745 static void
12746 resolve_oacc_loop (gfc_code *code)
12747 {
12748 gfc_code *do_code;
12749 int collapse;
12750
12751 if (code->ext.omp_clauses)
12752 resolve_omp_clauses (code, code->ext.omp_clauses, NULL, true);
12753
12754 do_code = code->block->next;
12755 collapse = code->ext.omp_clauses->collapse;
12756
12757 /* Both collapsed and tiled loops are lowered the same way, but are not
12758 compatible. In gfc_trans_omp_do, the tile is prioritized. */
12759 if (code->ext.omp_clauses->tile_list)
12760 {
12761 int num = 0;
12762 gfc_expr_list *el;
12763 for (el = code->ext.omp_clauses->tile_list; el; el = el->next)
12764 ++num;
12765 resolve_oacc_nested_loops (code, code->block->next, num, "tiled");
12766 return;
12767 }
12768
12769 if (collapse <= 0)
12770 collapse = 1;
12771 resolve_oacc_nested_loops (code, do_code, collapse, "collapsed");
12772 }
12773
12774 void
12775 gfc_resolve_oacc_declare (gfc_namespace *ns)
12776 {
12777 int list;
12778 gfc_omp_namelist *n;
12779 gfc_oacc_declare *oc;
12780
12781 if (ns->oacc_declare == NULL)
12782 return;
12783
12784 for (oc = ns->oacc_declare; oc; oc = oc->next)
12785 {
12786 for (list = 0; list < OMP_LIST_NUM; list++)
12787 for (n = oc->clauses->lists[list]; n; n = n->next)
12788 {
12789 n->sym->mark = 0;
12790 if (n->sym->attr.flavor != FL_VARIABLE
12791 && (n->sym->attr.flavor != FL_PROCEDURE
12792 || n->sym->result != n->sym))
12793 {
12794 gfc_error ("Object %qs is not a variable at %L",
12795 n->sym->name, &oc->loc);
12796 continue;
12797 }
12798
12799 if (n->expr && n->expr->ref->type == REF_ARRAY)
12800 {
12801 gfc_error ("Array sections: %qs not allowed in"
12802 " !$ACC DECLARE at %L", n->sym->name, &oc->loc);
12803 continue;
12804 }
12805 }
12806
12807 for (n = oc->clauses->lists[OMP_LIST_DEVICE_RESIDENT]; n; n = n->next)
12808 check_array_not_assumed (n->sym, oc->loc, "DEVICE_RESIDENT");
12809 }
12810
12811 for (oc = ns->oacc_declare; oc; oc = oc->next)
12812 {
12813 for (list = 0; list < OMP_LIST_NUM; list++)
12814 for (n = oc->clauses->lists[list]; n; n = n->next)
12815 {
12816 if (n->sym->mark)
12817 {
12818 gfc_error ("Symbol %qs present on multiple clauses at %L",
12819 n->sym->name, &oc->loc);
12820 continue;
12821 }
12822 else
12823 n->sym->mark = 1;
12824 }
12825 }
12826
12827 for (oc = ns->oacc_declare; oc; oc = oc->next)
12828 {
12829 for (list = 0; list < OMP_LIST_NUM; list++)
12830 for (n = oc->clauses->lists[list]; n; n = n->next)
12831 n->sym->mark = 0;
12832 }
12833 }
12834
12835
12836 void
12837 gfc_resolve_oacc_routines (gfc_namespace *ns)
12838 {
12839 for (gfc_oacc_routine_name *orn = ns->oacc_routine_names;
12840 orn;
12841 orn = orn->next)
12842 {
12843 gfc_symbol *sym = orn->sym;
12844 if (!sym->attr.external
12845 && !sym->attr.function
12846 && !sym->attr.subroutine)
12847 {
12848 gfc_error ("NAME %qs does not refer to a subroutine or function"
12849 " in !$ACC ROUTINE ( NAME ) at %L", sym->name, &orn->loc);
12850 continue;
12851 }
12852 if (!gfc_add_omp_declare_target (&sym->attr, sym->name, &orn->loc))
12853 {
12854 gfc_error ("NAME %qs invalid"
12855 " in !$ACC ROUTINE ( NAME ) at %L", sym->name, &orn->loc);
12856 continue;
12857 }
12858 }
12859 }
12860
12861
12862 void
12863 gfc_resolve_oacc_directive (gfc_code *code, gfc_namespace *ns ATTRIBUTE_UNUSED)
12864 {
12865 resolve_oacc_directive_inside_omp_region (code);
12866
12867 switch (code->op)
12868 {
12869 case EXEC_OACC_PARALLEL:
12870 case EXEC_OACC_KERNELS:
12871 case EXEC_OACC_SERIAL:
12872 case EXEC_OACC_DATA:
12873 case EXEC_OACC_HOST_DATA:
12874 case EXEC_OACC_UPDATE:
12875 case EXEC_OACC_ENTER_DATA:
12876 case EXEC_OACC_EXIT_DATA:
12877 case EXEC_OACC_WAIT:
12878 case EXEC_OACC_CACHE:
12879 resolve_omp_clauses (code, code->ext.omp_clauses, NULL, true);
12880 break;
12881 case EXEC_OACC_PARALLEL_LOOP:
12882 case EXEC_OACC_KERNELS_LOOP:
12883 case EXEC_OACC_SERIAL_LOOP:
12884 case EXEC_OACC_LOOP:
12885 resolve_oacc_loop (code);
12886 break;
12887 case EXEC_OACC_ATOMIC:
12888 resolve_omp_atomic (code);
12889 break;
12890 default:
12891 break;
12892 }
12893 }
12894
12895
12896 static void
12897 resolve_omp_target (gfc_code *code)
12898 {
12899 #define GFC_IS_TEAMS_CONSTRUCT(op) \
12900 (op == EXEC_OMP_TEAMS \
12901 || op == EXEC_OMP_TEAMS_DISTRIBUTE \
12902 || op == EXEC_OMP_TEAMS_DISTRIBUTE_SIMD \
12903 || op == EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO \
12904 || op == EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD \
12905 || op == EXEC_OMP_TEAMS_LOOP)
12906
12907 if (!code->ext.omp_clauses->contains_teams_construct)
12908 return;
12909 gfc_code *c = code->block->next;
12910 if (c->op == EXEC_BLOCK)
12911 c = c->ext.block.ns->code;
12912 if (code->ext.omp_clauses->target_first_st_is_teams_or_meta)
12913 {
12914 if (c->op == EXEC_OMP_METADIRECTIVE)
12915 {
12916 struct gfc_omp_variant *mc
12917 = c->ext.omp_variants;
12918 /* All mc->(next...->)code should be identical with regards
12919 to the diagnostic below. */
12920 do
12921 {
12922 if (mc->stmt != ST_NONE
12923 && GFC_IS_TEAMS_CONSTRUCT (mc->code->op))
12924 {
12925 if (c->next == NULL && mc->code->next == NULL)
12926 return;
12927 c = mc->code;
12928 break;
12929 }
12930 mc = mc->next;
12931 }
12932 while (mc);
12933 }
12934 else if (GFC_IS_TEAMS_CONSTRUCT (c->op) && c->next == NULL)
12935 return;
12936 }
12937
12938 while (c && !GFC_IS_TEAMS_CONSTRUCT (c->op))
12939 c = c->next;
12940 if (c)
12941 gfc_error ("!$OMP TARGET region at %L with a nested TEAMS at %L may not "
12942 "contain any other statement, declaration or directive outside "
12943 "of the single TEAMS construct", &c->loc, &code->loc);
12944 else
12945 gfc_error ("!$OMP TARGET region at %L with a nested TEAMS may not "
12946 "contain any other statement, declaration or directive outside "
12947 "of the single TEAMS construct", &code->loc);
12948 #undef GFC_IS_TEAMS_CONSTRUCT
12949 }
12950
12951 static void
12952 resolve_omp_dispatch (gfc_code *code)
12953 {
12954 gfc_code *next = code->block->next;
12955 if (next == NULL)
12956 return;
12957
12958 gfc_exec_op op = next->op;
12959 gcc_assert (op == EXEC_CALL || op == EXEC_ASSIGN);
12960 if (op != EXEC_CALL
12961 && (op != EXEC_ASSIGN || next->expr2->expr_type != EXPR_FUNCTION))
12962 gfc_error (
12963 "%<OMP DISPATCH%> directive at %L must be followed by a procedure "
12964 "call with optional assignment",
12965 &code->loc);
12966
12967 if ((op == EXEC_CALL && next->resolved_sym != NULL
12968 && next->resolved_sym->attr.proc_pointer)
12969 || (op == EXEC_ASSIGN && gfc_expr_attr (next->expr2).proc_pointer))
12970 gfc_error ("%<OMP DISPATCH%> directive at %L cannot be followed by a "
12971 "procedure pointer",
12972 &code->loc);
12973 }
12974
12975 /* Resolve OpenMP directive clauses and check various requirements
12976 of each directive. */
12977
12978 void
12979 gfc_resolve_omp_directive (gfc_code *code, gfc_namespace *ns)
12980 {
12981 resolve_omp_directive_inside_oacc_region (code);
12982
12983 if (code->op != EXEC_OMP_ATOMIC)
12984 gfc_maybe_initialize_eh ();
12985
12986 switch (code->op)
12987 {
12988 case EXEC_OMP_DISTRIBUTE:
12989 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12990 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12991 case EXEC_OMP_DISTRIBUTE_SIMD:
12992 case EXEC_OMP_DO:
12993 case EXEC_OMP_DO_SIMD:
12994 case EXEC_OMP_LOOP:
12995 case EXEC_OMP_PARALLEL_DO:
12996 case EXEC_OMP_PARALLEL_DO_SIMD:
12997 case EXEC_OMP_PARALLEL_LOOP:
12998 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
12999 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
13000 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
13001 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
13002 case EXEC_OMP_MASKED_TASKLOOP:
13003 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
13004 case EXEC_OMP_MASTER_TASKLOOP:
13005 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
13006 case EXEC_OMP_SIMD:
13007 case EXEC_OMP_TARGET_PARALLEL_DO:
13008 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
13009 case EXEC_OMP_TARGET_PARALLEL_LOOP:
13010 case EXEC_OMP_TARGET_SIMD:
13011 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
13012 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
13013 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13014 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
13015 case EXEC_OMP_TARGET_TEAMS_LOOP:
13016 case EXEC_OMP_TASKLOOP:
13017 case EXEC_OMP_TASKLOOP_SIMD:
13018 case EXEC_OMP_TEAMS_DISTRIBUTE:
13019 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
13020 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13021 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
13022 case EXEC_OMP_TEAMS_LOOP:
13023 case EXEC_OMP_TILE:
13024 case EXEC_OMP_UNROLL:
13025 resolve_omp_do (code);
13026 break;
13027 case EXEC_OMP_TARGET:
13028 resolve_omp_target (code);
13029 gcc_fallthrough ();
13030 case EXEC_OMP_ALLOCATE:
13031 case EXEC_OMP_ALLOCATORS:
13032 case EXEC_OMP_ASSUME:
13033 case EXEC_OMP_CANCEL:
13034 case EXEC_OMP_ERROR:
13035 case EXEC_OMP_INTEROP:
13036 case EXEC_OMP_MASKED:
13037 case EXEC_OMP_ORDERED:
13038 case EXEC_OMP_PARALLEL_WORKSHARE:
13039 case EXEC_OMP_PARALLEL:
13040 case EXEC_OMP_PARALLEL_MASKED:
13041 case EXEC_OMP_PARALLEL_MASTER:
13042 case EXEC_OMP_PARALLEL_SECTIONS:
13043 case EXEC_OMP_SCOPE:
13044 case EXEC_OMP_SECTIONS:
13045 case EXEC_OMP_SINGLE:
13046 case EXEC_OMP_TARGET_DATA:
13047 case EXEC_OMP_TARGET_ENTER_DATA:
13048 case EXEC_OMP_TARGET_EXIT_DATA:
13049 case EXEC_OMP_TARGET_PARALLEL:
13050 case EXEC_OMP_TARGET_TEAMS:
13051 case EXEC_OMP_TASK:
13052 case EXEC_OMP_TASKWAIT:
13053 case EXEC_OMP_TEAMS:
13054 case EXEC_OMP_WORKSHARE:
13055 case EXEC_OMP_DEPOBJ:
13056 if (code->ext.omp_clauses)
13057 resolve_omp_clauses (code, code->ext.omp_clauses, NULL);
13058 break;
13059 case EXEC_OMP_TARGET_UPDATE:
13060 if (code->ext.omp_clauses)
13061 resolve_omp_clauses (code, code->ext.omp_clauses, NULL);
13062 if (code->ext.omp_clauses == NULL
13063 || (code->ext.omp_clauses->lists[OMP_LIST_TO] == NULL
13064 && code->ext.omp_clauses->lists[OMP_LIST_FROM] == NULL))
13065 gfc_error ("OMP TARGET UPDATE at %L requires at least one TO or "
13066 "FROM clause", &code->loc);
13067 break;
13068 case EXEC_OMP_ATOMIC:
13069 resolve_omp_clauses (code, code->block->ext.omp_clauses, NULL);
13070 resolve_omp_atomic (code);
13071 break;
13072 case EXEC_OMP_CRITICAL:
13073 resolve_omp_clauses (code, code->ext.omp_clauses, NULL);
13074 if (!code->ext.omp_clauses->critical_name
13075 && code->ext.omp_clauses->hint
13076 && code->ext.omp_clauses->hint->ts.type == BT_INTEGER
13077 && code->ext.omp_clauses->hint->expr_type == EXPR_CONSTANT
13078 && mpz_sgn (code->ext.omp_clauses->hint->value.integer) != 0)
13079 gfc_error ("OMP CRITICAL at %L with HINT clause requires a NAME, "
13080 "except when omp_sync_hint_none is used", &code->loc);
13081 break;
13082 case EXEC_OMP_SCAN:
13083 /* Flag is only used to checking, hence, it is unset afterwards. */
13084 if (!code->ext.omp_clauses->if_present)
13085 gfc_error ("Unexpected !$OMP SCAN at %L outside loop construct with "
13086 "%<inscan%> REDUCTION clause", &code->loc);
13087 code->ext.omp_clauses->if_present = false;
13088 resolve_omp_clauses (code, code->ext.omp_clauses, ns);
13089 break;
13090 case EXEC_OMP_DISPATCH:
13091 if (code->ext.omp_clauses)
13092 resolve_omp_clauses (code, code->ext.omp_clauses, ns);
13093 resolve_omp_dispatch (code);
13094 break;
13095 case EXEC_OMP_METADIRECTIVE:
13096 resolve_omp_metadirective (code, ns);
13097 break;
13098 default:
13099 break;
13100 }
13101 }
13102
13103 /* Resolve !$omp declare {variant|simd} constructs in NS.
13104 Note that !$omp declare target is resolved in resolve_symbol. */
13105
13106 void
13107 gfc_resolve_omp_declare (gfc_namespace *ns)
13108 {
13109 gfc_omp_declare_simd *ods;
13110 for (ods = ns->omp_declare_simd; ods; ods = ods->next)
13111 {
13112 if (ods->proc_name != NULL
13113 && ods->proc_name != ns->proc_name)
13114 gfc_error ("!$OMP DECLARE SIMD should refer to containing procedure "
13115 "%qs at %L", ns->proc_name->name, &ods->where);
13116 if (ods->clauses)
13117 resolve_omp_clauses (NULL, ods->clauses, ns);
13118 }
13119
13120 gfc_omp_declare_variant *odv;
13121 gfc_omp_namelist *range_begin = NULL;
13122 for (odv = ns->omp_declare_variant; odv; odv = odv->next)
13123 for (gfc_omp_namelist *n = odv->adjust_args_list; n != NULL; n = n->next)
13124 {
13125 if ((n->expr == NULL
13126 && (range_begin
13127 || n->u.adj_args.range_start
13128 || n->u.adj_args.omp_num_args_plus
13129 || n->u.adj_args.omp_num_args_minus))
13130 || n->u.adj_args.error_p)
13131 {
13132 }
13133 else if (range_begin
13134 || n->u.adj_args.range_start
13135 || n->u.adj_args.omp_num_args_plus
13136 || n->u.adj_args.omp_num_args_minus)
13137 {
13138 if (!n->expr
13139 || !gfc_resolve_expr (n->expr)
13140 || n->expr->expr_type != EXPR_CONSTANT
13141 || n->expr->ts.type != BT_INTEGER
13142 || n->expr->rank != 0
13143 || mpz_sgn (n->expr->value.integer) < 0
13144 || ((n->u.adj_args.omp_num_args_plus
13145 || n->u.adj_args.omp_num_args_minus)
13146 && mpz_sgn (n->expr->value.integer) == 0))
13147 {
13148 if (n->u.adj_args.omp_num_args_plus
13149 || n->u.adj_args.omp_num_args_minus)
13150 gfc_error ("Expected constant non-negative scalar integer "
13151 "offset expression at %L", &n->where);
13152 else
13153 gfc_error ("For range-based %<adjust_args%>, a constant "
13154 "positive scalar integer expression is required "
13155 "at %L", &n->where);
13156 }
13157 }
13158 else if (n->expr
13159 && n->expr->expr_type == EXPR_CONSTANT
13160 && n->expr->ts.type == BT_INTEGER
13161 && mpz_sgn (n->expr->value.integer) > 0)
13162 {
13163 }
13164 else if (!n->expr
13165 || !gfc_resolve_expr (n->expr)
13166 || n->expr->expr_type != EXPR_VARIABLE)
13167 gfc_error ("Expected dummy parameter name or a positive integer "
13168 "at %L", &n->where);
13169 else if (n->expr->expr_type == EXPR_VARIABLE)
13170 n->sym = n->expr->symtree->n.sym;
13171
13172 range_begin = n->u.adj_args.range_start ? n : NULL;
13173 }
13174 }
13175
13176 struct omp_udr_callback_data
13177 {
13178 gfc_omp_udr *omp_udr;
13179 bool is_initializer;
13180 };
13181
13182 static int
13183 omp_udr_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
13184 void *data)
13185 {
13186 struct omp_udr_callback_data *cd = (struct omp_udr_callback_data *) data;
13187 if ((*e)->expr_type == EXPR_VARIABLE)
13188 {
13189 if (cd->is_initializer)
13190 {
13191 if ((*e)->symtree->n.sym != cd->omp_udr->omp_priv
13192 && (*e)->symtree->n.sym != cd->omp_udr->omp_orig)
13193 gfc_error ("Variable other than OMP_PRIV or OMP_ORIG used in "
13194 "INITIALIZER clause of !$OMP DECLARE REDUCTION at %L",
13195 &(*e)->where);
13196 }
13197 else
13198 {
13199 if ((*e)->symtree->n.sym != cd->omp_udr->omp_out
13200 && (*e)->symtree->n.sym != cd->omp_udr->omp_in)
13201 gfc_error ("Variable other than OMP_OUT or OMP_IN used in "
13202 "combiner of !$OMP DECLARE REDUCTION at %L",
13203 &(*e)->where);
13204 }
13205 }
13206 return 0;
13207 }
13208
13209 /* Resolve !$omp declare reduction constructs. */
13210
13211 static void
13212 gfc_resolve_omp_udr (gfc_omp_udr *omp_udr)
13213 {
13214 gfc_actual_arglist *a;
13215 const char *predef_name = NULL;
13216
13217 switch (omp_udr->rop)
13218 {
13219 case OMP_REDUCTION_PLUS:
13220 case OMP_REDUCTION_TIMES:
13221 case OMP_REDUCTION_MINUS:
13222 case OMP_REDUCTION_AND:
13223 case OMP_REDUCTION_OR:
13224 case OMP_REDUCTION_EQV:
13225 case OMP_REDUCTION_NEQV:
13226 case OMP_REDUCTION_MAX:
13227 case OMP_REDUCTION_USER:
13228 break;
13229 default:
13230 gfc_error ("Invalid operator for !$OMP DECLARE REDUCTION %s at %L",
13231 omp_udr->name, &omp_udr->where);
13232 return;
13233 }
13234
13235 if (gfc_omp_udr_predef (omp_udr->rop, omp_udr->name,
13236 &omp_udr->ts, &predef_name))
13237 {
13238 if (predef_name)
13239 gfc_error_now ("Redefinition of predefined %s "
13240 "!$OMP DECLARE REDUCTION at %L",
13241 predef_name, &omp_udr->where);
13242 else
13243 gfc_error_now ("Redefinition of predefined "
13244 "!$OMP DECLARE REDUCTION at %L", &omp_udr->where);
13245 return;
13246 }
13247
13248 if (omp_udr->ts.type == BT_CHARACTER
13249 && omp_udr->ts.u.cl->length
13250 && omp_udr->ts.u.cl->length->expr_type != EXPR_CONSTANT)
13251 {
13252 gfc_error ("CHARACTER length in !$OMP DECLARE REDUCTION %s not "
13253 "constant at %L", omp_udr->name, &omp_udr->where);
13254 return;
13255 }
13256
13257 struct omp_udr_callback_data cd;
13258 cd.omp_udr = omp_udr;
13259 cd.is_initializer = false;
13260 gfc_code_walker (&omp_udr->combiner_ns->code, gfc_dummy_code_callback,
13261 omp_udr_callback, &cd);
13262 if (omp_udr->combiner_ns->code->op == EXEC_CALL)
13263 {
13264 for (a = omp_udr->combiner_ns->code->ext.actual; a; a = a->next)
13265 if (a->expr == NULL)
13266 break;
13267 if (a)
13268 gfc_error ("Subroutine call with alternate returns in combiner "
13269 "of !$OMP DECLARE REDUCTION at %L",
13270 &omp_udr->combiner_ns->code->loc);
13271 }
13272 if (omp_udr->initializer_ns)
13273 {
13274 cd.is_initializer = true;
13275 gfc_code_walker (&omp_udr->initializer_ns->code, gfc_dummy_code_callback,
13276 omp_udr_callback, &cd);
13277 if (omp_udr->initializer_ns->code->op == EXEC_CALL)
13278 {
13279 for (a = omp_udr->initializer_ns->code->ext.actual; a; a = a->next)
13280 if (a->expr == NULL)
13281 break;
13282 if (a)
13283 gfc_error ("Subroutine call with alternate returns in "
13284 "INITIALIZER clause of !$OMP DECLARE REDUCTION "
13285 "at %L", &omp_udr->initializer_ns->code->loc);
13286 for (a = omp_udr->initializer_ns->code->ext.actual; a; a = a->next)
13287 if (a->expr
13288 && a->expr->expr_type == EXPR_VARIABLE
13289 && a->expr->symtree->n.sym == omp_udr->omp_priv
13290 && a->expr->ref == NULL)
13291 break;
13292 if (a == NULL)
13293 gfc_error ("One of actual subroutine arguments in INITIALIZER "
13294 "clause of !$OMP DECLARE REDUCTION must be OMP_PRIV "
13295 "at %L", &omp_udr->initializer_ns->code->loc);
13296 }
13297 }
13298 else if (omp_udr->ts.type == BT_DERIVED
13299 && !gfc_has_default_initializer (omp_udr->ts.u.derived))
13300 {
13301 gfc_error ("Missing INITIALIZER clause for !$OMP DECLARE REDUCTION "
13302 "of derived type without default initializer at %L",
13303 &omp_udr->where);
13304 return;
13305 }
13306 }
13307
13308 void
13309 gfc_resolve_omp_udrs (gfc_symtree *st)
13310 {
13311 gfc_omp_udr *omp_udr;
13312
13313 if (st == NULL)
13314 return;
13315 gfc_resolve_omp_udrs (st->left);
13316 gfc_resolve_omp_udrs (st->right);
13317 for (omp_udr = st->n.omp_udr; omp_udr; omp_udr = omp_udr->next)
13318 gfc_resolve_omp_udr (omp_udr);
13319 }