1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 /// This file implements semantic analysis for OpenMP directives and
11 /// clauses.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "TreeTransform.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTMutationListener.h"
18 #include "clang/AST/CXXInheritance.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclOpenMP.h"
22 #include "clang/AST/StmtCXX.h"
23 #include "clang/AST/StmtOpenMP.h"
24 #include "clang/AST/StmtVisitor.h"
25 #include "clang/Basic/OpenMPKinds.h"
26 #include "clang/Sema/Initialization.h"
27 #include "clang/Sema/Lookup.h"
28 #include "clang/Sema/Scope.h"
29 #include "clang/Sema/ScopeInfo.h"
30 #include "clang/Sema/SemaInternal.h"
31 #include "llvm/ADT/PointerEmbeddedInt.h"
32 using namespace clang;
33 
34 //===----------------------------------------------------------------------===//
35 // Stack of data-sharing attributes for variables
36 //===----------------------------------------------------------------------===//
37 
38 static const Expr *checkMapClauseExpressionBase(
39     Sema &SemaRef, Expr *E,
40     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
41     OpenMPClauseKind CKind, bool NoDiagnose);
42 
43 namespace {
44 /// Default data sharing attributes, which can be applied to directive.
45 enum DefaultDataSharingAttributes {
46   DSA_unspecified = 0, /// Data sharing attribute not specified.
47   DSA_none = 1 << 0,   /// Default data sharing attribute 'none'.
48   DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
49 };
50 
51 /// Attributes of the defaultmap clause.
52 enum DefaultMapAttributes {
53   DMA_unspecified,   /// Default mapping is not specified.
54   DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
55 };
56 
57 /// Stack for tracking declarations used in OpenMP directives and
58 /// clauses and their data-sharing attributes.
59 class DSAStackTy {
60 public:
61   struct DSAVarData {
62     OpenMPDirectiveKind DKind = OMPD_unknown;
63     OpenMPClauseKind CKind = OMPC_unknown;
64     const Expr *RefExpr = nullptr;
65     DeclRefExpr *PrivateCopy = nullptr;
66     SourceLocation ImplicitDSALoc;
67     DSAVarData() = default;
68     DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
69                const Expr *RefExpr, DeclRefExpr *PrivateCopy,
70                SourceLocation ImplicitDSALoc)
71         : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
72           PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
73   };
74   using OperatorOffsetTy =
75       llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
76   using DoacrossDependMapTy =
77       llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
78 
79 private:
80   struct DSAInfo {
81     OpenMPClauseKind Attributes = OMPC_unknown;
82     /// Pointer to a reference expression and a flag which shows that the
83     /// variable is marked as lastprivate(true) or not (false).
84     llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
85     DeclRefExpr *PrivateCopy = nullptr;
86   };
87   using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
88   using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
89   using LCDeclInfo = std::pair<unsigned, VarDecl *>;
90   using LoopControlVariablesMapTy =
91       llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
92   /// Struct that associates a component with the clause kind where they are
93   /// found.
94   struct MappedExprComponentTy {
95     OMPClauseMappableExprCommon::MappableExprComponentLists Components;
96     OpenMPClauseKind Kind = OMPC_unknown;
97   };
98   using MappedExprComponentsTy =
99       llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
100   using CriticalsWithHintsTy =
101       llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
102   struct ReductionData {
103     using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
104     SourceRange ReductionRange;
105     llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
106     ReductionData() = default;
107     void set(BinaryOperatorKind BO, SourceRange RR) {
108       ReductionRange = RR;
109       ReductionOp = BO;
110     }
111     void set(const Expr *RefExpr, SourceRange RR) {
112       ReductionRange = RR;
113       ReductionOp = RefExpr;
114     }
115   };
116   using DeclReductionMapTy =
117       llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
118 
119   struct SharingMapTy {
120     DeclSAMapTy SharingMap;
121     DeclReductionMapTy ReductionMap;
122     AlignedMapTy AlignedMap;
123     MappedExprComponentsTy MappedExprComponents;
124     LoopControlVariablesMapTy LCVMap;
125     DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
126     SourceLocation DefaultAttrLoc;
127     DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
128     SourceLocation DefaultMapAttrLoc;
129     OpenMPDirectiveKind Directive = OMPD_unknown;
130     DeclarationNameInfo DirectiveName;
131     Scope *CurScope = nullptr;
132     SourceLocation ConstructLoc;
133     /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
134     /// get the data (loop counters etc.) about enclosing loop-based construct.
135     /// This data is required during codegen.
136     DoacrossDependMapTy DoacrossDepends;
137     /// first argument (Expr *) contains optional argument of the
138     /// 'ordered' clause, the second one is true if the regions has 'ordered'
139     /// clause, false otherwise.
140     llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
141     unsigned AssociatedLoops = 1;
142     const Decl *PossiblyLoopCounter = nullptr;
143     bool NowaitRegion = false;
144     bool CancelRegion = false;
145     bool LoopStart = false;
146     SourceLocation InnerTeamsRegionLoc;
147     /// Reference to the taskgroup task_reduction reference expression.
148     Expr *TaskgroupReductionRef = nullptr;
149     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
150                  Scope *CurScope, SourceLocation Loc)
151         : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
152           ConstructLoc(Loc) {}
153     SharingMapTy() = default;
154   };
155 
156   using StackTy = SmallVector<SharingMapTy, 4>;
157 
158   /// Stack of used declaration and their data-sharing attributes.
159   DeclSAMapTy Threadprivates;
160   const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
161   SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
162   /// true, if check for DSA must be from parent directive, false, if
163   /// from current directive.
164   OpenMPClauseKind ClauseKindMode = OMPC_unknown;
165   Sema &SemaRef;
166   bool ForceCapturing = false;
167   /// true if all the vaiables in the target executable directives must be
168   /// captured by reference.
169   bool ForceCaptureByReferenceInTargetExecutable = false;
170   CriticalsWithHintsTy Criticals;
171 
172   using iterator = StackTy::const_reverse_iterator;
173 
174   DSAVarData getDSA(iterator &Iter, ValueDecl *D) const;
175 
176   /// Checks if the variable is a local for OpenMP region.
177   bool isOpenMPLocal(VarDecl *D, iterator Iter) const;
178 
179   bool isStackEmpty() const {
180     return Stack.empty() ||
181            Stack.back().second != CurrentNonCapturingFunctionScope ||
182            Stack.back().first.empty();
183   }
184 
185   /// Vector of previously declared requires directives
186   SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
187 
188 public:
189   explicit DSAStackTy(Sema &S) : SemaRef(S) {}
190 
191   bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
192   OpenMPClauseKind getClauseParsingMode() const {
193     assert(isClauseParsingMode() && "Must be in clause parsing mode.");
194     return ClauseKindMode;
195   }
196   void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
197 
198   bool isForceVarCapturing() const { return ForceCapturing; }
199   void setForceVarCapturing(bool V) { ForceCapturing = V; }
200 
201   void setForceCaptureByReferenceInTargetExecutable(bool V) {
202     ForceCaptureByReferenceInTargetExecutable = V;
203   }
204   bool isForceCaptureByReferenceInTargetExecutable() const {
205     return ForceCaptureByReferenceInTargetExecutable;
206   }
207 
208   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
209             Scope *CurScope, SourceLocation Loc) {
210     if (Stack.empty() ||
211         Stack.back().second != CurrentNonCapturingFunctionScope)
212       Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
213     Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
214     Stack.back().first.back().DefaultAttrLoc = Loc;
215   }
216 
217   void pop() {
218     assert(!Stack.back().first.empty() &&
219            "Data-sharing attributes stack is empty!");
220     Stack.back().first.pop_back();
221   }
222 
223   /// Marks that we're started loop parsing.
224   void loopInit() {
225     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
226            "Expected loop-based directive.");
227     Stack.back().first.back().LoopStart = true;
228   }
229   /// Start capturing of the variables in the loop context.
230   void loopStart() {
231     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
232            "Expected loop-based directive.");
233     Stack.back().first.back().LoopStart = false;
234   }
235   /// true, if variables are captured, false otherwise.
236   bool isLoopStarted() const {
237     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
238            "Expected loop-based directive.");
239     return !Stack.back().first.back().LoopStart;
240   }
241   /// Marks (or clears) declaration as possibly loop counter.
242   void resetPossibleLoopCounter(const Decl *D = nullptr) {
243     Stack.back().first.back().PossiblyLoopCounter =
244         D ? D->getCanonicalDecl() : D;
245   }
246   /// Gets the possible loop counter decl.
247   const Decl *getPossiblyLoopCunter() const {
248     return Stack.back().first.back().PossiblyLoopCounter;
249   }
250   /// Start new OpenMP region stack in new non-capturing function.
251   void pushFunction() {
252     const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
253     assert(!isa<CapturingScopeInfo>(CurFnScope));
254     CurrentNonCapturingFunctionScope = CurFnScope;
255   }
256   /// Pop region stack for non-capturing function.
257   void popFunction(const FunctionScopeInfo *OldFSI) {
258     if (!Stack.empty() && Stack.back().second == OldFSI) {
259       assert(Stack.back().first.empty());
260       Stack.pop_back();
261     }
262     CurrentNonCapturingFunctionScope = nullptr;
263     for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
264       if (!isa<CapturingScopeInfo>(FSI)) {
265         CurrentNonCapturingFunctionScope = FSI;
266         break;
267       }
268     }
269   }
270 
271   void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
272     Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
273   }
274   const std::pair<const OMPCriticalDirective *, llvm::APSInt>
275   getCriticalWithHint(const DeclarationNameInfo &Name) const {
276     auto I = Criticals.find(Name.getAsString());
277     if (I != Criticals.end())
278       return I->second;
279     return std::make_pair(nullptr, llvm::APSInt());
280   }
281   /// If 'aligned' declaration for given variable \a D was not seen yet,
282   /// add it and return NULL; otherwise return previous occurrence's expression
283   /// for diagnostics.
284   const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
285 
286   /// Register specified variable as loop control variable.
287   void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
288   /// Check if the specified variable is a loop control variable for
289   /// current region.
290   /// \return The index of the loop control variable in the list of associated
291   /// for-loops (from outer to inner).
292   const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
293   /// Check if the specified variable is a loop control variable for
294   /// parent region.
295   /// \return The index of the loop control variable in the list of associated
296   /// for-loops (from outer to inner).
297   const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
298   /// Get the loop control variable for the I-th loop (or nullptr) in
299   /// parent directive.
300   const ValueDecl *getParentLoopControlVariable(unsigned I) const;
301 
302   /// Adds explicit data sharing attribute to the specified declaration.
303   void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
304               DeclRefExpr *PrivateCopy = nullptr);
305 
306   /// Adds additional information for the reduction items with the reduction id
307   /// represented as an operator.
308   void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
309                                  BinaryOperatorKind BOK);
310   /// Adds additional information for the reduction items with the reduction id
311   /// represented as reduction identifier.
312   void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
313                                  const Expr *ReductionRef);
314   /// Returns the location and reduction operation from the innermost parent
315   /// region for the given \p D.
316   const DSAVarData
317   getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
318                                    BinaryOperatorKind &BOK,
319                                    Expr *&TaskgroupDescriptor) const;
320   /// Returns the location and reduction operation from the innermost parent
321   /// region for the given \p D.
322   const DSAVarData
323   getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
324                                    const Expr *&ReductionRef,
325                                    Expr *&TaskgroupDescriptor) const;
326   /// Return reduction reference expression for the current taskgroup.
327   Expr *getTaskgroupReductionRef() const {
328     assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
329            "taskgroup reference expression requested for non taskgroup "
330            "directive.");
331     return Stack.back().first.back().TaskgroupReductionRef;
332   }
333   /// Checks if the given \p VD declaration is actually a taskgroup reduction
334   /// descriptor variable at the \p Level of OpenMP regions.
335   bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
336     return Stack.back().first[Level].TaskgroupReductionRef &&
337            cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
338                    ->getDecl() == VD;
339   }
340 
341   /// Returns data sharing attributes from top of the stack for the
342   /// specified declaration.
343   const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
344   /// Returns data-sharing attributes for the specified declaration.
345   const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
346   /// Checks if the specified variables has data-sharing attributes which
347   /// match specified \a CPred predicate in any directive which matches \a DPred
348   /// predicate.
349   const DSAVarData
350   hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
351          const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
352          bool FromParent) const;
353   /// Checks if the specified variables has data-sharing attributes which
354   /// match specified \a CPred predicate in any innermost directive which
355   /// matches \a DPred predicate.
356   const DSAVarData
357   hasInnermostDSA(ValueDecl *D,
358                   const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
359                   const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
360                   bool FromParent) const;
361   /// Checks if the specified variables has explicit data-sharing
362   /// attributes which match specified \a CPred predicate at the specified
363   /// OpenMP region.
364   bool hasExplicitDSA(const ValueDecl *D,
365                       const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
366                       unsigned Level, bool NotLastprivate = false) const;
367 
368   /// Returns true if the directive at level \Level matches in the
369   /// specified \a DPred predicate.
370   bool hasExplicitDirective(
371       const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
372       unsigned Level) const;
373 
374   /// Finds a directive which matches specified \a DPred predicate.
375   bool hasDirective(
376       const llvm::function_ref<bool(
377           OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
378           DPred,
379       bool FromParent) const;
380 
381   /// Returns currently analyzed directive.
382   OpenMPDirectiveKind getCurrentDirective() const {
383     return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
384   }
385   /// Returns directive kind at specified level.
386   OpenMPDirectiveKind getDirective(unsigned Level) const {
387     assert(!isStackEmpty() && "No directive at specified level.");
388     return Stack.back().first[Level].Directive;
389   }
390   /// Returns parent directive.
391   OpenMPDirectiveKind getParentDirective() const {
392     if (isStackEmpty() || Stack.back().first.size() == 1)
393       return OMPD_unknown;
394     return std::next(Stack.back().first.rbegin())->Directive;
395   }
396 
397   /// Add requires decl to internal vector
398   void addRequiresDecl(OMPRequiresDecl *RD) {
399     RequiresDecls.push_back(RD);
400   }
401 
402   /// Checks for a duplicate clause amongst previously declared requires
403   /// directives
404   bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
405     bool IsDuplicate = false;
406     for (OMPClause *CNew : ClauseList) {
407       for (const OMPRequiresDecl *D : RequiresDecls) {
408         for (const OMPClause *CPrev : D->clauselists()) {
409           if (CNew->getClauseKind() == CPrev->getClauseKind()) {
410             SemaRef.Diag(CNew->getBeginLoc(),
411                          diag::err_omp_requires_clause_redeclaration)
412                 << getOpenMPClauseName(CNew->getClauseKind());
413             SemaRef.Diag(CPrev->getBeginLoc(),
414                          diag::note_omp_requires_previous_clause)
415                 << getOpenMPClauseName(CPrev->getClauseKind());
416             IsDuplicate = true;
417           }
418         }
419       }
420     }
421     return IsDuplicate;
422   }
423 
424   /// Set default data sharing attribute to none.
425   void setDefaultDSANone(SourceLocation Loc) {
426     assert(!isStackEmpty());
427     Stack.back().first.back().DefaultAttr = DSA_none;
428     Stack.back().first.back().DefaultAttrLoc = Loc;
429   }
430   /// Set default data sharing attribute to shared.
431   void setDefaultDSAShared(SourceLocation Loc) {
432     assert(!isStackEmpty());
433     Stack.back().first.back().DefaultAttr = DSA_shared;
434     Stack.back().first.back().DefaultAttrLoc = Loc;
435   }
436   /// Set default data mapping attribute to 'tofrom:scalar'.
437   void setDefaultDMAToFromScalar(SourceLocation Loc) {
438     assert(!isStackEmpty());
439     Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
440     Stack.back().first.back().DefaultMapAttrLoc = Loc;
441   }
442 
443   DefaultDataSharingAttributes getDefaultDSA() const {
444     return isStackEmpty() ? DSA_unspecified
445                           : Stack.back().first.back().DefaultAttr;
446   }
447   SourceLocation getDefaultDSALocation() const {
448     return isStackEmpty() ? SourceLocation()
449                           : Stack.back().first.back().DefaultAttrLoc;
450   }
451   DefaultMapAttributes getDefaultDMA() const {
452     return isStackEmpty() ? DMA_unspecified
453                           : Stack.back().first.back().DefaultMapAttr;
454   }
455   DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
456     return Stack.back().first[Level].DefaultMapAttr;
457   }
458   SourceLocation getDefaultDMALocation() const {
459     return isStackEmpty() ? SourceLocation()
460                           : Stack.back().first.back().DefaultMapAttrLoc;
461   }
462 
463   /// Checks if the specified variable is a threadprivate.
464   bool isThreadPrivate(VarDecl *D) {
465     const DSAVarData DVar = getTopDSA(D, false);
466     return isOpenMPThreadPrivate(DVar.CKind);
467   }
468 
469   /// Marks current region as ordered (it has an 'ordered' clause).
470   void setOrderedRegion(bool IsOrdered, const Expr *Param,
471                         OMPOrderedClause *Clause) {
472     assert(!isStackEmpty());
473     if (IsOrdered)
474       Stack.back().first.back().OrderedRegion.emplace(Param, Clause);
475     else
476       Stack.back().first.back().OrderedRegion.reset();
477   }
478   /// Returns true, if region is ordered (has associated 'ordered' clause),
479   /// false - otherwise.
480   bool isOrderedRegion() const {
481     if (isStackEmpty())
482       return false;
483     return Stack.back().first.rbegin()->OrderedRegion.hasValue();
484   }
485   /// Returns optional parameter for the ordered region.
486   std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
487     if (isStackEmpty() ||
488         !Stack.back().first.rbegin()->OrderedRegion.hasValue())
489       return std::make_pair(nullptr, nullptr);
490     return Stack.back().first.rbegin()->OrderedRegion.getValue();
491   }
492   /// Returns true, if parent region is ordered (has associated
493   /// 'ordered' clause), false - otherwise.
494   bool isParentOrderedRegion() const {
495     if (isStackEmpty() || Stack.back().first.size() == 1)
496       return false;
497     return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue();
498   }
499   /// Returns optional parameter for the ordered region.
500   std::pair<const Expr *, OMPOrderedClause *>
501   getParentOrderedRegionParam() const {
502     if (isStackEmpty() || Stack.back().first.size() == 1 ||
503         !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue())
504       return std::make_pair(nullptr, nullptr);
505     return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue();
506   }
507   /// Marks current region as nowait (it has a 'nowait' clause).
508   void setNowaitRegion(bool IsNowait = true) {
509     assert(!isStackEmpty());
510     Stack.back().first.back().NowaitRegion = IsNowait;
511   }
512   /// Returns true, if parent region is nowait (has associated
513   /// 'nowait' clause), false - otherwise.
514   bool isParentNowaitRegion() const {
515     if (isStackEmpty() || Stack.back().first.size() == 1)
516       return false;
517     return std::next(Stack.back().first.rbegin())->NowaitRegion;
518   }
519   /// Marks parent region as cancel region.
520   void setParentCancelRegion(bool Cancel = true) {
521     if (!isStackEmpty() && Stack.back().first.size() > 1) {
522       auto &StackElemRef = *std::next(Stack.back().first.rbegin());
523       StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
524     }
525   }
526   /// Return true if current region has inner cancel construct.
527   bool isCancelRegion() const {
528     return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
529   }
530 
531   /// Set collapse value for the region.
532   void setAssociatedLoops(unsigned Val) {
533     assert(!isStackEmpty());
534     Stack.back().first.back().AssociatedLoops = Val;
535   }
536   /// Return collapse value for region.
537   unsigned getAssociatedLoops() const {
538     return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
539   }
540 
541   /// Marks current target region as one with closely nested teams
542   /// region.
543   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
544     if (!isStackEmpty() && Stack.back().first.size() > 1) {
545       std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
546           TeamsRegionLoc;
547     }
548   }
549   /// Returns true, if current region has closely nested teams region.
550   bool hasInnerTeamsRegion() const {
551     return getInnerTeamsRegionLoc().isValid();
552   }
553   /// Returns location of the nested teams region (if any).
554   SourceLocation getInnerTeamsRegionLoc() const {
555     return isStackEmpty() ? SourceLocation()
556                           : Stack.back().first.back().InnerTeamsRegionLoc;
557   }
558 
559   Scope *getCurScope() const {
560     return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
561   }
562   SourceLocation getConstructLoc() const {
563     return isStackEmpty() ? SourceLocation()
564                           : Stack.back().first.back().ConstructLoc;
565   }
566 
567   /// Do the check specified in \a Check to all component lists and return true
568   /// if any issue is found.
569   bool checkMappableExprComponentListsForDecl(
570       const ValueDecl *VD, bool CurrentRegionOnly,
571       const llvm::function_ref<
572           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
573                OpenMPClauseKind)>
574           Check) const {
575     if (isStackEmpty())
576       return false;
577     auto SI = Stack.back().first.rbegin();
578     auto SE = Stack.back().first.rend();
579 
580     if (SI == SE)
581       return false;
582 
583     if (CurrentRegionOnly)
584       SE = std::next(SI);
585     else
586       std::advance(SI, 1);
587 
588     for (; SI != SE; ++SI) {
589       auto MI = SI->MappedExprComponents.find(VD);
590       if (MI != SI->MappedExprComponents.end())
591         for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
592              MI->second.Components)
593           if (Check(L, MI->second.Kind))
594             return true;
595     }
596     return false;
597   }
598 
599   /// Do the check specified in \a Check to all component lists at a given level
600   /// and return true if any issue is found.
601   bool checkMappableExprComponentListsForDeclAtLevel(
602       const ValueDecl *VD, unsigned Level,
603       const llvm::function_ref<
604           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
605                OpenMPClauseKind)>
606           Check) const {
607     if (isStackEmpty())
608       return false;
609 
610     auto StartI = Stack.back().first.begin();
611     auto EndI = Stack.back().first.end();
612     if (std::distance(StartI, EndI) <= (int)Level)
613       return false;
614     std::advance(StartI, Level);
615 
616     auto MI = StartI->MappedExprComponents.find(VD);
617     if (MI != StartI->MappedExprComponents.end())
618       for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
619            MI->second.Components)
620         if (Check(L, MI->second.Kind))
621           return true;
622     return false;
623   }
624 
625   /// Create a new mappable expression component list associated with a given
626   /// declaration and initialize it with the provided list of components.
627   void addMappableExpressionComponents(
628       const ValueDecl *VD,
629       OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
630       OpenMPClauseKind WhereFoundClauseKind) {
631     assert(!isStackEmpty() &&
632            "Not expecting to retrieve components from a empty stack!");
633     MappedExprComponentTy &MEC =
634         Stack.back().first.back().MappedExprComponents[VD];
635     // Create new entry and append the new components there.
636     MEC.Components.resize(MEC.Components.size() + 1);
637     MEC.Components.back().append(Components.begin(), Components.end());
638     MEC.Kind = WhereFoundClauseKind;
639   }
640 
641   unsigned getNestingLevel() const {
642     assert(!isStackEmpty());
643     return Stack.back().first.size() - 1;
644   }
645   void addDoacrossDependClause(OMPDependClause *C,
646                                const OperatorOffsetTy &OpsOffs) {
647     assert(!isStackEmpty() && Stack.back().first.size() > 1);
648     SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
649     assert(isOpenMPWorksharingDirective(StackElem.Directive));
650     StackElem.DoacrossDepends.try_emplace(C, OpsOffs);
651   }
652   llvm::iterator_range<DoacrossDependMapTy::const_iterator>
653   getDoacrossDependClauses() const {
654     assert(!isStackEmpty());
655     const SharingMapTy &StackElem = Stack.back().first.back();
656     if (isOpenMPWorksharingDirective(StackElem.Directive)) {
657       const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
658       return llvm::make_range(Ref.begin(), Ref.end());
659     }
660     return llvm::make_range(StackElem.DoacrossDepends.end(),
661                             StackElem.DoacrossDepends.end());
662   }
663 };
664 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
665   return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
666          isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
667 }
668 
669 } // namespace
670 
671 static const Expr *getExprAsWritten(const Expr *E) {
672   if (const auto *FE = dyn_cast<FullExpr>(E))
673     E = FE->getSubExpr();
674 
675   if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
676     E = MTE->GetTemporaryExpr();
677 
678   while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
679     E = Binder->getSubExpr();
680 
681   if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
682     E = ICE->getSubExprAsWritten();
683   return E->IgnoreParens();
684 }
685 
686 static Expr *getExprAsWritten(Expr *E) {
687   return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
688 }
689 
690 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
691   if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
692     if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
693       D = ME->getMemberDecl();
694   const auto *VD = dyn_cast<VarDecl>(D);
695   const auto *FD = dyn_cast<FieldDecl>(D);
696   if (VD != nullptr) {
697     VD = VD->getCanonicalDecl();
698     D = VD;
699   } else {
700     assert(FD);
701     FD = FD->getCanonicalDecl();
702     D = FD;
703   }
704   return D;
705 }
706 
707 static ValueDecl *getCanonicalDecl(ValueDecl *D) {
708   return const_cast<ValueDecl *>(
709       getCanonicalDecl(const_cast<const ValueDecl *>(D)));
710 }
711 
712 DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
713                                           ValueDecl *D) const {
714   D = getCanonicalDecl(D);
715   auto *VD = dyn_cast<VarDecl>(D);
716   const auto *FD = dyn_cast<FieldDecl>(D);
717   DSAVarData DVar;
718   if (isStackEmpty() || Iter == Stack.back().first.rend()) {
719     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
720     // in a region but not in construct]
721     //  File-scope or namespace-scope variables referenced in called routines
722     //  in the region are shared unless they appear in a threadprivate
723     //  directive.
724     if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
725       DVar.CKind = OMPC_shared;
726 
727     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
728     // in a region but not in construct]
729     //  Variables with static storage duration that are declared in called
730     //  routines in the region are shared.
731     if (VD && VD->hasGlobalStorage())
732       DVar.CKind = OMPC_shared;
733 
734     // Non-static data members are shared by default.
735     if (FD)
736       DVar.CKind = OMPC_shared;
737 
738     return DVar;
739   }
740 
741   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
742   // in a Construct, C/C++, predetermined, p.1]
743   // Variables with automatic storage duration that are declared in a scope
744   // inside the construct are private.
745   if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
746       (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
747     DVar.CKind = OMPC_private;
748     return DVar;
749   }
750 
751   DVar.DKind = Iter->Directive;
752   // Explicitly specified attributes and local variables with predetermined
753   // attributes.
754   if (Iter->SharingMap.count(D)) {
755     const DSAInfo &Data = Iter->SharingMap.lookup(D);
756     DVar.RefExpr = Data.RefExpr.getPointer();
757     DVar.PrivateCopy = Data.PrivateCopy;
758     DVar.CKind = Data.Attributes;
759     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
760     return DVar;
761   }
762 
763   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
764   // in a Construct, C/C++, implicitly determined, p.1]
765   //  In a parallel or task construct, the data-sharing attributes of these
766   //  variables are determined by the default clause, if present.
767   switch (Iter->DefaultAttr) {
768   case DSA_shared:
769     DVar.CKind = OMPC_shared;
770     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
771     return DVar;
772   case DSA_none:
773     return DVar;
774   case DSA_unspecified:
775     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
776     // in a Construct, implicitly determined, p.2]
777     //  In a parallel construct, if no default clause is present, these
778     //  variables are shared.
779     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
780     if (isOpenMPParallelDirective(DVar.DKind) ||
781         isOpenMPTeamsDirective(DVar.DKind)) {
782       DVar.CKind = OMPC_shared;
783       return DVar;
784     }
785 
786     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
787     // in a Construct, implicitly determined, p.4]
788     //  In a task construct, if no default clause is present, a variable that in
789     //  the enclosing context is determined to be shared by all implicit tasks
790     //  bound to the current team is shared.
791     if (isOpenMPTaskingDirective(DVar.DKind)) {
792       DSAVarData DVarTemp;
793       iterator I = Iter, E = Stack.back().first.rend();
794       do {
795         ++I;
796         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
797         // Referenced in a Construct, implicitly determined, p.6]
798         //  In a task construct, if no default clause is present, a variable
799         //  whose data-sharing attribute is not determined by the rules above is
800         //  firstprivate.
801         DVarTemp = getDSA(I, D);
802         if (DVarTemp.CKind != OMPC_shared) {
803           DVar.RefExpr = nullptr;
804           DVar.CKind = OMPC_firstprivate;
805           return DVar;
806         }
807       } while (I != E && !isParallelOrTaskRegion(I->Directive));
808       DVar.CKind =
809           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
810       return DVar;
811     }
812   }
813   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
814   // in a Construct, implicitly determined, p.3]
815   //  For constructs other than task, if no default clause is present, these
816   //  variables inherit their data-sharing attributes from the enclosing
817   //  context.
818   return getDSA(++Iter, D);
819 }
820 
821 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
822                                          const Expr *NewDE) {
823   assert(!isStackEmpty() && "Data sharing attributes stack is empty");
824   D = getCanonicalDecl(D);
825   SharingMapTy &StackElem = Stack.back().first.back();
826   auto It = StackElem.AlignedMap.find(D);
827   if (It == StackElem.AlignedMap.end()) {
828     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
829     StackElem.AlignedMap[D] = NewDE;
830     return nullptr;
831   }
832   assert(It->second && "Unexpected nullptr expr in the aligned map");
833   return It->second;
834 }
835 
836 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
837   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
838   D = getCanonicalDecl(D);
839   SharingMapTy &StackElem = Stack.back().first.back();
840   StackElem.LCVMap.try_emplace(
841       D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
842 }
843 
844 const DSAStackTy::LCDeclInfo
845 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
846   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
847   D = getCanonicalDecl(D);
848   const SharingMapTy &StackElem = Stack.back().first.back();
849   auto It = StackElem.LCVMap.find(D);
850   if (It != StackElem.LCVMap.end())
851     return It->second;
852   return {0, nullptr};
853 }
854 
855 const DSAStackTy::LCDeclInfo
856 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
857   assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
858          "Data-sharing attributes stack is empty");
859   D = getCanonicalDecl(D);
860   const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
861   auto It = StackElem.LCVMap.find(D);
862   if (It != StackElem.LCVMap.end())
863     return It->second;
864   return {0, nullptr};
865 }
866 
867 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
868   assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
869          "Data-sharing attributes stack is empty");
870   const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
871   if (StackElem.LCVMap.size() < I)
872     return nullptr;
873   for (const auto &Pair : StackElem.LCVMap)
874     if (Pair.second.first == I)
875       return Pair.first;
876   return nullptr;
877 }
878 
879 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
880                         DeclRefExpr *PrivateCopy) {
881   D = getCanonicalDecl(D);
882   if (A == OMPC_threadprivate) {
883     DSAInfo &Data = Threadprivates[D];
884     Data.Attributes = A;
885     Data.RefExpr.setPointer(E);
886     Data.PrivateCopy = nullptr;
887   } else {
888     assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
889     DSAInfo &Data = Stack.back().first.back().SharingMap[D];
890     assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
891            (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
892            (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
893            (isLoopControlVariable(D).first && A == OMPC_private));
894     if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
895       Data.RefExpr.setInt(/*IntVal=*/true);
896       return;
897     }
898     const bool IsLastprivate =
899         A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
900     Data.Attributes = A;
901     Data.RefExpr.setPointerAndInt(E, IsLastprivate);
902     Data.PrivateCopy = PrivateCopy;
903     if (PrivateCopy) {
904       DSAInfo &Data =
905           Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
906       Data.Attributes = A;
907       Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
908       Data.PrivateCopy = nullptr;
909     }
910   }
911 }
912 
913 /// Build a variable declaration for OpenMP loop iteration variable.
914 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
915                              StringRef Name, const AttrVec *Attrs = nullptr,
916                              DeclRefExpr *OrigRef = nullptr) {
917   DeclContext *DC = SemaRef.CurContext;
918   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
919   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
920   auto *Decl =
921       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
922   if (Attrs) {
923     for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
924          I != E; ++I)
925       Decl->addAttr(*I);
926   }
927   Decl->setImplicit();
928   if (OrigRef) {
929     Decl->addAttr(
930         OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
931   }
932   return Decl;
933 }
934 
935 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
936                                      SourceLocation Loc,
937                                      bool RefersToCapture = false) {
938   D->setReferenced();
939   D->markUsed(S.Context);
940   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
941                              SourceLocation(), D, RefersToCapture, Loc, Ty,
942                              VK_LValue);
943 }
944 
945 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
946                                            BinaryOperatorKind BOK) {
947   D = getCanonicalDecl(D);
948   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
949   assert(
950       Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
951       "Additional reduction info may be specified only for reduction items.");
952   ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
953   assert(ReductionData.ReductionRange.isInvalid() &&
954          Stack.back().first.back().Directive == OMPD_taskgroup &&
955          "Additional reduction info may be specified only once for reduction "
956          "items.");
957   ReductionData.set(BOK, SR);
958   Expr *&TaskgroupReductionRef =
959       Stack.back().first.back().TaskgroupReductionRef;
960   if (!TaskgroupReductionRef) {
961     VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
962                                SemaRef.Context.VoidPtrTy, ".task_red.");
963     TaskgroupReductionRef =
964         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
965   }
966 }
967 
968 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
969                                            const Expr *ReductionRef) {
970   D = getCanonicalDecl(D);
971   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
972   assert(
973       Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
974       "Additional reduction info may be specified only for reduction items.");
975   ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
976   assert(ReductionData.ReductionRange.isInvalid() &&
977          Stack.back().first.back().Directive == OMPD_taskgroup &&
978          "Additional reduction info may be specified only once for reduction "
979          "items.");
980   ReductionData.set(ReductionRef, SR);
981   Expr *&TaskgroupReductionRef =
982       Stack.back().first.back().TaskgroupReductionRef;
983   if (!TaskgroupReductionRef) {
984     VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
985                                SemaRef.Context.VoidPtrTy, ".task_red.");
986     TaskgroupReductionRef =
987         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
988   }
989 }
990 
991 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
992     const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
993     Expr *&TaskgroupDescriptor) const {
994   D = getCanonicalDecl(D);
995   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
996   if (Stack.back().first.empty())
997       return DSAVarData();
998   for (iterator I = std::next(Stack.back().first.rbegin(), 1),
999                 E = Stack.back().first.rend();
1000        I != E; std::advance(I, 1)) {
1001     const DSAInfo &Data = I->SharingMap.lookup(D);
1002     if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
1003       continue;
1004     const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1005     if (!ReductionData.ReductionOp ||
1006         ReductionData.ReductionOp.is<const Expr *>())
1007       return DSAVarData();
1008     SR = ReductionData.ReductionRange;
1009     BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
1010     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1011                                        "expression for the descriptor is not "
1012                                        "set.");
1013     TaskgroupDescriptor = I->TaskgroupReductionRef;
1014     return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1015                       Data.PrivateCopy, I->DefaultAttrLoc);
1016   }
1017   return DSAVarData();
1018 }
1019 
1020 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1021     const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1022     Expr *&TaskgroupDescriptor) const {
1023   D = getCanonicalDecl(D);
1024   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1025   if (Stack.back().first.empty())
1026       return DSAVarData();
1027   for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1028                 E = Stack.back().first.rend();
1029        I != E; std::advance(I, 1)) {
1030     const DSAInfo &Data = I->SharingMap.lookup(D);
1031     if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
1032       continue;
1033     const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1034     if (!ReductionData.ReductionOp ||
1035         !ReductionData.ReductionOp.is<const Expr *>())
1036       return DSAVarData();
1037     SR = ReductionData.ReductionRange;
1038     ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
1039     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1040                                        "expression for the descriptor is not "
1041                                        "set.");
1042     TaskgroupDescriptor = I->TaskgroupReductionRef;
1043     return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1044                       Data.PrivateCopy, I->DefaultAttrLoc);
1045   }
1046   return DSAVarData();
1047 }
1048 
1049 bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
1050   D = D->getCanonicalDecl();
1051   if (!isStackEmpty()) {
1052     iterator I = Iter, E = Stack.back().first.rend();
1053     Scope *TopScope = nullptr;
1054     while (I != E && !isParallelOrTaskRegion(I->Directive) &&
1055            !isOpenMPTargetExecutionDirective(I->Directive))
1056       ++I;
1057     if (I == E)
1058       return false;
1059     TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
1060     Scope *CurScope = getCurScope();
1061     while (CurScope != TopScope && !CurScope->isDeclScope(D))
1062       CurScope = CurScope->getParent();
1063     return CurScope != TopScope;
1064   }
1065   return false;
1066 }
1067 
1068 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1069                                                    bool FromParent) {
1070   D = getCanonicalDecl(D);
1071   DSAVarData DVar;
1072 
1073   auto *VD = dyn_cast<VarDecl>(D);
1074   auto TI = Threadprivates.find(D);
1075   if (TI != Threadprivates.end()) {
1076     DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1077     DVar.CKind = OMPC_threadprivate;
1078     return DVar;
1079   }
1080   if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
1081     DVar.RefExpr = buildDeclRefExpr(
1082         SemaRef, VD, D->getType().getNonReferenceType(),
1083         VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1084     DVar.CKind = OMPC_threadprivate;
1085     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1086     return DVar;
1087   }
1088   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1089   // in a Construct, C/C++, predetermined, p.1]
1090   //  Variables appearing in threadprivate directives are threadprivate.
1091   if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1092        !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1093          SemaRef.getLangOpts().OpenMPUseTLS &&
1094          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1095       (VD && VD->getStorageClass() == SC_Register &&
1096        VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1097     DVar.RefExpr = buildDeclRefExpr(
1098         SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1099     DVar.CKind = OMPC_threadprivate;
1100     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1101     return DVar;
1102   }
1103   if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1104       VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1105       !isLoopControlVariable(D).first) {
1106     iterator IterTarget =
1107         std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1108                      [](const SharingMapTy &Data) {
1109                        return isOpenMPTargetExecutionDirective(Data.Directive);
1110                      });
1111     if (IterTarget != Stack.back().first.rend()) {
1112       iterator ParentIterTarget = std::next(IterTarget, 1);
1113       for (iterator Iter = Stack.back().first.rbegin();
1114            Iter != ParentIterTarget; std::advance(Iter, 1)) {
1115         if (isOpenMPLocal(VD, Iter)) {
1116           DVar.RefExpr =
1117               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1118                                D->getLocation());
1119           DVar.CKind = OMPC_threadprivate;
1120           return DVar;
1121         }
1122       }
1123       if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1124         auto DSAIter = IterTarget->SharingMap.find(D);
1125         if (DSAIter != IterTarget->SharingMap.end() &&
1126             isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1127           DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1128           DVar.CKind = OMPC_threadprivate;
1129           return DVar;
1130         }
1131         iterator End = Stack.back().first.rend();
1132         if (!SemaRef.isOpenMPCapturedByRef(
1133                 D, std::distance(ParentIterTarget, End))) {
1134           DVar.RefExpr =
1135               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1136                                IterTarget->ConstructLoc);
1137           DVar.CKind = OMPC_threadprivate;
1138           return DVar;
1139         }
1140       }
1141     }
1142   }
1143 
1144   if (isStackEmpty())
1145     // Not in OpenMP execution region and top scope was already checked.
1146     return DVar;
1147 
1148   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1149   // in a Construct, C/C++, predetermined, p.4]
1150   //  Static data members are shared.
1151   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1152   // in a Construct, C/C++, predetermined, p.7]
1153   //  Variables with static storage duration that are declared in a scope
1154   //  inside the construct are shared.
1155   auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
1156   if (VD && VD->isStaticDataMember()) {
1157     DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
1158     if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1159       return DVar;
1160 
1161     DVar.CKind = OMPC_shared;
1162     return DVar;
1163   }
1164 
1165   QualType Type = D->getType().getNonReferenceType().getCanonicalType();
1166   bool IsConstant = Type.isConstant(SemaRef.getASTContext());
1167   Type = SemaRef.getASTContext().getBaseElementType(Type);
1168   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1169   // in a Construct, C/C++, predetermined, p.6]
1170   //  Variables with const qualified type having no mutable member are
1171   //  shared.
1172   const CXXRecordDecl *RD =
1173       SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
1174   if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1175     if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1176       RD = CTD->getTemplatedDecl();
1177   if (IsConstant &&
1178       !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
1179         RD->hasMutableFields())) {
1180     // Variables with const-qualified type having no mutable member may be
1181     // listed in a firstprivate clause, even if they are static data members.
1182     DSAVarData DVarTemp =
1183         hasDSA(D, [](OpenMPClauseKind C) { return C == OMPC_firstprivate; },
1184                MatchesAlways, FromParent);
1185     if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
1186       return DVarTemp;
1187 
1188     DVar.CKind = OMPC_shared;
1189     return DVar;
1190   }
1191 
1192   // Explicitly specified attributes and local variables with predetermined
1193   // attributes.
1194   iterator I = Stack.back().first.rbegin();
1195   iterator EndI = Stack.back().first.rend();
1196   if (FromParent && I != EndI)
1197     std::advance(I, 1);
1198   auto It = I->SharingMap.find(D);
1199   if (It != I->SharingMap.end()) {
1200     const DSAInfo &Data = It->getSecond();
1201     DVar.RefExpr = Data.RefExpr.getPointer();
1202     DVar.PrivateCopy = Data.PrivateCopy;
1203     DVar.CKind = Data.Attributes;
1204     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1205     DVar.DKind = I->Directive;
1206   }
1207 
1208   return DVar;
1209 }
1210 
1211 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1212                                                         bool FromParent) const {
1213   if (isStackEmpty()) {
1214     iterator I;
1215     return getDSA(I, D);
1216   }
1217   D = getCanonicalDecl(D);
1218   iterator StartI = Stack.back().first.rbegin();
1219   iterator EndI = Stack.back().first.rend();
1220   if (FromParent && StartI != EndI)
1221     std::advance(StartI, 1);
1222   return getDSA(StartI, D);
1223 }
1224 
1225 const DSAStackTy::DSAVarData
1226 DSAStackTy::hasDSA(ValueDecl *D,
1227                    const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1228                    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1229                    bool FromParent) const {
1230   if (isStackEmpty())
1231     return {};
1232   D = getCanonicalDecl(D);
1233   iterator I = Stack.back().first.rbegin();
1234   iterator EndI = Stack.back().first.rend();
1235   if (FromParent && I != EndI)
1236     std::advance(I, 1);
1237   for (; I != EndI; std::advance(I, 1)) {
1238     if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
1239       continue;
1240     iterator NewI = I;
1241     DSAVarData DVar = getDSA(NewI, D);
1242     if (I == NewI && CPred(DVar.CKind))
1243       return DVar;
1244   }
1245   return {};
1246 }
1247 
1248 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1249     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1250     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1251     bool FromParent) const {
1252   if (isStackEmpty())
1253     return {};
1254   D = getCanonicalDecl(D);
1255   iterator StartI = Stack.back().first.rbegin();
1256   iterator EndI = Stack.back().first.rend();
1257   if (FromParent && StartI != EndI)
1258     std::advance(StartI, 1);
1259   if (StartI == EndI || !DPred(StartI->Directive))
1260     return {};
1261   iterator NewI = StartI;
1262   DSAVarData DVar = getDSA(NewI, D);
1263   return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
1264 }
1265 
1266 bool DSAStackTy::hasExplicitDSA(
1267     const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1268     unsigned Level, bool NotLastprivate) const {
1269   if (isStackEmpty())
1270     return false;
1271   D = getCanonicalDecl(D);
1272   auto StartI = Stack.back().first.begin();
1273   auto EndI = Stack.back().first.end();
1274   if (std::distance(StartI, EndI) <= (int)Level)
1275     return false;
1276   std::advance(StartI, Level);
1277   auto I = StartI->SharingMap.find(D);
1278   if ((I != StartI->SharingMap.end()) &&
1279          I->getSecond().RefExpr.getPointer() &&
1280          CPred(I->getSecond().Attributes) &&
1281          (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1282     return true;
1283   // Check predetermined rules for the loop control variables.
1284   auto LI = StartI->LCVMap.find(D);
1285   if (LI != StartI->LCVMap.end())
1286     return CPred(OMPC_private);
1287   return false;
1288 }
1289 
1290 bool DSAStackTy::hasExplicitDirective(
1291     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1292     unsigned Level) const {
1293   if (isStackEmpty())
1294     return false;
1295   auto StartI = Stack.back().first.begin();
1296   auto EndI = Stack.back().first.end();
1297   if (std::distance(StartI, EndI) <= (int)Level)
1298     return false;
1299   std::advance(StartI, Level);
1300   return DPred(StartI->Directive);
1301 }
1302 
1303 bool DSAStackTy::hasDirective(
1304     const llvm::function_ref<bool(OpenMPDirectiveKind,
1305                                   const DeclarationNameInfo &, SourceLocation)>
1306         DPred,
1307     bool FromParent) const {
1308   // We look only in the enclosing region.
1309   if (isStackEmpty())
1310     return false;
1311   auto StartI = std::next(Stack.back().first.rbegin());
1312   auto EndI = Stack.back().first.rend();
1313   if (FromParent && StartI != EndI)
1314     StartI = std::next(StartI);
1315   for (auto I = StartI, EE = EndI; I != EE; ++I) {
1316     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1317       return true;
1318   }
1319   return false;
1320 }
1321 
1322 void Sema::InitDataSharingAttributesStack() {
1323   VarDataSharingAttributesStack = new DSAStackTy(*this);
1324 }
1325 
1326 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1327 
1328 void Sema::pushOpenMPFunctionRegion() {
1329   DSAStack->pushFunction();
1330 }
1331 
1332 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1333   DSAStack->popFunction(OldFSI);
1334 }
1335 
1336 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
1337   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1338 
1339   ASTContext &Ctx = getASTContext();
1340   bool IsByRef = true;
1341 
1342   // Find the directive that is associated with the provided scope.
1343   D = cast<ValueDecl>(D->getCanonicalDecl());
1344   QualType Ty = D->getType();
1345 
1346   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1347     // This table summarizes how a given variable should be passed to the device
1348     // given its type and the clauses where it appears. This table is based on
1349     // the description in OpenMP 4.5 [2.10.4, target Construct] and
1350     // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1351     //
1352     // =========================================================================
1353     // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
1354     // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
1355     // =========================================================================
1356     // | scl  |               |     |       |       -       |          | bycopy|
1357     // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
1358     // | scl  |               |  x  |   -   |       -       |     -    | null  |
1359     // | scl  |       x       |     |       |       -       |          | byref |
1360     // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
1361     // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
1362     // | scl  |               |  -  |   -   |       -       |     x    | byref |
1363     // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
1364     //
1365     // | agg  |      n.a.     |     |       |       -       |          | byref |
1366     // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
1367     // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1368     // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1369     // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
1370     //
1371     // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
1372     // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
1373     // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1374     // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1375     // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
1376     // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
1377     // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
1378     // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
1379     // =========================================================================
1380     // Legend:
1381     //  scl - scalar
1382     //  ptr - pointer
1383     //  agg - aggregate
1384     //  x - applies
1385     //  - - invalid in this combination
1386     //  [] - mapped with an array section
1387     //  byref - should be mapped by reference
1388     //  byval - should be mapped by value
1389     //  null - initialize a local variable to null on the device
1390     //
1391     // Observations:
1392     //  - All scalar declarations that show up in a map clause have to be passed
1393     //    by reference, because they may have been mapped in the enclosing data
1394     //    environment.
1395     //  - If the scalar value does not fit the size of uintptr, it has to be
1396     //    passed by reference, regardless the result in the table above.
1397     //  - For pointers mapped by value that have either an implicit map or an
1398     //    array section, the runtime library may pass the NULL value to the
1399     //    device instead of the value passed to it by the compiler.
1400 
1401     if (Ty->isReferenceType())
1402       Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1403 
1404     // Locate map clauses and see if the variable being captured is referred to
1405     // in any of those clauses. Here we only care about variables, not fields,
1406     // because fields are part of aggregates.
1407     bool IsVariableUsedInMapClause = false;
1408     bool IsVariableAssociatedWithSection = false;
1409 
1410     DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1411         D, Level,
1412         [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1413             OMPClauseMappableExprCommon::MappableExprComponentListRef
1414                 MapExprComponents,
1415             OpenMPClauseKind WhereFoundClauseKind) {
1416           // Only the map clause information influences how a variable is
1417           // captured. E.g. is_device_ptr does not require changing the default
1418           // behavior.
1419           if (WhereFoundClauseKind != OMPC_map)
1420             return false;
1421 
1422           auto EI = MapExprComponents.rbegin();
1423           auto EE = MapExprComponents.rend();
1424 
1425           assert(EI != EE && "Invalid map expression!");
1426 
1427           if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1428             IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1429 
1430           ++EI;
1431           if (EI == EE)
1432             return false;
1433 
1434           if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1435               isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1436               isa<MemberExpr>(EI->getAssociatedExpression())) {
1437             IsVariableAssociatedWithSection = true;
1438             // There is nothing more we need to know about this variable.
1439             return true;
1440           }
1441 
1442           // Keep looking for more map info.
1443           return false;
1444         });
1445 
1446     if (IsVariableUsedInMapClause) {
1447       // If variable is identified in a map clause it is always captured by
1448       // reference except if it is a pointer that is dereferenced somehow.
1449       IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1450     } else {
1451       // By default, all the data that has a scalar type is mapped by copy
1452       // (except for reduction variables).
1453       IsByRef =
1454           (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1455            !Ty->isAnyPointerType()) ||
1456           !Ty->isScalarType() ||
1457           DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1458           DSAStack->hasExplicitDSA(
1459               D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
1460     }
1461   }
1462 
1463   if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
1464     IsByRef =
1465         ((DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1466           !Ty->isAnyPointerType()) ||
1467          !DSAStack->hasExplicitDSA(
1468              D,
1469              [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1470              Level, /*NotLastprivate=*/true)) &&
1471         // If the variable is artificial and must be captured by value - try to
1472         // capture by value.
1473         !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1474           !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
1475   }
1476 
1477   // When passing data by copy, we need to make sure it fits the uintptr size
1478   // and alignment, because the runtime library only deals with uintptr types.
1479   // If it does not fit the uintptr size, we need to pass the data by reference
1480   // instead.
1481   if (!IsByRef &&
1482       (Ctx.getTypeSizeInChars(Ty) >
1483            Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
1484        Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
1485     IsByRef = true;
1486   }
1487 
1488   return IsByRef;
1489 }
1490 
1491 unsigned Sema::getOpenMPNestingLevel() const {
1492   assert(getLangOpts().OpenMP);
1493   return DSAStack->getNestingLevel();
1494 }
1495 
1496 bool Sema::isInOpenMPTargetExecutionDirective() const {
1497   return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1498           !DSAStack->isClauseParsingMode()) ||
1499          DSAStack->hasDirective(
1500              [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1501                 SourceLocation) -> bool {
1502                return isOpenMPTargetExecutionDirective(K);
1503              },
1504              false);
1505 }
1506 
1507 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
1508   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1509   D = getCanonicalDecl(D);
1510 
1511   // If we are attempting to capture a global variable in a directive with
1512   // 'target' we return true so that this global is also mapped to the device.
1513   //
1514   auto *VD = dyn_cast<VarDecl>(D);
1515   if (VD && !VD->hasLocalStorage()) {
1516     if (isInOpenMPDeclareTargetContext() &&
1517         (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1518       // Try to mark variable as declare target if it is used in capturing
1519       // regions.
1520       if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
1521         checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
1522       return nullptr;
1523     } else if (isInOpenMPTargetExecutionDirective()) {
1524       // If the declaration is enclosed in a 'declare target' directive,
1525       // then it should not be captured.
1526       //
1527       if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
1528         return nullptr;
1529       return VD;
1530     }
1531   }
1532   // Capture variables captured by reference in lambdas for target-based
1533   // directives.
1534   if (VD && !DSAStack->isClauseParsingMode()) {
1535     if (const auto *RD = VD->getType()
1536                              .getCanonicalType()
1537                              .getNonReferenceType()
1538                              ->getAsCXXRecordDecl()) {
1539       bool SavedForceCaptureByReferenceInTargetExecutable =
1540           DSAStack->isForceCaptureByReferenceInTargetExecutable();
1541       DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true);
1542       if (RD->isLambda()) {
1543         llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
1544         FieldDecl *ThisCapture;
1545         RD->getCaptureFields(Captures, ThisCapture);
1546         for (const LambdaCapture &LC : RD->captures()) {
1547           if (LC.getCaptureKind() == LCK_ByRef) {
1548             VarDecl *VD = LC.getCapturedVar();
1549             DeclContext *VDC = VD->getDeclContext();
1550             if (!VDC->Encloses(CurContext))
1551               continue;
1552             DSAStackTy::DSAVarData DVarPrivate =
1553                 DSAStack->getTopDSA(VD, /*FromParent=*/false);
1554             // Do not capture already captured variables.
1555             if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) &&
1556                 DVarPrivate.CKind == OMPC_unknown &&
1557                 !DSAStack->checkMappableExprComponentListsForDecl(
1558                     D, /*CurrentRegionOnly=*/true,
1559                     [](OMPClauseMappableExprCommon::
1560                            MappableExprComponentListRef,
1561                        OpenMPClauseKind) { return true; }))
1562               MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar());
1563           } else if (LC.getCaptureKind() == LCK_This) {
1564             QualType ThisTy = getCurrentThisType();
1565             if (!ThisTy.isNull() &&
1566                 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
1567               CheckCXXThisCapture(LC.getLocation());
1568           }
1569         }
1570       }
1571       DSAStack->setForceCaptureByReferenceInTargetExecutable(
1572           SavedForceCaptureByReferenceInTargetExecutable);
1573     }
1574   }
1575 
1576   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1577       (!DSAStack->isClauseParsingMode() ||
1578        DSAStack->getParentDirective() != OMPD_unknown)) {
1579     auto &&Info = DSAStack->isLoopControlVariable(D);
1580     if (Info.first ||
1581         (VD && VD->hasLocalStorage() &&
1582          isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
1583         (VD && DSAStack->isForceVarCapturing()))
1584       return VD ? VD : Info.second;
1585     DSAStackTy::DSAVarData DVarPrivate =
1586         DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
1587     if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
1588       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1589     DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1590                                    [](OpenMPDirectiveKind) { return true; },
1591                                    DSAStack->isClauseParsingMode());
1592     if (DVarPrivate.CKind != OMPC_unknown)
1593       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1594   }
1595   return nullptr;
1596 }
1597 
1598 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1599                                         unsigned Level) const {
1600   SmallVector<OpenMPDirectiveKind, 4> Regions;
1601   getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1602   FunctionScopesIndex -= Regions.size();
1603 }
1604 
1605 void Sema::startOpenMPLoop() {
1606   assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1607   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1608     DSAStack->loopInit();
1609 }
1610 
1611 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
1612   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1613   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1614     if (DSAStack->getAssociatedLoops() > 0 &&
1615         !DSAStack->isLoopStarted()) {
1616       DSAStack->resetPossibleLoopCounter(D);
1617       DSAStack->loopStart();
1618       return true;
1619     }
1620     if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
1621          DSAStack->isLoopControlVariable(D).first) &&
1622         !DSAStack->hasExplicitDSA(
1623             D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
1624         !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
1625       return true;
1626   }
1627   return DSAStack->hasExplicitDSA(
1628              D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
1629          (DSAStack->isClauseParsingMode() &&
1630           DSAStack->getClauseParsingMode() == OMPC_private) ||
1631          // Consider taskgroup reduction descriptor variable a private to avoid
1632          // possible capture in the region.
1633          (DSAStack->hasExplicitDirective(
1634               [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1635               Level) &&
1636           DSAStack->isTaskgroupReductionRef(D, Level));
1637 }
1638 
1639 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1640                                 unsigned Level) {
1641   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1642   D = getCanonicalDecl(D);
1643   OpenMPClauseKind OMPC = OMPC_unknown;
1644   for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1645     const unsigned NewLevel = I - 1;
1646     if (DSAStack->hasExplicitDSA(D,
1647                                  [&OMPC](const OpenMPClauseKind K) {
1648                                    if (isOpenMPPrivate(K)) {
1649                                      OMPC = K;
1650                                      return true;
1651                                    }
1652                                    return false;
1653                                  },
1654                                  NewLevel))
1655       break;
1656     if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1657             D, NewLevel,
1658             [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1659                OpenMPClauseKind) { return true; })) {
1660       OMPC = OMPC_map;
1661       break;
1662     }
1663     if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1664                                        NewLevel)) {
1665       OMPC = OMPC_map;
1666       if (D->getType()->isScalarType() &&
1667           DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1668               DefaultMapAttributes::DMA_tofrom_scalar)
1669         OMPC = OMPC_firstprivate;
1670       break;
1671     }
1672   }
1673   if (OMPC != OMPC_unknown)
1674     FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1675 }
1676 
1677 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1678                                       unsigned Level) const {
1679   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1680   // Return true if the current level is no longer enclosed in a target region.
1681 
1682   const auto *VD = dyn_cast<VarDecl>(D);
1683   return VD && !VD->hasLocalStorage() &&
1684          DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1685                                         Level);
1686 }
1687 
1688 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
1689 
1690 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1691                                const DeclarationNameInfo &DirName,
1692                                Scope *CurScope, SourceLocation Loc) {
1693   DSAStack->push(DKind, DirName, CurScope, Loc);
1694   PushExpressionEvaluationContext(
1695       ExpressionEvaluationContext::PotentiallyEvaluated);
1696 }
1697 
1698 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1699   DSAStack->setClauseParsingMode(K);
1700 }
1701 
1702 void Sema::EndOpenMPClause() {
1703   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
1704 }
1705 
1706 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
1707   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1708   //  A variable of class type (or array thereof) that appears in a lastprivate
1709   //  clause requires an accessible, unambiguous default constructor for the
1710   //  class type, unless the list item is also specified in a firstprivate
1711   //  clause.
1712   if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1713     for (OMPClause *C : D->clauses()) {
1714       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1715         SmallVector<Expr *, 8> PrivateCopies;
1716         for (Expr *DE : Clause->varlists()) {
1717           if (DE->isValueDependent() || DE->isTypeDependent()) {
1718             PrivateCopies.push_back(nullptr);
1719             continue;
1720           }
1721           auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
1722           auto *VD = cast<VarDecl>(DRE->getDecl());
1723           QualType Type = VD->getType().getNonReferenceType();
1724           const DSAStackTy::DSAVarData DVar =
1725               DSAStack->getTopDSA(VD, /*FromParent=*/false);
1726           if (DVar.CKind == OMPC_lastprivate) {
1727             // Generate helper private variable and initialize it with the
1728             // default value. The address of the original variable is replaced
1729             // by the address of the new private variable in CodeGen. This new
1730             // variable is not added to IdResolver, so the code in the OpenMP
1731             // region uses original variable for proper diagnostics.
1732             VarDecl *VDPrivate = buildVarDecl(
1733                 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
1734                 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
1735             ActOnUninitializedDecl(VDPrivate);
1736             if (VDPrivate->isInvalidDecl())
1737               continue;
1738             PrivateCopies.push_back(buildDeclRefExpr(
1739                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
1740           } else {
1741             // The variable is also a firstprivate, so initialization sequence
1742             // for private copy is generated already.
1743             PrivateCopies.push_back(nullptr);
1744           }
1745         }
1746         // Set initializers to private copies if no errors were found.
1747         if (PrivateCopies.size() == Clause->varlist_size())
1748           Clause->setPrivateCopies(PrivateCopies);
1749       }
1750     }
1751   }
1752 
1753   DSAStack->pop();
1754   DiscardCleanupsInEvaluationContext();
1755   PopExpressionEvaluationContext();
1756 }
1757 
1758 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1759                                      Expr *NumIterations, Sema &SemaRef,
1760                                      Scope *S, DSAStackTy *Stack);
1761 
1762 namespace {
1763 
1764 class VarDeclFilterCCC final : public CorrectionCandidateCallback {
1765 private:
1766   Sema &SemaRef;
1767 
1768 public:
1769   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
1770   bool ValidateCandidate(const TypoCorrection &Candidate) override {
1771     NamedDecl *ND = Candidate.getCorrectionDecl();
1772     if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
1773       return VD->hasGlobalStorage() &&
1774              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1775                                    SemaRef.getCurScope());
1776     }
1777     return false;
1778   }
1779 };
1780 
1781 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
1782 private:
1783   Sema &SemaRef;
1784 
1785 public:
1786   explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1787   bool ValidateCandidate(const TypoCorrection &Candidate) override {
1788     NamedDecl *ND = Candidate.getCorrectionDecl();
1789     if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
1790       return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1791                                    SemaRef.getCurScope());
1792     }
1793     return false;
1794   }
1795 };
1796 
1797 } // namespace
1798 
1799 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1800                                          CXXScopeSpec &ScopeSpec,
1801                                          const DeclarationNameInfo &Id) {
1802   LookupResult Lookup(*this, Id, LookupOrdinaryName);
1803   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1804 
1805   if (Lookup.isAmbiguous())
1806     return ExprError();
1807 
1808   VarDecl *VD;
1809   if (!Lookup.isSingleResult()) {
1810     if (TypoCorrection Corrected = CorrectTypo(
1811             Id, LookupOrdinaryName, CurScope, nullptr,
1812             llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
1813       diagnoseTypo(Corrected,
1814                    PDiag(Lookup.empty()
1815                              ? diag::err_undeclared_var_use_suggest
1816                              : diag::err_omp_expected_var_arg_suggest)
1817                        << Id.getName());
1818       VD = Corrected.getCorrectionDeclAs<VarDecl>();
1819     } else {
1820       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1821                                        : diag::err_omp_expected_var_arg)
1822           << Id.getName();
1823       return ExprError();
1824     }
1825   } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1826     Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1827     Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1828     return ExprError();
1829   }
1830   Lookup.suppressDiagnostics();
1831 
1832   // OpenMP [2.9.2, Syntax, C/C++]
1833   //   Variables must be file-scope, namespace-scope, or static block-scope.
1834   if (!VD->hasGlobalStorage()) {
1835     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
1836         << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1837     bool IsDecl =
1838         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1839     Diag(VD->getLocation(),
1840          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1841         << VD;
1842     return ExprError();
1843   }
1844 
1845   VarDecl *CanonicalVD = VD->getCanonicalDecl();
1846   NamedDecl *ND = CanonicalVD;
1847   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1848   //   A threadprivate directive for file-scope variables must appear outside
1849   //   any definition or declaration.
1850   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1851       !getCurLexicalContext()->isTranslationUnit()) {
1852     Diag(Id.getLoc(), diag::err_omp_var_scope)
1853         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1854     bool IsDecl =
1855         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1856     Diag(VD->getLocation(),
1857          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1858         << VD;
1859     return ExprError();
1860   }
1861   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1862   //   A threadprivate directive for static class member variables must appear
1863   //   in the class definition, in the same scope in which the member
1864   //   variables are declared.
1865   if (CanonicalVD->isStaticDataMember() &&
1866       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1867     Diag(Id.getLoc(), diag::err_omp_var_scope)
1868         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1869     bool IsDecl =
1870         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1871     Diag(VD->getLocation(),
1872          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1873         << VD;
1874     return ExprError();
1875   }
1876   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1877   //   A threadprivate directive for namespace-scope variables must appear
1878   //   outside any definition or declaration other than the namespace
1879   //   definition itself.
1880   if (CanonicalVD->getDeclContext()->isNamespace() &&
1881       (!getCurLexicalContext()->isFileContext() ||
1882        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1883     Diag(Id.getLoc(), diag::err_omp_var_scope)
1884         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1885     bool IsDecl =
1886         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1887     Diag(VD->getLocation(),
1888          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1889         << VD;
1890     return ExprError();
1891   }
1892   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1893   //   A threadprivate directive for static block-scope variables must appear
1894   //   in the scope of the variable and not in a nested scope.
1895   if (CanonicalVD->isStaticLocal() && CurScope &&
1896       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
1897     Diag(Id.getLoc(), diag::err_omp_var_scope)
1898         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1899     bool IsDecl =
1900         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1901     Diag(VD->getLocation(),
1902          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1903         << VD;
1904     return ExprError();
1905   }
1906 
1907   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1908   //   A threadprivate directive must lexically precede all references to any
1909   //   of the variables in its list.
1910   if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
1911     Diag(Id.getLoc(), diag::err_omp_var_used)
1912         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1913     return ExprError();
1914   }
1915 
1916   QualType ExprType = VD->getType().getNonReferenceType();
1917   return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1918                              SourceLocation(), VD,
1919                              /*RefersToEnclosingVariableOrCapture=*/false,
1920                              Id.getLoc(), ExprType, VK_LValue);
1921 }
1922 
1923 Sema::DeclGroupPtrTy
1924 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1925                                         ArrayRef<Expr *> VarList) {
1926   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
1927     CurContext->addDecl(D);
1928     return DeclGroupPtrTy::make(DeclGroupRef(D));
1929   }
1930   return nullptr;
1931 }
1932 
1933 namespace {
1934 class LocalVarRefChecker final
1935     : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1936   Sema &SemaRef;
1937 
1938 public:
1939   bool VisitDeclRefExpr(const DeclRefExpr *E) {
1940     if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1941       if (VD->hasLocalStorage()) {
1942         SemaRef.Diag(E->getBeginLoc(),
1943                      diag::err_omp_local_var_in_threadprivate_init)
1944             << E->getSourceRange();
1945         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1946             << VD << VD->getSourceRange();
1947         return true;
1948       }
1949     }
1950     return false;
1951   }
1952   bool VisitStmt(const Stmt *S) {
1953     for (const Stmt *Child : S->children()) {
1954       if (Child && Visit(Child))
1955         return true;
1956     }
1957     return false;
1958   }
1959   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
1960 };
1961 } // namespace
1962 
1963 OMPThreadPrivateDecl *
1964 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
1965   SmallVector<Expr *, 8> Vars;
1966   for (Expr *RefExpr : VarList) {
1967     auto *DE = cast<DeclRefExpr>(RefExpr);
1968     auto *VD = cast<VarDecl>(DE->getDecl());
1969     SourceLocation ILoc = DE->getExprLoc();
1970 
1971     // Mark variable as used.
1972     VD->setReferenced();
1973     VD->markUsed(Context);
1974 
1975     QualType QType = VD->getType();
1976     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1977       // It will be analyzed later.
1978       Vars.push_back(DE);
1979       continue;
1980     }
1981 
1982     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1983     //   A threadprivate variable must not have an incomplete type.
1984     if (RequireCompleteType(ILoc, VD->getType(),
1985                             diag::err_omp_threadprivate_incomplete_type)) {
1986       continue;
1987     }
1988 
1989     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1990     //   A threadprivate variable must not have a reference type.
1991     if (VD->getType()->isReferenceType()) {
1992       Diag(ILoc, diag::err_omp_ref_type_arg)
1993           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1994       bool IsDecl =
1995           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1996       Diag(VD->getLocation(),
1997            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1998           << VD;
1999       continue;
2000     }
2001 
2002     // Check if this is a TLS variable. If TLS is not being supported, produce
2003     // the corresponding diagnostic.
2004     if ((VD->getTLSKind() != VarDecl::TLS_None &&
2005          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2006            getLangOpts().OpenMPUseTLS &&
2007            getASTContext().getTargetInfo().isTLSSupported())) ||
2008         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2009          !VD->isLocalVarDecl())) {
2010       Diag(ILoc, diag::err_omp_var_thread_local)
2011           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
2012       bool IsDecl =
2013           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2014       Diag(VD->getLocation(),
2015            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2016           << VD;
2017       continue;
2018     }
2019 
2020     // Check if initial value of threadprivate variable reference variable with
2021     // local storage (it is not supported by runtime).
2022     if (const Expr *Init = VD->getAnyInitializer()) {
2023       LocalVarRefChecker Checker(*this);
2024       if (Checker.Visit(Init))
2025         continue;
2026     }
2027 
2028     Vars.push_back(RefExpr);
2029     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
2030     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2031         Context, SourceRange(Loc, Loc)));
2032     if (ASTMutationListener *ML = Context.getASTMutationListener())
2033       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
2034   }
2035   OMPThreadPrivateDecl *D = nullptr;
2036   if (!Vars.empty()) {
2037     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2038                                      Vars);
2039     D->setAccess(AS_public);
2040   }
2041   return D;
2042 }
2043 
2044 Sema::DeclGroupPtrTy
2045 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2046                                    ArrayRef<OMPClause *> ClauseList) {
2047   OMPRequiresDecl *D = nullptr;
2048   if (!CurContext->isFileContext()) {
2049     Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2050   } else {
2051     D = CheckOMPRequiresDecl(Loc, ClauseList);
2052     if (D) {
2053       CurContext->addDecl(D);
2054       DSAStack->addRequiresDecl(D);
2055     }
2056   }
2057   return DeclGroupPtrTy::make(DeclGroupRef(D));
2058 }
2059 
2060 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2061                                             ArrayRef<OMPClause *> ClauseList) {
2062   if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2063     return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2064                                    ClauseList);
2065   return nullptr;
2066 }
2067 
2068 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2069                               const ValueDecl *D,
2070                               const DSAStackTy::DSAVarData &DVar,
2071                               bool IsLoopIterVar = false) {
2072   if (DVar.RefExpr) {
2073     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2074         << getOpenMPClauseName(DVar.CKind);
2075     return;
2076   }
2077   enum {
2078     PDSA_StaticMemberShared,
2079     PDSA_StaticLocalVarShared,
2080     PDSA_LoopIterVarPrivate,
2081     PDSA_LoopIterVarLinear,
2082     PDSA_LoopIterVarLastprivate,
2083     PDSA_ConstVarShared,
2084     PDSA_GlobalVarShared,
2085     PDSA_TaskVarFirstprivate,
2086     PDSA_LocalVarPrivate,
2087     PDSA_Implicit
2088   } Reason = PDSA_Implicit;
2089   bool ReportHint = false;
2090   auto ReportLoc = D->getLocation();
2091   auto *VD = dyn_cast<VarDecl>(D);
2092   if (IsLoopIterVar) {
2093     if (DVar.CKind == OMPC_private)
2094       Reason = PDSA_LoopIterVarPrivate;
2095     else if (DVar.CKind == OMPC_lastprivate)
2096       Reason = PDSA_LoopIterVarLastprivate;
2097     else
2098       Reason = PDSA_LoopIterVarLinear;
2099   } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2100              DVar.CKind == OMPC_firstprivate) {
2101     Reason = PDSA_TaskVarFirstprivate;
2102     ReportLoc = DVar.ImplicitDSALoc;
2103   } else if (VD && VD->isStaticLocal())
2104     Reason = PDSA_StaticLocalVarShared;
2105   else if (VD && VD->isStaticDataMember())
2106     Reason = PDSA_StaticMemberShared;
2107   else if (VD && VD->isFileVarDecl())
2108     Reason = PDSA_GlobalVarShared;
2109   else if (D->getType().isConstant(SemaRef.getASTContext()))
2110     Reason = PDSA_ConstVarShared;
2111   else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
2112     ReportHint = true;
2113     Reason = PDSA_LocalVarPrivate;
2114   }
2115   if (Reason != PDSA_Implicit) {
2116     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
2117         << Reason << ReportHint
2118         << getOpenMPDirectiveName(Stack->getCurrentDirective());
2119   } else if (DVar.ImplicitDSALoc.isValid()) {
2120     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2121         << getOpenMPClauseName(DVar.CKind);
2122   }
2123 }
2124 
2125 namespace {
2126 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
2127   DSAStackTy *Stack;
2128   Sema &SemaRef;
2129   bool ErrorFound = false;
2130   CapturedStmt *CS = nullptr;
2131   llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2132   llvm::SmallVector<Expr *, 4> ImplicitMap;
2133   Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2134   llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
2135 
2136   void VisitSubCaptures(OMPExecutableDirective *S) {
2137     // Check implicitly captured variables.
2138     if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2139       return;
2140     for (const CapturedStmt::Capture &Cap :
2141          S->getInnermostCapturedStmt()->captures()) {
2142       if (!Cap.capturesVariable())
2143         continue;
2144       VarDecl *VD = Cap.getCapturedVar();
2145       // Do not try to map the variable if it or its sub-component was mapped
2146       // already.
2147       if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2148           Stack->checkMappableExprComponentListsForDecl(
2149               VD, /*CurrentRegionOnly=*/true,
2150               [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2151                  OpenMPClauseKind) { return true; }))
2152         continue;
2153       DeclRefExpr *DRE = buildDeclRefExpr(
2154           SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
2155           Cap.getLocation(), /*RefersToCapture=*/true);
2156       Visit(DRE);
2157     }
2158   }
2159 
2160 public:
2161   void VisitDeclRefExpr(DeclRefExpr *E) {
2162     if (E->isTypeDependent() || E->isValueDependent() ||
2163         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2164       return;
2165     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2166       VD = VD->getCanonicalDecl();
2167       // Skip internally declared variables.
2168       if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
2169         return;
2170 
2171       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
2172       // Check if the variable has explicit DSA set and stop analysis if it so.
2173       if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
2174         return;
2175 
2176       // Skip internally declared static variables.
2177       llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
2178           OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
2179       if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
2180           (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
2181         return;
2182 
2183       SourceLocation ELoc = E->getExprLoc();
2184       OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2185       // The default(none) clause requires that each variable that is referenced
2186       // in the construct, and does not have a predetermined data-sharing
2187       // attribute, must have its data-sharing attribute explicitly determined
2188       // by being listed in a data-sharing attribute clause.
2189       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
2190           isParallelOrTaskRegion(DKind) &&
2191           VarsWithInheritedDSA.count(VD) == 0) {
2192         VarsWithInheritedDSA[VD] = E;
2193         return;
2194       }
2195 
2196       if (isOpenMPTargetExecutionDirective(DKind) &&
2197           !Stack->isLoopControlVariable(VD).first) {
2198         if (!Stack->checkMappableExprComponentListsForDecl(
2199                 VD, /*CurrentRegionOnly=*/true,
2200                 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2201                        StackComponents,
2202                    OpenMPClauseKind) {
2203                   // Variable is used if it has been marked as an array, array
2204                   // section or the variable iself.
2205                   return StackComponents.size() == 1 ||
2206                          std::all_of(
2207                              std::next(StackComponents.rbegin()),
2208                              StackComponents.rend(),
2209                              [](const OMPClauseMappableExprCommon::
2210                                     MappableComponent &MC) {
2211                                return MC.getAssociatedDeclaration() ==
2212                                           nullptr &&
2213                                       (isa<OMPArraySectionExpr>(
2214                                            MC.getAssociatedExpression()) ||
2215                                        isa<ArraySubscriptExpr>(
2216                                            MC.getAssociatedExpression()));
2217                              });
2218                 })) {
2219           bool IsFirstprivate = false;
2220           // By default lambdas are captured as firstprivates.
2221           if (const auto *RD =
2222                   VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
2223             IsFirstprivate = RD->isLambda();
2224           IsFirstprivate =
2225               IsFirstprivate ||
2226               (VD->getType().getNonReferenceType()->isScalarType() &&
2227                Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
2228           if (IsFirstprivate)
2229             ImplicitFirstprivate.emplace_back(E);
2230           else
2231             ImplicitMap.emplace_back(E);
2232           return;
2233         }
2234       }
2235 
2236       // OpenMP [2.9.3.6, Restrictions, p.2]
2237       //  A list item that appears in a reduction clause of the innermost
2238       //  enclosing worksharing or parallel construct may not be accessed in an
2239       //  explicit task.
2240       DVar = Stack->hasInnermostDSA(
2241           VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2242           [](OpenMPDirectiveKind K) {
2243             return isOpenMPParallelDirective(K) ||
2244                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2245           },
2246           /*FromParent=*/true);
2247       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2248         ErrorFound = true;
2249         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2250         reportOriginalDsa(SemaRef, Stack, VD, DVar);
2251         return;
2252       }
2253 
2254       // Define implicit data-sharing attributes for task.
2255       DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
2256       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2257           !Stack->isLoopControlVariable(VD).first)
2258         ImplicitFirstprivate.push_back(E);
2259     }
2260   }
2261   void VisitMemberExpr(MemberExpr *E) {
2262     if (E->isTypeDependent() || E->isValueDependent() ||
2263         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2264       return;
2265     auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
2266     OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2267     if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
2268       if (!FD)
2269         return;
2270       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
2271       // Check if the variable has explicit DSA set and stop analysis if it
2272       // so.
2273       if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2274         return;
2275 
2276       if (isOpenMPTargetExecutionDirective(DKind) &&
2277           !Stack->isLoopControlVariable(FD).first &&
2278           !Stack->checkMappableExprComponentListsForDecl(
2279               FD, /*CurrentRegionOnly=*/true,
2280               [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2281                      StackComponents,
2282                  OpenMPClauseKind) {
2283                 return isa<CXXThisExpr>(
2284                     cast<MemberExpr>(
2285                         StackComponents.back().getAssociatedExpression())
2286                         ->getBase()
2287                         ->IgnoreParens());
2288               })) {
2289         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2290         //  A bit-field cannot appear in a map clause.
2291         //
2292         if (FD->isBitField())
2293           return;
2294         ImplicitMap.emplace_back(E);
2295         return;
2296       }
2297 
2298       SourceLocation ELoc = E->getExprLoc();
2299       // OpenMP [2.9.3.6, Restrictions, p.2]
2300       //  A list item that appears in a reduction clause of the innermost
2301       //  enclosing worksharing or parallel construct may not be accessed in
2302       //  an  explicit task.
2303       DVar = Stack->hasInnermostDSA(
2304           FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2305           [](OpenMPDirectiveKind K) {
2306             return isOpenMPParallelDirective(K) ||
2307                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2308           },
2309           /*FromParent=*/true);
2310       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2311         ErrorFound = true;
2312         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2313         reportOriginalDsa(SemaRef, Stack, FD, DVar);
2314         return;
2315       }
2316 
2317       // Define implicit data-sharing attributes for task.
2318       DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
2319       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2320           !Stack->isLoopControlVariable(FD).first) {
2321         // Check if there is a captured expression for the current field in the
2322         // region. Do not mark it as firstprivate unless there is no captured
2323         // expression.
2324         // TODO: try to make it firstprivate.
2325         if (DVar.CKind != OMPC_unknown)
2326           ImplicitFirstprivate.push_back(E);
2327       }
2328       return;
2329     }
2330     if (isOpenMPTargetExecutionDirective(DKind)) {
2331       OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
2332       if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
2333                                         /*NoDiagnose=*/true))
2334         return;
2335       const auto *VD = cast<ValueDecl>(
2336           CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2337       if (!Stack->checkMappableExprComponentListsForDecl(
2338               VD, /*CurrentRegionOnly=*/true,
2339               [&CurComponents](
2340                   OMPClauseMappableExprCommon::MappableExprComponentListRef
2341                       StackComponents,
2342                   OpenMPClauseKind) {
2343                 auto CCI = CurComponents.rbegin();
2344                 auto CCE = CurComponents.rend();
2345                 for (const auto &SC : llvm::reverse(StackComponents)) {
2346                   // Do both expressions have the same kind?
2347                   if (CCI->getAssociatedExpression()->getStmtClass() !=
2348                       SC.getAssociatedExpression()->getStmtClass())
2349                     if (!(isa<OMPArraySectionExpr>(
2350                               SC.getAssociatedExpression()) &&
2351                           isa<ArraySubscriptExpr>(
2352                               CCI->getAssociatedExpression())))
2353                       return false;
2354 
2355                   const Decl *CCD = CCI->getAssociatedDeclaration();
2356                   const Decl *SCD = SC.getAssociatedDeclaration();
2357                   CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2358                   SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2359                   if (SCD != CCD)
2360                     return false;
2361                   std::advance(CCI, 1);
2362                   if (CCI == CCE)
2363                     break;
2364                 }
2365                 return true;
2366               })) {
2367         Visit(E->getBase());
2368       }
2369     } else {
2370       Visit(E->getBase());
2371     }
2372   }
2373   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
2374     for (OMPClause *C : S->clauses()) {
2375       // Skip analysis of arguments of implicitly defined firstprivate clause
2376       // for task|target directives.
2377       // Skip analysis of arguments of implicitly defined map clause for target
2378       // directives.
2379       if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2380                  C->isImplicit())) {
2381         for (Stmt *CC : C->children()) {
2382           if (CC)
2383             Visit(CC);
2384         }
2385       }
2386     }
2387     // Check implicitly captured variables.
2388     VisitSubCaptures(S);
2389   }
2390   void VisitStmt(Stmt *S) {
2391     for (Stmt *C : S->children()) {
2392       if (C) {
2393         if (auto *OED = dyn_cast<OMPExecutableDirective>(C)) {
2394           // Check implicitly captured variables in the task-based directives to
2395           // check if they must be firstprivatized.
2396           VisitSubCaptures(OED);
2397         } else {
2398           Visit(C);
2399         }
2400       }
2401     }
2402   }
2403 
2404   bool isErrorFound() const { return ErrorFound; }
2405   ArrayRef<Expr *> getImplicitFirstprivate() const {
2406     return ImplicitFirstprivate;
2407   }
2408   ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
2409   const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
2410     return VarsWithInheritedDSA;
2411   }
2412 
2413   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2414       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
2415 };
2416 } // namespace
2417 
2418 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
2419   switch (DKind) {
2420   case OMPD_parallel:
2421   case OMPD_parallel_for:
2422   case OMPD_parallel_for_simd:
2423   case OMPD_parallel_sections:
2424   case OMPD_teams:
2425   case OMPD_teams_distribute:
2426   case OMPD_teams_distribute_simd: {
2427     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2428     QualType KmpInt32PtrTy =
2429         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2430     Sema::CapturedParamNameType Params[] = {
2431         std::make_pair(".global_tid.", KmpInt32PtrTy),
2432         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2433         std::make_pair(StringRef(), QualType()) // __context with shared vars
2434     };
2435     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2436                              Params);
2437     break;
2438   }
2439   case OMPD_target_teams:
2440   case OMPD_target_parallel:
2441   case OMPD_target_parallel_for:
2442   case OMPD_target_parallel_for_simd:
2443   case OMPD_target_teams_distribute:
2444   case OMPD_target_teams_distribute_simd: {
2445     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2446     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2447     QualType KmpInt32PtrTy =
2448         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2449     QualType Args[] = {VoidPtrTy};
2450     FunctionProtoType::ExtProtoInfo EPI;
2451     EPI.Variadic = true;
2452     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2453     Sema::CapturedParamNameType Params[] = {
2454         std::make_pair(".global_tid.", KmpInt32Ty),
2455         std::make_pair(".part_id.", KmpInt32PtrTy),
2456         std::make_pair(".privates.", VoidPtrTy),
2457         std::make_pair(
2458             ".copy_fn.",
2459             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2460         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2461         std::make_pair(StringRef(), QualType()) // __context with shared vars
2462     };
2463     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2464                              Params);
2465     // Mark this captured region as inlined, because we don't use outlined
2466     // function directly.
2467     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2468         AlwaysInlineAttr::CreateImplicit(
2469             Context, AlwaysInlineAttr::Keyword_forceinline));
2470     Sema::CapturedParamNameType ParamsTarget[] = {
2471         std::make_pair(StringRef(), QualType()) // __context with shared vars
2472     };
2473     // Start a captured region for 'target' with no implicit parameters.
2474     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2475                              ParamsTarget);
2476     Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
2477         std::make_pair(".global_tid.", KmpInt32PtrTy),
2478         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2479         std::make_pair(StringRef(), QualType()) // __context with shared vars
2480     };
2481     // Start a captured region for 'teams' or 'parallel'.  Both regions have
2482     // the same implicit parameters.
2483     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2484                              ParamsTeamsOrParallel);
2485     break;
2486   }
2487   case OMPD_target:
2488   case OMPD_target_simd: {
2489     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2490     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2491     QualType KmpInt32PtrTy =
2492         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2493     QualType Args[] = {VoidPtrTy};
2494     FunctionProtoType::ExtProtoInfo EPI;
2495     EPI.Variadic = true;
2496     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2497     Sema::CapturedParamNameType Params[] = {
2498         std::make_pair(".global_tid.", KmpInt32Ty),
2499         std::make_pair(".part_id.", KmpInt32PtrTy),
2500         std::make_pair(".privates.", VoidPtrTy),
2501         std::make_pair(
2502             ".copy_fn.",
2503             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2504         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2505         std::make_pair(StringRef(), QualType()) // __context with shared vars
2506     };
2507     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2508                              Params);
2509     // Mark this captured region as inlined, because we don't use outlined
2510     // function directly.
2511     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2512         AlwaysInlineAttr::CreateImplicit(
2513             Context, AlwaysInlineAttr::Keyword_forceinline));
2514     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2515                              std::make_pair(StringRef(), QualType()));
2516     break;
2517   }
2518   case OMPD_simd:
2519   case OMPD_for:
2520   case OMPD_for_simd:
2521   case OMPD_sections:
2522   case OMPD_section:
2523   case OMPD_single:
2524   case OMPD_master:
2525   case OMPD_critical:
2526   case OMPD_taskgroup:
2527   case OMPD_distribute:
2528   case OMPD_distribute_simd:
2529   case OMPD_ordered:
2530   case OMPD_atomic:
2531   case OMPD_target_data: {
2532     Sema::CapturedParamNameType Params[] = {
2533         std::make_pair(StringRef(), QualType()) // __context with shared vars
2534     };
2535     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2536                              Params);
2537     break;
2538   }
2539   case OMPD_task: {
2540     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2541     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2542     QualType KmpInt32PtrTy =
2543         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2544     QualType Args[] = {VoidPtrTy};
2545     FunctionProtoType::ExtProtoInfo EPI;
2546     EPI.Variadic = true;
2547     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2548     Sema::CapturedParamNameType Params[] = {
2549         std::make_pair(".global_tid.", KmpInt32Ty),
2550         std::make_pair(".part_id.", KmpInt32PtrTy),
2551         std::make_pair(".privates.", VoidPtrTy),
2552         std::make_pair(
2553             ".copy_fn.",
2554             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2555         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2556         std::make_pair(StringRef(), QualType()) // __context with shared vars
2557     };
2558     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2559                              Params);
2560     // Mark this captured region as inlined, because we don't use outlined
2561     // function directly.
2562     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2563         AlwaysInlineAttr::CreateImplicit(
2564             Context, AlwaysInlineAttr::Keyword_forceinline));
2565     break;
2566   }
2567   case OMPD_taskloop:
2568   case OMPD_taskloop_simd: {
2569     QualType KmpInt32Ty =
2570         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2571             .withConst();
2572     QualType KmpUInt64Ty =
2573         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2574             .withConst();
2575     QualType KmpInt64Ty =
2576         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2577             .withConst();
2578     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2579     QualType KmpInt32PtrTy =
2580         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2581     QualType Args[] = {VoidPtrTy};
2582     FunctionProtoType::ExtProtoInfo EPI;
2583     EPI.Variadic = true;
2584     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2585     Sema::CapturedParamNameType Params[] = {
2586         std::make_pair(".global_tid.", KmpInt32Ty),
2587         std::make_pair(".part_id.", KmpInt32PtrTy),
2588         std::make_pair(".privates.", VoidPtrTy),
2589         std::make_pair(
2590             ".copy_fn.",
2591             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2592         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2593         std::make_pair(".lb.", KmpUInt64Ty),
2594         std::make_pair(".ub.", KmpUInt64Ty),
2595         std::make_pair(".st.", KmpInt64Ty),
2596         std::make_pair(".liter.", KmpInt32Ty),
2597         std::make_pair(".reductions.", VoidPtrTy),
2598         std::make_pair(StringRef(), QualType()) // __context with shared vars
2599     };
2600     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2601                              Params);
2602     // Mark this captured region as inlined, because we don't use outlined
2603     // function directly.
2604     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2605         AlwaysInlineAttr::CreateImplicit(
2606             Context, AlwaysInlineAttr::Keyword_forceinline));
2607     break;
2608   }
2609   case OMPD_distribute_parallel_for_simd:
2610   case OMPD_distribute_parallel_for: {
2611     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2612     QualType KmpInt32PtrTy =
2613         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2614     Sema::CapturedParamNameType Params[] = {
2615         std::make_pair(".global_tid.", KmpInt32PtrTy),
2616         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2617         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2618         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
2619         std::make_pair(StringRef(), QualType()) // __context with shared vars
2620     };
2621     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2622                              Params);
2623     break;
2624   }
2625   case OMPD_target_teams_distribute_parallel_for:
2626   case OMPD_target_teams_distribute_parallel_for_simd: {
2627     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2628     QualType KmpInt32PtrTy =
2629         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2630     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2631 
2632     QualType Args[] = {VoidPtrTy};
2633     FunctionProtoType::ExtProtoInfo EPI;
2634     EPI.Variadic = true;
2635     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2636     Sema::CapturedParamNameType Params[] = {
2637         std::make_pair(".global_tid.", KmpInt32Ty),
2638         std::make_pair(".part_id.", KmpInt32PtrTy),
2639         std::make_pair(".privates.", VoidPtrTy),
2640         std::make_pair(
2641             ".copy_fn.",
2642             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2643         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2644         std::make_pair(StringRef(), QualType()) // __context with shared vars
2645     };
2646     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2647                              Params);
2648     // Mark this captured region as inlined, because we don't use outlined
2649     // function directly.
2650     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2651         AlwaysInlineAttr::CreateImplicit(
2652             Context, AlwaysInlineAttr::Keyword_forceinline));
2653     Sema::CapturedParamNameType ParamsTarget[] = {
2654         std::make_pair(StringRef(), QualType()) // __context with shared vars
2655     };
2656     // Start a captured region for 'target' with no implicit parameters.
2657     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2658                              ParamsTarget);
2659 
2660     Sema::CapturedParamNameType ParamsTeams[] = {
2661         std::make_pair(".global_tid.", KmpInt32PtrTy),
2662         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2663         std::make_pair(StringRef(), QualType()) // __context with shared vars
2664     };
2665     // Start a captured region for 'target' with no implicit parameters.
2666     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2667                              ParamsTeams);
2668 
2669     Sema::CapturedParamNameType ParamsParallel[] = {
2670         std::make_pair(".global_tid.", KmpInt32PtrTy),
2671         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2672         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2673         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
2674         std::make_pair(StringRef(), QualType()) // __context with shared vars
2675     };
2676     // Start a captured region for 'teams' or 'parallel'.  Both regions have
2677     // the same implicit parameters.
2678     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2679                              ParamsParallel);
2680     break;
2681   }
2682 
2683   case OMPD_teams_distribute_parallel_for:
2684   case OMPD_teams_distribute_parallel_for_simd: {
2685     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2686     QualType KmpInt32PtrTy =
2687         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2688 
2689     Sema::CapturedParamNameType ParamsTeams[] = {
2690         std::make_pair(".global_tid.", KmpInt32PtrTy),
2691         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2692         std::make_pair(StringRef(), QualType()) // __context with shared vars
2693     };
2694     // Start a captured region for 'target' with no implicit parameters.
2695     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2696                              ParamsTeams);
2697 
2698     Sema::CapturedParamNameType ParamsParallel[] = {
2699         std::make_pair(".global_tid.", KmpInt32PtrTy),
2700         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2701         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2702         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
2703         std::make_pair(StringRef(), QualType()) // __context with shared vars
2704     };
2705     // Start a captured region for 'teams' or 'parallel'.  Both regions have
2706     // the same implicit parameters.
2707     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2708                              ParamsParallel);
2709     break;
2710   }
2711   case OMPD_target_update:
2712   case OMPD_target_enter_data:
2713   case OMPD_target_exit_data: {
2714     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2715     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2716     QualType KmpInt32PtrTy =
2717         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2718     QualType Args[] = {VoidPtrTy};
2719     FunctionProtoType::ExtProtoInfo EPI;
2720     EPI.Variadic = true;
2721     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2722     Sema::CapturedParamNameType Params[] = {
2723         std::make_pair(".global_tid.", KmpInt32Ty),
2724         std::make_pair(".part_id.", KmpInt32PtrTy),
2725         std::make_pair(".privates.", VoidPtrTy),
2726         std::make_pair(
2727             ".copy_fn.",
2728             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2729         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2730         std::make_pair(StringRef(), QualType()) // __context with shared vars
2731     };
2732     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2733                              Params);
2734     // Mark this captured region as inlined, because we don't use outlined
2735     // function directly.
2736     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2737         AlwaysInlineAttr::CreateImplicit(
2738             Context, AlwaysInlineAttr::Keyword_forceinline));
2739     break;
2740   }
2741   case OMPD_threadprivate:
2742   case OMPD_taskyield:
2743   case OMPD_barrier:
2744   case OMPD_taskwait:
2745   case OMPD_cancellation_point:
2746   case OMPD_cancel:
2747   case OMPD_flush:
2748   case OMPD_declare_reduction:
2749   case OMPD_declare_simd:
2750   case OMPD_declare_target:
2751   case OMPD_end_declare_target:
2752   case OMPD_requires:
2753     llvm_unreachable("OpenMP Directive is not allowed");
2754   case OMPD_unknown:
2755     llvm_unreachable("Unknown OpenMP directive");
2756   }
2757 }
2758 
2759 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2760   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2761   getOpenMPCaptureRegions(CaptureRegions, DKind);
2762   return CaptureRegions.size();
2763 }
2764 
2765 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
2766                                              Expr *CaptureExpr, bool WithInit,
2767                                              bool AsExpression) {
2768   assert(CaptureExpr);
2769   ASTContext &C = S.getASTContext();
2770   Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
2771   QualType Ty = Init->getType();
2772   if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
2773     if (S.getLangOpts().CPlusPlus) {
2774       Ty = C.getLValueReferenceType(Ty);
2775     } else {
2776       Ty = C.getPointerType(Ty);
2777       ExprResult Res =
2778           S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2779       if (!Res.isUsable())
2780         return nullptr;
2781       Init = Res.get();
2782     }
2783     WithInit = true;
2784   }
2785   auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
2786                                           CaptureExpr->getBeginLoc());
2787   if (!WithInit)
2788     CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
2789   S.CurContext->addHiddenDecl(CED);
2790   S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
2791   return CED;
2792 }
2793 
2794 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2795                                  bool WithInit) {
2796   OMPCapturedExprDecl *CD;
2797   if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
2798     CD = cast<OMPCapturedExprDecl>(VD);
2799   else
2800     CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2801                           /*AsExpression=*/false);
2802   return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2803                           CaptureExpr->getExprLoc());
2804 }
2805 
2806 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
2807   CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
2808   if (!Ref) {
2809     OMPCapturedExprDecl *CD = buildCaptureDecl(
2810         S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2811         /*WithInit=*/true, /*AsExpression=*/true);
2812     Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2813                            CaptureExpr->getExprLoc());
2814   }
2815   ExprResult Res = Ref;
2816   if (!S.getLangOpts().CPlusPlus &&
2817       CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
2818       Ref->getType()->isPointerType()) {
2819     Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
2820     if (!Res.isUsable())
2821       return ExprError();
2822   }
2823   return S.DefaultLvalueConversion(Res.get());
2824 }
2825 
2826 namespace {
2827 // OpenMP directives parsed in this section are represented as a
2828 // CapturedStatement with an associated statement.  If a syntax error
2829 // is detected during the parsing of the associated statement, the
2830 // compiler must abort processing and close the CapturedStatement.
2831 //
2832 // Combined directives such as 'target parallel' have more than one
2833 // nested CapturedStatements.  This RAII ensures that we unwind out
2834 // of all the nested CapturedStatements when an error is found.
2835 class CaptureRegionUnwinderRAII {
2836 private:
2837   Sema &S;
2838   bool &ErrorFound;
2839   OpenMPDirectiveKind DKind = OMPD_unknown;
2840 
2841 public:
2842   CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2843                             OpenMPDirectiveKind DKind)
2844       : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2845   ~CaptureRegionUnwinderRAII() {
2846     if (ErrorFound) {
2847       int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2848       while (--ThisCaptureLevel >= 0)
2849         S.ActOnCapturedRegionError();
2850     }
2851   }
2852 };
2853 } // namespace
2854 
2855 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2856                                       ArrayRef<OMPClause *> Clauses) {
2857   bool ErrorFound = false;
2858   CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2859       *this, ErrorFound, DSAStack->getCurrentDirective());
2860   if (!S.isUsable()) {
2861     ErrorFound = true;
2862     return StmtError();
2863   }
2864 
2865   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2866   getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
2867   OMPOrderedClause *OC = nullptr;
2868   OMPScheduleClause *SC = nullptr;
2869   SmallVector<const OMPLinearClause *, 4> LCs;
2870   SmallVector<const OMPClauseWithPreInit *, 4> PICs;
2871   // This is required for proper codegen.
2872   for (OMPClause *Clause : Clauses) {
2873     if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2874         Clause->getClauseKind() == OMPC_in_reduction) {
2875       // Capture taskgroup task_reduction descriptors inside the tasking regions
2876       // with the corresponding in_reduction items.
2877       auto *IRC = cast<OMPInReductionClause>(Clause);
2878       for (Expr *E : IRC->taskgroup_descriptors())
2879         if (E)
2880           MarkDeclarationsReferencedInExpr(E);
2881     }
2882     if (isOpenMPPrivate(Clause->getClauseKind()) ||
2883         Clause->getClauseKind() == OMPC_copyprivate ||
2884         (getLangOpts().OpenMPUseTLS &&
2885          getASTContext().getTargetInfo().isTLSSupported() &&
2886          Clause->getClauseKind() == OMPC_copyin)) {
2887       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
2888       // Mark all variables in private list clauses as used in inner region.
2889       for (Stmt *VarRef : Clause->children()) {
2890         if (auto *E = cast_or_null<Expr>(VarRef)) {
2891           MarkDeclarationsReferencedInExpr(E);
2892         }
2893       }
2894       DSAStack->setForceVarCapturing(/*V=*/false);
2895     } else if (CaptureRegions.size() > 1 ||
2896                CaptureRegions.back() != OMPD_unknown) {
2897       if (auto *C = OMPClauseWithPreInit::get(Clause))
2898         PICs.push_back(C);
2899       if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
2900         if (Expr *E = C->getPostUpdateExpr())
2901           MarkDeclarationsReferencedInExpr(E);
2902       }
2903     }
2904     if (Clause->getClauseKind() == OMPC_schedule)
2905       SC = cast<OMPScheduleClause>(Clause);
2906     else if (Clause->getClauseKind() == OMPC_ordered)
2907       OC = cast<OMPOrderedClause>(Clause);
2908     else if (Clause->getClauseKind() == OMPC_linear)
2909       LCs.push_back(cast<OMPLinearClause>(Clause));
2910   }
2911   // OpenMP, 2.7.1 Loop Construct, Restrictions
2912   // The nonmonotonic modifier cannot be specified if an ordered clause is
2913   // specified.
2914   if (SC &&
2915       (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2916        SC->getSecondScheduleModifier() ==
2917            OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2918       OC) {
2919     Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2920              ? SC->getFirstScheduleModifierLoc()
2921              : SC->getSecondScheduleModifierLoc(),
2922          diag::err_omp_schedule_nonmonotonic_ordered)
2923         << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
2924     ErrorFound = true;
2925   }
2926   if (!LCs.empty() && OC && OC->getNumForLoops()) {
2927     for (const OMPLinearClause *C : LCs) {
2928       Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
2929           << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
2930     }
2931     ErrorFound = true;
2932   }
2933   if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2934       isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2935       OC->getNumForLoops()) {
2936     Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
2937         << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2938     ErrorFound = true;
2939   }
2940   if (ErrorFound) {
2941     return StmtError();
2942   }
2943   StmtResult SR = S;
2944   for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
2945     // Mark all variables in private list clauses as used in inner region.
2946     // Required for proper codegen of combined directives.
2947     // TODO: add processing for other clauses.
2948     if (ThisCaptureRegion != OMPD_unknown) {
2949       for (const clang::OMPClauseWithPreInit *C : PICs) {
2950         OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2951         // Find the particular capture region for the clause if the
2952         // directive is a combined one with multiple capture regions.
2953         // If the directive is not a combined one, the capture region
2954         // associated with the clause is OMPD_unknown and is generated
2955         // only once.
2956         if (CaptureRegion == ThisCaptureRegion ||
2957             CaptureRegion == OMPD_unknown) {
2958           if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
2959             for (Decl *D : DS->decls())
2960               MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2961           }
2962         }
2963       }
2964     }
2965     SR = ActOnCapturedRegionEnd(SR.get());
2966   }
2967   return SR;
2968 }
2969 
2970 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2971                               OpenMPDirectiveKind CancelRegion,
2972                               SourceLocation StartLoc) {
2973   // CancelRegion is only needed for cancel and cancellation_point.
2974   if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2975     return false;
2976 
2977   if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2978       CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2979     return false;
2980 
2981   SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2982       << getOpenMPDirectiveName(CancelRegion);
2983   return true;
2984 }
2985 
2986 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
2987                                   OpenMPDirectiveKind CurrentRegion,
2988                                   const DeclarationNameInfo &CurrentName,
2989                                   OpenMPDirectiveKind CancelRegion,
2990                                   SourceLocation StartLoc) {
2991   if (Stack->getCurScope()) {
2992     OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
2993     OpenMPDirectiveKind OffendingRegion = ParentRegion;
2994     bool NestingProhibited = false;
2995     bool CloseNesting = true;
2996     bool OrphanSeen = false;
2997     enum {
2998       NoRecommend,
2999       ShouldBeInParallelRegion,
3000       ShouldBeInOrderedRegion,
3001       ShouldBeInTargetRegion,
3002       ShouldBeInTeamsRegion
3003     } Recommend = NoRecommend;
3004     if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
3005       // OpenMP [2.16, Nesting of Regions]
3006       // OpenMP constructs may not be nested inside a simd region.
3007       // OpenMP [2.8.1,simd Construct, Restrictions]
3008       // An ordered construct with the simd clause is the only OpenMP
3009       // construct that can appear in the simd region.
3010       // Allowing a SIMD construct nested in another SIMD construct is an
3011       // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3012       // message.
3013       SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3014                                  ? diag::err_omp_prohibited_region_simd
3015                                  : diag::warn_omp_nesting_simd);
3016       return CurrentRegion != OMPD_simd;
3017     }
3018     if (ParentRegion == OMPD_atomic) {
3019       // OpenMP [2.16, Nesting of Regions]
3020       // OpenMP constructs may not be nested inside an atomic region.
3021       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3022       return true;
3023     }
3024     if (CurrentRegion == OMPD_section) {
3025       // OpenMP [2.7.2, sections Construct, Restrictions]
3026       // Orphaned section directives are prohibited. That is, the section
3027       // directives must appear within the sections construct and must not be
3028       // encountered elsewhere in the sections region.
3029       if (ParentRegion != OMPD_sections &&
3030           ParentRegion != OMPD_parallel_sections) {
3031         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3032             << (ParentRegion != OMPD_unknown)
3033             << getOpenMPDirectiveName(ParentRegion);
3034         return true;
3035       }
3036       return false;
3037     }
3038     // Allow some constructs (except teams) to be orphaned (they could be
3039     // used in functions, called from OpenMP regions with the required
3040     // preconditions).
3041     if (ParentRegion == OMPD_unknown &&
3042         !isOpenMPNestingTeamsDirective(CurrentRegion))
3043       return false;
3044     if (CurrentRegion == OMPD_cancellation_point ||
3045         CurrentRegion == OMPD_cancel) {
3046       // OpenMP [2.16, Nesting of Regions]
3047       // A cancellation point construct for which construct-type-clause is
3048       // taskgroup must be nested inside a task construct. A cancellation
3049       // point construct for which construct-type-clause is not taskgroup must
3050       // be closely nested inside an OpenMP construct that matches the type
3051       // specified in construct-type-clause.
3052       // A cancel construct for which construct-type-clause is taskgroup must be
3053       // nested inside a task construct. A cancel construct for which
3054       // construct-type-clause is not taskgroup must be closely nested inside an
3055       // OpenMP construct that matches the type specified in
3056       // construct-type-clause.
3057       NestingProhibited =
3058           !((CancelRegion == OMPD_parallel &&
3059              (ParentRegion == OMPD_parallel ||
3060               ParentRegion == OMPD_target_parallel)) ||
3061             (CancelRegion == OMPD_for &&
3062              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3063               ParentRegion == OMPD_target_parallel_for ||
3064               ParentRegion == OMPD_distribute_parallel_for ||
3065               ParentRegion == OMPD_teams_distribute_parallel_for ||
3066               ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
3067             (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3068             (CancelRegion == OMPD_sections &&
3069              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3070               ParentRegion == OMPD_parallel_sections)));
3071     } else if (CurrentRegion == OMPD_master) {
3072       // OpenMP [2.16, Nesting of Regions]
3073       // A master region may not be closely nested inside a worksharing,
3074       // atomic, or explicit task region.
3075       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3076                           isOpenMPTaskingDirective(ParentRegion);
3077     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3078       // OpenMP [2.16, Nesting of Regions]
3079       // A critical region may not be nested (closely or otherwise) inside a
3080       // critical region with the same name. Note that this restriction is not
3081       // sufficient to prevent deadlock.
3082       SourceLocation PreviousCriticalLoc;
3083       bool DeadLock = Stack->hasDirective(
3084           [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3085                                               const DeclarationNameInfo &DNI,
3086                                               SourceLocation Loc) {
3087             if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3088               PreviousCriticalLoc = Loc;
3089               return true;
3090             }
3091             return false;
3092           },
3093           false /* skip top directive */);
3094       if (DeadLock) {
3095         SemaRef.Diag(StartLoc,
3096                      diag::err_omp_prohibited_region_critical_same_name)
3097             << CurrentName.getName();
3098         if (PreviousCriticalLoc.isValid())
3099           SemaRef.Diag(PreviousCriticalLoc,
3100                        diag::note_omp_previous_critical_region);
3101         return true;
3102       }
3103     } else if (CurrentRegion == OMPD_barrier) {
3104       // OpenMP [2.16, Nesting of Regions]
3105       // A barrier region may not be closely nested inside a worksharing,
3106       // explicit task, critical, ordered, atomic, or master region.
3107       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3108                           isOpenMPTaskingDirective(ParentRegion) ||
3109                           ParentRegion == OMPD_master ||
3110                           ParentRegion == OMPD_critical ||
3111                           ParentRegion == OMPD_ordered;
3112     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
3113                !isOpenMPParallelDirective(CurrentRegion) &&
3114                !isOpenMPTeamsDirective(CurrentRegion)) {
3115       // OpenMP [2.16, Nesting of Regions]
3116       // A worksharing region may not be closely nested inside a worksharing,
3117       // explicit task, critical, ordered, atomic, or master region.
3118       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3119                           isOpenMPTaskingDirective(ParentRegion) ||
3120                           ParentRegion == OMPD_master ||
3121                           ParentRegion == OMPD_critical ||
3122                           ParentRegion == OMPD_ordered;
3123       Recommend = ShouldBeInParallelRegion;
3124     } else if (CurrentRegion == OMPD_ordered) {
3125       // OpenMP [2.16, Nesting of Regions]
3126       // An ordered region may not be closely nested inside a critical,
3127       // atomic, or explicit task region.
3128       // An ordered region must be closely nested inside a loop region (or
3129       // parallel loop region) with an ordered clause.
3130       // OpenMP [2.8.1,simd Construct, Restrictions]
3131       // An ordered construct with the simd clause is the only OpenMP construct
3132       // that can appear in the simd region.
3133       NestingProhibited = ParentRegion == OMPD_critical ||
3134                           isOpenMPTaskingDirective(ParentRegion) ||
3135                           !(isOpenMPSimdDirective(ParentRegion) ||
3136                             Stack->isParentOrderedRegion());
3137       Recommend = ShouldBeInOrderedRegion;
3138     } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
3139       // OpenMP [2.16, Nesting of Regions]
3140       // If specified, a teams construct must be contained within a target
3141       // construct.
3142       NestingProhibited = ParentRegion != OMPD_target;
3143       OrphanSeen = ParentRegion == OMPD_unknown;
3144       Recommend = ShouldBeInTargetRegion;
3145     }
3146     if (!NestingProhibited &&
3147         !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3148         !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3149         (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
3150       // OpenMP [2.16, Nesting of Regions]
3151       // distribute, parallel, parallel sections, parallel workshare, and the
3152       // parallel loop and parallel loop SIMD constructs are the only OpenMP
3153       // constructs that can be closely nested in the teams region.
3154       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3155                           !isOpenMPDistributeDirective(CurrentRegion);
3156       Recommend = ShouldBeInParallelRegion;
3157     }
3158     if (!NestingProhibited &&
3159         isOpenMPNestingDistributeDirective(CurrentRegion)) {
3160       // OpenMP 4.5 [2.17 Nesting of Regions]
3161       // The region associated with the distribute construct must be strictly
3162       // nested inside a teams region
3163       NestingProhibited =
3164           (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
3165       Recommend = ShouldBeInTeamsRegion;
3166     }
3167     if (!NestingProhibited &&
3168         (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3169          isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3170       // OpenMP 4.5 [2.17 Nesting of Regions]
3171       // If a target, target update, target data, target enter data, or
3172       // target exit data construct is encountered during execution of a
3173       // target region, the behavior is unspecified.
3174       NestingProhibited = Stack->hasDirective(
3175           [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3176                              SourceLocation) {
3177             if (isOpenMPTargetExecutionDirective(K)) {
3178               OffendingRegion = K;
3179               return true;
3180             }
3181             return false;
3182           },
3183           false /* don't skip top directive */);
3184       CloseNesting = false;
3185     }
3186     if (NestingProhibited) {
3187       if (OrphanSeen) {
3188         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3189             << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3190       } else {
3191         SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3192             << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3193             << Recommend << getOpenMPDirectiveName(CurrentRegion);
3194       }
3195       return true;
3196     }
3197   }
3198   return false;
3199 }
3200 
3201 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3202                            ArrayRef<OMPClause *> Clauses,
3203                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3204   bool ErrorFound = false;
3205   unsigned NamedModifiersNumber = 0;
3206   SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3207       OMPD_unknown + 1);
3208   SmallVector<SourceLocation, 4> NameModifierLoc;
3209   for (const OMPClause *C : Clauses) {
3210     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3211       // At most one if clause without a directive-name-modifier can appear on
3212       // the directive.
3213       OpenMPDirectiveKind CurNM = IC->getNameModifier();
3214       if (FoundNameModifiers[CurNM]) {
3215         S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
3216             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3217             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3218         ErrorFound = true;
3219       } else if (CurNM != OMPD_unknown) {
3220         NameModifierLoc.push_back(IC->getNameModifierLoc());
3221         ++NamedModifiersNumber;
3222       }
3223       FoundNameModifiers[CurNM] = IC;
3224       if (CurNM == OMPD_unknown)
3225         continue;
3226       // Check if the specified name modifier is allowed for the current
3227       // directive.
3228       // At most one if clause with the particular directive-name-modifier can
3229       // appear on the directive.
3230       bool MatchFound = false;
3231       for (auto NM : AllowedNameModifiers) {
3232         if (CurNM == NM) {
3233           MatchFound = true;
3234           break;
3235         }
3236       }
3237       if (!MatchFound) {
3238         S.Diag(IC->getNameModifierLoc(),
3239                diag::err_omp_wrong_if_directive_name_modifier)
3240             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3241         ErrorFound = true;
3242       }
3243     }
3244   }
3245   // If any if clause on the directive includes a directive-name-modifier then
3246   // all if clauses on the directive must include a directive-name-modifier.
3247   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3248     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3249       S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
3250              diag::err_omp_no_more_if_clause);
3251     } else {
3252       std::string Values;
3253       std::string Sep(", ");
3254       unsigned AllowedCnt = 0;
3255       unsigned TotalAllowedNum =
3256           AllowedNameModifiers.size() - NamedModifiersNumber;
3257       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3258            ++Cnt) {
3259         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3260         if (!FoundNameModifiers[NM]) {
3261           Values += "'";
3262           Values += getOpenMPDirectiveName(NM);
3263           Values += "'";
3264           if (AllowedCnt + 2 == TotalAllowedNum)
3265             Values += " or ";
3266           else if (AllowedCnt + 1 != TotalAllowedNum)
3267             Values += Sep;
3268           ++AllowedCnt;
3269         }
3270       }
3271       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
3272              diag::err_omp_unnamed_if_clause)
3273           << (TotalAllowedNum > 1) << Values;
3274     }
3275     for (SourceLocation Loc : NameModifierLoc) {
3276       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3277     }
3278     ErrorFound = true;
3279   }
3280   return ErrorFound;
3281 }
3282 
3283 StmtResult Sema::ActOnOpenMPExecutableDirective(
3284     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3285     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3286     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
3287   StmtResult Res = StmtError();
3288   // First check CancelRegion which is then used in checkNestingOfRegions.
3289   if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3290       checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3291                             StartLoc))
3292     return StmtError();
3293 
3294   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
3295   VarsWithInheritedDSAType VarsWithInheritedDSA;
3296   bool ErrorFound = false;
3297   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
3298   if (AStmt && !CurContext->isDependentContext()) {
3299     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3300 
3301     // Check default data sharing attributes for referenced variables.
3302     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3303     int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3304     Stmt *S = AStmt;
3305     while (--ThisCaptureLevel >= 0)
3306       S = cast<CapturedStmt>(S)->getCapturedStmt();
3307     DSAChecker.Visit(S);
3308     if (DSAChecker.isErrorFound())
3309       return StmtError();
3310     // Generate list of implicitly defined firstprivate variables.
3311     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
3312 
3313     SmallVector<Expr *, 4> ImplicitFirstprivates(
3314         DSAChecker.getImplicitFirstprivate().begin(),
3315         DSAChecker.getImplicitFirstprivate().end());
3316     SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3317                                         DSAChecker.getImplicitMap().end());
3318     // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
3319     for (OMPClause *C : Clauses) {
3320       if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
3321         for (Expr *E : IRC->taskgroup_descriptors())
3322           if (E)
3323             ImplicitFirstprivates.emplace_back(E);
3324       }
3325     }
3326     if (!ImplicitFirstprivates.empty()) {
3327       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3328               ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3329               SourceLocation())) {
3330         ClausesWithImplicit.push_back(Implicit);
3331         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3332                      ImplicitFirstprivates.size();
3333       } else {
3334         ErrorFound = true;
3335       }
3336     }
3337     if (!ImplicitMaps.empty()) {
3338       if (OMPClause *Implicit = ActOnOpenMPMapClause(
3339               llvm::None, llvm::None, OMPC_MAP_tofrom,
3340               /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(),
3341               ImplicitMaps, SourceLocation(), SourceLocation(),
3342               SourceLocation())) {
3343         ClausesWithImplicit.emplace_back(Implicit);
3344         ErrorFound |=
3345             cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
3346       } else {
3347         ErrorFound = true;
3348       }
3349     }
3350   }
3351 
3352   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
3353   switch (Kind) {
3354   case OMPD_parallel:
3355     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3356                                        EndLoc);
3357     AllowedNameModifiers.push_back(OMPD_parallel);
3358     break;
3359   case OMPD_simd:
3360     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3361                                    VarsWithInheritedDSA);
3362     break;
3363   case OMPD_for:
3364     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3365                                   VarsWithInheritedDSA);
3366     break;
3367   case OMPD_for_simd:
3368     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3369                                       EndLoc, VarsWithInheritedDSA);
3370     break;
3371   case OMPD_sections:
3372     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3373                                        EndLoc);
3374     break;
3375   case OMPD_section:
3376     assert(ClausesWithImplicit.empty() &&
3377            "No clauses are allowed for 'omp section' directive");
3378     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3379     break;
3380   case OMPD_single:
3381     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3382                                      EndLoc);
3383     break;
3384   case OMPD_master:
3385     assert(ClausesWithImplicit.empty() &&
3386            "No clauses are allowed for 'omp master' directive");
3387     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3388     break;
3389   case OMPD_critical:
3390     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3391                                        StartLoc, EndLoc);
3392     break;
3393   case OMPD_parallel_for:
3394     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3395                                           EndLoc, VarsWithInheritedDSA);
3396     AllowedNameModifiers.push_back(OMPD_parallel);
3397     break;
3398   case OMPD_parallel_for_simd:
3399     Res = ActOnOpenMPParallelForSimdDirective(
3400         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3401     AllowedNameModifiers.push_back(OMPD_parallel);
3402     break;
3403   case OMPD_parallel_sections:
3404     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3405                                                StartLoc, EndLoc);
3406     AllowedNameModifiers.push_back(OMPD_parallel);
3407     break;
3408   case OMPD_task:
3409     Res =
3410         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3411     AllowedNameModifiers.push_back(OMPD_task);
3412     break;
3413   case OMPD_taskyield:
3414     assert(ClausesWithImplicit.empty() &&
3415            "No clauses are allowed for 'omp taskyield' directive");
3416     assert(AStmt == nullptr &&
3417            "No associated statement allowed for 'omp taskyield' directive");
3418     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3419     break;
3420   case OMPD_barrier:
3421     assert(ClausesWithImplicit.empty() &&
3422            "No clauses are allowed for 'omp barrier' directive");
3423     assert(AStmt == nullptr &&
3424            "No associated statement allowed for 'omp barrier' directive");
3425     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3426     break;
3427   case OMPD_taskwait:
3428     assert(ClausesWithImplicit.empty() &&
3429            "No clauses are allowed for 'omp taskwait' directive");
3430     assert(AStmt == nullptr &&
3431            "No associated statement allowed for 'omp taskwait' directive");
3432     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3433     break;
3434   case OMPD_taskgroup:
3435     Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3436                                         EndLoc);
3437     break;
3438   case OMPD_flush:
3439     assert(AStmt == nullptr &&
3440            "No associated statement allowed for 'omp flush' directive");
3441     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3442     break;
3443   case OMPD_ordered:
3444     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3445                                       EndLoc);
3446     break;
3447   case OMPD_atomic:
3448     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3449                                      EndLoc);
3450     break;
3451   case OMPD_teams:
3452     Res =
3453         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3454     break;
3455   case OMPD_target:
3456     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3457                                      EndLoc);
3458     AllowedNameModifiers.push_back(OMPD_target);
3459     break;
3460   case OMPD_target_parallel:
3461     Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3462                                              StartLoc, EndLoc);
3463     AllowedNameModifiers.push_back(OMPD_target);
3464     AllowedNameModifiers.push_back(OMPD_parallel);
3465     break;
3466   case OMPD_target_parallel_for:
3467     Res = ActOnOpenMPTargetParallelForDirective(
3468         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3469     AllowedNameModifiers.push_back(OMPD_target);
3470     AllowedNameModifiers.push_back(OMPD_parallel);
3471     break;
3472   case OMPD_cancellation_point:
3473     assert(ClausesWithImplicit.empty() &&
3474            "No clauses are allowed for 'omp cancellation point' directive");
3475     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3476                                "cancellation point' directive");
3477     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3478     break;
3479   case OMPD_cancel:
3480     assert(AStmt == nullptr &&
3481            "No associated statement allowed for 'omp cancel' directive");
3482     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3483                                      CancelRegion);
3484     AllowedNameModifiers.push_back(OMPD_cancel);
3485     break;
3486   case OMPD_target_data:
3487     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3488                                          EndLoc);
3489     AllowedNameModifiers.push_back(OMPD_target_data);
3490     break;
3491   case OMPD_target_enter_data:
3492     Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3493                                               EndLoc, AStmt);
3494     AllowedNameModifiers.push_back(OMPD_target_enter_data);
3495     break;
3496   case OMPD_target_exit_data:
3497     Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3498                                              EndLoc, AStmt);
3499     AllowedNameModifiers.push_back(OMPD_target_exit_data);
3500     break;
3501   case OMPD_taskloop:
3502     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3503                                        EndLoc, VarsWithInheritedDSA);
3504     AllowedNameModifiers.push_back(OMPD_taskloop);
3505     break;
3506   case OMPD_taskloop_simd:
3507     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3508                                            EndLoc, VarsWithInheritedDSA);
3509     AllowedNameModifiers.push_back(OMPD_taskloop);
3510     break;
3511   case OMPD_distribute:
3512     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3513                                          EndLoc, VarsWithInheritedDSA);
3514     break;
3515   case OMPD_target_update:
3516     Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3517                                            EndLoc, AStmt);
3518     AllowedNameModifiers.push_back(OMPD_target_update);
3519     break;
3520   case OMPD_distribute_parallel_for:
3521     Res = ActOnOpenMPDistributeParallelForDirective(
3522         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3523     AllowedNameModifiers.push_back(OMPD_parallel);
3524     break;
3525   case OMPD_distribute_parallel_for_simd:
3526     Res = ActOnOpenMPDistributeParallelForSimdDirective(
3527         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3528     AllowedNameModifiers.push_back(OMPD_parallel);
3529     break;
3530   case OMPD_distribute_simd:
3531     Res = ActOnOpenMPDistributeSimdDirective(
3532         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3533     break;
3534   case OMPD_target_parallel_for_simd:
3535     Res = ActOnOpenMPTargetParallelForSimdDirective(
3536         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3537     AllowedNameModifiers.push_back(OMPD_target);
3538     AllowedNameModifiers.push_back(OMPD_parallel);
3539     break;
3540   case OMPD_target_simd:
3541     Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3542                                          EndLoc, VarsWithInheritedDSA);
3543     AllowedNameModifiers.push_back(OMPD_target);
3544     break;
3545   case OMPD_teams_distribute:
3546     Res = ActOnOpenMPTeamsDistributeDirective(
3547         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3548     break;
3549   case OMPD_teams_distribute_simd:
3550     Res = ActOnOpenMPTeamsDistributeSimdDirective(
3551         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3552     break;
3553   case OMPD_teams_distribute_parallel_for_simd:
3554     Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3555         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3556     AllowedNameModifiers.push_back(OMPD_parallel);
3557     break;
3558   case OMPD_teams_distribute_parallel_for:
3559     Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3560         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3561     AllowedNameModifiers.push_back(OMPD_parallel);
3562     break;
3563   case OMPD_target_teams:
3564     Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3565                                           EndLoc);
3566     AllowedNameModifiers.push_back(OMPD_target);
3567     break;
3568   case OMPD_target_teams_distribute:
3569     Res = ActOnOpenMPTargetTeamsDistributeDirective(
3570         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3571     AllowedNameModifiers.push_back(OMPD_target);
3572     break;
3573   case OMPD_target_teams_distribute_parallel_for:
3574     Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3575         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3576     AllowedNameModifiers.push_back(OMPD_target);
3577     AllowedNameModifiers.push_back(OMPD_parallel);
3578     break;
3579   case OMPD_target_teams_distribute_parallel_for_simd:
3580     Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3581         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3582     AllowedNameModifiers.push_back(OMPD_target);
3583     AllowedNameModifiers.push_back(OMPD_parallel);
3584     break;
3585   case OMPD_target_teams_distribute_simd:
3586     Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3587         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3588     AllowedNameModifiers.push_back(OMPD_target);
3589     break;
3590   case OMPD_declare_target:
3591   case OMPD_end_declare_target:
3592   case OMPD_threadprivate:
3593   case OMPD_declare_reduction:
3594   case OMPD_declare_simd:
3595   case OMPD_requires:
3596     llvm_unreachable("OpenMP Directive is not allowed");
3597   case OMPD_unknown:
3598     llvm_unreachable("Unknown OpenMP directive");
3599   }
3600 
3601   for (const auto &P : VarsWithInheritedDSA) {
3602     Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3603         << P.first << P.second->getSourceRange();
3604   }
3605   ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3606 
3607   if (!AllowedNameModifiers.empty())
3608     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3609                  ErrorFound;
3610 
3611   if (ErrorFound)
3612     return StmtError();
3613   return Res;
3614 }
3615 
3616 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3617     DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
3618     ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
3619     ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3620     ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
3621   assert(Aligneds.size() == Alignments.size());
3622   assert(Linears.size() == LinModifiers.size());
3623   assert(Linears.size() == Steps.size());
3624   if (!DG || DG.get().isNull())
3625     return DeclGroupPtrTy();
3626 
3627   if (!DG.get().isSingleDecl()) {
3628     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
3629     return DG;
3630   }
3631   Decl *ADecl = DG.get().getSingleDecl();
3632   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3633     ADecl = FTD->getTemplatedDecl();
3634 
3635   auto *FD = dyn_cast<FunctionDecl>(ADecl);
3636   if (!FD) {
3637     Diag(ADecl->getLocation(), diag::err_omp_function_expected);
3638     return DeclGroupPtrTy();
3639   }
3640 
3641   // OpenMP [2.8.2, declare simd construct, Description]
3642   // The parameter of the simdlen clause must be a constant positive integer
3643   // expression.
3644   ExprResult SL;
3645   if (Simdlen)
3646     SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
3647   // OpenMP [2.8.2, declare simd construct, Description]
3648   // The special this pointer can be used as if was one of the arguments to the
3649   // function in any of the linear, aligned, or uniform clauses.
3650   // The uniform clause declares one or more arguments to have an invariant
3651   // value for all concurrent invocations of the function in the execution of a
3652   // single SIMD loop.
3653   llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3654   const Expr *UniformedLinearThis = nullptr;
3655   for (const Expr *E : Uniforms) {
3656     E = E->IgnoreParenImpCasts();
3657     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3658       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3659         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3660             FD->getParamDecl(PVD->getFunctionScopeIndex())
3661                     ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3662           UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
3663           continue;
3664         }
3665     if (isa<CXXThisExpr>(E)) {
3666       UniformedLinearThis = E;
3667       continue;
3668     }
3669     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3670         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3671   }
3672   // OpenMP [2.8.2, declare simd construct, Description]
3673   // The aligned clause declares that the object to which each list item points
3674   // is aligned to the number of bytes expressed in the optional parameter of
3675   // the aligned clause.
3676   // The special this pointer can be used as if was one of the arguments to the
3677   // function in any of the linear, aligned, or uniform clauses.
3678   // The type of list items appearing in the aligned clause must be array,
3679   // pointer, reference to array, or reference to pointer.
3680   llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3681   const Expr *AlignedThis = nullptr;
3682   for (const Expr *E : Aligneds) {
3683     E = E->IgnoreParenImpCasts();
3684     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3685       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3686         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
3687         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3688             FD->getParamDecl(PVD->getFunctionScopeIndex())
3689                     ->getCanonicalDecl() == CanonPVD) {
3690           // OpenMP  [2.8.1, simd construct, Restrictions]
3691           // A list-item cannot appear in more than one aligned clause.
3692           if (AlignedArgs.count(CanonPVD) > 0) {
3693             Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3694                 << 1 << E->getSourceRange();
3695             Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3696                  diag::note_omp_explicit_dsa)
3697                 << getOpenMPClauseName(OMPC_aligned);
3698             continue;
3699           }
3700           AlignedArgs[CanonPVD] = E;
3701           QualType QTy = PVD->getType()
3702                              .getNonReferenceType()
3703                              .getUnqualifiedType()
3704                              .getCanonicalType();
3705           const Type *Ty = QTy.getTypePtrOrNull();
3706           if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3707             Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3708                 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3709             Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3710           }
3711           continue;
3712         }
3713       }
3714     if (isa<CXXThisExpr>(E)) {
3715       if (AlignedThis) {
3716         Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3717             << 2 << E->getSourceRange();
3718         Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3719             << getOpenMPClauseName(OMPC_aligned);
3720       }
3721       AlignedThis = E;
3722       continue;
3723     }
3724     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3725         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3726   }
3727   // The optional parameter of the aligned clause, alignment, must be a constant
3728   // positive integer expression. If no optional parameter is specified,
3729   // implementation-defined default alignments for SIMD instructions on the
3730   // target platforms are assumed.
3731   SmallVector<const Expr *, 4> NewAligns;
3732   for (Expr *E : Alignments) {
3733     ExprResult Align;
3734     if (E)
3735       Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3736     NewAligns.push_back(Align.get());
3737   }
3738   // OpenMP [2.8.2, declare simd construct, Description]
3739   // The linear clause declares one or more list items to be private to a SIMD
3740   // lane and to have a linear relationship with respect to the iteration space
3741   // of a loop.
3742   // The special this pointer can be used as if was one of the arguments to the
3743   // function in any of the linear, aligned, or uniform clauses.
3744   // When a linear-step expression is specified in a linear clause it must be
3745   // either a constant integer expression or an integer-typed parameter that is
3746   // specified in a uniform clause on the directive.
3747   llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
3748   const bool IsUniformedThis = UniformedLinearThis != nullptr;
3749   auto MI = LinModifiers.begin();
3750   for (const Expr *E : Linears) {
3751     auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3752     ++MI;
3753     E = E->IgnoreParenImpCasts();
3754     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3755       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3756         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
3757         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3758             FD->getParamDecl(PVD->getFunctionScopeIndex())
3759                     ->getCanonicalDecl() == CanonPVD) {
3760           // OpenMP  [2.15.3.7, linear Clause, Restrictions]
3761           // A list-item cannot appear in more than one linear clause.
3762           if (LinearArgs.count(CanonPVD) > 0) {
3763             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3764                 << getOpenMPClauseName(OMPC_linear)
3765                 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3766             Diag(LinearArgs[CanonPVD]->getExprLoc(),
3767                  diag::note_omp_explicit_dsa)
3768                 << getOpenMPClauseName(OMPC_linear);
3769             continue;
3770           }
3771           // Each argument can appear in at most one uniform or linear clause.
3772           if (UniformedArgs.count(CanonPVD) > 0) {
3773             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3774                 << getOpenMPClauseName(OMPC_linear)
3775                 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3776             Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3777                  diag::note_omp_explicit_dsa)
3778                 << getOpenMPClauseName(OMPC_uniform);
3779             continue;
3780           }
3781           LinearArgs[CanonPVD] = E;
3782           if (E->isValueDependent() || E->isTypeDependent() ||
3783               E->isInstantiationDependent() ||
3784               E->containsUnexpandedParameterPack())
3785             continue;
3786           (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3787                                       PVD->getOriginalType());
3788           continue;
3789         }
3790       }
3791     if (isa<CXXThisExpr>(E)) {
3792       if (UniformedLinearThis) {
3793         Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3794             << getOpenMPClauseName(OMPC_linear)
3795             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3796             << E->getSourceRange();
3797         Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3798             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3799                                                    : OMPC_linear);
3800         continue;
3801       }
3802       UniformedLinearThis = E;
3803       if (E->isValueDependent() || E->isTypeDependent() ||
3804           E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3805         continue;
3806       (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3807                                   E->getType());
3808       continue;
3809     }
3810     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3811         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3812   }
3813   Expr *Step = nullptr;
3814   Expr *NewStep = nullptr;
3815   SmallVector<Expr *, 4> NewSteps;
3816   for (Expr *E : Steps) {
3817     // Skip the same step expression, it was checked already.
3818     if (Step == E || !E) {
3819       NewSteps.push_back(E ? NewStep : nullptr);
3820       continue;
3821     }
3822     Step = E;
3823     if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
3824       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3825         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
3826         if (UniformedArgs.count(CanonPVD) == 0) {
3827           Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3828               << Step->getSourceRange();
3829         } else if (E->isValueDependent() || E->isTypeDependent() ||
3830                    E->isInstantiationDependent() ||
3831                    E->containsUnexpandedParameterPack() ||
3832                    CanonPVD->getType()->hasIntegerRepresentation()) {
3833           NewSteps.push_back(Step);
3834         } else {
3835           Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3836               << Step->getSourceRange();
3837         }
3838         continue;
3839       }
3840     NewStep = Step;
3841     if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3842         !Step->isInstantiationDependent() &&
3843         !Step->containsUnexpandedParameterPack()) {
3844       NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3845                     .get();
3846       if (NewStep)
3847         NewStep = VerifyIntegerConstantExpression(NewStep).get();
3848     }
3849     NewSteps.push_back(NewStep);
3850   }
3851   auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3852       Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
3853       Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
3854       const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3855       const_cast<Expr **>(Linears.data()), Linears.size(),
3856       const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3857       NewSteps.data(), NewSteps.size(), SR);
3858   ADecl->addAttr(NewAttr);
3859   return ConvertDeclToDeclGroup(ADecl);
3860 }
3861 
3862 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3863                                               Stmt *AStmt,
3864                                               SourceLocation StartLoc,
3865                                               SourceLocation EndLoc) {
3866   if (!AStmt)
3867     return StmtError();
3868 
3869   auto *CS = cast<CapturedStmt>(AStmt);
3870   // 1.2.2 OpenMP Language Terminology
3871   // Structured block - An executable statement with a single entry at the
3872   // top and a single exit at the bottom.
3873   // The point of exit cannot be a branch out of the structured block.
3874   // longjmp() and throw() must not violate the entry/exit criteria.
3875   CS->getCapturedDecl()->setNothrow();
3876 
3877   setFunctionHasBranchProtectedScope();
3878 
3879   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3880                                       DSAStack->isCancelRegion());
3881 }
3882 
3883 namespace {
3884 /// Helper class for checking canonical form of the OpenMP loops and
3885 /// extracting iteration space of each loop in the loop nest, that will be used
3886 /// for IR generation.
3887 class OpenMPIterationSpaceChecker {
3888   /// Reference to Sema.
3889   Sema &SemaRef;
3890   /// A location for diagnostics (when there is no some better location).
3891   SourceLocation DefaultLoc;
3892   /// A location for diagnostics (when increment is not compatible).
3893   SourceLocation ConditionLoc;
3894   /// A source location for referring to loop init later.
3895   SourceRange InitSrcRange;
3896   /// A source location for referring to condition later.
3897   SourceRange ConditionSrcRange;
3898   /// A source location for referring to increment later.
3899   SourceRange IncrementSrcRange;
3900   /// Loop variable.
3901   ValueDecl *LCDecl = nullptr;
3902   /// Reference to loop variable.
3903   Expr *LCRef = nullptr;
3904   /// Lower bound (initializer for the var).
3905   Expr *LB = nullptr;
3906   /// Upper bound.
3907   Expr *UB = nullptr;
3908   /// Loop step (increment).
3909   Expr *Step = nullptr;
3910   /// This flag is true when condition is one of:
3911   ///   Var <  UB
3912   ///   Var <= UB
3913   ///   UB  >  Var
3914   ///   UB  >= Var
3915   /// This will have no value when the condition is !=
3916   llvm::Optional<bool> TestIsLessOp;
3917   /// This flag is true when condition is strict ( < or > ).
3918   bool TestIsStrictOp = false;
3919   /// This flag is true when step is subtracted on each iteration.
3920   bool SubtractStep = false;
3921 
3922 public:
3923   OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3924       : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
3925   /// Check init-expr for canonical loop form and save loop counter
3926   /// variable - #Var and its initialization value - #LB.
3927   bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
3928   /// Check test-expr for canonical form, save upper-bound (#UB), flags
3929   /// for less/greater and for strict/non-strict comparison.
3930   bool checkAndSetCond(Expr *S);
3931   /// Check incr-expr for canonical loop form and return true if it
3932   /// does not conform, otherwise save loop step (#Step).
3933   bool checkAndSetInc(Expr *S);
3934   /// Return the loop counter variable.
3935   ValueDecl *getLoopDecl() const { return LCDecl; }
3936   /// Return the reference expression to loop counter variable.
3937   Expr *getLoopDeclRefExpr() const { return LCRef; }
3938   /// Source range of the loop init.
3939   SourceRange getInitSrcRange() const { return InitSrcRange; }
3940   /// Source range of the loop condition.
3941   SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
3942   /// Source range of the loop increment.
3943   SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
3944   /// True if the step should be subtracted.
3945   bool shouldSubtractStep() const { return SubtractStep; }
3946   /// Build the expression to calculate the number of iterations.
3947   Expr *buildNumIterations(
3948       Scope *S, const bool LimitedType,
3949       llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
3950   /// Build the precondition expression for the loops.
3951   Expr *
3952   buildPreCond(Scope *S, Expr *Cond,
3953                llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
3954   /// Build reference expression to the counter be used for codegen.
3955   DeclRefExpr *
3956   buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3957                   DSAStackTy &DSA) const;
3958   /// Build reference expression to the private counter be used for
3959   /// codegen.
3960   Expr *buildPrivateCounterVar() const;
3961   /// Build initialization of the counter be used for codegen.
3962   Expr *buildCounterInit() const;
3963   /// Build step of the counter be used for codegen.
3964   Expr *buildCounterStep() const;
3965   /// Build loop data with counter value for depend clauses in ordered
3966   /// directives.
3967   Expr *
3968   buildOrderedLoopData(Scope *S, Expr *Counter,
3969                        llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3970                        SourceLocation Loc, Expr *Inc = nullptr,
3971                        OverloadedOperatorKind OOK = OO_Amp);
3972   /// Return true if any expression is dependent.
3973   bool dependent() const;
3974 
3975 private:
3976   /// Check the right-hand side of an assignment in the increment
3977   /// expression.
3978   bool checkAndSetIncRHS(Expr *RHS);
3979   /// Helper to set loop counter variable and its initializer.
3980   bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
3981   /// Helper to set upper bound.
3982   bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
3983              SourceRange SR, SourceLocation SL);
3984   /// Helper to set loop increment.
3985   bool setStep(Expr *NewStep, bool Subtract);
3986 };
3987 
3988 bool OpenMPIterationSpaceChecker::dependent() const {
3989   if (!LCDecl) {
3990     assert(!LB && !UB && !Step);
3991     return false;
3992   }
3993   return LCDecl->getType()->isDependentType() ||
3994          (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3995          (Step && Step->isValueDependent());
3996 }
3997 
3998 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
3999                                                  Expr *NewLCRefExpr,
4000                                                  Expr *NewLB) {
4001   // State consistency checking to ensure correct usage.
4002   assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
4003          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
4004   if (!NewLCDecl || !NewLB)
4005     return true;
4006   LCDecl = getCanonicalDecl(NewLCDecl);
4007   LCRef = NewLCRefExpr;
4008   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4009     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
4010       if ((Ctor->isCopyOrMoveConstructor() ||
4011            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4012           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
4013         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
4014   LB = NewLB;
4015   return false;
4016 }
4017 
4018 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, llvm::Optional<bool> LessOp,
4019                                         bool StrictOp, SourceRange SR,
4020                                         SourceLocation SL) {
4021   // State consistency checking to ensure correct usage.
4022   assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4023          Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
4024   if (!NewUB)
4025     return true;
4026   UB = NewUB;
4027   if (LessOp)
4028     TestIsLessOp = LessOp;
4029   TestIsStrictOp = StrictOp;
4030   ConditionSrcRange = SR;
4031   ConditionLoc = SL;
4032   return false;
4033 }
4034 
4035 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
4036   // State consistency checking to ensure correct usage.
4037   assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
4038   if (!NewStep)
4039     return true;
4040   if (!NewStep->isValueDependent()) {
4041     // Check that the step is integer expression.
4042     SourceLocation StepLoc = NewStep->getBeginLoc();
4043     ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
4044         StepLoc, getExprAsWritten(NewStep));
4045     if (Val.isInvalid())
4046       return true;
4047     NewStep = Val.get();
4048 
4049     // OpenMP [2.6, Canonical Loop Form, Restrictions]
4050     //  If test-expr is of form var relational-op b and relational-op is < or
4051     //  <= then incr-expr must cause var to increase on each iteration of the
4052     //  loop. If test-expr is of form var relational-op b and relational-op is
4053     //  > or >= then incr-expr must cause var to decrease on each iteration of
4054     //  the loop.
4055     //  If test-expr is of form b relational-op var and relational-op is < or
4056     //  <= then incr-expr must cause var to decrease on each iteration of the
4057     //  loop. If test-expr is of form b relational-op var and relational-op is
4058     //  > or >= then incr-expr must cause var to increase on each iteration of
4059     //  the loop.
4060     llvm::APSInt Result;
4061     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4062     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4063     bool IsConstNeg =
4064         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
4065     bool IsConstPos =
4066         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
4067     bool IsConstZero = IsConstant && !Result.getBoolValue();
4068 
4069     // != with increment is treated as <; != with decrement is treated as >
4070     if (!TestIsLessOp.hasValue())
4071       TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
4072     if (UB && (IsConstZero ||
4073                (TestIsLessOp.getValue() ?
4074                   (IsConstNeg || (IsUnsigned && Subtract)) :
4075                   (IsConstPos || (IsUnsigned && !Subtract))))) {
4076       SemaRef.Diag(NewStep->getExprLoc(),
4077                    diag::err_omp_loop_incr_not_compatible)
4078           << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
4079       SemaRef.Diag(ConditionLoc,
4080                    diag::note_omp_loop_cond_requres_compatible_incr)
4081           << TestIsLessOp.getValue() << ConditionSrcRange;
4082       return true;
4083     }
4084     if (TestIsLessOp.getValue() == Subtract) {
4085       NewStep =
4086           SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4087               .get();
4088       Subtract = !Subtract;
4089     }
4090   }
4091 
4092   Step = NewStep;
4093   SubtractStep = Subtract;
4094   return false;
4095 }
4096 
4097 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
4098   // Check init-expr for canonical loop form and save loop counter
4099   // variable - #Var and its initialization value - #LB.
4100   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4101   //   var = lb
4102   //   integer-type var = lb
4103   //   random-access-iterator-type var = lb
4104   //   pointer-type var = lb
4105   //
4106   if (!S) {
4107     if (EmitDiags) {
4108       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4109     }
4110     return true;
4111   }
4112   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4113     if (!ExprTemp->cleanupsHaveSideEffects())
4114       S = ExprTemp->getSubExpr();
4115 
4116   InitSrcRange = S->getSourceRange();
4117   if (Expr *E = dyn_cast<Expr>(S))
4118     S = E->IgnoreParens();
4119   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
4120     if (BO->getOpcode() == BO_Assign) {
4121       Expr *LHS = BO->getLHS()->IgnoreParens();
4122       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4123         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4124           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4125             return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4126         return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4127       }
4128       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4129         if (ME->isArrow() &&
4130             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4131           return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4132       }
4133     }
4134   } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
4135     if (DS->isSingleDecl()) {
4136       if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
4137         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
4138           // Accept non-canonical init form here but emit ext. warning.
4139           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
4140             SemaRef.Diag(S->getBeginLoc(),
4141                          diag::ext_omp_loop_not_canonical_init)
4142                 << S->getSourceRange();
4143           return setLCDeclAndLB(
4144               Var,
4145               buildDeclRefExpr(SemaRef, Var,
4146                                Var->getType().getNonReferenceType(),
4147                                DS->getBeginLoc()),
4148               Var->getInit());
4149         }
4150       }
4151     }
4152   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4153     if (CE->getOperator() == OO_Equal) {
4154       Expr *LHS = CE->getArg(0);
4155       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4156         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4157           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4158             return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4159         return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4160       }
4161       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4162         if (ME->isArrow() &&
4163             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4164           return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4165       }
4166     }
4167   }
4168 
4169   if (dependent() || SemaRef.CurContext->isDependentContext())
4170     return false;
4171   if (EmitDiags) {
4172     SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
4173         << S->getSourceRange();
4174   }
4175   return true;
4176 }
4177 
4178 /// Ignore parenthesizes, implicit casts, copy constructor and return the
4179 /// variable (which may be the loop variable) if possible.
4180 static const ValueDecl *getInitLCDecl(const Expr *E) {
4181   if (!E)
4182     return nullptr;
4183   E = getExprAsWritten(E);
4184   if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4185     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
4186       if ((Ctor->isCopyOrMoveConstructor() ||
4187            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4188           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
4189         E = CE->getArg(0)->IgnoreParenImpCasts();
4190   if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4191     if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
4192       return getCanonicalDecl(VD);
4193   }
4194   if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
4195     if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4196       return getCanonicalDecl(ME->getMemberDecl());
4197   return nullptr;
4198 }
4199 
4200 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
4201   // Check test-expr for canonical form, save upper-bound UB, flags for
4202   // less/greater and for strict/non-strict comparison.
4203   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4204   //   var relational-op b
4205   //   b relational-op var
4206   //
4207   if (!S) {
4208     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
4209     return true;
4210   }
4211   S = getExprAsWritten(S);
4212   SourceLocation CondLoc = S->getBeginLoc();
4213   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
4214     if (BO->isRelationalOp()) {
4215       if (getInitLCDecl(BO->getLHS()) == LCDecl)
4216         return setUB(BO->getRHS(),
4217                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4218                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4219                      BO->getSourceRange(), BO->getOperatorLoc());
4220       if (getInitLCDecl(BO->getRHS()) == LCDecl)
4221         return setUB(BO->getLHS(),
4222                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4223                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4224                      BO->getSourceRange(), BO->getOperatorLoc());
4225     } else if (BO->getOpcode() == BO_NE)
4226         return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ?
4227                        BO->getRHS() : BO->getLHS(),
4228                      /*LessOp=*/llvm::None,
4229                      /*StrictOp=*/true,
4230                      BO->getSourceRange(), BO->getOperatorLoc());
4231   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4232     if (CE->getNumArgs() == 2) {
4233       auto Op = CE->getOperator();
4234       switch (Op) {
4235       case OO_Greater:
4236       case OO_GreaterEqual:
4237       case OO_Less:
4238       case OO_LessEqual:
4239         if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4240           return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4241                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4242                        CE->getOperatorLoc());
4243         if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4244           return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4245                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4246                        CE->getOperatorLoc());
4247         break;
4248       case OO_ExclaimEqual:
4249         return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ?
4250                      CE->getArg(1) : CE->getArg(0),
4251                      /*LessOp=*/llvm::None,
4252                      /*StrictOp=*/true,
4253                      CE->getSourceRange(),
4254                      CE->getOperatorLoc());
4255         break;
4256       default:
4257         break;
4258       }
4259     }
4260   }
4261   if (dependent() || SemaRef.CurContext->isDependentContext())
4262     return false;
4263   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
4264       << S->getSourceRange() << LCDecl;
4265   return true;
4266 }
4267 
4268 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
4269   // RHS of canonical loop form increment can be:
4270   //   var + incr
4271   //   incr + var
4272   //   var - incr
4273   //
4274   RHS = RHS->IgnoreParenImpCasts();
4275   if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
4276     if (BO->isAdditiveOp()) {
4277       bool IsAdd = BO->getOpcode() == BO_Add;
4278       if (getInitLCDecl(BO->getLHS()) == LCDecl)
4279         return setStep(BO->getRHS(), !IsAdd);
4280       if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4281         return setStep(BO->getLHS(), /*Subtract=*/false);
4282     }
4283   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4284     bool IsAdd = CE->getOperator() == OO_Plus;
4285     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
4286       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4287         return setStep(CE->getArg(1), !IsAdd);
4288       if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4289         return setStep(CE->getArg(0), /*Subtract=*/false);
4290     }
4291   }
4292   if (dependent() || SemaRef.CurContext->isDependentContext())
4293     return false;
4294   SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
4295       << RHS->getSourceRange() << LCDecl;
4296   return true;
4297 }
4298 
4299 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
4300   // Check incr-expr for canonical loop form and return true if it
4301   // does not conform.
4302   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4303   //   ++var
4304   //   var++
4305   //   --var
4306   //   var--
4307   //   var += incr
4308   //   var -= incr
4309   //   var = var + incr
4310   //   var = incr + var
4311   //   var = var - incr
4312   //
4313   if (!S) {
4314     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
4315     return true;
4316   }
4317   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4318     if (!ExprTemp->cleanupsHaveSideEffects())
4319       S = ExprTemp->getSubExpr();
4320 
4321   IncrementSrcRange = S->getSourceRange();
4322   S = S->IgnoreParens();
4323   if (auto *UO = dyn_cast<UnaryOperator>(S)) {
4324     if (UO->isIncrementDecrementOp() &&
4325         getInitLCDecl(UO->getSubExpr()) == LCDecl)
4326       return setStep(SemaRef
4327                          .ActOnIntegerConstant(UO->getBeginLoc(),
4328                                                (UO->isDecrementOp() ? -1 : 1))
4329                          .get(),
4330                      /*Subtract=*/false);
4331   } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
4332     switch (BO->getOpcode()) {
4333     case BO_AddAssign:
4334     case BO_SubAssign:
4335       if (getInitLCDecl(BO->getLHS()) == LCDecl)
4336         return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4337       break;
4338     case BO_Assign:
4339       if (getInitLCDecl(BO->getLHS()) == LCDecl)
4340         return checkAndSetIncRHS(BO->getRHS());
4341       break;
4342     default:
4343       break;
4344     }
4345   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4346     switch (CE->getOperator()) {
4347     case OO_PlusPlus:
4348     case OO_MinusMinus:
4349       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4350         return setStep(SemaRef
4351                            .ActOnIntegerConstant(
4352                                CE->getBeginLoc(),
4353                                ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4354                            .get(),
4355                        /*Subtract=*/false);
4356       break;
4357     case OO_PlusEqual:
4358     case OO_MinusEqual:
4359       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4360         return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4361       break;
4362     case OO_Equal:
4363       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4364         return checkAndSetIncRHS(CE->getArg(1));
4365       break;
4366     default:
4367       break;
4368     }
4369   }
4370   if (dependent() || SemaRef.CurContext->isDependentContext())
4371     return false;
4372   SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
4373       << S->getSourceRange() << LCDecl;
4374   return true;
4375 }
4376 
4377 static ExprResult
4378 tryBuildCapture(Sema &SemaRef, Expr *Capture,
4379                 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
4380   if (SemaRef.CurContext->isDependentContext())
4381     return ExprResult(Capture);
4382   if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4383     return SemaRef.PerformImplicitConversion(
4384         Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4385         /*AllowExplicit=*/true);
4386   auto I = Captures.find(Capture);
4387   if (I != Captures.end())
4388     return buildCapture(SemaRef, Capture, I->second);
4389   DeclRefExpr *Ref = nullptr;
4390   ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4391   Captures[Capture] = Ref;
4392   return Res;
4393 }
4394 
4395 /// Build the expression to calculate the number of iterations.
4396 Expr *OpenMPIterationSpaceChecker::buildNumIterations(
4397     Scope *S, const bool LimitedType,
4398     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
4399   ExprResult Diff;
4400   QualType VarType = LCDecl->getType().getNonReferenceType();
4401   if (VarType->isIntegerType() || VarType->isPointerType() ||
4402       SemaRef.getLangOpts().CPlusPlus) {
4403     // Upper - Lower
4404     Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
4405     Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
4406     Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4407     Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
4408     if (!Upper || !Lower)
4409       return nullptr;
4410 
4411     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4412 
4413     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4414       // BuildBinOp already emitted error, this one is to point user to upper
4415       // and lower bound, and to tell what is passed to 'operator-'.
4416       SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4417           << Upper->getSourceRange() << Lower->getSourceRange();
4418       return nullptr;
4419     }
4420   }
4421 
4422   if (!Diff.isUsable())
4423     return nullptr;
4424 
4425   // Upper - Lower [- 1]
4426   if (TestIsStrictOp)
4427     Diff = SemaRef.BuildBinOp(
4428         S, DefaultLoc, BO_Sub, Diff.get(),
4429         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4430   if (!Diff.isUsable())
4431     return nullptr;
4432 
4433   // Upper - Lower [- 1] + Step
4434   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4435   if (!NewStep.isUsable())
4436     return nullptr;
4437   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
4438   if (!Diff.isUsable())
4439     return nullptr;
4440 
4441   // Parentheses (for dumping/debugging purposes only).
4442   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4443   if (!Diff.isUsable())
4444     return nullptr;
4445 
4446   // (Upper - Lower [- 1] + Step) / Step
4447   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4448   if (!Diff.isUsable())
4449     return nullptr;
4450 
4451   // OpenMP runtime requires 32-bit or 64-bit loop variables.
4452   QualType Type = Diff.get()->getType();
4453   ASTContext &C = SemaRef.Context;
4454   bool UseVarType = VarType->hasIntegerRepresentation() &&
4455                     C.getTypeSize(Type) > C.getTypeSize(VarType);
4456   if (!Type->isIntegerType() || UseVarType) {
4457     unsigned NewSize =
4458         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4459     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4460                                : Type->hasSignedIntegerRepresentation();
4461     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
4462     if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4463       Diff = SemaRef.PerformImplicitConversion(
4464           Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4465       if (!Diff.isUsable())
4466         return nullptr;
4467     }
4468   }
4469   if (LimitedType) {
4470     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4471     if (NewSize != C.getTypeSize(Type)) {
4472       if (NewSize < C.getTypeSize(Type)) {
4473         assert(NewSize == 64 && "incorrect loop var size");
4474         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4475             << InitSrcRange << ConditionSrcRange;
4476       }
4477       QualType NewType = C.getIntTypeForBitwidth(
4478           NewSize, Type->hasSignedIntegerRepresentation() ||
4479                        C.getTypeSize(Type) < NewSize);
4480       if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4481         Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4482                                                  Sema::AA_Converting, true);
4483         if (!Diff.isUsable())
4484           return nullptr;
4485       }
4486     }
4487   }
4488 
4489   return Diff.get();
4490 }
4491 
4492 Expr *OpenMPIterationSpaceChecker::buildPreCond(
4493     Scope *S, Expr *Cond,
4494     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
4495   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4496   bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4497   SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4498 
4499   ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4500   ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
4501   if (!NewLB.isUsable() || !NewUB.isUsable())
4502     return nullptr;
4503 
4504   ExprResult CondExpr =
4505       SemaRef.BuildBinOp(S, DefaultLoc,
4506                          TestIsLessOp.getValue() ?
4507                            (TestIsStrictOp ? BO_LT : BO_LE) :
4508                            (TestIsStrictOp ? BO_GT : BO_GE),
4509                          NewLB.get(), NewUB.get());
4510   if (CondExpr.isUsable()) {
4511     if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4512                                                 SemaRef.Context.BoolTy))
4513       CondExpr = SemaRef.PerformImplicitConversion(
4514           CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4515           /*AllowExplicit=*/true);
4516   }
4517   SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4518   // Otherwise use original loop conditon and evaluate it in runtime.
4519   return CondExpr.isUsable() ? CondExpr.get() : Cond;
4520 }
4521 
4522 /// Build reference expression to the counter be used for codegen.
4523 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
4524     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4525     DSAStackTy &DSA) const {
4526   auto *VD = dyn_cast<VarDecl>(LCDecl);
4527   if (!VD) {
4528     VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4529     DeclRefExpr *Ref = buildDeclRefExpr(
4530         SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
4531     const DSAStackTy::DSAVarData Data =
4532         DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4533     // If the loop control decl is explicitly marked as private, do not mark it
4534     // as captured again.
4535     if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4536       Captures.insert(std::make_pair(LCRef, Ref));
4537     return Ref;
4538   }
4539   return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
4540                           DefaultLoc);
4541 }
4542 
4543 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
4544   if (LCDecl && !LCDecl->isInvalidDecl()) {
4545     QualType Type = LCDecl->getType().getNonReferenceType();
4546     VarDecl *PrivateVar = buildVarDecl(
4547         SemaRef, DefaultLoc, Type, LCDecl->getName(),
4548         LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4549         isa<VarDecl>(LCDecl)
4550             ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4551             : nullptr);
4552     if (PrivateVar->isInvalidDecl())
4553       return nullptr;
4554     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4555   }
4556   return nullptr;
4557 }
4558 
4559 /// Build initialization of the counter to be used for codegen.
4560 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
4561 
4562 /// Build step of the counter be used for codegen.
4563 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
4564 
4565 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4566     Scope *S, Expr *Counter,
4567     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4568     Expr *Inc, OverloadedOperatorKind OOK) {
4569   Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4570   if (!Cnt)
4571     return nullptr;
4572   if (Inc) {
4573     assert((OOK == OO_Plus || OOK == OO_Minus) &&
4574            "Expected only + or - operations for depend clauses.");
4575     BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4576     Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4577     if (!Cnt)
4578       return nullptr;
4579   }
4580   ExprResult Diff;
4581   QualType VarType = LCDecl->getType().getNonReferenceType();
4582   if (VarType->isIntegerType() || VarType->isPointerType() ||
4583       SemaRef.getLangOpts().CPlusPlus) {
4584     // Upper - Lower
4585     Expr *Upper =
4586         TestIsLessOp.getValue() ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get();
4587     Expr *Lower =
4588         TestIsLessOp.getValue() ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt;
4589     if (!Upper || !Lower)
4590       return nullptr;
4591 
4592     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4593 
4594     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4595       // BuildBinOp already emitted error, this one is to point user to upper
4596       // and lower bound, and to tell what is passed to 'operator-'.
4597       SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4598           << Upper->getSourceRange() << Lower->getSourceRange();
4599       return nullptr;
4600     }
4601   }
4602 
4603   if (!Diff.isUsable())
4604     return nullptr;
4605 
4606   // Parentheses (for dumping/debugging purposes only).
4607   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4608   if (!Diff.isUsable())
4609     return nullptr;
4610 
4611   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4612   if (!NewStep.isUsable())
4613     return nullptr;
4614   // (Upper - Lower) / Step
4615   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4616   if (!Diff.isUsable())
4617     return nullptr;
4618 
4619   return Diff.get();
4620 }
4621 
4622 /// Iteration space of a single for loop.
4623 struct LoopIterationSpace final {
4624   /// Condition of the loop.
4625   Expr *PreCond = nullptr;
4626   /// This expression calculates the number of iterations in the loop.
4627   /// It is always possible to calculate it before starting the loop.
4628   Expr *NumIterations = nullptr;
4629   /// The loop counter variable.
4630   Expr *CounterVar = nullptr;
4631   /// Private loop counter variable.
4632   Expr *PrivateCounterVar = nullptr;
4633   /// This is initializer for the initial value of #CounterVar.
4634   Expr *CounterInit = nullptr;
4635   /// This is step for the #CounterVar used to generate its update:
4636   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4637   Expr *CounterStep = nullptr;
4638   /// Should step be subtracted?
4639   bool Subtract = false;
4640   /// Source range of the loop init.
4641   SourceRange InitSrcRange;
4642   /// Source range of the loop condition.
4643   SourceRange CondSrcRange;
4644   /// Source range of the loop increment.
4645   SourceRange IncSrcRange;
4646 };
4647 
4648 } // namespace
4649 
4650 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4651   assert(getLangOpts().OpenMP && "OpenMP is not active.");
4652   assert(Init && "Expected loop in canonical form.");
4653   unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4654   if (AssociatedLoops > 0 &&
4655       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4656     OpenMPIterationSpaceChecker ISC(*this, ForLoc);
4657     if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4658       if (ValueDecl *D = ISC.getLoopDecl()) {
4659         auto *VD = dyn_cast<VarDecl>(D);
4660         if (!VD) {
4661           if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
4662             VD = Private;
4663           } else {
4664             DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4665                                             /*WithInit=*/false);
4666             VD = cast<VarDecl>(Ref->getDecl());
4667           }
4668         }
4669         DSAStack->addLoopControlVariable(D, VD);
4670         const Decl *LD = DSAStack->getPossiblyLoopCunter();
4671         if (LD != D->getCanonicalDecl()) {
4672           DSAStack->resetPossibleLoopCounter();
4673           if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
4674             MarkDeclarationsReferencedInExpr(
4675                 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
4676                                  Var->getType().getNonLValueExprType(Context),
4677                                  ForLoc, /*RefersToCapture=*/true));
4678         }
4679       }
4680     }
4681     DSAStack->setAssociatedLoops(AssociatedLoops - 1);
4682   }
4683 }
4684 
4685 /// Called on a for stmt to check and extract its iteration space
4686 /// for further processing (such as collapsing).
4687 static bool checkOpenMPIterationSpace(
4688     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4689     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
4690     unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4691     Expr *OrderedLoopCountExpr,
4692     Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
4693     LoopIterationSpace &ResultIterSpace,
4694     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
4695   // OpenMP [2.6, Canonical Loop Form]
4696   //   for (init-expr; test-expr; incr-expr) structured-block
4697   auto *For = dyn_cast_or_null<ForStmt>(S);
4698   if (!For) {
4699     SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
4700         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4701         << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
4702         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4703     if (TotalNestedLoopCount > 1) {
4704       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4705         SemaRef.Diag(DSA.getConstructLoc(),
4706                      diag::note_omp_collapse_ordered_expr)
4707             << 2 << CollapseLoopCountExpr->getSourceRange()
4708             << OrderedLoopCountExpr->getSourceRange();
4709       else if (CollapseLoopCountExpr)
4710         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4711                      diag::note_omp_collapse_ordered_expr)
4712             << 0 << CollapseLoopCountExpr->getSourceRange();
4713       else
4714         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4715                      diag::note_omp_collapse_ordered_expr)
4716             << 1 << OrderedLoopCountExpr->getSourceRange();
4717     }
4718     return true;
4719   }
4720   assert(For->getBody());
4721 
4722   OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4723 
4724   // Check init.
4725   Stmt *Init = For->getInit();
4726   if (ISC.checkAndSetInit(Init))
4727     return true;
4728 
4729   bool HasErrors = false;
4730 
4731   // Check loop variable's type.
4732   if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4733     Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
4734 
4735     // OpenMP [2.6, Canonical Loop Form]
4736     // Var is one of the following:
4737     //   A variable of signed or unsigned integer type.
4738     //   For C++, a variable of a random access iterator type.
4739     //   For C, a variable of a pointer type.
4740     QualType VarType = LCDecl->getType().getNonReferenceType();
4741     if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4742         !VarType->isPointerType() &&
4743         !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4744       SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
4745           << SemaRef.getLangOpts().CPlusPlus;
4746       HasErrors = true;
4747     }
4748 
4749     // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4750     // a Construct
4751     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4752     // parallel for construct is (are) private.
4753     // The loop iteration variable in the associated for-loop of a simd
4754     // construct with just one associated for-loop is linear with a
4755     // constant-linear-step that is the increment of the associated for-loop.
4756     // Exclude loop var from the list of variables with implicitly defined data
4757     // sharing attributes.
4758     VarsWithImplicitDSA.erase(LCDecl);
4759 
4760     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4761     // in a Construct, C/C++].
4762     // The loop iteration variable in the associated for-loop of a simd
4763     // construct with just one associated for-loop may be listed in a linear
4764     // clause with a constant-linear-step that is the increment of the
4765     // associated for-loop.
4766     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4767     // parallel for construct may be listed in a private or lastprivate clause.
4768     DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4769     // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4770     // declared in the loop and it is predetermined as a private.
4771     OpenMPClauseKind PredeterminedCKind =
4772         isOpenMPSimdDirective(DKind)
4773             ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4774             : OMPC_private;
4775     if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4776           DVar.CKind != PredeterminedCKind) ||
4777          ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4778            isOpenMPDistributeDirective(DKind)) &&
4779           !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4780           DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4781         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4782       SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
4783           << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4784           << getOpenMPClauseName(PredeterminedCKind);
4785       if (DVar.RefExpr == nullptr)
4786         DVar.CKind = PredeterminedCKind;
4787       reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4788       HasErrors = true;
4789     } else if (LoopDeclRefExpr != nullptr) {
4790       // Make the loop iteration variable private (for worksharing constructs),
4791       // linear (for simd directives with the only one associated loop) or
4792       // lastprivate (for simd directives with several collapsed or ordered
4793       // loops).
4794       if (DVar.CKind == OMPC_unknown)
4795         DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4796                           [](OpenMPDirectiveKind) -> bool { return true; },
4797                           /*FromParent=*/false);
4798       DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4799     }
4800 
4801     assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4802 
4803     // Check test-expr.
4804     HasErrors |= ISC.checkAndSetCond(For->getCond());
4805 
4806     // Check incr-expr.
4807     HasErrors |= ISC.checkAndSetInc(For->getInc());
4808   }
4809 
4810   if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
4811     return HasErrors;
4812 
4813   // Build the loop's iteration space representation.
4814   ResultIterSpace.PreCond =
4815       ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4816   ResultIterSpace.NumIterations = ISC.buildNumIterations(
4817       DSA.getCurScope(),
4818       (isOpenMPWorksharingDirective(DKind) ||
4819        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4820       Captures);
4821   ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
4822   ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
4823   ResultIterSpace.CounterInit = ISC.buildCounterInit();
4824   ResultIterSpace.CounterStep = ISC.buildCounterStep();
4825   ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
4826   ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
4827   ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
4828   ResultIterSpace.Subtract = ISC.shouldSubtractStep();
4829 
4830   HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4831                 ResultIterSpace.NumIterations == nullptr ||
4832                 ResultIterSpace.CounterVar == nullptr ||
4833                 ResultIterSpace.PrivateCounterVar == nullptr ||
4834                 ResultIterSpace.CounterInit == nullptr ||
4835                 ResultIterSpace.CounterStep == nullptr);
4836   if (!HasErrors && DSA.isOrderedRegion()) {
4837     if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
4838       if (CurrentNestedLoopCount <
4839           DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
4840         DSA.getOrderedRegionParam().second->setLoopNumIterations(
4841             CurrentNestedLoopCount, ResultIterSpace.NumIterations);
4842         DSA.getOrderedRegionParam().second->setLoopCounter(
4843             CurrentNestedLoopCount, ResultIterSpace.CounterVar);
4844       }
4845     }
4846     for (auto &Pair : DSA.getDoacrossDependClauses()) {
4847       if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
4848         // Erroneous case - clause has some problems.
4849         continue;
4850       }
4851       if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
4852           Pair.second.size() <= CurrentNestedLoopCount) {
4853         // Erroneous case - clause has some problems.
4854         Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
4855         continue;
4856       }
4857       Expr *CntValue;
4858       if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4859         CntValue = ISC.buildOrderedLoopData(
4860             DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4861             Pair.first->getDependencyLoc());
4862       else
4863         CntValue = ISC.buildOrderedLoopData(
4864             DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4865             Pair.first->getDependencyLoc(),
4866             Pair.second[CurrentNestedLoopCount].first,
4867             Pair.second[CurrentNestedLoopCount].second);
4868       Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
4869     }
4870   }
4871 
4872   return HasErrors;
4873 }
4874 
4875 /// Build 'VarRef = Start.
4876 static ExprResult
4877 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4878                  ExprResult Start,
4879                  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
4880   // Build 'VarRef = Start.
4881   ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4882   if (!NewStart.isUsable())
4883     return ExprError();
4884   if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4885                                    VarRef.get()->getType())) {
4886     NewStart = SemaRef.PerformImplicitConversion(
4887         NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4888         /*AllowExplicit=*/true);
4889     if (!NewStart.isUsable())
4890       return ExprError();
4891   }
4892 
4893   ExprResult Init =
4894       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4895   return Init;
4896 }
4897 
4898 /// Build 'VarRef = Start + Iter * Step'.
4899 static ExprResult buildCounterUpdate(
4900     Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4901     ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
4902     llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
4903   // Add parentheses (for debugging purposes only).
4904   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4905   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4906       !Step.isUsable())
4907     return ExprError();
4908 
4909   ExprResult NewStep = Step;
4910   if (Captures)
4911     NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
4912   if (NewStep.isInvalid())
4913     return ExprError();
4914   ExprResult Update =
4915       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
4916   if (!Update.isUsable())
4917     return ExprError();
4918 
4919   // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4920   // 'VarRef = Start (+|-) Iter * Step'.
4921   ExprResult NewStart = Start;
4922   if (Captures)
4923     NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
4924   if (NewStart.isInvalid())
4925     return ExprError();
4926 
4927   // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4928   ExprResult SavedUpdate = Update;
4929   ExprResult UpdateVal;
4930   if (VarRef.get()->getType()->isOverloadableType() ||
4931       NewStart.get()->getType()->isOverloadableType() ||
4932       Update.get()->getType()->isOverloadableType()) {
4933     bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4934     SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4935     Update =
4936         SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4937     if (Update.isUsable()) {
4938       UpdateVal =
4939           SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4940                              VarRef.get(), SavedUpdate.get());
4941       if (UpdateVal.isUsable()) {
4942         Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4943                                             UpdateVal.get());
4944       }
4945     }
4946     SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4947   }
4948 
4949   // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4950   if (!Update.isUsable() || !UpdateVal.isUsable()) {
4951     Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4952                                 NewStart.get(), SavedUpdate.get());
4953     if (!Update.isUsable())
4954       return ExprError();
4955 
4956     if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4957                                      VarRef.get()->getType())) {
4958       Update = SemaRef.PerformImplicitConversion(
4959           Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4960       if (!Update.isUsable())
4961         return ExprError();
4962     }
4963 
4964     Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4965   }
4966   return Update;
4967 }
4968 
4969 /// Convert integer expression \a E to make it have at least \a Bits
4970 /// bits.
4971 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
4972   if (E == nullptr)
4973     return ExprError();
4974   ASTContext &C = SemaRef.Context;
4975   QualType OldType = E->getType();
4976   unsigned HasBits = C.getTypeSize(OldType);
4977   if (HasBits >= Bits)
4978     return ExprResult(E);
4979   // OK to convert to signed, because new type has more bits than old.
4980   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4981   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4982                                            true);
4983 }
4984 
4985 /// Check if the given expression \a E is a constant integer that fits
4986 /// into \a Bits bits.
4987 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
4988   if (E == nullptr)
4989     return false;
4990   llvm::APSInt Result;
4991   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4992     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4993   return false;
4994 }
4995 
4996 /// Build preinits statement for the given declarations.
4997 static Stmt *buildPreInits(ASTContext &Context,
4998                            MutableArrayRef<Decl *> PreInits) {
4999   if (!PreInits.empty()) {
5000     return new (Context) DeclStmt(
5001         DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5002         SourceLocation(), SourceLocation());
5003   }
5004   return nullptr;
5005 }
5006 
5007 /// Build preinits statement for the given declarations.
5008 static Stmt *
5009 buildPreInits(ASTContext &Context,
5010               const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
5011   if (!Captures.empty()) {
5012     SmallVector<Decl *, 16> PreInits;
5013     for (const auto &Pair : Captures)
5014       PreInits.push_back(Pair.second->getDecl());
5015     return buildPreInits(Context, PreInits);
5016   }
5017   return nullptr;
5018 }
5019 
5020 /// Build postupdate expression for the given list of postupdates expressions.
5021 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5022   Expr *PostUpdate = nullptr;
5023   if (!PostUpdates.empty()) {
5024     for (Expr *E : PostUpdates) {
5025       Expr *ConvE = S.BuildCStyleCastExpr(
5026                          E->getExprLoc(),
5027                          S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5028                          E->getExprLoc(), E)
5029                         .get();
5030       PostUpdate = PostUpdate
5031                        ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5032                                               PostUpdate, ConvE)
5033                              .get()
5034                        : ConvE;
5035     }
5036   }
5037   return PostUpdate;
5038 }
5039 
5040 /// Called on a for stmt to check itself and nested loops (if any).
5041 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5042 /// number of collapsed loops otherwise.
5043 static unsigned
5044 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
5045                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5046                 DSAStackTy &DSA,
5047                 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
5048                 OMPLoopDirective::HelperExprs &Built) {
5049   unsigned NestedLoopCount = 1;
5050   if (CollapseLoopCountExpr) {
5051     // Found 'collapse' clause - calculate collapse number.
5052     Expr::EvalResult Result;
5053     if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
5054       NestedLoopCount = Result.Val.getInt().getLimitedValue();
5055   }
5056   unsigned OrderedLoopCount = 1;
5057   if (OrderedLoopCountExpr) {
5058     // Found 'ordered' clause - calculate collapse number.
5059     Expr::EvalResult EVResult;
5060     if (OrderedLoopCountExpr->EvaluateAsInt(EVResult, SemaRef.getASTContext())) {
5061       llvm::APSInt Result = EVResult.Val.getInt();
5062       if (Result.getLimitedValue() < NestedLoopCount) {
5063         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5064                      diag::err_omp_wrong_ordered_loop_count)
5065             << OrderedLoopCountExpr->getSourceRange();
5066         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5067                      diag::note_collapse_loop_count)
5068             << CollapseLoopCountExpr->getSourceRange();
5069       }
5070       OrderedLoopCount = Result.getLimitedValue();
5071     }
5072   }
5073   // This is helper routine for loop directives (e.g., 'for', 'simd',
5074   // 'for simd', etc.).
5075   llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
5076   SmallVector<LoopIterationSpace, 4> IterSpaces;
5077   IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount));
5078   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
5079   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
5080     if (checkOpenMPIterationSpace(
5081             DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5082             std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5083             OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5084             Captures))
5085       return 0;
5086     // Move on to the next nested for loop, or to the loop body.
5087     // OpenMP [2.8.1, simd construct, Restrictions]
5088     // All loops associated with the construct must be perfectly nested; that
5089     // is, there must be no intervening code nor any OpenMP directive between
5090     // any two loops.
5091     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5092   }
5093   for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
5094     if (checkOpenMPIterationSpace(
5095             DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5096             std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5097             OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5098             Captures))
5099       return 0;
5100     if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
5101       // Handle initialization of captured loop iterator variables.
5102       auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
5103       if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
5104         Captures[DRE] = DRE;
5105       }
5106     }
5107     // Move on to the next nested for loop, or to the loop body.
5108     // OpenMP [2.8.1, simd construct, Restrictions]
5109     // All loops associated with the construct must be perfectly nested; that
5110     // is, there must be no intervening code nor any OpenMP directive between
5111     // any two loops.
5112     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5113   }
5114 
5115   Built.clear(/* size */ NestedLoopCount);
5116 
5117   if (SemaRef.CurContext->isDependentContext())
5118     return NestedLoopCount;
5119 
5120   // An example of what is generated for the following code:
5121   //
5122   //   #pragma omp simd collapse(2) ordered(2)
5123   //   for (i = 0; i < NI; ++i)
5124   //     for (k = 0; k < NK; ++k)
5125   //       for (j = J0; j < NJ; j+=2) {
5126   //         <loop body>
5127   //       }
5128   //
5129   // We generate the code below.
5130   // Note: the loop body may be outlined in CodeGen.
5131   // Note: some counters may be C++ classes, operator- is used to find number of
5132   // iterations and operator+= to calculate counter value.
5133   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5134   // or i64 is currently supported).
5135   //
5136   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5137   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5138   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5139   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5140   //     // similar updates for vars in clauses (e.g. 'linear')
5141   //     <loop body (using local i and j)>
5142   //   }
5143   //   i = NI; // assign final values of counters
5144   //   j = NJ;
5145   //
5146 
5147   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5148   // the iteration counts of the collapsed for loops.
5149   // Precondition tests if there is at least one iteration (all conditions are
5150   // true).
5151   auto PreCond = ExprResult(IterSpaces[0].PreCond);
5152   Expr *N0 = IterSpaces[0].NumIterations;
5153   ExprResult LastIteration32 =
5154       widenIterationCount(/*Bits=*/32,
5155                           SemaRef
5156                               .PerformImplicitConversion(
5157                                   N0->IgnoreImpCasts(), N0->getType(),
5158                                   Sema::AA_Converting, /*AllowExplicit=*/true)
5159                               .get(),
5160                           SemaRef);
5161   ExprResult LastIteration64 = widenIterationCount(
5162       /*Bits=*/64,
5163       SemaRef
5164           .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
5165                                      Sema::AA_Converting,
5166                                      /*AllowExplicit=*/true)
5167           .get(),
5168       SemaRef);
5169 
5170   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5171     return NestedLoopCount;
5172 
5173   ASTContext &C = SemaRef.Context;
5174   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5175 
5176   Scope *CurScope = DSA.getCurScope();
5177   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
5178     if (PreCond.isUsable()) {
5179       PreCond =
5180           SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
5181                              PreCond.get(), IterSpaces[Cnt].PreCond);
5182     }
5183     Expr *N = IterSpaces[Cnt].NumIterations;
5184     SourceLocation Loc = N->getExprLoc();
5185     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5186     if (LastIteration32.isUsable())
5187       LastIteration32 = SemaRef.BuildBinOp(
5188           CurScope, Loc, BO_Mul, LastIteration32.get(),
5189           SemaRef
5190               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5191                                          Sema::AA_Converting,
5192                                          /*AllowExplicit=*/true)
5193               .get());
5194     if (LastIteration64.isUsable())
5195       LastIteration64 = SemaRef.BuildBinOp(
5196           CurScope, Loc, BO_Mul, LastIteration64.get(),
5197           SemaRef
5198               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5199                                          Sema::AA_Converting,
5200                                          /*AllowExplicit=*/true)
5201               .get());
5202   }
5203 
5204   // Choose either the 32-bit or 64-bit version.
5205   ExprResult LastIteration = LastIteration64;
5206   if (LastIteration32.isUsable() &&
5207       C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5208       (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5209        fitsInto(
5210            /*Bits=*/32,
5211            LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5212            LastIteration64.get(), SemaRef)))
5213     LastIteration = LastIteration32;
5214   QualType VType = LastIteration.get()->getType();
5215   QualType RealVType = VType;
5216   QualType StrideVType = VType;
5217   if (isOpenMPTaskLoopDirective(DKind)) {
5218     VType =
5219         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5220     StrideVType =
5221         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5222   }
5223 
5224   if (!LastIteration.isUsable())
5225     return 0;
5226 
5227   // Save the number of iterations.
5228   ExprResult NumIterations = LastIteration;
5229   {
5230     LastIteration = SemaRef.BuildBinOp(
5231         CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5232         LastIteration.get(),
5233         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5234     if (!LastIteration.isUsable())
5235       return 0;
5236   }
5237 
5238   // Calculate the last iteration number beforehand instead of doing this on
5239   // each iteration. Do not do this if the number of iterations may be kfold-ed.
5240   llvm::APSInt Result;
5241   bool IsConstant =
5242       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5243   ExprResult CalcLastIteration;
5244   if (!IsConstant) {
5245     ExprResult SaveRef =
5246         tryBuildCapture(SemaRef, LastIteration.get(), Captures);
5247     LastIteration = SaveRef;
5248 
5249     // Prepare SaveRef + 1.
5250     NumIterations = SemaRef.BuildBinOp(
5251         CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
5252         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5253     if (!NumIterations.isUsable())
5254       return 0;
5255   }
5256 
5257   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5258 
5259   // Build variables passed into runtime, necessary for worksharing directives.
5260   ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
5261   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5262       isOpenMPDistributeDirective(DKind)) {
5263     // Lower bound variable, initialized with zero.
5264     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5265     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
5266     SemaRef.AddInitializerToDecl(LBDecl,
5267                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5268                                  /*DirectInit*/ false);
5269 
5270     // Upper bound variable, initialized with last iteration number.
5271     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5272     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
5273     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5274                                  /*DirectInit*/ false);
5275 
5276     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5277     // This will be used to implement clause 'lastprivate'.
5278     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
5279     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5280     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
5281     SemaRef.AddInitializerToDecl(ILDecl,
5282                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5283                                  /*DirectInit*/ false);
5284 
5285     // Stride variable returned by runtime (we initialize it to 1 by default).
5286     VarDecl *STDecl =
5287         buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5288     ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
5289     SemaRef.AddInitializerToDecl(STDecl,
5290                                  SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5291                                  /*DirectInit*/ false);
5292 
5293     // Build expression: UB = min(UB, LastIteration)
5294     // It is necessary for CodeGen of directives with static scheduling.
5295     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5296                                                 UB.get(), LastIteration.get());
5297     ExprResult CondOp = SemaRef.ActOnConditionalOp(
5298         LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5299         LastIteration.get(), UB.get());
5300     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5301                              CondOp.get());
5302     EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
5303 
5304     // If we have a combined directive that combines 'distribute', 'for' or
5305     // 'simd' we need to be able to access the bounds of the schedule of the
5306     // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5307     // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5308     if (isOpenMPLoopBoundSharingDirective(DKind)) {
5309       // Lower bound variable, initialized with zero.
5310       VarDecl *CombLBDecl =
5311           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5312       CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5313       SemaRef.AddInitializerToDecl(
5314           CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5315           /*DirectInit*/ false);
5316 
5317       // Upper bound variable, initialized with last iteration number.
5318       VarDecl *CombUBDecl =
5319           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5320       CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5321       SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5322                                    /*DirectInit*/ false);
5323 
5324       ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5325           CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5326       ExprResult CombCondOp =
5327           SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5328                                      LastIteration.get(), CombUB.get());
5329       CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5330                                    CombCondOp.get());
5331       CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
5332 
5333       const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5334       // We expect to have at least 2 more parameters than the 'parallel'
5335       // directive does - the lower and upper bounds of the previous schedule.
5336       assert(CD->getNumParams() >= 4 &&
5337              "Unexpected number of parameters in loop combined directive");
5338 
5339       // Set the proper type for the bounds given what we learned from the
5340       // enclosed loops.
5341       ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5342       ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5343 
5344       // Previous lower and upper bounds are obtained from the region
5345       // parameters.
5346       PrevLB =
5347           buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5348       PrevUB =
5349           buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5350     }
5351   }
5352 
5353   // Build the iteration variable and its initialization before loop.
5354   ExprResult IV;
5355   ExprResult Init, CombInit;
5356   {
5357     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5358     IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
5359     Expr *RHS =
5360         (isOpenMPWorksharingDirective(DKind) ||
5361          isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5362             ? LB.get()
5363             : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5364     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5365     Init = SemaRef.ActOnFinishFullExpr(Init.get());
5366 
5367     if (isOpenMPLoopBoundSharingDirective(DKind)) {
5368       Expr *CombRHS =
5369           (isOpenMPWorksharingDirective(DKind) ||
5370            isOpenMPTaskLoopDirective(DKind) ||
5371            isOpenMPDistributeDirective(DKind))
5372               ? CombLB.get()
5373               : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5374       CombInit =
5375           SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
5376       CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
5377     }
5378   }
5379 
5380   // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
5381   SourceLocation CondLoc = AStmt->getBeginLoc();
5382   ExprResult Cond =
5383       (isOpenMPWorksharingDirective(DKind) ||
5384        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5385           ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5386           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5387                                NumIterations.get());
5388   ExprResult CombDistCond;
5389   if (isOpenMPLoopBoundSharingDirective(DKind)) {
5390     CombDistCond =
5391         SemaRef.BuildBinOp(
5392             CurScope, CondLoc, BO_LT, IV.get(), NumIterations.get());
5393   }
5394 
5395   ExprResult CombCond;
5396   if (isOpenMPLoopBoundSharingDirective(DKind)) {
5397     CombCond =
5398         SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
5399   }
5400   // Loop increment (IV = IV + 1)
5401   SourceLocation IncLoc = AStmt->getBeginLoc();
5402   ExprResult Inc =
5403       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5404                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5405   if (!Inc.isUsable())
5406     return 0;
5407   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
5408   Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5409   if (!Inc.isUsable())
5410     return 0;
5411 
5412   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5413   // Used for directives with static scheduling.
5414   // In combined construct, add combined version that use CombLB and CombUB
5415   // base variables for the update
5416   ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
5417   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5418       isOpenMPDistributeDirective(DKind)) {
5419     // LB + ST
5420     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5421     if (!NextLB.isUsable())
5422       return 0;
5423     // LB = LB + ST
5424     NextLB =
5425         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5426     NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5427     if (!NextLB.isUsable())
5428       return 0;
5429     // UB + ST
5430     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5431     if (!NextUB.isUsable())
5432       return 0;
5433     // UB = UB + ST
5434     NextUB =
5435         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5436     NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5437     if (!NextUB.isUsable())
5438       return 0;
5439     if (isOpenMPLoopBoundSharingDirective(DKind)) {
5440       CombNextLB =
5441           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5442       if (!NextLB.isUsable())
5443         return 0;
5444       // LB = LB + ST
5445       CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5446                                       CombNextLB.get());
5447       CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
5448       if (!CombNextLB.isUsable())
5449         return 0;
5450       // UB + ST
5451       CombNextUB =
5452           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5453       if (!CombNextUB.isUsable())
5454         return 0;
5455       // UB = UB + ST
5456       CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5457                                       CombNextUB.get());
5458       CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
5459       if (!CombNextUB.isUsable())
5460         return 0;
5461     }
5462   }
5463 
5464   // Create increment expression for distribute loop when combined in a same
5465   // directive with for as IV = IV + ST; ensure upper bound expression based
5466   // on PrevUB instead of NumIterations - used to implement 'for' when found
5467   // in combination with 'distribute', like in 'distribute parallel for'
5468   SourceLocation DistIncLoc = AStmt->getBeginLoc();
5469   ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
5470   if (isOpenMPLoopBoundSharingDirective(DKind)) {
5471     DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5472     assert(DistCond.isUsable() && "distribute cond expr was not built");
5473 
5474     DistInc =
5475         SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5476     assert(DistInc.isUsable() && "distribute inc expr was not built");
5477     DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5478                                  DistInc.get());
5479     DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
5480     assert(DistInc.isUsable() && "distribute inc expr was not built");
5481 
5482     // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5483     // construct
5484     SourceLocation DistEUBLoc = AStmt->getBeginLoc();
5485     ExprResult IsUBGreater =
5486         SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5487     ExprResult CondOp = SemaRef.ActOnConditionalOp(
5488         DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5489     PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5490                                  CondOp.get());
5491     PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
5492 
5493     // Build IV <= PrevUB to be used in parallel for is in combination with
5494     // a distribute directive with schedule(static, 1)
5495     ParForInDistCond =
5496         SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), PrevUB.get());
5497   }
5498 
5499   // Build updates and final values of the loop counters.
5500   bool HasErrors = false;
5501   Built.Counters.resize(NestedLoopCount);
5502   Built.Inits.resize(NestedLoopCount);
5503   Built.Updates.resize(NestedLoopCount);
5504   Built.Finals.resize(NestedLoopCount);
5505   {
5506     ExprResult Div;
5507     // Go from inner nested loop to outer.
5508     for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5509       LoopIterationSpace &IS = IterSpaces[Cnt];
5510       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5511       // Build: Iter = (IV / Div) % IS.NumIters
5512       // where Div is product of previous iterations' IS.NumIters.
5513       ExprResult Iter;
5514       if (Div.isUsable()) {
5515         Iter =
5516             SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5517       } else {
5518         Iter = IV;
5519         assert((Cnt == (int)NestedLoopCount - 1) &&
5520                "unusable div expected on first iteration only");
5521       }
5522 
5523       if (Cnt != 0 && Iter.isUsable())
5524         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5525                                   IS.NumIterations);
5526       if (!Iter.isUsable()) {
5527         HasErrors = true;
5528         break;
5529       }
5530 
5531       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
5532       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5533       DeclRefExpr *CounterVar = buildDeclRefExpr(
5534           SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5535           /*RefersToCapture=*/true);
5536       ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
5537                                          IS.CounterInit, Captures);
5538       if (!Init.isUsable()) {
5539         HasErrors = true;
5540         break;
5541       }
5542       ExprResult Update = buildCounterUpdate(
5543           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5544           IS.CounterStep, IS.Subtract, &Captures);
5545       if (!Update.isUsable()) {
5546         HasErrors = true;
5547         break;
5548       }
5549 
5550       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5551       ExprResult Final = buildCounterUpdate(
5552           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
5553           IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
5554       if (!Final.isUsable()) {
5555         HasErrors = true;
5556         break;
5557       }
5558 
5559       // Build Div for the next iteration: Div <- Div * IS.NumIters
5560       if (Cnt != 0) {
5561         if (Div.isUnset())
5562           Div = IS.NumIterations;
5563         else
5564           Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5565                                    IS.NumIterations);
5566 
5567         // Add parentheses (for debugging purposes only).
5568         if (Div.isUsable())
5569           Div = tryBuildCapture(SemaRef, Div.get(), Captures);
5570         if (!Div.isUsable()) {
5571           HasErrors = true;
5572           break;
5573         }
5574       }
5575       if (!Update.isUsable() || !Final.isUsable()) {
5576         HasErrors = true;
5577         break;
5578       }
5579       // Save results
5580       Built.Counters[Cnt] = IS.CounterVar;
5581       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
5582       Built.Inits[Cnt] = Init.get();
5583       Built.Updates[Cnt] = Update.get();
5584       Built.Finals[Cnt] = Final.get();
5585     }
5586   }
5587 
5588   if (HasErrors)
5589     return 0;
5590 
5591   // Save results
5592   Built.IterationVarRef = IV.get();
5593   Built.LastIteration = LastIteration.get();
5594   Built.NumIterations = NumIterations.get();
5595   Built.CalcLastIteration =
5596       SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
5597   Built.PreCond = PreCond.get();
5598   Built.PreInits = buildPreInits(C, Captures);
5599   Built.Cond = Cond.get();
5600   Built.Init = Init.get();
5601   Built.Inc = Inc.get();
5602   Built.LB = LB.get();
5603   Built.UB = UB.get();
5604   Built.IL = IL.get();
5605   Built.ST = ST.get();
5606   Built.EUB = EUB.get();
5607   Built.NLB = NextLB.get();
5608   Built.NUB = NextUB.get();
5609   Built.PrevLB = PrevLB.get();
5610   Built.PrevUB = PrevUB.get();
5611   Built.DistInc = DistInc.get();
5612   Built.PrevEUB = PrevEUB.get();
5613   Built.DistCombinedFields.LB = CombLB.get();
5614   Built.DistCombinedFields.UB = CombUB.get();
5615   Built.DistCombinedFields.EUB = CombEUB.get();
5616   Built.DistCombinedFields.Init = CombInit.get();
5617   Built.DistCombinedFields.Cond = CombCond.get();
5618   Built.DistCombinedFields.NLB = CombNextLB.get();
5619   Built.DistCombinedFields.NUB = CombNextUB.get();
5620   Built.DistCombinedFields.DistCond = CombDistCond.get();
5621   Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
5622 
5623   return NestedLoopCount;
5624 }
5625 
5626 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
5627   auto CollapseClauses =
5628       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5629   if (CollapseClauses.begin() != CollapseClauses.end())
5630     return (*CollapseClauses.begin())->getNumForLoops();
5631   return nullptr;
5632 }
5633 
5634 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
5635   auto OrderedClauses =
5636       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5637   if (OrderedClauses.begin() != OrderedClauses.end())
5638     return (*OrderedClauses.begin())->getNumForLoops();
5639   return nullptr;
5640 }
5641 
5642 static bool checkSimdlenSafelenSpecified(Sema &S,
5643                                          const ArrayRef<OMPClause *> Clauses) {
5644   const OMPSafelenClause *Safelen = nullptr;
5645   const OMPSimdlenClause *Simdlen = nullptr;
5646 
5647   for (const OMPClause *Clause : Clauses) {
5648     if (Clause->getClauseKind() == OMPC_safelen)
5649       Safelen = cast<OMPSafelenClause>(Clause);
5650     else if (Clause->getClauseKind() == OMPC_simdlen)
5651       Simdlen = cast<OMPSimdlenClause>(Clause);
5652     if (Safelen && Simdlen)
5653       break;
5654   }
5655 
5656   if (Simdlen && Safelen) {
5657     const Expr *SimdlenLength = Simdlen->getSimdlen();
5658     const Expr *SafelenLength = Safelen->getSafelen();
5659     if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5660         SimdlenLength->isInstantiationDependent() ||
5661         SimdlenLength->containsUnexpandedParameterPack())
5662       return false;
5663     if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5664         SafelenLength->isInstantiationDependent() ||
5665         SafelenLength->containsUnexpandedParameterPack())
5666       return false;
5667     Expr::EvalResult SimdlenResult, SafelenResult;
5668     SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
5669     SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
5670     llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
5671     llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
5672     // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5673     // If both simdlen and safelen clauses are specified, the value of the
5674     // simdlen parameter must be less than or equal to the value of the safelen
5675     // parameter.
5676     if (SimdlenRes > SafelenRes) {
5677       S.Diag(SimdlenLength->getExprLoc(),
5678              diag::err_omp_wrong_simdlen_safelen_values)
5679           << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5680       return true;
5681     }
5682   }
5683   return false;
5684 }
5685 
5686 StmtResult
5687 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5688                                SourceLocation StartLoc, SourceLocation EndLoc,
5689                                VarsWithInheritedDSAType &VarsWithImplicitDSA) {
5690   if (!AStmt)
5691     return StmtError();
5692 
5693   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5694   OMPLoopDirective::HelperExprs B;
5695   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5696   // define the nested loops number.
5697   unsigned NestedLoopCount = checkOpenMPLoop(
5698       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5699       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5700   if (NestedLoopCount == 0)
5701     return StmtError();
5702 
5703   assert((CurContext->isDependentContext() || B.builtAll()) &&
5704          "omp simd loop exprs were not built");
5705 
5706   if (!CurContext->isDependentContext()) {
5707     // Finalize the clauses that need pre-built expressions for CodeGen.
5708     for (OMPClause *C : Clauses) {
5709       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5710         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5711                                      B.NumIterations, *this, CurScope,
5712                                      DSAStack))
5713           return StmtError();
5714     }
5715   }
5716 
5717   if (checkSimdlenSafelenSpecified(*this, Clauses))
5718     return StmtError();
5719 
5720   setFunctionHasBranchProtectedScope();
5721   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5722                                   Clauses, AStmt, B);
5723 }
5724 
5725 StmtResult
5726 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5727                               SourceLocation StartLoc, SourceLocation EndLoc,
5728                               VarsWithInheritedDSAType &VarsWithImplicitDSA) {
5729   if (!AStmt)
5730     return StmtError();
5731 
5732   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5733   OMPLoopDirective::HelperExprs B;
5734   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5735   // define the nested loops number.
5736   unsigned NestedLoopCount = checkOpenMPLoop(
5737       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5738       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5739   if (NestedLoopCount == 0)
5740     return StmtError();
5741 
5742   assert((CurContext->isDependentContext() || B.builtAll()) &&
5743          "omp for loop exprs were not built");
5744 
5745   if (!CurContext->isDependentContext()) {
5746     // Finalize the clauses that need pre-built expressions for CodeGen.
5747     for (OMPClause *C : Clauses) {
5748       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5749         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5750                                      B.NumIterations, *this, CurScope,
5751                                      DSAStack))
5752           return StmtError();
5753     }
5754   }
5755 
5756   setFunctionHasBranchProtectedScope();
5757   return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5758                                  Clauses, AStmt, B, DSAStack->isCancelRegion());
5759 }
5760 
5761 StmtResult Sema::ActOnOpenMPForSimdDirective(
5762     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5763     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
5764   if (!AStmt)
5765     return StmtError();
5766 
5767   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5768   OMPLoopDirective::HelperExprs B;
5769   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5770   // define the nested loops number.
5771   unsigned NestedLoopCount =
5772       checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5773                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5774                       VarsWithImplicitDSA, B);
5775   if (NestedLoopCount == 0)
5776     return StmtError();
5777 
5778   assert((CurContext->isDependentContext() || B.builtAll()) &&
5779          "omp for simd loop exprs were not built");
5780 
5781   if (!CurContext->isDependentContext()) {
5782     // Finalize the clauses that need pre-built expressions for CodeGen.
5783     for (OMPClause *C : Clauses) {
5784       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5785         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5786                                      B.NumIterations, *this, CurScope,
5787                                      DSAStack))
5788           return StmtError();
5789     }
5790   }
5791 
5792   if (checkSimdlenSafelenSpecified(*this, Clauses))
5793     return StmtError();
5794 
5795   setFunctionHasBranchProtectedScope();
5796   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5797                                      Clauses, AStmt, B);
5798 }
5799 
5800 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5801                                               Stmt *AStmt,
5802                                               SourceLocation StartLoc,
5803                                               SourceLocation EndLoc) {
5804   if (!AStmt)
5805     return StmtError();
5806 
5807   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5808   auto BaseStmt = AStmt;
5809   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5810     BaseStmt = CS->getCapturedStmt();
5811   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5812     auto S = C->children();
5813     if (S.begin() == S.end())
5814       return StmtError();
5815     // All associated statements must be '#pragma omp section' except for
5816     // the first one.
5817     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5818       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5819         if (SectionStmt)
5820           Diag(SectionStmt->getBeginLoc(),
5821                diag::err_omp_sections_substmt_not_section);
5822         return StmtError();
5823       }
5824       cast<OMPSectionDirective>(SectionStmt)
5825           ->setHasCancel(DSAStack->isCancelRegion());
5826     }
5827   } else {
5828     Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
5829     return StmtError();
5830   }
5831 
5832   setFunctionHasBranchProtectedScope();
5833 
5834   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5835                                       DSAStack->isCancelRegion());
5836 }
5837 
5838 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5839                                              SourceLocation StartLoc,
5840                                              SourceLocation EndLoc) {
5841   if (!AStmt)
5842     return StmtError();
5843 
5844   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5845 
5846   setFunctionHasBranchProtectedScope();
5847   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
5848 
5849   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5850                                      DSAStack->isCancelRegion());
5851 }
5852 
5853 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5854                                             Stmt *AStmt,
5855                                             SourceLocation StartLoc,
5856                                             SourceLocation EndLoc) {
5857   if (!AStmt)
5858     return StmtError();
5859 
5860   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5861 
5862   setFunctionHasBranchProtectedScope();
5863 
5864   // OpenMP [2.7.3, single Construct, Restrictions]
5865   // The copyprivate clause must not be used with the nowait clause.
5866   const OMPClause *Nowait = nullptr;
5867   const OMPClause *Copyprivate = nullptr;
5868   for (const OMPClause *Clause : Clauses) {
5869     if (Clause->getClauseKind() == OMPC_nowait)
5870       Nowait = Clause;
5871     else if (Clause->getClauseKind() == OMPC_copyprivate)
5872       Copyprivate = Clause;
5873     if (Copyprivate && Nowait) {
5874       Diag(Copyprivate->getBeginLoc(),
5875            diag::err_omp_single_copyprivate_with_nowait);
5876       Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
5877       return StmtError();
5878     }
5879   }
5880 
5881   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5882 }
5883 
5884 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5885                                             SourceLocation StartLoc,
5886                                             SourceLocation EndLoc) {
5887   if (!AStmt)
5888     return StmtError();
5889 
5890   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5891 
5892   setFunctionHasBranchProtectedScope();
5893 
5894   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5895 }
5896 
5897 StmtResult Sema::ActOnOpenMPCriticalDirective(
5898     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5899     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
5900   if (!AStmt)
5901     return StmtError();
5902 
5903   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5904 
5905   bool ErrorFound = false;
5906   llvm::APSInt Hint;
5907   SourceLocation HintLoc;
5908   bool DependentHint = false;
5909   for (const OMPClause *C : Clauses) {
5910     if (C->getClauseKind() == OMPC_hint) {
5911       if (!DirName.getName()) {
5912         Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
5913         ErrorFound = true;
5914       }
5915       Expr *E = cast<OMPHintClause>(C)->getHint();
5916       if (E->isTypeDependent() || E->isValueDependent() ||
5917           E->isInstantiationDependent()) {
5918         DependentHint = true;
5919       } else {
5920         Hint = E->EvaluateKnownConstInt(Context);
5921         HintLoc = C->getBeginLoc();
5922       }
5923     }
5924   }
5925   if (ErrorFound)
5926     return StmtError();
5927   const auto Pair = DSAStack->getCriticalWithHint(DirName);
5928   if (Pair.first && DirName.getName() && !DependentHint) {
5929     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5930       Diag(StartLoc, diag::err_omp_critical_with_hint);
5931       if (HintLoc.isValid())
5932         Diag(HintLoc, diag::note_omp_critical_hint_here)
5933             << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5934       else
5935         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5936       if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5937         Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
5938             << 1
5939             << C->getHint()->EvaluateKnownConstInt(Context).toString(
5940                    /*Radix=*/10, /*Signed=*/false);
5941       } else {
5942         Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
5943       }
5944     }
5945   }
5946 
5947   setFunctionHasBranchProtectedScope();
5948 
5949   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5950                                            Clauses, AStmt);
5951   if (!Pair.first && DirName.getName() && !DependentHint)
5952     DSAStack->addCriticalWithHint(Dir, Hint);
5953   return Dir;
5954 }
5955 
5956 StmtResult Sema::ActOnOpenMPParallelForDirective(
5957     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5958     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
5959   if (!AStmt)
5960     return StmtError();
5961 
5962   auto *CS = cast<CapturedStmt>(AStmt);
5963   // 1.2.2 OpenMP Language Terminology
5964   // Structured block - An executable statement with a single entry at the
5965   // top and a single exit at the bottom.
5966   // The point of exit cannot be a branch out of the structured block.
5967   // longjmp() and throw() must not violate the entry/exit criteria.
5968   CS->getCapturedDecl()->setNothrow();
5969 
5970   OMPLoopDirective::HelperExprs B;
5971   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5972   // define the nested loops number.
5973   unsigned NestedLoopCount =
5974       checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5975                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5976                       VarsWithImplicitDSA, B);
5977   if (NestedLoopCount == 0)
5978     return StmtError();
5979 
5980   assert((CurContext->isDependentContext() || B.builtAll()) &&
5981          "omp parallel for loop exprs were not built");
5982 
5983   if (!CurContext->isDependentContext()) {
5984     // Finalize the clauses that need pre-built expressions for CodeGen.
5985     for (OMPClause *C : Clauses) {
5986       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5987         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5988                                      B.NumIterations, *this, CurScope,
5989                                      DSAStack))
5990           return StmtError();
5991     }
5992   }
5993 
5994   setFunctionHasBranchProtectedScope();
5995   return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
5996                                          NestedLoopCount, Clauses, AStmt, B,
5997                                          DSAStack->isCancelRegion());
5998 }
5999 
6000 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
6001     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6002     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
6003   if (!AStmt)
6004     return StmtError();
6005 
6006   auto *CS = cast<CapturedStmt>(AStmt);
6007   // 1.2.2 OpenMP Language Terminology
6008   // Structured block - An executable statement with a single entry at the
6009   // top and a single exit at the bottom.
6010   // The point of exit cannot be a branch out of the structured block.
6011   // longjmp() and throw() must not violate the entry/exit criteria.
6012   CS->getCapturedDecl()->setNothrow();
6013 
6014   OMPLoopDirective::HelperExprs B;
6015   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6016   // define the nested loops number.
6017   unsigned NestedLoopCount =
6018       checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
6019                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6020                       VarsWithImplicitDSA, B);
6021   if (NestedLoopCount == 0)
6022     return StmtError();
6023 
6024   if (!CurContext->isDependentContext()) {
6025     // Finalize the clauses that need pre-built expressions for CodeGen.
6026     for (OMPClause *C : Clauses) {
6027       if (auto *LC = dyn_cast<OMPLinearClause>(C))
6028         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6029                                      B.NumIterations, *this, CurScope,
6030                                      DSAStack))
6031           return StmtError();
6032     }
6033   }
6034 
6035   if (checkSimdlenSafelenSpecified(*this, Clauses))
6036     return StmtError();
6037 
6038   setFunctionHasBranchProtectedScope();
6039   return OMPParallelForSimdDirective::Create(
6040       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6041 }
6042 
6043 StmtResult
6044 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6045                                            Stmt *AStmt, SourceLocation StartLoc,
6046                                            SourceLocation EndLoc) {
6047   if (!AStmt)
6048     return StmtError();
6049 
6050   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6051   auto BaseStmt = AStmt;
6052   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
6053     BaseStmt = CS->getCapturedStmt();
6054   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
6055     auto S = C->children();
6056     if (S.begin() == S.end())
6057       return StmtError();
6058     // All associated statements must be '#pragma omp section' except for
6059     // the first one.
6060     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
6061       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6062         if (SectionStmt)
6063           Diag(SectionStmt->getBeginLoc(),
6064                diag::err_omp_parallel_sections_substmt_not_section);
6065         return StmtError();
6066       }
6067       cast<OMPSectionDirective>(SectionStmt)
6068           ->setHasCancel(DSAStack->isCancelRegion());
6069     }
6070   } else {
6071     Diag(AStmt->getBeginLoc(),
6072          diag::err_omp_parallel_sections_not_compound_stmt);
6073     return StmtError();
6074   }
6075 
6076   setFunctionHasBranchProtectedScope();
6077 
6078   return OMPParallelSectionsDirective::Create(
6079       Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
6080 }
6081 
6082 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6083                                           Stmt *AStmt, SourceLocation StartLoc,
6084                                           SourceLocation EndLoc) {
6085   if (!AStmt)
6086     return StmtError();
6087 
6088   auto *CS = cast<CapturedStmt>(AStmt);
6089   // 1.2.2 OpenMP Language Terminology
6090   // Structured block - An executable statement with a single entry at the
6091   // top and a single exit at the bottom.
6092   // The point of exit cannot be a branch out of the structured block.
6093   // longjmp() and throw() must not violate the entry/exit criteria.
6094   CS->getCapturedDecl()->setNothrow();
6095 
6096   setFunctionHasBranchProtectedScope();
6097 
6098   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6099                                   DSAStack->isCancelRegion());
6100 }
6101 
6102 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6103                                                SourceLocation EndLoc) {
6104   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6105 }
6106 
6107 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6108                                              SourceLocation EndLoc) {
6109   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6110 }
6111 
6112 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6113                                               SourceLocation EndLoc) {
6114   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6115 }
6116 
6117 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
6118                                                Stmt *AStmt,
6119                                                SourceLocation StartLoc,
6120                                                SourceLocation EndLoc) {
6121   if (!AStmt)
6122     return StmtError();
6123 
6124   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6125 
6126   setFunctionHasBranchProtectedScope();
6127 
6128   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
6129                                        AStmt,
6130                                        DSAStack->getTaskgroupReductionRef());
6131 }
6132 
6133 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6134                                            SourceLocation StartLoc,
6135                                            SourceLocation EndLoc) {
6136   assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6137   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6138 }
6139 
6140 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6141                                              Stmt *AStmt,
6142                                              SourceLocation StartLoc,
6143                                              SourceLocation EndLoc) {
6144   const OMPClause *DependFound = nullptr;
6145   const OMPClause *DependSourceClause = nullptr;
6146   const OMPClause *DependSinkClause = nullptr;
6147   bool ErrorFound = false;
6148   const OMPThreadsClause *TC = nullptr;
6149   const OMPSIMDClause *SC = nullptr;
6150   for (const OMPClause *C : Clauses) {
6151     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6152       DependFound = C;
6153       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6154         if (DependSourceClause) {
6155           Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
6156               << getOpenMPDirectiveName(OMPD_ordered)
6157               << getOpenMPClauseName(OMPC_depend) << 2;
6158           ErrorFound = true;
6159         } else {
6160           DependSourceClause = C;
6161         }
6162         if (DependSinkClause) {
6163           Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
6164               << 0;
6165           ErrorFound = true;
6166         }
6167       } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6168         if (DependSourceClause) {
6169           Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
6170               << 1;
6171           ErrorFound = true;
6172         }
6173         DependSinkClause = C;
6174       }
6175     } else if (C->getClauseKind() == OMPC_threads) {
6176       TC = cast<OMPThreadsClause>(C);
6177     } else if (C->getClauseKind() == OMPC_simd) {
6178       SC = cast<OMPSIMDClause>(C);
6179     }
6180   }
6181   if (!ErrorFound && !SC &&
6182       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
6183     // OpenMP [2.8.1,simd Construct, Restrictions]
6184     // An ordered construct with the simd clause is the only OpenMP construct
6185     // that can appear in the simd region.
6186     Diag(StartLoc, diag::err_omp_prohibited_region_simd);
6187     ErrorFound = true;
6188   } else if (DependFound && (TC || SC)) {
6189     Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
6190         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6191     ErrorFound = true;
6192   } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
6193     Diag(DependFound->getBeginLoc(),
6194          diag::err_omp_ordered_directive_without_param);
6195     ErrorFound = true;
6196   } else if (TC || Clauses.empty()) {
6197     if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
6198       SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
6199       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6200           << (TC != nullptr);
6201       Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
6202       ErrorFound = true;
6203     }
6204   }
6205   if ((!AStmt && !DependFound) || ErrorFound)
6206     return StmtError();
6207 
6208   if (AStmt) {
6209     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6210 
6211     setFunctionHasBranchProtectedScope();
6212   }
6213 
6214   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6215 }
6216 
6217 namespace {
6218 /// Helper class for checking expression in 'omp atomic [update]'
6219 /// construct.
6220 class OpenMPAtomicUpdateChecker {
6221   /// Error results for atomic update expressions.
6222   enum ExprAnalysisErrorCode {
6223     /// A statement is not an expression statement.
6224     NotAnExpression,
6225     /// Expression is not builtin binary or unary operation.
6226     NotABinaryOrUnaryExpression,
6227     /// Unary operation is not post-/pre- increment/decrement operation.
6228     NotAnUnaryIncDecExpression,
6229     /// An expression is not of scalar type.
6230     NotAScalarType,
6231     /// A binary operation is not an assignment operation.
6232     NotAnAssignmentOp,
6233     /// RHS part of the binary operation is not a binary expression.
6234     NotABinaryExpression,
6235     /// RHS part is not additive/multiplicative/shift/biwise binary
6236     /// expression.
6237     NotABinaryOperator,
6238     /// RHS binary operation does not have reference to the updated LHS
6239     /// part.
6240     NotAnUpdateExpression,
6241     /// No errors is found.
6242     NoError
6243   };
6244   /// Reference to Sema.
6245   Sema &SemaRef;
6246   /// A location for note diagnostics (when error is found).
6247   SourceLocation NoteLoc;
6248   /// 'x' lvalue part of the source atomic expression.
6249   Expr *X;
6250   /// 'expr' rvalue part of the source atomic expression.
6251   Expr *E;
6252   /// Helper expression of the form
6253   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6254   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6255   Expr *UpdateExpr;
6256   /// Is 'x' a LHS in a RHS part of full update expression. It is
6257   /// important for non-associative operations.
6258   bool IsXLHSInRHSPart;
6259   BinaryOperatorKind Op;
6260   SourceLocation OpLoc;
6261   /// true if the source expression is a postfix unary operation, false
6262   /// if it is a prefix unary operation.
6263   bool IsPostfixUpdate;
6264 
6265 public:
6266   OpenMPAtomicUpdateChecker(Sema &SemaRef)
6267       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
6268         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
6269   /// Check specified statement that it is suitable for 'atomic update'
6270   /// constructs and extract 'x', 'expr' and Operation from the original
6271   /// expression. If DiagId and NoteId == 0, then only check is performed
6272   /// without error notification.
6273   /// \param DiagId Diagnostic which should be emitted if error is found.
6274   /// \param NoteId Diagnostic note for the main error message.
6275   /// \return true if statement is not an update expression, false otherwise.
6276   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
6277   /// Return the 'x' lvalue part of the source atomic expression.
6278   Expr *getX() const { return X; }
6279   /// Return the 'expr' rvalue part of the source atomic expression.
6280   Expr *getExpr() const { return E; }
6281   /// Return the update expression used in calculation of the updated
6282   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6283   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6284   Expr *getUpdateExpr() const { return UpdateExpr; }
6285   /// Return true if 'x' is LHS in RHS part of full update expression,
6286   /// false otherwise.
6287   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6288 
6289   /// true if the source expression is a postfix unary operation, false
6290   /// if it is a prefix unary operation.
6291   bool isPostfixUpdate() const { return IsPostfixUpdate; }
6292 
6293 private:
6294   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6295                             unsigned NoteId = 0);
6296 };
6297 } // namespace
6298 
6299 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6300     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6301   ExprAnalysisErrorCode ErrorFound = NoError;
6302   SourceLocation ErrorLoc, NoteLoc;
6303   SourceRange ErrorRange, NoteRange;
6304   // Allowed constructs are:
6305   //  x = x binop expr;
6306   //  x = expr binop x;
6307   if (AtomicBinOp->getOpcode() == BO_Assign) {
6308     X = AtomicBinOp->getLHS();
6309     if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6310             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6311       if (AtomicInnerBinOp->isMultiplicativeOp() ||
6312           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6313           AtomicInnerBinOp->isBitwiseOp()) {
6314         Op = AtomicInnerBinOp->getOpcode();
6315         OpLoc = AtomicInnerBinOp->getOperatorLoc();
6316         Expr *LHS = AtomicInnerBinOp->getLHS();
6317         Expr *RHS = AtomicInnerBinOp->getRHS();
6318         llvm::FoldingSetNodeID XId, LHSId, RHSId;
6319         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6320                                           /*Canonical=*/true);
6321         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6322                                             /*Canonical=*/true);
6323         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6324                                             /*Canonical=*/true);
6325         if (XId == LHSId) {
6326           E = RHS;
6327           IsXLHSInRHSPart = true;
6328         } else if (XId == RHSId) {
6329           E = LHS;
6330           IsXLHSInRHSPart = false;
6331         } else {
6332           ErrorLoc = AtomicInnerBinOp->getExprLoc();
6333           ErrorRange = AtomicInnerBinOp->getSourceRange();
6334           NoteLoc = X->getExprLoc();
6335           NoteRange = X->getSourceRange();
6336           ErrorFound = NotAnUpdateExpression;
6337         }
6338       } else {
6339         ErrorLoc = AtomicInnerBinOp->getExprLoc();
6340         ErrorRange = AtomicInnerBinOp->getSourceRange();
6341         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6342         NoteRange = SourceRange(NoteLoc, NoteLoc);
6343         ErrorFound = NotABinaryOperator;
6344       }
6345     } else {
6346       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6347       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6348       ErrorFound = NotABinaryExpression;
6349     }
6350   } else {
6351     ErrorLoc = AtomicBinOp->getExprLoc();
6352     ErrorRange = AtomicBinOp->getSourceRange();
6353     NoteLoc = AtomicBinOp->getOperatorLoc();
6354     NoteRange = SourceRange(NoteLoc, NoteLoc);
6355     ErrorFound = NotAnAssignmentOp;
6356   }
6357   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
6358     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6359     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6360     return true;
6361   }
6362   if (SemaRef.CurContext->isDependentContext())
6363     E = X = UpdateExpr = nullptr;
6364   return ErrorFound != NoError;
6365 }
6366 
6367 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6368                                                unsigned NoteId) {
6369   ExprAnalysisErrorCode ErrorFound = NoError;
6370   SourceLocation ErrorLoc, NoteLoc;
6371   SourceRange ErrorRange, NoteRange;
6372   // Allowed constructs are:
6373   //  x++;
6374   //  x--;
6375   //  ++x;
6376   //  --x;
6377   //  x binop= expr;
6378   //  x = x binop expr;
6379   //  x = expr binop x;
6380   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6381     AtomicBody = AtomicBody->IgnoreParenImpCasts();
6382     if (AtomicBody->getType()->isScalarType() ||
6383         AtomicBody->isInstantiationDependent()) {
6384       if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6385               AtomicBody->IgnoreParenImpCasts())) {
6386         // Check for Compound Assignment Operation
6387         Op = BinaryOperator::getOpForCompoundAssignment(
6388             AtomicCompAssignOp->getOpcode());
6389         OpLoc = AtomicCompAssignOp->getOperatorLoc();
6390         E = AtomicCompAssignOp->getRHS();
6391         X = AtomicCompAssignOp->getLHS()->IgnoreParens();
6392         IsXLHSInRHSPart = true;
6393       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6394                      AtomicBody->IgnoreParenImpCasts())) {
6395         // Check for Binary Operation
6396         if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6397           return true;
6398       } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
6399                      AtomicBody->IgnoreParenImpCasts())) {
6400         // Check for Unary Operation
6401         if (AtomicUnaryOp->isIncrementDecrementOp()) {
6402           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
6403           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6404           OpLoc = AtomicUnaryOp->getOperatorLoc();
6405           X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
6406           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6407           IsXLHSInRHSPart = true;
6408         } else {
6409           ErrorFound = NotAnUnaryIncDecExpression;
6410           ErrorLoc = AtomicUnaryOp->getExprLoc();
6411           ErrorRange = AtomicUnaryOp->getSourceRange();
6412           NoteLoc = AtomicUnaryOp->getOperatorLoc();
6413           NoteRange = SourceRange(NoteLoc, NoteLoc);
6414         }
6415       } else if (!AtomicBody->isInstantiationDependent()) {
6416         ErrorFound = NotABinaryOrUnaryExpression;
6417         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6418         NoteRange = ErrorRange = AtomicBody->getSourceRange();
6419       }
6420     } else {
6421       ErrorFound = NotAScalarType;
6422       NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
6423       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6424     }
6425   } else {
6426     ErrorFound = NotAnExpression;
6427     NoteLoc = ErrorLoc = S->getBeginLoc();
6428     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6429   }
6430   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
6431     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6432     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6433     return true;
6434   }
6435   if (SemaRef.CurContext->isDependentContext())
6436     E = X = UpdateExpr = nullptr;
6437   if (ErrorFound == NoError && E && X) {
6438     // Build an update expression of form 'OpaqueValueExpr(x) binop
6439     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6440     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6441     auto *OVEX = new (SemaRef.getASTContext())
6442         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6443     auto *OVEExpr = new (SemaRef.getASTContext())
6444         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6445     ExprResult Update =
6446         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6447                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
6448     if (Update.isInvalid())
6449       return true;
6450     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6451                                                Sema::AA_Casting);
6452     if (Update.isInvalid())
6453       return true;
6454     UpdateExpr = Update.get();
6455   }
6456   return ErrorFound != NoError;
6457 }
6458 
6459 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6460                                             Stmt *AStmt,
6461                                             SourceLocation StartLoc,
6462                                             SourceLocation EndLoc) {
6463   if (!AStmt)
6464     return StmtError();
6465 
6466   auto *CS = cast<CapturedStmt>(AStmt);
6467   // 1.2.2 OpenMP Language Terminology
6468   // Structured block - An executable statement with a single entry at the
6469   // top and a single exit at the bottom.
6470   // The point of exit cannot be a branch out of the structured block.
6471   // longjmp() and throw() must not violate the entry/exit criteria.
6472   OpenMPClauseKind AtomicKind = OMPC_unknown;
6473   SourceLocation AtomicKindLoc;
6474   for (const OMPClause *C : Clauses) {
6475     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
6476         C->getClauseKind() == OMPC_update ||
6477         C->getClauseKind() == OMPC_capture) {
6478       if (AtomicKind != OMPC_unknown) {
6479         Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
6480             << SourceRange(C->getBeginLoc(), C->getEndLoc());
6481         Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6482             << getOpenMPClauseName(AtomicKind);
6483       } else {
6484         AtomicKind = C->getClauseKind();
6485         AtomicKindLoc = C->getBeginLoc();
6486       }
6487     }
6488   }
6489 
6490   Stmt *Body = CS->getCapturedStmt();
6491   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6492     Body = EWC->getSubExpr();
6493 
6494   Expr *X = nullptr;
6495   Expr *V = nullptr;
6496   Expr *E = nullptr;
6497   Expr *UE = nullptr;
6498   bool IsXLHSInRHSPart = false;
6499   bool IsPostfixUpdate = false;
6500   // OpenMP [2.12.6, atomic Construct]
6501   // In the next expressions:
6502   // * x and v (as applicable) are both l-value expressions with scalar type.
6503   // * During the execution of an atomic region, multiple syntactic
6504   // occurrences of x must designate the same storage location.
6505   // * Neither of v and expr (as applicable) may access the storage location
6506   // designated by x.
6507   // * Neither of x and expr (as applicable) may access the storage location
6508   // designated by v.
6509   // * expr is an expression with scalar type.
6510   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6511   // * binop, binop=, ++, and -- are not overloaded operators.
6512   // * The expression x binop expr must be numerically equivalent to x binop
6513   // (expr). This requirement is satisfied if the operators in expr have
6514   // precedence greater than binop, or by using parentheses around expr or
6515   // subexpressions of expr.
6516   // * The expression expr binop x must be numerically equivalent to (expr)
6517   // binop x. This requirement is satisfied if the operators in expr have
6518   // precedence equal to or greater than binop, or by using parentheses around
6519   // expr or subexpressions of expr.
6520   // * For forms that allow multiple occurrences of x, the number of times
6521   // that x is evaluated is unspecified.
6522   if (AtomicKind == OMPC_read) {
6523     enum {
6524       NotAnExpression,
6525       NotAnAssignmentOp,
6526       NotAScalarType,
6527       NotAnLValue,
6528       NoError
6529     } ErrorFound = NoError;
6530     SourceLocation ErrorLoc, NoteLoc;
6531     SourceRange ErrorRange, NoteRange;
6532     // If clause is read:
6533     //  v = x;
6534     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6535       const auto *AtomicBinOp =
6536           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6537       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6538         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6539         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6540         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6541             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6542           if (!X->isLValue() || !V->isLValue()) {
6543             const Expr *NotLValueExpr = X->isLValue() ? V : X;
6544             ErrorFound = NotAnLValue;
6545             ErrorLoc = AtomicBinOp->getExprLoc();
6546             ErrorRange = AtomicBinOp->getSourceRange();
6547             NoteLoc = NotLValueExpr->getExprLoc();
6548             NoteRange = NotLValueExpr->getSourceRange();
6549           }
6550         } else if (!X->isInstantiationDependent() ||
6551                    !V->isInstantiationDependent()) {
6552           const Expr *NotScalarExpr =
6553               (X->isInstantiationDependent() || X->getType()->isScalarType())
6554                   ? V
6555                   : X;
6556           ErrorFound = NotAScalarType;
6557           ErrorLoc = AtomicBinOp->getExprLoc();
6558           ErrorRange = AtomicBinOp->getSourceRange();
6559           NoteLoc = NotScalarExpr->getExprLoc();
6560           NoteRange = NotScalarExpr->getSourceRange();
6561         }
6562       } else if (!AtomicBody->isInstantiationDependent()) {
6563         ErrorFound = NotAnAssignmentOp;
6564         ErrorLoc = AtomicBody->getExprLoc();
6565         ErrorRange = AtomicBody->getSourceRange();
6566         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6567                               : AtomicBody->getExprLoc();
6568         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6569                                 : AtomicBody->getSourceRange();
6570       }
6571     } else {
6572       ErrorFound = NotAnExpression;
6573       NoteLoc = ErrorLoc = Body->getBeginLoc();
6574       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6575     }
6576     if (ErrorFound != NoError) {
6577       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6578           << ErrorRange;
6579       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6580                                                       << NoteRange;
6581       return StmtError();
6582     }
6583     if (CurContext->isDependentContext())
6584       V = X = nullptr;
6585   } else if (AtomicKind == OMPC_write) {
6586     enum {
6587       NotAnExpression,
6588       NotAnAssignmentOp,
6589       NotAScalarType,
6590       NotAnLValue,
6591       NoError
6592     } ErrorFound = NoError;
6593     SourceLocation ErrorLoc, NoteLoc;
6594     SourceRange ErrorRange, NoteRange;
6595     // If clause is write:
6596     //  x = expr;
6597     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6598       const auto *AtomicBinOp =
6599           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6600       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6601         X = AtomicBinOp->getLHS();
6602         E = AtomicBinOp->getRHS();
6603         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6604             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6605           if (!X->isLValue()) {
6606             ErrorFound = NotAnLValue;
6607             ErrorLoc = AtomicBinOp->getExprLoc();
6608             ErrorRange = AtomicBinOp->getSourceRange();
6609             NoteLoc = X->getExprLoc();
6610             NoteRange = X->getSourceRange();
6611           }
6612         } else if (!X->isInstantiationDependent() ||
6613                    !E->isInstantiationDependent()) {
6614           const Expr *NotScalarExpr =
6615               (X->isInstantiationDependent() || X->getType()->isScalarType())
6616                   ? E
6617                   : X;
6618           ErrorFound = NotAScalarType;
6619           ErrorLoc = AtomicBinOp->getExprLoc();
6620           ErrorRange = AtomicBinOp->getSourceRange();
6621           NoteLoc = NotScalarExpr->getExprLoc();
6622           NoteRange = NotScalarExpr->getSourceRange();
6623         }
6624       } else if (!AtomicBody->isInstantiationDependent()) {
6625         ErrorFound = NotAnAssignmentOp;
6626         ErrorLoc = AtomicBody->getExprLoc();
6627         ErrorRange = AtomicBody->getSourceRange();
6628         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6629                               : AtomicBody->getExprLoc();
6630         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6631                                 : AtomicBody->getSourceRange();
6632       }
6633     } else {
6634       ErrorFound = NotAnExpression;
6635       NoteLoc = ErrorLoc = Body->getBeginLoc();
6636       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6637     }
6638     if (ErrorFound != NoError) {
6639       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6640           << ErrorRange;
6641       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6642                                                       << NoteRange;
6643       return StmtError();
6644     }
6645     if (CurContext->isDependentContext())
6646       E = X = nullptr;
6647   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
6648     // If clause is update:
6649     //  x++;
6650     //  x--;
6651     //  ++x;
6652     //  --x;
6653     //  x binop= expr;
6654     //  x = x binop expr;
6655     //  x = expr binop x;
6656     OpenMPAtomicUpdateChecker Checker(*this);
6657     if (Checker.checkStatement(
6658             Body, (AtomicKind == OMPC_update)
6659                       ? diag::err_omp_atomic_update_not_expression_statement
6660                       : diag::err_omp_atomic_not_expression_statement,
6661             diag::note_omp_atomic_update))
6662       return StmtError();
6663     if (!CurContext->isDependentContext()) {
6664       E = Checker.getExpr();
6665       X = Checker.getX();
6666       UE = Checker.getUpdateExpr();
6667       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6668     }
6669   } else if (AtomicKind == OMPC_capture) {
6670     enum {
6671       NotAnAssignmentOp,
6672       NotACompoundStatement,
6673       NotTwoSubstatements,
6674       NotASpecificExpression,
6675       NoError
6676     } ErrorFound = NoError;
6677     SourceLocation ErrorLoc, NoteLoc;
6678     SourceRange ErrorRange, NoteRange;
6679     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6680       // If clause is a capture:
6681       //  v = x++;
6682       //  v = x--;
6683       //  v = ++x;
6684       //  v = --x;
6685       //  v = x binop= expr;
6686       //  v = x = x binop expr;
6687       //  v = x = expr binop x;
6688       const auto *AtomicBinOp =
6689           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6690       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6691         V = AtomicBinOp->getLHS();
6692         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6693         OpenMPAtomicUpdateChecker Checker(*this);
6694         if (Checker.checkStatement(
6695                 Body, diag::err_omp_atomic_capture_not_expression_statement,
6696                 diag::note_omp_atomic_update))
6697           return StmtError();
6698         E = Checker.getExpr();
6699         X = Checker.getX();
6700         UE = Checker.getUpdateExpr();
6701         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6702         IsPostfixUpdate = Checker.isPostfixUpdate();
6703       } else if (!AtomicBody->isInstantiationDependent()) {
6704         ErrorLoc = AtomicBody->getExprLoc();
6705         ErrorRange = AtomicBody->getSourceRange();
6706         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6707                               : AtomicBody->getExprLoc();
6708         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6709                                 : AtomicBody->getSourceRange();
6710         ErrorFound = NotAnAssignmentOp;
6711       }
6712       if (ErrorFound != NoError) {
6713         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6714             << ErrorRange;
6715         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6716         return StmtError();
6717       }
6718       if (CurContext->isDependentContext())
6719         UE = V = E = X = nullptr;
6720     } else {
6721       // If clause is a capture:
6722       //  { v = x; x = expr; }
6723       //  { v = x; x++; }
6724       //  { v = x; x--; }
6725       //  { v = x; ++x; }
6726       //  { v = x; --x; }
6727       //  { v = x; x binop= expr; }
6728       //  { v = x; x = x binop expr; }
6729       //  { v = x; x = expr binop x; }
6730       //  { x++; v = x; }
6731       //  { x--; v = x; }
6732       //  { ++x; v = x; }
6733       //  { --x; v = x; }
6734       //  { x binop= expr; v = x; }
6735       //  { x = x binop expr; v = x; }
6736       //  { x = expr binop x; v = x; }
6737       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6738         // Check that this is { expr1; expr2; }
6739         if (CS->size() == 2) {
6740           Stmt *First = CS->body_front();
6741           Stmt *Second = CS->body_back();
6742           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6743             First = EWC->getSubExpr()->IgnoreParenImpCasts();
6744           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6745             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6746           // Need to find what subexpression is 'v' and what is 'x'.
6747           OpenMPAtomicUpdateChecker Checker(*this);
6748           bool IsUpdateExprFound = !Checker.checkStatement(Second);
6749           BinaryOperator *BinOp = nullptr;
6750           if (IsUpdateExprFound) {
6751             BinOp = dyn_cast<BinaryOperator>(First);
6752             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6753           }
6754           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6755             //  { v = x; x++; }
6756             //  { v = x; x--; }
6757             //  { v = x; ++x; }
6758             //  { v = x; --x; }
6759             //  { v = x; x binop= expr; }
6760             //  { v = x; x = x binop expr; }
6761             //  { v = x; x = expr binop x; }
6762             // Check that the first expression has form v = x.
6763             Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6764             llvm::FoldingSetNodeID XId, PossibleXId;
6765             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6766             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6767             IsUpdateExprFound = XId == PossibleXId;
6768             if (IsUpdateExprFound) {
6769               V = BinOp->getLHS();
6770               X = Checker.getX();
6771               E = Checker.getExpr();
6772               UE = Checker.getUpdateExpr();
6773               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6774               IsPostfixUpdate = true;
6775             }
6776           }
6777           if (!IsUpdateExprFound) {
6778             IsUpdateExprFound = !Checker.checkStatement(First);
6779             BinOp = nullptr;
6780             if (IsUpdateExprFound) {
6781               BinOp = dyn_cast<BinaryOperator>(Second);
6782               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6783             }
6784             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6785               //  { x++; v = x; }
6786               //  { x--; v = x; }
6787               //  { ++x; v = x; }
6788               //  { --x; v = x; }
6789               //  { x binop= expr; v = x; }
6790               //  { x = x binop expr; v = x; }
6791               //  { x = expr binop x; v = x; }
6792               // Check that the second expression has form v = x.
6793               Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6794               llvm::FoldingSetNodeID XId, PossibleXId;
6795               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6796               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6797               IsUpdateExprFound = XId == PossibleXId;
6798               if (IsUpdateExprFound) {
6799                 V = BinOp->getLHS();
6800                 X = Checker.getX();
6801                 E = Checker.getExpr();
6802                 UE = Checker.getUpdateExpr();
6803                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6804                 IsPostfixUpdate = false;
6805               }
6806             }
6807           }
6808           if (!IsUpdateExprFound) {
6809             //  { v = x; x = expr; }
6810             auto *FirstExpr = dyn_cast<Expr>(First);
6811             auto *SecondExpr = dyn_cast<Expr>(Second);
6812             if (!FirstExpr || !SecondExpr ||
6813                 !(FirstExpr->isInstantiationDependent() ||
6814                   SecondExpr->isInstantiationDependent())) {
6815               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6816               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
6817                 ErrorFound = NotAnAssignmentOp;
6818                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6819                                                 : First->getBeginLoc();
6820                 NoteRange = ErrorRange = FirstBinOp
6821                                              ? FirstBinOp->getSourceRange()
6822                                              : SourceRange(ErrorLoc, ErrorLoc);
6823               } else {
6824                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6825                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6826                   ErrorFound = NotAnAssignmentOp;
6827                   NoteLoc = ErrorLoc = SecondBinOp
6828                                            ? SecondBinOp->getOperatorLoc()
6829                                            : Second->getBeginLoc();
6830                   NoteRange = ErrorRange =
6831                       SecondBinOp ? SecondBinOp->getSourceRange()
6832                                   : SourceRange(ErrorLoc, ErrorLoc);
6833                 } else {
6834                   Expr *PossibleXRHSInFirst =
6835                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
6836                   Expr *PossibleXLHSInSecond =
6837                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
6838                   llvm::FoldingSetNodeID X1Id, X2Id;
6839                   PossibleXRHSInFirst->Profile(X1Id, Context,
6840                                                /*Canonical=*/true);
6841                   PossibleXLHSInSecond->Profile(X2Id, Context,
6842                                                 /*Canonical=*/true);
6843                   IsUpdateExprFound = X1Id == X2Id;
6844                   if (IsUpdateExprFound) {
6845                     V = FirstBinOp->getLHS();
6846                     X = SecondBinOp->getLHS();
6847                     E = SecondBinOp->getRHS();
6848                     UE = nullptr;
6849                     IsXLHSInRHSPart = false;
6850                     IsPostfixUpdate = true;
6851                   } else {
6852                     ErrorFound = NotASpecificExpression;
6853                     ErrorLoc = FirstBinOp->getExprLoc();
6854                     ErrorRange = FirstBinOp->getSourceRange();
6855                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6856                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
6857                   }
6858                 }
6859               }
6860             }
6861           }
6862         } else {
6863           NoteLoc = ErrorLoc = Body->getBeginLoc();
6864           NoteRange = ErrorRange =
6865               SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
6866           ErrorFound = NotTwoSubstatements;
6867         }
6868       } else {
6869         NoteLoc = ErrorLoc = Body->getBeginLoc();
6870         NoteRange = ErrorRange =
6871             SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
6872         ErrorFound = NotACompoundStatement;
6873       }
6874       if (ErrorFound != NoError) {
6875         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6876             << ErrorRange;
6877         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6878         return StmtError();
6879       }
6880       if (CurContext->isDependentContext())
6881         UE = V = E = X = nullptr;
6882     }
6883   }
6884 
6885   setFunctionHasBranchProtectedScope();
6886 
6887   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6888                                     X, V, E, UE, IsXLHSInRHSPart,
6889                                     IsPostfixUpdate);
6890 }
6891 
6892 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6893                                             Stmt *AStmt,
6894                                             SourceLocation StartLoc,
6895                                             SourceLocation EndLoc) {
6896   if (!AStmt)
6897     return StmtError();
6898 
6899   auto *CS = cast<CapturedStmt>(AStmt);
6900   // 1.2.2 OpenMP Language Terminology
6901   // Structured block - An executable statement with a single entry at the
6902   // top and a single exit at the bottom.
6903   // The point of exit cannot be a branch out of the structured block.
6904   // longjmp() and throw() must not violate the entry/exit criteria.
6905   CS->getCapturedDecl()->setNothrow();
6906   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6907        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6908     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6909     // 1.2.2 OpenMP Language Terminology
6910     // Structured block - An executable statement with a single entry at the
6911     // top and a single exit at the bottom.
6912     // The point of exit cannot be a branch out of the structured block.
6913     // longjmp() and throw() must not violate the entry/exit criteria.
6914     CS->getCapturedDecl()->setNothrow();
6915   }
6916 
6917   // OpenMP [2.16, Nesting of Regions]
6918   // If specified, a teams construct must be contained within a target
6919   // construct. That target construct must contain no statements or directives
6920   // outside of the teams construct.
6921   if (DSAStack->hasInnerTeamsRegion()) {
6922     const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
6923     bool OMPTeamsFound = true;
6924     if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
6925       auto I = CS->body_begin();
6926       while (I != CS->body_end()) {
6927         const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
6928         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6929           OMPTeamsFound = false;
6930           break;
6931         }
6932         ++I;
6933       }
6934       assert(I != CS->body_end() && "Not found statement");
6935       S = *I;
6936     } else {
6937       const auto *OED = dyn_cast<OMPExecutableDirective>(S);
6938       OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
6939     }
6940     if (!OMPTeamsFound) {
6941       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6942       Diag(DSAStack->getInnerTeamsRegionLoc(),
6943            diag::note_omp_nested_teams_construct_here);
6944       Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
6945           << isa<OMPExecutableDirective>(S);
6946       return StmtError();
6947     }
6948   }
6949 
6950   setFunctionHasBranchProtectedScope();
6951 
6952   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6953 }
6954 
6955 StmtResult
6956 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6957                                          Stmt *AStmt, SourceLocation StartLoc,
6958                                          SourceLocation EndLoc) {
6959   if (!AStmt)
6960     return StmtError();
6961 
6962   auto *CS = cast<CapturedStmt>(AStmt);
6963   // 1.2.2 OpenMP Language Terminology
6964   // Structured block - An executable statement with a single entry at the
6965   // top and a single exit at the bottom.
6966   // The point of exit cannot be a branch out of the structured block.
6967   // longjmp() and throw() must not violate the entry/exit criteria.
6968   CS->getCapturedDecl()->setNothrow();
6969   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
6970        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6971     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6972     // 1.2.2 OpenMP Language Terminology
6973     // Structured block - An executable statement with a single entry at the
6974     // top and a single exit at the bottom.
6975     // The point of exit cannot be a branch out of the structured block.
6976     // longjmp() and throw() must not violate the entry/exit criteria.
6977     CS->getCapturedDecl()->setNothrow();
6978   }
6979 
6980   setFunctionHasBranchProtectedScope();
6981 
6982   return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6983                                             AStmt);
6984 }
6985 
6986 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6987     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6988     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
6989   if (!AStmt)
6990     return StmtError();
6991 
6992   auto *CS = cast<CapturedStmt>(AStmt);
6993   // 1.2.2 OpenMP Language Terminology
6994   // Structured block - An executable statement with a single entry at the
6995   // top and a single exit at the bottom.
6996   // The point of exit cannot be a branch out of the structured block.
6997   // longjmp() and throw() must not violate the entry/exit criteria.
6998   CS->getCapturedDecl()->setNothrow();
6999   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7000        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7001     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7002     // 1.2.2 OpenMP Language Terminology
7003     // Structured block - An executable statement with a single entry at the
7004     // top and a single exit at the bottom.
7005     // The point of exit cannot be a branch out of the structured block.
7006     // longjmp() and throw() must not violate the entry/exit criteria.
7007     CS->getCapturedDecl()->setNothrow();
7008   }
7009 
7010   OMPLoopDirective::HelperExprs B;
7011   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7012   // define the nested loops number.
7013   unsigned NestedLoopCount =
7014       checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
7015                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
7016                       VarsWithImplicitDSA, B);
7017   if (NestedLoopCount == 0)
7018     return StmtError();
7019 
7020   assert((CurContext->isDependentContext() || B.builtAll()) &&
7021          "omp target parallel for loop exprs were not built");
7022 
7023   if (!CurContext->isDependentContext()) {
7024     // Finalize the clauses that need pre-built expressions for CodeGen.
7025     for (OMPClause *C : Clauses) {
7026       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7027         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7028                                      B.NumIterations, *this, CurScope,
7029                                      DSAStack))
7030           return StmtError();
7031     }
7032   }
7033 
7034   setFunctionHasBranchProtectedScope();
7035   return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
7036                                                NestedLoopCount, Clauses, AStmt,
7037                                                B, DSAStack->isCancelRegion());
7038 }
7039 
7040 /// Check for existence of a map clause in the list of clauses.
7041 static bool hasClauses(ArrayRef<OMPClause *> Clauses,
7042                        const OpenMPClauseKind K) {
7043   return llvm::any_of(
7044       Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
7045 }
7046 
7047 template <typename... Params>
7048 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
7049                        const Params... ClauseTypes) {
7050   return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
7051 }
7052 
7053 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
7054                                                 Stmt *AStmt,
7055                                                 SourceLocation StartLoc,
7056                                                 SourceLocation EndLoc) {
7057   if (!AStmt)
7058     return StmtError();
7059 
7060   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7061 
7062   // OpenMP [2.10.1, Restrictions, p. 97]
7063   // At least one map clause must appear on the directive.
7064   if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
7065     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7066         << "'map' or 'use_device_ptr'"
7067         << getOpenMPDirectiveName(OMPD_target_data);
7068     return StmtError();
7069   }
7070 
7071   setFunctionHasBranchProtectedScope();
7072 
7073   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7074                                         AStmt);
7075 }
7076 
7077 StmtResult
7078 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7079                                           SourceLocation StartLoc,
7080                                           SourceLocation EndLoc, Stmt *AStmt) {
7081   if (!AStmt)
7082     return StmtError();
7083 
7084   auto *CS = cast<CapturedStmt>(AStmt);
7085   // 1.2.2 OpenMP Language Terminology
7086   // Structured block - An executable statement with a single entry at the
7087   // top and a single exit at the bottom.
7088   // The point of exit cannot be a branch out of the structured block.
7089   // longjmp() and throw() must not violate the entry/exit criteria.
7090   CS->getCapturedDecl()->setNothrow();
7091   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
7092        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7093     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7094     // 1.2.2 OpenMP Language Terminology
7095     // Structured block - An executable statement with a single entry at the
7096     // top and a single exit at the bottom.
7097     // The point of exit cannot be a branch out of the structured block.
7098     // longjmp() and throw() must not violate the entry/exit criteria.
7099     CS->getCapturedDecl()->setNothrow();
7100   }
7101 
7102   // OpenMP [2.10.2, Restrictions, p. 99]
7103   // At least one map clause must appear on the directive.
7104   if (!hasClauses(Clauses, OMPC_map)) {
7105     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7106         << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
7107     return StmtError();
7108   }
7109 
7110   return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7111                                              AStmt);
7112 }
7113 
7114 StmtResult
7115 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7116                                          SourceLocation StartLoc,
7117                                          SourceLocation EndLoc, Stmt *AStmt) {
7118   if (!AStmt)
7119     return StmtError();
7120 
7121   auto *CS = cast<CapturedStmt>(AStmt);
7122   // 1.2.2 OpenMP Language Terminology
7123   // Structured block - An executable statement with a single entry at the
7124   // top and a single exit at the bottom.
7125   // The point of exit cannot be a branch out of the structured block.
7126   // longjmp() and throw() must not violate the entry/exit criteria.
7127   CS->getCapturedDecl()->setNothrow();
7128   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
7129        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7130     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7131     // 1.2.2 OpenMP Language Terminology
7132     // Structured block - An executable statement with a single entry at the
7133     // top and a single exit at the bottom.
7134     // The point of exit cannot be a branch out of the structured block.
7135     // longjmp() and throw() must not violate the entry/exit criteria.
7136     CS->getCapturedDecl()->setNothrow();
7137   }
7138 
7139   // OpenMP [2.10.3, Restrictions, p. 102]
7140   // At least one map clause must appear on the directive.
7141   if (!hasClauses(Clauses, OMPC_map)) {
7142     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7143         << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
7144     return StmtError();
7145   }
7146 
7147   return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7148                                             AStmt);
7149 }
7150 
7151 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7152                                                   SourceLocation StartLoc,
7153                                                   SourceLocation EndLoc,
7154                                                   Stmt *AStmt) {
7155   if (!AStmt)
7156     return StmtError();
7157 
7158   auto *CS = cast<CapturedStmt>(AStmt);
7159   // 1.2.2 OpenMP Language Terminology
7160   // Structured block - An executable statement with a single entry at the
7161   // top and a single exit at the bottom.
7162   // The point of exit cannot be a branch out of the structured block.
7163   // longjmp() and throw() must not violate the entry/exit criteria.
7164   CS->getCapturedDecl()->setNothrow();
7165   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
7166        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7167     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7168     // 1.2.2 OpenMP Language Terminology
7169     // Structured block - An executable statement with a single entry at the
7170     // top and a single exit at the bottom.
7171     // The point of exit cannot be a branch out of the structured block.
7172     // longjmp() and throw() must not violate the entry/exit criteria.
7173     CS->getCapturedDecl()->setNothrow();
7174   }
7175 
7176   if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
7177     Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7178     return StmtError();
7179   }
7180   return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
7181                                           AStmt);
7182 }
7183 
7184 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7185                                            Stmt *AStmt, SourceLocation StartLoc,
7186                                            SourceLocation EndLoc) {
7187   if (!AStmt)
7188     return StmtError();
7189 
7190   auto *CS = cast<CapturedStmt>(AStmt);
7191   // 1.2.2 OpenMP Language Terminology
7192   // Structured block - An executable statement with a single entry at the
7193   // top and a single exit at the bottom.
7194   // The point of exit cannot be a branch out of the structured block.
7195   // longjmp() and throw() must not violate the entry/exit criteria.
7196   CS->getCapturedDecl()->setNothrow();
7197 
7198   setFunctionHasBranchProtectedScope();
7199 
7200   DSAStack->setParentTeamsRegionLoc(StartLoc);
7201 
7202   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7203 }
7204 
7205 StmtResult
7206 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7207                                             SourceLocation EndLoc,
7208                                             OpenMPDirectiveKind CancelRegion) {
7209   if (DSAStack->isParentNowaitRegion()) {
7210     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7211     return StmtError();
7212   }
7213   if (DSAStack->isParentOrderedRegion()) {
7214     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7215     return StmtError();
7216   }
7217   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7218                                                CancelRegion);
7219 }
7220 
7221 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7222                                             SourceLocation StartLoc,
7223                                             SourceLocation EndLoc,
7224                                             OpenMPDirectiveKind CancelRegion) {
7225   if (DSAStack->isParentNowaitRegion()) {
7226     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7227     return StmtError();
7228   }
7229   if (DSAStack->isParentOrderedRegion()) {
7230     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7231     return StmtError();
7232   }
7233   DSAStack->setParentCancelRegion(/*Cancel=*/true);
7234   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7235                                     CancelRegion);
7236 }
7237 
7238 static bool checkGrainsizeNumTasksClauses(Sema &S,
7239                                           ArrayRef<OMPClause *> Clauses) {
7240   const OMPClause *PrevClause = nullptr;
7241   bool ErrorFound = false;
7242   for (const OMPClause *C : Clauses) {
7243     if (C->getClauseKind() == OMPC_grainsize ||
7244         C->getClauseKind() == OMPC_num_tasks) {
7245       if (!PrevClause)
7246         PrevClause = C;
7247       else if (PrevClause->getClauseKind() != C->getClauseKind()) {
7248         S.Diag(C->getBeginLoc(),
7249                diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7250             << getOpenMPClauseName(C->getClauseKind())
7251             << getOpenMPClauseName(PrevClause->getClauseKind());
7252         S.Diag(PrevClause->getBeginLoc(),
7253                diag::note_omp_previous_grainsize_num_tasks)
7254             << getOpenMPClauseName(PrevClause->getClauseKind());
7255         ErrorFound = true;
7256       }
7257     }
7258   }
7259   return ErrorFound;
7260 }
7261 
7262 static bool checkReductionClauseWithNogroup(Sema &S,
7263                                             ArrayRef<OMPClause *> Clauses) {
7264   const OMPClause *ReductionClause = nullptr;
7265   const OMPClause *NogroupClause = nullptr;
7266   for (const OMPClause *C : Clauses) {
7267     if (C->getClauseKind() == OMPC_reduction) {
7268       ReductionClause = C;
7269       if (NogroupClause)
7270         break;
7271       continue;
7272     }
7273     if (C->getClauseKind() == OMPC_nogroup) {
7274       NogroupClause = C;
7275       if (ReductionClause)
7276         break;
7277       continue;
7278     }
7279   }
7280   if (ReductionClause && NogroupClause) {
7281     S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7282         << SourceRange(NogroupClause->getBeginLoc(),
7283                        NogroupClause->getEndLoc());
7284     return true;
7285   }
7286   return false;
7287 }
7288 
7289 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7290     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7291     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7292   if (!AStmt)
7293     return StmtError();
7294 
7295   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7296   OMPLoopDirective::HelperExprs B;
7297   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7298   // define the nested loops number.
7299   unsigned NestedLoopCount =
7300       checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
7301                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7302                       VarsWithImplicitDSA, B);
7303   if (NestedLoopCount == 0)
7304     return StmtError();
7305 
7306   assert((CurContext->isDependentContext() || B.builtAll()) &&
7307          "omp for loop exprs were not built");
7308 
7309   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7310   // The grainsize clause and num_tasks clause are mutually exclusive and may
7311   // not appear on the same taskloop directive.
7312   if (checkGrainsizeNumTasksClauses(*this, Clauses))
7313     return StmtError();
7314   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7315   // If a reduction clause is present on the taskloop directive, the nogroup
7316   // clause must not be specified.
7317   if (checkReductionClauseWithNogroup(*this, Clauses))
7318     return StmtError();
7319 
7320   setFunctionHasBranchProtectedScope();
7321   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7322                                       NestedLoopCount, Clauses, AStmt, B);
7323 }
7324 
7325 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7326     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7327     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7328   if (!AStmt)
7329     return StmtError();
7330 
7331   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7332   OMPLoopDirective::HelperExprs B;
7333   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7334   // define the nested loops number.
7335   unsigned NestedLoopCount =
7336       checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7337                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7338                       VarsWithImplicitDSA, B);
7339   if (NestedLoopCount == 0)
7340     return StmtError();
7341 
7342   assert((CurContext->isDependentContext() || B.builtAll()) &&
7343          "omp for loop exprs were not built");
7344 
7345   if (!CurContext->isDependentContext()) {
7346     // Finalize the clauses that need pre-built expressions for CodeGen.
7347     for (OMPClause *C : Clauses) {
7348       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7349         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7350                                      B.NumIterations, *this, CurScope,
7351                                      DSAStack))
7352           return StmtError();
7353     }
7354   }
7355 
7356   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7357   // The grainsize clause and num_tasks clause are mutually exclusive and may
7358   // not appear on the same taskloop directive.
7359   if (checkGrainsizeNumTasksClauses(*this, Clauses))
7360     return StmtError();
7361   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7362   // If a reduction clause is present on the taskloop directive, the nogroup
7363   // clause must not be specified.
7364   if (checkReductionClauseWithNogroup(*this, Clauses))
7365     return StmtError();
7366   if (checkSimdlenSafelenSpecified(*this, Clauses))
7367     return StmtError();
7368 
7369   setFunctionHasBranchProtectedScope();
7370   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7371                                           NestedLoopCount, Clauses, AStmt, B);
7372 }
7373 
7374 StmtResult Sema::ActOnOpenMPDistributeDirective(
7375     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7376     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7377   if (!AStmt)
7378     return StmtError();
7379 
7380   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7381   OMPLoopDirective::HelperExprs B;
7382   // In presence of clause 'collapse' with number of loops, it will
7383   // define the nested loops number.
7384   unsigned NestedLoopCount =
7385       checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7386                       nullptr /*ordered not a clause on distribute*/, AStmt,
7387                       *this, *DSAStack, VarsWithImplicitDSA, B);
7388   if (NestedLoopCount == 0)
7389     return StmtError();
7390 
7391   assert((CurContext->isDependentContext() || B.builtAll()) &&
7392          "omp for loop exprs were not built");
7393 
7394   setFunctionHasBranchProtectedScope();
7395   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7396                                         NestedLoopCount, Clauses, AStmt, B);
7397 }
7398 
7399 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7400     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7401     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7402   if (!AStmt)
7403     return StmtError();
7404 
7405   auto *CS = cast<CapturedStmt>(AStmt);
7406   // 1.2.2 OpenMP Language Terminology
7407   // Structured block - An executable statement with a single entry at the
7408   // top and a single exit at the bottom.
7409   // The point of exit cannot be a branch out of the structured block.
7410   // longjmp() and throw() must not violate the entry/exit criteria.
7411   CS->getCapturedDecl()->setNothrow();
7412   for (int ThisCaptureLevel =
7413            getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7414        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7415     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7416     // 1.2.2 OpenMP Language Terminology
7417     // Structured block - An executable statement with a single entry at the
7418     // top and a single exit at the bottom.
7419     // The point of exit cannot be a branch out of the structured block.
7420     // longjmp() and throw() must not violate the entry/exit criteria.
7421     CS->getCapturedDecl()->setNothrow();
7422   }
7423 
7424   OMPLoopDirective::HelperExprs B;
7425   // In presence of clause 'collapse' with number of loops, it will
7426   // define the nested loops number.
7427   unsigned NestedLoopCount = checkOpenMPLoop(
7428       OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7429       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7430       VarsWithImplicitDSA, B);
7431   if (NestedLoopCount == 0)
7432     return StmtError();
7433 
7434   assert((CurContext->isDependentContext() || B.builtAll()) &&
7435          "omp for loop exprs were not built");
7436 
7437   setFunctionHasBranchProtectedScope();
7438   return OMPDistributeParallelForDirective::Create(
7439       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7440       DSAStack->isCancelRegion());
7441 }
7442 
7443 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7444     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7445     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7446   if (!AStmt)
7447     return StmtError();
7448 
7449   auto *CS = cast<CapturedStmt>(AStmt);
7450   // 1.2.2 OpenMP Language Terminology
7451   // Structured block - An executable statement with a single entry at the
7452   // top and a single exit at the bottom.
7453   // The point of exit cannot be a branch out of the structured block.
7454   // longjmp() and throw() must not violate the entry/exit criteria.
7455   CS->getCapturedDecl()->setNothrow();
7456   for (int ThisCaptureLevel =
7457            getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7458        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7459     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7460     // 1.2.2 OpenMP Language Terminology
7461     // Structured block - An executable statement with a single entry at the
7462     // top and a single exit at the bottom.
7463     // The point of exit cannot be a branch out of the structured block.
7464     // longjmp() and throw() must not violate the entry/exit criteria.
7465     CS->getCapturedDecl()->setNothrow();
7466   }
7467 
7468   OMPLoopDirective::HelperExprs B;
7469   // In presence of clause 'collapse' with number of loops, it will
7470   // define the nested loops number.
7471   unsigned NestedLoopCount = checkOpenMPLoop(
7472       OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7473       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7474       VarsWithImplicitDSA, B);
7475   if (NestedLoopCount == 0)
7476     return StmtError();
7477 
7478   assert((CurContext->isDependentContext() || B.builtAll()) &&
7479          "omp for loop exprs were not built");
7480 
7481   if (!CurContext->isDependentContext()) {
7482     // Finalize the clauses that need pre-built expressions for CodeGen.
7483     for (OMPClause *C : Clauses) {
7484       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7485         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7486                                      B.NumIterations, *this, CurScope,
7487                                      DSAStack))
7488           return StmtError();
7489     }
7490   }
7491 
7492   if (checkSimdlenSafelenSpecified(*this, Clauses))
7493     return StmtError();
7494 
7495   setFunctionHasBranchProtectedScope();
7496   return OMPDistributeParallelForSimdDirective::Create(
7497       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7498 }
7499 
7500 StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7501     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7502     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7503   if (!AStmt)
7504     return StmtError();
7505 
7506   auto *CS = cast<CapturedStmt>(AStmt);
7507   // 1.2.2 OpenMP Language Terminology
7508   // Structured block - An executable statement with a single entry at the
7509   // top and a single exit at the bottom.
7510   // The point of exit cannot be a branch out of the structured block.
7511   // longjmp() and throw() must not violate the entry/exit criteria.
7512   CS->getCapturedDecl()->setNothrow();
7513   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7514        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7515     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7516     // 1.2.2 OpenMP Language Terminology
7517     // Structured block - An executable statement with a single entry at the
7518     // top and a single exit at the bottom.
7519     // The point of exit cannot be a branch out of the structured block.
7520     // longjmp() and throw() must not violate the entry/exit criteria.
7521     CS->getCapturedDecl()->setNothrow();
7522   }
7523 
7524   OMPLoopDirective::HelperExprs B;
7525   // In presence of clause 'collapse' with number of loops, it will
7526   // define the nested loops number.
7527   unsigned NestedLoopCount =
7528       checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7529                       nullptr /*ordered not a clause on distribute*/, CS, *this,
7530                       *DSAStack, VarsWithImplicitDSA, B);
7531   if (NestedLoopCount == 0)
7532     return StmtError();
7533 
7534   assert((CurContext->isDependentContext() || B.builtAll()) &&
7535          "omp for loop exprs were not built");
7536 
7537   if (!CurContext->isDependentContext()) {
7538     // Finalize the clauses that need pre-built expressions for CodeGen.
7539     for (OMPClause *C : Clauses) {
7540       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7541         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7542                                      B.NumIterations, *this, CurScope,
7543                                      DSAStack))
7544           return StmtError();
7545     }
7546   }
7547 
7548   if (checkSimdlenSafelenSpecified(*this, Clauses))
7549     return StmtError();
7550 
7551   setFunctionHasBranchProtectedScope();
7552   return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7553                                             NestedLoopCount, Clauses, AStmt, B);
7554 }
7555 
7556 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7557     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7558     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7559   if (!AStmt)
7560     return StmtError();
7561 
7562   auto *CS = cast<CapturedStmt>(AStmt);
7563   // 1.2.2 OpenMP Language Terminology
7564   // Structured block - An executable statement with a single entry at the
7565   // top and a single exit at the bottom.
7566   // The point of exit cannot be a branch out of the structured block.
7567   // longjmp() and throw() must not violate the entry/exit criteria.
7568   CS->getCapturedDecl()->setNothrow();
7569   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7570        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7571     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7572     // 1.2.2 OpenMP Language Terminology
7573     // Structured block - An executable statement with a single entry at the
7574     // top and a single exit at the bottom.
7575     // The point of exit cannot be a branch out of the structured block.
7576     // longjmp() and throw() must not violate the entry/exit criteria.
7577     CS->getCapturedDecl()->setNothrow();
7578   }
7579 
7580   OMPLoopDirective::HelperExprs B;
7581   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7582   // define the nested loops number.
7583   unsigned NestedLoopCount = checkOpenMPLoop(
7584       OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7585       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
7586       VarsWithImplicitDSA, B);
7587   if (NestedLoopCount == 0)
7588     return StmtError();
7589 
7590   assert((CurContext->isDependentContext() || B.builtAll()) &&
7591          "omp target parallel for simd loop exprs were not built");
7592 
7593   if (!CurContext->isDependentContext()) {
7594     // Finalize the clauses that need pre-built expressions for CodeGen.
7595     for (OMPClause *C : Clauses) {
7596       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7597         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7598                                      B.NumIterations, *this, CurScope,
7599                                      DSAStack))
7600           return StmtError();
7601     }
7602   }
7603   if (checkSimdlenSafelenSpecified(*this, Clauses))
7604     return StmtError();
7605 
7606   setFunctionHasBranchProtectedScope();
7607   return OMPTargetParallelForSimdDirective::Create(
7608       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7609 }
7610 
7611 StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7612     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7613     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7614   if (!AStmt)
7615     return StmtError();
7616 
7617   auto *CS = cast<CapturedStmt>(AStmt);
7618   // 1.2.2 OpenMP Language Terminology
7619   // Structured block - An executable statement with a single entry at the
7620   // top and a single exit at the bottom.
7621   // The point of exit cannot be a branch out of the structured block.
7622   // longjmp() and throw() must not violate the entry/exit criteria.
7623   CS->getCapturedDecl()->setNothrow();
7624   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7625        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7626     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7627     // 1.2.2 OpenMP Language Terminology
7628     // Structured block - An executable statement with a single entry at the
7629     // top and a single exit at the bottom.
7630     // The point of exit cannot be a branch out of the structured block.
7631     // longjmp() and throw() must not violate the entry/exit criteria.
7632     CS->getCapturedDecl()->setNothrow();
7633   }
7634 
7635   OMPLoopDirective::HelperExprs B;
7636   // In presence of clause 'collapse' with number of loops, it will define the
7637   // nested loops number.
7638   unsigned NestedLoopCount =
7639       checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
7640                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
7641                       VarsWithImplicitDSA, B);
7642   if (NestedLoopCount == 0)
7643     return StmtError();
7644 
7645   assert((CurContext->isDependentContext() || B.builtAll()) &&
7646          "omp target simd loop exprs were not built");
7647 
7648   if (!CurContext->isDependentContext()) {
7649     // Finalize the clauses that need pre-built expressions for CodeGen.
7650     for (OMPClause *C : Clauses) {
7651       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7652         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7653                                      B.NumIterations, *this, CurScope,
7654                                      DSAStack))
7655           return StmtError();
7656     }
7657   }
7658 
7659   if (checkSimdlenSafelenSpecified(*this, Clauses))
7660     return StmtError();
7661 
7662   setFunctionHasBranchProtectedScope();
7663   return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7664                                         NestedLoopCount, Clauses, AStmt, B);
7665 }
7666 
7667 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7668     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7669     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7670   if (!AStmt)
7671     return StmtError();
7672 
7673   auto *CS = cast<CapturedStmt>(AStmt);
7674   // 1.2.2 OpenMP Language Terminology
7675   // Structured block - An executable statement with a single entry at the
7676   // top and a single exit at the bottom.
7677   // The point of exit cannot be a branch out of the structured block.
7678   // longjmp() and throw() must not violate the entry/exit criteria.
7679   CS->getCapturedDecl()->setNothrow();
7680   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7681        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7682     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7683     // 1.2.2 OpenMP Language Terminology
7684     // Structured block - An executable statement with a single entry at the
7685     // top and a single exit at the bottom.
7686     // The point of exit cannot be a branch out of the structured block.
7687     // longjmp() and throw() must not violate the entry/exit criteria.
7688     CS->getCapturedDecl()->setNothrow();
7689   }
7690 
7691   OMPLoopDirective::HelperExprs B;
7692   // In presence of clause 'collapse' with number of loops, it will
7693   // define the nested loops number.
7694   unsigned NestedLoopCount =
7695       checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
7696                       nullptr /*ordered not a clause on distribute*/, CS, *this,
7697                       *DSAStack, VarsWithImplicitDSA, B);
7698   if (NestedLoopCount == 0)
7699     return StmtError();
7700 
7701   assert((CurContext->isDependentContext() || B.builtAll()) &&
7702          "omp teams distribute loop exprs were not built");
7703 
7704   setFunctionHasBranchProtectedScope();
7705 
7706   DSAStack->setParentTeamsRegionLoc(StartLoc);
7707 
7708   return OMPTeamsDistributeDirective::Create(
7709       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7710 }
7711 
7712 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7713     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7714     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7715   if (!AStmt)
7716     return StmtError();
7717 
7718   auto *CS = cast<CapturedStmt>(AStmt);
7719   // 1.2.2 OpenMP Language Terminology
7720   // Structured block - An executable statement with a single entry at the
7721   // top and a single exit at the bottom.
7722   // The point of exit cannot be a branch out of the structured block.
7723   // longjmp() and throw() must not violate the entry/exit criteria.
7724   CS->getCapturedDecl()->setNothrow();
7725   for (int ThisCaptureLevel =
7726            getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7727        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7728     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7729     // 1.2.2 OpenMP Language Terminology
7730     // Structured block - An executable statement with a single entry at the
7731     // top and a single exit at the bottom.
7732     // The point of exit cannot be a branch out of the structured block.
7733     // longjmp() and throw() must not violate the entry/exit criteria.
7734     CS->getCapturedDecl()->setNothrow();
7735   }
7736 
7737 
7738   OMPLoopDirective::HelperExprs B;
7739   // In presence of clause 'collapse' with number of loops, it will
7740   // define the nested loops number.
7741   unsigned NestedLoopCount = checkOpenMPLoop(
7742       OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
7743       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7744       VarsWithImplicitDSA, B);
7745 
7746   if (NestedLoopCount == 0)
7747     return StmtError();
7748 
7749   assert((CurContext->isDependentContext() || B.builtAll()) &&
7750          "omp teams distribute simd loop exprs were not built");
7751 
7752   if (!CurContext->isDependentContext()) {
7753     // Finalize the clauses that need pre-built expressions for CodeGen.
7754     for (OMPClause *C : Clauses) {
7755       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7756         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7757                                      B.NumIterations, *this, CurScope,
7758                                      DSAStack))
7759           return StmtError();
7760     }
7761   }
7762 
7763   if (checkSimdlenSafelenSpecified(*this, Clauses))
7764     return StmtError();
7765 
7766   setFunctionHasBranchProtectedScope();
7767 
7768   DSAStack->setParentTeamsRegionLoc(StartLoc);
7769 
7770   return OMPTeamsDistributeSimdDirective::Create(
7771       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7772 }
7773 
7774 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7775     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7776     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7777   if (!AStmt)
7778     return StmtError();
7779 
7780   auto *CS = cast<CapturedStmt>(AStmt);
7781   // 1.2.2 OpenMP Language Terminology
7782   // Structured block - An executable statement with a single entry at the
7783   // top and a single exit at the bottom.
7784   // The point of exit cannot be a branch out of the structured block.
7785   // longjmp() and throw() must not violate the entry/exit criteria.
7786   CS->getCapturedDecl()->setNothrow();
7787 
7788   for (int ThisCaptureLevel =
7789            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7790        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7791     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7792     // 1.2.2 OpenMP Language Terminology
7793     // Structured block - An executable statement with a single entry at the
7794     // top and a single exit at the bottom.
7795     // The point of exit cannot be a branch out of the structured block.
7796     // longjmp() and throw() must not violate the entry/exit criteria.
7797     CS->getCapturedDecl()->setNothrow();
7798   }
7799 
7800   OMPLoopDirective::HelperExprs B;
7801   // In presence of clause 'collapse' with number of loops, it will
7802   // define the nested loops number.
7803   unsigned NestedLoopCount = checkOpenMPLoop(
7804       OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7805       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7806       VarsWithImplicitDSA, B);
7807 
7808   if (NestedLoopCount == 0)
7809     return StmtError();
7810 
7811   assert((CurContext->isDependentContext() || B.builtAll()) &&
7812          "omp for loop exprs were not built");
7813 
7814   if (!CurContext->isDependentContext()) {
7815     // Finalize the clauses that need pre-built expressions for CodeGen.
7816     for (OMPClause *C : Clauses) {
7817       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7818         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7819                                      B.NumIterations, *this, CurScope,
7820                                      DSAStack))
7821           return StmtError();
7822     }
7823   }
7824 
7825   if (checkSimdlenSafelenSpecified(*this, Clauses))
7826     return StmtError();
7827 
7828   setFunctionHasBranchProtectedScope();
7829 
7830   DSAStack->setParentTeamsRegionLoc(StartLoc);
7831 
7832   return OMPTeamsDistributeParallelForSimdDirective::Create(
7833       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7834 }
7835 
7836 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7837     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7838     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7839   if (!AStmt)
7840     return StmtError();
7841 
7842   auto *CS = cast<CapturedStmt>(AStmt);
7843   // 1.2.2 OpenMP Language Terminology
7844   // Structured block - An executable statement with a single entry at the
7845   // top and a single exit at the bottom.
7846   // The point of exit cannot be a branch out of the structured block.
7847   // longjmp() and throw() must not violate the entry/exit criteria.
7848   CS->getCapturedDecl()->setNothrow();
7849 
7850   for (int ThisCaptureLevel =
7851            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7852        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7853     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7854     // 1.2.2 OpenMP Language Terminology
7855     // Structured block - An executable statement with a single entry at the
7856     // top and a single exit at the bottom.
7857     // The point of exit cannot be a branch out of the structured block.
7858     // longjmp() and throw() must not violate the entry/exit criteria.
7859     CS->getCapturedDecl()->setNothrow();
7860   }
7861 
7862   OMPLoopDirective::HelperExprs B;
7863   // In presence of clause 'collapse' with number of loops, it will
7864   // define the nested loops number.
7865   unsigned NestedLoopCount = checkOpenMPLoop(
7866       OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7867       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7868       VarsWithImplicitDSA, B);
7869 
7870   if (NestedLoopCount == 0)
7871     return StmtError();
7872 
7873   assert((CurContext->isDependentContext() || B.builtAll()) &&
7874          "omp for loop exprs were not built");
7875 
7876   setFunctionHasBranchProtectedScope();
7877 
7878   DSAStack->setParentTeamsRegionLoc(StartLoc);
7879 
7880   return OMPTeamsDistributeParallelForDirective::Create(
7881       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7882       DSAStack->isCancelRegion());
7883 }
7884 
7885 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7886                                                  Stmt *AStmt,
7887                                                  SourceLocation StartLoc,
7888                                                  SourceLocation EndLoc) {
7889   if (!AStmt)
7890     return StmtError();
7891 
7892   auto *CS = cast<CapturedStmt>(AStmt);
7893   // 1.2.2 OpenMP Language Terminology
7894   // Structured block - An executable statement with a single entry at the
7895   // top and a single exit at the bottom.
7896   // The point of exit cannot be a branch out of the structured block.
7897   // longjmp() and throw() must not violate the entry/exit criteria.
7898   CS->getCapturedDecl()->setNothrow();
7899 
7900   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7901        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7902     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7903     // 1.2.2 OpenMP Language Terminology
7904     // Structured block - An executable statement with a single entry at the
7905     // top and a single exit at the bottom.
7906     // The point of exit cannot be a branch out of the structured block.
7907     // longjmp() and throw() must not violate the entry/exit criteria.
7908     CS->getCapturedDecl()->setNothrow();
7909   }
7910   setFunctionHasBranchProtectedScope();
7911 
7912   return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7913                                          AStmt);
7914 }
7915 
7916 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7917     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7918     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7919   if (!AStmt)
7920     return StmtError();
7921 
7922   auto *CS = cast<CapturedStmt>(AStmt);
7923   // 1.2.2 OpenMP Language Terminology
7924   // Structured block - An executable statement with a single entry at the
7925   // top and a single exit at the bottom.
7926   // The point of exit cannot be a branch out of the structured block.
7927   // longjmp() and throw() must not violate the entry/exit criteria.
7928   CS->getCapturedDecl()->setNothrow();
7929   for (int ThisCaptureLevel =
7930            getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7931        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7932     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7933     // 1.2.2 OpenMP Language Terminology
7934     // Structured block - An executable statement with a single entry at the
7935     // top and a single exit at the bottom.
7936     // The point of exit cannot be a branch out of the structured block.
7937     // longjmp() and throw() must not violate the entry/exit criteria.
7938     CS->getCapturedDecl()->setNothrow();
7939   }
7940 
7941   OMPLoopDirective::HelperExprs B;
7942   // In presence of clause 'collapse' with number of loops, it will
7943   // define the nested loops number.
7944   unsigned NestedLoopCount = checkOpenMPLoop(
7945       OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7946       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7947       VarsWithImplicitDSA, B);
7948   if (NestedLoopCount == 0)
7949     return StmtError();
7950 
7951   assert((CurContext->isDependentContext() || B.builtAll()) &&
7952          "omp target teams distribute loop exprs were not built");
7953 
7954   setFunctionHasBranchProtectedScope();
7955   return OMPTargetTeamsDistributeDirective::Create(
7956       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7957 }
7958 
7959 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7960     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7961     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7962   if (!AStmt)
7963     return StmtError();
7964 
7965   auto *CS = cast<CapturedStmt>(AStmt);
7966   // 1.2.2 OpenMP Language Terminology
7967   // Structured block - An executable statement with a single entry at the
7968   // top and a single exit at the bottom.
7969   // The point of exit cannot be a branch out of the structured block.
7970   // longjmp() and throw() must not violate the entry/exit criteria.
7971   CS->getCapturedDecl()->setNothrow();
7972   for (int ThisCaptureLevel =
7973            getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7974        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7975     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7976     // 1.2.2 OpenMP Language Terminology
7977     // Structured block - An executable statement with a single entry at the
7978     // top and a single exit at the bottom.
7979     // The point of exit cannot be a branch out of the structured block.
7980     // longjmp() and throw() must not violate the entry/exit criteria.
7981     CS->getCapturedDecl()->setNothrow();
7982   }
7983 
7984   OMPLoopDirective::HelperExprs B;
7985   // In presence of clause 'collapse' with number of loops, it will
7986   // define the nested loops number.
7987   unsigned NestedLoopCount = checkOpenMPLoop(
7988       OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7989       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7990       VarsWithImplicitDSA, B);
7991   if (NestedLoopCount == 0)
7992     return StmtError();
7993 
7994   assert((CurContext->isDependentContext() || B.builtAll()) &&
7995          "omp target teams distribute parallel for loop exprs were not built");
7996 
7997   if (!CurContext->isDependentContext()) {
7998     // Finalize the clauses that need pre-built expressions for CodeGen.
7999     for (OMPClause *C : Clauses) {
8000       if (auto *LC = dyn_cast<OMPLinearClause>(C))
8001         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8002                                      B.NumIterations, *this, CurScope,
8003                                      DSAStack))
8004           return StmtError();
8005     }
8006   }
8007 
8008   setFunctionHasBranchProtectedScope();
8009   return OMPTargetTeamsDistributeParallelForDirective::Create(
8010       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8011       DSAStack->isCancelRegion());
8012 }
8013 
8014 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
8015     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8016     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8017   if (!AStmt)
8018     return StmtError();
8019 
8020   auto *CS = cast<CapturedStmt>(AStmt);
8021   // 1.2.2 OpenMP Language Terminology
8022   // Structured block - An executable statement with a single entry at the
8023   // top and a single exit at the bottom.
8024   // The point of exit cannot be a branch out of the structured block.
8025   // longjmp() and throw() must not violate the entry/exit criteria.
8026   CS->getCapturedDecl()->setNothrow();
8027   for (int ThisCaptureLevel = getOpenMPCaptureLevels(
8028            OMPD_target_teams_distribute_parallel_for_simd);
8029        ThisCaptureLevel > 1; --ThisCaptureLevel) {
8030     CS = cast<CapturedStmt>(CS->getCapturedStmt());
8031     // 1.2.2 OpenMP Language Terminology
8032     // Structured block - An executable statement with a single entry at the
8033     // top and a single exit at the bottom.
8034     // The point of exit cannot be a branch out of the structured block.
8035     // longjmp() and throw() must not violate the entry/exit criteria.
8036     CS->getCapturedDecl()->setNothrow();
8037   }
8038 
8039   OMPLoopDirective::HelperExprs B;
8040   // In presence of clause 'collapse' with number of loops, it will
8041   // define the nested loops number.
8042   unsigned NestedLoopCount =
8043       checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
8044                       getCollapseNumberExpr(Clauses),
8045                       nullptr /*ordered not a clause on distribute*/, CS, *this,
8046                       *DSAStack, VarsWithImplicitDSA, B);
8047   if (NestedLoopCount == 0)
8048     return StmtError();
8049 
8050   assert((CurContext->isDependentContext() || B.builtAll()) &&
8051          "omp target teams distribute parallel for simd loop exprs were not "
8052          "built");
8053 
8054   if (!CurContext->isDependentContext()) {
8055     // Finalize the clauses that need pre-built expressions for CodeGen.
8056     for (OMPClause *C : Clauses) {
8057       if (auto *LC = dyn_cast<OMPLinearClause>(C))
8058         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8059                                      B.NumIterations, *this, CurScope,
8060                                      DSAStack))
8061           return StmtError();
8062     }
8063   }
8064 
8065   if (checkSimdlenSafelenSpecified(*this, Clauses))
8066     return StmtError();
8067 
8068   setFunctionHasBranchProtectedScope();
8069   return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
8070       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8071 }
8072 
8073 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
8074     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8075     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8076   if (!AStmt)
8077     return StmtError();
8078 
8079   auto *CS = cast<CapturedStmt>(AStmt);
8080   // 1.2.2 OpenMP Language Terminology
8081   // Structured block - An executable statement with a single entry at the
8082   // top and a single exit at the bottom.
8083   // The point of exit cannot be a branch out of the structured block.
8084   // longjmp() and throw() must not violate the entry/exit criteria.
8085   CS->getCapturedDecl()->setNothrow();
8086   for (int ThisCaptureLevel =
8087            getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
8088        ThisCaptureLevel > 1; --ThisCaptureLevel) {
8089     CS = cast<CapturedStmt>(CS->getCapturedStmt());
8090     // 1.2.2 OpenMP Language Terminology
8091     // Structured block - An executable statement with a single entry at the
8092     // top and a single exit at the bottom.
8093     // The point of exit cannot be a branch out of the structured block.
8094     // longjmp() and throw() must not violate the entry/exit criteria.
8095     CS->getCapturedDecl()->setNothrow();
8096   }
8097 
8098   OMPLoopDirective::HelperExprs B;
8099   // In presence of clause 'collapse' with number of loops, it will
8100   // define the nested loops number.
8101   unsigned NestedLoopCount = checkOpenMPLoop(
8102       OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
8103       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
8104       VarsWithImplicitDSA, B);
8105   if (NestedLoopCount == 0)
8106     return StmtError();
8107 
8108   assert((CurContext->isDependentContext() || B.builtAll()) &&
8109          "omp target teams distribute simd loop exprs were not built");
8110 
8111   if (!CurContext->isDependentContext()) {
8112     // Finalize the clauses that need pre-built expressions for CodeGen.
8113     for (OMPClause *C : Clauses) {
8114       if (auto *LC = dyn_cast<OMPLinearClause>(C))
8115         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8116                                      B.NumIterations, *this, CurScope,
8117                                      DSAStack))
8118           return StmtError();
8119     }
8120   }
8121 
8122   if (checkSimdlenSafelenSpecified(*this, Clauses))
8123     return StmtError();
8124 
8125   setFunctionHasBranchProtectedScope();
8126   return OMPTargetTeamsDistributeSimdDirective::Create(
8127       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8128 }
8129 
8130 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
8131                                              SourceLocation StartLoc,
8132                                              SourceLocation LParenLoc,
8133                                              SourceLocation EndLoc) {
8134   OMPClause *Res = nullptr;
8135   switch (Kind) {
8136   case OMPC_final:
8137     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
8138     break;
8139   case OMPC_num_threads:
8140     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
8141     break;
8142   case OMPC_safelen:
8143     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
8144     break;
8145   case OMPC_simdlen:
8146     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
8147     break;
8148   case OMPC_collapse:
8149     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
8150     break;
8151   case OMPC_ordered:
8152     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
8153     break;
8154   case OMPC_device:
8155     Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
8156     break;
8157   case OMPC_num_teams:
8158     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
8159     break;
8160   case OMPC_thread_limit:
8161     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
8162     break;
8163   case OMPC_priority:
8164     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
8165     break;
8166   case OMPC_grainsize:
8167     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
8168     break;
8169   case OMPC_num_tasks:
8170     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
8171     break;
8172   case OMPC_hint:
8173     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
8174     break;
8175   case OMPC_if:
8176   case OMPC_default:
8177   case OMPC_proc_bind:
8178   case OMPC_schedule:
8179   case OMPC_private:
8180   case OMPC_firstprivate:
8181   case OMPC_lastprivate:
8182   case OMPC_shared:
8183   case OMPC_reduction:
8184   case OMPC_task_reduction:
8185   case OMPC_in_reduction:
8186   case OMPC_linear:
8187   case OMPC_aligned:
8188   case OMPC_copyin:
8189   case OMPC_copyprivate:
8190   case OMPC_nowait:
8191   case OMPC_untied:
8192   case OMPC_mergeable:
8193   case OMPC_threadprivate:
8194   case OMPC_flush:
8195   case OMPC_read:
8196   case OMPC_write:
8197   case OMPC_update:
8198   case OMPC_capture:
8199   case OMPC_seq_cst:
8200   case OMPC_depend:
8201   case OMPC_threads:
8202   case OMPC_simd:
8203   case OMPC_map:
8204   case OMPC_nogroup:
8205   case OMPC_dist_schedule:
8206   case OMPC_defaultmap:
8207   case OMPC_unknown:
8208   case OMPC_uniform:
8209   case OMPC_to:
8210   case OMPC_from:
8211   case OMPC_use_device_ptr:
8212   case OMPC_is_device_ptr:
8213   case OMPC_unified_address:
8214   case OMPC_unified_shared_memory:
8215   case OMPC_reverse_offload:
8216   case OMPC_dynamic_allocators:
8217   case OMPC_atomic_default_mem_order:
8218     llvm_unreachable("Clause is not allowed.");
8219   }
8220   return Res;
8221 }
8222 
8223 // An OpenMP directive such as 'target parallel' has two captured regions:
8224 // for the 'target' and 'parallel' respectively.  This function returns
8225 // the region in which to capture expressions associated with a clause.
8226 // A return value of OMPD_unknown signifies that the expression should not
8227 // be captured.
8228 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8229     OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8230     OpenMPDirectiveKind NameModifier = OMPD_unknown) {
8231   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
8232   switch (CKind) {
8233   case OMPC_if:
8234     switch (DKind) {
8235     case OMPD_target_parallel:
8236     case OMPD_target_parallel_for:
8237     case OMPD_target_parallel_for_simd:
8238       // If this clause applies to the nested 'parallel' region, capture within
8239       // the 'target' region, otherwise do not capture.
8240       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8241         CaptureRegion = OMPD_target;
8242       break;
8243     case OMPD_target_teams_distribute_parallel_for:
8244     case OMPD_target_teams_distribute_parallel_for_simd:
8245       // If this clause applies to the nested 'parallel' region, capture within
8246       // the 'teams' region, otherwise do not capture.
8247       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8248         CaptureRegion = OMPD_teams;
8249       break;
8250     case OMPD_teams_distribute_parallel_for:
8251     case OMPD_teams_distribute_parallel_for_simd:
8252       CaptureRegion = OMPD_teams;
8253       break;
8254     case OMPD_target_update:
8255     case OMPD_target_enter_data:
8256     case OMPD_target_exit_data:
8257       CaptureRegion = OMPD_task;
8258       break;
8259     case OMPD_cancel:
8260     case OMPD_parallel:
8261     case OMPD_parallel_sections:
8262     case OMPD_parallel_for:
8263     case OMPD_parallel_for_simd:
8264     case OMPD_target:
8265     case OMPD_target_simd:
8266     case OMPD_target_teams:
8267     case OMPD_target_teams_distribute:
8268     case OMPD_target_teams_distribute_simd:
8269     case OMPD_distribute_parallel_for:
8270     case OMPD_distribute_parallel_for_simd:
8271     case OMPD_task:
8272     case OMPD_taskloop:
8273     case OMPD_taskloop_simd:
8274     case OMPD_target_data:
8275       // Do not capture if-clause expressions.
8276       break;
8277     case OMPD_threadprivate:
8278     case OMPD_taskyield:
8279     case OMPD_barrier:
8280     case OMPD_taskwait:
8281     case OMPD_cancellation_point:
8282     case OMPD_flush:
8283     case OMPD_declare_reduction:
8284     case OMPD_declare_simd:
8285     case OMPD_declare_target:
8286     case OMPD_end_declare_target:
8287     case OMPD_teams:
8288     case OMPD_simd:
8289     case OMPD_for:
8290     case OMPD_for_simd:
8291     case OMPD_sections:
8292     case OMPD_section:
8293     case OMPD_single:
8294     case OMPD_master:
8295     case OMPD_critical:
8296     case OMPD_taskgroup:
8297     case OMPD_distribute:
8298     case OMPD_ordered:
8299     case OMPD_atomic:
8300     case OMPD_distribute_simd:
8301     case OMPD_teams_distribute:
8302     case OMPD_teams_distribute_simd:
8303     case OMPD_requires:
8304       llvm_unreachable("Unexpected OpenMP directive with if-clause");
8305     case OMPD_unknown:
8306       llvm_unreachable("Unknown OpenMP directive");
8307     }
8308     break;
8309   case OMPC_num_threads:
8310     switch (DKind) {
8311     case OMPD_target_parallel:
8312     case OMPD_target_parallel_for:
8313     case OMPD_target_parallel_for_simd:
8314       CaptureRegion = OMPD_target;
8315       break;
8316     case OMPD_teams_distribute_parallel_for:
8317     case OMPD_teams_distribute_parallel_for_simd:
8318     case OMPD_target_teams_distribute_parallel_for:
8319     case OMPD_target_teams_distribute_parallel_for_simd:
8320       CaptureRegion = OMPD_teams;
8321       break;
8322     case OMPD_parallel:
8323     case OMPD_parallel_sections:
8324     case OMPD_parallel_for:
8325     case OMPD_parallel_for_simd:
8326     case OMPD_distribute_parallel_for:
8327     case OMPD_distribute_parallel_for_simd:
8328       // Do not capture num_threads-clause expressions.
8329       break;
8330     case OMPD_target_data:
8331     case OMPD_target_enter_data:
8332     case OMPD_target_exit_data:
8333     case OMPD_target_update:
8334     case OMPD_target:
8335     case OMPD_target_simd:
8336     case OMPD_target_teams:
8337     case OMPD_target_teams_distribute:
8338     case OMPD_target_teams_distribute_simd:
8339     case OMPD_cancel:
8340     case OMPD_task:
8341     case OMPD_taskloop:
8342     case OMPD_taskloop_simd:
8343     case OMPD_threadprivate:
8344     case OMPD_taskyield:
8345     case OMPD_barrier:
8346     case OMPD_taskwait:
8347     case OMPD_cancellation_point:
8348     case OMPD_flush:
8349     case OMPD_declare_reduction:
8350     case OMPD_declare_simd:
8351     case OMPD_declare_target:
8352     case OMPD_end_declare_target:
8353     case OMPD_teams:
8354     case OMPD_simd:
8355     case OMPD_for:
8356     case OMPD_for_simd:
8357     case OMPD_sections:
8358     case OMPD_section:
8359     case OMPD_single:
8360     case OMPD_master:
8361     case OMPD_critical:
8362     case OMPD_taskgroup:
8363     case OMPD_distribute:
8364     case OMPD_ordered:
8365     case OMPD_atomic:
8366     case OMPD_distribute_simd:
8367     case OMPD_teams_distribute:
8368     case OMPD_teams_distribute_simd:
8369     case OMPD_requires:
8370       llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8371     case OMPD_unknown:
8372       llvm_unreachable("Unknown OpenMP directive");
8373     }
8374     break;
8375   case OMPC_num_teams:
8376     switch (DKind) {
8377     case OMPD_target_teams:
8378     case OMPD_target_teams_distribute:
8379     case OMPD_target_teams_distribute_simd:
8380     case OMPD_target_teams_distribute_parallel_for:
8381     case OMPD_target_teams_distribute_parallel_for_simd:
8382       CaptureRegion = OMPD_target;
8383       break;
8384     case OMPD_teams_distribute_parallel_for:
8385     case OMPD_teams_distribute_parallel_for_simd:
8386     case OMPD_teams:
8387     case OMPD_teams_distribute:
8388     case OMPD_teams_distribute_simd:
8389       // Do not capture num_teams-clause expressions.
8390       break;
8391     case OMPD_distribute_parallel_for:
8392     case OMPD_distribute_parallel_for_simd:
8393     case OMPD_task:
8394     case OMPD_taskloop:
8395     case OMPD_taskloop_simd:
8396     case OMPD_target_data:
8397     case OMPD_target_enter_data:
8398     case OMPD_target_exit_data:
8399     case OMPD_target_update:
8400     case OMPD_cancel:
8401     case OMPD_parallel:
8402     case OMPD_parallel_sections:
8403     case OMPD_parallel_for:
8404     case OMPD_parallel_for_simd:
8405     case OMPD_target:
8406     case OMPD_target_simd:
8407     case OMPD_target_parallel:
8408     case OMPD_target_parallel_for:
8409     case OMPD_target_parallel_for_simd:
8410     case OMPD_threadprivate:
8411     case OMPD_taskyield:
8412     case OMPD_barrier:
8413     case OMPD_taskwait:
8414     case OMPD_cancellation_point:
8415     case OMPD_flush:
8416     case OMPD_declare_reduction:
8417     case OMPD_declare_simd:
8418     case OMPD_declare_target:
8419     case OMPD_end_declare_target:
8420     case OMPD_simd:
8421     case OMPD_for:
8422     case OMPD_for_simd:
8423     case OMPD_sections:
8424     case OMPD_section:
8425     case OMPD_single:
8426     case OMPD_master:
8427     case OMPD_critical:
8428     case OMPD_taskgroup:
8429     case OMPD_distribute:
8430     case OMPD_ordered:
8431     case OMPD_atomic:
8432     case OMPD_distribute_simd:
8433     case OMPD_requires:
8434       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8435     case OMPD_unknown:
8436       llvm_unreachable("Unknown OpenMP directive");
8437     }
8438     break;
8439   case OMPC_thread_limit:
8440     switch (DKind) {
8441     case OMPD_target_teams:
8442     case OMPD_target_teams_distribute:
8443     case OMPD_target_teams_distribute_simd:
8444     case OMPD_target_teams_distribute_parallel_for:
8445     case OMPD_target_teams_distribute_parallel_for_simd:
8446       CaptureRegion = OMPD_target;
8447       break;
8448     case OMPD_teams_distribute_parallel_for:
8449     case OMPD_teams_distribute_parallel_for_simd:
8450     case OMPD_teams:
8451     case OMPD_teams_distribute:
8452     case OMPD_teams_distribute_simd:
8453       // Do not capture thread_limit-clause expressions.
8454       break;
8455     case OMPD_distribute_parallel_for:
8456     case OMPD_distribute_parallel_for_simd:
8457     case OMPD_task:
8458     case OMPD_taskloop:
8459     case OMPD_taskloop_simd:
8460     case OMPD_target_data:
8461     case OMPD_target_enter_data:
8462     case OMPD_target_exit_data:
8463     case OMPD_target_update:
8464     case OMPD_cancel:
8465     case OMPD_parallel:
8466     case OMPD_parallel_sections:
8467     case OMPD_parallel_for:
8468     case OMPD_parallel_for_simd:
8469     case OMPD_target:
8470     case OMPD_target_simd:
8471     case OMPD_target_parallel:
8472     case OMPD_target_parallel_for:
8473     case OMPD_target_parallel_for_simd:
8474     case OMPD_threadprivate:
8475     case OMPD_taskyield:
8476     case OMPD_barrier:
8477     case OMPD_taskwait:
8478     case OMPD_cancellation_point:
8479     case OMPD_flush:
8480     case OMPD_declare_reduction:
8481     case OMPD_declare_simd:
8482     case OMPD_declare_target:
8483     case OMPD_end_declare_target:
8484     case OMPD_simd:
8485     case OMPD_for:
8486     case OMPD_for_simd:
8487     case OMPD_sections:
8488     case OMPD_section:
8489     case OMPD_single:
8490     case OMPD_master:
8491     case OMPD_critical:
8492     case OMPD_taskgroup:
8493     case OMPD_distribute:
8494     case OMPD_ordered:
8495     case OMPD_atomic:
8496     case OMPD_distribute_simd:
8497     case OMPD_requires:
8498       llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8499     case OMPD_unknown:
8500       llvm_unreachable("Unknown OpenMP directive");
8501     }
8502     break;
8503   case OMPC_schedule:
8504     switch (DKind) {
8505     case OMPD_parallel_for:
8506     case OMPD_parallel_for_simd:
8507     case OMPD_distribute_parallel_for:
8508     case OMPD_distribute_parallel_for_simd:
8509     case OMPD_teams_distribute_parallel_for:
8510     case OMPD_teams_distribute_parallel_for_simd:
8511     case OMPD_target_parallel_for:
8512     case OMPD_target_parallel_for_simd:
8513     case OMPD_target_teams_distribute_parallel_for:
8514     case OMPD_target_teams_distribute_parallel_for_simd:
8515       CaptureRegion = OMPD_parallel;
8516       break;
8517     case OMPD_for:
8518     case OMPD_for_simd:
8519       // Do not capture schedule-clause expressions.
8520       break;
8521     case OMPD_task:
8522     case OMPD_taskloop:
8523     case OMPD_taskloop_simd:
8524     case OMPD_target_data:
8525     case OMPD_target_enter_data:
8526     case OMPD_target_exit_data:
8527     case OMPD_target_update:
8528     case OMPD_teams:
8529     case OMPD_teams_distribute:
8530     case OMPD_teams_distribute_simd:
8531     case OMPD_target_teams_distribute:
8532     case OMPD_target_teams_distribute_simd:
8533     case OMPD_target:
8534     case OMPD_target_simd:
8535     case OMPD_target_parallel:
8536     case OMPD_cancel:
8537     case OMPD_parallel:
8538     case OMPD_parallel_sections:
8539     case OMPD_threadprivate:
8540     case OMPD_taskyield:
8541     case OMPD_barrier:
8542     case OMPD_taskwait:
8543     case OMPD_cancellation_point:
8544     case OMPD_flush:
8545     case OMPD_declare_reduction:
8546     case OMPD_declare_simd:
8547     case OMPD_declare_target:
8548     case OMPD_end_declare_target:
8549     case OMPD_simd:
8550     case OMPD_sections:
8551     case OMPD_section:
8552     case OMPD_single:
8553     case OMPD_master:
8554     case OMPD_critical:
8555     case OMPD_taskgroup:
8556     case OMPD_distribute:
8557     case OMPD_ordered:
8558     case OMPD_atomic:
8559     case OMPD_distribute_simd:
8560     case OMPD_target_teams:
8561     case OMPD_requires:
8562       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8563     case OMPD_unknown:
8564       llvm_unreachable("Unknown OpenMP directive");
8565     }
8566     break;
8567   case OMPC_dist_schedule:
8568     switch (DKind) {
8569     case OMPD_teams_distribute_parallel_for:
8570     case OMPD_teams_distribute_parallel_for_simd:
8571     case OMPD_teams_distribute:
8572     case OMPD_teams_distribute_simd:
8573     case OMPD_target_teams_distribute_parallel_for:
8574     case OMPD_target_teams_distribute_parallel_for_simd:
8575     case OMPD_target_teams_distribute:
8576     case OMPD_target_teams_distribute_simd:
8577       CaptureRegion = OMPD_teams;
8578       break;
8579     case OMPD_distribute_parallel_for:
8580     case OMPD_distribute_parallel_for_simd:
8581     case OMPD_distribute:
8582     case OMPD_distribute_simd:
8583       // Do not capture thread_limit-clause expressions.
8584       break;
8585     case OMPD_parallel_for:
8586     case OMPD_parallel_for_simd:
8587     case OMPD_target_parallel_for_simd:
8588     case OMPD_target_parallel_for:
8589     case OMPD_task:
8590     case OMPD_taskloop:
8591     case OMPD_taskloop_simd:
8592     case OMPD_target_data:
8593     case OMPD_target_enter_data:
8594     case OMPD_target_exit_data:
8595     case OMPD_target_update:
8596     case OMPD_teams:
8597     case OMPD_target:
8598     case OMPD_target_simd:
8599     case OMPD_target_parallel:
8600     case OMPD_cancel:
8601     case OMPD_parallel:
8602     case OMPD_parallel_sections:
8603     case OMPD_threadprivate:
8604     case OMPD_taskyield:
8605     case OMPD_barrier:
8606     case OMPD_taskwait:
8607     case OMPD_cancellation_point:
8608     case OMPD_flush:
8609     case OMPD_declare_reduction:
8610     case OMPD_declare_simd:
8611     case OMPD_declare_target:
8612     case OMPD_end_declare_target:
8613     case OMPD_simd:
8614     case OMPD_for:
8615     case OMPD_for_simd:
8616     case OMPD_sections:
8617     case OMPD_section:
8618     case OMPD_single:
8619     case OMPD_master:
8620     case OMPD_critical:
8621     case OMPD_taskgroup:
8622     case OMPD_ordered:
8623     case OMPD_atomic:
8624     case OMPD_target_teams:
8625     case OMPD_requires:
8626       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8627     case OMPD_unknown:
8628       llvm_unreachable("Unknown OpenMP directive");
8629     }
8630     break;
8631   case OMPC_device:
8632     switch (DKind) {
8633     case OMPD_target_update:
8634     case OMPD_target_enter_data:
8635     case OMPD_target_exit_data:
8636     case OMPD_target:
8637     case OMPD_target_simd:
8638     case OMPD_target_teams:
8639     case OMPD_target_parallel:
8640     case OMPD_target_teams_distribute:
8641     case OMPD_target_teams_distribute_simd:
8642     case OMPD_target_parallel_for:
8643     case OMPD_target_parallel_for_simd:
8644     case OMPD_target_teams_distribute_parallel_for:
8645     case OMPD_target_teams_distribute_parallel_for_simd:
8646       CaptureRegion = OMPD_task;
8647       break;
8648     case OMPD_target_data:
8649       // Do not capture device-clause expressions.
8650       break;
8651     case OMPD_teams_distribute_parallel_for:
8652     case OMPD_teams_distribute_parallel_for_simd:
8653     case OMPD_teams:
8654     case OMPD_teams_distribute:
8655     case OMPD_teams_distribute_simd:
8656     case OMPD_distribute_parallel_for:
8657     case OMPD_distribute_parallel_for_simd:
8658     case OMPD_task:
8659     case OMPD_taskloop:
8660     case OMPD_taskloop_simd:
8661     case OMPD_cancel:
8662     case OMPD_parallel:
8663     case OMPD_parallel_sections:
8664     case OMPD_parallel_for:
8665     case OMPD_parallel_for_simd:
8666     case OMPD_threadprivate:
8667     case OMPD_taskyield:
8668     case OMPD_barrier:
8669     case OMPD_taskwait:
8670     case OMPD_cancellation_point:
8671     case OMPD_flush:
8672     case OMPD_declare_reduction:
8673     case OMPD_declare_simd:
8674     case OMPD_declare_target:
8675     case OMPD_end_declare_target:
8676     case OMPD_simd:
8677     case OMPD_for:
8678     case OMPD_for_simd:
8679     case OMPD_sections:
8680     case OMPD_section:
8681     case OMPD_single:
8682     case OMPD_master:
8683     case OMPD_critical:
8684     case OMPD_taskgroup:
8685     case OMPD_distribute:
8686     case OMPD_ordered:
8687     case OMPD_atomic:
8688     case OMPD_distribute_simd:
8689     case OMPD_requires:
8690       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8691     case OMPD_unknown:
8692       llvm_unreachable("Unknown OpenMP directive");
8693     }
8694     break;
8695   case OMPC_firstprivate:
8696   case OMPC_lastprivate:
8697   case OMPC_reduction:
8698   case OMPC_task_reduction:
8699   case OMPC_in_reduction:
8700   case OMPC_linear:
8701   case OMPC_default:
8702   case OMPC_proc_bind:
8703   case OMPC_final:
8704   case OMPC_safelen:
8705   case OMPC_simdlen:
8706   case OMPC_collapse:
8707   case OMPC_private:
8708   case OMPC_shared:
8709   case OMPC_aligned:
8710   case OMPC_copyin:
8711   case OMPC_copyprivate:
8712   case OMPC_ordered:
8713   case OMPC_nowait:
8714   case OMPC_untied:
8715   case OMPC_mergeable:
8716   case OMPC_threadprivate:
8717   case OMPC_flush:
8718   case OMPC_read:
8719   case OMPC_write:
8720   case OMPC_update:
8721   case OMPC_capture:
8722   case OMPC_seq_cst:
8723   case OMPC_depend:
8724   case OMPC_threads:
8725   case OMPC_simd:
8726   case OMPC_map:
8727   case OMPC_priority:
8728   case OMPC_grainsize:
8729   case OMPC_nogroup:
8730   case OMPC_num_tasks:
8731   case OMPC_hint:
8732   case OMPC_defaultmap:
8733   case OMPC_unknown:
8734   case OMPC_uniform:
8735   case OMPC_to:
8736   case OMPC_from:
8737   case OMPC_use_device_ptr:
8738   case OMPC_is_device_ptr:
8739   case OMPC_unified_address:
8740   case OMPC_unified_shared_memory:
8741   case OMPC_reverse_offload:
8742   case OMPC_dynamic_allocators:
8743   case OMPC_atomic_default_mem_order:
8744     llvm_unreachable("Unexpected OpenMP clause.");
8745   }
8746   return CaptureRegion;
8747 }
8748 
8749 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8750                                      Expr *Condition, SourceLocation StartLoc,
8751                                      SourceLocation LParenLoc,
8752                                      SourceLocation NameModifierLoc,
8753                                      SourceLocation ColonLoc,
8754                                      SourceLocation EndLoc) {
8755   Expr *ValExpr = Condition;
8756   Stmt *HelperValStmt = nullptr;
8757   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
8758   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8759       !Condition->isInstantiationDependent() &&
8760       !Condition->containsUnexpandedParameterPack()) {
8761     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
8762     if (Val.isInvalid())
8763       return nullptr;
8764 
8765     ValExpr = Val.get();
8766 
8767     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8768     CaptureRegion =
8769         getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
8770     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
8771       ValExpr = MakeFullExpr(ValExpr).get();
8772       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
8773       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8774       HelperValStmt = buildPreInits(Context, Captures);
8775     }
8776   }
8777 
8778   return new (Context)
8779       OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8780                   LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
8781 }
8782 
8783 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8784                                         SourceLocation StartLoc,
8785                                         SourceLocation LParenLoc,
8786                                         SourceLocation EndLoc) {
8787   Expr *ValExpr = Condition;
8788   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8789       !Condition->isInstantiationDependent() &&
8790       !Condition->containsUnexpandedParameterPack()) {
8791     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
8792     if (Val.isInvalid())
8793       return nullptr;
8794 
8795     ValExpr = MakeFullExpr(Val.get()).get();
8796   }
8797 
8798   return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8799 }
8800 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8801                                                         Expr *Op) {
8802   if (!Op)
8803     return ExprError();
8804 
8805   class IntConvertDiagnoser : public ICEConvertDiagnoser {
8806   public:
8807     IntConvertDiagnoser()
8808         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
8809     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8810                                          QualType T) override {
8811       return S.Diag(Loc, diag::err_omp_not_integral) << T;
8812     }
8813     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8814                                              QualType T) override {
8815       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8816     }
8817     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8818                                                QualType T,
8819                                                QualType ConvTy) override {
8820       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8821     }
8822     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8823                                            QualType ConvTy) override {
8824       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
8825              << ConvTy->isEnumeralType() << ConvTy;
8826     }
8827     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8828                                             QualType T) override {
8829       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8830     }
8831     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8832                                         QualType ConvTy) override {
8833       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
8834              << ConvTy->isEnumeralType() << ConvTy;
8835     }
8836     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8837                                              QualType) override {
8838       llvm_unreachable("conversion functions are permitted");
8839     }
8840   } ConvertDiagnoser;
8841   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8842 }
8843 
8844 static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
8845                                       OpenMPClauseKind CKind,
8846                                       bool StrictlyPositive) {
8847   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8848       !ValExpr->isInstantiationDependent()) {
8849     SourceLocation Loc = ValExpr->getExprLoc();
8850     ExprResult Value =
8851         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8852     if (Value.isInvalid())
8853       return false;
8854 
8855     ValExpr = Value.get();
8856     // The expression must evaluate to a non-negative integer value.
8857     llvm::APSInt Result;
8858     if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
8859         Result.isSigned() &&
8860         !((!StrictlyPositive && Result.isNonNegative()) ||
8861           (StrictlyPositive && Result.isStrictlyPositive()))) {
8862       SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
8863           << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8864           << ValExpr->getSourceRange();
8865       return false;
8866     }
8867   }
8868   return true;
8869 }
8870 
8871 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8872                                              SourceLocation StartLoc,
8873                                              SourceLocation LParenLoc,
8874                                              SourceLocation EndLoc) {
8875   Expr *ValExpr = NumThreads;
8876   Stmt *HelperValStmt = nullptr;
8877 
8878   // OpenMP [2.5, Restrictions]
8879   //  The num_threads expression must evaluate to a positive integer value.
8880   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
8881                                  /*StrictlyPositive=*/true))
8882     return nullptr;
8883 
8884   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8885   OpenMPDirectiveKind CaptureRegion =
8886       getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8887   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
8888     ValExpr = MakeFullExpr(ValExpr).get();
8889     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
8890     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8891     HelperValStmt = buildPreInits(Context, Captures);
8892   }
8893 
8894   return new (Context) OMPNumThreadsClause(
8895       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
8896 }
8897 
8898 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
8899                                                        OpenMPClauseKind CKind,
8900                                                        bool StrictlyPositive) {
8901   if (!E)
8902     return ExprError();
8903   if (E->isValueDependent() || E->isTypeDependent() ||
8904       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
8905     return E;
8906   llvm::APSInt Result;
8907   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8908   if (ICE.isInvalid())
8909     return ExprError();
8910   if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8911       (!StrictlyPositive && !Result.isNonNegative())) {
8912     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
8913         << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8914         << E->getSourceRange();
8915     return ExprError();
8916   }
8917   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8918     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8919         << E->getSourceRange();
8920     return ExprError();
8921   }
8922   if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8923     DSAStack->setAssociatedLoops(Result.getExtValue());
8924   else if (CKind == OMPC_ordered)
8925     DSAStack->setAssociatedLoops(Result.getExtValue());
8926   return ICE;
8927 }
8928 
8929 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8930                                           SourceLocation LParenLoc,
8931                                           SourceLocation EndLoc) {
8932   // OpenMP [2.8.1, simd construct, Description]
8933   // The parameter of the safelen clause must be a constant
8934   // positive integer expression.
8935   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8936   if (Safelen.isInvalid())
8937     return nullptr;
8938   return new (Context)
8939       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
8940 }
8941 
8942 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8943                                           SourceLocation LParenLoc,
8944                                           SourceLocation EndLoc) {
8945   // OpenMP [2.8.1, simd construct, Description]
8946   // The parameter of the simdlen clause must be a constant
8947   // positive integer expression.
8948   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8949   if (Simdlen.isInvalid())
8950     return nullptr;
8951   return new (Context)
8952       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8953 }
8954 
8955 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8956                                            SourceLocation StartLoc,
8957                                            SourceLocation LParenLoc,
8958                                            SourceLocation EndLoc) {
8959   // OpenMP [2.7.1, loop construct, Description]
8960   // OpenMP [2.8.1, simd construct, Description]
8961   // OpenMP [2.9.6, distribute construct, Description]
8962   // The parameter of the collapse clause must be a constant
8963   // positive integer expression.
8964   ExprResult NumForLoopsResult =
8965       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8966   if (NumForLoopsResult.isInvalid())
8967     return nullptr;
8968   return new (Context)
8969       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
8970 }
8971 
8972 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8973                                           SourceLocation EndLoc,
8974                                           SourceLocation LParenLoc,
8975                                           Expr *NumForLoops) {
8976   // OpenMP [2.7.1, loop construct, Description]
8977   // OpenMP [2.8.1, simd construct, Description]
8978   // OpenMP [2.9.6, distribute construct, Description]
8979   // The parameter of the ordered clause must be a constant
8980   // positive integer expression if any.
8981   if (NumForLoops && LParenLoc.isValid()) {
8982     ExprResult NumForLoopsResult =
8983         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8984     if (NumForLoopsResult.isInvalid())
8985       return nullptr;
8986     NumForLoops = NumForLoopsResult.get();
8987   } else {
8988     NumForLoops = nullptr;
8989   }
8990   auto *Clause = OMPOrderedClause::Create(
8991       Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
8992       StartLoc, LParenLoc, EndLoc);
8993   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
8994   return Clause;
8995 }
8996 
8997 OMPClause *Sema::ActOnOpenMPSimpleClause(
8998     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8999     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
9000   OMPClause *Res = nullptr;
9001   switch (Kind) {
9002   case OMPC_default:
9003     Res =
9004         ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
9005                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
9006     break;
9007   case OMPC_proc_bind:
9008     Res = ActOnOpenMPProcBindClause(
9009         static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
9010         LParenLoc, EndLoc);
9011     break;
9012   case OMPC_atomic_default_mem_order:
9013     Res = ActOnOpenMPAtomicDefaultMemOrderClause(
9014         static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
9015         ArgumentLoc, StartLoc, LParenLoc, EndLoc);
9016     break;
9017   case OMPC_if:
9018   case OMPC_final:
9019   case OMPC_num_threads:
9020   case OMPC_safelen:
9021   case OMPC_simdlen:
9022   case OMPC_collapse:
9023   case OMPC_schedule:
9024   case OMPC_private:
9025   case OMPC_firstprivate:
9026   case OMPC_lastprivate:
9027   case OMPC_shared:
9028   case OMPC_reduction:
9029   case OMPC_task_reduction:
9030   case OMPC_in_reduction:
9031   case OMPC_linear:
9032   case OMPC_aligned:
9033   case OMPC_copyin:
9034   case OMPC_copyprivate:
9035   case OMPC_ordered:
9036   case OMPC_nowait:
9037   case OMPC_untied:
9038   case OMPC_mergeable:
9039   case OMPC_threadprivate:
9040   case OMPC_flush:
9041   case OMPC_read:
9042   case OMPC_write:
9043   case OMPC_update:
9044   case OMPC_capture:
9045   case OMPC_seq_cst:
9046   case OMPC_depend:
9047   case OMPC_device:
9048   case OMPC_threads:
9049   case OMPC_simd:
9050   case OMPC_map:
9051   case OMPC_num_teams:
9052   case OMPC_thread_limit:
9053   case OMPC_priority:
9054   case OMPC_grainsize:
9055   case OMPC_nogroup:
9056   case OMPC_num_tasks:
9057   case OMPC_hint:
9058   case OMPC_dist_schedule:
9059   case OMPC_defaultmap:
9060   case OMPC_unknown:
9061   case OMPC_uniform:
9062   case OMPC_to:
9063   case OMPC_from:
9064   case OMPC_use_device_ptr:
9065   case OMPC_is_device_ptr:
9066   case OMPC_unified_address:
9067   case OMPC_unified_shared_memory:
9068   case OMPC_reverse_offload:
9069   case OMPC_dynamic_allocators:
9070     llvm_unreachable("Clause is not allowed.");
9071   }
9072   return Res;
9073 }
9074 
9075 static std::string
9076 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
9077                         ArrayRef<unsigned> Exclude = llvm::None) {
9078   SmallString<256> Buffer;
9079   llvm::raw_svector_ostream Out(Buffer);
9080   unsigned Bound = Last >= 2 ? Last - 2 : 0;
9081   unsigned Skipped = Exclude.size();
9082   auto S = Exclude.begin(), E = Exclude.end();
9083   for (unsigned I = First; I < Last; ++I) {
9084     if (std::find(S, E, I) != E) {
9085       --Skipped;
9086       continue;
9087     }
9088     Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
9089     if (I == Bound - Skipped)
9090       Out << " or ";
9091     else if (I != Bound + 1 - Skipped)
9092       Out << ", ";
9093   }
9094   return Out.str();
9095 }
9096 
9097 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
9098                                           SourceLocation KindKwLoc,
9099                                           SourceLocation StartLoc,
9100                                           SourceLocation LParenLoc,
9101                                           SourceLocation EndLoc) {
9102   if (Kind == OMPC_DEFAULT_unknown) {
9103     static_assert(OMPC_DEFAULT_unknown > 0,
9104                   "OMPC_DEFAULT_unknown not greater than 0");
9105     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
9106         << getListOfPossibleValues(OMPC_default, /*First=*/0,
9107                                    /*Last=*/OMPC_DEFAULT_unknown)
9108         << getOpenMPClauseName(OMPC_default);
9109     return nullptr;
9110   }
9111   switch (Kind) {
9112   case OMPC_DEFAULT_none:
9113     DSAStack->setDefaultDSANone(KindKwLoc);
9114     break;
9115   case OMPC_DEFAULT_shared:
9116     DSAStack->setDefaultDSAShared(KindKwLoc);
9117     break;
9118   case OMPC_DEFAULT_unknown:
9119     llvm_unreachable("Clause kind is not allowed.");
9120     break;
9121   }
9122   return new (Context)
9123       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
9124 }
9125 
9126 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
9127                                            SourceLocation KindKwLoc,
9128                                            SourceLocation StartLoc,
9129                                            SourceLocation LParenLoc,
9130                                            SourceLocation EndLoc) {
9131   if (Kind == OMPC_PROC_BIND_unknown) {
9132     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
9133         << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
9134                                    /*Last=*/OMPC_PROC_BIND_unknown)
9135         << getOpenMPClauseName(OMPC_proc_bind);
9136     return nullptr;
9137   }
9138   return new (Context)
9139       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
9140 }
9141 
9142 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
9143     OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
9144     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
9145   if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
9146     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
9147         << getListOfPossibleValues(
9148                OMPC_atomic_default_mem_order, /*First=*/0,
9149                /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
9150         << getOpenMPClauseName(OMPC_atomic_default_mem_order);
9151     return nullptr;
9152   }
9153   return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
9154                                                       LParenLoc, EndLoc);
9155 }
9156 
9157 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
9158     OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
9159     SourceLocation StartLoc, SourceLocation LParenLoc,
9160     ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
9161     SourceLocation EndLoc) {
9162   OMPClause *Res = nullptr;
9163   switch (Kind) {
9164   case OMPC_schedule:
9165     enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
9166     assert(Argument.size() == NumberOfElements &&
9167            ArgumentLoc.size() == NumberOfElements);
9168     Res = ActOnOpenMPScheduleClause(
9169         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
9170         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
9171         static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
9172         StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
9173         ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
9174     break;
9175   case OMPC_if:
9176     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
9177     Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
9178                               Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
9179                               DelimLoc, EndLoc);
9180     break;
9181   case OMPC_dist_schedule:
9182     Res = ActOnOpenMPDistScheduleClause(
9183         static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
9184         StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
9185     break;
9186   case OMPC_defaultmap:
9187     enum { Modifier, DefaultmapKind };
9188     Res = ActOnOpenMPDefaultmapClause(
9189         static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
9190         static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
9191         StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
9192         EndLoc);
9193     break;
9194   case OMPC_final:
9195   case OMPC_num_threads:
9196   case OMPC_safelen:
9197   case OMPC_simdlen:
9198   case OMPC_collapse:
9199   case OMPC_default:
9200   case OMPC_proc_bind:
9201   case OMPC_private:
9202   case OMPC_firstprivate:
9203   case OMPC_lastprivate:
9204   case OMPC_shared:
9205   case OMPC_reduction:
9206   case OMPC_task_reduction:
9207   case OMPC_in_reduction:
9208   case OMPC_linear:
9209   case OMPC_aligned:
9210   case OMPC_copyin:
9211   case OMPC_copyprivate:
9212   case OMPC_ordered:
9213   case OMPC_nowait:
9214   case OMPC_untied:
9215   case OMPC_mergeable:
9216   case OMPC_threadprivate:
9217   case OMPC_flush:
9218   case OMPC_read:
9219   case OMPC_write:
9220   case OMPC_update:
9221   case OMPC_capture:
9222   case OMPC_seq_cst:
9223   case OMPC_depend:
9224   case OMPC_device:
9225   case OMPC_threads:
9226   case OMPC_simd:
9227   case OMPC_map:
9228   case OMPC_num_teams:
9229   case OMPC_thread_limit:
9230   case OMPC_priority:
9231   case OMPC_grainsize:
9232   case OMPC_nogroup:
9233   case OMPC_num_tasks:
9234   case OMPC_hint:
9235   case OMPC_unknown:
9236   case OMPC_uniform:
9237   case OMPC_to:
9238   case OMPC_from:
9239   case OMPC_use_device_ptr:
9240   case OMPC_is_device_ptr:
9241   case OMPC_unified_address:
9242   case OMPC_unified_shared_memory:
9243   case OMPC_reverse_offload:
9244   case OMPC_dynamic_allocators:
9245   case OMPC_atomic_default_mem_order:
9246     llvm_unreachable("Clause is not allowed.");
9247   }
9248   return Res;
9249 }
9250 
9251 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9252                                    OpenMPScheduleClauseModifier M2,
9253                                    SourceLocation M1Loc, SourceLocation M2Loc) {
9254   if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9255     SmallVector<unsigned, 2> Excluded;
9256     if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9257       Excluded.push_back(M2);
9258     if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9259       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9260     if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9261       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9262     S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9263         << getListOfPossibleValues(OMPC_schedule,
9264                                    /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9265                                    /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9266                                    Excluded)
9267         << getOpenMPClauseName(OMPC_schedule);
9268     return true;
9269   }
9270   return false;
9271 }
9272 
9273 OMPClause *Sema::ActOnOpenMPScheduleClause(
9274     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
9275     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9276     SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9277     SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9278   if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9279       checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9280     return nullptr;
9281   // OpenMP, 2.7.1, Loop Construct, Restrictions
9282   // Either the monotonic modifier or the nonmonotonic modifier can be specified
9283   // but not both.
9284   if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9285       (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9286        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9287       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9288        M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9289     Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9290         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9291         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9292     return nullptr;
9293   }
9294   if (Kind == OMPC_SCHEDULE_unknown) {
9295     std::string Values;
9296     if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9297       unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9298       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9299                                        /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9300                                        Exclude);
9301     } else {
9302       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9303                                        /*Last=*/OMPC_SCHEDULE_unknown);
9304     }
9305     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9306         << Values << getOpenMPClauseName(OMPC_schedule);
9307     return nullptr;
9308   }
9309   // OpenMP, 2.7.1, Loop Construct, Restrictions
9310   // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9311   // schedule(guided).
9312   if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9313        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9314       Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9315     Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9316          diag::err_omp_schedule_nonmonotonic_static);
9317     return nullptr;
9318   }
9319   Expr *ValExpr = ChunkSize;
9320   Stmt *HelperValStmt = nullptr;
9321   if (ChunkSize) {
9322     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9323         !ChunkSize->isInstantiationDependent() &&
9324         !ChunkSize->containsUnexpandedParameterPack()) {
9325       SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
9326       ExprResult Val =
9327           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9328       if (Val.isInvalid())
9329         return nullptr;
9330 
9331       ValExpr = Val.get();
9332 
9333       // OpenMP [2.7.1, Restrictions]
9334       //  chunk_size must be a loop invariant integer expression with a positive
9335       //  value.
9336       llvm::APSInt Result;
9337       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9338         if (Result.isSigned() && !Result.isStrictlyPositive()) {
9339           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9340               << "schedule" << 1 << ChunkSize->getSourceRange();
9341           return nullptr;
9342         }
9343       } else if (getOpenMPCaptureRegionForClause(
9344                      DSAStack->getCurrentDirective(), OMPC_schedule) !=
9345                      OMPD_unknown &&
9346                  !CurContext->isDependentContext()) {
9347         ValExpr = MakeFullExpr(ValExpr).get();
9348         llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
9349         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9350         HelperValStmt = buildPreInits(Context, Captures);
9351       }
9352     }
9353   }
9354 
9355   return new (Context)
9356       OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
9357                         ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
9358 }
9359 
9360 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9361                                    SourceLocation StartLoc,
9362                                    SourceLocation EndLoc) {
9363   OMPClause *Res = nullptr;
9364   switch (Kind) {
9365   case OMPC_ordered:
9366     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9367     break;
9368   case OMPC_nowait:
9369     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9370     break;
9371   case OMPC_untied:
9372     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9373     break;
9374   case OMPC_mergeable:
9375     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9376     break;
9377   case OMPC_read:
9378     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9379     break;
9380   case OMPC_write:
9381     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9382     break;
9383   case OMPC_update:
9384     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9385     break;
9386   case OMPC_capture:
9387     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9388     break;
9389   case OMPC_seq_cst:
9390     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9391     break;
9392   case OMPC_threads:
9393     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9394     break;
9395   case OMPC_simd:
9396     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9397     break;
9398   case OMPC_nogroup:
9399     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9400     break;
9401   case OMPC_unified_address:
9402     Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9403     break;
9404   case OMPC_unified_shared_memory:
9405     Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9406     break;
9407   case OMPC_reverse_offload:
9408     Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
9409     break;
9410   case OMPC_dynamic_allocators:
9411     Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
9412     break;
9413   case OMPC_if:
9414   case OMPC_final:
9415   case OMPC_num_threads:
9416   case OMPC_safelen:
9417   case OMPC_simdlen:
9418   case OMPC_collapse:
9419   case OMPC_schedule:
9420   case OMPC_private:
9421   case OMPC_firstprivate:
9422   case OMPC_lastprivate:
9423   case OMPC_shared:
9424   case OMPC_reduction:
9425   case OMPC_task_reduction:
9426   case OMPC_in_reduction:
9427   case OMPC_linear:
9428   case OMPC_aligned:
9429   case OMPC_copyin:
9430   case OMPC_copyprivate:
9431   case OMPC_default:
9432   case OMPC_proc_bind:
9433   case OMPC_threadprivate:
9434   case OMPC_flush:
9435   case OMPC_depend:
9436   case OMPC_device:
9437   case OMPC_map:
9438   case OMPC_num_teams:
9439   case OMPC_thread_limit:
9440   case OMPC_priority:
9441   case OMPC_grainsize:
9442   case OMPC_num_tasks:
9443   case OMPC_hint:
9444   case OMPC_dist_schedule:
9445   case OMPC_defaultmap:
9446   case OMPC_unknown:
9447   case OMPC_uniform:
9448   case OMPC_to:
9449   case OMPC_from:
9450   case OMPC_use_device_ptr:
9451   case OMPC_is_device_ptr:
9452   case OMPC_atomic_default_mem_order:
9453     llvm_unreachable("Clause is not allowed.");
9454   }
9455   return Res;
9456 }
9457 
9458 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9459                                          SourceLocation EndLoc) {
9460   DSAStack->setNowaitRegion();
9461   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9462 }
9463 
9464 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9465                                          SourceLocation EndLoc) {
9466   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9467 }
9468 
9469 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9470                                             SourceLocation EndLoc) {
9471   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9472 }
9473 
9474 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9475                                        SourceLocation EndLoc) {
9476   return new (Context) OMPReadClause(StartLoc, EndLoc);
9477 }
9478 
9479 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9480                                         SourceLocation EndLoc) {
9481   return new (Context) OMPWriteClause(StartLoc, EndLoc);
9482 }
9483 
9484 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9485                                          SourceLocation EndLoc) {
9486   return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9487 }
9488 
9489 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9490                                           SourceLocation EndLoc) {
9491   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9492 }
9493 
9494 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9495                                          SourceLocation EndLoc) {
9496   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9497 }
9498 
9499 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9500                                           SourceLocation EndLoc) {
9501   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9502 }
9503 
9504 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9505                                        SourceLocation EndLoc) {
9506   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9507 }
9508 
9509 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9510                                           SourceLocation EndLoc) {
9511   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9512 }
9513 
9514 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9515                                                  SourceLocation EndLoc) {
9516   return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9517 }
9518 
9519 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9520                                                       SourceLocation EndLoc) {
9521   return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9522 }
9523 
9524 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
9525                                                  SourceLocation EndLoc) {
9526   return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
9527 }
9528 
9529 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
9530                                                     SourceLocation EndLoc) {
9531   return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
9532 }
9533 
9534 OMPClause *Sema::ActOnOpenMPVarListClause(
9535     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9536     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9537     SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
9538     const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
9539     OpenMPLinearClauseKind LinKind,
9540     ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
9541     ArrayRef<SourceLocation> MapTypeModifiersLoc,
9542     OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9543     SourceLocation DepLinMapLoc) {
9544   OMPClause *Res = nullptr;
9545   switch (Kind) {
9546   case OMPC_private:
9547     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9548     break;
9549   case OMPC_firstprivate:
9550     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9551     break;
9552   case OMPC_lastprivate:
9553     Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9554     break;
9555   case OMPC_shared:
9556     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9557     break;
9558   case OMPC_reduction:
9559     Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9560                                      EndLoc, ReductionIdScopeSpec, ReductionId);
9561     break;
9562   case OMPC_task_reduction:
9563     Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9564                                          EndLoc, ReductionIdScopeSpec,
9565                                          ReductionId);
9566     break;
9567   case OMPC_in_reduction:
9568     Res =
9569         ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9570                                      EndLoc, ReductionIdScopeSpec, ReductionId);
9571     break;
9572   case OMPC_linear:
9573     Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
9574                                   LinKind, DepLinMapLoc, ColonLoc, EndLoc);
9575     break;
9576   case OMPC_aligned:
9577     Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9578                                    ColonLoc, EndLoc);
9579     break;
9580   case OMPC_copyin:
9581     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9582     break;
9583   case OMPC_copyprivate:
9584     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9585     break;
9586   case OMPC_flush:
9587     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9588     break;
9589   case OMPC_depend:
9590     Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
9591                                   StartLoc, LParenLoc, EndLoc);
9592     break;
9593   case OMPC_map:
9594     Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc, MapType,
9595                                IsMapTypeImplicit, DepLinMapLoc, ColonLoc,
9596                                VarList, StartLoc, LParenLoc, EndLoc);
9597     break;
9598   case OMPC_to:
9599     Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9600     break;
9601   case OMPC_from:
9602     Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9603     break;
9604   case OMPC_use_device_ptr:
9605     Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9606     break;
9607   case OMPC_is_device_ptr:
9608     Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9609     break;
9610   case OMPC_if:
9611   case OMPC_final:
9612   case OMPC_num_threads:
9613   case OMPC_safelen:
9614   case OMPC_simdlen:
9615   case OMPC_collapse:
9616   case OMPC_default:
9617   case OMPC_proc_bind:
9618   case OMPC_schedule:
9619   case OMPC_ordered:
9620   case OMPC_nowait:
9621   case OMPC_untied:
9622   case OMPC_mergeable:
9623   case OMPC_threadprivate:
9624   case OMPC_read:
9625   case OMPC_write:
9626   case OMPC_update:
9627   case OMPC_capture:
9628   case OMPC_seq_cst:
9629   case OMPC_device:
9630   case OMPC_threads:
9631   case OMPC_simd:
9632   case OMPC_num_teams:
9633   case OMPC_thread_limit:
9634   case OMPC_priority:
9635   case OMPC_grainsize:
9636   case OMPC_nogroup:
9637   case OMPC_num_tasks:
9638   case OMPC_hint:
9639   case OMPC_dist_schedule:
9640   case OMPC_defaultmap:
9641   case OMPC_unknown:
9642   case OMPC_uniform:
9643   case OMPC_unified_address:
9644   case OMPC_unified_shared_memory:
9645   case OMPC_reverse_offload:
9646   case OMPC_dynamic_allocators:
9647   case OMPC_atomic_default_mem_order:
9648     llvm_unreachable("Clause is not allowed.");
9649   }
9650   return Res;
9651 }
9652 
9653 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
9654                                        ExprObjectKind OK, SourceLocation Loc) {
9655   ExprResult Res = BuildDeclRefExpr(
9656       Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9657   if (!Res.isUsable())
9658     return ExprError();
9659   if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9660     Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9661     if (!Res.isUsable())
9662       return ExprError();
9663   }
9664   if (VK != VK_LValue && Res.get()->isGLValue()) {
9665     Res = DefaultLvalueConversion(Res.get());
9666     if (!Res.isUsable())
9667       return ExprError();
9668   }
9669   return Res;
9670 }
9671 
9672 static std::pair<ValueDecl *, bool>
9673 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9674                SourceRange &ERange, bool AllowArraySection = false) {
9675   if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9676       RefExpr->containsUnexpandedParameterPack())
9677     return std::make_pair(nullptr, true);
9678 
9679   // OpenMP [3.1, C/C++]
9680   //  A list item is a variable name.
9681   // OpenMP  [2.9.3.3, Restrictions, p.1]
9682   //  A variable that is part of another variable (as an array or
9683   //  structure element) cannot appear in a private clause.
9684   RefExpr = RefExpr->IgnoreParens();
9685   enum {
9686     NoArrayExpr = -1,
9687     ArraySubscript = 0,
9688     OMPArraySection = 1
9689   } IsArrayExpr = NoArrayExpr;
9690   if (AllowArraySection) {
9691     if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
9692       Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
9693       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9694         Base = TempASE->getBase()->IgnoreParenImpCasts();
9695       RefExpr = Base;
9696       IsArrayExpr = ArraySubscript;
9697     } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
9698       Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
9699       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9700         Base = TempOASE->getBase()->IgnoreParenImpCasts();
9701       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9702         Base = TempASE->getBase()->IgnoreParenImpCasts();
9703       RefExpr = Base;
9704       IsArrayExpr = OMPArraySection;
9705     }
9706   }
9707   ELoc = RefExpr->getExprLoc();
9708   ERange = RefExpr->getSourceRange();
9709   RefExpr = RefExpr->IgnoreParenImpCasts();
9710   auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9711   auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9712   if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9713       (S.getCurrentThisType().isNull() || !ME ||
9714        !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9715        !isa<FieldDecl>(ME->getMemberDecl()))) {
9716     if (IsArrayExpr != NoArrayExpr) {
9717       S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9718                                                          << ERange;
9719     } else {
9720       S.Diag(ELoc,
9721              AllowArraySection
9722                  ? diag::err_omp_expected_var_name_member_expr_or_array_item
9723                  : diag::err_omp_expected_var_name_member_expr)
9724           << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9725     }
9726     return std::make_pair(nullptr, false);
9727   }
9728   return std::make_pair(
9729       getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
9730 }
9731 
9732 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9733                                           SourceLocation StartLoc,
9734                                           SourceLocation LParenLoc,
9735                                           SourceLocation EndLoc) {
9736   SmallVector<Expr *, 8> Vars;
9737   SmallVector<Expr *, 8> PrivateCopies;
9738   for (Expr *RefExpr : VarList) {
9739     assert(RefExpr && "NULL expr in OpenMP private clause.");
9740     SourceLocation ELoc;
9741     SourceRange ERange;
9742     Expr *SimpleRefExpr = RefExpr;
9743     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
9744     if (Res.second) {
9745       // It will be analyzed later.
9746       Vars.push_back(RefExpr);
9747       PrivateCopies.push_back(nullptr);
9748     }
9749     ValueDecl *D = Res.first;
9750     if (!D)
9751       continue;
9752 
9753     QualType Type = D->getType();
9754     auto *VD = dyn_cast<VarDecl>(D);
9755 
9756     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9757     //  A variable that appears in a private clause must not have an incomplete
9758     //  type or a reference type.
9759     if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
9760       continue;
9761     Type = Type.getNonReferenceType();
9762 
9763     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9764     // in a Construct]
9765     //  Variables with the predetermined data-sharing attributes may not be
9766     //  listed in data-sharing attributes clauses, except for the cases
9767     //  listed below. For these exceptions only, listing a predetermined
9768     //  variable in a data-sharing attribute clause is allowed and overrides
9769     //  the variable's predetermined data-sharing attributes.
9770     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
9771     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
9772       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9773                                           << getOpenMPClauseName(OMPC_private);
9774       reportOriginalDsa(*this, DSAStack, D, DVar);
9775       continue;
9776     }
9777 
9778     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9779     // Variably modified types are not supported for tasks.
9780     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
9781         isOpenMPTaskingDirective(CurrDir)) {
9782       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9783           << getOpenMPClauseName(OMPC_private) << Type
9784           << getOpenMPDirectiveName(CurrDir);
9785       bool IsDecl =
9786           !VD ||
9787           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9788       Diag(D->getLocation(),
9789            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9790           << D;
9791       continue;
9792     }
9793 
9794     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9795     // A list item cannot appear in both a map clause and a data-sharing
9796     // attribute clause on the same construct
9797     if (isOpenMPTargetExecutionDirective(CurrDir)) {
9798       OpenMPClauseKind ConflictKind;
9799       if (DSAStack->checkMappableExprComponentListsForDecl(
9800               VD, /*CurrentRegionOnly=*/true,
9801               [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9802                   OpenMPClauseKind WhereFoundClauseKind) -> bool {
9803                 ConflictKind = WhereFoundClauseKind;
9804                 return true;
9805               })) {
9806         Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
9807             << getOpenMPClauseName(OMPC_private)
9808             << getOpenMPClauseName(ConflictKind)
9809             << getOpenMPDirectiveName(CurrDir);
9810         reportOriginalDsa(*this, DSAStack, D, DVar);
9811         continue;
9812       }
9813     }
9814 
9815     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9816     //  A variable of class type (or array thereof) that appears in a private
9817     //  clause requires an accessible, unambiguous default constructor for the
9818     //  class type.
9819     // Generate helper private variable and initialize it with the default
9820     // value. The address of the original variable is replaced by the address of
9821     // the new private variable in CodeGen. This new variable is not added to
9822     // IdResolver, so the code in the OpenMP region uses original variable for
9823     // proper diagnostics.
9824     Type = Type.getUnqualifiedType();
9825     VarDecl *VDPrivate =
9826         buildVarDecl(*this, ELoc, Type, D->getName(),
9827                      D->hasAttrs() ? &D->getAttrs() : nullptr,
9828                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
9829     ActOnUninitializedDecl(VDPrivate);
9830     if (VDPrivate->isInvalidDecl())
9831       continue;
9832     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
9833         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
9834 
9835     DeclRefExpr *Ref = nullptr;
9836     if (!VD && !CurContext->isDependentContext())
9837       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9838     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
9839     Vars.push_back((VD || CurContext->isDependentContext())
9840                        ? RefExpr->IgnoreParens()
9841                        : Ref);
9842     PrivateCopies.push_back(VDPrivateRefExpr);
9843   }
9844 
9845   if (Vars.empty())
9846     return nullptr;
9847 
9848   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9849                                   PrivateCopies);
9850 }
9851 
9852 namespace {
9853 class DiagsUninitializedSeveretyRAII {
9854 private:
9855   DiagnosticsEngine &Diags;
9856   SourceLocation SavedLoc;
9857   bool IsIgnored = false;
9858 
9859 public:
9860   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9861                                  bool IsIgnored)
9862       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9863     if (!IsIgnored) {
9864       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9865                         /*Map*/ diag::Severity::Ignored, Loc);
9866     }
9867   }
9868   ~DiagsUninitializedSeveretyRAII() {
9869     if (!IsIgnored)
9870       Diags.popMappings(SavedLoc);
9871   }
9872 };
9873 }
9874 
9875 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9876                                                SourceLocation StartLoc,
9877                                                SourceLocation LParenLoc,
9878                                                SourceLocation EndLoc) {
9879   SmallVector<Expr *, 8> Vars;
9880   SmallVector<Expr *, 8> PrivateCopies;
9881   SmallVector<Expr *, 8> Inits;
9882   SmallVector<Decl *, 4> ExprCaptures;
9883   bool IsImplicitClause =
9884       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
9885   SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
9886 
9887   for (Expr *RefExpr : VarList) {
9888     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
9889     SourceLocation ELoc;
9890     SourceRange ERange;
9891     Expr *SimpleRefExpr = RefExpr;
9892     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
9893     if (Res.second) {
9894       // It will be analyzed later.
9895       Vars.push_back(RefExpr);
9896       PrivateCopies.push_back(nullptr);
9897       Inits.push_back(nullptr);
9898     }
9899     ValueDecl *D = Res.first;
9900     if (!D)
9901       continue;
9902 
9903     ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
9904     QualType Type = D->getType();
9905     auto *VD = dyn_cast<VarDecl>(D);
9906 
9907     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9908     //  A variable that appears in a private clause must not have an incomplete
9909     //  type or a reference type.
9910     if (RequireCompleteType(ELoc, Type,
9911                             diag::err_omp_firstprivate_incomplete_type))
9912       continue;
9913     Type = Type.getNonReferenceType();
9914 
9915     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9916     //  A variable of class type (or array thereof) that appears in a private
9917     //  clause requires an accessible, unambiguous copy constructor for the
9918     //  class type.
9919     QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
9920 
9921     // If an implicit firstprivate variable found it was checked already.
9922     DSAStackTy::DSAVarData TopDVar;
9923     if (!IsImplicitClause) {
9924       DSAStackTy::DSAVarData DVar =
9925           DSAStack->getTopDSA(D, /*FromParent=*/false);
9926       TopDVar = DVar;
9927       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9928       bool IsConstant = ElemType.isConstant(Context);
9929       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9930       //  A list item that specifies a given variable may not appear in more
9931       // than one clause on the same directive, except that a variable may be
9932       //  specified in both firstprivate and lastprivate clauses.
9933       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9934       // A list item may appear in a firstprivate or lastprivate clause but not
9935       // both.
9936       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
9937           (isOpenMPDistributeDirective(CurrDir) ||
9938            DVar.CKind != OMPC_lastprivate) &&
9939           DVar.RefExpr) {
9940         Diag(ELoc, diag::err_omp_wrong_dsa)
9941             << getOpenMPClauseName(DVar.CKind)
9942             << getOpenMPClauseName(OMPC_firstprivate);
9943         reportOriginalDsa(*this, DSAStack, D, DVar);
9944         continue;
9945       }
9946 
9947       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9948       // in a Construct]
9949       //  Variables with the predetermined data-sharing attributes may not be
9950       //  listed in data-sharing attributes clauses, except for the cases
9951       //  listed below. For these exceptions only, listing a predetermined
9952       //  variable in a data-sharing attribute clause is allowed and overrides
9953       //  the variable's predetermined data-sharing attributes.
9954       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9955       // in a Construct, C/C++, p.2]
9956       //  Variables with const-qualified type having no mutable member may be
9957       //  listed in a firstprivate clause, even if they are static data members.
9958       if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
9959           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9960         Diag(ELoc, diag::err_omp_wrong_dsa)
9961             << getOpenMPClauseName(DVar.CKind)
9962             << getOpenMPClauseName(OMPC_firstprivate);
9963         reportOriginalDsa(*this, DSAStack, D, DVar);
9964         continue;
9965       }
9966 
9967       // OpenMP [2.9.3.4, Restrictions, p.2]
9968       //  A list item that is private within a parallel region must not appear
9969       //  in a firstprivate clause on a worksharing construct if any of the
9970       //  worksharing regions arising from the worksharing construct ever bind
9971       //  to any of the parallel regions arising from the parallel construct.
9972       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9973       // A list item that is private within a teams region must not appear in a
9974       // firstprivate clause on a distribute construct if any of the distribute
9975       // regions arising from the distribute construct ever bind to any of the
9976       // teams regions arising from the teams construct.
9977       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9978       // A list item that appears in a reduction clause of a teams construct
9979       // must not appear in a firstprivate clause on a distribute construct if
9980       // any of the distribute regions arising from the distribute construct
9981       // ever bind to any of the teams regions arising from the teams construct.
9982       if ((isOpenMPWorksharingDirective(CurrDir) ||
9983            isOpenMPDistributeDirective(CurrDir)) &&
9984           !isOpenMPParallelDirective(CurrDir) &&
9985           !isOpenMPTeamsDirective(CurrDir)) {
9986         DVar = DSAStack->getImplicitDSA(D, true);
9987         if (DVar.CKind != OMPC_shared &&
9988             (isOpenMPParallelDirective(DVar.DKind) ||
9989              isOpenMPTeamsDirective(DVar.DKind) ||
9990              DVar.DKind == OMPD_unknown)) {
9991           Diag(ELoc, diag::err_omp_required_access)
9992               << getOpenMPClauseName(OMPC_firstprivate)
9993               << getOpenMPClauseName(OMPC_shared);
9994           reportOriginalDsa(*this, DSAStack, D, DVar);
9995           continue;
9996         }
9997       }
9998       // OpenMP [2.9.3.4, Restrictions, p.3]
9999       //  A list item that appears in a reduction clause of a parallel construct
10000       //  must not appear in a firstprivate clause on a worksharing or task
10001       //  construct if any of the worksharing or task regions arising from the
10002       //  worksharing or task construct ever bind to any of the parallel regions
10003       //  arising from the parallel construct.
10004       // OpenMP [2.9.3.4, Restrictions, p.4]
10005       //  A list item that appears in a reduction clause in worksharing
10006       //  construct must not appear in a firstprivate clause in a task construct
10007       //  encountered during execution of any of the worksharing regions arising
10008       //  from the worksharing construct.
10009       if (isOpenMPTaskingDirective(CurrDir)) {
10010         DVar = DSAStack->hasInnermostDSA(
10011             D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
10012             [](OpenMPDirectiveKind K) {
10013               return isOpenMPParallelDirective(K) ||
10014                      isOpenMPWorksharingDirective(K) ||
10015                      isOpenMPTeamsDirective(K);
10016             },
10017             /*FromParent=*/true);
10018         if (DVar.CKind == OMPC_reduction &&
10019             (isOpenMPParallelDirective(DVar.DKind) ||
10020              isOpenMPWorksharingDirective(DVar.DKind) ||
10021              isOpenMPTeamsDirective(DVar.DKind))) {
10022           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
10023               << getOpenMPDirectiveName(DVar.DKind);
10024           reportOriginalDsa(*this, DSAStack, D, DVar);
10025           continue;
10026         }
10027       }
10028 
10029       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10030       // A list item cannot appear in both a map clause and a data-sharing
10031       // attribute clause on the same construct
10032       if (isOpenMPTargetExecutionDirective(CurrDir)) {
10033         OpenMPClauseKind ConflictKind;
10034         if (DSAStack->checkMappableExprComponentListsForDecl(
10035                 VD, /*CurrentRegionOnly=*/true,
10036                 [&ConflictKind](
10037                     OMPClauseMappableExprCommon::MappableExprComponentListRef,
10038                     OpenMPClauseKind WhereFoundClauseKind) {
10039                   ConflictKind = WhereFoundClauseKind;
10040                   return true;
10041                 })) {
10042           Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
10043               << getOpenMPClauseName(OMPC_firstprivate)
10044               << getOpenMPClauseName(ConflictKind)
10045               << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10046           reportOriginalDsa(*this, DSAStack, D, DVar);
10047           continue;
10048         }
10049       }
10050     }
10051 
10052     // Variably modified types are not supported for tasks.
10053     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
10054         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
10055       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10056           << getOpenMPClauseName(OMPC_firstprivate) << Type
10057           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10058       bool IsDecl =
10059           !VD ||
10060           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10061       Diag(D->getLocation(),
10062            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10063           << D;
10064       continue;
10065     }
10066 
10067     Type = Type.getUnqualifiedType();
10068     VarDecl *VDPrivate =
10069         buildVarDecl(*this, ELoc, Type, D->getName(),
10070                      D->hasAttrs() ? &D->getAttrs() : nullptr,
10071                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
10072     // Generate helper private variable and initialize it with the value of the
10073     // original variable. The address of the original variable is replaced by
10074     // the address of the new private variable in the CodeGen. This new variable
10075     // is not added to IdResolver, so the code in the OpenMP region uses
10076     // original variable for proper diagnostics and variable capturing.
10077     Expr *VDInitRefExpr = nullptr;
10078     // For arrays generate initializer for single element and replace it by the
10079     // original array element in CodeGen.
10080     if (Type->isArrayType()) {
10081       VarDecl *VDInit =
10082           buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
10083       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
10084       Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
10085       ElemType = ElemType.getUnqualifiedType();
10086       VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
10087                                          ".firstprivate.temp");
10088       InitializedEntity Entity =
10089           InitializedEntity::InitializeVariable(VDInitTemp);
10090       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
10091 
10092       InitializationSequence InitSeq(*this, Entity, Kind, Init);
10093       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
10094       if (Result.isInvalid())
10095         VDPrivate->setInvalidDecl();
10096       else
10097         VDPrivate->setInit(Result.getAs<Expr>());
10098       // Remove temp variable declaration.
10099       Context.Deallocate(VDInitTemp);
10100     } else {
10101       VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
10102                                      ".firstprivate.temp");
10103       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
10104                                        RefExpr->getExprLoc());
10105       AddInitializerToDecl(VDPrivate,
10106                            DefaultLvalueConversion(VDInitRefExpr).get(),
10107                            /*DirectInit=*/false);
10108     }
10109     if (VDPrivate->isInvalidDecl()) {
10110       if (IsImplicitClause) {
10111         Diag(RefExpr->getExprLoc(),
10112              diag::note_omp_task_predetermined_firstprivate_here);
10113       }
10114       continue;
10115     }
10116     CurContext->addDecl(VDPrivate);
10117     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
10118         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
10119         RefExpr->getExprLoc());
10120     DeclRefExpr *Ref = nullptr;
10121     if (!VD && !CurContext->isDependentContext()) {
10122       if (TopDVar.CKind == OMPC_lastprivate) {
10123         Ref = TopDVar.PrivateCopy;
10124       } else {
10125         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
10126         if (!isOpenMPCapturedDecl(D))
10127           ExprCaptures.push_back(Ref->getDecl());
10128       }
10129     }
10130     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
10131     Vars.push_back((VD || CurContext->isDependentContext())
10132                        ? RefExpr->IgnoreParens()
10133                        : Ref);
10134     PrivateCopies.push_back(VDPrivateRefExpr);
10135     Inits.push_back(VDInitRefExpr);
10136   }
10137 
10138   if (Vars.empty())
10139     return nullptr;
10140 
10141   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10142                                        Vars, PrivateCopies, Inits,
10143                                        buildPreInits(Context, ExprCaptures));
10144 }
10145 
10146 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
10147                                               SourceLocation StartLoc,
10148                                               SourceLocation LParenLoc,
10149                                               SourceLocation EndLoc) {
10150   SmallVector<Expr *, 8> Vars;
10151   SmallVector<Expr *, 8> SrcExprs;
10152   SmallVector<Expr *, 8> DstExprs;
10153   SmallVector<Expr *, 8> AssignmentOps;
10154   SmallVector<Decl *, 4> ExprCaptures;
10155   SmallVector<Expr *, 4> ExprPostUpdates;
10156   for (Expr *RefExpr : VarList) {
10157     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
10158     SourceLocation ELoc;
10159     SourceRange ERange;
10160     Expr *SimpleRefExpr = RefExpr;
10161     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
10162     if (Res.second) {
10163       // It will be analyzed later.
10164       Vars.push_back(RefExpr);
10165       SrcExprs.push_back(nullptr);
10166       DstExprs.push_back(nullptr);
10167       AssignmentOps.push_back(nullptr);
10168     }
10169     ValueDecl *D = Res.first;
10170     if (!D)
10171       continue;
10172 
10173     QualType Type = D->getType();
10174     auto *VD = dyn_cast<VarDecl>(D);
10175 
10176     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
10177     //  A variable that appears in a lastprivate clause must not have an
10178     //  incomplete type or a reference type.
10179     if (RequireCompleteType(ELoc, Type,
10180                             diag::err_omp_lastprivate_incomplete_type))
10181       continue;
10182     Type = Type.getNonReferenceType();
10183 
10184     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
10185     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10186     // in a Construct]
10187     //  Variables with the predetermined data-sharing attributes may not be
10188     //  listed in data-sharing attributes clauses, except for the cases
10189     //  listed below.
10190     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10191     // A list item may appear in a firstprivate or lastprivate clause but not
10192     // both.
10193     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
10194     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
10195         (isOpenMPDistributeDirective(CurrDir) ||
10196          DVar.CKind != OMPC_firstprivate) &&
10197         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
10198       Diag(ELoc, diag::err_omp_wrong_dsa)
10199           << getOpenMPClauseName(DVar.CKind)
10200           << getOpenMPClauseName(OMPC_lastprivate);
10201       reportOriginalDsa(*this, DSAStack, D, DVar);
10202       continue;
10203     }
10204 
10205     // OpenMP [2.14.3.5, Restrictions, p.2]
10206     // A list item that is private within a parallel region, or that appears in
10207     // the reduction clause of a parallel construct, must not appear in a
10208     // lastprivate clause on a worksharing construct if any of the corresponding
10209     // worksharing regions ever binds to any of the corresponding parallel
10210     // regions.
10211     DSAStackTy::DSAVarData TopDVar = DVar;
10212     if (isOpenMPWorksharingDirective(CurrDir) &&
10213         !isOpenMPParallelDirective(CurrDir) &&
10214         !isOpenMPTeamsDirective(CurrDir)) {
10215       DVar = DSAStack->getImplicitDSA(D, true);
10216       if (DVar.CKind != OMPC_shared) {
10217         Diag(ELoc, diag::err_omp_required_access)
10218             << getOpenMPClauseName(OMPC_lastprivate)
10219             << getOpenMPClauseName(OMPC_shared);
10220         reportOriginalDsa(*this, DSAStack, D, DVar);
10221         continue;
10222       }
10223     }
10224 
10225     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
10226     //  A variable of class type (or array thereof) that appears in a
10227     //  lastprivate clause requires an accessible, unambiguous default
10228     //  constructor for the class type, unless the list item is also specified
10229     //  in a firstprivate clause.
10230     //  A variable of class type (or array thereof) that appears in a
10231     //  lastprivate clause requires an accessible, unambiguous copy assignment
10232     //  operator for the class type.
10233     Type = Context.getBaseElementType(Type).getNonReferenceType();
10234     VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
10235                                   Type.getUnqualifiedType(), ".lastprivate.src",
10236                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
10237     DeclRefExpr *PseudoSrcExpr =
10238         buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
10239     VarDecl *DstVD =
10240         buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
10241                      D->hasAttrs() ? &D->getAttrs() : nullptr);
10242     DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
10243     // For arrays generate assignment operation for single element and replace
10244     // it by the original array element in CodeGen.
10245     ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
10246                                          PseudoDstExpr, PseudoSrcExpr);
10247     if (AssignmentOp.isInvalid())
10248       continue;
10249     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
10250                                        /*DiscardedValue=*/true);
10251     if (AssignmentOp.isInvalid())
10252       continue;
10253 
10254     DeclRefExpr *Ref = nullptr;
10255     if (!VD && !CurContext->isDependentContext()) {
10256       if (TopDVar.CKind == OMPC_firstprivate) {
10257         Ref = TopDVar.PrivateCopy;
10258       } else {
10259         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
10260         if (!isOpenMPCapturedDecl(D))
10261           ExprCaptures.push_back(Ref->getDecl());
10262       }
10263       if (TopDVar.CKind == OMPC_firstprivate ||
10264           (!isOpenMPCapturedDecl(D) &&
10265            Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
10266         ExprResult RefRes = DefaultLvalueConversion(Ref);
10267         if (!RefRes.isUsable())
10268           continue;
10269         ExprResult PostUpdateRes =
10270             BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10271                        RefRes.get());
10272         if (!PostUpdateRes.isUsable())
10273           continue;
10274         ExprPostUpdates.push_back(
10275             IgnoredValueConversions(PostUpdateRes.get()).get());
10276       }
10277     }
10278     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
10279     Vars.push_back((VD || CurContext->isDependentContext())
10280                        ? RefExpr->IgnoreParens()
10281                        : Ref);
10282     SrcExprs.push_back(PseudoSrcExpr);
10283     DstExprs.push_back(PseudoDstExpr);
10284     AssignmentOps.push_back(AssignmentOp.get());
10285   }
10286 
10287   if (Vars.empty())
10288     return nullptr;
10289 
10290   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10291                                       Vars, SrcExprs, DstExprs, AssignmentOps,
10292                                       buildPreInits(Context, ExprCaptures),
10293                                       buildPostUpdate(*this, ExprPostUpdates));
10294 }
10295 
10296 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10297                                          SourceLocation StartLoc,
10298                                          SourceLocation LParenLoc,
10299                                          SourceLocation EndLoc) {
10300   SmallVector<Expr *, 8> Vars;
10301   for (Expr *RefExpr : VarList) {
10302     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
10303     SourceLocation ELoc;
10304     SourceRange ERange;
10305     Expr *SimpleRefExpr = RefExpr;
10306     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
10307     if (Res.second) {
10308       // It will be analyzed later.
10309       Vars.push_back(RefExpr);
10310     }
10311     ValueDecl *D = Res.first;
10312     if (!D)
10313       continue;
10314 
10315     auto *VD = dyn_cast<VarDecl>(D);
10316     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10317     // in a Construct]
10318     //  Variables with the predetermined data-sharing attributes may not be
10319     //  listed in data-sharing attributes clauses, except for the cases
10320     //  listed below. For these exceptions only, listing a predetermined
10321     //  variable in a data-sharing attribute clause is allowed and overrides
10322     //  the variable's predetermined data-sharing attributes.
10323     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
10324     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10325         DVar.RefExpr) {
10326       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10327                                           << getOpenMPClauseName(OMPC_shared);
10328       reportOriginalDsa(*this, DSAStack, D, DVar);
10329       continue;
10330     }
10331 
10332     DeclRefExpr *Ref = nullptr;
10333     if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
10334       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
10335     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
10336     Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10337                        ? RefExpr->IgnoreParens()
10338                        : Ref);
10339   }
10340 
10341   if (Vars.empty())
10342     return nullptr;
10343 
10344   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10345 }
10346 
10347 namespace {
10348 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10349   DSAStackTy *Stack;
10350 
10351 public:
10352   bool VisitDeclRefExpr(DeclRefExpr *E) {
10353     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10354       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
10355       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10356         return false;
10357       if (DVar.CKind != OMPC_unknown)
10358         return true;
10359       DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
10360           VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
10361           /*FromParent=*/true);
10362       return DVarPrivate.CKind != OMPC_unknown;
10363     }
10364     return false;
10365   }
10366   bool VisitStmt(Stmt *S) {
10367     for (Stmt *Child : S->children()) {
10368       if (Child && Visit(Child))
10369         return true;
10370     }
10371     return false;
10372   }
10373   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
10374 };
10375 } // namespace
10376 
10377 namespace {
10378 // Transform MemberExpression for specified FieldDecl of current class to
10379 // DeclRefExpr to specified OMPCapturedExprDecl.
10380 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10381   typedef TreeTransform<TransformExprToCaptures> BaseTransform;
10382   ValueDecl *Field = nullptr;
10383   DeclRefExpr *CapturedExpr = nullptr;
10384 
10385 public:
10386   TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10387       : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10388 
10389   ExprResult TransformMemberExpr(MemberExpr *E) {
10390     if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10391         E->getMemberDecl() == Field) {
10392       CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
10393       return CapturedExpr;
10394     }
10395     return BaseTransform::TransformMemberExpr(E);
10396   }
10397   DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10398 };
10399 } // namespace
10400 
10401 template <typename T, typename U>
10402 static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
10403                             const llvm::function_ref<T(ValueDecl *)> Gen) {
10404   for (U &Set : Lookups) {
10405     for (auto *D : Set) {
10406       if (T Res = Gen(cast<ValueDecl>(D)))
10407         return Res;
10408     }
10409   }
10410   return T();
10411 }
10412 
10413 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10414   assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10415 
10416   for (auto RD : D->redecls()) {
10417     // Don't bother with extra checks if we already know this one isn't visible.
10418     if (RD == D)
10419       continue;
10420 
10421     auto ND = cast<NamedDecl>(RD);
10422     if (LookupResult::isVisible(SemaRef, ND))
10423       return ND;
10424   }
10425 
10426   return nullptr;
10427 }
10428 
10429 static void
10430 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId,
10431                         SourceLocation Loc, QualType Ty,
10432                         SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10433   // Find all of the associated namespaces and classes based on the
10434   // arguments we have.
10435   Sema::AssociatedNamespaceSet AssociatedNamespaces;
10436   Sema::AssociatedClassSet AssociatedClasses;
10437   OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10438   SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10439                                              AssociatedClasses);
10440 
10441   // C++ [basic.lookup.argdep]p3:
10442   //   Let X be the lookup set produced by unqualified lookup (3.4.1)
10443   //   and let Y be the lookup set produced by argument dependent
10444   //   lookup (defined as follows). If X contains [...] then Y is
10445   //   empty. Otherwise Y is the set of declarations found in the
10446   //   namespaces associated with the argument types as described
10447   //   below. The set of declarations found by the lookup of the name
10448   //   is the union of X and Y.
10449   //
10450   // Here, we compute Y and add its members to the overloaded
10451   // candidate set.
10452   for (auto *NS : AssociatedNamespaces) {
10453     //   When considering an associated namespace, the lookup is the
10454     //   same as the lookup performed when the associated namespace is
10455     //   used as a qualifier (3.4.3.2) except that:
10456     //
10457     //     -- Any using-directives in the associated namespace are
10458     //        ignored.
10459     //
10460     //     -- Any namespace-scope friend functions declared in
10461     //        associated classes are visible within their respective
10462     //        namespaces even if they are not visible during an ordinary
10463     //        lookup (11.4).
10464     DeclContext::lookup_result R = NS->lookup(ReductionId.getName());
10465     for (auto *D : R) {
10466       auto *Underlying = D;
10467       if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10468         Underlying = USD->getTargetDecl();
10469 
10470       if (!isa<OMPDeclareReductionDecl>(Underlying))
10471         continue;
10472 
10473       if (!SemaRef.isVisible(D)) {
10474         D = findAcceptableDecl(SemaRef, D);
10475         if (!D)
10476           continue;
10477         if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10478           Underlying = USD->getTargetDecl();
10479       }
10480       Lookups.emplace_back();
10481       Lookups.back().addDecl(Underlying);
10482     }
10483   }
10484 }
10485 
10486 static ExprResult
10487 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10488                          Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10489                          const DeclarationNameInfo &ReductionId, QualType Ty,
10490                          CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10491   if (ReductionIdScopeSpec.isInvalid())
10492     return ExprError();
10493   SmallVector<UnresolvedSet<8>, 4> Lookups;
10494   if (S) {
10495     LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10496     Lookup.suppressDiagnostics();
10497     while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
10498       NamedDecl *D = Lookup.getRepresentativeDecl();
10499       do {
10500         S = S->getParent();
10501       } while (S && !S->isDeclScope(D));
10502       if (S)
10503         S = S->getParent();
10504       Lookups.emplace_back();
10505       Lookups.back().append(Lookup.begin(), Lookup.end());
10506       Lookup.clear();
10507     }
10508   } else if (auto *ULE =
10509                  cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10510     Lookups.push_back(UnresolvedSet<8>());
10511     Decl *PrevD = nullptr;
10512     for (NamedDecl *D : ULE->decls()) {
10513       if (D == PrevD)
10514         Lookups.push_back(UnresolvedSet<8>());
10515       else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10516         Lookups.back().addDecl(DRD);
10517       PrevD = D;
10518     }
10519   }
10520   if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10521       Ty->isInstantiationDependentType() ||
10522       Ty->containsUnexpandedParameterPack() ||
10523       filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
10524         return !D->isInvalidDecl() &&
10525                (D->getType()->isDependentType() ||
10526                 D->getType()->isInstantiationDependentType() ||
10527                 D->getType()->containsUnexpandedParameterPack());
10528       })) {
10529     UnresolvedSet<8> ResSet;
10530     for (const UnresolvedSet<8> &Set : Lookups) {
10531       if (Set.empty())
10532         continue;
10533       ResSet.append(Set.begin(), Set.end());
10534       // The last item marks the end of all declarations at the specified scope.
10535       ResSet.addDecl(Set[Set.size() - 1]);
10536     }
10537     return UnresolvedLookupExpr::Create(
10538         SemaRef.Context, /*NamingClass=*/nullptr,
10539         ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10540         /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10541   }
10542   // Lookup inside the classes.
10543   // C++ [over.match.oper]p3:
10544   //   For a unary operator @ with an operand of a type whose
10545   //   cv-unqualified version is T1, and for a binary operator @ with
10546   //   a left operand of a type whose cv-unqualified version is T1 and
10547   //   a right operand of a type whose cv-unqualified version is T2,
10548   //   three sets of candidate functions, designated member
10549   //   candidates, non-member candidates and built-in candidates, are
10550   //   constructed as follows:
10551   //     -- If T1 is a complete class type or a class currently being
10552   //        defined, the set of member candidates is the result of the
10553   //        qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10554   //        the set of member candidates is empty.
10555   LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10556   Lookup.suppressDiagnostics();
10557   if (const auto *TyRec = Ty->getAs<RecordType>()) {
10558     // Complete the type if it can be completed.
10559     // If the type is neither complete nor being defined, bail out now.
10560     if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10561         TyRec->getDecl()->getDefinition()) {
10562       Lookup.clear();
10563       SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10564       if (Lookup.empty()) {
10565         Lookups.emplace_back();
10566         Lookups.back().append(Lookup.begin(), Lookup.end());
10567       }
10568     }
10569   }
10570   // Perform ADL.
10571   argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
10572   if (auto *VD = filterLookupForUDR<ValueDecl *>(
10573           Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10574             if (!D->isInvalidDecl() &&
10575                 SemaRef.Context.hasSameType(D->getType(), Ty))
10576               return D;
10577             return nullptr;
10578           }))
10579     return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10580   if (auto *VD = filterLookupForUDR<ValueDecl *>(
10581           Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10582             if (!D->isInvalidDecl() &&
10583                 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10584                 !Ty.isMoreQualifiedThan(D->getType()))
10585               return D;
10586             return nullptr;
10587           })) {
10588     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10589                        /*DetectVirtual=*/false);
10590     if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10591       if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10592               VD->getType().getUnqualifiedType()))) {
10593         if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10594                                          /*DiagID=*/0) !=
10595             Sema::AR_inaccessible) {
10596           SemaRef.BuildBasePathArray(Paths, BasePath);
10597           return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10598         }
10599       }
10600     }
10601   }
10602   if (ReductionIdScopeSpec.isSet()) {
10603     SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10604     return ExprError();
10605   }
10606   return ExprEmpty();
10607 }
10608 
10609 namespace {
10610 /// Data for the reduction-based clauses.
10611 struct ReductionData {
10612   /// List of original reduction items.
10613   SmallVector<Expr *, 8> Vars;
10614   /// List of private copies of the reduction items.
10615   SmallVector<Expr *, 8> Privates;
10616   /// LHS expressions for the reduction_op expressions.
10617   SmallVector<Expr *, 8> LHSs;
10618   /// RHS expressions for the reduction_op expressions.
10619   SmallVector<Expr *, 8> RHSs;
10620   /// Reduction operation expression.
10621   SmallVector<Expr *, 8> ReductionOps;
10622   /// Taskgroup descriptors for the corresponding reduction items in
10623   /// in_reduction clauses.
10624   SmallVector<Expr *, 8> TaskgroupDescriptors;
10625   /// List of captures for clause.
10626   SmallVector<Decl *, 4> ExprCaptures;
10627   /// List of postupdate expressions.
10628   SmallVector<Expr *, 4> ExprPostUpdates;
10629   ReductionData() = delete;
10630   /// Reserves required memory for the reduction data.
10631   ReductionData(unsigned Size) {
10632     Vars.reserve(Size);
10633     Privates.reserve(Size);
10634     LHSs.reserve(Size);
10635     RHSs.reserve(Size);
10636     ReductionOps.reserve(Size);
10637     TaskgroupDescriptors.reserve(Size);
10638     ExprCaptures.reserve(Size);
10639     ExprPostUpdates.reserve(Size);
10640   }
10641   /// Stores reduction item and reduction operation only (required for dependent
10642   /// reduction item).
10643   void push(Expr *Item, Expr *ReductionOp) {
10644     Vars.emplace_back(Item);
10645     Privates.emplace_back(nullptr);
10646     LHSs.emplace_back(nullptr);
10647     RHSs.emplace_back(nullptr);
10648     ReductionOps.emplace_back(ReductionOp);
10649     TaskgroupDescriptors.emplace_back(nullptr);
10650   }
10651   /// Stores reduction data.
10652   void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10653             Expr *TaskgroupDescriptor) {
10654     Vars.emplace_back(Item);
10655     Privates.emplace_back(Private);
10656     LHSs.emplace_back(LHS);
10657     RHSs.emplace_back(RHS);
10658     ReductionOps.emplace_back(ReductionOp);
10659     TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
10660   }
10661 };
10662 } // namespace
10663 
10664 static bool checkOMPArraySectionConstantForReduction(
10665     ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10666     SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10667   const Expr *Length = OASE->getLength();
10668   if (Length == nullptr) {
10669     // For array sections of the form [1:] or [:], we would need to analyze
10670     // the lower bound...
10671     if (OASE->getColonLoc().isValid())
10672       return false;
10673 
10674     // This is an array subscript which has implicit length 1!
10675     SingleElement = true;
10676     ArraySizes.push_back(llvm::APSInt::get(1));
10677   } else {
10678     Expr::EvalResult Result;
10679     if (!Length->EvaluateAsInt(Result, Context))
10680       return false;
10681 
10682     llvm::APSInt ConstantLengthValue = Result.Val.getInt();
10683     SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10684     ArraySizes.push_back(ConstantLengthValue);
10685   }
10686 
10687   // Get the base of this array section and walk up from there.
10688   const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10689 
10690   // We require length = 1 for all array sections except the right-most to
10691   // guarantee that the memory region is contiguous and has no holes in it.
10692   while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10693     Length = TempOASE->getLength();
10694     if (Length == nullptr) {
10695       // For array sections of the form [1:] or [:], we would need to analyze
10696       // the lower bound...
10697       if (OASE->getColonLoc().isValid())
10698         return false;
10699 
10700       // This is an array subscript which has implicit length 1!
10701       ArraySizes.push_back(llvm::APSInt::get(1));
10702     } else {
10703       Expr::EvalResult Result;
10704       if (!Length->EvaluateAsInt(Result, Context))
10705         return false;
10706 
10707       llvm::APSInt ConstantLengthValue = Result.Val.getInt();
10708       if (ConstantLengthValue.getSExtValue() != 1)
10709         return false;
10710 
10711       ArraySizes.push_back(ConstantLengthValue);
10712     }
10713     Base = TempOASE->getBase()->IgnoreParenImpCasts();
10714   }
10715 
10716   // If we have a single element, we don't need to add the implicit lengths.
10717   if (!SingleElement) {
10718     while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10719       // Has implicit length 1!
10720       ArraySizes.push_back(llvm::APSInt::get(1));
10721       Base = TempASE->getBase()->IgnoreParenImpCasts();
10722     }
10723   }
10724 
10725   // This array section can be privatized as a single value or as a constant
10726   // sized array.
10727   return true;
10728 }
10729 
10730 static bool actOnOMPReductionKindClause(
10731     Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10732     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10733     SourceLocation ColonLoc, SourceLocation EndLoc,
10734     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10735     ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
10736   DeclarationName DN = ReductionId.getName();
10737   OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
10738   BinaryOperatorKind BOK = BO_Comma;
10739 
10740   ASTContext &Context = S.Context;
10741   // OpenMP [2.14.3.6, reduction clause]
10742   // C
10743   // reduction-identifier is either an identifier or one of the following
10744   // operators: +, -, *,  &, |, ^, && and ||
10745   // C++
10746   // reduction-identifier is either an id-expression or one of the following
10747   // operators: +, -, *, &, |, ^, && and ||
10748   switch (OOK) {
10749   case OO_Plus:
10750   case OO_Minus:
10751     BOK = BO_Add;
10752     break;
10753   case OO_Star:
10754     BOK = BO_Mul;
10755     break;
10756   case OO_Amp:
10757     BOK = BO_And;
10758     break;
10759   case OO_Pipe:
10760     BOK = BO_Or;
10761     break;
10762   case OO_Caret:
10763     BOK = BO_Xor;
10764     break;
10765   case OO_AmpAmp:
10766     BOK = BO_LAnd;
10767     break;
10768   case OO_PipePipe:
10769     BOK = BO_LOr;
10770     break;
10771   case OO_New:
10772   case OO_Delete:
10773   case OO_Array_New:
10774   case OO_Array_Delete:
10775   case OO_Slash:
10776   case OO_Percent:
10777   case OO_Tilde:
10778   case OO_Exclaim:
10779   case OO_Equal:
10780   case OO_Less:
10781   case OO_Greater:
10782   case OO_LessEqual:
10783   case OO_GreaterEqual:
10784   case OO_PlusEqual:
10785   case OO_MinusEqual:
10786   case OO_StarEqual:
10787   case OO_SlashEqual:
10788   case OO_PercentEqual:
10789   case OO_CaretEqual:
10790   case OO_AmpEqual:
10791   case OO_PipeEqual:
10792   case OO_LessLess:
10793   case OO_GreaterGreater:
10794   case OO_LessLessEqual:
10795   case OO_GreaterGreaterEqual:
10796   case OO_EqualEqual:
10797   case OO_ExclaimEqual:
10798   case OO_Spaceship:
10799   case OO_PlusPlus:
10800   case OO_MinusMinus:
10801   case OO_Comma:
10802   case OO_ArrowStar:
10803   case OO_Arrow:
10804   case OO_Call:
10805   case OO_Subscript:
10806   case OO_Conditional:
10807   case OO_Coawait:
10808   case NUM_OVERLOADED_OPERATORS:
10809     llvm_unreachable("Unexpected reduction identifier");
10810   case OO_None:
10811     if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
10812       if (II->isStr("max"))
10813         BOK = BO_GT;
10814       else if (II->isStr("min"))
10815         BOK = BO_LT;
10816     }
10817     break;
10818   }
10819   SourceRange ReductionIdRange;
10820   if (ReductionIdScopeSpec.isValid())
10821     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
10822   else
10823     ReductionIdRange.setBegin(ReductionId.getBeginLoc());
10824   ReductionIdRange.setEnd(ReductionId.getEndLoc());
10825 
10826   auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10827   bool FirstIter = true;
10828   for (Expr *RefExpr : VarList) {
10829     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
10830     // OpenMP [2.1, C/C++]
10831     //  A list item is a variable or array section, subject to the restrictions
10832     //  specified in Section 2.4 on page 42 and in each of the sections
10833     // describing clauses and directives for which a list appears.
10834     // OpenMP  [2.14.3.3, Restrictions, p.1]
10835     //  A variable that is part of another variable (as an array or
10836     //  structure element) cannot appear in a private clause.
10837     if (!FirstIter && IR != ER)
10838       ++IR;
10839     FirstIter = false;
10840     SourceLocation ELoc;
10841     SourceRange ERange;
10842     Expr *SimpleRefExpr = RefExpr;
10843     auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
10844                               /*AllowArraySection=*/true);
10845     if (Res.second) {
10846       // Try to find 'declare reduction' corresponding construct before using
10847       // builtin/overloaded operators.
10848       QualType Type = Context.DependentTy;
10849       CXXCastPath BasePath;
10850       ExprResult DeclareReductionRef = buildDeclareReductionRef(
10851           S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
10852           ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10853       Expr *ReductionOp = nullptr;
10854       if (S.CurContext->isDependentContext() &&
10855           (DeclareReductionRef.isUnset() ||
10856            isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
10857         ReductionOp = DeclareReductionRef.get();
10858       // It will be analyzed later.
10859       RD.push(RefExpr, ReductionOp);
10860     }
10861     ValueDecl *D = Res.first;
10862     if (!D)
10863       continue;
10864 
10865     Expr *TaskgroupDescriptor = nullptr;
10866     QualType Type;
10867     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10868     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
10869     if (ASE) {
10870       Type = ASE->getType().getNonReferenceType();
10871     } else if (OASE) {
10872       QualType BaseType =
10873           OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10874       if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
10875         Type = ATy->getElementType();
10876       else
10877         Type = BaseType->getPointeeType();
10878       Type = Type.getNonReferenceType();
10879     } else {
10880       Type = Context.getBaseElementType(D->getType().getNonReferenceType());
10881     }
10882     auto *VD = dyn_cast<VarDecl>(D);
10883 
10884     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10885     //  A variable that appears in a private clause must not have an incomplete
10886     //  type or a reference type.
10887     if (S.RequireCompleteType(ELoc, D->getType(),
10888                               diag::err_omp_reduction_incomplete_type))
10889       continue;
10890     // OpenMP [2.14.3.6, reduction clause, Restrictions]
10891     // A list item that appears in a reduction clause must not be
10892     // const-qualified.
10893     if (Type.getNonReferenceType().isConstant(Context)) {
10894       S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
10895       if (!ASE && !OASE) {
10896         bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10897                                  VarDecl::DeclarationOnly;
10898         S.Diag(D->getLocation(),
10899                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10900             << D;
10901       }
10902       continue;
10903     }
10904 
10905     OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
10906     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10907     //  If a list-item is a reference type then it must bind to the same object
10908     //  for all threads of the team.
10909     if (!ASE && !OASE) {
10910       if (VD) {
10911         VarDecl *VDDef = VD->getDefinition();
10912         if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
10913           DSARefChecker Check(Stack);
10914           if (Check.Visit(VDDef->getInit())) {
10915             S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10916                 << getOpenMPClauseName(ClauseKind) << ERange;
10917             S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
10918             continue;
10919           }
10920         }
10921       }
10922 
10923       // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10924       // in a Construct]
10925       //  Variables with the predetermined data-sharing attributes may not be
10926       //  listed in data-sharing attributes clauses, except for the cases
10927       //  listed below. For these exceptions only, listing a predetermined
10928       //  variable in a data-sharing attribute clause is allowed and overrides
10929       //  the variable's predetermined data-sharing attributes.
10930       // OpenMP [2.14.3.6, Restrictions, p.3]
10931       //  Any number of reduction clauses can be specified on the directive,
10932       //  but a list item can appear only once in the reduction clauses for that
10933       //  directive.
10934       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
10935       if (DVar.CKind == OMPC_reduction) {
10936         S.Diag(ELoc, diag::err_omp_once_referenced)
10937             << getOpenMPClauseName(ClauseKind);
10938         if (DVar.RefExpr)
10939           S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
10940         continue;
10941       }
10942       if (DVar.CKind != OMPC_unknown) {
10943         S.Diag(ELoc, diag::err_omp_wrong_dsa)
10944             << getOpenMPClauseName(DVar.CKind)
10945             << getOpenMPClauseName(OMPC_reduction);
10946         reportOriginalDsa(S, Stack, D, DVar);
10947         continue;
10948       }
10949 
10950       // OpenMP [2.14.3.6, Restrictions, p.1]
10951       //  A list item that appears in a reduction clause of a worksharing
10952       //  construct must be shared in the parallel regions to which any of the
10953       //  worksharing regions arising from the worksharing construct bind.
10954       if (isOpenMPWorksharingDirective(CurrDir) &&
10955           !isOpenMPParallelDirective(CurrDir) &&
10956           !isOpenMPTeamsDirective(CurrDir)) {
10957         DVar = Stack->getImplicitDSA(D, true);
10958         if (DVar.CKind != OMPC_shared) {
10959           S.Diag(ELoc, diag::err_omp_required_access)
10960               << getOpenMPClauseName(OMPC_reduction)
10961               << getOpenMPClauseName(OMPC_shared);
10962           reportOriginalDsa(S, Stack, D, DVar);
10963           continue;
10964         }
10965       }
10966     }
10967 
10968     // Try to find 'declare reduction' corresponding construct before using
10969     // builtin/overloaded operators.
10970     CXXCastPath BasePath;
10971     ExprResult DeclareReductionRef = buildDeclareReductionRef(
10972         S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
10973         ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10974     if (DeclareReductionRef.isInvalid())
10975       continue;
10976     if (S.CurContext->isDependentContext() &&
10977         (DeclareReductionRef.isUnset() ||
10978          isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
10979       RD.push(RefExpr, DeclareReductionRef.get());
10980       continue;
10981     }
10982     if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10983       // Not allowed reduction identifier is found.
10984       S.Diag(ReductionId.getBeginLoc(),
10985              diag::err_omp_unknown_reduction_identifier)
10986           << Type << ReductionIdRange;
10987       continue;
10988     }
10989 
10990     // OpenMP [2.14.3.6, reduction clause, Restrictions]
10991     // The type of a list item that appears in a reduction clause must be valid
10992     // for the reduction-identifier. For a max or min reduction in C, the type
10993     // of the list item must be an allowed arithmetic data type: char, int,
10994     // float, double, or _Bool, possibly modified with long, short, signed, or
10995     // unsigned. For a max or min reduction in C++, the type of the list item
10996     // must be an allowed arithmetic data type: char, wchar_t, int, float,
10997     // double, or bool, possibly modified with long, short, signed, or unsigned.
10998     if (DeclareReductionRef.isUnset()) {
10999       if ((BOK == BO_GT || BOK == BO_LT) &&
11000           !(Type->isScalarType() ||
11001             (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
11002         S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
11003             << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
11004         if (!ASE && !OASE) {
11005           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11006                                    VarDecl::DeclarationOnly;
11007           S.Diag(D->getLocation(),
11008                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11009               << D;
11010         }
11011         continue;
11012       }
11013       if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
11014           !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
11015         S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
11016             << getOpenMPClauseName(ClauseKind);
11017         if (!ASE && !OASE) {
11018           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11019                                    VarDecl::DeclarationOnly;
11020           S.Diag(D->getLocation(),
11021                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11022               << D;
11023         }
11024         continue;
11025       }
11026     }
11027 
11028     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
11029     VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
11030                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
11031     VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
11032                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
11033     QualType PrivateTy = Type;
11034 
11035     // Try if we can determine constant lengths for all array sections and avoid
11036     // the VLA.
11037     bool ConstantLengthOASE = false;
11038     if (OASE) {
11039       bool SingleElement;
11040       llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
11041       ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
11042           Context, OASE, SingleElement, ArraySizes);
11043 
11044       // If we don't have a single element, we must emit a constant array type.
11045       if (ConstantLengthOASE && !SingleElement) {
11046         for (llvm::APSInt &Size : ArraySizes)
11047           PrivateTy = Context.getConstantArrayType(
11048               PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
11049       }
11050     }
11051 
11052     if ((OASE && !ConstantLengthOASE) ||
11053         (!OASE && !ASE &&
11054          D->getType().getNonReferenceType()->isVariablyModifiedType())) {
11055       if (!Context.getTargetInfo().isVLASupported() &&
11056           S.shouldDiagnoseTargetSupportFromOpenMP()) {
11057         S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
11058         S.Diag(ELoc, diag::note_vla_unsupported);
11059         continue;
11060       }
11061       // For arrays/array sections only:
11062       // Create pseudo array type for private copy. The size for this array will
11063       // be generated during codegen.
11064       // For array subscripts or single variables Private Ty is the same as Type
11065       // (type of the variable or single array element).
11066       PrivateTy = Context.getVariableArrayType(
11067           Type,
11068           new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
11069           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
11070     } else if (!ASE && !OASE &&
11071                Context.getAsArrayType(D->getType().getNonReferenceType())) {
11072       PrivateTy = D->getType().getNonReferenceType();
11073     }
11074     // Private copy.
11075     VarDecl *PrivateVD =
11076         buildVarDecl(S, ELoc, PrivateTy, D->getName(),
11077                      D->hasAttrs() ? &D->getAttrs() : nullptr,
11078                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
11079     // Add initializer for private variable.
11080     Expr *Init = nullptr;
11081     DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
11082     DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
11083     if (DeclareReductionRef.isUsable()) {
11084       auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
11085       auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
11086       if (DRD->getInitializer()) {
11087         Init = DRDRef;
11088         RHSVD->setInit(DRDRef);
11089         RHSVD->setInitStyle(VarDecl::CallInit);
11090       }
11091     } else {
11092       switch (BOK) {
11093       case BO_Add:
11094       case BO_Xor:
11095       case BO_Or:
11096       case BO_LOr:
11097         // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
11098         if (Type->isScalarType() || Type->isAnyComplexType())
11099           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
11100         break;
11101       case BO_Mul:
11102       case BO_LAnd:
11103         if (Type->isScalarType() || Type->isAnyComplexType()) {
11104           // '*' and '&&' reduction ops - initializer is '1'.
11105           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
11106         }
11107         break;
11108       case BO_And: {
11109         // '&' reduction op - initializer is '~0'.
11110         QualType OrigType = Type;
11111         if (auto *ComplexTy = OrigType->getAs<ComplexType>())
11112           Type = ComplexTy->getElementType();
11113         if (Type->isRealFloatingType()) {
11114           llvm::APFloat InitValue =
11115               llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
11116                                              /*isIEEE=*/true);
11117           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11118                                          Type, ELoc);
11119         } else if (Type->isScalarType()) {
11120           uint64_t Size = Context.getTypeSize(Type);
11121           QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
11122           llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
11123           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11124         }
11125         if (Init && OrigType->isAnyComplexType()) {
11126           // Init = 0xFFFF + 0xFFFFi;
11127           auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
11128           Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
11129         }
11130         Type = OrigType;
11131         break;
11132       }
11133       case BO_LT:
11134       case BO_GT: {
11135         // 'min' reduction op - initializer is 'Largest representable number in
11136         // the reduction list item type'.
11137         // 'max' reduction op - initializer is 'Least representable number in
11138         // the reduction list item type'.
11139         if (Type->isIntegerType() || Type->isPointerType()) {
11140           bool IsSigned = Type->hasSignedIntegerRepresentation();
11141           uint64_t Size = Context.getTypeSize(Type);
11142           QualType IntTy =
11143               Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
11144           llvm::APInt InitValue =
11145               (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
11146                                         : llvm::APInt::getMinValue(Size)
11147                              : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
11148                                         : llvm::APInt::getMaxValue(Size);
11149           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11150           if (Type->isPointerType()) {
11151             // Cast to pointer type.
11152             ExprResult CastExpr = S.BuildCStyleCastExpr(
11153                 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
11154             if (CastExpr.isInvalid())
11155               continue;
11156             Init = CastExpr.get();
11157           }
11158         } else if (Type->isRealFloatingType()) {
11159           llvm::APFloat InitValue = llvm::APFloat::getLargest(
11160               Context.getFloatTypeSemantics(Type), BOK != BO_LT);
11161           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11162                                          Type, ELoc);
11163         }
11164         break;
11165       }
11166       case BO_PtrMemD:
11167       case BO_PtrMemI:
11168       case BO_MulAssign:
11169       case BO_Div:
11170       case BO_Rem:
11171       case BO_Sub:
11172       case BO_Shl:
11173       case BO_Shr:
11174       case BO_LE:
11175       case BO_GE:
11176       case BO_EQ:
11177       case BO_NE:
11178       case BO_Cmp:
11179       case BO_AndAssign:
11180       case BO_XorAssign:
11181       case BO_OrAssign:
11182       case BO_Assign:
11183       case BO_AddAssign:
11184       case BO_SubAssign:
11185       case BO_DivAssign:
11186       case BO_RemAssign:
11187       case BO_ShlAssign:
11188       case BO_ShrAssign:
11189       case BO_Comma:
11190         llvm_unreachable("Unexpected reduction operation");
11191       }
11192     }
11193     if (Init && DeclareReductionRef.isUnset())
11194       S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
11195     else if (!Init)
11196       S.ActOnUninitializedDecl(RHSVD);
11197     if (RHSVD->isInvalidDecl())
11198       continue;
11199     if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
11200       S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
11201           << Type << ReductionIdRange;
11202       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11203                                VarDecl::DeclarationOnly;
11204       S.Diag(D->getLocation(),
11205              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11206           << D;
11207       continue;
11208     }
11209     // Store initializer for single element in private copy. Will be used during
11210     // codegen.
11211     PrivateVD->setInit(RHSVD->getInit());
11212     PrivateVD->setInitStyle(RHSVD->getInitStyle());
11213     DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
11214     ExprResult ReductionOp;
11215     if (DeclareReductionRef.isUsable()) {
11216       QualType RedTy = DeclareReductionRef.get()->getType();
11217       QualType PtrRedTy = Context.getPointerType(RedTy);
11218       ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
11219       ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
11220       if (!BasePath.empty()) {
11221         LHS = S.DefaultLvalueConversion(LHS.get());
11222         RHS = S.DefaultLvalueConversion(RHS.get());
11223         LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11224                                        CK_UncheckedDerivedToBase, LHS.get(),
11225                                        &BasePath, LHS.get()->getValueKind());
11226         RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11227                                        CK_UncheckedDerivedToBase, RHS.get(),
11228                                        &BasePath, RHS.get()->getValueKind());
11229       }
11230       FunctionProtoType::ExtProtoInfo EPI;
11231       QualType Params[] = {PtrRedTy, PtrRedTy};
11232       QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
11233       auto *OVE = new (Context) OpaqueValueExpr(
11234           ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
11235           S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
11236       Expr *Args[] = {LHS.get(), RHS.get()};
11237       ReductionOp = new (Context)
11238           CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
11239     } else {
11240       ReductionOp = S.BuildBinOp(
11241           Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
11242       if (ReductionOp.isUsable()) {
11243         if (BOK != BO_LT && BOK != BO_GT) {
11244           ReductionOp =
11245               S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
11246                            BO_Assign, LHSDRE, ReductionOp.get());
11247         } else {
11248           auto *ConditionalOp = new (Context)
11249               ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
11250                                   Type, VK_LValue, OK_Ordinary);
11251           ReductionOp =
11252               S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
11253                            BO_Assign, LHSDRE, ConditionalOp);
11254         }
11255         if (ReductionOp.isUsable())
11256           ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
11257       }
11258       if (!ReductionOp.isUsable())
11259         continue;
11260     }
11261 
11262     // OpenMP [2.15.4.6, Restrictions, p.2]
11263     // A list item that appears in an in_reduction clause of a task construct
11264     // must appear in a task_reduction clause of a construct associated with a
11265     // taskgroup region that includes the participating task in its taskgroup
11266     // set. The construct associated with the innermost region that meets this
11267     // condition must specify the same reduction-identifier as the in_reduction
11268     // clause.
11269     if (ClauseKind == OMPC_in_reduction) {
11270       SourceRange ParentSR;
11271       BinaryOperatorKind ParentBOK;
11272       const Expr *ParentReductionOp;
11273       Expr *ParentBOKTD, *ParentReductionOpTD;
11274       DSAStackTy::DSAVarData ParentBOKDSA =
11275           Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11276                                                   ParentBOKTD);
11277       DSAStackTy::DSAVarData ParentReductionOpDSA =
11278           Stack->getTopMostTaskgroupReductionData(
11279               D, ParentSR, ParentReductionOp, ParentReductionOpTD);
11280       bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11281       bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11282       if (!IsParentBOK && !IsParentReductionOp) {
11283         S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11284         continue;
11285       }
11286       if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11287           (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11288           IsParentReductionOp) {
11289         bool EmitError = true;
11290         if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11291           llvm::FoldingSetNodeID RedId, ParentRedId;
11292           ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11293           DeclareReductionRef.get()->Profile(RedId, Context,
11294                                              /*Canonical=*/true);
11295           EmitError = RedId != ParentRedId;
11296         }
11297         if (EmitError) {
11298           S.Diag(ReductionId.getBeginLoc(),
11299                  diag::err_omp_reduction_identifier_mismatch)
11300               << ReductionIdRange << RefExpr->getSourceRange();
11301           S.Diag(ParentSR.getBegin(),
11302                  diag::note_omp_previous_reduction_identifier)
11303               << ParentSR
11304               << (IsParentBOK ? ParentBOKDSA.RefExpr
11305                               : ParentReductionOpDSA.RefExpr)
11306                      ->getSourceRange();
11307           continue;
11308         }
11309       }
11310       TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11311       assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
11312     }
11313 
11314     DeclRefExpr *Ref = nullptr;
11315     Expr *VarsExpr = RefExpr->IgnoreParens();
11316     if (!VD && !S.CurContext->isDependentContext()) {
11317       if (ASE || OASE) {
11318         TransformExprToCaptures RebuildToCapture(S, D);
11319         VarsExpr =
11320             RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11321         Ref = RebuildToCapture.getCapturedExpr();
11322       } else {
11323         VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
11324       }
11325       if (!S.isOpenMPCapturedDecl(D)) {
11326         RD.ExprCaptures.emplace_back(Ref->getDecl());
11327         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11328           ExprResult RefRes = S.DefaultLvalueConversion(Ref);
11329           if (!RefRes.isUsable())
11330             continue;
11331           ExprResult PostUpdateRes =
11332               S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11333                            RefRes.get());
11334           if (!PostUpdateRes.isUsable())
11335             continue;
11336           if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11337               Stack->getCurrentDirective() == OMPD_taskgroup) {
11338             S.Diag(RefExpr->getExprLoc(),
11339                    diag::err_omp_reduction_non_addressable_expression)
11340                 << RefExpr->getSourceRange();
11341             continue;
11342           }
11343           RD.ExprPostUpdates.emplace_back(
11344               S.IgnoredValueConversions(PostUpdateRes.get()).get());
11345         }
11346       }
11347     }
11348     // All reduction items are still marked as reduction (to do not increase
11349     // code base size).
11350     Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
11351     if (CurrDir == OMPD_taskgroup) {
11352       if (DeclareReductionRef.isUsable())
11353         Stack->addTaskgroupReductionData(D, ReductionIdRange,
11354                                          DeclareReductionRef.get());
11355       else
11356         Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
11357     }
11358     RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11359             TaskgroupDescriptor);
11360   }
11361   return RD.Vars.empty();
11362 }
11363 
11364 OMPClause *Sema::ActOnOpenMPReductionClause(
11365     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11366     SourceLocation ColonLoc, SourceLocation EndLoc,
11367     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11368     ArrayRef<Expr *> UnresolvedReductions) {
11369   ReductionData RD(VarList.size());
11370   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
11371                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
11372                                   ReductionIdScopeSpec, ReductionId,
11373                                   UnresolvedReductions, RD))
11374     return nullptr;
11375 
11376   return OMPReductionClause::Create(
11377       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11378       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11379       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11380       buildPreInits(Context, RD.ExprCaptures),
11381       buildPostUpdate(*this, RD.ExprPostUpdates));
11382 }
11383 
11384 OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11385     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11386     SourceLocation ColonLoc, SourceLocation EndLoc,
11387     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11388     ArrayRef<Expr *> UnresolvedReductions) {
11389   ReductionData RD(VarList.size());
11390   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11391                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
11392                                   ReductionIdScopeSpec, ReductionId,
11393                                   UnresolvedReductions, RD))
11394     return nullptr;
11395 
11396   return OMPTaskReductionClause::Create(
11397       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11398       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11399       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11400       buildPreInits(Context, RD.ExprCaptures),
11401       buildPostUpdate(*this, RD.ExprPostUpdates));
11402 }
11403 
11404 OMPClause *Sema::ActOnOpenMPInReductionClause(
11405     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11406     SourceLocation ColonLoc, SourceLocation EndLoc,
11407     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11408     ArrayRef<Expr *> UnresolvedReductions) {
11409   ReductionData RD(VarList.size());
11410   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
11411                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
11412                                   ReductionIdScopeSpec, ReductionId,
11413                                   UnresolvedReductions, RD))
11414     return nullptr;
11415 
11416   return OMPInReductionClause::Create(
11417       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11418       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11419       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
11420       buildPreInits(Context, RD.ExprCaptures),
11421       buildPostUpdate(*this, RD.ExprPostUpdates));
11422 }
11423 
11424 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11425                                      SourceLocation LinLoc) {
11426   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11427       LinKind == OMPC_LINEAR_unknown) {
11428     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11429     return true;
11430   }
11431   return false;
11432 }
11433 
11434 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
11435                                  OpenMPLinearClauseKind LinKind,
11436                                  QualType Type) {
11437   const auto *VD = dyn_cast_or_null<VarDecl>(D);
11438   // A variable must not have an incomplete type or a reference type.
11439   if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11440     return true;
11441   if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11442       !Type->isReferenceType()) {
11443     Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11444         << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11445     return true;
11446   }
11447   Type = Type.getNonReferenceType();
11448 
11449   // A list item must not be const-qualified.
11450   if (Type.isConstant(Context)) {
11451     Diag(ELoc, diag::err_omp_const_variable)
11452         << getOpenMPClauseName(OMPC_linear);
11453     if (D) {
11454       bool IsDecl =
11455           !VD ||
11456           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11457       Diag(D->getLocation(),
11458            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11459           << D;
11460     }
11461     return true;
11462   }
11463 
11464   // A list item must be of integral or pointer type.
11465   Type = Type.getUnqualifiedType().getCanonicalType();
11466   const auto *Ty = Type.getTypePtrOrNull();
11467   if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11468               !Ty->isPointerType())) {
11469     Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11470     if (D) {
11471       bool IsDecl =
11472           !VD ||
11473           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11474       Diag(D->getLocation(),
11475            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11476           << D;
11477     }
11478     return true;
11479   }
11480   return false;
11481 }
11482 
11483 OMPClause *Sema::ActOnOpenMPLinearClause(
11484     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11485     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11486     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
11487   SmallVector<Expr *, 8> Vars;
11488   SmallVector<Expr *, 8> Privates;
11489   SmallVector<Expr *, 8> Inits;
11490   SmallVector<Decl *, 4> ExprCaptures;
11491   SmallVector<Expr *, 4> ExprPostUpdates;
11492   if (CheckOpenMPLinearModifier(LinKind, LinLoc))
11493     LinKind = OMPC_LINEAR_val;
11494   for (Expr *RefExpr : VarList) {
11495     assert(RefExpr && "NULL expr in OpenMP linear clause.");
11496     SourceLocation ELoc;
11497     SourceRange ERange;
11498     Expr *SimpleRefExpr = RefExpr;
11499     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11500     if (Res.second) {
11501       // It will be analyzed later.
11502       Vars.push_back(RefExpr);
11503       Privates.push_back(nullptr);
11504       Inits.push_back(nullptr);
11505     }
11506     ValueDecl *D = Res.first;
11507     if (!D)
11508       continue;
11509 
11510     QualType Type = D->getType();
11511     auto *VD = dyn_cast<VarDecl>(D);
11512 
11513     // OpenMP [2.14.3.7, linear clause]
11514     //  A list-item cannot appear in more than one linear clause.
11515     //  A list-item that appears in a linear clause cannot appear in any
11516     //  other data-sharing attribute clause.
11517     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
11518     if (DVar.RefExpr) {
11519       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11520                                           << getOpenMPClauseName(OMPC_linear);
11521       reportOriginalDsa(*this, DSAStack, D, DVar);
11522       continue;
11523     }
11524 
11525     if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
11526       continue;
11527     Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
11528 
11529     // Build private copy of original var.
11530     VarDecl *Private =
11531         buildVarDecl(*this, ELoc, Type, D->getName(),
11532                      D->hasAttrs() ? &D->getAttrs() : nullptr,
11533                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
11534     DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
11535     // Build var to save initial value.
11536     VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
11537     Expr *InitExpr;
11538     DeclRefExpr *Ref = nullptr;
11539     if (!VD && !CurContext->isDependentContext()) {
11540       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
11541       if (!isOpenMPCapturedDecl(D)) {
11542         ExprCaptures.push_back(Ref->getDecl());
11543         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11544           ExprResult RefRes = DefaultLvalueConversion(Ref);
11545           if (!RefRes.isUsable())
11546             continue;
11547           ExprResult PostUpdateRes =
11548               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11549                          SimpleRefExpr, RefRes.get());
11550           if (!PostUpdateRes.isUsable())
11551             continue;
11552           ExprPostUpdates.push_back(
11553               IgnoredValueConversions(PostUpdateRes.get()).get());
11554         }
11555       }
11556     }
11557     if (LinKind == OMPC_LINEAR_uval)
11558       InitExpr = VD ? VD->getInit() : SimpleRefExpr;
11559     else
11560       InitExpr = VD ? SimpleRefExpr : Ref;
11561     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
11562                          /*DirectInit=*/false);
11563     DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
11564 
11565     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
11566     Vars.push_back((VD || CurContext->isDependentContext())
11567                        ? RefExpr->IgnoreParens()
11568                        : Ref);
11569     Privates.push_back(PrivateRef);
11570     Inits.push_back(InitRef);
11571   }
11572 
11573   if (Vars.empty())
11574     return nullptr;
11575 
11576   Expr *StepExpr = Step;
11577   Expr *CalcStepExpr = nullptr;
11578   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11579       !Step->isInstantiationDependent() &&
11580       !Step->containsUnexpandedParameterPack()) {
11581     SourceLocation StepLoc = Step->getBeginLoc();
11582     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
11583     if (Val.isInvalid())
11584       return nullptr;
11585     StepExpr = Val.get();
11586 
11587     // Build var to save the step value.
11588     VarDecl *SaveVar =
11589         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
11590     ExprResult SaveRef =
11591         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
11592     ExprResult CalcStep =
11593         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
11594     CalcStep = ActOnFinishFullExpr(CalcStep.get());
11595 
11596     // Warn about zero linear step (it would be probably better specified as
11597     // making corresponding variables 'const').
11598     llvm::APSInt Result;
11599     bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11600     if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
11601       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11602                                                      << (Vars.size() > 1);
11603     if (!IsConstant && CalcStep.isUsable()) {
11604       // Calculate the step beforehand instead of doing this on each iteration.
11605       // (This is not used if the number of iterations may be kfold-ed).
11606       CalcStepExpr = CalcStep.get();
11607     }
11608   }
11609 
11610   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11611                                  ColonLoc, EndLoc, Vars, Privates, Inits,
11612                                  StepExpr, CalcStepExpr,
11613                                  buildPreInits(Context, ExprCaptures),
11614                                  buildPostUpdate(*this, ExprPostUpdates));
11615 }
11616 
11617 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11618                                      Expr *NumIterations, Sema &SemaRef,
11619                                      Scope *S, DSAStackTy *Stack) {
11620   // Walk the vars and build update/final expressions for the CodeGen.
11621   SmallVector<Expr *, 8> Updates;
11622   SmallVector<Expr *, 8> Finals;
11623   Expr *Step = Clause.getStep();
11624   Expr *CalcStep = Clause.getCalcStep();
11625   // OpenMP [2.14.3.7, linear clause]
11626   // If linear-step is not specified it is assumed to be 1.
11627   if (!Step)
11628     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
11629   else if (CalcStep)
11630     Step = cast<BinaryOperator>(CalcStep)->getLHS();
11631   bool HasErrors = false;
11632   auto CurInit = Clause.inits().begin();
11633   auto CurPrivate = Clause.privates().begin();
11634   OpenMPLinearClauseKind LinKind = Clause.getModifier();
11635   for (Expr *RefExpr : Clause.varlists()) {
11636     SourceLocation ELoc;
11637     SourceRange ERange;
11638     Expr *SimpleRefExpr = RefExpr;
11639     auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
11640     ValueDecl *D = Res.first;
11641     if (Res.second || !D) {
11642       Updates.push_back(nullptr);
11643       Finals.push_back(nullptr);
11644       HasErrors = true;
11645       continue;
11646     }
11647     auto &&Info = Stack->isLoopControlVariable(D);
11648     // OpenMP [2.15.11, distribute simd Construct]
11649     // A list item may not appear in a linear clause, unless it is the loop
11650     // iteration variable.
11651     if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11652         isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11653       SemaRef.Diag(ELoc,
11654                    diag::err_omp_linear_distribute_var_non_loop_iteration);
11655       Updates.push_back(nullptr);
11656       Finals.push_back(nullptr);
11657       HasErrors = true;
11658       continue;
11659     }
11660     Expr *InitExpr = *CurInit;
11661 
11662     // Build privatized reference to the current linear var.
11663     auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
11664     Expr *CapturedRef;
11665     if (LinKind == OMPC_LINEAR_uval)
11666       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11667     else
11668       CapturedRef =
11669           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11670                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11671                            /*RefersToCapture=*/true);
11672 
11673     // Build update: Var = InitExpr + IV * Step
11674     ExprResult Update;
11675     if (!Info.first)
11676       Update =
11677           buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
11678                              InitExpr, IV, Step, /* Subtract */ false);
11679     else
11680       Update = *CurPrivate;
11681     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
11682                                          /*DiscardedValue=*/true);
11683 
11684     // Build final: Var = InitExpr + NumIterations * Step
11685     ExprResult Final;
11686     if (!Info.first)
11687       Final =
11688           buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11689                              InitExpr, NumIterations, Step, /*Subtract=*/false);
11690     else
11691       Final = *CurPrivate;
11692     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
11693                                         /*DiscardedValue=*/true);
11694 
11695     if (!Update.isUsable() || !Final.isUsable()) {
11696       Updates.push_back(nullptr);
11697       Finals.push_back(nullptr);
11698       HasErrors = true;
11699     } else {
11700       Updates.push_back(Update.get());
11701       Finals.push_back(Final.get());
11702     }
11703     ++CurInit;
11704     ++CurPrivate;
11705   }
11706   Clause.setUpdates(Updates);
11707   Clause.setFinals(Finals);
11708   return HasErrors;
11709 }
11710 
11711 OMPClause *Sema::ActOnOpenMPAlignedClause(
11712     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11713     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
11714   SmallVector<Expr *, 8> Vars;
11715   for (Expr *RefExpr : VarList) {
11716     assert(RefExpr && "NULL expr in OpenMP linear clause.");
11717     SourceLocation ELoc;
11718     SourceRange ERange;
11719     Expr *SimpleRefExpr = RefExpr;
11720     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11721     if (Res.second) {
11722       // It will be analyzed later.
11723       Vars.push_back(RefExpr);
11724     }
11725     ValueDecl *D = Res.first;
11726     if (!D)
11727       continue;
11728 
11729     QualType QType = D->getType();
11730     auto *VD = dyn_cast<VarDecl>(D);
11731 
11732     // OpenMP  [2.8.1, simd construct, Restrictions]
11733     // The type of list items appearing in the aligned clause must be
11734     // array, pointer, reference to array, or reference to pointer.
11735     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
11736     const Type *Ty = QType.getTypePtrOrNull();
11737     if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
11738       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
11739           << QType << getLangOpts().CPlusPlus << ERange;
11740       bool IsDecl =
11741           !VD ||
11742           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11743       Diag(D->getLocation(),
11744            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11745           << D;
11746       continue;
11747     }
11748 
11749     // OpenMP  [2.8.1, simd construct, Restrictions]
11750     // A list-item cannot appear in more than one aligned clause.
11751     if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
11752       Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
11753       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11754           << getOpenMPClauseName(OMPC_aligned);
11755       continue;
11756     }
11757 
11758     DeclRefExpr *Ref = nullptr;
11759     if (!VD && isOpenMPCapturedDecl(D))
11760       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11761     Vars.push_back(DefaultFunctionArrayConversion(
11762                        (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11763                        .get());
11764   }
11765 
11766   // OpenMP [2.8.1, simd construct, Description]
11767   // The parameter of the aligned clause, alignment, must be a constant
11768   // positive integer expression.
11769   // If no optional parameter is specified, implementation-defined default
11770   // alignments for SIMD instructions on the target platforms are assumed.
11771   if (Alignment != nullptr) {
11772     ExprResult AlignResult =
11773         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11774     if (AlignResult.isInvalid())
11775       return nullptr;
11776     Alignment = AlignResult.get();
11777   }
11778   if (Vars.empty())
11779     return nullptr;
11780 
11781   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11782                                   EndLoc, Vars, Alignment);
11783 }
11784 
11785 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11786                                          SourceLocation StartLoc,
11787                                          SourceLocation LParenLoc,
11788                                          SourceLocation EndLoc) {
11789   SmallVector<Expr *, 8> Vars;
11790   SmallVector<Expr *, 8> SrcExprs;
11791   SmallVector<Expr *, 8> DstExprs;
11792   SmallVector<Expr *, 8> AssignmentOps;
11793   for (Expr *RefExpr : VarList) {
11794     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11795     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
11796       // It will be analyzed later.
11797       Vars.push_back(RefExpr);
11798       SrcExprs.push_back(nullptr);
11799       DstExprs.push_back(nullptr);
11800       AssignmentOps.push_back(nullptr);
11801       continue;
11802     }
11803 
11804     SourceLocation ELoc = RefExpr->getExprLoc();
11805     // OpenMP [2.1, C/C++]
11806     //  A list item is a variable name.
11807     // OpenMP  [2.14.4.1, Restrictions, p.1]
11808     //  A list item that appears in a copyin clause must be threadprivate.
11809     auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
11810     if (!DE || !isa<VarDecl>(DE->getDecl())) {
11811       Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11812           << 0 << RefExpr->getSourceRange();
11813       continue;
11814     }
11815 
11816     Decl *D = DE->getDecl();
11817     auto *VD = cast<VarDecl>(D);
11818 
11819     QualType Type = VD->getType();
11820     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11821       // It will be analyzed later.
11822       Vars.push_back(DE);
11823       SrcExprs.push_back(nullptr);
11824       DstExprs.push_back(nullptr);
11825       AssignmentOps.push_back(nullptr);
11826       continue;
11827     }
11828 
11829     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11830     //  A list item that appears in a copyin clause must be threadprivate.
11831     if (!DSAStack->isThreadPrivate(VD)) {
11832       Diag(ELoc, diag::err_omp_required_access)
11833           << getOpenMPClauseName(OMPC_copyin)
11834           << getOpenMPDirectiveName(OMPD_threadprivate);
11835       continue;
11836     }
11837 
11838     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11839     //  A variable of class type (or array thereof) that appears in a
11840     //  copyin clause requires an accessible, unambiguous copy assignment
11841     //  operator for the class type.
11842     QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11843     VarDecl *SrcVD =
11844         buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
11845                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
11846     DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
11847         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
11848     VarDecl *DstVD =
11849         buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
11850                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
11851     DeclRefExpr *PseudoDstExpr =
11852         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
11853     // For arrays generate assignment operation for single element and replace
11854     // it by the original array element in CodeGen.
11855     ExprResult AssignmentOp =
11856         BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
11857                    PseudoSrcExpr);
11858     if (AssignmentOp.isInvalid())
11859       continue;
11860     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11861                                        /*DiscardedValue=*/true);
11862     if (AssignmentOp.isInvalid())
11863       continue;
11864 
11865     DSAStack->addDSA(VD, DE, OMPC_copyin);
11866     Vars.push_back(DE);
11867     SrcExprs.push_back(PseudoSrcExpr);
11868     DstExprs.push_back(PseudoDstExpr);
11869     AssignmentOps.push_back(AssignmentOp.get());
11870   }
11871 
11872   if (Vars.empty())
11873     return nullptr;
11874 
11875   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11876                                  SrcExprs, DstExprs, AssignmentOps);
11877 }
11878 
11879 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11880                                               SourceLocation StartLoc,
11881                                               SourceLocation LParenLoc,
11882                                               SourceLocation EndLoc) {
11883   SmallVector<Expr *, 8> Vars;
11884   SmallVector<Expr *, 8> SrcExprs;
11885   SmallVector<Expr *, 8> DstExprs;
11886   SmallVector<Expr *, 8> AssignmentOps;
11887   for (Expr *RefExpr : VarList) {
11888     assert(RefExpr && "NULL expr in OpenMP linear clause.");
11889     SourceLocation ELoc;
11890     SourceRange ERange;
11891     Expr *SimpleRefExpr = RefExpr;
11892     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11893     if (Res.second) {
11894       // It will be analyzed later.
11895       Vars.push_back(RefExpr);
11896       SrcExprs.push_back(nullptr);
11897       DstExprs.push_back(nullptr);
11898       AssignmentOps.push_back(nullptr);
11899     }
11900     ValueDecl *D = Res.first;
11901     if (!D)
11902       continue;
11903 
11904     QualType Type = D->getType();
11905     auto *VD = dyn_cast<VarDecl>(D);
11906 
11907     // OpenMP [2.14.4.2, Restrictions, p.2]
11908     //  A list item that appears in a copyprivate clause may not appear in a
11909     //  private or firstprivate clause on the single construct.
11910     if (!VD || !DSAStack->isThreadPrivate(VD)) {
11911       DSAStackTy::DSAVarData DVar =
11912           DSAStack->getTopDSA(D, /*FromParent=*/false);
11913       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11914           DVar.RefExpr) {
11915         Diag(ELoc, diag::err_omp_wrong_dsa)
11916             << getOpenMPClauseName(DVar.CKind)
11917             << getOpenMPClauseName(OMPC_copyprivate);
11918         reportOriginalDsa(*this, DSAStack, D, DVar);
11919         continue;
11920       }
11921 
11922       // OpenMP [2.11.4.2, Restrictions, p.1]
11923       //  All list items that appear in a copyprivate clause must be either
11924       //  threadprivate or private in the enclosing context.
11925       if (DVar.CKind == OMPC_unknown) {
11926         DVar = DSAStack->getImplicitDSA(D, false);
11927         if (DVar.CKind == OMPC_shared) {
11928           Diag(ELoc, diag::err_omp_required_access)
11929               << getOpenMPClauseName(OMPC_copyprivate)
11930               << "threadprivate or private in the enclosing context";
11931           reportOriginalDsa(*this, DSAStack, D, DVar);
11932           continue;
11933         }
11934       }
11935     }
11936 
11937     // Variably modified types are not supported.
11938     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
11939       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
11940           << getOpenMPClauseName(OMPC_copyprivate) << Type
11941           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
11942       bool IsDecl =
11943           !VD ||
11944           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11945       Diag(D->getLocation(),
11946            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11947           << D;
11948       continue;
11949     }
11950 
11951     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11952     //  A variable of class type (or array thereof) that appears in a
11953     //  copyin clause requires an accessible, unambiguous copy assignment
11954     //  operator for the class type.
11955     Type = Context.getBaseElementType(Type.getNonReferenceType())
11956                .getUnqualifiedType();
11957     VarDecl *SrcVD =
11958         buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
11959                      D->hasAttrs() ? &D->getAttrs() : nullptr);
11960     DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
11961     VarDecl *DstVD =
11962         buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
11963                      D->hasAttrs() ? &D->getAttrs() : nullptr);
11964     DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11965     ExprResult AssignmentOp = BuildBinOp(
11966         DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
11967     if (AssignmentOp.isInvalid())
11968       continue;
11969     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
11970                                        /*DiscardedValue=*/true);
11971     if (AssignmentOp.isInvalid())
11972       continue;
11973 
11974     // No need to mark vars as copyprivate, they are already threadprivate or
11975     // implicitly private.
11976     assert(VD || isOpenMPCapturedDecl(D));
11977     Vars.push_back(
11978         VD ? RefExpr->IgnoreParens()
11979            : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
11980     SrcExprs.push_back(PseudoSrcExpr);
11981     DstExprs.push_back(PseudoDstExpr);
11982     AssignmentOps.push_back(AssignmentOp.get());
11983   }
11984 
11985   if (Vars.empty())
11986     return nullptr;
11987 
11988   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11989                                       Vars, SrcExprs, DstExprs, AssignmentOps);
11990 }
11991 
11992 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11993                                         SourceLocation StartLoc,
11994                                         SourceLocation LParenLoc,
11995                                         SourceLocation EndLoc) {
11996   if (VarList.empty())
11997     return nullptr;
11998 
11999   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
12000 }
12001 
12002 OMPClause *
12003 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
12004                               SourceLocation DepLoc, SourceLocation ColonLoc,
12005                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12006                               SourceLocation LParenLoc, SourceLocation EndLoc) {
12007   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
12008       DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
12009     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
12010         << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
12011     return nullptr;
12012   }
12013   if (DSAStack->getCurrentDirective() != OMPD_ordered &&
12014       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
12015        DepKind == OMPC_DEPEND_sink)) {
12016     unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
12017     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
12018         << getListOfPossibleValues(OMPC_depend, /*First=*/0,
12019                                    /*Last=*/OMPC_DEPEND_unknown, Except)
12020         << getOpenMPClauseName(OMPC_depend);
12021     return nullptr;
12022   }
12023   SmallVector<Expr *, 8> Vars;
12024   DSAStackTy::OperatorOffsetTy OpsOffs;
12025   llvm::APSInt DepCounter(/*BitWidth=*/32);
12026   llvm::APSInt TotalDepCount(/*BitWidth=*/32);
12027   if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
12028     if (const Expr *OrderedCountExpr =
12029             DSAStack->getParentOrderedRegionParam().first) {
12030       TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
12031       TotalDepCount.setIsUnsigned(/*Val=*/true);
12032     }
12033   }
12034   for (Expr *RefExpr : VarList) {
12035     assert(RefExpr && "NULL expr in OpenMP shared clause.");
12036     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
12037       // It will be analyzed later.
12038       Vars.push_back(RefExpr);
12039       continue;
12040     }
12041 
12042     SourceLocation ELoc = RefExpr->getExprLoc();
12043     Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
12044     if (DepKind == OMPC_DEPEND_sink) {
12045       if (DSAStack->getParentOrderedRegionParam().first &&
12046           DepCounter >= TotalDepCount) {
12047         Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
12048         continue;
12049       }
12050       ++DepCounter;
12051       // OpenMP  [2.13.9, Summary]
12052       // depend(dependence-type : vec), where dependence-type is:
12053       // 'sink' and where vec is the iteration vector, which has the form:
12054       //  x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
12055       // where n is the value specified by the ordered clause in the loop
12056       // directive, xi denotes the loop iteration variable of the i-th nested
12057       // loop associated with the loop directive, and di is a constant
12058       // non-negative integer.
12059       if (CurContext->isDependentContext()) {
12060         // It will be analyzed later.
12061         Vars.push_back(RefExpr);
12062         continue;
12063       }
12064       SimpleExpr = SimpleExpr->IgnoreImplicit();
12065       OverloadedOperatorKind OOK = OO_None;
12066       SourceLocation OOLoc;
12067       Expr *LHS = SimpleExpr;
12068       Expr *RHS = nullptr;
12069       if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
12070         OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
12071         OOLoc = BO->getOperatorLoc();
12072         LHS = BO->getLHS()->IgnoreParenImpCasts();
12073         RHS = BO->getRHS()->IgnoreParenImpCasts();
12074       } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
12075         OOK = OCE->getOperator();
12076         OOLoc = OCE->getOperatorLoc();
12077         LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
12078         RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
12079       } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
12080         OOK = MCE->getMethodDecl()
12081                   ->getNameInfo()
12082                   .getName()
12083                   .getCXXOverloadedOperator();
12084         OOLoc = MCE->getCallee()->getExprLoc();
12085         LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
12086         RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
12087       }
12088       SourceLocation ELoc;
12089       SourceRange ERange;
12090       auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
12091       if (Res.second) {
12092         // It will be analyzed later.
12093         Vars.push_back(RefExpr);
12094       }
12095       ValueDecl *D = Res.first;
12096       if (!D)
12097         continue;
12098 
12099       if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
12100         Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
12101         continue;
12102       }
12103       if (RHS) {
12104         ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
12105             RHS, OMPC_depend, /*StrictlyPositive=*/false);
12106         if (RHSRes.isInvalid())
12107           continue;
12108       }
12109       if (!CurContext->isDependentContext() &&
12110           DSAStack->getParentOrderedRegionParam().first &&
12111           DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
12112         const ValueDecl *VD =
12113             DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
12114         if (VD)
12115           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
12116               << 1 << VD;
12117         else
12118           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
12119         continue;
12120       }
12121       OpsOffs.emplace_back(RHS, OOK);
12122     } else {
12123       auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
12124       if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
12125           (ASE &&
12126            !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
12127            !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
12128         Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12129             << RefExpr->getSourceRange();
12130         continue;
12131       }
12132       bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
12133       getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
12134       ExprResult Res =
12135           CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
12136       getDiagnostics().setSuppressAllDiagnostics(Suppress);
12137       if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
12138         Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12139             << RefExpr->getSourceRange();
12140         continue;
12141       }
12142     }
12143     Vars.push_back(RefExpr->IgnoreParenImpCasts());
12144   }
12145 
12146   if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
12147       TotalDepCount > VarList.size() &&
12148       DSAStack->getParentOrderedRegionParam().first &&
12149       DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
12150     Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
12151         << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
12152   }
12153   if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
12154       Vars.empty())
12155     return nullptr;
12156 
12157   auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12158                                     DepKind, DepLoc, ColonLoc, Vars,
12159                                     TotalDepCount.getZExtValue());
12160   if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
12161       DSAStack->isParentOrderedRegion())
12162     DSAStack->addDoacrossDependClause(C, OpsOffs);
12163   return C;
12164 }
12165 
12166 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
12167                                          SourceLocation LParenLoc,
12168                                          SourceLocation EndLoc) {
12169   Expr *ValExpr = Device;
12170   Stmt *HelperValStmt = nullptr;
12171 
12172   // OpenMP [2.9.1, Restrictions]
12173   // The device expression must evaluate to a non-negative integer value.
12174   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
12175                                  /*StrictlyPositive=*/false))
12176     return nullptr;
12177 
12178   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
12179   OpenMPDirectiveKind CaptureRegion =
12180       getOpenMPCaptureRegionForClause(DKind, OMPC_device);
12181   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
12182     ValExpr = MakeFullExpr(ValExpr).get();
12183     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
12184     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12185     HelperValStmt = buildPreInits(Context, Captures);
12186   }
12187 
12188   return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
12189                                        StartLoc, LParenLoc, EndLoc);
12190 }
12191 
12192 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
12193                               DSAStackTy *Stack, QualType QTy,
12194                               bool FullCheck = true) {
12195   NamedDecl *ND;
12196   if (QTy->isIncompleteType(&ND)) {
12197     SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
12198     return false;
12199   }
12200   if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
12201       !QTy.isTrivialType(SemaRef.Context))
12202     SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
12203   return true;
12204 }
12205 
12206 /// Return true if it can be proven that the provided array expression
12207 /// (array section or array subscript) does NOT specify the whole size of the
12208 /// array whose base type is \a BaseQTy.
12209 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
12210                                                         const Expr *E,
12211                                                         QualType BaseQTy) {
12212   const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
12213 
12214   // If this is an array subscript, it refers to the whole size if the size of
12215   // the dimension is constant and equals 1. Also, an array section assumes the
12216   // format of an array subscript if no colon is used.
12217   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
12218     if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
12219       return ATy->getSize().getSExtValue() != 1;
12220     // Size can't be evaluated statically.
12221     return false;
12222   }
12223 
12224   assert(OASE && "Expecting array section if not an array subscript.");
12225   const Expr *LowerBound = OASE->getLowerBound();
12226   const Expr *Length = OASE->getLength();
12227 
12228   // If there is a lower bound that does not evaluates to zero, we are not
12229   // covering the whole dimension.
12230   if (LowerBound) {
12231     Expr::EvalResult Result;
12232     if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
12233       return false; // Can't get the integer value as a constant.
12234 
12235     llvm::APSInt ConstLowerBound = Result.Val.getInt();
12236     if (ConstLowerBound.getSExtValue())
12237       return true;
12238   }
12239 
12240   // If we don't have a length we covering the whole dimension.
12241   if (!Length)
12242     return false;
12243 
12244   // If the base is a pointer, we don't have a way to get the size of the
12245   // pointee.
12246   if (BaseQTy->isPointerType())
12247     return false;
12248 
12249   // We can only check if the length is the same as the size of the dimension
12250   // if we have a constant array.
12251   const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
12252   if (!CATy)
12253     return false;
12254 
12255   Expr::EvalResult Result;
12256   if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
12257     return false; // Can't get the integer value as a constant.
12258 
12259   llvm::APSInt ConstLength = Result.Val.getInt();
12260   return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12261 }
12262 
12263 // Return true if it can be proven that the provided array expression (array
12264 // section or array subscript) does NOT specify a single element of the array
12265 // whose base type is \a BaseQTy.
12266 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
12267                                                         const Expr *E,
12268                                                         QualType BaseQTy) {
12269   const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
12270 
12271   // An array subscript always refer to a single element. Also, an array section
12272   // assumes the format of an array subscript if no colon is used.
12273   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12274     return false;
12275 
12276   assert(OASE && "Expecting array section if not an array subscript.");
12277   const Expr *Length = OASE->getLength();
12278 
12279   // If we don't have a length we have to check if the array has unitary size
12280   // for this dimension. Also, we should always expect a length if the base type
12281   // is pointer.
12282   if (!Length) {
12283     if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
12284       return ATy->getSize().getSExtValue() != 1;
12285     // We cannot assume anything.
12286     return false;
12287   }
12288 
12289   // Check if the length evaluates to 1.
12290   Expr::EvalResult Result;
12291   if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
12292     return false; // Can't get the integer value as a constant.
12293 
12294   llvm::APSInt ConstLength = Result.Val.getInt();
12295   return ConstLength.getSExtValue() != 1;
12296 }
12297 
12298 // Return the expression of the base of the mappable expression or null if it
12299 // cannot be determined and do all the necessary checks to see if the expression
12300 // is valid as a standalone mappable expression. In the process, record all the
12301 // components of the expression.
12302 static const Expr *checkMapClauseExpressionBase(
12303     Sema &SemaRef, Expr *E,
12304     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
12305     OpenMPClauseKind CKind, bool NoDiagnose) {
12306   SourceLocation ELoc = E->getExprLoc();
12307   SourceRange ERange = E->getSourceRange();
12308 
12309   // The base of elements of list in a map clause have to be either:
12310   //  - a reference to variable or field.
12311   //  - a member expression.
12312   //  - an array expression.
12313   //
12314   // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12315   // reference to 'r'.
12316   //
12317   // If we have:
12318   //
12319   // struct SS {
12320   //   Bla S;
12321   //   foo() {
12322   //     #pragma omp target map (S.Arr[:12]);
12323   //   }
12324   // }
12325   //
12326   // We want to retrieve the member expression 'this->S';
12327 
12328   const Expr *RelevantExpr = nullptr;
12329 
12330   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12331   //  If a list item is an array section, it must specify contiguous storage.
12332   //
12333   // For this restriction it is sufficient that we make sure only references
12334   // to variables or fields and array expressions, and that no array sections
12335   // exist except in the rightmost expression (unless they cover the whole
12336   // dimension of the array). E.g. these would be invalid:
12337   //
12338   //   r.ArrS[3:5].Arr[6:7]
12339   //
12340   //   r.ArrS[3:5].x
12341   //
12342   // but these would be valid:
12343   //   r.ArrS[3].Arr[6:7]
12344   //
12345   //   r.ArrS[3].x
12346 
12347   bool AllowUnitySizeArraySection = true;
12348   bool AllowWholeSizeArraySection = true;
12349 
12350   while (!RelevantExpr) {
12351     E = E->IgnoreParenImpCasts();
12352 
12353     if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12354       if (!isa<VarDecl>(CurE->getDecl()))
12355         return nullptr;
12356 
12357       RelevantExpr = CurE;
12358 
12359       // If we got a reference to a declaration, we should not expect any array
12360       // section before that.
12361       AllowUnitySizeArraySection = false;
12362       AllowWholeSizeArraySection = false;
12363 
12364       // Record the component.
12365       CurComponents.emplace_back(CurE, CurE->getDecl());
12366     } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
12367       Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
12368 
12369       if (isa<CXXThisExpr>(BaseE))
12370         // We found a base expression: this->Val.
12371         RelevantExpr = CurE;
12372       else
12373         E = BaseE;
12374 
12375       if (!isa<FieldDecl>(CurE->getMemberDecl())) {
12376         if (!NoDiagnose) {
12377           SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12378               << CurE->getSourceRange();
12379           return nullptr;
12380         }
12381         if (RelevantExpr)
12382           return nullptr;
12383         continue;
12384       }
12385 
12386       auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12387 
12388       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12389       //  A bit-field cannot appear in a map clause.
12390       //
12391       if (FD->isBitField()) {
12392         if (!NoDiagnose) {
12393           SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12394               << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12395           return nullptr;
12396         }
12397         if (RelevantExpr)
12398           return nullptr;
12399         continue;
12400       }
12401 
12402       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12403       //  If the type of a list item is a reference to a type T then the type
12404       //  will be considered to be T for all purposes of this clause.
12405       QualType CurType = BaseE->getType().getNonReferenceType();
12406 
12407       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12408       //  A list item cannot be a variable that is a member of a structure with
12409       //  a union type.
12410       //
12411       if (CurType->isUnionType()) {
12412         if (!NoDiagnose) {
12413           SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12414               << CurE->getSourceRange();
12415           return nullptr;
12416         }
12417         continue;
12418       }
12419 
12420       // If we got a member expression, we should not expect any array section
12421       // before that:
12422       //
12423       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12424       //  If a list item is an element of a structure, only the rightmost symbol
12425       //  of the variable reference can be an array section.
12426       //
12427       AllowUnitySizeArraySection = false;
12428       AllowWholeSizeArraySection = false;
12429 
12430       // Record the component.
12431       CurComponents.emplace_back(CurE, FD);
12432     } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
12433       E = CurE->getBase()->IgnoreParenImpCasts();
12434 
12435       if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
12436         if (!NoDiagnose) {
12437           SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12438               << 0 << CurE->getSourceRange();
12439           return nullptr;
12440         }
12441         continue;
12442       }
12443 
12444       // If we got an array subscript that express the whole dimension we
12445       // can have any array expressions before. If it only expressing part of
12446       // the dimension, we can only have unitary-size array expressions.
12447       if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
12448                                                       E->getType()))
12449         AllowWholeSizeArraySection = false;
12450 
12451       // Record the component - we don't have any declaration associated.
12452       CurComponents.emplace_back(CurE, nullptr);
12453     } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
12454       assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
12455       E = CurE->getBase()->IgnoreParenImpCasts();
12456 
12457       QualType CurType =
12458           OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12459 
12460       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12461       //  If the type of a list item is a reference to a type T then the type
12462       //  will be considered to be T for all purposes of this clause.
12463       if (CurType->isReferenceType())
12464         CurType = CurType->getPointeeType();
12465 
12466       bool IsPointer = CurType->isAnyPointerType();
12467 
12468       if (!IsPointer && !CurType->isArrayType()) {
12469         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12470             << 0 << CurE->getSourceRange();
12471         return nullptr;
12472       }
12473 
12474       bool NotWhole =
12475           checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
12476       bool NotUnity =
12477           checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
12478 
12479       if (AllowWholeSizeArraySection) {
12480         // Any array section is currently allowed. Allowing a whole size array
12481         // section implies allowing a unity array section as well.
12482         //
12483         // If this array section refers to the whole dimension we can still
12484         // accept other array sections before this one, except if the base is a
12485         // pointer. Otherwise, only unitary sections are accepted.
12486         if (NotWhole || IsPointer)
12487           AllowWholeSizeArraySection = false;
12488       } else if (AllowUnitySizeArraySection && NotUnity) {
12489         // A unity or whole array section is not allowed and that is not
12490         // compatible with the properties of the current array section.
12491         SemaRef.Diag(
12492             ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12493             << CurE->getSourceRange();
12494         return nullptr;
12495       }
12496 
12497       // Record the component - we don't have any declaration associated.
12498       CurComponents.emplace_back(CurE, nullptr);
12499     } else {
12500       if (!NoDiagnose) {
12501         // If nothing else worked, this is not a valid map clause expression.
12502         SemaRef.Diag(
12503             ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12504             << ERange;
12505       }
12506       return nullptr;
12507     }
12508   }
12509 
12510   return RelevantExpr;
12511 }
12512 
12513 // Return true if expression E associated with value VD has conflicts with other
12514 // map information.
12515 static bool checkMapConflicts(
12516     Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
12517     bool CurrentRegionOnly,
12518     OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12519     OpenMPClauseKind CKind) {
12520   assert(VD && E);
12521   SourceLocation ELoc = E->getExprLoc();
12522   SourceRange ERange = E->getSourceRange();
12523 
12524   // In order to easily check the conflicts we need to match each component of
12525   // the expression under test with the components of the expressions that are
12526   // already in the stack.
12527 
12528   assert(!CurComponents.empty() && "Map clause expression with no components!");
12529   assert(CurComponents.back().getAssociatedDeclaration() == VD &&
12530          "Map clause expression with unexpected base!");
12531 
12532   // Variables to help detecting enclosing problems in data environment nests.
12533   bool IsEnclosedByDataEnvironmentExpr = false;
12534   const Expr *EnclosingExpr = nullptr;
12535 
12536   bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12537       VD, CurrentRegionOnly,
12538       [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12539        ERange, CKind, &EnclosingExpr,
12540        CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12541                           StackComponents,
12542                       OpenMPClauseKind) {
12543         assert(!StackComponents.empty() &&
12544                "Map clause expression with no components!");
12545         assert(StackComponents.back().getAssociatedDeclaration() == VD &&
12546                "Map clause expression with unexpected base!");
12547         (void)VD;
12548 
12549         // The whole expression in the stack.
12550         const Expr *RE = StackComponents.front().getAssociatedExpression();
12551 
12552         // Expressions must start from the same base. Here we detect at which
12553         // point both expressions diverge from each other and see if we can
12554         // detect if the memory referred to both expressions is contiguous and
12555         // do not overlap.
12556         auto CI = CurComponents.rbegin();
12557         auto CE = CurComponents.rend();
12558         auto SI = StackComponents.rbegin();
12559         auto SE = StackComponents.rend();
12560         for (; CI != CE && SI != SE; ++CI, ++SI) {
12561 
12562           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12563           //  At most one list item can be an array item derived from a given
12564           //  variable in map clauses of the same construct.
12565           if (CurrentRegionOnly &&
12566               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12567                isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12568               (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12569                isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12570             SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
12571                          diag::err_omp_multiple_array_items_in_map_clause)
12572                 << CI->getAssociatedExpression()->getSourceRange();
12573             SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12574                          diag::note_used_here)
12575                 << SI->getAssociatedExpression()->getSourceRange();
12576             return true;
12577           }
12578 
12579           // Do both expressions have the same kind?
12580           if (CI->getAssociatedExpression()->getStmtClass() !=
12581               SI->getAssociatedExpression()->getStmtClass())
12582             break;
12583 
12584           // Are we dealing with different variables/fields?
12585           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
12586             break;
12587         }
12588         // Check if the extra components of the expressions in the enclosing
12589         // data environment are redundant for the current base declaration.
12590         // If they are, the maps completely overlap, which is legal.
12591         for (; SI != SE; ++SI) {
12592           QualType Type;
12593           if (const auto *ASE =
12594                   dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
12595             Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
12596           } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
12597                          SI->getAssociatedExpression())) {
12598             const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
12599             Type =
12600                 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12601           }
12602           if (Type.isNull() || Type->isAnyPointerType() ||
12603               checkArrayExpressionDoesNotReferToWholeSize(
12604                   SemaRef, SI->getAssociatedExpression(), Type))
12605             break;
12606         }
12607 
12608         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12609         //  List items of map clauses in the same construct must not share
12610         //  original storage.
12611         //
12612         // If the expressions are exactly the same or one is a subset of the
12613         // other, it means they are sharing storage.
12614         if (CI == CE && SI == SE) {
12615           if (CurrentRegionOnly) {
12616             if (CKind == OMPC_map) {
12617               SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
12618             } else {
12619               assert(CKind == OMPC_to || CKind == OMPC_from);
12620               SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12621                   << ERange;
12622             }
12623             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12624                 << RE->getSourceRange();
12625             return true;
12626           }
12627           // If we find the same expression in the enclosing data environment,
12628           // that is legal.
12629           IsEnclosedByDataEnvironmentExpr = true;
12630           return false;
12631         }
12632 
12633         QualType DerivedType =
12634             std::prev(CI)->getAssociatedDeclaration()->getType();
12635         SourceLocation DerivedLoc =
12636             std::prev(CI)->getAssociatedExpression()->getExprLoc();
12637 
12638         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12639         //  If the type of a list item is a reference to a type T then the type
12640         //  will be considered to be T for all purposes of this clause.
12641         DerivedType = DerivedType.getNonReferenceType();
12642 
12643         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12644         //  A variable for which the type is pointer and an array section
12645         //  derived from that variable must not appear as list items of map
12646         //  clauses of the same construct.
12647         //
12648         // Also, cover one of the cases in:
12649         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12650         //  If any part of the original storage of a list item has corresponding
12651         //  storage in the device data environment, all of the original storage
12652         //  must have corresponding storage in the device data environment.
12653         //
12654         if (DerivedType->isAnyPointerType()) {
12655           if (CI == CE || SI == SE) {
12656             SemaRef.Diag(
12657                 DerivedLoc,
12658                 diag::err_omp_pointer_mapped_along_with_derived_section)
12659                 << DerivedLoc;
12660             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12661                 << RE->getSourceRange();
12662             return true;
12663           }
12664           if (CI->getAssociatedExpression()->getStmtClass() !=
12665                          SI->getAssociatedExpression()->getStmtClass() ||
12666                      CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12667                          SI->getAssociatedDeclaration()->getCanonicalDecl()) {
12668             assert(CI != CE && SI != SE);
12669             SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
12670                 << DerivedLoc;
12671             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12672                 << RE->getSourceRange();
12673             return true;
12674           }
12675         }
12676 
12677         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12678         //  List items of map clauses in the same construct must not share
12679         //  original storage.
12680         //
12681         // An expression is a subset of the other.
12682         if (CurrentRegionOnly && (CI == CE || SI == SE)) {
12683           if (CKind == OMPC_map) {
12684             if (CI != CE || SI != SE) {
12685               // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
12686               // a pointer.
12687               auto Begin =
12688                   CI != CE ? CurComponents.begin() : StackComponents.begin();
12689               auto End = CI != CE ? CurComponents.end() : StackComponents.end();
12690               auto It = Begin;
12691               while (It != End && !It->getAssociatedDeclaration())
12692                 std::advance(It, 1);
12693               assert(It != End &&
12694                      "Expected at least one component with the declaration.");
12695               if (It != Begin && It->getAssociatedDeclaration()
12696                                      ->getType()
12697                                      .getCanonicalType()
12698                                      ->isAnyPointerType()) {
12699                 IsEnclosedByDataEnvironmentExpr = false;
12700                 EnclosingExpr = nullptr;
12701                 return false;
12702               }
12703             }
12704             SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
12705           } else {
12706             assert(CKind == OMPC_to || CKind == OMPC_from);
12707             SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12708                 << ERange;
12709           }
12710           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12711               << RE->getSourceRange();
12712           return true;
12713         }
12714 
12715         // The current expression uses the same base as other expression in the
12716         // data environment but does not contain it completely.
12717         if (!CurrentRegionOnly && SI != SE)
12718           EnclosingExpr = RE;
12719 
12720         // The current expression is a subset of the expression in the data
12721         // environment.
12722         IsEnclosedByDataEnvironmentExpr |=
12723             (!CurrentRegionOnly && CI != CE && SI == SE);
12724 
12725         return false;
12726       });
12727 
12728   if (CurrentRegionOnly)
12729     return FoundError;
12730 
12731   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12732   //  If any part of the original storage of a list item has corresponding
12733   //  storage in the device data environment, all of the original storage must
12734   //  have corresponding storage in the device data environment.
12735   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12736   //  If a list item is an element of a structure, and a different element of
12737   //  the structure has a corresponding list item in the device data environment
12738   //  prior to a task encountering the construct associated with the map clause,
12739   //  then the list item must also have a corresponding list item in the device
12740   //  data environment prior to the task encountering the construct.
12741   //
12742   if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12743     SemaRef.Diag(ELoc,
12744                  diag::err_omp_original_storage_is_shared_and_does_not_contain)
12745         << ERange;
12746     SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12747         << EnclosingExpr->getSourceRange();
12748     return true;
12749   }
12750 
12751   return FoundError;
12752 }
12753 
12754 namespace {
12755 // Utility struct that gathers all the related lists associated with a mappable
12756 // expression.
12757 struct MappableVarListInfo {
12758   // The list of expressions.
12759   ArrayRef<Expr *> VarList;
12760   // The list of processed expressions.
12761   SmallVector<Expr *, 16> ProcessedVarList;
12762   // The mappble components for each expression.
12763   OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12764   // The base declaration of the variable.
12765   SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12766 
12767   MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12768     // We have a list of components and base declarations for each entry in the
12769     // variable list.
12770     VarComponents.reserve(VarList.size());
12771     VarBaseDeclarations.reserve(VarList.size());
12772   }
12773 };
12774 }
12775 
12776 // Check the validity of the provided variable list for the provided clause kind
12777 // \a CKind. In the check process the valid expressions, and mappable expression
12778 // components and variables are extracted and used to fill \a Vars,
12779 // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12780 // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12781 static void
12782 checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12783                             OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12784                             SourceLocation StartLoc,
12785                             OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12786                             bool IsMapTypeImplicit = false) {
12787   // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12788   assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
12789          "Unexpected clause kind with mappable expressions!");
12790 
12791   // Keep track of the mappable components and base declarations in this clause.
12792   // Each entry in the list is going to have a list of components associated. We
12793   // record each set of the components so that we can build the clause later on.
12794   // In the end we should have the same amount of declarations and component
12795   // lists.
12796 
12797   for (Expr *RE : MVLI.VarList) {
12798     assert(RE && "Null expr in omp to/from/map clause");
12799     SourceLocation ELoc = RE->getExprLoc();
12800 
12801     const Expr *VE = RE->IgnoreParenLValueCasts();
12802 
12803     if (VE->isValueDependent() || VE->isTypeDependent() ||
12804         VE->isInstantiationDependent() ||
12805         VE->containsUnexpandedParameterPack()) {
12806       // We can only analyze this information once the missing information is
12807       // resolved.
12808       MVLI.ProcessedVarList.push_back(RE);
12809       continue;
12810     }
12811 
12812     Expr *SimpleExpr = RE->IgnoreParenCasts();
12813 
12814     if (!RE->IgnoreParenImpCasts()->isLValue()) {
12815       SemaRef.Diag(ELoc,
12816                    diag::err_omp_expected_named_var_member_or_array_expression)
12817           << RE->getSourceRange();
12818       continue;
12819     }
12820 
12821     OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12822     ValueDecl *CurDeclaration = nullptr;
12823 
12824     // Obtain the array or member expression bases if required. Also, fill the
12825     // components array with all the components identified in the process.
12826     const Expr *BE = checkMapClauseExpressionBase(
12827         SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
12828     if (!BE)
12829       continue;
12830 
12831     assert(!CurComponents.empty() &&
12832            "Invalid mappable expression information.");
12833 
12834     // For the following checks, we rely on the base declaration which is
12835     // expected to be associated with the last component. The declaration is
12836     // expected to be a variable or a field (if 'this' is being mapped).
12837     CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12838     assert(CurDeclaration && "Null decl on map clause.");
12839     assert(
12840         CurDeclaration->isCanonicalDecl() &&
12841         "Expecting components to have associated only canonical declarations.");
12842 
12843     auto *VD = dyn_cast<VarDecl>(CurDeclaration);
12844     const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
12845 
12846     assert((VD || FD) && "Only variables or fields are expected here!");
12847     (void)FD;
12848 
12849     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
12850     // threadprivate variables cannot appear in a map clause.
12851     // OpenMP 4.5 [2.10.5, target update Construct]
12852     // threadprivate variables cannot appear in a from clause.
12853     if (VD && DSAS->isThreadPrivate(VD)) {
12854       DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
12855       SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12856           << getOpenMPClauseName(CKind);
12857       reportOriginalDsa(SemaRef, DSAS, VD, DVar);
12858       continue;
12859     }
12860 
12861     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12862     //  A list item cannot appear in both a map clause and a data-sharing
12863     //  attribute clause on the same construct.
12864 
12865     // Check conflicts with other map clause expressions. We check the conflicts
12866     // with the current construct separately from the enclosing data
12867     // environment, because the restrictions are different. We only have to
12868     // check conflicts across regions for the map clauses.
12869     if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
12870                           /*CurrentRegionOnly=*/true, CurComponents, CKind))
12871       break;
12872     if (CKind == OMPC_map &&
12873         checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
12874                           /*CurrentRegionOnly=*/false, CurComponents, CKind))
12875       break;
12876 
12877     // OpenMP 4.5 [2.10.5, target update Construct]
12878     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12879     //  If the type of a list item is a reference to a type T then the type will
12880     //  be considered to be T for all purposes of this clause.
12881     auto I = llvm::find_if(
12882         CurComponents,
12883         [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
12884           return MC.getAssociatedDeclaration();
12885         });
12886     assert(I != CurComponents.end() && "Null decl on map clause.");
12887     QualType Type =
12888         I->getAssociatedDeclaration()->getType().getNonReferenceType();
12889 
12890     // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12891     // A list item in a to or from clause must have a mappable type.
12892     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12893     //  A list item must have a mappable type.
12894     if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
12895                            DSAS, Type))
12896       continue;
12897 
12898     if (CKind == OMPC_map) {
12899       // target enter data
12900       // OpenMP [2.10.2, Restrictions, p. 99]
12901       // A map-type must be specified in all map clauses and must be either
12902       // to or alloc.
12903       OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12904       if (DKind == OMPD_target_enter_data &&
12905           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12906         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12907             << (IsMapTypeImplicit ? 1 : 0)
12908             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12909             << getOpenMPDirectiveName(DKind);
12910         continue;
12911       }
12912 
12913       // target exit_data
12914       // OpenMP [2.10.3, Restrictions, p. 102]
12915       // A map-type must be specified in all map clauses and must be either
12916       // from, release, or delete.
12917       if (DKind == OMPD_target_exit_data &&
12918           !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12919             MapType == OMPC_MAP_delete)) {
12920         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12921             << (IsMapTypeImplicit ? 1 : 0)
12922             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12923             << getOpenMPDirectiveName(DKind);
12924         continue;
12925       }
12926 
12927       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12928       // A list item cannot appear in both a map clause and a data-sharing
12929       // attribute clause on the same construct
12930       if (VD && isOpenMPTargetExecutionDirective(DKind)) {
12931         DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
12932         if (isOpenMPPrivate(DVar.CKind)) {
12933           SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
12934               << getOpenMPClauseName(DVar.CKind)
12935               << getOpenMPClauseName(OMPC_map)
12936               << getOpenMPDirectiveName(DSAS->getCurrentDirective());
12937           reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
12938           continue;
12939         }
12940       }
12941     }
12942 
12943     // Save the current expression.
12944     MVLI.ProcessedVarList.push_back(RE);
12945 
12946     // Store the components in the stack so that they can be used to check
12947     // against other clauses later on.
12948     DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12949                                           /*WhereFoundClauseKind=*/OMPC_map);
12950 
12951     // Save the components and declaration to create the clause. For purposes of
12952     // the clause creation, any component list that has has base 'this' uses
12953     // null as base declaration.
12954     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12955     MVLI.VarComponents.back().append(CurComponents.begin(),
12956                                      CurComponents.end());
12957     MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12958                                                            : CurDeclaration);
12959   }
12960 }
12961 
12962 OMPClause *
12963 Sema::ActOnOpenMPMapClause(ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
12964                            ArrayRef<SourceLocation> MapTypeModifiersLoc,
12965                            OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12966                            SourceLocation MapLoc, SourceLocation ColonLoc,
12967                            ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12968                            SourceLocation LParenLoc, SourceLocation EndLoc) {
12969   MappableVarListInfo MVLI(VarList);
12970   checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12971                               MapType, IsMapTypeImplicit);
12972 
12973   OpenMPMapModifierKind Modifiers[] = { OMPC_MAP_MODIFIER_unknown,
12974                                         OMPC_MAP_MODIFIER_unknown };
12975   SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers];
12976 
12977   // Process map-type-modifiers, flag errors for duplicate modifiers.
12978   unsigned Count = 0;
12979   for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
12980     if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
12981         llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
12982       Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
12983       continue;
12984     }
12985     assert(Count < OMPMapClause::NumberOfModifiers &&
12986            "Modifiers exceed the allowed number of map type modifiers");
12987     Modifiers[Count] = MapTypeModifiers[I];
12988     ModifiersLoc[Count] = MapTypeModifiersLoc[I];
12989     ++Count;
12990   }
12991 
12992   // We need to produce a map clause even if we don't have variables so that
12993   // other diagnostics related with non-existing map clauses are accurate.
12994   return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12995                               MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12996                               MVLI.VarComponents, Modifiers, ModifiersLoc,
12997                               MapType, IsMapTypeImplicit, MapLoc);
12998 }
12999 
13000 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
13001                                                TypeResult ParsedType) {
13002   assert(ParsedType.isUsable());
13003 
13004   QualType ReductionType = GetTypeFromParser(ParsedType.get());
13005   if (ReductionType.isNull())
13006     return QualType();
13007 
13008   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
13009   // A type name in a declare reduction directive cannot be a function type, an
13010   // array type, a reference type, or a type qualified with const, volatile or
13011   // restrict.
13012   if (ReductionType.hasQualifiers()) {
13013     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
13014     return QualType();
13015   }
13016 
13017   if (ReductionType->isFunctionType()) {
13018     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
13019     return QualType();
13020   }
13021   if (ReductionType->isReferenceType()) {
13022     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
13023     return QualType();
13024   }
13025   if (ReductionType->isArrayType()) {
13026     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
13027     return QualType();
13028   }
13029   return ReductionType;
13030 }
13031 
13032 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
13033     Scope *S, DeclContext *DC, DeclarationName Name,
13034     ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
13035     AccessSpecifier AS, Decl *PrevDeclInScope) {
13036   SmallVector<Decl *, 8> Decls;
13037   Decls.reserve(ReductionTypes.size());
13038 
13039   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
13040                       forRedeclarationInCurContext());
13041   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
13042   // A reduction-identifier may not be re-declared in the current scope for the
13043   // same type or for a type that is compatible according to the base language
13044   // rules.
13045   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
13046   OMPDeclareReductionDecl *PrevDRD = nullptr;
13047   bool InCompoundScope = true;
13048   if (S != nullptr) {
13049     // Find previous declaration with the same name not referenced in other
13050     // declarations.
13051     FunctionScopeInfo *ParentFn = getEnclosingFunction();
13052     InCompoundScope =
13053         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
13054     LookupName(Lookup, S);
13055     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
13056                          /*AllowInlineNamespace=*/false);
13057     llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
13058     LookupResult::Filter Filter = Lookup.makeFilter();
13059     while (Filter.hasNext()) {
13060       auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
13061       if (InCompoundScope) {
13062         auto I = UsedAsPrevious.find(PrevDecl);
13063         if (I == UsedAsPrevious.end())
13064           UsedAsPrevious[PrevDecl] = false;
13065         if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
13066           UsedAsPrevious[D] = true;
13067       }
13068       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
13069           PrevDecl->getLocation();
13070     }
13071     Filter.done();
13072     if (InCompoundScope) {
13073       for (const auto &PrevData : UsedAsPrevious) {
13074         if (!PrevData.second) {
13075           PrevDRD = PrevData.first;
13076           break;
13077         }
13078       }
13079     }
13080   } else if (PrevDeclInScope != nullptr) {
13081     auto *PrevDRDInScope = PrevDRD =
13082         cast<OMPDeclareReductionDecl>(PrevDeclInScope);
13083     do {
13084       PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
13085           PrevDRDInScope->getLocation();
13086       PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
13087     } while (PrevDRDInScope != nullptr);
13088   }
13089   for (const auto &TyData : ReductionTypes) {
13090     const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
13091     bool Invalid = false;
13092     if (I != PreviousRedeclTypes.end()) {
13093       Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
13094           << TyData.first;
13095       Diag(I->second, diag::note_previous_definition);
13096       Invalid = true;
13097     }
13098     PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
13099     auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
13100                                                 Name, TyData.first, PrevDRD);
13101     DC->addDecl(DRD);
13102     DRD->setAccess(AS);
13103     Decls.push_back(DRD);
13104     if (Invalid)
13105       DRD->setInvalidDecl();
13106     else
13107       PrevDRD = DRD;
13108   }
13109 
13110   return DeclGroupPtrTy::make(
13111       DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
13112 }
13113 
13114 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
13115   auto *DRD = cast<OMPDeclareReductionDecl>(D);
13116 
13117   // Enter new function scope.
13118   PushFunctionScope();
13119   setFunctionHasBranchProtectedScope();
13120   getCurFunction()->setHasOMPDeclareReductionCombiner();
13121 
13122   if (S != nullptr)
13123     PushDeclContext(S, DRD);
13124   else
13125     CurContext = DRD;
13126 
13127   PushExpressionEvaluationContext(
13128       ExpressionEvaluationContext::PotentiallyEvaluated);
13129 
13130   QualType ReductionType = DRD->getType();
13131   // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
13132   // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
13133   // uses semantics of argument handles by value, but it should be passed by
13134   // reference. C lang does not support references, so pass all parameters as
13135   // pointers.
13136   // Create 'T omp_in;' variable.
13137   VarDecl *OmpInParm =
13138       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
13139   // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
13140   // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
13141   // uses semantics of argument handles by value, but it should be passed by
13142   // reference. C lang does not support references, so pass all parameters as
13143   // pointers.
13144   // Create 'T omp_out;' variable.
13145   VarDecl *OmpOutParm =
13146       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
13147   if (S != nullptr) {
13148     PushOnScopeChains(OmpInParm, S);
13149     PushOnScopeChains(OmpOutParm, S);
13150   } else {
13151     DRD->addDecl(OmpInParm);
13152     DRD->addDecl(OmpOutParm);
13153   }
13154   Expr *InE =
13155       ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
13156   Expr *OutE =
13157       ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
13158   DRD->setCombinerData(InE, OutE);
13159 }
13160 
13161 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
13162   auto *DRD = cast<OMPDeclareReductionDecl>(D);
13163   DiscardCleanupsInEvaluationContext();
13164   PopExpressionEvaluationContext();
13165 
13166   PopDeclContext();
13167   PopFunctionScopeInfo();
13168 
13169   if (Combiner != nullptr)
13170     DRD->setCombiner(Combiner);
13171   else
13172     DRD->setInvalidDecl();
13173 }
13174 
13175 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
13176   auto *DRD = cast<OMPDeclareReductionDecl>(D);
13177 
13178   // Enter new function scope.
13179   PushFunctionScope();
13180   setFunctionHasBranchProtectedScope();
13181 
13182   if (S != nullptr)
13183     PushDeclContext(S, DRD);
13184   else
13185     CurContext = DRD;
13186 
13187   PushExpressionEvaluationContext(
13188       ExpressionEvaluationContext::PotentiallyEvaluated);
13189 
13190   QualType ReductionType = DRD->getType();
13191   // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
13192   // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
13193   // uses semantics of argument handles by value, but it should be passed by
13194   // reference. C lang does not support references, so pass all parameters as
13195   // pointers.
13196   // Create 'T omp_priv;' variable.
13197   VarDecl *OmpPrivParm =
13198       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
13199   // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
13200   // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
13201   // uses semantics of argument handles by value, but it should be passed by
13202   // reference. C lang does not support references, so pass all parameters as
13203   // pointers.
13204   // Create 'T omp_orig;' variable.
13205   VarDecl *OmpOrigParm =
13206       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
13207   if (S != nullptr) {
13208     PushOnScopeChains(OmpPrivParm, S);
13209     PushOnScopeChains(OmpOrigParm, S);
13210   } else {
13211     DRD->addDecl(OmpPrivParm);
13212     DRD->addDecl(OmpOrigParm);
13213   }
13214   Expr *OrigE =
13215       ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
13216   Expr *PrivE =
13217       ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
13218   DRD->setInitializerData(OrigE, PrivE);
13219   return OmpPrivParm;
13220 }
13221 
13222 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
13223                                                      VarDecl *OmpPrivParm) {
13224   auto *DRD = cast<OMPDeclareReductionDecl>(D);
13225   DiscardCleanupsInEvaluationContext();
13226   PopExpressionEvaluationContext();
13227 
13228   PopDeclContext();
13229   PopFunctionScopeInfo();
13230 
13231   if (Initializer != nullptr) {
13232     DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
13233   } else if (OmpPrivParm->hasInit()) {
13234     DRD->setInitializer(OmpPrivParm->getInit(),
13235                         OmpPrivParm->isDirectInit()
13236                             ? OMPDeclareReductionDecl::DirectInit
13237                             : OMPDeclareReductionDecl::CopyInit);
13238   } else {
13239     DRD->setInvalidDecl();
13240   }
13241 }
13242 
13243 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
13244     Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
13245   for (Decl *D : DeclReductions.get()) {
13246     if (IsValid) {
13247       if (S)
13248         PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
13249                           /*AddToContext=*/false);
13250     } else {
13251       D->setInvalidDecl();
13252     }
13253   }
13254   return DeclReductions;
13255 }
13256 
13257 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
13258                                            SourceLocation StartLoc,
13259                                            SourceLocation LParenLoc,
13260                                            SourceLocation EndLoc) {
13261   Expr *ValExpr = NumTeams;
13262   Stmt *HelperValStmt = nullptr;
13263 
13264   // OpenMP [teams Constrcut, Restrictions]
13265   // The num_teams expression must evaluate to a positive integer value.
13266   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
13267                                  /*StrictlyPositive=*/true))
13268     return nullptr;
13269 
13270   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
13271   OpenMPDirectiveKind CaptureRegion =
13272       getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
13273   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
13274     ValExpr = MakeFullExpr(ValExpr).get();
13275     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
13276     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13277     HelperValStmt = buildPreInits(Context, Captures);
13278   }
13279 
13280   return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
13281                                          StartLoc, LParenLoc, EndLoc);
13282 }
13283 
13284 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
13285                                               SourceLocation StartLoc,
13286                                               SourceLocation LParenLoc,
13287                                               SourceLocation EndLoc) {
13288   Expr *ValExpr = ThreadLimit;
13289   Stmt *HelperValStmt = nullptr;
13290 
13291   // OpenMP [teams Constrcut, Restrictions]
13292   // The thread_limit expression must evaluate to a positive integer value.
13293   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
13294                                  /*StrictlyPositive=*/true))
13295     return nullptr;
13296 
13297   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
13298   OpenMPDirectiveKind CaptureRegion =
13299       getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
13300   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
13301     ValExpr = MakeFullExpr(ValExpr).get();
13302     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
13303     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13304     HelperValStmt = buildPreInits(Context, Captures);
13305   }
13306 
13307   return new (Context) OMPThreadLimitClause(
13308       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
13309 }
13310 
13311 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
13312                                            SourceLocation StartLoc,
13313                                            SourceLocation LParenLoc,
13314                                            SourceLocation EndLoc) {
13315   Expr *ValExpr = Priority;
13316 
13317   // OpenMP [2.9.1, task Constrcut]
13318   // The priority-value is a non-negative numerical scalar expression.
13319   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
13320                                  /*StrictlyPositive=*/false))
13321     return nullptr;
13322 
13323   return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13324 }
13325 
13326 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
13327                                             SourceLocation StartLoc,
13328                                             SourceLocation LParenLoc,
13329                                             SourceLocation EndLoc) {
13330   Expr *ValExpr = Grainsize;
13331 
13332   // OpenMP [2.9.2, taskloop Constrcut]
13333   // The parameter of the grainsize clause must be a positive integer
13334   // expression.
13335   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
13336                                  /*StrictlyPositive=*/true))
13337     return nullptr;
13338 
13339   return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13340 }
13341 
13342 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
13343                                            SourceLocation StartLoc,
13344                                            SourceLocation LParenLoc,
13345                                            SourceLocation EndLoc) {
13346   Expr *ValExpr = NumTasks;
13347 
13348   // OpenMP [2.9.2, taskloop Constrcut]
13349   // The parameter of the num_tasks clause must be a positive integer
13350   // expression.
13351   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
13352                                  /*StrictlyPositive=*/true))
13353     return nullptr;
13354 
13355   return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13356 }
13357 
13358 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
13359                                        SourceLocation LParenLoc,
13360                                        SourceLocation EndLoc) {
13361   // OpenMP [2.13.2, critical construct, Description]
13362   // ... where hint-expression is an integer constant expression that evaluates
13363   // to a valid lock hint.
13364   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
13365   if (HintExpr.isInvalid())
13366     return nullptr;
13367   return new (Context)
13368       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
13369 }
13370 
13371 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
13372     OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
13373     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
13374     SourceLocation EndLoc) {
13375   if (Kind == OMPC_DIST_SCHEDULE_unknown) {
13376     std::string Values;
13377     Values += "'";
13378     Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
13379     Values += "'";
13380     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
13381         << Values << getOpenMPClauseName(OMPC_dist_schedule);
13382     return nullptr;
13383   }
13384   Expr *ValExpr = ChunkSize;
13385   Stmt *HelperValStmt = nullptr;
13386   if (ChunkSize) {
13387     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
13388         !ChunkSize->isInstantiationDependent() &&
13389         !ChunkSize->containsUnexpandedParameterPack()) {
13390       SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
13391       ExprResult Val =
13392           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
13393       if (Val.isInvalid())
13394         return nullptr;
13395 
13396       ValExpr = Val.get();
13397 
13398       // OpenMP [2.7.1, Restrictions]
13399       //  chunk_size must be a loop invariant integer expression with a positive
13400       //  value.
13401       llvm::APSInt Result;
13402       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
13403         if (Result.isSigned() && !Result.isStrictlyPositive()) {
13404           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
13405               << "dist_schedule" << ChunkSize->getSourceRange();
13406           return nullptr;
13407         }
13408       } else if (getOpenMPCaptureRegionForClause(
13409                      DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
13410                      OMPD_unknown &&
13411                  !CurContext->isDependentContext()) {
13412         ValExpr = MakeFullExpr(ValExpr).get();
13413         llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
13414         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13415         HelperValStmt = buildPreInits(Context, Captures);
13416       }
13417     }
13418   }
13419 
13420   return new (Context)
13421       OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
13422                             Kind, ValExpr, HelperValStmt);
13423 }
13424 
13425 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
13426     OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
13427     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
13428     SourceLocation KindLoc, SourceLocation EndLoc) {
13429   // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
13430   if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
13431     std::string Value;
13432     SourceLocation Loc;
13433     Value += "'";
13434     if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
13435       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
13436                                              OMPC_DEFAULTMAP_MODIFIER_tofrom);
13437       Loc = MLoc;
13438     } else {
13439       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
13440                                              OMPC_DEFAULTMAP_scalar);
13441       Loc = KindLoc;
13442     }
13443     Value += "'";
13444     Diag(Loc, diag::err_omp_unexpected_clause_value)
13445         << Value << getOpenMPClauseName(OMPC_defaultmap);
13446     return nullptr;
13447   }
13448   DSAStack->setDefaultDMAToFromScalar(StartLoc);
13449 
13450   return new (Context)
13451       OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
13452 }
13453 
13454 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
13455   DeclContext *CurLexicalContext = getCurLexicalContext();
13456   if (!CurLexicalContext->isFileContext() &&
13457       !CurLexicalContext->isExternCContext() &&
13458       !CurLexicalContext->isExternCXXContext() &&
13459       !isa<CXXRecordDecl>(CurLexicalContext) &&
13460       !isa<ClassTemplateDecl>(CurLexicalContext) &&
13461       !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
13462       !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
13463     Diag(Loc, diag::err_omp_region_not_file_context);
13464     return false;
13465   }
13466   ++DeclareTargetNestingLevel;
13467   return true;
13468 }
13469 
13470 void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
13471   assert(DeclareTargetNestingLevel > 0 &&
13472          "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
13473   --DeclareTargetNestingLevel;
13474 }
13475 
13476 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
13477                                         CXXScopeSpec &ScopeSpec,
13478                                         const DeclarationNameInfo &Id,
13479                                         OMPDeclareTargetDeclAttr::MapTypeTy MT,
13480                                         NamedDeclSetType &SameDirectiveDecls) {
13481   LookupResult Lookup(*this, Id, LookupOrdinaryName);
13482   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
13483 
13484   if (Lookup.isAmbiguous())
13485     return;
13486   Lookup.suppressDiagnostics();
13487 
13488   if (!Lookup.isSingleResult()) {
13489     if (TypoCorrection Corrected =
13490             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
13491                         llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
13492                         CTK_ErrorRecovery)) {
13493       diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
13494                                   << Id.getName());
13495       checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
13496       return;
13497     }
13498 
13499     Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
13500     return;
13501   }
13502 
13503   NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
13504   if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
13505       isa<FunctionTemplateDecl>(ND)) {
13506     if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
13507       Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
13508     llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13509         OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
13510             cast<ValueDecl>(ND));
13511     if (!Res) {
13512       auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
13513       ND->addAttr(A);
13514       if (ASTMutationListener *ML = Context.getASTMutationListener())
13515         ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
13516       checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
13517     } else if (*Res != MT) {
13518       Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
13519           << Id.getName();
13520     }
13521   } else {
13522     Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
13523   }
13524 }
13525 
13526 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
13527                                      Sema &SemaRef, Decl *D) {
13528   if (!D || !isa<VarDecl>(D))
13529     return;
13530   auto *VD = cast<VarDecl>(D);
13531   if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
13532     return;
13533   SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
13534   SemaRef.Diag(SL, diag::note_used_here) << SR;
13535 }
13536 
13537 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13538                                    Sema &SemaRef, DSAStackTy *Stack,
13539                                    ValueDecl *VD) {
13540   return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13541          checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13542                            /*FullCheck=*/false);
13543 }
13544 
13545 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13546                                             SourceLocation IdLoc) {
13547   if (!D || D->isInvalidDecl())
13548     return;
13549   SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
13550   SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
13551   if (auto *VD = dyn_cast<VarDecl>(D)) {
13552     // Only global variables can be marked as declare target.
13553     if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
13554         !VD->isStaticDataMember())
13555       return;
13556     // 2.10.6: threadprivate variable cannot appear in a declare target
13557     // directive.
13558     if (DSAStack->isThreadPrivate(VD)) {
13559       Diag(SL, diag::err_omp_threadprivate_in_target);
13560       reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
13561       return;
13562     }
13563   }
13564   if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
13565     D = FTD->getTemplatedDecl();
13566   if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
13567     llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13568         OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
13569     if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
13570       assert(IdLoc.isValid() && "Source location is expected");
13571       Diag(IdLoc, diag::err_omp_function_in_link_clause);
13572       Diag(FD->getLocation(), diag::note_defined_here) << FD;
13573       return;
13574     }
13575   }
13576   if (auto *VD = dyn_cast<ValueDecl>(D)) {
13577     // Problem if any with var declared with incomplete type will be reported
13578     // as normal, so no need to check it here.
13579     if ((E || !VD->getType()->isIncompleteType()) &&
13580         !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
13581       return;
13582     if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
13583       // Checking declaration inside declare target region.
13584       if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13585           isa<FunctionTemplateDecl>(D)) {
13586         auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13587             Context, OMPDeclareTargetDeclAttr::MT_To);
13588         D->addAttr(A);
13589         if (ASTMutationListener *ML = Context.getASTMutationListener())
13590           ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13591       }
13592       return;
13593     }
13594   }
13595   if (!E)
13596     return;
13597   checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13598 }
13599 
13600 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13601                                      SourceLocation StartLoc,
13602                                      SourceLocation LParenLoc,
13603                                      SourceLocation EndLoc) {
13604   MappableVarListInfo MVLI(VarList);
13605   checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13606   if (MVLI.ProcessedVarList.empty())
13607     return nullptr;
13608 
13609   return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13610                              MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13611                              MVLI.VarComponents);
13612 }
13613 
13614 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13615                                        SourceLocation StartLoc,
13616                                        SourceLocation LParenLoc,
13617                                        SourceLocation EndLoc) {
13618   MappableVarListInfo MVLI(VarList);
13619   checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13620   if (MVLI.ProcessedVarList.empty())
13621     return nullptr;
13622 
13623   return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13624                                MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13625                                MVLI.VarComponents);
13626 }
13627 
13628 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13629                                                SourceLocation StartLoc,
13630                                                SourceLocation LParenLoc,
13631                                                SourceLocation EndLoc) {
13632   MappableVarListInfo MVLI(VarList);
13633   SmallVector<Expr *, 8> PrivateCopies;
13634   SmallVector<Expr *, 8> Inits;
13635 
13636   for (Expr *RefExpr : VarList) {
13637     assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13638     SourceLocation ELoc;
13639     SourceRange ERange;
13640     Expr *SimpleRefExpr = RefExpr;
13641     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13642     if (Res.second) {
13643       // It will be analyzed later.
13644       MVLI.ProcessedVarList.push_back(RefExpr);
13645       PrivateCopies.push_back(nullptr);
13646       Inits.push_back(nullptr);
13647     }
13648     ValueDecl *D = Res.first;
13649     if (!D)
13650       continue;
13651 
13652     QualType Type = D->getType();
13653     Type = Type.getNonReferenceType().getUnqualifiedType();
13654 
13655     auto *VD = dyn_cast<VarDecl>(D);
13656 
13657     // Item should be a pointer or reference to pointer.
13658     if (!Type->isPointerType()) {
13659       Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13660           << 0 << RefExpr->getSourceRange();
13661       continue;
13662     }
13663 
13664     // Build the private variable and the expression that refers to it.
13665     auto VDPrivate =
13666         buildVarDecl(*this, ELoc, Type, D->getName(),
13667                      D->hasAttrs() ? &D->getAttrs() : nullptr,
13668                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
13669     if (VDPrivate->isInvalidDecl())
13670       continue;
13671 
13672     CurContext->addDecl(VDPrivate);
13673     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
13674         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13675 
13676     // Add temporary variable to initialize the private copy of the pointer.
13677     VarDecl *VDInit =
13678         buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
13679     DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13680         *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
13681     AddInitializerToDecl(VDPrivate,
13682                          DefaultLvalueConversion(VDInitRefExpr).get(),
13683                          /*DirectInit=*/false);
13684 
13685     // If required, build a capture to implement the privatization initialized
13686     // with the current list item value.
13687     DeclRefExpr *Ref = nullptr;
13688     if (!VD)
13689       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13690     MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13691     PrivateCopies.push_back(VDPrivateRefExpr);
13692     Inits.push_back(VDInitRefExpr);
13693 
13694     // We need to add a data sharing attribute for this variable to make sure it
13695     // is correctly captured. A variable that shows up in a use_device_ptr has
13696     // similar properties of a first private variable.
13697     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13698 
13699     // Create a mappable component for the list item. List items in this clause
13700     // only need a component.
13701     MVLI.VarBaseDeclarations.push_back(D);
13702     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13703     MVLI.VarComponents.back().push_back(
13704         OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
13705   }
13706 
13707   if (MVLI.ProcessedVarList.empty())
13708     return nullptr;
13709 
13710   return OMPUseDevicePtrClause::Create(
13711       Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13712       PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
13713 }
13714 
13715 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13716                                               SourceLocation StartLoc,
13717                                               SourceLocation LParenLoc,
13718                                               SourceLocation EndLoc) {
13719   MappableVarListInfo MVLI(VarList);
13720   for (Expr *RefExpr : VarList) {
13721     assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
13722     SourceLocation ELoc;
13723     SourceRange ERange;
13724     Expr *SimpleRefExpr = RefExpr;
13725     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13726     if (Res.second) {
13727       // It will be analyzed later.
13728       MVLI.ProcessedVarList.push_back(RefExpr);
13729     }
13730     ValueDecl *D = Res.first;
13731     if (!D)
13732       continue;
13733 
13734     QualType Type = D->getType();
13735     // item should be a pointer or array or reference to pointer or array
13736     if (!Type.getNonReferenceType()->isPointerType() &&
13737         !Type.getNonReferenceType()->isArrayType()) {
13738       Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13739           << 0 << RefExpr->getSourceRange();
13740       continue;
13741     }
13742 
13743     // Check if the declaration in the clause does not show up in any data
13744     // sharing attribute.
13745     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
13746     if (isOpenMPPrivate(DVar.CKind)) {
13747       Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13748           << getOpenMPClauseName(DVar.CKind)
13749           << getOpenMPClauseName(OMPC_is_device_ptr)
13750           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
13751       reportOriginalDsa(*this, DSAStack, D, DVar);
13752       continue;
13753     }
13754 
13755     const Expr *ConflictExpr;
13756     if (DSAStack->checkMappableExprComponentListsForDecl(
13757             D, /*CurrentRegionOnly=*/true,
13758             [&ConflictExpr](
13759                 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13760                 OpenMPClauseKind) -> bool {
13761               ConflictExpr = R.front().getAssociatedExpression();
13762               return true;
13763             })) {
13764       Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13765       Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13766           << ConflictExpr->getSourceRange();
13767       continue;
13768     }
13769 
13770     // Store the components in the stack so that they can be used to check
13771     // against other clauses later on.
13772     OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13773     DSAStack->addMappableExpressionComponents(
13774         D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13775 
13776     // Record the expression we've just processed.
13777     MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13778 
13779     // Create a mappable component for the list item. List items in this clause
13780     // only need a component. We use a null declaration to signal fields in
13781     // 'this'.
13782     assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13783             isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13784            "Unexpected device pointer expression!");
13785     MVLI.VarBaseDeclarations.push_back(
13786         isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13787     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13788     MVLI.VarComponents.back().push_back(MC);
13789   }
13790 
13791   if (MVLI.ProcessedVarList.empty())
13792     return nullptr;
13793 
13794   return OMPIsDevicePtrClause::Create(
13795       Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13796       MVLI.VarBaseDeclarations, MVLI.VarComponents);
13797 }
13798