1 //===-- lib/Semantics/check-omp-structure.cpp -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "check-omp-structure.h"
10 #include "flang/Parser/parse-tree.h"
11 #include "flang/Semantics/tools.h"
12 #include <algorithm>
13 
14 namespace Fortran::semantics {
15 
16 // Use when clause falls under 'struct OmpClause' in 'parse-tree.h'.
17 #define CHECK_SIMPLE_CLAUSE(X, Y) \
18   void OmpStructureChecker::Enter(const parser::OmpClause::X &) { \
19     CheckAllowed(llvm::omp::Clause::Y); \
20   }
21 
22 #define CHECK_REQ_CONSTANT_SCALAR_INT_CLAUSE(X, Y) \
23   void OmpStructureChecker::Enter(const parser::OmpClause::X &c) { \
24     CheckAllowed(llvm::omp::Clause::Y); \
25     RequiresConstantPositiveParameter(llvm::omp::Clause::Y, c.v); \
26   }
27 
28 #define CHECK_REQ_SCALAR_INT_CLAUSE(X, Y) \
29   void OmpStructureChecker::Enter(const parser::OmpClause::X &c) { \
30     CheckAllowed(llvm::omp::Clause::Y); \
31     RequiresPositiveParameter(llvm::omp::Clause::Y, c.v); \
32   }
33 
34 // Use when clause don't falls under 'struct OmpClause' in 'parse-tree.h'.
35 #define CHECK_SIMPLE_PARSER_CLAUSE(X, Y) \
36   void OmpStructureChecker::Enter(const parser::X &) { \
37     CheckAllowed(llvm::omp::Y); \
38   }
39 
40 // 'OmpWorkshareBlockChecker' is used to check the validity of the assignment
41 // statements and the expressions enclosed in an OpenMP Workshare construct
42 class OmpWorkshareBlockChecker {
43 public:
44   OmpWorkshareBlockChecker(SemanticsContext &context, parser::CharBlock source)
45       : context_{context}, source_{source} {}
46 
47   template <typename T> bool Pre(const T &) { return true; }
48   template <typename T> void Post(const T &) {}
49 
50   bool Pre(const parser::AssignmentStmt &assignment) {
51     const auto &var{std::get<parser::Variable>(assignment.t)};
52     const auto &expr{std::get<parser::Expr>(assignment.t)};
53     const auto *lhs{GetExpr(var)};
54     const auto *rhs{GetExpr(expr)};
55     Tristate isDefined{semantics::IsDefinedAssignment(
56         lhs->GetType(), lhs->Rank(), rhs->GetType(), rhs->Rank())};
57     if (isDefined == Tristate::Yes) {
58       context_.Say(expr.source,
59           "Defined assignment statement is not "
60           "allowed in a WORKSHARE construct"_err_en_US);
61     }
62     return true;
63   }
64 
65   bool Pre(const parser::Expr &expr) {
66     if (const auto *e{GetExpr(expr)}) {
67       for (const Symbol &symbol : evaluate::CollectSymbols(*e)) {
68         const Symbol &root{GetAssociationRoot(symbol)};
69         if (IsFunction(root) &&
70             !(root.attrs().test(Attr::ELEMENTAL) ||
71                 root.attrs().test(Attr::INTRINSIC))) {
72           context_.Say(expr.source,
73               "User defined non-ELEMENTAL function "
74               "'%s' is not allowed in a WORKSHARE construct"_err_en_US,
75               root.name());
76         }
77       }
78     }
79     return false;
80   }
81 
82 private:
83   SemanticsContext &context_;
84   parser::CharBlock source_;
85 };
86 
87 class OmpCycleChecker {
88 public:
89   OmpCycleChecker(SemanticsContext &context, std::int64_t cycleLevel)
90       : context_{context}, cycleLevel_{cycleLevel} {}
91 
92   template <typename T> bool Pre(const T &) { return true; }
93   template <typename T> void Post(const T &) {}
94 
95   bool Pre(const parser::DoConstruct &dc) {
96     cycleLevel_--;
97     const auto &labelName{std::get<0>(std::get<0>(dc.t).statement.t)};
98     if (labelName) {
99       labelNamesandLevels_.emplace(labelName.value().ToString(), cycleLevel_);
100     }
101     return true;
102   }
103 
104   bool Pre(const parser::CycleStmt &cyclestmt) {
105     std::map<std::string, std::int64_t>::iterator it;
106     bool err{false};
107     if (cyclestmt.v) {
108       it = labelNamesandLevels_.find(cyclestmt.v->source.ToString());
109       err = (it != labelNamesandLevels_.end() && it->second > 0);
110     }
111     if (cycleLevel_ > 0 || err) {
112       context_.Say(*cycleSource_,
113           "CYCLE statement to non-innermost associated loop of an OpenMP DO construct"_err_en_US);
114     }
115     return true;
116   }
117 
118   bool Pre(const parser::Statement<parser::ActionStmt> &actionstmt) {
119     cycleSource_ = &actionstmt.source;
120     return true;
121   }
122 
123 private:
124   SemanticsContext &context_;
125   const parser::CharBlock *cycleSource_;
126   std::int64_t cycleLevel_;
127   std::map<std::string, std::int64_t> labelNamesandLevels_;
128 };
129 
130 bool OmpStructureChecker::IsCloselyNestedRegion(const OmpDirectiveSet &set) {
131   // Definition of close nesting:
132   //
133   // `A region nested inside another region with no parallel region nested
134   // between them`
135   //
136   // Examples:
137   //   non-parallel construct 1
138   //    non-parallel construct 2
139   //      parallel construct
140   //        construct 3
141   // In the above example, construct 3 is NOT closely nested inside construct 1
142   // or 2
143   //
144   //   non-parallel construct 1
145   //    non-parallel construct 2
146   //        construct 3
147   // In the above example, construct 3 is closely nested inside BOTH construct 1
148   // and 2
149   //
150   // Algorithm:
151   // Starting from the parent context, Check in a bottom-up fashion, each level
152   // of the context stack. If we have a match for one of the (supplied)
153   // violating directives, `close nesting` is satisfied. If no match is there in
154   // the entire stack, `close nesting` is not satisfied. If at any level, a
155   // `parallel` region is found, `close nesting` is not satisfied.
156 
157   if (CurrentDirectiveIsNested()) {
158     int index = dirContext_.size() - 2;
159     while (index != -1) {
160       if (set.test(dirContext_[index].directive)) {
161         return true;
162       } else if (llvm::omp::parallelSet.test(dirContext_[index].directive)) {
163         return false;
164       }
165       index--;
166     }
167   }
168   return false;
169 }
170 
171 bool OmpStructureChecker::HasInvalidWorksharingNesting(
172     const parser::CharBlock &source, const OmpDirectiveSet &set) {
173   // set contains all the invalid closely nested directives
174   // for the given directive (`source` here)
175   if (IsCloselyNestedRegion(set)) {
176     context_.Say(source,
177         "A worksharing region may not be closely nested inside a "
178         "worksharing, explicit task, taskloop, critical, ordered, atomic, or "
179         "master region"_err_en_US);
180     return true;
181   }
182   return false;
183 }
184 
185 void OmpStructureChecker::Enter(const parser::OpenMPConstruct &) {
186   // 2.8.1 TODO: Simd Construct with Ordered Construct Nesting check
187 }
188 
189 void OmpStructureChecker::Enter(const parser::OpenMPLoopConstruct &x) {
190   const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)};
191   const auto &beginDir{std::get<parser::OmpLoopDirective>(beginLoopDir.t)};
192 
193   // check matching, End directive is optional
194   if (const auto &endLoopDir{
195           std::get<std::optional<parser::OmpEndLoopDirective>>(x.t)}) {
196     const auto &endDir{
197         std::get<parser::OmpLoopDirective>(endLoopDir.value().t)};
198 
199     CheckMatching<parser::OmpLoopDirective>(beginDir, endDir);
200   }
201 
202   PushContextAndClauseSets(beginDir.source, beginDir.v);
203 
204   if (beginDir.v == llvm::omp::Directive::OMPD_do) {
205     // 2.7.1 do-clause -> private-clause |
206     //                    firstprivate-clause |
207     //                    lastprivate-clause |
208     //                    linear-clause |
209     //                    reduction-clause |
210     //                    schedule-clause |
211     //                    collapse-clause |
212     //                    ordered-clause
213 
214     // nesting check
215     HasInvalidWorksharingNesting(beginDir.source,
216         {llvm::omp::Directive::OMPD_do, llvm::omp::Directive::OMPD_sections,
217             llvm::omp::Directive::OMPD_single,
218             llvm::omp::Directive::OMPD_workshare,
219             llvm::omp::Directive::OMPD_task,
220             llvm::omp::Directive::OMPD_taskloop,
221             llvm::omp::Directive::OMPD_critical,
222             llvm::omp::Directive::OMPD_ordered,
223             llvm::omp::Directive::OMPD_atomic,
224             llvm::omp::Directive::OMPD_master});
225   }
226   SetLoopInfo(x);
227 
228   if (const auto &doConstruct{
229           std::get<std::optional<parser::DoConstruct>>(x.t)}) {
230     const auto &doBlock{std::get<parser::Block>(doConstruct->t)};
231     CheckNoBranching(doBlock, beginDir.v, beginDir.source);
232   }
233   CheckDoWhile(x);
234   CheckLoopItrVariableIsInt(x);
235   CheckCycleConstraints(x);
236 }
237 const parser::Name OmpStructureChecker::GetLoopIndex(
238     const parser::DoConstruct *x) {
239   using Bounds = parser::LoopControl::Bounds;
240   return std::get<Bounds>(x->GetLoopControl()->u).name.thing;
241 }
242 void OmpStructureChecker::SetLoopInfo(const parser::OpenMPLoopConstruct &x) {
243   if (const auto &loopConstruct{
244           std::get<std::optional<parser::DoConstruct>>(x.t)}) {
245     const parser::DoConstruct *loop{&*loopConstruct};
246     if (loop && loop->IsDoNormal()) {
247       const parser::Name &itrVal{GetLoopIndex(loop)};
248       SetLoopIv(itrVal.symbol);
249     }
250   }
251 }
252 void OmpStructureChecker::CheckDoWhile(const parser::OpenMPLoopConstruct &x) {
253   const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)};
254   const auto &beginDir{std::get<parser::OmpLoopDirective>(beginLoopDir.t)};
255   if (beginDir.v == llvm::omp::Directive::OMPD_do) {
256     if (const auto &doConstruct{
257             std::get<std::optional<parser::DoConstruct>>(x.t)}) {
258       if (doConstruct.value().IsDoWhile()) {
259         const auto &doStmt{std::get<parser::Statement<parser::NonLabelDoStmt>>(
260             doConstruct.value().t)};
261         context_.Say(doStmt.source,
262             "The DO loop cannot be a DO WHILE with DO directive."_err_en_US);
263       }
264     }
265   }
266 }
267 
268 void OmpStructureChecker::CheckLoopItrVariableIsInt(
269     const parser::OpenMPLoopConstruct &x) {
270   if (const auto &loopConstruct{
271           std::get<std::optional<parser::DoConstruct>>(x.t)}) {
272 
273     for (const parser::DoConstruct *loop{&*loopConstruct}; loop;) {
274       if (loop->IsDoNormal()) {
275         const parser::Name &itrVal{GetLoopIndex(loop)};
276         if (itrVal.symbol) {
277           const auto *type{itrVal.symbol->GetType()};
278           if (!type->IsNumeric(TypeCategory::Integer)) {
279             context_.Say(itrVal.source,
280                 "The DO loop iteration"
281                 " variable must be of the type integer."_err_en_US,
282                 itrVal.ToString());
283           }
284         }
285       }
286       // Get the next DoConstruct if block is not empty.
287       const auto &block{std::get<parser::Block>(loop->t)};
288       const auto it{block.begin()};
289       loop = it != block.end() ? parser::Unwrap<parser::DoConstruct>(*it)
290                                : nullptr;
291     }
292   }
293 }
294 
295 std::int64_t OmpStructureChecker::GetOrdCollapseLevel(
296     const parser::OpenMPLoopConstruct &x) {
297   const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)};
298   const auto &clauseList{std::get<parser::OmpClauseList>(beginLoopDir.t)};
299   std::int64_t orderedCollapseLevel{1};
300   std::int64_t orderedLevel{0};
301   std::int64_t collapseLevel{0};
302 
303   for (const auto &clause : clauseList.v) {
304     if (const auto *collapseClause{
305             std::get_if<parser::OmpClause::Collapse>(&clause.u)}) {
306       if (const auto v{GetIntValue(collapseClause->v)}) {
307         collapseLevel = *v;
308       }
309     }
310     if (const auto *orderedClause{
311             std::get_if<parser::OmpClause::Ordered>(&clause.u)}) {
312       if (const auto v{GetIntValue(orderedClause->v)}) {
313         orderedLevel = *v;
314       }
315     }
316   }
317   if (orderedLevel >= collapseLevel) {
318     orderedCollapseLevel = orderedLevel;
319   } else {
320     orderedCollapseLevel = collapseLevel;
321   }
322   return orderedCollapseLevel;
323 }
324 
325 void OmpStructureChecker::CheckCycleConstraints(
326     const parser::OpenMPLoopConstruct &x) {
327   std::int64_t ordCollapseLevel{GetOrdCollapseLevel(x)};
328   OmpCycleChecker ompCycleChecker{context_, ordCollapseLevel};
329   parser::Walk(x, ompCycleChecker);
330 }
331 
332 void OmpStructureChecker::Leave(const parser::OpenMPLoopConstruct &) {
333   dirContext_.pop_back();
334 }
335 
336 void OmpStructureChecker::Enter(const parser::OmpEndLoopDirective &x) {
337   const auto &dir{std::get<parser::OmpLoopDirective>(x.t)};
338   ResetPartialContext(dir.source);
339   switch (dir.v) {
340   // 2.7.1 end-do -> END DO [nowait-clause]
341   // 2.8.3 end-do-simd -> END DO SIMD [nowait-clause]
342   case llvm::omp::Directive::OMPD_do:
343   case llvm::omp::Directive::OMPD_do_simd:
344     SetClauseSets(dir.v);
345     break;
346   default:
347     // no clauses are allowed
348     break;
349   }
350 }
351 
352 void OmpStructureChecker::Enter(const parser::OpenMPBlockConstruct &x) {
353   const auto &beginBlockDir{std::get<parser::OmpBeginBlockDirective>(x.t)};
354   const auto &endBlockDir{std::get<parser::OmpEndBlockDirective>(x.t)};
355   const auto &beginDir{std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
356   const auto &endDir{std::get<parser::OmpBlockDirective>(endBlockDir.t)};
357   const parser::Block &block{std::get<parser::Block>(x.t)};
358 
359   CheckMatching<parser::OmpBlockDirective>(beginDir, endDir);
360 
361   PushContextAndClauseSets(beginDir.source, beginDir.v);
362 
363   // TODO: This check needs to be extended while implementing nesting of regions
364   // checks.
365   if (beginDir.v == llvm::omp::Directive::OMPD_single) {
366     HasInvalidWorksharingNesting(
367         beginDir.source, {llvm::omp::Directive::OMPD_do});
368   }
369   if (CurrentDirectiveIsNested())
370     CheckIfDoOrderedClause(beginDir);
371 
372   CheckNoBranching(block, beginDir.v, beginDir.source);
373 
374   switch (beginDir.v) {
375   case llvm::omp::OMPD_workshare:
376   case llvm::omp::OMPD_parallel_workshare:
377     CheckWorkshareBlockStmts(block, beginDir.source);
378     break;
379   default:
380     break;
381   }
382 }
383 
384 void OmpStructureChecker::CheckIfDoOrderedClause(
385     const parser::OmpBlockDirective &blkDirective) {
386   if (blkDirective.v == llvm::omp::OMPD_ordered) {
387     // Loops
388     if (llvm::omp::doSet.test(GetContextParent().directive) &&
389         !FindClauseParent(llvm::omp::Clause::OMPC_ordered)) {
390       context_.Say(blkDirective.source,
391           "The ORDERED clause must be present on the loop"
392           " construct if any ORDERED region ever binds"
393           " to a loop region arising from the loop construct."_err_en_US);
394     }
395     // Other disallowed nestings, these directives do not support
396     // ordered clause in them, so no need to check
397     else if (IsCloselyNestedRegion(llvm::omp::nestedOrderedErrSet)) {
398       context_.Say(blkDirective.source,
399           "`ORDERED` region may not be closely nested inside of "
400           "`CRITICAL`, `ORDERED`, explicit `TASK` or `TASKLOOP` region."_err_en_US);
401     }
402   }
403 }
404 
405 void OmpStructureChecker::Leave(const parser::OpenMPBlockConstruct &) {
406   dirContext_.pop_back();
407 }
408 
409 void OmpStructureChecker::Enter(const parser::OpenMPSectionsConstruct &x) {
410   const auto &beginSectionsDir{
411       std::get<parser::OmpBeginSectionsDirective>(x.t)};
412   const auto &endSectionsDir{std::get<parser::OmpEndSectionsDirective>(x.t)};
413   const auto &beginDir{
414       std::get<parser::OmpSectionsDirective>(beginSectionsDir.t)};
415   const auto &endDir{std::get<parser::OmpSectionsDirective>(endSectionsDir.t)};
416   CheckMatching<parser::OmpSectionsDirective>(beginDir, endDir);
417 
418   PushContextAndClauseSets(beginDir.source, beginDir.v);
419   const auto &sectionBlocks{std::get<parser::OmpSectionBlocks>(x.t)};
420   for (const auto &block : sectionBlocks.v) {
421     CheckNoBranching(block, beginDir.v, beginDir.source);
422   }
423 }
424 
425 void OmpStructureChecker::Leave(const parser::OpenMPSectionsConstruct &) {
426   dirContext_.pop_back();
427 }
428 
429 void OmpStructureChecker::Enter(const parser::OmpEndSectionsDirective &x) {
430   const auto &dir{std::get<parser::OmpSectionsDirective>(x.t)};
431   ResetPartialContext(dir.source);
432   switch (dir.v) {
433     // 2.7.2 end-sections -> END SECTIONS [nowait-clause]
434   case llvm::omp::Directive::OMPD_sections:
435     PushContextAndClauseSets(
436         dir.source, llvm::omp::Directive::OMPD_end_sections);
437     break;
438   default:
439     // no clauses are allowed
440     break;
441   }
442 }
443 
444 // TODO: Verify the popping of dirContext requirement after nowait
445 // implementation, as there is an implicit barrier at the end of the worksharing
446 // constructs unless a nowait clause is specified. Only OMPD_end_sections is
447 // popped becuase it is pushed while entering the EndSectionsDirective.
448 void OmpStructureChecker::Leave(const parser::OmpEndSectionsDirective &x) {
449   if (GetContext().directive == llvm::omp::Directive::OMPD_end_sections) {
450     dirContext_.pop_back();
451   }
452 }
453 
454 void OmpStructureChecker::Enter(const parser::OpenMPDeclareSimdConstruct &x) {
455   const auto &dir{std::get<parser::Verbatim>(x.t)};
456   PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_declare_simd);
457 }
458 
459 void OmpStructureChecker::Leave(const parser::OpenMPDeclareSimdConstruct &) {
460   dirContext_.pop_back();
461 }
462 
463 void OmpStructureChecker::Enter(const parser::OpenMPDeclarativeAllocate &x) {
464   const auto &dir{std::get<parser::Verbatim>(x.t)};
465   const auto &objectList{std::get<parser::OmpObjectList>(x.t)};
466   PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_allocate);
467   CheckIsVarPartOfAnotherVar(dir.source, objectList);
468 }
469 
470 void OmpStructureChecker::Leave(const parser::OpenMPDeclarativeAllocate &x) {
471   dirContext_.pop_back();
472 }
473 
474 void OmpStructureChecker::Enter(const parser::OpenMPDeclareTargetConstruct &x) {
475   const auto &dir{std::get<parser::Verbatim>(x.t)};
476   PushContext(dir.source, llvm::omp::Directive::OMPD_declare_target);
477   const auto &spec{std::get<parser::OmpDeclareTargetSpecifier>(x.t)};
478   if (std::holds_alternative<parser::OmpDeclareTargetWithClause>(spec.u)) {
479     SetClauseSets(llvm::omp::Directive::OMPD_declare_target);
480   }
481 }
482 
483 void OmpStructureChecker::Leave(const parser::OpenMPDeclareTargetConstruct &) {
484   dirContext_.pop_back();
485 }
486 
487 void OmpStructureChecker::Enter(const parser::OpenMPExecutableAllocate &x) {
488   const auto &dir{std::get<parser::Verbatim>(x.t)};
489   const auto &objectList{std::get<std::optional<parser::OmpObjectList>>(x.t)};
490   PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_allocate);
491   if (objectList)
492     CheckIsVarPartOfAnotherVar(dir.source, *objectList);
493 }
494 
495 void OmpStructureChecker::Leave(const parser::OpenMPExecutableAllocate &) {
496   dirContext_.pop_back();
497 }
498 
499 void OmpStructureChecker::Enter(
500     const parser::OpenMPSimpleStandaloneConstruct &x) {
501   const auto &dir{std::get<parser::OmpSimpleStandaloneDirective>(x.t)};
502   PushContextAndClauseSets(dir.source, dir.v);
503 }
504 
505 void OmpStructureChecker::Leave(
506     const parser::OpenMPSimpleStandaloneConstruct &) {
507   dirContext_.pop_back();
508 }
509 
510 void OmpStructureChecker::Enter(const parser::OpenMPFlushConstruct &x) {
511   const auto &dir{std::get<parser::Verbatim>(x.t)};
512   PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_flush);
513 }
514 
515 void OmpStructureChecker::Leave(const parser::OpenMPFlushConstruct &x) {
516   if (FindClause(llvm::omp::Clause::OMPC_acquire) ||
517       FindClause(llvm::omp::Clause::OMPC_release) ||
518       FindClause(llvm::omp::Clause::OMPC_acq_rel)) {
519     if (const auto &flushList{
520             std::get<std::optional<parser::OmpObjectList>>(x.t)}) {
521       context_.Say(parser::FindSourceLocation(flushList),
522           "If memory-order-clause is RELEASE, ACQUIRE, or ACQ_REL, list items "
523           "must not be specified on the FLUSH directive"_err_en_US);
524     }
525   }
526   dirContext_.pop_back();
527 }
528 
529 void OmpStructureChecker::Enter(const parser::OpenMPCancelConstruct &x) {
530   const auto &dir{std::get<parser::Verbatim>(x.t)};
531   PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_cancel);
532 }
533 
534 void OmpStructureChecker::Leave(const parser::OpenMPCancelConstruct &) {
535   dirContext_.pop_back();
536 }
537 
538 void OmpStructureChecker::Enter(const parser::OpenMPCriticalConstruct &x) {
539   const auto &dir{std::get<parser::OmpCriticalDirective>(x.t)};
540   PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_critical);
541   const auto &block{std::get<parser::Block>(x.t)};
542   CheckNoBranching(block, llvm::omp::Directive::OMPD_critical, dir.source);
543 }
544 
545 void OmpStructureChecker::Leave(const parser::OpenMPCriticalConstruct &) {
546   dirContext_.pop_back();
547 }
548 
549 void OmpStructureChecker::Enter(
550     const parser::OpenMPCancellationPointConstruct &x) {
551   const auto &dir{std::get<parser::Verbatim>(x.t)};
552   PushContextAndClauseSets(
553       dir.source, llvm::omp::Directive::OMPD_cancellation_point);
554 }
555 
556 void OmpStructureChecker::Leave(
557     const parser::OpenMPCancellationPointConstruct &) {
558   dirContext_.pop_back();
559 }
560 
561 void OmpStructureChecker::Enter(const parser::OmpEndBlockDirective &x) {
562   const auto &dir{std::get<parser::OmpBlockDirective>(x.t)};
563   ResetPartialContext(dir.source);
564   switch (dir.v) {
565   // 2.7.3 end-single-clause -> copyprivate-clause |
566   //                            nowait-clause
567   case llvm::omp::Directive::OMPD_single:
568     PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_end_single);
569     break;
570   // 2.7.4 end-workshare -> END WORKSHARE [nowait-clause]
571   case llvm::omp::Directive::OMPD_workshare:
572     PushContextAndClauseSets(
573         dir.source, llvm::omp::Directive::OMPD_end_workshare);
574     break;
575   default:
576     // no clauses are allowed
577     break;
578   }
579 }
580 
581 // TODO: Verify the popping of dirContext requirement after nowait
582 // implementation, as there is an implicit barrier at the end of the worksharing
583 // constructs unless a nowait clause is specified. Only OMPD_end_single and
584 // end_workshareare popped as they are pushed while entering the
585 // EndBlockDirective.
586 void OmpStructureChecker::Leave(const parser::OmpEndBlockDirective &x) {
587   if ((GetContext().directive == llvm::omp::Directive::OMPD_end_single) ||
588       (GetContext().directive == llvm::omp::Directive::OMPD_end_workshare)) {
589     dirContext_.pop_back();
590   }
591 }
592 
593 void OmpStructureChecker::Enter(const parser::OpenMPAtomicConstruct &x) {
594   std::visit(
595       common::visitors{
596           [&](const auto &someAtomicConstruct) {
597             const auto &dir{std::get<parser::Verbatim>(someAtomicConstruct.t)};
598             PushContextAndClauseSets(
599                 dir.source, llvm::omp::Directive::OMPD_atomic);
600           },
601       },
602       x.u);
603 }
604 
605 void OmpStructureChecker::Leave(const parser::OpenMPAtomicConstruct &) {
606   dirContext_.pop_back();
607 }
608 
609 // Clauses
610 // Mainly categorized as
611 // 1. Checks on 'OmpClauseList' from 'parse-tree.h'.
612 // 2. Checks on clauses which fall under 'struct OmpClause' from parse-tree.h.
613 // 3. Checks on clauses which are not in 'struct OmpClause' from parse-tree.h.
614 
615 void OmpStructureChecker::Leave(const parser::OmpClauseList &) {
616   // 2.7 Loop Construct Restriction
617   if (llvm::omp::doSet.test(GetContext().directive)) {
618     if (auto *clause{FindClause(llvm::omp::Clause::OMPC_schedule)}) {
619       // only one schedule clause is allowed
620       const auto &schedClause{std::get<parser::OmpClause::Schedule>(clause->u)};
621       if (ScheduleModifierHasType(schedClause.v,
622               parser::OmpScheduleModifierType::ModType::Nonmonotonic)) {
623         if (FindClause(llvm::omp::Clause::OMPC_ordered)) {
624           context_.Say(clause->source,
625               "The NONMONOTONIC modifier cannot be specified "
626               "if an ORDERED clause is specified"_err_en_US);
627         }
628         if (ScheduleModifierHasType(schedClause.v,
629                 parser::OmpScheduleModifierType::ModType::Monotonic)) {
630           context_.Say(clause->source,
631               "The MONOTONIC and NONMONOTONIC modifiers "
632               "cannot be both specified"_err_en_US);
633         }
634       }
635     }
636 
637     if (auto *clause{FindClause(llvm::omp::Clause::OMPC_ordered)}) {
638       // only one ordered clause is allowed
639       const auto &orderedClause{
640           std::get<parser::OmpClause::Ordered>(clause->u)};
641 
642       if (orderedClause.v) {
643         CheckNotAllowedIfClause(
644             llvm::omp::Clause::OMPC_ordered, {llvm::omp::Clause::OMPC_linear});
645 
646         if (auto *clause2{FindClause(llvm::omp::Clause::OMPC_collapse)}) {
647           const auto &collapseClause{
648               std::get<parser::OmpClause::Collapse>(clause2->u)};
649           // ordered and collapse both have parameters
650           if (const auto orderedValue{GetIntValue(orderedClause.v)}) {
651             if (const auto collapseValue{GetIntValue(collapseClause.v)}) {
652               if (*orderedValue > 0 && *orderedValue < *collapseValue) {
653                 context_.Say(clause->source,
654                     "The parameter of the ORDERED clause must be "
655                     "greater than or equal to "
656                     "the parameter of the COLLAPSE clause"_err_en_US);
657               }
658             }
659           }
660         }
661       }
662 
663       // TODO: ordered region binding check (requires nesting implementation)
664     }
665   } // doSet
666 
667   // 2.8.1 Simd Construct Restriction
668   if (llvm::omp::simdSet.test(GetContext().directive)) {
669     if (auto *clause{FindClause(llvm::omp::Clause::OMPC_simdlen)}) {
670       if (auto *clause2{FindClause(llvm::omp::Clause::OMPC_safelen)}) {
671         const auto &simdlenClause{
672             std::get<parser::OmpClause::Simdlen>(clause->u)};
673         const auto &safelenClause{
674             std::get<parser::OmpClause::Safelen>(clause2->u)};
675         // simdlen and safelen both have parameters
676         if (const auto simdlenValue{GetIntValue(simdlenClause.v)}) {
677           if (const auto safelenValue{GetIntValue(safelenClause.v)}) {
678             if (*safelenValue > 0 && *simdlenValue > *safelenValue) {
679               context_.Say(clause->source,
680                   "The parameter of the SIMDLEN clause must be less than or "
681                   "equal to the parameter of the SAFELEN clause"_err_en_US);
682             }
683           }
684         }
685       }
686     }
687     // A list-item cannot appear in more than one aligned clause
688     semantics::UnorderedSymbolSet alignedVars;
689     auto clauseAll = FindClauses(llvm::omp::Clause::OMPC_aligned);
690     for (auto itr = clauseAll.first; itr != clauseAll.second; ++itr) {
691       const auto &alignedClause{
692           std::get<parser::OmpClause::Aligned>(itr->second->u)};
693       const auto &alignedNameList{
694           std::get<std::list<parser::Name>>(alignedClause.v.t)};
695       for (auto const &var : alignedNameList) {
696         if (alignedVars.count(*(var.symbol)) == 1) {
697           context_.Say(itr->second->source,
698               "List item '%s' present at multiple ALIGNED clauses"_err_en_US,
699               var.ToString());
700           break;
701         }
702         alignedVars.insert(*(var.symbol));
703       }
704     }
705   } // SIMD
706 
707   // 2.7.3 Single Construct Restriction
708   if (GetContext().directive == llvm::omp::Directive::OMPD_end_single) {
709     CheckNotAllowedIfClause(
710         llvm::omp::Clause::OMPC_copyprivate, {llvm::omp::Clause::OMPC_nowait});
711   }
712 
713   CheckRequireAtLeastOneOf();
714 }
715 
716 void OmpStructureChecker::Enter(const parser::OmpClause &x) {
717   SetContextClause(x);
718 }
719 
720 // Following clauses do not have a separate node in parse-tree.h.
721 CHECK_SIMPLE_CLAUSE(AcqRel, OMPC_acq_rel)
722 CHECK_SIMPLE_CLAUSE(Acquire, OMPC_acquire)
723 CHECK_SIMPLE_CLAUSE(AtomicDefaultMemOrder, OMPC_atomic_default_mem_order)
724 CHECK_SIMPLE_CLAUSE(Affinity, OMPC_affinity)
725 CHECK_SIMPLE_CLAUSE(Allocate, OMPC_allocate)
726 CHECK_SIMPLE_CLAUSE(Capture, OMPC_capture)
727 CHECK_SIMPLE_CLAUSE(Copyin, OMPC_copyin)
728 CHECK_SIMPLE_CLAUSE(Default, OMPC_default)
729 CHECK_SIMPLE_CLAUSE(Depobj, OMPC_depobj)
730 CHECK_SIMPLE_CLAUSE(Destroy, OMPC_destroy)
731 CHECK_SIMPLE_CLAUSE(Detach, OMPC_detach)
732 CHECK_SIMPLE_CLAUSE(Device, OMPC_device)
733 CHECK_SIMPLE_CLAUSE(DeviceType, OMPC_device_type)
734 CHECK_SIMPLE_CLAUSE(DistSchedule, OMPC_dist_schedule)
735 CHECK_SIMPLE_CLAUSE(DynamicAllocators, OMPC_dynamic_allocators)
736 CHECK_SIMPLE_CLAUSE(Exclusive, OMPC_exclusive)
737 CHECK_SIMPLE_CLAUSE(Final, OMPC_final)
738 CHECK_SIMPLE_CLAUSE(Flush, OMPC_flush)
739 CHECK_SIMPLE_CLAUSE(From, OMPC_from)
740 CHECK_SIMPLE_CLAUSE(Hint, OMPC_hint)
741 CHECK_SIMPLE_CLAUSE(InReduction, OMPC_in_reduction)
742 CHECK_SIMPLE_CLAUSE(Inclusive, OMPC_inclusive)
743 CHECK_SIMPLE_CLAUSE(Match, OMPC_match)
744 CHECK_SIMPLE_CLAUSE(Nontemporal, OMPC_nontemporal)
745 CHECK_SIMPLE_CLAUSE(Order, OMPC_order)
746 CHECK_SIMPLE_CLAUSE(Read, OMPC_read)
747 CHECK_SIMPLE_CLAUSE(ReverseOffload, OMPC_reverse_offload)
748 CHECK_SIMPLE_CLAUSE(Threadprivate, OMPC_threadprivate)
749 CHECK_SIMPLE_CLAUSE(Threads, OMPC_threads)
750 CHECK_SIMPLE_CLAUSE(Inbranch, OMPC_inbranch)
751 CHECK_SIMPLE_CLAUSE(IsDevicePtr, OMPC_is_device_ptr)
752 CHECK_SIMPLE_CLAUSE(Link, OMPC_link)
753 CHECK_SIMPLE_CLAUSE(Mergeable, OMPC_mergeable)
754 CHECK_SIMPLE_CLAUSE(Nogroup, OMPC_nogroup)
755 CHECK_SIMPLE_CLAUSE(Notinbranch, OMPC_notinbranch)
756 CHECK_SIMPLE_CLAUSE(Nowait, OMPC_nowait)
757 CHECK_SIMPLE_CLAUSE(ProcBind, OMPC_proc_bind)
758 CHECK_SIMPLE_CLAUSE(Release, OMPC_release)
759 CHECK_SIMPLE_CLAUSE(Relaxed, OMPC_relaxed)
760 CHECK_SIMPLE_CLAUSE(SeqCst, OMPC_seq_cst)
761 CHECK_SIMPLE_CLAUSE(Simd, OMPC_simd)
762 CHECK_SIMPLE_CLAUSE(Sizes, OMPC_sizes)
763 CHECK_SIMPLE_CLAUSE(TaskReduction, OMPC_task_reduction)
764 CHECK_SIMPLE_CLAUSE(To, OMPC_to)
765 CHECK_SIMPLE_CLAUSE(UnifiedAddress, OMPC_unified_address)
766 CHECK_SIMPLE_CLAUSE(UnifiedSharedMemory, OMPC_unified_shared_memory)
767 CHECK_SIMPLE_CLAUSE(Uniform, OMPC_uniform)
768 CHECK_SIMPLE_CLAUSE(Unknown, OMPC_unknown)
769 CHECK_SIMPLE_CLAUSE(Untied, OMPC_untied)
770 CHECK_SIMPLE_CLAUSE(UseDevicePtr, OMPC_use_device_ptr)
771 CHECK_SIMPLE_CLAUSE(UsesAllocators, OMPC_uses_allocators)
772 CHECK_SIMPLE_CLAUSE(Update, OMPC_update)
773 CHECK_SIMPLE_CLAUSE(UseDeviceAddr, OMPC_use_device_addr)
774 CHECK_SIMPLE_CLAUSE(Write, OMPC_write)
775 CHECK_SIMPLE_CLAUSE(Init, OMPC_init)
776 CHECK_SIMPLE_CLAUSE(Use, OMPC_use)
777 CHECK_SIMPLE_CLAUSE(Novariants, OMPC_novariants)
778 CHECK_SIMPLE_CLAUSE(Nocontext, OMPC_nocontext)
779 CHECK_SIMPLE_CLAUSE(Filter, OMPC_filter)
780 
781 CHECK_REQ_SCALAR_INT_CLAUSE(Allocator, OMPC_allocator)
782 CHECK_REQ_SCALAR_INT_CLAUSE(Grainsize, OMPC_grainsize)
783 CHECK_REQ_SCALAR_INT_CLAUSE(NumTasks, OMPC_num_tasks)
784 CHECK_REQ_SCALAR_INT_CLAUSE(NumTeams, OMPC_num_teams)
785 CHECK_REQ_SCALAR_INT_CLAUSE(NumThreads, OMPC_num_threads)
786 CHECK_REQ_SCALAR_INT_CLAUSE(Priority, OMPC_priority)
787 CHECK_REQ_SCALAR_INT_CLAUSE(ThreadLimit, OMPC_thread_limit)
788 
789 CHECK_REQ_CONSTANT_SCALAR_INT_CLAUSE(Collapse, OMPC_collapse)
790 CHECK_REQ_CONSTANT_SCALAR_INT_CLAUSE(Safelen, OMPC_safelen)
791 CHECK_REQ_CONSTANT_SCALAR_INT_CLAUSE(Simdlen, OMPC_simdlen)
792 
793 // Restrictions specific to each clause are implemented apart from the
794 // generalized restrictions.
795 void OmpStructureChecker::Enter(const parser::OmpClause::Reduction &x) {
796   CheckAllowed(llvm::omp::Clause::OMPC_reduction);
797   if (CheckReductionOperators(x)) {
798     CheckReductionTypeList(x);
799   }
800 }
801 bool OmpStructureChecker::CheckReductionOperators(
802     const parser::OmpClause::Reduction &x) {
803 
804   const auto &definedOp{std::get<0>(x.v.t)};
805   bool ok = false;
806   std::visit(
807       common::visitors{
808           [&](const parser::DefinedOperator &dOpr) {
809             const auto &intrinsicOp{
810                 std::get<parser::DefinedOperator::IntrinsicOperator>(dOpr.u)};
811             ok = CheckIntrinsicOperator(intrinsicOp);
812           },
813           [&](const parser::ProcedureDesignator &procD) {
814             const parser::Name *name{std::get_if<parser::Name>(&procD.u)};
815             if (name) {
816               if (name->source == "max" || name->source == "min" ||
817                   name->source == "iand" || name->source == "ior" ||
818                   name->source == "ieor") {
819                 ok = true;
820               } else {
821                 context_.Say(GetContext().clauseSource,
822                     "Invalid reduction identifier in REDUCTION clause."_err_en_US,
823                     ContextDirectiveAsFortran());
824               }
825             }
826           },
827       },
828       definedOp.u);
829 
830   return ok;
831 }
832 bool OmpStructureChecker::CheckIntrinsicOperator(
833     const parser::DefinedOperator::IntrinsicOperator &op) {
834 
835   switch (op) {
836   case parser::DefinedOperator::IntrinsicOperator::Add:
837   case parser::DefinedOperator::IntrinsicOperator::Subtract:
838   case parser::DefinedOperator::IntrinsicOperator::Multiply:
839   case parser::DefinedOperator::IntrinsicOperator::AND:
840   case parser::DefinedOperator::IntrinsicOperator::OR:
841   case parser::DefinedOperator::IntrinsicOperator::EQV:
842   case parser::DefinedOperator::IntrinsicOperator::NEQV:
843     return true;
844   default:
845     context_.Say(GetContext().clauseSource,
846         "Invalid reduction operator in REDUCTION clause."_err_en_US,
847         ContextDirectiveAsFortran());
848   }
849   return false;
850 }
851 
852 void OmpStructureChecker::CheckReductionTypeList(
853     const parser::OmpClause::Reduction &x) {
854   const auto &ompObjectList{std::get<parser::OmpObjectList>(x.v.t)};
855   CheckIntentInPointerAndDefinable(
856       ompObjectList, llvm::omp::Clause::OMPC_reduction);
857   CheckReductionArraySection(ompObjectList);
858   CheckMultipleAppearanceAcrossContext(ompObjectList);
859 }
860 
861 void OmpStructureChecker::CheckIntentInPointerAndDefinable(
862     const parser::OmpObjectList &objectList, const llvm::omp::Clause clause) {
863   for (const auto &ompObject : objectList.v) {
864     if (const auto *name{parser::Unwrap<parser::Name>(ompObject)}) {
865       if (const auto *symbol{name->symbol}) {
866         if (IsPointer(symbol->GetUltimate()) &&
867             IsIntentIn(symbol->GetUltimate())) {
868           context_.Say(GetContext().clauseSource,
869               "Pointer '%s' with the INTENT(IN) attribute may not appear "
870               "in a %s clause"_err_en_US,
871               symbol->name(),
872               parser::ToUpperCaseLetters(getClauseName(clause).str()));
873         }
874         if (auto msg{
875                 WhyNotModifiable(*symbol, context_.FindScope(name->source))}) {
876           context_.Say(GetContext().clauseSource,
877               "Variable '%s' on the %s clause is not definable"_err_en_US,
878               symbol->name(),
879               parser::ToUpperCaseLetters(getClauseName(clause).str()));
880         }
881       }
882     }
883   }
884 }
885 
886 void OmpStructureChecker::CheckReductionArraySection(
887     const parser::OmpObjectList &ompObjectList) {
888   for (const auto &ompObject : ompObjectList.v) {
889     if (const auto *dataRef{parser::Unwrap<parser::DataRef>(ompObject)}) {
890       if (const auto *arrayElement{
891               parser::Unwrap<parser::ArrayElement>(ompObject)}) {
892         if (arrayElement) {
893           CheckArraySection(*arrayElement, GetLastName(*dataRef),
894               llvm::omp::Clause::OMPC_reduction);
895         }
896       }
897     }
898   }
899 }
900 
901 void OmpStructureChecker::CheckMultipleAppearanceAcrossContext(
902     const parser::OmpObjectList &redObjectList) {
903   //  TODO: Verify the assumption here that the immediately enclosing region is
904   //  the parallel region to which the worksharing construct having reduction
905   //  binds to.
906   if (auto *enclosingContext{GetEnclosingDirContext()}) {
907     for (auto it : enclosingContext->clauseInfo) {
908       llvmOmpClause type = it.first;
909       const auto *clause = it.second;
910       if (llvm::omp::privateReductionSet.test(type)) {
911         if (const auto *objList{GetOmpObjectList(*clause)}) {
912           for (const auto &ompObject : objList->v) {
913             if (const auto *name{parser::Unwrap<parser::Name>(ompObject)}) {
914               if (const auto *symbol{name->symbol}) {
915                 for (const auto &redOmpObject : redObjectList.v) {
916                   if (const auto *rname{
917                           parser::Unwrap<parser::Name>(redOmpObject)}) {
918                     if (const auto *rsymbol{rname->symbol}) {
919                       if (rsymbol->name() == symbol->name()) {
920                         context_.Say(GetContext().clauseSource,
921                             "%s variable '%s' is %s in outer context must"
922                             " be shared in the parallel regions to which any"
923                             " of the worksharing regions arising from the "
924                             "worksharing"
925                             " construct bind."_err_en_US,
926                             parser::ToUpperCaseLetters(
927                                 getClauseName(llvm::omp::Clause::OMPC_reduction)
928                                     .str()),
929                             symbol->name(),
930                             parser::ToUpperCaseLetters(
931                                 getClauseName(type).str()));
932                       }
933                     }
934                   }
935                 }
936               }
937             }
938           }
939         }
940       }
941     }
942   }
943 }
944 
945 void OmpStructureChecker::Enter(const parser::OmpClause::Ordered &x) {
946   CheckAllowed(llvm::omp::Clause::OMPC_ordered);
947   // the parameter of ordered clause is optional
948   if (const auto &expr{x.v}) {
949     RequiresConstantPositiveParameter(llvm::omp::Clause::OMPC_ordered, *expr);
950     // 2.8.3 Loop SIMD Construct Restriction
951     if (llvm::omp::doSimdSet.test(GetContext().directive)) {
952       context_.Say(GetContext().clauseSource,
953           "No ORDERED clause with a parameter can be specified "
954           "on the %s directive"_err_en_US,
955           ContextDirectiveAsFortran());
956     }
957   }
958 }
959 
960 void OmpStructureChecker::Enter(const parser::OmpClause::Shared &x) {
961   CheckAllowed(llvm::omp::Clause::OMPC_shared);
962   CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v);
963 }
964 void OmpStructureChecker::Enter(const parser::OmpClause::Private &x) {
965   CheckAllowed(llvm::omp::Clause::OMPC_private);
966   CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v);
967   CheckIntentInPointer(x.v, llvm::omp::Clause::OMPC_private);
968 }
969 
970 void OmpStructureChecker::CheckIsVarPartOfAnotherVar(
971     const parser::CharBlock &source, const parser::OmpObjectList &objList) {
972 
973   for (const auto &ompObject : objList.v) {
974     std::visit(
975         common::visitors{
976             [&](const parser::Designator &designator) {
977               if (std::get_if<parser::DataRef>(&designator.u)) {
978                 if ((parser::Unwrap<parser::StructureComponent>(ompObject)) ||
979                     (parser::Unwrap<parser::ArrayElement>(ompObject))) {
980                   context_.Say(source,
981                       "A variable that is part of another variable (as an "
982                       "array or structure element)"
983                       " cannot appear in a PRIVATE or SHARED clause or on the ALLOCATE directive."_err_en_US);
984                 }
985               }
986             },
987             [&](const parser::Name &name) {},
988         },
989         ompObject.u);
990   }
991 }
992 
993 void OmpStructureChecker::Enter(const parser::OmpClause::Firstprivate &x) {
994   CheckAllowed(llvm::omp::Clause::OMPC_firstprivate);
995   CheckIsLoopIvPartOfClause(llvmOmpClause::OMPC_firstprivate, x.v);
996 
997   SymbolSourceMap currSymbols;
998   GetSymbolsInObjectList(x.v, currSymbols);
999 
1000   DirectivesClauseTriple dirClauseTriple;
1001   // Check firstprivate variables in worksharing constructs
1002   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_do,
1003       std::make_pair(
1004           llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet));
1005   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_sections,
1006       std::make_pair(
1007           llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet));
1008   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_single,
1009       std::make_pair(
1010           llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet));
1011   // Check firstprivate variables in distribute construct
1012   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_distribute,
1013       std::make_pair(
1014           llvm::omp::Directive::OMPD_teams, llvm::omp::privateReductionSet));
1015   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_distribute,
1016       std::make_pair(llvm::omp::Directive::OMPD_target_teams,
1017           llvm::omp::privateReductionSet));
1018   // Check firstprivate variables in task and taskloop constructs
1019   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_task,
1020       std::make_pair(llvm::omp::Directive::OMPD_parallel,
1021           OmpClauseSet{llvm::omp::Clause::OMPC_reduction}));
1022   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_taskloop,
1023       std::make_pair(llvm::omp::Directive::OMPD_parallel,
1024           OmpClauseSet{llvm::omp::Clause::OMPC_reduction}));
1025 
1026   CheckPrivateSymbolsInOuterCxt(
1027       currSymbols, dirClauseTriple, llvm::omp::Clause::OMPC_firstprivate);
1028 }
1029 
1030 void OmpStructureChecker::CheckIsLoopIvPartOfClause(
1031     llvmOmpClause clause, const parser::OmpObjectList &ompObjectList) {
1032   for (const auto &ompObject : ompObjectList.v) {
1033     if (const parser::Name * name{parser::Unwrap<parser::Name>(ompObject)}) {
1034       if (name->symbol == GetContext().loopIV) {
1035         context_.Say(name->source,
1036             "DO iteration variable %s is not allowed in %s clause."_err_en_US,
1037             name->ToString(),
1038             parser::ToUpperCaseLetters(getClauseName(clause).str()));
1039       }
1040     }
1041   }
1042 }
1043 // Following clauses have a seperate node in parse-tree.h.
1044 // Atomic-clause
1045 CHECK_SIMPLE_PARSER_CLAUSE(OmpAtomicRead, OMPC_read)
1046 CHECK_SIMPLE_PARSER_CLAUSE(OmpAtomicWrite, OMPC_write)
1047 CHECK_SIMPLE_PARSER_CLAUSE(OmpAtomicUpdate, OMPC_update)
1048 CHECK_SIMPLE_PARSER_CLAUSE(OmpAtomicCapture, OMPC_capture)
1049 
1050 void OmpStructureChecker::Leave(const parser::OmpAtomicRead &) {
1051   CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_read,
1052       {llvm::omp::Clause::OMPC_release, llvm::omp::Clause::OMPC_acq_rel});
1053 }
1054 void OmpStructureChecker::Leave(const parser::OmpAtomicWrite &) {
1055   CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_write,
1056       {llvm::omp::Clause::OMPC_acquire, llvm::omp::Clause::OMPC_acq_rel});
1057 }
1058 void OmpStructureChecker::Leave(const parser::OmpAtomicUpdate &) {
1059   CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_update,
1060       {llvm::omp::Clause::OMPC_acquire, llvm::omp::Clause::OMPC_acq_rel});
1061 }
1062 // OmpAtomic node represents atomic directive without atomic-clause.
1063 // atomic-clause - READ,WRITE,UPDATE,CAPTURE.
1064 void OmpStructureChecker::Leave(const parser::OmpAtomic &) {
1065   if (const auto *clause{FindClause(llvm::omp::Clause::OMPC_acquire)}) {
1066     context_.Say(clause->source,
1067         "Clause ACQUIRE is not allowed on the ATOMIC directive"_err_en_US);
1068   }
1069   if (const auto *clause{FindClause(llvm::omp::Clause::OMPC_acq_rel)}) {
1070     context_.Say(clause->source,
1071         "Clause ACQ_REL is not allowed on the ATOMIC directive"_err_en_US);
1072   }
1073 }
1074 // Restrictions specific to each clause are implemented apart from the
1075 // generalized restrictions.
1076 void OmpStructureChecker::Enter(const parser::OmpClause::Aligned &x) {
1077   CheckAllowed(llvm::omp::Clause::OMPC_aligned);
1078 
1079   if (const auto &expr{
1080           std::get<std::optional<parser::ScalarIntConstantExpr>>(x.v.t)}) {
1081     RequiresConstantPositiveParameter(llvm::omp::Clause::OMPC_aligned, *expr);
1082   }
1083   // 2.8.1 TODO: list-item attribute check
1084 }
1085 void OmpStructureChecker::Enter(const parser::OmpClause::Defaultmap &x) {
1086   CheckAllowed(llvm::omp::Clause::OMPC_defaultmap);
1087   using VariableCategory = parser::OmpDefaultmapClause::VariableCategory;
1088   if (!std::get<std::optional<VariableCategory>>(x.v.t)) {
1089     context_.Say(GetContext().clauseSource,
1090         "The argument TOFROM:SCALAR must be specified on the DEFAULTMAP "
1091         "clause"_err_en_US);
1092   }
1093 }
1094 void OmpStructureChecker::Enter(const parser::OmpClause::If &x) {
1095   CheckAllowed(llvm::omp::Clause::OMPC_if);
1096   using dirNameModifier = parser::OmpIfClause::DirectiveNameModifier;
1097   static std::unordered_map<dirNameModifier, OmpDirectiveSet>
1098       dirNameModifierMap{{dirNameModifier::Parallel, llvm::omp::parallelSet},
1099           {dirNameModifier::Target, llvm::omp::targetSet},
1100           {dirNameModifier::TargetEnterData,
1101               {llvm::omp::Directive::OMPD_target_enter_data}},
1102           {dirNameModifier::TargetExitData,
1103               {llvm::omp::Directive::OMPD_target_exit_data}},
1104           {dirNameModifier::TargetData,
1105               {llvm::omp::Directive::OMPD_target_data}},
1106           {dirNameModifier::TargetUpdate,
1107               {llvm::omp::Directive::OMPD_target_update}},
1108           {dirNameModifier::Task, {llvm::omp::Directive::OMPD_task}},
1109           {dirNameModifier::Taskloop, llvm::omp::taskloopSet}};
1110   if (const auto &directiveName{
1111           std::get<std::optional<dirNameModifier>>(x.v.t)}) {
1112     auto search{dirNameModifierMap.find(*directiveName)};
1113     if (search == dirNameModifierMap.end() ||
1114         !search->second.test(GetContext().directive)) {
1115       context_
1116           .Say(GetContext().clauseSource,
1117               "Unmatched directive name modifier %s on the IF clause"_err_en_US,
1118               parser::ToUpperCaseLetters(
1119                   parser::OmpIfClause::EnumToString(*directiveName)))
1120           .Attach(
1121               GetContext().directiveSource, "Cannot apply to directive"_en_US);
1122     }
1123   }
1124 }
1125 
1126 void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) {
1127   CheckAllowed(llvm::omp::Clause::OMPC_linear);
1128 
1129   // 2.7 Loop Construct Restriction
1130   if ((llvm::omp::doSet | llvm::omp::simdSet).test(GetContext().directive)) {
1131     if (std::holds_alternative<parser::OmpLinearClause::WithModifier>(x.v.u)) {
1132       context_.Say(GetContext().clauseSource,
1133           "A modifier may not be specified in a LINEAR clause "
1134           "on the %s directive"_err_en_US,
1135           ContextDirectiveAsFortran());
1136     }
1137   }
1138 }
1139 
1140 void OmpStructureChecker::CheckAllowedMapTypes(
1141     const parser::OmpMapType::Type &type,
1142     const std::list<parser::OmpMapType::Type> &allowedMapTypeList) {
1143   const auto found{std::find(
1144       std::begin(allowedMapTypeList), std::end(allowedMapTypeList), type)};
1145   if (found == std::end(allowedMapTypeList)) {
1146     std::string commaSeperatedMapTypes;
1147     llvm::interleave(
1148         allowedMapTypeList.begin(), allowedMapTypeList.end(),
1149         [&](const parser::OmpMapType::Type &mapType) {
1150           commaSeperatedMapTypes.append(parser::ToUpperCaseLetters(
1151               parser::OmpMapType::EnumToString(mapType)));
1152         },
1153         [&] { commaSeperatedMapTypes.append(", "); });
1154     context_.Say(GetContext().clauseSource,
1155         "Only the %s map types are permitted "
1156         "for MAP clauses on the %s directive"_err_en_US,
1157         commaSeperatedMapTypes, ContextDirectiveAsFortran());
1158   }
1159 }
1160 
1161 void OmpStructureChecker::Enter(const parser::OmpClause::Map &x) {
1162   CheckAllowed(llvm::omp::Clause::OMPC_map);
1163 
1164   if (const auto &maptype{std::get<std::optional<parser::OmpMapType>>(x.v.t)}) {
1165     using Type = parser::OmpMapType::Type;
1166     const Type &type{std::get<Type>(maptype->t)};
1167     switch (GetContext().directive) {
1168     case llvm::omp::Directive::OMPD_target:
1169     case llvm::omp::Directive::OMPD_target_teams:
1170     case llvm::omp::Directive::OMPD_target_teams_distribute:
1171     case llvm::omp::Directive::OMPD_target_teams_distribute_simd:
1172     case llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do:
1173     case llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do_simd:
1174     case llvm::omp::Directive::OMPD_target_data:
1175       CheckAllowedMapTypes(
1176           type, {Type::To, Type::From, Type::Tofrom, Type::Alloc});
1177       break;
1178     case llvm::omp::Directive::OMPD_target_enter_data:
1179       CheckAllowedMapTypes(type, {Type::To, Type::Alloc});
1180       break;
1181     case llvm::omp::Directive::OMPD_target_exit_data:
1182       CheckAllowedMapTypes(type, {Type::From, Type::Release, Type::Delete});
1183       break;
1184     default:
1185       break;
1186     }
1187   }
1188 }
1189 
1190 bool OmpStructureChecker::ScheduleModifierHasType(
1191     const parser::OmpScheduleClause &x,
1192     const parser::OmpScheduleModifierType::ModType &type) {
1193   const auto &modifier{
1194       std::get<std::optional<parser::OmpScheduleModifier>>(x.t)};
1195   if (modifier) {
1196     const auto &modType1{
1197         std::get<parser::OmpScheduleModifier::Modifier1>(modifier->t)};
1198     const auto &modType2{
1199         std::get<std::optional<parser::OmpScheduleModifier::Modifier2>>(
1200             modifier->t)};
1201     if (modType1.v.v == type || (modType2 && modType2->v.v == type)) {
1202       return true;
1203     }
1204   }
1205   return false;
1206 }
1207 void OmpStructureChecker::Enter(const parser::OmpClause::Schedule &x) {
1208   CheckAllowed(llvm::omp::Clause::OMPC_schedule);
1209   const parser::OmpScheduleClause &scheduleClause = x.v;
1210 
1211   // 2.7 Loop Construct Restriction
1212   if (llvm::omp::doSet.test(GetContext().directive)) {
1213     const auto &kind{std::get<1>(scheduleClause.t)};
1214     const auto &chunk{std::get<2>(scheduleClause.t)};
1215     if (chunk) {
1216       if (kind == parser::OmpScheduleClause::ScheduleType::Runtime ||
1217           kind == parser::OmpScheduleClause::ScheduleType::Auto) {
1218         context_.Say(GetContext().clauseSource,
1219             "When SCHEDULE clause has %s specified, "
1220             "it must not have chunk size specified"_err_en_US,
1221             parser::ToUpperCaseLetters(
1222                 parser::OmpScheduleClause::EnumToString(kind)));
1223       }
1224       if (const auto &chunkExpr{std::get<std::optional<parser::ScalarIntExpr>>(
1225               scheduleClause.t)}) {
1226         RequiresPositiveParameter(
1227             llvm::omp::Clause::OMPC_schedule, *chunkExpr, "chunk size");
1228       }
1229     }
1230 
1231     if (ScheduleModifierHasType(scheduleClause,
1232             parser::OmpScheduleModifierType::ModType::Nonmonotonic)) {
1233       if (kind != parser::OmpScheduleClause::ScheduleType::Dynamic &&
1234           kind != parser::OmpScheduleClause::ScheduleType::Guided) {
1235         context_.Say(GetContext().clauseSource,
1236             "The NONMONOTONIC modifier can only be specified with "
1237             "SCHEDULE(DYNAMIC) or SCHEDULE(GUIDED)"_err_en_US);
1238       }
1239     }
1240   }
1241 }
1242 
1243 void OmpStructureChecker::Enter(const parser::OmpClause::Depend &x) {
1244   CheckAllowed(llvm::omp::Clause::OMPC_depend);
1245   if (const auto *inOut{std::get_if<parser::OmpDependClause::InOut>(&x.v.u)}) {
1246     const auto &designators{std::get<std::list<parser::Designator>>(inOut->t)};
1247     for (const auto &ele : designators) {
1248       if (const auto *dataRef{std::get_if<parser::DataRef>(&ele.u)}) {
1249         CheckDependList(*dataRef);
1250         if (const auto *arr{
1251                 std::get_if<common::Indirection<parser::ArrayElement>>(
1252                     &dataRef->u)}) {
1253           CheckArraySection(arr->value(), GetLastName(*dataRef),
1254               llvm::omp::Clause::OMPC_depend);
1255         }
1256       }
1257     }
1258   }
1259 }
1260 
1261 void OmpStructureChecker::Enter(const parser::OmpClause::Copyprivate &x) {
1262   CheckAllowed(llvm::omp::Clause::OMPC_copyprivate);
1263   CheckIntentInPointer(x.v, llvm::omp::Clause::OMPC_copyprivate);
1264 }
1265 
1266 void OmpStructureChecker::Enter(const parser::OmpClause::Lastprivate &x) {
1267   CheckAllowed(llvm::omp::Clause::OMPC_lastprivate);
1268 
1269   DirectivesClauseTriple dirClauseTriple;
1270   SymbolSourceMap currSymbols;
1271   GetSymbolsInObjectList(x.v, currSymbols);
1272   CheckDefinableObjects(currSymbols, GetClauseKindForParserClass(x));
1273 
1274   // Check lastprivate variables in worksharing constructs
1275   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_do,
1276       std::make_pair(
1277           llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet));
1278   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_sections,
1279       std::make_pair(
1280           llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet));
1281 
1282   CheckPrivateSymbolsInOuterCxt(
1283       currSymbols, dirClauseTriple, GetClauseKindForParserClass(x));
1284 }
1285 
1286 llvm::StringRef OmpStructureChecker::getClauseName(llvm::omp::Clause clause) {
1287   return llvm::omp::getOpenMPClauseName(clause);
1288 }
1289 
1290 llvm::StringRef OmpStructureChecker::getDirectiveName(
1291     llvm::omp::Directive directive) {
1292   return llvm::omp::getOpenMPDirectiveName(directive);
1293 }
1294 
1295 void OmpStructureChecker::CheckDependList(const parser::DataRef &d) {
1296   std::visit(
1297       common::visitors{
1298           [&](const common::Indirection<parser::ArrayElement> &elem) {
1299             // Check if the base element is valid on Depend Clause
1300             CheckDependList(elem.value().base);
1301           },
1302           [&](const common::Indirection<parser::StructureComponent> &) {
1303             context_.Say(GetContext().clauseSource,
1304                 "A variable that is part of another variable "
1305                 "(such as an element of a structure) but is not an array "
1306                 "element or an array section cannot appear in a DEPEND "
1307                 "clause"_err_en_US);
1308           },
1309           [&](const common::Indirection<parser::CoindexedNamedObject> &) {
1310             context_.Say(GetContext().clauseSource,
1311                 "Coarrays are not supported in DEPEND clause"_err_en_US);
1312           },
1313           [&](const parser::Name &) { return; },
1314       },
1315       d.u);
1316 }
1317 
1318 // Called from both Reduction and Depend clause.
1319 void OmpStructureChecker::CheckArraySection(
1320     const parser::ArrayElement &arrayElement, const parser::Name &name,
1321     const llvm::omp::Clause clause) {
1322   if (!arrayElement.subscripts.empty()) {
1323     for (const auto &subscript : arrayElement.subscripts) {
1324       if (const auto *triplet{
1325               std::get_if<parser::SubscriptTriplet>(&subscript.u)}) {
1326         if (std::get<0>(triplet->t) && std::get<1>(triplet->t)) {
1327           const auto &lower{std::get<0>(triplet->t)};
1328           const auto &upper{std::get<1>(triplet->t)};
1329           if (lower && upper) {
1330             const auto lval{GetIntValue(lower)};
1331             const auto uval{GetIntValue(upper)};
1332             if (lval && uval && *uval < *lval) {
1333               context_.Say(GetContext().clauseSource,
1334                   "'%s' in %s clause"
1335                   " is a zero size array section"_err_en_US,
1336                   name.ToString(),
1337                   parser::ToUpperCaseLetters(getClauseName(clause).str()));
1338               break;
1339             } else if (std::get<2>(triplet->t)) {
1340               const auto &strideExpr{std::get<2>(triplet->t)};
1341               if (strideExpr) {
1342                 if (clause == llvm::omp::Clause::OMPC_depend) {
1343                   context_.Say(GetContext().clauseSource,
1344                       "Stride should not be specified for array section in "
1345                       "DEPEND "
1346                       "clause"_err_en_US);
1347                 }
1348                 const auto stride{GetIntValue(strideExpr)};
1349                 if ((stride && stride != 1)) {
1350                   context_.Say(GetContext().clauseSource,
1351                       "A list item that appears in a REDUCTION clause"
1352                       " should have a contiguous storage array section."_err_en_US,
1353                       ContextDirectiveAsFortran());
1354                   break;
1355                 }
1356               }
1357             }
1358           }
1359         }
1360       }
1361     }
1362   }
1363 }
1364 
1365 void OmpStructureChecker::CheckIntentInPointer(
1366     const parser::OmpObjectList &objectList, const llvm::omp::Clause clause) {
1367   SymbolSourceMap symbols;
1368   GetSymbolsInObjectList(objectList, symbols);
1369   for (auto it{symbols.begin()}; it != symbols.end(); ++it) {
1370     const auto *symbol{it->first};
1371     const auto source{it->second};
1372     if (IsPointer(*symbol) && IsIntentIn(*symbol)) {
1373       context_.Say(source,
1374           "Pointer '%s' with the INTENT(IN) attribute may not appear "
1375           "in a %s clause"_err_en_US,
1376           symbol->name(),
1377           parser::ToUpperCaseLetters(getClauseName(clause).str()));
1378     }
1379   }
1380 }
1381 
1382 void OmpStructureChecker::GetSymbolsInObjectList(
1383     const parser::OmpObjectList &objectList, SymbolSourceMap &symbols) {
1384   for (const auto &ompObject : objectList.v) {
1385     if (const auto *name{parser::Unwrap<parser::Name>(ompObject)}) {
1386       if (const auto *symbol{name->symbol}) {
1387         if (const auto *commonBlockDetails{
1388                 symbol->detailsIf<CommonBlockDetails>()}) {
1389           for (const auto &object : commonBlockDetails->objects()) {
1390             symbols.emplace(&object->GetUltimate(), name->source);
1391           }
1392         } else {
1393           symbols.emplace(&symbol->GetUltimate(), name->source);
1394         }
1395       }
1396     }
1397   }
1398 }
1399 
1400 void OmpStructureChecker::CheckDefinableObjects(
1401     SymbolSourceMap &symbols, const llvm::omp::Clause clause) {
1402   for (auto it{symbols.begin()}; it != symbols.end(); ++it) {
1403     const auto *symbol{it->first};
1404     const auto source{it->second};
1405     if (auto msg{WhyNotModifiable(*symbol, context_.FindScope(source))}) {
1406       context_
1407           .Say(source,
1408               "Variable '%s' on the %s clause is not definable"_err_en_US,
1409               symbol->name(),
1410               parser::ToUpperCaseLetters(getClauseName(clause).str()))
1411           .Attach(source, std::move(*msg), symbol->name());
1412     }
1413   }
1414 }
1415 
1416 void OmpStructureChecker::CheckPrivateSymbolsInOuterCxt(
1417     SymbolSourceMap &currSymbols, DirectivesClauseTriple &dirClauseTriple,
1418     const llvm::omp::Clause currClause) {
1419   SymbolSourceMap enclosingSymbols;
1420   auto range{dirClauseTriple.equal_range(GetContext().directive)};
1421   for (auto dirIter{range.first}; dirIter != range.second; ++dirIter) {
1422     auto enclosingDir{dirIter->second.first};
1423     auto enclosingClauseSet{dirIter->second.second};
1424     if (auto *enclosingContext{GetEnclosingContextWithDir(enclosingDir)}) {
1425       for (auto it{enclosingContext->clauseInfo.begin()};
1426            it != enclosingContext->clauseInfo.end(); ++it) {
1427         if (enclosingClauseSet.test(it->first)) {
1428           if (const auto *ompObjectList{GetOmpObjectList(*it->second)}) {
1429             GetSymbolsInObjectList(*ompObjectList, enclosingSymbols);
1430           }
1431         }
1432       }
1433 
1434       // Check if the symbols in current context are private in outer context
1435       for (auto iter{currSymbols.begin()}; iter != currSymbols.end(); ++iter) {
1436         const auto *symbol{iter->first};
1437         const auto source{iter->second};
1438         if (enclosingSymbols.find(symbol) != enclosingSymbols.end()) {
1439           context_.Say(source,
1440               "%s variable '%s' is PRIVATE in outer context"_err_en_US,
1441               parser::ToUpperCaseLetters(getClauseName(currClause).str()),
1442               symbol->name());
1443         }
1444       }
1445     }
1446   }
1447 }
1448 
1449 void OmpStructureChecker::CheckWorkshareBlockStmts(
1450     const parser::Block &block, parser::CharBlock source) {
1451   OmpWorkshareBlockChecker ompWorkshareBlockChecker{context_, source};
1452 
1453   for (auto it{block.begin()}; it != block.end(); ++it) {
1454     if (parser::Unwrap<parser::AssignmentStmt>(*it) ||
1455         parser::Unwrap<parser::ForallStmt>(*it) ||
1456         parser::Unwrap<parser::ForallConstruct>(*it) ||
1457         parser::Unwrap<parser::WhereStmt>(*it) ||
1458         parser::Unwrap<parser::WhereConstruct>(*it)) {
1459       parser::Walk(*it, ompWorkshareBlockChecker);
1460     } else if (const auto *ompConstruct{
1461                    parser::Unwrap<parser::OpenMPConstruct>(*it)}) {
1462       if (const auto *ompAtomicConstruct{
1463               std::get_if<parser::OpenMPAtomicConstruct>(&ompConstruct->u)}) {
1464         // Check if assignment statements in the enclosing OpenMP Atomic
1465         // construct are allowed in the Workshare construct
1466         parser::Walk(*ompAtomicConstruct, ompWorkshareBlockChecker);
1467       } else if (const auto *ompCriticalConstruct{
1468                      std::get_if<parser::OpenMPCriticalConstruct>(
1469                          &ompConstruct->u)}) {
1470         // All the restrictions on the Workshare construct apply to the
1471         // statements in the enclosing critical constructs
1472         const auto &criticalBlock{
1473             std::get<parser::Block>(ompCriticalConstruct->t)};
1474         CheckWorkshareBlockStmts(criticalBlock, source);
1475       } else {
1476         // Check if OpenMP constructs enclosed in the Workshare construct are
1477         // 'Parallel' constructs
1478         auto currentDir{llvm::omp::Directive::OMPD_unknown};
1479         const OmpDirectiveSet parallelDirSet{
1480             llvm::omp::Directive::OMPD_parallel,
1481             llvm::omp::Directive::OMPD_parallel_do,
1482             llvm::omp::Directive::OMPD_parallel_sections,
1483             llvm::omp::Directive::OMPD_parallel_workshare,
1484             llvm::omp::Directive::OMPD_parallel_do_simd};
1485 
1486         if (const auto *ompBlockConstruct{
1487                 std::get_if<parser::OpenMPBlockConstruct>(&ompConstruct->u)}) {
1488           const auto &beginBlockDir{
1489               std::get<parser::OmpBeginBlockDirective>(ompBlockConstruct->t)};
1490           const auto &beginDir{
1491               std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
1492           currentDir = beginDir.v;
1493         } else if (const auto *ompLoopConstruct{
1494                        std::get_if<parser::OpenMPLoopConstruct>(
1495                            &ompConstruct->u)}) {
1496           const auto &beginLoopDir{
1497               std::get<parser::OmpBeginLoopDirective>(ompLoopConstruct->t)};
1498           const auto &beginDir{
1499               std::get<parser::OmpLoopDirective>(beginLoopDir.t)};
1500           currentDir = beginDir.v;
1501         } else if (const auto *ompSectionsConstruct{
1502                        std::get_if<parser::OpenMPSectionsConstruct>(
1503                            &ompConstruct->u)}) {
1504           const auto &beginSectionsDir{
1505               std::get<parser::OmpBeginSectionsDirective>(
1506                   ompSectionsConstruct->t)};
1507           const auto &beginDir{
1508               std::get<parser::OmpSectionsDirective>(beginSectionsDir.t)};
1509           currentDir = beginDir.v;
1510         }
1511 
1512         if (!parallelDirSet.test(currentDir)) {
1513           context_.Say(source,
1514               "OpenMP constructs enclosed in WORKSHARE construct may consist "
1515               "of ATOMIC, CRITICAL or PARALLEL constructs only"_err_en_US);
1516         }
1517       }
1518     } else {
1519       context_.Say(source,
1520           "The structured block in a WORKSHARE construct may consist of only "
1521           "SCALAR or ARRAY assignments, FORALL or WHERE statements, "
1522           "FORALL, WHERE, ATOMIC, CRITICAL or PARALLEL constructs"_err_en_US);
1523     }
1524   }
1525 }
1526 
1527 const parser::OmpObjectList *OmpStructureChecker::GetOmpObjectList(
1528     const parser::OmpClause &clause) {
1529 
1530   // Clauses with OmpObjectList as its data member
1531   using MemberObjectListClauses = std::tuple<parser::OmpClause::Copyprivate,
1532       parser::OmpClause::Copyin, parser::OmpClause::Firstprivate,
1533       parser::OmpClause::From, parser::OmpClause::Lastprivate,
1534       parser::OmpClause::Link, parser::OmpClause::Private,
1535       parser::OmpClause::Shared, parser::OmpClause::To>;
1536 
1537   // Clauses with OmpObjectList in the tuple
1538   using TupleObjectListClauses = std::tuple<parser::OmpClause::Allocate,
1539       parser::OmpClause::Map, parser::OmpClause::Reduction>;
1540 
1541   // TODO:: Generate the tuples using TableGen.
1542   // Handle other constructs with OmpObjectList such as OpenMPThreadprivate.
1543   return std::visit(
1544       common::visitors{
1545           [&](const auto &x) -> const parser::OmpObjectList * {
1546             using Ty = std::decay_t<decltype(x)>;
1547             if constexpr (common::HasMember<Ty, MemberObjectListClauses>) {
1548               return &x.v;
1549             } else if constexpr (common::HasMember<Ty,
1550                                      TupleObjectListClauses>) {
1551               return &(std::get<parser::OmpObjectList>(x.v.t));
1552             } else {
1553               return nullptr;
1554             }
1555           },
1556       },
1557       clause.u);
1558 }
1559 
1560 } // namespace Fortran::semantics
1561