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::HasInvalidDistributeNesting(
186     const parser::OpenMPLoopConstruct &x) {
187   bool violation{false};
188 
189   OmpDirectiveSet distributeSet{llvm::omp::Directive::OMPD_distribute,
190       llvm::omp::Directive::OMPD_distribute_parallel_do,
191       llvm::omp::Directive::OMPD_distribute_parallel_do_simd,
192       llvm::omp::Directive::OMPD_distribute_parallel_for,
193       llvm::omp::Directive::OMPD_distribute_parallel_for_simd,
194       llvm::omp::Directive::OMPD_distribute_simd};
195 
196   const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)};
197   const auto &beginDir{std::get<parser::OmpLoopDirective>(beginLoopDir.t)};
198   if (distributeSet.test(beginDir.v)) {
199     // `distribute` region has to be nested
200     if (!CurrentDirectiveIsNested()) {
201       violation = true;
202     } else {
203       // `distribute` region has to be strictly nested inside `teams`
204       if (!llvm::omp::teamSet.test(GetContextParent().directive)) {
205         violation = true;
206       }
207     }
208   }
209   if (violation) {
210     context_.Say(beginDir.source,
211         "`DISTRIBUTE` region has to be strictly nested inside `TEAMS` region."_err_en_US);
212   }
213 }
214 
215 void OmpStructureChecker::HasInvalidTeamsNesting(
216     const llvm::omp::Directive &dir, const parser::CharBlock &source) {
217   OmpDirectiveSet allowedSet{llvm::omp::Directive::OMPD_parallel,
218       llvm::omp::Directive::OMPD_parallel_do,
219       llvm::omp::Directive::OMPD_parallel_do_simd,
220       llvm::omp::Directive::OMPD_parallel_for,
221       llvm::omp::Directive::OMPD_parallel_for_simd,
222       llvm::omp::Directive::OMPD_parallel_master,
223       llvm::omp::Directive::OMPD_parallel_master_taskloop,
224       llvm::omp::Directive::OMPD_parallel_master_taskloop_simd,
225       llvm::omp::Directive::OMPD_parallel_sections,
226       llvm::omp::Directive::OMPD_parallel_workshare,
227       llvm::omp::Directive::OMPD_distribute,
228       llvm::omp::Directive::OMPD_distribute_parallel_do,
229       llvm::omp::Directive::OMPD_distribute_parallel_do_simd,
230       llvm::omp::Directive::OMPD_distribute_parallel_for,
231       llvm::omp::Directive::OMPD_distribute_parallel_for_simd,
232       llvm::omp::Directive::OMPD_distribute_simd};
233 
234   if (!allowedSet.test(dir)) {
235     context_.Say(source,
236         "Only `DISTRIBUTE` or `PARALLEL` regions are allowed to be strictly nested inside `TEAMS` region."_err_en_US);
237   }
238 }
239 
240 void OmpStructureChecker::CheckPredefinedAllocatorRestriction(
241     const parser::CharBlock &source, const parser::Name &name) {
242   if (const auto *symbol{name.symbol}) {
243     const auto *commonBlock{FindCommonBlockContaining(*symbol)};
244     const auto &scope{context_.FindScope(symbol->name())};
245     const Scope &containingScope{GetProgramUnitContaining(scope)};
246     if (!isPredefinedAllocator &&
247         (IsSave(*symbol) || commonBlock ||
248             containingScope.kind() == Scope::Kind::Module)) {
249       context_.Say(source,
250           "If list items within the ALLOCATE directive have the "
251           "SAVE attribute, are a common block name, or are "
252           "declared in the scope of a module, then only "
253           "predefined memory allocator parameters can be used "
254           "in the allocator clause"_err_en_US);
255     }
256   }
257 }
258 
259 void OmpStructureChecker::CheckPredefinedAllocatorRestriction(
260     const parser::CharBlock &source,
261     const parser::OmpObjectList &ompObjectList) {
262   for (const auto &ompObject : ompObjectList.v) {
263     std::visit(
264         common::visitors{
265             [&](const parser::Designator &designator) {
266               if (const auto *dataRef{
267                       std::get_if<parser::DataRef>(&designator.u)}) {
268                 if (const auto *name{std::get_if<parser::Name>(&dataRef->u)}) {
269                   CheckPredefinedAllocatorRestriction(source, *name);
270                 }
271               }
272             },
273             [&](const parser::Name &name) {
274               CheckPredefinedAllocatorRestriction(source, name);
275             },
276         },
277         ompObject.u);
278   }
279 }
280 
281 void OmpStructureChecker::Enter(const parser::OpenMPConstruct &x) {
282   // Simd Construct with Ordered Construct Nesting check
283   // We cannot use CurrentDirectiveIsNested() here because
284   // PushContextAndClauseSets() has not been called yet, it is
285   // called individually for each construct.  Therefore a
286   // dirContext_ size `1` means the current construct is nested
287   if (dirContext_.size() >= 1) {
288     if (GetDirectiveNest(SIMDNest) > 0) {
289       CheckSIMDNest(x);
290     }
291   }
292 }
293 
294 void OmpStructureChecker::Enter(const parser::OpenMPLoopConstruct &x) {
295   const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)};
296   const auto &beginDir{std::get<parser::OmpLoopDirective>(beginLoopDir.t)};
297 
298   // check matching, End directive is optional
299   if (const auto &endLoopDir{
300           std::get<std::optional<parser::OmpEndLoopDirective>>(x.t)}) {
301     const auto &endDir{
302         std::get<parser::OmpLoopDirective>(endLoopDir.value().t)};
303 
304     CheckMatching<parser::OmpLoopDirective>(beginDir, endDir);
305   }
306 
307   PushContextAndClauseSets(beginDir.source, beginDir.v);
308   if (llvm::omp::simdSet.test(GetContext().directive)) {
309     EnterDirectiveNest(SIMDNest);
310   }
311 
312   if (beginDir.v == llvm::omp::Directive::OMPD_do) {
313     // 2.7.1 do-clause -> private-clause |
314     //                    firstprivate-clause |
315     //                    lastprivate-clause |
316     //                    linear-clause |
317     //                    reduction-clause |
318     //                    schedule-clause |
319     //                    collapse-clause |
320     //                    ordered-clause
321 
322     // nesting check
323     HasInvalidWorksharingNesting(
324         beginDir.source, llvm::omp::nestedWorkshareErrSet);
325   }
326   SetLoopInfo(x);
327 
328   if (const auto &doConstruct{
329           std::get<std::optional<parser::DoConstruct>>(x.t)}) {
330     const auto &doBlock{std::get<parser::Block>(doConstruct->t)};
331     CheckNoBranching(doBlock, beginDir.v, beginDir.source);
332   }
333   CheckDoWhile(x);
334   CheckLoopItrVariableIsInt(x);
335   CheckCycleConstraints(x);
336   HasInvalidDistributeNesting(x);
337   if (CurrentDirectiveIsNested() &&
338       llvm::omp::teamSet.test(GetContextParent().directive)) {
339     HasInvalidTeamsNesting(beginDir.v, beginDir.source);
340   }
341   if ((beginDir.v == llvm::omp::Directive::OMPD_distribute_parallel_do_simd) ||
342       (beginDir.v == llvm::omp::Directive::OMPD_distribute_simd)) {
343     CheckDistLinear(x);
344   }
345 }
346 const parser::Name OmpStructureChecker::GetLoopIndex(
347     const parser::DoConstruct *x) {
348   using Bounds = parser::LoopControl::Bounds;
349   return std::get<Bounds>(x->GetLoopControl()->u).name.thing;
350 }
351 void OmpStructureChecker::SetLoopInfo(const parser::OpenMPLoopConstruct &x) {
352   if (const auto &loopConstruct{
353           std::get<std::optional<parser::DoConstruct>>(x.t)}) {
354     const parser::DoConstruct *loop{&*loopConstruct};
355     if (loop && loop->IsDoNormal()) {
356       const parser::Name &itrVal{GetLoopIndex(loop)};
357       SetLoopIv(itrVal.symbol);
358     }
359   }
360 }
361 void OmpStructureChecker::CheckDoWhile(const parser::OpenMPLoopConstruct &x) {
362   const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)};
363   const auto &beginDir{std::get<parser::OmpLoopDirective>(beginLoopDir.t)};
364   if (beginDir.v == llvm::omp::Directive::OMPD_do) {
365     if (const auto &doConstruct{
366             std::get<std::optional<parser::DoConstruct>>(x.t)}) {
367       if (doConstruct.value().IsDoWhile()) {
368         const auto &doStmt{std::get<parser::Statement<parser::NonLabelDoStmt>>(
369             doConstruct.value().t)};
370         context_.Say(doStmt.source,
371             "The DO loop cannot be a DO WHILE with DO directive."_err_en_US);
372       }
373     }
374   }
375 }
376 
377 void OmpStructureChecker::CheckLoopItrVariableIsInt(
378     const parser::OpenMPLoopConstruct &x) {
379   if (const auto &loopConstruct{
380           std::get<std::optional<parser::DoConstruct>>(x.t)}) {
381 
382     for (const parser::DoConstruct *loop{&*loopConstruct}; loop;) {
383       if (loop->IsDoNormal()) {
384         const parser::Name &itrVal{GetLoopIndex(loop)};
385         if (itrVal.symbol) {
386           const auto *type{itrVal.symbol->GetType()};
387           if (!type->IsNumeric(TypeCategory::Integer)) {
388             context_.Say(itrVal.source,
389                 "The DO loop iteration"
390                 " variable must be of the type integer."_err_en_US,
391                 itrVal.ToString());
392           }
393         }
394       }
395       // Get the next DoConstruct if block is not empty.
396       const auto &block{std::get<parser::Block>(loop->t)};
397       const auto it{block.begin()};
398       loop = it != block.end() ? parser::Unwrap<parser::DoConstruct>(*it)
399                                : nullptr;
400     }
401   }
402 }
403 
404 void OmpStructureChecker::CheckSIMDNest(const parser::OpenMPConstruct &c) {
405   // Check the following:
406   //  The only OpenMP constructs that can be encountered during execution of
407   // a simd region are the `atomic` construct, the `loop` construct, the `simd`
408   // construct and the `ordered` construct with the `simd` clause.
409   // TODO:  Expand the check to include `LOOP` construct as well when it is
410   // supported.
411 
412   // Check if the parent context has the SIMD clause
413   // Please note that we use GetContext() instead of GetContextParent()
414   // because PushContextAndClauseSets() has not been called on the
415   // current context yet.
416   // TODO: Check for declare simd regions.
417   bool eligibleSIMD{false};
418   std::visit(Fortran::common::visitors{
419                  // Allow `!$OMP ORDERED SIMD`
420                  [&](const parser::OpenMPBlockConstruct &c) {
421                    const auto &beginBlockDir{
422                        std::get<parser::OmpBeginBlockDirective>(c.t)};
423                    const auto &beginDir{
424                        std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
425                    if (beginDir.v == llvm::omp::Directive::OMPD_ordered) {
426                      const auto &clauses{
427                          std::get<parser::OmpClauseList>(beginBlockDir.t)};
428                      for (const auto &clause : clauses.v) {
429                        if (std::get_if<parser::OmpClause::Simd>(&clause.u)) {
430                          eligibleSIMD = true;
431                          break;
432                        }
433                      }
434                    }
435                  },
436                  [&](const parser::OpenMPSimpleStandaloneConstruct &c) {
437                    const auto &dir{
438                        std::get<parser::OmpSimpleStandaloneDirective>(c.t)};
439                    if (dir.v == llvm::omp::Directive::OMPD_ordered) {
440                      const auto &clauses{std::get<parser::OmpClauseList>(c.t)};
441                      for (const auto &clause : clauses.v) {
442                        if (std::get_if<parser::OmpClause::Simd>(&clause.u)) {
443                          eligibleSIMD = true;
444                          break;
445                        }
446                      }
447                    }
448                  },
449                  // Allowing SIMD construct
450                  [&](const parser::OpenMPLoopConstruct &c) {
451                    const auto &beginLoopDir{
452                        std::get<parser::OmpBeginLoopDirective>(c.t)};
453                    const auto &beginDir{
454                        std::get<parser::OmpLoopDirective>(beginLoopDir.t)};
455                    if ((beginDir.v == llvm::omp::Directive::OMPD_simd) ||
456                        (beginDir.v == llvm::omp::Directive::OMPD_do_simd)) {
457                      eligibleSIMD = true;
458                    }
459                  },
460                  [&](const parser::OpenMPAtomicConstruct &c) {
461                    // Allow `!$OMP ATOMIC`
462                    eligibleSIMD = true;
463                  },
464                  [&](const auto &c) {},
465              },
466       c.u);
467   if (!eligibleSIMD) {
468     context_.Say(parser::FindSourceLocation(c),
469         "The only OpenMP constructs that can be encountered during execution "
470         "of a 'SIMD'"
471         " region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD`"
472         " construct and the `ORDERED` construct with the `SIMD` clause."_err_en_US);
473   }
474 }
475 
476 std::int64_t OmpStructureChecker::GetOrdCollapseLevel(
477     const parser::OpenMPLoopConstruct &x) {
478   const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)};
479   const auto &clauseList{std::get<parser::OmpClauseList>(beginLoopDir.t)};
480   std::int64_t orderedCollapseLevel{1};
481   std::int64_t orderedLevel{0};
482   std::int64_t collapseLevel{0};
483 
484   for (const auto &clause : clauseList.v) {
485     if (const auto *collapseClause{
486             std::get_if<parser::OmpClause::Collapse>(&clause.u)}) {
487       if (const auto v{GetIntValue(collapseClause->v)}) {
488         collapseLevel = *v;
489       }
490     }
491     if (const auto *orderedClause{
492             std::get_if<parser::OmpClause::Ordered>(&clause.u)}) {
493       if (const auto v{GetIntValue(orderedClause->v)}) {
494         orderedLevel = *v;
495       }
496     }
497   }
498   if (orderedLevel >= collapseLevel) {
499     orderedCollapseLevel = orderedLevel;
500   } else {
501     orderedCollapseLevel = collapseLevel;
502   }
503   return orderedCollapseLevel;
504 }
505 
506 void OmpStructureChecker::CheckCycleConstraints(
507     const parser::OpenMPLoopConstruct &x) {
508   std::int64_t ordCollapseLevel{GetOrdCollapseLevel(x)};
509   OmpCycleChecker ompCycleChecker{context_, ordCollapseLevel};
510   parser::Walk(x, ompCycleChecker);
511 }
512 
513 void OmpStructureChecker::CheckDistLinear(
514     const parser::OpenMPLoopConstruct &x) {
515 
516   const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)};
517   const auto &clauses{std::get<parser::OmpClauseList>(beginLoopDir.t)};
518 
519   semantics::UnorderedSymbolSet indexVars;
520 
521   // Collect symbols of all the variables from linear clauses
522   for (const auto &clause : clauses.v) {
523     if (const auto *linearClause{
524             std::get_if<parser::OmpClause::Linear>(&clause.u)}) {
525 
526       std::list<parser::Name> values;
527       // Get the variant type
528       if (std::holds_alternative<parser::OmpLinearClause::WithModifier>(
529               linearClause->v.u)) {
530         const auto &withM{
531             std::get<parser::OmpLinearClause::WithModifier>(linearClause->v.u)};
532         values = withM.names;
533       } else {
534         const auto &withOutM{std::get<parser::OmpLinearClause::WithoutModifier>(
535             linearClause->v.u)};
536         values = withOutM.names;
537       }
538       for (auto const &v : values) {
539         indexVars.insert(*(v.symbol));
540       }
541     }
542   }
543 
544   if (!indexVars.empty()) {
545     // Get collapse level, if given, to find which loops are "associated."
546     std::int64_t collapseVal{GetOrdCollapseLevel(x)};
547     // Include the top loop if no collapse is specified
548     if (collapseVal == 0) {
549       collapseVal = 1;
550     }
551 
552     // Match the loop index variables with the collected symbols from linear
553     // clauses.
554     if (const auto &loopConstruct{
555             std::get<std::optional<parser::DoConstruct>>(x.t)}) {
556       for (const parser::DoConstruct *loop{&*loopConstruct}; loop;) {
557         if (loop->IsDoNormal()) {
558           const parser::Name &itrVal{GetLoopIndex(loop)};
559           if (itrVal.symbol) {
560             // Remove the symbol from the collcted set
561             indexVars.erase(*(itrVal.symbol));
562           }
563           collapseVal--;
564           if (collapseVal == 0) {
565             break;
566           }
567         }
568         // Get the next DoConstruct if block is not empty.
569         const auto &block{std::get<parser::Block>(loop->t)};
570         const auto it{block.begin()};
571         loop = it != block.end() ? parser::Unwrap<parser::DoConstruct>(*it)
572                                  : nullptr;
573       }
574     }
575 
576     // Show error for the remaining variables
577     for (auto var : indexVars) {
578       const Symbol &root{GetAssociationRoot(var)};
579       context_.Say(parser::FindSourceLocation(x),
580           "Variable '%s' not allowed in `LINEAR` clause, only loop iterator can be specified in `LINEAR` clause of a construct combined with `DISTRIBUTE`"_err_en_US,
581           root.name());
582     }
583   }
584 }
585 
586 void OmpStructureChecker::Leave(const parser::OpenMPLoopConstruct &) {
587   if (llvm::omp::simdSet.test(GetContext().directive)) {
588     ExitDirectiveNest(SIMDNest);
589   }
590   dirContext_.pop_back();
591 }
592 
593 void OmpStructureChecker::Enter(const parser::OmpEndLoopDirective &x) {
594   const auto &dir{std::get<parser::OmpLoopDirective>(x.t)};
595   ResetPartialContext(dir.source);
596   switch (dir.v) {
597   // 2.7.1 end-do -> END DO [nowait-clause]
598   // 2.8.3 end-do-simd -> END DO SIMD [nowait-clause]
599   case llvm::omp::Directive::OMPD_do:
600   case llvm::omp::Directive::OMPD_do_simd:
601     SetClauseSets(dir.v);
602     break;
603   default:
604     // no clauses are allowed
605     break;
606   }
607 }
608 
609 void OmpStructureChecker::Enter(const parser::OpenMPBlockConstruct &x) {
610   const auto &beginBlockDir{std::get<parser::OmpBeginBlockDirective>(x.t)};
611   const auto &endBlockDir{std::get<parser::OmpEndBlockDirective>(x.t)};
612   const auto &beginDir{std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
613   const auto &endDir{std::get<parser::OmpBlockDirective>(endBlockDir.t)};
614   const parser::Block &block{std::get<parser::Block>(x.t)};
615 
616   CheckMatching<parser::OmpBlockDirective>(beginDir, endDir);
617 
618   PushContextAndClauseSets(beginDir.source, beginDir.v);
619 
620   if (CurrentDirectiveIsNested()) {
621     CheckIfDoOrderedClause(beginDir);
622     if (llvm::omp::teamSet.test(GetContextParent().directive)) {
623       HasInvalidTeamsNesting(beginDir.v, beginDir.source);
624     }
625     if (GetContext().directive == llvm::omp::Directive::OMPD_master) {
626       CheckMasterNesting(x);
627     }
628     // A teams region can only be strictly nested within the implicit parallel
629     // region or a target region.
630     if (GetContext().directive == llvm::omp::Directive::OMPD_teams &&
631         GetContextParent().directive != llvm::omp::Directive::OMPD_target) {
632       context_.Say(parser::FindSourceLocation(x),
633           "%s region can only be strictly nested within the implicit parallel "
634           "region or TARGET region"_err_en_US,
635           ContextDirectiveAsFortran());
636     }
637     // If a teams construct is nested within a target construct, that target
638     // construct must contain no statements, declarations or directives outside
639     // of the teams construct.
640     if (GetContext().directive == llvm::omp::Directive::OMPD_teams &&
641         GetContextParent().directive == llvm::omp::Directive::OMPD_target &&
642         !GetDirectiveNest(TargetBlockOnlyTeams)) {
643       context_.Say(GetContextParent().directiveSource,
644           "TARGET construct with nested TEAMS region contains statements or "
645           "directives outside of the TEAMS construct"_err_en_US);
646     }
647   }
648 
649   CheckNoBranching(block, beginDir.v, beginDir.source);
650 
651   switch (beginDir.v) {
652   case llvm::omp::Directive::OMPD_target:
653     if (CheckTargetBlockOnlyTeams(block)) {
654       EnterDirectiveNest(TargetBlockOnlyTeams);
655     }
656     break;
657   case llvm::omp::OMPD_workshare:
658   case llvm::omp::OMPD_parallel_workshare:
659     CheckWorkshareBlockStmts(block, beginDir.source);
660     HasInvalidWorksharingNesting(
661         beginDir.source, llvm::omp::nestedWorkshareErrSet);
662     break;
663   case llvm::omp::Directive::OMPD_single:
664     // TODO: This check needs to be extended while implementing nesting of
665     // regions checks.
666     HasInvalidWorksharingNesting(
667         beginDir.source, llvm::omp::nestedWorkshareErrSet);
668     break;
669   default:
670     break;
671   }
672 }
673 
674 void OmpStructureChecker::CheckMasterNesting(
675     const parser::OpenMPBlockConstruct &x) {
676   // A MASTER region may not be `closely nested` inside a worksharing, loop,
677   // task, taskloop, or atomic region.
678   // TODO:  Expand the check to include `LOOP` construct as well when it is
679   // supported.
680   if (IsCloselyNestedRegion(llvm::omp::nestedMasterErrSet)) {
681     context_.Say(parser::FindSourceLocation(x),
682         "`MASTER` region may not be closely nested inside of `WORKSHARING`, "
683         "`LOOP`, `TASK`, `TASKLOOP`,"
684         " or `ATOMIC` region."_err_en_US);
685   }
686 }
687 
688 void OmpStructureChecker::CheckIfDoOrderedClause(
689     const parser::OmpBlockDirective &blkDirective) {
690   if (blkDirective.v == llvm::omp::OMPD_ordered) {
691     // Loops
692     if (llvm::omp::doSet.test(GetContextParent().directive) &&
693         !FindClauseParent(llvm::omp::Clause::OMPC_ordered)) {
694       context_.Say(blkDirective.source,
695           "The ORDERED clause must be present on the loop"
696           " construct if any ORDERED region ever binds"
697           " to a loop region arising from the loop construct."_err_en_US);
698     }
699     // Other disallowed nestings, these directives do not support
700     // ordered clause in them, so no need to check
701     else if (IsCloselyNestedRegion(llvm::omp::nestedOrderedErrSet)) {
702       context_.Say(blkDirective.source,
703           "`ORDERED` region may not be closely nested inside of "
704           "`CRITICAL`, `ORDERED`, explicit `TASK` or `TASKLOOP` region."_err_en_US);
705     }
706   }
707 }
708 
709 void OmpStructureChecker::Leave(const parser::OpenMPBlockConstruct &) {
710   if (GetDirectiveNest(TargetBlockOnlyTeams)) {
711     ExitDirectiveNest(TargetBlockOnlyTeams);
712   }
713   dirContext_.pop_back();
714 }
715 
716 void OmpStructureChecker::Enter(const parser::OpenMPSectionsConstruct &x) {
717   const auto &beginSectionsDir{
718       std::get<parser::OmpBeginSectionsDirective>(x.t)};
719   const auto &endSectionsDir{std::get<parser::OmpEndSectionsDirective>(x.t)};
720   const auto &beginDir{
721       std::get<parser::OmpSectionsDirective>(beginSectionsDir.t)};
722   const auto &endDir{std::get<parser::OmpSectionsDirective>(endSectionsDir.t)};
723   CheckMatching<parser::OmpSectionsDirective>(beginDir, endDir);
724 
725   PushContextAndClauseSets(beginDir.source, beginDir.v);
726   const auto &sectionBlocks{std::get<parser::OmpSectionBlocks>(x.t)};
727   for (const auto &block : sectionBlocks.v) {
728     CheckNoBranching(block, beginDir.v, beginDir.source);
729   }
730   HasInvalidWorksharingNesting(
731       beginDir.source, llvm::omp::nestedWorkshareErrSet);
732 }
733 
734 void OmpStructureChecker::Leave(const parser::OpenMPSectionsConstruct &) {
735   dirContext_.pop_back();
736 }
737 
738 void OmpStructureChecker::Enter(const parser::OmpEndSectionsDirective &x) {
739   const auto &dir{std::get<parser::OmpSectionsDirective>(x.t)};
740   ResetPartialContext(dir.source);
741   switch (dir.v) {
742     // 2.7.2 end-sections -> END SECTIONS [nowait-clause]
743   case llvm::omp::Directive::OMPD_sections:
744     PushContextAndClauseSets(
745         dir.source, llvm::omp::Directive::OMPD_end_sections);
746     break;
747   default:
748     // no clauses are allowed
749     break;
750   }
751 }
752 
753 // TODO: Verify the popping of dirContext requirement after nowait
754 // implementation, as there is an implicit barrier at the end of the worksharing
755 // constructs unless a nowait clause is specified. Only OMPD_end_sections is
756 // popped becuase it is pushed while entering the EndSectionsDirective.
757 void OmpStructureChecker::Leave(const parser::OmpEndSectionsDirective &x) {
758   if (GetContext().directive == llvm::omp::Directive::OMPD_end_sections) {
759     dirContext_.pop_back();
760   }
761 }
762 
763 void OmpStructureChecker::Enter(const parser::OpenMPDeclareSimdConstruct &x) {
764   const auto &dir{std::get<parser::Verbatim>(x.t)};
765   PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_declare_simd);
766 }
767 
768 void OmpStructureChecker::Leave(const parser::OpenMPDeclareSimdConstruct &) {
769   dirContext_.pop_back();
770 }
771 
772 void OmpStructureChecker::Enter(const parser::OpenMPDeclarativeAllocate &x) {
773   isPredefinedAllocator = true;
774   const auto &dir{std::get<parser::Verbatim>(x.t)};
775   const auto &objectList{std::get<parser::OmpObjectList>(x.t)};
776   PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_allocate);
777   CheckIsVarPartOfAnotherVar(dir.source, objectList);
778 }
779 
780 void OmpStructureChecker::Leave(const parser::OpenMPDeclarativeAllocate &x) {
781   const auto &dir{std::get<parser::Verbatim>(x.t)};
782   const auto &objectList{std::get<parser::OmpObjectList>(x.t)};
783   CheckPredefinedAllocatorRestriction(dir.source, objectList);
784   dirContext_.pop_back();
785 }
786 
787 void OmpStructureChecker::Enter(const parser::OmpClause::Allocator &x) {
788   CheckAllowed(llvm::omp::Clause::OMPC_allocator);
789   // Note: Predefined allocators are stored in ScalarExpr as numbers
790   //   whereas custom allocators are stored as strings, so if the ScalarExpr
791   //   actually has an int value, then it must be a predefined allocator
792   isPredefinedAllocator = GetIntValue(x.v).has_value();
793   RequiresPositiveParameter(llvm::omp::Clause::OMPC_allocator, x.v);
794 }
795 
796 void OmpStructureChecker::Enter(const parser::OpenMPDeclareTargetConstruct &x) {
797   const auto &dir{std::get<parser::Verbatim>(x.t)};
798   PushContext(dir.source, llvm::omp::Directive::OMPD_declare_target);
799   const auto &spec{std::get<parser::OmpDeclareTargetSpecifier>(x.t)};
800   if (std::holds_alternative<parser::OmpDeclareTargetWithClause>(spec.u)) {
801     SetClauseSets(llvm::omp::Directive::OMPD_declare_target);
802   }
803 }
804 
805 void OmpStructureChecker::Leave(const parser::OpenMPDeclareTargetConstruct &) {
806   dirContext_.pop_back();
807 }
808 
809 void OmpStructureChecker::Enter(const parser::OpenMPExecutableAllocate &x) {
810   isPredefinedAllocator = true;
811   const auto &dir{std::get<parser::Verbatim>(x.t)};
812   const auto &objectList{std::get<std::optional<parser::OmpObjectList>>(x.t)};
813   PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_allocate);
814   if (objectList) {
815     CheckIsVarPartOfAnotherVar(dir.source, *objectList);
816   }
817 }
818 
819 void OmpStructureChecker::Leave(const parser::OpenMPExecutableAllocate &x) {
820   const auto &dir{std::get<parser::Verbatim>(x.t)};
821   const auto &objectList{std::get<std::optional<parser::OmpObjectList>>(x.t)};
822   if (objectList)
823     CheckPredefinedAllocatorRestriction(dir.source, *objectList);
824   dirContext_.pop_back();
825 }
826 
827 void OmpStructureChecker::CheckBarrierNesting(
828     const parser::OpenMPSimpleStandaloneConstruct &x) {
829   // A barrier region may not be `closely nested` inside a worksharing, loop,
830   // task, taskloop, critical, ordered, atomic, or master region.
831   // TODO:  Expand the check to include `LOOP` construct as well when it is
832   // supported.
833   if (GetContext().directive == llvm::omp::Directive::OMPD_barrier) {
834     if (IsCloselyNestedRegion(llvm::omp::nestedBarrierErrSet)) {
835       context_.Say(parser::FindSourceLocation(x),
836           "`BARRIER` region may not be closely nested inside of `WORKSHARING`, "
837           "`LOOP`, `TASK`, `TASKLOOP`,"
838           "`CRITICAL`, `ORDERED`, `ATOMIC` or `MASTER` region."_err_en_US);
839     }
840   }
841 }
842 
843 void OmpStructureChecker::Enter(
844     const parser::OpenMPSimpleStandaloneConstruct &x) {
845   const auto &dir{std::get<parser::OmpSimpleStandaloneDirective>(x.t)};
846   PushContextAndClauseSets(dir.source, dir.v);
847   CheckBarrierNesting(x);
848 }
849 
850 void OmpStructureChecker::Leave(
851     const parser::OpenMPSimpleStandaloneConstruct &) {
852   dirContext_.pop_back();
853 }
854 
855 void OmpStructureChecker::Enter(const parser::OpenMPFlushConstruct &x) {
856   const auto &dir{std::get<parser::Verbatim>(x.t)};
857   PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_flush);
858 }
859 
860 void OmpStructureChecker::Leave(const parser::OpenMPFlushConstruct &x) {
861   if (FindClause(llvm::omp::Clause::OMPC_acquire) ||
862       FindClause(llvm::omp::Clause::OMPC_release) ||
863       FindClause(llvm::omp::Clause::OMPC_acq_rel)) {
864     if (const auto &flushList{
865             std::get<std::optional<parser::OmpObjectList>>(x.t)}) {
866       context_.Say(parser::FindSourceLocation(flushList),
867           "If memory-order-clause is RELEASE, ACQUIRE, or ACQ_REL, list items "
868           "must not be specified on the FLUSH directive"_err_en_US);
869     }
870   }
871   dirContext_.pop_back();
872 }
873 
874 void OmpStructureChecker::Enter(const parser::OpenMPCancelConstruct &x) {
875   const auto &dir{std::get<parser::Verbatim>(x.t)};
876   const auto &type{std::get<parser::OmpCancelType>(x.t)};
877   PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_cancel);
878   CheckCancellationNest(dir.source, type.v);
879 }
880 
881 void OmpStructureChecker::Leave(const parser::OpenMPCancelConstruct &) {
882   dirContext_.pop_back();
883 }
884 
885 void OmpStructureChecker::Enter(const parser::OpenMPCriticalConstruct &x) {
886   const auto &dir{std::get<parser::OmpCriticalDirective>(x.t)};
887   PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_critical);
888   const auto &block{std::get<parser::Block>(x.t)};
889   CheckNoBranching(block, llvm::omp::Directive::OMPD_critical, dir.source);
890 }
891 
892 void OmpStructureChecker::Leave(const parser::OpenMPCriticalConstruct &) {
893   dirContext_.pop_back();
894 }
895 
896 void OmpStructureChecker::Enter(
897     const parser::OpenMPCancellationPointConstruct &x) {
898   const auto &dir{std::get<parser::Verbatim>(x.t)};
899   const auto &type{std::get<parser::OmpCancelType>(x.t)};
900   PushContextAndClauseSets(
901       dir.source, llvm::omp::Directive::OMPD_cancellation_point);
902   CheckCancellationNest(dir.source, type.v);
903 }
904 
905 void OmpStructureChecker::Leave(
906     const parser::OpenMPCancellationPointConstruct &) {
907   dirContext_.pop_back();
908 }
909 
910 void OmpStructureChecker::CheckCancellationNest(
911     const parser::CharBlock &source, const parser::OmpCancelType::Type &type) {
912   if (CurrentDirectiveIsNested()) {
913     // If construct-type-clause is taskgroup, the cancellation construct must be
914     // closely nested inside a task or a taskloop construct and the cancellation
915     // region must be closely nested inside a taskgroup region. If
916     // construct-type-clause is sections, the cancellation construct must be
917     // closely nested inside a sections or section construct. Otherwise, the
918     // cancellation construct must be closely nested inside an OpenMP construct
919     // that matches the type specified in construct-type-clause of the
920     // cancellation construct.
921 
922     OmpDirectiveSet allowedTaskgroupSet{
923         llvm::omp::Directive::OMPD_task, llvm::omp::Directive::OMPD_taskloop};
924     OmpDirectiveSet allowedSectionsSet{llvm::omp::Directive::OMPD_sections,
925         llvm::omp::Directive::OMPD_parallel_sections};
926     OmpDirectiveSet allowedDoSet{llvm::omp::Directive::OMPD_do,
927         llvm::omp::Directive::OMPD_distribute_parallel_do,
928         llvm::omp::Directive::OMPD_parallel_do,
929         llvm::omp::Directive::OMPD_target_parallel_do,
930         llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do,
931         llvm::omp::Directive::OMPD_teams_distribute_parallel_do};
932     OmpDirectiveSet allowedParallelSet{llvm::omp::Directive::OMPD_parallel,
933         llvm::omp::Directive::OMPD_target_parallel};
934 
935     bool eligibleCancellation{false};
936     switch (type) {
937     case parser::OmpCancelType::Type::Taskgroup:
938       if (allowedTaskgroupSet.test(GetContextParent().directive)) {
939         eligibleCancellation = true;
940         if (dirContext_.size() >= 3) {
941           // Check if the cancellation region is closely nested inside a
942           // taskgroup region when there are more than two levels of directives
943           // in the directive context stack.
944           if (GetContextParent().directive == llvm::omp::Directive::OMPD_task ||
945               FindClauseParent(llvm::omp::Clause::OMPC_nogroup)) {
946             for (int i = dirContext_.size() - 3; i >= 0; i--) {
947               if (dirContext_[i].directive ==
948                   llvm::omp::Directive::OMPD_taskgroup) {
949                 break;
950               }
951               if (allowedParallelSet.test(dirContext_[i].directive)) {
952                 eligibleCancellation = false;
953                 break;
954               }
955             }
956           }
957         }
958       }
959       if (!eligibleCancellation) {
960         context_.Say(source,
961             "With %s clause, %s construct must be closely nested inside TASK "
962             "or TASKLOOP construct and %s region must be closely nested inside "
963             "TASKGROUP region"_err_en_US,
964             parser::ToUpperCaseLetters(
965                 parser::OmpCancelType::EnumToString(type)),
966             ContextDirectiveAsFortran(), ContextDirectiveAsFortran());
967       }
968       return;
969     case parser::OmpCancelType::Type::Sections:
970       if (allowedSectionsSet.test(GetContextParent().directive)) {
971         eligibleCancellation = true;
972       }
973       break;
974     case Fortran::parser::OmpCancelType::Type::Do:
975       if (allowedDoSet.test(GetContextParent().directive)) {
976         eligibleCancellation = true;
977       }
978       break;
979     case parser::OmpCancelType::Type::Parallel:
980       if (allowedParallelSet.test(GetContextParent().directive)) {
981         eligibleCancellation = true;
982       }
983       break;
984     }
985     if (!eligibleCancellation) {
986       context_.Say(source,
987           "With %s clause, %s construct cannot be closely nested inside %s "
988           "construct"_err_en_US,
989           parser::ToUpperCaseLetters(parser::OmpCancelType::EnumToString(type)),
990           ContextDirectiveAsFortran(),
991           parser::ToUpperCaseLetters(
992               getDirectiveName(GetContextParent().directive).str()));
993     }
994   } else {
995     // The cancellation directive cannot be orphaned.
996     switch (type) {
997     case parser::OmpCancelType::Type::Taskgroup:
998       context_.Say(source,
999           "%s %s directive is not closely nested inside "
1000           "TASK or TASKLOOP"_err_en_US,
1001           ContextDirectiveAsFortran(),
1002           parser::ToUpperCaseLetters(
1003               parser::OmpCancelType::EnumToString(type)));
1004       break;
1005     case parser::OmpCancelType::Type::Sections:
1006       context_.Say(source,
1007           "%s %s directive is not closely nested inside "
1008           "SECTION or SECTIONS"_err_en_US,
1009           ContextDirectiveAsFortran(),
1010           parser::ToUpperCaseLetters(
1011               parser::OmpCancelType::EnumToString(type)));
1012       break;
1013     case Fortran::parser::OmpCancelType::Type::Do:
1014       context_.Say(source,
1015           "%s %s directive is not closely nested inside "
1016           "the construct that matches the DO clause type"_err_en_US,
1017           ContextDirectiveAsFortran(),
1018           parser::ToUpperCaseLetters(
1019               parser::OmpCancelType::EnumToString(type)));
1020       break;
1021     case parser::OmpCancelType::Type::Parallel:
1022       context_.Say(source,
1023           "%s %s directive is not closely nested inside "
1024           "the construct that matches the PARALLEL clause type"_err_en_US,
1025           ContextDirectiveAsFortran(),
1026           parser::ToUpperCaseLetters(
1027               parser::OmpCancelType::EnumToString(type)));
1028       break;
1029     }
1030   }
1031 }
1032 
1033 void OmpStructureChecker::Enter(const parser::OmpEndBlockDirective &x) {
1034   const auto &dir{std::get<parser::OmpBlockDirective>(x.t)};
1035   ResetPartialContext(dir.source);
1036   switch (dir.v) {
1037   // 2.7.3 end-single-clause -> copyprivate-clause |
1038   //                            nowait-clause
1039   case llvm::omp::Directive::OMPD_single:
1040     PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_end_single);
1041     break;
1042   // 2.7.4 end-workshare -> END WORKSHARE [nowait-clause]
1043   case llvm::omp::Directive::OMPD_workshare:
1044     PushContextAndClauseSets(
1045         dir.source, llvm::omp::Directive::OMPD_end_workshare);
1046     break;
1047   default:
1048     // no clauses are allowed
1049     break;
1050   }
1051 }
1052 
1053 // TODO: Verify the popping of dirContext requirement after nowait
1054 // implementation, as there is an implicit barrier at the end of the worksharing
1055 // constructs unless a nowait clause is specified. Only OMPD_end_single and
1056 // end_workshareare popped as they are pushed while entering the
1057 // EndBlockDirective.
1058 void OmpStructureChecker::Leave(const parser::OmpEndBlockDirective &x) {
1059   if ((GetContext().directive == llvm::omp::Directive::OMPD_end_single) ||
1060       (GetContext().directive == llvm::omp::Directive::OMPD_end_workshare)) {
1061     dirContext_.pop_back();
1062   }
1063 }
1064 
1065 void OmpStructureChecker::Enter(const parser::OpenMPAtomicConstruct &x) {
1066   std::visit(
1067       common::visitors{
1068           [&](const auto &someAtomicConstruct) {
1069             const auto &dir{std::get<parser::Verbatim>(someAtomicConstruct.t)};
1070             PushContextAndClauseSets(
1071                 dir.source, llvm::omp::Directive::OMPD_atomic);
1072           },
1073       },
1074       x.u);
1075 }
1076 
1077 void OmpStructureChecker::Leave(const parser::OpenMPAtomicConstruct &) {
1078   dirContext_.pop_back();
1079 }
1080 
1081 // Clauses
1082 // Mainly categorized as
1083 // 1. Checks on 'OmpClauseList' from 'parse-tree.h'.
1084 // 2. Checks on clauses which fall under 'struct OmpClause' from parse-tree.h.
1085 // 3. Checks on clauses which are not in 'struct OmpClause' from parse-tree.h.
1086 
1087 void OmpStructureChecker::Leave(const parser::OmpClauseList &) {
1088   // 2.7.1 Loop Construct Restriction
1089   if (llvm::omp::doSet.test(GetContext().directive)) {
1090     if (auto *clause{FindClause(llvm::omp::Clause::OMPC_schedule)}) {
1091       // only one schedule clause is allowed
1092       const auto &schedClause{std::get<parser::OmpClause::Schedule>(clause->u)};
1093       if (ScheduleModifierHasType(schedClause.v,
1094               parser::OmpScheduleModifierType::ModType::Nonmonotonic)) {
1095         if (FindClause(llvm::omp::Clause::OMPC_ordered)) {
1096           context_.Say(clause->source,
1097               "The NONMONOTONIC modifier cannot be specified "
1098               "if an ORDERED clause is specified"_err_en_US);
1099         }
1100         if (ScheduleModifierHasType(schedClause.v,
1101                 parser::OmpScheduleModifierType::ModType::Monotonic)) {
1102           context_.Say(clause->source,
1103               "The MONOTONIC and NONMONOTONIC modifiers "
1104               "cannot be both specified"_err_en_US);
1105         }
1106       }
1107     }
1108 
1109     if (auto *clause{FindClause(llvm::omp::Clause::OMPC_ordered)}) {
1110       // only one ordered clause is allowed
1111       const auto &orderedClause{
1112           std::get<parser::OmpClause::Ordered>(clause->u)};
1113 
1114       if (orderedClause.v) {
1115         CheckNotAllowedIfClause(
1116             llvm::omp::Clause::OMPC_ordered, {llvm::omp::Clause::OMPC_linear});
1117 
1118         if (auto *clause2{FindClause(llvm::omp::Clause::OMPC_collapse)}) {
1119           const auto &collapseClause{
1120               std::get<parser::OmpClause::Collapse>(clause2->u)};
1121           // ordered and collapse both have parameters
1122           if (const auto orderedValue{GetIntValue(orderedClause.v)}) {
1123             if (const auto collapseValue{GetIntValue(collapseClause.v)}) {
1124               if (*orderedValue > 0 && *orderedValue < *collapseValue) {
1125                 context_.Say(clause->source,
1126                     "The parameter of the ORDERED clause must be "
1127                     "greater than or equal to "
1128                     "the parameter of the COLLAPSE clause"_err_en_US);
1129               }
1130             }
1131           }
1132         }
1133       }
1134 
1135       // TODO: ordered region binding check (requires nesting implementation)
1136     }
1137   } // doSet
1138 
1139   // 2.8.1 Simd Construct Restriction
1140   if (llvm::omp::simdSet.test(GetContext().directive)) {
1141     if (auto *clause{FindClause(llvm::omp::Clause::OMPC_simdlen)}) {
1142       if (auto *clause2{FindClause(llvm::omp::Clause::OMPC_safelen)}) {
1143         const auto &simdlenClause{
1144             std::get<parser::OmpClause::Simdlen>(clause->u)};
1145         const auto &safelenClause{
1146             std::get<parser::OmpClause::Safelen>(clause2->u)};
1147         // simdlen and safelen both have parameters
1148         if (const auto simdlenValue{GetIntValue(simdlenClause.v)}) {
1149           if (const auto safelenValue{GetIntValue(safelenClause.v)}) {
1150             if (*safelenValue > 0 && *simdlenValue > *safelenValue) {
1151               context_.Say(clause->source,
1152                   "The parameter of the SIMDLEN clause must be less than or "
1153                   "equal to the parameter of the SAFELEN clause"_err_en_US);
1154             }
1155           }
1156         }
1157       }
1158     }
1159     // A list-item cannot appear in more than one aligned clause
1160     semantics::UnorderedSymbolSet alignedVars;
1161     auto clauseAll = FindClauses(llvm::omp::Clause::OMPC_aligned);
1162     for (auto itr = clauseAll.first; itr != clauseAll.second; ++itr) {
1163       const auto &alignedClause{
1164           std::get<parser::OmpClause::Aligned>(itr->second->u)};
1165       const auto &alignedNameList{
1166           std::get<std::list<parser::Name>>(alignedClause.v.t)};
1167       for (auto const &var : alignedNameList) {
1168         if (alignedVars.count(*(var.symbol)) == 1) {
1169           context_.Say(itr->second->source,
1170               "List item '%s' present at multiple ALIGNED clauses"_err_en_US,
1171               var.ToString());
1172           break;
1173         }
1174         alignedVars.insert(*(var.symbol));
1175       }
1176     }
1177   } // SIMD
1178 
1179   // 2.7.3 Single Construct Restriction
1180   if (GetContext().directive == llvm::omp::Directive::OMPD_end_single) {
1181     CheckNotAllowedIfClause(
1182         llvm::omp::Clause::OMPC_copyprivate, {llvm::omp::Clause::OMPC_nowait});
1183   }
1184 
1185   CheckRequireAtLeastOneOf();
1186 }
1187 
1188 void OmpStructureChecker::Enter(const parser::OmpClause &x) {
1189   SetContextClause(x);
1190 }
1191 
1192 // Following clauses do not have a separate node in parse-tree.h.
1193 CHECK_SIMPLE_CLAUSE(AcqRel, OMPC_acq_rel)
1194 CHECK_SIMPLE_CLAUSE(Acquire, OMPC_acquire)
1195 CHECK_SIMPLE_CLAUSE(AtomicDefaultMemOrder, OMPC_atomic_default_mem_order)
1196 CHECK_SIMPLE_CLAUSE(Affinity, OMPC_affinity)
1197 CHECK_SIMPLE_CLAUSE(Allocate, OMPC_allocate)
1198 CHECK_SIMPLE_CLAUSE(Capture, OMPC_capture)
1199 CHECK_SIMPLE_CLAUSE(Copyin, OMPC_copyin)
1200 CHECK_SIMPLE_CLAUSE(Default, OMPC_default)
1201 CHECK_SIMPLE_CLAUSE(Depobj, OMPC_depobj)
1202 CHECK_SIMPLE_CLAUSE(Destroy, OMPC_destroy)
1203 CHECK_SIMPLE_CLAUSE(Detach, OMPC_detach)
1204 CHECK_SIMPLE_CLAUSE(Device, OMPC_device)
1205 CHECK_SIMPLE_CLAUSE(DeviceType, OMPC_device_type)
1206 CHECK_SIMPLE_CLAUSE(DistSchedule, OMPC_dist_schedule)
1207 CHECK_SIMPLE_CLAUSE(DynamicAllocators, OMPC_dynamic_allocators)
1208 CHECK_SIMPLE_CLAUSE(Exclusive, OMPC_exclusive)
1209 CHECK_SIMPLE_CLAUSE(Final, OMPC_final)
1210 CHECK_SIMPLE_CLAUSE(Flush, OMPC_flush)
1211 CHECK_SIMPLE_CLAUSE(From, OMPC_from)
1212 CHECK_SIMPLE_CLAUSE(Full, OMPC_full)
1213 CHECK_SIMPLE_CLAUSE(Hint, OMPC_hint)
1214 CHECK_SIMPLE_CLAUSE(InReduction, OMPC_in_reduction)
1215 CHECK_SIMPLE_CLAUSE(Inclusive, OMPC_inclusive)
1216 CHECK_SIMPLE_CLAUSE(Match, OMPC_match)
1217 CHECK_SIMPLE_CLAUSE(Nontemporal, OMPC_nontemporal)
1218 CHECK_SIMPLE_CLAUSE(Order, OMPC_order)
1219 CHECK_SIMPLE_CLAUSE(Read, OMPC_read)
1220 CHECK_SIMPLE_CLAUSE(ReverseOffload, OMPC_reverse_offload)
1221 CHECK_SIMPLE_CLAUSE(Threadprivate, OMPC_threadprivate)
1222 CHECK_SIMPLE_CLAUSE(Threads, OMPC_threads)
1223 CHECK_SIMPLE_CLAUSE(Inbranch, OMPC_inbranch)
1224 CHECK_SIMPLE_CLAUSE(IsDevicePtr, OMPC_is_device_ptr)
1225 CHECK_SIMPLE_CLAUSE(Link, OMPC_link)
1226 CHECK_SIMPLE_CLAUSE(Mergeable, OMPC_mergeable)
1227 CHECK_SIMPLE_CLAUSE(Nogroup, OMPC_nogroup)
1228 CHECK_SIMPLE_CLAUSE(Notinbranch, OMPC_notinbranch)
1229 CHECK_SIMPLE_CLAUSE(Nowait, OMPC_nowait)
1230 CHECK_SIMPLE_CLAUSE(Partial, OMPC_partial)
1231 CHECK_SIMPLE_CLAUSE(ProcBind, OMPC_proc_bind)
1232 CHECK_SIMPLE_CLAUSE(Release, OMPC_release)
1233 CHECK_SIMPLE_CLAUSE(Relaxed, OMPC_relaxed)
1234 CHECK_SIMPLE_CLAUSE(SeqCst, OMPC_seq_cst)
1235 CHECK_SIMPLE_CLAUSE(Simd, OMPC_simd)
1236 CHECK_SIMPLE_CLAUSE(Sizes, OMPC_sizes)
1237 CHECK_SIMPLE_CLAUSE(TaskReduction, OMPC_task_reduction)
1238 CHECK_SIMPLE_CLAUSE(To, OMPC_to)
1239 CHECK_SIMPLE_CLAUSE(UnifiedAddress, OMPC_unified_address)
1240 CHECK_SIMPLE_CLAUSE(UnifiedSharedMemory, OMPC_unified_shared_memory)
1241 CHECK_SIMPLE_CLAUSE(Uniform, OMPC_uniform)
1242 CHECK_SIMPLE_CLAUSE(Unknown, OMPC_unknown)
1243 CHECK_SIMPLE_CLAUSE(Untied, OMPC_untied)
1244 CHECK_SIMPLE_CLAUSE(UseDevicePtr, OMPC_use_device_ptr)
1245 CHECK_SIMPLE_CLAUSE(UsesAllocators, OMPC_uses_allocators)
1246 CHECK_SIMPLE_CLAUSE(Update, OMPC_update)
1247 CHECK_SIMPLE_CLAUSE(UseDeviceAddr, OMPC_use_device_addr)
1248 CHECK_SIMPLE_CLAUSE(Write, OMPC_write)
1249 CHECK_SIMPLE_CLAUSE(Init, OMPC_init)
1250 CHECK_SIMPLE_CLAUSE(Use, OMPC_use)
1251 CHECK_SIMPLE_CLAUSE(Novariants, OMPC_novariants)
1252 CHECK_SIMPLE_CLAUSE(Nocontext, OMPC_nocontext)
1253 CHECK_SIMPLE_CLAUSE(Filter, OMPC_filter)
1254 
1255 CHECK_REQ_SCALAR_INT_CLAUSE(Grainsize, OMPC_grainsize)
1256 CHECK_REQ_SCALAR_INT_CLAUSE(NumTasks, OMPC_num_tasks)
1257 CHECK_REQ_SCALAR_INT_CLAUSE(NumTeams, OMPC_num_teams)
1258 CHECK_REQ_SCALAR_INT_CLAUSE(NumThreads, OMPC_num_threads)
1259 CHECK_REQ_SCALAR_INT_CLAUSE(Priority, OMPC_priority)
1260 CHECK_REQ_SCALAR_INT_CLAUSE(ThreadLimit, OMPC_thread_limit)
1261 
1262 CHECK_REQ_CONSTANT_SCALAR_INT_CLAUSE(Collapse, OMPC_collapse)
1263 CHECK_REQ_CONSTANT_SCALAR_INT_CLAUSE(Safelen, OMPC_safelen)
1264 CHECK_REQ_CONSTANT_SCALAR_INT_CLAUSE(Simdlen, OMPC_simdlen)
1265 
1266 // Restrictions specific to each clause are implemented apart from the
1267 // generalized restrictions.
1268 void OmpStructureChecker::Enter(const parser::OmpClause::Reduction &x) {
1269   CheckAllowed(llvm::omp::Clause::OMPC_reduction);
1270   if (CheckReductionOperators(x)) {
1271     CheckReductionTypeList(x);
1272   }
1273 }
1274 bool OmpStructureChecker::CheckReductionOperators(
1275     const parser::OmpClause::Reduction &x) {
1276 
1277   const auto &definedOp{std::get<0>(x.v.t)};
1278   bool ok = false;
1279   std::visit(
1280       common::visitors{
1281           [&](const parser::DefinedOperator &dOpr) {
1282             const auto &intrinsicOp{
1283                 std::get<parser::DefinedOperator::IntrinsicOperator>(dOpr.u)};
1284             ok = CheckIntrinsicOperator(intrinsicOp);
1285           },
1286           [&](const parser::ProcedureDesignator &procD) {
1287             const parser::Name *name{std::get_if<parser::Name>(&procD.u)};
1288             if (name) {
1289               if (name->source == "max" || name->source == "min" ||
1290                   name->source == "iand" || name->source == "ior" ||
1291                   name->source == "ieor") {
1292                 ok = true;
1293               } else {
1294                 context_.Say(GetContext().clauseSource,
1295                     "Invalid reduction identifier in REDUCTION clause."_err_en_US,
1296                     ContextDirectiveAsFortran());
1297               }
1298             }
1299           },
1300       },
1301       definedOp.u);
1302 
1303   return ok;
1304 }
1305 bool OmpStructureChecker::CheckIntrinsicOperator(
1306     const parser::DefinedOperator::IntrinsicOperator &op) {
1307 
1308   switch (op) {
1309   case parser::DefinedOperator::IntrinsicOperator::Add:
1310   case parser::DefinedOperator::IntrinsicOperator::Subtract:
1311   case parser::DefinedOperator::IntrinsicOperator::Multiply:
1312   case parser::DefinedOperator::IntrinsicOperator::AND:
1313   case parser::DefinedOperator::IntrinsicOperator::OR:
1314   case parser::DefinedOperator::IntrinsicOperator::EQV:
1315   case parser::DefinedOperator::IntrinsicOperator::NEQV:
1316     return true;
1317   default:
1318     context_.Say(GetContext().clauseSource,
1319         "Invalid reduction operator in REDUCTION clause."_err_en_US,
1320         ContextDirectiveAsFortran());
1321   }
1322   return false;
1323 }
1324 
1325 void OmpStructureChecker::CheckReductionTypeList(
1326     const parser::OmpClause::Reduction &x) {
1327   const auto &ompObjectList{std::get<parser::OmpObjectList>(x.v.t)};
1328   CheckIntentInPointerAndDefinable(
1329       ompObjectList, llvm::omp::Clause::OMPC_reduction);
1330   CheckReductionArraySection(ompObjectList);
1331   CheckMultipleAppearanceAcrossContext(ompObjectList);
1332 }
1333 
1334 void OmpStructureChecker::CheckIntentInPointerAndDefinable(
1335     const parser::OmpObjectList &objectList, const llvm::omp::Clause clause) {
1336   for (const auto &ompObject : objectList.v) {
1337     if (const auto *name{parser::Unwrap<parser::Name>(ompObject)}) {
1338       if (const auto *symbol{name->symbol}) {
1339         if (IsPointer(symbol->GetUltimate()) &&
1340             IsIntentIn(symbol->GetUltimate())) {
1341           context_.Say(GetContext().clauseSource,
1342               "Pointer '%s' with the INTENT(IN) attribute may not appear "
1343               "in a %s clause"_err_en_US,
1344               symbol->name(),
1345               parser::ToUpperCaseLetters(getClauseName(clause).str()));
1346         }
1347         if (auto msg{
1348                 WhyNotModifiable(*symbol, context_.FindScope(name->source))}) {
1349           context_.Say(GetContext().clauseSource,
1350               "Variable '%s' on the %s clause is not definable"_err_en_US,
1351               symbol->name(),
1352               parser::ToUpperCaseLetters(getClauseName(clause).str()));
1353         }
1354       }
1355     }
1356   }
1357 }
1358 
1359 void OmpStructureChecker::CheckReductionArraySection(
1360     const parser::OmpObjectList &ompObjectList) {
1361   for (const auto &ompObject : ompObjectList.v) {
1362     if (const auto *dataRef{parser::Unwrap<parser::DataRef>(ompObject)}) {
1363       if (const auto *arrayElement{
1364               parser::Unwrap<parser::ArrayElement>(ompObject)}) {
1365         if (arrayElement) {
1366           CheckArraySection(*arrayElement, GetLastName(*dataRef),
1367               llvm::omp::Clause::OMPC_reduction);
1368         }
1369       }
1370     }
1371   }
1372 }
1373 
1374 void OmpStructureChecker::CheckMultipleAppearanceAcrossContext(
1375     const parser::OmpObjectList &redObjectList) {
1376   //  TODO: Verify the assumption here that the immediately enclosing region is
1377   //  the parallel region to which the worksharing construct having reduction
1378   //  binds to.
1379   if (auto *enclosingContext{GetEnclosingDirContext()}) {
1380     for (auto it : enclosingContext->clauseInfo) {
1381       llvmOmpClause type = it.first;
1382       const auto *clause = it.second;
1383       if (llvm::omp::privateReductionSet.test(type)) {
1384         if (const auto *objList{GetOmpObjectList(*clause)}) {
1385           for (const auto &ompObject : objList->v) {
1386             if (const auto *name{parser::Unwrap<parser::Name>(ompObject)}) {
1387               if (const auto *symbol{name->symbol}) {
1388                 for (const auto &redOmpObject : redObjectList.v) {
1389                   if (const auto *rname{
1390                           parser::Unwrap<parser::Name>(redOmpObject)}) {
1391                     if (const auto *rsymbol{rname->symbol}) {
1392                       if (rsymbol->name() == symbol->name()) {
1393                         context_.Say(GetContext().clauseSource,
1394                             "%s variable '%s' is %s in outer context must"
1395                             " be shared in the parallel regions to which any"
1396                             " of the worksharing regions arising from the "
1397                             "worksharing"
1398                             " construct bind."_err_en_US,
1399                             parser::ToUpperCaseLetters(
1400                                 getClauseName(llvm::omp::Clause::OMPC_reduction)
1401                                     .str()),
1402                             symbol->name(),
1403                             parser::ToUpperCaseLetters(
1404                                 getClauseName(type).str()));
1405                       }
1406                     }
1407                   }
1408                 }
1409               }
1410             }
1411           }
1412         }
1413       }
1414     }
1415   }
1416 }
1417 
1418 void OmpStructureChecker::Enter(const parser::OmpClause::Ordered &x) {
1419   CheckAllowed(llvm::omp::Clause::OMPC_ordered);
1420   // the parameter of ordered clause is optional
1421   if (const auto &expr{x.v}) {
1422     RequiresConstantPositiveParameter(llvm::omp::Clause::OMPC_ordered, *expr);
1423     // 2.8.3 Loop SIMD Construct Restriction
1424     if (llvm::omp::doSimdSet.test(GetContext().directive)) {
1425       context_.Say(GetContext().clauseSource,
1426           "No ORDERED clause with a parameter can be specified "
1427           "on the %s directive"_err_en_US,
1428           ContextDirectiveAsFortran());
1429     }
1430   }
1431 }
1432 
1433 void OmpStructureChecker::Enter(const parser::OmpClause::Shared &x) {
1434   CheckAllowed(llvm::omp::Clause::OMPC_shared);
1435   CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v);
1436 }
1437 void OmpStructureChecker::Enter(const parser::OmpClause::Private &x) {
1438   CheckAllowed(llvm::omp::Clause::OMPC_private);
1439   CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v);
1440   CheckIntentInPointer(x.v, llvm::omp::Clause::OMPC_private);
1441 }
1442 
1443 bool OmpStructureChecker::IsDataRefTypeParamInquiry(
1444     const parser::DataRef *dataRef) {
1445   bool dataRefIsTypeParamInquiry{false};
1446   if (const auto *structComp{
1447           parser::Unwrap<parser::StructureComponent>(dataRef)}) {
1448     if (const auto *compSymbol{structComp->component.symbol}) {
1449       if (const auto *compSymbolMiscDetails{
1450               std::get_if<MiscDetails>(&compSymbol->details())}) {
1451         const auto detailsKind = compSymbolMiscDetails->kind();
1452         dataRefIsTypeParamInquiry =
1453             (detailsKind == MiscDetails::Kind::KindParamInquiry ||
1454                 detailsKind == MiscDetails::Kind::LenParamInquiry);
1455       } else if (compSymbol->has<TypeParamDetails>()) {
1456         dataRefIsTypeParamInquiry = true;
1457       }
1458     }
1459   }
1460   return dataRefIsTypeParamInquiry;
1461 }
1462 
1463 void OmpStructureChecker::CheckIsVarPartOfAnotherVar(
1464     const parser::CharBlock &source, const parser::OmpObjectList &objList) {
1465 
1466   for (const auto &ompObject : objList.v) {
1467     std::visit(
1468         common::visitors{
1469             [&](const parser::Designator &designator) {
1470               if (const auto *dataRef{
1471                       std::get_if<parser::DataRef>(&designator.u)}) {
1472                 if (IsDataRefTypeParamInquiry(dataRef)) {
1473                   context_.Say(source,
1474                       "A type parameter inquiry cannot appear in an ALLOCATE directive"_err_en_US);
1475                 } else if (parser::Unwrap<parser::StructureComponent>(
1476                                ompObject) ||
1477                     parser::Unwrap<parser::ArrayElement>(ompObject)) {
1478                   context_.Say(source,
1479                       "A variable that is part of another variable (as an "
1480                       "array or structure element)"
1481                       " cannot appear in a PRIVATE or SHARED clause or on the ALLOCATE directive."_err_en_US);
1482                 }
1483               }
1484             },
1485             [&](const parser::Name &name) {},
1486         },
1487         ompObject.u);
1488   }
1489 }
1490 
1491 void OmpStructureChecker::Enter(const parser::OmpClause::Firstprivate &x) {
1492   CheckAllowed(llvm::omp::Clause::OMPC_firstprivate);
1493   CheckIsLoopIvPartOfClause(llvmOmpClause::OMPC_firstprivate, x.v);
1494 
1495   SymbolSourceMap currSymbols;
1496   GetSymbolsInObjectList(x.v, currSymbols);
1497 
1498   DirectivesClauseTriple dirClauseTriple;
1499   // Check firstprivate variables in worksharing constructs
1500   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_do,
1501       std::make_pair(
1502           llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet));
1503   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_sections,
1504       std::make_pair(
1505           llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet));
1506   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_single,
1507       std::make_pair(
1508           llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet));
1509   // Check firstprivate variables in distribute construct
1510   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_distribute,
1511       std::make_pair(
1512           llvm::omp::Directive::OMPD_teams, llvm::omp::privateReductionSet));
1513   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_distribute,
1514       std::make_pair(llvm::omp::Directive::OMPD_target_teams,
1515           llvm::omp::privateReductionSet));
1516   // Check firstprivate variables in task and taskloop constructs
1517   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_task,
1518       std::make_pair(llvm::omp::Directive::OMPD_parallel,
1519           OmpClauseSet{llvm::omp::Clause::OMPC_reduction}));
1520   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_taskloop,
1521       std::make_pair(llvm::omp::Directive::OMPD_parallel,
1522           OmpClauseSet{llvm::omp::Clause::OMPC_reduction}));
1523 
1524   CheckPrivateSymbolsInOuterCxt(
1525       currSymbols, dirClauseTriple, llvm::omp::Clause::OMPC_firstprivate);
1526 }
1527 
1528 void OmpStructureChecker::CheckIsLoopIvPartOfClause(
1529     llvmOmpClause clause, const parser::OmpObjectList &ompObjectList) {
1530   for (const auto &ompObject : ompObjectList.v) {
1531     if (const parser::Name * name{parser::Unwrap<parser::Name>(ompObject)}) {
1532       if (name->symbol == GetContext().loopIV) {
1533         context_.Say(name->source,
1534             "DO iteration variable %s is not allowed in %s clause."_err_en_US,
1535             name->ToString(),
1536             parser::ToUpperCaseLetters(getClauseName(clause).str()));
1537       }
1538     }
1539   }
1540 }
1541 // Following clauses have a seperate node in parse-tree.h.
1542 // Atomic-clause
1543 CHECK_SIMPLE_PARSER_CLAUSE(OmpAtomicRead, OMPC_read)
1544 CHECK_SIMPLE_PARSER_CLAUSE(OmpAtomicWrite, OMPC_write)
1545 CHECK_SIMPLE_PARSER_CLAUSE(OmpAtomicUpdate, OMPC_update)
1546 CHECK_SIMPLE_PARSER_CLAUSE(OmpAtomicCapture, OMPC_capture)
1547 
1548 void OmpStructureChecker::Leave(const parser::OmpAtomicRead &) {
1549   CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_read,
1550       {llvm::omp::Clause::OMPC_release, llvm::omp::Clause::OMPC_acq_rel});
1551 }
1552 void OmpStructureChecker::Leave(const parser::OmpAtomicWrite &) {
1553   CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_write,
1554       {llvm::omp::Clause::OMPC_acquire, llvm::omp::Clause::OMPC_acq_rel});
1555 }
1556 void OmpStructureChecker::Leave(const parser::OmpAtomicUpdate &) {
1557   CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_update,
1558       {llvm::omp::Clause::OMPC_acquire, llvm::omp::Clause::OMPC_acq_rel});
1559 }
1560 // OmpAtomic node represents atomic directive without atomic-clause.
1561 // atomic-clause - READ,WRITE,UPDATE,CAPTURE.
1562 void OmpStructureChecker::Leave(const parser::OmpAtomic &) {
1563   if (const auto *clause{FindClause(llvm::omp::Clause::OMPC_acquire)}) {
1564     context_.Say(clause->source,
1565         "Clause ACQUIRE is not allowed on the ATOMIC directive"_err_en_US);
1566   }
1567   if (const auto *clause{FindClause(llvm::omp::Clause::OMPC_acq_rel)}) {
1568     context_.Say(clause->source,
1569         "Clause ACQ_REL is not allowed on the ATOMIC directive"_err_en_US);
1570   }
1571 }
1572 // Restrictions specific to each clause are implemented apart from the
1573 // generalized restrictions.
1574 void OmpStructureChecker::Enter(const parser::OmpClause::Aligned &x) {
1575   CheckAllowed(llvm::omp::Clause::OMPC_aligned);
1576 
1577   if (const auto &expr{
1578           std::get<std::optional<parser::ScalarIntConstantExpr>>(x.v.t)}) {
1579     RequiresConstantPositiveParameter(llvm::omp::Clause::OMPC_aligned, *expr);
1580   }
1581   // 2.8.1 TODO: list-item attribute check
1582 }
1583 void OmpStructureChecker::Enter(const parser::OmpClause::Defaultmap &x) {
1584   CheckAllowed(llvm::omp::Clause::OMPC_defaultmap);
1585   using VariableCategory = parser::OmpDefaultmapClause::VariableCategory;
1586   if (!std::get<std::optional<VariableCategory>>(x.v.t)) {
1587     context_.Say(GetContext().clauseSource,
1588         "The argument TOFROM:SCALAR must be specified on the DEFAULTMAP "
1589         "clause"_err_en_US);
1590   }
1591 }
1592 void OmpStructureChecker::Enter(const parser::OmpClause::If &x) {
1593   CheckAllowed(llvm::omp::Clause::OMPC_if);
1594   using dirNameModifier = parser::OmpIfClause::DirectiveNameModifier;
1595   static std::unordered_map<dirNameModifier, OmpDirectiveSet>
1596       dirNameModifierMap{{dirNameModifier::Parallel, llvm::omp::parallelSet},
1597           {dirNameModifier::Target, llvm::omp::targetSet},
1598           {dirNameModifier::TargetEnterData,
1599               {llvm::omp::Directive::OMPD_target_enter_data}},
1600           {dirNameModifier::TargetExitData,
1601               {llvm::omp::Directive::OMPD_target_exit_data}},
1602           {dirNameModifier::TargetData,
1603               {llvm::omp::Directive::OMPD_target_data}},
1604           {dirNameModifier::TargetUpdate,
1605               {llvm::omp::Directive::OMPD_target_update}},
1606           {dirNameModifier::Task, {llvm::omp::Directive::OMPD_task}},
1607           {dirNameModifier::Taskloop, llvm::omp::taskloopSet}};
1608   if (const auto &directiveName{
1609           std::get<std::optional<dirNameModifier>>(x.v.t)}) {
1610     auto search{dirNameModifierMap.find(*directiveName)};
1611     if (search == dirNameModifierMap.end() ||
1612         !search->second.test(GetContext().directive)) {
1613       context_
1614           .Say(GetContext().clauseSource,
1615               "Unmatched directive name modifier %s on the IF clause"_err_en_US,
1616               parser::ToUpperCaseLetters(
1617                   parser::OmpIfClause::EnumToString(*directiveName)))
1618           .Attach(
1619               GetContext().directiveSource, "Cannot apply to directive"_en_US);
1620     }
1621   }
1622 }
1623 
1624 void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) {
1625   CheckAllowed(llvm::omp::Clause::OMPC_linear);
1626 
1627   // 2.7 Loop Construct Restriction
1628   if ((llvm::omp::doSet | llvm::omp::simdSet).test(GetContext().directive)) {
1629     if (std::holds_alternative<parser::OmpLinearClause::WithModifier>(x.v.u)) {
1630       context_.Say(GetContext().clauseSource,
1631           "A modifier may not be specified in a LINEAR clause "
1632           "on the %s directive"_err_en_US,
1633           ContextDirectiveAsFortran());
1634     }
1635   }
1636 }
1637 
1638 void OmpStructureChecker::CheckAllowedMapTypes(
1639     const parser::OmpMapType::Type &type,
1640     const std::list<parser::OmpMapType::Type> &allowedMapTypeList) {
1641   const auto found{std::find(
1642       std::begin(allowedMapTypeList), std::end(allowedMapTypeList), type)};
1643   if (found == std::end(allowedMapTypeList)) {
1644     std::string commaSeperatedMapTypes;
1645     llvm::interleave(
1646         allowedMapTypeList.begin(), allowedMapTypeList.end(),
1647         [&](const parser::OmpMapType::Type &mapType) {
1648           commaSeperatedMapTypes.append(parser::ToUpperCaseLetters(
1649               parser::OmpMapType::EnumToString(mapType)));
1650         },
1651         [&] { commaSeperatedMapTypes.append(", "); });
1652     context_.Say(GetContext().clauseSource,
1653         "Only the %s map types are permitted "
1654         "for MAP clauses on the %s directive"_err_en_US,
1655         commaSeperatedMapTypes, ContextDirectiveAsFortran());
1656   }
1657 }
1658 
1659 void OmpStructureChecker::Enter(const parser::OmpClause::Map &x) {
1660   CheckAllowed(llvm::omp::Clause::OMPC_map);
1661 
1662   if (const auto &maptype{std::get<std::optional<parser::OmpMapType>>(x.v.t)}) {
1663     using Type = parser::OmpMapType::Type;
1664     const Type &type{std::get<Type>(maptype->t)};
1665     switch (GetContext().directive) {
1666     case llvm::omp::Directive::OMPD_target:
1667     case llvm::omp::Directive::OMPD_target_teams:
1668     case llvm::omp::Directive::OMPD_target_teams_distribute:
1669     case llvm::omp::Directive::OMPD_target_teams_distribute_simd:
1670     case llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do:
1671     case llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do_simd:
1672     case llvm::omp::Directive::OMPD_target_data:
1673       CheckAllowedMapTypes(
1674           type, {Type::To, Type::From, Type::Tofrom, Type::Alloc});
1675       break;
1676     case llvm::omp::Directive::OMPD_target_enter_data:
1677       CheckAllowedMapTypes(type, {Type::To, Type::Alloc});
1678       break;
1679     case llvm::omp::Directive::OMPD_target_exit_data:
1680       CheckAllowedMapTypes(type, {Type::From, Type::Release, Type::Delete});
1681       break;
1682     default:
1683       break;
1684     }
1685   }
1686 }
1687 
1688 bool OmpStructureChecker::ScheduleModifierHasType(
1689     const parser::OmpScheduleClause &x,
1690     const parser::OmpScheduleModifierType::ModType &type) {
1691   const auto &modifier{
1692       std::get<std::optional<parser::OmpScheduleModifier>>(x.t)};
1693   if (modifier) {
1694     const auto &modType1{
1695         std::get<parser::OmpScheduleModifier::Modifier1>(modifier->t)};
1696     const auto &modType2{
1697         std::get<std::optional<parser::OmpScheduleModifier::Modifier2>>(
1698             modifier->t)};
1699     if (modType1.v.v == type || (modType2 && modType2->v.v == type)) {
1700       return true;
1701     }
1702   }
1703   return false;
1704 }
1705 void OmpStructureChecker::Enter(const parser::OmpClause::Schedule &x) {
1706   CheckAllowed(llvm::omp::Clause::OMPC_schedule);
1707   const parser::OmpScheduleClause &scheduleClause = x.v;
1708 
1709   // 2.7 Loop Construct Restriction
1710   if (llvm::omp::doSet.test(GetContext().directive)) {
1711     const auto &kind{std::get<1>(scheduleClause.t)};
1712     const auto &chunk{std::get<2>(scheduleClause.t)};
1713     if (chunk) {
1714       if (kind == parser::OmpScheduleClause::ScheduleType::Runtime ||
1715           kind == parser::OmpScheduleClause::ScheduleType::Auto) {
1716         context_.Say(GetContext().clauseSource,
1717             "When SCHEDULE clause has %s specified, "
1718             "it must not have chunk size specified"_err_en_US,
1719             parser::ToUpperCaseLetters(
1720                 parser::OmpScheduleClause::EnumToString(kind)));
1721       }
1722       if (const auto &chunkExpr{std::get<std::optional<parser::ScalarIntExpr>>(
1723               scheduleClause.t)}) {
1724         RequiresPositiveParameter(
1725             llvm::omp::Clause::OMPC_schedule, *chunkExpr, "chunk size");
1726       }
1727     }
1728 
1729     if (ScheduleModifierHasType(scheduleClause,
1730             parser::OmpScheduleModifierType::ModType::Nonmonotonic)) {
1731       if (kind != parser::OmpScheduleClause::ScheduleType::Dynamic &&
1732           kind != parser::OmpScheduleClause::ScheduleType::Guided) {
1733         context_.Say(GetContext().clauseSource,
1734             "The NONMONOTONIC modifier can only be specified with "
1735             "SCHEDULE(DYNAMIC) or SCHEDULE(GUIDED)"_err_en_US);
1736       }
1737     }
1738   }
1739 }
1740 
1741 void OmpStructureChecker::Enter(const parser::OmpClause::Depend &x) {
1742   CheckAllowed(llvm::omp::Clause::OMPC_depend);
1743   if (const auto *inOut{std::get_if<parser::OmpDependClause::InOut>(&x.v.u)}) {
1744     const auto &designators{std::get<std::list<parser::Designator>>(inOut->t)};
1745     for (const auto &ele : designators) {
1746       if (const auto *dataRef{std::get_if<parser::DataRef>(&ele.u)}) {
1747         CheckDependList(*dataRef);
1748         if (const auto *arr{
1749                 std::get_if<common::Indirection<parser::ArrayElement>>(
1750                     &dataRef->u)}) {
1751           CheckArraySection(arr->value(), GetLastName(*dataRef),
1752               llvm::omp::Clause::OMPC_depend);
1753         }
1754       }
1755     }
1756   }
1757 }
1758 
1759 void OmpStructureChecker::Enter(const parser::OmpClause::Copyprivate &x) {
1760   CheckAllowed(llvm::omp::Clause::OMPC_copyprivate);
1761   CheckIntentInPointer(x.v, llvm::omp::Clause::OMPC_copyprivate);
1762 }
1763 
1764 void OmpStructureChecker::Enter(const parser::OmpClause::Lastprivate &x) {
1765   CheckAllowed(llvm::omp::Clause::OMPC_lastprivate);
1766 
1767   DirectivesClauseTriple dirClauseTriple;
1768   SymbolSourceMap currSymbols;
1769   GetSymbolsInObjectList(x.v, currSymbols);
1770   CheckDefinableObjects(currSymbols, GetClauseKindForParserClass(x));
1771 
1772   // Check lastprivate variables in worksharing constructs
1773   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_do,
1774       std::make_pair(
1775           llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet));
1776   dirClauseTriple.emplace(llvm::omp::Directive::OMPD_sections,
1777       std::make_pair(
1778           llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet));
1779 
1780   CheckPrivateSymbolsInOuterCxt(
1781       currSymbols, dirClauseTriple, GetClauseKindForParserClass(x));
1782 }
1783 
1784 llvm::StringRef OmpStructureChecker::getClauseName(llvm::omp::Clause clause) {
1785   return llvm::omp::getOpenMPClauseName(clause);
1786 }
1787 
1788 llvm::StringRef OmpStructureChecker::getDirectiveName(
1789     llvm::omp::Directive directive) {
1790   return llvm::omp::getOpenMPDirectiveName(directive);
1791 }
1792 
1793 void OmpStructureChecker::CheckDependList(const parser::DataRef &d) {
1794   std::visit(
1795       common::visitors{
1796           [&](const common::Indirection<parser::ArrayElement> &elem) {
1797             // Check if the base element is valid on Depend Clause
1798             CheckDependList(elem.value().base);
1799           },
1800           [&](const common::Indirection<parser::StructureComponent> &) {
1801             context_.Say(GetContext().clauseSource,
1802                 "A variable that is part of another variable "
1803                 "(such as an element of a structure) but is not an array "
1804                 "element or an array section cannot appear in a DEPEND "
1805                 "clause"_err_en_US);
1806           },
1807           [&](const common::Indirection<parser::CoindexedNamedObject> &) {
1808             context_.Say(GetContext().clauseSource,
1809                 "Coarrays are not supported in DEPEND clause"_err_en_US);
1810           },
1811           [&](const parser::Name &) { return; },
1812       },
1813       d.u);
1814 }
1815 
1816 // Called from both Reduction and Depend clause.
1817 void OmpStructureChecker::CheckArraySection(
1818     const parser::ArrayElement &arrayElement, const parser::Name &name,
1819     const llvm::omp::Clause clause) {
1820   if (!arrayElement.subscripts.empty()) {
1821     for (const auto &subscript : arrayElement.subscripts) {
1822       if (const auto *triplet{
1823               std::get_if<parser::SubscriptTriplet>(&subscript.u)}) {
1824         if (std::get<0>(triplet->t) && std::get<1>(triplet->t)) {
1825           const auto &lower{std::get<0>(triplet->t)};
1826           const auto &upper{std::get<1>(triplet->t)};
1827           if (lower && upper) {
1828             const auto lval{GetIntValue(lower)};
1829             const auto uval{GetIntValue(upper)};
1830             if (lval && uval && *uval < *lval) {
1831               context_.Say(GetContext().clauseSource,
1832                   "'%s' in %s clause"
1833                   " is a zero size array section"_err_en_US,
1834                   name.ToString(),
1835                   parser::ToUpperCaseLetters(getClauseName(clause).str()));
1836               break;
1837             } else if (std::get<2>(triplet->t)) {
1838               const auto &strideExpr{std::get<2>(triplet->t)};
1839               if (strideExpr) {
1840                 if (clause == llvm::omp::Clause::OMPC_depend) {
1841                   context_.Say(GetContext().clauseSource,
1842                       "Stride should not be specified for array section in "
1843                       "DEPEND "
1844                       "clause"_err_en_US);
1845                 }
1846                 const auto stride{GetIntValue(strideExpr)};
1847                 if ((stride && stride != 1)) {
1848                   context_.Say(GetContext().clauseSource,
1849                       "A list item that appears in a REDUCTION clause"
1850                       " should have a contiguous storage array section."_err_en_US,
1851                       ContextDirectiveAsFortran());
1852                   break;
1853                 }
1854               }
1855             }
1856           }
1857         }
1858       }
1859     }
1860   }
1861 }
1862 
1863 void OmpStructureChecker::CheckIntentInPointer(
1864     const parser::OmpObjectList &objectList, const llvm::omp::Clause clause) {
1865   SymbolSourceMap symbols;
1866   GetSymbolsInObjectList(objectList, symbols);
1867   for (auto it{symbols.begin()}; it != symbols.end(); ++it) {
1868     const auto *symbol{it->first};
1869     const auto source{it->second};
1870     if (IsPointer(*symbol) && IsIntentIn(*symbol)) {
1871       context_.Say(source,
1872           "Pointer '%s' with the INTENT(IN) attribute may not appear "
1873           "in a %s clause"_err_en_US,
1874           symbol->name(),
1875           parser::ToUpperCaseLetters(getClauseName(clause).str()));
1876     }
1877   }
1878 }
1879 
1880 void OmpStructureChecker::GetSymbolsInObjectList(
1881     const parser::OmpObjectList &objectList, SymbolSourceMap &symbols) {
1882   for (const auto &ompObject : objectList.v) {
1883     if (const auto *name{parser::Unwrap<parser::Name>(ompObject)}) {
1884       if (const auto *symbol{name->symbol}) {
1885         if (const auto *commonBlockDetails{
1886                 symbol->detailsIf<CommonBlockDetails>()}) {
1887           for (const auto &object : commonBlockDetails->objects()) {
1888             symbols.emplace(&object->GetUltimate(), name->source);
1889           }
1890         } else {
1891           symbols.emplace(&symbol->GetUltimate(), name->source);
1892         }
1893       }
1894     }
1895   }
1896 }
1897 
1898 void OmpStructureChecker::CheckDefinableObjects(
1899     SymbolSourceMap &symbols, const llvm::omp::Clause clause) {
1900   for (auto it{symbols.begin()}; it != symbols.end(); ++it) {
1901     const auto *symbol{it->first};
1902     const auto source{it->second};
1903     if (auto msg{WhyNotModifiable(*symbol, context_.FindScope(source))}) {
1904       context_
1905           .Say(source,
1906               "Variable '%s' on the %s clause is not definable"_err_en_US,
1907               symbol->name(),
1908               parser::ToUpperCaseLetters(getClauseName(clause).str()))
1909           .Attach(source, std::move(*msg), symbol->name());
1910     }
1911   }
1912 }
1913 
1914 void OmpStructureChecker::CheckPrivateSymbolsInOuterCxt(
1915     SymbolSourceMap &currSymbols, DirectivesClauseTriple &dirClauseTriple,
1916     const llvm::omp::Clause currClause) {
1917   SymbolSourceMap enclosingSymbols;
1918   auto range{dirClauseTriple.equal_range(GetContext().directive)};
1919   for (auto dirIter{range.first}; dirIter != range.second; ++dirIter) {
1920     auto enclosingDir{dirIter->second.first};
1921     auto enclosingClauseSet{dirIter->second.second};
1922     if (auto *enclosingContext{GetEnclosingContextWithDir(enclosingDir)}) {
1923       for (auto it{enclosingContext->clauseInfo.begin()};
1924            it != enclosingContext->clauseInfo.end(); ++it) {
1925         if (enclosingClauseSet.test(it->first)) {
1926           if (const auto *ompObjectList{GetOmpObjectList(*it->second)}) {
1927             GetSymbolsInObjectList(*ompObjectList, enclosingSymbols);
1928           }
1929         }
1930       }
1931 
1932       // Check if the symbols in current context are private in outer context
1933       for (auto iter{currSymbols.begin()}; iter != currSymbols.end(); ++iter) {
1934         const auto *symbol{iter->first};
1935         const auto source{iter->second};
1936         if (enclosingSymbols.find(symbol) != enclosingSymbols.end()) {
1937           context_.Say(source,
1938               "%s variable '%s' is PRIVATE in outer context"_err_en_US,
1939               parser::ToUpperCaseLetters(getClauseName(currClause).str()),
1940               symbol->name());
1941         }
1942       }
1943     }
1944   }
1945 }
1946 
1947 bool OmpStructureChecker::CheckTargetBlockOnlyTeams(
1948     const parser::Block &block) {
1949   bool nestedTeams{false};
1950   auto it{block.begin()};
1951 
1952   if (const auto *ompConstruct{parser::Unwrap<parser::OpenMPConstruct>(*it)}) {
1953     if (const auto *ompBlockConstruct{
1954             std::get_if<parser::OpenMPBlockConstruct>(&ompConstruct->u)}) {
1955       const auto &beginBlockDir{
1956           std::get<parser::OmpBeginBlockDirective>(ompBlockConstruct->t)};
1957       const auto &beginDir{
1958           std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
1959       if (beginDir.v == llvm::omp::Directive::OMPD_teams) {
1960         nestedTeams = true;
1961       }
1962     }
1963   }
1964 
1965   if (nestedTeams && ++it == block.end()) {
1966     return true;
1967   }
1968   return false;
1969 }
1970 
1971 void OmpStructureChecker::CheckWorkshareBlockStmts(
1972     const parser::Block &block, parser::CharBlock source) {
1973   OmpWorkshareBlockChecker ompWorkshareBlockChecker{context_, source};
1974 
1975   for (auto it{block.begin()}; it != block.end(); ++it) {
1976     if (parser::Unwrap<parser::AssignmentStmt>(*it) ||
1977         parser::Unwrap<parser::ForallStmt>(*it) ||
1978         parser::Unwrap<parser::ForallConstruct>(*it) ||
1979         parser::Unwrap<parser::WhereStmt>(*it) ||
1980         parser::Unwrap<parser::WhereConstruct>(*it)) {
1981       parser::Walk(*it, ompWorkshareBlockChecker);
1982     } else if (const auto *ompConstruct{
1983                    parser::Unwrap<parser::OpenMPConstruct>(*it)}) {
1984       if (const auto *ompAtomicConstruct{
1985               std::get_if<parser::OpenMPAtomicConstruct>(&ompConstruct->u)}) {
1986         // Check if assignment statements in the enclosing OpenMP Atomic
1987         // construct are allowed in the Workshare construct
1988         parser::Walk(*ompAtomicConstruct, ompWorkshareBlockChecker);
1989       } else if (const auto *ompCriticalConstruct{
1990                      std::get_if<parser::OpenMPCriticalConstruct>(
1991                          &ompConstruct->u)}) {
1992         // All the restrictions on the Workshare construct apply to the
1993         // statements in the enclosing critical constructs
1994         const auto &criticalBlock{
1995             std::get<parser::Block>(ompCriticalConstruct->t)};
1996         CheckWorkshareBlockStmts(criticalBlock, source);
1997       } else {
1998         // Check if OpenMP constructs enclosed in the Workshare construct are
1999         // 'Parallel' constructs
2000         auto currentDir{llvm::omp::Directive::OMPD_unknown};
2001         const OmpDirectiveSet parallelDirSet{
2002             llvm::omp::Directive::OMPD_parallel,
2003             llvm::omp::Directive::OMPD_parallel_do,
2004             llvm::omp::Directive::OMPD_parallel_sections,
2005             llvm::omp::Directive::OMPD_parallel_workshare,
2006             llvm::omp::Directive::OMPD_parallel_do_simd};
2007 
2008         if (const auto *ompBlockConstruct{
2009                 std::get_if<parser::OpenMPBlockConstruct>(&ompConstruct->u)}) {
2010           const auto &beginBlockDir{
2011               std::get<parser::OmpBeginBlockDirective>(ompBlockConstruct->t)};
2012           const auto &beginDir{
2013               std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
2014           currentDir = beginDir.v;
2015         } else if (const auto *ompLoopConstruct{
2016                        std::get_if<parser::OpenMPLoopConstruct>(
2017                            &ompConstruct->u)}) {
2018           const auto &beginLoopDir{
2019               std::get<parser::OmpBeginLoopDirective>(ompLoopConstruct->t)};
2020           const auto &beginDir{
2021               std::get<parser::OmpLoopDirective>(beginLoopDir.t)};
2022           currentDir = beginDir.v;
2023         } else if (const auto *ompSectionsConstruct{
2024                        std::get_if<parser::OpenMPSectionsConstruct>(
2025                            &ompConstruct->u)}) {
2026           const auto &beginSectionsDir{
2027               std::get<parser::OmpBeginSectionsDirective>(
2028                   ompSectionsConstruct->t)};
2029           const auto &beginDir{
2030               std::get<parser::OmpSectionsDirective>(beginSectionsDir.t)};
2031           currentDir = beginDir.v;
2032         }
2033 
2034         if (!parallelDirSet.test(currentDir)) {
2035           context_.Say(source,
2036               "OpenMP constructs enclosed in WORKSHARE construct may consist "
2037               "of ATOMIC, CRITICAL or PARALLEL constructs only"_err_en_US);
2038         }
2039       }
2040     } else {
2041       context_.Say(source,
2042           "The structured block in a WORKSHARE construct may consist of only "
2043           "SCALAR or ARRAY assignments, FORALL or WHERE statements, "
2044           "FORALL, WHERE, ATOMIC, CRITICAL or PARALLEL constructs"_err_en_US);
2045     }
2046   }
2047 }
2048 
2049 const parser::OmpObjectList *OmpStructureChecker::GetOmpObjectList(
2050     const parser::OmpClause &clause) {
2051 
2052   // Clauses with OmpObjectList as its data member
2053   using MemberObjectListClauses = std::tuple<parser::OmpClause::Copyprivate,
2054       parser::OmpClause::Copyin, parser::OmpClause::Firstprivate,
2055       parser::OmpClause::From, parser::OmpClause::Lastprivate,
2056       parser::OmpClause::Link, parser::OmpClause::Private,
2057       parser::OmpClause::Shared, parser::OmpClause::To>;
2058 
2059   // Clauses with OmpObjectList in the tuple
2060   using TupleObjectListClauses = std::tuple<parser::OmpClause::Allocate,
2061       parser::OmpClause::Map, parser::OmpClause::Reduction>;
2062 
2063   // TODO:: Generate the tuples using TableGen.
2064   // Handle other constructs with OmpObjectList such as OpenMPThreadprivate.
2065   return std::visit(
2066       common::visitors{
2067           [&](const auto &x) -> const parser::OmpObjectList * {
2068             using Ty = std::decay_t<decltype(x)>;
2069             if constexpr (common::HasMember<Ty, MemberObjectListClauses>) {
2070               return &x.v;
2071             } else if constexpr (common::HasMember<Ty,
2072                                      TupleObjectListClauses>) {
2073               return &(std::get<parser::OmpObjectList>(x.v.t));
2074             } else {
2075               return nullptr;
2076             }
2077           },
2078       },
2079       clause.u);
2080 }
2081 
2082 } // namespace Fortran::semantics
2083