1 //===----------------------------------------------------------------------===//
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 "resolve-directives.h"
10 
11 #include "check-acc-structure.h"
12 #include "check-omp-structure.h"
13 #include "resolve-names-utils.h"
14 #include "flang/Common/idioms.h"
15 #include "flang/Evaluate/fold.h"
16 #include "flang/Evaluate/type.h"
17 #include "flang/Parser/parse-tree-visitor.h"
18 #include "flang/Parser/parse-tree.h"
19 #include "flang/Parser/tools.h"
20 #include "flang/Semantics/expression.h"
21 #include <list>
22 #include <map>
23 
24 namespace Fortran::semantics {
25 
26 template <typename T> class DirectiveAttributeVisitor {
27 public:
28   explicit DirectiveAttributeVisitor(SemanticsContext &context)
29       : context_{context} {}
30 
31   template <typename A> bool Pre(const A &) { return true; }
32   template <typename A> void Post(const A &) {}
33 
34 protected:
35   struct DirContext {
36     DirContext(const parser::CharBlock &source, T d, Scope &s)
37         : directiveSource{source}, directive{d}, scope{s} {}
38     parser::CharBlock directiveSource;
39     T directive;
40     Scope &scope;
41     Symbol::Flag defaultDSA{Symbol::Flag::AccShared}; // TODOACC
42     std::map<const Symbol *, Symbol::Flag> objectWithDSA;
43     bool withinConstruct{false};
44     std::int64_t associatedLoopLevel{0};
45   };
46 
47   DirContext &GetContext() {
48     CHECK(!dirContext_.empty());
49     return dirContext_.back();
50   }
51   std::optional<DirContext> GetContextIf() {
52     return dirContext_.empty()
53         ? std::nullopt
54         : std::make_optional<DirContext>(dirContext_.back());
55   }
56   void PushContext(const parser::CharBlock &source, T dir) {
57     dirContext_.emplace_back(source, dir, context_.FindScope(source));
58   }
59   void PopContext() { dirContext_.pop_back(); }
60   void SetContextDirectiveSource(parser::CharBlock &dir) {
61     GetContext().directiveSource = dir;
62   }
63   Scope &currScope() { return GetContext().scope; }
64   void SetContextDefaultDSA(Symbol::Flag flag) {
65     GetContext().defaultDSA = flag;
66   }
67   void AddToContextObjectWithDSA(
68       const Symbol &symbol, Symbol::Flag flag, DirContext &context) {
69     context.objectWithDSA.emplace(&symbol, flag);
70   }
71   void AddToContextObjectWithDSA(const Symbol &symbol, Symbol::Flag flag) {
72     AddToContextObjectWithDSA(symbol, flag, GetContext());
73   }
74   bool IsObjectWithDSA(const Symbol &symbol) {
75     auto it{GetContext().objectWithDSA.find(&symbol)};
76     return it != GetContext().objectWithDSA.end();
77   }
78   void SetContextAssociatedLoopLevel(std::int64_t level) {
79     GetContext().associatedLoopLevel = level;
80   }
81   Symbol &MakeAssocSymbol(const SourceName &name, Symbol &prev, Scope &scope) {
82     const auto pair{scope.try_emplace(name, Attrs{}, HostAssocDetails{prev})};
83     return *pair.first->second;
84   }
85   Symbol &MakeAssocSymbol(const SourceName &name, Symbol &prev) {
86     return MakeAssocSymbol(name, prev, currScope());
87   }
88   static const parser::Name *GetDesignatorNameIfDataRef(
89       const parser::Designator &designator) {
90     const auto *dataRef{std::get_if<parser::DataRef>(&designator.u)};
91     return dataRef ? std::get_if<parser::Name>(&dataRef->u) : nullptr;
92   }
93   void AddDataSharingAttributeObject(SymbolRef object) {
94     dataSharingAttributeObjects_.insert(object);
95   }
96   void ClearDataSharingAttributeObjects() {
97     dataSharingAttributeObjects_.clear();
98   }
99   bool HasDataSharingAttributeObject(const Symbol &);
100   const parser::Name &GetLoopIndex(const parser::DoConstruct &);
101   const parser::DoConstruct *GetDoConstructIf(
102       const parser::ExecutionPartConstruct &);
103   Symbol *DeclarePrivateAccessEntity(
104       const parser::Name &, Symbol::Flag, Scope &);
105   Symbol *DeclarePrivateAccessEntity(Symbol &, Symbol::Flag, Scope &);
106   Symbol *DeclareOrMarkOtherAccessEntity(const parser::Name &, Symbol::Flag);
107 
108   SymbolSet dataSharingAttributeObjects_; // on one directive
109   SemanticsContext &context_;
110   std::vector<DirContext> dirContext_; // used as a stack
111 };
112 
113 class AccAttributeVisitor : DirectiveAttributeVisitor<llvm::acc::Directive> {
114 public:
115   explicit AccAttributeVisitor(SemanticsContext &context)
116       : DirectiveAttributeVisitor(context) {}
117 
118   template <typename A> void Walk(const A &x) { parser::Walk(x, *this); }
119   template <typename A> bool Pre(const A &) { return true; }
120   template <typename A> void Post(const A &) {}
121 
122   bool Pre(const parser::OpenACCBlockConstruct &);
123   void Post(const parser::OpenACCBlockConstruct &) { PopContext(); }
124   bool Pre(const parser::OpenACCCombinedConstruct &);
125   void Post(const parser::OpenACCCombinedConstruct &) { PopContext(); }
126 
127   bool Pre(const parser::OpenACCDeclarativeConstruct &);
128   void Post(const parser::OpenACCDeclarativeConstruct &) { PopContext(); }
129 
130   bool Pre(const parser::OpenACCRoutineConstruct &);
131   bool Pre(const parser::AccBindClause &);
132   void Post(const parser::OpenACCStandaloneDeclarativeConstruct &);
133 
134   void Post(const parser::AccBeginBlockDirective &) {
135     GetContext().withinConstruct = true;
136   }
137 
138   bool Pre(const parser::OpenACCLoopConstruct &);
139   void Post(const parser::OpenACCLoopConstruct &) { PopContext(); }
140   void Post(const parser::AccLoopDirective &) {
141     GetContext().withinConstruct = true;
142   }
143 
144   bool Pre(const parser::OpenACCStandaloneConstruct &);
145   void Post(const parser::OpenACCStandaloneConstruct &) { PopContext(); }
146   void Post(const parser::AccStandaloneDirective &) {
147     GetContext().withinConstruct = true;
148   }
149 
150   bool Pre(const parser::OpenACCCacheConstruct &);
151   void Post(const parser::OpenACCCacheConstruct &) { PopContext(); }
152 
153   void Post(const parser::AccDefaultClause &);
154 
155   bool Pre(const parser::AccClause::Copy &x) {
156     ResolveAccObjectList(x.v, Symbol::Flag::AccCopyIn);
157     ResolveAccObjectList(x.v, Symbol::Flag::AccCopyOut);
158     return false;
159   }
160 
161   bool Pre(const parser::AccClause::Create &x) {
162     const auto &objectList{std::get<parser::AccObjectList>(x.v.t)};
163     ResolveAccObjectList(objectList, Symbol::Flag::AccCreate);
164     return false;
165   }
166 
167   bool Pre(const parser::AccClause::Copyin &x) {
168     const auto &objectList{std::get<parser::AccObjectList>(x.v.t)};
169     ResolveAccObjectList(objectList, Symbol::Flag::AccCopyIn);
170     return false;
171   }
172 
173   bool Pre(const parser::AccClause::Copyout &x) {
174     const auto &objectList{std::get<parser::AccObjectList>(x.v.t)};
175     ResolveAccObjectList(objectList, Symbol::Flag::AccCopyOut);
176     return false;
177   }
178 
179   bool Pre(const parser::AccClause::Present &x) {
180     ResolveAccObjectList(x.v, Symbol::Flag::AccPresent);
181     return false;
182   }
183   bool Pre(const parser::AccClause::Private &x) {
184     ResolveAccObjectList(x.v, Symbol::Flag::AccPrivate);
185     return false;
186   }
187   bool Pre(const parser::AccClause::Firstprivate &x) {
188     ResolveAccObjectList(x.v, Symbol::Flag::AccFirstPrivate);
189     return false;
190   }
191 
192   void Post(const parser::Name &);
193 
194 private:
195   std::int64_t GetAssociatedLoopLevelFromClauses(const parser::AccClauseList &);
196 
197   static constexpr Symbol::Flags dataSharingAttributeFlags{
198       Symbol::Flag::AccShared, Symbol::Flag::AccPrivate,
199       Symbol::Flag::AccPresent, Symbol::Flag::AccFirstPrivate,
200       Symbol::Flag::AccReduction};
201 
202   static constexpr Symbol::Flags dataMappingAttributeFlags{
203       Symbol::Flag::AccCreate, Symbol::Flag::AccCopyIn,
204       Symbol::Flag::AccCopyOut, Symbol::Flag::AccDelete};
205 
206   static constexpr Symbol::Flags accFlagsRequireNewSymbol{
207       Symbol::Flag::AccPrivate, Symbol::Flag::AccFirstPrivate,
208       Symbol::Flag::AccReduction};
209 
210   static constexpr Symbol::Flags accFlagsRequireMark{};
211 
212   void PrivatizeAssociatedLoopIndex(const parser::OpenACCLoopConstruct &);
213   void ResolveAccObjectList(const parser::AccObjectList &, Symbol::Flag);
214   void ResolveAccObject(const parser::AccObject &, Symbol::Flag);
215   Symbol *ResolveAcc(const parser::Name &, Symbol::Flag, Scope &);
216   Symbol *ResolveAcc(Symbol &, Symbol::Flag, Scope &);
217   Symbol *ResolveName(const parser::Name &);
218   Symbol *ResolveAccCommonBlockName(const parser::Name *);
219   Symbol *DeclareOrMarkOtherAccessEntity(const parser::Name &, Symbol::Flag);
220   Symbol *DeclareOrMarkOtherAccessEntity(Symbol &, Symbol::Flag);
221   void CheckMultipleAppearances(
222       const parser::Name &, const Symbol &, Symbol::Flag);
223   void AllowOnlyArrayAndSubArray(const parser::AccObjectList &objectList);
224   void DoNotAllowAssumedSizedArray(const parser::AccObjectList &objectList);
225 };
226 
227 // Data-sharing and Data-mapping attributes for data-refs in OpenMP construct
228 class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
229 public:
230   explicit OmpAttributeVisitor(SemanticsContext &context)
231       : DirectiveAttributeVisitor(context) {}
232 
233   template <typename A> void Walk(const A &x) { parser::Walk(x, *this); }
234   template <typename A> bool Pre(const A &) { return true; }
235   template <typename A> void Post(const A &) {}
236 
237   template <typename A> bool Pre(const parser::Statement<A> &statement) {
238     currentStatementSource_ = statement.source;
239     // Keep track of the labels in all the labelled statements
240     if (statement.label) {
241       auto label{statement.label.value()};
242       // Get the context to check if the labelled statement is in an
243       // enclosing OpenMP construct
244       std::optional<DirContext> thisContext{GetContextIf()};
245       targetLabels_.emplace(
246           label, std::make_pair(currentStatementSource_, thisContext));
247       // Check if a statement that causes a jump to the 'label'
248       // has already been encountered
249       auto range{sourceLabels_.equal_range(label)};
250       for (auto it{range.first}; it != range.second; ++it) {
251         // Check if both the statement with 'label' and the statement that
252         // causes a jump to the 'label' are in the same scope
253         CheckLabelContext(it->second.first, currentStatementSource_,
254             it->second.second, thisContext);
255       }
256     }
257     return true;
258   }
259 
260   bool Pre(const parser::InternalSubprogram &) {
261     // Clear the labels being tracked in the previous scope
262     ClearLabels();
263     return true;
264   }
265 
266   bool Pre(const parser::ModuleSubprogram &) {
267     // Clear the labels being tracked in the previous scope
268     ClearLabels();
269     return true;
270   }
271 
272   bool Pre(const parser::SpecificationPart &x) {
273     Walk(std::get<std::list<parser::OpenMPDeclarativeConstruct>>(x.t));
274     return true;
275   }
276 
277   bool Pre(const parser::StmtFunctionStmt &x) {
278     const auto &parsedExpr{std::get<parser::Scalar<parser::Expr>>(x.t)};
279     if (const auto *expr{GetExpr(parsedExpr)}) {
280       for (const Symbol &symbol : evaluate::CollectSymbols(*expr)) {
281         if (!IsStmtFunctionDummy(symbol)) {
282           stmtFunctionExprSymbols_.insert(symbol.GetUltimate());
283         }
284       }
285     }
286     return true;
287   }
288 
289   bool Pre(const parser::OpenMPBlockConstruct &);
290   void Post(const parser::OpenMPBlockConstruct &);
291 
292   void Post(const parser::OmpBeginBlockDirective &) {
293     GetContext().withinConstruct = true;
294   }
295 
296   bool Pre(const parser::OpenMPLoopConstruct &);
297   void Post(const parser::OpenMPLoopConstruct &) { PopContext(); }
298   void Post(const parser::OmpBeginLoopDirective &) {
299     GetContext().withinConstruct = true;
300   }
301   bool Pre(const parser::DoConstruct &);
302 
303   bool Pre(const parser::OpenMPSectionsConstruct &);
304   void Post(const parser::OpenMPSectionsConstruct &) { PopContext(); }
305 
306   bool Pre(const parser::OpenMPCriticalConstruct &);
307   void Post(const parser::OpenMPCriticalConstruct &) { PopContext(); }
308 
309   bool Pre(const parser::OpenMPDeclareSimdConstruct &x) {
310     PushContext(x.source, llvm::omp::Directive::OMPD_declare_simd);
311     const auto &name{std::get<std::optional<parser::Name>>(x.t)};
312     if (name) {
313       ResolveOmpName(*name, Symbol::Flag::OmpDeclareSimd);
314     }
315     return true;
316   }
317   void Post(const parser::OpenMPDeclareSimdConstruct &) { PopContext(); }
318   bool Pre(const parser::OpenMPThreadprivate &);
319   void Post(const parser::OpenMPThreadprivate &) { PopContext(); }
320 
321   // 2.15.3 Data-Sharing Attribute Clauses
322   void Post(const parser::OmpDefaultClause &);
323   bool Pre(const parser::OmpClause::Shared &x) {
324     ResolveOmpObjectList(x.v, Symbol::Flag::OmpShared);
325     return false;
326   }
327   bool Pre(const parser::OmpClause::Private &x) {
328     ResolveOmpObjectList(x.v, Symbol::Flag::OmpPrivate);
329     return false;
330   }
331   bool Pre(const parser::OmpAllocateClause &x) {
332     const auto &objectList{std::get<parser::OmpObjectList>(x.t)};
333     ResolveOmpObjectList(objectList, Symbol::Flag::OmpAllocate);
334     return false;
335   }
336   bool Pre(const parser::OmpClause::Firstprivate &x) {
337     ResolveOmpObjectList(x.v, Symbol::Flag::OmpFirstPrivate);
338     return false;
339   }
340   bool Pre(const parser::OmpClause::Lastprivate &x) {
341     ResolveOmpObjectList(x.v, Symbol::Flag::OmpLastPrivate);
342     return false;
343   }
344   bool Pre(const parser::OmpClause::Copyin &x) {
345     ResolveOmpObjectList(x.v, Symbol::Flag::OmpCopyIn);
346     return false;
347   }
348   bool Pre(const parser::OmpLinearClause &x) {
349     std::visit(common::visitors{
350                    [&](const parser::OmpLinearClause::WithoutModifier
351                            &linearWithoutModifier) {
352                      ResolveOmpNameList(
353                          linearWithoutModifier.names, Symbol::Flag::OmpLinear);
354                    },
355                    [&](const parser::OmpLinearClause::WithModifier
356                            &linearWithModifier) {
357                      ResolveOmpNameList(
358                          linearWithModifier.names, Symbol::Flag::OmpLinear);
359                    },
360                },
361         x.u);
362     return false;
363   }
364   bool Pre(const parser::OmpAlignedClause &x) {
365     const auto &alignedNameList{std::get<std::list<parser::Name>>(x.t)};
366     ResolveOmpNameList(alignedNameList, Symbol::Flag::OmpAligned);
367     return false;
368   }
369   void Post(const parser::Name &);
370 
371   // Keep track of labels in the statements that causes jumps to target labels
372   void Post(const parser::GotoStmt &gotoStmt) { CheckSourceLabel(gotoStmt.v); }
373   void Post(const parser::ComputedGotoStmt &computedGotoStmt) {
374     for (auto &label : std::get<std::list<parser::Label>>(computedGotoStmt.t)) {
375       CheckSourceLabel(label);
376     }
377   }
378   void Post(const parser::ArithmeticIfStmt &arithmeticIfStmt) {
379     CheckSourceLabel(std::get<1>(arithmeticIfStmt.t));
380     CheckSourceLabel(std::get<2>(arithmeticIfStmt.t));
381     CheckSourceLabel(std::get<3>(arithmeticIfStmt.t));
382   }
383   void Post(const parser::AssignedGotoStmt &assignedGotoStmt) {
384     for (auto &label : std::get<std::list<parser::Label>>(assignedGotoStmt.t)) {
385       CheckSourceLabel(label);
386     }
387   }
388   void Post(const parser::AltReturnSpec &altReturnSpec) {
389     CheckSourceLabel(altReturnSpec.v);
390   }
391   void Post(const parser::ErrLabel &errLabel) { CheckSourceLabel(errLabel.v); }
392   void Post(const parser::EndLabel &endLabel) { CheckSourceLabel(endLabel.v); }
393   void Post(const parser::EorLabel &eorLabel) { CheckSourceLabel(eorLabel.v); }
394 
395   const parser::OmpClause *associatedClause{nullptr};
396   void SetAssociatedClause(const parser::OmpClause &c) {
397     associatedClause = &c;
398   }
399   const parser::OmpClause *GetAssociatedClause() { return associatedClause; }
400 
401 private:
402   std::int64_t GetAssociatedLoopLevelFromClauses(const parser::OmpClauseList &);
403 
404   static constexpr Symbol::Flags dataSharingAttributeFlags{
405       Symbol::Flag::OmpShared, Symbol::Flag::OmpPrivate,
406       Symbol::Flag::OmpFirstPrivate, Symbol::Flag::OmpLastPrivate,
407       Symbol::Flag::OmpReduction, Symbol::Flag::OmpLinear};
408 
409   static constexpr Symbol::Flags privateDataSharingAttributeFlags{
410       Symbol::Flag::OmpPrivate, Symbol::Flag::OmpFirstPrivate,
411       Symbol::Flag::OmpLastPrivate};
412 
413   static constexpr Symbol::Flags ompFlagsRequireNewSymbol{
414       Symbol::Flag::OmpPrivate, Symbol::Flag::OmpLinear,
415       Symbol::Flag::OmpFirstPrivate, Symbol::Flag::OmpLastPrivate,
416       Symbol::Flag::OmpReduction};
417 
418   static constexpr Symbol::Flags ompFlagsRequireMark{
419       Symbol::Flag::OmpThreadprivate};
420 
421   static constexpr Symbol::Flags dataCopyingAttributeFlags{
422       Symbol::Flag::OmpCopyIn};
423 
424   std::vector<const parser::Name *> allocateNames_; // on one directive
425   SymbolSet privateDataSharingAttributeObjects_; // on one directive
426   SymbolSet stmtFunctionExprSymbols_;
427   std::multimap<const parser::Label,
428       std::pair<parser::CharBlock, std::optional<DirContext>>>
429       sourceLabels_;
430   std::map<const parser::Label,
431       std::pair<parser::CharBlock, std::optional<DirContext>>>
432       targetLabels_;
433   parser::CharBlock currentStatementSource_;
434 
435   void AddAllocateName(const parser::Name *&object) {
436     allocateNames_.push_back(object);
437   }
438   void ClearAllocateNames() { allocateNames_.clear(); }
439 
440   void AddPrivateDataSharingAttributeObjects(SymbolRef object) {
441     privateDataSharingAttributeObjects_.insert(object);
442   }
443   void ClearPrivateDataSharingAttributeObjects() {
444     privateDataSharingAttributeObjects_.clear();
445   }
446 
447   // Predetermined DSA rules
448   void PrivatizeAssociatedLoopIndexAndCheckLoopLevel(
449       const parser::OpenMPLoopConstruct &);
450   void ResolveSeqLoopIndexInParallelOrTaskConstruct(const parser::Name &);
451 
452   void ResolveOmpObjectList(const parser::OmpObjectList &, Symbol::Flag);
453   void ResolveOmpObject(const parser::OmpObject &, Symbol::Flag);
454   Symbol *ResolveOmp(const parser::Name &, Symbol::Flag, Scope &);
455   Symbol *ResolveOmp(Symbol &, Symbol::Flag, Scope &);
456   Symbol *ResolveOmpCommonBlockName(const parser::Name *);
457   void ResolveOmpNameList(const std::list<parser::Name> &, Symbol::Flag);
458   void ResolveOmpName(const parser::Name &, Symbol::Flag);
459   Symbol *ResolveName(const parser::Name *);
460   Symbol *DeclareOrMarkOtherAccessEntity(const parser::Name &, Symbol::Flag);
461   Symbol *DeclareOrMarkOtherAccessEntity(Symbol &, Symbol::Flag);
462   void CheckMultipleAppearances(
463       const parser::Name &, const Symbol &, Symbol::Flag);
464 
465   void CheckDataCopyingClause(
466       const parser::Name &, const Symbol &, Symbol::Flag);
467 
468   void CheckAssocLoopLevel(std::int64_t level, const parser::OmpClause *clause);
469   void CheckPrivateDSAObject(
470       const parser::Name &, const Symbol &, Symbol::Flag);
471   void CheckSourceLabel(const parser::Label &);
472   void CheckLabelContext(const parser::CharBlock, const parser::CharBlock,
473       std::optional<DirContext>, std::optional<DirContext>);
474   void ClearLabels() {
475     sourceLabels_.clear();
476     targetLabels_.clear();
477   };
478 };
479 
480 template <typename T>
481 bool DirectiveAttributeVisitor<T>::HasDataSharingAttributeObject(
482     const Symbol &object) {
483   auto it{dataSharingAttributeObjects_.find(object)};
484   return it != dataSharingAttributeObjects_.end();
485 }
486 
487 template <typename T>
488 const parser::Name &DirectiveAttributeVisitor<T>::GetLoopIndex(
489     const parser::DoConstruct &x) {
490   using Bounds = parser::LoopControl::Bounds;
491   return std::get<Bounds>(x.GetLoopControl()->u).name.thing;
492 }
493 
494 template <typename T>
495 const parser::DoConstruct *DirectiveAttributeVisitor<T>::GetDoConstructIf(
496     const parser::ExecutionPartConstruct &x) {
497   return parser::Unwrap<parser::DoConstruct>(x);
498 }
499 
500 template <typename T>
501 Symbol *DirectiveAttributeVisitor<T>::DeclarePrivateAccessEntity(
502     const parser::Name &name, Symbol::Flag flag, Scope &scope) {
503   if (!name.symbol) {
504     return nullptr; // not resolved by Name Resolution step, do nothing
505   }
506   name.symbol = DeclarePrivateAccessEntity(*name.symbol, flag, scope);
507   return name.symbol;
508 }
509 
510 template <typename T>
511 Symbol *DirectiveAttributeVisitor<T>::DeclarePrivateAccessEntity(
512     Symbol &object, Symbol::Flag flag, Scope &scope) {
513   if (object.owner() != currScope()) {
514     auto &symbol{MakeAssocSymbol(object.name(), object, scope)};
515     symbol.set(flag);
516     return &symbol;
517   } else {
518     object.set(flag);
519     return &object;
520   }
521 }
522 
523 bool AccAttributeVisitor::Pre(const parser::OpenACCBlockConstruct &x) {
524   const auto &beginBlockDir{std::get<parser::AccBeginBlockDirective>(x.t)};
525   const auto &blockDir{std::get<parser::AccBlockDirective>(beginBlockDir.t)};
526   switch (blockDir.v) {
527   case llvm::acc::Directive::ACCD_data:
528   case llvm::acc::Directive::ACCD_host_data:
529   case llvm::acc::Directive::ACCD_kernels:
530   case llvm::acc::Directive::ACCD_parallel:
531   case llvm::acc::Directive::ACCD_serial:
532     PushContext(blockDir.source, blockDir.v);
533     break;
534   default:
535     break;
536   }
537   ClearDataSharingAttributeObjects();
538   return true;
539 }
540 
541 bool AccAttributeVisitor::Pre(const parser::OpenACCDeclarativeConstruct &x) {
542   if (const auto *declConstruct{
543           std::get_if<parser::OpenACCStandaloneDeclarativeConstruct>(&x.u)}) {
544     const auto &declDir{
545         std::get<parser::AccDeclarativeDirective>(declConstruct->t)};
546     PushContext(declDir.source, llvm::acc::Directive::ACCD_declare);
547   } else if (const auto *routineConstruct{
548                  std::get_if<parser::OpenACCRoutineConstruct>(&x.u)}) {
549     const auto &verbatim{std::get<parser::Verbatim>(routineConstruct->t)};
550     PushContext(verbatim.source, llvm::acc::Directive::ACCD_routine);
551   }
552   ClearDataSharingAttributeObjects();
553   return true;
554 }
555 
556 static const parser::AccObjectList &GetAccObjectList(
557     const parser::AccClause &clause) {
558   if (const auto *copyClause =
559           std::get_if<Fortran::parser::AccClause::Copy>(&clause.u)) {
560     return copyClause->v;
561   } else if (const auto *createClause =
562                  std::get_if<Fortran::parser::AccClause::Create>(&clause.u)) {
563     const Fortran::parser::AccObjectListWithModifier &listWithModifier =
564         createClause->v;
565     const Fortran::parser::AccObjectList &accObjectList =
566         std::get<Fortran::parser::AccObjectList>(listWithModifier.t);
567     return accObjectList;
568   } else if (const auto *copyinClause =
569                  std::get_if<Fortran::parser::AccClause::Copyin>(&clause.u)) {
570     const Fortran::parser::AccObjectListWithModifier &listWithModifier =
571         copyinClause->v;
572     const Fortran::parser::AccObjectList &accObjectList =
573         std::get<Fortran::parser::AccObjectList>(listWithModifier.t);
574     return accObjectList;
575   } else if (const auto *copyoutClause =
576                  std::get_if<Fortran::parser::AccClause::Copyout>(&clause.u)) {
577     const Fortran::parser::AccObjectListWithModifier &listWithModifier =
578         copyoutClause->v;
579     const Fortran::parser::AccObjectList &accObjectList =
580         std::get<Fortran::parser::AccObjectList>(listWithModifier.t);
581     return accObjectList;
582   } else if (const auto *presentClause =
583                  std::get_if<Fortran::parser::AccClause::Present>(&clause.u)) {
584     return presentClause->v;
585   } else if (const auto *deviceptrClause =
586                  std::get_if<Fortran::parser::AccClause::Deviceptr>(
587                      &clause.u)) {
588     return deviceptrClause->v;
589   } else if (const auto *deviceResidentClause =
590                  std::get_if<Fortran::parser::AccClause::DeviceResident>(
591                      &clause.u)) {
592     return deviceResidentClause->v;
593   } else if (const auto *linkClause =
594                  std::get_if<Fortran::parser::AccClause::Link>(&clause.u)) {
595     return linkClause->v;
596   } else {
597     llvm_unreachable("Clause without object list!");
598   }
599 }
600 
601 void AccAttributeVisitor::Post(
602     const parser::OpenACCStandaloneDeclarativeConstruct &x) {
603   const auto &clauseList = std::get<parser::AccClauseList>(x.t);
604   for (const auto &clause : clauseList.v) {
605     // Restriction - line 2414
606     DoNotAllowAssumedSizedArray(GetAccObjectList(clause));
607   }
608 }
609 
610 bool AccAttributeVisitor::Pre(const parser::OpenACCLoopConstruct &x) {
611   const auto &beginDir{std::get<parser::AccBeginLoopDirective>(x.t)};
612   const auto &loopDir{std::get<parser::AccLoopDirective>(beginDir.t)};
613   const auto &clauseList{std::get<parser::AccClauseList>(beginDir.t)};
614   if (loopDir.v == llvm::acc::Directive::ACCD_loop) {
615     PushContext(loopDir.source, loopDir.v);
616   }
617   ClearDataSharingAttributeObjects();
618   SetContextAssociatedLoopLevel(GetAssociatedLoopLevelFromClauses(clauseList));
619   PrivatizeAssociatedLoopIndex(x);
620   return true;
621 }
622 
623 bool AccAttributeVisitor::Pre(const parser::OpenACCStandaloneConstruct &x) {
624   const auto &standaloneDir{std::get<parser::AccStandaloneDirective>(x.t)};
625   switch (standaloneDir.v) {
626   case llvm::acc::Directive::ACCD_enter_data:
627   case llvm::acc::Directive::ACCD_exit_data:
628   case llvm::acc::Directive::ACCD_init:
629   case llvm::acc::Directive::ACCD_set:
630   case llvm::acc::Directive::ACCD_shutdown:
631   case llvm::acc::Directive::ACCD_update:
632     PushContext(standaloneDir.source, standaloneDir.v);
633     break;
634   default:
635     break;
636   }
637   ClearDataSharingAttributeObjects();
638   return true;
639 }
640 
641 Symbol *AccAttributeVisitor::ResolveName(const parser::Name &name) {
642   Symbol *prev{currScope().FindSymbol(name.source)};
643   if (prev != name.symbol) {
644     name.symbol = prev;
645   }
646   return prev;
647 }
648 
649 bool AccAttributeVisitor::Pre(const parser::OpenACCRoutineConstruct &x) {
650   const auto &optName{std::get<std::optional<parser::Name>>(x.t)};
651   if (optName) {
652     if (!ResolveName(*optName))
653       context_.Say((*optName).source,
654           "No function or subroutine declared for '%s'"_err_en_US,
655           (*optName).source);
656   }
657   return true;
658 }
659 
660 bool AccAttributeVisitor::Pre(const parser::AccBindClause &x) {
661   if (const auto *name{std::get_if<parser::Name>(&x.u)}) {
662     if (!ResolveName(*name))
663       context_.Say(name->source,
664           "No function or subroutine declared for '%s'"_err_en_US,
665           name->source);
666   }
667   return true;
668 }
669 
670 bool AccAttributeVisitor::Pre(const parser::OpenACCCombinedConstruct &x) {
671   const auto &beginBlockDir{std::get<parser::AccBeginCombinedDirective>(x.t)};
672   const auto &combinedDir{
673       std::get<parser::AccCombinedDirective>(beginBlockDir.t)};
674   switch (combinedDir.v) {
675   case llvm::acc::Directive::ACCD_kernels_loop:
676   case llvm::acc::Directive::ACCD_parallel_loop:
677   case llvm::acc::Directive::ACCD_serial_loop:
678     PushContext(combinedDir.source, combinedDir.v);
679     break;
680   default:
681     break;
682   }
683   ClearDataSharingAttributeObjects();
684   return true;
685 }
686 
687 static bool IsLastNameArray(const parser::Designator &designator) {
688   const auto &name{GetLastName(designator)};
689   const evaluate::DataRef dataRef{*(name.symbol)};
690   return std::visit(
691       common::visitors{
692           [](const evaluate::SymbolRef &ref) { return ref->Rank() > 0; },
693           [](const evaluate::ArrayRef &aref) {
694             return aref.base().IsSymbol() ||
695                 aref.base().GetComponent().base().Rank() == 0;
696           },
697           [](const auto &) { return false; },
698       },
699       dataRef.u);
700 }
701 
702 void AccAttributeVisitor::AllowOnlyArrayAndSubArray(
703     const parser::AccObjectList &objectList) {
704   for (const auto &accObject : objectList.v) {
705     std::visit(
706         common::visitors{
707             [&](const parser::Designator &designator) {
708               if (!IsLastNameArray(designator))
709                 context_.Say(designator.source,
710                     "Only array element or subarray are allowed in %s directive"_err_en_US,
711                     parser::ToUpperCaseLetters(
712                         llvm::acc::getOpenACCDirectiveName(
713                             GetContext().directive)
714                             .str()));
715             },
716             [&](const auto &name) {
717               context_.Say(name.source,
718                   "Only array element or subarray are allowed in %s directive"_err_en_US,
719                   parser::ToUpperCaseLetters(
720                       llvm::acc::getOpenACCDirectiveName(GetContext().directive)
721                           .str()));
722             },
723         },
724         accObject.u);
725   }
726 }
727 
728 void AccAttributeVisitor::DoNotAllowAssumedSizedArray(
729     const parser::AccObjectList &objectList) {
730   for (const auto &accObject : objectList.v) {
731     std::visit(
732         common::visitors{
733             [&](const parser::Designator &designator) {
734               const auto &name{GetLastName(designator)};
735               if (name.symbol && semantics::IsAssumedSizeArray(*name.symbol))
736                 context_.Say(designator.source,
737                     "Assumed-size dummy arrays may not appear on the %s "
738                     "directive"_err_en_US,
739                     parser::ToUpperCaseLetters(
740                         llvm::acc::getOpenACCDirectiveName(
741                             GetContext().directive)
742                             .str()));
743             },
744             [&](const auto &name) {
745 
746             },
747         },
748         accObject.u);
749   }
750 }
751 
752 bool AccAttributeVisitor::Pre(const parser::OpenACCCacheConstruct &x) {
753   const auto &verbatim{std::get<parser::Verbatim>(x.t)};
754   PushContext(verbatim.source, llvm::acc::Directive::ACCD_cache);
755   ClearDataSharingAttributeObjects();
756 
757   const auto &objectListWithModifier =
758       std::get<parser::AccObjectListWithModifier>(x.t);
759   const auto &objectList =
760       std::get<Fortran::parser::AccObjectList>(objectListWithModifier.t);
761 
762   // 2.10 Cache directive restriction: A var in a cache directive must be a
763   // single array element or a simple subarray.
764   AllowOnlyArrayAndSubArray(objectList);
765 
766   return true;
767 }
768 
769 std::int64_t AccAttributeVisitor::GetAssociatedLoopLevelFromClauses(
770     const parser::AccClauseList &x) {
771   std::int64_t collapseLevel{0};
772   for (const auto &clause : x.v) {
773     if (const auto *collapseClause{
774             std::get_if<parser::AccClause::Collapse>(&clause.u)}) {
775       if (const auto v{EvaluateInt64(context_, collapseClause->v)}) {
776         collapseLevel = *v;
777       }
778     }
779   }
780 
781   if (collapseLevel) {
782     return collapseLevel;
783   }
784   return 1; // default is outermost loop
785 }
786 
787 void AccAttributeVisitor::PrivatizeAssociatedLoopIndex(
788     const parser::OpenACCLoopConstruct &x) {
789   std::int64_t level{GetContext().associatedLoopLevel};
790   if (level <= 0) { // collpase value was negative or 0
791     return;
792   }
793   Symbol::Flag ivDSA{Symbol::Flag::AccPrivate};
794 
795   const auto &outer{std::get<std::optional<parser::DoConstruct>>(x.t)};
796   for (const parser::DoConstruct *loop{&*outer}; loop && level > 0; --level) {
797     // go through all the nested do-loops and resolve index variables
798     const parser::Name &iv{GetLoopIndex(*loop)};
799     if (auto *symbol{ResolveAcc(iv, ivDSA, currScope())}) {
800       symbol->set(Symbol::Flag::AccPreDetermined);
801       iv.symbol = symbol; // adjust the symbol within region
802       AddToContextObjectWithDSA(*symbol, ivDSA);
803     }
804 
805     const auto &block{std::get<parser::Block>(loop->t)};
806     const auto it{block.begin()};
807     loop = it != block.end() ? GetDoConstructIf(*it) : nullptr;
808   }
809   CHECK(level == 0);
810 }
811 
812 void AccAttributeVisitor::Post(const parser::AccDefaultClause &x) {
813   if (!dirContext_.empty()) {
814     switch (x.v) {
815     case llvm::acc::DefaultValue::ACC_Default_present:
816       SetContextDefaultDSA(Symbol::Flag::AccPresent);
817       break;
818     case llvm::acc::DefaultValue::ACC_Default_none:
819       SetContextDefaultDSA(Symbol::Flag::AccNone);
820       break;
821     }
822   }
823 }
824 
825 // For OpenACC constructs, check all the data-refs within the constructs
826 // and adjust the symbol for each Name if necessary
827 void AccAttributeVisitor::Post(const parser::Name &name) {
828   auto *symbol{name.symbol};
829   if (symbol && !dirContext_.empty() && GetContext().withinConstruct) {
830     if (!symbol->owner().IsDerivedType() && !symbol->has<ProcEntityDetails>() &&
831         !IsObjectWithDSA(*symbol)) {
832       if (Symbol * found{currScope().FindSymbol(name.source)}) {
833         if (symbol != found) {
834           name.symbol = found; // adjust the symbol within region
835         } else if (GetContext().defaultDSA == Symbol::Flag::AccNone) {
836           // 2.5.14.
837           context_.Say(name.source,
838               "The DEFAULT(NONE) clause requires that '%s' must be listed in "
839               "a data-mapping clause"_err_en_US,
840               symbol->name());
841         }
842       }
843     }
844   } // within OpenACC construct
845 }
846 
847 Symbol *AccAttributeVisitor::ResolveAccCommonBlockName(
848     const parser::Name *name) {
849   if (!name) {
850     return nullptr;
851   } else if (auto *prev{
852                  GetContext().scope.parent().FindCommonBlock(name->source)}) {
853     name->symbol = prev;
854     return prev;
855   } else {
856     return nullptr;
857   }
858 }
859 
860 void AccAttributeVisitor::ResolveAccObjectList(
861     const parser::AccObjectList &accObjectList, Symbol::Flag accFlag) {
862   for (const auto &accObject : accObjectList.v) {
863     ResolveAccObject(accObject, accFlag);
864   }
865 }
866 
867 void AccAttributeVisitor::ResolveAccObject(
868     const parser::AccObject &accObject, Symbol::Flag accFlag) {
869   std::visit(
870       common::visitors{
871           [&](const parser::Designator &designator) {
872             if (const auto *name{GetDesignatorNameIfDataRef(designator)}) {
873               if (auto *symbol{ResolveAcc(*name, accFlag, currScope())}) {
874                 AddToContextObjectWithDSA(*symbol, accFlag);
875                 if (dataSharingAttributeFlags.test(accFlag)) {
876                   CheckMultipleAppearances(*name, *symbol, accFlag);
877                 }
878               }
879             } else {
880               // Array sections to be changed to substrings as needed
881               if (AnalyzeExpr(context_, designator)) {
882                 if (std::holds_alternative<parser::Substring>(designator.u)) {
883                   context_.Say(designator.source,
884                       "Substrings are not allowed on OpenACC "
885                       "directives or clauses"_err_en_US);
886                 }
887               }
888               // other checks, more TBD
889             }
890           },
891           [&](const parser::Name &name) { // common block
892             if (auto *symbol{ResolveAccCommonBlockName(&name)}) {
893               CheckMultipleAppearances(
894                   name, *symbol, Symbol::Flag::AccCommonBlock);
895               for (auto &object : symbol->get<CommonBlockDetails>().objects()) {
896                 if (auto *resolvedObject{
897                         ResolveAcc(*object, accFlag, currScope())}) {
898                   AddToContextObjectWithDSA(*resolvedObject, accFlag);
899                 }
900               }
901             } else {
902               context_.Say(name.source,
903                   "COMMON block must be declared in the same scoping unit "
904                   "in which the OpenACC directive or clause appears"_err_en_US);
905             }
906           },
907       },
908       accObject.u);
909 }
910 
911 Symbol *AccAttributeVisitor::ResolveAcc(
912     const parser::Name &name, Symbol::Flag accFlag, Scope &scope) {
913   if (accFlagsRequireNewSymbol.test(accFlag)) {
914     return DeclarePrivateAccessEntity(name, accFlag, scope);
915   } else {
916     return DeclareOrMarkOtherAccessEntity(name, accFlag);
917   }
918 }
919 
920 Symbol *AccAttributeVisitor::ResolveAcc(
921     Symbol &symbol, Symbol::Flag accFlag, Scope &scope) {
922   if (accFlagsRequireNewSymbol.test(accFlag)) {
923     return DeclarePrivateAccessEntity(symbol, accFlag, scope);
924   } else {
925     return DeclareOrMarkOtherAccessEntity(symbol, accFlag);
926   }
927 }
928 
929 Symbol *AccAttributeVisitor::DeclareOrMarkOtherAccessEntity(
930     const parser::Name &name, Symbol::Flag accFlag) {
931   Symbol *prev{currScope().FindSymbol(name.source)};
932   if (!name.symbol || !prev) {
933     return nullptr;
934   } else if (prev != name.symbol) {
935     name.symbol = prev;
936   }
937   return DeclareOrMarkOtherAccessEntity(*prev, accFlag);
938 }
939 
940 Symbol *AccAttributeVisitor::DeclareOrMarkOtherAccessEntity(
941     Symbol &object, Symbol::Flag accFlag) {
942   if (accFlagsRequireMark.test(accFlag)) {
943     object.set(accFlag);
944   }
945   return &object;
946 }
947 
948 static bool WithMultipleAppearancesAccException(
949     const Symbol &symbol, Symbol::Flag flag) {
950   return false; // Place holder
951 }
952 
953 void AccAttributeVisitor::CheckMultipleAppearances(
954     const parser::Name &name, const Symbol &symbol, Symbol::Flag accFlag) {
955   const auto *target{&symbol};
956   if (accFlagsRequireNewSymbol.test(accFlag)) {
957     if (const auto *details{symbol.detailsIf<HostAssocDetails>()}) {
958       target = &details->symbol();
959     }
960   }
961   if (HasDataSharingAttributeObject(*target) &&
962       !WithMultipleAppearancesAccException(symbol, accFlag)) {
963     context_.Say(name.source,
964         "'%s' appears in more than one data-sharing clause "
965         "on the same OpenACC directive"_err_en_US,
966         name.ToString());
967   } else {
968     AddDataSharingAttributeObject(*target);
969   }
970 }
971 
972 bool OmpAttributeVisitor::Pre(const parser::OpenMPBlockConstruct &x) {
973   const auto &beginBlockDir{std::get<parser::OmpBeginBlockDirective>(x.t)};
974   const auto &beginDir{std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
975   switch (beginDir.v) {
976   case llvm::omp::Directive::OMPD_master:
977   case llvm::omp::Directive::OMPD_ordered:
978   case llvm::omp::Directive::OMPD_parallel:
979   case llvm::omp::Directive::OMPD_single:
980   case llvm::omp::Directive::OMPD_target:
981   case llvm::omp::Directive::OMPD_target_data:
982   case llvm::omp::Directive::OMPD_task:
983   case llvm::omp::Directive::OMPD_teams:
984   case llvm::omp::Directive::OMPD_workshare:
985   case llvm::omp::Directive::OMPD_parallel_workshare:
986   case llvm::omp::Directive::OMPD_target_teams:
987   case llvm::omp::Directive::OMPD_target_parallel:
988   case llvm::omp::Directive::OMPD_taskgroup:
989     PushContext(beginDir.source, beginDir.v);
990     break;
991   default:
992     // TODO others
993     break;
994   }
995   ClearDataSharingAttributeObjects();
996   ClearPrivateDataSharingAttributeObjects();
997   ClearAllocateNames();
998   return true;
999 }
1000 
1001 void OmpAttributeVisitor::Post(const parser::OpenMPBlockConstruct &x) {
1002   const auto &beginBlockDir{std::get<parser::OmpBeginBlockDirective>(x.t)};
1003   const auto &beginDir{std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
1004   switch (beginDir.v) {
1005   case llvm::omp::Directive::OMPD_parallel:
1006   case llvm::omp::Directive::OMPD_single:
1007   case llvm::omp::Directive::OMPD_target:
1008   case llvm::omp::Directive::OMPD_task:
1009   case llvm::omp::Directive::OMPD_teams:
1010   case llvm::omp::Directive::OMPD_parallel_workshare:
1011   case llvm::omp::Directive::OMPD_target_teams:
1012   case llvm::omp::Directive::OMPD_target_parallel: {
1013     bool hasPrivate;
1014     for (const auto *allocName : allocateNames_) {
1015       hasPrivate = false;
1016       for (auto privateObj : privateDataSharingAttributeObjects_) {
1017         const Symbol &symbolPrivate{*privateObj};
1018         if (allocName->source == symbolPrivate.name()) {
1019           hasPrivate = true;
1020           break;
1021         }
1022       }
1023       if (!hasPrivate) {
1024         context_.Say(allocName->source,
1025             "The ALLOCATE clause requires that '%s' must be listed in a "
1026             "private "
1027             "data-sharing attribute clause on the same directive"_err_en_US,
1028             allocName->ToString());
1029       }
1030     }
1031     break;
1032   }
1033   default:
1034     break;
1035   }
1036   PopContext();
1037 }
1038 
1039 bool OmpAttributeVisitor::Pre(const parser::OpenMPLoopConstruct &x) {
1040   const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)};
1041   const auto &beginDir{std::get<parser::OmpLoopDirective>(beginLoopDir.t)};
1042   const auto &clauseList{std::get<parser::OmpClauseList>(beginLoopDir.t)};
1043   switch (beginDir.v) {
1044   case llvm::omp::Directive::OMPD_distribute:
1045   case llvm::omp::Directive::OMPD_distribute_parallel_do:
1046   case llvm::omp::Directive::OMPD_distribute_parallel_do_simd:
1047   case llvm::omp::Directive::OMPD_distribute_simd:
1048   case llvm::omp::Directive::OMPD_do:
1049   case llvm::omp::Directive::OMPD_do_simd:
1050   case llvm::omp::Directive::OMPD_parallel_do:
1051   case llvm::omp::Directive::OMPD_parallel_do_simd:
1052   case llvm::omp::Directive::OMPD_simd:
1053   case llvm::omp::Directive::OMPD_target_parallel_do:
1054   case llvm::omp::Directive::OMPD_target_parallel_do_simd:
1055   case llvm::omp::Directive::OMPD_target_teams_distribute:
1056   case llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do:
1057   case llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do_simd:
1058   case llvm::omp::Directive::OMPD_target_teams_distribute_simd:
1059   case llvm::omp::Directive::OMPD_target_simd:
1060   case llvm::omp::Directive::OMPD_taskloop:
1061   case llvm::omp::Directive::OMPD_taskloop_simd:
1062   case llvm::omp::Directive::OMPD_teams_distribute:
1063   case llvm::omp::Directive::OMPD_teams_distribute_parallel_do:
1064   case llvm::omp::Directive::OMPD_teams_distribute_parallel_do_simd:
1065   case llvm::omp::Directive::OMPD_teams_distribute_simd:
1066     PushContext(beginDir.source, beginDir.v);
1067     break;
1068   default:
1069     break;
1070   }
1071   ClearDataSharingAttributeObjects();
1072   SetContextAssociatedLoopLevel(GetAssociatedLoopLevelFromClauses(clauseList));
1073   PrivatizeAssociatedLoopIndexAndCheckLoopLevel(x);
1074   return true;
1075 }
1076 
1077 void OmpAttributeVisitor::ResolveSeqLoopIndexInParallelOrTaskConstruct(
1078     const parser::Name &iv) {
1079   auto targetIt{dirContext_.rbegin()};
1080   for (;; ++targetIt) {
1081     if (targetIt == dirContext_.rend()) {
1082       return;
1083     }
1084     if (llvm::omp::parallelSet.test(targetIt->directive) ||
1085         llvm::omp::taskGeneratingSet.test(targetIt->directive)) {
1086       break;
1087     }
1088   }
1089   if (auto *symbol{ResolveOmp(iv, Symbol::Flag::OmpPrivate, targetIt->scope)}) {
1090     targetIt++;
1091     symbol->set(Symbol::Flag::OmpPreDetermined);
1092     iv.symbol = symbol; // adjust the symbol within region
1093     for (auto it{dirContext_.rbegin()}; it != targetIt; ++it) {
1094       AddToContextObjectWithDSA(*symbol, Symbol::Flag::OmpPrivate, *it);
1095     }
1096   }
1097 }
1098 
1099 // [OMP-4.5]2.15.1.1 Data-sharing Attribute Rules - Predetermined
1100 //   - A loop iteration variable for a sequential loop in a parallel
1101 //     or task generating construct is private in the innermost such
1102 //     construct that encloses the loop
1103 // Loop iteration variables are not well defined for DO WHILE loop.
1104 // Use of DO CONCURRENT inside OpenMP construct is unspecified behavior
1105 // till OpenMP-5.0 standard.
1106 // In above both cases we skip the privatization of iteration variables.
1107 bool OmpAttributeVisitor::Pre(const parser::DoConstruct &x) {
1108   // TODO:[OpenMP 5.1] DO CONCURRENT indices are private
1109   if (x.IsDoNormal()) {
1110     if (!dirContext_.empty() && GetContext().withinConstruct) {
1111       if (const auto &iv{GetLoopIndex(x)}; iv.symbol) {
1112         if (!iv.symbol->test(Symbol::Flag::OmpPreDetermined)) {
1113           ResolveSeqLoopIndexInParallelOrTaskConstruct(iv);
1114         } else {
1115           // TODO: conflict checks with explicitly determined DSA
1116         }
1117       }
1118     }
1119   }
1120   return true;
1121 }
1122 
1123 std::int64_t OmpAttributeVisitor::GetAssociatedLoopLevelFromClauses(
1124     const parser::OmpClauseList &x) {
1125   std::int64_t orderedLevel{0};
1126   std::int64_t collapseLevel{0};
1127 
1128   const parser::OmpClause *ordClause{nullptr};
1129   const parser::OmpClause *collClause{nullptr};
1130 
1131   for (const auto &clause : x.v) {
1132     if (const auto *orderedClause{
1133             std::get_if<parser::OmpClause::Ordered>(&clause.u)}) {
1134       if (const auto v{EvaluateInt64(context_, orderedClause->v)}) {
1135         orderedLevel = *v;
1136       }
1137       ordClause = &clause;
1138     }
1139     if (const auto *collapseClause{
1140             std::get_if<parser::OmpClause::Collapse>(&clause.u)}) {
1141       if (const auto v{EvaluateInt64(context_, collapseClause->v)}) {
1142         collapseLevel = *v;
1143       }
1144       collClause = &clause;
1145     }
1146   }
1147 
1148   if (orderedLevel && (!collapseLevel || orderedLevel >= collapseLevel)) {
1149     SetAssociatedClause(*ordClause);
1150     return orderedLevel;
1151   } else if (!orderedLevel && collapseLevel) {
1152     SetAssociatedClause(*collClause);
1153     return collapseLevel;
1154   } // orderedLevel < collapseLevel is an error handled in structural checks
1155   return 1; // default is outermost loop
1156 }
1157 
1158 // 2.15.1.1 Data-sharing Attribute Rules - Predetermined
1159 //   - The loop iteration variable(s) in the associated do-loop(s) of a do,
1160 //     parallel do, taskloop, or distribute construct is (are) private.
1161 //   - The loop iteration variable in the associated do-loop of a simd construct
1162 //     with just one associated do-loop is linear with a linear-step that is the
1163 //     increment of the associated do-loop.
1164 //   - The loop iteration variables in the associated do-loops of a simd
1165 //     construct with multiple associated do-loops are lastprivate.
1166 void OmpAttributeVisitor::PrivatizeAssociatedLoopIndexAndCheckLoopLevel(
1167     const parser::OpenMPLoopConstruct &x) {
1168   std::int64_t level{GetContext().associatedLoopLevel};
1169   if (level <= 0) {
1170     return;
1171   }
1172   Symbol::Flag ivDSA;
1173   if (!llvm::omp::simdSet.test(GetContext().directive)) {
1174     ivDSA = Symbol::Flag::OmpPrivate;
1175   } else if (level == 1) {
1176     ivDSA = Symbol::Flag::OmpLinear;
1177   } else {
1178     ivDSA = Symbol::Flag::OmpLastPrivate;
1179   }
1180 
1181   const auto &outer{std::get<std::optional<parser::DoConstruct>>(x.t)};
1182   for (const parser::DoConstruct *loop{&*outer}; loop && level > 0; --level) {
1183     // go through all the nested do-loops and resolve index variables
1184     const parser::Name &iv{GetLoopIndex(*loop)};
1185     if (auto *symbol{ResolveOmp(iv, ivDSA, currScope())}) {
1186       symbol->set(Symbol::Flag::OmpPreDetermined);
1187       iv.symbol = symbol; // adjust the symbol within region
1188       AddToContextObjectWithDSA(*symbol, ivDSA);
1189     }
1190 
1191     const auto &block{std::get<parser::Block>(loop->t)};
1192     const auto it{block.begin()};
1193     loop = it != block.end() ? GetDoConstructIf(*it) : nullptr;
1194   }
1195   CheckAssocLoopLevel(level, GetAssociatedClause());
1196 }
1197 void OmpAttributeVisitor::CheckAssocLoopLevel(
1198     std::int64_t level, const parser::OmpClause *clause) {
1199   if (clause && level != 0) {
1200     context_.Say(clause->source,
1201         "The value of the parameter in the COLLAPSE or ORDERED clause must"
1202         " not be larger than the number of nested loops"
1203         " following the construct."_err_en_US);
1204   }
1205 }
1206 
1207 bool OmpAttributeVisitor::Pre(const parser::OpenMPSectionsConstruct &x) {
1208   const auto &beginSectionsDir{
1209       std::get<parser::OmpBeginSectionsDirective>(x.t)};
1210   const auto &beginDir{
1211       std::get<parser::OmpSectionsDirective>(beginSectionsDir.t)};
1212   switch (beginDir.v) {
1213   case llvm::omp::Directive::OMPD_parallel_sections:
1214   case llvm::omp::Directive::OMPD_sections:
1215     PushContext(beginDir.source, beginDir.v);
1216     break;
1217   default:
1218     break;
1219   }
1220   ClearDataSharingAttributeObjects();
1221   return true;
1222 }
1223 
1224 bool OmpAttributeVisitor::Pre(const parser::OpenMPCriticalConstruct &x) {
1225   const auto &criticalDir{std::get<parser::OmpCriticalDirective>(x.t)};
1226   PushContext(criticalDir.source, llvm::omp::Directive::OMPD_critical);
1227   return true;
1228 }
1229 
1230 bool OmpAttributeVisitor::Pre(const parser::OpenMPThreadprivate &x) {
1231   PushContext(x.source, llvm::omp::Directive::OMPD_threadprivate);
1232   const auto &list{std::get<parser::OmpObjectList>(x.t)};
1233   ResolveOmpObjectList(list, Symbol::Flag::OmpThreadprivate);
1234   return true;
1235 }
1236 
1237 void OmpAttributeVisitor::Post(const parser::OmpDefaultClause &x) {
1238   if (!dirContext_.empty()) {
1239     switch (x.v) {
1240     case parser::OmpDefaultClause::Type::Private:
1241       SetContextDefaultDSA(Symbol::Flag::OmpPrivate);
1242       break;
1243     case parser::OmpDefaultClause::Type::Firstprivate:
1244       SetContextDefaultDSA(Symbol::Flag::OmpFirstPrivate);
1245       break;
1246     case parser::OmpDefaultClause::Type::Shared:
1247       SetContextDefaultDSA(Symbol::Flag::OmpShared);
1248       break;
1249     case parser::OmpDefaultClause::Type::None:
1250       SetContextDefaultDSA(Symbol::Flag::OmpNone);
1251       break;
1252     }
1253   }
1254 }
1255 
1256 // For OpenMP constructs, check all the data-refs within the constructs
1257 // and adjust the symbol for each Name if necessary
1258 void OmpAttributeVisitor::Post(const parser::Name &name) {
1259   auto *symbol{name.symbol};
1260   if (symbol && !dirContext_.empty() && GetContext().withinConstruct) {
1261     if (!symbol->owner().IsDerivedType() && !symbol->has<ProcEntityDetails>() &&
1262         !IsObjectWithDSA(*symbol)) {
1263       // TODO: create a separate function to go through the rules for
1264       //       predetermined, explicitly determined, and implicitly
1265       //       determined data-sharing attributes (2.15.1.1).
1266       if (Symbol * found{currScope().FindSymbol(name.source)}) {
1267         if (symbol != found) {
1268           name.symbol = found; // adjust the symbol within region
1269         } else if (GetContext().defaultDSA == Symbol::Flag::OmpNone) {
1270           context_.Say(name.source,
1271               "The DEFAULT(NONE) clause requires that '%s' must be listed in "
1272               "a data-sharing attribute clause"_err_en_US,
1273               symbol->name());
1274         }
1275       }
1276     }
1277   } // within OpenMP construct
1278 }
1279 
1280 Symbol *OmpAttributeVisitor::ResolveName(const parser::Name *name) {
1281   if (auto *resolvedSymbol{
1282           name ? GetContext().scope.FindSymbol(name->source) : nullptr}) {
1283     name->symbol = resolvedSymbol;
1284     return resolvedSymbol;
1285   } else {
1286     return nullptr;
1287   }
1288 }
1289 
1290 void OmpAttributeVisitor::ResolveOmpName(
1291     const parser::Name &name, Symbol::Flag ompFlag) {
1292   if (ResolveName(&name)) {
1293     if (auto *resolvedSymbol{ResolveOmp(name, ompFlag, currScope())}) {
1294       if (dataSharingAttributeFlags.test(ompFlag)) {
1295         AddToContextObjectWithDSA(*resolvedSymbol, ompFlag);
1296       }
1297     }
1298   }
1299 }
1300 
1301 void OmpAttributeVisitor::ResolveOmpNameList(
1302     const std::list<parser::Name> &nameList, Symbol::Flag ompFlag) {
1303   for (const auto &name : nameList) {
1304     ResolveOmpName(name, ompFlag);
1305   }
1306 }
1307 
1308 Symbol *OmpAttributeVisitor::ResolveOmpCommonBlockName(
1309     const parser::Name *name) {
1310   if (auto *prev{name
1311               ? GetContext().scope.parent().FindCommonBlock(name->source)
1312               : nullptr}) {
1313     name->symbol = prev;
1314     return prev;
1315   }
1316   // Check if the Common Block is declared in the current scope
1317   if (auto *commonBlockSymbol{
1318           name ? GetContext().scope.FindCommonBlock(name->source) : nullptr}) {
1319     name->symbol = commonBlockSymbol;
1320     return commonBlockSymbol;
1321   }
1322   return nullptr;
1323 }
1324 
1325 void OmpAttributeVisitor::ResolveOmpObjectList(
1326     const parser::OmpObjectList &ompObjectList, Symbol::Flag ompFlag) {
1327   for (const auto &ompObject : ompObjectList.v) {
1328     ResolveOmpObject(ompObject, ompFlag);
1329   }
1330 }
1331 
1332 void OmpAttributeVisitor::ResolveOmpObject(
1333     const parser::OmpObject &ompObject, Symbol::Flag ompFlag) {
1334   std::visit(
1335       common::visitors{
1336           [&](const parser::Designator &designator) {
1337             if (const auto *name{GetDesignatorNameIfDataRef(designator)}) {
1338               if (auto *symbol{ResolveOmp(*name, ompFlag, currScope())}) {
1339                 if (dataCopyingAttributeFlags.test(ompFlag)) {
1340                   CheckDataCopyingClause(*name, *symbol, ompFlag);
1341                 } else {
1342                   AddToContextObjectWithDSA(*symbol, ompFlag);
1343                   if (dataSharingAttributeFlags.test(ompFlag)) {
1344                     CheckMultipleAppearances(*name, *symbol, ompFlag);
1345                   }
1346                   if (privateDataSharingAttributeFlags.test(ompFlag)) {
1347                     CheckPrivateDSAObject(*name, *symbol, ompFlag);
1348                   }
1349 
1350                   if (ompFlag == Symbol::Flag::OmpAllocate) {
1351                     AddAllocateName(name);
1352                   }
1353                 }
1354               }
1355             } else {
1356               // Array sections to be changed to substrings as needed
1357               if (AnalyzeExpr(context_, designator)) {
1358                 if (std::holds_alternative<parser::Substring>(designator.u)) {
1359                   context_.Say(designator.source,
1360                       "Substrings are not allowed on OpenMP "
1361                       "directives or clauses"_err_en_US);
1362                 }
1363               }
1364               // other checks, more TBD
1365             }
1366           },
1367           [&](const parser::Name &name) { // common block
1368             if (auto *symbol{ResolveOmpCommonBlockName(&name)}) {
1369               if (!dataCopyingAttributeFlags.test(ompFlag)) {
1370                 CheckMultipleAppearances(
1371                     name, *symbol, Symbol::Flag::OmpCommonBlock);
1372               }
1373               // 2.15.3 When a named common block appears in a list, it has the
1374               // same meaning as if every explicit member of the common block
1375               // appeared in the list
1376               for (auto &object : symbol->get<CommonBlockDetails>().objects()) {
1377                 if (auto *resolvedObject{
1378                         ResolveOmp(*object, ompFlag, currScope())}) {
1379                   if (dataCopyingAttributeFlags.test(ompFlag)) {
1380                     CheckDataCopyingClause(name, *resolvedObject, ompFlag);
1381                   } else {
1382                     AddToContextObjectWithDSA(*resolvedObject, ompFlag);
1383                   }
1384                 }
1385               }
1386             } else {
1387               context_.Say(name.source, // 2.15.3
1388                   "COMMON block must be declared in the same scoping unit "
1389                   "in which the OpenMP directive or clause appears"_err_en_US);
1390             }
1391           },
1392       },
1393       ompObject.u);
1394 }
1395 
1396 Symbol *OmpAttributeVisitor::ResolveOmp(
1397     const parser::Name &name, Symbol::Flag ompFlag, Scope &scope) {
1398   if (ompFlagsRequireNewSymbol.test(ompFlag)) {
1399     return DeclarePrivateAccessEntity(name, ompFlag, scope);
1400   } else {
1401     return DeclareOrMarkOtherAccessEntity(name, ompFlag);
1402   }
1403 }
1404 
1405 Symbol *OmpAttributeVisitor::ResolveOmp(
1406     Symbol &symbol, Symbol::Flag ompFlag, Scope &scope) {
1407   if (ompFlagsRequireNewSymbol.test(ompFlag)) {
1408     return DeclarePrivateAccessEntity(symbol, ompFlag, scope);
1409   } else {
1410     return DeclareOrMarkOtherAccessEntity(symbol, ompFlag);
1411   }
1412 }
1413 
1414 Symbol *OmpAttributeVisitor::DeclareOrMarkOtherAccessEntity(
1415     const parser::Name &name, Symbol::Flag ompFlag) {
1416   Symbol *prev{currScope().FindSymbol(name.source)};
1417   if (!name.symbol || !prev) {
1418     return nullptr;
1419   } else if (prev != name.symbol) {
1420     name.symbol = prev;
1421   }
1422   return DeclareOrMarkOtherAccessEntity(*prev, ompFlag);
1423 }
1424 
1425 Symbol *OmpAttributeVisitor::DeclareOrMarkOtherAccessEntity(
1426     Symbol &object, Symbol::Flag ompFlag) {
1427   if (ompFlagsRequireMark.test(ompFlag)) {
1428     object.set(ompFlag);
1429   }
1430   return &object;
1431 }
1432 
1433 static bool WithMultipleAppearancesOmpException(
1434     const Symbol &symbol, Symbol::Flag flag) {
1435   return (flag == Symbol::Flag::OmpFirstPrivate &&
1436              symbol.test(Symbol::Flag::OmpLastPrivate)) ||
1437       (flag == Symbol::Flag::OmpLastPrivate &&
1438           symbol.test(Symbol::Flag::OmpFirstPrivate));
1439 }
1440 
1441 void OmpAttributeVisitor::CheckMultipleAppearances(
1442     const parser::Name &name, const Symbol &symbol, Symbol::Flag ompFlag) {
1443   const auto *target{&symbol};
1444   if (ompFlagsRequireNewSymbol.test(ompFlag)) {
1445     if (const auto *details{symbol.detailsIf<HostAssocDetails>()}) {
1446       target = &details->symbol();
1447     }
1448   }
1449   if (HasDataSharingAttributeObject(*target) &&
1450       !WithMultipleAppearancesOmpException(symbol, ompFlag)) {
1451     context_.Say(name.source,
1452         "'%s' appears in more than one data-sharing clause "
1453         "on the same OpenMP directive"_err_en_US,
1454         name.ToString());
1455   } else {
1456     AddDataSharingAttributeObject(*target);
1457     if (privateDataSharingAttributeFlags.test(ompFlag)) {
1458       AddPrivateDataSharingAttributeObjects(*target);
1459     }
1460   }
1461 }
1462 
1463 void ResolveAccParts(
1464     SemanticsContext &context, const parser::ProgramUnit &node) {
1465   if (context.IsEnabled(common::LanguageFeature::OpenACC)) {
1466     AccAttributeVisitor{context}.Walk(node);
1467   }
1468 }
1469 
1470 void ResolveOmpParts(
1471     SemanticsContext &context, const parser::ProgramUnit &node) {
1472   if (context.IsEnabled(common::LanguageFeature::OpenMP)) {
1473     OmpAttributeVisitor{context}.Walk(node);
1474     if (!context.AnyFatalError()) {
1475       // The data-sharing attribute of the loop iteration variable for a
1476       // sequential loop (2.15.1.1) can only be determined when visiting
1477       // the corresponding DoConstruct, a second walk is to adjust the
1478       // symbols for all the data-refs of that loop iteration variable
1479       // prior to the DoConstruct.
1480       OmpAttributeVisitor{context}.Walk(node);
1481     }
1482   }
1483 }
1484 
1485 void OmpAttributeVisitor::CheckDataCopyingClause(
1486     const parser::Name &name, const Symbol &symbol, Symbol::Flag ompFlag) {
1487   const auto *checkSymbol{&symbol};
1488   if (ompFlag == Symbol::Flag::OmpCopyIn) {
1489     if (const auto *details{symbol.detailsIf<HostAssocDetails>()})
1490       checkSymbol = &details->symbol();
1491 
1492     // List of items/objects that can appear in a 'copyin' clause must be
1493     // 'threadprivate'
1494     if (!checkSymbol->test(Symbol::Flag::OmpThreadprivate))
1495       context_.Say(name.source,
1496           "Non-THREADPRIVATE object '%s' in COPYIN clause"_err_en_US,
1497           checkSymbol->name());
1498   }
1499 }
1500 
1501 void OmpAttributeVisitor::CheckPrivateDSAObject(
1502     const parser::Name &name, const Symbol &symbol, Symbol::Flag ompFlag) {
1503   const auto &ultimateSymbol{symbol.GetUltimate()};
1504   llvm::StringRef clauseName{"PRIVATE"};
1505   if (ompFlag == Symbol::Flag::OmpFirstPrivate)
1506     clauseName = "FIRSTPRIVATE";
1507   else if (ompFlag == Symbol::Flag::OmpLastPrivate)
1508     clauseName = "LASTPRIVATE";
1509 
1510   if (ultimateSymbol.test(Symbol::Flag::InNamelist)) {
1511     context_.Say(name.source,
1512         "Variable '%s' in NAMELIST cannot be in a %s clause"_err_en_US,
1513         name.ToString(), clauseName.str());
1514   }
1515 
1516   if (stmtFunctionExprSymbols_.find(ultimateSymbol) !=
1517       stmtFunctionExprSymbols_.end()) {
1518     context_.Say(name.source,
1519         "Variable '%s' in STATEMENT FUNCTION expression cannot be in a "
1520         "%s clause"_err_en_US,
1521         name.ToString(), clauseName.str());
1522   }
1523 }
1524 
1525 void OmpAttributeVisitor::CheckSourceLabel(const parser::Label &label) {
1526   // Get the context to check if the statement causing a jump to the 'label' is
1527   // in an enclosing OpenMP construct
1528   std::optional<DirContext> thisContext{GetContextIf()};
1529   sourceLabels_.emplace(
1530       label, std::make_pair(currentStatementSource_, thisContext));
1531   // Check if the statement with 'label' to which a jump is being introduced
1532   // has already been encountered
1533   auto it{targetLabels_.find(label)};
1534   if (it != targetLabels_.end()) {
1535     // Check if both the statement with 'label' and the statement that causes a
1536     // jump to the 'label' are in the same scope
1537     CheckLabelContext(currentStatementSource_, it->second.first, thisContext,
1538         it->second.second);
1539   }
1540 }
1541 
1542 // Check for invalid branch into or out of OpenMP structured blocks
1543 void OmpAttributeVisitor::CheckLabelContext(const parser::CharBlock source,
1544     const parser::CharBlock target, std::optional<DirContext> sourceContext,
1545     std::optional<DirContext> targetContext) {
1546   if (targetContext &&
1547       (!sourceContext ||
1548           (sourceContext->scope != targetContext->scope &&
1549               !DoesScopeContain(
1550                   &targetContext->scope, sourceContext->scope)))) {
1551     context_
1552         .Say(source, "invalid branch into an OpenMP structured block"_err_en_US)
1553         .Attach(target, "In the enclosing %s directive branched into"_en_US,
1554             parser::ToUpperCaseLetters(
1555                 llvm::omp::getOpenMPDirectiveName(targetContext->directive)
1556                     .str()));
1557   }
1558   if (sourceContext &&
1559       (!targetContext ||
1560           (sourceContext->scope != targetContext->scope &&
1561               !DoesScopeContain(
1562                   &sourceContext->scope, targetContext->scope)))) {
1563     context_
1564         .Say(source,
1565             "invalid branch leaving an OpenMP structured block"_err_en_US)
1566         .Attach(target, "Outside the enclosing %s directive"_en_US,
1567             parser::ToUpperCaseLetters(
1568                 llvm::omp::getOpenMPDirectiveName(sourceContext->directive)
1569                     .str()));
1570   }
1571 }
1572 
1573 } // namespace Fortran::semantics
1574