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 /// \brief 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 Expr *CheckMapClauseExpressionBase(
39     Sema &SemaRef, Expr *E,
40     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
41     OpenMPClauseKind CKind, bool NoDiagnose);
42 
43 namespace {
44 /// \brief Default data sharing attributes, which can be applied to directive.
45 enum DefaultDataSharingAttributes {
46   DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
47   DSA_none = 1 << 0,   /// \brief Default data sharing attribute 'none'.
48   DSA_shared = 1 << 1, /// \brief 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 /// \brief Stack for tracking declarations used in OpenMP directives and
58 /// clauses and their data-sharing attributes.
59 class DSAStackTy final {
60 public:
61   struct DSAVarData final {
62     OpenMPDirectiveKind DKind = OMPD_unknown;
63     OpenMPClauseKind CKind = OMPC_unknown;
64     Expr *RefExpr = nullptr;
65     DeclRefExpr *PrivateCopy = nullptr;
66     SourceLocation ImplicitDSALoc;
67     DSAVarData() = default;
68     DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, Expr *RefExpr,
69                DeclRefExpr *PrivateCopy, SourceLocation ImplicitDSALoc)
70         : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
71           PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
72   };
73   typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>
74       OperatorOffsetTy;
75 
76 private:
77   struct DSAInfo final {
78     OpenMPClauseKind Attributes = OMPC_unknown;
79     /// Pointer to a reference expression and a flag which shows that the
80     /// variable is marked as lastprivate(true) or not (false).
81     llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
82     DeclRefExpr *PrivateCopy = nullptr;
83   };
84   typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
85   typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
86   typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
87   typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
88   /// Struct that associates a component with the clause kind where they are
89   /// found.
90   struct MappedExprComponentTy {
91     OMPClauseMappableExprCommon::MappableExprComponentLists Components;
92     OpenMPClauseKind Kind = OMPC_unknown;
93   };
94   typedef llvm::DenseMap<ValueDecl *, MappedExprComponentTy>
95       MappedExprComponentsTy;
96   typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
97       CriticalsWithHintsTy;
98   typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
99       DoacrossDependMapTy;
100   struct ReductionData {
101     typedef llvm::PointerEmbeddedInt<BinaryOperatorKind, 16> BOKPtrType;
102     SourceRange ReductionRange;
103     llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
104     ReductionData() = default;
105     void set(BinaryOperatorKind BO, SourceRange RR) {
106       ReductionRange = RR;
107       ReductionOp = BO;
108     }
109     void set(const Expr *RefExpr, SourceRange RR) {
110       ReductionRange = RR;
111       ReductionOp = RefExpr;
112     }
113   };
114   typedef llvm::DenseMap<ValueDecl *, ReductionData> DeclReductionMapTy;
115 
116   struct SharingMapTy final {
117     DeclSAMapTy SharingMap;
118     DeclReductionMapTy ReductionMap;
119     AlignedMapTy AlignedMap;
120     MappedExprComponentsTy MappedExprComponents;
121     LoopControlVariablesMapTy LCVMap;
122     DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
123     SourceLocation DefaultAttrLoc;
124     DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
125     SourceLocation DefaultMapAttrLoc;
126     OpenMPDirectiveKind Directive = OMPD_unknown;
127     DeclarationNameInfo DirectiveName;
128     Scope *CurScope = nullptr;
129     SourceLocation ConstructLoc;
130     /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
131     /// get the data (loop counters etc.) about enclosing loop-based construct.
132     /// This data is required during codegen.
133     DoacrossDependMapTy DoacrossDepends;
134     /// \brief first argument (Expr *) contains optional argument of the
135     /// 'ordered' clause, the second one is true if the regions has 'ordered'
136     /// clause, false otherwise.
137     llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
138     bool NowaitRegion = false;
139     bool CancelRegion = false;
140     unsigned AssociatedLoops = 1;
141     SourceLocation InnerTeamsRegionLoc;
142     /// Reference to the taskgroup task_reduction reference expression.
143     Expr *TaskgroupReductionRef = nullptr;
144     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
145                  Scope *CurScope, SourceLocation Loc)
146         : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
147           ConstructLoc(Loc) {}
148     SharingMapTy() = default;
149   };
150 
151   typedef SmallVector<SharingMapTy, 4> StackTy;
152 
153   /// \brief Stack of used declaration and their data-sharing attributes.
154   DeclSAMapTy Threadprivates;
155   const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
156   SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
157   /// \brief true, if check for DSA must be from parent directive, false, if
158   /// from current directive.
159   OpenMPClauseKind ClauseKindMode = OMPC_unknown;
160   Sema &SemaRef;
161   bool ForceCapturing = false;
162   CriticalsWithHintsTy Criticals;
163 
164   typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
165 
166   DSAVarData getDSA(StackTy::reverse_iterator &Iter, ValueDecl *D);
167 
168   /// \brief Checks if the variable is a local for OpenMP region.
169   bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
170 
171   bool isStackEmpty() const {
172     return Stack.empty() ||
173            Stack.back().second != CurrentNonCapturingFunctionScope ||
174            Stack.back().first.empty();
175   }
176 
177 public:
178   explicit DSAStackTy(Sema &S) : SemaRef(S) {}
179 
180   bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
181   OpenMPClauseKind getClauseParsingMode() const {
182     assert(isClauseParsingMode() && "Must be in clause parsing mode.");
183     return ClauseKindMode;
184   }
185   void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
186 
187   bool isForceVarCapturing() const { return ForceCapturing; }
188   void setForceVarCapturing(bool V) { ForceCapturing = V; }
189 
190   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
191             Scope *CurScope, SourceLocation Loc) {
192     if (Stack.empty() ||
193         Stack.back().second != CurrentNonCapturingFunctionScope)
194       Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
195     Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
196     Stack.back().first.back().DefaultAttrLoc = Loc;
197   }
198 
199   void pop() {
200     assert(!Stack.back().first.empty() &&
201            "Data-sharing attributes stack is empty!");
202     Stack.back().first.pop_back();
203   }
204 
205   /// Start new OpenMP region stack in new non-capturing function.
206   void pushFunction() {
207     const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
208     assert(!isa<CapturingScopeInfo>(CurFnScope));
209     CurrentNonCapturingFunctionScope = CurFnScope;
210   }
211   /// Pop region stack for non-capturing function.
212   void popFunction(const FunctionScopeInfo *OldFSI) {
213     if (!Stack.empty() && Stack.back().second == OldFSI) {
214       assert(Stack.back().first.empty());
215       Stack.pop_back();
216     }
217     CurrentNonCapturingFunctionScope = nullptr;
218     for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
219       if (!isa<CapturingScopeInfo>(FSI)) {
220         CurrentNonCapturingFunctionScope = FSI;
221         break;
222       }
223     }
224   }
225 
226   void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
227     Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
228   }
229   const std::pair<OMPCriticalDirective *, llvm::APSInt>
230   getCriticalWithHint(const DeclarationNameInfo &Name) const {
231     auto I = Criticals.find(Name.getAsString());
232     if (I != Criticals.end())
233       return I->second;
234     return std::make_pair(nullptr, llvm::APSInt());
235   }
236   /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
237   /// add it and return NULL; otherwise return previous occurrence's expression
238   /// for diagnostics.
239   Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
240 
241   /// \brief Register specified variable as loop control variable.
242   void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
243   /// \brief Check if the specified variable is a loop control variable for
244   /// current region.
245   /// \return The index of the loop control variable in the list of associated
246   /// for-loops (from outer to inner).
247   LCDeclInfo isLoopControlVariable(ValueDecl *D);
248   /// \brief Check if the specified variable is a loop control variable for
249   /// parent region.
250   /// \return The index of the loop control variable in the list of associated
251   /// for-loops (from outer to inner).
252   LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
253   /// \brief Get the loop control variable for the I-th loop (or nullptr) in
254   /// parent directive.
255   ValueDecl *getParentLoopControlVariable(unsigned I);
256 
257   /// \brief Adds explicit data sharing attribute to the specified declaration.
258   void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
259               DeclRefExpr *PrivateCopy = nullptr);
260 
261   /// Adds additional information for the reduction items with the reduction id
262   /// represented as an operator.
263   void addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
264                                  BinaryOperatorKind BOK);
265   /// Adds additional information for the reduction items with the reduction id
266   /// represented as reduction identifier.
267   void addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
268                                  const Expr *ReductionRef);
269   /// Returns the location and reduction operation from the innermost parent
270   /// region for the given \p D.
271   DSAVarData getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
272                                               BinaryOperatorKind &BOK,
273                                               Expr *&TaskgroupDescriptor);
274   /// Returns the location and reduction operation from the innermost parent
275   /// region for the given \p D.
276   DSAVarData getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
277                                               const Expr *&ReductionRef,
278                                               Expr *&TaskgroupDescriptor);
279   /// Return reduction reference expression for the current taskgroup.
280   Expr *getTaskgroupReductionRef() const {
281     assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
282            "taskgroup reference expression requested for non taskgroup "
283            "directive.");
284     return Stack.back().first.back().TaskgroupReductionRef;
285   }
286   /// Checks if the given \p VD declaration is actually a taskgroup reduction
287   /// descriptor variable at the \p Level of OpenMP regions.
288   bool isTaskgroupReductionRef(ValueDecl *VD, unsigned Level) const {
289     return Stack.back().first[Level].TaskgroupReductionRef &&
290            cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
291                    ->getDecl() == VD;
292   }
293 
294   /// \brief Returns data sharing attributes from top of the stack for the
295   /// specified declaration.
296   DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
297   /// \brief Returns data-sharing attributes for the specified declaration.
298   DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
299   /// \brief Checks if the specified variables has data-sharing attributes which
300   /// match specified \a CPred predicate in any directive which matches \a DPred
301   /// predicate.
302   DSAVarData hasDSA(ValueDecl *D,
303                     const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
304                     const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
305                     bool FromParent);
306   /// \brief Checks if the specified variables has data-sharing attributes which
307   /// match specified \a CPred predicate in any innermost directive which
308   /// matches \a DPred predicate.
309   DSAVarData
310   hasInnermostDSA(ValueDecl *D,
311                   const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
312                   const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
313                   bool FromParent);
314   /// \brief Checks if the specified variables has explicit data-sharing
315   /// attributes which match specified \a CPred predicate at the specified
316   /// OpenMP region.
317   bool hasExplicitDSA(ValueDecl *D,
318                       const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
319                       unsigned Level, bool NotLastprivate = false);
320 
321   /// \brief Returns true if the directive at level \Level matches in the
322   /// specified \a DPred predicate.
323   bool hasExplicitDirective(
324       const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
325       unsigned Level);
326 
327   /// \brief Finds a directive which matches specified \a DPred predicate.
328   bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
329                                                   const DeclarationNameInfo &,
330                                                   SourceLocation)> &DPred,
331                     bool FromParent);
332 
333   /// \brief Returns currently analyzed directive.
334   OpenMPDirectiveKind getCurrentDirective() const {
335     return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
336   }
337   /// \brief Returns directive kind at specified level.
338   OpenMPDirectiveKind getDirective(unsigned Level) const {
339     assert(!isStackEmpty() && "No directive at specified level.");
340     return Stack.back().first[Level].Directive;
341   }
342   /// \brief Returns parent directive.
343   OpenMPDirectiveKind getParentDirective() const {
344     if (isStackEmpty() || Stack.back().first.size() == 1)
345       return OMPD_unknown;
346     return std::next(Stack.back().first.rbegin())->Directive;
347   }
348 
349   /// \brief Set default data sharing attribute to none.
350   void setDefaultDSANone(SourceLocation Loc) {
351     assert(!isStackEmpty());
352     Stack.back().first.back().DefaultAttr = DSA_none;
353     Stack.back().first.back().DefaultAttrLoc = Loc;
354   }
355   /// \brief Set default data sharing attribute to shared.
356   void setDefaultDSAShared(SourceLocation Loc) {
357     assert(!isStackEmpty());
358     Stack.back().first.back().DefaultAttr = DSA_shared;
359     Stack.back().first.back().DefaultAttrLoc = Loc;
360   }
361   /// Set default data mapping attribute to 'tofrom:scalar'.
362   void setDefaultDMAToFromScalar(SourceLocation Loc) {
363     assert(!isStackEmpty());
364     Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
365     Stack.back().first.back().DefaultMapAttrLoc = Loc;
366   }
367 
368   DefaultDataSharingAttributes getDefaultDSA() const {
369     return isStackEmpty() ? DSA_unspecified
370                           : Stack.back().first.back().DefaultAttr;
371   }
372   SourceLocation getDefaultDSALocation() const {
373     return isStackEmpty() ? SourceLocation()
374                           : Stack.back().first.back().DefaultAttrLoc;
375   }
376   DefaultMapAttributes getDefaultDMA() const {
377     return isStackEmpty() ? DMA_unspecified
378                           : Stack.back().first.back().DefaultMapAttr;
379   }
380   DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
381     return Stack.back().first[Level].DefaultMapAttr;
382   }
383   SourceLocation getDefaultDMALocation() const {
384     return isStackEmpty() ? SourceLocation()
385                           : Stack.back().first.back().DefaultMapAttrLoc;
386   }
387 
388   /// \brief Checks if the specified variable is a threadprivate.
389   bool isThreadPrivate(VarDecl *D) {
390     DSAVarData DVar = getTopDSA(D, false);
391     return isOpenMPThreadPrivate(DVar.CKind);
392   }
393 
394   /// \brief Marks current region as ordered (it has an 'ordered' clause).
395   void setOrderedRegion(bool IsOrdered, Expr *Param) {
396     assert(!isStackEmpty());
397     Stack.back().first.back().OrderedRegion.setInt(IsOrdered);
398     Stack.back().first.back().OrderedRegion.setPointer(Param);
399   }
400   /// \brief Returns true, if parent region is ordered (has associated
401   /// 'ordered' clause), false - otherwise.
402   bool isParentOrderedRegion() const {
403     if (isStackEmpty() || Stack.back().first.size() == 1)
404       return false;
405     return std::next(Stack.back().first.rbegin())->OrderedRegion.getInt();
406   }
407   /// \brief Returns optional parameter for the ordered region.
408   Expr *getParentOrderedRegionParam() const {
409     if (isStackEmpty() || Stack.back().first.size() == 1)
410       return nullptr;
411     return std::next(Stack.back().first.rbegin())->OrderedRegion.getPointer();
412   }
413   /// \brief Marks current region as nowait (it has a 'nowait' clause).
414   void setNowaitRegion(bool IsNowait = true) {
415     assert(!isStackEmpty());
416     Stack.back().first.back().NowaitRegion = IsNowait;
417   }
418   /// \brief Returns true, if parent region is nowait (has associated
419   /// 'nowait' clause), false - otherwise.
420   bool isParentNowaitRegion() const {
421     if (isStackEmpty() || Stack.back().first.size() == 1)
422       return false;
423     return std::next(Stack.back().first.rbegin())->NowaitRegion;
424   }
425   /// \brief Marks parent region as cancel region.
426   void setParentCancelRegion(bool Cancel = true) {
427     if (!isStackEmpty() && Stack.back().first.size() > 1) {
428       auto &StackElemRef = *std::next(Stack.back().first.rbegin());
429       StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
430     }
431   }
432   /// \brief Return true if current region has inner cancel construct.
433   bool isCancelRegion() const {
434     return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
435   }
436 
437   /// \brief Set collapse value for the region.
438   void setAssociatedLoops(unsigned Val) {
439     assert(!isStackEmpty());
440     Stack.back().first.back().AssociatedLoops = Val;
441   }
442   /// \brief Return collapse value for region.
443   unsigned getAssociatedLoops() const {
444     return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
445   }
446 
447   /// \brief Marks current target region as one with closely nested teams
448   /// region.
449   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
450     if (!isStackEmpty() && Stack.back().first.size() > 1) {
451       std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
452           TeamsRegionLoc;
453     }
454   }
455   /// \brief Returns true, if current region has closely nested teams region.
456   bool hasInnerTeamsRegion() const {
457     return getInnerTeamsRegionLoc().isValid();
458   }
459   /// \brief Returns location of the nested teams region (if any).
460   SourceLocation getInnerTeamsRegionLoc() const {
461     return isStackEmpty() ? SourceLocation()
462                           : Stack.back().first.back().InnerTeamsRegionLoc;
463   }
464 
465   Scope *getCurScope() const {
466     return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
467   }
468   Scope *getCurScope() {
469     return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
470   }
471   SourceLocation getConstructLoc() {
472     return isStackEmpty() ? SourceLocation()
473                           : Stack.back().first.back().ConstructLoc;
474   }
475 
476   /// Do the check specified in \a Check to all component lists and return true
477   /// if any issue is found.
478   bool checkMappableExprComponentListsForDecl(
479       ValueDecl *VD, bool CurrentRegionOnly,
480       const llvm::function_ref<
481           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
482                OpenMPClauseKind)> &Check) {
483     if (isStackEmpty())
484       return false;
485     auto SI = Stack.back().first.rbegin();
486     auto SE = Stack.back().first.rend();
487 
488     if (SI == SE)
489       return false;
490 
491     if (CurrentRegionOnly) {
492       SE = std::next(SI);
493     } else {
494       ++SI;
495     }
496 
497     for (; SI != SE; ++SI) {
498       auto MI = SI->MappedExprComponents.find(VD);
499       if (MI != SI->MappedExprComponents.end())
500         for (auto &L : MI->second.Components)
501           if (Check(L, MI->second.Kind))
502             return true;
503     }
504     return false;
505   }
506 
507   /// Do the check specified in \a Check to all component lists at a given level
508   /// and return true if any issue is found.
509   bool checkMappableExprComponentListsForDeclAtLevel(
510       ValueDecl *VD, unsigned Level,
511       const llvm::function_ref<
512           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
513                OpenMPClauseKind)> &Check) {
514     if (isStackEmpty())
515       return false;
516 
517     auto StartI = Stack.back().first.begin();
518     auto EndI = Stack.back().first.end();
519     if (std::distance(StartI, EndI) <= (int)Level)
520       return false;
521     std::advance(StartI, Level);
522 
523     auto MI = StartI->MappedExprComponents.find(VD);
524     if (MI != StartI->MappedExprComponents.end())
525       for (auto &L : MI->second.Components)
526         if (Check(L, MI->second.Kind))
527           return true;
528     return false;
529   }
530 
531   /// Create a new mappable expression component list associated with a given
532   /// declaration and initialize it with the provided list of components.
533   void addMappableExpressionComponents(
534       ValueDecl *VD,
535       OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
536       OpenMPClauseKind WhereFoundClauseKind) {
537     assert(!isStackEmpty() &&
538            "Not expecting to retrieve components from a empty stack!");
539     auto &MEC = Stack.back().first.back().MappedExprComponents[VD];
540     // Create new entry and append the new components there.
541     MEC.Components.resize(MEC.Components.size() + 1);
542     MEC.Components.back().append(Components.begin(), Components.end());
543     MEC.Kind = WhereFoundClauseKind;
544   }
545 
546   unsigned getNestingLevel() const {
547     assert(!isStackEmpty());
548     return Stack.back().first.size() - 1;
549   }
550   void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
551     assert(!isStackEmpty() && Stack.back().first.size() > 1);
552     auto &StackElem = *std::next(Stack.back().first.rbegin());
553     assert(isOpenMPWorksharingDirective(StackElem.Directive));
554     StackElem.DoacrossDepends.insert({C, OpsOffs});
555   }
556   llvm::iterator_range<DoacrossDependMapTy::const_iterator>
557   getDoacrossDependClauses() const {
558     assert(!isStackEmpty());
559     auto &StackElem = Stack.back().first.back();
560     if (isOpenMPWorksharingDirective(StackElem.Directive)) {
561       auto &Ref = StackElem.DoacrossDepends;
562       return llvm::make_range(Ref.begin(), Ref.end());
563     }
564     return llvm::make_range(StackElem.DoacrossDepends.end(),
565                             StackElem.DoacrossDepends.end());
566   }
567 };
568 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
569   return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
570          isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
571 }
572 } // namespace
573 
574 static Expr *getExprAsWritten(Expr *E) {
575   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
576     E = ExprTemp->getSubExpr();
577 
578   if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
579     E = MTE->GetTemporaryExpr();
580 
581   while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
582     E = Binder->getSubExpr();
583 
584   if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
585     E = ICE->getSubExprAsWritten();
586   return E->IgnoreParens();
587 }
588 
589 static ValueDecl *getCanonicalDecl(ValueDecl *D) {
590   if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
591     if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
592       D = ME->getMemberDecl();
593   auto *VD = dyn_cast<VarDecl>(D);
594   auto *FD = dyn_cast<FieldDecl>(D);
595   if (VD != nullptr) {
596     VD = VD->getCanonicalDecl();
597     D = VD;
598   } else {
599     assert(FD);
600     FD = FD->getCanonicalDecl();
601     D = FD;
602   }
603   return D;
604 }
605 
606 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator &Iter,
607                                           ValueDecl *D) {
608   D = getCanonicalDecl(D);
609   auto *VD = dyn_cast<VarDecl>(D);
610   auto *FD = dyn_cast<FieldDecl>(D);
611   DSAVarData DVar;
612   if (isStackEmpty() || Iter == Stack.back().first.rend()) {
613     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
614     // in a region but not in construct]
615     //  File-scope or namespace-scope variables referenced in called routines
616     //  in the region are shared unless they appear in a threadprivate
617     //  directive.
618     if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
619       DVar.CKind = OMPC_shared;
620 
621     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
622     // in a region but not in construct]
623     //  Variables with static storage duration that are declared in called
624     //  routines in the region are shared.
625     if (VD && VD->hasGlobalStorage())
626       DVar.CKind = OMPC_shared;
627 
628     // Non-static data members are shared by default.
629     if (FD)
630       DVar.CKind = OMPC_shared;
631 
632     return DVar;
633   }
634 
635   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
636   // in a Construct, C/C++, predetermined, p.1]
637   // Variables with automatic storage duration that are declared in a scope
638   // inside the construct are private.
639   if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
640       (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
641     DVar.CKind = OMPC_private;
642     return DVar;
643   }
644 
645   DVar.DKind = Iter->Directive;
646   // Explicitly specified attributes and local variables with predetermined
647   // attributes.
648   if (Iter->SharingMap.count(D)) {
649     DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
650     DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
651     DVar.CKind = Iter->SharingMap[D].Attributes;
652     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
653     return DVar;
654   }
655 
656   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
657   // in a Construct, C/C++, implicitly determined, p.1]
658   //  In a parallel or task construct, the data-sharing attributes of these
659   //  variables are determined by the default clause, if present.
660   switch (Iter->DefaultAttr) {
661   case DSA_shared:
662     DVar.CKind = OMPC_shared;
663     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
664     return DVar;
665   case DSA_none:
666     return DVar;
667   case DSA_unspecified:
668     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
669     // in a Construct, implicitly determined, p.2]
670     //  In a parallel construct, if no default clause is present, these
671     //  variables are shared.
672     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
673     if (isOpenMPParallelDirective(DVar.DKind) ||
674         isOpenMPTeamsDirective(DVar.DKind)) {
675       DVar.CKind = OMPC_shared;
676       return DVar;
677     }
678 
679     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
680     // in a Construct, implicitly determined, p.4]
681     //  In a task construct, if no default clause is present, a variable that in
682     //  the enclosing context is determined to be shared by all implicit tasks
683     //  bound to the current team is shared.
684     if (isOpenMPTaskingDirective(DVar.DKind)) {
685       DSAVarData DVarTemp;
686       auto I = Iter, E = Stack.back().first.rend();
687       do {
688         ++I;
689         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
690         // Referenced in a Construct, implicitly determined, p.6]
691         //  In a task construct, if no default clause is present, a variable
692         //  whose data-sharing attribute is not determined by the rules above is
693         //  firstprivate.
694         DVarTemp = getDSA(I, D);
695         if (DVarTemp.CKind != OMPC_shared) {
696           DVar.RefExpr = nullptr;
697           DVar.CKind = OMPC_firstprivate;
698           return DVar;
699         }
700       } while (I != E && !isParallelOrTaskRegion(I->Directive));
701       DVar.CKind =
702           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
703       return DVar;
704     }
705   }
706   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
707   // in a Construct, implicitly determined, p.3]
708   //  For constructs other than task, if no default clause is present, these
709   //  variables inherit their data-sharing attributes from the enclosing
710   //  context.
711   return getDSA(++Iter, D);
712 }
713 
714 Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
715   assert(!isStackEmpty() && "Data sharing attributes stack is empty");
716   D = getCanonicalDecl(D);
717   auto &StackElem = Stack.back().first.back();
718   auto It = StackElem.AlignedMap.find(D);
719   if (It == StackElem.AlignedMap.end()) {
720     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
721     StackElem.AlignedMap[D] = NewDE;
722     return nullptr;
723   } else {
724     assert(It->second && "Unexpected nullptr expr in the aligned map");
725     return It->second;
726   }
727   return nullptr;
728 }
729 
730 void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
731   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
732   D = getCanonicalDecl(D);
733   auto &StackElem = Stack.back().first.back();
734   StackElem.LCVMap.insert(
735       {D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)});
736 }
737 
738 DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
739   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
740   D = getCanonicalDecl(D);
741   auto &StackElem = Stack.back().first.back();
742   auto It = StackElem.LCVMap.find(D);
743   if (It != StackElem.LCVMap.end())
744     return It->second;
745   return {0, nullptr};
746 }
747 
748 DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
749   assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
750          "Data-sharing attributes stack is empty");
751   D = getCanonicalDecl(D);
752   auto &StackElem = *std::next(Stack.back().first.rbegin());
753   auto It = StackElem.LCVMap.find(D);
754   if (It != StackElem.LCVMap.end())
755     return It->second;
756   return {0, nullptr};
757 }
758 
759 ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
760   assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
761          "Data-sharing attributes stack is empty");
762   auto &StackElem = *std::next(Stack.back().first.rbegin());
763   if (StackElem.LCVMap.size() < I)
764     return nullptr;
765   for (auto &Pair : StackElem.LCVMap)
766     if (Pair.second.first == I)
767       return Pair.first;
768   return nullptr;
769 }
770 
771 void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
772                         DeclRefExpr *PrivateCopy) {
773   D = getCanonicalDecl(D);
774   if (A == OMPC_threadprivate) {
775     auto &Data = Threadprivates[D];
776     Data.Attributes = A;
777     Data.RefExpr.setPointer(E);
778     Data.PrivateCopy = nullptr;
779   } else {
780     assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
781     auto &Data = Stack.back().first.back().SharingMap[D];
782     assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
783            (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
784            (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
785            (isLoopControlVariable(D).first && A == OMPC_private));
786     if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
787       Data.RefExpr.setInt(/*IntVal=*/true);
788       return;
789     }
790     const bool IsLastprivate =
791         A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
792     Data.Attributes = A;
793     Data.RefExpr.setPointerAndInt(E, IsLastprivate);
794     Data.PrivateCopy = PrivateCopy;
795     if (PrivateCopy) {
796       auto &Data = Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
797       Data.Attributes = A;
798       Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
799       Data.PrivateCopy = nullptr;
800     }
801   }
802 }
803 
804 /// \brief Build a variable declaration for OpenMP loop iteration variable.
805 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
806                              StringRef Name, const AttrVec *Attrs = nullptr,
807                              DeclRefExpr *OrigRef = nullptr) {
808   DeclContext *DC = SemaRef.CurContext;
809   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
810   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
811   VarDecl *Decl =
812       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
813   if (Attrs) {
814     for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
815          I != E; ++I)
816       Decl->addAttr(*I);
817   }
818   Decl->setImplicit();
819   if (OrigRef) {
820     Decl->addAttr(
821         OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
822   }
823   return Decl;
824 }
825 
826 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
827                                      SourceLocation Loc,
828                                      bool RefersToCapture = false) {
829   D->setReferenced();
830   D->markUsed(S.Context);
831   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
832                              SourceLocation(), D, RefersToCapture, Loc, Ty,
833                              VK_LValue);
834 }
835 
836 void DSAStackTy::addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
837                                            BinaryOperatorKind BOK) {
838   D = getCanonicalDecl(D);
839   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
840   assert(
841       Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
842       "Additional reduction info may be specified only for reduction items.");
843   auto &ReductionData = Stack.back().first.back().ReductionMap[D];
844   assert(ReductionData.ReductionRange.isInvalid() &&
845          Stack.back().first.back().Directive == OMPD_taskgroup &&
846          "Additional reduction info may be specified only once for reduction "
847          "items.");
848   ReductionData.set(BOK, SR);
849   Expr *&TaskgroupReductionRef =
850       Stack.back().first.back().TaskgroupReductionRef;
851   if (!TaskgroupReductionRef) {
852     auto *VD = buildVarDecl(SemaRef, SR.getBegin(),
853                             SemaRef.Context.VoidPtrTy, ".task_red.");
854     TaskgroupReductionRef =
855         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
856   }
857 }
858 
859 void DSAStackTy::addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
860                                            const Expr *ReductionRef) {
861   D = getCanonicalDecl(D);
862   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
863   assert(
864       Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
865       "Additional reduction info may be specified only for reduction items.");
866   auto &ReductionData = Stack.back().first.back().ReductionMap[D];
867   assert(ReductionData.ReductionRange.isInvalid() &&
868          Stack.back().first.back().Directive == OMPD_taskgroup &&
869          "Additional reduction info may be specified only once for reduction "
870          "items.");
871   ReductionData.set(ReductionRef, SR);
872   Expr *&TaskgroupReductionRef =
873       Stack.back().first.back().TaskgroupReductionRef;
874   if (!TaskgroupReductionRef) {
875     auto *VD = buildVarDecl(SemaRef, SR.getBegin(), SemaRef.Context.VoidPtrTy,
876                             ".task_red.");
877     TaskgroupReductionRef =
878         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
879   }
880 }
881 
882 DSAStackTy::DSAVarData
883 DSAStackTy::getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
884                                              BinaryOperatorKind &BOK,
885                                              Expr *&TaskgroupDescriptor) {
886   D = getCanonicalDecl(D);
887   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
888   if (Stack.back().first.empty())
889       return DSAVarData();
890   for (auto I = std::next(Stack.back().first.rbegin(), 1),
891             E = Stack.back().first.rend();
892        I != E; std::advance(I, 1)) {
893     auto &Data = I->SharingMap[D];
894     if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
895       continue;
896     auto &ReductionData = I->ReductionMap[D];
897     if (!ReductionData.ReductionOp ||
898         ReductionData.ReductionOp.is<const Expr *>())
899       return DSAVarData();
900     SR = ReductionData.ReductionRange;
901     BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
902     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
903                                        "expression for the descriptor is not "
904                                        "set.");
905     TaskgroupDescriptor = I->TaskgroupReductionRef;
906     return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
907                       Data.PrivateCopy, I->DefaultAttrLoc);
908   }
909   return DSAVarData();
910 }
911 
912 DSAStackTy::DSAVarData
913 DSAStackTy::getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
914                                              const Expr *&ReductionRef,
915                                              Expr *&TaskgroupDescriptor) {
916   D = getCanonicalDecl(D);
917   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
918   if (Stack.back().first.empty())
919       return DSAVarData();
920   for (auto I = std::next(Stack.back().first.rbegin(), 1),
921             E = Stack.back().first.rend();
922        I != E; std::advance(I, 1)) {
923     auto &Data = I->SharingMap[D];
924     if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
925       continue;
926     auto &ReductionData = I->ReductionMap[D];
927     if (!ReductionData.ReductionOp ||
928         !ReductionData.ReductionOp.is<const Expr *>())
929       return DSAVarData();
930     SR = ReductionData.ReductionRange;
931     ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
932     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
933                                        "expression for the descriptor is not "
934                                        "set.");
935     TaskgroupDescriptor = I->TaskgroupReductionRef;
936     return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
937                       Data.PrivateCopy, I->DefaultAttrLoc);
938   }
939   return DSAVarData();
940 }
941 
942 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
943   D = D->getCanonicalDecl();
944   if (!isStackEmpty()) {
945     reverse_iterator I = Iter, E = Stack.back().first.rend();
946     Scope *TopScope = nullptr;
947     while (I != E && !isParallelOrTaskRegion(I->Directive) &&
948            !isOpenMPTargetExecutionDirective(I->Directive))
949       ++I;
950     if (I == E)
951       return false;
952     TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
953     Scope *CurScope = getCurScope();
954     while (CurScope != TopScope && !CurScope->isDeclScope(D))
955       CurScope = CurScope->getParent();
956     return CurScope != TopScope;
957   }
958   return false;
959 }
960 
961 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
962   D = getCanonicalDecl(D);
963   DSAVarData DVar;
964 
965   auto *VD = dyn_cast<VarDecl>(D);
966   auto TI = Threadprivates.find(D);
967   if (TI != Threadprivates.end()) {
968     DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
969     DVar.CKind = OMPC_threadprivate;
970     return DVar;
971   } else if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
972     DVar.RefExpr = buildDeclRefExpr(
973         SemaRef, VD, D->getType().getNonReferenceType(),
974         VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
975     DVar.CKind = OMPC_threadprivate;
976     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
977     return DVar;
978   }
979   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
980   // in a Construct, C/C++, predetermined, p.1]
981   //  Variables appearing in threadprivate directives are threadprivate.
982   if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
983        !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
984          SemaRef.getLangOpts().OpenMPUseTLS &&
985          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
986       (VD && VD->getStorageClass() == SC_Register &&
987        VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
988     DVar.RefExpr = buildDeclRefExpr(
989         SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
990     DVar.CKind = OMPC_threadprivate;
991     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
992     return DVar;
993   }
994   if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
995       VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
996       !isLoopControlVariable(D).first) {
997     auto IterTarget =
998         std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
999                      [](const SharingMapTy &Data) {
1000                        return isOpenMPTargetExecutionDirective(Data.Directive);
1001                      });
1002     if (IterTarget != Stack.back().first.rend()) {
1003       auto ParentIterTarget = std::next(IterTarget, 1);
1004       auto Iter = Stack.back().first.rbegin();
1005       while (Iter != ParentIterTarget) {
1006         if (isOpenMPLocal(VD, Iter)) {
1007           DVar.RefExpr =
1008               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1009                                D->getLocation());
1010           DVar.CKind = OMPC_threadprivate;
1011           return DVar;
1012         }
1013         std::advance(Iter, 1);
1014       }
1015       if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1016         auto DSAIter = IterTarget->SharingMap.find(D);
1017         if (DSAIter != IterTarget->SharingMap.end() &&
1018             isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1019           DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1020           DVar.CKind = OMPC_threadprivate;
1021           return DVar;
1022         } else if (!SemaRef.IsOpenMPCapturedByRef(
1023                        D, std::distance(ParentIterTarget,
1024                                         Stack.back().first.rend()))) {
1025           DVar.RefExpr =
1026               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1027                                IterTarget->ConstructLoc);
1028           DVar.CKind = OMPC_threadprivate;
1029           return DVar;
1030         }
1031       }
1032     }
1033   }
1034 
1035   if (isStackEmpty())
1036     // Not in OpenMP execution region and top scope was already checked.
1037     return DVar;
1038 
1039   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1040   // in a Construct, C/C++, predetermined, p.4]
1041   //  Static data members are shared.
1042   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1043   // in a Construct, C/C++, predetermined, p.7]
1044   //  Variables with static storage duration that are declared in a scope
1045   //  inside the construct are shared.
1046   auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
1047   if (VD && VD->isStaticDataMember()) {
1048     DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
1049     if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1050       return DVar;
1051 
1052     DVar.CKind = OMPC_shared;
1053     return DVar;
1054   }
1055 
1056   QualType Type = D->getType().getNonReferenceType().getCanonicalType();
1057   bool IsConstant = Type.isConstant(SemaRef.getASTContext());
1058   Type = SemaRef.getASTContext().getBaseElementType(Type);
1059   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1060   // in a Construct, C/C++, predetermined, p.6]
1061   //  Variables with const qualified type having no mutable member are
1062   //  shared.
1063   CXXRecordDecl *RD =
1064       SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
1065   if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1066     if (auto *CTD = CTSD->getSpecializedTemplate())
1067       RD = CTD->getTemplatedDecl();
1068   if (IsConstant &&
1069       !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
1070         RD->hasMutableFields())) {
1071     // Variables with const-qualified type having no mutable member may be
1072     // listed in a firstprivate clause, even if they are static data members.
1073     DSAVarData DVarTemp = hasDSA(
1074         D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
1075         MatchesAlways, FromParent);
1076     if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
1077       return DVarTemp;
1078 
1079     DVar.CKind = OMPC_shared;
1080     return DVar;
1081   }
1082 
1083   // Explicitly specified attributes and local variables with predetermined
1084   // attributes.
1085   auto I = Stack.back().first.rbegin();
1086   auto EndI = Stack.back().first.rend();
1087   if (FromParent && I != EndI)
1088     std::advance(I, 1);
1089   if (I->SharingMap.count(D)) {
1090     DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
1091     DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
1092     DVar.CKind = I->SharingMap[D].Attributes;
1093     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1094     DVar.DKind = I->Directive;
1095   }
1096 
1097   return DVar;
1098 }
1099 
1100 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1101                                                   bool FromParent) {
1102   if (isStackEmpty()) {
1103     StackTy::reverse_iterator I;
1104     return getDSA(I, D);
1105   }
1106   D = getCanonicalDecl(D);
1107   auto StartI = Stack.back().first.rbegin();
1108   auto EndI = Stack.back().first.rend();
1109   if (FromParent && StartI != EndI)
1110     std::advance(StartI, 1);
1111   return getDSA(StartI, D);
1112 }
1113 
1114 DSAStackTy::DSAVarData
1115 DSAStackTy::hasDSA(ValueDecl *D,
1116                    const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
1117                    const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
1118                    bool FromParent) {
1119   if (isStackEmpty())
1120     return {};
1121   D = getCanonicalDecl(D);
1122   auto I = Stack.back().first.rbegin();
1123   auto EndI = Stack.back().first.rend();
1124   if (FromParent && I != EndI)
1125     std::advance(I, 1);
1126   for (; I != EndI; std::advance(I, 1)) {
1127     if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
1128       continue;
1129     auto NewI = I;
1130     DSAVarData DVar = getDSA(NewI, D);
1131     if (I == NewI && CPred(DVar.CKind))
1132       return DVar;
1133   }
1134   return {};
1135 }
1136 
1137 DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1138     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
1139     const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
1140     bool FromParent) {
1141   if (isStackEmpty())
1142     return {};
1143   D = getCanonicalDecl(D);
1144   auto StartI = Stack.back().first.rbegin();
1145   auto EndI = Stack.back().first.rend();
1146   if (FromParent && StartI != EndI)
1147     std::advance(StartI, 1);
1148   if (StartI == EndI || !DPred(StartI->Directive))
1149     return {};
1150   auto NewI = StartI;
1151   DSAVarData DVar = getDSA(NewI, D);
1152   return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
1153 }
1154 
1155 bool DSAStackTy::hasExplicitDSA(
1156     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
1157     unsigned Level, bool NotLastprivate) {
1158   if (isStackEmpty())
1159     return false;
1160   D = getCanonicalDecl(D);
1161   auto StartI = Stack.back().first.begin();
1162   auto EndI = Stack.back().first.end();
1163   if (std::distance(StartI, EndI) <= (int)Level)
1164     return false;
1165   std::advance(StartI, Level);
1166   return (StartI->SharingMap.count(D) > 0) &&
1167          StartI->SharingMap[D].RefExpr.getPointer() &&
1168          CPred(StartI->SharingMap[D].Attributes) &&
1169          (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
1170 }
1171 
1172 bool DSAStackTy::hasExplicitDirective(
1173     const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
1174     unsigned Level) {
1175   if (isStackEmpty())
1176     return false;
1177   auto StartI = Stack.back().first.begin();
1178   auto EndI = Stack.back().first.end();
1179   if (std::distance(StartI, EndI) <= (int)Level)
1180     return false;
1181   std::advance(StartI, Level);
1182   return DPred(StartI->Directive);
1183 }
1184 
1185 bool DSAStackTy::hasDirective(
1186     const llvm::function_ref<bool(OpenMPDirectiveKind,
1187                                   const DeclarationNameInfo &, SourceLocation)>
1188         &DPred,
1189     bool FromParent) {
1190   // We look only in the enclosing region.
1191   if (isStackEmpty())
1192     return false;
1193   auto StartI = std::next(Stack.back().first.rbegin());
1194   auto EndI = Stack.back().first.rend();
1195   if (FromParent && StartI != EndI)
1196     StartI = std::next(StartI);
1197   for (auto I = StartI, EE = EndI; I != EE; ++I) {
1198     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1199       return true;
1200   }
1201   return false;
1202 }
1203 
1204 void Sema::InitDataSharingAttributesStack() {
1205   VarDataSharingAttributesStack = new DSAStackTy(*this);
1206 }
1207 
1208 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1209 
1210 void Sema::pushOpenMPFunctionRegion() {
1211   DSAStack->pushFunction();
1212 }
1213 
1214 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1215   DSAStack->popFunction(OldFSI);
1216 }
1217 
1218 static llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy>
1219 isDeclareTargetDeclaration(const ValueDecl *VD) {
1220   for (const auto *D : VD->redecls()) {
1221     if (!D->hasAttrs())
1222       continue;
1223     if (const auto *Attr = D->getAttr<OMPDeclareTargetDeclAttr>())
1224       return Attr->getMapType();
1225   }
1226   return llvm::None;
1227 }
1228 
1229 bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
1230   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1231 
1232   auto &Ctx = getASTContext();
1233   bool IsByRef = true;
1234 
1235   // Find the directive that is associated with the provided scope.
1236   D = cast<ValueDecl>(D->getCanonicalDecl());
1237   auto Ty = D->getType();
1238 
1239   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1240     // This table summarizes how a given variable should be passed to the device
1241     // given its type and the clauses where it appears. This table is based on
1242     // the description in OpenMP 4.5 [2.10.4, target Construct] and
1243     // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1244     //
1245     // =========================================================================
1246     // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
1247     // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
1248     // =========================================================================
1249     // | scl  |               |     |       |       -       |          | bycopy|
1250     // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
1251     // | scl  |               |  x  |   -   |       -       |     -    | null  |
1252     // | scl  |       x       |     |       |       -       |          | byref |
1253     // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
1254     // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
1255     // | scl  |               |  -  |   -   |       -       |     x    | byref |
1256     // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
1257     //
1258     // | agg  |      n.a.     |     |       |       -       |          | byref |
1259     // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
1260     // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1261     // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1262     // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
1263     //
1264     // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
1265     // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
1266     // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1267     // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1268     // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
1269     // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
1270     // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
1271     // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
1272     // =========================================================================
1273     // Legend:
1274     //  scl - scalar
1275     //  ptr - pointer
1276     //  agg - aggregate
1277     //  x - applies
1278     //  - - invalid in this combination
1279     //  [] - mapped with an array section
1280     //  byref - should be mapped by reference
1281     //  byval - should be mapped by value
1282     //  null - initialize a local variable to null on the device
1283     //
1284     // Observations:
1285     //  - All scalar declarations that show up in a map clause have to be passed
1286     //    by reference, because they may have been mapped in the enclosing data
1287     //    environment.
1288     //  - If the scalar value does not fit the size of uintptr, it has to be
1289     //    passed by reference, regardless the result in the table above.
1290     //  - For pointers mapped by value that have either an implicit map or an
1291     //    array section, the runtime library may pass the NULL value to the
1292     //    device instead of the value passed to it by the compiler.
1293 
1294     if (Ty->isReferenceType())
1295       Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1296 
1297     // Locate map clauses and see if the variable being captured is referred to
1298     // in any of those clauses. Here we only care about variables, not fields,
1299     // because fields are part of aggregates.
1300     bool IsVariableUsedInMapClause = false;
1301     bool IsVariableAssociatedWithSection = false;
1302 
1303     DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1304         D, Level, [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
1305                 MapExprComponents,
1306             OpenMPClauseKind WhereFoundClauseKind) {
1307           // Only the map clause information influences how a variable is
1308           // captured. E.g. is_device_ptr does not require changing the default
1309           // behavior.
1310           if (WhereFoundClauseKind != OMPC_map)
1311             return false;
1312 
1313           auto EI = MapExprComponents.rbegin();
1314           auto EE = MapExprComponents.rend();
1315 
1316           assert(EI != EE && "Invalid map expression!");
1317 
1318           if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1319             IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1320 
1321           ++EI;
1322           if (EI == EE)
1323             return false;
1324 
1325           if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1326               isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1327               isa<MemberExpr>(EI->getAssociatedExpression())) {
1328             IsVariableAssociatedWithSection = true;
1329             // There is nothing more we need to know about this variable.
1330             return true;
1331           }
1332 
1333           // Keep looking for more map info.
1334           return false;
1335         });
1336 
1337     if (IsVariableUsedInMapClause) {
1338       // If variable is identified in a map clause it is always captured by
1339       // reference except if it is a pointer that is dereferenced somehow.
1340       IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1341     } else {
1342       // By default, all the data that has a scalar type is mapped by copy
1343       // (except for reduction variables).
1344       IsByRef =
1345           !Ty->isScalarType() ||
1346           DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1347           DSAStack->hasExplicitDSA(
1348               D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
1349     }
1350   }
1351 
1352   if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
1353     IsByRef =
1354         !DSAStack->hasExplicitDSA(
1355             D,
1356             [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1357             Level, /*NotLastprivate=*/true) &&
1358         // If the variable is artificial and must be captured by value - try to
1359         // capture by value.
1360         !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1361           !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
1362   }
1363 
1364   // When passing data by copy, we need to make sure it fits the uintptr size
1365   // and alignment, because the runtime library only deals with uintptr types.
1366   // If it does not fit the uintptr size, we need to pass the data by reference
1367   // instead.
1368   if (!IsByRef &&
1369       (Ctx.getTypeSizeInChars(Ty) >
1370            Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
1371        Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
1372     IsByRef = true;
1373   }
1374 
1375   return IsByRef;
1376 }
1377 
1378 unsigned Sema::getOpenMPNestingLevel() const {
1379   assert(getLangOpts().OpenMP);
1380   return DSAStack->getNestingLevel();
1381 }
1382 
1383 bool Sema::isInOpenMPTargetExecutionDirective() const {
1384   return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1385           !DSAStack->isClauseParsingMode()) ||
1386          DSAStack->hasDirective(
1387              [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1388                 SourceLocation) -> bool {
1389                return isOpenMPTargetExecutionDirective(K);
1390              },
1391              false);
1392 }
1393 
1394 VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
1395   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1396   D = getCanonicalDecl(D);
1397 
1398   // If we are attempting to capture a global variable in a directive with
1399   // 'target' we return true so that this global is also mapped to the device.
1400   //
1401   auto *VD = dyn_cast<VarDecl>(D);
1402   if (VD && !VD->hasLocalStorage() && isInOpenMPTargetExecutionDirective()) {
1403     // If the declaration is enclosed in a 'declare target' directive,
1404     // then it should not be captured.
1405     //
1406     if (isDeclareTargetDeclaration(VD))
1407       return nullptr;
1408     return VD;
1409   }
1410 
1411   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1412       (!DSAStack->isClauseParsingMode() ||
1413        DSAStack->getParentDirective() != OMPD_unknown)) {
1414     auto &&Info = DSAStack->isLoopControlVariable(D);
1415     if (Info.first ||
1416         (VD && VD->hasLocalStorage() &&
1417          isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
1418         (VD && DSAStack->isForceVarCapturing()))
1419       return VD ? VD : Info.second;
1420     auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
1421     if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
1422       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1423     DVarPrivate = DSAStack->hasDSA(
1424         D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1425         DSAStack->isClauseParsingMode());
1426     if (DVarPrivate.CKind != OMPC_unknown)
1427       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1428   }
1429   return nullptr;
1430 }
1431 
1432 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1433                                         unsigned Level) const {
1434   SmallVector<OpenMPDirectiveKind, 4> Regions;
1435   getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1436   FunctionScopesIndex -= Regions.size();
1437 }
1438 
1439 bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
1440   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1441   return DSAStack->hasExplicitDSA(
1442              D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; },
1443              Level) ||
1444          (DSAStack->isClauseParsingMode() &&
1445           DSAStack->getClauseParsingMode() == OMPC_private) ||
1446          // Consider taskgroup reduction descriptor variable a private to avoid
1447          // possible capture in the region.
1448          (DSAStack->hasExplicitDirective(
1449               [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1450               Level) &&
1451           DSAStack->isTaskgroupReductionRef(D, Level));
1452 }
1453 
1454 void Sema::setOpenMPCaptureKind(FieldDecl *FD, ValueDecl *D, unsigned Level) {
1455   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1456   D = getCanonicalDecl(D);
1457   OpenMPClauseKind OMPC = OMPC_unknown;
1458   for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1459     const unsigned NewLevel = I - 1;
1460     if (DSAStack->hasExplicitDSA(D,
1461                                  [&OMPC](const OpenMPClauseKind K) {
1462                                    if (isOpenMPPrivate(K)) {
1463                                      OMPC = K;
1464                                      return true;
1465                                    }
1466                                    return false;
1467                                  },
1468                                  NewLevel))
1469       break;
1470     if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1471             D, NewLevel,
1472             [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1473                OpenMPClauseKind) { return true; })) {
1474       OMPC = OMPC_map;
1475       break;
1476     }
1477     if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1478                                        NewLevel)) {
1479       OMPC = OMPC_map;
1480       if (D->getType()->isScalarType() &&
1481           DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1482               DefaultMapAttributes::DMA_tofrom_scalar)
1483         OMPC = OMPC_firstprivate;
1484       break;
1485     }
1486   }
1487   if (OMPC != OMPC_unknown)
1488     FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1489 }
1490 
1491 bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
1492   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1493   // Return true if the current level is no longer enclosed in a target region.
1494 
1495   auto *VD = dyn_cast<VarDecl>(D);
1496   return VD && !VD->hasLocalStorage() &&
1497          DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1498                                         Level);
1499 }
1500 
1501 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
1502 
1503 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1504                                const DeclarationNameInfo &DirName,
1505                                Scope *CurScope, SourceLocation Loc) {
1506   DSAStack->push(DKind, DirName, CurScope, Loc);
1507   PushExpressionEvaluationContext(
1508       ExpressionEvaluationContext::PotentiallyEvaluated);
1509 }
1510 
1511 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1512   DSAStack->setClauseParsingMode(K);
1513 }
1514 
1515 void Sema::EndOpenMPClause() {
1516   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
1517 }
1518 
1519 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
1520   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1521   //  A variable of class type (or array thereof) that appears in a lastprivate
1522   //  clause requires an accessible, unambiguous default constructor for the
1523   //  class type, unless the list item is also specified in a firstprivate
1524   //  clause.
1525   if (auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1526     for (auto *C : D->clauses()) {
1527       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1528         SmallVector<Expr *, 8> PrivateCopies;
1529         for (auto *DE : Clause->varlists()) {
1530           if (DE->isValueDependent() || DE->isTypeDependent()) {
1531             PrivateCopies.push_back(nullptr);
1532             continue;
1533           }
1534           auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
1535           VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1536           QualType Type = VD->getType().getNonReferenceType();
1537           auto DVar = DSAStack->getTopDSA(VD, false);
1538           if (DVar.CKind == OMPC_lastprivate) {
1539             // Generate helper private variable and initialize it with the
1540             // default value. The address of the original variable is replaced
1541             // by the address of the new private variable in CodeGen. This new
1542             // variable is not added to IdResolver, so the code in the OpenMP
1543             // region uses original variable for proper diagnostics.
1544             auto *VDPrivate = buildVarDecl(
1545                 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
1546                 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
1547             ActOnUninitializedDecl(VDPrivate);
1548             if (VDPrivate->isInvalidDecl())
1549               continue;
1550             PrivateCopies.push_back(buildDeclRefExpr(
1551                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
1552           } else {
1553             // The variable is also a firstprivate, so initialization sequence
1554             // for private copy is generated already.
1555             PrivateCopies.push_back(nullptr);
1556           }
1557         }
1558         // Set initializers to private copies if no errors were found.
1559         if (PrivateCopies.size() == Clause->varlist_size())
1560           Clause->setPrivateCopies(PrivateCopies);
1561       }
1562     }
1563   }
1564 
1565   DSAStack->pop();
1566   DiscardCleanupsInEvaluationContext();
1567   PopExpressionEvaluationContext();
1568 }
1569 
1570 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1571                                      Expr *NumIterations, Sema &SemaRef,
1572                                      Scope *S, DSAStackTy *Stack);
1573 
1574 namespace {
1575 
1576 class VarDeclFilterCCC : public CorrectionCandidateCallback {
1577 private:
1578   Sema &SemaRef;
1579 
1580 public:
1581   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
1582   bool ValidateCandidate(const TypoCorrection &Candidate) override {
1583     NamedDecl *ND = Candidate.getCorrectionDecl();
1584     if (auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
1585       return VD->hasGlobalStorage() &&
1586              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1587                                    SemaRef.getCurScope());
1588     }
1589     return false;
1590   }
1591 };
1592 
1593 class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1594 private:
1595   Sema &SemaRef;
1596 
1597 public:
1598   explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1599   bool ValidateCandidate(const TypoCorrection &Candidate) override {
1600     NamedDecl *ND = Candidate.getCorrectionDecl();
1601     if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
1602       return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1603                                    SemaRef.getCurScope());
1604     }
1605     return false;
1606   }
1607 };
1608 
1609 } // namespace
1610 
1611 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1612                                          CXXScopeSpec &ScopeSpec,
1613                                          const DeclarationNameInfo &Id) {
1614   LookupResult Lookup(*this, Id, LookupOrdinaryName);
1615   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1616 
1617   if (Lookup.isAmbiguous())
1618     return ExprError();
1619 
1620   VarDecl *VD;
1621   if (!Lookup.isSingleResult()) {
1622     if (TypoCorrection Corrected = CorrectTypo(
1623             Id, LookupOrdinaryName, CurScope, nullptr,
1624             llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
1625       diagnoseTypo(Corrected,
1626                    PDiag(Lookup.empty()
1627                              ? diag::err_undeclared_var_use_suggest
1628                              : diag::err_omp_expected_var_arg_suggest)
1629                        << Id.getName());
1630       VD = Corrected.getCorrectionDeclAs<VarDecl>();
1631     } else {
1632       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1633                                        : diag::err_omp_expected_var_arg)
1634           << Id.getName();
1635       return ExprError();
1636     }
1637   } else {
1638     if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1639       Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1640       Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1641       return ExprError();
1642     }
1643   }
1644   Lookup.suppressDiagnostics();
1645 
1646   // OpenMP [2.9.2, Syntax, C/C++]
1647   //   Variables must be file-scope, namespace-scope, or static block-scope.
1648   if (!VD->hasGlobalStorage()) {
1649     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
1650         << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1651     bool IsDecl =
1652         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1653     Diag(VD->getLocation(),
1654          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1655         << VD;
1656     return ExprError();
1657   }
1658 
1659   VarDecl *CanonicalVD = VD->getCanonicalDecl();
1660   NamedDecl *ND = CanonicalVD;
1661   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1662   //   A threadprivate directive for file-scope variables must appear outside
1663   //   any definition or declaration.
1664   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1665       !getCurLexicalContext()->isTranslationUnit()) {
1666     Diag(Id.getLoc(), diag::err_omp_var_scope)
1667         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1668     bool IsDecl =
1669         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1670     Diag(VD->getLocation(),
1671          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1672         << VD;
1673     return ExprError();
1674   }
1675   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1676   //   A threadprivate directive for static class member variables must appear
1677   //   in the class definition, in the same scope in which the member
1678   //   variables are declared.
1679   if (CanonicalVD->isStaticDataMember() &&
1680       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1681     Diag(Id.getLoc(), diag::err_omp_var_scope)
1682         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1683     bool IsDecl =
1684         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1685     Diag(VD->getLocation(),
1686          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1687         << VD;
1688     return ExprError();
1689   }
1690   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1691   //   A threadprivate directive for namespace-scope variables must appear
1692   //   outside any definition or declaration other than the namespace
1693   //   definition itself.
1694   if (CanonicalVD->getDeclContext()->isNamespace() &&
1695       (!getCurLexicalContext()->isFileContext() ||
1696        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1697     Diag(Id.getLoc(), diag::err_omp_var_scope)
1698         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1699     bool IsDecl =
1700         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1701     Diag(VD->getLocation(),
1702          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1703         << VD;
1704     return ExprError();
1705   }
1706   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1707   //   A threadprivate directive for static block-scope variables must appear
1708   //   in the scope of the variable and not in a nested scope.
1709   if (CanonicalVD->isStaticLocal() && CurScope &&
1710       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
1711     Diag(Id.getLoc(), diag::err_omp_var_scope)
1712         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1713     bool IsDecl =
1714         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1715     Diag(VD->getLocation(),
1716          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1717         << VD;
1718     return ExprError();
1719   }
1720 
1721   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1722   //   A threadprivate directive must lexically precede all references to any
1723   //   of the variables in its list.
1724   if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
1725     Diag(Id.getLoc(), diag::err_omp_var_used)
1726         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1727     return ExprError();
1728   }
1729 
1730   QualType ExprType = VD->getType().getNonReferenceType();
1731   return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1732                              SourceLocation(), VD,
1733                              /*RefersToEnclosingVariableOrCapture=*/false,
1734                              Id.getLoc(), ExprType, VK_LValue);
1735 }
1736 
1737 Sema::DeclGroupPtrTy
1738 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1739                                         ArrayRef<Expr *> VarList) {
1740   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
1741     CurContext->addDecl(D);
1742     return DeclGroupPtrTy::make(DeclGroupRef(D));
1743   }
1744   return nullptr;
1745 }
1746 
1747 namespace {
1748 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1749   Sema &SemaRef;
1750 
1751 public:
1752   bool VisitDeclRefExpr(const DeclRefExpr *E) {
1753     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1754       if (VD->hasLocalStorage()) {
1755         SemaRef.Diag(E->getLocStart(),
1756                      diag::err_omp_local_var_in_threadprivate_init)
1757             << E->getSourceRange();
1758         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1759             << VD << VD->getSourceRange();
1760         return true;
1761       }
1762     }
1763     return false;
1764   }
1765   bool VisitStmt(const Stmt *S) {
1766     for (auto Child : S->children()) {
1767       if (Child && Visit(Child))
1768         return true;
1769     }
1770     return false;
1771   }
1772   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
1773 };
1774 } // namespace
1775 
1776 OMPThreadPrivateDecl *
1777 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
1778   SmallVector<Expr *, 8> Vars;
1779   for (auto &RefExpr : VarList) {
1780     DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
1781     VarDecl *VD = cast<VarDecl>(DE->getDecl());
1782     SourceLocation ILoc = DE->getExprLoc();
1783 
1784     // Mark variable as used.
1785     VD->setReferenced();
1786     VD->markUsed(Context);
1787 
1788     QualType QType = VD->getType();
1789     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1790       // It will be analyzed later.
1791       Vars.push_back(DE);
1792       continue;
1793     }
1794 
1795     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1796     //   A threadprivate variable must not have an incomplete type.
1797     if (RequireCompleteType(ILoc, VD->getType(),
1798                             diag::err_omp_threadprivate_incomplete_type)) {
1799       continue;
1800     }
1801 
1802     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1803     //   A threadprivate variable must not have a reference type.
1804     if (VD->getType()->isReferenceType()) {
1805       Diag(ILoc, diag::err_omp_ref_type_arg)
1806           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1807       bool IsDecl =
1808           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1809       Diag(VD->getLocation(),
1810            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1811           << VD;
1812       continue;
1813     }
1814 
1815     // Check if this is a TLS variable. If TLS is not being supported, produce
1816     // the corresponding diagnostic.
1817     if ((VD->getTLSKind() != VarDecl::TLS_None &&
1818          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1819            getLangOpts().OpenMPUseTLS &&
1820            getASTContext().getTargetInfo().isTLSSupported())) ||
1821         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1822          !VD->isLocalVarDecl())) {
1823       Diag(ILoc, diag::err_omp_var_thread_local)
1824           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
1825       bool IsDecl =
1826           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1827       Diag(VD->getLocation(),
1828            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1829           << VD;
1830       continue;
1831     }
1832 
1833     // Check if initial value of threadprivate variable reference variable with
1834     // local storage (it is not supported by runtime).
1835     if (auto Init = VD->getAnyInitializer()) {
1836       LocalVarRefChecker Checker(*this);
1837       if (Checker.Visit(Init))
1838         continue;
1839     }
1840 
1841     Vars.push_back(RefExpr);
1842     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
1843     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1844         Context, SourceRange(Loc, Loc)));
1845     if (auto *ML = Context.getASTMutationListener())
1846       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
1847   }
1848   OMPThreadPrivateDecl *D = nullptr;
1849   if (!Vars.empty()) {
1850     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1851                                      Vars);
1852     D->setAccess(AS_public);
1853   }
1854   return D;
1855 }
1856 
1857 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1858                               const ValueDecl *D, DSAStackTy::DSAVarData DVar,
1859                               bool IsLoopIterVar = false) {
1860   if (DVar.RefExpr) {
1861     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1862         << getOpenMPClauseName(DVar.CKind);
1863     return;
1864   }
1865   enum {
1866     PDSA_StaticMemberShared,
1867     PDSA_StaticLocalVarShared,
1868     PDSA_LoopIterVarPrivate,
1869     PDSA_LoopIterVarLinear,
1870     PDSA_LoopIterVarLastprivate,
1871     PDSA_ConstVarShared,
1872     PDSA_GlobalVarShared,
1873     PDSA_TaskVarFirstprivate,
1874     PDSA_LocalVarPrivate,
1875     PDSA_Implicit
1876   } Reason = PDSA_Implicit;
1877   bool ReportHint = false;
1878   auto ReportLoc = D->getLocation();
1879   auto *VD = dyn_cast<VarDecl>(D);
1880   if (IsLoopIterVar) {
1881     if (DVar.CKind == OMPC_private)
1882       Reason = PDSA_LoopIterVarPrivate;
1883     else if (DVar.CKind == OMPC_lastprivate)
1884       Reason = PDSA_LoopIterVarLastprivate;
1885     else
1886       Reason = PDSA_LoopIterVarLinear;
1887   } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1888              DVar.CKind == OMPC_firstprivate) {
1889     Reason = PDSA_TaskVarFirstprivate;
1890     ReportLoc = DVar.ImplicitDSALoc;
1891   } else if (VD && VD->isStaticLocal())
1892     Reason = PDSA_StaticLocalVarShared;
1893   else if (VD && VD->isStaticDataMember())
1894     Reason = PDSA_StaticMemberShared;
1895   else if (VD && VD->isFileVarDecl())
1896     Reason = PDSA_GlobalVarShared;
1897   else if (D->getType().isConstant(SemaRef.getASTContext()))
1898     Reason = PDSA_ConstVarShared;
1899   else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
1900     ReportHint = true;
1901     Reason = PDSA_LocalVarPrivate;
1902   }
1903   if (Reason != PDSA_Implicit) {
1904     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
1905         << Reason << ReportHint
1906         << getOpenMPDirectiveName(Stack->getCurrentDirective());
1907   } else if (DVar.ImplicitDSALoc.isValid()) {
1908     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1909         << getOpenMPClauseName(DVar.CKind);
1910   }
1911 }
1912 
1913 namespace {
1914 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1915   DSAStackTy *Stack;
1916   Sema &SemaRef;
1917   bool ErrorFound;
1918   CapturedStmt *CS;
1919   llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
1920   llvm::SmallVector<Expr *, 8> ImplicitMap;
1921   llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
1922   llvm::DenseSet<ValueDecl *> ImplicitDeclarations;
1923 
1924 public:
1925   void VisitDeclRefExpr(DeclRefExpr *E) {
1926     if (E->isTypeDependent() || E->isValueDependent() ||
1927         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1928       return;
1929     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1930       VD = VD->getCanonicalDecl();
1931       // Skip internally declared variables.
1932       if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
1933         return;
1934 
1935       auto DVar = Stack->getTopDSA(VD, false);
1936       // Check if the variable has explicit DSA set and stop analysis if it so.
1937       if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
1938         return;
1939 
1940       // Skip internally declared static variables.
1941       llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
1942           isDeclareTargetDeclaration(VD);
1943       if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
1944           (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
1945         return;
1946 
1947       auto ELoc = E->getExprLoc();
1948       auto DKind = Stack->getCurrentDirective();
1949       // The default(none) clause requires that each variable that is referenced
1950       // in the construct, and does not have a predetermined data-sharing
1951       // attribute, must have its data-sharing attribute explicitly determined
1952       // by being listed in a data-sharing attribute clause.
1953       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
1954           isParallelOrTaskRegion(DKind) &&
1955           VarsWithInheritedDSA.count(VD) == 0) {
1956         VarsWithInheritedDSA[VD] = E;
1957         return;
1958       }
1959 
1960       if (isOpenMPTargetExecutionDirective(DKind) &&
1961           !Stack->isLoopControlVariable(VD).first) {
1962         if (!Stack->checkMappableExprComponentListsForDecl(
1963                 VD, /*CurrentRegionOnly=*/true,
1964                 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
1965                        StackComponents,
1966                    OpenMPClauseKind) {
1967                   // Variable is used if it has been marked as an array, array
1968                   // section or the variable iself.
1969                   return StackComponents.size() == 1 ||
1970                          std::all_of(
1971                              std::next(StackComponents.rbegin()),
1972                              StackComponents.rend(),
1973                              [](const OMPClauseMappableExprCommon::
1974                                     MappableComponent &MC) {
1975                                return MC.getAssociatedDeclaration() ==
1976                                           nullptr &&
1977                                       (isa<OMPArraySectionExpr>(
1978                                            MC.getAssociatedExpression()) ||
1979                                        isa<ArraySubscriptExpr>(
1980                                            MC.getAssociatedExpression()));
1981                              });
1982                 })) {
1983           bool IsFirstprivate = false;
1984           // By default lambdas are captured as firstprivates.
1985           if (const auto *RD =
1986                   VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
1987             IsFirstprivate = RD->isLambda();
1988           IsFirstprivate =
1989               IsFirstprivate ||
1990               (VD->getType().getNonReferenceType()->isScalarType() &&
1991                Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
1992           if (IsFirstprivate)
1993             ImplicitFirstprivate.emplace_back(E);
1994           else
1995             ImplicitMap.emplace_back(E);
1996           return;
1997         }
1998       }
1999 
2000       // OpenMP [2.9.3.6, Restrictions, p.2]
2001       //  A list item that appears in a reduction clause of the innermost
2002       //  enclosing worksharing or parallel construct may not be accessed in an
2003       //  explicit task.
2004       DVar = Stack->hasInnermostDSA(
2005           VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
2006           [](OpenMPDirectiveKind K) -> bool {
2007             return isOpenMPParallelDirective(K) ||
2008                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2009           },
2010           /*FromParent=*/true);
2011       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2012         ErrorFound = true;
2013         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2014         ReportOriginalDSA(SemaRef, Stack, VD, DVar);
2015         return;
2016       }
2017 
2018       // Define implicit data-sharing attributes for task.
2019       DVar = Stack->getImplicitDSA(VD, false);
2020       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2021           !Stack->isLoopControlVariable(VD).first)
2022         ImplicitFirstprivate.push_back(E);
2023     }
2024   }
2025   void VisitMemberExpr(MemberExpr *E) {
2026     if (E->isTypeDependent() || E->isValueDependent() ||
2027         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2028       return;
2029     auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
2030     OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2031     if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
2032       if (!FD)
2033         return;
2034       auto DVar = Stack->getTopDSA(FD, false);
2035       // Check if the variable has explicit DSA set and stop analysis if it
2036       // so.
2037       if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2038         return;
2039 
2040       if (isOpenMPTargetExecutionDirective(DKind) &&
2041           !Stack->isLoopControlVariable(FD).first &&
2042           !Stack->checkMappableExprComponentListsForDecl(
2043               FD, /*CurrentRegionOnly=*/true,
2044               [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2045                      StackComponents,
2046                  OpenMPClauseKind) {
2047                 return isa<CXXThisExpr>(
2048                     cast<MemberExpr>(
2049                         StackComponents.back().getAssociatedExpression())
2050                         ->getBase()
2051                         ->IgnoreParens());
2052               })) {
2053         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2054         //  A bit-field cannot appear in a map clause.
2055         //
2056         if (FD->isBitField())
2057           return;
2058         ImplicitMap.emplace_back(E);
2059         return;
2060       }
2061 
2062       auto ELoc = E->getExprLoc();
2063       // OpenMP [2.9.3.6, Restrictions, p.2]
2064       //  A list item that appears in a reduction clause of the innermost
2065       //  enclosing worksharing or parallel construct may not be accessed in
2066       //  an  explicit task.
2067       DVar = Stack->hasInnermostDSA(
2068           FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
2069           [](OpenMPDirectiveKind K) -> bool {
2070             return isOpenMPParallelDirective(K) ||
2071                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2072           },
2073           /*FromParent=*/true);
2074       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2075         ErrorFound = true;
2076         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2077         ReportOriginalDSA(SemaRef, Stack, FD, DVar);
2078         return;
2079       }
2080 
2081       // Define implicit data-sharing attributes for task.
2082       DVar = Stack->getImplicitDSA(FD, false);
2083       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2084           !Stack->isLoopControlVariable(FD).first)
2085         ImplicitFirstprivate.push_back(E);
2086       return;
2087     }
2088     if (isOpenMPTargetExecutionDirective(DKind)) {
2089       OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
2090       if (!CheckMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
2091                                         /*NoDiagnose=*/true))
2092         return;
2093       auto *VD = cast<ValueDecl>(
2094           CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2095       if (!Stack->checkMappableExprComponentListsForDecl(
2096               VD, /*CurrentRegionOnly=*/true,
2097               [&CurComponents](
2098                   OMPClauseMappableExprCommon::MappableExprComponentListRef
2099                       StackComponents,
2100                   OpenMPClauseKind) {
2101                 auto CCI = CurComponents.rbegin();
2102                 auto CCE = CurComponents.rend();
2103                 for (const auto &SC : llvm::reverse(StackComponents)) {
2104                   // Do both expressions have the same kind?
2105                   if (CCI->getAssociatedExpression()->getStmtClass() !=
2106                       SC.getAssociatedExpression()->getStmtClass())
2107                     if (!(isa<OMPArraySectionExpr>(
2108                               SC.getAssociatedExpression()) &&
2109                           isa<ArraySubscriptExpr>(
2110                               CCI->getAssociatedExpression())))
2111                       return false;
2112 
2113                   Decl *CCD = CCI->getAssociatedDeclaration();
2114                   Decl *SCD = SC.getAssociatedDeclaration();
2115                   CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2116                   SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2117                   if (SCD != CCD)
2118                     return false;
2119                   std::advance(CCI, 1);
2120                   if (CCI == CCE)
2121                     break;
2122                 }
2123                 return true;
2124               })) {
2125         Visit(E->getBase());
2126       }
2127     } else
2128       Visit(E->getBase());
2129   }
2130   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
2131     for (auto *C : S->clauses()) {
2132       // Skip analysis of arguments of implicitly defined firstprivate clause
2133       // for task|target directives.
2134       // Skip analysis of arguments of implicitly defined map clause for target
2135       // directives.
2136       if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2137                  C->isImplicit())) {
2138         for (auto *CC : C->children()) {
2139           if (CC)
2140             Visit(CC);
2141         }
2142       }
2143     }
2144   }
2145   void VisitStmt(Stmt *S) {
2146     for (auto *C : S->children()) {
2147       if (C && !isa<OMPExecutableDirective>(C))
2148         Visit(C);
2149     }
2150   }
2151 
2152   bool isErrorFound() { return ErrorFound; }
2153   ArrayRef<Expr *> getImplicitFirstprivate() const {
2154     return ImplicitFirstprivate;
2155   }
2156   ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
2157   llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
2158     return VarsWithInheritedDSA;
2159   }
2160 
2161   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2162       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
2163 };
2164 } // namespace
2165 
2166 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
2167   switch (DKind) {
2168   case OMPD_parallel:
2169   case OMPD_parallel_for:
2170   case OMPD_parallel_for_simd:
2171   case OMPD_parallel_sections:
2172   case OMPD_teams:
2173   case OMPD_teams_distribute:
2174   case OMPD_teams_distribute_simd: {
2175     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2176     QualType KmpInt32PtrTy =
2177         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2178     Sema::CapturedParamNameType Params[] = {
2179         std::make_pair(".global_tid.", KmpInt32PtrTy),
2180         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2181         std::make_pair(StringRef(), QualType()) // __context with shared vars
2182     };
2183     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2184                              Params);
2185     break;
2186   }
2187   case OMPD_target_teams:
2188   case OMPD_target_parallel:
2189   case OMPD_target_parallel_for:
2190   case OMPD_target_parallel_for_simd:
2191   case OMPD_target_teams_distribute:
2192   case OMPD_target_teams_distribute_simd: {
2193     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2194     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
2195     FunctionProtoType::ExtProtoInfo EPI;
2196     EPI.Variadic = true;
2197     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2198     Sema::CapturedParamNameType Params[] = {
2199         std::make_pair(".global_tid.", KmpInt32Ty),
2200         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
2201         std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
2202         std::make_pair(".copy_fn.",
2203                        Context.getPointerType(CopyFnType).withConst()),
2204         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2205         std::make_pair(StringRef(), QualType()) // __context with shared vars
2206     };
2207     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2208                              Params);
2209     // Mark this captured region as inlined, because we don't use outlined
2210     // function directly.
2211     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2212         AlwaysInlineAttr::CreateImplicit(
2213             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
2214     Sema::CapturedParamNameType ParamsTarget[] = {
2215         std::make_pair(StringRef(), QualType()) // __context with shared vars
2216     };
2217     // Start a captured region for 'target' with no implicit parameters.
2218     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2219                              ParamsTarget);
2220     QualType KmpInt32PtrTy =
2221         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2222     Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
2223         std::make_pair(".global_tid.", KmpInt32PtrTy),
2224         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2225         std::make_pair(StringRef(), QualType()) // __context with shared vars
2226     };
2227     // Start a captured region for 'teams' or 'parallel'.  Both regions have
2228     // the same implicit parameters.
2229     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2230                              ParamsTeamsOrParallel);
2231     break;
2232   }
2233   case OMPD_target:
2234   case OMPD_target_simd: {
2235     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2236     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
2237     FunctionProtoType::ExtProtoInfo EPI;
2238     EPI.Variadic = true;
2239     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2240     Sema::CapturedParamNameType Params[] = {
2241         std::make_pair(".global_tid.", KmpInt32Ty),
2242         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
2243         std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
2244         std::make_pair(".copy_fn.",
2245                        Context.getPointerType(CopyFnType).withConst()),
2246         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2247         std::make_pair(StringRef(), QualType()) // __context with shared vars
2248     };
2249     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2250                              Params);
2251     // Mark this captured region as inlined, because we don't use outlined
2252     // function directly.
2253     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2254         AlwaysInlineAttr::CreateImplicit(
2255             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
2256     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2257                              std::make_pair(StringRef(), QualType()));
2258     break;
2259   }
2260   case OMPD_simd:
2261   case OMPD_for:
2262   case OMPD_for_simd:
2263   case OMPD_sections:
2264   case OMPD_section:
2265   case OMPD_single:
2266   case OMPD_master:
2267   case OMPD_critical:
2268   case OMPD_taskgroup:
2269   case OMPD_distribute:
2270   case OMPD_distribute_simd:
2271   case OMPD_ordered:
2272   case OMPD_atomic:
2273   case OMPD_target_data: {
2274     Sema::CapturedParamNameType Params[] = {
2275         std::make_pair(StringRef(), QualType()) // __context with shared vars
2276     };
2277     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2278                              Params);
2279     break;
2280   }
2281   case OMPD_task: {
2282     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2283     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
2284     FunctionProtoType::ExtProtoInfo EPI;
2285     EPI.Variadic = true;
2286     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2287     Sema::CapturedParamNameType Params[] = {
2288         std::make_pair(".global_tid.", KmpInt32Ty),
2289         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
2290         std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
2291         std::make_pair(".copy_fn.",
2292                        Context.getPointerType(CopyFnType).withConst()),
2293         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2294         std::make_pair(StringRef(), QualType()) // __context with shared vars
2295     };
2296     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2297                              Params);
2298     // Mark this captured region as inlined, because we don't use outlined
2299     // function directly.
2300     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2301         AlwaysInlineAttr::CreateImplicit(
2302             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
2303     break;
2304   }
2305   case OMPD_taskloop:
2306   case OMPD_taskloop_simd: {
2307     QualType KmpInt32Ty =
2308         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
2309     QualType KmpUInt64Ty =
2310         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
2311     QualType KmpInt64Ty =
2312         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
2313     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
2314     FunctionProtoType::ExtProtoInfo EPI;
2315     EPI.Variadic = true;
2316     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2317     Sema::CapturedParamNameType Params[] = {
2318         std::make_pair(".global_tid.", KmpInt32Ty),
2319         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
2320         std::make_pair(".privates.",
2321                        Context.VoidPtrTy.withConst().withRestrict()),
2322         std::make_pair(
2323             ".copy_fn.",
2324             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2325         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2326         std::make_pair(".lb.", KmpUInt64Ty),
2327         std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
2328         std::make_pair(".liter.", KmpInt32Ty),
2329         std::make_pair(".reductions.",
2330                        Context.VoidPtrTy.withConst().withRestrict()),
2331         std::make_pair(StringRef(), QualType()) // __context with shared vars
2332     };
2333     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2334                              Params);
2335     // Mark this captured region as inlined, because we don't use outlined
2336     // function directly.
2337     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2338         AlwaysInlineAttr::CreateImplicit(
2339             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
2340     break;
2341   }
2342   case OMPD_distribute_parallel_for_simd:
2343   case OMPD_distribute_parallel_for: {
2344     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2345     QualType KmpInt32PtrTy =
2346         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2347     Sema::CapturedParamNameType Params[] = {
2348         std::make_pair(".global_tid.", KmpInt32PtrTy),
2349         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2350         std::make_pair(".previous.lb.", Context.getSizeType()),
2351         std::make_pair(".previous.ub.", Context.getSizeType()),
2352         std::make_pair(StringRef(), QualType()) // __context with shared vars
2353     };
2354     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2355                              Params);
2356     break;
2357   }
2358   case OMPD_target_teams_distribute_parallel_for:
2359   case OMPD_target_teams_distribute_parallel_for_simd: {
2360     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2361     QualType KmpInt32PtrTy =
2362         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2363 
2364     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
2365     FunctionProtoType::ExtProtoInfo EPI;
2366     EPI.Variadic = true;
2367     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2368     Sema::CapturedParamNameType Params[] = {
2369         std::make_pair(".global_tid.", KmpInt32Ty),
2370         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
2371         std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
2372         std::make_pair(".copy_fn.",
2373                        Context.getPointerType(CopyFnType).withConst()),
2374         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2375         std::make_pair(StringRef(), QualType()) // __context with shared vars
2376     };
2377     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2378                              Params);
2379     // Mark this captured region as inlined, because we don't use outlined
2380     // function directly.
2381     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2382         AlwaysInlineAttr::CreateImplicit(
2383             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
2384     Sema::CapturedParamNameType ParamsTarget[] = {
2385         std::make_pair(StringRef(), QualType()) // __context with shared vars
2386     };
2387     // Start a captured region for 'target' with no implicit parameters.
2388     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2389                              ParamsTarget);
2390 
2391     Sema::CapturedParamNameType ParamsTeams[] = {
2392         std::make_pair(".global_tid.", KmpInt32PtrTy),
2393         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2394         std::make_pair(StringRef(), QualType()) // __context with shared vars
2395     };
2396     // Start a captured region for 'target' with no implicit parameters.
2397     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2398                              ParamsTeams);
2399 
2400     Sema::CapturedParamNameType ParamsParallel[] = {
2401         std::make_pair(".global_tid.", KmpInt32PtrTy),
2402         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2403         std::make_pair(".previous.lb.", Context.getSizeType()),
2404         std::make_pair(".previous.ub.", Context.getSizeType()),
2405         std::make_pair(StringRef(), QualType()) // __context with shared vars
2406     };
2407     // Start a captured region for 'teams' or 'parallel'.  Both regions have
2408     // the same implicit parameters.
2409     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2410                              ParamsParallel);
2411     break;
2412   }
2413 
2414   case OMPD_teams_distribute_parallel_for:
2415   case OMPD_teams_distribute_parallel_for_simd: {
2416     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2417     QualType KmpInt32PtrTy =
2418         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2419 
2420     Sema::CapturedParamNameType ParamsTeams[] = {
2421         std::make_pair(".global_tid.", KmpInt32PtrTy),
2422         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2423         std::make_pair(StringRef(), QualType()) // __context with shared vars
2424     };
2425     // Start a captured region for 'target' with no implicit parameters.
2426     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2427                              ParamsTeams);
2428 
2429     Sema::CapturedParamNameType ParamsParallel[] = {
2430         std::make_pair(".global_tid.", KmpInt32PtrTy),
2431         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2432         std::make_pair(".previous.lb.", Context.getSizeType()),
2433         std::make_pair(".previous.ub.", Context.getSizeType()),
2434         std::make_pair(StringRef(), QualType()) // __context with shared vars
2435     };
2436     // Start a captured region for 'teams' or 'parallel'.  Both regions have
2437     // the same implicit parameters.
2438     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2439                              ParamsParallel);
2440     break;
2441   }
2442   case OMPD_target_update:
2443   case OMPD_target_enter_data:
2444   case OMPD_target_exit_data: {
2445     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2446     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
2447     FunctionProtoType::ExtProtoInfo EPI;
2448     EPI.Variadic = true;
2449     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2450     Sema::CapturedParamNameType Params[] = {
2451         std::make_pair(".global_tid.", KmpInt32Ty),
2452         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
2453         std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
2454         std::make_pair(".copy_fn.",
2455                        Context.getPointerType(CopyFnType).withConst()),
2456         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2457         std::make_pair(StringRef(), QualType()) // __context with shared vars
2458     };
2459     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2460                              Params);
2461     // Mark this captured region as inlined, because we don't use outlined
2462     // function directly.
2463     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2464         AlwaysInlineAttr::CreateImplicit(
2465             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
2466     break;
2467   }
2468   case OMPD_threadprivate:
2469   case OMPD_taskyield:
2470   case OMPD_barrier:
2471   case OMPD_taskwait:
2472   case OMPD_cancellation_point:
2473   case OMPD_cancel:
2474   case OMPD_flush:
2475   case OMPD_declare_reduction:
2476   case OMPD_declare_simd:
2477   case OMPD_declare_target:
2478   case OMPD_end_declare_target:
2479     llvm_unreachable("OpenMP Directive is not allowed");
2480   case OMPD_unknown:
2481     llvm_unreachable("Unknown OpenMP directive");
2482   }
2483 }
2484 
2485 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2486   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2487   getOpenMPCaptureRegions(CaptureRegions, DKind);
2488   return CaptureRegions.size();
2489 }
2490 
2491 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
2492                                              Expr *CaptureExpr, bool WithInit,
2493                                              bool AsExpression) {
2494   assert(CaptureExpr);
2495   ASTContext &C = S.getASTContext();
2496   Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
2497   QualType Ty = Init->getType();
2498   if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
2499     if (S.getLangOpts().CPlusPlus) {
2500       Ty = C.getLValueReferenceType(Ty);
2501     } else {
2502       Ty = C.getPointerType(Ty);
2503       ExprResult Res =
2504           S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2505       if (!Res.isUsable())
2506         return nullptr;
2507       Init = Res.get();
2508     }
2509     WithInit = true;
2510   }
2511   auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
2512                                           CaptureExpr->getLocStart());
2513   if (!WithInit)
2514     CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
2515   S.CurContext->addHiddenDecl(CED);
2516   S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
2517   return CED;
2518 }
2519 
2520 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2521                                  bool WithInit) {
2522   OMPCapturedExprDecl *CD;
2523   if (auto *VD = S.IsOpenMPCapturedDecl(D)) {
2524     CD = cast<OMPCapturedExprDecl>(VD);
2525   } else {
2526     CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2527                           /*AsExpression=*/false);
2528   }
2529   return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2530                           CaptureExpr->getExprLoc());
2531 }
2532 
2533 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
2534   CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
2535   if (!Ref) {
2536     OMPCapturedExprDecl *CD = buildCaptureDecl(
2537         S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2538         /*WithInit=*/true, /*AsExpression=*/true);
2539     Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2540                            CaptureExpr->getExprLoc());
2541   }
2542   ExprResult Res = Ref;
2543   if (!S.getLangOpts().CPlusPlus &&
2544       CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
2545       Ref->getType()->isPointerType()) {
2546     Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
2547     if (!Res.isUsable())
2548       return ExprError();
2549   }
2550   return S.DefaultLvalueConversion(Res.get());
2551 }
2552 
2553 namespace {
2554 // OpenMP directives parsed in this section are represented as a
2555 // CapturedStatement with an associated statement.  If a syntax error
2556 // is detected during the parsing of the associated statement, the
2557 // compiler must abort processing and close the CapturedStatement.
2558 //
2559 // Combined directives such as 'target parallel' have more than one
2560 // nested CapturedStatements.  This RAII ensures that we unwind out
2561 // of all the nested CapturedStatements when an error is found.
2562 class CaptureRegionUnwinderRAII {
2563 private:
2564   Sema &S;
2565   bool &ErrorFound;
2566   OpenMPDirectiveKind DKind;
2567 
2568 public:
2569   CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2570                             OpenMPDirectiveKind DKind)
2571       : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2572   ~CaptureRegionUnwinderRAII() {
2573     if (ErrorFound) {
2574       int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2575       while (--ThisCaptureLevel >= 0)
2576         S.ActOnCapturedRegionError();
2577     }
2578   }
2579 };
2580 } // namespace
2581 
2582 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2583                                       ArrayRef<OMPClause *> Clauses) {
2584   bool ErrorFound = false;
2585   CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2586       *this, ErrorFound, DSAStack->getCurrentDirective());
2587   if (!S.isUsable()) {
2588     ErrorFound = true;
2589     return StmtError();
2590   }
2591 
2592   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2593   getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
2594   OMPOrderedClause *OC = nullptr;
2595   OMPScheduleClause *SC = nullptr;
2596   SmallVector<OMPLinearClause *, 4> LCs;
2597   SmallVector<OMPClauseWithPreInit *, 8> PICs;
2598   // This is required for proper codegen.
2599   for (auto *Clause : Clauses) {
2600     if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2601         Clause->getClauseKind() == OMPC_in_reduction) {
2602       // Capture taskgroup task_reduction descriptors inside the tasking regions
2603       // with the corresponding in_reduction items.
2604       auto *IRC = cast<OMPInReductionClause>(Clause);
2605       for (auto *E : IRC->taskgroup_descriptors())
2606         if (E)
2607           MarkDeclarationsReferencedInExpr(E);
2608     }
2609     if (isOpenMPPrivate(Clause->getClauseKind()) ||
2610         Clause->getClauseKind() == OMPC_copyprivate ||
2611         (getLangOpts().OpenMPUseTLS &&
2612          getASTContext().getTargetInfo().isTLSSupported() &&
2613          Clause->getClauseKind() == OMPC_copyin)) {
2614       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
2615       // Mark all variables in private list clauses as used in inner region.
2616       for (auto *VarRef : Clause->children()) {
2617         if (auto *E = cast_or_null<Expr>(VarRef)) {
2618           MarkDeclarationsReferencedInExpr(E);
2619         }
2620       }
2621       DSAStack->setForceVarCapturing(/*V=*/false);
2622     } else if (CaptureRegions.size() > 1 ||
2623                CaptureRegions.back() != OMPD_unknown) {
2624       if (auto *C = OMPClauseWithPreInit::get(Clause))
2625         PICs.push_back(C);
2626       if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
2627         if (auto *E = C->getPostUpdateExpr())
2628           MarkDeclarationsReferencedInExpr(E);
2629       }
2630     }
2631     if (Clause->getClauseKind() == OMPC_schedule)
2632       SC = cast<OMPScheduleClause>(Clause);
2633     else if (Clause->getClauseKind() == OMPC_ordered)
2634       OC = cast<OMPOrderedClause>(Clause);
2635     else if (Clause->getClauseKind() == OMPC_linear)
2636       LCs.push_back(cast<OMPLinearClause>(Clause));
2637   }
2638   // OpenMP, 2.7.1 Loop Construct, Restrictions
2639   // The nonmonotonic modifier cannot be specified if an ordered clause is
2640   // specified.
2641   if (SC &&
2642       (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2643        SC->getSecondScheduleModifier() ==
2644            OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2645       OC) {
2646     Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2647              ? SC->getFirstScheduleModifierLoc()
2648              : SC->getSecondScheduleModifierLoc(),
2649          diag::err_omp_schedule_nonmonotonic_ordered)
2650         << SourceRange(OC->getLocStart(), OC->getLocEnd());
2651     ErrorFound = true;
2652   }
2653   if (!LCs.empty() && OC && OC->getNumForLoops()) {
2654     for (auto *C : LCs) {
2655       Diag(C->getLocStart(), diag::err_omp_linear_ordered)
2656           << SourceRange(OC->getLocStart(), OC->getLocEnd());
2657     }
2658     ErrorFound = true;
2659   }
2660   if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2661       isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2662       OC->getNumForLoops()) {
2663     Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
2664         << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2665     ErrorFound = true;
2666   }
2667   if (ErrorFound) {
2668     return StmtError();
2669   }
2670   StmtResult SR = S;
2671   for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
2672     // Mark all variables in private list clauses as used in inner region.
2673     // Required for proper codegen of combined directives.
2674     // TODO: add processing for other clauses.
2675     if (ThisCaptureRegion != OMPD_unknown) {
2676       for (auto *C : PICs) {
2677         OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2678         // Find the particular capture region for the clause if the
2679         // directive is a combined one with multiple capture regions.
2680         // If the directive is not a combined one, the capture region
2681         // associated with the clause is OMPD_unknown and is generated
2682         // only once.
2683         if (CaptureRegion == ThisCaptureRegion ||
2684             CaptureRegion == OMPD_unknown) {
2685           if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
2686             for (auto *D : DS->decls())
2687               MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2688           }
2689         }
2690       }
2691     }
2692     SR = ActOnCapturedRegionEnd(SR.get());
2693   }
2694   return SR;
2695 }
2696 
2697 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2698                               OpenMPDirectiveKind CancelRegion,
2699                               SourceLocation StartLoc) {
2700   // CancelRegion is only needed for cancel and cancellation_point.
2701   if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2702     return false;
2703 
2704   if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2705       CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2706     return false;
2707 
2708   SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2709       << getOpenMPDirectiveName(CancelRegion);
2710   return true;
2711 }
2712 
2713 static bool checkNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
2714                                   OpenMPDirectiveKind CurrentRegion,
2715                                   const DeclarationNameInfo &CurrentName,
2716                                   OpenMPDirectiveKind CancelRegion,
2717                                   SourceLocation StartLoc) {
2718   if (Stack->getCurScope()) {
2719     auto ParentRegion = Stack->getParentDirective();
2720     auto OffendingRegion = ParentRegion;
2721     bool NestingProhibited = false;
2722     bool CloseNesting = true;
2723     bool OrphanSeen = false;
2724     enum {
2725       NoRecommend,
2726       ShouldBeInParallelRegion,
2727       ShouldBeInOrderedRegion,
2728       ShouldBeInTargetRegion,
2729       ShouldBeInTeamsRegion
2730     } Recommend = NoRecommend;
2731     if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
2732       // OpenMP [2.16, Nesting of Regions]
2733       // OpenMP constructs may not be nested inside a simd region.
2734       // OpenMP [2.8.1,simd Construct, Restrictions]
2735       // An ordered construct with the simd clause is the only OpenMP
2736       // construct that can appear in the simd region.
2737       // Allowing a SIMD construct nested in another SIMD construct is an
2738       // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2739       // message.
2740       SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
2741                                  ? diag::err_omp_prohibited_region_simd
2742                                  : diag::warn_omp_nesting_simd);
2743       return CurrentRegion != OMPD_simd;
2744     }
2745     if (ParentRegion == OMPD_atomic) {
2746       // OpenMP [2.16, Nesting of Regions]
2747       // OpenMP constructs may not be nested inside an atomic region.
2748       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2749       return true;
2750     }
2751     if (CurrentRegion == OMPD_section) {
2752       // OpenMP [2.7.2, sections Construct, Restrictions]
2753       // Orphaned section directives are prohibited. That is, the section
2754       // directives must appear within the sections construct and must not be
2755       // encountered elsewhere in the sections region.
2756       if (ParentRegion != OMPD_sections &&
2757           ParentRegion != OMPD_parallel_sections) {
2758         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2759             << (ParentRegion != OMPD_unknown)
2760             << getOpenMPDirectiveName(ParentRegion);
2761         return true;
2762       }
2763       return false;
2764     }
2765     // Allow some constructs (except teams) to be orphaned (they could be
2766     // used in functions, called from OpenMP regions with the required
2767     // preconditions).
2768     if (ParentRegion == OMPD_unknown &&
2769         !isOpenMPNestingTeamsDirective(CurrentRegion))
2770       return false;
2771     if (CurrentRegion == OMPD_cancellation_point ||
2772         CurrentRegion == OMPD_cancel) {
2773       // OpenMP [2.16, Nesting of Regions]
2774       // A cancellation point construct for which construct-type-clause is
2775       // taskgroup must be nested inside a task construct. A cancellation
2776       // point construct for which construct-type-clause is not taskgroup must
2777       // be closely nested inside an OpenMP construct that matches the type
2778       // specified in construct-type-clause.
2779       // A cancel construct for which construct-type-clause is taskgroup must be
2780       // nested inside a task construct. A cancel construct for which
2781       // construct-type-clause is not taskgroup must be closely nested inside an
2782       // OpenMP construct that matches the type specified in
2783       // construct-type-clause.
2784       NestingProhibited =
2785           !((CancelRegion == OMPD_parallel &&
2786              (ParentRegion == OMPD_parallel ||
2787               ParentRegion == OMPD_target_parallel)) ||
2788             (CancelRegion == OMPD_for &&
2789              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2790               ParentRegion == OMPD_target_parallel_for ||
2791               ParentRegion == OMPD_distribute_parallel_for ||
2792               ParentRegion == OMPD_teams_distribute_parallel_for ||
2793               ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
2794             (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2795             (CancelRegion == OMPD_sections &&
2796              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2797               ParentRegion == OMPD_parallel_sections)));
2798     } else if (CurrentRegion == OMPD_master) {
2799       // OpenMP [2.16, Nesting of Regions]
2800       // A master region may not be closely nested inside a worksharing,
2801       // atomic, or explicit task region.
2802       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2803                           isOpenMPTaskingDirective(ParentRegion);
2804     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2805       // OpenMP [2.16, Nesting of Regions]
2806       // A critical region may not be nested (closely or otherwise) inside a
2807       // critical region with the same name. Note that this restriction is not
2808       // sufficient to prevent deadlock.
2809       SourceLocation PreviousCriticalLoc;
2810       bool DeadLock = Stack->hasDirective(
2811           [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
2812                                               const DeclarationNameInfo &DNI,
2813                                               SourceLocation Loc) -> bool {
2814             if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
2815               PreviousCriticalLoc = Loc;
2816               return true;
2817             } else
2818               return false;
2819           },
2820           false /* skip top directive */);
2821       if (DeadLock) {
2822         SemaRef.Diag(StartLoc,
2823                      diag::err_omp_prohibited_region_critical_same_name)
2824             << CurrentName.getName();
2825         if (PreviousCriticalLoc.isValid())
2826           SemaRef.Diag(PreviousCriticalLoc,
2827                        diag::note_omp_previous_critical_region);
2828         return true;
2829       }
2830     } else if (CurrentRegion == OMPD_barrier) {
2831       // OpenMP [2.16, Nesting of Regions]
2832       // A barrier region may not be closely nested inside a worksharing,
2833       // explicit task, critical, ordered, atomic, or master region.
2834       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2835                           isOpenMPTaskingDirective(ParentRegion) ||
2836                           ParentRegion == OMPD_master ||
2837                           ParentRegion == OMPD_critical ||
2838                           ParentRegion == OMPD_ordered;
2839     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
2840                !isOpenMPParallelDirective(CurrentRegion) &&
2841                !isOpenMPTeamsDirective(CurrentRegion)) {
2842       // OpenMP [2.16, Nesting of Regions]
2843       // A worksharing region may not be closely nested inside a worksharing,
2844       // explicit task, critical, ordered, atomic, or master region.
2845       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2846                           isOpenMPTaskingDirective(ParentRegion) ||
2847                           ParentRegion == OMPD_master ||
2848                           ParentRegion == OMPD_critical ||
2849                           ParentRegion == OMPD_ordered;
2850       Recommend = ShouldBeInParallelRegion;
2851     } else if (CurrentRegion == OMPD_ordered) {
2852       // OpenMP [2.16, Nesting of Regions]
2853       // An ordered region may not be closely nested inside a critical,
2854       // atomic, or explicit task region.
2855       // An ordered region must be closely nested inside a loop region (or
2856       // parallel loop region) with an ordered clause.
2857       // OpenMP [2.8.1,simd Construct, Restrictions]
2858       // An ordered construct with the simd clause is the only OpenMP construct
2859       // that can appear in the simd region.
2860       NestingProhibited = ParentRegion == OMPD_critical ||
2861                           isOpenMPTaskingDirective(ParentRegion) ||
2862                           !(isOpenMPSimdDirective(ParentRegion) ||
2863                             Stack->isParentOrderedRegion());
2864       Recommend = ShouldBeInOrderedRegion;
2865     } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
2866       // OpenMP [2.16, Nesting of Regions]
2867       // If specified, a teams construct must be contained within a target
2868       // construct.
2869       NestingProhibited = ParentRegion != OMPD_target;
2870       OrphanSeen = ParentRegion == OMPD_unknown;
2871       Recommend = ShouldBeInTargetRegion;
2872     }
2873     if (!NestingProhibited &&
2874         !isOpenMPTargetExecutionDirective(CurrentRegion) &&
2875         !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
2876         (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
2877       // OpenMP [2.16, Nesting of Regions]
2878       // distribute, parallel, parallel sections, parallel workshare, and the
2879       // parallel loop and parallel loop SIMD constructs are the only OpenMP
2880       // constructs that can be closely nested in the teams region.
2881       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2882                           !isOpenMPDistributeDirective(CurrentRegion);
2883       Recommend = ShouldBeInParallelRegion;
2884     }
2885     if (!NestingProhibited &&
2886         isOpenMPNestingDistributeDirective(CurrentRegion)) {
2887       // OpenMP 4.5 [2.17 Nesting of Regions]
2888       // The region associated with the distribute construct must be strictly
2889       // nested inside a teams region
2890       NestingProhibited =
2891           (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
2892       Recommend = ShouldBeInTeamsRegion;
2893     }
2894     if (!NestingProhibited &&
2895         (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2896          isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2897       // OpenMP 4.5 [2.17 Nesting of Regions]
2898       // If a target, target update, target data, target enter data, or
2899       // target exit data construct is encountered during execution of a
2900       // target region, the behavior is unspecified.
2901       NestingProhibited = Stack->hasDirective(
2902           [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2903                              SourceLocation) -> bool {
2904             if (isOpenMPTargetExecutionDirective(K)) {
2905               OffendingRegion = K;
2906               return true;
2907             } else
2908               return false;
2909           },
2910           false /* don't skip top directive */);
2911       CloseNesting = false;
2912     }
2913     if (NestingProhibited) {
2914       if (OrphanSeen) {
2915         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
2916             << getOpenMPDirectiveName(CurrentRegion) << Recommend;
2917       } else {
2918         SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
2919             << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2920             << Recommend << getOpenMPDirectiveName(CurrentRegion);
2921       }
2922       return true;
2923     }
2924   }
2925   return false;
2926 }
2927 
2928 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2929                            ArrayRef<OMPClause *> Clauses,
2930                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2931   bool ErrorFound = false;
2932   unsigned NamedModifiersNumber = 0;
2933   SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2934       OMPD_unknown + 1);
2935   SmallVector<SourceLocation, 4> NameModifierLoc;
2936   for (const auto *C : Clauses) {
2937     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2938       // At most one if clause without a directive-name-modifier can appear on
2939       // the directive.
2940       OpenMPDirectiveKind CurNM = IC->getNameModifier();
2941       if (FoundNameModifiers[CurNM]) {
2942         S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2943             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2944             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2945         ErrorFound = true;
2946       } else if (CurNM != OMPD_unknown) {
2947         NameModifierLoc.push_back(IC->getNameModifierLoc());
2948         ++NamedModifiersNumber;
2949       }
2950       FoundNameModifiers[CurNM] = IC;
2951       if (CurNM == OMPD_unknown)
2952         continue;
2953       // Check if the specified name modifier is allowed for the current
2954       // directive.
2955       // At most one if clause with the particular directive-name-modifier can
2956       // appear on the directive.
2957       bool MatchFound = false;
2958       for (auto NM : AllowedNameModifiers) {
2959         if (CurNM == NM) {
2960           MatchFound = true;
2961           break;
2962         }
2963       }
2964       if (!MatchFound) {
2965         S.Diag(IC->getNameModifierLoc(),
2966                diag::err_omp_wrong_if_directive_name_modifier)
2967             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2968         ErrorFound = true;
2969       }
2970     }
2971   }
2972   // If any if clause on the directive includes a directive-name-modifier then
2973   // all if clauses on the directive must include a directive-name-modifier.
2974   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2975     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2976       S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2977              diag::err_omp_no_more_if_clause);
2978     } else {
2979       std::string Values;
2980       std::string Sep(", ");
2981       unsigned AllowedCnt = 0;
2982       unsigned TotalAllowedNum =
2983           AllowedNameModifiers.size() - NamedModifiersNumber;
2984       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2985            ++Cnt) {
2986         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2987         if (!FoundNameModifiers[NM]) {
2988           Values += "'";
2989           Values += getOpenMPDirectiveName(NM);
2990           Values += "'";
2991           if (AllowedCnt + 2 == TotalAllowedNum)
2992             Values += " or ";
2993           else if (AllowedCnt + 1 != TotalAllowedNum)
2994             Values += Sep;
2995           ++AllowedCnt;
2996         }
2997       }
2998       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2999              diag::err_omp_unnamed_if_clause)
3000           << (TotalAllowedNum > 1) << Values;
3001     }
3002     for (auto Loc : NameModifierLoc) {
3003       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3004     }
3005     ErrorFound = true;
3006   }
3007   return ErrorFound;
3008 }
3009 
3010 StmtResult Sema::ActOnOpenMPExecutableDirective(
3011     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3012     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3013     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
3014   StmtResult Res = StmtError();
3015   // First check CancelRegion which is then used in checkNestingOfRegions.
3016   if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3017       checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3018                             StartLoc))
3019     return StmtError();
3020 
3021   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
3022   llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
3023   bool ErrorFound = false;
3024   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
3025   if (AStmt && !CurContext->isDependentContext()) {
3026     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3027 
3028     // Check default data sharing attributes for referenced variables.
3029     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3030     int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3031     Stmt *S = AStmt;
3032     while (--ThisCaptureLevel >= 0)
3033       S = cast<CapturedStmt>(S)->getCapturedStmt();
3034     DSAChecker.Visit(S);
3035     if (DSAChecker.isErrorFound())
3036       return StmtError();
3037     // Generate list of implicitly defined firstprivate variables.
3038     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
3039 
3040     SmallVector<Expr *, 4> ImplicitFirstprivates(
3041         DSAChecker.getImplicitFirstprivate().begin(),
3042         DSAChecker.getImplicitFirstprivate().end());
3043     SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3044                                         DSAChecker.getImplicitMap().end());
3045     // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
3046     for (auto *C : Clauses) {
3047       if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
3048         for (auto *E : IRC->taskgroup_descriptors())
3049           if (E)
3050             ImplicitFirstprivates.emplace_back(E);
3051       }
3052     }
3053     if (!ImplicitFirstprivates.empty()) {
3054       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3055               ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3056               SourceLocation())) {
3057         ClausesWithImplicit.push_back(Implicit);
3058         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3059                      ImplicitFirstprivates.size();
3060       } else
3061         ErrorFound = true;
3062     }
3063     if (!ImplicitMaps.empty()) {
3064       if (OMPClause *Implicit = ActOnOpenMPMapClause(
3065               OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
3066               SourceLocation(), SourceLocation(), ImplicitMaps,
3067               SourceLocation(), SourceLocation(), SourceLocation())) {
3068         ClausesWithImplicit.emplace_back(Implicit);
3069         ErrorFound |=
3070             cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
3071       } else
3072         ErrorFound = true;
3073     }
3074   }
3075 
3076   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
3077   switch (Kind) {
3078   case OMPD_parallel:
3079     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3080                                        EndLoc);
3081     AllowedNameModifiers.push_back(OMPD_parallel);
3082     break;
3083   case OMPD_simd:
3084     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3085                                    VarsWithInheritedDSA);
3086     break;
3087   case OMPD_for:
3088     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3089                                   VarsWithInheritedDSA);
3090     break;
3091   case OMPD_for_simd:
3092     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3093                                       EndLoc, VarsWithInheritedDSA);
3094     break;
3095   case OMPD_sections:
3096     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3097                                        EndLoc);
3098     break;
3099   case OMPD_section:
3100     assert(ClausesWithImplicit.empty() &&
3101            "No clauses are allowed for 'omp section' directive");
3102     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3103     break;
3104   case OMPD_single:
3105     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3106                                      EndLoc);
3107     break;
3108   case OMPD_master:
3109     assert(ClausesWithImplicit.empty() &&
3110            "No clauses are allowed for 'omp master' directive");
3111     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3112     break;
3113   case OMPD_critical:
3114     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3115                                        StartLoc, EndLoc);
3116     break;
3117   case OMPD_parallel_for:
3118     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3119                                           EndLoc, VarsWithInheritedDSA);
3120     AllowedNameModifiers.push_back(OMPD_parallel);
3121     break;
3122   case OMPD_parallel_for_simd:
3123     Res = ActOnOpenMPParallelForSimdDirective(
3124         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3125     AllowedNameModifiers.push_back(OMPD_parallel);
3126     break;
3127   case OMPD_parallel_sections:
3128     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3129                                                StartLoc, EndLoc);
3130     AllowedNameModifiers.push_back(OMPD_parallel);
3131     break;
3132   case OMPD_task:
3133     Res =
3134         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3135     AllowedNameModifiers.push_back(OMPD_task);
3136     break;
3137   case OMPD_taskyield:
3138     assert(ClausesWithImplicit.empty() &&
3139            "No clauses are allowed for 'omp taskyield' directive");
3140     assert(AStmt == nullptr &&
3141            "No associated statement allowed for 'omp taskyield' directive");
3142     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3143     break;
3144   case OMPD_barrier:
3145     assert(ClausesWithImplicit.empty() &&
3146            "No clauses are allowed for 'omp barrier' directive");
3147     assert(AStmt == nullptr &&
3148            "No associated statement allowed for 'omp barrier' directive");
3149     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3150     break;
3151   case OMPD_taskwait:
3152     assert(ClausesWithImplicit.empty() &&
3153            "No clauses are allowed for 'omp taskwait' directive");
3154     assert(AStmt == nullptr &&
3155            "No associated statement allowed for 'omp taskwait' directive");
3156     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3157     break;
3158   case OMPD_taskgroup:
3159     Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3160                                         EndLoc);
3161     break;
3162   case OMPD_flush:
3163     assert(AStmt == nullptr &&
3164            "No associated statement allowed for 'omp flush' directive");
3165     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3166     break;
3167   case OMPD_ordered:
3168     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3169                                       EndLoc);
3170     break;
3171   case OMPD_atomic:
3172     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3173                                      EndLoc);
3174     break;
3175   case OMPD_teams:
3176     Res =
3177         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3178     break;
3179   case OMPD_target:
3180     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3181                                      EndLoc);
3182     AllowedNameModifiers.push_back(OMPD_target);
3183     break;
3184   case OMPD_target_parallel:
3185     Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3186                                              StartLoc, EndLoc);
3187     AllowedNameModifiers.push_back(OMPD_target);
3188     AllowedNameModifiers.push_back(OMPD_parallel);
3189     break;
3190   case OMPD_target_parallel_for:
3191     Res = ActOnOpenMPTargetParallelForDirective(
3192         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3193     AllowedNameModifiers.push_back(OMPD_target);
3194     AllowedNameModifiers.push_back(OMPD_parallel);
3195     break;
3196   case OMPD_cancellation_point:
3197     assert(ClausesWithImplicit.empty() &&
3198            "No clauses are allowed for 'omp cancellation point' directive");
3199     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3200                                "cancellation point' directive");
3201     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3202     break;
3203   case OMPD_cancel:
3204     assert(AStmt == nullptr &&
3205            "No associated statement allowed for 'omp cancel' directive");
3206     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3207                                      CancelRegion);
3208     AllowedNameModifiers.push_back(OMPD_cancel);
3209     break;
3210   case OMPD_target_data:
3211     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3212                                          EndLoc);
3213     AllowedNameModifiers.push_back(OMPD_target_data);
3214     break;
3215   case OMPD_target_enter_data:
3216     Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3217                                               EndLoc, AStmt);
3218     AllowedNameModifiers.push_back(OMPD_target_enter_data);
3219     break;
3220   case OMPD_target_exit_data:
3221     Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3222                                              EndLoc, AStmt);
3223     AllowedNameModifiers.push_back(OMPD_target_exit_data);
3224     break;
3225   case OMPD_taskloop:
3226     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3227                                        EndLoc, VarsWithInheritedDSA);
3228     AllowedNameModifiers.push_back(OMPD_taskloop);
3229     break;
3230   case OMPD_taskloop_simd:
3231     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3232                                            EndLoc, VarsWithInheritedDSA);
3233     AllowedNameModifiers.push_back(OMPD_taskloop);
3234     break;
3235   case OMPD_distribute:
3236     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3237                                          EndLoc, VarsWithInheritedDSA);
3238     break;
3239   case OMPD_target_update:
3240     Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3241                                            EndLoc, AStmt);
3242     AllowedNameModifiers.push_back(OMPD_target_update);
3243     break;
3244   case OMPD_distribute_parallel_for:
3245     Res = ActOnOpenMPDistributeParallelForDirective(
3246         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3247     AllowedNameModifiers.push_back(OMPD_parallel);
3248     break;
3249   case OMPD_distribute_parallel_for_simd:
3250     Res = ActOnOpenMPDistributeParallelForSimdDirective(
3251         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3252     AllowedNameModifiers.push_back(OMPD_parallel);
3253     break;
3254   case OMPD_distribute_simd:
3255     Res = ActOnOpenMPDistributeSimdDirective(
3256         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3257     break;
3258   case OMPD_target_parallel_for_simd:
3259     Res = ActOnOpenMPTargetParallelForSimdDirective(
3260         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3261     AllowedNameModifiers.push_back(OMPD_target);
3262     AllowedNameModifiers.push_back(OMPD_parallel);
3263     break;
3264   case OMPD_target_simd:
3265     Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3266                                          EndLoc, VarsWithInheritedDSA);
3267     AllowedNameModifiers.push_back(OMPD_target);
3268     break;
3269   case OMPD_teams_distribute:
3270     Res = ActOnOpenMPTeamsDistributeDirective(
3271         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3272     break;
3273   case OMPD_teams_distribute_simd:
3274     Res = ActOnOpenMPTeamsDistributeSimdDirective(
3275         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3276     break;
3277   case OMPD_teams_distribute_parallel_for_simd:
3278     Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3279         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3280     AllowedNameModifiers.push_back(OMPD_parallel);
3281     break;
3282   case OMPD_teams_distribute_parallel_for:
3283     Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3284         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3285     AllowedNameModifiers.push_back(OMPD_parallel);
3286     break;
3287   case OMPD_target_teams:
3288     Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3289                                           EndLoc);
3290     AllowedNameModifiers.push_back(OMPD_target);
3291     break;
3292   case OMPD_target_teams_distribute:
3293     Res = ActOnOpenMPTargetTeamsDistributeDirective(
3294         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3295     AllowedNameModifiers.push_back(OMPD_target);
3296     break;
3297   case OMPD_target_teams_distribute_parallel_for:
3298     Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3299         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3300     AllowedNameModifiers.push_back(OMPD_target);
3301     AllowedNameModifiers.push_back(OMPD_parallel);
3302     break;
3303   case OMPD_target_teams_distribute_parallel_for_simd:
3304     Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3305         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3306     AllowedNameModifiers.push_back(OMPD_target);
3307     AllowedNameModifiers.push_back(OMPD_parallel);
3308     break;
3309   case OMPD_target_teams_distribute_simd:
3310     Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3311         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3312     AllowedNameModifiers.push_back(OMPD_target);
3313     break;
3314   case OMPD_declare_target:
3315   case OMPD_end_declare_target:
3316   case OMPD_threadprivate:
3317   case OMPD_declare_reduction:
3318   case OMPD_declare_simd:
3319     llvm_unreachable("OpenMP Directive is not allowed");
3320   case OMPD_unknown:
3321     llvm_unreachable("Unknown OpenMP directive");
3322   }
3323 
3324   for (auto P : VarsWithInheritedDSA) {
3325     Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3326         << P.first << P.second->getSourceRange();
3327   }
3328   ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3329 
3330   if (!AllowedNameModifiers.empty())
3331     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3332                  ErrorFound;
3333 
3334   if (ErrorFound)
3335     return StmtError();
3336   return Res;
3337 }
3338 
3339 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3340     DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
3341     ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
3342     ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3343     ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
3344   assert(Aligneds.size() == Alignments.size());
3345   assert(Linears.size() == LinModifiers.size());
3346   assert(Linears.size() == Steps.size());
3347   if (!DG || DG.get().isNull())
3348     return DeclGroupPtrTy();
3349 
3350   if (!DG.get().isSingleDecl()) {
3351     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
3352     return DG;
3353   }
3354   auto *ADecl = DG.get().getSingleDecl();
3355   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3356     ADecl = FTD->getTemplatedDecl();
3357 
3358   auto *FD = dyn_cast<FunctionDecl>(ADecl);
3359   if (!FD) {
3360     Diag(ADecl->getLocation(), diag::err_omp_function_expected);
3361     return DeclGroupPtrTy();
3362   }
3363 
3364   // OpenMP [2.8.2, declare simd construct, Description]
3365   // The parameter of the simdlen clause must be a constant positive integer
3366   // expression.
3367   ExprResult SL;
3368   if (Simdlen)
3369     SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
3370   // OpenMP [2.8.2, declare simd construct, Description]
3371   // The special this pointer can be used as if was one of the arguments to the
3372   // function in any of the linear, aligned, or uniform clauses.
3373   // The uniform clause declares one or more arguments to have an invariant
3374   // value for all concurrent invocations of the function in the execution of a
3375   // single SIMD loop.
3376   llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3377   Expr *UniformedLinearThis = nullptr;
3378   for (auto *E : Uniforms) {
3379     E = E->IgnoreParenImpCasts();
3380     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3381       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3382         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3383             FD->getParamDecl(PVD->getFunctionScopeIndex())
3384                     ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3385           UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
3386           continue;
3387         }
3388     if (isa<CXXThisExpr>(E)) {
3389       UniformedLinearThis = E;
3390       continue;
3391     }
3392     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3393         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3394   }
3395   // OpenMP [2.8.2, declare simd construct, Description]
3396   // The aligned clause declares that the object to which each list item points
3397   // is aligned to the number of bytes expressed in the optional parameter of
3398   // the aligned clause.
3399   // The special this pointer can be used as if was one of the arguments to the
3400   // function in any of the linear, aligned, or uniform clauses.
3401   // The type of list items appearing in the aligned clause must be array,
3402   // pointer, reference to array, or reference to pointer.
3403   llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3404   Expr *AlignedThis = nullptr;
3405   for (auto *E : Aligneds) {
3406     E = E->IgnoreParenImpCasts();
3407     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3408       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3409         auto *CanonPVD = PVD->getCanonicalDecl();
3410         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3411             FD->getParamDecl(PVD->getFunctionScopeIndex())
3412                     ->getCanonicalDecl() == CanonPVD) {
3413           // OpenMP  [2.8.1, simd construct, Restrictions]
3414           // A list-item cannot appear in more than one aligned clause.
3415           if (AlignedArgs.count(CanonPVD) > 0) {
3416             Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3417                 << 1 << E->getSourceRange();
3418             Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3419                  diag::note_omp_explicit_dsa)
3420                 << getOpenMPClauseName(OMPC_aligned);
3421             continue;
3422           }
3423           AlignedArgs[CanonPVD] = E;
3424           QualType QTy = PVD->getType()
3425                              .getNonReferenceType()
3426                              .getUnqualifiedType()
3427                              .getCanonicalType();
3428           const Type *Ty = QTy.getTypePtrOrNull();
3429           if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3430             Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3431                 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3432             Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3433           }
3434           continue;
3435         }
3436       }
3437     if (isa<CXXThisExpr>(E)) {
3438       if (AlignedThis) {
3439         Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3440             << 2 << E->getSourceRange();
3441         Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3442             << getOpenMPClauseName(OMPC_aligned);
3443       }
3444       AlignedThis = E;
3445       continue;
3446     }
3447     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3448         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3449   }
3450   // The optional parameter of the aligned clause, alignment, must be a constant
3451   // positive integer expression. If no optional parameter is specified,
3452   // implementation-defined default alignments for SIMD instructions on the
3453   // target platforms are assumed.
3454   SmallVector<Expr *, 4> NewAligns;
3455   for (auto *E : Alignments) {
3456     ExprResult Align;
3457     if (E)
3458       Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3459     NewAligns.push_back(Align.get());
3460   }
3461   // OpenMP [2.8.2, declare simd construct, Description]
3462   // The linear clause declares one or more list items to be private to a SIMD
3463   // lane and to have a linear relationship with respect to the iteration space
3464   // of a loop.
3465   // The special this pointer can be used as if was one of the arguments to the
3466   // function in any of the linear, aligned, or uniform clauses.
3467   // When a linear-step expression is specified in a linear clause it must be
3468   // either a constant integer expression or an integer-typed parameter that is
3469   // specified in a uniform clause on the directive.
3470   llvm::DenseMap<Decl *, Expr *> LinearArgs;
3471   const bool IsUniformedThis = UniformedLinearThis != nullptr;
3472   auto MI = LinModifiers.begin();
3473   for (auto *E : Linears) {
3474     auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3475     ++MI;
3476     E = E->IgnoreParenImpCasts();
3477     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3478       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3479         auto *CanonPVD = PVD->getCanonicalDecl();
3480         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3481             FD->getParamDecl(PVD->getFunctionScopeIndex())
3482                     ->getCanonicalDecl() == CanonPVD) {
3483           // OpenMP  [2.15.3.7, linear Clause, Restrictions]
3484           // A list-item cannot appear in more than one linear clause.
3485           if (LinearArgs.count(CanonPVD) > 0) {
3486             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3487                 << getOpenMPClauseName(OMPC_linear)
3488                 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3489             Diag(LinearArgs[CanonPVD]->getExprLoc(),
3490                  diag::note_omp_explicit_dsa)
3491                 << getOpenMPClauseName(OMPC_linear);
3492             continue;
3493           }
3494           // Each argument can appear in at most one uniform or linear clause.
3495           if (UniformedArgs.count(CanonPVD) > 0) {
3496             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3497                 << getOpenMPClauseName(OMPC_linear)
3498                 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3499             Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3500                  diag::note_omp_explicit_dsa)
3501                 << getOpenMPClauseName(OMPC_uniform);
3502             continue;
3503           }
3504           LinearArgs[CanonPVD] = E;
3505           if (E->isValueDependent() || E->isTypeDependent() ||
3506               E->isInstantiationDependent() ||
3507               E->containsUnexpandedParameterPack())
3508             continue;
3509           (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3510                                       PVD->getOriginalType());
3511           continue;
3512         }
3513       }
3514     if (isa<CXXThisExpr>(E)) {
3515       if (UniformedLinearThis) {
3516         Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3517             << getOpenMPClauseName(OMPC_linear)
3518             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3519             << E->getSourceRange();
3520         Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3521             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3522                                                    : OMPC_linear);
3523         continue;
3524       }
3525       UniformedLinearThis = E;
3526       if (E->isValueDependent() || E->isTypeDependent() ||
3527           E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3528         continue;
3529       (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3530                                   E->getType());
3531       continue;
3532     }
3533     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3534         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3535   }
3536   Expr *Step = nullptr;
3537   Expr *NewStep = nullptr;
3538   SmallVector<Expr *, 4> NewSteps;
3539   for (auto *E : Steps) {
3540     // Skip the same step expression, it was checked already.
3541     if (Step == E || !E) {
3542       NewSteps.push_back(E ? NewStep : nullptr);
3543       continue;
3544     }
3545     Step = E;
3546     if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3547       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3548         auto *CanonPVD = PVD->getCanonicalDecl();
3549         if (UniformedArgs.count(CanonPVD) == 0) {
3550           Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3551               << Step->getSourceRange();
3552         } else if (E->isValueDependent() || E->isTypeDependent() ||
3553                    E->isInstantiationDependent() ||
3554                    E->containsUnexpandedParameterPack() ||
3555                    CanonPVD->getType()->hasIntegerRepresentation())
3556           NewSteps.push_back(Step);
3557         else {
3558           Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3559               << Step->getSourceRange();
3560         }
3561         continue;
3562       }
3563     NewStep = Step;
3564     if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3565         !Step->isInstantiationDependent() &&
3566         !Step->containsUnexpandedParameterPack()) {
3567       NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3568                     .get();
3569       if (NewStep)
3570         NewStep = VerifyIntegerConstantExpression(NewStep).get();
3571     }
3572     NewSteps.push_back(NewStep);
3573   }
3574   auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3575       Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
3576       Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
3577       const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3578       const_cast<Expr **>(Linears.data()), Linears.size(),
3579       const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3580       NewSteps.data(), NewSteps.size(), SR);
3581   ADecl->addAttr(NewAttr);
3582   return ConvertDeclToDeclGroup(ADecl);
3583 }
3584 
3585 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3586                                               Stmt *AStmt,
3587                                               SourceLocation StartLoc,
3588                                               SourceLocation EndLoc) {
3589   if (!AStmt)
3590     return StmtError();
3591 
3592   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3593   // 1.2.2 OpenMP Language Terminology
3594   // Structured block - An executable statement with a single entry at the
3595   // top and a single exit at the bottom.
3596   // The point of exit cannot be a branch out of the structured block.
3597   // longjmp() and throw() must not violate the entry/exit criteria.
3598   CS->getCapturedDecl()->setNothrow();
3599 
3600   setFunctionHasBranchProtectedScope();
3601 
3602   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3603                                       DSAStack->isCancelRegion());
3604 }
3605 
3606 namespace {
3607 /// \brief Helper class for checking canonical form of the OpenMP loops and
3608 /// extracting iteration space of each loop in the loop nest, that will be used
3609 /// for IR generation.
3610 class OpenMPIterationSpaceChecker {
3611   /// \brief Reference to Sema.
3612   Sema &SemaRef;
3613   /// \brief A location for diagnostics (when there is no some better location).
3614   SourceLocation DefaultLoc;
3615   /// \brief A location for diagnostics (when increment is not compatible).
3616   SourceLocation ConditionLoc;
3617   /// \brief A source location for referring to loop init later.
3618   SourceRange InitSrcRange;
3619   /// \brief A source location for referring to condition later.
3620   SourceRange ConditionSrcRange;
3621   /// \brief A source location for referring to increment later.
3622   SourceRange IncrementSrcRange;
3623   /// \brief Loop variable.
3624   ValueDecl *LCDecl = nullptr;
3625   /// \brief Reference to loop variable.
3626   Expr *LCRef = nullptr;
3627   /// \brief Lower bound (initializer for the var).
3628   Expr *LB = nullptr;
3629   /// \brief Upper bound.
3630   Expr *UB = nullptr;
3631   /// \brief Loop step (increment).
3632   Expr *Step = nullptr;
3633   /// \brief This flag is true when condition is one of:
3634   ///   Var <  UB
3635   ///   Var <= UB
3636   ///   UB  >  Var
3637   ///   UB  >= Var
3638   bool TestIsLessOp = false;
3639   /// \brief This flag is true when condition is strict ( < or > ).
3640   bool TestIsStrictOp = false;
3641   /// \brief This flag is true when step is subtracted on each iteration.
3642   bool SubtractStep = false;
3643 
3644 public:
3645   OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3646       : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
3647   /// \brief Check init-expr for canonical loop form and save loop counter
3648   /// variable - #Var and its initialization value - #LB.
3649   bool CheckInit(Stmt *S, bool EmitDiags = true);
3650   /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3651   /// for less/greater and for strict/non-strict comparison.
3652   bool CheckCond(Expr *S);
3653   /// \brief Check incr-expr for canonical loop form and return true if it
3654   /// does not conform, otherwise save loop step (#Step).
3655   bool CheckInc(Expr *S);
3656   /// \brief Return the loop counter variable.
3657   ValueDecl *GetLoopDecl() const { return LCDecl; }
3658   /// \brief Return the reference expression to loop counter variable.
3659   Expr *GetLoopDeclRefExpr() const { return LCRef; }
3660   /// \brief Source range of the loop init.
3661   SourceRange GetInitSrcRange() const { return InitSrcRange; }
3662   /// \brief Source range of the loop condition.
3663   SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3664   /// \brief Source range of the loop increment.
3665   SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3666   /// \brief True if the step should be subtracted.
3667   bool ShouldSubtractStep() const { return SubtractStep; }
3668   /// \brief Build the expression to calculate the number of iterations.
3669   Expr *
3670   BuildNumIterations(Scope *S, const bool LimitedType,
3671                      llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3672   /// \brief Build the precondition expression for the loops.
3673   Expr *BuildPreCond(Scope *S, Expr *Cond,
3674                      llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3675   /// \brief Build reference expression to the counter be used for codegen.
3676   DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3677                                DSAStackTy &DSA) const;
3678   /// \brief Build reference expression to the private counter be used for
3679   /// codegen.
3680   Expr *BuildPrivateCounterVar() const;
3681   /// \brief Build initialization of the counter be used for codegen.
3682   Expr *BuildCounterInit() const;
3683   /// \brief Build step of the counter be used for codegen.
3684   Expr *BuildCounterStep() const;
3685   /// \brief Return true if any expression is dependent.
3686   bool Dependent() const;
3687 
3688 private:
3689   /// \brief Check the right-hand side of an assignment in the increment
3690   /// expression.
3691   bool CheckIncRHS(Expr *RHS);
3692   /// \brief Helper to set loop counter variable and its initializer.
3693   bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
3694   /// \brief Helper to set upper bound.
3695   bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
3696              SourceLocation SL);
3697   /// \brief Helper to set loop increment.
3698   bool SetStep(Expr *NewStep, bool Subtract);
3699 };
3700 
3701 bool OpenMPIterationSpaceChecker::Dependent() const {
3702   if (!LCDecl) {
3703     assert(!LB && !UB && !Step);
3704     return false;
3705   }
3706   return LCDecl->getType()->isDependentType() ||
3707          (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3708          (Step && Step->isValueDependent());
3709 }
3710 
3711 bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3712                                                  Expr *NewLCRefExpr,
3713                                                  Expr *NewLB) {
3714   // State consistency checking to ensure correct usage.
3715   assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
3716          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
3717   if (!NewLCDecl || !NewLB)
3718     return true;
3719   LCDecl = getCanonicalDecl(NewLCDecl);
3720   LCRef = NewLCRefExpr;
3721   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3722     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3723       if ((Ctor->isCopyOrMoveConstructor() ||
3724            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3725           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3726         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
3727   LB = NewLB;
3728   return false;
3729 }
3730 
3731 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
3732                                         SourceRange SR, SourceLocation SL) {
3733   // State consistency checking to ensure correct usage.
3734   assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3735          Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
3736   if (!NewUB)
3737     return true;
3738   UB = NewUB;
3739   TestIsLessOp = LessOp;
3740   TestIsStrictOp = StrictOp;
3741   ConditionSrcRange = SR;
3742   ConditionLoc = SL;
3743   return false;
3744 }
3745 
3746 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3747   // State consistency checking to ensure correct usage.
3748   assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
3749   if (!NewStep)
3750     return true;
3751   if (!NewStep->isValueDependent()) {
3752     // Check that the step is integer expression.
3753     SourceLocation StepLoc = NewStep->getLocStart();
3754     ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
3755         StepLoc, getExprAsWritten(NewStep));
3756     if (Val.isInvalid())
3757       return true;
3758     NewStep = Val.get();
3759 
3760     // OpenMP [2.6, Canonical Loop Form, Restrictions]
3761     //  If test-expr is of form var relational-op b and relational-op is < or
3762     //  <= then incr-expr must cause var to increase on each iteration of the
3763     //  loop. If test-expr is of form var relational-op b and relational-op is
3764     //  > or >= then incr-expr must cause var to decrease on each iteration of
3765     //  the loop.
3766     //  If test-expr is of form b relational-op var and relational-op is < or
3767     //  <= then incr-expr must cause var to decrease on each iteration of the
3768     //  loop. If test-expr is of form b relational-op var and relational-op is
3769     //  > or >= then incr-expr must cause var to increase on each iteration of
3770     //  the loop.
3771     llvm::APSInt Result;
3772     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3773     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3774     bool IsConstNeg =
3775         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
3776     bool IsConstPos =
3777         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
3778     bool IsConstZero = IsConstant && !Result.getBoolValue();
3779     if (UB && (IsConstZero ||
3780                (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
3781                              : (IsConstPos || (IsUnsigned && !Subtract))))) {
3782       SemaRef.Diag(NewStep->getExprLoc(),
3783                    diag::err_omp_loop_incr_not_compatible)
3784           << LCDecl << TestIsLessOp << NewStep->getSourceRange();
3785       SemaRef.Diag(ConditionLoc,
3786                    diag::note_omp_loop_cond_requres_compatible_incr)
3787           << TestIsLessOp << ConditionSrcRange;
3788       return true;
3789     }
3790     if (TestIsLessOp == Subtract) {
3791       NewStep =
3792           SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
3793               .get();
3794       Subtract = !Subtract;
3795     }
3796   }
3797 
3798   Step = NewStep;
3799   SubtractStep = Subtract;
3800   return false;
3801 }
3802 
3803 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
3804   // Check init-expr for canonical loop form and save loop counter
3805   // variable - #Var and its initialization value - #LB.
3806   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3807   //   var = lb
3808   //   integer-type var = lb
3809   //   random-access-iterator-type var = lb
3810   //   pointer-type var = lb
3811   //
3812   if (!S) {
3813     if (EmitDiags) {
3814       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3815     }
3816     return true;
3817   }
3818   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3819     if (!ExprTemp->cleanupsHaveSideEffects())
3820       S = ExprTemp->getSubExpr();
3821 
3822   InitSrcRange = S->getSourceRange();
3823   if (Expr *E = dyn_cast<Expr>(S))
3824     S = E->IgnoreParens();
3825   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3826     if (BO->getOpcode() == BO_Assign) {
3827       auto *LHS = BO->getLHS()->IgnoreParens();
3828       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3829         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3830           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3831             return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3832         return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3833       }
3834       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3835         if (ME->isArrow() &&
3836             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3837           return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3838       }
3839     }
3840   } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
3841     if (DS->isSingleDecl()) {
3842       if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
3843         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
3844           // Accept non-canonical init form here but emit ext. warning.
3845           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
3846             SemaRef.Diag(S->getLocStart(),
3847                          diag::ext_omp_loop_not_canonical_init)
3848                 << S->getSourceRange();
3849           return SetLCDeclAndLB(Var, nullptr, Var->getInit());
3850         }
3851       }
3852     }
3853   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3854     if (CE->getOperator() == OO_Equal) {
3855       auto *LHS = CE->getArg(0);
3856       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3857         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3858           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3859             return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3860         return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3861       }
3862       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3863         if (ME->isArrow() &&
3864             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3865           return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3866       }
3867     }
3868   }
3869 
3870   if (Dependent() || SemaRef.CurContext->isDependentContext())
3871     return false;
3872   if (EmitDiags) {
3873     SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3874         << S->getSourceRange();
3875   }
3876   return true;
3877 }
3878 
3879 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
3880 /// variable (which may be the loop variable) if possible.
3881 static const ValueDecl *GetInitLCDecl(Expr *E) {
3882   if (!E)
3883     return nullptr;
3884   E = getExprAsWritten(E);
3885   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3886     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3887       if ((Ctor->isCopyOrMoveConstructor() ||
3888            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3889           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3890         E = CE->getArg(0)->IgnoreParenImpCasts();
3891   if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3892     if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
3893       return getCanonicalDecl(VD);
3894   }
3895   if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3896     if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3897       return getCanonicalDecl(ME->getMemberDecl());
3898   return nullptr;
3899 }
3900 
3901 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3902   // Check test-expr for canonical form, save upper-bound UB, flags for
3903   // less/greater and for strict/non-strict comparison.
3904   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3905   //   var relational-op b
3906   //   b relational-op var
3907   //
3908   if (!S) {
3909     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
3910     return true;
3911   }
3912   S = getExprAsWritten(S);
3913   SourceLocation CondLoc = S->getLocStart();
3914   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3915     if (BO->isRelationalOp()) {
3916       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3917         return SetUB(BO->getRHS(),
3918                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3919                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3920                      BO->getSourceRange(), BO->getOperatorLoc());
3921       if (GetInitLCDecl(BO->getRHS()) == LCDecl)
3922         return SetUB(BO->getLHS(),
3923                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3924                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3925                      BO->getSourceRange(), BO->getOperatorLoc());
3926     }
3927   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3928     if (CE->getNumArgs() == 2) {
3929       auto Op = CE->getOperator();
3930       switch (Op) {
3931       case OO_Greater:
3932       case OO_GreaterEqual:
3933       case OO_Less:
3934       case OO_LessEqual:
3935         if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3936           return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3937                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3938                        CE->getOperatorLoc());
3939         if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
3940           return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3941                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3942                        CE->getOperatorLoc());
3943         break;
3944       default:
3945         break;
3946       }
3947     }
3948   }
3949   if (Dependent() || SemaRef.CurContext->isDependentContext())
3950     return false;
3951   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3952       << S->getSourceRange() << LCDecl;
3953   return true;
3954 }
3955 
3956 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3957   // RHS of canonical loop form increment can be:
3958   //   var + incr
3959   //   incr + var
3960   //   var - incr
3961   //
3962   RHS = RHS->IgnoreParenImpCasts();
3963   if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
3964     if (BO->isAdditiveOp()) {
3965       bool IsAdd = BO->getOpcode() == BO_Add;
3966       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3967         return SetStep(BO->getRHS(), !IsAdd);
3968       if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
3969         return SetStep(BO->getLHS(), false);
3970     }
3971   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3972     bool IsAdd = CE->getOperator() == OO_Plus;
3973     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3974       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3975         return SetStep(CE->getArg(1), !IsAdd);
3976       if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
3977         return SetStep(CE->getArg(0), false);
3978     }
3979   }
3980   if (Dependent() || SemaRef.CurContext->isDependentContext())
3981     return false;
3982   SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3983       << RHS->getSourceRange() << LCDecl;
3984   return true;
3985 }
3986 
3987 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3988   // Check incr-expr for canonical loop form and return true if it
3989   // does not conform.
3990   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3991   //   ++var
3992   //   var++
3993   //   --var
3994   //   var--
3995   //   var += incr
3996   //   var -= incr
3997   //   var = var + incr
3998   //   var = incr + var
3999   //   var = var - incr
4000   //
4001   if (!S) {
4002     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
4003     return true;
4004   }
4005   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4006     if (!ExprTemp->cleanupsHaveSideEffects())
4007       S = ExprTemp->getSubExpr();
4008 
4009   IncrementSrcRange = S->getSourceRange();
4010   S = S->IgnoreParens();
4011   if (auto *UO = dyn_cast<UnaryOperator>(S)) {
4012     if (UO->isIncrementDecrementOp() &&
4013         GetInitLCDecl(UO->getSubExpr()) == LCDecl)
4014       return SetStep(SemaRef
4015                          .ActOnIntegerConstant(UO->getLocStart(),
4016                                                (UO->isDecrementOp() ? -1 : 1))
4017                          .get(),
4018                      false);
4019   } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
4020     switch (BO->getOpcode()) {
4021     case BO_AddAssign:
4022     case BO_SubAssign:
4023       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
4024         return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4025       break;
4026     case BO_Assign:
4027       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
4028         return CheckIncRHS(BO->getRHS());
4029       break;
4030     default:
4031       break;
4032     }
4033   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4034     switch (CE->getOperator()) {
4035     case OO_PlusPlus:
4036     case OO_MinusMinus:
4037       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4038         return SetStep(SemaRef
4039                            .ActOnIntegerConstant(
4040                                CE->getLocStart(),
4041                                ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4042                            .get(),
4043                        false);
4044       break;
4045     case OO_PlusEqual:
4046     case OO_MinusEqual:
4047       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4048         return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4049       break;
4050     case OO_Equal:
4051       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4052         return CheckIncRHS(CE->getArg(1));
4053       break;
4054     default:
4055       break;
4056     }
4057   }
4058   if (Dependent() || SemaRef.CurContext->isDependentContext())
4059     return false;
4060   SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
4061       << S->getSourceRange() << LCDecl;
4062   return true;
4063 }
4064 
4065 static ExprResult
4066 tryBuildCapture(Sema &SemaRef, Expr *Capture,
4067                 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4068   if (SemaRef.CurContext->isDependentContext())
4069     return ExprResult(Capture);
4070   if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4071     return SemaRef.PerformImplicitConversion(
4072         Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4073         /*AllowExplicit=*/true);
4074   auto I = Captures.find(Capture);
4075   if (I != Captures.end())
4076     return buildCapture(SemaRef, Capture, I->second);
4077   DeclRefExpr *Ref = nullptr;
4078   ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4079   Captures[Capture] = Ref;
4080   return Res;
4081 }
4082 
4083 /// \brief Build the expression to calculate the number of iterations.
4084 Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4085     Scope *S, const bool LimitedType,
4086     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4087   ExprResult Diff;
4088   auto VarType = LCDecl->getType().getNonReferenceType();
4089   if (VarType->isIntegerType() || VarType->isPointerType() ||
4090       SemaRef.getLangOpts().CPlusPlus) {
4091     // Upper - Lower
4092     auto *UBExpr = TestIsLessOp ? UB : LB;
4093     auto *LBExpr = TestIsLessOp ? LB : UB;
4094     Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4095     Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
4096     if (!Upper || !Lower)
4097       return nullptr;
4098 
4099     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4100 
4101     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4102       // BuildBinOp already emitted error, this one is to point user to upper
4103       // and lower bound, and to tell what is passed to 'operator-'.
4104       SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4105           << Upper->getSourceRange() << Lower->getSourceRange();
4106       return nullptr;
4107     }
4108   }
4109 
4110   if (!Diff.isUsable())
4111     return nullptr;
4112 
4113   // Upper - Lower [- 1]
4114   if (TestIsStrictOp)
4115     Diff = SemaRef.BuildBinOp(
4116         S, DefaultLoc, BO_Sub, Diff.get(),
4117         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4118   if (!Diff.isUsable())
4119     return nullptr;
4120 
4121   // Upper - Lower [- 1] + Step
4122   auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4123   if (!NewStep.isUsable())
4124     return nullptr;
4125   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
4126   if (!Diff.isUsable())
4127     return nullptr;
4128 
4129   // Parentheses (for dumping/debugging purposes only).
4130   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4131   if (!Diff.isUsable())
4132     return nullptr;
4133 
4134   // (Upper - Lower [- 1] + Step) / Step
4135   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4136   if (!Diff.isUsable())
4137     return nullptr;
4138 
4139   // OpenMP runtime requires 32-bit or 64-bit loop variables.
4140   QualType Type = Diff.get()->getType();
4141   auto &C = SemaRef.Context;
4142   bool UseVarType = VarType->hasIntegerRepresentation() &&
4143                     C.getTypeSize(Type) > C.getTypeSize(VarType);
4144   if (!Type->isIntegerType() || UseVarType) {
4145     unsigned NewSize =
4146         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4147     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4148                                : Type->hasSignedIntegerRepresentation();
4149     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
4150     if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4151       Diff = SemaRef.PerformImplicitConversion(
4152           Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4153       if (!Diff.isUsable())
4154         return nullptr;
4155     }
4156   }
4157   if (LimitedType) {
4158     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4159     if (NewSize != C.getTypeSize(Type)) {
4160       if (NewSize < C.getTypeSize(Type)) {
4161         assert(NewSize == 64 && "incorrect loop var size");
4162         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4163             << InitSrcRange << ConditionSrcRange;
4164       }
4165       QualType NewType = C.getIntTypeForBitwidth(
4166           NewSize, Type->hasSignedIntegerRepresentation() ||
4167                        C.getTypeSize(Type) < NewSize);
4168       if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4169         Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4170                                                  Sema::AA_Converting, true);
4171         if (!Diff.isUsable())
4172           return nullptr;
4173       }
4174     }
4175   }
4176 
4177   return Diff.get();
4178 }
4179 
4180 Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4181     Scope *S, Expr *Cond,
4182     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4183   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4184   bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4185   SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4186 
4187   auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4188   auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4189   if (!NewLB.isUsable() || !NewUB.isUsable())
4190     return nullptr;
4191 
4192   auto CondExpr = SemaRef.BuildBinOp(
4193       S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4194                                   : (TestIsStrictOp ? BO_GT : BO_GE),
4195       NewLB.get(), NewUB.get());
4196   if (CondExpr.isUsable()) {
4197     if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4198                                                 SemaRef.Context.BoolTy))
4199       CondExpr = SemaRef.PerformImplicitConversion(
4200           CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4201           /*AllowExplicit=*/true);
4202   }
4203   SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4204   // Otherwise use original loop conditon and evaluate it in runtime.
4205   return CondExpr.isUsable() ? CondExpr.get() : Cond;
4206 }
4207 
4208 /// \brief Build reference expression to the counter be used for codegen.
4209 DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
4210     llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
4211   auto *VD = dyn_cast<VarDecl>(LCDecl);
4212   if (!VD) {
4213     VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4214     auto *Ref = buildDeclRefExpr(
4215         SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
4216     DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4217     // If the loop control decl is explicitly marked as private, do not mark it
4218     // as captured again.
4219     if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4220       Captures.insert(std::make_pair(LCRef, Ref));
4221     return Ref;
4222   }
4223   return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
4224                           DefaultLoc);
4225 }
4226 
4227 Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
4228   if (LCDecl && !LCDecl->isInvalidDecl()) {
4229     auto Type = LCDecl->getType().getNonReferenceType();
4230     auto *PrivateVar = buildVarDecl(
4231         SemaRef, DefaultLoc, Type, LCDecl->getName(),
4232         LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4233         isa<VarDecl>(LCDecl)
4234             ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4235             : nullptr);
4236     if (PrivateVar->isInvalidDecl())
4237       return nullptr;
4238     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4239   }
4240   return nullptr;
4241 }
4242 
4243 /// \brief Build initialization of the counter to be used for codegen.
4244 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4245 
4246 /// \brief Build step of the counter be used for codegen.
4247 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4248 
4249 /// \brief Iteration space of a single for loop.
4250 struct LoopIterationSpace final {
4251   /// \brief Condition of the loop.
4252   Expr *PreCond = nullptr;
4253   /// \brief This expression calculates the number of iterations in the loop.
4254   /// It is always possible to calculate it before starting the loop.
4255   Expr *NumIterations = nullptr;
4256   /// \brief The loop counter variable.
4257   Expr *CounterVar = nullptr;
4258   /// \brief Private loop counter variable.
4259   Expr *PrivateCounterVar = nullptr;
4260   /// \brief This is initializer for the initial value of #CounterVar.
4261   Expr *CounterInit = nullptr;
4262   /// \brief This is step for the #CounterVar used to generate its update:
4263   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4264   Expr *CounterStep = nullptr;
4265   /// \brief Should step be subtracted?
4266   bool Subtract = false;
4267   /// \brief Source range of the loop init.
4268   SourceRange InitSrcRange;
4269   /// \brief Source range of the loop condition.
4270   SourceRange CondSrcRange;
4271   /// \brief Source range of the loop increment.
4272   SourceRange IncSrcRange;
4273 };
4274 
4275 } // namespace
4276 
4277 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4278   assert(getLangOpts().OpenMP && "OpenMP is not active.");
4279   assert(Init && "Expected loop in canonical form.");
4280   unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4281   if (AssociatedLoops > 0 &&
4282       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4283     OpenMPIterationSpaceChecker ISC(*this, ForLoc);
4284     if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4285       if (auto *D = ISC.GetLoopDecl()) {
4286         auto *VD = dyn_cast<VarDecl>(D);
4287         if (!VD) {
4288           if (auto *Private = IsOpenMPCapturedDecl(D))
4289             VD = Private;
4290           else {
4291             auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4292                                      /*WithInit=*/false);
4293             VD = cast<VarDecl>(Ref->getDecl());
4294           }
4295         }
4296         DSAStack->addLoopControlVariable(D, VD);
4297       }
4298     }
4299     DSAStack->setAssociatedLoops(AssociatedLoops - 1);
4300   }
4301 }
4302 
4303 /// \brief Called on a for stmt to check and extract its iteration space
4304 /// for further processing (such as collapsing).
4305 static bool CheckOpenMPIterationSpace(
4306     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4307     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
4308     Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
4309     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4310     LoopIterationSpace &ResultIterSpace,
4311     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4312   // OpenMP [2.6, Canonical Loop Form]
4313   //   for (init-expr; test-expr; incr-expr) structured-block
4314   auto *For = dyn_cast_or_null<ForStmt>(S);
4315   if (!For) {
4316     SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
4317         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4318         << getOpenMPDirectiveName(DKind) << NestedLoopCount
4319         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4320     if (NestedLoopCount > 1) {
4321       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4322         SemaRef.Diag(DSA.getConstructLoc(),
4323                      diag::note_omp_collapse_ordered_expr)
4324             << 2 << CollapseLoopCountExpr->getSourceRange()
4325             << OrderedLoopCountExpr->getSourceRange();
4326       else if (CollapseLoopCountExpr)
4327         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4328                      diag::note_omp_collapse_ordered_expr)
4329             << 0 << CollapseLoopCountExpr->getSourceRange();
4330       else
4331         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4332                      diag::note_omp_collapse_ordered_expr)
4333             << 1 << OrderedLoopCountExpr->getSourceRange();
4334     }
4335     return true;
4336   }
4337   assert(For->getBody());
4338 
4339   OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4340 
4341   // Check init.
4342   auto Init = For->getInit();
4343   if (ISC.CheckInit(Init))
4344     return true;
4345 
4346   bool HasErrors = false;
4347 
4348   // Check loop variable's type.
4349   if (auto *LCDecl = ISC.GetLoopDecl()) {
4350     auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
4351 
4352     // OpenMP [2.6, Canonical Loop Form]
4353     // Var is one of the following:
4354     //   A variable of signed or unsigned integer type.
4355     //   For C++, a variable of a random access iterator type.
4356     //   For C, a variable of a pointer type.
4357     auto VarType = LCDecl->getType().getNonReferenceType();
4358     if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4359         !VarType->isPointerType() &&
4360         !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4361       SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4362           << SemaRef.getLangOpts().CPlusPlus;
4363       HasErrors = true;
4364     }
4365 
4366     // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4367     // a Construct
4368     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4369     // parallel for construct is (are) private.
4370     // The loop iteration variable in the associated for-loop of a simd
4371     // construct with just one associated for-loop is linear with a
4372     // constant-linear-step that is the increment of the associated for-loop.
4373     // Exclude loop var from the list of variables with implicitly defined data
4374     // sharing attributes.
4375     VarsWithImplicitDSA.erase(LCDecl);
4376 
4377     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4378     // in a Construct, C/C++].
4379     // The loop iteration variable in the associated for-loop of a simd
4380     // construct with just one associated for-loop may be listed in a linear
4381     // clause with a constant-linear-step that is the increment of the
4382     // associated for-loop.
4383     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4384     // parallel for construct may be listed in a private or lastprivate clause.
4385     DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4386     // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4387     // declared in the loop and it is predetermined as a private.
4388     auto PredeterminedCKind =
4389         isOpenMPSimdDirective(DKind)
4390             ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4391             : OMPC_private;
4392     if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4393           DVar.CKind != PredeterminedCKind) ||
4394          ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4395            isOpenMPDistributeDirective(DKind)) &&
4396           !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4397           DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4398         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4399       SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4400           << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4401           << getOpenMPClauseName(PredeterminedCKind);
4402       if (DVar.RefExpr == nullptr)
4403         DVar.CKind = PredeterminedCKind;
4404       ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4405       HasErrors = true;
4406     } else if (LoopDeclRefExpr != nullptr) {
4407       // Make the loop iteration variable private (for worksharing constructs),
4408       // linear (for simd directives with the only one associated loop) or
4409       // lastprivate (for simd directives with several collapsed or ordered
4410       // loops).
4411       if (DVar.CKind == OMPC_unknown)
4412         DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4413                           [](OpenMPDirectiveKind) -> bool { return true; },
4414                           /*FromParent=*/false);
4415       DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4416     }
4417 
4418     assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4419 
4420     // Check test-expr.
4421     HasErrors |= ISC.CheckCond(For->getCond());
4422 
4423     // Check incr-expr.
4424     HasErrors |= ISC.CheckInc(For->getInc());
4425   }
4426 
4427   if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
4428     return HasErrors;
4429 
4430   // Build the loop's iteration space representation.
4431   ResultIterSpace.PreCond =
4432       ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4433   ResultIterSpace.NumIterations = ISC.BuildNumIterations(
4434       DSA.getCurScope(),
4435       (isOpenMPWorksharingDirective(DKind) ||
4436        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4437       Captures);
4438   ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
4439   ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
4440   ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4441   ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4442   ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4443   ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4444   ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4445   ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4446 
4447   HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4448                 ResultIterSpace.NumIterations == nullptr ||
4449                 ResultIterSpace.CounterVar == nullptr ||
4450                 ResultIterSpace.PrivateCounterVar == nullptr ||
4451                 ResultIterSpace.CounterInit == nullptr ||
4452                 ResultIterSpace.CounterStep == nullptr);
4453 
4454   return HasErrors;
4455 }
4456 
4457 /// \brief Build 'VarRef = Start.
4458 static ExprResult
4459 BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4460                  ExprResult Start,
4461                  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4462   // Build 'VarRef = Start.
4463   auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4464   if (!NewStart.isUsable())
4465     return ExprError();
4466   if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4467                                    VarRef.get()->getType())) {
4468     NewStart = SemaRef.PerformImplicitConversion(
4469         NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4470         /*AllowExplicit=*/true);
4471     if (!NewStart.isUsable())
4472       return ExprError();
4473   }
4474 
4475   auto Init =
4476       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4477   return Init;
4478 }
4479 
4480 /// \brief Build 'VarRef = Start + Iter * Step'.
4481 static ExprResult
4482 BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4483                    ExprResult VarRef, ExprResult Start, ExprResult Iter,
4484                    ExprResult Step, bool Subtract,
4485                    llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
4486   // Add parentheses (for debugging purposes only).
4487   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4488   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4489       !Step.isUsable())
4490     return ExprError();
4491 
4492   ExprResult NewStep = Step;
4493   if (Captures)
4494     NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
4495   if (NewStep.isInvalid())
4496     return ExprError();
4497   ExprResult Update =
4498       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
4499   if (!Update.isUsable())
4500     return ExprError();
4501 
4502   // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4503   // 'VarRef = Start (+|-) Iter * Step'.
4504   ExprResult NewStart = Start;
4505   if (Captures)
4506     NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
4507   if (NewStart.isInvalid())
4508     return ExprError();
4509 
4510   // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4511   ExprResult SavedUpdate = Update;
4512   ExprResult UpdateVal;
4513   if (VarRef.get()->getType()->isOverloadableType() ||
4514       NewStart.get()->getType()->isOverloadableType() ||
4515       Update.get()->getType()->isOverloadableType()) {
4516     bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4517     SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4518     Update =
4519         SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4520     if (Update.isUsable()) {
4521       UpdateVal =
4522           SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4523                              VarRef.get(), SavedUpdate.get());
4524       if (UpdateVal.isUsable()) {
4525         Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4526                                             UpdateVal.get());
4527       }
4528     }
4529     SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4530   }
4531 
4532   // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4533   if (!Update.isUsable() || !UpdateVal.isUsable()) {
4534     Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4535                                 NewStart.get(), SavedUpdate.get());
4536     if (!Update.isUsable())
4537       return ExprError();
4538 
4539     if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4540                                      VarRef.get()->getType())) {
4541       Update = SemaRef.PerformImplicitConversion(
4542           Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4543       if (!Update.isUsable())
4544         return ExprError();
4545     }
4546 
4547     Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4548   }
4549   return Update;
4550 }
4551 
4552 /// \brief Convert integer expression \a E to make it have at least \a Bits
4553 /// bits.
4554 static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
4555   if (E == nullptr)
4556     return ExprError();
4557   auto &C = SemaRef.Context;
4558   QualType OldType = E->getType();
4559   unsigned HasBits = C.getTypeSize(OldType);
4560   if (HasBits >= Bits)
4561     return ExprResult(E);
4562   // OK to convert to signed, because new type has more bits than old.
4563   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4564   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4565                                            true);
4566 }
4567 
4568 /// \brief Check if the given expression \a E is a constant integer that fits
4569 /// into \a Bits bits.
4570 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4571   if (E == nullptr)
4572     return false;
4573   llvm::APSInt Result;
4574   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4575     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4576   return false;
4577 }
4578 
4579 /// Build preinits statement for the given declarations.
4580 static Stmt *buildPreInits(ASTContext &Context,
4581                            MutableArrayRef<Decl *> PreInits) {
4582   if (!PreInits.empty()) {
4583     return new (Context) DeclStmt(
4584         DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4585         SourceLocation(), SourceLocation());
4586   }
4587   return nullptr;
4588 }
4589 
4590 /// Build preinits statement for the given declarations.
4591 static Stmt *
4592 buildPreInits(ASTContext &Context,
4593               const llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4594   if (!Captures.empty()) {
4595     SmallVector<Decl *, 16> PreInits;
4596     for (auto &Pair : Captures)
4597       PreInits.push_back(Pair.second->getDecl());
4598     return buildPreInits(Context, PreInits);
4599   }
4600   return nullptr;
4601 }
4602 
4603 /// Build postupdate expression for the given list of postupdates expressions.
4604 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4605   Expr *PostUpdate = nullptr;
4606   if (!PostUpdates.empty()) {
4607     for (auto *E : PostUpdates) {
4608       Expr *ConvE = S.BuildCStyleCastExpr(
4609                          E->getExprLoc(),
4610                          S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4611                          E->getExprLoc(), E)
4612                         .get();
4613       PostUpdate = PostUpdate
4614                        ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4615                                               PostUpdate, ConvE)
4616                              .get()
4617                        : ConvE;
4618     }
4619   }
4620   return PostUpdate;
4621 }
4622 
4623 /// \brief Called on a for stmt to check itself and nested loops (if any).
4624 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4625 /// number of collapsed loops otherwise.
4626 static unsigned
4627 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4628                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4629                 DSAStackTy &DSA,
4630                 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4631                 OMPLoopDirective::HelperExprs &Built) {
4632   unsigned NestedLoopCount = 1;
4633   if (CollapseLoopCountExpr) {
4634     // Found 'collapse' clause - calculate collapse number.
4635     llvm::APSInt Result;
4636     if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
4637       NestedLoopCount = Result.getLimitedValue();
4638   }
4639   if (OrderedLoopCountExpr) {
4640     // Found 'ordered' clause - calculate collapse number.
4641     llvm::APSInt Result;
4642     if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4643       if (Result.getLimitedValue() < NestedLoopCount) {
4644         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4645                      diag::err_omp_wrong_ordered_loop_count)
4646             << OrderedLoopCountExpr->getSourceRange();
4647         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4648                      diag::note_collapse_loop_count)
4649             << CollapseLoopCountExpr->getSourceRange();
4650       }
4651       NestedLoopCount = Result.getLimitedValue();
4652     }
4653   }
4654   // This is helper routine for loop directives (e.g., 'for', 'simd',
4655   // 'for simd', etc.).
4656   llvm::MapVector<Expr *, DeclRefExpr *> Captures;
4657   SmallVector<LoopIterationSpace, 4> IterSpaces;
4658   IterSpaces.resize(NestedLoopCount);
4659   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
4660   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
4661     if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
4662                                   NestedLoopCount, CollapseLoopCountExpr,
4663                                   OrderedLoopCountExpr, VarsWithImplicitDSA,
4664                                   IterSpaces[Cnt], Captures))
4665       return 0;
4666     // Move on to the next nested for loop, or to the loop body.
4667     // OpenMP [2.8.1, simd construct, Restrictions]
4668     // All loops associated with the construct must be perfectly nested; that
4669     // is, there must be no intervening code nor any OpenMP directive between
4670     // any two loops.
4671     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
4672   }
4673 
4674   Built.clear(/* size */ NestedLoopCount);
4675 
4676   if (SemaRef.CurContext->isDependentContext())
4677     return NestedLoopCount;
4678 
4679   // An example of what is generated for the following code:
4680   //
4681   //   #pragma omp simd collapse(2) ordered(2)
4682   //   for (i = 0; i < NI; ++i)
4683   //     for (k = 0; k < NK; ++k)
4684   //       for (j = J0; j < NJ; j+=2) {
4685   //         <loop body>
4686   //       }
4687   //
4688   // We generate the code below.
4689   // Note: the loop body may be outlined in CodeGen.
4690   // Note: some counters may be C++ classes, operator- is used to find number of
4691   // iterations and operator+= to calculate counter value.
4692   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4693   // or i64 is currently supported).
4694   //
4695   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4696   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4697   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4698   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4699   //     // similar updates for vars in clauses (e.g. 'linear')
4700   //     <loop body (using local i and j)>
4701   //   }
4702   //   i = NI; // assign final values of counters
4703   //   j = NJ;
4704   //
4705 
4706   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4707   // the iteration counts of the collapsed for loops.
4708   // Precondition tests if there is at least one iteration (all conditions are
4709   // true).
4710   auto PreCond = ExprResult(IterSpaces[0].PreCond);
4711   auto N0 = IterSpaces[0].NumIterations;
4712   ExprResult LastIteration32 = WidenIterationCount(
4713       32 /* Bits */, SemaRef
4714                          .PerformImplicitConversion(
4715                              N0->IgnoreImpCasts(), N0->getType(),
4716                              Sema::AA_Converting, /*AllowExplicit=*/true)
4717                          .get(),
4718       SemaRef);
4719   ExprResult LastIteration64 = WidenIterationCount(
4720       64 /* Bits */, SemaRef
4721                          .PerformImplicitConversion(
4722                              N0->IgnoreImpCasts(), N0->getType(),
4723                              Sema::AA_Converting, /*AllowExplicit=*/true)
4724                          .get(),
4725       SemaRef);
4726 
4727   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4728     return NestedLoopCount;
4729 
4730   auto &C = SemaRef.Context;
4731   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4732 
4733   Scope *CurScope = DSA.getCurScope();
4734   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
4735     if (PreCond.isUsable()) {
4736       PreCond =
4737           SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
4738                              PreCond.get(), IterSpaces[Cnt].PreCond);
4739     }
4740     auto N = IterSpaces[Cnt].NumIterations;
4741     SourceLocation Loc = N->getExprLoc();
4742     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4743     if (LastIteration32.isUsable())
4744       LastIteration32 = SemaRef.BuildBinOp(
4745           CurScope, Loc, BO_Mul, LastIteration32.get(),
4746           SemaRef
4747               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4748                                          Sema::AA_Converting,
4749                                          /*AllowExplicit=*/true)
4750               .get());
4751     if (LastIteration64.isUsable())
4752       LastIteration64 = SemaRef.BuildBinOp(
4753           CurScope, Loc, BO_Mul, LastIteration64.get(),
4754           SemaRef
4755               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4756                                          Sema::AA_Converting,
4757                                          /*AllowExplicit=*/true)
4758               .get());
4759   }
4760 
4761   // Choose either the 32-bit or 64-bit version.
4762   ExprResult LastIteration = LastIteration64;
4763   if (LastIteration32.isUsable() &&
4764       C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4765       (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4766        FitsInto(
4767            32 /* Bits */,
4768            LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4769            LastIteration64.get(), SemaRef)))
4770     LastIteration = LastIteration32;
4771   QualType VType = LastIteration.get()->getType();
4772   QualType RealVType = VType;
4773   QualType StrideVType = VType;
4774   if (isOpenMPTaskLoopDirective(DKind)) {
4775     VType =
4776         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4777     StrideVType =
4778         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4779   }
4780 
4781   if (!LastIteration.isUsable())
4782     return 0;
4783 
4784   // Save the number of iterations.
4785   ExprResult NumIterations = LastIteration;
4786   {
4787     LastIteration = SemaRef.BuildBinOp(
4788         CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
4789         LastIteration.get(),
4790         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4791     if (!LastIteration.isUsable())
4792       return 0;
4793   }
4794 
4795   // Calculate the last iteration number beforehand instead of doing this on
4796   // each iteration. Do not do this if the number of iterations may be kfold-ed.
4797   llvm::APSInt Result;
4798   bool IsConstant =
4799       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4800   ExprResult CalcLastIteration;
4801   if (!IsConstant) {
4802     ExprResult SaveRef =
4803         tryBuildCapture(SemaRef, LastIteration.get(), Captures);
4804     LastIteration = SaveRef;
4805 
4806     // Prepare SaveRef + 1.
4807     NumIterations = SemaRef.BuildBinOp(
4808         CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
4809         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4810     if (!NumIterations.isUsable())
4811       return 0;
4812   }
4813 
4814   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4815 
4816   // Build variables passed into runtime, necessary for worksharing directives.
4817   ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
4818   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4819       isOpenMPDistributeDirective(DKind)) {
4820     // Lower bound variable, initialized with zero.
4821     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4822     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
4823     SemaRef.AddInitializerToDecl(LBDecl,
4824                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4825                                  /*DirectInit*/ false);
4826 
4827     // Upper bound variable, initialized with last iteration number.
4828     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4829     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
4830     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4831                                  /*DirectInit*/ false);
4832 
4833     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4834     // This will be used to implement clause 'lastprivate'.
4835     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
4836     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4837     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
4838     SemaRef.AddInitializerToDecl(ILDecl,
4839                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4840                                  /*DirectInit*/ false);
4841 
4842     // Stride variable returned by runtime (we initialize it to 1 by default).
4843     VarDecl *STDecl =
4844         buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4845     ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
4846     SemaRef.AddInitializerToDecl(STDecl,
4847                                  SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4848                                  /*DirectInit*/ false);
4849 
4850     // Build expression: UB = min(UB, LastIteration)
4851     // It is necessary for CodeGen of directives with static scheduling.
4852     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4853                                                 UB.get(), LastIteration.get());
4854     ExprResult CondOp = SemaRef.ActOnConditionalOp(
4855         InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4856     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4857                              CondOp.get());
4858     EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4859 
4860     // If we have a combined directive that combines 'distribute', 'for' or
4861     // 'simd' we need to be able to access the bounds of the schedule of the
4862     // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
4863     // by scheduling 'distribute' have to be passed to the schedule of 'for'.
4864     if (isOpenMPLoopBoundSharingDirective(DKind)) {
4865 
4866       // Lower bound variable, initialized with zero.
4867       VarDecl *CombLBDecl =
4868           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
4869       CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
4870       SemaRef.AddInitializerToDecl(
4871           CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4872           /*DirectInit*/ false);
4873 
4874       // Upper bound variable, initialized with last iteration number.
4875       VarDecl *CombUBDecl =
4876           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
4877       CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
4878       SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
4879                                    /*DirectInit*/ false);
4880 
4881       ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
4882           CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
4883       ExprResult CombCondOp =
4884           SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
4885                                      LastIteration.get(), CombUB.get());
4886       CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
4887                                    CombCondOp.get());
4888       CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
4889 
4890       auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
4891       // We expect to have at least 2 more parameters than the 'parallel'
4892       // directive does - the lower and upper bounds of the previous schedule.
4893       assert(CD->getNumParams() >= 4 &&
4894              "Unexpected number of parameters in loop combined directive");
4895 
4896       // Set the proper type for the bounds given what we learned from the
4897       // enclosed loops.
4898       auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
4899       auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
4900 
4901       // Previous lower and upper bounds are obtained from the region
4902       // parameters.
4903       PrevLB =
4904           buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
4905       PrevUB =
4906           buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
4907     }
4908   }
4909 
4910   // Build the iteration variable and its initialization before loop.
4911   ExprResult IV;
4912   ExprResult Init, CombInit;
4913   {
4914     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4915     IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
4916     Expr *RHS =
4917         (isOpenMPWorksharingDirective(DKind) ||
4918          isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4919             ? LB.get()
4920             : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4921     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4922     Init = SemaRef.ActOnFinishFullExpr(Init.get());
4923 
4924     if (isOpenMPLoopBoundSharingDirective(DKind)) {
4925       Expr *CombRHS =
4926           (isOpenMPWorksharingDirective(DKind) ||
4927            isOpenMPTaskLoopDirective(DKind) ||
4928            isOpenMPDistributeDirective(DKind))
4929               ? CombLB.get()
4930               : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4931       CombInit =
4932           SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
4933       CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
4934     }
4935   }
4936 
4937   // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
4938   SourceLocation CondLoc = AStmt->getLocStart();
4939   ExprResult Cond =
4940       (isOpenMPWorksharingDirective(DKind) ||
4941        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4942           ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4943           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4944                                NumIterations.get());
4945   ExprResult CombCond;
4946   if (isOpenMPLoopBoundSharingDirective(DKind)) {
4947     CombCond =
4948         SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
4949   }
4950   // Loop increment (IV = IV + 1)
4951   SourceLocation IncLoc = AStmt->getLocStart();
4952   ExprResult Inc =
4953       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4954                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4955   if (!Inc.isUsable())
4956     return 0;
4957   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
4958   Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4959   if (!Inc.isUsable())
4960     return 0;
4961 
4962   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4963   // Used for directives with static scheduling.
4964   // In combined construct, add combined version that use CombLB and CombUB
4965   // base variables for the update
4966   ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
4967   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4968       isOpenMPDistributeDirective(DKind)) {
4969     // LB + ST
4970     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4971     if (!NextLB.isUsable())
4972       return 0;
4973     // LB = LB + ST
4974     NextLB =
4975         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4976     NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4977     if (!NextLB.isUsable())
4978       return 0;
4979     // UB + ST
4980     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4981     if (!NextUB.isUsable())
4982       return 0;
4983     // UB = UB + ST
4984     NextUB =
4985         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4986     NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4987     if (!NextUB.isUsable())
4988       return 0;
4989     if (isOpenMPLoopBoundSharingDirective(DKind)) {
4990       CombNextLB =
4991           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
4992       if (!NextLB.isUsable())
4993         return 0;
4994       // LB = LB + ST
4995       CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
4996                                       CombNextLB.get());
4997       CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
4998       if (!CombNextLB.isUsable())
4999         return 0;
5000       // UB + ST
5001       CombNextUB =
5002           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5003       if (!CombNextUB.isUsable())
5004         return 0;
5005       // UB = UB + ST
5006       CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5007                                       CombNextUB.get());
5008       CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
5009       if (!CombNextUB.isUsable())
5010         return 0;
5011     }
5012   }
5013 
5014   // Create increment expression for distribute loop when combined in a same
5015   // directive with for as IV = IV + ST; ensure upper bound expression based
5016   // on PrevUB instead of NumIterations - used to implement 'for' when found
5017   // in combination with 'distribute', like in 'distribute parallel for'
5018   SourceLocation DistIncLoc = AStmt->getLocStart();
5019   ExprResult DistCond, DistInc, PrevEUB;
5020   if (isOpenMPLoopBoundSharingDirective(DKind)) {
5021     DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5022     assert(DistCond.isUsable() && "distribute cond expr was not built");
5023 
5024     DistInc =
5025         SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5026     assert(DistInc.isUsable() && "distribute inc expr was not built");
5027     DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5028                                  DistInc.get());
5029     DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
5030     assert(DistInc.isUsable() && "distribute inc expr was not built");
5031 
5032     // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5033     // construct
5034     SourceLocation DistEUBLoc = AStmt->getLocStart();
5035     ExprResult IsUBGreater =
5036         SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5037     ExprResult CondOp = SemaRef.ActOnConditionalOp(
5038         DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5039     PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5040                                  CondOp.get());
5041     PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
5042   }
5043 
5044   // Build updates and final values of the loop counters.
5045   bool HasErrors = false;
5046   Built.Counters.resize(NestedLoopCount);
5047   Built.Inits.resize(NestedLoopCount);
5048   Built.Updates.resize(NestedLoopCount);
5049   Built.Finals.resize(NestedLoopCount);
5050   SmallVector<Expr *, 4> LoopMultipliers;
5051   {
5052     ExprResult Div;
5053     // Go from inner nested loop to outer.
5054     for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5055       LoopIterationSpace &IS = IterSpaces[Cnt];
5056       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5057       // Build: Iter = (IV / Div) % IS.NumIters
5058       // where Div is product of previous iterations' IS.NumIters.
5059       ExprResult Iter;
5060       if (Div.isUsable()) {
5061         Iter =
5062             SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5063       } else {
5064         Iter = IV;
5065         assert((Cnt == (int)NestedLoopCount - 1) &&
5066                "unusable div expected on first iteration only");
5067       }
5068 
5069       if (Cnt != 0 && Iter.isUsable())
5070         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5071                                   IS.NumIterations);
5072       if (!Iter.isUsable()) {
5073         HasErrors = true;
5074         break;
5075       }
5076 
5077       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
5078       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5079       auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5080                                           IS.CounterVar->getExprLoc(),
5081                                           /*RefersToCapture=*/true);
5082       ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
5083                                          IS.CounterInit, Captures);
5084       if (!Init.isUsable()) {
5085         HasErrors = true;
5086         break;
5087       }
5088       ExprResult Update = BuildCounterUpdate(
5089           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5090           IS.CounterStep, IS.Subtract, &Captures);
5091       if (!Update.isUsable()) {
5092         HasErrors = true;
5093         break;
5094       }
5095 
5096       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5097       ExprResult Final = BuildCounterUpdate(
5098           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
5099           IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
5100       if (!Final.isUsable()) {
5101         HasErrors = true;
5102         break;
5103       }
5104 
5105       // Build Div for the next iteration: Div <- Div * IS.NumIters
5106       if (Cnt != 0) {
5107         if (Div.isUnset())
5108           Div = IS.NumIterations;
5109         else
5110           Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5111                                    IS.NumIterations);
5112 
5113         // Add parentheses (for debugging purposes only).
5114         if (Div.isUsable())
5115           Div = tryBuildCapture(SemaRef, Div.get(), Captures);
5116         if (!Div.isUsable()) {
5117           HasErrors = true;
5118           break;
5119         }
5120         LoopMultipliers.push_back(Div.get());
5121       }
5122       if (!Update.isUsable() || !Final.isUsable()) {
5123         HasErrors = true;
5124         break;
5125       }
5126       // Save results
5127       Built.Counters[Cnt] = IS.CounterVar;
5128       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
5129       Built.Inits[Cnt] = Init.get();
5130       Built.Updates[Cnt] = Update.get();
5131       Built.Finals[Cnt] = Final.get();
5132     }
5133   }
5134 
5135   if (HasErrors)
5136     return 0;
5137 
5138   // Save results
5139   Built.IterationVarRef = IV.get();
5140   Built.LastIteration = LastIteration.get();
5141   Built.NumIterations = NumIterations.get();
5142   Built.CalcLastIteration =
5143       SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
5144   Built.PreCond = PreCond.get();
5145   Built.PreInits = buildPreInits(C, Captures);
5146   Built.Cond = Cond.get();
5147   Built.Init = Init.get();
5148   Built.Inc = Inc.get();
5149   Built.LB = LB.get();
5150   Built.UB = UB.get();
5151   Built.IL = IL.get();
5152   Built.ST = ST.get();
5153   Built.EUB = EUB.get();
5154   Built.NLB = NextLB.get();
5155   Built.NUB = NextUB.get();
5156   Built.PrevLB = PrevLB.get();
5157   Built.PrevUB = PrevUB.get();
5158   Built.DistInc = DistInc.get();
5159   Built.PrevEUB = PrevEUB.get();
5160   Built.DistCombinedFields.LB = CombLB.get();
5161   Built.DistCombinedFields.UB = CombUB.get();
5162   Built.DistCombinedFields.EUB = CombEUB.get();
5163   Built.DistCombinedFields.Init = CombInit.get();
5164   Built.DistCombinedFields.Cond = CombCond.get();
5165   Built.DistCombinedFields.NLB = CombNextLB.get();
5166   Built.DistCombinedFields.NUB = CombNextUB.get();
5167 
5168   Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5169   // Fill data for doacross depend clauses.
5170   for (auto Pair : DSA.getDoacrossDependClauses()) {
5171     if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5172       Pair.first->setCounterValue(CounterVal);
5173     else {
5174       if (NestedLoopCount != Pair.second.size() ||
5175           NestedLoopCount != LoopMultipliers.size() + 1) {
5176         // Erroneous case - clause has some problems.
5177         Pair.first->setCounterValue(CounterVal);
5178         continue;
5179       }
5180       assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5181       auto I = Pair.second.rbegin();
5182       auto IS = IterSpaces.rbegin();
5183       auto ILM = LoopMultipliers.rbegin();
5184       Expr *UpCounterVal = CounterVal;
5185       Expr *Multiplier = nullptr;
5186       for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5187         if (I->first) {
5188           assert(IS->CounterStep);
5189           Expr *NormalizedOffset =
5190               SemaRef
5191                   .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5192                               I->first, IS->CounterStep)
5193                   .get();
5194           if (Multiplier) {
5195             NormalizedOffset =
5196                 SemaRef
5197                     .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5198                                 NormalizedOffset, Multiplier)
5199                     .get();
5200           }
5201           assert(I->second == OO_Plus || I->second == OO_Minus);
5202           BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5203           UpCounterVal = SemaRef
5204                              .BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5205                                          UpCounterVal, NormalizedOffset)
5206                              .get();
5207         }
5208         Multiplier = *ILM;
5209         ++I;
5210         ++IS;
5211         ++ILM;
5212       }
5213       Pair.first->setCounterValue(UpCounterVal);
5214     }
5215   }
5216 
5217   return NestedLoopCount;
5218 }
5219 
5220 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
5221   auto CollapseClauses =
5222       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5223   if (CollapseClauses.begin() != CollapseClauses.end())
5224     return (*CollapseClauses.begin())->getNumForLoops();
5225   return nullptr;
5226 }
5227 
5228 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
5229   auto OrderedClauses =
5230       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5231   if (OrderedClauses.begin() != OrderedClauses.end())
5232     return (*OrderedClauses.begin())->getNumForLoops();
5233   return nullptr;
5234 }
5235 
5236 static bool checkSimdlenSafelenSpecified(Sema &S,
5237                                          const ArrayRef<OMPClause *> Clauses) {
5238   OMPSafelenClause *Safelen = nullptr;
5239   OMPSimdlenClause *Simdlen = nullptr;
5240 
5241   for (auto *Clause : Clauses) {
5242     if (Clause->getClauseKind() == OMPC_safelen)
5243       Safelen = cast<OMPSafelenClause>(Clause);
5244     else if (Clause->getClauseKind() == OMPC_simdlen)
5245       Simdlen = cast<OMPSimdlenClause>(Clause);
5246     if (Safelen && Simdlen)
5247       break;
5248   }
5249 
5250   if (Simdlen && Safelen) {
5251     llvm::APSInt SimdlenRes, SafelenRes;
5252     auto SimdlenLength = Simdlen->getSimdlen();
5253     auto SafelenLength = Safelen->getSafelen();
5254     if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5255         SimdlenLength->isInstantiationDependent() ||
5256         SimdlenLength->containsUnexpandedParameterPack())
5257       return false;
5258     if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5259         SafelenLength->isInstantiationDependent() ||
5260         SafelenLength->containsUnexpandedParameterPack())
5261       return false;
5262     SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5263     SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5264     // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5265     // If both simdlen and safelen clauses are specified, the value of the
5266     // simdlen parameter must be less than or equal to the value of the safelen
5267     // parameter.
5268     if (SimdlenRes > SafelenRes) {
5269       S.Diag(SimdlenLength->getExprLoc(),
5270              diag::err_omp_wrong_simdlen_safelen_values)
5271           << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5272       return true;
5273     }
5274   }
5275   return false;
5276 }
5277 
5278 StmtResult Sema::ActOnOpenMPSimdDirective(
5279     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5280     SourceLocation EndLoc,
5281     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5282   if (!AStmt)
5283     return StmtError();
5284 
5285   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5286   OMPLoopDirective::HelperExprs B;
5287   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5288   // define the nested loops number.
5289   unsigned NestedLoopCount = CheckOpenMPLoop(
5290       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5291       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5292   if (NestedLoopCount == 0)
5293     return StmtError();
5294 
5295   assert((CurContext->isDependentContext() || B.builtAll()) &&
5296          "omp simd loop exprs were not built");
5297 
5298   if (!CurContext->isDependentContext()) {
5299     // Finalize the clauses that need pre-built expressions for CodeGen.
5300     for (auto C : Clauses) {
5301       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5302         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5303                                      B.NumIterations, *this, CurScope,
5304                                      DSAStack))
5305           return StmtError();
5306     }
5307   }
5308 
5309   if (checkSimdlenSafelenSpecified(*this, Clauses))
5310     return StmtError();
5311 
5312   setFunctionHasBranchProtectedScope();
5313   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5314                                   Clauses, AStmt, B);
5315 }
5316 
5317 StmtResult Sema::ActOnOpenMPForDirective(
5318     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5319     SourceLocation EndLoc,
5320     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5321   if (!AStmt)
5322     return StmtError();
5323 
5324   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5325   OMPLoopDirective::HelperExprs B;
5326   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5327   // define the nested loops number.
5328   unsigned NestedLoopCount = CheckOpenMPLoop(
5329       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5330       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5331   if (NestedLoopCount == 0)
5332     return StmtError();
5333 
5334   assert((CurContext->isDependentContext() || B.builtAll()) &&
5335          "omp for loop exprs were not built");
5336 
5337   if (!CurContext->isDependentContext()) {
5338     // Finalize the clauses that need pre-built expressions for CodeGen.
5339     for (auto C : Clauses) {
5340       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5341         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5342                                      B.NumIterations, *this, CurScope,
5343                                      DSAStack))
5344           return StmtError();
5345     }
5346   }
5347 
5348   setFunctionHasBranchProtectedScope();
5349   return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5350                                  Clauses, AStmt, B, DSAStack->isCancelRegion());
5351 }
5352 
5353 StmtResult Sema::ActOnOpenMPForSimdDirective(
5354     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5355     SourceLocation EndLoc,
5356     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5357   if (!AStmt)
5358     return StmtError();
5359 
5360   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5361   OMPLoopDirective::HelperExprs B;
5362   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5363   // define the nested loops number.
5364   unsigned NestedLoopCount =
5365       CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5366                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5367                       VarsWithImplicitDSA, B);
5368   if (NestedLoopCount == 0)
5369     return StmtError();
5370 
5371   assert((CurContext->isDependentContext() || B.builtAll()) &&
5372          "omp for simd loop exprs were not built");
5373 
5374   if (!CurContext->isDependentContext()) {
5375     // Finalize the clauses that need pre-built expressions for CodeGen.
5376     for (auto C : Clauses) {
5377       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5378         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5379                                      B.NumIterations, *this, CurScope,
5380                                      DSAStack))
5381           return StmtError();
5382     }
5383   }
5384 
5385   if (checkSimdlenSafelenSpecified(*this, Clauses))
5386     return StmtError();
5387 
5388   setFunctionHasBranchProtectedScope();
5389   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5390                                      Clauses, AStmt, B);
5391 }
5392 
5393 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5394                                               Stmt *AStmt,
5395                                               SourceLocation StartLoc,
5396                                               SourceLocation EndLoc) {
5397   if (!AStmt)
5398     return StmtError();
5399 
5400   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5401   auto BaseStmt = AStmt;
5402   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5403     BaseStmt = CS->getCapturedStmt();
5404   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5405     auto S = C->children();
5406     if (S.begin() == S.end())
5407       return StmtError();
5408     // All associated statements must be '#pragma omp section' except for
5409     // the first one.
5410     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5411       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5412         if (SectionStmt)
5413           Diag(SectionStmt->getLocStart(),
5414                diag::err_omp_sections_substmt_not_section);
5415         return StmtError();
5416       }
5417       cast<OMPSectionDirective>(SectionStmt)
5418           ->setHasCancel(DSAStack->isCancelRegion());
5419     }
5420   } else {
5421     Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5422     return StmtError();
5423   }
5424 
5425   setFunctionHasBranchProtectedScope();
5426 
5427   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5428                                       DSAStack->isCancelRegion());
5429 }
5430 
5431 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5432                                              SourceLocation StartLoc,
5433                                              SourceLocation EndLoc) {
5434   if (!AStmt)
5435     return StmtError();
5436 
5437   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5438 
5439   setFunctionHasBranchProtectedScope();
5440   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
5441 
5442   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5443                                      DSAStack->isCancelRegion());
5444 }
5445 
5446 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5447                                             Stmt *AStmt,
5448                                             SourceLocation StartLoc,
5449                                             SourceLocation EndLoc) {
5450   if (!AStmt)
5451     return StmtError();
5452 
5453   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5454 
5455   setFunctionHasBranchProtectedScope();
5456 
5457   // OpenMP [2.7.3, single Construct, Restrictions]
5458   // The copyprivate clause must not be used with the nowait clause.
5459   OMPClause *Nowait = nullptr;
5460   OMPClause *Copyprivate = nullptr;
5461   for (auto *Clause : Clauses) {
5462     if (Clause->getClauseKind() == OMPC_nowait)
5463       Nowait = Clause;
5464     else if (Clause->getClauseKind() == OMPC_copyprivate)
5465       Copyprivate = Clause;
5466     if (Copyprivate && Nowait) {
5467       Diag(Copyprivate->getLocStart(),
5468            diag::err_omp_single_copyprivate_with_nowait);
5469       Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5470       return StmtError();
5471     }
5472   }
5473 
5474   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5475 }
5476 
5477 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5478                                             SourceLocation StartLoc,
5479                                             SourceLocation EndLoc) {
5480   if (!AStmt)
5481     return StmtError();
5482 
5483   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5484 
5485   setFunctionHasBranchProtectedScope();
5486 
5487   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5488 }
5489 
5490 StmtResult Sema::ActOnOpenMPCriticalDirective(
5491     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5492     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
5493   if (!AStmt)
5494     return StmtError();
5495 
5496   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5497 
5498   bool ErrorFound = false;
5499   llvm::APSInt Hint;
5500   SourceLocation HintLoc;
5501   bool DependentHint = false;
5502   for (auto *C : Clauses) {
5503     if (C->getClauseKind() == OMPC_hint) {
5504       if (!DirName.getName()) {
5505         Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5506         ErrorFound = true;
5507       }
5508       Expr *E = cast<OMPHintClause>(C)->getHint();
5509       if (E->isTypeDependent() || E->isValueDependent() ||
5510           E->isInstantiationDependent())
5511         DependentHint = true;
5512       else {
5513         Hint = E->EvaluateKnownConstInt(Context);
5514         HintLoc = C->getLocStart();
5515       }
5516     }
5517   }
5518   if (ErrorFound)
5519     return StmtError();
5520   auto Pair = DSAStack->getCriticalWithHint(DirName);
5521   if (Pair.first && DirName.getName() && !DependentHint) {
5522     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5523       Diag(StartLoc, diag::err_omp_critical_with_hint);
5524       if (HintLoc.isValid()) {
5525         Diag(HintLoc, diag::note_omp_critical_hint_here)
5526             << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5527       } else
5528         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5529       if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5530         Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5531             << 1
5532             << C->getHint()->EvaluateKnownConstInt(Context).toString(
5533                    /*Radix=*/10, /*Signed=*/false);
5534       } else
5535         Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5536     }
5537   }
5538 
5539   setFunctionHasBranchProtectedScope();
5540 
5541   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5542                                            Clauses, AStmt);
5543   if (!Pair.first && DirName.getName() && !DependentHint)
5544     DSAStack->addCriticalWithHint(Dir, Hint);
5545   return Dir;
5546 }
5547 
5548 StmtResult Sema::ActOnOpenMPParallelForDirective(
5549     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5550     SourceLocation EndLoc,
5551     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5552   if (!AStmt)
5553     return StmtError();
5554 
5555   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5556   // 1.2.2 OpenMP Language Terminology
5557   // Structured block - An executable statement with a single entry at the
5558   // top and a single exit at the bottom.
5559   // The point of exit cannot be a branch out of the structured block.
5560   // longjmp() and throw() must not violate the entry/exit criteria.
5561   CS->getCapturedDecl()->setNothrow();
5562 
5563   OMPLoopDirective::HelperExprs B;
5564   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5565   // define the nested loops number.
5566   unsigned NestedLoopCount =
5567       CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5568                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5569                       VarsWithImplicitDSA, B);
5570   if (NestedLoopCount == 0)
5571     return StmtError();
5572 
5573   assert((CurContext->isDependentContext() || B.builtAll()) &&
5574          "omp parallel for loop exprs were not built");
5575 
5576   if (!CurContext->isDependentContext()) {
5577     // Finalize the clauses that need pre-built expressions for CodeGen.
5578     for (auto C : Clauses) {
5579       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5580         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5581                                      B.NumIterations, *this, CurScope,
5582                                      DSAStack))
5583           return StmtError();
5584     }
5585   }
5586 
5587   setFunctionHasBranchProtectedScope();
5588   return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
5589                                          NestedLoopCount, Clauses, AStmt, B,
5590                                          DSAStack->isCancelRegion());
5591 }
5592 
5593 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5594     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5595     SourceLocation EndLoc,
5596     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5597   if (!AStmt)
5598     return StmtError();
5599 
5600   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5601   // 1.2.2 OpenMP Language Terminology
5602   // Structured block - An executable statement with a single entry at the
5603   // top and a single exit at the bottom.
5604   // The point of exit cannot be a branch out of the structured block.
5605   // longjmp() and throw() must not violate the entry/exit criteria.
5606   CS->getCapturedDecl()->setNothrow();
5607 
5608   OMPLoopDirective::HelperExprs B;
5609   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5610   // define the nested loops number.
5611   unsigned NestedLoopCount =
5612       CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5613                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5614                       VarsWithImplicitDSA, B);
5615   if (NestedLoopCount == 0)
5616     return StmtError();
5617 
5618   if (!CurContext->isDependentContext()) {
5619     // Finalize the clauses that need pre-built expressions for CodeGen.
5620     for (auto C : Clauses) {
5621       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5622         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5623                                      B.NumIterations, *this, CurScope,
5624                                      DSAStack))
5625           return StmtError();
5626     }
5627   }
5628 
5629   if (checkSimdlenSafelenSpecified(*this, Clauses))
5630     return StmtError();
5631 
5632   setFunctionHasBranchProtectedScope();
5633   return OMPParallelForSimdDirective::Create(
5634       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
5635 }
5636 
5637 StmtResult
5638 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5639                                            Stmt *AStmt, SourceLocation StartLoc,
5640                                            SourceLocation EndLoc) {
5641   if (!AStmt)
5642     return StmtError();
5643 
5644   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5645   auto BaseStmt = AStmt;
5646   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5647     BaseStmt = CS->getCapturedStmt();
5648   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5649     auto S = C->children();
5650     if (S.begin() == S.end())
5651       return StmtError();
5652     // All associated statements must be '#pragma omp section' except for
5653     // the first one.
5654     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5655       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5656         if (SectionStmt)
5657           Diag(SectionStmt->getLocStart(),
5658                diag::err_omp_parallel_sections_substmt_not_section);
5659         return StmtError();
5660       }
5661       cast<OMPSectionDirective>(SectionStmt)
5662           ->setHasCancel(DSAStack->isCancelRegion());
5663     }
5664   } else {
5665     Diag(AStmt->getLocStart(),
5666          diag::err_omp_parallel_sections_not_compound_stmt);
5667     return StmtError();
5668   }
5669 
5670   setFunctionHasBranchProtectedScope();
5671 
5672   return OMPParallelSectionsDirective::Create(
5673       Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
5674 }
5675 
5676 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5677                                           Stmt *AStmt, SourceLocation StartLoc,
5678                                           SourceLocation EndLoc) {
5679   if (!AStmt)
5680     return StmtError();
5681 
5682   auto *CS = cast<CapturedStmt>(AStmt);
5683   // 1.2.2 OpenMP Language Terminology
5684   // Structured block - An executable statement with a single entry at the
5685   // top and a single exit at the bottom.
5686   // The point of exit cannot be a branch out of the structured block.
5687   // longjmp() and throw() must not violate the entry/exit criteria.
5688   CS->getCapturedDecl()->setNothrow();
5689 
5690   setFunctionHasBranchProtectedScope();
5691 
5692   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5693                                   DSAStack->isCancelRegion());
5694 }
5695 
5696 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5697                                                SourceLocation EndLoc) {
5698   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5699 }
5700 
5701 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5702                                              SourceLocation EndLoc) {
5703   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5704 }
5705 
5706 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5707                                               SourceLocation EndLoc) {
5708   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5709 }
5710 
5711 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
5712                                                Stmt *AStmt,
5713                                                SourceLocation StartLoc,
5714                                                SourceLocation EndLoc) {
5715   if (!AStmt)
5716     return StmtError();
5717 
5718   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5719 
5720   setFunctionHasBranchProtectedScope();
5721 
5722   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
5723                                        AStmt,
5724                                        DSAStack->getTaskgroupReductionRef());
5725 }
5726 
5727 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5728                                            SourceLocation StartLoc,
5729                                            SourceLocation EndLoc) {
5730   assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5731   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5732 }
5733 
5734 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5735                                              Stmt *AStmt,
5736                                              SourceLocation StartLoc,
5737                                              SourceLocation EndLoc) {
5738   OMPClause *DependFound = nullptr;
5739   OMPClause *DependSourceClause = nullptr;
5740   OMPClause *DependSinkClause = nullptr;
5741   bool ErrorFound = false;
5742   OMPThreadsClause *TC = nullptr;
5743   OMPSIMDClause *SC = nullptr;
5744   for (auto *C : Clauses) {
5745     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5746       DependFound = C;
5747       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5748         if (DependSourceClause) {
5749           Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5750               << getOpenMPDirectiveName(OMPD_ordered)
5751               << getOpenMPClauseName(OMPC_depend) << 2;
5752           ErrorFound = true;
5753         } else
5754           DependSourceClause = C;
5755         if (DependSinkClause) {
5756           Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5757               << 0;
5758           ErrorFound = true;
5759         }
5760       } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5761         if (DependSourceClause) {
5762           Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5763               << 1;
5764           ErrorFound = true;
5765         }
5766         DependSinkClause = C;
5767       }
5768     } else if (C->getClauseKind() == OMPC_threads)
5769       TC = cast<OMPThreadsClause>(C);
5770     else if (C->getClauseKind() == OMPC_simd)
5771       SC = cast<OMPSIMDClause>(C);
5772   }
5773   if (!ErrorFound && !SC &&
5774       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
5775     // OpenMP [2.8.1,simd Construct, Restrictions]
5776     // An ordered construct with the simd clause is the only OpenMP construct
5777     // that can appear in the simd region.
5778     Diag(StartLoc, diag::err_omp_prohibited_region_simd);
5779     ErrorFound = true;
5780   } else if (DependFound && (TC || SC)) {
5781     Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5782         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5783     ErrorFound = true;
5784   } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5785     Diag(DependFound->getLocStart(),
5786          diag::err_omp_ordered_directive_without_param);
5787     ErrorFound = true;
5788   } else if (TC || Clauses.empty()) {
5789     if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5790       SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5791       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5792           << (TC != nullptr);
5793       Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5794       ErrorFound = true;
5795     }
5796   }
5797   if ((!AStmt && !DependFound) || ErrorFound)
5798     return StmtError();
5799 
5800   if (AStmt) {
5801     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5802 
5803     setFunctionHasBranchProtectedScope();
5804   }
5805 
5806   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5807 }
5808 
5809 namespace {
5810 /// \brief Helper class for checking expression in 'omp atomic [update]'
5811 /// construct.
5812 class OpenMPAtomicUpdateChecker {
5813   /// \brief Error results for atomic update expressions.
5814   enum ExprAnalysisErrorCode {
5815     /// \brief A statement is not an expression statement.
5816     NotAnExpression,
5817     /// \brief Expression is not builtin binary or unary operation.
5818     NotABinaryOrUnaryExpression,
5819     /// \brief Unary operation is not post-/pre- increment/decrement operation.
5820     NotAnUnaryIncDecExpression,
5821     /// \brief An expression is not of scalar type.
5822     NotAScalarType,
5823     /// \brief A binary operation is not an assignment operation.
5824     NotAnAssignmentOp,
5825     /// \brief RHS part of the binary operation is not a binary expression.
5826     NotABinaryExpression,
5827     /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5828     /// expression.
5829     NotABinaryOperator,
5830     /// \brief RHS binary operation does not have reference to the updated LHS
5831     /// part.
5832     NotAnUpdateExpression,
5833     /// \brief No errors is found.
5834     NoError
5835   };
5836   /// \brief Reference to Sema.
5837   Sema &SemaRef;
5838   /// \brief A location for note diagnostics (when error is found).
5839   SourceLocation NoteLoc;
5840   /// \brief 'x' lvalue part of the source atomic expression.
5841   Expr *X;
5842   /// \brief 'expr' rvalue part of the source atomic expression.
5843   Expr *E;
5844   /// \brief Helper expression of the form
5845   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5846   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5847   Expr *UpdateExpr;
5848   /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5849   /// important for non-associative operations.
5850   bool IsXLHSInRHSPart;
5851   BinaryOperatorKind Op;
5852   SourceLocation OpLoc;
5853   /// \brief true if the source expression is a postfix unary operation, false
5854   /// if it is a prefix unary operation.
5855   bool IsPostfixUpdate;
5856 
5857 public:
5858   OpenMPAtomicUpdateChecker(Sema &SemaRef)
5859       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
5860         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
5861   /// \brief Check specified statement that it is suitable for 'atomic update'
5862   /// constructs and extract 'x', 'expr' and Operation from the original
5863   /// expression. If DiagId and NoteId == 0, then only check is performed
5864   /// without error notification.
5865   /// \param DiagId Diagnostic which should be emitted if error is found.
5866   /// \param NoteId Diagnostic note for the main error message.
5867   /// \return true if statement is not an update expression, false otherwise.
5868   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
5869   /// \brief Return the 'x' lvalue part of the source atomic expression.
5870   Expr *getX() const { return X; }
5871   /// \brief Return the 'expr' rvalue part of the source atomic expression.
5872   Expr *getExpr() const { return E; }
5873   /// \brief Return the update expression used in calculation of the updated
5874   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5875   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5876   Expr *getUpdateExpr() const { return UpdateExpr; }
5877   /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5878   /// false otherwise.
5879   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5880 
5881   /// \brief true if the source expression is a postfix unary operation, false
5882   /// if it is a prefix unary operation.
5883   bool isPostfixUpdate() const { return IsPostfixUpdate; }
5884 
5885 private:
5886   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5887                             unsigned NoteId = 0);
5888 };
5889 } // namespace
5890 
5891 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5892     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5893   ExprAnalysisErrorCode ErrorFound = NoError;
5894   SourceLocation ErrorLoc, NoteLoc;
5895   SourceRange ErrorRange, NoteRange;
5896   // Allowed constructs are:
5897   //  x = x binop expr;
5898   //  x = expr binop x;
5899   if (AtomicBinOp->getOpcode() == BO_Assign) {
5900     X = AtomicBinOp->getLHS();
5901     if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5902             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5903       if (AtomicInnerBinOp->isMultiplicativeOp() ||
5904           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5905           AtomicInnerBinOp->isBitwiseOp()) {
5906         Op = AtomicInnerBinOp->getOpcode();
5907         OpLoc = AtomicInnerBinOp->getOperatorLoc();
5908         auto *LHS = AtomicInnerBinOp->getLHS();
5909         auto *RHS = AtomicInnerBinOp->getRHS();
5910         llvm::FoldingSetNodeID XId, LHSId, RHSId;
5911         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5912                                           /*Canonical=*/true);
5913         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5914                                             /*Canonical=*/true);
5915         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5916                                             /*Canonical=*/true);
5917         if (XId == LHSId) {
5918           E = RHS;
5919           IsXLHSInRHSPart = true;
5920         } else if (XId == RHSId) {
5921           E = LHS;
5922           IsXLHSInRHSPart = false;
5923         } else {
5924           ErrorLoc = AtomicInnerBinOp->getExprLoc();
5925           ErrorRange = AtomicInnerBinOp->getSourceRange();
5926           NoteLoc = X->getExprLoc();
5927           NoteRange = X->getSourceRange();
5928           ErrorFound = NotAnUpdateExpression;
5929         }
5930       } else {
5931         ErrorLoc = AtomicInnerBinOp->getExprLoc();
5932         ErrorRange = AtomicInnerBinOp->getSourceRange();
5933         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5934         NoteRange = SourceRange(NoteLoc, NoteLoc);
5935         ErrorFound = NotABinaryOperator;
5936       }
5937     } else {
5938       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5939       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5940       ErrorFound = NotABinaryExpression;
5941     }
5942   } else {
5943     ErrorLoc = AtomicBinOp->getExprLoc();
5944     ErrorRange = AtomicBinOp->getSourceRange();
5945     NoteLoc = AtomicBinOp->getOperatorLoc();
5946     NoteRange = SourceRange(NoteLoc, NoteLoc);
5947     ErrorFound = NotAnAssignmentOp;
5948   }
5949   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
5950     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5951     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5952     return true;
5953   } else if (SemaRef.CurContext->isDependentContext())
5954     E = X = UpdateExpr = nullptr;
5955   return ErrorFound != NoError;
5956 }
5957 
5958 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5959                                                unsigned NoteId) {
5960   ExprAnalysisErrorCode ErrorFound = NoError;
5961   SourceLocation ErrorLoc, NoteLoc;
5962   SourceRange ErrorRange, NoteRange;
5963   // Allowed constructs are:
5964   //  x++;
5965   //  x--;
5966   //  ++x;
5967   //  --x;
5968   //  x binop= expr;
5969   //  x = x binop expr;
5970   //  x = expr binop x;
5971   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5972     AtomicBody = AtomicBody->IgnoreParenImpCasts();
5973     if (AtomicBody->getType()->isScalarType() ||
5974         AtomicBody->isInstantiationDependent()) {
5975       if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5976               AtomicBody->IgnoreParenImpCasts())) {
5977         // Check for Compound Assignment Operation
5978         Op = BinaryOperator::getOpForCompoundAssignment(
5979             AtomicCompAssignOp->getOpcode());
5980         OpLoc = AtomicCompAssignOp->getOperatorLoc();
5981         E = AtomicCompAssignOp->getRHS();
5982         X = AtomicCompAssignOp->getLHS()->IgnoreParens();
5983         IsXLHSInRHSPart = true;
5984       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5985                      AtomicBody->IgnoreParenImpCasts())) {
5986         // Check for Binary Operation
5987         if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5988           return true;
5989       } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
5990                      AtomicBody->IgnoreParenImpCasts())) {
5991         // Check for Unary Operation
5992         if (AtomicUnaryOp->isIncrementDecrementOp()) {
5993           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
5994           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5995           OpLoc = AtomicUnaryOp->getOperatorLoc();
5996           X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
5997           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5998           IsXLHSInRHSPart = true;
5999         } else {
6000           ErrorFound = NotAnUnaryIncDecExpression;
6001           ErrorLoc = AtomicUnaryOp->getExprLoc();
6002           ErrorRange = AtomicUnaryOp->getSourceRange();
6003           NoteLoc = AtomicUnaryOp->getOperatorLoc();
6004           NoteRange = SourceRange(NoteLoc, NoteLoc);
6005         }
6006       } else if (!AtomicBody->isInstantiationDependent()) {
6007         ErrorFound = NotABinaryOrUnaryExpression;
6008         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6009         NoteRange = ErrorRange = AtomicBody->getSourceRange();
6010       }
6011     } else {
6012       ErrorFound = NotAScalarType;
6013       NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6014       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6015     }
6016   } else {
6017     ErrorFound = NotAnExpression;
6018     NoteLoc = ErrorLoc = S->getLocStart();
6019     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6020   }
6021   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
6022     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6023     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6024     return true;
6025   } else if (SemaRef.CurContext->isDependentContext())
6026     E = X = UpdateExpr = nullptr;
6027   if (ErrorFound == NoError && E && X) {
6028     // Build an update expression of form 'OpaqueValueExpr(x) binop
6029     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6030     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6031     auto *OVEX = new (SemaRef.getASTContext())
6032         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6033     auto *OVEExpr = new (SemaRef.getASTContext())
6034         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6035     auto Update =
6036         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6037                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
6038     if (Update.isInvalid())
6039       return true;
6040     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6041                                                Sema::AA_Casting);
6042     if (Update.isInvalid())
6043       return true;
6044     UpdateExpr = Update.get();
6045   }
6046   return ErrorFound != NoError;
6047 }
6048 
6049 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6050                                             Stmt *AStmt,
6051                                             SourceLocation StartLoc,
6052                                             SourceLocation EndLoc) {
6053   if (!AStmt)
6054     return StmtError();
6055 
6056   auto *CS = cast<CapturedStmt>(AStmt);
6057   // 1.2.2 OpenMP Language Terminology
6058   // Structured block - An executable statement with a single entry at the
6059   // top and a single exit at the bottom.
6060   // The point of exit cannot be a branch out of the structured block.
6061   // longjmp() and throw() must not violate the entry/exit criteria.
6062   OpenMPClauseKind AtomicKind = OMPC_unknown;
6063   SourceLocation AtomicKindLoc;
6064   for (auto *C : Clauses) {
6065     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
6066         C->getClauseKind() == OMPC_update ||
6067         C->getClauseKind() == OMPC_capture) {
6068       if (AtomicKind != OMPC_unknown) {
6069         Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6070             << SourceRange(C->getLocStart(), C->getLocEnd());
6071         Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6072             << getOpenMPClauseName(AtomicKind);
6073       } else {
6074         AtomicKind = C->getClauseKind();
6075         AtomicKindLoc = C->getLocStart();
6076       }
6077     }
6078   }
6079 
6080   auto Body = CS->getCapturedStmt();
6081   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6082     Body = EWC->getSubExpr();
6083 
6084   Expr *X = nullptr;
6085   Expr *V = nullptr;
6086   Expr *E = nullptr;
6087   Expr *UE = nullptr;
6088   bool IsXLHSInRHSPart = false;
6089   bool IsPostfixUpdate = false;
6090   // OpenMP [2.12.6, atomic Construct]
6091   // In the next expressions:
6092   // * x and v (as applicable) are both l-value expressions with scalar type.
6093   // * During the execution of an atomic region, multiple syntactic
6094   // occurrences of x must designate the same storage location.
6095   // * Neither of v and expr (as applicable) may access the storage location
6096   // designated by x.
6097   // * Neither of x and expr (as applicable) may access the storage location
6098   // designated by v.
6099   // * expr is an expression with scalar type.
6100   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6101   // * binop, binop=, ++, and -- are not overloaded operators.
6102   // * The expression x binop expr must be numerically equivalent to x binop
6103   // (expr). This requirement is satisfied if the operators in expr have
6104   // precedence greater than binop, or by using parentheses around expr or
6105   // subexpressions of expr.
6106   // * The expression expr binop x must be numerically equivalent to (expr)
6107   // binop x. This requirement is satisfied if the operators in expr have
6108   // precedence equal to or greater than binop, or by using parentheses around
6109   // expr or subexpressions of expr.
6110   // * For forms that allow multiple occurrences of x, the number of times
6111   // that x is evaluated is unspecified.
6112   if (AtomicKind == OMPC_read) {
6113     enum {
6114       NotAnExpression,
6115       NotAnAssignmentOp,
6116       NotAScalarType,
6117       NotAnLValue,
6118       NoError
6119     } ErrorFound = NoError;
6120     SourceLocation ErrorLoc, NoteLoc;
6121     SourceRange ErrorRange, NoteRange;
6122     // If clause is read:
6123     //  v = x;
6124     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6125       auto *AtomicBinOp =
6126           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6127       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6128         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6129         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6130         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6131             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6132           if (!X->isLValue() || !V->isLValue()) {
6133             auto NotLValueExpr = X->isLValue() ? V : X;
6134             ErrorFound = NotAnLValue;
6135             ErrorLoc = AtomicBinOp->getExprLoc();
6136             ErrorRange = AtomicBinOp->getSourceRange();
6137             NoteLoc = NotLValueExpr->getExprLoc();
6138             NoteRange = NotLValueExpr->getSourceRange();
6139           }
6140         } else if (!X->isInstantiationDependent() ||
6141                    !V->isInstantiationDependent()) {
6142           auto NotScalarExpr =
6143               (X->isInstantiationDependent() || X->getType()->isScalarType())
6144                   ? V
6145                   : X;
6146           ErrorFound = NotAScalarType;
6147           ErrorLoc = AtomicBinOp->getExprLoc();
6148           ErrorRange = AtomicBinOp->getSourceRange();
6149           NoteLoc = NotScalarExpr->getExprLoc();
6150           NoteRange = NotScalarExpr->getSourceRange();
6151         }
6152       } else if (!AtomicBody->isInstantiationDependent()) {
6153         ErrorFound = NotAnAssignmentOp;
6154         ErrorLoc = AtomicBody->getExprLoc();
6155         ErrorRange = AtomicBody->getSourceRange();
6156         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6157                               : AtomicBody->getExprLoc();
6158         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6159                                 : AtomicBody->getSourceRange();
6160       }
6161     } else {
6162       ErrorFound = NotAnExpression;
6163       NoteLoc = ErrorLoc = Body->getLocStart();
6164       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6165     }
6166     if (ErrorFound != NoError) {
6167       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6168           << ErrorRange;
6169       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6170                                                       << NoteRange;
6171       return StmtError();
6172     } else if (CurContext->isDependentContext())
6173       V = X = nullptr;
6174   } else if (AtomicKind == OMPC_write) {
6175     enum {
6176       NotAnExpression,
6177       NotAnAssignmentOp,
6178       NotAScalarType,
6179       NotAnLValue,
6180       NoError
6181     } ErrorFound = NoError;
6182     SourceLocation ErrorLoc, NoteLoc;
6183     SourceRange ErrorRange, NoteRange;
6184     // If clause is write:
6185     //  x = expr;
6186     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6187       auto *AtomicBinOp =
6188           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6189       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6190         X = AtomicBinOp->getLHS();
6191         E = AtomicBinOp->getRHS();
6192         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6193             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6194           if (!X->isLValue()) {
6195             ErrorFound = NotAnLValue;
6196             ErrorLoc = AtomicBinOp->getExprLoc();
6197             ErrorRange = AtomicBinOp->getSourceRange();
6198             NoteLoc = X->getExprLoc();
6199             NoteRange = X->getSourceRange();
6200           }
6201         } else if (!X->isInstantiationDependent() ||
6202                    !E->isInstantiationDependent()) {
6203           auto NotScalarExpr =
6204               (X->isInstantiationDependent() || X->getType()->isScalarType())
6205                   ? E
6206                   : X;
6207           ErrorFound = NotAScalarType;
6208           ErrorLoc = AtomicBinOp->getExprLoc();
6209           ErrorRange = AtomicBinOp->getSourceRange();
6210           NoteLoc = NotScalarExpr->getExprLoc();
6211           NoteRange = NotScalarExpr->getSourceRange();
6212         }
6213       } else if (!AtomicBody->isInstantiationDependent()) {
6214         ErrorFound = NotAnAssignmentOp;
6215         ErrorLoc = AtomicBody->getExprLoc();
6216         ErrorRange = AtomicBody->getSourceRange();
6217         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6218                               : AtomicBody->getExprLoc();
6219         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6220                                 : AtomicBody->getSourceRange();
6221       }
6222     } else {
6223       ErrorFound = NotAnExpression;
6224       NoteLoc = ErrorLoc = Body->getLocStart();
6225       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6226     }
6227     if (ErrorFound != NoError) {
6228       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6229           << ErrorRange;
6230       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6231                                                       << NoteRange;
6232       return StmtError();
6233     } else if (CurContext->isDependentContext())
6234       E = X = nullptr;
6235   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
6236     // If clause is update:
6237     //  x++;
6238     //  x--;
6239     //  ++x;
6240     //  --x;
6241     //  x binop= expr;
6242     //  x = x binop expr;
6243     //  x = expr binop x;
6244     OpenMPAtomicUpdateChecker Checker(*this);
6245     if (Checker.checkStatement(
6246             Body, (AtomicKind == OMPC_update)
6247                       ? diag::err_omp_atomic_update_not_expression_statement
6248                       : diag::err_omp_atomic_not_expression_statement,
6249             diag::note_omp_atomic_update))
6250       return StmtError();
6251     if (!CurContext->isDependentContext()) {
6252       E = Checker.getExpr();
6253       X = Checker.getX();
6254       UE = Checker.getUpdateExpr();
6255       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6256     }
6257   } else if (AtomicKind == OMPC_capture) {
6258     enum {
6259       NotAnAssignmentOp,
6260       NotACompoundStatement,
6261       NotTwoSubstatements,
6262       NotASpecificExpression,
6263       NoError
6264     } ErrorFound = NoError;
6265     SourceLocation ErrorLoc, NoteLoc;
6266     SourceRange ErrorRange, NoteRange;
6267     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6268       // If clause is a capture:
6269       //  v = x++;
6270       //  v = x--;
6271       //  v = ++x;
6272       //  v = --x;
6273       //  v = x binop= expr;
6274       //  v = x = x binop expr;
6275       //  v = x = expr binop x;
6276       auto *AtomicBinOp =
6277           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6278       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6279         V = AtomicBinOp->getLHS();
6280         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6281         OpenMPAtomicUpdateChecker Checker(*this);
6282         if (Checker.checkStatement(
6283                 Body, diag::err_omp_atomic_capture_not_expression_statement,
6284                 diag::note_omp_atomic_update))
6285           return StmtError();
6286         E = Checker.getExpr();
6287         X = Checker.getX();
6288         UE = Checker.getUpdateExpr();
6289         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6290         IsPostfixUpdate = Checker.isPostfixUpdate();
6291       } else if (!AtomicBody->isInstantiationDependent()) {
6292         ErrorLoc = AtomicBody->getExprLoc();
6293         ErrorRange = AtomicBody->getSourceRange();
6294         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6295                               : AtomicBody->getExprLoc();
6296         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6297                                 : AtomicBody->getSourceRange();
6298         ErrorFound = NotAnAssignmentOp;
6299       }
6300       if (ErrorFound != NoError) {
6301         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6302             << ErrorRange;
6303         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6304         return StmtError();
6305       } else if (CurContext->isDependentContext()) {
6306         UE = V = E = X = nullptr;
6307       }
6308     } else {
6309       // If clause is a capture:
6310       //  { v = x; x = expr; }
6311       //  { v = x; x++; }
6312       //  { v = x; x--; }
6313       //  { v = x; ++x; }
6314       //  { v = x; --x; }
6315       //  { v = x; x binop= expr; }
6316       //  { v = x; x = x binop expr; }
6317       //  { v = x; x = expr binop x; }
6318       //  { x++; v = x; }
6319       //  { x--; v = x; }
6320       //  { ++x; v = x; }
6321       //  { --x; v = x; }
6322       //  { x binop= expr; v = x; }
6323       //  { x = x binop expr; v = x; }
6324       //  { x = expr binop x; v = x; }
6325       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6326         // Check that this is { expr1; expr2; }
6327         if (CS->size() == 2) {
6328           auto *First = CS->body_front();
6329           auto *Second = CS->body_back();
6330           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6331             First = EWC->getSubExpr()->IgnoreParenImpCasts();
6332           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6333             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6334           // Need to find what subexpression is 'v' and what is 'x'.
6335           OpenMPAtomicUpdateChecker Checker(*this);
6336           bool IsUpdateExprFound = !Checker.checkStatement(Second);
6337           BinaryOperator *BinOp = nullptr;
6338           if (IsUpdateExprFound) {
6339             BinOp = dyn_cast<BinaryOperator>(First);
6340             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6341           }
6342           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6343             //  { v = x; x++; }
6344             //  { v = x; x--; }
6345             //  { v = x; ++x; }
6346             //  { v = x; --x; }
6347             //  { v = x; x binop= expr; }
6348             //  { v = x; x = x binop expr; }
6349             //  { v = x; x = expr binop x; }
6350             // Check that the first expression has form v = x.
6351             auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6352             llvm::FoldingSetNodeID XId, PossibleXId;
6353             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6354             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6355             IsUpdateExprFound = XId == PossibleXId;
6356             if (IsUpdateExprFound) {
6357               V = BinOp->getLHS();
6358               X = Checker.getX();
6359               E = Checker.getExpr();
6360               UE = Checker.getUpdateExpr();
6361               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6362               IsPostfixUpdate = true;
6363             }
6364           }
6365           if (!IsUpdateExprFound) {
6366             IsUpdateExprFound = !Checker.checkStatement(First);
6367             BinOp = nullptr;
6368             if (IsUpdateExprFound) {
6369               BinOp = dyn_cast<BinaryOperator>(Second);
6370               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6371             }
6372             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6373               //  { x++; v = x; }
6374               //  { x--; v = x; }
6375               //  { ++x; v = x; }
6376               //  { --x; v = x; }
6377               //  { x binop= expr; v = x; }
6378               //  { x = x binop expr; v = x; }
6379               //  { x = expr binop x; v = x; }
6380               // Check that the second expression has form v = x.
6381               auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6382               llvm::FoldingSetNodeID XId, PossibleXId;
6383               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6384               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6385               IsUpdateExprFound = XId == PossibleXId;
6386               if (IsUpdateExprFound) {
6387                 V = BinOp->getLHS();
6388                 X = Checker.getX();
6389                 E = Checker.getExpr();
6390                 UE = Checker.getUpdateExpr();
6391                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6392                 IsPostfixUpdate = false;
6393               }
6394             }
6395           }
6396           if (!IsUpdateExprFound) {
6397             //  { v = x; x = expr; }
6398             auto *FirstExpr = dyn_cast<Expr>(First);
6399             auto *SecondExpr = dyn_cast<Expr>(Second);
6400             if (!FirstExpr || !SecondExpr ||
6401                 !(FirstExpr->isInstantiationDependent() ||
6402                   SecondExpr->isInstantiationDependent())) {
6403               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6404               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
6405                 ErrorFound = NotAnAssignmentOp;
6406                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6407                                                 : First->getLocStart();
6408                 NoteRange = ErrorRange = FirstBinOp
6409                                              ? FirstBinOp->getSourceRange()
6410                                              : SourceRange(ErrorLoc, ErrorLoc);
6411               } else {
6412                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6413                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6414                   ErrorFound = NotAnAssignmentOp;
6415                   NoteLoc = ErrorLoc = SecondBinOp
6416                                            ? SecondBinOp->getOperatorLoc()
6417                                            : Second->getLocStart();
6418                   NoteRange = ErrorRange =
6419                       SecondBinOp ? SecondBinOp->getSourceRange()
6420                                   : SourceRange(ErrorLoc, ErrorLoc);
6421                 } else {
6422                   auto *PossibleXRHSInFirst =
6423                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
6424                   auto *PossibleXLHSInSecond =
6425                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
6426                   llvm::FoldingSetNodeID X1Id, X2Id;
6427                   PossibleXRHSInFirst->Profile(X1Id, Context,
6428                                                /*Canonical=*/true);
6429                   PossibleXLHSInSecond->Profile(X2Id, Context,
6430                                                 /*Canonical=*/true);
6431                   IsUpdateExprFound = X1Id == X2Id;
6432                   if (IsUpdateExprFound) {
6433                     V = FirstBinOp->getLHS();
6434                     X = SecondBinOp->getLHS();
6435                     E = SecondBinOp->getRHS();
6436                     UE = nullptr;
6437                     IsXLHSInRHSPart = false;
6438                     IsPostfixUpdate = true;
6439                   } else {
6440                     ErrorFound = NotASpecificExpression;
6441                     ErrorLoc = FirstBinOp->getExprLoc();
6442                     ErrorRange = FirstBinOp->getSourceRange();
6443                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6444                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
6445                   }
6446                 }
6447               }
6448             }
6449           }
6450         } else {
6451           NoteLoc = ErrorLoc = Body->getLocStart();
6452           NoteRange = ErrorRange =
6453               SourceRange(Body->getLocStart(), Body->getLocStart());
6454           ErrorFound = NotTwoSubstatements;
6455         }
6456       } else {
6457         NoteLoc = ErrorLoc = Body->getLocStart();
6458         NoteRange = ErrorRange =
6459             SourceRange(Body->getLocStart(), Body->getLocStart());
6460         ErrorFound = NotACompoundStatement;
6461       }
6462       if (ErrorFound != NoError) {
6463         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6464             << ErrorRange;
6465         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6466         return StmtError();
6467       } else if (CurContext->isDependentContext()) {
6468         UE = V = E = X = nullptr;
6469       }
6470     }
6471   }
6472 
6473   setFunctionHasBranchProtectedScope();
6474 
6475   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6476                                     X, V, E, UE, IsXLHSInRHSPart,
6477                                     IsPostfixUpdate);
6478 }
6479 
6480 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6481                                             Stmt *AStmt,
6482                                             SourceLocation StartLoc,
6483                                             SourceLocation EndLoc) {
6484   if (!AStmt)
6485     return StmtError();
6486 
6487   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6488   // 1.2.2 OpenMP Language Terminology
6489   // Structured block - An executable statement with a single entry at the
6490   // top and a single exit at the bottom.
6491   // The point of exit cannot be a branch out of the structured block.
6492   // longjmp() and throw() must not violate the entry/exit criteria.
6493   CS->getCapturedDecl()->setNothrow();
6494   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6495        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6496     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6497     // 1.2.2 OpenMP Language Terminology
6498     // Structured block - An executable statement with a single entry at the
6499     // top and a single exit at the bottom.
6500     // The point of exit cannot be a branch out of the structured block.
6501     // longjmp() and throw() must not violate the entry/exit criteria.
6502     CS->getCapturedDecl()->setNothrow();
6503   }
6504 
6505   // OpenMP [2.16, Nesting of Regions]
6506   // If specified, a teams construct must be contained within a target
6507   // construct. That target construct must contain no statements or directives
6508   // outside of the teams construct.
6509   if (DSAStack->hasInnerTeamsRegion()) {
6510     Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
6511     bool OMPTeamsFound = true;
6512     if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6513       auto I = CS->body_begin();
6514       while (I != CS->body_end()) {
6515         auto *OED = dyn_cast<OMPExecutableDirective>(*I);
6516         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6517           OMPTeamsFound = false;
6518           break;
6519         }
6520         ++I;
6521       }
6522       assert(I != CS->body_end() && "Not found statement");
6523       S = *I;
6524     } else {
6525       auto *OED = dyn_cast<OMPExecutableDirective>(S);
6526       OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
6527     }
6528     if (!OMPTeamsFound) {
6529       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6530       Diag(DSAStack->getInnerTeamsRegionLoc(),
6531            diag::note_omp_nested_teams_construct_here);
6532       Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6533           << isa<OMPExecutableDirective>(S);
6534       return StmtError();
6535     }
6536   }
6537 
6538   setFunctionHasBranchProtectedScope();
6539 
6540   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6541 }
6542 
6543 StmtResult
6544 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6545                                          Stmt *AStmt, SourceLocation StartLoc,
6546                                          SourceLocation EndLoc) {
6547   if (!AStmt)
6548     return StmtError();
6549 
6550   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6551   // 1.2.2 OpenMP Language Terminology
6552   // Structured block - An executable statement with a single entry at the
6553   // top and a single exit at the bottom.
6554   // The point of exit cannot be a branch out of the structured block.
6555   // longjmp() and throw() must not violate the entry/exit criteria.
6556   CS->getCapturedDecl()->setNothrow();
6557   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
6558        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6559     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6560     // 1.2.2 OpenMP Language Terminology
6561     // Structured block - An executable statement with a single entry at the
6562     // top and a single exit at the bottom.
6563     // The point of exit cannot be a branch out of the structured block.
6564     // longjmp() and throw() must not violate the entry/exit criteria.
6565     CS->getCapturedDecl()->setNothrow();
6566   }
6567 
6568   setFunctionHasBranchProtectedScope();
6569 
6570   return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6571                                             AStmt);
6572 }
6573 
6574 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6575     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6576     SourceLocation EndLoc,
6577     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6578   if (!AStmt)
6579     return StmtError();
6580 
6581   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6582   // 1.2.2 OpenMP Language Terminology
6583   // Structured block - An executable statement with a single entry at the
6584   // top and a single exit at the bottom.
6585   // The point of exit cannot be a branch out of the structured block.
6586   // longjmp() and throw() must not violate the entry/exit criteria.
6587   CS->getCapturedDecl()->setNothrow();
6588   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6589        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6590     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6591     // 1.2.2 OpenMP Language Terminology
6592     // Structured block - An executable statement with a single entry at the
6593     // top and a single exit at the bottom.
6594     // The point of exit cannot be a branch out of the structured block.
6595     // longjmp() and throw() must not violate the entry/exit criteria.
6596     CS->getCapturedDecl()->setNothrow();
6597   }
6598 
6599   OMPLoopDirective::HelperExprs B;
6600   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6601   // define the nested loops number.
6602   unsigned NestedLoopCount =
6603       CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6604                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
6605                       VarsWithImplicitDSA, B);
6606   if (NestedLoopCount == 0)
6607     return StmtError();
6608 
6609   assert((CurContext->isDependentContext() || B.builtAll()) &&
6610          "omp target parallel for loop exprs were not built");
6611 
6612   if (!CurContext->isDependentContext()) {
6613     // Finalize the clauses that need pre-built expressions for CodeGen.
6614     for (auto C : Clauses) {
6615       if (auto *LC = dyn_cast<OMPLinearClause>(C))
6616         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6617                                      B.NumIterations, *this, CurScope,
6618                                      DSAStack))
6619           return StmtError();
6620     }
6621   }
6622 
6623   setFunctionHasBranchProtectedScope();
6624   return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6625                                                NestedLoopCount, Clauses, AStmt,
6626                                                B, DSAStack->isCancelRegion());
6627 }
6628 
6629 /// Check for existence of a map clause in the list of clauses.
6630 static bool hasClauses(ArrayRef<OMPClause *> Clauses,
6631                        const OpenMPClauseKind K) {
6632   return llvm::any_of(
6633       Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
6634 }
6635 
6636 template <typename... Params>
6637 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
6638                        const Params... ClauseTypes) {
6639   return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
6640 }
6641 
6642 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6643                                                 Stmt *AStmt,
6644                                                 SourceLocation StartLoc,
6645                                                 SourceLocation EndLoc) {
6646   if (!AStmt)
6647     return StmtError();
6648 
6649   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6650 
6651   // OpenMP [2.10.1, Restrictions, p. 97]
6652   // At least one map clause must appear on the directive.
6653   if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
6654     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6655         << "'map' or 'use_device_ptr'"
6656         << getOpenMPDirectiveName(OMPD_target_data);
6657     return StmtError();
6658   }
6659 
6660   setFunctionHasBranchProtectedScope();
6661 
6662   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6663                                         AStmt);
6664 }
6665 
6666 StmtResult
6667 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6668                                           SourceLocation StartLoc,
6669                                           SourceLocation EndLoc, Stmt *AStmt) {
6670   if (!AStmt)
6671     return StmtError();
6672 
6673   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6674   // 1.2.2 OpenMP Language Terminology
6675   // Structured block - An executable statement with a single entry at the
6676   // top and a single exit at the bottom.
6677   // The point of exit cannot be a branch out of the structured block.
6678   // longjmp() and throw() must not violate the entry/exit criteria.
6679   CS->getCapturedDecl()->setNothrow();
6680   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
6681        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6682     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6683     // 1.2.2 OpenMP Language Terminology
6684     // Structured block - An executable statement with a single entry at the
6685     // top and a single exit at the bottom.
6686     // The point of exit cannot be a branch out of the structured block.
6687     // longjmp() and throw() must not violate the entry/exit criteria.
6688     CS->getCapturedDecl()->setNothrow();
6689   }
6690 
6691   // OpenMP [2.10.2, Restrictions, p. 99]
6692   // At least one map clause must appear on the directive.
6693   if (!hasClauses(Clauses, OMPC_map)) {
6694     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6695         << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
6696     return StmtError();
6697   }
6698 
6699   return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6700                                              AStmt);
6701 }
6702 
6703 StmtResult
6704 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6705                                          SourceLocation StartLoc,
6706                                          SourceLocation EndLoc, Stmt *AStmt) {
6707   if (!AStmt)
6708     return StmtError();
6709 
6710   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6711   // 1.2.2 OpenMP Language Terminology
6712   // Structured block - An executable statement with a single entry at the
6713   // top and a single exit at the bottom.
6714   // The point of exit cannot be a branch out of the structured block.
6715   // longjmp() and throw() must not violate the entry/exit criteria.
6716   CS->getCapturedDecl()->setNothrow();
6717   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
6718        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6719     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6720     // 1.2.2 OpenMP Language Terminology
6721     // Structured block - An executable statement with a single entry at the
6722     // top and a single exit at the bottom.
6723     // The point of exit cannot be a branch out of the structured block.
6724     // longjmp() and throw() must not violate the entry/exit criteria.
6725     CS->getCapturedDecl()->setNothrow();
6726   }
6727 
6728   // OpenMP [2.10.3, Restrictions, p. 102]
6729   // At least one map clause must appear on the directive.
6730   if (!hasClauses(Clauses, OMPC_map)) {
6731     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6732         << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
6733     return StmtError();
6734   }
6735 
6736   return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6737                                             AStmt);
6738 }
6739 
6740 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6741                                                   SourceLocation StartLoc,
6742                                                   SourceLocation EndLoc,
6743                                                   Stmt *AStmt) {
6744   if (!AStmt)
6745     return StmtError();
6746 
6747   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6748   // 1.2.2 OpenMP Language Terminology
6749   // Structured block - An executable statement with a single entry at the
6750   // top and a single exit at the bottom.
6751   // The point of exit cannot be a branch out of the structured block.
6752   // longjmp() and throw() must not violate the entry/exit criteria.
6753   CS->getCapturedDecl()->setNothrow();
6754   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
6755        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6756     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6757     // 1.2.2 OpenMP Language Terminology
6758     // Structured block - An executable statement with a single entry at the
6759     // top and a single exit at the bottom.
6760     // The point of exit cannot be a branch out of the structured block.
6761     // longjmp() and throw() must not violate the entry/exit criteria.
6762     CS->getCapturedDecl()->setNothrow();
6763   }
6764 
6765   if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
6766     Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6767     return StmtError();
6768   }
6769   return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
6770                                           AStmt);
6771 }
6772 
6773 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6774                                            Stmt *AStmt, SourceLocation StartLoc,
6775                                            SourceLocation EndLoc) {
6776   if (!AStmt)
6777     return StmtError();
6778 
6779   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6780   // 1.2.2 OpenMP Language Terminology
6781   // Structured block - An executable statement with a single entry at the
6782   // top and a single exit at the bottom.
6783   // The point of exit cannot be a branch out of the structured block.
6784   // longjmp() and throw() must not violate the entry/exit criteria.
6785   CS->getCapturedDecl()->setNothrow();
6786 
6787   setFunctionHasBranchProtectedScope();
6788 
6789   DSAStack->setParentTeamsRegionLoc(StartLoc);
6790 
6791   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6792 }
6793 
6794 StmtResult
6795 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6796                                             SourceLocation EndLoc,
6797                                             OpenMPDirectiveKind CancelRegion) {
6798   if (DSAStack->isParentNowaitRegion()) {
6799     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6800     return StmtError();
6801   }
6802   if (DSAStack->isParentOrderedRegion()) {
6803     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6804     return StmtError();
6805   }
6806   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6807                                                CancelRegion);
6808 }
6809 
6810 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6811                                             SourceLocation StartLoc,
6812                                             SourceLocation EndLoc,
6813                                             OpenMPDirectiveKind CancelRegion) {
6814   if (DSAStack->isParentNowaitRegion()) {
6815     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6816     return StmtError();
6817   }
6818   if (DSAStack->isParentOrderedRegion()) {
6819     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6820     return StmtError();
6821   }
6822   DSAStack->setParentCancelRegion(/*Cancel=*/true);
6823   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6824                                     CancelRegion);
6825 }
6826 
6827 static bool checkGrainsizeNumTasksClauses(Sema &S,
6828                                           ArrayRef<OMPClause *> Clauses) {
6829   OMPClause *PrevClause = nullptr;
6830   bool ErrorFound = false;
6831   for (auto *C : Clauses) {
6832     if (C->getClauseKind() == OMPC_grainsize ||
6833         C->getClauseKind() == OMPC_num_tasks) {
6834       if (!PrevClause)
6835         PrevClause = C;
6836       else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6837         S.Diag(C->getLocStart(),
6838                diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6839             << getOpenMPClauseName(C->getClauseKind())
6840             << getOpenMPClauseName(PrevClause->getClauseKind());
6841         S.Diag(PrevClause->getLocStart(),
6842                diag::note_omp_previous_grainsize_num_tasks)
6843             << getOpenMPClauseName(PrevClause->getClauseKind());
6844         ErrorFound = true;
6845       }
6846     }
6847   }
6848   return ErrorFound;
6849 }
6850 
6851 static bool checkReductionClauseWithNogroup(Sema &S,
6852                                             ArrayRef<OMPClause *> Clauses) {
6853   OMPClause *ReductionClause = nullptr;
6854   OMPClause *NogroupClause = nullptr;
6855   for (auto *C : Clauses) {
6856     if (C->getClauseKind() == OMPC_reduction) {
6857       ReductionClause = C;
6858       if (NogroupClause)
6859         break;
6860       continue;
6861     }
6862     if (C->getClauseKind() == OMPC_nogroup) {
6863       NogroupClause = C;
6864       if (ReductionClause)
6865         break;
6866       continue;
6867     }
6868   }
6869   if (ReductionClause && NogroupClause) {
6870     S.Diag(ReductionClause->getLocStart(), diag::err_omp_reduction_with_nogroup)
6871         << SourceRange(NogroupClause->getLocStart(),
6872                        NogroupClause->getLocEnd());
6873     return true;
6874   }
6875   return false;
6876 }
6877 
6878 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6879     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6880     SourceLocation EndLoc,
6881     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6882   if (!AStmt)
6883     return StmtError();
6884 
6885   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6886   OMPLoopDirective::HelperExprs B;
6887   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6888   // define the nested loops number.
6889   unsigned NestedLoopCount =
6890       CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
6891                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6892                       VarsWithImplicitDSA, B);
6893   if (NestedLoopCount == 0)
6894     return StmtError();
6895 
6896   assert((CurContext->isDependentContext() || B.builtAll()) &&
6897          "omp for loop exprs were not built");
6898 
6899   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6900   // The grainsize clause and num_tasks clause are mutually exclusive and may
6901   // not appear on the same taskloop directive.
6902   if (checkGrainsizeNumTasksClauses(*this, Clauses))
6903     return StmtError();
6904   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6905   // If a reduction clause is present on the taskloop directive, the nogroup
6906   // clause must not be specified.
6907   if (checkReductionClauseWithNogroup(*this, Clauses))
6908     return StmtError();
6909 
6910   setFunctionHasBranchProtectedScope();
6911   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6912                                       NestedLoopCount, Clauses, AStmt, B);
6913 }
6914 
6915 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6916     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6917     SourceLocation EndLoc,
6918     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6919   if (!AStmt)
6920     return StmtError();
6921 
6922   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6923   OMPLoopDirective::HelperExprs B;
6924   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6925   // define the nested loops number.
6926   unsigned NestedLoopCount =
6927       CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6928                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6929                       VarsWithImplicitDSA, B);
6930   if (NestedLoopCount == 0)
6931     return StmtError();
6932 
6933   assert((CurContext->isDependentContext() || B.builtAll()) &&
6934          "omp for loop exprs were not built");
6935 
6936   if (!CurContext->isDependentContext()) {
6937     // Finalize the clauses that need pre-built expressions for CodeGen.
6938     for (auto C : Clauses) {
6939       if (auto *LC = dyn_cast<OMPLinearClause>(C))
6940         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6941                                      B.NumIterations, *this, CurScope,
6942                                      DSAStack))
6943           return StmtError();
6944     }
6945   }
6946 
6947   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6948   // The grainsize clause and num_tasks clause are mutually exclusive and may
6949   // not appear on the same taskloop directive.
6950   if (checkGrainsizeNumTasksClauses(*this, Clauses))
6951     return StmtError();
6952   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6953   // If a reduction clause is present on the taskloop directive, the nogroup
6954   // clause must not be specified.
6955   if (checkReductionClauseWithNogroup(*this, Clauses))
6956     return StmtError();
6957   if (checkSimdlenSafelenSpecified(*this, Clauses))
6958     return StmtError();
6959 
6960   setFunctionHasBranchProtectedScope();
6961   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6962                                           NestedLoopCount, Clauses, AStmt, B);
6963 }
6964 
6965 StmtResult Sema::ActOnOpenMPDistributeDirective(
6966     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6967     SourceLocation EndLoc,
6968     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6969   if (!AStmt)
6970     return StmtError();
6971 
6972   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6973   OMPLoopDirective::HelperExprs B;
6974   // In presence of clause 'collapse' with number of loops, it will
6975   // define the nested loops number.
6976   unsigned NestedLoopCount =
6977       CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6978                       nullptr /*ordered not a clause on distribute*/, AStmt,
6979                       *this, *DSAStack, VarsWithImplicitDSA, B);
6980   if (NestedLoopCount == 0)
6981     return StmtError();
6982 
6983   assert((CurContext->isDependentContext() || B.builtAll()) &&
6984          "omp for loop exprs were not built");
6985 
6986   setFunctionHasBranchProtectedScope();
6987   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6988                                         NestedLoopCount, Clauses, AStmt, B);
6989 }
6990 
6991 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
6992     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6993     SourceLocation EndLoc,
6994     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6995   if (!AStmt)
6996     return StmtError();
6997 
6998   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6999   // 1.2.2 OpenMP Language Terminology
7000   // Structured block - An executable statement with a single entry at the
7001   // top and a single exit at the bottom.
7002   // The point of exit cannot be a branch out of the structured block.
7003   // longjmp() and throw() must not violate the entry/exit criteria.
7004   CS->getCapturedDecl()->setNothrow();
7005   for (int ThisCaptureLevel =
7006            getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7007        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7008     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7009     // 1.2.2 OpenMP Language Terminology
7010     // Structured block - An executable statement with a single entry at the
7011     // top and a single exit at the bottom.
7012     // The point of exit cannot be a branch out of the structured block.
7013     // longjmp() and throw() must not violate the entry/exit criteria.
7014     CS->getCapturedDecl()->setNothrow();
7015   }
7016 
7017   OMPLoopDirective::HelperExprs B;
7018   // In presence of clause 'collapse' with number of loops, it will
7019   // define the nested loops number.
7020   unsigned NestedLoopCount = CheckOpenMPLoop(
7021       OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7022       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7023       VarsWithImplicitDSA, B);
7024   if (NestedLoopCount == 0)
7025     return StmtError();
7026 
7027   assert((CurContext->isDependentContext() || B.builtAll()) &&
7028          "omp for loop exprs were not built");
7029 
7030   setFunctionHasBranchProtectedScope();
7031   return OMPDistributeParallelForDirective::Create(
7032       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7033       DSAStack->isCancelRegion());
7034 }
7035 
7036 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7037     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7038     SourceLocation EndLoc,
7039     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7040   if (!AStmt)
7041     return StmtError();
7042 
7043   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7044   // 1.2.2 OpenMP Language Terminology
7045   // Structured block - An executable statement with a single entry at the
7046   // top and a single exit at the bottom.
7047   // The point of exit cannot be a branch out of the structured block.
7048   // longjmp() and throw() must not violate the entry/exit criteria.
7049   CS->getCapturedDecl()->setNothrow();
7050   for (int ThisCaptureLevel =
7051            getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7052        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7053     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7054     // 1.2.2 OpenMP Language Terminology
7055     // Structured block - An executable statement with a single entry at the
7056     // top and a single exit at the bottom.
7057     // The point of exit cannot be a branch out of the structured block.
7058     // longjmp() and throw() must not violate the entry/exit criteria.
7059     CS->getCapturedDecl()->setNothrow();
7060   }
7061 
7062   OMPLoopDirective::HelperExprs B;
7063   // In presence of clause 'collapse' with number of loops, it will
7064   // define the nested loops number.
7065   unsigned NestedLoopCount = CheckOpenMPLoop(
7066       OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7067       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7068       VarsWithImplicitDSA, B);
7069   if (NestedLoopCount == 0)
7070     return StmtError();
7071 
7072   assert((CurContext->isDependentContext() || B.builtAll()) &&
7073          "omp for loop exprs were not built");
7074 
7075   if (!CurContext->isDependentContext()) {
7076     // Finalize the clauses that need pre-built expressions for CodeGen.
7077     for (auto C : Clauses) {
7078       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7079         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7080                                      B.NumIterations, *this, CurScope,
7081                                      DSAStack))
7082           return StmtError();
7083     }
7084   }
7085 
7086   if (checkSimdlenSafelenSpecified(*this, Clauses))
7087     return StmtError();
7088 
7089   setFunctionHasBranchProtectedScope();
7090   return OMPDistributeParallelForSimdDirective::Create(
7091       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7092 }
7093 
7094 StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7095     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7096     SourceLocation EndLoc,
7097     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7098   if (!AStmt)
7099     return StmtError();
7100 
7101   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7102   // 1.2.2 OpenMP Language Terminology
7103   // Structured block - An executable statement with a single entry at the
7104   // top and a single exit at the bottom.
7105   // The point of exit cannot be a branch out of the structured block.
7106   // longjmp() and throw() must not violate the entry/exit criteria.
7107   CS->getCapturedDecl()->setNothrow();
7108   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7109        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7110     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7111     // 1.2.2 OpenMP Language Terminology
7112     // Structured block - An executable statement with a single entry at the
7113     // top and a single exit at the bottom.
7114     // The point of exit cannot be a branch out of the structured block.
7115     // longjmp() and throw() must not violate the entry/exit criteria.
7116     CS->getCapturedDecl()->setNothrow();
7117   }
7118 
7119   OMPLoopDirective::HelperExprs B;
7120   // In presence of clause 'collapse' with number of loops, it will
7121   // define the nested loops number.
7122   unsigned NestedLoopCount =
7123       CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7124                       nullptr /*ordered not a clause on distribute*/, CS, *this,
7125                       *DSAStack, VarsWithImplicitDSA, B);
7126   if (NestedLoopCount == 0)
7127     return StmtError();
7128 
7129   assert((CurContext->isDependentContext() || B.builtAll()) &&
7130          "omp for loop exprs were not built");
7131 
7132   if (!CurContext->isDependentContext()) {
7133     // Finalize the clauses that need pre-built expressions for CodeGen.
7134     for (auto C : Clauses) {
7135       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7136         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7137                                      B.NumIterations, *this, CurScope,
7138                                      DSAStack))
7139           return StmtError();
7140     }
7141   }
7142 
7143   if (checkSimdlenSafelenSpecified(*this, Clauses))
7144     return StmtError();
7145 
7146   setFunctionHasBranchProtectedScope();
7147   return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7148                                             NestedLoopCount, Clauses, AStmt, B);
7149 }
7150 
7151 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7152     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7153     SourceLocation EndLoc,
7154     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7155   if (!AStmt)
7156     return StmtError();
7157 
7158   CapturedStmt *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_parallel_for);
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   OMPLoopDirective::HelperExprs B;
7177   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7178   // define the nested loops number.
7179   unsigned NestedLoopCount = CheckOpenMPLoop(
7180       OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7181       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
7182       VarsWithImplicitDSA, B);
7183   if (NestedLoopCount == 0)
7184     return StmtError();
7185 
7186   assert((CurContext->isDependentContext() || B.builtAll()) &&
7187          "omp target parallel for simd loop exprs were not built");
7188 
7189   if (!CurContext->isDependentContext()) {
7190     // Finalize the clauses that need pre-built expressions for CodeGen.
7191     for (auto C : Clauses) {
7192       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7193         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7194                                      B.NumIterations, *this, CurScope,
7195                                      DSAStack))
7196           return StmtError();
7197     }
7198   }
7199   if (checkSimdlenSafelenSpecified(*this, Clauses))
7200     return StmtError();
7201 
7202   setFunctionHasBranchProtectedScope();
7203   return OMPTargetParallelForSimdDirective::Create(
7204       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7205 }
7206 
7207 StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7208     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7209     SourceLocation EndLoc,
7210     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7211   if (!AStmt)
7212     return StmtError();
7213 
7214   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7215   // 1.2.2 OpenMP Language Terminology
7216   // Structured block - An executable statement with a single entry at the
7217   // top and a single exit at the bottom.
7218   // The point of exit cannot be a branch out of the structured block.
7219   // longjmp() and throw() must not violate the entry/exit criteria.
7220   CS->getCapturedDecl()->setNothrow();
7221   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7222        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7223     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7224     // 1.2.2 OpenMP Language Terminology
7225     // Structured block - An executable statement with a single entry at the
7226     // top and a single exit at the bottom.
7227     // The point of exit cannot be a branch out of the structured block.
7228     // longjmp() and throw() must not violate the entry/exit criteria.
7229     CS->getCapturedDecl()->setNothrow();
7230   }
7231 
7232   OMPLoopDirective::HelperExprs B;
7233   // In presence of clause 'collapse' with number of loops, it will define the
7234   // nested loops number.
7235   unsigned NestedLoopCount =
7236       CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
7237                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
7238                       VarsWithImplicitDSA, B);
7239   if (NestedLoopCount == 0)
7240     return StmtError();
7241 
7242   assert((CurContext->isDependentContext() || B.builtAll()) &&
7243          "omp target simd loop exprs were not built");
7244 
7245   if (!CurContext->isDependentContext()) {
7246     // Finalize the clauses that need pre-built expressions for CodeGen.
7247     for (auto C : Clauses) {
7248       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7249         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7250                                      B.NumIterations, *this, CurScope,
7251                                      DSAStack))
7252           return StmtError();
7253     }
7254   }
7255 
7256   if (checkSimdlenSafelenSpecified(*this, Clauses))
7257     return StmtError();
7258 
7259   setFunctionHasBranchProtectedScope();
7260   return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7261                                         NestedLoopCount, Clauses, AStmt, B);
7262 }
7263 
7264 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7265     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7266     SourceLocation EndLoc,
7267     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7268   if (!AStmt)
7269     return StmtError();
7270 
7271   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7272   // 1.2.2 OpenMP Language Terminology
7273   // Structured block - An executable statement with a single entry at the
7274   // top and a single exit at the bottom.
7275   // The point of exit cannot be a branch out of the structured block.
7276   // longjmp() and throw() must not violate the entry/exit criteria.
7277   CS->getCapturedDecl()->setNothrow();
7278   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7279        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7280     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7281     // 1.2.2 OpenMP Language Terminology
7282     // Structured block - An executable statement with a single entry at the
7283     // top and a single exit at the bottom.
7284     // The point of exit cannot be a branch out of the structured block.
7285     // longjmp() and throw() must not violate the entry/exit criteria.
7286     CS->getCapturedDecl()->setNothrow();
7287   }
7288 
7289   OMPLoopDirective::HelperExprs B;
7290   // In presence of clause 'collapse' with number of loops, it will
7291   // define the nested loops number.
7292   unsigned NestedLoopCount =
7293       CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
7294                       nullptr /*ordered not a clause on distribute*/, CS, *this,
7295                       *DSAStack, VarsWithImplicitDSA, B);
7296   if (NestedLoopCount == 0)
7297     return StmtError();
7298 
7299   assert((CurContext->isDependentContext() || B.builtAll()) &&
7300          "omp teams distribute loop exprs were not built");
7301 
7302   setFunctionHasBranchProtectedScope();
7303 
7304   DSAStack->setParentTeamsRegionLoc(StartLoc);
7305 
7306   return OMPTeamsDistributeDirective::Create(
7307       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7308 }
7309 
7310 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7311     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7312     SourceLocation EndLoc,
7313     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7314   if (!AStmt)
7315     return StmtError();
7316 
7317   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7318   // 1.2.2 OpenMP Language Terminology
7319   // Structured block - An executable statement with a single entry at the
7320   // top and a single exit at the bottom.
7321   // The point of exit cannot be a branch out of the structured block.
7322   // longjmp() and throw() must not violate the entry/exit criteria.
7323   CS->getCapturedDecl()->setNothrow();
7324   for (int ThisCaptureLevel =
7325            getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7326        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7327     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7328     // 1.2.2 OpenMP Language Terminology
7329     // Structured block - An executable statement with a single entry at the
7330     // top and a single exit at the bottom.
7331     // The point of exit cannot be a branch out of the structured block.
7332     // longjmp() and throw() must not violate the entry/exit criteria.
7333     CS->getCapturedDecl()->setNothrow();
7334   }
7335 
7336 
7337   OMPLoopDirective::HelperExprs B;
7338   // In presence of clause 'collapse' with number of loops, it will
7339   // define the nested loops number.
7340   unsigned NestedLoopCount = CheckOpenMPLoop(
7341       OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
7342       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7343       VarsWithImplicitDSA, B);
7344 
7345   if (NestedLoopCount == 0)
7346     return StmtError();
7347 
7348   assert((CurContext->isDependentContext() || B.builtAll()) &&
7349          "omp teams distribute simd loop exprs were not built");
7350 
7351   if (!CurContext->isDependentContext()) {
7352     // Finalize the clauses that need pre-built expressions for CodeGen.
7353     for (auto C : Clauses) {
7354       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7355         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7356                                      B.NumIterations, *this, CurScope,
7357                                      DSAStack))
7358           return StmtError();
7359     }
7360   }
7361 
7362   if (checkSimdlenSafelenSpecified(*this, Clauses))
7363     return StmtError();
7364 
7365   setFunctionHasBranchProtectedScope();
7366 
7367   DSAStack->setParentTeamsRegionLoc(StartLoc);
7368 
7369   return OMPTeamsDistributeSimdDirective::Create(
7370       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7371 }
7372 
7373 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7374     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7375     SourceLocation EndLoc,
7376     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7377   if (!AStmt)
7378     return StmtError();
7379 
7380   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7381   // 1.2.2 OpenMP Language Terminology
7382   // Structured block - An executable statement with a single entry at the
7383   // top and a single exit at the bottom.
7384   // The point of exit cannot be a branch out of the structured block.
7385   // longjmp() and throw() must not violate the entry/exit criteria.
7386   CS->getCapturedDecl()->setNothrow();
7387 
7388   for (int ThisCaptureLevel =
7389            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7390        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7391     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7392     // 1.2.2 OpenMP Language Terminology
7393     // Structured block - An executable statement with a single entry at the
7394     // top and a single exit at the bottom.
7395     // The point of exit cannot be a branch out of the structured block.
7396     // longjmp() and throw() must not violate the entry/exit criteria.
7397     CS->getCapturedDecl()->setNothrow();
7398   }
7399 
7400   OMPLoopDirective::HelperExprs B;
7401   // In presence of clause 'collapse' with number of loops, it will
7402   // define the nested loops number.
7403   auto NestedLoopCount = CheckOpenMPLoop(
7404       OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7405       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7406       VarsWithImplicitDSA, B);
7407 
7408   if (NestedLoopCount == 0)
7409     return StmtError();
7410 
7411   assert((CurContext->isDependentContext() || B.builtAll()) &&
7412          "omp for loop exprs were not built");
7413 
7414   if (!CurContext->isDependentContext()) {
7415     // Finalize the clauses that need pre-built expressions for CodeGen.
7416     for (auto C : Clauses) {
7417       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7418         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7419                                      B.NumIterations, *this, CurScope,
7420                                      DSAStack))
7421           return StmtError();
7422     }
7423   }
7424 
7425   if (checkSimdlenSafelenSpecified(*this, Clauses))
7426     return StmtError();
7427 
7428   setFunctionHasBranchProtectedScope();
7429 
7430   DSAStack->setParentTeamsRegionLoc(StartLoc);
7431 
7432   return OMPTeamsDistributeParallelForSimdDirective::Create(
7433       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7434 }
7435 
7436 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7437     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7438     SourceLocation EndLoc,
7439     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7440   if (!AStmt)
7441     return StmtError();
7442 
7443   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7444   // 1.2.2 OpenMP Language Terminology
7445   // Structured block - An executable statement with a single entry at the
7446   // top and a single exit at the bottom.
7447   // The point of exit cannot be a branch out of the structured block.
7448   // longjmp() and throw() must not violate the entry/exit criteria.
7449   CS->getCapturedDecl()->setNothrow();
7450 
7451   for (int ThisCaptureLevel =
7452            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7453        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7454     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7455     // 1.2.2 OpenMP Language Terminology
7456     // Structured block - An executable statement with a single entry at the
7457     // top and a single exit at the bottom.
7458     // The point of exit cannot be a branch out of the structured block.
7459     // longjmp() and throw() must not violate the entry/exit criteria.
7460     CS->getCapturedDecl()->setNothrow();
7461   }
7462 
7463   OMPLoopDirective::HelperExprs B;
7464   // In presence of clause 'collapse' with number of loops, it will
7465   // define the nested loops number.
7466   unsigned NestedLoopCount = CheckOpenMPLoop(
7467       OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7468       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7469       VarsWithImplicitDSA, B);
7470 
7471   if (NestedLoopCount == 0)
7472     return StmtError();
7473 
7474   assert((CurContext->isDependentContext() || B.builtAll()) &&
7475          "omp for loop exprs were not built");
7476 
7477   setFunctionHasBranchProtectedScope();
7478 
7479   DSAStack->setParentTeamsRegionLoc(StartLoc);
7480 
7481   return OMPTeamsDistributeParallelForDirective::Create(
7482       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7483       DSAStack->isCancelRegion());
7484 }
7485 
7486 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7487                                                  Stmt *AStmt,
7488                                                  SourceLocation StartLoc,
7489                                                  SourceLocation EndLoc) {
7490   if (!AStmt)
7491     return StmtError();
7492 
7493   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7494   // 1.2.2 OpenMP Language Terminology
7495   // Structured block - An executable statement with a single entry at the
7496   // top and a single exit at the bottom.
7497   // The point of exit cannot be a branch out of the structured block.
7498   // longjmp() and throw() must not violate the entry/exit criteria.
7499   CS->getCapturedDecl()->setNothrow();
7500 
7501   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7502        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7503     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7504     // 1.2.2 OpenMP Language Terminology
7505     // Structured block - An executable statement with a single entry at the
7506     // top and a single exit at the bottom.
7507     // The point of exit cannot be a branch out of the structured block.
7508     // longjmp() and throw() must not violate the entry/exit criteria.
7509     CS->getCapturedDecl()->setNothrow();
7510   }
7511   setFunctionHasBranchProtectedScope();
7512 
7513   return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7514                                          AStmt);
7515 }
7516 
7517 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7518     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7519     SourceLocation EndLoc,
7520     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7521   if (!AStmt)
7522     return StmtError();
7523 
7524   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7525   // 1.2.2 OpenMP Language Terminology
7526   // Structured block - An executable statement with a single entry at the
7527   // top and a single exit at the bottom.
7528   // The point of exit cannot be a branch out of the structured block.
7529   // longjmp() and throw() must not violate the entry/exit criteria.
7530   CS->getCapturedDecl()->setNothrow();
7531   for (int ThisCaptureLevel =
7532            getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7533        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7534     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7535     // 1.2.2 OpenMP Language Terminology
7536     // Structured block - An executable statement with a single entry at the
7537     // top and a single exit at the bottom.
7538     // The point of exit cannot be a branch out of the structured block.
7539     // longjmp() and throw() must not violate the entry/exit criteria.
7540     CS->getCapturedDecl()->setNothrow();
7541   }
7542 
7543   OMPLoopDirective::HelperExprs B;
7544   // In presence of clause 'collapse' with number of loops, it will
7545   // define the nested loops number.
7546   auto NestedLoopCount = CheckOpenMPLoop(
7547       OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7548       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7549       VarsWithImplicitDSA, B);
7550   if (NestedLoopCount == 0)
7551     return StmtError();
7552 
7553   assert((CurContext->isDependentContext() || B.builtAll()) &&
7554          "omp target teams distribute loop exprs were not built");
7555 
7556   setFunctionHasBranchProtectedScope();
7557   return OMPTargetTeamsDistributeDirective::Create(
7558       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7559 }
7560 
7561 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7562     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7563     SourceLocation EndLoc,
7564     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7565   if (!AStmt)
7566     return StmtError();
7567 
7568   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7569   // 1.2.2 OpenMP Language Terminology
7570   // Structured block - An executable statement with a single entry at the
7571   // top and a single exit at the bottom.
7572   // The point of exit cannot be a branch out of the structured block.
7573   // longjmp() and throw() must not violate the entry/exit criteria.
7574   CS->getCapturedDecl()->setNothrow();
7575   for (int ThisCaptureLevel =
7576            getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7577        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7578     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7579     // 1.2.2 OpenMP Language Terminology
7580     // Structured block - An executable statement with a single entry at the
7581     // top and a single exit at the bottom.
7582     // The point of exit cannot be a branch out of the structured block.
7583     // longjmp() and throw() must not violate the entry/exit criteria.
7584     CS->getCapturedDecl()->setNothrow();
7585   }
7586 
7587   OMPLoopDirective::HelperExprs B;
7588   // In presence of clause 'collapse' with number of loops, it will
7589   // define the nested loops number.
7590   auto NestedLoopCount = CheckOpenMPLoop(
7591       OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7592       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7593       VarsWithImplicitDSA, B);
7594   if (NestedLoopCount == 0)
7595     return StmtError();
7596 
7597   assert((CurContext->isDependentContext() || B.builtAll()) &&
7598          "omp target teams distribute parallel for loop exprs were not built");
7599 
7600   if (!CurContext->isDependentContext()) {
7601     // Finalize the clauses that need pre-built expressions for CodeGen.
7602     for (auto C : Clauses) {
7603       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7604         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7605                                      B.NumIterations, *this, CurScope,
7606                                      DSAStack))
7607           return StmtError();
7608     }
7609   }
7610 
7611   setFunctionHasBranchProtectedScope();
7612   return OMPTargetTeamsDistributeParallelForDirective::Create(
7613       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7614       DSAStack->isCancelRegion());
7615 }
7616 
7617 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
7618     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7619     SourceLocation EndLoc,
7620     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7621   if (!AStmt)
7622     return StmtError();
7623 
7624   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7625   // 1.2.2 OpenMP Language Terminology
7626   // Structured block - An executable statement with a single entry at the
7627   // top and a single exit at the bottom.
7628   // The point of exit cannot be a branch out of the structured block.
7629   // longjmp() and throw() must not violate the entry/exit criteria.
7630   CS->getCapturedDecl()->setNothrow();
7631   for (int ThisCaptureLevel = getOpenMPCaptureLevels(
7632            OMPD_target_teams_distribute_parallel_for_simd);
7633        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7634     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7635     // 1.2.2 OpenMP Language Terminology
7636     // Structured block - An executable statement with a single entry at the
7637     // top and a single exit at the bottom.
7638     // The point of exit cannot be a branch out of the structured block.
7639     // longjmp() and throw() must not violate the entry/exit criteria.
7640     CS->getCapturedDecl()->setNothrow();
7641   }
7642 
7643   OMPLoopDirective::HelperExprs B;
7644   // In presence of clause 'collapse' with number of loops, it will
7645   // define the nested loops number.
7646   auto NestedLoopCount =
7647       CheckOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
7648                       getCollapseNumberExpr(Clauses),
7649                       nullptr /*ordered not a clause on distribute*/, CS, *this,
7650                       *DSAStack, VarsWithImplicitDSA, B);
7651   if (NestedLoopCount == 0)
7652     return StmtError();
7653 
7654   assert((CurContext->isDependentContext() || B.builtAll()) &&
7655          "omp target teams distribute parallel for simd loop exprs were not "
7656          "built");
7657 
7658   if (!CurContext->isDependentContext()) {
7659     // Finalize the clauses that need pre-built expressions for CodeGen.
7660     for (auto C : Clauses) {
7661       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7662         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7663                                      B.NumIterations, *this, CurScope,
7664                                      DSAStack))
7665           return StmtError();
7666     }
7667   }
7668 
7669   if (checkSimdlenSafelenSpecified(*this, Clauses))
7670     return StmtError();
7671 
7672   setFunctionHasBranchProtectedScope();
7673   return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
7674       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7675 }
7676 
7677 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
7678     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7679     SourceLocation EndLoc,
7680     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7681   if (!AStmt)
7682     return StmtError();
7683 
7684   auto *CS = cast<CapturedStmt>(AStmt);
7685   // 1.2.2 OpenMP Language Terminology
7686   // Structured block - An executable statement with a single entry at the
7687   // top and a single exit at the bottom.
7688   // The point of exit cannot be a branch out of the structured block.
7689   // longjmp() and throw() must not violate the entry/exit criteria.
7690   CS->getCapturedDecl()->setNothrow();
7691   for (int ThisCaptureLevel =
7692            getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
7693        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7694     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7695     // 1.2.2 OpenMP Language Terminology
7696     // Structured block - An executable statement with a single entry at the
7697     // top and a single exit at the bottom.
7698     // The point of exit cannot be a branch out of the structured block.
7699     // longjmp() and throw() must not violate the entry/exit criteria.
7700     CS->getCapturedDecl()->setNothrow();
7701   }
7702 
7703   OMPLoopDirective::HelperExprs B;
7704   // In presence of clause 'collapse' with number of loops, it will
7705   // define the nested loops number.
7706   auto NestedLoopCount = CheckOpenMPLoop(
7707       OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
7708       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7709       VarsWithImplicitDSA, B);
7710   if (NestedLoopCount == 0)
7711     return StmtError();
7712 
7713   assert((CurContext->isDependentContext() || B.builtAll()) &&
7714          "omp target teams distribute simd loop exprs were not built");
7715 
7716   if (!CurContext->isDependentContext()) {
7717     // Finalize the clauses that need pre-built expressions for CodeGen.
7718     for (auto C : Clauses) {
7719       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7720         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7721                                      B.NumIterations, *this, CurScope,
7722                                      DSAStack))
7723           return StmtError();
7724     }
7725   }
7726 
7727   if (checkSimdlenSafelenSpecified(*this, Clauses))
7728     return StmtError();
7729 
7730   setFunctionHasBranchProtectedScope();
7731   return OMPTargetTeamsDistributeSimdDirective::Create(
7732       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7733 }
7734 
7735 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
7736                                              SourceLocation StartLoc,
7737                                              SourceLocation LParenLoc,
7738                                              SourceLocation EndLoc) {
7739   OMPClause *Res = nullptr;
7740   switch (Kind) {
7741   case OMPC_final:
7742     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7743     break;
7744   case OMPC_num_threads:
7745     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7746     break;
7747   case OMPC_safelen:
7748     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7749     break;
7750   case OMPC_simdlen:
7751     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7752     break;
7753   case OMPC_collapse:
7754     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7755     break;
7756   case OMPC_ordered:
7757     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7758     break;
7759   case OMPC_device:
7760     Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7761     break;
7762   case OMPC_num_teams:
7763     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7764     break;
7765   case OMPC_thread_limit:
7766     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7767     break;
7768   case OMPC_priority:
7769     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7770     break;
7771   case OMPC_grainsize:
7772     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7773     break;
7774   case OMPC_num_tasks:
7775     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7776     break;
7777   case OMPC_hint:
7778     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7779     break;
7780   case OMPC_if:
7781   case OMPC_default:
7782   case OMPC_proc_bind:
7783   case OMPC_schedule:
7784   case OMPC_private:
7785   case OMPC_firstprivate:
7786   case OMPC_lastprivate:
7787   case OMPC_shared:
7788   case OMPC_reduction:
7789   case OMPC_task_reduction:
7790   case OMPC_in_reduction:
7791   case OMPC_linear:
7792   case OMPC_aligned:
7793   case OMPC_copyin:
7794   case OMPC_copyprivate:
7795   case OMPC_nowait:
7796   case OMPC_untied:
7797   case OMPC_mergeable:
7798   case OMPC_threadprivate:
7799   case OMPC_flush:
7800   case OMPC_read:
7801   case OMPC_write:
7802   case OMPC_update:
7803   case OMPC_capture:
7804   case OMPC_seq_cst:
7805   case OMPC_depend:
7806   case OMPC_threads:
7807   case OMPC_simd:
7808   case OMPC_map:
7809   case OMPC_nogroup:
7810   case OMPC_dist_schedule:
7811   case OMPC_defaultmap:
7812   case OMPC_unknown:
7813   case OMPC_uniform:
7814   case OMPC_to:
7815   case OMPC_from:
7816   case OMPC_use_device_ptr:
7817   case OMPC_is_device_ptr:
7818     llvm_unreachable("Clause is not allowed.");
7819   }
7820   return Res;
7821 }
7822 
7823 // An OpenMP directive such as 'target parallel' has two captured regions:
7824 // for the 'target' and 'parallel' respectively.  This function returns
7825 // the region in which to capture expressions associated with a clause.
7826 // A return value of OMPD_unknown signifies that the expression should not
7827 // be captured.
7828 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
7829     OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
7830     OpenMPDirectiveKind NameModifier = OMPD_unknown) {
7831   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
7832   switch (CKind) {
7833   case OMPC_if:
7834     switch (DKind) {
7835     case OMPD_target_parallel:
7836     case OMPD_target_parallel_for:
7837     case OMPD_target_parallel_for_simd:
7838       // If this clause applies to the nested 'parallel' region, capture within
7839       // the 'target' region, otherwise do not capture.
7840       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
7841         CaptureRegion = OMPD_target;
7842       break;
7843     case OMPD_target_teams_distribute_parallel_for:
7844     case OMPD_target_teams_distribute_parallel_for_simd:
7845       // If this clause applies to the nested 'parallel' region, capture within
7846       // the 'teams' region, otherwise do not capture.
7847       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
7848         CaptureRegion = OMPD_teams;
7849       break;
7850     case OMPD_teams_distribute_parallel_for:
7851     case OMPD_teams_distribute_parallel_for_simd:
7852       CaptureRegion = OMPD_teams;
7853       break;
7854     case OMPD_target_update:
7855     case OMPD_target_enter_data:
7856     case OMPD_target_exit_data:
7857       CaptureRegion = OMPD_task;
7858       break;
7859     case OMPD_cancel:
7860     case OMPD_parallel:
7861     case OMPD_parallel_sections:
7862     case OMPD_parallel_for:
7863     case OMPD_parallel_for_simd:
7864     case OMPD_target:
7865     case OMPD_target_simd:
7866     case OMPD_target_teams:
7867     case OMPD_target_teams_distribute:
7868     case OMPD_target_teams_distribute_simd:
7869     case OMPD_distribute_parallel_for:
7870     case OMPD_distribute_parallel_for_simd:
7871     case OMPD_task:
7872     case OMPD_taskloop:
7873     case OMPD_taskloop_simd:
7874     case OMPD_target_data:
7875       // Do not capture if-clause expressions.
7876       break;
7877     case OMPD_threadprivate:
7878     case OMPD_taskyield:
7879     case OMPD_barrier:
7880     case OMPD_taskwait:
7881     case OMPD_cancellation_point:
7882     case OMPD_flush:
7883     case OMPD_declare_reduction:
7884     case OMPD_declare_simd:
7885     case OMPD_declare_target:
7886     case OMPD_end_declare_target:
7887     case OMPD_teams:
7888     case OMPD_simd:
7889     case OMPD_for:
7890     case OMPD_for_simd:
7891     case OMPD_sections:
7892     case OMPD_section:
7893     case OMPD_single:
7894     case OMPD_master:
7895     case OMPD_critical:
7896     case OMPD_taskgroup:
7897     case OMPD_distribute:
7898     case OMPD_ordered:
7899     case OMPD_atomic:
7900     case OMPD_distribute_simd:
7901     case OMPD_teams_distribute:
7902     case OMPD_teams_distribute_simd:
7903       llvm_unreachable("Unexpected OpenMP directive with if-clause");
7904     case OMPD_unknown:
7905       llvm_unreachable("Unknown OpenMP directive");
7906     }
7907     break;
7908   case OMPC_num_threads:
7909     switch (DKind) {
7910     case OMPD_target_parallel:
7911     case OMPD_target_parallel_for:
7912     case OMPD_target_parallel_for_simd:
7913       CaptureRegion = OMPD_target;
7914       break;
7915     case OMPD_teams_distribute_parallel_for:
7916     case OMPD_teams_distribute_parallel_for_simd:
7917     case OMPD_target_teams_distribute_parallel_for:
7918     case OMPD_target_teams_distribute_parallel_for_simd:
7919       CaptureRegion = OMPD_teams;
7920       break;
7921     case OMPD_parallel:
7922     case OMPD_parallel_sections:
7923     case OMPD_parallel_for:
7924     case OMPD_parallel_for_simd:
7925     case OMPD_distribute_parallel_for:
7926     case OMPD_distribute_parallel_for_simd:
7927       // Do not capture num_threads-clause expressions.
7928       break;
7929     case OMPD_target_data:
7930     case OMPD_target_enter_data:
7931     case OMPD_target_exit_data:
7932     case OMPD_target_update:
7933     case OMPD_target:
7934     case OMPD_target_simd:
7935     case OMPD_target_teams:
7936     case OMPD_target_teams_distribute:
7937     case OMPD_target_teams_distribute_simd:
7938     case OMPD_cancel:
7939     case OMPD_task:
7940     case OMPD_taskloop:
7941     case OMPD_taskloop_simd:
7942     case OMPD_threadprivate:
7943     case OMPD_taskyield:
7944     case OMPD_barrier:
7945     case OMPD_taskwait:
7946     case OMPD_cancellation_point:
7947     case OMPD_flush:
7948     case OMPD_declare_reduction:
7949     case OMPD_declare_simd:
7950     case OMPD_declare_target:
7951     case OMPD_end_declare_target:
7952     case OMPD_teams:
7953     case OMPD_simd:
7954     case OMPD_for:
7955     case OMPD_for_simd:
7956     case OMPD_sections:
7957     case OMPD_section:
7958     case OMPD_single:
7959     case OMPD_master:
7960     case OMPD_critical:
7961     case OMPD_taskgroup:
7962     case OMPD_distribute:
7963     case OMPD_ordered:
7964     case OMPD_atomic:
7965     case OMPD_distribute_simd:
7966     case OMPD_teams_distribute:
7967     case OMPD_teams_distribute_simd:
7968       llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
7969     case OMPD_unknown:
7970       llvm_unreachable("Unknown OpenMP directive");
7971     }
7972     break;
7973   case OMPC_num_teams:
7974     switch (DKind) {
7975     case OMPD_target_teams:
7976     case OMPD_target_teams_distribute:
7977     case OMPD_target_teams_distribute_simd:
7978     case OMPD_target_teams_distribute_parallel_for:
7979     case OMPD_target_teams_distribute_parallel_for_simd:
7980       CaptureRegion = OMPD_target;
7981       break;
7982     case OMPD_teams_distribute_parallel_for:
7983     case OMPD_teams_distribute_parallel_for_simd:
7984     case OMPD_teams:
7985     case OMPD_teams_distribute:
7986     case OMPD_teams_distribute_simd:
7987       // Do not capture num_teams-clause expressions.
7988       break;
7989     case OMPD_distribute_parallel_for:
7990     case OMPD_distribute_parallel_for_simd:
7991     case OMPD_task:
7992     case OMPD_taskloop:
7993     case OMPD_taskloop_simd:
7994     case OMPD_target_data:
7995     case OMPD_target_enter_data:
7996     case OMPD_target_exit_data:
7997     case OMPD_target_update:
7998     case OMPD_cancel:
7999     case OMPD_parallel:
8000     case OMPD_parallel_sections:
8001     case OMPD_parallel_for:
8002     case OMPD_parallel_for_simd:
8003     case OMPD_target:
8004     case OMPD_target_simd:
8005     case OMPD_target_parallel:
8006     case OMPD_target_parallel_for:
8007     case OMPD_target_parallel_for_simd:
8008     case OMPD_threadprivate:
8009     case OMPD_taskyield:
8010     case OMPD_barrier:
8011     case OMPD_taskwait:
8012     case OMPD_cancellation_point:
8013     case OMPD_flush:
8014     case OMPD_declare_reduction:
8015     case OMPD_declare_simd:
8016     case OMPD_declare_target:
8017     case OMPD_end_declare_target:
8018     case OMPD_simd:
8019     case OMPD_for:
8020     case OMPD_for_simd:
8021     case OMPD_sections:
8022     case OMPD_section:
8023     case OMPD_single:
8024     case OMPD_master:
8025     case OMPD_critical:
8026     case OMPD_taskgroup:
8027     case OMPD_distribute:
8028     case OMPD_ordered:
8029     case OMPD_atomic:
8030     case OMPD_distribute_simd:
8031       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8032     case OMPD_unknown:
8033       llvm_unreachable("Unknown OpenMP directive");
8034     }
8035     break;
8036   case OMPC_thread_limit:
8037     switch (DKind) {
8038     case OMPD_target_teams:
8039     case OMPD_target_teams_distribute:
8040     case OMPD_target_teams_distribute_simd:
8041     case OMPD_target_teams_distribute_parallel_for:
8042     case OMPD_target_teams_distribute_parallel_for_simd:
8043       CaptureRegion = OMPD_target;
8044       break;
8045     case OMPD_teams_distribute_parallel_for:
8046     case OMPD_teams_distribute_parallel_for_simd:
8047     case OMPD_teams:
8048     case OMPD_teams_distribute:
8049     case OMPD_teams_distribute_simd:
8050       // Do not capture thread_limit-clause expressions.
8051       break;
8052     case OMPD_distribute_parallel_for:
8053     case OMPD_distribute_parallel_for_simd:
8054     case OMPD_task:
8055     case OMPD_taskloop:
8056     case OMPD_taskloop_simd:
8057     case OMPD_target_data:
8058     case OMPD_target_enter_data:
8059     case OMPD_target_exit_data:
8060     case OMPD_target_update:
8061     case OMPD_cancel:
8062     case OMPD_parallel:
8063     case OMPD_parallel_sections:
8064     case OMPD_parallel_for:
8065     case OMPD_parallel_for_simd:
8066     case OMPD_target:
8067     case OMPD_target_simd:
8068     case OMPD_target_parallel:
8069     case OMPD_target_parallel_for:
8070     case OMPD_target_parallel_for_simd:
8071     case OMPD_threadprivate:
8072     case OMPD_taskyield:
8073     case OMPD_barrier:
8074     case OMPD_taskwait:
8075     case OMPD_cancellation_point:
8076     case OMPD_flush:
8077     case OMPD_declare_reduction:
8078     case OMPD_declare_simd:
8079     case OMPD_declare_target:
8080     case OMPD_end_declare_target:
8081     case OMPD_simd:
8082     case OMPD_for:
8083     case OMPD_for_simd:
8084     case OMPD_sections:
8085     case OMPD_section:
8086     case OMPD_single:
8087     case OMPD_master:
8088     case OMPD_critical:
8089     case OMPD_taskgroup:
8090     case OMPD_distribute:
8091     case OMPD_ordered:
8092     case OMPD_atomic:
8093     case OMPD_distribute_simd:
8094       llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8095     case OMPD_unknown:
8096       llvm_unreachable("Unknown OpenMP directive");
8097     }
8098     break;
8099   case OMPC_schedule:
8100     switch (DKind) {
8101     case OMPD_parallel_for:
8102     case OMPD_parallel_for_simd:
8103     case OMPD_distribute_parallel_for:
8104     case OMPD_distribute_parallel_for_simd:
8105     case OMPD_teams_distribute_parallel_for:
8106     case OMPD_teams_distribute_parallel_for_simd:
8107     case OMPD_target_parallel_for:
8108     case OMPD_target_parallel_for_simd:
8109     case OMPD_target_teams_distribute_parallel_for:
8110     case OMPD_target_teams_distribute_parallel_for_simd:
8111       CaptureRegion = OMPD_parallel;
8112       break;
8113     case OMPD_for:
8114     case OMPD_for_simd:
8115       // Do not capture schedule-clause expressions.
8116       break;
8117     case OMPD_task:
8118     case OMPD_taskloop:
8119     case OMPD_taskloop_simd:
8120     case OMPD_target_data:
8121     case OMPD_target_enter_data:
8122     case OMPD_target_exit_data:
8123     case OMPD_target_update:
8124     case OMPD_teams:
8125     case OMPD_teams_distribute:
8126     case OMPD_teams_distribute_simd:
8127     case OMPD_target_teams_distribute:
8128     case OMPD_target_teams_distribute_simd:
8129     case OMPD_target:
8130     case OMPD_target_simd:
8131     case OMPD_target_parallel:
8132     case OMPD_cancel:
8133     case OMPD_parallel:
8134     case OMPD_parallel_sections:
8135     case OMPD_threadprivate:
8136     case OMPD_taskyield:
8137     case OMPD_barrier:
8138     case OMPD_taskwait:
8139     case OMPD_cancellation_point:
8140     case OMPD_flush:
8141     case OMPD_declare_reduction:
8142     case OMPD_declare_simd:
8143     case OMPD_declare_target:
8144     case OMPD_end_declare_target:
8145     case OMPD_simd:
8146     case OMPD_sections:
8147     case OMPD_section:
8148     case OMPD_single:
8149     case OMPD_master:
8150     case OMPD_critical:
8151     case OMPD_taskgroup:
8152     case OMPD_distribute:
8153     case OMPD_ordered:
8154     case OMPD_atomic:
8155     case OMPD_distribute_simd:
8156     case OMPD_target_teams:
8157       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8158     case OMPD_unknown:
8159       llvm_unreachable("Unknown OpenMP directive");
8160     }
8161     break;
8162   case OMPC_dist_schedule:
8163     switch (DKind) {
8164     case OMPD_teams_distribute_parallel_for:
8165     case OMPD_teams_distribute_parallel_for_simd:
8166     case OMPD_teams_distribute:
8167     case OMPD_teams_distribute_simd:
8168     case OMPD_target_teams_distribute_parallel_for:
8169     case OMPD_target_teams_distribute_parallel_for_simd:
8170     case OMPD_target_teams_distribute:
8171     case OMPD_target_teams_distribute_simd:
8172       CaptureRegion = OMPD_teams;
8173       break;
8174     case OMPD_distribute_parallel_for:
8175     case OMPD_distribute_parallel_for_simd:
8176     case OMPD_distribute:
8177     case OMPD_distribute_simd:
8178       // Do not capture thread_limit-clause expressions.
8179       break;
8180     case OMPD_parallel_for:
8181     case OMPD_parallel_for_simd:
8182     case OMPD_target_parallel_for_simd:
8183     case OMPD_target_parallel_for:
8184     case OMPD_task:
8185     case OMPD_taskloop:
8186     case OMPD_taskloop_simd:
8187     case OMPD_target_data:
8188     case OMPD_target_enter_data:
8189     case OMPD_target_exit_data:
8190     case OMPD_target_update:
8191     case OMPD_teams:
8192     case OMPD_target:
8193     case OMPD_target_simd:
8194     case OMPD_target_parallel:
8195     case OMPD_cancel:
8196     case OMPD_parallel:
8197     case OMPD_parallel_sections:
8198     case OMPD_threadprivate:
8199     case OMPD_taskyield:
8200     case OMPD_barrier:
8201     case OMPD_taskwait:
8202     case OMPD_cancellation_point:
8203     case OMPD_flush:
8204     case OMPD_declare_reduction:
8205     case OMPD_declare_simd:
8206     case OMPD_declare_target:
8207     case OMPD_end_declare_target:
8208     case OMPD_simd:
8209     case OMPD_for:
8210     case OMPD_for_simd:
8211     case OMPD_sections:
8212     case OMPD_section:
8213     case OMPD_single:
8214     case OMPD_master:
8215     case OMPD_critical:
8216     case OMPD_taskgroup:
8217     case OMPD_ordered:
8218     case OMPD_atomic:
8219     case OMPD_target_teams:
8220       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8221     case OMPD_unknown:
8222       llvm_unreachable("Unknown OpenMP directive");
8223     }
8224     break;
8225   case OMPC_device:
8226     switch (DKind) {
8227     case OMPD_target_update:
8228     case OMPD_target_enter_data:
8229     case OMPD_target_exit_data:
8230     case OMPD_target:
8231     case OMPD_target_simd:
8232     case OMPD_target_teams:
8233     case OMPD_target_parallel:
8234     case OMPD_target_teams_distribute:
8235     case OMPD_target_teams_distribute_simd:
8236     case OMPD_target_parallel_for:
8237     case OMPD_target_parallel_for_simd:
8238     case OMPD_target_teams_distribute_parallel_for:
8239     case OMPD_target_teams_distribute_parallel_for_simd:
8240       CaptureRegion = OMPD_task;
8241       break;
8242     case OMPD_target_data:
8243       // Do not capture device-clause expressions.
8244       break;
8245     case OMPD_teams_distribute_parallel_for:
8246     case OMPD_teams_distribute_parallel_for_simd:
8247     case OMPD_teams:
8248     case OMPD_teams_distribute:
8249     case OMPD_teams_distribute_simd:
8250     case OMPD_distribute_parallel_for:
8251     case OMPD_distribute_parallel_for_simd:
8252     case OMPD_task:
8253     case OMPD_taskloop:
8254     case OMPD_taskloop_simd:
8255     case OMPD_cancel:
8256     case OMPD_parallel:
8257     case OMPD_parallel_sections:
8258     case OMPD_parallel_for:
8259     case OMPD_parallel_for_simd:
8260     case OMPD_threadprivate:
8261     case OMPD_taskyield:
8262     case OMPD_barrier:
8263     case OMPD_taskwait:
8264     case OMPD_cancellation_point:
8265     case OMPD_flush:
8266     case OMPD_declare_reduction:
8267     case OMPD_declare_simd:
8268     case OMPD_declare_target:
8269     case OMPD_end_declare_target:
8270     case OMPD_simd:
8271     case OMPD_for:
8272     case OMPD_for_simd:
8273     case OMPD_sections:
8274     case OMPD_section:
8275     case OMPD_single:
8276     case OMPD_master:
8277     case OMPD_critical:
8278     case OMPD_taskgroup:
8279     case OMPD_distribute:
8280     case OMPD_ordered:
8281     case OMPD_atomic:
8282     case OMPD_distribute_simd:
8283       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8284     case OMPD_unknown:
8285       llvm_unreachable("Unknown OpenMP directive");
8286     }
8287     break;
8288   case OMPC_firstprivate:
8289   case OMPC_lastprivate:
8290   case OMPC_reduction:
8291   case OMPC_task_reduction:
8292   case OMPC_in_reduction:
8293   case OMPC_linear:
8294   case OMPC_default:
8295   case OMPC_proc_bind:
8296   case OMPC_final:
8297   case OMPC_safelen:
8298   case OMPC_simdlen:
8299   case OMPC_collapse:
8300   case OMPC_private:
8301   case OMPC_shared:
8302   case OMPC_aligned:
8303   case OMPC_copyin:
8304   case OMPC_copyprivate:
8305   case OMPC_ordered:
8306   case OMPC_nowait:
8307   case OMPC_untied:
8308   case OMPC_mergeable:
8309   case OMPC_threadprivate:
8310   case OMPC_flush:
8311   case OMPC_read:
8312   case OMPC_write:
8313   case OMPC_update:
8314   case OMPC_capture:
8315   case OMPC_seq_cst:
8316   case OMPC_depend:
8317   case OMPC_threads:
8318   case OMPC_simd:
8319   case OMPC_map:
8320   case OMPC_priority:
8321   case OMPC_grainsize:
8322   case OMPC_nogroup:
8323   case OMPC_num_tasks:
8324   case OMPC_hint:
8325   case OMPC_defaultmap:
8326   case OMPC_unknown:
8327   case OMPC_uniform:
8328   case OMPC_to:
8329   case OMPC_from:
8330   case OMPC_use_device_ptr:
8331   case OMPC_is_device_ptr:
8332     llvm_unreachable("Unexpected OpenMP clause.");
8333   }
8334   return CaptureRegion;
8335 }
8336 
8337 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8338                                      Expr *Condition, SourceLocation StartLoc,
8339                                      SourceLocation LParenLoc,
8340                                      SourceLocation NameModifierLoc,
8341                                      SourceLocation ColonLoc,
8342                                      SourceLocation EndLoc) {
8343   Expr *ValExpr = Condition;
8344   Stmt *HelperValStmt = nullptr;
8345   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
8346   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8347       !Condition->isInstantiationDependent() &&
8348       !Condition->containsUnexpandedParameterPack()) {
8349     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
8350     if (Val.isInvalid())
8351       return nullptr;
8352 
8353     ValExpr = Val.get();
8354 
8355     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8356     CaptureRegion =
8357         getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
8358     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
8359       ValExpr = MakeFullExpr(ValExpr).get();
8360       llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8361       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8362       HelperValStmt = buildPreInits(Context, Captures);
8363     }
8364   }
8365 
8366   return new (Context)
8367       OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8368                   LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
8369 }
8370 
8371 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8372                                         SourceLocation StartLoc,
8373                                         SourceLocation LParenLoc,
8374                                         SourceLocation EndLoc) {
8375   Expr *ValExpr = Condition;
8376   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8377       !Condition->isInstantiationDependent() &&
8378       !Condition->containsUnexpandedParameterPack()) {
8379     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
8380     if (Val.isInvalid())
8381       return nullptr;
8382 
8383     ValExpr = MakeFullExpr(Val.get()).get();
8384   }
8385 
8386   return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8387 }
8388 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8389                                                         Expr *Op) {
8390   if (!Op)
8391     return ExprError();
8392 
8393   class IntConvertDiagnoser : public ICEConvertDiagnoser {
8394   public:
8395     IntConvertDiagnoser()
8396         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
8397     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8398                                          QualType T) override {
8399       return S.Diag(Loc, diag::err_omp_not_integral) << T;
8400     }
8401     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8402                                              QualType T) override {
8403       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8404     }
8405     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8406                                                QualType T,
8407                                                QualType ConvTy) override {
8408       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8409     }
8410     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8411                                            QualType ConvTy) override {
8412       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
8413              << ConvTy->isEnumeralType() << ConvTy;
8414     }
8415     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8416                                             QualType T) override {
8417       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8418     }
8419     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8420                                         QualType ConvTy) override {
8421       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
8422              << ConvTy->isEnumeralType() << ConvTy;
8423     }
8424     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8425                                              QualType) override {
8426       llvm_unreachable("conversion functions are permitted");
8427     }
8428   } ConvertDiagnoser;
8429   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8430 }
8431 
8432 static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
8433                                       OpenMPClauseKind CKind,
8434                                       bool StrictlyPositive) {
8435   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8436       !ValExpr->isInstantiationDependent()) {
8437     SourceLocation Loc = ValExpr->getExprLoc();
8438     ExprResult Value =
8439         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8440     if (Value.isInvalid())
8441       return false;
8442 
8443     ValExpr = Value.get();
8444     // The expression must evaluate to a non-negative integer value.
8445     llvm::APSInt Result;
8446     if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
8447         Result.isSigned() &&
8448         !((!StrictlyPositive && Result.isNonNegative()) ||
8449           (StrictlyPositive && Result.isStrictlyPositive()))) {
8450       SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
8451           << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8452           << ValExpr->getSourceRange();
8453       return false;
8454     }
8455   }
8456   return true;
8457 }
8458 
8459 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8460                                              SourceLocation StartLoc,
8461                                              SourceLocation LParenLoc,
8462                                              SourceLocation EndLoc) {
8463   Expr *ValExpr = NumThreads;
8464   Stmt *HelperValStmt = nullptr;
8465 
8466   // OpenMP [2.5, Restrictions]
8467   //  The num_threads expression must evaluate to a positive integer value.
8468   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
8469                                  /*StrictlyPositive=*/true))
8470     return nullptr;
8471 
8472   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8473   OpenMPDirectiveKind CaptureRegion =
8474       getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8475   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
8476     ValExpr = MakeFullExpr(ValExpr).get();
8477     llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8478     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8479     HelperValStmt = buildPreInits(Context, Captures);
8480   }
8481 
8482   return new (Context) OMPNumThreadsClause(
8483       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
8484 }
8485 
8486 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
8487                                                        OpenMPClauseKind CKind,
8488                                                        bool StrictlyPositive) {
8489   if (!E)
8490     return ExprError();
8491   if (E->isValueDependent() || E->isTypeDependent() ||
8492       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
8493     return E;
8494   llvm::APSInt Result;
8495   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8496   if (ICE.isInvalid())
8497     return ExprError();
8498   if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8499       (!StrictlyPositive && !Result.isNonNegative())) {
8500     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
8501         << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8502         << E->getSourceRange();
8503     return ExprError();
8504   }
8505   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8506     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8507         << E->getSourceRange();
8508     return ExprError();
8509   }
8510   if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8511     DSAStack->setAssociatedLoops(Result.getExtValue());
8512   else if (CKind == OMPC_ordered)
8513     DSAStack->setAssociatedLoops(Result.getExtValue());
8514   return ICE;
8515 }
8516 
8517 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8518                                           SourceLocation LParenLoc,
8519                                           SourceLocation EndLoc) {
8520   // OpenMP [2.8.1, simd construct, Description]
8521   // The parameter of the safelen clause must be a constant
8522   // positive integer expression.
8523   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8524   if (Safelen.isInvalid())
8525     return nullptr;
8526   return new (Context)
8527       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
8528 }
8529 
8530 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8531                                           SourceLocation LParenLoc,
8532                                           SourceLocation EndLoc) {
8533   // OpenMP [2.8.1, simd construct, Description]
8534   // The parameter of the simdlen clause must be a constant
8535   // positive integer expression.
8536   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8537   if (Simdlen.isInvalid())
8538     return nullptr;
8539   return new (Context)
8540       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8541 }
8542 
8543 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8544                                            SourceLocation StartLoc,
8545                                            SourceLocation LParenLoc,
8546                                            SourceLocation EndLoc) {
8547   // OpenMP [2.7.1, loop construct, Description]
8548   // OpenMP [2.8.1, simd construct, Description]
8549   // OpenMP [2.9.6, distribute construct, Description]
8550   // The parameter of the collapse clause must be a constant
8551   // positive integer expression.
8552   ExprResult NumForLoopsResult =
8553       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8554   if (NumForLoopsResult.isInvalid())
8555     return nullptr;
8556   return new (Context)
8557       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
8558 }
8559 
8560 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8561                                           SourceLocation EndLoc,
8562                                           SourceLocation LParenLoc,
8563                                           Expr *NumForLoops) {
8564   // OpenMP [2.7.1, loop construct, Description]
8565   // OpenMP [2.8.1, simd construct, Description]
8566   // OpenMP [2.9.6, distribute construct, Description]
8567   // The parameter of the ordered clause must be a constant
8568   // positive integer expression if any.
8569   if (NumForLoops && LParenLoc.isValid()) {
8570     ExprResult NumForLoopsResult =
8571         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8572     if (NumForLoopsResult.isInvalid())
8573       return nullptr;
8574     NumForLoops = NumForLoopsResult.get();
8575   } else
8576     NumForLoops = nullptr;
8577   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
8578   return new (Context)
8579       OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
8580 }
8581 
8582 OMPClause *Sema::ActOnOpenMPSimpleClause(
8583     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8584     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
8585   OMPClause *Res = nullptr;
8586   switch (Kind) {
8587   case OMPC_default:
8588     Res =
8589         ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
8590                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
8591     break;
8592   case OMPC_proc_bind:
8593     Res = ActOnOpenMPProcBindClause(
8594         static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
8595         LParenLoc, EndLoc);
8596     break;
8597   case OMPC_if:
8598   case OMPC_final:
8599   case OMPC_num_threads:
8600   case OMPC_safelen:
8601   case OMPC_simdlen:
8602   case OMPC_collapse:
8603   case OMPC_schedule:
8604   case OMPC_private:
8605   case OMPC_firstprivate:
8606   case OMPC_lastprivate:
8607   case OMPC_shared:
8608   case OMPC_reduction:
8609   case OMPC_task_reduction:
8610   case OMPC_in_reduction:
8611   case OMPC_linear:
8612   case OMPC_aligned:
8613   case OMPC_copyin:
8614   case OMPC_copyprivate:
8615   case OMPC_ordered:
8616   case OMPC_nowait:
8617   case OMPC_untied:
8618   case OMPC_mergeable:
8619   case OMPC_threadprivate:
8620   case OMPC_flush:
8621   case OMPC_read:
8622   case OMPC_write:
8623   case OMPC_update:
8624   case OMPC_capture:
8625   case OMPC_seq_cst:
8626   case OMPC_depend:
8627   case OMPC_device:
8628   case OMPC_threads:
8629   case OMPC_simd:
8630   case OMPC_map:
8631   case OMPC_num_teams:
8632   case OMPC_thread_limit:
8633   case OMPC_priority:
8634   case OMPC_grainsize:
8635   case OMPC_nogroup:
8636   case OMPC_num_tasks:
8637   case OMPC_hint:
8638   case OMPC_dist_schedule:
8639   case OMPC_defaultmap:
8640   case OMPC_unknown:
8641   case OMPC_uniform:
8642   case OMPC_to:
8643   case OMPC_from:
8644   case OMPC_use_device_ptr:
8645   case OMPC_is_device_ptr:
8646     llvm_unreachable("Clause is not allowed.");
8647   }
8648   return Res;
8649 }
8650 
8651 static std::string
8652 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
8653                         ArrayRef<unsigned> Exclude = llvm::None) {
8654   std::string Values;
8655   unsigned Bound = Last >= 2 ? Last - 2 : 0;
8656   unsigned Skipped = Exclude.size();
8657   auto S = Exclude.begin(), E = Exclude.end();
8658   for (unsigned i = First; i < Last; ++i) {
8659     if (std::find(S, E, i) != E) {
8660       --Skipped;
8661       continue;
8662     }
8663     Values += "'";
8664     Values += getOpenMPSimpleClauseTypeName(K, i);
8665     Values += "'";
8666     if (i == Bound - Skipped)
8667       Values += " or ";
8668     else if (i != Bound + 1 - Skipped)
8669       Values += ", ";
8670   }
8671   return Values;
8672 }
8673 
8674 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
8675                                           SourceLocation KindKwLoc,
8676                                           SourceLocation StartLoc,
8677                                           SourceLocation LParenLoc,
8678                                           SourceLocation EndLoc) {
8679   if (Kind == OMPC_DEFAULT_unknown) {
8680     static_assert(OMPC_DEFAULT_unknown > 0,
8681                   "OMPC_DEFAULT_unknown not greater than 0");
8682     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
8683         << getListOfPossibleValues(OMPC_default, /*First=*/0,
8684                                    /*Last=*/OMPC_DEFAULT_unknown)
8685         << getOpenMPClauseName(OMPC_default);
8686     return nullptr;
8687   }
8688   switch (Kind) {
8689   case OMPC_DEFAULT_none:
8690     DSAStack->setDefaultDSANone(KindKwLoc);
8691     break;
8692   case OMPC_DEFAULT_shared:
8693     DSAStack->setDefaultDSAShared(KindKwLoc);
8694     break;
8695   case OMPC_DEFAULT_unknown:
8696     llvm_unreachable("Clause kind is not allowed.");
8697     break;
8698   }
8699   return new (Context)
8700       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
8701 }
8702 
8703 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
8704                                            SourceLocation KindKwLoc,
8705                                            SourceLocation StartLoc,
8706                                            SourceLocation LParenLoc,
8707                                            SourceLocation EndLoc) {
8708   if (Kind == OMPC_PROC_BIND_unknown) {
8709     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
8710         << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
8711                                    /*Last=*/OMPC_PROC_BIND_unknown)
8712         << getOpenMPClauseName(OMPC_proc_bind);
8713     return nullptr;
8714   }
8715   return new (Context)
8716       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
8717 }
8718 
8719 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
8720     OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
8721     SourceLocation StartLoc, SourceLocation LParenLoc,
8722     ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
8723     SourceLocation EndLoc) {
8724   OMPClause *Res = nullptr;
8725   switch (Kind) {
8726   case OMPC_schedule:
8727     enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
8728     assert(Argument.size() == NumberOfElements &&
8729            ArgumentLoc.size() == NumberOfElements);
8730     Res = ActOnOpenMPScheduleClause(
8731         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
8732         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
8733         static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
8734         StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
8735         ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
8736     break;
8737   case OMPC_if:
8738     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
8739     Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
8740                               Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
8741                               DelimLoc, EndLoc);
8742     break;
8743   case OMPC_dist_schedule:
8744     Res = ActOnOpenMPDistScheduleClause(
8745         static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
8746         StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
8747     break;
8748   case OMPC_defaultmap:
8749     enum { Modifier, DefaultmapKind };
8750     Res = ActOnOpenMPDefaultmapClause(
8751         static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
8752         static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
8753         StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
8754         EndLoc);
8755     break;
8756   case OMPC_final:
8757   case OMPC_num_threads:
8758   case OMPC_safelen:
8759   case OMPC_simdlen:
8760   case OMPC_collapse:
8761   case OMPC_default:
8762   case OMPC_proc_bind:
8763   case OMPC_private:
8764   case OMPC_firstprivate:
8765   case OMPC_lastprivate:
8766   case OMPC_shared:
8767   case OMPC_reduction:
8768   case OMPC_task_reduction:
8769   case OMPC_in_reduction:
8770   case OMPC_linear:
8771   case OMPC_aligned:
8772   case OMPC_copyin:
8773   case OMPC_copyprivate:
8774   case OMPC_ordered:
8775   case OMPC_nowait:
8776   case OMPC_untied:
8777   case OMPC_mergeable:
8778   case OMPC_threadprivate:
8779   case OMPC_flush:
8780   case OMPC_read:
8781   case OMPC_write:
8782   case OMPC_update:
8783   case OMPC_capture:
8784   case OMPC_seq_cst:
8785   case OMPC_depend:
8786   case OMPC_device:
8787   case OMPC_threads:
8788   case OMPC_simd:
8789   case OMPC_map:
8790   case OMPC_num_teams:
8791   case OMPC_thread_limit:
8792   case OMPC_priority:
8793   case OMPC_grainsize:
8794   case OMPC_nogroup:
8795   case OMPC_num_tasks:
8796   case OMPC_hint:
8797   case OMPC_unknown:
8798   case OMPC_uniform:
8799   case OMPC_to:
8800   case OMPC_from:
8801   case OMPC_use_device_ptr:
8802   case OMPC_is_device_ptr:
8803     llvm_unreachable("Clause is not allowed.");
8804   }
8805   return Res;
8806 }
8807 
8808 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
8809                                    OpenMPScheduleClauseModifier M2,
8810                                    SourceLocation M1Loc, SourceLocation M2Loc) {
8811   if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
8812     SmallVector<unsigned, 2> Excluded;
8813     if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
8814       Excluded.push_back(M2);
8815     if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
8816       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
8817     if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
8818       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
8819     S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
8820         << getListOfPossibleValues(OMPC_schedule,
8821                                    /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
8822                                    /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8823                                    Excluded)
8824         << getOpenMPClauseName(OMPC_schedule);
8825     return true;
8826   }
8827   return false;
8828 }
8829 
8830 OMPClause *Sema::ActOnOpenMPScheduleClause(
8831     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
8832     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
8833     SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
8834     SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
8835   if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
8836       checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
8837     return nullptr;
8838   // OpenMP, 2.7.1, Loop Construct, Restrictions
8839   // Either the monotonic modifier or the nonmonotonic modifier can be specified
8840   // but not both.
8841   if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
8842       (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
8843        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
8844       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
8845        M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
8846     Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
8847         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
8848         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
8849     return nullptr;
8850   }
8851   if (Kind == OMPC_SCHEDULE_unknown) {
8852     std::string Values;
8853     if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
8854       unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
8855       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8856                                        /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8857                                        Exclude);
8858     } else {
8859       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8860                                        /*Last=*/OMPC_SCHEDULE_unknown);
8861     }
8862     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8863         << Values << getOpenMPClauseName(OMPC_schedule);
8864     return nullptr;
8865   }
8866   // OpenMP, 2.7.1, Loop Construct, Restrictions
8867   // The nonmonotonic modifier can only be specified with schedule(dynamic) or
8868   // schedule(guided).
8869   if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
8870        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
8871       Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
8872     Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
8873          diag::err_omp_schedule_nonmonotonic_static);
8874     return nullptr;
8875   }
8876   Expr *ValExpr = ChunkSize;
8877   Stmt *HelperValStmt = nullptr;
8878   if (ChunkSize) {
8879     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8880         !ChunkSize->isInstantiationDependent() &&
8881         !ChunkSize->containsUnexpandedParameterPack()) {
8882       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8883       ExprResult Val =
8884           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8885       if (Val.isInvalid())
8886         return nullptr;
8887 
8888       ValExpr = Val.get();
8889 
8890       // OpenMP [2.7.1, Restrictions]
8891       //  chunk_size must be a loop invariant integer expression with a positive
8892       //  value.
8893       llvm::APSInt Result;
8894       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8895         if (Result.isSigned() && !Result.isStrictlyPositive()) {
8896           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
8897               << "schedule" << 1 << ChunkSize->getSourceRange();
8898           return nullptr;
8899         }
8900       } else if (getOpenMPCaptureRegionForClause(
8901                      DSAStack->getCurrentDirective(), OMPC_schedule) !=
8902                      OMPD_unknown &&
8903                  !CurContext->isDependentContext()) {
8904         ValExpr = MakeFullExpr(ValExpr).get();
8905         llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8906         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8907         HelperValStmt = buildPreInits(Context, Captures);
8908       }
8909     }
8910   }
8911 
8912   return new (Context)
8913       OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
8914                         ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
8915 }
8916 
8917 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
8918                                    SourceLocation StartLoc,
8919                                    SourceLocation EndLoc) {
8920   OMPClause *Res = nullptr;
8921   switch (Kind) {
8922   case OMPC_ordered:
8923     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
8924     break;
8925   case OMPC_nowait:
8926     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
8927     break;
8928   case OMPC_untied:
8929     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
8930     break;
8931   case OMPC_mergeable:
8932     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
8933     break;
8934   case OMPC_read:
8935     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
8936     break;
8937   case OMPC_write:
8938     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
8939     break;
8940   case OMPC_update:
8941     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
8942     break;
8943   case OMPC_capture:
8944     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
8945     break;
8946   case OMPC_seq_cst:
8947     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
8948     break;
8949   case OMPC_threads:
8950     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
8951     break;
8952   case OMPC_simd:
8953     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
8954     break;
8955   case OMPC_nogroup:
8956     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
8957     break;
8958   case OMPC_if:
8959   case OMPC_final:
8960   case OMPC_num_threads:
8961   case OMPC_safelen:
8962   case OMPC_simdlen:
8963   case OMPC_collapse:
8964   case OMPC_schedule:
8965   case OMPC_private:
8966   case OMPC_firstprivate:
8967   case OMPC_lastprivate:
8968   case OMPC_shared:
8969   case OMPC_reduction:
8970   case OMPC_task_reduction:
8971   case OMPC_in_reduction:
8972   case OMPC_linear:
8973   case OMPC_aligned:
8974   case OMPC_copyin:
8975   case OMPC_copyprivate:
8976   case OMPC_default:
8977   case OMPC_proc_bind:
8978   case OMPC_threadprivate:
8979   case OMPC_flush:
8980   case OMPC_depend:
8981   case OMPC_device:
8982   case OMPC_map:
8983   case OMPC_num_teams:
8984   case OMPC_thread_limit:
8985   case OMPC_priority:
8986   case OMPC_grainsize:
8987   case OMPC_num_tasks:
8988   case OMPC_hint:
8989   case OMPC_dist_schedule:
8990   case OMPC_defaultmap:
8991   case OMPC_unknown:
8992   case OMPC_uniform:
8993   case OMPC_to:
8994   case OMPC_from:
8995   case OMPC_use_device_ptr:
8996   case OMPC_is_device_ptr:
8997     llvm_unreachable("Clause is not allowed.");
8998   }
8999   return Res;
9000 }
9001 
9002 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9003                                          SourceLocation EndLoc) {
9004   DSAStack->setNowaitRegion();
9005   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9006 }
9007 
9008 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9009                                          SourceLocation EndLoc) {
9010   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9011 }
9012 
9013 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9014                                             SourceLocation EndLoc) {
9015   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9016 }
9017 
9018 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9019                                        SourceLocation EndLoc) {
9020   return new (Context) OMPReadClause(StartLoc, EndLoc);
9021 }
9022 
9023 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9024                                         SourceLocation EndLoc) {
9025   return new (Context) OMPWriteClause(StartLoc, EndLoc);
9026 }
9027 
9028 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9029                                          SourceLocation EndLoc) {
9030   return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9031 }
9032 
9033 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9034                                           SourceLocation EndLoc) {
9035   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9036 }
9037 
9038 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9039                                          SourceLocation EndLoc) {
9040   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9041 }
9042 
9043 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9044                                           SourceLocation EndLoc) {
9045   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9046 }
9047 
9048 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9049                                        SourceLocation EndLoc) {
9050   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9051 }
9052 
9053 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9054                                           SourceLocation EndLoc) {
9055   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9056 }
9057 
9058 OMPClause *Sema::ActOnOpenMPVarListClause(
9059     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9060     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9061     SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
9062     const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
9063     OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
9064     OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9065     SourceLocation DepLinMapLoc) {
9066   OMPClause *Res = nullptr;
9067   switch (Kind) {
9068   case OMPC_private:
9069     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9070     break;
9071   case OMPC_firstprivate:
9072     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9073     break;
9074   case OMPC_lastprivate:
9075     Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9076     break;
9077   case OMPC_shared:
9078     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9079     break;
9080   case OMPC_reduction:
9081     Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9082                                      EndLoc, ReductionIdScopeSpec, ReductionId);
9083     break;
9084   case OMPC_task_reduction:
9085     Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9086                                          EndLoc, ReductionIdScopeSpec,
9087                                          ReductionId);
9088     break;
9089   case OMPC_in_reduction:
9090     Res =
9091         ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9092                                      EndLoc, ReductionIdScopeSpec, ReductionId);
9093     break;
9094   case OMPC_linear:
9095     Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
9096                                   LinKind, DepLinMapLoc, ColonLoc, EndLoc);
9097     break;
9098   case OMPC_aligned:
9099     Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9100                                    ColonLoc, EndLoc);
9101     break;
9102   case OMPC_copyin:
9103     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9104     break;
9105   case OMPC_copyprivate:
9106     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9107     break;
9108   case OMPC_flush:
9109     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9110     break;
9111   case OMPC_depend:
9112     Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
9113                                   StartLoc, LParenLoc, EndLoc);
9114     break;
9115   case OMPC_map:
9116     Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
9117                                DepLinMapLoc, ColonLoc, VarList, StartLoc,
9118                                LParenLoc, EndLoc);
9119     break;
9120   case OMPC_to:
9121     Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9122     break;
9123   case OMPC_from:
9124     Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9125     break;
9126   case OMPC_use_device_ptr:
9127     Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9128     break;
9129   case OMPC_is_device_ptr:
9130     Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9131     break;
9132   case OMPC_if:
9133   case OMPC_final:
9134   case OMPC_num_threads:
9135   case OMPC_safelen:
9136   case OMPC_simdlen:
9137   case OMPC_collapse:
9138   case OMPC_default:
9139   case OMPC_proc_bind:
9140   case OMPC_schedule:
9141   case OMPC_ordered:
9142   case OMPC_nowait:
9143   case OMPC_untied:
9144   case OMPC_mergeable:
9145   case OMPC_threadprivate:
9146   case OMPC_read:
9147   case OMPC_write:
9148   case OMPC_update:
9149   case OMPC_capture:
9150   case OMPC_seq_cst:
9151   case OMPC_device:
9152   case OMPC_threads:
9153   case OMPC_simd:
9154   case OMPC_num_teams:
9155   case OMPC_thread_limit:
9156   case OMPC_priority:
9157   case OMPC_grainsize:
9158   case OMPC_nogroup:
9159   case OMPC_num_tasks:
9160   case OMPC_hint:
9161   case OMPC_dist_schedule:
9162   case OMPC_defaultmap:
9163   case OMPC_unknown:
9164   case OMPC_uniform:
9165     llvm_unreachable("Clause is not allowed.");
9166   }
9167   return Res;
9168 }
9169 
9170 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
9171                                        ExprObjectKind OK, SourceLocation Loc) {
9172   ExprResult Res = BuildDeclRefExpr(
9173       Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9174   if (!Res.isUsable())
9175     return ExprError();
9176   if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9177     Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9178     if (!Res.isUsable())
9179       return ExprError();
9180   }
9181   if (VK != VK_LValue && Res.get()->isGLValue()) {
9182     Res = DefaultLvalueConversion(Res.get());
9183     if (!Res.isUsable())
9184       return ExprError();
9185   }
9186   return Res;
9187 }
9188 
9189 static std::pair<ValueDecl *, bool>
9190 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9191                SourceRange &ERange, bool AllowArraySection = false) {
9192   if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9193       RefExpr->containsUnexpandedParameterPack())
9194     return std::make_pair(nullptr, true);
9195 
9196   // OpenMP [3.1, C/C++]
9197   //  A list item is a variable name.
9198   // OpenMP  [2.9.3.3, Restrictions, p.1]
9199   //  A variable that is part of another variable (as an array or
9200   //  structure element) cannot appear in a private clause.
9201   RefExpr = RefExpr->IgnoreParens();
9202   enum {
9203     NoArrayExpr = -1,
9204     ArraySubscript = 0,
9205     OMPArraySection = 1
9206   } IsArrayExpr = NoArrayExpr;
9207   if (AllowArraySection) {
9208     if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
9209       auto *Base = ASE->getBase()->IgnoreParenImpCasts();
9210       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9211         Base = TempASE->getBase()->IgnoreParenImpCasts();
9212       RefExpr = Base;
9213       IsArrayExpr = ArraySubscript;
9214     } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
9215       auto *Base = OASE->getBase()->IgnoreParenImpCasts();
9216       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9217         Base = TempOASE->getBase()->IgnoreParenImpCasts();
9218       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9219         Base = TempASE->getBase()->IgnoreParenImpCasts();
9220       RefExpr = Base;
9221       IsArrayExpr = OMPArraySection;
9222     }
9223   }
9224   ELoc = RefExpr->getExprLoc();
9225   ERange = RefExpr->getSourceRange();
9226   RefExpr = RefExpr->IgnoreParenImpCasts();
9227   auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9228   auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9229   if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9230       (S.getCurrentThisType().isNull() || !ME ||
9231        !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9232        !isa<FieldDecl>(ME->getMemberDecl()))) {
9233     if (IsArrayExpr != NoArrayExpr)
9234       S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9235                                                          << ERange;
9236     else {
9237       S.Diag(ELoc,
9238              AllowArraySection
9239                  ? diag::err_omp_expected_var_name_member_expr_or_array_item
9240                  : diag::err_omp_expected_var_name_member_expr)
9241           << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9242     }
9243     return std::make_pair(nullptr, false);
9244   }
9245   return std::make_pair(
9246       getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
9247 }
9248 
9249 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9250                                           SourceLocation StartLoc,
9251                                           SourceLocation LParenLoc,
9252                                           SourceLocation EndLoc) {
9253   SmallVector<Expr *, 8> Vars;
9254   SmallVector<Expr *, 8> PrivateCopies;
9255   for (auto &RefExpr : VarList) {
9256     assert(RefExpr && "NULL expr in OpenMP private clause.");
9257     SourceLocation ELoc;
9258     SourceRange ERange;
9259     Expr *SimpleRefExpr = RefExpr;
9260     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
9261     if (Res.second) {
9262       // It will be analyzed later.
9263       Vars.push_back(RefExpr);
9264       PrivateCopies.push_back(nullptr);
9265     }
9266     ValueDecl *D = Res.first;
9267     if (!D)
9268       continue;
9269 
9270     QualType Type = D->getType();
9271     auto *VD = dyn_cast<VarDecl>(D);
9272 
9273     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9274     //  A variable that appears in a private clause must not have an incomplete
9275     //  type or a reference type.
9276     if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
9277       continue;
9278     Type = Type.getNonReferenceType();
9279 
9280     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9281     // in a Construct]
9282     //  Variables with the predetermined data-sharing attributes may not be
9283     //  listed in data-sharing attributes clauses, except for the cases
9284     //  listed below. For these exceptions only, listing a predetermined
9285     //  variable in a data-sharing attribute clause is allowed and overrides
9286     //  the variable's predetermined data-sharing attributes.
9287     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9288     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
9289       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9290                                           << getOpenMPClauseName(OMPC_private);
9291       ReportOriginalDSA(*this, DSAStack, D, DVar);
9292       continue;
9293     }
9294 
9295     auto CurrDir = DSAStack->getCurrentDirective();
9296     // Variably modified types are not supported for tasks.
9297     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
9298         isOpenMPTaskingDirective(CurrDir)) {
9299       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9300           << getOpenMPClauseName(OMPC_private) << Type
9301           << getOpenMPDirectiveName(CurrDir);
9302       bool IsDecl =
9303           !VD ||
9304           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9305       Diag(D->getLocation(),
9306            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9307           << D;
9308       continue;
9309     }
9310 
9311     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9312     // A list item cannot appear in both a map clause and a data-sharing
9313     // attribute clause on the same construct
9314     if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel ||
9315         CurrDir == OMPD_target_teams ||
9316         CurrDir == OMPD_target_teams_distribute ||
9317         CurrDir == OMPD_target_teams_distribute_parallel_for ||
9318         CurrDir == OMPD_target_teams_distribute_parallel_for_simd ||
9319         CurrDir == OMPD_target_teams_distribute_simd ||
9320         CurrDir == OMPD_target_parallel_for_simd ||
9321         CurrDir == OMPD_target_parallel_for) {
9322       OpenMPClauseKind ConflictKind;
9323       if (DSAStack->checkMappableExprComponentListsForDecl(
9324               VD, /*CurrentRegionOnly=*/true,
9325               [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9326                   OpenMPClauseKind WhereFoundClauseKind) -> bool {
9327                 ConflictKind = WhereFoundClauseKind;
9328                 return true;
9329               })) {
9330         Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
9331             << getOpenMPClauseName(OMPC_private)
9332             << getOpenMPClauseName(ConflictKind)
9333             << getOpenMPDirectiveName(CurrDir);
9334         ReportOriginalDSA(*this, DSAStack, D, DVar);
9335         continue;
9336       }
9337     }
9338 
9339     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9340     //  A variable of class type (or array thereof) that appears in a private
9341     //  clause requires an accessible, unambiguous default constructor for the
9342     //  class type.
9343     // Generate helper private variable and initialize it with the default
9344     // value. The address of the original variable is replaced by the address of
9345     // the new private variable in CodeGen. This new variable is not added to
9346     // IdResolver, so the code in the OpenMP region uses original variable for
9347     // proper diagnostics.
9348     Type = Type.getUnqualifiedType();
9349     auto VDPrivate =
9350         buildVarDecl(*this, ELoc, Type, D->getName(),
9351                      D->hasAttrs() ? &D->getAttrs() : nullptr,
9352                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
9353     ActOnUninitializedDecl(VDPrivate);
9354     if (VDPrivate->isInvalidDecl())
9355       continue;
9356     auto VDPrivateRefExpr = buildDeclRefExpr(
9357         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
9358 
9359     DeclRefExpr *Ref = nullptr;
9360     if (!VD && !CurContext->isDependentContext())
9361       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9362     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
9363     Vars.push_back((VD || CurContext->isDependentContext())
9364                        ? RefExpr->IgnoreParens()
9365                        : Ref);
9366     PrivateCopies.push_back(VDPrivateRefExpr);
9367   }
9368 
9369   if (Vars.empty())
9370     return nullptr;
9371 
9372   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9373                                   PrivateCopies);
9374 }
9375 
9376 namespace {
9377 class DiagsUninitializedSeveretyRAII {
9378 private:
9379   DiagnosticsEngine &Diags;
9380   SourceLocation SavedLoc;
9381   bool IsIgnored;
9382 
9383 public:
9384   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9385                                  bool IsIgnored)
9386       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9387     if (!IsIgnored) {
9388       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9389                         /*Map*/ diag::Severity::Ignored, Loc);
9390     }
9391   }
9392   ~DiagsUninitializedSeveretyRAII() {
9393     if (!IsIgnored)
9394       Diags.popMappings(SavedLoc);
9395   }
9396 };
9397 }
9398 
9399 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9400                                                SourceLocation StartLoc,
9401                                                SourceLocation LParenLoc,
9402                                                SourceLocation EndLoc) {
9403   SmallVector<Expr *, 8> Vars;
9404   SmallVector<Expr *, 8> PrivateCopies;
9405   SmallVector<Expr *, 8> Inits;
9406   SmallVector<Decl *, 4> ExprCaptures;
9407   bool IsImplicitClause =
9408       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
9409   auto ImplicitClauseLoc = DSAStack->getConstructLoc();
9410 
9411   for (auto &RefExpr : VarList) {
9412     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
9413     SourceLocation ELoc;
9414     SourceRange ERange;
9415     Expr *SimpleRefExpr = RefExpr;
9416     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
9417     if (Res.second) {
9418       // It will be analyzed later.
9419       Vars.push_back(RefExpr);
9420       PrivateCopies.push_back(nullptr);
9421       Inits.push_back(nullptr);
9422     }
9423     ValueDecl *D = Res.first;
9424     if (!D)
9425       continue;
9426 
9427     ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
9428     QualType Type = D->getType();
9429     auto *VD = dyn_cast<VarDecl>(D);
9430 
9431     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9432     //  A variable that appears in a private clause must not have an incomplete
9433     //  type or a reference type.
9434     if (RequireCompleteType(ELoc, Type,
9435                             diag::err_omp_firstprivate_incomplete_type))
9436       continue;
9437     Type = Type.getNonReferenceType();
9438 
9439     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9440     //  A variable of class type (or array thereof) that appears in a private
9441     //  clause requires an accessible, unambiguous copy constructor for the
9442     //  class type.
9443     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
9444 
9445     // If an implicit firstprivate variable found it was checked already.
9446     DSAStackTy::DSAVarData TopDVar;
9447     if (!IsImplicitClause) {
9448       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9449       TopDVar = DVar;
9450       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9451       bool IsConstant = ElemType.isConstant(Context);
9452       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9453       //  A list item that specifies a given variable may not appear in more
9454       // than one clause on the same directive, except that a variable may be
9455       //  specified in both firstprivate and lastprivate clauses.
9456       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9457       // A list item may appear in a firstprivate or lastprivate clause but not
9458       // both.
9459       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
9460           (isOpenMPDistributeDirective(CurrDir) ||
9461            DVar.CKind != OMPC_lastprivate) &&
9462           DVar.RefExpr) {
9463         Diag(ELoc, diag::err_omp_wrong_dsa)
9464             << getOpenMPClauseName(DVar.CKind)
9465             << getOpenMPClauseName(OMPC_firstprivate);
9466         ReportOriginalDSA(*this, DSAStack, D, DVar);
9467         continue;
9468       }
9469 
9470       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9471       // in a Construct]
9472       //  Variables with the predetermined data-sharing attributes may not be
9473       //  listed in data-sharing attributes clauses, except for the cases
9474       //  listed below. For these exceptions only, listing a predetermined
9475       //  variable in a data-sharing attribute clause is allowed and overrides
9476       //  the variable's predetermined data-sharing attributes.
9477       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9478       // in a Construct, C/C++, p.2]
9479       //  Variables with const-qualified type having no mutable member may be
9480       //  listed in a firstprivate clause, even if they are static data members.
9481       if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
9482           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9483         Diag(ELoc, diag::err_omp_wrong_dsa)
9484             << getOpenMPClauseName(DVar.CKind)
9485             << getOpenMPClauseName(OMPC_firstprivate);
9486         ReportOriginalDSA(*this, DSAStack, D, DVar);
9487         continue;
9488       }
9489 
9490       // OpenMP [2.9.3.4, Restrictions, p.2]
9491       //  A list item that is private within a parallel region must not appear
9492       //  in a firstprivate clause on a worksharing construct if any of the
9493       //  worksharing regions arising from the worksharing construct ever bind
9494       //  to any of the parallel regions arising from the parallel construct.
9495       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9496       // A list item that is private within a teams region must not appear in a
9497       // firstprivate clause on a distribute construct if any of the distribute
9498       // regions arising from the distribute construct ever bind to any of the
9499       // teams regions arising from the teams construct.
9500       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9501       // A list item that appears in a reduction clause of a teams construct
9502       // must not appear in a firstprivate clause on a distribute construct if
9503       // any of the distribute regions arising from the distribute construct
9504       // ever bind to any of the teams regions arising from the teams construct.
9505       if ((isOpenMPWorksharingDirective(CurrDir) ||
9506            isOpenMPDistributeDirective(CurrDir)) &&
9507           !isOpenMPParallelDirective(CurrDir) &&
9508           !isOpenMPTeamsDirective(CurrDir)) {
9509         DVar = DSAStack->getImplicitDSA(D, true);
9510         if (DVar.CKind != OMPC_shared &&
9511             (isOpenMPParallelDirective(DVar.DKind) ||
9512              isOpenMPTeamsDirective(DVar.DKind) ||
9513              DVar.DKind == OMPD_unknown)) {
9514           Diag(ELoc, diag::err_omp_required_access)
9515               << getOpenMPClauseName(OMPC_firstprivate)
9516               << getOpenMPClauseName(OMPC_shared);
9517           ReportOriginalDSA(*this, DSAStack, D, DVar);
9518           continue;
9519         }
9520       }
9521       // OpenMP [2.9.3.4, Restrictions, p.3]
9522       //  A list item that appears in a reduction clause of a parallel construct
9523       //  must not appear in a firstprivate clause on a worksharing or task
9524       //  construct if any of the worksharing or task regions arising from the
9525       //  worksharing or task construct ever bind to any of the parallel regions
9526       //  arising from the parallel construct.
9527       // OpenMP [2.9.3.4, Restrictions, p.4]
9528       //  A list item that appears in a reduction clause in worksharing
9529       //  construct must not appear in a firstprivate clause in a task construct
9530       //  encountered during execution of any of the worksharing regions arising
9531       //  from the worksharing construct.
9532       if (isOpenMPTaskingDirective(CurrDir)) {
9533         DVar = DSAStack->hasInnermostDSA(
9534             D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
9535             [](OpenMPDirectiveKind K) -> bool {
9536               return isOpenMPParallelDirective(K) ||
9537                      isOpenMPWorksharingDirective(K) ||
9538                      isOpenMPTeamsDirective(K);
9539             },
9540             /*FromParent=*/true);
9541         if (DVar.CKind == OMPC_reduction &&
9542             (isOpenMPParallelDirective(DVar.DKind) ||
9543              isOpenMPWorksharingDirective(DVar.DKind) ||
9544              isOpenMPTeamsDirective(DVar.DKind))) {
9545           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
9546               << getOpenMPDirectiveName(DVar.DKind);
9547           ReportOriginalDSA(*this, DSAStack, D, DVar);
9548           continue;
9549         }
9550       }
9551 
9552       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9553       // A list item cannot appear in both a map clause and a data-sharing
9554       // attribute clause on the same construct
9555       if (isOpenMPTargetExecutionDirective(CurrDir)) {
9556         OpenMPClauseKind ConflictKind;
9557         if (DSAStack->checkMappableExprComponentListsForDecl(
9558                 VD, /*CurrentRegionOnly=*/true,
9559                 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9560                     OpenMPClauseKind WhereFoundClauseKind) -> bool {
9561                   ConflictKind = WhereFoundClauseKind;
9562                   return true;
9563                 })) {
9564           Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
9565               << getOpenMPClauseName(OMPC_firstprivate)
9566               << getOpenMPClauseName(ConflictKind)
9567               << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9568           ReportOriginalDSA(*this, DSAStack, D, DVar);
9569           continue;
9570         }
9571       }
9572     }
9573 
9574     // Variably modified types are not supported for tasks.
9575     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
9576         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
9577       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9578           << getOpenMPClauseName(OMPC_firstprivate) << Type
9579           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9580       bool IsDecl =
9581           !VD ||
9582           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9583       Diag(D->getLocation(),
9584            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9585           << D;
9586       continue;
9587     }
9588 
9589     Type = Type.getUnqualifiedType();
9590     auto VDPrivate =
9591         buildVarDecl(*this, ELoc, Type, D->getName(),
9592                      D->hasAttrs() ? &D->getAttrs() : nullptr,
9593                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
9594     // Generate helper private variable and initialize it with the value of the
9595     // original variable. The address of the original variable is replaced by
9596     // the address of the new private variable in the CodeGen. This new variable
9597     // is not added to IdResolver, so the code in the OpenMP region uses
9598     // original variable for proper diagnostics and variable capturing.
9599     Expr *VDInitRefExpr = nullptr;
9600     // For arrays generate initializer for single element and replace it by the
9601     // original array element in CodeGen.
9602     if (Type->isArrayType()) {
9603       auto VDInit =
9604           buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
9605       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
9606       auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
9607       ElemType = ElemType.getUnqualifiedType();
9608       auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
9609                                       ".firstprivate.temp");
9610       InitializedEntity Entity =
9611           InitializedEntity::InitializeVariable(VDInitTemp);
9612       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
9613 
9614       InitializationSequence InitSeq(*this, Entity, Kind, Init);
9615       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
9616       if (Result.isInvalid())
9617         VDPrivate->setInvalidDecl();
9618       else
9619         VDPrivate->setInit(Result.getAs<Expr>());
9620       // Remove temp variable declaration.
9621       Context.Deallocate(VDInitTemp);
9622     } else {
9623       auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
9624                                   ".firstprivate.temp");
9625       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
9626                                        RefExpr->getExprLoc());
9627       AddInitializerToDecl(VDPrivate,
9628                            DefaultLvalueConversion(VDInitRefExpr).get(),
9629                            /*DirectInit=*/false);
9630     }
9631     if (VDPrivate->isInvalidDecl()) {
9632       if (IsImplicitClause) {
9633         Diag(RefExpr->getExprLoc(),
9634              diag::note_omp_task_predetermined_firstprivate_here);
9635       }
9636       continue;
9637     }
9638     CurContext->addDecl(VDPrivate);
9639     auto VDPrivateRefExpr = buildDeclRefExpr(
9640         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
9641         RefExpr->getExprLoc());
9642     DeclRefExpr *Ref = nullptr;
9643     if (!VD && !CurContext->isDependentContext()) {
9644       if (TopDVar.CKind == OMPC_lastprivate)
9645         Ref = TopDVar.PrivateCopy;
9646       else {
9647         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9648         if (!IsOpenMPCapturedDecl(D))
9649           ExprCaptures.push_back(Ref->getDecl());
9650       }
9651     }
9652     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
9653     Vars.push_back((VD || CurContext->isDependentContext())
9654                        ? RefExpr->IgnoreParens()
9655                        : Ref);
9656     PrivateCopies.push_back(VDPrivateRefExpr);
9657     Inits.push_back(VDInitRefExpr);
9658   }
9659 
9660   if (Vars.empty())
9661     return nullptr;
9662 
9663   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9664                                        Vars, PrivateCopies, Inits,
9665                                        buildPreInits(Context, ExprCaptures));
9666 }
9667 
9668 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
9669                                               SourceLocation StartLoc,
9670                                               SourceLocation LParenLoc,
9671                                               SourceLocation EndLoc) {
9672   SmallVector<Expr *, 8> Vars;
9673   SmallVector<Expr *, 8> SrcExprs;
9674   SmallVector<Expr *, 8> DstExprs;
9675   SmallVector<Expr *, 8> AssignmentOps;
9676   SmallVector<Decl *, 4> ExprCaptures;
9677   SmallVector<Expr *, 4> ExprPostUpdates;
9678   for (auto &RefExpr : VarList) {
9679     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
9680     SourceLocation ELoc;
9681     SourceRange ERange;
9682     Expr *SimpleRefExpr = RefExpr;
9683     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
9684     if (Res.second) {
9685       // It will be analyzed later.
9686       Vars.push_back(RefExpr);
9687       SrcExprs.push_back(nullptr);
9688       DstExprs.push_back(nullptr);
9689       AssignmentOps.push_back(nullptr);
9690     }
9691     ValueDecl *D = Res.first;
9692     if (!D)
9693       continue;
9694 
9695     QualType Type = D->getType();
9696     auto *VD = dyn_cast<VarDecl>(D);
9697 
9698     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
9699     //  A variable that appears in a lastprivate clause must not have an
9700     //  incomplete type or a reference type.
9701     if (RequireCompleteType(ELoc, Type,
9702                             diag::err_omp_lastprivate_incomplete_type))
9703       continue;
9704     Type = Type.getNonReferenceType();
9705 
9706     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9707     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9708     // in a Construct]
9709     //  Variables with the predetermined data-sharing attributes may not be
9710     //  listed in data-sharing attributes clauses, except for the cases
9711     //  listed below.
9712     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9713     // A list item may appear in a firstprivate or lastprivate clause but not
9714     // both.
9715     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9716     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
9717         (isOpenMPDistributeDirective(CurrDir) ||
9718          DVar.CKind != OMPC_firstprivate) &&
9719         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
9720       Diag(ELoc, diag::err_omp_wrong_dsa)
9721           << getOpenMPClauseName(DVar.CKind)
9722           << getOpenMPClauseName(OMPC_lastprivate);
9723       ReportOriginalDSA(*this, DSAStack, D, DVar);
9724       continue;
9725     }
9726 
9727     // OpenMP [2.14.3.5, Restrictions, p.2]
9728     // A list item that is private within a parallel region, or that appears in
9729     // the reduction clause of a parallel construct, must not appear in a
9730     // lastprivate clause on a worksharing construct if any of the corresponding
9731     // worksharing regions ever binds to any of the corresponding parallel
9732     // regions.
9733     DSAStackTy::DSAVarData TopDVar = DVar;
9734     if (isOpenMPWorksharingDirective(CurrDir) &&
9735         !isOpenMPParallelDirective(CurrDir) &&
9736         !isOpenMPTeamsDirective(CurrDir)) {
9737       DVar = DSAStack->getImplicitDSA(D, true);
9738       if (DVar.CKind != OMPC_shared) {
9739         Diag(ELoc, diag::err_omp_required_access)
9740             << getOpenMPClauseName(OMPC_lastprivate)
9741             << getOpenMPClauseName(OMPC_shared);
9742         ReportOriginalDSA(*this, DSAStack, D, DVar);
9743         continue;
9744       }
9745     }
9746 
9747     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
9748     //  A variable of class type (or array thereof) that appears in a
9749     //  lastprivate clause requires an accessible, unambiguous default
9750     //  constructor for the class type, unless the list item is also specified
9751     //  in a firstprivate clause.
9752     //  A variable of class type (or array thereof) that appears in a
9753     //  lastprivate clause requires an accessible, unambiguous copy assignment
9754     //  operator for the class type.
9755     Type = Context.getBaseElementType(Type).getNonReferenceType();
9756     auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
9757                                Type.getUnqualifiedType(), ".lastprivate.src",
9758                                D->hasAttrs() ? &D->getAttrs() : nullptr);
9759     auto *PseudoSrcExpr =
9760         buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
9761     auto *DstVD =
9762         buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
9763                      D->hasAttrs() ? &D->getAttrs() : nullptr);
9764     auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
9765     // For arrays generate assignment operation for single element and replace
9766     // it by the original array element in CodeGen.
9767     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
9768                                    PseudoDstExpr, PseudoSrcExpr);
9769     if (AssignmentOp.isInvalid())
9770       continue;
9771     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
9772                                        /*DiscardedValue=*/true);
9773     if (AssignmentOp.isInvalid())
9774       continue;
9775 
9776     DeclRefExpr *Ref = nullptr;
9777     if (!VD && !CurContext->isDependentContext()) {
9778       if (TopDVar.CKind == OMPC_firstprivate)
9779         Ref = TopDVar.PrivateCopy;
9780       else {
9781         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9782         if (!IsOpenMPCapturedDecl(D))
9783           ExprCaptures.push_back(Ref->getDecl());
9784       }
9785       if (TopDVar.CKind == OMPC_firstprivate ||
9786           (!IsOpenMPCapturedDecl(D) &&
9787            Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
9788         ExprResult RefRes = DefaultLvalueConversion(Ref);
9789         if (!RefRes.isUsable())
9790           continue;
9791         ExprResult PostUpdateRes =
9792             BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
9793                        RefRes.get());
9794         if (!PostUpdateRes.isUsable())
9795           continue;
9796         ExprPostUpdates.push_back(
9797             IgnoredValueConversions(PostUpdateRes.get()).get());
9798       }
9799     }
9800     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
9801     Vars.push_back((VD || CurContext->isDependentContext())
9802                        ? RefExpr->IgnoreParens()
9803                        : Ref);
9804     SrcExprs.push_back(PseudoSrcExpr);
9805     DstExprs.push_back(PseudoDstExpr);
9806     AssignmentOps.push_back(AssignmentOp.get());
9807   }
9808 
9809   if (Vars.empty())
9810     return nullptr;
9811 
9812   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9813                                       Vars, SrcExprs, DstExprs, AssignmentOps,
9814                                       buildPreInits(Context, ExprCaptures),
9815                                       buildPostUpdate(*this, ExprPostUpdates));
9816 }
9817 
9818 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
9819                                          SourceLocation StartLoc,
9820                                          SourceLocation LParenLoc,
9821                                          SourceLocation EndLoc) {
9822   SmallVector<Expr *, 8> Vars;
9823   for (auto &RefExpr : VarList) {
9824     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
9825     SourceLocation ELoc;
9826     SourceRange ERange;
9827     Expr *SimpleRefExpr = RefExpr;
9828     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
9829     if (Res.second) {
9830       // It will be analyzed later.
9831       Vars.push_back(RefExpr);
9832     }
9833     ValueDecl *D = Res.first;
9834     if (!D)
9835       continue;
9836 
9837     auto *VD = dyn_cast<VarDecl>(D);
9838     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9839     // in a Construct]
9840     //  Variables with the predetermined data-sharing attributes may not be
9841     //  listed in data-sharing attributes clauses, except for the cases
9842     //  listed below. For these exceptions only, listing a predetermined
9843     //  variable in a data-sharing attribute clause is allowed and overrides
9844     //  the variable's predetermined data-sharing attributes.
9845     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9846     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
9847         DVar.RefExpr) {
9848       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9849                                           << getOpenMPClauseName(OMPC_shared);
9850       ReportOriginalDSA(*this, DSAStack, D, DVar);
9851       continue;
9852     }
9853 
9854     DeclRefExpr *Ref = nullptr;
9855     if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
9856       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9857     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
9858     Vars.push_back((VD || !Ref || CurContext->isDependentContext())
9859                        ? RefExpr->IgnoreParens()
9860                        : Ref);
9861   }
9862 
9863   if (Vars.empty())
9864     return nullptr;
9865 
9866   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
9867 }
9868 
9869 namespace {
9870 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
9871   DSAStackTy *Stack;
9872 
9873 public:
9874   bool VisitDeclRefExpr(DeclRefExpr *E) {
9875     if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
9876       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
9877       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
9878         return false;
9879       if (DVar.CKind != OMPC_unknown)
9880         return true;
9881       DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
9882           VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
9883           /*FromParent=*/true);
9884       if (DVarPrivate.CKind != OMPC_unknown)
9885         return true;
9886       return false;
9887     }
9888     return false;
9889   }
9890   bool VisitStmt(Stmt *S) {
9891     for (auto Child : S->children()) {
9892       if (Child && Visit(Child))
9893         return true;
9894     }
9895     return false;
9896   }
9897   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
9898 };
9899 } // namespace
9900 
9901 namespace {
9902 // Transform MemberExpression for specified FieldDecl of current class to
9903 // DeclRefExpr to specified OMPCapturedExprDecl.
9904 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
9905   typedef TreeTransform<TransformExprToCaptures> BaseTransform;
9906   ValueDecl *Field;
9907   DeclRefExpr *CapturedExpr;
9908 
9909 public:
9910   TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
9911       : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
9912 
9913   ExprResult TransformMemberExpr(MemberExpr *E) {
9914     if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
9915         E->getMemberDecl() == Field) {
9916       CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
9917       return CapturedExpr;
9918     }
9919     return BaseTransform::TransformMemberExpr(E);
9920   }
9921   DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
9922 };
9923 } // namespace
9924 
9925 template <typename T>
9926 static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
9927                             const llvm::function_ref<T(ValueDecl *)> &Gen) {
9928   for (auto &Set : Lookups) {
9929     for (auto *D : Set) {
9930       if (auto Res = Gen(cast<ValueDecl>(D)))
9931         return Res;
9932     }
9933   }
9934   return T();
9935 }
9936 
9937 static ExprResult
9938 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
9939                          Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
9940                          const DeclarationNameInfo &ReductionId, QualType Ty,
9941                          CXXCastPath &BasePath, Expr *UnresolvedReduction) {
9942   if (ReductionIdScopeSpec.isInvalid())
9943     return ExprError();
9944   SmallVector<UnresolvedSet<8>, 4> Lookups;
9945   if (S) {
9946     LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
9947     Lookup.suppressDiagnostics();
9948     while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
9949       auto *D = Lookup.getRepresentativeDecl();
9950       do {
9951         S = S->getParent();
9952       } while (S && !S->isDeclScope(D));
9953       if (S)
9954         S = S->getParent();
9955       Lookups.push_back(UnresolvedSet<8>());
9956       Lookups.back().append(Lookup.begin(), Lookup.end());
9957       Lookup.clear();
9958     }
9959   } else if (auto *ULE =
9960                  cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
9961     Lookups.push_back(UnresolvedSet<8>());
9962     Decl *PrevD = nullptr;
9963     for (auto *D : ULE->decls()) {
9964       if (D == PrevD)
9965         Lookups.push_back(UnresolvedSet<8>());
9966       else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
9967         Lookups.back().addDecl(DRD);
9968       PrevD = D;
9969     }
9970   }
9971   if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
9972       Ty->isInstantiationDependentType() ||
9973       Ty->containsUnexpandedParameterPack() ||
9974       filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
9975         return !D->isInvalidDecl() &&
9976                (D->getType()->isDependentType() ||
9977                 D->getType()->isInstantiationDependentType() ||
9978                 D->getType()->containsUnexpandedParameterPack());
9979       })) {
9980     UnresolvedSet<8> ResSet;
9981     for (auto &Set : Lookups) {
9982       ResSet.append(Set.begin(), Set.end());
9983       // The last item marks the end of all declarations at the specified scope.
9984       ResSet.addDecl(Set[Set.size() - 1]);
9985     }
9986     return UnresolvedLookupExpr::Create(
9987         SemaRef.Context, /*NamingClass=*/nullptr,
9988         ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
9989         /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
9990   }
9991   if (auto *VD = filterLookupForUDR<ValueDecl *>(
9992           Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
9993             if (!D->isInvalidDecl() &&
9994                 SemaRef.Context.hasSameType(D->getType(), Ty))
9995               return D;
9996             return nullptr;
9997           }))
9998     return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9999   if (auto *VD = filterLookupForUDR<ValueDecl *>(
10000           Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10001             if (!D->isInvalidDecl() &&
10002                 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10003                 !Ty.isMoreQualifiedThan(D->getType()))
10004               return D;
10005             return nullptr;
10006           })) {
10007     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10008                        /*DetectVirtual=*/false);
10009     if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10010       if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10011               VD->getType().getUnqualifiedType()))) {
10012         if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10013                                          /*DiagID=*/0) !=
10014             Sema::AR_inaccessible) {
10015           SemaRef.BuildBasePathArray(Paths, BasePath);
10016           return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10017         }
10018       }
10019     }
10020   }
10021   if (ReductionIdScopeSpec.isSet()) {
10022     SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10023     return ExprError();
10024   }
10025   return ExprEmpty();
10026 }
10027 
10028 namespace {
10029 /// Data for the reduction-based clauses.
10030 struct ReductionData {
10031   /// List of original reduction items.
10032   SmallVector<Expr *, 8> Vars;
10033   /// List of private copies of the reduction items.
10034   SmallVector<Expr *, 8> Privates;
10035   /// LHS expressions for the reduction_op expressions.
10036   SmallVector<Expr *, 8> LHSs;
10037   /// RHS expressions for the reduction_op expressions.
10038   SmallVector<Expr *, 8> RHSs;
10039   /// Reduction operation expression.
10040   SmallVector<Expr *, 8> ReductionOps;
10041   /// Taskgroup descriptors for the corresponding reduction items in
10042   /// in_reduction clauses.
10043   SmallVector<Expr *, 8> TaskgroupDescriptors;
10044   /// List of captures for clause.
10045   SmallVector<Decl *, 4> ExprCaptures;
10046   /// List of postupdate expressions.
10047   SmallVector<Expr *, 4> ExprPostUpdates;
10048   ReductionData() = delete;
10049   /// Reserves required memory for the reduction data.
10050   ReductionData(unsigned Size) {
10051     Vars.reserve(Size);
10052     Privates.reserve(Size);
10053     LHSs.reserve(Size);
10054     RHSs.reserve(Size);
10055     ReductionOps.reserve(Size);
10056     TaskgroupDescriptors.reserve(Size);
10057     ExprCaptures.reserve(Size);
10058     ExprPostUpdates.reserve(Size);
10059   }
10060   /// Stores reduction item and reduction operation only (required for dependent
10061   /// reduction item).
10062   void push(Expr *Item, Expr *ReductionOp) {
10063     Vars.emplace_back(Item);
10064     Privates.emplace_back(nullptr);
10065     LHSs.emplace_back(nullptr);
10066     RHSs.emplace_back(nullptr);
10067     ReductionOps.emplace_back(ReductionOp);
10068     TaskgroupDescriptors.emplace_back(nullptr);
10069   }
10070   /// Stores reduction data.
10071   void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10072             Expr *TaskgroupDescriptor) {
10073     Vars.emplace_back(Item);
10074     Privates.emplace_back(Private);
10075     LHSs.emplace_back(LHS);
10076     RHSs.emplace_back(RHS);
10077     ReductionOps.emplace_back(ReductionOp);
10078     TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
10079   }
10080 };
10081 } // namespace
10082 
10083 static bool CheckOMPArraySectionConstantForReduction(
10084     ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10085     SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10086   const Expr *Length = OASE->getLength();
10087   if (Length == nullptr) {
10088     // For array sections of the form [1:] or [:], we would need to analyze
10089     // the lower bound...
10090     if (OASE->getColonLoc().isValid())
10091       return false;
10092 
10093     // This is an array subscript which has implicit length 1!
10094     SingleElement = true;
10095     ArraySizes.push_back(llvm::APSInt::get(1));
10096   } else {
10097     llvm::APSInt ConstantLengthValue;
10098     if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
10099       return false;
10100 
10101     SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10102     ArraySizes.push_back(ConstantLengthValue);
10103   }
10104 
10105   // Get the base of this array section and walk up from there.
10106   const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10107 
10108   // We require length = 1 for all array sections except the right-most to
10109   // guarantee that the memory region is contiguous and has no holes in it.
10110   while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10111     Length = TempOASE->getLength();
10112     if (Length == nullptr) {
10113       // For array sections of the form [1:] or [:], we would need to analyze
10114       // the lower bound...
10115       if (OASE->getColonLoc().isValid())
10116         return false;
10117 
10118       // This is an array subscript which has implicit length 1!
10119       ArraySizes.push_back(llvm::APSInt::get(1));
10120     } else {
10121       llvm::APSInt ConstantLengthValue;
10122       if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
10123           ConstantLengthValue.getSExtValue() != 1)
10124         return false;
10125 
10126       ArraySizes.push_back(ConstantLengthValue);
10127     }
10128     Base = TempOASE->getBase()->IgnoreParenImpCasts();
10129   }
10130 
10131   // If we have a single element, we don't need to add the implicit lengths.
10132   if (!SingleElement) {
10133     while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10134       // Has implicit length 1!
10135       ArraySizes.push_back(llvm::APSInt::get(1));
10136       Base = TempASE->getBase()->IgnoreParenImpCasts();
10137     }
10138   }
10139 
10140   // This array section can be privatized as a single value or as a constant
10141   // sized array.
10142   return true;
10143 }
10144 
10145 static bool ActOnOMPReductionKindClause(
10146     Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10147     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10148     SourceLocation ColonLoc, SourceLocation EndLoc,
10149     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10150     ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
10151   auto DN = ReductionId.getName();
10152   auto OOK = DN.getCXXOverloadedOperator();
10153   BinaryOperatorKind BOK = BO_Comma;
10154 
10155   ASTContext &Context = S.Context;
10156   // OpenMP [2.14.3.6, reduction clause]
10157   // C
10158   // reduction-identifier is either an identifier or one of the following
10159   // operators: +, -, *,  &, |, ^, && and ||
10160   // C++
10161   // reduction-identifier is either an id-expression or one of the following
10162   // operators: +, -, *, &, |, ^, && and ||
10163   switch (OOK) {
10164   case OO_Plus:
10165   case OO_Minus:
10166     BOK = BO_Add;
10167     break;
10168   case OO_Star:
10169     BOK = BO_Mul;
10170     break;
10171   case OO_Amp:
10172     BOK = BO_And;
10173     break;
10174   case OO_Pipe:
10175     BOK = BO_Or;
10176     break;
10177   case OO_Caret:
10178     BOK = BO_Xor;
10179     break;
10180   case OO_AmpAmp:
10181     BOK = BO_LAnd;
10182     break;
10183   case OO_PipePipe:
10184     BOK = BO_LOr;
10185     break;
10186   case OO_New:
10187   case OO_Delete:
10188   case OO_Array_New:
10189   case OO_Array_Delete:
10190   case OO_Slash:
10191   case OO_Percent:
10192   case OO_Tilde:
10193   case OO_Exclaim:
10194   case OO_Equal:
10195   case OO_Less:
10196   case OO_Greater:
10197   case OO_LessEqual:
10198   case OO_GreaterEqual:
10199   case OO_PlusEqual:
10200   case OO_MinusEqual:
10201   case OO_StarEqual:
10202   case OO_SlashEqual:
10203   case OO_PercentEqual:
10204   case OO_CaretEqual:
10205   case OO_AmpEqual:
10206   case OO_PipeEqual:
10207   case OO_LessLess:
10208   case OO_GreaterGreater:
10209   case OO_LessLessEqual:
10210   case OO_GreaterGreaterEqual:
10211   case OO_EqualEqual:
10212   case OO_ExclaimEqual:
10213   case OO_Spaceship:
10214   case OO_PlusPlus:
10215   case OO_MinusMinus:
10216   case OO_Comma:
10217   case OO_ArrowStar:
10218   case OO_Arrow:
10219   case OO_Call:
10220   case OO_Subscript:
10221   case OO_Conditional:
10222   case OO_Coawait:
10223   case NUM_OVERLOADED_OPERATORS:
10224     llvm_unreachable("Unexpected reduction identifier");
10225   case OO_None:
10226     if (auto *II = DN.getAsIdentifierInfo()) {
10227       if (II->isStr("max"))
10228         BOK = BO_GT;
10229       else if (II->isStr("min"))
10230         BOK = BO_LT;
10231     }
10232     break;
10233   }
10234   SourceRange ReductionIdRange;
10235   if (ReductionIdScopeSpec.isValid())
10236     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
10237   else
10238     ReductionIdRange.setBegin(ReductionId.getBeginLoc());
10239   ReductionIdRange.setEnd(ReductionId.getEndLoc());
10240 
10241   auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10242   bool FirstIter = true;
10243   for (auto RefExpr : VarList) {
10244     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
10245     // OpenMP [2.1, C/C++]
10246     //  A list item is a variable or array section, subject to the restrictions
10247     //  specified in Section 2.4 on page 42 and in each of the sections
10248     // describing clauses and directives for which a list appears.
10249     // OpenMP  [2.14.3.3, Restrictions, p.1]
10250     //  A variable that is part of another variable (as an array or
10251     //  structure element) cannot appear in a private clause.
10252     if (!FirstIter && IR != ER)
10253       ++IR;
10254     FirstIter = false;
10255     SourceLocation ELoc;
10256     SourceRange ERange;
10257     Expr *SimpleRefExpr = RefExpr;
10258     auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
10259                               /*AllowArraySection=*/true);
10260     if (Res.second) {
10261       // Try to find 'declare reduction' corresponding construct before using
10262       // builtin/overloaded operators.
10263       QualType Type = Context.DependentTy;
10264       CXXCastPath BasePath;
10265       ExprResult DeclareReductionRef = buildDeclareReductionRef(
10266           S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
10267           ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10268       Expr *ReductionOp = nullptr;
10269       if (S.CurContext->isDependentContext() &&
10270           (DeclareReductionRef.isUnset() ||
10271            isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
10272         ReductionOp = DeclareReductionRef.get();
10273       // It will be analyzed later.
10274       RD.push(RefExpr, ReductionOp);
10275     }
10276     ValueDecl *D = Res.first;
10277     if (!D)
10278       continue;
10279 
10280     Expr *TaskgroupDescriptor = nullptr;
10281     QualType Type;
10282     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10283     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
10284     if (ASE)
10285       Type = ASE->getType().getNonReferenceType();
10286     else if (OASE) {
10287       auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10288       if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
10289         Type = ATy->getElementType();
10290       else
10291         Type = BaseType->getPointeeType();
10292       Type = Type.getNonReferenceType();
10293     } else
10294       Type = Context.getBaseElementType(D->getType().getNonReferenceType());
10295     auto *VD = dyn_cast<VarDecl>(D);
10296 
10297     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10298     //  A variable that appears in a private clause must not have an incomplete
10299     //  type or a reference type.
10300     if (S.RequireCompleteType(ELoc, Type,
10301                               diag::err_omp_reduction_incomplete_type))
10302       continue;
10303     // OpenMP [2.14.3.6, reduction clause, Restrictions]
10304     // A list item that appears in a reduction clause must not be
10305     // const-qualified.
10306     if (Type.getNonReferenceType().isConstant(Context)) {
10307       S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
10308       if (!ASE && !OASE) {
10309         bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10310                                  VarDecl::DeclarationOnly;
10311         S.Diag(D->getLocation(),
10312                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10313             << D;
10314       }
10315       continue;
10316     }
10317     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10318     //  If a list-item is a reference type then it must bind to the same object
10319     //  for all threads of the team.
10320     if (!ASE && !OASE && VD) {
10321       VarDecl *VDDef = VD->getDefinition();
10322       if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
10323         DSARefChecker Check(Stack);
10324         if (Check.Visit(VDDef->getInit())) {
10325           S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10326               << getOpenMPClauseName(ClauseKind) << ERange;
10327           S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
10328           continue;
10329         }
10330       }
10331     }
10332 
10333     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10334     // in a Construct]
10335     //  Variables with the predetermined data-sharing attributes may not be
10336     //  listed in data-sharing attributes clauses, except for the cases
10337     //  listed below. For these exceptions only, listing a predetermined
10338     //  variable in a data-sharing attribute clause is allowed and overrides
10339     //  the variable's predetermined data-sharing attributes.
10340     // OpenMP [2.14.3.6, Restrictions, p.3]
10341     //  Any number of reduction clauses can be specified on the directive,
10342     //  but a list item can appear only once in the reduction clauses for that
10343     //  directive.
10344     DSAStackTy::DSAVarData DVar;
10345     DVar = Stack->getTopDSA(D, false);
10346     if (DVar.CKind == OMPC_reduction) {
10347       S.Diag(ELoc, diag::err_omp_once_referenced)
10348           << getOpenMPClauseName(ClauseKind);
10349       if (DVar.RefExpr)
10350         S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
10351       continue;
10352     } else if (DVar.CKind != OMPC_unknown) {
10353       S.Diag(ELoc, diag::err_omp_wrong_dsa)
10354           << getOpenMPClauseName(DVar.CKind)
10355           << getOpenMPClauseName(OMPC_reduction);
10356       ReportOriginalDSA(S, Stack, D, DVar);
10357       continue;
10358     }
10359 
10360     // OpenMP [2.14.3.6, Restrictions, p.1]
10361     //  A list item that appears in a reduction clause of a worksharing
10362     //  construct must be shared in the parallel regions to which any of the
10363     //  worksharing regions arising from the worksharing construct bind.
10364     OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
10365     if (isOpenMPWorksharingDirective(CurrDir) &&
10366         !isOpenMPParallelDirective(CurrDir) &&
10367         !isOpenMPTeamsDirective(CurrDir)) {
10368       DVar = Stack->getImplicitDSA(D, true);
10369       if (DVar.CKind != OMPC_shared) {
10370         S.Diag(ELoc, diag::err_omp_required_access)
10371             << getOpenMPClauseName(OMPC_reduction)
10372             << getOpenMPClauseName(OMPC_shared);
10373         ReportOriginalDSA(S, Stack, D, DVar);
10374         continue;
10375       }
10376     }
10377 
10378     // Try to find 'declare reduction' corresponding construct before using
10379     // builtin/overloaded operators.
10380     CXXCastPath BasePath;
10381     ExprResult DeclareReductionRef = buildDeclareReductionRef(
10382         S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
10383         ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10384     if (DeclareReductionRef.isInvalid())
10385       continue;
10386     if (S.CurContext->isDependentContext() &&
10387         (DeclareReductionRef.isUnset() ||
10388          isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
10389       RD.push(RefExpr, DeclareReductionRef.get());
10390       continue;
10391     }
10392     if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10393       // Not allowed reduction identifier is found.
10394       S.Diag(ReductionId.getLocStart(),
10395              diag::err_omp_unknown_reduction_identifier)
10396           << Type << ReductionIdRange;
10397       continue;
10398     }
10399 
10400     // OpenMP [2.14.3.6, reduction clause, Restrictions]
10401     // The type of a list item that appears in a reduction clause must be valid
10402     // for the reduction-identifier. For a max or min reduction in C, the type
10403     // of the list item must be an allowed arithmetic data type: char, int,
10404     // float, double, or _Bool, possibly modified with long, short, signed, or
10405     // unsigned. For a max or min reduction in C++, the type of the list item
10406     // must be an allowed arithmetic data type: char, wchar_t, int, float,
10407     // double, or bool, possibly modified with long, short, signed, or unsigned.
10408     if (DeclareReductionRef.isUnset()) {
10409       if ((BOK == BO_GT || BOK == BO_LT) &&
10410           !(Type->isScalarType() ||
10411             (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10412         S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
10413             << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
10414         if (!ASE && !OASE) {
10415           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10416                                    VarDecl::DeclarationOnly;
10417           S.Diag(D->getLocation(),
10418                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10419               << D;
10420         }
10421         continue;
10422       }
10423       if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
10424           !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
10425         S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
10426             << getOpenMPClauseName(ClauseKind);
10427         if (!ASE && !OASE) {
10428           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10429                                    VarDecl::DeclarationOnly;
10430           S.Diag(D->getLocation(),
10431                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10432               << D;
10433         }
10434         continue;
10435       }
10436     }
10437 
10438     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
10439     auto *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
10440                                D->hasAttrs() ? &D->getAttrs() : nullptr);
10441     auto *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
10442                                D->hasAttrs() ? &D->getAttrs() : nullptr);
10443     auto PrivateTy = Type;
10444 
10445     // Try if we can determine constant lengths for all array sections and avoid
10446     // the VLA.
10447     bool ConstantLengthOASE = false;
10448     if (OASE) {
10449       bool SingleElement;
10450       llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
10451       ConstantLengthOASE = CheckOMPArraySectionConstantForReduction(
10452           Context, OASE, SingleElement, ArraySizes);
10453 
10454       // If we don't have a single element, we must emit a constant array type.
10455       if (ConstantLengthOASE && !SingleElement) {
10456         for (auto &Size : ArraySizes) {
10457           PrivateTy = Context.getConstantArrayType(
10458               PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
10459         }
10460       }
10461     }
10462 
10463     if ((OASE && !ConstantLengthOASE) ||
10464         (!OASE && !ASE &&
10465          D->getType().getNonReferenceType()->isVariablyModifiedType())) {
10466       if (!Context.getTargetInfo().isVLASupported() &&
10467           S.shouldDiagnoseTargetSupportFromOpenMP()) {
10468         S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
10469         S.Diag(ELoc, diag::note_vla_unsupported);
10470         continue;
10471       }
10472       // For arrays/array sections only:
10473       // Create pseudo array type for private copy. The size for this array will
10474       // be generated during codegen.
10475       // For array subscripts or single variables Private Ty is the same as Type
10476       // (type of the variable or single array element).
10477       PrivateTy = Context.getVariableArrayType(
10478           Type,
10479           new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
10480           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
10481     } else if (!ASE && !OASE &&
10482                Context.getAsArrayType(D->getType().getNonReferenceType()))
10483       PrivateTy = D->getType().getNonReferenceType();
10484     // Private copy.
10485     auto *PrivateVD =
10486         buildVarDecl(S, ELoc, PrivateTy, D->getName(),
10487                      D->hasAttrs() ? &D->getAttrs() : nullptr,
10488                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
10489     // Add initializer for private variable.
10490     Expr *Init = nullptr;
10491     auto *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
10492     auto *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
10493     if (DeclareReductionRef.isUsable()) {
10494       auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
10495       auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
10496       if (DRD->getInitializer()) {
10497         Init = DRDRef;
10498         RHSVD->setInit(DRDRef);
10499         RHSVD->setInitStyle(VarDecl::CallInit);
10500       }
10501     } else {
10502       switch (BOK) {
10503       case BO_Add:
10504       case BO_Xor:
10505       case BO_Or:
10506       case BO_LOr:
10507         // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
10508         if (Type->isScalarType() || Type->isAnyComplexType())
10509           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
10510         break;
10511       case BO_Mul:
10512       case BO_LAnd:
10513         if (Type->isScalarType() || Type->isAnyComplexType()) {
10514           // '*' and '&&' reduction ops - initializer is '1'.
10515           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
10516         }
10517         break;
10518       case BO_And: {
10519         // '&' reduction op - initializer is '~0'.
10520         QualType OrigType = Type;
10521         if (auto *ComplexTy = OrigType->getAs<ComplexType>())
10522           Type = ComplexTy->getElementType();
10523         if (Type->isRealFloatingType()) {
10524           llvm::APFloat InitValue =
10525               llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
10526                                              /*isIEEE=*/true);
10527           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10528                                          Type, ELoc);
10529         } else if (Type->isScalarType()) {
10530           auto Size = Context.getTypeSize(Type);
10531           QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
10532           llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
10533           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10534         }
10535         if (Init && OrigType->isAnyComplexType()) {
10536           // Init = 0xFFFF + 0xFFFFi;
10537           auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
10538           Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
10539         }
10540         Type = OrigType;
10541         break;
10542       }
10543       case BO_LT:
10544       case BO_GT: {
10545         // 'min' reduction op - initializer is 'Largest representable number in
10546         // the reduction list item type'.
10547         // 'max' reduction op - initializer is 'Least representable number in
10548         // the reduction list item type'.
10549         if (Type->isIntegerType() || Type->isPointerType()) {
10550           bool IsSigned = Type->hasSignedIntegerRepresentation();
10551           auto Size = Context.getTypeSize(Type);
10552           QualType IntTy =
10553               Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
10554           llvm::APInt InitValue =
10555               (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
10556                                         : llvm::APInt::getMinValue(Size)
10557                              : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
10558                                         : llvm::APInt::getMaxValue(Size);
10559           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10560           if (Type->isPointerType()) {
10561             // Cast to pointer type.
10562             auto CastExpr = S.BuildCStyleCastExpr(
10563                 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
10564             if (CastExpr.isInvalid())
10565               continue;
10566             Init = CastExpr.get();
10567           }
10568         } else if (Type->isRealFloatingType()) {
10569           llvm::APFloat InitValue = llvm::APFloat::getLargest(
10570               Context.getFloatTypeSemantics(Type), BOK != BO_LT);
10571           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10572                                          Type, ELoc);
10573         }
10574         break;
10575       }
10576       case BO_PtrMemD:
10577       case BO_PtrMemI:
10578       case BO_MulAssign:
10579       case BO_Div:
10580       case BO_Rem:
10581       case BO_Sub:
10582       case BO_Shl:
10583       case BO_Shr:
10584       case BO_LE:
10585       case BO_GE:
10586       case BO_EQ:
10587       case BO_NE:
10588       case BO_Cmp:
10589       case BO_AndAssign:
10590       case BO_XorAssign:
10591       case BO_OrAssign:
10592       case BO_Assign:
10593       case BO_AddAssign:
10594       case BO_SubAssign:
10595       case BO_DivAssign:
10596       case BO_RemAssign:
10597       case BO_ShlAssign:
10598       case BO_ShrAssign:
10599       case BO_Comma:
10600         llvm_unreachable("Unexpected reduction operation");
10601       }
10602     }
10603     if (Init && DeclareReductionRef.isUnset())
10604       S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
10605     else if (!Init)
10606       S.ActOnUninitializedDecl(RHSVD);
10607     if (RHSVD->isInvalidDecl())
10608       continue;
10609     if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
10610       S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
10611           << Type << ReductionIdRange;
10612       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10613                                VarDecl::DeclarationOnly;
10614       S.Diag(D->getLocation(),
10615              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10616           << D;
10617       continue;
10618     }
10619     // Store initializer for single element in private copy. Will be used during
10620     // codegen.
10621     PrivateVD->setInit(RHSVD->getInit());
10622     PrivateVD->setInitStyle(RHSVD->getInitStyle());
10623     auto *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
10624     ExprResult ReductionOp;
10625     if (DeclareReductionRef.isUsable()) {
10626       QualType RedTy = DeclareReductionRef.get()->getType();
10627       QualType PtrRedTy = Context.getPointerType(RedTy);
10628       ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
10629       ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
10630       if (!BasePath.empty()) {
10631         LHS = S.DefaultLvalueConversion(LHS.get());
10632         RHS = S.DefaultLvalueConversion(RHS.get());
10633         LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10634                                        CK_UncheckedDerivedToBase, LHS.get(),
10635                                        &BasePath, LHS.get()->getValueKind());
10636         RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10637                                        CK_UncheckedDerivedToBase, RHS.get(),
10638                                        &BasePath, RHS.get()->getValueKind());
10639       }
10640       FunctionProtoType::ExtProtoInfo EPI;
10641       QualType Params[] = {PtrRedTy, PtrRedTy};
10642       QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
10643       auto *OVE = new (Context) OpaqueValueExpr(
10644           ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
10645           S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
10646       Expr *Args[] = {LHS.get(), RHS.get()};
10647       ReductionOp = new (Context)
10648           CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
10649     } else {
10650       ReductionOp = S.BuildBinOp(
10651           Stack->getCurScope(), ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
10652       if (ReductionOp.isUsable()) {
10653         if (BOK != BO_LT && BOK != BO_GT) {
10654           ReductionOp =
10655               S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(),
10656                            BO_Assign, LHSDRE, ReductionOp.get());
10657         } else {
10658           auto *ConditionalOp = new (Context)
10659               ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
10660                                   Type, VK_LValue, OK_Ordinary);
10661           ReductionOp =
10662               S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(),
10663                            BO_Assign, LHSDRE, ConditionalOp);
10664         }
10665         if (ReductionOp.isUsable())
10666           ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
10667       }
10668       if (!ReductionOp.isUsable())
10669         continue;
10670     }
10671 
10672     // OpenMP [2.15.4.6, Restrictions, p.2]
10673     // A list item that appears in an in_reduction clause of a task construct
10674     // must appear in a task_reduction clause of a construct associated with a
10675     // taskgroup region that includes the participating task in its taskgroup
10676     // set. The construct associated with the innermost region that meets this
10677     // condition must specify the same reduction-identifier as the in_reduction
10678     // clause.
10679     if (ClauseKind == OMPC_in_reduction) {
10680       SourceRange ParentSR;
10681       BinaryOperatorKind ParentBOK;
10682       const Expr *ParentReductionOp;
10683       Expr *ParentBOKTD, *ParentReductionOpTD;
10684       DSAStackTy::DSAVarData ParentBOKDSA =
10685           Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
10686                                                   ParentBOKTD);
10687       DSAStackTy::DSAVarData ParentReductionOpDSA =
10688           Stack->getTopMostTaskgroupReductionData(
10689               D, ParentSR, ParentReductionOp, ParentReductionOpTD);
10690       bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
10691       bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
10692       if (!IsParentBOK && !IsParentReductionOp) {
10693         S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
10694         continue;
10695       }
10696       if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
10697           (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
10698           IsParentReductionOp) {
10699         bool EmitError = true;
10700         if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
10701           llvm::FoldingSetNodeID RedId, ParentRedId;
10702           ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
10703           DeclareReductionRef.get()->Profile(RedId, Context,
10704                                              /*Canonical=*/true);
10705           EmitError = RedId != ParentRedId;
10706         }
10707         if (EmitError) {
10708           S.Diag(ReductionId.getLocStart(),
10709                  diag::err_omp_reduction_identifier_mismatch)
10710               << ReductionIdRange << RefExpr->getSourceRange();
10711           S.Diag(ParentSR.getBegin(),
10712                  diag::note_omp_previous_reduction_identifier)
10713               << ParentSR
10714               << (IsParentBOK ? ParentBOKDSA.RefExpr
10715                               : ParentReductionOpDSA.RefExpr)
10716                      ->getSourceRange();
10717           continue;
10718         }
10719       }
10720       TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
10721       assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
10722     }
10723 
10724     DeclRefExpr *Ref = nullptr;
10725     Expr *VarsExpr = RefExpr->IgnoreParens();
10726     if (!VD && !S.CurContext->isDependentContext()) {
10727       if (ASE || OASE) {
10728         TransformExprToCaptures RebuildToCapture(S, D);
10729         VarsExpr =
10730             RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
10731         Ref = RebuildToCapture.getCapturedExpr();
10732       } else {
10733         VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
10734       }
10735       if (!S.IsOpenMPCapturedDecl(D)) {
10736         RD.ExprCaptures.emplace_back(Ref->getDecl());
10737         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
10738           ExprResult RefRes = S.DefaultLvalueConversion(Ref);
10739           if (!RefRes.isUsable())
10740             continue;
10741           ExprResult PostUpdateRes =
10742               S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10743                            RefRes.get());
10744           if (!PostUpdateRes.isUsable())
10745             continue;
10746           if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
10747               Stack->getCurrentDirective() == OMPD_taskgroup) {
10748             S.Diag(RefExpr->getExprLoc(),
10749                    diag::err_omp_reduction_non_addressable_expression)
10750                 << RefExpr->getSourceRange();
10751             continue;
10752           }
10753           RD.ExprPostUpdates.emplace_back(
10754               S.IgnoredValueConversions(PostUpdateRes.get()).get());
10755         }
10756       }
10757     }
10758     // All reduction items are still marked as reduction (to do not increase
10759     // code base size).
10760     Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
10761     if (CurrDir == OMPD_taskgroup) {
10762       if (DeclareReductionRef.isUsable())
10763         Stack->addTaskgroupReductionData(D, ReductionIdRange,
10764                                          DeclareReductionRef.get());
10765       else
10766         Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
10767     }
10768     RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
10769             TaskgroupDescriptor);
10770   }
10771   return RD.Vars.empty();
10772 }
10773 
10774 OMPClause *Sema::ActOnOpenMPReductionClause(
10775     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10776     SourceLocation ColonLoc, SourceLocation EndLoc,
10777     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10778     ArrayRef<Expr *> UnresolvedReductions) {
10779   ReductionData RD(VarList.size());
10780 
10781   if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
10782                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
10783                                   ReductionIdScopeSpec, ReductionId,
10784                                   UnresolvedReductions, RD))
10785     return nullptr;
10786 
10787   return OMPReductionClause::Create(
10788       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10789       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10790       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
10791       buildPreInits(Context, RD.ExprCaptures),
10792       buildPostUpdate(*this, RD.ExprPostUpdates));
10793 }
10794 
10795 OMPClause *Sema::ActOnOpenMPTaskReductionClause(
10796     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10797     SourceLocation ColonLoc, SourceLocation EndLoc,
10798     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10799     ArrayRef<Expr *> UnresolvedReductions) {
10800   ReductionData RD(VarList.size());
10801 
10802   if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction,
10803                                   VarList, StartLoc, LParenLoc, ColonLoc,
10804                                   EndLoc, ReductionIdScopeSpec, ReductionId,
10805                                   UnresolvedReductions, RD))
10806     return nullptr;
10807 
10808   return OMPTaskReductionClause::Create(
10809       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10810       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10811       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
10812       buildPreInits(Context, RD.ExprCaptures),
10813       buildPostUpdate(*this, RD.ExprPostUpdates));
10814 }
10815 
10816 OMPClause *Sema::ActOnOpenMPInReductionClause(
10817     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10818     SourceLocation ColonLoc, SourceLocation EndLoc,
10819     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10820     ArrayRef<Expr *> UnresolvedReductions) {
10821   ReductionData RD(VarList.size());
10822 
10823   if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
10824                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
10825                                   ReductionIdScopeSpec, ReductionId,
10826                                   UnresolvedReductions, RD))
10827     return nullptr;
10828 
10829   return OMPInReductionClause::Create(
10830       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10831       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10832       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
10833       buildPreInits(Context, RD.ExprCaptures),
10834       buildPostUpdate(*this, RD.ExprPostUpdates));
10835 }
10836 
10837 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
10838                                      SourceLocation LinLoc) {
10839   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
10840       LinKind == OMPC_LINEAR_unknown) {
10841     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
10842     return true;
10843   }
10844   return false;
10845 }
10846 
10847 bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
10848                                  OpenMPLinearClauseKind LinKind,
10849                                  QualType Type) {
10850   auto *VD = dyn_cast_or_null<VarDecl>(D);
10851   // A variable must not have an incomplete type or a reference type.
10852   if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
10853     return true;
10854   if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
10855       !Type->isReferenceType()) {
10856     Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
10857         << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
10858     return true;
10859   }
10860   Type = Type.getNonReferenceType();
10861 
10862   // A list item must not be const-qualified.
10863   if (Type.isConstant(Context)) {
10864     Diag(ELoc, diag::err_omp_const_variable)
10865         << getOpenMPClauseName(OMPC_linear);
10866     if (D) {
10867       bool IsDecl =
10868           !VD ||
10869           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10870       Diag(D->getLocation(),
10871            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10872           << D;
10873     }
10874     return true;
10875   }
10876 
10877   // A list item must be of integral or pointer type.
10878   Type = Type.getUnqualifiedType().getCanonicalType();
10879   const auto *Ty = Type.getTypePtrOrNull();
10880   if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
10881               !Ty->isPointerType())) {
10882     Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
10883     if (D) {
10884       bool IsDecl =
10885           !VD ||
10886           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10887       Diag(D->getLocation(),
10888            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10889           << D;
10890     }
10891     return true;
10892   }
10893   return false;
10894 }
10895 
10896 OMPClause *Sema::ActOnOpenMPLinearClause(
10897     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
10898     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
10899     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
10900   SmallVector<Expr *, 8> Vars;
10901   SmallVector<Expr *, 8> Privates;
10902   SmallVector<Expr *, 8> Inits;
10903   SmallVector<Decl *, 4> ExprCaptures;
10904   SmallVector<Expr *, 4> ExprPostUpdates;
10905   if (CheckOpenMPLinearModifier(LinKind, LinLoc))
10906     LinKind = OMPC_LINEAR_val;
10907   for (auto &RefExpr : VarList) {
10908     assert(RefExpr && "NULL expr in OpenMP linear clause.");
10909     SourceLocation ELoc;
10910     SourceRange ERange;
10911     Expr *SimpleRefExpr = RefExpr;
10912     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10913                               /*AllowArraySection=*/false);
10914     if (Res.second) {
10915       // It will be analyzed later.
10916       Vars.push_back(RefExpr);
10917       Privates.push_back(nullptr);
10918       Inits.push_back(nullptr);
10919     }
10920     ValueDecl *D = Res.first;
10921     if (!D)
10922       continue;
10923 
10924     QualType Type = D->getType();
10925     auto *VD = dyn_cast<VarDecl>(D);
10926 
10927     // OpenMP [2.14.3.7, linear clause]
10928     //  A list-item cannot appear in more than one linear clause.
10929     //  A list-item that appears in a linear clause cannot appear in any
10930     //  other data-sharing attribute clause.
10931     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
10932     if (DVar.RefExpr) {
10933       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10934                                           << getOpenMPClauseName(OMPC_linear);
10935       ReportOriginalDSA(*this, DSAStack, D, DVar);
10936       continue;
10937     }
10938 
10939     if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
10940       continue;
10941     Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
10942 
10943     // Build private copy of original var.
10944     auto *Private =
10945         buildVarDecl(*this, ELoc, Type, D->getName(),
10946                      D->hasAttrs() ? &D->getAttrs() : nullptr,
10947                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
10948     auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
10949     // Build var to save initial value.
10950     VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
10951     Expr *InitExpr;
10952     DeclRefExpr *Ref = nullptr;
10953     if (!VD && !CurContext->isDependentContext()) {
10954       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
10955       if (!IsOpenMPCapturedDecl(D)) {
10956         ExprCaptures.push_back(Ref->getDecl());
10957         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
10958           ExprResult RefRes = DefaultLvalueConversion(Ref);
10959           if (!RefRes.isUsable())
10960             continue;
10961           ExprResult PostUpdateRes =
10962               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
10963                          SimpleRefExpr, RefRes.get());
10964           if (!PostUpdateRes.isUsable())
10965             continue;
10966           ExprPostUpdates.push_back(
10967               IgnoredValueConversions(PostUpdateRes.get()).get());
10968         }
10969       }
10970     }
10971     if (LinKind == OMPC_LINEAR_uval)
10972       InitExpr = VD ? VD->getInit() : SimpleRefExpr;
10973     else
10974       InitExpr = VD ? SimpleRefExpr : Ref;
10975     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
10976                          /*DirectInit=*/false);
10977     auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
10978 
10979     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
10980     Vars.push_back((VD || CurContext->isDependentContext())
10981                        ? RefExpr->IgnoreParens()
10982                        : Ref);
10983     Privates.push_back(PrivateRef);
10984     Inits.push_back(InitRef);
10985   }
10986 
10987   if (Vars.empty())
10988     return nullptr;
10989 
10990   Expr *StepExpr = Step;
10991   Expr *CalcStepExpr = nullptr;
10992   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
10993       !Step->isInstantiationDependent() &&
10994       !Step->containsUnexpandedParameterPack()) {
10995     SourceLocation StepLoc = Step->getLocStart();
10996     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
10997     if (Val.isInvalid())
10998       return nullptr;
10999     StepExpr = Val.get();
11000 
11001     // Build var to save the step value.
11002     VarDecl *SaveVar =
11003         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
11004     ExprResult SaveRef =
11005         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
11006     ExprResult CalcStep =
11007         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
11008     CalcStep = ActOnFinishFullExpr(CalcStep.get());
11009 
11010     // Warn about zero linear step (it would be probably better specified as
11011     // making corresponding variables 'const').
11012     llvm::APSInt Result;
11013     bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11014     if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
11015       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11016                                                      << (Vars.size() > 1);
11017     if (!IsConstant && CalcStep.isUsable()) {
11018       // Calculate the step beforehand instead of doing this on each iteration.
11019       // (This is not used if the number of iterations may be kfold-ed).
11020       CalcStepExpr = CalcStep.get();
11021     }
11022   }
11023 
11024   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11025                                  ColonLoc, EndLoc, Vars, Privates, Inits,
11026                                  StepExpr, CalcStepExpr,
11027                                  buildPreInits(Context, ExprCaptures),
11028                                  buildPostUpdate(*this, ExprPostUpdates));
11029 }
11030 
11031 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11032                                      Expr *NumIterations, Sema &SemaRef,
11033                                      Scope *S, DSAStackTy *Stack) {
11034   // Walk the vars and build update/final expressions for the CodeGen.
11035   SmallVector<Expr *, 8> Updates;
11036   SmallVector<Expr *, 8> Finals;
11037   Expr *Step = Clause.getStep();
11038   Expr *CalcStep = Clause.getCalcStep();
11039   // OpenMP [2.14.3.7, linear clause]
11040   // If linear-step is not specified it is assumed to be 1.
11041   if (Step == nullptr)
11042     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
11043   else if (CalcStep) {
11044     Step = cast<BinaryOperator>(CalcStep)->getLHS();
11045   }
11046   bool HasErrors = false;
11047   auto CurInit = Clause.inits().begin();
11048   auto CurPrivate = Clause.privates().begin();
11049   auto LinKind = Clause.getModifier();
11050   for (auto &RefExpr : Clause.varlists()) {
11051     SourceLocation ELoc;
11052     SourceRange ERange;
11053     Expr *SimpleRefExpr = RefExpr;
11054     auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
11055                               /*AllowArraySection=*/false);
11056     ValueDecl *D = Res.first;
11057     if (Res.second || !D) {
11058       Updates.push_back(nullptr);
11059       Finals.push_back(nullptr);
11060       HasErrors = true;
11061       continue;
11062     }
11063     auto &&Info = Stack->isLoopControlVariable(D);
11064     // OpenMP [2.15.11, distribute simd Construct]
11065     // A list item may not appear in a linear clause, unless it is the loop
11066     // iteration variable.
11067     if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11068         isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11069       SemaRef.Diag(ELoc,
11070                    diag::err_omp_linear_distribute_var_non_loop_iteration);
11071       Updates.push_back(nullptr);
11072       Finals.push_back(nullptr);
11073       HasErrors = true;
11074       continue;
11075     }
11076     Expr *InitExpr = *CurInit;
11077 
11078     // Build privatized reference to the current linear var.
11079     auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
11080     Expr *CapturedRef;
11081     if (LinKind == OMPC_LINEAR_uval)
11082       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11083     else
11084       CapturedRef =
11085           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11086                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11087                            /*RefersToCapture=*/true);
11088 
11089     // Build update: Var = InitExpr + IV * Step
11090     ExprResult Update;
11091     if (!Info.first) {
11092       Update =
11093           BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
11094                              InitExpr, IV, Step, /* Subtract */ false);
11095     } else
11096       Update = *CurPrivate;
11097     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
11098                                          /*DiscardedValue=*/true);
11099 
11100     // Build final: Var = InitExpr + NumIterations * Step
11101     ExprResult Final;
11102     if (!Info.first) {
11103       Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11104                                  InitExpr, NumIterations, Step,
11105                                  /* Subtract */ false);
11106     } else
11107       Final = *CurPrivate;
11108     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
11109                                         /*DiscardedValue=*/true);
11110 
11111     if (!Update.isUsable() || !Final.isUsable()) {
11112       Updates.push_back(nullptr);
11113       Finals.push_back(nullptr);
11114       HasErrors = true;
11115     } else {
11116       Updates.push_back(Update.get());
11117       Finals.push_back(Final.get());
11118     }
11119     ++CurInit;
11120     ++CurPrivate;
11121   }
11122   Clause.setUpdates(Updates);
11123   Clause.setFinals(Finals);
11124   return HasErrors;
11125 }
11126 
11127 OMPClause *Sema::ActOnOpenMPAlignedClause(
11128     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11129     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
11130 
11131   SmallVector<Expr *, 8> Vars;
11132   for (auto &RefExpr : VarList) {
11133     assert(RefExpr && "NULL expr in OpenMP linear clause.");
11134     SourceLocation ELoc;
11135     SourceRange ERange;
11136     Expr *SimpleRefExpr = RefExpr;
11137     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
11138                               /*AllowArraySection=*/false);
11139     if (Res.second) {
11140       // It will be analyzed later.
11141       Vars.push_back(RefExpr);
11142     }
11143     ValueDecl *D = Res.first;
11144     if (!D)
11145       continue;
11146 
11147     QualType QType = D->getType();
11148     auto *VD = dyn_cast<VarDecl>(D);
11149 
11150     // OpenMP  [2.8.1, simd construct, Restrictions]
11151     // The type of list items appearing in the aligned clause must be
11152     // array, pointer, reference to array, or reference to pointer.
11153     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
11154     const Type *Ty = QType.getTypePtrOrNull();
11155     if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
11156       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
11157           << QType << getLangOpts().CPlusPlus << ERange;
11158       bool IsDecl =
11159           !VD ||
11160           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11161       Diag(D->getLocation(),
11162            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11163           << D;
11164       continue;
11165     }
11166 
11167     // OpenMP  [2.8.1, simd construct, Restrictions]
11168     // A list-item cannot appear in more than one aligned clause.
11169     if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
11170       Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
11171       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11172           << getOpenMPClauseName(OMPC_aligned);
11173       continue;
11174     }
11175 
11176     DeclRefExpr *Ref = nullptr;
11177     if (!VD && IsOpenMPCapturedDecl(D))
11178       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11179     Vars.push_back(DefaultFunctionArrayConversion(
11180                        (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11181                        .get());
11182   }
11183 
11184   // OpenMP [2.8.1, simd construct, Description]
11185   // The parameter of the aligned clause, alignment, must be a constant
11186   // positive integer expression.
11187   // If no optional parameter is specified, implementation-defined default
11188   // alignments for SIMD instructions on the target platforms are assumed.
11189   if (Alignment != nullptr) {
11190     ExprResult AlignResult =
11191         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11192     if (AlignResult.isInvalid())
11193       return nullptr;
11194     Alignment = AlignResult.get();
11195   }
11196   if (Vars.empty())
11197     return nullptr;
11198 
11199   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11200                                   EndLoc, Vars, Alignment);
11201 }
11202 
11203 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11204                                          SourceLocation StartLoc,
11205                                          SourceLocation LParenLoc,
11206                                          SourceLocation EndLoc) {
11207   SmallVector<Expr *, 8> Vars;
11208   SmallVector<Expr *, 8> SrcExprs;
11209   SmallVector<Expr *, 8> DstExprs;
11210   SmallVector<Expr *, 8> AssignmentOps;
11211   for (auto &RefExpr : VarList) {
11212     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11213     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
11214       // It will be analyzed later.
11215       Vars.push_back(RefExpr);
11216       SrcExprs.push_back(nullptr);
11217       DstExprs.push_back(nullptr);
11218       AssignmentOps.push_back(nullptr);
11219       continue;
11220     }
11221 
11222     SourceLocation ELoc = RefExpr->getExprLoc();
11223     // OpenMP [2.1, C/C++]
11224     //  A list item is a variable name.
11225     // OpenMP  [2.14.4.1, Restrictions, p.1]
11226     //  A list item that appears in a copyin clause must be threadprivate.
11227     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
11228     if (!DE || !isa<VarDecl>(DE->getDecl())) {
11229       Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11230           << 0 << RefExpr->getSourceRange();
11231       continue;
11232     }
11233 
11234     Decl *D = DE->getDecl();
11235     VarDecl *VD = cast<VarDecl>(D);
11236 
11237     QualType Type = VD->getType();
11238     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11239       // It will be analyzed later.
11240       Vars.push_back(DE);
11241       SrcExprs.push_back(nullptr);
11242       DstExprs.push_back(nullptr);
11243       AssignmentOps.push_back(nullptr);
11244       continue;
11245     }
11246 
11247     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11248     //  A list item that appears in a copyin clause must be threadprivate.
11249     if (!DSAStack->isThreadPrivate(VD)) {
11250       Diag(ELoc, diag::err_omp_required_access)
11251           << getOpenMPClauseName(OMPC_copyin)
11252           << getOpenMPDirectiveName(OMPD_threadprivate);
11253       continue;
11254     }
11255 
11256     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11257     //  A variable of class type (or array thereof) that appears in a
11258     //  copyin clause requires an accessible, unambiguous copy assignment
11259     //  operator for the class type.
11260     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11261     auto *SrcVD =
11262         buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
11263                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
11264     auto *PseudoSrcExpr = buildDeclRefExpr(
11265         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
11266     auto *DstVD =
11267         buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
11268                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
11269     auto *PseudoDstExpr =
11270         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
11271     // For arrays generate assignment operation for single element and replace
11272     // it by the original array element in CodeGen.
11273     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
11274                                    PseudoDstExpr, PseudoSrcExpr);
11275     if (AssignmentOp.isInvalid())
11276       continue;
11277     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11278                                        /*DiscardedValue=*/true);
11279     if (AssignmentOp.isInvalid())
11280       continue;
11281 
11282     DSAStack->addDSA(VD, DE, OMPC_copyin);
11283     Vars.push_back(DE);
11284     SrcExprs.push_back(PseudoSrcExpr);
11285     DstExprs.push_back(PseudoDstExpr);
11286     AssignmentOps.push_back(AssignmentOp.get());
11287   }
11288 
11289   if (Vars.empty())
11290     return nullptr;
11291 
11292   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11293                                  SrcExprs, DstExprs, AssignmentOps);
11294 }
11295 
11296 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11297                                               SourceLocation StartLoc,
11298                                               SourceLocation LParenLoc,
11299                                               SourceLocation EndLoc) {
11300   SmallVector<Expr *, 8> Vars;
11301   SmallVector<Expr *, 8> SrcExprs;
11302   SmallVector<Expr *, 8> DstExprs;
11303   SmallVector<Expr *, 8> AssignmentOps;
11304   for (auto &RefExpr : VarList) {
11305     assert(RefExpr && "NULL expr in OpenMP linear clause.");
11306     SourceLocation ELoc;
11307     SourceRange ERange;
11308     Expr *SimpleRefExpr = RefExpr;
11309     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
11310                               /*AllowArraySection=*/false);
11311     if (Res.second) {
11312       // It will be analyzed later.
11313       Vars.push_back(RefExpr);
11314       SrcExprs.push_back(nullptr);
11315       DstExprs.push_back(nullptr);
11316       AssignmentOps.push_back(nullptr);
11317     }
11318     ValueDecl *D = Res.first;
11319     if (!D)
11320       continue;
11321 
11322     QualType Type = D->getType();
11323     auto *VD = dyn_cast<VarDecl>(D);
11324 
11325     // OpenMP [2.14.4.2, Restrictions, p.2]
11326     //  A list item that appears in a copyprivate clause may not appear in a
11327     //  private or firstprivate clause on the single construct.
11328     if (!VD || !DSAStack->isThreadPrivate(VD)) {
11329       auto DVar = DSAStack->getTopDSA(D, false);
11330       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11331           DVar.RefExpr) {
11332         Diag(ELoc, diag::err_omp_wrong_dsa)
11333             << getOpenMPClauseName(DVar.CKind)
11334             << getOpenMPClauseName(OMPC_copyprivate);
11335         ReportOriginalDSA(*this, DSAStack, D, DVar);
11336         continue;
11337       }
11338 
11339       // OpenMP [2.11.4.2, Restrictions, p.1]
11340       //  All list items that appear in a copyprivate clause must be either
11341       //  threadprivate or private in the enclosing context.
11342       if (DVar.CKind == OMPC_unknown) {
11343         DVar = DSAStack->getImplicitDSA(D, false);
11344         if (DVar.CKind == OMPC_shared) {
11345           Diag(ELoc, diag::err_omp_required_access)
11346               << getOpenMPClauseName(OMPC_copyprivate)
11347               << "threadprivate or private in the enclosing context";
11348           ReportOriginalDSA(*this, DSAStack, D, DVar);
11349           continue;
11350         }
11351       }
11352     }
11353 
11354     // Variably modified types are not supported.
11355     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
11356       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
11357           << getOpenMPClauseName(OMPC_copyprivate) << Type
11358           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
11359       bool IsDecl =
11360           !VD ||
11361           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11362       Diag(D->getLocation(),
11363            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11364           << D;
11365       continue;
11366     }
11367 
11368     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11369     //  A variable of class type (or array thereof) that appears in a
11370     //  copyin clause requires an accessible, unambiguous copy assignment
11371     //  operator for the class type.
11372     Type = Context.getBaseElementType(Type.getNonReferenceType())
11373                .getUnqualifiedType();
11374     auto *SrcVD =
11375         buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
11376                      D->hasAttrs() ? &D->getAttrs() : nullptr);
11377     auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
11378     auto *DstVD =
11379         buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
11380                      D->hasAttrs() ? &D->getAttrs() : nullptr);
11381     auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11382     auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11383                                    PseudoDstExpr, PseudoSrcExpr);
11384     if (AssignmentOp.isInvalid())
11385       continue;
11386     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
11387                                        /*DiscardedValue=*/true);
11388     if (AssignmentOp.isInvalid())
11389       continue;
11390 
11391     // No need to mark vars as copyprivate, they are already threadprivate or
11392     // implicitly private.
11393     assert(VD || IsOpenMPCapturedDecl(D));
11394     Vars.push_back(
11395         VD ? RefExpr->IgnoreParens()
11396            : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
11397     SrcExprs.push_back(PseudoSrcExpr);
11398     DstExprs.push_back(PseudoDstExpr);
11399     AssignmentOps.push_back(AssignmentOp.get());
11400   }
11401 
11402   if (Vars.empty())
11403     return nullptr;
11404 
11405   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11406                                       Vars, SrcExprs, DstExprs, AssignmentOps);
11407 }
11408 
11409 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11410                                         SourceLocation StartLoc,
11411                                         SourceLocation LParenLoc,
11412                                         SourceLocation EndLoc) {
11413   if (VarList.empty())
11414     return nullptr;
11415 
11416   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11417 }
11418 
11419 OMPClause *
11420 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
11421                               SourceLocation DepLoc, SourceLocation ColonLoc,
11422                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11423                               SourceLocation LParenLoc, SourceLocation EndLoc) {
11424   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
11425       DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
11426     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
11427         << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
11428     return nullptr;
11429   }
11430   if (DSAStack->getCurrentDirective() != OMPD_ordered &&
11431       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
11432        DepKind == OMPC_DEPEND_sink)) {
11433     unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
11434     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
11435         << getListOfPossibleValues(OMPC_depend, /*First=*/0,
11436                                    /*Last=*/OMPC_DEPEND_unknown, Except)
11437         << getOpenMPClauseName(OMPC_depend);
11438     return nullptr;
11439   }
11440   SmallVector<Expr *, 8> Vars;
11441   DSAStackTy::OperatorOffsetTy OpsOffs;
11442   llvm::APSInt DepCounter(/*BitWidth=*/32);
11443   llvm::APSInt TotalDepCount(/*BitWidth=*/32);
11444   if (DepKind == OMPC_DEPEND_sink) {
11445     if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
11446       TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
11447       TotalDepCount.setIsUnsigned(/*Val=*/true);
11448     }
11449   }
11450   for (auto &RefExpr : VarList) {
11451     assert(RefExpr && "NULL expr in OpenMP shared clause.");
11452     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
11453       // It will be analyzed later.
11454       Vars.push_back(RefExpr);
11455       continue;
11456     }
11457 
11458     SourceLocation ELoc = RefExpr->getExprLoc();
11459     auto *SimpleExpr = RefExpr->IgnoreParenCasts();
11460     if (DepKind == OMPC_DEPEND_sink) {
11461       if (DSAStack->getParentOrderedRegionParam() &&
11462           DepCounter >= TotalDepCount) {
11463         Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
11464         continue;
11465       }
11466       ++DepCounter;
11467       // OpenMP  [2.13.9, Summary]
11468       // depend(dependence-type : vec), where dependence-type is:
11469       // 'sink' and where vec is the iteration vector, which has the form:
11470       //  x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
11471       // where n is the value specified by the ordered clause in the loop
11472       // directive, xi denotes the loop iteration variable of the i-th nested
11473       // loop associated with the loop directive, and di is a constant
11474       // non-negative integer.
11475       if (CurContext->isDependentContext()) {
11476         // It will be analyzed later.
11477         Vars.push_back(RefExpr);
11478         continue;
11479       }
11480       SimpleExpr = SimpleExpr->IgnoreImplicit();
11481       OverloadedOperatorKind OOK = OO_None;
11482       SourceLocation OOLoc;
11483       Expr *LHS = SimpleExpr;
11484       Expr *RHS = nullptr;
11485       if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
11486         OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
11487         OOLoc = BO->getOperatorLoc();
11488         LHS = BO->getLHS()->IgnoreParenImpCasts();
11489         RHS = BO->getRHS()->IgnoreParenImpCasts();
11490       } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
11491         OOK = OCE->getOperator();
11492         OOLoc = OCE->getOperatorLoc();
11493         LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11494         RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
11495       } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
11496         OOK = MCE->getMethodDecl()
11497                   ->getNameInfo()
11498                   .getName()
11499                   .getCXXOverloadedOperator();
11500         OOLoc = MCE->getCallee()->getExprLoc();
11501         LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
11502         RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11503       }
11504       SourceLocation ELoc;
11505       SourceRange ERange;
11506       auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
11507                                 /*AllowArraySection=*/false);
11508       if (Res.second) {
11509         // It will be analyzed later.
11510         Vars.push_back(RefExpr);
11511       }
11512       ValueDecl *D = Res.first;
11513       if (!D)
11514         continue;
11515 
11516       if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
11517         Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
11518         continue;
11519       }
11520       if (RHS) {
11521         ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
11522             RHS, OMPC_depend, /*StrictlyPositive=*/false);
11523         if (RHSRes.isInvalid())
11524           continue;
11525       }
11526       if (!CurContext->isDependentContext() &&
11527           DSAStack->getParentOrderedRegionParam() &&
11528           DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
11529         ValueDecl *VD =
11530             DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
11531         if (VD) {
11532           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
11533               << 1 << VD;
11534         } else {
11535           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
11536         }
11537         continue;
11538       }
11539       OpsOffs.push_back({RHS, OOK});
11540     } else {
11541       auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
11542       if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
11543           (ASE &&
11544            !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
11545            !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
11546         Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11547             << RefExpr->getSourceRange();
11548         continue;
11549       }
11550       bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
11551       getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
11552       ExprResult Res =
11553           CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
11554       getDiagnostics().setSuppressAllDiagnostics(Suppress);
11555       if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
11556         Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11557             << RefExpr->getSourceRange();
11558         continue;
11559       }
11560     }
11561     Vars.push_back(RefExpr->IgnoreParenImpCasts());
11562   }
11563 
11564   if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
11565       TotalDepCount > VarList.size() &&
11566       DSAStack->getParentOrderedRegionParam() &&
11567       DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
11568     Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
11569         << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
11570   }
11571   if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
11572       Vars.empty())
11573     return nullptr;
11574 
11575   auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11576                                     DepKind, DepLoc, ColonLoc, Vars);
11577   if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
11578       DSAStack->isParentOrderedRegion())
11579     DSAStack->addDoacrossDependClause(C, OpsOffs);
11580   return C;
11581 }
11582 
11583 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
11584                                          SourceLocation LParenLoc,
11585                                          SourceLocation EndLoc) {
11586   Expr *ValExpr = Device;
11587   Stmt *HelperValStmt = nullptr;
11588 
11589   // OpenMP [2.9.1, Restrictions]
11590   // The device expression must evaluate to a non-negative integer value.
11591   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
11592                                  /*StrictlyPositive=*/false))
11593     return nullptr;
11594 
11595   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
11596   OpenMPDirectiveKind CaptureRegion =
11597       getOpenMPCaptureRegionForClause(DKind, OMPC_device);
11598   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
11599     ValExpr = MakeFullExpr(ValExpr).get();
11600     llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11601     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11602     HelperValStmt = buildPreInits(Context, Captures);
11603   }
11604 
11605   return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
11606                                        StartLoc, LParenLoc, EndLoc);
11607 }
11608 
11609 static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
11610                               DSAStackTy *Stack, QualType QTy,
11611                               bool FullCheck = true) {
11612   NamedDecl *ND;
11613   if (QTy->isIncompleteType(&ND)) {
11614     SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
11615     return false;
11616   }
11617   if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
11618       !QTy.isTrivialType(SemaRef.Context))
11619     SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
11620   return true;
11621 }
11622 
11623 /// \brief Return true if it can be proven that the provided array expression
11624 /// (array section or array subscript) does NOT specify the whole size of the
11625 /// array whose base type is \a BaseQTy.
11626 static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
11627                                                         const Expr *E,
11628                                                         QualType BaseQTy) {
11629   auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
11630 
11631   // If this is an array subscript, it refers to the whole size if the size of
11632   // the dimension is constant and equals 1. Also, an array section assumes the
11633   // format of an array subscript if no colon is used.
11634   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
11635     if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
11636       return ATy->getSize().getSExtValue() != 1;
11637     // Size can't be evaluated statically.
11638     return false;
11639   }
11640 
11641   assert(OASE && "Expecting array section if not an array subscript.");
11642   auto *LowerBound = OASE->getLowerBound();
11643   auto *Length = OASE->getLength();
11644 
11645   // If there is a lower bound that does not evaluates to zero, we are not
11646   // covering the whole dimension.
11647   if (LowerBound) {
11648     llvm::APSInt ConstLowerBound;
11649     if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
11650       return false; // Can't get the integer value as a constant.
11651     if (ConstLowerBound.getSExtValue())
11652       return true;
11653   }
11654 
11655   // If we don't have a length we covering the whole dimension.
11656   if (!Length)
11657     return false;
11658 
11659   // If the base is a pointer, we don't have a way to get the size of the
11660   // pointee.
11661   if (BaseQTy->isPointerType())
11662     return false;
11663 
11664   // We can only check if the length is the same as the size of the dimension
11665   // if we have a constant array.
11666   auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
11667   if (!CATy)
11668     return false;
11669 
11670   llvm::APSInt ConstLength;
11671   if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11672     return false; // Can't get the integer value as a constant.
11673 
11674   return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
11675 }
11676 
11677 // Return true if it can be proven that the provided array expression (array
11678 // section or array subscript) does NOT specify a single element of the array
11679 // whose base type is \a BaseQTy.
11680 static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
11681                                                         const Expr *E,
11682                                                         QualType BaseQTy) {
11683   auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
11684 
11685   // An array subscript always refer to a single element. Also, an array section
11686   // assumes the format of an array subscript if no colon is used.
11687   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
11688     return false;
11689 
11690   assert(OASE && "Expecting array section if not an array subscript.");
11691   auto *Length = OASE->getLength();
11692 
11693   // If we don't have a length we have to check if the array has unitary size
11694   // for this dimension. Also, we should always expect a length if the base type
11695   // is pointer.
11696   if (!Length) {
11697     if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
11698       return ATy->getSize().getSExtValue() != 1;
11699     // We cannot assume anything.
11700     return false;
11701   }
11702 
11703   // Check if the length evaluates to 1.
11704   llvm::APSInt ConstLength;
11705   if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11706     return false; // Can't get the integer value as a constant.
11707 
11708   return ConstLength.getSExtValue() != 1;
11709 }
11710 
11711 // Return the expression of the base of the mappable expression or null if it
11712 // cannot be determined and do all the necessary checks to see if the expression
11713 // is valid as a standalone mappable expression. In the process, record all the
11714 // components of the expression.
11715 static Expr *CheckMapClauseExpressionBase(
11716     Sema &SemaRef, Expr *E,
11717     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
11718     OpenMPClauseKind CKind, bool NoDiagnose) {
11719   SourceLocation ELoc = E->getExprLoc();
11720   SourceRange ERange = E->getSourceRange();
11721 
11722   // The base of elements of list in a map clause have to be either:
11723   //  - a reference to variable or field.
11724   //  - a member expression.
11725   //  - an array expression.
11726   //
11727   // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
11728   // reference to 'r'.
11729   //
11730   // If we have:
11731   //
11732   // struct SS {
11733   //   Bla S;
11734   //   foo() {
11735   //     #pragma omp target map (S.Arr[:12]);
11736   //   }
11737   // }
11738   //
11739   // We want to retrieve the member expression 'this->S';
11740 
11741   Expr *RelevantExpr = nullptr;
11742 
11743   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
11744   //  If a list item is an array section, it must specify contiguous storage.
11745   //
11746   // For this restriction it is sufficient that we make sure only references
11747   // to variables or fields and array expressions, and that no array sections
11748   // exist except in the rightmost expression (unless they cover the whole
11749   // dimension of the array). E.g. these would be invalid:
11750   //
11751   //   r.ArrS[3:5].Arr[6:7]
11752   //
11753   //   r.ArrS[3:5].x
11754   //
11755   // but these would be valid:
11756   //   r.ArrS[3].Arr[6:7]
11757   //
11758   //   r.ArrS[3].x
11759 
11760   bool AllowUnitySizeArraySection = true;
11761   bool AllowWholeSizeArraySection = true;
11762 
11763   while (!RelevantExpr) {
11764     E = E->IgnoreParenImpCasts();
11765 
11766     if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
11767       if (!isa<VarDecl>(CurE->getDecl()))
11768         return nullptr;
11769 
11770       RelevantExpr = CurE;
11771 
11772       // If we got a reference to a declaration, we should not expect any array
11773       // section before that.
11774       AllowUnitySizeArraySection = false;
11775       AllowWholeSizeArraySection = false;
11776 
11777       // Record the component.
11778       CurComponents.emplace_back(CurE, CurE->getDecl());
11779     } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
11780       auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
11781 
11782       if (isa<CXXThisExpr>(BaseE))
11783         // We found a base expression: this->Val.
11784         RelevantExpr = CurE;
11785       else
11786         E = BaseE;
11787 
11788       if (!isa<FieldDecl>(CurE->getMemberDecl())) {
11789         if (!NoDiagnose) {
11790           SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
11791               << CurE->getSourceRange();
11792           return nullptr;
11793         }
11794         if (RelevantExpr)
11795           return nullptr;
11796         continue;
11797       }
11798 
11799       auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
11800 
11801       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
11802       //  A bit-field cannot appear in a map clause.
11803       //
11804       if (FD->isBitField()) {
11805         if (!NoDiagnose) {
11806           SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
11807               << CurE->getSourceRange() << getOpenMPClauseName(CKind);
11808           return nullptr;
11809         }
11810         if (RelevantExpr)
11811           return nullptr;
11812         continue;
11813       }
11814 
11815       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11816       //  If the type of a list item is a reference to a type T then the type
11817       //  will be considered to be T for all purposes of this clause.
11818       QualType CurType = BaseE->getType().getNonReferenceType();
11819 
11820       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
11821       //  A list item cannot be a variable that is a member of a structure with
11822       //  a union type.
11823       //
11824       if (auto *RT = CurType->getAs<RecordType>()) {
11825         if (RT->isUnionType()) {
11826           if (!NoDiagnose) {
11827             SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
11828                 << CurE->getSourceRange();
11829             return nullptr;
11830           }
11831           continue;
11832         }
11833       }
11834 
11835       // If we got a member expression, we should not expect any array section
11836       // before that:
11837       //
11838       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
11839       //  If a list item is an element of a structure, only the rightmost symbol
11840       //  of the variable reference can be an array section.
11841       //
11842       AllowUnitySizeArraySection = false;
11843       AllowWholeSizeArraySection = false;
11844 
11845       // Record the component.
11846       CurComponents.emplace_back(CurE, FD);
11847     } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
11848       E = CurE->getBase()->IgnoreParenImpCasts();
11849 
11850       if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
11851         if (!NoDiagnose) {
11852           SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
11853               << 0 << CurE->getSourceRange();
11854           return nullptr;
11855         }
11856         continue;
11857       }
11858 
11859       // If we got an array subscript that express the whole dimension we
11860       // can have any array expressions before. If it only expressing part of
11861       // the dimension, we can only have unitary-size array expressions.
11862       if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
11863                                                       E->getType()))
11864         AllowWholeSizeArraySection = false;
11865 
11866       // Record the component - we don't have any declaration associated.
11867       CurComponents.emplace_back(CurE, nullptr);
11868     } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
11869       assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
11870       E = CurE->getBase()->IgnoreParenImpCasts();
11871 
11872       QualType CurType =
11873           OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
11874 
11875       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11876       //  If the type of a list item is a reference to a type T then the type
11877       //  will be considered to be T for all purposes of this clause.
11878       if (CurType->isReferenceType())
11879         CurType = CurType->getPointeeType();
11880 
11881       bool IsPointer = CurType->isAnyPointerType();
11882 
11883       if (!IsPointer && !CurType->isArrayType()) {
11884         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
11885             << 0 << CurE->getSourceRange();
11886         return nullptr;
11887       }
11888 
11889       bool NotWhole =
11890           CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
11891       bool NotUnity =
11892           CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
11893 
11894       if (AllowWholeSizeArraySection) {
11895         // Any array section is currently allowed. Allowing a whole size array
11896         // section implies allowing a unity array section as well.
11897         //
11898         // If this array section refers to the whole dimension we can still
11899         // accept other array sections before this one, except if the base is a
11900         // pointer. Otherwise, only unitary sections are accepted.
11901         if (NotWhole || IsPointer)
11902           AllowWholeSizeArraySection = false;
11903       } else if (AllowUnitySizeArraySection && NotUnity) {
11904         // A unity or whole array section is not allowed and that is not
11905         // compatible with the properties of the current array section.
11906         SemaRef.Diag(
11907             ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
11908             << CurE->getSourceRange();
11909         return nullptr;
11910       }
11911 
11912       // Record the component - we don't have any declaration associated.
11913       CurComponents.emplace_back(CurE, nullptr);
11914     } else {
11915       if (!NoDiagnose) {
11916         // If nothing else worked, this is not a valid map clause expression.
11917         SemaRef.Diag(
11918             ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
11919             << ERange;
11920       }
11921       return nullptr;
11922     }
11923   }
11924 
11925   return RelevantExpr;
11926 }
11927 
11928 // Return true if expression E associated with value VD has conflicts with other
11929 // map information.
11930 static bool CheckMapConflicts(
11931     Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
11932     bool CurrentRegionOnly,
11933     OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
11934     OpenMPClauseKind CKind) {
11935   assert(VD && E);
11936   SourceLocation ELoc = E->getExprLoc();
11937   SourceRange ERange = E->getSourceRange();
11938 
11939   // In order to easily check the conflicts we need to match each component of
11940   // the expression under test with the components of the expressions that are
11941   // already in the stack.
11942 
11943   assert(!CurComponents.empty() && "Map clause expression with no components!");
11944   assert(CurComponents.back().getAssociatedDeclaration() == VD &&
11945          "Map clause expression with unexpected base!");
11946 
11947   // Variables to help detecting enclosing problems in data environment nests.
11948   bool IsEnclosedByDataEnvironmentExpr = false;
11949   const Expr *EnclosingExpr = nullptr;
11950 
11951   bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
11952       VD, CurrentRegionOnly,
11953       [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
11954               StackComponents,
11955           OpenMPClauseKind) -> bool {
11956 
11957         assert(!StackComponents.empty() &&
11958                "Map clause expression with no components!");
11959         assert(StackComponents.back().getAssociatedDeclaration() == VD &&
11960                "Map clause expression with unexpected base!");
11961 
11962         // The whole expression in the stack.
11963         auto *RE = StackComponents.front().getAssociatedExpression();
11964 
11965         // Expressions must start from the same base. Here we detect at which
11966         // point both expressions diverge from each other and see if we can
11967         // detect if the memory referred to both expressions is contiguous and
11968         // do not overlap.
11969         auto CI = CurComponents.rbegin();
11970         auto CE = CurComponents.rend();
11971         auto SI = StackComponents.rbegin();
11972         auto SE = StackComponents.rend();
11973         for (; CI != CE && SI != SE; ++CI, ++SI) {
11974 
11975           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
11976           //  At most one list item can be an array item derived from a given
11977           //  variable in map clauses of the same construct.
11978           if (CurrentRegionOnly &&
11979               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
11980                isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
11981               (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
11982                isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
11983             SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
11984                          diag::err_omp_multiple_array_items_in_map_clause)
11985                 << CI->getAssociatedExpression()->getSourceRange();
11986             SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
11987                          diag::note_used_here)
11988                 << SI->getAssociatedExpression()->getSourceRange();
11989             return true;
11990           }
11991 
11992           // Do both expressions have the same kind?
11993           if (CI->getAssociatedExpression()->getStmtClass() !=
11994               SI->getAssociatedExpression()->getStmtClass())
11995             break;
11996 
11997           // Are we dealing with different variables/fields?
11998           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
11999             break;
12000         }
12001         // Check if the extra components of the expressions in the enclosing
12002         // data environment are redundant for the current base declaration.
12003         // If they are, the maps completely overlap, which is legal.
12004         for (; SI != SE; ++SI) {
12005           QualType Type;
12006           if (auto *ASE =
12007                   dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
12008             Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
12009           } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>(
12010                          SI->getAssociatedExpression())) {
12011             auto *E = OASE->getBase()->IgnoreParenImpCasts();
12012             Type =
12013                 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12014           }
12015           if (Type.isNull() || Type->isAnyPointerType() ||
12016               CheckArrayExpressionDoesNotReferToWholeSize(
12017                   SemaRef, SI->getAssociatedExpression(), Type))
12018             break;
12019         }
12020 
12021         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12022         //  List items of map clauses in the same construct must not share
12023         //  original storage.
12024         //
12025         // If the expressions are exactly the same or one is a subset of the
12026         // other, it means they are sharing storage.
12027         if (CI == CE && SI == SE) {
12028           if (CurrentRegionOnly) {
12029             if (CKind == OMPC_map)
12030               SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
12031             else {
12032               assert(CKind == OMPC_to || CKind == OMPC_from);
12033               SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12034                   << ERange;
12035             }
12036             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12037                 << RE->getSourceRange();
12038             return true;
12039           } else {
12040             // If we find the same expression in the enclosing data environment,
12041             // that is legal.
12042             IsEnclosedByDataEnvironmentExpr = true;
12043             return false;
12044           }
12045         }
12046 
12047         QualType DerivedType =
12048             std::prev(CI)->getAssociatedDeclaration()->getType();
12049         SourceLocation DerivedLoc =
12050             std::prev(CI)->getAssociatedExpression()->getExprLoc();
12051 
12052         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12053         //  If the type of a list item is a reference to a type T then the type
12054         //  will be considered to be T for all purposes of this clause.
12055         DerivedType = DerivedType.getNonReferenceType();
12056 
12057         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12058         //  A variable for which the type is pointer and an array section
12059         //  derived from that variable must not appear as list items of map
12060         //  clauses of the same construct.
12061         //
12062         // Also, cover one of the cases in:
12063         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12064         //  If any part of the original storage of a list item has corresponding
12065         //  storage in the device data environment, all of the original storage
12066         //  must have corresponding storage in the device data environment.
12067         //
12068         if (DerivedType->isAnyPointerType()) {
12069           if (CI == CE || SI == SE) {
12070             SemaRef.Diag(
12071                 DerivedLoc,
12072                 diag::err_omp_pointer_mapped_along_with_derived_section)
12073                 << DerivedLoc;
12074             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12075                 << RE->getSourceRange();
12076             return true;
12077           } else if (CI->getAssociatedExpression()->getStmtClass() !=
12078                          SI->getAssociatedExpression()->getStmtClass() ||
12079                      CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12080                          SI->getAssociatedDeclaration()->getCanonicalDecl()) {
12081             assert(CI != CE && SI != SE);
12082             SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
12083                 << DerivedLoc;
12084             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12085                 << RE->getSourceRange();
12086             return true;
12087           }
12088         }
12089 
12090         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12091         //  List items of map clauses in the same construct must not share
12092         //  original storage.
12093         //
12094         // An expression is a subset of the other.
12095         if (CurrentRegionOnly && (CI == CE || SI == SE)) {
12096           if (CKind == OMPC_map)
12097             SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
12098           else {
12099             assert(CKind == OMPC_to || CKind == OMPC_from);
12100             SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12101                 << ERange;
12102           }
12103           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12104               << RE->getSourceRange();
12105           return true;
12106         }
12107 
12108         // The current expression uses the same base as other expression in the
12109         // data environment but does not contain it completely.
12110         if (!CurrentRegionOnly && SI != SE)
12111           EnclosingExpr = RE;
12112 
12113         // The current expression is a subset of the expression in the data
12114         // environment.
12115         IsEnclosedByDataEnvironmentExpr |=
12116             (!CurrentRegionOnly && CI != CE && SI == SE);
12117 
12118         return false;
12119       });
12120 
12121   if (CurrentRegionOnly)
12122     return FoundError;
12123 
12124   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12125   //  If any part of the original storage of a list item has corresponding
12126   //  storage in the device data environment, all of the original storage must
12127   //  have corresponding storage in the device data environment.
12128   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12129   //  If a list item is an element of a structure, and a different element of
12130   //  the structure has a corresponding list item in the device data environment
12131   //  prior to a task encountering the construct associated with the map clause,
12132   //  then the list item must also have a corresponding list item in the device
12133   //  data environment prior to the task encountering the construct.
12134   //
12135   if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12136     SemaRef.Diag(ELoc,
12137                  diag::err_omp_original_storage_is_shared_and_does_not_contain)
12138         << ERange;
12139     SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12140         << EnclosingExpr->getSourceRange();
12141     return true;
12142   }
12143 
12144   return FoundError;
12145 }
12146 
12147 namespace {
12148 // Utility struct that gathers all the related lists associated with a mappable
12149 // expression.
12150 struct MappableVarListInfo final {
12151   // The list of expressions.
12152   ArrayRef<Expr *> VarList;
12153   // The list of processed expressions.
12154   SmallVector<Expr *, 16> ProcessedVarList;
12155   // The mappble components for each expression.
12156   OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12157   // The base declaration of the variable.
12158   SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12159 
12160   MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12161     // We have a list of components and base declarations for each entry in the
12162     // variable list.
12163     VarComponents.reserve(VarList.size());
12164     VarBaseDeclarations.reserve(VarList.size());
12165   }
12166 };
12167 }
12168 
12169 // Check the validity of the provided variable list for the provided clause kind
12170 // \a CKind. In the check process the valid expressions, and mappable expression
12171 // components and variables are extracted and used to fill \a Vars,
12172 // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12173 // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12174 static void
12175 checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12176                             OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12177                             SourceLocation StartLoc,
12178                             OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12179                             bool IsMapTypeImplicit = false) {
12180   // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12181   assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
12182          "Unexpected clause kind with mappable expressions!");
12183 
12184   // Keep track of the mappable components and base declarations in this clause.
12185   // Each entry in the list is going to have a list of components associated. We
12186   // record each set of the components so that we can build the clause later on.
12187   // In the end we should have the same amount of declarations and component
12188   // lists.
12189 
12190   for (auto &RE : MVLI.VarList) {
12191     assert(RE && "Null expr in omp to/from/map clause");
12192     SourceLocation ELoc = RE->getExprLoc();
12193 
12194     auto *VE = RE->IgnoreParenLValueCasts();
12195 
12196     if (VE->isValueDependent() || VE->isTypeDependent() ||
12197         VE->isInstantiationDependent() ||
12198         VE->containsUnexpandedParameterPack()) {
12199       // We can only analyze this information once the missing information is
12200       // resolved.
12201       MVLI.ProcessedVarList.push_back(RE);
12202       continue;
12203     }
12204 
12205     auto *SimpleExpr = RE->IgnoreParenCasts();
12206 
12207     if (!RE->IgnoreParenImpCasts()->isLValue()) {
12208       SemaRef.Diag(ELoc,
12209                    diag::err_omp_expected_named_var_member_or_array_expression)
12210           << RE->getSourceRange();
12211       continue;
12212     }
12213 
12214     OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12215     ValueDecl *CurDeclaration = nullptr;
12216 
12217     // Obtain the array or member expression bases if required. Also, fill the
12218     // components array with all the components identified in the process.
12219     auto *BE = CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents,
12220                                             CKind, /*NoDiagnose=*/false);
12221     if (!BE)
12222       continue;
12223 
12224     assert(!CurComponents.empty() &&
12225            "Invalid mappable expression information.");
12226 
12227     // For the following checks, we rely on the base declaration which is
12228     // expected to be associated with the last component. The declaration is
12229     // expected to be a variable or a field (if 'this' is being mapped).
12230     CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12231     assert(CurDeclaration && "Null decl on map clause.");
12232     assert(
12233         CurDeclaration->isCanonicalDecl() &&
12234         "Expecting components to have associated only canonical declarations.");
12235 
12236     auto *VD = dyn_cast<VarDecl>(CurDeclaration);
12237     auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
12238 
12239     assert((VD || FD) && "Only variables or fields are expected here!");
12240     (void)FD;
12241 
12242     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
12243     // threadprivate variables cannot appear in a map clause.
12244     // OpenMP 4.5 [2.10.5, target update Construct]
12245     // threadprivate variables cannot appear in a from clause.
12246     if (VD && DSAS->isThreadPrivate(VD)) {
12247       auto DVar = DSAS->getTopDSA(VD, false);
12248       SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12249           << getOpenMPClauseName(CKind);
12250       ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
12251       continue;
12252     }
12253 
12254     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12255     //  A list item cannot appear in both a map clause and a data-sharing
12256     //  attribute clause on the same construct.
12257 
12258     // Check conflicts with other map clause expressions. We check the conflicts
12259     // with the current construct separately from the enclosing data
12260     // environment, because the restrictions are different. We only have to
12261     // check conflicts across regions for the map clauses.
12262     if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
12263                           /*CurrentRegionOnly=*/true, CurComponents, CKind))
12264       break;
12265     if (CKind == OMPC_map &&
12266         CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
12267                           /*CurrentRegionOnly=*/false, CurComponents, CKind))
12268       break;
12269 
12270     // OpenMP 4.5 [2.10.5, target update Construct]
12271     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12272     //  If the type of a list item is a reference to a type T then the type will
12273     //  be considered to be T for all purposes of this clause.
12274     QualType Type = CurDeclaration->getType().getNonReferenceType();
12275 
12276     // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12277     // A list item in a to or from clause must have a mappable type.
12278     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12279     //  A list item must have a mappable type.
12280     if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
12281                            DSAS, Type))
12282       continue;
12283 
12284     if (CKind == OMPC_map) {
12285       // target enter data
12286       // OpenMP [2.10.2, Restrictions, p. 99]
12287       // A map-type must be specified in all map clauses and must be either
12288       // to or alloc.
12289       OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12290       if (DKind == OMPD_target_enter_data &&
12291           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12292         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12293             << (IsMapTypeImplicit ? 1 : 0)
12294             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12295             << getOpenMPDirectiveName(DKind);
12296         continue;
12297       }
12298 
12299       // target exit_data
12300       // OpenMP [2.10.3, Restrictions, p. 102]
12301       // A map-type must be specified in all map clauses and must be either
12302       // from, release, or delete.
12303       if (DKind == OMPD_target_exit_data &&
12304           !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12305             MapType == OMPC_MAP_delete)) {
12306         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12307             << (IsMapTypeImplicit ? 1 : 0)
12308             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12309             << getOpenMPDirectiveName(DKind);
12310         continue;
12311       }
12312 
12313       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12314       // A list item cannot appear in both a map clause and a data-sharing
12315       // attribute clause on the same construct
12316       if ((DKind == OMPD_target || DKind == OMPD_target_teams ||
12317            DKind == OMPD_target_teams_distribute ||
12318            DKind == OMPD_target_teams_distribute_parallel_for ||
12319            DKind == OMPD_target_teams_distribute_parallel_for_simd ||
12320            DKind == OMPD_target_teams_distribute_simd) && VD) {
12321         auto DVar = DSAS->getTopDSA(VD, false);
12322         if (isOpenMPPrivate(DVar.CKind)) {
12323           SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
12324               << getOpenMPClauseName(DVar.CKind)
12325               << getOpenMPClauseName(OMPC_map)
12326               << getOpenMPDirectiveName(DSAS->getCurrentDirective());
12327           ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
12328           continue;
12329         }
12330       }
12331     }
12332 
12333     // Save the current expression.
12334     MVLI.ProcessedVarList.push_back(RE);
12335 
12336     // Store the components in the stack so that they can be used to check
12337     // against other clauses later on.
12338     DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12339                                           /*WhereFoundClauseKind=*/OMPC_map);
12340 
12341     // Save the components and declaration to create the clause. For purposes of
12342     // the clause creation, any component list that has has base 'this' uses
12343     // null as base declaration.
12344     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12345     MVLI.VarComponents.back().append(CurComponents.begin(),
12346                                      CurComponents.end());
12347     MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12348                                                            : CurDeclaration);
12349   }
12350 }
12351 
12352 OMPClause *
12353 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12354                            OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12355                            SourceLocation MapLoc, SourceLocation ColonLoc,
12356                            ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12357                            SourceLocation LParenLoc, SourceLocation EndLoc) {
12358   MappableVarListInfo MVLI(VarList);
12359   checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12360                               MapType, IsMapTypeImplicit);
12361 
12362   // We need to produce a map clause even if we don't have variables so that
12363   // other diagnostics related with non-existing map clauses are accurate.
12364   return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12365                               MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12366                               MVLI.VarComponents, MapTypeModifier, MapType,
12367                               IsMapTypeImplicit, MapLoc);
12368 }
12369 
12370 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12371                                                TypeResult ParsedType) {
12372   assert(ParsedType.isUsable());
12373 
12374   QualType ReductionType = GetTypeFromParser(ParsedType.get());
12375   if (ReductionType.isNull())
12376     return QualType();
12377 
12378   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12379   // A type name in a declare reduction directive cannot be a function type, an
12380   // array type, a reference type, or a type qualified with const, volatile or
12381   // restrict.
12382   if (ReductionType.hasQualifiers()) {
12383     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12384     return QualType();
12385   }
12386 
12387   if (ReductionType->isFunctionType()) {
12388     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12389     return QualType();
12390   }
12391   if (ReductionType->isReferenceType()) {
12392     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
12393     return QualType();
12394   }
12395   if (ReductionType->isArrayType()) {
12396     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
12397     return QualType();
12398   }
12399   return ReductionType;
12400 }
12401 
12402 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
12403     Scope *S, DeclContext *DC, DeclarationName Name,
12404     ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
12405     AccessSpecifier AS, Decl *PrevDeclInScope) {
12406   SmallVector<Decl *, 8> Decls;
12407   Decls.reserve(ReductionTypes.size());
12408 
12409   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
12410                       forRedeclarationInCurContext());
12411   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
12412   // A reduction-identifier may not be re-declared in the current scope for the
12413   // same type or for a type that is compatible according to the base language
12414   // rules.
12415   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
12416   OMPDeclareReductionDecl *PrevDRD = nullptr;
12417   bool InCompoundScope = true;
12418   if (S != nullptr) {
12419     // Find previous declaration with the same name not referenced in other
12420     // declarations.
12421     FunctionScopeInfo *ParentFn = getEnclosingFunction();
12422     InCompoundScope =
12423         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
12424     LookupName(Lookup, S);
12425     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
12426                          /*AllowInlineNamespace=*/false);
12427     llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
12428     auto Filter = Lookup.makeFilter();
12429     while (Filter.hasNext()) {
12430       auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
12431       if (InCompoundScope) {
12432         auto I = UsedAsPrevious.find(PrevDecl);
12433         if (I == UsedAsPrevious.end())
12434           UsedAsPrevious[PrevDecl] = false;
12435         if (auto *D = PrevDecl->getPrevDeclInScope())
12436           UsedAsPrevious[D] = true;
12437       }
12438       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
12439           PrevDecl->getLocation();
12440     }
12441     Filter.done();
12442     if (InCompoundScope) {
12443       for (auto &PrevData : UsedAsPrevious) {
12444         if (!PrevData.second) {
12445           PrevDRD = PrevData.first;
12446           break;
12447         }
12448       }
12449     }
12450   } else if (PrevDeclInScope != nullptr) {
12451     auto *PrevDRDInScope = PrevDRD =
12452         cast<OMPDeclareReductionDecl>(PrevDeclInScope);
12453     do {
12454       PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
12455           PrevDRDInScope->getLocation();
12456       PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
12457     } while (PrevDRDInScope != nullptr);
12458   }
12459   for (auto &TyData : ReductionTypes) {
12460     auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
12461     bool Invalid = false;
12462     if (I != PreviousRedeclTypes.end()) {
12463       Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
12464           << TyData.first;
12465       Diag(I->second, diag::note_previous_definition);
12466       Invalid = true;
12467     }
12468     PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
12469     auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
12470                                                 Name, TyData.first, PrevDRD);
12471     DC->addDecl(DRD);
12472     DRD->setAccess(AS);
12473     Decls.push_back(DRD);
12474     if (Invalid)
12475       DRD->setInvalidDecl();
12476     else
12477       PrevDRD = DRD;
12478   }
12479 
12480   return DeclGroupPtrTy::make(
12481       DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
12482 }
12483 
12484 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
12485   auto *DRD = cast<OMPDeclareReductionDecl>(D);
12486 
12487   // Enter new function scope.
12488   PushFunctionScope();
12489   setFunctionHasBranchProtectedScope();
12490   getCurFunction()->setHasOMPDeclareReductionCombiner();
12491 
12492   if (S != nullptr)
12493     PushDeclContext(S, DRD);
12494   else
12495     CurContext = DRD;
12496 
12497   PushExpressionEvaluationContext(
12498       ExpressionEvaluationContext::PotentiallyEvaluated);
12499 
12500   QualType ReductionType = DRD->getType();
12501   // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
12502   // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
12503   // uses semantics of argument handles by value, but it should be passed by
12504   // reference. C lang does not support references, so pass all parameters as
12505   // pointers.
12506   // Create 'T omp_in;' variable.
12507   auto *OmpInParm =
12508       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
12509   // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
12510   // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
12511   // uses semantics of argument handles by value, but it should be passed by
12512   // reference. C lang does not support references, so pass all parameters as
12513   // pointers.
12514   // Create 'T omp_out;' variable.
12515   auto *OmpOutParm =
12516       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
12517   if (S != nullptr) {
12518     PushOnScopeChains(OmpInParm, S);
12519     PushOnScopeChains(OmpOutParm, S);
12520   } else {
12521     DRD->addDecl(OmpInParm);
12522     DRD->addDecl(OmpOutParm);
12523   }
12524 }
12525 
12526 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
12527   auto *DRD = cast<OMPDeclareReductionDecl>(D);
12528   DiscardCleanupsInEvaluationContext();
12529   PopExpressionEvaluationContext();
12530 
12531   PopDeclContext();
12532   PopFunctionScopeInfo();
12533 
12534   if (Combiner != nullptr)
12535     DRD->setCombiner(Combiner);
12536   else
12537     DRD->setInvalidDecl();
12538 }
12539 
12540 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
12541   auto *DRD = cast<OMPDeclareReductionDecl>(D);
12542 
12543   // Enter new function scope.
12544   PushFunctionScope();
12545   setFunctionHasBranchProtectedScope();
12546 
12547   if (S != nullptr)
12548     PushDeclContext(S, DRD);
12549   else
12550     CurContext = DRD;
12551 
12552   PushExpressionEvaluationContext(
12553       ExpressionEvaluationContext::PotentiallyEvaluated);
12554 
12555   QualType ReductionType = DRD->getType();
12556   // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
12557   // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
12558   // uses semantics of argument handles by value, but it should be passed by
12559   // reference. C lang does not support references, so pass all parameters as
12560   // pointers.
12561   // Create 'T omp_priv;' variable.
12562   auto *OmpPrivParm =
12563       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
12564   // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
12565   // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
12566   // uses semantics of argument handles by value, but it should be passed by
12567   // reference. C lang does not support references, so pass all parameters as
12568   // pointers.
12569   // Create 'T omp_orig;' variable.
12570   auto *OmpOrigParm =
12571       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
12572   if (S != nullptr) {
12573     PushOnScopeChains(OmpPrivParm, S);
12574     PushOnScopeChains(OmpOrigParm, S);
12575   } else {
12576     DRD->addDecl(OmpPrivParm);
12577     DRD->addDecl(OmpOrigParm);
12578   }
12579   return OmpPrivParm;
12580 }
12581 
12582 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
12583                                                      VarDecl *OmpPrivParm) {
12584   auto *DRD = cast<OMPDeclareReductionDecl>(D);
12585   DiscardCleanupsInEvaluationContext();
12586   PopExpressionEvaluationContext();
12587 
12588   PopDeclContext();
12589   PopFunctionScopeInfo();
12590 
12591   if (Initializer != nullptr) {
12592     DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
12593   } else if (OmpPrivParm->hasInit()) {
12594     DRD->setInitializer(OmpPrivParm->getInit(),
12595                         OmpPrivParm->isDirectInit()
12596                             ? OMPDeclareReductionDecl::DirectInit
12597                             : OMPDeclareReductionDecl::CopyInit);
12598   } else {
12599     DRD->setInvalidDecl();
12600   }
12601 }
12602 
12603 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
12604     Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
12605   for (auto *D : DeclReductions.get()) {
12606     if (IsValid) {
12607       auto *DRD = cast<OMPDeclareReductionDecl>(D);
12608       if (S != nullptr)
12609         PushOnScopeChains(DRD, S, /*AddToContext=*/false);
12610     } else
12611       D->setInvalidDecl();
12612   }
12613   return DeclReductions;
12614 }
12615 
12616 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
12617                                            SourceLocation StartLoc,
12618                                            SourceLocation LParenLoc,
12619                                            SourceLocation EndLoc) {
12620   Expr *ValExpr = NumTeams;
12621   Stmt *HelperValStmt = nullptr;
12622 
12623   // OpenMP [teams Constrcut, Restrictions]
12624   // The num_teams expression must evaluate to a positive integer value.
12625   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
12626                                  /*StrictlyPositive=*/true))
12627     return nullptr;
12628 
12629   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
12630   OpenMPDirectiveKind CaptureRegion =
12631       getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
12632   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
12633     ValExpr = MakeFullExpr(ValExpr).get();
12634     llvm::MapVector<Expr *, DeclRefExpr *> Captures;
12635     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12636     HelperValStmt = buildPreInits(Context, Captures);
12637   }
12638 
12639   return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
12640                                          StartLoc, LParenLoc, EndLoc);
12641 }
12642 
12643 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
12644                                               SourceLocation StartLoc,
12645                                               SourceLocation LParenLoc,
12646                                               SourceLocation EndLoc) {
12647   Expr *ValExpr = ThreadLimit;
12648   Stmt *HelperValStmt = nullptr;
12649 
12650   // OpenMP [teams Constrcut, Restrictions]
12651   // The thread_limit expression must evaluate to a positive integer value.
12652   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
12653                                  /*StrictlyPositive=*/true))
12654     return nullptr;
12655 
12656   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
12657   OpenMPDirectiveKind CaptureRegion =
12658       getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
12659   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
12660     ValExpr = MakeFullExpr(ValExpr).get();
12661     llvm::MapVector<Expr *, DeclRefExpr *> Captures;
12662     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12663     HelperValStmt = buildPreInits(Context, Captures);
12664   }
12665 
12666   return new (Context) OMPThreadLimitClause(
12667       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
12668 }
12669 
12670 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
12671                                            SourceLocation StartLoc,
12672                                            SourceLocation LParenLoc,
12673                                            SourceLocation EndLoc) {
12674   Expr *ValExpr = Priority;
12675 
12676   // OpenMP [2.9.1, task Constrcut]
12677   // The priority-value is a non-negative numerical scalar expression.
12678   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
12679                                  /*StrictlyPositive=*/false))
12680     return nullptr;
12681 
12682   return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12683 }
12684 
12685 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
12686                                             SourceLocation StartLoc,
12687                                             SourceLocation LParenLoc,
12688                                             SourceLocation EndLoc) {
12689   Expr *ValExpr = Grainsize;
12690 
12691   // OpenMP [2.9.2, taskloop Constrcut]
12692   // The parameter of the grainsize clause must be a positive integer
12693   // expression.
12694   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
12695                                  /*StrictlyPositive=*/true))
12696     return nullptr;
12697 
12698   return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12699 }
12700 
12701 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
12702                                            SourceLocation StartLoc,
12703                                            SourceLocation LParenLoc,
12704                                            SourceLocation EndLoc) {
12705   Expr *ValExpr = NumTasks;
12706 
12707   // OpenMP [2.9.2, taskloop Constrcut]
12708   // The parameter of the num_tasks clause must be a positive integer
12709   // expression.
12710   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
12711                                  /*StrictlyPositive=*/true))
12712     return nullptr;
12713 
12714   return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12715 }
12716 
12717 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
12718                                        SourceLocation LParenLoc,
12719                                        SourceLocation EndLoc) {
12720   // OpenMP [2.13.2, critical construct, Description]
12721   // ... where hint-expression is an integer constant expression that evaluates
12722   // to a valid lock hint.
12723   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
12724   if (HintExpr.isInvalid())
12725     return nullptr;
12726   return new (Context)
12727       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
12728 }
12729 
12730 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
12731     OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
12732     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
12733     SourceLocation EndLoc) {
12734   if (Kind == OMPC_DIST_SCHEDULE_unknown) {
12735     std::string Values;
12736     Values += "'";
12737     Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
12738     Values += "'";
12739     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
12740         << Values << getOpenMPClauseName(OMPC_dist_schedule);
12741     return nullptr;
12742   }
12743   Expr *ValExpr = ChunkSize;
12744   Stmt *HelperValStmt = nullptr;
12745   if (ChunkSize) {
12746     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
12747         !ChunkSize->isInstantiationDependent() &&
12748         !ChunkSize->containsUnexpandedParameterPack()) {
12749       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
12750       ExprResult Val =
12751           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
12752       if (Val.isInvalid())
12753         return nullptr;
12754 
12755       ValExpr = Val.get();
12756 
12757       // OpenMP [2.7.1, Restrictions]
12758       //  chunk_size must be a loop invariant integer expression with a positive
12759       //  value.
12760       llvm::APSInt Result;
12761       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
12762         if (Result.isSigned() && !Result.isStrictlyPositive()) {
12763           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
12764               << "dist_schedule" << ChunkSize->getSourceRange();
12765           return nullptr;
12766         }
12767       } else if (getOpenMPCaptureRegionForClause(
12768                      DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
12769                      OMPD_unknown &&
12770                  !CurContext->isDependentContext()) {
12771         ValExpr = MakeFullExpr(ValExpr).get();
12772         llvm::MapVector<Expr *, DeclRefExpr *> Captures;
12773         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12774         HelperValStmt = buildPreInits(Context, Captures);
12775       }
12776     }
12777   }
12778 
12779   return new (Context)
12780       OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
12781                             Kind, ValExpr, HelperValStmt);
12782 }
12783 
12784 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
12785     OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
12786     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
12787     SourceLocation KindLoc, SourceLocation EndLoc) {
12788   // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
12789   if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
12790     std::string Value;
12791     SourceLocation Loc;
12792     Value += "'";
12793     if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
12794       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
12795                                              OMPC_DEFAULTMAP_MODIFIER_tofrom);
12796       Loc = MLoc;
12797     } else {
12798       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
12799                                              OMPC_DEFAULTMAP_scalar);
12800       Loc = KindLoc;
12801     }
12802     Value += "'";
12803     Diag(Loc, diag::err_omp_unexpected_clause_value)
12804         << Value << getOpenMPClauseName(OMPC_defaultmap);
12805     return nullptr;
12806   }
12807   DSAStack->setDefaultDMAToFromScalar(StartLoc);
12808 
12809   return new (Context)
12810       OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
12811 }
12812 
12813 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
12814   DeclContext *CurLexicalContext = getCurLexicalContext();
12815   if (!CurLexicalContext->isFileContext() &&
12816       !CurLexicalContext->isExternCContext() &&
12817       !CurLexicalContext->isExternCXXContext() &&
12818       !isa<CXXRecordDecl>(CurLexicalContext) &&
12819       !isa<ClassTemplateDecl>(CurLexicalContext) &&
12820       !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
12821       !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
12822     Diag(Loc, diag::err_omp_region_not_file_context);
12823     return false;
12824   }
12825   if (IsInOpenMPDeclareTargetContext) {
12826     Diag(Loc, diag::err_omp_enclosed_declare_target);
12827     return false;
12828   }
12829 
12830   IsInOpenMPDeclareTargetContext = true;
12831   return true;
12832 }
12833 
12834 void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
12835   assert(IsInOpenMPDeclareTargetContext &&
12836          "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
12837 
12838   IsInOpenMPDeclareTargetContext = false;
12839 }
12840 
12841 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
12842                                         CXXScopeSpec &ScopeSpec,
12843                                         const DeclarationNameInfo &Id,
12844                                         OMPDeclareTargetDeclAttr::MapTypeTy MT,
12845                                         NamedDeclSetType &SameDirectiveDecls) {
12846   LookupResult Lookup(*this, Id, LookupOrdinaryName);
12847   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
12848 
12849   if (Lookup.isAmbiguous())
12850     return;
12851   Lookup.suppressDiagnostics();
12852 
12853   if (!Lookup.isSingleResult()) {
12854     if (TypoCorrection Corrected =
12855             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
12856                         llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
12857                         CTK_ErrorRecovery)) {
12858       diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
12859                                   << Id.getName());
12860       checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
12861       return;
12862     }
12863 
12864     Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
12865     return;
12866   }
12867 
12868   NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
12869   if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
12870     if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
12871       Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
12872 
12873     if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
12874       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
12875       ND->addAttr(A);
12876       if (ASTMutationListener *ML = Context.getASTMutationListener())
12877         ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
12878       checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
12879     } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
12880       Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
12881           << Id.getName();
12882     }
12883   } else
12884     Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
12885 }
12886 
12887 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
12888                                      Sema &SemaRef, Decl *D) {
12889   if (!D)
12890     return;
12891   const Decl *LD = nullptr;
12892   if (isa<TagDecl>(D)) {
12893     LD = cast<TagDecl>(D)->getDefinition();
12894   } else if (isa<VarDecl>(D)) {
12895     LD = cast<VarDecl>(D)->getDefinition();
12896 
12897     // If this is an implicit variable that is legal and we do not need to do
12898     // anything.
12899     if (cast<VarDecl>(D)->isImplicit()) {
12900       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12901           SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12902       D->addAttr(A);
12903       if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
12904         ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
12905       return;
12906     }
12907   } else if (auto *F = dyn_cast<FunctionDecl>(D)) {
12908     const FunctionDecl *FD = nullptr;
12909     if (cast<FunctionDecl>(D)->hasBody(FD)) {
12910       LD = FD;
12911       // If the definition is associated with the current declaration in the
12912       // target region (it can be e.g. a lambda) that is legal and we do not
12913       // need to do anything else.
12914       if (LD == D) {
12915         Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12916             SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12917         D->addAttr(A);
12918         if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
12919           ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
12920         return;
12921       }
12922     } else if (F->isFunctionTemplateSpecialization() &&
12923                F->getTemplateSpecializationKind() ==
12924                    TSK_ImplicitInstantiation) {
12925       // Check if the function is implicitly instantiated from the template
12926       // defined in the declare target region.
12927       const FunctionTemplateDecl *FTD = F->getPrimaryTemplate();
12928       if (FTD && FTD->hasAttr<OMPDeclareTargetDeclAttr>())
12929         return;
12930     }
12931   }
12932   if (!LD)
12933     LD = D;
12934   if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
12935       (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
12936     // Outlined declaration is not declared target.
12937     if (LD->isOutOfLine()) {
12938       SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
12939       SemaRef.Diag(SL, diag::note_used_here) << SR;
12940     } else {
12941       const DeclContext *DC = LD->getDeclContext();
12942       while (DC) {
12943         if (isa<FunctionDecl>(DC) &&
12944             cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
12945           break;
12946         DC = DC->getParent();
12947       }
12948       if (DC)
12949         return;
12950 
12951       // Is not declared in target context.
12952       SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
12953       SemaRef.Diag(SL, diag::note_used_here) << SR;
12954     }
12955     // Mark decl as declared target to prevent further diagnostic.
12956     Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12957         SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12958     D->addAttr(A);
12959     if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
12960       ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
12961   }
12962 }
12963 
12964 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
12965                                    Sema &SemaRef, DSAStackTy *Stack,
12966                                    ValueDecl *VD) {
12967   if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
12968     return true;
12969   if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
12970                          /*FullCheck=*/false))
12971     return false;
12972   return true;
12973 }
12974 
12975 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
12976                                             SourceLocation IdLoc) {
12977   if (!D || D->isInvalidDecl())
12978     return;
12979   SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
12980   SourceLocation SL = E ? E->getLocStart() : D->getLocation();
12981   // 2.10.6: threadprivate variable cannot appear in a declare target directive.
12982   if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
12983     if (DSAStack->isThreadPrivate(VD)) {
12984       Diag(SL, diag::err_omp_threadprivate_in_target);
12985       ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
12986       return;
12987     }
12988   }
12989   if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
12990     // Problem if any with var declared with incomplete type will be reported
12991     // as normal, so no need to check it here.
12992     if ((E || !VD->getType()->isIncompleteType()) &&
12993         !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
12994       // Mark decl as declared target to prevent further diagnostic.
12995       if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD) ||
12996           isa<FunctionTemplateDecl>(VD)) {
12997         Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12998             Context, OMPDeclareTargetDeclAttr::MT_To);
12999         VD->addAttr(A);
13000         if (ASTMutationListener *ML = Context.getASTMutationListener())
13001           ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
13002       }
13003       return;
13004     }
13005   }
13006   if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
13007     if (FD->hasAttr<OMPDeclareTargetDeclAttr>() &&
13008         (FD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() ==
13009          OMPDeclareTargetDeclAttr::MT_Link)) {
13010       assert(IdLoc.isValid() && "Source location is expected");
13011       Diag(IdLoc, diag::err_omp_function_in_link_clause);
13012       Diag(FD->getLocation(), diag::note_defined_here) << FD;
13013       return;
13014     }
13015   }
13016   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) {
13017     if (FTD->hasAttr<OMPDeclareTargetDeclAttr>() &&
13018         (FTD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() ==
13019          OMPDeclareTargetDeclAttr::MT_Link)) {
13020       assert(IdLoc.isValid() && "Source location is expected");
13021       Diag(IdLoc, diag::err_omp_function_in_link_clause);
13022       Diag(FTD->getLocation(), diag::note_defined_here) << FTD;
13023       return;
13024     }
13025   }
13026   if (!E) {
13027     // Checking declaration inside declare target region.
13028     if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
13029         (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13030          isa<FunctionTemplateDecl>(D))) {
13031       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13032           Context, OMPDeclareTargetDeclAttr::MT_To);
13033       D->addAttr(A);
13034       if (ASTMutationListener *ML = Context.getASTMutationListener())
13035         ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13036     }
13037     return;
13038   }
13039   checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13040 }
13041 
13042 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13043                                      SourceLocation StartLoc,
13044                                      SourceLocation LParenLoc,
13045                                      SourceLocation EndLoc) {
13046   MappableVarListInfo MVLI(VarList);
13047   checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13048   if (MVLI.ProcessedVarList.empty())
13049     return nullptr;
13050 
13051   return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13052                              MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13053                              MVLI.VarComponents);
13054 }
13055 
13056 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13057                                        SourceLocation StartLoc,
13058                                        SourceLocation LParenLoc,
13059                                        SourceLocation EndLoc) {
13060   MappableVarListInfo MVLI(VarList);
13061   checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13062   if (MVLI.ProcessedVarList.empty())
13063     return nullptr;
13064 
13065   return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13066                                MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13067                                MVLI.VarComponents);
13068 }
13069 
13070 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13071                                                SourceLocation StartLoc,
13072                                                SourceLocation LParenLoc,
13073                                                SourceLocation EndLoc) {
13074   MappableVarListInfo MVLI(VarList);
13075   SmallVector<Expr *, 8> PrivateCopies;
13076   SmallVector<Expr *, 8> Inits;
13077 
13078   for (auto &RefExpr : VarList) {
13079     assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13080     SourceLocation ELoc;
13081     SourceRange ERange;
13082     Expr *SimpleRefExpr = RefExpr;
13083     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13084     if (Res.second) {
13085       // It will be analyzed later.
13086       MVLI.ProcessedVarList.push_back(RefExpr);
13087       PrivateCopies.push_back(nullptr);
13088       Inits.push_back(nullptr);
13089     }
13090     ValueDecl *D = Res.first;
13091     if (!D)
13092       continue;
13093 
13094     QualType Type = D->getType();
13095     Type = Type.getNonReferenceType().getUnqualifiedType();
13096 
13097     auto *VD = dyn_cast<VarDecl>(D);
13098 
13099     // Item should be a pointer or reference to pointer.
13100     if (!Type->isPointerType()) {
13101       Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13102           << 0 << RefExpr->getSourceRange();
13103       continue;
13104     }
13105 
13106     // Build the private variable and the expression that refers to it.
13107     auto VDPrivate =
13108         buildVarDecl(*this, ELoc, Type, D->getName(),
13109                      D->hasAttrs() ? &D->getAttrs() : nullptr,
13110                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
13111     if (VDPrivate->isInvalidDecl())
13112       continue;
13113 
13114     CurContext->addDecl(VDPrivate);
13115     auto VDPrivateRefExpr = buildDeclRefExpr(
13116         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13117 
13118     // Add temporary variable to initialize the private copy of the pointer.
13119     auto *VDInit =
13120         buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
13121     auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
13122                                            RefExpr->getExprLoc());
13123     AddInitializerToDecl(VDPrivate,
13124                          DefaultLvalueConversion(VDInitRefExpr).get(),
13125                          /*DirectInit=*/false);
13126 
13127     // If required, build a capture to implement the privatization initialized
13128     // with the current list item value.
13129     DeclRefExpr *Ref = nullptr;
13130     if (!VD)
13131       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13132     MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13133     PrivateCopies.push_back(VDPrivateRefExpr);
13134     Inits.push_back(VDInitRefExpr);
13135 
13136     // We need to add a data sharing attribute for this variable to make sure it
13137     // is correctly captured. A variable that shows up in a use_device_ptr has
13138     // similar properties of a first private variable.
13139     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13140 
13141     // Create a mappable component for the list item. List items in this clause
13142     // only need a component.
13143     MVLI.VarBaseDeclarations.push_back(D);
13144     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13145     MVLI.VarComponents.back().push_back(
13146         OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
13147   }
13148 
13149   if (MVLI.ProcessedVarList.empty())
13150     return nullptr;
13151 
13152   return OMPUseDevicePtrClause::Create(
13153       Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13154       PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
13155 }
13156 
13157 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13158                                               SourceLocation StartLoc,
13159                                               SourceLocation LParenLoc,
13160                                               SourceLocation EndLoc) {
13161   MappableVarListInfo MVLI(VarList);
13162   for (auto &RefExpr : VarList) {
13163     assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
13164     SourceLocation ELoc;
13165     SourceRange ERange;
13166     Expr *SimpleRefExpr = RefExpr;
13167     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13168     if (Res.second) {
13169       // It will be analyzed later.
13170       MVLI.ProcessedVarList.push_back(RefExpr);
13171     }
13172     ValueDecl *D = Res.first;
13173     if (!D)
13174       continue;
13175 
13176     QualType Type = D->getType();
13177     // item should be a pointer or array or reference to pointer or array
13178     if (!Type.getNonReferenceType()->isPointerType() &&
13179         !Type.getNonReferenceType()->isArrayType()) {
13180       Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13181           << 0 << RefExpr->getSourceRange();
13182       continue;
13183     }
13184 
13185     // Check if the declaration in the clause does not show up in any data
13186     // sharing attribute.
13187     auto DVar = DSAStack->getTopDSA(D, false);
13188     if (isOpenMPPrivate(DVar.CKind)) {
13189       Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13190           << getOpenMPClauseName(DVar.CKind)
13191           << getOpenMPClauseName(OMPC_is_device_ptr)
13192           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
13193       ReportOriginalDSA(*this, DSAStack, D, DVar);
13194       continue;
13195     }
13196 
13197     Expr *ConflictExpr;
13198     if (DSAStack->checkMappableExprComponentListsForDecl(
13199             D, /*CurrentRegionOnly=*/true,
13200             [&ConflictExpr](
13201                 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13202                 OpenMPClauseKind) -> bool {
13203               ConflictExpr = R.front().getAssociatedExpression();
13204               return true;
13205             })) {
13206       Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13207       Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13208           << ConflictExpr->getSourceRange();
13209       continue;
13210     }
13211 
13212     // Store the components in the stack so that they can be used to check
13213     // against other clauses later on.
13214     OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13215     DSAStack->addMappableExpressionComponents(
13216         D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13217 
13218     // Record the expression we've just processed.
13219     MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13220 
13221     // Create a mappable component for the list item. List items in this clause
13222     // only need a component. We use a null declaration to signal fields in
13223     // 'this'.
13224     assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13225             isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13226            "Unexpected device pointer expression!");
13227     MVLI.VarBaseDeclarations.push_back(
13228         isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13229     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13230     MVLI.VarComponents.back().push_back(MC);
13231   }
13232 
13233   if (MVLI.ProcessedVarList.empty())
13234     return nullptr;
13235 
13236   return OMPIsDevicePtrClause::Create(
13237       Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13238       MVLI.VarBaseDeclarations, MVLI.VarComponents);
13239 }
13240